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