blob: 663c2fdba775dc3ed6add2d6e162e4d55fdcd169 [file] [log] [blame]
Zhang Rui203d3d42008-01-17 15:51:08 +08001/*
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>
Matthew Garrettb1569e92008-12-03 17:55:32 +000033#include <linux/reboot.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080034
Zhang Rui63c4ec92008-04-21 16:07:13 +080035MODULE_AUTHOR("Zhang Rui");
Zhang Rui203d3d42008-01-17 15:51:08 +080036MODULE_DESCRIPTION("Generic thermal management sysfs support");
37MODULE_LICENSE("GPL");
38
39#define PREFIX "Thermal: "
40
41struct 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
52static DEFINE_IDR(thermal_tz_idr);
53static DEFINE_IDR(thermal_cdev_idr);
54static DEFINE_MUTEX(thermal_idr_lock);
55
56static LIST_HEAD(thermal_tz_list);
57static LIST_HEAD(thermal_cdev_list);
58static DEFINE_MUTEX(thermal_list_lock);
59
60static 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
82static 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
96static ssize_t
97type_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
104static ssize_t
105temp_show(struct device *dev, struct device_attribute *attr, char *buf)
106{
107 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000108 long temperature;
109 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800110
111 if (!tz->ops->get_temp)
112 return -EPERM;
113
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000114 ret = tz->ops->get_temp(tz, &temperature);
115
116 if (ret)
117 return ret;
118
119 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800120}
121
122static ssize_t
123mode_show(struct device *dev, struct device_attribute *attr, char *buf)
124{
125 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000126 enum thermal_device_mode mode;
127 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800128
129 if (!tz->ops->get_mode)
130 return -EPERM;
131
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000132 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");
Zhang Rui203d3d42008-01-17 15:51:08 +0800138}
139
140static ssize_t
141mode_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
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000150 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
Zhang Rui203d3d42008-01-17 15:51:08 +0800157 if (result)
158 return result;
159
160 return count;
161}
162
163static ssize_t
164trip_point_type_show(struct device *dev, struct device_attribute *attr,
165 char *buf)
166{
167 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000168 enum thermal_trip_type type;
169 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800170
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
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000177 result = tz->ops->get_trip_type(tz, trip, &type);
178 if (result)
179 return result;
180
181 switch (type) {
182 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300183 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000184 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300185 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000186 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300187 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000188 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300189 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000190 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300191 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000192 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800193}
194
195static ssize_t
196trip_point_temp_show(struct device *dev, struct device_attribute *attr,
197 char *buf)
198{
199 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000200 int trip, ret;
201 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800202
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
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000209 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
210
211 if (ret)
212 return ret;
213
214 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800215}
216
Matthew Garrett03a971a2008-12-03 18:00:38 +0000217static ssize_t
218passive_store(struct device *dev, struct device_attribute *attr,
219 const char *buf, size_t count)
220{
221 struct thermal_zone_device *tz = to_thermal_zone(dev);
222 struct thermal_cooling_device *cdev = NULL;
223 int state;
224
225 if (!sscanf(buf, "%d\n", &state))
226 return -EINVAL;
227
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100228 /* sanity check: values below 1000 millicelcius don't make sense
229 * and can cause the system to go into a thermal heart attack
230 */
231 if (state && state < 1000)
232 return -EINVAL;
233
Matthew Garrett03a971a2008-12-03 18:00:38 +0000234 if (state && !tz->forced_passive) {
235 mutex_lock(&thermal_list_lock);
236 list_for_each_entry(cdev, &thermal_cdev_list, node) {
237 if (!strncmp("Processor", cdev->type,
238 sizeof("Processor")))
239 thermal_zone_bind_cooling_device(tz,
240 THERMAL_TRIPS_NONE,
241 cdev);
242 }
243 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100244 if (!tz->passive_delay)
245 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000246 } else if (!state && tz->forced_passive) {
247 mutex_lock(&thermal_list_lock);
248 list_for_each_entry(cdev, &thermal_cdev_list, node) {
249 if (!strncmp("Processor", cdev->type,
250 sizeof("Processor")))
251 thermal_zone_unbind_cooling_device(tz,
252 THERMAL_TRIPS_NONE,
253 cdev);
254 }
255 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100256 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000257 }
258
259 tz->tc1 = 1;
260 tz->tc2 = 1;
261
Matthew Garrett03a971a2008-12-03 18:00:38 +0000262 tz->forced_passive = state;
263
264 thermal_zone_device_update(tz);
265
266 return count;
267}
268
269static ssize_t
270passive_show(struct device *dev, struct device_attribute *attr,
271 char *buf)
272{
273 struct thermal_zone_device *tz = to_thermal_zone(dev);
274
275 return sprintf(buf, "%d\n", tz->forced_passive);
276}
277
Zhang Rui203d3d42008-01-17 15:51:08 +0800278static DEVICE_ATTR(type, 0444, type_show, NULL);
279static DEVICE_ATTR(temp, 0444, temp_show, NULL);
280static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000281static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, \
282 passive_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800283
284static struct device_attribute trip_point_attrs[] = {
285 __ATTR(trip_point_0_type, 0444, trip_point_type_show, NULL),
286 __ATTR(trip_point_0_temp, 0444, trip_point_temp_show, NULL),
287 __ATTR(trip_point_1_type, 0444, trip_point_type_show, NULL),
288 __ATTR(trip_point_1_temp, 0444, trip_point_temp_show, NULL),
289 __ATTR(trip_point_2_type, 0444, trip_point_type_show, NULL),
290 __ATTR(trip_point_2_temp, 0444, trip_point_temp_show, NULL),
291 __ATTR(trip_point_3_type, 0444, trip_point_type_show, NULL),
292 __ATTR(trip_point_3_temp, 0444, trip_point_temp_show, NULL),
293 __ATTR(trip_point_4_type, 0444, trip_point_type_show, NULL),
294 __ATTR(trip_point_4_temp, 0444, trip_point_temp_show, NULL),
295 __ATTR(trip_point_5_type, 0444, trip_point_type_show, NULL),
296 __ATTR(trip_point_5_temp, 0444, trip_point_temp_show, NULL),
297 __ATTR(trip_point_6_type, 0444, trip_point_type_show, NULL),
298 __ATTR(trip_point_6_temp, 0444, trip_point_temp_show, NULL),
299 __ATTR(trip_point_7_type, 0444, trip_point_type_show, NULL),
300 __ATTR(trip_point_7_temp, 0444, trip_point_temp_show, NULL),
301 __ATTR(trip_point_8_type, 0444, trip_point_type_show, NULL),
302 __ATTR(trip_point_8_temp, 0444, trip_point_temp_show, NULL),
303 __ATTR(trip_point_9_type, 0444, trip_point_type_show, NULL),
304 __ATTR(trip_point_9_temp, 0444, trip_point_temp_show, NULL),
Krzysztof Helt5f1a3f22008-04-15 14:34:47 -0700305 __ATTR(trip_point_10_type, 0444, trip_point_type_show, NULL),
306 __ATTR(trip_point_10_temp, 0444, trip_point_temp_show, NULL),
307 __ATTR(trip_point_11_type, 0444, trip_point_type_show, NULL),
308 __ATTR(trip_point_11_temp, 0444, trip_point_temp_show, NULL),
Zhang Rui203d3d42008-01-17 15:51:08 +0800309};
310
311#define TRIP_POINT_ATTR_ADD(_dev, _index, result) \
312do { \
313 result = device_create_file(_dev, \
314 &trip_point_attrs[_index * 2]); \
315 if (result) \
316 break; \
317 result = device_create_file(_dev, \
318 &trip_point_attrs[_index * 2 + 1]); \
319} while (0)
320
321#define TRIP_POINT_ATTR_REMOVE(_dev, _index) \
322do { \
323 device_remove_file(_dev, &trip_point_attrs[_index * 2]); \
324 device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \
325} while (0)
326
327/* sys I/F for cooling device */
328#define to_cooling_device(_dev) \
329 container_of(_dev, struct thermal_cooling_device, device)
330
331static ssize_t
332thermal_cooling_device_type_show(struct device *dev,
333 struct device_attribute *attr, char *buf)
334{
335 struct thermal_cooling_device *cdev = to_cooling_device(dev);
336
337 return sprintf(buf, "%s\n", cdev->type);
338}
339
340static ssize_t
341thermal_cooling_device_max_state_show(struct device *dev,
342 struct device_attribute *attr, char *buf)
343{
344 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000345 unsigned long state;
346 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800347
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000348 ret = cdev->ops->get_max_state(cdev, &state);
349 if (ret)
350 return ret;
351 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800352}
353
354static ssize_t
355thermal_cooling_device_cur_state_show(struct device *dev,
356 struct device_attribute *attr, char *buf)
357{
358 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000359 unsigned long state;
360 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800361
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000362 ret = cdev->ops->get_cur_state(cdev, &state);
363 if (ret)
364 return ret;
365 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800366}
367
368static ssize_t
369thermal_cooling_device_cur_state_store(struct device *dev,
370 struct device_attribute *attr,
371 const char *buf, size_t count)
372{
373 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000374 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800375 int result;
376
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000377 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800378 return -EINVAL;
379
380 if (state < 0)
381 return -EINVAL;
382
383 result = cdev->ops->set_cur_state(cdev, state);
384 if (result)
385 return result;
386 return count;
387}
388
389static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500390__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800391static DEVICE_ATTR(max_state, 0444,
392 thermal_cooling_device_max_state_show, NULL);
393static DEVICE_ATTR(cur_state, 0644,
394 thermal_cooling_device_cur_state_show,
395 thermal_cooling_device_cur_state_store);
396
397static ssize_t
398thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500399 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800400{
401 struct thermal_cooling_device_instance *instance;
402
403 instance =
404 container_of(attr, struct thermal_cooling_device_instance, attr);
405
406 if (instance->trip == THERMAL_TRIPS_NONE)
407 return sprintf(buf, "-1\n");
408 else
409 return sprintf(buf, "%d\n", instance->trip);
410}
411
412/* Device management */
413
Rene Herman16d75232008-06-24 19:38:56 +0200414#if defined(CONFIG_THERMAL_HWMON)
415
Zhang Ruie68b16a2008-04-21 16:07:52 +0800416/* hwmon sys I/F */
417#include <linux/hwmon.h>
418static LIST_HEAD(thermal_hwmon_list);
419
420static ssize_t
421name_show(struct device *dev, struct device_attribute *attr, char *buf)
422{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700423 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800424 return sprintf(buf, "%s\n", hwmon->type);
425}
426static DEVICE_ATTR(name, 0444, name_show, NULL);
427
428static ssize_t
429temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
430{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000431 long temperature;
432 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800433 struct thermal_hwmon_attr *hwmon_attr
434 = container_of(attr, struct thermal_hwmon_attr, attr);
435 struct thermal_zone_device *tz
436 = container_of(hwmon_attr, struct thermal_zone_device,
437 temp_input);
438
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000439 ret = tz->ops->get_temp(tz, &temperature);
440
441 if (ret)
442 return ret;
443
444 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800445}
446
447static ssize_t
448temp_crit_show(struct device *dev, struct device_attribute *attr,
449 char *buf)
450{
451 struct thermal_hwmon_attr *hwmon_attr
452 = container_of(attr, struct thermal_hwmon_attr, attr);
453 struct thermal_zone_device *tz
454 = container_of(hwmon_attr, struct thermal_zone_device,
455 temp_crit);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000456 long temperature;
457 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800458
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000459 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
460 if (ret)
461 return ret;
462
463 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800464}
465
466
467static int
468thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
469{
470 struct thermal_hwmon_device *hwmon;
471 int new_hwmon_device = 1;
472 int result;
473
474 mutex_lock(&thermal_list_lock);
475 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
476 if (!strcmp(hwmon->type, tz->type)) {
477 new_hwmon_device = 0;
478 mutex_unlock(&thermal_list_lock);
479 goto register_sys_interface;
480 }
481 mutex_unlock(&thermal_list_lock);
482
483 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
484 if (!hwmon)
485 return -ENOMEM;
486
487 INIT_LIST_HEAD(&hwmon->tz_list);
488 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
489 hwmon->device = hwmon_device_register(NULL);
490 if (IS_ERR(hwmon->device)) {
491 result = PTR_ERR(hwmon->device);
492 goto free_mem;
493 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700494 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800495 result = device_create_file(hwmon->device, &dev_attr_name);
496 if (result)
497 goto unregister_hwmon_device;
498
499 register_sys_interface:
500 tz->hwmon = hwmon;
501 hwmon->count++;
502
503 snprintf(tz->temp_input.name, THERMAL_NAME_LENGTH,
504 "temp%d_input", hwmon->count);
505 tz->temp_input.attr.attr.name = tz->temp_input.name;
506 tz->temp_input.attr.attr.mode = 0444;
507 tz->temp_input.attr.show = temp_input_show;
508 result = device_create_file(hwmon->device, &tz->temp_input.attr);
509 if (result)
510 goto unregister_hwmon_device;
511
512 if (tz->ops->get_crit_temp) {
513 unsigned long temperature;
514 if (!tz->ops->get_crit_temp(tz, &temperature)) {
515 snprintf(tz->temp_crit.name, THERMAL_NAME_LENGTH,
516 "temp%d_crit", hwmon->count);
517 tz->temp_crit.attr.attr.name = tz->temp_crit.name;
518 tz->temp_crit.attr.attr.mode = 0444;
519 tz->temp_crit.attr.show = temp_crit_show;
520 result = device_create_file(hwmon->device,
521 &tz->temp_crit.attr);
522 if (result)
523 goto unregister_hwmon_device;
524 }
525 }
526
527 mutex_lock(&thermal_list_lock);
528 if (new_hwmon_device)
529 list_add_tail(&hwmon->node, &thermal_hwmon_list);
530 list_add_tail(&tz->hwmon_node, &hwmon->tz_list);
531 mutex_unlock(&thermal_list_lock);
532
533 return 0;
534
535 unregister_hwmon_device:
536 device_remove_file(hwmon->device, &tz->temp_crit.attr);
537 device_remove_file(hwmon->device, &tz->temp_input.attr);
538 if (new_hwmon_device) {
539 device_remove_file(hwmon->device, &dev_attr_name);
540 hwmon_device_unregister(hwmon->device);
541 }
542 free_mem:
543 if (new_hwmon_device)
544 kfree(hwmon);
545
546 return result;
547}
548
549static void
550thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
551{
552 struct thermal_hwmon_device *hwmon = tz->hwmon;
553
554 tz->hwmon = NULL;
555 device_remove_file(hwmon->device, &tz->temp_input.attr);
556 device_remove_file(hwmon->device, &tz->temp_crit.attr);
557
558 mutex_lock(&thermal_list_lock);
559 list_del(&tz->hwmon_node);
560 if (!list_empty(&hwmon->tz_list)) {
561 mutex_unlock(&thermal_list_lock);
562 return;
563 }
564 list_del(&hwmon->node);
565 mutex_unlock(&thermal_list_lock);
566
567 device_remove_file(hwmon->device, &dev_attr_name);
568 hwmon_device_unregister(hwmon->device);
569 kfree(hwmon);
570}
571#else
572static int
573thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
574{
575 return 0;
576}
577
578static void
579thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
580{
581}
582#endif
583
Matthew Garrettb1569e92008-12-03 17:55:32 +0000584static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
585 int delay)
586{
587 cancel_delayed_work(&(tz->poll_queue));
588
589 if (!delay)
590 return;
591
592 if (delay > 1000)
593 schedule_delayed_work(&(tz->poll_queue),
594 round_jiffies(msecs_to_jiffies(delay)));
595 else
596 schedule_delayed_work(&(tz->poll_queue),
597 msecs_to_jiffies(delay));
598}
599
600static void thermal_zone_device_passive(struct thermal_zone_device *tz,
601 int temp, int trip_temp, int trip)
602{
603 int trend = 0;
604 struct thermal_cooling_device_instance *instance;
605 struct thermal_cooling_device *cdev;
606 long state, max_state;
607
608 /*
609 * Above Trip?
610 * -----------
611 * Calculate the thermal trend (using the passive cooling equation)
612 * and modify the performance limit for all passive cooling devices
613 * accordingly. Note that we assume symmetry.
614 */
615 if (temp >= trip_temp) {
616 tz->passive = true;
617
618 trend = (tz->tc1 * (temp - tz->last_temperature)) +
619 (tz->tc2 * (temp - trip_temp));
620
621 /* Heating up? */
622 if (trend > 0) {
623 list_for_each_entry(instance, &tz->cooling_devices,
624 node) {
625 if (instance->trip != trip)
626 continue;
627 cdev = instance->cdev;
628 cdev->ops->get_cur_state(cdev, &state);
629 cdev->ops->get_max_state(cdev, &max_state);
630 if (state++ < max_state)
631 cdev->ops->set_cur_state(cdev, state);
632 }
633 } else if (trend < 0) { /* Cooling off? */
634 list_for_each_entry(instance, &tz->cooling_devices,
635 node) {
636 if (instance->trip != trip)
637 continue;
638 cdev = instance->cdev;
639 cdev->ops->get_cur_state(cdev, &state);
640 cdev->ops->get_max_state(cdev, &max_state);
641 if (state > 0)
642 cdev->ops->set_cur_state(cdev, --state);
643 }
644 }
645 return;
646 }
647
648 /*
649 * Below Trip?
650 * -----------
651 * Implement passive cooling hysteresis to slowly increase performance
652 * and avoid thrashing around the passive trip point. Note that we
653 * assume symmetry.
654 */
655 list_for_each_entry(instance, &tz->cooling_devices, node) {
656 if (instance->trip != trip)
657 continue;
658 cdev = instance->cdev;
659 cdev->ops->get_cur_state(cdev, &state);
660 cdev->ops->get_max_state(cdev, &max_state);
661 if (state > 0)
662 cdev->ops->set_cur_state(cdev, --state);
663 if (state == 0)
664 tz->passive = false;
665 }
666}
667
668static void thermal_zone_device_check(struct work_struct *work)
669{
670 struct thermal_zone_device *tz = container_of(work, struct
671 thermal_zone_device,
672 poll_queue.work);
673 thermal_zone_device_update(tz);
674}
Zhang Ruie68b16a2008-04-21 16:07:52 +0800675
Zhang Rui203d3d42008-01-17 15:51:08 +0800676/**
677 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800678 * @tz: thermal zone device
679 * @trip: indicates which trip point the cooling devices is
680 * associated with in this thermal zone.
681 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500682 *
683 * This function is usually called in the thermal zone device .bind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800684 */
685int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
686 int trip,
687 struct thermal_cooling_device *cdev)
688{
689 struct thermal_cooling_device_instance *dev;
690 struct thermal_cooling_device_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500691 struct thermal_zone_device *pos1;
692 struct thermal_cooling_device *pos2;
Zhang Rui203d3d42008-01-17 15:51:08 +0800693 int result;
694
Len Brown543a9562008-02-07 16:55:08 -0500695 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800696 return -EINVAL;
697
Thomas Sujithc7516702008-02-15 00:58:50 -0500698 list_for_each_entry(pos1, &thermal_tz_list, node) {
699 if (pos1 == tz)
700 break;
701 }
702 list_for_each_entry(pos2, &thermal_cdev_list, node) {
703 if (pos2 == cdev)
704 break;
705 }
706
707 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800708 return -EINVAL;
709
710 dev =
711 kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL);
712 if (!dev)
713 return -ENOMEM;
714 dev->tz = tz;
715 dev->cdev = cdev;
716 dev->trip = trip;
717 result = get_idr(&tz->idr, &tz->lock, &dev->id);
718 if (result)
719 goto free_mem;
720
721 sprintf(dev->name, "cdev%d", dev->id);
722 result =
723 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
724 if (result)
725 goto release_idr;
726
727 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
728 dev->attr.attr.name = dev->attr_name;
729 dev->attr.attr.mode = 0444;
730 dev->attr.show = thermal_cooling_device_trip_point_show;
731 result = device_create_file(&tz->device, &dev->attr);
732 if (result)
733 goto remove_symbol_link;
734
735 mutex_lock(&tz->lock);
736 list_for_each_entry(pos, &tz->cooling_devices, node)
737 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
738 result = -EEXIST;
739 break;
740 }
741 if (!result)
742 list_add_tail(&dev->node, &tz->cooling_devices);
743 mutex_unlock(&tz->lock);
744
745 if (!result)
746 return 0;
747
748 device_remove_file(&tz->device, &dev->attr);
749 remove_symbol_link:
750 sysfs_remove_link(&tz->device.kobj, dev->name);
751 release_idr:
752 release_idr(&tz->idr, &tz->lock, dev->id);
753 free_mem:
754 kfree(dev);
755 return result;
756}
Len Brown543a9562008-02-07 16:55:08 -0500757
Zhang Rui203d3d42008-01-17 15:51:08 +0800758EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
759
760/**
761 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800762 * @tz: thermal zone device
763 * @trip: indicates which trip point the cooling devices is
764 * associated with in this thermal zone.
765 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500766 *
767 * This function is usually called in the thermal zone device .unbind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800768 */
769int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
770 int trip,
771 struct thermal_cooling_device *cdev)
772{
773 struct thermal_cooling_device_instance *pos, *next;
774
775 mutex_lock(&tz->lock);
776 list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
Len Brown543a9562008-02-07 16:55:08 -0500777 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800778 list_del(&pos->node);
779 mutex_unlock(&tz->lock);
780 goto unbind;
781 }
782 }
783 mutex_unlock(&tz->lock);
784
785 return -ENODEV;
786
787 unbind:
788 device_remove_file(&tz->device, &pos->attr);
789 sysfs_remove_link(&tz->device.kobj, pos->name);
790 release_idr(&tz->idr, &tz->lock, pos->id);
791 kfree(pos);
792 return 0;
793}
Len Brown543a9562008-02-07 16:55:08 -0500794
Zhang Rui203d3d42008-01-17 15:51:08 +0800795EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
796
797static void thermal_release(struct device *dev)
798{
799 struct thermal_zone_device *tz;
800 struct thermal_cooling_device *cdev;
801
Kay Sievers354655e2009-01-06 10:44:37 -0800802 if (!strncmp(dev_name(dev), "thermal_zone", sizeof "thermal_zone" - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800803 tz = to_thermal_zone(dev);
804 kfree(tz);
805 } else {
806 cdev = to_cooling_device(dev);
807 kfree(cdev);
808 }
809}
810
811static struct class thermal_class = {
812 .name = "thermal",
813 .dev_release = thermal_release,
814};
815
816/**
817 * thermal_cooling_device_register - register a new thermal cooling device
818 * @type: the thermal cooling device type.
819 * @devdata: device private data.
820 * @ops: standard thermal cooling devices callbacks.
821 */
822struct thermal_cooling_device *thermal_cooling_device_register(char *type,
Len Brown543a9562008-02-07 16:55:08 -0500823 void *devdata,
824 struct
825 thermal_cooling_device_ops
826 *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800827{
828 struct thermal_cooling_device *cdev;
829 struct thermal_zone_device *pos;
830 int result;
831
832 if (strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500833 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800834
835 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -0500836 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500837 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800838
839 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
840 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500841 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +0800842
843 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
844 if (result) {
845 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500846 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800847 }
848
849 strcpy(cdev->type, type);
850 cdev->ops = ops;
851 cdev->device.class = &thermal_class;
852 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -0800853 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800854 result = device_register(&cdev->device);
855 if (result) {
856 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
857 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500858 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800859 }
860
861 /* sys I/F */
862 if (type) {
Len Brown543a9562008-02-07 16:55:08 -0500863 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +0800864 if (result)
865 goto unregister;
866 }
867
868 result = device_create_file(&cdev->device, &dev_attr_max_state);
869 if (result)
870 goto unregister;
871
872 result = device_create_file(&cdev->device, &dev_attr_cur_state);
873 if (result)
874 goto unregister;
875
876 mutex_lock(&thermal_list_lock);
877 list_add(&cdev->node, &thermal_cdev_list);
878 list_for_each_entry(pos, &thermal_tz_list, node) {
879 if (!pos->ops->bind)
880 continue;
881 result = pos->ops->bind(pos, cdev);
882 if (result)
883 break;
884
885 }
886 mutex_unlock(&thermal_list_lock);
887
888 if (!result)
889 return cdev;
890
891 unregister:
892 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
893 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500894 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800895}
Len Brown543a9562008-02-07 16:55:08 -0500896
Zhang Rui203d3d42008-01-17 15:51:08 +0800897EXPORT_SYMBOL(thermal_cooling_device_register);
898
899/**
900 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +0800901 * @cdev: the thermal cooling device to remove.
902 *
903 * thermal_cooling_device_unregister() must be called when the device is no
904 * longer needed.
905 */
906void thermal_cooling_device_unregister(struct
907 thermal_cooling_device
908 *cdev)
909{
910 struct thermal_zone_device *tz;
911 struct thermal_cooling_device *pos = NULL;
912
913 if (!cdev)
914 return;
915
916 mutex_lock(&thermal_list_lock);
917 list_for_each_entry(pos, &thermal_cdev_list, node)
918 if (pos == cdev)
919 break;
920 if (pos != cdev) {
921 /* thermal cooling device not found */
922 mutex_unlock(&thermal_list_lock);
923 return;
924 }
925 list_del(&cdev->node);
926 list_for_each_entry(tz, &thermal_tz_list, node) {
927 if (!tz->ops->unbind)
928 continue;
929 tz->ops->unbind(tz, cdev);
930 }
931 mutex_unlock(&thermal_list_lock);
932 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -0500933 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +0800934 device_remove_file(&cdev->device, &dev_attr_max_state);
935 device_remove_file(&cdev->device, &dev_attr_cur_state);
936
937 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
938 device_unregister(&cdev->device);
939 return;
940}
Len Brown543a9562008-02-07 16:55:08 -0500941
Zhang Rui203d3d42008-01-17 15:51:08 +0800942EXPORT_SYMBOL(thermal_cooling_device_unregister);
943
944/**
Matthew Garrettb1569e92008-12-03 17:55:32 +0000945 * thermal_zone_device_update - force an update of a thermal zone's state
946 * @ttz: the thermal zone to update
947 */
948
949void thermal_zone_device_update(struct thermal_zone_device *tz)
950{
951 int count, ret = 0;
952 long temp, trip_temp;
953 enum thermal_trip_type trip_type;
954 struct thermal_cooling_device_instance *instance;
955 struct thermal_cooling_device *cdev;
956
957 mutex_lock(&tz->lock);
958
Michael Brunner0d288162009-08-26 14:29:25 -0700959 if (tz->ops->get_temp(tz, &temp)) {
960 /* get_temp failed - retry it later */
961 printk(KERN_WARNING PREFIX "failed to read out thermal zone "
962 "%d\n", tz->id);
963 goto leave;
964 }
Matthew Garrettb1569e92008-12-03 17:55:32 +0000965
966 for (count = 0; count < tz->trips; count++) {
967 tz->ops->get_trip_type(tz, count, &trip_type);
968 tz->ops->get_trip_temp(tz, count, &trip_temp);
969
970 switch (trip_type) {
971 case THERMAL_TRIP_CRITICAL:
Vladimir Zajac29321352009-05-06 19:34:21 +0200972 if (temp >= trip_temp) {
Matthew Garrettb1569e92008-12-03 17:55:32 +0000973 if (tz->ops->notify)
974 ret = tz->ops->notify(tz, count,
975 trip_type);
976 if (!ret) {
977 printk(KERN_EMERG
978 "Critical temperature reached (%ld C), shutting down.\n",
979 temp/1000);
980 orderly_poweroff(true);
981 }
982 }
983 break;
984 case THERMAL_TRIP_HOT:
Vladimir Zajac29321352009-05-06 19:34:21 +0200985 if (temp >= trip_temp)
Matthew Garrettb1569e92008-12-03 17:55:32 +0000986 if (tz->ops->notify)
987 tz->ops->notify(tz, count, trip_type);
988 break;
989 case THERMAL_TRIP_ACTIVE:
990 list_for_each_entry(instance, &tz->cooling_devices,
991 node) {
992 if (instance->trip != count)
993 continue;
994
995 cdev = instance->cdev;
996
Vladimir Zajac29321352009-05-06 19:34:21 +0200997 if (temp >= trip_temp)
Matthew Garrettb1569e92008-12-03 17:55:32 +0000998 cdev->ops->set_cur_state(cdev, 1);
999 else
1000 cdev->ops->set_cur_state(cdev, 0);
1001 }
1002 break;
1003 case THERMAL_TRIP_PASSIVE:
Vladimir Zajac29321352009-05-06 19:34:21 +02001004 if (temp >= trip_temp || tz->passive)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001005 thermal_zone_device_passive(tz, temp,
1006 trip_temp, count);
1007 break;
1008 }
1009 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001010
1011 if (tz->forced_passive)
1012 thermal_zone_device_passive(tz, temp, tz->forced_passive,
1013 THERMAL_TRIPS_NONE);
1014
Matthew Garrettb1569e92008-12-03 17:55:32 +00001015 tz->last_temperature = temp;
Michael Brunner0d288162009-08-26 14:29:25 -07001016
1017 leave:
Matthew Garrettb1569e92008-12-03 17:55:32 +00001018 if (tz->passive)
1019 thermal_zone_device_set_polling(tz, tz->passive_delay);
1020 else if (tz->polling_delay)
1021 thermal_zone_device_set_polling(tz, tz->polling_delay);
1022 mutex_unlock(&tz->lock);
1023}
1024EXPORT_SYMBOL(thermal_zone_device_update);
1025
1026/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001027 * thermal_zone_device_register - register a new thermal zone device
1028 * @type: the thermal zone device type
1029 * @trips: the number of trip points the thermal zone support
1030 * @devdata: private device data
1031 * @ops: standard thermal zone device callbacks
Matthew Garrettb1569e92008-12-03 17:55:32 +00001032 * @tc1: thermal coefficient 1 for passive calculations
1033 * @tc2: thermal coefficient 2 for passive calculations
1034 * @passive_delay: number of milliseconds to wait between polls when
1035 * performing passive cooling
1036 * @polling_delay: number of milliseconds to wait between polls when checking
1037 * whether trip points have been crossed (0 for interrupt
1038 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001039 *
1040 * thermal_zone_device_unregister() must be called when the device is no
Matthew Garrettb1569e92008-12-03 17:55:32 +00001041 * longer needed. The passive cooling formula uses tc1 and tc2 as described in
1042 * section 11.1.5.1 of the ACPI specification 3.0.
Zhang Rui203d3d42008-01-17 15:51:08 +08001043 */
1044struct thermal_zone_device *thermal_zone_device_register(char *type,
Len Brown543a9562008-02-07 16:55:08 -05001045 int trips,
1046 void *devdata, struct
1047 thermal_zone_device_ops
Matthew Garrettb1569e92008-12-03 17:55:32 +00001048 *ops, int tc1, int
1049 tc2,
1050 int passive_delay,
1051 int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001052{
1053 struct thermal_zone_device *tz;
1054 struct thermal_cooling_device *pos;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001055 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001056 int result;
1057 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001058 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001059
1060 if (strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001061 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001062
1063 if (trips > THERMAL_MAX_TRIPS || trips < 0)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001064 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001065
1066 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001067 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001068
1069 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1070 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001071 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001072
1073 INIT_LIST_HEAD(&tz->cooling_devices);
1074 idr_init(&tz->idr);
1075 mutex_init(&tz->lock);
1076 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1077 if (result) {
1078 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001079 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001080 }
1081
1082 strcpy(tz->type, type);
1083 tz->ops = ops;
1084 tz->device.class = &thermal_class;
1085 tz->devdata = devdata;
1086 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001087 tz->tc1 = tc1;
1088 tz->tc2 = tc2;
1089 tz->passive_delay = passive_delay;
1090 tz->polling_delay = polling_delay;
1091
Kay Sievers354655e2009-01-06 10:44:37 -08001092 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001093 result = device_register(&tz->device);
1094 if (result) {
1095 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1096 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001097 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001098 }
1099
1100 /* sys I/F */
1101 if (type) {
1102 result = device_create_file(&tz->device, &dev_attr_type);
1103 if (result)
1104 goto unregister;
1105 }
1106
1107 result = device_create_file(&tz->device, &dev_attr_temp);
1108 if (result)
1109 goto unregister;
1110
1111 if (ops->get_mode) {
1112 result = device_create_file(&tz->device, &dev_attr_mode);
1113 if (result)
1114 goto unregister;
1115 }
1116
1117 for (count = 0; count < trips; count++) {
1118 TRIP_POINT_ATTR_ADD(&tz->device, count, result);
1119 if (result)
1120 goto unregister;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001121 tz->ops->get_trip_type(tz, count, &trip_type);
1122 if (trip_type == THERMAL_TRIP_PASSIVE)
1123 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001124 }
1125
Matthew Garrett03a971a2008-12-03 18:00:38 +00001126 if (!passive)
1127 result = device_create_file(&tz->device,
1128 &dev_attr_passive);
1129
1130 if (result)
1131 goto unregister;
1132
Zhang Ruie68b16a2008-04-21 16:07:52 +08001133 result = thermal_add_hwmon_sysfs(tz);
1134 if (result)
1135 goto unregister;
1136
Zhang Rui203d3d42008-01-17 15:51:08 +08001137 mutex_lock(&thermal_list_lock);
1138 list_add_tail(&tz->node, &thermal_tz_list);
1139 if (ops->bind)
1140 list_for_each_entry(pos, &thermal_cdev_list, node) {
Len Brown543a9562008-02-07 16:55:08 -05001141 result = ops->bind(tz, pos);
1142 if (result)
1143 break;
Zhang Rui203d3d42008-01-17 15:51:08 +08001144 }
1145 mutex_unlock(&thermal_list_lock);
1146
Matthew Garrettb1569e92008-12-03 17:55:32 +00001147 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1148
1149 thermal_zone_device_update(tz);
1150
Zhang Rui203d3d42008-01-17 15:51:08 +08001151 if (!result)
1152 return tz;
1153
1154 unregister:
1155 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1156 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001157 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001158}
Len Brown543a9562008-02-07 16:55:08 -05001159
Zhang Rui203d3d42008-01-17 15:51:08 +08001160EXPORT_SYMBOL(thermal_zone_device_register);
1161
1162/**
1163 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001164 * @tz: the thermal zone device to remove
1165 */
1166void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1167{
1168 struct thermal_cooling_device *cdev;
1169 struct thermal_zone_device *pos = NULL;
1170 int count;
1171
1172 if (!tz)
1173 return;
1174
1175 mutex_lock(&thermal_list_lock);
1176 list_for_each_entry(pos, &thermal_tz_list, node)
1177 if (pos == tz)
1178 break;
1179 if (pos != tz) {
1180 /* thermal zone device not found */
1181 mutex_unlock(&thermal_list_lock);
1182 return;
1183 }
1184 list_del(&tz->node);
1185 if (tz->ops->unbind)
1186 list_for_each_entry(cdev, &thermal_cdev_list, node)
1187 tz->ops->unbind(tz, cdev);
1188 mutex_unlock(&thermal_list_lock);
1189
Matthew Garrettb1569e92008-12-03 17:55:32 +00001190 thermal_zone_device_set_polling(tz, 0);
1191
Zhang Rui203d3d42008-01-17 15:51:08 +08001192 if (tz->type[0])
1193 device_remove_file(&tz->device, &dev_attr_type);
1194 device_remove_file(&tz->device, &dev_attr_temp);
1195 if (tz->ops->get_mode)
1196 device_remove_file(&tz->device, &dev_attr_mode);
1197
1198 for (count = 0; count < tz->trips; count++)
1199 TRIP_POINT_ATTR_REMOVE(&tz->device, count);
1200
Zhang Ruie68b16a2008-04-21 16:07:52 +08001201 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001202 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1203 idr_destroy(&tz->idr);
1204 mutex_destroy(&tz->lock);
1205 device_unregister(&tz->device);
1206 return;
1207}
Len Brown543a9562008-02-07 16:55:08 -05001208
Zhang Rui203d3d42008-01-17 15:51:08 +08001209EXPORT_SYMBOL(thermal_zone_device_unregister);
1210
1211static int __init thermal_init(void)
1212{
1213 int result = 0;
1214
1215 result = class_register(&thermal_class);
1216 if (result) {
1217 idr_destroy(&thermal_tz_idr);
1218 idr_destroy(&thermal_cdev_idr);
1219 mutex_destroy(&thermal_idr_lock);
1220 mutex_destroy(&thermal_list_lock);
1221 }
1222 return result;
1223}
1224
1225static void __exit thermal_exit(void)
1226{
1227 class_unregister(&thermal_class);
1228 idr_destroy(&thermal_tz_idr);
1229 idr_destroy(&thermal_cdev_idr);
1230 mutex_destroy(&thermal_idr_lock);
1231 mutex_destroy(&thermal_list_lock);
1232}
1233
1234subsys_initcall(thermal_init);
1235module_exit(thermal_exit);