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