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