blob: a6933dad3bab778aed5350e51bed337433161599 [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
Durgadoss R5a2c0902012-09-18 11:04:58 +0530457static ssize_t
458policy_store(struct device *dev, struct device_attribute *attr,
459 const char *buf, size_t count)
460{
461 int ret = -EINVAL;
462 struct thermal_zone_device *tz = to_thermal_zone(dev);
463 struct thermal_governor *gov;
464
465 mutex_lock(&thermal_governor_lock);
466
467 gov = __find_governor(buf);
468 if (!gov)
469 goto exit;
470
471 tz->governor = gov;
472 ret = count;
473
474exit:
475 mutex_unlock(&thermal_governor_lock);
476 return ret;
477}
478
479static ssize_t
480policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
481{
482 struct thermal_zone_device *tz = to_thermal_zone(dev);
483
484 return sprintf(buf, "%s\n", tz->governor->name);
485}
486
Zhang Rui203d3d42008-01-17 15:51:08 +0800487static DEVICE_ATTR(type, 0444, type_show, NULL);
488static DEVICE_ATTR(temp, 0444, temp_show, NULL);
489static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700490static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530491static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800492
Zhang Rui203d3d42008-01-17 15:51:08 +0800493/* sys I/F for cooling device */
494#define to_cooling_device(_dev) \
495 container_of(_dev, struct thermal_cooling_device, device)
496
497static ssize_t
498thermal_cooling_device_type_show(struct device *dev,
499 struct device_attribute *attr, char *buf)
500{
501 struct thermal_cooling_device *cdev = to_cooling_device(dev);
502
503 return sprintf(buf, "%s\n", cdev->type);
504}
505
506static ssize_t
507thermal_cooling_device_max_state_show(struct device *dev,
508 struct device_attribute *attr, char *buf)
509{
510 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000511 unsigned long state;
512 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800513
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000514 ret = cdev->ops->get_max_state(cdev, &state);
515 if (ret)
516 return ret;
517 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800518}
519
520static ssize_t
521thermal_cooling_device_cur_state_show(struct device *dev,
522 struct device_attribute *attr, char *buf)
523{
524 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000525 unsigned long state;
526 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800527
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000528 ret = cdev->ops->get_cur_state(cdev, &state);
529 if (ret)
530 return ret;
531 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800532}
533
534static ssize_t
535thermal_cooling_device_cur_state_store(struct device *dev,
536 struct device_attribute *attr,
537 const char *buf, size_t count)
538{
539 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000540 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800541 int result;
542
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000543 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800544 return -EINVAL;
545
Roel Kluinedb94912009-12-15 22:46:50 +0100546 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800547 return -EINVAL;
548
549 result = cdev->ops->set_cur_state(cdev, state);
550 if (result)
551 return result;
552 return count;
553}
554
555static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500556__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800557static DEVICE_ATTR(max_state, 0444,
558 thermal_cooling_device_max_state_show, NULL);
559static DEVICE_ATTR(cur_state, 0644,
560 thermal_cooling_device_cur_state_show,
561 thermal_cooling_device_cur_state_store);
562
563static ssize_t
564thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500565 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800566{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800567 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800568
569 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800570 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800571
572 if (instance->trip == THERMAL_TRIPS_NONE)
573 return sprintf(buf, "-1\n");
574 else
575 return sprintf(buf, "%d\n", instance->trip);
576}
577
578/* Device management */
579
Rene Herman16d75232008-06-24 19:38:56 +0200580#if defined(CONFIG_THERMAL_HWMON)
581
Zhang Ruie68b16a2008-04-21 16:07:52 +0800582/* hwmon sys I/F */
583#include <linux/hwmon.h>
Jean Delvare31f53962011-07-28 13:48:42 -0700584
585/* thermal zone devices with the same type share one hwmon device */
586struct thermal_hwmon_device {
587 char type[THERMAL_NAME_LENGTH];
588 struct device *device;
589 int count;
590 struct list_head tz_list;
591 struct list_head node;
592};
593
594struct thermal_hwmon_attr {
595 struct device_attribute attr;
596 char name[16];
597};
598
599/* one temperature input for each thermal zone */
600struct thermal_hwmon_temp {
601 struct list_head hwmon_node;
602 struct thermal_zone_device *tz;
603 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
604 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
605};
606
Zhang Ruie68b16a2008-04-21 16:07:52 +0800607static LIST_HEAD(thermal_hwmon_list);
608
609static ssize_t
610name_show(struct device *dev, struct device_attribute *attr, char *buf)
611{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700612 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800613 return sprintf(buf, "%s\n", hwmon->type);
614}
615static DEVICE_ATTR(name, 0444, name_show, NULL);
616
617static ssize_t
618temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
619{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000620 long temperature;
621 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800622 struct thermal_hwmon_attr *hwmon_attr
623 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700624 struct thermal_hwmon_temp *temp
625 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800626 temp_input);
Jean Delvare31f53962011-07-28 13:48:42 -0700627 struct thermal_zone_device *tz = temp->tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800628
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000629 ret = tz->ops->get_temp(tz, &temperature);
630
631 if (ret)
632 return ret;
633
634 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800635}
636
637static ssize_t
638temp_crit_show(struct device *dev, struct device_attribute *attr,
639 char *buf)
640{
641 struct thermal_hwmon_attr *hwmon_attr
642 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700643 struct thermal_hwmon_temp *temp
644 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800645 temp_crit);
Jean Delvare31f53962011-07-28 13:48:42 -0700646 struct thermal_zone_device *tz = temp->tz;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000647 long temperature;
648 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800649
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000650 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
651 if (ret)
652 return ret;
653
654 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800655}
656
657
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700658static struct thermal_hwmon_device *
659thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
660{
661 struct thermal_hwmon_device *hwmon;
662
663 mutex_lock(&thermal_list_lock);
664 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
665 if (!strcmp(hwmon->type, tz->type)) {
666 mutex_unlock(&thermal_list_lock);
667 return hwmon;
668 }
669 mutex_unlock(&thermal_list_lock);
670
671 return NULL;
672}
673
Jean Delvare31f53962011-07-28 13:48:42 -0700674/* Find the temperature input matching a given thermal zone */
675static struct thermal_hwmon_temp *
676thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
677 const struct thermal_zone_device *tz)
678{
679 struct thermal_hwmon_temp *temp;
680
681 mutex_lock(&thermal_list_lock);
682 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
683 if (temp->tz == tz) {
684 mutex_unlock(&thermal_list_lock);
685 return temp;
686 }
687 mutex_unlock(&thermal_list_lock);
688
689 return NULL;
690}
691
Zhang Ruie68b16a2008-04-21 16:07:52 +0800692static int
693thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
694{
695 struct thermal_hwmon_device *hwmon;
Jean Delvare31f53962011-07-28 13:48:42 -0700696 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800697 int new_hwmon_device = 1;
698 int result;
699
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700700 hwmon = thermal_hwmon_lookup_by_type(tz);
701 if (hwmon) {
702 new_hwmon_device = 0;
703 goto register_sys_interface;
704 }
Zhang Ruie68b16a2008-04-21 16:07:52 +0800705
706 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
707 if (!hwmon)
708 return -ENOMEM;
709
710 INIT_LIST_HEAD(&hwmon->tz_list);
711 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
712 hwmon->device = hwmon_device_register(NULL);
713 if (IS_ERR(hwmon->device)) {
714 result = PTR_ERR(hwmon->device);
715 goto free_mem;
716 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700717 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800718 result = device_create_file(hwmon->device, &dev_attr_name);
719 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530720 goto free_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800721
722 register_sys_interface:
Jean Delvare31f53962011-07-28 13:48:42 -0700723 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
724 if (!temp) {
725 result = -ENOMEM;
726 goto unregister_name;
727 }
728
729 temp->tz = tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800730 hwmon->count++;
731
Guenter Roeck79a49162012-07-21 10:53:48 +1000732 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +0800733 "temp%d_input", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700734 temp->temp_input.attr.attr.name = temp->temp_input.name;
735 temp->temp_input.attr.attr.mode = 0444;
736 temp->temp_input.attr.show = temp_input_show;
737 sysfs_attr_init(&temp->temp_input.attr.attr);
738 result = device_create_file(hwmon->device, &temp->temp_input.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800739 if (result)
Jean Delvare31f53962011-07-28 13:48:42 -0700740 goto free_temp_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800741
742 if (tz->ops->get_crit_temp) {
743 unsigned long temperature;
744 if (!tz->ops->get_crit_temp(tz, &temperature)) {
Guenter Roeck79a49162012-07-21 10:53:48 +1000745 snprintf(temp->temp_crit.name,
746 sizeof(temp->temp_crit.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +0800747 "temp%d_crit", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700748 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
749 temp->temp_crit.attr.attr.mode = 0444;
750 temp->temp_crit.attr.show = temp_crit_show;
751 sysfs_attr_init(&temp->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800752 result = device_create_file(hwmon->device,
Jean Delvare31f53962011-07-28 13:48:42 -0700753 &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800754 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530755 goto unregister_input;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800756 }
757 }
758
759 mutex_lock(&thermal_list_lock);
760 if (new_hwmon_device)
761 list_add_tail(&hwmon->node, &thermal_hwmon_list);
Jean Delvare31f53962011-07-28 13:48:42 -0700762 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800763 mutex_unlock(&thermal_list_lock);
764
765 return 0;
766
Durgadoss Rb299eb52011-03-03 04:30:13 +0530767 unregister_input:
Jean Delvare31f53962011-07-28 13:48:42 -0700768 device_remove_file(hwmon->device, &temp->temp_input.attr);
769 free_temp_mem:
770 kfree(temp);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530771 unregister_name:
Zhang Ruie68b16a2008-04-21 16:07:52 +0800772 if (new_hwmon_device) {
773 device_remove_file(hwmon->device, &dev_attr_name);
774 hwmon_device_unregister(hwmon->device);
775 }
776 free_mem:
777 if (new_hwmon_device)
778 kfree(hwmon);
779
780 return result;
781}
782
783static void
784thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
785{
Jean Delvare31f53962011-07-28 13:48:42 -0700786 struct thermal_hwmon_device *hwmon;
787 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800788
Jean Delvare31f53962011-07-28 13:48:42 -0700789 hwmon = thermal_hwmon_lookup_by_type(tz);
790 if (unlikely(!hwmon)) {
791 /* Should never happen... */
792 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
793 return;
794 }
795
796 temp = thermal_hwmon_lookup_temp(hwmon, tz);
797 if (unlikely(!temp)) {
798 /* Should never happen... */
799 dev_dbg(&tz->device, "temperature input lookup failed!\n");
800 return;
801 }
802
803 device_remove_file(hwmon->device, &temp->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530804 if (tz->ops->get_crit_temp)
Jean Delvare31f53962011-07-28 13:48:42 -0700805 device_remove_file(hwmon->device, &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800806
807 mutex_lock(&thermal_list_lock);
Jean Delvare31f53962011-07-28 13:48:42 -0700808 list_del(&temp->hwmon_node);
809 kfree(temp);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800810 if (!list_empty(&hwmon->tz_list)) {
811 mutex_unlock(&thermal_list_lock);
812 return;
813 }
814 list_del(&hwmon->node);
815 mutex_unlock(&thermal_list_lock);
816
817 device_remove_file(hwmon->device, &dev_attr_name);
818 hwmon_device_unregister(hwmon->device);
819 kfree(hwmon);
820}
821#else
822static int
823thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
824{
825 return 0;
826}
827
828static void
829thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
830{
831}
832#endif
833
Matthew Garrettb1569e92008-12-03 17:55:32 +0000834static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
835 int delay)
836{
Matthew Garrettb1569e92008-12-03 17:55:32 +0000837 if (delay > 1000)
Tejun Heo41f63c52012-08-03 10:30:47 -0700838 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
839 round_jiffies(msecs_to_jiffies(delay)));
840 else if (delay)
841 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
842 msecs_to_jiffies(delay));
Matthew Garrettb1569e92008-12-03 17:55:32 +0000843 else
Tejun Heo41f63c52012-08-03 10:30:47 -0700844 cancel_delayed_work(&tz->poll_queue);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000845}
846
Matthew Garrettb1569e92008-12-03 17:55:32 +0000847static void thermal_zone_device_check(struct work_struct *work)
848{
849 struct thermal_zone_device *tz = container_of(work, struct
850 thermal_zone_device,
851 poll_queue.work);
852 thermal_zone_device_update(tz);
853}
Zhang Ruie68b16a2008-04-21 16:07:52 +0800854
Zhang Rui203d3d42008-01-17 15:51:08 +0800855/**
856 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800857 * @tz: thermal zone device
858 * @trip: indicates which trip point the cooling devices is
859 * associated with in this thermal zone.
860 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500861 *
862 * This function is usually called in the thermal zone device .bind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800863 */
864int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
865 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800866 struct thermal_cooling_device *cdev,
867 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +0800868{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800869 struct thermal_instance *dev;
870 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500871 struct thermal_zone_device *pos1;
872 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800873 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800874 int result;
875
Len Brown543a9562008-02-07 16:55:08 -0500876 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800877 return -EINVAL;
878
Thomas Sujithc7516702008-02-15 00:58:50 -0500879 list_for_each_entry(pos1, &thermal_tz_list, node) {
880 if (pos1 == tz)
881 break;
882 }
883 list_for_each_entry(pos2, &thermal_cdev_list, node) {
884 if (pos2 == cdev)
885 break;
886 }
887
888 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800889 return -EINVAL;
890
Zhang Rui9d998422012-06-26 16:35:57 +0800891 cdev->ops->get_max_state(cdev, &max_state);
892
893 /* lower default 0, upper default max_state */
894 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
895 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
896
897 if (lower > upper || upper > max_state)
898 return -EINVAL;
899
Zhang Rui203d3d42008-01-17 15:51:08 +0800900 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800901 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800902 if (!dev)
903 return -ENOMEM;
904 dev->tz = tz;
905 dev->cdev = cdev;
906 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800907 dev->upper = upper;
908 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800909 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +0800910
Zhang Rui203d3d42008-01-17 15:51:08 +0800911 result = get_idr(&tz->idr, &tz->lock, &dev->id);
912 if (result)
913 goto free_mem;
914
915 sprintf(dev->name, "cdev%d", dev->id);
916 result =
917 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
918 if (result)
919 goto release_idr;
920
921 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700922 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800923 dev->attr.attr.name = dev->attr_name;
924 dev->attr.attr.mode = 0444;
925 dev->attr.show = thermal_cooling_device_trip_point_show;
926 result = device_create_file(&tz->device, &dev->attr);
927 if (result)
928 goto remove_symbol_link;
929
930 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800931 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800932 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +0800933 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
934 result = -EEXIST;
935 break;
936 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800937 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800938 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800939 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
940 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800941 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800942 mutex_unlock(&tz->lock);
943
944 if (!result)
945 return 0;
946
947 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -0700948remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800949 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -0700950release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +0800951 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -0700952free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800953 kfree(dev);
954 return result;
955}
956EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
957
958/**
959 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +0800960 * @tz: thermal zone device
961 * @trip: indicates which trip point the cooling devices is
962 * associated with in this thermal zone.
963 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -0500964 *
965 * This function is usually called in the thermal zone device .unbind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +0800966 */
967int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
968 int trip,
969 struct thermal_cooling_device *cdev)
970{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800971 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +0800972
973 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800974 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800975 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -0500976 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800977 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800978 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800979 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800980 mutex_unlock(&tz->lock);
981 goto unbind;
982 }
983 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800984 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800985 mutex_unlock(&tz->lock);
986
987 return -ENODEV;
988
Joe Perchescaca8b82012-03-21 12:55:02 -0700989unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +0800990 device_remove_file(&tz->device, &pos->attr);
991 sysfs_remove_link(&tz->device.kobj, pos->name);
992 release_idr(&tz->idr, &tz->lock, pos->id);
993 kfree(pos);
994 return 0;
995}
996EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
997
998static void thermal_release(struct device *dev)
999{
1000 struct thermal_zone_device *tz;
1001 struct thermal_cooling_device *cdev;
1002
Joe Perchescaca8b82012-03-21 12:55:02 -07001003 if (!strncmp(dev_name(dev), "thermal_zone",
1004 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001005 tz = to_thermal_zone(dev);
1006 kfree(tz);
1007 } else {
1008 cdev = to_cooling_device(dev);
1009 kfree(cdev);
1010 }
1011}
1012
1013static struct class thermal_class = {
1014 .name = "thermal",
1015 .dev_release = thermal_release,
1016};
1017
1018/**
1019 * thermal_cooling_device_register - register a new thermal cooling device
1020 * @type: the thermal cooling device type.
1021 * @devdata: device private data.
1022 * @ops: standard thermal cooling devices callbacks.
1023 */
Joe Perchescaca8b82012-03-21 12:55:02 -07001024struct thermal_cooling_device *
1025thermal_cooling_device_register(char *type, void *devdata,
1026 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001027{
1028 struct thermal_cooling_device *cdev;
1029 struct thermal_zone_device *pos;
1030 int result;
1031
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001032 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001033 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001034
1035 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001036 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001037 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001038
1039 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1040 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001041 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001042
1043 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1044 if (result) {
1045 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001046 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001047 }
1048
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001049 strcpy(cdev->type, type ? : "");
Zhang Ruif4a821c2012-07-24 16:56:21 +08001050 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001051 INIT_LIST_HEAD(&cdev->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001052 cdev->ops = ops;
Zhang Ruice119f82012-06-27 14:13:04 +08001053 cdev->updated = true;
Zhang Rui203d3d42008-01-17 15:51:08 +08001054 cdev->device.class = &thermal_class;
1055 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001056 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001057 result = device_register(&cdev->device);
1058 if (result) {
1059 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1060 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001061 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001062 }
1063
1064 /* sys I/F */
1065 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001066 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001067 if (result)
1068 goto unregister;
1069 }
1070
1071 result = device_create_file(&cdev->device, &dev_attr_max_state);
1072 if (result)
1073 goto unregister;
1074
1075 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1076 if (result)
1077 goto unregister;
1078
1079 mutex_lock(&thermal_list_lock);
1080 list_add(&cdev->node, &thermal_cdev_list);
1081 list_for_each_entry(pos, &thermal_tz_list, node) {
1082 if (!pos->ops->bind)
1083 continue;
1084 result = pos->ops->bind(pos, cdev);
1085 if (result)
1086 break;
1087
1088 }
1089 mutex_unlock(&thermal_list_lock);
1090
1091 if (!result)
1092 return cdev;
1093
Joe Perchescaca8b82012-03-21 12:55:02 -07001094unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001095 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1096 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001097 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001098}
1099EXPORT_SYMBOL(thermal_cooling_device_register);
1100
1101/**
1102 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001103 * @cdev: the thermal cooling device to remove.
1104 *
1105 * thermal_cooling_device_unregister() must be called when the device is no
1106 * longer needed.
1107 */
1108void thermal_cooling_device_unregister(struct
1109 thermal_cooling_device
1110 *cdev)
1111{
1112 struct thermal_zone_device *tz;
1113 struct thermal_cooling_device *pos = NULL;
1114
1115 if (!cdev)
1116 return;
1117
1118 mutex_lock(&thermal_list_lock);
1119 list_for_each_entry(pos, &thermal_cdev_list, node)
1120 if (pos == cdev)
1121 break;
1122 if (pos != cdev) {
1123 /* thermal cooling device not found */
1124 mutex_unlock(&thermal_list_lock);
1125 return;
1126 }
1127 list_del(&cdev->node);
1128 list_for_each_entry(tz, &thermal_tz_list, node) {
1129 if (!tz->ops->unbind)
1130 continue;
1131 tz->ops->unbind(tz, cdev);
1132 }
1133 mutex_unlock(&thermal_list_lock);
1134 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001135 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001136 device_remove_file(&cdev->device, &dev_attr_max_state);
1137 device_remove_file(&cdev->device, &dev_attr_cur_state);
1138
1139 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1140 device_unregister(&cdev->device);
1141 return;
1142}
1143EXPORT_SYMBOL(thermal_cooling_device_unregister);
1144
Zhang Ruice119f82012-06-27 14:13:04 +08001145static void thermal_cdev_do_update(struct thermal_cooling_device *cdev)
1146{
1147 struct thermal_instance *instance;
1148 unsigned long target = 0;
1149
1150 /* cooling device is updated*/
1151 if (cdev->updated)
1152 return;
1153
Zhang Ruif4a821c2012-07-24 16:56:21 +08001154 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001155 /* Make sure cdev enters the deepest cooling state */
1156 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1157 if (instance->target == THERMAL_NO_TARGET)
1158 continue;
1159 if (instance->target > target)
1160 target = instance->target;
1161 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001162 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001163 cdev->ops->set_cur_state(cdev, target);
1164 cdev->updated = true;
1165}
1166
1167static void thermal_zone_do_update(struct thermal_zone_device *tz)
1168{
1169 struct thermal_instance *instance;
1170
1171 list_for_each_entry(instance, &tz->thermal_instances, tz_node)
1172 thermal_cdev_do_update(instance->cdev);
1173}
1174
Zhang Rui4ae46be2012-06-27 10:05:39 +08001175/*
Zhang Rui908b9fb2012-06-27 14:14:05 +08001176 * Cooling algorithm for both active and passive cooling
Zhang Rui4ae46be2012-06-27 10:05:39 +08001177 *
1178 * 1. if the temperature is higher than a trip point,
1179 * a. if the trend is THERMAL_TREND_RAISING, use higher cooling
1180 * state for this trip point
1181 * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
1182 * state for this trip point
1183 *
1184 * 2. if the temperature is lower than a trip point, use lower
1185 * cooling state for this trip point
1186 *
1187 * Note that this behaves the same as the previous passive cooling
1188 * algorithm.
1189 */
1190
1191static void thermal_zone_trip_update(struct thermal_zone_device *tz,
1192 int trip, long temp)
1193{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001194 struct thermal_instance *instance;
Zhang Rui4ae46be2012-06-27 10:05:39 +08001195 struct thermal_cooling_device *cdev = NULL;
1196 unsigned long cur_state, max_state;
1197 long trip_temp;
Zhang Rui908b9fb2012-06-27 14:14:05 +08001198 enum thermal_trip_type trip_type;
Zhang Rui4ae46be2012-06-27 10:05:39 +08001199 enum thermal_trend trend;
1200
Zhang Rui908b9fb2012-06-27 14:14:05 +08001201 if (trip == THERMAL_TRIPS_NONE) {
1202 trip_temp = tz->forced_passive;
1203 trip_type = THERMAL_TRIPS_NONE;
1204 } else {
1205 tz->ops->get_trip_temp(tz, trip, &trip_temp);
1206 tz->ops->get_trip_type(tz, trip, &trip_type);
1207 }
Zhang Rui4ae46be2012-06-27 10:05:39 +08001208
1209 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
1210 /*
1211 * compare the current temperature and previous temperature
1212 * to get the thermal trend, if no special requirement
1213 */
1214 if (tz->temperature > tz->last_temperature)
1215 trend = THERMAL_TREND_RAISING;
1216 else if (tz->temperature < tz->last_temperature)
1217 trend = THERMAL_TREND_DROPPING;
1218 else
1219 trend = THERMAL_TREND_STABLE;
1220 }
1221
1222 if (temp >= trip_temp) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001223 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
Zhang Rui4ae46be2012-06-27 10:05:39 +08001224 if (instance->trip != trip)
1225 continue;
1226
1227 cdev = instance->cdev;
1228
1229 cdev->ops->get_cur_state(cdev, &cur_state);
1230 cdev->ops->get_max_state(cdev, &max_state);
1231
1232 if (trend == THERMAL_TREND_RAISING) {
1233 cur_state = cur_state < instance->upper ?
1234 (cur_state + 1) : instance->upper;
1235 } else if (trend == THERMAL_TREND_DROPPING) {
1236 cur_state = cur_state > instance->lower ?
1237 (cur_state - 1) : instance->lower;
1238 }
Zhang Rui908b9fb2012-06-27 14:14:05 +08001239
1240 /* activate a passive thermal instance */
1241 if ((trip_type == THERMAL_TRIP_PASSIVE ||
1242 trip_type == THERMAL_TRIPS_NONE) &&
1243 instance->target == THERMAL_NO_TARGET)
1244 tz->passive++;
1245
Zhang Ruice119f82012-06-27 14:13:04 +08001246 instance->target = cur_state;
1247 cdev->updated = false; /* cooling device needs update */
Zhang Rui4ae46be2012-06-27 10:05:39 +08001248 }
1249 } else { /* below trip */
Zhang Ruicddf31b2012-06-27 10:09:36 +08001250 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
Zhang Rui4ae46be2012-06-27 10:05:39 +08001251 if (instance->trip != trip)
1252 continue;
1253
Zhang Ruice119f82012-06-27 14:13:04 +08001254 /* Do not use the inactive thermal instance */
1255 if (instance->target == THERMAL_NO_TARGET)
1256 continue;
Zhang Rui4ae46be2012-06-27 10:05:39 +08001257 cdev = instance->cdev;
1258 cdev->ops->get_cur_state(cdev, &cur_state);
1259
1260 cur_state = cur_state > instance->lower ?
Zhang Ruice119f82012-06-27 14:13:04 +08001261 (cur_state - 1) : THERMAL_NO_TARGET;
Zhang Rui908b9fb2012-06-27 14:14:05 +08001262
1263 /* deactivate a passive thermal instance */
1264 if ((trip_type == THERMAL_TRIP_PASSIVE ||
1265 trip_type == THERMAL_TRIPS_NONE) &&
1266 cur_state == THERMAL_NO_TARGET)
1267 tz->passive--;
Zhang Ruice119f82012-06-27 14:13:04 +08001268 instance->target = cur_state;
1269 cdev->updated = false; /* cooling device needs update */
Zhang Rui4ae46be2012-06-27 10:05:39 +08001270 }
1271 }
1272
1273 return;
1274}
Zhang Rui203d3d42008-01-17 15:51:08 +08001275/**
Matthew Garrettb1569e92008-12-03 17:55:32 +00001276 * thermal_zone_device_update - force an update of a thermal zone's state
1277 * @ttz: the thermal zone to update
1278 */
1279
1280void thermal_zone_device_update(struct thermal_zone_device *tz)
1281{
1282 int count, ret = 0;
1283 long temp, trip_temp;
1284 enum thermal_trip_type trip_type;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001285
1286 mutex_lock(&tz->lock);
1287
Michael Brunner0d288162009-08-26 14:29:25 -07001288 if (tz->ops->get_temp(tz, &temp)) {
1289 /* get_temp failed - retry it later */
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001290 pr_warn("failed to read out thermal zone %d\n", tz->id);
Michael Brunner0d288162009-08-26 14:29:25 -07001291 goto leave;
1292 }
Matthew Garrettb1569e92008-12-03 17:55:32 +00001293
Zhang Rui601f3d42012-06-27 09:54:33 +08001294 tz->last_temperature = tz->temperature;
1295 tz->temperature = temp;
1296
Matthew Garrettb1569e92008-12-03 17:55:32 +00001297 for (count = 0; count < tz->trips; count++) {
1298 tz->ops->get_trip_type(tz, count, &trip_type);
1299 tz->ops->get_trip_temp(tz, count, &trip_temp);
1300
1301 switch (trip_type) {
1302 case THERMAL_TRIP_CRITICAL:
Vladimir Zajac29321352009-05-06 19:34:21 +02001303 if (temp >= trip_temp) {
Matthew Garrettb1569e92008-12-03 17:55:32 +00001304 if (tz->ops->notify)
1305 ret = tz->ops->notify(tz, count,
1306 trip_type);
1307 if (!ret) {
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001308 pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1309 temp/1000);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001310 orderly_poweroff(true);
1311 }
1312 }
1313 break;
1314 case THERMAL_TRIP_HOT:
Vladimir Zajac29321352009-05-06 19:34:21 +02001315 if (temp >= trip_temp)
Matthew Garrettb1569e92008-12-03 17:55:32 +00001316 if (tz->ops->notify)
1317 tz->ops->notify(tz, count, trip_type);
1318 break;
1319 case THERMAL_TRIP_ACTIVE:
Zhang Rui4ae46be2012-06-27 10:05:39 +08001320 thermal_zone_trip_update(tz, count, temp);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001321 break;
1322 case THERMAL_TRIP_PASSIVE:
Vladimir Zajac29321352009-05-06 19:34:21 +02001323 if (temp >= trip_temp || tz->passive)
Zhang Rui908b9fb2012-06-27 14:14:05 +08001324 thermal_zone_trip_update(tz, count, temp);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001325 break;
1326 }
1327 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001328
1329 if (tz->forced_passive)
Zhang Rui908b9fb2012-06-27 14:14:05 +08001330 thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE, temp);
1331 thermal_zone_do_update(tz);
Michael Brunner0d288162009-08-26 14:29:25 -07001332
Joe Perchescaca8b82012-03-21 12:55:02 -07001333leave:
Matthew Garrettb1569e92008-12-03 17:55:32 +00001334 if (tz->passive)
1335 thermal_zone_device_set_polling(tz, tz->passive_delay);
1336 else if (tz->polling_delay)
1337 thermal_zone_device_set_polling(tz, tz->polling_delay);
Frans Pop3767cb52009-10-26 08:39:04 +01001338 else
1339 thermal_zone_device_set_polling(tz, 0);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001340 mutex_unlock(&tz->lock);
1341}
1342EXPORT_SYMBOL(thermal_zone_device_update);
1343
1344/**
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001345 * create_trip_attrs - create attributes for trip points
1346 * @tz: the thermal zone device
1347 * @mask: Writeable trip point bitmap.
1348 */
1349static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1350{
1351 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001352 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001353
Durgadoss R27365a62012-07-25 10:10:59 +08001354 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001355 if (!tz->trip_type_attrs)
1356 return -ENOMEM;
1357
Durgadoss R27365a62012-07-25 10:10:59 +08001358 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001359 if (!tz->trip_temp_attrs) {
1360 kfree(tz->trip_type_attrs);
1361 return -ENOMEM;
1362 }
1363
Durgadoss R27365a62012-07-25 10:10:59 +08001364 if (tz->ops->get_trip_hyst) {
1365 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1366 if (!tz->trip_hyst_attrs) {
1367 kfree(tz->trip_type_attrs);
1368 kfree(tz->trip_temp_attrs);
1369 return -ENOMEM;
1370 }
1371 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001372
Durgadoss R27365a62012-07-25 10:10:59 +08001373
1374 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001375 /* create trip type attribute */
1376 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1377 "trip_point_%d_type", indx);
1378
1379 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1380 tz->trip_type_attrs[indx].attr.attr.name =
1381 tz->trip_type_attrs[indx].name;
1382 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1383 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1384
1385 device_create_file(&tz->device,
1386 &tz->trip_type_attrs[indx].attr);
1387
1388 /* create trip temp attribute */
1389 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1390 "trip_point_%d_temp", indx);
1391
1392 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1393 tz->trip_temp_attrs[indx].attr.attr.name =
1394 tz->trip_temp_attrs[indx].name;
1395 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1396 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1397 if (mask & (1 << indx)) {
1398 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1399 tz->trip_temp_attrs[indx].attr.store =
1400 trip_point_temp_store;
1401 }
1402
1403 device_create_file(&tz->device,
1404 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001405
1406 /* create Optional trip hyst attribute */
1407 if (!tz->ops->get_trip_hyst)
1408 continue;
1409 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1410 "trip_point_%d_hyst", indx);
1411
1412 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1413 tz->trip_hyst_attrs[indx].attr.attr.name =
1414 tz->trip_hyst_attrs[indx].name;
1415 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1416 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1417 if (tz->ops->set_trip_hyst) {
1418 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1419 tz->trip_hyst_attrs[indx].attr.store =
1420 trip_point_hyst_store;
1421 }
1422
1423 device_create_file(&tz->device,
1424 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001425 }
1426 return 0;
1427}
1428
1429static void remove_trip_attrs(struct thermal_zone_device *tz)
1430{
1431 int indx;
1432
1433 for (indx = 0; indx < tz->trips; indx++) {
1434 device_remove_file(&tz->device,
1435 &tz->trip_type_attrs[indx].attr);
1436 device_remove_file(&tz->device,
1437 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001438 if (tz->ops->get_trip_hyst)
1439 device_remove_file(&tz->device,
1440 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001441 }
1442 kfree(tz->trip_type_attrs);
1443 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001444 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001445}
1446
1447/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001448 * thermal_zone_device_register - register a new thermal zone device
1449 * @type: the thermal zone device type
1450 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001451 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001452 * @devdata: private device data
1453 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301454 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001455 * @passive_delay: number of milliseconds to wait between polls when
1456 * performing passive cooling
1457 * @polling_delay: number of milliseconds to wait between polls when checking
1458 * whether trip points have been crossed (0 for interrupt
1459 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001460 *
1461 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001462 * longer needed. The passive cooling depends on the .get_trend() return value.
Zhang Rui203d3d42008-01-17 15:51:08 +08001463 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001464struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001465 int trips, int mask, void *devdata,
Alan Cox5b275ce2010-11-11 15:27:29 +00001466 const struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301467 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001468 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001469{
1470 struct thermal_zone_device *tz;
1471 struct thermal_cooling_device *pos;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001472 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001473 int result;
1474 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001475 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001476
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001477 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001478 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001479
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001480 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001481 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001482
1483 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001484 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001485
1486 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1487 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001488 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001489
Zhang Rui2d374132012-06-27 10:09:00 +08001490 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001491 idr_init(&tz->idr);
1492 mutex_init(&tz->lock);
1493 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1494 if (result) {
1495 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001496 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001497 }
1498
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001499 strcpy(tz->type, type ? : "");
Zhang Rui203d3d42008-01-17 15:51:08 +08001500 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301501 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001502 tz->device.class = &thermal_class;
1503 tz->devdata = devdata;
1504 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001505 tz->passive_delay = passive_delay;
1506 tz->polling_delay = polling_delay;
1507
Kay Sievers354655e2009-01-06 10:44:37 -08001508 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001509 result = device_register(&tz->device);
1510 if (result) {
1511 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1512 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001513 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001514 }
1515
1516 /* sys I/F */
1517 if (type) {
1518 result = device_create_file(&tz->device, &dev_attr_type);
1519 if (result)
1520 goto unregister;
1521 }
1522
1523 result = device_create_file(&tz->device, &dev_attr_temp);
1524 if (result)
1525 goto unregister;
1526
1527 if (ops->get_mode) {
1528 result = device_create_file(&tz->device, &dev_attr_mode);
1529 if (result)
1530 goto unregister;
1531 }
1532
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001533 result = create_trip_attrs(tz, mask);
1534 if (result)
1535 goto unregister;
1536
Zhang Rui203d3d42008-01-17 15:51:08 +08001537 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001538 tz->ops->get_trip_type(tz, count, &trip_type);
1539 if (trip_type == THERMAL_TRIP_PASSIVE)
1540 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001541 }
1542
Durgadoss R5a2c0902012-09-18 11:04:58 +05301543 if (!passive) {
1544 result = device_create_file(&tz->device, &dev_attr_passive);
1545 if (result)
1546 goto unregister;
1547 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001548
Durgadoss R5a2c0902012-09-18 11:04:58 +05301549 /* Create policy attribute */
1550 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001551 if (result)
1552 goto unregister;
1553
Durgadoss Ra4a15482012-09-18 11:04:57 +05301554 /* Update 'this' zone's governor information */
1555 mutex_lock(&thermal_governor_lock);
1556
1557 if (tz->tzp)
1558 tz->governor = __find_governor(tz->tzp->governor_name);
1559 else
1560 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1561
1562 mutex_unlock(&thermal_governor_lock);
1563
Zhang Ruie68b16a2008-04-21 16:07:52 +08001564 result = thermal_add_hwmon_sysfs(tz);
1565 if (result)
1566 goto unregister;
1567
Zhang Rui203d3d42008-01-17 15:51:08 +08001568 mutex_lock(&thermal_list_lock);
1569 list_add_tail(&tz->node, &thermal_tz_list);
1570 if (ops->bind)
1571 list_for_each_entry(pos, &thermal_cdev_list, node) {
Len Brown543a9562008-02-07 16:55:08 -05001572 result = ops->bind(tz, pos);
1573 if (result)
1574 break;
Zhang Rui203d3d42008-01-17 15:51:08 +08001575 }
1576 mutex_unlock(&thermal_list_lock);
1577
Matthew Garrettb1569e92008-12-03 17:55:32 +00001578 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1579
1580 thermal_zone_device_update(tz);
1581
Zhang Rui203d3d42008-01-17 15:51:08 +08001582 if (!result)
1583 return tz;
1584
Joe Perchescaca8b82012-03-21 12:55:02 -07001585unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001586 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1587 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001588 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001589}
1590EXPORT_SYMBOL(thermal_zone_device_register);
1591
1592/**
1593 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001594 * @tz: the thermal zone device to remove
1595 */
1596void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1597{
1598 struct thermal_cooling_device *cdev;
1599 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001600
1601 if (!tz)
1602 return;
1603
1604 mutex_lock(&thermal_list_lock);
1605 list_for_each_entry(pos, &thermal_tz_list, node)
1606 if (pos == tz)
1607 break;
1608 if (pos != tz) {
1609 /* thermal zone device not found */
1610 mutex_unlock(&thermal_list_lock);
1611 return;
1612 }
1613 list_del(&tz->node);
1614 if (tz->ops->unbind)
1615 list_for_each_entry(cdev, &thermal_cdev_list, node)
1616 tz->ops->unbind(tz, cdev);
1617 mutex_unlock(&thermal_list_lock);
1618
Matthew Garrettb1569e92008-12-03 17:55:32 +00001619 thermal_zone_device_set_polling(tz, 0);
1620
Zhang Rui203d3d42008-01-17 15:51:08 +08001621 if (tz->type[0])
1622 device_remove_file(&tz->device, &dev_attr_type);
1623 device_remove_file(&tz->device, &dev_attr_temp);
1624 if (tz->ops->get_mode)
1625 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301626 device_remove_file(&tz->device, &dev_attr_policy);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001627 remove_trip_attrs(tz);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301628 tz->governor = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001629
Zhang Ruie68b16a2008-04-21 16:07:52 +08001630 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001631 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1632 idr_destroy(&tz->idr);
1633 mutex_destroy(&tz->lock);
1634 device_unregister(&tz->device);
1635 return;
1636}
Zhang Rui203d3d42008-01-17 15:51:08 +08001637EXPORT_SYMBOL(thermal_zone_device_unregister);
1638
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001639#ifdef CONFIG_NET
1640static struct genl_family thermal_event_genl_family = {
1641 .id = GENL_ID_GENERATE,
1642 .name = THERMAL_GENL_FAMILY_NAME,
1643 .version = THERMAL_GENL_VERSION,
1644 .maxattr = THERMAL_GENL_ATTR_MAX,
1645};
1646
1647static struct genl_multicast_group thermal_event_mcgrp = {
1648 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1649};
1650
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001651int thermal_generate_netlink_event(u32 orig, enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301652{
1653 struct sk_buff *skb;
1654 struct nlattr *attr;
1655 struct thermal_genl_event *thermal_event;
1656 void *msg_header;
1657 int size;
1658 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001659 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301660
1661 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001662 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1663 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301664
1665 skb = genlmsg_new(size, GFP_ATOMIC);
1666 if (!skb)
1667 return -ENOMEM;
1668
1669 /* add the genetlink message header */
1670 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1671 &thermal_event_genl_family, 0,
1672 THERMAL_GENL_CMD_EVENT);
1673 if (!msg_header) {
1674 nlmsg_free(skb);
1675 return -ENOMEM;
1676 }
1677
1678 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001679 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1680 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301681
1682 if (!attr) {
1683 nlmsg_free(skb);
1684 return -EINVAL;
1685 }
1686
1687 thermal_event = nla_data(attr);
1688 if (!thermal_event) {
1689 nlmsg_free(skb);
1690 return -EINVAL;
1691 }
1692
1693 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1694
1695 thermal_event->orig = orig;
1696 thermal_event->event = event;
1697
1698 /* send multicast genetlink message */
1699 result = genlmsg_end(skb, msg_header);
1700 if (result < 0) {
1701 nlmsg_free(skb);
1702 return result;
1703 }
1704
1705 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1706 if (result)
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001707 pr_info("failed to send netlink event:%d\n", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301708
1709 return result;
1710}
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001711EXPORT_SYMBOL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301712
1713static int genetlink_init(void)
1714{
1715 int result;
1716
1717 result = genl_register_family(&thermal_event_genl_family);
1718 if (result)
1719 return result;
1720
1721 result = genl_register_mc_group(&thermal_event_genl_family,
1722 &thermal_event_mcgrp);
1723 if (result)
1724 genl_unregister_family(&thermal_event_genl_family);
1725 return result;
1726}
1727
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001728static void genetlink_exit(void)
1729{
1730 genl_unregister_family(&thermal_event_genl_family);
1731}
1732#else /* !CONFIG_NET */
1733static inline int genetlink_init(void) { return 0; }
1734static inline void genetlink_exit(void) {}
1735#endif /* !CONFIG_NET */
1736
Zhang Rui203d3d42008-01-17 15:51:08 +08001737static int __init thermal_init(void)
1738{
1739 int result = 0;
1740
1741 result = class_register(&thermal_class);
1742 if (result) {
1743 idr_destroy(&thermal_tz_idr);
1744 idr_destroy(&thermal_cdev_idr);
1745 mutex_destroy(&thermal_idr_lock);
1746 mutex_destroy(&thermal_list_lock);
1747 }
R.Durgadoss4cb18722010-10-27 03:33:29 +05301748 result = genetlink_init();
Zhang Rui203d3d42008-01-17 15:51:08 +08001749 return result;
1750}
1751
1752static void __exit thermal_exit(void)
1753{
1754 class_unregister(&thermal_class);
1755 idr_destroy(&thermal_tz_idr);
1756 idr_destroy(&thermal_cdev_idr);
1757 mutex_destroy(&thermal_idr_lock);
1758 mutex_destroy(&thermal_list_lock);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301759 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001760}
1761
R.Durgadoss4cb18722010-10-27 03:33:29 +05301762fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001763module_exit(thermal_exit);