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