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