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