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