Merge remote-tracking branch 'lsk/v3.10/topic/configs' into linux-linaro-lsk
[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
41 #include "thermal_core.h"
42 #include "thermal_hwmon.h"
43
44 MODULE_AUTHOR("Zhang Rui");
45 MODULE_DESCRIPTION("Generic thermal management sysfs support");
46 MODULE_LICENSE("GPL v2");
47
48 static DEFINE_IDR(thermal_tz_idr);
49 static DEFINE_IDR(thermal_cdev_idr);
50 static DEFINE_MUTEX(thermal_idr_lock);
51
52 static LIST_HEAD(thermal_tz_list);
53 static LIST_HEAD(thermal_cdev_list);
54 static LIST_HEAD(thermal_governor_list);
55
56 static DEFINE_MUTEX(thermal_list_lock);
57 static DEFINE_MUTEX(thermal_governor_lock);
58
59 static struct thermal_governor *__find_governor(const char *name)
60 {
61         struct thermal_governor *pos;
62
63         list_for_each_entry(pos, &thermal_governor_list, governor_list)
64                 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
65                         return pos;
66
67         return NULL;
68 }
69
70 int thermal_register_governor(struct thermal_governor *governor)
71 {
72         int err;
73         const char *name;
74         struct thermal_zone_device *pos;
75
76         if (!governor)
77                 return -EINVAL;
78
79         mutex_lock(&thermal_governor_lock);
80
81         err = -EBUSY;
82         if (__find_governor(governor->name) == NULL) {
83                 err = 0;
84                 list_add(&governor->governor_list, &thermal_governor_list);
85         }
86
87         mutex_lock(&thermal_list_lock);
88
89         list_for_each_entry(pos, &thermal_tz_list, node) {
90                 if (pos->governor)
91                         continue;
92                 if (pos->tzp)
93                         name = pos->tzp->governor_name;
94                 else
95                         name = DEFAULT_THERMAL_GOVERNOR;
96                 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
97                         pos->governor = governor;
98         }
99
100         mutex_unlock(&thermal_list_lock);
101         mutex_unlock(&thermal_governor_lock);
102
103         return err;
104 }
105
106 void thermal_unregister_governor(struct thermal_governor *governor)
107 {
108         struct thermal_zone_device *pos;
109
110         if (!governor)
111                 return;
112
113         mutex_lock(&thermal_governor_lock);
114
115         if (__find_governor(governor->name) == NULL)
116                 goto exit;
117
118         mutex_lock(&thermal_list_lock);
119
120         list_for_each_entry(pos, &thermal_tz_list, node) {
121                 if (!strnicmp(pos->governor->name, governor->name,
122                                                 THERMAL_NAME_LENGTH))
123                         pos->governor = NULL;
124         }
125
126         mutex_unlock(&thermal_list_lock);
127         list_del(&governor->governor_list);
128 exit:
129         mutex_unlock(&thermal_governor_lock);
130         return;
131 }
132
133 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
134 {
135         int ret;
136
137         if (lock)
138                 mutex_lock(lock);
139         ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
140         if (lock)
141                 mutex_unlock(lock);
142         if (unlikely(ret < 0))
143                 return ret;
144         *id = ret;
145         return 0;
146 }
147
148 static void release_idr(struct idr *idr, struct mutex *lock, int id)
149 {
150         if (lock)
151                 mutex_lock(lock);
152         idr_remove(idr, id);
153         if (lock)
154                 mutex_unlock(lock);
155 }
156
157 int get_tz_trend(struct thermal_zone_device *tz, int trip)
158 {
159         enum thermal_trend trend;
160
161         if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
162                 if (tz->temperature > tz->last_temperature)
163                         trend = THERMAL_TREND_RAISING;
164                 else if (tz->temperature < tz->last_temperature)
165                         trend = THERMAL_TREND_DROPPING;
166                 else
167                         trend = THERMAL_TREND_STABLE;
168         }
169
170         return trend;
171 }
172 EXPORT_SYMBOL(get_tz_trend);
173
174 struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
175                         struct thermal_cooling_device *cdev, int trip)
176 {
177         struct thermal_instance *pos = NULL;
178         struct thermal_instance *target_instance = NULL;
179
180         mutex_lock(&tz->lock);
181         mutex_lock(&cdev->lock);
182
183         list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
184                 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
185                         target_instance = pos;
186                         break;
187                 }
188         }
189
190         mutex_unlock(&cdev->lock);
191         mutex_unlock(&tz->lock);
192
193         return target_instance;
194 }
195 EXPORT_SYMBOL(get_thermal_instance);
196
197 static void print_bind_err_msg(struct thermal_zone_device *tz,
198                         struct thermal_cooling_device *cdev, int ret)
199 {
200         dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
201                                 tz->type, cdev->type, ret);
202 }
203
204 static void __bind(struct thermal_zone_device *tz, int mask,
205                         struct thermal_cooling_device *cdev)
206 {
207         int i, ret;
208
209         for (i = 0; i < tz->trips; i++) {
210                 if (mask & (1 << i)) {
211                         ret = thermal_zone_bind_cooling_device(tz, i, cdev,
212                                         THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
213                         if (ret)
214                                 print_bind_err_msg(tz, cdev, ret);
215                 }
216         }
217 }
218
219 static void __unbind(struct thermal_zone_device *tz, int mask,
220                         struct thermal_cooling_device *cdev)
221 {
222         int i;
223
224         for (i = 0; i < tz->trips; i++)
225                 if (mask & (1 << i))
226                         thermal_zone_unbind_cooling_device(tz, i, cdev);
227 }
228
229 static void bind_cdev(struct thermal_cooling_device *cdev)
230 {
231         int i, ret;
232         const struct thermal_zone_params *tzp;
233         struct thermal_zone_device *pos = NULL;
234
235         mutex_lock(&thermal_list_lock);
236
237         list_for_each_entry(pos, &thermal_tz_list, node) {
238                 if (!pos->tzp && !pos->ops->bind)
239                         continue;
240
241                 if (!pos->tzp && pos->ops->bind) {
242                         ret = pos->ops->bind(pos, cdev);
243                         if (ret)
244                                 print_bind_err_msg(pos, cdev, ret);
245                 }
246
247                 tzp = pos->tzp;
248                 if (!tzp || !tzp->tbp)
249                         continue;
250
251                 for (i = 0; i < tzp->num_tbps; i++) {
252                         if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
253                                 continue;
254                         if (tzp->tbp[i].match(pos, cdev))
255                                 continue;
256                         tzp->tbp[i].cdev = cdev;
257                         __bind(pos, tzp->tbp[i].trip_mask, cdev);
258                 }
259         }
260
261         mutex_unlock(&thermal_list_lock);
262 }
263
264 static void bind_tz(struct thermal_zone_device *tz)
265 {
266         int i, ret;
267         struct thermal_cooling_device *pos = NULL;
268         const struct thermal_zone_params *tzp = tz->tzp;
269
270         if (!tzp && !tz->ops->bind)
271                 return;
272
273         mutex_lock(&thermal_list_lock);
274
275         /* If there is no platform data, try to use ops->bind */
276         if (!tzp && tz->ops->bind) {
277                 list_for_each_entry(pos, &thermal_cdev_list, node) {
278                         ret = tz->ops->bind(tz, pos);
279                         if (ret)
280                                 print_bind_err_msg(tz, pos, ret);
281                 }
282                 goto exit;
283         }
284
285         if (!tzp || !tzp->tbp)
286                 goto exit;
287
288         list_for_each_entry(pos, &thermal_cdev_list, node) {
289                 for (i = 0; i < tzp->num_tbps; i++) {
290                         if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
291                                 continue;
292                         if (tzp->tbp[i].match(tz, pos))
293                                 continue;
294                         tzp->tbp[i].cdev = pos;
295                         __bind(tz, tzp->tbp[i].trip_mask, pos);
296                 }
297         }
298 exit:
299         mutex_unlock(&thermal_list_lock);
300 }
301
302 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
303                                             int delay)
304 {
305         if (delay > 1000)
306                 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
307                                  round_jiffies(msecs_to_jiffies(delay)));
308         else if (delay)
309                 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
310                                  msecs_to_jiffies(delay));
311         else
312                 cancel_delayed_work(&tz->poll_queue);
313 }
314
315 static void monitor_thermal_zone(struct thermal_zone_device *tz)
316 {
317         mutex_lock(&tz->lock);
318
319         if (tz->passive)
320                 thermal_zone_device_set_polling(tz, tz->passive_delay);
321         else if (tz->polling_delay)
322                 thermal_zone_device_set_polling(tz, tz->polling_delay);
323         else
324                 thermal_zone_device_set_polling(tz, 0);
325
326         mutex_unlock(&tz->lock);
327 }
328
329 static void handle_non_critical_trips(struct thermal_zone_device *tz,
330                         int trip, enum thermal_trip_type trip_type)
331 {
332         if (tz->governor)
333                 tz->governor->throttle(tz, trip);
334 }
335
336 static void handle_critical_trips(struct thermal_zone_device *tz,
337                                 int trip, enum thermal_trip_type trip_type)
338 {
339         long trip_temp;
340
341         tz->ops->get_trip_temp(tz, trip, &trip_temp);
342
343         /* If we have not crossed the trip_temp, we do not care. */
344         if (tz->temperature < trip_temp)
345                 return;
346
347         if (tz->ops->notify)
348                 tz->ops->notify(tz, trip, trip_type);
349
350         if (trip_type == THERMAL_TRIP_CRITICAL) {
351                 dev_emerg(&tz->device,
352                           "critical temperature reached(%d C),shutting down\n",
353                           tz->temperature / 1000);
354                 orderly_poweroff(true);
355         }
356 }
357
358 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
359 {
360         enum thermal_trip_type type;
361
362         tz->ops->get_trip_type(tz, trip, &type);
363
364         if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
365                 handle_critical_trips(tz, trip, type);
366         else
367                 handle_non_critical_trips(tz, trip, type);
368         /*
369          * Alright, we handled this trip successfully.
370          * So, start monitoring again.
371          */
372         monitor_thermal_zone(tz);
373 }
374
375 /**
376  * thermal_zone_get_temp() - returns its the temperature of thermal zone
377  * @tz: a valid pointer to a struct thermal_zone_device
378  * @temp: a valid pointer to where to store the resulting temperature.
379  *
380  * When a valid thermal zone reference is passed, it will fetch its
381  * temperature and fill @temp.
382  *
383  * Return: On success returns 0, an error code otherwise
384  */
385 int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
386 {
387         int ret = -EINVAL;
388 #ifdef CONFIG_THERMAL_EMULATION
389         int count;
390         unsigned long crit_temp = -1UL;
391         enum thermal_trip_type type;
392 #endif
393
394         if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
395                 goto exit;
396
397         mutex_lock(&tz->lock);
398
399         ret = tz->ops->get_temp(tz, temp);
400 #ifdef CONFIG_THERMAL_EMULATION
401         if (!tz->emul_temperature)
402                 goto skip_emul;
403
404         for (count = 0; count < tz->trips; count++) {
405                 ret = tz->ops->get_trip_type(tz, count, &type);
406                 if (!ret && type == THERMAL_TRIP_CRITICAL) {
407                         ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
408                         break;
409                 }
410         }
411
412         if (ret)
413                 goto skip_emul;
414
415         if (*temp < crit_temp)
416                 *temp = tz->emul_temperature;
417 skip_emul:
418 #endif
419         mutex_unlock(&tz->lock);
420 exit:
421         return ret;
422 }
423 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
424
425 static void update_temperature(struct thermal_zone_device *tz)
426 {
427         long temp;
428         int ret;
429
430         ret = thermal_zone_get_temp(tz, &temp);
431         if (ret) {
432                 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
433                          tz->id);
434                 return;
435         }
436
437         mutex_lock(&tz->lock);
438         tz->last_temperature = tz->temperature;
439         tz->temperature = temp;
440         mutex_unlock(&tz->lock);
441
442         dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
443                                 tz->last_temperature, tz->temperature);
444 }
445
446 void thermal_zone_device_update(struct thermal_zone_device *tz)
447 {
448         int count;
449
450         if (!tz->ops->get_temp)
451                 return;
452
453         update_temperature(tz);
454
455         for (count = 0; count < tz->trips; count++)
456                 handle_thermal_trip(tz, count);
457 }
458 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
459
460 static void thermal_zone_device_check(struct work_struct *work)
461 {
462         struct thermal_zone_device *tz = container_of(work, struct
463                                                       thermal_zone_device,
464                                                       poll_queue.work);
465         thermal_zone_device_update(tz);
466 }
467
468 /* sys I/F for thermal zone */
469
470 #define to_thermal_zone(_dev) \
471         container_of(_dev, struct thermal_zone_device, device)
472
473 static ssize_t
474 type_show(struct device *dev, struct device_attribute *attr, char *buf)
475 {
476         struct thermal_zone_device *tz = to_thermal_zone(dev);
477
478         return sprintf(buf, "%s\n", tz->type);
479 }
480
481 static ssize_t
482 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
483 {
484         struct thermal_zone_device *tz = to_thermal_zone(dev);
485         long temperature;
486         int ret;
487
488         ret = thermal_zone_get_temp(tz, &temperature);
489
490         if (ret)
491                 return ret;
492
493         return sprintf(buf, "%ld\n", temperature);
494 }
495
496 static ssize_t
497 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
498 {
499         struct thermal_zone_device *tz = to_thermal_zone(dev);
500         enum thermal_device_mode mode;
501         int result;
502
503         if (!tz->ops->get_mode)
504                 return -EPERM;
505
506         result = tz->ops->get_mode(tz, &mode);
507         if (result)
508                 return result;
509
510         return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
511                        : "disabled");
512 }
513
514 static ssize_t
515 mode_store(struct device *dev, struct device_attribute *attr,
516            const char *buf, size_t count)
517 {
518         struct thermal_zone_device *tz = to_thermal_zone(dev);
519         int result;
520
521         if (!tz->ops->set_mode)
522                 return -EPERM;
523
524         if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
525                 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
526         else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
527                 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
528         else
529                 result = -EINVAL;
530
531         if (result)
532                 return result;
533
534         return count;
535 }
536
537 static ssize_t
538 trip_point_type_show(struct device *dev, struct device_attribute *attr,
539                      char *buf)
540 {
541         struct thermal_zone_device *tz = to_thermal_zone(dev);
542         enum thermal_trip_type type;
543         int trip, result;
544
545         if (!tz->ops->get_trip_type)
546                 return -EPERM;
547
548         if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
549                 return -EINVAL;
550
551         result = tz->ops->get_trip_type(tz, trip, &type);
552         if (result)
553                 return result;
554
555         switch (type) {
556         case THERMAL_TRIP_CRITICAL:
557                 return sprintf(buf, "critical\n");
558         case THERMAL_TRIP_HOT:
559                 return sprintf(buf, "hot\n");
560         case THERMAL_TRIP_PASSIVE:
561                 return sprintf(buf, "passive\n");
562         case THERMAL_TRIP_ACTIVE:
563                 return sprintf(buf, "active\n");
564         default:
565                 return sprintf(buf, "unknown\n");
566         }
567 }
568
569 static ssize_t
570 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
571                      const char *buf, size_t count)
572 {
573         struct thermal_zone_device *tz = to_thermal_zone(dev);
574         int trip, ret;
575         unsigned long temperature;
576
577         if (!tz->ops->set_trip_temp)
578                 return -EPERM;
579
580         if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
581                 return -EINVAL;
582
583         if (kstrtoul(buf, 10, &temperature))
584                 return -EINVAL;
585
586         ret = tz->ops->set_trip_temp(tz, trip, temperature);
587
588         return ret ? ret : count;
589 }
590
591 static ssize_t
592 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
593                      char *buf)
594 {
595         struct thermal_zone_device *tz = to_thermal_zone(dev);
596         int trip, ret;
597         long temperature;
598
599         if (!tz->ops->get_trip_temp)
600                 return -EPERM;
601
602         if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
603                 return -EINVAL;
604
605         ret = tz->ops->get_trip_temp(tz, trip, &temperature);
606
607         if (ret)
608                 return ret;
609
610         return sprintf(buf, "%ld\n", temperature);
611 }
612
613 static ssize_t
614 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
615                         const char *buf, size_t count)
616 {
617         struct thermal_zone_device *tz = to_thermal_zone(dev);
618         int trip, ret;
619         unsigned long temperature;
620
621         if (!tz->ops->set_trip_hyst)
622                 return -EPERM;
623
624         if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
625                 return -EINVAL;
626
627         if (kstrtoul(buf, 10, &temperature))
628                 return -EINVAL;
629
630         /*
631          * We are not doing any check on the 'temperature' value
632          * here. The driver implementing 'set_trip_hyst' has to
633          * take care of this.
634          */
635         ret = tz->ops->set_trip_hyst(tz, trip, temperature);
636
637         return ret ? ret : count;
638 }
639
640 static ssize_t
641 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
642                         char *buf)
643 {
644         struct thermal_zone_device *tz = to_thermal_zone(dev);
645         int trip, ret;
646         unsigned long temperature;
647
648         if (!tz->ops->get_trip_hyst)
649                 return -EPERM;
650
651         if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
652                 return -EINVAL;
653
654         ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
655
656         return ret ? ret : sprintf(buf, "%ld\n", temperature);
657 }
658
659 static ssize_t
660 passive_store(struct device *dev, struct device_attribute *attr,
661                     const char *buf, size_t count)
662 {
663         struct thermal_zone_device *tz = to_thermal_zone(dev);
664         struct thermal_cooling_device *cdev = NULL;
665         int state;
666
667         if (!sscanf(buf, "%d\n", &state))
668                 return -EINVAL;
669
670         /* sanity check: values below 1000 millicelcius don't make sense
671          * and can cause the system to go into a thermal heart attack
672          */
673         if (state && state < 1000)
674                 return -EINVAL;
675
676         if (state && !tz->forced_passive) {
677                 mutex_lock(&thermal_list_lock);
678                 list_for_each_entry(cdev, &thermal_cdev_list, node) {
679                         if (!strncmp("Processor", cdev->type,
680                                      sizeof("Processor")))
681                                 thermal_zone_bind_cooling_device(tz,
682                                                 THERMAL_TRIPS_NONE, cdev,
683                                                 THERMAL_NO_LIMIT,
684                                                 THERMAL_NO_LIMIT);
685                 }
686                 mutex_unlock(&thermal_list_lock);
687                 if (!tz->passive_delay)
688                         tz->passive_delay = 1000;
689         } else if (!state && tz->forced_passive) {
690                 mutex_lock(&thermal_list_lock);
691                 list_for_each_entry(cdev, &thermal_cdev_list, node) {
692                         if (!strncmp("Processor", cdev->type,
693                                      sizeof("Processor")))
694                                 thermal_zone_unbind_cooling_device(tz,
695                                                                    THERMAL_TRIPS_NONE,
696                                                                    cdev);
697                 }
698                 mutex_unlock(&thermal_list_lock);
699                 tz->passive_delay = 0;
700         }
701
702         tz->forced_passive = state;
703
704         thermal_zone_device_update(tz);
705
706         return count;
707 }
708
709 static ssize_t
710 passive_show(struct device *dev, struct device_attribute *attr,
711                    char *buf)
712 {
713         struct thermal_zone_device *tz = to_thermal_zone(dev);
714
715         return sprintf(buf, "%d\n", tz->forced_passive);
716 }
717
718 static ssize_t
719 policy_store(struct device *dev, struct device_attribute *attr,
720                     const char *buf, size_t count)
721 {
722         int ret = -EINVAL;
723         struct thermal_zone_device *tz = to_thermal_zone(dev);
724         struct thermal_governor *gov;
725
726         mutex_lock(&thermal_governor_lock);
727
728         gov = __find_governor(buf);
729         if (!gov)
730                 goto exit;
731
732         tz->governor = gov;
733         ret = count;
734
735 exit:
736         mutex_unlock(&thermal_governor_lock);
737         return ret;
738 }
739
740 static ssize_t
741 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
742 {
743         struct thermal_zone_device *tz = to_thermal_zone(dev);
744
745         return sprintf(buf, "%s\n", tz->governor->name);
746 }
747
748 #ifdef CONFIG_THERMAL_EMULATION
749 static ssize_t
750 emul_temp_store(struct device *dev, struct device_attribute *attr,
751                      const char *buf, size_t count)
752 {
753         struct thermal_zone_device *tz = to_thermal_zone(dev);
754         int ret = 0;
755         unsigned long temperature;
756
757         if (kstrtoul(buf, 10, &temperature))
758                 return -EINVAL;
759
760         if (!tz->ops->set_emul_temp) {
761                 mutex_lock(&tz->lock);
762                 tz->emul_temperature = temperature;
763                 mutex_unlock(&tz->lock);
764         } else {
765                 ret = tz->ops->set_emul_temp(tz, temperature);
766         }
767
768         return ret ? ret : count;
769 }
770 static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
771 #endif/*CONFIG_THERMAL_EMULATION*/
772
773 static DEVICE_ATTR(type, 0444, type_show, NULL);
774 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
775 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
776 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
777 static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
778
779 /* sys I/F for cooling device */
780 #define to_cooling_device(_dev) \
781         container_of(_dev, struct thermal_cooling_device, device)
782
783 static ssize_t
784 thermal_cooling_device_type_show(struct device *dev,
785                                  struct device_attribute *attr, char *buf)
786 {
787         struct thermal_cooling_device *cdev = to_cooling_device(dev);
788
789         return sprintf(buf, "%s\n", cdev->type);
790 }
791
792 static ssize_t
793 thermal_cooling_device_max_state_show(struct device *dev,
794                                       struct device_attribute *attr, char *buf)
795 {
796         struct thermal_cooling_device *cdev = to_cooling_device(dev);
797         unsigned long state;
798         int ret;
799
800         ret = cdev->ops->get_max_state(cdev, &state);
801         if (ret)
802                 return ret;
803         return sprintf(buf, "%ld\n", state);
804 }
805
806 static ssize_t
807 thermal_cooling_device_cur_state_show(struct device *dev,
808                                       struct device_attribute *attr, char *buf)
809 {
810         struct thermal_cooling_device *cdev = to_cooling_device(dev);
811         unsigned long state;
812         int ret;
813
814         ret = cdev->ops->get_cur_state(cdev, &state);
815         if (ret)
816                 return ret;
817         return sprintf(buf, "%ld\n", state);
818 }
819
820 static ssize_t
821 thermal_cooling_device_cur_state_store(struct device *dev,
822                                        struct device_attribute *attr,
823                                        const char *buf, size_t count)
824 {
825         struct thermal_cooling_device *cdev = to_cooling_device(dev);
826         unsigned long state;
827         int result;
828
829         if (!sscanf(buf, "%ld\n", &state))
830                 return -EINVAL;
831
832         if ((long)state < 0)
833                 return -EINVAL;
834
835         result = cdev->ops->set_cur_state(cdev, state);
836         if (result)
837                 return result;
838         return count;
839 }
840
841 static struct device_attribute dev_attr_cdev_type =
842 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
843 static DEVICE_ATTR(max_state, 0444,
844                    thermal_cooling_device_max_state_show, NULL);
845 static DEVICE_ATTR(cur_state, 0644,
846                    thermal_cooling_device_cur_state_show,
847                    thermal_cooling_device_cur_state_store);
848
849 static ssize_t
850 thermal_cooling_device_trip_point_show(struct device *dev,
851                                        struct device_attribute *attr, char *buf)
852 {
853         struct thermal_instance *instance;
854
855         instance =
856             container_of(attr, struct thermal_instance, attr);
857
858         if (instance->trip == THERMAL_TRIPS_NONE)
859                 return sprintf(buf, "-1\n");
860         else
861                 return sprintf(buf, "%d\n", instance->trip);
862 }
863
864 /* Device management */
865
866 /**
867  * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
868  * @tz:         pointer to struct thermal_zone_device
869  * @trip:       indicates which trip point the cooling devices is
870  *              associated with in this thermal zone.
871  * @cdev:       pointer to struct thermal_cooling_device
872  * @upper:      the Maximum cooling state for this trip point.
873  *              THERMAL_NO_LIMIT means no upper limit,
874  *              and the cooling device can be in max_state.
875  * @lower:      the Minimum cooling state can be used for this trip point.
876  *              THERMAL_NO_LIMIT means no lower limit,
877  *              and the cooling device can be in cooling state 0.
878  *
879  * This interface function bind a thermal cooling device to the certain trip
880  * point of a thermal zone device.
881  * This function is usually called in the thermal zone device .bind callback.
882  *
883  * Return: 0 on success, the proper error value otherwise.
884  */
885 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
886                                      int trip,
887                                      struct thermal_cooling_device *cdev,
888                                      unsigned long upper, unsigned long lower)
889 {
890         struct thermal_instance *dev;
891         struct thermal_instance *pos;
892         struct thermal_zone_device *pos1;
893         struct thermal_cooling_device *pos2;
894         unsigned long max_state;
895         int result;
896
897         if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
898                 return -EINVAL;
899
900         list_for_each_entry(pos1, &thermal_tz_list, node) {
901                 if (pos1 == tz)
902                         break;
903         }
904         list_for_each_entry(pos2, &thermal_cdev_list, node) {
905                 if (pos2 == cdev)
906                         break;
907         }
908
909         if (tz != pos1 || cdev != pos2)
910                 return -EINVAL;
911
912         cdev->ops->get_max_state(cdev, &max_state);
913
914         /* lower default 0, upper default max_state */
915         lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
916         upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
917
918         if (lower > upper || upper > max_state)
919                 return -EINVAL;
920
921         dev =
922             kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
923         if (!dev)
924                 return -ENOMEM;
925         dev->tz = tz;
926         dev->cdev = cdev;
927         dev->trip = trip;
928         dev->upper = upper;
929         dev->lower = lower;
930         dev->target = THERMAL_NO_TARGET;
931
932         result = get_idr(&tz->idr, &tz->lock, &dev->id);
933         if (result)
934                 goto free_mem;
935
936         sprintf(dev->name, "cdev%d", dev->id);
937         result =
938             sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
939         if (result)
940                 goto release_idr;
941
942         sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
943         sysfs_attr_init(&dev->attr.attr);
944         dev->attr.attr.name = dev->attr_name;
945         dev->attr.attr.mode = 0444;
946         dev->attr.show = thermal_cooling_device_trip_point_show;
947         result = device_create_file(&tz->device, &dev->attr);
948         if (result)
949                 goto remove_symbol_link;
950
951         mutex_lock(&tz->lock);
952         mutex_lock(&cdev->lock);
953         list_for_each_entry(pos, &tz->thermal_instances, tz_node)
954             if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
955                 result = -EEXIST;
956                 break;
957         }
958         if (!result) {
959                 list_add_tail(&dev->tz_node, &tz->thermal_instances);
960                 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
961         }
962         mutex_unlock(&cdev->lock);
963         mutex_unlock(&tz->lock);
964
965         if (!result)
966                 return 0;
967
968         device_remove_file(&tz->device, &dev->attr);
969 remove_symbol_link:
970         sysfs_remove_link(&tz->device.kobj, dev->name);
971 release_idr:
972         release_idr(&tz->idr, &tz->lock, dev->id);
973 free_mem:
974         kfree(dev);
975         return result;
976 }
977 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
978
979 /**
980  * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
981  *                                        thermal zone.
982  * @tz:         pointer to a struct thermal_zone_device.
983  * @trip:       indicates which trip point the cooling devices is
984  *              associated with in this thermal zone.
985  * @cdev:       pointer to a struct thermal_cooling_device.
986  *
987  * This interface function unbind a thermal cooling device from the certain
988  * trip point of a thermal zone device.
989  * This function is usually called in the thermal zone device .unbind callback.
990  *
991  * Return: 0 on success, the proper error value otherwise.
992  */
993 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
994                                        int trip,
995                                        struct thermal_cooling_device *cdev)
996 {
997         struct thermal_instance *pos, *next;
998
999         mutex_lock(&tz->lock);
1000         mutex_lock(&cdev->lock);
1001         list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
1002                 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1003                         list_del(&pos->tz_node);
1004                         list_del(&pos->cdev_node);
1005                         mutex_unlock(&cdev->lock);
1006                         mutex_unlock(&tz->lock);
1007                         goto unbind;
1008                 }
1009         }
1010         mutex_unlock(&cdev->lock);
1011         mutex_unlock(&tz->lock);
1012
1013         return -ENODEV;
1014
1015 unbind:
1016         device_remove_file(&tz->device, &pos->attr);
1017         sysfs_remove_link(&tz->device.kobj, pos->name);
1018         release_idr(&tz->idr, &tz->lock, pos->id);
1019         kfree(pos);
1020         return 0;
1021 }
1022 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
1023
1024 static void thermal_release(struct device *dev)
1025 {
1026         struct thermal_zone_device *tz;
1027         struct thermal_cooling_device *cdev;
1028
1029         if (!strncmp(dev_name(dev), "thermal_zone",
1030                      sizeof("thermal_zone") - 1)) {
1031                 tz = to_thermal_zone(dev);
1032                 kfree(tz);
1033         } else {
1034                 cdev = to_cooling_device(dev);
1035                 kfree(cdev);
1036         }
1037 }
1038
1039 static struct class thermal_class = {
1040         .name = "thermal",
1041         .dev_release = thermal_release,
1042 };
1043
1044 /**
1045  * __thermal_cooling_device_register() - register a new thermal cooling device
1046  * @np:         a pointer to a device tree node.
1047  * @type:       the thermal cooling device type.
1048  * @devdata:    device private data.
1049  * @ops:                standard thermal cooling devices callbacks.
1050  *
1051  * This interface function adds a new thermal cooling device (fan/processor/...)
1052  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1053  * to all the thermal zone devices registered at the same time.
1054  * It also gives the opportunity to link the cooling device to a device tree
1055  * node, so that it can be bound to a thermal zone created out of device tree.
1056  *
1057  * Return: a pointer to the created struct thermal_cooling_device or an
1058  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1059  */
1060 static struct thermal_cooling_device *
1061 __thermal_cooling_device_register(struct device_node *np,
1062                                   char *type, void *devdata,
1063                                   const struct thermal_cooling_device_ops *ops)
1064 {
1065         struct thermal_cooling_device *cdev;
1066         int result;
1067
1068         if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1069                 return ERR_PTR(-EINVAL);
1070
1071         if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1072             !ops->set_cur_state)
1073                 return ERR_PTR(-EINVAL);
1074
1075         cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1076         if (!cdev)
1077                 return ERR_PTR(-ENOMEM);
1078
1079         result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1080         if (result) {
1081                 kfree(cdev);
1082                 return ERR_PTR(result);
1083         }
1084
1085         strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
1086         mutex_init(&cdev->lock);
1087         INIT_LIST_HEAD(&cdev->thermal_instances);
1088         cdev->np = np;
1089         cdev->ops = ops;
1090         cdev->updated = true;
1091         cdev->device.class = &thermal_class;
1092         cdev->devdata = devdata;
1093         dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1094         result = device_register(&cdev->device);
1095         if (result) {
1096                 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1097                 kfree(cdev);
1098                 return ERR_PTR(result);
1099         }
1100
1101         /* sys I/F */
1102         if (type) {
1103                 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
1104                 if (result)
1105                         goto unregister;
1106         }
1107
1108         result = device_create_file(&cdev->device, &dev_attr_max_state);
1109         if (result)
1110                 goto unregister;
1111
1112         result = device_create_file(&cdev->device, &dev_attr_cur_state);
1113         if (result)
1114                 goto unregister;
1115
1116         /* Add 'this' new cdev to the global cdev list */
1117         mutex_lock(&thermal_list_lock);
1118         list_add(&cdev->node, &thermal_cdev_list);
1119         mutex_unlock(&thermal_list_lock);
1120
1121         /* Update binding information for 'this' new cdev */
1122         bind_cdev(cdev);
1123
1124         return cdev;
1125
1126 unregister:
1127         release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1128         device_unregister(&cdev->device);
1129         return ERR_PTR(result);
1130 }
1131
1132 /**
1133  * thermal_cooling_device_register() - register a new thermal cooling device
1134  * @type:       the thermal cooling device type.
1135  * @devdata:    device private data.
1136  * @ops:                standard thermal cooling devices callbacks.
1137  *
1138  * This interface function adds a new thermal cooling device (fan/processor/...)
1139  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1140  * to all the thermal zone devices registered at the same time.
1141  *
1142  * Return: a pointer to the created struct thermal_cooling_device or an
1143  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1144  */
1145 struct thermal_cooling_device *
1146 thermal_cooling_device_register(char *type, void *devdata,
1147                                 const struct thermal_cooling_device_ops *ops)
1148 {
1149         return __thermal_cooling_device_register(NULL, type, devdata, ops);
1150 }
1151 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1152
1153 /**
1154  * thermal_of_cooling_device_register() - register an OF thermal cooling device
1155  * @np:         a pointer to a device tree node.
1156  * @type:       the thermal cooling device type.
1157  * @devdata:    device private data.
1158  * @ops:                standard thermal cooling devices callbacks.
1159  *
1160  * This function will register a cooling device with device tree node reference.
1161  * This interface function adds a new thermal cooling device (fan/processor/...)
1162  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1163  * to all the thermal zone devices registered at the same time.
1164  *
1165  * Return: a pointer to the created struct thermal_cooling_device or an
1166  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1167  */
1168 struct thermal_cooling_device *
1169 thermal_of_cooling_device_register(struct device_node *np,
1170                                    char *type, void *devdata,
1171                                    const struct thermal_cooling_device_ops *ops)
1172 {
1173         return __thermal_cooling_device_register(np, type, devdata, ops);
1174 }
1175 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1176
1177 /**
1178  * thermal_cooling_device_unregister - removes the registered thermal cooling device
1179  * @cdev:       the thermal cooling device to remove.
1180  *
1181  * thermal_cooling_device_unregister() must be called when the device is no
1182  * longer needed.
1183  */
1184 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1185 {
1186         int i;
1187         const struct thermal_zone_params *tzp;
1188         struct thermal_zone_device *tz;
1189         struct thermal_cooling_device *pos = NULL;
1190
1191         if (!cdev)
1192                 return;
1193
1194         mutex_lock(&thermal_list_lock);
1195         list_for_each_entry(pos, &thermal_cdev_list, node)
1196             if (pos == cdev)
1197                 break;
1198         if (pos != cdev) {
1199                 /* thermal cooling device not found */
1200                 mutex_unlock(&thermal_list_lock);
1201                 return;
1202         }
1203         list_del(&cdev->node);
1204
1205         /* Unbind all thermal zones associated with 'this' cdev */
1206         list_for_each_entry(tz, &thermal_tz_list, node) {
1207                 if (tz->ops->unbind) {
1208                         tz->ops->unbind(tz, cdev);
1209                         continue;
1210                 }
1211
1212                 if (!tz->tzp || !tz->tzp->tbp)
1213                         continue;
1214
1215                 tzp = tz->tzp;
1216                 for (i = 0; i < tzp->num_tbps; i++) {
1217                         if (tzp->tbp[i].cdev == cdev) {
1218                                 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1219                                 tzp->tbp[i].cdev = NULL;
1220                         }
1221                 }
1222         }
1223
1224         mutex_unlock(&thermal_list_lock);
1225
1226         if (cdev->type[0])
1227                 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1228         device_remove_file(&cdev->device, &dev_attr_max_state);
1229         device_remove_file(&cdev->device, &dev_attr_cur_state);
1230
1231         release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1232         device_unregister(&cdev->device);
1233         return;
1234 }
1235 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1236
1237 void thermal_cdev_update(struct thermal_cooling_device *cdev)
1238 {
1239         struct thermal_instance *instance;
1240         unsigned long target = 0;
1241
1242         /* cooling device is updated*/
1243         if (cdev->updated)
1244                 return;
1245
1246         mutex_lock(&cdev->lock);
1247         /* Make sure cdev enters the deepest cooling state */
1248         list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1249                 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1250                                 instance->tz->id, instance->target);
1251                 if (instance->target == THERMAL_NO_TARGET)
1252                         continue;
1253                 if (instance->target > target)
1254                         target = instance->target;
1255         }
1256         mutex_unlock(&cdev->lock);
1257         cdev->ops->set_cur_state(cdev, target);
1258         cdev->updated = true;
1259         dev_dbg(&cdev->device, "set to state %lu\n", target);
1260 }
1261 EXPORT_SYMBOL(thermal_cdev_update);
1262
1263 /**
1264  * thermal_notify_framework - Sensor drivers use this API to notify framework
1265  * @tz:         thermal zone device
1266  * @trip:       indicates which trip point has been crossed
1267  *
1268  * This function handles the trip events from sensor drivers. It starts
1269  * throttling the cooling devices according to the policy configured.
1270  * For CRITICAL and HOT trip points, this notifies the respective drivers,
1271  * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1272  * The throttling policy is based on the configured platform data; if no
1273  * platform data is provided, this uses the step_wise throttling policy.
1274  */
1275 void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
1276 {
1277         handle_thermal_trip(tz, trip);
1278 }
1279 EXPORT_SYMBOL_GPL(thermal_notify_framework);
1280
1281 /**
1282  * create_trip_attrs() - create attributes for trip points
1283  * @tz:         the thermal zone device
1284  * @mask:       Writeable trip point bitmap.
1285  *
1286  * helper function to instantiate sysfs entries for every trip
1287  * point and its properties of a struct thermal_zone_device.
1288  *
1289  * Return: 0 on success, the proper error value otherwise.
1290  */
1291 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1292 {
1293         int indx;
1294         int size = sizeof(struct thermal_attr) * tz->trips;
1295
1296         tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1297         if (!tz->trip_type_attrs)
1298                 return -ENOMEM;
1299
1300         tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1301         if (!tz->trip_temp_attrs) {
1302                 kfree(tz->trip_type_attrs);
1303                 return -ENOMEM;
1304         }
1305
1306         if (tz->ops->get_trip_hyst) {
1307                 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1308                 if (!tz->trip_hyst_attrs) {
1309                         kfree(tz->trip_type_attrs);
1310                         kfree(tz->trip_temp_attrs);
1311                         return -ENOMEM;
1312                 }
1313         }
1314
1315
1316         for (indx = 0; indx < tz->trips; indx++) {
1317                 /* create trip type attribute */
1318                 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1319                          "trip_point_%d_type", indx);
1320
1321                 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1322                 tz->trip_type_attrs[indx].attr.attr.name =
1323                                                 tz->trip_type_attrs[indx].name;
1324                 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1325                 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1326
1327                 device_create_file(&tz->device,
1328                                    &tz->trip_type_attrs[indx].attr);
1329
1330                 /* create trip temp attribute */
1331                 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1332                          "trip_point_%d_temp", indx);
1333
1334                 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1335                 tz->trip_temp_attrs[indx].attr.attr.name =
1336                                                 tz->trip_temp_attrs[indx].name;
1337                 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1338                 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1339                 if (mask & (1 << indx)) {
1340                         tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1341                         tz->trip_temp_attrs[indx].attr.store =
1342                                                         trip_point_temp_store;
1343                 }
1344
1345                 device_create_file(&tz->device,
1346                                    &tz->trip_temp_attrs[indx].attr);
1347
1348                 /* create Optional trip hyst attribute */
1349                 if (!tz->ops->get_trip_hyst)
1350                         continue;
1351                 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1352                          "trip_point_%d_hyst", indx);
1353
1354                 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1355                 tz->trip_hyst_attrs[indx].attr.attr.name =
1356                                         tz->trip_hyst_attrs[indx].name;
1357                 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1358                 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1359                 if (tz->ops->set_trip_hyst) {
1360                         tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1361                         tz->trip_hyst_attrs[indx].attr.store =
1362                                         trip_point_hyst_store;
1363                 }
1364
1365                 device_create_file(&tz->device,
1366                                    &tz->trip_hyst_attrs[indx].attr);
1367         }
1368         return 0;
1369 }
1370
1371 static void remove_trip_attrs(struct thermal_zone_device *tz)
1372 {
1373         int indx;
1374
1375         for (indx = 0; indx < tz->trips; indx++) {
1376                 device_remove_file(&tz->device,
1377                                    &tz->trip_type_attrs[indx].attr);
1378                 device_remove_file(&tz->device,
1379                                    &tz->trip_temp_attrs[indx].attr);
1380                 if (tz->ops->get_trip_hyst)
1381                         device_remove_file(&tz->device,
1382                                   &tz->trip_hyst_attrs[indx].attr);
1383         }
1384         kfree(tz->trip_type_attrs);
1385         kfree(tz->trip_temp_attrs);
1386         kfree(tz->trip_hyst_attrs);
1387 }
1388
1389 /**
1390  * thermal_zone_device_register() - register a new thermal zone device
1391  * @type:       the thermal zone device type
1392  * @trips:      the number of trip points the thermal zone support
1393  * @mask:       a bit string indicating the writeablility of trip points
1394  * @devdata:    private device data
1395  * @ops:        standard thermal zone device callbacks
1396  * @tzp:        thermal zone platform parameters
1397  * @passive_delay: number of milliseconds to wait between polls when
1398  *                 performing passive cooling
1399  * @polling_delay: number of milliseconds to wait between polls when checking
1400  *                 whether trip points have been crossed (0 for interrupt
1401  *                 driven systems)
1402  *
1403  * This interface function adds a new thermal zone device (sensor) to
1404  * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1405  * thermal cooling devices registered at the same time.
1406  * thermal_zone_device_unregister() must be called when the device is no
1407  * longer needed. The passive cooling depends on the .get_trend() return value.
1408  *
1409  * Return: a pointer to the created struct thermal_zone_device or an
1410  * in case of error, an ERR_PTR. Caller must check return value with
1411  * IS_ERR*() helpers.
1412  */
1413 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1414         int trips, int mask, void *devdata,
1415         struct thermal_zone_device_ops *ops,
1416         const struct thermal_zone_params *tzp,
1417         int passive_delay, int polling_delay)
1418 {
1419         struct thermal_zone_device *tz;
1420         enum thermal_trip_type trip_type;
1421         int result;
1422         int count;
1423         int passive = 0;
1424
1425         if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1426                 return ERR_PTR(-EINVAL);
1427
1428         if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1429                 return ERR_PTR(-EINVAL);
1430
1431         if (!ops)
1432                 return ERR_PTR(-EINVAL);
1433
1434         if (trips > 0 && !ops->get_trip_type)
1435                 return ERR_PTR(-EINVAL);
1436
1437         tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1438         if (!tz)
1439                 return ERR_PTR(-ENOMEM);
1440
1441         INIT_LIST_HEAD(&tz->thermal_instances);
1442         idr_init(&tz->idr);
1443         mutex_init(&tz->lock);
1444         result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1445         if (result) {
1446                 kfree(tz);
1447                 return ERR_PTR(result);
1448         }
1449
1450         strlcpy(tz->type, type ? : "", sizeof(tz->type));
1451         tz->ops = ops;
1452         tz->tzp = tzp;
1453         tz->device.class = &thermal_class;
1454         tz->devdata = devdata;
1455         tz->trips = trips;
1456         tz->passive_delay = passive_delay;
1457         tz->polling_delay = polling_delay;
1458
1459         dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1460         result = device_register(&tz->device);
1461         if (result) {
1462                 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1463                 kfree(tz);
1464                 return ERR_PTR(result);
1465         }
1466
1467         /* sys I/F */
1468         if (type) {
1469                 result = device_create_file(&tz->device, &dev_attr_type);
1470                 if (result)
1471                         goto unregister;
1472         }
1473
1474         result = device_create_file(&tz->device, &dev_attr_temp);
1475         if (result)
1476                 goto unregister;
1477
1478         if (ops->get_mode) {
1479                 result = device_create_file(&tz->device, &dev_attr_mode);
1480                 if (result)
1481                         goto unregister;
1482         }
1483
1484         result = create_trip_attrs(tz, mask);
1485         if (result)
1486                 goto unregister;
1487
1488         for (count = 0; count < trips; count++) {
1489                 tz->ops->get_trip_type(tz, count, &trip_type);
1490                 if (trip_type == THERMAL_TRIP_PASSIVE)
1491                         passive = 1;
1492         }
1493
1494         if (!passive) {
1495                 result = device_create_file(&tz->device, &dev_attr_passive);
1496                 if (result)
1497                         goto unregister;
1498         }
1499
1500 #ifdef CONFIG_THERMAL_EMULATION
1501         result = device_create_file(&tz->device, &dev_attr_emul_temp);
1502         if (result)
1503                 goto unregister;
1504 #endif
1505         /* Create policy attribute */
1506         result = device_create_file(&tz->device, &dev_attr_policy);
1507         if (result)
1508                 goto unregister;
1509
1510         /* Update 'this' zone's governor information */
1511         mutex_lock(&thermal_governor_lock);
1512
1513         if (tz->tzp)
1514                 tz->governor = __find_governor(tz->tzp->governor_name);
1515         else
1516                 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1517
1518         mutex_unlock(&thermal_governor_lock);
1519
1520         if (!tz->tzp || !tz->tzp->no_hwmon) {
1521                 result = thermal_add_hwmon_sysfs(tz);
1522                 if (result)
1523                         goto unregister;
1524         }
1525
1526         mutex_lock(&thermal_list_lock);
1527         list_add_tail(&tz->node, &thermal_tz_list);
1528         mutex_unlock(&thermal_list_lock);
1529
1530         /* Bind cooling devices for this zone */
1531         bind_tz(tz);
1532
1533         INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1534
1535         if (!tz->ops->get_temp)
1536                 thermal_zone_device_set_polling(tz, 0);
1537
1538         thermal_zone_device_update(tz);
1539
1540         if (!result)
1541                 return tz;
1542
1543 unregister:
1544         release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1545         device_unregister(&tz->device);
1546         return ERR_PTR(result);
1547 }
1548 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1549
1550 /**
1551  * thermal_device_unregister - removes the registered thermal zone device
1552  * @tz: the thermal zone device to remove
1553  */
1554 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1555 {
1556         int i;
1557         const struct thermal_zone_params *tzp;
1558         struct thermal_cooling_device *cdev;
1559         struct thermal_zone_device *pos = NULL;
1560
1561         if (!tz)
1562                 return;
1563
1564         tzp = tz->tzp;
1565
1566         mutex_lock(&thermal_list_lock);
1567         list_for_each_entry(pos, &thermal_tz_list, node)
1568             if (pos == tz)
1569                 break;
1570         if (pos != tz) {
1571                 /* thermal zone device not found */
1572                 mutex_unlock(&thermal_list_lock);
1573                 return;
1574         }
1575         list_del(&tz->node);
1576
1577         /* Unbind all cdevs associated with 'this' thermal zone */
1578         list_for_each_entry(cdev, &thermal_cdev_list, node) {
1579                 if (tz->ops->unbind) {
1580                         tz->ops->unbind(tz, cdev);
1581                         continue;
1582                 }
1583
1584                 if (!tzp || !tzp->tbp)
1585                         break;
1586
1587                 for (i = 0; i < tzp->num_tbps; i++) {
1588                         if (tzp->tbp[i].cdev == cdev) {
1589                                 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1590                                 tzp->tbp[i].cdev = NULL;
1591                         }
1592                 }
1593         }
1594
1595         mutex_unlock(&thermal_list_lock);
1596
1597         thermal_zone_device_set_polling(tz, 0);
1598
1599         if (tz->type[0])
1600                 device_remove_file(&tz->device, &dev_attr_type);
1601         device_remove_file(&tz->device, &dev_attr_temp);
1602         if (tz->ops->get_mode)
1603                 device_remove_file(&tz->device, &dev_attr_mode);
1604         device_remove_file(&tz->device, &dev_attr_policy);
1605         remove_trip_attrs(tz);
1606         tz->governor = NULL;
1607
1608         thermal_remove_hwmon_sysfs(tz);
1609         release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1610         idr_destroy(&tz->idr);
1611         mutex_destroy(&tz->lock);
1612         device_unregister(&tz->device);
1613         return;
1614 }
1615 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1616
1617 /**
1618  * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1619  * @name: thermal zone name to fetch the temperature
1620  *
1621  * When only one zone is found with the passed name, returns a reference to it.
1622  *
1623  * Return: On success returns a reference to an unique thermal zone with
1624  * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1625  * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1626  */
1627 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1628 {
1629         struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1630         unsigned int found = 0;
1631
1632         if (!name)
1633                 goto exit;
1634
1635         mutex_lock(&thermal_list_lock);
1636         list_for_each_entry(pos, &thermal_tz_list, node)
1637                 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1638                         found++;
1639                         ref = pos;
1640                 }
1641         mutex_unlock(&thermal_list_lock);
1642
1643         /* nothing has been found, thus an error code for it */
1644         if (found == 0)
1645                 ref = ERR_PTR(-ENODEV);
1646         else if (found > 1)
1647         /* Success only when an unique zone is found */
1648                 ref = ERR_PTR(-EEXIST);
1649
1650 exit:
1651         return ref;
1652 }
1653 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1654
1655 #ifdef CONFIG_NET
1656 static struct genl_family thermal_event_genl_family = {
1657         .id = GENL_ID_GENERATE,
1658         .name = THERMAL_GENL_FAMILY_NAME,
1659         .version = THERMAL_GENL_VERSION,
1660         .maxattr = THERMAL_GENL_ATTR_MAX,
1661 };
1662
1663 static struct genl_multicast_group thermal_event_mcgrp = {
1664         .name = THERMAL_GENL_MCAST_GROUP_NAME,
1665 };
1666
1667 int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1668                                         enum events event)
1669 {
1670         struct sk_buff *skb;
1671         struct nlattr *attr;
1672         struct thermal_genl_event *thermal_event;
1673         void *msg_header;
1674         int size;
1675         int result;
1676         static unsigned int thermal_event_seqnum;
1677
1678         if (!tz)
1679                 return -EINVAL;
1680
1681         /* allocate memory */
1682         size = nla_total_size(sizeof(struct thermal_genl_event)) +
1683                nla_total_size(0);
1684
1685         skb = genlmsg_new(size, GFP_ATOMIC);
1686         if (!skb)
1687                 return -ENOMEM;
1688
1689         /* add the genetlink message header */
1690         msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1691                                  &thermal_event_genl_family, 0,
1692                                  THERMAL_GENL_CMD_EVENT);
1693         if (!msg_header) {
1694                 nlmsg_free(skb);
1695                 return -ENOMEM;
1696         }
1697
1698         /* fill the data */
1699         attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1700                            sizeof(struct thermal_genl_event));
1701
1702         if (!attr) {
1703                 nlmsg_free(skb);
1704                 return -EINVAL;
1705         }
1706
1707         thermal_event = nla_data(attr);
1708         if (!thermal_event) {
1709                 nlmsg_free(skb);
1710                 return -EINVAL;
1711         }
1712
1713         memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1714
1715         thermal_event->orig = tz->id;
1716         thermal_event->event = event;
1717
1718         /* send multicast genetlink message */
1719         result = genlmsg_end(skb, msg_header);
1720         if (result < 0) {
1721                 nlmsg_free(skb);
1722                 return result;
1723         }
1724
1725         result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1726         if (result)
1727                 dev_err(&tz->device, "Failed to send netlink event:%d", result);
1728
1729         return result;
1730 }
1731 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
1732
1733 static int genetlink_init(void)
1734 {
1735         int result;
1736
1737         result = genl_register_family(&thermal_event_genl_family);
1738         if (result)
1739                 return result;
1740
1741         result = genl_register_mc_group(&thermal_event_genl_family,
1742                                         &thermal_event_mcgrp);
1743         if (result)
1744                 genl_unregister_family(&thermal_event_genl_family);
1745         return result;
1746 }
1747
1748 static void genetlink_exit(void)
1749 {
1750         genl_unregister_family(&thermal_event_genl_family);
1751 }
1752 #else /* !CONFIG_NET */
1753 static inline int genetlink_init(void) { return 0; }
1754 static inline void genetlink_exit(void) {}
1755 #endif /* !CONFIG_NET */
1756
1757 static int __init thermal_register_governors(void)
1758 {
1759         int result;
1760
1761         result = thermal_gov_step_wise_register();
1762         if (result)
1763                 return result;
1764
1765         result = thermal_gov_fair_share_register();
1766         if (result)
1767                 return result;
1768
1769         return thermal_gov_user_space_register();
1770 }
1771
1772 static void thermal_unregister_governors(void)
1773 {
1774         thermal_gov_step_wise_unregister();
1775         thermal_gov_fair_share_unregister();
1776         thermal_gov_user_space_unregister();
1777 }
1778
1779 static int __init thermal_init(void)
1780 {
1781         int result;
1782
1783         result = thermal_register_governors();
1784         if (result)
1785                 goto error;
1786
1787         result = class_register(&thermal_class);
1788         if (result)
1789                 goto unregister_governors;
1790
1791         result = genetlink_init();
1792         if (result)
1793                 goto unregister_class;
1794
1795         result = of_parse_thermal_zones();
1796         if (result)
1797                 goto exit_netlink;
1798
1799         return 0;
1800
1801 exit_netlink:
1802         genetlink_exit();
1803 unregister_governors:
1804         thermal_unregister_governors();
1805 unregister_class:
1806         class_unregister(&thermal_class);
1807 error:
1808         idr_destroy(&thermal_tz_idr);
1809         idr_destroy(&thermal_cdev_idr);
1810         mutex_destroy(&thermal_idr_lock);
1811         mutex_destroy(&thermal_list_lock);
1812         mutex_destroy(&thermal_governor_lock);
1813         return result;
1814 }
1815
1816 static void __exit thermal_exit(void)
1817 {
1818         of_thermal_destroy_zones();
1819         genetlink_exit();
1820         class_unregister(&thermal_class);
1821         thermal_unregister_governors();
1822         idr_destroy(&thermal_tz_idr);
1823         idr_destroy(&thermal_cdev_idr);
1824         mutex_destroy(&thermal_idr_lock);
1825         mutex_destroy(&thermal_list_lock);
1826         mutex_destroy(&thermal_governor_lock);
1827 }
1828
1829 fs_initcall(thermal_init);
1830 module_exit(thermal_exit);