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