OSDN Git Service

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