1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * of-thermal.c - Generic Thermal Management device tree support. 4 * 5 * Copyright (C) 2013 Texas Instruments 6 * Copyright (C) 2013 Eduardo Valentin <eduardo.valentin@ti.com> 7 */ 8 9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 11 #include <linux/err.h> 12 #include <linux/export.h> 13 #include <linux/of.h> 14 #include <linux/slab.h> 15 #include <linux/thermal.h> 16 #include <linux/types.h> 17 #include <linux/string.h> 18 19 #include "thermal_core.h" 20 21 /*** functions parsing device tree nodes ***/ 22 23 static int of_find_trip_id(struct device_node *np, struct device_node *trip) 24 { 25 struct device_node *trips; 26 struct device_node *t; 27 int i = 0; 28 29 trips = of_get_child_by_name(np, "trips"); 30 if (!trips) { 31 pr_err("Failed to find 'trips' node\n"); 32 return -EINVAL; 33 } 34 35 /* 36 * Find the trip id point associated with the cooling device map 37 */ 38 for_each_child_of_node(trips, t) { 39 40 if (t == trip) { 41 of_node_put(t); 42 goto out; 43 } 44 i++; 45 } 46 47 i = -ENXIO; 48 out: 49 of_node_put(trips); 50 51 return i; 52 } 53 54 /* 55 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h 56 * into the device tree binding of 'trip', property type. 57 */ 58 static const char * const trip_types[] = { 59 [THERMAL_TRIP_ACTIVE] = "active", 60 [THERMAL_TRIP_PASSIVE] = "passive", 61 [THERMAL_TRIP_HOT] = "hot", 62 [THERMAL_TRIP_CRITICAL] = "critical", 63 }; 64 65 /** 66 * thermal_of_get_trip_type - Get phy mode for given device_node 67 * @np: Pointer to the given device_node 68 * @type: Pointer to resulting trip type 69 * 70 * The function gets trip type string from property 'type', 71 * and store its index in trip_types table in @type, 72 * 73 * Return: 0 on success, or errno in error case. 74 */ 75 static int thermal_of_get_trip_type(struct device_node *np, 76 enum thermal_trip_type *type) 77 { 78 const char *t; 79 int err, i; 80 81 err = of_property_read_string(np, "type", &t); 82 if (err < 0) 83 return err; 84 85 for (i = 0; i < ARRAY_SIZE(trip_types); i++) 86 if (!strcasecmp(t, trip_types[i])) { 87 *type = i; 88 return 0; 89 } 90 91 return -ENODEV; 92 } 93 94 static int thermal_of_populate_trip(struct device_node *np, 95 struct thermal_trip *trip) 96 { 97 int prop; 98 int ret; 99 100 ret = of_property_read_u32(np, "temperature", &prop); 101 if (ret < 0) { 102 pr_err("missing temperature property\n"); 103 return ret; 104 } 105 trip->temperature = prop; 106 107 ret = of_property_read_u32(np, "hysteresis", &prop); 108 if (ret < 0) { 109 pr_err("missing hysteresis property\n"); 110 return ret; 111 } 112 trip->hysteresis = prop; 113 114 ret = thermal_of_get_trip_type(np, &trip->type); 115 if (ret < 0) { 116 pr_err("wrong trip type property\n"); 117 return ret; 118 } 119 120 trip->flags = THERMAL_TRIP_FLAG_RW_TEMP; 121 122 return 0; 123 } 124 125 static struct thermal_trip *thermal_of_trips_init(struct device_node *np, int *ntrips) 126 { 127 struct thermal_trip *tt; 128 struct device_node *trips; 129 int ret, count; 130 131 trips = of_get_child_by_name(np, "trips"); 132 if (!trips) { 133 pr_err("Failed to find 'trips' node\n"); 134 return ERR_PTR(-EINVAL); 135 } 136 137 count = of_get_child_count(trips); 138 if (!count) { 139 pr_err("No trip point defined\n"); 140 ret = -EINVAL; 141 goto out_of_node_put; 142 } 143 144 tt = kzalloc(sizeof(*tt) * count, GFP_KERNEL); 145 if (!tt) { 146 ret = -ENOMEM; 147 goto out_of_node_put; 148 } 149 150 *ntrips = count; 151 152 count = 0; 153 for_each_child_of_node_scoped(trips, trip) { 154 ret = thermal_of_populate_trip(trip, &tt[count++]); 155 if (ret) 156 goto out_kfree; 157 } 158 159 of_node_put(trips); 160 161 return tt; 162 163 out_kfree: 164 kfree(tt); 165 *ntrips = 0; 166 out_of_node_put: 167 of_node_put(trips); 168 169 return ERR_PTR(ret); 170 } 171 172 static struct device_node *of_thermal_zone_find(struct device_node *sensor, int id) 173 { 174 struct device_node *np, *tz; 175 struct of_phandle_args sensor_specs; 176 177 np = of_find_node_by_name(NULL, "thermal-zones"); 178 if (!np) { 179 pr_debug("No thermal zones description\n"); 180 return ERR_PTR(-ENODEV); 181 } 182 183 /* 184 * Search for each thermal zone, a defined sensor 185 * corresponding to the one passed as parameter 186 */ 187 for_each_available_child_of_node_scoped(np, child) { 188 189 int count, i; 190 191 count = of_count_phandle_with_args(child, "thermal-sensors", 192 "#thermal-sensor-cells"); 193 if (count <= 0) { 194 pr_err("%pOFn: missing thermal sensor\n", child); 195 tz = ERR_PTR(-EINVAL); 196 goto out; 197 } 198 199 for (i = 0; i < count; i++) { 200 201 int ret; 202 203 ret = of_parse_phandle_with_args(child, "thermal-sensors", 204 "#thermal-sensor-cells", 205 i, &sensor_specs); 206 if (ret < 0) { 207 pr_err("%pOFn: Failed to read thermal-sensors cells: %d\n", child, ret); 208 tz = ERR_PTR(ret); 209 goto out; 210 } 211 212 if ((sensor == sensor_specs.np) && id == (sensor_specs.args_count ? 213 sensor_specs.args[0] : 0)) { 214 pr_debug("sensor %pOFn id=%d belongs to %pOFn\n", sensor, id, child); 215 tz = no_free_ptr(child); 216 goto out; 217 } 218 } 219 } 220 tz = ERR_PTR(-ENODEV); 221 out: 222 of_node_put(np); 223 return tz; 224 } 225 226 static int thermal_of_monitor_init(struct device_node *np, int *delay, int *pdelay) 227 { 228 int ret; 229 230 ret = of_property_read_u32(np, "polling-delay-passive", pdelay); 231 if (ret == -EINVAL) { 232 *pdelay = 0; 233 } else if (ret < 0) { 234 pr_err("%pOFn: Couldn't get polling-delay-passive: %d\n", np, ret); 235 return ret; 236 } 237 238 ret = of_property_read_u32(np, "polling-delay", delay); 239 if (ret == -EINVAL) { 240 *delay = 0; 241 } else if (ret < 0) { 242 pr_err("%pOFn: Couldn't get polling-delay: %d\n", np, ret); 243 return ret; 244 } 245 246 return 0; 247 } 248 249 static void thermal_of_parameters_init(struct device_node *np, 250 struct thermal_zone_params *tzp) 251 { 252 int coef[2]; 253 int ncoef = ARRAY_SIZE(coef); 254 int prop, ret; 255 256 tzp->no_hwmon = true; 257 258 if (!of_property_read_u32(np, "sustainable-power", &prop)) 259 tzp->sustainable_power = prop; 260 261 /* 262 * For now, the thermal framework supports only one sensor per 263 * thermal zone. Thus, we are considering only the first two 264 * values as slope and offset. 265 */ 266 ret = of_property_read_u32_array(np, "coefficients", coef, ncoef); 267 if (ret) { 268 coef[0] = 1; 269 coef[1] = 0; 270 } 271 272 tzp->slope = coef[0]; 273 tzp->offset = coef[1]; 274 } 275 276 static struct device_node *thermal_of_zone_get_by_name(struct thermal_zone_device *tz) 277 { 278 struct device_node *np, *tz_np; 279 280 np = of_find_node_by_name(NULL, "thermal-zones"); 281 if (!np) 282 return ERR_PTR(-ENODEV); 283 284 tz_np = of_get_child_by_name(np, tz->type); 285 286 of_node_put(np); 287 288 if (!tz_np) 289 return ERR_PTR(-ENODEV); 290 291 return tz_np; 292 } 293 294 static int __thermal_of_unbind(struct device_node *map_np, int index, int trip_id, 295 struct thermal_zone_device *tz, struct thermal_cooling_device *cdev) 296 { 297 struct of_phandle_args cooling_spec; 298 int ret; 299 300 ret = of_parse_phandle_with_args(map_np, "cooling-device", "#cooling-cells", 301 index, &cooling_spec); 302 303 if (ret < 0) { 304 pr_err("Invalid cooling-device entry\n"); 305 return ret; 306 } 307 308 of_node_put(cooling_spec.np); 309 310 if (cooling_spec.args_count < 2) { 311 pr_err("wrong reference to cooling device, missing limits\n"); 312 return -EINVAL; 313 } 314 315 if (cooling_spec.np != cdev->np) 316 return 0; 317 318 ret = thermal_zone_unbind_cooling_device(tz, trip_id, cdev); 319 if (ret) 320 pr_err("Failed to unbind '%s' with '%s': %d\n", tz->type, cdev->type, ret); 321 322 return ret; 323 } 324 325 static int __thermal_of_bind(struct device_node *map_np, int index, int trip_id, 326 struct thermal_zone_device *tz, struct thermal_cooling_device *cdev) 327 { 328 struct of_phandle_args cooling_spec; 329 int ret, weight = THERMAL_WEIGHT_DEFAULT; 330 331 of_property_read_u32(map_np, "contribution", &weight); 332 333 ret = of_parse_phandle_with_args(map_np, "cooling-device", "#cooling-cells", 334 index, &cooling_spec); 335 336 if (ret < 0) { 337 pr_err("Invalid cooling-device entry\n"); 338 return ret; 339 } 340 341 of_node_put(cooling_spec.np); 342 343 if (cooling_spec.args_count < 2) { 344 pr_err("wrong reference to cooling device, missing limits\n"); 345 return -EINVAL; 346 } 347 348 if (cooling_spec.np != cdev->np) 349 return 0; 350 351 ret = thermal_zone_bind_cooling_device(tz, trip_id, cdev, cooling_spec.args[1], 352 cooling_spec.args[0], 353 weight); 354 if (ret) 355 pr_err("Failed to bind '%s' with '%s': %d\n", tz->type, cdev->type, ret); 356 357 return ret; 358 } 359 360 static int thermal_of_for_each_cooling_device(struct device_node *tz_np, struct device_node *map_np, 361 struct thermal_zone_device *tz, struct thermal_cooling_device *cdev, 362 int (*action)(struct device_node *, int, int, 363 struct thermal_zone_device *, struct thermal_cooling_device *)) 364 { 365 struct device_node *tr_np; 366 int count, i, trip_id; 367 368 tr_np = of_parse_phandle(map_np, "trip", 0); 369 if (!tr_np) 370 return -ENODEV; 371 372 trip_id = of_find_trip_id(tz_np, tr_np); 373 if (trip_id < 0) 374 return trip_id; 375 376 count = of_count_phandle_with_args(map_np, "cooling-device", "#cooling-cells"); 377 if (count <= 0) { 378 pr_err("Add a cooling_device property with at least one device\n"); 379 return -ENOENT; 380 } 381 382 /* 383 * At this point, we don't want to bail out when there is an 384 * error, we will try to bind/unbind as many as possible 385 * cooling devices 386 */ 387 for (i = 0; i < count; i++) 388 action(map_np, i, trip_id, tz, cdev); 389 390 return 0; 391 } 392 393 static int thermal_of_for_each_cooling_maps(struct thermal_zone_device *tz, 394 struct thermal_cooling_device *cdev, 395 int (*action)(struct device_node *, int, int, 396 struct thermal_zone_device *, struct thermal_cooling_device *)) 397 { 398 struct device_node *tz_np, *cm_np, *child; 399 int ret = 0; 400 401 tz_np = thermal_of_zone_get_by_name(tz); 402 if (IS_ERR(tz_np)) { 403 pr_err("Failed to get node tz by name\n"); 404 return PTR_ERR(tz_np); 405 } 406 407 cm_np = of_get_child_by_name(tz_np, "cooling-maps"); 408 if (!cm_np) 409 goto out; 410 411 for_each_child_of_node(cm_np, child) { 412 ret = thermal_of_for_each_cooling_device(tz_np, child, tz, cdev, action); 413 if (ret) { 414 of_node_put(child); 415 break; 416 } 417 } 418 419 of_node_put(cm_np); 420 out: 421 of_node_put(tz_np); 422 423 return ret; 424 } 425 426 static int thermal_of_bind(struct thermal_zone_device *tz, 427 struct thermal_cooling_device *cdev) 428 { 429 return thermal_of_for_each_cooling_maps(tz, cdev, __thermal_of_bind); 430 } 431 432 static int thermal_of_unbind(struct thermal_zone_device *tz, 433 struct thermal_cooling_device *cdev) 434 { 435 return thermal_of_for_each_cooling_maps(tz, cdev, __thermal_of_unbind); 436 } 437 438 /** 439 * thermal_of_zone_unregister - Cleanup the specific allocated ressources 440 * 441 * This function disables the thermal zone and frees the different 442 * ressources allocated specific to the thermal OF. 443 * 444 * @tz: a pointer to the thermal zone structure 445 */ 446 static void thermal_of_zone_unregister(struct thermal_zone_device *tz) 447 { 448 thermal_zone_device_disable(tz); 449 thermal_zone_device_unregister(tz); 450 } 451 452 /** 453 * thermal_of_zone_register - Register a thermal zone with device node 454 * sensor 455 * 456 * The thermal_of_zone_register() parses a device tree given a device 457 * node sensor and identifier. It searches for the thermal zone 458 * associated to the couple sensor/id and retrieves all the thermal 459 * zone properties and registers new thermal zone with those 460 * properties. 461 * 462 * @sensor: A device node pointer corresponding to the sensor in the device tree 463 * @id: An integer as sensor identifier 464 * @data: A private data to be stored in the thermal zone dedicated private area 465 * @ops: A set of thermal sensor ops 466 * 467 * Return: a valid thermal zone structure pointer on success. 468 * - EINVAL: if the device tree thermal description is malformed 469 * - ENOMEM: if one structure can not be allocated 470 * - Other negative errors are returned by the underlying called functions 471 */ 472 static struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor, int id, void *data, 473 const struct thermal_zone_device_ops *ops) 474 { 475 struct thermal_zone_device_ops of_ops = *ops; 476 struct thermal_zone_device *tz; 477 struct thermal_trip *trips; 478 struct thermal_zone_params tzp = {}; 479 struct device_node *np; 480 const char *action; 481 int delay, pdelay; 482 int ntrips; 483 int ret; 484 485 np = of_thermal_zone_find(sensor, id); 486 if (IS_ERR(np)) { 487 if (PTR_ERR(np) != -ENODEV) 488 pr_err("Failed to find thermal zone for %pOFn id=%d\n", sensor, id); 489 return ERR_CAST(np); 490 } 491 492 trips = thermal_of_trips_init(np, &ntrips); 493 if (IS_ERR(trips)) { 494 pr_err("Failed to find trip points for %pOFn id=%d\n", sensor, id); 495 ret = PTR_ERR(trips); 496 goto out_of_node_put; 497 } 498 499 ret = thermal_of_monitor_init(np, &delay, &pdelay); 500 if (ret) { 501 pr_err("Failed to initialize monitoring delays from %pOFn\n", np); 502 goto out_kfree_trips; 503 } 504 505 thermal_of_parameters_init(np, &tzp); 506 507 of_ops.bind = thermal_of_bind; 508 of_ops.unbind = thermal_of_unbind; 509 510 ret = of_property_read_string(np, "critical-action", &action); 511 if (!ret) 512 if (!of_ops.critical && !strcasecmp(action, "reboot")) 513 of_ops.critical = thermal_zone_device_critical_reboot; 514 515 tz = thermal_zone_device_register_with_trips(np->name, trips, ntrips, 516 data, &of_ops, &tzp, 517 pdelay, delay); 518 if (IS_ERR(tz)) { 519 ret = PTR_ERR(tz); 520 pr_err("Failed to register thermal zone %pOFn: %d\n", np, ret); 521 goto out_kfree_trips; 522 } 523 524 of_node_put(np); 525 kfree(trips); 526 527 ret = thermal_zone_device_enable(tz); 528 if (ret) { 529 pr_err("Failed to enabled thermal zone '%s', id=%d: %d\n", 530 tz->type, tz->id, ret); 531 thermal_of_zone_unregister(tz); 532 return ERR_PTR(ret); 533 } 534 535 return tz; 536 537 out_kfree_trips: 538 kfree(trips); 539 out_of_node_put: 540 of_node_put(np); 541 542 return ERR_PTR(ret); 543 } 544 545 static void devm_thermal_of_zone_release(struct device *dev, void *res) 546 { 547 thermal_of_zone_unregister(*(struct thermal_zone_device **)res); 548 } 549 550 static int devm_thermal_of_zone_match(struct device *dev, void *res, 551 void *data) 552 { 553 struct thermal_zone_device **r = res; 554 555 if (WARN_ON(!r || !*r)) 556 return 0; 557 558 return *r == data; 559 } 560 561 /** 562 * devm_thermal_of_zone_register - register a thermal tied with the sensor life cycle 563 * 564 * This function is the device version of the thermal_of_zone_register() function. 565 * 566 * @dev: a device structure pointer to sensor to be tied with the thermal zone OF life cycle 567 * @sensor_id: the sensor identifier 568 * @data: a pointer to a private data to be stored in the thermal zone 'devdata' field 569 * @ops: a pointer to the ops structure associated with the sensor 570 */ 571 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int sensor_id, void *data, 572 const struct thermal_zone_device_ops *ops) 573 { 574 struct thermal_zone_device **ptr, *tzd; 575 576 ptr = devres_alloc(devm_thermal_of_zone_release, sizeof(*ptr), 577 GFP_KERNEL); 578 if (!ptr) 579 return ERR_PTR(-ENOMEM); 580 581 tzd = thermal_of_zone_register(dev->of_node, sensor_id, data, ops); 582 if (IS_ERR(tzd)) { 583 devres_free(ptr); 584 return tzd; 585 } 586 587 *ptr = tzd; 588 devres_add(dev, ptr); 589 590 return tzd; 591 } 592 EXPORT_SYMBOL_GPL(devm_thermal_of_zone_register); 593 594 /** 595 * devm_thermal_of_zone_unregister - Resource managed version of 596 * thermal_of_zone_unregister(). 597 * @dev: Device for which which resource was allocated. 598 * @tz: a pointer to struct thermal_zone where the sensor is registered. 599 * 600 * This function removes the sensor callbacks and private data from the 601 * thermal zone device registered with devm_thermal_zone_of_sensor_register() 602 * API. It will also silent the zone by remove the .get_temp() and .get_trend() 603 * thermal zone device callbacks. 604 * Normally this function will not need to be called and the resource 605 * management code will ensure that the resource is freed. 606 */ 607 void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz) 608 { 609 WARN_ON(devres_release(dev, devm_thermal_of_zone_release, 610 devm_thermal_of_zone_match, tz)); 611 } 612 EXPORT_SYMBOL_GPL(devm_thermal_of_zone_unregister); 613