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