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 size_t offset; 112 int i; 113 114 offset = offsetof(struct tegra_bpmp_mb_data, data); 115 iosys_map_memcpy_from(&req, &ch->ib, offset, sizeof(req)); 116 117 if (req.type != CMD_THERMAL_HOST_TRIP_REACHED) { 118 dev_err(tegra->dev, "%s: invalid request type: %d\n", __func__, req.type); 119 tegra_bpmp_mrq_return(ch, -EINVAL, NULL, 0); 120 return; 121 } 122 123 for (i = 0; i < tegra->num_zones; ++i) { 124 if (tegra->zones[i]->idx != req.host_trip_reached.zone) 125 continue; 126 127 schedule_work(&tegra->zones[i]->tz_device_update_work); 128 tegra_bpmp_mrq_return(ch, 0, NULL, 0); 129 return; 130 } 131 132 dev_err(tegra->dev, "%s: invalid thermal zone: %d\n", __func__, 133 req.host_trip_reached.zone); 134 tegra_bpmp_mrq_return(ch, -EINVAL, NULL, 0); 135 } 136 137 static int tegra_bpmp_thermal_get_num_zones(struct tegra_bpmp *bpmp, 138 int *num_zones) 139 { 140 struct mrq_thermal_host_to_bpmp_request req; 141 union mrq_thermal_bpmp_to_host_response reply; 142 struct tegra_bpmp_message msg; 143 int err; 144 145 memset(&req, 0, sizeof(req)); 146 req.type = CMD_THERMAL_GET_NUM_ZONES; 147 148 memset(&msg, 0, sizeof(msg)); 149 msg.mrq = MRQ_THERMAL; 150 msg.tx.data = &req; 151 msg.tx.size = sizeof(req); 152 msg.rx.data = &reply; 153 msg.rx.size = sizeof(reply); 154 155 err = tegra_bpmp_transfer(bpmp, &msg); 156 if (err) 157 return err; 158 if (msg.rx.ret) 159 return -EINVAL; 160 161 *num_zones = reply.get_num_zones.num; 162 163 return 0; 164 } 165 166 static const struct thermal_zone_device_ops tegra_bpmp_of_thermal_ops = { 167 .get_temp = tegra_bpmp_thermal_get_temp, 168 .set_trips = tegra_bpmp_thermal_set_trips, 169 }; 170 171 static int tegra_bpmp_thermal_probe(struct platform_device *pdev) 172 { 173 struct tegra_bpmp *bpmp = dev_get_drvdata(pdev->dev.parent); 174 struct tegra_bpmp_thermal *tegra; 175 struct thermal_zone_device *tzd; 176 unsigned int i, max_num_zones; 177 int err; 178 179 tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL); 180 if (!tegra) 181 return -ENOMEM; 182 183 tegra->dev = &pdev->dev; 184 tegra->bpmp = bpmp; 185 186 err = tegra_bpmp_thermal_get_num_zones(bpmp, &max_num_zones); 187 if (err) { 188 dev_err(&pdev->dev, "failed to get the number of zones: %d\n", 189 err); 190 return err; 191 } 192 193 tegra->zones = devm_kcalloc(&pdev->dev, max_num_zones, 194 sizeof(*tegra->zones), GFP_KERNEL); 195 if (!tegra->zones) 196 return -ENOMEM; 197 198 for (i = 0; i < max_num_zones; ++i) { 199 struct tegra_bpmp_thermal_zone *zone; 200 int temp; 201 202 zone = devm_kzalloc(&pdev->dev, sizeof(*zone), GFP_KERNEL); 203 if (!zone) 204 return -ENOMEM; 205 206 zone->idx = i; 207 zone->tegra = tegra; 208 209 err = __tegra_bpmp_thermal_get_temp(zone, &temp); 210 if (err < 0) { 211 devm_kfree(&pdev->dev, zone); 212 continue; 213 } 214 215 tzd = devm_thermal_of_zone_register( 216 &pdev->dev, i, zone, &tegra_bpmp_of_thermal_ops); 217 if (IS_ERR(tzd)) { 218 if (PTR_ERR(tzd) == -EPROBE_DEFER) 219 return -EPROBE_DEFER; 220 devm_kfree(&pdev->dev, zone); 221 continue; 222 } 223 224 zone->tzd = tzd; 225 INIT_WORK(&zone->tz_device_update_work, 226 tz_device_update_work_fn); 227 228 tegra->zones[tegra->num_zones++] = zone; 229 } 230 231 err = tegra_bpmp_request_mrq(bpmp, MRQ_THERMAL, bpmp_mrq_thermal, 232 tegra); 233 if (err) { 234 dev_err(&pdev->dev, "failed to register mrq handler: %d\n", 235 err); 236 return err; 237 } 238 239 platform_set_drvdata(pdev, tegra); 240 241 return 0; 242 } 243 244 static int tegra_bpmp_thermal_remove(struct platform_device *pdev) 245 { 246 struct tegra_bpmp_thermal *tegra = platform_get_drvdata(pdev); 247 248 tegra_bpmp_free_mrq(tegra->bpmp, MRQ_THERMAL, tegra); 249 250 return 0; 251 } 252 253 static const struct of_device_id tegra_bpmp_thermal_of_match[] = { 254 { .compatible = "nvidia,tegra186-bpmp-thermal" }, 255 { }, 256 }; 257 MODULE_DEVICE_TABLE(of, tegra_bpmp_thermal_of_match); 258 259 static struct platform_driver tegra_bpmp_thermal_driver = { 260 .probe = tegra_bpmp_thermal_probe, 261 .remove = tegra_bpmp_thermal_remove, 262 .driver = { 263 .name = "tegra-bpmp-thermal", 264 .of_match_table = tegra_bpmp_thermal_of_match, 265 }, 266 }; 267 module_platform_driver(tegra_bpmp_thermal_driver); 268 269 MODULE_AUTHOR("Mikko Perttunen <mperttunen@nvidia.com>"); 270 MODULE_DESCRIPTION("NVIDIA Tegra BPMP thermal sensor driver"); 271 MODULE_LICENSE("GPL v2"); 272