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