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