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