iwlwifi: don't include iwl-dev.h from iwl-devtrace.h
[deliverable/linux.git] / drivers / thermal / thermal_sys.c
CommitLineData
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
26#include <linux/module.h>
27#include <linux/device.h>
28#include <linux/err.h>
29#include <linux/kdev_t.h>
30#include <linux/idr.h>
31#include <linux/thermal.h>
32#include <linux/spinlock.h>
b1569e99 33#include <linux/reboot.h>
203d3d4a 34
63c4ec90 35MODULE_AUTHOR("Zhang Rui");
203d3d4a
ZR
36MODULE_DESCRIPTION("Generic thermal management sysfs support");
37MODULE_LICENSE("GPL");
38
39#define PREFIX "Thermal: "
40
41struct thermal_cooling_device_instance {
42 int id;
43 char name[THERMAL_NAME_LENGTH];
44 struct thermal_zone_device *tz;
45 struct thermal_cooling_device *cdev;
46 int trip;
47 char attr_name[THERMAL_NAME_LENGTH];
48 struct device_attribute attr;
49 struct list_head node;
50};
51
52static DEFINE_IDR(thermal_tz_idr);
53static DEFINE_IDR(thermal_cdev_idr);
54static DEFINE_MUTEX(thermal_idr_lock);
55
56static LIST_HEAD(thermal_tz_list);
57static LIST_HEAD(thermal_cdev_list);
58static DEFINE_MUTEX(thermal_list_lock);
59
60static int get_idr(struct idr *idr, struct mutex *lock, int *id)
61{
62 int err;
63
64 again:
65 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
66 return -ENOMEM;
67
68 if (lock)
69 mutex_lock(lock);
70 err = idr_get_new(idr, NULL, id);
71 if (lock)
72 mutex_unlock(lock);
73 if (unlikely(err == -EAGAIN))
74 goto again;
75 else if (unlikely(err))
76 return err;
77
78 *id = *id & MAX_ID_MASK;
79 return 0;
80}
81
82static void release_idr(struct idr *idr, struct mutex *lock, int id)
83{
84 if (lock)
85 mutex_lock(lock);
86 idr_remove(idr, id);
87 if (lock)
88 mutex_unlock(lock);
89}
90
91/* sys I/F for thermal zone */
92
93#define to_thermal_zone(_dev) \
94 container_of(_dev, struct thermal_zone_device, device)
95
96static ssize_t
97type_show(struct device *dev, struct device_attribute *attr, char *buf)
98{
99 struct thermal_zone_device *tz = to_thermal_zone(dev);
100
101 return sprintf(buf, "%s\n", tz->type);
102}
103
104static ssize_t
105temp_show(struct device *dev, struct device_attribute *attr, char *buf)
106{
107 struct thermal_zone_device *tz = to_thermal_zone(dev);
6503e5df
MG
108 long temperature;
109 int ret;
203d3d4a
ZR
110
111 if (!tz->ops->get_temp)
112 return -EPERM;
113
6503e5df
MG
114 ret = tz->ops->get_temp(tz, &temperature);
115
116 if (ret)
117 return ret;
118
119 return sprintf(buf, "%ld\n", temperature);
203d3d4a
ZR
120}
121
122static ssize_t
123mode_show(struct device *dev, struct device_attribute *attr, char *buf)
124{
125 struct thermal_zone_device *tz = to_thermal_zone(dev);
6503e5df
MG
126 enum thermal_device_mode mode;
127 int result;
203d3d4a
ZR
128
129 if (!tz->ops->get_mode)
130 return -EPERM;
131
6503e5df
MG
132 result = tz->ops->get_mode(tz, &mode);
133 if (result)
134 return result;
135
136 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
137 : "disabled");
203d3d4a
ZR
138}
139
140static ssize_t
141mode_store(struct device *dev, struct device_attribute *attr,
142 const char *buf, size_t count)
143{
144 struct thermal_zone_device *tz = to_thermal_zone(dev);
145 int result;
146
147 if (!tz->ops->set_mode)
148 return -EPERM;
149
6503e5df
MG
150 if (!strncmp(buf, "enabled", sizeof("enabled")))
151 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
152 else if (!strncmp(buf, "disabled", sizeof("disabled")))
153 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
154 else
155 result = -EINVAL;
156
203d3d4a
ZR
157 if (result)
158 return result;
159
160 return count;
161}
162
163static ssize_t
164trip_point_type_show(struct device *dev, struct device_attribute *attr,
165 char *buf)
166{
167 struct thermal_zone_device *tz = to_thermal_zone(dev);
6503e5df
MG
168 enum thermal_trip_type type;
169 int trip, result;
203d3d4a
ZR
170
171 if (!tz->ops->get_trip_type)
172 return -EPERM;
173
174 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
175 return -EINVAL;
176
6503e5df
MG
177 result = tz->ops->get_trip_type(tz, trip, &type);
178 if (result)
179 return result;
180
181 switch (type) {
182 case THERMAL_TRIP_CRITICAL:
625120a4 183 return sprintf(buf, "critical\n");
6503e5df 184 case THERMAL_TRIP_HOT:
625120a4 185 return sprintf(buf, "hot\n");
6503e5df 186 case THERMAL_TRIP_PASSIVE:
625120a4 187 return sprintf(buf, "passive\n");
6503e5df 188 case THERMAL_TRIP_ACTIVE:
625120a4 189 return sprintf(buf, "active\n");
6503e5df 190 default:
625120a4 191 return sprintf(buf, "unknown\n");
6503e5df 192 }
203d3d4a
ZR
193}
194
195static ssize_t
196trip_point_temp_show(struct device *dev, struct device_attribute *attr,
197 char *buf)
198{
199 struct thermal_zone_device *tz = to_thermal_zone(dev);
6503e5df
MG
200 int trip, ret;
201 long temperature;
203d3d4a
ZR
202
203 if (!tz->ops->get_trip_temp)
204 return -EPERM;
205
206 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
207 return -EINVAL;
208
6503e5df
MG
209 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
210
211 if (ret)
212 return ret;
213
214 return sprintf(buf, "%ld\n", temperature);
203d3d4a
ZR
215}
216
03a971a2
MG
217static ssize_t
218passive_store(struct device *dev, struct device_attribute *attr,
219 const char *buf, size_t count)
220{
221 struct thermal_zone_device *tz = to_thermal_zone(dev);
222 struct thermal_cooling_device *cdev = NULL;
223 int state;
224
225 if (!sscanf(buf, "%d\n", &state))
226 return -EINVAL;
227
3d8e3ad8
FP
228 /* sanity check: values below 1000 millicelcius don't make sense
229 * and can cause the system to go into a thermal heart attack
230 */
231 if (state && state < 1000)
232 return -EINVAL;
233
03a971a2
MG
234 if (state && !tz->forced_passive) {
235 mutex_lock(&thermal_list_lock);
236 list_for_each_entry(cdev, &thermal_cdev_list, node) {
237 if (!strncmp("Processor", cdev->type,
238 sizeof("Processor")))
239 thermal_zone_bind_cooling_device(tz,
240 THERMAL_TRIPS_NONE,
241 cdev);
242 }
243 mutex_unlock(&thermal_list_lock);
e4143b03
FP
244 if (!tz->passive_delay)
245 tz->passive_delay = 1000;
03a971a2
MG
246 } else if (!state && tz->forced_passive) {
247 mutex_lock(&thermal_list_lock);
248 list_for_each_entry(cdev, &thermal_cdev_list, node) {
249 if (!strncmp("Processor", cdev->type,
250 sizeof("Processor")))
251 thermal_zone_unbind_cooling_device(tz,
252 THERMAL_TRIPS_NONE,
253 cdev);
254 }
255 mutex_unlock(&thermal_list_lock);
e4143b03 256 tz->passive_delay = 0;
03a971a2
MG
257 }
258
259 tz->tc1 = 1;
260 tz->tc2 = 1;
261
03a971a2
MG
262 tz->forced_passive = state;
263
264 thermal_zone_device_update(tz);
265
266 return count;
267}
268
269static ssize_t
270passive_show(struct device *dev, struct device_attribute *attr,
271 char *buf)
272{
273 struct thermal_zone_device *tz = to_thermal_zone(dev);
274
275 return sprintf(buf, "%d\n", tz->forced_passive);
276}
277
203d3d4a
ZR
278static DEVICE_ATTR(type, 0444, type_show, NULL);
279static DEVICE_ATTR(temp, 0444, temp_show, NULL);
280static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
03a971a2
MG
281static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, \
282 passive_store);
203d3d4a
ZR
283
284static struct device_attribute trip_point_attrs[] = {
285 __ATTR(trip_point_0_type, 0444, trip_point_type_show, NULL),
286 __ATTR(trip_point_0_temp, 0444, trip_point_temp_show, NULL),
287 __ATTR(trip_point_1_type, 0444, trip_point_type_show, NULL),
288 __ATTR(trip_point_1_temp, 0444, trip_point_temp_show, NULL),
289 __ATTR(trip_point_2_type, 0444, trip_point_type_show, NULL),
290 __ATTR(trip_point_2_temp, 0444, trip_point_temp_show, NULL),
291 __ATTR(trip_point_3_type, 0444, trip_point_type_show, NULL),
292 __ATTR(trip_point_3_temp, 0444, trip_point_temp_show, NULL),
293 __ATTR(trip_point_4_type, 0444, trip_point_type_show, NULL),
294 __ATTR(trip_point_4_temp, 0444, trip_point_temp_show, NULL),
295 __ATTR(trip_point_5_type, 0444, trip_point_type_show, NULL),
296 __ATTR(trip_point_5_temp, 0444, trip_point_temp_show, NULL),
297 __ATTR(trip_point_6_type, 0444, trip_point_type_show, NULL),
298 __ATTR(trip_point_6_temp, 0444, trip_point_temp_show, NULL),
299 __ATTR(trip_point_7_type, 0444, trip_point_type_show, NULL),
300 __ATTR(trip_point_7_temp, 0444, trip_point_temp_show, NULL),
301 __ATTR(trip_point_8_type, 0444, trip_point_type_show, NULL),
302 __ATTR(trip_point_8_temp, 0444, trip_point_temp_show, NULL),
303 __ATTR(trip_point_9_type, 0444, trip_point_type_show, NULL),
304 __ATTR(trip_point_9_temp, 0444, trip_point_temp_show, NULL),
5f1a3f2a
KH
305 __ATTR(trip_point_10_type, 0444, trip_point_type_show, NULL),
306 __ATTR(trip_point_10_temp, 0444, trip_point_temp_show, NULL),
307 __ATTR(trip_point_11_type, 0444, trip_point_type_show, NULL),
308 __ATTR(trip_point_11_temp, 0444, trip_point_temp_show, NULL),
203d3d4a
ZR
309};
310
311#define TRIP_POINT_ATTR_ADD(_dev, _index, result) \
312do { \
313 result = device_create_file(_dev, \
314 &trip_point_attrs[_index * 2]); \
315 if (result) \
316 break; \
317 result = device_create_file(_dev, \
318 &trip_point_attrs[_index * 2 + 1]); \
319} while (0)
320
321#define TRIP_POINT_ATTR_REMOVE(_dev, _index) \
322do { \
323 device_remove_file(_dev, &trip_point_attrs[_index * 2]); \
324 device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \
325} while (0)
326
327/* sys I/F for cooling device */
328#define to_cooling_device(_dev) \
329 container_of(_dev, struct thermal_cooling_device, device)
330
331static ssize_t
332thermal_cooling_device_type_show(struct device *dev,
333 struct device_attribute *attr, char *buf)
334{
335 struct thermal_cooling_device *cdev = to_cooling_device(dev);
336
337 return sprintf(buf, "%s\n", cdev->type);
338}
339
340static ssize_t
341thermal_cooling_device_max_state_show(struct device *dev,
342 struct device_attribute *attr, char *buf)
343{
344 struct thermal_cooling_device *cdev = to_cooling_device(dev);
6503e5df
MG
345 unsigned long state;
346 int ret;
203d3d4a 347
6503e5df
MG
348 ret = cdev->ops->get_max_state(cdev, &state);
349 if (ret)
350 return ret;
351 return sprintf(buf, "%ld\n", state);
203d3d4a
ZR
352}
353
354static ssize_t
355thermal_cooling_device_cur_state_show(struct device *dev,
356 struct device_attribute *attr, char *buf)
357{
358 struct thermal_cooling_device *cdev = to_cooling_device(dev);
6503e5df
MG
359 unsigned long state;
360 int ret;
203d3d4a 361
6503e5df
MG
362 ret = cdev->ops->get_cur_state(cdev, &state);
363 if (ret)
364 return ret;
365 return sprintf(buf, "%ld\n", state);
203d3d4a
ZR
366}
367
368static ssize_t
369thermal_cooling_device_cur_state_store(struct device *dev,
370 struct device_attribute *attr,
371 const char *buf, size_t count)
372{
373 struct thermal_cooling_device *cdev = to_cooling_device(dev);
6503e5df 374 unsigned long state;
203d3d4a
ZR
375 int result;
376
6503e5df 377 if (!sscanf(buf, "%ld\n", &state))
203d3d4a
ZR
378 return -EINVAL;
379
edb94918 380 if ((long)state < 0)
203d3d4a
ZR
381 return -EINVAL;
382
383 result = cdev->ops->set_cur_state(cdev, state);
384 if (result)
385 return result;
386 return count;
387}
388
389static struct device_attribute dev_attr_cdev_type =
543a9561 390__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
203d3d4a
ZR
391static DEVICE_ATTR(max_state, 0444,
392 thermal_cooling_device_max_state_show, NULL);
393static DEVICE_ATTR(cur_state, 0644,
394 thermal_cooling_device_cur_state_show,
395 thermal_cooling_device_cur_state_store);
396
397static ssize_t
398thermal_cooling_device_trip_point_show(struct device *dev,
543a9561 399 struct device_attribute *attr, char *buf)
203d3d4a
ZR
400{
401 struct thermal_cooling_device_instance *instance;
402
403 instance =
404 container_of(attr, struct thermal_cooling_device_instance, attr);
405
406 if (instance->trip == THERMAL_TRIPS_NONE)
407 return sprintf(buf, "-1\n");
408 else
409 return sprintf(buf, "%d\n", instance->trip);
410}
411
412/* Device management */
413
16d75239
RH
414#if defined(CONFIG_THERMAL_HWMON)
415
e68b16ab
ZR
416/* hwmon sys I/F */
417#include <linux/hwmon.h>
418static LIST_HEAD(thermal_hwmon_list);
419
420static ssize_t
421name_show(struct device *dev, struct device_attribute *attr, char *buf)
422{
0e968a3b 423 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
e68b16ab
ZR
424 return sprintf(buf, "%s\n", hwmon->type);
425}
426static DEVICE_ATTR(name, 0444, name_show, NULL);
427
428static ssize_t
429temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
430{
6503e5df
MG
431 long temperature;
432 int ret;
e68b16ab
ZR
433 struct thermal_hwmon_attr *hwmon_attr
434 = container_of(attr, struct thermal_hwmon_attr, attr);
435 struct thermal_zone_device *tz
436 = container_of(hwmon_attr, struct thermal_zone_device,
437 temp_input);
438
6503e5df
MG
439 ret = tz->ops->get_temp(tz, &temperature);
440
441 if (ret)
442 return ret;
443
444 return sprintf(buf, "%ld\n", temperature);
e68b16ab
ZR
445}
446
447static ssize_t
448temp_crit_show(struct device *dev, struct device_attribute *attr,
449 char *buf)
450{
451 struct thermal_hwmon_attr *hwmon_attr
452 = container_of(attr, struct thermal_hwmon_attr, attr);
453 struct thermal_zone_device *tz
454 = container_of(hwmon_attr, struct thermal_zone_device,
455 temp_crit);
6503e5df
MG
456 long temperature;
457 int ret;
458
459 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
460 if (ret)
461 return ret;
e68b16ab 462
6503e5df 463 return sprintf(buf, "%ld\n", temperature);
e68b16ab
ZR
464}
465
466
467static int
468thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
469{
470 struct thermal_hwmon_device *hwmon;
471 int new_hwmon_device = 1;
472 int result;
473
474 mutex_lock(&thermal_list_lock);
475 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
476 if (!strcmp(hwmon->type, tz->type)) {
477 new_hwmon_device = 0;
478 mutex_unlock(&thermal_list_lock);
479 goto register_sys_interface;
480 }
481 mutex_unlock(&thermal_list_lock);
482
483 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
484 if (!hwmon)
485 return -ENOMEM;
486
487 INIT_LIST_HEAD(&hwmon->tz_list);
488 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
489 hwmon->device = hwmon_device_register(NULL);
490 if (IS_ERR(hwmon->device)) {
491 result = PTR_ERR(hwmon->device);
492 goto free_mem;
493 }
0e968a3b 494 dev_set_drvdata(hwmon->device, hwmon);
e68b16ab
ZR
495 result = device_create_file(hwmon->device, &dev_attr_name);
496 if (result)
497 goto unregister_hwmon_device;
498
499 register_sys_interface:
500 tz->hwmon = hwmon;
501 hwmon->count++;
502
503 snprintf(tz->temp_input.name, THERMAL_NAME_LENGTH,
504 "temp%d_input", hwmon->count);
505 tz->temp_input.attr.attr.name = tz->temp_input.name;
506 tz->temp_input.attr.attr.mode = 0444;
507 tz->temp_input.attr.show = temp_input_show;
508 result = device_create_file(hwmon->device, &tz->temp_input.attr);
509 if (result)
510 goto unregister_hwmon_device;
511
512 if (tz->ops->get_crit_temp) {
513 unsigned long temperature;
514 if (!tz->ops->get_crit_temp(tz, &temperature)) {
515 snprintf(tz->temp_crit.name, THERMAL_NAME_LENGTH,
516 "temp%d_crit", hwmon->count);
517 tz->temp_crit.attr.attr.name = tz->temp_crit.name;
518 tz->temp_crit.attr.attr.mode = 0444;
519 tz->temp_crit.attr.show = temp_crit_show;
520 result = device_create_file(hwmon->device,
521 &tz->temp_crit.attr);
522 if (result)
523 goto unregister_hwmon_device;
524 }
525 }
526
527 mutex_lock(&thermal_list_lock);
528 if (new_hwmon_device)
529 list_add_tail(&hwmon->node, &thermal_hwmon_list);
530 list_add_tail(&tz->hwmon_node, &hwmon->tz_list);
531 mutex_unlock(&thermal_list_lock);
532
533 return 0;
534
535 unregister_hwmon_device:
536 device_remove_file(hwmon->device, &tz->temp_crit.attr);
537 device_remove_file(hwmon->device, &tz->temp_input.attr);
538 if (new_hwmon_device) {
539 device_remove_file(hwmon->device, &dev_attr_name);
540 hwmon_device_unregister(hwmon->device);
541 }
542 free_mem:
543 if (new_hwmon_device)
544 kfree(hwmon);
545
546 return result;
547}
548
549static void
550thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
551{
552 struct thermal_hwmon_device *hwmon = tz->hwmon;
553
554 tz->hwmon = NULL;
555 device_remove_file(hwmon->device, &tz->temp_input.attr);
556 device_remove_file(hwmon->device, &tz->temp_crit.attr);
557
558 mutex_lock(&thermal_list_lock);
559 list_del(&tz->hwmon_node);
560 if (!list_empty(&hwmon->tz_list)) {
561 mutex_unlock(&thermal_list_lock);
562 return;
563 }
564 list_del(&hwmon->node);
565 mutex_unlock(&thermal_list_lock);
566
567 device_remove_file(hwmon->device, &dev_attr_name);
568 hwmon_device_unregister(hwmon->device);
569 kfree(hwmon);
570}
571#else
572static int
573thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
574{
575 return 0;
576}
577
578static void
579thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
580{
581}
582#endif
583
b1569e99
MG
584static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
585 int delay)
586{
587 cancel_delayed_work(&(tz->poll_queue));
588
589 if (!delay)
590 return;
591
592 if (delay > 1000)
593 schedule_delayed_work(&(tz->poll_queue),
594 round_jiffies(msecs_to_jiffies(delay)));
595 else
596 schedule_delayed_work(&(tz->poll_queue),
597 msecs_to_jiffies(delay));
598}
599
600static void thermal_zone_device_passive(struct thermal_zone_device *tz,
601 int temp, int trip_temp, int trip)
602{
603 int trend = 0;
604 struct thermal_cooling_device_instance *instance;
605 struct thermal_cooling_device *cdev;
606 long state, max_state;
607
608 /*
609 * Above Trip?
610 * -----------
611 * Calculate the thermal trend (using the passive cooling equation)
612 * and modify the performance limit for all passive cooling devices
613 * accordingly. Note that we assume symmetry.
614 */
615 if (temp >= trip_temp) {
616 tz->passive = true;
617
618 trend = (tz->tc1 * (temp - tz->last_temperature)) +
619 (tz->tc2 * (temp - trip_temp));
620
621 /* Heating up? */
622 if (trend > 0) {
623 list_for_each_entry(instance, &tz->cooling_devices,
624 node) {
625 if (instance->trip != trip)
626 continue;
627 cdev = instance->cdev;
628 cdev->ops->get_cur_state(cdev, &state);
629 cdev->ops->get_max_state(cdev, &max_state);
630 if (state++ < max_state)
631 cdev->ops->set_cur_state(cdev, state);
632 }
633 } else if (trend < 0) { /* Cooling off? */
634 list_for_each_entry(instance, &tz->cooling_devices,
635 node) {
636 if (instance->trip != trip)
637 continue;
638 cdev = instance->cdev;
639 cdev->ops->get_cur_state(cdev, &state);
640 cdev->ops->get_max_state(cdev, &max_state);
641 if (state > 0)
642 cdev->ops->set_cur_state(cdev, --state);
643 }
644 }
645 return;
646 }
647
648 /*
649 * Below Trip?
650 * -----------
651 * Implement passive cooling hysteresis to slowly increase performance
652 * and avoid thrashing around the passive trip point. Note that we
653 * assume symmetry.
654 */
655 list_for_each_entry(instance, &tz->cooling_devices, node) {
656 if (instance->trip != trip)
657 continue;
658 cdev = instance->cdev;
659 cdev->ops->get_cur_state(cdev, &state);
660 cdev->ops->get_max_state(cdev, &max_state);
661 if (state > 0)
662 cdev->ops->set_cur_state(cdev, --state);
663 if (state == 0)
664 tz->passive = false;
665 }
666}
667
668static void thermal_zone_device_check(struct work_struct *work)
669{
670 struct thermal_zone_device *tz = container_of(work, struct
671 thermal_zone_device,
672 poll_queue.work);
673 thermal_zone_device_update(tz);
674}
e68b16ab 675
203d3d4a
ZR
676/**
677 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
203d3d4a
ZR
678 * @tz: thermal zone device
679 * @trip: indicates which trip point the cooling devices is
680 * associated with in this thermal zone.
681 * @cdev: thermal cooling device
543a9561
LB
682 *
683 * This function is usually called in the thermal zone device .bind callback.
203d3d4a
ZR
684 */
685int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
686 int trip,
687 struct thermal_cooling_device *cdev)
688{
689 struct thermal_cooling_device_instance *dev;
690 struct thermal_cooling_device_instance *pos;
c7516709
TS
691 struct thermal_zone_device *pos1;
692 struct thermal_cooling_device *pos2;
203d3d4a
ZR
693 int result;
694
543a9561 695 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
203d3d4a
ZR
696 return -EINVAL;
697
c7516709
TS
698 list_for_each_entry(pos1, &thermal_tz_list, node) {
699 if (pos1 == tz)
700 break;
701 }
702 list_for_each_entry(pos2, &thermal_cdev_list, node) {
703 if (pos2 == cdev)
704 break;
705 }
706
707 if (tz != pos1 || cdev != pos2)
203d3d4a
ZR
708 return -EINVAL;
709
710 dev =
711 kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL);
712 if (!dev)
713 return -ENOMEM;
714 dev->tz = tz;
715 dev->cdev = cdev;
716 dev->trip = trip;
717 result = get_idr(&tz->idr, &tz->lock, &dev->id);
718 if (result)
719 goto free_mem;
720
721 sprintf(dev->name, "cdev%d", dev->id);
722 result =
723 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
724 if (result)
725 goto release_idr;
726
727 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
728 dev->attr.attr.name = dev->attr_name;
729 dev->attr.attr.mode = 0444;
730 dev->attr.show = thermal_cooling_device_trip_point_show;
731 result = device_create_file(&tz->device, &dev->attr);
732 if (result)
733 goto remove_symbol_link;
734
735 mutex_lock(&tz->lock);
736 list_for_each_entry(pos, &tz->cooling_devices, node)
737 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
738 result = -EEXIST;
739 break;
740 }
741 if (!result)
742 list_add_tail(&dev->node, &tz->cooling_devices);
743 mutex_unlock(&tz->lock);
744
745 if (!result)
746 return 0;
747
748 device_remove_file(&tz->device, &dev->attr);
749 remove_symbol_link:
750 sysfs_remove_link(&tz->device.kobj, dev->name);
751 release_idr:
752 release_idr(&tz->idr, &tz->lock, dev->id);
753 free_mem:
754 kfree(dev);
755 return result;
756}
543a9561 757
203d3d4a
ZR
758EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
759
760/**
761 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
203d3d4a
ZR
762 * @tz: thermal zone device
763 * @trip: indicates which trip point the cooling devices is
764 * associated with in this thermal zone.
765 * @cdev: thermal cooling device
543a9561
LB
766 *
767 * This function is usually called in the thermal zone device .unbind callback.
203d3d4a
ZR
768 */
769int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
770 int trip,
771 struct thermal_cooling_device *cdev)
772{
773 struct thermal_cooling_device_instance *pos, *next;
774
775 mutex_lock(&tz->lock);
776 list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
543a9561 777 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
203d3d4a
ZR
778 list_del(&pos->node);
779 mutex_unlock(&tz->lock);
780 goto unbind;
781 }
782 }
783 mutex_unlock(&tz->lock);
784
785 return -ENODEV;
786
787 unbind:
788 device_remove_file(&tz->device, &pos->attr);
789 sysfs_remove_link(&tz->device.kobj, pos->name);
790 release_idr(&tz->idr, &tz->lock, pos->id);
791 kfree(pos);
792 return 0;
793}
543a9561 794
203d3d4a
ZR
795EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
796
797static void thermal_release(struct device *dev)
798{
799 struct thermal_zone_device *tz;
800 struct thermal_cooling_device *cdev;
801
354655ea 802 if (!strncmp(dev_name(dev), "thermal_zone", sizeof "thermal_zone" - 1)) {
203d3d4a
ZR
803 tz = to_thermal_zone(dev);
804 kfree(tz);
805 } else {
806 cdev = to_cooling_device(dev);
807 kfree(cdev);
808 }
809}
810
811static struct class thermal_class = {
812 .name = "thermal",
813 .dev_release = thermal_release,
814};
815
816/**
817 * thermal_cooling_device_register - register a new thermal cooling device
818 * @type: the thermal cooling device type.
819 * @devdata: device private data.
820 * @ops: standard thermal cooling devices callbacks.
821 */
822struct thermal_cooling_device *thermal_cooling_device_register(char *type,
543a9561
LB
823 void *devdata,
824 struct
825 thermal_cooling_device_ops
826 *ops)
203d3d4a
ZR
827{
828 struct thermal_cooling_device *cdev;
829 struct thermal_zone_device *pos;
830 int result;
831
832 if (strlen(type) >= THERMAL_NAME_LENGTH)
3e6fda5c 833 return ERR_PTR(-EINVAL);
203d3d4a
ZR
834
835 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
543a9561 836 !ops->set_cur_state)
3e6fda5c 837 return ERR_PTR(-EINVAL);
203d3d4a
ZR
838
839 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
840 if (!cdev)
3e6fda5c 841 return ERR_PTR(-ENOMEM);
203d3d4a
ZR
842
843 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
844 if (result) {
845 kfree(cdev);
3e6fda5c 846 return ERR_PTR(result);
203d3d4a
ZR
847 }
848
849 strcpy(cdev->type, type);
850 cdev->ops = ops;
851 cdev->device.class = &thermal_class;
852 cdev->devdata = devdata;
354655ea 853 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
203d3d4a
ZR
854 result = device_register(&cdev->device);
855 if (result) {
856 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
857 kfree(cdev);
3e6fda5c 858 return ERR_PTR(result);
203d3d4a
ZR
859 }
860
861 /* sys I/F */
862 if (type) {
543a9561 863 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
203d3d4a
ZR
864 if (result)
865 goto unregister;
866 }
867
868 result = device_create_file(&cdev->device, &dev_attr_max_state);
869 if (result)
870 goto unregister;
871
872 result = device_create_file(&cdev->device, &dev_attr_cur_state);
873 if (result)
874 goto unregister;
875
876 mutex_lock(&thermal_list_lock);
877 list_add(&cdev->node, &thermal_cdev_list);
878 list_for_each_entry(pos, &thermal_tz_list, node) {
879 if (!pos->ops->bind)
880 continue;
881 result = pos->ops->bind(pos, cdev);
882 if (result)
883 break;
884
885 }
886 mutex_unlock(&thermal_list_lock);
887
888 if (!result)
889 return cdev;
890
891 unregister:
892 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
893 device_unregister(&cdev->device);
3e6fda5c 894 return ERR_PTR(result);
203d3d4a 895}
543a9561 896
203d3d4a
ZR
897EXPORT_SYMBOL(thermal_cooling_device_register);
898
899/**
900 * thermal_cooling_device_unregister - removes the registered thermal cooling device
203d3d4a
ZR
901 * @cdev: the thermal cooling device to remove.
902 *
903 * thermal_cooling_device_unregister() must be called when the device is no
904 * longer needed.
905 */
906void thermal_cooling_device_unregister(struct
907 thermal_cooling_device
908 *cdev)
909{
910 struct thermal_zone_device *tz;
911 struct thermal_cooling_device *pos = NULL;
912
913 if (!cdev)
914 return;
915
916 mutex_lock(&thermal_list_lock);
917 list_for_each_entry(pos, &thermal_cdev_list, node)
918 if (pos == cdev)
919 break;
920 if (pos != cdev) {
921 /* thermal cooling device not found */
922 mutex_unlock(&thermal_list_lock);
923 return;
924 }
925 list_del(&cdev->node);
926 list_for_each_entry(tz, &thermal_tz_list, node) {
927 if (!tz->ops->unbind)
928 continue;
929 tz->ops->unbind(tz, cdev);
930 }
931 mutex_unlock(&thermal_list_lock);
932 if (cdev->type[0])
543a9561 933 device_remove_file(&cdev->device, &dev_attr_cdev_type);
203d3d4a
ZR
934 device_remove_file(&cdev->device, &dev_attr_max_state);
935 device_remove_file(&cdev->device, &dev_attr_cur_state);
936
937 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
938 device_unregister(&cdev->device);
939 return;
940}
543a9561 941
203d3d4a
ZR
942EXPORT_SYMBOL(thermal_cooling_device_unregister);
943
b1569e99
MG
944/**
945 * thermal_zone_device_update - force an update of a thermal zone's state
946 * @ttz: the thermal zone to update
947 */
948
949void thermal_zone_device_update(struct thermal_zone_device *tz)
950{
951 int count, ret = 0;
952 long temp, trip_temp;
953 enum thermal_trip_type trip_type;
954 struct thermal_cooling_device_instance *instance;
955 struct thermal_cooling_device *cdev;
956
957 mutex_lock(&tz->lock);
958
0d288162
MB
959 if (tz->ops->get_temp(tz, &temp)) {
960 /* get_temp failed - retry it later */
961 printk(KERN_WARNING PREFIX "failed to read out thermal zone "
962 "%d\n", tz->id);
963 goto leave;
964 }
b1569e99
MG
965
966 for (count = 0; count < tz->trips; count++) {
967 tz->ops->get_trip_type(tz, count, &trip_type);
968 tz->ops->get_trip_temp(tz, count, &trip_temp);
969
970 switch (trip_type) {
971 case THERMAL_TRIP_CRITICAL:
29321357 972 if (temp >= trip_temp) {
b1569e99
MG
973 if (tz->ops->notify)
974 ret = tz->ops->notify(tz, count,
975 trip_type);
976 if (!ret) {
977 printk(KERN_EMERG
978 "Critical temperature reached (%ld C), shutting down.\n",
979 temp/1000);
980 orderly_poweroff(true);
981 }
982 }
983 break;
984 case THERMAL_TRIP_HOT:
29321357 985 if (temp >= trip_temp)
b1569e99
MG
986 if (tz->ops->notify)
987 tz->ops->notify(tz, count, trip_type);
988 break;
989 case THERMAL_TRIP_ACTIVE:
990 list_for_each_entry(instance, &tz->cooling_devices,
991 node) {
992 if (instance->trip != count)
993 continue;
994
995 cdev = instance->cdev;
996
29321357 997 if (temp >= trip_temp)
b1569e99
MG
998 cdev->ops->set_cur_state(cdev, 1);
999 else
1000 cdev->ops->set_cur_state(cdev, 0);
1001 }
1002 break;
1003 case THERMAL_TRIP_PASSIVE:
29321357 1004 if (temp >= trip_temp || tz->passive)
b1569e99
MG
1005 thermal_zone_device_passive(tz, temp,
1006 trip_temp, count);
1007 break;
1008 }
1009 }
03a971a2
MG
1010
1011 if (tz->forced_passive)
1012 thermal_zone_device_passive(tz, temp, tz->forced_passive,
1013 THERMAL_TRIPS_NONE);
1014
b1569e99 1015 tz->last_temperature = temp;
0d288162
MB
1016
1017 leave:
b1569e99
MG
1018 if (tz->passive)
1019 thermal_zone_device_set_polling(tz, tz->passive_delay);
1020 else if (tz->polling_delay)
1021 thermal_zone_device_set_polling(tz, tz->polling_delay);
3767cb54
FP
1022 else
1023 thermal_zone_device_set_polling(tz, 0);
b1569e99
MG
1024 mutex_unlock(&tz->lock);
1025}
1026EXPORT_SYMBOL(thermal_zone_device_update);
1027
203d3d4a
ZR
1028/**
1029 * thermal_zone_device_register - register a new thermal zone device
1030 * @type: the thermal zone device type
1031 * @trips: the number of trip points the thermal zone support
1032 * @devdata: private device data
1033 * @ops: standard thermal zone device callbacks
b1569e99
MG
1034 * @tc1: thermal coefficient 1 for passive calculations
1035 * @tc2: thermal coefficient 2 for passive calculations
1036 * @passive_delay: number of milliseconds to wait between polls when
1037 * performing passive cooling
1038 * @polling_delay: number of milliseconds to wait between polls when checking
1039 * whether trip points have been crossed (0 for interrupt
1040 * driven systems)
203d3d4a
ZR
1041 *
1042 * thermal_zone_device_unregister() must be called when the device is no
b1569e99
MG
1043 * longer needed. The passive cooling formula uses tc1 and tc2 as described in
1044 * section 11.1.5.1 of the ACPI specification 3.0.
203d3d4a
ZR
1045 */
1046struct thermal_zone_device *thermal_zone_device_register(char *type,
543a9561
LB
1047 int trips,
1048 void *devdata, struct
1049 thermal_zone_device_ops
b1569e99
MG
1050 *ops, int tc1, int
1051 tc2,
1052 int passive_delay,
1053 int polling_delay)
203d3d4a
ZR
1054{
1055 struct thermal_zone_device *tz;
1056 struct thermal_cooling_device *pos;
03a971a2 1057 enum thermal_trip_type trip_type;
203d3d4a
ZR
1058 int result;
1059 int count;
03a971a2 1060 int passive = 0;
203d3d4a
ZR
1061
1062 if (strlen(type) >= THERMAL_NAME_LENGTH)
3e6fda5c 1063 return ERR_PTR(-EINVAL);
203d3d4a
ZR
1064
1065 if (trips > THERMAL_MAX_TRIPS || trips < 0)
3e6fda5c 1066 return ERR_PTR(-EINVAL);
203d3d4a
ZR
1067
1068 if (!ops || !ops->get_temp)
3e6fda5c 1069 return ERR_PTR(-EINVAL);
203d3d4a
ZR
1070
1071 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1072 if (!tz)
3e6fda5c 1073 return ERR_PTR(-ENOMEM);
203d3d4a
ZR
1074
1075 INIT_LIST_HEAD(&tz->cooling_devices);
1076 idr_init(&tz->idr);
1077 mutex_init(&tz->lock);
1078 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1079 if (result) {
1080 kfree(tz);
3e6fda5c 1081 return ERR_PTR(result);
203d3d4a
ZR
1082 }
1083
1084 strcpy(tz->type, type);
1085 tz->ops = ops;
1086 tz->device.class = &thermal_class;
1087 tz->devdata = devdata;
1088 tz->trips = trips;
b1569e99
MG
1089 tz->tc1 = tc1;
1090 tz->tc2 = tc2;
1091 tz->passive_delay = passive_delay;
1092 tz->polling_delay = polling_delay;
1093
354655ea 1094 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
203d3d4a
ZR
1095 result = device_register(&tz->device);
1096 if (result) {
1097 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1098 kfree(tz);
3e6fda5c 1099 return ERR_PTR(result);
203d3d4a
ZR
1100 }
1101
1102 /* sys I/F */
1103 if (type) {
1104 result = device_create_file(&tz->device, &dev_attr_type);
1105 if (result)
1106 goto unregister;
1107 }
1108
1109 result = device_create_file(&tz->device, &dev_attr_temp);
1110 if (result)
1111 goto unregister;
1112
1113 if (ops->get_mode) {
1114 result = device_create_file(&tz->device, &dev_attr_mode);
1115 if (result)
1116 goto unregister;
1117 }
1118
1119 for (count = 0; count < trips; count++) {
1120 TRIP_POINT_ATTR_ADD(&tz->device, count, result);
1121 if (result)
1122 goto unregister;
03a971a2
MG
1123 tz->ops->get_trip_type(tz, count, &trip_type);
1124 if (trip_type == THERMAL_TRIP_PASSIVE)
1125 passive = 1;
203d3d4a
ZR
1126 }
1127
03a971a2
MG
1128 if (!passive)
1129 result = device_create_file(&tz->device,
1130 &dev_attr_passive);
1131
1132 if (result)
1133 goto unregister;
1134
e68b16ab
ZR
1135 result = thermal_add_hwmon_sysfs(tz);
1136 if (result)
1137 goto unregister;
1138
203d3d4a
ZR
1139 mutex_lock(&thermal_list_lock);
1140 list_add_tail(&tz->node, &thermal_tz_list);
1141 if (ops->bind)
1142 list_for_each_entry(pos, &thermal_cdev_list, node) {
543a9561
LB
1143 result = ops->bind(tz, pos);
1144 if (result)
1145 break;
203d3d4a
ZR
1146 }
1147 mutex_unlock(&thermal_list_lock);
1148
b1569e99
MG
1149 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1150
1151 thermal_zone_device_update(tz);
1152
203d3d4a
ZR
1153 if (!result)
1154 return tz;
1155
1156 unregister:
1157 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1158 device_unregister(&tz->device);
3e6fda5c 1159 return ERR_PTR(result);
203d3d4a 1160}
543a9561 1161
203d3d4a
ZR
1162EXPORT_SYMBOL(thermal_zone_device_register);
1163
1164/**
1165 * thermal_device_unregister - removes the registered thermal zone device
203d3d4a
ZR
1166 * @tz: the thermal zone device to remove
1167 */
1168void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1169{
1170 struct thermal_cooling_device *cdev;
1171 struct thermal_zone_device *pos = NULL;
1172 int count;
1173
1174 if (!tz)
1175 return;
1176
1177 mutex_lock(&thermal_list_lock);
1178 list_for_each_entry(pos, &thermal_tz_list, node)
1179 if (pos == tz)
1180 break;
1181 if (pos != tz) {
1182 /* thermal zone device not found */
1183 mutex_unlock(&thermal_list_lock);
1184 return;
1185 }
1186 list_del(&tz->node);
1187 if (tz->ops->unbind)
1188 list_for_each_entry(cdev, &thermal_cdev_list, node)
1189 tz->ops->unbind(tz, cdev);
1190 mutex_unlock(&thermal_list_lock);
1191
b1569e99
MG
1192 thermal_zone_device_set_polling(tz, 0);
1193
203d3d4a
ZR
1194 if (tz->type[0])
1195 device_remove_file(&tz->device, &dev_attr_type);
1196 device_remove_file(&tz->device, &dev_attr_temp);
1197 if (tz->ops->get_mode)
1198 device_remove_file(&tz->device, &dev_attr_mode);
1199
1200 for (count = 0; count < tz->trips; count++)
1201 TRIP_POINT_ATTR_REMOVE(&tz->device, count);
1202
e68b16ab 1203 thermal_remove_hwmon_sysfs(tz);
203d3d4a
ZR
1204 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1205 idr_destroy(&tz->idr);
1206 mutex_destroy(&tz->lock);
1207 device_unregister(&tz->device);
1208 return;
1209}
543a9561 1210
203d3d4a
ZR
1211EXPORT_SYMBOL(thermal_zone_device_unregister);
1212
1213static int __init thermal_init(void)
1214{
1215 int result = 0;
1216
1217 result = class_register(&thermal_class);
1218 if (result) {
1219 idr_destroy(&thermal_tz_idr);
1220 idr_destroy(&thermal_cdev_idr);
1221 mutex_destroy(&thermal_idr_lock);
1222 mutex_destroy(&thermal_list_lock);
1223 }
1224 return result;
1225}
1226
1227static void __exit thermal_exit(void)
1228{
1229 class_unregister(&thermal_class);
1230 idr_destroy(&thermal_tz_idr);
1231 idr_destroy(&thermal_cdev_idr);
1232 mutex_destroy(&thermal_idr_lock);
1233 mutex_destroy(&thermal_list_lock);
1234}
1235
1236subsys_initcall(thermal_init);
1237module_exit(thermal_exit);
This page took 0.230736 seconds and 5 git commands to generate.