45f51727410cc40a22b76f97f314d12e97a2b26e
[releases.git] / 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 "thermal_trace.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 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
49 static 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  */
71 static 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  */
93 static 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
115 int 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
169 void 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);
191 exit:
192         mutex_unlock(&thermal_governor_lock);
193 }
194
195 int 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         if (!device_is_registered(&tz->device))
205                 goto exit;
206
207         gov = __find_governor(strim(policy));
208         if (!gov)
209                 goto exit;
210
211         ret = thermal_set_governor(tz, gov);
212
213 exit:
214         mutex_unlock(&tz->lock);
215         mutex_unlock(&thermal_governor_lock);
216
217         thermal_notify_tz_gov_change(tz->id, policy);
218
219         return ret;
220 }
221
222 int thermal_build_list_of_policies(char *buf)
223 {
224         struct thermal_governor *pos;
225         ssize_t count = 0;
226
227         mutex_lock(&thermal_governor_lock);
228
229         list_for_each_entry(pos, &thermal_governor_list, governor_list) {
230                 count += sysfs_emit_at(buf, count, "%s ", pos->name);
231         }
232         count += sysfs_emit_at(buf, 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 void monitor_thermal_zone(struct thermal_zone_device *tz)
299 {
300         if (tz->mode != THERMAL_DEVICE_ENABLED)
301                 thermal_zone_device_set_polling(tz, 0);
302         else if (tz->passive)
303                 thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies);
304         else if (tz->polling_delay_jiffies)
305                 thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies);
306 }
307
308 static void handle_non_critical_trips(struct thermal_zone_device *tz,
309                                       const struct thermal_trip *trip)
310 {
311         tz->governor ? tz->governor->throttle(tz, trip) :
312                        def_governor->throttle(tz, trip);
313 }
314
315 void thermal_zone_device_critical(struct thermal_zone_device *tz)
316 {
317         /*
318          * poweroff_delay_ms must be a carefully profiled positive value.
319          * Its a must for forced_emergency_poweroff_work to be scheduled.
320          */
321         int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
322
323         dev_emerg(&tz->device, "%s: critical temperature reached, "
324                   "shutting down\n", tz->type);
325
326         hw_protection_shutdown("Temperature too high", poweroff_delay_ms);
327 }
328 EXPORT_SYMBOL(thermal_zone_device_critical);
329
330 static void handle_critical_trips(struct thermal_zone_device *tz,
331                                   const struct thermal_trip *trip)
332 {
333         /* If we have not crossed the trip_temp, we do not care. */
334         if (trip->temperature <= 0 || tz->temperature < trip->temperature)
335                 return;
336
337         trace_thermal_zone_trip(tz, thermal_zone_trip_id(tz, trip), trip->type);
338
339         if (trip->type == THERMAL_TRIP_CRITICAL)
340                 tz->ops->critical(tz);
341         else if (tz->ops->hot)
342                 tz->ops->hot(tz);
343 }
344
345 static void handle_thermal_trip(struct thermal_zone_device *tz,
346                                 const struct thermal_trip *trip)
347 {
348         if (trip->temperature == THERMAL_TEMP_INVALID)
349                 return;
350
351         if (tz->last_temperature != THERMAL_TEMP_INVALID) {
352                 if (tz->last_temperature < trip->temperature &&
353                     tz->temperature >= trip->temperature)
354                         thermal_notify_tz_trip_up(tz->id,
355                                                   thermal_zone_trip_id(tz, trip),
356                                                   tz->temperature);
357                 if (tz->last_temperature >= trip->temperature &&
358                     tz->temperature < trip->temperature - trip->hysteresis)
359                         thermal_notify_tz_trip_down(tz->id,
360                                                     thermal_zone_trip_id(tz, trip),
361                                                     tz->temperature);
362         }
363
364         if (trip->type == THERMAL_TRIP_CRITICAL || trip->type == THERMAL_TRIP_HOT)
365                 handle_critical_trips(tz, trip);
366         else
367                 handle_non_critical_trips(tz, trip);
368 }
369
370 static void update_temperature(struct thermal_zone_device *tz)
371 {
372         int temp, ret;
373
374         ret = __thermal_zone_get_temp(tz, &temp);
375         if (ret) {
376                 if (ret != -EAGAIN)
377                         dev_warn(&tz->device,
378                                  "failed to read out thermal zone (%d)\n",
379                                  ret);
380                 return;
381         }
382
383         tz->last_temperature = tz->temperature;
384         tz->temperature = temp;
385
386         trace_thermal_temperature(tz);
387
388         thermal_genl_sampling_temp(tz->id, temp);
389 }
390
391 static void thermal_zone_device_init(struct thermal_zone_device *tz)
392 {
393         struct thermal_instance *pos;
394         tz->temperature = THERMAL_TEMP_INVALID;
395         tz->prev_low_trip = -INT_MAX;
396         tz->prev_high_trip = INT_MAX;
397         list_for_each_entry(pos, &tz->thermal_instances, tz_node)
398                 pos->initialized = false;
399 }
400
401 void __thermal_zone_device_update(struct thermal_zone_device *tz,
402                                   enum thermal_notify_event event)
403 {
404         const struct thermal_trip *trip;
405
406         if (tz->suspended)
407                 return;
408
409         if (WARN_ONCE(!tz->ops->get_temp,
410                       "'%s' must not be called without 'get_temp' ops set\n",
411                       __func__))
412                 return;
413
414         if (!thermal_zone_device_is_enabled(tz))
415                 return;
416
417         update_temperature(tz);
418
419         __thermal_zone_set_trips(tz);
420
421         tz->notify_event = event;
422
423         for_each_trip(tz, trip)
424                 handle_thermal_trip(tz, trip);
425
426         monitor_thermal_zone(tz);
427 }
428
429 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
430                                         enum thermal_device_mode mode)
431 {
432         int ret = 0;
433
434         mutex_lock(&tz->lock);
435
436         /* do nothing if mode isn't changing */
437         if (mode == tz->mode) {
438                 mutex_unlock(&tz->lock);
439
440                 return ret;
441         }
442
443         if (!device_is_registered(&tz->device)) {
444                 mutex_unlock(&tz->lock);
445
446                 return -ENODEV;
447         }
448
449         if (tz->ops->change_mode)
450                 ret = tz->ops->change_mode(tz, mode);
451
452         if (!ret)
453                 tz->mode = mode;
454
455         __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
456
457         mutex_unlock(&tz->lock);
458
459         if (mode == THERMAL_DEVICE_ENABLED)
460                 thermal_notify_tz_enable(tz->id);
461         else
462                 thermal_notify_tz_disable(tz->id);
463
464         return ret;
465 }
466
467 int thermal_zone_device_enable(struct thermal_zone_device *tz)
468 {
469         return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
470 }
471 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
472
473 int thermal_zone_device_disable(struct thermal_zone_device *tz)
474 {
475         return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
476 }
477 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
478
479 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
480 {
481         lockdep_assert_held(&tz->lock);
482
483         return tz->mode == THERMAL_DEVICE_ENABLED;
484 }
485
486 void thermal_zone_device_update(struct thermal_zone_device *tz,
487                                 enum thermal_notify_event event)
488 {
489         mutex_lock(&tz->lock);
490         if (device_is_registered(&tz->device))
491                 __thermal_zone_device_update(tz, event);
492         mutex_unlock(&tz->lock);
493 }
494 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
495
496 static void thermal_zone_device_check(struct work_struct *work)
497 {
498         struct thermal_zone_device *tz = container_of(work, struct
499                                                       thermal_zone_device,
500                                                       poll_queue.work);
501         thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
502 }
503
504 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
505                               void *data)
506 {
507         struct thermal_governor *gov;
508         int ret = 0;
509
510         mutex_lock(&thermal_governor_lock);
511         list_for_each_entry(gov, &thermal_governor_list, governor_list) {
512                 ret = cb(gov, data);
513                 if (ret)
514                         break;
515         }
516         mutex_unlock(&thermal_governor_lock);
517
518         return ret;
519 }
520
521 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
522                                               void *), void *data)
523 {
524         struct thermal_cooling_device *cdev;
525         int ret = 0;
526
527         mutex_lock(&thermal_list_lock);
528         list_for_each_entry(cdev, &thermal_cdev_list, node) {
529                 ret = cb(cdev, data);
530                 if (ret)
531                         break;
532         }
533         mutex_unlock(&thermal_list_lock);
534
535         return ret;
536 }
537
538 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
539                           void *data)
540 {
541         struct thermal_zone_device *tz;
542         int ret = 0;
543
544         mutex_lock(&thermal_list_lock);
545         list_for_each_entry(tz, &thermal_tz_list, node) {
546                 ret = cb(tz, data);
547                 if (ret)
548                         break;
549         }
550         mutex_unlock(&thermal_list_lock);
551
552         return ret;
553 }
554
555 struct thermal_zone_device *thermal_zone_get_by_id(int id)
556 {
557         struct thermal_zone_device *tz, *match = NULL;
558
559         mutex_lock(&thermal_list_lock);
560         list_for_each_entry(tz, &thermal_tz_list, node) {
561                 if (tz->id == id) {
562                         match = tz;
563                         break;
564                 }
565         }
566         mutex_unlock(&thermal_list_lock);
567
568         return match;
569 }
570
571 /*
572  * Device management section: cooling devices, zones devices, and binding
573  *
574  * Set of functions provided by the thermal core for:
575  * - cooling devices lifecycle: registration, unregistration,
576  *                              binding, and unbinding.
577  * - thermal zone devices lifecycle: registration, unregistration,
578  *                                   binding, and unbinding.
579  */
580
581 /**
582  * thermal_bind_cdev_to_trip - bind a cooling device to a thermal zone
583  * @tz:         pointer to struct thermal_zone_device
584  * @trip:       trip point the cooling devices is associated with in this zone.
585  * @cdev:       pointer to struct thermal_cooling_device
586  * @upper:      the Maximum cooling state for this trip point.
587  *              THERMAL_NO_LIMIT means no upper limit,
588  *              and the cooling device can be in max_state.
589  * @lower:      the Minimum cooling state can be used for this trip point.
590  *              THERMAL_NO_LIMIT means no lower limit,
591  *              and the cooling device can be in cooling state 0.
592  * @weight:     The weight of the cooling device to be bound to the
593  *              thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
594  *              default value
595  *
596  * This interface function bind a thermal cooling device to the certain trip
597  * point of a thermal zone device.
598  * This function is usually called in the thermal zone device .bind callback.
599  *
600  * Return: 0 on success, the proper error value otherwise.
601  */
602 int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz,
603                                      const struct thermal_trip *trip,
604                                      struct thermal_cooling_device *cdev,
605                                      unsigned long upper, unsigned long lower,
606                                      unsigned int weight)
607 {
608         struct thermal_instance *dev;
609         struct thermal_instance *pos;
610         struct thermal_zone_device *pos1;
611         struct thermal_cooling_device *pos2;
612         bool upper_no_limit;
613         int result;
614
615         list_for_each_entry(pos1, &thermal_tz_list, node) {
616                 if (pos1 == tz)
617                         break;
618         }
619         list_for_each_entry(pos2, &thermal_cdev_list, node) {
620                 if (pos2 == cdev)
621                         break;
622         }
623
624         if (tz != pos1 || cdev != pos2)
625                 return -EINVAL;
626
627         /* lower default 0, upper default max_state */
628         lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
629
630         if (upper == THERMAL_NO_LIMIT) {
631                 upper = cdev->max_state;
632                 upper_no_limit = true;
633         } else {
634                 upper_no_limit = false;
635         }
636
637         if (lower > upper || upper > cdev->max_state)
638                 return -EINVAL;
639
640         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
641         if (!dev)
642                 return -ENOMEM;
643         dev->tz = tz;
644         dev->cdev = cdev;
645         dev->trip = trip;
646         dev->upper = upper;
647         dev->upper_no_limit = upper_no_limit;
648         dev->lower = lower;
649         dev->target = THERMAL_NO_TARGET;
650         dev->weight = weight;
651
652         result = ida_alloc(&tz->ida, GFP_KERNEL);
653         if (result < 0)
654                 goto free_mem;
655
656         dev->id = result;
657         sprintf(dev->name, "cdev%d", dev->id);
658         result =
659             sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
660         if (result)
661                 goto release_ida;
662
663         snprintf(dev->attr_name, sizeof(dev->attr_name), "cdev%d_trip_point",
664                  dev->id);
665         sysfs_attr_init(&dev->attr.attr);
666         dev->attr.attr.name = dev->attr_name;
667         dev->attr.attr.mode = 0444;
668         dev->attr.show = trip_point_show;
669         result = device_create_file(&tz->device, &dev->attr);
670         if (result)
671                 goto remove_symbol_link;
672
673         snprintf(dev->weight_attr_name, sizeof(dev->weight_attr_name),
674                  "cdev%d_weight", dev->id);
675         sysfs_attr_init(&dev->weight_attr.attr);
676         dev->weight_attr.attr.name = dev->weight_attr_name;
677         dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
678         dev->weight_attr.show = weight_show;
679         dev->weight_attr.store = weight_store;
680         result = device_create_file(&tz->device, &dev->weight_attr);
681         if (result)
682                 goto remove_trip_file;
683
684         mutex_lock(&tz->lock);
685         mutex_lock(&cdev->lock);
686         list_for_each_entry(pos, &tz->thermal_instances, tz_node)
687                 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
688                         result = -EEXIST;
689                         break;
690                 }
691         if (!result) {
692                 list_add_tail(&dev->tz_node, &tz->thermal_instances);
693                 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
694                 atomic_set(&tz->need_update, 1);
695         }
696         mutex_unlock(&cdev->lock);
697         mutex_unlock(&tz->lock);
698
699         if (!result)
700                 return 0;
701
702         device_remove_file(&tz->device, &dev->weight_attr);
703 remove_trip_file:
704         device_remove_file(&tz->device, &dev->attr);
705 remove_symbol_link:
706         sysfs_remove_link(&tz->device.kobj, dev->name);
707 release_ida:
708         ida_free(&tz->ida, dev->id);
709 free_mem:
710         kfree(dev);
711         return result;
712 }
713 EXPORT_SYMBOL_GPL(thermal_bind_cdev_to_trip);
714
715 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
716                                      int trip_index,
717                                      struct thermal_cooling_device *cdev,
718                                      unsigned long upper, unsigned long lower,
719                                      unsigned int weight)
720 {
721         if (trip_index < 0 || trip_index >= tz->num_trips)
722                 return -EINVAL;
723
724         return thermal_bind_cdev_to_trip(tz, &tz->trips[trip_index], cdev,
725                                          upper, lower, weight);
726 }
727 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
728
729 /**
730  * thermal_unbind_cdev_from_trip - unbind a cooling device from a thermal zone.
731  * @tz:         pointer to a struct thermal_zone_device.
732  * @trip:       trip point the cooling devices is associated with in this zone.
733  * @cdev:       pointer to a struct thermal_cooling_device.
734  *
735  * This interface function unbind a thermal cooling device from the certain
736  * trip point of a thermal zone device.
737  * This function is usually called in the thermal zone device .unbind callback.
738  *
739  * Return: 0 on success, the proper error value otherwise.
740  */
741 int thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz,
742                                   const struct thermal_trip *trip,
743                                   struct thermal_cooling_device *cdev)
744 {
745         struct thermal_instance *pos, *next;
746
747         mutex_lock(&tz->lock);
748         mutex_lock(&cdev->lock);
749         list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
750                 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
751                         list_del(&pos->tz_node);
752                         list_del(&pos->cdev_node);
753                         mutex_unlock(&cdev->lock);
754                         mutex_unlock(&tz->lock);
755                         goto unbind;
756                 }
757         }
758         mutex_unlock(&cdev->lock);
759         mutex_unlock(&tz->lock);
760
761         return -ENODEV;
762
763 unbind:
764         device_remove_file(&tz->device, &pos->weight_attr);
765         device_remove_file(&tz->device, &pos->attr);
766         sysfs_remove_link(&tz->device.kobj, pos->name);
767         ida_free(&tz->ida, pos->id);
768         kfree(pos);
769         return 0;
770 }
771 EXPORT_SYMBOL_GPL(thermal_unbind_cdev_from_trip);
772
773 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
774                                        int trip_index,
775                                        struct thermal_cooling_device *cdev)
776 {
777         if (trip_index < 0 || trip_index >= tz->num_trips)
778                 return -EINVAL;
779
780         return thermal_unbind_cdev_from_trip(tz, &tz->trips[trip_index], cdev);
781 }
782 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
783
784 static void thermal_release(struct device *dev)
785 {
786         struct thermal_zone_device *tz;
787         struct thermal_cooling_device *cdev;
788
789         if (!strncmp(dev_name(dev), "thermal_zone",
790                      sizeof("thermal_zone") - 1)) {
791                 tz = to_thermal_zone(dev);
792                 thermal_zone_destroy_device_groups(tz);
793                 mutex_destroy(&tz->lock);
794                 kfree(tz);
795         } else if (!strncmp(dev_name(dev), "cooling_device",
796                             sizeof("cooling_device") - 1)) {
797                 cdev = to_cooling_device(dev);
798                 thermal_cooling_device_destroy_sysfs(cdev);
799                 kfree(cdev->type);
800                 ida_free(&thermal_cdev_ida, cdev->id);
801                 kfree(cdev);
802         }
803 }
804
805 static struct class *thermal_class;
806
807 static inline
808 void print_bind_err_msg(struct thermal_zone_device *tz,
809                         struct thermal_cooling_device *cdev, int ret)
810 {
811         dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
812                 tz->type, cdev->type, ret);
813 }
814
815 static void bind_cdev(struct thermal_cooling_device *cdev)
816 {
817         int ret;
818         struct thermal_zone_device *pos = NULL;
819
820         list_for_each_entry(pos, &thermal_tz_list, node) {
821                 if (pos->ops->bind) {
822                         ret = pos->ops->bind(pos, cdev);
823                         if (ret)
824                                 print_bind_err_msg(pos, cdev, ret);
825                 }
826         }
827 }
828
829 /**
830  * __thermal_cooling_device_register() - register a new thermal cooling device
831  * @np:         a pointer to a device tree node.
832  * @type:       the thermal cooling device type.
833  * @devdata:    device private data.
834  * @ops:                standard thermal cooling devices callbacks.
835  *
836  * This interface function adds a new thermal cooling device (fan/processor/...)
837  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
838  * to all the thermal zone devices registered at the same time.
839  * It also gives the opportunity to link the cooling device to a device tree
840  * node, so that it can be bound to a thermal zone created out of device tree.
841  *
842  * Return: a pointer to the created struct thermal_cooling_device or an
843  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
844  */
845 static struct thermal_cooling_device *
846 __thermal_cooling_device_register(struct device_node *np,
847                                   const char *type, void *devdata,
848                                   const struct thermal_cooling_device_ops *ops)
849 {
850         struct thermal_cooling_device *cdev;
851         struct thermal_zone_device *pos = NULL;
852         int id, ret;
853
854         if (!ops || !ops->get_max_state || !ops->get_cur_state ||
855             !ops->set_cur_state)
856                 return ERR_PTR(-EINVAL);
857
858         if (!thermal_class)
859                 return ERR_PTR(-ENODEV);
860
861         cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
862         if (!cdev)
863                 return ERR_PTR(-ENOMEM);
864
865         ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL);
866         if (ret < 0)
867                 goto out_kfree_cdev;
868         cdev->id = ret;
869         id = ret;
870
871         cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
872         if (!cdev->type) {
873                 ret = -ENOMEM;
874                 goto out_ida_remove;
875         }
876
877         mutex_init(&cdev->lock);
878         INIT_LIST_HEAD(&cdev->thermal_instances);
879         cdev->np = np;
880         cdev->ops = ops;
881         cdev->updated = false;
882         cdev->device.class = thermal_class;
883         cdev->devdata = devdata;
884
885         ret = cdev->ops->get_max_state(cdev, &cdev->max_state);
886         if (ret)
887                 goto out_cdev_type;
888
889         thermal_cooling_device_setup_sysfs(cdev);
890
891         ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
892         if (ret)
893                 goto out_cooling_dev;
894
895         ret = device_register(&cdev->device);
896         if (ret) {
897                 /* thermal_release() handles rest of the cleanup */
898                 put_device(&cdev->device);
899                 return ERR_PTR(ret);
900         }
901
902         /* Add 'this' new cdev to the global cdev list */
903         mutex_lock(&thermal_list_lock);
904
905         list_add(&cdev->node, &thermal_cdev_list);
906
907         /* Update binding information for 'this' new cdev */
908         bind_cdev(cdev);
909
910         list_for_each_entry(pos, &thermal_tz_list, node)
911                 if (atomic_cmpxchg(&pos->need_update, 1, 0))
912                         thermal_zone_device_update(pos,
913                                                    THERMAL_EVENT_UNSPECIFIED);
914
915         mutex_unlock(&thermal_list_lock);
916
917         return cdev;
918
919 out_cooling_dev:
920         thermal_cooling_device_destroy_sysfs(cdev);
921 out_cdev_type:
922         kfree(cdev->type);
923 out_ida_remove:
924         ida_free(&thermal_cdev_ida, id);
925 out_kfree_cdev:
926         kfree(cdev);
927         return ERR_PTR(ret);
928 }
929
930 /**
931  * thermal_cooling_device_register() - register a new thermal cooling device
932  * @type:       the thermal cooling device type.
933  * @devdata:    device private data.
934  * @ops:                standard thermal cooling devices callbacks.
935  *
936  * This interface function adds a new thermal cooling device (fan/processor/...)
937  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
938  * to all the thermal zone devices registered at the same time.
939  *
940  * Return: a pointer to the created struct thermal_cooling_device or an
941  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
942  */
943 struct thermal_cooling_device *
944 thermal_cooling_device_register(const char *type, void *devdata,
945                                 const struct thermal_cooling_device_ops *ops)
946 {
947         return __thermal_cooling_device_register(NULL, type, devdata, ops);
948 }
949 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
950
951 /**
952  * thermal_of_cooling_device_register() - register an OF thermal cooling device
953  * @np:         a pointer to a device tree node.
954  * @type:       the thermal cooling device type.
955  * @devdata:    device private data.
956  * @ops:                standard thermal cooling devices callbacks.
957  *
958  * This function will register a cooling device with device tree node reference.
959  * This interface function adds a new thermal cooling device (fan/processor/...)
960  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
961  * to all the thermal zone devices registered at the same time.
962  *
963  * Return: a pointer to the created struct thermal_cooling_device or an
964  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
965  */
966 struct thermal_cooling_device *
967 thermal_of_cooling_device_register(struct device_node *np,
968                                    const char *type, void *devdata,
969                                    const struct thermal_cooling_device_ops *ops)
970 {
971         return __thermal_cooling_device_register(np, type, devdata, ops);
972 }
973 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
974
975 static void thermal_cooling_device_release(struct device *dev, void *res)
976 {
977         thermal_cooling_device_unregister(
978                                 *(struct thermal_cooling_device **)res);
979 }
980
981 /**
982  * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
983  *                                             device
984  * @dev:        a valid struct device pointer of a sensor device.
985  * @np:         a pointer to a device tree node.
986  * @type:       the thermal cooling device type.
987  * @devdata:    device private data.
988  * @ops:        standard thermal cooling devices callbacks.
989  *
990  * This function will register a cooling device with device tree node reference.
991  * This interface function adds a new thermal cooling device (fan/processor/...)
992  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
993  * to all the thermal zone devices registered at the same time.
994  *
995  * Return: a pointer to the created struct thermal_cooling_device or an
996  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
997  */
998 struct thermal_cooling_device *
999 devm_thermal_of_cooling_device_register(struct device *dev,
1000                                 struct device_node *np,
1001                                 char *type, void *devdata,
1002                                 const struct thermal_cooling_device_ops *ops)
1003 {
1004         struct thermal_cooling_device **ptr, *tcd;
1005
1006         ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1007                            GFP_KERNEL);
1008         if (!ptr)
1009                 return ERR_PTR(-ENOMEM);
1010
1011         tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1012         if (IS_ERR(tcd)) {
1013                 devres_free(ptr);
1014                 return tcd;
1015         }
1016
1017         *ptr = tcd;
1018         devres_add(dev, ptr);
1019
1020         return tcd;
1021 }
1022 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1023
1024 static bool thermal_cooling_device_present(struct thermal_cooling_device *cdev)
1025 {
1026         struct thermal_cooling_device *pos = NULL;
1027
1028         list_for_each_entry(pos, &thermal_cdev_list, node) {
1029                 if (pos == cdev)
1030                         return true;
1031         }
1032
1033         return false;
1034 }
1035
1036 /**
1037  * thermal_cooling_device_update - Update a cooling device object
1038  * @cdev: Target cooling device.
1039  *
1040  * Update @cdev to reflect a change of the underlying hardware or platform.
1041  *
1042  * Must be called when the maximum cooling state of @cdev becomes invalid and so
1043  * its .get_max_state() callback needs to be run to produce the new maximum
1044  * cooling state value.
1045  */
1046 void thermal_cooling_device_update(struct thermal_cooling_device *cdev)
1047 {
1048         struct thermal_instance *ti;
1049         unsigned long state;
1050
1051         if (IS_ERR_OR_NULL(cdev))
1052                 return;
1053
1054         /*
1055          * Hold thermal_list_lock throughout the update to prevent the device
1056          * from going away while being updated.
1057          */
1058         mutex_lock(&thermal_list_lock);
1059
1060         if (!thermal_cooling_device_present(cdev))
1061                 goto unlock_list;
1062
1063         /*
1064          * Update under the cdev lock to prevent the state from being set beyond
1065          * the new limit concurrently.
1066          */
1067         mutex_lock(&cdev->lock);
1068
1069         if (cdev->ops->get_max_state(cdev, &cdev->max_state))
1070                 goto unlock;
1071
1072         thermal_cooling_device_stats_reinit(cdev);
1073
1074         list_for_each_entry(ti, &cdev->thermal_instances, cdev_node) {
1075                 if (ti->upper == cdev->max_state)
1076                         continue;
1077
1078                 if (ti->upper < cdev->max_state) {
1079                         if (ti->upper_no_limit)
1080                                 ti->upper = cdev->max_state;
1081
1082                         continue;
1083                 }
1084
1085                 ti->upper = cdev->max_state;
1086                 if (ti->lower > ti->upper)
1087                         ti->lower = ti->upper;
1088
1089                 if (ti->target == THERMAL_NO_TARGET)
1090                         continue;
1091
1092                 if (ti->target > ti->upper)
1093                         ti->target = ti->upper;
1094         }
1095
1096         if (cdev->ops->get_cur_state(cdev, &state) || state > cdev->max_state)
1097                 goto unlock;
1098
1099         thermal_cooling_device_stats_update(cdev, state);
1100
1101 unlock:
1102         mutex_unlock(&cdev->lock);
1103
1104 unlock_list:
1105         mutex_unlock(&thermal_list_lock);
1106 }
1107 EXPORT_SYMBOL_GPL(thermal_cooling_device_update);
1108
1109 /**
1110  * thermal_cooling_device_unregister - removes a thermal cooling device
1111  * @cdev:       the thermal cooling device to remove.
1112  *
1113  * thermal_cooling_device_unregister() must be called when a registered
1114  * thermal cooling device is no longer needed.
1115  */
1116 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1117 {
1118         struct thermal_zone_device *tz;
1119
1120         if (!cdev)
1121                 return;
1122
1123         mutex_lock(&thermal_list_lock);
1124
1125         if (!thermal_cooling_device_present(cdev)) {
1126                 mutex_unlock(&thermal_list_lock);
1127                 return;
1128         }
1129
1130         list_del(&cdev->node);
1131
1132         /* Unbind all thermal zones associated with 'this' cdev */
1133         list_for_each_entry(tz, &thermal_tz_list, node) {
1134                 if (tz->ops->unbind)
1135                         tz->ops->unbind(tz, cdev);
1136         }
1137
1138         mutex_unlock(&thermal_list_lock);
1139
1140         device_unregister(&cdev->device);
1141 }
1142 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1143
1144 static void bind_tz(struct thermal_zone_device *tz)
1145 {
1146         int ret;
1147         struct thermal_cooling_device *pos = NULL;
1148
1149         if (!tz->ops->bind)
1150                 return;
1151
1152         mutex_lock(&thermal_list_lock);
1153
1154         list_for_each_entry(pos, &thermal_cdev_list, node) {
1155                 ret = tz->ops->bind(tz, pos);
1156                 if (ret)
1157                         print_bind_err_msg(tz, pos, ret);
1158         }
1159
1160         mutex_unlock(&thermal_list_lock);
1161 }
1162
1163 static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms)
1164 {
1165         *delay_jiffies = msecs_to_jiffies(delay_ms);
1166         if (delay_ms > 1000)
1167                 *delay_jiffies = round_jiffies(*delay_jiffies);
1168 }
1169
1170 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
1171 {
1172         int i, ret = -EINVAL;
1173
1174         if (tz->ops->get_crit_temp)
1175                 return tz->ops->get_crit_temp(tz, temp);
1176
1177         if (!tz->trips)
1178                 return -EINVAL;
1179
1180         mutex_lock(&tz->lock);
1181
1182         for (i = 0; i < tz->num_trips; i++) {
1183                 if (tz->trips[i].type == THERMAL_TRIP_CRITICAL) {
1184                         *temp = tz->trips[i].temperature;
1185                         ret = 0;
1186                         break;
1187                 }
1188         }
1189
1190         mutex_unlock(&tz->lock);
1191
1192         return ret;
1193 }
1194 EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp);
1195
1196 /**
1197  * thermal_zone_device_register_with_trips() - register a new thermal zone device
1198  * @type:       the thermal zone device type
1199  * @trips:      a pointer to an array of thermal trips
1200  * @num_trips:  the number of trip points the thermal zone support
1201  * @mask:       a bit string indicating the writeablility of trip points
1202  * @devdata:    private device data
1203  * @ops:        standard thermal zone device callbacks
1204  * @tzp:        thermal zone platform parameters
1205  * @passive_delay: number of milliseconds to wait between polls when
1206  *                 performing passive cooling
1207  * @polling_delay: number of milliseconds to wait between polls when checking
1208  *                 whether trip points have been crossed (0 for interrupt
1209  *                 driven systems)
1210  *
1211  * This interface function adds a new thermal zone device (sensor) to
1212  * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1213  * thermal cooling devices registered at the same time.
1214  * thermal_zone_device_unregister() must be called when the device is no
1215  * longer needed. The passive cooling depends on the .get_trend() return value.
1216  *
1217  * Return: a pointer to the created struct thermal_zone_device or an
1218  * in case of error, an ERR_PTR. Caller must check return value with
1219  * IS_ERR*() helpers.
1220  */
1221 struct thermal_zone_device *
1222 thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *trips, int num_trips, int mask,
1223                                         void *devdata, struct thermal_zone_device_ops *ops,
1224                                         const struct thermal_zone_params *tzp, int passive_delay,
1225                                         int polling_delay)
1226 {
1227         struct thermal_zone_device *tz;
1228         int id;
1229         int result;
1230         struct thermal_governor *governor;
1231
1232         if (!type || strlen(type) == 0) {
1233                 pr_err("No thermal zone type defined\n");
1234                 return ERR_PTR(-EINVAL);
1235         }
1236
1237         if (strlen(type) >= THERMAL_NAME_LENGTH) {
1238                 pr_err("Thermal zone name (%s) too long, should be under %d chars\n",
1239                        type, THERMAL_NAME_LENGTH);
1240                 return ERR_PTR(-EINVAL);
1241         }
1242
1243         /*
1244          * Max trip count can't exceed 31 as the "mask >> num_trips" condition.
1245          * For example, shifting by 32 will result in compiler warning:
1246          * warning: right shift count >= width of type [-Wshift-count- overflow]
1247          *
1248          * Also "mask >> num_trips" will always be true with 32 bit shift.
1249          * E.g. mask = 0x80000000 for trip id 31 to be RW. Then
1250          * mask >> 32 = 0x80000000
1251          * This will result in failure for the below condition.
1252          *
1253          * Check will be true when the bit 31 of the mask is set.
1254          * 32 bit shift will cause overflow of 4 byte integer.
1255          */
1256         if (num_trips > (BITS_PER_TYPE(int) - 1) || num_trips < 0 || mask >> num_trips) {
1257                 pr_err("Incorrect number of thermal trips\n");
1258                 return ERR_PTR(-EINVAL);
1259         }
1260
1261         if (!ops) {
1262                 pr_err("Thermal zone device ops not defined\n");
1263                 return ERR_PTR(-EINVAL);
1264         }
1265
1266         if (num_trips > 0 && !trips)
1267                 return ERR_PTR(-EINVAL);
1268
1269         if (!thermal_class)
1270                 return ERR_PTR(-ENODEV);
1271
1272         tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1273         if (!tz)
1274                 return ERR_PTR(-ENOMEM);
1275
1276         if (tzp) {
1277                 tz->tzp = kmemdup(tzp, sizeof(*tzp), GFP_KERNEL);
1278                 if (!tz->tzp) {
1279                         result = -ENOMEM;
1280                         goto free_tz;
1281                 }
1282         }
1283
1284         INIT_LIST_HEAD(&tz->thermal_instances);
1285         ida_init(&tz->ida);
1286         mutex_init(&tz->lock);
1287         id = ida_alloc(&thermal_tz_ida, GFP_KERNEL);
1288         if (id < 0) {
1289                 result = id;
1290                 goto free_tzp;
1291         }
1292
1293         tz->id = id;
1294         strscpy(tz->type, type, sizeof(tz->type));
1295
1296         if (!ops->critical)
1297                 ops->critical = thermal_zone_device_critical;
1298
1299         tz->ops = ops;
1300         tz->device.class = thermal_class;
1301         tz->devdata = devdata;
1302         tz->trips = trips;
1303         tz->num_trips = num_trips;
1304
1305         thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay);
1306         thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay);
1307
1308         /* sys I/F */
1309         /* Add nodes that are always present via .groups */
1310         result = thermal_zone_create_device_groups(tz, mask);
1311         if (result)
1312                 goto remove_id;
1313
1314         /* A new thermal zone needs to be updated anyway. */
1315         atomic_set(&tz->need_update, 1);
1316
1317         result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1318         if (result) {
1319                 thermal_zone_destroy_device_groups(tz);
1320                 goto remove_id;
1321         }
1322         result = device_register(&tz->device);
1323         if (result)
1324                 goto release_device;
1325
1326         /* Update 'this' zone's governor information */
1327         mutex_lock(&thermal_governor_lock);
1328
1329         if (tz->tzp)
1330                 governor = __find_governor(tz->tzp->governor_name);
1331         else
1332                 governor = def_governor;
1333
1334         result = thermal_set_governor(tz, governor);
1335         if (result) {
1336                 mutex_unlock(&thermal_governor_lock);
1337                 goto unregister;
1338         }
1339
1340         mutex_unlock(&thermal_governor_lock);
1341
1342         if (!tz->tzp || !tz->tzp->no_hwmon) {
1343                 result = thermal_add_hwmon_sysfs(tz);
1344                 if (result)
1345                         goto unregister;
1346         }
1347
1348         mutex_lock(&thermal_list_lock);
1349         list_add_tail(&tz->node, &thermal_tz_list);
1350         mutex_unlock(&thermal_list_lock);
1351
1352         /* Bind cooling devices for this zone */
1353         bind_tz(tz);
1354
1355         INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1356
1357         thermal_zone_device_init(tz);
1358         /* Update the new thermal zone and mark it as already updated. */
1359         if (atomic_cmpxchg(&tz->need_update, 1, 0))
1360                 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1361
1362         thermal_notify_tz_create(tz->id, tz->type);
1363
1364         return tz;
1365
1366 unregister:
1367         device_del(&tz->device);
1368 release_device:
1369         put_device(&tz->device);
1370 remove_id:
1371         ida_free(&thermal_tz_ida, id);
1372 free_tzp:
1373         kfree(tz->tzp);
1374 free_tz:
1375         kfree(tz);
1376         return ERR_PTR(result);
1377 }
1378 EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips);
1379
1380 struct thermal_zone_device *thermal_tripless_zone_device_register(
1381                                         const char *type,
1382                                         void *devdata,
1383                                         struct thermal_zone_device_ops *ops,
1384                                         const struct thermal_zone_params *tzp)
1385 {
1386         return thermal_zone_device_register_with_trips(type, NULL, 0, 0, devdata,
1387                                                        ops, tzp, 0, 0);
1388 }
1389 EXPORT_SYMBOL_GPL(thermal_tripless_zone_device_register);
1390
1391 void *thermal_zone_device_priv(struct thermal_zone_device *tzd)
1392 {
1393         return tzd->devdata;
1394 }
1395 EXPORT_SYMBOL_GPL(thermal_zone_device_priv);
1396
1397 const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
1398 {
1399         return tzd->type;
1400 }
1401 EXPORT_SYMBOL_GPL(thermal_zone_device_type);
1402
1403 int thermal_zone_device_id(struct thermal_zone_device *tzd)
1404 {
1405         return tzd->id;
1406 }
1407 EXPORT_SYMBOL_GPL(thermal_zone_device_id);
1408
1409 struct device *thermal_zone_device(struct thermal_zone_device *tzd)
1410 {
1411         return &tzd->device;
1412 }
1413 EXPORT_SYMBOL_GPL(thermal_zone_device);
1414
1415 /**
1416  * thermal_zone_device_unregister - removes the registered thermal zone device
1417  * @tz: the thermal zone device to remove
1418  */
1419 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1420 {
1421         int tz_id;
1422         struct thermal_cooling_device *cdev;
1423         struct thermal_zone_device *pos = NULL;
1424
1425         if (!tz)
1426                 return;
1427
1428         tz_id = tz->id;
1429
1430         mutex_lock(&thermal_list_lock);
1431         list_for_each_entry(pos, &thermal_tz_list, node)
1432                 if (pos == tz)
1433                         break;
1434         if (pos != tz) {
1435                 /* thermal zone device not found */
1436                 mutex_unlock(&thermal_list_lock);
1437                 return;
1438         }
1439         list_del(&tz->node);
1440
1441         /* Unbind all cdevs associated with 'this' thermal zone */
1442         list_for_each_entry(cdev, &thermal_cdev_list, node)
1443                 if (tz->ops->unbind)
1444                         tz->ops->unbind(tz, cdev);
1445
1446         mutex_unlock(&thermal_list_lock);
1447
1448         cancel_delayed_work_sync(&tz->poll_queue);
1449
1450         thermal_set_governor(tz, NULL);
1451
1452         thermal_remove_hwmon_sysfs(tz);
1453         ida_free(&thermal_tz_ida, tz->id);
1454         ida_destroy(&tz->ida);
1455
1456         mutex_lock(&tz->lock);
1457         device_del(&tz->device);
1458         mutex_unlock(&tz->lock);
1459
1460         kfree(tz->tzp);
1461
1462         put_device(&tz->device);
1463
1464         thermal_notify_tz_delete(tz_id);
1465 }
1466 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1467
1468 /**
1469  * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1470  * @name: thermal zone name to fetch the temperature
1471  *
1472  * When only one zone is found with the passed name, returns a reference to it.
1473  *
1474  * Return: On success returns a reference to an unique thermal zone with
1475  * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1476  * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1477  */
1478 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1479 {
1480         struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1481         unsigned int found = 0;
1482
1483         if (!name)
1484                 goto exit;
1485
1486         mutex_lock(&thermal_list_lock);
1487         list_for_each_entry(pos, &thermal_tz_list, node)
1488                 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1489                         found++;
1490                         ref = pos;
1491                 }
1492         mutex_unlock(&thermal_list_lock);
1493
1494         /* nothing has been found, thus an error code for it */
1495         if (found == 0)
1496                 ref = ERR_PTR(-ENODEV);
1497         else if (found > 1)
1498         /* Success only when an unique zone is found */
1499                 ref = ERR_PTR(-EEXIST);
1500
1501 exit:
1502         return ref;
1503 }
1504 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1505
1506 static int thermal_pm_notify(struct notifier_block *nb,
1507                              unsigned long mode, void *_unused)
1508 {
1509         struct thermal_zone_device *tz;
1510
1511         switch (mode) {
1512         case PM_HIBERNATION_PREPARE:
1513         case PM_RESTORE_PREPARE:
1514         case PM_SUSPEND_PREPARE:
1515                 mutex_lock(&thermal_list_lock);
1516
1517                 list_for_each_entry(tz, &thermal_tz_list, node) {
1518                         mutex_lock(&tz->lock);
1519
1520                         tz->suspended = true;
1521
1522                         mutex_unlock(&tz->lock);
1523                 }
1524
1525                 mutex_unlock(&thermal_list_lock);
1526                 break;
1527         case PM_POST_HIBERNATION:
1528         case PM_POST_RESTORE:
1529         case PM_POST_SUSPEND:
1530                 mutex_lock(&thermal_list_lock);
1531
1532                 list_for_each_entry(tz, &thermal_tz_list, node) {
1533                         mutex_lock(&tz->lock);
1534
1535                         tz->suspended = false;
1536
1537                         thermal_zone_device_init(tz);
1538                         __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1539
1540                         mutex_unlock(&tz->lock);
1541                 }
1542
1543                 mutex_unlock(&thermal_list_lock);
1544                 break;
1545         default:
1546                 break;
1547         }
1548         return 0;
1549 }
1550
1551 static struct notifier_block thermal_pm_nb = {
1552         .notifier_call = thermal_pm_notify,
1553 };
1554
1555 static int __init thermal_init(void)
1556 {
1557         int result;
1558
1559         result = thermal_netlink_init();
1560         if (result)
1561                 goto error;
1562
1563         result = thermal_register_governors();
1564         if (result)
1565                 goto unregister_netlink;
1566
1567         thermal_class = kzalloc(sizeof(*thermal_class), GFP_KERNEL);
1568         if (!thermal_class) {
1569                 result = -ENOMEM;
1570                 goto unregister_governors;
1571         }
1572
1573         thermal_class->name = "thermal";
1574         thermal_class->dev_release = thermal_release;
1575
1576         result = class_register(thermal_class);
1577         if (result) {
1578                 kfree(thermal_class);
1579                 thermal_class = NULL;
1580                 goto unregister_governors;
1581         }
1582
1583         result = register_pm_notifier(&thermal_pm_nb);
1584         if (result)
1585                 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1586                         result);
1587
1588         return 0;
1589
1590 unregister_governors:
1591         thermal_unregister_governors();
1592 unregister_netlink:
1593         thermal_netlink_exit();
1594 error:
1595         mutex_destroy(&thermal_list_lock);
1596         mutex_destroy(&thermal_governor_lock);
1597         return result;
1598 }
1599 postcore_initcall(thermal_init);