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