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