blob: e0d8ef79e519c012aafdfc9b9c7f0662d5323bd3 [file] [log] [blame]
Zhang Rui203d3d42008-01-17 15:51:08 +08001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080030#include <linux/kdev_t.h>
31#include <linux/idr.h>
32#include <linux/thermal.h>
33#include <linux/spinlock.h>
Matthew Garrettb1569e92008-12-03 17:55:32 +000034#include <linux/reboot.h>
R.Durgadoss4cb18722010-10-27 03:33:29 +053035#include <net/netlink.h>
36#include <net/genetlink.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080037
Zhang Rui63c4ec92008-04-21 16:07:13 +080038MODULE_AUTHOR("Zhang Rui");
Zhang Rui203d3d42008-01-17 15:51:08 +080039MODULE_DESCRIPTION("Generic thermal management sysfs support");
40MODULE_LICENSE("GPL");
41
42#define PREFIX "Thermal: "
43
44struct thermal_cooling_device_instance {
45 int id;
46 char name[THERMAL_NAME_LENGTH];
47 struct thermal_zone_device *tz;
48 struct thermal_cooling_device *cdev;
49 int trip;
50 char attr_name[THERMAL_NAME_LENGTH];
51 struct device_attribute attr;
52 struct list_head node;
53};
54
55static DEFINE_IDR(thermal_tz_idr);
56static DEFINE_IDR(thermal_cdev_idr);
57static DEFINE_MUTEX(thermal_idr_lock);
58
59static LIST_HEAD(thermal_tz_list);
60static LIST_HEAD(thermal_cdev_list);
61static DEFINE_MUTEX(thermal_list_lock);
62
R.Durgadoss4cb18722010-10-27 03:33:29 +053063static unsigned int thermal_event_seqnum;
64
Zhang Rui203d3d42008-01-17 15:51:08 +080065static int get_idr(struct idr *idr, struct mutex *lock, int *id)
66{
67 int err;
68
69 again:
70 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
71 return -ENOMEM;
72
73 if (lock)
74 mutex_lock(lock);
75 err = idr_get_new(idr, NULL, id);
76 if (lock)
77 mutex_unlock(lock);
78 if (unlikely(err == -EAGAIN))
79 goto again;
80 else if (unlikely(err))
81 return err;
82
83 *id = *id & MAX_ID_MASK;
84 return 0;
85}
86
87static void release_idr(struct idr *idr, struct mutex *lock, int id)
88{
89 if (lock)
90 mutex_lock(lock);
91 idr_remove(idr, id);
92 if (lock)
93 mutex_unlock(lock);
94}
95
96/* sys I/F for thermal zone */
97
98#define to_thermal_zone(_dev) \
99 container_of(_dev, struct thermal_zone_device, device)
100
101static ssize_t
102type_show(struct device *dev, struct device_attribute *attr, char *buf)
103{
104 struct thermal_zone_device *tz = to_thermal_zone(dev);
105
106 return sprintf(buf, "%s\n", tz->type);
107}
108
109static ssize_t
110temp_show(struct device *dev, struct device_attribute *attr, char *buf)
111{
112 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000113 long temperature;
114 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800115
116 if (!tz->ops->get_temp)
117 return -EPERM;
118
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000119 ret = tz->ops->get_temp(tz, &temperature);
120
121 if (ret)
122 return ret;
123
124 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800125}
126
127static ssize_t
128mode_show(struct device *dev, struct device_attribute *attr, char *buf)
129{
130 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000131 enum thermal_device_mode mode;
132 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800133
134 if (!tz->ops->get_mode)
135 return -EPERM;
136
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000137 result = tz->ops->get_mode(tz, &mode);
138 if (result)
139 return result;
140
141 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
142 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800143}
144
145static ssize_t
146mode_store(struct device *dev, struct device_attribute *attr,
147 const char *buf, size_t count)
148{
149 struct thermal_zone_device *tz = to_thermal_zone(dev);
150 int result;
151
152 if (!tz->ops->set_mode)
153 return -EPERM;
154
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000155 if (!strncmp(buf, "enabled", sizeof("enabled")))
156 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
157 else if (!strncmp(buf, "disabled", sizeof("disabled")))
158 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
159 else
160 result = -EINVAL;
161
Zhang Rui203d3d42008-01-17 15:51:08 +0800162 if (result)
163 return result;
164
165 return count;
166}
167
168static ssize_t
169trip_point_type_show(struct device *dev, struct device_attribute *attr,
170 char *buf)
171{
172 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000173 enum thermal_trip_type type;
174 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800175
176 if (!tz->ops->get_trip_type)
177 return -EPERM;
178
179 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
180 return -EINVAL;
181
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000182 result = tz->ops->get_trip_type(tz, trip, &type);
183 if (result)
184 return result;
185
186 switch (type) {
187 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a2009-10-16 12:46:02 +0300188 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000189 case THERMAL_TRIP_HOT:
Amit Kucheria625120a2009-10-16 12:46:02 +0300190 return sprintf(buf, "hot\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191 case THERMAL_TRIP_CONFIGURABLE_HI:
192 return sprintf(buf, "configurable_hi\n");
193 case THERMAL_TRIP_CONFIGURABLE_LOW:
194 return sprintf(buf, "configurable_low\n");
195 case THERMAL_TRIP_CRITICAL_LOW:
196 return sprintf(buf, "critical_low\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000197 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a2009-10-16 12:46:02 +0300198 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000199 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a2009-10-16 12:46:02 +0300200 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000201 default:
Amit Kucheria625120a2009-10-16 12:46:02 +0300202 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000203 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800204}
205
206static ssize_t
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700207trip_point_type_activate(struct device *dev, struct device_attribute *attr,
208 const char *buf, size_t count)
209{
210 struct thermal_zone_device *tz = to_thermal_zone(dev);
211 int trip, result;
212
213 if (!tz->ops->activate_trip_type)
214 return -EPERM;
215
216 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
217 return -EINVAL;
218
219 if (!strncmp(buf, "enabled", sizeof("enabled")))
220 result = tz->ops->activate_trip_type(tz, trip,
221 THERMAL_TRIP_ACTIVATION_ENABLED);
222 else if (!strncmp(buf, "disabled", sizeof("disabled")))
223 result = tz->ops->activate_trip_type(tz, trip,
224 THERMAL_TRIP_ACTIVATION_DISABLED);
225 else
226 result = -EINVAL;
227
228 if (result)
229 return result;
230
231 return count;
232}
233
234static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800235trip_point_temp_show(struct device *dev, struct device_attribute *attr,
236 char *buf)
237{
238 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000239 int trip, ret;
240 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800241
242 if (!tz->ops->get_trip_temp)
243 return -EPERM;
244
245 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
246 return -EINVAL;
247
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000248 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
249
250 if (ret)
251 return ret;
252
253 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800254}
255
Matthew Garrett03a971a2008-12-03 18:00:38 +0000256static ssize_t
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257trip_point_temp_set(struct device *dev, struct device_attribute *attr,
258 const char *buf, size_t count)
259{
260 struct thermal_zone_device *tz = to_thermal_zone(dev);
261 int trip, ret;
262 long temperature;
263
264 if (!tz->ops->set_trip_temp)
265 return -EPERM;
266
267 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
268 return -EINVAL;
269
270 if (!sscanf(buf, "%ld", &temperature))
271 return -EINVAL;
272
273 ret = tz->ops->set_trip_temp(tz, trip, temperature);
274 if (ret)
275 return ret;
276
277 return count;
278}
279
280static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000281passive_store(struct device *dev, struct device_attribute *attr,
282 const char *buf, size_t count)
283{
284 struct thermal_zone_device *tz = to_thermal_zone(dev);
285 struct thermal_cooling_device *cdev = NULL;
286 int state;
287
288 if (!sscanf(buf, "%d\n", &state))
289 return -EINVAL;
290
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100291 /* sanity check: values below 1000 millicelcius don't make sense
292 * and can cause the system to go into a thermal heart attack
293 */
294 if (state && state < 1000)
295 return -EINVAL;
296
Matthew Garrett03a971a2008-12-03 18:00:38 +0000297 if (state && !tz->forced_passive) {
298 mutex_lock(&thermal_list_lock);
299 list_for_each_entry(cdev, &thermal_cdev_list, node) {
300 if (!strncmp("Processor", cdev->type,
301 sizeof("Processor")))
302 thermal_zone_bind_cooling_device(tz,
303 THERMAL_TRIPS_NONE,
304 cdev);
305 }
306 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100307 if (!tz->passive_delay)
308 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000309 } else if (!state && tz->forced_passive) {
310 mutex_lock(&thermal_list_lock);
311 list_for_each_entry(cdev, &thermal_cdev_list, node) {
312 if (!strncmp("Processor", cdev->type,
313 sizeof("Processor")))
314 thermal_zone_unbind_cooling_device(tz,
315 THERMAL_TRIPS_NONE,
316 cdev);
317 }
318 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100319 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000320 }
321
322 tz->tc1 = 1;
323 tz->tc2 = 1;
324
Matthew Garrett03a971a2008-12-03 18:00:38 +0000325 tz->forced_passive = state;
326
327 thermal_zone_device_update(tz);
328
329 return count;
330}
331
332static ssize_t
333passive_show(struct device *dev, struct device_attribute *attr,
334 char *buf)
335{
336 struct thermal_zone_device *tz = to_thermal_zone(dev);
337
338 return sprintf(buf, "%d\n", tz->forced_passive);
339}
340
Zhang Rui203d3d42008-01-17 15:51:08 +0800341static DEVICE_ATTR(type, 0444, type_show, NULL);
342static DEVICE_ATTR(temp, 0444, temp_show, NULL);
343static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000344static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, \
345 passive_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800346
347static struct device_attribute trip_point_attrs[] = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348 __ATTR(trip_point_0_type, 0644, trip_point_type_show,
349 trip_point_type_activate),
350 __ATTR(trip_point_0_temp, 0644, trip_point_temp_show,
351 trip_point_temp_set),
352 __ATTR(trip_point_1_type, 0644, trip_point_type_show,
353 trip_point_type_activate),
354 __ATTR(trip_point_1_temp, 0644, trip_point_temp_show,
355 trip_point_temp_set),
356 __ATTR(trip_point_2_type, 0644, trip_point_type_show,
357 trip_point_type_activate),
358 __ATTR(trip_point_2_temp, 0644, trip_point_temp_show,
359 trip_point_temp_set),
360 __ATTR(trip_point_3_type, 0644, trip_point_type_show,
361 trip_point_type_activate),
362 __ATTR(trip_point_3_temp, 0644, trip_point_temp_show,
363 trip_point_temp_set),
364 __ATTR(trip_point_4_type, 0644, trip_point_type_show,
365 trip_point_type_activate),
366 __ATTR(trip_point_4_temp, 0644, trip_point_temp_show,
367 trip_point_temp_set),
368 __ATTR(trip_point_5_type, 0644, trip_point_type_show,
369 trip_point_type_activate),
370 __ATTR(trip_point_5_temp, 0644, trip_point_temp_show,
371 trip_point_temp_set),
372 __ATTR(trip_point_6_type, 0644, trip_point_type_show,
373 trip_point_type_activate),
374 __ATTR(trip_point_6_temp, 0644, trip_point_temp_show,
375 trip_point_temp_set),
376 __ATTR(trip_point_7_type, 0644, trip_point_type_show,
377 trip_point_type_activate),
378 __ATTR(trip_point_7_temp, 0644, trip_point_temp_show,
379 trip_point_temp_set),
380 __ATTR(trip_point_8_type, 0644, trip_point_type_show,
381 trip_point_type_activate),
382 __ATTR(trip_point_8_temp, 0644, trip_point_temp_show,
383 trip_point_temp_set),
384 __ATTR(trip_point_9_type, 0644, trip_point_type_show,
385 trip_point_type_activate),
386 __ATTR(trip_point_9_temp, 0644, trip_point_temp_show,
387 trip_point_temp_set),
388 __ATTR(trip_point_10_type, 0644, trip_point_type_show,
389 trip_point_type_activate),
390 __ATTR(trip_point_10_temp, 0644, trip_point_temp_show,
391 trip_point_temp_set),
392 __ATTR(trip_point_11_type, 0644, trip_point_type_show,
393 trip_point_type_activate),
394 __ATTR(trip_point_11_temp, 0644, trip_point_temp_show,
395 trip_point_temp_set),
Zhang Rui203d3d42008-01-17 15:51:08 +0800396};
397
398#define TRIP_POINT_ATTR_ADD(_dev, _index, result) \
399do { \
400 result = device_create_file(_dev, \
401 &trip_point_attrs[_index * 2]); \
402 if (result) \
403 break; \
404 result = device_create_file(_dev, \
405 &trip_point_attrs[_index * 2 + 1]); \
406} while (0)
407
408#define TRIP_POINT_ATTR_REMOVE(_dev, _index) \
409do { \
410 device_remove_file(_dev, &trip_point_attrs[_index * 2]); \
411 device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \
412} while (0)
413
414/* sys I/F for cooling device */
415#define to_cooling_device(_dev) \
416 container_of(_dev, struct thermal_cooling_device, device)
417
418static ssize_t
419thermal_cooling_device_type_show(struct device *dev,
420 struct device_attribute *attr, char *buf)
421{
422 struct thermal_cooling_device *cdev = to_cooling_device(dev);
423
424 return sprintf(buf, "%s\n", cdev->type);
425}
426
427static ssize_t
428thermal_cooling_device_max_state_show(struct device *dev,
429 struct device_attribute *attr, char *buf)
430{
431 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000432 unsigned long state;
433 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800434
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000435 ret = cdev->ops->get_max_state(cdev, &state);
436 if (ret)
437 return ret;
438 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800439}
440
441static ssize_t
442thermal_cooling_device_cur_state_show(struct device *dev,
443 struct device_attribute *attr, char *buf)
444{
445 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000446 unsigned long state;
447 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800448
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000449 ret = cdev->ops->get_cur_state(cdev, &state);
450 if (ret)
451 return ret;
452 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800453}
454
455static ssize_t
456thermal_cooling_device_cur_state_store(struct device *dev,
457 struct device_attribute *attr,
458 const char *buf, size_t count)
459{
460 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000461 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800462 int result;
463
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000464 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800465 return -EINVAL;
466
Roel Kluinedb94912009-12-15 22:46:50 +0100467 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800468 return -EINVAL;
469
470 result = cdev->ops->set_cur_state(cdev, state);
471 if (result)
472 return result;
473 return count;
474}
475
476static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500477__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800478static DEVICE_ATTR(max_state, 0444,
479 thermal_cooling_device_max_state_show, NULL);
480static DEVICE_ATTR(cur_state, 0644,
481 thermal_cooling_device_cur_state_show,
482 thermal_cooling_device_cur_state_store);
483
484static ssize_t
485thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500486 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800487{
488 struct thermal_cooling_device_instance *instance;
489
490 instance =
491 container_of(attr, struct thermal_cooling_device_instance, attr);
492
493 if (instance->trip == THERMAL_TRIPS_NONE)
494 return sprintf(buf, "-1\n");
495 else
496 return sprintf(buf, "%d\n", instance->trip);
497}
498
499/* Device management */
500
Rene Herman16d75232008-06-24 19:38:56 +0200501#if defined(CONFIG_THERMAL_HWMON)
502
Zhang Ruie68b16a2008-04-21 16:07:52 +0800503/* hwmon sys I/F */
504#include <linux/hwmon.h>
505static LIST_HEAD(thermal_hwmon_list);
506
507static ssize_t
508name_show(struct device *dev, struct device_attribute *attr, char *buf)
509{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700510 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800511 return sprintf(buf, "%s\n", hwmon->type);
512}
513static DEVICE_ATTR(name, 0444, name_show, NULL);
514
515static ssize_t
516temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
517{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000518 long temperature;
519 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800520 struct thermal_hwmon_attr *hwmon_attr
521 = container_of(attr, struct thermal_hwmon_attr, attr);
522 struct thermal_zone_device *tz
523 = container_of(hwmon_attr, struct thermal_zone_device,
524 temp_input);
525
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000526 ret = tz->ops->get_temp(tz, &temperature);
527
528 if (ret)
529 return ret;
530
531 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800532}
533
534static ssize_t
535temp_crit_show(struct device *dev, struct device_attribute *attr,
536 char *buf)
537{
538 struct thermal_hwmon_attr *hwmon_attr
539 = container_of(attr, struct thermal_hwmon_attr, attr);
540 struct thermal_zone_device *tz
541 = container_of(hwmon_attr, struct thermal_zone_device,
542 temp_crit);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000543 long temperature;
544 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800545
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000546 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
547 if (ret)
548 return ret;
549
550 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800551}
552
553
554static int
555thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
556{
557 struct thermal_hwmon_device *hwmon;
558 int new_hwmon_device = 1;
559 int result;
560
561 mutex_lock(&thermal_list_lock);
562 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
563 if (!strcmp(hwmon->type, tz->type)) {
564 new_hwmon_device = 0;
565 mutex_unlock(&thermal_list_lock);
566 goto register_sys_interface;
567 }
568 mutex_unlock(&thermal_list_lock);
569
570 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
571 if (!hwmon)
572 return -ENOMEM;
573
574 INIT_LIST_HEAD(&hwmon->tz_list);
575 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
576 hwmon->device = hwmon_device_register(NULL);
577 if (IS_ERR(hwmon->device)) {
578 result = PTR_ERR(hwmon->device);
579 goto free_mem;
580 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700581 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800582 result = device_create_file(hwmon->device, &dev_attr_name);
583 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530584 goto free_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800585
586 register_sys_interface:
587 tz->hwmon = hwmon;
588 hwmon->count++;
589
590 snprintf(tz->temp_input.name, THERMAL_NAME_LENGTH,
591 "temp%d_input", hwmon->count);
592 tz->temp_input.attr.attr.name = tz->temp_input.name;
593 tz->temp_input.attr.attr.mode = 0444;
594 tz->temp_input.attr.show = temp_input_show;
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700595 sysfs_attr_init(&tz->temp_input.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800596 result = device_create_file(hwmon->device, &tz->temp_input.attr);
597 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530598 goto unregister_name;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800599
600 if (tz->ops->get_crit_temp) {
601 unsigned long temperature;
602 if (!tz->ops->get_crit_temp(tz, &temperature)) {
603 snprintf(tz->temp_crit.name, THERMAL_NAME_LENGTH,
604 "temp%d_crit", hwmon->count);
605 tz->temp_crit.attr.attr.name = tz->temp_crit.name;
606 tz->temp_crit.attr.attr.mode = 0444;
607 tz->temp_crit.attr.show = temp_crit_show;
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700608 sysfs_attr_init(&tz->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800609 result = device_create_file(hwmon->device,
610 &tz->temp_crit.attr);
611 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530612 goto unregister_input;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800613 }
614 }
615
616 mutex_lock(&thermal_list_lock);
617 if (new_hwmon_device)
618 list_add_tail(&hwmon->node, &thermal_hwmon_list);
619 list_add_tail(&tz->hwmon_node, &hwmon->tz_list);
620 mutex_unlock(&thermal_list_lock);
621
622 return 0;
623
Durgadoss Rb299eb52011-03-03 04:30:13 +0530624 unregister_input:
Zhang Ruie68b16a2008-04-21 16:07:52 +0800625 device_remove_file(hwmon->device, &tz->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530626 unregister_name:
Zhang Ruie68b16a2008-04-21 16:07:52 +0800627 if (new_hwmon_device) {
628 device_remove_file(hwmon->device, &dev_attr_name);
629 hwmon_device_unregister(hwmon->device);
630 }
631 free_mem:
632 if (new_hwmon_device)
633 kfree(hwmon);
634
635 return result;
636}
637
638static void
639thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
640{
641 struct thermal_hwmon_device *hwmon = tz->hwmon;
642
643 tz->hwmon = NULL;
644 device_remove_file(hwmon->device, &tz->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530645 if (tz->ops->get_crit_temp)
646 device_remove_file(hwmon->device, &tz->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800647
648 mutex_lock(&thermal_list_lock);
649 list_del(&tz->hwmon_node);
650 if (!list_empty(&hwmon->tz_list)) {
651 mutex_unlock(&thermal_list_lock);
652 return;
653 }
654 list_del(&hwmon->node);
655 mutex_unlock(&thermal_list_lock);
656
657 device_remove_file(hwmon->device, &dev_attr_name);
658 hwmon_device_unregister(hwmon->device);
659 kfree(hwmon);
660}
661#else
662static int
663thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
664{
665 return 0;
666}
667
668static void
669thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
670{
671}
672#endif
673
Matthew Garrettb1569e92008-12-03 17:55:32 +0000674static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
675 int delay)
676{
677 cancel_delayed_work(&(tz->poll_queue));
678
679 if (!delay)
680 return;
681
682 if (delay > 1000)
683 schedule_delayed_work(&(tz->poll_queue),
684 round_jiffies(msecs_to_jiffies(delay)));
685 else
686 schedule_delayed_work(&(tz->poll_queue),
687 msecs_to_jiffies(delay));
688}
689
690static void thermal_zone_device_passive(struct thermal_zone_device *tz,
691 int temp, int trip_temp, int trip)
692{
693 int trend = 0;
694 struct thermal_cooling_device_instance *instance;
695 struct thermal_cooling_device *cdev;
696 long state, max_state;
697
698 /*
699 * Above Trip?
700 * -----------
701 * Calculate the thermal trend (using the passive cooling equation)
702 * and modify the performance limit for all passive cooling devices
703 * accordingly. Note that we assume symmetry.
704 */
705 if (temp >= trip_temp) {
706 tz->passive = true;
707
708 trend = (tz->tc1 * (temp - tz->last_temperature)) +
709 (tz->tc2 * (temp - trip_temp));
710
711 /* Heating up? */
712 if (trend > 0) {
713 list_for_each_entry(instance, &tz->cooling_devices,
714 node) {
715 if (instance->trip != trip)
716 continue;
717 cdev = instance->cdev;
718 cdev->ops->get_cur_state(cdev, &state);
719 cdev->ops->get_max_state(cdev, &max_state);
720 if (state++ < max_state)
721 cdev->ops->set_cur_state(cdev, state);
722 }
723 } else if (trend < 0) { /* Cooling off? */
724 list_for_each_entry(instance, &tz->cooling_devices,
725 node) {
726 if (instance->trip != trip)
727 continue;
728 cdev = instance->cdev;
729 cdev->ops->get_cur_state(cdev, &state);
730 cdev->ops->get_max_state(cdev, &max_state);
731 if (state > 0)
732 cdev->ops->set_cur_state(cdev, --state);
733 }
734 }
735 return;
736 }
737
738 /*
739 * Below Trip?
740 * -----------
741 * Implement passive cooling hysteresis to slowly increase performance
742 * and avoid thrashing around the passive trip point. Note that we
743 * assume symmetry.
744 */
745 list_for_each_entry(instance, &tz->cooling_devices, node) {
746 if (instance->trip != trip)
747 continue;
748 cdev = instance->cdev;
749 cdev->ops->get_cur_state(cdev, &state);
750 cdev->ops->get_max_state(cdev, &max_state);
751 if (state > 0)
752 cdev->ops->set_cur_state(cdev, --state);
753 if (state == 0)
754 tz->passive = false;
755 }
756}
757
758static void thermal_zone_device_check(struct work_struct *work)
759{
760 struct thermal_zone_device *tz = container_of(work, struct
761 thermal_zone_device,
762 poll_queue.work);
763 thermal_zone_device_update(tz);
764}
Zhang Ruie68b16a2008-04-21 16:07:52 +0800765
Zhang Rui203d3d42008-01-17 15:51:08 +0800766/**
767 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800768 * @tz: thermal zone device
769 * @trip: indicates which trip point the cooling devices is
770 * associated with in this thermal zone.
771 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500772 *
773 * This function is usually called in the thermal zone device .bind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800774 */
775int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
776 int trip,
777 struct thermal_cooling_device *cdev)
778{
779 struct thermal_cooling_device_instance *dev;
780 struct thermal_cooling_device_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500781 struct thermal_zone_device *pos1;
782 struct thermal_cooling_device *pos2;
Zhang Rui203d3d42008-01-17 15:51:08 +0800783 int result;
784
Len Brown543a9562008-02-07 16:55:08 -0500785 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800786 return -EINVAL;
787
Thomas Sujithc7516702008-02-15 00:58:50 -0500788 list_for_each_entry(pos1, &thermal_tz_list, node) {
789 if (pos1 == tz)
790 break;
791 }
792 list_for_each_entry(pos2, &thermal_cdev_list, node) {
793 if (pos2 == cdev)
794 break;
795 }
796
797 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800798 return -EINVAL;
799
800 dev =
801 kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL);
802 if (!dev)
803 return -ENOMEM;
804 dev->tz = tz;
805 dev->cdev = cdev;
806 dev->trip = trip;
807 result = get_idr(&tz->idr, &tz->lock, &dev->id);
808 if (result)
809 goto free_mem;
810
811 sprintf(dev->name, "cdev%d", dev->id);
812 result =
813 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
814 if (result)
815 goto release_idr;
816
817 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700818 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800819 dev->attr.attr.name = dev->attr_name;
820 dev->attr.attr.mode = 0444;
821 dev->attr.show = thermal_cooling_device_trip_point_show;
822 result = device_create_file(&tz->device, &dev->attr);
823 if (result)
824 goto remove_symbol_link;
825
826 mutex_lock(&tz->lock);
827 list_for_each_entry(pos, &tz->cooling_devices, node)
828 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
829 result = -EEXIST;
830 break;
831 }
832 if (!result)
833 list_add_tail(&dev->node, &tz->cooling_devices);
834 mutex_unlock(&tz->lock);
835
836 if (!result)
837 return 0;
838
839 device_remove_file(&tz->device, &dev->attr);
840 remove_symbol_link:
841 sysfs_remove_link(&tz->device.kobj, dev->name);
842 release_idr:
843 release_idr(&tz->idr, &tz->lock, dev->id);
844 free_mem:
845 kfree(dev);
846 return result;
847}
Len Brown543a9562008-02-07 16:55:08 -0500848
Zhang Rui203d3d42008-01-17 15:51:08 +0800849EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
850
851/**
852 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800853 * @tz: thermal zone device
854 * @trip: indicates which trip point the cooling devices is
855 * associated with in this thermal zone.
856 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500857 *
858 * This function is usually called in the thermal zone device .unbind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800859 */
860int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
861 int trip,
862 struct thermal_cooling_device *cdev)
863{
864 struct thermal_cooling_device_instance *pos, *next;
865
866 mutex_lock(&tz->lock);
867 list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
Len Brown543a9562008-02-07 16:55:08 -0500868 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800869 list_del(&pos->node);
870 mutex_unlock(&tz->lock);
871 goto unbind;
872 }
873 }
874 mutex_unlock(&tz->lock);
875
876 return -ENODEV;
877
878 unbind:
879 device_remove_file(&tz->device, &pos->attr);
880 sysfs_remove_link(&tz->device.kobj, pos->name);
881 release_idr(&tz->idr, &tz->lock, pos->id);
882 kfree(pos);
883 return 0;
884}
Len Brown543a9562008-02-07 16:55:08 -0500885
Zhang Rui203d3d42008-01-17 15:51:08 +0800886EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
887
888static void thermal_release(struct device *dev)
889{
890 struct thermal_zone_device *tz;
891 struct thermal_cooling_device *cdev;
892
Kay Sievers354655e2009-01-06 10:44:37 -0800893 if (!strncmp(dev_name(dev), "thermal_zone", sizeof "thermal_zone" - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800894 tz = to_thermal_zone(dev);
895 kfree(tz);
896 } else {
897 cdev = to_cooling_device(dev);
898 kfree(cdev);
899 }
900}
901
902static struct class thermal_class = {
903 .name = "thermal",
904 .dev_release = thermal_release,
905};
906
907/**
908 * thermal_cooling_device_register - register a new thermal cooling device
909 * @type: the thermal cooling device type.
910 * @devdata: device private data.
911 * @ops: standard thermal cooling devices callbacks.
912 */
Alan Cox5b275ce2010-11-11 15:27:29 +0000913struct thermal_cooling_device *thermal_cooling_device_register(
914 char *type, void *devdata, const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800915{
916 struct thermal_cooling_device *cdev;
917 struct thermal_zone_device *pos;
918 int result;
919
920 if (strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500921 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800922
923 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -0500924 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500925 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800926
927 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
928 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500929 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +0800930
931 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
932 if (result) {
933 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500934 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800935 }
936
937 strcpy(cdev->type, type);
938 cdev->ops = ops;
939 cdev->device.class = &thermal_class;
940 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -0800941 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800942 result = device_register(&cdev->device);
943 if (result) {
944 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
945 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500946 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800947 }
948
949 /* sys I/F */
950 if (type) {
Len Brown543a9562008-02-07 16:55:08 -0500951 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +0800952 if (result)
953 goto unregister;
954 }
955
956 result = device_create_file(&cdev->device, &dev_attr_max_state);
957 if (result)
958 goto unregister;
959
960 result = device_create_file(&cdev->device, &dev_attr_cur_state);
961 if (result)
962 goto unregister;
963
964 mutex_lock(&thermal_list_lock);
965 list_add(&cdev->node, &thermal_cdev_list);
966 list_for_each_entry(pos, &thermal_tz_list, node) {
967 if (!pos->ops->bind)
968 continue;
969 result = pos->ops->bind(pos, cdev);
970 if (result)
971 break;
972
973 }
974 mutex_unlock(&thermal_list_lock);
975
976 if (!result)
977 return cdev;
978
979 unregister:
980 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
981 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500982 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800983}
Len Brown543a9562008-02-07 16:55:08 -0500984
Zhang Rui203d3d42008-01-17 15:51:08 +0800985EXPORT_SYMBOL(thermal_cooling_device_register);
986
987/**
988 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +0800989 * @cdev: the thermal cooling device to remove.
990 *
991 * thermal_cooling_device_unregister() must be called when the device is no
992 * longer needed.
993 */
994void thermal_cooling_device_unregister(struct
995 thermal_cooling_device
996 *cdev)
997{
998 struct thermal_zone_device *tz;
999 struct thermal_cooling_device *pos = NULL;
1000
1001 if (!cdev)
1002 return;
1003
1004 mutex_lock(&thermal_list_lock);
1005 list_for_each_entry(pos, &thermal_cdev_list, node)
1006 if (pos == cdev)
1007 break;
1008 if (pos != cdev) {
1009 /* thermal cooling device not found */
1010 mutex_unlock(&thermal_list_lock);
1011 return;
1012 }
1013 list_del(&cdev->node);
1014 list_for_each_entry(tz, &thermal_tz_list, node) {
1015 if (!tz->ops->unbind)
1016 continue;
1017 tz->ops->unbind(tz, cdev);
1018 }
1019 mutex_unlock(&thermal_list_lock);
1020 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001021 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001022 device_remove_file(&cdev->device, &dev_attr_max_state);
1023 device_remove_file(&cdev->device, &dev_attr_cur_state);
1024
1025 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1026 device_unregister(&cdev->device);
1027 return;
1028}
Len Brown543a9562008-02-07 16:55:08 -05001029
Zhang Rui203d3d42008-01-17 15:51:08 +08001030EXPORT_SYMBOL(thermal_cooling_device_unregister);
1031
1032/**
Matthew Garrettb1569e92008-12-03 17:55:32 +00001033 * thermal_zone_device_update - force an update of a thermal zone's state
1034 * @ttz: the thermal zone to update
1035 */
1036
1037void thermal_zone_device_update(struct thermal_zone_device *tz)
1038{
1039 int count, ret = 0;
1040 long temp, trip_temp;
1041 enum thermal_trip_type trip_type;
1042 struct thermal_cooling_device_instance *instance;
1043 struct thermal_cooling_device *cdev;
1044
1045 mutex_lock(&tz->lock);
1046
Michael Brunner0d288162009-08-26 14:29:25 -07001047 if (tz->ops->get_temp(tz, &temp)) {
1048 /* get_temp failed - retry it later */
1049 printk(KERN_WARNING PREFIX "failed to read out thermal zone "
1050 "%d\n", tz->id);
1051 goto leave;
1052 }
Matthew Garrettb1569e92008-12-03 17:55:32 +00001053
1054 for (count = 0; count < tz->trips; count++) {
1055 tz->ops->get_trip_type(tz, count, &trip_type);
1056 tz->ops->get_trip_temp(tz, count, &trip_temp);
1057
1058 switch (trip_type) {
1059 case THERMAL_TRIP_CRITICAL:
Vladimir Zajac29321352009-05-06 19:34:21 +02001060 if (temp >= trip_temp) {
Matthew Garrettb1569e92008-12-03 17:55:32 +00001061 if (tz->ops->notify)
1062 ret = tz->ops->notify(tz, count,
1063 trip_type);
1064 if (!ret) {
1065 printk(KERN_EMERG
1066 "Critical temperature reached (%ld C), shutting down.\n",
1067 temp/1000);
1068 orderly_poweroff(true);
1069 }
1070 }
1071 break;
1072 case THERMAL_TRIP_HOT:
Vladimir Zajac29321352009-05-06 19:34:21 +02001073 if (temp >= trip_temp)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001074 if (tz->ops->notify)
1075 tz->ops->notify(tz, count, trip_type);
1076 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001077 case THERMAL_TRIP_CONFIGURABLE_HI:
1078 if (temp >= trip_temp)
1079 if (tz->ops->notify)
1080 tz->ops->notify(tz, count, trip_type);
1081 break;
1082 case THERMAL_TRIP_CONFIGURABLE_LOW:
1083 if (temp <= trip_temp)
1084 if (tz->ops->notify)
1085 tz->ops->notify(tz, count, trip_type);
1086 break;
1087 case THERMAL_TRIP_CRITICAL_LOW:
1088 if (temp <= trip_temp) {
1089 if (tz->ops->notify)
1090 ret = tz->ops->notify(tz, count,
1091 trip_type);
1092 if (!ret) {
1093 printk(KERN_EMERG
1094 "Critical temperature reached (%ld C), \
1095 shutting down.\n", temp/1000);
1096 orderly_poweroff(true);
1097 }
1098 }
1099 break;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001100 case THERMAL_TRIP_ACTIVE:
1101 list_for_each_entry(instance, &tz->cooling_devices,
1102 node) {
1103 if (instance->trip != count)
1104 continue;
1105
1106 cdev = instance->cdev;
1107
Vladimir Zajac29321352009-05-06 19:34:21 +02001108 if (temp >= trip_temp)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001109 cdev->ops->set_cur_state(cdev, 1);
1110 else
1111 cdev->ops->set_cur_state(cdev, 0);
1112 }
1113 break;
1114 case THERMAL_TRIP_PASSIVE:
Vladimir Zajac29321352009-05-06 19:34:21 +02001115 if (temp >= trip_temp || tz->passive)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001116 thermal_zone_device_passive(tz, temp,
1117 trip_temp, count);
1118 break;
1119 }
1120 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001121
1122 if (tz->forced_passive)
1123 thermal_zone_device_passive(tz, temp, tz->forced_passive,
1124 THERMAL_TRIPS_NONE);
1125
Matthew Garrettb1569e92008-12-03 17:55:32 +00001126 tz->last_temperature = temp;
Michael Brunner0d288162009-08-26 14:29:25 -07001127
1128 leave:
Matthew Garrettb1569e92008-12-03 17:55:32 +00001129 if (tz->passive)
1130 thermal_zone_device_set_polling(tz, tz->passive_delay);
1131 else if (tz->polling_delay)
1132 thermal_zone_device_set_polling(tz, tz->polling_delay);
Frans Pop3767cb52009-10-26 08:39:04 +01001133 else
1134 thermal_zone_device_set_polling(tz, 0);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001135 mutex_unlock(&tz->lock);
1136}
1137EXPORT_SYMBOL(thermal_zone_device_update);
1138
1139/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001140 * thermal_zone_device_register - register a new thermal zone device
1141 * @type: the thermal zone device type
1142 * @trips: the number of trip points the thermal zone support
1143 * @devdata: private device data
1144 * @ops: standard thermal zone device callbacks
Matthew Garrettb1569e92008-12-03 17:55:32 +00001145 * @tc1: thermal coefficient 1 for passive calculations
1146 * @tc2: thermal coefficient 2 for passive calculations
1147 * @passive_delay: number of milliseconds to wait between polls when
1148 * performing passive cooling
1149 * @polling_delay: number of milliseconds to wait between polls when checking
1150 * whether trip points have been crossed (0 for interrupt
1151 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001152 *
1153 * thermal_zone_device_unregister() must be called when the device is no
Matthew Garrettb1569e92008-12-03 17:55:32 +00001154 * longer needed. The passive cooling formula uses tc1 and tc2 as described in
1155 * section 11.1.5.1 of the ACPI specification 3.0.
Zhang Rui203d3d42008-01-17 15:51:08 +08001156 */
1157struct thermal_zone_device *thermal_zone_device_register(char *type,
Alan Cox5b275ce2010-11-11 15:27:29 +00001158 int trips, void *devdata,
1159 const struct thermal_zone_device_ops *ops,
1160 int tc1, int tc2, int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001161{
1162 struct thermal_zone_device *tz;
1163 struct thermal_cooling_device *pos;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001164 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001165 int result;
1166 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001167 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001168
1169 if (strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001170 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001171
1172 if (trips > THERMAL_MAX_TRIPS || trips < 0)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001173 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001174
1175 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001176 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001177
1178 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1179 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001180 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001181
1182 INIT_LIST_HEAD(&tz->cooling_devices);
1183 idr_init(&tz->idr);
1184 mutex_init(&tz->lock);
1185 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1186 if (result) {
1187 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001188 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001189 }
1190
1191 strcpy(tz->type, type);
1192 tz->ops = ops;
1193 tz->device.class = &thermal_class;
1194 tz->devdata = devdata;
1195 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001196 tz->tc1 = tc1;
1197 tz->tc2 = tc2;
1198 tz->passive_delay = passive_delay;
1199 tz->polling_delay = polling_delay;
1200
Kay Sievers354655e2009-01-06 10:44:37 -08001201 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001202 result = device_register(&tz->device);
1203 if (result) {
1204 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1205 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001206 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001207 }
1208
1209 /* sys I/F */
1210 if (type) {
1211 result = device_create_file(&tz->device, &dev_attr_type);
1212 if (result)
1213 goto unregister;
1214 }
1215
1216 result = device_create_file(&tz->device, &dev_attr_temp);
1217 if (result)
1218 goto unregister;
1219
1220 if (ops->get_mode) {
1221 result = device_create_file(&tz->device, &dev_attr_mode);
1222 if (result)
1223 goto unregister;
1224 }
1225
1226 for (count = 0; count < trips; count++) {
1227 TRIP_POINT_ATTR_ADD(&tz->device, count, result);
1228 if (result)
1229 goto unregister;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001230 tz->ops->get_trip_type(tz, count, &trip_type);
1231 if (trip_type == THERMAL_TRIP_PASSIVE)
1232 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001233 }
1234
Matthew Garrett03a971a2008-12-03 18:00:38 +00001235 if (!passive)
1236 result = device_create_file(&tz->device,
1237 &dev_attr_passive);
1238
1239 if (result)
1240 goto unregister;
1241
Zhang Ruie68b16a2008-04-21 16:07:52 +08001242 result = thermal_add_hwmon_sysfs(tz);
1243 if (result)
1244 goto unregister;
1245
Zhang Rui203d3d42008-01-17 15:51:08 +08001246 mutex_lock(&thermal_list_lock);
1247 list_add_tail(&tz->node, &thermal_tz_list);
1248 if (ops->bind)
1249 list_for_each_entry(pos, &thermal_cdev_list, node) {
Len Brown543a9562008-02-07 16:55:08 -05001250 result = ops->bind(tz, pos);
1251 if (result)
1252 break;
Zhang Rui203d3d42008-01-17 15:51:08 +08001253 }
1254 mutex_unlock(&thermal_list_lock);
1255
Matthew Garrettb1569e92008-12-03 17:55:32 +00001256 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1257
1258 thermal_zone_device_update(tz);
1259
Zhang Rui203d3d42008-01-17 15:51:08 +08001260 if (!result)
1261 return tz;
1262
1263 unregister:
1264 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1265 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001266 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001267}
Len Brown543a9562008-02-07 16:55:08 -05001268
Zhang Rui203d3d42008-01-17 15:51:08 +08001269EXPORT_SYMBOL(thermal_zone_device_register);
1270
1271/**
1272 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001273 * @tz: the thermal zone device to remove
1274 */
1275void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1276{
1277 struct thermal_cooling_device *cdev;
1278 struct thermal_zone_device *pos = NULL;
1279 int count;
1280
1281 if (!tz)
1282 return;
1283
1284 mutex_lock(&thermal_list_lock);
1285 list_for_each_entry(pos, &thermal_tz_list, node)
1286 if (pos == tz)
1287 break;
1288 if (pos != tz) {
1289 /* thermal zone device not found */
1290 mutex_unlock(&thermal_list_lock);
1291 return;
1292 }
1293 list_del(&tz->node);
1294 if (tz->ops->unbind)
1295 list_for_each_entry(cdev, &thermal_cdev_list, node)
1296 tz->ops->unbind(tz, cdev);
1297 mutex_unlock(&thermal_list_lock);
1298
Matthew Garrettb1569e92008-12-03 17:55:32 +00001299 thermal_zone_device_set_polling(tz, 0);
1300
Zhang Rui203d3d42008-01-17 15:51:08 +08001301 if (tz->type[0])
1302 device_remove_file(&tz->device, &dev_attr_type);
1303 device_remove_file(&tz->device, &dev_attr_temp);
1304 if (tz->ops->get_mode)
1305 device_remove_file(&tz->device, &dev_attr_mode);
1306
1307 for (count = 0; count < tz->trips; count++)
1308 TRIP_POINT_ATTR_REMOVE(&tz->device, count);
1309
Zhang Ruie68b16a2008-04-21 16:07:52 +08001310 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001311 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1312 idr_destroy(&tz->idr);
1313 mutex_destroy(&tz->lock);
1314 device_unregister(&tz->device);
1315 return;
1316}
Len Brown543a9562008-02-07 16:55:08 -05001317
Zhang Rui203d3d42008-01-17 15:51:08 +08001318EXPORT_SYMBOL(thermal_zone_device_unregister);
1319
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001320#ifdef CONFIG_NET
1321static struct genl_family thermal_event_genl_family = {
1322 .id = GENL_ID_GENERATE,
1323 .name = THERMAL_GENL_FAMILY_NAME,
1324 .version = THERMAL_GENL_VERSION,
1325 .maxattr = THERMAL_GENL_ATTR_MAX,
1326};
1327
1328static struct genl_multicast_group thermal_event_mcgrp = {
1329 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1330};
1331
R.Durgadoss4cb18722010-10-27 03:33:29 +05301332int generate_netlink_event(u32 orig, enum events event)
1333{
1334 struct sk_buff *skb;
1335 struct nlattr *attr;
1336 struct thermal_genl_event *thermal_event;
1337 void *msg_header;
1338 int size;
1339 int result;
1340
1341 /* allocate memory */
1342 size = nla_total_size(sizeof(struct thermal_genl_event)) + \
1343 nla_total_size(0);
1344
1345 skb = genlmsg_new(size, GFP_ATOMIC);
1346 if (!skb)
1347 return -ENOMEM;
1348
1349 /* add the genetlink message header */
1350 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1351 &thermal_event_genl_family, 0,
1352 THERMAL_GENL_CMD_EVENT);
1353 if (!msg_header) {
1354 nlmsg_free(skb);
1355 return -ENOMEM;
1356 }
1357
1358 /* fill the data */
1359 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT, \
1360 sizeof(struct thermal_genl_event));
1361
1362 if (!attr) {
1363 nlmsg_free(skb);
1364 return -EINVAL;
1365 }
1366
1367 thermal_event = nla_data(attr);
1368 if (!thermal_event) {
1369 nlmsg_free(skb);
1370 return -EINVAL;
1371 }
1372
1373 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1374
1375 thermal_event->orig = orig;
1376 thermal_event->event = event;
1377
1378 /* send multicast genetlink message */
1379 result = genlmsg_end(skb, msg_header);
1380 if (result < 0) {
1381 nlmsg_free(skb);
1382 return result;
1383 }
1384
1385 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1386 if (result)
1387 printk(KERN_INFO "failed to send netlink event:%d", result);
1388
1389 return result;
1390}
1391EXPORT_SYMBOL(generate_netlink_event);
1392
1393static int genetlink_init(void)
1394{
1395 int result;
1396
1397 result = genl_register_family(&thermal_event_genl_family);
1398 if (result)
1399 return result;
1400
1401 result = genl_register_mc_group(&thermal_event_genl_family,
1402 &thermal_event_mcgrp);
1403 if (result)
1404 genl_unregister_family(&thermal_event_genl_family);
1405 return result;
1406}
1407
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001408static void genetlink_exit(void)
1409{
1410 genl_unregister_family(&thermal_event_genl_family);
1411}
1412#else /* !CONFIG_NET */
1413static inline int genetlink_init(void) { return 0; }
1414static inline void genetlink_exit(void) {}
1415#endif /* !CONFIG_NET */
1416
Zhang Rui203d3d42008-01-17 15:51:08 +08001417static int __init thermal_init(void)
1418{
1419 int result = 0;
1420
1421 result = class_register(&thermal_class);
1422 if (result) {
1423 idr_destroy(&thermal_tz_idr);
1424 idr_destroy(&thermal_cdev_idr);
1425 mutex_destroy(&thermal_idr_lock);
1426 mutex_destroy(&thermal_list_lock);
1427 }
R.Durgadoss4cb18722010-10-27 03:33:29 +05301428 result = genetlink_init();
Zhang Rui203d3d42008-01-17 15:51:08 +08001429 return result;
1430}
1431
1432static void __exit thermal_exit(void)
1433{
1434 class_unregister(&thermal_class);
1435 idr_destroy(&thermal_tz_idr);
1436 idr_destroy(&thermal_cdev_idr);
1437 mutex_destroy(&thermal_idr_lock);
1438 mutex_destroy(&thermal_list_lock);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301439 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001440}
1441
R.Durgadoss4cb18722010-10-27 03:33:29 +05301442fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001443module_exit(thermal_exit);