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