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