ACPI: move thermal trip handling to generic thermal layer
[linux-2.6.git] / drivers / thermal / thermal_sys.c
1 /*
2  *  thermal.c - Generic Thermal Management Sysfs support.
3  *
4  *  Copyright (C) 2008 Intel Corp
5  *  Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
6  *  Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
7  *
8  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; version 2 of the License.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  */
25
26 #include <linux/module.h>
27 #include <linux/device.h>
28 #include <linux/err.h>
29 #include <linux/kdev_t.h>
30 #include <linux/idr.h>
31 #include <linux/thermal.h>
32 #include <linux/spinlock.h>
33 #include <linux/reboot.h>
34
35 MODULE_AUTHOR("Zhang Rui");
36 MODULE_DESCRIPTION("Generic thermal management sysfs support");
37 MODULE_LICENSE("GPL");
38
39 #define PREFIX "Thermal: "
40
41 struct thermal_cooling_device_instance {
42         int id;
43         char name[THERMAL_NAME_LENGTH];
44         struct thermal_zone_device *tz;
45         struct thermal_cooling_device *cdev;
46         int trip;
47         char attr_name[THERMAL_NAME_LENGTH];
48         struct device_attribute attr;
49         struct list_head node;
50 };
51
52 static DEFINE_IDR(thermal_tz_idr);
53 static DEFINE_IDR(thermal_cdev_idr);
54 static DEFINE_MUTEX(thermal_idr_lock);
55
56 static LIST_HEAD(thermal_tz_list);
57 static LIST_HEAD(thermal_cdev_list);
58 static DEFINE_MUTEX(thermal_list_lock);
59
60 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
61 {
62         int err;
63
64       again:
65         if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
66                 return -ENOMEM;
67
68         if (lock)
69                 mutex_lock(lock);
70         err = idr_get_new(idr, NULL, id);
71         if (lock)
72                 mutex_unlock(lock);
73         if (unlikely(err == -EAGAIN))
74                 goto again;
75         else if (unlikely(err))
76                 return err;
77
78         *id = *id & MAX_ID_MASK;
79         return 0;
80 }
81
82 static void release_idr(struct idr *idr, struct mutex *lock, int id)
83 {
84         if (lock)
85                 mutex_lock(lock);
86         idr_remove(idr, id);
87         if (lock)
88                 mutex_unlock(lock);
89 }
90
91 /* sys I/F for thermal zone */
92
93 #define to_thermal_zone(_dev) \
94         container_of(_dev, struct thermal_zone_device, device)
95
96 static ssize_t
97 type_show(struct device *dev, struct device_attribute *attr, char *buf)
98 {
99         struct thermal_zone_device *tz = to_thermal_zone(dev);
100
101         return sprintf(buf, "%s\n", tz->type);
102 }
103
104 static ssize_t
105 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
106 {
107         struct thermal_zone_device *tz = to_thermal_zone(dev);
108         long temperature;
109         int ret;
110
111         if (!tz->ops->get_temp)
112                 return -EPERM;
113
114         ret = tz->ops->get_temp(tz, &temperature);
115
116         if (ret)
117                 return ret;
118
119         return sprintf(buf, "%ld\n", temperature);
120 }
121
122 static ssize_t
123 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
124 {
125         struct thermal_zone_device *tz = to_thermal_zone(dev);
126         enum thermal_device_mode mode;
127         int result;
128
129         if (!tz->ops->get_mode)
130                 return -EPERM;
131
132         result = tz->ops->get_mode(tz, &mode);
133         if (result)
134                 return result;
135
136         return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
137                        : "disabled");
138 }
139
140 static ssize_t
141 mode_store(struct device *dev, struct device_attribute *attr,
142            const char *buf, size_t count)
143 {
144         struct thermal_zone_device *tz = to_thermal_zone(dev);
145         int result;
146
147         if (!tz->ops->set_mode)
148                 return -EPERM;
149
150         if (!strncmp(buf, "enabled", sizeof("enabled")))
151                 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
152         else if (!strncmp(buf, "disabled", sizeof("disabled")))
153                 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
154         else
155                 result = -EINVAL;
156
157         if (result)
158                 return result;
159
160         return count;
161 }
162
163 static ssize_t
164 trip_point_type_show(struct device *dev, struct device_attribute *attr,
165                      char *buf)
166 {
167         struct thermal_zone_device *tz = to_thermal_zone(dev);
168         enum thermal_trip_type type;
169         int trip, result;
170
171         if (!tz->ops->get_trip_type)
172                 return -EPERM;
173
174         if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
175                 return -EINVAL;
176
177         result = tz->ops->get_trip_type(tz, trip, &type);
178         if (result)
179                 return result;
180
181         switch (type) {
182         case THERMAL_TRIP_CRITICAL:
183                 return sprintf(buf, "critical");
184         case THERMAL_TRIP_HOT:
185                 return sprintf(buf, "hot");
186         case THERMAL_TRIP_PASSIVE:
187                 return sprintf(buf, "passive");
188         case THERMAL_TRIP_ACTIVE:
189                 return sprintf(buf, "active");
190         default:
191                 return sprintf(buf, "unknown");
192         }
193 }
194
195 static ssize_t
196 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
197                      char *buf)
198 {
199         struct thermal_zone_device *tz = to_thermal_zone(dev);
200         int trip, ret;
201         long temperature;
202
203         if (!tz->ops->get_trip_temp)
204                 return -EPERM;
205
206         if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
207                 return -EINVAL;
208
209         ret = tz->ops->get_trip_temp(tz, trip, &temperature);
210
211         if (ret)
212                 return ret;
213
214         return sprintf(buf, "%ld\n", temperature);
215 }
216
217 static DEVICE_ATTR(type, 0444, type_show, NULL);
218 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
219 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
220
221 static struct device_attribute trip_point_attrs[] = {
222         __ATTR(trip_point_0_type, 0444, trip_point_type_show, NULL),
223         __ATTR(trip_point_0_temp, 0444, trip_point_temp_show, NULL),
224         __ATTR(trip_point_1_type, 0444, trip_point_type_show, NULL),
225         __ATTR(trip_point_1_temp, 0444, trip_point_temp_show, NULL),
226         __ATTR(trip_point_2_type, 0444, trip_point_type_show, NULL),
227         __ATTR(trip_point_2_temp, 0444, trip_point_temp_show, NULL),
228         __ATTR(trip_point_3_type, 0444, trip_point_type_show, NULL),
229         __ATTR(trip_point_3_temp, 0444, trip_point_temp_show, NULL),
230         __ATTR(trip_point_4_type, 0444, trip_point_type_show, NULL),
231         __ATTR(trip_point_4_temp, 0444, trip_point_temp_show, NULL),
232         __ATTR(trip_point_5_type, 0444, trip_point_type_show, NULL),
233         __ATTR(trip_point_5_temp, 0444, trip_point_temp_show, NULL),
234         __ATTR(trip_point_6_type, 0444, trip_point_type_show, NULL),
235         __ATTR(trip_point_6_temp, 0444, trip_point_temp_show, NULL),
236         __ATTR(trip_point_7_type, 0444, trip_point_type_show, NULL),
237         __ATTR(trip_point_7_temp, 0444, trip_point_temp_show, NULL),
238         __ATTR(trip_point_8_type, 0444, trip_point_type_show, NULL),
239         __ATTR(trip_point_8_temp, 0444, trip_point_temp_show, NULL),
240         __ATTR(trip_point_9_type, 0444, trip_point_type_show, NULL),
241         __ATTR(trip_point_9_temp, 0444, trip_point_temp_show, NULL),
242         __ATTR(trip_point_10_type, 0444, trip_point_type_show, NULL),
243         __ATTR(trip_point_10_temp, 0444, trip_point_temp_show, NULL),
244         __ATTR(trip_point_11_type, 0444, trip_point_type_show, NULL),
245         __ATTR(trip_point_11_temp, 0444, trip_point_temp_show, NULL),
246 };
247
248 #define TRIP_POINT_ATTR_ADD(_dev, _index, result)     \
249 do {    \
250         result = device_create_file(_dev,       \
251                                 &trip_point_attrs[_index * 2]); \
252         if (result)     \
253                 break;  \
254         result = device_create_file(_dev,       \
255                         &trip_point_attrs[_index * 2 + 1]);     \
256 } while (0)
257
258 #define TRIP_POINT_ATTR_REMOVE(_dev, _index)    \
259 do {    \
260         device_remove_file(_dev, &trip_point_attrs[_index * 2]);        \
261         device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]);    \
262 } while (0)
263
264 /* sys I/F for cooling device */
265 #define to_cooling_device(_dev) \
266         container_of(_dev, struct thermal_cooling_device, device)
267
268 static ssize_t
269 thermal_cooling_device_type_show(struct device *dev,
270                                  struct device_attribute *attr, char *buf)
271 {
272         struct thermal_cooling_device *cdev = to_cooling_device(dev);
273
274         return sprintf(buf, "%s\n", cdev->type);
275 }
276
277 static ssize_t
278 thermal_cooling_device_max_state_show(struct device *dev,
279                                       struct device_attribute *attr, char *buf)
280 {
281         struct thermal_cooling_device *cdev = to_cooling_device(dev);
282         unsigned long state;
283         int ret;
284
285         ret = cdev->ops->get_max_state(cdev, &state);
286         if (ret)
287                 return ret;
288         return sprintf(buf, "%ld\n", state);
289 }
290
291 static ssize_t
292 thermal_cooling_device_cur_state_show(struct device *dev,
293                                       struct device_attribute *attr, char *buf)
294 {
295         struct thermal_cooling_device *cdev = to_cooling_device(dev);
296         unsigned long state;
297         int ret;
298
299         ret = cdev->ops->get_cur_state(cdev, &state);
300         if (ret)
301                 return ret;
302         return sprintf(buf, "%ld\n", state);
303 }
304
305 static ssize_t
306 thermal_cooling_device_cur_state_store(struct device *dev,
307                                        struct device_attribute *attr,
308                                        const char *buf, size_t count)
309 {
310         struct thermal_cooling_device *cdev = to_cooling_device(dev);
311         unsigned long state;
312         int result;
313
314         if (!sscanf(buf, "%ld\n", &state))
315                 return -EINVAL;
316
317         if (state < 0)
318                 return -EINVAL;
319
320         result = cdev->ops->set_cur_state(cdev, state);
321         if (result)
322                 return result;
323         return count;
324 }
325
326 static struct device_attribute dev_attr_cdev_type =
327 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
328 static DEVICE_ATTR(max_state, 0444,
329                    thermal_cooling_device_max_state_show, NULL);
330 static DEVICE_ATTR(cur_state, 0644,
331                    thermal_cooling_device_cur_state_show,
332                    thermal_cooling_device_cur_state_store);
333
334 static ssize_t
335 thermal_cooling_device_trip_point_show(struct device *dev,
336                                        struct device_attribute *attr, char *buf)
337 {
338         struct thermal_cooling_device_instance *instance;
339
340         instance =
341             container_of(attr, struct thermal_cooling_device_instance, attr);
342
343         if (instance->trip == THERMAL_TRIPS_NONE)
344                 return sprintf(buf, "-1\n");
345         else
346                 return sprintf(buf, "%d\n", instance->trip);
347 }
348
349 /* Device management */
350
351 #if defined(CONFIG_THERMAL_HWMON)
352
353 /* hwmon sys I/F */
354 #include <linux/hwmon.h>
355 static LIST_HEAD(thermal_hwmon_list);
356
357 static ssize_t
358 name_show(struct device *dev, struct device_attribute *attr, char *buf)
359 {
360         struct thermal_hwmon_device *hwmon = dev->driver_data;
361         return sprintf(buf, "%s\n", hwmon->type);
362 }
363 static DEVICE_ATTR(name, 0444, name_show, NULL);
364
365 static ssize_t
366 temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
367 {
368         long temperature;
369         int ret;
370         struct thermal_hwmon_attr *hwmon_attr
371                         = container_of(attr, struct thermal_hwmon_attr, attr);
372         struct thermal_zone_device *tz
373                         = container_of(hwmon_attr, struct thermal_zone_device,
374                                        temp_input);
375
376         ret = tz->ops->get_temp(tz, &temperature);
377
378         if (ret)
379                 return ret;
380
381         return sprintf(buf, "%ld\n", temperature);
382 }
383
384 static ssize_t
385 temp_crit_show(struct device *dev, struct device_attribute *attr,
386                 char *buf)
387 {
388         struct thermal_hwmon_attr *hwmon_attr
389                         = container_of(attr, struct thermal_hwmon_attr, attr);
390         struct thermal_zone_device *tz
391                         = container_of(hwmon_attr, struct thermal_zone_device,
392                                        temp_crit);
393         long temperature;
394         int ret;
395
396         ret = tz->ops->get_trip_temp(tz, 0, &temperature);
397         if (ret)
398                 return ret;
399
400         return sprintf(buf, "%ld\n", temperature);
401 }
402
403
404 static int
405 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
406 {
407         struct thermal_hwmon_device *hwmon;
408         int new_hwmon_device = 1;
409         int result;
410
411         mutex_lock(&thermal_list_lock);
412         list_for_each_entry(hwmon, &thermal_hwmon_list, node)
413                 if (!strcmp(hwmon->type, tz->type)) {
414                         new_hwmon_device = 0;
415                         mutex_unlock(&thermal_list_lock);
416                         goto register_sys_interface;
417                 }
418         mutex_unlock(&thermal_list_lock);
419
420         hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
421         if (!hwmon)
422                 return -ENOMEM;
423
424         INIT_LIST_HEAD(&hwmon->tz_list);
425         strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
426         hwmon->device = hwmon_device_register(NULL);
427         if (IS_ERR(hwmon->device)) {
428                 result = PTR_ERR(hwmon->device);
429                 goto free_mem;
430         }
431         hwmon->device->driver_data = hwmon;
432         result = device_create_file(hwmon->device, &dev_attr_name);
433         if (result)
434                 goto unregister_hwmon_device;
435
436  register_sys_interface:
437         tz->hwmon = hwmon;
438         hwmon->count++;
439
440         snprintf(tz->temp_input.name, THERMAL_NAME_LENGTH,
441                  "temp%d_input", hwmon->count);
442         tz->temp_input.attr.attr.name = tz->temp_input.name;
443         tz->temp_input.attr.attr.mode = 0444;
444         tz->temp_input.attr.show = temp_input_show;
445         result = device_create_file(hwmon->device, &tz->temp_input.attr);
446         if (result)
447                 goto unregister_hwmon_device;
448
449         if (tz->ops->get_crit_temp) {
450                 unsigned long temperature;
451                 if (!tz->ops->get_crit_temp(tz, &temperature)) {
452                         snprintf(tz->temp_crit.name, THERMAL_NAME_LENGTH,
453                                 "temp%d_crit", hwmon->count);
454                         tz->temp_crit.attr.attr.name = tz->temp_crit.name;
455                         tz->temp_crit.attr.attr.mode = 0444;
456                         tz->temp_crit.attr.show = temp_crit_show;
457                         result = device_create_file(hwmon->device,
458                                                     &tz->temp_crit.attr);
459                         if (result)
460                                 goto unregister_hwmon_device;
461                 }
462         }
463
464         mutex_lock(&thermal_list_lock);
465         if (new_hwmon_device)
466                 list_add_tail(&hwmon->node, &thermal_hwmon_list);
467         list_add_tail(&tz->hwmon_node, &hwmon->tz_list);
468         mutex_unlock(&thermal_list_lock);
469
470         return 0;
471
472  unregister_hwmon_device:
473         device_remove_file(hwmon->device, &tz->temp_crit.attr);
474         device_remove_file(hwmon->device, &tz->temp_input.attr);
475         if (new_hwmon_device) {
476                 device_remove_file(hwmon->device, &dev_attr_name);
477                 hwmon_device_unregister(hwmon->device);
478         }
479  free_mem:
480         if (new_hwmon_device)
481                 kfree(hwmon);
482
483         return result;
484 }
485
486 static void
487 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
488 {
489         struct thermal_hwmon_device *hwmon = tz->hwmon;
490
491         tz->hwmon = NULL;
492         device_remove_file(hwmon->device, &tz->temp_input.attr);
493         device_remove_file(hwmon->device, &tz->temp_crit.attr);
494
495         mutex_lock(&thermal_list_lock);
496         list_del(&tz->hwmon_node);
497         if (!list_empty(&hwmon->tz_list)) {
498                 mutex_unlock(&thermal_list_lock);
499                 return;
500         }
501         list_del(&hwmon->node);
502         mutex_unlock(&thermal_list_lock);
503
504         device_remove_file(hwmon->device, &dev_attr_name);
505         hwmon_device_unregister(hwmon->device);
506         kfree(hwmon);
507 }
508 #else
509 static int
510 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
511 {
512         return 0;
513 }
514
515 static void
516 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
517 {
518 }
519 #endif
520
521 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
522                                             int delay)
523 {
524         cancel_delayed_work(&(tz->poll_queue));
525
526         if (!delay)
527                 return;
528
529         if (delay > 1000)
530                 schedule_delayed_work(&(tz->poll_queue),
531                                       round_jiffies(msecs_to_jiffies(delay)));
532         else
533                 schedule_delayed_work(&(tz->poll_queue),
534                                       msecs_to_jiffies(delay));
535 }
536
537 static void thermal_zone_device_passive(struct thermal_zone_device *tz,
538                                         int temp, int trip_temp, int trip)
539 {
540         int trend = 0;
541         struct thermal_cooling_device_instance *instance;
542         struct thermal_cooling_device *cdev;
543         long state, max_state;
544
545         /*
546          * Above Trip?
547          * -----------
548          * Calculate the thermal trend (using the passive cooling equation)
549          * and modify the performance limit for all passive cooling devices
550          * accordingly.  Note that we assume symmetry.
551          */
552         if (temp >= trip_temp) {
553                 tz->passive = true;
554
555                 trend = (tz->tc1 * (temp - tz->last_temperature)) +
556                         (tz->tc2 * (temp - trip_temp));
557
558                 /* Heating up? */
559                 if (trend > 0) {
560                         list_for_each_entry(instance, &tz->cooling_devices,
561                                             node) {
562                                 if (instance->trip != trip)
563                                         continue;
564                                 cdev = instance->cdev;
565                                 cdev->ops->get_cur_state(cdev, &state);
566                                 cdev->ops->get_max_state(cdev, &max_state);
567                                 if (state++ < max_state)
568                                         cdev->ops->set_cur_state(cdev, state);
569                         }
570                 } else if (trend < 0) { /* Cooling off? */
571                         list_for_each_entry(instance, &tz->cooling_devices,
572                                             node) {
573                                 if (instance->trip != trip)
574                                         continue;
575                                 cdev = instance->cdev;
576                                 cdev->ops->get_cur_state(cdev, &state);
577                                 cdev->ops->get_max_state(cdev, &max_state);
578                                 if (state > 0)
579                                         cdev->ops->set_cur_state(cdev, --state);
580                         }
581                 }
582                 return;
583         }
584
585         /*
586          * Below Trip?
587          * -----------
588          * Implement passive cooling hysteresis to slowly increase performance
589          * and avoid thrashing around the passive trip point.  Note that we
590          * assume symmetry.
591          */
592         list_for_each_entry(instance, &tz->cooling_devices, node) {
593                 if (instance->trip != trip)
594                         continue;
595                 cdev = instance->cdev;
596                 cdev->ops->get_cur_state(cdev, &state);
597                 cdev->ops->get_max_state(cdev, &max_state);
598                 if (state > 0)
599                         cdev->ops->set_cur_state(cdev, --state);
600                 if (state == 0)
601                         tz->passive = false;
602         }
603 }
604
605 static void thermal_zone_device_check(struct work_struct *work)
606 {
607         struct thermal_zone_device *tz = container_of(work, struct
608                                                       thermal_zone_device,
609                                                       poll_queue.work);
610         thermal_zone_device_update(tz);
611 }
612
613 /**
614  * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
615  * @tz:         thermal zone device
616  * @trip:       indicates which trip point the cooling devices is
617  *              associated with in this thermal zone.
618  * @cdev:       thermal cooling device
619  *
620  * This function is usually called in the thermal zone device .bind callback.
621  */
622 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
623                                      int trip,
624                                      struct thermal_cooling_device *cdev)
625 {
626         struct thermal_cooling_device_instance *dev;
627         struct thermal_cooling_device_instance *pos;
628         struct thermal_zone_device *pos1;
629         struct thermal_cooling_device *pos2;
630         int result;
631
632         if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
633                 return -EINVAL;
634
635         list_for_each_entry(pos1, &thermal_tz_list, node) {
636                 if (pos1 == tz)
637                         break;
638         }
639         list_for_each_entry(pos2, &thermal_cdev_list, node) {
640                 if (pos2 == cdev)
641                         break;
642         }
643
644         if (tz != pos1 || cdev != pos2)
645                 return -EINVAL;
646
647         dev =
648             kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL);
649         if (!dev)
650                 return -ENOMEM;
651         dev->tz = tz;
652         dev->cdev = cdev;
653         dev->trip = trip;
654         result = get_idr(&tz->idr, &tz->lock, &dev->id);
655         if (result)
656                 goto free_mem;
657
658         sprintf(dev->name, "cdev%d", dev->id);
659         result =
660             sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
661         if (result)
662                 goto release_idr;
663
664         sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
665         dev->attr.attr.name = dev->attr_name;
666         dev->attr.attr.mode = 0444;
667         dev->attr.show = thermal_cooling_device_trip_point_show;
668         result = device_create_file(&tz->device, &dev->attr);
669         if (result)
670                 goto remove_symbol_link;
671
672         mutex_lock(&tz->lock);
673         list_for_each_entry(pos, &tz->cooling_devices, node)
674             if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
675                 result = -EEXIST;
676                 break;
677         }
678         if (!result)
679                 list_add_tail(&dev->node, &tz->cooling_devices);
680         mutex_unlock(&tz->lock);
681
682         if (!result)
683                 return 0;
684
685         device_remove_file(&tz->device, &dev->attr);
686       remove_symbol_link:
687         sysfs_remove_link(&tz->device.kobj, dev->name);
688       release_idr:
689         release_idr(&tz->idr, &tz->lock, dev->id);
690       free_mem:
691         kfree(dev);
692         return result;
693 }
694
695 EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
696
697 /**
698  * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
699  * @tz:         thermal zone device
700  * @trip:       indicates which trip point the cooling devices is
701  *              associated with in this thermal zone.
702  * @cdev:       thermal cooling device
703  *
704  * This function is usually called in the thermal zone device .unbind callback.
705  */
706 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
707                                        int trip,
708                                        struct thermal_cooling_device *cdev)
709 {
710         struct thermal_cooling_device_instance *pos, *next;
711
712         mutex_lock(&tz->lock);
713         list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
714                 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
715                         list_del(&pos->node);
716                         mutex_unlock(&tz->lock);
717                         goto unbind;
718                 }
719         }
720         mutex_unlock(&tz->lock);
721
722         return -ENODEV;
723
724       unbind:
725         device_remove_file(&tz->device, &pos->attr);
726         sysfs_remove_link(&tz->device.kobj, pos->name);
727         release_idr(&tz->idr, &tz->lock, pos->id);
728         kfree(pos);
729         return 0;
730 }
731
732 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
733
734 static void thermal_release(struct device *dev)
735 {
736         struct thermal_zone_device *tz;
737         struct thermal_cooling_device *cdev;
738
739         if (!strncmp(dev_name(dev), "thermal_zone", sizeof "thermal_zone" - 1)) {
740                 tz = to_thermal_zone(dev);
741                 kfree(tz);
742         } else {
743                 cdev = to_cooling_device(dev);
744                 kfree(cdev);
745         }
746 }
747
748 static struct class thermal_class = {
749         .name = "thermal",
750         .dev_release = thermal_release,
751 };
752
753 /**
754  * thermal_cooling_device_register - register a new thermal cooling device
755  * @type:       the thermal cooling device type.
756  * @devdata:    device private data.
757  * @ops:                standard thermal cooling devices callbacks.
758  */
759 struct thermal_cooling_device *thermal_cooling_device_register(char *type,
760                                                                void *devdata,
761                                                                struct
762                                                                thermal_cooling_device_ops
763                                                                *ops)
764 {
765         struct thermal_cooling_device *cdev;
766         struct thermal_zone_device *pos;
767         int result;
768
769         if (strlen(type) >= THERMAL_NAME_LENGTH)
770                 return ERR_PTR(-EINVAL);
771
772         if (!ops || !ops->get_max_state || !ops->get_cur_state ||
773             !ops->set_cur_state)
774                 return ERR_PTR(-EINVAL);
775
776         cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
777         if (!cdev)
778                 return ERR_PTR(-ENOMEM);
779
780         result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
781         if (result) {
782                 kfree(cdev);
783                 return ERR_PTR(result);
784         }
785
786         strcpy(cdev->type, type);
787         cdev->ops = ops;
788         cdev->device.class = &thermal_class;
789         cdev->devdata = devdata;
790         dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
791         result = device_register(&cdev->device);
792         if (result) {
793                 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
794                 kfree(cdev);
795                 return ERR_PTR(result);
796         }
797
798         /* sys I/F */
799         if (type) {
800                 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
801                 if (result)
802                         goto unregister;
803         }
804
805         result = device_create_file(&cdev->device, &dev_attr_max_state);
806         if (result)
807                 goto unregister;
808
809         result = device_create_file(&cdev->device, &dev_attr_cur_state);
810         if (result)
811                 goto unregister;
812
813         mutex_lock(&thermal_list_lock);
814         list_add(&cdev->node, &thermal_cdev_list);
815         list_for_each_entry(pos, &thermal_tz_list, node) {
816                 if (!pos->ops->bind)
817                         continue;
818                 result = pos->ops->bind(pos, cdev);
819                 if (result)
820                         break;
821
822         }
823         mutex_unlock(&thermal_list_lock);
824
825         if (!result)
826                 return cdev;
827
828       unregister:
829         release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
830         device_unregister(&cdev->device);
831         return ERR_PTR(result);
832 }
833
834 EXPORT_SYMBOL(thermal_cooling_device_register);
835
836 /**
837  * thermal_cooling_device_unregister - removes the registered thermal cooling device
838  * @cdev:       the thermal cooling device to remove.
839  *
840  * thermal_cooling_device_unregister() must be called when the device is no
841  * longer needed.
842  */
843 void thermal_cooling_device_unregister(struct
844                                        thermal_cooling_device
845                                        *cdev)
846 {
847         struct thermal_zone_device *tz;
848         struct thermal_cooling_device *pos = NULL;
849
850         if (!cdev)
851                 return;
852
853         mutex_lock(&thermal_list_lock);
854         list_for_each_entry(pos, &thermal_cdev_list, node)
855             if (pos == cdev)
856                 break;
857         if (pos != cdev) {
858                 /* thermal cooling device not found */
859                 mutex_unlock(&thermal_list_lock);
860                 return;
861         }
862         list_del(&cdev->node);
863         list_for_each_entry(tz, &thermal_tz_list, node) {
864                 if (!tz->ops->unbind)
865                         continue;
866                 tz->ops->unbind(tz, cdev);
867         }
868         mutex_unlock(&thermal_list_lock);
869         if (cdev->type[0])
870                 device_remove_file(&cdev->device, &dev_attr_cdev_type);
871         device_remove_file(&cdev->device, &dev_attr_max_state);
872         device_remove_file(&cdev->device, &dev_attr_cur_state);
873
874         release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
875         device_unregister(&cdev->device);
876         return;
877 }
878
879 EXPORT_SYMBOL(thermal_cooling_device_unregister);
880
881 /**
882  * thermal_zone_device_update - force an update of a thermal zone's state
883  * @ttz:        the thermal zone to update
884  */
885
886 void thermal_zone_device_update(struct thermal_zone_device *tz)
887 {
888         int count, ret = 0;
889         long temp, trip_temp;
890         enum thermal_trip_type trip_type;
891         struct thermal_cooling_device_instance *instance;
892         struct thermal_cooling_device *cdev;
893
894         mutex_lock(&tz->lock);
895
896         tz->ops->get_temp(tz, &temp);
897
898         for (count = 0; count < tz->trips; count++) {
899                 tz->ops->get_trip_type(tz, count, &trip_type);
900                 tz->ops->get_trip_temp(tz, count, &trip_temp);
901
902                 switch (trip_type) {
903                 case THERMAL_TRIP_CRITICAL:
904                         if (temp > trip_temp) {
905                                 if (tz->ops->notify)
906                                         ret = tz->ops->notify(tz, count,
907                                                               trip_type);
908                                 if (!ret) {
909                                         printk(KERN_EMERG
910                                                "Critical temperature reached (%ld C), shutting down.\n",
911                                                temp/1000);
912                                         orderly_poweroff(true);
913                                 }
914                         }
915                         break;
916                 case THERMAL_TRIP_HOT:
917                         if (temp > trip_temp)
918                                 if (tz->ops->notify)
919                                         tz->ops->notify(tz, count, trip_type);
920                         break;
921                 case THERMAL_TRIP_ACTIVE:
922                         list_for_each_entry(instance, &tz->cooling_devices,
923                                             node) {
924                                 if (instance->trip != count)
925                                         continue;
926
927                                 cdev = instance->cdev;
928
929                                 if (temp > trip_temp)
930                                         cdev->ops->set_cur_state(cdev, 1);
931                                 else
932                                         cdev->ops->set_cur_state(cdev, 0);
933                         }
934                         break;
935                 case THERMAL_TRIP_PASSIVE:
936                         if (temp > trip_temp || tz->passive)
937                                 thermal_zone_device_passive(tz, temp,
938                                                             trip_temp, count);
939                         break;
940                 }
941         }
942         tz->last_temperature = temp;
943         if (tz->passive)
944                 thermal_zone_device_set_polling(tz, tz->passive_delay);
945         else if (tz->polling_delay)
946                 thermal_zone_device_set_polling(tz, tz->polling_delay);
947         mutex_unlock(&tz->lock);
948 }
949 EXPORT_SYMBOL(thermal_zone_device_update);
950
951 /**
952  * thermal_zone_device_register - register a new thermal zone device
953  * @type:       the thermal zone device type
954  * @trips:      the number of trip points the thermal zone support
955  * @devdata:    private device data
956  * @ops:        standard thermal zone device callbacks
957  * @tc1:        thermal coefficient 1 for passive calculations
958  * @tc2:        thermal coefficient 2 for passive calculations
959  * @passive_delay: number of milliseconds to wait between polls when
960  *                 performing passive cooling
961  * @polling_delay: number of milliseconds to wait between polls when checking
962  *                 whether trip points have been crossed (0 for interrupt
963  *                 driven systems)
964  *
965  * thermal_zone_device_unregister() must be called when the device is no
966  * longer needed. The passive cooling formula uses tc1 and tc2 as described in
967  * section 11.1.5.1 of the ACPI specification 3.0.
968  */
969 struct thermal_zone_device *thermal_zone_device_register(char *type,
970                                                          int trips,
971                                                          void *devdata, struct
972                                                          thermal_zone_device_ops
973                                                          *ops, int tc1, int
974                                                          tc2,
975                                                          int passive_delay,
976                                                          int polling_delay)
977 {
978         struct thermal_zone_device *tz;
979         struct thermal_cooling_device *pos;
980         int result;
981         int count;
982
983         if (strlen(type) >= THERMAL_NAME_LENGTH)
984                 return ERR_PTR(-EINVAL);
985
986         if (trips > THERMAL_MAX_TRIPS || trips < 0)
987                 return ERR_PTR(-EINVAL);
988
989         if (!ops || !ops->get_temp)
990                 return ERR_PTR(-EINVAL);
991
992         tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
993         if (!tz)
994                 return ERR_PTR(-ENOMEM);
995
996         INIT_LIST_HEAD(&tz->cooling_devices);
997         idr_init(&tz->idr);
998         mutex_init(&tz->lock);
999         result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1000         if (result) {
1001                 kfree(tz);
1002                 return ERR_PTR(result);
1003         }
1004
1005         strcpy(tz->type, type);
1006         tz->ops = ops;
1007         tz->device.class = &thermal_class;
1008         tz->devdata = devdata;
1009         tz->trips = trips;
1010         tz->tc1 = tc1;
1011         tz->tc2 = tc2;
1012         tz->passive_delay = passive_delay;
1013         tz->polling_delay = polling_delay;
1014
1015         dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1016         result = device_register(&tz->device);
1017         if (result) {
1018                 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1019                 kfree(tz);
1020                 return ERR_PTR(result);
1021         }
1022
1023         /* sys I/F */
1024         if (type) {
1025                 result = device_create_file(&tz->device, &dev_attr_type);
1026                 if (result)
1027                         goto unregister;
1028         }
1029
1030         result = device_create_file(&tz->device, &dev_attr_temp);
1031         if (result)
1032                 goto unregister;
1033
1034         if (ops->get_mode) {
1035                 result = device_create_file(&tz->device, &dev_attr_mode);
1036                 if (result)
1037                         goto unregister;
1038         }
1039
1040         for (count = 0; count < trips; count++) {
1041                 TRIP_POINT_ATTR_ADD(&tz->device, count, result);
1042                 if (result)
1043                         goto unregister;
1044         }
1045
1046         result = thermal_add_hwmon_sysfs(tz);
1047         if (result)
1048                 goto unregister;
1049
1050         mutex_lock(&thermal_list_lock);
1051         list_add_tail(&tz->node, &thermal_tz_list);
1052         if (ops->bind)
1053                 list_for_each_entry(pos, &thermal_cdev_list, node) {
1054                 result = ops->bind(tz, pos);
1055                 if (result)
1056                         break;
1057                 }
1058         mutex_unlock(&thermal_list_lock);
1059
1060         INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1061
1062         thermal_zone_device_update(tz);
1063
1064         if (!result)
1065                 return tz;
1066
1067       unregister:
1068         release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1069         device_unregister(&tz->device);
1070         return ERR_PTR(result);
1071 }
1072
1073 EXPORT_SYMBOL(thermal_zone_device_register);
1074
1075 /**
1076  * thermal_device_unregister - removes the registered thermal zone device
1077  * @tz: the thermal zone device to remove
1078  */
1079 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1080 {
1081         struct thermal_cooling_device *cdev;
1082         struct thermal_zone_device *pos = NULL;
1083         int count;
1084
1085         if (!tz)
1086                 return;
1087
1088         mutex_lock(&thermal_list_lock);
1089         list_for_each_entry(pos, &thermal_tz_list, node)
1090             if (pos == tz)
1091                 break;
1092         if (pos != tz) {
1093                 /* thermal zone device not found */
1094                 mutex_unlock(&thermal_list_lock);
1095                 return;
1096         }
1097         list_del(&tz->node);
1098         if (tz->ops->unbind)
1099                 list_for_each_entry(cdev, &thermal_cdev_list, node)
1100                     tz->ops->unbind(tz, cdev);
1101         mutex_unlock(&thermal_list_lock);
1102
1103         thermal_zone_device_set_polling(tz, 0);
1104
1105         if (tz->type[0])
1106                 device_remove_file(&tz->device, &dev_attr_type);
1107         device_remove_file(&tz->device, &dev_attr_temp);
1108         if (tz->ops->get_mode)
1109                 device_remove_file(&tz->device, &dev_attr_mode);
1110
1111         for (count = 0; count < tz->trips; count++)
1112                 TRIP_POINT_ATTR_REMOVE(&tz->device, count);
1113
1114         thermal_remove_hwmon_sysfs(tz);
1115         release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1116         idr_destroy(&tz->idr);
1117         mutex_destroy(&tz->lock);
1118         device_unregister(&tz->device);
1119         return;
1120 }
1121
1122 EXPORT_SYMBOL(thermal_zone_device_unregister);
1123
1124 static int __init thermal_init(void)
1125 {
1126         int result = 0;
1127
1128         result = class_register(&thermal_class);
1129         if (result) {
1130                 idr_destroy(&thermal_tz_idr);
1131                 idr_destroy(&thermal_cdev_idr);
1132                 mutex_destroy(&thermal_idr_lock);
1133                 mutex_destroy(&thermal_list_lock);
1134         }
1135         return result;
1136 }
1137
1138 static void __exit thermal_exit(void)
1139 {
1140         class_unregister(&thermal_class);
1141         idr_destroy(&thermal_tz_idr);
1142         idr_destroy(&thermal_cdev_idr);
1143         mutex_destroy(&thermal_idr_lock);
1144         mutex_destroy(&thermal_list_lock);
1145 }
1146
1147 subsys_initcall(thermal_init);
1148 module_exit(thermal_exit);