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