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