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