blob: 338a88bf6662d1fd0b963f0f8bc7c02dce55e73b [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);
Aaron Lu06475b52013-12-02 13:54:26 +0800454
455 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
456 tz->last_temperature, tz->temperature);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530457}
458
459void thermal_zone_device_update(struct thermal_zone_device *tz)
460{
461 int count;
462
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400463 if (!tz->ops->get_temp)
464 return;
465
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530466 update_temperature(tz);
467
468 for (count = 0; count < tz->trips; count++)
469 handle_thermal_trip(tz, count);
470}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000471EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530472
473static void thermal_zone_device_check(struct work_struct *work)
474{
475 struct thermal_zone_device *tz = container_of(work, struct
476 thermal_zone_device,
477 poll_queue.work);
478 thermal_zone_device_update(tz);
479}
480
Zhang Rui203d3d42008-01-17 15:51:08 +0800481/* sys I/F for thermal zone */
482
483#define to_thermal_zone(_dev) \
484 container_of(_dev, struct thermal_zone_device, device)
485
486static ssize_t
487type_show(struct device *dev, struct device_attribute *attr, char *buf)
488{
489 struct thermal_zone_device *tz = to_thermal_zone(dev);
490
491 return sprintf(buf, "%s\n", tz->type);
492}
493
494static ssize_t
495temp_show(struct device *dev, struct device_attribute *attr, char *buf)
496{
497 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000498 long temperature;
499 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800500
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000501 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000502
503 if (ret)
504 return ret;
505
506 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800507}
508
509static ssize_t
510mode_show(struct device *dev, struct device_attribute *attr, char *buf)
511{
512 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000513 enum thermal_device_mode mode;
514 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800515
516 if (!tz->ops->get_mode)
517 return -EPERM;
518
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000519 result = tz->ops->get_mode(tz, &mode);
520 if (result)
521 return result;
522
523 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
524 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800525}
526
527static ssize_t
528mode_store(struct device *dev, struct device_attribute *attr,
529 const char *buf, size_t count)
530{
531 struct thermal_zone_device *tz = to_thermal_zone(dev);
532 int result;
533
534 if (!tz->ops->set_mode)
535 return -EPERM;
536
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530537 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000538 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530539 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000540 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
541 else
542 result = -EINVAL;
543
Zhang Rui203d3d42008-01-17 15:51:08 +0800544 if (result)
545 return result;
546
547 return count;
548}
549
550static ssize_t
551trip_point_type_show(struct device *dev, struct device_attribute *attr,
552 char *buf)
553{
554 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000555 enum thermal_trip_type type;
556 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800557
558 if (!tz->ops->get_trip_type)
559 return -EPERM;
560
561 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
562 return -EINVAL;
563
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000564 result = tz->ops->get_trip_type(tz, trip, &type);
565 if (result)
566 return result;
567
568 switch (type) {
569 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300570 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000571 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300572 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000573 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300574 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000575 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300576 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000577 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300578 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000579 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800580}
581
582static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800583trip_point_temp_store(struct device *dev, struct device_attribute *attr,
584 const char *buf, size_t count)
585{
586 struct thermal_zone_device *tz = to_thermal_zone(dev);
587 int trip, ret;
588 unsigned long temperature;
589
590 if (!tz->ops->set_trip_temp)
591 return -EPERM;
592
593 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
594 return -EINVAL;
595
596 if (kstrtoul(buf, 10, &temperature))
597 return -EINVAL;
598
599 ret = tz->ops->set_trip_temp(tz, trip, temperature);
600
601 return ret ? ret : count;
602}
603
604static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800605trip_point_temp_show(struct device *dev, struct device_attribute *attr,
606 char *buf)
607{
608 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000609 int trip, ret;
610 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800611
612 if (!tz->ops->get_trip_temp)
613 return -EPERM;
614
615 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
616 return -EINVAL;
617
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000618 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
619
620 if (ret)
621 return ret;
622
623 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800624}
625
Matthew Garrett03a971a2008-12-03 18:00:38 +0000626static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800627trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
628 const char *buf, size_t count)
629{
630 struct thermal_zone_device *tz = to_thermal_zone(dev);
631 int trip, ret;
632 unsigned long temperature;
633
634 if (!tz->ops->set_trip_hyst)
635 return -EPERM;
636
637 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
638 return -EINVAL;
639
640 if (kstrtoul(buf, 10, &temperature))
641 return -EINVAL;
642
643 /*
644 * We are not doing any check on the 'temperature' value
645 * here. The driver implementing 'set_trip_hyst' has to
646 * take care of this.
647 */
648 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
649
650 return ret ? ret : count;
651}
652
653static ssize_t
654trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
655 char *buf)
656{
657 struct thermal_zone_device *tz = to_thermal_zone(dev);
658 int trip, ret;
659 unsigned long temperature;
660
661 if (!tz->ops->get_trip_hyst)
662 return -EPERM;
663
664 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
665 return -EINVAL;
666
667 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
668
669 return ret ? ret : sprintf(buf, "%ld\n", temperature);
670}
671
672static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000673passive_store(struct device *dev, struct device_attribute *attr,
674 const char *buf, size_t count)
675{
676 struct thermal_zone_device *tz = to_thermal_zone(dev);
677 struct thermal_cooling_device *cdev = NULL;
678 int state;
679
680 if (!sscanf(buf, "%d\n", &state))
681 return -EINVAL;
682
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100683 /* sanity check: values below 1000 millicelcius don't make sense
684 * and can cause the system to go into a thermal heart attack
685 */
686 if (state && state < 1000)
687 return -EINVAL;
688
Matthew Garrett03a971a2008-12-03 18:00:38 +0000689 if (state && !tz->forced_passive) {
690 mutex_lock(&thermal_list_lock);
691 list_for_each_entry(cdev, &thermal_cdev_list, node) {
692 if (!strncmp("Processor", cdev->type,
693 sizeof("Processor")))
694 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800695 THERMAL_TRIPS_NONE, cdev,
696 THERMAL_NO_LIMIT,
697 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000698 }
699 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100700 if (!tz->passive_delay)
701 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000702 } else if (!state && tz->forced_passive) {
703 mutex_lock(&thermal_list_lock);
704 list_for_each_entry(cdev, &thermal_cdev_list, node) {
705 if (!strncmp("Processor", cdev->type,
706 sizeof("Processor")))
707 thermal_zone_unbind_cooling_device(tz,
708 THERMAL_TRIPS_NONE,
709 cdev);
710 }
711 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100712 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000713 }
714
Matthew Garrett03a971a2008-12-03 18:00:38 +0000715 tz->forced_passive = state;
716
717 thermal_zone_device_update(tz);
718
719 return count;
720}
721
722static ssize_t
723passive_show(struct device *dev, struct device_attribute *attr,
724 char *buf)
725{
726 struct thermal_zone_device *tz = to_thermal_zone(dev);
727
728 return sprintf(buf, "%d\n", tz->forced_passive);
729}
730
Durgadoss R5a2c0902012-09-18 11:04:58 +0530731static ssize_t
732policy_store(struct device *dev, struct device_attribute *attr,
733 const char *buf, size_t count)
734{
735 int ret = -EINVAL;
736 struct thermal_zone_device *tz = to_thermal_zone(dev);
737 struct thermal_governor *gov;
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000738 char name[THERMAL_NAME_LENGTH];
739
740 snprintf(name, sizeof(name), "%s", buf);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530741
742 mutex_lock(&thermal_governor_lock);
743
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000744 gov = __find_governor(strim(name));
Durgadoss R5a2c0902012-09-18 11:04:58 +0530745 if (!gov)
746 goto exit;
747
748 tz->governor = gov;
749 ret = count;
750
751exit:
752 mutex_unlock(&thermal_governor_lock);
753 return ret;
754}
755
756static ssize_t
757policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
758{
759 struct thermal_zone_device *tz = to_thermal_zone(dev);
760
761 return sprintf(buf, "%s\n", tz->governor->name);
762}
763
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000764#ifdef CONFIG_THERMAL_EMULATION
765static ssize_t
766emul_temp_store(struct device *dev, struct device_attribute *attr,
767 const char *buf, size_t count)
768{
769 struct thermal_zone_device *tz = to_thermal_zone(dev);
770 int ret = 0;
771 unsigned long temperature;
772
773 if (kstrtoul(buf, 10, &temperature))
774 return -EINVAL;
775
776 if (!tz->ops->set_emul_temp) {
777 mutex_lock(&tz->lock);
778 tz->emul_temperature = temperature;
779 mutex_unlock(&tz->lock);
780 } else {
781 ret = tz->ops->set_emul_temp(tz, temperature);
782 }
783
lan,Tianyu800744b2014-01-02 15:47:54 +0800784 if (!ret)
785 thermal_zone_device_update(tz);
786
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000787 return ret ? ret : count;
788}
789static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
790#endif/*CONFIG_THERMAL_EMULATION*/
791
Zhang Rui203d3d42008-01-17 15:51:08 +0800792static DEVICE_ATTR(type, 0444, type_show, NULL);
793static DEVICE_ATTR(temp, 0444, temp_show, NULL);
794static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700795static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530796static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800797
Zhang Rui203d3d42008-01-17 15:51:08 +0800798/* sys I/F for cooling device */
799#define to_cooling_device(_dev) \
800 container_of(_dev, struct thermal_cooling_device, device)
801
802static ssize_t
803thermal_cooling_device_type_show(struct device *dev,
804 struct device_attribute *attr, char *buf)
805{
806 struct thermal_cooling_device *cdev = to_cooling_device(dev);
807
808 return sprintf(buf, "%s\n", cdev->type);
809}
810
811static ssize_t
812thermal_cooling_device_max_state_show(struct device *dev,
813 struct device_attribute *attr, char *buf)
814{
815 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000816 unsigned long state;
817 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800818
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000819 ret = cdev->ops->get_max_state(cdev, &state);
820 if (ret)
821 return ret;
822 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800823}
824
825static ssize_t
826thermal_cooling_device_cur_state_show(struct device *dev,
827 struct device_attribute *attr, char *buf)
828{
829 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000830 unsigned long state;
831 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800832
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000833 ret = cdev->ops->get_cur_state(cdev, &state);
834 if (ret)
835 return ret;
836 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800837}
838
839static ssize_t
840thermal_cooling_device_cur_state_store(struct device *dev,
841 struct device_attribute *attr,
842 const char *buf, size_t count)
843{
844 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000845 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800846 int result;
847
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000848 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800849 return -EINVAL;
850
Roel Kluinedb94912009-12-15 22:46:50 +0100851 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800852 return -EINVAL;
853
854 result = cdev->ops->set_cur_state(cdev, state);
855 if (result)
856 return result;
857 return count;
858}
859
860static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500861__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800862static DEVICE_ATTR(max_state, 0444,
863 thermal_cooling_device_max_state_show, NULL);
864static DEVICE_ATTR(cur_state, 0644,
865 thermal_cooling_device_cur_state_show,
866 thermal_cooling_device_cur_state_store);
867
868static ssize_t
869thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500870 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800871{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800872 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800873
874 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800875 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800876
877 if (instance->trip == THERMAL_TRIPS_NONE)
878 return sprintf(buf, "-1\n");
879 else
880 return sprintf(buf, "%d\n", instance->trip);
881}
882
883/* Device management */
884
885/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000886 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
887 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +0800888 * @trip: indicates which trip point the cooling devices is
889 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000890 * @cdev: pointer to struct thermal_cooling_device
891 * @upper: the Maximum cooling state for this trip point.
892 * THERMAL_NO_LIMIT means no upper limit,
893 * and the cooling device can be in max_state.
894 * @lower: the Minimum cooling state can be used for this trip point.
895 * THERMAL_NO_LIMIT means no lower limit,
896 * and the cooling device can be in cooling state 0.
Len Brown543a9562008-02-07 16:55:08 -0500897 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000898 * This interface function bind a thermal cooling device to the certain trip
899 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500900 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000901 *
902 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800903 */
904int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
905 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800906 struct thermal_cooling_device *cdev,
907 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +0800908{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800909 struct thermal_instance *dev;
910 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500911 struct thermal_zone_device *pos1;
912 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800913 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800914 int result;
915
Len Brown543a9562008-02-07 16:55:08 -0500916 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800917 return -EINVAL;
918
Thomas Sujithc7516702008-02-15 00:58:50 -0500919 list_for_each_entry(pos1, &thermal_tz_list, node) {
920 if (pos1 == tz)
921 break;
922 }
923 list_for_each_entry(pos2, &thermal_cdev_list, node) {
924 if (pos2 == cdev)
925 break;
926 }
927
928 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800929 return -EINVAL;
930
Zhang Rui9d998422012-06-26 16:35:57 +0800931 cdev->ops->get_max_state(cdev, &max_state);
932
933 /* lower default 0, upper default max_state */
934 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
935 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
936
937 if (lower > upper || upper > max_state)
938 return -EINVAL;
939
Zhang Rui203d3d42008-01-17 15:51:08 +0800940 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800941 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800942 if (!dev)
943 return -ENOMEM;
944 dev->tz = tz;
945 dev->cdev = cdev;
946 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800947 dev->upper = upper;
948 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800949 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +0800950
Zhang Rui203d3d42008-01-17 15:51:08 +0800951 result = get_idr(&tz->idr, &tz->lock, &dev->id);
952 if (result)
953 goto free_mem;
954
955 sprintf(dev->name, "cdev%d", dev->id);
956 result =
957 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
958 if (result)
959 goto release_idr;
960
961 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700962 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800963 dev->attr.attr.name = dev->attr_name;
964 dev->attr.attr.mode = 0444;
965 dev->attr.show = thermal_cooling_device_trip_point_show;
966 result = device_create_file(&tz->device, &dev->attr);
967 if (result)
968 goto remove_symbol_link;
969
970 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800971 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800972 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +0800973 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
974 result = -EEXIST;
975 break;
976 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800977 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800978 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800979 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
980 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800981 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800982 mutex_unlock(&tz->lock);
983
984 if (!result)
985 return 0;
986
987 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -0700988remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800989 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -0700990release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +0800991 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -0700992free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800993 kfree(dev);
994 return result;
995}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000996EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +0800997
998/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000999 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1000 * thermal zone.
1001 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +08001002 * @trip: indicates which trip point the cooling devices is
1003 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001004 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -05001005 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001006 * This interface function unbind a thermal cooling device from the certain
1007 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001008 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001009 *
1010 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001011 */
1012int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1013 int trip,
1014 struct thermal_cooling_device *cdev)
1015{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001016 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001017
1018 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001019 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001020 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001021 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001022 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001023 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001024 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001025 mutex_unlock(&tz->lock);
1026 goto unbind;
1027 }
1028 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001029 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001030 mutex_unlock(&tz->lock);
1031
1032 return -ENODEV;
1033
Joe Perchescaca8b82012-03-21 12:55:02 -07001034unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +08001035 device_remove_file(&tz->device, &pos->attr);
1036 sysfs_remove_link(&tz->device.kobj, pos->name);
1037 release_idr(&tz->idr, &tz->lock, pos->id);
1038 kfree(pos);
1039 return 0;
1040}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001041EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001042
1043static void thermal_release(struct device *dev)
1044{
1045 struct thermal_zone_device *tz;
1046 struct thermal_cooling_device *cdev;
1047
Joe Perchescaca8b82012-03-21 12:55:02 -07001048 if (!strncmp(dev_name(dev), "thermal_zone",
1049 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001050 tz = to_thermal_zone(dev);
1051 kfree(tz);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +05301052 } else if(!strncmp(dev_name(dev), "cooling_device",
1053 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +08001054 cdev = to_cooling_device(dev);
1055 kfree(cdev);
1056 }
1057}
1058
1059static struct class thermal_class = {
1060 .name = "thermal",
1061 .dev_release = thermal_release,
1062};
1063
1064/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001065 * __thermal_cooling_device_register() - register a new thermal cooling device
1066 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +08001067 * @type: the thermal cooling device type.
1068 * @devdata: device private data.
1069 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001070 *
1071 * This interface function adds a new thermal cooling device (fan/processor/...)
1072 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1073 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001074 * It also gives the opportunity to link the cooling device to a device tree
1075 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001076 *
1077 * Return: a pointer to the created struct thermal_cooling_device or an
1078 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001079 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001080static struct thermal_cooling_device *
1081__thermal_cooling_device_register(struct device_node *np,
1082 char *type, void *devdata,
1083 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001084{
1085 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001086 int result;
1087
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001088 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001089 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001090
1091 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001092 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001093 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001094
1095 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1096 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001097 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001098
1099 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1100 if (result) {
1101 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001102 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001103 }
1104
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001105 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +08001106 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001107 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001108 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +08001109 cdev->ops = ops;
Zhang Ruice119f82012-06-27 14:13:04 +08001110 cdev->updated = true;
Zhang Rui203d3d42008-01-17 15:51:08 +08001111 cdev->device.class = &thermal_class;
1112 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001113 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001114 result = device_register(&cdev->device);
1115 if (result) {
1116 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1117 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001118 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001119 }
1120
1121 /* sys I/F */
1122 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001123 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001124 if (result)
1125 goto unregister;
1126 }
1127
1128 result = device_create_file(&cdev->device, &dev_attr_max_state);
1129 if (result)
1130 goto unregister;
1131
1132 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1133 if (result)
1134 goto unregister;
1135
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301136 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001137 mutex_lock(&thermal_list_lock);
1138 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001139 mutex_unlock(&thermal_list_lock);
1140
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301141 /* Update binding information for 'this' new cdev */
1142 bind_cdev(cdev);
1143
1144 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001145
Joe Perchescaca8b82012-03-21 12:55:02 -07001146unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001147 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1148 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001149 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001150}
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001151
1152/**
1153 * thermal_cooling_device_register() - register a new thermal cooling device
1154 * @type: the thermal cooling device type.
1155 * @devdata: device private data.
1156 * @ops: standard thermal cooling devices callbacks.
1157 *
1158 * This interface function adds a new thermal cooling device (fan/processor/...)
1159 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1160 * to all the thermal zone devices registered at the same time.
1161 *
1162 * Return: a pointer to the created struct thermal_cooling_device or an
1163 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1164 */
1165struct thermal_cooling_device *
1166thermal_cooling_device_register(char *type, void *devdata,
1167 const struct thermal_cooling_device_ops *ops)
1168{
1169 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1170}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001171EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001172
1173/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001174 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1175 * @np: a pointer to a device tree node.
1176 * @type: the thermal cooling device type.
1177 * @devdata: device private data.
1178 * @ops: standard thermal cooling devices callbacks.
1179 *
1180 * This function will register a cooling device with device tree node reference.
1181 * This interface function adds a new thermal cooling device (fan/processor/...)
1182 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1183 * to all the thermal zone devices registered at the same time.
1184 *
1185 * Return: a pointer to the created struct thermal_cooling_device or an
1186 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1187 */
1188struct thermal_cooling_device *
1189thermal_of_cooling_device_register(struct device_node *np,
1190 char *type, void *devdata,
1191 const struct thermal_cooling_device_ops *ops)
1192{
1193 return __thermal_cooling_device_register(np, type, devdata, ops);
1194}
1195EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1196
1197/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001198 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001199 * @cdev: the thermal cooling device to remove.
1200 *
1201 * thermal_cooling_device_unregister() must be called when the device is no
1202 * longer needed.
1203 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301204void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001205{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301206 int i;
1207 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001208 struct thermal_zone_device *tz;
1209 struct thermal_cooling_device *pos = NULL;
1210
1211 if (!cdev)
1212 return;
1213
1214 mutex_lock(&thermal_list_lock);
1215 list_for_each_entry(pos, &thermal_cdev_list, node)
1216 if (pos == cdev)
1217 break;
1218 if (pos != cdev) {
1219 /* thermal cooling device not found */
1220 mutex_unlock(&thermal_list_lock);
1221 return;
1222 }
1223 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301224
1225 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001226 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301227 if (tz->ops->unbind) {
1228 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001229 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301230 }
1231
1232 if (!tz->tzp || !tz->tzp->tbp)
1233 continue;
1234
1235 tzp = tz->tzp;
1236 for (i = 0; i < tzp->num_tbps; i++) {
1237 if (tzp->tbp[i].cdev == cdev) {
1238 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1239 tzp->tbp[i].cdev = NULL;
1240 }
1241 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001242 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301243
Zhang Rui203d3d42008-01-17 15:51:08 +08001244 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301245
Zhang Rui203d3d42008-01-17 15:51:08 +08001246 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001247 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001248 device_remove_file(&cdev->device, &dev_attr_max_state);
1249 device_remove_file(&cdev->device, &dev_attr_cur_state);
1250
1251 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1252 device_unregister(&cdev->device);
1253 return;
1254}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001255EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001256
Durgadoss Rdc765482012-09-18 11:05:00 +05301257void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001258{
1259 struct thermal_instance *instance;
1260 unsigned long target = 0;
1261
1262 /* cooling device is updated*/
1263 if (cdev->updated)
1264 return;
1265
Zhang Ruif4a821c2012-07-24 16:56:21 +08001266 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001267 /* Make sure cdev enters the deepest cooling state */
1268 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
Aaron Lu06475b52013-12-02 13:54:26 +08001269 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1270 instance->tz->id, instance->target);
Zhang Ruice119f82012-06-27 14:13:04 +08001271 if (instance->target == THERMAL_NO_TARGET)
1272 continue;
1273 if (instance->target > target)
1274 target = instance->target;
1275 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001276 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001277 cdev->ops->set_cur_state(cdev, target);
1278 cdev->updated = true;
Aaron Lu06475b52013-12-02 13:54:26 +08001279 dev_dbg(&cdev->device, "set to state %lu\n", target);
Zhang Ruice119f82012-06-27 14:13:04 +08001280}
Durgadoss Rdc765482012-09-18 11:05:00 +05301281EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001282
Matthew Garrettb1569e92008-12-03 17:55:32 +00001283/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001284 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301285 * @tz: thermal zone device
1286 * @trip: indicates which trip point has been crossed
1287 *
1288 * This function handles the trip events from sensor drivers. It starts
1289 * throttling the cooling devices according to the policy configured.
1290 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1291 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1292 * The throttling policy is based on the configured platform data; if no
1293 * platform data is provided, this uses the step_wise throttling policy.
1294 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001295void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301296{
1297 handle_thermal_trip(tz, trip);
1298}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001299EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301300
1301/**
Eduardo Valentin269c1742013-04-23 21:48:19 +00001302 * create_trip_attrs() - create attributes for trip points
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001303 * @tz: the thermal zone device
1304 * @mask: Writeable trip point bitmap.
Eduardo Valentin269c1742013-04-23 21:48:19 +00001305 *
1306 * helper function to instantiate sysfs entries for every trip
1307 * point and its properties of a struct thermal_zone_device.
1308 *
1309 * Return: 0 on success, the proper error value otherwise.
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001310 */
1311static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1312{
1313 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001314 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001315
Durgadoss R27365a62012-07-25 10:10:59 +08001316 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001317 if (!tz->trip_type_attrs)
1318 return -ENOMEM;
1319
Durgadoss R27365a62012-07-25 10:10:59 +08001320 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001321 if (!tz->trip_temp_attrs) {
1322 kfree(tz->trip_type_attrs);
1323 return -ENOMEM;
1324 }
1325
Durgadoss R27365a62012-07-25 10:10:59 +08001326 if (tz->ops->get_trip_hyst) {
1327 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1328 if (!tz->trip_hyst_attrs) {
1329 kfree(tz->trip_type_attrs);
1330 kfree(tz->trip_temp_attrs);
1331 return -ENOMEM;
1332 }
1333 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001334
Durgadoss R27365a62012-07-25 10:10:59 +08001335
1336 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001337 /* create trip type attribute */
1338 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1339 "trip_point_%d_type", indx);
1340
1341 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1342 tz->trip_type_attrs[indx].attr.attr.name =
1343 tz->trip_type_attrs[indx].name;
1344 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1345 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1346
1347 device_create_file(&tz->device,
1348 &tz->trip_type_attrs[indx].attr);
1349
1350 /* create trip temp attribute */
1351 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1352 "trip_point_%d_temp", indx);
1353
1354 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1355 tz->trip_temp_attrs[indx].attr.attr.name =
1356 tz->trip_temp_attrs[indx].name;
1357 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1358 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1359 if (mask & (1 << indx)) {
1360 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1361 tz->trip_temp_attrs[indx].attr.store =
1362 trip_point_temp_store;
1363 }
1364
1365 device_create_file(&tz->device,
1366 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001367
1368 /* create Optional trip hyst attribute */
1369 if (!tz->ops->get_trip_hyst)
1370 continue;
1371 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1372 "trip_point_%d_hyst", indx);
1373
1374 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1375 tz->trip_hyst_attrs[indx].attr.attr.name =
1376 tz->trip_hyst_attrs[indx].name;
1377 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1378 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1379 if (tz->ops->set_trip_hyst) {
1380 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1381 tz->trip_hyst_attrs[indx].attr.store =
1382 trip_point_hyst_store;
1383 }
1384
1385 device_create_file(&tz->device,
1386 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001387 }
1388 return 0;
1389}
1390
1391static void remove_trip_attrs(struct thermal_zone_device *tz)
1392{
1393 int indx;
1394
1395 for (indx = 0; indx < tz->trips; indx++) {
1396 device_remove_file(&tz->device,
1397 &tz->trip_type_attrs[indx].attr);
1398 device_remove_file(&tz->device,
1399 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001400 if (tz->ops->get_trip_hyst)
1401 device_remove_file(&tz->device,
1402 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001403 }
1404 kfree(tz->trip_type_attrs);
1405 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001406 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001407}
1408
1409/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001410 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001411 * @type: the thermal zone device type
1412 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001413 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001414 * @devdata: private device data
1415 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301416 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001417 * @passive_delay: number of milliseconds to wait between polls when
1418 * performing passive cooling
1419 * @polling_delay: number of milliseconds to wait between polls when checking
1420 * whether trip points have been crossed (0 for interrupt
1421 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001422 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001423 * This interface function adds a new thermal zone device (sensor) to
1424 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1425 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001426 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001427 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001428 *
1429 * Return: a pointer to the created struct thermal_zone_device or an
1430 * in case of error, an ERR_PTR. Caller must check return value with
1431 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001432 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001433struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001434 int trips, int mask, void *devdata,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001435 struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301436 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001437 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001438{
1439 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001440 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001441 int result;
1442 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001443 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001444
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001445 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001446 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001447
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001448 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001449 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001450
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001451 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001452 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001453
Jonghwa Lee83720d02013-05-18 09:50:26 +00001454 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001455 return ERR_PTR(-EINVAL);
1456
Zhang Rui203d3d42008-01-17 15:51:08 +08001457 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1458 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001459 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001460
Zhang Rui2d374132012-06-27 10:09:00 +08001461 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001462 idr_init(&tz->idr);
1463 mutex_init(&tz->lock);
1464 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1465 if (result) {
1466 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001467 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001468 }
1469
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001470 strlcpy(tz->type, type ? : "", sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001471 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301472 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001473 tz->device.class = &thermal_class;
1474 tz->devdata = devdata;
1475 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001476 tz->passive_delay = passive_delay;
1477 tz->polling_delay = polling_delay;
1478
Kay Sievers354655e2009-01-06 10:44:37 -08001479 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001480 result = device_register(&tz->device);
1481 if (result) {
1482 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1483 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001484 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001485 }
1486
1487 /* sys I/F */
1488 if (type) {
1489 result = device_create_file(&tz->device, &dev_attr_type);
1490 if (result)
1491 goto unregister;
1492 }
1493
1494 result = device_create_file(&tz->device, &dev_attr_temp);
1495 if (result)
1496 goto unregister;
1497
1498 if (ops->get_mode) {
1499 result = device_create_file(&tz->device, &dev_attr_mode);
1500 if (result)
1501 goto unregister;
1502 }
1503
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001504 result = create_trip_attrs(tz, mask);
1505 if (result)
1506 goto unregister;
1507
Zhang Rui203d3d42008-01-17 15:51:08 +08001508 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001509 tz->ops->get_trip_type(tz, count, &trip_type);
1510 if (trip_type == THERMAL_TRIP_PASSIVE)
1511 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001512 }
1513
Durgadoss R5a2c0902012-09-18 11:04:58 +05301514 if (!passive) {
1515 result = device_create_file(&tz->device, &dev_attr_passive);
1516 if (result)
1517 goto unregister;
1518 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001519
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001520#ifdef CONFIG_THERMAL_EMULATION
1521 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1522 if (result)
1523 goto unregister;
1524#endif
Durgadoss R5a2c0902012-09-18 11:04:58 +05301525 /* Create policy attribute */
1526 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001527 if (result)
1528 goto unregister;
1529
Durgadoss Ra4a15482012-09-18 11:04:57 +05301530 /* Update 'this' zone's governor information */
1531 mutex_lock(&thermal_governor_lock);
1532
1533 if (tz->tzp)
1534 tz->governor = __find_governor(tz->tzp->governor_name);
1535 else
1536 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1537
1538 mutex_unlock(&thermal_governor_lock);
1539
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04001540 if (!tz->tzp || !tz->tzp->no_hwmon) {
1541 result = thermal_add_hwmon_sysfs(tz);
1542 if (result)
1543 goto unregister;
1544 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08001545
Zhang Rui203d3d42008-01-17 15:51:08 +08001546 mutex_lock(&thermal_list_lock);
1547 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001548 mutex_unlock(&thermal_list_lock);
1549
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301550 /* Bind cooling devices for this zone */
1551 bind_tz(tz);
1552
Matthew Garrettb1569e92008-12-03 17:55:32 +00001553 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1554
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001555 if (!tz->ops->get_temp)
1556 thermal_zone_device_set_polling(tz, 0);
1557
Matthew Garrettb1569e92008-12-03 17:55:32 +00001558 thermal_zone_device_update(tz);
1559
Zhang Rui203d3d42008-01-17 15:51:08 +08001560 if (!result)
1561 return tz;
1562
Joe Perchescaca8b82012-03-21 12:55:02 -07001563unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001564 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1565 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001566 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001567}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001568EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001569
1570/**
1571 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001572 * @tz: the thermal zone device to remove
1573 */
1574void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1575{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301576 int i;
1577 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001578 struct thermal_cooling_device *cdev;
1579 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001580
1581 if (!tz)
1582 return;
1583
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301584 tzp = tz->tzp;
1585
Zhang Rui203d3d42008-01-17 15:51:08 +08001586 mutex_lock(&thermal_list_lock);
1587 list_for_each_entry(pos, &thermal_tz_list, node)
1588 if (pos == tz)
1589 break;
1590 if (pos != tz) {
1591 /* thermal zone device not found */
1592 mutex_unlock(&thermal_list_lock);
1593 return;
1594 }
1595 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301596
1597 /* Unbind all cdevs associated with 'this' thermal zone */
1598 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1599 if (tz->ops->unbind) {
1600 tz->ops->unbind(tz, cdev);
1601 continue;
1602 }
1603
1604 if (!tzp || !tzp->tbp)
1605 break;
1606
1607 for (i = 0; i < tzp->num_tbps; i++) {
1608 if (tzp->tbp[i].cdev == cdev) {
1609 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1610 tzp->tbp[i].cdev = NULL;
1611 }
1612 }
1613 }
1614
Zhang Rui203d3d42008-01-17 15:51:08 +08001615 mutex_unlock(&thermal_list_lock);
1616
Matthew Garrettb1569e92008-12-03 17:55:32 +00001617 thermal_zone_device_set_polling(tz, 0);
1618
Zhang Rui203d3d42008-01-17 15:51:08 +08001619 if (tz->type[0])
1620 device_remove_file(&tz->device, &dev_attr_type);
1621 device_remove_file(&tz->device, &dev_attr_temp);
1622 if (tz->ops->get_mode)
1623 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301624 device_remove_file(&tz->device, &dev_attr_policy);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001625 remove_trip_attrs(tz);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301626 tz->governor = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001627
Zhang Ruie68b16a2008-04-21 16:07:52 +08001628 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001629 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1630 idr_destroy(&tz->idr);
1631 mutex_destroy(&tz->lock);
1632 device_unregister(&tz->device);
1633 return;
1634}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001635EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001636
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001637/**
1638 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1639 * @name: thermal zone name to fetch the temperature
1640 *
1641 * When only one zone is found with the passed name, returns a reference to it.
1642 *
1643 * Return: On success returns a reference to an unique thermal zone with
1644 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1645 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1646 */
1647struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1648{
1649 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1650 unsigned int found = 0;
1651
1652 if (!name)
1653 goto exit;
1654
1655 mutex_lock(&thermal_list_lock);
1656 list_for_each_entry(pos, &thermal_tz_list, node)
1657 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1658 found++;
1659 ref = pos;
1660 }
1661 mutex_unlock(&thermal_list_lock);
1662
1663 /* nothing has been found, thus an error code for it */
1664 if (found == 0)
1665 ref = ERR_PTR(-ENODEV);
1666 else if (found > 1)
1667 /* Success only when an unique zone is found */
1668 ref = ERR_PTR(-EEXIST);
1669
1670exit:
1671 return ref;
1672}
1673EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1674
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001675#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01001676static const struct genl_multicast_group thermal_event_mcgrps[] = {
1677 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1678};
1679
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001680static struct genl_family thermal_event_genl_family = {
1681 .id = GENL_ID_GENERATE,
1682 .name = THERMAL_GENL_FAMILY_NAME,
1683 .version = THERMAL_GENL_VERSION,
1684 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001685 .mcgrps = thermal_event_mcgrps,
1686 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001687};
1688
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001689int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1690 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301691{
1692 struct sk_buff *skb;
1693 struct nlattr *attr;
1694 struct thermal_genl_event *thermal_event;
1695 void *msg_header;
1696 int size;
1697 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001698 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301699
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001700 if (!tz)
1701 return -EINVAL;
1702
R.Durgadoss4cb18722010-10-27 03:33:29 +05301703 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001704 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1705 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301706
1707 skb = genlmsg_new(size, GFP_ATOMIC);
1708 if (!skb)
1709 return -ENOMEM;
1710
1711 /* add the genetlink message header */
1712 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1713 &thermal_event_genl_family, 0,
1714 THERMAL_GENL_CMD_EVENT);
1715 if (!msg_header) {
1716 nlmsg_free(skb);
1717 return -ENOMEM;
1718 }
1719
1720 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001721 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1722 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301723
1724 if (!attr) {
1725 nlmsg_free(skb);
1726 return -EINVAL;
1727 }
1728
1729 thermal_event = nla_data(attr);
1730 if (!thermal_event) {
1731 nlmsg_free(skb);
1732 return -EINVAL;
1733 }
1734
1735 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1736
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001737 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301738 thermal_event->event = event;
1739
1740 /* send multicast genetlink message */
1741 result = genlmsg_end(skb, msg_header);
1742 if (result < 0) {
1743 nlmsg_free(skb);
1744 return result;
1745 }
1746
Johannes Berg68eb5502013-11-19 15:19:38 +01001747 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001748 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301749 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001750 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301751
1752 return result;
1753}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001754EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301755
1756static int genetlink_init(void)
1757{
Johannes Berg2a94fe42013-11-19 15:19:39 +01001758 return genl_register_family(&thermal_event_genl_family);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301759}
1760
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001761static void genetlink_exit(void)
1762{
1763 genl_unregister_family(&thermal_event_genl_family);
1764}
1765#else /* !CONFIG_NET */
1766static inline int genetlink_init(void) { return 0; }
1767static inline void genetlink_exit(void) {}
1768#endif /* !CONFIG_NET */
1769
Zhang Rui80a26a52013-03-26 16:38:29 +08001770static int __init thermal_register_governors(void)
1771{
1772 int result;
1773
1774 result = thermal_gov_step_wise_register();
1775 if (result)
1776 return result;
1777
1778 result = thermal_gov_fair_share_register();
1779 if (result)
1780 return result;
1781
1782 return thermal_gov_user_space_register();
1783}
1784
1785static void thermal_unregister_governors(void)
1786{
1787 thermal_gov_step_wise_unregister();
1788 thermal_gov_fair_share_unregister();
1789 thermal_gov_user_space_unregister();
1790}
1791
Zhang Rui203d3d42008-01-17 15:51:08 +08001792static int __init thermal_init(void)
1793{
Zhang Rui80a26a52013-03-26 16:38:29 +08001794 int result;
1795
1796 result = thermal_register_governors();
1797 if (result)
1798 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08001799
1800 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001801 if (result)
1802 goto unregister_governors;
1803
R.Durgadoss4cb18722010-10-27 03:33:29 +05301804 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08001805 if (result)
1806 goto unregister_class;
1807
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001808 result = of_parse_thermal_zones();
1809 if (result)
1810 goto exit_netlink;
1811
Zhang Rui80a26a52013-03-26 16:38:29 +08001812 return 0;
1813
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001814exit_netlink:
1815 genetlink_exit();
Zhang Rui80a26a52013-03-26 16:38:29 +08001816unregister_governors:
1817 thermal_unregister_governors();
1818unregister_class:
1819 class_unregister(&thermal_class);
1820error:
1821 idr_destroy(&thermal_tz_idr);
1822 idr_destroy(&thermal_cdev_idr);
1823 mutex_destroy(&thermal_idr_lock);
1824 mutex_destroy(&thermal_list_lock);
1825 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001826 return result;
1827}
1828
1829static void __exit thermal_exit(void)
1830{
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001831 of_thermal_destroy_zones();
Zhang Rui80a26a52013-03-26 16:38:29 +08001832 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001833 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001834 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08001835 idr_destroy(&thermal_tz_idr);
1836 idr_destroy(&thermal_cdev_idr);
1837 mutex_destroy(&thermal_idr_lock);
1838 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08001839 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001840}
1841
R.Durgadoss4cb18722010-10-27 03:33:29 +05301842fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001843module_exit(thermal_exit);