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