1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2015-2017, NVIDIA CORPORATION. All rights reserved. 4 * 5 * Author: 6 * Mikko Perttunen <mperttunen@nvidia.com> 7 * Aapo Vienamo <avienamo@nvidia.com> 8 */ 9 10 #include <linux/err.h> 11 #include <linux/module.h> 12 #include <linux/platform_device.h> 13 #include <linux/thermal.h> 14 #include <linux/workqueue.h> 15 16 #include <soc/tegra/bpmp.h> 17 #include <soc/tegra/bpmp-abi.h> 18 19 struct tegra_bpmp_thermal_zone { 20 struct tegra_bpmp_thermal *tegra; 21 struct thermal_zone_device *tzd; 22 struct work_struct tz_device_update_work; 23 unsigned int idx; 24 }; 25 26 struct tegra_bpmp_thermal { 27 struct device *dev; 28 struct tegra_bpmp *bpmp; 29 unsigned int num_zones; 30 struct tegra_bpmp_thermal_zone **zones; 31 }; 32 33 static int __tegra_bpmp_thermal_get_temp(struct tegra_bpmp_thermal_zone *zone, 34 int *out_temp) 35 { 36 struct mrq_thermal_host_to_bpmp_request req; 37 union mrq_thermal_bpmp_to_host_response reply; 38 struct tegra_bpmp_message msg; 39 int err; 40 41 memset(&req, 0, sizeof(req)); 42 req.type = CMD_THERMAL_GET_TEMP; 43 req.get_temp.zone = zone->idx; 44 45 memset(&msg, 0, sizeof(msg)); 46 msg.mrq = MRQ_THERMAL; 47 msg.tx.data = &req; 48 msg.tx.size = sizeof(req); 49 msg.rx.data = &reply; 50 msg.rx.size = sizeof(reply); 51 52 err = tegra_bpmp_transfer(zone->tegra->bpmp, &msg); 53 if (err) 54 return err; 55 if (msg.rx.ret) 56 return -EINVAL; 57 58 *out_temp = reply.get_temp.temp; 59 60 return 0; 61 } 62 63 static int tegra_bpmp_thermal_get_temp(struct thermal_zone_device *tz, int *out_temp) 64 { 65 return __tegra_bpmp_thermal_get_temp(tz->devdata, out_temp); 66 } 67 68 static int tegra_bpmp_thermal_set_trips(struct thermal_zone_device *tz, int low, int high) 69 { 70 struct tegra_bpmp_thermal_zone *zone = tz->devdata; 71 struct mrq_thermal_host_to_bpmp_request req; 72 struct tegra_bpmp_message msg; 73 int err; 74 75 memset(&req, 0, sizeof(req)); 76 req.type = CMD_THERMAL_SET_TRIP; 77 req.set_trip.zone = zone->idx; 78 req.set_trip.enabled = true; 79 req.set_trip.low = low; 80 req.set_trip.high = high; 81 82 memset(&msg, 0, sizeof(msg)); 83 msg.mrq = MRQ_THERMAL; 84 msg.tx.data = &req; 85 msg.tx.size = sizeof(req); 86 87 err = tegra_bpmp_transfer(zone->tegra->bpmp, &msg); 88 if (err) 89 return err; 90 if (msg.rx.ret) 91 return -EINVAL; 92 93 return 0; 94 } 95 96 static void tz_device_update_work_fn(struct work_struct *work) 97 { 98 struct tegra_bpmp_thermal_zone *zone; 99 100 zone = container_of(work, struct tegra_bpmp_thermal_zone, 101 tz_device_update_work); 102 103 thermal_zone_device_update(zone->tzd, THERMAL_TRIP_VIOLATED); 104 } 105 106 static void bpmp_mrq_thermal(unsigned int mrq, struct tegra_bpmp_channel *ch, 107 void *data) 108 { 109 struct mrq_thermal_bpmp_to_host_request *req; 110 struct tegra_bpmp_thermal *tegra = data; 111 int i; 112 113 req = (struct mrq_thermal_bpmp_to_host_request *)ch->ib->data; 114 115 if (req->type != CMD_THERMAL_HOST_TRIP_REACHED) { 116 dev_err(tegra->dev, "%s: invalid request type: %d\n", 117 __func__, req->type); 118 tegra_bpmp_mrq_return(ch, -EINVAL, NULL, 0); 119 return; 120 } 121 122 for (i = 0; i < tegra->num_zones; ++i) { 123 if (tegra->zones[i]->idx != req->host_trip_reached.zone) 124 continue; 125 126 schedule_work(&tegra->zones[i]->tz_device_update_work); 127 tegra_bpmp_mrq_return(ch, 0, NULL, 0); 128 return; 129 } 130 131 dev_err(tegra->dev, "%s: invalid thermal zone: %d\n", __func__, 132 req->host_trip_reached.zone); 133 tegra_bpmp_mrq_return(ch, -EINVAL, NULL, 0); 134 } 135 136 static int tegra_bpmp_thermal_get_num_zones(struct tegra_bpmp *bpmp, 137 int *num_zones) 138 { 139 struct mrq_thermal_host_to_bpmp_request req; 140 union mrq_thermal_bpmp_to_host_response reply; 141 struct tegra_bpmp_message msg; 142 int err; 143 144 memset(&req, 0, sizeof(req)); 145 req.type = CMD_THERMAL_GET_NUM_ZONES; 146 147 memset(&msg, 0, sizeof(msg)); 148 msg.mrq = MRQ_THERMAL; 149 msg.tx.data = &req; 150 msg.tx.size = sizeof(req); 151 msg.rx.data = &reply; 152 msg.rx.size = sizeof(reply); 153 154 err = tegra_bpmp_transfer(bpmp, &msg); 155 if (err) 156 return err; 157 if (msg.rx.ret) 158 return -EINVAL; 159 160 *num_zones = reply.get_num_zones.num; 161 162 return 0; 163 } 164 165 static const struct thermal_zone_device_ops tegra_bpmp_of_thermal_ops = { 166 .get_temp = tegra_bpmp_thermal_get_temp, 167 .set_trips = tegra_bpmp_thermal_set_trips, 168 }; 169 170 static int tegra_bpmp_thermal_probe(struct platform_device *pdev) 171 { 172 struct tegra_bpmp *bpmp = dev_get_drvdata(pdev->dev.parent); 173 struct tegra_bpmp_thermal *tegra; 174 struct thermal_zone_device *tzd; 175 unsigned int i, max_num_zones; 176 int err; 177 178 tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL); 179 if (!tegra) 180 return -ENOMEM; 181 182 tegra->dev = &pdev->dev; 183 tegra->bpmp = bpmp; 184 185 err = tegra_bpmp_thermal_get_num_zones(bpmp, &max_num_zones); 186 if (err) { 187 dev_err(&pdev->dev, "failed to get the number of zones: %d\n", 188 err); 189 return err; 190 } 191 192 tegra->zones = devm_kcalloc(&pdev->dev, max_num_zones, 193 sizeof(*tegra->zones), GFP_KERNEL); 194 if (!tegra->zones) 195 return -ENOMEM; 196 197 for (i = 0; i < max_num_zones; ++i) { 198 struct tegra_bpmp_thermal_zone *zone; 199 int temp; 200 201 zone = devm_kzalloc(&pdev->dev, sizeof(*zone), GFP_KERNEL); 202 if (!zone) 203 return -ENOMEM; 204 205 zone->idx = i; 206 zone->tegra = tegra; 207 208 err = __tegra_bpmp_thermal_get_temp(zone, &temp); 209 if (err < 0) { 210 devm_kfree(&pdev->dev, zone); 211 continue; 212 } 213 214 tzd = devm_thermal_of_zone_register( 215 &pdev->dev, i, zone, &tegra_bpmp_of_thermal_ops); 216 if (IS_ERR(tzd)) { 217 if (PTR_ERR(tzd) == -EPROBE_DEFER) 218 return -EPROBE_DEFER; 219 devm_kfree(&pdev->dev, zone); 220 continue; 221 } 222 223 zone->tzd = tzd; 224 INIT_WORK(&zone->tz_device_update_work, 225 tz_device_update_work_fn); 226 227 tegra->zones[tegra->num_zones++] = zone; 228 } 229 230 err = tegra_bpmp_request_mrq(bpmp, MRQ_THERMAL, bpmp_mrq_thermal, 231 tegra); 232 if (err) { 233 dev_err(&pdev->dev, "failed to register mrq handler: %d\n", 234 err); 235 return err; 236 } 237 238 platform_set_drvdata(pdev, tegra); 239 240 return 0; 241 } 242 243 static int tegra_bpmp_thermal_remove(struct platform_device *pdev) 244 { 245 struct tegra_bpmp_thermal *tegra = platform_get_drvdata(pdev); 246 247 tegra_bpmp_free_mrq(tegra->bpmp, MRQ_THERMAL, tegra); 248 249 return 0; 250 } 251 252 static const struct of_device_id tegra_bpmp_thermal_of_match[] = { 253 { .compatible = "nvidia,tegra186-bpmp-thermal" }, 254 { }, 255 }; 256 MODULE_DEVICE_TABLE(of, tegra_bpmp_thermal_of_match); 257 258 static struct platform_driver tegra_bpmp_thermal_driver = { 259 .probe = tegra_bpmp_thermal_probe, 260 .remove = tegra_bpmp_thermal_remove, 261 .driver = { 262 .name = "tegra-bpmp-thermal", 263 .of_match_table = tegra_bpmp_thermal_of_match, 264 }, 265 }; 266 module_platform_driver(tegra_bpmp_thermal_driver); 267 268 MODULE_AUTHOR("Mikko Perttunen <mperttunen@nvidia.com>"); 269 MODULE_DESCRIPTION("NVIDIA Tegra BPMP thermal sensor driver"); 270 MODULE_LICENSE("GPL v2"); 271