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