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 #include <trace/hooks/thermal.h>
25
26 #define CREATE_TRACE_POINTS
27 #include "thermal_trace.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 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 if (!device_is_registered(&tz->device))
207 goto exit;
208
209 gov = __find_governor(strim(policy));
210 if (!gov)
211 goto exit;
212
213 ret = thermal_set_governor(tz, gov);
214
215 exit:
216 mutex_unlock(&tz->lock);
217 mutex_unlock(&thermal_governor_lock);
218
219 thermal_notify_tz_gov_change(tz->id, policy);
220
221 return ret;
222 }
223
thermal_build_list_of_policies(char * buf)224 int thermal_build_list_of_policies(char *buf)
225 {
226 struct thermal_governor *pos;
227 ssize_t count = 0;
228
229 mutex_lock(&thermal_governor_lock);
230
231 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
232 count += sysfs_emit_at(buf, count, "%s ", pos->name);
233 }
234 count += sysfs_emit_at(buf, 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_id)346 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip_id)
347 {
348 struct thermal_trip trip;
349
350 /* Ignore disabled trip points */
351 if (test_bit(trip_id, &tz->trips_disabled))
352 return;
353
354 __thermal_zone_get_trip(tz, trip_id, &trip);
355
356 if (trip.temperature == THERMAL_TEMP_INVALID)
357 return;
358
359 if (tz->last_temperature != THERMAL_TEMP_INVALID) {
360 if (tz->last_temperature < trip.temperature &&
361 tz->temperature >= trip.temperature)
362 thermal_notify_tz_trip_up(tz->id, trip_id,
363 tz->temperature);
364 if (tz->last_temperature >= trip.temperature &&
365 tz->temperature < (trip.temperature - trip.hysteresis))
366 thermal_notify_tz_trip_down(tz->id, trip_id,
367 tz->temperature);
368 }
369
370 if (trip.type == THERMAL_TRIP_CRITICAL || trip.type == THERMAL_TRIP_HOT)
371 handle_critical_trips(tz, trip_id, trip.temperature, trip.type);
372 else
373 handle_non_critical_trips(tz, trip_id);
374 }
375
update_temperature(struct thermal_zone_device * tz)376 static void update_temperature(struct thermal_zone_device *tz)
377 {
378 int temp, ret;
379
380 ret = __thermal_zone_get_temp(tz, &temp);
381 if (ret) {
382 if (ret != -EAGAIN)
383 dev_warn(&tz->device,
384 "failed to read out thermal zone (%d)\n",
385 ret);
386 return;
387 }
388
389 tz->last_temperature = tz->temperature;
390 tz->temperature = temp;
391
392 trace_thermal_temperature(tz);
393
394 thermal_genl_sampling_temp(tz->id, temp);
395 }
396
thermal_zone_device_init(struct thermal_zone_device * tz)397 static void thermal_zone_device_init(struct thermal_zone_device *tz)
398 {
399 struct thermal_instance *pos;
400 tz->temperature = THERMAL_TEMP_INVALID;
401 tz->prev_low_trip = -INT_MAX;
402 tz->prev_high_trip = INT_MAX;
403 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
404 pos->initialized = false;
405 }
406
__thermal_zone_device_update(struct thermal_zone_device * tz,enum thermal_notify_event event)407 void __thermal_zone_device_update(struct thermal_zone_device *tz,
408 enum thermal_notify_event event)
409 {
410 int count;
411
412 if (tz->suspended)
413 return;
414
415 if (WARN_ONCE(!tz->ops->get_temp,
416 "'%s' must not be called without 'get_temp' ops set\n",
417 __func__))
418 return;
419
420 if (!thermal_zone_device_is_enabled(tz))
421 return;
422
423 update_temperature(tz);
424
425 __thermal_zone_set_trips(tz);
426
427 tz->notify_event = event;
428
429 for (count = 0; count < tz->num_trips; count++)
430 handle_thermal_trip(tz, count);
431
432 monitor_thermal_zone(tz);
433 }
434
thermal_zone_device_set_mode(struct thermal_zone_device * tz,enum thermal_device_mode mode)435 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
436 enum thermal_device_mode mode)
437 {
438 int ret = 0;
439
440 mutex_lock(&tz->lock);
441
442 /* do nothing if mode isn't changing */
443 if (mode == tz->mode) {
444 mutex_unlock(&tz->lock);
445
446 return ret;
447 }
448
449 if (!device_is_registered(&tz->device)) {
450 mutex_unlock(&tz->lock);
451
452 return -ENODEV;
453 }
454
455 if (tz->ops->change_mode)
456 ret = tz->ops->change_mode(tz, mode);
457
458 if (!ret)
459 tz->mode = mode;
460
461 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
462
463 mutex_unlock(&tz->lock);
464
465 if (mode == THERMAL_DEVICE_ENABLED)
466 thermal_notify_tz_enable(tz->id);
467 else
468 thermal_notify_tz_disable(tz->id);
469
470 return ret;
471 }
472
thermal_zone_device_enable(struct thermal_zone_device * tz)473 int thermal_zone_device_enable(struct thermal_zone_device *tz)
474 {
475 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
476 }
477 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
478
thermal_zone_device_disable(struct thermal_zone_device * tz)479 int thermal_zone_device_disable(struct thermal_zone_device *tz)
480 {
481 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
482 }
483 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
484
thermal_zone_device_is_enabled(struct thermal_zone_device * tz)485 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
486 {
487 lockdep_assert_held(&tz->lock);
488
489 return tz->mode == THERMAL_DEVICE_ENABLED;
490 }
491
thermal_zone_device_update(struct thermal_zone_device * tz,enum thermal_notify_event event)492 void thermal_zone_device_update(struct thermal_zone_device *tz,
493 enum thermal_notify_event event)
494 {
495 mutex_lock(&tz->lock);
496 if (device_is_registered(&tz->device))
497 __thermal_zone_device_update(tz, event);
498 mutex_unlock(&tz->lock);
499 }
500 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
501
502 /**
503 * thermal_zone_device_exec - Run a callback under the zone lock.
504 * @tz: Thermal zone.
505 * @cb: Callback to run.
506 * @data: Data to pass to the callback.
507 */
thermal_zone_device_exec(struct thermal_zone_device * tz,void (* cb)(struct thermal_zone_device *,unsigned long),unsigned long data)508 void thermal_zone_device_exec(struct thermal_zone_device *tz,
509 void (*cb)(struct thermal_zone_device *,
510 unsigned long),
511 unsigned long data)
512 {
513 mutex_lock(&tz->lock);
514
515 cb(tz, data);
516
517 mutex_unlock(&tz->lock);
518 }
519 EXPORT_SYMBOL_GPL(thermal_zone_device_exec);
520
thermal_zone_device_check(struct work_struct * work)521 static void thermal_zone_device_check(struct work_struct *work)
522 {
523 struct thermal_zone_device *tz = container_of(work, struct
524 thermal_zone_device,
525 poll_queue.work);
526 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
527 }
528
for_each_thermal_governor(int (* cb)(struct thermal_governor *,void *),void * data)529 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
530 void *data)
531 {
532 struct thermal_governor *gov;
533 int ret = 0;
534
535 mutex_lock(&thermal_governor_lock);
536 list_for_each_entry(gov, &thermal_governor_list, governor_list) {
537 ret = cb(gov, data);
538 if (ret)
539 break;
540 }
541 mutex_unlock(&thermal_governor_lock);
542
543 return ret;
544 }
545
for_each_thermal_cooling_device(int (* cb)(struct thermal_cooling_device *,void *),void * data)546 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
547 void *), void *data)
548 {
549 struct thermal_cooling_device *cdev;
550 int ret = 0;
551
552 mutex_lock(&thermal_list_lock);
553 list_for_each_entry(cdev, &thermal_cdev_list, node) {
554 ret = cb(cdev, data);
555 if (ret)
556 break;
557 }
558 mutex_unlock(&thermal_list_lock);
559
560 return ret;
561 }
562
for_each_thermal_zone(int (* cb)(struct thermal_zone_device *,void *),void * data)563 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
564 void *data)
565 {
566 struct thermal_zone_device *tz;
567 int ret = 0;
568
569 mutex_lock(&thermal_list_lock);
570 list_for_each_entry(tz, &thermal_tz_list, node) {
571 ret = cb(tz, data);
572 if (ret)
573 break;
574 }
575 mutex_unlock(&thermal_list_lock);
576
577 return ret;
578 }
579
thermal_zone_get_by_id(int id)580 struct thermal_zone_device *thermal_zone_get_by_id(int id)
581 {
582 struct thermal_zone_device *tz, *match = NULL;
583
584 mutex_lock(&thermal_list_lock);
585 list_for_each_entry(tz, &thermal_tz_list, node) {
586 if (tz->id == id) {
587 match = tz;
588 break;
589 }
590 }
591 mutex_unlock(&thermal_list_lock);
592
593 return match;
594 }
595
596 /*
597 * Device management section: cooling devices, zones devices, and binding
598 *
599 * Set of functions provided by the thermal core for:
600 * - cooling devices lifecycle: registration, unregistration,
601 * binding, and unbinding.
602 * - thermal zone devices lifecycle: registration, unregistration,
603 * binding, and unbinding.
604 */
605
606 /**
607 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
608 * @tz: pointer to struct thermal_zone_device
609 * @trip_index: indicates which trip point the cooling devices is
610 * associated with in this thermal zone.
611 * @cdev: pointer to struct thermal_cooling_device
612 * @upper: the Maximum cooling state for this trip point.
613 * THERMAL_NO_LIMIT means no upper limit,
614 * and the cooling device can be in max_state.
615 * @lower: the Minimum cooling state can be used for this trip point.
616 * THERMAL_NO_LIMIT means no lower limit,
617 * and the cooling device can be in cooling state 0.
618 * @weight: The weight of the cooling device to be bound to the
619 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
620 * default value
621 *
622 * This interface function bind a thermal cooling device to the certain trip
623 * point of a thermal zone device.
624 * This function is usually called in the thermal zone device .bind callback.
625 *
626 * Return: 0 on success, the proper error value otherwise.
627 */
thermal_zone_bind_cooling_device(struct thermal_zone_device * tz,int trip_index,struct thermal_cooling_device * cdev,unsigned long upper,unsigned long lower,unsigned int weight)628 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
629 int trip_index,
630 struct thermal_cooling_device *cdev,
631 unsigned long upper, unsigned long lower,
632 unsigned int weight)
633 {
634 struct thermal_instance *dev;
635 struct thermal_instance *pos;
636 struct thermal_zone_device *pos1;
637 struct thermal_cooling_device *pos2;
638 const struct thermal_trip *trip;
639 bool upper_no_limit;
640 int result;
641
642 if (trip_index >= tz->num_trips || trip_index < 0)
643 return -EINVAL;
644
645 trip = &tz->trips[trip_index];
646
647 list_for_each_entry(pos1, &thermal_tz_list, node) {
648 if (pos1 == tz)
649 break;
650 }
651 list_for_each_entry(pos2, &thermal_cdev_list, node) {
652 if (pos2 == cdev)
653 break;
654 }
655
656 if (tz != pos1 || cdev != pos2)
657 return -EINVAL;
658
659 /* lower default 0, upper default max_state */
660 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
661
662 if (upper == THERMAL_NO_LIMIT) {
663 upper = cdev->max_state;
664 upper_no_limit = true;
665 } else {
666 upper_no_limit = false;
667 }
668
669 if (lower > upper || upper > cdev->max_state)
670 return -EINVAL;
671
672 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
673 if (!dev)
674 return -ENOMEM;
675 dev->tz = tz;
676 dev->cdev = cdev;
677 dev->trip = trip;
678 dev->upper = upper;
679 dev->upper_no_limit = upper_no_limit;
680 dev->lower = lower;
681 dev->target = THERMAL_NO_TARGET;
682 dev->weight = weight;
683
684 result = ida_alloc(&tz->ida, GFP_KERNEL);
685 if (result < 0)
686 goto free_mem;
687
688 dev->id = result;
689 sprintf(dev->name, "cdev%d", dev->id);
690 result =
691 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
692 if (result)
693 goto release_ida;
694
695 snprintf(dev->attr_name, sizeof(dev->attr_name), "cdev%d_trip_point",
696 dev->id);
697 sysfs_attr_init(&dev->attr.attr);
698 dev->attr.attr.name = dev->attr_name;
699 dev->attr.attr.mode = 0444;
700 dev->attr.show = trip_point_show;
701 result = device_create_file(&tz->device, &dev->attr);
702 if (result)
703 goto remove_symbol_link;
704
705 snprintf(dev->weight_attr_name, sizeof(dev->weight_attr_name),
706 "cdev%d_weight", dev->id);
707 sysfs_attr_init(&dev->weight_attr.attr);
708 dev->weight_attr.attr.name = dev->weight_attr_name;
709 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
710 dev->weight_attr.show = weight_show;
711 dev->weight_attr.store = weight_store;
712 result = device_create_file(&tz->device, &dev->weight_attr);
713 if (result)
714 goto remove_trip_file;
715
716 mutex_lock(&tz->lock);
717 mutex_lock(&cdev->lock);
718 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
719 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
720 result = -EEXIST;
721 break;
722 }
723 if (!result) {
724 list_add_tail(&dev->tz_node, &tz->thermal_instances);
725 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
726 atomic_set(&tz->need_update, 1);
727 }
728 mutex_unlock(&cdev->lock);
729 mutex_unlock(&tz->lock);
730
731 if (!result)
732 return 0;
733
734 device_remove_file(&tz->device, &dev->weight_attr);
735 remove_trip_file:
736 device_remove_file(&tz->device, &dev->attr);
737 remove_symbol_link:
738 sysfs_remove_link(&tz->device.kobj, dev->name);
739 release_ida:
740 ida_free(&tz->ida, dev->id);
741 free_mem:
742 kfree(dev);
743 return result;
744 }
745 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
746
747 /**
748 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
749 * thermal zone.
750 * @tz: pointer to a struct thermal_zone_device.
751 * @trip_index: indicates which trip point the cooling devices is
752 * associated with in this thermal zone.
753 * @cdev: pointer to a struct thermal_cooling_device.
754 *
755 * This interface function unbind a thermal cooling device from the certain
756 * trip point of a thermal zone device.
757 * This function is usually called in the thermal zone device .unbind callback.
758 *
759 * Return: 0 on success, the proper error value otherwise.
760 */
thermal_zone_unbind_cooling_device(struct thermal_zone_device * tz,int trip_index,struct thermal_cooling_device * cdev)761 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
762 int trip_index,
763 struct thermal_cooling_device *cdev)
764 {
765 struct thermal_instance *pos, *next;
766 const struct thermal_trip *trip;
767
768 mutex_lock(&tz->lock);
769 mutex_lock(&cdev->lock);
770 trip = &tz->trips[trip_index];
771 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
772 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
773 list_del(&pos->tz_node);
774 list_del(&pos->cdev_node);
775 mutex_unlock(&cdev->lock);
776 mutex_unlock(&tz->lock);
777 goto unbind;
778 }
779 }
780 mutex_unlock(&cdev->lock);
781 mutex_unlock(&tz->lock);
782
783 return -ENODEV;
784
785 unbind:
786 device_remove_file(&tz->device, &pos->weight_attr);
787 device_remove_file(&tz->device, &pos->attr);
788 sysfs_remove_link(&tz->device.kobj, pos->name);
789 ida_free(&tz->ida, pos->id);
790 kfree(pos);
791 return 0;
792 }
793 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
794
thermal_release(struct device * dev)795 static void thermal_release(struct device *dev)
796 {
797 struct thermal_zone_device *tz;
798 struct thermal_cooling_device *cdev;
799
800 if (!strncmp(dev_name(dev), "thermal_zone",
801 sizeof("thermal_zone") - 1)) {
802 tz = to_thermal_zone(dev);
803 thermal_zone_destroy_device_groups(tz);
804 mutex_destroy(&tz->lock);
805 kfree(tz);
806 } else if (!strncmp(dev_name(dev), "cooling_device",
807 sizeof("cooling_device") - 1)) {
808 cdev = to_cooling_device(dev);
809 thermal_cooling_device_destroy_sysfs(cdev);
810 kfree(cdev->type);
811 ida_free(&thermal_cdev_ida, cdev->id);
812 kfree(cdev);
813 }
814 }
815
816 static struct class *thermal_class;
817
818 static inline
print_bind_err_msg(struct thermal_zone_device * tz,struct thermal_cooling_device * cdev,int ret)819 void print_bind_err_msg(struct thermal_zone_device *tz,
820 struct thermal_cooling_device *cdev, int ret)
821 {
822 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
823 tz->type, cdev->type, ret);
824 }
825
bind_cdev(struct thermal_cooling_device * cdev)826 static void bind_cdev(struct thermal_cooling_device *cdev)
827 {
828 int ret;
829 struct thermal_zone_device *pos = NULL;
830
831 list_for_each_entry(pos, &thermal_tz_list, node) {
832 if (pos->ops->bind) {
833 ret = pos->ops->bind(pos, cdev);
834 if (ret)
835 print_bind_err_msg(pos, cdev, ret);
836 }
837 }
838 }
839
840 /**
841 * __thermal_cooling_device_register() - register a new thermal cooling device
842 * @np: a pointer to a device tree node.
843 * @type: the thermal cooling device type.
844 * @devdata: device private data.
845 * @ops: standard thermal cooling devices callbacks.
846 *
847 * This interface function adds a new thermal cooling device (fan/processor/...)
848 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
849 * to all the thermal zone devices registered at the same time.
850 * It also gives the opportunity to link the cooling device to a device tree
851 * node, so that it can be bound to a thermal zone created out of device tree.
852 *
853 * Return: a pointer to the created struct thermal_cooling_device or an
854 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
855 */
856 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)857 __thermal_cooling_device_register(struct device_node *np,
858 const char *type, void *devdata,
859 const struct thermal_cooling_device_ops *ops)
860 {
861 struct thermal_cooling_device *cdev;
862 struct thermal_zone_device *pos = NULL;
863 int id, ret;
864
865 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
866 !ops->set_cur_state)
867 return ERR_PTR(-EINVAL);
868
869 if (!thermal_class)
870 return ERR_PTR(-ENODEV);
871
872 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
873 if (!cdev)
874 return ERR_PTR(-ENOMEM);
875
876 ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL);
877 if (ret < 0)
878 goto out_kfree_cdev;
879 cdev->id = ret;
880 id = ret;
881
882 cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
883 if (!cdev->type) {
884 ret = -ENOMEM;
885 goto out_ida_remove;
886 }
887
888 mutex_init(&cdev->lock);
889 INIT_LIST_HEAD(&cdev->thermal_instances);
890 cdev->np = np;
891 cdev->ops = ops;
892 cdev->updated = false;
893 cdev->device.class = thermal_class;
894 cdev->devdata = devdata;
895
896 ret = cdev->ops->get_max_state(cdev, &cdev->max_state);
897 if (ret)
898 goto out_cdev_type;
899
900 thermal_cooling_device_setup_sysfs(cdev);
901
902 ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
903 if (ret)
904 goto out_cooling_dev;
905
906 ret = device_register(&cdev->device);
907 if (ret) {
908 /* thermal_release() handles rest of the cleanup */
909 put_device(&cdev->device);
910 return ERR_PTR(ret);
911 }
912
913 /* Add 'this' new cdev to the global cdev list */
914 mutex_lock(&thermal_list_lock);
915
916 list_add(&cdev->node, &thermal_cdev_list);
917
918 /* Update binding information for 'this' new cdev */
919 bind_cdev(cdev);
920
921 list_for_each_entry(pos, &thermal_tz_list, node)
922 if (atomic_cmpxchg(&pos->need_update, 1, 0))
923 thermal_zone_device_update(pos,
924 THERMAL_EVENT_UNSPECIFIED);
925
926 mutex_unlock(&thermal_list_lock);
927
928 return cdev;
929
930 out_cooling_dev:
931 thermal_cooling_device_destroy_sysfs(cdev);
932 out_cdev_type:
933 kfree(cdev->type);
934 out_ida_remove:
935 ida_free(&thermal_cdev_ida, id);
936 out_kfree_cdev:
937 kfree(cdev);
938 return ERR_PTR(ret);
939 }
940
941 /**
942 * thermal_cooling_device_register() - register a new thermal cooling device
943 * @type: the thermal cooling device type.
944 * @devdata: device private data.
945 * @ops: standard thermal cooling devices callbacks.
946 *
947 * This interface function adds a new thermal cooling device (fan/processor/...)
948 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
949 * to all the thermal zone devices registered at the same time.
950 *
951 * Return: a pointer to the created struct thermal_cooling_device or an
952 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
953 */
954 struct thermal_cooling_device *
thermal_cooling_device_register(const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)955 thermal_cooling_device_register(const char *type, void *devdata,
956 const struct thermal_cooling_device_ops *ops)
957 {
958 return __thermal_cooling_device_register(NULL, type, devdata, ops);
959 }
960 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
961
962 /**
963 * thermal_of_cooling_device_register() - register an OF thermal cooling device
964 * @np: a pointer to a device tree node.
965 * @type: the thermal cooling device type.
966 * @devdata: device private data.
967 * @ops: standard thermal cooling devices callbacks.
968 *
969 * This function will register a cooling device with device tree node reference.
970 * This interface function adds a new thermal cooling device (fan/processor/...)
971 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
972 * to all the thermal zone devices registered at the same time.
973 *
974 * Return: a pointer to the created struct thermal_cooling_device or an
975 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
976 */
977 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)978 thermal_of_cooling_device_register(struct device_node *np,
979 const char *type, void *devdata,
980 const struct thermal_cooling_device_ops *ops)
981 {
982 return __thermal_cooling_device_register(np, type, devdata, ops);
983 }
984 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
985
thermal_cooling_device_release(struct device * dev,void * res)986 static void thermal_cooling_device_release(struct device *dev, void *res)
987 {
988 thermal_cooling_device_unregister(
989 *(struct thermal_cooling_device **)res);
990 }
991
992 /**
993 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
994 * device
995 * @dev: a valid struct device pointer of a sensor device.
996 * @np: a pointer to a device tree node.
997 * @type: the thermal cooling device type.
998 * @devdata: device private data.
999 * @ops: standard thermal cooling devices callbacks.
1000 *
1001 * This function will register a cooling device with device tree node reference.
1002 * This interface function adds a new thermal cooling device (fan/processor/...)
1003 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1004 * to all the thermal zone devices registered at the same time.
1005 *
1006 * Return: a pointer to the created struct thermal_cooling_device or an
1007 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1008 */
1009 struct thermal_cooling_device *
devm_thermal_of_cooling_device_register(struct device * dev,struct device_node * np,char * type,void * devdata,const struct thermal_cooling_device_ops * ops)1010 devm_thermal_of_cooling_device_register(struct device *dev,
1011 struct device_node *np,
1012 char *type, void *devdata,
1013 const struct thermal_cooling_device_ops *ops)
1014 {
1015 struct thermal_cooling_device **ptr, *tcd;
1016
1017 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1018 GFP_KERNEL);
1019 if (!ptr)
1020 return ERR_PTR(-ENOMEM);
1021
1022 tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1023 if (IS_ERR(tcd)) {
1024 devres_free(ptr);
1025 return tcd;
1026 }
1027
1028 *ptr = tcd;
1029 devres_add(dev, ptr);
1030
1031 return tcd;
1032 }
1033 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1034
thermal_cooling_device_present(struct thermal_cooling_device * cdev)1035 static bool thermal_cooling_device_present(struct thermal_cooling_device *cdev)
1036 {
1037 struct thermal_cooling_device *pos = NULL;
1038
1039 list_for_each_entry(pos, &thermal_cdev_list, node) {
1040 if (pos == cdev)
1041 return true;
1042 }
1043
1044 return false;
1045 }
1046
1047 /**
1048 * thermal_cooling_device_update - Update a cooling device object
1049 * @cdev: Target cooling device.
1050 *
1051 * Update @cdev to reflect a change of the underlying hardware or platform.
1052 *
1053 * Must be called when the maximum cooling state of @cdev becomes invalid and so
1054 * its .get_max_state() callback needs to be run to produce the new maximum
1055 * cooling state value.
1056 */
thermal_cooling_device_update(struct thermal_cooling_device * cdev)1057 void thermal_cooling_device_update(struct thermal_cooling_device *cdev)
1058 {
1059 struct thermal_instance *ti;
1060 unsigned long state;
1061
1062 if (IS_ERR_OR_NULL(cdev))
1063 return;
1064
1065 /*
1066 * Hold thermal_list_lock throughout the update to prevent the device
1067 * from going away while being updated.
1068 */
1069 mutex_lock(&thermal_list_lock);
1070
1071 if (!thermal_cooling_device_present(cdev))
1072 goto unlock_list;
1073
1074 /*
1075 * Update under the cdev lock to prevent the state from being set beyond
1076 * the new limit concurrently.
1077 */
1078 mutex_lock(&cdev->lock);
1079
1080 if (cdev->ops->get_max_state(cdev, &cdev->max_state))
1081 goto unlock;
1082
1083 thermal_cooling_device_stats_reinit(cdev);
1084
1085 list_for_each_entry(ti, &cdev->thermal_instances, cdev_node) {
1086 if (ti->upper == cdev->max_state)
1087 continue;
1088
1089 if (ti->upper < cdev->max_state) {
1090 if (ti->upper_no_limit)
1091 ti->upper = cdev->max_state;
1092
1093 continue;
1094 }
1095
1096 ti->upper = cdev->max_state;
1097 if (ti->lower > ti->upper)
1098 ti->lower = ti->upper;
1099
1100 if (ti->target == THERMAL_NO_TARGET)
1101 continue;
1102
1103 if (ti->target > ti->upper)
1104 ti->target = ti->upper;
1105 }
1106
1107 if (cdev->ops->get_cur_state(cdev, &state) || state > cdev->max_state)
1108 goto unlock;
1109
1110 thermal_cooling_device_stats_update(cdev, state);
1111
1112 unlock:
1113 mutex_unlock(&cdev->lock);
1114
1115 unlock_list:
1116 mutex_unlock(&thermal_list_lock);
1117 }
1118 EXPORT_SYMBOL_GPL(thermal_cooling_device_update);
1119
1120 /**
1121 * thermal_cooling_device_unregister - removes a thermal cooling device
1122 * @cdev: the thermal cooling device to remove.
1123 *
1124 * thermal_cooling_device_unregister() must be called when a registered
1125 * thermal cooling device is no longer needed.
1126 */
thermal_cooling_device_unregister(struct thermal_cooling_device * cdev)1127 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1128 {
1129 struct thermal_zone_device *tz;
1130
1131 if (!cdev)
1132 return;
1133
1134 mutex_lock(&thermal_list_lock);
1135
1136 if (!thermal_cooling_device_present(cdev)) {
1137 mutex_unlock(&thermal_list_lock);
1138 return;
1139 }
1140
1141 list_del(&cdev->node);
1142
1143 /* Unbind all thermal zones associated with 'this' cdev */
1144 list_for_each_entry(tz, &thermal_tz_list, node) {
1145 if (tz->ops->unbind)
1146 tz->ops->unbind(tz, cdev);
1147 }
1148
1149 mutex_unlock(&thermal_list_lock);
1150
1151 device_unregister(&cdev->device);
1152 }
1153 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1154
bind_tz(struct thermal_zone_device * tz)1155 static void bind_tz(struct thermal_zone_device *tz)
1156 {
1157 int ret;
1158 struct thermal_cooling_device *pos = NULL;
1159
1160 if (!tz->ops->bind)
1161 return;
1162
1163 mutex_lock(&thermal_list_lock);
1164
1165 list_for_each_entry(pos, &thermal_cdev_list, node) {
1166 ret = tz->ops->bind(tz, pos);
1167 if (ret)
1168 print_bind_err_msg(tz, pos, ret);
1169 }
1170
1171 mutex_unlock(&thermal_list_lock);
1172 }
1173
thermal_set_delay_jiffies(unsigned long * delay_jiffies,int delay_ms)1174 static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms)
1175 {
1176 *delay_jiffies = msecs_to_jiffies(delay_ms);
1177 if (delay_ms > 1000)
1178 *delay_jiffies = round_jiffies(*delay_jiffies);
1179 }
1180
thermal_zone_get_crit_temp(struct thermal_zone_device * tz,int * temp)1181 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
1182 {
1183 int i, ret = -EINVAL;
1184
1185 if (tz->ops->get_crit_temp)
1186 return tz->ops->get_crit_temp(tz, temp);
1187
1188 if (!tz->trips)
1189 return -EINVAL;
1190
1191 mutex_lock(&tz->lock);
1192
1193 for (i = 0; i < tz->num_trips; i++) {
1194 if (tz->trips[i].type == THERMAL_TRIP_CRITICAL) {
1195 *temp = tz->trips[i].temperature;
1196 ret = 0;
1197 break;
1198 }
1199 }
1200
1201 mutex_unlock(&tz->lock);
1202
1203 return ret;
1204 }
1205 EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp);
1206
1207 /**
1208 * thermal_zone_device_register_with_trips() - register a new thermal zone device
1209 * @type: the thermal zone device type
1210 * @trips: a pointer to an array of thermal trips
1211 * @num_trips: the number of trip points the thermal zone support
1212 * @mask: a bit string indicating the writeablility of trip points
1213 * @devdata: private device data
1214 * @ops: standard thermal zone device callbacks
1215 * @tzp: thermal zone platform parameters
1216 * @passive_delay: number of milliseconds to wait between polls when
1217 * performing passive cooling
1218 * @polling_delay: number of milliseconds to wait between polls when checking
1219 * whether trip points have been crossed (0 for interrupt
1220 * driven systems)
1221 *
1222 * This interface function adds a new thermal zone device (sensor) to
1223 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1224 * thermal cooling devices registered at the same time.
1225 * thermal_zone_device_unregister() must be called when the device is no
1226 * longer needed. The passive cooling depends on the .get_trend() return value.
1227 *
1228 * Return: a pointer to the created struct thermal_zone_device or an
1229 * in case of error, an ERR_PTR. Caller must check return value with
1230 * IS_ERR*() helpers.
1231 */
1232 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,const struct thermal_zone_params * tzp,int passive_delay,int polling_delay)1233 thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *trips, int num_trips, int mask,
1234 void *devdata, struct thermal_zone_device_ops *ops,
1235 const struct thermal_zone_params *tzp, int passive_delay,
1236 int polling_delay)
1237 {
1238 struct thermal_zone_device *tz;
1239 int id;
1240 int result;
1241 int count;
1242 struct thermal_governor *governor;
1243
1244 if (!type || strlen(type) == 0) {
1245 pr_err("No thermal zone type defined\n");
1246 return ERR_PTR(-EINVAL);
1247 }
1248
1249 if (strlen(type) >= THERMAL_NAME_LENGTH) {
1250 pr_err("Thermal zone name (%s) too long, should be under %d chars\n",
1251 type, THERMAL_NAME_LENGTH);
1252 return ERR_PTR(-EINVAL);
1253 }
1254
1255 /*
1256 * Max trip count can't exceed 31 as the "mask >> num_trips" condition.
1257 * For example, shifting by 32 will result in compiler warning:
1258 * warning: right shift count >= width of type [-Wshift-count- overflow]
1259 *
1260 * Also "mask >> num_trips" will always be true with 32 bit shift.
1261 * E.g. mask = 0x80000000 for trip id 31 to be RW. Then
1262 * mask >> 32 = 0x80000000
1263 * This will result in failure for the below condition.
1264 *
1265 * Check will be true when the bit 31 of the mask is set.
1266 * 32 bit shift will cause overflow of 4 byte integer.
1267 */
1268 if (num_trips > (BITS_PER_TYPE(int) - 1) || num_trips < 0 || mask >> num_trips) {
1269 pr_err("Incorrect number of thermal trips\n");
1270 return ERR_PTR(-EINVAL);
1271 }
1272
1273 if (!ops) {
1274 pr_err("Thermal zone device ops not defined\n");
1275 return ERR_PTR(-EINVAL);
1276 }
1277
1278 if (num_trips > 0 && !trips)
1279 return ERR_PTR(-EINVAL);
1280
1281 if (!thermal_class)
1282 return ERR_PTR(-ENODEV);
1283
1284 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1285 if (!tz)
1286 return ERR_PTR(-ENOMEM);
1287
1288 if (tzp) {
1289 tz->tzp = kmemdup(tzp, sizeof(*tzp), GFP_KERNEL);
1290 if (!tz->tzp) {
1291 result = -ENOMEM;
1292 goto free_tz;
1293 }
1294 }
1295
1296 INIT_LIST_HEAD(&tz->thermal_instances);
1297 ida_init(&tz->ida);
1298 mutex_init(&tz->lock);
1299 id = ida_alloc(&thermal_tz_ida, GFP_KERNEL);
1300 if (id < 0) {
1301 result = id;
1302 goto free_tzp;
1303 }
1304
1305 tz->id = id;
1306 strscpy(tz->type, type, sizeof(tz->type));
1307
1308 if (!ops->critical)
1309 ops->critical = thermal_zone_device_critical;
1310
1311 tz->ops = ops;
1312 tz->device.class = thermal_class;
1313 tz->devdata = devdata;
1314 tz->trips = trips;
1315 tz->num_trips = num_trips;
1316
1317 thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay);
1318 thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay);
1319
1320 /* sys I/F */
1321 /* Add nodes that are always present via .groups */
1322 result = thermal_zone_create_device_groups(tz, mask);
1323 if (result)
1324 goto remove_id;
1325
1326 /* A new thermal zone needs to be updated anyway. */
1327 atomic_set(&tz->need_update, 1);
1328
1329 result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1330 if (result) {
1331 thermal_zone_destroy_device_groups(tz);
1332 goto remove_id;
1333 }
1334 result = device_register(&tz->device);
1335 if (result)
1336 goto release_device;
1337
1338 for (count = 0; count < num_trips; count++) {
1339 struct thermal_trip trip;
1340
1341 result = thermal_zone_get_trip(tz, count, &trip);
1342 if (result || !trip.temperature)
1343 set_bit(count, &tz->trips_disabled);
1344 }
1345
1346 /* Update 'this' zone's governor information */
1347 mutex_lock(&thermal_governor_lock);
1348
1349 if (tz->tzp)
1350 governor = __find_governor(tz->tzp->governor_name);
1351 else
1352 governor = def_governor;
1353
1354 result = thermal_set_governor(tz, governor);
1355 if (result) {
1356 mutex_unlock(&thermal_governor_lock);
1357 goto unregister;
1358 }
1359
1360 mutex_unlock(&thermal_governor_lock);
1361
1362 if (!tz->tzp || !tz->tzp->no_hwmon) {
1363 result = thermal_add_hwmon_sysfs(tz);
1364 if (result)
1365 goto unregister;
1366 }
1367
1368 mutex_lock(&thermal_list_lock);
1369 list_add_tail(&tz->node, &thermal_tz_list);
1370 mutex_unlock(&thermal_list_lock);
1371
1372 /* Bind cooling devices for this zone */
1373 bind_tz(tz);
1374
1375 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1376
1377 thermal_zone_device_init(tz);
1378 /* Update the new thermal zone and mark it as already updated. */
1379 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1380 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1381
1382 thermal_notify_tz_create(tz->id, tz->type);
1383
1384 return tz;
1385
1386 unregister:
1387 device_del(&tz->device);
1388 release_device:
1389 put_device(&tz->device);
1390 remove_id:
1391 ida_free(&thermal_tz_ida, id);
1392 free_tzp:
1393 kfree(tz->tzp);
1394 free_tz:
1395 kfree(tz);
1396 return ERR_PTR(result);
1397 }
1398 EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips);
1399
thermal_tripless_zone_device_register(const char * type,void * devdata,struct thermal_zone_device_ops * ops,const struct thermal_zone_params * tzp)1400 struct thermal_zone_device *thermal_tripless_zone_device_register(
1401 const char *type,
1402 void *devdata,
1403 struct thermal_zone_device_ops *ops,
1404 const struct thermal_zone_params *tzp)
1405 {
1406 return thermal_zone_device_register_with_trips(type, NULL, 0, 0, devdata,
1407 ops, tzp, 0, 0);
1408 }
1409 EXPORT_SYMBOL_GPL(thermal_tripless_zone_device_register);
1410
thermal_zone_device_priv(struct thermal_zone_device * tzd)1411 void *thermal_zone_device_priv(struct thermal_zone_device *tzd)
1412 {
1413 return tzd->devdata;
1414 }
1415 EXPORT_SYMBOL_GPL(thermal_zone_device_priv);
1416
thermal_zone_device_type(struct thermal_zone_device * tzd)1417 const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
1418 {
1419 return tzd->type;
1420 }
1421 EXPORT_SYMBOL_GPL(thermal_zone_device_type);
1422
thermal_zone_device_id(struct thermal_zone_device * tzd)1423 int thermal_zone_device_id(struct thermal_zone_device *tzd)
1424 {
1425 return tzd->id;
1426 }
1427 EXPORT_SYMBOL_GPL(thermal_zone_device_id);
1428
thermal_zone_device(struct thermal_zone_device * tzd)1429 struct device *thermal_zone_device(struct thermal_zone_device *tzd)
1430 {
1431 return &tzd->device;
1432 }
1433 EXPORT_SYMBOL_GPL(thermal_zone_device);
1434
1435 /**
1436 * thermal_zone_device_unregister - removes the registered thermal zone device
1437 * @tz: the thermal zone device to remove
1438 */
thermal_zone_device_unregister(struct thermal_zone_device * tz)1439 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1440 {
1441 int tz_id;
1442 struct thermal_cooling_device *cdev;
1443 struct thermal_zone_device *pos = NULL;
1444
1445 if (!tz)
1446 return;
1447
1448 tz_id = tz->id;
1449
1450 mutex_lock(&thermal_list_lock);
1451 list_for_each_entry(pos, &thermal_tz_list, node)
1452 if (pos == tz)
1453 break;
1454 if (pos != tz) {
1455 /* thermal zone device not found */
1456 mutex_unlock(&thermal_list_lock);
1457 return;
1458 }
1459 list_del(&tz->node);
1460
1461 /* Unbind all cdevs associated with 'this' thermal zone */
1462 list_for_each_entry(cdev, &thermal_cdev_list, node)
1463 if (tz->ops->unbind)
1464 tz->ops->unbind(tz, cdev);
1465
1466 mutex_unlock(&thermal_list_lock);
1467
1468 cancel_delayed_work_sync(&tz->poll_queue);
1469
1470 thermal_set_governor(tz, NULL);
1471
1472 thermal_remove_hwmon_sysfs(tz);
1473 ida_free(&thermal_tz_ida, tz->id);
1474 ida_destroy(&tz->ida);
1475
1476 mutex_lock(&tz->lock);
1477 device_del(&tz->device);
1478 mutex_unlock(&tz->lock);
1479
1480 kfree(tz->tzp);
1481
1482 put_device(&tz->device);
1483
1484 thermal_notify_tz_delete(tz_id);
1485 }
1486 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1487
1488 /**
1489 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1490 * @name: thermal zone name to fetch the temperature
1491 *
1492 * When only one zone is found with the passed name, returns a reference to it.
1493 *
1494 * Return: On success returns a reference to an unique thermal zone with
1495 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1496 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1497 */
thermal_zone_get_zone_by_name(const char * name)1498 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1499 {
1500 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1501 unsigned int found = 0;
1502
1503 if (!name)
1504 goto exit;
1505
1506 mutex_lock(&thermal_list_lock);
1507 list_for_each_entry(pos, &thermal_tz_list, node)
1508 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1509 found++;
1510 ref = pos;
1511 }
1512 mutex_unlock(&thermal_list_lock);
1513
1514 /* nothing has been found, thus an error code for it */
1515 if (found == 0)
1516 ref = ERR_PTR(-ENODEV);
1517 else if (found > 1)
1518 /* Success only when an unique zone is found */
1519 ref = ERR_PTR(-EEXIST);
1520
1521 exit:
1522 return ref;
1523 }
1524 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1525
thermal_pm_notify(struct notifier_block * nb,unsigned long mode,void * _unused)1526 static int thermal_pm_notify(struct notifier_block *nb,
1527 unsigned long mode, void *_unused)
1528 {
1529 struct thermal_zone_device *tz;
1530 int irq_wakeable = 0;
1531
1532 switch (mode) {
1533 case PM_HIBERNATION_PREPARE:
1534 case PM_RESTORE_PREPARE:
1535 case PM_SUSPEND_PREPARE:
1536 mutex_lock(&thermal_list_lock);
1537
1538 list_for_each_entry(tz, &thermal_tz_list, node) {
1539 mutex_lock(&tz->lock);
1540
1541 trace_android_vh_thermal_pm_notify_suspend(tz, &irq_wakeable);
1542 if (irq_wakeable) {
1543 mutex_unlock(&tz->lock);
1544 continue;
1545 }
1546
1547 tz->suspended = true;
1548
1549 mutex_unlock(&tz->lock);
1550 }
1551
1552 mutex_unlock(&thermal_list_lock);
1553 break;
1554 case PM_POST_HIBERNATION:
1555 case PM_POST_RESTORE:
1556 case PM_POST_SUSPEND:
1557 mutex_lock(&thermal_list_lock);
1558
1559 list_for_each_entry(tz, &thermal_tz_list, node) {
1560 mutex_lock(&tz->lock);
1561
1562 trace_android_vh_thermal_pm_notify_suspend(tz, &irq_wakeable);
1563 if (irq_wakeable) {
1564 mutex_unlock(&tz->lock);
1565 continue;
1566 }
1567
1568 tz->suspended = false;
1569
1570 thermal_zone_device_init(tz);
1571 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1572
1573 mutex_unlock(&tz->lock);
1574 }
1575
1576 mutex_unlock(&thermal_list_lock);
1577 break;
1578 default:
1579 break;
1580 }
1581 return 0;
1582 }
1583
1584 static struct notifier_block thermal_pm_nb = {
1585 .notifier_call = thermal_pm_notify,
1586 };
1587
thermal_init(void)1588 static int __init thermal_init(void)
1589 {
1590 int result;
1591
1592 result = thermal_netlink_init();
1593 if (result)
1594 goto error;
1595
1596 result = thermal_register_governors();
1597 if (result)
1598 goto unregister_netlink;
1599
1600 thermal_class = kzalloc(sizeof(*thermal_class), GFP_KERNEL);
1601 if (!thermal_class) {
1602 result = -ENOMEM;
1603 goto unregister_governors;
1604 }
1605
1606 thermal_class->name = "thermal";
1607 thermal_class->dev_release = thermal_release;
1608
1609 result = class_register(thermal_class);
1610 if (result) {
1611 kfree(thermal_class);
1612 thermal_class = NULL;
1613 goto unregister_governors;
1614 }
1615
1616 result = register_pm_notifier(&thermal_pm_nb);
1617 if (result)
1618 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1619 result);
1620
1621 return 0;
1622
1623 unregister_governors:
1624 thermal_unregister_governors();
1625 unregister_netlink:
1626 thermal_netlink_exit();
1627 error:
1628 mutex_destroy(&thermal_list_lock);
1629 mutex_destroy(&thermal_governor_lock);
1630 return result;
1631 }
1632 postcore_initcall(thermal_init);
1633