blob: 70045c12d7df09cbbee27e4a42b51437e151e8b8 [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 Ruib81b6ba2012-06-27 10:08:19 +080049struct thermal_instance {
Zhang Rui203d3d42008-01-17 15:51:08 +080050 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;
Zhang Ruicddf31b2012-06-27 10:09:36 +080059 struct list_head tz_node; /* node in tz->thermal_instances */
Zhang Ruib5e4ae62012-06-27 14:11:52 +080060 struct list_head cdev_node; /* node in cdev->thermal_instances */
Zhang Rui203d3d42008-01-17 15:51:08 +080061};
62
63static DEFINE_IDR(thermal_tz_idr);
64static DEFINE_IDR(thermal_cdev_idr);
65static DEFINE_MUTEX(thermal_idr_lock);
66
67static LIST_HEAD(thermal_tz_list);
68static LIST_HEAD(thermal_cdev_list);
69static DEFINE_MUTEX(thermal_list_lock);
70
71static int get_idr(struct idr *idr, struct mutex *lock, int *id)
72{
73 int err;
74
Joe Perchescaca8b82012-03-21 12:55:02 -070075again:
Zhang Rui203d3d42008-01-17 15:51:08 +080076 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
77 return -ENOMEM;
78
79 if (lock)
80 mutex_lock(lock);
81 err = idr_get_new(idr, NULL, id);
82 if (lock)
83 mutex_unlock(lock);
84 if (unlikely(err == -EAGAIN))
85 goto again;
86 else if (unlikely(err))
87 return err;
88
89 *id = *id & MAX_ID_MASK;
90 return 0;
91}
92
93static void release_idr(struct idr *idr, struct mutex *lock, int id)
94{
95 if (lock)
96 mutex_lock(lock);
97 idr_remove(idr, id);
98 if (lock)
99 mutex_unlock(lock);
100}
101
102/* sys I/F for thermal zone */
103
104#define to_thermal_zone(_dev) \
105 container_of(_dev, struct thermal_zone_device, device)
106
107static ssize_t
108type_show(struct device *dev, struct device_attribute *attr, char *buf)
109{
110 struct thermal_zone_device *tz = to_thermal_zone(dev);
111
112 return sprintf(buf, "%s\n", tz->type);
113}
114
115static ssize_t
116temp_show(struct device *dev, struct device_attribute *attr, char *buf)
117{
118 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000119 long temperature;
120 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800121
122 if (!tz->ops->get_temp)
123 return -EPERM;
124
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000125 ret = tz->ops->get_temp(tz, &temperature);
126
127 if (ret)
128 return ret;
129
130 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800131}
132
133static ssize_t
134mode_show(struct device *dev, struct device_attribute *attr, char *buf)
135{
136 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000137 enum thermal_device_mode mode;
138 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800139
140 if (!tz->ops->get_mode)
141 return -EPERM;
142
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000143 result = tz->ops->get_mode(tz, &mode);
144 if (result)
145 return result;
146
147 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
148 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800149}
150
151static ssize_t
152mode_store(struct device *dev, struct device_attribute *attr,
153 const char *buf, size_t count)
154{
155 struct thermal_zone_device *tz = to_thermal_zone(dev);
156 int result;
157
158 if (!tz->ops->set_mode)
159 return -EPERM;
160
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530161 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000162 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530163 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000164 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
165 else
166 result = -EINVAL;
167
Zhang Rui203d3d42008-01-17 15:51:08 +0800168 if (result)
169 return result;
170
171 return count;
172}
173
174static ssize_t
175trip_point_type_show(struct device *dev, struct device_attribute *attr,
176 char *buf)
177{
178 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000179 enum thermal_trip_type type;
180 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800181
182 if (!tz->ops->get_trip_type)
183 return -EPERM;
184
185 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
186 return -EINVAL;
187
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000188 result = tz->ops->get_trip_type(tz, trip, &type);
189 if (result)
190 return result;
191
192 switch (type) {
193 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300194 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000195 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300196 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000197 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300198 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000199 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300200 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000201 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300202 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000203 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800204}
205
206static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800207trip_point_temp_store(struct device *dev, struct device_attribute *attr,
208 const char *buf, size_t count)
209{
210 struct thermal_zone_device *tz = to_thermal_zone(dev);
211 int trip, ret;
212 unsigned long temperature;
213
214 if (!tz->ops->set_trip_temp)
215 return -EPERM;
216
217 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
218 return -EINVAL;
219
220 if (kstrtoul(buf, 10, &temperature))
221 return -EINVAL;
222
223 ret = tz->ops->set_trip_temp(tz, trip, temperature);
224
225 return ret ? ret : count;
226}
227
228static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800229trip_point_temp_show(struct device *dev, struct device_attribute *attr,
230 char *buf)
231{
232 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000233 int trip, ret;
234 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800235
236 if (!tz->ops->get_trip_temp)
237 return -EPERM;
238
239 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
240 return -EINVAL;
241
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000242 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
243
244 if (ret)
245 return ret;
246
247 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800248}
249
Matthew Garrett03a971a2008-12-03 18:00:38 +0000250static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800251trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
252 const char *buf, size_t count)
253{
254 struct thermal_zone_device *tz = to_thermal_zone(dev);
255 int trip, ret;
256 unsigned long temperature;
257
258 if (!tz->ops->set_trip_hyst)
259 return -EPERM;
260
261 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
262 return -EINVAL;
263
264 if (kstrtoul(buf, 10, &temperature))
265 return -EINVAL;
266
267 /*
268 * We are not doing any check on the 'temperature' value
269 * here. The driver implementing 'set_trip_hyst' has to
270 * take care of this.
271 */
272 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
273
274 return ret ? ret : count;
275}
276
277static ssize_t
278trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
279 char *buf)
280{
281 struct thermal_zone_device *tz = to_thermal_zone(dev);
282 int trip, ret;
283 unsigned long temperature;
284
285 if (!tz->ops->get_trip_hyst)
286 return -EPERM;
287
288 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
289 return -EINVAL;
290
291 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
292
293 return ret ? ret : sprintf(buf, "%ld\n", temperature);
294}
295
296static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000297passive_store(struct device *dev, struct device_attribute *attr,
298 const char *buf, size_t count)
299{
300 struct thermal_zone_device *tz = to_thermal_zone(dev);
301 struct thermal_cooling_device *cdev = NULL;
302 int state;
303
304 if (!sscanf(buf, "%d\n", &state))
305 return -EINVAL;
306
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100307 /* sanity check: values below 1000 millicelcius don't make sense
308 * and can cause the system to go into a thermal heart attack
309 */
310 if (state && state < 1000)
311 return -EINVAL;
312
Matthew Garrett03a971a2008-12-03 18:00:38 +0000313 if (state && !tz->forced_passive) {
314 mutex_lock(&thermal_list_lock);
315 list_for_each_entry(cdev, &thermal_cdev_list, node) {
316 if (!strncmp("Processor", cdev->type,
317 sizeof("Processor")))
318 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800319 THERMAL_TRIPS_NONE, cdev,
320 THERMAL_NO_LIMIT,
321 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000322 }
323 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100324 if (!tz->passive_delay)
325 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000326 } else if (!state && tz->forced_passive) {
327 mutex_lock(&thermal_list_lock);
328 list_for_each_entry(cdev, &thermal_cdev_list, node) {
329 if (!strncmp("Processor", cdev->type,
330 sizeof("Processor")))
331 thermal_zone_unbind_cooling_device(tz,
332 THERMAL_TRIPS_NONE,
333 cdev);
334 }
335 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100336 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000337 }
338
Matthew Garrett03a971a2008-12-03 18:00:38 +0000339 tz->forced_passive = state;
340
341 thermal_zone_device_update(tz);
342
343 return count;
344}
345
346static ssize_t
347passive_show(struct device *dev, struct device_attribute *attr,
348 char *buf)
349{
350 struct thermal_zone_device *tz = to_thermal_zone(dev);
351
352 return sprintf(buf, "%d\n", tz->forced_passive);
353}
354
Zhang Rui203d3d42008-01-17 15:51:08 +0800355static DEVICE_ATTR(type, 0444, type_show, NULL);
356static DEVICE_ATTR(temp, 0444, temp_show, NULL);
357static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700358static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800359
Zhang Rui203d3d42008-01-17 15:51:08 +0800360/* sys I/F for cooling device */
361#define to_cooling_device(_dev) \
362 container_of(_dev, struct thermal_cooling_device, device)
363
364static ssize_t
365thermal_cooling_device_type_show(struct device *dev,
366 struct device_attribute *attr, char *buf)
367{
368 struct thermal_cooling_device *cdev = to_cooling_device(dev);
369
370 return sprintf(buf, "%s\n", cdev->type);
371}
372
373static ssize_t
374thermal_cooling_device_max_state_show(struct device *dev,
375 struct device_attribute *attr, char *buf)
376{
377 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000378 unsigned long state;
379 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800380
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000381 ret = cdev->ops->get_max_state(cdev, &state);
382 if (ret)
383 return ret;
384 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800385}
386
387static ssize_t
388thermal_cooling_device_cur_state_show(struct device *dev,
389 struct device_attribute *attr, char *buf)
390{
391 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000392 unsigned long state;
393 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800394
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000395 ret = cdev->ops->get_cur_state(cdev, &state);
396 if (ret)
397 return ret;
398 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800399}
400
401static ssize_t
402thermal_cooling_device_cur_state_store(struct device *dev,
403 struct device_attribute *attr,
404 const char *buf, size_t count)
405{
406 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000407 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800408 int result;
409
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000410 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800411 return -EINVAL;
412
Roel Kluinedb94912009-12-15 22:46:50 +0100413 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800414 return -EINVAL;
415
416 result = cdev->ops->set_cur_state(cdev, state);
417 if (result)
418 return result;
419 return count;
420}
421
422static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500423__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800424static DEVICE_ATTR(max_state, 0444,
425 thermal_cooling_device_max_state_show, NULL);
426static DEVICE_ATTR(cur_state, 0644,
427 thermal_cooling_device_cur_state_show,
428 thermal_cooling_device_cur_state_store);
429
430static ssize_t
431thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500432 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800433{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800434 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800435
436 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800437 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800438
439 if (instance->trip == THERMAL_TRIPS_NONE)
440 return sprintf(buf, "-1\n");
441 else
442 return sprintf(buf, "%d\n", instance->trip);
443}
444
445/* Device management */
446
Rene Herman16d75232008-06-24 19:38:56 +0200447#if defined(CONFIG_THERMAL_HWMON)
448
Zhang Ruie68b16a2008-04-21 16:07:52 +0800449/* hwmon sys I/F */
450#include <linux/hwmon.h>
Jean Delvare31f53962011-07-28 13:48:42 -0700451
452/* thermal zone devices with the same type share one hwmon device */
453struct thermal_hwmon_device {
454 char type[THERMAL_NAME_LENGTH];
455 struct device *device;
456 int count;
457 struct list_head tz_list;
458 struct list_head node;
459};
460
461struct thermal_hwmon_attr {
462 struct device_attribute attr;
463 char name[16];
464};
465
466/* one temperature input for each thermal zone */
467struct thermal_hwmon_temp {
468 struct list_head hwmon_node;
469 struct thermal_zone_device *tz;
470 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
471 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
472};
473
Zhang Ruie68b16a2008-04-21 16:07:52 +0800474static LIST_HEAD(thermal_hwmon_list);
475
476static ssize_t
477name_show(struct device *dev, struct device_attribute *attr, char *buf)
478{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700479 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800480 return sprintf(buf, "%s\n", hwmon->type);
481}
482static DEVICE_ATTR(name, 0444, name_show, NULL);
483
484static ssize_t
485temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
486{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000487 long temperature;
488 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800489 struct thermal_hwmon_attr *hwmon_attr
490 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700491 struct thermal_hwmon_temp *temp
492 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800493 temp_input);
Jean Delvare31f53962011-07-28 13:48:42 -0700494 struct thermal_zone_device *tz = temp->tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800495
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000496 ret = tz->ops->get_temp(tz, &temperature);
497
498 if (ret)
499 return ret;
500
501 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800502}
503
504static ssize_t
505temp_crit_show(struct device *dev, struct device_attribute *attr,
506 char *buf)
507{
508 struct thermal_hwmon_attr *hwmon_attr
509 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700510 struct thermal_hwmon_temp *temp
511 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800512 temp_crit);
Jean Delvare31f53962011-07-28 13:48:42 -0700513 struct thermal_zone_device *tz = temp->tz;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000514 long temperature;
515 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800516
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000517 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
518 if (ret)
519 return ret;
520
521 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800522}
523
524
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700525static struct thermal_hwmon_device *
526thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
527{
528 struct thermal_hwmon_device *hwmon;
529
530 mutex_lock(&thermal_list_lock);
531 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
532 if (!strcmp(hwmon->type, tz->type)) {
533 mutex_unlock(&thermal_list_lock);
534 return hwmon;
535 }
536 mutex_unlock(&thermal_list_lock);
537
538 return NULL;
539}
540
Jean Delvare31f53962011-07-28 13:48:42 -0700541/* Find the temperature input matching a given thermal zone */
542static struct thermal_hwmon_temp *
543thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
544 const struct thermal_zone_device *tz)
545{
546 struct thermal_hwmon_temp *temp;
547
548 mutex_lock(&thermal_list_lock);
549 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
550 if (temp->tz == tz) {
551 mutex_unlock(&thermal_list_lock);
552 return temp;
553 }
554 mutex_unlock(&thermal_list_lock);
555
556 return NULL;
557}
558
Zhang Ruie68b16a2008-04-21 16:07:52 +0800559static int
560thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
561{
562 struct thermal_hwmon_device *hwmon;
Jean Delvare31f53962011-07-28 13:48:42 -0700563 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800564 int new_hwmon_device = 1;
565 int result;
566
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700567 hwmon = thermal_hwmon_lookup_by_type(tz);
568 if (hwmon) {
569 new_hwmon_device = 0;
570 goto register_sys_interface;
571 }
Zhang Ruie68b16a2008-04-21 16:07:52 +0800572
573 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
574 if (!hwmon)
575 return -ENOMEM;
576
577 INIT_LIST_HEAD(&hwmon->tz_list);
578 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
579 hwmon->device = hwmon_device_register(NULL);
580 if (IS_ERR(hwmon->device)) {
581 result = PTR_ERR(hwmon->device);
582 goto free_mem;
583 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700584 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800585 result = device_create_file(hwmon->device, &dev_attr_name);
586 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530587 goto free_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800588
589 register_sys_interface:
Jean Delvare31f53962011-07-28 13:48:42 -0700590 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
591 if (!temp) {
592 result = -ENOMEM;
593 goto unregister_name;
594 }
595
596 temp->tz = tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800597 hwmon->count++;
598
Jean Delvare31f53962011-07-28 13:48:42 -0700599 snprintf(temp->temp_input.name, THERMAL_NAME_LENGTH,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800600 "temp%d_input", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700601 temp->temp_input.attr.attr.name = temp->temp_input.name;
602 temp->temp_input.attr.attr.mode = 0444;
603 temp->temp_input.attr.show = temp_input_show;
604 sysfs_attr_init(&temp->temp_input.attr.attr);
605 result = device_create_file(hwmon->device, &temp->temp_input.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800606 if (result)
Jean Delvare31f53962011-07-28 13:48:42 -0700607 goto free_temp_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800608
609 if (tz->ops->get_crit_temp) {
610 unsigned long temperature;
611 if (!tz->ops->get_crit_temp(tz, &temperature)) {
Jean Delvare31f53962011-07-28 13:48:42 -0700612 snprintf(temp->temp_crit.name, THERMAL_NAME_LENGTH,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800613 "temp%d_crit", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700614 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
615 temp->temp_crit.attr.attr.mode = 0444;
616 temp->temp_crit.attr.show = temp_crit_show;
617 sysfs_attr_init(&temp->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800618 result = device_create_file(hwmon->device,
Jean Delvare31f53962011-07-28 13:48:42 -0700619 &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800620 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530621 goto unregister_input;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800622 }
623 }
624
625 mutex_lock(&thermal_list_lock);
626 if (new_hwmon_device)
627 list_add_tail(&hwmon->node, &thermal_hwmon_list);
Jean Delvare31f53962011-07-28 13:48:42 -0700628 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800629 mutex_unlock(&thermal_list_lock);
630
631 return 0;
632
Durgadoss Rb299eb52011-03-03 04:30:13 +0530633 unregister_input:
Jean Delvare31f53962011-07-28 13:48:42 -0700634 device_remove_file(hwmon->device, &temp->temp_input.attr);
635 free_temp_mem:
636 kfree(temp);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530637 unregister_name:
Zhang Ruie68b16a2008-04-21 16:07:52 +0800638 if (new_hwmon_device) {
639 device_remove_file(hwmon->device, &dev_attr_name);
640 hwmon_device_unregister(hwmon->device);
641 }
642 free_mem:
643 if (new_hwmon_device)
644 kfree(hwmon);
645
646 return result;
647}
648
649static void
650thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
651{
Jean Delvare31f53962011-07-28 13:48:42 -0700652 struct thermal_hwmon_device *hwmon;
653 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800654
Jean Delvare31f53962011-07-28 13:48:42 -0700655 hwmon = thermal_hwmon_lookup_by_type(tz);
656 if (unlikely(!hwmon)) {
657 /* Should never happen... */
658 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
659 return;
660 }
661
662 temp = thermal_hwmon_lookup_temp(hwmon, tz);
663 if (unlikely(!temp)) {
664 /* Should never happen... */
665 dev_dbg(&tz->device, "temperature input lookup failed!\n");
666 return;
667 }
668
669 device_remove_file(hwmon->device, &temp->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530670 if (tz->ops->get_crit_temp)
Jean Delvare31f53962011-07-28 13:48:42 -0700671 device_remove_file(hwmon->device, &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800672
673 mutex_lock(&thermal_list_lock);
Jean Delvare31f53962011-07-28 13:48:42 -0700674 list_del(&temp->hwmon_node);
675 kfree(temp);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800676 if (!list_empty(&hwmon->tz_list)) {
677 mutex_unlock(&thermal_list_lock);
678 return;
679 }
680 list_del(&hwmon->node);
681 mutex_unlock(&thermal_list_lock);
682
683 device_remove_file(hwmon->device, &dev_attr_name);
684 hwmon_device_unregister(hwmon->device);
685 kfree(hwmon);
686}
687#else
688static int
689thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
690{
691 return 0;
692}
693
694static void
695thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
696{
697}
698#endif
699
Matthew Garrettb1569e92008-12-03 17:55:32 +0000700static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
701 int delay)
702{
703 cancel_delayed_work(&(tz->poll_queue));
704
705 if (!delay)
706 return;
707
708 if (delay > 1000)
Rafael J. Wysocki51e20d02011-11-06 14:21:38 +0100709 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
Matthew Garrettb1569e92008-12-03 17:55:32 +0000710 round_jiffies(msecs_to_jiffies(delay)));
711 else
Rafael J. Wysocki51e20d02011-11-06 14:21:38 +0100712 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
Matthew Garrettb1569e92008-12-03 17:55:32 +0000713 msecs_to_jiffies(delay));
714}
715
716static void thermal_zone_device_passive(struct thermal_zone_device *tz,
717 int temp, int trip_temp, int trip)
718{
Zhang Rui1b7ddb82012-06-27 09:51:12 +0800719 enum thermal_trend trend;
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800720 struct thermal_instance *instance;
Matthew Garrettb1569e92008-12-03 17:55:32 +0000721 struct thermal_cooling_device *cdev;
722 long state, max_state;
723
Zhang Rui1b7ddb82012-06-27 09:51:12 +0800724 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
Zhang Rui601f3d42012-06-27 09:54:33 +0800725 /*
726 * compare the current temperature and previous temperature
727 * to get the thermal trend, if no special requirement
728 */
729 if (tz->temperature > tz->last_temperature)
730 trend = THERMAL_TREND_RAISING;
731 else if (tz->temperature < tz->last_temperature)
732 trend = THERMAL_TREND_DROPPING;
733 else
734 trend = THERMAL_TREND_STABLE;
735 }
736
Matthew Garrettb1569e92008-12-03 17:55:32 +0000737 /*
738 * Above Trip?
739 * -----------
740 * Calculate the thermal trend (using the passive cooling equation)
741 * and modify the performance limit for all passive cooling devices
742 * accordingly. Note that we assume symmetry.
743 */
744 if (temp >= trip_temp) {
745 tz->passive = true;
746
Matthew Garrettb1569e92008-12-03 17:55:32 +0000747 /* Heating up? */
Zhang Rui1b7ddb82012-06-27 09:51:12 +0800748 if (trend == THERMAL_TREND_RAISING) {
Zhang Rui2d374132012-06-27 10:09:00 +0800749 list_for_each_entry(instance, &tz->thermal_instances,
Zhang Ruicddf31b2012-06-27 10:09:36 +0800750 tz_node) {
Matthew Garrettb1569e92008-12-03 17:55:32 +0000751 if (instance->trip != trip)
752 continue;
753 cdev = instance->cdev;
754 cdev->ops->get_cur_state(cdev, &state);
755 cdev->ops->get_max_state(cdev, &max_state);
756 if (state++ < max_state)
757 cdev->ops->set_cur_state(cdev, state);
758 }
Zhang Rui1b7ddb82012-06-27 09:51:12 +0800759 } else if (trend == THERMAL_TREND_DROPPING) { /* Cooling off? */
Zhang Rui2d374132012-06-27 10:09:00 +0800760 list_for_each_entry(instance, &tz->thermal_instances,
Zhang Ruicddf31b2012-06-27 10:09:36 +0800761 tz_node) {
Matthew Garrettb1569e92008-12-03 17:55:32 +0000762 if (instance->trip != trip)
763 continue;
764 cdev = instance->cdev;
765 cdev->ops->get_cur_state(cdev, &state);
766 cdev->ops->get_max_state(cdev, &max_state);
767 if (state > 0)
768 cdev->ops->set_cur_state(cdev, --state);
769 }
770 }
771 return;
772 }
773
774 /*
775 * Below Trip?
776 * -----------
777 * Implement passive cooling hysteresis to slowly increase performance
778 * and avoid thrashing around the passive trip point. Note that we
779 * assume symmetry.
780 */
Zhang Ruicddf31b2012-06-27 10:09:36 +0800781 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
Matthew Garrettb1569e92008-12-03 17:55:32 +0000782 if (instance->trip != trip)
783 continue;
784 cdev = instance->cdev;
785 cdev->ops->get_cur_state(cdev, &state);
786 cdev->ops->get_max_state(cdev, &max_state);
787 if (state > 0)
788 cdev->ops->set_cur_state(cdev, --state);
789 if (state == 0)
790 tz->passive = false;
791 }
792}
793
794static void thermal_zone_device_check(struct work_struct *work)
795{
796 struct thermal_zone_device *tz = container_of(work, struct
797 thermal_zone_device,
798 poll_queue.work);
799 thermal_zone_device_update(tz);
800}
Zhang Ruie68b16a2008-04-21 16:07:52 +0800801
Zhang Rui203d3d42008-01-17 15:51:08 +0800802/**
803 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800804 * @tz: thermal zone device
805 * @trip: indicates which trip point the cooling devices is
806 * associated with in this thermal zone.
807 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500808 *
809 * This function is usually called in the thermal zone device .bind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800810 */
811int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
812 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800813 struct thermal_cooling_device *cdev,
814 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +0800815{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800816 struct thermal_instance *dev;
817 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500818 struct thermal_zone_device *pos1;
819 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800820 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800821 int result;
822
Len Brown543a9562008-02-07 16:55:08 -0500823 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800824 return -EINVAL;
825
Thomas Sujithc7516702008-02-15 00:58:50 -0500826 list_for_each_entry(pos1, &thermal_tz_list, node) {
827 if (pos1 == tz)
828 break;
829 }
830 list_for_each_entry(pos2, &thermal_cdev_list, node) {
831 if (pos2 == cdev)
832 break;
833 }
834
835 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800836 return -EINVAL;
837
Zhang Rui9d998422012-06-26 16:35:57 +0800838 cdev->ops->get_max_state(cdev, &max_state);
839
840 /* lower default 0, upper default max_state */
841 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
842 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
843
844 if (lower > upper || upper > max_state)
845 return -EINVAL;
846
Zhang Rui203d3d42008-01-17 15:51:08 +0800847 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800848 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800849 if (!dev)
850 return -ENOMEM;
851 dev->tz = tz;
852 dev->cdev = cdev;
853 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800854 dev->upper = upper;
855 dev->lower = lower;
Zhang Rui74051ba2012-06-26 16:27:22 +0800856
Zhang Rui203d3d42008-01-17 15:51:08 +0800857 result = get_idr(&tz->idr, &tz->lock, &dev->id);
858 if (result)
859 goto free_mem;
860
861 sprintf(dev->name, "cdev%d", dev->id);
862 result =
863 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
864 if (result)
865 goto release_idr;
866
867 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700868 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800869 dev->attr.attr.name = dev->attr_name;
870 dev->attr.attr.mode = 0444;
871 dev->attr.show = thermal_cooling_device_trip_point_show;
872 result = device_create_file(&tz->device, &dev->attr);
873 if (result)
874 goto remove_symbol_link;
875
876 mutex_lock(&tz->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800877 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +0800878 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
879 result = -EEXIST;
880 break;
881 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800882 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800883 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800884 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
885 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800886 mutex_unlock(&tz->lock);
887
888 if (!result)
889 return 0;
890
891 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -0700892remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800893 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -0700894release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +0800895 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -0700896free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800897 kfree(dev);
898 return result;
899}
900EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
901
902/**
903 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800904 * @tz: thermal zone device
905 * @trip: indicates which trip point the cooling devices is
906 * associated with in this thermal zone.
907 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500908 *
909 * This function is usually called in the thermal zone device .unbind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800910 */
911int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
912 int trip,
913 struct thermal_cooling_device *cdev)
914{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800915 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +0800916
917 mutex_lock(&tz->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800918 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -0500919 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800920 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800921 list_del(&pos->cdev_node);
Zhang Rui203d3d42008-01-17 15:51:08 +0800922 mutex_unlock(&tz->lock);
923 goto unbind;
924 }
925 }
926 mutex_unlock(&tz->lock);
927
928 return -ENODEV;
929
Joe Perchescaca8b82012-03-21 12:55:02 -0700930unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +0800931 device_remove_file(&tz->device, &pos->attr);
932 sysfs_remove_link(&tz->device.kobj, pos->name);
933 release_idr(&tz->idr, &tz->lock, pos->id);
934 kfree(pos);
935 return 0;
936}
937EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
938
939static void thermal_release(struct device *dev)
940{
941 struct thermal_zone_device *tz;
942 struct thermal_cooling_device *cdev;
943
Joe Perchescaca8b82012-03-21 12:55:02 -0700944 if (!strncmp(dev_name(dev), "thermal_zone",
945 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800946 tz = to_thermal_zone(dev);
947 kfree(tz);
948 } else {
949 cdev = to_cooling_device(dev);
950 kfree(cdev);
951 }
952}
953
954static struct class thermal_class = {
955 .name = "thermal",
956 .dev_release = thermal_release,
957};
958
959/**
960 * thermal_cooling_device_register - register a new thermal cooling device
961 * @type: the thermal cooling device type.
962 * @devdata: device private data.
963 * @ops: standard thermal cooling devices callbacks.
964 */
Joe Perchescaca8b82012-03-21 12:55:02 -0700965struct thermal_cooling_device *
966thermal_cooling_device_register(char *type, void *devdata,
967 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800968{
969 struct thermal_cooling_device *cdev;
970 struct thermal_zone_device *pos;
971 int result;
972
973 if (strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500974 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800975
976 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -0500977 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500978 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800979
980 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
981 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500982 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +0800983
984 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
985 if (result) {
986 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500987 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800988 }
989
990 strcpy(cdev->type, type);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800991 INIT_LIST_HEAD(&cdev->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +0800992 cdev->ops = ops;
993 cdev->device.class = &thermal_class;
994 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -0800995 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800996 result = device_register(&cdev->device);
997 if (result) {
998 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
999 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001000 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001001 }
1002
1003 /* sys I/F */
1004 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001005 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001006 if (result)
1007 goto unregister;
1008 }
1009
1010 result = device_create_file(&cdev->device, &dev_attr_max_state);
1011 if (result)
1012 goto unregister;
1013
1014 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1015 if (result)
1016 goto unregister;
1017
1018 mutex_lock(&thermal_list_lock);
1019 list_add(&cdev->node, &thermal_cdev_list);
1020 list_for_each_entry(pos, &thermal_tz_list, node) {
1021 if (!pos->ops->bind)
1022 continue;
1023 result = pos->ops->bind(pos, cdev);
1024 if (result)
1025 break;
1026
1027 }
1028 mutex_unlock(&thermal_list_lock);
1029
1030 if (!result)
1031 return cdev;
1032
Joe Perchescaca8b82012-03-21 12:55:02 -07001033unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001034 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1035 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001036 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001037}
1038EXPORT_SYMBOL(thermal_cooling_device_register);
1039
1040/**
1041 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001042 * @cdev: the thermal cooling device to remove.
1043 *
1044 * thermal_cooling_device_unregister() must be called when the device is no
1045 * longer needed.
1046 */
1047void thermal_cooling_device_unregister(struct
1048 thermal_cooling_device
1049 *cdev)
1050{
1051 struct thermal_zone_device *tz;
1052 struct thermal_cooling_device *pos = NULL;
1053
1054 if (!cdev)
1055 return;
1056
1057 mutex_lock(&thermal_list_lock);
1058 list_for_each_entry(pos, &thermal_cdev_list, node)
1059 if (pos == cdev)
1060 break;
1061 if (pos != cdev) {
1062 /* thermal cooling device not found */
1063 mutex_unlock(&thermal_list_lock);
1064 return;
1065 }
1066 list_del(&cdev->node);
1067 list_for_each_entry(tz, &thermal_tz_list, node) {
1068 if (!tz->ops->unbind)
1069 continue;
1070 tz->ops->unbind(tz, cdev);
1071 }
1072 mutex_unlock(&thermal_list_lock);
1073 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001074 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001075 device_remove_file(&cdev->device, &dev_attr_max_state);
1076 device_remove_file(&cdev->device, &dev_attr_cur_state);
1077
1078 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1079 device_unregister(&cdev->device);
1080 return;
1081}
1082EXPORT_SYMBOL(thermal_cooling_device_unregister);
1083
Zhang Rui4ae46be2012-06-27 10:05:39 +08001084/*
1085 * Cooling algorithm for active trip points
1086 *
1087 * 1. if the temperature is higher than a trip point,
1088 * a. if the trend is THERMAL_TREND_RAISING, use higher cooling
1089 * state for this trip point
1090 * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
1091 * state for this trip point
1092 *
1093 * 2. if the temperature is lower than a trip point, use lower
1094 * cooling state for this trip point
1095 *
1096 * Note that this behaves the same as the previous passive cooling
1097 * algorithm.
1098 */
1099
1100static void thermal_zone_trip_update(struct thermal_zone_device *tz,
1101 int trip, long temp)
1102{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001103 struct thermal_instance *instance;
Zhang Rui4ae46be2012-06-27 10:05:39 +08001104 struct thermal_cooling_device *cdev = NULL;
1105 unsigned long cur_state, max_state;
1106 long trip_temp;
1107 enum thermal_trend trend;
1108
1109 tz->ops->get_trip_temp(tz, trip, &trip_temp);
1110
1111 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
1112 /*
1113 * compare the current temperature and previous temperature
1114 * to get the thermal trend, if no special requirement
1115 */
1116 if (tz->temperature > tz->last_temperature)
1117 trend = THERMAL_TREND_RAISING;
1118 else if (tz->temperature < tz->last_temperature)
1119 trend = THERMAL_TREND_DROPPING;
1120 else
1121 trend = THERMAL_TREND_STABLE;
1122 }
1123
1124 if (temp >= trip_temp) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001125 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
Zhang Rui4ae46be2012-06-27 10:05:39 +08001126 if (instance->trip != trip)
1127 continue;
1128
1129 cdev = instance->cdev;
1130
1131 cdev->ops->get_cur_state(cdev, &cur_state);
1132 cdev->ops->get_max_state(cdev, &max_state);
1133
1134 if (trend == THERMAL_TREND_RAISING) {
1135 cur_state = cur_state < instance->upper ?
1136 (cur_state + 1) : instance->upper;
1137 } else if (trend == THERMAL_TREND_DROPPING) {
1138 cur_state = cur_state > instance->lower ?
1139 (cur_state - 1) : instance->lower;
1140 }
1141 cdev->ops->set_cur_state(cdev, cur_state);
1142 }
1143 } else { /* below trip */
Zhang Ruicddf31b2012-06-27 10:09:36 +08001144 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
Zhang Rui4ae46be2012-06-27 10:05:39 +08001145 if (instance->trip != trip)
1146 continue;
1147
1148 cdev = instance->cdev;
1149 cdev->ops->get_cur_state(cdev, &cur_state);
1150
1151 cur_state = cur_state > instance->lower ?
1152 (cur_state - 1) : instance->lower;
1153 cdev->ops->set_cur_state(cdev, cur_state);
1154 }
1155 }
1156
1157 return;
1158}
Zhang Rui203d3d42008-01-17 15:51:08 +08001159/**
Matthew Garrettb1569e92008-12-03 17:55:32 +00001160 * thermal_zone_device_update - force an update of a thermal zone's state
1161 * @ttz: the thermal zone to update
1162 */
1163
1164void thermal_zone_device_update(struct thermal_zone_device *tz)
1165{
1166 int count, ret = 0;
1167 long temp, trip_temp;
1168 enum thermal_trip_type trip_type;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001169
1170 mutex_lock(&tz->lock);
1171
Michael Brunner0d288162009-08-26 14:29:25 -07001172 if (tz->ops->get_temp(tz, &temp)) {
1173 /* get_temp failed - retry it later */
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001174 pr_warn("failed to read out thermal zone %d\n", tz->id);
Michael Brunner0d288162009-08-26 14:29:25 -07001175 goto leave;
1176 }
Matthew Garrettb1569e92008-12-03 17:55:32 +00001177
Zhang Rui601f3d42012-06-27 09:54:33 +08001178 tz->last_temperature = tz->temperature;
1179 tz->temperature = temp;
1180
Matthew Garrettb1569e92008-12-03 17:55:32 +00001181 for (count = 0; count < tz->trips; count++) {
1182 tz->ops->get_trip_type(tz, count, &trip_type);
1183 tz->ops->get_trip_temp(tz, count, &trip_temp);
1184
1185 switch (trip_type) {
1186 case THERMAL_TRIP_CRITICAL:
Vladimir Zajac29321352009-05-06 19:34:21 +02001187 if (temp >= trip_temp) {
Matthew Garrettb1569e92008-12-03 17:55:32 +00001188 if (tz->ops->notify)
1189 ret = tz->ops->notify(tz, count,
1190 trip_type);
1191 if (!ret) {
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001192 pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1193 temp/1000);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001194 orderly_poweroff(true);
1195 }
1196 }
1197 break;
1198 case THERMAL_TRIP_HOT:
Vladimir Zajac29321352009-05-06 19:34:21 +02001199 if (temp >= trip_temp)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001200 if (tz->ops->notify)
1201 tz->ops->notify(tz, count, trip_type);
1202 break;
1203 case THERMAL_TRIP_ACTIVE:
Zhang Rui4ae46be2012-06-27 10:05:39 +08001204 thermal_zone_trip_update(tz, count, temp);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001205 break;
1206 case THERMAL_TRIP_PASSIVE:
Vladimir Zajac29321352009-05-06 19:34:21 +02001207 if (temp >= trip_temp || tz->passive)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001208 thermal_zone_device_passive(tz, temp,
1209 trip_temp, count);
1210 break;
1211 }
1212 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001213
1214 if (tz->forced_passive)
1215 thermal_zone_device_passive(tz, temp, tz->forced_passive,
1216 THERMAL_TRIPS_NONE);
1217
Joe Perchescaca8b82012-03-21 12:55:02 -07001218leave:
Matthew Garrettb1569e92008-12-03 17:55:32 +00001219 if (tz->passive)
1220 thermal_zone_device_set_polling(tz, tz->passive_delay);
1221 else if (tz->polling_delay)
1222 thermal_zone_device_set_polling(tz, tz->polling_delay);
Frans Pop3767cb52009-10-26 08:39:04 +01001223 else
1224 thermal_zone_device_set_polling(tz, 0);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001225 mutex_unlock(&tz->lock);
1226}
1227EXPORT_SYMBOL(thermal_zone_device_update);
1228
1229/**
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001230 * create_trip_attrs - create attributes for trip points
1231 * @tz: the thermal zone device
1232 * @mask: Writeable trip point bitmap.
1233 */
1234static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1235{
1236 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001237 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001238
Durgadoss R27365a62012-07-25 10:10:59 +08001239 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001240 if (!tz->trip_type_attrs)
1241 return -ENOMEM;
1242
Durgadoss R27365a62012-07-25 10:10:59 +08001243 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001244 if (!tz->trip_temp_attrs) {
1245 kfree(tz->trip_type_attrs);
1246 return -ENOMEM;
1247 }
1248
Durgadoss R27365a62012-07-25 10:10:59 +08001249 if (tz->ops->get_trip_hyst) {
1250 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1251 if (!tz->trip_hyst_attrs) {
1252 kfree(tz->trip_type_attrs);
1253 kfree(tz->trip_temp_attrs);
1254 return -ENOMEM;
1255 }
1256 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001257
Durgadoss R27365a62012-07-25 10:10:59 +08001258
1259 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001260 /* create trip type attribute */
1261 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1262 "trip_point_%d_type", indx);
1263
1264 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1265 tz->trip_type_attrs[indx].attr.attr.name =
1266 tz->trip_type_attrs[indx].name;
1267 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1268 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1269
1270 device_create_file(&tz->device,
1271 &tz->trip_type_attrs[indx].attr);
1272
1273 /* create trip temp attribute */
1274 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1275 "trip_point_%d_temp", indx);
1276
1277 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1278 tz->trip_temp_attrs[indx].attr.attr.name =
1279 tz->trip_temp_attrs[indx].name;
1280 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1281 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1282 if (mask & (1 << indx)) {
1283 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1284 tz->trip_temp_attrs[indx].attr.store =
1285 trip_point_temp_store;
1286 }
1287
1288 device_create_file(&tz->device,
1289 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001290
1291 /* create Optional trip hyst attribute */
1292 if (!tz->ops->get_trip_hyst)
1293 continue;
1294 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1295 "trip_point_%d_hyst", indx);
1296
1297 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1298 tz->trip_hyst_attrs[indx].attr.attr.name =
1299 tz->trip_hyst_attrs[indx].name;
1300 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1301 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1302 if (tz->ops->set_trip_hyst) {
1303 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1304 tz->trip_hyst_attrs[indx].attr.store =
1305 trip_point_hyst_store;
1306 }
1307
1308 device_create_file(&tz->device,
1309 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001310 }
1311 return 0;
1312}
1313
1314static void remove_trip_attrs(struct thermal_zone_device *tz)
1315{
1316 int indx;
1317
1318 for (indx = 0; indx < tz->trips; indx++) {
1319 device_remove_file(&tz->device,
1320 &tz->trip_type_attrs[indx].attr);
1321 device_remove_file(&tz->device,
1322 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001323 if (tz->ops->get_trip_hyst)
1324 device_remove_file(&tz->device,
1325 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001326 }
1327 kfree(tz->trip_type_attrs);
1328 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001329 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001330}
1331
1332/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001333 * thermal_zone_device_register - register a new thermal zone device
1334 * @type: the thermal zone device type
1335 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001336 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001337 * @devdata: private device data
1338 * @ops: standard thermal zone device callbacks
Matthew Garrettb1569e92008-12-03 17:55:32 +00001339 * @passive_delay: number of milliseconds to wait between polls when
1340 * performing passive cooling
1341 * @polling_delay: number of milliseconds to wait between polls when checking
1342 * whether trip points have been crossed (0 for interrupt
1343 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001344 *
1345 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001346 * longer needed. The passive cooling depends on the .get_trend() return value.
Zhang Rui203d3d42008-01-17 15:51:08 +08001347 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001348struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001349 int trips, int mask, void *devdata,
Alan Cox5b275ce2010-11-11 15:27:29 +00001350 const struct thermal_zone_device_ops *ops,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001351 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001352{
1353 struct thermal_zone_device *tz;
1354 struct thermal_cooling_device *pos;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001355 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001356 int result;
1357 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001358 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001359
1360 if (strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001361 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001362
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001363 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001364 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001365
1366 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001367 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001368
1369 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1370 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001371 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001372
Zhang Rui2d374132012-06-27 10:09:00 +08001373 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001374 idr_init(&tz->idr);
1375 mutex_init(&tz->lock);
1376 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1377 if (result) {
1378 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001379 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001380 }
1381
1382 strcpy(tz->type, type);
1383 tz->ops = ops;
1384 tz->device.class = &thermal_class;
1385 tz->devdata = devdata;
1386 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001387 tz->passive_delay = passive_delay;
1388 tz->polling_delay = polling_delay;
1389
Kay Sievers354655e2009-01-06 10:44:37 -08001390 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001391 result = device_register(&tz->device);
1392 if (result) {
1393 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1394 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001395 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001396 }
1397
1398 /* sys I/F */
1399 if (type) {
1400 result = device_create_file(&tz->device, &dev_attr_type);
1401 if (result)
1402 goto unregister;
1403 }
1404
1405 result = device_create_file(&tz->device, &dev_attr_temp);
1406 if (result)
1407 goto unregister;
1408
1409 if (ops->get_mode) {
1410 result = device_create_file(&tz->device, &dev_attr_mode);
1411 if (result)
1412 goto unregister;
1413 }
1414
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001415 result = create_trip_attrs(tz, mask);
1416 if (result)
1417 goto unregister;
1418
Zhang Rui203d3d42008-01-17 15:51:08 +08001419 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001420 tz->ops->get_trip_type(tz, count, &trip_type);
1421 if (trip_type == THERMAL_TRIP_PASSIVE)
1422 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001423 }
1424
Matthew Garrett03a971a2008-12-03 18:00:38 +00001425 if (!passive)
1426 result = device_create_file(&tz->device,
1427 &dev_attr_passive);
1428
1429 if (result)
1430 goto unregister;
1431
Zhang Ruie68b16a2008-04-21 16:07:52 +08001432 result = thermal_add_hwmon_sysfs(tz);
1433 if (result)
1434 goto unregister;
1435
Zhang Rui203d3d42008-01-17 15:51:08 +08001436 mutex_lock(&thermal_list_lock);
1437 list_add_tail(&tz->node, &thermal_tz_list);
1438 if (ops->bind)
1439 list_for_each_entry(pos, &thermal_cdev_list, node) {
Len Brown543a9562008-02-07 16:55:08 -05001440 result = ops->bind(tz, pos);
1441 if (result)
1442 break;
Zhang Rui203d3d42008-01-17 15:51:08 +08001443 }
1444 mutex_unlock(&thermal_list_lock);
1445
Matthew Garrettb1569e92008-12-03 17:55:32 +00001446 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1447
1448 thermal_zone_device_update(tz);
1449
Zhang Rui203d3d42008-01-17 15:51:08 +08001450 if (!result)
1451 return tz;
1452
Joe Perchescaca8b82012-03-21 12:55:02 -07001453unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001454 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1455 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001456 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001457}
1458EXPORT_SYMBOL(thermal_zone_device_register);
1459
1460/**
1461 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001462 * @tz: the thermal zone device to remove
1463 */
1464void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1465{
1466 struct thermal_cooling_device *cdev;
1467 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001468
1469 if (!tz)
1470 return;
1471
1472 mutex_lock(&thermal_list_lock);
1473 list_for_each_entry(pos, &thermal_tz_list, node)
1474 if (pos == tz)
1475 break;
1476 if (pos != tz) {
1477 /* thermal zone device not found */
1478 mutex_unlock(&thermal_list_lock);
1479 return;
1480 }
1481 list_del(&tz->node);
1482 if (tz->ops->unbind)
1483 list_for_each_entry(cdev, &thermal_cdev_list, node)
1484 tz->ops->unbind(tz, cdev);
1485 mutex_unlock(&thermal_list_lock);
1486
Matthew Garrettb1569e92008-12-03 17:55:32 +00001487 thermal_zone_device_set_polling(tz, 0);
1488
Zhang Rui203d3d42008-01-17 15:51:08 +08001489 if (tz->type[0])
1490 device_remove_file(&tz->device, &dev_attr_type);
1491 device_remove_file(&tz->device, &dev_attr_temp);
1492 if (tz->ops->get_mode)
1493 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001494 remove_trip_attrs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001495
Zhang Ruie68b16a2008-04-21 16:07:52 +08001496 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001497 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1498 idr_destroy(&tz->idr);
1499 mutex_destroy(&tz->lock);
1500 device_unregister(&tz->device);
1501 return;
1502}
Zhang Rui203d3d42008-01-17 15:51:08 +08001503EXPORT_SYMBOL(thermal_zone_device_unregister);
1504
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001505#ifdef CONFIG_NET
1506static struct genl_family thermal_event_genl_family = {
1507 .id = GENL_ID_GENERATE,
1508 .name = THERMAL_GENL_FAMILY_NAME,
1509 .version = THERMAL_GENL_VERSION,
1510 .maxattr = THERMAL_GENL_ATTR_MAX,
1511};
1512
1513static struct genl_multicast_group thermal_event_mcgrp = {
1514 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1515};
1516
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001517int thermal_generate_netlink_event(u32 orig, enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301518{
1519 struct sk_buff *skb;
1520 struct nlattr *attr;
1521 struct thermal_genl_event *thermal_event;
1522 void *msg_header;
1523 int size;
1524 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001525 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301526
1527 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001528 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1529 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301530
1531 skb = genlmsg_new(size, GFP_ATOMIC);
1532 if (!skb)
1533 return -ENOMEM;
1534
1535 /* add the genetlink message header */
1536 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1537 &thermal_event_genl_family, 0,
1538 THERMAL_GENL_CMD_EVENT);
1539 if (!msg_header) {
1540 nlmsg_free(skb);
1541 return -ENOMEM;
1542 }
1543
1544 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001545 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1546 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301547
1548 if (!attr) {
1549 nlmsg_free(skb);
1550 return -EINVAL;
1551 }
1552
1553 thermal_event = nla_data(attr);
1554 if (!thermal_event) {
1555 nlmsg_free(skb);
1556 return -EINVAL;
1557 }
1558
1559 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1560
1561 thermal_event->orig = orig;
1562 thermal_event->event = event;
1563
1564 /* send multicast genetlink message */
1565 result = genlmsg_end(skb, msg_header);
1566 if (result < 0) {
1567 nlmsg_free(skb);
1568 return result;
1569 }
1570
1571 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1572 if (result)
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001573 pr_info("failed to send netlink event:%d\n", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301574
1575 return result;
1576}
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001577EXPORT_SYMBOL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301578
1579static int genetlink_init(void)
1580{
1581 int result;
1582
1583 result = genl_register_family(&thermal_event_genl_family);
1584 if (result)
1585 return result;
1586
1587 result = genl_register_mc_group(&thermal_event_genl_family,
1588 &thermal_event_mcgrp);
1589 if (result)
1590 genl_unregister_family(&thermal_event_genl_family);
1591 return result;
1592}
1593
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001594static void genetlink_exit(void)
1595{
1596 genl_unregister_family(&thermal_event_genl_family);
1597}
1598#else /* !CONFIG_NET */
1599static inline int genetlink_init(void) { return 0; }
1600static inline void genetlink_exit(void) {}
1601#endif /* !CONFIG_NET */
1602
Zhang Rui203d3d42008-01-17 15:51:08 +08001603static int __init thermal_init(void)
1604{
1605 int result = 0;
1606
1607 result = class_register(&thermal_class);
1608 if (result) {
1609 idr_destroy(&thermal_tz_idr);
1610 idr_destroy(&thermal_cdev_idr);
1611 mutex_destroy(&thermal_idr_lock);
1612 mutex_destroy(&thermal_list_lock);
1613 }
R.Durgadoss4cb18722010-10-27 03:33:29 +05301614 result = genetlink_init();
Zhang Rui203d3d42008-01-17 15:51:08 +08001615 return result;
1616}
1617
1618static void __exit thermal_exit(void)
1619{
1620 class_unregister(&thermal_class);
1621 idr_destroy(&thermal_tz_idr);
1622 idr_destroy(&thermal_cdev_idr);
1623 mutex_destroy(&thermal_idr_lock);
1624 mutex_destroy(&thermal_list_lock);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301625 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001626}
1627
R.Durgadoss4cb18722010-10-27 03:33:29 +05301628fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001629module_exit(thermal_exit);