usb: gadget: f_mtp: Avoid race between mtp_read and mtp_function_disable
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / drivers / thermal / thermal_core.c
... / ...
CommitLineData
1/*
2 * thermal.c - Generic Thermal Management Sysfs support.
3 *
4 * Copyright (C) 2008 Intel Corp
5 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
6 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
7 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
25
26#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
28#include <linux/module.h>
29#include <linux/device.h>
30#include <linux/err.h>
31#include <linux/slab.h>
32#include <linux/kdev_t.h>
33#include <linux/idr.h>
34#include <linux/thermal.h>
35#include <linux/reboot.h>
36#include <linux/string.h>
37#include <linux/of.h>
38#include <linux/cpu.h>
39#include <net/netlink.h>
40#include <net/genetlink.h>
41#include <linux/suspend.h>
42
43#define CREATE_TRACE_POINTS
44#include <trace/events/thermal.h>
45
46#include "thermal_core.h"
47#include "thermal_hwmon.h"
48
49MODULE_AUTHOR("Zhang Rui");
50MODULE_DESCRIPTION("Generic thermal management sysfs support");
51MODULE_LICENSE("GPL v2");
52
53static DEFINE_IDR(thermal_tz_idr);
54static DEFINE_IDR(thermal_cdev_idr);
55static DEFINE_MUTEX(thermal_idr_lock);
56
57static LIST_HEAD(thermal_tz_list);
58static LIST_HEAD(thermal_cdev_list);
59static LIST_HEAD(thermal_governor_list);
60
61static DEFINE_MUTEX(thermal_list_lock);
62static DEFINE_MUTEX(thermal_governor_lock);
63
64static atomic_t in_suspend;
65
66#ifdef CONFIG_SCHED_HMP
67#define BOUNDED_CPU 1
68static void start_poll_queue(struct thermal_zone_device *tz, int delay)
69{
70 mod_delayed_work_on(tz->poll_queue_cpu, system_freezable_wq, &tz->poll_queue,
71 msecs_to_jiffies(delay));
72}
73#endif
74
75static struct thermal_governor *def_governor;
76
77static struct thermal_governor *__find_governor(const char *name)
78{
79 struct thermal_governor *pos;
80
81 if (!name || !name[0])
82 return def_governor;
83
84 list_for_each_entry(pos, &thermal_governor_list, governor_list)
85 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
86 return pos;
87
88 return NULL;
89}
90
91/**
92 * bind_previous_governor() - bind the previous governor of the thermal zone
93 * @tz: a valid pointer to a struct thermal_zone_device
94 * @failed_gov_name: the name of the governor that failed to register
95 *
96 * Register the previous governor of the thermal zone after a new
97 * governor has failed to be bound.
98 */
99static void bind_previous_governor(struct thermal_zone_device *tz,
100 const char *failed_gov_name)
101{
102 if (tz->governor && tz->governor->bind_to_tz) {
103 if (tz->governor->bind_to_tz(tz)) {
104 dev_err(&tz->device,
105 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
106 failed_gov_name, tz->governor->name, tz->type);
107 tz->governor = NULL;
108 }
109 }
110}
111
112/**
113 * thermal_set_governor() - Switch to another governor
114 * @tz: a valid pointer to a struct thermal_zone_device
115 * @new_gov: pointer to the new governor
116 *
117 * Change the governor of thermal zone @tz.
118 *
119 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
120 */
121static int thermal_set_governor(struct thermal_zone_device *tz,
122 struct thermal_governor *new_gov)
123{
124 int ret = 0;
125
126 if (tz->governor && tz->governor->unbind_from_tz)
127 tz->governor->unbind_from_tz(tz);
128
129 if (new_gov && new_gov->bind_to_tz) {
130 ret = new_gov->bind_to_tz(tz);
131 if (ret) {
132 bind_previous_governor(tz, new_gov->name);
133
134 return ret;
135 }
136 }
137
138 tz->governor = new_gov;
139
140 return ret;
141}
142
143int thermal_register_governor(struct thermal_governor *governor)
144{
145 int err;
146 const char *name;
147 struct thermal_zone_device *pos;
148
149 if (!governor)
150 return -EINVAL;
151
152 mutex_lock(&thermal_governor_lock);
153
154 err = -EBUSY;
155 if (__find_governor(governor->name) == NULL) {
156 err = 0;
157 list_add(&governor->governor_list, &thermal_governor_list);
158 if (!def_governor && !strncmp(governor->name,
159 DEFAULT_THERMAL_GOVERNOR, THERMAL_NAME_LENGTH))
160 def_governor = governor;
161 }
162
163 mutex_lock(&thermal_list_lock);
164
165 list_for_each_entry(pos, &thermal_tz_list, node) {
166 /*
167 * only thermal zones with specified tz->tzp->governor_name
168 * may run with tz->govenor unset
169 */
170 if (pos->governor)
171 continue;
172
173 name = pos->tzp->governor_name;
174
175 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
176 int ret;
177
178 ret = thermal_set_governor(pos, governor);
179 if (ret)
180 dev_err(&pos->device,
181 "Failed to set governor %s for thermal zone %s: %d\n",
182 governor->name, pos->type, ret);
183 }
184 }
185
186 mutex_unlock(&thermal_list_lock);
187 mutex_unlock(&thermal_governor_lock);
188
189 return err;
190}
191
192void thermal_unregister_governor(struct thermal_governor *governor)
193{
194 struct thermal_zone_device *pos;
195
196 if (!governor)
197 return;
198
199 mutex_lock(&thermal_governor_lock);
200
201 if (__find_governor(governor->name) == NULL)
202 goto exit;
203
204 mutex_lock(&thermal_list_lock);
205
206 list_for_each_entry(pos, &thermal_tz_list, node) {
207 if (!strncasecmp(pos->governor->name, governor->name,
208 THERMAL_NAME_LENGTH))
209 thermal_set_governor(pos, NULL);
210 }
211
212 mutex_unlock(&thermal_list_lock);
213 list_del(&governor->governor_list);
214exit:
215 mutex_unlock(&thermal_governor_lock);
216 return;
217}
218
219static int get_idr(struct idr *idr, struct mutex *lock, int *id)
220{
221 int ret;
222
223 if (lock)
224 mutex_lock(lock);
225 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
226 if (lock)
227 mutex_unlock(lock);
228 if (unlikely(ret < 0))
229 return ret;
230 *id = ret;
231 return 0;
232}
233
234static void release_idr(struct idr *idr, struct mutex *lock, int id)
235{
236 if (lock)
237 mutex_lock(lock);
238 idr_remove(idr, id);
239 if (lock)
240 mutex_unlock(lock);
241}
242
243int get_tz_trend(struct thermal_zone_device *tz, int trip)
244{
245 enum thermal_trend trend;
246
247 if (tz->emul_temperature || !tz->ops->get_trend ||
248 tz->ops->get_trend(tz, trip, &trend)) {
249 if (tz->temperature > tz->last_temperature)
250 trend = THERMAL_TREND_RAISING;
251 else if (tz->temperature < tz->last_temperature)
252 trend = THERMAL_TREND_DROPPING;
253 else
254 trend = THERMAL_TREND_STABLE;
255 }
256
257 return trend;
258}
259EXPORT_SYMBOL(get_tz_trend);
260
261struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
262 struct thermal_cooling_device *cdev, int trip)
263{
264 struct thermal_instance *pos = NULL;
265 struct thermal_instance *target_instance = NULL;
266
267 mutex_lock(&tz->lock);
268 mutex_lock(&cdev->lock);
269
270 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
271 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
272 target_instance = pos;
273 break;
274 }
275 }
276
277 mutex_unlock(&cdev->lock);
278 mutex_unlock(&tz->lock);
279
280 return target_instance;
281}
282EXPORT_SYMBOL(get_thermal_instance);
283
284static void print_bind_err_msg(struct thermal_zone_device *tz,
285 struct thermal_cooling_device *cdev, int ret)
286{
287 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
288 tz->type, cdev->type, ret);
289}
290
291static void __bind(struct thermal_zone_device *tz, int mask,
292 struct thermal_cooling_device *cdev,
293 unsigned long *limits,
294 unsigned int weight)
295{
296 int i, ret;
297
298 for (i = 0; i < tz->trips; i++) {
299 if (mask & (1 << i)) {
300 unsigned long upper, lower;
301
302 upper = THERMAL_NO_LIMIT;
303 lower = THERMAL_NO_LIMIT;
304 if (limits) {
305 lower = limits[i * 2];
306 upper = limits[i * 2 + 1];
307 }
308 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
309 upper, lower,
310 weight);
311 if (ret)
312 print_bind_err_msg(tz, cdev, ret);
313 }
314 }
315}
316
317static void __unbind(struct thermal_zone_device *tz, int mask,
318 struct thermal_cooling_device *cdev)
319{
320 int i;
321
322 for (i = 0; i < tz->trips; i++)
323 if (mask & (1 << i))
324 thermal_zone_unbind_cooling_device(tz, i, cdev);
325}
326
327static void bind_cdev(struct thermal_cooling_device *cdev)
328{
329 int i, ret;
330 const struct thermal_zone_params *tzp;
331 struct thermal_zone_device *pos = NULL;
332
333 mutex_lock(&thermal_list_lock);
334
335 list_for_each_entry(pos, &thermal_tz_list, node) {
336 if (!pos->tzp && !pos->ops->bind)
337 continue;
338
339 if (pos->ops->bind) {
340 ret = pos->ops->bind(pos, cdev);
341 if (ret)
342 print_bind_err_msg(pos, cdev, ret);
343 continue;
344 }
345
346 tzp = pos->tzp;
347 if (!tzp || !tzp->tbp)
348 continue;
349
350 for (i = 0; i < tzp->num_tbps; i++) {
351 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
352 continue;
353 if (tzp->tbp[i].match(pos, cdev))
354 continue;
355 tzp->tbp[i].cdev = cdev;
356 __bind(pos, tzp->tbp[i].trip_mask, cdev,
357 tzp->tbp[i].binding_limits,
358 tzp->tbp[i].weight);
359 }
360 }
361
362 mutex_unlock(&thermal_list_lock);
363}
364
365static void bind_tz(struct thermal_zone_device *tz)
366{
367 int i, ret;
368 struct thermal_cooling_device *pos = NULL;
369 const struct thermal_zone_params *tzp = tz->tzp;
370
371 if (!tzp && !tz->ops->bind)
372 return;
373
374 mutex_lock(&thermal_list_lock);
375
376 /* If there is ops->bind, try to use ops->bind */
377 if (tz->ops->bind) {
378 list_for_each_entry(pos, &thermal_cdev_list, node) {
379 ret = tz->ops->bind(tz, pos);
380 if (ret)
381 print_bind_err_msg(tz, pos, ret);
382 }
383 goto exit;
384 }
385
386 if (!tzp || !tzp->tbp)
387 goto exit;
388
389 list_for_each_entry(pos, &thermal_cdev_list, node) {
390 for (i = 0; i < tzp->num_tbps; i++) {
391 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
392 continue;
393 if (tzp->tbp[i].match(tz, pos))
394 continue;
395 tzp->tbp[i].cdev = pos;
396 __bind(tz, tzp->tbp[i].trip_mask, pos,
397 tzp->tbp[i].binding_limits,
398 tzp->tbp[i].weight);
399 }
400 }
401exit:
402 mutex_unlock(&thermal_list_lock);
403}
404
405static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
406 int delay)
407{
408 if (delay > 1000)
409#ifdef CONFIG_SCHED_HMP
410 start_poll_queue(tz, delay);
411#else
412 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
413 round_jiffies(msecs_to_jiffies(delay)));
414#endif
415 else if (delay)
416#ifdef CONFIG_SCHED_HMP
417 start_poll_queue(tz, delay);
418#else
419 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
420 msecs_to_jiffies(delay));
421#endif
422 else
423 cancel_delayed_work(&tz->poll_queue);
424}
425
426static void monitor_thermal_zone(struct thermal_zone_device *tz)
427{
428 mutex_lock(&tz->lock);
429
430 if (tz->passive)
431 thermal_zone_device_set_polling(tz, tz->passive_delay);
432 else if (tz->polling_delay)
433 thermal_zone_device_set_polling(tz, tz->polling_delay);
434 else
435 thermal_zone_device_set_polling(tz, 0);
436
437 mutex_unlock(&tz->lock);
438}
439
440static void handle_non_critical_trips(struct thermal_zone_device *tz,
441 int trip, enum thermal_trip_type trip_type)
442{
443 tz->governor ? tz->governor->throttle(tz, trip) :
444 def_governor->throttle(tz, trip);
445}
446
447static void handle_critical_trips(struct thermal_zone_device *tz,
448 int trip, enum thermal_trip_type trip_type)
449{
450 int trip_temp;
451
452 tz->ops->get_trip_temp(tz, trip, &trip_temp);
453
454 /* If we have not crossed the trip_temp, we do not care. */
455 if (trip_temp <= 0 || tz->temperature < trip_temp)
456 return;
457
458 trace_thermal_zone_trip(tz, trip, trip_type);
459
460 if (tz->ops->notify)
461 tz->ops->notify(tz, trip, trip_type);
462
463 if (trip_type == THERMAL_TRIP_CRITICAL) {
464 dev_emerg(&tz->device,
465 "critical temperature reached(%d C),shutting down\n",
466 tz->temperature / 1000);
467 orderly_poweroff(true);
468 }
469}
470
471#ifdef CONFIG_SEC_DEBUG_HW_PARAM
472#define APO_THROTTLE_TEMP 81000
473static bool period_check;
474static bool is_apo_check;
475static int trip_temp;
476static u64 last_time[THERMAL_ZONE_MAX], curr_time[THERMAL_ZONE_MAX];
477static u64 last_apo, curr_apo;
478static enum thermal_trip_type result_type;
479struct thermal_data_devices thermal_data_info[THERMAL_ZONE_MAX];
480#endif
481
482static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
483{
484 enum thermal_trip_type type;
485#ifdef CONFIG_SEC_DEBUG_HW_PARAM
486 int tid = tz->id;
487#endif
488
489 /* Ignore disabled trip points */
490 if (test_bit(trip, &tz->trips_disabled))
491 return;
492
493#ifdef CONFIG_SEC_DEBUG_HW_PARAM
494 if (trip == 0) {
495 period_check = false;
496 result_type = THERMAL_TRIP_ACTIVE;
497 }
498
499 if (tz->temperature > thermal_data_info[tid].max_temp)
500 thermal_data_info[tid].max_temp = tz->temperature;
501
502 tz->ops->get_trip_temp(tz, trip, &trip_temp);
503 if (tz->temperature > trip_temp) {
504 tz->ops->get_trip_type(tz, trip + 1, &result_type);
505 }
506 else {
507 if (!period_check) {
508 curr_time[tid] = ktime_to_ns(ktime_get()) / 1000000;
509 if(last_time[tid]) {
510 if(result_type == THERMAL_TRIP_ACTIVE)
511 thermal_data_info[tid].times[ACTIVE_TIMES] +=
512 (curr_time[tid] - last_time[tid]);
513 else if (result_type == THERMAL_TRIP_PASSIVE)
514 thermal_data_info[tid].times[PASSIVE_TIMES] +=
515 (curr_time[tid] - last_time[tid]);
516 else if (result_type == THERMAL_TRIP_HOT)
517 thermal_data_info[tid].times[HOT_TIMES] +=
518 (curr_time[tid] - last_time[tid]);
519
520 period_check = true;
521 }
522
523 if (tid == THERMAL_ZONE_APOLLO) {
524 if (tz->temperature >= APO_THROTTLE_TEMP) {
525 if (!is_apo_check) {
526 is_apo_check = true;
527 last_apo = ktime_to_ns(ktime_get()) / 1000000;
528 }
529 }
530 else {
531 if (is_apo_check) {
532 curr_apo = ktime_to_ns(ktime_get()) / 1000000;
533 thermal_data_info[tid].hotplug_out +=
534 (curr_apo - last_apo);
535 is_apo_check = false;
536 }
537 }
538 }
539
540 last_time[tid] = curr_time[tid];
541 }
542 }
543#endif
544
545 tz->ops->get_trip_type(tz, trip, &type);
546
547 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
548 handle_critical_trips(tz, trip, type);
549 else
550 handle_non_critical_trips(tz, trip, type);
551 /*
552 * Alright, we handled this trip successfully.
553 * So, start monitoring again.
554 */
555 monitor_thermal_zone(tz);
556}
557
558/**
559 * thermal_zone_get_temp() - returns the temperature of a thermal zone
560 * @tz: a valid pointer to a struct thermal_zone_device
561 * @temp: a valid pointer to where to store the resulting temperature.
562 *
563 * When a valid thermal zone reference is passed, it will fetch its
564 * temperature and fill @temp.
565 *
566 * Return: On success returns 0, an error code otherwise
567 */
568int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
569{
570 int ret = -EINVAL;
571 int count;
572 int crit_temp = INT_MAX;
573 enum thermal_trip_type type;
574
575 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
576 goto exit;
577
578 mutex_lock(&tz->lock);
579
580 ret = tz->ops->get_temp(tz, temp);
581
582 if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
583 for (count = 0; count < tz->trips; count++) {
584 ret = tz->ops->get_trip_type(tz, count, &type);
585 if (!ret && type == THERMAL_TRIP_CRITICAL) {
586 ret = tz->ops->get_trip_temp(tz, count,
587 &crit_temp);
588 break;
589 }
590 }
591
592 /*
593 * Only allow emulating a temperature when the real temperature
594 * is below the critical temperature so that the emulation code
595 * cannot hide critical conditions.
596 */
597 if (!ret && *temp < crit_temp)
598 *temp = tz->emul_temperature;
599 }
600
601 mutex_unlock(&tz->lock);
602exit:
603 return ret;
604}
605EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
606
607#ifdef CONFIG_SEC_PM_DEBUG
608#define TEMP_COUNT 10
609#endif
610
611static void update_temperature(struct thermal_zone_device *tz)
612{
613 int temp, ret;
614#ifdef CONFIG_SEC_PM_DEBUG
615 static int count = 1;
616#endif
617
618 ret = thermal_zone_get_temp(tz, &temp);
619 if (ret) {
620 if (ret != -EAGAIN)
621 dev_warn(&tz->device,
622 "failed to read out thermal zone (%d)\n",
623 ret);
624 return;
625 }
626
627 mutex_lock(&tz->lock);
628 tz->last_temperature = tz->temperature;
629 tz->temperature = temp;
630 mutex_unlock(&tz->lock);
631
632 trace_thermal_temperature(tz);
633 if (tz->last_temperature == THERMAL_TEMP_INVALID)
634 dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
635 tz->temperature);
636 else
637 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
638 tz->last_temperature, tz->temperature);
639
640#ifdef CONFIG_SEC_PM_DEBUG
641 if (!(count++ % TEMP_COUNT)) {
642 count = 1;
643 dev_info(&tz->device, "[TMU] last_temperature=%d, current_temperature=%d\n",
644 tz->last_temperature, tz->temperature);
645 }
646#endif
647}
648
649static void thermal_zone_device_reset(struct thermal_zone_device *tz)
650{
651 struct thermal_instance *pos;
652
653 tz->temperature = THERMAL_TEMP_INVALID;
654 tz->passive = 0;
655 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
656 pos->initialized = false;
657}
658
659void thermal_zone_device_update(struct thermal_zone_device *tz)
660{
661 int count;
662 enum thermal_device_mode mode;
663
664 if (atomic_read(&in_suspend))
665 return;
666
667 if (!tz->ops->get_temp || !tz->ops->get_mode)
668 return;
669
670 tz->ops->get_mode(tz, &mode);
671
672 if (mode == THERMAL_DEVICE_ENABLED) {
673 update_temperature(tz);
674
675 for (count = 0; count < tz->trips; count++)
676 handle_thermal_trip(tz, count);
677
678 if (tz->ops->throttle_hotplug)
679 tz->ops->throttle_hotplug(tz);
680 }
681}
682EXPORT_SYMBOL_GPL(thermal_zone_device_update);
683
684static void thermal_zone_device_check(struct work_struct *work)
685{
686 struct thermal_zone_device *tz = container_of(work, struct
687 thermal_zone_device,
688 poll_queue.work);
689 thermal_zone_device_update(tz);
690}
691
692/* sys I/F for thermal zone */
693
694#define to_thermal_zone(_dev) \
695 container_of(_dev, struct thermal_zone_device, device)
696
697static ssize_t
698type_show(struct device *dev, struct device_attribute *attr, char *buf)
699{
700 struct thermal_zone_device *tz = to_thermal_zone(dev);
701
702 return sprintf(buf, "%s\n", tz->type);
703}
704
705static ssize_t
706temp_show(struct device *dev, struct device_attribute *attr, char *buf)
707{
708 struct thermal_zone_device *tz = to_thermal_zone(dev);
709 int temperature, ret;
710
711 ret = thermal_zone_get_temp(tz, &temperature);
712
713 if (ret)
714 return ret;
715
716 return sprintf(buf, "%d\n", temperature);
717}
718
719static ssize_t
720mode_show(struct device *dev, struct device_attribute *attr, char *buf)
721{
722 struct thermal_zone_device *tz = to_thermal_zone(dev);
723 enum thermal_device_mode mode;
724 int result;
725
726 if (!tz->ops->get_mode)
727 return -EPERM;
728
729 result = tz->ops->get_mode(tz, &mode);
730 if (result)
731 return result;
732
733 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
734 : "disabled");
735}
736
737static ssize_t
738mode_store(struct device *dev, struct device_attribute *attr,
739 const char *buf, size_t count)
740{
741 struct thermal_zone_device *tz = to_thermal_zone(dev);
742 int result;
743
744 if (!tz->ops->set_mode)
745 return -EPERM;
746
747 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
748 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
749 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
750 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
751 else
752 result = -EINVAL;
753
754 if (result)
755 return result;
756
757 return count;
758}
759
760static ssize_t
761trip_point_type_show(struct device *dev, struct device_attribute *attr,
762 char *buf)
763{
764 struct thermal_zone_device *tz = to_thermal_zone(dev);
765 enum thermal_trip_type type;
766 int trip, result;
767
768 if (!tz->ops->get_trip_type)
769 return -EPERM;
770
771 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
772 return -EINVAL;
773
774 result = tz->ops->get_trip_type(tz, trip, &type);
775 if (result)
776 return result;
777
778 switch (type) {
779 case THERMAL_TRIP_CRITICAL:
780 return sprintf(buf, "critical\n");
781 case THERMAL_TRIP_HOT:
782 return sprintf(buf, "hot\n");
783 case THERMAL_TRIP_PASSIVE:
784 return sprintf(buf, "passive\n");
785 case THERMAL_TRIP_ACTIVE:
786 return sprintf(buf, "active\n");
787 default:
788 return sprintf(buf, "unknown\n");
789 }
790}
791
792static ssize_t
793trip_point_temp_store(struct device *dev, struct device_attribute *attr,
794 const char *buf, size_t count)
795{
796 struct thermal_zone_device *tz = to_thermal_zone(dev);
797 int trip, ret;
798 unsigned long temperature;
799
800 if (!tz->ops->set_trip_temp)
801 return -EPERM;
802
803 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
804 return -EINVAL;
805
806 if (kstrtoul(buf, 10, &temperature))
807 return -EINVAL;
808
809 ret = tz->ops->set_trip_temp(tz, trip, temperature);
810
811 return ret ? ret : count;
812}
813
814static ssize_t
815trip_point_temp_show(struct device *dev, struct device_attribute *attr,
816 char *buf)
817{
818 struct thermal_zone_device *tz = to_thermal_zone(dev);
819 int trip, ret;
820 int temperature;
821
822 if (!tz->ops->get_trip_temp)
823 return -EPERM;
824
825 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
826 return -EINVAL;
827
828 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
829
830 if (ret)
831 return ret;
832
833 return sprintf(buf, "%d\n", temperature);
834}
835
836static ssize_t
837trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
838 const char *buf, size_t count)
839{
840 struct thermal_zone_device *tz = to_thermal_zone(dev);
841 int trip, ret;
842 int temperature;
843
844 if (!tz->ops->set_trip_hyst)
845 return -EPERM;
846
847 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
848 return -EINVAL;
849
850 if (kstrtoint(buf, 10, &temperature))
851 return -EINVAL;
852
853 /*
854 * We are not doing any check on the 'temperature' value
855 * here. The driver implementing 'set_trip_hyst' has to
856 * take care of this.
857 */
858 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
859
860 return ret ? ret : count;
861}
862
863static ssize_t
864trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
865 char *buf)
866{
867 struct thermal_zone_device *tz = to_thermal_zone(dev);
868 int trip, ret;
869 int temperature;
870
871 if (!tz->ops->get_trip_hyst)
872 return -EPERM;
873
874 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
875 return -EINVAL;
876
877 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
878
879 return ret ? ret : sprintf(buf, "%d\n", temperature);
880}
881
882static ssize_t
883passive_store(struct device *dev, struct device_attribute *attr,
884 const char *buf, size_t count)
885{
886 struct thermal_zone_device *tz = to_thermal_zone(dev);
887 struct thermal_cooling_device *cdev = NULL;
888 int state;
889
890 if (!sscanf(buf, "%d\n", &state))
891 return -EINVAL;
892
893 /* sanity check: values below 1000 millicelcius don't make sense
894 * and can cause the system to go into a thermal heart attack
895 */
896 if (state && state < 1000)
897 return -EINVAL;
898
899 if (state && !tz->forced_passive) {
900 mutex_lock(&thermal_list_lock);
901 list_for_each_entry(cdev, &thermal_cdev_list, node) {
902 if (!strncmp("Processor", cdev->type,
903 sizeof("Processor")))
904 thermal_zone_bind_cooling_device(tz,
905 THERMAL_TRIPS_NONE, cdev,
906 THERMAL_NO_LIMIT,
907 THERMAL_NO_LIMIT,
908 THERMAL_WEIGHT_DEFAULT);
909 }
910 mutex_unlock(&thermal_list_lock);
911 if (!tz->passive_delay)
912 tz->passive_delay = 1000;
913 } else if (!state && tz->forced_passive) {
914 mutex_lock(&thermal_list_lock);
915 list_for_each_entry(cdev, &thermal_cdev_list, node) {
916 if (!strncmp("Processor", cdev->type,
917 sizeof("Processor")))
918 thermal_zone_unbind_cooling_device(tz,
919 THERMAL_TRIPS_NONE,
920 cdev);
921 }
922 mutex_unlock(&thermal_list_lock);
923 tz->passive_delay = 0;
924 }
925
926 tz->forced_passive = state;
927
928 thermal_zone_device_update(tz);
929
930 return count;
931}
932
933static ssize_t
934passive_show(struct device *dev, struct device_attribute *attr,
935 char *buf)
936{
937 struct thermal_zone_device *tz = to_thermal_zone(dev);
938
939 return sprintf(buf, "%d\n", tz->forced_passive);
940}
941
942static ssize_t
943policy_store(struct device *dev, struct device_attribute *attr,
944 const char *buf, size_t count)
945{
946 int ret = -EINVAL;
947 struct thermal_zone_device *tz = to_thermal_zone(dev);
948 struct thermal_governor *gov;
949 char name[THERMAL_NAME_LENGTH];
950
951 snprintf(name, sizeof(name), "%s", buf);
952
953 mutex_lock(&thermal_governor_lock);
954 mutex_lock(&tz->lock);
955
956 gov = __find_governor(strim(name));
957 if (!gov)
958 goto exit;
959
960 ret = thermal_set_governor(tz, gov);
961 if (!ret)
962 ret = count;
963
964exit:
965 mutex_unlock(&tz->lock);
966 mutex_unlock(&thermal_governor_lock);
967 return ret;
968}
969
970static ssize_t
971policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
972{
973 struct thermal_zone_device *tz = to_thermal_zone(dev);
974
975 return sprintf(buf, "%s\n", tz->governor->name);
976}
977
978static ssize_t
979available_policies_show(struct device *dev, struct device_attribute *devattr,
980 char *buf)
981{
982 struct thermal_governor *pos;
983 ssize_t count = 0;
984 ssize_t size = PAGE_SIZE;
985
986 mutex_lock(&thermal_governor_lock);
987
988 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
989 size = PAGE_SIZE - count;
990 count += scnprintf(buf + count, size, "%s ", pos->name);
991 }
992 count += scnprintf(buf + count, size, "\n");
993
994 mutex_unlock(&thermal_governor_lock);
995
996 return count;
997}
998
999static ssize_t
1000emul_temp_store(struct device *dev, struct device_attribute *attr,
1001 const char *buf, size_t count)
1002{
1003 struct thermal_zone_device *tz = to_thermal_zone(dev);
1004 int ret = 0;
1005 unsigned long temperature;
1006
1007 if (kstrtoul(buf, 10, &temperature))
1008 return -EINVAL;
1009
1010 if (!tz->ops->set_emul_temp) {
1011 mutex_lock(&tz->lock);
1012 tz->emul_temperature = temperature;
1013 mutex_unlock(&tz->lock);
1014 } else {
1015 ret = tz->ops->set_emul_temp(tz, temperature);
1016 }
1017
1018 if (!ret)
1019 thermal_zone_device_update(tz);
1020
1021 return ret ? ret : count;
1022}
1023static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
1024
1025static ssize_t
1026sustainable_power_show(struct device *dev, struct device_attribute *devattr,
1027 char *buf)
1028{
1029 struct thermal_zone_device *tz = to_thermal_zone(dev);
1030
1031 if (tz->tzp)
1032 return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
1033 else
1034 return -EIO;
1035}
1036
1037static ssize_t
1038sustainable_power_store(struct device *dev, struct device_attribute *devattr,
1039 const char *buf, size_t count)
1040{
1041 struct thermal_zone_device *tz = to_thermal_zone(dev);
1042 u32 sustainable_power;
1043
1044 if (!tz->tzp)
1045 return -EIO;
1046
1047 if (kstrtou32(buf, 10, &sustainable_power))
1048 return -EINVAL;
1049
1050 tz->tzp->sustainable_power = sustainable_power;
1051
1052 return count;
1053}
1054static DEVICE_ATTR(sustainable_power, S_IWUSR | S_IRUGO, sustainable_power_show,
1055 sustainable_power_store);
1056
1057#define create_s32_tzp_attr(name) \
1058 static ssize_t \
1059 name##_show(struct device *dev, struct device_attribute *devattr, \
1060 char *buf) \
1061 { \
1062 struct thermal_zone_device *tz = to_thermal_zone(dev); \
1063 \
1064 if (tz->tzp) \
1065 return sprintf(buf, "%u\n", tz->tzp->name); \
1066 else \
1067 return -EIO; \
1068 } \
1069 \
1070 static ssize_t \
1071 name##_store(struct device *dev, struct device_attribute *devattr, \
1072 const char *buf, size_t count) \
1073 { \
1074 struct thermal_zone_device *tz = to_thermal_zone(dev); \
1075 s32 value; \
1076 \
1077 if (!tz->tzp) \
1078 return -EIO; \
1079 \
1080 if (kstrtos32(buf, 10, &value)) \
1081 return -EINVAL; \
1082 \
1083 tz->tzp->name = value; \
1084 \
1085 return count; \
1086 } \
1087 static DEVICE_ATTR(name, S_IWUSR | S_IRUGO, name##_show, name##_store)
1088
1089create_s32_tzp_attr(k_po);
1090create_s32_tzp_attr(k_pu);
1091create_s32_tzp_attr(k_i);
1092create_s32_tzp_attr(k_d);
1093create_s32_tzp_attr(integral_cutoff);
1094create_s32_tzp_attr(slope);
1095create_s32_tzp_attr(offset);
1096create_s32_tzp_attr(integral_max);
1097#undef create_s32_tzp_attr
1098
1099static struct device_attribute *dev_tzp_attrs[] = {
1100 &dev_attr_sustainable_power,
1101 &dev_attr_k_po,
1102 &dev_attr_k_pu,
1103 &dev_attr_k_i,
1104 &dev_attr_k_d,
1105 &dev_attr_integral_cutoff,
1106 &dev_attr_slope,
1107 &dev_attr_offset,
1108 &dev_attr_integral_max,
1109};
1110
1111static int create_tzp_attrs(struct device *dev)
1112{
1113 int i;
1114
1115 for (i = 0; i < ARRAY_SIZE(dev_tzp_attrs); i++) {
1116 int ret;
1117 struct device_attribute *dev_attr = dev_tzp_attrs[i];
1118
1119 ret = device_create_file(dev, dev_attr);
1120 if (ret)
1121 return ret;
1122 }
1123
1124 return 0;
1125}
1126
1127/**
1128 * power_actor_get_max_power() - get the maximum power that a cdev can consume
1129 * @cdev: pointer to &thermal_cooling_device
1130 * @tz: a valid thermal zone device pointer
1131 * @max_power: pointer in which to store the maximum power
1132 *
1133 * Calculate the maximum power consumption in milliwats that the
1134 * cooling device can currently consume and store it in @max_power.
1135 *
1136 * Return: 0 on success, -EINVAL if @cdev doesn't support the
1137 * power_actor API or -E* on other error.
1138 */
1139int power_actor_get_max_power(struct thermal_cooling_device *cdev,
1140 struct thermal_zone_device *tz, u32 *max_power)
1141{
1142 if (!cdev_is_power_actor(cdev))
1143 return -EINVAL;
1144
1145 return cdev->ops->state2power(cdev, tz, 0, max_power);
1146}
1147
1148/**
1149 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
1150 * @cdev: pointer to &thermal_cooling_device
1151 * @tz: a valid thermal zone device pointer
1152 * @min_power: pointer in which to store the minimum power
1153 *
1154 * Calculate the minimum power consumption in milliwatts that the
1155 * cooling device can currently consume and store it in @min_power.
1156 *
1157 * Return: 0 on success, -EINVAL if @cdev doesn't support the
1158 * power_actor API or -E* on other error.
1159 */
1160int power_actor_get_min_power(struct thermal_cooling_device *cdev,
1161 struct thermal_zone_device *tz, u32 *min_power)
1162{
1163 unsigned long max_state;
1164 int ret;
1165
1166 if (!cdev_is_power_actor(cdev))
1167 return -EINVAL;
1168
1169 ret = cdev->ops->get_max_state(cdev, &max_state);
1170 if (ret)
1171 return ret;
1172
1173 return cdev->ops->state2power(cdev, tz, max_state, min_power);
1174}
1175
1176/**
1177 * power_actor_set_power() - limit the maximum power that a cooling device can consume
1178 * @cdev: pointer to &thermal_cooling_device
1179 * @instance: thermal instance to update
1180 * @power: the power in milliwatts
1181 *
1182 * Set the cooling device to consume at most @power milliwatts.
1183 *
1184 * Return: 0 on success, -EINVAL if the cooling device does not
1185 * implement the power actor API or -E* for other failures.
1186 */
1187int power_actor_set_power(struct thermal_cooling_device *cdev,
1188 struct thermal_instance *instance, u32 power)
1189{
1190 unsigned long state;
1191 int ret;
1192
1193 if (!cdev_is_power_actor(cdev))
1194 return -EINVAL;
1195
1196 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
1197 if (ret)
1198 return ret;
1199
1200 instance->target = state;
1201 cdev->updated = false;
1202 thermal_cdev_update(cdev);
1203
1204 return 0;
1205}
1206
1207static DEVICE_ATTR(type, 0444, type_show, NULL);
1208static DEVICE_ATTR(temp, 0444, temp_show, NULL);
1209static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
1210static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
1211static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
1212static DEVICE_ATTR(available_policies, S_IRUGO, available_policies_show, NULL);
1213
1214/* sys I/F for cooling device */
1215#define to_cooling_device(_dev) \
1216 container_of(_dev, struct thermal_cooling_device, device)
1217
1218static ssize_t
1219thermal_cooling_device_type_show(struct device *dev,
1220 struct device_attribute *attr, char *buf)
1221{
1222 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1223
1224 return sprintf(buf, "%s\n", cdev->type);
1225}
1226
1227static ssize_t
1228thermal_cooling_device_max_state_show(struct device *dev,
1229 struct device_attribute *attr, char *buf)
1230{
1231 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1232 unsigned long state;
1233 int ret;
1234
1235 ret = cdev->ops->get_max_state(cdev, &state);
1236 if (ret)
1237 return ret;
1238 return sprintf(buf, "%ld\n", state);
1239}
1240
1241static ssize_t
1242thermal_cooling_device_cur_state_show(struct device *dev,
1243 struct device_attribute *attr, char *buf)
1244{
1245 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1246 unsigned long state;
1247 int ret;
1248
1249 ret = cdev->ops->get_cur_state(cdev, &state);
1250 if (ret)
1251 return ret;
1252 return sprintf(buf, "%ld\n", state);
1253}
1254
1255static ssize_t
1256thermal_cooling_device_cur_state_store(struct device *dev,
1257 struct device_attribute *attr,
1258 const char *buf, size_t count)
1259{
1260 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1261 unsigned long state;
1262 int result;
1263
1264 if (!sscanf(buf, "%ld\n", &state))
1265 return -EINVAL;
1266
1267 if ((long)state < 0)
1268 return -EINVAL;
1269
1270 result = cdev->ops->set_cur_state(cdev, state);
1271 if (result)
1272 return result;
1273 return count;
1274}
1275
1276static struct device_attribute dev_attr_cdev_type =
1277__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
1278static DEVICE_ATTR(max_state, 0444,
1279 thermal_cooling_device_max_state_show, NULL);
1280static DEVICE_ATTR(cur_state, 0644,
1281 thermal_cooling_device_cur_state_show,
1282 thermal_cooling_device_cur_state_store);
1283
1284static ssize_t
1285thermal_cooling_device_trip_point_show(struct device *dev,
1286 struct device_attribute *attr, char *buf)
1287{
1288 struct thermal_instance *instance;
1289
1290 instance =
1291 container_of(attr, struct thermal_instance, attr);
1292
1293 if (instance->trip == THERMAL_TRIPS_NONE)
1294 return sprintf(buf, "-1\n");
1295 else
1296 return sprintf(buf, "%d\n", instance->trip);
1297}
1298
1299static struct attribute *cooling_device_attrs[] = {
1300 &dev_attr_cdev_type.attr,
1301 &dev_attr_max_state.attr,
1302 &dev_attr_cur_state.attr,
1303 NULL,
1304};
1305
1306static const struct attribute_group cooling_device_attr_group = {
1307 .attrs = cooling_device_attrs,
1308};
1309
1310static const struct attribute_group *cooling_device_attr_groups[] = {
1311 &cooling_device_attr_group,
1312 NULL,
1313};
1314
1315static ssize_t
1316thermal_cooling_device_weight_show(struct device *dev,
1317 struct device_attribute *attr, char *buf)
1318{
1319 struct thermal_instance *instance;
1320
1321 instance = container_of(attr, struct thermal_instance, weight_attr);
1322
1323 return sprintf(buf, "%d\n", instance->weight);
1324}
1325
1326static ssize_t
1327thermal_cooling_device_weight_store(struct device *dev,
1328 struct device_attribute *attr,
1329 const char *buf, size_t count)
1330{
1331 struct thermal_instance *instance;
1332 int ret, weight;
1333
1334 ret = kstrtoint(buf, 0, &weight);
1335 if (ret)
1336 return ret;
1337
1338 instance = container_of(attr, struct thermal_instance, weight_attr);
1339 instance->weight = weight;
1340
1341 return count;
1342}
1343/* Device management */
1344
1345/**
1346 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1347 * @tz: pointer to struct thermal_zone_device
1348 * @trip: indicates which trip point the cooling devices is
1349 * associated with in this thermal zone.
1350 * @cdev: pointer to struct thermal_cooling_device
1351 * @upper: the Maximum cooling state for this trip point.
1352 * THERMAL_NO_LIMIT means no upper limit,
1353 * and the cooling device can be in max_state.
1354 * @lower: the Minimum cooling state can be used for this trip point.
1355 * THERMAL_NO_LIMIT means no lower limit,
1356 * and the cooling device can be in cooling state 0.
1357 * @weight: The weight of the cooling device to be bound to the
1358 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
1359 * default value
1360 *
1361 * This interface function bind a thermal cooling device to the certain trip
1362 * point of a thermal zone device.
1363 * This function is usually called in the thermal zone device .bind callback.
1364 *
1365 * Return: 0 on success, the proper error value otherwise.
1366 */
1367int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1368 int trip,
1369 struct thermal_cooling_device *cdev,
1370 unsigned long upper, unsigned long lower,
1371 unsigned int weight)
1372{
1373 struct thermal_instance *dev;
1374 struct thermal_instance *pos;
1375 struct thermal_zone_device *pos1;
1376 struct thermal_cooling_device *pos2;
1377 unsigned long max_state;
1378 int result, ret;
1379
1380 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
1381 return -EINVAL;
1382
1383 list_for_each_entry(pos1, &thermal_tz_list, node) {
1384 if (pos1 == tz)
1385 break;
1386 }
1387 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1388 if (pos2 == cdev)
1389 break;
1390 }
1391
1392 if (tz != pos1 || cdev != pos2)
1393 return -EINVAL;
1394
1395 ret = cdev->ops->get_max_state(cdev, &max_state);
1396 if (ret)
1397 return ret;
1398
1399 /* lower default 0, upper default max_state */
1400 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1401 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1402
1403 if (lower > upper || upper > max_state)
1404 return -EINVAL;
1405
1406 dev =
1407 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
1408 if (!dev)
1409 return -ENOMEM;
1410 dev->tz = tz;
1411 dev->cdev = cdev;
1412 dev->trip = trip;
1413 dev->upper = upper;
1414 dev->lower = lower;
1415 dev->target = THERMAL_NO_TARGET;
1416 dev->weight = weight;
1417
1418 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1419 if (result)
1420 goto free_mem;
1421
1422 sprintf(dev->name, "cdev%d", dev->id);
1423 result =
1424 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1425 if (result)
1426 goto release_idr;
1427
1428 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
1429 sysfs_attr_init(&dev->attr.attr);
1430 dev->attr.attr.name = dev->attr_name;
1431 dev->attr.attr.mode = 0444;
1432 dev->attr.show = thermal_cooling_device_trip_point_show;
1433 result = device_create_file(&tz->device, &dev->attr);
1434 if (result)
1435 goto remove_symbol_link;
1436
1437 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
1438 sysfs_attr_init(&dev->weight_attr.attr);
1439 dev->weight_attr.attr.name = dev->weight_attr_name;
1440 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
1441 dev->weight_attr.show = thermal_cooling_device_weight_show;
1442 dev->weight_attr.store = thermal_cooling_device_weight_store;
1443 result = device_create_file(&tz->device, &dev->weight_attr);
1444 if (result)
1445 goto remove_trip_file;
1446
1447 mutex_lock(&tz->lock);
1448 mutex_lock(&cdev->lock);
1449 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
1450 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1451 result = -EEXIST;
1452 break;
1453 }
1454 if (!result) {
1455 list_add_tail(&dev->tz_node, &tz->thermal_instances);
1456 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1457 atomic_set(&tz->need_update, 1);
1458 }
1459 mutex_unlock(&cdev->lock);
1460 mutex_unlock(&tz->lock);
1461
1462 if (!result)
1463 return 0;
1464
1465 device_remove_file(&tz->device, &dev->weight_attr);
1466remove_trip_file:
1467 device_remove_file(&tz->device, &dev->attr);
1468remove_symbol_link:
1469 sysfs_remove_link(&tz->device.kobj, dev->name);
1470release_idr:
1471 release_idr(&tz->idr, &tz->lock, dev->id);
1472free_mem:
1473 kfree(dev);
1474 return result;
1475}
1476EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
1477
1478/**
1479 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1480 * thermal zone.
1481 * @tz: pointer to a struct thermal_zone_device.
1482 * @trip: indicates which trip point the cooling devices is
1483 * associated with in this thermal zone.
1484 * @cdev: pointer to a struct thermal_cooling_device.
1485 *
1486 * This interface function unbind a thermal cooling device from the certain
1487 * trip point of a thermal zone device.
1488 * This function is usually called in the thermal zone device .unbind callback.
1489 *
1490 * Return: 0 on success, the proper error value otherwise.
1491 */
1492int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1493 int trip,
1494 struct thermal_cooling_device *cdev)
1495{
1496 struct thermal_instance *pos, *next;
1497
1498 mutex_lock(&tz->lock);
1499 mutex_lock(&cdev->lock);
1500 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
1501 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1502 list_del(&pos->tz_node);
1503 list_del(&pos->cdev_node);
1504 mutex_unlock(&cdev->lock);
1505 mutex_unlock(&tz->lock);
1506 goto unbind;
1507 }
1508 }
1509 mutex_unlock(&cdev->lock);
1510 mutex_unlock(&tz->lock);
1511
1512 return -ENODEV;
1513
1514unbind:
1515 device_remove_file(&tz->device, &pos->weight_attr);
1516 device_remove_file(&tz->device, &pos->attr);
1517 sysfs_remove_link(&tz->device.kobj, pos->name);
1518 release_idr(&tz->idr, &tz->lock, pos->id);
1519 kfree(pos);
1520 return 0;
1521}
1522EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
1523
1524static void thermal_release(struct device *dev)
1525{
1526 struct thermal_zone_device *tz;
1527 struct thermal_cooling_device *cdev;
1528
1529 if (!strncmp(dev_name(dev), "thermal_zone",
1530 sizeof("thermal_zone") - 1)) {
1531 tz = to_thermal_zone(dev);
1532 kfree(tz);
1533 } else if(!strncmp(dev_name(dev), "cooling_device",
1534 sizeof("cooling_device") - 1)){
1535 cdev = to_cooling_device(dev);
1536 kfree(cdev);
1537 }
1538}
1539
1540static struct class thermal_class = {
1541 .name = "thermal",
1542 .dev_release = thermal_release,
1543};
1544
1545/**
1546 * __thermal_cooling_device_register() - register a new thermal cooling device
1547 * @np: a pointer to a device tree node.
1548 * @type: the thermal cooling device type.
1549 * @devdata: device private data.
1550 * @ops: standard thermal cooling devices callbacks.
1551 *
1552 * This interface function adds a new thermal cooling device (fan/processor/...)
1553 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1554 * to all the thermal zone devices registered at the same time.
1555 * It also gives the opportunity to link the cooling device to a device tree
1556 * node, so that it can be bound to a thermal zone created out of device tree.
1557 *
1558 * Return: a pointer to the created struct thermal_cooling_device or an
1559 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1560 */
1561static struct thermal_cooling_device *
1562__thermal_cooling_device_register(struct device_node *np,
1563 char *type, void *devdata,
1564 const struct thermal_cooling_device_ops *ops)
1565{
1566 struct thermal_cooling_device *cdev;
1567 struct thermal_zone_device *pos = NULL;
1568 int result;
1569
1570 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1571 return ERR_PTR(-EINVAL);
1572
1573 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1574 !ops->set_cur_state)
1575 return ERR_PTR(-EINVAL);
1576
1577 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1578 if (!cdev)
1579 return ERR_PTR(-ENOMEM);
1580
1581 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1582 if (result) {
1583 kfree(cdev);
1584 return ERR_PTR(result);
1585 }
1586
1587 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
1588 mutex_init(&cdev->lock);
1589 INIT_LIST_HEAD(&cdev->thermal_instances);
1590 cdev->np = np;
1591 cdev->ops = ops;
1592 cdev->updated = false;
1593 cdev->device.class = &thermal_class;
1594 cdev->device.groups = cooling_device_attr_groups;
1595 cdev->devdata = devdata;
1596 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1597 result = device_register(&cdev->device);
1598 if (result) {
1599 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1600 kfree(cdev);
1601 return ERR_PTR(result);
1602 }
1603
1604 /* Add 'this' new cdev to the global cdev list */
1605 mutex_lock(&thermal_list_lock);
1606 list_add(&cdev->node, &thermal_cdev_list);
1607 mutex_unlock(&thermal_list_lock);
1608
1609 /* Update binding information for 'this' new cdev */
1610 bind_cdev(cdev);
1611
1612 mutex_lock(&thermal_list_lock);
1613 list_for_each_entry(pos, &thermal_tz_list, node)
1614 if (atomic_cmpxchg(&pos->need_update, 1, 0))
1615 thermal_zone_device_update(pos);
1616 mutex_unlock(&thermal_list_lock);
1617
1618 return cdev;
1619}
1620
1621/**
1622 * thermal_cooling_device_register() - register a new thermal cooling device
1623 * @type: the thermal cooling device type.
1624 * @devdata: device private data.
1625 * @ops: standard thermal cooling devices callbacks.
1626 *
1627 * This interface function adds a new thermal cooling device (fan/processor/...)
1628 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1629 * to all the thermal zone devices registered at the same time.
1630 *
1631 * Return: a pointer to the created struct thermal_cooling_device or an
1632 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1633 */
1634struct thermal_cooling_device *
1635thermal_cooling_device_register(char *type, void *devdata,
1636 const struct thermal_cooling_device_ops *ops)
1637{
1638 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1639}
1640EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1641
1642/**
1643 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1644 * @np: a pointer to a device tree node.
1645 * @type: the thermal cooling device type.
1646 * @devdata: device private data.
1647 * @ops: standard thermal cooling devices callbacks.
1648 *
1649 * This function will register a cooling device with device tree node reference.
1650 * This interface function adds a new thermal cooling device (fan/processor/...)
1651 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1652 * to all the thermal zone devices registered at the same time.
1653 *
1654 * Return: a pointer to the created struct thermal_cooling_device or an
1655 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1656 */
1657struct thermal_cooling_device *
1658thermal_of_cooling_device_register(struct device_node *np,
1659 char *type, void *devdata,
1660 const struct thermal_cooling_device_ops *ops)
1661{
1662 return __thermal_cooling_device_register(np, type, devdata, ops);
1663}
1664EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1665
1666/**
1667 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1668 * @cdev: the thermal cooling device to remove.
1669 *
1670 * thermal_cooling_device_unregister() must be called when the device is no
1671 * longer needed.
1672 */
1673void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1674{
1675 int i;
1676 const struct thermal_zone_params *tzp;
1677 struct thermal_zone_device *tz;
1678 struct thermal_cooling_device *pos = NULL;
1679
1680 if (!cdev)
1681 return;
1682
1683 mutex_lock(&thermal_list_lock);
1684 list_for_each_entry(pos, &thermal_cdev_list, node)
1685 if (pos == cdev)
1686 break;
1687 if (pos != cdev) {
1688 /* thermal cooling device not found */
1689 mutex_unlock(&thermal_list_lock);
1690 return;
1691 }
1692 list_del(&cdev->node);
1693
1694 /* Unbind all thermal zones associated with 'this' cdev */
1695 list_for_each_entry(tz, &thermal_tz_list, node) {
1696 if (tz->ops->unbind) {
1697 tz->ops->unbind(tz, cdev);
1698 continue;
1699 }
1700
1701 if (!tz->tzp || !tz->tzp->tbp)
1702 continue;
1703
1704 tzp = tz->tzp;
1705 for (i = 0; i < tzp->num_tbps; i++) {
1706 if (tzp->tbp[i].cdev == cdev) {
1707 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1708 tzp->tbp[i].cdev = NULL;
1709 }
1710 }
1711 }
1712
1713 mutex_unlock(&thermal_list_lock);
1714
1715 if (cdev->type[0])
1716 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1717 device_remove_file(&cdev->device, &dev_attr_max_state);
1718 device_remove_file(&cdev->device, &dev_attr_cur_state);
1719
1720 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1721 device_unregister(&cdev->device);
1722 return;
1723}
1724EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1725
1726void thermal_cdev_update(struct thermal_cooling_device *cdev)
1727{
1728 struct thermal_instance *instance;
1729 unsigned long target = 0;
1730
1731 /* cooling device is updated*/
1732 if (cdev->updated)
1733 return;
1734
1735 mutex_lock(&cdev->lock);
1736 /* Make sure cdev enters the deepest cooling state */
1737 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1738 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1739 instance->tz->id, instance->target);
1740 if (instance->target == THERMAL_NO_TARGET)
1741 continue;
1742 if (instance->target > target)
1743 target = instance->target;
1744 }
1745 mutex_unlock(&cdev->lock);
1746 cdev->ops->set_cur_state(cdev, target);
1747 cdev->updated = true;
1748 trace_cdev_update(cdev, target);
1749 dev_dbg(&cdev->device, "set to state %lu\n", target);
1750}
1751EXPORT_SYMBOL(thermal_cdev_update);
1752
1753/**
1754 * thermal_notify_framework - Sensor drivers use this API to notify framework
1755 * @tz: thermal zone device
1756 * @trip: indicates which trip point has been crossed
1757 *
1758 * This function handles the trip events from sensor drivers. It starts
1759 * throttling the cooling devices according to the policy configured.
1760 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1761 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1762 * The throttling policy is based on the configured platform data; if no
1763 * platform data is provided, this uses the step_wise throttling policy.
1764 */
1765void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
1766{
1767 if (atomic_read(&in_suspend))
1768 return;
1769
1770 handle_thermal_trip(tz, trip);
1771}
1772EXPORT_SYMBOL_GPL(thermal_notify_framework);
1773
1774/**
1775 * create_trip_attrs() - create attributes for trip points
1776 * @tz: the thermal zone device
1777 * @mask: Writeable trip point bitmap.
1778 *
1779 * helper function to instantiate sysfs entries for every trip
1780 * point and its properties of a struct thermal_zone_device.
1781 *
1782 * Return: 0 on success, the proper error value otherwise.
1783 */
1784static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1785{
1786 int indx;
1787 int size = sizeof(struct thermal_attr) * tz->trips;
1788
1789 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1790 if (!tz->trip_type_attrs)
1791 return -ENOMEM;
1792
1793 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1794 if (!tz->trip_temp_attrs) {
1795 kfree(tz->trip_type_attrs);
1796 return -ENOMEM;
1797 }
1798
1799 if (tz->ops->get_trip_hyst) {
1800 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1801 if (!tz->trip_hyst_attrs) {
1802 kfree(tz->trip_type_attrs);
1803 kfree(tz->trip_temp_attrs);
1804 return -ENOMEM;
1805 }
1806 }
1807
1808
1809 for (indx = 0; indx < tz->trips; indx++) {
1810 /* create trip type attribute */
1811 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1812 "trip_point_%d_type", indx);
1813
1814 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1815 tz->trip_type_attrs[indx].attr.attr.name =
1816 tz->trip_type_attrs[indx].name;
1817 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1818 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1819
1820 device_create_file(&tz->device,
1821 &tz->trip_type_attrs[indx].attr);
1822
1823 /* create trip temp attribute */
1824 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1825 "trip_point_%d_temp", indx);
1826
1827 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1828 tz->trip_temp_attrs[indx].attr.attr.name =
1829 tz->trip_temp_attrs[indx].name;
1830 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1831 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1832 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
1833 mask & (1 << indx)) {
1834 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1835 tz->trip_temp_attrs[indx].attr.store =
1836 trip_point_temp_store;
1837 }
1838
1839 device_create_file(&tz->device,
1840 &tz->trip_temp_attrs[indx].attr);
1841
1842 /* create Optional trip hyst attribute */
1843 if (!tz->ops->get_trip_hyst)
1844 continue;
1845 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1846 "trip_point_%d_hyst", indx);
1847
1848 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1849 tz->trip_hyst_attrs[indx].attr.attr.name =
1850 tz->trip_hyst_attrs[indx].name;
1851 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1852 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1853 if (tz->ops->set_trip_hyst) {
1854 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1855 tz->trip_hyst_attrs[indx].attr.store =
1856 trip_point_hyst_store;
1857 }
1858
1859 device_create_file(&tz->device,
1860 &tz->trip_hyst_attrs[indx].attr);
1861 }
1862 return 0;
1863}
1864
1865static void remove_trip_attrs(struct thermal_zone_device *tz)
1866{
1867 int indx;
1868
1869 for (indx = 0; indx < tz->trips; indx++) {
1870 device_remove_file(&tz->device,
1871 &tz->trip_type_attrs[indx].attr);
1872 device_remove_file(&tz->device,
1873 &tz->trip_temp_attrs[indx].attr);
1874 if (tz->ops->get_trip_hyst)
1875 device_remove_file(&tz->device,
1876 &tz->trip_hyst_attrs[indx].attr);
1877 }
1878 kfree(tz->trip_type_attrs);
1879 kfree(tz->trip_temp_attrs);
1880 kfree(tz->trip_hyst_attrs);
1881}
1882
1883/**
1884 * thermal_zone_device_register() - register a new thermal zone device
1885 * @type: the thermal zone device type
1886 * @trips: the number of trip points the thermal zone support
1887 * @mask: a bit string indicating the writeablility of trip points
1888 * @devdata: private device data
1889 * @ops: standard thermal zone device callbacks
1890 * @tzp: thermal zone platform parameters
1891 * @passive_delay: number of milliseconds to wait between polls when
1892 * performing passive cooling
1893 * @polling_delay: number of milliseconds to wait between polls when checking
1894 * whether trip points have been crossed (0 for interrupt
1895 * driven systems)
1896 *
1897 * This interface function adds a new thermal zone device (sensor) to
1898 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1899 * thermal cooling devices registered at the same time.
1900 * thermal_zone_device_unregister() must be called when the device is no
1901 * longer needed. The passive cooling depends on the .get_trend() return value.
1902 *
1903 * Return: a pointer to the created struct thermal_zone_device or an
1904 * in case of error, an ERR_PTR. Caller must check return value with
1905 * IS_ERR*() helpers.
1906 */
1907struct thermal_zone_device *thermal_zone_device_register(const char *type,
1908 int trips, int mask, void *devdata,
1909 struct thermal_zone_device_ops *ops,
1910 struct thermal_zone_params *tzp,
1911 int passive_delay, int polling_delay)
1912{
1913 struct thermal_zone_device *tz;
1914 enum thermal_trip_type trip_type;
1915 int trip_temp;
1916 int result;
1917 int count;
1918 int passive = 0;
1919 struct thermal_governor *governor;
1920
1921 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1922 return ERR_PTR(-EINVAL);
1923
1924 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1925 return ERR_PTR(-EINVAL);
1926
1927 if (!ops)
1928 return ERR_PTR(-EINVAL);
1929
1930 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1931 return ERR_PTR(-EINVAL);
1932
1933 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1934 if (!tz)
1935 return ERR_PTR(-ENOMEM);
1936
1937 INIT_LIST_HEAD(&tz->thermal_instances);
1938 idr_init(&tz->idr);
1939 mutex_init(&tz->lock);
1940 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1941 if (result) {
1942 kfree(tz);
1943 return ERR_PTR(result);
1944 }
1945
1946 strlcpy(tz->type, type ? : "", sizeof(tz->type));
1947 tz->ops = ops;
1948 tz->tzp = tzp;
1949 tz->device.class = &thermal_class;
1950 tz->devdata = devdata;
1951 tz->trips = trips;
1952 tz->passive_delay = passive_delay;
1953 tz->polling_delay = polling_delay;
1954#ifdef CONFIG_SCHED_HMP
1955 tz->poll_queue_cpu = BOUNDED_CPU;
1956#endif
1957 /* A new thermal zone needs to be updated anyway. */
1958 atomic_set(&tz->need_update, 1);
1959
1960 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1961 result = device_register(&tz->device);
1962 if (result) {
1963 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1964 kfree(tz);
1965 return ERR_PTR(result);
1966 }
1967
1968 /* sys I/F */
1969 if (type) {
1970 result = device_create_file(&tz->device, &dev_attr_type);
1971 if (result)
1972 goto unregister;
1973 }
1974
1975 result = device_create_file(&tz->device, &dev_attr_temp);
1976 if (result)
1977 goto unregister;
1978
1979 if (ops->get_mode) {
1980 result = device_create_file(&tz->device, &dev_attr_mode);
1981 if (result)
1982 goto unregister;
1983 }
1984
1985 result = create_trip_attrs(tz, mask);
1986 if (result)
1987 goto unregister;
1988
1989 for (count = 0; count < trips; count++) {
1990 if (tz->ops->get_trip_type(tz, count, &trip_type))
1991 set_bit(count, &tz->trips_disabled);
1992 if (trip_type == THERMAL_TRIP_PASSIVE)
1993 passive = 1;
1994 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
1995 set_bit(count, &tz->trips_disabled);
1996 /* Check for bogus trip points */
1997 if (trip_temp == 0)
1998 set_bit(count, &tz->trips_disabled);
1999 }
2000
2001 if (!passive) {
2002 result = device_create_file(&tz->device, &dev_attr_passive);
2003 if (result)
2004 goto unregister;
2005 }
2006
2007 if (IS_ENABLED(CONFIG_THERMAL_EMULATION)) {
2008 result = device_create_file(&tz->device, &dev_attr_emul_temp);
2009 if (result)
2010 goto unregister;
2011 }
2012
2013 /* Create policy attribute */
2014 result = device_create_file(&tz->device, &dev_attr_policy);
2015 if (result)
2016 goto unregister;
2017
2018 /* Add thermal zone params */
2019 result = create_tzp_attrs(&tz->device);
2020 if (result)
2021 goto unregister;
2022
2023 /* Create available_policies attribute */
2024 result = device_create_file(&tz->device, &dev_attr_available_policies);
2025 if (result)
2026 goto unregister;
2027
2028 /* Update 'this' zone's governor information */
2029 mutex_lock(&thermal_governor_lock);
2030
2031 if (tz->tzp)
2032 governor = __find_governor(tz->tzp->governor_name);
2033 else
2034 governor = def_governor;
2035
2036 result = thermal_set_governor(tz, governor);
2037 if (result) {
2038 mutex_unlock(&thermal_governor_lock);
2039 goto unregister;
2040 }
2041
2042 mutex_unlock(&thermal_governor_lock);
2043
2044 if (!tz->tzp || !tz->tzp->no_hwmon) {
2045 result = thermal_add_hwmon_sysfs(tz);
2046 if (result)
2047 goto unregister;
2048 }
2049
2050 mutex_lock(&thermal_list_lock);
2051 list_add_tail(&tz->node, &thermal_tz_list);
2052 mutex_unlock(&thermal_list_lock);
2053
2054 /* Bind cooling devices for this zone */
2055 bind_tz(tz);
2056
2057 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
2058
2059 thermal_zone_device_reset(tz);
2060 /* Update the new thermal zone and mark it as already updated. */
2061 if (atomic_cmpxchg(&tz->need_update, 1, 0))
2062 thermal_zone_device_update(tz);
2063
2064 return tz;
2065
2066unregister:
2067 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2068 device_unregister(&tz->device);
2069 return ERR_PTR(result);
2070}
2071EXPORT_SYMBOL_GPL(thermal_zone_device_register);
2072
2073/**
2074 * thermal_device_unregister - removes the registered thermal zone device
2075 * @tz: the thermal zone device to remove
2076 */
2077void thermal_zone_device_unregister(struct thermal_zone_device *tz)
2078{
2079 int i;
2080 const struct thermal_zone_params *tzp;
2081 struct thermal_cooling_device *cdev;
2082 struct thermal_zone_device *pos = NULL;
2083
2084 if (!tz)
2085 return;
2086
2087 tzp = tz->tzp;
2088
2089 mutex_lock(&thermal_list_lock);
2090 list_for_each_entry(pos, &thermal_tz_list, node)
2091 if (pos == tz)
2092 break;
2093 if (pos != tz) {
2094 /* thermal zone device not found */
2095 mutex_unlock(&thermal_list_lock);
2096 return;
2097 }
2098 list_del(&tz->node);
2099
2100 /* Unbind all cdevs associated with 'this' thermal zone */
2101 list_for_each_entry(cdev, &thermal_cdev_list, node) {
2102 if (tz->ops->unbind) {
2103 tz->ops->unbind(tz, cdev);
2104 continue;
2105 }
2106
2107 if (!tzp || !tzp->tbp)
2108 break;
2109
2110 for (i = 0; i < tzp->num_tbps; i++) {
2111 if (tzp->tbp[i].cdev == cdev) {
2112 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
2113 tzp->tbp[i].cdev = NULL;
2114 }
2115 }
2116 }
2117
2118 mutex_unlock(&thermal_list_lock);
2119
2120 thermal_zone_device_set_polling(tz, 0);
2121
2122 if (tz->type[0])
2123 device_remove_file(&tz->device, &dev_attr_type);
2124 device_remove_file(&tz->device, &dev_attr_temp);
2125 if (tz->ops->get_mode)
2126 device_remove_file(&tz->device, &dev_attr_mode);
2127 device_remove_file(&tz->device, &dev_attr_policy);
2128 device_remove_file(&tz->device, &dev_attr_available_policies);
2129 remove_trip_attrs(tz);
2130 thermal_set_governor(tz, NULL);
2131
2132 thermal_remove_hwmon_sysfs(tz);
2133 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2134 idr_destroy(&tz->idr);
2135 mutex_destroy(&tz->lock);
2136 device_unregister(&tz->device);
2137 return;
2138}
2139EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
2140
2141/**
2142 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
2143 * @name: thermal zone name to fetch the temperature
2144 *
2145 * When only one zone is found with the passed name, returns a reference to it.
2146 *
2147 * Return: On success returns a reference to an unique thermal zone with
2148 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
2149 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
2150 */
2151struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
2152{
2153 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
2154 unsigned int found = 0;
2155
2156 if (!name)
2157 goto exit;
2158
2159 mutex_lock(&thermal_list_lock);
2160 list_for_each_entry(pos, &thermal_tz_list, node)
2161 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
2162 found++;
2163 ref = pos;
2164 }
2165 mutex_unlock(&thermal_list_lock);
2166
2167 /* nothing has been found, thus an error code for it */
2168 if (found == 0)
2169 ref = ERR_PTR(-ENODEV);
2170 else if (found > 1)
2171 /* Success only when an unique zone is found */
2172 ref = ERR_PTR(-EEXIST);
2173
2174exit:
2175 return ref;
2176}
2177EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
2178
2179#ifdef CONFIG_NET
2180static const struct genl_multicast_group thermal_event_mcgrps[] = {
2181 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
2182};
2183
2184static struct genl_family thermal_event_genl_family = {
2185 .id = GENL_ID_GENERATE,
2186 .name = THERMAL_GENL_FAMILY_NAME,
2187 .version = THERMAL_GENL_VERSION,
2188 .maxattr = THERMAL_GENL_ATTR_MAX,
2189 .mcgrps = thermal_event_mcgrps,
2190 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
2191};
2192
2193int thermal_generate_netlink_event(struct thermal_zone_device *tz,
2194 enum events event)
2195{
2196 struct sk_buff *skb;
2197 struct nlattr *attr;
2198 struct thermal_genl_event *thermal_event;
2199 void *msg_header;
2200 int size;
2201 int result;
2202 static unsigned int thermal_event_seqnum;
2203
2204 if (!tz)
2205 return -EINVAL;
2206
2207 /* allocate memory */
2208 size = nla_total_size(sizeof(struct thermal_genl_event)) +
2209 nla_total_size(0);
2210
2211 skb = genlmsg_new(size, GFP_ATOMIC);
2212 if (!skb)
2213 return -ENOMEM;
2214
2215 /* add the genetlink message header */
2216 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
2217 &thermal_event_genl_family, 0,
2218 THERMAL_GENL_CMD_EVENT);
2219 if (!msg_header) {
2220 nlmsg_free(skb);
2221 return -ENOMEM;
2222 }
2223
2224 /* fill the data */
2225 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
2226 sizeof(struct thermal_genl_event));
2227
2228 if (!attr) {
2229 nlmsg_free(skb);
2230 return -EINVAL;
2231 }
2232
2233 thermal_event = nla_data(attr);
2234 if (!thermal_event) {
2235 nlmsg_free(skb);
2236 return -EINVAL;
2237 }
2238
2239 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
2240
2241 thermal_event->orig = tz->id;
2242 thermal_event->event = event;
2243
2244 /* send multicast genetlink message */
2245 genlmsg_end(skb, msg_header);
2246
2247 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
2248 0, GFP_ATOMIC);
2249 if (result)
2250 dev_err(&tz->device, "Failed to send netlink event:%d", result);
2251
2252 return result;
2253}
2254EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
2255
2256static int genetlink_init(void)
2257{
2258 return genl_register_family(&thermal_event_genl_family);
2259}
2260
2261static void genetlink_exit(void)
2262{
2263 genl_unregister_family(&thermal_event_genl_family);
2264}
2265#else /* !CONFIG_NET */
2266static inline int genetlink_init(void) { return 0; }
2267static inline void genetlink_exit(void) {}
2268#endif /* !CONFIG_NET */
2269
2270static int __init thermal_register_governors(void)
2271{
2272 int result;
2273
2274 result = thermal_gov_step_wise_register();
2275 if (result)
2276 return result;
2277
2278 result = thermal_gov_fair_share_register();
2279 if (result)
2280 return result;
2281
2282 result = thermal_gov_bang_bang_register();
2283 if (result)
2284 return result;
2285
2286 result = thermal_gov_user_space_register();
2287 if (result)
2288 return result;
2289
2290 return thermal_gov_power_allocator_register();
2291}
2292
2293static void thermal_unregister_governors(void)
2294{
2295 thermal_gov_step_wise_unregister();
2296 thermal_gov_fair_share_unregister();
2297 thermal_gov_bang_bang_unregister();
2298 thermal_gov_user_space_unregister();
2299 thermal_gov_power_allocator_unregister();
2300}
2301
2302#ifdef CONFIG_SCHED_HMP
2303static int thermal_cpu_callback(struct notifier_block *nfb,
2304 unsigned long action, void *hcpu)
2305{
2306 unsigned long cpu = (unsigned long)hcpu;
2307 struct thermal_zone_device *pos;
2308
2309 switch (action) {
2310 case CPU_ONLINE:
2311 if (cpu == BOUNDED_CPU) {
2312 list_for_each_entry(pos, &thermal_tz_list, node) {
2313 pos->poll_queue_cpu = BOUNDED_CPU;
2314 if (pos->polling_delay) {
2315 start_poll_queue(pos, pos->polling_delay);
2316 }
2317 }
2318 }
2319 break;
2320 case CPU_DOWN_PREPARE:
2321 list_for_each_entry(pos, &thermal_tz_list, node) {
2322 if (pos->poll_queue_cpu == cpu) {
2323 pos->poll_queue_cpu = 0;
2324 if (pos->polling_delay)
2325 start_poll_queue(pos, pos->polling_delay);
2326 }
2327 }
2328 break;
2329 }
2330 return NOTIFY_OK;
2331}
2332
2333static struct notifier_block thermal_cpu_notifier =
2334{
2335 .notifier_call = thermal_cpu_callback,
2336};
2337#endif
2338
2339static int thermal_pm_notify(struct notifier_block *nb,
2340 unsigned long mode, void *_unused)
2341{
2342 struct thermal_zone_device *tz;
2343
2344 switch (mode) {
2345 case PM_HIBERNATION_PREPARE:
2346 case PM_RESTORE_PREPARE:
2347 case PM_SUSPEND_PREPARE:
2348 atomic_set(&in_suspend, 1);
2349 break;
2350 case PM_POST_HIBERNATION:
2351 case PM_POST_RESTORE:
2352 case PM_POST_SUSPEND:
2353 atomic_set(&in_suspend, 0);
2354 list_for_each_entry(tz, &thermal_tz_list, node) {
2355 thermal_zone_device_reset(tz);
2356 thermal_zone_device_update(tz);
2357 }
2358 break;
2359 default:
2360 break;
2361 }
2362 return 0;
2363}
2364
2365static struct notifier_block thermal_pm_nb = {
2366 .notifier_call = thermal_pm_notify,
2367};
2368
2369static int __init thermal_init(void)
2370{
2371 int result;
2372
2373 result = thermal_register_governors();
2374 if (result)
2375 goto error;
2376
2377 result = class_register(&thermal_class);
2378 if (result)
2379 goto unregister_governors;
2380
2381 result = genetlink_init();
2382 if (result)
2383 goto unregister_class;
2384
2385 result = of_parse_thermal_zones();
2386 if (result)
2387 goto exit_netlink;
2388
2389#ifdef CONFIG_SCHED_HMP
2390 register_hotcpu_notifier(&thermal_cpu_notifier);
2391#endif
2392 result = register_pm_notifier(&thermal_pm_nb);
2393 if (result)
2394 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
2395 result);
2396
2397 return 0;
2398
2399exit_netlink:
2400 genetlink_exit();
2401unregister_class:
2402 class_unregister(&thermal_class);
2403unregister_governors:
2404 thermal_unregister_governors();
2405error:
2406 idr_destroy(&thermal_tz_idr);
2407 idr_destroy(&thermal_cdev_idr);
2408 mutex_destroy(&thermal_idr_lock);
2409 mutex_destroy(&thermal_list_lock);
2410 mutex_destroy(&thermal_governor_lock);
2411 return result;
2412}
2413
2414static void __exit thermal_exit(void)
2415{
2416 unregister_pm_notifier(&thermal_pm_nb);
2417#ifdef CONFIG_SCHED_HMP
2418 unregister_hotcpu_notifier(&thermal_cpu_notifier);
2419#endif
2420 of_thermal_destroy_zones();
2421 genetlink_exit();
2422 class_unregister(&thermal_class);
2423 thermal_unregister_governors();
2424 idr_destroy(&thermal_tz_idr);
2425 idr_destroy(&thermal_cdev_idr);
2426 mutex_destroy(&thermal_idr_lock);
2427 mutex_destroy(&thermal_list_lock);
2428 mutex_destroy(&thermal_governor_lock);
2429}
2430
2431fs_initcall(thermal_init);
2432module_exit(thermal_exit);