blob: 0cf3dce554628e73bcda5b67137720b7f704ae5b [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
Matthew Garrett03a971a2008-12-03 18:00:38 +0000338 tz->forced_passive = state;
339
340 thermal_zone_device_update(tz);
341
342 return count;
343}
344
345static ssize_t
346passive_show(struct device *dev, struct device_attribute *attr,
347 char *buf)
348{
349 struct thermal_zone_device *tz = to_thermal_zone(dev);
350
351 return sprintf(buf, "%d\n", tz->forced_passive);
352}
353
Zhang Rui203d3d42008-01-17 15:51:08 +0800354static DEVICE_ATTR(type, 0444, type_show, NULL);
355static DEVICE_ATTR(temp, 0444, temp_show, NULL);
356static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700357static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800358
Zhang Rui203d3d42008-01-17 15:51:08 +0800359/* sys I/F for cooling device */
360#define to_cooling_device(_dev) \
361 container_of(_dev, struct thermal_cooling_device, device)
362
363static ssize_t
364thermal_cooling_device_type_show(struct device *dev,
365 struct device_attribute *attr, char *buf)
366{
367 struct thermal_cooling_device *cdev = to_cooling_device(dev);
368
369 return sprintf(buf, "%s\n", cdev->type);
370}
371
372static ssize_t
373thermal_cooling_device_max_state_show(struct device *dev,
374 struct device_attribute *attr, char *buf)
375{
376 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000377 unsigned long state;
378 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800379
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000380 ret = cdev->ops->get_max_state(cdev, &state);
381 if (ret)
382 return ret;
383 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800384}
385
386static ssize_t
387thermal_cooling_device_cur_state_show(struct device *dev,
388 struct device_attribute *attr, char *buf)
389{
390 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000391 unsigned long state;
392 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800393
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000394 ret = cdev->ops->get_cur_state(cdev, &state);
395 if (ret)
396 return ret;
397 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800398}
399
400static ssize_t
401thermal_cooling_device_cur_state_store(struct device *dev,
402 struct device_attribute *attr,
403 const char *buf, size_t count)
404{
405 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000406 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800407 int result;
408
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000409 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800410 return -EINVAL;
411
Roel Kluinedb94912009-12-15 22:46:50 +0100412 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800413 return -EINVAL;
414
415 result = cdev->ops->set_cur_state(cdev, state);
416 if (result)
417 return result;
418 return count;
419}
420
421static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500422__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800423static DEVICE_ATTR(max_state, 0444,
424 thermal_cooling_device_max_state_show, NULL);
425static DEVICE_ATTR(cur_state, 0644,
426 thermal_cooling_device_cur_state_show,
427 thermal_cooling_device_cur_state_store);
428
429static ssize_t
430thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500431 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800432{
433 struct thermal_cooling_device_instance *instance;
434
435 instance =
436 container_of(attr, struct thermal_cooling_device_instance, attr);
437
438 if (instance->trip == THERMAL_TRIPS_NONE)
439 return sprintf(buf, "-1\n");
440 else
441 return sprintf(buf, "%d\n", instance->trip);
442}
443
444/* Device management */
445
Rene Herman16d75232008-06-24 19:38:56 +0200446#if defined(CONFIG_THERMAL_HWMON)
447
Zhang Ruie68b16a2008-04-21 16:07:52 +0800448/* hwmon sys I/F */
449#include <linux/hwmon.h>
Jean Delvare31f53962011-07-28 13:48:42 -0700450
451/* thermal zone devices with the same type share one hwmon device */
452struct thermal_hwmon_device {
453 char type[THERMAL_NAME_LENGTH];
454 struct device *device;
455 int count;
456 struct list_head tz_list;
457 struct list_head node;
458};
459
460struct thermal_hwmon_attr {
461 struct device_attribute attr;
462 char name[16];
463};
464
465/* one temperature input for each thermal zone */
466struct thermal_hwmon_temp {
467 struct list_head hwmon_node;
468 struct thermal_zone_device *tz;
469 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
470 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
471};
472
Zhang Ruie68b16a2008-04-21 16:07:52 +0800473static LIST_HEAD(thermal_hwmon_list);
474
475static ssize_t
476name_show(struct device *dev, struct device_attribute *attr, char *buf)
477{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700478 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800479 return sprintf(buf, "%s\n", hwmon->type);
480}
481static DEVICE_ATTR(name, 0444, name_show, NULL);
482
483static ssize_t
484temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
485{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000486 long temperature;
487 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800488 struct thermal_hwmon_attr *hwmon_attr
489 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700490 struct thermal_hwmon_temp *temp
491 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800492 temp_input);
Jean Delvare31f53962011-07-28 13:48:42 -0700493 struct thermal_zone_device *tz = temp->tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800494
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000495 ret = tz->ops->get_temp(tz, &temperature);
496
497 if (ret)
498 return ret;
499
500 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800501}
502
503static ssize_t
504temp_crit_show(struct device *dev, struct device_attribute *attr,
505 char *buf)
506{
507 struct thermal_hwmon_attr *hwmon_attr
508 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700509 struct thermal_hwmon_temp *temp
510 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800511 temp_crit);
Jean Delvare31f53962011-07-28 13:48:42 -0700512 struct thermal_zone_device *tz = temp->tz;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000513 long temperature;
514 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800515
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000516 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
517 if (ret)
518 return ret;
519
520 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800521}
522
523
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700524static struct thermal_hwmon_device *
525thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
526{
527 struct thermal_hwmon_device *hwmon;
528
529 mutex_lock(&thermal_list_lock);
530 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
531 if (!strcmp(hwmon->type, tz->type)) {
532 mutex_unlock(&thermal_list_lock);
533 return hwmon;
534 }
535 mutex_unlock(&thermal_list_lock);
536
537 return NULL;
538}
539
Jean Delvare31f53962011-07-28 13:48:42 -0700540/* Find the temperature input matching a given thermal zone */
541static struct thermal_hwmon_temp *
542thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
543 const struct thermal_zone_device *tz)
544{
545 struct thermal_hwmon_temp *temp;
546
547 mutex_lock(&thermal_list_lock);
548 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
549 if (temp->tz == tz) {
550 mutex_unlock(&thermal_list_lock);
551 return temp;
552 }
553 mutex_unlock(&thermal_list_lock);
554
555 return NULL;
556}
557
Zhang Ruie68b16a2008-04-21 16:07:52 +0800558static int
559thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
560{
561 struct thermal_hwmon_device *hwmon;
Jean Delvare31f53962011-07-28 13:48:42 -0700562 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800563 int new_hwmon_device = 1;
564 int result;
565
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700566 hwmon = thermal_hwmon_lookup_by_type(tz);
567 if (hwmon) {
568 new_hwmon_device = 0;
569 goto register_sys_interface;
570 }
Zhang Ruie68b16a2008-04-21 16:07:52 +0800571
572 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
573 if (!hwmon)
574 return -ENOMEM;
575
576 INIT_LIST_HEAD(&hwmon->tz_list);
577 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
578 hwmon->device = hwmon_device_register(NULL);
579 if (IS_ERR(hwmon->device)) {
580 result = PTR_ERR(hwmon->device);
581 goto free_mem;
582 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700583 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800584 result = device_create_file(hwmon->device, &dev_attr_name);
585 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530586 goto free_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800587
588 register_sys_interface:
Jean Delvare31f53962011-07-28 13:48:42 -0700589 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
590 if (!temp) {
591 result = -ENOMEM;
592 goto unregister_name;
593 }
594
595 temp->tz = tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800596 hwmon->count++;
597
Jean Delvare31f53962011-07-28 13:48:42 -0700598 snprintf(temp->temp_input.name, THERMAL_NAME_LENGTH,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800599 "temp%d_input", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700600 temp->temp_input.attr.attr.name = temp->temp_input.name;
601 temp->temp_input.attr.attr.mode = 0444;
602 temp->temp_input.attr.show = temp_input_show;
603 sysfs_attr_init(&temp->temp_input.attr.attr);
604 result = device_create_file(hwmon->device, &temp->temp_input.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800605 if (result)
Jean Delvare31f53962011-07-28 13:48:42 -0700606 goto free_temp_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800607
608 if (tz->ops->get_crit_temp) {
609 unsigned long temperature;
610 if (!tz->ops->get_crit_temp(tz, &temperature)) {
Jean Delvare31f53962011-07-28 13:48:42 -0700611 snprintf(temp->temp_crit.name, THERMAL_NAME_LENGTH,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800612 "temp%d_crit", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700613 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
614 temp->temp_crit.attr.attr.mode = 0444;
615 temp->temp_crit.attr.show = temp_crit_show;
616 sysfs_attr_init(&temp->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800617 result = device_create_file(hwmon->device,
Jean Delvare31f53962011-07-28 13:48:42 -0700618 &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800619 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530620 goto unregister_input;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800621 }
622 }
623
624 mutex_lock(&thermal_list_lock);
625 if (new_hwmon_device)
626 list_add_tail(&hwmon->node, &thermal_hwmon_list);
Jean Delvare31f53962011-07-28 13:48:42 -0700627 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800628 mutex_unlock(&thermal_list_lock);
629
630 return 0;
631
Durgadoss Rb299eb52011-03-03 04:30:13 +0530632 unregister_input:
Jean Delvare31f53962011-07-28 13:48:42 -0700633 device_remove_file(hwmon->device, &temp->temp_input.attr);
634 free_temp_mem:
635 kfree(temp);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530636 unregister_name:
Zhang Ruie68b16a2008-04-21 16:07:52 +0800637 if (new_hwmon_device) {
638 device_remove_file(hwmon->device, &dev_attr_name);
639 hwmon_device_unregister(hwmon->device);
640 }
641 free_mem:
642 if (new_hwmon_device)
643 kfree(hwmon);
644
645 return result;
646}
647
648static void
649thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
650{
Jean Delvare31f53962011-07-28 13:48:42 -0700651 struct thermal_hwmon_device *hwmon;
652 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800653
Jean Delvare31f53962011-07-28 13:48:42 -0700654 hwmon = thermal_hwmon_lookup_by_type(tz);
655 if (unlikely(!hwmon)) {
656 /* Should never happen... */
657 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
658 return;
659 }
660
661 temp = thermal_hwmon_lookup_temp(hwmon, tz);
662 if (unlikely(!temp)) {
663 /* Should never happen... */
664 dev_dbg(&tz->device, "temperature input lookup failed!\n");
665 return;
666 }
667
668 device_remove_file(hwmon->device, &temp->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530669 if (tz->ops->get_crit_temp)
Jean Delvare31f53962011-07-28 13:48:42 -0700670 device_remove_file(hwmon->device, &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800671
672 mutex_lock(&thermal_list_lock);
Jean Delvare31f53962011-07-28 13:48:42 -0700673 list_del(&temp->hwmon_node);
674 kfree(temp);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800675 if (!list_empty(&hwmon->tz_list)) {
676 mutex_unlock(&thermal_list_lock);
677 return;
678 }
679 list_del(&hwmon->node);
680 mutex_unlock(&thermal_list_lock);
681
682 device_remove_file(hwmon->device, &dev_attr_name);
683 hwmon_device_unregister(hwmon->device);
684 kfree(hwmon);
685}
686#else
687static int
688thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
689{
690 return 0;
691}
692
693static void
694thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
695{
696}
697#endif
698
Matthew Garrettb1569e92008-12-03 17:55:32 +0000699static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
700 int delay)
701{
702 cancel_delayed_work(&(tz->poll_queue));
703
704 if (!delay)
705 return;
706
707 if (delay > 1000)
Rafael J. Wysocki51e20d02011-11-06 14:21:38 +0100708 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
Matthew Garrettb1569e92008-12-03 17:55:32 +0000709 round_jiffies(msecs_to_jiffies(delay)));
710 else
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 msecs_to_jiffies(delay));
713}
714
715static void thermal_zone_device_passive(struct thermal_zone_device *tz,
716 int temp, int trip_temp, int trip)
717{
Zhang Rui1b7ddb82012-06-27 09:51:12 +0800718 enum thermal_trend trend;
Matthew Garrettb1569e92008-12-03 17:55:32 +0000719 struct thermal_cooling_device_instance *instance;
720 struct thermal_cooling_device *cdev;
721 long state, max_state;
722
Zhang Rui1b7ddb82012-06-27 09:51:12 +0800723 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
Zhang Rui601f3d42012-06-27 09:54:33 +0800724 /*
725 * compare the current temperature and previous temperature
726 * to get the thermal trend, if no special requirement
727 */
728 if (tz->temperature > tz->last_temperature)
729 trend = THERMAL_TREND_RAISING;
730 else if (tz->temperature < tz->last_temperature)
731 trend = THERMAL_TREND_DROPPING;
732 else
733 trend = THERMAL_TREND_STABLE;
734 }
735
Matthew Garrettb1569e92008-12-03 17:55:32 +0000736 /*
737 * Above Trip?
738 * -----------
739 * Calculate the thermal trend (using the passive cooling equation)
740 * and modify the performance limit for all passive cooling devices
741 * accordingly. Note that we assume symmetry.
742 */
743 if (temp >= trip_temp) {
744 tz->passive = true;
745
Matthew Garrettb1569e92008-12-03 17:55:32 +0000746 /* Heating up? */
Zhang Rui1b7ddb82012-06-27 09:51:12 +0800747 if (trend == THERMAL_TREND_RAISING) {
Matthew Garrettb1569e92008-12-03 17:55:32 +0000748 list_for_each_entry(instance, &tz->cooling_devices,
749 node) {
750 if (instance->trip != trip)
751 continue;
752 cdev = instance->cdev;
753 cdev->ops->get_cur_state(cdev, &state);
754 cdev->ops->get_max_state(cdev, &max_state);
755 if (state++ < max_state)
756 cdev->ops->set_cur_state(cdev, state);
757 }
Zhang Rui1b7ddb82012-06-27 09:51:12 +0800758 } else if (trend == THERMAL_TREND_DROPPING) { /* Cooling off? */
Matthew Garrettb1569e92008-12-03 17:55:32 +0000759 list_for_each_entry(instance, &tz->cooling_devices,
760 node) {
761 if (instance->trip != trip)
762 continue;
763 cdev = instance->cdev;
764 cdev->ops->get_cur_state(cdev, &state);
765 cdev->ops->get_max_state(cdev, &max_state);
766 if (state > 0)
767 cdev->ops->set_cur_state(cdev, --state);
768 }
769 }
770 return;
771 }
772
773 /*
774 * Below Trip?
775 * -----------
776 * Implement passive cooling hysteresis to slowly increase performance
777 * and avoid thrashing around the passive trip point. Note that we
778 * assume symmetry.
779 */
780 list_for_each_entry(instance, &tz->cooling_devices, node) {
781 if (instance->trip != trip)
782 continue;
783 cdev = instance->cdev;
784 cdev->ops->get_cur_state(cdev, &state);
785 cdev->ops->get_max_state(cdev, &max_state);
786 if (state > 0)
787 cdev->ops->set_cur_state(cdev, --state);
788 if (state == 0)
789 tz->passive = false;
790 }
791}
792
793static void thermal_zone_device_check(struct work_struct *work)
794{
795 struct thermal_zone_device *tz = container_of(work, struct
796 thermal_zone_device,
797 poll_queue.work);
798 thermal_zone_device_update(tz);
799}
Zhang Ruie68b16a2008-04-21 16:07:52 +0800800
Zhang Rui203d3d42008-01-17 15:51:08 +0800801/**
802 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800803 * @tz: thermal zone device
804 * @trip: indicates which trip point the cooling devices is
805 * associated with in this thermal zone.
806 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500807 *
808 * This function is usually called in the thermal zone device .bind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800809 */
810int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
811 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800812 struct thermal_cooling_device *cdev,
813 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +0800814{
815 struct thermal_cooling_device_instance *dev;
816 struct thermal_cooling_device_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500817 struct thermal_zone_device *pos1;
818 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800819 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800820 int result;
821
Len Brown543a9562008-02-07 16:55:08 -0500822 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800823 return -EINVAL;
824
Thomas Sujithc7516702008-02-15 00:58:50 -0500825 list_for_each_entry(pos1, &thermal_tz_list, node) {
826 if (pos1 == tz)
827 break;
828 }
829 list_for_each_entry(pos2, &thermal_cdev_list, node) {
830 if (pos2 == cdev)
831 break;
832 }
833
834 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800835 return -EINVAL;
836
Zhang Rui9d998422012-06-26 16:35:57 +0800837 cdev->ops->get_max_state(cdev, &max_state);
838
839 /* lower default 0, upper default max_state */
840 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
841 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
842
843 if (lower > upper || upper > max_state)
844 return -EINVAL;
845
Zhang Rui203d3d42008-01-17 15:51:08 +0800846 dev =
847 kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL);
848 if (!dev)
849 return -ENOMEM;
850 dev->tz = tz;
851 dev->cdev = cdev;
852 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800853 dev->upper = upper;
854 dev->lower = lower;
Zhang Rui74051ba2012-06-26 16:27:22 +0800855
Zhang Rui203d3d42008-01-17 15:51:08 +0800856 result = get_idr(&tz->idr, &tz->lock, &dev->id);
857 if (result)
858 goto free_mem;
859
860 sprintf(dev->name, "cdev%d", dev->id);
861 result =
862 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
863 if (result)
864 goto release_idr;
865
866 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700867 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800868 dev->attr.attr.name = dev->attr_name;
869 dev->attr.attr.mode = 0444;
870 dev->attr.show = thermal_cooling_device_trip_point_show;
871 result = device_create_file(&tz->device, &dev->attr);
872 if (result)
873 goto remove_symbol_link;
874
875 mutex_lock(&tz->lock);
876 list_for_each_entry(pos, &tz->cooling_devices, node)
877 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
878 result = -EEXIST;
879 break;
880 }
881 if (!result)
882 list_add_tail(&dev->node, &tz->cooling_devices);
883 mutex_unlock(&tz->lock);
884
885 if (!result)
886 return 0;
887
888 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -0700889remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800890 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -0700891release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +0800892 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -0700893free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800894 kfree(dev);
895 return result;
896}
897EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
898
899/**
900 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800901 * @tz: thermal zone device
902 * @trip: indicates which trip point the cooling devices is
903 * associated with in this thermal zone.
904 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500905 *
906 * This function is usually called in the thermal zone device .unbind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800907 */
908int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
909 int trip,
910 struct thermal_cooling_device *cdev)
911{
912 struct thermal_cooling_device_instance *pos, *next;
913
914 mutex_lock(&tz->lock);
915 list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
Len Brown543a9562008-02-07 16:55:08 -0500916 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800917 list_del(&pos->node);
918 mutex_unlock(&tz->lock);
919 goto unbind;
920 }
921 }
922 mutex_unlock(&tz->lock);
923
924 return -ENODEV;
925
Joe Perchescaca8b82012-03-21 12:55:02 -0700926unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +0800927 device_remove_file(&tz->device, &pos->attr);
928 sysfs_remove_link(&tz->device.kobj, pos->name);
929 release_idr(&tz->idr, &tz->lock, pos->id);
930 kfree(pos);
931 return 0;
932}
933EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
934
935static void thermal_release(struct device *dev)
936{
937 struct thermal_zone_device *tz;
938 struct thermal_cooling_device *cdev;
939
Joe Perchescaca8b82012-03-21 12:55:02 -0700940 if (!strncmp(dev_name(dev), "thermal_zone",
941 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800942 tz = to_thermal_zone(dev);
943 kfree(tz);
944 } else {
945 cdev = to_cooling_device(dev);
946 kfree(cdev);
947 }
948}
949
950static struct class thermal_class = {
951 .name = "thermal",
952 .dev_release = thermal_release,
953};
954
955/**
956 * thermal_cooling_device_register - register a new thermal cooling device
957 * @type: the thermal cooling device type.
958 * @devdata: device private data.
959 * @ops: standard thermal cooling devices callbacks.
960 */
Joe Perchescaca8b82012-03-21 12:55:02 -0700961struct thermal_cooling_device *
962thermal_cooling_device_register(char *type, void *devdata,
963 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800964{
965 struct thermal_cooling_device *cdev;
966 struct thermal_zone_device *pos;
967 int result;
968
969 if (strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500970 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800971
972 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -0500973 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500974 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800975
976 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
977 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500978 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +0800979
980 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
981 if (result) {
982 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500983 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800984 }
985
986 strcpy(cdev->type, type);
987 cdev->ops = ops;
988 cdev->device.class = &thermal_class;
989 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -0800990 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800991 result = device_register(&cdev->device);
992 if (result) {
993 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
994 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500995 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800996 }
997
998 /* sys I/F */
999 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001000 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001001 if (result)
1002 goto unregister;
1003 }
1004
1005 result = device_create_file(&cdev->device, &dev_attr_max_state);
1006 if (result)
1007 goto unregister;
1008
1009 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1010 if (result)
1011 goto unregister;
1012
1013 mutex_lock(&thermal_list_lock);
1014 list_add(&cdev->node, &thermal_cdev_list);
1015 list_for_each_entry(pos, &thermal_tz_list, node) {
1016 if (!pos->ops->bind)
1017 continue;
1018 result = pos->ops->bind(pos, cdev);
1019 if (result)
1020 break;
1021
1022 }
1023 mutex_unlock(&thermal_list_lock);
1024
1025 if (!result)
1026 return cdev;
1027
Joe Perchescaca8b82012-03-21 12:55:02 -07001028unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001029 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1030 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001031 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001032}
1033EXPORT_SYMBOL(thermal_cooling_device_register);
1034
1035/**
1036 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001037 * @cdev: the thermal cooling device to remove.
1038 *
1039 * thermal_cooling_device_unregister() must be called when the device is no
1040 * longer needed.
1041 */
1042void thermal_cooling_device_unregister(struct
1043 thermal_cooling_device
1044 *cdev)
1045{
1046 struct thermal_zone_device *tz;
1047 struct thermal_cooling_device *pos = NULL;
1048
1049 if (!cdev)
1050 return;
1051
1052 mutex_lock(&thermal_list_lock);
1053 list_for_each_entry(pos, &thermal_cdev_list, node)
1054 if (pos == cdev)
1055 break;
1056 if (pos != cdev) {
1057 /* thermal cooling device not found */
1058 mutex_unlock(&thermal_list_lock);
1059 return;
1060 }
1061 list_del(&cdev->node);
1062 list_for_each_entry(tz, &thermal_tz_list, node) {
1063 if (!tz->ops->unbind)
1064 continue;
1065 tz->ops->unbind(tz, cdev);
1066 }
1067 mutex_unlock(&thermal_list_lock);
1068 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001069 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001070 device_remove_file(&cdev->device, &dev_attr_max_state);
1071 device_remove_file(&cdev->device, &dev_attr_cur_state);
1072
1073 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1074 device_unregister(&cdev->device);
1075 return;
1076}
1077EXPORT_SYMBOL(thermal_cooling_device_unregister);
1078
1079/**
Matthew Garrettb1569e92008-12-03 17:55:32 +00001080 * thermal_zone_device_update - force an update of a thermal zone's state
1081 * @ttz: the thermal zone to update
1082 */
1083
1084void thermal_zone_device_update(struct thermal_zone_device *tz)
1085{
1086 int count, ret = 0;
1087 long temp, trip_temp;
1088 enum thermal_trip_type trip_type;
1089 struct thermal_cooling_device_instance *instance;
1090 struct thermal_cooling_device *cdev;
Zhang Ruie3f25e62012-06-26 16:26:40 +08001091 unsigned long cur_state, max_state;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001092
1093 mutex_lock(&tz->lock);
1094
Michael Brunner0d288162009-08-26 14:29:25 -07001095 if (tz->ops->get_temp(tz, &temp)) {
1096 /* get_temp failed - retry it later */
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001097 pr_warn("failed to read out thermal zone %d\n", tz->id);
Michael Brunner0d288162009-08-26 14:29:25 -07001098 goto leave;
1099 }
Matthew Garrettb1569e92008-12-03 17:55:32 +00001100
Zhang Rui601f3d42012-06-27 09:54:33 +08001101 tz->last_temperature = tz->temperature;
1102 tz->temperature = temp;
1103
Matthew Garrettb1569e92008-12-03 17:55:32 +00001104 for (count = 0; count < tz->trips; count++) {
1105 tz->ops->get_trip_type(tz, count, &trip_type);
1106 tz->ops->get_trip_temp(tz, count, &trip_temp);
1107
1108 switch (trip_type) {
1109 case THERMAL_TRIP_CRITICAL:
Vladimir Zajac29321352009-05-06 19:34:21 +02001110 if (temp >= trip_temp) {
Matthew Garrettb1569e92008-12-03 17:55:32 +00001111 if (tz->ops->notify)
1112 ret = tz->ops->notify(tz, count,
1113 trip_type);
1114 if (!ret) {
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001115 pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1116 temp/1000);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001117 orderly_poweroff(true);
1118 }
1119 }
1120 break;
1121 case THERMAL_TRIP_HOT:
Vladimir Zajac29321352009-05-06 19:34:21 +02001122 if (temp >= trip_temp)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001123 if (tz->ops->notify)
1124 tz->ops->notify(tz, count, trip_type);
1125 break;
1126 case THERMAL_TRIP_ACTIVE:
1127 list_for_each_entry(instance, &tz->cooling_devices,
1128 node) {
1129 if (instance->trip != count)
1130 continue;
1131
1132 cdev = instance->cdev;
1133
Zhang Ruie3f25e62012-06-26 16:26:40 +08001134 cdev->ops->get_cur_state(cdev, &cur_state);
1135 cdev->ops->get_max_state(cdev, &max_state);
1136
Vladimir Zajac29321352009-05-06 19:34:21 +02001137 if (temp >= trip_temp)
Zhang Rui74051ba2012-06-26 16:27:22 +08001138 cur_state =
1139 cur_state < instance->upper ?
1140 (cur_state + 1) :
1141 instance->upper;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001142 else
Zhang Rui74051ba2012-06-26 16:27:22 +08001143 cur_state =
1144 cur_state > instance->lower ?
1145 (cur_state - 1) :
1146 instance->lower;
Zhang Ruie3f25e62012-06-26 16:26:40 +08001147
1148 cdev->ops->set_cur_state(cdev, cur_state);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001149 }
1150 break;
1151 case THERMAL_TRIP_PASSIVE:
Vladimir Zajac29321352009-05-06 19:34:21 +02001152 if (temp >= trip_temp || tz->passive)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001153 thermal_zone_device_passive(tz, temp,
1154 trip_temp, count);
1155 break;
1156 }
1157 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001158
1159 if (tz->forced_passive)
1160 thermal_zone_device_passive(tz, temp, tz->forced_passive,
1161 THERMAL_TRIPS_NONE);
1162
Joe Perchescaca8b82012-03-21 12:55:02 -07001163leave:
Matthew Garrettb1569e92008-12-03 17:55:32 +00001164 if (tz->passive)
1165 thermal_zone_device_set_polling(tz, tz->passive_delay);
1166 else if (tz->polling_delay)
1167 thermal_zone_device_set_polling(tz, tz->polling_delay);
Frans Pop3767cb52009-10-26 08:39:04 +01001168 else
1169 thermal_zone_device_set_polling(tz, 0);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001170 mutex_unlock(&tz->lock);
1171}
1172EXPORT_SYMBOL(thermal_zone_device_update);
1173
1174/**
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001175 * create_trip_attrs - create attributes for trip points
1176 * @tz: the thermal zone device
1177 * @mask: Writeable trip point bitmap.
1178 */
1179static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1180{
1181 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001182 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001183
Durgadoss R27365a62012-07-25 10:10:59 +08001184 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001185 if (!tz->trip_type_attrs)
1186 return -ENOMEM;
1187
Durgadoss R27365a62012-07-25 10:10:59 +08001188 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001189 if (!tz->trip_temp_attrs) {
1190 kfree(tz->trip_type_attrs);
1191 return -ENOMEM;
1192 }
1193
Durgadoss R27365a62012-07-25 10:10:59 +08001194 if (tz->ops->get_trip_hyst) {
1195 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1196 if (!tz->trip_hyst_attrs) {
1197 kfree(tz->trip_type_attrs);
1198 kfree(tz->trip_temp_attrs);
1199 return -ENOMEM;
1200 }
1201 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001202
Durgadoss R27365a62012-07-25 10:10:59 +08001203
1204 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001205 /* create trip type attribute */
1206 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1207 "trip_point_%d_type", indx);
1208
1209 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1210 tz->trip_type_attrs[indx].attr.attr.name =
1211 tz->trip_type_attrs[indx].name;
1212 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1213 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1214
1215 device_create_file(&tz->device,
1216 &tz->trip_type_attrs[indx].attr);
1217
1218 /* create trip temp attribute */
1219 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1220 "trip_point_%d_temp", indx);
1221
1222 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1223 tz->trip_temp_attrs[indx].attr.attr.name =
1224 tz->trip_temp_attrs[indx].name;
1225 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1226 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1227 if (mask & (1 << indx)) {
1228 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1229 tz->trip_temp_attrs[indx].attr.store =
1230 trip_point_temp_store;
1231 }
1232
1233 device_create_file(&tz->device,
1234 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001235
1236 /* create Optional trip hyst attribute */
1237 if (!tz->ops->get_trip_hyst)
1238 continue;
1239 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1240 "trip_point_%d_hyst", indx);
1241
1242 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1243 tz->trip_hyst_attrs[indx].attr.attr.name =
1244 tz->trip_hyst_attrs[indx].name;
1245 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1246 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1247 if (tz->ops->set_trip_hyst) {
1248 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1249 tz->trip_hyst_attrs[indx].attr.store =
1250 trip_point_hyst_store;
1251 }
1252
1253 device_create_file(&tz->device,
1254 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001255 }
1256 return 0;
1257}
1258
1259static void remove_trip_attrs(struct thermal_zone_device *tz)
1260{
1261 int indx;
1262
1263 for (indx = 0; indx < tz->trips; indx++) {
1264 device_remove_file(&tz->device,
1265 &tz->trip_type_attrs[indx].attr);
1266 device_remove_file(&tz->device,
1267 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001268 if (tz->ops->get_trip_hyst)
1269 device_remove_file(&tz->device,
1270 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001271 }
1272 kfree(tz->trip_type_attrs);
1273 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001274 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001275}
1276
1277/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001278 * thermal_zone_device_register - register a new thermal zone device
1279 * @type: the thermal zone device type
1280 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001281 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001282 * @devdata: private device data
1283 * @ops: standard thermal zone device callbacks
Matthew Garrettb1569e92008-12-03 17:55:32 +00001284 * @passive_delay: number of milliseconds to wait between polls when
1285 * performing passive cooling
1286 * @polling_delay: number of milliseconds to wait between polls when checking
1287 * whether trip points have been crossed (0 for interrupt
1288 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001289 *
1290 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001291 * longer needed. The passive cooling depends on the .get_trend() return value.
Zhang Rui203d3d42008-01-17 15:51:08 +08001292 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001293struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001294 int trips, int mask, void *devdata,
Alan Cox5b275ce2010-11-11 15:27:29 +00001295 const struct thermal_zone_device_ops *ops,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001296 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001297{
1298 struct thermal_zone_device *tz;
1299 struct thermal_cooling_device *pos;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001300 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001301 int result;
1302 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001303 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001304
1305 if (strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001306 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001307
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001308 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001309 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001310
1311 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001312 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001313
1314 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1315 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001316 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001317
1318 INIT_LIST_HEAD(&tz->cooling_devices);
1319 idr_init(&tz->idr);
1320 mutex_init(&tz->lock);
1321 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1322 if (result) {
1323 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001324 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001325 }
1326
1327 strcpy(tz->type, type);
1328 tz->ops = ops;
1329 tz->device.class = &thermal_class;
1330 tz->devdata = devdata;
1331 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001332 tz->passive_delay = passive_delay;
1333 tz->polling_delay = polling_delay;
1334
Kay Sievers354655e2009-01-06 10:44:37 -08001335 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001336 result = device_register(&tz->device);
1337 if (result) {
1338 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1339 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001340 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001341 }
1342
1343 /* sys I/F */
1344 if (type) {
1345 result = device_create_file(&tz->device, &dev_attr_type);
1346 if (result)
1347 goto unregister;
1348 }
1349
1350 result = device_create_file(&tz->device, &dev_attr_temp);
1351 if (result)
1352 goto unregister;
1353
1354 if (ops->get_mode) {
1355 result = device_create_file(&tz->device, &dev_attr_mode);
1356 if (result)
1357 goto unregister;
1358 }
1359
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001360 result = create_trip_attrs(tz, mask);
1361 if (result)
1362 goto unregister;
1363
Zhang Rui203d3d42008-01-17 15:51:08 +08001364 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001365 tz->ops->get_trip_type(tz, count, &trip_type);
1366 if (trip_type == THERMAL_TRIP_PASSIVE)
1367 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001368 }
1369
Matthew Garrett03a971a2008-12-03 18:00:38 +00001370 if (!passive)
1371 result = device_create_file(&tz->device,
1372 &dev_attr_passive);
1373
1374 if (result)
1375 goto unregister;
1376
Zhang Ruie68b16a2008-04-21 16:07:52 +08001377 result = thermal_add_hwmon_sysfs(tz);
1378 if (result)
1379 goto unregister;
1380
Zhang Rui203d3d42008-01-17 15:51:08 +08001381 mutex_lock(&thermal_list_lock);
1382 list_add_tail(&tz->node, &thermal_tz_list);
1383 if (ops->bind)
1384 list_for_each_entry(pos, &thermal_cdev_list, node) {
Len Brown543a9562008-02-07 16:55:08 -05001385 result = ops->bind(tz, pos);
1386 if (result)
1387 break;
Zhang Rui203d3d42008-01-17 15:51:08 +08001388 }
1389 mutex_unlock(&thermal_list_lock);
1390
Matthew Garrettb1569e92008-12-03 17:55:32 +00001391 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1392
1393 thermal_zone_device_update(tz);
1394
Zhang Rui203d3d42008-01-17 15:51:08 +08001395 if (!result)
1396 return tz;
1397
Joe Perchescaca8b82012-03-21 12:55:02 -07001398unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001399 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1400 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001401 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001402}
1403EXPORT_SYMBOL(thermal_zone_device_register);
1404
1405/**
1406 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001407 * @tz: the thermal zone device to remove
1408 */
1409void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1410{
1411 struct thermal_cooling_device *cdev;
1412 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001413
1414 if (!tz)
1415 return;
1416
1417 mutex_lock(&thermal_list_lock);
1418 list_for_each_entry(pos, &thermal_tz_list, node)
1419 if (pos == tz)
1420 break;
1421 if (pos != tz) {
1422 /* thermal zone device not found */
1423 mutex_unlock(&thermal_list_lock);
1424 return;
1425 }
1426 list_del(&tz->node);
1427 if (tz->ops->unbind)
1428 list_for_each_entry(cdev, &thermal_cdev_list, node)
1429 tz->ops->unbind(tz, cdev);
1430 mutex_unlock(&thermal_list_lock);
1431
Matthew Garrettb1569e92008-12-03 17:55:32 +00001432 thermal_zone_device_set_polling(tz, 0);
1433
Zhang Rui203d3d42008-01-17 15:51:08 +08001434 if (tz->type[0])
1435 device_remove_file(&tz->device, &dev_attr_type);
1436 device_remove_file(&tz->device, &dev_attr_temp);
1437 if (tz->ops->get_mode)
1438 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001439 remove_trip_attrs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001440
Zhang Ruie68b16a2008-04-21 16:07:52 +08001441 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001442 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1443 idr_destroy(&tz->idr);
1444 mutex_destroy(&tz->lock);
1445 device_unregister(&tz->device);
1446 return;
1447}
Zhang Rui203d3d42008-01-17 15:51:08 +08001448EXPORT_SYMBOL(thermal_zone_device_unregister);
1449
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001450#ifdef CONFIG_NET
1451static struct genl_family thermal_event_genl_family = {
1452 .id = GENL_ID_GENERATE,
1453 .name = THERMAL_GENL_FAMILY_NAME,
1454 .version = THERMAL_GENL_VERSION,
1455 .maxattr = THERMAL_GENL_ATTR_MAX,
1456};
1457
1458static struct genl_multicast_group thermal_event_mcgrp = {
1459 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1460};
1461
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001462int thermal_generate_netlink_event(u32 orig, enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301463{
1464 struct sk_buff *skb;
1465 struct nlattr *attr;
1466 struct thermal_genl_event *thermal_event;
1467 void *msg_header;
1468 int size;
1469 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001470 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301471
1472 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001473 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1474 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301475
1476 skb = genlmsg_new(size, GFP_ATOMIC);
1477 if (!skb)
1478 return -ENOMEM;
1479
1480 /* add the genetlink message header */
1481 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1482 &thermal_event_genl_family, 0,
1483 THERMAL_GENL_CMD_EVENT);
1484 if (!msg_header) {
1485 nlmsg_free(skb);
1486 return -ENOMEM;
1487 }
1488
1489 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001490 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1491 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301492
1493 if (!attr) {
1494 nlmsg_free(skb);
1495 return -EINVAL;
1496 }
1497
1498 thermal_event = nla_data(attr);
1499 if (!thermal_event) {
1500 nlmsg_free(skb);
1501 return -EINVAL;
1502 }
1503
1504 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1505
1506 thermal_event->orig = orig;
1507 thermal_event->event = event;
1508
1509 /* send multicast genetlink message */
1510 result = genlmsg_end(skb, msg_header);
1511 if (result < 0) {
1512 nlmsg_free(skb);
1513 return result;
1514 }
1515
1516 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1517 if (result)
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001518 pr_info("failed to send netlink event:%d\n", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301519
1520 return result;
1521}
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001522EXPORT_SYMBOL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301523
1524static int genetlink_init(void)
1525{
1526 int result;
1527
1528 result = genl_register_family(&thermal_event_genl_family);
1529 if (result)
1530 return result;
1531
1532 result = genl_register_mc_group(&thermal_event_genl_family,
1533 &thermal_event_mcgrp);
1534 if (result)
1535 genl_unregister_family(&thermal_event_genl_family);
1536 return result;
1537}
1538
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001539static void genetlink_exit(void)
1540{
1541 genl_unregister_family(&thermal_event_genl_family);
1542}
1543#else /* !CONFIG_NET */
1544static inline int genetlink_init(void) { return 0; }
1545static inline void genetlink_exit(void) {}
1546#endif /* !CONFIG_NET */
1547
Zhang Rui203d3d42008-01-17 15:51:08 +08001548static int __init thermal_init(void)
1549{
1550 int result = 0;
1551
1552 result = class_register(&thermal_class);
1553 if (result) {
1554 idr_destroy(&thermal_tz_idr);
1555 idr_destroy(&thermal_cdev_idr);
1556 mutex_destroy(&thermal_idr_lock);
1557 mutex_destroy(&thermal_list_lock);
1558 }
R.Durgadoss4cb18722010-10-27 03:33:29 +05301559 result = genetlink_init();
Zhang Rui203d3d42008-01-17 15:51:08 +08001560 return result;
1561}
1562
1563static void __exit thermal_exit(void)
1564{
1565 class_unregister(&thermal_class);
1566 idr_destroy(&thermal_tz_idr);
1567 idr_destroy(&thermal_cdev_idr);
1568 mutex_destroy(&thermal_idr_lock);
1569 mutex_destroy(&thermal_list_lock);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301570 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001571}
1572
R.Durgadoss4cb18722010-10-27 03:33:29 +05301573fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001574module_exit(thermal_exit);