Lina Iyer | 7e3c038 | 2018-05-07 11:52:29 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 2 | /* |
| 3 | * of-thermal.c - Generic Thermal Management device tree support. |
| 4 | * |
| 5 | * Copyright (C) 2013 Texas Instruments |
| 6 | * Copyright (C) 2013 Eduardo Valentin <eduardo.valentin@ti.com> |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 7 | */ |
Yangtao Li | 7ffd87c | 2019-02-24 02:10:58 -0500 | [diff] [blame] | 8 | |
| 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 11 | #include <linux/thermal.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/of_device.h> |
| 15 | #include <linux/of_platform.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/export.h> |
| 18 | #include <linux/string.h> |
| 19 | |
| 20 | #include "thermal_core.h" |
| 21 | |
| 22 | /*** Private data structures to represent thermal device tree data ***/ |
| 23 | |
| 24 | /** |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 25 | * struct __thermal_cooling_bind_param - a cooling device for a trip point |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 26 | * @cooling_device: a pointer to identify the referred cooling device |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 27 | * @min: minimum cooling state used at this trip point |
| 28 | * @max: maximum cooling state used at this trip point |
| 29 | */ |
| 30 | |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 31 | struct __thermal_cooling_bind_param { |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 32 | struct device_node *cooling_device; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 33 | unsigned long min; |
| 34 | unsigned long max; |
| 35 | }; |
| 36 | |
| 37 | /** |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 38 | * struct __thermal_bind_param - a match between trip and cooling device |
| 39 | * @tcbp: a pointer to an array of cooling devices |
| 40 | * @count: number of elements in array |
| 41 | * @trip_id: the trip point index |
| 42 | * @usage: the percentage (from 0 to 100) of cooling contribution |
| 43 | */ |
| 44 | |
| 45 | struct __thermal_bind_params { |
| 46 | struct __thermal_cooling_bind_param *tcbp; |
| 47 | unsigned int count; |
| 48 | unsigned int trip_id; |
| 49 | unsigned int usage; |
| 50 | }; |
| 51 | |
| 52 | /** |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 53 | * struct __thermal_zone - internal representation of a thermal zone |
| 54 | * @mode: current thermal zone device mode (enabled/disabled) |
| 55 | * @passive_delay: polling interval while passive cooling is activated |
| 56 | * @polling_delay: zone polling interval |
Eduardo Valentin | a46dbae | 2015-05-11 19:48:09 -0700 | [diff] [blame] | 57 | * @slope: slope of the temperature adjustment curve |
| 58 | * @offset: offset of the temperature adjustment curve |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 59 | * @ntrips: number of trip points |
| 60 | * @trips: an array of trip points (0..ntrips - 1) |
| 61 | * @num_tbps: number of thermal bind params |
| 62 | * @tbps: an array of thermal bind params (0..num_tbps - 1) |
| 63 | * @sensor_data: sensor private data used while reading temperature and trend |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 64 | * @ops: set of callbacks to handle the thermal zone based on DT |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 65 | */ |
| 66 | |
| 67 | struct __thermal_zone { |
| 68 | enum thermal_device_mode mode; |
| 69 | int passive_delay; |
| 70 | int polling_delay; |
Eduardo Valentin | a46dbae | 2015-05-11 19:48:09 -0700 | [diff] [blame] | 71 | int slope; |
| 72 | int offset; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 73 | |
| 74 | /* trip data */ |
| 75 | int ntrips; |
Lukasz Majewski | ad9914a | 2014-12-08 18:04:19 +0100 | [diff] [blame] | 76 | struct thermal_trip *trips; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 77 | |
| 78 | /* cooling binding data */ |
| 79 | int num_tbps; |
| 80 | struct __thermal_bind_params *tbps; |
| 81 | |
| 82 | /* sensor interface */ |
| 83 | void *sensor_data; |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 84 | const struct thermal_zone_of_device_ops *ops; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | /*** DT thermal zone device callbacks ***/ |
| 88 | |
| 89 | static int of_thermal_get_temp(struct thermal_zone_device *tz, |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 90 | int *temp) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 91 | { |
| 92 | struct __thermal_zone *data = tz->devdata; |
| 93 | |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 94 | if (!data->ops->get_temp) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 95 | return -EINVAL; |
| 96 | |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 97 | return data->ops->get_temp(data->sensor_data, temp); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 98 | } |
| 99 | |
Sascha Hauer | 826386e | 2016-06-22 16:42:02 +0800 | [diff] [blame] | 100 | static int of_thermal_set_trips(struct thermal_zone_device *tz, |
| 101 | int low, int high) |
| 102 | { |
| 103 | struct __thermal_zone *data = tz->devdata; |
| 104 | |
| 105 | if (!data->ops || !data->ops->set_trips) |
| 106 | return -EINVAL; |
| 107 | |
| 108 | return data->ops->set_trips(data->sensor_data, low, high); |
| 109 | } |
| 110 | |
Lukasz Majewski | 08dab66 | 2014-12-08 18:04:17 +0100 | [diff] [blame] | 111 | /** |
| 112 | * of_thermal_get_ntrips - function to export number of available trip |
| 113 | * points. |
| 114 | * @tz: pointer to a thermal zone |
| 115 | * |
| 116 | * This function is a globally visible wrapper to get number of trip points |
| 117 | * stored in the local struct __thermal_zone |
| 118 | * |
| 119 | * Return: number of available trip points, -ENODEV when data not available |
| 120 | */ |
| 121 | int of_thermal_get_ntrips(struct thermal_zone_device *tz) |
| 122 | { |
| 123 | struct __thermal_zone *data = tz->devdata; |
| 124 | |
| 125 | if (!data || IS_ERR(data)) |
| 126 | return -ENODEV; |
| 127 | |
| 128 | return data->ntrips; |
| 129 | } |
| 130 | EXPORT_SYMBOL_GPL(of_thermal_get_ntrips); |
| 131 | |
Lukasz Majewski | a9bf2cc | 2014-12-08 18:04:18 +0100 | [diff] [blame] | 132 | /** |
| 133 | * of_thermal_is_trip_valid - function to check if trip point is valid |
| 134 | * |
| 135 | * @tz: pointer to a thermal zone |
| 136 | * @trip: trip point to evaluate |
| 137 | * |
| 138 | * This function is responsible for checking if passed trip point is valid |
| 139 | * |
| 140 | * Return: true if trip point is valid, false otherwise |
| 141 | */ |
| 142 | bool of_thermal_is_trip_valid(struct thermal_zone_device *tz, int trip) |
| 143 | { |
| 144 | struct __thermal_zone *data = tz->devdata; |
| 145 | |
| 146 | if (!data || trip >= data->ntrips || trip < 0) |
| 147 | return false; |
| 148 | |
| 149 | return true; |
| 150 | } |
| 151 | EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid); |
| 152 | |
Lukasz Majewski | ce8be77 | 2014-12-08 18:04:20 +0100 | [diff] [blame] | 153 | /** |
| 154 | * of_thermal_get_trip_points - function to get access to a globally exported |
| 155 | * trip points |
| 156 | * |
| 157 | * @tz: pointer to a thermal zone |
| 158 | * |
| 159 | * This function provides a pointer to trip points table |
| 160 | * |
| 161 | * Return: pointer to trip points table, NULL otherwise |
| 162 | */ |
Geert Uytterhoeven | ebc3193 | 2015-01-03 22:56:56 +0100 | [diff] [blame] | 163 | const struct thermal_trip * |
Lukasz Majewski | ce8be77 | 2014-12-08 18:04:20 +0100 | [diff] [blame] | 164 | of_thermal_get_trip_points(struct thermal_zone_device *tz) |
| 165 | { |
| 166 | struct __thermal_zone *data = tz->devdata; |
| 167 | |
| 168 | if (!data) |
| 169 | return NULL; |
| 170 | |
| 171 | return data->trips; |
| 172 | } |
| 173 | EXPORT_SYMBOL_GPL(of_thermal_get_trip_points); |
| 174 | |
Lukasz Majewski | 184a4bf | 2014-12-08 18:04:21 +0100 | [diff] [blame] | 175 | /** |
| 176 | * of_thermal_set_emul_temp - function to set emulated temperature |
| 177 | * |
| 178 | * @tz: pointer to a thermal zone |
| 179 | * @temp: temperature to set |
| 180 | * |
| 181 | * This function gives the ability to set emulated value of temperature, |
| 182 | * which is handy for debugging |
| 183 | * |
| 184 | * Return: zero on success, error code otherwise |
| 185 | */ |
| 186 | static int of_thermal_set_emul_temp(struct thermal_zone_device *tz, |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 187 | int temp) |
Lukasz Majewski | 184a4bf | 2014-12-08 18:04:21 +0100 | [diff] [blame] | 188 | { |
| 189 | struct __thermal_zone *data = tz->devdata; |
| 190 | |
Lukasz Majewski | 184a4bf | 2014-12-08 18:04:21 +0100 | [diff] [blame] | 191 | return data->ops->set_emul_temp(data->sensor_data, temp); |
| 192 | } |
| 193 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 194 | static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip, |
| 195 | enum thermal_trend *trend) |
| 196 | { |
| 197 | struct __thermal_zone *data = tz->devdata; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 198 | |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 199 | if (!data->ops->get_trend) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 200 | return -EINVAL; |
| 201 | |
Sascha Hauer | e78eaf4 | 2016-06-22 16:42:03 +0800 | [diff] [blame] | 202 | return data->ops->get_trend(data->sensor_data, trip, trend); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static int of_thermal_bind(struct thermal_zone_device *thermal, |
| 206 | struct thermal_cooling_device *cdev) |
| 207 | { |
| 208 | struct __thermal_zone *data = thermal->devdata; |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 209 | struct __thermal_bind_params *tbp; |
| 210 | struct __thermal_cooling_bind_param *tcbp; |
| 211 | int i, j; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 212 | |
| 213 | if (!data || IS_ERR(data)) |
| 214 | return -ENODEV; |
| 215 | |
| 216 | /* find where to bind */ |
| 217 | for (i = 0; i < data->num_tbps; i++) { |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 218 | tbp = data->tbps + i; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 219 | |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 220 | for (j = 0; j < tbp->count; j++) { |
| 221 | tcbp = tbp->tcbp + j; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 222 | |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 223 | if (tcbp->cooling_device == cdev->np) { |
| 224 | int ret; |
| 225 | |
| 226 | ret = thermal_zone_bind_cooling_device(thermal, |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 227 | tbp->trip_id, cdev, |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 228 | tcbp->max, |
| 229 | tcbp->min, |
Kapileshwar Singh | 6cd9e9f | 2015-02-18 16:04:21 +0000 | [diff] [blame] | 230 | tbp->usage); |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 231 | if (ret) |
| 232 | return ret; |
| 233 | } |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static int of_thermal_unbind(struct thermal_zone_device *thermal, |
| 241 | struct thermal_cooling_device *cdev) |
| 242 | { |
| 243 | struct __thermal_zone *data = thermal->devdata; |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 244 | struct __thermal_bind_params *tbp; |
| 245 | struct __thermal_cooling_bind_param *tcbp; |
| 246 | int i, j; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 247 | |
| 248 | if (!data || IS_ERR(data)) |
| 249 | return -ENODEV; |
| 250 | |
| 251 | /* find where to unbind */ |
| 252 | for (i = 0; i < data->num_tbps; i++) { |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 253 | tbp = data->tbps + i; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 254 | |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 255 | for (j = 0; j < tbp->count; j++) { |
| 256 | tcbp = tbp->tcbp + j; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 257 | |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 258 | if (tcbp->cooling_device == cdev->np) { |
| 259 | int ret; |
| 260 | |
| 261 | ret = thermal_zone_unbind_cooling_device(thermal, |
| 262 | tbp->trip_id, cdev); |
| 263 | if (ret) |
| 264 | return ret; |
| 265 | } |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static int of_thermal_get_mode(struct thermal_zone_device *tz, |
| 273 | enum thermal_device_mode *mode) |
| 274 | { |
| 275 | struct __thermal_zone *data = tz->devdata; |
| 276 | |
| 277 | *mode = data->mode; |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static int of_thermal_set_mode(struct thermal_zone_device *tz, |
| 283 | enum thermal_device_mode mode) |
| 284 | { |
| 285 | struct __thermal_zone *data = tz->devdata; |
| 286 | |
| 287 | mutex_lock(&tz->lock); |
| 288 | |
Anson Huang | 152395f | 2018-07-31 00:56:49 +0800 | [diff] [blame] | 289 | if (mode == THERMAL_DEVICE_ENABLED) { |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 290 | tz->polling_delay = data->polling_delay; |
Anson Huang | 152395f | 2018-07-31 00:56:49 +0800 | [diff] [blame] | 291 | tz->passive_delay = data->passive_delay; |
| 292 | } else { |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 293 | tz->polling_delay = 0; |
Anson Huang | 152395f | 2018-07-31 00:56:49 +0800 | [diff] [blame] | 294 | tz->passive_delay = 0; |
| 295 | } |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 296 | |
| 297 | mutex_unlock(&tz->lock); |
| 298 | |
| 299 | data->mode = mode; |
Srinivas Pandruvada | 0e70f46 | 2016-08-26 16:21:16 -0700 | [diff] [blame] | 300 | thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip, |
| 306 | enum thermal_trip_type *type) |
| 307 | { |
| 308 | struct __thermal_zone *data = tz->devdata; |
| 309 | |
| 310 | if (trip >= data->ntrips || trip < 0) |
| 311 | return -EDOM; |
| 312 | |
| 313 | *type = data->trips[trip].type; |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip, |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 319 | int *temp) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 320 | { |
| 321 | struct __thermal_zone *data = tz->devdata; |
| 322 | |
| 323 | if (trip >= data->ntrips || trip < 0) |
| 324 | return -EDOM; |
| 325 | |
| 326 | *temp = data->trips[trip].temperature; |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip, |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 332 | int temp) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 333 | { |
| 334 | struct __thermal_zone *data = tz->devdata; |
| 335 | |
| 336 | if (trip >= data->ntrips || trip < 0) |
| 337 | return -EDOM; |
| 338 | |
Wei Ni | c350952 | 2016-03-29 18:29:17 +0800 | [diff] [blame] | 339 | if (data->ops->set_trip_temp) { |
| 340 | int ret; |
| 341 | |
| 342 | ret = data->ops->set_trip_temp(data->sensor_data, trip, temp); |
| 343 | if (ret) |
| 344 | return ret; |
| 345 | } |
| 346 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 347 | /* thermal framework should take care of data->mask & (1 << trip) */ |
| 348 | data->trips[trip].temperature = temp; |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip, |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 354 | int *hyst) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 355 | { |
| 356 | struct __thermal_zone *data = tz->devdata; |
| 357 | |
| 358 | if (trip >= data->ntrips || trip < 0) |
| 359 | return -EDOM; |
| 360 | |
| 361 | *hyst = data->trips[trip].hysteresis; |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip, |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 367 | int hyst) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 368 | { |
| 369 | struct __thermal_zone *data = tz->devdata; |
| 370 | |
| 371 | if (trip >= data->ntrips || trip < 0) |
| 372 | return -EDOM; |
| 373 | |
| 374 | /* thermal framework should take care of data->mask & (1 << trip) */ |
| 375 | data->trips[trip].hysteresis = hyst; |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | static int of_thermal_get_crit_temp(struct thermal_zone_device *tz, |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 381 | int *temp) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 382 | { |
| 383 | struct __thermal_zone *data = tz->devdata; |
| 384 | int i; |
| 385 | |
| 386 | for (i = 0; i < data->ntrips; i++) |
| 387 | if (data->trips[i].type == THERMAL_TRIP_CRITICAL) { |
| 388 | *temp = data->trips[i].temperature; |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | return -EINVAL; |
| 393 | } |
| 394 | |
| 395 | static struct thermal_zone_device_ops of_thermal_ops = { |
| 396 | .get_mode = of_thermal_get_mode, |
| 397 | .set_mode = of_thermal_set_mode, |
| 398 | |
| 399 | .get_trip_type = of_thermal_get_trip_type, |
| 400 | .get_trip_temp = of_thermal_get_trip_temp, |
| 401 | .set_trip_temp = of_thermal_set_trip_temp, |
| 402 | .get_trip_hyst = of_thermal_get_trip_hyst, |
| 403 | .set_trip_hyst = of_thermal_set_trip_hyst, |
| 404 | .get_crit_temp = of_thermal_get_crit_temp, |
| 405 | |
| 406 | .bind = of_thermal_bind, |
| 407 | .unbind = of_thermal_unbind, |
| 408 | }; |
| 409 | |
| 410 | /*** sensor API ***/ |
| 411 | |
| 412 | static struct thermal_zone_device * |
| 413 | thermal_zone_of_add_sensor(struct device_node *zone, |
| 414 | struct device_node *sensor, void *data, |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 415 | const struct thermal_zone_of_device_ops *ops) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 416 | { |
| 417 | struct thermal_zone_device *tzd; |
| 418 | struct __thermal_zone *tz; |
| 419 | |
| 420 | tzd = thermal_zone_get_zone_by_name(zone->name); |
| 421 | if (IS_ERR(tzd)) |
| 422 | return ERR_PTR(-EPROBE_DEFER); |
| 423 | |
| 424 | tz = tzd->devdata; |
| 425 | |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 426 | if (!ops) |
| 427 | return ERR_PTR(-EINVAL); |
| 428 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 429 | mutex_lock(&tzd->lock); |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 430 | tz->ops = ops; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 431 | tz->sensor_data = data; |
| 432 | |
| 433 | tzd->ops->get_temp = of_thermal_get_temp; |
| 434 | tzd->ops->get_trend = of_thermal_get_trend; |
Sascha Hauer | 826386e | 2016-06-22 16:42:02 +0800 | [diff] [blame] | 435 | |
| 436 | /* |
| 437 | * The thermal zone core will calculate the window if they have set the |
| 438 | * optional set_trips pointer. |
| 439 | */ |
| 440 | if (ops->set_trips) |
| 441 | tzd->ops->set_trips = of_thermal_set_trips; |
| 442 | |
Keerthy | e2fa748 | 2016-06-02 14:24:50 +0530 | [diff] [blame] | 443 | if (ops->set_emul_temp) |
| 444 | tzd->ops->set_emul_temp = of_thermal_set_emul_temp; |
| 445 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 446 | mutex_unlock(&tzd->lock); |
| 447 | |
| 448 | return tzd; |
| 449 | } |
| 450 | |
| 451 | /** |
Anson Huang | 34471ab | 2020-02-22 08:08:49 +0800 | [diff] [blame] | 452 | * thermal_zone_of_get_sensor_id - get sensor ID from a DT thermal zone |
| 453 | * @tz_np: a valid thermal zone device node. |
| 454 | * @sensor_np: a sensor node of a valid sensor device. |
| 455 | * @id: the sensor ID returned if success. |
| 456 | * |
| 457 | * This function will get sensor ID from a given thermal zone node and |
| 458 | * the sensor node must match the temperature provider @sensor_np. |
| 459 | * |
| 460 | * Return: 0 on success, proper error code otherwise. |
| 461 | */ |
| 462 | |
| 463 | int thermal_zone_of_get_sensor_id(struct device_node *tz_np, |
| 464 | struct device_node *sensor_np, |
| 465 | u32 *id) |
| 466 | { |
| 467 | struct of_phandle_args sensor_specs; |
| 468 | int ret; |
| 469 | |
| 470 | ret = of_parse_phandle_with_args(tz_np, |
| 471 | "thermal-sensors", |
| 472 | "#thermal-sensor-cells", |
| 473 | 0, |
| 474 | &sensor_specs); |
| 475 | if (ret) |
| 476 | return ret; |
| 477 | |
| 478 | if (sensor_specs.np != sensor_np) { |
| 479 | of_node_put(sensor_specs.np); |
| 480 | return -ENODEV; |
| 481 | } |
| 482 | |
| 483 | if (sensor_specs.args_count > 1) |
| 484 | pr_warn("%pOFn: too many cells in sensor specifier %d\n", |
| 485 | sensor_specs.np, sensor_specs.args_count); |
| 486 | |
| 487 | *id = sensor_specs.args_count ? sensor_specs.args[0] : 0; |
| 488 | |
| 489 | of_node_put(sensor_specs.np); |
| 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | EXPORT_SYMBOL_GPL(thermal_zone_of_get_sensor_id); |
| 494 | |
| 495 | /** |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 496 | * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone |
| 497 | * @dev: a valid struct device pointer of a sensor device. Must contain |
| 498 | * a valid .of_node, for the sensor node. |
| 499 | * @sensor_id: a sensor identifier, in case the sensor IP has more |
| 500 | * than one sensors |
| 501 | * @data: a private pointer (owned by the caller) that will be passed |
| 502 | * back, when a temperature reading is needed. |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 503 | * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp. |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 504 | * |
| 505 | * This function will search the list of thermal zones described in device |
| 506 | * tree and look for the zone that refer to the sensor device pointed by |
| 507 | * @dev->of_node as temperature providers. For the zone pointing to the |
| 508 | * sensor node, the sensor will be added to the DT thermal zone device. |
| 509 | * |
| 510 | * The thermal zone temperature is provided by the @get_temp function |
| 511 | * pointer. When called, it will have the private pointer @data back. |
| 512 | * |
| 513 | * The thermal zone temperature trend is provided by the @get_trend function |
| 514 | * pointer. When called, it will have the private pointer @data back. |
| 515 | * |
| 516 | * TODO: |
| 517 | * 01 - This function must enqueue the new sensor instead of using |
| 518 | * it as the only source of temperature values. |
| 519 | * |
| 520 | * 02 - There must be a way to match the sensor with all thermal zones |
| 521 | * that refer to it. |
| 522 | * |
| 523 | * Return: On success returns a valid struct thermal_zone_device, |
| 524 | * otherwise, it returns a corresponding ERR_PTR(). Caller must |
| 525 | * check the return value with help of IS_ERR() helper. |
| 526 | */ |
| 527 | struct thermal_zone_device * |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 528 | thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data, |
| 529 | const struct thermal_zone_of_device_ops *ops) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 530 | { |
| 531 | struct device_node *np, *child, *sensor_np; |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 532 | struct thermal_zone_device *tzd = ERR_PTR(-ENODEV); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 533 | |
| 534 | np = of_find_node_by_name(NULL, "thermal-zones"); |
| 535 | if (!np) |
| 536 | return ERR_PTR(-ENODEV); |
| 537 | |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 538 | if (!dev || !dev->of_node) { |
| 539 | of_node_put(np); |
Peter Mamonov | 370f995 | 2019-08-27 17:39:52 +0300 | [diff] [blame] | 540 | return ERR_PTR(-ENODEV); |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 541 | } |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 542 | |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 543 | sensor_np = of_node_get(dev->of_node); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 544 | |
Laxman Dewangan | 42bbe40 | 2016-02-08 18:58:34 +0530 | [diff] [blame] | 545 | for_each_available_child_of_node(np, child) { |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 546 | int ret, id; |
| 547 | |
| 548 | /* For now, thermal framework supports only 1 sensor per zone */ |
Anson Huang | 34471ab | 2020-02-22 08:08:49 +0800 | [diff] [blame] | 549 | ret = thermal_zone_of_get_sensor_id(child, sensor_np, &id); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 550 | if (ret) |
| 551 | continue; |
| 552 | |
Anson Huang | 34471ab | 2020-02-22 08:08:49 +0800 | [diff] [blame] | 553 | if (id == sensor_id) { |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 554 | tzd = thermal_zone_of_add_sensor(child, sensor_np, |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 555 | data, ops); |
Lukasz Majewski | 528012c1 | 2015-01-19 12:44:04 +0100 | [diff] [blame] | 556 | if (!IS_ERR(tzd)) |
| 557 | tzd->ops->set_mode(tzd, THERMAL_DEVICE_ENABLED); |
| 558 | |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 559 | of_node_put(child); |
| 560 | goto exit; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 561 | } |
| 562 | } |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 563 | exit: |
| 564 | of_node_put(sensor_np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 565 | of_node_put(np); |
| 566 | |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 567 | return tzd; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 568 | } |
| 569 | EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register); |
| 570 | |
| 571 | /** |
| 572 | * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone |
| 573 | * @dev: a valid struct device pointer of a sensor device. Must contain |
| 574 | * a valid .of_node, for the sensor node. |
| 575 | * @tzd: a pointer to struct thermal_zone_device where the sensor is registered. |
| 576 | * |
| 577 | * This function removes the sensor callbacks and private data from the |
| 578 | * thermal zone device registered with thermal_zone_of_sensor_register() |
| 579 | * API. It will also silent the zone by remove the .get_temp() and .get_trend() |
| 580 | * thermal zone device callbacks. |
| 581 | * |
| 582 | * TODO: When the support to several sensors per zone is added, this |
| 583 | * function must search the sensor list based on @dev parameter. |
| 584 | * |
| 585 | */ |
| 586 | void thermal_zone_of_sensor_unregister(struct device *dev, |
| 587 | struct thermal_zone_device *tzd) |
| 588 | { |
| 589 | struct __thermal_zone *tz; |
| 590 | |
| 591 | if (!dev || !tzd || !tzd->devdata) |
| 592 | return; |
| 593 | |
| 594 | tz = tzd->devdata; |
| 595 | |
| 596 | /* no __thermal_zone, nothing to be done */ |
| 597 | if (!tz) |
| 598 | return; |
| 599 | |
| 600 | mutex_lock(&tzd->lock); |
| 601 | tzd->ops->get_temp = NULL; |
| 602 | tzd->ops->get_trend = NULL; |
Lukasz Majewski | 184a4bf | 2014-12-08 18:04:21 +0100 | [diff] [blame] | 603 | tzd->ops->set_emul_temp = NULL; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 604 | |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 605 | tz->ops = NULL; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 606 | tz->sensor_data = NULL; |
| 607 | mutex_unlock(&tzd->lock); |
| 608 | } |
| 609 | EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister); |
| 610 | |
Laxman Dewangan | e498b49 | 2016-03-09 18:40:06 +0530 | [diff] [blame] | 611 | static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res) |
| 612 | { |
| 613 | thermal_zone_of_sensor_unregister(dev, |
| 614 | *(struct thermal_zone_device **)res); |
| 615 | } |
| 616 | |
| 617 | static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res, |
| 618 | void *data) |
| 619 | { |
| 620 | struct thermal_zone_device **r = res; |
| 621 | |
| 622 | if (WARN_ON(!r || !*r)) |
| 623 | return 0; |
| 624 | |
| 625 | return *r == data; |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * devm_thermal_zone_of_sensor_register - Resource managed version of |
| 630 | * thermal_zone_of_sensor_register() |
| 631 | * @dev: a valid struct device pointer of a sensor device. Must contain |
| 632 | * a valid .of_node, for the sensor node. |
| 633 | * @sensor_id: a sensor identifier, in case the sensor IP has more |
| 634 | * than one sensors |
| 635 | * @data: a private pointer (owned by the caller) that will be passed |
| 636 | * back, when a temperature reading is needed. |
| 637 | * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp. |
| 638 | * |
| 639 | * Refer thermal_zone_of_sensor_register() for more details. |
| 640 | * |
| 641 | * Return: On success returns a valid struct thermal_zone_device, |
| 642 | * otherwise, it returns a corresponding ERR_PTR(). Caller must |
| 643 | * check the return value with help of IS_ERR() helper. |
Zhang Rui | 7b5c4a0 | 2016-08-22 15:48:11 +0800 | [diff] [blame] | 644 | * Registered thermal_zone_device device will automatically be |
Laxman Dewangan | e498b49 | 2016-03-09 18:40:06 +0530 | [diff] [blame] | 645 | * released when device is unbounded. |
| 646 | */ |
| 647 | struct thermal_zone_device *devm_thermal_zone_of_sensor_register( |
| 648 | struct device *dev, int sensor_id, |
| 649 | void *data, const struct thermal_zone_of_device_ops *ops) |
| 650 | { |
| 651 | struct thermal_zone_device **ptr, *tzd; |
| 652 | |
| 653 | ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr), |
| 654 | GFP_KERNEL); |
| 655 | if (!ptr) |
| 656 | return ERR_PTR(-ENOMEM); |
| 657 | |
| 658 | tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops); |
| 659 | if (IS_ERR(tzd)) { |
| 660 | devres_free(ptr); |
| 661 | return tzd; |
| 662 | } |
| 663 | |
| 664 | *ptr = tzd; |
| 665 | devres_add(dev, ptr); |
| 666 | |
| 667 | return tzd; |
| 668 | } |
| 669 | EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register); |
| 670 | |
| 671 | /** |
| 672 | * devm_thermal_zone_of_sensor_unregister - Resource managed version of |
| 673 | * thermal_zone_of_sensor_unregister(). |
| 674 | * @dev: Device for which which resource was allocated. |
| 675 | * @tzd: a pointer to struct thermal_zone_device where the sensor is registered. |
| 676 | * |
| 677 | * This function removes the sensor callbacks and private data from the |
| 678 | * thermal zone device registered with devm_thermal_zone_of_sensor_register() |
| 679 | * API. It will also silent the zone by remove the .get_temp() and .get_trend() |
| 680 | * thermal zone device callbacks. |
| 681 | * Normally this function will not need to be called and the resource |
| 682 | * management code will ensure that the resource is freed. |
| 683 | */ |
| 684 | void devm_thermal_zone_of_sensor_unregister(struct device *dev, |
| 685 | struct thermal_zone_device *tzd) |
| 686 | { |
| 687 | WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release, |
| 688 | devm_thermal_zone_of_sensor_match, tzd)); |
| 689 | } |
| 690 | EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister); |
| 691 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 692 | /*** functions parsing device tree nodes ***/ |
| 693 | |
| 694 | /** |
| 695 | * thermal_of_populate_bind_params - parse and fill cooling map data |
| 696 | * @np: DT node containing a cooling-map node |
| 697 | * @__tbp: data structure to be filled with cooling map info |
| 698 | * @trips: array of thermal zone trip points |
| 699 | * @ntrips: number of trip points inside trips. |
| 700 | * |
| 701 | * This function parses a cooling-map type of node represented by |
| 702 | * @np parameter and fills the read data into @__tbp data structure. |
| 703 | * It needs the already parsed array of trip points of the thermal zone |
| 704 | * in consideration. |
| 705 | * |
| 706 | * Return: 0 on success, proper error code otherwise |
| 707 | */ |
| 708 | static int thermal_of_populate_bind_params(struct device_node *np, |
| 709 | struct __thermal_bind_params *__tbp, |
Lukasz Majewski | ad9914a | 2014-12-08 18:04:19 +0100 | [diff] [blame] | 710 | struct thermal_trip *trips, |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 711 | int ntrips) |
| 712 | { |
| 713 | struct of_phandle_args cooling_spec; |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 714 | struct __thermal_cooling_bind_param *__tcbp; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 715 | struct device_node *trip; |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 716 | int ret, i, count; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 717 | u32 prop; |
| 718 | |
| 719 | /* Default weight. Usage is optional */ |
Kapileshwar Singh | 6cd9e9f | 2015-02-18 16:04:21 +0000 | [diff] [blame] | 720 | __tbp->usage = THERMAL_WEIGHT_DEFAULT; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 721 | ret = of_property_read_u32(np, "contribution", &prop); |
| 722 | if (ret == 0) |
| 723 | __tbp->usage = prop; |
| 724 | |
| 725 | trip = of_parse_phandle(np, "trip", 0); |
| 726 | if (!trip) { |
| 727 | pr_err("missing trip property\n"); |
| 728 | return -ENODEV; |
| 729 | } |
| 730 | |
| 731 | /* match using device_node */ |
| 732 | for (i = 0; i < ntrips; i++) |
| 733 | if (trip == trips[i].np) { |
| 734 | __tbp->trip_id = i; |
| 735 | break; |
| 736 | } |
| 737 | |
| 738 | if (i == ntrips) { |
| 739 | ret = -ENODEV; |
| 740 | goto end; |
| 741 | } |
| 742 | |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 743 | count = of_count_phandle_with_args(np, "cooling-device", |
| 744 | "#cooling-cells"); |
| 745 | if (!count) { |
| 746 | pr_err("Add a cooling_device property with at least one device\n"); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 747 | goto end; |
| 748 | } |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 749 | |
| 750 | __tcbp = kcalloc(count, sizeof(*__tcbp), GFP_KERNEL); |
| 751 | if (!__tcbp) |
| 752 | goto end; |
| 753 | |
| 754 | for (i = 0; i < count; i++) { |
| 755 | ret = of_parse_phandle_with_args(np, "cooling-device", |
| 756 | "#cooling-cells", i, &cooling_spec); |
| 757 | if (ret < 0) { |
| 758 | pr_err("Invalid cooling-device entry\n"); |
| 759 | goto free_tcbp; |
| 760 | } |
| 761 | |
| 762 | __tcbp[i].cooling_device = cooling_spec.np; |
| 763 | |
| 764 | if (cooling_spec.args_count >= 2) { /* at least min and max */ |
| 765 | __tcbp[i].min = cooling_spec.args[0]; |
| 766 | __tcbp[i].max = cooling_spec.args[1]; |
| 767 | } else { |
| 768 | pr_err("wrong reference to cooling device, missing limits\n"); |
| 769 | } |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 770 | } |
| 771 | |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 772 | __tbp->tcbp = __tcbp; |
| 773 | __tbp->count = count; |
| 774 | |
| 775 | goto end; |
| 776 | |
| 777 | free_tcbp: |
| 778 | for (i = i - 1; i >= 0; i--) |
| 779 | of_node_put(__tcbp[i].cooling_device); |
| 780 | kfree(__tcbp); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 781 | end: |
| 782 | of_node_put(trip); |
| 783 | |
| 784 | return ret; |
| 785 | } |
| 786 | |
Amit Kucheria | faae0ed | 2019-11-20 21:15:10 +0530 | [diff] [blame] | 787 | /* |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 788 | * It maps 'enum thermal_trip_type' found in include/linux/thermal.h |
| 789 | * into the device tree binding of 'trip', property type. |
| 790 | */ |
| 791 | static const char * const trip_types[] = { |
| 792 | [THERMAL_TRIP_ACTIVE] = "active", |
| 793 | [THERMAL_TRIP_PASSIVE] = "passive", |
| 794 | [THERMAL_TRIP_HOT] = "hot", |
| 795 | [THERMAL_TRIP_CRITICAL] = "critical", |
| 796 | }; |
| 797 | |
| 798 | /** |
| 799 | * thermal_of_get_trip_type - Get phy mode for given device_node |
| 800 | * @np: Pointer to the given device_node |
| 801 | * @type: Pointer to resulting trip type |
| 802 | * |
| 803 | * The function gets trip type string from property 'type', |
| 804 | * and store its index in trip_types table in @type, |
| 805 | * |
| 806 | * Return: 0 on success, or errno in error case. |
| 807 | */ |
| 808 | static int thermal_of_get_trip_type(struct device_node *np, |
| 809 | enum thermal_trip_type *type) |
| 810 | { |
| 811 | const char *t; |
| 812 | int err, i; |
| 813 | |
| 814 | err = of_property_read_string(np, "type", &t); |
| 815 | if (err < 0) |
| 816 | return err; |
| 817 | |
| 818 | for (i = 0; i < ARRAY_SIZE(trip_types); i++) |
| 819 | if (!strcasecmp(t, trip_types[i])) { |
| 820 | *type = i; |
| 821 | return 0; |
| 822 | } |
| 823 | |
| 824 | return -ENODEV; |
| 825 | } |
| 826 | |
| 827 | /** |
| 828 | * thermal_of_populate_trip - parse and fill one trip point data |
| 829 | * @np: DT node containing a trip point node |
| 830 | * @trip: trip point data structure to be filled up |
| 831 | * |
| 832 | * This function parses a trip point type of node represented by |
| 833 | * @np parameter and fills the read data into @trip data structure. |
| 834 | * |
| 835 | * Return: 0 on success, proper error code otherwise |
| 836 | */ |
| 837 | static int thermal_of_populate_trip(struct device_node *np, |
Lukasz Majewski | ad9914a | 2014-12-08 18:04:19 +0100 | [diff] [blame] | 838 | struct thermal_trip *trip) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 839 | { |
| 840 | int prop; |
| 841 | int ret; |
| 842 | |
| 843 | ret = of_property_read_u32(np, "temperature", &prop); |
| 844 | if (ret < 0) { |
| 845 | pr_err("missing temperature property\n"); |
| 846 | return ret; |
| 847 | } |
| 848 | trip->temperature = prop; |
| 849 | |
| 850 | ret = of_property_read_u32(np, "hysteresis", &prop); |
| 851 | if (ret < 0) { |
| 852 | pr_err("missing hysteresis property\n"); |
| 853 | return ret; |
| 854 | } |
| 855 | trip->hysteresis = prop; |
| 856 | |
| 857 | ret = thermal_of_get_trip_type(np, &trip->type); |
| 858 | if (ret < 0) { |
| 859 | pr_err("wrong trip type property\n"); |
| 860 | return ret; |
| 861 | } |
| 862 | |
| 863 | /* Required for cooling map matching */ |
| 864 | trip->np = np; |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 865 | of_node_get(np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 866 | |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | /** |
| 871 | * thermal_of_build_thermal_zone - parse and fill one thermal zone data |
| 872 | * @np: DT node containing a thermal zone node |
| 873 | * |
| 874 | * This function parses a thermal zone type of node represented by |
| 875 | * @np parameter and fills the read data into a __thermal_zone data structure |
| 876 | * and return this pointer. |
| 877 | * |
Eduardo Valentin | a46dbae | 2015-05-11 19:48:09 -0700 | [diff] [blame] | 878 | * TODO: Missing properties to parse: thermal-sensor-names |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 879 | * |
| 880 | * Return: On success returns a valid struct __thermal_zone, |
| 881 | * otherwise, it returns a corresponding ERR_PTR(). Caller must |
| 882 | * check the return value with help of IS_ERR() helper. |
| 883 | */ |
Julia Lawall | c0ff8aa | 2016-04-19 14:33:32 +0200 | [diff] [blame] | 884 | static struct __thermal_zone |
| 885 | __init *thermal_of_build_thermal_zone(struct device_node *np) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 886 | { |
| 887 | struct device_node *child = NULL, *gchild; |
| 888 | struct __thermal_zone *tz; |
| 889 | int ret, i; |
Eduardo Valentin | a46dbae | 2015-05-11 19:48:09 -0700 | [diff] [blame] | 890 | u32 prop, coef[2]; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 891 | |
| 892 | if (!np) { |
| 893 | pr_err("no thermal zone np\n"); |
| 894 | return ERR_PTR(-EINVAL); |
| 895 | } |
| 896 | |
| 897 | tz = kzalloc(sizeof(*tz), GFP_KERNEL); |
| 898 | if (!tz) |
| 899 | return ERR_PTR(-ENOMEM); |
| 900 | |
| 901 | ret = of_property_read_u32(np, "polling-delay-passive", &prop); |
| 902 | if (ret < 0) { |
Amit Kucheria | 3079f34 | 2019-01-21 15:12:22 +0530 | [diff] [blame] | 903 | pr_err("%pOFn: missing polling-delay-passive property\n", np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 904 | goto free_tz; |
| 905 | } |
| 906 | tz->passive_delay = prop; |
| 907 | |
| 908 | ret = of_property_read_u32(np, "polling-delay", &prop); |
| 909 | if (ret < 0) { |
Amit Kucheria | 3079f34 | 2019-01-21 15:12:22 +0530 | [diff] [blame] | 910 | pr_err("%pOFn: missing polling-delay property\n", np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 911 | goto free_tz; |
| 912 | } |
| 913 | tz->polling_delay = prop; |
| 914 | |
Eduardo Valentin | a46dbae | 2015-05-11 19:48:09 -0700 | [diff] [blame] | 915 | /* |
| 916 | * REVIST: for now, the thermal framework supports only |
| 917 | * one sensor per thermal zone. Thus, we are considering |
| 918 | * only the first two values as slope and offset. |
| 919 | */ |
| 920 | ret = of_property_read_u32_array(np, "coefficients", coef, 2); |
| 921 | if (ret == 0) { |
| 922 | tz->slope = coef[0]; |
| 923 | tz->offset = coef[1]; |
| 924 | } else { |
| 925 | tz->slope = 1; |
| 926 | tz->offset = 0; |
| 927 | } |
| 928 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 929 | /* trips */ |
| 930 | child = of_get_child_by_name(np, "trips"); |
| 931 | |
| 932 | /* No trips provided */ |
| 933 | if (!child) |
| 934 | goto finish; |
| 935 | |
| 936 | tz->ntrips = of_get_child_count(child); |
| 937 | if (tz->ntrips == 0) /* must have at least one child */ |
| 938 | goto finish; |
| 939 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 940 | tz->trips = kcalloc(tz->ntrips, sizeof(*tz->trips), GFP_KERNEL); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 941 | if (!tz->trips) { |
| 942 | ret = -ENOMEM; |
| 943 | goto free_tz; |
| 944 | } |
| 945 | |
| 946 | i = 0; |
| 947 | for_each_child_of_node(child, gchild) { |
| 948 | ret = thermal_of_populate_trip(gchild, &tz->trips[i++]); |
| 949 | if (ret) |
| 950 | goto free_trips; |
| 951 | } |
| 952 | |
| 953 | of_node_put(child); |
| 954 | |
| 955 | /* cooling-maps */ |
| 956 | child = of_get_child_by_name(np, "cooling-maps"); |
| 957 | |
| 958 | /* cooling-maps not provided */ |
| 959 | if (!child) |
| 960 | goto finish; |
| 961 | |
| 962 | tz->num_tbps = of_get_child_count(child); |
| 963 | if (tz->num_tbps == 0) |
| 964 | goto finish; |
| 965 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 966 | tz->tbps = kcalloc(tz->num_tbps, sizeof(*tz->tbps), GFP_KERNEL); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 967 | if (!tz->tbps) { |
| 968 | ret = -ENOMEM; |
| 969 | goto free_trips; |
| 970 | } |
| 971 | |
| 972 | i = 0; |
Stephen Boyd | ca9521b | 2014-06-18 16:32:08 -0700 | [diff] [blame] | 973 | for_each_child_of_node(child, gchild) { |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 974 | ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++], |
| 975 | tz->trips, tz->ntrips); |
| 976 | if (ret) |
| 977 | goto free_tbps; |
Stephen Boyd | ca9521b | 2014-06-18 16:32:08 -0700 | [diff] [blame] | 978 | } |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 979 | |
| 980 | finish: |
| 981 | of_node_put(child); |
| 982 | tz->mode = THERMAL_DEVICE_DISABLED; |
| 983 | |
| 984 | return tz; |
| 985 | |
| 986 | free_tbps: |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 987 | for (i = i - 1; i >= 0; i--) { |
| 988 | struct __thermal_bind_params *tbp = tz->tbps + i; |
| 989 | int j; |
| 990 | |
| 991 | for (j = 0; j < tbp->count; j++) |
| 992 | of_node_put(tbp->tcbp[j].cooling_device); |
| 993 | |
| 994 | kfree(tbp->tcbp); |
| 995 | } |
| 996 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 997 | kfree(tz->tbps); |
| 998 | free_trips: |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 999 | for (i = 0; i < tz->ntrips; i++) |
| 1000 | of_node_put(tz->trips[i].np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1001 | kfree(tz->trips); |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 1002 | of_node_put(gchild); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1003 | free_tz: |
| 1004 | kfree(tz); |
| 1005 | of_node_put(child); |
| 1006 | |
| 1007 | return ERR_PTR(ret); |
| 1008 | } |
| 1009 | |
Daniel Lezcano | 93802b0 | 2019-12-19 23:21:53 +0100 | [diff] [blame] | 1010 | static __init void of_thermal_free_zone(struct __thermal_zone *tz) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1011 | { |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 1012 | struct __thermal_bind_params *tbp; |
| 1013 | int i, j; |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 1014 | |
Viresh Kumar | a92bab8 | 2018-08-08 12:38:14 +0530 | [diff] [blame] | 1015 | for (i = 0; i < tz->num_tbps; i++) { |
| 1016 | tbp = tz->tbps + i; |
| 1017 | |
| 1018 | for (j = 0; j < tbp->count; j++) |
| 1019 | of_node_put(tbp->tcbp[j].cooling_device); |
| 1020 | |
| 1021 | kfree(tbp->tcbp); |
| 1022 | } |
| 1023 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1024 | kfree(tz->tbps); |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 1025 | for (i = 0; i < tz->ntrips; i++) |
| 1026 | of_node_put(tz->trips[i].np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1027 | kfree(tz->trips); |
| 1028 | kfree(tz); |
| 1029 | } |
| 1030 | |
| 1031 | /** |
Daniel Lezcano | 8c24b85 | 2019-12-19 23:21:52 +0100 | [diff] [blame] | 1032 | * of_thermal_destroy_zones - remove all zones parsed and allocated resources |
| 1033 | * |
| 1034 | * Finds all zones parsed and added to the thermal framework and remove them |
| 1035 | * from the system, together with their resources. |
| 1036 | * |
| 1037 | */ |
| 1038 | static __init void of_thermal_destroy_zones(void) |
| 1039 | { |
| 1040 | struct device_node *np, *child; |
| 1041 | |
| 1042 | np = of_find_node_by_name(NULL, "thermal-zones"); |
| 1043 | if (!np) { |
| 1044 | pr_debug("unable to find thermal zones\n"); |
| 1045 | return; |
| 1046 | } |
| 1047 | |
| 1048 | for_each_available_child_of_node(np, child) { |
| 1049 | struct thermal_zone_device *zone; |
| 1050 | |
| 1051 | zone = thermal_zone_get_zone_by_name(child->name); |
| 1052 | if (IS_ERR(zone)) |
| 1053 | continue; |
| 1054 | |
| 1055 | thermal_zone_device_unregister(zone); |
| 1056 | kfree(zone->tzp); |
| 1057 | kfree(zone->ops); |
| 1058 | of_thermal_free_zone(zone->devdata); |
| 1059 | } |
| 1060 | of_node_put(np); |
| 1061 | } |
| 1062 | |
| 1063 | /** |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1064 | * of_parse_thermal_zones - parse device tree thermal data |
| 1065 | * |
| 1066 | * Initialization function that can be called by machine initialization |
| 1067 | * code to parse thermal data and populate the thermal framework |
| 1068 | * with hardware thermal zones info. This function only parses thermal zones. |
| 1069 | * Cooling devices and sensor devices nodes are supposed to be parsed |
| 1070 | * by their respective drivers. |
| 1071 | * |
| 1072 | * Return: 0 on success, proper error code otherwise |
| 1073 | * |
| 1074 | */ |
| 1075 | int __init of_parse_thermal_zones(void) |
| 1076 | { |
| 1077 | struct device_node *np, *child; |
| 1078 | struct __thermal_zone *tz; |
| 1079 | struct thermal_zone_device_ops *ops; |
| 1080 | |
| 1081 | np = of_find_node_by_name(NULL, "thermal-zones"); |
| 1082 | if (!np) { |
| 1083 | pr_debug("unable to find thermal zones\n"); |
| 1084 | return 0; /* Run successfully on systems without thermal DT */ |
| 1085 | } |
| 1086 | |
Laxman Dewangan | 42bbe40 | 2016-02-08 18:58:34 +0530 | [diff] [blame] | 1087 | for_each_available_child_of_node(np, child) { |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1088 | struct thermal_zone_device *zone; |
| 1089 | struct thermal_zone_params *tzp; |
Punit Agrawal | 76af549 | 2015-03-03 10:43:04 +0000 | [diff] [blame] | 1090 | int i, mask = 0; |
Punit Agrawal | 647f992 | 2015-02-26 19:00:32 +0000 | [diff] [blame] | 1091 | u32 prop; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1092 | |
| 1093 | tz = thermal_of_build_thermal_zone(child); |
| 1094 | if (IS_ERR(tz)) { |
Rob Herring | 9b96566 | 2018-08-27 20:52:46 -0500 | [diff] [blame] | 1095 | pr_err("failed to build thermal zone %pOFn: %ld\n", |
| 1096 | child, |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1097 | PTR_ERR(tz)); |
| 1098 | continue; |
| 1099 | } |
| 1100 | |
| 1101 | ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL); |
| 1102 | if (!ops) |
| 1103 | goto exit_free; |
| 1104 | |
| 1105 | tzp = kzalloc(sizeof(*tzp), GFP_KERNEL); |
| 1106 | if (!tzp) { |
| 1107 | kfree(ops); |
| 1108 | goto exit_free; |
| 1109 | } |
| 1110 | |
| 1111 | /* No hwmon because there might be hwmon drivers registering */ |
| 1112 | tzp->no_hwmon = true; |
| 1113 | |
Punit Agrawal | 647f992 | 2015-02-26 19:00:32 +0000 | [diff] [blame] | 1114 | if (!of_property_read_u32(child, "sustainable-power", &prop)) |
| 1115 | tzp->sustainable_power = prop; |
| 1116 | |
Punit Agrawal | 76af549 | 2015-03-03 10:43:04 +0000 | [diff] [blame] | 1117 | for (i = 0; i < tz->ntrips; i++) |
| 1118 | mask |= 1 << i; |
| 1119 | |
Eduardo Valentin | a46dbae | 2015-05-11 19:48:09 -0700 | [diff] [blame] | 1120 | /* these two are left for temperature drivers to use */ |
| 1121 | tzp->slope = tz->slope; |
| 1122 | tzp->offset = tz->offset; |
| 1123 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1124 | zone = thermal_zone_device_register(child->name, tz->ntrips, |
Punit Agrawal | 76af549 | 2015-03-03 10:43:04 +0000 | [diff] [blame] | 1125 | mask, tz, |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1126 | ops, tzp, |
| 1127 | tz->passive_delay, |
| 1128 | tz->polling_delay); |
| 1129 | if (IS_ERR(zone)) { |
Rob Herring | 9b96566 | 2018-08-27 20:52:46 -0500 | [diff] [blame] | 1130 | pr_err("Failed to build %pOFn zone %ld\n", child, |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1131 | PTR_ERR(zone)); |
| 1132 | kfree(tzp); |
| 1133 | kfree(ops); |
| 1134 | of_thermal_free_zone(tz); |
| 1135 | /* attempting to build remaining zones still */ |
| 1136 | } |
| 1137 | } |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 1138 | of_node_put(np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1139 | |
| 1140 | return 0; |
| 1141 | |
| 1142 | exit_free: |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 1143 | of_node_put(child); |
| 1144 | of_node_put(np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1145 | of_thermal_free_zone(tz); |
| 1146 | |
| 1147 | /* no memory available, so free what we have built */ |
| 1148 | of_thermal_destroy_zones(); |
| 1149 | |
| 1150 | return -ENOMEM; |
| 1151 | } |