blob: 6e9d3dc9d89bf1404944575f9fc4d29109e8fe90 [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 Ruice119f82012-06-27 14:13:04 +080044#define THERMAL_NO_TARGET -1UL
Zhang Rui74051ba2012-06-26 16:27:22 +080045/*
46 * This structure is used to describe the behavior of
47 * a certain cooling device on a certain trip point
48 * in a certain thermal zone
49 */
Zhang Ruib81b6ba2012-06-27 10:08:19 +080050struct thermal_instance {
Zhang Rui203d3d42008-01-17 15:51:08 +080051 int id;
52 char name[THERMAL_NAME_LENGTH];
53 struct thermal_zone_device *tz;
54 struct thermal_cooling_device *cdev;
55 int trip;
Zhang Rui74051ba2012-06-26 16:27:22 +080056 unsigned long upper; /* Highest cooling state for this trip point */
57 unsigned long lower; /* Lowest cooling state for this trip point */
Zhang Ruice119f82012-06-27 14:13:04 +080058 unsigned long target; /* expected cooling state */
Zhang Rui203d3d42008-01-17 15:51:08 +080059 char attr_name[THERMAL_NAME_LENGTH];
60 struct device_attribute attr;
Zhang Ruicddf31b2012-06-27 10:09:36 +080061 struct list_head tz_node; /* node in tz->thermal_instances */
Zhang Ruib5e4ae62012-06-27 14:11:52 +080062 struct list_head cdev_node; /* node in cdev->thermal_instances */
Zhang Rui203d3d42008-01-17 15:51:08 +080063};
64
65static DEFINE_IDR(thermal_tz_idr);
66static DEFINE_IDR(thermal_cdev_idr);
67static DEFINE_MUTEX(thermal_idr_lock);
68
69static LIST_HEAD(thermal_tz_list);
70static LIST_HEAD(thermal_cdev_list);
71static DEFINE_MUTEX(thermal_list_lock);
72
73static int get_idr(struct idr *idr, struct mutex *lock, int *id)
74{
75 int err;
76
Joe Perchescaca8b82012-03-21 12:55:02 -070077again:
Zhang Rui203d3d42008-01-17 15:51:08 +080078 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
79 return -ENOMEM;
80
81 if (lock)
82 mutex_lock(lock);
83 err = idr_get_new(idr, NULL, id);
84 if (lock)
85 mutex_unlock(lock);
86 if (unlikely(err == -EAGAIN))
87 goto again;
88 else if (unlikely(err))
89 return err;
90
91 *id = *id & MAX_ID_MASK;
92 return 0;
93}
94
95static void release_idr(struct idr *idr, struct mutex *lock, int id)
96{
97 if (lock)
98 mutex_lock(lock);
99 idr_remove(idr, id);
100 if (lock)
101 mutex_unlock(lock);
102}
103
104/* sys I/F for thermal zone */
105
106#define to_thermal_zone(_dev) \
107 container_of(_dev, struct thermal_zone_device, device)
108
109static ssize_t
110type_show(struct device *dev, struct device_attribute *attr, char *buf)
111{
112 struct thermal_zone_device *tz = to_thermal_zone(dev);
113
114 return sprintf(buf, "%s\n", tz->type);
115}
116
117static ssize_t
118temp_show(struct device *dev, struct device_attribute *attr, char *buf)
119{
120 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000121 long temperature;
122 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800123
124 if (!tz->ops->get_temp)
125 return -EPERM;
126
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000127 ret = tz->ops->get_temp(tz, &temperature);
128
129 if (ret)
130 return ret;
131
132 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800133}
134
135static ssize_t
136mode_show(struct device *dev, struct device_attribute *attr, char *buf)
137{
138 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000139 enum thermal_device_mode mode;
140 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800141
142 if (!tz->ops->get_mode)
143 return -EPERM;
144
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000145 result = tz->ops->get_mode(tz, &mode);
146 if (result)
147 return result;
148
149 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
150 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800151}
152
153static ssize_t
154mode_store(struct device *dev, struct device_attribute *attr,
155 const char *buf, size_t count)
156{
157 struct thermal_zone_device *tz = to_thermal_zone(dev);
158 int result;
159
160 if (!tz->ops->set_mode)
161 return -EPERM;
162
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530163 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000164 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530165 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000166 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
167 else
168 result = -EINVAL;
169
Zhang Rui203d3d42008-01-17 15:51:08 +0800170 if (result)
171 return result;
172
173 return count;
174}
175
176static ssize_t
177trip_point_type_show(struct device *dev, struct device_attribute *attr,
178 char *buf)
179{
180 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000181 enum thermal_trip_type type;
182 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800183
184 if (!tz->ops->get_trip_type)
185 return -EPERM;
186
187 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
188 return -EINVAL;
189
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000190 result = tz->ops->get_trip_type(tz, trip, &type);
191 if (result)
192 return result;
193
194 switch (type) {
195 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300196 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000197 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300198 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000199 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300200 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000201 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300202 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000203 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300204 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000205 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800206}
207
208static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800209trip_point_temp_store(struct device *dev, struct device_attribute *attr,
210 const char *buf, size_t count)
211{
212 struct thermal_zone_device *tz = to_thermal_zone(dev);
213 int trip, ret;
214 unsigned long temperature;
215
216 if (!tz->ops->set_trip_temp)
217 return -EPERM;
218
219 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
220 return -EINVAL;
221
222 if (kstrtoul(buf, 10, &temperature))
223 return -EINVAL;
224
225 ret = tz->ops->set_trip_temp(tz, trip, temperature);
226
227 return ret ? ret : count;
228}
229
230static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800231trip_point_temp_show(struct device *dev, struct device_attribute *attr,
232 char *buf)
233{
234 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000235 int trip, ret;
236 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800237
238 if (!tz->ops->get_trip_temp)
239 return -EPERM;
240
241 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
242 return -EINVAL;
243
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000244 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
245
246 if (ret)
247 return ret;
248
249 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800250}
251
Matthew Garrett03a971a2008-12-03 18:00:38 +0000252static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800253trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
254 const char *buf, size_t count)
255{
256 struct thermal_zone_device *tz = to_thermal_zone(dev);
257 int trip, ret;
258 unsigned long temperature;
259
260 if (!tz->ops->set_trip_hyst)
261 return -EPERM;
262
263 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
264 return -EINVAL;
265
266 if (kstrtoul(buf, 10, &temperature))
267 return -EINVAL;
268
269 /*
270 * We are not doing any check on the 'temperature' value
271 * here. The driver implementing 'set_trip_hyst' has to
272 * take care of this.
273 */
274 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
275
276 return ret ? ret : count;
277}
278
279static ssize_t
280trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
281 char *buf)
282{
283 struct thermal_zone_device *tz = to_thermal_zone(dev);
284 int trip, ret;
285 unsigned long temperature;
286
287 if (!tz->ops->get_trip_hyst)
288 return -EPERM;
289
290 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
291 return -EINVAL;
292
293 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
294
295 return ret ? ret : sprintf(buf, "%ld\n", temperature);
296}
297
298static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000299passive_store(struct device *dev, struct device_attribute *attr,
300 const char *buf, size_t count)
301{
302 struct thermal_zone_device *tz = to_thermal_zone(dev);
303 struct thermal_cooling_device *cdev = NULL;
304 int state;
305
306 if (!sscanf(buf, "%d\n", &state))
307 return -EINVAL;
308
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100309 /* sanity check: values below 1000 millicelcius don't make sense
310 * and can cause the system to go into a thermal heart attack
311 */
312 if (state && state < 1000)
313 return -EINVAL;
314
Matthew Garrett03a971a2008-12-03 18:00:38 +0000315 if (state && !tz->forced_passive) {
316 mutex_lock(&thermal_list_lock);
317 list_for_each_entry(cdev, &thermal_cdev_list, node) {
318 if (!strncmp("Processor", cdev->type,
319 sizeof("Processor")))
320 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800321 THERMAL_TRIPS_NONE, cdev,
322 THERMAL_NO_LIMIT,
323 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000324 }
325 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100326 if (!tz->passive_delay)
327 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000328 } else if (!state && tz->forced_passive) {
329 mutex_lock(&thermal_list_lock);
330 list_for_each_entry(cdev, &thermal_cdev_list, node) {
331 if (!strncmp("Processor", cdev->type,
332 sizeof("Processor")))
333 thermal_zone_unbind_cooling_device(tz,
334 THERMAL_TRIPS_NONE,
335 cdev);
336 }
337 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100338 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000339 }
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{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800436 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800437
438 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800439 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800440
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
Matthew Garrettb1569e92008-12-03 17:55:32 +0000718static void thermal_zone_device_check(struct work_struct *work)
719{
720 struct thermal_zone_device *tz = container_of(work, struct
721 thermal_zone_device,
722 poll_queue.work);
723 thermal_zone_device_update(tz);
724}
Zhang Ruie68b16a2008-04-21 16:07:52 +0800725
Zhang Rui203d3d42008-01-17 15:51:08 +0800726/**
727 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800728 * @tz: thermal zone device
729 * @trip: indicates which trip point the cooling devices is
730 * associated with in this thermal zone.
731 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500732 *
733 * This function is usually called in the thermal zone device .bind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800734 */
735int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
736 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800737 struct thermal_cooling_device *cdev,
738 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +0800739{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800740 struct thermal_instance *dev;
741 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500742 struct thermal_zone_device *pos1;
743 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800744 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800745 int result;
746
Len Brown543a9562008-02-07 16:55:08 -0500747 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800748 return -EINVAL;
749
Thomas Sujithc7516702008-02-15 00:58:50 -0500750 list_for_each_entry(pos1, &thermal_tz_list, node) {
751 if (pos1 == tz)
752 break;
753 }
754 list_for_each_entry(pos2, &thermal_cdev_list, node) {
755 if (pos2 == cdev)
756 break;
757 }
758
759 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800760 return -EINVAL;
761
Zhang Rui9d998422012-06-26 16:35:57 +0800762 cdev->ops->get_max_state(cdev, &max_state);
763
764 /* lower default 0, upper default max_state */
765 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
766 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
767
768 if (lower > upper || upper > max_state)
769 return -EINVAL;
770
Zhang Rui203d3d42008-01-17 15:51:08 +0800771 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800772 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800773 if (!dev)
774 return -ENOMEM;
775 dev->tz = tz;
776 dev->cdev = cdev;
777 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800778 dev->upper = upper;
779 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800780 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +0800781
Zhang Rui203d3d42008-01-17 15:51:08 +0800782 result = get_idr(&tz->idr, &tz->lock, &dev->id);
783 if (result)
784 goto free_mem;
785
786 sprintf(dev->name, "cdev%d", dev->id);
787 result =
788 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
789 if (result)
790 goto release_idr;
791
792 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700793 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800794 dev->attr.attr.name = dev->attr_name;
795 dev->attr.attr.mode = 0444;
796 dev->attr.show = thermal_cooling_device_trip_point_show;
797 result = device_create_file(&tz->device, &dev->attr);
798 if (result)
799 goto remove_symbol_link;
800
801 mutex_lock(&tz->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800802 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +0800803 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
804 result = -EEXIST;
805 break;
806 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800807 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800808 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800809 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
810 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800811 mutex_unlock(&tz->lock);
812
813 if (!result)
814 return 0;
815
816 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -0700817remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800818 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -0700819release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +0800820 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -0700821free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800822 kfree(dev);
823 return result;
824}
825EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
826
827/**
828 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800829 * @tz: thermal zone device
830 * @trip: indicates which trip point the cooling devices is
831 * associated with in this thermal zone.
832 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500833 *
834 * This function is usually called in the thermal zone device .unbind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800835 */
836int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
837 int trip,
838 struct thermal_cooling_device *cdev)
839{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800840 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +0800841
842 mutex_lock(&tz->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800843 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -0500844 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800845 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800846 list_del(&pos->cdev_node);
Zhang Rui203d3d42008-01-17 15:51:08 +0800847 mutex_unlock(&tz->lock);
848 goto unbind;
849 }
850 }
851 mutex_unlock(&tz->lock);
852
853 return -ENODEV;
854
Joe Perchescaca8b82012-03-21 12:55:02 -0700855unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +0800856 device_remove_file(&tz->device, &pos->attr);
857 sysfs_remove_link(&tz->device.kobj, pos->name);
858 release_idr(&tz->idr, &tz->lock, pos->id);
859 kfree(pos);
860 return 0;
861}
862EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
863
864static void thermal_release(struct device *dev)
865{
866 struct thermal_zone_device *tz;
867 struct thermal_cooling_device *cdev;
868
Joe Perchescaca8b82012-03-21 12:55:02 -0700869 if (!strncmp(dev_name(dev), "thermal_zone",
870 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800871 tz = to_thermal_zone(dev);
872 kfree(tz);
873 } else {
874 cdev = to_cooling_device(dev);
875 kfree(cdev);
876 }
877}
878
879static struct class thermal_class = {
880 .name = "thermal",
881 .dev_release = thermal_release,
882};
883
884/**
885 * thermal_cooling_device_register - register a new thermal cooling device
886 * @type: the thermal cooling device type.
887 * @devdata: device private data.
888 * @ops: standard thermal cooling devices callbacks.
889 */
Joe Perchescaca8b82012-03-21 12:55:02 -0700890struct thermal_cooling_device *
891thermal_cooling_device_register(char *type, void *devdata,
892 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800893{
894 struct thermal_cooling_device *cdev;
895 struct thermal_zone_device *pos;
896 int result;
897
898 if (strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500899 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800900
901 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -0500902 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500903 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800904
905 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
906 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500907 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +0800908
909 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
910 if (result) {
911 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500912 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800913 }
914
915 strcpy(cdev->type, type);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800916 INIT_LIST_HEAD(&cdev->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +0800917 cdev->ops = ops;
Zhang Ruice119f82012-06-27 14:13:04 +0800918 cdev->updated = true;
Zhang Rui203d3d42008-01-17 15:51:08 +0800919 cdev->device.class = &thermal_class;
920 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -0800921 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800922 result = device_register(&cdev->device);
923 if (result) {
924 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
925 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500926 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800927 }
928
929 /* sys I/F */
930 if (type) {
Len Brown543a9562008-02-07 16:55:08 -0500931 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +0800932 if (result)
933 goto unregister;
934 }
935
936 result = device_create_file(&cdev->device, &dev_attr_max_state);
937 if (result)
938 goto unregister;
939
940 result = device_create_file(&cdev->device, &dev_attr_cur_state);
941 if (result)
942 goto unregister;
943
944 mutex_lock(&thermal_list_lock);
945 list_add(&cdev->node, &thermal_cdev_list);
946 list_for_each_entry(pos, &thermal_tz_list, node) {
947 if (!pos->ops->bind)
948 continue;
949 result = pos->ops->bind(pos, cdev);
950 if (result)
951 break;
952
953 }
954 mutex_unlock(&thermal_list_lock);
955
956 if (!result)
957 return cdev;
958
Joe Perchescaca8b82012-03-21 12:55:02 -0700959unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +0800960 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
961 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500962 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800963}
964EXPORT_SYMBOL(thermal_cooling_device_register);
965
966/**
967 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +0800968 * @cdev: the thermal cooling device to remove.
969 *
970 * thermal_cooling_device_unregister() must be called when the device is no
971 * longer needed.
972 */
973void thermal_cooling_device_unregister(struct
974 thermal_cooling_device
975 *cdev)
976{
977 struct thermal_zone_device *tz;
978 struct thermal_cooling_device *pos = NULL;
979
980 if (!cdev)
981 return;
982
983 mutex_lock(&thermal_list_lock);
984 list_for_each_entry(pos, &thermal_cdev_list, node)
985 if (pos == cdev)
986 break;
987 if (pos != cdev) {
988 /* thermal cooling device not found */
989 mutex_unlock(&thermal_list_lock);
990 return;
991 }
992 list_del(&cdev->node);
993 list_for_each_entry(tz, &thermal_tz_list, node) {
994 if (!tz->ops->unbind)
995 continue;
996 tz->ops->unbind(tz, cdev);
997 }
998 mutex_unlock(&thermal_list_lock);
999 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001000 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001001 device_remove_file(&cdev->device, &dev_attr_max_state);
1002 device_remove_file(&cdev->device, &dev_attr_cur_state);
1003
1004 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1005 device_unregister(&cdev->device);
1006 return;
1007}
1008EXPORT_SYMBOL(thermal_cooling_device_unregister);
1009
Zhang Ruice119f82012-06-27 14:13:04 +08001010static void thermal_cdev_do_update(struct thermal_cooling_device *cdev)
1011{
1012 struct thermal_instance *instance;
1013 unsigned long target = 0;
1014
1015 /* cooling device is updated*/
1016 if (cdev->updated)
1017 return;
1018
1019 /* Make sure cdev enters the deepest cooling state */
1020 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1021 if (instance->target == THERMAL_NO_TARGET)
1022 continue;
1023 if (instance->target > target)
1024 target = instance->target;
1025 }
1026 cdev->ops->set_cur_state(cdev, target);
1027 cdev->updated = true;
1028}
1029
1030static void thermal_zone_do_update(struct thermal_zone_device *tz)
1031{
1032 struct thermal_instance *instance;
1033
1034 list_for_each_entry(instance, &tz->thermal_instances, tz_node)
1035 thermal_cdev_do_update(instance->cdev);
1036}
1037
Zhang Rui4ae46be2012-06-27 10:05:39 +08001038/*
Zhang Rui908b9fb2012-06-27 14:14:05 +08001039 * Cooling algorithm for both active and passive cooling
Zhang Rui4ae46be2012-06-27 10:05:39 +08001040 *
1041 * 1. if the temperature is higher than a trip point,
1042 * a. if the trend is THERMAL_TREND_RAISING, use higher cooling
1043 * state for this trip point
1044 * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
1045 * state for this trip point
1046 *
1047 * 2. if the temperature is lower than a trip point, use lower
1048 * cooling state for this trip point
1049 *
1050 * Note that this behaves the same as the previous passive cooling
1051 * algorithm.
1052 */
1053
1054static void thermal_zone_trip_update(struct thermal_zone_device *tz,
1055 int trip, long temp)
1056{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001057 struct thermal_instance *instance;
Zhang Rui4ae46be2012-06-27 10:05:39 +08001058 struct thermal_cooling_device *cdev = NULL;
1059 unsigned long cur_state, max_state;
1060 long trip_temp;
Zhang Rui908b9fb2012-06-27 14:14:05 +08001061 enum thermal_trip_type trip_type;
Zhang Rui4ae46be2012-06-27 10:05:39 +08001062 enum thermal_trend trend;
1063
Zhang Rui908b9fb2012-06-27 14:14:05 +08001064 if (trip == THERMAL_TRIPS_NONE) {
1065 trip_temp = tz->forced_passive;
1066 trip_type = THERMAL_TRIPS_NONE;
1067 } else {
1068 tz->ops->get_trip_temp(tz, trip, &trip_temp);
1069 tz->ops->get_trip_type(tz, trip, &trip_type);
1070 }
Zhang Rui4ae46be2012-06-27 10:05:39 +08001071
1072 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
1073 /*
1074 * compare the current temperature and previous temperature
1075 * to get the thermal trend, if no special requirement
1076 */
1077 if (tz->temperature > tz->last_temperature)
1078 trend = THERMAL_TREND_RAISING;
1079 else if (tz->temperature < tz->last_temperature)
1080 trend = THERMAL_TREND_DROPPING;
1081 else
1082 trend = THERMAL_TREND_STABLE;
1083 }
1084
1085 if (temp >= trip_temp) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001086 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
Zhang Rui4ae46be2012-06-27 10:05:39 +08001087 if (instance->trip != trip)
1088 continue;
1089
1090 cdev = instance->cdev;
1091
1092 cdev->ops->get_cur_state(cdev, &cur_state);
1093 cdev->ops->get_max_state(cdev, &max_state);
1094
1095 if (trend == THERMAL_TREND_RAISING) {
1096 cur_state = cur_state < instance->upper ?
1097 (cur_state + 1) : instance->upper;
1098 } else if (trend == THERMAL_TREND_DROPPING) {
1099 cur_state = cur_state > instance->lower ?
1100 (cur_state - 1) : instance->lower;
1101 }
Zhang Rui908b9fb2012-06-27 14:14:05 +08001102
1103 /* activate a passive thermal instance */
1104 if ((trip_type == THERMAL_TRIP_PASSIVE ||
1105 trip_type == THERMAL_TRIPS_NONE) &&
1106 instance->target == THERMAL_NO_TARGET)
1107 tz->passive++;
1108
Zhang Ruice119f82012-06-27 14:13:04 +08001109 instance->target = cur_state;
1110 cdev->updated = false; /* cooling device needs update */
Zhang Rui4ae46be2012-06-27 10:05:39 +08001111 }
1112 } else { /* below trip */
Zhang Ruicddf31b2012-06-27 10:09:36 +08001113 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
Zhang Rui4ae46be2012-06-27 10:05:39 +08001114 if (instance->trip != trip)
1115 continue;
1116
Zhang Ruice119f82012-06-27 14:13:04 +08001117 /* Do not use the inactive thermal instance */
1118 if (instance->target == THERMAL_NO_TARGET)
1119 continue;
Zhang Rui4ae46be2012-06-27 10:05:39 +08001120 cdev = instance->cdev;
1121 cdev->ops->get_cur_state(cdev, &cur_state);
1122
1123 cur_state = cur_state > instance->lower ?
Zhang Ruice119f82012-06-27 14:13:04 +08001124 (cur_state - 1) : THERMAL_NO_TARGET;
Zhang Rui908b9fb2012-06-27 14:14:05 +08001125
1126 /* deactivate a passive thermal instance */
1127 if ((trip_type == THERMAL_TRIP_PASSIVE ||
1128 trip_type == THERMAL_TRIPS_NONE) &&
1129 cur_state == THERMAL_NO_TARGET)
1130 tz->passive--;
Zhang Ruice119f82012-06-27 14:13:04 +08001131 instance->target = cur_state;
1132 cdev->updated = false; /* cooling device needs update */
Zhang Rui4ae46be2012-06-27 10:05:39 +08001133 }
1134 }
1135
1136 return;
1137}
Zhang Rui203d3d42008-01-17 15:51:08 +08001138/**
Matthew Garrettb1569e92008-12-03 17:55:32 +00001139 * thermal_zone_device_update - force an update of a thermal zone's state
1140 * @ttz: the thermal zone to update
1141 */
1142
1143void thermal_zone_device_update(struct thermal_zone_device *tz)
1144{
1145 int count, ret = 0;
1146 long temp, trip_temp;
1147 enum thermal_trip_type trip_type;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001148
1149 mutex_lock(&tz->lock);
1150
Michael Brunner0d288162009-08-26 14:29:25 -07001151 if (tz->ops->get_temp(tz, &temp)) {
1152 /* get_temp failed - retry it later */
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001153 pr_warn("failed to read out thermal zone %d\n", tz->id);
Michael Brunner0d288162009-08-26 14:29:25 -07001154 goto leave;
1155 }
Matthew Garrettb1569e92008-12-03 17:55:32 +00001156
Zhang Rui601f3d42012-06-27 09:54:33 +08001157 tz->last_temperature = tz->temperature;
1158 tz->temperature = temp;
1159
Matthew Garrettb1569e92008-12-03 17:55:32 +00001160 for (count = 0; count < tz->trips; count++) {
1161 tz->ops->get_trip_type(tz, count, &trip_type);
1162 tz->ops->get_trip_temp(tz, count, &trip_temp);
1163
1164 switch (trip_type) {
1165 case THERMAL_TRIP_CRITICAL:
Vladimir Zajac29321352009-05-06 19:34:21 +02001166 if (temp >= trip_temp) {
Matthew Garrettb1569e92008-12-03 17:55:32 +00001167 if (tz->ops->notify)
1168 ret = tz->ops->notify(tz, count,
1169 trip_type);
1170 if (!ret) {
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001171 pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1172 temp/1000);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001173 orderly_poweroff(true);
1174 }
1175 }
1176 break;
1177 case THERMAL_TRIP_HOT:
Vladimir Zajac29321352009-05-06 19:34:21 +02001178 if (temp >= trip_temp)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001179 if (tz->ops->notify)
1180 tz->ops->notify(tz, count, trip_type);
1181 break;
1182 case THERMAL_TRIP_ACTIVE:
Zhang Rui4ae46be2012-06-27 10:05:39 +08001183 thermal_zone_trip_update(tz, count, temp);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001184 break;
1185 case THERMAL_TRIP_PASSIVE:
Vladimir Zajac29321352009-05-06 19:34:21 +02001186 if (temp >= trip_temp || tz->passive)
Zhang Rui908b9fb2012-06-27 14:14:05 +08001187 thermal_zone_trip_update(tz, count, temp);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001188 break;
1189 }
1190 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001191
1192 if (tz->forced_passive)
Zhang Rui908b9fb2012-06-27 14:14:05 +08001193 thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE, temp);
1194 thermal_zone_do_update(tz);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001195
Joe Perchescaca8b82012-03-21 12:55:02 -07001196leave:
Matthew Garrettb1569e92008-12-03 17:55:32 +00001197 if (tz->passive)
1198 thermal_zone_device_set_polling(tz, tz->passive_delay);
1199 else if (tz->polling_delay)
1200 thermal_zone_device_set_polling(tz, tz->polling_delay);
Frans Pop3767cb52009-10-26 08:39:04 +01001201 else
1202 thermal_zone_device_set_polling(tz, 0);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001203 mutex_unlock(&tz->lock);
1204}
1205EXPORT_SYMBOL(thermal_zone_device_update);
1206
1207/**
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001208 * create_trip_attrs - create attributes for trip points
1209 * @tz: the thermal zone device
1210 * @mask: Writeable trip point bitmap.
1211 */
1212static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1213{
1214 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001215 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001216
Durgadoss R27365a62012-07-25 10:10:59 +08001217 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001218 if (!tz->trip_type_attrs)
1219 return -ENOMEM;
1220
Durgadoss R27365a62012-07-25 10:10:59 +08001221 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001222 if (!tz->trip_temp_attrs) {
1223 kfree(tz->trip_type_attrs);
1224 return -ENOMEM;
1225 }
1226
Durgadoss R27365a62012-07-25 10:10:59 +08001227 if (tz->ops->get_trip_hyst) {
1228 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1229 if (!tz->trip_hyst_attrs) {
1230 kfree(tz->trip_type_attrs);
1231 kfree(tz->trip_temp_attrs);
1232 return -ENOMEM;
1233 }
1234 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001235
Durgadoss R27365a62012-07-25 10:10:59 +08001236
1237 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001238 /* create trip type attribute */
1239 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1240 "trip_point_%d_type", indx);
1241
1242 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1243 tz->trip_type_attrs[indx].attr.attr.name =
1244 tz->trip_type_attrs[indx].name;
1245 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1246 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1247
1248 device_create_file(&tz->device,
1249 &tz->trip_type_attrs[indx].attr);
1250
1251 /* create trip temp attribute */
1252 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1253 "trip_point_%d_temp", indx);
1254
1255 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1256 tz->trip_temp_attrs[indx].attr.attr.name =
1257 tz->trip_temp_attrs[indx].name;
1258 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1259 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1260 if (mask & (1 << indx)) {
1261 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1262 tz->trip_temp_attrs[indx].attr.store =
1263 trip_point_temp_store;
1264 }
1265
1266 device_create_file(&tz->device,
1267 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001268
1269 /* create Optional trip hyst attribute */
1270 if (!tz->ops->get_trip_hyst)
1271 continue;
1272 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1273 "trip_point_%d_hyst", indx);
1274
1275 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1276 tz->trip_hyst_attrs[indx].attr.attr.name =
1277 tz->trip_hyst_attrs[indx].name;
1278 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1279 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1280 if (tz->ops->set_trip_hyst) {
1281 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1282 tz->trip_hyst_attrs[indx].attr.store =
1283 trip_point_hyst_store;
1284 }
1285
1286 device_create_file(&tz->device,
1287 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001288 }
1289 return 0;
1290}
1291
1292static void remove_trip_attrs(struct thermal_zone_device *tz)
1293{
1294 int indx;
1295
1296 for (indx = 0; indx < tz->trips; indx++) {
1297 device_remove_file(&tz->device,
1298 &tz->trip_type_attrs[indx].attr);
1299 device_remove_file(&tz->device,
1300 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001301 if (tz->ops->get_trip_hyst)
1302 device_remove_file(&tz->device,
1303 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001304 }
1305 kfree(tz->trip_type_attrs);
1306 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001307 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001308}
1309
1310/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001311 * thermal_zone_device_register - register a new thermal zone device
1312 * @type: the thermal zone device type
1313 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001314 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001315 * @devdata: private device data
1316 * @ops: standard thermal zone device callbacks
Matthew Garrettb1569e92008-12-03 17:55:32 +00001317 * @passive_delay: number of milliseconds to wait between polls when
1318 * performing passive cooling
1319 * @polling_delay: number of milliseconds to wait between polls when checking
1320 * whether trip points have been crossed (0 for interrupt
1321 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001322 *
1323 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001324 * longer needed. The passive cooling depends on the .get_trend() return value.
Zhang Rui203d3d42008-01-17 15:51:08 +08001325 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001326struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001327 int trips, int mask, void *devdata,
Alan Cox5b275ce2010-11-11 15:27:29 +00001328 const struct thermal_zone_device_ops *ops,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001329 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001330{
1331 struct thermal_zone_device *tz;
1332 struct thermal_cooling_device *pos;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001333 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001334 int result;
1335 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001336 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001337
1338 if (strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001339 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001340
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001341 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001342 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001343
1344 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001345 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001346
1347 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1348 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001349 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001350
Zhang Rui2d374132012-06-27 10:09:00 +08001351 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001352 idr_init(&tz->idr);
1353 mutex_init(&tz->lock);
1354 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1355 if (result) {
1356 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001357 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001358 }
1359
1360 strcpy(tz->type, type);
1361 tz->ops = ops;
1362 tz->device.class = &thermal_class;
1363 tz->devdata = devdata;
1364 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001365 tz->passive_delay = passive_delay;
1366 tz->polling_delay = polling_delay;
1367
Kay Sievers354655e2009-01-06 10:44:37 -08001368 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001369 result = device_register(&tz->device);
1370 if (result) {
1371 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1372 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001373 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001374 }
1375
1376 /* sys I/F */
1377 if (type) {
1378 result = device_create_file(&tz->device, &dev_attr_type);
1379 if (result)
1380 goto unregister;
1381 }
1382
1383 result = device_create_file(&tz->device, &dev_attr_temp);
1384 if (result)
1385 goto unregister;
1386
1387 if (ops->get_mode) {
1388 result = device_create_file(&tz->device, &dev_attr_mode);
1389 if (result)
1390 goto unregister;
1391 }
1392
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001393 result = create_trip_attrs(tz, mask);
1394 if (result)
1395 goto unregister;
1396
Zhang Rui203d3d42008-01-17 15:51:08 +08001397 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001398 tz->ops->get_trip_type(tz, count, &trip_type);
1399 if (trip_type == THERMAL_TRIP_PASSIVE)
1400 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001401 }
1402
Matthew Garrett03a971a2008-12-03 18:00:38 +00001403 if (!passive)
1404 result = device_create_file(&tz->device,
1405 &dev_attr_passive);
1406
1407 if (result)
1408 goto unregister;
1409
Zhang Ruie68b16a2008-04-21 16:07:52 +08001410 result = thermal_add_hwmon_sysfs(tz);
1411 if (result)
1412 goto unregister;
1413
Zhang Rui203d3d42008-01-17 15:51:08 +08001414 mutex_lock(&thermal_list_lock);
1415 list_add_tail(&tz->node, &thermal_tz_list);
1416 if (ops->bind)
1417 list_for_each_entry(pos, &thermal_cdev_list, node) {
Len Brown543a9562008-02-07 16:55:08 -05001418 result = ops->bind(tz, pos);
1419 if (result)
1420 break;
Zhang Rui203d3d42008-01-17 15:51:08 +08001421 }
1422 mutex_unlock(&thermal_list_lock);
1423
Matthew Garrettb1569e92008-12-03 17:55:32 +00001424 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1425
1426 thermal_zone_device_update(tz);
1427
Zhang Rui203d3d42008-01-17 15:51:08 +08001428 if (!result)
1429 return tz;
1430
Joe Perchescaca8b82012-03-21 12:55:02 -07001431unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001432 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1433 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001434 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001435}
1436EXPORT_SYMBOL(thermal_zone_device_register);
1437
1438/**
1439 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001440 * @tz: the thermal zone device to remove
1441 */
1442void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1443{
1444 struct thermal_cooling_device *cdev;
1445 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001446
1447 if (!tz)
1448 return;
1449
1450 mutex_lock(&thermal_list_lock);
1451 list_for_each_entry(pos, &thermal_tz_list, node)
1452 if (pos == tz)
1453 break;
1454 if (pos != tz) {
1455 /* thermal zone device not found */
1456 mutex_unlock(&thermal_list_lock);
1457 return;
1458 }
1459 list_del(&tz->node);
1460 if (tz->ops->unbind)
1461 list_for_each_entry(cdev, &thermal_cdev_list, node)
1462 tz->ops->unbind(tz, cdev);
1463 mutex_unlock(&thermal_list_lock);
1464
Matthew Garrettb1569e92008-12-03 17:55:32 +00001465 thermal_zone_device_set_polling(tz, 0);
1466
Zhang Rui203d3d42008-01-17 15:51:08 +08001467 if (tz->type[0])
1468 device_remove_file(&tz->device, &dev_attr_type);
1469 device_remove_file(&tz->device, &dev_attr_temp);
1470 if (tz->ops->get_mode)
1471 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001472 remove_trip_attrs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001473
Zhang Ruie68b16a2008-04-21 16:07:52 +08001474 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001475 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1476 idr_destroy(&tz->idr);
1477 mutex_destroy(&tz->lock);
1478 device_unregister(&tz->device);
1479 return;
1480}
Zhang Rui203d3d42008-01-17 15:51:08 +08001481EXPORT_SYMBOL(thermal_zone_device_unregister);
1482
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001483#ifdef CONFIG_NET
1484static struct genl_family thermal_event_genl_family = {
1485 .id = GENL_ID_GENERATE,
1486 .name = THERMAL_GENL_FAMILY_NAME,
1487 .version = THERMAL_GENL_VERSION,
1488 .maxattr = THERMAL_GENL_ATTR_MAX,
1489};
1490
1491static struct genl_multicast_group thermal_event_mcgrp = {
1492 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1493};
1494
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001495int thermal_generate_netlink_event(u32 orig, enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301496{
1497 struct sk_buff *skb;
1498 struct nlattr *attr;
1499 struct thermal_genl_event *thermal_event;
1500 void *msg_header;
1501 int size;
1502 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001503 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301504
1505 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001506 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1507 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301508
1509 skb = genlmsg_new(size, GFP_ATOMIC);
1510 if (!skb)
1511 return -ENOMEM;
1512
1513 /* add the genetlink message header */
1514 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1515 &thermal_event_genl_family, 0,
1516 THERMAL_GENL_CMD_EVENT);
1517 if (!msg_header) {
1518 nlmsg_free(skb);
1519 return -ENOMEM;
1520 }
1521
1522 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001523 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1524 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301525
1526 if (!attr) {
1527 nlmsg_free(skb);
1528 return -EINVAL;
1529 }
1530
1531 thermal_event = nla_data(attr);
1532 if (!thermal_event) {
1533 nlmsg_free(skb);
1534 return -EINVAL;
1535 }
1536
1537 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1538
1539 thermal_event->orig = orig;
1540 thermal_event->event = event;
1541
1542 /* send multicast genetlink message */
1543 result = genlmsg_end(skb, msg_header);
1544 if (result < 0) {
1545 nlmsg_free(skb);
1546 return result;
1547 }
1548
1549 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1550 if (result)
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001551 pr_info("failed to send netlink event:%d\n", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301552
1553 return result;
1554}
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001555EXPORT_SYMBOL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301556
1557static int genetlink_init(void)
1558{
1559 int result;
1560
1561 result = genl_register_family(&thermal_event_genl_family);
1562 if (result)
1563 return result;
1564
1565 result = genl_register_mc_group(&thermal_event_genl_family,
1566 &thermal_event_mcgrp);
1567 if (result)
1568 genl_unregister_family(&thermal_event_genl_family);
1569 return result;
1570}
1571
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001572static void genetlink_exit(void)
1573{
1574 genl_unregister_family(&thermal_event_genl_family);
1575}
1576#else /* !CONFIG_NET */
1577static inline int genetlink_init(void) { return 0; }
1578static inline void genetlink_exit(void) {}
1579#endif /* !CONFIG_NET */
1580
Zhang Rui203d3d42008-01-17 15:51:08 +08001581static int __init thermal_init(void)
1582{
1583 int result = 0;
1584
1585 result = class_register(&thermal_class);
1586 if (result) {
1587 idr_destroy(&thermal_tz_idr);
1588 idr_destroy(&thermal_cdev_idr);
1589 mutex_destroy(&thermal_idr_lock);
1590 mutex_destroy(&thermal_list_lock);
1591 }
R.Durgadoss4cb18722010-10-27 03:33:29 +05301592 result = genetlink_init();
Zhang Rui203d3d42008-01-17 15:51:08 +08001593 return result;
1594}
1595
1596static void __exit thermal_exit(void)
1597{
1598 class_unregister(&thermal_class);
1599 idr_destroy(&thermal_tz_idr);
1600 idr_destroy(&thermal_cdev_idr);
1601 mutex_destroy(&thermal_idr_lock);
1602 mutex_destroy(&thermal_list_lock);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301603 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001604}
1605
R.Durgadoss4cb18722010-10-27 03:33:29 +05301606fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001607module_exit(thermal_exit);