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