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