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