blob: 71b0ec0c370d73aad18d685118c197735c5b2833 [file] [log] [blame]
Zhang Rui203d3d42008-01-17 15:51:08 +08001/*
2 * thermal.c - Generic Thermal Management Sysfs support.
3 *
4 * Copyright (C) 2008 Intel Corp
5 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
6 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
7 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
25
Joe Perchesc5a01dd2012-03-21 12:55:02 -070026#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
Zhang Rui203d3d42008-01-17 15:51:08 +080028#include <linux/module.h>
29#include <linux/device.h>
30#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080032#include <linux/kdev_t.h>
33#include <linux/idr.h>
34#include <linux/thermal.h>
Matthew Garrettb1569e92008-12-03 17:55:32 +000035#include <linux/reboot.h>
Andy Shevchenko42a5bf52013-05-17 11:52:02 +000036#include <linux/string.h>
Eduardo Valentina116b5d2013-09-26 15:55:01 -040037#include <linux/of.h>
R.Durgadoss4cb18722010-10-27 03:33:29 +053038#include <net/netlink.h>
39#include <net/genetlink.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080040
Durgadoss R71350db2012-09-18 11:04:53 +053041#include "thermal_core.h"
Eduardo Valentin0dd88792013-07-03 15:14:28 -040042#include "thermal_hwmon.h"
Durgadoss R71350db2012-09-18 11:04:53 +053043
Zhang Rui63c4ec92008-04-21 16:07:13 +080044MODULE_AUTHOR("Zhang Rui");
Zhang Rui203d3d42008-01-17 15:51:08 +080045MODULE_DESCRIPTION("Generic thermal management sysfs support");
Eduardo Valentin6d8d4972013-04-23 21:48:13 +000046MODULE_LICENSE("GPL v2");
Zhang Rui203d3d42008-01-17 15:51:08 +080047
Zhang Rui203d3d42008-01-17 15:51:08 +080048static DEFINE_IDR(thermal_tz_idr);
49static DEFINE_IDR(thermal_cdev_idr);
50static DEFINE_MUTEX(thermal_idr_lock);
51
52static LIST_HEAD(thermal_tz_list);
53static LIST_HEAD(thermal_cdev_list);
Durgadoss Ra4a15482012-09-18 11:04:57 +053054static LIST_HEAD(thermal_governor_list);
55
Zhang Rui203d3d42008-01-17 15:51:08 +080056static DEFINE_MUTEX(thermal_list_lock);
Durgadoss Ra4a15482012-09-18 11:04:57 +053057static DEFINE_MUTEX(thermal_governor_lock);
58
Zhang Ruif2234bc2014-01-24 10:23:19 +080059static struct thermal_governor *def_governor;
60
Durgadoss Ra4a15482012-09-18 11:04:57 +053061static struct thermal_governor *__find_governor(const char *name)
62{
63 struct thermal_governor *pos;
64
Zhang Ruif2234bc2014-01-24 10:23:19 +080065 if (!name || !name[0])
66 return def_governor;
67
Durgadoss Ra4a15482012-09-18 11:04:57 +053068 list_for_each_entry(pos, &thermal_governor_list, governor_list)
69 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
70 return pos;
71
72 return NULL;
73}
74
75int thermal_register_governor(struct thermal_governor *governor)
76{
77 int err;
78 const char *name;
79 struct thermal_zone_device *pos;
80
81 if (!governor)
82 return -EINVAL;
83
84 mutex_lock(&thermal_governor_lock);
85
86 err = -EBUSY;
87 if (__find_governor(governor->name) == NULL) {
88 err = 0;
89 list_add(&governor->governor_list, &thermal_governor_list);
Zhang Ruif2234bc2014-01-24 10:23:19 +080090 if (!def_governor && !strncmp(governor->name,
91 DEFAULT_THERMAL_GOVERNOR, THERMAL_NAME_LENGTH))
92 def_governor = governor;
Durgadoss Ra4a15482012-09-18 11:04:57 +053093 }
94
95 mutex_lock(&thermal_list_lock);
96
97 list_for_each_entry(pos, &thermal_tz_list, node) {
Zhang Ruif2234bc2014-01-24 10:23:19 +080098 /*
99 * only thermal zones with specified tz->tzp->governor_name
100 * may run with tz->govenor unset
101 */
Durgadoss Ra4a15482012-09-18 11:04:57 +0530102 if (pos->governor)
103 continue;
Zhang Ruif2234bc2014-01-24 10:23:19 +0800104
105 name = pos->tzp->governor_name;
106
Durgadoss Ra4a15482012-09-18 11:04:57 +0530107 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
108 pos->governor = governor;
109 }
110
111 mutex_unlock(&thermal_list_lock);
112 mutex_unlock(&thermal_governor_lock);
113
114 return err;
115}
Durgadoss Ra4a15482012-09-18 11:04:57 +0530116
117void thermal_unregister_governor(struct thermal_governor *governor)
118{
119 struct thermal_zone_device *pos;
120
121 if (!governor)
122 return;
123
124 mutex_lock(&thermal_governor_lock);
125
126 if (__find_governor(governor->name) == NULL)
127 goto exit;
128
129 mutex_lock(&thermal_list_lock);
130
131 list_for_each_entry(pos, &thermal_tz_list, node) {
132 if (!strnicmp(pos->governor->name, governor->name,
133 THERMAL_NAME_LENGTH))
134 pos->governor = NULL;
135 }
136
137 mutex_unlock(&thermal_list_lock);
138 list_del(&governor->governor_list);
139exit:
140 mutex_unlock(&thermal_governor_lock);
141 return;
142}
Zhang Rui203d3d42008-01-17 15:51:08 +0800143
144static int get_idr(struct idr *idr, struct mutex *lock, int *id)
145{
Tejun Heo6deb69f2013-02-27 17:04:46 -0800146 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800147
148 if (lock)
149 mutex_lock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800150 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800151 if (lock)
152 mutex_unlock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800153 if (unlikely(ret < 0))
154 return ret;
155 *id = ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800156 return 0;
157}
158
159static void release_idr(struct idr *idr, struct mutex *lock, int id)
160{
161 if (lock)
162 mutex_lock(lock);
163 idr_remove(idr, id);
164 if (lock)
165 mutex_unlock(lock);
166}
167
Durgadoss R9b4298a2012-09-18 11:04:54 +0530168int get_tz_trend(struct thermal_zone_device *tz, int trip)
169{
170 enum thermal_trend trend;
171
Eduardo Valentin0c872502013-05-29 21:37:00 +0000172 if (tz->emul_temperature || !tz->ops->get_trend ||
173 tz->ops->get_trend(tz, trip, &trend)) {
Durgadoss R9b4298a2012-09-18 11:04:54 +0530174 if (tz->temperature > tz->last_temperature)
175 trend = THERMAL_TREND_RAISING;
176 else if (tz->temperature < tz->last_temperature)
177 trend = THERMAL_TREND_DROPPING;
178 else
179 trend = THERMAL_TREND_STABLE;
180 }
181
182 return trend;
183}
184EXPORT_SYMBOL(get_tz_trend);
185
186struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
187 struct thermal_cooling_device *cdev, int trip)
188{
189 struct thermal_instance *pos = NULL;
190 struct thermal_instance *target_instance = NULL;
191
192 mutex_lock(&tz->lock);
193 mutex_lock(&cdev->lock);
194
195 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
196 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
197 target_instance = pos;
198 break;
199 }
200 }
201
202 mutex_unlock(&cdev->lock);
203 mutex_unlock(&tz->lock);
204
205 return target_instance;
206}
207EXPORT_SYMBOL(get_thermal_instance);
208
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530209static void print_bind_err_msg(struct thermal_zone_device *tz,
210 struct thermal_cooling_device *cdev, int ret)
211{
212 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
213 tz->type, cdev->type, ret);
214}
215
216static void __bind(struct thermal_zone_device *tz, int mask,
Eduardo Valentina8892d832013-07-16 15:26:28 -0400217 struct thermal_cooling_device *cdev,
218 unsigned long *limits)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530219{
220 int i, ret;
221
222 for (i = 0; i < tz->trips; i++) {
223 if (mask & (1 << i)) {
Eduardo Valentina8892d832013-07-16 15:26:28 -0400224 unsigned long upper, lower;
225
226 upper = THERMAL_NO_LIMIT;
227 lower = THERMAL_NO_LIMIT;
228 if (limits) {
229 lower = limits[i * 2];
230 upper = limits[i * 2 + 1];
231 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530232 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
Eduardo Valentina8892d832013-07-16 15:26:28 -0400233 upper, lower);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530234 if (ret)
235 print_bind_err_msg(tz, cdev, ret);
236 }
237 }
238}
239
240static void __unbind(struct thermal_zone_device *tz, int mask,
241 struct thermal_cooling_device *cdev)
242{
243 int i;
244
245 for (i = 0; i < tz->trips; i++)
246 if (mask & (1 << i))
247 thermal_zone_unbind_cooling_device(tz, i, cdev);
248}
249
250static void bind_cdev(struct thermal_cooling_device *cdev)
251{
252 int i, ret;
253 const struct thermal_zone_params *tzp;
254 struct thermal_zone_device *pos = NULL;
255
256 mutex_lock(&thermal_list_lock);
257
258 list_for_each_entry(pos, &thermal_tz_list, node) {
259 if (!pos->tzp && !pos->ops->bind)
260 continue;
261
Ni Wadea9f2d192013-11-06 14:30:13 +0800262 if (pos->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530263 ret = pos->ops->bind(pos, cdev);
264 if (ret)
265 print_bind_err_msg(pos, cdev, ret);
Ni Wadea9f2d192013-11-06 14:30:13 +0800266 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530267 }
268
269 tzp = pos->tzp;
Hugh Dickins791700c2012-09-27 16:48:28 -0700270 if (!tzp || !tzp->tbp)
271 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530272
273 for (i = 0; i < tzp->num_tbps; i++) {
274 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
275 continue;
276 if (tzp->tbp[i].match(pos, cdev))
277 continue;
278 tzp->tbp[i].cdev = cdev;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400279 __bind(pos, tzp->tbp[i].trip_mask, cdev,
280 tzp->tbp[i].binding_limits);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530281 }
282 }
283
284 mutex_unlock(&thermal_list_lock);
285}
286
287static void bind_tz(struct thermal_zone_device *tz)
288{
289 int i, ret;
290 struct thermal_cooling_device *pos = NULL;
291 const struct thermal_zone_params *tzp = tz->tzp;
292
293 if (!tzp && !tz->ops->bind)
294 return;
295
296 mutex_lock(&thermal_list_lock);
297
Ni Wadea9f2d192013-11-06 14:30:13 +0800298 /* If there is ops->bind, try to use ops->bind */
299 if (tz->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530300 list_for_each_entry(pos, &thermal_cdev_list, node) {
301 ret = tz->ops->bind(tz, pos);
302 if (ret)
303 print_bind_err_msg(tz, pos, ret);
304 }
305 goto exit;
306 }
307
Hugh Dickins791700c2012-09-27 16:48:28 -0700308 if (!tzp || !tzp->tbp)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530309 goto exit;
310
311 list_for_each_entry(pos, &thermal_cdev_list, node) {
312 for (i = 0; i < tzp->num_tbps; i++) {
313 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
314 continue;
315 if (tzp->tbp[i].match(tz, pos))
316 continue;
317 tzp->tbp[i].cdev = pos;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400318 __bind(tz, tzp->tbp[i].trip_mask, pos,
319 tzp->tbp[i].binding_limits);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530320 }
321 }
322exit:
323 mutex_unlock(&thermal_list_lock);
324}
325
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530326static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
327 int delay)
328{
329 if (delay > 1000)
330 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
331 round_jiffies(msecs_to_jiffies(delay)));
332 else if (delay)
333 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
334 msecs_to_jiffies(delay));
335 else
336 cancel_delayed_work(&tz->poll_queue);
337}
338
339static void monitor_thermal_zone(struct thermal_zone_device *tz)
340{
341 mutex_lock(&tz->lock);
342
343 if (tz->passive)
344 thermal_zone_device_set_polling(tz, tz->passive_delay);
345 else if (tz->polling_delay)
346 thermal_zone_device_set_polling(tz, tz->polling_delay);
347 else
348 thermal_zone_device_set_polling(tz, 0);
349
350 mutex_unlock(&tz->lock);
351}
352
353static void handle_non_critical_trips(struct thermal_zone_device *tz,
354 int trip, enum thermal_trip_type trip_type)
355{
Zhang Ruif2234bc2014-01-24 10:23:19 +0800356 tz->governor ? tz->governor->throttle(tz, trip) :
357 def_governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530358}
359
360static void handle_critical_trips(struct thermal_zone_device *tz,
361 int trip, enum thermal_trip_type trip_type)
362{
363 long trip_temp;
364
365 tz->ops->get_trip_temp(tz, trip, &trip_temp);
366
367 /* If we have not crossed the trip_temp, we do not care. */
368 if (tz->temperature < trip_temp)
369 return;
370
371 if (tz->ops->notify)
372 tz->ops->notify(tz, trip, trip_type);
373
374 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000375 dev_emerg(&tz->device,
376 "critical temperature reached(%d C),shutting down\n",
377 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530378 orderly_poweroff(true);
379 }
380}
381
382static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
383{
384 enum thermal_trip_type type;
385
386 tz->ops->get_trip_type(tz, trip, &type);
387
388 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
389 handle_critical_trips(tz, trip, type);
390 else
391 handle_non_critical_trips(tz, trip, type);
392 /*
393 * Alright, we handled this trip successfully.
394 * So, start monitoring again.
395 */
396 monitor_thermal_zone(tz);
397}
398
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000399/**
400 * thermal_zone_get_temp() - returns its the temperature of thermal zone
401 * @tz: a valid pointer to a struct thermal_zone_device
402 * @temp: a valid pointer to where to store the resulting temperature.
403 *
404 * When a valid thermal zone reference is passed, it will fetch its
405 * temperature and fill @temp.
406 *
407 * Return: On success returns 0, an error code otherwise
408 */
409int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000410{
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000411 int ret = -EINVAL;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800412#ifdef CONFIG_THERMAL_EMULATION
413 int count;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000414 unsigned long crit_temp = -1UL;
415 enum thermal_trip_type type;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800416#endif
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000417
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400418 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000419 goto exit;
420
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000421 mutex_lock(&tz->lock);
422
423 ret = tz->ops->get_temp(tz, temp);
424#ifdef CONFIG_THERMAL_EMULATION
425 if (!tz->emul_temperature)
426 goto skip_emul;
427
428 for (count = 0; count < tz->trips; count++) {
429 ret = tz->ops->get_trip_type(tz, count, &type);
430 if (!ret && type == THERMAL_TRIP_CRITICAL) {
431 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
432 break;
433 }
434 }
435
436 if (ret)
437 goto skip_emul;
438
439 if (*temp < crit_temp)
440 *temp = tz->emul_temperature;
441skip_emul:
442#endif
443 mutex_unlock(&tz->lock);
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000444exit:
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000445 return ret;
446}
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000447EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000448
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530449static void update_temperature(struct thermal_zone_device *tz)
450{
451 long temp;
452 int ret;
453
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000454 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530455 if (ret) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000456 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
457 tz->id);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000458 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530459 }
460
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000461 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530462 tz->last_temperature = tz->temperature;
463 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530464 mutex_unlock(&tz->lock);
Aaron Lu06475b52013-12-02 13:54:26 +0800465
466 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
467 tz->last_temperature, tz->temperature);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530468}
469
470void thermal_zone_device_update(struct thermal_zone_device *tz)
471{
472 int count;
473
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400474 if (!tz->ops->get_temp)
475 return;
476
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530477 update_temperature(tz);
478
479 for (count = 0; count < tz->trips; count++)
480 handle_thermal_trip(tz, count);
481}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000482EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530483
484static void thermal_zone_device_check(struct work_struct *work)
485{
486 struct thermal_zone_device *tz = container_of(work, struct
487 thermal_zone_device,
488 poll_queue.work);
489 thermal_zone_device_update(tz);
490}
491
Zhang Rui203d3d42008-01-17 15:51:08 +0800492/* sys I/F for thermal zone */
493
494#define to_thermal_zone(_dev) \
495 container_of(_dev, struct thermal_zone_device, device)
496
497static ssize_t
498type_show(struct device *dev, struct device_attribute *attr, char *buf)
499{
500 struct thermal_zone_device *tz = to_thermal_zone(dev);
501
502 return sprintf(buf, "%s\n", tz->type);
503}
504
505static ssize_t
506temp_show(struct device *dev, struct device_attribute *attr, char *buf)
507{
508 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000509 long temperature;
510 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800511
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000512 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000513
514 if (ret)
515 return ret;
516
517 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800518}
519
520static ssize_t
521mode_show(struct device *dev, struct device_attribute *attr, char *buf)
522{
523 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000524 enum thermal_device_mode mode;
525 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800526
527 if (!tz->ops->get_mode)
528 return -EPERM;
529
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000530 result = tz->ops->get_mode(tz, &mode);
531 if (result)
532 return result;
533
534 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
535 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800536}
537
538static ssize_t
539mode_store(struct device *dev, struct device_attribute *attr,
540 const char *buf, size_t count)
541{
542 struct thermal_zone_device *tz = to_thermal_zone(dev);
543 int result;
544
545 if (!tz->ops->set_mode)
546 return -EPERM;
547
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530548 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000549 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530550 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000551 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
552 else
553 result = -EINVAL;
554
Zhang Rui203d3d42008-01-17 15:51:08 +0800555 if (result)
556 return result;
557
558 return count;
559}
560
561static ssize_t
562trip_point_type_show(struct device *dev, struct device_attribute *attr,
563 char *buf)
564{
565 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000566 enum thermal_trip_type type;
567 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800568
569 if (!tz->ops->get_trip_type)
570 return -EPERM;
571
572 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
573 return -EINVAL;
574
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000575 result = tz->ops->get_trip_type(tz, trip, &type);
576 if (result)
577 return result;
578
579 switch (type) {
580 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300581 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000582 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300583 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000584 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300585 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000586 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300587 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000588 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300589 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000590 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800591}
592
593static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800594trip_point_temp_store(struct device *dev, struct device_attribute *attr,
595 const char *buf, size_t count)
596{
597 struct thermal_zone_device *tz = to_thermal_zone(dev);
598 int trip, ret;
599 unsigned long temperature;
600
601 if (!tz->ops->set_trip_temp)
602 return -EPERM;
603
604 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
605 return -EINVAL;
606
607 if (kstrtoul(buf, 10, &temperature))
608 return -EINVAL;
609
610 ret = tz->ops->set_trip_temp(tz, trip, temperature);
611
612 return ret ? ret : count;
613}
614
615static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800616trip_point_temp_show(struct device *dev, struct device_attribute *attr,
617 char *buf)
618{
619 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000620 int trip, ret;
621 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800622
623 if (!tz->ops->get_trip_temp)
624 return -EPERM;
625
626 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
627 return -EINVAL;
628
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000629 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
630
631 if (ret)
632 return ret;
633
634 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800635}
636
Matthew Garrett03a971a2008-12-03 18:00:38 +0000637static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800638trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
639 const char *buf, size_t count)
640{
641 struct thermal_zone_device *tz = to_thermal_zone(dev);
642 int trip, ret;
643 unsigned long temperature;
644
645 if (!tz->ops->set_trip_hyst)
646 return -EPERM;
647
648 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
649 return -EINVAL;
650
651 if (kstrtoul(buf, 10, &temperature))
652 return -EINVAL;
653
654 /*
655 * We are not doing any check on the 'temperature' value
656 * here. The driver implementing 'set_trip_hyst' has to
657 * take care of this.
658 */
659 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
660
661 return ret ? ret : count;
662}
663
664static ssize_t
665trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
666 char *buf)
667{
668 struct thermal_zone_device *tz = to_thermal_zone(dev);
669 int trip, ret;
670 unsigned long temperature;
671
672 if (!tz->ops->get_trip_hyst)
673 return -EPERM;
674
675 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
676 return -EINVAL;
677
678 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
679
680 return ret ? ret : sprintf(buf, "%ld\n", temperature);
681}
682
683static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000684passive_store(struct device *dev, struct device_attribute *attr,
685 const char *buf, size_t count)
686{
687 struct thermal_zone_device *tz = to_thermal_zone(dev);
688 struct thermal_cooling_device *cdev = NULL;
689 int state;
690
691 if (!sscanf(buf, "%d\n", &state))
692 return -EINVAL;
693
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100694 /* sanity check: values below 1000 millicelcius don't make sense
695 * and can cause the system to go into a thermal heart attack
696 */
697 if (state && state < 1000)
698 return -EINVAL;
699
Matthew Garrett03a971a2008-12-03 18:00:38 +0000700 if (state && !tz->forced_passive) {
701 mutex_lock(&thermal_list_lock);
702 list_for_each_entry(cdev, &thermal_cdev_list, node) {
703 if (!strncmp("Processor", cdev->type,
704 sizeof("Processor")))
705 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800706 THERMAL_TRIPS_NONE, cdev,
707 THERMAL_NO_LIMIT,
708 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000709 }
710 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100711 if (!tz->passive_delay)
712 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000713 } else if (!state && tz->forced_passive) {
714 mutex_lock(&thermal_list_lock);
715 list_for_each_entry(cdev, &thermal_cdev_list, node) {
716 if (!strncmp("Processor", cdev->type,
717 sizeof("Processor")))
718 thermal_zone_unbind_cooling_device(tz,
719 THERMAL_TRIPS_NONE,
720 cdev);
721 }
722 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100723 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000724 }
725
Matthew Garrett03a971a2008-12-03 18:00:38 +0000726 tz->forced_passive = state;
727
728 thermal_zone_device_update(tz);
729
730 return count;
731}
732
733static ssize_t
734passive_show(struct device *dev, struct device_attribute *attr,
735 char *buf)
736{
737 struct thermal_zone_device *tz = to_thermal_zone(dev);
738
739 return sprintf(buf, "%d\n", tz->forced_passive);
740}
741
Durgadoss R5a2c0902012-09-18 11:04:58 +0530742static ssize_t
743policy_store(struct device *dev, struct device_attribute *attr,
744 const char *buf, size_t count)
745{
746 int ret = -EINVAL;
747 struct thermal_zone_device *tz = to_thermal_zone(dev);
748 struct thermal_governor *gov;
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000749 char name[THERMAL_NAME_LENGTH];
750
751 snprintf(name, sizeof(name), "%s", buf);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530752
753 mutex_lock(&thermal_governor_lock);
754
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000755 gov = __find_governor(strim(name));
Durgadoss R5a2c0902012-09-18 11:04:58 +0530756 if (!gov)
757 goto exit;
758
759 tz->governor = gov;
760 ret = count;
761
762exit:
763 mutex_unlock(&thermal_governor_lock);
764 return ret;
765}
766
767static ssize_t
768policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
769{
770 struct thermal_zone_device *tz = to_thermal_zone(dev);
771
772 return sprintf(buf, "%s\n", tz->governor->name);
773}
774
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000775#ifdef CONFIG_THERMAL_EMULATION
776static ssize_t
777emul_temp_store(struct device *dev, struct device_attribute *attr,
778 const char *buf, size_t count)
779{
780 struct thermal_zone_device *tz = to_thermal_zone(dev);
781 int ret = 0;
782 unsigned long temperature;
783
784 if (kstrtoul(buf, 10, &temperature))
785 return -EINVAL;
786
787 if (!tz->ops->set_emul_temp) {
788 mutex_lock(&tz->lock);
789 tz->emul_temperature = temperature;
790 mutex_unlock(&tz->lock);
791 } else {
792 ret = tz->ops->set_emul_temp(tz, temperature);
793 }
794
lan,Tianyu800744b2014-01-02 15:47:54 +0800795 if (!ret)
796 thermal_zone_device_update(tz);
797
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000798 return ret ? ret : count;
799}
800static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
801#endif/*CONFIG_THERMAL_EMULATION*/
802
Zhang Rui203d3d42008-01-17 15:51:08 +0800803static DEVICE_ATTR(type, 0444, type_show, NULL);
804static DEVICE_ATTR(temp, 0444, temp_show, NULL);
805static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700806static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530807static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800808
Zhang Rui203d3d42008-01-17 15:51:08 +0800809/* sys I/F for cooling device */
810#define to_cooling_device(_dev) \
811 container_of(_dev, struct thermal_cooling_device, device)
812
813static ssize_t
814thermal_cooling_device_type_show(struct device *dev,
815 struct device_attribute *attr, char *buf)
816{
817 struct thermal_cooling_device *cdev = to_cooling_device(dev);
818
819 return sprintf(buf, "%s\n", cdev->type);
820}
821
822static ssize_t
823thermal_cooling_device_max_state_show(struct device *dev,
824 struct device_attribute *attr, char *buf)
825{
826 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000827 unsigned long state;
828 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800829
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000830 ret = cdev->ops->get_max_state(cdev, &state);
831 if (ret)
832 return ret;
833 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800834}
835
836static ssize_t
837thermal_cooling_device_cur_state_show(struct device *dev,
838 struct device_attribute *attr, char *buf)
839{
840 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000841 unsigned long state;
842 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800843
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000844 ret = cdev->ops->get_cur_state(cdev, &state);
845 if (ret)
846 return ret;
847 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800848}
849
850static ssize_t
851thermal_cooling_device_cur_state_store(struct device *dev,
852 struct device_attribute *attr,
853 const char *buf, size_t count)
854{
855 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000856 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800857 int result;
858
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000859 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800860 return -EINVAL;
861
Roel Kluinedb94912009-12-15 22:46:50 +0100862 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800863 return -EINVAL;
864
865 result = cdev->ops->set_cur_state(cdev, state);
866 if (result)
867 return result;
868 return count;
869}
870
871static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500872__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800873static DEVICE_ATTR(max_state, 0444,
874 thermal_cooling_device_max_state_show, NULL);
875static DEVICE_ATTR(cur_state, 0644,
876 thermal_cooling_device_cur_state_show,
877 thermal_cooling_device_cur_state_store);
878
879static ssize_t
880thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500881 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800882{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800883 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800884
885 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800886 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800887
888 if (instance->trip == THERMAL_TRIPS_NONE)
889 return sprintf(buf, "-1\n");
890 else
891 return sprintf(buf, "%d\n", instance->trip);
892}
893
894/* Device management */
895
896/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000897 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
898 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +0800899 * @trip: indicates which trip point the cooling devices is
900 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000901 * @cdev: pointer to struct thermal_cooling_device
902 * @upper: the Maximum cooling state for this trip point.
903 * THERMAL_NO_LIMIT means no upper limit,
904 * and the cooling device can be in max_state.
905 * @lower: the Minimum cooling state can be used for this trip point.
906 * THERMAL_NO_LIMIT means no lower limit,
907 * and the cooling device can be in cooling state 0.
Len Brown543a9562008-02-07 16:55:08 -0500908 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000909 * This interface function bind a thermal cooling device to the certain trip
910 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500911 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000912 *
913 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800914 */
915int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
916 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800917 struct thermal_cooling_device *cdev,
918 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +0800919{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800920 struct thermal_instance *dev;
921 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500922 struct thermal_zone_device *pos1;
923 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800924 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800925 int result;
926
Len Brown543a9562008-02-07 16:55:08 -0500927 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800928 return -EINVAL;
929
Thomas Sujithc7516702008-02-15 00:58:50 -0500930 list_for_each_entry(pos1, &thermal_tz_list, node) {
931 if (pos1 == tz)
932 break;
933 }
934 list_for_each_entry(pos2, &thermal_cdev_list, node) {
935 if (pos2 == cdev)
936 break;
937 }
938
939 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800940 return -EINVAL;
941
Zhang Rui9d998422012-06-26 16:35:57 +0800942 cdev->ops->get_max_state(cdev, &max_state);
943
944 /* lower default 0, upper default max_state */
945 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
946 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
947
948 if (lower > upper || upper > max_state)
949 return -EINVAL;
950
Zhang Rui203d3d42008-01-17 15:51:08 +0800951 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800952 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800953 if (!dev)
954 return -ENOMEM;
955 dev->tz = tz;
956 dev->cdev = cdev;
957 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800958 dev->upper = upper;
959 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800960 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +0800961
Zhang Rui203d3d42008-01-17 15:51:08 +0800962 result = get_idr(&tz->idr, &tz->lock, &dev->id);
963 if (result)
964 goto free_mem;
965
966 sprintf(dev->name, "cdev%d", dev->id);
967 result =
968 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
969 if (result)
970 goto release_idr;
971
972 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700973 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800974 dev->attr.attr.name = dev->attr_name;
975 dev->attr.attr.mode = 0444;
976 dev->attr.show = thermal_cooling_device_trip_point_show;
977 result = device_create_file(&tz->device, &dev->attr);
978 if (result)
979 goto remove_symbol_link;
980
981 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800982 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800983 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +0800984 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
985 result = -EEXIST;
986 break;
987 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800988 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800989 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800990 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
991 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800992 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800993 mutex_unlock(&tz->lock);
994
995 if (!result)
996 return 0;
997
998 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -0700999remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001000 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -07001001release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001002 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -07001003free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001004 kfree(dev);
1005 return result;
1006}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001007EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001008
1009/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001010 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1011 * thermal zone.
1012 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +08001013 * @trip: indicates which trip point the cooling devices is
1014 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001015 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -05001016 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001017 * This interface function unbind a thermal cooling device from the certain
1018 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001019 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001020 *
1021 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001022 */
1023int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1024 int trip,
1025 struct thermal_cooling_device *cdev)
1026{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001027 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001028
1029 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001030 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001031 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001032 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001033 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001034 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001035 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001036 mutex_unlock(&tz->lock);
1037 goto unbind;
1038 }
1039 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001040 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001041 mutex_unlock(&tz->lock);
1042
1043 return -ENODEV;
1044
Joe Perchescaca8b82012-03-21 12:55:02 -07001045unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +08001046 device_remove_file(&tz->device, &pos->attr);
1047 sysfs_remove_link(&tz->device.kobj, pos->name);
1048 release_idr(&tz->idr, &tz->lock, pos->id);
1049 kfree(pos);
1050 return 0;
1051}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001052EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001053
1054static void thermal_release(struct device *dev)
1055{
1056 struct thermal_zone_device *tz;
1057 struct thermal_cooling_device *cdev;
1058
Joe Perchescaca8b82012-03-21 12:55:02 -07001059 if (!strncmp(dev_name(dev), "thermal_zone",
1060 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001061 tz = to_thermal_zone(dev);
1062 kfree(tz);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +05301063 } else if(!strncmp(dev_name(dev), "cooling_device",
1064 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +08001065 cdev = to_cooling_device(dev);
1066 kfree(cdev);
1067 }
1068}
1069
1070static struct class thermal_class = {
1071 .name = "thermal",
1072 .dev_release = thermal_release,
1073};
1074
1075/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001076 * __thermal_cooling_device_register() - register a new thermal cooling device
1077 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +08001078 * @type: the thermal cooling device type.
1079 * @devdata: device private data.
1080 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001081 *
1082 * This interface function adds a new thermal cooling device (fan/processor/...)
1083 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1084 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001085 * It also gives the opportunity to link the cooling device to a device tree
1086 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001087 *
1088 * Return: a pointer to the created struct thermal_cooling_device or an
1089 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001090 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001091static struct thermal_cooling_device *
1092__thermal_cooling_device_register(struct device_node *np,
1093 char *type, void *devdata,
1094 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001095{
1096 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001097 int result;
1098
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001099 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001100 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001101
1102 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001103 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001104 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001105
1106 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1107 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001108 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001109
1110 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1111 if (result) {
1112 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001113 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001114 }
1115
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001116 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +08001117 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001118 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001119 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +08001120 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +08001121 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +08001122 cdev->device.class = &thermal_class;
1123 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001124 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001125 result = device_register(&cdev->device);
1126 if (result) {
1127 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1128 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001129 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001130 }
1131
1132 /* sys I/F */
1133 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001134 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001135 if (result)
1136 goto unregister;
1137 }
1138
1139 result = device_create_file(&cdev->device, &dev_attr_max_state);
1140 if (result)
1141 goto unregister;
1142
1143 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1144 if (result)
1145 goto unregister;
1146
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301147 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001148 mutex_lock(&thermal_list_lock);
1149 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001150 mutex_unlock(&thermal_list_lock);
1151
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301152 /* Update binding information for 'this' new cdev */
1153 bind_cdev(cdev);
1154
1155 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001156
Joe Perchescaca8b82012-03-21 12:55:02 -07001157unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001158 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1159 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001160 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001161}
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001162
1163/**
1164 * thermal_cooling_device_register() - register a new thermal cooling device
1165 * @type: the thermal cooling device type.
1166 * @devdata: device private data.
1167 * @ops: standard thermal cooling devices callbacks.
1168 *
1169 * This interface function adds a new thermal cooling device (fan/processor/...)
1170 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1171 * to all the thermal zone devices registered at the same time.
1172 *
1173 * Return: a pointer to the created struct thermal_cooling_device or an
1174 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1175 */
1176struct thermal_cooling_device *
1177thermal_cooling_device_register(char *type, void *devdata,
1178 const struct thermal_cooling_device_ops *ops)
1179{
1180 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1181}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001182EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001183
1184/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001185 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1186 * @np: a pointer to a device tree node.
1187 * @type: the thermal cooling device type.
1188 * @devdata: device private data.
1189 * @ops: standard thermal cooling devices callbacks.
1190 *
1191 * This function will register a cooling device with device tree node reference.
1192 * This interface function adds a new thermal cooling device (fan/processor/...)
1193 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1194 * to all the thermal zone devices registered at the same time.
1195 *
1196 * Return: a pointer to the created struct thermal_cooling_device or an
1197 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1198 */
1199struct thermal_cooling_device *
1200thermal_of_cooling_device_register(struct device_node *np,
1201 char *type, void *devdata,
1202 const struct thermal_cooling_device_ops *ops)
1203{
1204 return __thermal_cooling_device_register(np, type, devdata, ops);
1205}
1206EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1207
1208/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001209 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001210 * @cdev: the thermal cooling device to remove.
1211 *
1212 * thermal_cooling_device_unregister() must be called when the device is no
1213 * longer needed.
1214 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301215void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001216{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301217 int i;
1218 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001219 struct thermal_zone_device *tz;
1220 struct thermal_cooling_device *pos = NULL;
1221
1222 if (!cdev)
1223 return;
1224
1225 mutex_lock(&thermal_list_lock);
1226 list_for_each_entry(pos, &thermal_cdev_list, node)
1227 if (pos == cdev)
1228 break;
1229 if (pos != cdev) {
1230 /* thermal cooling device not found */
1231 mutex_unlock(&thermal_list_lock);
1232 return;
1233 }
1234 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301235
1236 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001237 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301238 if (tz->ops->unbind) {
1239 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001240 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301241 }
1242
1243 if (!tz->tzp || !tz->tzp->tbp)
1244 continue;
1245
1246 tzp = tz->tzp;
1247 for (i = 0; i < tzp->num_tbps; i++) {
1248 if (tzp->tbp[i].cdev == cdev) {
1249 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1250 tzp->tbp[i].cdev = NULL;
1251 }
1252 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001253 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301254
Zhang Rui203d3d42008-01-17 15:51:08 +08001255 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301256
Zhang Rui203d3d42008-01-17 15:51:08 +08001257 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001258 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001259 device_remove_file(&cdev->device, &dev_attr_max_state);
1260 device_remove_file(&cdev->device, &dev_attr_cur_state);
1261
1262 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1263 device_unregister(&cdev->device);
1264 return;
1265}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001266EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001267
Durgadoss Rdc765482012-09-18 11:05:00 +05301268void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001269{
1270 struct thermal_instance *instance;
1271 unsigned long target = 0;
1272
1273 /* cooling device is updated*/
1274 if (cdev->updated)
1275 return;
1276
Zhang Ruif4a821c2012-07-24 16:56:21 +08001277 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001278 /* Make sure cdev enters the deepest cooling state */
1279 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
Aaron Lu06475b52013-12-02 13:54:26 +08001280 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1281 instance->tz->id, instance->target);
Zhang Ruice119f82012-06-27 14:13:04 +08001282 if (instance->target == THERMAL_NO_TARGET)
1283 continue;
1284 if (instance->target > target)
1285 target = instance->target;
1286 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001287 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001288 cdev->ops->set_cur_state(cdev, target);
1289 cdev->updated = true;
Aaron Lu06475b52013-12-02 13:54:26 +08001290 dev_dbg(&cdev->device, "set to state %lu\n", target);
Zhang Ruice119f82012-06-27 14:13:04 +08001291}
Durgadoss Rdc765482012-09-18 11:05:00 +05301292EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001293
Matthew Garrettb1569e92008-12-03 17:55:32 +00001294/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001295 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301296 * @tz: thermal zone device
1297 * @trip: indicates which trip point has been crossed
1298 *
1299 * This function handles the trip events from sensor drivers. It starts
1300 * throttling the cooling devices according to the policy configured.
1301 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1302 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1303 * The throttling policy is based on the configured platform data; if no
1304 * platform data is provided, this uses the step_wise throttling policy.
1305 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001306void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301307{
1308 handle_thermal_trip(tz, trip);
1309}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001310EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301311
1312/**
Eduardo Valentin269c1742013-04-23 21:48:19 +00001313 * create_trip_attrs() - create attributes for trip points
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001314 * @tz: the thermal zone device
1315 * @mask: Writeable trip point bitmap.
Eduardo Valentin269c1742013-04-23 21:48:19 +00001316 *
1317 * helper function to instantiate sysfs entries for every trip
1318 * point and its properties of a struct thermal_zone_device.
1319 *
1320 * Return: 0 on success, the proper error value otherwise.
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001321 */
1322static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1323{
1324 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001325 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001326
Durgadoss R27365a62012-07-25 10:10:59 +08001327 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001328 if (!tz->trip_type_attrs)
1329 return -ENOMEM;
1330
Durgadoss R27365a62012-07-25 10:10:59 +08001331 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001332 if (!tz->trip_temp_attrs) {
1333 kfree(tz->trip_type_attrs);
1334 return -ENOMEM;
1335 }
1336
Durgadoss R27365a62012-07-25 10:10:59 +08001337 if (tz->ops->get_trip_hyst) {
1338 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1339 if (!tz->trip_hyst_attrs) {
1340 kfree(tz->trip_type_attrs);
1341 kfree(tz->trip_temp_attrs);
1342 return -ENOMEM;
1343 }
1344 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001345
Durgadoss R27365a62012-07-25 10:10:59 +08001346
1347 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001348 /* create trip type attribute */
1349 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1350 "trip_point_%d_type", indx);
1351
1352 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1353 tz->trip_type_attrs[indx].attr.attr.name =
1354 tz->trip_type_attrs[indx].name;
1355 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1356 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1357
1358 device_create_file(&tz->device,
1359 &tz->trip_type_attrs[indx].attr);
1360
1361 /* create trip temp attribute */
1362 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1363 "trip_point_%d_temp", indx);
1364
1365 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1366 tz->trip_temp_attrs[indx].attr.attr.name =
1367 tz->trip_temp_attrs[indx].name;
1368 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1369 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1370 if (mask & (1 << indx)) {
1371 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1372 tz->trip_temp_attrs[indx].attr.store =
1373 trip_point_temp_store;
1374 }
1375
1376 device_create_file(&tz->device,
1377 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001378
1379 /* create Optional trip hyst attribute */
1380 if (!tz->ops->get_trip_hyst)
1381 continue;
1382 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1383 "trip_point_%d_hyst", indx);
1384
1385 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1386 tz->trip_hyst_attrs[indx].attr.attr.name =
1387 tz->trip_hyst_attrs[indx].name;
1388 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1389 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1390 if (tz->ops->set_trip_hyst) {
1391 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1392 tz->trip_hyst_attrs[indx].attr.store =
1393 trip_point_hyst_store;
1394 }
1395
1396 device_create_file(&tz->device,
1397 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001398 }
1399 return 0;
1400}
1401
1402static void remove_trip_attrs(struct thermal_zone_device *tz)
1403{
1404 int indx;
1405
1406 for (indx = 0; indx < tz->trips; indx++) {
1407 device_remove_file(&tz->device,
1408 &tz->trip_type_attrs[indx].attr);
1409 device_remove_file(&tz->device,
1410 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001411 if (tz->ops->get_trip_hyst)
1412 device_remove_file(&tz->device,
1413 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001414 }
1415 kfree(tz->trip_type_attrs);
1416 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001417 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001418}
1419
1420/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001421 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001422 * @type: the thermal zone device type
1423 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001424 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001425 * @devdata: private device data
1426 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301427 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001428 * @passive_delay: number of milliseconds to wait between polls when
1429 * performing passive cooling
1430 * @polling_delay: number of milliseconds to wait between polls when checking
1431 * whether trip points have been crossed (0 for interrupt
1432 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001433 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001434 * This interface function adds a new thermal zone device (sensor) to
1435 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1436 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001437 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001438 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001439 *
1440 * Return: a pointer to the created struct thermal_zone_device or an
1441 * in case of error, an ERR_PTR. Caller must check return value with
1442 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001443 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001444struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001445 int trips, int mask, void *devdata,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001446 struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301447 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001448 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001449{
1450 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001451 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001452 int result;
1453 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001454 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001455
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001456 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001457 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001458
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001459 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001460 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001461
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001462 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001463 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001464
Jonghwa Lee83720d02013-05-18 09:50:26 +00001465 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001466 return ERR_PTR(-EINVAL);
1467
Zhang Rui203d3d42008-01-17 15:51:08 +08001468 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1469 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001470 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001471
Zhang Rui2d374132012-06-27 10:09:00 +08001472 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001473 idr_init(&tz->idr);
1474 mutex_init(&tz->lock);
1475 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1476 if (result) {
1477 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001478 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001479 }
1480
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001481 strlcpy(tz->type, type ? : "", sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001482 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301483 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001484 tz->device.class = &thermal_class;
1485 tz->devdata = devdata;
1486 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001487 tz->passive_delay = passive_delay;
1488 tz->polling_delay = polling_delay;
1489
Kay Sievers354655e2009-01-06 10:44:37 -08001490 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001491 result = device_register(&tz->device);
1492 if (result) {
1493 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1494 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001495 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001496 }
1497
1498 /* sys I/F */
1499 if (type) {
1500 result = device_create_file(&tz->device, &dev_attr_type);
1501 if (result)
1502 goto unregister;
1503 }
1504
1505 result = device_create_file(&tz->device, &dev_attr_temp);
1506 if (result)
1507 goto unregister;
1508
1509 if (ops->get_mode) {
1510 result = device_create_file(&tz->device, &dev_attr_mode);
1511 if (result)
1512 goto unregister;
1513 }
1514
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001515 result = create_trip_attrs(tz, mask);
1516 if (result)
1517 goto unregister;
1518
Zhang Rui203d3d42008-01-17 15:51:08 +08001519 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001520 tz->ops->get_trip_type(tz, count, &trip_type);
1521 if (trip_type == THERMAL_TRIP_PASSIVE)
1522 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001523 }
1524
Durgadoss R5a2c0902012-09-18 11:04:58 +05301525 if (!passive) {
1526 result = device_create_file(&tz->device, &dev_attr_passive);
1527 if (result)
1528 goto unregister;
1529 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001530
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001531#ifdef CONFIG_THERMAL_EMULATION
1532 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1533 if (result)
1534 goto unregister;
1535#endif
Durgadoss R5a2c0902012-09-18 11:04:58 +05301536 /* Create policy attribute */
1537 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001538 if (result)
1539 goto unregister;
1540
Durgadoss Ra4a15482012-09-18 11:04:57 +05301541 /* Update 'this' zone's governor information */
1542 mutex_lock(&thermal_governor_lock);
1543
1544 if (tz->tzp)
1545 tz->governor = __find_governor(tz->tzp->governor_name);
1546 else
Zhang Ruif2234bc2014-01-24 10:23:19 +08001547 tz->governor = def_governor;
Durgadoss Ra4a15482012-09-18 11:04:57 +05301548
1549 mutex_unlock(&thermal_governor_lock);
1550
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04001551 if (!tz->tzp || !tz->tzp->no_hwmon) {
1552 result = thermal_add_hwmon_sysfs(tz);
1553 if (result)
1554 goto unregister;
1555 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08001556
Zhang Rui203d3d42008-01-17 15:51:08 +08001557 mutex_lock(&thermal_list_lock);
1558 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001559 mutex_unlock(&thermal_list_lock);
1560
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301561 /* Bind cooling devices for this zone */
1562 bind_tz(tz);
1563
Matthew Garrettb1569e92008-12-03 17:55:32 +00001564 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1565
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001566 if (!tz->ops->get_temp)
1567 thermal_zone_device_set_polling(tz, 0);
1568
Matthew Garrettb1569e92008-12-03 17:55:32 +00001569 thermal_zone_device_update(tz);
1570
Zhang Rui203d3d42008-01-17 15:51:08 +08001571 if (!result)
1572 return tz;
1573
Joe Perchescaca8b82012-03-21 12:55:02 -07001574unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001575 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1576 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001577 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001578}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001579EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001580
1581/**
1582 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001583 * @tz: the thermal zone device to remove
1584 */
1585void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1586{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301587 int i;
1588 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001589 struct thermal_cooling_device *cdev;
1590 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001591
1592 if (!tz)
1593 return;
1594
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301595 tzp = tz->tzp;
1596
Zhang Rui203d3d42008-01-17 15:51:08 +08001597 mutex_lock(&thermal_list_lock);
1598 list_for_each_entry(pos, &thermal_tz_list, node)
1599 if (pos == tz)
1600 break;
1601 if (pos != tz) {
1602 /* thermal zone device not found */
1603 mutex_unlock(&thermal_list_lock);
1604 return;
1605 }
1606 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301607
1608 /* Unbind all cdevs associated with 'this' thermal zone */
1609 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1610 if (tz->ops->unbind) {
1611 tz->ops->unbind(tz, cdev);
1612 continue;
1613 }
1614
1615 if (!tzp || !tzp->tbp)
1616 break;
1617
1618 for (i = 0; i < tzp->num_tbps; i++) {
1619 if (tzp->tbp[i].cdev == cdev) {
1620 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1621 tzp->tbp[i].cdev = NULL;
1622 }
1623 }
1624 }
1625
Zhang Rui203d3d42008-01-17 15:51:08 +08001626 mutex_unlock(&thermal_list_lock);
1627
Matthew Garrettb1569e92008-12-03 17:55:32 +00001628 thermal_zone_device_set_polling(tz, 0);
1629
Zhang Rui203d3d42008-01-17 15:51:08 +08001630 if (tz->type[0])
1631 device_remove_file(&tz->device, &dev_attr_type);
1632 device_remove_file(&tz->device, &dev_attr_temp);
1633 if (tz->ops->get_mode)
1634 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301635 device_remove_file(&tz->device, &dev_attr_policy);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001636 remove_trip_attrs(tz);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301637 tz->governor = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001638
Zhang Ruie68b16a2008-04-21 16:07:52 +08001639 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001640 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1641 idr_destroy(&tz->idr);
1642 mutex_destroy(&tz->lock);
1643 device_unregister(&tz->device);
1644 return;
1645}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001646EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001647
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001648/**
1649 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1650 * @name: thermal zone name to fetch the temperature
1651 *
1652 * When only one zone is found with the passed name, returns a reference to it.
1653 *
1654 * Return: On success returns a reference to an unique thermal zone with
1655 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1656 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1657 */
1658struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1659{
1660 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1661 unsigned int found = 0;
1662
1663 if (!name)
1664 goto exit;
1665
1666 mutex_lock(&thermal_list_lock);
1667 list_for_each_entry(pos, &thermal_tz_list, node)
1668 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1669 found++;
1670 ref = pos;
1671 }
1672 mutex_unlock(&thermal_list_lock);
1673
1674 /* nothing has been found, thus an error code for it */
1675 if (found == 0)
1676 ref = ERR_PTR(-ENODEV);
1677 else if (found > 1)
1678 /* Success only when an unique zone is found */
1679 ref = ERR_PTR(-EEXIST);
1680
1681exit:
1682 return ref;
1683}
1684EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1685
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001686#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01001687static const struct genl_multicast_group thermal_event_mcgrps[] = {
1688 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1689};
1690
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001691static struct genl_family thermal_event_genl_family = {
1692 .id = GENL_ID_GENERATE,
1693 .name = THERMAL_GENL_FAMILY_NAME,
1694 .version = THERMAL_GENL_VERSION,
1695 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001696 .mcgrps = thermal_event_mcgrps,
1697 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001698};
1699
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001700int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1701 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301702{
1703 struct sk_buff *skb;
1704 struct nlattr *attr;
1705 struct thermal_genl_event *thermal_event;
1706 void *msg_header;
1707 int size;
1708 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001709 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301710
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001711 if (!tz)
1712 return -EINVAL;
1713
R.Durgadoss4cb18722010-10-27 03:33:29 +05301714 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001715 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1716 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301717
1718 skb = genlmsg_new(size, GFP_ATOMIC);
1719 if (!skb)
1720 return -ENOMEM;
1721
1722 /* add the genetlink message header */
1723 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1724 &thermal_event_genl_family, 0,
1725 THERMAL_GENL_CMD_EVENT);
1726 if (!msg_header) {
1727 nlmsg_free(skb);
1728 return -ENOMEM;
1729 }
1730
1731 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001732 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1733 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301734
1735 if (!attr) {
1736 nlmsg_free(skb);
1737 return -EINVAL;
1738 }
1739
1740 thermal_event = nla_data(attr);
1741 if (!thermal_event) {
1742 nlmsg_free(skb);
1743 return -EINVAL;
1744 }
1745
1746 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1747
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001748 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301749 thermal_event->event = event;
1750
1751 /* send multicast genetlink message */
1752 result = genlmsg_end(skb, msg_header);
1753 if (result < 0) {
1754 nlmsg_free(skb);
1755 return result;
1756 }
1757
Johannes Berg68eb5502013-11-19 15:19:38 +01001758 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001759 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301760 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001761 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301762
1763 return result;
1764}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001765EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301766
1767static int genetlink_init(void)
1768{
Johannes Berg2a94fe42013-11-19 15:19:39 +01001769 return genl_register_family(&thermal_event_genl_family);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301770}
1771
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001772static void genetlink_exit(void)
1773{
1774 genl_unregister_family(&thermal_event_genl_family);
1775}
1776#else /* !CONFIG_NET */
1777static inline int genetlink_init(void) { return 0; }
1778static inline void genetlink_exit(void) {}
1779#endif /* !CONFIG_NET */
1780
Zhang Rui80a26a52013-03-26 16:38:29 +08001781static int __init thermal_register_governors(void)
1782{
1783 int result;
1784
1785 result = thermal_gov_step_wise_register();
1786 if (result)
1787 return result;
1788
1789 result = thermal_gov_fair_share_register();
1790 if (result)
1791 return result;
1792
1793 return thermal_gov_user_space_register();
1794}
1795
1796static void thermal_unregister_governors(void)
1797{
1798 thermal_gov_step_wise_unregister();
1799 thermal_gov_fair_share_unregister();
1800 thermal_gov_user_space_unregister();
1801}
1802
Zhang Rui203d3d42008-01-17 15:51:08 +08001803static int __init thermal_init(void)
1804{
Zhang Rui80a26a52013-03-26 16:38:29 +08001805 int result;
1806
1807 result = thermal_register_governors();
1808 if (result)
1809 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08001810
1811 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001812 if (result)
1813 goto unregister_governors;
1814
R.Durgadoss4cb18722010-10-27 03:33:29 +05301815 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08001816 if (result)
1817 goto unregister_class;
1818
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001819 result = of_parse_thermal_zones();
1820 if (result)
1821 goto exit_netlink;
1822
Zhang Rui80a26a52013-03-26 16:38:29 +08001823 return 0;
1824
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001825exit_netlink:
1826 genetlink_exit();
Zhang Rui80a26a52013-03-26 16:38:29 +08001827unregister_governors:
1828 thermal_unregister_governors();
1829unregister_class:
1830 class_unregister(&thermal_class);
1831error:
1832 idr_destroy(&thermal_tz_idr);
1833 idr_destroy(&thermal_cdev_idr);
1834 mutex_destroy(&thermal_idr_lock);
1835 mutex_destroy(&thermal_list_lock);
1836 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001837 return result;
1838}
1839
1840static void __exit thermal_exit(void)
1841{
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001842 of_thermal_destroy_zones();
Zhang Rui80a26a52013-03-26 16:38:29 +08001843 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001844 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001845 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08001846 idr_destroy(&thermal_tz_idr);
1847 idr_destroy(&thermal_cdev_idr);
1848 mutex_destroy(&thermal_idr_lock);
1849 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08001850 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001851}
1852
R.Durgadoss4cb18722010-10-27 03:33:29 +05301853fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001854module_exit(thermal_exit);