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_device.h> 14 #include <linux/of_platform.h> 15 #include <linux/slab.h> 16 #include <linux/thermal.h> 17 #include <linux/types.h> 18 #include <linux/string.h> 19 20 #include "thermal_core.h" 21 22 /*** Private data structures to represent thermal device tree data ***/ 23 24 /** 25 * struct __thermal_cooling_bind_param - a cooling device for a trip point 26 * @cooling_device: a pointer to identify the referred cooling device 27 * @min: minimum cooling state used at this trip point 28 * @max: maximum cooling state used at this trip point 29 */ 30 31 struct __thermal_cooling_bind_param { 32 struct device_node *cooling_device; 33 unsigned long min; 34 unsigned long max; 35 }; 36 37 /** 38 * struct __thermal_bind_params - a match between trip and cooling device 39 * @tcbp: a pointer to an array of cooling devices 40 * @count: number of elements in array 41 * @trip_id: the trip point index 42 * @usage: the percentage (from 0 to 100) of cooling contribution 43 */ 44 45 struct __thermal_bind_params { 46 struct __thermal_cooling_bind_param *tcbp; 47 unsigned int count; 48 unsigned int trip_id; 49 unsigned int usage; 50 }; 51 52 /** 53 * struct __thermal_zone - internal representation of a thermal zone 54 * @passive_delay: polling interval while passive cooling is activated 55 * @polling_delay: zone polling interval 56 * @slope: slope of the temperature adjustment curve 57 * @offset: offset of the temperature adjustment curve 58 * @ntrips: number of trip points 59 * @trips: an array of trip points (0..ntrips - 1) 60 * @num_tbps: number of thermal bind params 61 * @tbps: an array of thermal bind params (0..num_tbps - 1) 62 * @sensor_data: sensor private data used while reading temperature and trend 63 * @ops: set of callbacks to handle the thermal zone based on DT 64 */ 65 66 struct __thermal_zone { 67 int passive_delay; 68 int polling_delay; 69 int slope; 70 int offset; 71 72 /* trip data */ 73 int ntrips; 74 struct thermal_trip *trips; 75 76 /* cooling binding data */ 77 int num_tbps; 78 struct __thermal_bind_params *tbps; 79 80 /* sensor interface */ 81 void *sensor_data; 82 const struct thermal_zone_of_device_ops *ops; 83 }; 84 85 /*** DT thermal zone device callbacks ***/ 86 87 static int of_thermal_get_temp(struct thermal_zone_device *tz, 88 int *temp) 89 { 90 struct __thermal_zone *data = tz->devdata; 91 92 if (!data->ops || !data->ops->get_temp) 93 return -EINVAL; 94 95 return data->ops->get_temp(data->sensor_data, temp); 96 } 97 98 static int of_thermal_set_trips(struct thermal_zone_device *tz, 99 int low, int high) 100 { 101 struct __thermal_zone *data = tz->devdata; 102 103 if (!data->ops || !data->ops->set_trips) 104 return -EINVAL; 105 106 return data->ops->set_trips(data->sensor_data, low, high); 107 } 108 109 /** 110 * of_thermal_get_ntrips - function to export number of available trip 111 * points. 112 * @tz: pointer to a thermal zone 113 * 114 * This function is a globally visible wrapper to get number of trip points 115 * stored in the local struct __thermal_zone 116 * 117 * Return: number of available trip points, -ENODEV when data not available 118 */ 119 int of_thermal_get_ntrips(struct thermal_zone_device *tz) 120 { 121 return tz->num_trips; 122 } 123 EXPORT_SYMBOL_GPL(of_thermal_get_ntrips); 124 125 /** 126 * of_thermal_is_trip_valid - function to check if trip point is valid 127 * 128 * @tz: pointer to a thermal zone 129 * @trip: trip point to evaluate 130 * 131 * This function is responsible for checking if passed trip point is valid 132 * 133 * Return: true if trip point is valid, false otherwise 134 */ 135 bool of_thermal_is_trip_valid(struct thermal_zone_device *tz, int trip) 136 { 137 if (trip >= tz->num_trips || trip < 0) 138 return false; 139 140 return true; 141 } 142 EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid); 143 144 /** 145 * of_thermal_get_trip_points - function to get access to a globally exported 146 * trip points 147 * 148 * @tz: pointer to a thermal zone 149 * 150 * This function provides a pointer to trip points table 151 * 152 * Return: pointer to trip points table, NULL otherwise 153 */ 154 const struct thermal_trip * 155 of_thermal_get_trip_points(struct thermal_zone_device *tz) 156 { 157 return tz->trips; 158 } 159 EXPORT_SYMBOL_GPL(of_thermal_get_trip_points); 160 161 /** 162 * of_thermal_set_emul_temp - function to set emulated temperature 163 * 164 * @tz: pointer to a thermal zone 165 * @temp: temperature to set 166 * 167 * This function gives the ability to set emulated value of temperature, 168 * which is handy for debugging 169 * 170 * Return: zero on success, error code otherwise 171 */ 172 static int of_thermal_set_emul_temp(struct thermal_zone_device *tz, 173 int temp) 174 { 175 struct __thermal_zone *data = tz->devdata; 176 177 if (!data->ops || !data->ops->set_emul_temp) 178 return -EINVAL; 179 180 return data->ops->set_emul_temp(data->sensor_data, temp); 181 } 182 183 static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip, 184 enum thermal_trend *trend) 185 { 186 struct __thermal_zone *data = tz->devdata; 187 188 if (!data->ops || !data->ops->get_trend) 189 return -EINVAL; 190 191 return data->ops->get_trend(data->sensor_data, trip, trend); 192 } 193 194 static int of_thermal_change_mode(struct thermal_zone_device *tz, 195 enum thermal_device_mode mode) 196 { 197 struct __thermal_zone *data = tz->devdata; 198 199 return data->ops->change_mode(data->sensor_data, mode); 200 } 201 202 static int of_thermal_bind(struct thermal_zone_device *thermal, 203 struct thermal_cooling_device *cdev) 204 { 205 struct __thermal_zone *data = thermal->devdata; 206 struct __thermal_bind_params *tbp; 207 struct __thermal_cooling_bind_param *tcbp; 208 int i, j; 209 210 if (!data || IS_ERR(data)) 211 return -ENODEV; 212 213 /* find where to bind */ 214 for (i = 0; i < data->num_tbps; i++) { 215 tbp = data->tbps + i; 216 217 for (j = 0; j < tbp->count; j++) { 218 tcbp = tbp->tcbp + j; 219 220 if (tcbp->cooling_device == cdev->np) { 221 int ret; 222 223 ret = thermal_zone_bind_cooling_device(thermal, 224 tbp->trip_id, cdev, 225 tcbp->max, 226 tcbp->min, 227 tbp->usage); 228 if (ret) 229 return ret; 230 } 231 } 232 } 233 234 return 0; 235 } 236 237 static int of_thermal_unbind(struct thermal_zone_device *thermal, 238 struct thermal_cooling_device *cdev) 239 { 240 struct __thermal_zone *data = thermal->devdata; 241 struct __thermal_bind_params *tbp; 242 struct __thermal_cooling_bind_param *tcbp; 243 int i, j; 244 245 if (!data || IS_ERR(data)) 246 return -ENODEV; 247 248 /* find where to unbind */ 249 for (i = 0; i < data->num_tbps; i++) { 250 tbp = data->tbps + i; 251 252 for (j = 0; j < tbp->count; j++) { 253 tcbp = tbp->tcbp + j; 254 255 if (tcbp->cooling_device == cdev->np) { 256 int ret; 257 258 ret = thermal_zone_unbind_cooling_device(thermal, 259 tbp->trip_id, cdev); 260 if (ret) 261 return ret; 262 } 263 } 264 } 265 266 return 0; 267 } 268 269 static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip, 270 enum thermal_trip_type *type) 271 { 272 if (trip >= tz->num_trips || trip < 0) 273 return -EDOM; 274 275 *type = tz->trips[trip].type; 276 277 return 0; 278 } 279 280 static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip, 281 int *temp) 282 { 283 if (trip >= tz->num_trips || trip < 0) 284 return -EDOM; 285 286 *temp = tz->trips[trip].temperature; 287 288 return 0; 289 } 290 291 static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip, 292 int temp) 293 { 294 struct __thermal_zone *data = tz->devdata; 295 296 if (trip >= tz->num_trips || trip < 0) 297 return -EDOM; 298 299 if (data->ops && data->ops->set_trip_temp) { 300 int ret; 301 302 ret = data->ops->set_trip_temp(data->sensor_data, trip, temp); 303 if (ret) 304 return ret; 305 } 306 307 /* thermal framework should take care of data->mask & (1 << trip) */ 308 tz->trips[trip].temperature = temp; 309 310 return 0; 311 } 312 313 static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip, 314 int *hyst) 315 { 316 if (trip >= tz->num_trips || trip < 0) 317 return -EDOM; 318 319 *hyst = tz->trips[trip].hysteresis; 320 321 return 0; 322 } 323 324 static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip, 325 int hyst) 326 { 327 if (trip >= tz->num_trips || trip < 0) 328 return -EDOM; 329 330 /* thermal framework should take care of data->mask & (1 << trip) */ 331 tz->trips[trip].hysteresis = hyst; 332 333 return 0; 334 } 335 336 static int of_thermal_get_crit_temp(struct thermal_zone_device *tz, 337 int *temp) 338 { 339 int i; 340 341 for (i = 0; i < tz->num_trips; i++) 342 if (tz->trips[i].type == THERMAL_TRIP_CRITICAL) { 343 *temp = tz->trips[i].temperature; 344 return 0; 345 } 346 347 return -EINVAL; 348 } 349 350 static struct thermal_zone_device_ops of_thermal_ops = { 351 .get_trip_type = of_thermal_get_trip_type, 352 .get_trip_temp = of_thermal_get_trip_temp, 353 .set_trip_temp = of_thermal_set_trip_temp, 354 .get_trip_hyst = of_thermal_get_trip_hyst, 355 .set_trip_hyst = of_thermal_set_trip_hyst, 356 .get_crit_temp = of_thermal_get_crit_temp, 357 358 .bind = of_thermal_bind, 359 .unbind = of_thermal_unbind, 360 }; 361 362 /*** sensor API ***/ 363 364 static struct thermal_zone_device * 365 thermal_zone_of_add_sensor(struct device_node *zone, 366 struct device_node *sensor, void *data, 367 const struct thermal_zone_of_device_ops *ops) 368 { 369 struct thermal_zone_device *tzd; 370 struct __thermal_zone *tz; 371 372 tzd = thermal_zone_get_zone_by_name(zone->name); 373 if (IS_ERR(tzd)) 374 return ERR_PTR(-EPROBE_DEFER); 375 376 tz = tzd->devdata; 377 378 if (!ops) 379 return ERR_PTR(-EINVAL); 380 381 mutex_lock(&tzd->lock); 382 tz->ops = ops; 383 tz->sensor_data = data; 384 385 tzd->ops->get_temp = of_thermal_get_temp; 386 tzd->ops->get_trend = of_thermal_get_trend; 387 388 /* 389 * The thermal zone core will calculate the window if they have set the 390 * optional set_trips pointer. 391 */ 392 if (ops->set_trips) 393 tzd->ops->set_trips = of_thermal_set_trips; 394 395 if (ops->set_emul_temp) 396 tzd->ops->set_emul_temp = of_thermal_set_emul_temp; 397 398 if (ops->change_mode) 399 tzd->ops->change_mode = of_thermal_change_mode; 400 401 mutex_unlock(&tzd->lock); 402 403 return tzd; 404 } 405 406 /** 407 * thermal_zone_of_get_sensor_id - get sensor ID from a DT thermal zone 408 * @tz_np: a valid thermal zone device node. 409 * @sensor_np: a sensor node of a valid sensor device. 410 * @id: the sensor ID returned if success. 411 * 412 * This function will get sensor ID from a given thermal zone node and 413 * the sensor node must match the temperature provider @sensor_np. 414 * 415 * Return: 0 on success, proper error code otherwise. 416 */ 417 418 int thermal_zone_of_get_sensor_id(struct device_node *tz_np, 419 struct device_node *sensor_np, 420 u32 *id) 421 { 422 struct of_phandle_args sensor_specs; 423 int ret; 424 425 ret = of_parse_phandle_with_args(tz_np, 426 "thermal-sensors", 427 "#thermal-sensor-cells", 428 0, 429 &sensor_specs); 430 if (ret) 431 return ret; 432 433 if (sensor_specs.np != sensor_np) { 434 of_node_put(sensor_specs.np); 435 return -ENODEV; 436 } 437 438 if (sensor_specs.args_count > 1) 439 pr_warn("%pOFn: too many cells in sensor specifier %d\n", 440 sensor_specs.np, sensor_specs.args_count); 441 442 *id = sensor_specs.args_count ? sensor_specs.args[0] : 0; 443 444 of_node_put(sensor_specs.np); 445 446 return 0; 447 } 448 EXPORT_SYMBOL_GPL(thermal_zone_of_get_sensor_id); 449 450 /** 451 * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone 452 * @dev: a valid struct device pointer of a sensor device. Must contain 453 * a valid .of_node, for the sensor node. 454 * @sensor_id: a sensor identifier, in case the sensor IP has more 455 * than one sensors 456 * @data: a private pointer (owned by the caller) that will be passed 457 * back, when a temperature reading is needed. 458 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp. 459 * 460 * This function will search the list of thermal zones described in device 461 * tree and look for the zone that refer to the sensor device pointed by 462 * @dev->of_node as temperature providers. For the zone pointing to the 463 * sensor node, the sensor will be added to the DT thermal zone device. 464 * 465 * The thermal zone temperature is provided by the @get_temp function 466 * pointer. When called, it will have the private pointer @data back. 467 * 468 * The thermal zone temperature trend is provided by the @get_trend function 469 * pointer. When called, it will have the private pointer @data back. 470 * 471 * TODO: 472 * 01 - This function must enqueue the new sensor instead of using 473 * it as the only source of temperature values. 474 * 475 * 02 - There must be a way to match the sensor with all thermal zones 476 * that refer to it. 477 * 478 * Return: On success returns a valid struct thermal_zone_device, 479 * otherwise, it returns a corresponding ERR_PTR(). Caller must 480 * check the return value with help of IS_ERR() helper. 481 */ 482 struct thermal_zone_device * 483 thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data, 484 const struct thermal_zone_of_device_ops *ops) 485 { 486 struct device_node *np, *child, *sensor_np; 487 struct thermal_zone_device *tzd = ERR_PTR(-ENODEV); 488 static int old_tz_initialized; 489 int ret; 490 491 if (!old_tz_initialized) { 492 ret = of_parse_thermal_zones(); 493 if (ret) 494 return ERR_PTR(ret); 495 old_tz_initialized = 1; 496 } 497 498 np = of_find_node_by_name(NULL, "thermal-zones"); 499 if (!np) 500 return ERR_PTR(-ENODEV); 501 502 if (!dev || !dev->of_node) { 503 of_node_put(np); 504 return ERR_PTR(-ENODEV); 505 } 506 507 sensor_np = of_node_get(dev->of_node); 508 509 for_each_available_child_of_node(np, child) { 510 int ret, id; 511 512 /* For now, thermal framework supports only 1 sensor per zone */ 513 ret = thermal_zone_of_get_sensor_id(child, sensor_np, &id); 514 if (ret) 515 continue; 516 517 if (id == sensor_id) { 518 tzd = thermal_zone_of_add_sensor(child, sensor_np, 519 data, ops); 520 if (!IS_ERR(tzd)) 521 thermal_zone_device_enable(tzd); 522 523 of_node_put(child); 524 goto exit; 525 } 526 } 527 exit: 528 of_node_put(sensor_np); 529 of_node_put(np); 530 531 return tzd; 532 } 533 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register); 534 535 /** 536 * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone 537 * @dev: a valid struct device pointer of a sensor device. Must contain 538 * a valid .of_node, for the sensor node. 539 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered. 540 * 541 * This function removes the sensor callbacks and private data from the 542 * thermal zone device registered with thermal_zone_of_sensor_register() 543 * API. It will also silent the zone by remove the .get_temp() and .get_trend() 544 * thermal zone device callbacks. 545 * 546 * TODO: When the support to several sensors per zone is added, this 547 * function must search the sensor list based on @dev parameter. 548 * 549 */ 550 void thermal_zone_of_sensor_unregister(struct device *dev, 551 struct thermal_zone_device *tzd) 552 { 553 struct __thermal_zone *tz; 554 555 if (!dev || !tzd || !tzd->devdata) 556 return; 557 558 tz = tzd->devdata; 559 560 /* no __thermal_zone, nothing to be done */ 561 if (!tz) 562 return; 563 564 /* stop temperature polling */ 565 thermal_zone_device_disable(tzd); 566 567 mutex_lock(&tzd->lock); 568 tzd->ops->get_temp = NULL; 569 tzd->ops->get_trend = NULL; 570 tzd->ops->set_emul_temp = NULL; 571 tzd->ops->change_mode = NULL; 572 573 tz->ops = NULL; 574 tz->sensor_data = NULL; 575 mutex_unlock(&tzd->lock); 576 } 577 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister); 578 579 static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res) 580 { 581 thermal_zone_of_sensor_unregister(dev, 582 *(struct thermal_zone_device **)res); 583 } 584 585 static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res, 586 void *data) 587 { 588 struct thermal_zone_device **r = res; 589 590 if (WARN_ON(!r || !*r)) 591 return 0; 592 593 return *r == data; 594 } 595 596 /** 597 * devm_thermal_zone_of_sensor_register - Resource managed version of 598 * thermal_zone_of_sensor_register() 599 * @dev: a valid struct device pointer of a sensor device. Must contain 600 * a valid .of_node, for the sensor node. 601 * @sensor_id: a sensor identifier, in case the sensor IP has more 602 * than one sensors 603 * @data: a private pointer (owned by the caller) that will be passed 604 * back, when a temperature reading is needed. 605 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp. 606 * 607 * Refer thermal_zone_of_sensor_register() for more details. 608 * 609 * Return: On success returns a valid struct thermal_zone_device, 610 * otherwise, it returns a corresponding ERR_PTR(). Caller must 611 * check the return value with help of IS_ERR() helper. 612 * Registered thermal_zone_device device will automatically be 613 * released when device is unbounded. 614 */ 615 struct thermal_zone_device *devm_thermal_zone_of_sensor_register( 616 struct device *dev, int sensor_id, 617 void *data, const struct thermal_zone_of_device_ops *ops) 618 { 619 struct thermal_zone_device **ptr, *tzd; 620 621 ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr), 622 GFP_KERNEL); 623 if (!ptr) 624 return ERR_PTR(-ENOMEM); 625 626 tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops); 627 if (IS_ERR(tzd)) { 628 devres_free(ptr); 629 return tzd; 630 } 631 632 *ptr = tzd; 633 devres_add(dev, ptr); 634 635 return tzd; 636 } 637 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register); 638 639 /** 640 * devm_thermal_zone_of_sensor_unregister - Resource managed version of 641 * thermal_zone_of_sensor_unregister(). 642 * @dev: Device for which which resource was allocated. 643 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered. 644 * 645 * This function removes the sensor callbacks and private data from the 646 * thermal zone device registered with devm_thermal_zone_of_sensor_register() 647 * API. It will also silent the zone by remove the .get_temp() and .get_trend() 648 * thermal zone device callbacks. 649 * Normally this function will not need to be called and the resource 650 * management code will ensure that the resource is freed. 651 */ 652 void devm_thermal_zone_of_sensor_unregister(struct device *dev, 653 struct thermal_zone_device *tzd) 654 { 655 WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release, 656 devm_thermal_zone_of_sensor_match, tzd)); 657 } 658 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister); 659 660 /*** functions parsing device tree nodes ***/ 661 662 static int of_find_trip_id(struct device_node *np, struct device_node *trip) 663 { 664 struct device_node *trips; 665 struct device_node *t; 666 int i = 0; 667 668 trips = of_get_child_by_name(np, "trips"); 669 if (!trips) { 670 pr_err("Failed to find 'trips' node\n"); 671 return -EINVAL; 672 } 673 674 /* 675 * Find the trip id point associated with the cooling device map 676 */ 677 for_each_child_of_node(trips, t) { 678 679 if (t == trip) 680 goto out; 681 i++; 682 } 683 684 i = -ENXIO; 685 out: 686 of_node_put(trips); 687 688 return i; 689 } 690 691 /** 692 * thermal_of_populate_bind_params - parse and fill cooling map data 693 * @np: DT node containing a cooling-map node 694 * @__tbp: data structure to be filled with cooling map info 695 * @trips: array of thermal zone trip points 696 * @ntrips: number of trip points inside trips. 697 * 698 * This function parses a cooling-map type of node represented by 699 * @np parameter and fills the read data into @__tbp data structure. 700 * It needs the already parsed array of trip points of the thermal zone 701 * in consideration. 702 * 703 * Return: 0 on success, proper error code otherwise 704 */ 705 static int thermal_of_populate_bind_params(struct device_node *tz_np, 706 struct device_node *np, 707 struct __thermal_bind_params *__tbp) 708 { 709 struct of_phandle_args cooling_spec; 710 struct __thermal_cooling_bind_param *__tcbp; 711 struct device_node *trip; 712 int ret, i, count; 713 int trip_id; 714 u32 prop; 715 716 /* Default weight. Usage is optional */ 717 __tbp->usage = THERMAL_WEIGHT_DEFAULT; 718 ret = of_property_read_u32(np, "contribution", &prop); 719 if (ret == 0) 720 __tbp->usage = prop; 721 722 trip = of_parse_phandle(np, "trip", 0); 723 if (!trip) { 724 pr_err("missing trip property\n"); 725 return -ENODEV; 726 } 727 728 trip_id = of_find_trip_id(tz_np, trip); 729 if (trip_id < 0) { 730 ret = trip_id; 731 goto end; 732 } 733 734 __tbp->trip_id = trip_id; 735 736 count = of_count_phandle_with_args(np, "cooling-device", 737 "#cooling-cells"); 738 if (count <= 0) { 739 pr_err("Add a cooling_device property with at least one device\n"); 740 ret = -ENOENT; 741 goto end; 742 } 743 744 __tcbp = kcalloc(count, sizeof(*__tcbp), GFP_KERNEL); 745 if (!__tcbp) { 746 ret = -ENOMEM; 747 goto end; 748 } 749 750 for (i = 0; i < count; i++) { 751 ret = of_parse_phandle_with_args(np, "cooling-device", 752 "#cooling-cells", i, &cooling_spec); 753 if (ret < 0) { 754 pr_err("Invalid cooling-device entry\n"); 755 goto free_tcbp; 756 } 757 758 __tcbp[i].cooling_device = cooling_spec.np; 759 760 if (cooling_spec.args_count >= 2) { /* at least min and max */ 761 __tcbp[i].min = cooling_spec.args[0]; 762 __tcbp[i].max = cooling_spec.args[1]; 763 } else { 764 pr_err("wrong reference to cooling device, missing limits\n"); 765 } 766 } 767 768 __tbp->tcbp = __tcbp; 769 __tbp->count = count; 770 771 goto end; 772 773 free_tcbp: 774 for (i = i - 1; i >= 0; i--) 775 of_node_put(__tcbp[i].cooling_device); 776 kfree(__tcbp); 777 end: 778 of_node_put(trip); 779 780 return ret; 781 } 782 783 /* 784 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h 785 * into the device tree binding of 'trip', property type. 786 */ 787 static const char * const trip_types[] = { 788 [THERMAL_TRIP_ACTIVE] = "active", 789 [THERMAL_TRIP_PASSIVE] = "passive", 790 [THERMAL_TRIP_HOT] = "hot", 791 [THERMAL_TRIP_CRITICAL] = "critical", 792 }; 793 794 /** 795 * thermal_of_get_trip_type - Get phy mode for given device_node 796 * @np: Pointer to the given device_node 797 * @type: Pointer to resulting trip type 798 * 799 * The function gets trip type string from property 'type', 800 * and store its index in trip_types table in @type, 801 * 802 * Return: 0 on success, or errno in error case. 803 */ 804 static int thermal_of_get_trip_type(struct device_node *np, 805 enum thermal_trip_type *type) 806 { 807 const char *t; 808 int err, i; 809 810 err = of_property_read_string(np, "type", &t); 811 if (err < 0) 812 return err; 813 814 for (i = 0; i < ARRAY_SIZE(trip_types); i++) 815 if (!strcasecmp(t, trip_types[i])) { 816 *type = i; 817 return 0; 818 } 819 820 return -ENODEV; 821 } 822 823 static int thermal_of_populate_trip(struct device_node *np, 824 struct thermal_trip *trip) 825 { 826 int prop; 827 int ret; 828 829 ret = of_property_read_u32(np, "temperature", &prop); 830 if (ret < 0) { 831 pr_err("missing temperature property\n"); 832 return ret; 833 } 834 trip->temperature = prop; 835 836 ret = of_property_read_u32(np, "hysteresis", &prop); 837 if (ret < 0) { 838 pr_err("missing hysteresis property\n"); 839 return ret; 840 } 841 trip->hysteresis = prop; 842 843 ret = thermal_of_get_trip_type(np, &trip->type); 844 if (ret < 0) { 845 pr_err("wrong trip type property\n"); 846 return ret; 847 } 848 849 return 0; 850 } 851 852 static struct thermal_trip *thermal_of_trips_init(struct device_node *np, int *ntrips) 853 { 854 struct thermal_trip *tt; 855 struct device_node *trips, *trip; 856 int ret, count; 857 858 trips = of_get_child_by_name(np, "trips"); 859 if (!trips) { 860 pr_err("Failed to find 'trips' node\n"); 861 return ERR_PTR(-EINVAL); 862 } 863 864 count = of_get_child_count(trips); 865 if (!count) { 866 pr_err("No trip point defined\n"); 867 ret = -EINVAL; 868 goto out_of_node_put; 869 } 870 871 tt = kzalloc(sizeof(*tt) * count, GFP_KERNEL); 872 if (!tt) { 873 ret = -ENOMEM; 874 goto out_of_node_put; 875 } 876 877 *ntrips = count; 878 879 count = 0; 880 for_each_child_of_node(trips, trip) { 881 ret = thermal_of_populate_trip(trip, &tt[count++]); 882 if (ret) 883 goto out_kfree; 884 } 885 886 of_node_put(trips); 887 888 return tt; 889 890 out_kfree: 891 kfree(tt); 892 *ntrips = 0; 893 out_of_node_put: 894 of_node_put(trips); 895 896 return ERR_PTR(ret); 897 } 898 899 /** 900 * thermal_of_build_thermal_zone - parse and fill one thermal zone data 901 * @np: DT node containing a thermal zone node 902 * 903 * This function parses a thermal zone type of node represented by 904 * @np parameter and fills the read data into a __thermal_zone data structure 905 * and return this pointer. 906 * 907 * TODO: Missing properties to parse: thermal-sensor-names 908 * 909 * Return: On success returns a valid struct __thermal_zone, 910 * otherwise, it returns a corresponding ERR_PTR(). Caller must 911 * check the return value with help of IS_ERR() helper. 912 */ 913 static struct __thermal_zone 914 __init *thermal_of_build_thermal_zone(struct device_node *np) 915 { 916 struct device_node *child = NULL, *gchild; 917 struct __thermal_zone *tz; 918 int ret, i; 919 u32 prop, coef[2]; 920 921 if (!np) { 922 pr_err("no thermal zone np\n"); 923 return ERR_PTR(-EINVAL); 924 } 925 926 tz = kzalloc(sizeof(*tz), GFP_KERNEL); 927 if (!tz) 928 return ERR_PTR(-ENOMEM); 929 930 ret = of_property_read_u32(np, "polling-delay-passive", &prop); 931 if (ret < 0) { 932 pr_err("%pOFn: missing polling-delay-passive property\n", np); 933 goto free_tz; 934 } 935 tz->passive_delay = prop; 936 937 ret = of_property_read_u32(np, "polling-delay", &prop); 938 if (ret < 0) { 939 pr_err("%pOFn: missing polling-delay property\n", np); 940 goto free_tz; 941 } 942 tz->polling_delay = prop; 943 944 /* 945 * REVIST: for now, the thermal framework supports only 946 * one sensor per thermal zone. Thus, we are considering 947 * only the first two values as slope and offset. 948 */ 949 ret = of_property_read_u32_array(np, "coefficients", coef, 2); 950 if (ret == 0) { 951 tz->slope = coef[0]; 952 tz->offset = coef[1]; 953 } else { 954 tz->slope = 1; 955 tz->offset = 0; 956 } 957 958 tz->trips = thermal_of_trips_init(np, &tz->ntrips); 959 if (IS_ERR(tz->trips)) { 960 ret = PTR_ERR(tz->trips); 961 goto finish; 962 } 963 964 /* cooling-maps */ 965 child = of_get_child_by_name(np, "cooling-maps"); 966 967 /* cooling-maps not provided */ 968 if (!child) 969 goto finish; 970 971 tz->num_tbps = of_get_child_count(child); 972 if (tz->num_tbps == 0) 973 goto finish; 974 975 tz->tbps = kcalloc(tz->num_tbps, sizeof(*tz->tbps), GFP_KERNEL); 976 if (!tz->tbps) { 977 ret = -ENOMEM; 978 goto free_trips; 979 } 980 981 i = 0; 982 for_each_child_of_node(child, gchild) { 983 ret = thermal_of_populate_bind_params(np, gchild, &tz->tbps[i++]); 984 if (ret) { 985 of_node_put(gchild); 986 goto free_tbps; 987 } 988 } 989 990 finish: 991 of_node_put(child); 992 993 return tz; 994 995 free_tbps: 996 for (i = i - 1; i >= 0; i--) { 997 struct __thermal_bind_params *tbp = tz->tbps + i; 998 int j; 999 1000 for (j = 0; j < tbp->count; j++) 1001 of_node_put(tbp->tcbp[j].cooling_device); 1002 1003 kfree(tbp->tcbp); 1004 } 1005 1006 kfree(tz->tbps); 1007 free_trips: 1008 kfree(tz->trips); 1009 free_tz: 1010 kfree(tz); 1011 of_node_put(child); 1012 1013 return ERR_PTR(ret); 1014 } 1015 1016 static void of_thermal_free_zone(struct __thermal_zone *tz) 1017 { 1018 struct __thermal_bind_params *tbp; 1019 int i, j; 1020 1021 for (i = 0; i < tz->num_tbps; i++) { 1022 tbp = tz->tbps + i; 1023 1024 for (j = 0; j < tbp->count; j++) 1025 of_node_put(tbp->tcbp[j].cooling_device); 1026 1027 kfree(tbp->tcbp); 1028 } 1029 1030 kfree(tz->tbps); 1031 kfree(tz->trips); 1032 kfree(tz); 1033 } 1034 1035 /** 1036 * of_thermal_destroy_zones - remove all zones parsed and allocated resources 1037 * 1038 * Finds all zones parsed and added to the thermal framework and remove them 1039 * from the system, together with their resources. 1040 * 1041 */ 1042 static __init void of_thermal_destroy_zones(void) 1043 { 1044 struct device_node *np, *child; 1045 1046 np = of_find_node_by_name(NULL, "thermal-zones"); 1047 if (!np) { 1048 pr_debug("unable to find thermal zones\n"); 1049 return; 1050 } 1051 1052 for_each_available_child_of_node(np, child) { 1053 struct thermal_zone_device *zone; 1054 1055 zone = thermal_zone_get_zone_by_name(child->name); 1056 if (IS_ERR(zone)) 1057 continue; 1058 1059 thermal_zone_device_unregister(zone); 1060 kfree(zone->tzp); 1061 kfree(zone->ops); 1062 of_thermal_free_zone(zone->devdata); 1063 } 1064 of_node_put(np); 1065 } 1066 1067 static struct device_node *of_thermal_zone_find(struct device_node *sensor, int id) 1068 { 1069 struct device_node *np, *tz; 1070 struct of_phandle_args sensor_specs; 1071 1072 np = of_find_node_by_name(NULL, "thermal-zones"); 1073 if (!np) { 1074 pr_debug("No thermal zones description\n"); 1075 return ERR_PTR(-ENODEV); 1076 } 1077 1078 /* 1079 * Search for each thermal zone, a defined sensor 1080 * corresponding to the one passed as parameter 1081 */ 1082 for_each_available_child_of_node(np, tz) { 1083 1084 int count, i; 1085 1086 count = of_count_phandle_with_args(tz, "thermal-sensors", 1087 "#thermal-sensor-cells"); 1088 if (count <= 0) { 1089 pr_err("%pOFn: missing thermal sensor\n", tz); 1090 tz = ERR_PTR(-EINVAL); 1091 goto out; 1092 } 1093 1094 for (i = 0; i < count; i++) { 1095 1096 int ret; 1097 1098 ret = of_parse_phandle_with_args(tz, "thermal-sensors", 1099 "#thermal-sensor-cells", 1100 i, &sensor_specs); 1101 if (ret < 0) { 1102 pr_err("%pOFn: Failed to read thermal-sensors cells: %d\n", tz, ret); 1103 tz = ERR_PTR(ret); 1104 goto out; 1105 } 1106 1107 if ((sensor == sensor_specs.np) && id == (sensor_specs.args_count ? 1108 sensor_specs.args[0] : 0)) { 1109 pr_debug("sensor %pOFn id=%d belongs to %pOFn\n", sensor, id, tz); 1110 goto out; 1111 } 1112 } 1113 } 1114 tz = ERR_PTR(-ENODEV); 1115 out: 1116 of_node_put(np); 1117 return tz; 1118 } 1119 1120 static int thermal_of_monitor_init(struct device_node *np, int *delay, int *pdelay) 1121 { 1122 int ret; 1123 1124 ret = of_property_read_u32(np, "polling-delay-passive", pdelay); 1125 if (ret < 0) { 1126 pr_err("%pOFn: missing polling-delay-passive property\n", np); 1127 return ret; 1128 } 1129 1130 ret = of_property_read_u32(np, "polling-delay", delay); 1131 if (ret < 0) { 1132 pr_err("%pOFn: missing polling-delay property\n", np); 1133 return ret; 1134 } 1135 1136 return 0; 1137 } 1138 1139 static struct thermal_zone_params *thermal_of_parameters_init(struct device_node *np) 1140 { 1141 struct thermal_zone_params *tzp; 1142 int coef[2]; 1143 int ncoef = ARRAY_SIZE(coef); 1144 int prop, ret; 1145 1146 tzp = kzalloc(sizeof(*tzp), GFP_KERNEL); 1147 if (!tzp) 1148 return ERR_PTR(-ENOMEM); 1149 1150 tzp->no_hwmon = true; 1151 1152 if (!of_property_read_u32(np, "sustainable-power", &prop)) 1153 tzp->sustainable_power = prop; 1154 1155 /* 1156 * For now, the thermal framework supports only one sensor per 1157 * thermal zone. Thus, we are considering only the first two 1158 * values as slope and offset. 1159 */ 1160 ret = of_property_read_u32_array(np, "coefficients", coef, ncoef); 1161 if (ret) { 1162 coef[0] = 1; 1163 coef[1] = 0; 1164 } 1165 1166 tzp->slope = coef[0]; 1167 tzp->offset = coef[1]; 1168 1169 return tzp; 1170 } 1171 1172 static struct device_node *thermal_of_zone_get_by_name(struct thermal_zone_device *tz) 1173 { 1174 struct device_node *np, *tz_np; 1175 1176 np = of_find_node_by_name(NULL, "thermal-zones"); 1177 if (!np) 1178 return ERR_PTR(-ENODEV); 1179 1180 tz_np = of_get_child_by_name(np, tz->type); 1181 1182 of_node_put(np); 1183 1184 if (!tz_np) 1185 return ERR_PTR(-ENODEV); 1186 1187 return tz_np; 1188 } 1189 1190 static int __thermal_of_unbind(struct device_node *map_np, int index, int trip_id, 1191 struct thermal_zone_device *tz, struct thermal_cooling_device *cdev) 1192 { 1193 struct of_phandle_args cooling_spec; 1194 int ret; 1195 1196 ret = of_parse_phandle_with_args(map_np, "cooling-device", "#cooling-cells", 1197 index, &cooling_spec); 1198 1199 of_node_put(cooling_spec.np); 1200 1201 if (ret < 0) { 1202 pr_err("Invalid cooling-device entry\n"); 1203 return ret; 1204 } 1205 1206 if (cooling_spec.args_count < 2) { 1207 pr_err("wrong reference to cooling device, missing limits\n"); 1208 return -EINVAL; 1209 } 1210 1211 if (cooling_spec.np != cdev->np) 1212 return 0; 1213 1214 ret = thermal_zone_unbind_cooling_device(tz, trip_id, cdev); 1215 if (ret) 1216 pr_err("Failed to unbind '%s' with '%s': %d\n", tz->type, cdev->type, ret); 1217 1218 return ret; 1219 } 1220 1221 static int __thermal_of_bind(struct device_node *map_np, int index, int trip_id, 1222 struct thermal_zone_device *tz, struct thermal_cooling_device *cdev) 1223 { 1224 struct of_phandle_args cooling_spec; 1225 int ret, weight = THERMAL_WEIGHT_DEFAULT; 1226 1227 of_property_read_u32(map_np, "contribution", &weight); 1228 1229 ret = of_parse_phandle_with_args(map_np, "cooling-device", "#cooling-cells", 1230 index, &cooling_spec); 1231 1232 of_node_put(cooling_spec.np); 1233 1234 if (ret < 0) { 1235 pr_err("Invalid cooling-device entry\n"); 1236 return ret; 1237 } 1238 1239 if (cooling_spec.args_count < 2) { 1240 pr_err("wrong reference to cooling device, missing limits\n"); 1241 return -EINVAL; 1242 } 1243 1244 if (cooling_spec.np != cdev->np) 1245 return 0; 1246 1247 ret = thermal_zone_bind_cooling_device(tz, trip_id, cdev, cooling_spec.args[1], 1248 cooling_spec.args[0], 1249 weight); 1250 if (ret) 1251 pr_err("Failed to bind '%s' with '%s': %d\n", tz->type, cdev->type, ret); 1252 1253 return ret; 1254 } 1255 1256 static int thermal_of_for_each_cooling_device(struct device_node *tz_np, struct device_node *map_np, 1257 struct thermal_zone_device *tz, struct thermal_cooling_device *cdev, 1258 int (*action)(struct device_node *, int, int, 1259 struct thermal_zone_device *, struct thermal_cooling_device *)) 1260 { 1261 struct device_node *tr_np; 1262 int count, i, trip_id; 1263 1264 tr_np = of_parse_phandle(map_np, "trip", 0); 1265 if (!tr_np) 1266 return -ENODEV; 1267 1268 trip_id = of_find_trip_id(tz_np, tr_np); 1269 if (trip_id < 0) 1270 return trip_id; 1271 1272 count = of_count_phandle_with_args(map_np, "cooling-device", "#cooling-cells"); 1273 if (count <= 0) { 1274 pr_err("Add a cooling_device property with at least one device\n"); 1275 return -ENOENT; 1276 } 1277 1278 /* 1279 * At this point, we don't want to bail out when there is an 1280 * error, we will try to bind/unbind as many as possible 1281 * cooling devices 1282 */ 1283 for (i = 0; i < count; i++) 1284 action(map_np, i, trip_id, tz, cdev); 1285 1286 return 0; 1287 } 1288 1289 static int thermal_of_for_each_cooling_maps(struct thermal_zone_device *tz, 1290 struct thermal_cooling_device *cdev, 1291 int (*action)(struct device_node *, int, int, 1292 struct thermal_zone_device *, struct thermal_cooling_device *)) 1293 { 1294 struct device_node *tz_np, *cm_np, *child; 1295 int ret = 0; 1296 1297 tz_np = thermal_of_zone_get_by_name(tz); 1298 if (IS_ERR(tz_np)) { 1299 pr_err("Failed to get node tz by name\n"); 1300 return PTR_ERR(tz_np); 1301 } 1302 1303 cm_np = of_get_child_by_name(tz_np, "cooling-maps"); 1304 if (!cm_np) 1305 goto out; 1306 1307 for_each_child_of_node(cm_np, child) { 1308 ret = thermal_of_for_each_cooling_device(tz_np, child, tz, cdev, action); 1309 if (ret) 1310 break; 1311 } 1312 1313 of_node_put(cm_np); 1314 out: 1315 of_node_put(tz_np); 1316 1317 return ret; 1318 } 1319 1320 static int thermal_of_bind(struct thermal_zone_device *tz, 1321 struct thermal_cooling_device *cdev) 1322 { 1323 return thermal_of_for_each_cooling_maps(tz, cdev, __thermal_of_bind); 1324 } 1325 1326 static int thermal_of_unbind(struct thermal_zone_device *tz, 1327 struct thermal_cooling_device *cdev) 1328 { 1329 return thermal_of_for_each_cooling_maps(tz, cdev, __thermal_of_unbind); 1330 } 1331 1332 /** 1333 * thermal_of_zone_unregister - Cleanup the specific allocated ressources 1334 * 1335 * This function disables the thermal zone and frees the different 1336 * ressources allocated specific to the thermal OF. 1337 * 1338 * @tz: a pointer to the thermal zone structure 1339 */ 1340 void thermal_of_zone_unregister(struct thermal_zone_device *tz) 1341 { 1342 struct thermal_trip *trips = tz->trips; 1343 struct thermal_zone_params *tzp = tz->tzp; 1344 struct thermal_zone_device_ops *ops = tz->ops; 1345 1346 thermal_zone_device_disable(tz); 1347 thermal_zone_device_unregister(tz); 1348 kfree(trips); 1349 kfree(tzp); 1350 kfree(ops); 1351 } 1352 EXPORT_SYMBOL_GPL(thermal_of_zone_unregister); 1353 1354 /** 1355 * thermal_of_zone_register - Register a thermal zone with device node 1356 * sensor 1357 * 1358 * The thermal_of_zone_register() parses a device tree given a device 1359 * node sensor and identifier. It searches for the thermal zone 1360 * associated to the couple sensor/id and retrieves all the thermal 1361 * zone properties and registers new thermal zone with those 1362 * properties. 1363 * 1364 * @sensor: A device node pointer corresponding to the sensor in the device tree 1365 * @id: An integer as sensor identifier 1366 * @data: A private data to be stored in the thermal zone dedicated private area 1367 * @ops: A set of thermal sensor ops 1368 * 1369 * Return: a valid thermal zone structure pointer on success. 1370 * - EINVAL: if the device tree thermal description is malformed 1371 * - ENOMEM: if one structure can not be allocated 1372 * - Other negative errors are returned by the underlying called functions 1373 */ 1374 struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor, int id, void *data, 1375 const struct thermal_zone_device_ops *ops) 1376 { 1377 struct thermal_zone_device *tz; 1378 struct thermal_trip *trips; 1379 struct thermal_zone_params *tzp; 1380 struct thermal_zone_device_ops *of_ops; 1381 struct device_node *np; 1382 int delay, pdelay; 1383 int ntrips, mask; 1384 int ret; 1385 1386 of_ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL); 1387 if (!of_ops) 1388 return ERR_PTR(-ENOMEM); 1389 1390 np = of_thermal_zone_find(sensor, id); 1391 if (IS_ERR(np)) { 1392 if (PTR_ERR(np) != -ENODEV) 1393 pr_err("Failed to find thermal zone for %pOFn id=%d\n", sensor, id); 1394 return ERR_CAST(np); 1395 } 1396 1397 trips = thermal_of_trips_init(np, &ntrips); 1398 if (IS_ERR(trips)) { 1399 pr_err("Failed to find trip points for %pOFn id=%d\n", sensor, id); 1400 return ERR_CAST(trips); 1401 } 1402 1403 ret = thermal_of_monitor_init(np, &delay, &pdelay); 1404 if (ret) { 1405 pr_err("Failed to initialize monitoring delays from %pOFn\n", np); 1406 goto out_kfree_trips; 1407 } 1408 1409 tzp = thermal_of_parameters_init(np); 1410 if (IS_ERR(tzp)) { 1411 ret = PTR_ERR(tzp); 1412 pr_err("Failed to initialize parameter from %pOFn: %d\n", np, ret); 1413 goto out_kfree_trips; 1414 } 1415 1416 of_ops->get_trip_type = of_ops->get_trip_type ? : of_thermal_get_trip_type; 1417 of_ops->get_trip_temp = of_ops->get_trip_temp ? : of_thermal_get_trip_temp; 1418 of_ops->get_trip_hyst = of_ops->get_trip_hyst ? : of_thermal_get_trip_hyst; 1419 of_ops->set_trip_hyst = of_ops->set_trip_hyst ? : of_thermal_set_trip_hyst; 1420 of_ops->get_crit_temp = of_ops->get_crit_temp ? : of_thermal_get_crit_temp; 1421 of_ops->bind = thermal_of_bind; 1422 of_ops->unbind = thermal_of_unbind; 1423 1424 mask = GENMASK_ULL((ntrips) - 1, 0); 1425 1426 tz = thermal_zone_device_register_with_trips(np->name, trips, ntrips, 1427 mask, data, of_ops, tzp, 1428 pdelay, delay); 1429 if (IS_ERR(tz)) { 1430 ret = PTR_ERR(tz); 1431 pr_err("Failed to register thermal zone %pOFn: %d\n", np, ret); 1432 goto out_kfree_tzp; 1433 } 1434 1435 ret = thermal_zone_device_enable(tz); 1436 if (ret) { 1437 pr_err("Failed to enabled thermal zone '%s', id=%d: %d\n", 1438 tz->type, tz->id, ret); 1439 thermal_of_zone_unregister(tz); 1440 return ERR_PTR(ret); 1441 } 1442 1443 return tz; 1444 1445 out_kfree_tzp: 1446 kfree(tzp); 1447 out_kfree_trips: 1448 kfree(trips); 1449 1450 return ERR_PTR(ret); 1451 } 1452 EXPORT_SYMBOL_GPL(thermal_of_zone_register); 1453 1454 static void devm_thermal_of_zone_release(struct device *dev, void *res) 1455 { 1456 thermal_of_zone_unregister(*(struct thermal_zone_device **)res); 1457 } 1458 1459 static int devm_thermal_of_zone_match(struct device *dev, void *res, 1460 void *data) 1461 { 1462 struct thermal_zone_device **r = res; 1463 1464 if (WARN_ON(!r || !*r)) 1465 return 0; 1466 1467 return *r == data; 1468 } 1469 1470 /** 1471 * devm_thermal_of_zone_register - register a thermal tied with the sensor life cycle 1472 * 1473 * This function is the device version of the thermal_of_zone_register() function. 1474 * 1475 * @dev: a device structure pointer to sensor to be tied with the thermal zone OF life cycle 1476 * @sensor_id: the sensor identifier 1477 * @data: a pointer to a private data to be stored in the thermal zone 'devdata' field 1478 * @ops: a pointer to the ops structure associated with the sensor 1479 */ 1480 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int sensor_id, void *data, 1481 const struct thermal_zone_device_ops *ops) 1482 { 1483 struct thermal_zone_device **ptr, *tzd; 1484 1485 ptr = devres_alloc(devm_thermal_of_zone_release, sizeof(*ptr), 1486 GFP_KERNEL); 1487 if (!ptr) 1488 return ERR_PTR(-ENOMEM); 1489 1490 tzd = thermal_of_zone_register(dev->of_node, sensor_id, data, ops); 1491 if (IS_ERR(tzd)) { 1492 devres_free(ptr); 1493 return tzd; 1494 } 1495 1496 *ptr = tzd; 1497 devres_add(dev, ptr); 1498 1499 return tzd; 1500 } 1501 EXPORT_SYMBOL_GPL(devm_thermal_of_zone_register); 1502 1503 /** 1504 * devm_thermal_of_zone_unregister - Resource managed version of 1505 * thermal_of_zone_unregister(). 1506 * @dev: Device for which which resource was allocated. 1507 * @tz: a pointer to struct thermal_zone where the sensor is registered. 1508 * 1509 * This function removes the sensor callbacks and private data from the 1510 * thermal zone device registered with devm_thermal_zone_of_sensor_register() 1511 * API. It will also silent the zone by remove the .get_temp() and .get_trend() 1512 * thermal zone device callbacks. 1513 * Normally this function will not need to be called and the resource 1514 * management code will ensure that the resource is freed. 1515 */ 1516 void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz) 1517 { 1518 WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release, 1519 devm_thermal_of_zone_match, tz)); 1520 } 1521 EXPORT_SYMBOL_GPL(devm_thermal_of_zone_unregister); 1522 1523 /** 1524 * of_parse_thermal_zones - parse device tree thermal data 1525 * 1526 * Initialization function that can be called by machine initialization 1527 * code to parse thermal data and populate the thermal framework 1528 * with hardware thermal zones info. This function only parses thermal zones. 1529 * Cooling devices and sensor devices nodes are supposed to be parsed 1530 * by their respective drivers. 1531 * 1532 * Return: 0 on success, proper error code otherwise 1533 * 1534 */ 1535 int of_parse_thermal_zones(void) 1536 { 1537 struct device_node *np, *child; 1538 struct __thermal_zone *tz; 1539 struct thermal_zone_device_ops *ops; 1540 1541 np = of_find_node_by_name(NULL, "thermal-zones"); 1542 if (!np) { 1543 pr_debug("unable to find thermal zones\n"); 1544 return 0; /* Run successfully on systems without thermal DT */ 1545 } 1546 1547 for_each_available_child_of_node(np, child) { 1548 struct thermal_zone_device *zone; 1549 struct thermal_zone_params *tzp; 1550 int i, mask = 0; 1551 u32 prop; 1552 1553 tz = thermal_of_build_thermal_zone(child); 1554 if (IS_ERR(tz)) { 1555 pr_err("failed to build thermal zone %pOFn: %ld\n", 1556 child, 1557 PTR_ERR(tz)); 1558 continue; 1559 } 1560 1561 ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL); 1562 if (!ops) 1563 goto exit_free; 1564 1565 tzp = kzalloc(sizeof(*tzp), GFP_KERNEL); 1566 if (!tzp) { 1567 kfree(ops); 1568 goto exit_free; 1569 } 1570 1571 /* No hwmon because there might be hwmon drivers registering */ 1572 tzp->no_hwmon = true; 1573 1574 if (!of_property_read_u32(child, "sustainable-power", &prop)) 1575 tzp->sustainable_power = prop; 1576 1577 for (i = 0; i < tz->ntrips; i++) 1578 mask |= 1 << i; 1579 1580 /* these two are left for temperature drivers to use */ 1581 tzp->slope = tz->slope; 1582 tzp->offset = tz->offset; 1583 1584 zone = thermal_zone_device_register_with_trips(child->name, tz->trips, tz->ntrips, 1585 mask, tz, ops, tzp, tz->passive_delay, 1586 tz->polling_delay); 1587 if (IS_ERR(zone)) { 1588 pr_err("Failed to build %pOFn zone %ld\n", child, 1589 PTR_ERR(zone)); 1590 kfree(tzp); 1591 kfree(ops); 1592 of_thermal_free_zone(tz); 1593 /* attempting to build remaining zones still */ 1594 } 1595 } 1596 of_node_put(np); 1597 1598 return 0; 1599 1600 exit_free: 1601 of_node_put(child); 1602 of_node_put(np); 1603 of_thermal_free_zone(tz); 1604 1605 /* no memory available, so free what we have built */ 1606 of_thermal_destroy_zones(); 1607 1608 return -ENOMEM; 1609 } 1610