blob: bbc834625f7f9199c26b50e3ab28c78ee78dfd4d [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
Durgadoss R71350db2012-09-18 11:04:53 +053040#include "thermal_core.h"
41
Zhang Rui63c4ec92008-04-21 16:07:13 +080042MODULE_AUTHOR("Zhang Rui");
Zhang Rui203d3d42008-01-17 15:51:08 +080043MODULE_DESCRIPTION("Generic thermal management sysfs support");
44MODULE_LICENSE("GPL");
45
Zhang Rui203d3d42008-01-17 15:51:08 +080046static DEFINE_IDR(thermal_tz_idr);
47static DEFINE_IDR(thermal_cdev_idr);
48static DEFINE_MUTEX(thermal_idr_lock);
49
50static LIST_HEAD(thermal_tz_list);
51static LIST_HEAD(thermal_cdev_list);
52static DEFINE_MUTEX(thermal_list_lock);
53
54static int get_idr(struct idr *idr, struct mutex *lock, int *id)
55{
56 int err;
57
Joe Perchescaca8b82012-03-21 12:55:02 -070058again:
Zhang Rui203d3d42008-01-17 15:51:08 +080059 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
60 return -ENOMEM;
61
62 if (lock)
63 mutex_lock(lock);
64 err = idr_get_new(idr, NULL, id);
65 if (lock)
66 mutex_unlock(lock);
67 if (unlikely(err == -EAGAIN))
68 goto again;
69 else if (unlikely(err))
70 return err;
71
Fengguang Wu125c4c72012-10-04 17:13:15 -070072 *id = *id & MAX_IDR_MASK;
Zhang Rui203d3d42008-01-17 15:51:08 +080073 return 0;
74}
75
76static void release_idr(struct idr *idr, struct mutex *lock, int id)
77{
78 if (lock)
79 mutex_lock(lock);
80 idr_remove(idr, id);
81 if (lock)
82 mutex_unlock(lock);
83}
84
85/* sys I/F for thermal zone */
86
87#define to_thermal_zone(_dev) \
88 container_of(_dev, struct thermal_zone_device, device)
89
90static ssize_t
91type_show(struct device *dev, struct device_attribute *attr, char *buf)
92{
93 struct thermal_zone_device *tz = to_thermal_zone(dev);
94
95 return sprintf(buf, "%s\n", tz->type);
96}
97
98static ssize_t
99temp_show(struct device *dev, struct device_attribute *attr, char *buf)
100{
101 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000102 long temperature;
103 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800104
105 if (!tz->ops->get_temp)
106 return -EPERM;
107
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000108 ret = tz->ops->get_temp(tz, &temperature);
109
110 if (ret)
111 return ret;
112
113 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800114}
115
116static ssize_t
117mode_show(struct device *dev, struct device_attribute *attr, char *buf)
118{
119 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000120 enum thermal_device_mode mode;
121 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800122
123 if (!tz->ops->get_mode)
124 return -EPERM;
125
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000126 result = tz->ops->get_mode(tz, &mode);
127 if (result)
128 return result;
129
130 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
131 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800132}
133
134static ssize_t
135mode_store(struct device *dev, struct device_attribute *attr,
136 const char *buf, size_t count)
137{
138 struct thermal_zone_device *tz = to_thermal_zone(dev);
139 int result;
140
141 if (!tz->ops->set_mode)
142 return -EPERM;
143
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530144 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000145 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530146 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000147 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
148 else
149 result = -EINVAL;
150
Zhang Rui203d3d42008-01-17 15:51:08 +0800151 if (result)
152 return result;
153
154 return count;
155}
156
157static ssize_t
158trip_point_type_show(struct device *dev, struct device_attribute *attr,
159 char *buf)
160{
161 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000162 enum thermal_trip_type type;
163 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800164
165 if (!tz->ops->get_trip_type)
166 return -EPERM;
167
168 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
169 return -EINVAL;
170
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000171 result = tz->ops->get_trip_type(tz, trip, &type);
172 if (result)
173 return result;
174
175 switch (type) {
176 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300177 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000178 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300179 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000180 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300181 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000182 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300183 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000184 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300185 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000186 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800187}
188
189static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800190trip_point_temp_store(struct device *dev, struct device_attribute *attr,
191 const char *buf, size_t count)
192{
193 struct thermal_zone_device *tz = to_thermal_zone(dev);
194 int trip, ret;
195 unsigned long temperature;
196
197 if (!tz->ops->set_trip_temp)
198 return -EPERM;
199
200 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
201 return -EINVAL;
202
203 if (kstrtoul(buf, 10, &temperature))
204 return -EINVAL;
205
206 ret = tz->ops->set_trip_temp(tz, trip, temperature);
207
208 return ret ? ret : count;
209}
210
211static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800212trip_point_temp_show(struct device *dev, struct device_attribute *attr,
213 char *buf)
214{
215 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000216 int trip, ret;
217 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800218
219 if (!tz->ops->get_trip_temp)
220 return -EPERM;
221
222 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
223 return -EINVAL;
224
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000225 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
226
227 if (ret)
228 return ret;
229
230 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800231}
232
Matthew Garrett03a971a2008-12-03 18:00:38 +0000233static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800234trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
235 const char *buf, size_t count)
236{
237 struct thermal_zone_device *tz = to_thermal_zone(dev);
238 int trip, ret;
239 unsigned long temperature;
240
241 if (!tz->ops->set_trip_hyst)
242 return -EPERM;
243
244 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
245 return -EINVAL;
246
247 if (kstrtoul(buf, 10, &temperature))
248 return -EINVAL;
249
250 /*
251 * We are not doing any check on the 'temperature' value
252 * here. The driver implementing 'set_trip_hyst' has to
253 * take care of this.
254 */
255 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
256
257 return ret ? ret : count;
258}
259
260static ssize_t
261trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
262 char *buf)
263{
264 struct thermal_zone_device *tz = to_thermal_zone(dev);
265 int trip, ret;
266 unsigned long temperature;
267
268 if (!tz->ops->get_trip_hyst)
269 return -EPERM;
270
271 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
272 return -EINVAL;
273
274 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
275
276 return ret ? ret : sprintf(buf, "%ld\n", temperature);
277}
278
279static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000280passive_store(struct device *dev, struct device_attribute *attr,
281 const char *buf, size_t count)
282{
283 struct thermal_zone_device *tz = to_thermal_zone(dev);
284 struct thermal_cooling_device *cdev = NULL;
285 int state;
286
287 if (!sscanf(buf, "%d\n", &state))
288 return -EINVAL;
289
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100290 /* sanity check: values below 1000 millicelcius don't make sense
291 * and can cause the system to go into a thermal heart attack
292 */
293 if (state && state < 1000)
294 return -EINVAL;
295
Matthew Garrett03a971a2008-12-03 18:00:38 +0000296 if (state && !tz->forced_passive) {
297 mutex_lock(&thermal_list_lock);
298 list_for_each_entry(cdev, &thermal_cdev_list, node) {
299 if (!strncmp("Processor", cdev->type,
300 sizeof("Processor")))
301 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800302 THERMAL_TRIPS_NONE, cdev,
303 THERMAL_NO_LIMIT,
304 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000305 }
306 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100307 if (!tz->passive_delay)
308 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000309 } else if (!state && tz->forced_passive) {
310 mutex_lock(&thermal_list_lock);
311 list_for_each_entry(cdev, &thermal_cdev_list, node) {
312 if (!strncmp("Processor", cdev->type,
313 sizeof("Processor")))
314 thermal_zone_unbind_cooling_device(tz,
315 THERMAL_TRIPS_NONE,
316 cdev);
317 }
318 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100319 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000320 }
321
Matthew Garrett03a971a2008-12-03 18:00:38 +0000322 tz->forced_passive = state;
323
324 thermal_zone_device_update(tz);
325
326 return count;
327}
328
329static ssize_t
330passive_show(struct device *dev, struct device_attribute *attr,
331 char *buf)
332{
333 struct thermal_zone_device *tz = to_thermal_zone(dev);
334
335 return sprintf(buf, "%d\n", tz->forced_passive);
336}
337
Zhang Rui203d3d42008-01-17 15:51:08 +0800338static DEVICE_ATTR(type, 0444, type_show, NULL);
339static DEVICE_ATTR(temp, 0444, temp_show, NULL);
340static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700341static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800342
Zhang Rui203d3d42008-01-17 15:51:08 +0800343/* sys I/F for cooling device */
344#define to_cooling_device(_dev) \
345 container_of(_dev, struct thermal_cooling_device, device)
346
347static ssize_t
348thermal_cooling_device_type_show(struct device *dev,
349 struct device_attribute *attr, char *buf)
350{
351 struct thermal_cooling_device *cdev = to_cooling_device(dev);
352
353 return sprintf(buf, "%s\n", cdev->type);
354}
355
356static ssize_t
357thermal_cooling_device_max_state_show(struct device *dev,
358 struct device_attribute *attr, char *buf)
359{
360 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000361 unsigned long state;
362 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800363
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000364 ret = cdev->ops->get_max_state(cdev, &state);
365 if (ret)
366 return ret;
367 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800368}
369
370static ssize_t
371thermal_cooling_device_cur_state_show(struct device *dev,
372 struct device_attribute *attr, char *buf)
373{
374 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000375 unsigned long state;
376 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800377
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000378 ret = cdev->ops->get_cur_state(cdev, &state);
379 if (ret)
380 return ret;
381 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800382}
383
384static ssize_t
385thermal_cooling_device_cur_state_store(struct device *dev,
386 struct device_attribute *attr,
387 const char *buf, size_t count)
388{
389 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000390 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800391 int result;
392
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000393 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800394 return -EINVAL;
395
Roel Kluinedb94912009-12-15 22:46:50 +0100396 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800397 return -EINVAL;
398
399 result = cdev->ops->set_cur_state(cdev, state);
400 if (result)
401 return result;
402 return count;
403}
404
405static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500406__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800407static DEVICE_ATTR(max_state, 0444,
408 thermal_cooling_device_max_state_show, NULL);
409static DEVICE_ATTR(cur_state, 0644,
410 thermal_cooling_device_cur_state_show,
411 thermal_cooling_device_cur_state_store);
412
413static ssize_t
414thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500415 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800416{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800417 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800418
419 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800420 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800421
422 if (instance->trip == THERMAL_TRIPS_NONE)
423 return sprintf(buf, "-1\n");
424 else
425 return sprintf(buf, "%d\n", instance->trip);
426}
427
428/* Device management */
429
Rene Herman16d75232008-06-24 19:38:56 +0200430#if defined(CONFIG_THERMAL_HWMON)
431
Zhang Ruie68b16a2008-04-21 16:07:52 +0800432/* hwmon sys I/F */
433#include <linux/hwmon.h>
Jean Delvare31f53962011-07-28 13:48:42 -0700434
435/* thermal zone devices with the same type share one hwmon device */
436struct thermal_hwmon_device {
437 char type[THERMAL_NAME_LENGTH];
438 struct device *device;
439 int count;
440 struct list_head tz_list;
441 struct list_head node;
442};
443
444struct thermal_hwmon_attr {
445 struct device_attribute attr;
446 char name[16];
447};
448
449/* one temperature input for each thermal zone */
450struct thermal_hwmon_temp {
451 struct list_head hwmon_node;
452 struct thermal_zone_device *tz;
453 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
454 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
455};
456
Zhang Ruie68b16a2008-04-21 16:07:52 +0800457static LIST_HEAD(thermal_hwmon_list);
458
459static ssize_t
460name_show(struct device *dev, struct device_attribute *attr, char *buf)
461{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700462 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800463 return sprintf(buf, "%s\n", hwmon->type);
464}
465static DEVICE_ATTR(name, 0444, name_show, NULL);
466
467static ssize_t
468temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
469{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000470 long temperature;
471 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800472 struct thermal_hwmon_attr *hwmon_attr
473 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700474 struct thermal_hwmon_temp *temp
475 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800476 temp_input);
Jean Delvare31f53962011-07-28 13:48:42 -0700477 struct thermal_zone_device *tz = temp->tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800478
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000479 ret = tz->ops->get_temp(tz, &temperature);
480
481 if (ret)
482 return ret;
483
484 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800485}
486
487static ssize_t
488temp_crit_show(struct device *dev, struct device_attribute *attr,
489 char *buf)
490{
491 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_crit);
Jean Delvare31f53962011-07-28 13:48:42 -0700496 struct thermal_zone_device *tz = temp->tz;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000497 long temperature;
498 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800499
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000500 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
501 if (ret)
502 return ret;
503
504 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800505}
506
507
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700508static struct thermal_hwmon_device *
509thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
510{
511 struct thermal_hwmon_device *hwmon;
512
513 mutex_lock(&thermal_list_lock);
514 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
515 if (!strcmp(hwmon->type, tz->type)) {
516 mutex_unlock(&thermal_list_lock);
517 return hwmon;
518 }
519 mutex_unlock(&thermal_list_lock);
520
521 return NULL;
522}
523
Jean Delvare31f53962011-07-28 13:48:42 -0700524/* Find the temperature input matching a given thermal zone */
525static struct thermal_hwmon_temp *
526thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
527 const struct thermal_zone_device *tz)
528{
529 struct thermal_hwmon_temp *temp;
530
531 mutex_lock(&thermal_list_lock);
532 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
533 if (temp->tz == tz) {
534 mutex_unlock(&thermal_list_lock);
535 return temp;
536 }
537 mutex_unlock(&thermal_list_lock);
538
539 return NULL;
540}
541
Zhang Ruie68b16a2008-04-21 16:07:52 +0800542static int
543thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
544{
545 struct thermal_hwmon_device *hwmon;
Jean Delvare31f53962011-07-28 13:48:42 -0700546 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800547 int new_hwmon_device = 1;
548 int result;
549
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700550 hwmon = thermal_hwmon_lookup_by_type(tz);
551 if (hwmon) {
552 new_hwmon_device = 0;
553 goto register_sys_interface;
554 }
Zhang Ruie68b16a2008-04-21 16:07:52 +0800555
556 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
557 if (!hwmon)
558 return -ENOMEM;
559
560 INIT_LIST_HEAD(&hwmon->tz_list);
561 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
562 hwmon->device = hwmon_device_register(NULL);
563 if (IS_ERR(hwmon->device)) {
564 result = PTR_ERR(hwmon->device);
565 goto free_mem;
566 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700567 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800568 result = device_create_file(hwmon->device, &dev_attr_name);
569 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530570 goto free_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800571
572 register_sys_interface:
Jean Delvare31f53962011-07-28 13:48:42 -0700573 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
574 if (!temp) {
575 result = -ENOMEM;
576 goto unregister_name;
577 }
578
579 temp->tz = tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800580 hwmon->count++;
581
Guenter Roeck79a49162012-07-21 10:53:48 +1000582 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +0800583 "temp%d_input", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700584 temp->temp_input.attr.attr.name = temp->temp_input.name;
585 temp->temp_input.attr.attr.mode = 0444;
586 temp->temp_input.attr.show = temp_input_show;
587 sysfs_attr_init(&temp->temp_input.attr.attr);
588 result = device_create_file(hwmon->device, &temp->temp_input.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800589 if (result)
Jean Delvare31f53962011-07-28 13:48:42 -0700590 goto free_temp_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800591
592 if (tz->ops->get_crit_temp) {
593 unsigned long temperature;
594 if (!tz->ops->get_crit_temp(tz, &temperature)) {
Guenter Roeck79a49162012-07-21 10:53:48 +1000595 snprintf(temp->temp_crit.name,
596 sizeof(temp->temp_crit.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +0800597 "temp%d_crit", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700598 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
599 temp->temp_crit.attr.attr.mode = 0444;
600 temp->temp_crit.attr.show = temp_crit_show;
601 sysfs_attr_init(&temp->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800602 result = device_create_file(hwmon->device,
Jean Delvare31f53962011-07-28 13:48:42 -0700603 &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800604 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530605 goto unregister_input;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800606 }
607 }
608
609 mutex_lock(&thermal_list_lock);
610 if (new_hwmon_device)
611 list_add_tail(&hwmon->node, &thermal_hwmon_list);
Jean Delvare31f53962011-07-28 13:48:42 -0700612 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800613 mutex_unlock(&thermal_list_lock);
614
615 return 0;
616
Durgadoss Rb299eb52011-03-03 04:30:13 +0530617 unregister_input:
Jean Delvare31f53962011-07-28 13:48:42 -0700618 device_remove_file(hwmon->device, &temp->temp_input.attr);
619 free_temp_mem:
620 kfree(temp);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530621 unregister_name:
Zhang Ruie68b16a2008-04-21 16:07:52 +0800622 if (new_hwmon_device) {
623 device_remove_file(hwmon->device, &dev_attr_name);
624 hwmon_device_unregister(hwmon->device);
625 }
626 free_mem:
627 if (new_hwmon_device)
628 kfree(hwmon);
629
630 return result;
631}
632
633static void
634thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
635{
Jean Delvare31f53962011-07-28 13:48:42 -0700636 struct thermal_hwmon_device *hwmon;
637 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800638
Jean Delvare31f53962011-07-28 13:48:42 -0700639 hwmon = thermal_hwmon_lookup_by_type(tz);
640 if (unlikely(!hwmon)) {
641 /* Should never happen... */
642 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
643 return;
644 }
645
646 temp = thermal_hwmon_lookup_temp(hwmon, tz);
647 if (unlikely(!temp)) {
648 /* Should never happen... */
649 dev_dbg(&tz->device, "temperature input lookup failed!\n");
650 return;
651 }
652
653 device_remove_file(hwmon->device, &temp->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530654 if (tz->ops->get_crit_temp)
Jean Delvare31f53962011-07-28 13:48:42 -0700655 device_remove_file(hwmon->device, &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800656
657 mutex_lock(&thermal_list_lock);
Jean Delvare31f53962011-07-28 13:48:42 -0700658 list_del(&temp->hwmon_node);
659 kfree(temp);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800660 if (!list_empty(&hwmon->tz_list)) {
661 mutex_unlock(&thermal_list_lock);
662 return;
663 }
664 list_del(&hwmon->node);
665 mutex_unlock(&thermal_list_lock);
666
667 device_remove_file(hwmon->device, &dev_attr_name);
668 hwmon_device_unregister(hwmon->device);
669 kfree(hwmon);
670}
671#else
672static int
673thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
674{
675 return 0;
676}
677
678static void
679thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
680{
681}
682#endif
683
Matthew Garrettb1569e92008-12-03 17:55:32 +0000684static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
685 int delay)
686{
Matthew Garrettb1569e92008-12-03 17:55:32 +0000687 if (delay > 1000)
Tejun Heo41f63c52012-08-03 10:30:47 -0700688 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
689 round_jiffies(msecs_to_jiffies(delay)));
690 else if (delay)
691 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
692 msecs_to_jiffies(delay));
Matthew Garrettb1569e92008-12-03 17:55:32 +0000693 else
Tejun Heo41f63c52012-08-03 10:30:47 -0700694 cancel_delayed_work(&tz->poll_queue);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000695}
696
Matthew Garrettb1569e92008-12-03 17:55:32 +0000697static void thermal_zone_device_check(struct work_struct *work)
698{
699 struct thermal_zone_device *tz = container_of(work, struct
700 thermal_zone_device,
701 poll_queue.work);
702 thermal_zone_device_update(tz);
703}
Zhang Ruie68b16a2008-04-21 16:07:52 +0800704
Zhang Rui203d3d42008-01-17 15:51:08 +0800705/**
706 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800707 * @tz: thermal zone device
708 * @trip: indicates which trip point the cooling devices is
709 * associated with in this thermal zone.
710 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500711 *
712 * This function is usually called in the thermal zone device .bind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800713 */
714int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
715 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800716 struct thermal_cooling_device *cdev,
717 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +0800718{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800719 struct thermal_instance *dev;
720 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500721 struct thermal_zone_device *pos1;
722 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800723 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800724 int result;
725
Len Brown543a9562008-02-07 16:55:08 -0500726 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800727 return -EINVAL;
728
Thomas Sujithc7516702008-02-15 00:58:50 -0500729 list_for_each_entry(pos1, &thermal_tz_list, node) {
730 if (pos1 == tz)
731 break;
732 }
733 list_for_each_entry(pos2, &thermal_cdev_list, node) {
734 if (pos2 == cdev)
735 break;
736 }
737
738 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800739 return -EINVAL;
740
Zhang Rui9d998422012-06-26 16:35:57 +0800741 cdev->ops->get_max_state(cdev, &max_state);
742
743 /* lower default 0, upper default max_state */
744 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
745 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
746
747 if (lower > upper || upper > max_state)
748 return -EINVAL;
749
Zhang Rui203d3d42008-01-17 15:51:08 +0800750 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800751 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800752 if (!dev)
753 return -ENOMEM;
754 dev->tz = tz;
755 dev->cdev = cdev;
756 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800757 dev->upper = upper;
758 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800759 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +0800760
Zhang Rui203d3d42008-01-17 15:51:08 +0800761 result = get_idr(&tz->idr, &tz->lock, &dev->id);
762 if (result)
763 goto free_mem;
764
765 sprintf(dev->name, "cdev%d", dev->id);
766 result =
767 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
768 if (result)
769 goto release_idr;
770
771 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700772 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800773 dev->attr.attr.name = dev->attr_name;
774 dev->attr.attr.mode = 0444;
775 dev->attr.show = thermal_cooling_device_trip_point_show;
776 result = device_create_file(&tz->device, &dev->attr);
777 if (result)
778 goto remove_symbol_link;
779
780 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800781 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800782 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +0800783 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
784 result = -EEXIST;
785 break;
786 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800787 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800788 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800789 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
790 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800791 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800792 mutex_unlock(&tz->lock);
793
794 if (!result)
795 return 0;
796
797 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -0700798remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800799 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -0700800release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +0800801 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -0700802free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800803 kfree(dev);
804 return result;
805}
806EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
807
808/**
809 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800810 * @tz: thermal zone device
811 * @trip: indicates which trip point the cooling devices is
812 * associated with in this thermal zone.
813 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500814 *
815 * This function is usually called in the thermal zone device .unbind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800816 */
817int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
818 int trip,
819 struct thermal_cooling_device *cdev)
820{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800821 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +0800822
823 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800824 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800825 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -0500826 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800827 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800828 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800829 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800830 mutex_unlock(&tz->lock);
831 goto unbind;
832 }
833 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800834 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800835 mutex_unlock(&tz->lock);
836
837 return -ENODEV;
838
Joe Perchescaca8b82012-03-21 12:55:02 -0700839unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +0800840 device_remove_file(&tz->device, &pos->attr);
841 sysfs_remove_link(&tz->device.kobj, pos->name);
842 release_idr(&tz->idr, &tz->lock, pos->id);
843 kfree(pos);
844 return 0;
845}
846EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
847
848static void thermal_release(struct device *dev)
849{
850 struct thermal_zone_device *tz;
851 struct thermal_cooling_device *cdev;
852
Joe Perchescaca8b82012-03-21 12:55:02 -0700853 if (!strncmp(dev_name(dev), "thermal_zone",
854 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800855 tz = to_thermal_zone(dev);
856 kfree(tz);
857 } else {
858 cdev = to_cooling_device(dev);
859 kfree(cdev);
860 }
861}
862
863static struct class thermal_class = {
864 .name = "thermal",
865 .dev_release = thermal_release,
866};
867
868/**
869 * thermal_cooling_device_register - register a new thermal cooling device
870 * @type: the thermal cooling device type.
871 * @devdata: device private data.
872 * @ops: standard thermal cooling devices callbacks.
873 */
Joe Perchescaca8b82012-03-21 12:55:02 -0700874struct thermal_cooling_device *
875thermal_cooling_device_register(char *type, void *devdata,
876 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800877{
878 struct thermal_cooling_device *cdev;
879 struct thermal_zone_device *pos;
880 int result;
881
Guenter Roeck204dd1d2012-08-07 22:36:45 -0700882 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500883 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800884
885 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -0500886 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500887 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800888
889 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
890 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500891 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +0800892
893 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
894 if (result) {
895 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500896 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800897 }
898
Guenter Roeck204dd1d2012-08-07 22:36:45 -0700899 strcpy(cdev->type, type ? : "");
Zhang Ruif4a821c2012-07-24 16:56:21 +0800900 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800901 INIT_LIST_HEAD(&cdev->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +0800902 cdev->ops = ops;
Zhang Ruice119f82012-06-27 14:13:04 +0800903 cdev->updated = true;
Zhang Rui203d3d42008-01-17 15:51:08 +0800904 cdev->device.class = &thermal_class;
905 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -0800906 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800907 result = device_register(&cdev->device);
908 if (result) {
909 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
910 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500911 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800912 }
913
914 /* sys I/F */
915 if (type) {
Len Brown543a9562008-02-07 16:55:08 -0500916 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +0800917 if (result)
918 goto unregister;
919 }
920
921 result = device_create_file(&cdev->device, &dev_attr_max_state);
922 if (result)
923 goto unregister;
924
925 result = device_create_file(&cdev->device, &dev_attr_cur_state);
926 if (result)
927 goto unregister;
928
929 mutex_lock(&thermal_list_lock);
930 list_add(&cdev->node, &thermal_cdev_list);
931 list_for_each_entry(pos, &thermal_tz_list, node) {
932 if (!pos->ops->bind)
933 continue;
934 result = pos->ops->bind(pos, cdev);
935 if (result)
936 break;
937
938 }
939 mutex_unlock(&thermal_list_lock);
940
941 if (!result)
942 return cdev;
943
Joe Perchescaca8b82012-03-21 12:55:02 -0700944unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +0800945 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
946 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500947 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800948}
949EXPORT_SYMBOL(thermal_cooling_device_register);
950
951/**
952 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +0800953 * @cdev: the thermal cooling device to remove.
954 *
955 * thermal_cooling_device_unregister() must be called when the device is no
956 * longer needed.
957 */
958void thermal_cooling_device_unregister(struct
959 thermal_cooling_device
960 *cdev)
961{
962 struct thermal_zone_device *tz;
963 struct thermal_cooling_device *pos = NULL;
964
965 if (!cdev)
966 return;
967
968 mutex_lock(&thermal_list_lock);
969 list_for_each_entry(pos, &thermal_cdev_list, node)
970 if (pos == cdev)
971 break;
972 if (pos != cdev) {
973 /* thermal cooling device not found */
974 mutex_unlock(&thermal_list_lock);
975 return;
976 }
977 list_del(&cdev->node);
978 list_for_each_entry(tz, &thermal_tz_list, node) {
979 if (!tz->ops->unbind)
980 continue;
981 tz->ops->unbind(tz, cdev);
982 }
983 mutex_unlock(&thermal_list_lock);
984 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -0500985 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +0800986 device_remove_file(&cdev->device, &dev_attr_max_state);
987 device_remove_file(&cdev->device, &dev_attr_cur_state);
988
989 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
990 device_unregister(&cdev->device);
991 return;
992}
993EXPORT_SYMBOL(thermal_cooling_device_unregister);
994
Zhang Ruice119f82012-06-27 14:13:04 +0800995static void thermal_cdev_do_update(struct thermal_cooling_device *cdev)
996{
997 struct thermal_instance *instance;
998 unsigned long target = 0;
999
1000 /* cooling device is updated*/
1001 if (cdev->updated)
1002 return;
1003
Zhang Ruif4a821c2012-07-24 16:56:21 +08001004 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001005 /* Make sure cdev enters the deepest cooling state */
1006 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1007 if (instance->target == THERMAL_NO_TARGET)
1008 continue;
1009 if (instance->target > target)
1010 target = instance->target;
1011 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001012 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001013 cdev->ops->set_cur_state(cdev, target);
1014 cdev->updated = true;
1015}
1016
1017static void thermal_zone_do_update(struct thermal_zone_device *tz)
1018{
1019 struct thermal_instance *instance;
1020
1021 list_for_each_entry(instance, &tz->thermal_instances, tz_node)
1022 thermal_cdev_do_update(instance->cdev);
1023}
1024
Zhang Rui4ae46be2012-06-27 10:05:39 +08001025/*
Zhang Rui908b9fb2012-06-27 14:14:05 +08001026 * Cooling algorithm for both active and passive cooling
Zhang Rui4ae46be2012-06-27 10:05:39 +08001027 *
1028 * 1. if the temperature is higher than a trip point,
1029 * a. if the trend is THERMAL_TREND_RAISING, use higher cooling
1030 * state for this trip point
1031 * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
1032 * state for this trip point
1033 *
1034 * 2. if the temperature is lower than a trip point, use lower
1035 * cooling state for this trip point
1036 *
1037 * Note that this behaves the same as the previous passive cooling
1038 * algorithm.
1039 */
1040
1041static void thermal_zone_trip_update(struct thermal_zone_device *tz,
1042 int trip, long temp)
1043{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001044 struct thermal_instance *instance;
Zhang Rui4ae46be2012-06-27 10:05:39 +08001045 struct thermal_cooling_device *cdev = NULL;
1046 unsigned long cur_state, max_state;
1047 long trip_temp;
Zhang Rui908b9fb2012-06-27 14:14:05 +08001048 enum thermal_trip_type trip_type;
Zhang Rui4ae46be2012-06-27 10:05:39 +08001049 enum thermal_trend trend;
1050
Zhang Rui908b9fb2012-06-27 14:14:05 +08001051 if (trip == THERMAL_TRIPS_NONE) {
1052 trip_temp = tz->forced_passive;
1053 trip_type = THERMAL_TRIPS_NONE;
1054 } else {
1055 tz->ops->get_trip_temp(tz, trip, &trip_temp);
1056 tz->ops->get_trip_type(tz, trip, &trip_type);
1057 }
Zhang Rui4ae46be2012-06-27 10:05:39 +08001058
1059 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
1060 /*
1061 * compare the current temperature and previous temperature
1062 * to get the thermal trend, if no special requirement
1063 */
1064 if (tz->temperature > tz->last_temperature)
1065 trend = THERMAL_TREND_RAISING;
1066 else if (tz->temperature < tz->last_temperature)
1067 trend = THERMAL_TREND_DROPPING;
1068 else
1069 trend = THERMAL_TREND_STABLE;
1070 }
1071
1072 if (temp >= trip_temp) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001073 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
Zhang Rui4ae46be2012-06-27 10:05:39 +08001074 if (instance->trip != trip)
1075 continue;
1076
1077 cdev = instance->cdev;
1078
1079 cdev->ops->get_cur_state(cdev, &cur_state);
1080 cdev->ops->get_max_state(cdev, &max_state);
1081
1082 if (trend == THERMAL_TREND_RAISING) {
1083 cur_state = cur_state < instance->upper ?
1084 (cur_state + 1) : instance->upper;
1085 } else if (trend == THERMAL_TREND_DROPPING) {
1086 cur_state = cur_state > instance->lower ?
1087 (cur_state - 1) : instance->lower;
1088 }
Zhang Rui908b9fb2012-06-27 14:14:05 +08001089
1090 /* activate a passive thermal instance */
1091 if ((trip_type == THERMAL_TRIP_PASSIVE ||
1092 trip_type == THERMAL_TRIPS_NONE) &&
1093 instance->target == THERMAL_NO_TARGET)
1094 tz->passive++;
1095
Zhang Ruice119f82012-06-27 14:13:04 +08001096 instance->target = cur_state;
1097 cdev->updated = false; /* cooling device needs update */
Zhang Rui4ae46be2012-06-27 10:05:39 +08001098 }
1099 } else { /* below trip */
Zhang Ruicddf31b2012-06-27 10:09:36 +08001100 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
Zhang Rui4ae46be2012-06-27 10:05:39 +08001101 if (instance->trip != trip)
1102 continue;
1103
Zhang Ruice119f82012-06-27 14:13:04 +08001104 /* Do not use the inactive thermal instance */
1105 if (instance->target == THERMAL_NO_TARGET)
1106 continue;
Zhang Rui4ae46be2012-06-27 10:05:39 +08001107 cdev = instance->cdev;
1108 cdev->ops->get_cur_state(cdev, &cur_state);
1109
1110 cur_state = cur_state > instance->lower ?
Zhang Ruice119f82012-06-27 14:13:04 +08001111 (cur_state - 1) : THERMAL_NO_TARGET;
Zhang Rui908b9fb2012-06-27 14:14:05 +08001112
1113 /* deactivate a passive thermal instance */
1114 if ((trip_type == THERMAL_TRIP_PASSIVE ||
1115 trip_type == THERMAL_TRIPS_NONE) &&
1116 cur_state == THERMAL_NO_TARGET)
1117 tz->passive--;
Zhang Ruice119f82012-06-27 14:13:04 +08001118 instance->target = cur_state;
1119 cdev->updated = false; /* cooling device needs update */
Zhang Rui4ae46be2012-06-27 10:05:39 +08001120 }
1121 }
1122
1123 return;
1124}
Zhang Rui203d3d42008-01-17 15:51:08 +08001125/**
Matthew Garrettb1569e92008-12-03 17:55:32 +00001126 * thermal_zone_device_update - force an update of a thermal zone's state
1127 * @ttz: the thermal zone to update
1128 */
1129
1130void thermal_zone_device_update(struct thermal_zone_device *tz)
1131{
1132 int count, ret = 0;
1133 long temp, trip_temp;
1134 enum thermal_trip_type trip_type;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001135
1136 mutex_lock(&tz->lock);
1137
Michael Brunner0d288162009-08-26 14:29:25 -07001138 if (tz->ops->get_temp(tz, &temp)) {
1139 /* get_temp failed - retry it later */
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001140 pr_warn("failed to read out thermal zone %d\n", tz->id);
Michael Brunner0d288162009-08-26 14:29:25 -07001141 goto leave;
1142 }
Matthew Garrettb1569e92008-12-03 17:55:32 +00001143
Zhang Rui601f3d42012-06-27 09:54:33 +08001144 tz->last_temperature = tz->temperature;
1145 tz->temperature = temp;
1146
Matthew Garrettb1569e92008-12-03 17:55:32 +00001147 for (count = 0; count < tz->trips; count++) {
1148 tz->ops->get_trip_type(tz, count, &trip_type);
1149 tz->ops->get_trip_temp(tz, count, &trip_temp);
1150
1151 switch (trip_type) {
1152 case THERMAL_TRIP_CRITICAL:
Vladimir Zajac29321352009-05-06 19:34:21 +02001153 if (temp >= trip_temp) {
Matthew Garrettb1569e92008-12-03 17:55:32 +00001154 if (tz->ops->notify)
1155 ret = tz->ops->notify(tz, count,
1156 trip_type);
1157 if (!ret) {
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001158 pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1159 temp/1000);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001160 orderly_poweroff(true);
1161 }
1162 }
1163 break;
1164 case THERMAL_TRIP_HOT:
Vladimir Zajac29321352009-05-06 19:34:21 +02001165 if (temp >= trip_temp)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001166 if (tz->ops->notify)
1167 tz->ops->notify(tz, count, trip_type);
1168 break;
1169 case THERMAL_TRIP_ACTIVE:
Zhang Rui4ae46be2012-06-27 10:05:39 +08001170 thermal_zone_trip_update(tz, count, temp);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001171 break;
1172 case THERMAL_TRIP_PASSIVE:
Vladimir Zajac29321352009-05-06 19:34:21 +02001173 if (temp >= trip_temp || tz->passive)
Zhang Rui908b9fb2012-06-27 14:14:05 +08001174 thermal_zone_trip_update(tz, count, temp);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001175 break;
1176 }
1177 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001178
1179 if (tz->forced_passive)
Zhang Rui908b9fb2012-06-27 14:14:05 +08001180 thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE, temp);
1181 thermal_zone_do_update(tz);
Michael Brunner0d288162009-08-26 14:29:25 -07001182
Joe Perchescaca8b82012-03-21 12:55:02 -07001183leave:
Matthew Garrettb1569e92008-12-03 17:55:32 +00001184 if (tz->passive)
1185 thermal_zone_device_set_polling(tz, tz->passive_delay);
1186 else if (tz->polling_delay)
1187 thermal_zone_device_set_polling(tz, tz->polling_delay);
Frans Pop3767cb52009-10-26 08:39:04 +01001188 else
1189 thermal_zone_device_set_polling(tz, 0);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001190 mutex_unlock(&tz->lock);
1191}
1192EXPORT_SYMBOL(thermal_zone_device_update);
1193
1194/**
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001195 * create_trip_attrs - create attributes for trip points
1196 * @tz: the thermal zone device
1197 * @mask: Writeable trip point bitmap.
1198 */
1199static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1200{
1201 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001202 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001203
Durgadoss R27365a62012-07-25 10:10:59 +08001204 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001205 if (!tz->trip_type_attrs)
1206 return -ENOMEM;
1207
Durgadoss R27365a62012-07-25 10:10:59 +08001208 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001209 if (!tz->trip_temp_attrs) {
1210 kfree(tz->trip_type_attrs);
1211 return -ENOMEM;
1212 }
1213
Durgadoss R27365a62012-07-25 10:10:59 +08001214 if (tz->ops->get_trip_hyst) {
1215 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1216 if (!tz->trip_hyst_attrs) {
1217 kfree(tz->trip_type_attrs);
1218 kfree(tz->trip_temp_attrs);
1219 return -ENOMEM;
1220 }
1221 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001222
Durgadoss R27365a62012-07-25 10:10:59 +08001223
1224 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001225 /* create trip type attribute */
1226 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1227 "trip_point_%d_type", indx);
1228
1229 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1230 tz->trip_type_attrs[indx].attr.attr.name =
1231 tz->trip_type_attrs[indx].name;
1232 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1233 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1234
1235 device_create_file(&tz->device,
1236 &tz->trip_type_attrs[indx].attr);
1237
1238 /* create trip temp attribute */
1239 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1240 "trip_point_%d_temp", indx);
1241
1242 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1243 tz->trip_temp_attrs[indx].attr.attr.name =
1244 tz->trip_temp_attrs[indx].name;
1245 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1246 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1247 if (mask & (1 << indx)) {
1248 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1249 tz->trip_temp_attrs[indx].attr.store =
1250 trip_point_temp_store;
1251 }
1252
1253 device_create_file(&tz->device,
1254 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001255
1256 /* create Optional trip hyst attribute */
1257 if (!tz->ops->get_trip_hyst)
1258 continue;
1259 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1260 "trip_point_%d_hyst", indx);
1261
1262 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1263 tz->trip_hyst_attrs[indx].attr.attr.name =
1264 tz->trip_hyst_attrs[indx].name;
1265 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1266 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1267 if (tz->ops->set_trip_hyst) {
1268 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1269 tz->trip_hyst_attrs[indx].attr.store =
1270 trip_point_hyst_store;
1271 }
1272
1273 device_create_file(&tz->device,
1274 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001275 }
1276 return 0;
1277}
1278
1279static void remove_trip_attrs(struct thermal_zone_device *tz)
1280{
1281 int indx;
1282
1283 for (indx = 0; indx < tz->trips; indx++) {
1284 device_remove_file(&tz->device,
1285 &tz->trip_type_attrs[indx].attr);
1286 device_remove_file(&tz->device,
1287 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001288 if (tz->ops->get_trip_hyst)
1289 device_remove_file(&tz->device,
1290 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001291 }
1292 kfree(tz->trip_type_attrs);
1293 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001294 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001295}
1296
1297/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001298 * thermal_zone_device_register - register a new thermal zone device
1299 * @type: the thermal zone device type
1300 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001301 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001302 * @devdata: private device data
1303 * @ops: standard thermal zone device callbacks
Matthew Garrettb1569e92008-12-03 17:55:32 +00001304 * @passive_delay: number of milliseconds to wait between polls when
1305 * performing passive cooling
1306 * @polling_delay: number of milliseconds to wait between polls when checking
1307 * whether trip points have been crossed (0 for interrupt
1308 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001309 *
1310 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001311 * longer needed. The passive cooling depends on the .get_trend() return value.
Zhang Rui203d3d42008-01-17 15:51:08 +08001312 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001313struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001314 int trips, int mask, void *devdata,
Alan Cox5b275ce2010-11-11 15:27:29 +00001315 const struct thermal_zone_device_ops *ops,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001316 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001317{
1318 struct thermal_zone_device *tz;
1319 struct thermal_cooling_device *pos;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001320 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001321 int result;
1322 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001323 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001324
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001325 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001326 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001327
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001328 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001329 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001330
1331 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001332 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001333
1334 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1335 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001336 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001337
Zhang Rui2d374132012-06-27 10:09:00 +08001338 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001339 idr_init(&tz->idr);
1340 mutex_init(&tz->lock);
1341 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1342 if (result) {
1343 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001344 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001345 }
1346
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001347 strcpy(tz->type, type ? : "");
Zhang Rui203d3d42008-01-17 15:51:08 +08001348 tz->ops = ops;
1349 tz->device.class = &thermal_class;
1350 tz->devdata = devdata;
1351 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001352 tz->passive_delay = passive_delay;
1353 tz->polling_delay = polling_delay;
1354
Kay Sievers354655e2009-01-06 10:44:37 -08001355 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001356 result = device_register(&tz->device);
1357 if (result) {
1358 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1359 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001360 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001361 }
1362
1363 /* sys I/F */
1364 if (type) {
1365 result = device_create_file(&tz->device, &dev_attr_type);
1366 if (result)
1367 goto unregister;
1368 }
1369
1370 result = device_create_file(&tz->device, &dev_attr_temp);
1371 if (result)
1372 goto unregister;
1373
1374 if (ops->get_mode) {
1375 result = device_create_file(&tz->device, &dev_attr_mode);
1376 if (result)
1377 goto unregister;
1378 }
1379
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001380 result = create_trip_attrs(tz, mask);
1381 if (result)
1382 goto unregister;
1383
Zhang Rui203d3d42008-01-17 15:51:08 +08001384 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001385 tz->ops->get_trip_type(tz, count, &trip_type);
1386 if (trip_type == THERMAL_TRIP_PASSIVE)
1387 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001388 }
1389
Matthew Garrett03a971a2008-12-03 18:00:38 +00001390 if (!passive)
1391 result = device_create_file(&tz->device,
1392 &dev_attr_passive);
1393
1394 if (result)
1395 goto unregister;
1396
Zhang Ruie68b16a2008-04-21 16:07:52 +08001397 result = thermal_add_hwmon_sysfs(tz);
1398 if (result)
1399 goto unregister;
1400
Zhang Rui203d3d42008-01-17 15:51:08 +08001401 mutex_lock(&thermal_list_lock);
1402 list_add_tail(&tz->node, &thermal_tz_list);
1403 if (ops->bind)
1404 list_for_each_entry(pos, &thermal_cdev_list, node) {
Len Brown543a9562008-02-07 16:55:08 -05001405 result = ops->bind(tz, pos);
1406 if (result)
1407 break;
Zhang Rui203d3d42008-01-17 15:51:08 +08001408 }
1409 mutex_unlock(&thermal_list_lock);
1410
Matthew Garrettb1569e92008-12-03 17:55:32 +00001411 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1412
1413 thermal_zone_device_update(tz);
1414
Zhang Rui203d3d42008-01-17 15:51:08 +08001415 if (!result)
1416 return tz;
1417
Joe Perchescaca8b82012-03-21 12:55:02 -07001418unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001419 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1420 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001421 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001422}
1423EXPORT_SYMBOL(thermal_zone_device_register);
1424
1425/**
1426 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001427 * @tz: the thermal zone device to remove
1428 */
1429void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1430{
1431 struct thermal_cooling_device *cdev;
1432 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001433
1434 if (!tz)
1435 return;
1436
1437 mutex_lock(&thermal_list_lock);
1438 list_for_each_entry(pos, &thermal_tz_list, node)
1439 if (pos == tz)
1440 break;
1441 if (pos != tz) {
1442 /* thermal zone device not found */
1443 mutex_unlock(&thermal_list_lock);
1444 return;
1445 }
1446 list_del(&tz->node);
1447 if (tz->ops->unbind)
1448 list_for_each_entry(cdev, &thermal_cdev_list, node)
1449 tz->ops->unbind(tz, cdev);
1450 mutex_unlock(&thermal_list_lock);
1451
Matthew Garrettb1569e92008-12-03 17:55:32 +00001452 thermal_zone_device_set_polling(tz, 0);
1453
Zhang Rui203d3d42008-01-17 15:51:08 +08001454 if (tz->type[0])
1455 device_remove_file(&tz->device, &dev_attr_type);
1456 device_remove_file(&tz->device, &dev_attr_temp);
1457 if (tz->ops->get_mode)
1458 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001459 remove_trip_attrs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001460
Zhang Ruie68b16a2008-04-21 16:07:52 +08001461 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001462 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1463 idr_destroy(&tz->idr);
1464 mutex_destroy(&tz->lock);
1465 device_unregister(&tz->device);
1466 return;
1467}
Zhang Rui203d3d42008-01-17 15:51:08 +08001468EXPORT_SYMBOL(thermal_zone_device_unregister);
1469
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001470#ifdef CONFIG_NET
1471static struct genl_family thermal_event_genl_family = {
1472 .id = GENL_ID_GENERATE,
1473 .name = THERMAL_GENL_FAMILY_NAME,
1474 .version = THERMAL_GENL_VERSION,
1475 .maxattr = THERMAL_GENL_ATTR_MAX,
1476};
1477
1478static struct genl_multicast_group thermal_event_mcgrp = {
1479 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1480};
1481
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001482int thermal_generate_netlink_event(u32 orig, enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301483{
1484 struct sk_buff *skb;
1485 struct nlattr *attr;
1486 struct thermal_genl_event *thermal_event;
1487 void *msg_header;
1488 int size;
1489 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001490 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301491
1492 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001493 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1494 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301495
1496 skb = genlmsg_new(size, GFP_ATOMIC);
1497 if (!skb)
1498 return -ENOMEM;
1499
1500 /* add the genetlink message header */
1501 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1502 &thermal_event_genl_family, 0,
1503 THERMAL_GENL_CMD_EVENT);
1504 if (!msg_header) {
1505 nlmsg_free(skb);
1506 return -ENOMEM;
1507 }
1508
1509 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001510 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1511 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301512
1513 if (!attr) {
1514 nlmsg_free(skb);
1515 return -EINVAL;
1516 }
1517
1518 thermal_event = nla_data(attr);
1519 if (!thermal_event) {
1520 nlmsg_free(skb);
1521 return -EINVAL;
1522 }
1523
1524 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1525
1526 thermal_event->orig = orig;
1527 thermal_event->event = event;
1528
1529 /* send multicast genetlink message */
1530 result = genlmsg_end(skb, msg_header);
1531 if (result < 0) {
1532 nlmsg_free(skb);
1533 return result;
1534 }
1535
1536 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1537 if (result)
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001538 pr_info("failed to send netlink event:%d\n", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301539
1540 return result;
1541}
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001542EXPORT_SYMBOL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301543
1544static int genetlink_init(void)
1545{
1546 int result;
1547
1548 result = genl_register_family(&thermal_event_genl_family);
1549 if (result)
1550 return result;
1551
1552 result = genl_register_mc_group(&thermal_event_genl_family,
1553 &thermal_event_mcgrp);
1554 if (result)
1555 genl_unregister_family(&thermal_event_genl_family);
1556 return result;
1557}
1558
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001559static void genetlink_exit(void)
1560{
1561 genl_unregister_family(&thermal_event_genl_family);
1562}
1563#else /* !CONFIG_NET */
1564static inline int genetlink_init(void) { return 0; }
1565static inline void genetlink_exit(void) {}
1566#endif /* !CONFIG_NET */
1567
Zhang Rui203d3d42008-01-17 15:51:08 +08001568static int __init thermal_init(void)
1569{
1570 int result = 0;
1571
1572 result = class_register(&thermal_class);
1573 if (result) {
1574 idr_destroy(&thermal_tz_idr);
1575 idr_destroy(&thermal_cdev_idr);
1576 mutex_destroy(&thermal_idr_lock);
1577 mutex_destroy(&thermal_list_lock);
1578 }
R.Durgadoss4cb18722010-10-27 03:33:29 +05301579 result = genetlink_init();
Zhang Rui203d3d42008-01-17 15:51:08 +08001580 return result;
1581}
1582
1583static void __exit thermal_exit(void)
1584{
1585 class_unregister(&thermal_class);
1586 idr_destroy(&thermal_tz_idr);
1587 idr_destroy(&thermal_cdev_idr);
1588 mutex_destroy(&thermal_idr_lock);
1589 mutex_destroy(&thermal_list_lock);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301590 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001591}
1592
R.Durgadoss4cb18722010-10-27 03:33:29 +05301593fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001594module_exit(thermal_exit);