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