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> |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 34 | |
| 35 | #include "thermal_core.h" |
| 36 | |
| 37 | /*** Private data structures to represent thermal device tree data ***/ |
| 38 | |
| 39 | /** |
| 40 | * struct __thermal_trip - representation of a point in temperature domain |
| 41 | * @np: pointer to struct device_node that this trip point was created from |
| 42 | * @temperature: temperature value in miliCelsius |
| 43 | * @hysteresis: relative hysteresis in miliCelsius |
| 44 | * @type: trip point type |
| 45 | */ |
| 46 | |
| 47 | struct __thermal_trip { |
| 48 | struct device_node *np; |
| 49 | unsigned long int temperature; |
| 50 | unsigned long int hysteresis; |
| 51 | enum thermal_trip_type type; |
| 52 | }; |
| 53 | |
| 54 | /** |
| 55 | * struct __thermal_bind_param - a match between trip and cooling device |
| 56 | * @cooling_device: a pointer to identify the referred cooling device |
| 57 | * @trip_id: the trip point index |
| 58 | * @usage: the percentage (from 0 to 100) of cooling contribution |
| 59 | * @min: minimum cooling state used at this trip point |
| 60 | * @max: maximum cooling state used at this trip point |
| 61 | */ |
| 62 | |
| 63 | struct __thermal_bind_params { |
| 64 | struct device_node *cooling_device; |
| 65 | unsigned int trip_id; |
| 66 | unsigned int usage; |
| 67 | unsigned long min; |
| 68 | unsigned long max; |
| 69 | }; |
| 70 | |
| 71 | /** |
| 72 | * struct __thermal_zone - internal representation of a thermal zone |
| 73 | * @mode: current thermal zone device mode (enabled/disabled) |
| 74 | * @passive_delay: polling interval while passive cooling is activated |
| 75 | * @polling_delay: zone polling interval |
| 76 | * @ntrips: number of trip points |
| 77 | * @trips: an array of trip points (0..ntrips - 1) |
| 78 | * @num_tbps: number of thermal bind params |
| 79 | * @tbps: an array of thermal bind params (0..num_tbps - 1) |
| 80 | * @sensor_data: sensor private data used while reading temperature and trend |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 81 | * @ops: set of callbacks to handle the thermal zone based on DT |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 82 | */ |
| 83 | |
| 84 | struct __thermal_zone { |
| 85 | enum thermal_device_mode mode; |
| 86 | int passive_delay; |
| 87 | int polling_delay; |
| 88 | |
| 89 | /* trip data */ |
| 90 | int ntrips; |
| 91 | struct __thermal_trip *trips; |
| 92 | |
| 93 | /* cooling binding data */ |
| 94 | int num_tbps; |
| 95 | struct __thermal_bind_params *tbps; |
| 96 | |
| 97 | /* sensor interface */ |
| 98 | void *sensor_data; |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 99 | const struct thermal_zone_of_device_ops *ops; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | /*** DT thermal zone device callbacks ***/ |
| 103 | |
| 104 | static int of_thermal_get_temp(struct thermal_zone_device *tz, |
| 105 | unsigned long *temp) |
| 106 | { |
| 107 | struct __thermal_zone *data = tz->devdata; |
| 108 | |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 109 | if (!data->ops->get_temp) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 110 | return -EINVAL; |
| 111 | |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 112 | return data->ops->get_temp(data->sensor_data, temp); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 113 | } |
| 114 | |
Lukasz Majewski | 08dab66 | 2014-12-08 18:04:17 +0100 | [diff] [blame^] | 115 | /** |
| 116 | * of_thermal_get_ntrips - function to export number of available trip |
| 117 | * points. |
| 118 | * @tz: pointer to a thermal zone |
| 119 | * |
| 120 | * This function is a globally visible wrapper to get number of trip points |
| 121 | * stored in the local struct __thermal_zone |
| 122 | * |
| 123 | * Return: number of available trip points, -ENODEV when data not available |
| 124 | */ |
| 125 | int of_thermal_get_ntrips(struct thermal_zone_device *tz) |
| 126 | { |
| 127 | struct __thermal_zone *data = tz->devdata; |
| 128 | |
| 129 | if (!data || IS_ERR(data)) |
| 130 | return -ENODEV; |
| 131 | |
| 132 | return data->ntrips; |
| 133 | } |
| 134 | EXPORT_SYMBOL_GPL(of_thermal_get_ntrips); |
| 135 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 136 | static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip, |
| 137 | enum thermal_trend *trend) |
| 138 | { |
| 139 | struct __thermal_zone *data = tz->devdata; |
| 140 | long dev_trend; |
| 141 | int r; |
| 142 | |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 143 | if (!data->ops->get_trend) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 144 | return -EINVAL; |
| 145 | |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 146 | r = data->ops->get_trend(data->sensor_data, &dev_trend); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 147 | if (r) |
| 148 | return r; |
| 149 | |
| 150 | /* TODO: These intervals might have some thresholds, but in core code */ |
| 151 | if (dev_trend > 0) |
| 152 | *trend = THERMAL_TREND_RAISING; |
| 153 | else if (dev_trend < 0) |
| 154 | *trend = THERMAL_TREND_DROPPING; |
| 155 | else |
| 156 | *trend = THERMAL_TREND_STABLE; |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static int of_thermal_bind(struct thermal_zone_device *thermal, |
| 162 | struct thermal_cooling_device *cdev) |
| 163 | { |
| 164 | struct __thermal_zone *data = thermal->devdata; |
| 165 | int i; |
| 166 | |
| 167 | if (!data || IS_ERR(data)) |
| 168 | return -ENODEV; |
| 169 | |
| 170 | /* find where to bind */ |
| 171 | for (i = 0; i < data->num_tbps; i++) { |
| 172 | struct __thermal_bind_params *tbp = data->tbps + i; |
| 173 | |
| 174 | if (tbp->cooling_device == cdev->np) { |
| 175 | int ret; |
| 176 | |
| 177 | ret = thermal_zone_bind_cooling_device(thermal, |
| 178 | tbp->trip_id, cdev, |
Punit Agrawal | dd354b8 | 2014-06-03 10:59:58 +0100 | [diff] [blame] | 179 | tbp->max, |
| 180 | tbp->min); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 181 | if (ret) |
| 182 | return ret; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static int of_thermal_unbind(struct thermal_zone_device *thermal, |
| 190 | struct thermal_cooling_device *cdev) |
| 191 | { |
| 192 | struct __thermal_zone *data = thermal->devdata; |
| 193 | int i; |
| 194 | |
| 195 | if (!data || IS_ERR(data)) |
| 196 | return -ENODEV; |
| 197 | |
| 198 | /* find where to unbind */ |
| 199 | for (i = 0; i < data->num_tbps; i++) { |
| 200 | struct __thermal_bind_params *tbp = data->tbps + i; |
| 201 | |
| 202 | if (tbp->cooling_device == cdev->np) { |
| 203 | int ret; |
| 204 | |
| 205 | ret = thermal_zone_unbind_cooling_device(thermal, |
| 206 | tbp->trip_id, cdev); |
| 207 | if (ret) |
| 208 | return ret; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static int of_thermal_get_mode(struct thermal_zone_device *tz, |
| 216 | enum thermal_device_mode *mode) |
| 217 | { |
| 218 | struct __thermal_zone *data = tz->devdata; |
| 219 | |
| 220 | *mode = data->mode; |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static int of_thermal_set_mode(struct thermal_zone_device *tz, |
| 226 | enum thermal_device_mode mode) |
| 227 | { |
| 228 | struct __thermal_zone *data = tz->devdata; |
| 229 | |
| 230 | mutex_lock(&tz->lock); |
| 231 | |
| 232 | if (mode == THERMAL_DEVICE_ENABLED) |
| 233 | tz->polling_delay = data->polling_delay; |
| 234 | else |
| 235 | tz->polling_delay = 0; |
| 236 | |
| 237 | mutex_unlock(&tz->lock); |
| 238 | |
| 239 | data->mode = mode; |
| 240 | thermal_zone_device_update(tz); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip, |
| 246 | enum thermal_trip_type *type) |
| 247 | { |
| 248 | struct __thermal_zone *data = tz->devdata; |
| 249 | |
| 250 | if (trip >= data->ntrips || trip < 0) |
| 251 | return -EDOM; |
| 252 | |
| 253 | *type = data->trips[trip].type; |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip, |
| 259 | unsigned long *temp) |
| 260 | { |
| 261 | struct __thermal_zone *data = tz->devdata; |
| 262 | |
| 263 | if (trip >= data->ntrips || trip < 0) |
| 264 | return -EDOM; |
| 265 | |
| 266 | *temp = data->trips[trip].temperature; |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip, |
| 272 | unsigned long temp) |
| 273 | { |
| 274 | struct __thermal_zone *data = tz->devdata; |
| 275 | |
| 276 | if (trip >= data->ntrips || trip < 0) |
| 277 | return -EDOM; |
| 278 | |
| 279 | /* thermal framework should take care of data->mask & (1 << trip) */ |
| 280 | data->trips[trip].temperature = temp; |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip, |
| 286 | unsigned long *hyst) |
| 287 | { |
| 288 | struct __thermal_zone *data = tz->devdata; |
| 289 | |
| 290 | if (trip >= data->ntrips || trip < 0) |
| 291 | return -EDOM; |
| 292 | |
| 293 | *hyst = data->trips[trip].hysteresis; |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip, |
| 299 | unsigned long hyst) |
| 300 | { |
| 301 | struct __thermal_zone *data = tz->devdata; |
| 302 | |
| 303 | if (trip >= data->ntrips || trip < 0) |
| 304 | return -EDOM; |
| 305 | |
| 306 | /* thermal framework should take care of data->mask & (1 << trip) */ |
| 307 | data->trips[trip].hysteresis = hyst; |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static int of_thermal_get_crit_temp(struct thermal_zone_device *tz, |
| 313 | unsigned long *temp) |
| 314 | { |
| 315 | struct __thermal_zone *data = tz->devdata; |
| 316 | int i; |
| 317 | |
| 318 | for (i = 0; i < data->ntrips; i++) |
| 319 | if (data->trips[i].type == THERMAL_TRIP_CRITICAL) { |
| 320 | *temp = data->trips[i].temperature; |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | return -EINVAL; |
| 325 | } |
| 326 | |
| 327 | static struct thermal_zone_device_ops of_thermal_ops = { |
| 328 | .get_mode = of_thermal_get_mode, |
| 329 | .set_mode = of_thermal_set_mode, |
| 330 | |
| 331 | .get_trip_type = of_thermal_get_trip_type, |
| 332 | .get_trip_temp = of_thermal_get_trip_temp, |
| 333 | .set_trip_temp = of_thermal_set_trip_temp, |
| 334 | .get_trip_hyst = of_thermal_get_trip_hyst, |
| 335 | .set_trip_hyst = of_thermal_set_trip_hyst, |
| 336 | .get_crit_temp = of_thermal_get_crit_temp, |
| 337 | |
| 338 | .bind = of_thermal_bind, |
| 339 | .unbind = of_thermal_unbind, |
| 340 | }; |
| 341 | |
| 342 | /*** sensor API ***/ |
| 343 | |
| 344 | static struct thermal_zone_device * |
| 345 | thermal_zone_of_add_sensor(struct device_node *zone, |
| 346 | struct device_node *sensor, void *data, |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 347 | const struct thermal_zone_of_device_ops *ops) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 348 | { |
| 349 | struct thermal_zone_device *tzd; |
| 350 | struct __thermal_zone *tz; |
| 351 | |
| 352 | tzd = thermal_zone_get_zone_by_name(zone->name); |
| 353 | if (IS_ERR(tzd)) |
| 354 | return ERR_PTR(-EPROBE_DEFER); |
| 355 | |
| 356 | tz = tzd->devdata; |
| 357 | |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 358 | if (!ops) |
| 359 | return ERR_PTR(-EINVAL); |
| 360 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 361 | mutex_lock(&tzd->lock); |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 362 | tz->ops = ops; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 363 | tz->sensor_data = data; |
| 364 | |
| 365 | tzd->ops->get_temp = of_thermal_get_temp; |
| 366 | tzd->ops->get_trend = of_thermal_get_trend; |
| 367 | mutex_unlock(&tzd->lock); |
| 368 | |
| 369 | return tzd; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone |
| 374 | * @dev: a valid struct device pointer of a sensor device. Must contain |
| 375 | * a valid .of_node, for the sensor node. |
| 376 | * @sensor_id: a sensor identifier, in case the sensor IP has more |
| 377 | * than one sensors |
| 378 | * @data: a private pointer (owned by the caller) that will be passed |
| 379 | * back, when a temperature reading is needed. |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 380 | * @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] | 381 | * |
| 382 | * This function will search the list of thermal zones described in device |
| 383 | * tree and look for the zone that refer to the sensor device pointed by |
| 384 | * @dev->of_node as temperature providers. For the zone pointing to the |
| 385 | * sensor node, the sensor will be added to the DT thermal zone device. |
| 386 | * |
| 387 | * The thermal zone temperature is provided by the @get_temp function |
| 388 | * pointer. When called, it will have the private pointer @data back. |
| 389 | * |
| 390 | * The thermal zone temperature trend is provided by the @get_trend function |
| 391 | * pointer. When called, it will have the private pointer @data back. |
| 392 | * |
| 393 | * TODO: |
| 394 | * 01 - This function must enqueue the new sensor instead of using |
| 395 | * it as the only source of temperature values. |
| 396 | * |
| 397 | * 02 - There must be a way to match the sensor with all thermal zones |
| 398 | * that refer to it. |
| 399 | * |
| 400 | * Return: On success returns a valid struct thermal_zone_device, |
| 401 | * otherwise, it returns a corresponding ERR_PTR(). Caller must |
| 402 | * check the return value with help of IS_ERR() helper. |
| 403 | */ |
| 404 | struct thermal_zone_device * |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 405 | thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data, |
| 406 | const struct thermal_zone_of_device_ops *ops) |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 407 | { |
| 408 | struct device_node *np, *child, *sensor_np; |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 409 | struct thermal_zone_device *tzd = ERR_PTR(-ENODEV); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 410 | |
| 411 | np = of_find_node_by_name(NULL, "thermal-zones"); |
| 412 | if (!np) |
| 413 | return ERR_PTR(-ENODEV); |
| 414 | |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 415 | if (!dev || !dev->of_node) { |
| 416 | of_node_put(np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 417 | return ERR_PTR(-EINVAL); |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 418 | } |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 419 | |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 420 | sensor_np = of_node_get(dev->of_node); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 421 | |
| 422 | for_each_child_of_node(np, child) { |
| 423 | struct of_phandle_args sensor_specs; |
| 424 | int ret, id; |
| 425 | |
Laxman Dewangan | a020279 | 2014-07-25 15:31:58 +0530 | [diff] [blame] | 426 | /* Check whether child is enabled or not */ |
| 427 | if (!of_device_is_available(child)) |
| 428 | continue; |
| 429 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 430 | /* For now, thermal framework supports only 1 sensor per zone */ |
| 431 | ret = of_parse_phandle_with_args(child, "thermal-sensors", |
| 432 | "#thermal-sensor-cells", |
| 433 | 0, &sensor_specs); |
| 434 | if (ret) |
| 435 | continue; |
| 436 | |
| 437 | if (sensor_specs.args_count >= 1) { |
| 438 | id = sensor_specs.args[0]; |
| 439 | WARN(sensor_specs.args_count > 1, |
| 440 | "%s: too many cells in sensor specifier %d\n", |
| 441 | sensor_specs.np->name, sensor_specs.args_count); |
| 442 | } else { |
| 443 | id = 0; |
| 444 | } |
| 445 | |
| 446 | if (sensor_specs.np == sensor_np && id == sensor_id) { |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 447 | tzd = thermal_zone_of_add_sensor(child, sensor_np, |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 448 | data, ops); |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 449 | of_node_put(sensor_specs.np); |
| 450 | of_node_put(child); |
| 451 | goto exit; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 452 | } |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 453 | of_node_put(sensor_specs.np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 454 | } |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 455 | exit: |
| 456 | of_node_put(sensor_np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 457 | of_node_put(np); |
| 458 | |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 459 | return tzd; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 460 | } |
| 461 | EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register); |
| 462 | |
| 463 | /** |
| 464 | * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone |
| 465 | * @dev: a valid struct device pointer of a sensor device. Must contain |
| 466 | * a valid .of_node, for the sensor node. |
| 467 | * @tzd: a pointer to struct thermal_zone_device where the sensor is registered. |
| 468 | * |
| 469 | * This function removes the sensor callbacks and private data from the |
| 470 | * thermal zone device registered with thermal_zone_of_sensor_register() |
| 471 | * API. It will also silent the zone by remove the .get_temp() and .get_trend() |
| 472 | * thermal zone device callbacks. |
| 473 | * |
| 474 | * TODO: When the support to several sensors per zone is added, this |
| 475 | * function must search the sensor list based on @dev parameter. |
| 476 | * |
| 477 | */ |
| 478 | void thermal_zone_of_sensor_unregister(struct device *dev, |
| 479 | struct thermal_zone_device *tzd) |
| 480 | { |
| 481 | struct __thermal_zone *tz; |
| 482 | |
| 483 | if (!dev || !tzd || !tzd->devdata) |
| 484 | return; |
| 485 | |
| 486 | tz = tzd->devdata; |
| 487 | |
| 488 | /* no __thermal_zone, nothing to be done */ |
| 489 | if (!tz) |
| 490 | return; |
| 491 | |
| 492 | mutex_lock(&tzd->lock); |
| 493 | tzd->ops->get_temp = NULL; |
| 494 | tzd->ops->get_trend = NULL; |
| 495 | |
Eduardo Valentin | 2251aef | 2014-11-07 21:24:39 -0400 | [diff] [blame] | 496 | tz->ops = NULL; |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 497 | tz->sensor_data = NULL; |
| 498 | mutex_unlock(&tzd->lock); |
| 499 | } |
| 500 | EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister); |
| 501 | |
| 502 | /*** functions parsing device tree nodes ***/ |
| 503 | |
| 504 | /** |
| 505 | * thermal_of_populate_bind_params - parse and fill cooling map data |
| 506 | * @np: DT node containing a cooling-map node |
| 507 | * @__tbp: data structure to be filled with cooling map info |
| 508 | * @trips: array of thermal zone trip points |
| 509 | * @ntrips: number of trip points inside trips. |
| 510 | * |
| 511 | * This function parses a cooling-map type of node represented by |
| 512 | * @np parameter and fills the read data into @__tbp data structure. |
| 513 | * It needs the already parsed array of trip points of the thermal zone |
| 514 | * in consideration. |
| 515 | * |
| 516 | * Return: 0 on success, proper error code otherwise |
| 517 | */ |
| 518 | static int thermal_of_populate_bind_params(struct device_node *np, |
| 519 | struct __thermal_bind_params *__tbp, |
| 520 | struct __thermal_trip *trips, |
| 521 | int ntrips) |
| 522 | { |
| 523 | struct of_phandle_args cooling_spec; |
| 524 | struct device_node *trip; |
| 525 | int ret, i; |
| 526 | u32 prop; |
| 527 | |
| 528 | /* Default weight. Usage is optional */ |
| 529 | __tbp->usage = 0; |
| 530 | ret = of_property_read_u32(np, "contribution", &prop); |
| 531 | if (ret == 0) |
| 532 | __tbp->usage = prop; |
| 533 | |
| 534 | trip = of_parse_phandle(np, "trip", 0); |
| 535 | if (!trip) { |
| 536 | pr_err("missing trip property\n"); |
| 537 | return -ENODEV; |
| 538 | } |
| 539 | |
| 540 | /* match using device_node */ |
| 541 | for (i = 0; i < ntrips; i++) |
| 542 | if (trip == trips[i].np) { |
| 543 | __tbp->trip_id = i; |
| 544 | break; |
| 545 | } |
| 546 | |
| 547 | if (i == ntrips) { |
| 548 | ret = -ENODEV; |
| 549 | goto end; |
| 550 | } |
| 551 | |
| 552 | ret = of_parse_phandle_with_args(np, "cooling-device", "#cooling-cells", |
| 553 | 0, &cooling_spec); |
| 554 | if (ret < 0) { |
| 555 | pr_err("missing cooling_device property\n"); |
| 556 | goto end; |
| 557 | } |
| 558 | __tbp->cooling_device = cooling_spec.np; |
| 559 | if (cooling_spec.args_count >= 2) { /* at least min and max */ |
| 560 | __tbp->min = cooling_spec.args[0]; |
| 561 | __tbp->max = cooling_spec.args[1]; |
| 562 | } else { |
| 563 | pr_err("wrong reference to cooling device, missing limits\n"); |
| 564 | } |
| 565 | |
| 566 | end: |
| 567 | of_node_put(trip); |
| 568 | |
| 569 | return ret; |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * It maps 'enum thermal_trip_type' found in include/linux/thermal.h |
| 574 | * into the device tree binding of 'trip', property type. |
| 575 | */ |
| 576 | static const char * const trip_types[] = { |
| 577 | [THERMAL_TRIP_ACTIVE] = "active", |
| 578 | [THERMAL_TRIP_PASSIVE] = "passive", |
| 579 | [THERMAL_TRIP_HOT] = "hot", |
| 580 | [THERMAL_TRIP_CRITICAL] = "critical", |
| 581 | }; |
| 582 | |
| 583 | /** |
| 584 | * thermal_of_get_trip_type - Get phy mode for given device_node |
| 585 | * @np: Pointer to the given device_node |
| 586 | * @type: Pointer to resulting trip type |
| 587 | * |
| 588 | * The function gets trip type string from property 'type', |
| 589 | * and store its index in trip_types table in @type, |
| 590 | * |
| 591 | * Return: 0 on success, or errno in error case. |
| 592 | */ |
| 593 | static int thermal_of_get_trip_type(struct device_node *np, |
| 594 | enum thermal_trip_type *type) |
| 595 | { |
| 596 | const char *t; |
| 597 | int err, i; |
| 598 | |
| 599 | err = of_property_read_string(np, "type", &t); |
| 600 | if (err < 0) |
| 601 | return err; |
| 602 | |
| 603 | for (i = 0; i < ARRAY_SIZE(trip_types); i++) |
| 604 | if (!strcasecmp(t, trip_types[i])) { |
| 605 | *type = i; |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | return -ENODEV; |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * thermal_of_populate_trip - parse and fill one trip point data |
| 614 | * @np: DT node containing a trip point node |
| 615 | * @trip: trip point data structure to be filled up |
| 616 | * |
| 617 | * This function parses a trip point type of node represented by |
| 618 | * @np parameter and fills the read data into @trip data structure. |
| 619 | * |
| 620 | * Return: 0 on success, proper error code otherwise |
| 621 | */ |
| 622 | static int thermal_of_populate_trip(struct device_node *np, |
| 623 | struct __thermal_trip *trip) |
| 624 | { |
| 625 | int prop; |
| 626 | int ret; |
| 627 | |
| 628 | ret = of_property_read_u32(np, "temperature", &prop); |
| 629 | if (ret < 0) { |
| 630 | pr_err("missing temperature property\n"); |
| 631 | return ret; |
| 632 | } |
| 633 | trip->temperature = prop; |
| 634 | |
| 635 | ret = of_property_read_u32(np, "hysteresis", &prop); |
| 636 | if (ret < 0) { |
| 637 | pr_err("missing hysteresis property\n"); |
| 638 | return ret; |
| 639 | } |
| 640 | trip->hysteresis = prop; |
| 641 | |
| 642 | ret = thermal_of_get_trip_type(np, &trip->type); |
| 643 | if (ret < 0) { |
| 644 | pr_err("wrong trip type property\n"); |
| 645 | return ret; |
| 646 | } |
| 647 | |
| 648 | /* Required for cooling map matching */ |
| 649 | trip->np = np; |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 650 | of_node_get(np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * thermal_of_build_thermal_zone - parse and fill one thermal zone data |
| 657 | * @np: DT node containing a thermal zone node |
| 658 | * |
| 659 | * This function parses a thermal zone type of node represented by |
| 660 | * @np parameter and fills the read data into a __thermal_zone data structure |
| 661 | * and return this pointer. |
| 662 | * |
| 663 | * TODO: Missing properties to parse: thermal-sensor-names and coefficients |
| 664 | * |
| 665 | * Return: On success returns a valid struct __thermal_zone, |
| 666 | * otherwise, it returns a corresponding ERR_PTR(). Caller must |
| 667 | * check the return value with help of IS_ERR() helper. |
| 668 | */ |
| 669 | static struct __thermal_zone * |
| 670 | thermal_of_build_thermal_zone(struct device_node *np) |
| 671 | { |
| 672 | struct device_node *child = NULL, *gchild; |
| 673 | struct __thermal_zone *tz; |
| 674 | int ret, i; |
| 675 | u32 prop; |
| 676 | |
| 677 | if (!np) { |
| 678 | pr_err("no thermal zone np\n"); |
| 679 | return ERR_PTR(-EINVAL); |
| 680 | } |
| 681 | |
| 682 | tz = kzalloc(sizeof(*tz), GFP_KERNEL); |
| 683 | if (!tz) |
| 684 | return ERR_PTR(-ENOMEM); |
| 685 | |
| 686 | ret = of_property_read_u32(np, "polling-delay-passive", &prop); |
| 687 | if (ret < 0) { |
| 688 | pr_err("missing polling-delay-passive property\n"); |
| 689 | goto free_tz; |
| 690 | } |
| 691 | tz->passive_delay = prop; |
| 692 | |
| 693 | ret = of_property_read_u32(np, "polling-delay", &prop); |
| 694 | if (ret < 0) { |
| 695 | pr_err("missing polling-delay property\n"); |
| 696 | goto free_tz; |
| 697 | } |
| 698 | tz->polling_delay = prop; |
| 699 | |
| 700 | /* trips */ |
| 701 | child = of_get_child_by_name(np, "trips"); |
| 702 | |
| 703 | /* No trips provided */ |
| 704 | if (!child) |
| 705 | goto finish; |
| 706 | |
| 707 | tz->ntrips = of_get_child_count(child); |
| 708 | if (tz->ntrips == 0) /* must have at least one child */ |
| 709 | goto finish; |
| 710 | |
| 711 | tz->trips = kzalloc(tz->ntrips * sizeof(*tz->trips), GFP_KERNEL); |
| 712 | if (!tz->trips) { |
| 713 | ret = -ENOMEM; |
| 714 | goto free_tz; |
| 715 | } |
| 716 | |
| 717 | i = 0; |
| 718 | for_each_child_of_node(child, gchild) { |
| 719 | ret = thermal_of_populate_trip(gchild, &tz->trips[i++]); |
| 720 | if (ret) |
| 721 | goto free_trips; |
| 722 | } |
| 723 | |
| 724 | of_node_put(child); |
| 725 | |
| 726 | /* cooling-maps */ |
| 727 | child = of_get_child_by_name(np, "cooling-maps"); |
| 728 | |
| 729 | /* cooling-maps not provided */ |
| 730 | if (!child) |
| 731 | goto finish; |
| 732 | |
| 733 | tz->num_tbps = of_get_child_count(child); |
| 734 | if (tz->num_tbps == 0) |
| 735 | goto finish; |
| 736 | |
| 737 | tz->tbps = kzalloc(tz->num_tbps * sizeof(*tz->tbps), GFP_KERNEL); |
| 738 | if (!tz->tbps) { |
| 739 | ret = -ENOMEM; |
| 740 | goto free_trips; |
| 741 | } |
| 742 | |
| 743 | i = 0; |
Stephen Boyd | ca9521b | 2014-06-18 16:32:08 -0700 | [diff] [blame] | 744 | for_each_child_of_node(child, gchild) { |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 745 | ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++], |
| 746 | tz->trips, tz->ntrips); |
| 747 | if (ret) |
| 748 | goto free_tbps; |
Stephen Boyd | ca9521b | 2014-06-18 16:32:08 -0700 | [diff] [blame] | 749 | } |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 750 | |
| 751 | finish: |
| 752 | of_node_put(child); |
| 753 | tz->mode = THERMAL_DEVICE_DISABLED; |
| 754 | |
| 755 | return tz; |
| 756 | |
| 757 | free_tbps: |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 758 | for (i = 0; i < tz->num_tbps; i++) |
| 759 | of_node_put(tz->tbps[i].cooling_device); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 760 | kfree(tz->tbps); |
| 761 | free_trips: |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 762 | for (i = 0; i < tz->ntrips; i++) |
| 763 | of_node_put(tz->trips[i].np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 764 | kfree(tz->trips); |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 765 | of_node_put(gchild); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 766 | free_tz: |
| 767 | kfree(tz); |
| 768 | of_node_put(child); |
| 769 | |
| 770 | return ERR_PTR(ret); |
| 771 | } |
| 772 | |
| 773 | static inline void of_thermal_free_zone(struct __thermal_zone *tz) |
| 774 | { |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 775 | int i; |
| 776 | |
| 777 | for (i = 0; i < tz->num_tbps; i++) |
| 778 | of_node_put(tz->tbps[i].cooling_device); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 779 | kfree(tz->tbps); |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 780 | for (i = 0; i < tz->ntrips; i++) |
| 781 | of_node_put(tz->trips[i].np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 782 | kfree(tz->trips); |
| 783 | kfree(tz); |
| 784 | } |
| 785 | |
| 786 | /** |
| 787 | * of_parse_thermal_zones - parse device tree thermal data |
| 788 | * |
| 789 | * Initialization function that can be called by machine initialization |
| 790 | * code to parse thermal data and populate the thermal framework |
| 791 | * with hardware thermal zones info. This function only parses thermal zones. |
| 792 | * Cooling devices and sensor devices nodes are supposed to be parsed |
| 793 | * by their respective drivers. |
| 794 | * |
| 795 | * Return: 0 on success, proper error code otherwise |
| 796 | * |
| 797 | */ |
| 798 | int __init of_parse_thermal_zones(void) |
| 799 | { |
| 800 | struct device_node *np, *child; |
| 801 | struct __thermal_zone *tz; |
| 802 | struct thermal_zone_device_ops *ops; |
| 803 | |
| 804 | np = of_find_node_by_name(NULL, "thermal-zones"); |
| 805 | if (!np) { |
| 806 | pr_debug("unable to find thermal zones\n"); |
| 807 | return 0; /* Run successfully on systems without thermal DT */ |
| 808 | } |
| 809 | |
| 810 | for_each_child_of_node(np, child) { |
| 811 | struct thermal_zone_device *zone; |
| 812 | struct thermal_zone_params *tzp; |
| 813 | |
Laxman Dewangan | a020279 | 2014-07-25 15:31:58 +0530 | [diff] [blame] | 814 | /* Check whether child is enabled or not */ |
| 815 | if (!of_device_is_available(child)) |
| 816 | continue; |
| 817 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 818 | tz = thermal_of_build_thermal_zone(child); |
| 819 | if (IS_ERR(tz)) { |
| 820 | pr_err("failed to build thermal zone %s: %ld\n", |
| 821 | child->name, |
| 822 | PTR_ERR(tz)); |
| 823 | continue; |
| 824 | } |
| 825 | |
| 826 | ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL); |
| 827 | if (!ops) |
| 828 | goto exit_free; |
| 829 | |
| 830 | tzp = kzalloc(sizeof(*tzp), GFP_KERNEL); |
| 831 | if (!tzp) { |
| 832 | kfree(ops); |
| 833 | goto exit_free; |
| 834 | } |
| 835 | |
| 836 | /* No hwmon because there might be hwmon drivers registering */ |
| 837 | tzp->no_hwmon = true; |
| 838 | |
| 839 | zone = thermal_zone_device_register(child->name, tz->ntrips, |
| 840 | 0, tz, |
| 841 | ops, tzp, |
| 842 | tz->passive_delay, |
| 843 | tz->polling_delay); |
| 844 | if (IS_ERR(zone)) { |
| 845 | pr_err("Failed to build %s zone %ld\n", child->name, |
| 846 | PTR_ERR(zone)); |
| 847 | kfree(tzp); |
| 848 | kfree(ops); |
| 849 | of_thermal_free_zone(tz); |
| 850 | /* attempting to build remaining zones still */ |
| 851 | } |
| 852 | } |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 853 | of_node_put(np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 854 | |
| 855 | return 0; |
| 856 | |
| 857 | exit_free: |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 858 | of_node_put(child); |
| 859 | of_node_put(np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 860 | of_thermal_free_zone(tz); |
| 861 | |
| 862 | /* no memory available, so free what we have built */ |
| 863 | of_thermal_destroy_zones(); |
| 864 | |
| 865 | return -ENOMEM; |
| 866 | } |
| 867 | |
| 868 | /** |
| 869 | * of_thermal_destroy_zones - remove all zones parsed and allocated resources |
| 870 | * |
| 871 | * Finds all zones parsed and added to the thermal framework and remove them |
| 872 | * from the system, together with their resources. |
| 873 | * |
| 874 | */ |
| 875 | void of_thermal_destroy_zones(void) |
| 876 | { |
| 877 | struct device_node *np, *child; |
| 878 | |
| 879 | np = of_find_node_by_name(NULL, "thermal-zones"); |
| 880 | if (!np) { |
| 881 | pr_err("unable to find thermal zones\n"); |
| 882 | return; |
| 883 | } |
| 884 | |
| 885 | for_each_child_of_node(np, child) { |
| 886 | struct thermal_zone_device *zone; |
| 887 | |
Laxman Dewangan | a020279 | 2014-07-25 15:31:58 +0530 | [diff] [blame] | 888 | /* Check whether child is enabled or not */ |
| 889 | if (!of_device_is_available(child)) |
| 890 | continue; |
| 891 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 892 | zone = thermal_zone_get_zone_by_name(child->name); |
| 893 | if (IS_ERR(zone)) |
| 894 | continue; |
| 895 | |
| 896 | thermal_zone_device_unregister(zone); |
| 897 | kfree(zone->tzp); |
| 898 | kfree(zone->ops); |
| 899 | of_thermal_free_zone(zone->devdata); |
| 900 | } |
Vladimir Zapolskiy | c2aad93c | 2014-09-29 02:47:46 +0300 | [diff] [blame] | 901 | of_node_put(np); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 902 | } |