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> | |
35 | #include <linux/spinlock.h> | |
b1569e99 | 36 | #include <linux/reboot.h> |
4cb18728 D |
37 | #include <net/netlink.h> |
38 | #include <net/genetlink.h> | |
203d3d4a | 39 | |
71350db4 D |
40 | #include "thermal_core.h" |
41 | ||
63c4ec90 | 42 | MODULE_AUTHOR("Zhang Rui"); |
203d3d4a ZR |
43 | MODULE_DESCRIPTION("Generic thermal management sysfs support"); |
44 | MODULE_LICENSE("GPL"); | |
45 | ||
203d3d4a ZR |
46 | static DEFINE_IDR(thermal_tz_idr); |
47 | static DEFINE_IDR(thermal_cdev_idr); | |
48 | static DEFINE_MUTEX(thermal_idr_lock); | |
49 | ||
50 | static LIST_HEAD(thermal_tz_list); | |
51 | static LIST_HEAD(thermal_cdev_list); | |
a4a15485 D |
52 | static LIST_HEAD(thermal_governor_list); |
53 | ||
203d3d4a | 54 | static DEFINE_MUTEX(thermal_list_lock); |
a4a15485 D |
55 | static DEFINE_MUTEX(thermal_governor_lock); |
56 | ||
57 | static struct thermal_governor *__find_governor(const char *name) | |
58 | { | |
59 | struct thermal_governor *pos; | |
60 | ||
61 | list_for_each_entry(pos, &thermal_governor_list, governor_list) | |
62 | if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH)) | |
63 | return pos; | |
64 | ||
65 | return NULL; | |
66 | } | |
67 | ||
68 | int thermal_register_governor(struct thermal_governor *governor) | |
69 | { | |
70 | int err; | |
71 | const char *name; | |
72 | struct thermal_zone_device *pos; | |
73 | ||
74 | if (!governor) | |
75 | return -EINVAL; | |
76 | ||
77 | mutex_lock(&thermal_governor_lock); | |
78 | ||
79 | err = -EBUSY; | |
80 | if (__find_governor(governor->name) == NULL) { | |
81 | err = 0; | |
82 | list_add(&governor->governor_list, &thermal_governor_list); | |
83 | } | |
84 | ||
85 | mutex_lock(&thermal_list_lock); | |
86 | ||
87 | list_for_each_entry(pos, &thermal_tz_list, node) { | |
88 | if (pos->governor) | |
89 | continue; | |
90 | if (pos->tzp) | |
91 | name = pos->tzp->governor_name; | |
92 | else | |
93 | name = DEFAULT_THERMAL_GOVERNOR; | |
94 | if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH)) | |
95 | pos->governor = governor; | |
96 | } | |
97 | ||
98 | mutex_unlock(&thermal_list_lock); | |
99 | mutex_unlock(&thermal_governor_lock); | |
100 | ||
101 | return err; | |
102 | } | |
103 | EXPORT_SYMBOL_GPL(thermal_register_governor); | |
104 | ||
105 | void thermal_unregister_governor(struct thermal_governor *governor) | |
106 | { | |
107 | struct thermal_zone_device *pos; | |
108 | ||
109 | if (!governor) | |
110 | return; | |
111 | ||
112 | mutex_lock(&thermal_governor_lock); | |
113 | ||
114 | if (__find_governor(governor->name) == NULL) | |
115 | goto exit; | |
116 | ||
117 | mutex_lock(&thermal_list_lock); | |
118 | ||
119 | list_for_each_entry(pos, &thermal_tz_list, node) { | |
120 | if (!strnicmp(pos->governor->name, governor->name, | |
121 | THERMAL_NAME_LENGTH)) | |
122 | pos->governor = NULL; | |
123 | } | |
124 | ||
125 | mutex_unlock(&thermal_list_lock); | |
126 | list_del(&governor->governor_list); | |
127 | exit: | |
128 | mutex_unlock(&thermal_governor_lock); | |
129 | return; | |
130 | } | |
131 | EXPORT_SYMBOL_GPL(thermal_unregister_governor); | |
203d3d4a ZR |
132 | |
133 | static int get_idr(struct idr *idr, struct mutex *lock, int *id) | |
134 | { | |
135 | int err; | |
136 | ||
caca8b80 | 137 | again: |
203d3d4a ZR |
138 | if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0)) |
139 | return -ENOMEM; | |
140 | ||
141 | if (lock) | |
142 | mutex_lock(lock); | |
143 | err = idr_get_new(idr, NULL, id); | |
144 | if (lock) | |
145 | mutex_unlock(lock); | |
146 | if (unlikely(err == -EAGAIN)) | |
147 | goto again; | |
148 | else if (unlikely(err)) | |
149 | return err; | |
150 | ||
125c4c70 | 151 | *id = *id & MAX_IDR_MASK; |
203d3d4a ZR |
152 | return 0; |
153 | } | |
154 | ||
155 | static void release_idr(struct idr *idr, struct mutex *lock, int id) | |
156 | { | |
157 | if (lock) | |
158 | mutex_lock(lock); | |
159 | idr_remove(idr, id); | |
160 | if (lock) | |
161 | mutex_unlock(lock); | |
162 | } | |
163 | ||
9b4298a0 D |
164 | int get_tz_trend(struct thermal_zone_device *tz, int trip) |
165 | { | |
166 | enum thermal_trend trend; | |
167 | ||
168 | if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) { | |
169 | if (tz->temperature > tz->last_temperature) | |
170 | trend = THERMAL_TREND_RAISING; | |
171 | else if (tz->temperature < tz->last_temperature) | |
172 | trend = THERMAL_TREND_DROPPING; | |
173 | else | |
174 | trend = THERMAL_TREND_STABLE; | |
175 | } | |
176 | ||
177 | return trend; | |
178 | } | |
179 | EXPORT_SYMBOL(get_tz_trend); | |
180 | ||
181 | struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz, | |
182 | struct thermal_cooling_device *cdev, int trip) | |
183 | { | |
184 | struct thermal_instance *pos = NULL; | |
185 | struct thermal_instance *target_instance = NULL; | |
186 | ||
187 | mutex_lock(&tz->lock); | |
188 | mutex_lock(&cdev->lock); | |
189 | ||
190 | list_for_each_entry(pos, &tz->thermal_instances, tz_node) { | |
191 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { | |
192 | target_instance = pos; | |
193 | break; | |
194 | } | |
195 | } | |
196 | ||
197 | mutex_unlock(&cdev->lock); | |
198 | mutex_unlock(&tz->lock); | |
199 | ||
200 | return target_instance; | |
201 | } | |
202 | EXPORT_SYMBOL(get_thermal_instance); | |
203 | ||
7e8ee1e9 D |
204 | static void print_bind_err_msg(struct thermal_zone_device *tz, |
205 | struct thermal_cooling_device *cdev, int ret) | |
206 | { | |
207 | dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n", | |
208 | tz->type, cdev->type, ret); | |
209 | } | |
210 | ||
211 | static void __bind(struct thermal_zone_device *tz, int mask, | |
212 | struct thermal_cooling_device *cdev) | |
213 | { | |
214 | int i, ret; | |
215 | ||
216 | for (i = 0; i < tz->trips; i++) { | |
217 | if (mask & (1 << i)) { | |
218 | ret = thermal_zone_bind_cooling_device(tz, i, cdev, | |
219 | THERMAL_NO_LIMIT, THERMAL_NO_LIMIT); | |
220 | if (ret) | |
221 | print_bind_err_msg(tz, cdev, ret); | |
222 | } | |
223 | } | |
224 | } | |
225 | ||
226 | static void __unbind(struct thermal_zone_device *tz, int mask, | |
227 | struct thermal_cooling_device *cdev) | |
228 | { | |
229 | int i; | |
230 | ||
231 | for (i = 0; i < tz->trips; i++) | |
232 | if (mask & (1 << i)) | |
233 | thermal_zone_unbind_cooling_device(tz, i, cdev); | |
234 | } | |
235 | ||
236 | static void bind_cdev(struct thermal_cooling_device *cdev) | |
237 | { | |
238 | int i, ret; | |
239 | const struct thermal_zone_params *tzp; | |
240 | struct thermal_zone_device *pos = NULL; | |
241 | ||
242 | mutex_lock(&thermal_list_lock); | |
243 | ||
244 | list_for_each_entry(pos, &thermal_tz_list, node) { | |
245 | if (!pos->tzp && !pos->ops->bind) | |
246 | continue; | |
247 | ||
248 | if (!pos->tzp && pos->ops->bind) { | |
249 | ret = pos->ops->bind(pos, cdev); | |
250 | if (ret) | |
251 | print_bind_err_msg(pos, cdev, ret); | |
252 | } | |
253 | ||
254 | tzp = pos->tzp; | |
255 | if (!tzp->tbp) | |
256 | return; | |
257 | ||
258 | for (i = 0; i < tzp->num_tbps; i++) { | |
259 | if (tzp->tbp[i].cdev || !tzp->tbp[i].match) | |
260 | continue; | |
261 | if (tzp->tbp[i].match(pos, cdev)) | |
262 | continue; | |
263 | tzp->tbp[i].cdev = cdev; | |
264 | __bind(pos, tzp->tbp[i].trip_mask, cdev); | |
265 | } | |
266 | } | |
267 | ||
268 | mutex_unlock(&thermal_list_lock); | |
269 | } | |
270 | ||
271 | static void bind_tz(struct thermal_zone_device *tz) | |
272 | { | |
273 | int i, ret; | |
274 | struct thermal_cooling_device *pos = NULL; | |
275 | const struct thermal_zone_params *tzp = tz->tzp; | |
276 | ||
277 | if (!tzp && !tz->ops->bind) | |
278 | return; | |
279 | ||
280 | mutex_lock(&thermal_list_lock); | |
281 | ||
282 | /* If there is no platform data, try to use ops->bind */ | |
283 | if (!tzp && tz->ops->bind) { | |
284 | list_for_each_entry(pos, &thermal_cdev_list, node) { | |
285 | ret = tz->ops->bind(tz, pos); | |
286 | if (ret) | |
287 | print_bind_err_msg(tz, pos, ret); | |
288 | } | |
289 | goto exit; | |
290 | } | |
291 | ||
292 | if (!tzp->tbp) | |
293 | goto exit; | |
294 | ||
295 | list_for_each_entry(pos, &thermal_cdev_list, node) { | |
296 | for (i = 0; i < tzp->num_tbps; i++) { | |
297 | if (tzp->tbp[i].cdev || !tzp->tbp[i].match) | |
298 | continue; | |
299 | if (tzp->tbp[i].match(tz, pos)) | |
300 | continue; | |
301 | tzp->tbp[i].cdev = pos; | |
302 | __bind(tz, tzp->tbp[i].trip_mask, pos); | |
303 | } | |
304 | } | |
305 | exit: | |
306 | mutex_unlock(&thermal_list_lock); | |
307 | } | |
308 | ||
0c01ebbf D |
309 | static void thermal_zone_device_set_polling(struct thermal_zone_device *tz, |
310 | int delay) | |
311 | { | |
312 | if (delay > 1000) | |
313 | mod_delayed_work(system_freezable_wq, &tz->poll_queue, | |
314 | round_jiffies(msecs_to_jiffies(delay))); | |
315 | else if (delay) | |
316 | mod_delayed_work(system_freezable_wq, &tz->poll_queue, | |
317 | msecs_to_jiffies(delay)); | |
318 | else | |
319 | cancel_delayed_work(&tz->poll_queue); | |
320 | } | |
321 | ||
322 | static void monitor_thermal_zone(struct thermal_zone_device *tz) | |
323 | { | |
324 | mutex_lock(&tz->lock); | |
325 | ||
326 | if (tz->passive) | |
327 | thermal_zone_device_set_polling(tz, tz->passive_delay); | |
328 | else if (tz->polling_delay) | |
329 | thermal_zone_device_set_polling(tz, tz->polling_delay); | |
330 | else | |
331 | thermal_zone_device_set_polling(tz, 0); | |
332 | ||
333 | mutex_unlock(&tz->lock); | |
334 | } | |
335 | ||
336 | static void handle_non_critical_trips(struct thermal_zone_device *tz, | |
337 | int trip, enum thermal_trip_type trip_type) | |
338 | { | |
339 | tz->governor->throttle(tz, trip); | |
340 | } | |
341 | ||
342 | static void handle_critical_trips(struct thermal_zone_device *tz, | |
343 | int trip, enum thermal_trip_type trip_type) | |
344 | { | |
345 | long trip_temp; | |
346 | ||
347 | tz->ops->get_trip_temp(tz, trip, &trip_temp); | |
348 | ||
349 | /* If we have not crossed the trip_temp, we do not care. */ | |
350 | if (tz->temperature < trip_temp) | |
351 | return; | |
352 | ||
353 | if (tz->ops->notify) | |
354 | tz->ops->notify(tz, trip, trip_type); | |
355 | ||
356 | if (trip_type == THERMAL_TRIP_CRITICAL) { | |
357 | pr_emerg("Critical temperature reached(%d C),shutting down\n", | |
358 | tz->temperature / 1000); | |
359 | orderly_poweroff(true); | |
360 | } | |
361 | } | |
362 | ||
363 | static void handle_thermal_trip(struct thermal_zone_device *tz, int trip) | |
364 | { | |
365 | enum thermal_trip_type type; | |
366 | ||
367 | tz->ops->get_trip_type(tz, trip, &type); | |
368 | ||
369 | if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT) | |
370 | handle_critical_trips(tz, trip, type); | |
371 | else | |
372 | handle_non_critical_trips(tz, trip, type); | |
373 | /* | |
374 | * Alright, we handled this trip successfully. | |
375 | * So, start monitoring again. | |
376 | */ | |
377 | monitor_thermal_zone(tz); | |
378 | } | |
379 | ||
380 | static void update_temperature(struct thermal_zone_device *tz) | |
381 | { | |
382 | long temp; | |
383 | int ret; | |
384 | ||
385 | mutex_lock(&tz->lock); | |
386 | ||
387 | ret = tz->ops->get_temp(tz, &temp); | |
388 | if (ret) { | |
389 | pr_warn("failed to read out thermal zone %d\n", tz->id); | |
390 | return; | |
391 | } | |
392 | ||
393 | tz->last_temperature = tz->temperature; | |
394 | tz->temperature = temp; | |
395 | ||
396 | mutex_unlock(&tz->lock); | |
397 | } | |
398 | ||
399 | void thermal_zone_device_update(struct thermal_zone_device *tz) | |
400 | { | |
401 | int count; | |
402 | ||
403 | update_temperature(tz); | |
404 | ||
405 | for (count = 0; count < tz->trips; count++) | |
406 | handle_thermal_trip(tz, count); | |
407 | } | |
408 | EXPORT_SYMBOL(thermal_zone_device_update); | |
409 | ||
410 | static void thermal_zone_device_check(struct work_struct *work) | |
411 | { | |
412 | struct thermal_zone_device *tz = container_of(work, struct | |
413 | thermal_zone_device, | |
414 | poll_queue.work); | |
415 | thermal_zone_device_update(tz); | |
416 | } | |
417 | ||
203d3d4a ZR |
418 | /* sys I/F for thermal zone */ |
419 | ||
420 | #define to_thermal_zone(_dev) \ | |
421 | container_of(_dev, struct thermal_zone_device, device) | |
422 | ||
423 | static ssize_t | |
424 | type_show(struct device *dev, struct device_attribute *attr, char *buf) | |
425 | { | |
426 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
427 | ||
428 | return sprintf(buf, "%s\n", tz->type); | |
429 | } | |
430 | ||
431 | static ssize_t | |
432 | temp_show(struct device *dev, struct device_attribute *attr, char *buf) | |
433 | { | |
434 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
6503e5df MG |
435 | long temperature; |
436 | int ret; | |
203d3d4a ZR |
437 | |
438 | if (!tz->ops->get_temp) | |
439 | return -EPERM; | |
440 | ||
6503e5df MG |
441 | ret = tz->ops->get_temp(tz, &temperature); |
442 | ||
443 | if (ret) | |
444 | return ret; | |
445 | ||
446 | return sprintf(buf, "%ld\n", temperature); | |
203d3d4a ZR |
447 | } |
448 | ||
449 | static ssize_t | |
450 | mode_show(struct device *dev, struct device_attribute *attr, char *buf) | |
451 | { | |
452 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
6503e5df MG |
453 | enum thermal_device_mode mode; |
454 | int result; | |
203d3d4a ZR |
455 | |
456 | if (!tz->ops->get_mode) | |
457 | return -EPERM; | |
458 | ||
6503e5df MG |
459 | result = tz->ops->get_mode(tz, &mode); |
460 | if (result) | |
461 | return result; | |
462 | ||
463 | return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled" | |
464 | : "disabled"); | |
203d3d4a ZR |
465 | } |
466 | ||
467 | static ssize_t | |
468 | mode_store(struct device *dev, struct device_attribute *attr, | |
469 | const char *buf, size_t count) | |
470 | { | |
471 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
472 | int result; | |
473 | ||
474 | if (!tz->ops->set_mode) | |
475 | return -EPERM; | |
476 | ||
f1f0e2ac | 477 | if (!strncmp(buf, "enabled", sizeof("enabled") - 1)) |
6503e5df | 478 | result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED); |
f1f0e2ac | 479 | else if (!strncmp(buf, "disabled", sizeof("disabled") - 1)) |
6503e5df MG |
480 | result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED); |
481 | else | |
482 | result = -EINVAL; | |
483 | ||
203d3d4a ZR |
484 | if (result) |
485 | return result; | |
486 | ||
487 | return count; | |
488 | } | |
489 | ||
490 | static ssize_t | |
491 | trip_point_type_show(struct device *dev, struct device_attribute *attr, | |
492 | char *buf) | |
493 | { | |
494 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
6503e5df MG |
495 | enum thermal_trip_type type; |
496 | int trip, result; | |
203d3d4a ZR |
497 | |
498 | if (!tz->ops->get_trip_type) | |
499 | return -EPERM; | |
500 | ||
501 | if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip)) | |
502 | return -EINVAL; | |
503 | ||
6503e5df MG |
504 | result = tz->ops->get_trip_type(tz, trip, &type); |
505 | if (result) | |
506 | return result; | |
507 | ||
508 | switch (type) { | |
509 | case THERMAL_TRIP_CRITICAL: | |
625120a4 | 510 | return sprintf(buf, "critical\n"); |
6503e5df | 511 | case THERMAL_TRIP_HOT: |
625120a4 | 512 | return sprintf(buf, "hot\n"); |
6503e5df | 513 | case THERMAL_TRIP_PASSIVE: |
625120a4 | 514 | return sprintf(buf, "passive\n"); |
6503e5df | 515 | case THERMAL_TRIP_ACTIVE: |
625120a4 | 516 | return sprintf(buf, "active\n"); |
6503e5df | 517 | default: |
625120a4 | 518 | return sprintf(buf, "unknown\n"); |
6503e5df | 519 | } |
203d3d4a ZR |
520 | } |
521 | ||
c56f5c03 D |
522 | static ssize_t |
523 | trip_point_temp_store(struct device *dev, struct device_attribute *attr, | |
524 | const char *buf, size_t count) | |
525 | { | |
526 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
527 | int trip, ret; | |
528 | unsigned long temperature; | |
529 | ||
530 | if (!tz->ops->set_trip_temp) | |
531 | return -EPERM; | |
532 | ||
533 | if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip)) | |
534 | return -EINVAL; | |
535 | ||
536 | if (kstrtoul(buf, 10, &temperature)) | |
537 | return -EINVAL; | |
538 | ||
539 | ret = tz->ops->set_trip_temp(tz, trip, temperature); | |
540 | ||
541 | return ret ? ret : count; | |
542 | } | |
543 | ||
203d3d4a ZR |
544 | static ssize_t |
545 | trip_point_temp_show(struct device *dev, struct device_attribute *attr, | |
546 | char *buf) | |
547 | { | |
548 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
6503e5df MG |
549 | int trip, ret; |
550 | long temperature; | |
203d3d4a ZR |
551 | |
552 | if (!tz->ops->get_trip_temp) | |
553 | return -EPERM; | |
554 | ||
555 | if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip)) | |
556 | return -EINVAL; | |
557 | ||
6503e5df MG |
558 | ret = tz->ops->get_trip_temp(tz, trip, &temperature); |
559 | ||
560 | if (ret) | |
561 | return ret; | |
562 | ||
563 | return sprintf(buf, "%ld\n", temperature); | |
203d3d4a ZR |
564 | } |
565 | ||
27365a6c D |
566 | static ssize_t |
567 | trip_point_hyst_store(struct device *dev, struct device_attribute *attr, | |
568 | const char *buf, size_t count) | |
569 | { | |
570 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
571 | int trip, ret; | |
572 | unsigned long temperature; | |
573 | ||
574 | if (!tz->ops->set_trip_hyst) | |
575 | return -EPERM; | |
576 | ||
577 | if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip)) | |
578 | return -EINVAL; | |
579 | ||
580 | if (kstrtoul(buf, 10, &temperature)) | |
581 | return -EINVAL; | |
582 | ||
583 | /* | |
584 | * We are not doing any check on the 'temperature' value | |
585 | * here. The driver implementing 'set_trip_hyst' has to | |
586 | * take care of this. | |
587 | */ | |
588 | ret = tz->ops->set_trip_hyst(tz, trip, temperature); | |
589 | ||
590 | return ret ? ret : count; | |
591 | } | |
592 | ||
593 | static ssize_t | |
594 | trip_point_hyst_show(struct device *dev, struct device_attribute *attr, | |
595 | char *buf) | |
596 | { | |
597 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
598 | int trip, ret; | |
599 | unsigned long temperature; | |
600 | ||
601 | if (!tz->ops->get_trip_hyst) | |
602 | return -EPERM; | |
603 | ||
604 | if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip)) | |
605 | return -EINVAL; | |
606 | ||
607 | ret = tz->ops->get_trip_hyst(tz, trip, &temperature); | |
608 | ||
609 | return ret ? ret : sprintf(buf, "%ld\n", temperature); | |
610 | } | |
611 | ||
03a971a2 MG |
612 | static ssize_t |
613 | passive_store(struct device *dev, struct device_attribute *attr, | |
614 | const char *buf, size_t count) | |
615 | { | |
616 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
617 | struct thermal_cooling_device *cdev = NULL; | |
618 | int state; | |
619 | ||
620 | if (!sscanf(buf, "%d\n", &state)) | |
621 | return -EINVAL; | |
622 | ||
3d8e3ad8 FP |
623 | /* sanity check: values below 1000 millicelcius don't make sense |
624 | * and can cause the system to go into a thermal heart attack | |
625 | */ | |
626 | if (state && state < 1000) | |
627 | return -EINVAL; | |
628 | ||
03a971a2 MG |
629 | if (state && !tz->forced_passive) { |
630 | mutex_lock(&thermal_list_lock); | |
631 | list_for_each_entry(cdev, &thermal_cdev_list, node) { | |
632 | if (!strncmp("Processor", cdev->type, | |
633 | sizeof("Processor"))) | |
634 | thermal_zone_bind_cooling_device(tz, | |
9d99842f ZR |
635 | THERMAL_TRIPS_NONE, cdev, |
636 | THERMAL_NO_LIMIT, | |
637 | THERMAL_NO_LIMIT); | |
03a971a2 MG |
638 | } |
639 | mutex_unlock(&thermal_list_lock); | |
e4143b03 FP |
640 | if (!tz->passive_delay) |
641 | tz->passive_delay = 1000; | |
03a971a2 MG |
642 | } else if (!state && tz->forced_passive) { |
643 | mutex_lock(&thermal_list_lock); | |
644 | list_for_each_entry(cdev, &thermal_cdev_list, node) { | |
645 | if (!strncmp("Processor", cdev->type, | |
646 | sizeof("Processor"))) | |
647 | thermal_zone_unbind_cooling_device(tz, | |
648 | THERMAL_TRIPS_NONE, | |
649 | cdev); | |
650 | } | |
651 | mutex_unlock(&thermal_list_lock); | |
e4143b03 | 652 | tz->passive_delay = 0; |
03a971a2 MG |
653 | } |
654 | ||
03a971a2 MG |
655 | tz->forced_passive = state; |
656 | ||
657 | thermal_zone_device_update(tz); | |
658 | ||
659 | return count; | |
660 | } | |
661 | ||
662 | static ssize_t | |
663 | passive_show(struct device *dev, struct device_attribute *attr, | |
664 | char *buf) | |
665 | { | |
666 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
667 | ||
668 | return sprintf(buf, "%d\n", tz->forced_passive); | |
669 | } | |
670 | ||
5a2c090b D |
671 | static ssize_t |
672 | policy_store(struct device *dev, struct device_attribute *attr, | |
673 | const char *buf, size_t count) | |
674 | { | |
675 | int ret = -EINVAL; | |
676 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
677 | struct thermal_governor *gov; | |
678 | ||
679 | mutex_lock(&thermal_governor_lock); | |
680 | ||
681 | gov = __find_governor(buf); | |
682 | if (!gov) | |
683 | goto exit; | |
684 | ||
685 | tz->governor = gov; | |
686 | ret = count; | |
687 | ||
688 | exit: | |
689 | mutex_unlock(&thermal_governor_lock); | |
690 | return ret; | |
691 | } | |
692 | ||
693 | static ssize_t | |
694 | policy_show(struct device *dev, struct device_attribute *devattr, char *buf) | |
695 | { | |
696 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
697 | ||
698 | return sprintf(buf, "%s\n", tz->governor->name); | |
699 | } | |
700 | ||
203d3d4a ZR |
701 | static DEVICE_ATTR(type, 0444, type_show, NULL); |
702 | static DEVICE_ATTR(temp, 0444, temp_show, NULL); | |
703 | static DEVICE_ATTR(mode, 0644, mode_show, mode_store); | |
886ee546 | 704 | static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store); |
5a2c090b | 705 | static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store); |
203d3d4a | 706 | |
203d3d4a ZR |
707 | /* sys I/F for cooling device */ |
708 | #define to_cooling_device(_dev) \ | |
709 | container_of(_dev, struct thermal_cooling_device, device) | |
710 | ||
711 | static ssize_t | |
712 | thermal_cooling_device_type_show(struct device *dev, | |
713 | struct device_attribute *attr, char *buf) | |
714 | { | |
715 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
716 | ||
717 | return sprintf(buf, "%s\n", cdev->type); | |
718 | } | |
719 | ||
720 | static ssize_t | |
721 | thermal_cooling_device_max_state_show(struct device *dev, | |
722 | struct device_attribute *attr, char *buf) | |
723 | { | |
724 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
6503e5df MG |
725 | unsigned long state; |
726 | int ret; | |
203d3d4a | 727 | |
6503e5df MG |
728 | ret = cdev->ops->get_max_state(cdev, &state); |
729 | if (ret) | |
730 | return ret; | |
731 | return sprintf(buf, "%ld\n", state); | |
203d3d4a ZR |
732 | } |
733 | ||
734 | static ssize_t | |
735 | thermal_cooling_device_cur_state_show(struct device *dev, | |
736 | struct device_attribute *attr, char *buf) | |
737 | { | |
738 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
6503e5df MG |
739 | unsigned long state; |
740 | int ret; | |
203d3d4a | 741 | |
6503e5df MG |
742 | ret = cdev->ops->get_cur_state(cdev, &state); |
743 | if (ret) | |
744 | return ret; | |
745 | return sprintf(buf, "%ld\n", state); | |
203d3d4a ZR |
746 | } |
747 | ||
748 | static ssize_t | |
749 | thermal_cooling_device_cur_state_store(struct device *dev, | |
750 | struct device_attribute *attr, | |
751 | const char *buf, size_t count) | |
752 | { | |
753 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
6503e5df | 754 | unsigned long state; |
203d3d4a ZR |
755 | int result; |
756 | ||
6503e5df | 757 | if (!sscanf(buf, "%ld\n", &state)) |
203d3d4a ZR |
758 | return -EINVAL; |
759 | ||
edb94918 | 760 | if ((long)state < 0) |
203d3d4a ZR |
761 | return -EINVAL; |
762 | ||
763 | result = cdev->ops->set_cur_state(cdev, state); | |
764 | if (result) | |
765 | return result; | |
766 | return count; | |
767 | } | |
768 | ||
769 | static struct device_attribute dev_attr_cdev_type = | |
543a9561 | 770 | __ATTR(type, 0444, thermal_cooling_device_type_show, NULL); |
203d3d4a ZR |
771 | static DEVICE_ATTR(max_state, 0444, |
772 | thermal_cooling_device_max_state_show, NULL); | |
773 | static DEVICE_ATTR(cur_state, 0644, | |
774 | thermal_cooling_device_cur_state_show, | |
775 | thermal_cooling_device_cur_state_store); | |
776 | ||
777 | static ssize_t | |
778 | thermal_cooling_device_trip_point_show(struct device *dev, | |
543a9561 | 779 | struct device_attribute *attr, char *buf) |
203d3d4a | 780 | { |
b81b6ba3 | 781 | struct thermal_instance *instance; |
203d3d4a ZR |
782 | |
783 | instance = | |
b81b6ba3 | 784 | container_of(attr, struct thermal_instance, attr); |
203d3d4a ZR |
785 | |
786 | if (instance->trip == THERMAL_TRIPS_NONE) | |
787 | return sprintf(buf, "-1\n"); | |
788 | else | |
789 | return sprintf(buf, "%d\n", instance->trip); | |
790 | } | |
791 | ||
792 | /* Device management */ | |
793 | ||
16d75239 RH |
794 | #if defined(CONFIG_THERMAL_HWMON) |
795 | ||
e68b16ab ZR |
796 | /* hwmon sys I/F */ |
797 | #include <linux/hwmon.h> | |
31f5396a JD |
798 | |
799 | /* thermal zone devices with the same type share one hwmon device */ | |
800 | struct thermal_hwmon_device { | |
801 | char type[THERMAL_NAME_LENGTH]; | |
802 | struct device *device; | |
803 | int count; | |
804 | struct list_head tz_list; | |
805 | struct list_head node; | |
806 | }; | |
807 | ||
808 | struct thermal_hwmon_attr { | |
809 | struct device_attribute attr; | |
810 | char name[16]; | |
811 | }; | |
812 | ||
813 | /* one temperature input for each thermal zone */ | |
814 | struct thermal_hwmon_temp { | |
815 | struct list_head hwmon_node; | |
816 | struct thermal_zone_device *tz; | |
817 | struct thermal_hwmon_attr temp_input; /* hwmon sys attr */ | |
818 | struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */ | |
819 | }; | |
820 | ||
e68b16ab ZR |
821 | static LIST_HEAD(thermal_hwmon_list); |
822 | ||
823 | static ssize_t | |
824 | name_show(struct device *dev, struct device_attribute *attr, char *buf) | |
825 | { | |
0e968a3b | 826 | struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev); |
e68b16ab ZR |
827 | return sprintf(buf, "%s\n", hwmon->type); |
828 | } | |
829 | static DEVICE_ATTR(name, 0444, name_show, NULL); | |
830 | ||
831 | static ssize_t | |
832 | temp_input_show(struct device *dev, struct device_attribute *attr, char *buf) | |
833 | { | |
6503e5df MG |
834 | long temperature; |
835 | int ret; | |
e68b16ab ZR |
836 | struct thermal_hwmon_attr *hwmon_attr |
837 | = container_of(attr, struct thermal_hwmon_attr, attr); | |
31f5396a JD |
838 | struct thermal_hwmon_temp *temp |
839 | = container_of(hwmon_attr, struct thermal_hwmon_temp, | |
e68b16ab | 840 | temp_input); |
31f5396a | 841 | struct thermal_zone_device *tz = temp->tz; |
e68b16ab | 842 | |
6503e5df MG |
843 | ret = tz->ops->get_temp(tz, &temperature); |
844 | ||
845 | if (ret) | |
846 | return ret; | |
847 | ||
848 | return sprintf(buf, "%ld\n", temperature); | |
e68b16ab ZR |
849 | } |
850 | ||
851 | static ssize_t | |
852 | temp_crit_show(struct device *dev, struct device_attribute *attr, | |
853 | char *buf) | |
854 | { | |
855 | struct thermal_hwmon_attr *hwmon_attr | |
856 | = container_of(attr, struct thermal_hwmon_attr, attr); | |
31f5396a JD |
857 | struct thermal_hwmon_temp *temp |
858 | = container_of(hwmon_attr, struct thermal_hwmon_temp, | |
e68b16ab | 859 | temp_crit); |
31f5396a | 860 | struct thermal_zone_device *tz = temp->tz; |
6503e5df MG |
861 | long temperature; |
862 | int ret; | |
863 | ||
864 | ret = tz->ops->get_trip_temp(tz, 0, &temperature); | |
865 | if (ret) | |
866 | return ret; | |
e68b16ab | 867 | |
6503e5df | 868 | return sprintf(buf, "%ld\n", temperature); |
e68b16ab ZR |
869 | } |
870 | ||
871 | ||
0d97d7a4 JD |
872 | static struct thermal_hwmon_device * |
873 | thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz) | |
e68b16ab ZR |
874 | { |
875 | struct thermal_hwmon_device *hwmon; | |
e68b16ab ZR |
876 | |
877 | mutex_lock(&thermal_list_lock); | |
878 | list_for_each_entry(hwmon, &thermal_hwmon_list, node) | |
879 | if (!strcmp(hwmon->type, tz->type)) { | |
e68b16ab | 880 | mutex_unlock(&thermal_list_lock); |
0d97d7a4 | 881 | return hwmon; |
e68b16ab ZR |
882 | } |
883 | mutex_unlock(&thermal_list_lock); | |
884 | ||
0d97d7a4 JD |
885 | return NULL; |
886 | } | |
887 | ||
31f5396a JD |
888 | /* Find the temperature input matching a given thermal zone */ |
889 | static struct thermal_hwmon_temp * | |
890 | thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon, | |
891 | const struct thermal_zone_device *tz) | |
892 | { | |
893 | struct thermal_hwmon_temp *temp; | |
894 | ||
895 | mutex_lock(&thermal_list_lock); | |
896 | list_for_each_entry(temp, &hwmon->tz_list, hwmon_node) | |
897 | if (temp->tz == tz) { | |
898 | mutex_unlock(&thermal_list_lock); | |
899 | return temp; | |
900 | } | |
901 | mutex_unlock(&thermal_list_lock); | |
902 | ||
903 | return NULL; | |
904 | } | |
905 | ||
0d97d7a4 JD |
906 | static int |
907 | thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) | |
908 | { | |
909 | struct thermal_hwmon_device *hwmon; | |
31f5396a | 910 | struct thermal_hwmon_temp *temp; |
0d97d7a4 JD |
911 | int new_hwmon_device = 1; |
912 | int result; | |
913 | ||
914 | hwmon = thermal_hwmon_lookup_by_type(tz); | |
915 | if (hwmon) { | |
916 | new_hwmon_device = 0; | |
917 | goto register_sys_interface; | |
918 | } | |
919 | ||
e68b16ab ZR |
920 | hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL); |
921 | if (!hwmon) | |
922 | return -ENOMEM; | |
923 | ||
924 | INIT_LIST_HEAD(&hwmon->tz_list); | |
925 | strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH); | |
926 | hwmon->device = hwmon_device_register(NULL); | |
927 | if (IS_ERR(hwmon->device)) { | |
928 | result = PTR_ERR(hwmon->device); | |
929 | goto free_mem; | |
930 | } | |
0e968a3b | 931 | dev_set_drvdata(hwmon->device, hwmon); |
e68b16ab ZR |
932 | result = device_create_file(hwmon->device, &dev_attr_name); |
933 | if (result) | |
b299eb5c | 934 | goto free_mem; |
e68b16ab ZR |
935 | |
936 | register_sys_interface: | |
31f5396a JD |
937 | temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL); |
938 | if (!temp) { | |
939 | result = -ENOMEM; | |
940 | goto unregister_name; | |
941 | } | |
942 | ||
943 | temp->tz = tz; | |
e68b16ab ZR |
944 | hwmon->count++; |
945 | ||
79a49168 | 946 | snprintf(temp->temp_input.name, sizeof(temp->temp_input.name), |
e68b16ab | 947 | "temp%d_input", hwmon->count); |
31f5396a JD |
948 | temp->temp_input.attr.attr.name = temp->temp_input.name; |
949 | temp->temp_input.attr.attr.mode = 0444; | |
950 | temp->temp_input.attr.show = temp_input_show; | |
951 | sysfs_attr_init(&temp->temp_input.attr.attr); | |
952 | result = device_create_file(hwmon->device, &temp->temp_input.attr); | |
e68b16ab | 953 | if (result) |
31f5396a | 954 | goto free_temp_mem; |
e68b16ab ZR |
955 | |
956 | if (tz->ops->get_crit_temp) { | |
957 | unsigned long temperature; | |
958 | if (!tz->ops->get_crit_temp(tz, &temperature)) { | |
79a49168 GR |
959 | snprintf(temp->temp_crit.name, |
960 | sizeof(temp->temp_crit.name), | |
e68b16ab | 961 | "temp%d_crit", hwmon->count); |
31f5396a JD |
962 | temp->temp_crit.attr.attr.name = temp->temp_crit.name; |
963 | temp->temp_crit.attr.attr.mode = 0444; | |
964 | temp->temp_crit.attr.show = temp_crit_show; | |
965 | sysfs_attr_init(&temp->temp_crit.attr.attr); | |
e68b16ab | 966 | result = device_create_file(hwmon->device, |
31f5396a | 967 | &temp->temp_crit.attr); |
e68b16ab | 968 | if (result) |
b299eb5c | 969 | goto unregister_input; |
e68b16ab ZR |
970 | } |
971 | } | |
972 | ||
973 | mutex_lock(&thermal_list_lock); | |
974 | if (new_hwmon_device) | |
975 | list_add_tail(&hwmon->node, &thermal_hwmon_list); | |
31f5396a | 976 | list_add_tail(&temp->hwmon_node, &hwmon->tz_list); |
e68b16ab ZR |
977 | mutex_unlock(&thermal_list_lock); |
978 | ||
979 | return 0; | |
980 | ||
b299eb5c | 981 | unregister_input: |
31f5396a JD |
982 | device_remove_file(hwmon->device, &temp->temp_input.attr); |
983 | free_temp_mem: | |
984 | kfree(temp); | |
b299eb5c | 985 | unregister_name: |
e68b16ab ZR |
986 | if (new_hwmon_device) { |
987 | device_remove_file(hwmon->device, &dev_attr_name); | |
988 | hwmon_device_unregister(hwmon->device); | |
989 | } | |
990 | free_mem: | |
991 | if (new_hwmon_device) | |
992 | kfree(hwmon); | |
993 | ||
994 | return result; | |
995 | } | |
996 | ||
997 | static void | |
998 | thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz) | |
999 | { | |
31f5396a JD |
1000 | struct thermal_hwmon_device *hwmon; |
1001 | struct thermal_hwmon_temp *temp; | |
1002 | ||
1003 | hwmon = thermal_hwmon_lookup_by_type(tz); | |
1004 | if (unlikely(!hwmon)) { | |
1005 | /* Should never happen... */ | |
1006 | dev_dbg(&tz->device, "hwmon device lookup failed!\n"); | |
1007 | return; | |
1008 | } | |
1009 | ||
1010 | temp = thermal_hwmon_lookup_temp(hwmon, tz); | |
1011 | if (unlikely(!temp)) { | |
1012 | /* Should never happen... */ | |
1013 | dev_dbg(&tz->device, "temperature input lookup failed!\n"); | |
1014 | return; | |
1015 | } | |
e68b16ab | 1016 | |
31f5396a | 1017 | device_remove_file(hwmon->device, &temp->temp_input.attr); |
b299eb5c | 1018 | if (tz->ops->get_crit_temp) |
31f5396a | 1019 | device_remove_file(hwmon->device, &temp->temp_crit.attr); |
e68b16ab ZR |
1020 | |
1021 | mutex_lock(&thermal_list_lock); | |
31f5396a JD |
1022 | list_del(&temp->hwmon_node); |
1023 | kfree(temp); | |
e68b16ab ZR |
1024 | if (!list_empty(&hwmon->tz_list)) { |
1025 | mutex_unlock(&thermal_list_lock); | |
1026 | return; | |
1027 | } | |
1028 | list_del(&hwmon->node); | |
1029 | mutex_unlock(&thermal_list_lock); | |
1030 | ||
1031 | device_remove_file(hwmon->device, &dev_attr_name); | |
1032 | hwmon_device_unregister(hwmon->device); | |
1033 | kfree(hwmon); | |
1034 | } | |
1035 | #else | |
1036 | static int | |
1037 | thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) | |
1038 | { | |
1039 | return 0; | |
1040 | } | |
1041 | ||
1042 | static void | |
1043 | thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz) | |
1044 | { | |
1045 | } | |
1046 | #endif | |
1047 | ||
203d3d4a ZR |
1048 | /** |
1049 | * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone | |
203d3d4a ZR |
1050 | * @tz: thermal zone device |
1051 | * @trip: indicates which trip point the cooling devices is | |
1052 | * associated with in this thermal zone. | |
1053 | * @cdev: thermal cooling device | |
543a9561 LB |
1054 | * |
1055 | * This function is usually called in the thermal zone device .bind callback. | |
203d3d4a ZR |
1056 | */ |
1057 | int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, | |
1058 | int trip, | |
9d99842f ZR |
1059 | struct thermal_cooling_device *cdev, |
1060 | unsigned long upper, unsigned long lower) | |
203d3d4a | 1061 | { |
b81b6ba3 ZR |
1062 | struct thermal_instance *dev; |
1063 | struct thermal_instance *pos; | |
c7516709 TS |
1064 | struct thermal_zone_device *pos1; |
1065 | struct thermal_cooling_device *pos2; | |
74051ba5 | 1066 | unsigned long max_state; |
203d3d4a ZR |
1067 | int result; |
1068 | ||
543a9561 | 1069 | if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE)) |
203d3d4a ZR |
1070 | return -EINVAL; |
1071 | ||
c7516709 TS |
1072 | list_for_each_entry(pos1, &thermal_tz_list, node) { |
1073 | if (pos1 == tz) | |
1074 | break; | |
1075 | } | |
1076 | list_for_each_entry(pos2, &thermal_cdev_list, node) { | |
1077 | if (pos2 == cdev) | |
1078 | break; | |
1079 | } | |
1080 | ||
1081 | if (tz != pos1 || cdev != pos2) | |
203d3d4a ZR |
1082 | return -EINVAL; |
1083 | ||
9d99842f ZR |
1084 | cdev->ops->get_max_state(cdev, &max_state); |
1085 | ||
1086 | /* lower default 0, upper default max_state */ | |
1087 | lower = lower == THERMAL_NO_LIMIT ? 0 : lower; | |
1088 | upper = upper == THERMAL_NO_LIMIT ? max_state : upper; | |
1089 | ||
1090 | if (lower > upper || upper > max_state) | |
1091 | return -EINVAL; | |
1092 | ||
203d3d4a | 1093 | dev = |
b81b6ba3 | 1094 | kzalloc(sizeof(struct thermal_instance), GFP_KERNEL); |
203d3d4a ZR |
1095 | if (!dev) |
1096 | return -ENOMEM; | |
1097 | dev->tz = tz; | |
1098 | dev->cdev = cdev; | |
1099 | dev->trip = trip; | |
9d99842f ZR |
1100 | dev->upper = upper; |
1101 | dev->lower = lower; | |
ce119f83 | 1102 | dev->target = THERMAL_NO_TARGET; |
74051ba5 | 1103 | |
203d3d4a ZR |
1104 | result = get_idr(&tz->idr, &tz->lock, &dev->id); |
1105 | if (result) | |
1106 | goto free_mem; | |
1107 | ||
1108 | sprintf(dev->name, "cdev%d", dev->id); | |
1109 | result = | |
1110 | sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name); | |
1111 | if (result) | |
1112 | goto release_idr; | |
1113 | ||
1114 | sprintf(dev->attr_name, "cdev%d_trip_point", dev->id); | |
975f8c56 | 1115 | sysfs_attr_init(&dev->attr.attr); |
203d3d4a ZR |
1116 | dev->attr.attr.name = dev->attr_name; |
1117 | dev->attr.attr.mode = 0444; | |
1118 | dev->attr.show = thermal_cooling_device_trip_point_show; | |
1119 | result = device_create_file(&tz->device, &dev->attr); | |
1120 | if (result) | |
1121 | goto remove_symbol_link; | |
1122 | ||
1123 | mutex_lock(&tz->lock); | |
f4a821ce | 1124 | mutex_lock(&cdev->lock); |
cddf31b3 | 1125 | list_for_each_entry(pos, &tz->thermal_instances, tz_node) |
203d3d4a ZR |
1126 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
1127 | result = -EEXIST; | |
1128 | break; | |
1129 | } | |
b5e4ae62 | 1130 | if (!result) { |
cddf31b3 | 1131 | list_add_tail(&dev->tz_node, &tz->thermal_instances); |
b5e4ae62 ZR |
1132 | list_add_tail(&dev->cdev_node, &cdev->thermal_instances); |
1133 | } | |
f4a821ce | 1134 | mutex_unlock(&cdev->lock); |
203d3d4a ZR |
1135 | mutex_unlock(&tz->lock); |
1136 | ||
1137 | if (!result) | |
1138 | return 0; | |
1139 | ||
1140 | device_remove_file(&tz->device, &dev->attr); | |
caca8b80 | 1141 | remove_symbol_link: |
203d3d4a | 1142 | sysfs_remove_link(&tz->device.kobj, dev->name); |
caca8b80 | 1143 | release_idr: |
203d3d4a | 1144 | release_idr(&tz->idr, &tz->lock, dev->id); |
caca8b80 | 1145 | free_mem: |
203d3d4a ZR |
1146 | kfree(dev); |
1147 | return result; | |
1148 | } | |
1149 | EXPORT_SYMBOL(thermal_zone_bind_cooling_device); | |
1150 | ||
1151 | /** | |
1152 | * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone | |
203d3d4a ZR |
1153 | * @tz: thermal zone device |
1154 | * @trip: indicates which trip point the cooling devices is | |
1155 | * associated with in this thermal zone. | |
1156 | * @cdev: thermal cooling device | |
543a9561 LB |
1157 | * |
1158 | * This function is usually called in the thermal zone device .unbind callback. | |
203d3d4a ZR |
1159 | */ |
1160 | int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz, | |
1161 | int trip, | |
1162 | struct thermal_cooling_device *cdev) | |
1163 | { | |
b81b6ba3 | 1164 | struct thermal_instance *pos, *next; |
203d3d4a ZR |
1165 | |
1166 | mutex_lock(&tz->lock); | |
f4a821ce | 1167 | mutex_lock(&cdev->lock); |
cddf31b3 | 1168 | list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) { |
543a9561 | 1169 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
cddf31b3 | 1170 | list_del(&pos->tz_node); |
b5e4ae62 | 1171 | list_del(&pos->cdev_node); |
f4a821ce | 1172 | mutex_unlock(&cdev->lock); |
203d3d4a ZR |
1173 | mutex_unlock(&tz->lock); |
1174 | goto unbind; | |
1175 | } | |
1176 | } | |
f4a821ce | 1177 | mutex_unlock(&cdev->lock); |
203d3d4a ZR |
1178 | mutex_unlock(&tz->lock); |
1179 | ||
1180 | return -ENODEV; | |
1181 | ||
caca8b80 | 1182 | unbind: |
203d3d4a ZR |
1183 | device_remove_file(&tz->device, &pos->attr); |
1184 | sysfs_remove_link(&tz->device.kobj, pos->name); | |
1185 | release_idr(&tz->idr, &tz->lock, pos->id); | |
1186 | kfree(pos); | |
1187 | return 0; | |
1188 | } | |
1189 | EXPORT_SYMBOL(thermal_zone_unbind_cooling_device); | |
1190 | ||
1191 | static void thermal_release(struct device *dev) | |
1192 | { | |
1193 | struct thermal_zone_device *tz; | |
1194 | struct thermal_cooling_device *cdev; | |
1195 | ||
caca8b80 JP |
1196 | if (!strncmp(dev_name(dev), "thermal_zone", |
1197 | sizeof("thermal_zone") - 1)) { | |
203d3d4a ZR |
1198 | tz = to_thermal_zone(dev); |
1199 | kfree(tz); | |
1200 | } else { | |
1201 | cdev = to_cooling_device(dev); | |
1202 | kfree(cdev); | |
1203 | } | |
1204 | } | |
1205 | ||
1206 | static struct class thermal_class = { | |
1207 | .name = "thermal", | |
1208 | .dev_release = thermal_release, | |
1209 | }; | |
1210 | ||
1211 | /** | |
1212 | * thermal_cooling_device_register - register a new thermal cooling device | |
1213 | * @type: the thermal cooling device type. | |
1214 | * @devdata: device private data. | |
1215 | * @ops: standard thermal cooling devices callbacks. | |
1216 | */ | |
caca8b80 JP |
1217 | struct thermal_cooling_device * |
1218 | thermal_cooling_device_register(char *type, void *devdata, | |
1219 | const struct thermal_cooling_device_ops *ops) | |
203d3d4a ZR |
1220 | { |
1221 | struct thermal_cooling_device *cdev; | |
203d3d4a ZR |
1222 | int result; |
1223 | ||
204dd1d3 | 1224 | if (type && strlen(type) >= THERMAL_NAME_LENGTH) |
3e6fda5c | 1225 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
1226 | |
1227 | if (!ops || !ops->get_max_state || !ops->get_cur_state || | |
543a9561 | 1228 | !ops->set_cur_state) |
3e6fda5c | 1229 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
1230 | |
1231 | cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL); | |
1232 | if (!cdev) | |
3e6fda5c | 1233 | return ERR_PTR(-ENOMEM); |
203d3d4a ZR |
1234 | |
1235 | result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id); | |
1236 | if (result) { | |
1237 | kfree(cdev); | |
3e6fda5c | 1238 | return ERR_PTR(result); |
203d3d4a ZR |
1239 | } |
1240 | ||
204dd1d3 | 1241 | strcpy(cdev->type, type ? : ""); |
f4a821ce | 1242 | mutex_init(&cdev->lock); |
b5e4ae62 | 1243 | INIT_LIST_HEAD(&cdev->thermal_instances); |
203d3d4a | 1244 | cdev->ops = ops; |
ce119f83 | 1245 | cdev->updated = true; |
203d3d4a ZR |
1246 | cdev->device.class = &thermal_class; |
1247 | cdev->devdata = devdata; | |
354655ea | 1248 | dev_set_name(&cdev->device, "cooling_device%d", cdev->id); |
203d3d4a ZR |
1249 | result = device_register(&cdev->device); |
1250 | if (result) { | |
1251 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); | |
1252 | kfree(cdev); | |
3e6fda5c | 1253 | return ERR_PTR(result); |
203d3d4a ZR |
1254 | } |
1255 | ||
1256 | /* sys I/F */ | |
1257 | if (type) { | |
543a9561 | 1258 | result = device_create_file(&cdev->device, &dev_attr_cdev_type); |
203d3d4a ZR |
1259 | if (result) |
1260 | goto unregister; | |
1261 | } | |
1262 | ||
1263 | result = device_create_file(&cdev->device, &dev_attr_max_state); | |
1264 | if (result) | |
1265 | goto unregister; | |
1266 | ||
1267 | result = device_create_file(&cdev->device, &dev_attr_cur_state); | |
1268 | if (result) | |
1269 | goto unregister; | |
1270 | ||
7e8ee1e9 | 1271 | /* Add 'this' new cdev to the global cdev list */ |
203d3d4a ZR |
1272 | mutex_lock(&thermal_list_lock); |
1273 | list_add(&cdev->node, &thermal_cdev_list); | |
203d3d4a ZR |
1274 | mutex_unlock(&thermal_list_lock); |
1275 | ||
7e8ee1e9 D |
1276 | /* Update binding information for 'this' new cdev */ |
1277 | bind_cdev(cdev); | |
1278 | ||
1279 | return cdev; | |
203d3d4a | 1280 | |
caca8b80 | 1281 | unregister: |
203d3d4a ZR |
1282 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); |
1283 | device_unregister(&cdev->device); | |
3e6fda5c | 1284 | return ERR_PTR(result); |
203d3d4a ZR |
1285 | } |
1286 | EXPORT_SYMBOL(thermal_cooling_device_register); | |
1287 | ||
1288 | /** | |
1289 | * thermal_cooling_device_unregister - removes the registered thermal cooling device | |
203d3d4a ZR |
1290 | * @cdev: the thermal cooling device to remove. |
1291 | * | |
1292 | * thermal_cooling_device_unregister() must be called when the device is no | |
1293 | * longer needed. | |
1294 | */ | |
7e8ee1e9 | 1295 | void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev) |
203d3d4a | 1296 | { |
7e8ee1e9 D |
1297 | int i; |
1298 | const struct thermal_zone_params *tzp; | |
203d3d4a ZR |
1299 | struct thermal_zone_device *tz; |
1300 | struct thermal_cooling_device *pos = NULL; | |
1301 | ||
1302 | if (!cdev) | |
1303 | return; | |
1304 | ||
1305 | mutex_lock(&thermal_list_lock); | |
1306 | list_for_each_entry(pos, &thermal_cdev_list, node) | |
1307 | if (pos == cdev) | |
1308 | break; | |
1309 | if (pos != cdev) { | |
1310 | /* thermal cooling device not found */ | |
1311 | mutex_unlock(&thermal_list_lock); | |
1312 | return; | |
1313 | } | |
1314 | list_del(&cdev->node); | |
7e8ee1e9 D |
1315 | |
1316 | /* Unbind all thermal zones associated with 'this' cdev */ | |
203d3d4a | 1317 | list_for_each_entry(tz, &thermal_tz_list, node) { |
7e8ee1e9 D |
1318 | if (tz->ops->unbind) { |
1319 | tz->ops->unbind(tz, cdev); | |
1320 | continue; | |
1321 | } | |
1322 | ||
1323 | if (!tz->tzp || !tz->tzp->tbp) | |
203d3d4a | 1324 | continue; |
7e8ee1e9 D |
1325 | |
1326 | tzp = tz->tzp; | |
1327 | for (i = 0; i < tzp->num_tbps; i++) { | |
1328 | if (tzp->tbp[i].cdev == cdev) { | |
1329 | __unbind(tz, tzp->tbp[i].trip_mask, cdev); | |
1330 | tzp->tbp[i].cdev = NULL; | |
1331 | } | |
1332 | } | |
203d3d4a | 1333 | } |
7e8ee1e9 | 1334 | |
203d3d4a | 1335 | mutex_unlock(&thermal_list_lock); |
7e8ee1e9 | 1336 | |
203d3d4a | 1337 | if (cdev->type[0]) |
543a9561 | 1338 | device_remove_file(&cdev->device, &dev_attr_cdev_type); |
203d3d4a ZR |
1339 | device_remove_file(&cdev->device, &dev_attr_max_state); |
1340 | device_remove_file(&cdev->device, &dev_attr_cur_state); | |
1341 | ||
1342 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); | |
1343 | device_unregister(&cdev->device); | |
1344 | return; | |
1345 | } | |
1346 | EXPORT_SYMBOL(thermal_cooling_device_unregister); | |
1347 | ||
dc765482 | 1348 | void thermal_cdev_update(struct thermal_cooling_device *cdev) |
ce119f83 ZR |
1349 | { |
1350 | struct thermal_instance *instance; | |
1351 | unsigned long target = 0; | |
1352 | ||
1353 | /* cooling device is updated*/ | |
1354 | if (cdev->updated) | |
1355 | return; | |
1356 | ||
f4a821ce | 1357 | mutex_lock(&cdev->lock); |
ce119f83 ZR |
1358 | /* Make sure cdev enters the deepest cooling state */ |
1359 | list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) { | |
1360 | if (instance->target == THERMAL_NO_TARGET) | |
1361 | continue; | |
1362 | if (instance->target > target) | |
1363 | target = instance->target; | |
1364 | } | |
f4a821ce | 1365 | mutex_unlock(&cdev->lock); |
ce119f83 ZR |
1366 | cdev->ops->set_cur_state(cdev, target); |
1367 | cdev->updated = true; | |
1368 | } | |
dc765482 | 1369 | EXPORT_SYMBOL(thermal_cdev_update); |
ce119f83 | 1370 | |
f2b4caaf D |
1371 | /** |
1372 | * notify_thermal_framework - Sensor drivers use this API to notify framework | |
1373 | * @tz: thermal zone device | |
1374 | * @trip: indicates which trip point has been crossed | |
1375 | * | |
1376 | * This function handles the trip events from sensor drivers. It starts | |
1377 | * throttling the cooling devices according to the policy configured. | |
1378 | * For CRITICAL and HOT trip points, this notifies the respective drivers, | |
1379 | * and does actual throttling for other trip points i.e ACTIVE and PASSIVE. | |
1380 | * The throttling policy is based on the configured platform data; if no | |
1381 | * platform data is provided, this uses the step_wise throttling policy. | |
1382 | */ | |
1383 | void notify_thermal_framework(struct thermal_zone_device *tz, int trip) | |
1384 | { | |
1385 | handle_thermal_trip(tz, trip); | |
1386 | } | |
1387 | EXPORT_SYMBOL(notify_thermal_framework); | |
1388 | ||
c56f5c03 D |
1389 | /** |
1390 | * create_trip_attrs - create attributes for trip points | |
1391 | * @tz: the thermal zone device | |
1392 | * @mask: Writeable trip point bitmap. | |
1393 | */ | |
1394 | static int create_trip_attrs(struct thermal_zone_device *tz, int mask) | |
1395 | { | |
1396 | int indx; | |
27365a6c | 1397 | int size = sizeof(struct thermal_attr) * tz->trips; |
c56f5c03 | 1398 | |
27365a6c | 1399 | tz->trip_type_attrs = kzalloc(size, GFP_KERNEL); |
c56f5c03 D |
1400 | if (!tz->trip_type_attrs) |
1401 | return -ENOMEM; | |
1402 | ||
27365a6c | 1403 | tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL); |
c56f5c03 D |
1404 | if (!tz->trip_temp_attrs) { |
1405 | kfree(tz->trip_type_attrs); | |
1406 | return -ENOMEM; | |
1407 | } | |
1408 | ||
27365a6c D |
1409 | if (tz->ops->get_trip_hyst) { |
1410 | tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL); | |
1411 | if (!tz->trip_hyst_attrs) { | |
1412 | kfree(tz->trip_type_attrs); | |
1413 | kfree(tz->trip_temp_attrs); | |
1414 | return -ENOMEM; | |
1415 | } | |
1416 | } | |
c56f5c03 | 1417 | |
27365a6c D |
1418 | |
1419 | for (indx = 0; indx < tz->trips; indx++) { | |
c56f5c03 D |
1420 | /* create trip type attribute */ |
1421 | snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH, | |
1422 | "trip_point_%d_type", indx); | |
1423 | ||
1424 | sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr); | |
1425 | tz->trip_type_attrs[indx].attr.attr.name = | |
1426 | tz->trip_type_attrs[indx].name; | |
1427 | tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO; | |
1428 | tz->trip_type_attrs[indx].attr.show = trip_point_type_show; | |
1429 | ||
1430 | device_create_file(&tz->device, | |
1431 | &tz->trip_type_attrs[indx].attr); | |
1432 | ||
1433 | /* create trip temp attribute */ | |
1434 | snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH, | |
1435 | "trip_point_%d_temp", indx); | |
1436 | ||
1437 | sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr); | |
1438 | tz->trip_temp_attrs[indx].attr.attr.name = | |
1439 | tz->trip_temp_attrs[indx].name; | |
1440 | tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO; | |
1441 | tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show; | |
1442 | if (mask & (1 << indx)) { | |
1443 | tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR; | |
1444 | tz->trip_temp_attrs[indx].attr.store = | |
1445 | trip_point_temp_store; | |
1446 | } | |
1447 | ||
1448 | device_create_file(&tz->device, | |
1449 | &tz->trip_temp_attrs[indx].attr); | |
27365a6c D |
1450 | |
1451 | /* create Optional trip hyst attribute */ | |
1452 | if (!tz->ops->get_trip_hyst) | |
1453 | continue; | |
1454 | snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH, | |
1455 | "trip_point_%d_hyst", indx); | |
1456 | ||
1457 | sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr); | |
1458 | tz->trip_hyst_attrs[indx].attr.attr.name = | |
1459 | tz->trip_hyst_attrs[indx].name; | |
1460 | tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO; | |
1461 | tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show; | |
1462 | if (tz->ops->set_trip_hyst) { | |
1463 | tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR; | |
1464 | tz->trip_hyst_attrs[indx].attr.store = | |
1465 | trip_point_hyst_store; | |
1466 | } | |
1467 | ||
1468 | device_create_file(&tz->device, | |
1469 | &tz->trip_hyst_attrs[indx].attr); | |
c56f5c03 D |
1470 | } |
1471 | return 0; | |
1472 | } | |
1473 | ||
1474 | static void remove_trip_attrs(struct thermal_zone_device *tz) | |
1475 | { | |
1476 | int indx; | |
1477 | ||
1478 | for (indx = 0; indx < tz->trips; indx++) { | |
1479 | device_remove_file(&tz->device, | |
1480 | &tz->trip_type_attrs[indx].attr); | |
1481 | device_remove_file(&tz->device, | |
1482 | &tz->trip_temp_attrs[indx].attr); | |
27365a6c D |
1483 | if (tz->ops->get_trip_hyst) |
1484 | device_remove_file(&tz->device, | |
1485 | &tz->trip_hyst_attrs[indx].attr); | |
c56f5c03 D |
1486 | } |
1487 | kfree(tz->trip_type_attrs); | |
1488 | kfree(tz->trip_temp_attrs); | |
27365a6c | 1489 | kfree(tz->trip_hyst_attrs); |
c56f5c03 D |
1490 | } |
1491 | ||
203d3d4a ZR |
1492 | /** |
1493 | * thermal_zone_device_register - register a new thermal zone device | |
1494 | * @type: the thermal zone device type | |
1495 | * @trips: the number of trip points the thermal zone support | |
c56f5c03 | 1496 | * @mask: a bit string indicating the writeablility of trip points |
203d3d4a ZR |
1497 | * @devdata: private device data |
1498 | * @ops: standard thermal zone device callbacks | |
50125a9b | 1499 | * @tzp: thermal zone platform parameters |
b1569e99 MG |
1500 | * @passive_delay: number of milliseconds to wait between polls when |
1501 | * performing passive cooling | |
1502 | * @polling_delay: number of milliseconds to wait between polls when checking | |
1503 | * whether trip points have been crossed (0 for interrupt | |
1504 | * driven systems) | |
203d3d4a ZR |
1505 | * |
1506 | * thermal_zone_device_unregister() must be called when the device is no | |
1b7ddb84 | 1507 | * longer needed. The passive cooling depends on the .get_trend() return value. |
203d3d4a | 1508 | */ |
4b1bf587 | 1509 | struct thermal_zone_device *thermal_zone_device_register(const char *type, |
c56f5c03 | 1510 | int trips, int mask, void *devdata, |
5b275ce2 | 1511 | const struct thermal_zone_device_ops *ops, |
50125a9b | 1512 | const struct thermal_zone_params *tzp, |
1b7ddb84 | 1513 | int passive_delay, int polling_delay) |
203d3d4a ZR |
1514 | { |
1515 | struct thermal_zone_device *tz; | |
03a971a2 | 1516 | enum thermal_trip_type trip_type; |
203d3d4a ZR |
1517 | int result; |
1518 | int count; | |
03a971a2 | 1519 | int passive = 0; |
203d3d4a | 1520 | |
204dd1d3 | 1521 | if (type && strlen(type) >= THERMAL_NAME_LENGTH) |
3e6fda5c | 1522 | return ERR_PTR(-EINVAL); |
203d3d4a | 1523 | |
c56f5c03 | 1524 | if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips) |
3e6fda5c | 1525 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
1526 | |
1527 | if (!ops || !ops->get_temp) | |
3e6fda5c | 1528 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
1529 | |
1530 | tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL); | |
1531 | if (!tz) | |
3e6fda5c | 1532 | return ERR_PTR(-ENOMEM); |
203d3d4a | 1533 | |
2d374139 | 1534 | INIT_LIST_HEAD(&tz->thermal_instances); |
203d3d4a ZR |
1535 | idr_init(&tz->idr); |
1536 | mutex_init(&tz->lock); | |
1537 | result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id); | |
1538 | if (result) { | |
1539 | kfree(tz); | |
3e6fda5c | 1540 | return ERR_PTR(result); |
203d3d4a ZR |
1541 | } |
1542 | ||
204dd1d3 | 1543 | strcpy(tz->type, type ? : ""); |
203d3d4a | 1544 | tz->ops = ops; |
50125a9b | 1545 | tz->tzp = tzp; |
203d3d4a ZR |
1546 | tz->device.class = &thermal_class; |
1547 | tz->devdata = devdata; | |
1548 | tz->trips = trips; | |
b1569e99 MG |
1549 | tz->passive_delay = passive_delay; |
1550 | tz->polling_delay = polling_delay; | |
1551 | ||
354655ea | 1552 | dev_set_name(&tz->device, "thermal_zone%d", tz->id); |
203d3d4a ZR |
1553 | result = device_register(&tz->device); |
1554 | if (result) { | |
1555 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); | |
1556 | kfree(tz); | |
3e6fda5c | 1557 | return ERR_PTR(result); |
203d3d4a ZR |
1558 | } |
1559 | ||
1560 | /* sys I/F */ | |
1561 | if (type) { | |
1562 | result = device_create_file(&tz->device, &dev_attr_type); | |
1563 | if (result) | |
1564 | goto unregister; | |
1565 | } | |
1566 | ||
1567 | result = device_create_file(&tz->device, &dev_attr_temp); | |
1568 | if (result) | |
1569 | goto unregister; | |
1570 | ||
1571 | if (ops->get_mode) { | |
1572 | result = device_create_file(&tz->device, &dev_attr_mode); | |
1573 | if (result) | |
1574 | goto unregister; | |
1575 | } | |
1576 | ||
c56f5c03 D |
1577 | result = create_trip_attrs(tz, mask); |
1578 | if (result) | |
1579 | goto unregister; | |
1580 | ||
203d3d4a | 1581 | for (count = 0; count < trips; count++) { |
03a971a2 MG |
1582 | tz->ops->get_trip_type(tz, count, &trip_type); |
1583 | if (trip_type == THERMAL_TRIP_PASSIVE) | |
1584 | passive = 1; | |
203d3d4a ZR |
1585 | } |
1586 | ||
5a2c090b D |
1587 | if (!passive) { |
1588 | result = device_create_file(&tz->device, &dev_attr_passive); | |
1589 | if (result) | |
1590 | goto unregister; | |
1591 | } | |
03a971a2 | 1592 | |
5a2c090b D |
1593 | /* Create policy attribute */ |
1594 | result = device_create_file(&tz->device, &dev_attr_policy); | |
03a971a2 MG |
1595 | if (result) |
1596 | goto unregister; | |
1597 | ||
a4a15485 D |
1598 | /* Update 'this' zone's governor information */ |
1599 | mutex_lock(&thermal_governor_lock); | |
1600 | ||
1601 | if (tz->tzp) | |
1602 | tz->governor = __find_governor(tz->tzp->governor_name); | |
1603 | else | |
1604 | tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR); | |
1605 | ||
1606 | mutex_unlock(&thermal_governor_lock); | |
1607 | ||
e68b16ab ZR |
1608 | result = thermal_add_hwmon_sysfs(tz); |
1609 | if (result) | |
1610 | goto unregister; | |
1611 | ||
203d3d4a ZR |
1612 | mutex_lock(&thermal_list_lock); |
1613 | list_add_tail(&tz->node, &thermal_tz_list); | |
203d3d4a ZR |
1614 | mutex_unlock(&thermal_list_lock); |
1615 | ||
7e8ee1e9 D |
1616 | /* Bind cooling devices for this zone */ |
1617 | bind_tz(tz); | |
1618 | ||
b1569e99 MG |
1619 | INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check); |
1620 | ||
1621 | thermal_zone_device_update(tz); | |
1622 | ||
203d3d4a ZR |
1623 | if (!result) |
1624 | return tz; | |
1625 | ||
caca8b80 | 1626 | unregister: |
203d3d4a ZR |
1627 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
1628 | device_unregister(&tz->device); | |
3e6fda5c | 1629 | return ERR_PTR(result); |
203d3d4a ZR |
1630 | } |
1631 | EXPORT_SYMBOL(thermal_zone_device_register); | |
1632 | ||
1633 | /** | |
1634 | * thermal_device_unregister - removes the registered thermal zone device | |
203d3d4a ZR |
1635 | * @tz: the thermal zone device to remove |
1636 | */ | |
1637 | void thermal_zone_device_unregister(struct thermal_zone_device *tz) | |
1638 | { | |
7e8ee1e9 D |
1639 | int i; |
1640 | const struct thermal_zone_params *tzp; | |
203d3d4a ZR |
1641 | struct thermal_cooling_device *cdev; |
1642 | struct thermal_zone_device *pos = NULL; | |
203d3d4a ZR |
1643 | |
1644 | if (!tz) | |
1645 | return; | |
1646 | ||
7e8ee1e9 D |
1647 | tzp = tz->tzp; |
1648 | ||
203d3d4a ZR |
1649 | mutex_lock(&thermal_list_lock); |
1650 | list_for_each_entry(pos, &thermal_tz_list, node) | |
1651 | if (pos == tz) | |
1652 | break; | |
1653 | if (pos != tz) { | |
1654 | /* thermal zone device not found */ | |
1655 | mutex_unlock(&thermal_list_lock); | |
1656 | return; | |
1657 | } | |
1658 | list_del(&tz->node); | |
7e8ee1e9 D |
1659 | |
1660 | /* Unbind all cdevs associated with 'this' thermal zone */ | |
1661 | list_for_each_entry(cdev, &thermal_cdev_list, node) { | |
1662 | if (tz->ops->unbind) { | |
1663 | tz->ops->unbind(tz, cdev); | |
1664 | continue; | |
1665 | } | |
1666 | ||
1667 | if (!tzp || !tzp->tbp) | |
1668 | break; | |
1669 | ||
1670 | for (i = 0; i < tzp->num_tbps; i++) { | |
1671 | if (tzp->tbp[i].cdev == cdev) { | |
1672 | __unbind(tz, tzp->tbp[i].trip_mask, cdev); | |
1673 | tzp->tbp[i].cdev = NULL; | |
1674 | } | |
1675 | } | |
1676 | } | |
1677 | ||
203d3d4a ZR |
1678 | mutex_unlock(&thermal_list_lock); |
1679 | ||
b1569e99 MG |
1680 | thermal_zone_device_set_polling(tz, 0); |
1681 | ||
203d3d4a ZR |
1682 | if (tz->type[0]) |
1683 | device_remove_file(&tz->device, &dev_attr_type); | |
1684 | device_remove_file(&tz->device, &dev_attr_temp); | |
1685 | if (tz->ops->get_mode) | |
1686 | device_remove_file(&tz->device, &dev_attr_mode); | |
5a2c090b | 1687 | device_remove_file(&tz->device, &dev_attr_policy); |
c56f5c03 | 1688 | remove_trip_attrs(tz); |
a4a15485 | 1689 | tz->governor = NULL; |
203d3d4a | 1690 | |
e68b16ab | 1691 | thermal_remove_hwmon_sysfs(tz); |
203d3d4a ZR |
1692 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
1693 | idr_destroy(&tz->idr); | |
1694 | mutex_destroy(&tz->lock); | |
1695 | device_unregister(&tz->device); | |
1696 | return; | |
1697 | } | |
1698 | EXPORT_SYMBOL(thermal_zone_device_unregister); | |
1699 | ||
af06216a RW |
1700 | #ifdef CONFIG_NET |
1701 | static struct genl_family thermal_event_genl_family = { | |
1702 | .id = GENL_ID_GENERATE, | |
1703 | .name = THERMAL_GENL_FAMILY_NAME, | |
1704 | .version = THERMAL_GENL_VERSION, | |
1705 | .maxattr = THERMAL_GENL_ATTR_MAX, | |
1706 | }; | |
1707 | ||
1708 | static struct genl_multicast_group thermal_event_mcgrp = { | |
1709 | .name = THERMAL_GENL_MCAST_GROUP_NAME, | |
1710 | }; | |
1711 | ||
2d58d7ea | 1712 | int thermal_generate_netlink_event(u32 orig, enum events event) |
4cb18728 D |
1713 | { |
1714 | struct sk_buff *skb; | |
1715 | struct nlattr *attr; | |
1716 | struct thermal_genl_event *thermal_event; | |
1717 | void *msg_header; | |
1718 | int size; | |
1719 | int result; | |
b11de07c | 1720 | static unsigned int thermal_event_seqnum; |
4cb18728 D |
1721 | |
1722 | /* allocate memory */ | |
886ee546 JP |
1723 | size = nla_total_size(sizeof(struct thermal_genl_event)) + |
1724 | nla_total_size(0); | |
4cb18728 D |
1725 | |
1726 | skb = genlmsg_new(size, GFP_ATOMIC); | |
1727 | if (!skb) | |
1728 | return -ENOMEM; | |
1729 | ||
1730 | /* add the genetlink message header */ | |
1731 | msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++, | |
1732 | &thermal_event_genl_family, 0, | |
1733 | THERMAL_GENL_CMD_EVENT); | |
1734 | if (!msg_header) { | |
1735 | nlmsg_free(skb); | |
1736 | return -ENOMEM; | |
1737 | } | |
1738 | ||
1739 | /* fill the data */ | |
886ee546 JP |
1740 | attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT, |
1741 | sizeof(struct thermal_genl_event)); | |
4cb18728 D |
1742 | |
1743 | if (!attr) { | |
1744 | nlmsg_free(skb); | |
1745 | return -EINVAL; | |
1746 | } | |
1747 | ||
1748 | thermal_event = nla_data(attr); | |
1749 | if (!thermal_event) { | |
1750 | nlmsg_free(skb); | |
1751 | return -EINVAL; | |
1752 | } | |
1753 | ||
1754 | memset(thermal_event, 0, sizeof(struct thermal_genl_event)); | |
1755 | ||
1756 | thermal_event->orig = orig; | |
1757 | thermal_event->event = event; | |
1758 | ||
1759 | /* send multicast genetlink message */ | |
1760 | result = genlmsg_end(skb, msg_header); | |
1761 | if (result < 0) { | |
1762 | nlmsg_free(skb); | |
1763 | return result; | |
1764 | } | |
1765 | ||
1766 | result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC); | |
1767 | if (result) | |
c5a01dd5 | 1768 | pr_info("failed to send netlink event:%d\n", result); |
4cb18728 D |
1769 | |
1770 | return result; | |
1771 | } | |
2d58d7ea | 1772 | EXPORT_SYMBOL(thermal_generate_netlink_event); |
4cb18728 D |
1773 | |
1774 | static int genetlink_init(void) | |
1775 | { | |
1776 | int result; | |
1777 | ||
1778 | result = genl_register_family(&thermal_event_genl_family); | |
1779 | if (result) | |
1780 | return result; | |
1781 | ||
1782 | result = genl_register_mc_group(&thermal_event_genl_family, | |
1783 | &thermal_event_mcgrp); | |
1784 | if (result) | |
1785 | genl_unregister_family(&thermal_event_genl_family); | |
1786 | return result; | |
1787 | } | |
1788 | ||
af06216a RW |
1789 | static void genetlink_exit(void) |
1790 | { | |
1791 | genl_unregister_family(&thermal_event_genl_family); | |
1792 | } | |
1793 | #else /* !CONFIG_NET */ | |
1794 | static inline int genetlink_init(void) { return 0; } | |
1795 | static inline void genetlink_exit(void) {} | |
1796 | #endif /* !CONFIG_NET */ | |
1797 | ||
203d3d4a ZR |
1798 | static int __init thermal_init(void) |
1799 | { | |
1800 | int result = 0; | |
1801 | ||
1802 | result = class_register(&thermal_class); | |
1803 | if (result) { | |
1804 | idr_destroy(&thermal_tz_idr); | |
1805 | idr_destroy(&thermal_cdev_idr); | |
1806 | mutex_destroy(&thermal_idr_lock); | |
1807 | mutex_destroy(&thermal_list_lock); | |
1808 | } | |
4cb18728 | 1809 | result = genetlink_init(); |
203d3d4a ZR |
1810 | return result; |
1811 | } | |
1812 | ||
1813 | static void __exit thermal_exit(void) | |
1814 | { | |
1815 | class_unregister(&thermal_class); | |
1816 | idr_destroy(&thermal_tz_idr); | |
1817 | idr_destroy(&thermal_cdev_idr); | |
1818 | mutex_destroy(&thermal_idr_lock); | |
1819 | mutex_destroy(&thermal_list_lock); | |
4cb18728 | 1820 | genetlink_exit(); |
203d3d4a ZR |
1821 | } |
1822 | ||
4cb18728 | 1823 | fs_initcall(thermal_init); |
203d3d4a | 1824 | module_exit(thermal_exit); |