]>
Commit | Line | Data |
---|---|---|
203d3d4a ZR |
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> | |
b1569e99 | 33 | #include <linux/reboot.h> |
203d3d4a | 34 | |
63c4ec90 | 35 | MODULE_AUTHOR("Zhang Rui"); |
203d3d4a ZR |
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); | |
6503e5df MG |
108 | long temperature; |
109 | int ret; | |
203d3d4a ZR |
110 | |
111 | if (!tz->ops->get_temp) | |
112 | return -EPERM; | |
113 | ||
6503e5df MG |
114 | ret = tz->ops->get_temp(tz, &temperature); |
115 | ||
116 | if (ret) | |
117 | return ret; | |
118 | ||
119 | return sprintf(buf, "%ld\n", temperature); | |
203d3d4a ZR |
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); | |
6503e5df MG |
126 | enum thermal_device_mode mode; |
127 | int result; | |
203d3d4a ZR |
128 | |
129 | if (!tz->ops->get_mode) | |
130 | return -EPERM; | |
131 | ||
6503e5df MG |
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"); | |
203d3d4a ZR |
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 | ||
6503e5df MG |
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 | ||
203d3d4a ZR |
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); | |
6503e5df MG |
168 | enum thermal_trip_type type; |
169 | int trip, result; | |
203d3d4a ZR |
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 | ||
6503e5df MG |
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: | |
625120a4 | 183 | return sprintf(buf, "critical\n"); |
6503e5df | 184 | case THERMAL_TRIP_HOT: |
625120a4 | 185 | return sprintf(buf, "hot\n"); |
6503e5df | 186 | case THERMAL_TRIP_PASSIVE: |
625120a4 | 187 | return sprintf(buf, "passive\n"); |
6503e5df | 188 | case THERMAL_TRIP_ACTIVE: |
625120a4 | 189 | return sprintf(buf, "active\n"); |
6503e5df | 190 | default: |
625120a4 | 191 | return sprintf(buf, "unknown\n"); |
6503e5df | 192 | } |
203d3d4a ZR |
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); | |
6503e5df MG |
200 | int trip, ret; |
201 | long temperature; | |
203d3d4a ZR |
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 | ||
6503e5df MG |
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); | |
203d3d4a ZR |
215 | } |
216 | ||
03a971a2 MG |
217 | static ssize_t |
218 | passive_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 | ||
3d8e3ad8 FP |
228 | /* 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 | ||
03a971a2 MG |
234 | 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); | |
244 | } else if (!state && tz->forced_passive) { | |
245 | mutex_lock(&thermal_list_lock); | |
246 | list_for_each_entry(cdev, &thermal_cdev_list, node) { | |
247 | if (!strncmp("Processor", cdev->type, | |
248 | sizeof("Processor"))) | |
249 | thermal_zone_unbind_cooling_device(tz, | |
250 | THERMAL_TRIPS_NONE, | |
251 | cdev); | |
252 | } | |
253 | mutex_unlock(&thermal_list_lock); | |
254 | } | |
255 | ||
256 | tz->tc1 = 1; | |
257 | tz->tc2 = 1; | |
258 | ||
259 | if (!tz->passive_delay) | |
260 | tz->passive_delay = 1000; | |
261 | ||
262 | if (!tz->polling_delay) | |
263 | tz->polling_delay = 10000; | |
264 | ||
265 | tz->forced_passive = state; | |
266 | ||
267 | thermal_zone_device_update(tz); | |
268 | ||
269 | return count; | |
270 | } | |
271 | ||
272 | static ssize_t | |
273 | passive_show(struct device *dev, struct device_attribute *attr, | |
274 | char *buf) | |
275 | { | |
276 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
277 | ||
278 | return sprintf(buf, "%d\n", tz->forced_passive); | |
279 | } | |
280 | ||
203d3d4a ZR |
281 | static DEVICE_ATTR(type, 0444, type_show, NULL); |
282 | static DEVICE_ATTR(temp, 0444, temp_show, NULL); | |
283 | static DEVICE_ATTR(mode, 0644, mode_show, mode_store); | |
03a971a2 MG |
284 | static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, \ |
285 | passive_store); | |
203d3d4a ZR |
286 | |
287 | static struct device_attribute trip_point_attrs[] = { | |
288 | __ATTR(trip_point_0_type, 0444, trip_point_type_show, NULL), | |
289 | __ATTR(trip_point_0_temp, 0444, trip_point_temp_show, NULL), | |
290 | __ATTR(trip_point_1_type, 0444, trip_point_type_show, NULL), | |
291 | __ATTR(trip_point_1_temp, 0444, trip_point_temp_show, NULL), | |
292 | __ATTR(trip_point_2_type, 0444, trip_point_type_show, NULL), | |
293 | __ATTR(trip_point_2_temp, 0444, trip_point_temp_show, NULL), | |
294 | __ATTR(trip_point_3_type, 0444, trip_point_type_show, NULL), | |
295 | __ATTR(trip_point_3_temp, 0444, trip_point_temp_show, NULL), | |
296 | __ATTR(trip_point_4_type, 0444, trip_point_type_show, NULL), | |
297 | __ATTR(trip_point_4_temp, 0444, trip_point_temp_show, NULL), | |
298 | __ATTR(trip_point_5_type, 0444, trip_point_type_show, NULL), | |
299 | __ATTR(trip_point_5_temp, 0444, trip_point_temp_show, NULL), | |
300 | __ATTR(trip_point_6_type, 0444, trip_point_type_show, NULL), | |
301 | __ATTR(trip_point_6_temp, 0444, trip_point_temp_show, NULL), | |
302 | __ATTR(trip_point_7_type, 0444, trip_point_type_show, NULL), | |
303 | __ATTR(trip_point_7_temp, 0444, trip_point_temp_show, NULL), | |
304 | __ATTR(trip_point_8_type, 0444, trip_point_type_show, NULL), | |
305 | __ATTR(trip_point_8_temp, 0444, trip_point_temp_show, NULL), | |
306 | __ATTR(trip_point_9_type, 0444, trip_point_type_show, NULL), | |
307 | __ATTR(trip_point_9_temp, 0444, trip_point_temp_show, NULL), | |
5f1a3f2a KH |
308 | __ATTR(trip_point_10_type, 0444, trip_point_type_show, NULL), |
309 | __ATTR(trip_point_10_temp, 0444, trip_point_temp_show, NULL), | |
310 | __ATTR(trip_point_11_type, 0444, trip_point_type_show, NULL), | |
311 | __ATTR(trip_point_11_temp, 0444, trip_point_temp_show, NULL), | |
203d3d4a ZR |
312 | }; |
313 | ||
314 | #define TRIP_POINT_ATTR_ADD(_dev, _index, result) \ | |
315 | do { \ | |
316 | result = device_create_file(_dev, \ | |
317 | &trip_point_attrs[_index * 2]); \ | |
318 | if (result) \ | |
319 | break; \ | |
320 | result = device_create_file(_dev, \ | |
321 | &trip_point_attrs[_index * 2 + 1]); \ | |
322 | } while (0) | |
323 | ||
324 | #define TRIP_POINT_ATTR_REMOVE(_dev, _index) \ | |
325 | do { \ | |
326 | device_remove_file(_dev, &trip_point_attrs[_index * 2]); \ | |
327 | device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \ | |
328 | } while (0) | |
329 | ||
330 | /* sys I/F for cooling device */ | |
331 | #define to_cooling_device(_dev) \ | |
332 | container_of(_dev, struct thermal_cooling_device, device) | |
333 | ||
334 | static ssize_t | |
335 | thermal_cooling_device_type_show(struct device *dev, | |
336 | struct device_attribute *attr, char *buf) | |
337 | { | |
338 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
339 | ||
340 | return sprintf(buf, "%s\n", cdev->type); | |
341 | } | |
342 | ||
343 | static ssize_t | |
344 | thermal_cooling_device_max_state_show(struct device *dev, | |
345 | struct device_attribute *attr, char *buf) | |
346 | { | |
347 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
6503e5df MG |
348 | unsigned long state; |
349 | int ret; | |
203d3d4a | 350 | |
6503e5df MG |
351 | ret = cdev->ops->get_max_state(cdev, &state); |
352 | if (ret) | |
353 | return ret; | |
354 | return sprintf(buf, "%ld\n", state); | |
203d3d4a ZR |
355 | } |
356 | ||
357 | static ssize_t | |
358 | thermal_cooling_device_cur_state_show(struct device *dev, | |
359 | struct device_attribute *attr, char *buf) | |
360 | { | |
361 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
6503e5df MG |
362 | unsigned long state; |
363 | int ret; | |
203d3d4a | 364 | |
6503e5df MG |
365 | ret = cdev->ops->get_cur_state(cdev, &state); |
366 | if (ret) | |
367 | return ret; | |
368 | return sprintf(buf, "%ld\n", state); | |
203d3d4a ZR |
369 | } |
370 | ||
371 | static ssize_t | |
372 | thermal_cooling_device_cur_state_store(struct device *dev, | |
373 | struct device_attribute *attr, | |
374 | const char *buf, size_t count) | |
375 | { | |
376 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
6503e5df | 377 | unsigned long state; |
203d3d4a ZR |
378 | int result; |
379 | ||
6503e5df | 380 | if (!sscanf(buf, "%ld\n", &state)) |
203d3d4a ZR |
381 | return -EINVAL; |
382 | ||
383 | if (state < 0) | |
384 | return -EINVAL; | |
385 | ||
386 | result = cdev->ops->set_cur_state(cdev, state); | |
387 | if (result) | |
388 | return result; | |
389 | return count; | |
390 | } | |
391 | ||
392 | static struct device_attribute dev_attr_cdev_type = | |
543a9561 | 393 | __ATTR(type, 0444, thermal_cooling_device_type_show, NULL); |
203d3d4a ZR |
394 | static DEVICE_ATTR(max_state, 0444, |
395 | thermal_cooling_device_max_state_show, NULL); | |
396 | static DEVICE_ATTR(cur_state, 0644, | |
397 | thermal_cooling_device_cur_state_show, | |
398 | thermal_cooling_device_cur_state_store); | |
399 | ||
400 | static ssize_t | |
401 | thermal_cooling_device_trip_point_show(struct device *dev, | |
543a9561 | 402 | struct device_attribute *attr, char *buf) |
203d3d4a ZR |
403 | { |
404 | struct thermal_cooling_device_instance *instance; | |
405 | ||
406 | instance = | |
407 | container_of(attr, struct thermal_cooling_device_instance, attr); | |
408 | ||
409 | if (instance->trip == THERMAL_TRIPS_NONE) | |
410 | return sprintf(buf, "-1\n"); | |
411 | else | |
412 | return sprintf(buf, "%d\n", instance->trip); | |
413 | } | |
414 | ||
415 | /* Device management */ | |
416 | ||
16d75239 RH |
417 | #if defined(CONFIG_THERMAL_HWMON) |
418 | ||
e68b16ab ZR |
419 | /* hwmon sys I/F */ |
420 | #include <linux/hwmon.h> | |
421 | static LIST_HEAD(thermal_hwmon_list); | |
422 | ||
423 | static ssize_t | |
424 | name_show(struct device *dev, struct device_attribute *attr, char *buf) | |
425 | { | |
0e968a3b | 426 | struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev); |
e68b16ab ZR |
427 | return sprintf(buf, "%s\n", hwmon->type); |
428 | } | |
429 | static DEVICE_ATTR(name, 0444, name_show, NULL); | |
430 | ||
431 | static ssize_t | |
432 | temp_input_show(struct device *dev, struct device_attribute *attr, char *buf) | |
433 | { | |
6503e5df MG |
434 | long temperature; |
435 | int ret; | |
e68b16ab ZR |
436 | struct thermal_hwmon_attr *hwmon_attr |
437 | = container_of(attr, struct thermal_hwmon_attr, attr); | |
438 | struct thermal_zone_device *tz | |
439 | = container_of(hwmon_attr, struct thermal_zone_device, | |
440 | temp_input); | |
441 | ||
6503e5df MG |
442 | ret = tz->ops->get_temp(tz, &temperature); |
443 | ||
444 | if (ret) | |
445 | return ret; | |
446 | ||
447 | return sprintf(buf, "%ld\n", temperature); | |
e68b16ab ZR |
448 | } |
449 | ||
450 | static ssize_t | |
451 | temp_crit_show(struct device *dev, struct device_attribute *attr, | |
452 | char *buf) | |
453 | { | |
454 | struct thermal_hwmon_attr *hwmon_attr | |
455 | = container_of(attr, struct thermal_hwmon_attr, attr); | |
456 | struct thermal_zone_device *tz | |
457 | = container_of(hwmon_attr, struct thermal_zone_device, | |
458 | temp_crit); | |
6503e5df MG |
459 | long temperature; |
460 | int ret; | |
461 | ||
462 | ret = tz->ops->get_trip_temp(tz, 0, &temperature); | |
463 | if (ret) | |
464 | return ret; | |
e68b16ab | 465 | |
6503e5df | 466 | return sprintf(buf, "%ld\n", temperature); |
e68b16ab ZR |
467 | } |
468 | ||
469 | ||
470 | static int | |
471 | thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) | |
472 | { | |
473 | struct thermal_hwmon_device *hwmon; | |
474 | int new_hwmon_device = 1; | |
475 | int result; | |
476 | ||
477 | mutex_lock(&thermal_list_lock); | |
478 | list_for_each_entry(hwmon, &thermal_hwmon_list, node) | |
479 | if (!strcmp(hwmon->type, tz->type)) { | |
480 | new_hwmon_device = 0; | |
481 | mutex_unlock(&thermal_list_lock); | |
482 | goto register_sys_interface; | |
483 | } | |
484 | mutex_unlock(&thermal_list_lock); | |
485 | ||
486 | hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL); | |
487 | if (!hwmon) | |
488 | return -ENOMEM; | |
489 | ||
490 | INIT_LIST_HEAD(&hwmon->tz_list); | |
491 | strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH); | |
492 | hwmon->device = hwmon_device_register(NULL); | |
493 | if (IS_ERR(hwmon->device)) { | |
494 | result = PTR_ERR(hwmon->device); | |
495 | goto free_mem; | |
496 | } | |
0e968a3b | 497 | dev_set_drvdata(hwmon->device, hwmon); |
e68b16ab ZR |
498 | result = device_create_file(hwmon->device, &dev_attr_name); |
499 | if (result) | |
500 | goto unregister_hwmon_device; | |
501 | ||
502 | register_sys_interface: | |
503 | tz->hwmon = hwmon; | |
504 | hwmon->count++; | |
505 | ||
506 | snprintf(tz->temp_input.name, THERMAL_NAME_LENGTH, | |
507 | "temp%d_input", hwmon->count); | |
508 | tz->temp_input.attr.attr.name = tz->temp_input.name; | |
509 | tz->temp_input.attr.attr.mode = 0444; | |
510 | tz->temp_input.attr.show = temp_input_show; | |
511 | result = device_create_file(hwmon->device, &tz->temp_input.attr); | |
512 | if (result) | |
513 | goto unregister_hwmon_device; | |
514 | ||
515 | if (tz->ops->get_crit_temp) { | |
516 | unsigned long temperature; | |
517 | if (!tz->ops->get_crit_temp(tz, &temperature)) { | |
518 | snprintf(tz->temp_crit.name, THERMAL_NAME_LENGTH, | |
519 | "temp%d_crit", hwmon->count); | |
520 | tz->temp_crit.attr.attr.name = tz->temp_crit.name; | |
521 | tz->temp_crit.attr.attr.mode = 0444; | |
522 | tz->temp_crit.attr.show = temp_crit_show; | |
523 | result = device_create_file(hwmon->device, | |
524 | &tz->temp_crit.attr); | |
525 | if (result) | |
526 | goto unregister_hwmon_device; | |
527 | } | |
528 | } | |
529 | ||
530 | mutex_lock(&thermal_list_lock); | |
531 | if (new_hwmon_device) | |
532 | list_add_tail(&hwmon->node, &thermal_hwmon_list); | |
533 | list_add_tail(&tz->hwmon_node, &hwmon->tz_list); | |
534 | mutex_unlock(&thermal_list_lock); | |
535 | ||
536 | return 0; | |
537 | ||
538 | unregister_hwmon_device: | |
539 | device_remove_file(hwmon->device, &tz->temp_crit.attr); | |
540 | device_remove_file(hwmon->device, &tz->temp_input.attr); | |
541 | if (new_hwmon_device) { | |
542 | device_remove_file(hwmon->device, &dev_attr_name); | |
543 | hwmon_device_unregister(hwmon->device); | |
544 | } | |
545 | free_mem: | |
546 | if (new_hwmon_device) | |
547 | kfree(hwmon); | |
548 | ||
549 | return result; | |
550 | } | |
551 | ||
552 | static void | |
553 | thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz) | |
554 | { | |
555 | struct thermal_hwmon_device *hwmon = tz->hwmon; | |
556 | ||
557 | tz->hwmon = NULL; | |
558 | device_remove_file(hwmon->device, &tz->temp_input.attr); | |
559 | device_remove_file(hwmon->device, &tz->temp_crit.attr); | |
560 | ||
561 | mutex_lock(&thermal_list_lock); | |
562 | list_del(&tz->hwmon_node); | |
563 | if (!list_empty(&hwmon->tz_list)) { | |
564 | mutex_unlock(&thermal_list_lock); | |
565 | return; | |
566 | } | |
567 | list_del(&hwmon->node); | |
568 | mutex_unlock(&thermal_list_lock); | |
569 | ||
570 | device_remove_file(hwmon->device, &dev_attr_name); | |
571 | hwmon_device_unregister(hwmon->device); | |
572 | kfree(hwmon); | |
573 | } | |
574 | #else | |
575 | static int | |
576 | thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) | |
577 | { | |
578 | return 0; | |
579 | } | |
580 | ||
581 | static void | |
582 | thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz) | |
583 | { | |
584 | } | |
585 | #endif | |
586 | ||
b1569e99 MG |
587 | static void thermal_zone_device_set_polling(struct thermal_zone_device *tz, |
588 | int delay) | |
589 | { | |
590 | cancel_delayed_work(&(tz->poll_queue)); | |
591 | ||
592 | if (!delay) | |
593 | return; | |
594 | ||
595 | if (delay > 1000) | |
596 | schedule_delayed_work(&(tz->poll_queue), | |
597 | round_jiffies(msecs_to_jiffies(delay))); | |
598 | else | |
599 | schedule_delayed_work(&(tz->poll_queue), | |
600 | msecs_to_jiffies(delay)); | |
601 | } | |
602 | ||
603 | static void thermal_zone_device_passive(struct thermal_zone_device *tz, | |
604 | int temp, int trip_temp, int trip) | |
605 | { | |
606 | int trend = 0; | |
607 | struct thermal_cooling_device_instance *instance; | |
608 | struct thermal_cooling_device *cdev; | |
609 | long state, max_state; | |
610 | ||
611 | /* | |
612 | * Above Trip? | |
613 | * ----------- | |
614 | * Calculate the thermal trend (using the passive cooling equation) | |
615 | * and modify the performance limit for all passive cooling devices | |
616 | * accordingly. Note that we assume symmetry. | |
617 | */ | |
618 | if (temp >= trip_temp) { | |
619 | tz->passive = true; | |
620 | ||
621 | trend = (tz->tc1 * (temp - tz->last_temperature)) + | |
622 | (tz->tc2 * (temp - trip_temp)); | |
623 | ||
624 | /* Heating up? */ | |
625 | if (trend > 0) { | |
626 | list_for_each_entry(instance, &tz->cooling_devices, | |
627 | node) { | |
628 | if (instance->trip != trip) | |
629 | continue; | |
630 | cdev = instance->cdev; | |
631 | cdev->ops->get_cur_state(cdev, &state); | |
632 | cdev->ops->get_max_state(cdev, &max_state); | |
633 | if (state++ < max_state) | |
634 | cdev->ops->set_cur_state(cdev, state); | |
635 | } | |
636 | } else if (trend < 0) { /* Cooling off? */ | |
637 | list_for_each_entry(instance, &tz->cooling_devices, | |
638 | node) { | |
639 | if (instance->trip != trip) | |
640 | continue; | |
641 | cdev = instance->cdev; | |
642 | cdev->ops->get_cur_state(cdev, &state); | |
643 | cdev->ops->get_max_state(cdev, &max_state); | |
644 | if (state > 0) | |
645 | cdev->ops->set_cur_state(cdev, --state); | |
646 | } | |
647 | } | |
648 | return; | |
649 | } | |
650 | ||
651 | /* | |
652 | * Below Trip? | |
653 | * ----------- | |
654 | * Implement passive cooling hysteresis to slowly increase performance | |
655 | * and avoid thrashing around the passive trip point. Note that we | |
656 | * assume symmetry. | |
657 | */ | |
658 | list_for_each_entry(instance, &tz->cooling_devices, node) { | |
659 | if (instance->trip != trip) | |
660 | continue; | |
661 | cdev = instance->cdev; | |
662 | cdev->ops->get_cur_state(cdev, &state); | |
663 | cdev->ops->get_max_state(cdev, &max_state); | |
664 | if (state > 0) | |
665 | cdev->ops->set_cur_state(cdev, --state); | |
666 | if (state == 0) | |
667 | tz->passive = false; | |
668 | } | |
669 | } | |
670 | ||
671 | static void thermal_zone_device_check(struct work_struct *work) | |
672 | { | |
673 | struct thermal_zone_device *tz = container_of(work, struct | |
674 | thermal_zone_device, | |
675 | poll_queue.work); | |
676 | thermal_zone_device_update(tz); | |
677 | } | |
e68b16ab | 678 | |
203d3d4a ZR |
679 | /** |
680 | * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone | |
203d3d4a ZR |
681 | * @tz: thermal zone device |
682 | * @trip: indicates which trip point the cooling devices is | |
683 | * associated with in this thermal zone. | |
684 | * @cdev: thermal cooling device | |
543a9561 LB |
685 | * |
686 | * This function is usually called in the thermal zone device .bind callback. | |
203d3d4a ZR |
687 | */ |
688 | int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, | |
689 | int trip, | |
690 | struct thermal_cooling_device *cdev) | |
691 | { | |
692 | struct thermal_cooling_device_instance *dev; | |
693 | struct thermal_cooling_device_instance *pos; | |
c7516709 TS |
694 | struct thermal_zone_device *pos1; |
695 | struct thermal_cooling_device *pos2; | |
203d3d4a ZR |
696 | int result; |
697 | ||
543a9561 | 698 | if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE)) |
203d3d4a ZR |
699 | return -EINVAL; |
700 | ||
c7516709 TS |
701 | list_for_each_entry(pos1, &thermal_tz_list, node) { |
702 | if (pos1 == tz) | |
703 | break; | |
704 | } | |
705 | list_for_each_entry(pos2, &thermal_cdev_list, node) { | |
706 | if (pos2 == cdev) | |
707 | break; | |
708 | } | |
709 | ||
710 | if (tz != pos1 || cdev != pos2) | |
203d3d4a ZR |
711 | return -EINVAL; |
712 | ||
713 | dev = | |
714 | kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL); | |
715 | if (!dev) | |
716 | return -ENOMEM; | |
717 | dev->tz = tz; | |
718 | dev->cdev = cdev; | |
719 | dev->trip = trip; | |
720 | result = get_idr(&tz->idr, &tz->lock, &dev->id); | |
721 | if (result) | |
722 | goto free_mem; | |
723 | ||
724 | sprintf(dev->name, "cdev%d", dev->id); | |
725 | result = | |
726 | sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name); | |
727 | if (result) | |
728 | goto release_idr; | |
729 | ||
730 | sprintf(dev->attr_name, "cdev%d_trip_point", dev->id); | |
731 | dev->attr.attr.name = dev->attr_name; | |
732 | dev->attr.attr.mode = 0444; | |
733 | dev->attr.show = thermal_cooling_device_trip_point_show; | |
734 | result = device_create_file(&tz->device, &dev->attr); | |
735 | if (result) | |
736 | goto remove_symbol_link; | |
737 | ||
738 | mutex_lock(&tz->lock); | |
739 | list_for_each_entry(pos, &tz->cooling_devices, node) | |
740 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { | |
741 | result = -EEXIST; | |
742 | break; | |
743 | } | |
744 | if (!result) | |
745 | list_add_tail(&dev->node, &tz->cooling_devices); | |
746 | mutex_unlock(&tz->lock); | |
747 | ||
748 | if (!result) | |
749 | return 0; | |
750 | ||
751 | device_remove_file(&tz->device, &dev->attr); | |
752 | remove_symbol_link: | |
753 | sysfs_remove_link(&tz->device.kobj, dev->name); | |
754 | release_idr: | |
755 | release_idr(&tz->idr, &tz->lock, dev->id); | |
756 | free_mem: | |
757 | kfree(dev); | |
758 | return result; | |
759 | } | |
543a9561 | 760 | |
203d3d4a ZR |
761 | EXPORT_SYMBOL(thermal_zone_bind_cooling_device); |
762 | ||
763 | /** | |
764 | * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone | |
203d3d4a ZR |
765 | * @tz: thermal zone device |
766 | * @trip: indicates which trip point the cooling devices is | |
767 | * associated with in this thermal zone. | |
768 | * @cdev: thermal cooling device | |
543a9561 LB |
769 | * |
770 | * This function is usually called in the thermal zone device .unbind callback. | |
203d3d4a ZR |
771 | */ |
772 | int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz, | |
773 | int trip, | |
774 | struct thermal_cooling_device *cdev) | |
775 | { | |
776 | struct thermal_cooling_device_instance *pos, *next; | |
777 | ||
778 | mutex_lock(&tz->lock); | |
779 | list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) { | |
543a9561 | 780 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
203d3d4a ZR |
781 | list_del(&pos->node); |
782 | mutex_unlock(&tz->lock); | |
783 | goto unbind; | |
784 | } | |
785 | } | |
786 | mutex_unlock(&tz->lock); | |
787 | ||
788 | return -ENODEV; | |
789 | ||
790 | unbind: | |
791 | device_remove_file(&tz->device, &pos->attr); | |
792 | sysfs_remove_link(&tz->device.kobj, pos->name); | |
793 | release_idr(&tz->idr, &tz->lock, pos->id); | |
794 | kfree(pos); | |
795 | return 0; | |
796 | } | |
543a9561 | 797 | |
203d3d4a ZR |
798 | EXPORT_SYMBOL(thermal_zone_unbind_cooling_device); |
799 | ||
800 | static void thermal_release(struct device *dev) | |
801 | { | |
802 | struct thermal_zone_device *tz; | |
803 | struct thermal_cooling_device *cdev; | |
804 | ||
354655ea | 805 | if (!strncmp(dev_name(dev), "thermal_zone", sizeof "thermal_zone" - 1)) { |
203d3d4a ZR |
806 | tz = to_thermal_zone(dev); |
807 | kfree(tz); | |
808 | } else { | |
809 | cdev = to_cooling_device(dev); | |
810 | kfree(cdev); | |
811 | } | |
812 | } | |
813 | ||
814 | static struct class thermal_class = { | |
815 | .name = "thermal", | |
816 | .dev_release = thermal_release, | |
817 | }; | |
818 | ||
819 | /** | |
820 | * thermal_cooling_device_register - register a new thermal cooling device | |
821 | * @type: the thermal cooling device type. | |
822 | * @devdata: device private data. | |
823 | * @ops: standard thermal cooling devices callbacks. | |
824 | */ | |
825 | struct thermal_cooling_device *thermal_cooling_device_register(char *type, | |
543a9561 LB |
826 | void *devdata, |
827 | struct | |
828 | thermal_cooling_device_ops | |
829 | *ops) | |
203d3d4a ZR |
830 | { |
831 | struct thermal_cooling_device *cdev; | |
832 | struct thermal_zone_device *pos; | |
833 | int result; | |
834 | ||
835 | if (strlen(type) >= THERMAL_NAME_LENGTH) | |
3e6fda5c | 836 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
837 | |
838 | if (!ops || !ops->get_max_state || !ops->get_cur_state || | |
543a9561 | 839 | !ops->set_cur_state) |
3e6fda5c | 840 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
841 | |
842 | cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL); | |
843 | if (!cdev) | |
3e6fda5c | 844 | return ERR_PTR(-ENOMEM); |
203d3d4a ZR |
845 | |
846 | result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id); | |
847 | if (result) { | |
848 | kfree(cdev); | |
3e6fda5c | 849 | return ERR_PTR(result); |
203d3d4a ZR |
850 | } |
851 | ||
852 | strcpy(cdev->type, type); | |
853 | cdev->ops = ops; | |
854 | cdev->device.class = &thermal_class; | |
855 | cdev->devdata = devdata; | |
354655ea | 856 | dev_set_name(&cdev->device, "cooling_device%d", cdev->id); |
203d3d4a ZR |
857 | result = device_register(&cdev->device); |
858 | if (result) { | |
859 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); | |
860 | kfree(cdev); | |
3e6fda5c | 861 | return ERR_PTR(result); |
203d3d4a ZR |
862 | } |
863 | ||
864 | /* sys I/F */ | |
865 | if (type) { | |
543a9561 | 866 | result = device_create_file(&cdev->device, &dev_attr_cdev_type); |
203d3d4a ZR |
867 | if (result) |
868 | goto unregister; | |
869 | } | |
870 | ||
871 | result = device_create_file(&cdev->device, &dev_attr_max_state); | |
872 | if (result) | |
873 | goto unregister; | |
874 | ||
875 | result = device_create_file(&cdev->device, &dev_attr_cur_state); | |
876 | if (result) | |
877 | goto unregister; | |
878 | ||
879 | mutex_lock(&thermal_list_lock); | |
880 | list_add(&cdev->node, &thermal_cdev_list); | |
881 | list_for_each_entry(pos, &thermal_tz_list, node) { | |
882 | if (!pos->ops->bind) | |
883 | continue; | |
884 | result = pos->ops->bind(pos, cdev); | |
885 | if (result) | |
886 | break; | |
887 | ||
888 | } | |
889 | mutex_unlock(&thermal_list_lock); | |
890 | ||
891 | if (!result) | |
892 | return cdev; | |
893 | ||
894 | unregister: | |
895 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); | |
896 | device_unregister(&cdev->device); | |
3e6fda5c | 897 | return ERR_PTR(result); |
203d3d4a | 898 | } |
543a9561 | 899 | |
203d3d4a ZR |
900 | EXPORT_SYMBOL(thermal_cooling_device_register); |
901 | ||
902 | /** | |
903 | * thermal_cooling_device_unregister - removes the registered thermal cooling device | |
203d3d4a ZR |
904 | * @cdev: the thermal cooling device to remove. |
905 | * | |
906 | * thermal_cooling_device_unregister() must be called when the device is no | |
907 | * longer needed. | |
908 | */ | |
909 | void thermal_cooling_device_unregister(struct | |
910 | thermal_cooling_device | |
911 | *cdev) | |
912 | { | |
913 | struct thermal_zone_device *tz; | |
914 | struct thermal_cooling_device *pos = NULL; | |
915 | ||
916 | if (!cdev) | |
917 | return; | |
918 | ||
919 | mutex_lock(&thermal_list_lock); | |
920 | list_for_each_entry(pos, &thermal_cdev_list, node) | |
921 | if (pos == cdev) | |
922 | break; | |
923 | if (pos != cdev) { | |
924 | /* thermal cooling device not found */ | |
925 | mutex_unlock(&thermal_list_lock); | |
926 | return; | |
927 | } | |
928 | list_del(&cdev->node); | |
929 | list_for_each_entry(tz, &thermal_tz_list, node) { | |
930 | if (!tz->ops->unbind) | |
931 | continue; | |
932 | tz->ops->unbind(tz, cdev); | |
933 | } | |
934 | mutex_unlock(&thermal_list_lock); | |
935 | if (cdev->type[0]) | |
543a9561 | 936 | device_remove_file(&cdev->device, &dev_attr_cdev_type); |
203d3d4a ZR |
937 | device_remove_file(&cdev->device, &dev_attr_max_state); |
938 | device_remove_file(&cdev->device, &dev_attr_cur_state); | |
939 | ||
940 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); | |
941 | device_unregister(&cdev->device); | |
942 | return; | |
943 | } | |
543a9561 | 944 | |
203d3d4a ZR |
945 | EXPORT_SYMBOL(thermal_cooling_device_unregister); |
946 | ||
b1569e99 MG |
947 | /** |
948 | * thermal_zone_device_update - force an update of a thermal zone's state | |
949 | * @ttz: the thermal zone to update | |
950 | */ | |
951 | ||
952 | void thermal_zone_device_update(struct thermal_zone_device *tz) | |
953 | { | |
954 | int count, ret = 0; | |
955 | long temp, trip_temp; | |
956 | enum thermal_trip_type trip_type; | |
957 | struct thermal_cooling_device_instance *instance; | |
958 | struct thermal_cooling_device *cdev; | |
959 | ||
960 | mutex_lock(&tz->lock); | |
961 | ||
0d288162 MB |
962 | if (tz->ops->get_temp(tz, &temp)) { |
963 | /* get_temp failed - retry it later */ | |
964 | printk(KERN_WARNING PREFIX "failed to read out thermal zone " | |
965 | "%d\n", tz->id); | |
966 | goto leave; | |
967 | } | |
b1569e99 MG |
968 | |
969 | for (count = 0; count < tz->trips; count++) { | |
970 | tz->ops->get_trip_type(tz, count, &trip_type); | |
971 | tz->ops->get_trip_temp(tz, count, &trip_temp); | |
972 | ||
973 | switch (trip_type) { | |
974 | case THERMAL_TRIP_CRITICAL: | |
29321357 | 975 | if (temp >= trip_temp) { |
b1569e99 MG |
976 | if (tz->ops->notify) |
977 | ret = tz->ops->notify(tz, count, | |
978 | trip_type); | |
979 | if (!ret) { | |
980 | printk(KERN_EMERG | |
981 | "Critical temperature reached (%ld C), shutting down.\n", | |
982 | temp/1000); | |
983 | orderly_poweroff(true); | |
984 | } | |
985 | } | |
986 | break; | |
987 | case THERMAL_TRIP_HOT: | |
29321357 | 988 | if (temp >= trip_temp) |
b1569e99 MG |
989 | if (tz->ops->notify) |
990 | tz->ops->notify(tz, count, trip_type); | |
991 | break; | |
992 | case THERMAL_TRIP_ACTIVE: | |
993 | list_for_each_entry(instance, &tz->cooling_devices, | |
994 | node) { | |
995 | if (instance->trip != count) | |
996 | continue; | |
997 | ||
998 | cdev = instance->cdev; | |
999 | ||
29321357 | 1000 | if (temp >= trip_temp) |
b1569e99 MG |
1001 | cdev->ops->set_cur_state(cdev, 1); |
1002 | else | |
1003 | cdev->ops->set_cur_state(cdev, 0); | |
1004 | } | |
1005 | break; | |
1006 | case THERMAL_TRIP_PASSIVE: | |
29321357 | 1007 | if (temp >= trip_temp || tz->passive) |
b1569e99 MG |
1008 | thermal_zone_device_passive(tz, temp, |
1009 | trip_temp, count); | |
1010 | break; | |
1011 | } | |
1012 | } | |
03a971a2 MG |
1013 | |
1014 | if (tz->forced_passive) | |
1015 | thermal_zone_device_passive(tz, temp, tz->forced_passive, | |
1016 | THERMAL_TRIPS_NONE); | |
1017 | ||
b1569e99 | 1018 | tz->last_temperature = temp; |
0d288162 MB |
1019 | |
1020 | leave: | |
b1569e99 MG |
1021 | if (tz->passive) |
1022 | thermal_zone_device_set_polling(tz, tz->passive_delay); | |
1023 | else if (tz->polling_delay) | |
1024 | thermal_zone_device_set_polling(tz, tz->polling_delay); | |
1025 | mutex_unlock(&tz->lock); | |
1026 | } | |
1027 | EXPORT_SYMBOL(thermal_zone_device_update); | |
1028 | ||
203d3d4a ZR |
1029 | /** |
1030 | * thermal_zone_device_register - register a new thermal zone device | |
1031 | * @type: the thermal zone device type | |
1032 | * @trips: the number of trip points the thermal zone support | |
1033 | * @devdata: private device data | |
1034 | * @ops: standard thermal zone device callbacks | |
b1569e99 MG |
1035 | * @tc1: thermal coefficient 1 for passive calculations |
1036 | * @tc2: thermal coefficient 2 for passive calculations | |
1037 | * @passive_delay: number of milliseconds to wait between polls when | |
1038 | * performing passive cooling | |
1039 | * @polling_delay: number of milliseconds to wait between polls when checking | |
1040 | * whether trip points have been crossed (0 for interrupt | |
1041 | * driven systems) | |
203d3d4a ZR |
1042 | * |
1043 | * thermal_zone_device_unregister() must be called when the device is no | |
b1569e99 MG |
1044 | * longer needed. The passive cooling formula uses tc1 and tc2 as described in |
1045 | * section 11.1.5.1 of the ACPI specification 3.0. | |
203d3d4a ZR |
1046 | */ |
1047 | struct thermal_zone_device *thermal_zone_device_register(char *type, | |
543a9561 LB |
1048 | int trips, |
1049 | void *devdata, struct | |
1050 | thermal_zone_device_ops | |
b1569e99 MG |
1051 | *ops, int tc1, int |
1052 | tc2, | |
1053 | int passive_delay, | |
1054 | int polling_delay) | |
203d3d4a ZR |
1055 | { |
1056 | struct thermal_zone_device *tz; | |
1057 | struct thermal_cooling_device *pos; | |
03a971a2 | 1058 | enum thermal_trip_type trip_type; |
203d3d4a ZR |
1059 | int result; |
1060 | int count; | |
03a971a2 | 1061 | int passive = 0; |
203d3d4a ZR |
1062 | |
1063 | if (strlen(type) >= THERMAL_NAME_LENGTH) | |
3e6fda5c | 1064 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
1065 | |
1066 | if (trips > THERMAL_MAX_TRIPS || trips < 0) | |
3e6fda5c | 1067 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
1068 | |
1069 | if (!ops || !ops->get_temp) | |
3e6fda5c | 1070 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
1071 | |
1072 | tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL); | |
1073 | if (!tz) | |
3e6fda5c | 1074 | return ERR_PTR(-ENOMEM); |
203d3d4a ZR |
1075 | |
1076 | INIT_LIST_HEAD(&tz->cooling_devices); | |
1077 | idr_init(&tz->idr); | |
1078 | mutex_init(&tz->lock); | |
1079 | result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id); | |
1080 | if (result) { | |
1081 | kfree(tz); | |
3e6fda5c | 1082 | return ERR_PTR(result); |
203d3d4a ZR |
1083 | } |
1084 | ||
1085 | strcpy(tz->type, type); | |
1086 | tz->ops = ops; | |
1087 | tz->device.class = &thermal_class; | |
1088 | tz->devdata = devdata; | |
1089 | tz->trips = trips; | |
b1569e99 MG |
1090 | tz->tc1 = tc1; |
1091 | tz->tc2 = tc2; | |
1092 | tz->passive_delay = passive_delay; | |
1093 | tz->polling_delay = polling_delay; | |
1094 | ||
354655ea | 1095 | dev_set_name(&tz->device, "thermal_zone%d", tz->id); |
203d3d4a ZR |
1096 | result = device_register(&tz->device); |
1097 | if (result) { | |
1098 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); | |
1099 | kfree(tz); | |
3e6fda5c | 1100 | return ERR_PTR(result); |
203d3d4a ZR |
1101 | } |
1102 | ||
1103 | /* sys I/F */ | |
1104 | if (type) { | |
1105 | result = device_create_file(&tz->device, &dev_attr_type); | |
1106 | if (result) | |
1107 | goto unregister; | |
1108 | } | |
1109 | ||
1110 | result = device_create_file(&tz->device, &dev_attr_temp); | |
1111 | if (result) | |
1112 | goto unregister; | |
1113 | ||
1114 | if (ops->get_mode) { | |
1115 | result = device_create_file(&tz->device, &dev_attr_mode); | |
1116 | if (result) | |
1117 | goto unregister; | |
1118 | } | |
1119 | ||
1120 | for (count = 0; count < trips; count++) { | |
1121 | TRIP_POINT_ATTR_ADD(&tz->device, count, result); | |
1122 | if (result) | |
1123 | goto unregister; | |
03a971a2 MG |
1124 | tz->ops->get_trip_type(tz, count, &trip_type); |
1125 | if (trip_type == THERMAL_TRIP_PASSIVE) | |
1126 | passive = 1; | |
203d3d4a ZR |
1127 | } |
1128 | ||
03a971a2 MG |
1129 | if (!passive) |
1130 | result = device_create_file(&tz->device, | |
1131 | &dev_attr_passive); | |
1132 | ||
1133 | if (result) | |
1134 | goto unregister; | |
1135 | ||
e68b16ab ZR |
1136 | result = thermal_add_hwmon_sysfs(tz); |
1137 | if (result) | |
1138 | goto unregister; | |
1139 | ||
203d3d4a ZR |
1140 | mutex_lock(&thermal_list_lock); |
1141 | list_add_tail(&tz->node, &thermal_tz_list); | |
1142 | if (ops->bind) | |
1143 | list_for_each_entry(pos, &thermal_cdev_list, node) { | |
543a9561 LB |
1144 | result = ops->bind(tz, pos); |
1145 | if (result) | |
1146 | break; | |
203d3d4a ZR |
1147 | } |
1148 | mutex_unlock(&thermal_list_lock); | |
1149 | ||
b1569e99 MG |
1150 | INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check); |
1151 | ||
1152 | thermal_zone_device_update(tz); | |
1153 | ||
203d3d4a ZR |
1154 | if (!result) |
1155 | return tz; | |
1156 | ||
1157 | unregister: | |
1158 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); | |
1159 | device_unregister(&tz->device); | |
3e6fda5c | 1160 | return ERR_PTR(result); |
203d3d4a | 1161 | } |
543a9561 | 1162 | |
203d3d4a ZR |
1163 | EXPORT_SYMBOL(thermal_zone_device_register); |
1164 | ||
1165 | /** | |
1166 | * thermal_device_unregister - removes the registered thermal zone device | |
203d3d4a ZR |
1167 | * @tz: the thermal zone device to remove |
1168 | */ | |
1169 | void thermal_zone_device_unregister(struct thermal_zone_device *tz) | |
1170 | { | |
1171 | struct thermal_cooling_device *cdev; | |
1172 | struct thermal_zone_device *pos = NULL; | |
1173 | int count; | |
1174 | ||
1175 | if (!tz) | |
1176 | return; | |
1177 | ||
1178 | mutex_lock(&thermal_list_lock); | |
1179 | list_for_each_entry(pos, &thermal_tz_list, node) | |
1180 | if (pos == tz) | |
1181 | break; | |
1182 | if (pos != tz) { | |
1183 | /* thermal zone device not found */ | |
1184 | mutex_unlock(&thermal_list_lock); | |
1185 | return; | |
1186 | } | |
1187 | list_del(&tz->node); | |
1188 | if (tz->ops->unbind) | |
1189 | list_for_each_entry(cdev, &thermal_cdev_list, node) | |
1190 | tz->ops->unbind(tz, cdev); | |
1191 | mutex_unlock(&thermal_list_lock); | |
1192 | ||
b1569e99 MG |
1193 | thermal_zone_device_set_polling(tz, 0); |
1194 | ||
203d3d4a ZR |
1195 | if (tz->type[0]) |
1196 | device_remove_file(&tz->device, &dev_attr_type); | |
1197 | device_remove_file(&tz->device, &dev_attr_temp); | |
1198 | if (tz->ops->get_mode) | |
1199 | device_remove_file(&tz->device, &dev_attr_mode); | |
1200 | ||
1201 | for (count = 0; count < tz->trips; count++) | |
1202 | TRIP_POINT_ATTR_REMOVE(&tz->device, count); | |
1203 | ||
e68b16ab | 1204 | thermal_remove_hwmon_sysfs(tz); |
203d3d4a ZR |
1205 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
1206 | idr_destroy(&tz->idr); | |
1207 | mutex_destroy(&tz->lock); | |
1208 | device_unregister(&tz->device); | |
1209 | return; | |
1210 | } | |
543a9561 | 1211 | |
203d3d4a ZR |
1212 | EXPORT_SYMBOL(thermal_zone_device_unregister); |
1213 | ||
1214 | static int __init thermal_init(void) | |
1215 | { | |
1216 | int result = 0; | |
1217 | ||
1218 | result = class_register(&thermal_class); | |
1219 | if (result) { | |
1220 | idr_destroy(&thermal_tz_idr); | |
1221 | idr_destroy(&thermal_cdev_idr); | |
1222 | mutex_destroy(&thermal_idr_lock); | |
1223 | mutex_destroy(&thermal_list_lock); | |
1224 | } | |
1225 | return result; | |
1226 | } | |
1227 | ||
1228 | static void __exit thermal_exit(void) | |
1229 | { | |
1230 | class_unregister(&thermal_class); | |
1231 | idr_destroy(&thermal_tz_idr); | |
1232 | idr_destroy(&thermal_cdev_idr); | |
1233 | mutex_destroy(&thermal_idr_lock); | |
1234 | mutex_destroy(&thermal_list_lock); | |
1235 | } | |
1236 | ||
1237 | subsys_initcall(thermal_init); | |
1238 | module_exit(thermal_exit); |