blob: a6cb9b78b6293bc7725e5859571d5528c3a7ef97 [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,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000221 unsigned long *limits,
222 unsigned int weight)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530223{
224 int i, ret;
225
226 for (i = 0; i < tz->trips; i++) {
227 if (mask & (1 << i)) {
Eduardo Valentina8892d832013-07-16 15:26:28 -0400228 unsigned long upper, lower;
229
230 upper = THERMAL_NO_LIMIT;
231 lower = THERMAL_NO_LIMIT;
232 if (limits) {
233 lower = limits[i * 2];
234 upper = limits[i * 2 + 1];
235 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530236 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000237 upper, lower,
238 weight);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530239 if (ret)
240 print_bind_err_msg(tz, cdev, ret);
241 }
242 }
243}
244
245static void __unbind(struct thermal_zone_device *tz, int mask,
246 struct thermal_cooling_device *cdev)
247{
248 int i;
249
250 for (i = 0; i < tz->trips; i++)
251 if (mask & (1 << i))
252 thermal_zone_unbind_cooling_device(tz, i, cdev);
253}
254
255static void bind_cdev(struct thermal_cooling_device *cdev)
256{
257 int i, ret;
258 const struct thermal_zone_params *tzp;
259 struct thermal_zone_device *pos = NULL;
260
261 mutex_lock(&thermal_list_lock);
262
263 list_for_each_entry(pos, &thermal_tz_list, node) {
264 if (!pos->tzp && !pos->ops->bind)
265 continue;
266
Ni Wadea9f2d192013-11-06 14:30:13 +0800267 if (pos->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530268 ret = pos->ops->bind(pos, cdev);
269 if (ret)
270 print_bind_err_msg(pos, cdev, ret);
Ni Wadea9f2d192013-11-06 14:30:13 +0800271 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530272 }
273
274 tzp = pos->tzp;
Hugh Dickins791700c2012-09-27 16:48:28 -0700275 if (!tzp || !tzp->tbp)
276 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530277
278 for (i = 0; i < tzp->num_tbps; i++) {
279 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
280 continue;
281 if (tzp->tbp[i].match(pos, cdev))
282 continue;
283 tzp->tbp[i].cdev = cdev;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400284 __bind(pos, tzp->tbp[i].trip_mask, cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000285 tzp->tbp[i].binding_limits,
286 tzp->tbp[i].weight);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530287 }
288 }
289
290 mutex_unlock(&thermal_list_lock);
291}
292
293static void bind_tz(struct thermal_zone_device *tz)
294{
295 int i, ret;
296 struct thermal_cooling_device *pos = NULL;
297 const struct thermal_zone_params *tzp = tz->tzp;
298
299 if (!tzp && !tz->ops->bind)
300 return;
301
302 mutex_lock(&thermal_list_lock);
303
Ni Wadea9f2d192013-11-06 14:30:13 +0800304 /* If there is ops->bind, try to use ops->bind */
305 if (tz->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530306 list_for_each_entry(pos, &thermal_cdev_list, node) {
307 ret = tz->ops->bind(tz, pos);
308 if (ret)
309 print_bind_err_msg(tz, pos, ret);
310 }
311 goto exit;
312 }
313
Hugh Dickins791700c2012-09-27 16:48:28 -0700314 if (!tzp || !tzp->tbp)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530315 goto exit;
316
317 list_for_each_entry(pos, &thermal_cdev_list, node) {
318 for (i = 0; i < tzp->num_tbps; i++) {
319 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
320 continue;
321 if (tzp->tbp[i].match(tz, pos))
322 continue;
323 tzp->tbp[i].cdev = pos;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400324 __bind(tz, tzp->tbp[i].trip_mask, pos,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000325 tzp->tbp[i].binding_limits,
326 tzp->tbp[i].weight);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530327 }
328 }
329exit:
330 mutex_unlock(&thermal_list_lock);
331}
332
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530333static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
334 int delay)
335{
336 if (delay > 1000)
337 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
338 round_jiffies(msecs_to_jiffies(delay)));
339 else if (delay)
340 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
341 msecs_to_jiffies(delay));
342 else
343 cancel_delayed_work(&tz->poll_queue);
344}
345
346static void monitor_thermal_zone(struct thermal_zone_device *tz)
347{
348 mutex_lock(&tz->lock);
349
350 if (tz->passive)
351 thermal_zone_device_set_polling(tz, tz->passive_delay);
352 else if (tz->polling_delay)
353 thermal_zone_device_set_polling(tz, tz->polling_delay);
354 else
355 thermal_zone_device_set_polling(tz, 0);
356
357 mutex_unlock(&tz->lock);
358}
359
360static void handle_non_critical_trips(struct thermal_zone_device *tz,
361 int trip, enum thermal_trip_type trip_type)
362{
Zhang Ruif2234bc2014-01-24 10:23:19 +0800363 tz->governor ? tz->governor->throttle(tz, trip) :
364 def_governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530365}
366
367static void handle_critical_trips(struct thermal_zone_device *tz,
368 int trip, enum thermal_trip_type trip_type)
369{
370 long trip_temp;
371
372 tz->ops->get_trip_temp(tz, trip, &trip_temp);
373
374 /* If we have not crossed the trip_temp, we do not care. */
Srinivas Pandruvada84ffe3e2014-11-12 15:43:29 -0800375 if (trip_temp <= 0 || tz->temperature < trip_temp)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530376 return;
377
Punit Agrawal208cd822014-07-29 11:50:50 +0100378 trace_thermal_zone_trip(tz, trip, trip_type);
379
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530380 if (tz->ops->notify)
381 tz->ops->notify(tz, trip, trip_type);
382
383 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000384 dev_emerg(&tz->device,
385 "critical temperature reached(%d C),shutting down\n",
386 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530387 orderly_poweroff(true);
388 }
389}
390
391static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
392{
393 enum thermal_trip_type type;
394
395 tz->ops->get_trip_type(tz, trip, &type);
396
397 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
398 handle_critical_trips(tz, trip, type);
399 else
400 handle_non_critical_trips(tz, trip, type);
401 /*
402 * Alright, we handled this trip successfully.
403 * So, start monitoring again.
404 */
405 monitor_thermal_zone(tz);
406}
407
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000408/**
409 * thermal_zone_get_temp() - returns its the temperature of thermal zone
410 * @tz: a valid pointer to a struct thermal_zone_device
411 * @temp: a valid pointer to where to store the resulting temperature.
412 *
413 * When a valid thermal zone reference is passed, it will fetch its
414 * temperature and fill @temp.
415 *
416 * Return: On success returns 0, an error code otherwise
417 */
418int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000419{
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000420 int ret = -EINVAL;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800421#ifdef CONFIG_THERMAL_EMULATION
422 int count;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000423 unsigned long crit_temp = -1UL;
424 enum thermal_trip_type type;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800425#endif
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000426
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400427 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000428 goto exit;
429
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000430 mutex_lock(&tz->lock);
431
432 ret = tz->ops->get_temp(tz, temp);
433#ifdef CONFIG_THERMAL_EMULATION
434 if (!tz->emul_temperature)
435 goto skip_emul;
436
437 for (count = 0; count < tz->trips; count++) {
438 ret = tz->ops->get_trip_type(tz, count, &type);
439 if (!ret && type == THERMAL_TRIP_CRITICAL) {
440 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
441 break;
442 }
443 }
444
445 if (ret)
446 goto skip_emul;
447
448 if (*temp < crit_temp)
449 *temp = tz->emul_temperature;
450skip_emul:
451#endif
452 mutex_unlock(&tz->lock);
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000453exit:
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000454 return ret;
455}
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000456EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000457
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530458static void update_temperature(struct thermal_zone_device *tz)
459{
460 long temp;
461 int ret;
462
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000463 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530464 if (ret) {
Hans de Goede7e497a72015-03-21 15:02:55 +0100465 if (ret != -EAGAIN)
466 dev_warn(&tz->device,
467 "failed to read out thermal zone (%d)\n",
468 ret);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000469 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530470 }
471
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000472 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530473 tz->last_temperature = tz->temperature;
474 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530475 mutex_unlock(&tz->lock);
Aaron Lu06475b52013-12-02 13:54:26 +0800476
Punit Agrawal100a8fd2014-07-29 11:50:48 +0100477 trace_thermal_temperature(tz);
Aaron Lu06475b52013-12-02 13:54:26 +0800478 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
479 tz->last_temperature, tz->temperature);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530480}
481
482void thermal_zone_device_update(struct thermal_zone_device *tz)
483{
484 int count;
485
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400486 if (!tz->ops->get_temp)
487 return;
488
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530489 update_temperature(tz);
490
491 for (count = 0; count < tz->trips; count++)
492 handle_thermal_trip(tz, count);
493}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000494EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530495
496static void thermal_zone_device_check(struct work_struct *work)
497{
498 struct thermal_zone_device *tz = container_of(work, struct
499 thermal_zone_device,
500 poll_queue.work);
501 thermal_zone_device_update(tz);
502}
503
Zhang Rui203d3d42008-01-17 15:51:08 +0800504/* sys I/F for thermal zone */
505
506#define to_thermal_zone(_dev) \
507 container_of(_dev, struct thermal_zone_device, device)
508
509static ssize_t
510type_show(struct device *dev, struct device_attribute *attr, char *buf)
511{
512 struct thermal_zone_device *tz = to_thermal_zone(dev);
513
514 return sprintf(buf, "%s\n", tz->type);
515}
516
517static ssize_t
518temp_show(struct device *dev, struct device_attribute *attr, char *buf)
519{
520 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000521 long temperature;
522 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800523
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000524 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000525
526 if (ret)
527 return ret;
528
529 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800530}
531
532static ssize_t
533mode_show(struct device *dev, struct device_attribute *attr, char *buf)
534{
535 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000536 enum thermal_device_mode mode;
537 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800538
539 if (!tz->ops->get_mode)
540 return -EPERM;
541
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000542 result = tz->ops->get_mode(tz, &mode);
543 if (result)
544 return result;
545
546 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
547 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800548}
549
550static ssize_t
551mode_store(struct device *dev, struct device_attribute *attr,
552 const char *buf, size_t count)
553{
554 struct thermal_zone_device *tz = to_thermal_zone(dev);
555 int result;
556
557 if (!tz->ops->set_mode)
558 return -EPERM;
559
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530560 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000561 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530562 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000563 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
564 else
565 result = -EINVAL;
566
Zhang Rui203d3d42008-01-17 15:51:08 +0800567 if (result)
568 return result;
569
570 return count;
571}
572
573static ssize_t
574trip_point_type_show(struct device *dev, struct device_attribute *attr,
575 char *buf)
576{
577 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000578 enum thermal_trip_type type;
579 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800580
581 if (!tz->ops->get_trip_type)
582 return -EPERM;
583
584 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
585 return -EINVAL;
586
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000587 result = tz->ops->get_trip_type(tz, trip, &type);
588 if (result)
589 return result;
590
591 switch (type) {
592 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300593 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000594 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300595 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000596 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300597 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000598 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300599 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000600 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300601 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000602 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800603}
604
605static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800606trip_point_temp_store(struct device *dev, struct device_attribute *attr,
607 const char *buf, size_t count)
608{
609 struct thermal_zone_device *tz = to_thermal_zone(dev);
610 int trip, ret;
611 unsigned long temperature;
612
613 if (!tz->ops->set_trip_temp)
614 return -EPERM;
615
616 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
617 return -EINVAL;
618
619 if (kstrtoul(buf, 10, &temperature))
620 return -EINVAL;
621
622 ret = tz->ops->set_trip_temp(tz, trip, temperature);
623
624 return ret ? ret : count;
625}
626
627static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800628trip_point_temp_show(struct device *dev, struct device_attribute *attr,
629 char *buf)
630{
631 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000632 int trip, ret;
633 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800634
635 if (!tz->ops->get_trip_temp)
636 return -EPERM;
637
638 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
639 return -EINVAL;
640
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000641 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
642
643 if (ret)
644 return ret;
645
646 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800647}
648
Matthew Garrett03a971a2008-12-03 18:00:38 +0000649static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800650trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
651 const char *buf, size_t count)
652{
653 struct thermal_zone_device *tz = to_thermal_zone(dev);
654 int trip, ret;
655 unsigned long temperature;
656
657 if (!tz->ops->set_trip_hyst)
658 return -EPERM;
659
660 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
661 return -EINVAL;
662
663 if (kstrtoul(buf, 10, &temperature))
664 return -EINVAL;
665
666 /*
667 * We are not doing any check on the 'temperature' value
668 * here. The driver implementing 'set_trip_hyst' has to
669 * take care of this.
670 */
671 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
672
673 return ret ? ret : count;
674}
675
676static ssize_t
677trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
678 char *buf)
679{
680 struct thermal_zone_device *tz = to_thermal_zone(dev);
681 int trip, ret;
682 unsigned long temperature;
683
684 if (!tz->ops->get_trip_hyst)
685 return -EPERM;
686
687 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
688 return -EINVAL;
689
690 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
691
692 return ret ? ret : sprintf(buf, "%ld\n", temperature);
693}
694
695static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000696passive_store(struct device *dev, struct device_attribute *attr,
697 const char *buf, size_t count)
698{
699 struct thermal_zone_device *tz = to_thermal_zone(dev);
700 struct thermal_cooling_device *cdev = NULL;
701 int state;
702
703 if (!sscanf(buf, "%d\n", &state))
704 return -EINVAL;
705
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100706 /* sanity check: values below 1000 millicelcius don't make sense
707 * and can cause the system to go into a thermal heart attack
708 */
709 if (state && state < 1000)
710 return -EINVAL;
711
Matthew Garrett03a971a2008-12-03 18:00:38 +0000712 if (state && !tz->forced_passive) {
713 mutex_lock(&thermal_list_lock);
714 list_for_each_entry(cdev, &thermal_cdev_list, node) {
715 if (!strncmp("Processor", cdev->type,
716 sizeof("Processor")))
717 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800718 THERMAL_TRIPS_NONE, cdev,
719 THERMAL_NO_LIMIT,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000720 THERMAL_NO_LIMIT,
721 THERMAL_WEIGHT_DEFAULT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000722 }
723 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100724 if (!tz->passive_delay)
725 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000726 } else if (!state && tz->forced_passive) {
727 mutex_lock(&thermal_list_lock);
728 list_for_each_entry(cdev, &thermal_cdev_list, node) {
729 if (!strncmp("Processor", cdev->type,
730 sizeof("Processor")))
731 thermal_zone_unbind_cooling_device(tz,
732 THERMAL_TRIPS_NONE,
733 cdev);
734 }
735 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100736 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000737 }
738
Matthew Garrett03a971a2008-12-03 18:00:38 +0000739 tz->forced_passive = state;
740
741 thermal_zone_device_update(tz);
742
743 return count;
744}
745
746static ssize_t
747passive_show(struct device *dev, struct device_attribute *attr,
748 char *buf)
749{
750 struct thermal_zone_device *tz = to_thermal_zone(dev);
751
752 return sprintf(buf, "%d\n", tz->forced_passive);
753}
754
Durgadoss R5a2c0902012-09-18 11:04:58 +0530755static ssize_t
756policy_store(struct device *dev, struct device_attribute *attr,
757 const char *buf, size_t count)
758{
759 int ret = -EINVAL;
760 struct thermal_zone_device *tz = to_thermal_zone(dev);
761 struct thermal_governor *gov;
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000762 char name[THERMAL_NAME_LENGTH];
763
764 snprintf(name, sizeof(name), "%s", buf);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530765
766 mutex_lock(&thermal_governor_lock);
Javi Merinob6cc7722014-11-25 16:00:33 +0000767 mutex_lock(&tz->lock);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530768
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000769 gov = __find_governor(strim(name));
Durgadoss R5a2c0902012-09-18 11:04:58 +0530770 if (!gov)
771 goto exit;
772
773 tz->governor = gov;
774 ret = count;
775
776exit:
Javi Merinob6cc7722014-11-25 16:00:33 +0000777 mutex_unlock(&tz->lock);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530778 mutex_unlock(&thermal_governor_lock);
779 return ret;
780}
781
782static ssize_t
783policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
784{
785 struct thermal_zone_device *tz = to_thermal_zone(dev);
786
787 return sprintf(buf, "%s\n", tz->governor->name);
788}
789
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000790#ifdef CONFIG_THERMAL_EMULATION
791static ssize_t
792emul_temp_store(struct device *dev, struct device_attribute *attr,
793 const char *buf, size_t count)
794{
795 struct thermal_zone_device *tz = to_thermal_zone(dev);
796 int ret = 0;
797 unsigned long temperature;
798
799 if (kstrtoul(buf, 10, &temperature))
800 return -EINVAL;
801
802 if (!tz->ops->set_emul_temp) {
803 mutex_lock(&tz->lock);
804 tz->emul_temperature = temperature;
805 mutex_unlock(&tz->lock);
806 } else {
807 ret = tz->ops->set_emul_temp(tz, temperature);
808 }
809
lan,Tianyu800744b2014-01-02 15:47:54 +0800810 if (!ret)
811 thermal_zone_device_update(tz);
812
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000813 return ret ? ret : count;
814}
815static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
816#endif/*CONFIG_THERMAL_EMULATION*/
817
Zhang Rui203d3d42008-01-17 15:51:08 +0800818static DEVICE_ATTR(type, 0444, type_show, NULL);
819static DEVICE_ATTR(temp, 0444, temp_show, NULL);
820static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700821static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530822static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800823
Zhang Rui203d3d42008-01-17 15:51:08 +0800824/* sys I/F for cooling device */
825#define to_cooling_device(_dev) \
826 container_of(_dev, struct thermal_cooling_device, device)
827
828static ssize_t
829thermal_cooling_device_type_show(struct device *dev,
830 struct device_attribute *attr, char *buf)
831{
832 struct thermal_cooling_device *cdev = to_cooling_device(dev);
833
834 return sprintf(buf, "%s\n", cdev->type);
835}
836
837static ssize_t
838thermal_cooling_device_max_state_show(struct device *dev,
839 struct device_attribute *attr, char *buf)
840{
841 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000842 unsigned long state;
843 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800844
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000845 ret = cdev->ops->get_max_state(cdev, &state);
846 if (ret)
847 return ret;
848 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800849}
850
851static ssize_t
852thermal_cooling_device_cur_state_show(struct device *dev,
853 struct device_attribute *attr, char *buf)
854{
855 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000856 unsigned long state;
857 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800858
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000859 ret = cdev->ops->get_cur_state(cdev, &state);
860 if (ret)
861 return ret;
862 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800863}
864
865static ssize_t
866thermal_cooling_device_cur_state_store(struct device *dev,
867 struct device_attribute *attr,
868 const char *buf, size_t count)
869{
870 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000871 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800872 int result;
873
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000874 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800875 return -EINVAL;
876
Roel Kluinedb94912009-12-15 22:46:50 +0100877 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800878 return -EINVAL;
879
880 result = cdev->ops->set_cur_state(cdev, state);
881 if (result)
882 return result;
883 return count;
884}
885
886static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500887__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800888static DEVICE_ATTR(max_state, 0444,
889 thermal_cooling_device_max_state_show, NULL);
890static DEVICE_ATTR(cur_state, 0644,
891 thermal_cooling_device_cur_state_show,
892 thermal_cooling_device_cur_state_store);
893
894static ssize_t
895thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500896 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800897{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800898 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800899
900 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800901 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800902
903 if (instance->trip == THERMAL_TRIPS_NONE)
904 return sprintf(buf, "-1\n");
905 else
906 return sprintf(buf, "%d\n", instance->trip);
907}
908
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -0800909static struct attribute *cooling_device_attrs[] = {
910 &dev_attr_cdev_type.attr,
911 &dev_attr_max_state.attr,
912 &dev_attr_cur_state.attr,
913 NULL,
914};
915
916static const struct attribute_group cooling_device_attr_group = {
917 .attrs = cooling_device_attrs,
918};
919
920static const struct attribute_group *cooling_device_attr_groups[] = {
921 &cooling_device_attr_group,
922 NULL,
923};
924
Zhang Rui203d3d42008-01-17 15:51:08 +0800925/* Device management */
926
927/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000928 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
929 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +0800930 * @trip: indicates which trip point the cooling devices is
931 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000932 * @cdev: pointer to struct thermal_cooling_device
933 * @upper: the Maximum cooling state for this trip point.
934 * THERMAL_NO_LIMIT means no upper limit,
935 * and the cooling device can be in max_state.
936 * @lower: the Minimum cooling state can be used for this trip point.
937 * THERMAL_NO_LIMIT means no lower limit,
938 * and the cooling device can be in cooling state 0.
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000939 * @weight: The weight of the cooling device to be bound to the
940 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
941 * default value
Len Brown543a9562008-02-07 16:55:08 -0500942 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000943 * This interface function bind a thermal cooling device to the certain trip
944 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500945 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000946 *
947 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800948 */
949int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
950 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800951 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000952 unsigned long upper, unsigned long lower,
953 unsigned int weight)
Zhang Rui203d3d42008-01-17 15:51:08 +0800954{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800955 struct thermal_instance *dev;
956 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500957 struct thermal_zone_device *pos1;
958 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800959 unsigned long max_state;
Lukasz Majewski9a3031d2014-11-18 11:16:30 +0100960 int result, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800961
Len Brown543a9562008-02-07 16:55:08 -0500962 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800963 return -EINVAL;
964
Thomas Sujithc7516702008-02-15 00:58:50 -0500965 list_for_each_entry(pos1, &thermal_tz_list, node) {
966 if (pos1 == tz)
967 break;
968 }
969 list_for_each_entry(pos2, &thermal_cdev_list, node) {
970 if (pos2 == cdev)
971 break;
972 }
973
974 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800975 return -EINVAL;
976
Lukasz Majewski9a3031d2014-11-18 11:16:30 +0100977 ret = cdev->ops->get_max_state(cdev, &max_state);
978 if (ret)
979 return ret;
Zhang Rui9d998422012-06-26 16:35:57 +0800980
981 /* lower default 0, upper default max_state */
982 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
983 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
984
985 if (lower > upper || upper > max_state)
986 return -EINVAL;
987
Zhang Rui203d3d42008-01-17 15:51:08 +0800988 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800989 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800990 if (!dev)
991 return -ENOMEM;
992 dev->tz = tz;
993 dev->cdev = cdev;
994 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800995 dev->upper = upper;
996 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800997 dev->target = THERMAL_NO_TARGET;
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000998 dev->weight = weight;
Zhang Rui74051ba2012-06-26 16:27:22 +0800999
Zhang Rui203d3d42008-01-17 15:51:08 +08001000 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1001 if (result)
1002 goto free_mem;
1003
1004 sprintf(dev->name, "cdev%d", dev->id);
1005 result =
1006 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1007 if (result)
1008 goto release_idr;
1009
1010 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -07001011 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001012 dev->attr.attr.name = dev->attr_name;
1013 dev->attr.attr.mode = 0444;
1014 dev->attr.show = thermal_cooling_device_trip_point_show;
1015 result = device_create_file(&tz->device, &dev->attr);
1016 if (result)
1017 goto remove_symbol_link;
1018
1019 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001020 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001021 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +08001022 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1023 result = -EEXIST;
1024 break;
1025 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001026 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001027 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001028 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1029 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001030 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001031 mutex_unlock(&tz->lock);
1032
1033 if (!result)
1034 return 0;
1035
1036 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -07001037remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001038 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -07001039release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001040 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -07001041free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001042 kfree(dev);
1043 return result;
1044}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001045EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001046
1047/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001048 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1049 * thermal zone.
1050 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +08001051 * @trip: indicates which trip point the cooling devices is
1052 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001053 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -05001054 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001055 * This interface function unbind a thermal cooling device from the certain
1056 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001057 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001058 *
1059 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001060 */
1061int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1062 int trip,
1063 struct thermal_cooling_device *cdev)
1064{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001065 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001066
1067 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001068 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001069 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001070 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001071 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001072 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001073 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001074 mutex_unlock(&tz->lock);
1075 goto unbind;
1076 }
1077 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001078 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001079 mutex_unlock(&tz->lock);
1080
1081 return -ENODEV;
1082
Joe Perchescaca8b82012-03-21 12:55:02 -07001083unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +08001084 device_remove_file(&tz->device, &pos->attr);
1085 sysfs_remove_link(&tz->device.kobj, pos->name);
1086 release_idr(&tz->idr, &tz->lock, pos->id);
1087 kfree(pos);
1088 return 0;
1089}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001090EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001091
1092static void thermal_release(struct device *dev)
1093{
1094 struct thermal_zone_device *tz;
1095 struct thermal_cooling_device *cdev;
1096
Joe Perchescaca8b82012-03-21 12:55:02 -07001097 if (!strncmp(dev_name(dev), "thermal_zone",
1098 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001099 tz = to_thermal_zone(dev);
1100 kfree(tz);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +05301101 } else if(!strncmp(dev_name(dev), "cooling_device",
1102 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +08001103 cdev = to_cooling_device(dev);
1104 kfree(cdev);
1105 }
1106}
1107
1108static struct class thermal_class = {
1109 .name = "thermal",
1110 .dev_release = thermal_release,
1111};
1112
1113/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001114 * __thermal_cooling_device_register() - register a new thermal cooling device
1115 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +08001116 * @type: the thermal cooling device type.
1117 * @devdata: device private data.
1118 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001119 *
1120 * This interface function adds a new thermal cooling device (fan/processor/...)
1121 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1122 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001123 * It also gives the opportunity to link the cooling device to a device tree
1124 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001125 *
1126 * Return: a pointer to the created struct thermal_cooling_device or an
1127 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001128 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001129static struct thermal_cooling_device *
1130__thermal_cooling_device_register(struct device_node *np,
1131 char *type, void *devdata,
1132 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001133{
1134 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001135 int result;
1136
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001137 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001138 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001139
1140 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001141 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001142 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001143
1144 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1145 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001146 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001147
1148 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1149 if (result) {
1150 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001151 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001152 }
1153
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001154 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +08001155 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001156 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001157 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +08001158 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +08001159 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +08001160 cdev->device.class = &thermal_class;
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001161 cdev->device.groups = cooling_device_attr_groups;
Zhang Rui203d3d42008-01-17 15:51:08 +08001162 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001163 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001164 result = device_register(&cdev->device);
1165 if (result) {
1166 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1167 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001168 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001169 }
1170
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301171 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001172 mutex_lock(&thermal_list_lock);
1173 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001174 mutex_unlock(&thermal_list_lock);
1175
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301176 /* Update binding information for 'this' new cdev */
1177 bind_cdev(cdev);
1178
1179 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001180}
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001181
1182/**
1183 * thermal_cooling_device_register() - register a new thermal cooling device
1184 * @type: the thermal cooling device type.
1185 * @devdata: device private data.
1186 * @ops: standard thermal cooling devices callbacks.
1187 *
1188 * This interface function adds a new thermal cooling device (fan/processor/...)
1189 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1190 * to all the thermal zone devices registered at the same time.
1191 *
1192 * Return: a pointer to the created struct thermal_cooling_device or an
1193 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1194 */
1195struct thermal_cooling_device *
1196thermal_cooling_device_register(char *type, void *devdata,
1197 const struct thermal_cooling_device_ops *ops)
1198{
1199 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1200}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001201EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001202
1203/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001204 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1205 * @np: a pointer to a device tree node.
1206 * @type: the thermal cooling device type.
1207 * @devdata: device private data.
1208 * @ops: standard thermal cooling devices callbacks.
1209 *
1210 * This function will register a cooling device with device tree node reference.
1211 * This interface function adds a new thermal cooling device (fan/processor/...)
1212 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1213 * to all the thermal zone devices registered at the same time.
1214 *
1215 * Return: a pointer to the created struct thermal_cooling_device or an
1216 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1217 */
1218struct thermal_cooling_device *
1219thermal_of_cooling_device_register(struct device_node *np,
1220 char *type, void *devdata,
1221 const struct thermal_cooling_device_ops *ops)
1222{
1223 return __thermal_cooling_device_register(np, type, devdata, ops);
1224}
1225EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1226
1227/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001228 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001229 * @cdev: the thermal cooling device to remove.
1230 *
1231 * thermal_cooling_device_unregister() must be called when the device is no
1232 * longer needed.
1233 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301234void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001235{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301236 int i;
1237 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001238 struct thermal_zone_device *tz;
1239 struct thermal_cooling_device *pos = NULL;
1240
1241 if (!cdev)
1242 return;
1243
1244 mutex_lock(&thermal_list_lock);
1245 list_for_each_entry(pos, &thermal_cdev_list, node)
1246 if (pos == cdev)
1247 break;
1248 if (pos != cdev) {
1249 /* thermal cooling device not found */
1250 mutex_unlock(&thermal_list_lock);
1251 return;
1252 }
1253 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301254
1255 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001256 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301257 if (tz->ops->unbind) {
1258 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001259 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301260 }
1261
1262 if (!tz->tzp || !tz->tzp->tbp)
1263 continue;
1264
1265 tzp = tz->tzp;
1266 for (i = 0; i < tzp->num_tbps; i++) {
1267 if (tzp->tbp[i].cdev == cdev) {
1268 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1269 tzp->tbp[i].cdev = NULL;
1270 }
1271 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001272 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301273
Zhang Rui203d3d42008-01-17 15:51:08 +08001274 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301275
Zhang Rui203d3d42008-01-17 15:51:08 +08001276 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001277 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001278 device_remove_file(&cdev->device, &dev_attr_max_state);
1279 device_remove_file(&cdev->device, &dev_attr_cur_state);
1280
1281 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1282 device_unregister(&cdev->device);
1283 return;
1284}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001285EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001286
Durgadoss Rdc765482012-09-18 11:05:00 +05301287void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001288{
1289 struct thermal_instance *instance;
1290 unsigned long target = 0;
1291
1292 /* cooling device is updated*/
1293 if (cdev->updated)
1294 return;
1295
Zhang Ruif4a821c2012-07-24 16:56:21 +08001296 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001297 /* Make sure cdev enters the deepest cooling state */
1298 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
Aaron Lu06475b52013-12-02 13:54:26 +08001299 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1300 instance->tz->id, instance->target);
Zhang Ruice119f82012-06-27 14:13:04 +08001301 if (instance->target == THERMAL_NO_TARGET)
1302 continue;
1303 if (instance->target > target)
1304 target = instance->target;
1305 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001306 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001307 cdev->ops->set_cur_state(cdev, target);
1308 cdev->updated = true;
Punit Agrawal39811562014-07-29 11:50:49 +01001309 trace_cdev_update(cdev, target);
Aaron Lu06475b52013-12-02 13:54:26 +08001310 dev_dbg(&cdev->device, "set to state %lu\n", target);
Zhang Ruice119f82012-06-27 14:13:04 +08001311}
Durgadoss Rdc765482012-09-18 11:05:00 +05301312EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001313
Matthew Garrettb1569e92008-12-03 17:55:32 +00001314/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001315 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301316 * @tz: thermal zone device
1317 * @trip: indicates which trip point has been crossed
1318 *
1319 * This function handles the trip events from sensor drivers. It starts
1320 * throttling the cooling devices according to the policy configured.
1321 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1322 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1323 * The throttling policy is based on the configured platform data; if no
1324 * platform data is provided, this uses the step_wise throttling policy.
1325 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001326void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301327{
1328 handle_thermal_trip(tz, trip);
1329}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001330EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301331
1332/**
Eduardo Valentin269c1742013-04-23 21:48:19 +00001333 * create_trip_attrs() - create attributes for trip points
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001334 * @tz: the thermal zone device
1335 * @mask: Writeable trip point bitmap.
Eduardo Valentin269c1742013-04-23 21:48:19 +00001336 *
1337 * helper function to instantiate sysfs entries for every trip
1338 * point and its properties of a struct thermal_zone_device.
1339 *
1340 * Return: 0 on success, the proper error value otherwise.
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001341 */
1342static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1343{
1344 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001345 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001346
Durgadoss R27365a62012-07-25 10:10:59 +08001347 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001348 if (!tz->trip_type_attrs)
1349 return -ENOMEM;
1350
Durgadoss R27365a62012-07-25 10:10:59 +08001351 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001352 if (!tz->trip_temp_attrs) {
1353 kfree(tz->trip_type_attrs);
1354 return -ENOMEM;
1355 }
1356
Durgadoss R27365a62012-07-25 10:10:59 +08001357 if (tz->ops->get_trip_hyst) {
1358 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1359 if (!tz->trip_hyst_attrs) {
1360 kfree(tz->trip_type_attrs);
1361 kfree(tz->trip_temp_attrs);
1362 return -ENOMEM;
1363 }
1364 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001365
Durgadoss R27365a62012-07-25 10:10:59 +08001366
1367 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001368 /* create trip type attribute */
1369 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1370 "trip_point_%d_type", indx);
1371
1372 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1373 tz->trip_type_attrs[indx].attr.attr.name =
1374 tz->trip_type_attrs[indx].name;
1375 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1376 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1377
1378 device_create_file(&tz->device,
1379 &tz->trip_type_attrs[indx].attr);
1380
1381 /* create trip temp attribute */
1382 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1383 "trip_point_%d_temp", indx);
1384
1385 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1386 tz->trip_temp_attrs[indx].attr.attr.name =
1387 tz->trip_temp_attrs[indx].name;
1388 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1389 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1390 if (mask & (1 << indx)) {
1391 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1392 tz->trip_temp_attrs[indx].attr.store =
1393 trip_point_temp_store;
1394 }
1395
1396 device_create_file(&tz->device,
1397 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001398
1399 /* create Optional trip hyst attribute */
1400 if (!tz->ops->get_trip_hyst)
1401 continue;
1402 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1403 "trip_point_%d_hyst", indx);
1404
1405 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1406 tz->trip_hyst_attrs[indx].attr.attr.name =
1407 tz->trip_hyst_attrs[indx].name;
1408 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1409 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1410 if (tz->ops->set_trip_hyst) {
1411 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1412 tz->trip_hyst_attrs[indx].attr.store =
1413 trip_point_hyst_store;
1414 }
1415
1416 device_create_file(&tz->device,
1417 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001418 }
1419 return 0;
1420}
1421
1422static void remove_trip_attrs(struct thermal_zone_device *tz)
1423{
1424 int indx;
1425
1426 for (indx = 0; indx < tz->trips; indx++) {
1427 device_remove_file(&tz->device,
1428 &tz->trip_type_attrs[indx].attr);
1429 device_remove_file(&tz->device,
1430 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001431 if (tz->ops->get_trip_hyst)
1432 device_remove_file(&tz->device,
1433 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001434 }
1435 kfree(tz->trip_type_attrs);
1436 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001437 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001438}
1439
1440/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001441 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001442 * @type: the thermal zone device type
1443 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001444 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001445 * @devdata: private device data
1446 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301447 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001448 * @passive_delay: number of milliseconds to wait between polls when
1449 * performing passive cooling
1450 * @polling_delay: number of milliseconds to wait between polls when checking
1451 * whether trip points have been crossed (0 for interrupt
1452 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001453 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001454 * This interface function adds a new thermal zone device (sensor) to
1455 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1456 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001457 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001458 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001459 *
1460 * Return: a pointer to the created struct thermal_zone_device or an
1461 * in case of error, an ERR_PTR. Caller must check return value with
1462 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001463 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001464struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001465 int trips, int mask, void *devdata,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001466 struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301467 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001468 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001469{
1470 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001471 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001472 int result;
1473 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001474 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001475
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001476 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001477 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001478
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001479 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001480 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001481
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001482 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001483 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001484
Jonghwa Lee83720d02013-05-18 09:50:26 +00001485 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001486 return ERR_PTR(-EINVAL);
1487
Zhang Rui203d3d42008-01-17 15:51:08 +08001488 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1489 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001490 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001491
Zhang Rui2d374132012-06-27 10:09:00 +08001492 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001493 idr_init(&tz->idr);
1494 mutex_init(&tz->lock);
1495 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1496 if (result) {
1497 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001498 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001499 }
1500
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001501 strlcpy(tz->type, type ? : "", sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001502 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301503 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001504 tz->device.class = &thermal_class;
1505 tz->devdata = devdata;
1506 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001507 tz->passive_delay = passive_delay;
1508 tz->polling_delay = polling_delay;
1509
Kay Sievers354655e2009-01-06 10:44:37 -08001510 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001511 result = device_register(&tz->device);
1512 if (result) {
1513 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1514 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001515 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001516 }
1517
1518 /* sys I/F */
1519 if (type) {
1520 result = device_create_file(&tz->device, &dev_attr_type);
1521 if (result)
1522 goto unregister;
1523 }
1524
1525 result = device_create_file(&tz->device, &dev_attr_temp);
1526 if (result)
1527 goto unregister;
1528
1529 if (ops->get_mode) {
1530 result = device_create_file(&tz->device, &dev_attr_mode);
1531 if (result)
1532 goto unregister;
1533 }
1534
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001535 result = create_trip_attrs(tz, mask);
1536 if (result)
1537 goto unregister;
1538
Zhang Rui203d3d42008-01-17 15:51:08 +08001539 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001540 tz->ops->get_trip_type(tz, count, &trip_type);
1541 if (trip_type == THERMAL_TRIP_PASSIVE)
1542 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001543 }
1544
Durgadoss R5a2c0902012-09-18 11:04:58 +05301545 if (!passive) {
1546 result = device_create_file(&tz->device, &dev_attr_passive);
1547 if (result)
1548 goto unregister;
1549 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001550
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001551#ifdef CONFIG_THERMAL_EMULATION
1552 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1553 if (result)
1554 goto unregister;
1555#endif
Durgadoss R5a2c0902012-09-18 11:04:58 +05301556 /* Create policy attribute */
1557 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001558 if (result)
1559 goto unregister;
1560
Durgadoss Ra4a15482012-09-18 11:04:57 +05301561 /* Update 'this' zone's governor information */
1562 mutex_lock(&thermal_governor_lock);
1563
1564 if (tz->tzp)
1565 tz->governor = __find_governor(tz->tzp->governor_name);
1566 else
Zhang Ruif2234bc2014-01-24 10:23:19 +08001567 tz->governor = def_governor;
Durgadoss Ra4a15482012-09-18 11:04:57 +05301568
1569 mutex_unlock(&thermal_governor_lock);
1570
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04001571 if (!tz->tzp || !tz->tzp->no_hwmon) {
1572 result = thermal_add_hwmon_sysfs(tz);
1573 if (result)
1574 goto unregister;
1575 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08001576
Zhang Rui203d3d42008-01-17 15:51:08 +08001577 mutex_lock(&thermal_list_lock);
1578 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001579 mutex_unlock(&thermal_list_lock);
1580
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301581 /* Bind cooling devices for this zone */
1582 bind_tz(tz);
1583
Matthew Garrettb1569e92008-12-03 17:55:32 +00001584 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1585
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001586 if (!tz->ops->get_temp)
1587 thermal_zone_device_set_polling(tz, 0);
1588
Matthew Garrettb1569e92008-12-03 17:55:32 +00001589 thermal_zone_device_update(tz);
1590
Yao Dongdong14015862014-10-28 07:40:25 +00001591 return tz;
Zhang Rui203d3d42008-01-17 15:51:08 +08001592
Joe Perchescaca8b82012-03-21 12:55:02 -07001593unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001594 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1595 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001596 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001597}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001598EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001599
1600/**
1601 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001602 * @tz: the thermal zone device to remove
1603 */
1604void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1605{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301606 int i;
1607 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001608 struct thermal_cooling_device *cdev;
1609 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001610
1611 if (!tz)
1612 return;
1613
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301614 tzp = tz->tzp;
1615
Zhang Rui203d3d42008-01-17 15:51:08 +08001616 mutex_lock(&thermal_list_lock);
1617 list_for_each_entry(pos, &thermal_tz_list, node)
1618 if (pos == tz)
1619 break;
1620 if (pos != tz) {
1621 /* thermal zone device not found */
1622 mutex_unlock(&thermal_list_lock);
1623 return;
1624 }
1625 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301626
1627 /* Unbind all cdevs associated with 'this' thermal zone */
1628 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1629 if (tz->ops->unbind) {
1630 tz->ops->unbind(tz, cdev);
1631 continue;
1632 }
1633
1634 if (!tzp || !tzp->tbp)
1635 break;
1636
1637 for (i = 0; i < tzp->num_tbps; i++) {
1638 if (tzp->tbp[i].cdev == cdev) {
1639 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1640 tzp->tbp[i].cdev = NULL;
1641 }
1642 }
1643 }
1644
Zhang Rui203d3d42008-01-17 15:51:08 +08001645 mutex_unlock(&thermal_list_lock);
1646
Matthew Garrettb1569e92008-12-03 17:55:32 +00001647 thermal_zone_device_set_polling(tz, 0);
1648
Zhang Rui203d3d42008-01-17 15:51:08 +08001649 if (tz->type[0])
1650 device_remove_file(&tz->device, &dev_attr_type);
1651 device_remove_file(&tz->device, &dev_attr_temp);
1652 if (tz->ops->get_mode)
1653 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301654 device_remove_file(&tz->device, &dev_attr_policy);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001655 remove_trip_attrs(tz);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301656 tz->governor = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001657
Zhang Ruie68b16a2008-04-21 16:07:52 +08001658 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001659 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1660 idr_destroy(&tz->idr);
1661 mutex_destroy(&tz->lock);
1662 device_unregister(&tz->device);
1663 return;
1664}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001665EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001666
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001667/**
1668 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1669 * @name: thermal zone name to fetch the temperature
1670 *
1671 * When only one zone is found with the passed name, returns a reference to it.
1672 *
1673 * Return: On success returns a reference to an unique thermal zone with
1674 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1675 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1676 */
1677struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1678{
1679 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1680 unsigned int found = 0;
1681
1682 if (!name)
1683 goto exit;
1684
1685 mutex_lock(&thermal_list_lock);
1686 list_for_each_entry(pos, &thermal_tz_list, node)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -07001687 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001688 found++;
1689 ref = pos;
1690 }
1691 mutex_unlock(&thermal_list_lock);
1692
1693 /* nothing has been found, thus an error code for it */
1694 if (found == 0)
1695 ref = ERR_PTR(-ENODEV);
1696 else if (found > 1)
1697 /* Success only when an unique zone is found */
1698 ref = ERR_PTR(-EEXIST);
1699
1700exit:
1701 return ref;
1702}
1703EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1704
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001705#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01001706static const struct genl_multicast_group thermal_event_mcgrps[] = {
1707 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1708};
1709
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001710static struct genl_family thermal_event_genl_family = {
1711 .id = GENL_ID_GENERATE,
1712 .name = THERMAL_GENL_FAMILY_NAME,
1713 .version = THERMAL_GENL_VERSION,
1714 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001715 .mcgrps = thermal_event_mcgrps,
1716 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001717};
1718
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001719int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1720 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301721{
1722 struct sk_buff *skb;
1723 struct nlattr *attr;
1724 struct thermal_genl_event *thermal_event;
1725 void *msg_header;
1726 int size;
1727 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001728 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301729
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001730 if (!tz)
1731 return -EINVAL;
1732
R.Durgadoss4cb18722010-10-27 03:33:29 +05301733 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001734 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1735 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301736
1737 skb = genlmsg_new(size, GFP_ATOMIC);
1738 if (!skb)
1739 return -ENOMEM;
1740
1741 /* add the genetlink message header */
1742 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1743 &thermal_event_genl_family, 0,
1744 THERMAL_GENL_CMD_EVENT);
1745 if (!msg_header) {
1746 nlmsg_free(skb);
1747 return -ENOMEM;
1748 }
1749
1750 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001751 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1752 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301753
1754 if (!attr) {
1755 nlmsg_free(skb);
1756 return -EINVAL;
1757 }
1758
1759 thermal_event = nla_data(attr);
1760 if (!thermal_event) {
1761 nlmsg_free(skb);
1762 return -EINVAL;
1763 }
1764
1765 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1766
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001767 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301768 thermal_event->event = event;
1769
1770 /* send multicast genetlink message */
Johannes Berg053c0952015-01-16 22:09:00 +01001771 genlmsg_end(skb, msg_header);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301772
Johannes Berg68eb5502013-11-19 15:19:38 +01001773 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001774 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301775 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001776 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301777
1778 return result;
1779}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001780EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301781
1782static int genetlink_init(void)
1783{
Johannes Berg2a94fe42013-11-19 15:19:39 +01001784 return genl_register_family(&thermal_event_genl_family);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301785}
1786
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001787static void genetlink_exit(void)
1788{
1789 genl_unregister_family(&thermal_event_genl_family);
1790}
1791#else /* !CONFIG_NET */
1792static inline int genetlink_init(void) { return 0; }
1793static inline void genetlink_exit(void) {}
1794#endif /* !CONFIG_NET */
1795
Zhang Rui80a26a52013-03-26 16:38:29 +08001796static int __init thermal_register_governors(void)
1797{
1798 int result;
1799
1800 result = thermal_gov_step_wise_register();
1801 if (result)
1802 return result;
1803
1804 result = thermal_gov_fair_share_register();
1805 if (result)
1806 return result;
1807
Peter Feuerere4dbf982014-07-22 17:37:13 +02001808 result = thermal_gov_bang_bang_register();
1809 if (result)
1810 return result;
1811
Zhang Rui80a26a52013-03-26 16:38:29 +08001812 return thermal_gov_user_space_register();
1813}
1814
1815static void thermal_unregister_governors(void)
1816{
1817 thermal_gov_step_wise_unregister();
1818 thermal_gov_fair_share_unregister();
Peter Feuerere4dbf982014-07-22 17:37:13 +02001819 thermal_gov_bang_bang_unregister();
Zhang Rui80a26a52013-03-26 16:38:29 +08001820 thermal_gov_user_space_unregister();
1821}
1822
Zhang Rui203d3d42008-01-17 15:51:08 +08001823static int __init thermal_init(void)
1824{
Zhang Rui80a26a52013-03-26 16:38:29 +08001825 int result;
1826
1827 result = thermal_register_governors();
1828 if (result)
1829 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08001830
1831 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001832 if (result)
1833 goto unregister_governors;
1834
R.Durgadoss4cb18722010-10-27 03:33:29 +05301835 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08001836 if (result)
1837 goto unregister_class;
1838
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001839 result = of_parse_thermal_zones();
1840 if (result)
1841 goto exit_netlink;
1842
Zhang Rui80a26a52013-03-26 16:38:29 +08001843 return 0;
1844
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001845exit_netlink:
1846 genetlink_exit();
Zhang Rui80a26a52013-03-26 16:38:29 +08001847unregister_class:
1848 class_unregister(&thermal_class);
Luis Henriques9d367e52014-12-03 21:20:21 +00001849unregister_governors:
1850 thermal_unregister_governors();
Zhang Rui80a26a52013-03-26 16:38:29 +08001851error:
1852 idr_destroy(&thermal_tz_idr);
1853 idr_destroy(&thermal_cdev_idr);
1854 mutex_destroy(&thermal_idr_lock);
1855 mutex_destroy(&thermal_list_lock);
1856 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001857 return result;
1858}
1859
1860static void __exit thermal_exit(void)
1861{
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001862 of_thermal_destroy_zones();
Zhang Rui80a26a52013-03-26 16:38:29 +08001863 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001864 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001865 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08001866 idr_destroy(&thermal_tz_idr);
1867 idr_destroy(&thermal_cdev_idr);
1868 mutex_destroy(&thermal_idr_lock);
1869 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08001870 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001871}
1872
R.Durgadoss4cb18722010-10-27 03:33:29 +05301873fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001874module_exit(thermal_exit);