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