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