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