1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * thermal.c - Generic Thermal Management Sysfs support.
4 *
5 * Copyright (C) 2008 Intel Corp
6 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
7 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
8 */
9
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/export.h>
15 #include <linux/slab.h>
16 #include <linux/kdev_t.h>
17 #include <linux/idr.h>
18 #include <linux/thermal.h>
19 #include <linux/reboot.h>
20 #include <linux/string.h>
21 #include <linux/of.h>
22 #include <linux/suspend.h>
23
24 #define CREATE_TRACE_POINTS
25 #include <trace/events/thermal.h>
26
27 #include "thermal_core.h"
28 #include "thermal_hwmon.h"
29
30 static DEFINE_IDA(thermal_tz_ida);
31 static DEFINE_IDA(thermal_cdev_ida);
32
33 static LIST_HEAD(thermal_tz_list);
34 static LIST_HEAD(thermal_cdev_list);
35 static LIST_HEAD(thermal_governor_list);
36
37 static DEFINE_MUTEX(thermal_list_lock);
38 static DEFINE_MUTEX(thermal_governor_lock);
39 static DEFINE_MUTEX(poweroff_lock);
40
41 static atomic_t in_suspend;
42 static bool power_off_triggered;
43
44 static struct thermal_governor *def_governor;
45
46 /*
47 * Governor section: set of functions to handle thermal governors
48 *
49 * Functions to help in the life cycle of thermal governors within
50 * the thermal core and by the thermal governor code.
51 */
52
__find_governor(const char * name)53 static struct thermal_governor *__find_governor(const char *name)
54 {
55 struct thermal_governor *pos;
56
57 if (!name || !name[0])
58 return def_governor;
59
60 list_for_each_entry(pos, &thermal_governor_list, governor_list)
61 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
62 return pos;
63
64 return NULL;
65 }
66
67 /**
68 * bind_previous_governor() - bind the previous governor of the thermal zone
69 * @tz: a valid pointer to a struct thermal_zone_device
70 * @failed_gov_name: the name of the governor that failed to register
71 *
72 * Register the previous governor of the thermal zone after a new
73 * governor has failed to be bound.
74 */
bind_previous_governor(struct thermal_zone_device * tz,const char * failed_gov_name)75 static void bind_previous_governor(struct thermal_zone_device *tz,
76 const char *failed_gov_name)
77 {
78 if (tz->governor && tz->governor->bind_to_tz) {
79 if (tz->governor->bind_to_tz(tz)) {
80 dev_err(&tz->device,
81 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
82 failed_gov_name, tz->governor->name, tz->type);
83 tz->governor = NULL;
84 }
85 }
86 }
87
88 /**
89 * thermal_set_governor() - Switch to another governor
90 * @tz: a valid pointer to a struct thermal_zone_device
91 * @new_gov: pointer to the new governor
92 *
93 * Change the governor of thermal zone @tz.
94 *
95 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
96 */
thermal_set_governor(struct thermal_zone_device * tz,struct thermal_governor * new_gov)97 static int thermal_set_governor(struct thermal_zone_device *tz,
98 struct thermal_governor *new_gov)
99 {
100 int ret = 0;
101
102 if (tz->governor && tz->governor->unbind_from_tz)
103 tz->governor->unbind_from_tz(tz);
104
105 if (new_gov && new_gov->bind_to_tz) {
106 ret = new_gov->bind_to_tz(tz);
107 if (ret) {
108 bind_previous_governor(tz, new_gov->name);
109
110 return ret;
111 }
112 }
113
114 tz->governor = new_gov;
115
116 return ret;
117 }
118
thermal_register_governor(struct thermal_governor * governor)119 int thermal_register_governor(struct thermal_governor *governor)
120 {
121 int err;
122 const char *name;
123 struct thermal_zone_device *pos;
124
125 if (!governor)
126 return -EINVAL;
127
128 mutex_lock(&thermal_governor_lock);
129
130 err = -EBUSY;
131 if (!__find_governor(governor->name)) {
132 bool match_default;
133
134 err = 0;
135 list_add(&governor->governor_list, &thermal_governor_list);
136 match_default = !strncmp(governor->name,
137 DEFAULT_THERMAL_GOVERNOR,
138 THERMAL_NAME_LENGTH);
139
140 if (!def_governor && match_default)
141 def_governor = governor;
142 }
143
144 mutex_lock(&thermal_list_lock);
145
146 list_for_each_entry(pos, &thermal_tz_list, node) {
147 /*
148 * only thermal zones with specified tz->tzp->governor_name
149 * may run with tz->govenor unset
150 */
151 if (pos->governor)
152 continue;
153
154 name = pos->tzp->governor_name;
155
156 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
157 int ret;
158
159 ret = thermal_set_governor(pos, governor);
160 if (ret)
161 dev_err(&pos->device,
162 "Failed to set governor %s for thermal zone %s: %d\n",
163 governor->name, pos->type, ret);
164 }
165 }
166
167 mutex_unlock(&thermal_list_lock);
168 mutex_unlock(&thermal_governor_lock);
169
170 return err;
171 }
172
thermal_unregister_governor(struct thermal_governor * governor)173 void thermal_unregister_governor(struct thermal_governor *governor)
174 {
175 struct thermal_zone_device *pos;
176
177 if (!governor)
178 return;
179
180 mutex_lock(&thermal_governor_lock);
181
182 if (!__find_governor(governor->name))
183 goto exit;
184
185 mutex_lock(&thermal_list_lock);
186
187 list_for_each_entry(pos, &thermal_tz_list, node) {
188 if (!strncasecmp(pos->governor->name, governor->name,
189 THERMAL_NAME_LENGTH))
190 thermal_set_governor(pos, NULL);
191 }
192
193 mutex_unlock(&thermal_list_lock);
194 list_del(&governor->governor_list);
195 exit:
196 mutex_unlock(&thermal_governor_lock);
197 }
198
thermal_zone_device_set_policy(struct thermal_zone_device * tz,char * policy)199 int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
200 char *policy)
201 {
202 struct thermal_governor *gov;
203 int ret = -EINVAL;
204
205 mutex_lock(&thermal_governor_lock);
206 mutex_lock(&tz->lock);
207
208 gov = __find_governor(strim(policy));
209 if (!gov)
210 goto exit;
211
212 ret = thermal_set_governor(tz, gov);
213
214 exit:
215 mutex_unlock(&tz->lock);
216 mutex_unlock(&thermal_governor_lock);
217
218 thermal_notify_tz_gov_change(tz->id, policy);
219
220 return ret;
221 }
222
thermal_build_list_of_policies(char * buf)223 int thermal_build_list_of_policies(char *buf)
224 {
225 struct thermal_governor *pos;
226 ssize_t count = 0;
227
228 mutex_lock(&thermal_governor_lock);
229
230 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
231 count += scnprintf(buf + count, PAGE_SIZE - count, "%s ",
232 pos->name);
233 }
234 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
235
236 mutex_unlock(&thermal_governor_lock);
237
238 return count;
239 }
240
thermal_unregister_governors(void)241 static void __init thermal_unregister_governors(void)
242 {
243 struct thermal_governor **governor;
244
245 for_each_governor_table(governor)
246 thermal_unregister_governor(*governor);
247 }
248
thermal_register_governors(void)249 static int __init thermal_register_governors(void)
250 {
251 int ret = 0;
252 struct thermal_governor **governor;
253
254 for_each_governor_table(governor) {
255 ret = thermal_register_governor(*governor);
256 if (ret) {
257 pr_err("Failed to register governor: '%s'",
258 (*governor)->name);
259 break;
260 }
261
262 pr_info("Registered thermal governor '%s'",
263 (*governor)->name);
264 }
265
266 if (ret) {
267 struct thermal_governor **gov;
268
269 for_each_governor_table(gov) {
270 if (gov == governor)
271 break;
272 thermal_unregister_governor(*gov);
273 }
274 }
275
276 return ret;
277 }
278
279 /*
280 * Zone update section: main control loop applied to each zone while monitoring
281 *
282 * in polling mode. The monitoring is done using a workqueue.
283 * Same update may be done on a zone by calling thermal_zone_device_update().
284 *
285 * An update means:
286 * - Non-critical trips will invoke the governor responsible for that zone;
287 * - Hot trips will produce a notification to userspace;
288 * - Critical trip point will cause a system shutdown.
289 */
thermal_zone_device_set_polling(struct thermal_zone_device * tz,int delay)290 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
291 int delay)
292 {
293 if (delay > 1000)
294 mod_delayed_work(system_freezable_power_efficient_wq,
295 &tz->poll_queue,
296 round_jiffies(msecs_to_jiffies(delay)));
297 else if (delay)
298 mod_delayed_work(system_freezable_power_efficient_wq,
299 &tz->poll_queue,
300 msecs_to_jiffies(delay));
301 else
302 cancel_delayed_work(&tz->poll_queue);
303 }
304
should_stop_polling(struct thermal_zone_device * tz)305 static inline bool should_stop_polling(struct thermal_zone_device *tz)
306 {
307 return !thermal_zone_device_is_enabled(tz);
308 }
309
monitor_thermal_zone(struct thermal_zone_device * tz)310 static void monitor_thermal_zone(struct thermal_zone_device *tz)
311 {
312 bool stop;
313
314 stop = should_stop_polling(tz);
315
316 mutex_lock(&tz->lock);
317
318 if (!stop && tz->passive)
319 thermal_zone_device_set_polling(tz, tz->passive_delay);
320 else if (!stop && tz->polling_delay)
321 thermal_zone_device_set_polling(tz, tz->polling_delay);
322 else
323 thermal_zone_device_set_polling(tz, 0);
324
325 mutex_unlock(&tz->lock);
326 }
327
handle_non_critical_trips(struct thermal_zone_device * tz,int trip)328 static void handle_non_critical_trips(struct thermal_zone_device *tz, int trip)
329 {
330 tz->governor ? tz->governor->throttle(tz, trip) :
331 def_governor->throttle(tz, trip);
332 }
333
334 /**
335 * thermal_emergency_poweroff_func - emergency poweroff work after a known delay
336 * @work: work_struct associated with the emergency poweroff function
337 *
338 * This function is called in very critical situations to force
339 * a kernel poweroff after a configurable timeout value.
340 */
thermal_emergency_poweroff_func(struct work_struct * work)341 static void thermal_emergency_poweroff_func(struct work_struct *work)
342 {
343 /*
344 * We have reached here after the emergency thermal shutdown
345 * Waiting period has expired. This means orderly_poweroff has
346 * not been able to shut off the system for some reason.
347 * Try to shut down the system immediately using kernel_power_off
348 * if populated
349 */
350 WARN(1, "Attempting kernel_power_off: Temperature too high\n");
351 kernel_power_off();
352
353 /*
354 * Worst of the worst case trigger emergency restart
355 */
356 WARN(1, "Attempting emergency_restart: Temperature too high\n");
357 emergency_restart();
358 }
359
360 static DECLARE_DELAYED_WORK(thermal_emergency_poweroff_work,
361 thermal_emergency_poweroff_func);
362
363 /**
364 * thermal_emergency_poweroff - Trigger an emergency system poweroff
365 *
366 * This may be called from any critical situation to trigger a system shutdown
367 * after a known period of time. By default this is not scheduled.
368 */
thermal_emergency_poweroff(void)369 static void thermal_emergency_poweroff(void)
370 {
371 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
372 /*
373 * poweroff_delay_ms must be a carefully profiled positive value.
374 * Its a must for thermal_emergency_poweroff_work to be scheduled
375 */
376 if (poweroff_delay_ms <= 0)
377 return;
378 schedule_delayed_work(&thermal_emergency_poweroff_work,
379 msecs_to_jiffies(poweroff_delay_ms));
380 }
381
handle_critical_trips(struct thermal_zone_device * tz,int trip,enum thermal_trip_type trip_type)382 static void handle_critical_trips(struct thermal_zone_device *tz,
383 int trip, enum thermal_trip_type trip_type)
384 {
385 int trip_temp;
386
387 tz->ops->get_trip_temp(tz, trip, &trip_temp);
388
389 /* If we have not crossed the trip_temp, we do not care. */
390 if (trip_temp <= 0 || tz->temperature < trip_temp)
391 return;
392
393 trace_thermal_zone_trip(tz, trip, trip_type);
394
395 if (tz->ops->notify)
396 tz->ops->notify(tz, trip, trip_type);
397
398 if (trip_type == THERMAL_TRIP_CRITICAL) {
399 dev_emerg(&tz->device,
400 "critical temperature reached (%d C), shutting down\n",
401 tz->temperature / 1000);
402 mutex_lock(&poweroff_lock);
403 if (!power_off_triggered) {
404 /*
405 * Queue a backup emergency shutdown in the event of
406 * orderly_poweroff failure
407 */
408 thermal_emergency_poweroff();
409 orderly_poweroff(true);
410 power_off_triggered = true;
411 }
412 mutex_unlock(&poweroff_lock);
413 }
414 }
415
handle_thermal_trip(struct thermal_zone_device * tz,int trip)416 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
417 {
418 enum thermal_trip_type type;
419 int trip_temp, hyst = 0;
420
421 /* Ignore disabled trip points */
422 if (test_bit(trip, &tz->trips_disabled))
423 return;
424
425 tz->ops->get_trip_temp(tz, trip, &trip_temp);
426 tz->ops->get_trip_type(tz, trip, &type);
427 if (tz->ops->get_trip_hyst)
428 tz->ops->get_trip_hyst(tz, trip, &hyst);
429
430 if (tz->last_temperature != THERMAL_TEMP_INVALID) {
431 if (tz->last_temperature < trip_temp &&
432 tz->temperature >= trip_temp)
433 thermal_notify_tz_trip_up(tz->id, trip);
434 if (tz->last_temperature >= trip_temp &&
435 tz->temperature < (trip_temp - hyst))
436 thermal_notify_tz_trip_down(tz->id, trip);
437 }
438
439 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
440 handle_critical_trips(tz, trip, type);
441 else
442 handle_non_critical_trips(tz, trip);
443 /*
444 * Alright, we handled this trip successfully.
445 * So, start monitoring again.
446 */
447 monitor_thermal_zone(tz);
448 }
449
update_temperature(struct thermal_zone_device * tz)450 static void update_temperature(struct thermal_zone_device *tz)
451 {
452 int temp, ret;
453
454 ret = thermal_zone_get_temp(tz, &temp);
455 if (ret) {
456 if (ret != -EAGAIN)
457 dev_warn(&tz->device,
458 "failed to read out thermal zone (%d)\n",
459 ret);
460 return;
461 }
462
463 mutex_lock(&tz->lock);
464 tz->last_temperature = tz->temperature;
465 tz->temperature = temp;
466 mutex_unlock(&tz->lock);
467
468 trace_thermal_temperature(tz);
469
470 thermal_genl_sampling_temp(tz->id, temp);
471 }
472
thermal_zone_device_init(struct thermal_zone_device * tz)473 static void thermal_zone_device_init(struct thermal_zone_device *tz)
474 {
475 struct thermal_instance *pos;
476 tz->temperature = THERMAL_TEMP_INVALID;
477 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
478 pos->initialized = false;
479 }
480
thermal_zone_device_reset(struct thermal_zone_device * tz)481 static void thermal_zone_device_reset(struct thermal_zone_device *tz)
482 {
483 tz->passive = 0;
484 thermal_zone_device_init(tz);
485 }
486
thermal_zone_device_set_mode(struct thermal_zone_device * tz,enum thermal_device_mode mode)487 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
488 enum thermal_device_mode mode)
489 {
490 int ret = 0;
491
492 mutex_lock(&tz->lock);
493
494 /* do nothing if mode isn't changing */
495 if (mode == tz->mode) {
496 mutex_unlock(&tz->lock);
497
498 return ret;
499 }
500
501 if (tz->ops->change_mode)
502 ret = tz->ops->change_mode(tz, mode);
503
504 if (!ret)
505 tz->mode = mode;
506
507 mutex_unlock(&tz->lock);
508
509 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
510
511 if (mode == THERMAL_DEVICE_ENABLED)
512 thermal_notify_tz_enable(tz->id);
513 else
514 thermal_notify_tz_disable(tz->id);
515
516 return ret;
517 }
518
thermal_zone_device_enable(struct thermal_zone_device * tz)519 int thermal_zone_device_enable(struct thermal_zone_device *tz)
520 {
521 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
522 }
523 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
524
thermal_zone_device_disable(struct thermal_zone_device * tz)525 int thermal_zone_device_disable(struct thermal_zone_device *tz)
526 {
527 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
528 }
529 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
530
thermal_zone_device_is_enabled(struct thermal_zone_device * tz)531 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
532 {
533 enum thermal_device_mode mode;
534
535 mutex_lock(&tz->lock);
536
537 mode = tz->mode;
538
539 mutex_unlock(&tz->lock);
540
541 return mode == THERMAL_DEVICE_ENABLED;
542 }
543
thermal_zone_device_update(struct thermal_zone_device * tz,enum thermal_notify_event event)544 void thermal_zone_device_update(struct thermal_zone_device *tz,
545 enum thermal_notify_event event)
546 {
547 int count;
548
549 if (should_stop_polling(tz))
550 return;
551
552 if (atomic_read(&in_suspend))
553 return;
554
555 if (!tz->ops->get_temp)
556 return;
557
558 update_temperature(tz);
559
560 thermal_zone_set_trips(tz);
561
562 tz->notify_event = event;
563
564 for (count = 0; count < tz->trips; count++)
565 handle_thermal_trip(tz, count);
566 }
567 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
568
569 /**
570 * thermal_notify_framework - Sensor drivers use this API to notify framework
571 * @tz: thermal zone device
572 * @trip: indicates which trip point has been crossed
573 *
574 * This function handles the trip events from sensor drivers. It starts
575 * throttling the cooling devices according to the policy configured.
576 * For CRITICAL and HOT trip points, this notifies the respective drivers,
577 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
578 * The throttling policy is based on the configured platform data; if no
579 * platform data is provided, this uses the step_wise throttling policy.
580 */
thermal_notify_framework(struct thermal_zone_device * tz,int trip)581 void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
582 {
583 handle_thermal_trip(tz, trip);
584 }
585 EXPORT_SYMBOL_GPL(thermal_notify_framework);
586
thermal_zone_device_check(struct work_struct * work)587 static void thermal_zone_device_check(struct work_struct *work)
588 {
589 struct thermal_zone_device *tz = container_of(work, struct
590 thermal_zone_device,
591 poll_queue.work);
592 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
593 }
594
595 /*
596 * Power actor section: interface to power actors to estimate power
597 *
598 * Set of functions used to interact to cooling devices that know
599 * how to estimate their devices power consumption.
600 */
601
602 /**
603 * power_actor_get_max_power() - get the maximum power that a cdev can consume
604 * @cdev: pointer to &thermal_cooling_device
605 * @max_power: pointer in which to store the maximum power
606 *
607 * Calculate the maximum power consumption in milliwats that the
608 * cooling device can currently consume and store it in @max_power.
609 *
610 * Return: 0 on success, -EINVAL if @cdev doesn't support the
611 * power_actor API or -E* on other error.
612 */
power_actor_get_max_power(struct thermal_cooling_device * cdev,u32 * max_power)613 int power_actor_get_max_power(struct thermal_cooling_device *cdev,
614 u32 *max_power)
615 {
616 if (!cdev_is_power_actor(cdev))
617 return -EINVAL;
618
619 return cdev->ops->state2power(cdev, 0, max_power);
620 }
621
622 /**
623 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
624 * @cdev: pointer to &thermal_cooling_device
625 * @min_power: pointer in which to store the minimum power
626 *
627 * Calculate the minimum power consumption in milliwatts that the
628 * cooling device can currently consume and store it in @min_power.
629 *
630 * Return: 0 on success, -EINVAL if @cdev doesn't support the
631 * power_actor API or -E* on other error.
632 */
power_actor_get_min_power(struct thermal_cooling_device * cdev,u32 * min_power)633 int power_actor_get_min_power(struct thermal_cooling_device *cdev,
634 u32 *min_power)
635 {
636 unsigned long max_state;
637 int ret;
638
639 if (!cdev_is_power_actor(cdev))
640 return -EINVAL;
641
642 ret = cdev->ops->get_max_state(cdev, &max_state);
643 if (ret)
644 return ret;
645
646 return cdev->ops->state2power(cdev, max_state, min_power);
647 }
648
649 /**
650 * power_actor_set_power() - limit the maximum power a cooling device consumes
651 * @cdev: pointer to &thermal_cooling_device
652 * @instance: thermal instance to update
653 * @power: the power in milliwatts
654 *
655 * Set the cooling device to consume at most @power milliwatts. The limit is
656 * expected to be a cap at the maximum power consumption.
657 *
658 * Return: 0 on success, -EINVAL if the cooling device does not
659 * implement the power actor API or -E* for other failures.
660 */
power_actor_set_power(struct thermal_cooling_device * cdev,struct thermal_instance * instance,u32 power)661 int power_actor_set_power(struct thermal_cooling_device *cdev,
662 struct thermal_instance *instance, u32 power)
663 {
664 unsigned long state;
665 int ret;
666
667 if (!cdev_is_power_actor(cdev))
668 return -EINVAL;
669
670 ret = cdev->ops->power2state(cdev, power, &state);
671 if (ret)
672 return ret;
673
674 instance->target = state;
675 mutex_lock(&cdev->lock);
676 cdev->updated = false;
677 mutex_unlock(&cdev->lock);
678 thermal_cdev_update(cdev);
679
680 return 0;
681 }
682
thermal_zone_device_rebind_exception(struct thermal_zone_device * tz,const char * cdev_type,size_t size)683 void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz,
684 const char *cdev_type, size_t size)
685 {
686 struct thermal_cooling_device *cdev = NULL;
687
688 mutex_lock(&thermal_list_lock);
689 list_for_each_entry(cdev, &thermal_cdev_list, node) {
690 /* skip non matching cdevs */
691 if (strncmp(cdev_type, cdev->type, size))
692 continue;
693
694 /* re binding the exception matching the type pattern */
695 thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev,
696 THERMAL_NO_LIMIT,
697 THERMAL_NO_LIMIT,
698 THERMAL_WEIGHT_DEFAULT);
699 }
700 mutex_unlock(&thermal_list_lock);
701 }
702
for_each_thermal_governor(int (* cb)(struct thermal_governor *,void *),void * data)703 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
704 void *data)
705 {
706 struct thermal_governor *gov;
707 int ret = 0;
708
709 mutex_lock(&thermal_governor_lock);
710 list_for_each_entry(gov, &thermal_governor_list, governor_list) {
711 ret = cb(gov, data);
712 if (ret)
713 break;
714 }
715 mutex_unlock(&thermal_governor_lock);
716
717 return ret;
718 }
719
for_each_thermal_cooling_device(int (* cb)(struct thermal_cooling_device *,void *),void * data)720 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
721 void *), void *data)
722 {
723 struct thermal_cooling_device *cdev;
724 int ret = 0;
725
726 mutex_lock(&thermal_list_lock);
727 list_for_each_entry(cdev, &thermal_cdev_list, node) {
728 ret = cb(cdev, data);
729 if (ret)
730 break;
731 }
732 mutex_unlock(&thermal_list_lock);
733
734 return ret;
735 }
736
for_each_thermal_zone(int (* cb)(struct thermal_zone_device *,void *),void * data)737 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
738 void *data)
739 {
740 struct thermal_zone_device *tz;
741 int ret = 0;
742
743 mutex_lock(&thermal_list_lock);
744 list_for_each_entry(tz, &thermal_tz_list, node) {
745 ret = cb(tz, data);
746 if (ret)
747 break;
748 }
749 mutex_unlock(&thermal_list_lock);
750
751 return ret;
752 }
753
thermal_zone_get_by_id(int id)754 struct thermal_zone_device *thermal_zone_get_by_id(int id)
755 {
756 struct thermal_zone_device *tz, *match = NULL;
757
758 mutex_lock(&thermal_list_lock);
759 list_for_each_entry(tz, &thermal_tz_list, node) {
760 if (tz->id == id) {
761 match = tz;
762 break;
763 }
764 }
765 mutex_unlock(&thermal_list_lock);
766
767 return match;
768 }
769
thermal_zone_device_unbind_exception(struct thermal_zone_device * tz,const char * cdev_type,size_t size)770 void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
771 const char *cdev_type, size_t size)
772 {
773 struct thermal_cooling_device *cdev = NULL;
774
775 mutex_lock(&thermal_list_lock);
776 list_for_each_entry(cdev, &thermal_cdev_list, node) {
777 /* skip non matching cdevs */
778 if (strncmp(cdev_type, cdev->type, size))
779 continue;
780 /* unbinding the exception matching the type pattern */
781 thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE,
782 cdev);
783 }
784 mutex_unlock(&thermal_list_lock);
785 }
786
787 /*
788 * Device management section: cooling devices, zones devices, and binding
789 *
790 * Set of functions provided by the thermal core for:
791 * - cooling devices lifecycle: registration, unregistration,
792 * binding, and unbinding.
793 * - thermal zone devices lifecycle: registration, unregistration,
794 * binding, and unbinding.
795 */
796
797 /**
798 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
799 * @tz: pointer to struct thermal_zone_device
800 * @trip: indicates which trip point the cooling devices is
801 * associated with in this thermal zone.
802 * @cdev: pointer to struct thermal_cooling_device
803 * @upper: the Maximum cooling state for this trip point.
804 * THERMAL_NO_LIMIT means no upper limit,
805 * and the cooling device can be in max_state.
806 * @lower: the Minimum cooling state can be used for this trip point.
807 * THERMAL_NO_LIMIT means no lower limit,
808 * and the cooling device can be in cooling state 0.
809 * @weight: The weight of the cooling device to be bound to the
810 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
811 * default value
812 *
813 * This interface function bind a thermal cooling device to the certain trip
814 * point of a thermal zone device.
815 * This function is usually called in the thermal zone device .bind callback.
816 *
817 * Return: 0 on success, the proper error value otherwise.
818 */
thermal_zone_bind_cooling_device(struct thermal_zone_device * tz,int trip,struct thermal_cooling_device * cdev,unsigned long upper,unsigned long lower,unsigned int weight)819 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
820 int trip,
821 struct thermal_cooling_device *cdev,
822 unsigned long upper, unsigned long lower,
823 unsigned int weight)
824 {
825 struct thermal_instance *dev;
826 struct thermal_instance *pos;
827 struct thermal_zone_device *pos1;
828 struct thermal_cooling_device *pos2;
829 unsigned long max_state;
830 int result, ret;
831
832 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
833 return -EINVAL;
834
835 list_for_each_entry(pos1, &thermal_tz_list, node) {
836 if (pos1 == tz)
837 break;
838 }
839 list_for_each_entry(pos2, &thermal_cdev_list, node) {
840 if (pos2 == cdev)
841 break;
842 }
843
844 if (tz != pos1 || cdev != pos2)
845 return -EINVAL;
846
847 ret = cdev->ops->get_max_state(cdev, &max_state);
848 if (ret)
849 return ret;
850
851 /* lower default 0, upper default max_state */
852 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
853 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
854
855 if (lower > upper || upper > max_state)
856 return -EINVAL;
857
858 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
859 if (!dev)
860 return -ENOMEM;
861 dev->tz = tz;
862 dev->cdev = cdev;
863 dev->trip = trip;
864 dev->upper = upper;
865 dev->lower = lower;
866 dev->target = THERMAL_NO_TARGET;
867 dev->weight = weight;
868
869 result = ida_simple_get(&tz->ida, 0, 0, GFP_KERNEL);
870 if (result < 0)
871 goto free_mem;
872
873 dev->id = result;
874 sprintf(dev->name, "cdev%d", dev->id);
875 result =
876 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
877 if (result)
878 goto release_ida;
879
880 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
881 sysfs_attr_init(&dev->attr.attr);
882 dev->attr.attr.name = dev->attr_name;
883 dev->attr.attr.mode = 0444;
884 dev->attr.show = trip_point_show;
885 result = device_create_file(&tz->device, &dev->attr);
886 if (result)
887 goto remove_symbol_link;
888
889 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
890 sysfs_attr_init(&dev->weight_attr.attr);
891 dev->weight_attr.attr.name = dev->weight_attr_name;
892 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
893 dev->weight_attr.show = weight_show;
894 dev->weight_attr.store = weight_store;
895 result = device_create_file(&tz->device, &dev->weight_attr);
896 if (result)
897 goto remove_trip_file;
898
899 mutex_lock(&tz->lock);
900 mutex_lock(&cdev->lock);
901 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
902 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
903 result = -EEXIST;
904 break;
905 }
906 if (!result) {
907 list_add_tail(&dev->tz_node, &tz->thermal_instances);
908 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
909 atomic_set(&tz->need_update, 1);
910 }
911 mutex_unlock(&cdev->lock);
912 mutex_unlock(&tz->lock);
913
914 if (!result)
915 return 0;
916
917 device_remove_file(&tz->device, &dev->weight_attr);
918 remove_trip_file:
919 device_remove_file(&tz->device, &dev->attr);
920 remove_symbol_link:
921 sysfs_remove_link(&tz->device.kobj, dev->name);
922 release_ida:
923 ida_simple_remove(&tz->ida, dev->id);
924 free_mem:
925 kfree(dev);
926 return result;
927 }
928 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
929
930 /**
931 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
932 * thermal zone.
933 * @tz: pointer to a struct thermal_zone_device.
934 * @trip: indicates which trip point the cooling devices is
935 * associated with in this thermal zone.
936 * @cdev: pointer to a struct thermal_cooling_device.
937 *
938 * This interface function unbind a thermal cooling device from the certain
939 * trip point of a thermal zone device.
940 * This function is usually called in the thermal zone device .unbind callback.
941 *
942 * Return: 0 on success, the proper error value otherwise.
943 */
thermal_zone_unbind_cooling_device(struct thermal_zone_device * tz,int trip,struct thermal_cooling_device * cdev)944 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
945 int trip,
946 struct thermal_cooling_device *cdev)
947 {
948 struct thermal_instance *pos, *next;
949
950 mutex_lock(&tz->lock);
951 mutex_lock(&cdev->lock);
952 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
953 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
954 list_del(&pos->tz_node);
955 list_del(&pos->cdev_node);
956 mutex_unlock(&cdev->lock);
957 mutex_unlock(&tz->lock);
958 goto unbind;
959 }
960 }
961 mutex_unlock(&cdev->lock);
962 mutex_unlock(&tz->lock);
963
964 return -ENODEV;
965
966 unbind:
967 device_remove_file(&tz->device, &pos->weight_attr);
968 device_remove_file(&tz->device, &pos->attr);
969 sysfs_remove_link(&tz->device.kobj, pos->name);
970 ida_simple_remove(&tz->ida, pos->id);
971 kfree(pos);
972 return 0;
973 }
974 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
975
thermal_release(struct device * dev)976 static void thermal_release(struct device *dev)
977 {
978 struct thermal_zone_device *tz;
979 struct thermal_cooling_device *cdev;
980
981 if (!strncmp(dev_name(dev), "thermal_zone",
982 sizeof("thermal_zone") - 1)) {
983 tz = to_thermal_zone(dev);
984 thermal_zone_destroy_device_groups(tz);
985 kfree(tz);
986 } else if (!strncmp(dev_name(dev), "cooling_device",
987 sizeof("cooling_device") - 1)) {
988 cdev = to_cooling_device(dev);
989 kfree(cdev);
990 }
991 }
992
993 static struct class thermal_class = {
994 .name = "thermal",
995 .dev_release = thermal_release,
996 };
997
998 static inline
print_bind_err_msg(struct thermal_zone_device * tz,struct thermal_cooling_device * cdev,int ret)999 void print_bind_err_msg(struct thermal_zone_device *tz,
1000 struct thermal_cooling_device *cdev, int ret)
1001 {
1002 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
1003 tz->type, cdev->type, ret);
1004 }
1005
__bind(struct thermal_zone_device * tz,int mask,struct thermal_cooling_device * cdev,unsigned long * limits,unsigned int weight)1006 static void __bind(struct thermal_zone_device *tz, int mask,
1007 struct thermal_cooling_device *cdev,
1008 unsigned long *limits,
1009 unsigned int weight)
1010 {
1011 int i, ret;
1012
1013 for (i = 0; i < tz->trips; i++) {
1014 if (mask & (1 << i)) {
1015 unsigned long upper, lower;
1016
1017 upper = THERMAL_NO_LIMIT;
1018 lower = THERMAL_NO_LIMIT;
1019 if (limits) {
1020 lower = limits[i * 2];
1021 upper = limits[i * 2 + 1];
1022 }
1023 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
1024 upper, lower,
1025 weight);
1026 if (ret)
1027 print_bind_err_msg(tz, cdev, ret);
1028 }
1029 }
1030 }
1031
bind_cdev(struct thermal_cooling_device * cdev)1032 static void bind_cdev(struct thermal_cooling_device *cdev)
1033 {
1034 int i, ret;
1035 const struct thermal_zone_params *tzp;
1036 struct thermal_zone_device *pos = NULL;
1037
1038 mutex_lock(&thermal_list_lock);
1039
1040 list_for_each_entry(pos, &thermal_tz_list, node) {
1041 if (!pos->tzp && !pos->ops->bind)
1042 continue;
1043
1044 if (pos->ops->bind) {
1045 ret = pos->ops->bind(pos, cdev);
1046 if (ret)
1047 print_bind_err_msg(pos, cdev, ret);
1048 continue;
1049 }
1050
1051 tzp = pos->tzp;
1052 if (!tzp || !tzp->tbp)
1053 continue;
1054
1055 for (i = 0; i < tzp->num_tbps; i++) {
1056 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1057 continue;
1058 if (tzp->tbp[i].match(pos, cdev))
1059 continue;
1060 tzp->tbp[i].cdev = cdev;
1061 __bind(pos, tzp->tbp[i].trip_mask, cdev,
1062 tzp->tbp[i].binding_limits,
1063 tzp->tbp[i].weight);
1064 }
1065 }
1066
1067 mutex_unlock(&thermal_list_lock);
1068 }
1069
1070 /**
1071 * __thermal_cooling_device_register() - register a new thermal cooling device
1072 * @np: a pointer to a device tree node.
1073 * @type: the thermal cooling device type.
1074 * @devdata: device private data.
1075 * @ops: standard thermal cooling devices callbacks.
1076 *
1077 * This interface function adds a new thermal cooling device (fan/processor/...)
1078 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1079 * to all the thermal zone devices registered at the same time.
1080 * It also gives the opportunity to link the cooling device to a device tree
1081 * node, so that it can be bound to a thermal zone created out of device tree.
1082 *
1083 * Return: a pointer to the created struct thermal_cooling_device or an
1084 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1085 */
1086 static struct thermal_cooling_device *
__thermal_cooling_device_register(struct device_node * np,const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)1087 __thermal_cooling_device_register(struct device_node *np,
1088 const char *type, void *devdata,
1089 const struct thermal_cooling_device_ops *ops)
1090 {
1091 struct thermal_cooling_device *cdev;
1092 struct thermal_zone_device *pos = NULL;
1093 int result;
1094
1095 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1096 return ERR_PTR(-EINVAL);
1097
1098 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1099 !ops->set_cur_state)
1100 return ERR_PTR(-EINVAL);
1101
1102 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
1103 if (!cdev)
1104 return ERR_PTR(-ENOMEM);
1105
1106 result = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL);
1107 if (result < 0) {
1108 kfree(cdev);
1109 return ERR_PTR(result);
1110 }
1111
1112 cdev->id = result;
1113 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
1114 mutex_init(&cdev->lock);
1115 INIT_LIST_HEAD(&cdev->thermal_instances);
1116 cdev->np = np;
1117 cdev->ops = ops;
1118 cdev->updated = false;
1119 cdev->device.class = &thermal_class;
1120 cdev->devdata = devdata;
1121 thermal_cooling_device_setup_sysfs(cdev);
1122 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1123 result = device_register(&cdev->device);
1124 if (result) {
1125 ida_simple_remove(&thermal_cdev_ida, cdev->id);
1126 put_device(&cdev->device);
1127 return ERR_PTR(result);
1128 }
1129
1130 /* Add 'this' new cdev to the global cdev list */
1131 mutex_lock(&thermal_list_lock);
1132 list_add(&cdev->node, &thermal_cdev_list);
1133 mutex_unlock(&thermal_list_lock);
1134
1135 /* Update binding information for 'this' new cdev */
1136 bind_cdev(cdev);
1137
1138 mutex_lock(&thermal_list_lock);
1139 list_for_each_entry(pos, &thermal_tz_list, node)
1140 if (atomic_cmpxchg(&pos->need_update, 1, 0))
1141 thermal_zone_device_update(pos,
1142 THERMAL_EVENT_UNSPECIFIED);
1143 mutex_unlock(&thermal_list_lock);
1144
1145 return cdev;
1146 }
1147
1148 /**
1149 * thermal_cooling_device_register() - register a new thermal cooling device
1150 * @type: the thermal cooling device type.
1151 * @devdata: device private data.
1152 * @ops: standard thermal cooling devices callbacks.
1153 *
1154 * This interface function adds a new thermal cooling device (fan/processor/...)
1155 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1156 * to all the thermal zone devices registered at the same time.
1157 *
1158 * Return: a pointer to the created struct thermal_cooling_device or an
1159 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1160 */
1161 struct thermal_cooling_device *
thermal_cooling_device_register(const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)1162 thermal_cooling_device_register(const char *type, void *devdata,
1163 const struct thermal_cooling_device_ops *ops)
1164 {
1165 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1166 }
1167 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1168
1169 /**
1170 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1171 * @np: a pointer to a device tree node.
1172 * @type: the thermal cooling device type.
1173 * @devdata: device private data.
1174 * @ops: standard thermal cooling devices callbacks.
1175 *
1176 * This function will register a cooling device with device tree node reference.
1177 * This interface function adds a new thermal cooling device (fan/processor/...)
1178 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1179 * to all the thermal zone devices registered at the same time.
1180 *
1181 * Return: a pointer to the created struct thermal_cooling_device or an
1182 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1183 */
1184 struct thermal_cooling_device *
thermal_of_cooling_device_register(struct device_node * np,const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)1185 thermal_of_cooling_device_register(struct device_node *np,
1186 const char *type, void *devdata,
1187 const struct thermal_cooling_device_ops *ops)
1188 {
1189 return __thermal_cooling_device_register(np, type, devdata, ops);
1190 }
1191 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1192
thermal_cooling_device_release(struct device * dev,void * res)1193 static void thermal_cooling_device_release(struct device *dev, void *res)
1194 {
1195 thermal_cooling_device_unregister(
1196 *(struct thermal_cooling_device **)res);
1197 }
1198
1199 /**
1200 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1201 * device
1202 * @dev: a valid struct device pointer of a sensor device.
1203 * @np: a pointer to a device tree node.
1204 * @type: the thermal cooling device type.
1205 * @devdata: device private data.
1206 * @ops: standard thermal cooling devices callbacks.
1207 *
1208 * This function will register a cooling device with device tree node reference.
1209 * This interface function adds a new thermal cooling device (fan/processor/...)
1210 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1211 * to all the thermal zone devices registered at the same time.
1212 *
1213 * Return: a pointer to the created struct thermal_cooling_device or an
1214 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1215 */
1216 struct thermal_cooling_device *
devm_thermal_of_cooling_device_register(struct device * dev,struct device_node * np,char * type,void * devdata,const struct thermal_cooling_device_ops * ops)1217 devm_thermal_of_cooling_device_register(struct device *dev,
1218 struct device_node *np,
1219 char *type, void *devdata,
1220 const struct thermal_cooling_device_ops *ops)
1221 {
1222 struct thermal_cooling_device **ptr, *tcd;
1223
1224 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1225 GFP_KERNEL);
1226 if (!ptr)
1227 return ERR_PTR(-ENOMEM);
1228
1229 tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1230 if (IS_ERR(tcd)) {
1231 devres_free(ptr);
1232 return tcd;
1233 }
1234
1235 *ptr = tcd;
1236 devres_add(dev, ptr);
1237
1238 return tcd;
1239 }
1240 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1241
__unbind(struct thermal_zone_device * tz,int mask,struct thermal_cooling_device * cdev)1242 static void __unbind(struct thermal_zone_device *tz, int mask,
1243 struct thermal_cooling_device *cdev)
1244 {
1245 int i;
1246
1247 for (i = 0; i < tz->trips; i++)
1248 if (mask & (1 << i))
1249 thermal_zone_unbind_cooling_device(tz, i, cdev);
1250 }
1251
1252 /**
1253 * thermal_cooling_device_unregister - removes a thermal cooling device
1254 * @cdev: the thermal cooling device to remove.
1255 *
1256 * thermal_cooling_device_unregister() must be called when a registered
1257 * thermal cooling device is no longer needed.
1258 */
thermal_cooling_device_unregister(struct thermal_cooling_device * cdev)1259 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1260 {
1261 int i;
1262 const struct thermal_zone_params *tzp;
1263 struct thermal_zone_device *tz;
1264 struct thermal_cooling_device *pos = NULL;
1265
1266 if (!cdev)
1267 return;
1268
1269 mutex_lock(&thermal_list_lock);
1270 list_for_each_entry(pos, &thermal_cdev_list, node)
1271 if (pos == cdev)
1272 break;
1273 if (pos != cdev) {
1274 /* thermal cooling device not found */
1275 mutex_unlock(&thermal_list_lock);
1276 return;
1277 }
1278 list_del(&cdev->node);
1279
1280 /* Unbind all thermal zones associated with 'this' cdev */
1281 list_for_each_entry(tz, &thermal_tz_list, node) {
1282 if (tz->ops->unbind) {
1283 tz->ops->unbind(tz, cdev);
1284 continue;
1285 }
1286
1287 if (!tz->tzp || !tz->tzp->tbp)
1288 continue;
1289
1290 tzp = tz->tzp;
1291 for (i = 0; i < tzp->num_tbps; i++) {
1292 if (tzp->tbp[i].cdev == cdev) {
1293 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1294 tzp->tbp[i].cdev = NULL;
1295 }
1296 }
1297 }
1298
1299 mutex_unlock(&thermal_list_lock);
1300
1301 ida_simple_remove(&thermal_cdev_ida, cdev->id);
1302 device_del(&cdev->device);
1303 thermal_cooling_device_destroy_sysfs(cdev);
1304 put_device(&cdev->device);
1305 }
1306 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1307
bind_tz(struct thermal_zone_device * tz)1308 static void bind_tz(struct thermal_zone_device *tz)
1309 {
1310 int i, ret;
1311 struct thermal_cooling_device *pos = NULL;
1312 const struct thermal_zone_params *tzp = tz->tzp;
1313
1314 if (!tzp && !tz->ops->bind)
1315 return;
1316
1317 mutex_lock(&thermal_list_lock);
1318
1319 /* If there is ops->bind, try to use ops->bind */
1320 if (tz->ops->bind) {
1321 list_for_each_entry(pos, &thermal_cdev_list, node) {
1322 ret = tz->ops->bind(tz, pos);
1323 if (ret)
1324 print_bind_err_msg(tz, pos, ret);
1325 }
1326 goto exit;
1327 }
1328
1329 if (!tzp || !tzp->tbp)
1330 goto exit;
1331
1332 list_for_each_entry(pos, &thermal_cdev_list, node) {
1333 for (i = 0; i < tzp->num_tbps; i++) {
1334 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1335 continue;
1336 if (tzp->tbp[i].match(tz, pos))
1337 continue;
1338 tzp->tbp[i].cdev = pos;
1339 __bind(tz, tzp->tbp[i].trip_mask, pos,
1340 tzp->tbp[i].binding_limits,
1341 tzp->tbp[i].weight);
1342 }
1343 }
1344 exit:
1345 mutex_unlock(&thermal_list_lock);
1346 }
1347
1348 /**
1349 * thermal_zone_device_register() - register a new thermal zone device
1350 * @type: the thermal zone device type
1351 * @trips: the number of trip points the thermal zone support
1352 * @mask: a bit string indicating the writeablility of trip points
1353 * @devdata: private device data
1354 * @ops: standard thermal zone device callbacks
1355 * @tzp: thermal zone platform parameters
1356 * @passive_delay: number of milliseconds to wait between polls when
1357 * performing passive cooling
1358 * @polling_delay: number of milliseconds to wait between polls when checking
1359 * whether trip points have been crossed (0 for interrupt
1360 * driven systems)
1361 *
1362 * This interface function adds a new thermal zone device (sensor) to
1363 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1364 * thermal cooling devices registered at the same time.
1365 * thermal_zone_device_unregister() must be called when the device is no
1366 * longer needed. The passive cooling depends on the .get_trend() return value.
1367 *
1368 * Return: a pointer to the created struct thermal_zone_device or an
1369 * in case of error, an ERR_PTR. Caller must check return value with
1370 * IS_ERR*() helpers.
1371 */
1372 struct thermal_zone_device *
thermal_zone_device_register(const char * type,int trips,int mask,void * devdata,struct thermal_zone_device_ops * ops,struct thermal_zone_params * tzp,int passive_delay,int polling_delay)1373 thermal_zone_device_register(const char *type, int trips, int mask,
1374 void *devdata, struct thermal_zone_device_ops *ops,
1375 struct thermal_zone_params *tzp, int passive_delay,
1376 int polling_delay)
1377 {
1378 struct thermal_zone_device *tz;
1379 enum thermal_trip_type trip_type;
1380 int trip_temp;
1381 int id;
1382 int result;
1383 int count;
1384 struct thermal_governor *governor;
1385
1386 if (!type || strlen(type) == 0) {
1387 pr_err("Error: No thermal zone type defined\n");
1388 return ERR_PTR(-EINVAL);
1389 }
1390
1391 if (type && strlen(type) >= THERMAL_NAME_LENGTH) {
1392 pr_err("Error: Thermal zone name (%s) too long, should be under %d chars\n",
1393 type, THERMAL_NAME_LENGTH);
1394 return ERR_PTR(-EINVAL);
1395 }
1396
1397 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips) {
1398 pr_err("Error: Incorrect number of thermal trips\n");
1399 return ERR_PTR(-EINVAL);
1400 }
1401
1402 if (!ops) {
1403 pr_err("Error: Thermal zone device ops not defined\n");
1404 return ERR_PTR(-EINVAL);
1405 }
1406
1407 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1408 return ERR_PTR(-EINVAL);
1409
1410 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1411 if (!tz)
1412 return ERR_PTR(-ENOMEM);
1413
1414 INIT_LIST_HEAD(&tz->thermal_instances);
1415 ida_init(&tz->ida);
1416 mutex_init(&tz->lock);
1417 id = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL);
1418 if (id < 0) {
1419 result = id;
1420 goto free_tz;
1421 }
1422
1423 tz->id = id;
1424 strlcpy(tz->type, type, sizeof(tz->type));
1425 tz->ops = ops;
1426 tz->tzp = tzp;
1427 tz->device.class = &thermal_class;
1428 tz->devdata = devdata;
1429 tz->trips = trips;
1430 tz->passive_delay = passive_delay;
1431 tz->polling_delay = polling_delay;
1432
1433 /* sys I/F */
1434 /* Add nodes that are always present via .groups */
1435 result = thermal_zone_create_device_groups(tz, mask);
1436 if (result)
1437 goto remove_id;
1438
1439 /* A new thermal zone needs to be updated anyway. */
1440 atomic_set(&tz->need_update, 1);
1441
1442 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1443 result = device_register(&tz->device);
1444 if (result)
1445 goto release_device;
1446
1447 for (count = 0; count < trips; count++) {
1448 if (tz->ops->get_trip_type(tz, count, &trip_type))
1449 set_bit(count, &tz->trips_disabled);
1450 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
1451 set_bit(count, &tz->trips_disabled);
1452 /* Check for bogus trip points */
1453 if (trip_temp == 0)
1454 set_bit(count, &tz->trips_disabled);
1455 }
1456
1457 /* Update 'this' zone's governor information */
1458 mutex_lock(&thermal_governor_lock);
1459
1460 if (tz->tzp)
1461 governor = __find_governor(tz->tzp->governor_name);
1462 else
1463 governor = def_governor;
1464
1465 result = thermal_set_governor(tz, governor);
1466 if (result) {
1467 mutex_unlock(&thermal_governor_lock);
1468 goto unregister;
1469 }
1470
1471 mutex_unlock(&thermal_governor_lock);
1472
1473 if (!tz->tzp || !tz->tzp->no_hwmon) {
1474 result = thermal_add_hwmon_sysfs(tz);
1475 if (result)
1476 goto unregister;
1477 }
1478
1479 mutex_lock(&thermal_list_lock);
1480 list_add_tail(&tz->node, &thermal_tz_list);
1481 mutex_unlock(&thermal_list_lock);
1482
1483 /* Bind cooling devices for this zone */
1484 bind_tz(tz);
1485
1486 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1487
1488 thermal_zone_device_reset(tz);
1489 /* Update the new thermal zone and mark it as already updated. */
1490 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1491 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1492
1493 thermal_notify_tz_create(tz->id, tz->type);
1494
1495 return tz;
1496
1497 unregister:
1498 device_del(&tz->device);
1499 release_device:
1500 put_device(&tz->device);
1501 tz = NULL;
1502 remove_id:
1503 ida_simple_remove(&thermal_tz_ida, id);
1504 free_tz:
1505 kfree(tz);
1506 return ERR_PTR(result);
1507 }
1508 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1509
1510 /**
1511 * thermal_zone_device_unregister - removes the registered thermal zone device
1512 * @tz: the thermal zone device to remove
1513 */
thermal_zone_device_unregister(struct thermal_zone_device * tz)1514 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1515 {
1516 int i, tz_id;
1517 const struct thermal_zone_params *tzp;
1518 struct thermal_cooling_device *cdev;
1519 struct thermal_zone_device *pos = NULL;
1520
1521 if (!tz)
1522 return;
1523
1524 tzp = tz->tzp;
1525 tz_id = tz->id;
1526
1527 mutex_lock(&thermal_list_lock);
1528 list_for_each_entry(pos, &thermal_tz_list, node)
1529 if (pos == tz)
1530 break;
1531 if (pos != tz) {
1532 /* thermal zone device not found */
1533 mutex_unlock(&thermal_list_lock);
1534 return;
1535 }
1536 list_del(&tz->node);
1537
1538 /* Unbind all cdevs associated with 'this' thermal zone */
1539 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1540 if (tz->ops->unbind) {
1541 tz->ops->unbind(tz, cdev);
1542 continue;
1543 }
1544
1545 if (!tzp || !tzp->tbp)
1546 break;
1547
1548 for (i = 0; i < tzp->num_tbps; i++) {
1549 if (tzp->tbp[i].cdev == cdev) {
1550 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1551 tzp->tbp[i].cdev = NULL;
1552 }
1553 }
1554 }
1555
1556 mutex_unlock(&thermal_list_lock);
1557
1558 cancel_delayed_work_sync(&tz->poll_queue);
1559
1560 thermal_set_governor(tz, NULL);
1561
1562 thermal_remove_hwmon_sysfs(tz);
1563 ida_simple_remove(&thermal_tz_ida, tz->id);
1564 ida_destroy(&tz->ida);
1565 mutex_destroy(&tz->lock);
1566 device_unregister(&tz->device);
1567
1568 thermal_notify_tz_delete(tz_id);
1569 }
1570 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1571
1572 /**
1573 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1574 * @name: thermal zone name to fetch the temperature
1575 *
1576 * When only one zone is found with the passed name, returns a reference to it.
1577 *
1578 * Return: On success returns a reference to an unique thermal zone with
1579 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1580 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1581 */
thermal_zone_get_zone_by_name(const char * name)1582 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1583 {
1584 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1585 unsigned int found = 0;
1586
1587 if (!name)
1588 goto exit;
1589
1590 mutex_lock(&thermal_list_lock);
1591 list_for_each_entry(pos, &thermal_tz_list, node)
1592 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1593 found++;
1594 ref = pos;
1595 }
1596 mutex_unlock(&thermal_list_lock);
1597
1598 /* nothing has been found, thus an error code for it */
1599 if (found == 0)
1600 ref = ERR_PTR(-ENODEV);
1601 else if (found > 1)
1602 /* Success only when an unique zone is found */
1603 ref = ERR_PTR(-EEXIST);
1604
1605 exit:
1606 return ref;
1607 }
1608 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1609
thermal_pm_notify(struct notifier_block * nb,unsigned long mode,void * _unused)1610 static int thermal_pm_notify(struct notifier_block *nb,
1611 unsigned long mode, void *_unused)
1612 {
1613 struct thermal_zone_device *tz;
1614
1615 switch (mode) {
1616 case PM_HIBERNATION_PREPARE:
1617 case PM_RESTORE_PREPARE:
1618 case PM_SUSPEND_PREPARE:
1619 atomic_set(&in_suspend, 1);
1620 break;
1621 case PM_POST_HIBERNATION:
1622 case PM_POST_RESTORE:
1623 case PM_POST_SUSPEND:
1624 atomic_set(&in_suspend, 0);
1625 list_for_each_entry(tz, &thermal_tz_list, node) {
1626 if (!thermal_zone_device_is_enabled(tz))
1627 continue;
1628
1629 thermal_zone_device_init(tz);
1630 thermal_zone_device_update(tz,
1631 THERMAL_EVENT_UNSPECIFIED);
1632 }
1633 break;
1634 default:
1635 break;
1636 }
1637 return 0;
1638 }
1639
1640 static struct notifier_block thermal_pm_nb = {
1641 .notifier_call = thermal_pm_notify,
1642 };
1643
thermal_init(void)1644 static int __init thermal_init(void)
1645 {
1646 int result;
1647
1648 result = thermal_netlink_init();
1649 if (result)
1650 goto error;
1651
1652 result = thermal_register_governors();
1653 if (result)
1654 goto error;
1655
1656 result = class_register(&thermal_class);
1657 if (result)
1658 goto unregister_governors;
1659
1660 result = of_parse_thermal_zones();
1661 if (result)
1662 goto unregister_class;
1663
1664 result = register_pm_notifier(&thermal_pm_nb);
1665 if (result)
1666 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1667 result);
1668
1669 return 0;
1670
1671 unregister_class:
1672 class_unregister(&thermal_class);
1673 unregister_governors:
1674 thermal_unregister_governors();
1675 error:
1676 ida_destroy(&thermal_tz_ida);
1677 ida_destroy(&thermal_cdev_ida);
1678 mutex_destroy(&thermal_list_lock);
1679 mutex_destroy(&thermal_governor_lock);
1680 mutex_destroy(&poweroff_lock);
1681 return result;
1682 }
1683 postcore_initcall(thermal_init);
1684