blob: 6b32391260a03ae59a46e7de36b570a01de758b6 [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)
72 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
73 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
Durgadoss Ra4a15482012-09-18 11:04:57 +0530110 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
111 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) {
135 if (!strnicmp(pos->governor->name, governor->name,
136 THERMAL_NAME_LENGTH))
137 pos->governor = NULL;
138 }
139
140 mutex_unlock(&thermal_list_lock);
141 list_del(&governor->governor_list);
142exit:
143 mutex_unlock(&thermal_governor_lock);
144 return;
145}
Zhang Rui203d3d42008-01-17 15:51:08 +0800146
147static int get_idr(struct idr *idr, struct mutex *lock, int *id)
148{
Tejun Heo6deb69f2013-02-27 17:04:46 -0800149 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800150
151 if (lock)
152 mutex_lock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800153 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800154 if (lock)
155 mutex_unlock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800156 if (unlikely(ret < 0))
157 return ret;
158 *id = ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800159 return 0;
160}
161
162static void release_idr(struct idr *idr, struct mutex *lock, int id)
163{
164 if (lock)
165 mutex_lock(lock);
166 idr_remove(idr, id);
167 if (lock)
168 mutex_unlock(lock);
169}
170
Durgadoss R9b4298a2012-09-18 11:04:54 +0530171int get_tz_trend(struct thermal_zone_device *tz, int trip)
172{
173 enum thermal_trend trend;
174
Eduardo Valentin0c872502013-05-29 21:37:00 +0000175 if (tz->emul_temperature || !tz->ops->get_trend ||
176 tz->ops->get_trend(tz, trip, &trend)) {
Durgadoss R9b4298a2012-09-18 11:04:54 +0530177 if (tz->temperature > tz->last_temperature)
178 trend = THERMAL_TREND_RAISING;
179 else if (tz->temperature < tz->last_temperature)
180 trend = THERMAL_TREND_DROPPING;
181 else
182 trend = THERMAL_TREND_STABLE;
183 }
184
185 return trend;
186}
187EXPORT_SYMBOL(get_tz_trend);
188
189struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
190 struct thermal_cooling_device *cdev, int trip)
191{
192 struct thermal_instance *pos = NULL;
193 struct thermal_instance *target_instance = NULL;
194
195 mutex_lock(&tz->lock);
196 mutex_lock(&cdev->lock);
197
198 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
199 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
200 target_instance = pos;
201 break;
202 }
203 }
204
205 mutex_unlock(&cdev->lock);
206 mutex_unlock(&tz->lock);
207
208 return target_instance;
209}
210EXPORT_SYMBOL(get_thermal_instance);
211
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530212static void print_bind_err_msg(struct thermal_zone_device *tz,
213 struct thermal_cooling_device *cdev, int ret)
214{
215 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
216 tz->type, cdev->type, ret);
217}
218
219static void __bind(struct thermal_zone_device *tz, int mask,
Eduardo Valentina8892d832013-07-16 15:26:28 -0400220 struct thermal_cooling_device *cdev,
221 unsigned long *limits)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530222{
223 int i, ret;
224
225 for (i = 0; i < tz->trips; i++) {
226 if (mask & (1 << i)) {
Eduardo Valentina8892d832013-07-16 15:26:28 -0400227 unsigned long upper, lower;
228
229 upper = THERMAL_NO_LIMIT;
230 lower = THERMAL_NO_LIMIT;
231 if (limits) {
232 lower = limits[i * 2];
233 upper = limits[i * 2 + 1];
234 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530235 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
Eduardo Valentina8892d832013-07-16 15:26:28 -0400236 upper, lower);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530237 if (ret)
238 print_bind_err_msg(tz, cdev, ret);
239 }
240 }
241}
242
243static void __unbind(struct thermal_zone_device *tz, int mask,
244 struct thermal_cooling_device *cdev)
245{
246 int i;
247
248 for (i = 0; i < tz->trips; i++)
249 if (mask & (1 << i))
250 thermal_zone_unbind_cooling_device(tz, i, cdev);
251}
252
253static void bind_cdev(struct thermal_cooling_device *cdev)
254{
255 int i, ret;
256 const struct thermal_zone_params *tzp;
257 struct thermal_zone_device *pos = NULL;
258
259 mutex_lock(&thermal_list_lock);
260
261 list_for_each_entry(pos, &thermal_tz_list, node) {
262 if (!pos->tzp && !pos->ops->bind)
263 continue;
264
Ni Wadea9f2d192013-11-06 14:30:13 +0800265 if (pos->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530266 ret = pos->ops->bind(pos, cdev);
267 if (ret)
268 print_bind_err_msg(pos, cdev, ret);
Ni Wadea9f2d192013-11-06 14:30:13 +0800269 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530270 }
271
272 tzp = pos->tzp;
Hugh Dickins791700c2012-09-27 16:48:28 -0700273 if (!tzp || !tzp->tbp)
274 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530275
276 for (i = 0; i < tzp->num_tbps; i++) {
277 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
278 continue;
279 if (tzp->tbp[i].match(pos, cdev))
280 continue;
281 tzp->tbp[i].cdev = cdev;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400282 __bind(pos, tzp->tbp[i].trip_mask, cdev,
283 tzp->tbp[i].binding_limits);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530284 }
285 }
286
287 mutex_unlock(&thermal_list_lock);
288}
289
290static void bind_tz(struct thermal_zone_device *tz)
291{
292 int i, ret;
293 struct thermal_cooling_device *pos = NULL;
294 const struct thermal_zone_params *tzp = tz->tzp;
295
296 if (!tzp && !tz->ops->bind)
297 return;
298
299 mutex_lock(&thermal_list_lock);
300
Ni Wadea9f2d192013-11-06 14:30:13 +0800301 /* If there is ops->bind, try to use ops->bind */
302 if (tz->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530303 list_for_each_entry(pos, &thermal_cdev_list, node) {
304 ret = tz->ops->bind(tz, pos);
305 if (ret)
306 print_bind_err_msg(tz, pos, ret);
307 }
308 goto exit;
309 }
310
Hugh Dickins791700c2012-09-27 16:48:28 -0700311 if (!tzp || !tzp->tbp)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530312 goto exit;
313
314 list_for_each_entry(pos, &thermal_cdev_list, node) {
315 for (i = 0; i < tzp->num_tbps; i++) {
316 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
317 continue;
318 if (tzp->tbp[i].match(tz, pos))
319 continue;
320 tzp->tbp[i].cdev = pos;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400321 __bind(tz, tzp->tbp[i].trip_mask, pos,
322 tzp->tbp[i].binding_limits);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530323 }
324 }
325exit:
326 mutex_unlock(&thermal_list_lock);
327}
328
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530329static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
330 int delay)
331{
332 if (delay > 1000)
333 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
334 round_jiffies(msecs_to_jiffies(delay)));
335 else if (delay)
336 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
337 msecs_to_jiffies(delay));
338 else
339 cancel_delayed_work(&tz->poll_queue);
340}
341
342static void monitor_thermal_zone(struct thermal_zone_device *tz)
343{
344 mutex_lock(&tz->lock);
345
346 if (tz->passive)
347 thermal_zone_device_set_polling(tz, tz->passive_delay);
348 else if (tz->polling_delay)
349 thermal_zone_device_set_polling(tz, tz->polling_delay);
350 else
351 thermal_zone_device_set_polling(tz, 0);
352
353 mutex_unlock(&tz->lock);
354}
355
356static void handle_non_critical_trips(struct thermal_zone_device *tz,
357 int trip, enum thermal_trip_type trip_type)
358{
Zhang Ruif2234bc2014-01-24 10:23:19 +0800359 tz->governor ? tz->governor->throttle(tz, trip) :
360 def_governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530361}
362
363static void handle_critical_trips(struct thermal_zone_device *tz,
364 int trip, enum thermal_trip_type trip_type)
365{
366 long trip_temp;
367
368 tz->ops->get_trip_temp(tz, trip, &trip_temp);
369
370 /* If we have not crossed the trip_temp, we do not care. */
371 if (tz->temperature < trip_temp)
372 return;
373
374 if (tz->ops->notify)
375 tz->ops->notify(tz, trip, trip_type);
376
377 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000378 dev_emerg(&tz->device,
379 "critical temperature reached(%d C),shutting down\n",
380 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530381 orderly_poweroff(true);
382 }
383}
384
385static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
386{
387 enum thermal_trip_type type;
388
389 tz->ops->get_trip_type(tz, trip, &type);
390
391 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
392 handle_critical_trips(tz, trip, type);
393 else
394 handle_non_critical_trips(tz, trip, type);
395 /*
396 * Alright, we handled this trip successfully.
397 * So, start monitoring again.
398 */
399 monitor_thermal_zone(tz);
400}
401
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000402/**
403 * thermal_zone_get_temp() - returns its the temperature of thermal zone
404 * @tz: a valid pointer to a struct thermal_zone_device
405 * @temp: a valid pointer to where to store the resulting temperature.
406 *
407 * When a valid thermal zone reference is passed, it will fetch its
408 * temperature and fill @temp.
409 *
410 * Return: On success returns 0, an error code otherwise
411 */
412int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000413{
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000414 int ret = -EINVAL;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800415#ifdef CONFIG_THERMAL_EMULATION
416 int count;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000417 unsigned long crit_temp = -1UL;
418 enum thermal_trip_type type;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800419#endif
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000420
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400421 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000422 goto exit;
423
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000424 mutex_lock(&tz->lock);
425
426 ret = tz->ops->get_temp(tz, temp);
427#ifdef CONFIG_THERMAL_EMULATION
428 if (!tz->emul_temperature)
429 goto skip_emul;
430
431 for (count = 0; count < tz->trips; count++) {
432 ret = tz->ops->get_trip_type(tz, count, &type);
433 if (!ret && type == THERMAL_TRIP_CRITICAL) {
434 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
435 break;
436 }
437 }
438
439 if (ret)
440 goto skip_emul;
441
442 if (*temp < crit_temp)
443 *temp = tz->emul_temperature;
444skip_emul:
445#endif
446 mutex_unlock(&tz->lock);
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000447exit:
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000448 return ret;
449}
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000450EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000451
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530452static void update_temperature(struct thermal_zone_device *tz)
453{
454 long temp;
455 int ret;
456
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000457 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530458 if (ret) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000459 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
460 tz->id);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000461 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530462 }
463
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000464 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530465 tz->last_temperature = tz->temperature;
466 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530467 mutex_unlock(&tz->lock);
Aaron Lu06475b52013-12-02 13:54:26 +0800468
Punit Agrawal100a8fd2014-07-29 11:50:48 +0100469 trace_thermal_temperature(tz);
Aaron Lu06475b52013-12-02 13:54:26 +0800470 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
471 tz->last_temperature, tz->temperature);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530472}
473
474void thermal_zone_device_update(struct thermal_zone_device *tz)
475{
476 int count;
477
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400478 if (!tz->ops->get_temp)
479 return;
480
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530481 update_temperature(tz);
482
483 for (count = 0; count < tz->trips; count++)
484 handle_thermal_trip(tz, count);
485}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000486EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530487
488static void thermal_zone_device_check(struct work_struct *work)
489{
490 struct thermal_zone_device *tz = container_of(work, struct
491 thermal_zone_device,
492 poll_queue.work);
493 thermal_zone_device_update(tz);
494}
495
Zhang Rui203d3d42008-01-17 15:51:08 +0800496/* sys I/F for thermal zone */
497
498#define to_thermal_zone(_dev) \
499 container_of(_dev, struct thermal_zone_device, device)
500
501static ssize_t
502type_show(struct device *dev, struct device_attribute *attr, char *buf)
503{
504 struct thermal_zone_device *tz = to_thermal_zone(dev);
505
506 return sprintf(buf, "%s\n", tz->type);
507}
508
509static ssize_t
510temp_show(struct device *dev, struct device_attribute *attr, char *buf)
511{
512 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000513 long temperature;
514 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800515
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000516 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000517
518 if (ret)
519 return ret;
520
521 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800522}
523
524static ssize_t
525mode_show(struct device *dev, struct device_attribute *attr, char *buf)
526{
527 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000528 enum thermal_device_mode mode;
529 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800530
531 if (!tz->ops->get_mode)
532 return -EPERM;
533
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000534 result = tz->ops->get_mode(tz, &mode);
535 if (result)
536 return result;
537
538 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
539 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800540}
541
542static ssize_t
543mode_store(struct device *dev, struct device_attribute *attr,
544 const char *buf, size_t count)
545{
546 struct thermal_zone_device *tz = to_thermal_zone(dev);
547 int result;
548
549 if (!tz->ops->set_mode)
550 return -EPERM;
551
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530552 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000553 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530554 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000555 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
556 else
557 result = -EINVAL;
558
Zhang Rui203d3d42008-01-17 15:51:08 +0800559 if (result)
560 return result;
561
562 return count;
563}
564
565static ssize_t
566trip_point_type_show(struct device *dev, struct device_attribute *attr,
567 char *buf)
568{
569 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000570 enum thermal_trip_type type;
571 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800572
573 if (!tz->ops->get_trip_type)
574 return -EPERM;
575
576 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
577 return -EINVAL;
578
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000579 result = tz->ops->get_trip_type(tz, trip, &type);
580 if (result)
581 return result;
582
583 switch (type) {
584 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300585 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000586 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300587 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000588 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300589 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000590 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300591 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000592 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300593 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000594 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800595}
596
597static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800598trip_point_temp_store(struct device *dev, struct device_attribute *attr,
599 const char *buf, size_t count)
600{
601 struct thermal_zone_device *tz = to_thermal_zone(dev);
602 int trip, ret;
603 unsigned long temperature;
604
605 if (!tz->ops->set_trip_temp)
606 return -EPERM;
607
608 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
609 return -EINVAL;
610
611 if (kstrtoul(buf, 10, &temperature))
612 return -EINVAL;
613
614 ret = tz->ops->set_trip_temp(tz, trip, temperature);
615
616 return ret ? ret : count;
617}
618
619static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800620trip_point_temp_show(struct device *dev, struct device_attribute *attr,
621 char *buf)
622{
623 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000624 int trip, ret;
625 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800626
627 if (!tz->ops->get_trip_temp)
628 return -EPERM;
629
630 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
631 return -EINVAL;
632
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000633 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
634
635 if (ret)
636 return ret;
637
638 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800639}
640
Matthew Garrett03a971a2008-12-03 18:00:38 +0000641static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800642trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
643 const char *buf, size_t count)
644{
645 struct thermal_zone_device *tz = to_thermal_zone(dev);
646 int trip, ret;
647 unsigned long temperature;
648
649 if (!tz->ops->set_trip_hyst)
650 return -EPERM;
651
652 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
653 return -EINVAL;
654
655 if (kstrtoul(buf, 10, &temperature))
656 return -EINVAL;
657
658 /*
659 * We are not doing any check on the 'temperature' value
660 * here. The driver implementing 'set_trip_hyst' has to
661 * take care of this.
662 */
663 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
664
665 return ret ? ret : count;
666}
667
668static ssize_t
669trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
670 char *buf)
671{
672 struct thermal_zone_device *tz = to_thermal_zone(dev);
673 int trip, ret;
674 unsigned long temperature;
675
676 if (!tz->ops->get_trip_hyst)
677 return -EPERM;
678
679 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
680 return -EINVAL;
681
682 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
683
684 return ret ? ret : sprintf(buf, "%ld\n", temperature);
685}
686
687static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000688passive_store(struct device *dev, struct device_attribute *attr,
689 const char *buf, size_t count)
690{
691 struct thermal_zone_device *tz = to_thermal_zone(dev);
692 struct thermal_cooling_device *cdev = NULL;
693 int state;
694
695 if (!sscanf(buf, "%d\n", &state))
696 return -EINVAL;
697
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100698 /* sanity check: values below 1000 millicelcius don't make sense
699 * and can cause the system to go into a thermal heart attack
700 */
701 if (state && state < 1000)
702 return -EINVAL;
703
Matthew Garrett03a971a2008-12-03 18:00:38 +0000704 if (state && !tz->forced_passive) {
705 mutex_lock(&thermal_list_lock);
706 list_for_each_entry(cdev, &thermal_cdev_list, node) {
707 if (!strncmp("Processor", cdev->type,
708 sizeof("Processor")))
709 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800710 THERMAL_TRIPS_NONE, cdev,
711 THERMAL_NO_LIMIT,
712 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000713 }
714 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100715 if (!tz->passive_delay)
716 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000717 } else if (!state && tz->forced_passive) {
718 mutex_lock(&thermal_list_lock);
719 list_for_each_entry(cdev, &thermal_cdev_list, node) {
720 if (!strncmp("Processor", cdev->type,
721 sizeof("Processor")))
722 thermal_zone_unbind_cooling_device(tz,
723 THERMAL_TRIPS_NONE,
724 cdev);
725 }
726 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100727 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000728 }
729
Matthew Garrett03a971a2008-12-03 18:00:38 +0000730 tz->forced_passive = state;
731
732 thermal_zone_device_update(tz);
733
734 return count;
735}
736
737static ssize_t
738passive_show(struct device *dev, struct device_attribute *attr,
739 char *buf)
740{
741 struct thermal_zone_device *tz = to_thermal_zone(dev);
742
743 return sprintf(buf, "%d\n", tz->forced_passive);
744}
745
Durgadoss R5a2c0902012-09-18 11:04:58 +0530746static ssize_t
747policy_store(struct device *dev, struct device_attribute *attr,
748 const char *buf, size_t count)
749{
750 int ret = -EINVAL;
751 struct thermal_zone_device *tz = to_thermal_zone(dev);
752 struct thermal_governor *gov;
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000753 char name[THERMAL_NAME_LENGTH];
754
755 snprintf(name, sizeof(name), "%s", buf);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530756
757 mutex_lock(&thermal_governor_lock);
758
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000759 gov = __find_governor(strim(name));
Durgadoss R5a2c0902012-09-18 11:04:58 +0530760 if (!gov)
761 goto exit;
762
763 tz->governor = gov;
764 ret = count;
765
766exit:
767 mutex_unlock(&thermal_governor_lock);
768 return ret;
769}
770
771static ssize_t
772policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
773{
774 struct thermal_zone_device *tz = to_thermal_zone(dev);
775
776 return sprintf(buf, "%s\n", tz->governor->name);
777}
778
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000779#ifdef CONFIG_THERMAL_EMULATION
780static ssize_t
781emul_temp_store(struct device *dev, struct device_attribute *attr,
782 const char *buf, size_t count)
783{
784 struct thermal_zone_device *tz = to_thermal_zone(dev);
785 int ret = 0;
786 unsigned long temperature;
787
788 if (kstrtoul(buf, 10, &temperature))
789 return -EINVAL;
790
791 if (!tz->ops->set_emul_temp) {
792 mutex_lock(&tz->lock);
793 tz->emul_temperature = temperature;
794 mutex_unlock(&tz->lock);
795 } else {
796 ret = tz->ops->set_emul_temp(tz, temperature);
797 }
798
lan,Tianyu800744b2014-01-02 15:47:54 +0800799 if (!ret)
800 thermal_zone_device_update(tz);
801
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000802 return ret ? ret : count;
803}
804static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
805#endif/*CONFIG_THERMAL_EMULATION*/
806
Zhang Rui203d3d42008-01-17 15:51:08 +0800807static DEVICE_ATTR(type, 0444, type_show, NULL);
808static DEVICE_ATTR(temp, 0444, temp_show, NULL);
809static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700810static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530811static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800812
Zhang Rui203d3d42008-01-17 15:51:08 +0800813/* sys I/F for cooling device */
814#define to_cooling_device(_dev) \
815 container_of(_dev, struct thermal_cooling_device, device)
816
817static ssize_t
818thermal_cooling_device_type_show(struct device *dev,
819 struct device_attribute *attr, char *buf)
820{
821 struct thermal_cooling_device *cdev = to_cooling_device(dev);
822
823 return sprintf(buf, "%s\n", cdev->type);
824}
825
826static ssize_t
827thermal_cooling_device_max_state_show(struct device *dev,
828 struct device_attribute *attr, char *buf)
829{
830 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000831 unsigned long state;
832 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800833
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000834 ret = cdev->ops->get_max_state(cdev, &state);
835 if (ret)
836 return ret;
837 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800838}
839
840static ssize_t
841thermal_cooling_device_cur_state_show(struct device *dev,
842 struct device_attribute *attr, char *buf)
843{
844 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000845 unsigned long state;
846 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800847
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000848 ret = cdev->ops->get_cur_state(cdev, &state);
849 if (ret)
850 return ret;
851 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800852}
853
854static ssize_t
855thermal_cooling_device_cur_state_store(struct device *dev,
856 struct device_attribute *attr,
857 const char *buf, size_t count)
858{
859 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000860 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800861 int result;
862
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000863 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800864 return -EINVAL;
865
Roel Kluinedb94912009-12-15 22:46:50 +0100866 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800867 return -EINVAL;
868
869 result = cdev->ops->set_cur_state(cdev, state);
870 if (result)
871 return result;
872 return count;
873}
874
875static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500876__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800877static DEVICE_ATTR(max_state, 0444,
878 thermal_cooling_device_max_state_show, NULL);
879static DEVICE_ATTR(cur_state, 0644,
880 thermal_cooling_device_cur_state_show,
881 thermal_cooling_device_cur_state_store);
882
883static ssize_t
884thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500885 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800886{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800887 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800888
889 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800890 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800891
892 if (instance->trip == THERMAL_TRIPS_NONE)
893 return sprintf(buf, "-1\n");
894 else
895 return sprintf(buf, "%d\n", instance->trip);
896}
897
898/* Device management */
899
900/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000901 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
902 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +0800903 * @trip: indicates which trip point the cooling devices is
904 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000905 * @cdev: pointer to struct thermal_cooling_device
906 * @upper: the Maximum cooling state for this trip point.
907 * THERMAL_NO_LIMIT means no upper limit,
908 * and the cooling device can be in max_state.
909 * @lower: the Minimum cooling state can be used for this trip point.
910 * THERMAL_NO_LIMIT means no lower limit,
911 * and the cooling device can be in cooling state 0.
Len Brown543a9562008-02-07 16:55:08 -0500912 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000913 * This interface function bind a thermal cooling device to the certain trip
914 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500915 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000916 *
917 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800918 */
919int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
920 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800921 struct thermal_cooling_device *cdev,
922 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +0800923{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800924 struct thermal_instance *dev;
925 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500926 struct thermal_zone_device *pos1;
927 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800928 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800929 int result;
930
Len Brown543a9562008-02-07 16:55:08 -0500931 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800932 return -EINVAL;
933
Thomas Sujithc7516702008-02-15 00:58:50 -0500934 list_for_each_entry(pos1, &thermal_tz_list, node) {
935 if (pos1 == tz)
936 break;
937 }
938 list_for_each_entry(pos2, &thermal_cdev_list, node) {
939 if (pos2 == cdev)
940 break;
941 }
942
943 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800944 return -EINVAL;
945
Zhang Rui9d998422012-06-26 16:35:57 +0800946 cdev->ops->get_max_state(cdev, &max_state);
947
948 /* lower default 0, upper default max_state */
949 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
950 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
951
952 if (lower > upper || upper > max_state)
953 return -EINVAL;
954
Zhang Rui203d3d42008-01-17 15:51:08 +0800955 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800956 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800957 if (!dev)
958 return -ENOMEM;
959 dev->tz = tz;
960 dev->cdev = cdev;
961 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800962 dev->upper = upper;
963 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800964 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +0800965
Zhang Rui203d3d42008-01-17 15:51:08 +0800966 result = get_idr(&tz->idr, &tz->lock, &dev->id);
967 if (result)
968 goto free_mem;
969
970 sprintf(dev->name, "cdev%d", dev->id);
971 result =
972 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
973 if (result)
974 goto release_idr;
975
976 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700977 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800978 dev->attr.attr.name = dev->attr_name;
979 dev->attr.attr.mode = 0444;
980 dev->attr.show = thermal_cooling_device_trip_point_show;
981 result = device_create_file(&tz->device, &dev->attr);
982 if (result)
983 goto remove_symbol_link;
984
985 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800986 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800987 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +0800988 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
989 result = -EEXIST;
990 break;
991 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800992 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800993 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800994 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
995 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800996 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800997 mutex_unlock(&tz->lock);
998
999 if (!result)
1000 return 0;
1001
1002 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b802012-03-21 12:55:02 -07001003remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001004 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b802012-03-21 12:55:02 -07001005release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001006 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b802012-03-21 12:55:02 -07001007free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001008 kfree(dev);
1009 return result;
1010}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001011EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001012
1013/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001014 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1015 * thermal zone.
1016 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +08001017 * @trip: indicates which trip point the cooling devices is
1018 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001019 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -05001020 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001021 * This interface function unbind a thermal cooling device from the certain
1022 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001023 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001024 *
1025 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001026 */
1027int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1028 int trip,
1029 struct thermal_cooling_device *cdev)
1030{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001031 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001032
1033 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001034 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001035 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001036 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001037 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001038 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001039 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001040 mutex_unlock(&tz->lock);
1041 goto unbind;
1042 }
1043 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001044 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001045 mutex_unlock(&tz->lock);
1046
1047 return -ENODEV;
1048
Joe Perchescaca8b802012-03-21 12:55:02 -07001049unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +08001050 device_remove_file(&tz->device, &pos->attr);
1051 sysfs_remove_link(&tz->device.kobj, pos->name);
1052 release_idr(&tz->idr, &tz->lock, pos->id);
1053 kfree(pos);
1054 return 0;
1055}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001056EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001057
1058static void thermal_release(struct device *dev)
1059{
1060 struct thermal_zone_device *tz;
1061 struct thermal_cooling_device *cdev;
1062
Joe Perchescaca8b802012-03-21 12:55:02 -07001063 if (!strncmp(dev_name(dev), "thermal_zone",
1064 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001065 tz = to_thermal_zone(dev);
1066 kfree(tz);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +05301067 } else if(!strncmp(dev_name(dev), "cooling_device",
1068 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +08001069 cdev = to_cooling_device(dev);
1070 kfree(cdev);
1071 }
1072}
1073
1074static struct class thermal_class = {
1075 .name = "thermal",
1076 .dev_release = thermal_release,
1077};
1078
1079/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001080 * __thermal_cooling_device_register() - register a new thermal cooling device
1081 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +08001082 * @type: the thermal cooling device type.
1083 * @devdata: device private data.
1084 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001085 *
1086 * This interface function adds a new thermal cooling device (fan/processor/...)
1087 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1088 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001089 * It also gives the opportunity to link the cooling device to a device tree
1090 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001091 *
1092 * Return: a pointer to the created struct thermal_cooling_device or an
1093 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001094 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001095static struct thermal_cooling_device *
1096__thermal_cooling_device_register(struct device_node *np,
1097 char *type, void *devdata,
1098 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001099{
1100 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001101 int result;
1102
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001103 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001104 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001105
1106 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001107 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001108 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001109
1110 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1111 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001112 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001113
1114 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1115 if (result) {
1116 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001117 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001118 }
1119
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001120 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +08001121 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001122 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001123 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +08001124 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +08001125 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +08001126 cdev->device.class = &thermal_class;
1127 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001128 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001129 result = device_register(&cdev->device);
1130 if (result) {
1131 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1132 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001133 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001134 }
1135
1136 /* sys I/F */
1137 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001138 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001139 if (result)
1140 goto unregister;
1141 }
1142
1143 result = device_create_file(&cdev->device, &dev_attr_max_state);
1144 if (result)
1145 goto unregister;
1146
1147 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1148 if (result)
1149 goto unregister;
1150
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301151 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001152 mutex_lock(&thermal_list_lock);
1153 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001154 mutex_unlock(&thermal_list_lock);
1155
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301156 /* Update binding information for 'this' new cdev */
1157 bind_cdev(cdev);
1158
1159 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001160
Joe Perchescaca8b802012-03-21 12:55:02 -07001161unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001162 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1163 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001164 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001165}
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001166
1167/**
1168 * thermal_cooling_device_register() - register a new thermal cooling device
1169 * @type: the thermal cooling device type.
1170 * @devdata: device private data.
1171 * @ops: standard thermal cooling devices callbacks.
1172 *
1173 * This interface function adds a new thermal cooling device (fan/processor/...)
1174 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1175 * to all the thermal zone devices registered at the same time.
1176 *
1177 * Return: a pointer to the created struct thermal_cooling_device or an
1178 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1179 */
1180struct thermal_cooling_device *
1181thermal_cooling_device_register(char *type, void *devdata,
1182 const struct thermal_cooling_device_ops *ops)
1183{
1184 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1185}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001186EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001187
1188/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001189 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1190 * @np: a pointer to a device tree node.
1191 * @type: the thermal cooling device type.
1192 * @devdata: device private data.
1193 * @ops: standard thermal cooling devices callbacks.
1194 *
1195 * This function will register a cooling device with device tree node reference.
1196 * This interface function adds a new thermal cooling device (fan/processor/...)
1197 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1198 * to all the thermal zone devices registered at the same time.
1199 *
1200 * Return: a pointer to the created struct thermal_cooling_device or an
1201 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1202 */
1203struct thermal_cooling_device *
1204thermal_of_cooling_device_register(struct device_node *np,
1205 char *type, void *devdata,
1206 const struct thermal_cooling_device_ops *ops)
1207{
1208 return __thermal_cooling_device_register(np, type, devdata, ops);
1209}
1210EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1211
1212/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001213 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001214 * @cdev: the thermal cooling device to remove.
1215 *
1216 * thermal_cooling_device_unregister() must be called when the device is no
1217 * longer needed.
1218 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301219void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001220{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301221 int i;
1222 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001223 struct thermal_zone_device *tz;
1224 struct thermal_cooling_device *pos = NULL;
1225
1226 if (!cdev)
1227 return;
1228
1229 mutex_lock(&thermal_list_lock);
1230 list_for_each_entry(pos, &thermal_cdev_list, node)
1231 if (pos == cdev)
1232 break;
1233 if (pos != cdev) {
1234 /* thermal cooling device not found */
1235 mutex_unlock(&thermal_list_lock);
1236 return;
1237 }
1238 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301239
1240 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001241 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301242 if (tz->ops->unbind) {
1243 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001244 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301245 }
1246
1247 if (!tz->tzp || !tz->tzp->tbp)
1248 continue;
1249
1250 tzp = tz->tzp;
1251 for (i = 0; i < tzp->num_tbps; i++) {
1252 if (tzp->tbp[i].cdev == cdev) {
1253 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1254 tzp->tbp[i].cdev = NULL;
1255 }
1256 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001257 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301258
Zhang Rui203d3d42008-01-17 15:51:08 +08001259 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301260
Zhang Rui203d3d42008-01-17 15:51:08 +08001261 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001262 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001263 device_remove_file(&cdev->device, &dev_attr_max_state);
1264 device_remove_file(&cdev->device, &dev_attr_cur_state);
1265
1266 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1267 device_unregister(&cdev->device);
1268 return;
1269}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001270EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001271
Durgadoss Rdc765482012-09-18 11:05:00 +05301272void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001273{
1274 struct thermal_instance *instance;
1275 unsigned long target = 0;
1276
1277 /* cooling device is updated*/
1278 if (cdev->updated)
1279 return;
1280
Zhang Ruif4a821c2012-07-24 16:56:21 +08001281 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001282 /* Make sure cdev enters the deepest cooling state */
1283 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
Aaron Lu06475b52013-12-02 13:54:26 +08001284 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1285 instance->tz->id, instance->target);
Zhang Ruice119f82012-06-27 14:13:04 +08001286 if (instance->target == THERMAL_NO_TARGET)
1287 continue;
1288 if (instance->target > target)
1289 target = instance->target;
1290 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001291 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001292 cdev->ops->set_cur_state(cdev, target);
1293 cdev->updated = true;
Aaron Lu06475b52013-12-02 13:54:26 +08001294 dev_dbg(&cdev->device, "set to state %lu\n", target);
Zhang Ruice119f82012-06-27 14:13:04 +08001295}
Durgadoss Rdc765482012-09-18 11:05:00 +05301296EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001297
Matthew Garrettb1569e92008-12-03 17:55:32 +00001298/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001299 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301300 * @tz: thermal zone device
1301 * @trip: indicates which trip point has been crossed
1302 *
1303 * This function handles the trip events from sensor drivers. It starts
1304 * throttling the cooling devices according to the policy configured.
1305 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1306 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1307 * The throttling policy is based on the configured platform data; if no
1308 * platform data is provided, this uses the step_wise throttling policy.
1309 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001310void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301311{
1312 handle_thermal_trip(tz, trip);
1313}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001314EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301315
1316/**
Eduardo Valentin269c1742013-04-23 21:48:19 +00001317 * create_trip_attrs() - create attributes for trip points
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001318 * @tz: the thermal zone device
1319 * @mask: Writeable trip point bitmap.
Eduardo Valentin269c1742013-04-23 21:48:19 +00001320 *
1321 * helper function to instantiate sysfs entries for every trip
1322 * point and its properties of a struct thermal_zone_device.
1323 *
1324 * Return: 0 on success, the proper error value otherwise.
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001325 */
1326static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1327{
1328 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001329 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001330
Durgadoss R27365a62012-07-25 10:10:59 +08001331 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001332 if (!tz->trip_type_attrs)
1333 return -ENOMEM;
1334
Durgadoss R27365a62012-07-25 10:10:59 +08001335 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001336 if (!tz->trip_temp_attrs) {
1337 kfree(tz->trip_type_attrs);
1338 return -ENOMEM;
1339 }
1340
Durgadoss R27365a62012-07-25 10:10:59 +08001341 if (tz->ops->get_trip_hyst) {
1342 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1343 if (!tz->trip_hyst_attrs) {
1344 kfree(tz->trip_type_attrs);
1345 kfree(tz->trip_temp_attrs);
1346 return -ENOMEM;
1347 }
1348 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001349
Durgadoss R27365a62012-07-25 10:10:59 +08001350
1351 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001352 /* create trip type attribute */
1353 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1354 "trip_point_%d_type", indx);
1355
1356 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1357 tz->trip_type_attrs[indx].attr.attr.name =
1358 tz->trip_type_attrs[indx].name;
1359 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1360 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1361
1362 device_create_file(&tz->device,
1363 &tz->trip_type_attrs[indx].attr);
1364
1365 /* create trip temp attribute */
1366 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1367 "trip_point_%d_temp", indx);
1368
1369 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1370 tz->trip_temp_attrs[indx].attr.attr.name =
1371 tz->trip_temp_attrs[indx].name;
1372 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1373 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1374 if (mask & (1 << indx)) {
1375 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1376 tz->trip_temp_attrs[indx].attr.store =
1377 trip_point_temp_store;
1378 }
1379
1380 device_create_file(&tz->device,
1381 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001382
1383 /* create Optional trip hyst attribute */
1384 if (!tz->ops->get_trip_hyst)
1385 continue;
1386 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1387 "trip_point_%d_hyst", indx);
1388
1389 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1390 tz->trip_hyst_attrs[indx].attr.attr.name =
1391 tz->trip_hyst_attrs[indx].name;
1392 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1393 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1394 if (tz->ops->set_trip_hyst) {
1395 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1396 tz->trip_hyst_attrs[indx].attr.store =
1397 trip_point_hyst_store;
1398 }
1399
1400 device_create_file(&tz->device,
1401 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001402 }
1403 return 0;
1404}
1405
1406static void remove_trip_attrs(struct thermal_zone_device *tz)
1407{
1408 int indx;
1409
1410 for (indx = 0; indx < tz->trips; indx++) {
1411 device_remove_file(&tz->device,
1412 &tz->trip_type_attrs[indx].attr);
1413 device_remove_file(&tz->device,
1414 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001415 if (tz->ops->get_trip_hyst)
1416 device_remove_file(&tz->device,
1417 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001418 }
1419 kfree(tz->trip_type_attrs);
1420 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001421 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001422}
1423
1424/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001425 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001426 * @type: the thermal zone device type
1427 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001428 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001429 * @devdata: private device data
1430 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301431 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001432 * @passive_delay: number of milliseconds to wait between polls when
1433 * performing passive cooling
1434 * @polling_delay: number of milliseconds to wait between polls when checking
1435 * whether trip points have been crossed (0 for interrupt
1436 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001437 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001438 * This interface function adds a new thermal zone device (sensor) to
1439 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1440 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001441 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001442 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001443 *
1444 * Return: a pointer to the created struct thermal_zone_device or an
1445 * in case of error, an ERR_PTR. Caller must check return value with
1446 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001447 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001448struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001449 int trips, int mask, void *devdata,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001450 struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301451 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001452 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001453{
1454 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001455 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001456 int result;
1457 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001458 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001459
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001460 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001461 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001462
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001463 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001464 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001465
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001466 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001467 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001468
Jonghwa Lee83720d02013-05-18 09:50:26 +00001469 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001470 return ERR_PTR(-EINVAL);
1471
Zhang Rui203d3d42008-01-17 15:51:08 +08001472 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1473 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001474 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001475
Zhang Rui2d374132012-06-27 10:09:00 +08001476 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001477 idr_init(&tz->idr);
1478 mutex_init(&tz->lock);
1479 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1480 if (result) {
1481 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001482 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001483 }
1484
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001485 strlcpy(tz->type, type ? : "", sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001486 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301487 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001488 tz->device.class = &thermal_class;
1489 tz->devdata = devdata;
1490 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001491 tz->passive_delay = passive_delay;
1492 tz->polling_delay = polling_delay;
1493
Kay Sievers354655e2009-01-06 10:44:37 -08001494 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001495 result = device_register(&tz->device);
1496 if (result) {
1497 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1498 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001499 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001500 }
1501
1502 /* sys I/F */
1503 if (type) {
1504 result = device_create_file(&tz->device, &dev_attr_type);
1505 if (result)
1506 goto unregister;
1507 }
1508
1509 result = device_create_file(&tz->device, &dev_attr_temp);
1510 if (result)
1511 goto unregister;
1512
1513 if (ops->get_mode) {
1514 result = device_create_file(&tz->device, &dev_attr_mode);
1515 if (result)
1516 goto unregister;
1517 }
1518
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001519 result = create_trip_attrs(tz, mask);
1520 if (result)
1521 goto unregister;
1522
Zhang Rui203d3d42008-01-17 15:51:08 +08001523 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001524 tz->ops->get_trip_type(tz, count, &trip_type);
1525 if (trip_type == THERMAL_TRIP_PASSIVE)
1526 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001527 }
1528
Durgadoss R5a2c0902012-09-18 11:04:58 +05301529 if (!passive) {
1530 result = device_create_file(&tz->device, &dev_attr_passive);
1531 if (result)
1532 goto unregister;
1533 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001534
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001535#ifdef CONFIG_THERMAL_EMULATION
1536 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1537 if (result)
1538 goto unregister;
1539#endif
Durgadoss R5a2c0902012-09-18 11:04:58 +05301540 /* Create policy attribute */
1541 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001542 if (result)
1543 goto unregister;
1544
Durgadoss Ra4a15482012-09-18 11:04:57 +05301545 /* Update 'this' zone's governor information */
1546 mutex_lock(&thermal_governor_lock);
1547
1548 if (tz->tzp)
1549 tz->governor = __find_governor(tz->tzp->governor_name);
1550 else
Zhang Ruif2234bc2014-01-24 10:23:19 +08001551 tz->governor = def_governor;
Durgadoss Ra4a15482012-09-18 11:04:57 +05301552
1553 mutex_unlock(&thermal_governor_lock);
1554
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04001555 if (!tz->tzp || !tz->tzp->no_hwmon) {
1556 result = thermal_add_hwmon_sysfs(tz);
1557 if (result)
1558 goto unregister;
1559 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08001560
Zhang Rui203d3d42008-01-17 15:51:08 +08001561 mutex_lock(&thermal_list_lock);
1562 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001563 mutex_unlock(&thermal_list_lock);
1564
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301565 /* Bind cooling devices for this zone */
1566 bind_tz(tz);
1567
Matthew Garrettb1569e92008-12-03 17:55:32 +00001568 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1569
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001570 if (!tz->ops->get_temp)
1571 thermal_zone_device_set_polling(tz, 0);
1572
Matthew Garrettb1569e92008-12-03 17:55:32 +00001573 thermal_zone_device_update(tz);
1574
Zhang Rui203d3d42008-01-17 15:51:08 +08001575 if (!result)
1576 return tz;
1577
Joe Perchescaca8b802012-03-21 12:55:02 -07001578unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001579 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1580 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001581 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001582}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001583EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001584
1585/**
1586 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001587 * @tz: the thermal zone device to remove
1588 */
1589void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1590{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301591 int i;
1592 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001593 struct thermal_cooling_device *cdev;
1594 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001595
1596 if (!tz)
1597 return;
1598
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301599 tzp = tz->tzp;
1600
Zhang Rui203d3d42008-01-17 15:51:08 +08001601 mutex_lock(&thermal_list_lock);
1602 list_for_each_entry(pos, &thermal_tz_list, node)
1603 if (pos == tz)
1604 break;
1605 if (pos != tz) {
1606 /* thermal zone device not found */
1607 mutex_unlock(&thermal_list_lock);
1608 return;
1609 }
1610 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301611
1612 /* Unbind all cdevs associated with 'this' thermal zone */
1613 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1614 if (tz->ops->unbind) {
1615 tz->ops->unbind(tz, cdev);
1616 continue;
1617 }
1618
1619 if (!tzp || !tzp->tbp)
1620 break;
1621
1622 for (i = 0; i < tzp->num_tbps; i++) {
1623 if (tzp->tbp[i].cdev == cdev) {
1624 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1625 tzp->tbp[i].cdev = NULL;
1626 }
1627 }
1628 }
1629
Zhang Rui203d3d42008-01-17 15:51:08 +08001630 mutex_unlock(&thermal_list_lock);
1631
Matthew Garrettb1569e92008-12-03 17:55:32 +00001632 thermal_zone_device_set_polling(tz, 0);
1633
Zhang Rui203d3d42008-01-17 15:51:08 +08001634 if (tz->type[0])
1635 device_remove_file(&tz->device, &dev_attr_type);
1636 device_remove_file(&tz->device, &dev_attr_temp);
1637 if (tz->ops->get_mode)
1638 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301639 device_remove_file(&tz->device, &dev_attr_policy);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001640 remove_trip_attrs(tz);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301641 tz->governor = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001642
Zhang Ruie68b16a2008-04-21 16:07:52 +08001643 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001644 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1645 idr_destroy(&tz->idr);
1646 mutex_destroy(&tz->lock);
1647 device_unregister(&tz->device);
1648 return;
1649}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001650EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001651
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001652/**
1653 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1654 * @name: thermal zone name to fetch the temperature
1655 *
1656 * When only one zone is found with the passed name, returns a reference to it.
1657 *
1658 * Return: On success returns a reference to an unique thermal zone with
1659 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1660 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1661 */
1662struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1663{
1664 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1665 unsigned int found = 0;
1666
1667 if (!name)
1668 goto exit;
1669
1670 mutex_lock(&thermal_list_lock);
1671 list_for_each_entry(pos, &thermal_tz_list, node)
1672 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1673 found++;
1674 ref = pos;
1675 }
1676 mutex_unlock(&thermal_list_lock);
1677
1678 /* nothing has been found, thus an error code for it */
1679 if (found == 0)
1680 ref = ERR_PTR(-ENODEV);
1681 else if (found > 1)
1682 /* Success only when an unique zone is found */
1683 ref = ERR_PTR(-EEXIST);
1684
1685exit:
1686 return ref;
1687}
1688EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1689
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001690#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01001691static const struct genl_multicast_group thermal_event_mcgrps[] = {
1692 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1693};
1694
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001695static struct genl_family thermal_event_genl_family = {
1696 .id = GENL_ID_GENERATE,
1697 .name = THERMAL_GENL_FAMILY_NAME,
1698 .version = THERMAL_GENL_VERSION,
1699 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001700 .mcgrps = thermal_event_mcgrps,
1701 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001702};
1703
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001704int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1705 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301706{
1707 struct sk_buff *skb;
1708 struct nlattr *attr;
1709 struct thermal_genl_event *thermal_event;
1710 void *msg_header;
1711 int size;
1712 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001713 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301714
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001715 if (!tz)
1716 return -EINVAL;
1717
R.Durgadoss4cb18722010-10-27 03:33:29 +05301718 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001719 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1720 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301721
1722 skb = genlmsg_new(size, GFP_ATOMIC);
1723 if (!skb)
1724 return -ENOMEM;
1725
1726 /* add the genetlink message header */
1727 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1728 &thermal_event_genl_family, 0,
1729 THERMAL_GENL_CMD_EVENT);
1730 if (!msg_header) {
1731 nlmsg_free(skb);
1732 return -ENOMEM;
1733 }
1734
1735 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001736 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1737 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301738
1739 if (!attr) {
1740 nlmsg_free(skb);
1741 return -EINVAL;
1742 }
1743
1744 thermal_event = nla_data(attr);
1745 if (!thermal_event) {
1746 nlmsg_free(skb);
1747 return -EINVAL;
1748 }
1749
1750 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1751
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001752 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301753 thermal_event->event = event;
1754
1755 /* send multicast genetlink message */
1756 result = genlmsg_end(skb, msg_header);
1757 if (result < 0) {
1758 nlmsg_free(skb);
1759 return result;
1760 }
1761
Johannes Berg68eb5502013-11-19 15:19:38 +01001762 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001763 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301764 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001765 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301766
1767 return result;
1768}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001769EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301770
1771static int genetlink_init(void)
1772{
Johannes Berg2a94fe42013-11-19 15:19:39 +01001773 return genl_register_family(&thermal_event_genl_family);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301774}
1775
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001776static void genetlink_exit(void)
1777{
1778 genl_unregister_family(&thermal_event_genl_family);
1779}
1780#else /* !CONFIG_NET */
1781static inline int genetlink_init(void) { return 0; }
1782static inline void genetlink_exit(void) {}
1783#endif /* !CONFIG_NET */
1784
Zhang Rui80a26a52013-03-26 16:38:29 +08001785static int __init thermal_register_governors(void)
1786{
1787 int result;
1788
1789 result = thermal_gov_step_wise_register();
1790 if (result)
1791 return result;
1792
1793 result = thermal_gov_fair_share_register();
1794 if (result)
1795 return result;
1796
1797 return thermal_gov_user_space_register();
1798}
1799
1800static void thermal_unregister_governors(void)
1801{
1802 thermal_gov_step_wise_unregister();
1803 thermal_gov_fair_share_unregister();
1804 thermal_gov_user_space_unregister();
1805}
1806
Zhang Rui203d3d42008-01-17 15:51:08 +08001807static int __init thermal_init(void)
1808{
Zhang Rui80a26a52013-03-26 16:38:29 +08001809 int result;
1810
1811 result = thermal_register_governors();
1812 if (result)
1813 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08001814
1815 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001816 if (result)
1817 goto unregister_governors;
1818
R.Durgadoss4cb18722010-10-27 03:33:29 +05301819 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08001820 if (result)
1821 goto unregister_class;
1822
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001823 result = of_parse_thermal_zones();
1824 if (result)
1825 goto exit_netlink;
1826
Zhang Rui80a26a52013-03-26 16:38:29 +08001827 return 0;
1828
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001829exit_netlink:
1830 genetlink_exit();
Zhang Rui80a26a52013-03-26 16:38:29 +08001831unregister_governors:
1832 thermal_unregister_governors();
1833unregister_class:
1834 class_unregister(&thermal_class);
1835error:
1836 idr_destroy(&thermal_tz_idr);
1837 idr_destroy(&thermal_cdev_idr);
1838 mutex_destroy(&thermal_idr_lock);
1839 mutex_destroy(&thermal_list_lock);
1840 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001841 return result;
1842}
1843
1844static void __exit thermal_exit(void)
1845{
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001846 of_thermal_destroy_zones();
Zhang Rui80a26a52013-03-26 16:38:29 +08001847 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001848 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001849 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08001850 idr_destroy(&thermal_tz_idr);
1851 idr_destroy(&thermal_cdev_idr);
1852 mutex_destroy(&thermal_idr_lock);
1853 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08001854 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001855}
1856
R.Durgadoss4cb18722010-10-27 03:33:29 +05301857fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001858module_exit(thermal_exit);