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