blob: 760e045c93c8f34e96d0dd2563dd7544d69bfae3 [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
65static struct genl_family thermal_event_genl_family = {
66 .id = GENL_ID_GENERATE,
67 .name = THERMAL_GENL_FAMILY_NAME,
68 .version = THERMAL_GENL_VERSION,
69 .maxattr = THERMAL_GENL_ATTR_MAX,
70};
71
72static struct genl_multicast_group thermal_event_mcgrp = {
73 .name = THERMAL_GENL_MCAST_GROUP_NAME,
74};
75
76static int genetlink_init(void);
77static void genetlink_exit(void);
78
Zhang Rui203d3d42008-01-17 15:51:08 +080079static int get_idr(struct idr *idr, struct mutex *lock, int *id)
80{
81 int err;
82
83 again:
84 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
85 return -ENOMEM;
86
87 if (lock)
88 mutex_lock(lock);
89 err = idr_get_new(idr, NULL, id);
90 if (lock)
91 mutex_unlock(lock);
92 if (unlikely(err == -EAGAIN))
93 goto again;
94 else if (unlikely(err))
95 return err;
96
97 *id = *id & MAX_ID_MASK;
98 return 0;
99}
100
101static void release_idr(struct idr *idr, struct mutex *lock, int id)
102{
103 if (lock)
104 mutex_lock(lock);
105 idr_remove(idr, id);
106 if (lock)
107 mutex_unlock(lock);
108}
109
110/* sys I/F for thermal zone */
111
112#define to_thermal_zone(_dev) \
113 container_of(_dev, struct thermal_zone_device, device)
114
115static ssize_t
116type_show(struct device *dev, struct device_attribute *attr, char *buf)
117{
118 struct thermal_zone_device *tz = to_thermal_zone(dev);
119
120 return sprintf(buf, "%s\n", tz->type);
121}
122
123static ssize_t
124temp_show(struct device *dev, struct device_attribute *attr, char *buf)
125{
126 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000127 long temperature;
128 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800129
130 if (!tz->ops->get_temp)
131 return -EPERM;
132
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000133 ret = tz->ops->get_temp(tz, &temperature);
134
135 if (ret)
136 return ret;
137
138 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800139}
140
141static ssize_t
142mode_show(struct device *dev, struct device_attribute *attr, char *buf)
143{
144 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000145 enum thermal_device_mode mode;
146 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800147
148 if (!tz->ops->get_mode)
149 return -EPERM;
150
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000151 result = tz->ops->get_mode(tz, &mode);
152 if (result)
153 return result;
154
155 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
156 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800157}
158
159static ssize_t
160mode_store(struct device *dev, struct device_attribute *attr,
161 const char *buf, size_t count)
162{
163 struct thermal_zone_device *tz = to_thermal_zone(dev);
164 int result;
165
166 if (!tz->ops->set_mode)
167 return -EPERM;
168
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000169 if (!strncmp(buf, "enabled", sizeof("enabled")))
170 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
171 else if (!strncmp(buf, "disabled", sizeof("disabled")))
172 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
173 else
174 result = -EINVAL;
175
Zhang Rui203d3d42008-01-17 15:51:08 +0800176 if (result)
177 return result;
178
179 return count;
180}
181
182static ssize_t
183trip_point_type_show(struct device *dev, struct device_attribute *attr,
184 char *buf)
185{
186 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000187 enum thermal_trip_type type;
188 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800189
190 if (!tz->ops->get_trip_type)
191 return -EPERM;
192
193 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
194 return -EINVAL;
195
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000196 result = tz->ops->get_trip_type(tz, trip, &type);
197 if (result)
198 return result;
199
200 switch (type) {
201 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300202 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000203 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300204 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000205 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300206 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000207 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300208 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000209 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300210 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000211 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800212}
213
214static ssize_t
215trip_point_temp_show(struct device *dev, struct device_attribute *attr,
216 char *buf)
217{
218 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000219 int trip, ret;
220 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800221
222 if (!tz->ops->get_trip_temp)
223 return -EPERM;
224
225 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
226 return -EINVAL;
227
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000228 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
229
230 if (ret)
231 return ret;
232
233 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800234}
235
Matthew Garrett03a971a2008-12-03 18:00:38 +0000236static ssize_t
237passive_store(struct device *dev, struct device_attribute *attr,
238 const char *buf, size_t count)
239{
240 struct thermal_zone_device *tz = to_thermal_zone(dev);
241 struct thermal_cooling_device *cdev = NULL;
242 int state;
243
244 if (!sscanf(buf, "%d\n", &state))
245 return -EINVAL;
246
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100247 /* sanity check: values below 1000 millicelcius don't make sense
248 * and can cause the system to go into a thermal heart attack
249 */
250 if (state && state < 1000)
251 return -EINVAL;
252
Matthew Garrett03a971a2008-12-03 18:00:38 +0000253 if (state && !tz->forced_passive) {
254 mutex_lock(&thermal_list_lock);
255 list_for_each_entry(cdev, &thermal_cdev_list, node) {
256 if (!strncmp("Processor", cdev->type,
257 sizeof("Processor")))
258 thermal_zone_bind_cooling_device(tz,
259 THERMAL_TRIPS_NONE,
260 cdev);
261 }
262 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100263 if (!tz->passive_delay)
264 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000265 } else if (!state && tz->forced_passive) {
266 mutex_lock(&thermal_list_lock);
267 list_for_each_entry(cdev, &thermal_cdev_list, node) {
268 if (!strncmp("Processor", cdev->type,
269 sizeof("Processor")))
270 thermal_zone_unbind_cooling_device(tz,
271 THERMAL_TRIPS_NONE,
272 cdev);
273 }
274 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100275 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000276 }
277
278 tz->tc1 = 1;
279 tz->tc2 = 1;
280
Matthew Garrett03a971a2008-12-03 18:00:38 +0000281 tz->forced_passive = state;
282
283 thermal_zone_device_update(tz);
284
285 return count;
286}
287
288static ssize_t
289passive_show(struct device *dev, struct device_attribute *attr,
290 char *buf)
291{
292 struct thermal_zone_device *tz = to_thermal_zone(dev);
293
294 return sprintf(buf, "%d\n", tz->forced_passive);
295}
296
Zhang Rui203d3d42008-01-17 15:51:08 +0800297static DEVICE_ATTR(type, 0444, type_show, NULL);
298static DEVICE_ATTR(temp, 0444, temp_show, NULL);
299static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000300static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, \
301 passive_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800302
303static struct device_attribute trip_point_attrs[] = {
304 __ATTR(trip_point_0_type, 0444, trip_point_type_show, NULL),
305 __ATTR(trip_point_0_temp, 0444, trip_point_temp_show, NULL),
306 __ATTR(trip_point_1_type, 0444, trip_point_type_show, NULL),
307 __ATTR(trip_point_1_temp, 0444, trip_point_temp_show, NULL),
308 __ATTR(trip_point_2_type, 0444, trip_point_type_show, NULL),
309 __ATTR(trip_point_2_temp, 0444, trip_point_temp_show, NULL),
310 __ATTR(trip_point_3_type, 0444, trip_point_type_show, NULL),
311 __ATTR(trip_point_3_temp, 0444, trip_point_temp_show, NULL),
312 __ATTR(trip_point_4_type, 0444, trip_point_type_show, NULL),
313 __ATTR(trip_point_4_temp, 0444, trip_point_temp_show, NULL),
314 __ATTR(trip_point_5_type, 0444, trip_point_type_show, NULL),
315 __ATTR(trip_point_5_temp, 0444, trip_point_temp_show, NULL),
316 __ATTR(trip_point_6_type, 0444, trip_point_type_show, NULL),
317 __ATTR(trip_point_6_temp, 0444, trip_point_temp_show, NULL),
318 __ATTR(trip_point_7_type, 0444, trip_point_type_show, NULL),
319 __ATTR(trip_point_7_temp, 0444, trip_point_temp_show, NULL),
320 __ATTR(trip_point_8_type, 0444, trip_point_type_show, NULL),
321 __ATTR(trip_point_8_temp, 0444, trip_point_temp_show, NULL),
322 __ATTR(trip_point_9_type, 0444, trip_point_type_show, NULL),
323 __ATTR(trip_point_9_temp, 0444, trip_point_temp_show, NULL),
Krzysztof Helt5f1a3f22008-04-15 14:34:47 -0700324 __ATTR(trip_point_10_type, 0444, trip_point_type_show, NULL),
325 __ATTR(trip_point_10_temp, 0444, trip_point_temp_show, NULL),
326 __ATTR(trip_point_11_type, 0444, trip_point_type_show, NULL),
327 __ATTR(trip_point_11_temp, 0444, trip_point_temp_show, NULL),
Zhang Rui203d3d42008-01-17 15:51:08 +0800328};
329
330#define TRIP_POINT_ATTR_ADD(_dev, _index, result) \
331do { \
332 result = device_create_file(_dev, \
333 &trip_point_attrs[_index * 2]); \
334 if (result) \
335 break; \
336 result = device_create_file(_dev, \
337 &trip_point_attrs[_index * 2 + 1]); \
338} while (0)
339
340#define TRIP_POINT_ATTR_REMOVE(_dev, _index) \
341do { \
342 device_remove_file(_dev, &trip_point_attrs[_index * 2]); \
343 device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \
344} while (0)
345
346/* sys I/F for cooling device */
347#define to_cooling_device(_dev) \
348 container_of(_dev, struct thermal_cooling_device, device)
349
350static ssize_t
351thermal_cooling_device_type_show(struct device *dev,
352 struct device_attribute *attr, char *buf)
353{
354 struct thermal_cooling_device *cdev = to_cooling_device(dev);
355
356 return sprintf(buf, "%s\n", cdev->type);
357}
358
359static ssize_t
360thermal_cooling_device_max_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_max_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_show(struct device *dev,
375 struct device_attribute *attr, char *buf)
376{
377 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000378 unsigned long state;
379 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800380
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000381 ret = cdev->ops->get_cur_state(cdev, &state);
382 if (ret)
383 return ret;
384 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800385}
386
387static ssize_t
388thermal_cooling_device_cur_state_store(struct device *dev,
389 struct device_attribute *attr,
390 const char *buf, size_t count)
391{
392 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000393 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800394 int result;
395
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000396 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800397 return -EINVAL;
398
Roel Kluinedb94912009-12-15 22:46:50 +0100399 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800400 return -EINVAL;
401
402 result = cdev->ops->set_cur_state(cdev, state);
403 if (result)
404 return result;
405 return count;
406}
407
408static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500409__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800410static DEVICE_ATTR(max_state, 0444,
411 thermal_cooling_device_max_state_show, NULL);
412static DEVICE_ATTR(cur_state, 0644,
413 thermal_cooling_device_cur_state_show,
414 thermal_cooling_device_cur_state_store);
415
416static ssize_t
417thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500418 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800419{
420 struct thermal_cooling_device_instance *instance;
421
422 instance =
423 container_of(attr, struct thermal_cooling_device_instance, attr);
424
425 if (instance->trip == THERMAL_TRIPS_NONE)
426 return sprintf(buf, "-1\n");
427 else
428 return sprintf(buf, "%d\n", instance->trip);
429}
430
431/* Device management */
432
Rene Herman16d75232008-06-24 19:38:56 +0200433#if defined(CONFIG_THERMAL_HWMON)
434
Zhang Ruie68b16a2008-04-21 16:07:52 +0800435/* hwmon sys I/F */
436#include <linux/hwmon.h>
437static LIST_HEAD(thermal_hwmon_list);
438
439static ssize_t
440name_show(struct device *dev, struct device_attribute *attr, char *buf)
441{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700442 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800443 return sprintf(buf, "%s\n", hwmon->type);
444}
445static DEVICE_ATTR(name, 0444, name_show, NULL);
446
447static ssize_t
448temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
449{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000450 long temperature;
451 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800452 struct thermal_hwmon_attr *hwmon_attr
453 = container_of(attr, struct thermal_hwmon_attr, attr);
454 struct thermal_zone_device *tz
455 = container_of(hwmon_attr, struct thermal_zone_device,
456 temp_input);
457
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000458 ret = tz->ops->get_temp(tz, &temperature);
459
460 if (ret)
461 return ret;
462
463 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800464}
465
466static ssize_t
467temp_crit_show(struct device *dev, struct device_attribute *attr,
468 char *buf)
469{
470 struct thermal_hwmon_attr *hwmon_attr
471 = container_of(attr, struct thermal_hwmon_attr, attr);
472 struct thermal_zone_device *tz
473 = container_of(hwmon_attr, struct thermal_zone_device,
474 temp_crit);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000475 long temperature;
476 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800477
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000478 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
479 if (ret)
480 return ret;
481
482 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800483}
484
485
486static int
487thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
488{
489 struct thermal_hwmon_device *hwmon;
490 int new_hwmon_device = 1;
491 int result;
492
493 mutex_lock(&thermal_list_lock);
494 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
495 if (!strcmp(hwmon->type, tz->type)) {
496 new_hwmon_device = 0;
497 mutex_unlock(&thermal_list_lock);
498 goto register_sys_interface;
499 }
500 mutex_unlock(&thermal_list_lock);
501
502 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
503 if (!hwmon)
504 return -ENOMEM;
505
506 INIT_LIST_HEAD(&hwmon->tz_list);
507 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
508 hwmon->device = hwmon_device_register(NULL);
509 if (IS_ERR(hwmon->device)) {
510 result = PTR_ERR(hwmon->device);
511 goto free_mem;
512 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700513 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800514 result = device_create_file(hwmon->device, &dev_attr_name);
515 if (result)
516 goto unregister_hwmon_device;
517
518 register_sys_interface:
519 tz->hwmon = hwmon;
520 hwmon->count++;
521
522 snprintf(tz->temp_input.name, THERMAL_NAME_LENGTH,
523 "temp%d_input", hwmon->count);
524 tz->temp_input.attr.attr.name = tz->temp_input.name;
525 tz->temp_input.attr.attr.mode = 0444;
526 tz->temp_input.attr.show = temp_input_show;
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700527 sysfs_attr_init(&tz->temp_input.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800528 result = device_create_file(hwmon->device, &tz->temp_input.attr);
529 if (result)
530 goto unregister_hwmon_device;
531
532 if (tz->ops->get_crit_temp) {
533 unsigned long temperature;
534 if (!tz->ops->get_crit_temp(tz, &temperature)) {
535 snprintf(tz->temp_crit.name, THERMAL_NAME_LENGTH,
536 "temp%d_crit", hwmon->count);
537 tz->temp_crit.attr.attr.name = tz->temp_crit.name;
538 tz->temp_crit.attr.attr.mode = 0444;
539 tz->temp_crit.attr.show = temp_crit_show;
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700540 sysfs_attr_init(&tz->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800541 result = device_create_file(hwmon->device,
542 &tz->temp_crit.attr);
543 if (result)
544 goto unregister_hwmon_device;
545 }
546 }
547
548 mutex_lock(&thermal_list_lock);
549 if (new_hwmon_device)
550 list_add_tail(&hwmon->node, &thermal_hwmon_list);
551 list_add_tail(&tz->hwmon_node, &hwmon->tz_list);
552 mutex_unlock(&thermal_list_lock);
553
554 return 0;
555
556 unregister_hwmon_device:
557 device_remove_file(hwmon->device, &tz->temp_crit.attr);
558 device_remove_file(hwmon->device, &tz->temp_input.attr);
559 if (new_hwmon_device) {
560 device_remove_file(hwmon->device, &dev_attr_name);
561 hwmon_device_unregister(hwmon->device);
562 }
563 free_mem:
564 if (new_hwmon_device)
565 kfree(hwmon);
566
567 return result;
568}
569
570static void
571thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
572{
573 struct thermal_hwmon_device *hwmon = tz->hwmon;
574
575 tz->hwmon = NULL;
576 device_remove_file(hwmon->device, &tz->temp_input.attr);
577 device_remove_file(hwmon->device, &tz->temp_crit.attr);
578
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 */
844struct thermal_cooling_device *thermal_cooling_device_register(char *type,
Len Brown543a9562008-02-07 16:55:08 -0500845 void *devdata,
846 struct
847 thermal_cooling_device_ops
848 *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800849{
850 struct thermal_cooling_device *cdev;
851 struct thermal_zone_device *pos;
852 int result;
853
854 if (strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500855 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800856
857 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -0500858 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500859 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800860
861 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
862 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500863 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +0800864
865 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
866 if (result) {
867 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500868 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800869 }
870
871 strcpy(cdev->type, type);
872 cdev->ops = ops;
873 cdev->device.class = &thermal_class;
874 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -0800875 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800876 result = device_register(&cdev->device);
877 if (result) {
878 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
879 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500880 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800881 }
882
883 /* sys I/F */
884 if (type) {
Len Brown543a9562008-02-07 16:55:08 -0500885 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +0800886 if (result)
887 goto unregister;
888 }
889
890 result = device_create_file(&cdev->device, &dev_attr_max_state);
891 if (result)
892 goto unregister;
893
894 result = device_create_file(&cdev->device, &dev_attr_cur_state);
895 if (result)
896 goto unregister;
897
898 mutex_lock(&thermal_list_lock);
899 list_add(&cdev->node, &thermal_cdev_list);
900 list_for_each_entry(pos, &thermal_tz_list, node) {
901 if (!pos->ops->bind)
902 continue;
903 result = pos->ops->bind(pos, cdev);
904 if (result)
905 break;
906
907 }
908 mutex_unlock(&thermal_list_lock);
909
910 if (!result)
911 return cdev;
912
913 unregister:
914 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
915 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500916 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800917}
Len Brown543a9562008-02-07 16:55:08 -0500918
Zhang Rui203d3d42008-01-17 15:51:08 +0800919EXPORT_SYMBOL(thermal_cooling_device_register);
920
921/**
922 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +0800923 * @cdev: the thermal cooling device to remove.
924 *
925 * thermal_cooling_device_unregister() must be called when the device is no
926 * longer needed.
927 */
928void thermal_cooling_device_unregister(struct
929 thermal_cooling_device
930 *cdev)
931{
932 struct thermal_zone_device *tz;
933 struct thermal_cooling_device *pos = NULL;
934
935 if (!cdev)
936 return;
937
938 mutex_lock(&thermal_list_lock);
939 list_for_each_entry(pos, &thermal_cdev_list, node)
940 if (pos == cdev)
941 break;
942 if (pos != cdev) {
943 /* thermal cooling device not found */
944 mutex_unlock(&thermal_list_lock);
945 return;
946 }
947 list_del(&cdev->node);
948 list_for_each_entry(tz, &thermal_tz_list, node) {
949 if (!tz->ops->unbind)
950 continue;
951 tz->ops->unbind(tz, cdev);
952 }
953 mutex_unlock(&thermal_list_lock);
954 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -0500955 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +0800956 device_remove_file(&cdev->device, &dev_attr_max_state);
957 device_remove_file(&cdev->device, &dev_attr_cur_state);
958
959 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
960 device_unregister(&cdev->device);
961 return;
962}
Len Brown543a9562008-02-07 16:55:08 -0500963
Zhang Rui203d3d42008-01-17 15:51:08 +0800964EXPORT_SYMBOL(thermal_cooling_device_unregister);
965
966/**
Matthew Garrettb1569e92008-12-03 17:55:32 +0000967 * thermal_zone_device_update - force an update of a thermal zone's state
968 * @ttz: the thermal zone to update
969 */
970
971void thermal_zone_device_update(struct thermal_zone_device *tz)
972{
973 int count, ret = 0;
974 long temp, trip_temp;
975 enum thermal_trip_type trip_type;
976 struct thermal_cooling_device_instance *instance;
977 struct thermal_cooling_device *cdev;
978
979 mutex_lock(&tz->lock);
980
Michael Brunner0d288162009-08-26 14:29:25 -0700981 if (tz->ops->get_temp(tz, &temp)) {
982 /* get_temp failed - retry it later */
983 printk(KERN_WARNING PREFIX "failed to read out thermal zone "
984 "%d\n", tz->id);
985 goto leave;
986 }
Matthew Garrettb1569e92008-12-03 17:55:32 +0000987
988 for (count = 0; count < tz->trips; count++) {
989 tz->ops->get_trip_type(tz, count, &trip_type);
990 tz->ops->get_trip_temp(tz, count, &trip_temp);
991
992 switch (trip_type) {
993 case THERMAL_TRIP_CRITICAL:
Vladimir Zajac29321352009-05-06 19:34:21 +0200994 if (temp >= trip_temp) {
Matthew Garrettb1569e92008-12-03 17:55:32 +0000995 if (tz->ops->notify)
996 ret = tz->ops->notify(tz, count,
997 trip_type);
998 if (!ret) {
999 printk(KERN_EMERG
1000 "Critical temperature reached (%ld C), shutting down.\n",
1001 temp/1000);
1002 orderly_poweroff(true);
1003 }
1004 }
1005 break;
1006 case THERMAL_TRIP_HOT:
Vladimir Zajac29321352009-05-06 19:34:21 +02001007 if (temp >= trip_temp)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001008 if (tz->ops->notify)
1009 tz->ops->notify(tz, count, trip_type);
1010 break;
1011 case THERMAL_TRIP_ACTIVE:
1012 list_for_each_entry(instance, &tz->cooling_devices,
1013 node) {
1014 if (instance->trip != count)
1015 continue;
1016
1017 cdev = instance->cdev;
1018
Vladimir Zajac29321352009-05-06 19:34:21 +02001019 if (temp >= trip_temp)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001020 cdev->ops->set_cur_state(cdev, 1);
1021 else
1022 cdev->ops->set_cur_state(cdev, 0);
1023 }
1024 break;
1025 case THERMAL_TRIP_PASSIVE:
Vladimir Zajac29321352009-05-06 19:34:21 +02001026 if (temp >= trip_temp || tz->passive)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001027 thermal_zone_device_passive(tz, temp,
1028 trip_temp, count);
1029 break;
1030 }
1031 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001032
1033 if (tz->forced_passive)
1034 thermal_zone_device_passive(tz, temp, tz->forced_passive,
1035 THERMAL_TRIPS_NONE);
1036
Matthew Garrettb1569e92008-12-03 17:55:32 +00001037 tz->last_temperature = temp;
Michael Brunner0d288162009-08-26 14:29:25 -07001038
1039 leave:
Matthew Garrettb1569e92008-12-03 17:55:32 +00001040 if (tz->passive)
1041 thermal_zone_device_set_polling(tz, tz->passive_delay);
1042 else if (tz->polling_delay)
1043 thermal_zone_device_set_polling(tz, tz->polling_delay);
Frans Pop3767cb52009-10-26 08:39:04 +01001044 else
1045 thermal_zone_device_set_polling(tz, 0);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001046 mutex_unlock(&tz->lock);
1047}
1048EXPORT_SYMBOL(thermal_zone_device_update);
1049
1050/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001051 * thermal_zone_device_register - register a new thermal zone device
1052 * @type: the thermal zone device type
1053 * @trips: the number of trip points the thermal zone support
1054 * @devdata: private device data
1055 * @ops: standard thermal zone device callbacks
Matthew Garrettb1569e92008-12-03 17:55:32 +00001056 * @tc1: thermal coefficient 1 for passive calculations
1057 * @tc2: thermal coefficient 2 for passive calculations
1058 * @passive_delay: number of milliseconds to wait between polls when
1059 * performing passive cooling
1060 * @polling_delay: number of milliseconds to wait between polls when checking
1061 * whether trip points have been crossed (0 for interrupt
1062 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001063 *
1064 * thermal_zone_device_unregister() must be called when the device is no
Matthew Garrettb1569e92008-12-03 17:55:32 +00001065 * longer needed. The passive cooling formula uses tc1 and tc2 as described in
1066 * section 11.1.5.1 of the ACPI specification 3.0.
Zhang Rui203d3d42008-01-17 15:51:08 +08001067 */
1068struct thermal_zone_device *thermal_zone_device_register(char *type,
Len Brown543a9562008-02-07 16:55:08 -05001069 int trips,
1070 void *devdata, struct
1071 thermal_zone_device_ops
Matthew Garrettb1569e92008-12-03 17:55:32 +00001072 *ops, int tc1, int
1073 tc2,
1074 int passive_delay,
1075 int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001076{
1077 struct thermal_zone_device *tz;
1078 struct thermal_cooling_device *pos;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001079 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001080 int result;
1081 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001082 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001083
1084 if (strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001085 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001086
1087 if (trips > THERMAL_MAX_TRIPS || trips < 0)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001088 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001089
1090 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001091 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001092
1093 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1094 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001095 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001096
1097 INIT_LIST_HEAD(&tz->cooling_devices);
1098 idr_init(&tz->idr);
1099 mutex_init(&tz->lock);
1100 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1101 if (result) {
1102 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001103 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001104 }
1105
1106 strcpy(tz->type, type);
1107 tz->ops = ops;
1108 tz->device.class = &thermal_class;
1109 tz->devdata = devdata;
1110 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001111 tz->tc1 = tc1;
1112 tz->tc2 = tc2;
1113 tz->passive_delay = passive_delay;
1114 tz->polling_delay = polling_delay;
1115
Kay Sievers354655e2009-01-06 10:44:37 -08001116 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001117 result = device_register(&tz->device);
1118 if (result) {
1119 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1120 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001121 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001122 }
1123
1124 /* sys I/F */
1125 if (type) {
1126 result = device_create_file(&tz->device, &dev_attr_type);
1127 if (result)
1128 goto unregister;
1129 }
1130
1131 result = device_create_file(&tz->device, &dev_attr_temp);
1132 if (result)
1133 goto unregister;
1134
1135 if (ops->get_mode) {
1136 result = device_create_file(&tz->device, &dev_attr_mode);
1137 if (result)
1138 goto unregister;
1139 }
1140
1141 for (count = 0; count < trips; count++) {
1142 TRIP_POINT_ATTR_ADD(&tz->device, count, result);
1143 if (result)
1144 goto unregister;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001145 tz->ops->get_trip_type(tz, count, &trip_type);
1146 if (trip_type == THERMAL_TRIP_PASSIVE)
1147 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001148 }
1149
Matthew Garrett03a971a2008-12-03 18:00:38 +00001150 if (!passive)
1151 result = device_create_file(&tz->device,
1152 &dev_attr_passive);
1153
1154 if (result)
1155 goto unregister;
1156
Zhang Ruie68b16a2008-04-21 16:07:52 +08001157 result = thermal_add_hwmon_sysfs(tz);
1158 if (result)
1159 goto unregister;
1160
Zhang Rui203d3d42008-01-17 15:51:08 +08001161 mutex_lock(&thermal_list_lock);
1162 list_add_tail(&tz->node, &thermal_tz_list);
1163 if (ops->bind)
1164 list_for_each_entry(pos, &thermal_cdev_list, node) {
Len Brown543a9562008-02-07 16:55:08 -05001165 result = ops->bind(tz, pos);
1166 if (result)
1167 break;
Zhang Rui203d3d42008-01-17 15:51:08 +08001168 }
1169 mutex_unlock(&thermal_list_lock);
1170
Matthew Garrettb1569e92008-12-03 17:55:32 +00001171 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1172
1173 thermal_zone_device_update(tz);
1174
Zhang Rui203d3d42008-01-17 15:51:08 +08001175 if (!result)
1176 return tz;
1177
1178 unregister:
1179 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1180 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001181 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001182}
Len Brown543a9562008-02-07 16:55:08 -05001183
Zhang Rui203d3d42008-01-17 15:51:08 +08001184EXPORT_SYMBOL(thermal_zone_device_register);
1185
1186/**
1187 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001188 * @tz: the thermal zone device to remove
1189 */
1190void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1191{
1192 struct thermal_cooling_device *cdev;
1193 struct thermal_zone_device *pos = NULL;
1194 int count;
1195
1196 if (!tz)
1197 return;
1198
1199 mutex_lock(&thermal_list_lock);
1200 list_for_each_entry(pos, &thermal_tz_list, node)
1201 if (pos == tz)
1202 break;
1203 if (pos != tz) {
1204 /* thermal zone device not found */
1205 mutex_unlock(&thermal_list_lock);
1206 return;
1207 }
1208 list_del(&tz->node);
1209 if (tz->ops->unbind)
1210 list_for_each_entry(cdev, &thermal_cdev_list, node)
1211 tz->ops->unbind(tz, cdev);
1212 mutex_unlock(&thermal_list_lock);
1213
Matthew Garrettb1569e92008-12-03 17:55:32 +00001214 thermal_zone_device_set_polling(tz, 0);
1215
Zhang Rui203d3d42008-01-17 15:51:08 +08001216 if (tz->type[0])
1217 device_remove_file(&tz->device, &dev_attr_type);
1218 device_remove_file(&tz->device, &dev_attr_temp);
1219 if (tz->ops->get_mode)
1220 device_remove_file(&tz->device, &dev_attr_mode);
1221
1222 for (count = 0; count < tz->trips; count++)
1223 TRIP_POINT_ATTR_REMOVE(&tz->device, count);
1224
Zhang Ruie68b16a2008-04-21 16:07:52 +08001225 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001226 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1227 idr_destroy(&tz->idr);
1228 mutex_destroy(&tz->lock);
1229 device_unregister(&tz->device);
1230 return;
1231}
Len Brown543a9562008-02-07 16:55:08 -05001232
Zhang Rui203d3d42008-01-17 15:51:08 +08001233EXPORT_SYMBOL(thermal_zone_device_unregister);
1234
R.Durgadoss4cb18722010-10-27 03:33:29 +05301235int generate_netlink_event(u32 orig, enum events event)
1236{
1237 struct sk_buff *skb;
1238 struct nlattr *attr;
1239 struct thermal_genl_event *thermal_event;
1240 void *msg_header;
1241 int size;
1242 int result;
1243
1244 /* allocate memory */
1245 size = nla_total_size(sizeof(struct thermal_genl_event)) + \
1246 nla_total_size(0);
1247
1248 skb = genlmsg_new(size, GFP_ATOMIC);
1249 if (!skb)
1250 return -ENOMEM;
1251
1252 /* add the genetlink message header */
1253 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1254 &thermal_event_genl_family, 0,
1255 THERMAL_GENL_CMD_EVENT);
1256 if (!msg_header) {
1257 nlmsg_free(skb);
1258 return -ENOMEM;
1259 }
1260
1261 /* fill the data */
1262 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT, \
1263 sizeof(struct thermal_genl_event));
1264
1265 if (!attr) {
1266 nlmsg_free(skb);
1267 return -EINVAL;
1268 }
1269
1270 thermal_event = nla_data(attr);
1271 if (!thermal_event) {
1272 nlmsg_free(skb);
1273 return -EINVAL;
1274 }
1275
1276 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1277
1278 thermal_event->orig = orig;
1279 thermal_event->event = event;
1280
1281 /* send multicast genetlink message */
1282 result = genlmsg_end(skb, msg_header);
1283 if (result < 0) {
1284 nlmsg_free(skb);
1285 return result;
1286 }
1287
1288 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1289 if (result)
1290 printk(KERN_INFO "failed to send netlink event:%d", result);
1291
1292 return result;
1293}
1294EXPORT_SYMBOL(generate_netlink_event);
1295
1296static int genetlink_init(void)
1297{
1298 int result;
1299
1300 result = genl_register_family(&thermal_event_genl_family);
1301 if (result)
1302 return result;
1303
1304 result = genl_register_mc_group(&thermal_event_genl_family,
1305 &thermal_event_mcgrp);
1306 if (result)
1307 genl_unregister_family(&thermal_event_genl_family);
1308 return result;
1309}
1310
Zhang Rui203d3d42008-01-17 15:51:08 +08001311static int __init thermal_init(void)
1312{
1313 int result = 0;
1314
1315 result = class_register(&thermal_class);
1316 if (result) {
1317 idr_destroy(&thermal_tz_idr);
1318 idr_destroy(&thermal_cdev_idr);
1319 mutex_destroy(&thermal_idr_lock);
1320 mutex_destroy(&thermal_list_lock);
1321 }
R.Durgadoss4cb18722010-10-27 03:33:29 +05301322 result = genetlink_init();
Zhang Rui203d3d42008-01-17 15:51:08 +08001323 return result;
1324}
1325
R.Durgadoss4cb18722010-10-27 03:33:29 +05301326static void genetlink_exit(void)
1327{
1328 genl_unregister_family(&thermal_event_genl_family);
1329}
1330
Zhang Rui203d3d42008-01-17 15:51:08 +08001331static void __exit thermal_exit(void)
1332{
1333 class_unregister(&thermal_class);
1334 idr_destroy(&thermal_tz_idr);
1335 idr_destroy(&thermal_cdev_idr);
1336 mutex_destroy(&thermal_idr_lock);
1337 mutex_destroy(&thermal_list_lock);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301338 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001339}
1340
R.Durgadoss4cb18722010-10-27 03:33:29 +05301341fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001342module_exit(thermal_exit);