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