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