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