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 | 24 | #define CREATE_TRACE_POINTS |
32a7a021 | 25 | #include "thermal_trace.h" |
100a8fdb | 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 */ |
fb2c1024 | 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 | 355 | |
fb2c1024 RW |
356 | if (trip.temperature == THERMAL_TEMP_INVALID) |
357 | return; | |
358 | ||
55cdf0a2 | 359 | if (tz->last_temperature != THERMAL_TEMP_INVALID) { |
7c3d5c20 DL |
360 | if (tz->last_temperature < trip.temperature && |
361 | tz->temperature >= trip.temperature) | |
362 | thermal_notify_tz_trip_up(tz->id, trip_id, | |
fc656fa1 | 363 | tz->temperature); |
7c3d5c20 DL |
364 | if (tz->last_temperature >= trip.temperature && |
365 | tz->temperature < (trip.temperature - trip.hysteresis)) | |
366 | thermal_notify_tz_trip_down(tz->id, trip_id, | |
fc656fa1 | 367 | tz->temperature); |
55cdf0a2 | 368 | } |
0c01ebbf | 369 | |
7c3d5c20 DL |
370 | if (trip.type == THERMAL_TRIP_CRITICAL || trip.type == THERMAL_TRIP_HOT) |
371 | handle_critical_trips(tz, trip_id, trip.temperature, trip.type); | |
0c01ebbf | 372 | else |
7c3d5c20 | 373 | handle_non_critical_trips(tz, trip_id); |
0c01ebbf D |
374 | } |
375 | ||
376 | static void update_temperature(struct thermal_zone_device *tz) | |
377 | { | |
17e8351a | 378 | int temp, ret; |
0c01ebbf | 379 | |
a930da9b | 380 | ret = __thermal_zone_get_temp(tz, &temp); |
0c01ebbf | 381 | if (ret) { |
7e497a73 HG |
382 | if (ret != -EAGAIN) |
383 | dev_warn(&tz->device, | |
384 | "failed to read out thermal zone (%d)\n", | |
385 | ret); | |
e6e238c3 | 386 | return; |
0c01ebbf D |
387 | } |
388 | ||
389 | tz->last_temperature = tz->temperature; | |
390 | tz->temperature = temp; | |
06475b55 | 391 | |
100a8fdb | 392 | trace_thermal_temperature(tz); |
55cdf0a2 DL |
393 | |
394 | thermal_genl_sampling_temp(tz->id, temp); | |
bb431ba2 ZR |
395 | } |
396 | ||
964f4843 | 397 | static void thermal_zone_device_init(struct thermal_zone_device *tz) |
bb431ba2 ZR |
398 | { |
399 | struct thermal_instance *pos; | |
bb431ba2 | 400 | tz->temperature = THERMAL_TEMP_INVALID; |
99b63316 MMP |
401 | tz->prev_low_trip = -INT_MAX; |
402 | tz->prev_high_trip = INT_MAX; | |
bb431ba2 ZR |
403 | list_for_each_entry(pos, &tz->thermal_instances, tz_node) |
404 | pos->initialized = false; | |
0c01ebbf D |
405 | } |
406 | ||
05eeee2b GR |
407 | void __thermal_zone_device_update(struct thermal_zone_device *tz, |
408 | enum thermal_notify_event event) | |
1c439dec GR |
409 | { |
410 | int count; | |
411 | ||
412 | if (atomic_read(&in_suspend)) | |
413 | return; | |
414 | ||
415 | if (WARN_ONCE(!tz->ops->get_temp, | |
416 | "'%s' must not be called without 'get_temp' ops set\n", | |
417 | __func__)) | |
418 | return; | |
419 | ||
420 | if (!thermal_zone_device_is_enabled(tz)) | |
421 | return; | |
422 | ||
423 | update_temperature(tz); | |
424 | ||
425 | __thermal_zone_set_trips(tz); | |
426 | ||
427 | tz->notify_event = event; | |
428 | ||
429 | for (count = 0; count < tz->num_trips; count++) | |
430 | handle_thermal_trip(tz, count); | |
431 | ||
432 | monitor_thermal_zone(tz); | |
433 | } | |
434 | ||
ac5d9ecc AP |
435 | static int thermal_zone_device_set_mode(struct thermal_zone_device *tz, |
436 | enum thermal_device_mode mode) | |
437 | { | |
438 | int ret = 0; | |
439 | ||
440 | mutex_lock(&tz->lock); | |
441 | ||
442 | /* do nothing if mode isn't changing */ | |
443 | if (mode == tz->mode) { | |
444 | mutex_unlock(&tz->lock); | |
445 | ||
446 | return ret; | |
447 | } | |
448 | ||
b778b4d7 GR |
449 | if (!device_is_registered(&tz->device)) { |
450 | mutex_unlock(&tz->lock); | |
451 | ||
452 | return -ENODEV; | |
453 | } | |
454 | ||
f5e50bf4 AP |
455 | if (tz->ops->change_mode) |
456 | ret = tz->ops->change_mode(tz, mode); | |
ac5d9ecc AP |
457 | |
458 | if (!ret) | |
459 | tz->mode = mode; | |
460 | ||
1c439dec | 461 | __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); |
ac5d9ecc | 462 | |
1c439dec | 463 | mutex_unlock(&tz->lock); |
ac5d9ecc | 464 | |
25be77e5 DL |
465 | if (mode == THERMAL_DEVICE_ENABLED) |
466 | thermal_notify_tz_enable(tz->id); | |
467 | else | |
468 | thermal_notify_tz_disable(tz->id); | |
469 | ||
ac5d9ecc AP |
470 | return ret; |
471 | } | |
472 | ||
473 | int thermal_zone_device_enable(struct thermal_zone_device *tz) | |
474 | { | |
475 | return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED); | |
476 | } | |
477 | EXPORT_SYMBOL_GPL(thermal_zone_device_enable); | |
478 | ||
479 | int thermal_zone_device_disable(struct thermal_zone_device *tz) | |
480 | { | |
481 | return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED); | |
482 | } | |
483 | EXPORT_SYMBOL_GPL(thermal_zone_device_disable); | |
484 | ||
485 | int thermal_zone_device_is_enabled(struct thermal_zone_device *tz) | |
486 | { | |
a930da9b | 487 | lockdep_assert_held(&tz->lock); |
ac5d9ecc | 488 | |
a930da9b | 489 | return tz->mode == THERMAL_DEVICE_ENABLED; |
ac5d9ecc | 490 | } |
ac5d9ecc | 491 | |
0e70f466 SP |
492 | void thermal_zone_device_update(struct thermal_zone_device *tz, |
493 | enum thermal_notify_event event) | |
0c01ebbf | 494 | { |
a930da9b | 495 | mutex_lock(&tz->lock); |
b778b4d7 GR |
496 | if (device_is_registered(&tz->device)) |
497 | __thermal_zone_device_update(tz, event); | |
a930da9b | 498 | mutex_unlock(&tz->lock); |
0c01ebbf | 499 | } |
910cb1e3 | 500 | EXPORT_SYMBOL_GPL(thermal_zone_device_update); |
0c01ebbf | 501 | |
9a99a996 RW |
502 | /** |
503 | * thermal_zone_device_exec - Run a callback under the zone lock. | |
504 | * @tz: Thermal zone. | |
505 | * @cb: Callback to run. | |
506 | * @data: Data to pass to the callback. | |
507 | */ | |
508 | void thermal_zone_device_exec(struct thermal_zone_device *tz, | |
509 | void (*cb)(struct thermal_zone_device *, | |
510 | unsigned long), | |
511 | unsigned long data) | |
512 | { | |
513 | mutex_lock(&tz->lock); | |
514 | ||
515 | cb(tz, data); | |
516 | ||
517 | mutex_unlock(&tz->lock); | |
518 | } | |
519 | EXPORT_SYMBOL_GPL(thermal_zone_device_exec); | |
520 | ||
0c01ebbf D |
521 | static void thermal_zone_device_check(struct work_struct *work) |
522 | { | |
523 | struct thermal_zone_device *tz = container_of(work, struct | |
524 | thermal_zone_device, | |
525 | poll_queue.work); | |
0e70f466 | 526 | thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); |
0c01ebbf D |
527 | } |
528 | ||
3d44a509 DL |
529 | int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), |
530 | void *data) | |
531 | { | |
532 | struct thermal_governor *gov; | |
533 | int ret = 0; | |
534 | ||
535 | mutex_lock(&thermal_governor_lock); | |
536 | list_for_each_entry(gov, &thermal_governor_list, governor_list) { | |
537 | ret = cb(gov, data); | |
538 | if (ret) | |
539 | break; | |
540 | } | |
541 | mutex_unlock(&thermal_governor_lock); | |
542 | ||
543 | return ret; | |
544 | } | |
545 | ||
546 | int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *, | |
547 | void *), void *data) | |
548 | { | |
549 | struct thermal_cooling_device *cdev; | |
550 | int ret = 0; | |
551 | ||
552 | mutex_lock(&thermal_list_lock); | |
553 | list_for_each_entry(cdev, &thermal_cdev_list, node) { | |
554 | ret = cb(cdev, data); | |
555 | if (ret) | |
556 | break; | |
557 | } | |
558 | mutex_unlock(&thermal_list_lock); | |
559 | ||
560 | return ret; | |
561 | } | |
562 | ||
563 | int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *), | |
564 | void *data) | |
565 | { | |
566 | struct thermal_zone_device *tz; | |
567 | int ret = 0; | |
568 | ||
569 | mutex_lock(&thermal_list_lock); | |
570 | list_for_each_entry(tz, &thermal_tz_list, node) { | |
571 | ret = cb(tz, data); | |
572 | if (ret) | |
573 | break; | |
574 | } | |
575 | mutex_unlock(&thermal_list_lock); | |
576 | ||
577 | return ret; | |
578 | } | |
579 | ||
329b064f DL |
580 | struct thermal_zone_device *thermal_zone_get_by_id(int id) |
581 | { | |
82aa68af | 582 | struct thermal_zone_device *tz, *match = NULL; |
329b064f DL |
583 | |
584 | mutex_lock(&thermal_list_lock); | |
585 | list_for_each_entry(tz, &thermal_tz_list, node) { | |
82aa68af TR |
586 | if (tz->id == id) { |
587 | match = tz; | |
329b064f | 588 | break; |
82aa68af | 589 | } |
329b064f DL |
590 | } |
591 | mutex_unlock(&thermal_list_lock); | |
592 | ||
82aa68af | 593 | return match; |
329b064f DL |
594 | } |
595 | ||
81193e2e EV |
596 | /* |
597 | * Device management section: cooling devices, zones devices, and binding | |
598 | * | |
599 | * Set of functions provided by the thermal core for: | |
600 | * - cooling devices lifecycle: registration, unregistration, | |
601 | * binding, and unbinding. | |
602 | * - thermal zone devices lifecycle: registration, unregistration, | |
603 | * binding, and unbinding. | |
604 | */ | |
203d3d4a ZR |
605 | |
606 | /** | |
d2e4eb83 EV |
607 | * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone |
608 | * @tz: pointer to struct thermal_zone_device | |
203d3d4a ZR |
609 | * @trip: indicates which trip point the cooling devices is |
610 | * associated with in this thermal zone. | |
d2e4eb83 EV |
611 | * @cdev: pointer to struct thermal_cooling_device |
612 | * @upper: the Maximum cooling state for this trip point. | |
613 | * THERMAL_NO_LIMIT means no upper limit, | |
614 | * and the cooling device can be in max_state. | |
615 | * @lower: the Minimum cooling state can be used for this trip point. | |
616 | * THERMAL_NO_LIMIT means no lower limit, | |
617 | * and the cooling device can be in cooling state 0. | |
6cd9e9f6 KS |
618 | * @weight: The weight of the cooling device to be bound to the |
619 | * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the | |
620 | * default value | |
543a9561 | 621 | * |
d2e4eb83 EV |
622 | * This interface function bind a thermal cooling device to the certain trip |
623 | * point of a thermal zone device. | |
543a9561 | 624 | * This function is usually called in the thermal zone device .bind callback. |
d2e4eb83 EV |
625 | * |
626 | * Return: 0 on success, the proper error value otherwise. | |
203d3d4a ZR |
627 | */ |
628 | int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, | |
629 | int trip, | |
9d99842f | 630 | struct thermal_cooling_device *cdev, |
6cd9e9f6 KS |
631 | unsigned long upper, unsigned long lower, |
632 | unsigned int weight) | |
203d3d4a | 633 | { |
b81b6ba3 ZR |
634 | struct thermal_instance *dev; |
635 | struct thermal_instance *pos; | |
c7516709 TS |
636 | struct thermal_zone_device *pos1; |
637 | struct thermal_cooling_device *pos2; | |
790930f4 | 638 | bool upper_no_limit; |
c408b3d1 | 639 | int result; |
203d3d4a | 640 | |
e5bfcd30 | 641 | if (trip >= tz->num_trips || trip < 0) |
203d3d4a ZR |
642 | return -EINVAL; |
643 | ||
c7516709 TS |
644 | list_for_each_entry(pos1, &thermal_tz_list, node) { |
645 | if (pos1 == tz) | |
646 | break; | |
647 | } | |
648 | list_for_each_entry(pos2, &thermal_cdev_list, node) { | |
649 | if (pos2 == cdev) | |
650 | break; | |
651 | } | |
652 | ||
653 | if (tz != pos1 || cdev != pos2) | |
203d3d4a ZR |
654 | return -EINVAL; |
655 | ||
9d99842f ZR |
656 | /* lower default 0, upper default max_state */ |
657 | lower = lower == THERMAL_NO_LIMIT ? 0 : lower; | |
790930f4 RW |
658 | |
659 | if (upper == THERMAL_NO_LIMIT) { | |
660 | upper = cdev->max_state; | |
661 | upper_no_limit = true; | |
662 | } else { | |
663 | upper_no_limit = false; | |
664 | } | |
9d99842f | 665 | |
c408b3d1 | 666 | if (lower > upper || upper > cdev->max_state) |
9d99842f ZR |
667 | return -EINVAL; |
668 | ||
95e3ed15 | 669 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
203d3d4a ZR |
670 | if (!dev) |
671 | return -ENOMEM; | |
672 | dev->tz = tz; | |
673 | dev->cdev = cdev; | |
674 | dev->trip = trip; | |
9d99842f | 675 | dev->upper = upper; |
790930f4 | 676 | dev->upper_no_limit = upper_no_limit; |
9d99842f | 677 | dev->lower = lower; |
ce119f83 | 678 | dev->target = THERMAL_NO_TARGET; |
6cd9e9f6 | 679 | dev->weight = weight; |
74051ba5 | 680 | |
5a5b7d8d | 681 | result = ida_alloc(&tz->ida, GFP_KERNEL); |
b31ef828 | 682 | if (result < 0) |
203d3d4a ZR |
683 | goto free_mem; |
684 | ||
b31ef828 | 685 | dev->id = result; |
203d3d4a ZR |
686 | sprintf(dev->name, "cdev%d", dev->id); |
687 | result = | |
688 | sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name); | |
689 | if (result) | |
b31ef828 | 690 | goto release_ida; |
203d3d4a ZR |
691 | |
692 | sprintf(dev->attr_name, "cdev%d_trip_point", dev->id); | |
975f8c56 | 693 | sysfs_attr_init(&dev->attr.attr); |
203d3d4a ZR |
694 | dev->attr.attr.name = dev->attr_name; |
695 | dev->attr.attr.mode = 0444; | |
33e678d4 | 696 | dev->attr.show = trip_point_show; |
203d3d4a ZR |
697 | result = device_create_file(&tz->device, &dev->attr); |
698 | if (result) | |
699 | goto remove_symbol_link; | |
700 | ||
db916513 JM |
701 | sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id); |
702 | sysfs_attr_init(&dev->weight_attr.attr); | |
703 | dev->weight_attr.attr.name = dev->weight_attr_name; | |
704 | dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO; | |
33e678d4 VK |
705 | dev->weight_attr.show = weight_show; |
706 | dev->weight_attr.store = weight_store; | |
db916513 JM |
707 | result = device_create_file(&tz->device, &dev->weight_attr); |
708 | if (result) | |
709 | goto remove_trip_file; | |
710 | ||
203d3d4a | 711 | mutex_lock(&tz->lock); |
f4a821ce | 712 | mutex_lock(&cdev->lock); |
cddf31b3 | 713 | list_for_each_entry(pos, &tz->thermal_instances, tz_node) |
b659a30d EV |
714 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
715 | result = -EEXIST; | |
716 | break; | |
717 | } | |
b5e4ae62 | 718 | if (!result) { |
cddf31b3 | 719 | list_add_tail(&dev->tz_node, &tz->thermal_instances); |
b5e4ae62 | 720 | list_add_tail(&dev->cdev_node, &cdev->thermal_instances); |
4511f716 | 721 | atomic_set(&tz->need_update, 1); |
b5e4ae62 | 722 | } |
f4a821ce | 723 | mutex_unlock(&cdev->lock); |
203d3d4a ZR |
724 | mutex_unlock(&tz->lock); |
725 | ||
726 | if (!result) | |
727 | return 0; | |
728 | ||
db916513 JM |
729 | device_remove_file(&tz->device, &dev->weight_attr); |
730 | remove_trip_file: | |
203d3d4a | 731 | device_remove_file(&tz->device, &dev->attr); |
caca8b80 | 732 | remove_symbol_link: |
203d3d4a | 733 | sysfs_remove_link(&tz->device.kobj, dev->name); |
b31ef828 | 734 | release_ida: |
5a5b7d8d | 735 | ida_free(&tz->ida, dev->id); |
caca8b80 | 736 | free_mem: |
203d3d4a ZR |
737 | kfree(dev); |
738 | return result; | |
739 | } | |
910cb1e3 | 740 | EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device); |
203d3d4a ZR |
741 | |
742 | /** | |
9892e5dc EV |
743 | * thermal_zone_unbind_cooling_device() - unbind a cooling device from a |
744 | * thermal zone. | |
745 | * @tz: pointer to a struct thermal_zone_device. | |
203d3d4a ZR |
746 | * @trip: indicates which trip point the cooling devices is |
747 | * associated with in this thermal zone. | |
9892e5dc | 748 | * @cdev: pointer to a struct thermal_cooling_device. |
543a9561 | 749 | * |
9892e5dc EV |
750 | * This interface function unbind a thermal cooling device from the certain |
751 | * trip point of a thermal zone device. | |
543a9561 | 752 | * This function is usually called in the thermal zone device .unbind callback. |
9892e5dc EV |
753 | * |
754 | * Return: 0 on success, the proper error value otherwise. | |
203d3d4a ZR |
755 | */ |
756 | int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz, | |
757 | int trip, | |
758 | struct thermal_cooling_device *cdev) | |
759 | { | |
b81b6ba3 | 760 | struct thermal_instance *pos, *next; |
203d3d4a ZR |
761 | |
762 | mutex_lock(&tz->lock); | |
f4a821ce | 763 | mutex_lock(&cdev->lock); |
cddf31b3 | 764 | list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) { |
543a9561 | 765 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
cddf31b3 | 766 | list_del(&pos->tz_node); |
b5e4ae62 | 767 | list_del(&pos->cdev_node); |
f4a821ce | 768 | mutex_unlock(&cdev->lock); |
203d3d4a ZR |
769 | mutex_unlock(&tz->lock); |
770 | goto unbind; | |
771 | } | |
772 | } | |
f4a821ce | 773 | mutex_unlock(&cdev->lock); |
203d3d4a ZR |
774 | mutex_unlock(&tz->lock); |
775 | ||
776 | return -ENODEV; | |
777 | ||
caca8b80 | 778 | unbind: |
528464ea | 779 | device_remove_file(&tz->device, &pos->weight_attr); |
203d3d4a ZR |
780 | device_remove_file(&tz->device, &pos->attr); |
781 | sysfs_remove_link(&tz->device.kobj, pos->name); | |
5a5b7d8d | 782 | ida_free(&tz->ida, pos->id); |
203d3d4a ZR |
783 | kfree(pos); |
784 | return 0; | |
785 | } | |
910cb1e3 | 786 | EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device); |
203d3d4a ZR |
787 | |
788 | static void thermal_release(struct device *dev) | |
789 | { | |
790 | struct thermal_zone_device *tz; | |
791 | struct thermal_cooling_device *cdev; | |
792 | ||
caca8b80 JP |
793 | if (!strncmp(dev_name(dev), "thermal_zone", |
794 | sizeof("thermal_zone") - 1)) { | |
203d3d4a | 795 | tz = to_thermal_zone(dev); |
6a6cd25b | 796 | thermal_zone_destroy_device_groups(tz); |
d35f29ed | 797 | mutex_destroy(&tz->lock); |
203d3d4a | 798 | kfree(tz); |
b659a30d EV |
799 | } else if (!strncmp(dev_name(dev), "cooling_device", |
800 | sizeof("cooling_device") - 1)) { | |
203d3d4a | 801 | cdev = to_cooling_device(dev); |
e398421f VK |
802 | thermal_cooling_device_destroy_sysfs(cdev); |
803 | kfree(cdev->type); | |
804 | ida_free(&thermal_cdev_ida, cdev->id); | |
203d3d4a ZR |
805 | kfree(cdev); |
806 | } | |
807 | } | |
808 | ||
9e0a9be2 | 809 | static struct class *thermal_class; |
203d3d4a | 810 | |
4b0d3c2d EV |
811 | static inline |
812 | void print_bind_err_msg(struct thermal_zone_device *tz, | |
813 | struct thermal_cooling_device *cdev, int ret) | |
f502ab84 EV |
814 | { |
815 | dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n", | |
816 | tz->type, cdev->type, ret); | |
817 | } | |
818 | ||
949aad83 EV |
819 | static void bind_cdev(struct thermal_cooling_device *cdev) |
820 | { | |
ded2d383 | 821 | int ret; |
949aad83 EV |
822 | struct thermal_zone_device *pos = NULL; |
823 | ||
949aad83 | 824 | list_for_each_entry(pos, &thermal_tz_list, node) { |
949aad83 EV |
825 | if (pos->ops->bind) { |
826 | ret = pos->ops->bind(pos, cdev); | |
827 | if (ret) | |
828 | print_bind_err_msg(pos, cdev, ret); | |
949aad83 EV |
829 | } |
830 | } | |
949aad83 EV |
831 | } |
832 | ||
203d3d4a | 833 | /** |
a116b5d4 EV |
834 | * __thermal_cooling_device_register() - register a new thermal cooling device |
835 | * @np: a pointer to a device tree node. | |
203d3d4a ZR |
836 | * @type: the thermal cooling device type. |
837 | * @devdata: device private data. | |
838 | * @ops: standard thermal cooling devices callbacks. | |
3a6eccb3 EV |
839 | * |
840 | * This interface function adds a new thermal cooling device (fan/processor/...) | |
841 | * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself | |
842 | * to all the thermal zone devices registered at the same time. | |
a116b5d4 EV |
843 | * It also gives the opportunity to link the cooling device to a device tree |
844 | * node, so that it can be bound to a thermal zone created out of device tree. | |
3a6eccb3 EV |
845 | * |
846 | * Return: a pointer to the created struct thermal_cooling_device or an | |
847 | * ERR_PTR. Caller must check return value with IS_ERR*() helpers. | |
203d3d4a | 848 | */ |
a116b5d4 EV |
849 | static struct thermal_cooling_device * |
850 | __thermal_cooling_device_register(struct device_node *np, | |
f991de53 | 851 | const char *type, void *devdata, |
a116b5d4 | 852 | const struct thermal_cooling_device_ops *ops) |
203d3d4a ZR |
853 | { |
854 | struct thermal_cooling_device *cdev; | |
4511f716 | 855 | struct thermal_zone_device *pos = NULL; |
0a5c2671 | 856 | int id, ret; |
203d3d4a ZR |
857 | |
858 | if (!ops || !ops->get_max_state || !ops->get_cur_state || | |
543a9561 | 859 | !ops->set_cur_state) |
3e6fda5c | 860 | return ERR_PTR(-EINVAL); |
203d3d4a | 861 | |
9e0a9be2 RW |
862 | if (!thermal_class) |
863 | return ERR_PTR(-ENODEV); | |
864 | ||
95e3ed15 | 865 | cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); |
203d3d4a | 866 | if (!cdev) |
3e6fda5c | 867 | return ERR_PTR(-ENOMEM); |
203d3d4a | 868 | |
5a5b7d8d | 869 | ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL); |
58483761 DL |
870 | if (ret < 0) |
871 | goto out_kfree_cdev; | |
872 | cdev->id = ret; | |
0a5c2671 | 873 | id = ret; |
58483761 DL |
874 | |
875 | cdev->type = kstrdup(type ? type : "", GFP_KERNEL); | |
876 | if (!cdev->type) { | |
877 | ret = -ENOMEM; | |
878 | goto out_ida_remove; | |
203d3d4a ZR |
879 | } |
880 | ||
f4a821ce | 881 | mutex_init(&cdev->lock); |
b5e4ae62 | 882 | INIT_LIST_HEAD(&cdev->thermal_instances); |
a116b5d4 | 883 | cdev->np = np; |
203d3d4a | 884 | cdev->ops = ops; |
5ca0cce5 | 885 | cdev->updated = false; |
9e0a9be2 | 886 | cdev->device.class = thermal_class; |
203d3d4a | 887 | cdev->devdata = devdata; |
c408b3d1 | 888 | |
e49a1e1e | 889 | ret = cdev->ops->get_max_state(cdev, &cdev->max_state); |
e398421f VK |
890 | if (ret) |
891 | goto out_cdev_type; | |
c408b3d1 | 892 | |
8ea22951 | 893 | thermal_cooling_device_setup_sysfs(cdev); |
6c54b7bc | 894 | |
4748f968 | 895 | ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id); |
e398421f VK |
896 | if (ret) |
897 | goto out_cooling_dev; | |
6c54b7bc | 898 | |
58483761 | 899 | ret = device_register(&cdev->device); |
e398421f VK |
900 | if (ret) { |
901 | /* thermal_release() handles rest of the cleanup */ | |
902 | put_device(&cdev->device); | |
903 | return ERR_PTR(ret); | |
904 | } | |
203d3d4a | 905 | |
7e8ee1e9 | 906 | /* Add 'this' new cdev to the global cdev list */ |
203d3d4a | 907 | mutex_lock(&thermal_list_lock); |
cd246fa9 | 908 | |
203d3d4a | 909 | list_add(&cdev->node, &thermal_cdev_list); |
203d3d4a | 910 | |
7e8ee1e9 D |
911 | /* Update binding information for 'this' new cdev */ |
912 | bind_cdev(cdev); | |
913 | ||
4511f716 CY |
914 | list_for_each_entry(pos, &thermal_tz_list, node) |
915 | if (atomic_cmpxchg(&pos->need_update, 1, 0)) | |
0e70f466 SP |
916 | thermal_zone_device_update(pos, |
917 | THERMAL_EVENT_UNSPECIFIED); | |
cd246fa9 | 918 | |
4511f716 CY |
919 | mutex_unlock(&thermal_list_lock); |
920 | ||
7e8ee1e9 | 921 | return cdev; |
58483761 | 922 | |
e398421f | 923 | out_cooling_dev: |
98a160e8 | 924 | thermal_cooling_device_destroy_sysfs(cdev); |
e398421f | 925 | out_cdev_type: |
58483761 | 926 | kfree(cdev->type); |
58483761 | 927 | out_ida_remove: |
5a5b7d8d | 928 | ida_free(&thermal_cdev_ida, id); |
58483761 | 929 | out_kfree_cdev: |
d44616c6 | 930 | kfree(cdev); |
58483761 | 931 | return ERR_PTR(ret); |
203d3d4a | 932 | } |
a116b5d4 EV |
933 | |
934 | /** | |
935 | * thermal_cooling_device_register() - register a new thermal cooling device | |
936 | * @type: the thermal cooling device type. | |
937 | * @devdata: device private data. | |
938 | * @ops: standard thermal cooling devices callbacks. | |
939 | * | |
940 | * This interface function adds a new thermal cooling device (fan/processor/...) | |
941 | * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself | |
942 | * to all the thermal zone devices registered at the same time. | |
943 | * | |
944 | * Return: a pointer to the created struct thermal_cooling_device or an | |
945 | * ERR_PTR. Caller must check return value with IS_ERR*() helpers. | |
946 | */ | |
947 | struct thermal_cooling_device * | |
f991de53 | 948 | thermal_cooling_device_register(const char *type, void *devdata, |
a116b5d4 EV |
949 | const struct thermal_cooling_device_ops *ops) |
950 | { | |
951 | return __thermal_cooling_device_register(NULL, type, devdata, ops); | |
952 | } | |
910cb1e3 | 953 | EXPORT_SYMBOL_GPL(thermal_cooling_device_register); |
203d3d4a | 954 | |
a116b5d4 EV |
955 | /** |
956 | * thermal_of_cooling_device_register() - register an OF thermal cooling device | |
957 | * @np: a pointer to a device tree node. | |
958 | * @type: the thermal cooling device type. | |
959 | * @devdata: device private data. | |
960 | * @ops: standard thermal cooling devices callbacks. | |
961 | * | |
962 | * This function will register a cooling device with device tree node reference. | |
963 | * This interface function adds a new thermal cooling device (fan/processor/...) | |
964 | * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself | |
965 | * to all the thermal zone devices registered at the same time. | |
966 | * | |
967 | * Return: a pointer to the created struct thermal_cooling_device or an | |
968 | * ERR_PTR. Caller must check return value with IS_ERR*() helpers. | |
969 | */ | |
970 | struct thermal_cooling_device * | |
971 | thermal_of_cooling_device_register(struct device_node *np, | |
f991de53 | 972 | const char *type, void *devdata, |
a116b5d4 EV |
973 | const struct thermal_cooling_device_ops *ops) |
974 | { | |
975 | return __thermal_cooling_device_register(np, type, devdata, ops); | |
976 | } | |
977 | EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register); | |
978 | ||
b4ab114c GR |
979 | static void thermal_cooling_device_release(struct device *dev, void *res) |
980 | { | |
981 | thermal_cooling_device_unregister( | |
982 | *(struct thermal_cooling_device **)res); | |
983 | } | |
984 | ||
985 | /** | |
986 | * devm_thermal_of_cooling_device_register() - register an OF thermal cooling | |
987 | * device | |
988 | * @dev: a valid struct device pointer of a sensor device. | |
989 | * @np: a pointer to a device tree node. | |
990 | * @type: the thermal cooling device type. | |
991 | * @devdata: device private data. | |
992 | * @ops: standard thermal cooling devices callbacks. | |
993 | * | |
994 | * This function will register a cooling device with device tree node reference. | |
995 | * This interface function adds a new thermal cooling device (fan/processor/...) | |
996 | * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself | |
997 | * to all the thermal zone devices registered at the same time. | |
998 | * | |
999 | * Return: a pointer to the created struct thermal_cooling_device or an | |
1000 | * ERR_PTR. Caller must check return value with IS_ERR*() helpers. | |
1001 | */ | |
1002 | struct thermal_cooling_device * | |
1003 | devm_thermal_of_cooling_device_register(struct device *dev, | |
1004 | struct device_node *np, | |
1005 | char *type, void *devdata, | |
1006 | const struct thermal_cooling_device_ops *ops) | |
1007 | { | |
1008 | struct thermal_cooling_device **ptr, *tcd; | |
1009 | ||
1010 | ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr), | |
1011 | GFP_KERNEL); | |
1012 | if (!ptr) | |
1013 | return ERR_PTR(-ENOMEM); | |
1014 | ||
1015 | tcd = __thermal_cooling_device_register(np, type, devdata, ops); | |
1016 | if (IS_ERR(tcd)) { | |
1017 | devres_free(ptr); | |
1018 | return tcd; | |
1019 | } | |
1020 | ||
1021 | *ptr = tcd; | |
1022 | devres_add(dev, ptr); | |
1023 | ||
1024 | return tcd; | |
1025 | } | |
1026 | EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register); | |
1027 | ||
c43198af RW |
1028 | static bool thermal_cooling_device_present(struct thermal_cooling_device *cdev) |
1029 | { | |
1030 | struct thermal_cooling_device *pos = NULL; | |
1031 | ||
1032 | list_for_each_entry(pos, &thermal_cdev_list, node) { | |
1033 | if (pos == cdev) | |
1034 | return true; | |
1035 | } | |
1036 | ||
1037 | return false; | |
1038 | } | |
1039 | ||
790930f4 RW |
1040 | /** |
1041 | * thermal_cooling_device_update - Update a cooling device object | |
1042 | * @cdev: Target cooling device. | |
1043 | * | |
1044 | * Update @cdev to reflect a change of the underlying hardware or platform. | |
1045 | * | |
1046 | * Must be called when the maximum cooling state of @cdev becomes invalid and so | |
1047 | * its .get_max_state() callback needs to be run to produce the new maximum | |
1048 | * cooling state value. | |
1049 | */ | |
1050 | void thermal_cooling_device_update(struct thermal_cooling_device *cdev) | |
1051 | { | |
1052 | struct thermal_instance *ti; | |
1053 | unsigned long state; | |
1054 | ||
1055 | if (IS_ERR_OR_NULL(cdev)) | |
1056 | return; | |
1057 | ||
1058 | /* | |
1059 | * Hold thermal_list_lock throughout the update to prevent the device | |
1060 | * from going away while being updated. | |
1061 | */ | |
1062 | mutex_lock(&thermal_list_lock); | |
1063 | ||
1064 | if (!thermal_cooling_device_present(cdev)) | |
1065 | goto unlock_list; | |
1066 | ||
1067 | /* | |
1068 | * Update under the cdev lock to prevent the state from being set beyond | |
1069 | * the new limit concurrently. | |
1070 | */ | |
1071 | mutex_lock(&cdev->lock); | |
1072 | ||
1073 | if (cdev->ops->get_max_state(cdev, &cdev->max_state)) | |
1074 | goto unlock; | |
1075 | ||
1076 | thermal_cooling_device_stats_reinit(cdev); | |
1077 | ||
1078 | list_for_each_entry(ti, &cdev->thermal_instances, cdev_node) { | |
1079 | if (ti->upper == cdev->max_state) | |
1080 | continue; | |
1081 | ||
1082 | if (ti->upper < cdev->max_state) { | |
1083 | if (ti->upper_no_limit) | |
1084 | ti->upper = cdev->max_state; | |
1085 | ||
1086 | continue; | |
1087 | } | |
1088 | ||
1089 | ti->upper = cdev->max_state; | |
1090 | if (ti->lower > ti->upper) | |
1091 | ti->lower = ti->upper; | |
1092 | ||
1093 | if (ti->target == THERMAL_NO_TARGET) | |
1094 | continue; | |
1095 | ||
1096 | if (ti->target > ti->upper) | |
1097 | ti->target = ti->upper; | |
1098 | } | |
1099 | ||
1100 | if (cdev->ops->get_cur_state(cdev, &state) || state > cdev->max_state) | |
1101 | goto unlock; | |
1102 | ||
1103 | thermal_cooling_device_stats_update(cdev, state); | |
1104 | ||
1105 | unlock: | |
1106 | mutex_unlock(&cdev->lock); | |
1107 | ||
1108 | unlock_list: | |
1109 | mutex_unlock(&thermal_list_lock); | |
1110 | } | |
1111 | EXPORT_SYMBOL_GPL(thermal_cooling_device_update); | |
1112 | ||
203d3d4a | 1113 | /** |
38e7b549 | 1114 | * thermal_cooling_device_unregister - removes a thermal cooling device |
203d3d4a ZR |
1115 | * @cdev: the thermal cooling device to remove. |
1116 | * | |
38e7b549 EV |
1117 | * thermal_cooling_device_unregister() must be called when a registered |
1118 | * thermal cooling device is no longer needed. | |
203d3d4a | 1119 | */ |
7e8ee1e9 | 1120 | void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev) |
203d3d4a ZR |
1121 | { |
1122 | struct thermal_zone_device *tz; | |
203d3d4a ZR |
1123 | |
1124 | if (!cdev) | |
1125 | return; | |
1126 | ||
1127 | mutex_lock(&thermal_list_lock); | |
c43198af RW |
1128 | |
1129 | if (!thermal_cooling_device_present(cdev)) { | |
203d3d4a ZR |
1130 | mutex_unlock(&thermal_list_lock); |
1131 | return; | |
1132 | } | |
c43198af | 1133 | |
203d3d4a | 1134 | list_del(&cdev->node); |
7e8ee1e9 D |
1135 | |
1136 | /* Unbind all thermal zones associated with 'this' cdev */ | |
203d3d4a | 1137 | list_for_each_entry(tz, &thermal_tz_list, node) { |
ded2d383 | 1138 | if (tz->ops->unbind) |
7e8ee1e9 | 1139 | tz->ops->unbind(tz, cdev); |
203d3d4a | 1140 | } |
7e8ee1e9 | 1141 | |
203d3d4a | 1142 | mutex_unlock(&thermal_list_lock); |
7e8ee1e9 | 1143 | |
47e3f000 | 1144 | device_unregister(&cdev->device); |
203d3d4a | 1145 | } |
910cb1e3 | 1146 | EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister); |
203d3d4a | 1147 | |
90f5b5bb | 1148 | static void bind_tz(struct thermal_zone_device *tz) |
ce119f83 | 1149 | { |
ded2d383 | 1150 | int ret; |
90f5b5bb | 1151 | struct thermal_cooling_device *pos = NULL; |
ce119f83 | 1152 | |
ded2d383 | 1153 | if (!tz->ops->bind) |
ce119f83 | 1154 | return; |
c56f5c03 | 1155 | |
90f5b5bb | 1156 | mutex_lock(&thermal_list_lock); |
c56f5c03 | 1157 | |
90f5b5bb | 1158 | list_for_each_entry(pos, &thermal_cdev_list, node) { |
ded2d383 ZR |
1159 | ret = tz->ops->bind(tz, pos); |
1160 | if (ret) | |
1161 | print_bind_err_msg(tz, pos, ret); | |
c56f5c03 | 1162 | } |
ded2d383 | 1163 | |
90f5b5bb | 1164 | mutex_unlock(&thermal_list_lock); |
c56f5c03 D |
1165 | } |
1166 | ||
e5f2cda6 DL |
1167 | static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms) |
1168 | { | |
1169 | *delay_jiffies = msecs_to_jiffies(delay_ms); | |
1170 | if (delay_ms > 1000) | |
1171 | *delay_jiffies = round_jiffies(*delay_jiffies); | |
1172 | } | |
1173 | ||
7c3d5c20 DL |
1174 | int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp) |
1175 | { | |
1176 | int i, ret = -EINVAL; | |
1177 | ||
1178 | if (tz->ops->get_crit_temp) | |
1179 | return tz->ops->get_crit_temp(tz, temp); | |
1180 | ||
1181 | if (!tz->trips) | |
1182 | return -EINVAL; | |
1183 | ||
1184 | mutex_lock(&tz->lock); | |
1185 | ||
1186 | for (i = 0; i < tz->num_trips; i++) { | |
1187 | if (tz->trips[i].type == THERMAL_TRIP_CRITICAL) { | |
1188 | *temp = tz->trips[i].temperature; | |
1189 | ret = 0; | |
1190 | break; | |
1191 | } | |
1192 | } | |
1193 | ||
1194 | mutex_unlock(&tz->lock); | |
1195 | ||
1196 | return ret; | |
1197 | } | |
1198 | EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp); | |
1199 | ||
203d3d4a | 1200 | /** |
fae11de5 | 1201 | * thermal_zone_device_register_with_trips() - register a new thermal zone device |
203d3d4a | 1202 | * @type: the thermal zone device type |
fae11de5 | 1203 | * @trips: a pointer to an array of thermal trips |
e5bfcd30 | 1204 | * @num_trips: the number of trip points the thermal zone support |
c56f5c03 | 1205 | * @mask: a bit string indicating the writeablility of trip points |
203d3d4a ZR |
1206 | * @devdata: private device data |
1207 | * @ops: standard thermal zone device callbacks | |
50125a9b | 1208 | * @tzp: thermal zone platform parameters |
b1569e99 MG |
1209 | * @passive_delay: number of milliseconds to wait between polls when |
1210 | * performing passive cooling | |
1211 | * @polling_delay: number of milliseconds to wait between polls when checking | |
1212 | * whether trip points have been crossed (0 for interrupt | |
1213 | * driven systems) | |
203d3d4a | 1214 | * |
a00e55f9 EV |
1215 | * This interface function adds a new thermal zone device (sensor) to |
1216 | * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the | |
1217 | * thermal cooling devices registered at the same time. | |
203d3d4a | 1218 | * thermal_zone_device_unregister() must be called when the device is no |
1b7ddb84 | 1219 | * longer needed. The passive cooling depends on the .get_trend() return value. |
a00e55f9 EV |
1220 | * |
1221 | * Return: a pointer to the created struct thermal_zone_device or an | |
1222 | * in case of error, an ERR_PTR. Caller must check return value with | |
1223 | * IS_ERR*() helpers. | |
203d3d4a | 1224 | */ |
eb7be329 | 1225 | struct thermal_zone_device * |
fae11de5 DL |
1226 | thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *trips, int num_trips, int mask, |
1227 | void *devdata, struct thermal_zone_device_ops *ops, | |
80ddce5f | 1228 | const struct thermal_zone_params *tzp, int passive_delay, |
fae11de5 | 1229 | int polling_delay) |
203d3d4a ZR |
1230 | { |
1231 | struct thermal_zone_device *tz; | |
adc8749b | 1232 | int id; |
203d3d4a ZR |
1233 | int result; |
1234 | int count; | |
e33df1d2 | 1235 | struct thermal_governor *governor; |
203d3d4a | 1236 | |
67eed44b | 1237 | if (!type || strlen(type) == 0) { |
3f95ac32 | 1238 | pr_err("No thermal zone type defined\n"); |
54fa38cc | 1239 | return ERR_PTR(-EINVAL); |
67eed44b | 1240 | } |
54fa38cc | 1241 | |
c71d8035 | 1242 | if (strlen(type) >= THERMAL_NAME_LENGTH) { |
3f95ac32 | 1243 | pr_err("Thermal zone name (%s) too long, should be under %d chars\n", |
67eed44b | 1244 | type, THERMAL_NAME_LENGTH); |
3e6fda5c | 1245 | return ERR_PTR(-EINVAL); |
67eed44b | 1246 | } |
203d3d4a | 1247 | |
82b1ec79 SP |
1248 | /* |
1249 | * Max trip count can't exceed 31 as the "mask >> num_trips" condition. | |
1250 | * For example, shifting by 32 will result in compiler warning: | |
1251 | * warning: right shift count >= width of type [-Wshift-count- overflow] | |
1252 | * | |
1253 | * Also "mask >> num_trips" will always be true with 32 bit shift. | |
1254 | * E.g. mask = 0x80000000 for trip id 31 to be RW. Then | |
1255 | * mask >> 32 = 0x80000000 | |
1256 | * This will result in failure for the below condition. | |
1257 | * | |
1258 | * Check will be true when the bit 31 of the mask is set. | |
1259 | * 32 bit shift will cause overflow of 4 byte integer. | |
1260 | */ | |
1261 | if (num_trips > (BITS_PER_TYPE(int) - 1) || num_trips < 0 || mask >> num_trips) { | |
3f95ac32 | 1262 | pr_err("Incorrect number of thermal trips\n"); |
3e6fda5c | 1263 | return ERR_PTR(-EINVAL); |
67eed44b | 1264 | } |
203d3d4a | 1265 | |
67eed44b | 1266 | if (!ops) { |
3f95ac32 | 1267 | pr_err("Thermal zone device ops not defined\n"); |
3e6fda5c | 1268 | return ERR_PTR(-EINVAL); |
67eed44b | 1269 | } |
203d3d4a | 1270 | |
35d8dbbb | 1271 | if (num_trips > 0 && !trips) |
6b2aa51d EV |
1272 | return ERR_PTR(-EINVAL); |
1273 | ||
9e0a9be2 RW |
1274 | if (!thermal_class) |
1275 | return ERR_PTR(-ENODEV); | |
1276 | ||
95e3ed15 | 1277 | tz = kzalloc(sizeof(*tz), GFP_KERNEL); |
203d3d4a | 1278 | if (!tz) |
3e6fda5c | 1279 | return ERR_PTR(-ENOMEM); |
203d3d4a | 1280 | |
3d439b1a DL |
1281 | if (tzp) { |
1282 | tz->tzp = kmemdup(tzp, sizeof(*tzp), GFP_KERNEL); | |
1283 | if (!tz->tzp) { | |
1284 | result = -ENOMEM; | |
1285 | goto free_tz; | |
1286 | } | |
1287 | } | |
1288 | ||
2d374139 | 1289 | INIT_LIST_HEAD(&tz->thermal_instances); |
b31ef828 | 1290 | ida_init(&tz->ida); |
203d3d4a | 1291 | mutex_init(&tz->lock); |
5a5b7d8d | 1292 | id = ida_alloc(&thermal_tz_ida, GFP_KERNEL); |
adc8749b YH |
1293 | if (id < 0) { |
1294 | result = id; | |
3d439b1a | 1295 | goto free_tzp; |
adc8749b | 1296 | } |
203d3d4a | 1297 | |
adc8749b | 1298 | tz->id = id; |
1e6c8fb8 | 1299 | strscpy(tz->type, type, sizeof(tz->type)); |
d7203eed DL |
1300 | |
1301 | if (!ops->critical) | |
1302 | ops->critical = thermal_zone_device_critical; | |
1303 | ||
203d3d4a | 1304 | tz->ops = ops; |
9e0a9be2 | 1305 | tz->device.class = thermal_class; |
203d3d4a ZR |
1306 | tz->devdata = devdata; |
1307 | tz->trips = trips; | |
e5bfcd30 | 1308 | tz->num_trips = num_trips; |
1c600861 | 1309 | |
17d399cd DL |
1310 | thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay); |
1311 | thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay); | |
1312 | ||
4d0fe749 | 1313 | /* sys I/F */ |
1c600861 | 1314 | /* Add nodes that are always present via .groups */ |
4d0fe749 EV |
1315 | result = thermal_zone_create_device_groups(tz, mask); |
1316 | if (result) | |
9d9ca1f9 | 1317 | goto remove_id; |
4d0fe749 | 1318 | |
4511f716 CY |
1319 | /* A new thermal zone needs to be updated anyway. */ |
1320 | atomic_set(&tz->need_update, 1); | |
b1569e99 | 1321 | |
4748f968 YY |
1322 | result = dev_set_name(&tz->device, "thermal_zone%d", tz->id); |
1323 | if (result) { | |
1324 | thermal_zone_destroy_device_groups(tz); | |
1325 | goto remove_id; | |
1326 | } | |
203d3d4a | 1327 | result = device_register(&tz->device); |
9d9ca1f9 | 1328 | if (result) |
adc8749b | 1329 | goto release_device; |
203d3d4a | 1330 | |
e5bfcd30 | 1331 | for (count = 0; count < num_trips; count++) { |
7c3d5c20 DL |
1332 | struct thermal_trip trip; |
1333 | ||
1334 | result = thermal_zone_get_trip(tz, count, &trip); | |
f1b80a38 | 1335 | if (result || !trip.temperature) |
81ad4276 | 1336 | set_bit(count, &tz->trips_disabled); |
203d3d4a ZR |
1337 | } |
1338 | ||
a4a15485 D |
1339 | /* Update 'this' zone's governor information */ |
1340 | mutex_lock(&thermal_governor_lock); | |
1341 | ||
1342 | if (tz->tzp) | |
e33df1d2 | 1343 | governor = __find_governor(tz->tzp->governor_name); |
a4a15485 | 1344 | else |
e33df1d2 JM |
1345 | governor = def_governor; |
1346 | ||
1347 | result = thermal_set_governor(tz, governor); | |
1348 | if (result) { | |
1349 | mutex_unlock(&thermal_governor_lock); | |
1350 | goto unregister; | |
1351 | } | |
a4a15485 D |
1352 | |
1353 | mutex_unlock(&thermal_governor_lock); | |
1354 | ||
ccba4ffd EV |
1355 | if (!tz->tzp || !tz->tzp->no_hwmon) { |
1356 | result = thermal_add_hwmon_sysfs(tz); | |
1357 | if (result) | |
1358 | goto unregister; | |
1359 | } | |
e68b16ab | 1360 | |
203d3d4a ZR |
1361 | mutex_lock(&thermal_list_lock); |
1362 | list_add_tail(&tz->node, &thermal_tz_list); | |
203d3d4a ZR |
1363 | mutex_unlock(&thermal_list_lock); |
1364 | ||
7e8ee1e9 D |
1365 | /* Bind cooling devices for this zone */ |
1366 | bind_tz(tz); | |
1367 | ||
b659a30d | 1368 | INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check); |
b1569e99 | 1369 | |
d0df264f | 1370 | thermal_zone_device_init(tz); |
4511f716 CY |
1371 | /* Update the new thermal zone and mark it as already updated. */ |
1372 | if (atomic_cmpxchg(&tz->need_update, 1, 0)) | |
0e70f466 | 1373 | thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); |
b1569e99 | 1374 | |
55cdf0a2 DL |
1375 | thermal_notify_tz_create(tz->id, tz->type); |
1376 | ||
14015860 | 1377 | return tz; |
203d3d4a | 1378 | |
caca8b80 | 1379 | unregister: |
adc8749b YH |
1380 | device_del(&tz->device); |
1381 | release_device: | |
1382 | put_device(&tz->device); | |
1383 | tz = NULL; | |
9d9ca1f9 | 1384 | remove_id: |
5a5b7d8d | 1385 | ida_free(&thermal_tz_ida, id); |
3d439b1a DL |
1386 | free_tzp: |
1387 | kfree(tz->tzp); | |
9d9ca1f9 CJ |
1388 | free_tz: |
1389 | kfree(tz); | |
1390 | return ERR_PTR(result); | |
203d3d4a | 1391 | } |
a921be53 | 1392 | EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips); |
fae11de5 | 1393 | |
d332db8f RW |
1394 | struct thermal_zone_device *thermal_tripless_zone_device_register( |
1395 | const char *type, | |
1396 | void *devdata, | |
1397 | struct thermal_zone_device_ops *ops, | |
1398 | const struct thermal_zone_params *tzp) | |
1399 | { | |
1400 | return thermal_zone_device_register_with_trips(type, NULL, 0, 0, devdata, | |
1401 | ops, tzp, 0, 0); | |
1402 | } | |
1403 | EXPORT_SYMBOL_GPL(thermal_tripless_zone_device_register); | |
1404 | ||
a6ff3c00 DL |
1405 | void *thermal_zone_device_priv(struct thermal_zone_device *tzd) |
1406 | { | |
1407 | return tzd->devdata; | |
1408 | } | |
1409 | EXPORT_SYMBOL_GPL(thermal_zone_device_priv); | |
1410 | ||
072e35c9 DL |
1411 | const char *thermal_zone_device_type(struct thermal_zone_device *tzd) |
1412 | { | |
1413 | return tzd->type; | |
1414 | } | |
1415 | EXPORT_SYMBOL_GPL(thermal_zone_device_type); | |
1416 | ||
3034f859 DL |
1417 | int thermal_zone_device_id(struct thermal_zone_device *tzd) |
1418 | { | |
1419 | return tzd->id; | |
1420 | } | |
1421 | EXPORT_SYMBOL_GPL(thermal_zone_device_id); | |
1422 | ||
7cefbaf0 DL |
1423 | struct device *thermal_zone_device(struct thermal_zone_device *tzd) |
1424 | { | |
1425 | return &tzd->device; | |
1426 | } | |
1427 | EXPORT_SYMBOL_GPL(thermal_zone_device); | |
1428 | ||
203d3d4a | 1429 | /** |
a052b511 | 1430 | * thermal_zone_device_unregister - removes the registered thermal zone device |
203d3d4a ZR |
1431 | * @tz: the thermal zone device to remove |
1432 | */ | |
1433 | void thermal_zone_device_unregister(struct thermal_zone_device *tz) | |
1434 | { | |
ded2d383 | 1435 | int tz_id; |
203d3d4a ZR |
1436 | struct thermal_cooling_device *cdev; |
1437 | struct thermal_zone_device *pos = NULL; | |
203d3d4a ZR |
1438 | |
1439 | if (!tz) | |
1440 | return; | |
1441 | ||
a5f785ce | 1442 | tz_id = tz->id; |
7e8ee1e9 | 1443 | |
203d3d4a ZR |
1444 | mutex_lock(&thermal_list_lock); |
1445 | list_for_each_entry(pos, &thermal_tz_list, node) | |
b659a30d EV |
1446 | if (pos == tz) |
1447 | break; | |
203d3d4a ZR |
1448 | if (pos != tz) { |
1449 | /* thermal zone device not found */ | |
1450 | mutex_unlock(&thermal_list_lock); | |
1451 | return; | |
1452 | } | |
1453 | list_del(&tz->node); | |
7e8ee1e9 D |
1454 | |
1455 | /* Unbind all cdevs associated with 'this' thermal zone */ | |
ded2d383 ZR |
1456 | list_for_each_entry(cdev, &thermal_cdev_list, node) |
1457 | if (tz->ops->unbind) | |
7e8ee1e9 | 1458 | tz->ops->unbind(tz, cdev); |
7e8ee1e9 | 1459 | |
203d3d4a ZR |
1460 | mutex_unlock(&thermal_list_lock); |
1461 | ||
163b00cd | 1462 | cancel_delayed_work_sync(&tz->poll_queue); |
b1569e99 | 1463 | |
e33df1d2 | 1464 | thermal_set_governor(tz, NULL); |
203d3d4a | 1465 | |
e68b16ab | 1466 | thermal_remove_hwmon_sysfs(tz); |
5a5b7d8d | 1467 | ida_free(&thermal_tz_ida, tz->id); |
b31ef828 | 1468 | ida_destroy(&tz->ida); |
30b2ae07 GR |
1469 | |
1470 | mutex_lock(&tz->lock); | |
1471 | device_del(&tz->device); | |
1472 | mutex_unlock(&tz->lock); | |
1473 | ||
3d439b1a DL |
1474 | kfree(tz->tzp); |
1475 | ||
30b2ae07 | 1476 | put_device(&tz->device); |
55cdf0a2 | 1477 | |
a5f785ce | 1478 | thermal_notify_tz_delete(tz_id); |
203d3d4a | 1479 | } |
910cb1e3 | 1480 | EXPORT_SYMBOL_GPL(thermal_zone_device_unregister); |
203d3d4a | 1481 | |
63c4d919 EV |
1482 | /** |
1483 | * thermal_zone_get_zone_by_name() - search for a zone and returns its ref | |
1484 | * @name: thermal zone name to fetch the temperature | |
1485 | * | |
1486 | * When only one zone is found with the passed name, returns a reference to it. | |
1487 | * | |
1488 | * Return: On success returns a reference to an unique thermal zone with | |
1489 | * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid | |
1490 | * paramenters, -ENODEV for not found and -EEXIST for multiple matches). | |
1491 | */ | |
1492 | struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name) | |
1493 | { | |
1494 | struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL); | |
1495 | unsigned int found = 0; | |
1496 | ||
1497 | if (!name) | |
1498 | goto exit; | |
1499 | ||
1500 | mutex_lock(&thermal_list_lock); | |
1501 | list_for_each_entry(pos, &thermal_tz_list, node) | |
484ac2f3 | 1502 | if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) { |
63c4d919 EV |
1503 | found++; |
1504 | ref = pos; | |
1505 | } | |
1506 | mutex_unlock(&thermal_list_lock); | |
1507 | ||
1508 | /* nothing has been found, thus an error code for it */ | |
1509 | if (found == 0) | |
1510 | ref = ERR_PTR(-ENODEV); | |
1511 | else if (found > 1) | |
1512 | /* Success only when an unique zone is found */ | |
1513 | ref = ERR_PTR(-EEXIST); | |
1514 | ||
1515 | exit: | |
1516 | return ref; | |
1517 | } | |
1518 | EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name); | |
1519 | ||
ff140fea | 1520 | static int thermal_pm_notify(struct notifier_block *nb, |
eb7be329 | 1521 | unsigned long mode, void *_unused) |
ff140fea ZR |
1522 | { |
1523 | struct thermal_zone_device *tz; | |
1524 | ||
1525 | switch (mode) { | |
1526 | case PM_HIBERNATION_PREPARE: | |
1527 | case PM_RESTORE_PREPARE: | |
1528 | case PM_SUSPEND_PREPARE: | |
1529 | atomic_set(&in_suspend, 1); | |
1530 | break; | |
1531 | case PM_POST_HIBERNATION: | |
1532 | case PM_POST_RESTORE: | |
1533 | case PM_POST_SUSPEND: | |
1534 | atomic_set(&in_suspend, 0); | |
1535 | list_for_each_entry(tz, &thermal_tz_list, node) { | |
964f4843 | 1536 | thermal_zone_device_init(tz); |
0e70f466 SP |
1537 | thermal_zone_device_update(tz, |
1538 | THERMAL_EVENT_UNSPECIFIED); | |
ff140fea ZR |
1539 | } |
1540 | break; | |
1541 | default: | |
1542 | break; | |
1543 | } | |
1544 | return 0; | |
1545 | } | |
1546 | ||
1547 | static struct notifier_block thermal_pm_nb = { | |
1548 | .notifier_call = thermal_pm_notify, | |
1549 | }; | |
1550 | ||
203d3d4a ZR |
1551 | static int __init thermal_init(void) |
1552 | { | |
80a26a5c ZR |
1553 | int result; |
1554 | ||
d2a89b52 DL |
1555 | result = thermal_netlink_init(); |
1556 | if (result) | |
1557 | goto error; | |
1558 | ||
80a26a5c ZR |
1559 | result = thermal_register_governors(); |
1560 | if (result) | |
58d1c9fd | 1561 | goto unregister_netlink; |
203d3d4a | 1562 | |
9e0a9be2 RW |
1563 | thermal_class = kzalloc(sizeof(*thermal_class), GFP_KERNEL); |
1564 | if (!thermal_class) { | |
1565 | result = -ENOMEM; | |
1566 | goto unregister_governors; | |
1567 | } | |
1568 | ||
1569 | thermal_class->name = "thermal"; | |
1570 | thermal_class->dev_release = thermal_release; | |
1571 | ||
1572 | result = class_register(thermal_class); | |
1573 | if (result) { | |
1574 | kfree(thermal_class); | |
1575 | thermal_class = NULL; | |
80a26a5c | 1576 | goto unregister_governors; |
9e0a9be2 | 1577 | } |
80a26a5c | 1578 | |
ff140fea ZR |
1579 | result = register_pm_notifier(&thermal_pm_nb); |
1580 | if (result) | |
1581 | pr_warn("Thermal: Can not register suspend notifier, return %d\n", | |
1582 | result); | |
1583 | ||
80a26a5c ZR |
1584 | return 0; |
1585 | ||
9d367e5e LH |
1586 | unregister_governors: |
1587 | thermal_unregister_governors(); | |
58d1c9fd DL |
1588 | unregister_netlink: |
1589 | thermal_netlink_exit(); | |
80a26a5c | 1590 | error: |
80a26a5c ZR |
1591 | mutex_destroy(&thermal_list_lock); |
1592 | mutex_destroy(&thermal_governor_lock); | |
203d3d4a ZR |
1593 | return result; |
1594 | } | |
3f5a2cbe | 1595 | postcore_initcall(thermal_init); |