]>
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> | |
33 | ||
34 | MODULE_AUTHOR("Zhang Rui") | |
35 | MODULE_DESCRIPTION("Generic thermal management sysfs support"); | |
36 | MODULE_LICENSE("GPL"); | |
37 | ||
38 | #define PREFIX "Thermal: " | |
39 | ||
40 | struct thermal_cooling_device_instance { | |
41 | int id; | |
42 | char name[THERMAL_NAME_LENGTH]; | |
43 | struct thermal_zone_device *tz; | |
44 | struct thermal_cooling_device *cdev; | |
45 | int trip; | |
46 | char attr_name[THERMAL_NAME_LENGTH]; | |
47 | struct device_attribute attr; | |
48 | struct list_head node; | |
49 | }; | |
50 | ||
51 | static DEFINE_IDR(thermal_tz_idr); | |
52 | static DEFINE_IDR(thermal_cdev_idr); | |
53 | static DEFINE_MUTEX(thermal_idr_lock); | |
54 | ||
55 | static LIST_HEAD(thermal_tz_list); | |
56 | static LIST_HEAD(thermal_cdev_list); | |
57 | static DEFINE_MUTEX(thermal_list_lock); | |
58 | ||
59 | static int get_idr(struct idr *idr, struct mutex *lock, int *id) | |
60 | { | |
61 | int err; | |
62 | ||
63 | again: | |
64 | if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0)) | |
65 | return -ENOMEM; | |
66 | ||
67 | if (lock) | |
68 | mutex_lock(lock); | |
69 | err = idr_get_new(idr, NULL, id); | |
70 | if (lock) | |
71 | mutex_unlock(lock); | |
72 | if (unlikely(err == -EAGAIN)) | |
73 | goto again; | |
74 | else if (unlikely(err)) | |
75 | return err; | |
76 | ||
77 | *id = *id & MAX_ID_MASK; | |
78 | return 0; | |
79 | } | |
80 | ||
81 | static void release_idr(struct idr *idr, struct mutex *lock, int id) | |
82 | { | |
83 | if (lock) | |
84 | mutex_lock(lock); | |
85 | idr_remove(idr, id); | |
86 | if (lock) | |
87 | mutex_unlock(lock); | |
88 | } | |
89 | ||
90 | /* sys I/F for thermal zone */ | |
91 | ||
92 | #define to_thermal_zone(_dev) \ | |
93 | container_of(_dev, struct thermal_zone_device, device) | |
94 | ||
95 | static ssize_t | |
96 | type_show(struct device *dev, struct device_attribute *attr, char *buf) | |
97 | { | |
98 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
99 | ||
100 | return sprintf(buf, "%s\n", tz->type); | |
101 | } | |
102 | ||
103 | static ssize_t | |
104 | temp_show(struct device *dev, struct device_attribute *attr, char *buf) | |
105 | { | |
106 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
107 | ||
108 | if (!tz->ops->get_temp) | |
109 | return -EPERM; | |
110 | ||
111 | return tz->ops->get_temp(tz, buf); | |
112 | } | |
113 | ||
114 | static ssize_t | |
115 | mode_show(struct device *dev, struct device_attribute *attr, char *buf) | |
116 | { | |
117 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
118 | ||
119 | if (!tz->ops->get_mode) | |
120 | return -EPERM; | |
121 | ||
122 | return tz->ops->get_mode(tz, buf); | |
123 | } | |
124 | ||
125 | static ssize_t | |
126 | mode_store(struct device *dev, struct device_attribute *attr, | |
127 | const char *buf, size_t count) | |
128 | { | |
129 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
130 | int result; | |
131 | ||
132 | if (!tz->ops->set_mode) | |
133 | return -EPERM; | |
134 | ||
135 | result = tz->ops->set_mode(tz, buf); | |
136 | if (result) | |
137 | return result; | |
138 | ||
139 | return count; | |
140 | } | |
141 | ||
142 | static ssize_t | |
143 | trip_point_type_show(struct device *dev, struct device_attribute *attr, | |
144 | char *buf) | |
145 | { | |
146 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
147 | int trip; | |
148 | ||
149 | if (!tz->ops->get_trip_type) | |
150 | return -EPERM; | |
151 | ||
152 | if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip)) | |
153 | return -EINVAL; | |
154 | ||
155 | return tz->ops->get_trip_type(tz, trip, buf); | |
156 | } | |
157 | ||
158 | static ssize_t | |
159 | trip_point_temp_show(struct device *dev, struct device_attribute *attr, | |
160 | char *buf) | |
161 | { | |
162 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
163 | int trip; | |
164 | ||
165 | if (!tz->ops->get_trip_temp) | |
166 | return -EPERM; | |
167 | ||
168 | if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip)) | |
169 | return -EINVAL; | |
170 | ||
171 | return tz->ops->get_trip_temp(tz, trip, buf); | |
172 | } | |
173 | ||
174 | static DEVICE_ATTR(type, 0444, type_show, NULL); | |
175 | static DEVICE_ATTR(temp, 0444, temp_show, NULL); | |
176 | static DEVICE_ATTR(mode, 0644, mode_show, mode_store); | |
177 | ||
178 | static struct device_attribute trip_point_attrs[] = { | |
179 | __ATTR(trip_point_0_type, 0444, trip_point_type_show, NULL), | |
180 | __ATTR(trip_point_0_temp, 0444, trip_point_temp_show, NULL), | |
181 | __ATTR(trip_point_1_type, 0444, trip_point_type_show, NULL), | |
182 | __ATTR(trip_point_1_temp, 0444, trip_point_temp_show, NULL), | |
183 | __ATTR(trip_point_2_type, 0444, trip_point_type_show, NULL), | |
184 | __ATTR(trip_point_2_temp, 0444, trip_point_temp_show, NULL), | |
185 | __ATTR(trip_point_3_type, 0444, trip_point_type_show, NULL), | |
186 | __ATTR(trip_point_3_temp, 0444, trip_point_temp_show, NULL), | |
187 | __ATTR(trip_point_4_type, 0444, trip_point_type_show, NULL), | |
188 | __ATTR(trip_point_4_temp, 0444, trip_point_temp_show, NULL), | |
189 | __ATTR(trip_point_5_type, 0444, trip_point_type_show, NULL), | |
190 | __ATTR(trip_point_5_temp, 0444, trip_point_temp_show, NULL), | |
191 | __ATTR(trip_point_6_type, 0444, trip_point_type_show, NULL), | |
192 | __ATTR(trip_point_6_temp, 0444, trip_point_temp_show, NULL), | |
193 | __ATTR(trip_point_7_type, 0444, trip_point_type_show, NULL), | |
194 | __ATTR(trip_point_7_temp, 0444, trip_point_temp_show, NULL), | |
195 | __ATTR(trip_point_8_type, 0444, trip_point_type_show, NULL), | |
196 | __ATTR(trip_point_8_temp, 0444, trip_point_temp_show, NULL), | |
197 | __ATTR(trip_point_9_type, 0444, trip_point_type_show, NULL), | |
198 | __ATTR(trip_point_9_temp, 0444, trip_point_temp_show, NULL), | |
5f1a3f2a KH |
199 | __ATTR(trip_point_10_type, 0444, trip_point_type_show, NULL), |
200 | __ATTR(trip_point_10_temp, 0444, trip_point_temp_show, NULL), | |
201 | __ATTR(trip_point_11_type, 0444, trip_point_type_show, NULL), | |
202 | __ATTR(trip_point_11_temp, 0444, trip_point_temp_show, NULL), | |
203d3d4a ZR |
203 | }; |
204 | ||
205 | #define TRIP_POINT_ATTR_ADD(_dev, _index, result) \ | |
206 | do { \ | |
207 | result = device_create_file(_dev, \ | |
208 | &trip_point_attrs[_index * 2]); \ | |
209 | if (result) \ | |
210 | break; \ | |
211 | result = device_create_file(_dev, \ | |
212 | &trip_point_attrs[_index * 2 + 1]); \ | |
213 | } while (0) | |
214 | ||
215 | #define TRIP_POINT_ATTR_REMOVE(_dev, _index) \ | |
216 | do { \ | |
217 | device_remove_file(_dev, &trip_point_attrs[_index * 2]); \ | |
218 | device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \ | |
219 | } while (0) | |
220 | ||
221 | /* sys I/F for cooling device */ | |
222 | #define to_cooling_device(_dev) \ | |
223 | container_of(_dev, struct thermal_cooling_device, device) | |
224 | ||
225 | static ssize_t | |
226 | thermal_cooling_device_type_show(struct device *dev, | |
227 | struct device_attribute *attr, char *buf) | |
228 | { | |
229 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
230 | ||
231 | return sprintf(buf, "%s\n", cdev->type); | |
232 | } | |
233 | ||
234 | static ssize_t | |
235 | thermal_cooling_device_max_state_show(struct device *dev, | |
236 | struct device_attribute *attr, char *buf) | |
237 | { | |
238 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
239 | ||
240 | return cdev->ops->get_max_state(cdev, buf); | |
241 | } | |
242 | ||
243 | static ssize_t | |
244 | thermal_cooling_device_cur_state_show(struct device *dev, | |
245 | struct device_attribute *attr, char *buf) | |
246 | { | |
247 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
248 | ||
249 | return cdev->ops->get_cur_state(cdev, buf); | |
250 | } | |
251 | ||
252 | static ssize_t | |
253 | thermal_cooling_device_cur_state_store(struct device *dev, | |
254 | struct device_attribute *attr, | |
255 | const char *buf, size_t count) | |
256 | { | |
257 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
258 | int state; | |
259 | int result; | |
260 | ||
261 | if (!sscanf(buf, "%d\n", &state)) | |
262 | return -EINVAL; | |
263 | ||
264 | if (state < 0) | |
265 | return -EINVAL; | |
266 | ||
267 | result = cdev->ops->set_cur_state(cdev, state); | |
268 | if (result) | |
269 | return result; | |
270 | return count; | |
271 | } | |
272 | ||
273 | static struct device_attribute dev_attr_cdev_type = | |
543a9561 | 274 | __ATTR(type, 0444, thermal_cooling_device_type_show, NULL); |
203d3d4a ZR |
275 | static DEVICE_ATTR(max_state, 0444, |
276 | thermal_cooling_device_max_state_show, NULL); | |
277 | static DEVICE_ATTR(cur_state, 0644, | |
278 | thermal_cooling_device_cur_state_show, | |
279 | thermal_cooling_device_cur_state_store); | |
280 | ||
281 | static ssize_t | |
282 | thermal_cooling_device_trip_point_show(struct device *dev, | |
543a9561 | 283 | struct device_attribute *attr, char *buf) |
203d3d4a ZR |
284 | { |
285 | struct thermal_cooling_device_instance *instance; | |
286 | ||
287 | instance = | |
288 | container_of(attr, struct thermal_cooling_device_instance, attr); | |
289 | ||
290 | if (instance->trip == THERMAL_TRIPS_NONE) | |
291 | return sprintf(buf, "-1\n"); | |
292 | else | |
293 | return sprintf(buf, "%d\n", instance->trip); | |
294 | } | |
295 | ||
296 | /* Device management */ | |
297 | ||
298 | /** | |
299 | * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone | |
203d3d4a ZR |
300 | * @tz: thermal zone device |
301 | * @trip: indicates which trip point the cooling devices is | |
302 | * associated with in this thermal zone. | |
303 | * @cdev: thermal cooling device | |
543a9561 LB |
304 | * |
305 | * This function is usually called in the thermal zone device .bind callback. | |
203d3d4a ZR |
306 | */ |
307 | int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, | |
308 | int trip, | |
309 | struct thermal_cooling_device *cdev) | |
310 | { | |
311 | struct thermal_cooling_device_instance *dev; | |
312 | struct thermal_cooling_device_instance *pos; | |
c7516709 TS |
313 | struct thermal_zone_device *pos1; |
314 | struct thermal_cooling_device *pos2; | |
203d3d4a ZR |
315 | int result; |
316 | ||
543a9561 | 317 | if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE)) |
203d3d4a ZR |
318 | return -EINVAL; |
319 | ||
c7516709 TS |
320 | list_for_each_entry(pos1, &thermal_tz_list, node) { |
321 | if (pos1 == tz) | |
322 | break; | |
323 | } | |
324 | list_for_each_entry(pos2, &thermal_cdev_list, node) { | |
325 | if (pos2 == cdev) | |
326 | break; | |
327 | } | |
328 | ||
329 | if (tz != pos1 || cdev != pos2) | |
203d3d4a ZR |
330 | return -EINVAL; |
331 | ||
332 | dev = | |
333 | kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL); | |
334 | if (!dev) | |
335 | return -ENOMEM; | |
336 | dev->tz = tz; | |
337 | dev->cdev = cdev; | |
338 | dev->trip = trip; | |
339 | result = get_idr(&tz->idr, &tz->lock, &dev->id); | |
340 | if (result) | |
341 | goto free_mem; | |
342 | ||
343 | sprintf(dev->name, "cdev%d", dev->id); | |
344 | result = | |
345 | sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name); | |
346 | if (result) | |
347 | goto release_idr; | |
348 | ||
349 | sprintf(dev->attr_name, "cdev%d_trip_point", dev->id); | |
350 | dev->attr.attr.name = dev->attr_name; | |
351 | dev->attr.attr.mode = 0444; | |
352 | dev->attr.show = thermal_cooling_device_trip_point_show; | |
353 | result = device_create_file(&tz->device, &dev->attr); | |
354 | if (result) | |
355 | goto remove_symbol_link; | |
356 | ||
357 | mutex_lock(&tz->lock); | |
358 | list_for_each_entry(pos, &tz->cooling_devices, node) | |
359 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { | |
360 | result = -EEXIST; | |
361 | break; | |
362 | } | |
363 | if (!result) | |
364 | list_add_tail(&dev->node, &tz->cooling_devices); | |
365 | mutex_unlock(&tz->lock); | |
366 | ||
367 | if (!result) | |
368 | return 0; | |
369 | ||
370 | device_remove_file(&tz->device, &dev->attr); | |
371 | remove_symbol_link: | |
372 | sysfs_remove_link(&tz->device.kobj, dev->name); | |
373 | release_idr: | |
374 | release_idr(&tz->idr, &tz->lock, dev->id); | |
375 | free_mem: | |
376 | kfree(dev); | |
377 | return result; | |
378 | } | |
543a9561 | 379 | |
203d3d4a ZR |
380 | EXPORT_SYMBOL(thermal_zone_bind_cooling_device); |
381 | ||
382 | /** | |
383 | * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone | |
203d3d4a ZR |
384 | * @tz: thermal zone device |
385 | * @trip: indicates which trip point the cooling devices is | |
386 | * associated with in this thermal zone. | |
387 | * @cdev: thermal cooling device | |
543a9561 LB |
388 | * |
389 | * This function is usually called in the thermal zone device .unbind callback. | |
203d3d4a ZR |
390 | */ |
391 | int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz, | |
392 | int trip, | |
393 | struct thermal_cooling_device *cdev) | |
394 | { | |
395 | struct thermal_cooling_device_instance *pos, *next; | |
396 | ||
397 | mutex_lock(&tz->lock); | |
398 | list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) { | |
543a9561 | 399 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
203d3d4a ZR |
400 | list_del(&pos->node); |
401 | mutex_unlock(&tz->lock); | |
402 | goto unbind; | |
403 | } | |
404 | } | |
405 | mutex_unlock(&tz->lock); | |
406 | ||
407 | return -ENODEV; | |
408 | ||
409 | unbind: | |
410 | device_remove_file(&tz->device, &pos->attr); | |
411 | sysfs_remove_link(&tz->device.kobj, pos->name); | |
412 | release_idr(&tz->idr, &tz->lock, pos->id); | |
413 | kfree(pos); | |
414 | return 0; | |
415 | } | |
543a9561 | 416 | |
203d3d4a ZR |
417 | EXPORT_SYMBOL(thermal_zone_unbind_cooling_device); |
418 | ||
419 | static void thermal_release(struct device *dev) | |
420 | { | |
421 | struct thermal_zone_device *tz; | |
422 | struct thermal_cooling_device *cdev; | |
423 | ||
424 | if (!strncmp(dev->bus_id, "thermal_zone", sizeof "thermal_zone" - 1)) { | |
425 | tz = to_thermal_zone(dev); | |
426 | kfree(tz); | |
427 | } else { | |
428 | cdev = to_cooling_device(dev); | |
429 | kfree(cdev); | |
430 | } | |
431 | } | |
432 | ||
433 | static struct class thermal_class = { | |
434 | .name = "thermal", | |
435 | .dev_release = thermal_release, | |
436 | }; | |
437 | ||
438 | /** | |
439 | * thermal_cooling_device_register - register a new thermal cooling device | |
440 | * @type: the thermal cooling device type. | |
441 | * @devdata: device private data. | |
442 | * @ops: standard thermal cooling devices callbacks. | |
443 | */ | |
444 | struct thermal_cooling_device *thermal_cooling_device_register(char *type, | |
543a9561 LB |
445 | void *devdata, |
446 | struct | |
447 | thermal_cooling_device_ops | |
448 | *ops) | |
203d3d4a ZR |
449 | { |
450 | struct thermal_cooling_device *cdev; | |
451 | struct thermal_zone_device *pos; | |
452 | int result; | |
453 | ||
454 | if (strlen(type) >= THERMAL_NAME_LENGTH) | |
3e6fda5c | 455 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
456 | |
457 | if (!ops || !ops->get_max_state || !ops->get_cur_state || | |
543a9561 | 458 | !ops->set_cur_state) |
3e6fda5c | 459 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
460 | |
461 | cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL); | |
462 | if (!cdev) | |
3e6fda5c | 463 | return ERR_PTR(-ENOMEM); |
203d3d4a ZR |
464 | |
465 | result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id); | |
466 | if (result) { | |
467 | kfree(cdev); | |
3e6fda5c | 468 | return ERR_PTR(result); |
203d3d4a ZR |
469 | } |
470 | ||
471 | strcpy(cdev->type, type); | |
472 | cdev->ops = ops; | |
473 | cdev->device.class = &thermal_class; | |
474 | cdev->devdata = devdata; | |
475 | sprintf(cdev->device.bus_id, "cooling_device%d", cdev->id); | |
476 | result = device_register(&cdev->device); | |
477 | if (result) { | |
478 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); | |
479 | kfree(cdev); | |
3e6fda5c | 480 | return ERR_PTR(result); |
203d3d4a ZR |
481 | } |
482 | ||
483 | /* sys I/F */ | |
484 | if (type) { | |
543a9561 | 485 | result = device_create_file(&cdev->device, &dev_attr_cdev_type); |
203d3d4a ZR |
486 | if (result) |
487 | goto unregister; | |
488 | } | |
489 | ||
490 | result = device_create_file(&cdev->device, &dev_attr_max_state); | |
491 | if (result) | |
492 | goto unregister; | |
493 | ||
494 | result = device_create_file(&cdev->device, &dev_attr_cur_state); | |
495 | if (result) | |
496 | goto unregister; | |
497 | ||
498 | mutex_lock(&thermal_list_lock); | |
499 | list_add(&cdev->node, &thermal_cdev_list); | |
500 | list_for_each_entry(pos, &thermal_tz_list, node) { | |
501 | if (!pos->ops->bind) | |
502 | continue; | |
503 | result = pos->ops->bind(pos, cdev); | |
504 | if (result) | |
505 | break; | |
506 | ||
507 | } | |
508 | mutex_unlock(&thermal_list_lock); | |
509 | ||
510 | if (!result) | |
511 | return cdev; | |
512 | ||
513 | unregister: | |
514 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); | |
515 | device_unregister(&cdev->device); | |
3e6fda5c | 516 | return ERR_PTR(result); |
203d3d4a | 517 | } |
543a9561 | 518 | |
203d3d4a ZR |
519 | EXPORT_SYMBOL(thermal_cooling_device_register); |
520 | ||
521 | /** | |
522 | * thermal_cooling_device_unregister - removes the registered thermal cooling device | |
203d3d4a ZR |
523 | * @cdev: the thermal cooling device to remove. |
524 | * | |
525 | * thermal_cooling_device_unregister() must be called when the device is no | |
526 | * longer needed. | |
527 | */ | |
528 | void thermal_cooling_device_unregister(struct | |
529 | thermal_cooling_device | |
530 | *cdev) | |
531 | { | |
532 | struct thermal_zone_device *tz; | |
533 | struct thermal_cooling_device *pos = NULL; | |
534 | ||
535 | if (!cdev) | |
536 | return; | |
537 | ||
538 | mutex_lock(&thermal_list_lock); | |
539 | list_for_each_entry(pos, &thermal_cdev_list, node) | |
540 | if (pos == cdev) | |
541 | break; | |
542 | if (pos != cdev) { | |
543 | /* thermal cooling device not found */ | |
544 | mutex_unlock(&thermal_list_lock); | |
545 | return; | |
546 | } | |
547 | list_del(&cdev->node); | |
548 | list_for_each_entry(tz, &thermal_tz_list, node) { | |
549 | if (!tz->ops->unbind) | |
550 | continue; | |
551 | tz->ops->unbind(tz, cdev); | |
552 | } | |
553 | mutex_unlock(&thermal_list_lock); | |
554 | if (cdev->type[0]) | |
543a9561 | 555 | device_remove_file(&cdev->device, &dev_attr_cdev_type); |
203d3d4a ZR |
556 | device_remove_file(&cdev->device, &dev_attr_max_state); |
557 | device_remove_file(&cdev->device, &dev_attr_cur_state); | |
558 | ||
559 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); | |
560 | device_unregister(&cdev->device); | |
561 | return; | |
562 | } | |
543a9561 | 563 | |
203d3d4a ZR |
564 | EXPORT_SYMBOL(thermal_cooling_device_unregister); |
565 | ||
566 | /** | |
567 | * thermal_zone_device_register - register a new thermal zone device | |
568 | * @type: the thermal zone device type | |
569 | * @trips: the number of trip points the thermal zone support | |
570 | * @devdata: private device data | |
571 | * @ops: standard thermal zone device callbacks | |
572 | * | |
573 | * thermal_zone_device_unregister() must be called when the device is no | |
574 | * longer needed. | |
575 | */ | |
576 | struct thermal_zone_device *thermal_zone_device_register(char *type, | |
543a9561 LB |
577 | int trips, |
578 | void *devdata, struct | |
579 | thermal_zone_device_ops | |
580 | *ops) | |
203d3d4a ZR |
581 | { |
582 | struct thermal_zone_device *tz; | |
583 | struct thermal_cooling_device *pos; | |
584 | int result; | |
585 | int count; | |
586 | ||
587 | if (strlen(type) >= THERMAL_NAME_LENGTH) | |
3e6fda5c | 588 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
589 | |
590 | if (trips > THERMAL_MAX_TRIPS || trips < 0) | |
3e6fda5c | 591 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
592 | |
593 | if (!ops || !ops->get_temp) | |
3e6fda5c | 594 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
595 | |
596 | tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL); | |
597 | if (!tz) | |
3e6fda5c | 598 | return ERR_PTR(-ENOMEM); |
203d3d4a ZR |
599 | |
600 | INIT_LIST_HEAD(&tz->cooling_devices); | |
601 | idr_init(&tz->idr); | |
602 | mutex_init(&tz->lock); | |
603 | result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id); | |
604 | if (result) { | |
605 | kfree(tz); | |
3e6fda5c | 606 | return ERR_PTR(result); |
203d3d4a ZR |
607 | } |
608 | ||
609 | strcpy(tz->type, type); | |
610 | tz->ops = ops; | |
611 | tz->device.class = &thermal_class; | |
612 | tz->devdata = devdata; | |
613 | tz->trips = trips; | |
614 | sprintf(tz->device.bus_id, "thermal_zone%d", tz->id); | |
615 | result = device_register(&tz->device); | |
616 | if (result) { | |
617 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); | |
618 | kfree(tz); | |
3e6fda5c | 619 | return ERR_PTR(result); |
203d3d4a ZR |
620 | } |
621 | ||
622 | /* sys I/F */ | |
623 | if (type) { | |
624 | result = device_create_file(&tz->device, &dev_attr_type); | |
625 | if (result) | |
626 | goto unregister; | |
627 | } | |
628 | ||
629 | result = device_create_file(&tz->device, &dev_attr_temp); | |
630 | if (result) | |
631 | goto unregister; | |
632 | ||
633 | if (ops->get_mode) { | |
634 | result = device_create_file(&tz->device, &dev_attr_mode); | |
635 | if (result) | |
636 | goto unregister; | |
637 | } | |
638 | ||
639 | for (count = 0; count < trips; count++) { | |
640 | TRIP_POINT_ATTR_ADD(&tz->device, count, result); | |
641 | if (result) | |
642 | goto unregister; | |
643 | } | |
644 | ||
645 | mutex_lock(&thermal_list_lock); | |
646 | list_add_tail(&tz->node, &thermal_tz_list); | |
647 | if (ops->bind) | |
648 | list_for_each_entry(pos, &thermal_cdev_list, node) { | |
543a9561 LB |
649 | result = ops->bind(tz, pos); |
650 | if (result) | |
651 | break; | |
203d3d4a ZR |
652 | } |
653 | mutex_unlock(&thermal_list_lock); | |
654 | ||
655 | if (!result) | |
656 | return tz; | |
657 | ||
658 | unregister: | |
659 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); | |
660 | device_unregister(&tz->device); | |
3e6fda5c | 661 | return ERR_PTR(result); |
203d3d4a | 662 | } |
543a9561 | 663 | |
203d3d4a ZR |
664 | EXPORT_SYMBOL(thermal_zone_device_register); |
665 | ||
666 | /** | |
667 | * thermal_device_unregister - removes the registered thermal zone device | |
203d3d4a ZR |
668 | * @tz: the thermal zone device to remove |
669 | */ | |
670 | void thermal_zone_device_unregister(struct thermal_zone_device *tz) | |
671 | { | |
672 | struct thermal_cooling_device *cdev; | |
673 | struct thermal_zone_device *pos = NULL; | |
674 | int count; | |
675 | ||
676 | if (!tz) | |
677 | return; | |
678 | ||
679 | mutex_lock(&thermal_list_lock); | |
680 | list_for_each_entry(pos, &thermal_tz_list, node) | |
681 | if (pos == tz) | |
682 | break; | |
683 | if (pos != tz) { | |
684 | /* thermal zone device not found */ | |
685 | mutex_unlock(&thermal_list_lock); | |
686 | return; | |
687 | } | |
688 | list_del(&tz->node); | |
689 | if (tz->ops->unbind) | |
690 | list_for_each_entry(cdev, &thermal_cdev_list, node) | |
691 | tz->ops->unbind(tz, cdev); | |
692 | mutex_unlock(&thermal_list_lock); | |
693 | ||
694 | if (tz->type[0]) | |
695 | device_remove_file(&tz->device, &dev_attr_type); | |
696 | device_remove_file(&tz->device, &dev_attr_temp); | |
697 | if (tz->ops->get_mode) | |
698 | device_remove_file(&tz->device, &dev_attr_mode); | |
699 | ||
700 | for (count = 0; count < tz->trips; count++) | |
701 | TRIP_POINT_ATTR_REMOVE(&tz->device, count); | |
702 | ||
703 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); | |
704 | idr_destroy(&tz->idr); | |
705 | mutex_destroy(&tz->lock); | |
706 | device_unregister(&tz->device); | |
707 | return; | |
708 | } | |
543a9561 | 709 | |
203d3d4a ZR |
710 | EXPORT_SYMBOL(thermal_zone_device_unregister); |
711 | ||
712 | static int __init thermal_init(void) | |
713 | { | |
714 | int result = 0; | |
715 | ||
716 | result = class_register(&thermal_class); | |
717 | if (result) { | |
718 | idr_destroy(&thermal_tz_idr); | |
719 | idr_destroy(&thermal_cdev_idr); | |
720 | mutex_destroy(&thermal_idr_lock); | |
721 | mutex_destroy(&thermal_list_lock); | |
722 | } | |
723 | return result; | |
724 | } | |
725 | ||
726 | static void __exit thermal_exit(void) | |
727 | { | |
728 | class_unregister(&thermal_class); | |
729 | idr_destroy(&thermal_tz_idr); | |
730 | idr_destroy(&thermal_cdev_idr); | |
731 | mutex_destroy(&thermal_idr_lock); | |
732 | mutex_destroy(&thermal_list_lock); | |
733 | } | |
734 | ||
735 | subsys_initcall(thermal_init); | |
736 | module_exit(thermal_exit); |