blob: b6353d61fac5944bc25cb4044c183fbab80027c7 [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 Kucheria625120a42009-10-16 12:46:02 +0300188 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000189 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300190 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000191 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300192 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000193 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300194 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000195 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300196 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000197 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800198}
199
200static ssize_t
201trip_point_temp_show(struct device *dev, struct device_attribute *attr,
202 char *buf)
203{
204 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000205 int trip, ret;
206 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800207
208 if (!tz->ops->get_trip_temp)
209 return -EPERM;
210
211 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
212 return -EINVAL;
213
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000214 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
215
216 if (ret)
217 return ret;
218
219 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800220}
221
Matthew Garrett03a971a2008-12-03 18:00:38 +0000222static ssize_t
223passive_store(struct device *dev, struct device_attribute *attr,
224 const char *buf, size_t count)
225{
226 struct thermal_zone_device *tz = to_thermal_zone(dev);
227 struct thermal_cooling_device *cdev = NULL;
228 int state;
229
230 if (!sscanf(buf, "%d\n", &state))
231 return -EINVAL;
232
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100233 /* sanity check: values below 1000 millicelcius don't make sense
234 * and can cause the system to go into a thermal heart attack
235 */
236 if (state && state < 1000)
237 return -EINVAL;
238
Matthew Garrett03a971a2008-12-03 18:00:38 +0000239 if (state && !tz->forced_passive) {
240 mutex_lock(&thermal_list_lock);
241 list_for_each_entry(cdev, &thermal_cdev_list, node) {
242 if (!strncmp("Processor", cdev->type,
243 sizeof("Processor")))
244 thermal_zone_bind_cooling_device(tz,
245 THERMAL_TRIPS_NONE,
246 cdev);
247 }
248 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100249 if (!tz->passive_delay)
250 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000251 } else if (!state && tz->forced_passive) {
252 mutex_lock(&thermal_list_lock);
253 list_for_each_entry(cdev, &thermal_cdev_list, node) {
254 if (!strncmp("Processor", cdev->type,
255 sizeof("Processor")))
256 thermal_zone_unbind_cooling_device(tz,
257 THERMAL_TRIPS_NONE,
258 cdev);
259 }
260 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100261 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000262 }
263
264 tz->tc1 = 1;
265 tz->tc2 = 1;
266
Matthew Garrett03a971a2008-12-03 18:00:38 +0000267 tz->forced_passive = state;
268
269 thermal_zone_device_update(tz);
270
271 return count;
272}
273
274static ssize_t
275passive_show(struct device *dev, struct device_attribute *attr,
276 char *buf)
277{
278 struct thermal_zone_device *tz = to_thermal_zone(dev);
279
280 return sprintf(buf, "%d\n", tz->forced_passive);
281}
282
Zhang Rui203d3d42008-01-17 15:51:08 +0800283static DEVICE_ATTR(type, 0444, type_show, NULL);
284static DEVICE_ATTR(temp, 0444, temp_show, NULL);
285static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000286static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, \
287 passive_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800288
289static struct device_attribute trip_point_attrs[] = {
290 __ATTR(trip_point_0_type, 0444, trip_point_type_show, NULL),
291 __ATTR(trip_point_0_temp, 0444, trip_point_temp_show, NULL),
292 __ATTR(trip_point_1_type, 0444, trip_point_type_show, NULL),
293 __ATTR(trip_point_1_temp, 0444, trip_point_temp_show, NULL),
294 __ATTR(trip_point_2_type, 0444, trip_point_type_show, NULL),
295 __ATTR(trip_point_2_temp, 0444, trip_point_temp_show, NULL),
296 __ATTR(trip_point_3_type, 0444, trip_point_type_show, NULL),
297 __ATTR(trip_point_3_temp, 0444, trip_point_temp_show, NULL),
298 __ATTR(trip_point_4_type, 0444, trip_point_type_show, NULL),
299 __ATTR(trip_point_4_temp, 0444, trip_point_temp_show, NULL),
300 __ATTR(trip_point_5_type, 0444, trip_point_type_show, NULL),
301 __ATTR(trip_point_5_temp, 0444, trip_point_temp_show, NULL),
302 __ATTR(trip_point_6_type, 0444, trip_point_type_show, NULL),
303 __ATTR(trip_point_6_temp, 0444, trip_point_temp_show, NULL),
304 __ATTR(trip_point_7_type, 0444, trip_point_type_show, NULL),
305 __ATTR(trip_point_7_temp, 0444, trip_point_temp_show, NULL),
306 __ATTR(trip_point_8_type, 0444, trip_point_type_show, NULL),
307 __ATTR(trip_point_8_temp, 0444, trip_point_temp_show, NULL),
308 __ATTR(trip_point_9_type, 0444, trip_point_type_show, NULL),
309 __ATTR(trip_point_9_temp, 0444, trip_point_temp_show, NULL),
Krzysztof Helt5f1a3f22008-04-15 14:34:47 -0700310 __ATTR(trip_point_10_type, 0444, trip_point_type_show, NULL),
311 __ATTR(trip_point_10_temp, 0444, trip_point_temp_show, NULL),
312 __ATTR(trip_point_11_type, 0444, trip_point_type_show, NULL),
313 __ATTR(trip_point_11_temp, 0444, trip_point_temp_show, NULL),
Zhang Rui203d3d42008-01-17 15:51:08 +0800314};
315
316#define TRIP_POINT_ATTR_ADD(_dev, _index, result) \
317do { \
318 result = device_create_file(_dev, \
319 &trip_point_attrs[_index * 2]); \
320 if (result) \
321 break; \
322 result = device_create_file(_dev, \
323 &trip_point_attrs[_index * 2 + 1]); \
324} while (0)
325
326#define TRIP_POINT_ATTR_REMOVE(_dev, _index) \
327do { \
328 device_remove_file(_dev, &trip_point_attrs[_index * 2]); \
329 device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \
330} while (0)
331
332/* sys I/F for cooling device */
333#define to_cooling_device(_dev) \
334 container_of(_dev, struct thermal_cooling_device, device)
335
336static ssize_t
337thermal_cooling_device_type_show(struct device *dev,
338 struct device_attribute *attr, char *buf)
339{
340 struct thermal_cooling_device *cdev = to_cooling_device(dev);
341
342 return sprintf(buf, "%s\n", cdev->type);
343}
344
345static ssize_t
346thermal_cooling_device_max_state_show(struct device *dev,
347 struct device_attribute *attr, char *buf)
348{
349 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000350 unsigned long state;
351 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800352
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000353 ret = cdev->ops->get_max_state(cdev, &state);
354 if (ret)
355 return ret;
356 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800357}
358
359static ssize_t
360thermal_cooling_device_cur_state_show(struct device *dev,
361 struct device_attribute *attr, char *buf)
362{
363 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000364 unsigned long state;
365 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800366
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000367 ret = cdev->ops->get_cur_state(cdev, &state);
368 if (ret)
369 return ret;
370 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800371}
372
373static ssize_t
374thermal_cooling_device_cur_state_store(struct device *dev,
375 struct device_attribute *attr,
376 const char *buf, size_t count)
377{
378 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000379 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800380 int result;
381
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000382 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800383 return -EINVAL;
384
Roel Kluinedb94912009-12-15 22:46:50 +0100385 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800386 return -EINVAL;
387
388 result = cdev->ops->set_cur_state(cdev, state);
389 if (result)
390 return result;
391 return count;
392}
393
394static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500395__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800396static DEVICE_ATTR(max_state, 0444,
397 thermal_cooling_device_max_state_show, NULL);
398static DEVICE_ATTR(cur_state, 0644,
399 thermal_cooling_device_cur_state_show,
400 thermal_cooling_device_cur_state_store);
401
402static ssize_t
403thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500404 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800405{
406 struct thermal_cooling_device_instance *instance;
407
408 instance =
409 container_of(attr, struct thermal_cooling_device_instance, attr);
410
411 if (instance->trip == THERMAL_TRIPS_NONE)
412 return sprintf(buf, "-1\n");
413 else
414 return sprintf(buf, "%d\n", instance->trip);
415}
416
417/* Device management */
418
Rene Herman16d75232008-06-24 19:38:56 +0200419#if defined(CONFIG_THERMAL_HWMON)
420
Zhang Ruie68b16a2008-04-21 16:07:52 +0800421/* hwmon sys I/F */
422#include <linux/hwmon.h>
423static LIST_HEAD(thermal_hwmon_list);
424
425static ssize_t
426name_show(struct device *dev, struct device_attribute *attr, char *buf)
427{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700428 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800429 return sprintf(buf, "%s\n", hwmon->type);
430}
431static DEVICE_ATTR(name, 0444, name_show, NULL);
432
433static ssize_t
434temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
435{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000436 long temperature;
437 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800438 struct thermal_hwmon_attr *hwmon_attr
439 = container_of(attr, struct thermal_hwmon_attr, attr);
440 struct thermal_zone_device *tz
441 = container_of(hwmon_attr, struct thermal_zone_device,
442 temp_input);
443
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000444 ret = tz->ops->get_temp(tz, &temperature);
445
446 if (ret)
447 return ret;
448
449 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800450}
451
452static ssize_t
453temp_crit_show(struct device *dev, struct device_attribute *attr,
454 char *buf)
455{
456 struct thermal_hwmon_attr *hwmon_attr
457 = container_of(attr, struct thermal_hwmon_attr, attr);
458 struct thermal_zone_device *tz
459 = container_of(hwmon_attr, struct thermal_zone_device,
460 temp_crit);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000461 long temperature;
462 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800463
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000464 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
465 if (ret)
466 return ret;
467
468 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800469}
470
471
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700472static struct thermal_hwmon_device *
473thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
474{
475 struct thermal_hwmon_device *hwmon;
476
477 mutex_lock(&thermal_list_lock);
478 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
479 if (!strcmp(hwmon->type, tz->type)) {
480 mutex_unlock(&thermal_list_lock);
481 return hwmon;
482 }
483 mutex_unlock(&thermal_list_lock);
484
485 return NULL;
486}
487
Zhang Ruie68b16a2008-04-21 16:07:52 +0800488static int
489thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
490{
491 struct thermal_hwmon_device *hwmon;
492 int new_hwmon_device = 1;
493 int result;
494
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700495 hwmon = thermal_hwmon_lookup_by_type(tz);
496 if (hwmon) {
497 new_hwmon_device = 0;
498 goto register_sys_interface;
499 }
Zhang Ruie68b16a2008-04-21 16:07:52 +0800500
501 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
502 if (!hwmon)
503 return -ENOMEM;
504
505 INIT_LIST_HEAD(&hwmon->tz_list);
506 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
507 hwmon->device = hwmon_device_register(NULL);
508 if (IS_ERR(hwmon->device)) {
509 result = PTR_ERR(hwmon->device);
510 goto free_mem;
511 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700512 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800513 result = device_create_file(hwmon->device, &dev_attr_name);
514 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530515 goto free_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800516
517 register_sys_interface:
518 tz->hwmon = hwmon;
519 hwmon->count++;
520
521 snprintf(tz->temp_input.name, THERMAL_NAME_LENGTH,
522 "temp%d_input", hwmon->count);
523 tz->temp_input.attr.attr.name = tz->temp_input.name;
524 tz->temp_input.attr.attr.mode = 0444;
525 tz->temp_input.attr.show = temp_input_show;
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700526 sysfs_attr_init(&tz->temp_input.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800527 result = device_create_file(hwmon->device, &tz->temp_input.attr);
528 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530529 goto unregister_name;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800530
531 if (tz->ops->get_crit_temp) {
532 unsigned long temperature;
533 if (!tz->ops->get_crit_temp(tz, &temperature)) {
534 snprintf(tz->temp_crit.name, THERMAL_NAME_LENGTH,
535 "temp%d_crit", hwmon->count);
536 tz->temp_crit.attr.attr.name = tz->temp_crit.name;
537 tz->temp_crit.attr.attr.mode = 0444;
538 tz->temp_crit.attr.show = temp_crit_show;
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700539 sysfs_attr_init(&tz->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800540 result = device_create_file(hwmon->device,
541 &tz->temp_crit.attr);
542 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530543 goto unregister_input;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800544 }
545 }
546
547 mutex_lock(&thermal_list_lock);
548 if (new_hwmon_device)
549 list_add_tail(&hwmon->node, &thermal_hwmon_list);
550 list_add_tail(&tz->hwmon_node, &hwmon->tz_list);
551 mutex_unlock(&thermal_list_lock);
552
553 return 0;
554
Durgadoss Rb299eb52011-03-03 04:30:13 +0530555 unregister_input:
Zhang Ruie68b16a2008-04-21 16:07:52 +0800556 device_remove_file(hwmon->device, &tz->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530557 unregister_name:
Zhang Ruie68b16a2008-04-21 16:07:52 +0800558 if (new_hwmon_device) {
559 device_remove_file(hwmon->device, &dev_attr_name);
560 hwmon_device_unregister(hwmon->device);
561 }
562 free_mem:
563 if (new_hwmon_device)
564 kfree(hwmon);
565
566 return result;
567}
568
569static void
570thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
571{
572 struct thermal_hwmon_device *hwmon = tz->hwmon;
573
574 tz->hwmon = NULL;
575 device_remove_file(hwmon->device, &tz->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530576 if (tz->ops->get_crit_temp)
577 device_remove_file(hwmon->device, &tz->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800578
579 mutex_lock(&thermal_list_lock);
580 list_del(&tz->hwmon_node);
581 if (!list_empty(&hwmon->tz_list)) {
582 mutex_unlock(&thermal_list_lock);
583 return;
584 }
585 list_del(&hwmon->node);
586 mutex_unlock(&thermal_list_lock);
587
588 device_remove_file(hwmon->device, &dev_attr_name);
589 hwmon_device_unregister(hwmon->device);
590 kfree(hwmon);
591}
592#else
593static int
594thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
595{
596 return 0;
597}
598
599static void
600thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
601{
602}
603#endif
604
Matthew Garrettb1569e92008-12-03 17:55:32 +0000605static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
606 int delay)
607{
608 cancel_delayed_work(&(tz->poll_queue));
609
610 if (!delay)
611 return;
612
613 if (delay > 1000)
614 schedule_delayed_work(&(tz->poll_queue),
615 round_jiffies(msecs_to_jiffies(delay)));
616 else
617 schedule_delayed_work(&(tz->poll_queue),
618 msecs_to_jiffies(delay));
619}
620
621static void thermal_zone_device_passive(struct thermal_zone_device *tz,
622 int temp, int trip_temp, int trip)
623{
624 int trend = 0;
625 struct thermal_cooling_device_instance *instance;
626 struct thermal_cooling_device *cdev;
627 long state, max_state;
628
629 /*
630 * Above Trip?
631 * -----------
632 * Calculate the thermal trend (using the passive cooling equation)
633 * and modify the performance limit for all passive cooling devices
634 * accordingly. Note that we assume symmetry.
635 */
636 if (temp >= trip_temp) {
637 tz->passive = true;
638
639 trend = (tz->tc1 * (temp - tz->last_temperature)) +
640 (tz->tc2 * (temp - trip_temp));
641
642 /* Heating up? */
643 if (trend > 0) {
644 list_for_each_entry(instance, &tz->cooling_devices,
645 node) {
646 if (instance->trip != trip)
647 continue;
648 cdev = instance->cdev;
649 cdev->ops->get_cur_state(cdev, &state);
650 cdev->ops->get_max_state(cdev, &max_state);
651 if (state++ < max_state)
652 cdev->ops->set_cur_state(cdev, state);
653 }
654 } else if (trend < 0) { /* Cooling off? */
655 list_for_each_entry(instance, &tz->cooling_devices,
656 node) {
657 if (instance->trip != trip)
658 continue;
659 cdev = instance->cdev;
660 cdev->ops->get_cur_state(cdev, &state);
661 cdev->ops->get_max_state(cdev, &max_state);
662 if (state > 0)
663 cdev->ops->set_cur_state(cdev, --state);
664 }
665 }
666 return;
667 }
668
669 /*
670 * Below Trip?
671 * -----------
672 * Implement passive cooling hysteresis to slowly increase performance
673 * and avoid thrashing around the passive trip point. Note that we
674 * assume symmetry.
675 */
676 list_for_each_entry(instance, &tz->cooling_devices, node) {
677 if (instance->trip != trip)
678 continue;
679 cdev = instance->cdev;
680 cdev->ops->get_cur_state(cdev, &state);
681 cdev->ops->get_max_state(cdev, &max_state);
682 if (state > 0)
683 cdev->ops->set_cur_state(cdev, --state);
684 if (state == 0)
685 tz->passive = false;
686 }
687}
688
689static void thermal_zone_device_check(struct work_struct *work)
690{
691 struct thermal_zone_device *tz = container_of(work, struct
692 thermal_zone_device,
693 poll_queue.work);
694 thermal_zone_device_update(tz);
695}
Zhang Ruie68b16a2008-04-21 16:07:52 +0800696
Zhang Rui203d3d42008-01-17 15:51:08 +0800697/**
698 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800699 * @tz: thermal zone device
700 * @trip: indicates which trip point the cooling devices is
701 * associated with in this thermal zone.
702 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500703 *
704 * This function is usually called in the thermal zone device .bind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800705 */
706int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
707 int trip,
708 struct thermal_cooling_device *cdev)
709{
710 struct thermal_cooling_device_instance *dev;
711 struct thermal_cooling_device_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500712 struct thermal_zone_device *pos1;
713 struct thermal_cooling_device *pos2;
Zhang Rui203d3d42008-01-17 15:51:08 +0800714 int result;
715
Len Brown543a9562008-02-07 16:55:08 -0500716 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800717 return -EINVAL;
718
Thomas Sujithc7516702008-02-15 00:58:50 -0500719 list_for_each_entry(pos1, &thermal_tz_list, node) {
720 if (pos1 == tz)
721 break;
722 }
723 list_for_each_entry(pos2, &thermal_cdev_list, node) {
724 if (pos2 == cdev)
725 break;
726 }
727
728 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800729 return -EINVAL;
730
731 dev =
732 kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL);
733 if (!dev)
734 return -ENOMEM;
735 dev->tz = tz;
736 dev->cdev = cdev;
737 dev->trip = trip;
738 result = get_idr(&tz->idr, &tz->lock, &dev->id);
739 if (result)
740 goto free_mem;
741
742 sprintf(dev->name, "cdev%d", dev->id);
743 result =
744 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
745 if (result)
746 goto release_idr;
747
748 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700749 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800750 dev->attr.attr.name = dev->attr_name;
751 dev->attr.attr.mode = 0444;
752 dev->attr.show = thermal_cooling_device_trip_point_show;
753 result = device_create_file(&tz->device, &dev->attr);
754 if (result)
755 goto remove_symbol_link;
756
757 mutex_lock(&tz->lock);
758 list_for_each_entry(pos, &tz->cooling_devices, node)
759 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
760 result = -EEXIST;
761 break;
762 }
763 if (!result)
764 list_add_tail(&dev->node, &tz->cooling_devices);
765 mutex_unlock(&tz->lock);
766
767 if (!result)
768 return 0;
769
770 device_remove_file(&tz->device, &dev->attr);
771 remove_symbol_link:
772 sysfs_remove_link(&tz->device.kobj, dev->name);
773 release_idr:
774 release_idr(&tz->idr, &tz->lock, dev->id);
775 free_mem:
776 kfree(dev);
777 return result;
778}
Len Brown543a9562008-02-07 16:55:08 -0500779
Zhang Rui203d3d42008-01-17 15:51:08 +0800780EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
781
782/**
783 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800784 * @tz: thermal zone device
785 * @trip: indicates which trip point the cooling devices is
786 * associated with in this thermal zone.
787 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500788 *
789 * This function is usually called in the thermal zone device .unbind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800790 */
791int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
792 int trip,
793 struct thermal_cooling_device *cdev)
794{
795 struct thermal_cooling_device_instance *pos, *next;
796
797 mutex_lock(&tz->lock);
798 list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
Len Brown543a9562008-02-07 16:55:08 -0500799 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800800 list_del(&pos->node);
801 mutex_unlock(&tz->lock);
802 goto unbind;
803 }
804 }
805 mutex_unlock(&tz->lock);
806
807 return -ENODEV;
808
809 unbind:
810 device_remove_file(&tz->device, &pos->attr);
811 sysfs_remove_link(&tz->device.kobj, pos->name);
812 release_idr(&tz->idr, &tz->lock, pos->id);
813 kfree(pos);
814 return 0;
815}
Len Brown543a9562008-02-07 16:55:08 -0500816
Zhang Rui203d3d42008-01-17 15:51:08 +0800817EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
818
819static void thermal_release(struct device *dev)
820{
821 struct thermal_zone_device *tz;
822 struct thermal_cooling_device *cdev;
823
Kay Sievers354655e2009-01-06 10:44:37 -0800824 if (!strncmp(dev_name(dev), "thermal_zone", sizeof "thermal_zone" - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800825 tz = to_thermal_zone(dev);
826 kfree(tz);
827 } else {
828 cdev = to_cooling_device(dev);
829 kfree(cdev);
830 }
831}
832
833static struct class thermal_class = {
834 .name = "thermal",
835 .dev_release = thermal_release,
836};
837
838/**
839 * thermal_cooling_device_register - register a new thermal cooling device
840 * @type: the thermal cooling device type.
841 * @devdata: device private data.
842 * @ops: standard thermal cooling devices callbacks.
843 */
Alan Cox5b275ce2010-11-11 15:27:29 +0000844struct thermal_cooling_device *thermal_cooling_device_register(
845 char *type, void *devdata, const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800846{
847 struct thermal_cooling_device *cdev;
848 struct thermal_zone_device *pos;
849 int result;
850
851 if (strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500852 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800853
854 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -0500855 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500856 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800857
858 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
859 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500860 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +0800861
862 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
863 if (result) {
864 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500865 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800866 }
867
868 strcpy(cdev->type, type);
869 cdev->ops = ops;
870 cdev->device.class = &thermal_class;
871 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -0800872 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800873 result = device_register(&cdev->device);
874 if (result) {
875 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
876 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500877 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800878 }
879
880 /* sys I/F */
881 if (type) {
Len Brown543a9562008-02-07 16:55:08 -0500882 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +0800883 if (result)
884 goto unregister;
885 }
886
887 result = device_create_file(&cdev->device, &dev_attr_max_state);
888 if (result)
889 goto unregister;
890
891 result = device_create_file(&cdev->device, &dev_attr_cur_state);
892 if (result)
893 goto unregister;
894
895 mutex_lock(&thermal_list_lock);
896 list_add(&cdev->node, &thermal_cdev_list);
897 list_for_each_entry(pos, &thermal_tz_list, node) {
898 if (!pos->ops->bind)
899 continue;
900 result = pos->ops->bind(pos, cdev);
901 if (result)
902 break;
903
904 }
905 mutex_unlock(&thermal_list_lock);
906
907 if (!result)
908 return cdev;
909
910 unregister:
911 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
912 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500913 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800914}
Len Brown543a9562008-02-07 16:55:08 -0500915
Zhang Rui203d3d42008-01-17 15:51:08 +0800916EXPORT_SYMBOL(thermal_cooling_device_register);
917
918/**
919 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +0800920 * @cdev: the thermal cooling device to remove.
921 *
922 * thermal_cooling_device_unregister() must be called when the device is no
923 * longer needed.
924 */
925void thermal_cooling_device_unregister(struct
926 thermal_cooling_device
927 *cdev)
928{
929 struct thermal_zone_device *tz;
930 struct thermal_cooling_device *pos = NULL;
931
932 if (!cdev)
933 return;
934
935 mutex_lock(&thermal_list_lock);
936 list_for_each_entry(pos, &thermal_cdev_list, node)
937 if (pos == cdev)
938 break;
939 if (pos != cdev) {
940 /* thermal cooling device not found */
941 mutex_unlock(&thermal_list_lock);
942 return;
943 }
944 list_del(&cdev->node);
945 list_for_each_entry(tz, &thermal_tz_list, node) {
946 if (!tz->ops->unbind)
947 continue;
948 tz->ops->unbind(tz, cdev);
949 }
950 mutex_unlock(&thermal_list_lock);
951 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -0500952 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +0800953 device_remove_file(&cdev->device, &dev_attr_max_state);
954 device_remove_file(&cdev->device, &dev_attr_cur_state);
955
956 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
957 device_unregister(&cdev->device);
958 return;
959}
Len Brown543a9562008-02-07 16:55:08 -0500960
Zhang Rui203d3d42008-01-17 15:51:08 +0800961EXPORT_SYMBOL(thermal_cooling_device_unregister);
962
963/**
Matthew Garrettb1569e92008-12-03 17:55:32 +0000964 * thermal_zone_device_update - force an update of a thermal zone's state
965 * @ttz: the thermal zone to update
966 */
967
968void thermal_zone_device_update(struct thermal_zone_device *tz)
969{
970 int count, ret = 0;
971 long temp, trip_temp;
972 enum thermal_trip_type trip_type;
973 struct thermal_cooling_device_instance *instance;
974 struct thermal_cooling_device *cdev;
975
976 mutex_lock(&tz->lock);
977
Michael Brunner0d288162009-08-26 14:29:25 -0700978 if (tz->ops->get_temp(tz, &temp)) {
979 /* get_temp failed - retry it later */
980 printk(KERN_WARNING PREFIX "failed to read out thermal zone "
981 "%d\n", tz->id);
982 goto leave;
983 }
Matthew Garrettb1569e92008-12-03 17:55:32 +0000984
985 for (count = 0; count < tz->trips; count++) {
986 tz->ops->get_trip_type(tz, count, &trip_type);
987 tz->ops->get_trip_temp(tz, count, &trip_temp);
988
989 switch (trip_type) {
990 case THERMAL_TRIP_CRITICAL:
Vladimir Zajac29321352009-05-06 19:34:21 +0200991 if (temp >= trip_temp) {
Matthew Garrettb1569e92008-12-03 17:55:32 +0000992 if (tz->ops->notify)
993 ret = tz->ops->notify(tz, count,
994 trip_type);
995 if (!ret) {
996 printk(KERN_EMERG
997 "Critical temperature reached (%ld C), shutting down.\n",
998 temp/1000);
999 orderly_poweroff(true);
1000 }
1001 }
1002 break;
1003 case THERMAL_TRIP_HOT:
Vladimir Zajac29321352009-05-06 19:34:21 +02001004 if (temp >= trip_temp)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001005 if (tz->ops->notify)
1006 tz->ops->notify(tz, count, trip_type);
1007 break;
1008 case THERMAL_TRIP_ACTIVE:
1009 list_for_each_entry(instance, &tz->cooling_devices,
1010 node) {
1011 if (instance->trip != count)
1012 continue;
1013
1014 cdev = instance->cdev;
1015
Vladimir Zajac29321352009-05-06 19:34:21 +02001016 if (temp >= trip_temp)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001017 cdev->ops->set_cur_state(cdev, 1);
1018 else
1019 cdev->ops->set_cur_state(cdev, 0);
1020 }
1021 break;
1022 case THERMAL_TRIP_PASSIVE:
Vladimir Zajac29321352009-05-06 19:34:21 +02001023 if (temp >= trip_temp || tz->passive)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001024 thermal_zone_device_passive(tz, temp,
1025 trip_temp, count);
1026 break;
1027 }
1028 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001029
1030 if (tz->forced_passive)
1031 thermal_zone_device_passive(tz, temp, tz->forced_passive,
1032 THERMAL_TRIPS_NONE);
1033
Matthew Garrettb1569e92008-12-03 17:55:32 +00001034 tz->last_temperature = temp;
Michael Brunner0d288162009-08-26 14:29:25 -07001035
1036 leave:
Matthew Garrettb1569e92008-12-03 17:55:32 +00001037 if (tz->passive)
1038 thermal_zone_device_set_polling(tz, tz->passive_delay);
1039 else if (tz->polling_delay)
1040 thermal_zone_device_set_polling(tz, tz->polling_delay);
Frans Pop3767cb52009-10-26 08:39:04 +01001041 else
1042 thermal_zone_device_set_polling(tz, 0);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001043 mutex_unlock(&tz->lock);
1044}
1045EXPORT_SYMBOL(thermal_zone_device_update);
1046
1047/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001048 * thermal_zone_device_register - register a new thermal zone device
1049 * @type: the thermal zone device type
1050 * @trips: the number of trip points the thermal zone support
1051 * @devdata: private device data
1052 * @ops: standard thermal zone device callbacks
Matthew Garrettb1569e92008-12-03 17:55:32 +00001053 * @tc1: thermal coefficient 1 for passive calculations
1054 * @tc2: thermal coefficient 2 for passive calculations
1055 * @passive_delay: number of milliseconds to wait between polls when
1056 * performing passive cooling
1057 * @polling_delay: number of milliseconds to wait between polls when checking
1058 * whether trip points have been crossed (0 for interrupt
1059 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001060 *
1061 * thermal_zone_device_unregister() must be called when the device is no
Matthew Garrettb1569e92008-12-03 17:55:32 +00001062 * longer needed. The passive cooling formula uses tc1 and tc2 as described in
1063 * section 11.1.5.1 of the ACPI specification 3.0.
Zhang Rui203d3d42008-01-17 15:51:08 +08001064 */
1065struct thermal_zone_device *thermal_zone_device_register(char *type,
Alan Cox5b275ce2010-11-11 15:27:29 +00001066 int trips, void *devdata,
1067 const struct thermal_zone_device_ops *ops,
1068 int tc1, int tc2, int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001069{
1070 struct thermal_zone_device *tz;
1071 struct thermal_cooling_device *pos;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001072 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001073 int result;
1074 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001075 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001076
1077 if (strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001078 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001079
1080 if (trips > THERMAL_MAX_TRIPS || trips < 0)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001081 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001082
1083 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001084 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001085
1086 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1087 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001088 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001089
1090 INIT_LIST_HEAD(&tz->cooling_devices);
1091 idr_init(&tz->idr);
1092 mutex_init(&tz->lock);
1093 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1094 if (result) {
1095 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001096 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001097 }
1098
1099 strcpy(tz->type, type);
1100 tz->ops = ops;
1101 tz->device.class = &thermal_class;
1102 tz->devdata = devdata;
1103 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001104 tz->tc1 = tc1;
1105 tz->tc2 = tc2;
1106 tz->passive_delay = passive_delay;
1107 tz->polling_delay = polling_delay;
1108
Kay Sievers354655e2009-01-06 10:44:37 -08001109 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001110 result = device_register(&tz->device);
1111 if (result) {
1112 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1113 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001114 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001115 }
1116
1117 /* sys I/F */
1118 if (type) {
1119 result = device_create_file(&tz->device, &dev_attr_type);
1120 if (result)
1121 goto unregister;
1122 }
1123
1124 result = device_create_file(&tz->device, &dev_attr_temp);
1125 if (result)
1126 goto unregister;
1127
1128 if (ops->get_mode) {
1129 result = device_create_file(&tz->device, &dev_attr_mode);
1130 if (result)
1131 goto unregister;
1132 }
1133
1134 for (count = 0; count < trips; count++) {
1135 TRIP_POINT_ATTR_ADD(&tz->device, count, result);
1136 if (result)
1137 goto unregister;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001138 tz->ops->get_trip_type(tz, count, &trip_type);
1139 if (trip_type == THERMAL_TRIP_PASSIVE)
1140 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001141 }
1142
Matthew Garrett03a971a2008-12-03 18:00:38 +00001143 if (!passive)
1144 result = device_create_file(&tz->device,
1145 &dev_attr_passive);
1146
1147 if (result)
1148 goto unregister;
1149
Zhang Ruie68b16a2008-04-21 16:07:52 +08001150 result = thermal_add_hwmon_sysfs(tz);
1151 if (result)
1152 goto unregister;
1153
Zhang Rui203d3d42008-01-17 15:51:08 +08001154 mutex_lock(&thermal_list_lock);
1155 list_add_tail(&tz->node, &thermal_tz_list);
1156 if (ops->bind)
1157 list_for_each_entry(pos, &thermal_cdev_list, node) {
Len Brown543a9562008-02-07 16:55:08 -05001158 result = ops->bind(tz, pos);
1159 if (result)
1160 break;
Zhang Rui203d3d42008-01-17 15:51:08 +08001161 }
1162 mutex_unlock(&thermal_list_lock);
1163
Matthew Garrettb1569e92008-12-03 17:55:32 +00001164 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1165
1166 thermal_zone_device_update(tz);
1167
Zhang Rui203d3d42008-01-17 15:51:08 +08001168 if (!result)
1169 return tz;
1170
1171 unregister:
1172 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1173 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001174 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001175}
Len Brown543a9562008-02-07 16:55:08 -05001176
Zhang Rui203d3d42008-01-17 15:51:08 +08001177EXPORT_SYMBOL(thermal_zone_device_register);
1178
1179/**
1180 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001181 * @tz: the thermal zone device to remove
1182 */
1183void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1184{
1185 struct thermal_cooling_device *cdev;
1186 struct thermal_zone_device *pos = NULL;
1187 int count;
1188
1189 if (!tz)
1190 return;
1191
1192 mutex_lock(&thermal_list_lock);
1193 list_for_each_entry(pos, &thermal_tz_list, node)
1194 if (pos == tz)
1195 break;
1196 if (pos != tz) {
1197 /* thermal zone device not found */
1198 mutex_unlock(&thermal_list_lock);
1199 return;
1200 }
1201 list_del(&tz->node);
1202 if (tz->ops->unbind)
1203 list_for_each_entry(cdev, &thermal_cdev_list, node)
1204 tz->ops->unbind(tz, cdev);
1205 mutex_unlock(&thermal_list_lock);
1206
Matthew Garrettb1569e92008-12-03 17:55:32 +00001207 thermal_zone_device_set_polling(tz, 0);
1208
Zhang Rui203d3d42008-01-17 15:51:08 +08001209 if (tz->type[0])
1210 device_remove_file(&tz->device, &dev_attr_type);
1211 device_remove_file(&tz->device, &dev_attr_temp);
1212 if (tz->ops->get_mode)
1213 device_remove_file(&tz->device, &dev_attr_mode);
1214
1215 for (count = 0; count < tz->trips; count++)
1216 TRIP_POINT_ATTR_REMOVE(&tz->device, count);
1217
Zhang Ruie68b16a2008-04-21 16:07:52 +08001218 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001219 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1220 idr_destroy(&tz->idr);
1221 mutex_destroy(&tz->lock);
1222 device_unregister(&tz->device);
1223 return;
1224}
Len Brown543a9562008-02-07 16:55:08 -05001225
Zhang Rui203d3d42008-01-17 15:51:08 +08001226EXPORT_SYMBOL(thermal_zone_device_unregister);
1227
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001228#ifdef CONFIG_NET
1229static struct genl_family thermal_event_genl_family = {
1230 .id = GENL_ID_GENERATE,
1231 .name = THERMAL_GENL_FAMILY_NAME,
1232 .version = THERMAL_GENL_VERSION,
1233 .maxattr = THERMAL_GENL_ATTR_MAX,
1234};
1235
1236static struct genl_multicast_group thermal_event_mcgrp = {
1237 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1238};
1239
R.Durgadoss4cb18722010-10-27 03:33:29 +05301240int generate_netlink_event(u32 orig, enum events event)
1241{
1242 struct sk_buff *skb;
1243 struct nlattr *attr;
1244 struct thermal_genl_event *thermal_event;
1245 void *msg_header;
1246 int size;
1247 int result;
1248
1249 /* allocate memory */
1250 size = nla_total_size(sizeof(struct thermal_genl_event)) + \
1251 nla_total_size(0);
1252
1253 skb = genlmsg_new(size, GFP_ATOMIC);
1254 if (!skb)
1255 return -ENOMEM;
1256
1257 /* add the genetlink message header */
1258 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1259 &thermal_event_genl_family, 0,
1260 THERMAL_GENL_CMD_EVENT);
1261 if (!msg_header) {
1262 nlmsg_free(skb);
1263 return -ENOMEM;
1264 }
1265
1266 /* fill the data */
1267 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT, \
1268 sizeof(struct thermal_genl_event));
1269
1270 if (!attr) {
1271 nlmsg_free(skb);
1272 return -EINVAL;
1273 }
1274
1275 thermal_event = nla_data(attr);
1276 if (!thermal_event) {
1277 nlmsg_free(skb);
1278 return -EINVAL;
1279 }
1280
1281 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1282
1283 thermal_event->orig = orig;
1284 thermal_event->event = event;
1285
1286 /* send multicast genetlink message */
1287 result = genlmsg_end(skb, msg_header);
1288 if (result < 0) {
1289 nlmsg_free(skb);
1290 return result;
1291 }
1292
1293 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1294 if (result)
1295 printk(KERN_INFO "failed to send netlink event:%d", result);
1296
1297 return result;
1298}
1299EXPORT_SYMBOL(generate_netlink_event);
1300
1301static int genetlink_init(void)
1302{
1303 int result;
1304
1305 result = genl_register_family(&thermal_event_genl_family);
1306 if (result)
1307 return result;
1308
1309 result = genl_register_mc_group(&thermal_event_genl_family,
1310 &thermal_event_mcgrp);
1311 if (result)
1312 genl_unregister_family(&thermal_event_genl_family);
1313 return result;
1314}
1315
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001316static void genetlink_exit(void)
1317{
1318 genl_unregister_family(&thermal_event_genl_family);
1319}
1320#else /* !CONFIG_NET */
1321static inline int genetlink_init(void) { return 0; }
1322static inline void genetlink_exit(void) {}
1323#endif /* !CONFIG_NET */
1324
Zhang Rui203d3d42008-01-17 15:51:08 +08001325static int __init thermal_init(void)
1326{
1327 int result = 0;
1328
1329 result = class_register(&thermal_class);
1330 if (result) {
1331 idr_destroy(&thermal_tz_idr);
1332 idr_destroy(&thermal_cdev_idr);
1333 mutex_destroy(&thermal_idr_lock);
1334 mutex_destroy(&thermal_list_lock);
1335 }
R.Durgadoss4cb18722010-10-27 03:33:29 +05301336 result = genetlink_init();
Zhang Rui203d3d42008-01-17 15:51:08 +08001337 return result;
1338}
1339
1340static void __exit thermal_exit(void)
1341{
1342 class_unregister(&thermal_class);
1343 idr_destroy(&thermal_tz_idr);
1344 idr_destroy(&thermal_cdev_idr);
1345 mutex_destroy(&thermal_idr_lock);
1346 mutex_destroy(&thermal_list_lock);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301347 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001348}
1349
R.Durgadoss4cb18722010-10-27 03:33:29 +05301350fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001351module_exit(thermal_exit);