blob: b04fe2c4b0d5ef0c9c554cddd4dba2f967ee956b [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
Joe Perchesc5a01dd2012-03-21 12:55:02 -070026#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
Zhang Rui203d3d42008-01-17 15:51:08 +080028#include <linux/module.h>
29#include <linux/device.h>
30#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080032#include <linux/kdev_t.h>
33#include <linux/idr.h>
34#include <linux/thermal.h>
35#include <linux/spinlock.h>
Matthew Garrettb1569e92008-12-03 17:55:32 +000036#include <linux/reboot.h>
R.Durgadoss4cb18722010-10-27 03:33:29 +053037#include <net/netlink.h>
38#include <net/genetlink.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080039
Zhang Rui63c4ec92008-04-21 16:07:13 +080040MODULE_AUTHOR("Zhang Rui");
Zhang Rui203d3d42008-01-17 15:51:08 +080041MODULE_DESCRIPTION("Generic thermal management sysfs support");
42MODULE_LICENSE("GPL");
43
Zhang Rui74051ba2012-06-26 16:27:22 +080044/*
45 * This structure is used to describe the behavior of
46 * a certain cooling device on a certain trip point
47 * in a certain thermal zone
48 */
Zhang Rui203d3d42008-01-17 15:51:08 +080049struct thermal_cooling_device_instance {
50 int id;
51 char name[THERMAL_NAME_LENGTH];
52 struct thermal_zone_device *tz;
53 struct thermal_cooling_device *cdev;
54 int trip;
Zhang Rui74051ba2012-06-26 16:27:22 +080055 unsigned long upper; /* Highest cooling state for this trip point */
56 unsigned long lower; /* Lowest cooling state for this trip point */
Zhang Rui203d3d42008-01-17 15:51:08 +080057 char attr_name[THERMAL_NAME_LENGTH];
58 struct device_attribute attr;
59 struct list_head node;
60};
61
62static DEFINE_IDR(thermal_tz_idr);
63static DEFINE_IDR(thermal_cdev_idr);
64static DEFINE_MUTEX(thermal_idr_lock);
65
66static LIST_HEAD(thermal_tz_list);
67static LIST_HEAD(thermal_cdev_list);
68static DEFINE_MUTEX(thermal_list_lock);
69
70static int get_idr(struct idr *idr, struct mutex *lock, int *id)
71{
72 int err;
73
Joe Perchescaca8b82012-03-21 12:55:02 -070074again:
Zhang Rui203d3d42008-01-17 15:51:08 +080075 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
76 return -ENOMEM;
77
78 if (lock)
79 mutex_lock(lock);
80 err = idr_get_new(idr, NULL, id);
81 if (lock)
82 mutex_unlock(lock);
83 if (unlikely(err == -EAGAIN))
84 goto again;
85 else if (unlikely(err))
86 return err;
87
88 *id = *id & MAX_ID_MASK;
89 return 0;
90}
91
92static void release_idr(struct idr *idr, struct mutex *lock, int id)
93{
94 if (lock)
95 mutex_lock(lock);
96 idr_remove(idr, id);
97 if (lock)
98 mutex_unlock(lock);
99}
100
101/* sys I/F for thermal zone */
102
103#define to_thermal_zone(_dev) \
104 container_of(_dev, struct thermal_zone_device, device)
105
106static ssize_t
107type_show(struct device *dev, struct device_attribute *attr, char *buf)
108{
109 struct thermal_zone_device *tz = to_thermal_zone(dev);
110
111 return sprintf(buf, "%s\n", tz->type);
112}
113
114static ssize_t
115temp_show(struct device *dev, struct device_attribute *attr, char *buf)
116{
117 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000118 long temperature;
119 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800120
121 if (!tz->ops->get_temp)
122 return -EPERM;
123
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000124 ret = tz->ops->get_temp(tz, &temperature);
125
126 if (ret)
127 return ret;
128
129 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800130}
131
132static ssize_t
133mode_show(struct device *dev, struct device_attribute *attr, char *buf)
134{
135 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000136 enum thermal_device_mode mode;
137 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800138
139 if (!tz->ops->get_mode)
140 return -EPERM;
141
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000142 result = tz->ops->get_mode(tz, &mode);
143 if (result)
144 return result;
145
146 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
147 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800148}
149
150static ssize_t
151mode_store(struct device *dev, struct device_attribute *attr,
152 const char *buf, size_t count)
153{
154 struct thermal_zone_device *tz = to_thermal_zone(dev);
155 int result;
156
157 if (!tz->ops->set_mode)
158 return -EPERM;
159
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530160 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000161 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530162 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000163 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
164 else
165 result = -EINVAL;
166
Zhang Rui203d3d42008-01-17 15:51:08 +0800167 if (result)
168 return result;
169
170 return count;
171}
172
173static ssize_t
174trip_point_type_show(struct device *dev, struct device_attribute *attr,
175 char *buf)
176{
177 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000178 enum thermal_trip_type type;
179 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800180
181 if (!tz->ops->get_trip_type)
182 return -EPERM;
183
184 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
185 return -EINVAL;
186
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000187 result = tz->ops->get_trip_type(tz, trip, &type);
188 if (result)
189 return result;
190
191 switch (type) {
192 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300193 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000194 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300195 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000196 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300197 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000198 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300199 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000200 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300201 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000202 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800203}
204
205static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800206trip_point_temp_store(struct device *dev, struct device_attribute *attr,
207 const char *buf, size_t count)
208{
209 struct thermal_zone_device *tz = to_thermal_zone(dev);
210 int trip, ret;
211 unsigned long temperature;
212
213 if (!tz->ops->set_trip_temp)
214 return -EPERM;
215
216 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
217 return -EINVAL;
218
219 if (kstrtoul(buf, 10, &temperature))
220 return -EINVAL;
221
222 ret = tz->ops->set_trip_temp(tz, trip, temperature);
223
224 return ret ? ret : count;
225}
226
227static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800228trip_point_temp_show(struct device *dev, struct device_attribute *attr,
229 char *buf)
230{
231 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000232 int trip, ret;
233 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800234
235 if (!tz->ops->get_trip_temp)
236 return -EPERM;
237
238 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
239 return -EINVAL;
240
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000241 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
242
243 if (ret)
244 return ret;
245
246 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800247}
248
Matthew Garrett03a971a2008-12-03 18:00:38 +0000249static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800250trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
251 const char *buf, size_t count)
252{
253 struct thermal_zone_device *tz = to_thermal_zone(dev);
254 int trip, ret;
255 unsigned long temperature;
256
257 if (!tz->ops->set_trip_hyst)
258 return -EPERM;
259
260 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
261 return -EINVAL;
262
263 if (kstrtoul(buf, 10, &temperature))
264 return -EINVAL;
265
266 /*
267 * We are not doing any check on the 'temperature' value
268 * here. The driver implementing 'set_trip_hyst' has to
269 * take care of this.
270 */
271 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
272
273 return ret ? ret : count;
274}
275
276static ssize_t
277trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
278 char *buf)
279{
280 struct thermal_zone_device *tz = to_thermal_zone(dev);
281 int trip, ret;
282 unsigned long temperature;
283
284 if (!tz->ops->get_trip_hyst)
285 return -EPERM;
286
287 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
288 return -EINVAL;
289
290 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
291
292 return ret ? ret : sprintf(buf, "%ld\n", temperature);
293}
294
295static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000296passive_store(struct device *dev, struct device_attribute *attr,
297 const char *buf, size_t count)
298{
299 struct thermal_zone_device *tz = to_thermal_zone(dev);
300 struct thermal_cooling_device *cdev = NULL;
301 int state;
302
303 if (!sscanf(buf, "%d\n", &state))
304 return -EINVAL;
305
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100306 /* sanity check: values below 1000 millicelcius don't make sense
307 * and can cause the system to go into a thermal heart attack
308 */
309 if (state && state < 1000)
310 return -EINVAL;
311
Matthew Garrett03a971a2008-12-03 18:00:38 +0000312 if (state && !tz->forced_passive) {
313 mutex_lock(&thermal_list_lock);
314 list_for_each_entry(cdev, &thermal_cdev_list, node) {
315 if (!strncmp("Processor", cdev->type,
316 sizeof("Processor")))
317 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800318 THERMAL_TRIPS_NONE, cdev,
319 THERMAL_NO_LIMIT,
320 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000321 }
322 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100323 if (!tz->passive_delay)
324 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000325 } else if (!state && tz->forced_passive) {
326 mutex_lock(&thermal_list_lock);
327 list_for_each_entry(cdev, &thermal_cdev_list, node) {
328 if (!strncmp("Processor", cdev->type,
329 sizeof("Processor")))
330 thermal_zone_unbind_cooling_device(tz,
331 THERMAL_TRIPS_NONE,
332 cdev);
333 }
334 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100335 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000336 }
337
338 tz->tc1 = 1;
339 tz->tc2 = 1;
340
Matthew Garrett03a971a2008-12-03 18:00:38 +0000341 tz->forced_passive = state;
342
343 thermal_zone_device_update(tz);
344
345 return count;
346}
347
348static ssize_t
349passive_show(struct device *dev, struct device_attribute *attr,
350 char *buf)
351{
352 struct thermal_zone_device *tz = to_thermal_zone(dev);
353
354 return sprintf(buf, "%d\n", tz->forced_passive);
355}
356
Zhang Rui203d3d42008-01-17 15:51:08 +0800357static DEVICE_ATTR(type, 0444, type_show, NULL);
358static DEVICE_ATTR(temp, 0444, temp_show, NULL);
359static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700360static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800361
Zhang Rui203d3d42008-01-17 15:51:08 +0800362/* sys I/F for cooling device */
363#define to_cooling_device(_dev) \
364 container_of(_dev, struct thermal_cooling_device, device)
365
366static ssize_t
367thermal_cooling_device_type_show(struct device *dev,
368 struct device_attribute *attr, char *buf)
369{
370 struct thermal_cooling_device *cdev = to_cooling_device(dev);
371
372 return sprintf(buf, "%s\n", cdev->type);
373}
374
375static ssize_t
376thermal_cooling_device_max_state_show(struct device *dev,
377 struct device_attribute *attr, char *buf)
378{
379 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000380 unsigned long state;
381 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800382
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000383 ret = cdev->ops->get_max_state(cdev, &state);
384 if (ret)
385 return ret;
386 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800387}
388
389static ssize_t
390thermal_cooling_device_cur_state_show(struct device *dev,
391 struct device_attribute *attr, char *buf)
392{
393 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000394 unsigned long state;
395 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800396
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000397 ret = cdev->ops->get_cur_state(cdev, &state);
398 if (ret)
399 return ret;
400 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800401}
402
403static ssize_t
404thermal_cooling_device_cur_state_store(struct device *dev,
405 struct device_attribute *attr,
406 const char *buf, size_t count)
407{
408 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000409 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800410 int result;
411
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000412 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800413 return -EINVAL;
414
Roel Kluinedb94912009-12-15 22:46:50 +0100415 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800416 return -EINVAL;
417
418 result = cdev->ops->set_cur_state(cdev, state);
419 if (result)
420 return result;
421 return count;
422}
423
424static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500425__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800426static DEVICE_ATTR(max_state, 0444,
427 thermal_cooling_device_max_state_show, NULL);
428static DEVICE_ATTR(cur_state, 0644,
429 thermal_cooling_device_cur_state_show,
430 thermal_cooling_device_cur_state_store);
431
432static ssize_t
433thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500434 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800435{
436 struct thermal_cooling_device_instance *instance;
437
438 instance =
439 container_of(attr, struct thermal_cooling_device_instance, attr);
440
441 if (instance->trip == THERMAL_TRIPS_NONE)
442 return sprintf(buf, "-1\n");
443 else
444 return sprintf(buf, "%d\n", instance->trip);
445}
446
447/* Device management */
448
Rene Herman16d75232008-06-24 19:38:56 +0200449#if defined(CONFIG_THERMAL_HWMON)
450
Zhang Ruie68b16a2008-04-21 16:07:52 +0800451/* hwmon sys I/F */
452#include <linux/hwmon.h>
Jean Delvare31f53962011-07-28 13:48:42 -0700453
454/* thermal zone devices with the same type share one hwmon device */
455struct thermal_hwmon_device {
456 char type[THERMAL_NAME_LENGTH];
457 struct device *device;
458 int count;
459 struct list_head tz_list;
460 struct list_head node;
461};
462
463struct thermal_hwmon_attr {
464 struct device_attribute attr;
465 char name[16];
466};
467
468/* one temperature input for each thermal zone */
469struct thermal_hwmon_temp {
470 struct list_head hwmon_node;
471 struct thermal_zone_device *tz;
472 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
473 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
474};
475
Zhang Ruie68b16a2008-04-21 16:07:52 +0800476static LIST_HEAD(thermal_hwmon_list);
477
478static ssize_t
479name_show(struct device *dev, struct device_attribute *attr, char *buf)
480{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700481 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800482 return sprintf(buf, "%s\n", hwmon->type);
483}
484static DEVICE_ATTR(name, 0444, name_show, NULL);
485
486static ssize_t
487temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
488{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000489 long temperature;
490 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800491 struct thermal_hwmon_attr *hwmon_attr
492 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700493 struct thermal_hwmon_temp *temp
494 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800495 temp_input);
Jean Delvare31f53962011-07-28 13:48:42 -0700496 struct thermal_zone_device *tz = temp->tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800497
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000498 ret = tz->ops->get_temp(tz, &temperature);
499
500 if (ret)
501 return ret;
502
503 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800504}
505
506static ssize_t
507temp_crit_show(struct device *dev, struct device_attribute *attr,
508 char *buf)
509{
510 struct thermal_hwmon_attr *hwmon_attr
511 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700512 struct thermal_hwmon_temp *temp
513 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800514 temp_crit);
Jean Delvare31f53962011-07-28 13:48:42 -0700515 struct thermal_zone_device *tz = temp->tz;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000516 long temperature;
517 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800518
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000519 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
520 if (ret)
521 return ret;
522
523 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800524}
525
526
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700527static struct thermal_hwmon_device *
528thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
529{
530 struct thermal_hwmon_device *hwmon;
531
532 mutex_lock(&thermal_list_lock);
533 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
534 if (!strcmp(hwmon->type, tz->type)) {
535 mutex_unlock(&thermal_list_lock);
536 return hwmon;
537 }
538 mutex_unlock(&thermal_list_lock);
539
540 return NULL;
541}
542
Jean Delvare31f53962011-07-28 13:48:42 -0700543/* Find the temperature input matching a given thermal zone */
544static struct thermal_hwmon_temp *
545thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
546 const struct thermal_zone_device *tz)
547{
548 struct thermal_hwmon_temp *temp;
549
550 mutex_lock(&thermal_list_lock);
551 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
552 if (temp->tz == tz) {
553 mutex_unlock(&thermal_list_lock);
554 return temp;
555 }
556 mutex_unlock(&thermal_list_lock);
557
558 return NULL;
559}
560
Zhang Ruie68b16a2008-04-21 16:07:52 +0800561static int
562thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
563{
564 struct thermal_hwmon_device *hwmon;
Jean Delvare31f53962011-07-28 13:48:42 -0700565 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800566 int new_hwmon_device = 1;
567 int result;
568
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700569 hwmon = thermal_hwmon_lookup_by_type(tz);
570 if (hwmon) {
571 new_hwmon_device = 0;
572 goto register_sys_interface;
573 }
Zhang Ruie68b16a2008-04-21 16:07:52 +0800574
575 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
576 if (!hwmon)
577 return -ENOMEM;
578
579 INIT_LIST_HEAD(&hwmon->tz_list);
580 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
581 hwmon->device = hwmon_device_register(NULL);
582 if (IS_ERR(hwmon->device)) {
583 result = PTR_ERR(hwmon->device);
584 goto free_mem;
585 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700586 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800587 result = device_create_file(hwmon->device, &dev_attr_name);
588 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530589 goto free_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800590
591 register_sys_interface:
Jean Delvare31f53962011-07-28 13:48:42 -0700592 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
593 if (!temp) {
594 result = -ENOMEM;
595 goto unregister_name;
596 }
597
598 temp->tz = tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800599 hwmon->count++;
600
Jean Delvare31f53962011-07-28 13:48:42 -0700601 snprintf(temp->temp_input.name, THERMAL_NAME_LENGTH,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800602 "temp%d_input", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700603 temp->temp_input.attr.attr.name = temp->temp_input.name;
604 temp->temp_input.attr.attr.mode = 0444;
605 temp->temp_input.attr.show = temp_input_show;
606 sysfs_attr_init(&temp->temp_input.attr.attr);
607 result = device_create_file(hwmon->device, &temp->temp_input.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800608 if (result)
Jean Delvare31f53962011-07-28 13:48:42 -0700609 goto free_temp_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800610
611 if (tz->ops->get_crit_temp) {
612 unsigned long temperature;
613 if (!tz->ops->get_crit_temp(tz, &temperature)) {
Jean Delvare31f53962011-07-28 13:48:42 -0700614 snprintf(temp->temp_crit.name, THERMAL_NAME_LENGTH,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800615 "temp%d_crit", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700616 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
617 temp->temp_crit.attr.attr.mode = 0444;
618 temp->temp_crit.attr.show = temp_crit_show;
619 sysfs_attr_init(&temp->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800620 result = device_create_file(hwmon->device,
Jean Delvare31f53962011-07-28 13:48:42 -0700621 &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800622 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530623 goto unregister_input;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800624 }
625 }
626
627 mutex_lock(&thermal_list_lock);
628 if (new_hwmon_device)
629 list_add_tail(&hwmon->node, &thermal_hwmon_list);
Jean Delvare31f53962011-07-28 13:48:42 -0700630 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800631 mutex_unlock(&thermal_list_lock);
632
633 return 0;
634
Durgadoss Rb299eb52011-03-03 04:30:13 +0530635 unregister_input:
Jean Delvare31f53962011-07-28 13:48:42 -0700636 device_remove_file(hwmon->device, &temp->temp_input.attr);
637 free_temp_mem:
638 kfree(temp);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530639 unregister_name:
Zhang Ruie68b16a2008-04-21 16:07:52 +0800640 if (new_hwmon_device) {
641 device_remove_file(hwmon->device, &dev_attr_name);
642 hwmon_device_unregister(hwmon->device);
643 }
644 free_mem:
645 if (new_hwmon_device)
646 kfree(hwmon);
647
648 return result;
649}
650
651static void
652thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
653{
Jean Delvare31f53962011-07-28 13:48:42 -0700654 struct thermal_hwmon_device *hwmon;
655 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800656
Jean Delvare31f53962011-07-28 13:48:42 -0700657 hwmon = thermal_hwmon_lookup_by_type(tz);
658 if (unlikely(!hwmon)) {
659 /* Should never happen... */
660 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
661 return;
662 }
663
664 temp = thermal_hwmon_lookup_temp(hwmon, tz);
665 if (unlikely(!temp)) {
666 /* Should never happen... */
667 dev_dbg(&tz->device, "temperature input lookup failed!\n");
668 return;
669 }
670
671 device_remove_file(hwmon->device, &temp->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530672 if (tz->ops->get_crit_temp)
Jean Delvare31f53962011-07-28 13:48:42 -0700673 device_remove_file(hwmon->device, &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800674
675 mutex_lock(&thermal_list_lock);
Jean Delvare31f53962011-07-28 13:48:42 -0700676 list_del(&temp->hwmon_node);
677 kfree(temp);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800678 if (!list_empty(&hwmon->tz_list)) {
679 mutex_unlock(&thermal_list_lock);
680 return;
681 }
682 list_del(&hwmon->node);
683 mutex_unlock(&thermal_list_lock);
684
685 device_remove_file(hwmon->device, &dev_attr_name);
686 hwmon_device_unregister(hwmon->device);
687 kfree(hwmon);
688}
689#else
690static int
691thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
692{
693 return 0;
694}
695
696static void
697thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
698{
699}
700#endif
701
Matthew Garrettb1569e92008-12-03 17:55:32 +0000702static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
703 int delay)
704{
705 cancel_delayed_work(&(tz->poll_queue));
706
707 if (!delay)
708 return;
709
710 if (delay > 1000)
Rafael J. Wysocki51e20d02011-11-06 14:21:38 +0100711 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
Matthew Garrettb1569e92008-12-03 17:55:32 +0000712 round_jiffies(msecs_to_jiffies(delay)));
713 else
Rafael J. Wysocki51e20d02011-11-06 14:21:38 +0100714 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
Matthew Garrettb1569e92008-12-03 17:55:32 +0000715 msecs_to_jiffies(delay));
716}
717
718static void thermal_zone_device_passive(struct thermal_zone_device *tz,
719 int temp, int trip_temp, int trip)
720{
721 int trend = 0;
722 struct thermal_cooling_device_instance *instance;
723 struct thermal_cooling_device *cdev;
724 long state, max_state;
725
726 /*
727 * Above Trip?
728 * -----------
729 * Calculate the thermal trend (using the passive cooling equation)
730 * and modify the performance limit for all passive cooling devices
731 * accordingly. Note that we assume symmetry.
732 */
733 if (temp >= trip_temp) {
734 tz->passive = true;
735
736 trend = (tz->tc1 * (temp - tz->last_temperature)) +
737 (tz->tc2 * (temp - trip_temp));
738
739 /* Heating up? */
740 if (trend > 0) {
741 list_for_each_entry(instance, &tz->cooling_devices,
742 node) {
743 if (instance->trip != trip)
744 continue;
745 cdev = instance->cdev;
746 cdev->ops->get_cur_state(cdev, &state);
747 cdev->ops->get_max_state(cdev, &max_state);
748 if (state++ < max_state)
749 cdev->ops->set_cur_state(cdev, state);
750 }
751 } else if (trend < 0) { /* Cooling off? */
752 list_for_each_entry(instance, &tz->cooling_devices,
753 node) {
754 if (instance->trip != trip)
755 continue;
756 cdev = instance->cdev;
757 cdev->ops->get_cur_state(cdev, &state);
758 cdev->ops->get_max_state(cdev, &max_state);
759 if (state > 0)
760 cdev->ops->set_cur_state(cdev, --state);
761 }
762 }
763 return;
764 }
765
766 /*
767 * Below Trip?
768 * -----------
769 * Implement passive cooling hysteresis to slowly increase performance
770 * and avoid thrashing around the passive trip point. Note that we
771 * assume symmetry.
772 */
773 list_for_each_entry(instance, &tz->cooling_devices, node) {
774 if (instance->trip != trip)
775 continue;
776 cdev = instance->cdev;
777 cdev->ops->get_cur_state(cdev, &state);
778 cdev->ops->get_max_state(cdev, &max_state);
779 if (state > 0)
780 cdev->ops->set_cur_state(cdev, --state);
781 if (state == 0)
782 tz->passive = false;
783 }
784}
785
786static void thermal_zone_device_check(struct work_struct *work)
787{
788 struct thermal_zone_device *tz = container_of(work, struct
789 thermal_zone_device,
790 poll_queue.work);
791 thermal_zone_device_update(tz);
792}
Zhang Ruie68b16a2008-04-21 16:07:52 +0800793
Zhang Rui203d3d42008-01-17 15:51:08 +0800794/**
795 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800796 * @tz: thermal zone device
797 * @trip: indicates which trip point the cooling devices is
798 * associated with in this thermal zone.
799 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500800 *
801 * This function is usually called in the thermal zone device .bind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800802 */
803int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
804 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800805 struct thermal_cooling_device *cdev,
806 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +0800807{
808 struct thermal_cooling_device_instance *dev;
809 struct thermal_cooling_device_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500810 struct thermal_zone_device *pos1;
811 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800812 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800813 int result;
814
Len Brown543a9562008-02-07 16:55:08 -0500815 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800816 return -EINVAL;
817
Thomas Sujithc7516702008-02-15 00:58:50 -0500818 list_for_each_entry(pos1, &thermal_tz_list, node) {
819 if (pos1 == tz)
820 break;
821 }
822 list_for_each_entry(pos2, &thermal_cdev_list, node) {
823 if (pos2 == cdev)
824 break;
825 }
826
827 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800828 return -EINVAL;
829
Zhang Rui9d998422012-06-26 16:35:57 +0800830 cdev->ops->get_max_state(cdev, &max_state);
831
832 /* lower default 0, upper default max_state */
833 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
834 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
835
836 if (lower > upper || upper > max_state)
837 return -EINVAL;
838
Zhang Rui203d3d42008-01-17 15:51:08 +0800839 dev =
840 kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL);
841 if (!dev)
842 return -ENOMEM;
843 dev->tz = tz;
844 dev->cdev = cdev;
845 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800846 dev->upper = upper;
847 dev->lower = lower;
Zhang Rui74051ba2012-06-26 16:27:22 +0800848
Zhang Rui203d3d42008-01-17 15:51:08 +0800849 result = get_idr(&tz->idr, &tz->lock, &dev->id);
850 if (result)
851 goto free_mem;
852
853 sprintf(dev->name, "cdev%d", dev->id);
854 result =
855 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
856 if (result)
857 goto release_idr;
858
859 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700860 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800861 dev->attr.attr.name = dev->attr_name;
862 dev->attr.attr.mode = 0444;
863 dev->attr.show = thermal_cooling_device_trip_point_show;
864 result = device_create_file(&tz->device, &dev->attr);
865 if (result)
866 goto remove_symbol_link;
867
868 mutex_lock(&tz->lock);
869 list_for_each_entry(pos, &tz->cooling_devices, node)
870 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
871 result = -EEXIST;
872 break;
873 }
874 if (!result)
875 list_add_tail(&dev->node, &tz->cooling_devices);
876 mutex_unlock(&tz->lock);
877
878 if (!result)
879 return 0;
880
881 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -0700882remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800883 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -0700884release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +0800885 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -0700886free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800887 kfree(dev);
888 return result;
889}
890EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
891
892/**
893 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800894 * @tz: thermal zone device
895 * @trip: indicates which trip point the cooling devices is
896 * associated with in this thermal zone.
897 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500898 *
899 * This function is usually called in the thermal zone device .unbind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800900 */
901int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
902 int trip,
903 struct thermal_cooling_device *cdev)
904{
905 struct thermal_cooling_device_instance *pos, *next;
906
907 mutex_lock(&tz->lock);
908 list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
Len Brown543a9562008-02-07 16:55:08 -0500909 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800910 list_del(&pos->node);
911 mutex_unlock(&tz->lock);
912 goto unbind;
913 }
914 }
915 mutex_unlock(&tz->lock);
916
917 return -ENODEV;
918
Joe Perchescaca8b82012-03-21 12:55:02 -0700919unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +0800920 device_remove_file(&tz->device, &pos->attr);
921 sysfs_remove_link(&tz->device.kobj, pos->name);
922 release_idr(&tz->idr, &tz->lock, pos->id);
923 kfree(pos);
924 return 0;
925}
926EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
927
928static void thermal_release(struct device *dev)
929{
930 struct thermal_zone_device *tz;
931 struct thermal_cooling_device *cdev;
932
Joe Perchescaca8b82012-03-21 12:55:02 -0700933 if (!strncmp(dev_name(dev), "thermal_zone",
934 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800935 tz = to_thermal_zone(dev);
936 kfree(tz);
937 } else {
938 cdev = to_cooling_device(dev);
939 kfree(cdev);
940 }
941}
942
943static struct class thermal_class = {
944 .name = "thermal",
945 .dev_release = thermal_release,
946};
947
948/**
949 * thermal_cooling_device_register - register a new thermal cooling device
950 * @type: the thermal cooling device type.
951 * @devdata: device private data.
952 * @ops: standard thermal cooling devices callbacks.
953 */
Joe Perchescaca8b82012-03-21 12:55:02 -0700954struct thermal_cooling_device *
955thermal_cooling_device_register(char *type, void *devdata,
956 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800957{
958 struct thermal_cooling_device *cdev;
959 struct thermal_zone_device *pos;
960 int result;
961
962 if (strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500963 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800964
965 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -0500966 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500967 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800968
969 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
970 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500971 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +0800972
973 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
974 if (result) {
975 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500976 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800977 }
978
979 strcpy(cdev->type, type);
980 cdev->ops = ops;
981 cdev->device.class = &thermal_class;
982 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -0800983 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800984 result = device_register(&cdev->device);
985 if (result) {
986 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
987 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500988 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800989 }
990
991 /* sys I/F */
992 if (type) {
Len Brown543a9562008-02-07 16:55:08 -0500993 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +0800994 if (result)
995 goto unregister;
996 }
997
998 result = device_create_file(&cdev->device, &dev_attr_max_state);
999 if (result)
1000 goto unregister;
1001
1002 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1003 if (result)
1004 goto unregister;
1005
1006 mutex_lock(&thermal_list_lock);
1007 list_add(&cdev->node, &thermal_cdev_list);
1008 list_for_each_entry(pos, &thermal_tz_list, node) {
1009 if (!pos->ops->bind)
1010 continue;
1011 result = pos->ops->bind(pos, cdev);
1012 if (result)
1013 break;
1014
1015 }
1016 mutex_unlock(&thermal_list_lock);
1017
1018 if (!result)
1019 return cdev;
1020
Joe Perchescaca8b82012-03-21 12:55:02 -07001021unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001022 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1023 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001024 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001025}
1026EXPORT_SYMBOL(thermal_cooling_device_register);
1027
1028/**
1029 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001030 * @cdev: the thermal cooling device to remove.
1031 *
1032 * thermal_cooling_device_unregister() must be called when the device is no
1033 * longer needed.
1034 */
1035void thermal_cooling_device_unregister(struct
1036 thermal_cooling_device
1037 *cdev)
1038{
1039 struct thermal_zone_device *tz;
1040 struct thermal_cooling_device *pos = NULL;
1041
1042 if (!cdev)
1043 return;
1044
1045 mutex_lock(&thermal_list_lock);
1046 list_for_each_entry(pos, &thermal_cdev_list, node)
1047 if (pos == cdev)
1048 break;
1049 if (pos != cdev) {
1050 /* thermal cooling device not found */
1051 mutex_unlock(&thermal_list_lock);
1052 return;
1053 }
1054 list_del(&cdev->node);
1055 list_for_each_entry(tz, &thermal_tz_list, node) {
1056 if (!tz->ops->unbind)
1057 continue;
1058 tz->ops->unbind(tz, cdev);
1059 }
1060 mutex_unlock(&thermal_list_lock);
1061 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001062 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001063 device_remove_file(&cdev->device, &dev_attr_max_state);
1064 device_remove_file(&cdev->device, &dev_attr_cur_state);
1065
1066 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1067 device_unregister(&cdev->device);
1068 return;
1069}
1070EXPORT_SYMBOL(thermal_cooling_device_unregister);
1071
1072/**
Matthew Garrettb1569e92008-12-03 17:55:32 +00001073 * thermal_zone_device_update - force an update of a thermal zone's state
1074 * @ttz: the thermal zone to update
1075 */
1076
1077void thermal_zone_device_update(struct thermal_zone_device *tz)
1078{
1079 int count, ret = 0;
1080 long temp, trip_temp;
1081 enum thermal_trip_type trip_type;
1082 struct thermal_cooling_device_instance *instance;
1083 struct thermal_cooling_device *cdev;
Zhang Ruie3f25e62012-06-26 16:26:40 +08001084 unsigned long cur_state, max_state;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001085
1086 mutex_lock(&tz->lock);
1087
Michael Brunner0d288162009-08-26 14:29:25 -07001088 if (tz->ops->get_temp(tz, &temp)) {
1089 /* get_temp failed - retry it later */
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001090 pr_warn("failed to read out thermal zone %d\n", tz->id);
Michael Brunner0d288162009-08-26 14:29:25 -07001091 goto leave;
1092 }
Matthew Garrettb1569e92008-12-03 17:55:32 +00001093
1094 for (count = 0; count < tz->trips; count++) {
1095 tz->ops->get_trip_type(tz, count, &trip_type);
1096 tz->ops->get_trip_temp(tz, count, &trip_temp);
1097
1098 switch (trip_type) {
1099 case THERMAL_TRIP_CRITICAL:
Vladimir Zajac29321352009-05-06 19:34:21 +02001100 if (temp >= trip_temp) {
Matthew Garrettb1569e92008-12-03 17:55:32 +00001101 if (tz->ops->notify)
1102 ret = tz->ops->notify(tz, count,
1103 trip_type);
1104 if (!ret) {
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001105 pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1106 temp/1000);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001107 orderly_poweroff(true);
1108 }
1109 }
1110 break;
1111 case THERMAL_TRIP_HOT:
Vladimir Zajac29321352009-05-06 19:34:21 +02001112 if (temp >= trip_temp)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001113 if (tz->ops->notify)
1114 tz->ops->notify(tz, count, trip_type);
1115 break;
1116 case THERMAL_TRIP_ACTIVE:
1117 list_for_each_entry(instance, &tz->cooling_devices,
1118 node) {
1119 if (instance->trip != count)
1120 continue;
1121
1122 cdev = instance->cdev;
1123
Zhang Ruie3f25e62012-06-26 16:26:40 +08001124 cdev->ops->get_cur_state(cdev, &cur_state);
1125 cdev->ops->get_max_state(cdev, &max_state);
1126
Vladimir Zajac29321352009-05-06 19:34:21 +02001127 if (temp >= trip_temp)
Zhang Rui74051ba2012-06-26 16:27:22 +08001128 cur_state =
1129 cur_state < instance->upper ?
1130 (cur_state + 1) :
1131 instance->upper;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001132 else
Zhang Rui74051ba2012-06-26 16:27:22 +08001133 cur_state =
1134 cur_state > instance->lower ?
1135 (cur_state - 1) :
1136 instance->lower;
Zhang Ruie3f25e62012-06-26 16:26:40 +08001137
1138 cdev->ops->set_cur_state(cdev, cur_state);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001139 }
1140 break;
1141 case THERMAL_TRIP_PASSIVE:
Vladimir Zajac29321352009-05-06 19:34:21 +02001142 if (temp >= trip_temp || tz->passive)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001143 thermal_zone_device_passive(tz, temp,
1144 trip_temp, count);
1145 break;
1146 }
1147 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001148
1149 if (tz->forced_passive)
1150 thermal_zone_device_passive(tz, temp, tz->forced_passive,
1151 THERMAL_TRIPS_NONE);
1152
Matthew Garrettb1569e92008-12-03 17:55:32 +00001153 tz->last_temperature = temp;
Michael Brunner0d288162009-08-26 14:29:25 -07001154
Joe Perchescaca8b82012-03-21 12:55:02 -07001155leave:
Matthew Garrettb1569e92008-12-03 17:55:32 +00001156 if (tz->passive)
1157 thermal_zone_device_set_polling(tz, tz->passive_delay);
1158 else if (tz->polling_delay)
1159 thermal_zone_device_set_polling(tz, tz->polling_delay);
Frans Pop3767cb52009-10-26 08:39:04 +01001160 else
1161 thermal_zone_device_set_polling(tz, 0);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001162 mutex_unlock(&tz->lock);
1163}
1164EXPORT_SYMBOL(thermal_zone_device_update);
1165
1166/**
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001167 * create_trip_attrs - create attributes for trip points
1168 * @tz: the thermal zone device
1169 * @mask: Writeable trip point bitmap.
1170 */
1171static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1172{
1173 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001174 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001175
Durgadoss R27365a62012-07-25 10:10:59 +08001176 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001177 if (!tz->trip_type_attrs)
1178 return -ENOMEM;
1179
Durgadoss R27365a62012-07-25 10:10:59 +08001180 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001181 if (!tz->trip_temp_attrs) {
1182 kfree(tz->trip_type_attrs);
1183 return -ENOMEM;
1184 }
1185
Durgadoss R27365a62012-07-25 10:10:59 +08001186 if (tz->ops->get_trip_hyst) {
1187 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1188 if (!tz->trip_hyst_attrs) {
1189 kfree(tz->trip_type_attrs);
1190 kfree(tz->trip_temp_attrs);
1191 return -ENOMEM;
1192 }
1193 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001194
Durgadoss R27365a62012-07-25 10:10:59 +08001195
1196 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001197 /* create trip type attribute */
1198 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1199 "trip_point_%d_type", indx);
1200
1201 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1202 tz->trip_type_attrs[indx].attr.attr.name =
1203 tz->trip_type_attrs[indx].name;
1204 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1205 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1206
1207 device_create_file(&tz->device,
1208 &tz->trip_type_attrs[indx].attr);
1209
1210 /* create trip temp attribute */
1211 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1212 "trip_point_%d_temp", indx);
1213
1214 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1215 tz->trip_temp_attrs[indx].attr.attr.name =
1216 tz->trip_temp_attrs[indx].name;
1217 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1218 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1219 if (mask & (1 << indx)) {
1220 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1221 tz->trip_temp_attrs[indx].attr.store =
1222 trip_point_temp_store;
1223 }
1224
1225 device_create_file(&tz->device,
1226 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001227
1228 /* create Optional trip hyst attribute */
1229 if (!tz->ops->get_trip_hyst)
1230 continue;
1231 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1232 "trip_point_%d_hyst", indx);
1233
1234 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1235 tz->trip_hyst_attrs[indx].attr.attr.name =
1236 tz->trip_hyst_attrs[indx].name;
1237 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1238 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1239 if (tz->ops->set_trip_hyst) {
1240 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1241 tz->trip_hyst_attrs[indx].attr.store =
1242 trip_point_hyst_store;
1243 }
1244
1245 device_create_file(&tz->device,
1246 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001247 }
1248 return 0;
1249}
1250
1251static void remove_trip_attrs(struct thermal_zone_device *tz)
1252{
1253 int indx;
1254
1255 for (indx = 0; indx < tz->trips; indx++) {
1256 device_remove_file(&tz->device,
1257 &tz->trip_type_attrs[indx].attr);
1258 device_remove_file(&tz->device,
1259 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001260 if (tz->ops->get_trip_hyst)
1261 device_remove_file(&tz->device,
1262 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001263 }
1264 kfree(tz->trip_type_attrs);
1265 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001266 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001267}
1268
1269/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001270 * thermal_zone_device_register - register a new thermal zone device
1271 * @type: the thermal zone device type
1272 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001273 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001274 * @devdata: private device data
1275 * @ops: standard thermal zone device callbacks
Matthew Garrettb1569e92008-12-03 17:55:32 +00001276 * @tc1: thermal coefficient 1 for passive calculations
1277 * @tc2: thermal coefficient 2 for passive calculations
1278 * @passive_delay: number of milliseconds to wait between polls when
1279 * performing passive cooling
1280 * @polling_delay: number of milliseconds to wait between polls when checking
1281 * whether trip points have been crossed (0 for interrupt
1282 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001283 *
1284 * thermal_zone_device_unregister() must be called when the device is no
Matthew Garrettb1569e92008-12-03 17:55:32 +00001285 * longer needed. The passive cooling formula uses tc1 and tc2 as described in
1286 * section 11.1.5.1 of the ACPI specification 3.0.
Zhang Rui203d3d42008-01-17 15:51:08 +08001287 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001288struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001289 int trips, int mask, void *devdata,
Alan Cox5b275ce2010-11-11 15:27:29 +00001290 const struct thermal_zone_device_ops *ops,
1291 int tc1, int tc2, int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001292{
1293 struct thermal_zone_device *tz;
1294 struct thermal_cooling_device *pos;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001295 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001296 int result;
1297 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001298 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001299
1300 if (strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001301 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001302
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001303 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001304 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001305
1306 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001307 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001308
1309 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1310 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001311 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001312
1313 INIT_LIST_HEAD(&tz->cooling_devices);
1314 idr_init(&tz->idr);
1315 mutex_init(&tz->lock);
1316 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1317 if (result) {
1318 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001319 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001320 }
1321
1322 strcpy(tz->type, type);
1323 tz->ops = ops;
1324 tz->device.class = &thermal_class;
1325 tz->devdata = devdata;
1326 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001327 tz->tc1 = tc1;
1328 tz->tc2 = tc2;
1329 tz->passive_delay = passive_delay;
1330 tz->polling_delay = polling_delay;
1331
Kay Sievers354655e2009-01-06 10:44:37 -08001332 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001333 result = device_register(&tz->device);
1334 if (result) {
1335 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1336 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001337 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001338 }
1339
1340 /* sys I/F */
1341 if (type) {
1342 result = device_create_file(&tz->device, &dev_attr_type);
1343 if (result)
1344 goto unregister;
1345 }
1346
1347 result = device_create_file(&tz->device, &dev_attr_temp);
1348 if (result)
1349 goto unregister;
1350
1351 if (ops->get_mode) {
1352 result = device_create_file(&tz->device, &dev_attr_mode);
1353 if (result)
1354 goto unregister;
1355 }
1356
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001357 result = create_trip_attrs(tz, mask);
1358 if (result)
1359 goto unregister;
1360
Zhang Rui203d3d42008-01-17 15:51:08 +08001361 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001362 tz->ops->get_trip_type(tz, count, &trip_type);
1363 if (trip_type == THERMAL_TRIP_PASSIVE)
1364 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001365 }
1366
Matthew Garrett03a971a2008-12-03 18:00:38 +00001367 if (!passive)
1368 result = device_create_file(&tz->device,
1369 &dev_attr_passive);
1370
1371 if (result)
1372 goto unregister;
1373
Zhang Ruie68b16a2008-04-21 16:07:52 +08001374 result = thermal_add_hwmon_sysfs(tz);
1375 if (result)
1376 goto unregister;
1377
Zhang Rui203d3d42008-01-17 15:51:08 +08001378 mutex_lock(&thermal_list_lock);
1379 list_add_tail(&tz->node, &thermal_tz_list);
1380 if (ops->bind)
1381 list_for_each_entry(pos, &thermal_cdev_list, node) {
Len Brown543a9562008-02-07 16:55:08 -05001382 result = ops->bind(tz, pos);
1383 if (result)
1384 break;
Zhang Rui203d3d42008-01-17 15:51:08 +08001385 }
1386 mutex_unlock(&thermal_list_lock);
1387
Matthew Garrettb1569e92008-12-03 17:55:32 +00001388 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1389
1390 thermal_zone_device_update(tz);
1391
Zhang Rui203d3d42008-01-17 15:51:08 +08001392 if (!result)
1393 return tz;
1394
Joe Perchescaca8b82012-03-21 12:55:02 -07001395unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001396 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1397 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001398 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001399}
1400EXPORT_SYMBOL(thermal_zone_device_register);
1401
1402/**
1403 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001404 * @tz: the thermal zone device to remove
1405 */
1406void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1407{
1408 struct thermal_cooling_device *cdev;
1409 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001410
1411 if (!tz)
1412 return;
1413
1414 mutex_lock(&thermal_list_lock);
1415 list_for_each_entry(pos, &thermal_tz_list, node)
1416 if (pos == tz)
1417 break;
1418 if (pos != tz) {
1419 /* thermal zone device not found */
1420 mutex_unlock(&thermal_list_lock);
1421 return;
1422 }
1423 list_del(&tz->node);
1424 if (tz->ops->unbind)
1425 list_for_each_entry(cdev, &thermal_cdev_list, node)
1426 tz->ops->unbind(tz, cdev);
1427 mutex_unlock(&thermal_list_lock);
1428
Matthew Garrettb1569e92008-12-03 17:55:32 +00001429 thermal_zone_device_set_polling(tz, 0);
1430
Zhang Rui203d3d42008-01-17 15:51:08 +08001431 if (tz->type[0])
1432 device_remove_file(&tz->device, &dev_attr_type);
1433 device_remove_file(&tz->device, &dev_attr_temp);
1434 if (tz->ops->get_mode)
1435 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001436 remove_trip_attrs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001437
Zhang Ruie68b16a2008-04-21 16:07:52 +08001438 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001439 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1440 idr_destroy(&tz->idr);
1441 mutex_destroy(&tz->lock);
1442 device_unregister(&tz->device);
1443 return;
1444}
Zhang Rui203d3d42008-01-17 15:51:08 +08001445EXPORT_SYMBOL(thermal_zone_device_unregister);
1446
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001447#ifdef CONFIG_NET
1448static struct genl_family thermal_event_genl_family = {
1449 .id = GENL_ID_GENERATE,
1450 .name = THERMAL_GENL_FAMILY_NAME,
1451 .version = THERMAL_GENL_VERSION,
1452 .maxattr = THERMAL_GENL_ATTR_MAX,
1453};
1454
1455static struct genl_multicast_group thermal_event_mcgrp = {
1456 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1457};
1458
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001459int thermal_generate_netlink_event(u32 orig, enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301460{
1461 struct sk_buff *skb;
1462 struct nlattr *attr;
1463 struct thermal_genl_event *thermal_event;
1464 void *msg_header;
1465 int size;
1466 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001467 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301468
1469 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001470 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1471 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301472
1473 skb = genlmsg_new(size, GFP_ATOMIC);
1474 if (!skb)
1475 return -ENOMEM;
1476
1477 /* add the genetlink message header */
1478 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1479 &thermal_event_genl_family, 0,
1480 THERMAL_GENL_CMD_EVENT);
1481 if (!msg_header) {
1482 nlmsg_free(skb);
1483 return -ENOMEM;
1484 }
1485
1486 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001487 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1488 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301489
1490 if (!attr) {
1491 nlmsg_free(skb);
1492 return -EINVAL;
1493 }
1494
1495 thermal_event = nla_data(attr);
1496 if (!thermal_event) {
1497 nlmsg_free(skb);
1498 return -EINVAL;
1499 }
1500
1501 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1502
1503 thermal_event->orig = orig;
1504 thermal_event->event = event;
1505
1506 /* send multicast genetlink message */
1507 result = genlmsg_end(skb, msg_header);
1508 if (result < 0) {
1509 nlmsg_free(skb);
1510 return result;
1511 }
1512
1513 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1514 if (result)
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001515 pr_info("failed to send netlink event:%d\n", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301516
1517 return result;
1518}
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001519EXPORT_SYMBOL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301520
1521static int genetlink_init(void)
1522{
1523 int result;
1524
1525 result = genl_register_family(&thermal_event_genl_family);
1526 if (result)
1527 return result;
1528
1529 result = genl_register_mc_group(&thermal_event_genl_family,
1530 &thermal_event_mcgrp);
1531 if (result)
1532 genl_unregister_family(&thermal_event_genl_family);
1533 return result;
1534}
1535
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001536static void genetlink_exit(void)
1537{
1538 genl_unregister_family(&thermal_event_genl_family);
1539}
1540#else /* !CONFIG_NET */
1541static inline int genetlink_init(void) { return 0; }
1542static inline void genetlink_exit(void) {}
1543#endif /* !CONFIG_NET */
1544
Zhang Rui203d3d42008-01-17 15:51:08 +08001545static int __init thermal_init(void)
1546{
1547 int result = 0;
1548
1549 result = class_register(&thermal_class);
1550 if (result) {
1551 idr_destroy(&thermal_tz_idr);
1552 idr_destroy(&thermal_cdev_idr);
1553 mutex_destroy(&thermal_idr_lock);
1554 mutex_destroy(&thermal_list_lock);
1555 }
R.Durgadoss4cb18722010-10-27 03:33:29 +05301556 result = genetlink_init();
Zhang Rui203d3d42008-01-17 15:51:08 +08001557 return result;
1558}
1559
1560static void __exit thermal_exit(void)
1561{
1562 class_unregister(&thermal_class);
1563 idr_destroy(&thermal_tz_idr);
1564 idr_destroy(&thermal_cdev_idr);
1565 mutex_destroy(&thermal_idr_lock);
1566 mutex_destroy(&thermal_list_lock);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301567 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001568}
1569
R.Durgadoss4cb18722010-10-27 03:33:29 +05301570fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001571module_exit(thermal_exit);