Commit | Line | Data |
---|---|---|
7e3c0381 | 1 | // SPDX-License-Identifier: GPL-2.0 |
203d3d4a ZR |
2 | /* |
3 | * thermal.c - Generic Thermal Management Sysfs support. | |
4 | * | |
5 | * Copyright (C) 2008 Intel Corp | |
6 | * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com> | |
7 | * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com> | |
203d3d4a ZR |
8 | */ |
9 | ||
c5a01dd5 JP |
10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
11 | ||
203d3d4a ZR |
12 | #include <linux/device.h> |
13 | #include <linux/err.h> | |
3f0cfea3 | 14 | #include <linux/export.h> |
5a0e3ad6 | 15 | #include <linux/slab.h> |
203d3d4a ZR |
16 | #include <linux/kdev_t.h> |
17 | #include <linux/idr.h> | |
18 | #include <linux/thermal.h> | |
b1569e99 | 19 | #include <linux/reboot.h> |
42a5bf50 | 20 | #include <linux/string.h> |
a116b5d4 | 21 | #include <linux/of.h> |
ff140fea | 22 | #include <linux/suspend.h> |
203d3d4a | 23 | |
100a8fdb PA |
24 | #define CREATE_TRACE_POINTS |
25 | #include <trace/events/thermal.h> | |
26 | ||
71350db4 | 27 | #include "thermal_core.h" |
0dd88793 | 28 | #include "thermal_hwmon.h" |
71350db4 | 29 | |
b31ef828 MW |
30 | static DEFINE_IDA(thermal_tz_ida); |
31 | static DEFINE_IDA(thermal_cdev_ida); | |
203d3d4a ZR |
32 | |
33 | static LIST_HEAD(thermal_tz_list); | |
34 | static LIST_HEAD(thermal_cdev_list); | |
a4a15485 D |
35 | static LIST_HEAD(thermal_governor_list); |
36 | ||
203d3d4a | 37 | static DEFINE_MUTEX(thermal_list_lock); |
a4a15485 | 38 | static DEFINE_MUTEX(thermal_governor_lock); |
e441fd68 | 39 | static DEFINE_MUTEX(poweroff_lock); |
a4a15485 | 40 | |
ff140fea | 41 | static atomic_t in_suspend; |
e441fd68 | 42 | static bool power_off_triggered; |
ff140fea | 43 | |
f2234bcd ZR |
44 | static struct thermal_governor *def_governor; |
45 | ||
1b4f4849 EV |
46 | /* |
47 | * Governor section: set of functions to handle thermal governors | |
48 | * | |
49 | * Functions to help in the life cycle of thermal governors within | |
50 | * the thermal core and by the thermal governor code. | |
51 | */ | |
52 | ||
a4a15485 D |
53 | static struct thermal_governor *__find_governor(const char *name) |
54 | { | |
55 | struct thermal_governor *pos; | |
56 | ||
f2234bcd ZR |
57 | if (!name || !name[0]) |
58 | return def_governor; | |
59 | ||
a4a15485 | 60 | list_for_each_entry(pos, &thermal_governor_list, governor_list) |
484ac2f3 | 61 | if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH)) |
a4a15485 D |
62 | return pos; |
63 | ||
64 | return NULL; | |
65 | } | |
66 | ||
e33df1d2 JM |
67 | /** |
68 | * bind_previous_governor() - bind the previous governor of the thermal zone | |
69 | * @tz: a valid pointer to a struct thermal_zone_device | |
70 | * @failed_gov_name: the name of the governor that failed to register | |
71 | * | |
72 | * Register the previous governor of the thermal zone after a new | |
73 | * governor has failed to be bound. | |
74 | */ | |
75 | static void bind_previous_governor(struct thermal_zone_device *tz, | |
76 | const char *failed_gov_name) | |
77 | { | |
78 | if (tz->governor && tz->governor->bind_to_tz) { | |
79 | if (tz->governor->bind_to_tz(tz)) { | |
80 | dev_err(&tz->device, | |
81 | "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n", | |
82 | failed_gov_name, tz->governor->name, tz->type); | |
83 | tz->governor = NULL; | |
84 | } | |
85 | } | |
86 | } | |
87 | ||
88 | /** | |
89 | * thermal_set_governor() - Switch to another governor | |
90 | * @tz: a valid pointer to a struct thermal_zone_device | |
91 | * @new_gov: pointer to the new governor | |
92 | * | |
93 | * Change the governor of thermal zone @tz. | |
94 | * | |
95 | * Return: 0 on success, an error if the new governor's bind_to_tz() failed. | |
96 | */ | |
97 | static int thermal_set_governor(struct thermal_zone_device *tz, | |
98 | struct thermal_governor *new_gov) | |
99 | { | |
100 | int ret = 0; | |
101 | ||
102 | if (tz->governor && tz->governor->unbind_from_tz) | |
103 | tz->governor->unbind_from_tz(tz); | |
104 | ||
105 | if (new_gov && new_gov->bind_to_tz) { | |
106 | ret = new_gov->bind_to_tz(tz); | |
107 | if (ret) { | |
108 | bind_previous_governor(tz, new_gov->name); | |
109 | ||
110 | return ret; | |
111 | } | |
112 | } | |
113 | ||
114 | tz->governor = new_gov; | |
115 | ||
116 | return ret; | |
117 | } | |
118 | ||
a4a15485 D |
119 | int thermal_register_governor(struct thermal_governor *governor) |
120 | { | |
121 | int err; | |
122 | const char *name; | |
123 | struct thermal_zone_device *pos; | |
124 | ||
125 | if (!governor) | |
126 | return -EINVAL; | |
127 | ||
128 | mutex_lock(&thermal_governor_lock); | |
129 | ||
130 | err = -EBUSY; | |
5027ba36 | 131 | if (!__find_governor(governor->name)) { |
eb7be329 EV |
132 | bool match_default; |
133 | ||
a4a15485 D |
134 | err = 0; |
135 | list_add(&governor->governor_list, &thermal_governor_list); | |
eb7be329 EV |
136 | match_default = !strncmp(governor->name, |
137 | DEFAULT_THERMAL_GOVERNOR, | |
138 | THERMAL_NAME_LENGTH); | |
139 | ||
140 | if (!def_governor && match_default) | |
f2234bcd | 141 | def_governor = governor; |
a4a15485 D |
142 | } |
143 | ||
144 | mutex_lock(&thermal_list_lock); | |
145 | ||
146 | list_for_each_entry(pos, &thermal_tz_list, node) { | |
f2234bcd ZR |
147 | /* |
148 | * only thermal zones with specified tz->tzp->governor_name | |
149 | * may run with tz->govenor unset | |
150 | */ | |
a4a15485 D |
151 | if (pos->governor) |
152 | continue; | |
f2234bcd ZR |
153 | |
154 | name = pos->tzp->governor_name; | |
155 | ||
e33df1d2 JM |
156 | if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) { |
157 | int ret; | |
158 | ||
159 | ret = thermal_set_governor(pos, governor); | |
160 | if (ret) | |
161 | dev_err(&pos->device, | |
162 | "Failed to set governor %s for thermal zone %s: %d\n", | |
163 | governor->name, pos->type, ret); | |
164 | } | |
a4a15485 D |
165 | } |
166 | ||
167 | mutex_unlock(&thermal_list_lock); | |
168 | mutex_unlock(&thermal_governor_lock); | |
169 | ||
170 | return err; | |
171 | } | |
a4a15485 D |
172 | |
173 | void thermal_unregister_governor(struct thermal_governor *governor) | |
174 | { | |
175 | struct thermal_zone_device *pos; | |
176 | ||
177 | if (!governor) | |
178 | return; | |
179 | ||
180 | mutex_lock(&thermal_governor_lock); | |
181 | ||
5027ba36 | 182 | if (!__find_governor(governor->name)) |
a4a15485 D |
183 | goto exit; |
184 | ||
185 | mutex_lock(&thermal_list_lock); | |
186 | ||
187 | list_for_each_entry(pos, &thermal_tz_list, node) { | |
484ac2f3 | 188 | if (!strncasecmp(pos->governor->name, governor->name, |
eb7be329 | 189 | THERMAL_NAME_LENGTH)) |
e33df1d2 | 190 | thermal_set_governor(pos, NULL); |
a4a15485 D |
191 | } |
192 | ||
193 | mutex_unlock(&thermal_list_lock); | |
194 | list_del(&governor->governor_list); | |
195 | exit: | |
196 | mutex_unlock(&thermal_governor_lock); | |
9b4298a0 | 197 | } |
9b4298a0 | 198 | |
1b4f4849 EV |
199 | int thermal_zone_device_set_policy(struct thermal_zone_device *tz, |
200 | char *policy) | |
9b4298a0 | 201 | { |
1b4f4849 EV |
202 | struct thermal_governor *gov; |
203 | int ret = -EINVAL; | |
9b4298a0 | 204 | |
1b4f4849 | 205 | mutex_lock(&thermal_governor_lock); |
9b4298a0 | 206 | mutex_lock(&tz->lock); |
9b4298a0 | 207 | |
1b4f4849 EV |
208 | gov = __find_governor(strim(policy)); |
209 | if (!gov) | |
210 | goto exit; | |
9b4298a0 | 211 | |
1b4f4849 | 212 | ret = thermal_set_governor(tz, gov); |
9b4298a0 | 213 | |
1b4f4849 EV |
214 | exit: |
215 | mutex_unlock(&tz->lock); | |
216 | mutex_unlock(&thermal_governor_lock); | |
9b4298a0 | 217 | |
55cdf0a2 DL |
218 | thermal_notify_tz_gov_change(tz->id, policy); |
219 | ||
1b4f4849 | 220 | return ret; |
7e8ee1e9 D |
221 | } |
222 | ||
1b4f4849 | 223 | int thermal_build_list_of_policies(char *buf) |
7e8ee1e9 | 224 | { |
1b4f4849 EV |
225 | struct thermal_governor *pos; |
226 | ssize_t count = 0; | |
227 | ssize_t size = PAGE_SIZE; | |
7e8ee1e9 | 228 | |
1b4f4849 | 229 | mutex_lock(&thermal_governor_lock); |
a8892d83 | 230 | |
1b4f4849 EV |
231 | list_for_each_entry(pos, &thermal_governor_list, governor_list) { |
232 | size = PAGE_SIZE - count; | |
233 | count += scnprintf(buf + count, size, "%s ", pos->name); | |
7e8ee1e9 | 234 | } |
1b4f4849 | 235 | count += scnprintf(buf + count, size, "\n"); |
7e8ee1e9 | 236 | |
1b4f4849 | 237 | mutex_unlock(&thermal_governor_lock); |
7e8ee1e9 | 238 | |
1b4f4849 | 239 | return count; |
7e8ee1e9 D |
240 | } |
241 | ||
57c5b2ec | 242 | static void __init thermal_unregister_governors(void) |
7e8ee1e9 | 243 | { |
57c5b2ec DL |
244 | struct thermal_governor **governor; |
245 | ||
246 | for_each_governor_table(governor) | |
247 | thermal_unregister_governor(*governor); | |
248 | } | |
7e8ee1e9 | 249 | |
57c5b2ec DL |
250 | static int __init thermal_register_governors(void) |
251 | { | |
252 | int ret = 0; | |
253 | struct thermal_governor **governor; | |
7e8ee1e9 | 254 | |
57c5b2ec DL |
255 | for_each_governor_table(governor) { |
256 | ret = thermal_register_governor(*governor); | |
257 | if (ret) { | |
258 | pr_err("Failed to register governor: '%s'", | |
259 | (*governor)->name); | |
260 | break; | |
261 | } | |
7e8ee1e9 | 262 | |
57c5b2ec DL |
263 | pr_info("Registered thermal governor '%s'", |
264 | (*governor)->name); | |
265 | } | |
7e8ee1e9 | 266 | |
57c5b2ec DL |
267 | if (ret) { |
268 | struct thermal_governor **gov; | |
7e8ee1e9 | 269 | |
57c5b2ec DL |
270 | for_each_governor_table(gov) { |
271 | if (gov == governor) | |
272 | break; | |
273 | thermal_unregister_governor(*gov); | |
274 | } | |
275 | } | |
7e8ee1e9 | 276 | |
57c5b2ec | 277 | return ret; |
7e8ee1e9 D |
278 | } |
279 | ||
8772e185 EV |
280 | /* |
281 | * Zone update section: main control loop applied to each zone while monitoring | |
282 | * | |
283 | * in polling mode. The monitoring is done using a workqueue. | |
284 | * Same update may be done on a zone by calling thermal_zone_device_update(). | |
285 | * | |
286 | * An update means: | |
287 | * - Non-critical trips will invoke the governor responsible for that zone; | |
288 | * - Hot trips will produce a notification to userspace; | |
289 | * - Critical trip point will cause a system shutdown. | |
290 | */ | |
0c01ebbf D |
291 | static void thermal_zone_device_set_polling(struct thermal_zone_device *tz, |
292 | int delay) | |
293 | { | |
294 | if (delay > 1000) | |
c2b59d27 JG |
295 | mod_delayed_work(system_freezable_power_efficient_wq, |
296 | &tz->poll_queue, | |
0c01ebbf D |
297 | round_jiffies(msecs_to_jiffies(delay))); |
298 | else if (delay) | |
c2b59d27 JG |
299 | mod_delayed_work(system_freezable_power_efficient_wq, |
300 | &tz->poll_queue, | |
0c01ebbf D |
301 | msecs_to_jiffies(delay)); |
302 | else | |
163b00cd | 303 | cancel_delayed_work(&tz->poll_queue); |
0c01ebbf D |
304 | } |
305 | ||
b56bdff7 AP |
306 | static inline bool should_stop_polling(struct thermal_zone_device *tz) |
307 | { | |
308 | return !thermal_zone_device_is_enabled(tz); | |
309 | } | |
310 | ||
0c01ebbf D |
311 | static void monitor_thermal_zone(struct thermal_zone_device *tz) |
312 | { | |
b56bdff7 AP |
313 | bool stop; |
314 | ||
315 | stop = should_stop_polling(tz); | |
316 | ||
0c01ebbf D |
317 | mutex_lock(&tz->lock); |
318 | ||
b56bdff7 | 319 | if (!stop && tz->passive) |
0c01ebbf | 320 | thermal_zone_device_set_polling(tz, tz->passive_delay); |
b56bdff7 | 321 | else if (!stop && tz->polling_delay) |
0c01ebbf D |
322 | thermal_zone_device_set_polling(tz, tz->polling_delay); |
323 | else | |
324 | thermal_zone_device_set_polling(tz, 0); | |
325 | ||
326 | mutex_unlock(&tz->lock); | |
327 | } | |
328 | ||
5be52fcc | 329 | static void handle_non_critical_trips(struct thermal_zone_device *tz, int trip) |
0c01ebbf | 330 | { |
f2234bcd ZR |
331 | tz->governor ? tz->governor->throttle(tz, trip) : |
332 | def_governor->throttle(tz, trip); | |
0c01ebbf D |
333 | } |
334 | ||
ef1d87e0 K |
335 | /** |
336 | * thermal_emergency_poweroff_func - emergency poweroff work after a known delay | |
337 | * @work: work_struct associated with the emergency poweroff function | |
338 | * | |
339 | * This function is called in very critical situations to force | |
340 | * a kernel poweroff after a configurable timeout value. | |
341 | */ | |
342 | static void thermal_emergency_poweroff_func(struct work_struct *work) | |
343 | { | |
344 | /* | |
345 | * We have reached here after the emergency thermal shutdown | |
346 | * Waiting period has expired. This means orderly_poweroff has | |
347 | * not been able to shut off the system for some reason. | |
348 | * Try to shut down the system immediately using kernel_power_off | |
349 | * if populated | |
350 | */ | |
351 | WARN(1, "Attempting kernel_power_off: Temperature too high\n"); | |
352 | kernel_power_off(); | |
353 | ||
354 | /* | |
355 | * Worst of the worst case trigger emergency restart | |
356 | */ | |
357 | WARN(1, "Attempting emergency_restart: Temperature too high\n"); | |
358 | emergency_restart(); | |
359 | } | |
360 | ||
361 | static DECLARE_DELAYED_WORK(thermal_emergency_poweroff_work, | |
362 | thermal_emergency_poweroff_func); | |
363 | ||
364 | /** | |
365 | * thermal_emergency_poweroff - Trigger an emergency system poweroff | |
366 | * | |
367 | * This may be called from any critical situation to trigger a system shutdown | |
368 | * after a known period of time. By default this is not scheduled. | |
369 | */ | |
c4b379d0 | 370 | static void thermal_emergency_poweroff(void) |
ef1d87e0 K |
371 | { |
372 | int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS; | |
373 | /* | |
374 | * poweroff_delay_ms must be a carefully profiled positive value. | |
375 | * Its a must for thermal_emergency_poweroff_work to be scheduled | |
376 | */ | |
377 | if (poweroff_delay_ms <= 0) | |
378 | return; | |
379 | schedule_delayed_work(&thermal_emergency_poweroff_work, | |
380 | msecs_to_jiffies(poweroff_delay_ms)); | |
381 | } | |
382 | ||
0c01ebbf | 383 | static void handle_critical_trips(struct thermal_zone_device *tz, |
eb7be329 | 384 | int trip, enum thermal_trip_type trip_type) |
0c01ebbf | 385 | { |
17e8351a | 386 | int trip_temp; |
0c01ebbf D |
387 | |
388 | tz->ops->get_trip_temp(tz, trip, &trip_temp); | |
389 | ||
390 | /* If we have not crossed the trip_temp, we do not care. */ | |
84ffe3ec | 391 | if (trip_temp <= 0 || tz->temperature < trip_temp) |
0c01ebbf D |
392 | return; |
393 | ||
208cd822 PA |
394 | trace_thermal_zone_trip(tz, trip, trip_type); |
395 | ||
0c01ebbf D |
396 | if (tz->ops->notify) |
397 | tz->ops->notify(tz, trip, trip_type); | |
398 | ||
399 | if (trip_type == THERMAL_TRIP_CRITICAL) { | |
923e0b1e | 400 | dev_emerg(&tz->device, |
039f6cf5 | 401 | "critical temperature reached (%d C), shutting down\n", |
923e0b1e | 402 | tz->temperature / 1000); |
e441fd68 K |
403 | mutex_lock(&poweroff_lock); |
404 | if (!power_off_triggered) { | |
ef1d87e0 K |
405 | /* |
406 | * Queue a backup emergency shutdown in the event of | |
407 | * orderly_poweroff failure | |
408 | */ | |
409 | thermal_emergency_poweroff(); | |
e441fd68 K |
410 | orderly_poweroff(true); |
411 | power_off_triggered = true; | |
412 | } | |
413 | mutex_unlock(&poweroff_lock); | |
0c01ebbf D |
414 | } |
415 | } | |
416 | ||
417 | static void handle_thermal_trip(struct thermal_zone_device *tz, int trip) | |
418 | { | |
419 | enum thermal_trip_type type; | |
55cdf0a2 | 420 | int trip_temp, hyst = 0; |
0c01ebbf | 421 | |
81ad4276 ZR |
422 | /* Ignore disabled trip points */ |
423 | if (test_bit(trip, &tz->trips_disabled)) | |
424 | return; | |
425 | ||
55cdf0a2 | 426 | tz->ops->get_trip_temp(tz, trip, &trip_temp); |
0c01ebbf | 427 | tz->ops->get_trip_type(tz, trip, &type); |
55cdf0a2 DL |
428 | if (tz->ops->get_trip_hyst) |
429 | tz->ops->get_trip_hyst(tz, trip, &hyst); | |
430 | ||
431 | if (tz->last_temperature != THERMAL_TEMP_INVALID) { | |
432 | if (tz->last_temperature < trip_temp && | |
433 | tz->temperature >= trip_temp) | |
434 | thermal_notify_tz_trip_up(tz->id, trip); | |
435 | if (tz->last_temperature >= trip_temp && | |
436 | tz->temperature < (trip_temp - hyst)) | |
437 | thermal_notify_tz_trip_down(tz->id, trip); | |
438 | } | |
0c01ebbf D |
439 | |
440 | if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT) | |
441 | handle_critical_trips(tz, trip, type); | |
442 | else | |
5be52fcc | 443 | handle_non_critical_trips(tz, trip); |
0c01ebbf D |
444 | /* |
445 | * Alright, we handled this trip successfully. | |
446 | * So, start monitoring again. | |
447 | */ | |
448 | monitor_thermal_zone(tz); | |
449 | } | |
450 | ||
451 | static void update_temperature(struct thermal_zone_device *tz) | |
452 | { | |
17e8351a | 453 | int temp, ret; |
0c01ebbf | 454 | |
e6e238c3 | 455 | ret = thermal_zone_get_temp(tz, &temp); |
0c01ebbf | 456 | if (ret) { |
7e497a73 HG |
457 | if (ret != -EAGAIN) |
458 | dev_warn(&tz->device, | |
459 | "failed to read out thermal zone (%d)\n", | |
460 | ret); | |
e6e238c3 | 461 | return; |
0c01ebbf D |
462 | } |
463 | ||
e6e238c3 | 464 | mutex_lock(&tz->lock); |
0c01ebbf D |
465 | tz->last_temperature = tz->temperature; |
466 | tz->temperature = temp; | |
0c01ebbf | 467 | mutex_unlock(&tz->lock); |
06475b55 | 468 | |
100a8fdb | 469 | trace_thermal_temperature(tz); |
55cdf0a2 DL |
470 | |
471 | thermal_genl_sampling_temp(tz->id, temp); | |
bb431ba2 ZR |
472 | } |
473 | ||
964f4843 | 474 | static void thermal_zone_device_init(struct thermal_zone_device *tz) |
bb431ba2 ZR |
475 | { |
476 | struct thermal_instance *pos; | |
bb431ba2 | 477 | tz->temperature = THERMAL_TEMP_INVALID; |
bb431ba2 ZR |
478 | list_for_each_entry(pos, &tz->thermal_instances, tz_node) |
479 | pos->initialized = false; | |
0c01ebbf D |
480 | } |
481 | ||
964f4843 WW |
482 | static void thermal_zone_device_reset(struct thermal_zone_device *tz) |
483 | { | |
484 | tz->passive = 0; | |
485 | thermal_zone_device_init(tz); | |
486 | } | |
487 | ||
ac5d9ecc AP |
488 | static int thermal_zone_device_set_mode(struct thermal_zone_device *tz, |
489 | enum thermal_device_mode mode) | |
490 | { | |
491 | int ret = 0; | |
492 | ||
493 | mutex_lock(&tz->lock); | |
494 | ||
495 | /* do nothing if mode isn't changing */ | |
496 | if (mode == tz->mode) { | |
497 | mutex_unlock(&tz->lock); | |
498 | ||
499 | return ret; | |
500 | } | |
501 | ||
f5e50bf4 AP |
502 | if (tz->ops->change_mode) |
503 | ret = tz->ops->change_mode(tz, mode); | |
ac5d9ecc AP |
504 | |
505 | if (!ret) | |
506 | tz->mode = mode; | |
507 | ||
508 | mutex_unlock(&tz->lock); | |
509 | ||
510 | thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); | |
511 | ||
25be77e5 DL |
512 | if (mode == THERMAL_DEVICE_ENABLED) |
513 | thermal_notify_tz_enable(tz->id); | |
514 | else | |
515 | thermal_notify_tz_disable(tz->id); | |
516 | ||
ac5d9ecc AP |
517 | return ret; |
518 | } | |
519 | ||
520 | int thermal_zone_device_enable(struct thermal_zone_device *tz) | |
521 | { | |
522 | return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED); | |
523 | } | |
524 | EXPORT_SYMBOL_GPL(thermal_zone_device_enable); | |
525 | ||
526 | int thermal_zone_device_disable(struct thermal_zone_device *tz) | |
527 | { | |
528 | return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED); | |
529 | } | |
530 | EXPORT_SYMBOL_GPL(thermal_zone_device_disable); | |
531 | ||
532 | int thermal_zone_device_is_enabled(struct thermal_zone_device *tz) | |
533 | { | |
534 | enum thermal_device_mode mode; | |
535 | ||
536 | mutex_lock(&tz->lock); | |
537 | ||
538 | mode = tz->mode; | |
539 | ||
540 | mutex_unlock(&tz->lock); | |
541 | ||
542 | return mode == THERMAL_DEVICE_ENABLED; | |
543 | } | |
ac5d9ecc | 544 | |
0e70f466 SP |
545 | void thermal_zone_device_update(struct thermal_zone_device *tz, |
546 | enum thermal_notify_event event) | |
0c01ebbf D |
547 | { |
548 | int count; | |
549 | ||
b56bdff7 AP |
550 | if (should_stop_polling(tz)) |
551 | return; | |
552 | ||
ff140fea ZR |
553 | if (atomic_read(&in_suspend)) |
554 | return; | |
555 | ||
433178e7 DL |
556 | if (WARN_ONCE(!tz->ops->get_temp, "'%s' must not be called without " |
557 | "'get_temp' ops set\n", __func__)) | |
81bd4e1c EV |
558 | return; |
559 | ||
0c01ebbf D |
560 | update_temperature(tz); |
561 | ||
060c034a SH |
562 | thermal_zone_set_trips(tz); |
563 | ||
0e70f466 SP |
564 | tz->notify_event = event; |
565 | ||
0c01ebbf D |
566 | for (count = 0; count < tz->trips; count++) |
567 | handle_thermal_trip(tz, count); | |
568 | } | |
910cb1e3 | 569 | EXPORT_SYMBOL_GPL(thermal_zone_device_update); |
0c01ebbf | 570 | |
106339ab EV |
571 | /** |
572 | * thermal_notify_framework - Sensor drivers use this API to notify framework | |
573 | * @tz: thermal zone device | |
574 | * @trip: indicates which trip point has been crossed | |
575 | * | |
576 | * This function handles the trip events from sensor drivers. It starts | |
577 | * throttling the cooling devices according to the policy configured. | |
578 | * For CRITICAL and HOT trip points, this notifies the respective drivers, | |
579 | * and does actual throttling for other trip points i.e ACTIVE and PASSIVE. | |
580 | * The throttling policy is based on the configured platform data; if no | |
581 | * platform data is provided, this uses the step_wise throttling policy. | |
582 | */ | |
583 | void thermal_notify_framework(struct thermal_zone_device *tz, int trip) | |
584 | { | |
585 | handle_thermal_trip(tz, trip); | |
586 | } | |
587 | EXPORT_SYMBOL_GPL(thermal_notify_framework); | |
588 | ||
0c01ebbf D |
589 | static void thermal_zone_device_check(struct work_struct *work) |
590 | { | |
591 | struct thermal_zone_device *tz = container_of(work, struct | |
592 | thermal_zone_device, | |
593 | poll_queue.work); | |
0e70f466 | 594 | thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); |
0c01ebbf D |
595 | } |
596 | ||
3d0055d2 EV |
597 | void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz, |
598 | const char *cdev_type, size_t size) | |
203d3d4a | 599 | { |
3d0055d2 | 600 | struct thermal_cooling_device *cdev = NULL; |
203d3d4a | 601 | |
3d0055d2 EV |
602 | mutex_lock(&thermal_list_lock); |
603 | list_for_each_entry(cdev, &thermal_cdev_list, node) { | |
604 | /* skip non matching cdevs */ | |
605 | if (strncmp(cdev_type, cdev->type, size)) | |
606 | continue; | |
607 | ||
608 | /* re binding the exception matching the type pattern */ | |
609 | thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev, | |
610 | THERMAL_NO_LIMIT, | |
611 | THERMAL_NO_LIMIT, | |
612 | THERMAL_WEIGHT_DEFAULT); | |
613 | } | |
614 | mutex_unlock(&thermal_list_lock); | |
203d3d4a ZR |
615 | } |
616 | ||
3d44a509 DL |
617 | int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), |
618 | void *data) | |
619 | { | |
620 | struct thermal_governor *gov; | |
621 | int ret = 0; | |
622 | ||
623 | mutex_lock(&thermal_governor_lock); | |
624 | list_for_each_entry(gov, &thermal_governor_list, governor_list) { | |
625 | ret = cb(gov, data); | |
626 | if (ret) | |
627 | break; | |
628 | } | |
629 | mutex_unlock(&thermal_governor_lock); | |
630 | ||
631 | return ret; | |
632 | } | |
633 | ||
634 | int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *, | |
635 | void *), void *data) | |
636 | { | |
637 | struct thermal_cooling_device *cdev; | |
638 | int ret = 0; | |
639 | ||
640 | mutex_lock(&thermal_list_lock); | |
641 | list_for_each_entry(cdev, &thermal_cdev_list, node) { | |
642 | ret = cb(cdev, data); | |
643 | if (ret) | |
644 | break; | |
645 | } | |
646 | mutex_unlock(&thermal_list_lock); | |
647 | ||
648 | return ret; | |
649 | } | |
650 | ||
651 | int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *), | |
652 | void *data) | |
653 | { | |
654 | struct thermal_zone_device *tz; | |
655 | int ret = 0; | |
656 | ||
657 | mutex_lock(&thermal_list_lock); | |
658 | list_for_each_entry(tz, &thermal_tz_list, node) { | |
659 | ret = cb(tz, data); | |
660 | if (ret) | |
661 | break; | |
662 | } | |
663 | mutex_unlock(&thermal_list_lock); | |
664 | ||
665 | return ret; | |
666 | } | |
667 | ||
329b064f DL |
668 | struct thermal_zone_device *thermal_zone_get_by_id(int id) |
669 | { | |
82aa68af | 670 | struct thermal_zone_device *tz, *match = NULL; |
329b064f DL |
671 | |
672 | mutex_lock(&thermal_list_lock); | |
673 | list_for_each_entry(tz, &thermal_tz_list, node) { | |
82aa68af TR |
674 | if (tz->id == id) { |
675 | match = tz; | |
329b064f | 676 | break; |
82aa68af | 677 | } |
329b064f DL |
678 | } |
679 | mutex_unlock(&thermal_list_lock); | |
680 | ||
82aa68af | 681 | return match; |
329b064f DL |
682 | } |
683 | ||
3d0055d2 EV |
684 | void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz, |
685 | const char *cdev_type, size_t size) | |
203d3d4a | 686 | { |
3d0055d2 | 687 | struct thermal_cooling_device *cdev = NULL; |
203d3d4a | 688 | |
3d0055d2 EV |
689 | mutex_lock(&thermal_list_lock); |
690 | list_for_each_entry(cdev, &thermal_cdev_list, node) { | |
691 | /* skip non matching cdevs */ | |
692 | if (strncmp(cdev_type, cdev->type, size)) | |
693 | continue; | |
694 | /* unbinding the exception matching the type pattern */ | |
695 | thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE, | |
696 | cdev); | |
697 | } | |
698 | mutex_unlock(&thermal_list_lock); | |
203d3d4a ZR |
699 | } |
700 | ||
81193e2e EV |
701 | /* |
702 | * Device management section: cooling devices, zones devices, and binding | |
703 | * | |
704 | * Set of functions provided by the thermal core for: | |
705 | * - cooling devices lifecycle: registration, unregistration, | |
706 | * binding, and unbinding. | |
707 | * - thermal zone devices lifecycle: registration, unregistration, | |
708 | * binding, and unbinding. | |
709 | */ | |
203d3d4a ZR |
710 | |
711 | /** | |
d2e4eb83 EV |
712 | * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone |
713 | * @tz: pointer to struct thermal_zone_device | |
203d3d4a ZR |
714 | * @trip: indicates which trip point the cooling devices is |
715 | * associated with in this thermal zone. | |
d2e4eb83 EV |
716 | * @cdev: pointer to struct thermal_cooling_device |
717 | * @upper: the Maximum cooling state for this trip point. | |
718 | * THERMAL_NO_LIMIT means no upper limit, | |
719 | * and the cooling device can be in max_state. | |
720 | * @lower: the Minimum cooling state can be used for this trip point. | |
721 | * THERMAL_NO_LIMIT means no lower limit, | |
722 | * and the cooling device can be in cooling state 0. | |
6cd9e9f6 KS |
723 | * @weight: The weight of the cooling device to be bound to the |
724 | * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the | |
725 | * default value | |
543a9561 | 726 | * |
d2e4eb83 EV |
727 | * This interface function bind a thermal cooling device to the certain trip |
728 | * point of a thermal zone device. | |
543a9561 | 729 | * This function is usually called in the thermal zone device .bind callback. |
d2e4eb83 EV |
730 | * |
731 | * Return: 0 on success, the proper error value otherwise. | |
203d3d4a ZR |
732 | */ |
733 | int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, | |
734 | int trip, | |
9d99842f | 735 | struct thermal_cooling_device *cdev, |
6cd9e9f6 KS |
736 | unsigned long upper, unsigned long lower, |
737 | unsigned int weight) | |
203d3d4a | 738 | { |
b81b6ba3 ZR |
739 | struct thermal_instance *dev; |
740 | struct thermal_instance *pos; | |
c7516709 TS |
741 | struct thermal_zone_device *pos1; |
742 | struct thermal_cooling_device *pos2; | |
74051ba5 | 743 | unsigned long max_state; |
9a3031dc | 744 | int result, ret; |
203d3d4a | 745 | |
543a9561 | 746 | if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE)) |
203d3d4a ZR |
747 | return -EINVAL; |
748 | ||
c7516709 TS |
749 | list_for_each_entry(pos1, &thermal_tz_list, node) { |
750 | if (pos1 == tz) | |
751 | break; | |
752 | } | |
753 | list_for_each_entry(pos2, &thermal_cdev_list, node) { | |
754 | if (pos2 == cdev) | |
755 | break; | |
756 | } | |
757 | ||
758 | if (tz != pos1 || cdev != pos2) | |
203d3d4a ZR |
759 | return -EINVAL; |
760 | ||
9a3031dc LM |
761 | ret = cdev->ops->get_max_state(cdev, &max_state); |
762 | if (ret) | |
763 | return ret; | |
9d99842f ZR |
764 | |
765 | /* lower default 0, upper default max_state */ | |
766 | lower = lower == THERMAL_NO_LIMIT ? 0 : lower; | |
767 | upper = upper == THERMAL_NO_LIMIT ? max_state : upper; | |
768 | ||
769 | if (lower > upper || upper > max_state) | |
770 | return -EINVAL; | |
771 | ||
95e3ed15 | 772 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
203d3d4a ZR |
773 | if (!dev) |
774 | return -ENOMEM; | |
775 | dev->tz = tz; | |
776 | dev->cdev = cdev; | |
777 | dev->trip = trip; | |
9d99842f ZR |
778 | dev->upper = upper; |
779 | dev->lower = lower; | |
ce119f83 | 780 | dev->target = THERMAL_NO_TARGET; |
6cd9e9f6 | 781 | dev->weight = weight; |
74051ba5 | 782 | |
b31ef828 MW |
783 | result = ida_simple_get(&tz->ida, 0, 0, GFP_KERNEL); |
784 | if (result < 0) | |
203d3d4a ZR |
785 | goto free_mem; |
786 | ||
b31ef828 | 787 | dev->id = result; |
203d3d4a ZR |
788 | sprintf(dev->name, "cdev%d", dev->id); |
789 | result = | |
790 | sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name); | |
791 | if (result) | |
b31ef828 | 792 | goto release_ida; |
203d3d4a ZR |
793 | |
794 | sprintf(dev->attr_name, "cdev%d_trip_point", dev->id); | |
975f8c56 | 795 | sysfs_attr_init(&dev->attr.attr); |
203d3d4a ZR |
796 | dev->attr.attr.name = dev->attr_name; |
797 | dev->attr.attr.mode = 0444; | |
33e678d4 | 798 | dev->attr.show = trip_point_show; |
203d3d4a ZR |
799 | result = device_create_file(&tz->device, &dev->attr); |
800 | if (result) | |
801 | goto remove_symbol_link; | |
802 | ||
db916513 JM |
803 | sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id); |
804 | sysfs_attr_init(&dev->weight_attr.attr); | |
805 | dev->weight_attr.attr.name = dev->weight_attr_name; | |
806 | dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO; | |
33e678d4 VK |
807 | dev->weight_attr.show = weight_show; |
808 | dev->weight_attr.store = weight_store; | |
db916513 JM |
809 | result = device_create_file(&tz->device, &dev->weight_attr); |
810 | if (result) | |
811 | goto remove_trip_file; | |
812 | ||
203d3d4a | 813 | mutex_lock(&tz->lock); |
f4a821ce | 814 | mutex_lock(&cdev->lock); |
cddf31b3 | 815 | list_for_each_entry(pos, &tz->thermal_instances, tz_node) |
b659a30d EV |
816 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
817 | result = -EEXIST; | |
818 | break; | |
819 | } | |
b5e4ae62 | 820 | if (!result) { |
cddf31b3 | 821 | list_add_tail(&dev->tz_node, &tz->thermal_instances); |
b5e4ae62 | 822 | list_add_tail(&dev->cdev_node, &cdev->thermal_instances); |
4511f716 | 823 | atomic_set(&tz->need_update, 1); |
b5e4ae62 | 824 | } |
f4a821ce | 825 | mutex_unlock(&cdev->lock); |
203d3d4a ZR |
826 | mutex_unlock(&tz->lock); |
827 | ||
828 | if (!result) | |
829 | return 0; | |
830 | ||
db916513 JM |
831 | device_remove_file(&tz->device, &dev->weight_attr); |
832 | remove_trip_file: | |
203d3d4a | 833 | device_remove_file(&tz->device, &dev->attr); |
caca8b80 | 834 | remove_symbol_link: |
203d3d4a | 835 | sysfs_remove_link(&tz->device.kobj, dev->name); |
b31ef828 MW |
836 | release_ida: |
837 | ida_simple_remove(&tz->ida, dev->id); | |
caca8b80 | 838 | free_mem: |
203d3d4a ZR |
839 | kfree(dev); |
840 | return result; | |
841 | } | |
910cb1e3 | 842 | EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device); |
203d3d4a ZR |
843 | |
844 | /** | |
9892e5dc EV |
845 | * thermal_zone_unbind_cooling_device() - unbind a cooling device from a |
846 | * thermal zone. | |
847 | * @tz: pointer to a struct thermal_zone_device. | |
203d3d4a ZR |
848 | * @trip: indicates which trip point the cooling devices is |
849 | * associated with in this thermal zone. | |
9892e5dc | 850 | * @cdev: pointer to a struct thermal_cooling_device. |
543a9561 | 851 | * |
9892e5dc EV |
852 | * This interface function unbind a thermal cooling device from the certain |
853 | * trip point of a thermal zone device. | |
543a9561 | 854 | * This function is usually called in the thermal zone device .unbind callback. |
9892e5dc EV |
855 | * |
856 | * Return: 0 on success, the proper error value otherwise. | |
203d3d4a ZR |
857 | */ |
858 | int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz, | |
859 | int trip, | |
860 | struct thermal_cooling_device *cdev) | |
861 | { | |
b81b6ba3 | 862 | struct thermal_instance *pos, *next; |
203d3d4a ZR |
863 | |
864 | mutex_lock(&tz->lock); | |
f4a821ce | 865 | mutex_lock(&cdev->lock); |
cddf31b3 | 866 | list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) { |
543a9561 | 867 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
cddf31b3 | 868 | list_del(&pos->tz_node); |
b5e4ae62 | 869 | list_del(&pos->cdev_node); |
f4a821ce | 870 | mutex_unlock(&cdev->lock); |
203d3d4a ZR |
871 | mutex_unlock(&tz->lock); |
872 | goto unbind; | |
873 | } | |
874 | } | |
f4a821ce | 875 | mutex_unlock(&cdev->lock); |
203d3d4a ZR |
876 | mutex_unlock(&tz->lock); |
877 | ||
878 | return -ENODEV; | |
879 | ||
caca8b80 | 880 | unbind: |
528464ea | 881 | device_remove_file(&tz->device, &pos->weight_attr); |
203d3d4a ZR |
882 | device_remove_file(&tz->device, &pos->attr); |
883 | sysfs_remove_link(&tz->device.kobj, pos->name); | |
b31ef828 | 884 | ida_simple_remove(&tz->ida, pos->id); |
203d3d4a ZR |
885 | kfree(pos); |
886 | return 0; | |
887 | } | |
910cb1e3 | 888 | EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device); |
203d3d4a ZR |
889 | |
890 | static void thermal_release(struct device *dev) | |
891 | { | |
892 | struct thermal_zone_device *tz; | |
893 | struct thermal_cooling_device *cdev; | |
894 | ||
caca8b80 JP |
895 | if (!strncmp(dev_name(dev), "thermal_zone", |
896 | sizeof("thermal_zone") - 1)) { | |
203d3d4a | 897 | tz = to_thermal_zone(dev); |
6a6cd25b | 898 | thermal_zone_destroy_device_groups(tz); |
203d3d4a | 899 | kfree(tz); |
b659a30d EV |
900 | } else if (!strncmp(dev_name(dev), "cooling_device", |
901 | sizeof("cooling_device") - 1)) { | |
203d3d4a ZR |
902 | cdev = to_cooling_device(dev); |
903 | kfree(cdev); | |
904 | } | |
905 | } | |
906 | ||
907 | static struct class thermal_class = { | |
908 | .name = "thermal", | |
909 | .dev_release = thermal_release, | |
910 | }; | |
911 | ||
4b0d3c2d EV |
912 | static inline |
913 | void print_bind_err_msg(struct thermal_zone_device *tz, | |
914 | struct thermal_cooling_device *cdev, int ret) | |
f502ab84 EV |
915 | { |
916 | dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n", | |
917 | tz->type, cdev->type, ret); | |
918 | } | |
919 | ||
920 | static void __bind(struct thermal_zone_device *tz, int mask, | |
921 | struct thermal_cooling_device *cdev, | |
922 | unsigned long *limits, | |
923 | unsigned int weight) | |
924 | { | |
925 | int i, ret; | |
926 | ||
927 | for (i = 0; i < tz->trips; i++) { | |
928 | if (mask & (1 << i)) { | |
929 | unsigned long upper, lower; | |
930 | ||
931 | upper = THERMAL_NO_LIMIT; | |
932 | lower = THERMAL_NO_LIMIT; | |
933 | if (limits) { | |
934 | lower = limits[i * 2]; | |
935 | upper = limits[i * 2 + 1]; | |
936 | } | |
937 | ret = thermal_zone_bind_cooling_device(tz, i, cdev, | |
938 | upper, lower, | |
939 | weight); | |
940 | if (ret) | |
941 | print_bind_err_msg(tz, cdev, ret); | |
942 | } | |
943 | } | |
944 | } | |
945 | ||
949aad83 EV |
946 | static void bind_cdev(struct thermal_cooling_device *cdev) |
947 | { | |
948 | int i, ret; | |
949 | const struct thermal_zone_params *tzp; | |
950 | struct thermal_zone_device *pos = NULL; | |
951 | ||
952 | mutex_lock(&thermal_list_lock); | |
953 | ||
954 | list_for_each_entry(pos, &thermal_tz_list, node) { | |
955 | if (!pos->tzp && !pos->ops->bind) | |
956 | continue; | |
957 | ||
958 | if (pos->ops->bind) { | |
959 | ret = pos->ops->bind(pos, cdev); | |
960 | if (ret) | |
961 | print_bind_err_msg(pos, cdev, ret); | |
962 | continue; | |
963 | } | |
964 | ||
965 | tzp = pos->tzp; | |
966 | if (!tzp || !tzp->tbp) | |
967 | continue; | |
968 | ||
969 | for (i = 0; i < tzp->num_tbps; i++) { | |
970 | if (tzp->tbp[i].cdev || !tzp->tbp[i].match) | |
971 | continue; | |
972 | if (tzp->tbp[i].match(pos, cdev)) | |
973 | continue; | |
974 | tzp->tbp[i].cdev = cdev; | |
975 | __bind(pos, tzp->tbp[i].trip_mask, cdev, | |
976 | tzp->tbp[i].binding_limits, | |
977 | tzp->tbp[i].weight); | |
978 | } | |
979 | } | |
980 | ||
981 | mutex_unlock(&thermal_list_lock); | |
982 | } | |
983 | ||
203d3d4a | 984 | /** |
a116b5d4 EV |
985 | * __thermal_cooling_device_register() - register a new thermal cooling device |
986 | * @np: a pointer to a device tree node. | |
203d3d4a ZR |
987 | * @type: the thermal cooling device type. |
988 | * @devdata: device private data. | |
989 | * @ops: standard thermal cooling devices callbacks. | |
3a6eccb3 EV |
990 | * |
991 | * This interface function adds a new thermal cooling device (fan/processor/...) | |
992 | * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself | |
993 | * to all the thermal zone devices registered at the same time. | |
a116b5d4 EV |
994 | * It also gives the opportunity to link the cooling device to a device tree |
995 | * node, so that it can be bound to a thermal zone created out of device tree. | |
3a6eccb3 EV |
996 | * |
997 | * Return: a pointer to the created struct thermal_cooling_device or an | |
998 | * ERR_PTR. Caller must check return value with IS_ERR*() helpers. | |
203d3d4a | 999 | */ |
a116b5d4 EV |
1000 | static struct thermal_cooling_device * |
1001 | __thermal_cooling_device_register(struct device_node *np, | |
f991de53 | 1002 | const char *type, void *devdata, |
a116b5d4 | 1003 | const struct thermal_cooling_device_ops *ops) |
203d3d4a ZR |
1004 | { |
1005 | struct thermal_cooling_device *cdev; | |
4511f716 | 1006 | struct thermal_zone_device *pos = NULL; |
203d3d4a ZR |
1007 | int result; |
1008 | ||
204dd1d3 | 1009 | if (type && strlen(type) >= THERMAL_NAME_LENGTH) |
3e6fda5c | 1010 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
1011 | |
1012 | if (!ops || !ops->get_max_state || !ops->get_cur_state || | |
543a9561 | 1013 | !ops->set_cur_state) |
3e6fda5c | 1014 | return ERR_PTR(-EINVAL); |
203d3d4a | 1015 | |
95e3ed15 | 1016 | cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); |
203d3d4a | 1017 | if (!cdev) |
3e6fda5c | 1018 | return ERR_PTR(-ENOMEM); |
203d3d4a | 1019 | |
b31ef828 MW |
1020 | result = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL); |
1021 | if (result < 0) { | |
203d3d4a | 1022 | kfree(cdev); |
3e6fda5c | 1023 | return ERR_PTR(result); |
203d3d4a ZR |
1024 | } |
1025 | ||
b31ef828 | 1026 | cdev->id = result; |
c7a8b9d9 | 1027 | strlcpy(cdev->type, type ? : "", sizeof(cdev->type)); |
f4a821ce | 1028 | mutex_init(&cdev->lock); |
b5e4ae62 | 1029 | INIT_LIST_HEAD(&cdev->thermal_instances); |
a116b5d4 | 1030 | cdev->np = np; |
203d3d4a | 1031 | cdev->ops = ops; |
5ca0cce5 | 1032 | cdev->updated = false; |
203d3d4a ZR |
1033 | cdev->device.class = &thermal_class; |
1034 | cdev->devdata = devdata; | |
8ea22951 | 1035 | thermal_cooling_device_setup_sysfs(cdev); |
354655ea | 1036 | dev_set_name(&cdev->device, "cooling_device%d", cdev->id); |
203d3d4a ZR |
1037 | result = device_register(&cdev->device); |
1038 | if (result) { | |
b31ef828 | 1039 | ida_simple_remove(&thermal_cdev_ida, cdev->id); |
adc8749b | 1040 | put_device(&cdev->device); |
3e6fda5c | 1041 | return ERR_PTR(result); |
203d3d4a ZR |
1042 | } |
1043 | ||
7e8ee1e9 | 1044 | /* Add 'this' new cdev to the global cdev list */ |
203d3d4a ZR |
1045 | mutex_lock(&thermal_list_lock); |
1046 | list_add(&cdev->node, &thermal_cdev_list); | |
203d3d4a ZR |
1047 | mutex_unlock(&thermal_list_lock); |
1048 | ||
7e8ee1e9 D |
1049 | /* Update binding information for 'this' new cdev */ |
1050 | bind_cdev(cdev); | |
1051 | ||
4511f716 CY |
1052 | mutex_lock(&thermal_list_lock); |
1053 | list_for_each_entry(pos, &thermal_tz_list, node) | |
1054 | if (atomic_cmpxchg(&pos->need_update, 1, 0)) | |
0e70f466 SP |
1055 | thermal_zone_device_update(pos, |
1056 | THERMAL_EVENT_UNSPECIFIED); | |
4511f716 CY |
1057 | mutex_unlock(&thermal_list_lock); |
1058 | ||
7e8ee1e9 | 1059 | return cdev; |
203d3d4a | 1060 | } |
a116b5d4 EV |
1061 | |
1062 | /** | |
1063 | * thermal_cooling_device_register() - register a new thermal cooling device | |
1064 | * @type: the thermal cooling device type. | |
1065 | * @devdata: device private data. | |
1066 | * @ops: standard thermal cooling devices callbacks. | |
1067 | * | |
1068 | * This interface function adds a new thermal cooling device (fan/processor/...) | |
1069 | * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself | |
1070 | * to all the thermal zone devices registered at the same time. | |
1071 | * | |
1072 | * Return: a pointer to the created struct thermal_cooling_device or an | |
1073 | * ERR_PTR. Caller must check return value with IS_ERR*() helpers. | |
1074 | */ | |
1075 | struct thermal_cooling_device * | |
f991de53 | 1076 | thermal_cooling_device_register(const char *type, void *devdata, |
a116b5d4 EV |
1077 | const struct thermal_cooling_device_ops *ops) |
1078 | { | |
1079 | return __thermal_cooling_device_register(NULL, type, devdata, ops); | |
1080 | } | |
910cb1e3 | 1081 | EXPORT_SYMBOL_GPL(thermal_cooling_device_register); |
203d3d4a | 1082 | |
a116b5d4 EV |
1083 | /** |
1084 | * thermal_of_cooling_device_register() - register an OF thermal cooling device | |
1085 | * @np: a pointer to a device tree node. | |
1086 | * @type: the thermal cooling device type. | |
1087 | * @devdata: device private data. | |
1088 | * @ops: standard thermal cooling devices callbacks. | |
1089 | * | |
1090 | * This function will register a cooling device with device tree node reference. | |
1091 | * This interface function adds a new thermal cooling device (fan/processor/...) | |
1092 | * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself | |
1093 | * to all the thermal zone devices registered at the same time. | |
1094 | * | |
1095 | * Return: a pointer to the created struct thermal_cooling_device or an | |
1096 | * ERR_PTR. Caller must check return value with IS_ERR*() helpers. | |
1097 | */ | |
1098 | struct thermal_cooling_device * | |
1099 | thermal_of_cooling_device_register(struct device_node *np, | |
f991de53 | 1100 | const char *type, void *devdata, |
a116b5d4 EV |
1101 | const struct thermal_cooling_device_ops *ops) |
1102 | { | |
1103 | return __thermal_cooling_device_register(np, type, devdata, ops); | |
1104 | } | |
1105 | EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register); | |
1106 | ||
b4ab114c GR |
1107 | static void thermal_cooling_device_release(struct device *dev, void *res) |
1108 | { | |
1109 | thermal_cooling_device_unregister( | |
1110 | *(struct thermal_cooling_device **)res); | |
1111 | } | |
1112 | ||
1113 | /** | |
1114 | * devm_thermal_of_cooling_device_register() - register an OF thermal cooling | |
1115 | * device | |
1116 | * @dev: a valid struct device pointer of a sensor device. | |
1117 | * @np: a pointer to a device tree node. | |
1118 | * @type: the thermal cooling device type. | |
1119 | * @devdata: device private data. | |
1120 | * @ops: standard thermal cooling devices callbacks. | |
1121 | * | |
1122 | * This function will register a cooling device with device tree node reference. | |
1123 | * This interface function adds a new thermal cooling device (fan/processor/...) | |
1124 | * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself | |
1125 | * to all the thermal zone devices registered at the same time. | |
1126 | * | |
1127 | * Return: a pointer to the created struct thermal_cooling_device or an | |
1128 | * ERR_PTR. Caller must check return value with IS_ERR*() helpers. | |
1129 | */ | |
1130 | struct thermal_cooling_device * | |
1131 | devm_thermal_of_cooling_device_register(struct device *dev, | |
1132 | struct device_node *np, | |
1133 | char *type, void *devdata, | |
1134 | const struct thermal_cooling_device_ops *ops) | |
1135 | { | |
1136 | struct thermal_cooling_device **ptr, *tcd; | |
1137 | ||
1138 | ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr), | |
1139 | GFP_KERNEL); | |
1140 | if (!ptr) | |
1141 | return ERR_PTR(-ENOMEM); | |
1142 | ||
1143 | tcd = __thermal_cooling_device_register(np, type, devdata, ops); | |
1144 | if (IS_ERR(tcd)) { | |
1145 | devres_free(ptr); | |
1146 | return tcd; | |
1147 | } | |
1148 | ||
1149 | *ptr = tcd; | |
1150 | devres_add(dev, ptr); | |
1151 | ||
1152 | return tcd; | |
1153 | } | |
1154 | EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register); | |
1155 | ||
f11997fa EV |
1156 | static void __unbind(struct thermal_zone_device *tz, int mask, |
1157 | struct thermal_cooling_device *cdev) | |
1158 | { | |
1159 | int i; | |
1160 | ||
1161 | for (i = 0; i < tz->trips; i++) | |
1162 | if (mask & (1 << i)) | |
1163 | thermal_zone_unbind_cooling_device(tz, i, cdev); | |
1164 | } | |
1165 | ||
203d3d4a | 1166 | /** |
38e7b549 | 1167 | * thermal_cooling_device_unregister - removes a thermal cooling device |
203d3d4a ZR |
1168 | * @cdev: the thermal cooling device to remove. |
1169 | * | |
38e7b549 EV |
1170 | * thermal_cooling_device_unregister() must be called when a registered |
1171 | * thermal cooling device is no longer needed. | |
203d3d4a | 1172 | */ |
7e8ee1e9 | 1173 | void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev) |
203d3d4a | 1174 | { |
7e8ee1e9 D |
1175 | int i; |
1176 | const struct thermal_zone_params *tzp; | |
203d3d4a ZR |
1177 | struct thermal_zone_device *tz; |
1178 | struct thermal_cooling_device *pos = NULL; | |
1179 | ||
1180 | if (!cdev) | |
1181 | return; | |
1182 | ||
1183 | mutex_lock(&thermal_list_lock); | |
1184 | list_for_each_entry(pos, &thermal_cdev_list, node) | |
b659a30d EV |
1185 | if (pos == cdev) |
1186 | break; | |
203d3d4a ZR |
1187 | if (pos != cdev) { |
1188 | /* thermal cooling device not found */ | |
1189 | mutex_unlock(&thermal_list_lock); | |
1190 | return; | |
1191 | } | |
1192 | list_del(&cdev->node); | |
7e8ee1e9 D |
1193 | |
1194 | /* Unbind all thermal zones associated with 'this' cdev */ | |
203d3d4a | 1195 | list_for_each_entry(tz, &thermal_tz_list, node) { |
7e8ee1e9 D |
1196 | if (tz->ops->unbind) { |
1197 | tz->ops->unbind(tz, cdev); | |
1198 | continue; | |
1199 | } | |
1200 | ||
1201 | if (!tz->tzp || !tz->tzp->tbp) | |
203d3d4a | 1202 | continue; |
7e8ee1e9 D |
1203 | |
1204 | tzp = tz->tzp; | |
1205 | for (i = 0; i < tzp->num_tbps; i++) { | |
1206 | if (tzp->tbp[i].cdev == cdev) { | |
1207 | __unbind(tz, tzp->tbp[i].trip_mask, cdev); | |
1208 | tzp->tbp[i].cdev = NULL; | |
1209 | } | |
1210 | } | |
203d3d4a | 1211 | } |
7e8ee1e9 | 1212 | |
203d3d4a | 1213 | mutex_unlock(&thermal_list_lock); |
7e8ee1e9 | 1214 | |
b31ef828 | 1215 | ida_simple_remove(&thermal_cdev_ida, cdev->id); |
3c587768 | 1216 | device_del(&cdev->device); |
8ea22951 | 1217 | thermal_cooling_device_destroy_sysfs(cdev); |
3c587768 | 1218 | put_device(&cdev->device); |
203d3d4a | 1219 | } |
910cb1e3 | 1220 | EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister); |
203d3d4a | 1221 | |
90f5b5bb | 1222 | static void bind_tz(struct thermal_zone_device *tz) |
ce119f83 | 1223 | { |
90f5b5bb EV |
1224 | int i, ret; |
1225 | struct thermal_cooling_device *pos = NULL; | |
1226 | const struct thermal_zone_params *tzp = tz->tzp; | |
ce119f83 | 1227 | |
90f5b5bb | 1228 | if (!tzp && !tz->ops->bind) |
ce119f83 | 1229 | return; |
c56f5c03 | 1230 | |
90f5b5bb | 1231 | mutex_lock(&thermal_list_lock); |
c56f5c03 | 1232 | |
90f5b5bb EV |
1233 | /* If there is ops->bind, try to use ops->bind */ |
1234 | if (tz->ops->bind) { | |
1235 | list_for_each_entry(pos, &thermal_cdev_list, node) { | |
1236 | ret = tz->ops->bind(tz, pos); | |
1237 | if (ret) | |
1238 | print_bind_err_msg(tz, pos, ret); | |
27365a6c | 1239 | } |
90f5b5bb | 1240 | goto exit; |
27365a6c | 1241 | } |
c56f5c03 | 1242 | |
90f5b5bb EV |
1243 | if (!tzp || !tzp->tbp) |
1244 | goto exit; | |
27365a6c | 1245 | |
90f5b5bb EV |
1246 | list_for_each_entry(pos, &thermal_cdev_list, node) { |
1247 | for (i = 0; i < tzp->num_tbps; i++) { | |
1248 | if (tzp->tbp[i].cdev || !tzp->tbp[i].match) | |
1249 | continue; | |
1250 | if (tzp->tbp[i].match(tz, pos)) | |
1251 | continue; | |
1252 | tzp->tbp[i].cdev = pos; | |
1253 | __bind(tz, tzp->tbp[i].trip_mask, pos, | |
1254 | tzp->tbp[i].binding_limits, | |
1255 | tzp->tbp[i].weight); | |
27365a6c | 1256 | } |
c56f5c03 | 1257 | } |
90f5b5bb EV |
1258 | exit: |
1259 | mutex_unlock(&thermal_list_lock); | |
c56f5c03 D |
1260 | } |
1261 | ||
203d3d4a | 1262 | /** |
a00e55f9 | 1263 | * thermal_zone_device_register() - register a new thermal zone device |
203d3d4a ZR |
1264 | * @type: the thermal zone device type |
1265 | * @trips: the number of trip points the thermal zone support | |
c56f5c03 | 1266 | * @mask: a bit string indicating the writeablility of trip points |
203d3d4a ZR |
1267 | * @devdata: private device data |
1268 | * @ops: standard thermal zone device callbacks | |
50125a9b | 1269 | * @tzp: thermal zone platform parameters |
b1569e99 MG |
1270 | * @passive_delay: number of milliseconds to wait between polls when |
1271 | * performing passive cooling | |
1272 | * @polling_delay: number of milliseconds to wait between polls when checking | |
1273 | * whether trip points have been crossed (0 for interrupt | |
1274 | * driven systems) | |
203d3d4a | 1275 | * |
a00e55f9 EV |
1276 | * This interface function adds a new thermal zone device (sensor) to |
1277 | * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the | |
1278 | * thermal cooling devices registered at the same time. | |
203d3d4a | 1279 | * thermal_zone_device_unregister() must be called when the device is no |
1b7ddb84 | 1280 | * longer needed. The passive cooling depends on the .get_trend() return value. |
a00e55f9 EV |
1281 | * |
1282 | * Return: a pointer to the created struct thermal_zone_device or an | |
1283 | * in case of error, an ERR_PTR. Caller must check return value with | |
1284 | * IS_ERR*() helpers. | |
203d3d4a | 1285 | */ |
eb7be329 EV |
1286 | struct thermal_zone_device * |
1287 | thermal_zone_device_register(const char *type, int trips, int mask, | |
1288 | void *devdata, struct thermal_zone_device_ops *ops, | |
1289 | struct thermal_zone_params *tzp, int passive_delay, | |
1290 | int polling_delay) | |
203d3d4a ZR |
1291 | { |
1292 | struct thermal_zone_device *tz; | |
03a971a2 | 1293 | enum thermal_trip_type trip_type; |
81ad4276 | 1294 | int trip_temp; |
adc8749b | 1295 | int id; |
203d3d4a ZR |
1296 | int result; |
1297 | int count; | |
e33df1d2 | 1298 | struct thermal_governor *governor; |
203d3d4a | 1299 | |
67eed44b AK |
1300 | if (!type || strlen(type) == 0) { |
1301 | pr_err("Error: No thermal zone type defined\n"); | |
54fa38cc | 1302 | return ERR_PTR(-EINVAL); |
67eed44b | 1303 | } |
54fa38cc | 1304 | |
67eed44b AK |
1305 | if (type && strlen(type) >= THERMAL_NAME_LENGTH) { |
1306 | pr_err("Error: Thermal zone name (%s) too long, should be under %d chars\n", | |
1307 | type, THERMAL_NAME_LENGTH); | |
3e6fda5c | 1308 | return ERR_PTR(-EINVAL); |
67eed44b | 1309 | } |
203d3d4a | 1310 | |
67eed44b AK |
1311 | if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips) { |
1312 | pr_err("Error: Incorrect number of thermal trips\n"); | |
3e6fda5c | 1313 | return ERR_PTR(-EINVAL); |
67eed44b | 1314 | } |
203d3d4a | 1315 | |
67eed44b AK |
1316 | if (!ops) { |
1317 | pr_err("Error: Thermal zone device ops not defined\n"); | |
3e6fda5c | 1318 | return ERR_PTR(-EINVAL); |
67eed44b | 1319 | } |
203d3d4a | 1320 | |
83720d0b | 1321 | if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp)) |
6b2aa51d EV |
1322 | return ERR_PTR(-EINVAL); |
1323 | ||
95e3ed15 | 1324 | tz = kzalloc(sizeof(*tz), GFP_KERNEL); |
203d3d4a | 1325 | if (!tz) |
3e6fda5c | 1326 | return ERR_PTR(-ENOMEM); |
203d3d4a | 1327 | |
2d374139 | 1328 | INIT_LIST_HEAD(&tz->thermal_instances); |
b31ef828 | 1329 | ida_init(&tz->ida); |
203d3d4a | 1330 | mutex_init(&tz->lock); |
adc8749b YH |
1331 | id = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL); |
1332 | if (id < 0) { | |
1333 | result = id; | |
9d9ca1f9 | 1334 | goto free_tz; |
adc8749b | 1335 | } |
203d3d4a | 1336 | |
adc8749b | 1337 | tz->id = id; |
54fa38cc | 1338 | strlcpy(tz->type, type, sizeof(tz->type)); |
203d3d4a | 1339 | tz->ops = ops; |
50125a9b | 1340 | tz->tzp = tzp; |
203d3d4a ZR |
1341 | tz->device.class = &thermal_class; |
1342 | tz->devdata = devdata; | |
1343 | tz->trips = trips; | |
b1569e99 MG |
1344 | tz->passive_delay = passive_delay; |
1345 | tz->polling_delay = polling_delay; | |
1c600861 | 1346 | |
4d0fe749 | 1347 | /* sys I/F */ |
1c600861 | 1348 | /* Add nodes that are always present via .groups */ |
4d0fe749 EV |
1349 | result = thermal_zone_create_device_groups(tz, mask); |
1350 | if (result) | |
9d9ca1f9 | 1351 | goto remove_id; |
4d0fe749 | 1352 | |
4511f716 CY |
1353 | /* A new thermal zone needs to be updated anyway. */ |
1354 | atomic_set(&tz->need_update, 1); | |
b1569e99 | 1355 | |
354655ea | 1356 | dev_set_name(&tz->device, "thermal_zone%d", tz->id); |
203d3d4a | 1357 | result = device_register(&tz->device); |
9d9ca1f9 | 1358 | if (result) |
adc8749b | 1359 | goto release_device; |
203d3d4a | 1360 | |
203d3d4a | 1361 | for (count = 0; count < trips; count++) { |
37b2539e BZ |
1362 | if (tz->ops->get_trip_type(tz, count, &trip_type) || |
1363 | tz->ops->get_trip_temp(tz, count, &trip_temp) || | |
1364 | !trip_temp) | |
81ad4276 | 1365 | set_bit(count, &tz->trips_disabled); |
203d3d4a ZR |
1366 | } |
1367 | ||
a4a15485 D |
1368 | /* Update 'this' zone's governor information */ |
1369 | mutex_lock(&thermal_governor_lock); | |
1370 | ||
1371 | if (tz->tzp) | |
e33df1d2 | 1372 | governor = __find_governor(tz->tzp->governor_name); |
a4a15485 | 1373 | else |
e33df1d2 JM |
1374 | governor = def_governor; |
1375 | ||
1376 | result = thermal_set_governor(tz, governor); | |
1377 | if (result) { | |
1378 | mutex_unlock(&thermal_governor_lock); | |
1379 | goto unregister; | |
1380 | } | |
a4a15485 D |
1381 | |
1382 | mutex_unlock(&thermal_governor_lock); | |
1383 | ||
ccba4ffd EV |
1384 | if (!tz->tzp || !tz->tzp->no_hwmon) { |
1385 | result = thermal_add_hwmon_sysfs(tz); | |
1386 | if (result) | |
1387 | goto unregister; | |
1388 | } | |
e68b16ab | 1389 | |
203d3d4a ZR |
1390 | mutex_lock(&thermal_list_lock); |
1391 | list_add_tail(&tz->node, &thermal_tz_list); | |
203d3d4a ZR |
1392 | mutex_unlock(&thermal_list_lock); |
1393 | ||
7e8ee1e9 D |
1394 | /* Bind cooling devices for this zone */ |
1395 | bind_tz(tz); | |
1396 | ||
b659a30d | 1397 | INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check); |
b1569e99 | 1398 | |
bb431ba2 | 1399 | thermal_zone_device_reset(tz); |
4511f716 CY |
1400 | /* Update the new thermal zone and mark it as already updated. */ |
1401 | if (atomic_cmpxchg(&tz->need_update, 1, 0)) | |
0e70f466 | 1402 | thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); |
b1569e99 | 1403 | |
55cdf0a2 DL |
1404 | thermal_notify_tz_create(tz->id, tz->type); |
1405 | ||
14015860 | 1406 | return tz; |
203d3d4a | 1407 | |
caca8b80 | 1408 | unregister: |
adc8749b YH |
1409 | device_del(&tz->device); |
1410 | release_device: | |
1411 | put_device(&tz->device); | |
1412 | tz = NULL; | |
9d9ca1f9 | 1413 | remove_id: |
adc8749b | 1414 | ida_simple_remove(&thermal_tz_ida, id); |
9d9ca1f9 CJ |
1415 | free_tz: |
1416 | kfree(tz); | |
1417 | return ERR_PTR(result); | |
203d3d4a | 1418 | } |
910cb1e3 | 1419 | EXPORT_SYMBOL_GPL(thermal_zone_device_register); |
203d3d4a ZR |
1420 | |
1421 | /** | |
1422 | * thermal_device_unregister - removes the registered thermal zone device | |
203d3d4a ZR |
1423 | * @tz: the thermal zone device to remove |
1424 | */ | |
1425 | void thermal_zone_device_unregister(struct thermal_zone_device *tz) | |
1426 | { | |
a5f785ce | 1427 | int i, tz_id; |
7e8ee1e9 | 1428 | const struct thermal_zone_params *tzp; |
203d3d4a ZR |
1429 | struct thermal_cooling_device *cdev; |
1430 | struct thermal_zone_device *pos = NULL; | |
203d3d4a ZR |
1431 | |
1432 | if (!tz) | |
1433 | return; | |
1434 | ||
7e8ee1e9 | 1435 | tzp = tz->tzp; |
a5f785ce | 1436 | tz_id = tz->id; |
7e8ee1e9 | 1437 | |
203d3d4a ZR |
1438 | mutex_lock(&thermal_list_lock); |
1439 | list_for_each_entry(pos, &thermal_tz_list, node) | |
b659a30d EV |
1440 | if (pos == tz) |
1441 | break; | |
203d3d4a ZR |
1442 | if (pos != tz) { |
1443 | /* thermal zone device not found */ | |
1444 | mutex_unlock(&thermal_list_lock); | |
1445 | return; | |
1446 | } | |
1447 | list_del(&tz->node); | |
7e8ee1e9 D |
1448 | |
1449 | /* Unbind all cdevs associated with 'this' thermal zone */ | |
1450 | list_for_each_entry(cdev, &thermal_cdev_list, node) { | |
1451 | if (tz->ops->unbind) { | |
1452 | tz->ops->unbind(tz, cdev); | |
1453 | continue; | |
1454 | } | |
1455 | ||
1456 | if (!tzp || !tzp->tbp) | |
1457 | break; | |
1458 | ||
1459 | for (i = 0; i < tzp->num_tbps; i++) { | |
1460 | if (tzp->tbp[i].cdev == cdev) { | |
1461 | __unbind(tz, tzp->tbp[i].trip_mask, cdev); | |
1462 | tzp->tbp[i].cdev = NULL; | |
1463 | } | |
1464 | } | |
1465 | } | |
1466 | ||
203d3d4a ZR |
1467 | mutex_unlock(&thermal_list_lock); |
1468 | ||
163b00cd | 1469 | cancel_delayed_work_sync(&tz->poll_queue); |
b1569e99 | 1470 | |
e33df1d2 | 1471 | thermal_set_governor(tz, NULL); |
203d3d4a | 1472 | |
e68b16ab | 1473 | thermal_remove_hwmon_sysfs(tz); |
b31ef828 MW |
1474 | ida_simple_remove(&thermal_tz_ida, tz->id); |
1475 | ida_destroy(&tz->ida); | |
203d3d4a ZR |
1476 | mutex_destroy(&tz->lock); |
1477 | device_unregister(&tz->device); | |
55cdf0a2 | 1478 | |
a5f785ce | 1479 | thermal_notify_tz_delete(tz_id); |
203d3d4a | 1480 | } |
910cb1e3 | 1481 | EXPORT_SYMBOL_GPL(thermal_zone_device_unregister); |
203d3d4a | 1482 | |
63c4d919 EV |
1483 | /** |
1484 | * thermal_zone_get_zone_by_name() - search for a zone and returns its ref | |
1485 | * @name: thermal zone name to fetch the temperature | |
1486 | * | |
1487 | * When only one zone is found with the passed name, returns a reference to it. | |
1488 | * | |
1489 | * Return: On success returns a reference to an unique thermal zone with | |
1490 | * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid | |
1491 | * paramenters, -ENODEV for not found and -EEXIST for multiple matches). | |
1492 | */ | |
1493 | struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name) | |
1494 | { | |
1495 | struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL); | |
1496 | unsigned int found = 0; | |
1497 | ||
1498 | if (!name) | |
1499 | goto exit; | |
1500 | ||
1501 | mutex_lock(&thermal_list_lock); | |
1502 | list_for_each_entry(pos, &thermal_tz_list, node) | |
484ac2f3 | 1503 | if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) { |
63c4d919 EV |
1504 | found++; |
1505 | ref = pos; | |
1506 | } | |
1507 | mutex_unlock(&thermal_list_lock); | |
1508 | ||
1509 | /* nothing has been found, thus an error code for it */ | |
1510 | if (found == 0) | |
1511 | ref = ERR_PTR(-ENODEV); | |
1512 | else if (found > 1) | |
1513 | /* Success only when an unique zone is found */ | |
1514 | ref = ERR_PTR(-EEXIST); | |
1515 | ||
1516 | exit: | |
1517 | return ref; | |
1518 | } | |
1519 | EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name); | |
1520 | ||
ff140fea | 1521 | static int thermal_pm_notify(struct notifier_block *nb, |
eb7be329 | 1522 | unsigned long mode, void *_unused) |
ff140fea ZR |
1523 | { |
1524 | struct thermal_zone_device *tz; | |
1525 | ||
1526 | switch (mode) { | |
1527 | case PM_HIBERNATION_PREPARE: | |
1528 | case PM_RESTORE_PREPARE: | |
1529 | case PM_SUSPEND_PREPARE: | |
1530 | atomic_set(&in_suspend, 1); | |
1531 | break; | |
1532 | case PM_POST_HIBERNATION: | |
1533 | case PM_POST_RESTORE: | |
1534 | case PM_POST_SUSPEND: | |
1535 | atomic_set(&in_suspend, 0); | |
1536 | list_for_each_entry(tz, &thermal_tz_list, node) { | |
7f4957be | 1537 | if (!thermal_zone_device_is_enabled(tz)) |
ff54bbd1 WW |
1538 | continue; |
1539 | ||
964f4843 | 1540 | thermal_zone_device_init(tz); |
0e70f466 SP |
1541 | thermal_zone_device_update(tz, |
1542 | THERMAL_EVENT_UNSPECIFIED); | |
ff140fea ZR |
1543 | } |
1544 | break; | |
1545 | default: | |
1546 | break; | |
1547 | } | |
1548 | return 0; | |
1549 | } | |
1550 | ||
1551 | static struct notifier_block thermal_pm_nb = { | |
1552 | .notifier_call = thermal_pm_notify, | |
1553 | }; | |
1554 | ||
203d3d4a ZR |
1555 | static int __init thermal_init(void) |
1556 | { | |
80a26a5c ZR |
1557 | int result; |
1558 | ||
d2a89b52 DL |
1559 | result = thermal_netlink_init(); |
1560 | if (result) | |
1561 | goto error; | |
1562 | ||
80a26a5c ZR |
1563 | result = thermal_register_governors(); |
1564 | if (result) | |
1565 | goto error; | |
203d3d4a ZR |
1566 | |
1567 | result = class_register(&thermal_class); | |
80a26a5c ZR |
1568 | if (result) |
1569 | goto unregister_governors; | |
1570 | ||
4e5e4705 EV |
1571 | result = of_parse_thermal_zones(); |
1572 | if (result) | |
f96c8e50 | 1573 | goto unregister_class; |
4e5e4705 | 1574 | |
ff140fea ZR |
1575 | result = register_pm_notifier(&thermal_pm_nb); |
1576 | if (result) | |
1577 | pr_warn("Thermal: Can not register suspend notifier, return %d\n", | |
1578 | result); | |
1579 | ||
80a26a5c ZR |
1580 | return 0; |
1581 | ||
80a26a5c ZR |
1582 | unregister_class: |
1583 | class_unregister(&thermal_class); | |
9d367e5e LH |
1584 | unregister_governors: |
1585 | thermal_unregister_governors(); | |
80a26a5c | 1586 | error: |
b31ef828 MW |
1587 | ida_destroy(&thermal_tz_ida); |
1588 | ida_destroy(&thermal_cdev_ida); | |
80a26a5c ZR |
1589 | mutex_destroy(&thermal_list_lock); |
1590 | mutex_destroy(&thermal_governor_lock); | |
e441fd68 | 1591 | mutex_destroy(&poweroff_lock); |
203d3d4a ZR |
1592 | return result; |
1593 | } | |
3f5a2cbe | 1594 | postcore_initcall(thermal_init); |