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 | 453 | mutex_unlock(&tz->lock); |
06475b55 AL |
454 | |
455 | dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n", | |
456 | tz->last_temperature, tz->temperature); | |
0c01ebbf D |
457 | } |
458 | ||
459 | void thermal_zone_device_update(struct thermal_zone_device *tz) | |
460 | { | |
461 | int count; | |
462 | ||
81bd4e1c EV |
463 | if (!tz->ops->get_temp) |
464 | return; | |
465 | ||
0c01ebbf D |
466 | update_temperature(tz); |
467 | ||
468 | for (count = 0; count < tz->trips; count++) | |
469 | handle_thermal_trip(tz, count); | |
470 | } | |
910cb1e3 | 471 | EXPORT_SYMBOL_GPL(thermal_zone_device_update); |
0c01ebbf D |
472 | |
473 | static void thermal_zone_device_check(struct work_struct *work) | |
474 | { | |
475 | struct thermal_zone_device *tz = container_of(work, struct | |
476 | thermal_zone_device, | |
477 | poll_queue.work); | |
478 | thermal_zone_device_update(tz); | |
479 | } | |
480 | ||
203d3d4a ZR |
481 | /* sys I/F for thermal zone */ |
482 | ||
483 | #define to_thermal_zone(_dev) \ | |
484 | container_of(_dev, struct thermal_zone_device, device) | |
485 | ||
486 | static ssize_t | |
487 | type_show(struct device *dev, struct device_attribute *attr, char *buf) | |
488 | { | |
489 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
490 | ||
491 | return sprintf(buf, "%s\n", tz->type); | |
492 | } | |
493 | ||
494 | static ssize_t | |
495 | temp_show(struct device *dev, struct device_attribute *attr, char *buf) | |
496 | { | |
497 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
6503e5df MG |
498 | long temperature; |
499 | int ret; | |
203d3d4a | 500 | |
e6e238c3 | 501 | ret = thermal_zone_get_temp(tz, &temperature); |
6503e5df MG |
502 | |
503 | if (ret) | |
504 | return ret; | |
505 | ||
506 | return sprintf(buf, "%ld\n", temperature); | |
203d3d4a ZR |
507 | } |
508 | ||
509 | static ssize_t | |
510 | mode_show(struct device *dev, struct device_attribute *attr, char *buf) | |
511 | { | |
512 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
6503e5df MG |
513 | enum thermal_device_mode mode; |
514 | int result; | |
203d3d4a ZR |
515 | |
516 | if (!tz->ops->get_mode) | |
517 | return -EPERM; | |
518 | ||
6503e5df MG |
519 | result = tz->ops->get_mode(tz, &mode); |
520 | if (result) | |
521 | return result; | |
522 | ||
523 | return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled" | |
524 | : "disabled"); | |
203d3d4a ZR |
525 | } |
526 | ||
527 | static ssize_t | |
528 | mode_store(struct device *dev, struct device_attribute *attr, | |
529 | const char *buf, size_t count) | |
530 | { | |
531 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
532 | int result; | |
533 | ||
534 | if (!tz->ops->set_mode) | |
535 | return -EPERM; | |
536 | ||
f1f0e2ac | 537 | if (!strncmp(buf, "enabled", sizeof("enabled") - 1)) |
6503e5df | 538 | result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED); |
f1f0e2ac | 539 | else if (!strncmp(buf, "disabled", sizeof("disabled") - 1)) |
6503e5df MG |
540 | result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED); |
541 | else | |
542 | result = -EINVAL; | |
543 | ||
203d3d4a ZR |
544 | if (result) |
545 | return result; | |
546 | ||
547 | return count; | |
548 | } | |
549 | ||
550 | static ssize_t | |
551 | trip_point_type_show(struct device *dev, struct device_attribute *attr, | |
552 | char *buf) | |
553 | { | |
554 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
6503e5df MG |
555 | enum thermal_trip_type type; |
556 | int trip, result; | |
203d3d4a ZR |
557 | |
558 | if (!tz->ops->get_trip_type) | |
559 | return -EPERM; | |
560 | ||
561 | if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip)) | |
562 | return -EINVAL; | |
563 | ||
6503e5df MG |
564 | result = tz->ops->get_trip_type(tz, trip, &type); |
565 | if (result) | |
566 | return result; | |
567 | ||
568 | switch (type) { | |
569 | case THERMAL_TRIP_CRITICAL: | |
625120a4 | 570 | return sprintf(buf, "critical\n"); |
6503e5df | 571 | case THERMAL_TRIP_HOT: |
625120a4 | 572 | return sprintf(buf, "hot\n"); |
6503e5df | 573 | case THERMAL_TRIP_PASSIVE: |
625120a4 | 574 | return sprintf(buf, "passive\n"); |
6503e5df | 575 | case THERMAL_TRIP_ACTIVE: |
625120a4 | 576 | return sprintf(buf, "active\n"); |
6503e5df | 577 | default: |
625120a4 | 578 | return sprintf(buf, "unknown\n"); |
6503e5df | 579 | } |
203d3d4a ZR |
580 | } |
581 | ||
c56f5c03 D |
582 | static ssize_t |
583 | trip_point_temp_store(struct device *dev, struct device_attribute *attr, | |
584 | const char *buf, size_t count) | |
585 | { | |
586 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
587 | int trip, ret; | |
588 | unsigned long temperature; | |
589 | ||
590 | if (!tz->ops->set_trip_temp) | |
591 | return -EPERM; | |
592 | ||
593 | if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip)) | |
594 | return -EINVAL; | |
595 | ||
596 | if (kstrtoul(buf, 10, &temperature)) | |
597 | return -EINVAL; | |
598 | ||
599 | ret = tz->ops->set_trip_temp(tz, trip, temperature); | |
600 | ||
601 | return ret ? ret : count; | |
602 | } | |
603 | ||
203d3d4a ZR |
604 | static ssize_t |
605 | trip_point_temp_show(struct device *dev, struct device_attribute *attr, | |
606 | char *buf) | |
607 | { | |
608 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
6503e5df MG |
609 | int trip, ret; |
610 | long temperature; | |
203d3d4a ZR |
611 | |
612 | if (!tz->ops->get_trip_temp) | |
613 | return -EPERM; | |
614 | ||
615 | if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip)) | |
616 | return -EINVAL; | |
617 | ||
6503e5df MG |
618 | ret = tz->ops->get_trip_temp(tz, trip, &temperature); |
619 | ||
620 | if (ret) | |
621 | return ret; | |
622 | ||
623 | return sprintf(buf, "%ld\n", temperature); | |
203d3d4a ZR |
624 | } |
625 | ||
27365a6c D |
626 | static ssize_t |
627 | trip_point_hyst_store(struct device *dev, struct device_attribute *attr, | |
628 | const char *buf, size_t count) | |
629 | { | |
630 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
631 | int trip, ret; | |
632 | unsigned long temperature; | |
633 | ||
634 | if (!tz->ops->set_trip_hyst) | |
635 | return -EPERM; | |
636 | ||
637 | if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip)) | |
638 | return -EINVAL; | |
639 | ||
640 | if (kstrtoul(buf, 10, &temperature)) | |
641 | return -EINVAL; | |
642 | ||
643 | /* | |
644 | * We are not doing any check on the 'temperature' value | |
645 | * here. The driver implementing 'set_trip_hyst' has to | |
646 | * take care of this. | |
647 | */ | |
648 | ret = tz->ops->set_trip_hyst(tz, trip, temperature); | |
649 | ||
650 | return ret ? ret : count; | |
651 | } | |
652 | ||
653 | static ssize_t | |
654 | trip_point_hyst_show(struct device *dev, struct device_attribute *attr, | |
655 | char *buf) | |
656 | { | |
657 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
658 | int trip, ret; | |
659 | unsigned long temperature; | |
660 | ||
661 | if (!tz->ops->get_trip_hyst) | |
662 | return -EPERM; | |
663 | ||
664 | if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip)) | |
665 | return -EINVAL; | |
666 | ||
667 | ret = tz->ops->get_trip_hyst(tz, trip, &temperature); | |
668 | ||
669 | return ret ? ret : sprintf(buf, "%ld\n", temperature); | |
670 | } | |
671 | ||
03a971a2 MG |
672 | static ssize_t |
673 | passive_store(struct device *dev, struct device_attribute *attr, | |
674 | const char *buf, size_t count) | |
675 | { | |
676 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
677 | struct thermal_cooling_device *cdev = NULL; | |
678 | int state; | |
679 | ||
680 | if (!sscanf(buf, "%d\n", &state)) | |
681 | return -EINVAL; | |
682 | ||
3d8e3ad8 FP |
683 | /* sanity check: values below 1000 millicelcius don't make sense |
684 | * and can cause the system to go into a thermal heart attack | |
685 | */ | |
686 | if (state && state < 1000) | |
687 | return -EINVAL; | |
688 | ||
03a971a2 MG |
689 | if (state && !tz->forced_passive) { |
690 | mutex_lock(&thermal_list_lock); | |
691 | list_for_each_entry(cdev, &thermal_cdev_list, node) { | |
692 | if (!strncmp("Processor", cdev->type, | |
693 | sizeof("Processor"))) | |
694 | thermal_zone_bind_cooling_device(tz, | |
9d99842f ZR |
695 | THERMAL_TRIPS_NONE, cdev, |
696 | THERMAL_NO_LIMIT, | |
697 | THERMAL_NO_LIMIT); | |
03a971a2 MG |
698 | } |
699 | mutex_unlock(&thermal_list_lock); | |
e4143b03 FP |
700 | if (!tz->passive_delay) |
701 | tz->passive_delay = 1000; | |
03a971a2 MG |
702 | } else if (!state && tz->forced_passive) { |
703 | mutex_lock(&thermal_list_lock); | |
704 | list_for_each_entry(cdev, &thermal_cdev_list, node) { | |
705 | if (!strncmp("Processor", cdev->type, | |
706 | sizeof("Processor"))) | |
707 | thermal_zone_unbind_cooling_device(tz, | |
708 | THERMAL_TRIPS_NONE, | |
709 | cdev); | |
710 | } | |
711 | mutex_unlock(&thermal_list_lock); | |
e4143b03 | 712 | tz->passive_delay = 0; |
03a971a2 MG |
713 | } |
714 | ||
03a971a2 MG |
715 | tz->forced_passive = state; |
716 | ||
717 | thermal_zone_device_update(tz); | |
718 | ||
719 | return count; | |
720 | } | |
721 | ||
722 | static ssize_t | |
723 | passive_show(struct device *dev, struct device_attribute *attr, | |
724 | char *buf) | |
725 | { | |
726 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
727 | ||
728 | return sprintf(buf, "%d\n", tz->forced_passive); | |
729 | } | |
730 | ||
5a2c090b D |
731 | static ssize_t |
732 | policy_store(struct device *dev, struct device_attribute *attr, | |
733 | const char *buf, size_t count) | |
734 | { | |
735 | int ret = -EINVAL; | |
736 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
737 | struct thermal_governor *gov; | |
42a5bf50 AS |
738 | char name[THERMAL_NAME_LENGTH]; |
739 | ||
740 | snprintf(name, sizeof(name), "%s", buf); | |
5a2c090b D |
741 | |
742 | mutex_lock(&thermal_governor_lock); | |
743 | ||
42a5bf50 | 744 | gov = __find_governor(strim(name)); |
5a2c090b D |
745 | if (!gov) |
746 | goto exit; | |
747 | ||
748 | tz->governor = gov; | |
749 | ret = count; | |
750 | ||
751 | exit: | |
752 | mutex_unlock(&thermal_governor_lock); | |
753 | return ret; | |
754 | } | |
755 | ||
756 | static ssize_t | |
757 | policy_show(struct device *dev, struct device_attribute *devattr, char *buf) | |
758 | { | |
759 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
760 | ||
761 | return sprintf(buf, "%s\n", tz->governor->name); | |
762 | } | |
763 | ||
e6e238c3 ADK |
764 | #ifdef CONFIG_THERMAL_EMULATION |
765 | static ssize_t | |
766 | emul_temp_store(struct device *dev, struct device_attribute *attr, | |
767 | const char *buf, size_t count) | |
768 | { | |
769 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
770 | int ret = 0; | |
771 | unsigned long temperature; | |
772 | ||
773 | if (kstrtoul(buf, 10, &temperature)) | |
774 | return -EINVAL; | |
775 | ||
776 | if (!tz->ops->set_emul_temp) { | |
777 | mutex_lock(&tz->lock); | |
778 | tz->emul_temperature = temperature; | |
779 | mutex_unlock(&tz->lock); | |
780 | } else { | |
781 | ret = tz->ops->set_emul_temp(tz, temperature); | |
782 | } | |
783 | ||
800744bf T |
784 | if (!ret) |
785 | thermal_zone_device_update(tz); | |
786 | ||
e6e238c3 ADK |
787 | return ret ? ret : count; |
788 | } | |
789 | static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store); | |
790 | #endif/*CONFIG_THERMAL_EMULATION*/ | |
791 | ||
203d3d4a ZR |
792 | static DEVICE_ATTR(type, 0444, type_show, NULL); |
793 | static DEVICE_ATTR(temp, 0444, temp_show, NULL); | |
794 | static DEVICE_ATTR(mode, 0644, mode_show, mode_store); | |
886ee546 | 795 | static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store); |
5a2c090b | 796 | static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store); |
203d3d4a | 797 | |
203d3d4a ZR |
798 | /* sys I/F for cooling device */ |
799 | #define to_cooling_device(_dev) \ | |
800 | container_of(_dev, struct thermal_cooling_device, device) | |
801 | ||
802 | static ssize_t | |
803 | thermal_cooling_device_type_show(struct device *dev, | |
804 | struct device_attribute *attr, char *buf) | |
805 | { | |
806 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
807 | ||
808 | return sprintf(buf, "%s\n", cdev->type); | |
809 | } | |
810 | ||
811 | static ssize_t | |
812 | thermal_cooling_device_max_state_show(struct device *dev, | |
813 | struct device_attribute *attr, char *buf) | |
814 | { | |
815 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
6503e5df MG |
816 | unsigned long state; |
817 | int ret; | |
203d3d4a | 818 | |
6503e5df MG |
819 | ret = cdev->ops->get_max_state(cdev, &state); |
820 | if (ret) | |
821 | return ret; | |
822 | return sprintf(buf, "%ld\n", state); | |
203d3d4a ZR |
823 | } |
824 | ||
825 | static ssize_t | |
826 | thermal_cooling_device_cur_state_show(struct device *dev, | |
827 | struct device_attribute *attr, char *buf) | |
828 | { | |
829 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
6503e5df MG |
830 | unsigned long state; |
831 | int ret; | |
203d3d4a | 832 | |
6503e5df MG |
833 | ret = cdev->ops->get_cur_state(cdev, &state); |
834 | if (ret) | |
835 | return ret; | |
836 | return sprintf(buf, "%ld\n", state); | |
203d3d4a ZR |
837 | } |
838 | ||
839 | static ssize_t | |
840 | thermal_cooling_device_cur_state_store(struct device *dev, | |
841 | struct device_attribute *attr, | |
842 | const char *buf, size_t count) | |
843 | { | |
844 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
6503e5df | 845 | unsigned long state; |
203d3d4a ZR |
846 | int result; |
847 | ||
6503e5df | 848 | if (!sscanf(buf, "%ld\n", &state)) |
203d3d4a ZR |
849 | return -EINVAL; |
850 | ||
edb94918 | 851 | if ((long)state < 0) |
203d3d4a ZR |
852 | return -EINVAL; |
853 | ||
854 | result = cdev->ops->set_cur_state(cdev, state); | |
855 | if (result) | |
856 | return result; | |
857 | return count; | |
858 | } | |
859 | ||
860 | static struct device_attribute dev_attr_cdev_type = | |
543a9561 | 861 | __ATTR(type, 0444, thermal_cooling_device_type_show, NULL); |
203d3d4a ZR |
862 | static DEVICE_ATTR(max_state, 0444, |
863 | thermal_cooling_device_max_state_show, NULL); | |
864 | static DEVICE_ATTR(cur_state, 0644, | |
865 | thermal_cooling_device_cur_state_show, | |
866 | thermal_cooling_device_cur_state_store); | |
867 | ||
868 | static ssize_t | |
869 | thermal_cooling_device_trip_point_show(struct device *dev, | |
543a9561 | 870 | struct device_attribute *attr, char *buf) |
203d3d4a | 871 | { |
b81b6ba3 | 872 | struct thermal_instance *instance; |
203d3d4a ZR |
873 | |
874 | instance = | |
b81b6ba3 | 875 | container_of(attr, struct thermal_instance, attr); |
203d3d4a ZR |
876 | |
877 | if (instance->trip == THERMAL_TRIPS_NONE) | |
878 | return sprintf(buf, "-1\n"); | |
879 | else | |
880 | return sprintf(buf, "%d\n", instance->trip); | |
881 | } | |
882 | ||
883 | /* Device management */ | |
884 | ||
885 | /** | |
d2e4eb83 EV |
886 | * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone |
887 | * @tz: pointer to struct thermal_zone_device | |
203d3d4a ZR |
888 | * @trip: indicates which trip point the cooling devices is |
889 | * associated with in this thermal zone. | |
d2e4eb83 EV |
890 | * @cdev: pointer to struct thermal_cooling_device |
891 | * @upper: the Maximum cooling state for this trip point. | |
892 | * THERMAL_NO_LIMIT means no upper limit, | |
893 | * and the cooling device can be in max_state. | |
894 | * @lower: the Minimum cooling state can be used for this trip point. | |
895 | * THERMAL_NO_LIMIT means no lower limit, | |
896 | * and the cooling device can be in cooling state 0. | |
543a9561 | 897 | * |
d2e4eb83 EV |
898 | * This interface function bind a thermal cooling device to the certain trip |
899 | * point of a thermal zone device. | |
543a9561 | 900 | * This function is usually called in the thermal zone device .bind callback. |
d2e4eb83 EV |
901 | * |
902 | * Return: 0 on success, the proper error value otherwise. | |
203d3d4a ZR |
903 | */ |
904 | int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, | |
905 | int trip, | |
9d99842f ZR |
906 | struct thermal_cooling_device *cdev, |
907 | unsigned long upper, unsigned long lower) | |
203d3d4a | 908 | { |
b81b6ba3 ZR |
909 | struct thermal_instance *dev; |
910 | struct thermal_instance *pos; | |
c7516709 TS |
911 | struct thermal_zone_device *pos1; |
912 | struct thermal_cooling_device *pos2; | |
74051ba5 | 913 | unsigned long max_state; |
203d3d4a ZR |
914 | int result; |
915 | ||
543a9561 | 916 | if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE)) |
203d3d4a ZR |
917 | return -EINVAL; |
918 | ||
c7516709 TS |
919 | list_for_each_entry(pos1, &thermal_tz_list, node) { |
920 | if (pos1 == tz) | |
921 | break; | |
922 | } | |
923 | list_for_each_entry(pos2, &thermal_cdev_list, node) { | |
924 | if (pos2 == cdev) | |
925 | break; | |
926 | } | |
927 | ||
928 | if (tz != pos1 || cdev != pos2) | |
203d3d4a ZR |
929 | return -EINVAL; |
930 | ||
9d99842f ZR |
931 | cdev->ops->get_max_state(cdev, &max_state); |
932 | ||
933 | /* lower default 0, upper default max_state */ | |
934 | lower = lower == THERMAL_NO_LIMIT ? 0 : lower; | |
935 | upper = upper == THERMAL_NO_LIMIT ? max_state : upper; | |
936 | ||
937 | if (lower > upper || upper > max_state) | |
938 | return -EINVAL; | |
939 | ||
203d3d4a | 940 | dev = |
b81b6ba3 | 941 | kzalloc(sizeof(struct thermal_instance), GFP_KERNEL); |
203d3d4a ZR |
942 | if (!dev) |
943 | return -ENOMEM; | |
944 | dev->tz = tz; | |
945 | dev->cdev = cdev; | |
946 | dev->trip = trip; | |
9d99842f ZR |
947 | dev->upper = upper; |
948 | dev->lower = lower; | |
ce119f83 | 949 | dev->target = THERMAL_NO_TARGET; |
74051ba5 | 950 | |
203d3d4a ZR |
951 | result = get_idr(&tz->idr, &tz->lock, &dev->id); |
952 | if (result) | |
953 | goto free_mem; | |
954 | ||
955 | sprintf(dev->name, "cdev%d", dev->id); | |
956 | result = | |
957 | sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name); | |
958 | if (result) | |
959 | goto release_idr; | |
960 | ||
961 | sprintf(dev->attr_name, "cdev%d_trip_point", dev->id); | |
975f8c56 | 962 | sysfs_attr_init(&dev->attr.attr); |
203d3d4a ZR |
963 | dev->attr.attr.name = dev->attr_name; |
964 | dev->attr.attr.mode = 0444; | |
965 | dev->attr.show = thermal_cooling_device_trip_point_show; | |
966 | result = device_create_file(&tz->device, &dev->attr); | |
967 | if (result) | |
968 | goto remove_symbol_link; | |
969 | ||
970 | mutex_lock(&tz->lock); | |
f4a821ce | 971 | mutex_lock(&cdev->lock); |
cddf31b3 | 972 | list_for_each_entry(pos, &tz->thermal_instances, tz_node) |
203d3d4a ZR |
973 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
974 | result = -EEXIST; | |
975 | break; | |
976 | } | |
b5e4ae62 | 977 | if (!result) { |
cddf31b3 | 978 | list_add_tail(&dev->tz_node, &tz->thermal_instances); |
b5e4ae62 ZR |
979 | list_add_tail(&dev->cdev_node, &cdev->thermal_instances); |
980 | } | |
f4a821ce | 981 | mutex_unlock(&cdev->lock); |
203d3d4a ZR |
982 | mutex_unlock(&tz->lock); |
983 | ||
984 | if (!result) | |
985 | return 0; | |
986 | ||
987 | device_remove_file(&tz->device, &dev->attr); | |
caca8b80 | 988 | remove_symbol_link: |
203d3d4a | 989 | sysfs_remove_link(&tz->device.kobj, dev->name); |
caca8b80 | 990 | release_idr: |
203d3d4a | 991 | release_idr(&tz->idr, &tz->lock, dev->id); |
caca8b80 | 992 | free_mem: |
203d3d4a ZR |
993 | kfree(dev); |
994 | return result; | |
995 | } | |
910cb1e3 | 996 | EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device); |
203d3d4a ZR |
997 | |
998 | /** | |
9892e5dc EV |
999 | * thermal_zone_unbind_cooling_device() - unbind a cooling device from a |
1000 | * thermal zone. | |
1001 | * @tz: pointer to a struct thermal_zone_device. | |
203d3d4a ZR |
1002 | * @trip: indicates which trip point the cooling devices is |
1003 | * associated with in this thermal zone. | |
9892e5dc | 1004 | * @cdev: pointer to a struct thermal_cooling_device. |
543a9561 | 1005 | * |
9892e5dc EV |
1006 | * This interface function unbind a thermal cooling device from the certain |
1007 | * trip point of a thermal zone device. | |
543a9561 | 1008 | * This function is usually called in the thermal zone device .unbind callback. |
9892e5dc EV |
1009 | * |
1010 | * Return: 0 on success, the proper error value otherwise. | |
203d3d4a ZR |
1011 | */ |
1012 | int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz, | |
1013 | int trip, | |
1014 | struct thermal_cooling_device *cdev) | |
1015 | { | |
b81b6ba3 | 1016 | struct thermal_instance *pos, *next; |
203d3d4a ZR |
1017 | |
1018 | mutex_lock(&tz->lock); | |
f4a821ce | 1019 | mutex_lock(&cdev->lock); |
cddf31b3 | 1020 | list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) { |
543a9561 | 1021 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
cddf31b3 | 1022 | list_del(&pos->tz_node); |
b5e4ae62 | 1023 | list_del(&pos->cdev_node); |
f4a821ce | 1024 | mutex_unlock(&cdev->lock); |
203d3d4a ZR |
1025 | mutex_unlock(&tz->lock); |
1026 | goto unbind; | |
1027 | } | |
1028 | } | |
f4a821ce | 1029 | mutex_unlock(&cdev->lock); |
203d3d4a ZR |
1030 | mutex_unlock(&tz->lock); |
1031 | ||
1032 | return -ENODEV; | |
1033 | ||
caca8b80 | 1034 | unbind: |
203d3d4a ZR |
1035 | device_remove_file(&tz->device, &pos->attr); |
1036 | sysfs_remove_link(&tz->device.kobj, pos->name); | |
1037 | release_idr(&tz->idr, &tz->lock, pos->id); | |
1038 | kfree(pos); | |
1039 | return 0; | |
1040 | } | |
910cb1e3 | 1041 | EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device); |
203d3d4a ZR |
1042 | |
1043 | static void thermal_release(struct device *dev) | |
1044 | { | |
1045 | struct thermal_zone_device *tz; | |
1046 | struct thermal_cooling_device *cdev; | |
1047 | ||
caca8b80 JP |
1048 | if (!strncmp(dev_name(dev), "thermal_zone", |
1049 | sizeof("thermal_zone") - 1)) { | |
203d3d4a ZR |
1050 | tz = to_thermal_zone(dev); |
1051 | kfree(tz); | |
732e4c8d | 1052 | } else if(!strncmp(dev_name(dev), "cooling_device", |
1053 | sizeof("cooling_device") - 1)){ | |
203d3d4a ZR |
1054 | cdev = to_cooling_device(dev); |
1055 | kfree(cdev); | |
1056 | } | |
1057 | } | |
1058 | ||
1059 | static struct class thermal_class = { | |
1060 | .name = "thermal", | |
1061 | .dev_release = thermal_release, | |
1062 | }; | |
1063 | ||
1064 | /** | |
a116b5d4 EV |
1065 | * __thermal_cooling_device_register() - register a new thermal cooling device |
1066 | * @np: a pointer to a device tree node. | |
203d3d4a ZR |
1067 | * @type: the thermal cooling device type. |
1068 | * @devdata: device private data. | |
1069 | * @ops: standard thermal cooling devices callbacks. | |
3a6eccb3 EV |
1070 | * |
1071 | * This interface function adds a new thermal cooling device (fan/processor/...) | |
1072 | * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself | |
1073 | * to all the thermal zone devices registered at the same time. | |
a116b5d4 EV |
1074 | * It also gives the opportunity to link the cooling device to a device tree |
1075 | * node, so that it can be bound to a thermal zone created out of device tree. | |
3a6eccb3 EV |
1076 | * |
1077 | * Return: a pointer to the created struct thermal_cooling_device or an | |
1078 | * ERR_PTR. Caller must check return value with IS_ERR*() helpers. | |
203d3d4a | 1079 | */ |
a116b5d4 EV |
1080 | static struct thermal_cooling_device * |
1081 | __thermal_cooling_device_register(struct device_node *np, | |
1082 | char *type, void *devdata, | |
1083 | const struct thermal_cooling_device_ops *ops) | |
203d3d4a ZR |
1084 | { |
1085 | struct thermal_cooling_device *cdev; | |
203d3d4a ZR |
1086 | int result; |
1087 | ||
204dd1d3 | 1088 | if (type && strlen(type) >= THERMAL_NAME_LENGTH) |
3e6fda5c | 1089 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
1090 | |
1091 | if (!ops || !ops->get_max_state || !ops->get_cur_state || | |
543a9561 | 1092 | !ops->set_cur_state) |
3e6fda5c | 1093 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
1094 | |
1095 | cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL); | |
1096 | if (!cdev) | |
3e6fda5c | 1097 | return ERR_PTR(-ENOMEM); |
203d3d4a ZR |
1098 | |
1099 | result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id); | |
1100 | if (result) { | |
1101 | kfree(cdev); | |
3e6fda5c | 1102 | return ERR_PTR(result); |
203d3d4a ZR |
1103 | } |
1104 | ||
c7a8b9d9 | 1105 | strlcpy(cdev->type, type ? : "", sizeof(cdev->type)); |
f4a821ce | 1106 | mutex_init(&cdev->lock); |
b5e4ae62 | 1107 | INIT_LIST_HEAD(&cdev->thermal_instances); |
a116b5d4 | 1108 | cdev->np = np; |
203d3d4a | 1109 | cdev->ops = ops; |
5ca0cce5 | 1110 | cdev->updated = false; |
203d3d4a ZR |
1111 | cdev->device.class = &thermal_class; |
1112 | cdev->devdata = devdata; | |
354655ea | 1113 | dev_set_name(&cdev->device, "cooling_device%d", cdev->id); |
203d3d4a ZR |
1114 | result = device_register(&cdev->device); |
1115 | if (result) { | |
1116 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); | |
1117 | kfree(cdev); | |
3e6fda5c | 1118 | return ERR_PTR(result); |
203d3d4a ZR |
1119 | } |
1120 | ||
1121 | /* sys I/F */ | |
1122 | if (type) { | |
543a9561 | 1123 | result = device_create_file(&cdev->device, &dev_attr_cdev_type); |
203d3d4a ZR |
1124 | if (result) |
1125 | goto unregister; | |
1126 | } | |
1127 | ||
1128 | result = device_create_file(&cdev->device, &dev_attr_max_state); | |
1129 | if (result) | |
1130 | goto unregister; | |
1131 | ||
1132 | result = device_create_file(&cdev->device, &dev_attr_cur_state); | |
1133 | if (result) | |
1134 | goto unregister; | |
1135 | ||
7e8ee1e9 | 1136 | /* Add 'this' new cdev to the global cdev list */ |
203d3d4a ZR |
1137 | mutex_lock(&thermal_list_lock); |
1138 | list_add(&cdev->node, &thermal_cdev_list); | |
203d3d4a ZR |
1139 | mutex_unlock(&thermal_list_lock); |
1140 | ||
7e8ee1e9 D |
1141 | /* Update binding information for 'this' new cdev */ |
1142 | bind_cdev(cdev); | |
1143 | ||
1144 | return cdev; | |
203d3d4a | 1145 | |
caca8b80 | 1146 | unregister: |
203d3d4a ZR |
1147 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); |
1148 | device_unregister(&cdev->device); | |
3e6fda5c | 1149 | return ERR_PTR(result); |
203d3d4a | 1150 | } |
a116b5d4 EV |
1151 | |
1152 | /** | |
1153 | * thermal_cooling_device_register() - register a new thermal cooling device | |
1154 | * @type: the thermal cooling device type. | |
1155 | * @devdata: device private data. | |
1156 | * @ops: standard thermal cooling devices callbacks. | |
1157 | * | |
1158 | * This interface function adds a new thermal cooling device (fan/processor/...) | |
1159 | * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself | |
1160 | * to all the thermal zone devices registered at the same time. | |
1161 | * | |
1162 | * Return: a pointer to the created struct thermal_cooling_device or an | |
1163 | * ERR_PTR. Caller must check return value with IS_ERR*() helpers. | |
1164 | */ | |
1165 | struct thermal_cooling_device * | |
1166 | thermal_cooling_device_register(char *type, void *devdata, | |
1167 | const struct thermal_cooling_device_ops *ops) | |
1168 | { | |
1169 | return __thermal_cooling_device_register(NULL, type, devdata, ops); | |
1170 | } | |
910cb1e3 | 1171 | EXPORT_SYMBOL_GPL(thermal_cooling_device_register); |
203d3d4a | 1172 | |
a116b5d4 EV |
1173 | /** |
1174 | * thermal_of_cooling_device_register() - register an OF thermal cooling device | |
1175 | * @np: a pointer to a device tree node. | |
1176 | * @type: the thermal cooling device type. | |
1177 | * @devdata: device private data. | |
1178 | * @ops: standard thermal cooling devices callbacks. | |
1179 | * | |
1180 | * This function will register a cooling device with device tree node reference. | |
1181 | * This interface function adds a new thermal cooling device (fan/processor/...) | |
1182 | * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself | |
1183 | * to all the thermal zone devices registered at the same time. | |
1184 | * | |
1185 | * Return: a pointer to the created struct thermal_cooling_device or an | |
1186 | * ERR_PTR. Caller must check return value with IS_ERR*() helpers. | |
1187 | */ | |
1188 | struct thermal_cooling_device * | |
1189 | thermal_of_cooling_device_register(struct device_node *np, | |
1190 | char *type, void *devdata, | |
1191 | const struct thermal_cooling_device_ops *ops) | |
1192 | { | |
1193 | return __thermal_cooling_device_register(np, type, devdata, ops); | |
1194 | } | |
1195 | EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register); | |
1196 | ||
203d3d4a ZR |
1197 | /** |
1198 | * thermal_cooling_device_unregister - removes the registered thermal cooling device | |
203d3d4a ZR |
1199 | * @cdev: the thermal cooling device to remove. |
1200 | * | |
1201 | * thermal_cooling_device_unregister() must be called when the device is no | |
1202 | * longer needed. | |
1203 | */ | |
7e8ee1e9 | 1204 | void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev) |
203d3d4a | 1205 | { |
7e8ee1e9 D |
1206 | int i; |
1207 | const struct thermal_zone_params *tzp; | |
203d3d4a ZR |
1208 | struct thermal_zone_device *tz; |
1209 | struct thermal_cooling_device *pos = NULL; | |
1210 | ||
1211 | if (!cdev) | |
1212 | return; | |
1213 | ||
1214 | mutex_lock(&thermal_list_lock); | |
1215 | list_for_each_entry(pos, &thermal_cdev_list, node) | |
1216 | if (pos == cdev) | |
1217 | break; | |
1218 | if (pos != cdev) { | |
1219 | /* thermal cooling device not found */ | |
1220 | mutex_unlock(&thermal_list_lock); | |
1221 | return; | |
1222 | } | |
1223 | list_del(&cdev->node); | |
7e8ee1e9 D |
1224 | |
1225 | /* Unbind all thermal zones associated with 'this' cdev */ | |
203d3d4a | 1226 | list_for_each_entry(tz, &thermal_tz_list, node) { |
7e8ee1e9 D |
1227 | if (tz->ops->unbind) { |
1228 | tz->ops->unbind(tz, cdev); | |
1229 | continue; | |
1230 | } | |
1231 | ||
1232 | if (!tz->tzp || !tz->tzp->tbp) | |
203d3d4a | 1233 | continue; |
7e8ee1e9 D |
1234 | |
1235 | tzp = tz->tzp; | |
1236 | for (i = 0; i < tzp->num_tbps; i++) { | |
1237 | if (tzp->tbp[i].cdev == cdev) { | |
1238 | __unbind(tz, tzp->tbp[i].trip_mask, cdev); | |
1239 | tzp->tbp[i].cdev = NULL; | |
1240 | } | |
1241 | } | |
203d3d4a | 1242 | } |
7e8ee1e9 | 1243 | |
203d3d4a | 1244 | mutex_unlock(&thermal_list_lock); |
7e8ee1e9 | 1245 | |
203d3d4a | 1246 | if (cdev->type[0]) |
543a9561 | 1247 | device_remove_file(&cdev->device, &dev_attr_cdev_type); |
203d3d4a ZR |
1248 | device_remove_file(&cdev->device, &dev_attr_max_state); |
1249 | device_remove_file(&cdev->device, &dev_attr_cur_state); | |
1250 | ||
1251 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); | |
1252 | device_unregister(&cdev->device); | |
1253 | return; | |
1254 | } | |
910cb1e3 | 1255 | EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister); |
203d3d4a | 1256 | |
dc765482 | 1257 | void thermal_cdev_update(struct thermal_cooling_device *cdev) |
ce119f83 ZR |
1258 | { |
1259 | struct thermal_instance *instance; | |
1260 | unsigned long target = 0; | |
1261 | ||
1262 | /* cooling device is updated*/ | |
1263 | if (cdev->updated) | |
1264 | return; | |
1265 | ||
f4a821ce | 1266 | mutex_lock(&cdev->lock); |
ce119f83 ZR |
1267 | /* Make sure cdev enters the deepest cooling state */ |
1268 | list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) { | |
06475b55 AL |
1269 | dev_dbg(&cdev->device, "zone%d->target=%lu\n", |
1270 | instance->tz->id, instance->target); | |
ce119f83 ZR |
1271 | if (instance->target == THERMAL_NO_TARGET) |
1272 | continue; | |
1273 | if (instance->target > target) | |
1274 | target = instance->target; | |
1275 | } | |
f4a821ce | 1276 | mutex_unlock(&cdev->lock); |
ce119f83 ZR |
1277 | cdev->ops->set_cur_state(cdev, target); |
1278 | cdev->updated = true; | |
06475b55 | 1279 | dev_dbg(&cdev->device, "set to state %lu\n", target); |
ce119f83 | 1280 | } |
dc765482 | 1281 | EXPORT_SYMBOL(thermal_cdev_update); |
ce119f83 | 1282 | |
f2b4caaf | 1283 | /** |
7b73c993 | 1284 | * thermal_notify_framework - Sensor drivers use this API to notify framework |
f2b4caaf D |
1285 | * @tz: thermal zone device |
1286 | * @trip: indicates which trip point has been crossed | |
1287 | * | |
1288 | * This function handles the trip events from sensor drivers. It starts | |
1289 | * throttling the cooling devices according to the policy configured. | |
1290 | * For CRITICAL and HOT trip points, this notifies the respective drivers, | |
1291 | * and does actual throttling for other trip points i.e ACTIVE and PASSIVE. | |
1292 | * The throttling policy is based on the configured platform data; if no | |
1293 | * platform data is provided, this uses the step_wise throttling policy. | |
1294 | */ | |
7b73c993 | 1295 | void thermal_notify_framework(struct thermal_zone_device *tz, int trip) |
f2b4caaf D |
1296 | { |
1297 | handle_thermal_trip(tz, trip); | |
1298 | } | |
910cb1e3 | 1299 | EXPORT_SYMBOL_GPL(thermal_notify_framework); |
f2b4caaf | 1300 | |
c56f5c03 | 1301 | /** |
269c174f | 1302 | * create_trip_attrs() - create attributes for trip points |
c56f5c03 D |
1303 | * @tz: the thermal zone device |
1304 | * @mask: Writeable trip point bitmap. | |
269c174f EV |
1305 | * |
1306 | * helper function to instantiate sysfs entries for every trip | |
1307 | * point and its properties of a struct thermal_zone_device. | |
1308 | * | |
1309 | * Return: 0 on success, the proper error value otherwise. | |
c56f5c03 D |
1310 | */ |
1311 | static int create_trip_attrs(struct thermal_zone_device *tz, int mask) | |
1312 | { | |
1313 | int indx; | |
27365a6c | 1314 | int size = sizeof(struct thermal_attr) * tz->trips; |
c56f5c03 | 1315 | |
27365a6c | 1316 | tz->trip_type_attrs = kzalloc(size, GFP_KERNEL); |
c56f5c03 D |
1317 | if (!tz->trip_type_attrs) |
1318 | return -ENOMEM; | |
1319 | ||
27365a6c | 1320 | tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL); |
c56f5c03 D |
1321 | if (!tz->trip_temp_attrs) { |
1322 | kfree(tz->trip_type_attrs); | |
1323 | return -ENOMEM; | |
1324 | } | |
1325 | ||
27365a6c D |
1326 | if (tz->ops->get_trip_hyst) { |
1327 | tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL); | |
1328 | if (!tz->trip_hyst_attrs) { | |
1329 | kfree(tz->trip_type_attrs); | |
1330 | kfree(tz->trip_temp_attrs); | |
1331 | return -ENOMEM; | |
1332 | } | |
1333 | } | |
c56f5c03 | 1334 | |
27365a6c D |
1335 | |
1336 | for (indx = 0; indx < tz->trips; indx++) { | |
c56f5c03 D |
1337 | /* create trip type attribute */ |
1338 | snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH, | |
1339 | "trip_point_%d_type", indx); | |
1340 | ||
1341 | sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr); | |
1342 | tz->trip_type_attrs[indx].attr.attr.name = | |
1343 | tz->trip_type_attrs[indx].name; | |
1344 | tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO; | |
1345 | tz->trip_type_attrs[indx].attr.show = trip_point_type_show; | |
1346 | ||
1347 | device_create_file(&tz->device, | |
1348 | &tz->trip_type_attrs[indx].attr); | |
1349 | ||
1350 | /* create trip temp attribute */ | |
1351 | snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH, | |
1352 | "trip_point_%d_temp", indx); | |
1353 | ||
1354 | sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr); | |
1355 | tz->trip_temp_attrs[indx].attr.attr.name = | |
1356 | tz->trip_temp_attrs[indx].name; | |
1357 | tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO; | |
1358 | tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show; | |
1359 | if (mask & (1 << indx)) { | |
1360 | tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR; | |
1361 | tz->trip_temp_attrs[indx].attr.store = | |
1362 | trip_point_temp_store; | |
1363 | } | |
1364 | ||
1365 | device_create_file(&tz->device, | |
1366 | &tz->trip_temp_attrs[indx].attr); | |
27365a6c D |
1367 | |
1368 | /* create Optional trip hyst attribute */ | |
1369 | if (!tz->ops->get_trip_hyst) | |
1370 | continue; | |
1371 | snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH, | |
1372 | "trip_point_%d_hyst", indx); | |
1373 | ||
1374 | sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr); | |
1375 | tz->trip_hyst_attrs[indx].attr.attr.name = | |
1376 | tz->trip_hyst_attrs[indx].name; | |
1377 | tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO; | |
1378 | tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show; | |
1379 | if (tz->ops->set_trip_hyst) { | |
1380 | tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR; | |
1381 | tz->trip_hyst_attrs[indx].attr.store = | |
1382 | trip_point_hyst_store; | |
1383 | } | |
1384 | ||
1385 | device_create_file(&tz->device, | |
1386 | &tz->trip_hyst_attrs[indx].attr); | |
c56f5c03 D |
1387 | } |
1388 | return 0; | |
1389 | } | |
1390 | ||
1391 | static void remove_trip_attrs(struct thermal_zone_device *tz) | |
1392 | { | |
1393 | int indx; | |
1394 | ||
1395 | for (indx = 0; indx < tz->trips; indx++) { | |
1396 | device_remove_file(&tz->device, | |
1397 | &tz->trip_type_attrs[indx].attr); | |
1398 | device_remove_file(&tz->device, | |
1399 | &tz->trip_temp_attrs[indx].attr); | |
27365a6c D |
1400 | if (tz->ops->get_trip_hyst) |
1401 | device_remove_file(&tz->device, | |
1402 | &tz->trip_hyst_attrs[indx].attr); | |
c56f5c03 D |
1403 | } |
1404 | kfree(tz->trip_type_attrs); | |
1405 | kfree(tz->trip_temp_attrs); | |
27365a6c | 1406 | kfree(tz->trip_hyst_attrs); |
c56f5c03 D |
1407 | } |
1408 | ||
203d3d4a | 1409 | /** |
a00e55f9 | 1410 | * thermal_zone_device_register() - register a new thermal zone device |
203d3d4a ZR |
1411 | * @type: the thermal zone device type |
1412 | * @trips: the number of trip points the thermal zone support | |
c56f5c03 | 1413 | * @mask: a bit string indicating the writeablility of trip points |
203d3d4a ZR |
1414 | * @devdata: private device data |
1415 | * @ops: standard thermal zone device callbacks | |
50125a9b | 1416 | * @tzp: thermal zone platform parameters |
b1569e99 MG |
1417 | * @passive_delay: number of milliseconds to wait between polls when |
1418 | * performing passive cooling | |
1419 | * @polling_delay: number of milliseconds to wait between polls when checking | |
1420 | * whether trip points have been crossed (0 for interrupt | |
1421 | * driven systems) | |
203d3d4a | 1422 | * |
a00e55f9 EV |
1423 | * This interface function adds a new thermal zone device (sensor) to |
1424 | * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the | |
1425 | * thermal cooling devices registered at the same time. | |
203d3d4a | 1426 | * thermal_zone_device_unregister() must be called when the device is no |
1b7ddb84 | 1427 | * longer needed. The passive cooling depends on the .get_trend() return value. |
a00e55f9 EV |
1428 | * |
1429 | * Return: a pointer to the created struct thermal_zone_device or an | |
1430 | * in case of error, an ERR_PTR. Caller must check return value with | |
1431 | * IS_ERR*() helpers. | |
203d3d4a | 1432 | */ |
4b1bf587 | 1433 | struct thermal_zone_device *thermal_zone_device_register(const char *type, |
c56f5c03 | 1434 | int trips, int mask, void *devdata, |
4e5e4705 | 1435 | struct thermal_zone_device_ops *ops, |
50125a9b | 1436 | const struct thermal_zone_params *tzp, |
1b7ddb84 | 1437 | int passive_delay, int polling_delay) |
203d3d4a ZR |
1438 | { |
1439 | struct thermal_zone_device *tz; | |
03a971a2 | 1440 | enum thermal_trip_type trip_type; |
203d3d4a ZR |
1441 | int result; |
1442 | int count; | |
03a971a2 | 1443 | int passive = 0; |
203d3d4a | 1444 | |
204dd1d3 | 1445 | if (type && strlen(type) >= THERMAL_NAME_LENGTH) |
3e6fda5c | 1446 | return ERR_PTR(-EINVAL); |
203d3d4a | 1447 | |
c56f5c03 | 1448 | if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips) |
3e6fda5c | 1449 | return ERR_PTR(-EINVAL); |
203d3d4a | 1450 | |
81bd4e1c | 1451 | if (!ops) |
3e6fda5c | 1452 | return ERR_PTR(-EINVAL); |
203d3d4a | 1453 | |
83720d0b | 1454 | if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp)) |
6b2aa51d EV |
1455 | return ERR_PTR(-EINVAL); |
1456 | ||
203d3d4a ZR |
1457 | tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL); |
1458 | if (!tz) | |
3e6fda5c | 1459 | return ERR_PTR(-ENOMEM); |
203d3d4a | 1460 | |
2d374139 | 1461 | INIT_LIST_HEAD(&tz->thermal_instances); |
203d3d4a ZR |
1462 | idr_init(&tz->idr); |
1463 | mutex_init(&tz->lock); | |
1464 | result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id); | |
1465 | if (result) { | |
1466 | kfree(tz); | |
3e6fda5c | 1467 | return ERR_PTR(result); |
203d3d4a ZR |
1468 | } |
1469 | ||
c7a8b9d9 | 1470 | strlcpy(tz->type, type ? : "", sizeof(tz->type)); |
203d3d4a | 1471 | tz->ops = ops; |
50125a9b | 1472 | tz->tzp = tzp; |
203d3d4a ZR |
1473 | tz->device.class = &thermal_class; |
1474 | tz->devdata = devdata; | |
1475 | tz->trips = trips; | |
b1569e99 MG |
1476 | tz->passive_delay = passive_delay; |
1477 | tz->polling_delay = polling_delay; | |
1478 | ||
354655ea | 1479 | dev_set_name(&tz->device, "thermal_zone%d", tz->id); |
203d3d4a ZR |
1480 | result = device_register(&tz->device); |
1481 | if (result) { | |
1482 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); | |
1483 | kfree(tz); | |
3e6fda5c | 1484 | return ERR_PTR(result); |
203d3d4a ZR |
1485 | } |
1486 | ||
1487 | /* sys I/F */ | |
1488 | if (type) { | |
1489 | result = device_create_file(&tz->device, &dev_attr_type); | |
1490 | if (result) | |
1491 | goto unregister; | |
1492 | } | |
1493 | ||
1494 | result = device_create_file(&tz->device, &dev_attr_temp); | |
1495 | if (result) | |
1496 | goto unregister; | |
1497 | ||
1498 | if (ops->get_mode) { | |
1499 | result = device_create_file(&tz->device, &dev_attr_mode); | |
1500 | if (result) | |
1501 | goto unregister; | |
1502 | } | |
1503 | ||
c56f5c03 D |
1504 | result = create_trip_attrs(tz, mask); |
1505 | if (result) | |
1506 | goto unregister; | |
1507 | ||
203d3d4a | 1508 | for (count = 0; count < trips; count++) { |
03a971a2 MG |
1509 | tz->ops->get_trip_type(tz, count, &trip_type); |
1510 | if (trip_type == THERMAL_TRIP_PASSIVE) | |
1511 | passive = 1; | |
203d3d4a ZR |
1512 | } |
1513 | ||
5a2c090b D |
1514 | if (!passive) { |
1515 | result = device_create_file(&tz->device, &dev_attr_passive); | |
1516 | if (result) | |
1517 | goto unregister; | |
1518 | } | |
03a971a2 | 1519 | |
e6e238c3 ADK |
1520 | #ifdef CONFIG_THERMAL_EMULATION |
1521 | result = device_create_file(&tz->device, &dev_attr_emul_temp); | |
1522 | if (result) | |
1523 | goto unregister; | |
1524 | #endif | |
5a2c090b D |
1525 | /* Create policy attribute */ |
1526 | result = device_create_file(&tz->device, &dev_attr_policy); | |
03a971a2 MG |
1527 | if (result) |
1528 | goto unregister; | |
1529 | ||
a4a15485 D |
1530 | /* Update 'this' zone's governor information */ |
1531 | mutex_lock(&thermal_governor_lock); | |
1532 | ||
1533 | if (tz->tzp) | |
1534 | tz->governor = __find_governor(tz->tzp->governor_name); | |
1535 | else | |
1536 | tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR); | |
1537 | ||
1538 | mutex_unlock(&thermal_governor_lock); | |
1539 | ||
ccba4ffd EV |
1540 | if (!tz->tzp || !tz->tzp->no_hwmon) { |
1541 | result = thermal_add_hwmon_sysfs(tz); | |
1542 | if (result) | |
1543 | goto unregister; | |
1544 | } | |
e68b16ab | 1545 | |
203d3d4a ZR |
1546 | mutex_lock(&thermal_list_lock); |
1547 | list_add_tail(&tz->node, &thermal_tz_list); | |
203d3d4a ZR |
1548 | mutex_unlock(&thermal_list_lock); |
1549 | ||
7e8ee1e9 D |
1550 | /* Bind cooling devices for this zone */ |
1551 | bind_tz(tz); | |
1552 | ||
b1569e99 MG |
1553 | INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check); |
1554 | ||
81bd4e1c EV |
1555 | if (!tz->ops->get_temp) |
1556 | thermal_zone_device_set_polling(tz, 0); | |
1557 | ||
b1569e99 MG |
1558 | thermal_zone_device_update(tz); |
1559 | ||
203d3d4a ZR |
1560 | if (!result) |
1561 | return tz; | |
1562 | ||
caca8b80 | 1563 | unregister: |
203d3d4a ZR |
1564 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
1565 | device_unregister(&tz->device); | |
3e6fda5c | 1566 | return ERR_PTR(result); |
203d3d4a | 1567 | } |
910cb1e3 | 1568 | EXPORT_SYMBOL_GPL(thermal_zone_device_register); |
203d3d4a ZR |
1569 | |
1570 | /** | |
1571 | * thermal_device_unregister - removes the registered thermal zone device | |
203d3d4a ZR |
1572 | * @tz: the thermal zone device to remove |
1573 | */ | |
1574 | void thermal_zone_device_unregister(struct thermal_zone_device *tz) | |
1575 | { | |
7e8ee1e9 D |
1576 | int i; |
1577 | const struct thermal_zone_params *tzp; | |
203d3d4a ZR |
1578 | struct thermal_cooling_device *cdev; |
1579 | struct thermal_zone_device *pos = NULL; | |
203d3d4a ZR |
1580 | |
1581 | if (!tz) | |
1582 | return; | |
1583 | ||
7e8ee1e9 D |
1584 | tzp = tz->tzp; |
1585 | ||
203d3d4a ZR |
1586 | mutex_lock(&thermal_list_lock); |
1587 | list_for_each_entry(pos, &thermal_tz_list, node) | |
1588 | if (pos == tz) | |
1589 | break; | |
1590 | if (pos != tz) { | |
1591 | /* thermal zone device not found */ | |
1592 | mutex_unlock(&thermal_list_lock); | |
1593 | return; | |
1594 | } | |
1595 | list_del(&tz->node); | |
7e8ee1e9 D |
1596 | |
1597 | /* Unbind all cdevs associated with 'this' thermal zone */ | |
1598 | list_for_each_entry(cdev, &thermal_cdev_list, node) { | |
1599 | if (tz->ops->unbind) { | |
1600 | tz->ops->unbind(tz, cdev); | |
1601 | continue; | |
1602 | } | |
1603 | ||
1604 | if (!tzp || !tzp->tbp) | |
1605 | break; | |
1606 | ||
1607 | for (i = 0; i < tzp->num_tbps; i++) { | |
1608 | if (tzp->tbp[i].cdev == cdev) { | |
1609 | __unbind(tz, tzp->tbp[i].trip_mask, cdev); | |
1610 | tzp->tbp[i].cdev = NULL; | |
1611 | } | |
1612 | } | |
1613 | } | |
1614 | ||
203d3d4a ZR |
1615 | mutex_unlock(&thermal_list_lock); |
1616 | ||
b1569e99 MG |
1617 | thermal_zone_device_set_polling(tz, 0); |
1618 | ||
203d3d4a ZR |
1619 | if (tz->type[0]) |
1620 | device_remove_file(&tz->device, &dev_attr_type); | |
1621 | device_remove_file(&tz->device, &dev_attr_temp); | |
1622 | if (tz->ops->get_mode) | |
1623 | device_remove_file(&tz->device, &dev_attr_mode); | |
5a2c090b | 1624 | device_remove_file(&tz->device, &dev_attr_policy); |
c56f5c03 | 1625 | remove_trip_attrs(tz); |
a4a15485 | 1626 | tz->governor = NULL; |
203d3d4a | 1627 | |
e68b16ab | 1628 | thermal_remove_hwmon_sysfs(tz); |
203d3d4a ZR |
1629 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
1630 | idr_destroy(&tz->idr); | |
1631 | mutex_destroy(&tz->lock); | |
1632 | device_unregister(&tz->device); | |
1633 | return; | |
1634 | } | |
910cb1e3 | 1635 | EXPORT_SYMBOL_GPL(thermal_zone_device_unregister); |
203d3d4a | 1636 | |
63c4d919 EV |
1637 | /** |
1638 | * thermal_zone_get_zone_by_name() - search for a zone and returns its ref | |
1639 | * @name: thermal zone name to fetch the temperature | |
1640 | * | |
1641 | * When only one zone is found with the passed name, returns a reference to it. | |
1642 | * | |
1643 | * Return: On success returns a reference to an unique thermal zone with | |
1644 | * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid | |
1645 | * paramenters, -ENODEV for not found and -EEXIST for multiple matches). | |
1646 | */ | |
1647 | struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name) | |
1648 | { | |
1649 | struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL); | |
1650 | unsigned int found = 0; | |
1651 | ||
1652 | if (!name) | |
1653 | goto exit; | |
1654 | ||
1655 | mutex_lock(&thermal_list_lock); | |
1656 | list_for_each_entry(pos, &thermal_tz_list, node) | |
1657 | if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) { | |
1658 | found++; | |
1659 | ref = pos; | |
1660 | } | |
1661 | mutex_unlock(&thermal_list_lock); | |
1662 | ||
1663 | /* nothing has been found, thus an error code for it */ | |
1664 | if (found == 0) | |
1665 | ref = ERR_PTR(-ENODEV); | |
1666 | else if (found > 1) | |
1667 | /* Success only when an unique zone is found */ | |
1668 | ref = ERR_PTR(-EEXIST); | |
1669 | ||
1670 | exit: | |
1671 | return ref; | |
1672 | } | |
1673 | EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name); | |
1674 | ||
af06216a | 1675 | #ifdef CONFIG_NET |
2a94fe48 JB |
1676 | static const struct genl_multicast_group thermal_event_mcgrps[] = { |
1677 | { .name = THERMAL_GENL_MCAST_GROUP_NAME, }, | |
1678 | }; | |
1679 | ||
af06216a RW |
1680 | static struct genl_family thermal_event_genl_family = { |
1681 | .id = GENL_ID_GENERATE, | |
1682 | .name = THERMAL_GENL_FAMILY_NAME, | |
1683 | .version = THERMAL_GENL_VERSION, | |
1684 | .maxattr = THERMAL_GENL_ATTR_MAX, | |
2a94fe48 JB |
1685 | .mcgrps = thermal_event_mcgrps, |
1686 | .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps), | |
af06216a RW |
1687 | }; |
1688 | ||
8ab3e6a0 EV |
1689 | int thermal_generate_netlink_event(struct thermal_zone_device *tz, |
1690 | enum events event) | |
4cb18728 D |
1691 | { |
1692 | struct sk_buff *skb; | |
1693 | struct nlattr *attr; | |
1694 | struct thermal_genl_event *thermal_event; | |
1695 | void *msg_header; | |
1696 | int size; | |
1697 | int result; | |
b11de07c | 1698 | static unsigned int thermal_event_seqnum; |
4cb18728 | 1699 | |
8ab3e6a0 EV |
1700 | if (!tz) |
1701 | return -EINVAL; | |
1702 | ||
4cb18728 | 1703 | /* allocate memory */ |
886ee546 JP |
1704 | size = nla_total_size(sizeof(struct thermal_genl_event)) + |
1705 | nla_total_size(0); | |
4cb18728 D |
1706 | |
1707 | skb = genlmsg_new(size, GFP_ATOMIC); | |
1708 | if (!skb) | |
1709 | return -ENOMEM; | |
1710 | ||
1711 | /* add the genetlink message header */ | |
1712 | msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++, | |
1713 | &thermal_event_genl_family, 0, | |
1714 | THERMAL_GENL_CMD_EVENT); | |
1715 | if (!msg_header) { | |
1716 | nlmsg_free(skb); | |
1717 | return -ENOMEM; | |
1718 | } | |
1719 | ||
1720 | /* fill the data */ | |
886ee546 JP |
1721 | attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT, |
1722 | sizeof(struct thermal_genl_event)); | |
4cb18728 D |
1723 | |
1724 | if (!attr) { | |
1725 | nlmsg_free(skb); | |
1726 | return -EINVAL; | |
1727 | } | |
1728 | ||
1729 | thermal_event = nla_data(attr); | |
1730 | if (!thermal_event) { | |
1731 | nlmsg_free(skb); | |
1732 | return -EINVAL; | |
1733 | } | |
1734 | ||
1735 | memset(thermal_event, 0, sizeof(struct thermal_genl_event)); | |
1736 | ||
8ab3e6a0 | 1737 | thermal_event->orig = tz->id; |
4cb18728 D |
1738 | thermal_event->event = event; |
1739 | ||
1740 | /* send multicast genetlink message */ | |
1741 | result = genlmsg_end(skb, msg_header); | |
1742 | if (result < 0) { | |
1743 | nlmsg_free(skb); | |
1744 | return result; | |
1745 | } | |
1746 | ||
68eb5503 | 1747 | result = genlmsg_multicast(&thermal_event_genl_family, skb, 0, |
2a94fe48 | 1748 | 0, GFP_ATOMIC); |
4cb18728 | 1749 | if (result) |
923e0b1e | 1750 | dev_err(&tz->device, "Failed to send netlink event:%d", result); |
4cb18728 D |
1751 | |
1752 | return result; | |
1753 | } | |
910cb1e3 | 1754 | EXPORT_SYMBOL_GPL(thermal_generate_netlink_event); |
4cb18728 D |
1755 | |
1756 | static int genetlink_init(void) | |
1757 | { | |
2a94fe48 | 1758 | return genl_register_family(&thermal_event_genl_family); |
4cb18728 D |
1759 | } |
1760 | ||
af06216a RW |
1761 | static void genetlink_exit(void) |
1762 | { | |
1763 | genl_unregister_family(&thermal_event_genl_family); | |
1764 | } | |
1765 | #else /* !CONFIG_NET */ | |
1766 | static inline int genetlink_init(void) { return 0; } | |
1767 | static inline void genetlink_exit(void) {} | |
1768 | #endif /* !CONFIG_NET */ | |
1769 | ||
80a26a5c ZR |
1770 | static int __init thermal_register_governors(void) |
1771 | { | |
1772 | int result; | |
1773 | ||
1774 | result = thermal_gov_step_wise_register(); | |
1775 | if (result) | |
1776 | return result; | |
1777 | ||
1778 | result = thermal_gov_fair_share_register(); | |
1779 | if (result) | |
1780 | return result; | |
1781 | ||
1782 | return thermal_gov_user_space_register(); | |
1783 | } | |
1784 | ||
1785 | static void thermal_unregister_governors(void) | |
1786 | { | |
1787 | thermal_gov_step_wise_unregister(); | |
1788 | thermal_gov_fair_share_unregister(); | |
1789 | thermal_gov_user_space_unregister(); | |
1790 | } | |
1791 | ||
203d3d4a ZR |
1792 | static int __init thermal_init(void) |
1793 | { | |
80a26a5c ZR |
1794 | int result; |
1795 | ||
1796 | result = thermal_register_governors(); | |
1797 | if (result) | |
1798 | goto error; | |
203d3d4a ZR |
1799 | |
1800 | result = class_register(&thermal_class); | |
80a26a5c ZR |
1801 | if (result) |
1802 | goto unregister_governors; | |
1803 | ||
4cb18728 | 1804 | result = genetlink_init(); |
80a26a5c ZR |
1805 | if (result) |
1806 | goto unregister_class; | |
1807 | ||
4e5e4705 EV |
1808 | result = of_parse_thermal_zones(); |
1809 | if (result) | |
1810 | goto exit_netlink; | |
1811 | ||
80a26a5c ZR |
1812 | return 0; |
1813 | ||
4e5e4705 EV |
1814 | exit_netlink: |
1815 | genetlink_exit(); | |
80a26a5c ZR |
1816 | unregister_governors: |
1817 | thermal_unregister_governors(); | |
1818 | unregister_class: | |
1819 | class_unregister(&thermal_class); | |
1820 | error: | |
1821 | idr_destroy(&thermal_tz_idr); | |
1822 | idr_destroy(&thermal_cdev_idr); | |
1823 | mutex_destroy(&thermal_idr_lock); | |
1824 | mutex_destroy(&thermal_list_lock); | |
1825 | mutex_destroy(&thermal_governor_lock); | |
203d3d4a ZR |
1826 | return result; |
1827 | } | |
1828 | ||
1829 | static void __exit thermal_exit(void) | |
1830 | { | |
4e5e4705 | 1831 | of_thermal_destroy_zones(); |
80a26a5c | 1832 | genetlink_exit(); |
203d3d4a | 1833 | class_unregister(&thermal_class); |
80a26a5c | 1834 | thermal_unregister_governors(); |
203d3d4a ZR |
1835 | idr_destroy(&thermal_tz_idr); |
1836 | idr_destroy(&thermal_cdev_idr); | |
1837 | mutex_destroy(&thermal_idr_lock); | |
1838 | mutex_destroy(&thermal_list_lock); | |
80a26a5c | 1839 | mutex_destroy(&thermal_governor_lock); |
203d3d4a ZR |
1840 | } |
1841 | ||
4cb18728 | 1842 | fs_initcall(thermal_init); |
203d3d4a | 1843 | module_exit(thermal_exit); |