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