Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * of-thermal.c - Generic Thermal Management device tree support. |
| 3 | * |
| 4 | * Copyright (C) 2013 Texas Instruments |
| 5 | * Copyright (C) 2013 Eduardo Valentin <eduardo.valentin@ti.com> |
| 6 | * |
| 7 | * |
| 8 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; version 2 of the License. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 22 | * |
| 23 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 24 | */ |
| 25 | #include <linux/thermal.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/of_device.h> |
| 29 | #include <linux/of_platform.h> |
| 30 | #include <linux/err.h> |
| 31 | #include <linux/export.h> |
| 32 | #include <linux/string.h> |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 33 | #include <linux/thermal.h> |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 34 | #include <linux/list.h> |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 35 | |
| 36 | #include "thermal_core.h" |
| 37 | |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 38 | /*** Private data structures to represent thermal device tree data ***/ |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 39 | /** |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 40 | * struct __thermal_bind_param - a match between trip and cooling device |
| 41 | * @cooling_device: a pointer to identify the referred cooling device |
| 42 | * @trip_id: the trip point index |
| 43 | * @usage: the percentage (from 0 to 100) of cooling contribution |
| 44 | * @min: minimum cooling state used at this trip point |
| 45 | * @max: maximum cooling state used at this trip point |
| 46 | */ |
| 47 | |
| 48 | struct __thermal_bind_params { |
| 49 | struct device_node *cooling_device; |
| 50 | unsigned int trip_id; |
| 51 | unsigned int usage; |
| 52 | unsigned long min; |
| 53 | unsigned long max; |
| 54 | }; |
| 55 | |
| 56 | /** |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 57 | * struct __sensor_param - Holds individual sensor data |
| 58 | * @sensor_data: sensor driver private data passed as input argument |
| 59 | * @ops: sensor driver ops |
Lina Iyer | 01ffea2 | 2016-07-27 13:46:32 -0600 | [diff] [blame] | 60 | * @trip_high: last trip high value programmed in the sensor driver |
| 61 | * @trip_low: last trip low value programmed in the sensor driver |
| 62 | * @lock: mutex lock acquired before updating the trip temperatures |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 63 | * @first_tz: list head pointing the first thermal zone |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 64 | */ |
| 65 | struct __sensor_param { |
| 66 | void *sensor_data; |
| 67 | const struct thermal_zone_of_device_ops *ops; |
Lina Iyer | 01ffea2 | 2016-07-27 13:46:32 -0600 | [diff] [blame] | 68 | int trip_high, trip_low; |
| 69 | struct mutex lock; |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 70 | struct list_head first_tz; |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | /** |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 74 | * struct __thermal_zone - internal representation of a thermal zone |
| 75 | * @mode: current thermal zone device mode (enabled/disabled) |
| 76 | * @passive_delay: polling interval while passive cooling is activated |
| 77 | * @polling_delay: zone polling interval |
Eduardo Valentin | a46dbae | 2015-05-11 19:48:09 -0700 | [diff] [blame] | 78 | * @slope: slope of the temperature adjustment curve |
| 79 | * @offset: offset of the temperature adjustment curve |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 80 | * @tzd: thermal zone device pointer for this sensor |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 81 | * @ntrips: number of trip points |
| 82 | * @trips: an array of trip points (0..ntrips - 1) |
| 83 | * @num_tbps: number of thermal bind params |
| 84 | * @tbps: an array of thermal bind params (0..num_tbps - 1) |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 85 | * @list: sibling thermal zone pointer |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 86 | * @senps: sensor related parameters |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 87 | */ |
| 88 | |
| 89 | struct __thermal_zone { |
| 90 | enum thermal_device_mode mode; |
| 91 | int passive_delay; |
| 92 | int polling_delay; |
Eduardo Valentin | a46dbae | 2015-05-11 19:48:09 -0700 | [diff] [blame] | 93 | int slope; |
| 94 | int offset; |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 95 | struct thermal_zone_device *tzd; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 96 | |
| 97 | /* trip data */ |
| 98 | int ntrips; |
Lukasz Majewski | ad9914a | 2014-12-08 18:04:19 +0100 | [diff] [blame] | 99 | struct thermal_trip *trips; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 100 | |
| 101 | /* cooling binding data */ |
| 102 | int num_tbps; |
| 103 | struct __thermal_bind_params *tbps; |
| 104 | |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 105 | struct list_head list; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 106 | /* sensor interface */ |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 107 | struct __sensor_param *senps; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 108 | }; |
| 109 | |
Ram Chandrasekar | 2f2b746 | 2017-03-11 19:35:11 -0700 | [diff] [blame^] | 110 | /** |
| 111 | * struct virtual_sensor - internal representation of a virtual thermal zone |
| 112 | * @num_sensors - number of sensors this virtual sensor will reference to |
| 113 | * estimate temperature |
| 114 | * @tz - Array of thermal zones of the sensors this virtual sensor will use |
| 115 | * to estimate temperature |
| 116 | * @logic - aggregation logic to be used to estimate the temperature |
| 117 | * @last_reading - last estimated temperature |
| 118 | * @coefficients - array of coefficients to be used for weighted aggregation |
| 119 | * logic |
| 120 | * @avg_offset - offset value to be used for the weighted aggregation logic |
| 121 | * @avg_denominator - denominator value to be used for the weighted aggregation |
| 122 | * logic |
| 123 | */ |
| 124 | struct virtual_sensor { |
| 125 | int num_sensors; |
| 126 | struct thermal_zone_device *tz[THERMAL_MAX_VIRT_SENSORS]; |
| 127 | enum aggregation_logic logic; |
| 128 | int last_reading; |
| 129 | int coefficients[THERMAL_MAX_VIRT_SENSORS]; |
| 130 | int avg_offset; |
| 131 | int avg_denominator; |
| 132 | }; |
| 133 | |
Lina Iyer | 01ffea2 | 2016-07-27 13:46:32 -0600 | [diff] [blame] | 134 | static int of_thermal_aggregate_trip_types(struct thermal_zone_device *tz, |
| 135 | unsigned int trip_type_mask, int *low, int *high); |
| 136 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 137 | /*** DT thermal zone device callbacks ***/ |
| 138 | |
Ram Chandrasekar | 2f2b746 | 2017-03-11 19:35:11 -0700 | [diff] [blame^] | 139 | static int virt_sensor_read_temp(void *data, int *val) |
| 140 | { |
| 141 | struct virtual_sensor *sens = data; |
| 142 | int idx, temp = 0, ret = 0; |
| 143 | |
| 144 | for (idx = 0; idx < sens->num_sensors; idx++) { |
| 145 | int sens_temp = 0; |
| 146 | |
| 147 | ret = thermal_zone_get_temp(sens->tz[idx], &sens_temp); |
| 148 | if (ret) { |
| 149 | pr_err("virt zone: sensor[%s] read error:%d\n", |
| 150 | sens->tz[idx]->type, ret); |
| 151 | return ret; |
| 152 | } |
| 153 | switch (sens->logic) { |
| 154 | case VIRT_WEIGHTED_AVG: |
| 155 | temp += sens_temp * sens->coefficients[idx]; |
| 156 | if (idx == (sens->num_sensors - 1)) |
| 157 | temp = (temp + sens->avg_offset) |
| 158 | / sens->avg_denominator; |
| 159 | break; |
| 160 | case VIRT_MAXIMUM: |
| 161 | if (idx == 0) |
| 162 | temp = INT_MIN; |
| 163 | if (sens_temp > temp) |
| 164 | temp = sens_temp; |
| 165 | break; |
| 166 | case VIRT_MINIMUM: |
| 167 | if (idx == 0) |
| 168 | temp = INT_MAX; |
| 169 | if (sens_temp < temp) |
| 170 | temp = sens_temp; |
| 171 | break; |
| 172 | default: |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | sens->last_reading = *val = temp; |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 182 | static int of_thermal_get_temp(struct thermal_zone_device *tz, |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 183 | int *temp) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 184 | { |
| 185 | struct __thermal_zone *data = tz->devdata; |
| 186 | |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 187 | if (!data->senps || !data->senps->ops->get_temp) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 188 | return -EINVAL; |
| 189 | |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 190 | return data->senps->ops->get_temp(data->senps->sensor_data, temp); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 191 | } |
| 192 | |
Sascha Hauer | 826386e | 2016-06-22 16:42:02 +0800 | [diff] [blame] | 193 | static int of_thermal_set_trips(struct thermal_zone_device *tz, |
Lina Iyer | 01ffea2 | 2016-07-27 13:46:32 -0600 | [diff] [blame] | 194 | int inp_low, int inp_high) |
Sascha Hauer | 826386e | 2016-06-22 16:42:02 +0800 | [diff] [blame] | 195 | { |
| 196 | struct __thermal_zone *data = tz->devdata; |
Lina Iyer | 01ffea2 | 2016-07-27 13:46:32 -0600 | [diff] [blame] | 197 | int high = INT_MAX, low = INT_MIN, ret = 0; |
Sascha Hauer | 826386e | 2016-06-22 16:42:02 +0800 | [diff] [blame] | 198 | |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 199 | if (!data->senps || !data->senps->ops->set_trips) |
Sascha Hauer | 826386e | 2016-06-22 16:42:02 +0800 | [diff] [blame] | 200 | return -EINVAL; |
| 201 | |
Lina Iyer | 01ffea2 | 2016-07-27 13:46:32 -0600 | [diff] [blame] | 202 | mutex_lock(&data->senps->lock); |
| 203 | of_thermal_aggregate_trip_types(tz, GENMASK(THERMAL_TRIP_CRITICAL, 0), |
| 204 | &low, &high); |
Lina Iyer | 01ffea2 | 2016-07-27 13:46:32 -0600 | [diff] [blame] | 205 | data->senps->trip_low = low; |
| 206 | data->senps->trip_high = high; |
| 207 | ret = data->senps->ops->set_trips(data->senps->sensor_data, |
| 208 | low, high); |
| 209 | |
Lina Iyer | 01ffea2 | 2016-07-27 13:46:32 -0600 | [diff] [blame] | 210 | mutex_unlock(&data->senps->lock); |
| 211 | return ret; |
Sascha Hauer | 826386e | 2016-06-22 16:42:02 +0800 | [diff] [blame] | 212 | } |
| 213 | |
Lukasz Majewski | 08dab66 | 2014-12-08 18:04:17 +0100 | [diff] [blame] | 214 | /** |
| 215 | * of_thermal_get_ntrips - function to export number of available trip |
| 216 | * points. |
| 217 | * @tz: pointer to a thermal zone |
| 218 | * |
| 219 | * This function is a globally visible wrapper to get number of trip points |
| 220 | * stored in the local struct __thermal_zone |
| 221 | * |
| 222 | * Return: number of available trip points, -ENODEV when data not available |
| 223 | */ |
| 224 | int of_thermal_get_ntrips(struct thermal_zone_device *tz) |
| 225 | { |
| 226 | struct __thermal_zone *data = tz->devdata; |
| 227 | |
| 228 | if (!data || IS_ERR(data)) |
| 229 | return -ENODEV; |
| 230 | |
| 231 | return data->ntrips; |
| 232 | } |
| 233 | EXPORT_SYMBOL_GPL(of_thermal_get_ntrips); |
| 234 | |
Lukasz Majewski | a9bf2cc | 2014-12-08 18:04:18 +0100 | [diff] [blame] | 235 | /** |
| 236 | * of_thermal_is_trip_valid - function to check if trip point is valid |
| 237 | * |
| 238 | * @tz: pointer to a thermal zone |
| 239 | * @trip: trip point to evaluate |
| 240 | * |
| 241 | * This function is responsible for checking if passed trip point is valid |
| 242 | * |
| 243 | * Return: true if trip point is valid, false otherwise |
| 244 | */ |
| 245 | bool of_thermal_is_trip_valid(struct thermal_zone_device *tz, int trip) |
| 246 | { |
| 247 | struct __thermal_zone *data = tz->devdata; |
| 248 | |
| 249 | if (!data || trip >= data->ntrips || trip < 0) |
| 250 | return false; |
| 251 | |
| 252 | return true; |
| 253 | } |
| 254 | EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid); |
| 255 | |
Lukasz Majewski | ce8be77 | 2014-12-08 18:04:20 +0100 | [diff] [blame] | 256 | /** |
| 257 | * of_thermal_get_trip_points - function to get access to a globally exported |
| 258 | * trip points |
| 259 | * |
| 260 | * @tz: pointer to a thermal zone |
| 261 | * |
| 262 | * This function provides a pointer to trip points table |
| 263 | * |
| 264 | * Return: pointer to trip points table, NULL otherwise |
| 265 | */ |
Geert Uytterhoeven | ebc3193 | 2015-01-03 22:56:56 +0100 | [diff] [blame] | 266 | const struct thermal_trip * |
Lukasz Majewski | ce8be77 | 2014-12-08 18:04:20 +0100 | [diff] [blame] | 267 | of_thermal_get_trip_points(struct thermal_zone_device *tz) |
| 268 | { |
| 269 | struct __thermal_zone *data = tz->devdata; |
| 270 | |
| 271 | if (!data) |
| 272 | return NULL; |
| 273 | |
| 274 | return data->trips; |
| 275 | } |
| 276 | EXPORT_SYMBOL_GPL(of_thermal_get_trip_points); |
| 277 | |
Lukasz Majewski | 184a4bf | 2014-12-08 18:04:21 +0100 | [diff] [blame] | 278 | /** |
| 279 | * of_thermal_set_emul_temp - function to set emulated temperature |
| 280 | * |
| 281 | * @tz: pointer to a thermal zone |
| 282 | * @temp: temperature to set |
| 283 | * |
| 284 | * This function gives the ability to set emulated value of temperature, |
| 285 | * which is handy for debugging |
| 286 | * |
| 287 | * Return: zero on success, error code otherwise |
| 288 | */ |
| 289 | static int of_thermal_set_emul_temp(struct thermal_zone_device *tz, |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 290 | int temp) |
Lukasz Majewski | 184a4bf | 2014-12-08 18:04:21 +0100 | [diff] [blame] | 291 | { |
| 292 | struct __thermal_zone *data = tz->devdata; |
| 293 | |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 294 | if (!data->senps || !data->senps->ops->set_emul_temp) |
| 295 | return -EINVAL; |
| 296 | |
| 297 | return data->senps->ops->set_emul_temp(data->senps->sensor_data, temp); |
Lukasz Majewski | 184a4bf | 2014-12-08 18:04:21 +0100 | [diff] [blame] | 298 | } |
| 299 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 300 | static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip, |
| 301 | enum thermal_trend *trend) |
| 302 | { |
| 303 | struct __thermal_zone *data = tz->devdata; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 304 | |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 305 | if (!data->senps || !data->senps->ops->get_trend) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 306 | return -EINVAL; |
| 307 | |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 308 | return data->senps->ops->get_trend(data->senps->sensor_data, |
| 309 | trip, trend); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static int of_thermal_bind(struct thermal_zone_device *thermal, |
| 313 | struct thermal_cooling_device *cdev) |
| 314 | { |
| 315 | struct __thermal_zone *data = thermal->devdata; |
| 316 | int i; |
| 317 | |
| 318 | if (!data || IS_ERR(data)) |
| 319 | return -ENODEV; |
| 320 | |
| 321 | /* find where to bind */ |
| 322 | for (i = 0; i < data->num_tbps; i++) { |
| 323 | struct __thermal_bind_params *tbp = data->tbps + i; |
| 324 | |
| 325 | if (tbp->cooling_device == cdev->np) { |
| 326 | int ret; |
| 327 | |
| 328 | ret = thermal_zone_bind_cooling_device(thermal, |
| 329 | tbp->trip_id, cdev, |
Punit Agrawal | dd354b8 | 2014-06-03 10:59:58 +0100 | [diff] [blame] | 330 | tbp->max, |
Kapileshwar Singh | 6cd9e9f | 2015-02-18 16:04:21 +0000 | [diff] [blame] | 331 | tbp->min, |
| 332 | tbp->usage); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 333 | if (ret) |
| 334 | return ret; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static int of_thermal_unbind(struct thermal_zone_device *thermal, |
| 342 | struct thermal_cooling_device *cdev) |
| 343 | { |
| 344 | struct __thermal_zone *data = thermal->devdata; |
| 345 | int i; |
| 346 | |
| 347 | if (!data || IS_ERR(data)) |
| 348 | return -ENODEV; |
| 349 | |
| 350 | /* find where to unbind */ |
| 351 | for (i = 0; i < data->num_tbps; i++) { |
| 352 | struct __thermal_bind_params *tbp = data->tbps + i; |
| 353 | |
| 354 | if (tbp->cooling_device == cdev->np) { |
| 355 | int ret; |
| 356 | |
| 357 | ret = thermal_zone_unbind_cooling_device(thermal, |
| 358 | tbp->trip_id, cdev); |
| 359 | if (ret) |
| 360 | return ret; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | static int of_thermal_get_mode(struct thermal_zone_device *tz, |
| 368 | enum thermal_device_mode *mode) |
| 369 | { |
| 370 | struct __thermal_zone *data = tz->devdata; |
| 371 | |
| 372 | *mode = data->mode; |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | static int of_thermal_set_mode(struct thermal_zone_device *tz, |
| 378 | enum thermal_device_mode mode) |
| 379 | { |
| 380 | struct __thermal_zone *data = tz->devdata; |
| 381 | |
| 382 | mutex_lock(&tz->lock); |
| 383 | |
| 384 | if (mode == THERMAL_DEVICE_ENABLED) |
| 385 | tz->polling_delay = data->polling_delay; |
| 386 | else |
| 387 | tz->polling_delay = 0; |
| 388 | |
| 389 | mutex_unlock(&tz->lock); |
| 390 | |
| 391 | data->mode = mode; |
Srinivas Pandruvada | 0e70f46 | 2016-08-26 16:21:16 -0700 | [diff] [blame] | 392 | thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip, |
| 398 | enum thermal_trip_type *type) |
| 399 | { |
| 400 | struct __thermal_zone *data = tz->devdata; |
| 401 | |
| 402 | if (trip >= data->ntrips || trip < 0) |
| 403 | return -EDOM; |
| 404 | |
| 405 | *type = data->trips[trip].type; |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | 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] | 411 | int *temp) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 412 | { |
| 413 | struct __thermal_zone *data = tz->devdata; |
| 414 | |
| 415 | if (trip >= data->ntrips || trip < 0) |
| 416 | return -EDOM; |
| 417 | |
| 418 | *temp = data->trips[trip].temperature; |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | 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] | 424 | int temp) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 425 | { |
| 426 | struct __thermal_zone *data = tz->devdata; |
| 427 | |
| 428 | if (trip >= data->ntrips || trip < 0) |
| 429 | return -EDOM; |
| 430 | |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 431 | if (data->senps && data->senps->ops->set_trip_temp) { |
Wei Ni | c350952 | 2016-03-29 18:29:17 +0800 | [diff] [blame] | 432 | int ret; |
| 433 | |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 434 | ret = data->senps->ops->set_trip_temp(data->senps->sensor_data, |
| 435 | trip, temp); |
Wei Ni | c350952 | 2016-03-29 18:29:17 +0800 | [diff] [blame] | 436 | if (ret) |
| 437 | return ret; |
| 438 | } |
| 439 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 440 | /* thermal framework should take care of data->mask & (1 << trip) */ |
| 441 | data->trips[trip].temperature = temp; |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | 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] | 447 | int *hyst) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 448 | { |
| 449 | struct __thermal_zone *data = tz->devdata; |
| 450 | |
| 451 | if (trip >= data->ntrips || trip < 0) |
| 452 | return -EDOM; |
| 453 | |
| 454 | *hyst = data->trips[trip].hysteresis; |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | 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] | 460 | int hyst) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 461 | { |
| 462 | struct __thermal_zone *data = tz->devdata; |
| 463 | |
| 464 | if (trip >= data->ntrips || trip < 0) |
| 465 | return -EDOM; |
| 466 | |
| 467 | /* thermal framework should take care of data->mask & (1 << trip) */ |
| 468 | data->trips[trip].hysteresis = hyst; |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | static int of_thermal_get_crit_temp(struct thermal_zone_device *tz, |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 474 | int *temp) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 475 | { |
| 476 | struct __thermal_zone *data = tz->devdata; |
| 477 | int i; |
| 478 | |
| 479 | for (i = 0; i < data->ntrips; i++) |
| 480 | if (data->trips[i].type == THERMAL_TRIP_CRITICAL) { |
| 481 | *temp = data->trips[i].temperature; |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | return -EINVAL; |
| 486 | } |
| 487 | |
Lina Iyer | 01ffea2 | 2016-07-27 13:46:32 -0600 | [diff] [blame] | 488 | static int of_thermal_aggregate_trip_types(struct thermal_zone_device *tz, |
| 489 | unsigned int trip_type_mask, int *low, int *high) |
| 490 | { |
| 491 | int min = INT_MIN; |
| 492 | int max = INT_MAX; |
| 493 | int tt, th, trip; |
| 494 | int temp = tz->temperature; |
| 495 | struct thermal_zone_device *zone = NULL; |
| 496 | struct __thermal_zone *data = tz->devdata; |
| 497 | struct list_head *head; |
| 498 | enum thermal_trip_type type = 0; |
| 499 | |
| 500 | head = &data->senps->first_tz; |
Ram Chandrasekar | 9a1da90 | 2017-05-09 16:58:55 -0600 | [diff] [blame] | 501 | list_for_each_entry(data, head, list) { |
Lina Iyer | 01ffea2 | 2016-07-27 13:46:32 -0600 | [diff] [blame] | 502 | zone = data->tzd; |
| 503 | for (trip = 0; trip < data->ntrips; trip++) { |
| 504 | of_thermal_get_trip_type(zone, trip, &type); |
| 505 | if (!(BIT(type) & trip_type_mask)) |
| 506 | continue; |
| 507 | |
| 508 | if (!zone->tzp->tracks_low) { |
| 509 | tt = data->trips[trip].temperature; |
| 510 | if (tt > temp && tt < max) |
| 511 | max = tt; |
| 512 | th = tt - data->trips[trip].hysteresis; |
| 513 | if (th < temp && th > min) |
| 514 | min = th; |
| 515 | } else { |
| 516 | tt = data->trips[trip].temperature; |
| 517 | if (tt < temp && tt > min) |
| 518 | min = tt; |
| 519 | th = tt + data->trips[trip].hysteresis; |
| 520 | if (th > temp && th < max) |
| 521 | max = th; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | *high = max; |
| 527 | *low = min; |
| 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | * of_thermal_aggregate_trip - aggregate trip temperatures across sibling |
| 534 | * thermal zones. |
| 535 | * @tz: pointer to the primary thermal zone. |
| 536 | * @type: the thermal trip type to be aggregated upon |
| 537 | * @low: the low trip threshold which the most lesser than the @temp |
| 538 | * @high: the high trip threshold which is the least greater than the @temp |
| 539 | */ |
| 540 | int of_thermal_aggregate_trip(struct thermal_zone_device *tz, |
| 541 | enum thermal_trip_type type, |
| 542 | int *low, int *high) |
| 543 | { |
| 544 | if (type <= THERMAL_TRIP_CRITICAL) |
| 545 | return of_thermal_aggregate_trip_types(tz, BIT(type), low, |
| 546 | high); |
| 547 | |
| 548 | return -EINVAL; |
| 549 | } |
| 550 | EXPORT_SYMBOL(of_thermal_aggregate_trip); |
| 551 | |
Ram Chandrasekar | cf54360 | 2017-03-13 22:59:01 -0600 | [diff] [blame] | 552 | /* |
| 553 | * of_thermal_handle_trip - Handle thermal trip from sensors |
| 554 | * |
| 555 | * @tz: pointer to the primary thermal zone. |
| 556 | */ |
| 557 | void of_thermal_handle_trip(struct thermal_zone_device *tz) |
| 558 | { |
| 559 | struct thermal_zone_device *zone; |
| 560 | struct __thermal_zone *data = tz->devdata; |
| 561 | struct list_head *head; |
| 562 | |
| 563 | head = &data->senps->first_tz; |
Ram Chandrasekar | 9a1da90 | 2017-05-09 16:58:55 -0600 | [diff] [blame] | 564 | list_for_each_entry(data, head, list) { |
Ram Chandrasekar | cf54360 | 2017-03-13 22:59:01 -0600 | [diff] [blame] | 565 | zone = data->tzd; |
| 566 | thermal_zone_device_update(zone, THERMAL_EVENT_UNSPECIFIED); |
| 567 | } |
| 568 | } |
| 569 | EXPORT_SYMBOL(of_thermal_handle_trip); |
| 570 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 571 | static struct thermal_zone_device_ops of_thermal_ops = { |
| 572 | .get_mode = of_thermal_get_mode, |
| 573 | .set_mode = of_thermal_set_mode, |
| 574 | |
| 575 | .get_trip_type = of_thermal_get_trip_type, |
| 576 | .get_trip_temp = of_thermal_get_trip_temp, |
| 577 | .set_trip_temp = of_thermal_set_trip_temp, |
| 578 | .get_trip_hyst = of_thermal_get_trip_hyst, |
| 579 | .set_trip_hyst = of_thermal_set_trip_hyst, |
| 580 | .get_crit_temp = of_thermal_get_crit_temp, |
| 581 | |
| 582 | .bind = of_thermal_bind, |
| 583 | .unbind = of_thermal_unbind, |
| 584 | }; |
| 585 | |
Ram Chandrasekar | 2f2b746 | 2017-03-11 19:35:11 -0700 | [diff] [blame^] | 586 | static struct thermal_zone_of_device_ops of_virt_ops = { |
| 587 | .get_temp = virt_sensor_read_temp, |
| 588 | }; |
| 589 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 590 | /*** sensor API ***/ |
| 591 | |
| 592 | static struct thermal_zone_device * |
| 593 | thermal_zone_of_add_sensor(struct device_node *zone, |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 594 | struct device_node *sensor, |
| 595 | struct __sensor_param *sens_param) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 596 | { |
| 597 | struct thermal_zone_device *tzd; |
| 598 | struct __thermal_zone *tz; |
| 599 | |
| 600 | tzd = thermal_zone_get_zone_by_name(zone->name); |
| 601 | if (IS_ERR(tzd)) |
| 602 | return ERR_PTR(-EPROBE_DEFER); |
| 603 | |
| 604 | tz = tzd->devdata; |
| 605 | |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 606 | if (!sens_param->ops) |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 607 | return ERR_PTR(-EINVAL); |
| 608 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 609 | mutex_lock(&tzd->lock); |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 610 | tz->senps = sens_param; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 611 | |
| 612 | tzd->ops->get_temp = of_thermal_get_temp; |
| 613 | tzd->ops->get_trend = of_thermal_get_trend; |
Sascha Hauer | 826386e | 2016-06-22 16:42:02 +0800 | [diff] [blame] | 614 | |
| 615 | /* |
| 616 | * The thermal zone core will calculate the window if they have set the |
| 617 | * optional set_trips pointer. |
| 618 | */ |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 619 | if (sens_param->ops->set_trips) |
Sascha Hauer | 826386e | 2016-06-22 16:42:02 +0800 | [diff] [blame] | 620 | tzd->ops->set_trips = of_thermal_set_trips; |
| 621 | |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 622 | if (sens_param->ops->set_emul_temp) |
Keerthy | e2fa748 | 2016-06-02 14:24:50 +0530 | [diff] [blame] | 623 | tzd->ops->set_emul_temp = of_thermal_set_emul_temp; |
| 624 | |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 625 | list_add_tail(&tz->list, &sens_param->first_tz); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 626 | mutex_unlock(&tzd->lock); |
| 627 | |
| 628 | return tzd; |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone |
| 633 | * @dev: a valid struct device pointer of a sensor device. Must contain |
| 634 | * a valid .of_node, for the sensor node. |
| 635 | * @sensor_id: a sensor identifier, in case the sensor IP has more |
| 636 | * than one sensors |
| 637 | * @data: a private pointer (owned by the caller) that will be passed |
| 638 | * back, when a temperature reading is needed. |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 639 | * @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] | 640 | * |
| 641 | * This function will search the list of thermal zones described in device |
| 642 | * tree and look for the zone that refer to the sensor device pointed by |
| 643 | * @dev->of_node as temperature providers. For the zone pointing to the |
| 644 | * sensor node, the sensor will be added to the DT thermal zone device. |
| 645 | * |
| 646 | * The thermal zone temperature is provided by the @get_temp function |
| 647 | * pointer. When called, it will have the private pointer @data back. |
| 648 | * |
| 649 | * The thermal zone temperature trend is provided by the @get_trend function |
| 650 | * pointer. When called, it will have the private pointer @data back. |
| 651 | * |
| 652 | * TODO: |
| 653 | * 01 - This function must enqueue the new sensor instead of using |
| 654 | * it as the only source of temperature values. |
| 655 | * |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 656 | * Return: On success returns a valid struct thermal_zone_device, |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 657 | * otherwise, it returns a corresponding ERR_PTR(). Incase there are multiple |
| 658 | * thermal zones referencing the same sensor, the return value will be |
| 659 | * thermal_zone_device pointer of the first thermal zone. Caller must |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 660 | * check the return value with help of IS_ERR() helper. |
| 661 | */ |
| 662 | struct thermal_zone_device * |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 663 | thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data, |
| 664 | const struct thermal_zone_of_device_ops *ops) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 665 | { |
| 666 | struct device_node *np, *child, *sensor_np; |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 667 | struct thermal_zone_device *tzd = ERR_PTR(-ENODEV); |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 668 | struct thermal_zone_device *first_tzd = NULL; |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 669 | struct __sensor_param *sens_param = NULL; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 670 | |
| 671 | np = of_find_node_by_name(NULL, "thermal-zones"); |
| 672 | if (!np) |
| 673 | return ERR_PTR(-ENODEV); |
| 674 | |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 675 | if (!dev || !dev->of_node) { |
| 676 | of_node_put(np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 677 | return ERR_PTR(-EINVAL); |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 678 | } |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 679 | |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 680 | sens_param = kzalloc(sizeof(*sens_param), GFP_KERNEL); |
| 681 | if (!sens_param) { |
| 682 | of_node_put(np); |
| 683 | return ERR_PTR(-ENOMEM); |
| 684 | } |
| 685 | sens_param->sensor_data = data; |
| 686 | sens_param->ops = ops; |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 687 | INIT_LIST_HEAD(&sens_param->first_tz); |
Lina Iyer | 01ffea2 | 2016-07-27 13:46:32 -0600 | [diff] [blame] | 688 | sens_param->trip_high = INT_MAX; |
| 689 | sens_param->trip_low = INT_MIN; |
| 690 | mutex_init(&sens_param->lock); |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 691 | sensor_np = of_node_get(dev->of_node); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 692 | |
Laxman Dewangan | 42bbe40 | 2016-02-08 18:58:34 +0530 | [diff] [blame] | 693 | for_each_available_child_of_node(np, child) { |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 694 | struct of_phandle_args sensor_specs; |
| 695 | int ret, id; |
| 696 | |
| 697 | /* For now, thermal framework supports only 1 sensor per zone */ |
| 698 | ret = of_parse_phandle_with_args(child, "thermal-sensors", |
| 699 | "#thermal-sensor-cells", |
| 700 | 0, &sensor_specs); |
| 701 | if (ret) |
| 702 | continue; |
| 703 | |
| 704 | if (sensor_specs.args_count >= 1) { |
| 705 | id = sensor_specs.args[0]; |
| 706 | WARN(sensor_specs.args_count > 1, |
| 707 | "%s: too many cells in sensor specifier %d\n", |
| 708 | sensor_specs.np->name, sensor_specs.args_count); |
| 709 | } else { |
| 710 | id = 0; |
| 711 | } |
| 712 | |
| 713 | if (sensor_specs.np == sensor_np && id == sensor_id) { |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 714 | tzd = thermal_zone_of_add_sensor(child, sensor_np, |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 715 | sens_param); |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 716 | if (!IS_ERR(tzd)) { |
| 717 | if (!first_tzd) |
| 718 | first_tzd = tzd; |
Lukasz Majewski | 528012c1 | 2015-01-19 12:44:04 +0100 | [diff] [blame] | 719 | tzd->ops->set_mode(tzd, THERMAL_DEVICE_ENABLED); |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 720 | } |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 721 | } |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 722 | of_node_put(sensor_specs.np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 723 | } |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 724 | of_node_put(sensor_np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 725 | of_node_put(np); |
| 726 | |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 727 | if (!first_tzd) { |
| 728 | first_tzd = ERR_PTR(-ENODEV); |
Ram Chandrasekar | e34ced6 | 2017-03-03 11:22:50 -0700 | [diff] [blame] | 729 | kfree(sens_param); |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 730 | } |
| 731 | return first_tzd; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 732 | } |
| 733 | EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register); |
| 734 | |
| 735 | /** |
| 736 | * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone |
| 737 | * @dev: a valid struct device pointer of a sensor device. Must contain |
| 738 | * a valid .of_node, for the sensor node. |
| 739 | * @tzd: a pointer to struct thermal_zone_device where the sensor is registered. |
| 740 | * |
| 741 | * This function removes the sensor callbacks and private data from the |
| 742 | * thermal zone device registered with thermal_zone_of_sensor_register() |
| 743 | * API. It will also silent the zone by remove the .get_temp() and .get_trend() |
| 744 | * thermal zone device callbacks. |
| 745 | * |
| 746 | * TODO: When the support to several sensors per zone is added, this |
| 747 | * function must search the sensor list based on @dev parameter. |
| 748 | * |
| 749 | */ |
| 750 | void thermal_zone_of_sensor_unregister(struct device *dev, |
| 751 | struct thermal_zone_device *tzd) |
| 752 | { |
Ram Chandrasekar | 9a1da90 | 2017-05-09 16:58:55 -0600 | [diff] [blame] | 753 | struct __thermal_zone *tz, *next; |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 754 | struct thermal_zone_device *pos; |
| 755 | struct list_head *head; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 756 | |
| 757 | if (!dev || !tzd || !tzd->devdata) |
| 758 | return; |
| 759 | |
| 760 | tz = tzd->devdata; |
| 761 | |
| 762 | /* no __thermal_zone, nothing to be done */ |
| 763 | if (!tz) |
| 764 | return; |
| 765 | |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 766 | head = &tz->senps->first_tz; |
Ram Chandrasekar | 9a1da90 | 2017-05-09 16:58:55 -0600 | [diff] [blame] | 767 | list_for_each_entry_safe(tz, next, head, list) { |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 768 | pos = tz->tzd; |
| 769 | mutex_lock(&pos->lock); |
| 770 | pos->ops->get_temp = NULL; |
| 771 | pos->ops->get_trend = NULL; |
| 772 | pos->ops->set_emul_temp = NULL; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 773 | |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 774 | list_del(&tz->list); |
| 775 | if (list_empty(&tz->senps->first_tz)) |
| 776 | kfree(tz->senps); |
| 777 | tz->senps = NULL; |
| 778 | mutex_unlock(&pos->lock); |
| 779 | } |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 780 | } |
| 781 | EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister); |
| 782 | |
Laxman Dewangan | e498b49 | 2016-03-09 18:40:06 +0530 | [diff] [blame] | 783 | static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res) |
| 784 | { |
| 785 | thermal_zone_of_sensor_unregister(dev, |
| 786 | *(struct thermal_zone_device **)res); |
| 787 | } |
| 788 | |
| 789 | static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res, |
| 790 | void *data) |
| 791 | { |
| 792 | struct thermal_zone_device **r = res; |
| 793 | |
| 794 | if (WARN_ON(!r || !*r)) |
| 795 | return 0; |
| 796 | |
| 797 | return *r == data; |
| 798 | } |
| 799 | |
| 800 | /** |
Ram Chandrasekar | 2f2b746 | 2017-03-11 19:35:11 -0700 | [diff] [blame^] | 801 | * devm_thermal_of_virtual_sensor_register - Register a virtual sensor. |
| 802 | * Three types of virtual sensors are supported. |
| 803 | * 1. Weighted aggregation type: |
| 804 | * Virtual sensor of this type calculates the weighted aggregation |
| 805 | * of sensor temperatures using the below formula, |
| 806 | * temp = (sensor_1_temp * coeff_1 + ... + sensor_n_temp * coeff_n) |
| 807 | * + avg_offset / avg_denominator |
| 808 | * So the sensor drivers has to specify n+2 coefficients. |
| 809 | * 2. Maximum type: |
| 810 | * Virtual sensors of this type will report the maximum of all |
| 811 | * sensor temperatures. |
| 812 | * 3. Minimum type: |
| 813 | * Virtual sensors of this type will report the minimum of all |
| 814 | * sensor temperatures. |
| 815 | * |
| 816 | * @input arguments: |
| 817 | * @dev: Virtual sensor driver device pointer. |
| 818 | * @sensor_data: Virtual sensor data supported for the device. |
| 819 | * |
| 820 | * @return: Returns a virtual thermal zone pointer. Returns error if thermal |
| 821 | * zone is not created. Returns -EAGAIN, if the sensor that is required for |
| 822 | * this virtual sensor temperature estimation is not registered yet. The |
| 823 | * sensor driver can try again later. |
| 824 | */ |
| 825 | struct thermal_zone_device *devm_thermal_of_virtual_sensor_register( |
| 826 | struct device *dev, |
| 827 | const struct virtual_sensor_data *sensor_data) |
| 828 | { |
| 829 | int sens_idx = 0; |
| 830 | struct virtual_sensor *sens; |
| 831 | struct __thermal_zone *tz; |
| 832 | struct thermal_zone_device **ptr; |
| 833 | struct thermal_zone_device *tzd; |
| 834 | struct __sensor_param *sens_param = NULL; |
| 835 | enum thermal_device_mode mode; |
| 836 | |
| 837 | if (!dev || !sensor_data) |
| 838 | return ERR_PTR(-EINVAL); |
| 839 | |
| 840 | tzd = thermal_zone_get_zone_by_name( |
| 841 | sensor_data->virt_zone_name); |
| 842 | if (IS_ERR(tzd)) { |
| 843 | dev_err(dev, "sens:%s not available err: %ld\n", |
| 844 | sensor_data->virt_zone_name, |
| 845 | PTR_ERR(tzd)); |
| 846 | return tzd; |
| 847 | } |
| 848 | |
| 849 | mutex_lock(&tzd->lock); |
| 850 | /* |
| 851 | * Check if the virtual zone is registered and enabled. |
| 852 | * If so return the registered thermal zone. |
| 853 | */ |
| 854 | tzd->ops->get_mode(tzd, &mode); |
| 855 | mutex_unlock(&tzd->lock); |
| 856 | if (mode == THERMAL_DEVICE_ENABLED) |
| 857 | return tzd; |
| 858 | |
| 859 | sens = devm_kzalloc(dev, sizeof(*sens), GFP_KERNEL); |
| 860 | if (!sens) |
| 861 | return ERR_PTR(-ENOMEM); |
| 862 | |
| 863 | sens->logic = sensor_data->logic; |
| 864 | sens->num_sensors = sensor_data->num_sensors; |
| 865 | if (sens->logic == VIRT_WEIGHTED_AVG) { |
| 866 | int coeff_ct = sensor_data->coefficient_ct; |
| 867 | |
| 868 | /* |
| 869 | * For weighted aggregation, sensor drivers has to specify |
| 870 | * n+2 coefficients. |
| 871 | */ |
| 872 | if (coeff_ct != sens->num_sensors) { |
| 873 | dev_err(dev, "sens:%s Invalid coefficient\n", |
| 874 | sensor_data->virt_zone_name); |
| 875 | return ERR_PTR(-EINVAL); |
| 876 | } |
| 877 | memcpy(sens->coefficients, sensor_data->coefficients, |
| 878 | coeff_ct * sizeof(*sens->coefficients)); |
| 879 | sens->avg_offset = sensor_data->avg_offset; |
| 880 | sens->avg_denominator = sensor_data->avg_denominator; |
| 881 | } |
| 882 | |
| 883 | for (sens_idx = 0; sens_idx < sens->num_sensors; sens_idx++) { |
| 884 | sens->tz[sens_idx] = thermal_zone_get_zone_by_name( |
| 885 | sensor_data->sensor_names[sens_idx]); |
| 886 | if (IS_ERR(sens->tz[sens_idx])) { |
| 887 | dev_err(dev, "sens:%s sensor[%s] fetch err:%ld\n", |
| 888 | sensor_data->virt_zone_name, |
| 889 | sensor_data->sensor_names[sens_idx], |
| 890 | PTR_ERR(sens->tz[sens_idx])); |
| 891 | break; |
| 892 | } |
| 893 | } |
| 894 | if (sens->num_sensors != sens_idx) |
| 895 | return ERR_PTR(-EAGAIN); |
| 896 | |
| 897 | sens_param = kzalloc(sizeof(*sens_param), GFP_KERNEL); |
| 898 | if (!sens_param) |
| 899 | return ERR_PTR(-ENOMEM); |
| 900 | sens_param->sensor_data = sens; |
| 901 | sens_param->ops = &of_virt_ops; |
| 902 | INIT_LIST_HEAD(&sens_param->first_tz); |
| 903 | sens_param->trip_high = INT_MAX; |
| 904 | sens_param->trip_low = INT_MIN; |
| 905 | mutex_init(&sens_param->lock); |
| 906 | |
| 907 | mutex_lock(&tzd->lock); |
| 908 | tz = tzd->devdata; |
| 909 | tz->senps = sens_param; |
| 910 | tzd->ops->get_temp = of_thermal_get_temp; |
| 911 | list_add_tail(&tz->list, &sens_param->first_tz); |
| 912 | mutex_unlock(&tzd->lock); |
| 913 | |
| 914 | ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr), |
| 915 | GFP_KERNEL); |
| 916 | if (!ptr) |
| 917 | return ERR_PTR(-ENOMEM); |
| 918 | |
| 919 | *ptr = tzd; |
| 920 | devres_add(dev, ptr); |
| 921 | |
| 922 | tzd->ops->set_mode(tzd, THERMAL_DEVICE_ENABLED); |
| 923 | |
| 924 | return tzd; |
| 925 | } |
| 926 | EXPORT_SYMBOL(devm_thermal_of_virtual_sensor_register); |
| 927 | |
| 928 | /** |
Laxman Dewangan | e498b49 | 2016-03-09 18:40:06 +0530 | [diff] [blame] | 929 | * devm_thermal_zone_of_sensor_register - Resource managed version of |
| 930 | * thermal_zone_of_sensor_register() |
| 931 | * @dev: a valid struct device pointer of a sensor device. Must contain |
| 932 | * a valid .of_node, for the sensor node. |
| 933 | * @sensor_id: a sensor identifier, in case the sensor IP has more |
| 934 | * than one sensors |
| 935 | * @data: a private pointer (owned by the caller) that will be passed |
| 936 | * back, when a temperature reading is needed. |
| 937 | * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp. |
| 938 | * |
| 939 | * Refer thermal_zone_of_sensor_register() for more details. |
| 940 | * |
| 941 | * Return: On success returns a valid struct thermal_zone_device, |
| 942 | * otherwise, it returns a corresponding ERR_PTR(). Caller must |
| 943 | * check the return value with help of IS_ERR() helper. |
Zhang Rui | 7b5c4a0 | 2016-08-22 15:48:11 +0800 | [diff] [blame] | 944 | * Registered thermal_zone_device device will automatically be |
Laxman Dewangan | e498b49 | 2016-03-09 18:40:06 +0530 | [diff] [blame] | 945 | * released when device is unbounded. |
| 946 | */ |
| 947 | struct thermal_zone_device *devm_thermal_zone_of_sensor_register( |
| 948 | struct device *dev, int sensor_id, |
| 949 | void *data, const struct thermal_zone_of_device_ops *ops) |
| 950 | { |
| 951 | struct thermal_zone_device **ptr, *tzd; |
| 952 | |
| 953 | ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr), |
| 954 | GFP_KERNEL); |
| 955 | if (!ptr) |
| 956 | return ERR_PTR(-ENOMEM); |
| 957 | |
| 958 | tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops); |
| 959 | if (IS_ERR(tzd)) { |
| 960 | devres_free(ptr); |
| 961 | return tzd; |
| 962 | } |
| 963 | |
| 964 | *ptr = tzd; |
| 965 | devres_add(dev, ptr); |
| 966 | |
| 967 | return tzd; |
| 968 | } |
| 969 | EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register); |
| 970 | |
| 971 | /** |
| 972 | * devm_thermal_zone_of_sensor_unregister - Resource managed version of |
| 973 | * thermal_zone_of_sensor_unregister(). |
| 974 | * @dev: Device for which which resource was allocated. |
| 975 | * @tzd: a pointer to struct thermal_zone_device where the sensor is registered. |
| 976 | * |
| 977 | * This function removes the sensor callbacks and private data from the |
| 978 | * thermal zone device registered with devm_thermal_zone_of_sensor_register() |
| 979 | * API. It will also silent the zone by remove the .get_temp() and .get_trend() |
| 980 | * thermal zone device callbacks. |
| 981 | * Normally this function will not need to be called and the resource |
| 982 | * management code will ensure that the resource is freed. |
| 983 | */ |
| 984 | void devm_thermal_zone_of_sensor_unregister(struct device *dev, |
| 985 | struct thermal_zone_device *tzd) |
| 986 | { |
| 987 | WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release, |
| 988 | devm_thermal_zone_of_sensor_match, tzd)); |
| 989 | } |
| 990 | EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister); |
| 991 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 992 | /*** functions parsing device tree nodes ***/ |
| 993 | |
| 994 | /** |
| 995 | * thermal_of_populate_bind_params - parse and fill cooling map data |
| 996 | * @np: DT node containing a cooling-map node |
| 997 | * @__tbp: data structure to be filled with cooling map info |
| 998 | * @trips: array of thermal zone trip points |
| 999 | * @ntrips: number of trip points inside trips. |
| 1000 | * |
| 1001 | * This function parses a cooling-map type of node represented by |
| 1002 | * @np parameter and fills the read data into @__tbp data structure. |
| 1003 | * It needs the already parsed array of trip points of the thermal zone |
| 1004 | * in consideration. |
| 1005 | * |
| 1006 | * Return: 0 on success, proper error code otherwise |
| 1007 | */ |
| 1008 | static int thermal_of_populate_bind_params(struct device_node *np, |
| 1009 | struct __thermal_bind_params *__tbp, |
Lukasz Majewski | ad9914a | 2014-12-08 18:04:19 +0100 | [diff] [blame] | 1010 | struct thermal_trip *trips, |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1011 | int ntrips) |
| 1012 | { |
| 1013 | struct of_phandle_args cooling_spec; |
| 1014 | struct device_node *trip; |
| 1015 | int ret, i; |
| 1016 | u32 prop; |
| 1017 | |
| 1018 | /* Default weight. Usage is optional */ |
Kapileshwar Singh | 6cd9e9f | 2015-02-18 16:04:21 +0000 | [diff] [blame] | 1019 | __tbp->usage = THERMAL_WEIGHT_DEFAULT; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1020 | ret = of_property_read_u32(np, "contribution", &prop); |
| 1021 | if (ret == 0) |
| 1022 | __tbp->usage = prop; |
| 1023 | |
| 1024 | trip = of_parse_phandle(np, "trip", 0); |
| 1025 | if (!trip) { |
| 1026 | pr_err("missing trip property\n"); |
| 1027 | return -ENODEV; |
| 1028 | } |
| 1029 | |
| 1030 | /* match using device_node */ |
| 1031 | for (i = 0; i < ntrips; i++) |
| 1032 | if (trip == trips[i].np) { |
| 1033 | __tbp->trip_id = i; |
| 1034 | break; |
| 1035 | } |
| 1036 | |
| 1037 | if (i == ntrips) { |
| 1038 | ret = -ENODEV; |
| 1039 | goto end; |
| 1040 | } |
| 1041 | |
| 1042 | ret = of_parse_phandle_with_args(np, "cooling-device", "#cooling-cells", |
| 1043 | 0, &cooling_spec); |
| 1044 | if (ret < 0) { |
| 1045 | pr_err("missing cooling_device property\n"); |
| 1046 | goto end; |
| 1047 | } |
| 1048 | __tbp->cooling_device = cooling_spec.np; |
| 1049 | if (cooling_spec.args_count >= 2) { /* at least min and max */ |
| 1050 | __tbp->min = cooling_spec.args[0]; |
| 1051 | __tbp->max = cooling_spec.args[1]; |
| 1052 | } else { |
| 1053 | pr_err("wrong reference to cooling device, missing limits\n"); |
| 1054 | } |
| 1055 | |
| 1056 | end: |
| 1057 | of_node_put(trip); |
| 1058 | |
| 1059 | return ret; |
| 1060 | } |
| 1061 | |
| 1062 | /** |
| 1063 | * It maps 'enum thermal_trip_type' found in include/linux/thermal.h |
| 1064 | * into the device tree binding of 'trip', property type. |
| 1065 | */ |
| 1066 | static const char * const trip_types[] = { |
| 1067 | [THERMAL_TRIP_ACTIVE] = "active", |
| 1068 | [THERMAL_TRIP_PASSIVE] = "passive", |
| 1069 | [THERMAL_TRIP_HOT] = "hot", |
| 1070 | [THERMAL_TRIP_CRITICAL] = "critical", |
| 1071 | }; |
| 1072 | |
| 1073 | /** |
| 1074 | * thermal_of_get_trip_type - Get phy mode for given device_node |
| 1075 | * @np: Pointer to the given device_node |
| 1076 | * @type: Pointer to resulting trip type |
| 1077 | * |
| 1078 | * The function gets trip type string from property 'type', |
| 1079 | * and store its index in trip_types table in @type, |
| 1080 | * |
| 1081 | * Return: 0 on success, or errno in error case. |
| 1082 | */ |
| 1083 | static int thermal_of_get_trip_type(struct device_node *np, |
| 1084 | enum thermal_trip_type *type) |
| 1085 | { |
| 1086 | const char *t; |
| 1087 | int err, i; |
| 1088 | |
| 1089 | err = of_property_read_string(np, "type", &t); |
| 1090 | if (err < 0) |
| 1091 | return err; |
| 1092 | |
| 1093 | for (i = 0; i < ARRAY_SIZE(trip_types); i++) |
| 1094 | if (!strcasecmp(t, trip_types[i])) { |
| 1095 | *type = i; |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | return -ENODEV; |
| 1100 | } |
| 1101 | |
| 1102 | /** |
| 1103 | * thermal_of_populate_trip - parse and fill one trip point data |
| 1104 | * @np: DT node containing a trip point node |
| 1105 | * @trip: trip point data structure to be filled up |
| 1106 | * |
| 1107 | * This function parses a trip point type of node represented by |
| 1108 | * @np parameter and fills the read data into @trip data structure. |
| 1109 | * |
| 1110 | * Return: 0 on success, proper error code otherwise |
| 1111 | */ |
| 1112 | static int thermal_of_populate_trip(struct device_node *np, |
Lukasz Majewski | ad9914a | 2014-12-08 18:04:19 +0100 | [diff] [blame] | 1113 | struct thermal_trip *trip) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1114 | { |
| 1115 | int prop; |
| 1116 | int ret; |
| 1117 | |
| 1118 | ret = of_property_read_u32(np, "temperature", &prop); |
| 1119 | if (ret < 0) { |
| 1120 | pr_err("missing temperature property\n"); |
| 1121 | return ret; |
| 1122 | } |
| 1123 | trip->temperature = prop; |
| 1124 | |
| 1125 | ret = of_property_read_u32(np, "hysteresis", &prop); |
| 1126 | if (ret < 0) { |
| 1127 | pr_err("missing hysteresis property\n"); |
| 1128 | return ret; |
| 1129 | } |
| 1130 | trip->hysteresis = prop; |
| 1131 | |
| 1132 | ret = thermal_of_get_trip_type(np, &trip->type); |
| 1133 | if (ret < 0) { |
| 1134 | pr_err("wrong trip type property\n"); |
| 1135 | return ret; |
| 1136 | } |
| 1137 | |
| 1138 | /* Required for cooling map matching */ |
| 1139 | trip->np = np; |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 1140 | of_node_get(np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1141 | |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
| 1145 | /** |
| 1146 | * thermal_of_build_thermal_zone - parse and fill one thermal zone data |
| 1147 | * @np: DT node containing a thermal zone node |
| 1148 | * |
| 1149 | * This function parses a thermal zone type of node represented by |
| 1150 | * @np parameter and fills the read data into a __thermal_zone data structure |
| 1151 | * and return this pointer. |
| 1152 | * |
Eduardo Valentin | a46dbae | 2015-05-11 19:48:09 -0700 | [diff] [blame] | 1153 | * TODO: Missing properties to parse: thermal-sensor-names |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1154 | * |
| 1155 | * Return: On success returns a valid struct __thermal_zone, |
| 1156 | * otherwise, it returns a corresponding ERR_PTR(). Caller must |
| 1157 | * check the return value with help of IS_ERR() helper. |
| 1158 | */ |
Julia Lawall | c0ff8aa | 2016-04-19 14:33:32 +0200 | [diff] [blame] | 1159 | static struct __thermal_zone |
| 1160 | __init *thermal_of_build_thermal_zone(struct device_node *np) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1161 | { |
| 1162 | struct device_node *child = NULL, *gchild; |
| 1163 | struct __thermal_zone *tz; |
| 1164 | int ret, i; |
Eduardo Valentin | a46dbae | 2015-05-11 19:48:09 -0700 | [diff] [blame] | 1165 | u32 prop, coef[2]; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1166 | |
| 1167 | if (!np) { |
| 1168 | pr_err("no thermal zone np\n"); |
| 1169 | return ERR_PTR(-EINVAL); |
| 1170 | } |
| 1171 | |
| 1172 | tz = kzalloc(sizeof(*tz), GFP_KERNEL); |
| 1173 | if (!tz) |
| 1174 | return ERR_PTR(-ENOMEM); |
| 1175 | |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 1176 | INIT_LIST_HEAD(&tz->list); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1177 | ret = of_property_read_u32(np, "polling-delay-passive", &prop); |
| 1178 | if (ret < 0) { |
| 1179 | pr_err("missing polling-delay-passive property\n"); |
| 1180 | goto free_tz; |
| 1181 | } |
| 1182 | tz->passive_delay = prop; |
| 1183 | |
| 1184 | ret = of_property_read_u32(np, "polling-delay", &prop); |
| 1185 | if (ret < 0) { |
| 1186 | pr_err("missing polling-delay property\n"); |
| 1187 | goto free_tz; |
| 1188 | } |
| 1189 | tz->polling_delay = prop; |
| 1190 | |
Eduardo Valentin | a46dbae | 2015-05-11 19:48:09 -0700 | [diff] [blame] | 1191 | /* |
| 1192 | * REVIST: for now, the thermal framework supports only |
| 1193 | * one sensor per thermal zone. Thus, we are considering |
| 1194 | * only the first two values as slope and offset. |
| 1195 | */ |
| 1196 | ret = of_property_read_u32_array(np, "coefficients", coef, 2); |
| 1197 | if (ret == 0) { |
| 1198 | tz->slope = coef[0]; |
| 1199 | tz->offset = coef[1]; |
| 1200 | } else { |
| 1201 | tz->slope = 1; |
| 1202 | tz->offset = 0; |
| 1203 | } |
| 1204 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1205 | /* trips */ |
| 1206 | child = of_get_child_by_name(np, "trips"); |
| 1207 | |
| 1208 | /* No trips provided */ |
| 1209 | if (!child) |
| 1210 | goto finish; |
| 1211 | |
| 1212 | tz->ntrips = of_get_child_count(child); |
| 1213 | if (tz->ntrips == 0) /* must have at least one child */ |
| 1214 | goto finish; |
| 1215 | |
| 1216 | tz->trips = kzalloc(tz->ntrips * sizeof(*tz->trips), GFP_KERNEL); |
| 1217 | if (!tz->trips) { |
| 1218 | ret = -ENOMEM; |
| 1219 | goto free_tz; |
| 1220 | } |
| 1221 | |
| 1222 | i = 0; |
| 1223 | for_each_child_of_node(child, gchild) { |
| 1224 | ret = thermal_of_populate_trip(gchild, &tz->trips[i++]); |
| 1225 | if (ret) |
| 1226 | goto free_trips; |
| 1227 | } |
| 1228 | |
| 1229 | of_node_put(child); |
| 1230 | |
| 1231 | /* cooling-maps */ |
| 1232 | child = of_get_child_by_name(np, "cooling-maps"); |
| 1233 | |
| 1234 | /* cooling-maps not provided */ |
| 1235 | if (!child) |
| 1236 | goto finish; |
| 1237 | |
| 1238 | tz->num_tbps = of_get_child_count(child); |
| 1239 | if (tz->num_tbps == 0) |
| 1240 | goto finish; |
| 1241 | |
| 1242 | tz->tbps = kzalloc(tz->num_tbps * sizeof(*tz->tbps), GFP_KERNEL); |
| 1243 | if (!tz->tbps) { |
| 1244 | ret = -ENOMEM; |
| 1245 | goto free_trips; |
| 1246 | } |
| 1247 | |
| 1248 | i = 0; |
Stephen Boyd | ca9521b | 2014-06-18 16:32:08 -0700 | [diff] [blame] | 1249 | for_each_child_of_node(child, gchild) { |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1250 | ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++], |
| 1251 | tz->trips, tz->ntrips); |
| 1252 | if (ret) |
| 1253 | goto free_tbps; |
Stephen Boyd | ca9521b | 2014-06-18 16:32:08 -0700 | [diff] [blame] | 1254 | } |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1255 | |
| 1256 | finish: |
| 1257 | of_node_put(child); |
| 1258 | tz->mode = THERMAL_DEVICE_DISABLED; |
| 1259 | |
| 1260 | return tz; |
| 1261 | |
| 1262 | free_tbps: |
Ulises Brindis | 1cd91c1 | 2016-03-25 12:55:41 -0700 | [diff] [blame] | 1263 | for (i = i - 1; i >= 0; i--) |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 1264 | of_node_put(tz->tbps[i].cooling_device); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1265 | kfree(tz->tbps); |
| 1266 | free_trips: |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 1267 | for (i = 0; i < tz->ntrips; i++) |
| 1268 | of_node_put(tz->trips[i].np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1269 | kfree(tz->trips); |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 1270 | of_node_put(gchild); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1271 | free_tz: |
| 1272 | kfree(tz); |
| 1273 | of_node_put(child); |
| 1274 | |
| 1275 | return ERR_PTR(ret); |
| 1276 | } |
| 1277 | |
| 1278 | static inline void of_thermal_free_zone(struct __thermal_zone *tz) |
| 1279 | { |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 1280 | int i; |
| 1281 | |
| 1282 | for (i = 0; i < tz->num_tbps; i++) |
| 1283 | of_node_put(tz->tbps[i].cooling_device); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1284 | kfree(tz->tbps); |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 1285 | for (i = 0; i < tz->ntrips; i++) |
| 1286 | of_node_put(tz->trips[i].np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1287 | kfree(tz->trips); |
| 1288 | kfree(tz); |
| 1289 | } |
| 1290 | |
| 1291 | /** |
| 1292 | * of_parse_thermal_zones - parse device tree thermal data |
| 1293 | * |
| 1294 | * Initialization function that can be called by machine initialization |
| 1295 | * code to parse thermal data and populate the thermal framework |
| 1296 | * with hardware thermal zones info. This function only parses thermal zones. |
| 1297 | * Cooling devices and sensor devices nodes are supposed to be parsed |
| 1298 | * by their respective drivers. |
| 1299 | * |
| 1300 | * Return: 0 on success, proper error code otherwise |
| 1301 | * |
| 1302 | */ |
| 1303 | int __init of_parse_thermal_zones(void) |
| 1304 | { |
| 1305 | struct device_node *np, *child; |
| 1306 | struct __thermal_zone *tz; |
| 1307 | struct thermal_zone_device_ops *ops; |
| 1308 | |
| 1309 | np = of_find_node_by_name(NULL, "thermal-zones"); |
| 1310 | if (!np) { |
| 1311 | pr_debug("unable to find thermal zones\n"); |
| 1312 | return 0; /* Run successfully on systems without thermal DT */ |
| 1313 | } |
| 1314 | |
Laxman Dewangan | 42bbe40 | 2016-02-08 18:58:34 +0530 | [diff] [blame] | 1315 | for_each_available_child_of_node(np, child) { |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1316 | struct thermal_zone_device *zone; |
| 1317 | struct thermal_zone_params *tzp; |
Punit Agrawal | 76af549 | 2015-03-03 10:43:04 +0000 | [diff] [blame] | 1318 | int i, mask = 0; |
Punit Agrawal | 647f992 | 2015-02-26 19:00:32 +0000 | [diff] [blame] | 1319 | u32 prop; |
Ram Chandrasekar | caafe20 | 2016-07-19 11:25:46 -0600 | [diff] [blame] | 1320 | const char *governor_name; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1321 | |
| 1322 | tz = thermal_of_build_thermal_zone(child); |
| 1323 | if (IS_ERR(tz)) { |
| 1324 | pr_err("failed to build thermal zone %s: %ld\n", |
| 1325 | child->name, |
| 1326 | PTR_ERR(tz)); |
| 1327 | continue; |
| 1328 | } |
| 1329 | |
| 1330 | ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL); |
| 1331 | if (!ops) |
| 1332 | goto exit_free; |
| 1333 | |
| 1334 | tzp = kzalloc(sizeof(*tzp), GFP_KERNEL); |
| 1335 | if (!tzp) { |
| 1336 | kfree(ops); |
| 1337 | goto exit_free; |
| 1338 | } |
| 1339 | |
| 1340 | /* No hwmon because there might be hwmon drivers registering */ |
| 1341 | tzp->no_hwmon = true; |
| 1342 | |
Ram Chandrasekar | caafe20 | 2016-07-19 11:25:46 -0600 | [diff] [blame] | 1343 | if (!of_property_read_string(child, "thermal-governor", |
| 1344 | &governor_name)) |
| 1345 | strlcpy(tzp->governor_name, governor_name, |
| 1346 | THERMAL_NAME_LENGTH); |
| 1347 | |
Punit Agrawal | 647f992 | 2015-02-26 19:00:32 +0000 | [diff] [blame] | 1348 | if (!of_property_read_u32(child, "sustainable-power", &prop)) |
| 1349 | tzp->sustainable_power = prop; |
| 1350 | |
Punit Agrawal | 76af549 | 2015-03-03 10:43:04 +0000 | [diff] [blame] | 1351 | for (i = 0; i < tz->ntrips; i++) |
| 1352 | mask |= 1 << i; |
| 1353 | |
Eduardo Valentin | a46dbae | 2015-05-11 19:48:09 -0700 | [diff] [blame] | 1354 | /* these two are left for temperature drivers to use */ |
| 1355 | tzp->slope = tz->slope; |
| 1356 | tzp->offset = tz->offset; |
| 1357 | |
Lina Iyer | 159f67d | 2016-07-27 11:34:46 -0600 | [diff] [blame] | 1358 | if (of_property_read_bool(child, "tracks-low")) |
| 1359 | tzp->tracks_low = true; |
| 1360 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1361 | zone = thermal_zone_device_register(child->name, tz->ntrips, |
Punit Agrawal | 76af549 | 2015-03-03 10:43:04 +0000 | [diff] [blame] | 1362 | mask, tz, |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1363 | ops, tzp, |
| 1364 | tz->passive_delay, |
| 1365 | tz->polling_delay); |
| 1366 | if (IS_ERR(zone)) { |
| 1367 | pr_err("Failed to build %s zone %ld\n", child->name, |
| 1368 | PTR_ERR(zone)); |
| 1369 | kfree(tzp); |
| 1370 | kfree(ops); |
| 1371 | of_thermal_free_zone(tz); |
| 1372 | /* attempting to build remaining zones still */ |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 1373 | continue; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1374 | } |
Ram Chandrasekar | d29230b | 2017-02-27 11:26:51 -0700 | [diff] [blame] | 1375 | tz->tzd = zone; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1376 | } |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 1377 | of_node_put(np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1378 | |
| 1379 | return 0; |
| 1380 | |
| 1381 | exit_free: |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 1382 | of_node_put(child); |
| 1383 | of_node_put(np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1384 | of_thermal_free_zone(tz); |
| 1385 | |
| 1386 | /* no memory available, so free what we have built */ |
| 1387 | of_thermal_destroy_zones(); |
| 1388 | |
| 1389 | return -ENOMEM; |
| 1390 | } |
| 1391 | |
| 1392 | /** |
| 1393 | * of_thermal_destroy_zones - remove all zones parsed and allocated resources |
| 1394 | * |
| 1395 | * Finds all zones parsed and added to the thermal framework and remove them |
| 1396 | * from the system, together with their resources. |
| 1397 | * |
| 1398 | */ |
| 1399 | void of_thermal_destroy_zones(void) |
| 1400 | { |
| 1401 | struct device_node *np, *child; |
| 1402 | |
| 1403 | np = of_find_node_by_name(NULL, "thermal-zones"); |
| 1404 | if (!np) { |
Jiada Wang | 2852498 | 2015-11-16 17:10:05 +0900 | [diff] [blame] | 1405 | pr_debug("unable to find thermal zones\n"); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1406 | return; |
| 1407 | } |
| 1408 | |
Laxman Dewangan | 42bbe40 | 2016-02-08 18:58:34 +0530 | [diff] [blame] | 1409 | for_each_available_child_of_node(np, child) { |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1410 | struct thermal_zone_device *zone; |
| 1411 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1412 | zone = thermal_zone_get_zone_by_name(child->name); |
| 1413 | if (IS_ERR(zone)) |
| 1414 | continue; |
| 1415 | |
| 1416 | thermal_zone_device_unregister(zone); |
| 1417 | kfree(zone->tzp); |
| 1418 | kfree(zone->ops); |
| 1419 | of_thermal_free_zone(zone->devdata); |
| 1420 | } |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 1421 | of_node_put(np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1422 | } |