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