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