thermal: add boundary check to set_cur_state
[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, max_state;
948         int result, ret;
949
950         if (!sscanf(buf, "%ld\n", &state))
951                 return -EINVAL;
952
953         if ((long)state < 0)
954                 return -EINVAL;
955
956         ret = cdev->ops->get_max_state(cdev, &max_state);
957         if (ret)
958                 return ret;
959
960         if (state > max_state)
961                 return -EINVAL;
962
963         result = cdev->ops->set_cur_state(cdev, state);
964         if (result)
965                 return result;
966         return count;
967 }
968
969 static struct device_attribute dev_attr_cdev_type =
970 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
971 static DEVICE_ATTR(max_state, 0444,
972                    thermal_cooling_device_max_state_show, NULL);
973 static DEVICE_ATTR(cur_state, 0644,
974                    thermal_cooling_device_cur_state_show,
975                    thermal_cooling_device_cur_state_store);
976
977 static ssize_t
978 thermal_cooling_device_trip_point_show(struct device *dev,
979                                        struct device_attribute *attr, char *buf)
980 {
981         struct thermal_instance *instance;
982
983         instance =
984             container_of(attr, struct thermal_instance, attr);
985
986         if (instance->trip == THERMAL_TRIPS_NONE)
987                 return sprintf(buf, "-1\n");
988         else
989                 return sprintf(buf, "%d\n", instance->trip);
990 }
991
992 /* Device management */
993
994 /**
995  * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
996  * @tz:         pointer to struct thermal_zone_device
997  * @trip:       indicates which trip point the cooling devices is
998  *              associated with in this thermal zone.
999  * @cdev:       pointer to struct thermal_cooling_device
1000  * @upper:      the Maximum cooling state for this trip point.
1001  *              THERMAL_NO_LIMIT means no upper limit,
1002  *              and the cooling device can be in max_state.
1003  * @lower:      the Minimum cooling state can be used for this trip point.
1004  *              THERMAL_NO_LIMIT means no lower limit,
1005  *              and the cooling device can be in cooling state 0.
1006  *
1007  * This interface function bind a thermal cooling device to the certain trip
1008  * point of a thermal zone device.
1009  * This function is usually called in the thermal zone device .bind callback.
1010  *
1011  * Return: 0 on success, the proper error value otherwise.
1012  */
1013 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1014                                      int trip,
1015                                      struct thermal_cooling_device *cdev,
1016                                      unsigned long upper, unsigned long lower)
1017 {
1018         struct thermal_instance *dev;
1019         struct thermal_instance *pos;
1020         struct thermal_zone_device *pos1;
1021         struct thermal_cooling_device *pos2;
1022         unsigned long max_state;
1023         int result;
1024
1025         if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
1026                 return -EINVAL;
1027
1028         list_for_each_entry(pos1, &thermal_tz_list, node) {
1029                 if (pos1 == tz)
1030                         break;
1031         }
1032         list_for_each_entry(pos2, &thermal_cdev_list, node) {
1033                 if (pos2 == cdev)
1034                         break;
1035         }
1036
1037         if (tz != pos1 || cdev != pos2)
1038                 return -EINVAL;
1039
1040         cdev->ops->get_max_state(cdev, &max_state);
1041
1042         /* lower default 0, upper default max_state */
1043         lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1044         upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1045
1046         if (lower > upper || upper > max_state)
1047                 return -EINVAL;
1048
1049         dev =
1050             kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
1051         if (!dev)
1052                 return -ENOMEM;
1053         dev->tz = tz;
1054         dev->cdev = cdev;
1055         dev->trip = trip;
1056         dev->upper = upper;
1057         dev->lower = lower;
1058         dev->target = THERMAL_NO_TARGET;
1059
1060         result = get_idr(&tz->idr, &tz->lock, &dev->id);
1061         if (result)
1062                 goto free_mem;
1063
1064         sprintf(dev->name, "cdev%d", dev->id);
1065         result =
1066             sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1067         if (result)
1068                 goto release_idr;
1069
1070         sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
1071         sysfs_attr_init(&dev->attr.attr);
1072         dev->attr.attr.name = dev->attr_name;
1073         dev->attr.attr.mode = 0444;
1074         dev->attr.show = thermal_cooling_device_trip_point_show;
1075         result = device_create_file(&tz->device, &dev->attr);
1076         if (result)
1077                 goto remove_symbol_link;
1078
1079         mutex_lock(&tz->lock);
1080         mutex_lock(&cdev->lock);
1081         list_for_each_entry(pos, &tz->thermal_instances, tz_node)
1082             if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1083                 result = -EEXIST;
1084                 break;
1085         }
1086         if (!result) {
1087                 list_add_tail(&dev->tz_node, &tz->thermal_instances);
1088                 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1089         }
1090         mutex_unlock(&cdev->lock);
1091         mutex_unlock(&tz->lock);
1092
1093         if (!result)
1094                 return 0;
1095
1096         device_remove_file(&tz->device, &dev->attr);
1097 remove_symbol_link:
1098         sysfs_remove_link(&tz->device.kobj, dev->name);
1099 release_idr:
1100         release_idr(&tz->idr, &tz->lock, dev->id);
1101 free_mem:
1102         kfree(dev);
1103         return result;
1104 }
1105 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
1106
1107 /**
1108  * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1109  *                                        thermal zone.
1110  * @tz:         pointer to a struct thermal_zone_device.
1111  * @trip:       indicates which trip point the cooling devices is
1112  *              associated with in this thermal zone.
1113  * @cdev:       pointer to a struct thermal_cooling_device.
1114  *
1115  * This interface function unbind a thermal cooling device from the certain
1116  * trip point of a thermal zone device.
1117  * This function is usually called in the thermal zone device .unbind callback.
1118  *
1119  * Return: 0 on success, the proper error value otherwise.
1120  */
1121 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1122                                        int trip,
1123                                        struct thermal_cooling_device *cdev)
1124 {
1125         struct thermal_instance *pos, *next;
1126
1127         mutex_lock(&tz->lock);
1128         mutex_lock(&cdev->lock);
1129         list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
1130                 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1131                         list_del(&pos->tz_node);
1132                         list_del(&pos->cdev_node);
1133                         mutex_unlock(&cdev->lock);
1134                         mutex_unlock(&tz->lock);
1135                         goto unbind;
1136                 }
1137         }
1138         mutex_unlock(&cdev->lock);
1139         mutex_unlock(&tz->lock);
1140
1141         return -ENODEV;
1142
1143 unbind:
1144         device_remove_file(&tz->device, &pos->attr);
1145         sysfs_remove_link(&tz->device.kobj, pos->name);
1146         release_idr(&tz->idr, &tz->lock, pos->id);
1147         kfree(pos);
1148         return 0;
1149 }
1150 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
1151
1152 static void thermal_release(struct device *dev)
1153 {
1154         struct thermal_zone_device *tz;
1155         struct thermal_cooling_device *cdev;
1156
1157         if (!strncmp(dev_name(dev), "thermal_zone",
1158                      sizeof("thermal_zone") - 1)) {
1159                 tz = to_thermal_zone(dev);
1160                 kfree(tz);
1161         } else if(!strncmp(dev_name(dev), "cooling_device",
1162                         sizeof("cooling_device") - 1)){
1163                 cdev = to_cooling_device(dev);
1164                 kfree(cdev);
1165         }
1166 }
1167
1168 static struct class thermal_class = {
1169         .name = "thermal",
1170         .dev_release = thermal_release,
1171 };
1172
1173 /**
1174  * __thermal_cooling_device_register() - register a new thermal cooling device
1175  * @np:         a pointer to a device tree node.
1176  * @type:       the thermal cooling device type.
1177  * @devdata:    device private data.
1178  * @ops:                standard thermal cooling devices callbacks.
1179  *
1180  * This interface function adds a new thermal cooling device (fan/processor/...)
1181  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1182  * to all the thermal zone devices registered at the same time.
1183  * It also gives the opportunity to link the cooling device to a device tree
1184  * node, so that it can be bound to a thermal zone created out of device tree.
1185  *
1186  * Return: a pointer to the created struct thermal_cooling_device or an
1187  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1188  */
1189 static struct thermal_cooling_device *
1190 __thermal_cooling_device_register(struct device_node *np,
1191                                   char *type, void *devdata,
1192                                   const struct thermal_cooling_device_ops *ops)
1193 {
1194         struct thermal_cooling_device *cdev;
1195         int result;
1196
1197         if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1198                 return ERR_PTR(-EINVAL);
1199
1200         if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1201             !ops->set_cur_state)
1202                 return ERR_PTR(-EINVAL);
1203
1204         cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1205         if (!cdev)
1206                 return ERR_PTR(-ENOMEM);
1207
1208         result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1209         if (result) {
1210                 kfree(cdev);
1211                 return ERR_PTR(result);
1212         }
1213
1214         strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
1215         mutex_init(&cdev->lock);
1216         INIT_LIST_HEAD(&cdev->thermal_instances);
1217         cdev->np = np;
1218         cdev->ops = ops;
1219         cdev->updated = false;
1220         cdev->device.class = &thermal_class;
1221         cdev->devdata = devdata;
1222         dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1223         result = device_register(&cdev->device);
1224         if (result) {
1225                 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1226                 kfree(cdev);
1227                 return ERR_PTR(result);
1228         }
1229
1230         /* sys I/F */
1231         if (type) {
1232                 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
1233                 if (result)
1234                         goto unregister;
1235         }
1236
1237         result = device_create_file(&cdev->device, &dev_attr_max_state);
1238         if (result)
1239                 goto unregister;
1240
1241         result = device_create_file(&cdev->device, &dev_attr_cur_state);
1242         if (result)
1243                 goto unregister;
1244
1245         /* Add 'this' new cdev to the global cdev list */
1246         mutex_lock(&thermal_list_lock);
1247         list_add(&cdev->node, &thermal_cdev_list);
1248         mutex_unlock(&thermal_list_lock);
1249
1250         /* Update binding information for 'this' new cdev */
1251         bind_cdev(cdev);
1252
1253         return cdev;
1254
1255 unregister:
1256         release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1257         device_unregister(&cdev->device);
1258         return ERR_PTR(result);
1259 }
1260
1261 /**
1262  * thermal_cooling_device_register() - register a new thermal cooling device
1263  * @type:       the thermal cooling device type.
1264  * @devdata:    device private data.
1265  * @ops:                standard thermal cooling devices callbacks.
1266  *
1267  * This interface function adds a new thermal cooling device (fan/processor/...)
1268  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1269  * to all the thermal zone devices registered at the same time.
1270  *
1271  * Return: a pointer to the created struct thermal_cooling_device or an
1272  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1273  */
1274 struct thermal_cooling_device *
1275 thermal_cooling_device_register(char *type, void *devdata,
1276                                 const struct thermal_cooling_device_ops *ops)
1277 {
1278         return __thermal_cooling_device_register(NULL, type, devdata, ops);
1279 }
1280 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1281
1282 /**
1283  * thermal_of_cooling_device_register() - register an OF thermal cooling device
1284  * @np:         a pointer to a device tree node.
1285  * @type:       the thermal cooling device type.
1286  * @devdata:    device private data.
1287  * @ops:                standard thermal cooling devices callbacks.
1288  *
1289  * This function will register a cooling device with device tree node reference.
1290  * This interface function adds a new thermal cooling device (fan/processor/...)
1291  * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1292  * to all the thermal zone devices registered at the same time.
1293  *
1294  * Return: a pointer to the created struct thermal_cooling_device or an
1295  * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1296  */
1297 struct thermal_cooling_device *
1298 thermal_of_cooling_device_register(struct device_node *np,
1299                                    char *type, void *devdata,
1300                                    const struct thermal_cooling_device_ops *ops)
1301 {
1302         return __thermal_cooling_device_register(np, type, devdata, ops);
1303 }
1304 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1305
1306 /**
1307  * thermal_cooling_device_unregister - removes the registered thermal cooling device
1308  * @cdev:       the thermal cooling device to remove.
1309  *
1310  * thermal_cooling_device_unregister() must be called when the device is no
1311  * longer needed.
1312  */
1313 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1314 {
1315         int i;
1316         const struct thermal_zone_params *tzp;
1317         struct thermal_zone_device *tz;
1318         struct thermal_cooling_device *pos = NULL;
1319
1320         if (!cdev)
1321                 return;
1322
1323         mutex_lock(&thermal_list_lock);
1324         list_for_each_entry(pos, &thermal_cdev_list, node)
1325             if (pos == cdev)
1326                 break;
1327         if (pos != cdev) {
1328                 /* thermal cooling device not found */
1329                 mutex_unlock(&thermal_list_lock);
1330                 return;
1331         }
1332         list_del(&cdev->node);
1333
1334         /* Unbind all thermal zones associated with 'this' cdev */
1335         list_for_each_entry(tz, &thermal_tz_list, node) {
1336                 if (tz->ops->unbind) {
1337                         tz->ops->unbind(tz, cdev);
1338                         continue;
1339                 }
1340
1341                 if (!tz->tzp || !tz->tzp->tbp)
1342                         continue;
1343
1344                 tzp = tz->tzp;
1345                 for (i = 0; i < tzp->num_tbps; i++) {
1346                         if (tzp->tbp[i].cdev == cdev) {
1347                                 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1348                                 tzp->tbp[i].cdev = NULL;
1349                         }
1350                 }
1351         }
1352
1353         mutex_unlock(&thermal_list_lock);
1354
1355         if (cdev->type[0])
1356                 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1357         device_remove_file(&cdev->device, &dev_attr_max_state);
1358         device_remove_file(&cdev->device, &dev_attr_cur_state);
1359
1360         release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1361         device_unregister(&cdev->device);
1362         return;
1363 }
1364 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1365
1366 void thermal_cdev_update(struct thermal_cooling_device *cdev)
1367 {
1368         struct thermal_instance *instance;
1369         unsigned long target = 0;
1370
1371         /* cooling device is updated*/
1372         if (cdev->updated)
1373                 return;
1374
1375         mutex_lock(&cdev->lock);
1376         /* Make sure cdev enters the deepest cooling state */
1377         list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1378                 if (instance->target == THERMAL_NO_TARGET)
1379                         continue;
1380                 if (instance->target > target)
1381                         target = instance->target;
1382         }
1383         mutex_unlock(&cdev->lock);
1384         cdev->ops->set_cur_state(cdev, target);
1385         trace_cooling_device_update(cdev->type, target);
1386         cdev->updated = true;
1387 }
1388 EXPORT_SYMBOL(thermal_cdev_update);
1389
1390 /**
1391  * thermal_notify_framework - Sensor drivers use this API to notify framework
1392  * @tz:         thermal zone device
1393  * @trip:       indicates which trip point has been crossed
1394  *
1395  * This function handles the trip events from sensor drivers. It starts
1396  * throttling the cooling devices according to the policy configured.
1397  * For CRITICAL and HOT trip points, this notifies the respective drivers,
1398  * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1399  * The throttling policy is based on the configured platform data; if no
1400  * platform data is provided, this uses the step_wise throttling policy.
1401  */
1402 void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
1403 {
1404         handle_thermal_trip(tz, trip);
1405 }
1406 EXPORT_SYMBOL_GPL(thermal_notify_framework);
1407
1408 struct thermal_zone_device *thermal_zone_device_find(void *data,
1409         int (*match)(struct thermal_zone_device *, void *))
1410 {
1411         struct thermal_zone_device *thz;
1412
1413         mutex_lock(&thermal_list_lock);
1414         list_for_each_entry(thz, &thermal_tz_list, node)
1415                 if (match(thz, data)) {
1416                         mutex_unlock(&thermal_list_lock);
1417                         return thz;
1418                 }
1419
1420         mutex_unlock(&thermal_list_lock);
1421         return NULL;
1422 }
1423 EXPORT_SYMBOL(thermal_zone_device_find);
1424
1425 struct thermal_zone_device *thermal_zone_device_find_by_name(const char *name)
1426 {
1427         struct thermal_zone_device *thz;
1428
1429         if (!name)
1430                 return NULL;
1431
1432         mutex_lock(&thermal_list_lock);
1433         list_for_each_entry(thz, &thermal_tz_list, node) {
1434                 if (!strncmp(name, thz->type, strlen(name))) {
1435                         mutex_unlock(&thermal_list_lock);
1436                         return thz;
1437                 }
1438         }
1439         mutex_unlock(&thermal_list_lock);
1440         return NULL;
1441 }
1442 EXPORT_SYMBOL(thermal_zone_device_find_by_name);
1443
1444 /**
1445  * create_trip_attrs() - create attributes for trip points
1446  * @tz:         the thermal zone device
1447  * @mask:       Writeable trip point bitmap.
1448  *
1449  * helper function to instantiate sysfs entries for every trip
1450  * point and its properties of a struct thermal_zone_device.
1451  *
1452  * Return: 0 on success, the proper error value otherwise.
1453  */
1454 static int create_trip_attrs(struct thermal_zone_device *tz, u64 mask)
1455 {
1456         int indx;
1457         int size = sizeof(struct thermal_attr) * tz->trips;
1458
1459         tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1460         if (!tz->trip_type_attrs)
1461                 return -ENOMEM;
1462
1463         tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1464         if (!tz->trip_temp_attrs) {
1465                 kfree(tz->trip_type_attrs);
1466                 return -ENOMEM;
1467         }
1468
1469         if (tz->ops->get_trip_hyst) {
1470                 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1471                 if (!tz->trip_hyst_attrs) {
1472                         kfree(tz->trip_type_attrs);
1473                         kfree(tz->trip_temp_attrs);
1474                         return -ENOMEM;
1475                 }
1476         }
1477
1478
1479         for (indx = 0; indx < tz->trips; indx++) {
1480                 /* create trip type attribute */
1481                 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1482                          "trip_point_%d_type", indx);
1483
1484                 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1485                 tz->trip_type_attrs[indx].attr.attr.name =
1486                                                 tz->trip_type_attrs[indx].name;
1487                 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1488                 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1489
1490                 device_create_file(&tz->device,
1491                                    &tz->trip_type_attrs[indx].attr);
1492
1493                 /* create trip temp attribute */
1494                 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1495                          "trip_point_%d_temp", indx);
1496
1497                 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1498                 tz->trip_temp_attrs[indx].attr.attr.name =
1499                                                 tz->trip_temp_attrs[indx].name;
1500                 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1501                 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1502                 if (mask & (1ULL << indx)) {
1503                         tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1504                         tz->trip_temp_attrs[indx].attr.store =
1505                                                         trip_point_temp_store;
1506                 }
1507
1508                 device_create_file(&tz->device,
1509                                    &tz->trip_temp_attrs[indx].attr);
1510
1511                 /* create Optional trip hyst attribute */
1512                 if (!tz->ops->get_trip_hyst)
1513                         continue;
1514                 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1515                          "trip_point_%d_hyst", indx);
1516
1517                 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1518                 tz->trip_hyst_attrs[indx].attr.attr.name =
1519                                         tz->trip_hyst_attrs[indx].name;
1520                 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1521                 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1522                 if (tz->ops->set_trip_hyst) {
1523                         tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1524                         tz->trip_hyst_attrs[indx].attr.store =
1525                                         trip_point_hyst_store;
1526                 }
1527
1528                 device_create_file(&tz->device,
1529                                    &tz->trip_hyst_attrs[indx].attr);
1530         }
1531         return 0;
1532 }
1533
1534 static void remove_trip_attrs(struct thermal_zone_device *tz)
1535 {
1536         int indx;
1537
1538         for (indx = 0; indx < tz->trips; indx++) {
1539                 device_remove_file(&tz->device,
1540                                    &tz->trip_type_attrs[indx].attr);
1541                 device_remove_file(&tz->device,
1542                                    &tz->trip_temp_attrs[indx].attr);
1543                 if (tz->ops->get_trip_hyst)
1544                         device_remove_file(&tz->device,
1545                                   &tz->trip_hyst_attrs[indx].attr);
1546         }
1547         kfree(tz->trip_type_attrs);
1548         kfree(tz->trip_temp_attrs);
1549         kfree(tz->trip_hyst_attrs);
1550 }
1551
1552 /**
1553  * thermal_zone_device_register() - register a new thermal zone device
1554  * @type:       the thermal zone device type
1555  * @trips:      the number of trip points the thermal zone support
1556  * @mask:       a bit string indicating the writeablility of trip points
1557  * @devdata:    private device data
1558  * @ops:        standard thermal zone device callbacks
1559  * @tzp:        thermal zone platform parameters
1560  * @passive_delay: number of milliseconds to wait between polls when
1561  *                 performing passive cooling
1562  * @polling_delay: number of milliseconds to wait between polls when checking
1563  *                 whether trip points have been crossed (0 for interrupt
1564  *                 driven systems)
1565  *
1566  * This interface function adds a new thermal zone device (sensor) to
1567  * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1568  * thermal cooling devices registered at the same time.
1569  * thermal_zone_device_unregister() must be called when the device is no
1570  * longer needed. The passive cooling depends on the .get_trend() return value.
1571  *
1572  * Return: a pointer to the created struct thermal_zone_device or an
1573  * in case of error, an ERR_PTR. Caller must check return value with
1574  * IS_ERR*() helpers.
1575  */
1576 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1577         int trips, u64 mask, void *devdata,
1578         struct thermal_zone_device_ops *ops,
1579         const struct thermal_zone_params *tzp,
1580         int passive_delay, int polling_delay)
1581 {
1582         struct thermal_zone_device *tz;
1583         enum thermal_trip_type trip_type;
1584         int result;
1585         int count;
1586         int passive = 0;
1587
1588         if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1589                 return ERR_PTR(-EINVAL);
1590
1591         if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1592                 return ERR_PTR(-EINVAL);
1593
1594         if (!ops)
1595                 return ERR_PTR(-EINVAL);
1596
1597         if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1598                 return ERR_PTR(-EINVAL);
1599
1600         tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1601         if (!tz)
1602                 return ERR_PTR(-ENOMEM);
1603
1604         INIT_LIST_HEAD(&tz->thermal_instances);
1605         idr_init(&tz->idr);
1606         mutex_init(&tz->lock);
1607         result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1608         if (result) {
1609                 kfree(tz);
1610                 return ERR_PTR(result);
1611         }
1612
1613         strlcpy(tz->type, type ? : "", sizeof(tz->type));
1614         tz->ops = ops;
1615         tz->tzp = tzp;
1616         tz->device.class = &thermal_class;
1617         tz->devdata = devdata;
1618         tz->trips = trips;
1619         tz->passive_delay = passive_delay;
1620         tz->polling_delay = polling_delay;
1621
1622         dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1623         result = device_register(&tz->device);
1624         if (result) {
1625                 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1626                 kfree(tz);
1627                 return ERR_PTR(result);
1628         }
1629
1630         /* sys I/F */
1631         if (type) {
1632                 result = device_create_file(&tz->device, &dev_attr_type);
1633                 if (result)
1634                         goto unregister;
1635         }
1636
1637         result = device_create_file(&tz->device, &dev_attr_temp);
1638         if (result)
1639                 goto unregister;
1640
1641         if (ops->get_mode) {
1642                 result = device_create_file(&tz->device, &dev_attr_mode);
1643                 if (result)
1644                         goto unregister;
1645         }
1646
1647         result = create_trip_attrs(tz, mask);
1648         if (result)
1649                 goto unregister;
1650
1651         for (count = 0; count < trips; count++) {
1652                 tz->ops->get_trip_type(tz, count, &trip_type);
1653                 if (trip_type == THERMAL_TRIP_PASSIVE)
1654                         passive = 1;
1655         }
1656
1657         if (!passive) {
1658                 result = device_create_file(&tz->device, &dev_attr_passive);
1659                 if (result)
1660                         goto unregister;
1661         }
1662
1663 #ifdef CONFIG_THERMAL_EMULATION
1664         result = device_create_file(&tz->device, &dev_attr_emul_temp);
1665         if (result)
1666                 goto unregister;
1667 #endif
1668         /* Create policy attribute */
1669         result = device_create_file(&tz->device, &dev_attr_policy);
1670         if (result)
1671                 goto unregister;
1672
1673         /* Create passive_delay attribute */
1674         result = device_create_file(&tz->device, &dev_attr_passive_delay);
1675         if (result)
1676                 goto unregister;
1677
1678         /* Create polling_delay attribute */
1679         result = device_create_file(&tz->device, &dev_attr_polling_delay);
1680         if (result)
1681                 goto unregister;
1682
1683         /* Create available_policies attribute */
1684         result = device_create_file(&tz->device, &dev_attr_available_policies);
1685         if (result)
1686                 goto unregister;
1687
1688         /* Update 'this' zone's governor information */
1689         mutex_lock(&thermal_governor_lock);
1690
1691         if (tz->tzp)
1692                 tz->governor = __find_governor(tz->tzp->governor_name);
1693
1694         if (!tz->governor) {
1695                 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1696                 if (!tz->governor) {
1697                         mutex_unlock(&thermal_governor_lock);
1698                         goto unregister;
1699                 }
1700         }
1701
1702         if (tz->governor->start) {
1703                 result = tz->governor->start(tz);
1704                 if (result < 0) {
1705                         mutex_unlock(&thermal_governor_lock);
1706                         goto unregister;
1707                 }
1708         }
1709
1710         mutex_unlock(&thermal_governor_lock);
1711
1712         if (!tz->tzp || !tz->tzp->no_hwmon) {
1713                 result = thermal_add_hwmon_sysfs(tz);
1714                 if (result)
1715                         goto unregister;
1716         }
1717
1718         mutex_lock(&thermal_list_lock);
1719         list_add_tail(&tz->node, &thermal_tz_list);
1720         mutex_unlock(&thermal_list_lock);
1721
1722         /* Bind cooling devices for this zone */
1723         bind_tz(tz);
1724
1725         INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1726
1727         if (!tz->ops->get_temp)
1728                 thermal_zone_device_set_polling(tz, 0);
1729
1730         thermal_zone_device_update(tz);
1731
1732         dev_info(&tz->device, "Registering thermal zone %s for type %s\n",
1733                         dev_name(&tz->device), type);
1734         if (!result)
1735                 return tz;
1736
1737 unregister:
1738         if (tz->governor && tz->governor->stop)
1739                 tz->governor->stop(tz);
1740         tz->governor = NULL;
1741         release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1742         device_unregister(&tz->device);
1743         return ERR_PTR(result);
1744 }
1745 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1746
1747 /**
1748  * thermal_device_unregister - removes the registered thermal zone device
1749  * @tz: the thermal zone device to remove
1750  */
1751 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1752 {
1753         int i;
1754         const struct thermal_zone_params *tzp;
1755         struct thermal_cooling_device *cdev;
1756         struct thermal_zone_device *pos = NULL;
1757
1758         if (!tz)
1759                 return;
1760
1761         tzp = tz->tzp;
1762
1763         mutex_lock(&thermal_list_lock);
1764         list_for_each_entry(pos, &thermal_tz_list, node)
1765             if (pos == tz)
1766                 break;
1767         if (pos != tz) {
1768                 /* thermal zone device not found */
1769                 mutex_unlock(&thermal_list_lock);
1770                 return;
1771         }
1772         list_del(&tz->node);
1773
1774         /* Unbind all cdevs associated with 'this' thermal zone */
1775         list_for_each_entry(cdev, &thermal_cdev_list, node) {
1776                 if (tz->ops->unbind) {
1777                         tz->ops->unbind(tz, cdev);
1778                         continue;
1779                 }
1780
1781                 if (!tzp || !tzp->tbp)
1782                         break;
1783
1784                 for (i = 0; i < tzp->num_tbps; i++) {
1785                         if (tzp->tbp[i].cdev == cdev) {
1786                                 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1787                                 tzp->tbp[i].cdev = NULL;
1788                         }
1789                 }
1790         }
1791
1792         mutex_unlock(&thermal_list_lock);
1793
1794         thermal_zone_device_set_polling(tz, 0);
1795
1796         if (tz->type[0])
1797                 device_remove_file(&tz->device, &dev_attr_type);
1798         device_remove_file(&tz->device, &dev_attr_temp);
1799         if (tz->ops->get_mode)
1800                 device_remove_file(&tz->device, &dev_attr_mode);
1801         device_remove_file(&tz->device, &dev_attr_passive_delay);
1802         device_remove_file(&tz->device, &dev_attr_polling_delay);
1803         device_remove_file(&tz->device, &dev_attr_policy);
1804         device_remove_file(&tz->device, &dev_attr_available_policies);
1805         remove_trip_attrs(tz);
1806
1807         if (tz->governor && tz->governor->stop)
1808                 tz->governor->stop(tz);
1809         tz->governor = NULL;
1810
1811         thermal_remove_hwmon_sysfs(tz);
1812         release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1813         idr_destroy(&tz->idr);
1814         mutex_destroy(&tz->lock);
1815         device_unregister(&tz->device);
1816         return;
1817 }
1818 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1819
1820 /**
1821  * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1822  * @name: thermal zone name to fetch the temperature
1823  *
1824  * When only one zone is found with the passed name, returns a reference to it.
1825  *
1826  * Return: On success returns a reference to an unique thermal zone with
1827  * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1828  * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1829  */
1830 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1831 {
1832         struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1833         unsigned int found = 0;
1834
1835         if (!name)
1836                 goto exit;
1837
1838         mutex_lock(&thermal_list_lock);
1839         list_for_each_entry(pos, &thermal_tz_list, node)
1840                 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1841                         found++;
1842                         ref = pos;
1843                 }
1844         mutex_unlock(&thermal_list_lock);
1845
1846         /* nothing has been found, thus an error code for it */
1847         if (found == 0)
1848                 ref = ERR_PTR(-ENODEV);
1849         else if (found > 1)
1850         /* Success only when an unique zone is found */
1851                 ref = ERR_PTR(-EEXIST);
1852
1853 exit:
1854         return ref;
1855 }
1856 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1857
1858 #ifdef CONFIG_NET
1859 static struct genl_family thermal_event_genl_family = {
1860         .id = GENL_ID_GENERATE,
1861         .name = THERMAL_GENL_FAMILY_NAME,
1862         .version = THERMAL_GENL_VERSION,
1863         .maxattr = THERMAL_GENL_ATTR_MAX,
1864 };
1865
1866 static struct genl_multicast_group thermal_event_mcgrp = {
1867         .name = THERMAL_GENL_MCAST_GROUP_NAME,
1868 };
1869
1870 int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1871                                    enum events event, int temp)
1872 {
1873         struct sk_buff *skb;
1874         struct nlattr *attr;
1875         struct thermal_genl_event *thermal_event;
1876         void *msg_header;
1877         int size;
1878         int result;
1879         static unsigned int thermal_event_seqnum;
1880
1881         if (!tz)
1882                 return -EINVAL;
1883
1884         /* allocate memory */
1885         size = nla_total_size(sizeof(struct thermal_genl_event)) +
1886                nla_total_size(0);
1887
1888         skb = genlmsg_new(size, GFP_ATOMIC);
1889         if (!skb)
1890                 return -ENOMEM;
1891
1892         /* add the genetlink message header */
1893         msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1894                                  &thermal_event_genl_family, 0,
1895                                  THERMAL_GENL_CMD_EVENT);
1896         if (!msg_header) {
1897                 nlmsg_free(skb);
1898                 return -ENOMEM;
1899         }
1900
1901         /* fill the data */
1902         attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1903                            sizeof(struct thermal_genl_event));
1904
1905         if (!attr) {
1906                 nlmsg_free(skb);
1907                 return -EINVAL;
1908         }
1909
1910         thermal_event = nla_data(attr);
1911         if (!thermal_event) {
1912                 nlmsg_free(skb);
1913                 return -EINVAL;
1914         }
1915
1916         memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1917
1918         thermal_event->orig = tz->id;
1919         thermal_event->event = event;
1920         thermal_event->temp = temp;
1921
1922         /* send multicast genetlink message */
1923         result = genlmsg_end(skb, msg_header);
1924         if (result < 0) {
1925                 nlmsg_free(skb);
1926                 return result;
1927         }
1928
1929         genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1930         return result;
1931 }
1932 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
1933
1934 static int genetlink_init(void)
1935 {
1936         int result;
1937
1938         result = genl_register_family(&thermal_event_genl_family);
1939         if (result)
1940                 return result;
1941
1942         result = genl_register_mc_group(&thermal_event_genl_family,
1943                                         &thermal_event_mcgrp);
1944         if (result)
1945                 genl_unregister_family(&thermal_event_genl_family);
1946         return result;
1947 }
1948
1949 static void genetlink_exit(void)
1950 {
1951         genl_unregister_family(&thermal_event_genl_family);
1952 }
1953 #else /* !CONFIG_NET */
1954 static inline int genetlink_init(void) { return 0; }
1955 static inline void genetlink_exit(void) {}
1956 #endif /* !CONFIG_NET */
1957
1958 static int __init thermal_register_governors(void)
1959 {
1960         int result;
1961
1962         result = thermal_gov_step_wise_register();
1963         if (result)
1964                 return result;
1965
1966         result = thermal_gov_fair_share_register();
1967         if (result)
1968                 return result;
1969
1970         result = pid_thermal_gov_register();
1971         if (result)
1972                 return result;
1973
1974         return thermal_gov_user_space_register();
1975 }
1976
1977 static void thermal_unregister_governors(void)
1978 {
1979         thermal_gov_step_wise_unregister();
1980         thermal_gov_fair_share_unregister();
1981         pid_thermal_gov_unregister();
1982         thermal_gov_user_space_unregister();
1983 }
1984
1985 static int __init thermal_init(void)
1986 {
1987         int result;
1988
1989         result = thermal_register_governors();
1990         if (result)
1991                 goto error;
1992
1993         result = class_register(&thermal_class);
1994         if (result)
1995                 goto unregister_governors;
1996
1997         result = genetlink_init();
1998         if (result)
1999                 goto unregister_class;
2000
2001         result = of_parse_thermal_zones();
2002         if (result)
2003                 goto exit_netlink;
2004
2005         return 0;
2006
2007 exit_netlink:
2008         genetlink_exit();
2009 unregister_governors:
2010         thermal_unregister_governors();
2011 unregister_class:
2012         class_unregister(&thermal_class);
2013 error:
2014         idr_destroy(&thermal_tz_idr);
2015         idr_destroy(&thermal_cdev_idr);
2016         mutex_destroy(&thermal_idr_lock);
2017         mutex_destroy(&thermal_list_lock);
2018         mutex_destroy(&thermal_governor_lock);
2019         return result;
2020 }
2021
2022 static void __exit thermal_exit(void)
2023 {
2024         of_thermal_destroy_zones();
2025         genetlink_exit();
2026         class_unregister(&thermal_class);
2027         thermal_unregister_governors();
2028         idr_destroy(&thermal_tz_idr);
2029         idr_destroy(&thermal_cdev_idr);
2030         mutex_destroy(&thermal_idr_lock);
2031         mutex_destroy(&thermal_list_lock);
2032         mutex_destroy(&thermal_governor_lock);
2033 }
2034
2035 fs_initcall(thermal_init);
2036 module_exit(thermal_exit);