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