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_param - 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
of_thermal_get_temp(struct thermal_zone_device * tz,int * temp)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
of_thermal_set_trips(struct thermal_zone_device * tz,int low,int high)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 */
of_thermal_get_ntrips(struct thermal_zone_device * tz)119 int of_thermal_get_ntrips(struct thermal_zone_device *tz)
120 {
121 struct __thermal_zone *data = tz->devdata;
122
123 if (!data || IS_ERR(data))
124 return -ENODEV;
125
126 return data->ntrips;
127 }
128 EXPORT_SYMBOL_GPL(of_thermal_get_ntrips);
129
130 /**
131 * of_thermal_is_trip_valid - function to check if trip point is valid
132 *
133 * @tz: pointer to a thermal zone
134 * @trip: trip point to evaluate
135 *
136 * This function is responsible for checking if passed trip point is valid
137 *
138 * Return: true if trip point is valid, false otherwise
139 */
of_thermal_is_trip_valid(struct thermal_zone_device * tz,int trip)140 bool of_thermal_is_trip_valid(struct thermal_zone_device *tz, int trip)
141 {
142 struct __thermal_zone *data = tz->devdata;
143
144 if (!data || trip >= data->ntrips || trip < 0)
145 return false;
146
147 return true;
148 }
149 EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid);
150
151 /**
152 * of_thermal_get_trip_points - function to get access to a globally exported
153 * trip points
154 *
155 * @tz: pointer to a thermal zone
156 *
157 * This function provides a pointer to trip points table
158 *
159 * Return: pointer to trip points table, NULL otherwise
160 */
161 const struct thermal_trip *
of_thermal_get_trip_points(struct thermal_zone_device * tz)162 of_thermal_get_trip_points(struct thermal_zone_device *tz)
163 {
164 struct __thermal_zone *data = tz->devdata;
165
166 if (!data)
167 return NULL;
168
169 return data->trips;
170 }
171 EXPORT_SYMBOL_GPL(of_thermal_get_trip_points);
172
173 /**
174 * of_thermal_set_emul_temp - function to set emulated temperature
175 *
176 * @tz: pointer to a thermal zone
177 * @temp: temperature to set
178 *
179 * This function gives the ability to set emulated value of temperature,
180 * which is handy for debugging
181 *
182 * Return: zero on success, error code otherwise
183 */
of_thermal_set_emul_temp(struct thermal_zone_device * tz,int temp)184 static int of_thermal_set_emul_temp(struct thermal_zone_device *tz,
185 int temp)
186 {
187 struct __thermal_zone *data = tz->devdata;
188
189 if (!data->ops || !data->ops->set_emul_temp)
190 return -EINVAL;
191
192 return data->ops->set_emul_temp(data->sensor_data, temp);
193 }
194
of_thermal_get_trend(struct thermal_zone_device * tz,int trip,enum thermal_trend * trend)195 static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip,
196 enum thermal_trend *trend)
197 {
198 struct __thermal_zone *data = tz->devdata;
199
200 if (!data->ops || !data->ops->get_trend)
201 return -EINVAL;
202
203 return data->ops->get_trend(data->sensor_data, trip, trend);
204 }
205
of_thermal_change_mode(struct thermal_zone_device * tz,enum thermal_device_mode mode)206 static int of_thermal_change_mode(struct thermal_zone_device *tz,
207 enum thermal_device_mode mode)
208 {
209 struct __thermal_zone *data = tz->devdata;
210
211 return data->ops->change_mode(data->sensor_data, mode);
212 }
213
of_thermal_hot_notify(struct thermal_zone_device * tz)214 static void of_thermal_hot_notify(struct thermal_zone_device *tz)
215 {
216 struct __thermal_zone *data = tz->devdata;
217
218 data->ops->hot(data->sensor_data);
219 }
220
of_thermal_critical_notify(struct thermal_zone_device * tz)221 static void of_thermal_critical_notify(struct thermal_zone_device *tz)
222 {
223 struct __thermal_zone *data = tz->devdata;
224
225 data->ops->critical(data->sensor_data);
226 }
227
of_thermal_bind(struct thermal_zone_device * thermal,struct thermal_cooling_device * cdev)228 static int of_thermal_bind(struct thermal_zone_device *thermal,
229 struct thermal_cooling_device *cdev)
230 {
231 struct __thermal_zone *data = thermal->devdata;
232 struct __thermal_bind_params *tbp;
233 struct __thermal_cooling_bind_param *tcbp;
234 int i, j;
235
236 if (!data || IS_ERR(data))
237 return -ENODEV;
238
239 /* find where to bind */
240 for (i = 0; i < data->num_tbps; i++) {
241 tbp = data->tbps + i;
242
243 for (j = 0; j < tbp->count; j++) {
244 tcbp = tbp->tcbp + j;
245
246 if (tcbp->cooling_device == cdev->np) {
247 int ret;
248
249 ret = thermal_zone_bind_cooling_device(thermal,
250 tbp->trip_id, cdev,
251 tcbp->max,
252 tcbp->min,
253 tbp->usage);
254 if (ret)
255 return ret;
256 }
257 }
258 }
259
260 return 0;
261 }
262
of_thermal_unbind(struct thermal_zone_device * thermal,struct thermal_cooling_device * cdev)263 static int of_thermal_unbind(struct thermal_zone_device *thermal,
264 struct thermal_cooling_device *cdev)
265 {
266 struct __thermal_zone *data = thermal->devdata;
267 struct __thermal_bind_params *tbp;
268 struct __thermal_cooling_bind_param *tcbp;
269 int i, j;
270
271 if (!data || IS_ERR(data))
272 return -ENODEV;
273
274 /* find where to unbind */
275 for (i = 0; i < data->num_tbps; i++) {
276 tbp = data->tbps + i;
277
278 for (j = 0; j < tbp->count; j++) {
279 tcbp = tbp->tcbp + j;
280
281 if (tcbp->cooling_device == cdev->np) {
282 int ret;
283
284 ret = thermal_zone_unbind_cooling_device(thermal,
285 tbp->trip_id, cdev);
286 if (ret)
287 return ret;
288 }
289 }
290 }
291
292 return 0;
293 }
294
of_thermal_get_trip_type(struct thermal_zone_device * tz,int trip,enum thermal_trip_type * type)295 static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip,
296 enum thermal_trip_type *type)
297 {
298 struct __thermal_zone *data = tz->devdata;
299
300 if (trip >= data->ntrips || trip < 0)
301 return -EDOM;
302
303 *type = data->trips[trip].type;
304
305 return 0;
306 }
307
of_thermal_get_trip_temp(struct thermal_zone_device * tz,int trip,int * temp)308 static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip,
309 int *temp)
310 {
311 struct __thermal_zone *data = tz->devdata;
312
313 if (trip >= data->ntrips || trip < 0)
314 return -EDOM;
315
316 *temp = data->trips[trip].temperature;
317
318 return 0;
319 }
320
of_thermal_set_trip_temp(struct thermal_zone_device * tz,int trip,int temp)321 static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip,
322 int temp)
323 {
324 struct __thermal_zone *data = tz->devdata;
325
326 if (trip >= data->ntrips || trip < 0)
327 return -EDOM;
328
329 if (data->ops && data->ops->set_trip_temp) {
330 int ret;
331
332 ret = data->ops->set_trip_temp(data->sensor_data, trip, temp);
333 if (ret)
334 return ret;
335 }
336
337 /* thermal framework should take care of data->mask & (1 << trip) */
338 data->trips[trip].temperature = temp;
339
340 return 0;
341 }
342
of_thermal_get_trip_hyst(struct thermal_zone_device * tz,int trip,int * hyst)343 static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip,
344 int *hyst)
345 {
346 struct __thermal_zone *data = tz->devdata;
347
348 if (trip >= data->ntrips || trip < 0)
349 return -EDOM;
350
351 *hyst = data->trips[trip].hysteresis;
352
353 return 0;
354 }
355
of_thermal_set_trip_hyst(struct thermal_zone_device * tz,int trip,int hyst)356 static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip,
357 int hyst)
358 {
359 struct __thermal_zone *data = tz->devdata;
360
361 if (trip >= data->ntrips || trip < 0)
362 return -EDOM;
363
364 /* thermal framework should take care of data->mask & (1 << trip) */
365 data->trips[trip].hysteresis = hyst;
366
367 return 0;
368 }
369
of_thermal_get_crit_temp(struct thermal_zone_device * tz,int * temp)370 static int of_thermal_get_crit_temp(struct thermal_zone_device *tz,
371 int *temp)
372 {
373 struct __thermal_zone *data = tz->devdata;
374 int i;
375
376 for (i = 0; i < data->ntrips; i++)
377 if (data->trips[i].type == THERMAL_TRIP_CRITICAL) {
378 *temp = data->trips[i].temperature;
379 return 0;
380 }
381
382 return -EINVAL;
383 }
384
385 static struct thermal_zone_device_ops of_thermal_ops = {
386 .get_trip_type = of_thermal_get_trip_type,
387 .get_trip_temp = of_thermal_get_trip_temp,
388 .set_trip_temp = of_thermal_set_trip_temp,
389 .get_trip_hyst = of_thermal_get_trip_hyst,
390 .set_trip_hyst = of_thermal_set_trip_hyst,
391 .get_crit_temp = of_thermal_get_crit_temp,
392
393 .bind = of_thermal_bind,
394 .unbind = of_thermal_unbind,
395 };
396
397 /*** sensor API ***/
398
399 static struct thermal_zone_device *
thermal_zone_of_add_sensor(struct device_node * zone,struct device_node * sensor,void * data,const struct thermal_zone_of_device_ops * ops)400 thermal_zone_of_add_sensor(struct device_node *zone,
401 struct device_node *sensor, void *data,
402 const struct thermal_zone_of_device_ops *ops)
403 {
404 struct thermal_zone_device *tzd;
405 struct __thermal_zone *tz;
406
407 tzd = thermal_zone_get_zone_by_name(zone->name);
408 if (IS_ERR(tzd))
409 return ERR_PTR(-EPROBE_DEFER);
410
411 tz = tzd->devdata;
412
413 if (!ops)
414 return ERR_PTR(-EINVAL);
415
416 mutex_lock(&tzd->lock);
417 tz->ops = ops;
418 tz->sensor_data = data;
419
420 tzd->ops->get_temp = of_thermal_get_temp;
421 tzd->ops->get_trend = of_thermal_get_trend;
422
423 /*
424 * The thermal zone core will calculate the window if they have set the
425 * optional set_trips pointer.
426 */
427 if (ops->set_trips)
428 tzd->ops->set_trips = of_thermal_set_trips;
429
430 if (ops->set_emul_temp)
431 tzd->ops->set_emul_temp = of_thermal_set_emul_temp;
432
433 if (ops->change_mode)
434 tzd->ops->change_mode = of_thermal_change_mode;
435
436 if (ops->hot)
437 tzd->ops->hot = of_thermal_hot_notify;
438
439 if (ops->critical)
440 tzd->ops->critical = of_thermal_critical_notify;
441 mutex_unlock(&tzd->lock);
442
443 return tzd;
444 }
445
446 /**
447 * thermal_zone_of_get_sensor_id - get sensor ID from a DT thermal zone
448 * @tz_np: a valid thermal zone device node.
449 * @sensor_np: a sensor node of a valid sensor device.
450 * @id: the sensor ID returned if success.
451 *
452 * This function will get sensor ID from a given thermal zone node and
453 * the sensor node must match the temperature provider @sensor_np.
454 *
455 * Return: 0 on success, proper error code otherwise.
456 */
457
thermal_zone_of_get_sensor_id(struct device_node * tz_np,struct device_node * sensor_np,u32 * id)458 int thermal_zone_of_get_sensor_id(struct device_node *tz_np,
459 struct device_node *sensor_np,
460 u32 *id)
461 {
462 struct of_phandle_args sensor_specs;
463 int ret;
464
465 ret = of_parse_phandle_with_args(tz_np,
466 "thermal-sensors",
467 "#thermal-sensor-cells",
468 0,
469 &sensor_specs);
470 if (ret)
471 return ret;
472
473 if (sensor_specs.np != sensor_np) {
474 of_node_put(sensor_specs.np);
475 return -ENODEV;
476 }
477
478 if (sensor_specs.args_count > 1)
479 pr_warn("%pOFn: too many cells in sensor specifier %d\n",
480 sensor_specs.np, sensor_specs.args_count);
481
482 *id = sensor_specs.args_count ? sensor_specs.args[0] : 0;
483
484 of_node_put(sensor_specs.np);
485
486 return 0;
487 }
488 EXPORT_SYMBOL_GPL(thermal_zone_of_get_sensor_id);
489
490 /**
491 * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
492 * @dev: a valid struct device pointer of a sensor device. Must contain
493 * a valid .of_node, for the sensor node.
494 * @sensor_id: a sensor identifier, in case the sensor IP has more
495 * than one sensors
496 * @data: a private pointer (owned by the caller) that will be passed
497 * back, when a temperature reading is needed.
498 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
499 *
500 * This function will search the list of thermal zones described in device
501 * tree and look for the zone that refer to the sensor device pointed by
502 * @dev->of_node as temperature providers. For the zone pointing to the
503 * sensor node, the sensor will be added to the DT thermal zone device.
504 *
505 * The thermal zone temperature is provided by the @get_temp function
506 * pointer. When called, it will have the private pointer @data back.
507 *
508 * The thermal zone temperature trend is provided by the @get_trend function
509 * pointer. When called, it will have the private pointer @data back.
510 *
511 * TODO:
512 * 01 - This function must enqueue the new sensor instead of using
513 * it as the only source of temperature values.
514 *
515 * 02 - There must be a way to match the sensor with all thermal zones
516 * that refer to it.
517 *
518 * Return: On success returns a valid struct thermal_zone_device,
519 * otherwise, it returns a corresponding ERR_PTR(). Caller must
520 * check the return value with help of IS_ERR() helper.
521 */
522 struct thermal_zone_device *
thermal_zone_of_sensor_register(struct device * dev,int sensor_id,void * data,const struct thermal_zone_of_device_ops * ops)523 thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
524 const struct thermal_zone_of_device_ops *ops)
525 {
526 struct device_node *np, *child, *sensor_np;
527 struct thermal_zone_device *tzd = ERR_PTR(-ENODEV);
528
529 np = of_find_node_by_name(NULL, "thermal-zones");
530 if (!np)
531 return ERR_PTR(-ENODEV);
532
533 if (!dev || !dev->of_node) {
534 of_node_put(np);
535 return ERR_PTR(-ENODEV);
536 }
537
538 sensor_np = of_node_get(dev->of_node);
539
540 for_each_available_child_of_node(np, child) {
541 int ret, id;
542
543 /* For now, thermal framework supports only 1 sensor per zone */
544 ret = thermal_zone_of_get_sensor_id(child, sensor_np, &id);
545 if (ret)
546 continue;
547
548 if (id == sensor_id) {
549 tzd = thermal_zone_of_add_sensor(child, sensor_np,
550 data, ops);
551 if (!IS_ERR(tzd))
552 thermal_zone_device_enable(tzd);
553
554 of_node_put(child);
555 goto exit;
556 }
557 }
558 exit:
559 of_node_put(sensor_np);
560 of_node_put(np);
561
562 return tzd;
563 }
564 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register);
565
566 /**
567 * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
568 * @dev: a valid struct device pointer of a sensor device. Must contain
569 * a valid .of_node, for the sensor node.
570 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
571 *
572 * This function removes the sensor callbacks and private data from the
573 * thermal zone device registered with thermal_zone_of_sensor_register()
574 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
575 * thermal zone device callbacks.
576 *
577 * TODO: When the support to several sensors per zone is added, this
578 * function must search the sensor list based on @dev parameter.
579 *
580 */
thermal_zone_of_sensor_unregister(struct device * dev,struct thermal_zone_device * tzd)581 void thermal_zone_of_sensor_unregister(struct device *dev,
582 struct thermal_zone_device *tzd)
583 {
584 struct __thermal_zone *tz;
585
586 if (!dev || !tzd || !tzd->devdata)
587 return;
588
589 tz = tzd->devdata;
590
591 /* no __thermal_zone, nothing to be done */
592 if (!tz)
593 return;
594
595 /* stop temperature polling */
596 thermal_zone_device_disable(tzd);
597
598 mutex_lock(&tzd->lock);
599 tzd->ops->get_temp = NULL;
600 tzd->ops->get_trend = NULL;
601 tzd->ops->set_emul_temp = NULL;
602 tzd->ops->change_mode = NULL;
603 tzd->ops->hot = NULL;
604 tzd->ops->critical = NULL;
605
606 tz->ops = NULL;
607 tz->sensor_data = NULL;
608 mutex_unlock(&tzd->lock);
609 }
610 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
611
devm_thermal_zone_of_sensor_release(struct device * dev,void * res)612 static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res)
613 {
614 thermal_zone_of_sensor_unregister(dev,
615 *(struct thermal_zone_device **)res);
616 }
617
devm_thermal_zone_of_sensor_match(struct device * dev,void * res,void * data)618 static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res,
619 void *data)
620 {
621 struct thermal_zone_device **r = res;
622
623 if (WARN_ON(!r || !*r))
624 return 0;
625
626 return *r == data;
627 }
628
629 /**
630 * devm_thermal_zone_of_sensor_register - Resource managed version of
631 * thermal_zone_of_sensor_register()
632 * @dev: a valid struct device pointer of a sensor device. Must contain
633 * a valid .of_node, for the sensor node.
634 * @sensor_id: a sensor identifier, in case the sensor IP has more
635 * than one sensors
636 * @data: a private pointer (owned by the caller) that will be passed
637 * back, when a temperature reading is needed.
638 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
639 *
640 * Refer thermal_zone_of_sensor_register() for more details.
641 *
642 * Return: On success returns a valid struct thermal_zone_device,
643 * otherwise, it returns a corresponding ERR_PTR(). Caller must
644 * check the return value with help of IS_ERR() helper.
645 * Registered thermal_zone_device device will automatically be
646 * released when device is unbounded.
647 */
devm_thermal_zone_of_sensor_register(struct device * dev,int sensor_id,void * data,const struct thermal_zone_of_device_ops * ops)648 struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
649 struct device *dev, int sensor_id,
650 void *data, const struct thermal_zone_of_device_ops *ops)
651 {
652 struct thermal_zone_device **ptr, *tzd;
653
654 ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
655 GFP_KERNEL);
656 if (!ptr)
657 return ERR_PTR(-ENOMEM);
658
659 tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops);
660 if (IS_ERR(tzd)) {
661 devres_free(ptr);
662 return tzd;
663 }
664
665 *ptr = tzd;
666 devres_add(dev, ptr);
667
668 return tzd;
669 }
670 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register);
671
672 /**
673 * devm_thermal_zone_of_sensor_unregister - Resource managed version of
674 * thermal_zone_of_sensor_unregister().
675 * @dev: Device for which which resource was allocated.
676 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
677 *
678 * This function removes the sensor callbacks and private data from the
679 * thermal zone device registered with devm_thermal_zone_of_sensor_register()
680 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
681 * thermal zone device callbacks.
682 * Normally this function will not need to be called and the resource
683 * management code will ensure that the resource is freed.
684 */
devm_thermal_zone_of_sensor_unregister(struct device * dev,struct thermal_zone_device * tzd)685 void devm_thermal_zone_of_sensor_unregister(struct device *dev,
686 struct thermal_zone_device *tzd)
687 {
688 WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release,
689 devm_thermal_zone_of_sensor_match, tzd));
690 }
691 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister);
692
693 /*** functions parsing device tree nodes ***/
694
695 /**
696 * thermal_of_populate_bind_params - parse and fill cooling map data
697 * @np: DT node containing a cooling-map node
698 * @__tbp: data structure to be filled with cooling map info
699 * @trips: array of thermal zone trip points
700 * @ntrips: number of trip points inside trips.
701 *
702 * This function parses a cooling-map type of node represented by
703 * @np parameter and fills the read data into @__tbp data structure.
704 * It needs the already parsed array of trip points of the thermal zone
705 * in consideration.
706 *
707 * Return: 0 on success, proper error code otherwise
708 */
thermal_of_populate_bind_params(struct device_node * np,struct __thermal_bind_params * __tbp,struct thermal_trip * trips,int ntrips)709 static int thermal_of_populate_bind_params(struct device_node *np,
710 struct __thermal_bind_params *__tbp,
711 struct thermal_trip *trips,
712 int ntrips)
713 {
714 struct of_phandle_args cooling_spec;
715 struct __thermal_cooling_bind_param *__tcbp;
716 struct device_node *trip;
717 int ret, i, count;
718 u32 prop;
719
720 /* Default weight. Usage is optional */
721 __tbp->usage = THERMAL_WEIGHT_DEFAULT;
722 ret = of_property_read_u32(np, "contribution", &prop);
723 if (ret == 0)
724 __tbp->usage = prop;
725
726 trip = of_parse_phandle(np, "trip", 0);
727 if (!trip) {
728 pr_err("missing trip property\n");
729 return -ENODEV;
730 }
731
732 /* match using device_node */
733 for (i = 0; i < ntrips; i++)
734 if (trip == trips[i].np) {
735 __tbp->trip_id = i;
736 break;
737 }
738
739 if (i == ntrips) {
740 ret = -ENODEV;
741 goto end;
742 }
743
744 count = of_count_phandle_with_args(np, "cooling-device",
745 "#cooling-cells");
746 if (count <= 0) {
747 pr_err("Add a cooling_device property with at least one device\n");
748 ret = -ENOENT;
749 goto end;
750 }
751
752 __tcbp = kcalloc(count, sizeof(*__tcbp), GFP_KERNEL);
753 if (!__tcbp) {
754 ret = -ENOMEM;
755 goto end;
756 }
757
758 for (i = 0; i < count; i++) {
759 ret = of_parse_phandle_with_args(np, "cooling-device",
760 "#cooling-cells", i, &cooling_spec);
761 if (ret < 0) {
762 pr_err("Invalid cooling-device entry\n");
763 goto free_tcbp;
764 }
765
766 __tcbp[i].cooling_device = cooling_spec.np;
767
768 if (cooling_spec.args_count >= 2) { /* at least min and max */
769 __tcbp[i].min = cooling_spec.args[0];
770 __tcbp[i].max = cooling_spec.args[1];
771 } else {
772 pr_err("wrong reference to cooling device, missing limits\n");
773 }
774 }
775
776 __tbp->tcbp = __tcbp;
777 __tbp->count = count;
778
779 goto end;
780
781 free_tcbp:
782 for (i = i - 1; i >= 0; i--)
783 of_node_put(__tcbp[i].cooling_device);
784 kfree(__tcbp);
785 end:
786 of_node_put(trip);
787
788 return ret;
789 }
790
791 /*
792 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
793 * into the device tree binding of 'trip', property type.
794 */
795 static const char * const trip_types[] = {
796 [THERMAL_TRIP_ACTIVE] = "active",
797 [THERMAL_TRIP_PASSIVE] = "passive",
798 [THERMAL_TRIP_HOT] = "hot",
799 [THERMAL_TRIP_CRITICAL] = "critical",
800 };
801
802 /**
803 * thermal_of_get_trip_type - Get phy mode for given device_node
804 * @np: Pointer to the given device_node
805 * @type: Pointer to resulting trip type
806 *
807 * The function gets trip type string from property 'type',
808 * and store its index in trip_types table in @type,
809 *
810 * Return: 0 on success, or errno in error case.
811 */
thermal_of_get_trip_type(struct device_node * np,enum thermal_trip_type * type)812 static int thermal_of_get_trip_type(struct device_node *np,
813 enum thermal_trip_type *type)
814 {
815 const char *t;
816 int err, i;
817
818 err = of_property_read_string(np, "type", &t);
819 if (err < 0)
820 return err;
821
822 for (i = 0; i < ARRAY_SIZE(trip_types); i++)
823 if (!strcasecmp(t, trip_types[i])) {
824 *type = i;
825 return 0;
826 }
827
828 return -ENODEV;
829 }
830
831 /**
832 * thermal_of_populate_trip - parse and fill one trip point data
833 * @np: DT node containing a trip point node
834 * @trip: trip point data structure to be filled up
835 *
836 * This function parses a trip point type of node represented by
837 * @np parameter and fills the read data into @trip data structure.
838 *
839 * Return: 0 on success, proper error code otherwise
840 */
thermal_of_populate_trip(struct device_node * np,struct thermal_trip * trip)841 static int thermal_of_populate_trip(struct device_node *np,
842 struct thermal_trip *trip)
843 {
844 int prop;
845 int ret;
846
847 ret = of_property_read_u32(np, "temperature", &prop);
848 if (ret < 0) {
849 pr_err("missing temperature property\n");
850 return ret;
851 }
852 trip->temperature = prop;
853
854 ret = of_property_read_u32(np, "hysteresis", &prop);
855 if (ret < 0) {
856 pr_err("missing hysteresis property\n");
857 return ret;
858 }
859 trip->hysteresis = prop;
860
861 ret = thermal_of_get_trip_type(np, &trip->type);
862 if (ret < 0) {
863 pr_err("wrong trip type property\n");
864 return ret;
865 }
866
867 /* Required for cooling map matching */
868 trip->np = np;
869 of_node_get(np);
870
871 return 0;
872 }
873
874 /**
875 * thermal_of_build_thermal_zone - parse and fill one thermal zone data
876 * @np: DT node containing a thermal zone node
877 *
878 * This function parses a thermal zone type of node represented by
879 * @np parameter and fills the read data into a __thermal_zone data structure
880 * and return this pointer.
881 *
882 * TODO: Missing properties to parse: thermal-sensor-names
883 *
884 * Return: On success returns a valid struct __thermal_zone,
885 * otherwise, it returns a corresponding ERR_PTR(). Caller must
886 * check the return value with help of IS_ERR() helper.
887 */
888 static struct __thermal_zone
thermal_of_build_thermal_zone(struct device_node * np)889 __init *thermal_of_build_thermal_zone(struct device_node *np)
890 {
891 struct device_node *child = NULL, *gchild;
892 struct __thermal_zone *tz;
893 int ret, i;
894 u32 prop, coef[2];
895
896 if (!np) {
897 pr_err("no thermal zone np\n");
898 return ERR_PTR(-EINVAL);
899 }
900
901 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
902 if (!tz)
903 return ERR_PTR(-ENOMEM);
904
905 ret = of_property_read_u32(np, "polling-delay-passive", &prop);
906 if (ret < 0) {
907 pr_err("%pOFn: missing polling-delay-passive property\n", np);
908 goto free_tz;
909 }
910 tz->passive_delay = prop;
911
912 ret = of_property_read_u32(np, "polling-delay", &prop);
913 if (ret < 0) {
914 pr_err("%pOFn: missing polling-delay property\n", np);
915 goto free_tz;
916 }
917 tz->polling_delay = prop;
918
919 /*
920 * REVIST: for now, the thermal framework supports only
921 * one sensor per thermal zone. Thus, we are considering
922 * only the first two values as slope and offset.
923 */
924 ret = of_property_read_u32_array(np, "coefficients", coef, 2);
925 if (ret == 0) {
926 tz->slope = coef[0];
927 tz->offset = coef[1];
928 } else {
929 tz->slope = 1;
930 tz->offset = 0;
931 }
932
933 /* trips */
934 child = of_get_child_by_name(np, "trips");
935
936 /* No trips provided */
937 if (!child)
938 goto finish;
939
940 tz->ntrips = of_get_child_count(child);
941 if (tz->ntrips == 0) /* must have at least one child */
942 goto finish;
943
944 tz->trips = kcalloc(tz->ntrips, sizeof(*tz->trips), GFP_KERNEL);
945 if (!tz->trips) {
946 ret = -ENOMEM;
947 goto free_tz;
948 }
949
950 i = 0;
951 for_each_child_of_node(child, gchild) {
952 ret = thermal_of_populate_trip(gchild, &tz->trips[i++]);
953 if (ret)
954 goto free_trips;
955 }
956
957 of_node_put(child);
958
959 /* cooling-maps */
960 child = of_get_child_by_name(np, "cooling-maps");
961
962 /* cooling-maps not provided */
963 if (!child)
964 goto finish;
965
966 tz->num_tbps = of_get_child_count(child);
967 if (tz->num_tbps == 0)
968 goto finish;
969
970 tz->tbps = kcalloc(tz->num_tbps, sizeof(*tz->tbps), GFP_KERNEL);
971 if (!tz->tbps) {
972 ret = -ENOMEM;
973 goto free_trips;
974 }
975
976 i = 0;
977 for_each_child_of_node(child, gchild) {
978 ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++],
979 tz->trips, tz->ntrips);
980 if (ret)
981 goto free_tbps;
982 }
983
984 finish:
985 of_node_put(child);
986
987 return tz;
988
989 free_tbps:
990 for (i = i - 1; i >= 0; i--) {
991 struct __thermal_bind_params *tbp = tz->tbps + i;
992 int j;
993
994 for (j = 0; j < tbp->count; j++)
995 of_node_put(tbp->tcbp[j].cooling_device);
996
997 kfree(tbp->tcbp);
998 }
999
1000 kfree(tz->tbps);
1001 free_trips:
1002 for (i = 0; i < tz->ntrips; i++)
1003 of_node_put(tz->trips[i].np);
1004 kfree(tz->trips);
1005 of_node_put(gchild);
1006 free_tz:
1007 kfree(tz);
1008 of_node_put(child);
1009
1010 return ERR_PTR(ret);
1011 }
1012
of_thermal_free_zone(struct __thermal_zone * tz)1013 static __init void of_thermal_free_zone(struct __thermal_zone *tz)
1014 {
1015 struct __thermal_bind_params *tbp;
1016 int i, j;
1017
1018 for (i = 0; i < tz->num_tbps; i++) {
1019 tbp = tz->tbps + i;
1020
1021 for (j = 0; j < tbp->count; j++)
1022 of_node_put(tbp->tcbp[j].cooling_device);
1023
1024 kfree(tbp->tcbp);
1025 }
1026
1027 kfree(tz->tbps);
1028 for (i = 0; i < tz->ntrips; i++)
1029 of_node_put(tz->trips[i].np);
1030 kfree(tz->trips);
1031 kfree(tz);
1032 }
1033
1034 /**
1035 * of_thermal_destroy_zones - remove all zones parsed and allocated resources
1036 *
1037 * Finds all zones parsed and added to the thermal framework and remove them
1038 * from the system, together with their resources.
1039 *
1040 */
of_thermal_destroy_zones(void)1041 static __init void of_thermal_destroy_zones(void)
1042 {
1043 struct device_node *np, *child;
1044
1045 np = of_find_node_by_name(NULL, "thermal-zones");
1046 if (!np) {
1047 pr_debug("unable to find thermal zones\n");
1048 return;
1049 }
1050
1051 for_each_available_child_of_node(np, child) {
1052 struct thermal_zone_device *zone;
1053
1054 zone = thermal_zone_get_zone_by_name(child->name);
1055 if (IS_ERR(zone))
1056 continue;
1057
1058 thermal_zone_device_unregister(zone);
1059 kfree(zone->tzp);
1060 kfree(zone->ops);
1061 of_thermal_free_zone(zone->devdata);
1062 }
1063 of_node_put(np);
1064 }
1065
1066 /**
1067 * of_parse_thermal_zones - parse device tree thermal data
1068 *
1069 * Initialization function that can be called by machine initialization
1070 * code to parse thermal data and populate the thermal framework
1071 * with hardware thermal zones info. This function only parses thermal zones.
1072 * Cooling devices and sensor devices nodes are supposed to be parsed
1073 * by their respective drivers.
1074 *
1075 * Return: 0 on success, proper error code otherwise
1076 *
1077 */
of_parse_thermal_zones(void)1078 int __init of_parse_thermal_zones(void)
1079 {
1080 struct device_node *np, *child;
1081 struct __thermal_zone *tz;
1082 struct thermal_zone_device_ops *ops;
1083
1084 np = of_find_node_by_name(NULL, "thermal-zones");
1085 if (!np) {
1086 pr_debug("unable to find thermal zones\n");
1087 return 0; /* Run successfully on systems without thermal DT */
1088 }
1089
1090 for_each_available_child_of_node(np, child) {
1091 struct thermal_zone_device *zone;
1092 struct thermal_zone_params *tzp;
1093 int i, mask = 0;
1094 u32 prop;
1095
1096 tz = thermal_of_build_thermal_zone(child);
1097 if (IS_ERR(tz)) {
1098 pr_err("failed to build thermal zone %pOFn: %ld\n",
1099 child,
1100 PTR_ERR(tz));
1101 continue;
1102 }
1103
1104 ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL);
1105 if (!ops)
1106 goto exit_free;
1107
1108 tzp = kzalloc(sizeof(*tzp), GFP_KERNEL);
1109 if (!tzp) {
1110 kfree(ops);
1111 goto exit_free;
1112 }
1113
1114 /* No hwmon because there might be hwmon drivers registering */
1115 tzp->no_hwmon = true;
1116
1117 if (!of_property_read_u32(child, "sustainable-power", &prop))
1118 tzp->sustainable_power = prop;
1119
1120 for (i = 0; i < tz->ntrips; i++)
1121 mask |= 1 << i;
1122
1123 /* these two are left for temperature drivers to use */
1124 tzp->slope = tz->slope;
1125 tzp->offset = tz->offset;
1126
1127 zone = thermal_zone_device_register(child->name, tz->ntrips,
1128 mask, tz,
1129 ops, tzp,
1130 tz->passive_delay,
1131 tz->polling_delay);
1132 if (IS_ERR(zone)) {
1133 pr_err("Failed to build %pOFn zone %ld\n", child,
1134 PTR_ERR(zone));
1135 kfree(tzp);
1136 kfree(ops);
1137 of_thermal_free_zone(tz);
1138 /* attempting to build remaining zones still */
1139 }
1140 }
1141 of_node_put(np);
1142
1143 return 0;
1144
1145 exit_free:
1146 of_node_put(child);
1147 of_node_put(np);
1148 of_thermal_free_zone(tz);
1149
1150 /* no memory available, so free what we have built */
1151 of_thermal_destroy_zones();
1152
1153 return -ENOMEM;
1154 }
1155