blob: e381d521c35560ab4c0cd4cb467837643cfd0120 [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>
Matthew Garrettb1569e92008-12-03 17:55:32 +000035#include <linux/reboot.h>
Andy Shevchenko42a5bf52013-05-17 11:52:02 +000036#include <linux/string.h>
Eduardo Valentina116b5d2013-09-26 15:55:01 -040037#include <linux/of.h>
R.Durgadoss4cb18722010-10-27 03:33:29 +053038#include <net/netlink.h>
39#include <net/genetlink.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080040
Durgadoss R71350db2012-09-18 11:04:53 +053041#include "thermal_core.h"
Eduardo Valentin0dd88792013-07-03 15:14:28 -040042#include "thermal_hwmon.h"
Durgadoss R71350db2012-09-18 11:04:53 +053043
Zhang Rui63c4ec92008-04-21 16:07:13 +080044MODULE_AUTHOR("Zhang Rui");
Zhang Rui203d3d42008-01-17 15:51:08 +080045MODULE_DESCRIPTION("Generic thermal management sysfs support");
Eduardo Valentin6d8d4972013-04-23 21:48:13 +000046MODULE_LICENSE("GPL v2");
Zhang Rui203d3d42008-01-17 15:51:08 +080047
Zhang Rui203d3d42008-01-17 15:51:08 +080048static DEFINE_IDR(thermal_tz_idr);
49static DEFINE_IDR(thermal_cdev_idr);
50static DEFINE_MUTEX(thermal_idr_lock);
51
52static LIST_HEAD(thermal_tz_list);
53static LIST_HEAD(thermal_cdev_list);
Durgadoss Ra4a15482012-09-18 11:04:57 +053054static LIST_HEAD(thermal_governor_list);
55
Zhang Rui203d3d42008-01-17 15:51:08 +080056static DEFINE_MUTEX(thermal_list_lock);
Durgadoss Ra4a15482012-09-18 11:04:57 +053057static DEFINE_MUTEX(thermal_governor_lock);
58
59static struct thermal_governor *__find_governor(const char *name)
60{
61 struct thermal_governor *pos;
62
63 list_for_each_entry(pos, &thermal_governor_list, governor_list)
64 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
65 return pos;
66
67 return NULL;
68}
69
70int thermal_register_governor(struct thermal_governor *governor)
71{
72 int err;
73 const char *name;
74 struct thermal_zone_device *pos;
75
76 if (!governor)
77 return -EINVAL;
78
79 mutex_lock(&thermal_governor_lock);
80
81 err = -EBUSY;
82 if (__find_governor(governor->name) == NULL) {
83 err = 0;
84 list_add(&governor->governor_list, &thermal_governor_list);
85 }
86
87 mutex_lock(&thermal_list_lock);
88
89 list_for_each_entry(pos, &thermal_tz_list, node) {
90 if (pos->governor)
91 continue;
92 if (pos->tzp)
93 name = pos->tzp->governor_name;
94 else
95 name = DEFAULT_THERMAL_GOVERNOR;
96 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
97 pos->governor = governor;
98 }
99
100 mutex_unlock(&thermal_list_lock);
101 mutex_unlock(&thermal_governor_lock);
102
103 return err;
104}
Durgadoss Ra4a15482012-09-18 11:04:57 +0530105
106void thermal_unregister_governor(struct thermal_governor *governor)
107{
108 struct thermal_zone_device *pos;
109
110 if (!governor)
111 return;
112
113 mutex_lock(&thermal_governor_lock);
114
115 if (__find_governor(governor->name) == NULL)
116 goto exit;
117
118 mutex_lock(&thermal_list_lock);
119
120 list_for_each_entry(pos, &thermal_tz_list, node) {
121 if (!strnicmp(pos->governor->name, governor->name,
122 THERMAL_NAME_LENGTH))
123 pos->governor = NULL;
124 }
125
126 mutex_unlock(&thermal_list_lock);
127 list_del(&governor->governor_list);
128exit:
129 mutex_unlock(&thermal_governor_lock);
130 return;
131}
Zhang Rui203d3d42008-01-17 15:51:08 +0800132
133static int get_idr(struct idr *idr, struct mutex *lock, int *id)
134{
Tejun Heo6deb69f2013-02-27 17:04:46 -0800135 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800136
137 if (lock)
138 mutex_lock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800139 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800140 if (lock)
141 mutex_unlock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800142 if (unlikely(ret < 0))
143 return ret;
144 *id = ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800145 return 0;
146}
147
148static void release_idr(struct idr *idr, struct mutex *lock, int id)
149{
150 if (lock)
151 mutex_lock(lock);
152 idr_remove(idr, id);
153 if (lock)
154 mutex_unlock(lock);
155}
156
Durgadoss R9b4298a2012-09-18 11:04:54 +0530157int get_tz_trend(struct thermal_zone_device *tz, int trip)
158{
159 enum thermal_trend trend;
160
Eduardo Valentin0c872502013-05-29 21:37:00 +0000161 if (tz->emul_temperature || !tz->ops->get_trend ||
162 tz->ops->get_trend(tz, trip, &trend)) {
Durgadoss R9b4298a2012-09-18 11:04:54 +0530163 if (tz->temperature > tz->last_temperature)
164 trend = THERMAL_TREND_RAISING;
165 else if (tz->temperature < tz->last_temperature)
166 trend = THERMAL_TREND_DROPPING;
167 else
168 trend = THERMAL_TREND_STABLE;
169 }
170
171 return trend;
172}
173EXPORT_SYMBOL(get_tz_trend);
174
175struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
176 struct thermal_cooling_device *cdev, int trip)
177{
178 struct thermal_instance *pos = NULL;
179 struct thermal_instance *target_instance = NULL;
180
181 mutex_lock(&tz->lock);
182 mutex_lock(&cdev->lock);
183
184 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
185 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
186 target_instance = pos;
187 break;
188 }
189 }
190
191 mutex_unlock(&cdev->lock);
192 mutex_unlock(&tz->lock);
193
194 return target_instance;
195}
196EXPORT_SYMBOL(get_thermal_instance);
197
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530198static void print_bind_err_msg(struct thermal_zone_device *tz,
199 struct thermal_cooling_device *cdev, int ret)
200{
201 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
202 tz->type, cdev->type, ret);
203}
204
205static void __bind(struct thermal_zone_device *tz, int mask,
Eduardo Valentina8892d832013-07-16 15:26:28 -0400206 struct thermal_cooling_device *cdev,
207 unsigned long *limits)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530208{
209 int i, ret;
210
211 for (i = 0; i < tz->trips; i++) {
212 if (mask & (1 << i)) {
Eduardo Valentina8892d832013-07-16 15:26:28 -0400213 unsigned long upper, lower;
214
215 upper = THERMAL_NO_LIMIT;
216 lower = THERMAL_NO_LIMIT;
217 if (limits) {
218 lower = limits[i * 2];
219 upper = limits[i * 2 + 1];
220 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530221 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
Eduardo Valentina8892d832013-07-16 15:26:28 -0400222 upper, lower);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530223 if (ret)
224 print_bind_err_msg(tz, cdev, ret);
225 }
226 }
227}
228
229static void __unbind(struct thermal_zone_device *tz, int mask,
230 struct thermal_cooling_device *cdev)
231{
232 int i;
233
234 for (i = 0; i < tz->trips; i++)
235 if (mask & (1 << i))
236 thermal_zone_unbind_cooling_device(tz, i, cdev);
237}
238
239static void bind_cdev(struct thermal_cooling_device *cdev)
240{
241 int i, ret;
242 const struct thermal_zone_params *tzp;
243 struct thermal_zone_device *pos = NULL;
244
245 mutex_lock(&thermal_list_lock);
246
247 list_for_each_entry(pos, &thermal_tz_list, node) {
248 if (!pos->tzp && !pos->ops->bind)
249 continue;
250
Ni Wadea9f2d192013-11-06 14:30:13 +0800251 if (pos->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530252 ret = pos->ops->bind(pos, cdev);
253 if (ret)
254 print_bind_err_msg(pos, cdev, ret);
Ni Wadea9f2d192013-11-06 14:30:13 +0800255 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530256 }
257
258 tzp = pos->tzp;
Hugh Dickins791700c2012-09-27 16:48:28 -0700259 if (!tzp || !tzp->tbp)
260 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530261
262 for (i = 0; i < tzp->num_tbps; i++) {
263 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
264 continue;
265 if (tzp->tbp[i].match(pos, cdev))
266 continue;
267 tzp->tbp[i].cdev = cdev;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400268 __bind(pos, tzp->tbp[i].trip_mask, cdev,
269 tzp->tbp[i].binding_limits);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530270 }
271 }
272
273 mutex_unlock(&thermal_list_lock);
274}
275
276static void bind_tz(struct thermal_zone_device *tz)
277{
278 int i, ret;
279 struct thermal_cooling_device *pos = NULL;
280 const struct thermal_zone_params *tzp = tz->tzp;
281
282 if (!tzp && !tz->ops->bind)
283 return;
284
285 mutex_lock(&thermal_list_lock);
286
Ni Wadea9f2d192013-11-06 14:30:13 +0800287 /* If there is ops->bind, try to use ops->bind */
288 if (tz->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530289 list_for_each_entry(pos, &thermal_cdev_list, node) {
290 ret = tz->ops->bind(tz, pos);
291 if (ret)
292 print_bind_err_msg(tz, pos, ret);
293 }
294 goto exit;
295 }
296
Hugh Dickins791700c2012-09-27 16:48:28 -0700297 if (!tzp || !tzp->tbp)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530298 goto exit;
299
300 list_for_each_entry(pos, &thermal_cdev_list, node) {
301 for (i = 0; i < tzp->num_tbps; i++) {
302 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
303 continue;
304 if (tzp->tbp[i].match(tz, pos))
305 continue;
306 tzp->tbp[i].cdev = pos;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400307 __bind(tz, tzp->tbp[i].trip_mask, pos,
308 tzp->tbp[i].binding_limits);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530309 }
310 }
311exit:
312 mutex_unlock(&thermal_list_lock);
313}
314
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530315static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
316 int delay)
317{
318 if (delay > 1000)
319 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
320 round_jiffies(msecs_to_jiffies(delay)));
321 else if (delay)
322 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
323 msecs_to_jiffies(delay));
324 else
325 cancel_delayed_work(&tz->poll_queue);
326}
327
328static void monitor_thermal_zone(struct thermal_zone_device *tz)
329{
330 mutex_lock(&tz->lock);
331
332 if (tz->passive)
333 thermal_zone_device_set_polling(tz, tz->passive_delay);
334 else if (tz->polling_delay)
335 thermal_zone_device_set_polling(tz, tz->polling_delay);
336 else
337 thermal_zone_device_set_polling(tz, 0);
338
339 mutex_unlock(&tz->lock);
340}
341
342static void handle_non_critical_trips(struct thermal_zone_device *tz,
343 int trip, enum thermal_trip_type trip_type)
344{
Zhang Ruid567c682012-12-12 15:23:54 +0800345 if (tz->governor)
346 tz->governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530347}
348
349static void handle_critical_trips(struct thermal_zone_device *tz,
350 int trip, enum thermal_trip_type trip_type)
351{
352 long trip_temp;
353
354 tz->ops->get_trip_temp(tz, trip, &trip_temp);
355
356 /* If we have not crossed the trip_temp, we do not care. */
357 if (tz->temperature < trip_temp)
358 return;
359
360 if (tz->ops->notify)
361 tz->ops->notify(tz, trip, trip_type);
362
363 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000364 dev_emerg(&tz->device,
365 "critical temperature reached(%d C),shutting down\n",
366 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530367 orderly_poweroff(true);
368 }
369}
370
371static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
372{
373 enum thermal_trip_type type;
374
375 tz->ops->get_trip_type(tz, trip, &type);
376
377 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
378 handle_critical_trips(tz, trip, type);
379 else
380 handle_non_critical_trips(tz, trip, type);
381 /*
382 * Alright, we handled this trip successfully.
383 * So, start monitoring again.
384 */
385 monitor_thermal_zone(tz);
386}
387
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000388/**
389 * thermal_zone_get_temp() - returns its the temperature of thermal zone
390 * @tz: a valid pointer to a struct thermal_zone_device
391 * @temp: a valid pointer to where to store the resulting temperature.
392 *
393 * When a valid thermal zone reference is passed, it will fetch its
394 * temperature and fill @temp.
395 *
396 * Return: On success returns 0, an error code otherwise
397 */
398int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000399{
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000400 int ret = -EINVAL;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800401#ifdef CONFIG_THERMAL_EMULATION
402 int count;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000403 unsigned long crit_temp = -1UL;
404 enum thermal_trip_type type;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800405#endif
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000406
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400407 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000408 goto exit;
409
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000410 mutex_lock(&tz->lock);
411
412 ret = tz->ops->get_temp(tz, temp);
413#ifdef CONFIG_THERMAL_EMULATION
414 if (!tz->emul_temperature)
415 goto skip_emul;
416
417 for (count = 0; count < tz->trips; count++) {
418 ret = tz->ops->get_trip_type(tz, count, &type);
419 if (!ret && type == THERMAL_TRIP_CRITICAL) {
420 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
421 break;
422 }
423 }
424
425 if (ret)
426 goto skip_emul;
427
428 if (*temp < crit_temp)
429 *temp = tz->emul_temperature;
430skip_emul:
431#endif
432 mutex_unlock(&tz->lock);
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000433exit:
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000434 return ret;
435}
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000436EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000437
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530438static void update_temperature(struct thermal_zone_device *tz)
439{
440 long temp;
441 int ret;
442
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000443 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530444 if (ret) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000445 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
446 tz->id);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000447 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530448 }
449
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000450 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530451 tz->last_temperature = tz->temperature;
452 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530453 mutex_unlock(&tz->lock);
454}
455
456void thermal_zone_device_update(struct thermal_zone_device *tz)
457{
458 int count;
459
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400460 if (!tz->ops->get_temp)
461 return;
462
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530463 update_temperature(tz);
464
465 for (count = 0; count < tz->trips; count++)
466 handle_thermal_trip(tz, count);
467}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000468EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530469
470static void thermal_zone_device_check(struct work_struct *work)
471{
472 struct thermal_zone_device *tz = container_of(work, struct
473 thermal_zone_device,
474 poll_queue.work);
475 thermal_zone_device_update(tz);
476}
477
Zhang Rui203d3d42008-01-17 15:51:08 +0800478/* sys I/F for thermal zone */
479
480#define to_thermal_zone(_dev) \
481 container_of(_dev, struct thermal_zone_device, device)
482
483static ssize_t
484type_show(struct device *dev, struct device_attribute *attr, char *buf)
485{
486 struct thermal_zone_device *tz = to_thermal_zone(dev);
487
488 return sprintf(buf, "%s\n", tz->type);
489}
490
491static ssize_t
492temp_show(struct device *dev, struct device_attribute *attr, char *buf)
493{
494 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000495 long temperature;
496 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800497
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000498 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000499
500 if (ret)
501 return ret;
502
503 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800504}
505
506static ssize_t
507mode_show(struct device *dev, struct device_attribute *attr, char *buf)
508{
509 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000510 enum thermal_device_mode mode;
511 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800512
513 if (!tz->ops->get_mode)
514 return -EPERM;
515
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000516 result = tz->ops->get_mode(tz, &mode);
517 if (result)
518 return result;
519
520 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
521 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800522}
523
524static ssize_t
525mode_store(struct device *dev, struct device_attribute *attr,
526 const char *buf, size_t count)
527{
528 struct thermal_zone_device *tz = to_thermal_zone(dev);
529 int result;
530
531 if (!tz->ops->set_mode)
532 return -EPERM;
533
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530534 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000535 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530536 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000537 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
538 else
539 result = -EINVAL;
540
Zhang Rui203d3d42008-01-17 15:51:08 +0800541 if (result)
542 return result;
543
544 return count;
545}
546
547static ssize_t
548trip_point_type_show(struct device *dev, struct device_attribute *attr,
549 char *buf)
550{
551 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000552 enum thermal_trip_type type;
553 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800554
555 if (!tz->ops->get_trip_type)
556 return -EPERM;
557
558 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
559 return -EINVAL;
560
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000561 result = tz->ops->get_trip_type(tz, trip, &type);
562 if (result)
563 return result;
564
565 switch (type) {
566 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300567 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000568 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300569 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000570 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300571 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000572 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300573 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000574 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300575 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000576 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800577}
578
579static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800580trip_point_temp_store(struct device *dev, struct device_attribute *attr,
581 const char *buf, size_t count)
582{
583 struct thermal_zone_device *tz = to_thermal_zone(dev);
584 int trip, ret;
585 unsigned long temperature;
586
587 if (!tz->ops->set_trip_temp)
588 return -EPERM;
589
590 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
591 return -EINVAL;
592
593 if (kstrtoul(buf, 10, &temperature))
594 return -EINVAL;
595
596 ret = tz->ops->set_trip_temp(tz, trip, temperature);
597
598 return ret ? ret : count;
599}
600
601static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800602trip_point_temp_show(struct device *dev, struct device_attribute *attr,
603 char *buf)
604{
605 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000606 int trip, ret;
607 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800608
609 if (!tz->ops->get_trip_temp)
610 return -EPERM;
611
612 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
613 return -EINVAL;
614
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000615 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
616
617 if (ret)
618 return ret;
619
620 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800621}
622
Matthew Garrett03a971a2008-12-03 18:00:38 +0000623static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800624trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
625 const char *buf, size_t count)
626{
627 struct thermal_zone_device *tz = to_thermal_zone(dev);
628 int trip, ret;
629 unsigned long temperature;
630
631 if (!tz->ops->set_trip_hyst)
632 return -EPERM;
633
634 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
635 return -EINVAL;
636
637 if (kstrtoul(buf, 10, &temperature))
638 return -EINVAL;
639
640 /*
641 * We are not doing any check on the 'temperature' value
642 * here. The driver implementing 'set_trip_hyst' has to
643 * take care of this.
644 */
645 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
646
647 return ret ? ret : count;
648}
649
650static ssize_t
651trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
652 char *buf)
653{
654 struct thermal_zone_device *tz = to_thermal_zone(dev);
655 int trip, ret;
656 unsigned long temperature;
657
658 if (!tz->ops->get_trip_hyst)
659 return -EPERM;
660
661 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
662 return -EINVAL;
663
664 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
665
666 return ret ? ret : sprintf(buf, "%ld\n", temperature);
667}
668
669static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000670passive_store(struct device *dev, struct device_attribute *attr,
671 const char *buf, size_t count)
672{
673 struct thermal_zone_device *tz = to_thermal_zone(dev);
674 struct thermal_cooling_device *cdev = NULL;
675 int state;
676
677 if (!sscanf(buf, "%d\n", &state))
678 return -EINVAL;
679
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100680 /* sanity check: values below 1000 millicelcius don't make sense
681 * and can cause the system to go into a thermal heart attack
682 */
683 if (state && state < 1000)
684 return -EINVAL;
685
Matthew Garrett03a971a2008-12-03 18:00:38 +0000686 if (state && !tz->forced_passive) {
687 mutex_lock(&thermal_list_lock);
688 list_for_each_entry(cdev, &thermal_cdev_list, node) {
689 if (!strncmp("Processor", cdev->type,
690 sizeof("Processor")))
691 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800692 THERMAL_TRIPS_NONE, cdev,
693 THERMAL_NO_LIMIT,
694 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000695 }
696 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100697 if (!tz->passive_delay)
698 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000699 } else if (!state && tz->forced_passive) {
700 mutex_lock(&thermal_list_lock);
701 list_for_each_entry(cdev, &thermal_cdev_list, node) {
702 if (!strncmp("Processor", cdev->type,
703 sizeof("Processor")))
704 thermal_zone_unbind_cooling_device(tz,
705 THERMAL_TRIPS_NONE,
706 cdev);
707 }
708 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100709 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000710 }
711
Matthew Garrett03a971a2008-12-03 18:00:38 +0000712 tz->forced_passive = state;
713
714 thermal_zone_device_update(tz);
715
716 return count;
717}
718
719static ssize_t
720passive_show(struct device *dev, struct device_attribute *attr,
721 char *buf)
722{
723 struct thermal_zone_device *tz = to_thermal_zone(dev);
724
725 return sprintf(buf, "%d\n", tz->forced_passive);
726}
727
Durgadoss R5a2c0902012-09-18 11:04:58 +0530728static ssize_t
729policy_store(struct device *dev, struct device_attribute *attr,
730 const char *buf, size_t count)
731{
732 int ret = -EINVAL;
733 struct thermal_zone_device *tz = to_thermal_zone(dev);
734 struct thermal_governor *gov;
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000735 char name[THERMAL_NAME_LENGTH];
736
737 snprintf(name, sizeof(name), "%s", buf);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530738
739 mutex_lock(&thermal_governor_lock);
740
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000741 gov = __find_governor(strim(name));
Durgadoss R5a2c0902012-09-18 11:04:58 +0530742 if (!gov)
743 goto exit;
744
745 tz->governor = gov;
746 ret = count;
747
748exit:
749 mutex_unlock(&thermal_governor_lock);
750 return ret;
751}
752
753static ssize_t
754policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
755{
756 struct thermal_zone_device *tz = to_thermal_zone(dev);
757
758 return sprintf(buf, "%s\n", tz->governor->name);
759}
760
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000761#ifdef CONFIG_THERMAL_EMULATION
762static ssize_t
763emul_temp_store(struct device *dev, struct device_attribute *attr,
764 const char *buf, size_t count)
765{
766 struct thermal_zone_device *tz = to_thermal_zone(dev);
767 int ret = 0;
768 unsigned long temperature;
769
770 if (kstrtoul(buf, 10, &temperature))
771 return -EINVAL;
772
773 if (!tz->ops->set_emul_temp) {
774 mutex_lock(&tz->lock);
775 tz->emul_temperature = temperature;
776 mutex_unlock(&tz->lock);
777 } else {
778 ret = tz->ops->set_emul_temp(tz, temperature);
779 }
780
781 return ret ? ret : count;
782}
783static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
784#endif/*CONFIG_THERMAL_EMULATION*/
785
Zhang Rui203d3d42008-01-17 15:51:08 +0800786static DEVICE_ATTR(type, 0444, type_show, NULL);
787static DEVICE_ATTR(temp, 0444, temp_show, NULL);
788static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700789static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530790static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800791
Zhang Rui203d3d42008-01-17 15:51:08 +0800792/* sys I/F for cooling device */
793#define to_cooling_device(_dev) \
794 container_of(_dev, struct thermal_cooling_device, device)
795
796static ssize_t
797thermal_cooling_device_type_show(struct device *dev,
798 struct device_attribute *attr, char *buf)
799{
800 struct thermal_cooling_device *cdev = to_cooling_device(dev);
801
802 return sprintf(buf, "%s\n", cdev->type);
803}
804
805static ssize_t
806thermal_cooling_device_max_state_show(struct device *dev,
807 struct device_attribute *attr, char *buf)
808{
809 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000810 unsigned long state;
811 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800812
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000813 ret = cdev->ops->get_max_state(cdev, &state);
814 if (ret)
815 return ret;
816 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800817}
818
819static ssize_t
820thermal_cooling_device_cur_state_show(struct device *dev,
821 struct device_attribute *attr, char *buf)
822{
823 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000824 unsigned long state;
825 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800826
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000827 ret = cdev->ops->get_cur_state(cdev, &state);
828 if (ret)
829 return ret;
830 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800831}
832
833static ssize_t
834thermal_cooling_device_cur_state_store(struct device *dev,
835 struct device_attribute *attr,
836 const char *buf, size_t count)
837{
838 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000839 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800840 int result;
841
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000842 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800843 return -EINVAL;
844
Roel Kluinedb94912009-12-15 22:46:50 +0100845 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800846 return -EINVAL;
847
848 result = cdev->ops->set_cur_state(cdev, state);
849 if (result)
850 return result;
851 return count;
852}
853
854static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500855__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800856static DEVICE_ATTR(max_state, 0444,
857 thermal_cooling_device_max_state_show, NULL);
858static DEVICE_ATTR(cur_state, 0644,
859 thermal_cooling_device_cur_state_show,
860 thermal_cooling_device_cur_state_store);
861
862static ssize_t
863thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500864 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800865{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800866 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800867
868 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800869 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800870
871 if (instance->trip == THERMAL_TRIPS_NONE)
872 return sprintf(buf, "-1\n");
873 else
874 return sprintf(buf, "%d\n", instance->trip);
875}
876
877/* Device management */
878
879/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000880 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
881 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +0800882 * @trip: indicates which trip point the cooling devices is
883 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000884 * @cdev: pointer to struct thermal_cooling_device
885 * @upper: the Maximum cooling state for this trip point.
886 * THERMAL_NO_LIMIT means no upper limit,
887 * and the cooling device can be in max_state.
888 * @lower: the Minimum cooling state can be used for this trip point.
889 * THERMAL_NO_LIMIT means no lower limit,
890 * and the cooling device can be in cooling state 0.
Len Brown543a9562008-02-07 16:55:08 -0500891 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000892 * This interface function bind a thermal cooling device to the certain trip
893 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500894 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000895 *
896 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800897 */
898int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
899 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800900 struct thermal_cooling_device *cdev,
901 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +0800902{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800903 struct thermal_instance *dev;
904 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500905 struct thermal_zone_device *pos1;
906 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800907 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800908 int result;
909
Len Brown543a9562008-02-07 16:55:08 -0500910 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800911 return -EINVAL;
912
Thomas Sujithc7516702008-02-15 00:58:50 -0500913 list_for_each_entry(pos1, &thermal_tz_list, node) {
914 if (pos1 == tz)
915 break;
916 }
917 list_for_each_entry(pos2, &thermal_cdev_list, node) {
918 if (pos2 == cdev)
919 break;
920 }
921
922 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800923 return -EINVAL;
924
Zhang Rui9d998422012-06-26 16:35:57 +0800925 cdev->ops->get_max_state(cdev, &max_state);
926
927 /* lower default 0, upper default max_state */
928 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
929 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
930
931 if (lower > upper || upper > max_state)
932 return -EINVAL;
933
Zhang Rui203d3d42008-01-17 15:51:08 +0800934 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800935 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800936 if (!dev)
937 return -ENOMEM;
938 dev->tz = tz;
939 dev->cdev = cdev;
940 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800941 dev->upper = upper;
942 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800943 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +0800944
Zhang Rui203d3d42008-01-17 15:51:08 +0800945 result = get_idr(&tz->idr, &tz->lock, &dev->id);
946 if (result)
947 goto free_mem;
948
949 sprintf(dev->name, "cdev%d", dev->id);
950 result =
951 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
952 if (result)
953 goto release_idr;
954
955 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700956 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800957 dev->attr.attr.name = dev->attr_name;
958 dev->attr.attr.mode = 0444;
959 dev->attr.show = thermal_cooling_device_trip_point_show;
960 result = device_create_file(&tz->device, &dev->attr);
961 if (result)
962 goto remove_symbol_link;
963
964 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800965 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800966 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +0800967 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
968 result = -EEXIST;
969 break;
970 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800971 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800972 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800973 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
974 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800975 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800976 mutex_unlock(&tz->lock);
977
978 if (!result)
979 return 0;
980
981 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -0700982remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800983 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -0700984release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +0800985 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -0700986free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800987 kfree(dev);
988 return result;
989}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000990EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +0800991
992/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000993 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
994 * thermal zone.
995 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +0800996 * @trip: indicates which trip point the cooling devices is
997 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000998 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -0500999 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001000 * This interface function unbind a thermal cooling device from the certain
1001 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001002 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001003 *
1004 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001005 */
1006int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1007 int trip,
1008 struct thermal_cooling_device *cdev)
1009{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001010 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001011
1012 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001013 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001014 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001015 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001016 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001017 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001018 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001019 mutex_unlock(&tz->lock);
1020 goto unbind;
1021 }
1022 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001023 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001024 mutex_unlock(&tz->lock);
1025
1026 return -ENODEV;
1027
Joe Perchescaca8b82012-03-21 12:55:02 -07001028unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +08001029 device_remove_file(&tz->device, &pos->attr);
1030 sysfs_remove_link(&tz->device.kobj, pos->name);
1031 release_idr(&tz->idr, &tz->lock, pos->id);
1032 kfree(pos);
1033 return 0;
1034}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001035EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001036
1037static void thermal_release(struct device *dev)
1038{
1039 struct thermal_zone_device *tz;
1040 struct thermal_cooling_device *cdev;
1041
Joe Perchescaca8b82012-03-21 12:55:02 -07001042 if (!strncmp(dev_name(dev), "thermal_zone",
1043 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001044 tz = to_thermal_zone(dev);
1045 kfree(tz);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +05301046 } else if(!strncmp(dev_name(dev), "cooling_device",
1047 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +08001048 cdev = to_cooling_device(dev);
1049 kfree(cdev);
1050 }
1051}
1052
1053static struct class thermal_class = {
1054 .name = "thermal",
1055 .dev_release = thermal_release,
1056};
1057
1058/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001059 * __thermal_cooling_device_register() - register a new thermal cooling device
1060 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +08001061 * @type: the thermal cooling device type.
1062 * @devdata: device private data.
1063 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001064 *
1065 * This interface function adds a new thermal cooling device (fan/processor/...)
1066 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1067 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001068 * It also gives the opportunity to link the cooling device to a device tree
1069 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001070 *
1071 * Return: a pointer to the created struct thermal_cooling_device or an
1072 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001073 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001074static struct thermal_cooling_device *
1075__thermal_cooling_device_register(struct device_node *np,
1076 char *type, void *devdata,
1077 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001078{
1079 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001080 int result;
1081
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001082 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001083 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001084
1085 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001086 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001087 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001088
1089 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1090 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001091 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001092
1093 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1094 if (result) {
1095 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001096 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001097 }
1098
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001099 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +08001100 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001101 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001102 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +08001103 cdev->ops = ops;
Zhang Ruice119f82012-06-27 14:13:04 +08001104 cdev->updated = true;
Zhang Rui203d3d42008-01-17 15:51:08 +08001105 cdev->device.class = &thermal_class;
1106 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001107 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001108 result = device_register(&cdev->device);
1109 if (result) {
1110 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1111 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001112 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001113 }
1114
1115 /* sys I/F */
1116 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001117 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001118 if (result)
1119 goto unregister;
1120 }
1121
1122 result = device_create_file(&cdev->device, &dev_attr_max_state);
1123 if (result)
1124 goto unregister;
1125
1126 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1127 if (result)
1128 goto unregister;
1129
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301130 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001131 mutex_lock(&thermal_list_lock);
1132 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001133 mutex_unlock(&thermal_list_lock);
1134
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301135 /* Update binding information for 'this' new cdev */
1136 bind_cdev(cdev);
1137
1138 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001139
Joe Perchescaca8b82012-03-21 12:55:02 -07001140unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001141 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1142 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001143 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001144}
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001145
1146/**
1147 * thermal_cooling_device_register() - register a new thermal cooling device
1148 * @type: the thermal cooling device type.
1149 * @devdata: device private data.
1150 * @ops: standard thermal cooling devices callbacks.
1151 *
1152 * This interface function adds a new thermal cooling device (fan/processor/...)
1153 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1154 * to all the thermal zone devices registered at the same time.
1155 *
1156 * Return: a pointer to the created struct thermal_cooling_device or an
1157 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1158 */
1159struct thermal_cooling_device *
1160thermal_cooling_device_register(char *type, void *devdata,
1161 const struct thermal_cooling_device_ops *ops)
1162{
1163 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1164}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001165EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001166
1167/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001168 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1169 * @np: a pointer to a device tree node.
1170 * @type: the thermal cooling device type.
1171 * @devdata: device private data.
1172 * @ops: standard thermal cooling devices callbacks.
1173 *
1174 * This function will register a cooling device with device tree node reference.
1175 * This interface function adds a new thermal cooling device (fan/processor/...)
1176 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1177 * to all the thermal zone devices registered at the same time.
1178 *
1179 * Return: a pointer to the created struct thermal_cooling_device or an
1180 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1181 */
1182struct thermal_cooling_device *
1183thermal_of_cooling_device_register(struct device_node *np,
1184 char *type, void *devdata,
1185 const struct thermal_cooling_device_ops *ops)
1186{
1187 return __thermal_cooling_device_register(np, type, devdata, ops);
1188}
1189EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1190
1191/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001192 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001193 * @cdev: the thermal cooling device to remove.
1194 *
1195 * thermal_cooling_device_unregister() must be called when the device is no
1196 * longer needed.
1197 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301198void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001199{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301200 int i;
1201 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001202 struct thermal_zone_device *tz;
1203 struct thermal_cooling_device *pos = NULL;
1204
1205 if (!cdev)
1206 return;
1207
1208 mutex_lock(&thermal_list_lock);
1209 list_for_each_entry(pos, &thermal_cdev_list, node)
1210 if (pos == cdev)
1211 break;
1212 if (pos != cdev) {
1213 /* thermal cooling device not found */
1214 mutex_unlock(&thermal_list_lock);
1215 return;
1216 }
1217 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301218
1219 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001220 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301221 if (tz->ops->unbind) {
1222 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001223 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301224 }
1225
1226 if (!tz->tzp || !tz->tzp->tbp)
1227 continue;
1228
1229 tzp = tz->tzp;
1230 for (i = 0; i < tzp->num_tbps; i++) {
1231 if (tzp->tbp[i].cdev == cdev) {
1232 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1233 tzp->tbp[i].cdev = NULL;
1234 }
1235 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001236 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301237
Zhang Rui203d3d42008-01-17 15:51:08 +08001238 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301239
Zhang Rui203d3d42008-01-17 15:51:08 +08001240 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001241 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001242 device_remove_file(&cdev->device, &dev_attr_max_state);
1243 device_remove_file(&cdev->device, &dev_attr_cur_state);
1244
1245 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1246 device_unregister(&cdev->device);
1247 return;
1248}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001249EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001250
Durgadoss Rdc765482012-09-18 11:05:00 +05301251void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001252{
1253 struct thermal_instance *instance;
1254 unsigned long target = 0;
1255
1256 /* cooling device is updated*/
1257 if (cdev->updated)
1258 return;
1259
Zhang Ruif4a821c2012-07-24 16:56:21 +08001260 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001261 /* Make sure cdev enters the deepest cooling state */
1262 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1263 if (instance->target == THERMAL_NO_TARGET)
1264 continue;
1265 if (instance->target > target)
1266 target = instance->target;
1267 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001268 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001269 cdev->ops->set_cur_state(cdev, target);
1270 cdev->updated = true;
1271}
Durgadoss Rdc765482012-09-18 11:05:00 +05301272EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001273
Matthew Garrettb1569e92008-12-03 17:55:32 +00001274/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001275 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301276 * @tz: thermal zone device
1277 * @trip: indicates which trip point has been crossed
1278 *
1279 * This function handles the trip events from sensor drivers. It starts
1280 * throttling the cooling devices according to the policy configured.
1281 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1282 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1283 * The throttling policy is based on the configured platform data; if no
1284 * platform data is provided, this uses the step_wise throttling policy.
1285 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001286void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301287{
1288 handle_thermal_trip(tz, trip);
1289}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001290EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301291
1292/**
Eduardo Valentin269c1742013-04-23 21:48:19 +00001293 * create_trip_attrs() - create attributes for trip points
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001294 * @tz: the thermal zone device
1295 * @mask: Writeable trip point bitmap.
Eduardo Valentin269c1742013-04-23 21:48:19 +00001296 *
1297 * helper function to instantiate sysfs entries for every trip
1298 * point and its properties of a struct thermal_zone_device.
1299 *
1300 * Return: 0 on success, the proper error value otherwise.
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001301 */
1302static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1303{
1304 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001305 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001306
Durgadoss R27365a62012-07-25 10:10:59 +08001307 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001308 if (!tz->trip_type_attrs)
1309 return -ENOMEM;
1310
Durgadoss R27365a62012-07-25 10:10:59 +08001311 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001312 if (!tz->trip_temp_attrs) {
1313 kfree(tz->trip_type_attrs);
1314 return -ENOMEM;
1315 }
1316
Durgadoss R27365a62012-07-25 10:10:59 +08001317 if (tz->ops->get_trip_hyst) {
1318 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1319 if (!tz->trip_hyst_attrs) {
1320 kfree(tz->trip_type_attrs);
1321 kfree(tz->trip_temp_attrs);
1322 return -ENOMEM;
1323 }
1324 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001325
Durgadoss R27365a62012-07-25 10:10:59 +08001326
1327 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001328 /* create trip type attribute */
1329 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1330 "trip_point_%d_type", indx);
1331
1332 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1333 tz->trip_type_attrs[indx].attr.attr.name =
1334 tz->trip_type_attrs[indx].name;
1335 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1336 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1337
1338 device_create_file(&tz->device,
1339 &tz->trip_type_attrs[indx].attr);
1340
1341 /* create trip temp attribute */
1342 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1343 "trip_point_%d_temp", indx);
1344
1345 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1346 tz->trip_temp_attrs[indx].attr.attr.name =
1347 tz->trip_temp_attrs[indx].name;
1348 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1349 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1350 if (mask & (1 << indx)) {
1351 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1352 tz->trip_temp_attrs[indx].attr.store =
1353 trip_point_temp_store;
1354 }
1355
1356 device_create_file(&tz->device,
1357 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001358
1359 /* create Optional trip hyst attribute */
1360 if (!tz->ops->get_trip_hyst)
1361 continue;
1362 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1363 "trip_point_%d_hyst", indx);
1364
1365 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1366 tz->trip_hyst_attrs[indx].attr.attr.name =
1367 tz->trip_hyst_attrs[indx].name;
1368 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1369 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1370 if (tz->ops->set_trip_hyst) {
1371 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1372 tz->trip_hyst_attrs[indx].attr.store =
1373 trip_point_hyst_store;
1374 }
1375
1376 device_create_file(&tz->device,
1377 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001378 }
1379 return 0;
1380}
1381
1382static void remove_trip_attrs(struct thermal_zone_device *tz)
1383{
1384 int indx;
1385
1386 for (indx = 0; indx < tz->trips; indx++) {
1387 device_remove_file(&tz->device,
1388 &tz->trip_type_attrs[indx].attr);
1389 device_remove_file(&tz->device,
1390 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001391 if (tz->ops->get_trip_hyst)
1392 device_remove_file(&tz->device,
1393 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001394 }
1395 kfree(tz->trip_type_attrs);
1396 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001397 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001398}
1399
1400/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001401 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001402 * @type: the thermal zone device type
1403 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001404 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001405 * @devdata: private device data
1406 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301407 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001408 * @passive_delay: number of milliseconds to wait between polls when
1409 * performing passive cooling
1410 * @polling_delay: number of milliseconds to wait between polls when checking
1411 * whether trip points have been crossed (0 for interrupt
1412 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001413 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001414 * This interface function adds a new thermal zone device (sensor) to
1415 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1416 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001417 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001418 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001419 *
1420 * Return: a pointer to the created struct thermal_zone_device or an
1421 * in case of error, an ERR_PTR. Caller must check return value with
1422 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001423 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001424struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001425 int trips, int mask, void *devdata,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001426 struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301427 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001428 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001429{
1430 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001431 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001432 int result;
1433 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001434 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001435
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001436 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001437 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001438
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001439 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001440 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001441
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001442 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001443 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001444
Jonghwa Lee83720d02013-05-18 09:50:26 +00001445 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001446 return ERR_PTR(-EINVAL);
1447
Zhang Rui203d3d42008-01-17 15:51:08 +08001448 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1449 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001450 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001451
Zhang Rui2d374132012-06-27 10:09:00 +08001452 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001453 idr_init(&tz->idr);
1454 mutex_init(&tz->lock);
1455 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1456 if (result) {
1457 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001458 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001459 }
1460
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001461 strlcpy(tz->type, type ? : "", sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001462 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301463 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001464 tz->device.class = &thermal_class;
1465 tz->devdata = devdata;
1466 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001467 tz->passive_delay = passive_delay;
1468 tz->polling_delay = polling_delay;
1469
Kay Sievers354655e2009-01-06 10:44:37 -08001470 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001471 result = device_register(&tz->device);
1472 if (result) {
1473 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1474 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001475 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001476 }
1477
1478 /* sys I/F */
1479 if (type) {
1480 result = device_create_file(&tz->device, &dev_attr_type);
1481 if (result)
1482 goto unregister;
1483 }
1484
1485 result = device_create_file(&tz->device, &dev_attr_temp);
1486 if (result)
1487 goto unregister;
1488
1489 if (ops->get_mode) {
1490 result = device_create_file(&tz->device, &dev_attr_mode);
1491 if (result)
1492 goto unregister;
1493 }
1494
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001495 result = create_trip_attrs(tz, mask);
1496 if (result)
1497 goto unregister;
1498
Zhang Rui203d3d42008-01-17 15:51:08 +08001499 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001500 tz->ops->get_trip_type(tz, count, &trip_type);
1501 if (trip_type == THERMAL_TRIP_PASSIVE)
1502 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001503 }
1504
Durgadoss R5a2c0902012-09-18 11:04:58 +05301505 if (!passive) {
1506 result = device_create_file(&tz->device, &dev_attr_passive);
1507 if (result)
1508 goto unregister;
1509 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001510
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001511#ifdef CONFIG_THERMAL_EMULATION
1512 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1513 if (result)
1514 goto unregister;
1515#endif
Durgadoss R5a2c0902012-09-18 11:04:58 +05301516 /* Create policy attribute */
1517 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001518 if (result)
1519 goto unregister;
1520
Durgadoss Ra4a15482012-09-18 11:04:57 +05301521 /* Update 'this' zone's governor information */
1522 mutex_lock(&thermal_governor_lock);
1523
1524 if (tz->tzp)
1525 tz->governor = __find_governor(tz->tzp->governor_name);
1526 else
1527 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1528
1529 mutex_unlock(&thermal_governor_lock);
1530
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04001531 if (!tz->tzp || !tz->tzp->no_hwmon) {
1532 result = thermal_add_hwmon_sysfs(tz);
1533 if (result)
1534 goto unregister;
1535 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08001536
Zhang Rui203d3d42008-01-17 15:51:08 +08001537 mutex_lock(&thermal_list_lock);
1538 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001539 mutex_unlock(&thermal_list_lock);
1540
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301541 /* Bind cooling devices for this zone */
1542 bind_tz(tz);
1543
Matthew Garrettb1569e92008-12-03 17:55:32 +00001544 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1545
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001546 if (!tz->ops->get_temp)
1547 thermal_zone_device_set_polling(tz, 0);
1548
Matthew Garrettb1569e92008-12-03 17:55:32 +00001549 thermal_zone_device_update(tz);
1550
Zhang Rui203d3d42008-01-17 15:51:08 +08001551 if (!result)
1552 return tz;
1553
Joe Perchescaca8b82012-03-21 12:55:02 -07001554unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001555 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1556 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001557 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001558}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001559EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001560
1561/**
1562 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001563 * @tz: the thermal zone device to remove
1564 */
1565void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1566{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301567 int i;
1568 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001569 struct thermal_cooling_device *cdev;
1570 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001571
1572 if (!tz)
1573 return;
1574
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301575 tzp = tz->tzp;
1576
Zhang Rui203d3d42008-01-17 15:51:08 +08001577 mutex_lock(&thermal_list_lock);
1578 list_for_each_entry(pos, &thermal_tz_list, node)
1579 if (pos == tz)
1580 break;
1581 if (pos != tz) {
1582 /* thermal zone device not found */
1583 mutex_unlock(&thermal_list_lock);
1584 return;
1585 }
1586 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301587
1588 /* Unbind all cdevs associated with 'this' thermal zone */
1589 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1590 if (tz->ops->unbind) {
1591 tz->ops->unbind(tz, cdev);
1592 continue;
1593 }
1594
1595 if (!tzp || !tzp->tbp)
1596 break;
1597
1598 for (i = 0; i < tzp->num_tbps; i++) {
1599 if (tzp->tbp[i].cdev == cdev) {
1600 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1601 tzp->tbp[i].cdev = NULL;
1602 }
1603 }
1604 }
1605
Zhang Rui203d3d42008-01-17 15:51:08 +08001606 mutex_unlock(&thermal_list_lock);
1607
Matthew Garrettb1569e92008-12-03 17:55:32 +00001608 thermal_zone_device_set_polling(tz, 0);
1609
Zhang Rui203d3d42008-01-17 15:51:08 +08001610 if (tz->type[0])
1611 device_remove_file(&tz->device, &dev_attr_type);
1612 device_remove_file(&tz->device, &dev_attr_temp);
1613 if (tz->ops->get_mode)
1614 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301615 device_remove_file(&tz->device, &dev_attr_policy);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001616 remove_trip_attrs(tz);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301617 tz->governor = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001618
Zhang Ruie68b16a2008-04-21 16:07:52 +08001619 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001620 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1621 idr_destroy(&tz->idr);
1622 mutex_destroy(&tz->lock);
1623 device_unregister(&tz->device);
1624 return;
1625}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001626EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001627
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001628/**
1629 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1630 * @name: thermal zone name to fetch the temperature
1631 *
1632 * When only one zone is found with the passed name, returns a reference to it.
1633 *
1634 * Return: On success returns a reference to an unique thermal zone with
1635 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1636 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1637 */
1638struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1639{
1640 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1641 unsigned int found = 0;
1642
1643 if (!name)
1644 goto exit;
1645
1646 mutex_lock(&thermal_list_lock);
1647 list_for_each_entry(pos, &thermal_tz_list, node)
1648 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1649 found++;
1650 ref = pos;
1651 }
1652 mutex_unlock(&thermal_list_lock);
1653
1654 /* nothing has been found, thus an error code for it */
1655 if (found == 0)
1656 ref = ERR_PTR(-ENODEV);
1657 else if (found > 1)
1658 /* Success only when an unique zone is found */
1659 ref = ERR_PTR(-EEXIST);
1660
1661exit:
1662 return ref;
1663}
1664EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1665
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001666#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01001667static const struct genl_multicast_group thermal_event_mcgrps[] = {
1668 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1669};
1670
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001671static struct genl_family thermal_event_genl_family = {
1672 .id = GENL_ID_GENERATE,
1673 .name = THERMAL_GENL_FAMILY_NAME,
1674 .version = THERMAL_GENL_VERSION,
1675 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001676 .mcgrps = thermal_event_mcgrps,
1677 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001678};
1679
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001680int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1681 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301682{
1683 struct sk_buff *skb;
1684 struct nlattr *attr;
1685 struct thermal_genl_event *thermal_event;
1686 void *msg_header;
1687 int size;
1688 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001689 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301690
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001691 if (!tz)
1692 return -EINVAL;
1693
R.Durgadoss4cb18722010-10-27 03:33:29 +05301694 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001695 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1696 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301697
1698 skb = genlmsg_new(size, GFP_ATOMIC);
1699 if (!skb)
1700 return -ENOMEM;
1701
1702 /* add the genetlink message header */
1703 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1704 &thermal_event_genl_family, 0,
1705 THERMAL_GENL_CMD_EVENT);
1706 if (!msg_header) {
1707 nlmsg_free(skb);
1708 return -ENOMEM;
1709 }
1710
1711 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001712 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1713 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301714
1715 if (!attr) {
1716 nlmsg_free(skb);
1717 return -EINVAL;
1718 }
1719
1720 thermal_event = nla_data(attr);
1721 if (!thermal_event) {
1722 nlmsg_free(skb);
1723 return -EINVAL;
1724 }
1725
1726 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1727
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001728 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301729 thermal_event->event = event;
1730
1731 /* send multicast genetlink message */
1732 result = genlmsg_end(skb, msg_header);
1733 if (result < 0) {
1734 nlmsg_free(skb);
1735 return result;
1736 }
1737
Johannes Berg68eb5502013-11-19 15:19:38 +01001738 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001739 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301740 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001741 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301742
1743 return result;
1744}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001745EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301746
1747static int genetlink_init(void)
1748{
Johannes Berg2a94fe42013-11-19 15:19:39 +01001749 return genl_register_family(&thermal_event_genl_family);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301750}
1751
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001752static void genetlink_exit(void)
1753{
1754 genl_unregister_family(&thermal_event_genl_family);
1755}
1756#else /* !CONFIG_NET */
1757static inline int genetlink_init(void) { return 0; }
1758static inline void genetlink_exit(void) {}
1759#endif /* !CONFIG_NET */
1760
Zhang Rui80a26a52013-03-26 16:38:29 +08001761static int __init thermal_register_governors(void)
1762{
1763 int result;
1764
1765 result = thermal_gov_step_wise_register();
1766 if (result)
1767 return result;
1768
1769 result = thermal_gov_fair_share_register();
1770 if (result)
1771 return result;
1772
1773 return thermal_gov_user_space_register();
1774}
1775
1776static void thermal_unregister_governors(void)
1777{
1778 thermal_gov_step_wise_unregister();
1779 thermal_gov_fair_share_unregister();
1780 thermal_gov_user_space_unregister();
1781}
1782
Zhang Rui203d3d42008-01-17 15:51:08 +08001783static int __init thermal_init(void)
1784{
Zhang Rui80a26a52013-03-26 16:38:29 +08001785 int result;
1786
1787 result = thermal_register_governors();
1788 if (result)
1789 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08001790
1791 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001792 if (result)
1793 goto unregister_governors;
1794
R.Durgadoss4cb18722010-10-27 03:33:29 +05301795 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08001796 if (result)
1797 goto unregister_class;
1798
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001799 result = of_parse_thermal_zones();
1800 if (result)
1801 goto exit_netlink;
1802
Zhang Rui80a26a52013-03-26 16:38:29 +08001803 return 0;
1804
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001805exit_netlink:
1806 genetlink_exit();
Zhang Rui80a26a52013-03-26 16:38:29 +08001807unregister_governors:
1808 thermal_unregister_governors();
1809unregister_class:
1810 class_unregister(&thermal_class);
1811error:
1812 idr_destroy(&thermal_tz_idr);
1813 idr_destroy(&thermal_cdev_idr);
1814 mutex_destroy(&thermal_idr_lock);
1815 mutex_destroy(&thermal_list_lock);
1816 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001817 return result;
1818}
1819
1820static void __exit thermal_exit(void)
1821{
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001822 of_thermal_destroy_zones();
Zhang Rui80a26a52013-03-26 16:38:29 +08001823 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001824 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001825 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08001826 idr_destroy(&thermal_tz_idr);
1827 idr_destroy(&thermal_cdev_idr);
1828 mutex_destroy(&thermal_idr_lock);
1829 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08001830 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001831}
1832
R.Durgadoss4cb18722010-10-27 03:33:29 +05301833fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001834module_exit(thermal_exit);