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 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
886 sysfs_attr_init(&dev->attr.attr);
887 dev->attr.attr.name = dev->attr_name;
888 dev->attr.attr.mode = 0444;
889 dev->attr.show = trip_point_show;
890 result = device_create_file(&tz->device, &dev->attr);
891 if (result)
892 goto remove_symbol_link;
893
894 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
895 sysfs_attr_init(&dev->weight_attr.attr);
896 dev->weight_attr.attr.name = dev->weight_attr_name;
897 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
898 dev->weight_attr.show = weight_show;
899 dev->weight_attr.store = weight_store;
900 result = device_create_file(&tz->device, &dev->weight_attr);
901 if (result)
902 goto remove_trip_file;
903
904 mutex_lock(&tz->lock);
905 mutex_lock(&cdev->lock);
906 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
907 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
908 result = -EEXIST;
909 break;
910 }
911 if (!result) {
912 list_add_tail(&dev->tz_node, &tz->thermal_instances);
913 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
914 atomic_set(&tz->need_update, 1);
915 }
916 mutex_unlock(&cdev->lock);
917 mutex_unlock(&tz->lock);
918
919 if (!result)
920 return 0;
921
922 device_remove_file(&tz->device, &dev->weight_attr);
923 remove_trip_file:
924 device_remove_file(&tz->device, &dev->attr);
925 remove_symbol_link:
926 sysfs_remove_link(&tz->device.kobj, dev->name);
927 release_ida:
928 ida_simple_remove(&tz->ida, dev->id);
929 free_mem:
930 kfree(dev);
931 return result;
932 }
933 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
934
935 /**
936 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
937 * thermal zone.
938 * @tz: pointer to a struct thermal_zone_device.
939 * @trip: indicates which trip point the cooling devices is
940 * associated with in this thermal zone.
941 * @cdev: pointer to a struct thermal_cooling_device.
942 *
943 * This interface function unbind a thermal cooling device from the certain
944 * trip point of a thermal zone device.
945 * This function is usually called in the thermal zone device .unbind callback.
946 *
947 * Return: 0 on success, the proper error value otherwise.
948 */
thermal_zone_unbind_cooling_device(struct thermal_zone_device * tz,int trip,struct thermal_cooling_device * cdev)949 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
950 int trip,
951 struct thermal_cooling_device *cdev)
952 {
953 struct thermal_instance *pos, *next;
954
955 mutex_lock(&tz->lock);
956 mutex_lock(&cdev->lock);
957 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
958 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
959 list_del(&pos->tz_node);
960 list_del(&pos->cdev_node);
961 mutex_unlock(&cdev->lock);
962 mutex_unlock(&tz->lock);
963 goto unbind;
964 }
965 }
966 mutex_unlock(&cdev->lock);
967 mutex_unlock(&tz->lock);
968
969 return -ENODEV;
970
971 unbind:
972 device_remove_file(&tz->device, &pos->weight_attr);
973 device_remove_file(&tz->device, &pos->attr);
974 sysfs_remove_link(&tz->device.kobj, pos->name);
975 ida_simple_remove(&tz->ida, pos->id);
976 kfree(pos);
977 return 0;
978 }
979 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
980
thermal_release(struct device * dev)981 static void thermal_release(struct device *dev)
982 {
983 struct thermal_zone_device *tz;
984 struct thermal_cooling_device *cdev;
985
986 if (!strncmp(dev_name(dev), "thermal_zone",
987 sizeof("thermal_zone") - 1)) {
988 tz = to_thermal_zone(dev);
989 thermal_zone_destroy_device_groups(tz);
990 kfree(tz);
991 } else if (!strncmp(dev_name(dev), "cooling_device",
992 sizeof("cooling_device") - 1)) {
993 cdev = to_cooling_device(dev);
994 kfree(cdev);
995 }
996 }
997
998 static struct class thermal_class = {
999 .name = "thermal",
1000 .dev_release = thermal_release,
1001 };
1002
1003 static inline
print_bind_err_msg(struct thermal_zone_device * tz,struct thermal_cooling_device * cdev,int ret)1004 void print_bind_err_msg(struct thermal_zone_device *tz,
1005 struct thermal_cooling_device *cdev, int ret)
1006 {
1007 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
1008 tz->type, cdev->type, ret);
1009 }
1010
__bind(struct thermal_zone_device * tz,int mask,struct thermal_cooling_device * cdev,unsigned long * limits,unsigned int weight)1011 static void __bind(struct thermal_zone_device *tz, int mask,
1012 struct thermal_cooling_device *cdev,
1013 unsigned long *limits,
1014 unsigned int weight)
1015 {
1016 int i, ret;
1017
1018 for (i = 0; i < tz->trips; i++) {
1019 if (mask & (1 << i)) {
1020 unsigned long upper, lower;
1021
1022 upper = THERMAL_NO_LIMIT;
1023 lower = THERMAL_NO_LIMIT;
1024 if (limits) {
1025 lower = limits[i * 2];
1026 upper = limits[i * 2 + 1];
1027 }
1028 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
1029 upper, lower,
1030 weight);
1031 if (ret)
1032 print_bind_err_msg(tz, cdev, ret);
1033 }
1034 }
1035 }
1036
bind_cdev(struct thermal_cooling_device * cdev)1037 static void bind_cdev(struct thermal_cooling_device *cdev)
1038 {
1039 int i, ret;
1040 const struct thermal_zone_params *tzp;
1041 struct thermal_zone_device *pos = NULL;
1042
1043 mutex_lock(&thermal_list_lock);
1044
1045 list_for_each_entry(pos, &thermal_tz_list, node) {
1046 if (!pos->tzp && !pos->ops->bind)
1047 continue;
1048
1049 if (pos->ops->bind) {
1050 ret = pos->ops->bind(pos, cdev);
1051 if (ret)
1052 print_bind_err_msg(pos, cdev, ret);
1053 continue;
1054 }
1055
1056 tzp = pos->tzp;
1057 if (!tzp || !tzp->tbp)
1058 continue;
1059
1060 for (i = 0; i < tzp->num_tbps; i++) {
1061 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1062 continue;
1063 if (tzp->tbp[i].match(pos, cdev))
1064 continue;
1065 tzp->tbp[i].cdev = cdev;
1066 __bind(pos, tzp->tbp[i].trip_mask, cdev,
1067 tzp->tbp[i].binding_limits,
1068 tzp->tbp[i].weight);
1069 }
1070 }
1071
1072 mutex_unlock(&thermal_list_lock);
1073 }
1074
1075 /**
1076 * __thermal_cooling_device_register() - register a new thermal cooling device
1077 * @np: a pointer to a device tree node.
1078 * @type: the thermal cooling device type.
1079 * @devdata: device private data.
1080 * @ops: standard thermal cooling devices callbacks.
1081 *
1082 * This interface function adds a new thermal cooling device (fan/processor/...)
1083 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1084 * to all the thermal zone devices registered at the same time.
1085 * It also gives the opportunity to link the cooling device to a device tree
1086 * node, so that it can be bound to a thermal zone created out of device tree.
1087 *
1088 * Return: a pointer to the created struct thermal_cooling_device or an
1089 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1090 */
1091 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)1092 __thermal_cooling_device_register(struct device_node *np,
1093 const char *type, void *devdata,
1094 const struct thermal_cooling_device_ops *ops)
1095 {
1096 struct thermal_cooling_device *cdev;
1097 struct thermal_zone_device *pos = NULL;
1098 int result;
1099
1100 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1101 return ERR_PTR(-EINVAL);
1102
1103 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1104 !ops->set_cur_state)
1105 return ERR_PTR(-EINVAL);
1106
1107 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
1108 if (!cdev)
1109 return ERR_PTR(-ENOMEM);
1110
1111 result = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL);
1112 if (result < 0) {
1113 kfree(cdev);
1114 return ERR_PTR(result);
1115 }
1116
1117 cdev->id = result;
1118 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
1119 mutex_init(&cdev->lock);
1120 INIT_LIST_HEAD(&cdev->thermal_instances);
1121 cdev->np = np;
1122 cdev->ops = ops;
1123 cdev->updated = false;
1124 cdev->device.class = &thermal_class;
1125 cdev->devdata = devdata;
1126 thermal_cooling_device_setup_sysfs(cdev);
1127 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1128 result = device_register(&cdev->device);
1129 if (result) {
1130 ida_simple_remove(&thermal_cdev_ida, cdev->id);
1131 put_device(&cdev->device);
1132 return ERR_PTR(result);
1133 }
1134
1135 /* Add 'this' new cdev to the global cdev list */
1136 mutex_lock(&thermal_list_lock);
1137 list_add(&cdev->node, &thermal_cdev_list);
1138 mutex_unlock(&thermal_list_lock);
1139
1140 /* Update binding information for 'this' new cdev */
1141 bind_cdev(cdev);
1142
1143 mutex_lock(&thermal_list_lock);
1144 list_for_each_entry(pos, &thermal_tz_list, node)
1145 if (atomic_cmpxchg(&pos->need_update, 1, 0))
1146 thermal_zone_device_update(pos,
1147 THERMAL_EVENT_UNSPECIFIED);
1148 mutex_unlock(&thermal_list_lock);
1149
1150 return cdev;
1151 }
1152
1153 /**
1154 * thermal_cooling_device_register() - register a new thermal cooling device
1155 * @type: the thermal cooling device type.
1156 * @devdata: device private data.
1157 * @ops: standard thermal cooling devices callbacks.
1158 *
1159 * This interface function adds a new thermal cooling device (fan/processor/...)
1160 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1161 * to all the thermal zone devices registered at the same time.
1162 *
1163 * Return: a pointer to the created struct thermal_cooling_device or an
1164 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1165 */
1166 struct thermal_cooling_device *
thermal_cooling_device_register(const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)1167 thermal_cooling_device_register(const char *type, void *devdata,
1168 const struct thermal_cooling_device_ops *ops)
1169 {
1170 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1171 }
1172 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1173
1174 /**
1175 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1176 * @np: a pointer to a device tree node.
1177 * @type: the thermal cooling device type.
1178 * @devdata: device private data.
1179 * @ops: standard thermal cooling devices callbacks.
1180 *
1181 * This function will register a cooling device with device tree node reference.
1182 * This interface function adds a new thermal cooling device (fan/processor/...)
1183 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1184 * to all the thermal zone devices registered at the same time.
1185 *
1186 * Return: a pointer to the created struct thermal_cooling_device or an
1187 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1188 */
1189 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)1190 thermal_of_cooling_device_register(struct device_node *np,
1191 const char *type, void *devdata,
1192 const struct thermal_cooling_device_ops *ops)
1193 {
1194 return __thermal_cooling_device_register(np, type, devdata, ops);
1195 }
1196 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1197
thermal_cooling_device_release(struct device * dev,void * res)1198 static void thermal_cooling_device_release(struct device *dev, void *res)
1199 {
1200 thermal_cooling_device_unregister(
1201 *(struct thermal_cooling_device **)res);
1202 }
1203
1204 /**
1205 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1206 * device
1207 * @dev: a valid struct device pointer of a sensor device.
1208 * @np: a pointer to a device tree node.
1209 * @type: the thermal cooling device type.
1210 * @devdata: device private data.
1211 * @ops: standard thermal cooling devices callbacks.
1212 *
1213 * This function will register a cooling device with device tree node reference.
1214 * This interface function adds a new thermal cooling device (fan/processor/...)
1215 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1216 * to all the thermal zone devices registered at the same time.
1217 *
1218 * Return: a pointer to the created struct thermal_cooling_device or an
1219 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1220 */
1221 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)1222 devm_thermal_of_cooling_device_register(struct device *dev,
1223 struct device_node *np,
1224 char *type, void *devdata,
1225 const struct thermal_cooling_device_ops *ops)
1226 {
1227 struct thermal_cooling_device **ptr, *tcd;
1228
1229 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1230 GFP_KERNEL);
1231 if (!ptr)
1232 return ERR_PTR(-ENOMEM);
1233
1234 tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1235 if (IS_ERR(tcd)) {
1236 devres_free(ptr);
1237 return tcd;
1238 }
1239
1240 *ptr = tcd;
1241 devres_add(dev, ptr);
1242
1243 return tcd;
1244 }
1245 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1246
__unbind(struct thermal_zone_device * tz,int mask,struct thermal_cooling_device * cdev)1247 static void __unbind(struct thermal_zone_device *tz, int mask,
1248 struct thermal_cooling_device *cdev)
1249 {
1250 int i;
1251
1252 for (i = 0; i < tz->trips; i++)
1253 if (mask & (1 << i))
1254 thermal_zone_unbind_cooling_device(tz, i, cdev);
1255 }
1256
1257 /**
1258 * thermal_cooling_device_unregister - removes a thermal cooling device
1259 * @cdev: the thermal cooling device to remove.
1260 *
1261 * thermal_cooling_device_unregister() must be called when a registered
1262 * thermal cooling device is no longer needed.
1263 */
thermal_cooling_device_unregister(struct thermal_cooling_device * cdev)1264 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1265 {
1266 int i;
1267 const struct thermal_zone_params *tzp;
1268 struct thermal_zone_device *tz;
1269 struct thermal_cooling_device *pos = NULL;
1270
1271 if (!cdev)
1272 return;
1273
1274 mutex_lock(&thermal_list_lock);
1275 list_for_each_entry(pos, &thermal_cdev_list, node)
1276 if (pos == cdev)
1277 break;
1278 if (pos != cdev) {
1279 /* thermal cooling device not found */
1280 mutex_unlock(&thermal_list_lock);
1281 return;
1282 }
1283 list_del(&cdev->node);
1284
1285 /* Unbind all thermal zones associated with 'this' cdev */
1286 list_for_each_entry(tz, &thermal_tz_list, node) {
1287 if (tz->ops->unbind) {
1288 tz->ops->unbind(tz, cdev);
1289 continue;
1290 }
1291
1292 if (!tz->tzp || !tz->tzp->tbp)
1293 continue;
1294
1295 tzp = tz->tzp;
1296 for (i = 0; i < tzp->num_tbps; i++) {
1297 if (tzp->tbp[i].cdev == cdev) {
1298 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1299 tzp->tbp[i].cdev = NULL;
1300 }
1301 }
1302 }
1303
1304 mutex_unlock(&thermal_list_lock);
1305
1306 ida_simple_remove(&thermal_cdev_ida, cdev->id);
1307 device_del(&cdev->device);
1308 thermal_cooling_device_destroy_sysfs(cdev);
1309 put_device(&cdev->device);
1310 }
1311 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1312
bind_tz(struct thermal_zone_device * tz)1313 static void bind_tz(struct thermal_zone_device *tz)
1314 {
1315 int i, ret;
1316 struct thermal_cooling_device *pos = NULL;
1317 const struct thermal_zone_params *tzp = tz->tzp;
1318
1319 if (!tzp && !tz->ops->bind)
1320 return;
1321
1322 mutex_lock(&thermal_list_lock);
1323
1324 /* If there is ops->bind, try to use ops->bind */
1325 if (tz->ops->bind) {
1326 list_for_each_entry(pos, &thermal_cdev_list, node) {
1327 ret = tz->ops->bind(tz, pos);
1328 if (ret)
1329 print_bind_err_msg(tz, pos, ret);
1330 }
1331 goto exit;
1332 }
1333
1334 if (!tzp || !tzp->tbp)
1335 goto exit;
1336
1337 list_for_each_entry(pos, &thermal_cdev_list, node) {
1338 for (i = 0; i < tzp->num_tbps; i++) {
1339 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1340 continue;
1341 if (tzp->tbp[i].match(tz, pos))
1342 continue;
1343 tzp->tbp[i].cdev = pos;
1344 __bind(tz, tzp->tbp[i].trip_mask, pos,
1345 tzp->tbp[i].binding_limits,
1346 tzp->tbp[i].weight);
1347 }
1348 }
1349 exit:
1350 mutex_unlock(&thermal_list_lock);
1351 }
1352
1353 /**
1354 * thermal_zone_device_register() - register a new thermal zone device
1355 * @type: the thermal zone device type
1356 * @trips: the number of trip points the thermal zone support
1357 * @mask: a bit string indicating the writeablility of trip points
1358 * @devdata: private device data
1359 * @ops: standard thermal zone device callbacks
1360 * @tzp: thermal zone platform parameters
1361 * @passive_delay: number of milliseconds to wait between polls when
1362 * performing passive cooling
1363 * @polling_delay: number of milliseconds to wait between polls when checking
1364 * whether trip points have been crossed (0 for interrupt
1365 * driven systems)
1366 *
1367 * This interface function adds a new thermal zone device (sensor) to
1368 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1369 * thermal cooling devices registered at the same time.
1370 * thermal_zone_device_unregister() must be called when the device is no
1371 * longer needed. The passive cooling depends on the .get_trend() return value.
1372 *
1373 * Return: a pointer to the created struct thermal_zone_device or an
1374 * in case of error, an ERR_PTR. Caller must check return value with
1375 * IS_ERR*() helpers.
1376 */
1377 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)1378 thermal_zone_device_register(const char *type, int trips, int mask,
1379 void *devdata, struct thermal_zone_device_ops *ops,
1380 struct thermal_zone_params *tzp, int passive_delay,
1381 int polling_delay)
1382 {
1383 struct thermal_zone_device *tz;
1384 enum thermal_trip_type trip_type;
1385 int trip_temp;
1386 int id;
1387 int result;
1388 int count;
1389 struct thermal_governor *governor;
1390
1391 if (!type || strlen(type) == 0) {
1392 pr_err("Error: No thermal zone type defined\n");
1393 return ERR_PTR(-EINVAL);
1394 }
1395
1396 if (type && strlen(type) >= THERMAL_NAME_LENGTH) {
1397 pr_err("Error: Thermal zone name (%s) too long, should be under %d chars\n",
1398 type, THERMAL_NAME_LENGTH);
1399 return ERR_PTR(-EINVAL);
1400 }
1401
1402 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips) {
1403 pr_err("Error: Incorrect number of thermal trips\n");
1404 return ERR_PTR(-EINVAL);
1405 }
1406
1407 if (!ops) {
1408 pr_err("Error: Thermal zone device ops not defined\n");
1409 return ERR_PTR(-EINVAL);
1410 }
1411
1412 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1413 return ERR_PTR(-EINVAL);
1414
1415 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1416 if (!tz)
1417 return ERR_PTR(-ENOMEM);
1418
1419 INIT_LIST_HEAD(&tz->thermal_instances);
1420 ida_init(&tz->ida);
1421 mutex_init(&tz->lock);
1422 id = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL);
1423 if (id < 0) {
1424 result = id;
1425 goto free_tz;
1426 }
1427
1428 tz->id = id;
1429 strlcpy(tz->type, type, sizeof(tz->type));
1430 tz->ops = ops;
1431 tz->tzp = tzp;
1432 tz->device.class = &thermal_class;
1433 tz->devdata = devdata;
1434 tz->trips = trips;
1435 tz->passive_delay = passive_delay;
1436 tz->polling_delay = polling_delay;
1437
1438 /* sys I/F */
1439 /* Add nodes that are always present via .groups */
1440 result = thermal_zone_create_device_groups(tz, mask);
1441 if (result)
1442 goto remove_id;
1443
1444 /* A new thermal zone needs to be updated anyway. */
1445 atomic_set(&tz->need_update, 1);
1446
1447 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1448 result = device_register(&tz->device);
1449 if (result)
1450 goto release_device;
1451
1452 for (count = 0; count < trips; count++) {
1453 if (tz->ops->get_trip_type(tz, count, &trip_type))
1454 set_bit(count, &tz->trips_disabled);
1455 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
1456 set_bit(count, &tz->trips_disabled);
1457 /* Check for bogus trip points */
1458 if (trip_temp == 0)
1459 set_bit(count, &tz->trips_disabled);
1460 }
1461
1462 /* Update 'this' zone's governor information */
1463 mutex_lock(&thermal_governor_lock);
1464
1465 if (tz->tzp)
1466 governor = __find_governor(tz->tzp->governor_name);
1467 else
1468 governor = def_governor;
1469
1470 result = thermal_set_governor(tz, governor);
1471 if (result) {
1472 mutex_unlock(&thermal_governor_lock);
1473 goto unregister;
1474 }
1475
1476 mutex_unlock(&thermal_governor_lock);
1477
1478 if (!tz->tzp || !tz->tzp->no_hwmon) {
1479 result = thermal_add_hwmon_sysfs(tz);
1480 if (result)
1481 goto unregister;
1482 }
1483
1484 mutex_lock(&thermal_list_lock);
1485 list_add_tail(&tz->node, &thermal_tz_list);
1486 mutex_unlock(&thermal_list_lock);
1487
1488 /* Bind cooling devices for this zone */
1489 bind_tz(tz);
1490
1491 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1492
1493 thermal_zone_device_reset(tz);
1494 /* Update the new thermal zone and mark it as already updated. */
1495 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1496 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1497
1498 thermal_notify_tz_create(tz->id, tz->type);
1499
1500 return tz;
1501
1502 unregister:
1503 device_del(&tz->device);
1504 release_device:
1505 put_device(&tz->device);
1506 tz = NULL;
1507 remove_id:
1508 ida_simple_remove(&thermal_tz_ida, id);
1509 free_tz:
1510 kfree(tz);
1511 return ERR_PTR(result);
1512 }
1513 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1514
1515 /**
1516 * thermal_zone_device_unregister - removes the registered thermal zone device
1517 * @tz: the thermal zone device to remove
1518 */
thermal_zone_device_unregister(struct thermal_zone_device * tz)1519 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1520 {
1521 int i, tz_id;
1522 const struct thermal_zone_params *tzp;
1523 struct thermal_cooling_device *cdev;
1524 struct thermal_zone_device *pos = NULL;
1525
1526 if (!tz)
1527 return;
1528
1529 tzp = tz->tzp;
1530 tz_id = tz->id;
1531
1532 mutex_lock(&thermal_list_lock);
1533 list_for_each_entry(pos, &thermal_tz_list, node)
1534 if (pos == tz)
1535 break;
1536 if (pos != tz) {
1537 /* thermal zone device not found */
1538 mutex_unlock(&thermal_list_lock);
1539 return;
1540 }
1541 list_del(&tz->node);
1542
1543 /* Unbind all cdevs associated with 'this' thermal zone */
1544 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1545 if (tz->ops->unbind) {
1546 tz->ops->unbind(tz, cdev);
1547 continue;
1548 }
1549
1550 if (!tzp || !tzp->tbp)
1551 break;
1552
1553 for (i = 0; i < tzp->num_tbps; i++) {
1554 if (tzp->tbp[i].cdev == cdev) {
1555 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1556 tzp->tbp[i].cdev = NULL;
1557 }
1558 }
1559 }
1560
1561 mutex_unlock(&thermal_list_lock);
1562
1563 cancel_delayed_work_sync(&tz->poll_queue);
1564
1565 thermal_set_governor(tz, NULL);
1566
1567 thermal_remove_hwmon_sysfs(tz);
1568 ida_simple_remove(&thermal_tz_ida, tz->id);
1569 ida_destroy(&tz->ida);
1570 mutex_destroy(&tz->lock);
1571 device_unregister(&tz->device);
1572
1573 thermal_notify_tz_delete(tz_id);
1574 }
1575 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1576
1577 /**
1578 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1579 * @name: thermal zone name to fetch the temperature
1580 *
1581 * When only one zone is found with the passed name, returns a reference to it.
1582 *
1583 * Return: On success returns a reference to an unique thermal zone with
1584 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1585 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1586 */
thermal_zone_get_zone_by_name(const char * name)1587 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1588 {
1589 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1590 unsigned int found = 0;
1591
1592 if (!name)
1593 goto exit;
1594
1595 mutex_lock(&thermal_list_lock);
1596 list_for_each_entry(pos, &thermal_tz_list, node)
1597 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1598 found++;
1599 ref = pos;
1600 }
1601 mutex_unlock(&thermal_list_lock);
1602
1603 /* nothing has been found, thus an error code for it */
1604 if (found == 0)
1605 ref = ERR_PTR(-ENODEV);
1606 else if (found > 1)
1607 /* Success only when an unique zone is found */
1608 ref = ERR_PTR(-EEXIST);
1609
1610 exit:
1611 return ref;
1612 }
1613 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1614
thermal_pm_notify(struct notifier_block * nb,unsigned long mode,void * _unused)1615 static int thermal_pm_notify(struct notifier_block *nb,
1616 unsigned long mode, void *_unused)
1617 {
1618 struct thermal_zone_device *tz;
1619 int irq_wakeable = 0;
1620
1621 switch (mode) {
1622 case PM_HIBERNATION_PREPARE:
1623 case PM_RESTORE_PREPARE:
1624 case PM_SUSPEND_PREPARE:
1625 atomic_set(&in_suspend, 1);
1626 break;
1627 case PM_POST_HIBERNATION:
1628 case PM_POST_RESTORE:
1629 case PM_POST_SUSPEND:
1630 atomic_set(&in_suspend, 0);
1631 list_for_each_entry(tz, &thermal_tz_list, node) {
1632 if (!thermal_zone_device_is_enabled(tz))
1633 continue;
1634
1635 trace_android_vh_thermal_pm_notify_suspend(tz, &irq_wakeable);
1636 if (irq_wakeable)
1637 continue;
1638
1639 thermal_zone_device_init(tz);
1640 thermal_zone_device_update(tz,
1641 THERMAL_EVENT_UNSPECIFIED);
1642 }
1643 break;
1644 default:
1645 break;
1646 }
1647 return 0;
1648 }
1649
1650 static struct notifier_block thermal_pm_nb = {
1651 .notifier_call = thermal_pm_notify,
1652 };
1653
thermal_init(void)1654 static int __init thermal_init(void)
1655 {
1656 int result;
1657
1658 result = thermal_netlink_init();
1659 if (result)
1660 goto error;
1661
1662 result = thermal_register_governors();
1663 if (result)
1664 goto error;
1665
1666 result = class_register(&thermal_class);
1667 if (result)
1668 goto unregister_governors;
1669
1670 result = of_parse_thermal_zones();
1671 if (result)
1672 goto unregister_class;
1673
1674 result = register_pm_notifier(&thermal_pm_nb);
1675 if (result)
1676 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1677 result);
1678
1679 return 0;
1680
1681 unregister_class:
1682 class_unregister(&thermal_class);
1683 unregister_governors:
1684 thermal_unregister_governors();
1685 error:
1686 ida_destroy(&thermal_tz_ida);
1687 ida_destroy(&thermal_cdev_ida);
1688 mutex_destroy(&thermal_list_lock);
1689 mutex_destroy(&thermal_governor_lock);
1690 mutex_destroy(&poweroff_lock);
1691 return result;
1692 }
1693 postcore_initcall(thermal_init);
1694