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