blob: dc4044b682a1afa9c586ba70c03ef74f4e29eb1e [file] [log] [blame]
Zhang Rui203d3d42008-01-17 15:51:08 +08001/*
2 * thermal.c - Generic Thermal Management Sysfs support.
3 *
4 * Copyright (C) 2008 Intel Corp
5 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
6 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
7 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
25
Joe Perchesc5a01dd2012-03-21 12:55:02 -070026#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
Zhang Rui203d3d42008-01-17 15:51:08 +080028#include <linux/module.h>
29#include <linux/device.h>
30#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080032#include <linux/kdev_t.h>
33#include <linux/idr.h>
34#include <linux/thermal.h>
35#include <linux/spinlock.h>
Matthew Garrettb1569e92008-12-03 17:55:32 +000036#include <linux/reboot.h>
R.Durgadoss4cb18722010-10-27 03:33:29 +053037#include <net/netlink.h>
38#include <net/genetlink.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080039
Zhang Rui63c4ec92008-04-21 16:07:13 +080040MODULE_AUTHOR("Zhang Rui");
Zhang Rui203d3d42008-01-17 15:51:08 +080041MODULE_DESCRIPTION("Generic thermal management sysfs support");
42MODULE_LICENSE("GPL");
43
Zhang Rui203d3d42008-01-17 15:51:08 +080044struct thermal_cooling_device_instance {
45 int id;
46 char name[THERMAL_NAME_LENGTH];
47 struct thermal_zone_device *tz;
48 struct thermal_cooling_device *cdev;
49 int trip;
50 char attr_name[THERMAL_NAME_LENGTH];
51 struct device_attribute attr;
52 struct list_head node;
53};
54
55static DEFINE_IDR(thermal_tz_idr);
56static DEFINE_IDR(thermal_cdev_idr);
57static DEFINE_MUTEX(thermal_idr_lock);
58
59static LIST_HEAD(thermal_tz_list);
60static LIST_HEAD(thermal_cdev_list);
61static DEFINE_MUTEX(thermal_list_lock);
62
63static int get_idr(struct idr *idr, struct mutex *lock, int *id)
64{
65 int err;
66
Joe Perchescaca8b82012-03-21 12:55:02 -070067again:
Zhang Rui203d3d42008-01-17 15:51:08 +080068 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
69 return -ENOMEM;
70
71 if (lock)
72 mutex_lock(lock);
73 err = idr_get_new(idr, NULL, id);
74 if (lock)
75 mutex_unlock(lock);
76 if (unlikely(err == -EAGAIN))
77 goto again;
78 else if (unlikely(err))
79 return err;
80
81 *id = *id & MAX_ID_MASK;
82 return 0;
83}
84
85static void release_idr(struct idr *idr, struct mutex *lock, int id)
86{
87 if (lock)
88 mutex_lock(lock);
89 idr_remove(idr, id);
90 if (lock)
91 mutex_unlock(lock);
92}
93
94/* sys I/F for thermal zone */
95
96#define to_thermal_zone(_dev) \
97 container_of(_dev, struct thermal_zone_device, device)
98
99static ssize_t
100type_show(struct device *dev, struct device_attribute *attr, char *buf)
101{
102 struct thermal_zone_device *tz = to_thermal_zone(dev);
103
104 return sprintf(buf, "%s\n", tz->type);
105}
106
107static ssize_t
108temp_show(struct device *dev, struct device_attribute *attr, char *buf)
109{
110 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000111 long temperature;
112 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800113
114 if (!tz->ops->get_temp)
115 return -EPERM;
116
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000117 ret = tz->ops->get_temp(tz, &temperature);
118
119 if (ret)
120 return ret;
121
122 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800123}
124
125static ssize_t
126mode_show(struct device *dev, struct device_attribute *attr, char *buf)
127{
128 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000129 enum thermal_device_mode mode;
130 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800131
132 if (!tz->ops->get_mode)
133 return -EPERM;
134
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000135 result = tz->ops->get_mode(tz, &mode);
136 if (result)
137 return result;
138
139 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
140 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800141}
142
143static ssize_t
144mode_store(struct device *dev, struct device_attribute *attr,
145 const char *buf, size_t count)
146{
147 struct thermal_zone_device *tz = to_thermal_zone(dev);
148 int result;
149
150 if (!tz->ops->set_mode)
151 return -EPERM;
152
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530153 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000154 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530155 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000156 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
157 else
158 result = -EINVAL;
159
Zhang Rui203d3d42008-01-17 15:51:08 +0800160 if (result)
161 return result;
162
163 return count;
164}
165
166static ssize_t
167trip_point_type_show(struct device *dev, struct device_attribute *attr,
168 char *buf)
169{
170 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000171 enum thermal_trip_type type;
172 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800173
174 if (!tz->ops->get_trip_type)
175 return -EPERM;
176
177 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
178 return -EINVAL;
179
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000180 result = tz->ops->get_trip_type(tz, trip, &type);
181 if (result)
182 return result;
183
184 switch (type) {
185 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300186 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000187 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300188 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000189 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300190 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000191 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300192 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000193 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300194 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000195 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800196}
197
198static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800199trip_point_temp_store(struct device *dev, struct device_attribute *attr,
200 const char *buf, size_t count)
201{
202 struct thermal_zone_device *tz = to_thermal_zone(dev);
203 int trip, ret;
204 unsigned long temperature;
205
206 if (!tz->ops->set_trip_temp)
207 return -EPERM;
208
209 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
210 return -EINVAL;
211
212 if (kstrtoul(buf, 10, &temperature))
213 return -EINVAL;
214
215 ret = tz->ops->set_trip_temp(tz, trip, temperature);
216
217 return ret ? ret : count;
218}
219
220static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800221trip_point_temp_show(struct device *dev, struct device_attribute *attr,
222 char *buf)
223{
224 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000225 int trip, ret;
226 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800227
228 if (!tz->ops->get_trip_temp)
229 return -EPERM;
230
231 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
232 return -EINVAL;
233
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000234 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
235
236 if (ret)
237 return ret;
238
239 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800240}
241
Matthew Garrett03a971a2008-12-03 18:00:38 +0000242static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800243trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
244 const char *buf, size_t count)
245{
246 struct thermal_zone_device *tz = to_thermal_zone(dev);
247 int trip, ret;
248 unsigned long temperature;
249
250 if (!tz->ops->set_trip_hyst)
251 return -EPERM;
252
253 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
254 return -EINVAL;
255
256 if (kstrtoul(buf, 10, &temperature))
257 return -EINVAL;
258
259 /*
260 * We are not doing any check on the 'temperature' value
261 * here. The driver implementing 'set_trip_hyst' has to
262 * take care of this.
263 */
264 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
265
266 return ret ? ret : count;
267}
268
269static ssize_t
270trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
271 char *buf)
272{
273 struct thermal_zone_device *tz = to_thermal_zone(dev);
274 int trip, ret;
275 unsigned long temperature;
276
277 if (!tz->ops->get_trip_hyst)
278 return -EPERM;
279
280 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
281 return -EINVAL;
282
283 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
284
285 return ret ? ret : sprintf(buf, "%ld\n", temperature);
286}
287
288static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000289passive_store(struct device *dev, struct device_attribute *attr,
290 const char *buf, size_t count)
291{
292 struct thermal_zone_device *tz = to_thermal_zone(dev);
293 struct thermal_cooling_device *cdev = NULL;
294 int state;
295
296 if (!sscanf(buf, "%d\n", &state))
297 return -EINVAL;
298
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100299 /* sanity check: values below 1000 millicelcius don't make sense
300 * and can cause the system to go into a thermal heart attack
301 */
302 if (state && state < 1000)
303 return -EINVAL;
304
Matthew Garrett03a971a2008-12-03 18:00:38 +0000305 if (state && !tz->forced_passive) {
306 mutex_lock(&thermal_list_lock);
307 list_for_each_entry(cdev, &thermal_cdev_list, node) {
308 if (!strncmp("Processor", cdev->type,
309 sizeof("Processor")))
310 thermal_zone_bind_cooling_device(tz,
311 THERMAL_TRIPS_NONE,
312 cdev);
313 }
314 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100315 if (!tz->passive_delay)
316 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000317 } else if (!state && tz->forced_passive) {
318 mutex_lock(&thermal_list_lock);
319 list_for_each_entry(cdev, &thermal_cdev_list, node) {
320 if (!strncmp("Processor", cdev->type,
321 sizeof("Processor")))
322 thermal_zone_unbind_cooling_device(tz,
323 THERMAL_TRIPS_NONE,
324 cdev);
325 }
326 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100327 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000328 }
329
330 tz->tc1 = 1;
331 tz->tc2 = 1;
332
Matthew Garrett03a971a2008-12-03 18:00:38 +0000333 tz->forced_passive = state;
334
335 thermal_zone_device_update(tz);
336
337 return count;
338}
339
340static ssize_t
341passive_show(struct device *dev, struct device_attribute *attr,
342 char *buf)
343{
344 struct thermal_zone_device *tz = to_thermal_zone(dev);
345
346 return sprintf(buf, "%d\n", tz->forced_passive);
347}
348
Zhang Rui203d3d42008-01-17 15:51:08 +0800349static DEVICE_ATTR(type, 0444, type_show, NULL);
350static DEVICE_ATTR(temp, 0444, temp_show, NULL);
351static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700352static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800353
Zhang Rui203d3d42008-01-17 15:51:08 +0800354/* sys I/F for cooling device */
355#define to_cooling_device(_dev) \
356 container_of(_dev, struct thermal_cooling_device, device)
357
358static ssize_t
359thermal_cooling_device_type_show(struct device *dev,
360 struct device_attribute *attr, char *buf)
361{
362 struct thermal_cooling_device *cdev = to_cooling_device(dev);
363
364 return sprintf(buf, "%s\n", cdev->type);
365}
366
367static ssize_t
368thermal_cooling_device_max_state_show(struct device *dev,
369 struct device_attribute *attr, char *buf)
370{
371 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000372 unsigned long state;
373 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800374
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000375 ret = cdev->ops->get_max_state(cdev, &state);
376 if (ret)
377 return ret;
378 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800379}
380
381static ssize_t
382thermal_cooling_device_cur_state_show(struct device *dev,
383 struct device_attribute *attr, char *buf)
384{
385 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000386 unsigned long state;
387 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800388
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000389 ret = cdev->ops->get_cur_state(cdev, &state);
390 if (ret)
391 return ret;
392 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800393}
394
395static ssize_t
396thermal_cooling_device_cur_state_store(struct device *dev,
397 struct device_attribute *attr,
398 const char *buf, size_t count)
399{
400 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000401 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800402 int result;
403
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000404 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800405 return -EINVAL;
406
Roel Kluinedb94912009-12-15 22:46:50 +0100407 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800408 return -EINVAL;
409
410 result = cdev->ops->set_cur_state(cdev, state);
411 if (result)
412 return result;
413 return count;
414}
415
416static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500417__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800418static DEVICE_ATTR(max_state, 0444,
419 thermal_cooling_device_max_state_show, NULL);
420static DEVICE_ATTR(cur_state, 0644,
421 thermal_cooling_device_cur_state_show,
422 thermal_cooling_device_cur_state_store);
423
424static ssize_t
425thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500426 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800427{
428 struct thermal_cooling_device_instance *instance;
429
430 instance =
431 container_of(attr, struct thermal_cooling_device_instance, attr);
432
433 if (instance->trip == THERMAL_TRIPS_NONE)
434 return sprintf(buf, "-1\n");
435 else
436 return sprintf(buf, "%d\n", instance->trip);
437}
438
439/* Device management */
440
Rene Herman16d75232008-06-24 19:38:56 +0200441#if defined(CONFIG_THERMAL_HWMON)
442
Zhang Ruie68b16a2008-04-21 16:07:52 +0800443/* hwmon sys I/F */
444#include <linux/hwmon.h>
Jean Delvare31f53962011-07-28 13:48:42 -0700445
446/* thermal zone devices with the same type share one hwmon device */
447struct thermal_hwmon_device {
448 char type[THERMAL_NAME_LENGTH];
449 struct device *device;
450 int count;
451 struct list_head tz_list;
452 struct list_head node;
453};
454
455struct thermal_hwmon_attr {
456 struct device_attribute attr;
457 char name[16];
458};
459
460/* one temperature input for each thermal zone */
461struct thermal_hwmon_temp {
462 struct list_head hwmon_node;
463 struct thermal_zone_device *tz;
464 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
465 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
466};
467
Zhang Ruie68b16a2008-04-21 16:07:52 +0800468static LIST_HEAD(thermal_hwmon_list);
469
470static ssize_t
471name_show(struct device *dev, struct device_attribute *attr, char *buf)
472{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700473 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800474 return sprintf(buf, "%s\n", hwmon->type);
475}
476static DEVICE_ATTR(name, 0444, name_show, NULL);
477
478static ssize_t
479temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
480{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000481 long temperature;
482 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800483 struct thermal_hwmon_attr *hwmon_attr
484 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700485 struct thermal_hwmon_temp *temp
486 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800487 temp_input);
Jean Delvare31f53962011-07-28 13:48:42 -0700488 struct thermal_zone_device *tz = temp->tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800489
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000490 ret = tz->ops->get_temp(tz, &temperature);
491
492 if (ret)
493 return ret;
494
495 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800496}
497
498static ssize_t
499temp_crit_show(struct device *dev, struct device_attribute *attr,
500 char *buf)
501{
502 struct thermal_hwmon_attr *hwmon_attr
503 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700504 struct thermal_hwmon_temp *temp
505 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800506 temp_crit);
Jean Delvare31f53962011-07-28 13:48:42 -0700507 struct thermal_zone_device *tz = temp->tz;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000508 long temperature;
509 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800510
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000511 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
512 if (ret)
513 return ret;
514
515 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800516}
517
518
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700519static struct thermal_hwmon_device *
520thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
521{
522 struct thermal_hwmon_device *hwmon;
523
524 mutex_lock(&thermal_list_lock);
525 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
526 if (!strcmp(hwmon->type, tz->type)) {
527 mutex_unlock(&thermal_list_lock);
528 return hwmon;
529 }
530 mutex_unlock(&thermal_list_lock);
531
532 return NULL;
533}
534
Jean Delvare31f53962011-07-28 13:48:42 -0700535/* Find the temperature input matching a given thermal zone */
536static struct thermal_hwmon_temp *
537thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
538 const struct thermal_zone_device *tz)
539{
540 struct thermal_hwmon_temp *temp;
541
542 mutex_lock(&thermal_list_lock);
543 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
544 if (temp->tz == tz) {
545 mutex_unlock(&thermal_list_lock);
546 return temp;
547 }
548 mutex_unlock(&thermal_list_lock);
549
550 return NULL;
551}
552
Zhang Ruie68b16a2008-04-21 16:07:52 +0800553static int
554thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
555{
556 struct thermal_hwmon_device *hwmon;
Jean Delvare31f53962011-07-28 13:48:42 -0700557 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800558 int new_hwmon_device = 1;
559 int result;
560
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700561 hwmon = thermal_hwmon_lookup_by_type(tz);
562 if (hwmon) {
563 new_hwmon_device = 0;
564 goto register_sys_interface;
565 }
Zhang Ruie68b16a2008-04-21 16:07:52 +0800566
567 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
568 if (!hwmon)
569 return -ENOMEM;
570
571 INIT_LIST_HEAD(&hwmon->tz_list);
572 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
573 hwmon->device = hwmon_device_register(NULL);
574 if (IS_ERR(hwmon->device)) {
575 result = PTR_ERR(hwmon->device);
576 goto free_mem;
577 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700578 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800579 result = device_create_file(hwmon->device, &dev_attr_name);
580 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530581 goto free_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800582
583 register_sys_interface:
Jean Delvare31f53962011-07-28 13:48:42 -0700584 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
585 if (!temp) {
586 result = -ENOMEM;
587 goto unregister_name;
588 }
589
590 temp->tz = tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800591 hwmon->count++;
592
Jean Delvare31f53962011-07-28 13:48:42 -0700593 snprintf(temp->temp_input.name, THERMAL_NAME_LENGTH,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800594 "temp%d_input", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700595 temp->temp_input.attr.attr.name = temp->temp_input.name;
596 temp->temp_input.attr.attr.mode = 0444;
597 temp->temp_input.attr.show = temp_input_show;
598 sysfs_attr_init(&temp->temp_input.attr.attr);
599 result = device_create_file(hwmon->device, &temp->temp_input.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800600 if (result)
Jean Delvare31f53962011-07-28 13:48:42 -0700601 goto free_temp_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800602
603 if (tz->ops->get_crit_temp) {
604 unsigned long temperature;
605 if (!tz->ops->get_crit_temp(tz, &temperature)) {
Jean Delvare31f53962011-07-28 13:48:42 -0700606 snprintf(temp->temp_crit.name, THERMAL_NAME_LENGTH,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800607 "temp%d_crit", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700608 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
609 temp->temp_crit.attr.attr.mode = 0444;
610 temp->temp_crit.attr.show = temp_crit_show;
611 sysfs_attr_init(&temp->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800612 result = device_create_file(hwmon->device,
Jean Delvare31f53962011-07-28 13:48:42 -0700613 &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800614 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530615 goto unregister_input;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800616 }
617 }
618
619 mutex_lock(&thermal_list_lock);
620 if (new_hwmon_device)
621 list_add_tail(&hwmon->node, &thermal_hwmon_list);
Jean Delvare31f53962011-07-28 13:48:42 -0700622 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800623 mutex_unlock(&thermal_list_lock);
624
625 return 0;
626
Durgadoss Rb299eb52011-03-03 04:30:13 +0530627 unregister_input:
Jean Delvare31f53962011-07-28 13:48:42 -0700628 device_remove_file(hwmon->device, &temp->temp_input.attr);
629 free_temp_mem:
630 kfree(temp);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530631 unregister_name:
Zhang Ruie68b16a2008-04-21 16:07:52 +0800632 if (new_hwmon_device) {
633 device_remove_file(hwmon->device, &dev_attr_name);
634 hwmon_device_unregister(hwmon->device);
635 }
636 free_mem:
637 if (new_hwmon_device)
638 kfree(hwmon);
639
640 return result;
641}
642
643static void
644thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
645{
Jean Delvare31f53962011-07-28 13:48:42 -0700646 struct thermal_hwmon_device *hwmon;
647 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800648
Jean Delvare31f53962011-07-28 13:48:42 -0700649 hwmon = thermal_hwmon_lookup_by_type(tz);
650 if (unlikely(!hwmon)) {
651 /* Should never happen... */
652 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
653 return;
654 }
655
656 temp = thermal_hwmon_lookup_temp(hwmon, tz);
657 if (unlikely(!temp)) {
658 /* Should never happen... */
659 dev_dbg(&tz->device, "temperature input lookup failed!\n");
660 return;
661 }
662
663 device_remove_file(hwmon->device, &temp->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530664 if (tz->ops->get_crit_temp)
Jean Delvare31f53962011-07-28 13:48:42 -0700665 device_remove_file(hwmon->device, &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800666
667 mutex_lock(&thermal_list_lock);
Jean Delvare31f53962011-07-28 13:48:42 -0700668 list_del(&temp->hwmon_node);
669 kfree(temp);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800670 if (!list_empty(&hwmon->tz_list)) {
671 mutex_unlock(&thermal_list_lock);
672 return;
673 }
674 list_del(&hwmon->node);
675 mutex_unlock(&thermal_list_lock);
676
677 device_remove_file(hwmon->device, &dev_attr_name);
678 hwmon_device_unregister(hwmon->device);
679 kfree(hwmon);
680}
681#else
682static int
683thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
684{
685 return 0;
686}
687
688static void
689thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
690{
691}
692#endif
693
Matthew Garrettb1569e92008-12-03 17:55:32 +0000694static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
695 int delay)
696{
697 cancel_delayed_work(&(tz->poll_queue));
698
699 if (!delay)
700 return;
701
702 if (delay > 1000)
Rafael J. Wysocki51e20d02011-11-06 14:21:38 +0100703 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
Matthew Garrettb1569e92008-12-03 17:55:32 +0000704 round_jiffies(msecs_to_jiffies(delay)));
705 else
Rafael J. Wysocki51e20d02011-11-06 14:21:38 +0100706 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
Matthew Garrettb1569e92008-12-03 17:55:32 +0000707 msecs_to_jiffies(delay));
708}
709
710static void thermal_zone_device_passive(struct thermal_zone_device *tz,
711 int temp, int trip_temp, int trip)
712{
713 int trend = 0;
714 struct thermal_cooling_device_instance *instance;
715 struct thermal_cooling_device *cdev;
716 long state, max_state;
717
718 /*
719 * Above Trip?
720 * -----------
721 * Calculate the thermal trend (using the passive cooling equation)
722 * and modify the performance limit for all passive cooling devices
723 * accordingly. Note that we assume symmetry.
724 */
725 if (temp >= trip_temp) {
726 tz->passive = true;
727
728 trend = (tz->tc1 * (temp - tz->last_temperature)) +
729 (tz->tc2 * (temp - trip_temp));
730
731 /* Heating up? */
732 if (trend > 0) {
733 list_for_each_entry(instance, &tz->cooling_devices,
734 node) {
735 if (instance->trip != trip)
736 continue;
737 cdev = instance->cdev;
738 cdev->ops->get_cur_state(cdev, &state);
739 cdev->ops->get_max_state(cdev, &max_state);
740 if (state++ < max_state)
741 cdev->ops->set_cur_state(cdev, state);
742 }
743 } else if (trend < 0) { /* Cooling off? */
744 list_for_each_entry(instance, &tz->cooling_devices,
745 node) {
746 if (instance->trip != trip)
747 continue;
748 cdev = instance->cdev;
749 cdev->ops->get_cur_state(cdev, &state);
750 cdev->ops->get_max_state(cdev, &max_state);
751 if (state > 0)
752 cdev->ops->set_cur_state(cdev, --state);
753 }
754 }
755 return;
756 }
757
758 /*
759 * Below Trip?
760 * -----------
761 * Implement passive cooling hysteresis to slowly increase performance
762 * and avoid thrashing around the passive trip point. Note that we
763 * assume symmetry.
764 */
765 list_for_each_entry(instance, &tz->cooling_devices, node) {
766 if (instance->trip != trip)
767 continue;
768 cdev = instance->cdev;
769 cdev->ops->get_cur_state(cdev, &state);
770 cdev->ops->get_max_state(cdev, &max_state);
771 if (state > 0)
772 cdev->ops->set_cur_state(cdev, --state);
773 if (state == 0)
774 tz->passive = false;
775 }
776}
777
778static void thermal_zone_device_check(struct work_struct *work)
779{
780 struct thermal_zone_device *tz = container_of(work, struct
781 thermal_zone_device,
782 poll_queue.work);
783 thermal_zone_device_update(tz);
784}
Zhang Ruie68b16a2008-04-21 16:07:52 +0800785
Zhang Rui203d3d42008-01-17 15:51:08 +0800786/**
787 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800788 * @tz: thermal zone device
789 * @trip: indicates which trip point the cooling devices is
790 * associated with in this thermal zone.
791 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500792 *
793 * This function is usually called in the thermal zone device .bind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800794 */
795int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
796 int trip,
797 struct thermal_cooling_device *cdev)
798{
799 struct thermal_cooling_device_instance *dev;
800 struct thermal_cooling_device_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500801 struct thermal_zone_device *pos1;
802 struct thermal_cooling_device *pos2;
Zhang Rui203d3d42008-01-17 15:51:08 +0800803 int result;
804
Len Brown543a9562008-02-07 16:55:08 -0500805 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800806 return -EINVAL;
807
Thomas Sujithc7516702008-02-15 00:58:50 -0500808 list_for_each_entry(pos1, &thermal_tz_list, node) {
809 if (pos1 == tz)
810 break;
811 }
812 list_for_each_entry(pos2, &thermal_cdev_list, node) {
813 if (pos2 == cdev)
814 break;
815 }
816
817 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800818 return -EINVAL;
819
820 dev =
821 kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL);
822 if (!dev)
823 return -ENOMEM;
824 dev->tz = tz;
825 dev->cdev = cdev;
826 dev->trip = trip;
827 result = get_idr(&tz->idr, &tz->lock, &dev->id);
828 if (result)
829 goto free_mem;
830
831 sprintf(dev->name, "cdev%d", dev->id);
832 result =
833 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
834 if (result)
835 goto release_idr;
836
837 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700838 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800839 dev->attr.attr.name = dev->attr_name;
840 dev->attr.attr.mode = 0444;
841 dev->attr.show = thermal_cooling_device_trip_point_show;
842 result = device_create_file(&tz->device, &dev->attr);
843 if (result)
844 goto remove_symbol_link;
845
846 mutex_lock(&tz->lock);
847 list_for_each_entry(pos, &tz->cooling_devices, node)
848 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
849 result = -EEXIST;
850 break;
851 }
852 if (!result)
853 list_add_tail(&dev->node, &tz->cooling_devices);
854 mutex_unlock(&tz->lock);
855
856 if (!result)
857 return 0;
858
859 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -0700860remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800861 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -0700862release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +0800863 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -0700864free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800865 kfree(dev);
866 return result;
867}
868EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
869
870/**
871 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800872 * @tz: thermal zone device
873 * @trip: indicates which trip point the cooling devices is
874 * associated with in this thermal zone.
875 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500876 *
877 * This function is usually called in the thermal zone device .unbind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800878 */
879int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
880 int trip,
881 struct thermal_cooling_device *cdev)
882{
883 struct thermal_cooling_device_instance *pos, *next;
884
885 mutex_lock(&tz->lock);
886 list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
Len Brown543a9562008-02-07 16:55:08 -0500887 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800888 list_del(&pos->node);
889 mutex_unlock(&tz->lock);
890 goto unbind;
891 }
892 }
893 mutex_unlock(&tz->lock);
894
895 return -ENODEV;
896
Joe Perchescaca8b82012-03-21 12:55:02 -0700897unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +0800898 device_remove_file(&tz->device, &pos->attr);
899 sysfs_remove_link(&tz->device.kobj, pos->name);
900 release_idr(&tz->idr, &tz->lock, pos->id);
901 kfree(pos);
902 return 0;
903}
904EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
905
906static void thermal_release(struct device *dev)
907{
908 struct thermal_zone_device *tz;
909 struct thermal_cooling_device *cdev;
910
Joe Perchescaca8b82012-03-21 12:55:02 -0700911 if (!strncmp(dev_name(dev), "thermal_zone",
912 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800913 tz = to_thermal_zone(dev);
914 kfree(tz);
915 } else {
916 cdev = to_cooling_device(dev);
917 kfree(cdev);
918 }
919}
920
921static struct class thermal_class = {
922 .name = "thermal",
923 .dev_release = thermal_release,
924};
925
926/**
927 * thermal_cooling_device_register - register a new thermal cooling device
928 * @type: the thermal cooling device type.
929 * @devdata: device private data.
930 * @ops: standard thermal cooling devices callbacks.
931 */
Joe Perchescaca8b82012-03-21 12:55:02 -0700932struct thermal_cooling_device *
933thermal_cooling_device_register(char *type, void *devdata,
934 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800935{
936 struct thermal_cooling_device *cdev;
937 struct thermal_zone_device *pos;
938 int result;
939
940 if (strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500941 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800942
943 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -0500944 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500945 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800946
947 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
948 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500949 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +0800950
951 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
952 if (result) {
953 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500954 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800955 }
956
957 strcpy(cdev->type, type);
958 cdev->ops = ops;
959 cdev->device.class = &thermal_class;
960 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -0800961 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800962 result = device_register(&cdev->device);
963 if (result) {
964 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
965 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500966 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800967 }
968
969 /* sys I/F */
970 if (type) {
Len Brown543a9562008-02-07 16:55:08 -0500971 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +0800972 if (result)
973 goto unregister;
974 }
975
976 result = device_create_file(&cdev->device, &dev_attr_max_state);
977 if (result)
978 goto unregister;
979
980 result = device_create_file(&cdev->device, &dev_attr_cur_state);
981 if (result)
982 goto unregister;
983
984 mutex_lock(&thermal_list_lock);
985 list_add(&cdev->node, &thermal_cdev_list);
986 list_for_each_entry(pos, &thermal_tz_list, node) {
987 if (!pos->ops->bind)
988 continue;
989 result = pos->ops->bind(pos, cdev);
990 if (result)
991 break;
992
993 }
994 mutex_unlock(&thermal_list_lock);
995
996 if (!result)
997 return cdev;
998
Joe Perchescaca8b82012-03-21 12:55:02 -0700999unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001000 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1001 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001002 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001003}
1004EXPORT_SYMBOL(thermal_cooling_device_register);
1005
1006/**
1007 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001008 * @cdev: the thermal cooling device to remove.
1009 *
1010 * thermal_cooling_device_unregister() must be called when the device is no
1011 * longer needed.
1012 */
1013void thermal_cooling_device_unregister(struct
1014 thermal_cooling_device
1015 *cdev)
1016{
1017 struct thermal_zone_device *tz;
1018 struct thermal_cooling_device *pos = NULL;
1019
1020 if (!cdev)
1021 return;
1022
1023 mutex_lock(&thermal_list_lock);
1024 list_for_each_entry(pos, &thermal_cdev_list, node)
1025 if (pos == cdev)
1026 break;
1027 if (pos != cdev) {
1028 /* thermal cooling device not found */
1029 mutex_unlock(&thermal_list_lock);
1030 return;
1031 }
1032 list_del(&cdev->node);
1033 list_for_each_entry(tz, &thermal_tz_list, node) {
1034 if (!tz->ops->unbind)
1035 continue;
1036 tz->ops->unbind(tz, cdev);
1037 }
1038 mutex_unlock(&thermal_list_lock);
1039 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001040 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001041 device_remove_file(&cdev->device, &dev_attr_max_state);
1042 device_remove_file(&cdev->device, &dev_attr_cur_state);
1043
1044 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1045 device_unregister(&cdev->device);
1046 return;
1047}
1048EXPORT_SYMBOL(thermal_cooling_device_unregister);
1049
1050/**
Matthew Garrettb1569e92008-12-03 17:55:32 +00001051 * thermal_zone_device_update - force an update of a thermal zone's state
1052 * @ttz: the thermal zone to update
1053 */
1054
1055void thermal_zone_device_update(struct thermal_zone_device *tz)
1056{
1057 int count, ret = 0;
1058 long temp, trip_temp;
1059 enum thermal_trip_type trip_type;
1060 struct thermal_cooling_device_instance *instance;
1061 struct thermal_cooling_device *cdev;
Zhang Ruie3f25e62012-06-26 16:26:40 +08001062 unsigned long cur_state, max_state;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001063
1064 mutex_lock(&tz->lock);
1065
Michael Brunner0d288162009-08-26 14:29:25 -07001066 if (tz->ops->get_temp(tz, &temp)) {
1067 /* get_temp failed - retry it later */
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001068 pr_warn("failed to read out thermal zone %d\n", tz->id);
Michael Brunner0d288162009-08-26 14:29:25 -07001069 goto leave;
1070 }
Matthew Garrettb1569e92008-12-03 17:55:32 +00001071
1072 for (count = 0; count < tz->trips; count++) {
1073 tz->ops->get_trip_type(tz, count, &trip_type);
1074 tz->ops->get_trip_temp(tz, count, &trip_temp);
1075
1076 switch (trip_type) {
1077 case THERMAL_TRIP_CRITICAL:
Vladimir Zajac29321352009-05-06 19:34:21 +02001078 if (temp >= trip_temp) {
Matthew Garrettb1569e92008-12-03 17:55:32 +00001079 if (tz->ops->notify)
1080 ret = tz->ops->notify(tz, count,
1081 trip_type);
1082 if (!ret) {
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001083 pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1084 temp/1000);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001085 orderly_poweroff(true);
1086 }
1087 }
1088 break;
1089 case THERMAL_TRIP_HOT:
Vladimir Zajac29321352009-05-06 19:34:21 +02001090 if (temp >= trip_temp)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001091 if (tz->ops->notify)
1092 tz->ops->notify(tz, count, trip_type);
1093 break;
1094 case THERMAL_TRIP_ACTIVE:
1095 list_for_each_entry(instance, &tz->cooling_devices,
1096 node) {
1097 if (instance->trip != count)
1098 continue;
1099
1100 cdev = instance->cdev;
1101
Zhang Ruie3f25e62012-06-26 16:26:40 +08001102 cdev->ops->get_cur_state(cdev, &cur_state);
1103 cdev->ops->get_max_state(cdev, &max_state);
1104
Vladimir Zajac29321352009-05-06 19:34:21 +02001105 if (temp >= trip_temp)
Zhang Ruie3f25e62012-06-26 16:26:40 +08001106 cur_state = cur_state < max_state ?
1107 (cur_state + 1) : max_state;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001108 else
Zhang Ruie3f25e62012-06-26 16:26:40 +08001109 cur_state = cur_state > 0 ?
1110 (cur_state - 1) : 0;
1111
1112 cdev->ops->set_cur_state(cdev, cur_state);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001113 }
1114 break;
1115 case THERMAL_TRIP_PASSIVE:
Vladimir Zajac29321352009-05-06 19:34:21 +02001116 if (temp >= trip_temp || tz->passive)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001117 thermal_zone_device_passive(tz, temp,
1118 trip_temp, count);
1119 break;
1120 }
1121 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001122
1123 if (tz->forced_passive)
1124 thermal_zone_device_passive(tz, temp, tz->forced_passive,
1125 THERMAL_TRIPS_NONE);
1126
Matthew Garrettb1569e92008-12-03 17:55:32 +00001127 tz->last_temperature = temp;
Michael Brunner0d288162009-08-26 14:29:25 -07001128
Joe Perchescaca8b82012-03-21 12:55:02 -07001129leave:
Matthew Garrettb1569e92008-12-03 17:55:32 +00001130 if (tz->passive)
1131 thermal_zone_device_set_polling(tz, tz->passive_delay);
1132 else if (tz->polling_delay)
1133 thermal_zone_device_set_polling(tz, tz->polling_delay);
Frans Pop3767cb52009-10-26 08:39:04 +01001134 else
1135 thermal_zone_device_set_polling(tz, 0);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001136 mutex_unlock(&tz->lock);
1137}
1138EXPORT_SYMBOL(thermal_zone_device_update);
1139
1140/**
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001141 * create_trip_attrs - create attributes for trip points
1142 * @tz: the thermal zone device
1143 * @mask: Writeable trip point bitmap.
1144 */
1145static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1146{
1147 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001148 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001149
Durgadoss R27365a62012-07-25 10:10:59 +08001150 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001151 if (!tz->trip_type_attrs)
1152 return -ENOMEM;
1153
Durgadoss R27365a62012-07-25 10:10:59 +08001154 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001155 if (!tz->trip_temp_attrs) {
1156 kfree(tz->trip_type_attrs);
1157 return -ENOMEM;
1158 }
1159
Durgadoss R27365a62012-07-25 10:10:59 +08001160 if (tz->ops->get_trip_hyst) {
1161 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1162 if (!tz->trip_hyst_attrs) {
1163 kfree(tz->trip_type_attrs);
1164 kfree(tz->trip_temp_attrs);
1165 return -ENOMEM;
1166 }
1167 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001168
Durgadoss R27365a62012-07-25 10:10:59 +08001169
1170 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001171 /* create trip type attribute */
1172 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1173 "trip_point_%d_type", indx);
1174
1175 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1176 tz->trip_type_attrs[indx].attr.attr.name =
1177 tz->trip_type_attrs[indx].name;
1178 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1179 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1180
1181 device_create_file(&tz->device,
1182 &tz->trip_type_attrs[indx].attr);
1183
1184 /* create trip temp attribute */
1185 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1186 "trip_point_%d_temp", indx);
1187
1188 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1189 tz->trip_temp_attrs[indx].attr.attr.name =
1190 tz->trip_temp_attrs[indx].name;
1191 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1192 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1193 if (mask & (1 << indx)) {
1194 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1195 tz->trip_temp_attrs[indx].attr.store =
1196 trip_point_temp_store;
1197 }
1198
1199 device_create_file(&tz->device,
1200 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001201
1202 /* create Optional trip hyst attribute */
1203 if (!tz->ops->get_trip_hyst)
1204 continue;
1205 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1206 "trip_point_%d_hyst", indx);
1207
1208 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1209 tz->trip_hyst_attrs[indx].attr.attr.name =
1210 tz->trip_hyst_attrs[indx].name;
1211 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1212 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1213 if (tz->ops->set_trip_hyst) {
1214 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1215 tz->trip_hyst_attrs[indx].attr.store =
1216 trip_point_hyst_store;
1217 }
1218
1219 device_create_file(&tz->device,
1220 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001221 }
1222 return 0;
1223}
1224
1225static void remove_trip_attrs(struct thermal_zone_device *tz)
1226{
1227 int indx;
1228
1229 for (indx = 0; indx < tz->trips; indx++) {
1230 device_remove_file(&tz->device,
1231 &tz->trip_type_attrs[indx].attr);
1232 device_remove_file(&tz->device,
1233 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001234 if (tz->ops->get_trip_hyst)
1235 device_remove_file(&tz->device,
1236 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001237 }
1238 kfree(tz->trip_type_attrs);
1239 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001240 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001241}
1242
1243/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001244 * thermal_zone_device_register - register a new thermal zone device
1245 * @type: the thermal zone device type
1246 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001247 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001248 * @devdata: private device data
1249 * @ops: standard thermal zone device callbacks
Matthew Garrettb1569e92008-12-03 17:55:32 +00001250 * @tc1: thermal coefficient 1 for passive calculations
1251 * @tc2: thermal coefficient 2 for passive calculations
1252 * @passive_delay: number of milliseconds to wait between polls when
1253 * performing passive cooling
1254 * @polling_delay: number of milliseconds to wait between polls when checking
1255 * whether trip points have been crossed (0 for interrupt
1256 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001257 *
1258 * thermal_zone_device_unregister() must be called when the device is no
Matthew Garrettb1569e92008-12-03 17:55:32 +00001259 * longer needed. The passive cooling formula uses tc1 and tc2 as described in
1260 * section 11.1.5.1 of the ACPI specification 3.0.
Zhang Rui203d3d42008-01-17 15:51:08 +08001261 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001262struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001263 int trips, int mask, void *devdata,
Alan Cox5b275ce2010-11-11 15:27:29 +00001264 const struct thermal_zone_device_ops *ops,
1265 int tc1, int tc2, int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001266{
1267 struct thermal_zone_device *tz;
1268 struct thermal_cooling_device *pos;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001269 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001270 int result;
1271 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001272 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001273
1274 if (strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001275 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001276
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001277 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001278 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001279
1280 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001281 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001282
1283 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1284 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001285 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001286
1287 INIT_LIST_HEAD(&tz->cooling_devices);
1288 idr_init(&tz->idr);
1289 mutex_init(&tz->lock);
1290 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1291 if (result) {
1292 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001293 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001294 }
1295
1296 strcpy(tz->type, type);
1297 tz->ops = ops;
1298 tz->device.class = &thermal_class;
1299 tz->devdata = devdata;
1300 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001301 tz->tc1 = tc1;
1302 tz->tc2 = tc2;
1303 tz->passive_delay = passive_delay;
1304 tz->polling_delay = polling_delay;
1305
Kay Sievers354655e2009-01-06 10:44:37 -08001306 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001307 result = device_register(&tz->device);
1308 if (result) {
1309 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1310 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001311 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001312 }
1313
1314 /* sys I/F */
1315 if (type) {
1316 result = device_create_file(&tz->device, &dev_attr_type);
1317 if (result)
1318 goto unregister;
1319 }
1320
1321 result = device_create_file(&tz->device, &dev_attr_temp);
1322 if (result)
1323 goto unregister;
1324
1325 if (ops->get_mode) {
1326 result = device_create_file(&tz->device, &dev_attr_mode);
1327 if (result)
1328 goto unregister;
1329 }
1330
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001331 result = create_trip_attrs(tz, mask);
1332 if (result)
1333 goto unregister;
1334
Zhang Rui203d3d42008-01-17 15:51:08 +08001335 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001336 tz->ops->get_trip_type(tz, count, &trip_type);
1337 if (trip_type == THERMAL_TRIP_PASSIVE)
1338 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001339 }
1340
Matthew Garrett03a971a2008-12-03 18:00:38 +00001341 if (!passive)
1342 result = device_create_file(&tz->device,
1343 &dev_attr_passive);
1344
1345 if (result)
1346 goto unregister;
1347
Zhang Ruie68b16a2008-04-21 16:07:52 +08001348 result = thermal_add_hwmon_sysfs(tz);
1349 if (result)
1350 goto unregister;
1351
Zhang Rui203d3d42008-01-17 15:51:08 +08001352 mutex_lock(&thermal_list_lock);
1353 list_add_tail(&tz->node, &thermal_tz_list);
1354 if (ops->bind)
1355 list_for_each_entry(pos, &thermal_cdev_list, node) {
Len Brown543a9562008-02-07 16:55:08 -05001356 result = ops->bind(tz, pos);
1357 if (result)
1358 break;
Zhang Rui203d3d42008-01-17 15:51:08 +08001359 }
1360 mutex_unlock(&thermal_list_lock);
1361
Matthew Garrettb1569e92008-12-03 17:55:32 +00001362 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1363
1364 thermal_zone_device_update(tz);
1365
Zhang Rui203d3d42008-01-17 15:51:08 +08001366 if (!result)
1367 return tz;
1368
Joe Perchescaca8b82012-03-21 12:55:02 -07001369unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001370 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1371 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001372 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001373}
1374EXPORT_SYMBOL(thermal_zone_device_register);
1375
1376/**
1377 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001378 * @tz: the thermal zone device to remove
1379 */
1380void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1381{
1382 struct thermal_cooling_device *cdev;
1383 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001384
1385 if (!tz)
1386 return;
1387
1388 mutex_lock(&thermal_list_lock);
1389 list_for_each_entry(pos, &thermal_tz_list, node)
1390 if (pos == tz)
1391 break;
1392 if (pos != tz) {
1393 /* thermal zone device not found */
1394 mutex_unlock(&thermal_list_lock);
1395 return;
1396 }
1397 list_del(&tz->node);
1398 if (tz->ops->unbind)
1399 list_for_each_entry(cdev, &thermal_cdev_list, node)
1400 tz->ops->unbind(tz, cdev);
1401 mutex_unlock(&thermal_list_lock);
1402
Matthew Garrettb1569e92008-12-03 17:55:32 +00001403 thermal_zone_device_set_polling(tz, 0);
1404
Zhang Rui203d3d42008-01-17 15:51:08 +08001405 if (tz->type[0])
1406 device_remove_file(&tz->device, &dev_attr_type);
1407 device_remove_file(&tz->device, &dev_attr_temp);
1408 if (tz->ops->get_mode)
1409 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001410 remove_trip_attrs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001411
Zhang Ruie68b16a2008-04-21 16:07:52 +08001412 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001413 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1414 idr_destroy(&tz->idr);
1415 mutex_destroy(&tz->lock);
1416 device_unregister(&tz->device);
1417 return;
1418}
Zhang Rui203d3d42008-01-17 15:51:08 +08001419EXPORT_SYMBOL(thermal_zone_device_unregister);
1420
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001421#ifdef CONFIG_NET
1422static struct genl_family thermal_event_genl_family = {
1423 .id = GENL_ID_GENERATE,
1424 .name = THERMAL_GENL_FAMILY_NAME,
1425 .version = THERMAL_GENL_VERSION,
1426 .maxattr = THERMAL_GENL_ATTR_MAX,
1427};
1428
1429static struct genl_multicast_group thermal_event_mcgrp = {
1430 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1431};
1432
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001433int thermal_generate_netlink_event(u32 orig, enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301434{
1435 struct sk_buff *skb;
1436 struct nlattr *attr;
1437 struct thermal_genl_event *thermal_event;
1438 void *msg_header;
1439 int size;
1440 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001441 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301442
1443 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001444 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1445 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301446
1447 skb = genlmsg_new(size, GFP_ATOMIC);
1448 if (!skb)
1449 return -ENOMEM;
1450
1451 /* add the genetlink message header */
1452 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1453 &thermal_event_genl_family, 0,
1454 THERMAL_GENL_CMD_EVENT);
1455 if (!msg_header) {
1456 nlmsg_free(skb);
1457 return -ENOMEM;
1458 }
1459
1460 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001461 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1462 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301463
1464 if (!attr) {
1465 nlmsg_free(skb);
1466 return -EINVAL;
1467 }
1468
1469 thermal_event = nla_data(attr);
1470 if (!thermal_event) {
1471 nlmsg_free(skb);
1472 return -EINVAL;
1473 }
1474
1475 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1476
1477 thermal_event->orig = orig;
1478 thermal_event->event = event;
1479
1480 /* send multicast genetlink message */
1481 result = genlmsg_end(skb, msg_header);
1482 if (result < 0) {
1483 nlmsg_free(skb);
1484 return result;
1485 }
1486
1487 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1488 if (result)
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001489 pr_info("failed to send netlink event:%d\n", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301490
1491 return result;
1492}
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001493EXPORT_SYMBOL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301494
1495static int genetlink_init(void)
1496{
1497 int result;
1498
1499 result = genl_register_family(&thermal_event_genl_family);
1500 if (result)
1501 return result;
1502
1503 result = genl_register_mc_group(&thermal_event_genl_family,
1504 &thermal_event_mcgrp);
1505 if (result)
1506 genl_unregister_family(&thermal_event_genl_family);
1507 return result;
1508}
1509
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001510static void genetlink_exit(void)
1511{
1512 genl_unregister_family(&thermal_event_genl_family);
1513}
1514#else /* !CONFIG_NET */
1515static inline int genetlink_init(void) { return 0; }
1516static inline void genetlink_exit(void) {}
1517#endif /* !CONFIG_NET */
1518
Zhang Rui203d3d42008-01-17 15:51:08 +08001519static int __init thermal_init(void)
1520{
1521 int result = 0;
1522
1523 result = class_register(&thermal_class);
1524 if (result) {
1525 idr_destroy(&thermal_tz_idr);
1526 idr_destroy(&thermal_cdev_idr);
1527 mutex_destroy(&thermal_idr_lock);
1528 mutex_destroy(&thermal_list_lock);
1529 }
R.Durgadoss4cb18722010-10-27 03:33:29 +05301530 result = genetlink_init();
Zhang Rui203d3d42008-01-17 15:51:08 +08001531 return result;
1532}
1533
1534static void __exit thermal_exit(void)
1535{
1536 class_unregister(&thermal_class);
1537 idr_destroy(&thermal_tz_idr);
1538 idr_destroy(&thermal_cdev_idr);
1539 mutex_destroy(&thermal_idr_lock);
1540 mutex_destroy(&thermal_list_lock);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301541 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001542}
1543
R.Durgadoss4cb18722010-10-27 03:33:29 +05301544fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001545module_exit(thermal_exit);