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