Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2015-2017, NVIDIA CORPORATION.  All rights reserved.
0004  *
0005  * Author:
0006  *  Mikko Perttunen <mperttunen@nvidia.com>
0007  *  Aapo Vienamo    <avienamo@nvidia.com>
0008  */
0009 
0010 #include <linux/err.h>
0011 #include <linux/module.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/thermal.h>
0014 #include <linux/workqueue.h>
0015 
0016 #include <soc/tegra/bpmp.h>
0017 #include <soc/tegra/bpmp-abi.h>
0018 
0019 struct tegra_bpmp_thermal_zone {
0020     struct tegra_bpmp_thermal *tegra;
0021     struct thermal_zone_device *tzd;
0022     struct work_struct tz_device_update_work;
0023     unsigned int idx;
0024 };
0025 
0026 struct tegra_bpmp_thermal {
0027     struct device *dev;
0028     struct tegra_bpmp *bpmp;
0029     unsigned int num_zones;
0030     struct tegra_bpmp_thermal_zone **zones;
0031 };
0032 
0033 static int tegra_bpmp_thermal_get_temp(void *data, int *out_temp)
0034 {
0035     struct tegra_bpmp_thermal_zone *zone = data;
0036     struct mrq_thermal_host_to_bpmp_request req;
0037     union mrq_thermal_bpmp_to_host_response reply;
0038     struct tegra_bpmp_message msg;
0039     int err;
0040 
0041     memset(&req, 0, sizeof(req));
0042     req.type = CMD_THERMAL_GET_TEMP;
0043     req.get_temp.zone = zone->idx;
0044 
0045     memset(&msg, 0, sizeof(msg));
0046     msg.mrq = MRQ_THERMAL;
0047     msg.tx.data = &req;
0048     msg.tx.size = sizeof(req);
0049     msg.rx.data = &reply;
0050     msg.rx.size = sizeof(reply);
0051 
0052     err = tegra_bpmp_transfer(zone->tegra->bpmp, &msg);
0053     if (err)
0054         return err;
0055     if (msg.rx.ret)
0056         return -EINVAL;
0057 
0058     *out_temp = reply.get_temp.temp;
0059 
0060     return 0;
0061 }
0062 
0063 static int tegra_bpmp_thermal_set_trips(void *data, int low, int high)
0064 {
0065     struct tegra_bpmp_thermal_zone *zone = data;
0066     struct mrq_thermal_host_to_bpmp_request req;
0067     struct tegra_bpmp_message msg;
0068     int err;
0069 
0070     memset(&req, 0, sizeof(req));
0071     req.type = CMD_THERMAL_SET_TRIP;
0072     req.set_trip.zone = zone->idx;
0073     req.set_trip.enabled = true;
0074     req.set_trip.low = low;
0075     req.set_trip.high = high;
0076 
0077     memset(&msg, 0, sizeof(msg));
0078     msg.mrq = MRQ_THERMAL;
0079     msg.tx.data = &req;
0080     msg.tx.size = sizeof(req);
0081 
0082     err = tegra_bpmp_transfer(zone->tegra->bpmp, &msg);
0083     if (err)
0084         return err;
0085     if (msg.rx.ret)
0086         return -EINVAL;
0087 
0088     return 0;
0089 }
0090 
0091 static void tz_device_update_work_fn(struct work_struct *work)
0092 {
0093     struct tegra_bpmp_thermal_zone *zone;
0094 
0095     zone = container_of(work, struct tegra_bpmp_thermal_zone,
0096                 tz_device_update_work);
0097 
0098     thermal_zone_device_update(zone->tzd, THERMAL_TRIP_VIOLATED);
0099 }
0100 
0101 static void bpmp_mrq_thermal(unsigned int mrq, struct tegra_bpmp_channel *ch,
0102                  void *data)
0103 {
0104     struct mrq_thermal_bpmp_to_host_request *req;
0105     struct tegra_bpmp_thermal *tegra = data;
0106     int i;
0107 
0108     req = (struct mrq_thermal_bpmp_to_host_request *)ch->ib->data;
0109 
0110     if (req->type != CMD_THERMAL_HOST_TRIP_REACHED) {
0111         dev_err(tegra->dev, "%s: invalid request type: %d\n",
0112             __func__, req->type);
0113         tegra_bpmp_mrq_return(ch, -EINVAL, NULL, 0);
0114         return;
0115     }
0116 
0117     for (i = 0; i < tegra->num_zones; ++i) {
0118         if (tegra->zones[i]->idx != req->host_trip_reached.zone)
0119             continue;
0120 
0121         schedule_work(&tegra->zones[i]->tz_device_update_work);
0122         tegra_bpmp_mrq_return(ch, 0, NULL, 0);
0123         return;
0124     }
0125 
0126     dev_err(tegra->dev, "%s: invalid thermal zone: %d\n", __func__,
0127         req->host_trip_reached.zone);
0128     tegra_bpmp_mrq_return(ch, -EINVAL, NULL, 0);
0129 }
0130 
0131 static int tegra_bpmp_thermal_get_num_zones(struct tegra_bpmp *bpmp,
0132                         int *num_zones)
0133 {
0134     struct mrq_thermal_host_to_bpmp_request req;
0135     union mrq_thermal_bpmp_to_host_response reply;
0136     struct tegra_bpmp_message msg;
0137     int err;
0138 
0139     memset(&req, 0, sizeof(req));
0140     req.type = CMD_THERMAL_GET_NUM_ZONES;
0141 
0142     memset(&msg, 0, sizeof(msg));
0143     msg.mrq = MRQ_THERMAL;
0144     msg.tx.data = &req;
0145     msg.tx.size = sizeof(req);
0146     msg.rx.data = &reply;
0147     msg.rx.size = sizeof(reply);
0148 
0149     err = tegra_bpmp_transfer(bpmp, &msg);
0150     if (err)
0151         return err;
0152     if (msg.rx.ret)
0153         return -EINVAL;
0154 
0155     *num_zones = reply.get_num_zones.num;
0156 
0157     return 0;
0158 }
0159 
0160 static const struct thermal_zone_of_device_ops tegra_bpmp_of_thermal_ops = {
0161     .get_temp = tegra_bpmp_thermal_get_temp,
0162     .set_trips = tegra_bpmp_thermal_set_trips,
0163 };
0164 
0165 static int tegra_bpmp_thermal_probe(struct platform_device *pdev)
0166 {
0167     struct tegra_bpmp *bpmp = dev_get_drvdata(pdev->dev.parent);
0168     struct tegra_bpmp_thermal *tegra;
0169     struct thermal_zone_device *tzd;
0170     unsigned int i, max_num_zones;
0171     int err;
0172 
0173     tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
0174     if (!tegra)
0175         return -ENOMEM;
0176 
0177     tegra->dev = &pdev->dev;
0178     tegra->bpmp = bpmp;
0179 
0180     err = tegra_bpmp_thermal_get_num_zones(bpmp, &max_num_zones);
0181     if (err) {
0182         dev_err(&pdev->dev, "failed to get the number of zones: %d\n",
0183             err);
0184         return err;
0185     }
0186 
0187     tegra->zones = devm_kcalloc(&pdev->dev, max_num_zones,
0188                     sizeof(*tegra->zones), GFP_KERNEL);
0189     if (!tegra->zones)
0190         return -ENOMEM;
0191 
0192     for (i = 0; i < max_num_zones; ++i) {
0193         struct tegra_bpmp_thermal_zone *zone;
0194         int temp;
0195 
0196         zone = devm_kzalloc(&pdev->dev, sizeof(*zone), GFP_KERNEL);
0197         if (!zone)
0198             return -ENOMEM;
0199 
0200         zone->idx = i;
0201         zone->tegra = tegra;
0202 
0203         err = tegra_bpmp_thermal_get_temp(zone, &temp);
0204         if (err < 0) {
0205             devm_kfree(&pdev->dev, zone);
0206             continue;
0207         }
0208 
0209         tzd = devm_thermal_zone_of_sensor_register(
0210             &pdev->dev, i, zone, &tegra_bpmp_of_thermal_ops);
0211         if (IS_ERR(tzd)) {
0212             if (PTR_ERR(tzd) == -EPROBE_DEFER)
0213                 return -EPROBE_DEFER;
0214             devm_kfree(&pdev->dev, zone);
0215             continue;
0216         }
0217 
0218         zone->tzd = tzd;
0219         INIT_WORK(&zone->tz_device_update_work,
0220               tz_device_update_work_fn);
0221 
0222         tegra->zones[tegra->num_zones++] = zone;
0223     }
0224 
0225     err = tegra_bpmp_request_mrq(bpmp, MRQ_THERMAL, bpmp_mrq_thermal,
0226                      tegra);
0227     if (err) {
0228         dev_err(&pdev->dev, "failed to register mrq handler: %d\n",
0229             err);
0230         return err;
0231     }
0232 
0233     platform_set_drvdata(pdev, tegra);
0234 
0235     return 0;
0236 }
0237 
0238 static int tegra_bpmp_thermal_remove(struct platform_device *pdev)
0239 {
0240     struct tegra_bpmp_thermal *tegra = platform_get_drvdata(pdev);
0241 
0242     tegra_bpmp_free_mrq(tegra->bpmp, MRQ_THERMAL, tegra);
0243 
0244     return 0;
0245 }
0246 
0247 static const struct of_device_id tegra_bpmp_thermal_of_match[] = {
0248     { .compatible = "nvidia,tegra186-bpmp-thermal" },
0249     { },
0250 };
0251 MODULE_DEVICE_TABLE(of, tegra_bpmp_thermal_of_match);
0252 
0253 static struct platform_driver tegra_bpmp_thermal_driver = {
0254     .probe = tegra_bpmp_thermal_probe,
0255     .remove = tegra_bpmp_thermal_remove,
0256     .driver = {
0257         .name = "tegra-bpmp-thermal",
0258         .of_match_table = tegra_bpmp_thermal_of_match,
0259     },
0260 };
0261 module_platform_driver(tegra_bpmp_thermal_driver);
0262 
0263 MODULE_AUTHOR("Mikko Perttunen <mperttunen@nvidia.com>");
0264 MODULE_DESCRIPTION("NVIDIA Tegra BPMP thermal sensor driver");
0265 MODULE_LICENSE("GPL v2");