blob: 0afd84cb127d7c01392085b34f640091eef42342 [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);
Durgadoss Ra4a15482012-09-18 11:04:57 +053052static LIST_HEAD(thermal_governor_list);
53
Zhang Rui203d3d42008-01-17 15:51:08 +080054static DEFINE_MUTEX(thermal_list_lock);
Durgadoss Ra4a15482012-09-18 11:04:57 +053055static DEFINE_MUTEX(thermal_governor_lock);
56
57static struct thermal_governor *__find_governor(const char *name)
58{
59 struct thermal_governor *pos;
60
61 list_for_each_entry(pos, &thermal_governor_list, governor_list)
62 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
63 return pos;
64
65 return NULL;
66}
67
68int thermal_register_governor(struct thermal_governor *governor)
69{
70 int err;
71 const char *name;
72 struct thermal_zone_device *pos;
73
74 if (!governor)
75 return -EINVAL;
76
77 mutex_lock(&thermal_governor_lock);
78
79 err = -EBUSY;
80 if (__find_governor(governor->name) == NULL) {
81 err = 0;
82 list_add(&governor->governor_list, &thermal_governor_list);
83 }
84
85 mutex_lock(&thermal_list_lock);
86
87 list_for_each_entry(pos, &thermal_tz_list, node) {
88 if (pos->governor)
89 continue;
90 if (pos->tzp)
91 name = pos->tzp->governor_name;
92 else
93 name = DEFAULT_THERMAL_GOVERNOR;
94 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
95 pos->governor = governor;
96 }
97
98 mutex_unlock(&thermal_list_lock);
99 mutex_unlock(&thermal_governor_lock);
100
101 return err;
102}
103EXPORT_SYMBOL_GPL(thermal_register_governor);
104
105void thermal_unregister_governor(struct thermal_governor *governor)
106{
107 struct thermal_zone_device *pos;
108
109 if (!governor)
110 return;
111
112 mutex_lock(&thermal_governor_lock);
113
114 if (__find_governor(governor->name) == NULL)
115 goto exit;
116
117 mutex_lock(&thermal_list_lock);
118
119 list_for_each_entry(pos, &thermal_tz_list, node) {
120 if (!strnicmp(pos->governor->name, governor->name,
121 THERMAL_NAME_LENGTH))
122 pos->governor = NULL;
123 }
124
125 mutex_unlock(&thermal_list_lock);
126 list_del(&governor->governor_list);
127exit:
128 mutex_unlock(&thermal_governor_lock);
129 return;
130}
131EXPORT_SYMBOL_GPL(thermal_unregister_governor);
Zhang Rui203d3d42008-01-17 15:51:08 +0800132
133static int get_idr(struct idr *idr, struct mutex *lock, int *id)
134{
135 int err;
136
Joe Perchescaca8b82012-03-21 12:55:02 -0700137again:
Zhang Rui203d3d42008-01-17 15:51:08 +0800138 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
139 return -ENOMEM;
140
141 if (lock)
142 mutex_lock(lock);
143 err = idr_get_new(idr, NULL, id);
144 if (lock)
145 mutex_unlock(lock);
146 if (unlikely(err == -EAGAIN))
147 goto again;
148 else if (unlikely(err))
149 return err;
150
Fengguang Wu125c4c72012-10-04 17:13:15 -0700151 *id = *id & MAX_IDR_MASK;
Zhang Rui203d3d42008-01-17 15:51:08 +0800152 return 0;
153}
154
155static void release_idr(struct idr *idr, struct mutex *lock, int id)
156{
157 if (lock)
158 mutex_lock(lock);
159 idr_remove(idr, id);
160 if (lock)
161 mutex_unlock(lock);
162}
163
Durgadoss R9b4298a2012-09-18 11:04:54 +0530164int get_tz_trend(struct thermal_zone_device *tz, int trip)
165{
166 enum thermal_trend trend;
167
168 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
169 if (tz->temperature > tz->last_temperature)
170 trend = THERMAL_TREND_RAISING;
171 else if (tz->temperature < tz->last_temperature)
172 trend = THERMAL_TREND_DROPPING;
173 else
174 trend = THERMAL_TREND_STABLE;
175 }
176
177 return trend;
178}
179EXPORT_SYMBOL(get_tz_trend);
180
181struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
182 struct thermal_cooling_device *cdev, int trip)
183{
184 struct thermal_instance *pos = NULL;
185 struct thermal_instance *target_instance = NULL;
186
187 mutex_lock(&tz->lock);
188 mutex_lock(&cdev->lock);
189
190 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
191 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
192 target_instance = pos;
193 break;
194 }
195 }
196
197 mutex_unlock(&cdev->lock);
198 mutex_unlock(&tz->lock);
199
200 return target_instance;
201}
202EXPORT_SYMBOL(get_thermal_instance);
203
Zhang Rui203d3d42008-01-17 15:51:08 +0800204/* sys I/F for thermal zone */
205
206#define to_thermal_zone(_dev) \
207 container_of(_dev, struct thermal_zone_device, device)
208
209static ssize_t
210type_show(struct device *dev, struct device_attribute *attr, char *buf)
211{
212 struct thermal_zone_device *tz = to_thermal_zone(dev);
213
214 return sprintf(buf, "%s\n", tz->type);
215}
216
217static ssize_t
218temp_show(struct device *dev, struct device_attribute *attr, char *buf)
219{
220 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000221 long temperature;
222 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800223
224 if (!tz->ops->get_temp)
225 return -EPERM;
226
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000227 ret = tz->ops->get_temp(tz, &temperature);
228
229 if (ret)
230 return ret;
231
232 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800233}
234
235static ssize_t
236mode_show(struct device *dev, struct device_attribute *attr, char *buf)
237{
238 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000239 enum thermal_device_mode mode;
240 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800241
242 if (!tz->ops->get_mode)
243 return -EPERM;
244
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000245 result = tz->ops->get_mode(tz, &mode);
246 if (result)
247 return result;
248
249 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
250 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800251}
252
253static ssize_t
254mode_store(struct device *dev, struct device_attribute *attr,
255 const char *buf, size_t count)
256{
257 struct thermal_zone_device *tz = to_thermal_zone(dev);
258 int result;
259
260 if (!tz->ops->set_mode)
261 return -EPERM;
262
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530263 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000264 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530265 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000266 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
267 else
268 result = -EINVAL;
269
Zhang Rui203d3d42008-01-17 15:51:08 +0800270 if (result)
271 return result;
272
273 return count;
274}
275
276static ssize_t
277trip_point_type_show(struct device *dev, struct device_attribute *attr,
278 char *buf)
279{
280 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000281 enum thermal_trip_type type;
282 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800283
284 if (!tz->ops->get_trip_type)
285 return -EPERM;
286
287 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
288 return -EINVAL;
289
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000290 result = tz->ops->get_trip_type(tz, trip, &type);
291 if (result)
292 return result;
293
294 switch (type) {
295 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300296 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000297 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300298 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000299 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300300 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000301 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300302 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000303 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300304 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000305 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800306}
307
308static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800309trip_point_temp_store(struct device *dev, struct device_attribute *attr,
310 const char *buf, size_t count)
311{
312 struct thermal_zone_device *tz = to_thermal_zone(dev);
313 int trip, ret;
314 unsigned long temperature;
315
316 if (!tz->ops->set_trip_temp)
317 return -EPERM;
318
319 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
320 return -EINVAL;
321
322 if (kstrtoul(buf, 10, &temperature))
323 return -EINVAL;
324
325 ret = tz->ops->set_trip_temp(tz, trip, temperature);
326
327 return ret ? ret : count;
328}
329
330static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800331trip_point_temp_show(struct device *dev, struct device_attribute *attr,
332 char *buf)
333{
334 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000335 int trip, ret;
336 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800337
338 if (!tz->ops->get_trip_temp)
339 return -EPERM;
340
341 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
342 return -EINVAL;
343
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000344 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
345
346 if (ret)
347 return ret;
348
349 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800350}
351
Matthew Garrett03a971a2008-12-03 18:00:38 +0000352static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800353trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
354 const char *buf, size_t count)
355{
356 struct thermal_zone_device *tz = to_thermal_zone(dev);
357 int trip, ret;
358 unsigned long temperature;
359
360 if (!tz->ops->set_trip_hyst)
361 return -EPERM;
362
363 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
364 return -EINVAL;
365
366 if (kstrtoul(buf, 10, &temperature))
367 return -EINVAL;
368
369 /*
370 * We are not doing any check on the 'temperature' value
371 * here. The driver implementing 'set_trip_hyst' has to
372 * take care of this.
373 */
374 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
375
376 return ret ? ret : count;
377}
378
379static ssize_t
380trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
381 char *buf)
382{
383 struct thermal_zone_device *tz = to_thermal_zone(dev);
384 int trip, ret;
385 unsigned long temperature;
386
387 if (!tz->ops->get_trip_hyst)
388 return -EPERM;
389
390 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
391 return -EINVAL;
392
393 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
394
395 return ret ? ret : sprintf(buf, "%ld\n", temperature);
396}
397
398static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000399passive_store(struct device *dev, struct device_attribute *attr,
400 const char *buf, size_t count)
401{
402 struct thermal_zone_device *tz = to_thermal_zone(dev);
403 struct thermal_cooling_device *cdev = NULL;
404 int state;
405
406 if (!sscanf(buf, "%d\n", &state))
407 return -EINVAL;
408
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100409 /* sanity check: values below 1000 millicelcius don't make sense
410 * and can cause the system to go into a thermal heart attack
411 */
412 if (state && state < 1000)
413 return -EINVAL;
414
Matthew Garrett03a971a2008-12-03 18:00:38 +0000415 if (state && !tz->forced_passive) {
416 mutex_lock(&thermal_list_lock);
417 list_for_each_entry(cdev, &thermal_cdev_list, node) {
418 if (!strncmp("Processor", cdev->type,
419 sizeof("Processor")))
420 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800421 THERMAL_TRIPS_NONE, cdev,
422 THERMAL_NO_LIMIT,
423 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000424 }
425 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100426 if (!tz->passive_delay)
427 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000428 } else if (!state && tz->forced_passive) {
429 mutex_lock(&thermal_list_lock);
430 list_for_each_entry(cdev, &thermal_cdev_list, node) {
431 if (!strncmp("Processor", cdev->type,
432 sizeof("Processor")))
433 thermal_zone_unbind_cooling_device(tz,
434 THERMAL_TRIPS_NONE,
435 cdev);
436 }
437 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100438 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000439 }
440
Matthew Garrett03a971a2008-12-03 18:00:38 +0000441 tz->forced_passive = state;
442
443 thermal_zone_device_update(tz);
444
445 return count;
446}
447
448static ssize_t
449passive_show(struct device *dev, struct device_attribute *attr,
450 char *buf)
451{
452 struct thermal_zone_device *tz = to_thermal_zone(dev);
453
454 return sprintf(buf, "%d\n", tz->forced_passive);
455}
456
Zhang Rui203d3d42008-01-17 15:51:08 +0800457static DEVICE_ATTR(type, 0444, type_show, NULL);
458static DEVICE_ATTR(temp, 0444, temp_show, NULL);
459static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700460static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800461
Zhang Rui203d3d42008-01-17 15:51:08 +0800462/* sys I/F for cooling device */
463#define to_cooling_device(_dev) \
464 container_of(_dev, struct thermal_cooling_device, device)
465
466static ssize_t
467thermal_cooling_device_type_show(struct device *dev,
468 struct device_attribute *attr, char *buf)
469{
470 struct thermal_cooling_device *cdev = to_cooling_device(dev);
471
472 return sprintf(buf, "%s\n", cdev->type);
473}
474
475static ssize_t
476thermal_cooling_device_max_state_show(struct device *dev,
477 struct device_attribute *attr, char *buf)
478{
479 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000480 unsigned long state;
481 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800482
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000483 ret = cdev->ops->get_max_state(cdev, &state);
484 if (ret)
485 return ret;
486 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800487}
488
489static ssize_t
490thermal_cooling_device_cur_state_show(struct device *dev,
491 struct device_attribute *attr, char *buf)
492{
493 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000494 unsigned long state;
495 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800496
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000497 ret = cdev->ops->get_cur_state(cdev, &state);
498 if (ret)
499 return ret;
500 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800501}
502
503static ssize_t
504thermal_cooling_device_cur_state_store(struct device *dev,
505 struct device_attribute *attr,
506 const char *buf, size_t count)
507{
508 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000509 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800510 int result;
511
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000512 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800513 return -EINVAL;
514
Roel Kluinedb94912009-12-15 22:46:50 +0100515 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800516 return -EINVAL;
517
518 result = cdev->ops->set_cur_state(cdev, state);
519 if (result)
520 return result;
521 return count;
522}
523
524static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500525__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800526static DEVICE_ATTR(max_state, 0444,
527 thermal_cooling_device_max_state_show, NULL);
528static DEVICE_ATTR(cur_state, 0644,
529 thermal_cooling_device_cur_state_show,
530 thermal_cooling_device_cur_state_store);
531
532static ssize_t
533thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500534 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800535{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800536 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800537
538 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800539 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800540
541 if (instance->trip == THERMAL_TRIPS_NONE)
542 return sprintf(buf, "-1\n");
543 else
544 return sprintf(buf, "%d\n", instance->trip);
545}
546
547/* Device management */
548
Rene Herman16d75232008-06-24 19:38:56 +0200549#if defined(CONFIG_THERMAL_HWMON)
550
Zhang Ruie68b16a2008-04-21 16:07:52 +0800551/* hwmon sys I/F */
552#include <linux/hwmon.h>
Jean Delvare31f53962011-07-28 13:48:42 -0700553
554/* thermal zone devices with the same type share one hwmon device */
555struct thermal_hwmon_device {
556 char type[THERMAL_NAME_LENGTH];
557 struct device *device;
558 int count;
559 struct list_head tz_list;
560 struct list_head node;
561};
562
563struct thermal_hwmon_attr {
564 struct device_attribute attr;
565 char name[16];
566};
567
568/* one temperature input for each thermal zone */
569struct thermal_hwmon_temp {
570 struct list_head hwmon_node;
571 struct thermal_zone_device *tz;
572 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
573 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
574};
575
Zhang Ruie68b16a2008-04-21 16:07:52 +0800576static LIST_HEAD(thermal_hwmon_list);
577
578static ssize_t
579name_show(struct device *dev, struct device_attribute *attr, char *buf)
580{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700581 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800582 return sprintf(buf, "%s\n", hwmon->type);
583}
584static DEVICE_ATTR(name, 0444, name_show, NULL);
585
586static ssize_t
587temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
588{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000589 long temperature;
590 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800591 struct thermal_hwmon_attr *hwmon_attr
592 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700593 struct thermal_hwmon_temp *temp
594 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800595 temp_input);
Jean Delvare31f53962011-07-28 13:48:42 -0700596 struct thermal_zone_device *tz = temp->tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800597
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000598 ret = tz->ops->get_temp(tz, &temperature);
599
600 if (ret)
601 return ret;
602
603 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800604}
605
606static ssize_t
607temp_crit_show(struct device *dev, struct device_attribute *attr,
608 char *buf)
609{
610 struct thermal_hwmon_attr *hwmon_attr
611 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700612 struct thermal_hwmon_temp *temp
613 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800614 temp_crit);
Jean Delvare31f53962011-07-28 13:48:42 -0700615 struct thermal_zone_device *tz = temp->tz;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000616 long temperature;
617 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800618
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000619 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
620 if (ret)
621 return ret;
622
623 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800624}
625
626
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700627static struct thermal_hwmon_device *
628thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
629{
630 struct thermal_hwmon_device *hwmon;
631
632 mutex_lock(&thermal_list_lock);
633 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
634 if (!strcmp(hwmon->type, tz->type)) {
635 mutex_unlock(&thermal_list_lock);
636 return hwmon;
637 }
638 mutex_unlock(&thermal_list_lock);
639
640 return NULL;
641}
642
Jean Delvare31f53962011-07-28 13:48:42 -0700643/* Find the temperature input matching a given thermal zone */
644static struct thermal_hwmon_temp *
645thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
646 const struct thermal_zone_device *tz)
647{
648 struct thermal_hwmon_temp *temp;
649
650 mutex_lock(&thermal_list_lock);
651 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
652 if (temp->tz == tz) {
653 mutex_unlock(&thermal_list_lock);
654 return temp;
655 }
656 mutex_unlock(&thermal_list_lock);
657
658 return NULL;
659}
660
Zhang Ruie68b16a2008-04-21 16:07:52 +0800661static int
662thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
663{
664 struct thermal_hwmon_device *hwmon;
Jean Delvare31f53962011-07-28 13:48:42 -0700665 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800666 int new_hwmon_device = 1;
667 int result;
668
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700669 hwmon = thermal_hwmon_lookup_by_type(tz);
670 if (hwmon) {
671 new_hwmon_device = 0;
672 goto register_sys_interface;
673 }
Zhang Ruie68b16a2008-04-21 16:07:52 +0800674
675 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
676 if (!hwmon)
677 return -ENOMEM;
678
679 INIT_LIST_HEAD(&hwmon->tz_list);
680 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
681 hwmon->device = hwmon_device_register(NULL);
682 if (IS_ERR(hwmon->device)) {
683 result = PTR_ERR(hwmon->device);
684 goto free_mem;
685 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700686 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800687 result = device_create_file(hwmon->device, &dev_attr_name);
688 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530689 goto free_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800690
691 register_sys_interface:
Jean Delvare31f53962011-07-28 13:48:42 -0700692 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
693 if (!temp) {
694 result = -ENOMEM;
695 goto unregister_name;
696 }
697
698 temp->tz = tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800699 hwmon->count++;
700
Guenter Roeck79a49162012-07-21 10:53:48 +1000701 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +0800702 "temp%d_input", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700703 temp->temp_input.attr.attr.name = temp->temp_input.name;
704 temp->temp_input.attr.attr.mode = 0444;
705 temp->temp_input.attr.show = temp_input_show;
706 sysfs_attr_init(&temp->temp_input.attr.attr);
707 result = device_create_file(hwmon->device, &temp->temp_input.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800708 if (result)
Jean Delvare31f53962011-07-28 13:48:42 -0700709 goto free_temp_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800710
711 if (tz->ops->get_crit_temp) {
712 unsigned long temperature;
713 if (!tz->ops->get_crit_temp(tz, &temperature)) {
Guenter Roeck79a49162012-07-21 10:53:48 +1000714 snprintf(temp->temp_crit.name,
715 sizeof(temp->temp_crit.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +0800716 "temp%d_crit", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700717 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
718 temp->temp_crit.attr.attr.mode = 0444;
719 temp->temp_crit.attr.show = temp_crit_show;
720 sysfs_attr_init(&temp->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800721 result = device_create_file(hwmon->device,
Jean Delvare31f53962011-07-28 13:48:42 -0700722 &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800723 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530724 goto unregister_input;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800725 }
726 }
727
728 mutex_lock(&thermal_list_lock);
729 if (new_hwmon_device)
730 list_add_tail(&hwmon->node, &thermal_hwmon_list);
Jean Delvare31f53962011-07-28 13:48:42 -0700731 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800732 mutex_unlock(&thermal_list_lock);
733
734 return 0;
735
Durgadoss Rb299eb52011-03-03 04:30:13 +0530736 unregister_input:
Jean Delvare31f53962011-07-28 13:48:42 -0700737 device_remove_file(hwmon->device, &temp->temp_input.attr);
738 free_temp_mem:
739 kfree(temp);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530740 unregister_name:
Zhang Ruie68b16a2008-04-21 16:07:52 +0800741 if (new_hwmon_device) {
742 device_remove_file(hwmon->device, &dev_attr_name);
743 hwmon_device_unregister(hwmon->device);
744 }
745 free_mem:
746 if (new_hwmon_device)
747 kfree(hwmon);
748
749 return result;
750}
751
752static void
753thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
754{
Jean Delvare31f53962011-07-28 13:48:42 -0700755 struct thermal_hwmon_device *hwmon;
756 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800757
Jean Delvare31f53962011-07-28 13:48:42 -0700758 hwmon = thermal_hwmon_lookup_by_type(tz);
759 if (unlikely(!hwmon)) {
760 /* Should never happen... */
761 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
762 return;
763 }
764
765 temp = thermal_hwmon_lookup_temp(hwmon, tz);
766 if (unlikely(!temp)) {
767 /* Should never happen... */
768 dev_dbg(&tz->device, "temperature input lookup failed!\n");
769 return;
770 }
771
772 device_remove_file(hwmon->device, &temp->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530773 if (tz->ops->get_crit_temp)
Jean Delvare31f53962011-07-28 13:48:42 -0700774 device_remove_file(hwmon->device, &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800775
776 mutex_lock(&thermal_list_lock);
Jean Delvare31f53962011-07-28 13:48:42 -0700777 list_del(&temp->hwmon_node);
778 kfree(temp);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800779 if (!list_empty(&hwmon->tz_list)) {
780 mutex_unlock(&thermal_list_lock);
781 return;
782 }
783 list_del(&hwmon->node);
784 mutex_unlock(&thermal_list_lock);
785
786 device_remove_file(hwmon->device, &dev_attr_name);
787 hwmon_device_unregister(hwmon->device);
788 kfree(hwmon);
789}
790#else
791static int
792thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
793{
794 return 0;
795}
796
797static void
798thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
799{
800}
801#endif
802
Matthew Garrettb1569e92008-12-03 17:55:32 +0000803static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
804 int delay)
805{
Matthew Garrettb1569e92008-12-03 17:55:32 +0000806 if (delay > 1000)
Tejun Heo41f63c52012-08-03 10:30:47 -0700807 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
808 round_jiffies(msecs_to_jiffies(delay)));
809 else if (delay)
810 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
811 msecs_to_jiffies(delay));
Matthew Garrettb1569e92008-12-03 17:55:32 +0000812 else
Tejun Heo41f63c52012-08-03 10:30:47 -0700813 cancel_delayed_work(&tz->poll_queue);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000814}
815
Matthew Garrettb1569e92008-12-03 17:55:32 +0000816static void thermal_zone_device_check(struct work_struct *work)
817{
818 struct thermal_zone_device *tz = container_of(work, struct
819 thermal_zone_device,
820 poll_queue.work);
821 thermal_zone_device_update(tz);
822}
Zhang Ruie68b16a2008-04-21 16:07:52 +0800823
Zhang Rui203d3d42008-01-17 15:51:08 +0800824/**
825 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800826 * @tz: thermal zone device
827 * @trip: indicates which trip point the cooling devices is
828 * associated with in this thermal zone.
829 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500830 *
831 * This function is usually called in the thermal zone device .bind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800832 */
833int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
834 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800835 struct thermal_cooling_device *cdev,
836 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +0800837{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800838 struct thermal_instance *dev;
839 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500840 struct thermal_zone_device *pos1;
841 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800842 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800843 int result;
844
Len Brown543a9562008-02-07 16:55:08 -0500845 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800846 return -EINVAL;
847
Thomas Sujithc7516702008-02-15 00:58:50 -0500848 list_for_each_entry(pos1, &thermal_tz_list, node) {
849 if (pos1 == tz)
850 break;
851 }
852 list_for_each_entry(pos2, &thermal_cdev_list, node) {
853 if (pos2 == cdev)
854 break;
855 }
856
857 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800858 return -EINVAL;
859
Zhang Rui9d998422012-06-26 16:35:57 +0800860 cdev->ops->get_max_state(cdev, &max_state);
861
862 /* lower default 0, upper default max_state */
863 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
864 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
865
866 if (lower > upper || upper > max_state)
867 return -EINVAL;
868
Zhang Rui203d3d42008-01-17 15:51:08 +0800869 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800870 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800871 if (!dev)
872 return -ENOMEM;
873 dev->tz = tz;
874 dev->cdev = cdev;
875 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800876 dev->upper = upper;
877 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800878 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +0800879
Zhang Rui203d3d42008-01-17 15:51:08 +0800880 result = get_idr(&tz->idr, &tz->lock, &dev->id);
881 if (result)
882 goto free_mem;
883
884 sprintf(dev->name, "cdev%d", dev->id);
885 result =
886 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
887 if (result)
888 goto release_idr;
889
890 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700891 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800892 dev->attr.attr.name = dev->attr_name;
893 dev->attr.attr.mode = 0444;
894 dev->attr.show = thermal_cooling_device_trip_point_show;
895 result = device_create_file(&tz->device, &dev->attr);
896 if (result)
897 goto remove_symbol_link;
898
899 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800900 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800901 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +0800902 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
903 result = -EEXIST;
904 break;
905 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800906 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800907 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800908 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
909 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800910 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800911 mutex_unlock(&tz->lock);
912
913 if (!result)
914 return 0;
915
916 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -0700917remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800918 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -0700919release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +0800920 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -0700921free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800922 kfree(dev);
923 return result;
924}
925EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
926
927/**
928 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800929 * @tz: thermal zone device
930 * @trip: indicates which trip point the cooling devices is
931 * associated with in this thermal zone.
932 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500933 *
934 * This function is usually called in the thermal zone device .unbind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800935 */
936int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
937 int trip,
938 struct thermal_cooling_device *cdev)
939{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800940 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +0800941
942 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800943 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800944 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -0500945 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800946 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800947 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800948 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800949 mutex_unlock(&tz->lock);
950 goto unbind;
951 }
952 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800953 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800954 mutex_unlock(&tz->lock);
955
956 return -ENODEV;
957
Joe Perchescaca8b82012-03-21 12:55:02 -0700958unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +0800959 device_remove_file(&tz->device, &pos->attr);
960 sysfs_remove_link(&tz->device.kobj, pos->name);
961 release_idr(&tz->idr, &tz->lock, pos->id);
962 kfree(pos);
963 return 0;
964}
965EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
966
967static void thermal_release(struct device *dev)
968{
969 struct thermal_zone_device *tz;
970 struct thermal_cooling_device *cdev;
971
Joe Perchescaca8b82012-03-21 12:55:02 -0700972 if (!strncmp(dev_name(dev), "thermal_zone",
973 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800974 tz = to_thermal_zone(dev);
975 kfree(tz);
976 } else {
977 cdev = to_cooling_device(dev);
978 kfree(cdev);
979 }
980}
981
982static struct class thermal_class = {
983 .name = "thermal",
984 .dev_release = thermal_release,
985};
986
987/**
988 * thermal_cooling_device_register - register a new thermal cooling device
989 * @type: the thermal cooling device type.
990 * @devdata: device private data.
991 * @ops: standard thermal cooling devices callbacks.
992 */
Joe Perchescaca8b82012-03-21 12:55:02 -0700993struct thermal_cooling_device *
994thermal_cooling_device_register(char *type, void *devdata,
995 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800996{
997 struct thermal_cooling_device *cdev;
998 struct thermal_zone_device *pos;
999 int result;
1000
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001001 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001002 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001003
1004 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001005 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001006 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001007
1008 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1009 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001010 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001011
1012 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1013 if (result) {
1014 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001015 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001016 }
1017
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001018 strcpy(cdev->type, type ? : "");
Zhang Ruif4a821c2012-07-24 16:56:21 +08001019 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001020 INIT_LIST_HEAD(&cdev->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001021 cdev->ops = ops;
Zhang Ruice119f82012-06-27 14:13:04 +08001022 cdev->updated = true;
Zhang Rui203d3d42008-01-17 15:51:08 +08001023 cdev->device.class = &thermal_class;
1024 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001025 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001026 result = device_register(&cdev->device);
1027 if (result) {
1028 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1029 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001030 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001031 }
1032
1033 /* sys I/F */
1034 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001035 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001036 if (result)
1037 goto unregister;
1038 }
1039
1040 result = device_create_file(&cdev->device, &dev_attr_max_state);
1041 if (result)
1042 goto unregister;
1043
1044 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1045 if (result)
1046 goto unregister;
1047
1048 mutex_lock(&thermal_list_lock);
1049 list_add(&cdev->node, &thermal_cdev_list);
1050 list_for_each_entry(pos, &thermal_tz_list, node) {
1051 if (!pos->ops->bind)
1052 continue;
1053 result = pos->ops->bind(pos, cdev);
1054 if (result)
1055 break;
1056
1057 }
1058 mutex_unlock(&thermal_list_lock);
1059
1060 if (!result)
1061 return cdev;
1062
Joe Perchescaca8b82012-03-21 12:55:02 -07001063unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001064 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1065 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001066 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001067}
1068EXPORT_SYMBOL(thermal_cooling_device_register);
1069
1070/**
1071 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001072 * @cdev: the thermal cooling device to remove.
1073 *
1074 * thermal_cooling_device_unregister() must be called when the device is no
1075 * longer needed.
1076 */
1077void thermal_cooling_device_unregister(struct
1078 thermal_cooling_device
1079 *cdev)
1080{
1081 struct thermal_zone_device *tz;
1082 struct thermal_cooling_device *pos = NULL;
1083
1084 if (!cdev)
1085 return;
1086
1087 mutex_lock(&thermal_list_lock);
1088 list_for_each_entry(pos, &thermal_cdev_list, node)
1089 if (pos == cdev)
1090 break;
1091 if (pos != cdev) {
1092 /* thermal cooling device not found */
1093 mutex_unlock(&thermal_list_lock);
1094 return;
1095 }
1096 list_del(&cdev->node);
1097 list_for_each_entry(tz, &thermal_tz_list, node) {
1098 if (!tz->ops->unbind)
1099 continue;
1100 tz->ops->unbind(tz, cdev);
1101 }
1102 mutex_unlock(&thermal_list_lock);
1103 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001104 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001105 device_remove_file(&cdev->device, &dev_attr_max_state);
1106 device_remove_file(&cdev->device, &dev_attr_cur_state);
1107
1108 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1109 device_unregister(&cdev->device);
1110 return;
1111}
1112EXPORT_SYMBOL(thermal_cooling_device_unregister);
1113
Zhang Ruice119f82012-06-27 14:13:04 +08001114static void thermal_cdev_do_update(struct thermal_cooling_device *cdev)
1115{
1116 struct thermal_instance *instance;
1117 unsigned long target = 0;
1118
1119 /* cooling device is updated*/
1120 if (cdev->updated)
1121 return;
1122
Zhang Ruif4a821c2012-07-24 16:56:21 +08001123 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001124 /* Make sure cdev enters the deepest cooling state */
1125 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1126 if (instance->target == THERMAL_NO_TARGET)
1127 continue;
1128 if (instance->target > target)
1129 target = instance->target;
1130 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001131 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001132 cdev->ops->set_cur_state(cdev, target);
1133 cdev->updated = true;
1134}
1135
1136static void thermal_zone_do_update(struct thermal_zone_device *tz)
1137{
1138 struct thermal_instance *instance;
1139
1140 list_for_each_entry(instance, &tz->thermal_instances, tz_node)
1141 thermal_cdev_do_update(instance->cdev);
1142}
1143
Zhang Rui4ae46be2012-06-27 10:05:39 +08001144/*
Zhang Rui908b9fb2012-06-27 14:14:05 +08001145 * Cooling algorithm for both active and passive cooling
Zhang Rui4ae46be2012-06-27 10:05:39 +08001146 *
1147 * 1. if the temperature is higher than a trip point,
1148 * a. if the trend is THERMAL_TREND_RAISING, use higher cooling
1149 * state for this trip point
1150 * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
1151 * state for this trip point
1152 *
1153 * 2. if the temperature is lower than a trip point, use lower
1154 * cooling state for this trip point
1155 *
1156 * Note that this behaves the same as the previous passive cooling
1157 * algorithm.
1158 */
1159
1160static void thermal_zone_trip_update(struct thermal_zone_device *tz,
1161 int trip, long temp)
1162{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001163 struct thermal_instance *instance;
Zhang Rui4ae46be2012-06-27 10:05:39 +08001164 struct thermal_cooling_device *cdev = NULL;
1165 unsigned long cur_state, max_state;
1166 long trip_temp;
Zhang Rui908b9fb2012-06-27 14:14:05 +08001167 enum thermal_trip_type trip_type;
Zhang Rui4ae46be2012-06-27 10:05:39 +08001168 enum thermal_trend trend;
1169
Zhang Rui908b9fb2012-06-27 14:14:05 +08001170 if (trip == THERMAL_TRIPS_NONE) {
1171 trip_temp = tz->forced_passive;
1172 trip_type = THERMAL_TRIPS_NONE;
1173 } else {
1174 tz->ops->get_trip_temp(tz, trip, &trip_temp);
1175 tz->ops->get_trip_type(tz, trip, &trip_type);
1176 }
Zhang Rui4ae46be2012-06-27 10:05:39 +08001177
1178 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
1179 /*
1180 * compare the current temperature and previous temperature
1181 * to get the thermal trend, if no special requirement
1182 */
1183 if (tz->temperature > tz->last_temperature)
1184 trend = THERMAL_TREND_RAISING;
1185 else if (tz->temperature < tz->last_temperature)
1186 trend = THERMAL_TREND_DROPPING;
1187 else
1188 trend = THERMAL_TREND_STABLE;
1189 }
1190
1191 if (temp >= trip_temp) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001192 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
Zhang Rui4ae46be2012-06-27 10:05:39 +08001193 if (instance->trip != trip)
1194 continue;
1195
1196 cdev = instance->cdev;
1197
1198 cdev->ops->get_cur_state(cdev, &cur_state);
1199 cdev->ops->get_max_state(cdev, &max_state);
1200
1201 if (trend == THERMAL_TREND_RAISING) {
1202 cur_state = cur_state < instance->upper ?
1203 (cur_state + 1) : instance->upper;
1204 } else if (trend == THERMAL_TREND_DROPPING) {
1205 cur_state = cur_state > instance->lower ?
1206 (cur_state - 1) : instance->lower;
1207 }
Zhang Rui908b9fb2012-06-27 14:14:05 +08001208
1209 /* activate a passive thermal instance */
1210 if ((trip_type == THERMAL_TRIP_PASSIVE ||
1211 trip_type == THERMAL_TRIPS_NONE) &&
1212 instance->target == THERMAL_NO_TARGET)
1213 tz->passive++;
1214
Zhang Ruice119f82012-06-27 14:13:04 +08001215 instance->target = cur_state;
1216 cdev->updated = false; /* cooling device needs update */
Zhang Rui4ae46be2012-06-27 10:05:39 +08001217 }
1218 } else { /* below trip */
Zhang Ruicddf31b2012-06-27 10:09:36 +08001219 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
Zhang Rui4ae46be2012-06-27 10:05:39 +08001220 if (instance->trip != trip)
1221 continue;
1222
Zhang Ruice119f82012-06-27 14:13:04 +08001223 /* Do not use the inactive thermal instance */
1224 if (instance->target == THERMAL_NO_TARGET)
1225 continue;
Zhang Rui4ae46be2012-06-27 10:05:39 +08001226 cdev = instance->cdev;
1227 cdev->ops->get_cur_state(cdev, &cur_state);
1228
1229 cur_state = cur_state > instance->lower ?
Zhang Ruice119f82012-06-27 14:13:04 +08001230 (cur_state - 1) : THERMAL_NO_TARGET;
Zhang Rui908b9fb2012-06-27 14:14:05 +08001231
1232 /* deactivate a passive thermal instance */
1233 if ((trip_type == THERMAL_TRIP_PASSIVE ||
1234 trip_type == THERMAL_TRIPS_NONE) &&
1235 cur_state == THERMAL_NO_TARGET)
1236 tz->passive--;
Zhang Ruice119f82012-06-27 14:13:04 +08001237 instance->target = cur_state;
1238 cdev->updated = false; /* cooling device needs update */
Zhang Rui4ae46be2012-06-27 10:05:39 +08001239 }
1240 }
1241
1242 return;
1243}
Zhang Rui203d3d42008-01-17 15:51:08 +08001244/**
Matthew Garrettb1569e92008-12-03 17:55:32 +00001245 * thermal_zone_device_update - force an update of a thermal zone's state
1246 * @ttz: the thermal zone to update
1247 */
1248
1249void thermal_zone_device_update(struct thermal_zone_device *tz)
1250{
1251 int count, ret = 0;
1252 long temp, trip_temp;
1253 enum thermal_trip_type trip_type;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001254
1255 mutex_lock(&tz->lock);
1256
Michael Brunner0d288162009-08-26 14:29:25 -07001257 if (tz->ops->get_temp(tz, &temp)) {
1258 /* get_temp failed - retry it later */
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001259 pr_warn("failed to read out thermal zone %d\n", tz->id);
Michael Brunner0d288162009-08-26 14:29:25 -07001260 goto leave;
1261 }
Matthew Garrettb1569e92008-12-03 17:55:32 +00001262
Zhang Rui601f3d42012-06-27 09:54:33 +08001263 tz->last_temperature = tz->temperature;
1264 tz->temperature = temp;
1265
Matthew Garrettb1569e92008-12-03 17:55:32 +00001266 for (count = 0; count < tz->trips; count++) {
1267 tz->ops->get_trip_type(tz, count, &trip_type);
1268 tz->ops->get_trip_temp(tz, count, &trip_temp);
1269
1270 switch (trip_type) {
1271 case THERMAL_TRIP_CRITICAL:
Vladimir Zajac29321352009-05-06 19:34:21 +02001272 if (temp >= trip_temp) {
Matthew Garrettb1569e92008-12-03 17:55:32 +00001273 if (tz->ops->notify)
1274 ret = tz->ops->notify(tz, count,
1275 trip_type);
1276 if (!ret) {
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001277 pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1278 temp/1000);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001279 orderly_poweroff(true);
1280 }
1281 }
1282 break;
1283 case THERMAL_TRIP_HOT:
Vladimir Zajac29321352009-05-06 19:34:21 +02001284 if (temp >= trip_temp)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001285 if (tz->ops->notify)
1286 tz->ops->notify(tz, count, trip_type);
1287 break;
1288 case THERMAL_TRIP_ACTIVE:
Zhang Rui4ae46be2012-06-27 10:05:39 +08001289 thermal_zone_trip_update(tz, count, temp);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001290 break;
1291 case THERMAL_TRIP_PASSIVE:
Vladimir Zajac29321352009-05-06 19:34:21 +02001292 if (temp >= trip_temp || tz->passive)
Zhang Rui908b9fb2012-06-27 14:14:05 +08001293 thermal_zone_trip_update(tz, count, temp);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001294 break;
1295 }
1296 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001297
1298 if (tz->forced_passive)
Zhang Rui908b9fb2012-06-27 14:14:05 +08001299 thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE, temp);
1300 thermal_zone_do_update(tz);
Michael Brunner0d288162009-08-26 14:29:25 -07001301
Joe Perchescaca8b82012-03-21 12:55:02 -07001302leave:
Matthew Garrettb1569e92008-12-03 17:55:32 +00001303 if (tz->passive)
1304 thermal_zone_device_set_polling(tz, tz->passive_delay);
1305 else if (tz->polling_delay)
1306 thermal_zone_device_set_polling(tz, tz->polling_delay);
Frans Pop3767cb52009-10-26 08:39:04 +01001307 else
1308 thermal_zone_device_set_polling(tz, 0);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001309 mutex_unlock(&tz->lock);
1310}
1311EXPORT_SYMBOL(thermal_zone_device_update);
1312
1313/**
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001314 * create_trip_attrs - create attributes for trip points
1315 * @tz: the thermal zone device
1316 * @mask: Writeable trip point bitmap.
1317 */
1318static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1319{
1320 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001321 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001322
Durgadoss R27365a62012-07-25 10:10:59 +08001323 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001324 if (!tz->trip_type_attrs)
1325 return -ENOMEM;
1326
Durgadoss R27365a62012-07-25 10:10:59 +08001327 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001328 if (!tz->trip_temp_attrs) {
1329 kfree(tz->trip_type_attrs);
1330 return -ENOMEM;
1331 }
1332
Durgadoss R27365a62012-07-25 10:10:59 +08001333 if (tz->ops->get_trip_hyst) {
1334 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1335 if (!tz->trip_hyst_attrs) {
1336 kfree(tz->trip_type_attrs);
1337 kfree(tz->trip_temp_attrs);
1338 return -ENOMEM;
1339 }
1340 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001341
Durgadoss R27365a62012-07-25 10:10:59 +08001342
1343 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001344 /* create trip type attribute */
1345 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1346 "trip_point_%d_type", indx);
1347
1348 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1349 tz->trip_type_attrs[indx].attr.attr.name =
1350 tz->trip_type_attrs[indx].name;
1351 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1352 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1353
1354 device_create_file(&tz->device,
1355 &tz->trip_type_attrs[indx].attr);
1356
1357 /* create trip temp attribute */
1358 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1359 "trip_point_%d_temp", indx);
1360
1361 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1362 tz->trip_temp_attrs[indx].attr.attr.name =
1363 tz->trip_temp_attrs[indx].name;
1364 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1365 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1366 if (mask & (1 << indx)) {
1367 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1368 tz->trip_temp_attrs[indx].attr.store =
1369 trip_point_temp_store;
1370 }
1371
1372 device_create_file(&tz->device,
1373 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001374
1375 /* create Optional trip hyst attribute */
1376 if (!tz->ops->get_trip_hyst)
1377 continue;
1378 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1379 "trip_point_%d_hyst", indx);
1380
1381 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1382 tz->trip_hyst_attrs[indx].attr.attr.name =
1383 tz->trip_hyst_attrs[indx].name;
1384 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1385 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1386 if (tz->ops->set_trip_hyst) {
1387 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1388 tz->trip_hyst_attrs[indx].attr.store =
1389 trip_point_hyst_store;
1390 }
1391
1392 device_create_file(&tz->device,
1393 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001394 }
1395 return 0;
1396}
1397
1398static void remove_trip_attrs(struct thermal_zone_device *tz)
1399{
1400 int indx;
1401
1402 for (indx = 0; indx < tz->trips; indx++) {
1403 device_remove_file(&tz->device,
1404 &tz->trip_type_attrs[indx].attr);
1405 device_remove_file(&tz->device,
1406 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001407 if (tz->ops->get_trip_hyst)
1408 device_remove_file(&tz->device,
1409 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001410 }
1411 kfree(tz->trip_type_attrs);
1412 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001413 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001414}
1415
1416/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001417 * thermal_zone_device_register - register a new thermal zone device
1418 * @type: the thermal zone device type
1419 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001420 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001421 * @devdata: private device data
1422 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301423 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001424 * @passive_delay: number of milliseconds to wait between polls when
1425 * performing passive cooling
1426 * @polling_delay: number of milliseconds to wait between polls when checking
1427 * whether trip points have been crossed (0 for interrupt
1428 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001429 *
1430 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001431 * longer needed. The passive cooling depends on the .get_trend() return value.
Zhang Rui203d3d42008-01-17 15:51:08 +08001432 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001433struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001434 int trips, int mask, void *devdata,
Alan Cox5b275ce2010-11-11 15:27:29 +00001435 const struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301436 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001437 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001438{
1439 struct thermal_zone_device *tz;
1440 struct thermal_cooling_device *pos;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001441 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001442 int result;
1443 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001444 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001445
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001446 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001447 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001448
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001449 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001450 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001451
1452 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001453 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001454
1455 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1456 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001457 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001458
Zhang Rui2d374132012-06-27 10:09:00 +08001459 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001460 idr_init(&tz->idr);
1461 mutex_init(&tz->lock);
1462 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1463 if (result) {
1464 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001465 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001466 }
1467
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001468 strcpy(tz->type, type ? : "");
Zhang Rui203d3d42008-01-17 15:51:08 +08001469 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301470 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001471 tz->device.class = &thermal_class;
1472 tz->devdata = devdata;
1473 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001474 tz->passive_delay = passive_delay;
1475 tz->polling_delay = polling_delay;
1476
Kay Sievers354655e2009-01-06 10:44:37 -08001477 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001478 result = device_register(&tz->device);
1479 if (result) {
1480 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1481 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001482 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001483 }
1484
1485 /* sys I/F */
1486 if (type) {
1487 result = device_create_file(&tz->device, &dev_attr_type);
1488 if (result)
1489 goto unregister;
1490 }
1491
1492 result = device_create_file(&tz->device, &dev_attr_temp);
1493 if (result)
1494 goto unregister;
1495
1496 if (ops->get_mode) {
1497 result = device_create_file(&tz->device, &dev_attr_mode);
1498 if (result)
1499 goto unregister;
1500 }
1501
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001502 result = create_trip_attrs(tz, mask);
1503 if (result)
1504 goto unregister;
1505
Zhang Rui203d3d42008-01-17 15:51:08 +08001506 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001507 tz->ops->get_trip_type(tz, count, &trip_type);
1508 if (trip_type == THERMAL_TRIP_PASSIVE)
1509 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001510 }
1511
Matthew Garrett03a971a2008-12-03 18:00:38 +00001512 if (!passive)
1513 result = device_create_file(&tz->device,
1514 &dev_attr_passive);
1515
1516 if (result)
1517 goto unregister;
1518
Durgadoss Ra4a15482012-09-18 11:04:57 +05301519 /* Update 'this' zone's governor information */
1520 mutex_lock(&thermal_governor_lock);
1521
1522 if (tz->tzp)
1523 tz->governor = __find_governor(tz->tzp->governor_name);
1524 else
1525 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1526
1527 mutex_unlock(&thermal_governor_lock);
1528
Zhang Ruie68b16a2008-04-21 16:07:52 +08001529 result = thermal_add_hwmon_sysfs(tz);
1530 if (result)
1531 goto unregister;
1532
Zhang Rui203d3d42008-01-17 15:51:08 +08001533 mutex_lock(&thermal_list_lock);
1534 list_add_tail(&tz->node, &thermal_tz_list);
1535 if (ops->bind)
1536 list_for_each_entry(pos, &thermal_cdev_list, node) {
Len Brown543a9562008-02-07 16:55:08 -05001537 result = ops->bind(tz, pos);
1538 if (result)
1539 break;
Zhang Rui203d3d42008-01-17 15:51:08 +08001540 }
1541 mutex_unlock(&thermal_list_lock);
1542
Matthew Garrettb1569e92008-12-03 17:55:32 +00001543 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1544
1545 thermal_zone_device_update(tz);
1546
Zhang Rui203d3d42008-01-17 15:51:08 +08001547 if (!result)
1548 return tz;
1549
Joe Perchescaca8b82012-03-21 12:55:02 -07001550unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001551 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1552 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001553 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001554}
1555EXPORT_SYMBOL(thermal_zone_device_register);
1556
1557/**
1558 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001559 * @tz: the thermal zone device to remove
1560 */
1561void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1562{
1563 struct thermal_cooling_device *cdev;
1564 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001565
1566 if (!tz)
1567 return;
1568
1569 mutex_lock(&thermal_list_lock);
1570 list_for_each_entry(pos, &thermal_tz_list, node)
1571 if (pos == tz)
1572 break;
1573 if (pos != tz) {
1574 /* thermal zone device not found */
1575 mutex_unlock(&thermal_list_lock);
1576 return;
1577 }
1578 list_del(&tz->node);
1579 if (tz->ops->unbind)
1580 list_for_each_entry(cdev, &thermal_cdev_list, node)
1581 tz->ops->unbind(tz, cdev);
1582 mutex_unlock(&thermal_list_lock);
1583
Matthew Garrettb1569e92008-12-03 17:55:32 +00001584 thermal_zone_device_set_polling(tz, 0);
1585
Zhang Rui203d3d42008-01-17 15:51:08 +08001586 if (tz->type[0])
1587 device_remove_file(&tz->device, &dev_attr_type);
1588 device_remove_file(&tz->device, &dev_attr_temp);
1589 if (tz->ops->get_mode)
1590 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001591 remove_trip_attrs(tz);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301592 tz->governor = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001593
Zhang Ruie68b16a2008-04-21 16:07:52 +08001594 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001595 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1596 idr_destroy(&tz->idr);
1597 mutex_destroy(&tz->lock);
1598 device_unregister(&tz->device);
1599 return;
1600}
Zhang Rui203d3d42008-01-17 15:51:08 +08001601EXPORT_SYMBOL(thermal_zone_device_unregister);
1602
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001603#ifdef CONFIG_NET
1604static struct genl_family thermal_event_genl_family = {
1605 .id = GENL_ID_GENERATE,
1606 .name = THERMAL_GENL_FAMILY_NAME,
1607 .version = THERMAL_GENL_VERSION,
1608 .maxattr = THERMAL_GENL_ATTR_MAX,
1609};
1610
1611static struct genl_multicast_group thermal_event_mcgrp = {
1612 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1613};
1614
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001615int thermal_generate_netlink_event(u32 orig, enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301616{
1617 struct sk_buff *skb;
1618 struct nlattr *attr;
1619 struct thermal_genl_event *thermal_event;
1620 void *msg_header;
1621 int size;
1622 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001623 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301624
1625 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001626 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1627 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301628
1629 skb = genlmsg_new(size, GFP_ATOMIC);
1630 if (!skb)
1631 return -ENOMEM;
1632
1633 /* add the genetlink message header */
1634 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1635 &thermal_event_genl_family, 0,
1636 THERMAL_GENL_CMD_EVENT);
1637 if (!msg_header) {
1638 nlmsg_free(skb);
1639 return -ENOMEM;
1640 }
1641
1642 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001643 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1644 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301645
1646 if (!attr) {
1647 nlmsg_free(skb);
1648 return -EINVAL;
1649 }
1650
1651 thermal_event = nla_data(attr);
1652 if (!thermal_event) {
1653 nlmsg_free(skb);
1654 return -EINVAL;
1655 }
1656
1657 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1658
1659 thermal_event->orig = orig;
1660 thermal_event->event = event;
1661
1662 /* send multicast genetlink message */
1663 result = genlmsg_end(skb, msg_header);
1664 if (result < 0) {
1665 nlmsg_free(skb);
1666 return result;
1667 }
1668
1669 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1670 if (result)
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001671 pr_info("failed to send netlink event:%d\n", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301672
1673 return result;
1674}
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001675EXPORT_SYMBOL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301676
1677static int genetlink_init(void)
1678{
1679 int result;
1680
1681 result = genl_register_family(&thermal_event_genl_family);
1682 if (result)
1683 return result;
1684
1685 result = genl_register_mc_group(&thermal_event_genl_family,
1686 &thermal_event_mcgrp);
1687 if (result)
1688 genl_unregister_family(&thermal_event_genl_family);
1689 return result;
1690}
1691
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001692static void genetlink_exit(void)
1693{
1694 genl_unregister_family(&thermal_event_genl_family);
1695}
1696#else /* !CONFIG_NET */
1697static inline int genetlink_init(void) { return 0; }
1698static inline void genetlink_exit(void) {}
1699#endif /* !CONFIG_NET */
1700
Zhang Rui203d3d42008-01-17 15:51:08 +08001701static int __init thermal_init(void)
1702{
1703 int result = 0;
1704
1705 result = class_register(&thermal_class);
1706 if (result) {
1707 idr_destroy(&thermal_tz_idr);
1708 idr_destroy(&thermal_cdev_idr);
1709 mutex_destroy(&thermal_idr_lock);
1710 mutex_destroy(&thermal_list_lock);
1711 }
R.Durgadoss4cb18722010-10-27 03:33:29 +05301712 result = genetlink_init();
Zhang Rui203d3d42008-01-17 15:51:08 +08001713 return result;
1714}
1715
1716static void __exit thermal_exit(void)
1717{
1718 class_unregister(&thermal_class);
1719 idr_destroy(&thermal_tz_idr);
1720 idr_destroy(&thermal_cdev_idr);
1721 mutex_destroy(&thermal_idr_lock);
1722 mutex_destroy(&thermal_list_lock);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301723 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001724}
1725
R.Durgadoss4cb18722010-10-27 03:33:29 +05301726fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001727module_exit(thermal_exit);