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