blob: dca3bfc0e702742fefc9790d98ddac01dd3e9650 [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
Durgadoss R9b4298a2012-09-18 11:04:54 +053085int get_tz_trend(struct thermal_zone_device *tz, int trip)
86{
87 enum thermal_trend trend;
88
89 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
90 if (tz->temperature > tz->last_temperature)
91 trend = THERMAL_TREND_RAISING;
92 else if (tz->temperature < tz->last_temperature)
93 trend = THERMAL_TREND_DROPPING;
94 else
95 trend = THERMAL_TREND_STABLE;
96 }
97
98 return trend;
99}
100EXPORT_SYMBOL(get_tz_trend);
101
102struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
103 struct thermal_cooling_device *cdev, int trip)
104{
105 struct thermal_instance *pos = NULL;
106 struct thermal_instance *target_instance = NULL;
107
108 mutex_lock(&tz->lock);
109 mutex_lock(&cdev->lock);
110
111 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
112 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
113 target_instance = pos;
114 break;
115 }
116 }
117
118 mutex_unlock(&cdev->lock);
119 mutex_unlock(&tz->lock);
120
121 return target_instance;
122}
123EXPORT_SYMBOL(get_thermal_instance);
124
Zhang Rui203d3d42008-01-17 15:51:08 +0800125/* sys I/F for thermal zone */
126
127#define to_thermal_zone(_dev) \
128 container_of(_dev, struct thermal_zone_device, device)
129
130static ssize_t
131type_show(struct device *dev, struct device_attribute *attr, char *buf)
132{
133 struct thermal_zone_device *tz = to_thermal_zone(dev);
134
135 return sprintf(buf, "%s\n", tz->type);
136}
137
138static ssize_t
139temp_show(struct device *dev, struct device_attribute *attr, char *buf)
140{
141 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000142 long temperature;
143 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800144
145 if (!tz->ops->get_temp)
146 return -EPERM;
147
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000148 ret = tz->ops->get_temp(tz, &temperature);
149
150 if (ret)
151 return ret;
152
153 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800154}
155
156static ssize_t
157mode_show(struct device *dev, struct device_attribute *attr, char *buf)
158{
159 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000160 enum thermal_device_mode mode;
161 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800162
163 if (!tz->ops->get_mode)
164 return -EPERM;
165
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000166 result = tz->ops->get_mode(tz, &mode);
167 if (result)
168 return result;
169
170 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
171 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800172}
173
174static ssize_t
175mode_store(struct device *dev, struct device_attribute *attr,
176 const char *buf, size_t count)
177{
178 struct thermal_zone_device *tz = to_thermal_zone(dev);
179 int result;
180
181 if (!tz->ops->set_mode)
182 return -EPERM;
183
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530184 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000185 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530186 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000187 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
188 else
189 result = -EINVAL;
190
Zhang Rui203d3d42008-01-17 15:51:08 +0800191 if (result)
192 return result;
193
194 return count;
195}
196
197static ssize_t
198trip_point_type_show(struct device *dev, struct device_attribute *attr,
199 char *buf)
200{
201 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000202 enum thermal_trip_type type;
203 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800204
205 if (!tz->ops->get_trip_type)
206 return -EPERM;
207
208 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
209 return -EINVAL;
210
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000211 result = tz->ops->get_trip_type(tz, trip, &type);
212 if (result)
213 return result;
214
215 switch (type) {
216 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300217 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000218 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300219 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000220 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300221 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000222 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300223 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000224 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300225 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000226 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800227}
228
229static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800230trip_point_temp_store(struct device *dev, struct device_attribute *attr,
231 const char *buf, size_t count)
232{
233 struct thermal_zone_device *tz = to_thermal_zone(dev);
234 int trip, ret;
235 unsigned long temperature;
236
237 if (!tz->ops->set_trip_temp)
238 return -EPERM;
239
240 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
241 return -EINVAL;
242
243 if (kstrtoul(buf, 10, &temperature))
244 return -EINVAL;
245
246 ret = tz->ops->set_trip_temp(tz, trip, temperature);
247
248 return ret ? ret : count;
249}
250
251static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800252trip_point_temp_show(struct device *dev, struct device_attribute *attr,
253 char *buf)
254{
255 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000256 int trip, ret;
257 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800258
259 if (!tz->ops->get_trip_temp)
260 return -EPERM;
261
262 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
263 return -EINVAL;
264
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000265 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
266
267 if (ret)
268 return ret;
269
270 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800271}
272
Matthew Garrett03a971a2008-12-03 18:00:38 +0000273static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800274trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
275 const char *buf, size_t count)
276{
277 struct thermal_zone_device *tz = to_thermal_zone(dev);
278 int trip, ret;
279 unsigned long temperature;
280
281 if (!tz->ops->set_trip_hyst)
282 return -EPERM;
283
284 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
285 return -EINVAL;
286
287 if (kstrtoul(buf, 10, &temperature))
288 return -EINVAL;
289
290 /*
291 * We are not doing any check on the 'temperature' value
292 * here. The driver implementing 'set_trip_hyst' has to
293 * take care of this.
294 */
295 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
296
297 return ret ? ret : count;
298}
299
300static ssize_t
301trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
302 char *buf)
303{
304 struct thermal_zone_device *tz = to_thermal_zone(dev);
305 int trip, ret;
306 unsigned long temperature;
307
308 if (!tz->ops->get_trip_hyst)
309 return -EPERM;
310
311 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
312 return -EINVAL;
313
314 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
315
316 return ret ? ret : sprintf(buf, "%ld\n", temperature);
317}
318
319static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000320passive_store(struct device *dev, struct device_attribute *attr,
321 const char *buf, size_t count)
322{
323 struct thermal_zone_device *tz = to_thermal_zone(dev);
324 struct thermal_cooling_device *cdev = NULL;
325 int state;
326
327 if (!sscanf(buf, "%d\n", &state))
328 return -EINVAL;
329
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100330 /* sanity check: values below 1000 millicelcius don't make sense
331 * and can cause the system to go into a thermal heart attack
332 */
333 if (state && state < 1000)
334 return -EINVAL;
335
Matthew Garrett03a971a2008-12-03 18:00:38 +0000336 if (state && !tz->forced_passive) {
337 mutex_lock(&thermal_list_lock);
338 list_for_each_entry(cdev, &thermal_cdev_list, node) {
339 if (!strncmp("Processor", cdev->type,
340 sizeof("Processor")))
341 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800342 THERMAL_TRIPS_NONE, cdev,
343 THERMAL_NO_LIMIT,
344 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000345 }
346 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100347 if (!tz->passive_delay)
348 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000349 } else if (!state && tz->forced_passive) {
350 mutex_lock(&thermal_list_lock);
351 list_for_each_entry(cdev, &thermal_cdev_list, node) {
352 if (!strncmp("Processor", cdev->type,
353 sizeof("Processor")))
354 thermal_zone_unbind_cooling_device(tz,
355 THERMAL_TRIPS_NONE,
356 cdev);
357 }
358 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100359 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000360 }
361
Matthew Garrett03a971a2008-12-03 18:00:38 +0000362 tz->forced_passive = state;
363
364 thermal_zone_device_update(tz);
365
366 return count;
367}
368
369static ssize_t
370passive_show(struct device *dev, struct device_attribute *attr,
371 char *buf)
372{
373 struct thermal_zone_device *tz = to_thermal_zone(dev);
374
375 return sprintf(buf, "%d\n", tz->forced_passive);
376}
377
Zhang Rui203d3d42008-01-17 15:51:08 +0800378static DEVICE_ATTR(type, 0444, type_show, NULL);
379static DEVICE_ATTR(temp, 0444, temp_show, NULL);
380static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700381static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800382
Zhang Rui203d3d42008-01-17 15:51:08 +0800383/* sys I/F for cooling device */
384#define to_cooling_device(_dev) \
385 container_of(_dev, struct thermal_cooling_device, device)
386
387static ssize_t
388thermal_cooling_device_type_show(struct device *dev,
389 struct device_attribute *attr, char *buf)
390{
391 struct thermal_cooling_device *cdev = to_cooling_device(dev);
392
393 return sprintf(buf, "%s\n", cdev->type);
394}
395
396static ssize_t
397thermal_cooling_device_max_state_show(struct device *dev,
398 struct device_attribute *attr, char *buf)
399{
400 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000401 unsigned long state;
402 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800403
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000404 ret = cdev->ops->get_max_state(cdev, &state);
405 if (ret)
406 return ret;
407 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800408}
409
410static ssize_t
411thermal_cooling_device_cur_state_show(struct device *dev,
412 struct device_attribute *attr, char *buf)
413{
414 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000415 unsigned long state;
416 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800417
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000418 ret = cdev->ops->get_cur_state(cdev, &state);
419 if (ret)
420 return ret;
421 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800422}
423
424static ssize_t
425thermal_cooling_device_cur_state_store(struct device *dev,
426 struct device_attribute *attr,
427 const char *buf, size_t count)
428{
429 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000430 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800431 int result;
432
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000433 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800434 return -EINVAL;
435
Roel Kluinedb94912009-12-15 22:46:50 +0100436 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800437 return -EINVAL;
438
439 result = cdev->ops->set_cur_state(cdev, state);
440 if (result)
441 return result;
442 return count;
443}
444
445static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500446__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800447static DEVICE_ATTR(max_state, 0444,
448 thermal_cooling_device_max_state_show, NULL);
449static DEVICE_ATTR(cur_state, 0644,
450 thermal_cooling_device_cur_state_show,
451 thermal_cooling_device_cur_state_store);
452
453static ssize_t
454thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500455 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800456{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800457 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800458
459 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800460 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800461
462 if (instance->trip == THERMAL_TRIPS_NONE)
463 return sprintf(buf, "-1\n");
464 else
465 return sprintf(buf, "%d\n", instance->trip);
466}
467
468/* Device management */
469
Rene Herman16d75232008-06-24 19:38:56 +0200470#if defined(CONFIG_THERMAL_HWMON)
471
Zhang Ruie68b16a2008-04-21 16:07:52 +0800472/* hwmon sys I/F */
473#include <linux/hwmon.h>
Jean Delvare31f53962011-07-28 13:48:42 -0700474
475/* thermal zone devices with the same type share one hwmon device */
476struct thermal_hwmon_device {
477 char type[THERMAL_NAME_LENGTH];
478 struct device *device;
479 int count;
480 struct list_head tz_list;
481 struct list_head node;
482};
483
484struct thermal_hwmon_attr {
485 struct device_attribute attr;
486 char name[16];
487};
488
489/* one temperature input for each thermal zone */
490struct thermal_hwmon_temp {
491 struct list_head hwmon_node;
492 struct thermal_zone_device *tz;
493 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
494 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
495};
496
Zhang Ruie68b16a2008-04-21 16:07:52 +0800497static LIST_HEAD(thermal_hwmon_list);
498
499static ssize_t
500name_show(struct device *dev, struct device_attribute *attr, char *buf)
501{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700502 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800503 return sprintf(buf, "%s\n", hwmon->type);
504}
505static DEVICE_ATTR(name, 0444, name_show, NULL);
506
507static ssize_t
508temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
509{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000510 long temperature;
511 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800512 struct thermal_hwmon_attr *hwmon_attr
513 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700514 struct thermal_hwmon_temp *temp
515 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800516 temp_input);
Jean Delvare31f53962011-07-28 13:48:42 -0700517 struct thermal_zone_device *tz = temp->tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800518
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000519 ret = tz->ops->get_temp(tz, &temperature);
520
521 if (ret)
522 return ret;
523
524 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800525}
526
527static ssize_t
528temp_crit_show(struct device *dev, struct device_attribute *attr,
529 char *buf)
530{
531 struct thermal_hwmon_attr *hwmon_attr
532 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700533 struct thermal_hwmon_temp *temp
534 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800535 temp_crit);
Jean Delvare31f53962011-07-28 13:48:42 -0700536 struct thermal_zone_device *tz = temp->tz;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000537 long temperature;
538 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800539
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000540 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
541 if (ret)
542 return ret;
543
544 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800545}
546
547
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700548static struct thermal_hwmon_device *
549thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
550{
551 struct thermal_hwmon_device *hwmon;
552
553 mutex_lock(&thermal_list_lock);
554 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
555 if (!strcmp(hwmon->type, tz->type)) {
556 mutex_unlock(&thermal_list_lock);
557 return hwmon;
558 }
559 mutex_unlock(&thermal_list_lock);
560
561 return NULL;
562}
563
Jean Delvare31f53962011-07-28 13:48:42 -0700564/* Find the temperature input matching a given thermal zone */
565static struct thermal_hwmon_temp *
566thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
567 const struct thermal_zone_device *tz)
568{
569 struct thermal_hwmon_temp *temp;
570
571 mutex_lock(&thermal_list_lock);
572 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
573 if (temp->tz == tz) {
574 mutex_unlock(&thermal_list_lock);
575 return temp;
576 }
577 mutex_unlock(&thermal_list_lock);
578
579 return NULL;
580}
581
Zhang Ruie68b16a2008-04-21 16:07:52 +0800582static int
583thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
584{
585 struct thermal_hwmon_device *hwmon;
Jean Delvare31f53962011-07-28 13:48:42 -0700586 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800587 int new_hwmon_device = 1;
588 int result;
589
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700590 hwmon = thermal_hwmon_lookup_by_type(tz);
591 if (hwmon) {
592 new_hwmon_device = 0;
593 goto register_sys_interface;
594 }
Zhang Ruie68b16a2008-04-21 16:07:52 +0800595
596 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
597 if (!hwmon)
598 return -ENOMEM;
599
600 INIT_LIST_HEAD(&hwmon->tz_list);
601 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
602 hwmon->device = hwmon_device_register(NULL);
603 if (IS_ERR(hwmon->device)) {
604 result = PTR_ERR(hwmon->device);
605 goto free_mem;
606 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700607 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800608 result = device_create_file(hwmon->device, &dev_attr_name);
609 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530610 goto free_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800611
612 register_sys_interface:
Jean Delvare31f53962011-07-28 13:48:42 -0700613 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
614 if (!temp) {
615 result = -ENOMEM;
616 goto unregister_name;
617 }
618
619 temp->tz = tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800620 hwmon->count++;
621
Guenter Roeck79a49162012-07-21 10:53:48 +1000622 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +0800623 "temp%d_input", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700624 temp->temp_input.attr.attr.name = temp->temp_input.name;
625 temp->temp_input.attr.attr.mode = 0444;
626 temp->temp_input.attr.show = temp_input_show;
627 sysfs_attr_init(&temp->temp_input.attr.attr);
628 result = device_create_file(hwmon->device, &temp->temp_input.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800629 if (result)
Jean Delvare31f53962011-07-28 13:48:42 -0700630 goto free_temp_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800631
632 if (tz->ops->get_crit_temp) {
633 unsigned long temperature;
634 if (!tz->ops->get_crit_temp(tz, &temperature)) {
Guenter Roeck79a49162012-07-21 10:53:48 +1000635 snprintf(temp->temp_crit.name,
636 sizeof(temp->temp_crit.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +0800637 "temp%d_crit", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700638 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
639 temp->temp_crit.attr.attr.mode = 0444;
640 temp->temp_crit.attr.show = temp_crit_show;
641 sysfs_attr_init(&temp->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800642 result = device_create_file(hwmon->device,
Jean Delvare31f53962011-07-28 13:48:42 -0700643 &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800644 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530645 goto unregister_input;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800646 }
647 }
648
649 mutex_lock(&thermal_list_lock);
650 if (new_hwmon_device)
651 list_add_tail(&hwmon->node, &thermal_hwmon_list);
Jean Delvare31f53962011-07-28 13:48:42 -0700652 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800653 mutex_unlock(&thermal_list_lock);
654
655 return 0;
656
Durgadoss Rb299eb52011-03-03 04:30:13 +0530657 unregister_input:
Jean Delvare31f53962011-07-28 13:48:42 -0700658 device_remove_file(hwmon->device, &temp->temp_input.attr);
659 free_temp_mem:
660 kfree(temp);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530661 unregister_name:
Zhang Ruie68b16a2008-04-21 16:07:52 +0800662 if (new_hwmon_device) {
663 device_remove_file(hwmon->device, &dev_attr_name);
664 hwmon_device_unregister(hwmon->device);
665 }
666 free_mem:
667 if (new_hwmon_device)
668 kfree(hwmon);
669
670 return result;
671}
672
673static void
674thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
675{
Jean Delvare31f53962011-07-28 13:48:42 -0700676 struct thermal_hwmon_device *hwmon;
677 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800678
Jean Delvare31f53962011-07-28 13:48:42 -0700679 hwmon = thermal_hwmon_lookup_by_type(tz);
680 if (unlikely(!hwmon)) {
681 /* Should never happen... */
682 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
683 return;
684 }
685
686 temp = thermal_hwmon_lookup_temp(hwmon, tz);
687 if (unlikely(!temp)) {
688 /* Should never happen... */
689 dev_dbg(&tz->device, "temperature input lookup failed!\n");
690 return;
691 }
692
693 device_remove_file(hwmon->device, &temp->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530694 if (tz->ops->get_crit_temp)
Jean Delvare31f53962011-07-28 13:48:42 -0700695 device_remove_file(hwmon->device, &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800696
697 mutex_lock(&thermal_list_lock);
Jean Delvare31f53962011-07-28 13:48:42 -0700698 list_del(&temp->hwmon_node);
699 kfree(temp);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800700 if (!list_empty(&hwmon->tz_list)) {
701 mutex_unlock(&thermal_list_lock);
702 return;
703 }
704 list_del(&hwmon->node);
705 mutex_unlock(&thermal_list_lock);
706
707 device_remove_file(hwmon->device, &dev_attr_name);
708 hwmon_device_unregister(hwmon->device);
709 kfree(hwmon);
710}
711#else
712static int
713thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
714{
715 return 0;
716}
717
718static void
719thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
720{
721}
722#endif
723
Matthew Garrettb1569e92008-12-03 17:55:32 +0000724static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
725 int delay)
726{
Matthew Garrettb1569e92008-12-03 17:55:32 +0000727 if (delay > 1000)
Tejun Heo41f63c52012-08-03 10:30:47 -0700728 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
729 round_jiffies(msecs_to_jiffies(delay)));
730 else if (delay)
731 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
732 msecs_to_jiffies(delay));
Matthew Garrettb1569e92008-12-03 17:55:32 +0000733 else
Tejun Heo41f63c52012-08-03 10:30:47 -0700734 cancel_delayed_work(&tz->poll_queue);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000735}
736
Matthew Garrettb1569e92008-12-03 17:55:32 +0000737static void thermal_zone_device_check(struct work_struct *work)
738{
739 struct thermal_zone_device *tz = container_of(work, struct
740 thermal_zone_device,
741 poll_queue.work);
742 thermal_zone_device_update(tz);
743}
Zhang Ruie68b16a2008-04-21 16:07:52 +0800744
Zhang Rui203d3d42008-01-17 15:51:08 +0800745/**
746 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800747 * @tz: thermal zone device
748 * @trip: indicates which trip point the cooling devices is
749 * associated with in this thermal zone.
750 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500751 *
752 * This function is usually called in the thermal zone device .bind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800753 */
754int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
755 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800756 struct thermal_cooling_device *cdev,
757 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +0800758{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800759 struct thermal_instance *dev;
760 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500761 struct thermal_zone_device *pos1;
762 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800763 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800764 int result;
765
Len Brown543a9562008-02-07 16:55:08 -0500766 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800767 return -EINVAL;
768
Thomas Sujithc7516702008-02-15 00:58:50 -0500769 list_for_each_entry(pos1, &thermal_tz_list, node) {
770 if (pos1 == tz)
771 break;
772 }
773 list_for_each_entry(pos2, &thermal_cdev_list, node) {
774 if (pos2 == cdev)
775 break;
776 }
777
778 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800779 return -EINVAL;
780
Zhang Rui9d998422012-06-26 16:35:57 +0800781 cdev->ops->get_max_state(cdev, &max_state);
782
783 /* lower default 0, upper default max_state */
784 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
785 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
786
787 if (lower > upper || upper > max_state)
788 return -EINVAL;
789
Zhang Rui203d3d42008-01-17 15:51:08 +0800790 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800791 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800792 if (!dev)
793 return -ENOMEM;
794 dev->tz = tz;
795 dev->cdev = cdev;
796 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800797 dev->upper = upper;
798 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800799 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +0800800
Zhang Rui203d3d42008-01-17 15:51:08 +0800801 result = get_idr(&tz->idr, &tz->lock, &dev->id);
802 if (result)
803 goto free_mem;
804
805 sprintf(dev->name, "cdev%d", dev->id);
806 result =
807 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
808 if (result)
809 goto release_idr;
810
811 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700812 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800813 dev->attr.attr.name = dev->attr_name;
814 dev->attr.attr.mode = 0444;
815 dev->attr.show = thermal_cooling_device_trip_point_show;
816 result = device_create_file(&tz->device, &dev->attr);
817 if (result)
818 goto remove_symbol_link;
819
820 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800821 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800822 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +0800823 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
824 result = -EEXIST;
825 break;
826 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800827 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800828 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800829 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
830 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800831 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800832 mutex_unlock(&tz->lock);
833
834 if (!result)
835 return 0;
836
837 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -0700838remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800839 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -0700840release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +0800841 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -0700842free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800843 kfree(dev);
844 return result;
845}
846EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
847
848/**
849 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800850 * @tz: thermal zone device
851 * @trip: indicates which trip point the cooling devices is
852 * associated with in this thermal zone.
853 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500854 *
855 * This function is usually called in the thermal zone device .unbind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800856 */
857int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
858 int trip,
859 struct thermal_cooling_device *cdev)
860{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800861 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +0800862
863 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800864 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800865 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -0500866 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800867 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800868 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800869 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800870 mutex_unlock(&tz->lock);
871 goto unbind;
872 }
873 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800874 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800875 mutex_unlock(&tz->lock);
876
877 return -ENODEV;
878
Joe Perchescaca8b82012-03-21 12:55:02 -0700879unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +0800880 device_remove_file(&tz->device, &pos->attr);
881 sysfs_remove_link(&tz->device.kobj, pos->name);
882 release_idr(&tz->idr, &tz->lock, pos->id);
883 kfree(pos);
884 return 0;
885}
886EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
887
888static void thermal_release(struct device *dev)
889{
890 struct thermal_zone_device *tz;
891 struct thermal_cooling_device *cdev;
892
Joe Perchescaca8b82012-03-21 12:55:02 -0700893 if (!strncmp(dev_name(dev), "thermal_zone",
894 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800895 tz = to_thermal_zone(dev);
896 kfree(tz);
897 } else {
898 cdev = to_cooling_device(dev);
899 kfree(cdev);
900 }
901}
902
903static struct class thermal_class = {
904 .name = "thermal",
905 .dev_release = thermal_release,
906};
907
908/**
909 * thermal_cooling_device_register - register a new thermal cooling device
910 * @type: the thermal cooling device type.
911 * @devdata: device private data.
912 * @ops: standard thermal cooling devices callbacks.
913 */
Joe Perchescaca8b82012-03-21 12:55:02 -0700914struct thermal_cooling_device *
915thermal_cooling_device_register(char *type, void *devdata,
916 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800917{
918 struct thermal_cooling_device *cdev;
919 struct thermal_zone_device *pos;
920 int result;
921
Guenter Roeck204dd1d2012-08-07 22:36:45 -0700922 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500923 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800924
925 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -0500926 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500927 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800928
929 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
930 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500931 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +0800932
933 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
934 if (result) {
935 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500936 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800937 }
938
Guenter Roeck204dd1d2012-08-07 22:36:45 -0700939 strcpy(cdev->type, type ? : "");
Zhang Ruif4a821c2012-07-24 16:56:21 +0800940 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800941 INIT_LIST_HEAD(&cdev->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +0800942 cdev->ops = ops;
Zhang Ruice119f82012-06-27 14:13:04 +0800943 cdev->updated = true;
Zhang Rui203d3d42008-01-17 15:51:08 +0800944 cdev->device.class = &thermal_class;
945 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -0800946 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800947 result = device_register(&cdev->device);
948 if (result) {
949 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
950 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500951 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800952 }
953
954 /* sys I/F */
955 if (type) {
Len Brown543a9562008-02-07 16:55:08 -0500956 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +0800957 if (result)
958 goto unregister;
959 }
960
961 result = device_create_file(&cdev->device, &dev_attr_max_state);
962 if (result)
963 goto unregister;
964
965 result = device_create_file(&cdev->device, &dev_attr_cur_state);
966 if (result)
967 goto unregister;
968
969 mutex_lock(&thermal_list_lock);
970 list_add(&cdev->node, &thermal_cdev_list);
971 list_for_each_entry(pos, &thermal_tz_list, node) {
972 if (!pos->ops->bind)
973 continue;
974 result = pos->ops->bind(pos, cdev);
975 if (result)
976 break;
977
978 }
979 mutex_unlock(&thermal_list_lock);
980
981 if (!result)
982 return cdev;
983
Joe Perchescaca8b82012-03-21 12:55:02 -0700984unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +0800985 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
986 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500987 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800988}
989EXPORT_SYMBOL(thermal_cooling_device_register);
990
991/**
992 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +0800993 * @cdev: the thermal cooling device to remove.
994 *
995 * thermal_cooling_device_unregister() must be called when the device is no
996 * longer needed.
997 */
998void thermal_cooling_device_unregister(struct
999 thermal_cooling_device
1000 *cdev)
1001{
1002 struct thermal_zone_device *tz;
1003 struct thermal_cooling_device *pos = NULL;
1004
1005 if (!cdev)
1006 return;
1007
1008 mutex_lock(&thermal_list_lock);
1009 list_for_each_entry(pos, &thermal_cdev_list, node)
1010 if (pos == cdev)
1011 break;
1012 if (pos != cdev) {
1013 /* thermal cooling device not found */
1014 mutex_unlock(&thermal_list_lock);
1015 return;
1016 }
1017 list_del(&cdev->node);
1018 list_for_each_entry(tz, &thermal_tz_list, node) {
1019 if (!tz->ops->unbind)
1020 continue;
1021 tz->ops->unbind(tz, cdev);
1022 }
1023 mutex_unlock(&thermal_list_lock);
1024 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001025 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001026 device_remove_file(&cdev->device, &dev_attr_max_state);
1027 device_remove_file(&cdev->device, &dev_attr_cur_state);
1028
1029 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1030 device_unregister(&cdev->device);
1031 return;
1032}
1033EXPORT_SYMBOL(thermal_cooling_device_unregister);
1034
Zhang Ruice119f82012-06-27 14:13:04 +08001035static void thermal_cdev_do_update(struct thermal_cooling_device *cdev)
1036{
1037 struct thermal_instance *instance;
1038 unsigned long target = 0;
1039
1040 /* cooling device is updated*/
1041 if (cdev->updated)
1042 return;
1043
Zhang Ruif4a821c2012-07-24 16:56:21 +08001044 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001045 /* Make sure cdev enters the deepest cooling state */
1046 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1047 if (instance->target == THERMAL_NO_TARGET)
1048 continue;
1049 if (instance->target > target)
1050 target = instance->target;
1051 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001052 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001053 cdev->ops->set_cur_state(cdev, target);
1054 cdev->updated = true;
1055}
1056
1057static void thermal_zone_do_update(struct thermal_zone_device *tz)
1058{
1059 struct thermal_instance *instance;
1060
1061 list_for_each_entry(instance, &tz->thermal_instances, tz_node)
1062 thermal_cdev_do_update(instance->cdev);
1063}
1064
Zhang Rui4ae46be2012-06-27 10:05:39 +08001065/*
Zhang Rui908b9fb2012-06-27 14:14:05 +08001066 * Cooling algorithm for both active and passive cooling
Zhang Rui4ae46be2012-06-27 10:05:39 +08001067 *
1068 * 1. if the temperature is higher than a trip point,
1069 * a. if the trend is THERMAL_TREND_RAISING, use higher cooling
1070 * state for this trip point
1071 * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
1072 * state for this trip point
1073 *
1074 * 2. if the temperature is lower than a trip point, use lower
1075 * cooling state for this trip point
1076 *
1077 * Note that this behaves the same as the previous passive cooling
1078 * algorithm.
1079 */
1080
1081static void thermal_zone_trip_update(struct thermal_zone_device *tz,
1082 int trip, long temp)
1083{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001084 struct thermal_instance *instance;
Zhang Rui4ae46be2012-06-27 10:05:39 +08001085 struct thermal_cooling_device *cdev = NULL;
1086 unsigned long cur_state, max_state;
1087 long trip_temp;
Zhang Rui908b9fb2012-06-27 14:14:05 +08001088 enum thermal_trip_type trip_type;
Zhang Rui4ae46be2012-06-27 10:05:39 +08001089 enum thermal_trend trend;
1090
Zhang Rui908b9fb2012-06-27 14:14:05 +08001091 if (trip == THERMAL_TRIPS_NONE) {
1092 trip_temp = tz->forced_passive;
1093 trip_type = THERMAL_TRIPS_NONE;
1094 } else {
1095 tz->ops->get_trip_temp(tz, trip, &trip_temp);
1096 tz->ops->get_trip_type(tz, trip, &trip_type);
1097 }
Zhang Rui4ae46be2012-06-27 10:05:39 +08001098
1099 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
1100 /*
1101 * compare the current temperature and previous temperature
1102 * to get the thermal trend, if no special requirement
1103 */
1104 if (tz->temperature > tz->last_temperature)
1105 trend = THERMAL_TREND_RAISING;
1106 else if (tz->temperature < tz->last_temperature)
1107 trend = THERMAL_TREND_DROPPING;
1108 else
1109 trend = THERMAL_TREND_STABLE;
1110 }
1111
1112 if (temp >= trip_temp) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001113 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
Zhang Rui4ae46be2012-06-27 10:05:39 +08001114 if (instance->trip != trip)
1115 continue;
1116
1117 cdev = instance->cdev;
1118
1119 cdev->ops->get_cur_state(cdev, &cur_state);
1120 cdev->ops->get_max_state(cdev, &max_state);
1121
1122 if (trend == THERMAL_TREND_RAISING) {
1123 cur_state = cur_state < instance->upper ?
1124 (cur_state + 1) : instance->upper;
1125 } else if (trend == THERMAL_TREND_DROPPING) {
1126 cur_state = cur_state > instance->lower ?
1127 (cur_state - 1) : instance->lower;
1128 }
Zhang Rui908b9fb2012-06-27 14:14:05 +08001129
1130 /* activate a passive thermal instance */
1131 if ((trip_type == THERMAL_TRIP_PASSIVE ||
1132 trip_type == THERMAL_TRIPS_NONE) &&
1133 instance->target == THERMAL_NO_TARGET)
1134 tz->passive++;
1135
Zhang Ruice119f82012-06-27 14:13:04 +08001136 instance->target = cur_state;
1137 cdev->updated = false; /* cooling device needs update */
Zhang Rui4ae46be2012-06-27 10:05:39 +08001138 }
1139 } else { /* below trip */
Zhang Ruicddf31b2012-06-27 10:09:36 +08001140 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
Zhang Rui4ae46be2012-06-27 10:05:39 +08001141 if (instance->trip != trip)
1142 continue;
1143
Zhang Ruice119f82012-06-27 14:13:04 +08001144 /* Do not use the inactive thermal instance */
1145 if (instance->target == THERMAL_NO_TARGET)
1146 continue;
Zhang Rui4ae46be2012-06-27 10:05:39 +08001147 cdev = instance->cdev;
1148 cdev->ops->get_cur_state(cdev, &cur_state);
1149
1150 cur_state = cur_state > instance->lower ?
Zhang Ruice119f82012-06-27 14:13:04 +08001151 (cur_state - 1) : THERMAL_NO_TARGET;
Zhang Rui908b9fb2012-06-27 14:14:05 +08001152
1153 /* deactivate a passive thermal instance */
1154 if ((trip_type == THERMAL_TRIP_PASSIVE ||
1155 trip_type == THERMAL_TRIPS_NONE) &&
1156 cur_state == THERMAL_NO_TARGET)
1157 tz->passive--;
Zhang Ruice119f82012-06-27 14:13:04 +08001158 instance->target = cur_state;
1159 cdev->updated = false; /* cooling device needs update */
Zhang Rui4ae46be2012-06-27 10:05:39 +08001160 }
1161 }
1162
1163 return;
1164}
Zhang Rui203d3d42008-01-17 15:51:08 +08001165/**
Matthew Garrettb1569e92008-12-03 17:55:32 +00001166 * thermal_zone_device_update - force an update of a thermal zone's state
1167 * @ttz: the thermal zone to update
1168 */
1169
1170void thermal_zone_device_update(struct thermal_zone_device *tz)
1171{
1172 int count, ret = 0;
1173 long temp, trip_temp;
1174 enum thermal_trip_type trip_type;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001175
1176 mutex_lock(&tz->lock);
1177
Michael Brunner0d288162009-08-26 14:29:25 -07001178 if (tz->ops->get_temp(tz, &temp)) {
1179 /* get_temp failed - retry it later */
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001180 pr_warn("failed to read out thermal zone %d\n", tz->id);
Michael Brunner0d288162009-08-26 14:29:25 -07001181 goto leave;
1182 }
Matthew Garrettb1569e92008-12-03 17:55:32 +00001183
Zhang Rui601f3d42012-06-27 09:54:33 +08001184 tz->last_temperature = tz->temperature;
1185 tz->temperature = temp;
1186
Matthew Garrettb1569e92008-12-03 17:55:32 +00001187 for (count = 0; count < tz->trips; count++) {
1188 tz->ops->get_trip_type(tz, count, &trip_type);
1189 tz->ops->get_trip_temp(tz, count, &trip_temp);
1190
1191 switch (trip_type) {
1192 case THERMAL_TRIP_CRITICAL:
Vladimir Zajac29321352009-05-06 19:34:21 +02001193 if (temp >= trip_temp) {
Matthew Garrettb1569e92008-12-03 17:55:32 +00001194 if (tz->ops->notify)
1195 ret = tz->ops->notify(tz, count,
1196 trip_type);
1197 if (!ret) {
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001198 pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1199 temp/1000);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001200 orderly_poweroff(true);
1201 }
1202 }
1203 break;
1204 case THERMAL_TRIP_HOT:
Vladimir Zajac29321352009-05-06 19:34:21 +02001205 if (temp >= trip_temp)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001206 if (tz->ops->notify)
1207 tz->ops->notify(tz, count, trip_type);
1208 break;
1209 case THERMAL_TRIP_ACTIVE:
Zhang Rui4ae46be2012-06-27 10:05:39 +08001210 thermal_zone_trip_update(tz, count, temp);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001211 break;
1212 case THERMAL_TRIP_PASSIVE:
Vladimir Zajac29321352009-05-06 19:34:21 +02001213 if (temp >= trip_temp || tz->passive)
Zhang Rui908b9fb2012-06-27 14:14:05 +08001214 thermal_zone_trip_update(tz, count, temp);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001215 break;
1216 }
1217 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001218
1219 if (tz->forced_passive)
Zhang Rui908b9fb2012-06-27 14:14:05 +08001220 thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE, temp);
1221 thermal_zone_do_update(tz);
Michael Brunner0d288162009-08-26 14:29:25 -07001222
Joe Perchescaca8b82012-03-21 12:55:02 -07001223leave:
Matthew Garrettb1569e92008-12-03 17:55:32 +00001224 if (tz->passive)
1225 thermal_zone_device_set_polling(tz, tz->passive_delay);
1226 else if (tz->polling_delay)
1227 thermal_zone_device_set_polling(tz, tz->polling_delay);
Frans Pop3767cb52009-10-26 08:39:04 +01001228 else
1229 thermal_zone_device_set_polling(tz, 0);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001230 mutex_unlock(&tz->lock);
1231}
1232EXPORT_SYMBOL(thermal_zone_device_update);
1233
1234/**
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001235 * create_trip_attrs - create attributes for trip points
1236 * @tz: the thermal zone device
1237 * @mask: Writeable trip point bitmap.
1238 */
1239static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1240{
1241 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001242 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001243
Durgadoss R27365a62012-07-25 10:10:59 +08001244 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001245 if (!tz->trip_type_attrs)
1246 return -ENOMEM;
1247
Durgadoss R27365a62012-07-25 10:10:59 +08001248 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001249 if (!tz->trip_temp_attrs) {
1250 kfree(tz->trip_type_attrs);
1251 return -ENOMEM;
1252 }
1253
Durgadoss R27365a62012-07-25 10:10:59 +08001254 if (tz->ops->get_trip_hyst) {
1255 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1256 if (!tz->trip_hyst_attrs) {
1257 kfree(tz->trip_type_attrs);
1258 kfree(tz->trip_temp_attrs);
1259 return -ENOMEM;
1260 }
1261 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001262
Durgadoss R27365a62012-07-25 10:10:59 +08001263
1264 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001265 /* create trip type attribute */
1266 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1267 "trip_point_%d_type", indx);
1268
1269 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1270 tz->trip_type_attrs[indx].attr.attr.name =
1271 tz->trip_type_attrs[indx].name;
1272 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1273 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1274
1275 device_create_file(&tz->device,
1276 &tz->trip_type_attrs[indx].attr);
1277
1278 /* create trip temp attribute */
1279 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1280 "trip_point_%d_temp", indx);
1281
1282 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1283 tz->trip_temp_attrs[indx].attr.attr.name =
1284 tz->trip_temp_attrs[indx].name;
1285 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1286 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1287 if (mask & (1 << indx)) {
1288 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1289 tz->trip_temp_attrs[indx].attr.store =
1290 trip_point_temp_store;
1291 }
1292
1293 device_create_file(&tz->device,
1294 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001295
1296 /* create Optional trip hyst attribute */
1297 if (!tz->ops->get_trip_hyst)
1298 continue;
1299 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1300 "trip_point_%d_hyst", indx);
1301
1302 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1303 tz->trip_hyst_attrs[indx].attr.attr.name =
1304 tz->trip_hyst_attrs[indx].name;
1305 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1306 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1307 if (tz->ops->set_trip_hyst) {
1308 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1309 tz->trip_hyst_attrs[indx].attr.store =
1310 trip_point_hyst_store;
1311 }
1312
1313 device_create_file(&tz->device,
1314 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001315 }
1316 return 0;
1317}
1318
1319static void remove_trip_attrs(struct thermal_zone_device *tz)
1320{
1321 int indx;
1322
1323 for (indx = 0; indx < tz->trips; indx++) {
1324 device_remove_file(&tz->device,
1325 &tz->trip_type_attrs[indx].attr);
1326 device_remove_file(&tz->device,
1327 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001328 if (tz->ops->get_trip_hyst)
1329 device_remove_file(&tz->device,
1330 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001331 }
1332 kfree(tz->trip_type_attrs);
1333 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001334 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001335}
1336
1337/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001338 * thermal_zone_device_register - register a new thermal zone device
1339 * @type: the thermal zone device type
1340 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001341 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001342 * @devdata: private device data
1343 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301344 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001345 * @passive_delay: number of milliseconds to wait between polls when
1346 * performing passive cooling
1347 * @polling_delay: number of milliseconds to wait between polls when checking
1348 * whether trip points have been crossed (0 for interrupt
1349 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001350 *
1351 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001352 * longer needed. The passive cooling depends on the .get_trend() return value.
Zhang Rui203d3d42008-01-17 15:51:08 +08001353 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001354struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001355 int trips, int mask, void *devdata,
Alan Cox5b275ce2010-11-11 15:27:29 +00001356 const struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301357 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001358 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001359{
1360 struct thermal_zone_device *tz;
1361 struct thermal_cooling_device *pos;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001362 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001363 int result;
1364 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001365 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001366
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001367 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001368 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001369
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001370 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001371 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001372
1373 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001374 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001375
1376 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1377 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001378 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001379
Zhang Rui2d374132012-06-27 10:09:00 +08001380 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001381 idr_init(&tz->idr);
1382 mutex_init(&tz->lock);
1383 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1384 if (result) {
1385 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001386 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001387 }
1388
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001389 strcpy(tz->type, type ? : "");
Zhang Rui203d3d42008-01-17 15:51:08 +08001390 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301391 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001392 tz->device.class = &thermal_class;
1393 tz->devdata = devdata;
1394 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001395 tz->passive_delay = passive_delay;
1396 tz->polling_delay = polling_delay;
1397
Kay Sievers354655e2009-01-06 10:44:37 -08001398 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001399 result = device_register(&tz->device);
1400 if (result) {
1401 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1402 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001403 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001404 }
1405
1406 /* sys I/F */
1407 if (type) {
1408 result = device_create_file(&tz->device, &dev_attr_type);
1409 if (result)
1410 goto unregister;
1411 }
1412
1413 result = device_create_file(&tz->device, &dev_attr_temp);
1414 if (result)
1415 goto unregister;
1416
1417 if (ops->get_mode) {
1418 result = device_create_file(&tz->device, &dev_attr_mode);
1419 if (result)
1420 goto unregister;
1421 }
1422
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001423 result = create_trip_attrs(tz, mask);
1424 if (result)
1425 goto unregister;
1426
Zhang Rui203d3d42008-01-17 15:51:08 +08001427 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001428 tz->ops->get_trip_type(tz, count, &trip_type);
1429 if (trip_type == THERMAL_TRIP_PASSIVE)
1430 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001431 }
1432
Matthew Garrett03a971a2008-12-03 18:00:38 +00001433 if (!passive)
1434 result = device_create_file(&tz->device,
1435 &dev_attr_passive);
1436
1437 if (result)
1438 goto unregister;
1439
Zhang Ruie68b16a2008-04-21 16:07:52 +08001440 result = thermal_add_hwmon_sysfs(tz);
1441 if (result)
1442 goto unregister;
1443
Zhang Rui203d3d42008-01-17 15:51:08 +08001444 mutex_lock(&thermal_list_lock);
1445 list_add_tail(&tz->node, &thermal_tz_list);
1446 if (ops->bind)
1447 list_for_each_entry(pos, &thermal_cdev_list, node) {
Len Brown543a9562008-02-07 16:55:08 -05001448 result = ops->bind(tz, pos);
1449 if (result)
1450 break;
Zhang Rui203d3d42008-01-17 15:51:08 +08001451 }
1452 mutex_unlock(&thermal_list_lock);
1453
Matthew Garrettb1569e92008-12-03 17:55:32 +00001454 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1455
1456 thermal_zone_device_update(tz);
1457
Zhang Rui203d3d42008-01-17 15:51:08 +08001458 if (!result)
1459 return tz;
1460
Joe Perchescaca8b82012-03-21 12:55:02 -07001461unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001462 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1463 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001464 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001465}
1466EXPORT_SYMBOL(thermal_zone_device_register);
1467
1468/**
1469 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001470 * @tz: the thermal zone device to remove
1471 */
1472void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1473{
1474 struct thermal_cooling_device *cdev;
1475 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001476
1477 if (!tz)
1478 return;
1479
1480 mutex_lock(&thermal_list_lock);
1481 list_for_each_entry(pos, &thermal_tz_list, node)
1482 if (pos == tz)
1483 break;
1484 if (pos != tz) {
1485 /* thermal zone device not found */
1486 mutex_unlock(&thermal_list_lock);
1487 return;
1488 }
1489 list_del(&tz->node);
1490 if (tz->ops->unbind)
1491 list_for_each_entry(cdev, &thermal_cdev_list, node)
1492 tz->ops->unbind(tz, cdev);
1493 mutex_unlock(&thermal_list_lock);
1494
Matthew Garrettb1569e92008-12-03 17:55:32 +00001495 thermal_zone_device_set_polling(tz, 0);
1496
Zhang Rui203d3d42008-01-17 15:51:08 +08001497 if (tz->type[0])
1498 device_remove_file(&tz->device, &dev_attr_type);
1499 device_remove_file(&tz->device, &dev_attr_temp);
1500 if (tz->ops->get_mode)
1501 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001502 remove_trip_attrs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001503
Zhang Ruie68b16a2008-04-21 16:07:52 +08001504 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001505 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1506 idr_destroy(&tz->idr);
1507 mutex_destroy(&tz->lock);
1508 device_unregister(&tz->device);
1509 return;
1510}
Zhang Rui203d3d42008-01-17 15:51:08 +08001511EXPORT_SYMBOL(thermal_zone_device_unregister);
1512
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001513#ifdef CONFIG_NET
1514static struct genl_family thermal_event_genl_family = {
1515 .id = GENL_ID_GENERATE,
1516 .name = THERMAL_GENL_FAMILY_NAME,
1517 .version = THERMAL_GENL_VERSION,
1518 .maxattr = THERMAL_GENL_ATTR_MAX,
1519};
1520
1521static struct genl_multicast_group thermal_event_mcgrp = {
1522 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1523};
1524
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001525int thermal_generate_netlink_event(u32 orig, enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301526{
1527 struct sk_buff *skb;
1528 struct nlattr *attr;
1529 struct thermal_genl_event *thermal_event;
1530 void *msg_header;
1531 int size;
1532 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001533 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301534
1535 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001536 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1537 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301538
1539 skb = genlmsg_new(size, GFP_ATOMIC);
1540 if (!skb)
1541 return -ENOMEM;
1542
1543 /* add the genetlink message header */
1544 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1545 &thermal_event_genl_family, 0,
1546 THERMAL_GENL_CMD_EVENT);
1547 if (!msg_header) {
1548 nlmsg_free(skb);
1549 return -ENOMEM;
1550 }
1551
1552 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001553 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1554 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301555
1556 if (!attr) {
1557 nlmsg_free(skb);
1558 return -EINVAL;
1559 }
1560
1561 thermal_event = nla_data(attr);
1562 if (!thermal_event) {
1563 nlmsg_free(skb);
1564 return -EINVAL;
1565 }
1566
1567 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1568
1569 thermal_event->orig = orig;
1570 thermal_event->event = event;
1571
1572 /* send multicast genetlink message */
1573 result = genlmsg_end(skb, msg_header);
1574 if (result < 0) {
1575 nlmsg_free(skb);
1576 return result;
1577 }
1578
1579 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1580 if (result)
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001581 pr_info("failed to send netlink event:%d\n", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301582
1583 return result;
1584}
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001585EXPORT_SYMBOL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301586
1587static int genetlink_init(void)
1588{
1589 int result;
1590
1591 result = genl_register_family(&thermal_event_genl_family);
1592 if (result)
1593 return result;
1594
1595 result = genl_register_mc_group(&thermal_event_genl_family,
1596 &thermal_event_mcgrp);
1597 if (result)
1598 genl_unregister_family(&thermal_event_genl_family);
1599 return result;
1600}
1601
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001602static void genetlink_exit(void)
1603{
1604 genl_unregister_family(&thermal_event_genl_family);
1605}
1606#else /* !CONFIG_NET */
1607static inline int genetlink_init(void) { return 0; }
1608static inline void genetlink_exit(void) {}
1609#endif /* !CONFIG_NET */
1610
Zhang Rui203d3d42008-01-17 15:51:08 +08001611static int __init thermal_init(void)
1612{
1613 int result = 0;
1614
1615 result = class_register(&thermal_class);
1616 if (result) {
1617 idr_destroy(&thermal_tz_idr);
1618 idr_destroy(&thermal_cdev_idr);
1619 mutex_destroy(&thermal_idr_lock);
1620 mutex_destroy(&thermal_list_lock);
1621 }
R.Durgadoss4cb18722010-10-27 03:33:29 +05301622 result = genetlink_init();
Zhang Rui203d3d42008-01-17 15:51:08 +08001623 return result;
1624}
1625
1626static void __exit thermal_exit(void)
1627{
1628 class_unregister(&thermal_class);
1629 idr_destroy(&thermal_tz_idr);
1630 idr_destroy(&thermal_cdev_idr);
1631 mutex_destroy(&thermal_idr_lock);
1632 mutex_destroy(&thermal_list_lock);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301633 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001634}
1635
R.Durgadoss4cb18722010-10-27 03:33:29 +05301636fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001637module_exit(thermal_exit);