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