blob: 71802caec2c3dfce48d908c3b865b38e6aad8b06 [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
63static int get_idr(struct idr *idr, struct mutex *lock, int *id)
64{
65 int err;
66
67 again:
68 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
69 return -ENOMEM;
70
71 if (lock)
72 mutex_lock(lock);
73 err = idr_get_new(idr, NULL, id);
74 if (lock)
75 mutex_unlock(lock);
76 if (unlikely(err == -EAGAIN))
77 goto again;
78 else if (unlikely(err))
79 return err;
80
81 *id = *id & MAX_ID_MASK;
82 return 0;
83}
84
85static void release_idr(struct idr *idr, struct mutex *lock, int id)
86{
87 if (lock)
88 mutex_lock(lock);
89 idr_remove(idr, id);
90 if (lock)
91 mutex_unlock(lock);
92}
93
94/* sys I/F for thermal zone */
95
96#define to_thermal_zone(_dev) \
97 container_of(_dev, struct thermal_zone_device, device)
98
99static ssize_t
100type_show(struct device *dev, struct device_attribute *attr, char *buf)
101{
102 struct thermal_zone_device *tz = to_thermal_zone(dev);
103
104 return sprintf(buf, "%s\n", tz->type);
105}
106
107static ssize_t
108temp_show(struct device *dev, struct device_attribute *attr, char *buf)
109{
110 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000111 long temperature;
112 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800113
114 if (!tz->ops->get_temp)
115 return -EPERM;
116
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000117 ret = tz->ops->get_temp(tz, &temperature);
118
119 if (ret)
120 return ret;
121
122 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800123}
124
125static ssize_t
126mode_show(struct device *dev, struct device_attribute *attr, char *buf)
127{
128 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000129 enum thermal_device_mode mode;
130 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800131
132 if (!tz->ops->get_mode)
133 return -EPERM;
134
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000135 result = tz->ops->get_mode(tz, &mode);
136 if (result)
137 return result;
138
139 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
140 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800141}
142
143static ssize_t
144mode_store(struct device *dev, struct device_attribute *attr,
145 const char *buf, size_t count)
146{
147 struct thermal_zone_device *tz = to_thermal_zone(dev);
148 int result;
149
150 if (!tz->ops->set_mode)
151 return -EPERM;
152
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000153 if (!strncmp(buf, "enabled", sizeof("enabled")))
154 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
155 else if (!strncmp(buf, "disabled", sizeof("disabled")))
156 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
157 else
158 result = -EINVAL;
159
Zhang Rui203d3d42008-01-17 15:51:08 +0800160 if (result)
161 return result;
162
163 return count;
164}
165
166static ssize_t
167trip_point_type_show(struct device *dev, struct device_attribute *attr,
168 char *buf)
169{
170 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000171 enum thermal_trip_type type;
172 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800173
174 if (!tz->ops->get_trip_type)
175 return -EPERM;
176
177 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
178 return -EINVAL;
179
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000180 result = tz->ops->get_trip_type(tz, trip, &type);
181 if (result)
182 return result;
183
184 switch (type) {
185 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300186 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000187 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300188 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000189 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300190 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000191 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300192 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000193 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300194 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000195 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800196}
197
198static ssize_t
199trip_point_temp_show(struct device *dev, struct device_attribute *attr,
200 char *buf)
201{
202 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000203 int trip, ret;
204 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800205
206 if (!tz->ops->get_trip_temp)
207 return -EPERM;
208
209 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
210 return -EINVAL;
211
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000212 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
213
214 if (ret)
215 return ret;
216
217 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800218}
219
Matthew Garrett03a971a2008-12-03 18:00:38 +0000220static ssize_t
221passive_store(struct device *dev, struct device_attribute *attr,
222 const char *buf, size_t count)
223{
224 struct thermal_zone_device *tz = to_thermal_zone(dev);
225 struct thermal_cooling_device *cdev = NULL;
226 int state;
227
228 if (!sscanf(buf, "%d\n", &state))
229 return -EINVAL;
230
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100231 /* sanity check: values below 1000 millicelcius don't make sense
232 * and can cause the system to go into a thermal heart attack
233 */
234 if (state && state < 1000)
235 return -EINVAL;
236
Matthew Garrett03a971a2008-12-03 18:00:38 +0000237 if (state && !tz->forced_passive) {
238 mutex_lock(&thermal_list_lock);
239 list_for_each_entry(cdev, &thermal_cdev_list, node) {
240 if (!strncmp("Processor", cdev->type,
241 sizeof("Processor")))
242 thermal_zone_bind_cooling_device(tz,
243 THERMAL_TRIPS_NONE,
244 cdev);
245 }
246 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100247 if (!tz->passive_delay)
248 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000249 } else if (!state && tz->forced_passive) {
250 mutex_lock(&thermal_list_lock);
251 list_for_each_entry(cdev, &thermal_cdev_list, node) {
252 if (!strncmp("Processor", cdev->type,
253 sizeof("Processor")))
254 thermal_zone_unbind_cooling_device(tz,
255 THERMAL_TRIPS_NONE,
256 cdev);
257 }
258 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100259 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000260 }
261
262 tz->tc1 = 1;
263 tz->tc2 = 1;
264
Matthew Garrett03a971a2008-12-03 18:00:38 +0000265 tz->forced_passive = state;
266
267 thermal_zone_device_update(tz);
268
269 return count;
270}
271
272static ssize_t
273passive_show(struct device *dev, struct device_attribute *attr,
274 char *buf)
275{
276 struct thermal_zone_device *tz = to_thermal_zone(dev);
277
278 return sprintf(buf, "%d\n", tz->forced_passive);
279}
280
Zhang Rui203d3d42008-01-17 15:51:08 +0800281static DEVICE_ATTR(type, 0444, type_show, NULL);
282static DEVICE_ATTR(temp, 0444, temp_show, NULL);
283static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700284static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800285
286static struct device_attribute trip_point_attrs[] = {
287 __ATTR(trip_point_0_type, 0444, trip_point_type_show, NULL),
288 __ATTR(trip_point_0_temp, 0444, trip_point_temp_show, NULL),
289 __ATTR(trip_point_1_type, 0444, trip_point_type_show, NULL),
290 __ATTR(trip_point_1_temp, 0444, trip_point_temp_show, NULL),
291 __ATTR(trip_point_2_type, 0444, trip_point_type_show, NULL),
292 __ATTR(trip_point_2_temp, 0444, trip_point_temp_show, NULL),
293 __ATTR(trip_point_3_type, 0444, trip_point_type_show, NULL),
294 __ATTR(trip_point_3_temp, 0444, trip_point_temp_show, NULL),
295 __ATTR(trip_point_4_type, 0444, trip_point_type_show, NULL),
296 __ATTR(trip_point_4_temp, 0444, trip_point_temp_show, NULL),
297 __ATTR(trip_point_5_type, 0444, trip_point_type_show, NULL),
298 __ATTR(trip_point_5_temp, 0444, trip_point_temp_show, NULL),
299 __ATTR(trip_point_6_type, 0444, trip_point_type_show, NULL),
300 __ATTR(trip_point_6_temp, 0444, trip_point_temp_show, NULL),
301 __ATTR(trip_point_7_type, 0444, trip_point_type_show, NULL),
302 __ATTR(trip_point_7_temp, 0444, trip_point_temp_show, NULL),
303 __ATTR(trip_point_8_type, 0444, trip_point_type_show, NULL),
304 __ATTR(trip_point_8_temp, 0444, trip_point_temp_show, NULL),
305 __ATTR(trip_point_9_type, 0444, trip_point_type_show, NULL),
306 __ATTR(trip_point_9_temp, 0444, trip_point_temp_show, NULL),
Krzysztof Helt5f1a3f22008-04-15 14:34:47 -0700307 __ATTR(trip_point_10_type, 0444, trip_point_type_show, NULL),
308 __ATTR(trip_point_10_temp, 0444, trip_point_temp_show, NULL),
309 __ATTR(trip_point_11_type, 0444, trip_point_type_show, NULL),
310 __ATTR(trip_point_11_temp, 0444, trip_point_temp_show, NULL),
Zhang Rui203d3d42008-01-17 15:51:08 +0800311};
312
313#define TRIP_POINT_ATTR_ADD(_dev, _index, result) \
314do { \
315 result = device_create_file(_dev, \
316 &trip_point_attrs[_index * 2]); \
317 if (result) \
318 break; \
319 result = device_create_file(_dev, \
320 &trip_point_attrs[_index * 2 + 1]); \
321} while (0)
322
323#define TRIP_POINT_ATTR_REMOVE(_dev, _index) \
324do { \
325 device_remove_file(_dev, &trip_point_attrs[_index * 2]); \
326 device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \
327} while (0)
328
329/* sys I/F for cooling device */
330#define to_cooling_device(_dev) \
331 container_of(_dev, struct thermal_cooling_device, device)
332
333static ssize_t
334thermal_cooling_device_type_show(struct device *dev,
335 struct device_attribute *attr, char *buf)
336{
337 struct thermal_cooling_device *cdev = to_cooling_device(dev);
338
339 return sprintf(buf, "%s\n", cdev->type);
340}
341
342static ssize_t
343thermal_cooling_device_max_state_show(struct device *dev,
344 struct device_attribute *attr, char *buf)
345{
346 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000347 unsigned long state;
348 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800349
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000350 ret = cdev->ops->get_max_state(cdev, &state);
351 if (ret)
352 return ret;
353 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800354}
355
356static ssize_t
357thermal_cooling_device_cur_state_show(struct device *dev,
358 struct device_attribute *attr, char *buf)
359{
360 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000361 unsigned long state;
362 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800363
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000364 ret = cdev->ops->get_cur_state(cdev, &state);
365 if (ret)
366 return ret;
367 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800368}
369
370static ssize_t
371thermal_cooling_device_cur_state_store(struct device *dev,
372 struct device_attribute *attr,
373 const char *buf, size_t count)
374{
375 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000376 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800377 int result;
378
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000379 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800380 return -EINVAL;
381
Roel Kluinedb94912009-12-15 22:46:50 +0100382 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800383 return -EINVAL;
384
385 result = cdev->ops->set_cur_state(cdev, state);
386 if (result)
387 return result;
388 return count;
389}
390
391static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500392__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800393static DEVICE_ATTR(max_state, 0444,
394 thermal_cooling_device_max_state_show, NULL);
395static DEVICE_ATTR(cur_state, 0644,
396 thermal_cooling_device_cur_state_show,
397 thermal_cooling_device_cur_state_store);
398
399static ssize_t
400thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500401 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800402{
403 struct thermal_cooling_device_instance *instance;
404
405 instance =
406 container_of(attr, struct thermal_cooling_device_instance, attr);
407
408 if (instance->trip == THERMAL_TRIPS_NONE)
409 return sprintf(buf, "-1\n");
410 else
411 return sprintf(buf, "%d\n", instance->trip);
412}
413
414/* Device management */
415
Rene Herman16d75232008-06-24 19:38:56 +0200416#if defined(CONFIG_THERMAL_HWMON)
417
Zhang Ruie68b16a2008-04-21 16:07:52 +0800418/* hwmon sys I/F */
419#include <linux/hwmon.h>
Jean Delvare31f53962011-07-28 13:48:42 -0700420
421/* thermal zone devices with the same type share one hwmon device */
422struct thermal_hwmon_device {
423 char type[THERMAL_NAME_LENGTH];
424 struct device *device;
425 int count;
426 struct list_head tz_list;
427 struct list_head node;
428};
429
430struct thermal_hwmon_attr {
431 struct device_attribute attr;
432 char name[16];
433};
434
435/* one temperature input for each thermal zone */
436struct thermal_hwmon_temp {
437 struct list_head hwmon_node;
438 struct thermal_zone_device *tz;
439 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
440 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
441};
442
Zhang Ruie68b16a2008-04-21 16:07:52 +0800443static LIST_HEAD(thermal_hwmon_list);
444
445static ssize_t
446name_show(struct device *dev, struct device_attribute *attr, char *buf)
447{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700448 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800449 return sprintf(buf, "%s\n", hwmon->type);
450}
451static DEVICE_ATTR(name, 0444, name_show, NULL);
452
453static ssize_t
454temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
455{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000456 long temperature;
457 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800458 struct thermal_hwmon_attr *hwmon_attr
459 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700460 struct thermal_hwmon_temp *temp
461 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800462 temp_input);
Jean Delvare31f53962011-07-28 13:48:42 -0700463 struct thermal_zone_device *tz = temp->tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800464
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000465 ret = tz->ops->get_temp(tz, &temperature);
466
467 if (ret)
468 return ret;
469
470 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800471}
472
473static ssize_t
474temp_crit_show(struct device *dev, struct device_attribute *attr,
475 char *buf)
476{
477 struct thermal_hwmon_attr *hwmon_attr
478 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700479 struct thermal_hwmon_temp *temp
480 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800481 temp_crit);
Jean Delvare31f53962011-07-28 13:48:42 -0700482 struct thermal_zone_device *tz = temp->tz;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000483 long temperature;
484 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800485
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000486 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
487 if (ret)
488 return ret;
489
490 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800491}
492
493
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700494static struct thermal_hwmon_device *
495thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
496{
497 struct thermal_hwmon_device *hwmon;
498
499 mutex_lock(&thermal_list_lock);
500 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
501 if (!strcmp(hwmon->type, tz->type)) {
502 mutex_unlock(&thermal_list_lock);
503 return hwmon;
504 }
505 mutex_unlock(&thermal_list_lock);
506
507 return NULL;
508}
509
Jean Delvare31f53962011-07-28 13:48:42 -0700510/* Find the temperature input matching a given thermal zone */
511static struct thermal_hwmon_temp *
512thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
513 const struct thermal_zone_device *tz)
514{
515 struct thermal_hwmon_temp *temp;
516
517 mutex_lock(&thermal_list_lock);
518 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
519 if (temp->tz == tz) {
520 mutex_unlock(&thermal_list_lock);
521 return temp;
522 }
523 mutex_unlock(&thermal_list_lock);
524
525 return NULL;
526}
527
Zhang Ruie68b16a2008-04-21 16:07:52 +0800528static int
529thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
530{
531 struct thermal_hwmon_device *hwmon;
Jean Delvare31f53962011-07-28 13:48:42 -0700532 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800533 int new_hwmon_device = 1;
534 int result;
535
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700536 hwmon = thermal_hwmon_lookup_by_type(tz);
537 if (hwmon) {
538 new_hwmon_device = 0;
539 goto register_sys_interface;
540 }
Zhang Ruie68b16a2008-04-21 16:07:52 +0800541
542 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
543 if (!hwmon)
544 return -ENOMEM;
545
546 INIT_LIST_HEAD(&hwmon->tz_list);
547 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
548 hwmon->device = hwmon_device_register(NULL);
549 if (IS_ERR(hwmon->device)) {
550 result = PTR_ERR(hwmon->device);
551 goto free_mem;
552 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700553 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800554 result = device_create_file(hwmon->device, &dev_attr_name);
555 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530556 goto free_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800557
558 register_sys_interface:
Jean Delvare31f53962011-07-28 13:48:42 -0700559 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
560 if (!temp) {
561 result = -ENOMEM;
562 goto unregister_name;
563 }
564
565 temp->tz = tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800566 hwmon->count++;
567
Jean Delvare31f53962011-07-28 13:48:42 -0700568 snprintf(temp->temp_input.name, THERMAL_NAME_LENGTH,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800569 "temp%d_input", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700570 temp->temp_input.attr.attr.name = temp->temp_input.name;
571 temp->temp_input.attr.attr.mode = 0444;
572 temp->temp_input.attr.show = temp_input_show;
573 sysfs_attr_init(&temp->temp_input.attr.attr);
574 result = device_create_file(hwmon->device, &temp->temp_input.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800575 if (result)
Jean Delvare31f53962011-07-28 13:48:42 -0700576 goto free_temp_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800577
578 if (tz->ops->get_crit_temp) {
579 unsigned long temperature;
580 if (!tz->ops->get_crit_temp(tz, &temperature)) {
Jean Delvare31f53962011-07-28 13:48:42 -0700581 snprintf(temp->temp_crit.name, THERMAL_NAME_LENGTH,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800582 "temp%d_crit", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700583 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
584 temp->temp_crit.attr.attr.mode = 0444;
585 temp->temp_crit.attr.show = temp_crit_show;
586 sysfs_attr_init(&temp->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800587 result = device_create_file(hwmon->device,
Jean Delvare31f53962011-07-28 13:48:42 -0700588 &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800589 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530590 goto unregister_input;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800591 }
592 }
593
594 mutex_lock(&thermal_list_lock);
595 if (new_hwmon_device)
596 list_add_tail(&hwmon->node, &thermal_hwmon_list);
Jean Delvare31f53962011-07-28 13:48:42 -0700597 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800598 mutex_unlock(&thermal_list_lock);
599
600 return 0;
601
Durgadoss Rb299eb52011-03-03 04:30:13 +0530602 unregister_input:
Jean Delvare31f53962011-07-28 13:48:42 -0700603 device_remove_file(hwmon->device, &temp->temp_input.attr);
604 free_temp_mem:
605 kfree(temp);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530606 unregister_name:
Zhang Ruie68b16a2008-04-21 16:07:52 +0800607 if (new_hwmon_device) {
608 device_remove_file(hwmon->device, &dev_attr_name);
609 hwmon_device_unregister(hwmon->device);
610 }
611 free_mem:
612 if (new_hwmon_device)
613 kfree(hwmon);
614
615 return result;
616}
617
618static void
619thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
620{
Jean Delvare31f53962011-07-28 13:48:42 -0700621 struct thermal_hwmon_device *hwmon;
622 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800623
Jean Delvare31f53962011-07-28 13:48:42 -0700624 hwmon = thermal_hwmon_lookup_by_type(tz);
625 if (unlikely(!hwmon)) {
626 /* Should never happen... */
627 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
628 return;
629 }
630
631 temp = thermal_hwmon_lookup_temp(hwmon, tz);
632 if (unlikely(!temp)) {
633 /* Should never happen... */
634 dev_dbg(&tz->device, "temperature input lookup failed!\n");
635 return;
636 }
637
638 device_remove_file(hwmon->device, &temp->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530639 if (tz->ops->get_crit_temp)
Jean Delvare31f53962011-07-28 13:48:42 -0700640 device_remove_file(hwmon->device, &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800641
642 mutex_lock(&thermal_list_lock);
Jean Delvare31f53962011-07-28 13:48:42 -0700643 list_del(&temp->hwmon_node);
644 kfree(temp);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800645 if (!list_empty(&hwmon->tz_list)) {
646 mutex_unlock(&thermal_list_lock);
647 return;
648 }
649 list_del(&hwmon->node);
650 mutex_unlock(&thermal_list_lock);
651
652 device_remove_file(hwmon->device, &dev_attr_name);
653 hwmon_device_unregister(hwmon->device);
654 kfree(hwmon);
655}
656#else
657static int
658thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
659{
660 return 0;
661}
662
663static void
664thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
665{
666}
667#endif
668
Matthew Garrettb1569e92008-12-03 17:55:32 +0000669static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
670 int delay)
671{
672 cancel_delayed_work(&(tz->poll_queue));
673
674 if (!delay)
675 return;
676
677 if (delay > 1000)
Rafael J. Wysocki51e20d02011-11-06 14:21:38 +0100678 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
Matthew Garrettb1569e92008-12-03 17:55:32 +0000679 round_jiffies(msecs_to_jiffies(delay)));
680 else
Rafael J. Wysocki51e20d02011-11-06 14:21:38 +0100681 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
Matthew Garrettb1569e92008-12-03 17:55:32 +0000682 msecs_to_jiffies(delay));
683}
684
685static void thermal_zone_device_passive(struct thermal_zone_device *tz,
686 int temp, int trip_temp, int trip)
687{
688 int trend = 0;
689 struct thermal_cooling_device_instance *instance;
690 struct thermal_cooling_device *cdev;
691 long state, max_state;
692
693 /*
694 * Above Trip?
695 * -----------
696 * Calculate the thermal trend (using the passive cooling equation)
697 * and modify the performance limit for all passive cooling devices
698 * accordingly. Note that we assume symmetry.
699 */
700 if (temp >= trip_temp) {
701 tz->passive = true;
702
703 trend = (tz->tc1 * (temp - tz->last_temperature)) +
704 (tz->tc2 * (temp - trip_temp));
705
706 /* Heating up? */
707 if (trend > 0) {
708 list_for_each_entry(instance, &tz->cooling_devices,
709 node) {
710 if (instance->trip != trip)
711 continue;
712 cdev = instance->cdev;
713 cdev->ops->get_cur_state(cdev, &state);
714 cdev->ops->get_max_state(cdev, &max_state);
715 if (state++ < max_state)
716 cdev->ops->set_cur_state(cdev, state);
717 }
718 } else if (trend < 0) { /* Cooling off? */
719 list_for_each_entry(instance, &tz->cooling_devices,
720 node) {
721 if (instance->trip != trip)
722 continue;
723 cdev = instance->cdev;
724 cdev->ops->get_cur_state(cdev, &state);
725 cdev->ops->get_max_state(cdev, &max_state);
726 if (state > 0)
727 cdev->ops->set_cur_state(cdev, --state);
728 }
729 }
730 return;
731 }
732
733 /*
734 * Below Trip?
735 * -----------
736 * Implement passive cooling hysteresis to slowly increase performance
737 * and avoid thrashing around the passive trip point. Note that we
738 * assume symmetry.
739 */
740 list_for_each_entry(instance, &tz->cooling_devices, node) {
741 if (instance->trip != trip)
742 continue;
743 cdev = instance->cdev;
744 cdev->ops->get_cur_state(cdev, &state);
745 cdev->ops->get_max_state(cdev, &max_state);
746 if (state > 0)
747 cdev->ops->set_cur_state(cdev, --state);
748 if (state == 0)
749 tz->passive = false;
750 }
751}
752
753static void thermal_zone_device_check(struct work_struct *work)
754{
755 struct thermal_zone_device *tz = container_of(work, struct
756 thermal_zone_device,
757 poll_queue.work);
758 thermal_zone_device_update(tz);
759}
Zhang Ruie68b16a2008-04-21 16:07:52 +0800760
Zhang Rui203d3d42008-01-17 15:51:08 +0800761/**
762 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800763 * @tz: thermal zone device
764 * @trip: indicates which trip point the cooling devices is
765 * associated with in this thermal zone.
766 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500767 *
768 * This function is usually called in the thermal zone device .bind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800769 */
770int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
771 int trip,
772 struct thermal_cooling_device *cdev)
773{
774 struct thermal_cooling_device_instance *dev;
775 struct thermal_cooling_device_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500776 struct thermal_zone_device *pos1;
777 struct thermal_cooling_device *pos2;
Zhang Rui203d3d42008-01-17 15:51:08 +0800778 int result;
779
Len Brown543a9562008-02-07 16:55:08 -0500780 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800781 return -EINVAL;
782
Thomas Sujithc7516702008-02-15 00:58:50 -0500783 list_for_each_entry(pos1, &thermal_tz_list, node) {
784 if (pos1 == tz)
785 break;
786 }
787 list_for_each_entry(pos2, &thermal_cdev_list, node) {
788 if (pos2 == cdev)
789 break;
790 }
791
792 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800793 return -EINVAL;
794
795 dev =
796 kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL);
797 if (!dev)
798 return -ENOMEM;
799 dev->tz = tz;
800 dev->cdev = cdev;
801 dev->trip = trip;
802 result = get_idr(&tz->idr, &tz->lock, &dev->id);
803 if (result)
804 goto free_mem;
805
806 sprintf(dev->name, "cdev%d", dev->id);
807 result =
808 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
809 if (result)
810 goto release_idr;
811
812 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700813 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800814 dev->attr.attr.name = dev->attr_name;
815 dev->attr.attr.mode = 0444;
816 dev->attr.show = thermal_cooling_device_trip_point_show;
817 result = device_create_file(&tz->device, &dev->attr);
818 if (result)
819 goto remove_symbol_link;
820
821 mutex_lock(&tz->lock);
822 list_for_each_entry(pos, &tz->cooling_devices, node)
823 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
824 result = -EEXIST;
825 break;
826 }
827 if (!result)
828 list_add_tail(&dev->node, &tz->cooling_devices);
829 mutex_unlock(&tz->lock);
830
831 if (!result)
832 return 0;
833
834 device_remove_file(&tz->device, &dev->attr);
835 remove_symbol_link:
836 sysfs_remove_link(&tz->device.kobj, dev->name);
837 release_idr:
838 release_idr(&tz->idr, &tz->lock, dev->id);
839 free_mem:
840 kfree(dev);
841 return result;
842}
Len Brown543a9562008-02-07 16:55:08 -0500843
Zhang Rui203d3d42008-01-17 15:51:08 +0800844EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
845
846/**
847 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800848 * @tz: thermal zone device
849 * @trip: indicates which trip point the cooling devices is
850 * associated with in this thermal zone.
851 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500852 *
853 * This function is usually called in the thermal zone device .unbind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800854 */
855int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
856 int trip,
857 struct thermal_cooling_device *cdev)
858{
859 struct thermal_cooling_device_instance *pos, *next;
860
861 mutex_lock(&tz->lock);
862 list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
Len Brown543a9562008-02-07 16:55:08 -0500863 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800864 list_del(&pos->node);
865 mutex_unlock(&tz->lock);
866 goto unbind;
867 }
868 }
869 mutex_unlock(&tz->lock);
870
871 return -ENODEV;
872
873 unbind:
874 device_remove_file(&tz->device, &pos->attr);
875 sysfs_remove_link(&tz->device.kobj, pos->name);
876 release_idr(&tz->idr, &tz->lock, pos->id);
877 kfree(pos);
878 return 0;
879}
Len Brown543a9562008-02-07 16:55:08 -0500880
Zhang Rui203d3d42008-01-17 15:51:08 +0800881EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
882
883static void thermal_release(struct device *dev)
884{
885 struct thermal_zone_device *tz;
886 struct thermal_cooling_device *cdev;
887
Kay Sievers354655e2009-01-06 10:44:37 -0800888 if (!strncmp(dev_name(dev), "thermal_zone", sizeof "thermal_zone" - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800889 tz = to_thermal_zone(dev);
890 kfree(tz);
891 } else {
892 cdev = to_cooling_device(dev);
893 kfree(cdev);
894 }
895}
896
897static struct class thermal_class = {
898 .name = "thermal",
899 .dev_release = thermal_release,
900};
901
902/**
903 * thermal_cooling_device_register - register a new thermal cooling device
904 * @type: the thermal cooling device type.
905 * @devdata: device private data.
906 * @ops: standard thermal cooling devices callbacks.
907 */
Alan Cox5b275ce2010-11-11 15:27:29 +0000908struct thermal_cooling_device *thermal_cooling_device_register(
909 char *type, void *devdata, const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800910{
911 struct thermal_cooling_device *cdev;
912 struct thermal_zone_device *pos;
913 int result;
914
915 if (strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500916 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800917
918 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -0500919 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500920 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800921
922 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
923 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500924 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +0800925
926 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
927 if (result) {
928 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500929 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800930 }
931
932 strcpy(cdev->type, type);
933 cdev->ops = ops;
934 cdev->device.class = &thermal_class;
935 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -0800936 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800937 result = device_register(&cdev->device);
938 if (result) {
939 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
940 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500941 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800942 }
943
944 /* sys I/F */
945 if (type) {
Len Brown543a9562008-02-07 16:55:08 -0500946 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +0800947 if (result)
948 goto unregister;
949 }
950
951 result = device_create_file(&cdev->device, &dev_attr_max_state);
952 if (result)
953 goto unregister;
954
955 result = device_create_file(&cdev->device, &dev_attr_cur_state);
956 if (result)
957 goto unregister;
958
959 mutex_lock(&thermal_list_lock);
960 list_add(&cdev->node, &thermal_cdev_list);
961 list_for_each_entry(pos, &thermal_tz_list, node) {
962 if (!pos->ops->bind)
963 continue;
964 result = pos->ops->bind(pos, cdev);
965 if (result)
966 break;
967
968 }
969 mutex_unlock(&thermal_list_lock);
970
971 if (!result)
972 return cdev;
973
974 unregister:
975 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
976 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500977 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800978}
Len Brown543a9562008-02-07 16:55:08 -0500979
Zhang Rui203d3d42008-01-17 15:51:08 +0800980EXPORT_SYMBOL(thermal_cooling_device_register);
981
982/**
983 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +0800984 * @cdev: the thermal cooling device to remove.
985 *
986 * thermal_cooling_device_unregister() must be called when the device is no
987 * longer needed.
988 */
989void thermal_cooling_device_unregister(struct
990 thermal_cooling_device
991 *cdev)
992{
993 struct thermal_zone_device *tz;
994 struct thermal_cooling_device *pos = NULL;
995
996 if (!cdev)
997 return;
998
999 mutex_lock(&thermal_list_lock);
1000 list_for_each_entry(pos, &thermal_cdev_list, node)
1001 if (pos == cdev)
1002 break;
1003 if (pos != cdev) {
1004 /* thermal cooling device not found */
1005 mutex_unlock(&thermal_list_lock);
1006 return;
1007 }
1008 list_del(&cdev->node);
1009 list_for_each_entry(tz, &thermal_tz_list, node) {
1010 if (!tz->ops->unbind)
1011 continue;
1012 tz->ops->unbind(tz, cdev);
1013 }
1014 mutex_unlock(&thermal_list_lock);
1015 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001016 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001017 device_remove_file(&cdev->device, &dev_attr_max_state);
1018 device_remove_file(&cdev->device, &dev_attr_cur_state);
1019
1020 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1021 device_unregister(&cdev->device);
1022 return;
1023}
Len Brown543a9562008-02-07 16:55:08 -05001024
Zhang Rui203d3d42008-01-17 15:51:08 +08001025EXPORT_SYMBOL(thermal_cooling_device_unregister);
1026
1027/**
Matthew Garrettb1569e92008-12-03 17:55:32 +00001028 * thermal_zone_device_update - force an update of a thermal zone's state
1029 * @ttz: the thermal zone to update
1030 */
1031
1032void thermal_zone_device_update(struct thermal_zone_device *tz)
1033{
1034 int count, ret = 0;
1035 long temp, trip_temp;
1036 enum thermal_trip_type trip_type;
1037 struct thermal_cooling_device_instance *instance;
1038 struct thermal_cooling_device *cdev;
1039
1040 mutex_lock(&tz->lock);
1041
Michael Brunner0d288162009-08-26 14:29:25 -07001042 if (tz->ops->get_temp(tz, &temp)) {
1043 /* get_temp failed - retry it later */
1044 printk(KERN_WARNING PREFIX "failed to read out thermal zone "
1045 "%d\n", tz->id);
1046 goto leave;
1047 }
Matthew Garrettb1569e92008-12-03 17:55:32 +00001048
1049 for (count = 0; count < tz->trips; count++) {
1050 tz->ops->get_trip_type(tz, count, &trip_type);
1051 tz->ops->get_trip_temp(tz, count, &trip_temp);
1052
1053 switch (trip_type) {
1054 case THERMAL_TRIP_CRITICAL:
Vladimir Zajac29321352009-05-06 19:34:21 +02001055 if (temp >= trip_temp) {
Matthew Garrettb1569e92008-12-03 17:55:32 +00001056 if (tz->ops->notify)
1057 ret = tz->ops->notify(tz, count,
1058 trip_type);
1059 if (!ret) {
1060 printk(KERN_EMERG
1061 "Critical temperature reached (%ld C), shutting down.\n",
1062 temp/1000);
1063 orderly_poweroff(true);
1064 }
1065 }
1066 break;
1067 case THERMAL_TRIP_HOT:
Vladimir Zajac29321352009-05-06 19:34:21 +02001068 if (temp >= trip_temp)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001069 if (tz->ops->notify)
1070 tz->ops->notify(tz, count, trip_type);
1071 break;
1072 case THERMAL_TRIP_ACTIVE:
1073 list_for_each_entry(instance, &tz->cooling_devices,
1074 node) {
1075 if (instance->trip != count)
1076 continue;
1077
1078 cdev = instance->cdev;
1079
Vladimir Zajac29321352009-05-06 19:34:21 +02001080 if (temp >= trip_temp)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001081 cdev->ops->set_cur_state(cdev, 1);
1082 else
1083 cdev->ops->set_cur_state(cdev, 0);
1084 }
1085 break;
1086 case THERMAL_TRIP_PASSIVE:
Vladimir Zajac29321352009-05-06 19:34:21 +02001087 if (temp >= trip_temp || tz->passive)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001088 thermal_zone_device_passive(tz, temp,
1089 trip_temp, count);
1090 break;
1091 }
1092 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001093
1094 if (tz->forced_passive)
1095 thermal_zone_device_passive(tz, temp, tz->forced_passive,
1096 THERMAL_TRIPS_NONE);
1097
Matthew Garrettb1569e92008-12-03 17:55:32 +00001098 tz->last_temperature = temp;
Michael Brunner0d288162009-08-26 14:29:25 -07001099
1100 leave:
Matthew Garrettb1569e92008-12-03 17:55:32 +00001101 if (tz->passive)
1102 thermal_zone_device_set_polling(tz, tz->passive_delay);
1103 else if (tz->polling_delay)
1104 thermal_zone_device_set_polling(tz, tz->polling_delay);
Frans Pop3767cb52009-10-26 08:39:04 +01001105 else
1106 thermal_zone_device_set_polling(tz, 0);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001107 mutex_unlock(&tz->lock);
1108}
1109EXPORT_SYMBOL(thermal_zone_device_update);
1110
1111/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001112 * thermal_zone_device_register - register a new thermal zone device
1113 * @type: the thermal zone device type
1114 * @trips: the number of trip points the thermal zone support
1115 * @devdata: private device data
1116 * @ops: standard thermal zone device callbacks
Matthew Garrettb1569e92008-12-03 17:55:32 +00001117 * @tc1: thermal coefficient 1 for passive calculations
1118 * @tc2: thermal coefficient 2 for passive calculations
1119 * @passive_delay: number of milliseconds to wait between polls when
1120 * performing passive cooling
1121 * @polling_delay: number of milliseconds to wait between polls when checking
1122 * whether trip points have been crossed (0 for interrupt
1123 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001124 *
1125 * thermal_zone_device_unregister() must be called when the device is no
Matthew Garrettb1569e92008-12-03 17:55:32 +00001126 * longer needed. The passive cooling formula uses tc1 and tc2 as described in
1127 * section 11.1.5.1 of the ACPI specification 3.0.
Zhang Rui203d3d42008-01-17 15:51:08 +08001128 */
1129struct thermal_zone_device *thermal_zone_device_register(char *type,
Alan Cox5b275ce2010-11-11 15:27:29 +00001130 int trips, void *devdata,
1131 const struct thermal_zone_device_ops *ops,
1132 int tc1, int tc2, int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001133{
1134 struct thermal_zone_device *tz;
1135 struct thermal_cooling_device *pos;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001136 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001137 int result;
1138 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001139 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001140
1141 if (strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001142 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001143
1144 if (trips > THERMAL_MAX_TRIPS || trips < 0)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001145 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001146
1147 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001148 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001149
1150 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1151 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001152 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001153
1154 INIT_LIST_HEAD(&tz->cooling_devices);
1155 idr_init(&tz->idr);
1156 mutex_init(&tz->lock);
1157 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1158 if (result) {
1159 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001160 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001161 }
1162
1163 strcpy(tz->type, type);
1164 tz->ops = ops;
1165 tz->device.class = &thermal_class;
1166 tz->devdata = devdata;
1167 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001168 tz->tc1 = tc1;
1169 tz->tc2 = tc2;
1170 tz->passive_delay = passive_delay;
1171 tz->polling_delay = polling_delay;
1172
Kay Sievers354655e2009-01-06 10:44:37 -08001173 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001174 result = device_register(&tz->device);
1175 if (result) {
1176 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1177 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001178 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001179 }
1180
1181 /* sys I/F */
1182 if (type) {
1183 result = device_create_file(&tz->device, &dev_attr_type);
1184 if (result)
1185 goto unregister;
1186 }
1187
1188 result = device_create_file(&tz->device, &dev_attr_temp);
1189 if (result)
1190 goto unregister;
1191
1192 if (ops->get_mode) {
1193 result = device_create_file(&tz->device, &dev_attr_mode);
1194 if (result)
1195 goto unregister;
1196 }
1197
1198 for (count = 0; count < trips; count++) {
1199 TRIP_POINT_ATTR_ADD(&tz->device, count, result);
1200 if (result)
1201 goto unregister;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001202 tz->ops->get_trip_type(tz, count, &trip_type);
1203 if (trip_type == THERMAL_TRIP_PASSIVE)
1204 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001205 }
1206
Matthew Garrett03a971a2008-12-03 18:00:38 +00001207 if (!passive)
1208 result = device_create_file(&tz->device,
1209 &dev_attr_passive);
1210
1211 if (result)
1212 goto unregister;
1213
Zhang Ruie68b16a2008-04-21 16:07:52 +08001214 result = thermal_add_hwmon_sysfs(tz);
1215 if (result)
1216 goto unregister;
1217
Zhang Rui203d3d42008-01-17 15:51:08 +08001218 mutex_lock(&thermal_list_lock);
1219 list_add_tail(&tz->node, &thermal_tz_list);
1220 if (ops->bind)
1221 list_for_each_entry(pos, &thermal_cdev_list, node) {
Len Brown543a9562008-02-07 16:55:08 -05001222 result = ops->bind(tz, pos);
1223 if (result)
1224 break;
Zhang Rui203d3d42008-01-17 15:51:08 +08001225 }
1226 mutex_unlock(&thermal_list_lock);
1227
Matthew Garrettb1569e92008-12-03 17:55:32 +00001228 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1229
1230 thermal_zone_device_update(tz);
1231
Zhang Rui203d3d42008-01-17 15:51:08 +08001232 if (!result)
1233 return tz;
1234
1235 unregister:
1236 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1237 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001238 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001239}
Len Brown543a9562008-02-07 16:55:08 -05001240
Zhang Rui203d3d42008-01-17 15:51:08 +08001241EXPORT_SYMBOL(thermal_zone_device_register);
1242
1243/**
1244 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001245 * @tz: the thermal zone device to remove
1246 */
1247void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1248{
1249 struct thermal_cooling_device *cdev;
1250 struct thermal_zone_device *pos = NULL;
1251 int count;
1252
1253 if (!tz)
1254 return;
1255
1256 mutex_lock(&thermal_list_lock);
1257 list_for_each_entry(pos, &thermal_tz_list, node)
1258 if (pos == tz)
1259 break;
1260 if (pos != tz) {
1261 /* thermal zone device not found */
1262 mutex_unlock(&thermal_list_lock);
1263 return;
1264 }
1265 list_del(&tz->node);
1266 if (tz->ops->unbind)
1267 list_for_each_entry(cdev, &thermal_cdev_list, node)
1268 tz->ops->unbind(tz, cdev);
1269 mutex_unlock(&thermal_list_lock);
1270
Matthew Garrettb1569e92008-12-03 17:55:32 +00001271 thermal_zone_device_set_polling(tz, 0);
1272
Zhang Rui203d3d42008-01-17 15:51:08 +08001273 if (tz->type[0])
1274 device_remove_file(&tz->device, &dev_attr_type);
1275 device_remove_file(&tz->device, &dev_attr_temp);
1276 if (tz->ops->get_mode)
1277 device_remove_file(&tz->device, &dev_attr_mode);
1278
1279 for (count = 0; count < tz->trips; count++)
1280 TRIP_POINT_ATTR_REMOVE(&tz->device, count);
1281
Zhang Ruie68b16a2008-04-21 16:07:52 +08001282 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001283 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1284 idr_destroy(&tz->idr);
1285 mutex_destroy(&tz->lock);
1286 device_unregister(&tz->device);
1287 return;
1288}
Len Brown543a9562008-02-07 16:55:08 -05001289
Zhang Rui203d3d42008-01-17 15:51:08 +08001290EXPORT_SYMBOL(thermal_zone_device_unregister);
1291
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001292#ifdef CONFIG_NET
1293static struct genl_family thermal_event_genl_family = {
1294 .id = GENL_ID_GENERATE,
1295 .name = THERMAL_GENL_FAMILY_NAME,
1296 .version = THERMAL_GENL_VERSION,
1297 .maxattr = THERMAL_GENL_ATTR_MAX,
1298};
1299
1300static struct genl_multicast_group thermal_event_mcgrp = {
1301 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1302};
1303
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001304int thermal_generate_netlink_event(u32 orig, enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301305{
1306 struct sk_buff *skb;
1307 struct nlattr *attr;
1308 struct thermal_genl_event *thermal_event;
1309 void *msg_header;
1310 int size;
1311 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001312 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301313
1314 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001315 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1316 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301317
1318 skb = genlmsg_new(size, GFP_ATOMIC);
1319 if (!skb)
1320 return -ENOMEM;
1321
1322 /* add the genetlink message header */
1323 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1324 &thermal_event_genl_family, 0,
1325 THERMAL_GENL_CMD_EVENT);
1326 if (!msg_header) {
1327 nlmsg_free(skb);
1328 return -ENOMEM;
1329 }
1330
1331 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001332 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1333 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301334
1335 if (!attr) {
1336 nlmsg_free(skb);
1337 return -EINVAL;
1338 }
1339
1340 thermal_event = nla_data(attr);
1341 if (!thermal_event) {
1342 nlmsg_free(skb);
1343 return -EINVAL;
1344 }
1345
1346 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1347
1348 thermal_event->orig = orig;
1349 thermal_event->event = event;
1350
1351 /* send multicast genetlink message */
1352 result = genlmsg_end(skb, msg_header);
1353 if (result < 0) {
1354 nlmsg_free(skb);
1355 return result;
1356 }
1357
1358 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1359 if (result)
1360 printk(KERN_INFO "failed to send netlink event:%d", result);
1361
1362 return result;
1363}
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001364EXPORT_SYMBOL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301365
1366static int genetlink_init(void)
1367{
1368 int result;
1369
1370 result = genl_register_family(&thermal_event_genl_family);
1371 if (result)
1372 return result;
1373
1374 result = genl_register_mc_group(&thermal_event_genl_family,
1375 &thermal_event_mcgrp);
1376 if (result)
1377 genl_unregister_family(&thermal_event_genl_family);
1378 return result;
1379}
1380
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001381static void genetlink_exit(void)
1382{
1383 genl_unregister_family(&thermal_event_genl_family);
1384}
1385#else /* !CONFIG_NET */
1386static inline int genetlink_init(void) { return 0; }
1387static inline void genetlink_exit(void) {}
1388#endif /* !CONFIG_NET */
1389
Zhang Rui203d3d42008-01-17 15:51:08 +08001390static int __init thermal_init(void)
1391{
1392 int result = 0;
1393
1394 result = class_register(&thermal_class);
1395 if (result) {
1396 idr_destroy(&thermal_tz_idr);
1397 idr_destroy(&thermal_cdev_idr);
1398 mutex_destroy(&thermal_idr_lock);
1399 mutex_destroy(&thermal_list_lock);
1400 }
R.Durgadoss4cb18722010-10-27 03:33:29 +05301401 result = genetlink_init();
Zhang Rui203d3d42008-01-17 15:51:08 +08001402 return result;
1403}
1404
1405static void __exit thermal_exit(void)
1406{
1407 class_unregister(&thermal_class);
1408 idr_destroy(&thermal_tz_idr);
1409 idr_destroy(&thermal_cdev_idr);
1410 mutex_destroy(&thermal_idr_lock);
1411 mutex_destroy(&thermal_list_lock);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301412 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001413}
1414
R.Durgadoss4cb18722010-10-27 03:33:29 +05301415fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001416module_exit(thermal_exit);