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 | ||
c5a01dd5 JP |
26 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
27 | ||
203d3d4a ZR |
28 | #include <linux/module.h> |
29 | #include <linux/device.h> | |
30 | #include <linux/err.h> | |
5a0e3ad6 | 31 | #include <linux/slab.h> |
203d3d4a ZR |
32 | #include <linux/kdev_t.h> |
33 | #include <linux/idr.h> | |
34 | #include <linux/thermal.h> | |
35 | #include <linux/spinlock.h> | |
b1569e99 | 36 | #include <linux/reboot.h> |
4cb18728 D |
37 | #include <net/netlink.h> |
38 | #include <net/genetlink.h> | |
203d3d4a | 39 | |
71350db4 D |
40 | #include "thermal_core.h" |
41 | ||
63c4ec90 | 42 | MODULE_AUTHOR("Zhang Rui"); |
203d3d4a ZR |
43 | MODULE_DESCRIPTION("Generic thermal management sysfs support"); |
44 | MODULE_LICENSE("GPL"); | |
45 | ||
203d3d4a ZR |
46 | static DEFINE_IDR(thermal_tz_idr); |
47 | static DEFINE_IDR(thermal_cdev_idr); | |
48 | static DEFINE_MUTEX(thermal_idr_lock); | |
49 | ||
50 | static LIST_HEAD(thermal_tz_list); | |
51 | static LIST_HEAD(thermal_cdev_list); | |
a4a15485 D |
52 | static LIST_HEAD(thermal_governor_list); |
53 | ||
203d3d4a | 54 | static DEFINE_MUTEX(thermal_list_lock); |
a4a15485 D |
55 | static DEFINE_MUTEX(thermal_governor_lock); |
56 | ||
57 | static struct thermal_governor *__find_governor(const char *name) | |
58 | { | |
59 | struct thermal_governor *pos; | |
60 | ||
61 | list_for_each_entry(pos, &thermal_governor_list, governor_list) | |
62 | if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH)) | |
63 | return pos; | |
64 | ||
65 | return NULL; | |
66 | } | |
67 | ||
68 | int thermal_register_governor(struct thermal_governor *governor) | |
69 | { | |
70 | int err; | |
71 | const char *name; | |
72 | struct thermal_zone_device *pos; | |
73 | ||
74 | if (!governor) | |
75 | return -EINVAL; | |
76 | ||
77 | mutex_lock(&thermal_governor_lock); | |
78 | ||
79 | err = -EBUSY; | |
80 | if (__find_governor(governor->name) == NULL) { | |
81 | err = 0; | |
82 | list_add(&governor->governor_list, &thermal_governor_list); | |
83 | } | |
84 | ||
85 | mutex_lock(&thermal_list_lock); | |
86 | ||
87 | list_for_each_entry(pos, &thermal_tz_list, node) { | |
88 | if (pos->governor) | |
89 | continue; | |
90 | if (pos->tzp) | |
91 | name = pos->tzp->governor_name; | |
92 | else | |
93 | name = DEFAULT_THERMAL_GOVERNOR; | |
94 | if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH)) | |
95 | pos->governor = governor; | |
96 | } | |
97 | ||
98 | mutex_unlock(&thermal_list_lock); | |
99 | mutex_unlock(&thermal_governor_lock); | |
100 | ||
101 | return err; | |
102 | } | |
103 | EXPORT_SYMBOL_GPL(thermal_register_governor); | |
104 | ||
105 | void thermal_unregister_governor(struct thermal_governor *governor) | |
106 | { | |
107 | struct thermal_zone_device *pos; | |
108 | ||
109 | if (!governor) | |
110 | return; | |
111 | ||
112 | mutex_lock(&thermal_governor_lock); | |
113 | ||
114 | if (__find_governor(governor->name) == NULL) | |
115 | goto exit; | |
116 | ||
117 | mutex_lock(&thermal_list_lock); | |
118 | ||
119 | list_for_each_entry(pos, &thermal_tz_list, node) { | |
120 | if (!strnicmp(pos->governor->name, governor->name, | |
121 | THERMAL_NAME_LENGTH)) | |
122 | pos->governor = NULL; | |
123 | } | |
124 | ||
125 | mutex_unlock(&thermal_list_lock); | |
126 | list_del(&governor->governor_list); | |
127 | exit: | |
128 | mutex_unlock(&thermal_governor_lock); | |
129 | return; | |
130 | } | |
131 | EXPORT_SYMBOL_GPL(thermal_unregister_governor); | |
203d3d4a ZR |
132 | |
133 | static int get_idr(struct idr *idr, struct mutex *lock, int *id) | |
134 | { | |
135 | int err; | |
136 | ||
caca8b80 | 137 | again: |
203d3d4a ZR |
138 | if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0)) |
139 | return -ENOMEM; | |
140 | ||
141 | if (lock) | |
142 | mutex_lock(lock); | |
143 | err = idr_get_new(idr, NULL, id); | |
144 | if (lock) | |
145 | mutex_unlock(lock); | |
146 | if (unlikely(err == -EAGAIN)) | |
147 | goto again; | |
148 | else if (unlikely(err)) | |
149 | return err; | |
150 | ||
125c4c70 | 151 | *id = *id & MAX_IDR_MASK; |
203d3d4a ZR |
152 | return 0; |
153 | } | |
154 | ||
155 | static void release_idr(struct idr *idr, struct mutex *lock, int id) | |
156 | { | |
157 | if (lock) | |
158 | mutex_lock(lock); | |
159 | idr_remove(idr, id); | |
160 | if (lock) | |
161 | mutex_unlock(lock); | |
162 | } | |
163 | ||
9b4298a0 D |
164 | int get_tz_trend(struct thermal_zone_device *tz, int trip) |
165 | { | |
166 | enum thermal_trend trend; | |
167 | ||
168 | if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) { | |
169 | if (tz->temperature > tz->last_temperature) | |
170 | trend = THERMAL_TREND_RAISING; | |
171 | else if (tz->temperature < tz->last_temperature) | |
172 | trend = THERMAL_TREND_DROPPING; | |
173 | else | |
174 | trend = THERMAL_TREND_STABLE; | |
175 | } | |
176 | ||
177 | return trend; | |
178 | } | |
179 | EXPORT_SYMBOL(get_tz_trend); | |
180 | ||
181 | struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz, | |
182 | struct thermal_cooling_device *cdev, int trip) | |
183 | { | |
184 | struct thermal_instance *pos = NULL; | |
185 | struct thermal_instance *target_instance = NULL; | |
186 | ||
187 | mutex_lock(&tz->lock); | |
188 | mutex_lock(&cdev->lock); | |
189 | ||
190 | list_for_each_entry(pos, &tz->thermal_instances, tz_node) { | |
191 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { | |
192 | target_instance = pos; | |
193 | break; | |
194 | } | |
195 | } | |
196 | ||
197 | mutex_unlock(&cdev->lock); | |
198 | mutex_unlock(&tz->lock); | |
199 | ||
200 | return target_instance; | |
201 | } | |
202 | EXPORT_SYMBOL(get_thermal_instance); | |
203 | ||
203d3d4a ZR |
204 | /* sys I/F for thermal zone */ |
205 | ||
206 | #define to_thermal_zone(_dev) \ | |
207 | container_of(_dev, struct thermal_zone_device, device) | |
208 | ||
209 | static ssize_t | |
210 | type_show(struct device *dev, struct device_attribute *attr, char *buf) | |
211 | { | |
212 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
213 | ||
214 | return sprintf(buf, "%s\n", tz->type); | |
215 | } | |
216 | ||
217 | static ssize_t | |
218 | temp_show(struct device *dev, struct device_attribute *attr, char *buf) | |
219 | { | |
220 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
6503e5df MG |
221 | long temperature; |
222 | int ret; | |
203d3d4a ZR |
223 | |
224 | if (!tz->ops->get_temp) | |
225 | return -EPERM; | |
226 | ||
6503e5df MG |
227 | ret = tz->ops->get_temp(tz, &temperature); |
228 | ||
229 | if (ret) | |
230 | return ret; | |
231 | ||
232 | return sprintf(buf, "%ld\n", temperature); | |
203d3d4a ZR |
233 | } |
234 | ||
235 | static ssize_t | |
236 | mode_show(struct device *dev, struct device_attribute *attr, char *buf) | |
237 | { | |
238 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
6503e5df MG |
239 | enum thermal_device_mode mode; |
240 | int result; | |
203d3d4a ZR |
241 | |
242 | if (!tz->ops->get_mode) | |
243 | return -EPERM; | |
244 | ||
6503e5df MG |
245 | result = tz->ops->get_mode(tz, &mode); |
246 | if (result) | |
247 | return result; | |
248 | ||
249 | return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled" | |
250 | : "disabled"); | |
203d3d4a ZR |
251 | } |
252 | ||
253 | static ssize_t | |
254 | mode_store(struct device *dev, struct device_attribute *attr, | |
255 | const char *buf, size_t count) | |
256 | { | |
257 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
258 | int result; | |
259 | ||
260 | if (!tz->ops->set_mode) | |
261 | return -EPERM; | |
262 | ||
f1f0e2ac | 263 | if (!strncmp(buf, "enabled", sizeof("enabled") - 1)) |
6503e5df | 264 | result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED); |
f1f0e2ac | 265 | else if (!strncmp(buf, "disabled", sizeof("disabled") - 1)) |
6503e5df MG |
266 | result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED); |
267 | else | |
268 | result = -EINVAL; | |
269 | ||
203d3d4a ZR |
270 | if (result) |
271 | return result; | |
272 | ||
273 | return count; | |
274 | } | |
275 | ||
276 | static ssize_t | |
277 | trip_point_type_show(struct device *dev, struct device_attribute *attr, | |
278 | char *buf) | |
279 | { | |
280 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
6503e5df MG |
281 | enum thermal_trip_type type; |
282 | int trip, result; | |
203d3d4a ZR |
283 | |
284 | if (!tz->ops->get_trip_type) | |
285 | return -EPERM; | |
286 | ||
287 | if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip)) | |
288 | return -EINVAL; | |
289 | ||
6503e5df MG |
290 | result = tz->ops->get_trip_type(tz, trip, &type); |
291 | if (result) | |
292 | return result; | |
293 | ||
294 | switch (type) { | |
295 | case THERMAL_TRIP_CRITICAL: | |
625120a4 | 296 | return sprintf(buf, "critical\n"); |
6503e5df | 297 | case THERMAL_TRIP_HOT: |
625120a4 | 298 | return sprintf(buf, "hot\n"); |
6503e5df | 299 | case THERMAL_TRIP_PASSIVE: |
625120a4 | 300 | return sprintf(buf, "passive\n"); |
6503e5df | 301 | case THERMAL_TRIP_ACTIVE: |
625120a4 | 302 | return sprintf(buf, "active\n"); |
6503e5df | 303 | default: |
625120a4 | 304 | return sprintf(buf, "unknown\n"); |
6503e5df | 305 | } |
203d3d4a ZR |
306 | } |
307 | ||
c56f5c03 D |
308 | static ssize_t |
309 | trip_point_temp_store(struct device *dev, struct device_attribute *attr, | |
310 | const char *buf, size_t count) | |
311 | { | |
312 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
313 | int trip, ret; | |
314 | unsigned long temperature; | |
315 | ||
316 | if (!tz->ops->set_trip_temp) | |
317 | return -EPERM; | |
318 | ||
319 | if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip)) | |
320 | return -EINVAL; | |
321 | ||
322 | if (kstrtoul(buf, 10, &temperature)) | |
323 | return -EINVAL; | |
324 | ||
325 | ret = tz->ops->set_trip_temp(tz, trip, temperature); | |
326 | ||
327 | return ret ? ret : count; | |
328 | } | |
329 | ||
203d3d4a ZR |
330 | static ssize_t |
331 | trip_point_temp_show(struct device *dev, struct device_attribute *attr, | |
332 | char *buf) | |
333 | { | |
334 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
6503e5df MG |
335 | int trip, ret; |
336 | long temperature; | |
203d3d4a ZR |
337 | |
338 | if (!tz->ops->get_trip_temp) | |
339 | return -EPERM; | |
340 | ||
341 | if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip)) | |
342 | return -EINVAL; | |
343 | ||
6503e5df MG |
344 | ret = tz->ops->get_trip_temp(tz, trip, &temperature); |
345 | ||
346 | if (ret) | |
347 | return ret; | |
348 | ||
349 | return sprintf(buf, "%ld\n", temperature); | |
203d3d4a ZR |
350 | } |
351 | ||
27365a6c D |
352 | static ssize_t |
353 | trip_point_hyst_store(struct device *dev, struct device_attribute *attr, | |
354 | const char *buf, size_t count) | |
355 | { | |
356 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
357 | int trip, ret; | |
358 | unsigned long temperature; | |
359 | ||
360 | if (!tz->ops->set_trip_hyst) | |
361 | return -EPERM; | |
362 | ||
363 | if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip)) | |
364 | return -EINVAL; | |
365 | ||
366 | if (kstrtoul(buf, 10, &temperature)) | |
367 | return -EINVAL; | |
368 | ||
369 | /* | |
370 | * We are not doing any check on the 'temperature' value | |
371 | * here. The driver implementing 'set_trip_hyst' has to | |
372 | * take care of this. | |
373 | */ | |
374 | ret = tz->ops->set_trip_hyst(tz, trip, temperature); | |
375 | ||
376 | return ret ? ret : count; | |
377 | } | |
378 | ||
379 | static ssize_t | |
380 | trip_point_hyst_show(struct device *dev, struct device_attribute *attr, | |
381 | char *buf) | |
382 | { | |
383 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
384 | int trip, ret; | |
385 | unsigned long temperature; | |
386 | ||
387 | if (!tz->ops->get_trip_hyst) | |
388 | return -EPERM; | |
389 | ||
390 | if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip)) | |
391 | return -EINVAL; | |
392 | ||
393 | ret = tz->ops->get_trip_hyst(tz, trip, &temperature); | |
394 | ||
395 | return ret ? ret : sprintf(buf, "%ld\n", temperature); | |
396 | } | |
397 | ||
03a971a2 MG |
398 | static ssize_t |
399 | passive_store(struct device *dev, struct device_attribute *attr, | |
400 | const char *buf, size_t count) | |
401 | { | |
402 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
403 | struct thermal_cooling_device *cdev = NULL; | |
404 | int state; | |
405 | ||
406 | if (!sscanf(buf, "%d\n", &state)) | |
407 | return -EINVAL; | |
408 | ||
3d8e3ad8 FP |
409 | /* sanity check: values below 1000 millicelcius don't make sense |
410 | * and can cause the system to go into a thermal heart attack | |
411 | */ | |
412 | if (state && state < 1000) | |
413 | return -EINVAL; | |
414 | ||
03a971a2 MG |
415 | if (state && !tz->forced_passive) { |
416 | mutex_lock(&thermal_list_lock); | |
417 | list_for_each_entry(cdev, &thermal_cdev_list, node) { | |
418 | if (!strncmp("Processor", cdev->type, | |
419 | sizeof("Processor"))) | |
420 | thermal_zone_bind_cooling_device(tz, | |
9d99842f ZR |
421 | THERMAL_TRIPS_NONE, cdev, |
422 | THERMAL_NO_LIMIT, | |
423 | THERMAL_NO_LIMIT); | |
03a971a2 MG |
424 | } |
425 | mutex_unlock(&thermal_list_lock); | |
e4143b03 FP |
426 | if (!tz->passive_delay) |
427 | tz->passive_delay = 1000; | |
03a971a2 MG |
428 | } else if (!state && tz->forced_passive) { |
429 | mutex_lock(&thermal_list_lock); | |
430 | list_for_each_entry(cdev, &thermal_cdev_list, node) { | |
431 | if (!strncmp("Processor", cdev->type, | |
432 | sizeof("Processor"))) | |
433 | thermal_zone_unbind_cooling_device(tz, | |
434 | THERMAL_TRIPS_NONE, | |
435 | cdev); | |
436 | } | |
437 | mutex_unlock(&thermal_list_lock); | |
e4143b03 | 438 | tz->passive_delay = 0; |
03a971a2 MG |
439 | } |
440 | ||
03a971a2 MG |
441 | tz->forced_passive = state; |
442 | ||
443 | thermal_zone_device_update(tz); | |
444 | ||
445 | return count; | |
446 | } | |
447 | ||
448 | static ssize_t | |
449 | passive_show(struct device *dev, struct device_attribute *attr, | |
450 | char *buf) | |
451 | { | |
452 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
453 | ||
454 | return sprintf(buf, "%d\n", tz->forced_passive); | |
455 | } | |
456 | ||
5a2c090b D |
457 | static ssize_t |
458 | policy_store(struct device *dev, struct device_attribute *attr, | |
459 | const char *buf, size_t count) | |
460 | { | |
461 | int ret = -EINVAL; | |
462 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
463 | struct thermal_governor *gov; | |
464 | ||
465 | mutex_lock(&thermal_governor_lock); | |
466 | ||
467 | gov = __find_governor(buf); | |
468 | if (!gov) | |
469 | goto exit; | |
470 | ||
471 | tz->governor = gov; | |
472 | ret = count; | |
473 | ||
474 | exit: | |
475 | mutex_unlock(&thermal_governor_lock); | |
476 | return ret; | |
477 | } | |
478 | ||
479 | static ssize_t | |
480 | policy_show(struct device *dev, struct device_attribute *devattr, char *buf) | |
481 | { | |
482 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
483 | ||
484 | return sprintf(buf, "%s\n", tz->governor->name); | |
485 | } | |
486 | ||
203d3d4a ZR |
487 | static DEVICE_ATTR(type, 0444, type_show, NULL); |
488 | static DEVICE_ATTR(temp, 0444, temp_show, NULL); | |
489 | static DEVICE_ATTR(mode, 0644, mode_show, mode_store); | |
886ee546 | 490 | static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store); |
5a2c090b | 491 | static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store); |
203d3d4a | 492 | |
203d3d4a ZR |
493 | /* sys I/F for cooling device */ |
494 | #define to_cooling_device(_dev) \ | |
495 | container_of(_dev, struct thermal_cooling_device, device) | |
496 | ||
497 | static ssize_t | |
498 | thermal_cooling_device_type_show(struct device *dev, | |
499 | struct device_attribute *attr, char *buf) | |
500 | { | |
501 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
502 | ||
503 | return sprintf(buf, "%s\n", cdev->type); | |
504 | } | |
505 | ||
506 | static ssize_t | |
507 | thermal_cooling_device_max_state_show(struct device *dev, | |
508 | struct device_attribute *attr, char *buf) | |
509 | { | |
510 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
6503e5df MG |
511 | unsigned long state; |
512 | int ret; | |
203d3d4a | 513 | |
6503e5df MG |
514 | ret = cdev->ops->get_max_state(cdev, &state); |
515 | if (ret) | |
516 | return ret; | |
517 | return sprintf(buf, "%ld\n", state); | |
203d3d4a ZR |
518 | } |
519 | ||
520 | static ssize_t | |
521 | thermal_cooling_device_cur_state_show(struct device *dev, | |
522 | struct device_attribute *attr, char *buf) | |
523 | { | |
524 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
6503e5df MG |
525 | unsigned long state; |
526 | int ret; | |
203d3d4a | 527 | |
6503e5df MG |
528 | ret = cdev->ops->get_cur_state(cdev, &state); |
529 | if (ret) | |
530 | return ret; | |
531 | return sprintf(buf, "%ld\n", state); | |
203d3d4a ZR |
532 | } |
533 | ||
534 | static ssize_t | |
535 | thermal_cooling_device_cur_state_store(struct device *dev, | |
536 | struct device_attribute *attr, | |
537 | const char *buf, size_t count) | |
538 | { | |
539 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
6503e5df | 540 | unsigned long state; |
203d3d4a ZR |
541 | int result; |
542 | ||
6503e5df | 543 | if (!sscanf(buf, "%ld\n", &state)) |
203d3d4a ZR |
544 | return -EINVAL; |
545 | ||
edb94918 | 546 | if ((long)state < 0) |
203d3d4a ZR |
547 | return -EINVAL; |
548 | ||
549 | result = cdev->ops->set_cur_state(cdev, state); | |
550 | if (result) | |
551 | return result; | |
552 | return count; | |
553 | } | |
554 | ||
555 | static struct device_attribute dev_attr_cdev_type = | |
543a9561 | 556 | __ATTR(type, 0444, thermal_cooling_device_type_show, NULL); |
203d3d4a ZR |
557 | static DEVICE_ATTR(max_state, 0444, |
558 | thermal_cooling_device_max_state_show, NULL); | |
559 | static DEVICE_ATTR(cur_state, 0644, | |
560 | thermal_cooling_device_cur_state_show, | |
561 | thermal_cooling_device_cur_state_store); | |
562 | ||
563 | static ssize_t | |
564 | thermal_cooling_device_trip_point_show(struct device *dev, | |
543a9561 | 565 | struct device_attribute *attr, char *buf) |
203d3d4a | 566 | { |
b81b6ba3 | 567 | struct thermal_instance *instance; |
203d3d4a ZR |
568 | |
569 | instance = | |
b81b6ba3 | 570 | container_of(attr, struct thermal_instance, attr); |
203d3d4a ZR |
571 | |
572 | if (instance->trip == THERMAL_TRIPS_NONE) | |
573 | return sprintf(buf, "-1\n"); | |
574 | else | |
575 | return sprintf(buf, "%d\n", instance->trip); | |
576 | } | |
577 | ||
578 | /* Device management */ | |
579 | ||
16d75239 RH |
580 | #if defined(CONFIG_THERMAL_HWMON) |
581 | ||
e68b16ab ZR |
582 | /* hwmon sys I/F */ |
583 | #include <linux/hwmon.h> | |
31f5396a JD |
584 | |
585 | /* thermal zone devices with the same type share one hwmon device */ | |
586 | struct thermal_hwmon_device { | |
587 | char type[THERMAL_NAME_LENGTH]; | |
588 | struct device *device; | |
589 | int count; | |
590 | struct list_head tz_list; | |
591 | struct list_head node; | |
592 | }; | |
593 | ||
594 | struct thermal_hwmon_attr { | |
595 | struct device_attribute attr; | |
596 | char name[16]; | |
597 | }; | |
598 | ||
599 | /* one temperature input for each thermal zone */ | |
600 | struct thermal_hwmon_temp { | |
601 | struct list_head hwmon_node; | |
602 | struct thermal_zone_device *tz; | |
603 | struct thermal_hwmon_attr temp_input; /* hwmon sys attr */ | |
604 | struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */ | |
605 | }; | |
606 | ||
e68b16ab ZR |
607 | static LIST_HEAD(thermal_hwmon_list); |
608 | ||
609 | static ssize_t | |
610 | name_show(struct device *dev, struct device_attribute *attr, char *buf) | |
611 | { | |
0e968a3b | 612 | struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev); |
e68b16ab ZR |
613 | return sprintf(buf, "%s\n", hwmon->type); |
614 | } | |
615 | static DEVICE_ATTR(name, 0444, name_show, NULL); | |
616 | ||
617 | static ssize_t | |
618 | temp_input_show(struct device *dev, struct device_attribute *attr, char *buf) | |
619 | { | |
6503e5df MG |
620 | long temperature; |
621 | int ret; | |
e68b16ab ZR |
622 | struct thermal_hwmon_attr *hwmon_attr |
623 | = container_of(attr, struct thermal_hwmon_attr, attr); | |
31f5396a JD |
624 | struct thermal_hwmon_temp *temp |
625 | = container_of(hwmon_attr, struct thermal_hwmon_temp, | |
e68b16ab | 626 | temp_input); |
31f5396a | 627 | struct thermal_zone_device *tz = temp->tz; |
e68b16ab | 628 | |
6503e5df MG |
629 | ret = tz->ops->get_temp(tz, &temperature); |
630 | ||
631 | if (ret) | |
632 | return ret; | |
633 | ||
634 | return sprintf(buf, "%ld\n", temperature); | |
e68b16ab ZR |
635 | } |
636 | ||
637 | static ssize_t | |
638 | temp_crit_show(struct device *dev, struct device_attribute *attr, | |
639 | char *buf) | |
640 | { | |
641 | struct thermal_hwmon_attr *hwmon_attr | |
642 | = container_of(attr, struct thermal_hwmon_attr, attr); | |
31f5396a JD |
643 | struct thermal_hwmon_temp *temp |
644 | = container_of(hwmon_attr, struct thermal_hwmon_temp, | |
e68b16ab | 645 | temp_crit); |
31f5396a | 646 | struct thermal_zone_device *tz = temp->tz; |
6503e5df MG |
647 | long temperature; |
648 | int ret; | |
649 | ||
650 | ret = tz->ops->get_trip_temp(tz, 0, &temperature); | |
651 | if (ret) | |
652 | return ret; | |
e68b16ab | 653 | |
6503e5df | 654 | return sprintf(buf, "%ld\n", temperature); |
e68b16ab ZR |
655 | } |
656 | ||
657 | ||
0d97d7a4 JD |
658 | static struct thermal_hwmon_device * |
659 | thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz) | |
e68b16ab ZR |
660 | { |
661 | struct thermal_hwmon_device *hwmon; | |
e68b16ab ZR |
662 | |
663 | mutex_lock(&thermal_list_lock); | |
664 | list_for_each_entry(hwmon, &thermal_hwmon_list, node) | |
665 | if (!strcmp(hwmon->type, tz->type)) { | |
e68b16ab | 666 | mutex_unlock(&thermal_list_lock); |
0d97d7a4 | 667 | return hwmon; |
e68b16ab ZR |
668 | } |
669 | mutex_unlock(&thermal_list_lock); | |
670 | ||
0d97d7a4 JD |
671 | return NULL; |
672 | } | |
673 | ||
31f5396a JD |
674 | /* Find the temperature input matching a given thermal zone */ |
675 | static struct thermal_hwmon_temp * | |
676 | thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon, | |
677 | const struct thermal_zone_device *tz) | |
678 | { | |
679 | struct thermal_hwmon_temp *temp; | |
680 | ||
681 | mutex_lock(&thermal_list_lock); | |
682 | list_for_each_entry(temp, &hwmon->tz_list, hwmon_node) | |
683 | if (temp->tz == tz) { | |
684 | mutex_unlock(&thermal_list_lock); | |
685 | return temp; | |
686 | } | |
687 | mutex_unlock(&thermal_list_lock); | |
688 | ||
689 | return NULL; | |
690 | } | |
691 | ||
0d97d7a4 JD |
692 | static int |
693 | thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) | |
694 | { | |
695 | struct thermal_hwmon_device *hwmon; | |
31f5396a | 696 | struct thermal_hwmon_temp *temp; |
0d97d7a4 JD |
697 | int new_hwmon_device = 1; |
698 | int result; | |
699 | ||
700 | hwmon = thermal_hwmon_lookup_by_type(tz); | |
701 | if (hwmon) { | |
702 | new_hwmon_device = 0; | |
703 | goto register_sys_interface; | |
704 | } | |
705 | ||
e68b16ab ZR |
706 | hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL); |
707 | if (!hwmon) | |
708 | return -ENOMEM; | |
709 | ||
710 | INIT_LIST_HEAD(&hwmon->tz_list); | |
711 | strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH); | |
712 | hwmon->device = hwmon_device_register(NULL); | |
713 | if (IS_ERR(hwmon->device)) { | |
714 | result = PTR_ERR(hwmon->device); | |
715 | goto free_mem; | |
716 | } | |
0e968a3b | 717 | dev_set_drvdata(hwmon->device, hwmon); |
e68b16ab ZR |
718 | result = device_create_file(hwmon->device, &dev_attr_name); |
719 | if (result) | |
b299eb5c | 720 | goto free_mem; |
e68b16ab ZR |
721 | |
722 | register_sys_interface: | |
31f5396a JD |
723 | temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL); |
724 | if (!temp) { | |
725 | result = -ENOMEM; | |
726 | goto unregister_name; | |
727 | } | |
728 | ||
729 | temp->tz = tz; | |
e68b16ab ZR |
730 | hwmon->count++; |
731 | ||
79a49168 | 732 | snprintf(temp->temp_input.name, sizeof(temp->temp_input.name), |
e68b16ab | 733 | "temp%d_input", hwmon->count); |
31f5396a JD |
734 | temp->temp_input.attr.attr.name = temp->temp_input.name; |
735 | temp->temp_input.attr.attr.mode = 0444; | |
736 | temp->temp_input.attr.show = temp_input_show; | |
737 | sysfs_attr_init(&temp->temp_input.attr.attr); | |
738 | result = device_create_file(hwmon->device, &temp->temp_input.attr); | |
e68b16ab | 739 | if (result) |
31f5396a | 740 | goto free_temp_mem; |
e68b16ab ZR |
741 | |
742 | if (tz->ops->get_crit_temp) { | |
743 | unsigned long temperature; | |
744 | if (!tz->ops->get_crit_temp(tz, &temperature)) { | |
79a49168 GR |
745 | snprintf(temp->temp_crit.name, |
746 | sizeof(temp->temp_crit.name), | |
e68b16ab | 747 | "temp%d_crit", hwmon->count); |
31f5396a JD |
748 | temp->temp_crit.attr.attr.name = temp->temp_crit.name; |
749 | temp->temp_crit.attr.attr.mode = 0444; | |
750 | temp->temp_crit.attr.show = temp_crit_show; | |
751 | sysfs_attr_init(&temp->temp_crit.attr.attr); | |
e68b16ab | 752 | result = device_create_file(hwmon->device, |
31f5396a | 753 | &temp->temp_crit.attr); |
e68b16ab | 754 | if (result) |
b299eb5c | 755 | goto unregister_input; |
e68b16ab ZR |
756 | } |
757 | } | |
758 | ||
759 | mutex_lock(&thermal_list_lock); | |
760 | if (new_hwmon_device) | |
761 | list_add_tail(&hwmon->node, &thermal_hwmon_list); | |
31f5396a | 762 | list_add_tail(&temp->hwmon_node, &hwmon->tz_list); |
e68b16ab ZR |
763 | mutex_unlock(&thermal_list_lock); |
764 | ||
765 | return 0; | |
766 | ||
b299eb5c | 767 | unregister_input: |
31f5396a JD |
768 | device_remove_file(hwmon->device, &temp->temp_input.attr); |
769 | free_temp_mem: | |
770 | kfree(temp); | |
b299eb5c | 771 | unregister_name: |
e68b16ab ZR |
772 | if (new_hwmon_device) { |
773 | device_remove_file(hwmon->device, &dev_attr_name); | |
774 | hwmon_device_unregister(hwmon->device); | |
775 | } | |
776 | free_mem: | |
777 | if (new_hwmon_device) | |
778 | kfree(hwmon); | |
779 | ||
780 | return result; | |
781 | } | |
782 | ||
783 | static void | |
784 | thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz) | |
785 | { | |
31f5396a JD |
786 | struct thermal_hwmon_device *hwmon; |
787 | struct thermal_hwmon_temp *temp; | |
788 | ||
789 | hwmon = thermal_hwmon_lookup_by_type(tz); | |
790 | if (unlikely(!hwmon)) { | |
791 | /* Should never happen... */ | |
792 | dev_dbg(&tz->device, "hwmon device lookup failed!\n"); | |
793 | return; | |
794 | } | |
795 | ||
796 | temp = thermal_hwmon_lookup_temp(hwmon, tz); | |
797 | if (unlikely(!temp)) { | |
798 | /* Should never happen... */ | |
799 | dev_dbg(&tz->device, "temperature input lookup failed!\n"); | |
800 | return; | |
801 | } | |
e68b16ab | 802 | |
31f5396a | 803 | device_remove_file(hwmon->device, &temp->temp_input.attr); |
b299eb5c | 804 | if (tz->ops->get_crit_temp) |
31f5396a | 805 | device_remove_file(hwmon->device, &temp->temp_crit.attr); |
e68b16ab ZR |
806 | |
807 | mutex_lock(&thermal_list_lock); | |
31f5396a JD |
808 | list_del(&temp->hwmon_node); |
809 | kfree(temp); | |
e68b16ab ZR |
810 | if (!list_empty(&hwmon->tz_list)) { |
811 | mutex_unlock(&thermal_list_lock); | |
812 | return; | |
813 | } | |
814 | list_del(&hwmon->node); | |
815 | mutex_unlock(&thermal_list_lock); | |
816 | ||
817 | device_remove_file(hwmon->device, &dev_attr_name); | |
818 | hwmon_device_unregister(hwmon->device); | |
819 | kfree(hwmon); | |
820 | } | |
821 | #else | |
822 | static int | |
823 | thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) | |
824 | { | |
825 | return 0; | |
826 | } | |
827 | ||
828 | static void | |
829 | thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz) | |
830 | { | |
831 | } | |
832 | #endif | |
833 | ||
b1569e99 MG |
834 | static void thermal_zone_device_set_polling(struct thermal_zone_device *tz, |
835 | int delay) | |
836 | { | |
b1569e99 | 837 | if (delay > 1000) |
41f63c53 TH |
838 | mod_delayed_work(system_freezable_wq, &tz->poll_queue, |
839 | round_jiffies(msecs_to_jiffies(delay))); | |
840 | else if (delay) | |
841 | mod_delayed_work(system_freezable_wq, &tz->poll_queue, | |
842 | msecs_to_jiffies(delay)); | |
b1569e99 | 843 | else |
41f63c53 | 844 | cancel_delayed_work(&tz->poll_queue); |
b1569e99 MG |
845 | } |
846 | ||
b1569e99 MG |
847 | static void thermal_zone_device_check(struct work_struct *work) |
848 | { | |
849 | struct thermal_zone_device *tz = container_of(work, struct | |
850 | thermal_zone_device, | |
851 | poll_queue.work); | |
852 | thermal_zone_device_update(tz); | |
853 | } | |
e68b16ab | 854 | |
203d3d4a ZR |
855 | /** |
856 | * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone | |
203d3d4a ZR |
857 | * @tz: thermal zone device |
858 | * @trip: indicates which trip point the cooling devices is | |
859 | * associated with in this thermal zone. | |
860 | * @cdev: thermal cooling device | |
543a9561 LB |
861 | * |
862 | * This function is usually called in the thermal zone device .bind callback. | |
203d3d4a ZR |
863 | */ |
864 | int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, | |
865 | int trip, | |
9d99842f ZR |
866 | struct thermal_cooling_device *cdev, |
867 | unsigned long upper, unsigned long lower) | |
203d3d4a | 868 | { |
b81b6ba3 ZR |
869 | struct thermal_instance *dev; |
870 | struct thermal_instance *pos; | |
c7516709 TS |
871 | struct thermal_zone_device *pos1; |
872 | struct thermal_cooling_device *pos2; | |
74051ba5 | 873 | unsigned long max_state; |
203d3d4a ZR |
874 | int result; |
875 | ||
543a9561 | 876 | if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE)) |
203d3d4a ZR |
877 | return -EINVAL; |
878 | ||
c7516709 TS |
879 | list_for_each_entry(pos1, &thermal_tz_list, node) { |
880 | if (pos1 == tz) | |
881 | break; | |
882 | } | |
883 | list_for_each_entry(pos2, &thermal_cdev_list, node) { | |
884 | if (pos2 == cdev) | |
885 | break; | |
886 | } | |
887 | ||
888 | if (tz != pos1 || cdev != pos2) | |
203d3d4a ZR |
889 | return -EINVAL; |
890 | ||
9d99842f ZR |
891 | cdev->ops->get_max_state(cdev, &max_state); |
892 | ||
893 | /* lower default 0, upper default max_state */ | |
894 | lower = lower == THERMAL_NO_LIMIT ? 0 : lower; | |
895 | upper = upper == THERMAL_NO_LIMIT ? max_state : upper; | |
896 | ||
897 | if (lower > upper || upper > max_state) | |
898 | return -EINVAL; | |
899 | ||
203d3d4a | 900 | dev = |
b81b6ba3 | 901 | kzalloc(sizeof(struct thermal_instance), GFP_KERNEL); |
203d3d4a ZR |
902 | if (!dev) |
903 | return -ENOMEM; | |
904 | dev->tz = tz; | |
905 | dev->cdev = cdev; | |
906 | dev->trip = trip; | |
9d99842f ZR |
907 | dev->upper = upper; |
908 | dev->lower = lower; | |
ce119f83 | 909 | dev->target = THERMAL_NO_TARGET; |
74051ba5 | 910 | |
203d3d4a ZR |
911 | result = get_idr(&tz->idr, &tz->lock, &dev->id); |
912 | if (result) | |
913 | goto free_mem; | |
914 | ||
915 | sprintf(dev->name, "cdev%d", dev->id); | |
916 | result = | |
917 | sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name); | |
918 | if (result) | |
919 | goto release_idr; | |
920 | ||
921 | sprintf(dev->attr_name, "cdev%d_trip_point", dev->id); | |
975f8c56 | 922 | sysfs_attr_init(&dev->attr.attr); |
203d3d4a ZR |
923 | dev->attr.attr.name = dev->attr_name; |
924 | dev->attr.attr.mode = 0444; | |
925 | dev->attr.show = thermal_cooling_device_trip_point_show; | |
926 | result = device_create_file(&tz->device, &dev->attr); | |
927 | if (result) | |
928 | goto remove_symbol_link; | |
929 | ||
930 | mutex_lock(&tz->lock); | |
f4a821ce | 931 | mutex_lock(&cdev->lock); |
cddf31b3 | 932 | list_for_each_entry(pos, &tz->thermal_instances, tz_node) |
203d3d4a ZR |
933 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
934 | result = -EEXIST; | |
935 | break; | |
936 | } | |
b5e4ae62 | 937 | if (!result) { |
cddf31b3 | 938 | list_add_tail(&dev->tz_node, &tz->thermal_instances); |
b5e4ae62 ZR |
939 | list_add_tail(&dev->cdev_node, &cdev->thermal_instances); |
940 | } | |
f4a821ce | 941 | mutex_unlock(&cdev->lock); |
203d3d4a ZR |
942 | mutex_unlock(&tz->lock); |
943 | ||
944 | if (!result) | |
945 | return 0; | |
946 | ||
947 | device_remove_file(&tz->device, &dev->attr); | |
caca8b80 | 948 | remove_symbol_link: |
203d3d4a | 949 | sysfs_remove_link(&tz->device.kobj, dev->name); |
caca8b80 | 950 | release_idr: |
203d3d4a | 951 | release_idr(&tz->idr, &tz->lock, dev->id); |
caca8b80 | 952 | free_mem: |
203d3d4a ZR |
953 | kfree(dev); |
954 | return result; | |
955 | } | |
956 | EXPORT_SYMBOL(thermal_zone_bind_cooling_device); | |
957 | ||
958 | /** | |
959 | * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone | |
203d3d4a ZR |
960 | * @tz: thermal zone device |
961 | * @trip: indicates which trip point the cooling devices is | |
962 | * associated with in this thermal zone. | |
963 | * @cdev: thermal cooling device | |
543a9561 LB |
964 | * |
965 | * This function is usually called in the thermal zone device .unbind callback. | |
203d3d4a ZR |
966 | */ |
967 | int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz, | |
968 | int trip, | |
969 | struct thermal_cooling_device *cdev) | |
970 | { | |
b81b6ba3 | 971 | struct thermal_instance *pos, *next; |
203d3d4a ZR |
972 | |
973 | mutex_lock(&tz->lock); | |
f4a821ce | 974 | mutex_lock(&cdev->lock); |
cddf31b3 | 975 | list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) { |
543a9561 | 976 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
cddf31b3 | 977 | list_del(&pos->tz_node); |
b5e4ae62 | 978 | list_del(&pos->cdev_node); |
f4a821ce | 979 | mutex_unlock(&cdev->lock); |
203d3d4a ZR |
980 | mutex_unlock(&tz->lock); |
981 | goto unbind; | |
982 | } | |
983 | } | |
f4a821ce | 984 | mutex_unlock(&cdev->lock); |
203d3d4a ZR |
985 | mutex_unlock(&tz->lock); |
986 | ||
987 | return -ENODEV; | |
988 | ||
caca8b80 | 989 | unbind: |
203d3d4a ZR |
990 | device_remove_file(&tz->device, &pos->attr); |
991 | sysfs_remove_link(&tz->device.kobj, pos->name); | |
992 | release_idr(&tz->idr, &tz->lock, pos->id); | |
993 | kfree(pos); | |
994 | return 0; | |
995 | } | |
996 | EXPORT_SYMBOL(thermal_zone_unbind_cooling_device); | |
997 | ||
998 | static void thermal_release(struct device *dev) | |
999 | { | |
1000 | struct thermal_zone_device *tz; | |
1001 | struct thermal_cooling_device *cdev; | |
1002 | ||
caca8b80 JP |
1003 | if (!strncmp(dev_name(dev), "thermal_zone", |
1004 | sizeof("thermal_zone") - 1)) { | |
203d3d4a ZR |
1005 | tz = to_thermal_zone(dev); |
1006 | kfree(tz); | |
1007 | } else { | |
1008 | cdev = to_cooling_device(dev); | |
1009 | kfree(cdev); | |
1010 | } | |
1011 | } | |
1012 | ||
1013 | static struct class thermal_class = { | |
1014 | .name = "thermal", | |
1015 | .dev_release = thermal_release, | |
1016 | }; | |
1017 | ||
1018 | /** | |
1019 | * thermal_cooling_device_register - register a new thermal cooling device | |
1020 | * @type: the thermal cooling device type. | |
1021 | * @devdata: device private data. | |
1022 | * @ops: standard thermal cooling devices callbacks. | |
1023 | */ | |
caca8b80 JP |
1024 | struct thermal_cooling_device * |
1025 | thermal_cooling_device_register(char *type, void *devdata, | |
1026 | const struct thermal_cooling_device_ops *ops) | |
203d3d4a ZR |
1027 | { |
1028 | struct thermal_cooling_device *cdev; | |
1029 | struct thermal_zone_device *pos; | |
1030 | int result; | |
1031 | ||
204dd1d3 | 1032 | if (type && strlen(type) >= THERMAL_NAME_LENGTH) |
3e6fda5c | 1033 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
1034 | |
1035 | if (!ops || !ops->get_max_state || !ops->get_cur_state || | |
543a9561 | 1036 | !ops->set_cur_state) |
3e6fda5c | 1037 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
1038 | |
1039 | cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL); | |
1040 | if (!cdev) | |
3e6fda5c | 1041 | return ERR_PTR(-ENOMEM); |
203d3d4a ZR |
1042 | |
1043 | result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id); | |
1044 | if (result) { | |
1045 | kfree(cdev); | |
3e6fda5c | 1046 | return ERR_PTR(result); |
203d3d4a ZR |
1047 | } |
1048 | ||
204dd1d3 | 1049 | strcpy(cdev->type, type ? : ""); |
f4a821ce | 1050 | mutex_init(&cdev->lock); |
b5e4ae62 | 1051 | INIT_LIST_HEAD(&cdev->thermal_instances); |
203d3d4a | 1052 | cdev->ops = ops; |
ce119f83 | 1053 | cdev->updated = true; |
203d3d4a ZR |
1054 | cdev->device.class = &thermal_class; |
1055 | cdev->devdata = devdata; | |
354655ea | 1056 | dev_set_name(&cdev->device, "cooling_device%d", cdev->id); |
203d3d4a ZR |
1057 | result = device_register(&cdev->device); |
1058 | if (result) { | |
1059 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); | |
1060 | kfree(cdev); | |
3e6fda5c | 1061 | return ERR_PTR(result); |
203d3d4a ZR |
1062 | } |
1063 | ||
1064 | /* sys I/F */ | |
1065 | if (type) { | |
543a9561 | 1066 | result = device_create_file(&cdev->device, &dev_attr_cdev_type); |
203d3d4a ZR |
1067 | if (result) |
1068 | goto unregister; | |
1069 | } | |
1070 | ||
1071 | result = device_create_file(&cdev->device, &dev_attr_max_state); | |
1072 | if (result) | |
1073 | goto unregister; | |
1074 | ||
1075 | result = device_create_file(&cdev->device, &dev_attr_cur_state); | |
1076 | if (result) | |
1077 | goto unregister; | |
1078 | ||
1079 | mutex_lock(&thermal_list_lock); | |
1080 | list_add(&cdev->node, &thermal_cdev_list); | |
1081 | list_for_each_entry(pos, &thermal_tz_list, node) { | |
1082 | if (!pos->ops->bind) | |
1083 | continue; | |
1084 | result = pos->ops->bind(pos, cdev); | |
1085 | if (result) | |
1086 | break; | |
1087 | ||
1088 | } | |
1089 | mutex_unlock(&thermal_list_lock); | |
1090 | ||
1091 | if (!result) | |
1092 | return cdev; | |
1093 | ||
caca8b80 | 1094 | unregister: |
203d3d4a ZR |
1095 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); |
1096 | device_unregister(&cdev->device); | |
3e6fda5c | 1097 | return ERR_PTR(result); |
203d3d4a ZR |
1098 | } |
1099 | EXPORT_SYMBOL(thermal_cooling_device_register); | |
1100 | ||
1101 | /** | |
1102 | * thermal_cooling_device_unregister - removes the registered thermal cooling device | |
203d3d4a ZR |
1103 | * @cdev: the thermal cooling device to remove. |
1104 | * | |
1105 | * thermal_cooling_device_unregister() must be called when the device is no | |
1106 | * longer needed. | |
1107 | */ | |
1108 | void thermal_cooling_device_unregister(struct | |
1109 | thermal_cooling_device | |
1110 | *cdev) | |
1111 | { | |
1112 | struct thermal_zone_device *tz; | |
1113 | struct thermal_cooling_device *pos = NULL; | |
1114 | ||
1115 | if (!cdev) | |
1116 | return; | |
1117 | ||
1118 | mutex_lock(&thermal_list_lock); | |
1119 | list_for_each_entry(pos, &thermal_cdev_list, node) | |
1120 | if (pos == cdev) | |
1121 | break; | |
1122 | if (pos != cdev) { | |
1123 | /* thermal cooling device not found */ | |
1124 | mutex_unlock(&thermal_list_lock); | |
1125 | return; | |
1126 | } | |
1127 | list_del(&cdev->node); | |
1128 | list_for_each_entry(tz, &thermal_tz_list, node) { | |
1129 | if (!tz->ops->unbind) | |
1130 | continue; | |
1131 | tz->ops->unbind(tz, cdev); | |
1132 | } | |
1133 | mutex_unlock(&thermal_list_lock); | |
1134 | if (cdev->type[0]) | |
543a9561 | 1135 | device_remove_file(&cdev->device, &dev_attr_cdev_type); |
203d3d4a ZR |
1136 | device_remove_file(&cdev->device, &dev_attr_max_state); |
1137 | device_remove_file(&cdev->device, &dev_attr_cur_state); | |
1138 | ||
1139 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); | |
1140 | device_unregister(&cdev->device); | |
1141 | return; | |
1142 | } | |
1143 | EXPORT_SYMBOL(thermal_cooling_device_unregister); | |
1144 | ||
ce119f83 ZR |
1145 | static void thermal_cdev_do_update(struct thermal_cooling_device *cdev) |
1146 | { | |
1147 | struct thermal_instance *instance; | |
1148 | unsigned long target = 0; | |
1149 | ||
1150 | /* cooling device is updated*/ | |
1151 | if (cdev->updated) | |
1152 | return; | |
1153 | ||
f4a821ce | 1154 | mutex_lock(&cdev->lock); |
ce119f83 ZR |
1155 | /* Make sure cdev enters the deepest cooling state */ |
1156 | list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) { | |
1157 | if (instance->target == THERMAL_NO_TARGET) | |
1158 | continue; | |
1159 | if (instance->target > target) | |
1160 | target = instance->target; | |
1161 | } | |
f4a821ce | 1162 | mutex_unlock(&cdev->lock); |
ce119f83 ZR |
1163 | cdev->ops->set_cur_state(cdev, target); |
1164 | cdev->updated = true; | |
1165 | } | |
1166 | ||
1167 | static void thermal_zone_do_update(struct thermal_zone_device *tz) | |
1168 | { | |
1169 | struct thermal_instance *instance; | |
1170 | ||
1171 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) | |
1172 | thermal_cdev_do_update(instance->cdev); | |
1173 | } | |
1174 | ||
4ae46bef | 1175 | /* |
908b9fb7 | 1176 | * Cooling algorithm for both active and passive cooling |
4ae46bef ZR |
1177 | * |
1178 | * 1. if the temperature is higher than a trip point, | |
1179 | * a. if the trend is THERMAL_TREND_RAISING, use higher cooling | |
1180 | * state for this trip point | |
1181 | * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling | |
1182 | * state for this trip point | |
1183 | * | |
1184 | * 2. if the temperature is lower than a trip point, use lower | |
1185 | * cooling state for this trip point | |
1186 | * | |
1187 | * Note that this behaves the same as the previous passive cooling | |
1188 | * algorithm. | |
1189 | */ | |
1190 | ||
1191 | static void thermal_zone_trip_update(struct thermal_zone_device *tz, | |
1192 | int trip, long temp) | |
1193 | { | |
b81b6ba3 | 1194 | struct thermal_instance *instance; |
4ae46bef ZR |
1195 | struct thermal_cooling_device *cdev = NULL; |
1196 | unsigned long cur_state, max_state; | |
1197 | long trip_temp; | |
908b9fb7 | 1198 | enum thermal_trip_type trip_type; |
4ae46bef ZR |
1199 | enum thermal_trend trend; |
1200 | ||
908b9fb7 ZR |
1201 | if (trip == THERMAL_TRIPS_NONE) { |
1202 | trip_temp = tz->forced_passive; | |
1203 | trip_type = THERMAL_TRIPS_NONE; | |
1204 | } else { | |
1205 | tz->ops->get_trip_temp(tz, trip, &trip_temp); | |
1206 | tz->ops->get_trip_type(tz, trip, &trip_type); | |
1207 | } | |
4ae46bef ZR |
1208 | |
1209 | if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) { | |
1210 | /* | |
1211 | * compare the current temperature and previous temperature | |
1212 | * to get the thermal trend, if no special requirement | |
1213 | */ | |
1214 | if (tz->temperature > tz->last_temperature) | |
1215 | trend = THERMAL_TREND_RAISING; | |
1216 | else if (tz->temperature < tz->last_temperature) | |
1217 | trend = THERMAL_TREND_DROPPING; | |
1218 | else | |
1219 | trend = THERMAL_TREND_STABLE; | |
1220 | } | |
1221 | ||
1222 | if (temp >= trip_temp) { | |
cddf31b3 | 1223 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) { |
4ae46bef ZR |
1224 | if (instance->trip != trip) |
1225 | continue; | |
1226 | ||
1227 | cdev = instance->cdev; | |
1228 | ||
1229 | cdev->ops->get_cur_state(cdev, &cur_state); | |
1230 | cdev->ops->get_max_state(cdev, &max_state); | |
1231 | ||
1232 | if (trend == THERMAL_TREND_RAISING) { | |
1233 | cur_state = cur_state < instance->upper ? | |
1234 | (cur_state + 1) : instance->upper; | |
1235 | } else if (trend == THERMAL_TREND_DROPPING) { | |
1236 | cur_state = cur_state > instance->lower ? | |
1237 | (cur_state - 1) : instance->lower; | |
1238 | } | |
908b9fb7 ZR |
1239 | |
1240 | /* activate a passive thermal instance */ | |
1241 | if ((trip_type == THERMAL_TRIP_PASSIVE || | |
1242 | trip_type == THERMAL_TRIPS_NONE) && | |
1243 | instance->target == THERMAL_NO_TARGET) | |
1244 | tz->passive++; | |
1245 | ||
ce119f83 ZR |
1246 | instance->target = cur_state; |
1247 | cdev->updated = false; /* cooling device needs update */ | |
4ae46bef ZR |
1248 | } |
1249 | } else { /* below trip */ | |
cddf31b3 | 1250 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) { |
4ae46bef ZR |
1251 | if (instance->trip != trip) |
1252 | continue; | |
1253 | ||
ce119f83 ZR |
1254 | /* Do not use the inactive thermal instance */ |
1255 | if (instance->target == THERMAL_NO_TARGET) | |
1256 | continue; | |
4ae46bef ZR |
1257 | cdev = instance->cdev; |
1258 | cdev->ops->get_cur_state(cdev, &cur_state); | |
1259 | ||
1260 | cur_state = cur_state > instance->lower ? | |
ce119f83 | 1261 | (cur_state - 1) : THERMAL_NO_TARGET; |
908b9fb7 ZR |
1262 | |
1263 | /* deactivate a passive thermal instance */ | |
1264 | if ((trip_type == THERMAL_TRIP_PASSIVE || | |
1265 | trip_type == THERMAL_TRIPS_NONE) && | |
1266 | cur_state == THERMAL_NO_TARGET) | |
1267 | tz->passive--; | |
ce119f83 ZR |
1268 | instance->target = cur_state; |
1269 | cdev->updated = false; /* cooling device needs update */ | |
4ae46bef ZR |
1270 | } |
1271 | } | |
1272 | ||
1273 | return; | |
1274 | } | |
b1569e99 MG |
1275 | /** |
1276 | * thermal_zone_device_update - force an update of a thermal zone's state | |
1277 | * @ttz: the thermal zone to update | |
1278 | */ | |
1279 | ||
1280 | void thermal_zone_device_update(struct thermal_zone_device *tz) | |
1281 | { | |
1282 | int count, ret = 0; | |
1283 | long temp, trip_temp; | |
1284 | enum thermal_trip_type trip_type; | |
b1569e99 MG |
1285 | |
1286 | mutex_lock(&tz->lock); | |
1287 | ||
0d288162 MB |
1288 | if (tz->ops->get_temp(tz, &temp)) { |
1289 | /* get_temp failed - retry it later */ | |
c5a01dd5 | 1290 | pr_warn("failed to read out thermal zone %d\n", tz->id); |
0d288162 MB |
1291 | goto leave; |
1292 | } | |
b1569e99 | 1293 | |
601f3d42 ZR |
1294 | tz->last_temperature = tz->temperature; |
1295 | tz->temperature = temp; | |
1296 | ||
b1569e99 MG |
1297 | for (count = 0; count < tz->trips; count++) { |
1298 | tz->ops->get_trip_type(tz, count, &trip_type); | |
1299 | tz->ops->get_trip_temp(tz, count, &trip_temp); | |
1300 | ||
1301 | switch (trip_type) { | |
1302 | case THERMAL_TRIP_CRITICAL: | |
29321357 | 1303 | if (temp >= trip_temp) { |
b1569e99 MG |
1304 | if (tz->ops->notify) |
1305 | ret = tz->ops->notify(tz, count, | |
1306 | trip_type); | |
1307 | if (!ret) { | |
c5a01dd5 JP |
1308 | pr_emerg("Critical temperature reached (%ld C), shutting down\n", |
1309 | temp/1000); | |
b1569e99 MG |
1310 | orderly_poweroff(true); |
1311 | } | |
1312 | } | |
1313 | break; | |
1314 | case THERMAL_TRIP_HOT: | |
29321357 | 1315 | if (temp >= trip_temp) |
b1569e99 MG |
1316 | if (tz->ops->notify) |
1317 | tz->ops->notify(tz, count, trip_type); | |
1318 | break; | |
1319 | case THERMAL_TRIP_ACTIVE: | |
4ae46bef | 1320 | thermal_zone_trip_update(tz, count, temp); |
b1569e99 MG |
1321 | break; |
1322 | case THERMAL_TRIP_PASSIVE: | |
29321357 | 1323 | if (temp >= trip_temp || tz->passive) |
908b9fb7 | 1324 | thermal_zone_trip_update(tz, count, temp); |
b1569e99 MG |
1325 | break; |
1326 | } | |
1327 | } | |
03a971a2 MG |
1328 | |
1329 | if (tz->forced_passive) | |
908b9fb7 ZR |
1330 | thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE, temp); |
1331 | thermal_zone_do_update(tz); | |
0d288162 | 1332 | |
caca8b80 | 1333 | leave: |
b1569e99 MG |
1334 | if (tz->passive) |
1335 | thermal_zone_device_set_polling(tz, tz->passive_delay); | |
1336 | else if (tz->polling_delay) | |
1337 | thermal_zone_device_set_polling(tz, tz->polling_delay); | |
3767cb54 FP |
1338 | else |
1339 | thermal_zone_device_set_polling(tz, 0); | |
b1569e99 MG |
1340 | mutex_unlock(&tz->lock); |
1341 | } | |
1342 | EXPORT_SYMBOL(thermal_zone_device_update); | |
1343 | ||
c56f5c03 D |
1344 | /** |
1345 | * create_trip_attrs - create attributes for trip points | |
1346 | * @tz: the thermal zone device | |
1347 | * @mask: Writeable trip point bitmap. | |
1348 | */ | |
1349 | static int create_trip_attrs(struct thermal_zone_device *tz, int mask) | |
1350 | { | |
1351 | int indx; | |
27365a6c | 1352 | int size = sizeof(struct thermal_attr) * tz->trips; |
c56f5c03 | 1353 | |
27365a6c | 1354 | tz->trip_type_attrs = kzalloc(size, GFP_KERNEL); |
c56f5c03 D |
1355 | if (!tz->trip_type_attrs) |
1356 | return -ENOMEM; | |
1357 | ||
27365a6c | 1358 | tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL); |
c56f5c03 D |
1359 | if (!tz->trip_temp_attrs) { |
1360 | kfree(tz->trip_type_attrs); | |
1361 | return -ENOMEM; | |
1362 | } | |
1363 | ||
27365a6c D |
1364 | if (tz->ops->get_trip_hyst) { |
1365 | tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL); | |
1366 | if (!tz->trip_hyst_attrs) { | |
1367 | kfree(tz->trip_type_attrs); | |
1368 | kfree(tz->trip_temp_attrs); | |
1369 | return -ENOMEM; | |
1370 | } | |
1371 | } | |
c56f5c03 | 1372 | |
27365a6c D |
1373 | |
1374 | for (indx = 0; indx < tz->trips; indx++) { | |
c56f5c03 D |
1375 | /* create trip type attribute */ |
1376 | snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH, | |
1377 | "trip_point_%d_type", indx); | |
1378 | ||
1379 | sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr); | |
1380 | tz->trip_type_attrs[indx].attr.attr.name = | |
1381 | tz->trip_type_attrs[indx].name; | |
1382 | tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO; | |
1383 | tz->trip_type_attrs[indx].attr.show = trip_point_type_show; | |
1384 | ||
1385 | device_create_file(&tz->device, | |
1386 | &tz->trip_type_attrs[indx].attr); | |
1387 | ||
1388 | /* create trip temp attribute */ | |
1389 | snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH, | |
1390 | "trip_point_%d_temp", indx); | |
1391 | ||
1392 | sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr); | |
1393 | tz->trip_temp_attrs[indx].attr.attr.name = | |
1394 | tz->trip_temp_attrs[indx].name; | |
1395 | tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO; | |
1396 | tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show; | |
1397 | if (mask & (1 << indx)) { | |
1398 | tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR; | |
1399 | tz->trip_temp_attrs[indx].attr.store = | |
1400 | trip_point_temp_store; | |
1401 | } | |
1402 | ||
1403 | device_create_file(&tz->device, | |
1404 | &tz->trip_temp_attrs[indx].attr); | |
27365a6c D |
1405 | |
1406 | /* create Optional trip hyst attribute */ | |
1407 | if (!tz->ops->get_trip_hyst) | |
1408 | continue; | |
1409 | snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH, | |
1410 | "trip_point_%d_hyst", indx); | |
1411 | ||
1412 | sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr); | |
1413 | tz->trip_hyst_attrs[indx].attr.attr.name = | |
1414 | tz->trip_hyst_attrs[indx].name; | |
1415 | tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO; | |
1416 | tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show; | |
1417 | if (tz->ops->set_trip_hyst) { | |
1418 | tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR; | |
1419 | tz->trip_hyst_attrs[indx].attr.store = | |
1420 | trip_point_hyst_store; | |
1421 | } | |
1422 | ||
1423 | device_create_file(&tz->device, | |
1424 | &tz->trip_hyst_attrs[indx].attr); | |
c56f5c03 D |
1425 | } |
1426 | return 0; | |
1427 | } | |
1428 | ||
1429 | static void remove_trip_attrs(struct thermal_zone_device *tz) | |
1430 | { | |
1431 | int indx; | |
1432 | ||
1433 | for (indx = 0; indx < tz->trips; indx++) { | |
1434 | device_remove_file(&tz->device, | |
1435 | &tz->trip_type_attrs[indx].attr); | |
1436 | device_remove_file(&tz->device, | |
1437 | &tz->trip_temp_attrs[indx].attr); | |
27365a6c D |
1438 | if (tz->ops->get_trip_hyst) |
1439 | device_remove_file(&tz->device, | |
1440 | &tz->trip_hyst_attrs[indx].attr); | |
c56f5c03 D |
1441 | } |
1442 | kfree(tz->trip_type_attrs); | |
1443 | kfree(tz->trip_temp_attrs); | |
27365a6c | 1444 | kfree(tz->trip_hyst_attrs); |
c56f5c03 D |
1445 | } |
1446 | ||
203d3d4a ZR |
1447 | /** |
1448 | * thermal_zone_device_register - register a new thermal zone device | |
1449 | * @type: the thermal zone device type | |
1450 | * @trips: the number of trip points the thermal zone support | |
c56f5c03 | 1451 | * @mask: a bit string indicating the writeablility of trip points |
203d3d4a ZR |
1452 | * @devdata: private device data |
1453 | * @ops: standard thermal zone device callbacks | |
50125a9b | 1454 | * @tzp: thermal zone platform parameters |
b1569e99 MG |
1455 | * @passive_delay: number of milliseconds to wait between polls when |
1456 | * performing passive cooling | |
1457 | * @polling_delay: number of milliseconds to wait between polls when checking | |
1458 | * whether trip points have been crossed (0 for interrupt | |
1459 | * driven systems) | |
203d3d4a ZR |
1460 | * |
1461 | * thermal_zone_device_unregister() must be called when the device is no | |
1b7ddb84 | 1462 | * longer needed. The passive cooling depends on the .get_trend() return value. |
203d3d4a | 1463 | */ |
4b1bf587 | 1464 | struct thermal_zone_device *thermal_zone_device_register(const char *type, |
c56f5c03 | 1465 | int trips, int mask, void *devdata, |
5b275ce2 | 1466 | const struct thermal_zone_device_ops *ops, |
50125a9b | 1467 | const struct thermal_zone_params *tzp, |
1b7ddb84 | 1468 | int passive_delay, int polling_delay) |
203d3d4a ZR |
1469 | { |
1470 | struct thermal_zone_device *tz; | |
1471 | struct thermal_cooling_device *pos; | |
03a971a2 | 1472 | enum thermal_trip_type trip_type; |
203d3d4a ZR |
1473 | int result; |
1474 | int count; | |
03a971a2 | 1475 | int passive = 0; |
203d3d4a | 1476 | |
204dd1d3 | 1477 | if (type && strlen(type) >= THERMAL_NAME_LENGTH) |
3e6fda5c | 1478 | return ERR_PTR(-EINVAL); |
203d3d4a | 1479 | |
c56f5c03 | 1480 | if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips) |
3e6fda5c | 1481 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
1482 | |
1483 | if (!ops || !ops->get_temp) | |
3e6fda5c | 1484 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
1485 | |
1486 | tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL); | |
1487 | if (!tz) | |
3e6fda5c | 1488 | return ERR_PTR(-ENOMEM); |
203d3d4a | 1489 | |
2d374139 | 1490 | INIT_LIST_HEAD(&tz->thermal_instances); |
203d3d4a ZR |
1491 | idr_init(&tz->idr); |
1492 | mutex_init(&tz->lock); | |
1493 | result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id); | |
1494 | if (result) { | |
1495 | kfree(tz); | |
3e6fda5c | 1496 | return ERR_PTR(result); |
203d3d4a ZR |
1497 | } |
1498 | ||
204dd1d3 | 1499 | strcpy(tz->type, type ? : ""); |
203d3d4a | 1500 | tz->ops = ops; |
50125a9b | 1501 | tz->tzp = tzp; |
203d3d4a ZR |
1502 | tz->device.class = &thermal_class; |
1503 | tz->devdata = devdata; | |
1504 | tz->trips = trips; | |
b1569e99 MG |
1505 | tz->passive_delay = passive_delay; |
1506 | tz->polling_delay = polling_delay; | |
1507 | ||
354655ea | 1508 | dev_set_name(&tz->device, "thermal_zone%d", tz->id); |
203d3d4a ZR |
1509 | result = device_register(&tz->device); |
1510 | if (result) { | |
1511 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); | |
1512 | kfree(tz); | |
3e6fda5c | 1513 | return ERR_PTR(result); |
203d3d4a ZR |
1514 | } |
1515 | ||
1516 | /* sys I/F */ | |
1517 | if (type) { | |
1518 | result = device_create_file(&tz->device, &dev_attr_type); | |
1519 | if (result) | |
1520 | goto unregister; | |
1521 | } | |
1522 | ||
1523 | result = device_create_file(&tz->device, &dev_attr_temp); | |
1524 | if (result) | |
1525 | goto unregister; | |
1526 | ||
1527 | if (ops->get_mode) { | |
1528 | result = device_create_file(&tz->device, &dev_attr_mode); | |
1529 | if (result) | |
1530 | goto unregister; | |
1531 | } | |
1532 | ||
c56f5c03 D |
1533 | result = create_trip_attrs(tz, mask); |
1534 | if (result) | |
1535 | goto unregister; | |
1536 | ||
203d3d4a | 1537 | for (count = 0; count < trips; count++) { |
03a971a2 MG |
1538 | tz->ops->get_trip_type(tz, count, &trip_type); |
1539 | if (trip_type == THERMAL_TRIP_PASSIVE) | |
1540 | passive = 1; | |
203d3d4a ZR |
1541 | } |
1542 | ||
5a2c090b D |
1543 | if (!passive) { |
1544 | result = device_create_file(&tz->device, &dev_attr_passive); | |
1545 | if (result) | |
1546 | goto unregister; | |
1547 | } | |
03a971a2 | 1548 | |
5a2c090b D |
1549 | /* Create policy attribute */ |
1550 | result = device_create_file(&tz->device, &dev_attr_policy); | |
03a971a2 MG |
1551 | if (result) |
1552 | goto unregister; | |
1553 | ||
a4a15485 D |
1554 | /* Update 'this' zone's governor information */ |
1555 | mutex_lock(&thermal_governor_lock); | |
1556 | ||
1557 | if (tz->tzp) | |
1558 | tz->governor = __find_governor(tz->tzp->governor_name); | |
1559 | else | |
1560 | tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR); | |
1561 | ||
1562 | mutex_unlock(&thermal_governor_lock); | |
1563 | ||
e68b16ab ZR |
1564 | result = thermal_add_hwmon_sysfs(tz); |
1565 | if (result) | |
1566 | goto unregister; | |
1567 | ||
203d3d4a ZR |
1568 | mutex_lock(&thermal_list_lock); |
1569 | list_add_tail(&tz->node, &thermal_tz_list); | |
1570 | if (ops->bind) | |
1571 | list_for_each_entry(pos, &thermal_cdev_list, node) { | |
543a9561 LB |
1572 | result = ops->bind(tz, pos); |
1573 | if (result) | |
1574 | break; | |
203d3d4a ZR |
1575 | } |
1576 | mutex_unlock(&thermal_list_lock); | |
1577 | ||
b1569e99 MG |
1578 | INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check); |
1579 | ||
1580 | thermal_zone_device_update(tz); | |
1581 | ||
203d3d4a ZR |
1582 | if (!result) |
1583 | return tz; | |
1584 | ||
caca8b80 | 1585 | unregister: |
203d3d4a ZR |
1586 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
1587 | device_unregister(&tz->device); | |
3e6fda5c | 1588 | return ERR_PTR(result); |
203d3d4a ZR |
1589 | } |
1590 | EXPORT_SYMBOL(thermal_zone_device_register); | |
1591 | ||
1592 | /** | |
1593 | * thermal_device_unregister - removes the registered thermal zone device | |
203d3d4a ZR |
1594 | * @tz: the thermal zone device to remove |
1595 | */ | |
1596 | void thermal_zone_device_unregister(struct thermal_zone_device *tz) | |
1597 | { | |
1598 | struct thermal_cooling_device *cdev; | |
1599 | struct thermal_zone_device *pos = NULL; | |
203d3d4a ZR |
1600 | |
1601 | if (!tz) | |
1602 | return; | |
1603 | ||
1604 | mutex_lock(&thermal_list_lock); | |
1605 | list_for_each_entry(pos, &thermal_tz_list, node) | |
1606 | if (pos == tz) | |
1607 | break; | |
1608 | if (pos != tz) { | |
1609 | /* thermal zone device not found */ | |
1610 | mutex_unlock(&thermal_list_lock); | |
1611 | return; | |
1612 | } | |
1613 | list_del(&tz->node); | |
1614 | if (tz->ops->unbind) | |
1615 | list_for_each_entry(cdev, &thermal_cdev_list, node) | |
1616 | tz->ops->unbind(tz, cdev); | |
1617 | mutex_unlock(&thermal_list_lock); | |
1618 | ||
b1569e99 MG |
1619 | thermal_zone_device_set_polling(tz, 0); |
1620 | ||
203d3d4a ZR |
1621 | if (tz->type[0]) |
1622 | device_remove_file(&tz->device, &dev_attr_type); | |
1623 | device_remove_file(&tz->device, &dev_attr_temp); | |
1624 | if (tz->ops->get_mode) | |
1625 | device_remove_file(&tz->device, &dev_attr_mode); | |
5a2c090b | 1626 | device_remove_file(&tz->device, &dev_attr_policy); |
c56f5c03 | 1627 | remove_trip_attrs(tz); |
a4a15485 | 1628 | tz->governor = NULL; |
203d3d4a | 1629 | |
e68b16ab | 1630 | thermal_remove_hwmon_sysfs(tz); |
203d3d4a ZR |
1631 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
1632 | idr_destroy(&tz->idr); | |
1633 | mutex_destroy(&tz->lock); | |
1634 | device_unregister(&tz->device); | |
1635 | return; | |
1636 | } | |
1637 | EXPORT_SYMBOL(thermal_zone_device_unregister); | |
1638 | ||
af06216a RW |
1639 | #ifdef CONFIG_NET |
1640 | static struct genl_family thermal_event_genl_family = { | |
1641 | .id = GENL_ID_GENERATE, | |
1642 | .name = THERMAL_GENL_FAMILY_NAME, | |
1643 | .version = THERMAL_GENL_VERSION, | |
1644 | .maxattr = THERMAL_GENL_ATTR_MAX, | |
1645 | }; | |
1646 | ||
1647 | static struct genl_multicast_group thermal_event_mcgrp = { | |
1648 | .name = THERMAL_GENL_MCAST_GROUP_NAME, | |
1649 | }; | |
1650 | ||
2d58d7ea | 1651 | int thermal_generate_netlink_event(u32 orig, enum events event) |
4cb18728 D |
1652 | { |
1653 | struct sk_buff *skb; | |
1654 | struct nlattr *attr; | |
1655 | struct thermal_genl_event *thermal_event; | |
1656 | void *msg_header; | |
1657 | int size; | |
1658 | int result; | |
b11de07c | 1659 | static unsigned int thermal_event_seqnum; |
4cb18728 D |
1660 | |
1661 | /* allocate memory */ | |
886ee546 JP |
1662 | size = nla_total_size(sizeof(struct thermal_genl_event)) + |
1663 | nla_total_size(0); | |
4cb18728 D |
1664 | |
1665 | skb = genlmsg_new(size, GFP_ATOMIC); | |
1666 | if (!skb) | |
1667 | return -ENOMEM; | |
1668 | ||
1669 | /* add the genetlink message header */ | |
1670 | msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++, | |
1671 | &thermal_event_genl_family, 0, | |
1672 | THERMAL_GENL_CMD_EVENT); | |
1673 | if (!msg_header) { | |
1674 | nlmsg_free(skb); | |
1675 | return -ENOMEM; | |
1676 | } | |
1677 | ||
1678 | /* fill the data */ | |
886ee546 JP |
1679 | attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT, |
1680 | sizeof(struct thermal_genl_event)); | |
4cb18728 D |
1681 | |
1682 | if (!attr) { | |
1683 | nlmsg_free(skb); | |
1684 | return -EINVAL; | |
1685 | } | |
1686 | ||
1687 | thermal_event = nla_data(attr); | |
1688 | if (!thermal_event) { | |
1689 | nlmsg_free(skb); | |
1690 | return -EINVAL; | |
1691 | } | |
1692 | ||
1693 | memset(thermal_event, 0, sizeof(struct thermal_genl_event)); | |
1694 | ||
1695 | thermal_event->orig = orig; | |
1696 | thermal_event->event = event; | |
1697 | ||
1698 | /* send multicast genetlink message */ | |
1699 | result = genlmsg_end(skb, msg_header); | |
1700 | if (result < 0) { | |
1701 | nlmsg_free(skb); | |
1702 | return result; | |
1703 | } | |
1704 | ||
1705 | result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC); | |
1706 | if (result) | |
c5a01dd5 | 1707 | pr_info("failed to send netlink event:%d\n", result); |
4cb18728 D |
1708 | |
1709 | return result; | |
1710 | } | |
2d58d7ea | 1711 | EXPORT_SYMBOL(thermal_generate_netlink_event); |
4cb18728 D |
1712 | |
1713 | static int genetlink_init(void) | |
1714 | { | |
1715 | int result; | |
1716 | ||
1717 | result = genl_register_family(&thermal_event_genl_family); | |
1718 | if (result) | |
1719 | return result; | |
1720 | ||
1721 | result = genl_register_mc_group(&thermal_event_genl_family, | |
1722 | &thermal_event_mcgrp); | |
1723 | if (result) | |
1724 | genl_unregister_family(&thermal_event_genl_family); | |
1725 | return result; | |
1726 | } | |
1727 | ||
af06216a RW |
1728 | static void genetlink_exit(void) |
1729 | { | |
1730 | genl_unregister_family(&thermal_event_genl_family); | |
1731 | } | |
1732 | #else /* !CONFIG_NET */ | |
1733 | static inline int genetlink_init(void) { return 0; } | |
1734 | static inline void genetlink_exit(void) {} | |
1735 | #endif /* !CONFIG_NET */ | |
1736 | ||
203d3d4a ZR |
1737 | static int __init thermal_init(void) |
1738 | { | |
1739 | int result = 0; | |
1740 | ||
1741 | result = class_register(&thermal_class); | |
1742 | if (result) { | |
1743 | idr_destroy(&thermal_tz_idr); | |
1744 | idr_destroy(&thermal_cdev_idr); | |
1745 | mutex_destroy(&thermal_idr_lock); | |
1746 | mutex_destroy(&thermal_list_lock); | |
1747 | } | |
4cb18728 | 1748 | result = genetlink_init(); |
203d3d4a ZR |
1749 | return result; |
1750 | } | |
1751 | ||
1752 | static void __exit thermal_exit(void) | |
1753 | { | |
1754 | class_unregister(&thermal_class); | |
1755 | idr_destroy(&thermal_tz_idr); | |
1756 | idr_destroy(&thermal_cdev_idr); | |
1757 | mutex_destroy(&thermal_idr_lock); | |
1758 | mutex_destroy(&thermal_list_lock); | |
4cb18728 | 1759 | genetlink_exit(); |
203d3d4a ZR |
1760 | } |
1761 | ||
4cb18728 | 1762 | fs_initcall(thermal_init); |
203d3d4a | 1763 | module_exit(thermal_exit); |