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