blob: 48491d1a81d650f1d10812c1fb8d5e05ff99cbe1 [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
Punit Agrawal100a8fd2014-07-29 11:50:48 +010041#define CREATE_TRACE_POINTS
42#include <trace/events/thermal.h>
43
Durgadoss R71350db2012-09-18 11:04:53 +053044#include "thermal_core.h"
Eduardo Valentin0dd88792013-07-03 15:14:28 -040045#include "thermal_hwmon.h"
Durgadoss R71350db2012-09-18 11:04:53 +053046
Zhang Rui63c4ec92008-04-21 16:07:13 +080047MODULE_AUTHOR("Zhang Rui");
Zhang Rui203d3d42008-01-17 15:51:08 +080048MODULE_DESCRIPTION("Generic thermal management sysfs support");
Eduardo Valentin6d8d4972013-04-23 21:48:13 +000049MODULE_LICENSE("GPL v2");
Zhang Rui203d3d42008-01-17 15:51:08 +080050
Zhang Rui203d3d42008-01-17 15:51:08 +080051static DEFINE_IDR(thermal_tz_idr);
52static DEFINE_IDR(thermal_cdev_idr);
53static DEFINE_MUTEX(thermal_idr_lock);
54
55static LIST_HEAD(thermal_tz_list);
56static LIST_HEAD(thermal_cdev_list);
Durgadoss Ra4a15482012-09-18 11:04:57 +053057static LIST_HEAD(thermal_governor_list);
58
Zhang Rui203d3d42008-01-17 15:51:08 +080059static DEFINE_MUTEX(thermal_list_lock);
Durgadoss Ra4a15482012-09-18 11:04:57 +053060static DEFINE_MUTEX(thermal_governor_lock);
61
Zhang Ruif2234bc2014-01-24 10:23:19 +080062static struct thermal_governor *def_governor;
63
Durgadoss Ra4a15482012-09-18 11:04:57 +053064static struct thermal_governor *__find_governor(const char *name)
65{
66 struct thermal_governor *pos;
67
Zhang Ruif2234bc2014-01-24 10:23:19 +080068 if (!name || !name[0])
69 return def_governor;
70
Durgadoss Ra4a15482012-09-18 11:04:57 +053071 list_for_each_entry(pos, &thermal_governor_list, governor_list)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -070072 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
Durgadoss Ra4a15482012-09-18 11:04:57 +053073 return pos;
74
75 return NULL;
76}
77
78int thermal_register_governor(struct thermal_governor *governor)
79{
80 int err;
81 const char *name;
82 struct thermal_zone_device *pos;
83
84 if (!governor)
85 return -EINVAL;
86
87 mutex_lock(&thermal_governor_lock);
88
89 err = -EBUSY;
90 if (__find_governor(governor->name) == NULL) {
91 err = 0;
92 list_add(&governor->governor_list, &thermal_governor_list);
Zhang Ruif2234bc2014-01-24 10:23:19 +080093 if (!def_governor && !strncmp(governor->name,
94 DEFAULT_THERMAL_GOVERNOR, THERMAL_NAME_LENGTH))
95 def_governor = governor;
Durgadoss Ra4a15482012-09-18 11:04:57 +053096 }
97
98 mutex_lock(&thermal_list_lock);
99
100 list_for_each_entry(pos, &thermal_tz_list, node) {
Zhang Ruif2234bc2014-01-24 10:23:19 +0800101 /*
102 * only thermal zones with specified tz->tzp->governor_name
103 * may run with tz->govenor unset
104 */
Durgadoss Ra4a15482012-09-18 11:04:57 +0530105 if (pos->governor)
106 continue;
Zhang Ruif2234bc2014-01-24 10:23:19 +0800107
108 name = pos->tzp->governor_name;
109
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -0700110 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH))
Durgadoss Ra4a15482012-09-18 11:04:57 +0530111 pos->governor = governor;
112 }
113
114 mutex_unlock(&thermal_list_lock);
115 mutex_unlock(&thermal_governor_lock);
116
117 return err;
118}
Durgadoss Ra4a15482012-09-18 11:04:57 +0530119
120void thermal_unregister_governor(struct thermal_governor *governor)
121{
122 struct thermal_zone_device *pos;
123
124 if (!governor)
125 return;
126
127 mutex_lock(&thermal_governor_lock);
128
129 if (__find_governor(governor->name) == NULL)
130 goto exit;
131
132 mutex_lock(&thermal_list_lock);
133
134 list_for_each_entry(pos, &thermal_tz_list, node) {
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -0700135 if (!strncasecmp(pos->governor->name, governor->name,
Durgadoss Ra4a15482012-09-18 11:04:57 +0530136 THERMAL_NAME_LENGTH))
137 pos->governor = NULL;
138 }
139
140 mutex_unlock(&thermal_list_lock);
141 list_del(&governor->governor_list);
142exit:
143 mutex_unlock(&thermal_governor_lock);
144 return;
145}
Zhang Rui203d3d42008-01-17 15:51:08 +0800146
147static int get_idr(struct idr *idr, struct mutex *lock, int *id)
148{
Tejun Heo6deb69f2013-02-27 17:04:46 -0800149 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800150
151 if (lock)
152 mutex_lock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800153 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800154 if (lock)
155 mutex_unlock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800156 if (unlikely(ret < 0))
157 return ret;
158 *id = ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800159 return 0;
160}
161
162static void release_idr(struct idr *idr, struct mutex *lock, int id)
163{
164 if (lock)
165 mutex_lock(lock);
166 idr_remove(idr, id);
167 if (lock)
168 mutex_unlock(lock);
169}
170
Durgadoss R9b4298a2012-09-18 11:04:54 +0530171int get_tz_trend(struct thermal_zone_device *tz, int trip)
172{
173 enum thermal_trend trend;
174
Eduardo Valentin0c872502013-05-29 21:37:00 +0000175 if (tz->emul_temperature || !tz->ops->get_trend ||
176 tz->ops->get_trend(tz, trip, &trend)) {
Durgadoss R9b4298a2012-09-18 11:04:54 +0530177 if (tz->temperature > tz->last_temperature)
178 trend = THERMAL_TREND_RAISING;
179 else if (tz->temperature < tz->last_temperature)
180 trend = THERMAL_TREND_DROPPING;
181 else
182 trend = THERMAL_TREND_STABLE;
183 }
184
185 return trend;
186}
187EXPORT_SYMBOL(get_tz_trend);
188
189struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
190 struct thermal_cooling_device *cdev, int trip)
191{
192 struct thermal_instance *pos = NULL;
193 struct thermal_instance *target_instance = NULL;
194
195 mutex_lock(&tz->lock);
196 mutex_lock(&cdev->lock);
197
198 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
199 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
200 target_instance = pos;
201 break;
202 }
203 }
204
205 mutex_unlock(&cdev->lock);
206 mutex_unlock(&tz->lock);
207
208 return target_instance;
209}
210EXPORT_SYMBOL(get_thermal_instance);
211
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530212static void print_bind_err_msg(struct thermal_zone_device *tz,
213 struct thermal_cooling_device *cdev, int ret)
214{
215 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
216 tz->type, cdev->type, ret);
217}
218
219static void __bind(struct thermal_zone_device *tz, int mask,
Eduardo Valentina8892d832013-07-16 15:26:28 -0400220 struct thermal_cooling_device *cdev,
221 unsigned long *limits)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530222{
223 int i, ret;
224
225 for (i = 0; i < tz->trips; i++) {
226 if (mask & (1 << i)) {
Eduardo Valentina8892d832013-07-16 15:26:28 -0400227 unsigned long upper, lower;
228
229 upper = THERMAL_NO_LIMIT;
230 lower = THERMAL_NO_LIMIT;
231 if (limits) {
232 lower = limits[i * 2];
233 upper = limits[i * 2 + 1];
234 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530235 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
Eduardo Valentina8892d832013-07-16 15:26:28 -0400236 upper, lower);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530237 if (ret)
238 print_bind_err_msg(tz, cdev, ret);
239 }
240 }
241}
242
243static void __unbind(struct thermal_zone_device *tz, int mask,
244 struct thermal_cooling_device *cdev)
245{
246 int i;
247
248 for (i = 0; i < tz->trips; i++)
249 if (mask & (1 << i))
250 thermal_zone_unbind_cooling_device(tz, i, cdev);
251}
252
253static void bind_cdev(struct thermal_cooling_device *cdev)
254{
255 int i, ret;
256 const struct thermal_zone_params *tzp;
257 struct thermal_zone_device *pos = NULL;
258
259 mutex_lock(&thermal_list_lock);
260
261 list_for_each_entry(pos, &thermal_tz_list, node) {
262 if (!pos->tzp && !pos->ops->bind)
263 continue;
264
Ni Wadea9f2d192013-11-06 14:30:13 +0800265 if (pos->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530266 ret = pos->ops->bind(pos, cdev);
267 if (ret)
268 print_bind_err_msg(pos, cdev, ret);
Ni Wadea9f2d192013-11-06 14:30:13 +0800269 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530270 }
271
272 tzp = pos->tzp;
Hugh Dickins791700c2012-09-27 16:48:28 -0700273 if (!tzp || !tzp->tbp)
274 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530275
276 for (i = 0; i < tzp->num_tbps; i++) {
277 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
278 continue;
279 if (tzp->tbp[i].match(pos, cdev))
280 continue;
281 tzp->tbp[i].cdev = cdev;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400282 __bind(pos, tzp->tbp[i].trip_mask, cdev,
283 tzp->tbp[i].binding_limits);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530284 }
285 }
286
287 mutex_unlock(&thermal_list_lock);
288}
289
290static void bind_tz(struct thermal_zone_device *tz)
291{
292 int i, ret;
293 struct thermal_cooling_device *pos = NULL;
294 const struct thermal_zone_params *tzp = tz->tzp;
295
296 if (!tzp && !tz->ops->bind)
297 return;
298
299 mutex_lock(&thermal_list_lock);
300
Ni Wadea9f2d192013-11-06 14:30:13 +0800301 /* If there is ops->bind, try to use ops->bind */
302 if (tz->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530303 list_for_each_entry(pos, &thermal_cdev_list, node) {
304 ret = tz->ops->bind(tz, pos);
305 if (ret)
306 print_bind_err_msg(tz, pos, ret);
307 }
308 goto exit;
309 }
310
Hugh Dickins791700c2012-09-27 16:48:28 -0700311 if (!tzp || !tzp->tbp)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530312 goto exit;
313
314 list_for_each_entry(pos, &thermal_cdev_list, node) {
315 for (i = 0; i < tzp->num_tbps; i++) {
316 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
317 continue;
318 if (tzp->tbp[i].match(tz, pos))
319 continue;
320 tzp->tbp[i].cdev = pos;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400321 __bind(tz, tzp->tbp[i].trip_mask, pos,
322 tzp->tbp[i].binding_limits);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530323 }
324 }
325exit:
326 mutex_unlock(&thermal_list_lock);
327}
328
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530329static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
330 int delay)
331{
332 if (delay > 1000)
333 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
334 round_jiffies(msecs_to_jiffies(delay)));
335 else if (delay)
336 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
337 msecs_to_jiffies(delay));
338 else
339 cancel_delayed_work(&tz->poll_queue);
340}
341
342static void monitor_thermal_zone(struct thermal_zone_device *tz)
343{
344 mutex_lock(&tz->lock);
345
346 if (tz->passive)
347 thermal_zone_device_set_polling(tz, tz->passive_delay);
348 else if (tz->polling_delay)
349 thermal_zone_device_set_polling(tz, tz->polling_delay);
350 else
351 thermal_zone_device_set_polling(tz, 0);
352
353 mutex_unlock(&tz->lock);
354}
355
356static void handle_non_critical_trips(struct thermal_zone_device *tz,
357 int trip, enum thermal_trip_type trip_type)
358{
Zhang Ruif2234bc2014-01-24 10:23:19 +0800359 tz->governor ? tz->governor->throttle(tz, trip) :
360 def_governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530361}
362
363static void handle_critical_trips(struct thermal_zone_device *tz,
364 int trip, enum thermal_trip_type trip_type)
365{
366 long trip_temp;
367
368 tz->ops->get_trip_temp(tz, trip, &trip_temp);
369
370 /* If we have not crossed the trip_temp, we do not care. */
Srinivas Pandruvada84ffe3e2014-11-12 15:43:29 -0800371 if (trip_temp <= 0 || tz->temperature < trip_temp)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530372 return;
373
Punit Agrawal208cd822014-07-29 11:50:50 +0100374 trace_thermal_zone_trip(tz, trip, trip_type);
375
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530376 if (tz->ops->notify)
377 tz->ops->notify(tz, trip, trip_type);
378
379 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000380 dev_emerg(&tz->device,
381 "critical temperature reached(%d C),shutting down\n",
382 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530383 orderly_poweroff(true);
384 }
385}
386
387static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
388{
389 enum thermal_trip_type type;
390
391 tz->ops->get_trip_type(tz, trip, &type);
392
393 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
394 handle_critical_trips(tz, trip, type);
395 else
396 handle_non_critical_trips(tz, trip, type);
397 /*
398 * Alright, we handled this trip successfully.
399 * So, start monitoring again.
400 */
401 monitor_thermal_zone(tz);
402}
403
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000404/**
405 * thermal_zone_get_temp() - returns its the temperature of thermal zone
406 * @tz: a valid pointer to a struct thermal_zone_device
407 * @temp: a valid pointer to where to store the resulting temperature.
408 *
409 * When a valid thermal zone reference is passed, it will fetch its
410 * temperature and fill @temp.
411 *
412 * Return: On success returns 0, an error code otherwise
413 */
414int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000415{
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000416 int ret = -EINVAL;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800417#ifdef CONFIG_THERMAL_EMULATION
418 int count;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000419 unsigned long crit_temp = -1UL;
420 enum thermal_trip_type type;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800421#endif
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000422
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400423 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000424 goto exit;
425
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000426 mutex_lock(&tz->lock);
427
428 ret = tz->ops->get_temp(tz, temp);
429#ifdef CONFIG_THERMAL_EMULATION
430 if (!tz->emul_temperature)
431 goto skip_emul;
432
433 for (count = 0; count < tz->trips; count++) {
434 ret = tz->ops->get_trip_type(tz, count, &type);
435 if (!ret && type == THERMAL_TRIP_CRITICAL) {
436 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
437 break;
438 }
439 }
440
441 if (ret)
442 goto skip_emul;
443
444 if (*temp < crit_temp)
445 *temp = tz->emul_temperature;
446skip_emul:
447#endif
448 mutex_unlock(&tz->lock);
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000449exit:
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000450 return ret;
451}
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000452EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000453
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530454static void update_temperature(struct thermal_zone_device *tz)
455{
456 long temp;
457 int ret;
458
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000459 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530460 if (ret) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000461 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
462 tz->id);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000463 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530464 }
465
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000466 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530467 tz->last_temperature = tz->temperature;
468 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530469 mutex_unlock(&tz->lock);
Aaron Lu06475b52013-12-02 13:54:26 +0800470
Punit Agrawal100a8fd2014-07-29 11:50:48 +0100471 trace_thermal_temperature(tz);
Aaron Lu06475b52013-12-02 13:54:26 +0800472 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
473 tz->last_temperature, tz->temperature);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530474}
475
476void thermal_zone_device_update(struct thermal_zone_device *tz)
477{
478 int count;
479
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400480 if (!tz->ops->get_temp)
481 return;
482
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530483 update_temperature(tz);
484
485 for (count = 0; count < tz->trips; count++)
486 handle_thermal_trip(tz, count);
487}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000488EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530489
490static void thermal_zone_device_check(struct work_struct *work)
491{
492 struct thermal_zone_device *tz = container_of(work, struct
493 thermal_zone_device,
494 poll_queue.work);
495 thermal_zone_device_update(tz);
496}
497
Zhang Rui203d3d42008-01-17 15:51:08 +0800498/* sys I/F for thermal zone */
499
500#define to_thermal_zone(_dev) \
501 container_of(_dev, struct thermal_zone_device, device)
502
503static ssize_t
504type_show(struct device *dev, struct device_attribute *attr, char *buf)
505{
506 struct thermal_zone_device *tz = to_thermal_zone(dev);
507
508 return sprintf(buf, "%s\n", tz->type);
509}
510
511static ssize_t
512temp_show(struct device *dev, struct device_attribute *attr, char *buf)
513{
514 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000515 long temperature;
516 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800517
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000518 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000519
520 if (ret)
521 return ret;
522
523 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800524}
525
526static ssize_t
527mode_show(struct device *dev, struct device_attribute *attr, char *buf)
528{
529 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000530 enum thermal_device_mode mode;
531 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800532
533 if (!tz->ops->get_mode)
534 return -EPERM;
535
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000536 result = tz->ops->get_mode(tz, &mode);
537 if (result)
538 return result;
539
540 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
541 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800542}
543
544static ssize_t
545mode_store(struct device *dev, struct device_attribute *attr,
546 const char *buf, size_t count)
547{
548 struct thermal_zone_device *tz = to_thermal_zone(dev);
549 int result;
550
551 if (!tz->ops->set_mode)
552 return -EPERM;
553
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530554 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000555 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530556 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000557 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
558 else
559 result = -EINVAL;
560
Zhang Rui203d3d42008-01-17 15:51:08 +0800561 if (result)
562 return result;
563
564 return count;
565}
566
567static ssize_t
568trip_point_type_show(struct device *dev, struct device_attribute *attr,
569 char *buf)
570{
571 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000572 enum thermal_trip_type type;
573 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800574
575 if (!tz->ops->get_trip_type)
576 return -EPERM;
577
578 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
579 return -EINVAL;
580
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000581 result = tz->ops->get_trip_type(tz, trip, &type);
582 if (result)
583 return result;
584
585 switch (type) {
586 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300587 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000588 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300589 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000590 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300591 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000592 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300593 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000594 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300595 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000596 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800597}
598
599static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800600trip_point_temp_store(struct device *dev, struct device_attribute *attr,
601 const char *buf, size_t count)
602{
603 struct thermal_zone_device *tz = to_thermal_zone(dev);
604 int trip, ret;
605 unsigned long temperature;
606
607 if (!tz->ops->set_trip_temp)
608 return -EPERM;
609
610 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
611 return -EINVAL;
612
613 if (kstrtoul(buf, 10, &temperature))
614 return -EINVAL;
615
616 ret = tz->ops->set_trip_temp(tz, trip, temperature);
617
618 return ret ? ret : count;
619}
620
621static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800622trip_point_temp_show(struct device *dev, struct device_attribute *attr,
623 char *buf)
624{
625 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000626 int trip, ret;
627 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800628
629 if (!tz->ops->get_trip_temp)
630 return -EPERM;
631
632 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
633 return -EINVAL;
634
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000635 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
636
637 if (ret)
638 return ret;
639
640 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800641}
642
Matthew Garrett03a971a2008-12-03 18:00:38 +0000643static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800644trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
645 const char *buf, size_t count)
646{
647 struct thermal_zone_device *tz = to_thermal_zone(dev);
648 int trip, ret;
649 unsigned long temperature;
650
651 if (!tz->ops->set_trip_hyst)
652 return -EPERM;
653
654 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
655 return -EINVAL;
656
657 if (kstrtoul(buf, 10, &temperature))
658 return -EINVAL;
659
660 /*
661 * We are not doing any check on the 'temperature' value
662 * here. The driver implementing 'set_trip_hyst' has to
663 * take care of this.
664 */
665 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
666
667 return ret ? ret : count;
668}
669
670static ssize_t
671trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
672 char *buf)
673{
674 struct thermal_zone_device *tz = to_thermal_zone(dev);
675 int trip, ret;
676 unsigned long temperature;
677
678 if (!tz->ops->get_trip_hyst)
679 return -EPERM;
680
681 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
682 return -EINVAL;
683
684 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
685
686 return ret ? ret : sprintf(buf, "%ld\n", temperature);
687}
688
689static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000690passive_store(struct device *dev, struct device_attribute *attr,
691 const char *buf, size_t count)
692{
693 struct thermal_zone_device *tz = to_thermal_zone(dev);
694 struct thermal_cooling_device *cdev = NULL;
695 int state;
696
697 if (!sscanf(buf, "%d\n", &state))
698 return -EINVAL;
699
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100700 /* sanity check: values below 1000 millicelcius don't make sense
701 * and can cause the system to go into a thermal heart attack
702 */
703 if (state && state < 1000)
704 return -EINVAL;
705
Matthew Garrett03a971a2008-12-03 18:00:38 +0000706 if (state && !tz->forced_passive) {
707 mutex_lock(&thermal_list_lock);
708 list_for_each_entry(cdev, &thermal_cdev_list, node) {
709 if (!strncmp("Processor", cdev->type,
710 sizeof("Processor")))
711 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800712 THERMAL_TRIPS_NONE, cdev,
713 THERMAL_NO_LIMIT,
714 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000715 }
716 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100717 if (!tz->passive_delay)
718 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000719 } else if (!state && tz->forced_passive) {
720 mutex_lock(&thermal_list_lock);
721 list_for_each_entry(cdev, &thermal_cdev_list, node) {
722 if (!strncmp("Processor", cdev->type,
723 sizeof("Processor")))
724 thermal_zone_unbind_cooling_device(tz,
725 THERMAL_TRIPS_NONE,
726 cdev);
727 }
728 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100729 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000730 }
731
Matthew Garrett03a971a2008-12-03 18:00:38 +0000732 tz->forced_passive = state;
733
734 thermal_zone_device_update(tz);
735
736 return count;
737}
738
739static ssize_t
740passive_show(struct device *dev, struct device_attribute *attr,
741 char *buf)
742{
743 struct thermal_zone_device *tz = to_thermal_zone(dev);
744
745 return sprintf(buf, "%d\n", tz->forced_passive);
746}
747
Durgadoss R5a2c0902012-09-18 11:04:58 +0530748static ssize_t
749policy_store(struct device *dev, struct device_attribute *attr,
750 const char *buf, size_t count)
751{
752 int ret = -EINVAL;
753 struct thermal_zone_device *tz = to_thermal_zone(dev);
754 struct thermal_governor *gov;
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000755 char name[THERMAL_NAME_LENGTH];
756
757 snprintf(name, sizeof(name), "%s", buf);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530758
759 mutex_lock(&thermal_governor_lock);
Javi Merinob6cc7722014-11-25 16:00:33 +0000760 mutex_lock(&tz->lock);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530761
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000762 gov = __find_governor(strim(name));
Durgadoss R5a2c0902012-09-18 11:04:58 +0530763 if (!gov)
764 goto exit;
765
766 tz->governor = gov;
767 ret = count;
768
769exit:
Javi Merinob6cc7722014-11-25 16:00:33 +0000770 mutex_unlock(&tz->lock);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530771 mutex_unlock(&thermal_governor_lock);
772 return ret;
773}
774
775static ssize_t
776policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
777{
778 struct thermal_zone_device *tz = to_thermal_zone(dev);
779
780 return sprintf(buf, "%s\n", tz->governor->name);
781}
782
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000783#ifdef CONFIG_THERMAL_EMULATION
784static ssize_t
785emul_temp_store(struct device *dev, struct device_attribute *attr,
786 const char *buf, size_t count)
787{
788 struct thermal_zone_device *tz = to_thermal_zone(dev);
789 int ret = 0;
790 unsigned long temperature;
791
792 if (kstrtoul(buf, 10, &temperature))
793 return -EINVAL;
794
795 if (!tz->ops->set_emul_temp) {
796 mutex_lock(&tz->lock);
797 tz->emul_temperature = temperature;
798 mutex_unlock(&tz->lock);
799 } else {
800 ret = tz->ops->set_emul_temp(tz, temperature);
801 }
802
lan,Tianyu800744b2014-01-02 15:47:54 +0800803 if (!ret)
804 thermal_zone_device_update(tz);
805
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000806 return ret ? ret : count;
807}
808static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
809#endif/*CONFIG_THERMAL_EMULATION*/
810
Zhang Rui203d3d42008-01-17 15:51:08 +0800811static DEVICE_ATTR(type, 0444, type_show, NULL);
812static DEVICE_ATTR(temp, 0444, temp_show, NULL);
813static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700814static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530815static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800816
Zhang Rui203d3d42008-01-17 15:51:08 +0800817/* sys I/F for cooling device */
818#define to_cooling_device(_dev) \
819 container_of(_dev, struct thermal_cooling_device, device)
820
821static ssize_t
822thermal_cooling_device_type_show(struct device *dev,
823 struct device_attribute *attr, char *buf)
824{
825 struct thermal_cooling_device *cdev = to_cooling_device(dev);
826
827 return sprintf(buf, "%s\n", cdev->type);
828}
829
830static ssize_t
831thermal_cooling_device_max_state_show(struct device *dev,
832 struct device_attribute *attr, char *buf)
833{
834 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000835 unsigned long state;
836 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800837
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000838 ret = cdev->ops->get_max_state(cdev, &state);
839 if (ret)
840 return ret;
841 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800842}
843
844static ssize_t
845thermal_cooling_device_cur_state_show(struct device *dev,
846 struct device_attribute *attr, char *buf)
847{
848 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000849 unsigned long state;
850 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800851
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000852 ret = cdev->ops->get_cur_state(cdev, &state);
853 if (ret)
854 return ret;
855 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800856}
857
858static ssize_t
859thermal_cooling_device_cur_state_store(struct device *dev,
860 struct device_attribute *attr,
861 const char *buf, size_t count)
862{
863 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000864 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800865 int result;
866
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000867 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800868 return -EINVAL;
869
Roel Kluinedb94912009-12-15 22:46:50 +0100870 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800871 return -EINVAL;
872
873 result = cdev->ops->set_cur_state(cdev, state);
874 if (result)
875 return result;
876 return count;
877}
878
879static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500880__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800881static DEVICE_ATTR(max_state, 0444,
882 thermal_cooling_device_max_state_show, NULL);
883static DEVICE_ATTR(cur_state, 0644,
884 thermal_cooling_device_cur_state_show,
885 thermal_cooling_device_cur_state_store);
886
887static ssize_t
888thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500889 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800890{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800891 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800892
893 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800894 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800895
896 if (instance->trip == THERMAL_TRIPS_NONE)
897 return sprintf(buf, "-1\n");
898 else
899 return sprintf(buf, "%d\n", instance->trip);
900}
901
902/* Device management */
903
904/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000905 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
906 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +0800907 * @trip: indicates which trip point the cooling devices is
908 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000909 * @cdev: pointer to struct thermal_cooling_device
910 * @upper: the Maximum cooling state for this trip point.
911 * THERMAL_NO_LIMIT means no upper limit,
912 * and the cooling device can be in max_state.
913 * @lower: the Minimum cooling state can be used for this trip point.
914 * THERMAL_NO_LIMIT means no lower limit,
915 * and the cooling device can be in cooling state 0.
Len Brown543a9562008-02-07 16:55:08 -0500916 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000917 * This interface function bind a thermal cooling device to the certain trip
918 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500919 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000920 *
921 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800922 */
923int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
924 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800925 struct thermal_cooling_device *cdev,
926 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +0800927{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800928 struct thermal_instance *dev;
929 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500930 struct thermal_zone_device *pos1;
931 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800932 unsigned long max_state;
Lukasz Majewski9a3031d2014-11-18 11:16:30 +0100933 int result, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800934
Len Brown543a9562008-02-07 16:55:08 -0500935 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800936 return -EINVAL;
937
Thomas Sujithc7516702008-02-15 00:58:50 -0500938 list_for_each_entry(pos1, &thermal_tz_list, node) {
939 if (pos1 == tz)
940 break;
941 }
942 list_for_each_entry(pos2, &thermal_cdev_list, node) {
943 if (pos2 == cdev)
944 break;
945 }
946
947 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800948 return -EINVAL;
949
Lukasz Majewski9a3031d2014-11-18 11:16:30 +0100950 ret = cdev->ops->get_max_state(cdev, &max_state);
951 if (ret)
952 return ret;
Zhang Rui9d998422012-06-26 16:35:57 +0800953
954 /* lower default 0, upper default max_state */
955 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
956 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
957
958 if (lower > upper || upper > max_state)
959 return -EINVAL;
960
Zhang Rui203d3d42008-01-17 15:51:08 +0800961 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800962 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800963 if (!dev)
964 return -ENOMEM;
965 dev->tz = tz;
966 dev->cdev = cdev;
967 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800968 dev->upper = upper;
969 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800970 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +0800971
Zhang Rui203d3d42008-01-17 15:51:08 +0800972 result = get_idr(&tz->idr, &tz->lock, &dev->id);
973 if (result)
974 goto free_mem;
975
976 sprintf(dev->name, "cdev%d", dev->id);
977 result =
978 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
979 if (result)
980 goto release_idr;
981
982 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700983 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800984 dev->attr.attr.name = dev->attr_name;
985 dev->attr.attr.mode = 0444;
986 dev->attr.show = thermal_cooling_device_trip_point_show;
987 result = device_create_file(&tz->device, &dev->attr);
988 if (result)
989 goto remove_symbol_link;
990
991 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800992 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800993 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +0800994 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
995 result = -EEXIST;
996 break;
997 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800998 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800999 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001000 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1001 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001002 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001003 mutex_unlock(&tz->lock);
1004
1005 if (!result)
1006 return 0;
1007
1008 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -07001009remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001010 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -07001011release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001012 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -07001013free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001014 kfree(dev);
1015 return result;
1016}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001017EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001018
1019/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001020 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1021 * thermal zone.
1022 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +08001023 * @trip: indicates which trip point the cooling devices is
1024 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001025 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -05001026 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001027 * This interface function unbind a thermal cooling device from the certain
1028 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001029 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001030 *
1031 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001032 */
1033int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1034 int trip,
1035 struct thermal_cooling_device *cdev)
1036{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001037 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001038
1039 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001040 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001041 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001042 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001043 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001044 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001045 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001046 mutex_unlock(&tz->lock);
1047 goto unbind;
1048 }
1049 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001050 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001051 mutex_unlock(&tz->lock);
1052
1053 return -ENODEV;
1054
Joe Perchescaca8b82012-03-21 12:55:02 -07001055unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +08001056 device_remove_file(&tz->device, &pos->attr);
1057 sysfs_remove_link(&tz->device.kobj, pos->name);
1058 release_idr(&tz->idr, &tz->lock, pos->id);
1059 kfree(pos);
1060 return 0;
1061}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001062EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001063
1064static void thermal_release(struct device *dev)
1065{
1066 struct thermal_zone_device *tz;
1067 struct thermal_cooling_device *cdev;
1068
Joe Perchescaca8b82012-03-21 12:55:02 -07001069 if (!strncmp(dev_name(dev), "thermal_zone",
1070 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001071 tz = to_thermal_zone(dev);
1072 kfree(tz);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +05301073 } else if(!strncmp(dev_name(dev), "cooling_device",
1074 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +08001075 cdev = to_cooling_device(dev);
1076 kfree(cdev);
1077 }
1078}
1079
1080static struct class thermal_class = {
1081 .name = "thermal",
1082 .dev_release = thermal_release,
1083};
1084
1085/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001086 * __thermal_cooling_device_register() - register a new thermal cooling device
1087 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +08001088 * @type: the thermal cooling device type.
1089 * @devdata: device private data.
1090 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001091 *
1092 * This interface function adds a new thermal cooling device (fan/processor/...)
1093 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1094 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001095 * It also gives the opportunity to link the cooling device to a device tree
1096 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001097 *
1098 * Return: a pointer to the created struct thermal_cooling_device or an
1099 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001100 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001101static struct thermal_cooling_device *
1102__thermal_cooling_device_register(struct device_node *np,
1103 char *type, void *devdata,
1104 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001105{
1106 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001107 int result;
1108
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001109 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001110 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001111
1112 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001113 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001114 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001115
1116 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1117 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001118 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001119
1120 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1121 if (result) {
1122 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001123 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001124 }
1125
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001126 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +08001127 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001128 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001129 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +08001130 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +08001131 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +08001132 cdev->device.class = &thermal_class;
1133 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001134 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001135 result = device_register(&cdev->device);
1136 if (result) {
1137 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1138 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001139 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001140 }
1141
1142 /* sys I/F */
1143 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001144 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001145 if (result)
1146 goto unregister;
1147 }
1148
1149 result = device_create_file(&cdev->device, &dev_attr_max_state);
1150 if (result)
1151 goto unregister;
1152
1153 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1154 if (result)
1155 goto unregister;
1156
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301157 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001158 mutex_lock(&thermal_list_lock);
1159 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001160 mutex_unlock(&thermal_list_lock);
1161
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301162 /* Update binding information for 'this' new cdev */
1163 bind_cdev(cdev);
1164
1165 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001166
Joe Perchescaca8b82012-03-21 12:55:02 -07001167unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001168 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1169 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001170 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001171}
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001172
1173/**
1174 * thermal_cooling_device_register() - register a new thermal cooling device
1175 * @type: the thermal cooling device type.
1176 * @devdata: device private data.
1177 * @ops: standard thermal cooling devices callbacks.
1178 *
1179 * This interface function adds a new thermal cooling device (fan/processor/...)
1180 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1181 * to all the thermal zone devices registered at the same time.
1182 *
1183 * Return: a pointer to the created struct thermal_cooling_device or an
1184 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1185 */
1186struct thermal_cooling_device *
1187thermal_cooling_device_register(char *type, void *devdata,
1188 const struct thermal_cooling_device_ops *ops)
1189{
1190 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1191}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001192EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001193
1194/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001195 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1196 * @np: a pointer to a device tree node.
1197 * @type: the thermal cooling device type.
1198 * @devdata: device private data.
1199 * @ops: standard thermal cooling devices callbacks.
1200 *
1201 * This function will register a cooling device with device tree node reference.
1202 * This interface function adds a new thermal cooling device (fan/processor/...)
1203 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1204 * to all the thermal zone devices registered at the same time.
1205 *
1206 * Return: a pointer to the created struct thermal_cooling_device or an
1207 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1208 */
1209struct thermal_cooling_device *
1210thermal_of_cooling_device_register(struct device_node *np,
1211 char *type, void *devdata,
1212 const struct thermal_cooling_device_ops *ops)
1213{
1214 return __thermal_cooling_device_register(np, type, devdata, ops);
1215}
1216EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1217
1218/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001219 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001220 * @cdev: the thermal cooling device to remove.
1221 *
1222 * thermal_cooling_device_unregister() must be called when the device is no
1223 * longer needed.
1224 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301225void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001226{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301227 int i;
1228 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001229 struct thermal_zone_device *tz;
1230 struct thermal_cooling_device *pos = NULL;
1231
1232 if (!cdev)
1233 return;
1234
1235 mutex_lock(&thermal_list_lock);
1236 list_for_each_entry(pos, &thermal_cdev_list, node)
1237 if (pos == cdev)
1238 break;
1239 if (pos != cdev) {
1240 /* thermal cooling device not found */
1241 mutex_unlock(&thermal_list_lock);
1242 return;
1243 }
1244 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301245
1246 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001247 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301248 if (tz->ops->unbind) {
1249 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001250 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301251 }
1252
1253 if (!tz->tzp || !tz->tzp->tbp)
1254 continue;
1255
1256 tzp = tz->tzp;
1257 for (i = 0; i < tzp->num_tbps; i++) {
1258 if (tzp->tbp[i].cdev == cdev) {
1259 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1260 tzp->tbp[i].cdev = NULL;
1261 }
1262 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001263 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301264
Zhang Rui203d3d42008-01-17 15:51:08 +08001265 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301266
Zhang Rui203d3d42008-01-17 15:51:08 +08001267 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001268 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001269 device_remove_file(&cdev->device, &dev_attr_max_state);
1270 device_remove_file(&cdev->device, &dev_attr_cur_state);
1271
1272 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1273 device_unregister(&cdev->device);
1274 return;
1275}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001276EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001277
Durgadoss Rdc765482012-09-18 11:05:00 +05301278void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001279{
1280 struct thermal_instance *instance;
1281 unsigned long target = 0;
1282
1283 /* cooling device is updated*/
1284 if (cdev->updated)
1285 return;
1286
Zhang Ruif4a821c2012-07-24 16:56:21 +08001287 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001288 /* Make sure cdev enters the deepest cooling state */
1289 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
Aaron Lu06475b52013-12-02 13:54:26 +08001290 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1291 instance->tz->id, instance->target);
Zhang Ruice119f82012-06-27 14:13:04 +08001292 if (instance->target == THERMAL_NO_TARGET)
1293 continue;
1294 if (instance->target > target)
1295 target = instance->target;
1296 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001297 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001298 cdev->ops->set_cur_state(cdev, target);
1299 cdev->updated = true;
Punit Agrawal39811562014-07-29 11:50:49 +01001300 trace_cdev_update(cdev, target);
Aaron Lu06475b52013-12-02 13:54:26 +08001301 dev_dbg(&cdev->device, "set to state %lu\n", target);
Zhang Ruice119f82012-06-27 14:13:04 +08001302}
Durgadoss Rdc765482012-09-18 11:05:00 +05301303EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001304
Matthew Garrettb1569e92008-12-03 17:55:32 +00001305/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001306 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301307 * @tz: thermal zone device
1308 * @trip: indicates which trip point has been crossed
1309 *
1310 * This function handles the trip events from sensor drivers. It starts
1311 * throttling the cooling devices according to the policy configured.
1312 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1313 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1314 * The throttling policy is based on the configured platform data; if no
1315 * platform data is provided, this uses the step_wise throttling policy.
1316 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001317void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301318{
1319 handle_thermal_trip(tz, trip);
1320}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001321EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301322
1323/**
Eduardo Valentin269c1742013-04-23 21:48:19 +00001324 * create_trip_attrs() - create attributes for trip points
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001325 * @tz: the thermal zone device
1326 * @mask: Writeable trip point bitmap.
Eduardo Valentin269c1742013-04-23 21:48:19 +00001327 *
1328 * helper function to instantiate sysfs entries for every trip
1329 * point and its properties of a struct thermal_zone_device.
1330 *
1331 * Return: 0 on success, the proper error value otherwise.
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001332 */
1333static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1334{
1335 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001336 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001337
Durgadoss R27365a62012-07-25 10:10:59 +08001338 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001339 if (!tz->trip_type_attrs)
1340 return -ENOMEM;
1341
Durgadoss R27365a62012-07-25 10:10:59 +08001342 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001343 if (!tz->trip_temp_attrs) {
1344 kfree(tz->trip_type_attrs);
1345 return -ENOMEM;
1346 }
1347
Durgadoss R27365a62012-07-25 10:10:59 +08001348 if (tz->ops->get_trip_hyst) {
1349 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1350 if (!tz->trip_hyst_attrs) {
1351 kfree(tz->trip_type_attrs);
1352 kfree(tz->trip_temp_attrs);
1353 return -ENOMEM;
1354 }
1355 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001356
Durgadoss R27365a62012-07-25 10:10:59 +08001357
1358 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001359 /* create trip type attribute */
1360 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1361 "trip_point_%d_type", indx);
1362
1363 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1364 tz->trip_type_attrs[indx].attr.attr.name =
1365 tz->trip_type_attrs[indx].name;
1366 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1367 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1368
1369 device_create_file(&tz->device,
1370 &tz->trip_type_attrs[indx].attr);
1371
1372 /* create trip temp attribute */
1373 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1374 "trip_point_%d_temp", indx);
1375
1376 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1377 tz->trip_temp_attrs[indx].attr.attr.name =
1378 tz->trip_temp_attrs[indx].name;
1379 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1380 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1381 if (mask & (1 << indx)) {
1382 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1383 tz->trip_temp_attrs[indx].attr.store =
1384 trip_point_temp_store;
1385 }
1386
1387 device_create_file(&tz->device,
1388 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001389
1390 /* create Optional trip hyst attribute */
1391 if (!tz->ops->get_trip_hyst)
1392 continue;
1393 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1394 "trip_point_%d_hyst", indx);
1395
1396 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1397 tz->trip_hyst_attrs[indx].attr.attr.name =
1398 tz->trip_hyst_attrs[indx].name;
1399 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1400 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1401 if (tz->ops->set_trip_hyst) {
1402 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1403 tz->trip_hyst_attrs[indx].attr.store =
1404 trip_point_hyst_store;
1405 }
1406
1407 device_create_file(&tz->device,
1408 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001409 }
1410 return 0;
1411}
1412
1413static void remove_trip_attrs(struct thermal_zone_device *tz)
1414{
1415 int indx;
1416
1417 for (indx = 0; indx < tz->trips; indx++) {
1418 device_remove_file(&tz->device,
1419 &tz->trip_type_attrs[indx].attr);
1420 device_remove_file(&tz->device,
1421 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001422 if (tz->ops->get_trip_hyst)
1423 device_remove_file(&tz->device,
1424 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001425 }
1426 kfree(tz->trip_type_attrs);
1427 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001428 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001429}
1430
1431/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001432 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001433 * @type: the thermal zone device type
1434 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001435 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001436 * @devdata: private device data
1437 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301438 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001439 * @passive_delay: number of milliseconds to wait between polls when
1440 * performing passive cooling
1441 * @polling_delay: number of milliseconds to wait between polls when checking
1442 * whether trip points have been crossed (0 for interrupt
1443 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001444 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001445 * This interface function adds a new thermal zone device (sensor) to
1446 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1447 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001448 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001449 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001450 *
1451 * Return: a pointer to the created struct thermal_zone_device or an
1452 * in case of error, an ERR_PTR. Caller must check return value with
1453 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001454 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001455struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001456 int trips, int mask, void *devdata,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001457 struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301458 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001459 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001460{
1461 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001462 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001463 int result;
1464 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001465 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001466
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001467 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001468 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001469
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001470 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001471 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001472
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001473 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001474 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001475
Jonghwa Lee83720d02013-05-18 09:50:26 +00001476 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001477 return ERR_PTR(-EINVAL);
1478
Zhang Rui203d3d42008-01-17 15:51:08 +08001479 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1480 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001481 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001482
Zhang Rui2d374132012-06-27 10:09:00 +08001483 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001484 idr_init(&tz->idr);
1485 mutex_init(&tz->lock);
1486 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1487 if (result) {
1488 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001489 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001490 }
1491
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001492 strlcpy(tz->type, type ? : "", sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001493 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301494 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001495 tz->device.class = &thermal_class;
1496 tz->devdata = devdata;
1497 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001498 tz->passive_delay = passive_delay;
1499 tz->polling_delay = polling_delay;
1500
Kay Sievers354655e2009-01-06 10:44:37 -08001501 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001502 result = device_register(&tz->device);
1503 if (result) {
1504 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1505 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001506 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001507 }
1508
1509 /* sys I/F */
1510 if (type) {
1511 result = device_create_file(&tz->device, &dev_attr_type);
1512 if (result)
1513 goto unregister;
1514 }
1515
1516 result = device_create_file(&tz->device, &dev_attr_temp);
1517 if (result)
1518 goto unregister;
1519
1520 if (ops->get_mode) {
1521 result = device_create_file(&tz->device, &dev_attr_mode);
1522 if (result)
1523 goto unregister;
1524 }
1525
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001526 result = create_trip_attrs(tz, mask);
1527 if (result)
1528 goto unregister;
1529
Zhang Rui203d3d42008-01-17 15:51:08 +08001530 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001531 tz->ops->get_trip_type(tz, count, &trip_type);
1532 if (trip_type == THERMAL_TRIP_PASSIVE)
1533 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001534 }
1535
Durgadoss R5a2c0902012-09-18 11:04:58 +05301536 if (!passive) {
1537 result = device_create_file(&tz->device, &dev_attr_passive);
1538 if (result)
1539 goto unregister;
1540 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001541
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001542#ifdef CONFIG_THERMAL_EMULATION
1543 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1544 if (result)
1545 goto unregister;
1546#endif
Durgadoss R5a2c0902012-09-18 11:04:58 +05301547 /* Create policy attribute */
1548 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001549 if (result)
1550 goto unregister;
1551
Durgadoss Ra4a15482012-09-18 11:04:57 +05301552 /* Update 'this' zone's governor information */
1553 mutex_lock(&thermal_governor_lock);
1554
1555 if (tz->tzp)
1556 tz->governor = __find_governor(tz->tzp->governor_name);
1557 else
Zhang Ruif2234bc2014-01-24 10:23:19 +08001558 tz->governor = def_governor;
Durgadoss Ra4a15482012-09-18 11:04:57 +05301559
1560 mutex_unlock(&thermal_governor_lock);
1561
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04001562 if (!tz->tzp || !tz->tzp->no_hwmon) {
1563 result = thermal_add_hwmon_sysfs(tz);
1564 if (result)
1565 goto unregister;
1566 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08001567
Zhang Rui203d3d42008-01-17 15:51:08 +08001568 mutex_lock(&thermal_list_lock);
1569 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001570 mutex_unlock(&thermal_list_lock);
1571
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301572 /* Bind cooling devices for this zone */
1573 bind_tz(tz);
1574
Matthew Garrettb1569e92008-12-03 17:55:32 +00001575 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1576
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001577 if (!tz->ops->get_temp)
1578 thermal_zone_device_set_polling(tz, 0);
1579
Matthew Garrettb1569e92008-12-03 17:55:32 +00001580 thermal_zone_device_update(tz);
1581
Yao Dongdong14015862014-10-28 07:40:25 +00001582 return tz;
Zhang Rui203d3d42008-01-17 15:51:08 +08001583
Joe Perchescaca8b82012-03-21 12:55:02 -07001584unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001585 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1586 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001587 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001588}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001589EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001590
1591/**
1592 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001593 * @tz: the thermal zone device to remove
1594 */
1595void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1596{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301597 int i;
1598 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001599 struct thermal_cooling_device *cdev;
1600 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001601
1602 if (!tz)
1603 return;
1604
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301605 tzp = tz->tzp;
1606
Zhang Rui203d3d42008-01-17 15:51:08 +08001607 mutex_lock(&thermal_list_lock);
1608 list_for_each_entry(pos, &thermal_tz_list, node)
1609 if (pos == tz)
1610 break;
1611 if (pos != tz) {
1612 /* thermal zone device not found */
1613 mutex_unlock(&thermal_list_lock);
1614 return;
1615 }
1616 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301617
1618 /* Unbind all cdevs associated with 'this' thermal zone */
1619 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1620 if (tz->ops->unbind) {
1621 tz->ops->unbind(tz, cdev);
1622 continue;
1623 }
1624
1625 if (!tzp || !tzp->tbp)
1626 break;
1627
1628 for (i = 0; i < tzp->num_tbps; i++) {
1629 if (tzp->tbp[i].cdev == cdev) {
1630 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1631 tzp->tbp[i].cdev = NULL;
1632 }
1633 }
1634 }
1635
Zhang Rui203d3d42008-01-17 15:51:08 +08001636 mutex_unlock(&thermal_list_lock);
1637
Matthew Garrettb1569e92008-12-03 17:55:32 +00001638 thermal_zone_device_set_polling(tz, 0);
1639
Zhang Rui203d3d42008-01-17 15:51:08 +08001640 if (tz->type[0])
1641 device_remove_file(&tz->device, &dev_attr_type);
1642 device_remove_file(&tz->device, &dev_attr_temp);
1643 if (tz->ops->get_mode)
1644 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301645 device_remove_file(&tz->device, &dev_attr_policy);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001646 remove_trip_attrs(tz);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301647 tz->governor = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001648
Zhang Ruie68b16a2008-04-21 16:07:52 +08001649 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001650 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1651 idr_destroy(&tz->idr);
1652 mutex_destroy(&tz->lock);
1653 device_unregister(&tz->device);
1654 return;
1655}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001656EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001657
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001658/**
1659 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1660 * @name: thermal zone name to fetch the temperature
1661 *
1662 * When only one zone is found with the passed name, returns a reference to it.
1663 *
1664 * Return: On success returns a reference to an unique thermal zone with
1665 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1666 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1667 */
1668struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1669{
1670 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1671 unsigned int found = 0;
1672
1673 if (!name)
1674 goto exit;
1675
1676 mutex_lock(&thermal_list_lock);
1677 list_for_each_entry(pos, &thermal_tz_list, node)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -07001678 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001679 found++;
1680 ref = pos;
1681 }
1682 mutex_unlock(&thermal_list_lock);
1683
1684 /* nothing has been found, thus an error code for it */
1685 if (found == 0)
1686 ref = ERR_PTR(-ENODEV);
1687 else if (found > 1)
1688 /* Success only when an unique zone is found */
1689 ref = ERR_PTR(-EEXIST);
1690
1691exit:
1692 return ref;
1693}
1694EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1695
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001696#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01001697static const struct genl_multicast_group thermal_event_mcgrps[] = {
1698 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1699};
1700
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001701static struct genl_family thermal_event_genl_family = {
1702 .id = GENL_ID_GENERATE,
1703 .name = THERMAL_GENL_FAMILY_NAME,
1704 .version = THERMAL_GENL_VERSION,
1705 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001706 .mcgrps = thermal_event_mcgrps,
1707 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001708};
1709
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001710int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1711 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301712{
1713 struct sk_buff *skb;
1714 struct nlattr *attr;
1715 struct thermal_genl_event *thermal_event;
1716 void *msg_header;
1717 int size;
1718 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001719 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301720
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001721 if (!tz)
1722 return -EINVAL;
1723
R.Durgadoss4cb18722010-10-27 03:33:29 +05301724 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001725 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1726 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301727
1728 skb = genlmsg_new(size, GFP_ATOMIC);
1729 if (!skb)
1730 return -ENOMEM;
1731
1732 /* add the genetlink message header */
1733 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1734 &thermal_event_genl_family, 0,
1735 THERMAL_GENL_CMD_EVENT);
1736 if (!msg_header) {
1737 nlmsg_free(skb);
1738 return -ENOMEM;
1739 }
1740
1741 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001742 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1743 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301744
1745 if (!attr) {
1746 nlmsg_free(skb);
1747 return -EINVAL;
1748 }
1749
1750 thermal_event = nla_data(attr);
1751 if (!thermal_event) {
1752 nlmsg_free(skb);
1753 return -EINVAL;
1754 }
1755
1756 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1757
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001758 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301759 thermal_event->event = event;
1760
1761 /* send multicast genetlink message */
Johannes Berg053c0952015-01-16 22:09:00 +01001762 genlmsg_end(skb, msg_header);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301763
Johannes Berg68eb5502013-11-19 15:19:38 +01001764 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001765 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301766 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001767 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301768
1769 return result;
1770}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001771EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301772
1773static int genetlink_init(void)
1774{
Johannes Berg2a94fe42013-11-19 15:19:39 +01001775 return genl_register_family(&thermal_event_genl_family);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301776}
1777
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001778static void genetlink_exit(void)
1779{
1780 genl_unregister_family(&thermal_event_genl_family);
1781}
1782#else /* !CONFIG_NET */
1783static inline int genetlink_init(void) { return 0; }
1784static inline void genetlink_exit(void) {}
1785#endif /* !CONFIG_NET */
1786
Zhang Rui80a26a52013-03-26 16:38:29 +08001787static int __init thermal_register_governors(void)
1788{
1789 int result;
1790
1791 result = thermal_gov_step_wise_register();
1792 if (result)
1793 return result;
1794
1795 result = thermal_gov_fair_share_register();
1796 if (result)
1797 return result;
1798
Peter Feuerere4dbf982014-07-22 17:37:13 +02001799 result = thermal_gov_bang_bang_register();
1800 if (result)
1801 return result;
1802
Zhang Rui80a26a52013-03-26 16:38:29 +08001803 return thermal_gov_user_space_register();
1804}
1805
1806static void thermal_unregister_governors(void)
1807{
1808 thermal_gov_step_wise_unregister();
1809 thermal_gov_fair_share_unregister();
Peter Feuerere4dbf982014-07-22 17:37:13 +02001810 thermal_gov_bang_bang_unregister();
Zhang Rui80a26a52013-03-26 16:38:29 +08001811 thermal_gov_user_space_unregister();
1812}
1813
Zhang Rui203d3d42008-01-17 15:51:08 +08001814static int __init thermal_init(void)
1815{
Zhang Rui80a26a52013-03-26 16:38:29 +08001816 int result;
1817
1818 result = thermal_register_governors();
1819 if (result)
1820 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08001821
1822 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001823 if (result)
1824 goto unregister_governors;
1825
R.Durgadoss4cb18722010-10-27 03:33:29 +05301826 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08001827 if (result)
1828 goto unregister_class;
1829
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001830 result = of_parse_thermal_zones();
1831 if (result)
1832 goto exit_netlink;
1833
Zhang Rui80a26a52013-03-26 16:38:29 +08001834 return 0;
1835
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001836exit_netlink:
1837 genetlink_exit();
Zhang Rui80a26a52013-03-26 16:38:29 +08001838unregister_class:
1839 class_unregister(&thermal_class);
Luis Henriques9d367e52014-12-03 21:20:21 +00001840unregister_governors:
1841 thermal_unregister_governors();
Zhang Rui80a26a52013-03-26 16:38:29 +08001842error:
1843 idr_destroy(&thermal_tz_idr);
1844 idr_destroy(&thermal_cdev_idr);
1845 mutex_destroy(&thermal_idr_lock);
1846 mutex_destroy(&thermal_list_lock);
1847 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001848 return result;
1849}
1850
1851static void __exit thermal_exit(void)
1852{
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001853 of_thermal_destroy_zones();
Zhang Rui80a26a52013-03-26 16:38:29 +08001854 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001855 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001856 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08001857 idr_destroy(&thermal_tz_idr);
1858 idr_destroy(&thermal_cdev_idr);
1859 mutex_destroy(&thermal_idr_lock);
1860 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08001861 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001862}
1863
R.Durgadoss4cb18722010-10-27 03:33:29 +05301864fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001865module_exit(thermal_exit);