blob: edc0cb88f1d0bb4f43270684619c15dc0427ec47 [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>
Eduardo Valentin0b871482013-09-26 15:55:01 -040036#include <linux/string.h>
37#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 Valentin3accd1f2013-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 Ruife174a02014-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 Ruife174a02014-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 Ruife174a02014-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 Ruife174a02014-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 Ruife174a02014-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
172 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
173 if (tz->temperature > tz->last_temperature)
174 trend = THERMAL_TREND_RAISING;
175 else if (tz->temperature < tz->last_temperature)
176 trend = THERMAL_TREND_DROPPING;
177 else
178 trend = THERMAL_TREND_STABLE;
179 }
180
181 return trend;
182}
183EXPORT_SYMBOL(get_tz_trend);
184
185struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
186 struct thermal_cooling_device *cdev, int trip)
187{
188 struct thermal_instance *pos = NULL;
189 struct thermal_instance *target_instance = NULL;
190
191 mutex_lock(&tz->lock);
192 mutex_lock(&cdev->lock);
193
194 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
195 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
196 target_instance = pos;
197 break;
198 }
199 }
200
201 mutex_unlock(&cdev->lock);
202 mutex_unlock(&tz->lock);
203
204 return target_instance;
205}
206EXPORT_SYMBOL(get_thermal_instance);
207
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530208static void print_bind_err_msg(struct thermal_zone_device *tz,
209 struct thermal_cooling_device *cdev, int ret)
210{
211 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
212 tz->type, cdev->type, ret);
213}
214
215static void __bind(struct thermal_zone_device *tz, int mask,
216 struct thermal_cooling_device *cdev)
217{
218 int i, ret;
219
220 for (i = 0; i < tz->trips; i++) {
221 if (mask & (1 << i)) {
222 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
223 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
224 if (ret)
225 print_bind_err_msg(tz, cdev, ret);
226 }
227 }
228}
229
230static void __unbind(struct thermal_zone_device *tz, int mask,
231 struct thermal_cooling_device *cdev)
232{
233 int i;
234
235 for (i = 0; i < tz->trips; i++)
236 if (mask & (1 << i))
237 thermal_zone_unbind_cooling_device(tz, i, cdev);
238}
239
240static void bind_cdev(struct thermal_cooling_device *cdev)
241{
242 int i, ret;
243 const struct thermal_zone_params *tzp;
244 struct thermal_zone_device *pos = NULL;
245
246 mutex_lock(&thermal_list_lock);
247
248 list_for_each_entry(pos, &thermal_tz_list, node) {
249 if (!pos->tzp && !pos->ops->bind)
250 continue;
251
Ni Waded05bc712013-11-06 14:30:13 +0800252 if (pos->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530253 ret = pos->ops->bind(pos, cdev);
254 if (ret)
255 print_bind_err_msg(pos, cdev, ret);
Ni Waded05bc712013-11-06 14:30:13 +0800256 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530257 }
258
259 tzp = pos->tzp;
Hugh Dickins791700c2012-09-27 16:48:28 -0700260 if (!tzp || !tzp->tbp)
261 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530262
263 for (i = 0; i < tzp->num_tbps; i++) {
264 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
265 continue;
266 if (tzp->tbp[i].match(pos, cdev))
267 continue;
268 tzp->tbp[i].cdev = cdev;
269 __bind(pos, tzp->tbp[i].trip_mask, cdev);
270 }
271 }
272
273 mutex_unlock(&thermal_list_lock);
274}
275
276static void bind_tz(struct thermal_zone_device *tz)
277{
278 int i, ret;
279 struct thermal_cooling_device *pos = NULL;
280 const struct thermal_zone_params *tzp = tz->tzp;
281
282 if (!tzp && !tz->ops->bind)
283 return;
284
285 mutex_lock(&thermal_list_lock);
286
Ni Waded05bc712013-11-06 14:30:13 +0800287 /* If there is ops->bind, try to use ops->bind */
288 if (tz->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530289 list_for_each_entry(pos, &thermal_cdev_list, node) {
290 ret = tz->ops->bind(tz, pos);
291 if (ret)
292 print_bind_err_msg(tz, pos, ret);
293 }
294 goto exit;
295 }
296
Hugh Dickins791700c2012-09-27 16:48:28 -0700297 if (!tzp || !tzp->tbp)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530298 goto exit;
299
300 list_for_each_entry(pos, &thermal_cdev_list, node) {
301 for (i = 0; i < tzp->num_tbps; i++) {
302 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
303 continue;
304 if (tzp->tbp[i].match(tz, pos))
305 continue;
306 tzp->tbp[i].cdev = pos;
307 __bind(tz, tzp->tbp[i].trip_mask, pos);
308 }
309 }
310exit:
311 mutex_unlock(&thermal_list_lock);
312}
313
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530314static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
315 int delay)
316{
317 if (delay > 1000)
318 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
319 round_jiffies(msecs_to_jiffies(delay)));
320 else if (delay)
321 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
322 msecs_to_jiffies(delay));
323 else
324 cancel_delayed_work(&tz->poll_queue);
325}
326
327static void monitor_thermal_zone(struct thermal_zone_device *tz)
328{
329 mutex_lock(&tz->lock);
330
331 if (tz->passive)
332 thermal_zone_device_set_polling(tz, tz->passive_delay);
333 else if (tz->polling_delay)
334 thermal_zone_device_set_polling(tz, tz->polling_delay);
335 else
336 thermal_zone_device_set_polling(tz, 0);
337
338 mutex_unlock(&tz->lock);
339}
340
341static void handle_non_critical_trips(struct thermal_zone_device *tz,
342 int trip, enum thermal_trip_type trip_type)
343{
Zhang Ruife174a02014-01-24 10:23:19 +0800344 tz->governor ? tz->governor->throttle(tz, trip) :
345 def_governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530346}
347
348static void handle_critical_trips(struct thermal_zone_device *tz,
349 int trip, enum thermal_trip_type trip_type)
350{
351 long trip_temp;
352
353 tz->ops->get_trip_temp(tz, trip, &trip_temp);
354
355 /* If we have not crossed the trip_temp, we do not care. */
356 if (tz->temperature < trip_temp)
357 return;
358
359 if (tz->ops->notify)
360 tz->ops->notify(tz, trip, trip_type);
361
362 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000363 dev_emerg(&tz->device,
364 "critical temperature reached(%d C),shutting down\n",
365 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530366 orderly_poweroff(true);
367 }
368}
369
370static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
371{
372 enum thermal_trip_type type;
373
374 tz->ops->get_trip_type(tz, trip, &type);
375
376 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
377 handle_critical_trips(tz, trip, type);
378 else
379 handle_non_critical_trips(tz, trip, type);
380 /*
381 * Alright, we handled this trip successfully.
382 * So, start monitoring again.
383 */
384 monitor_thermal_zone(tz);
385}
386
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000387/**
388 * thermal_zone_get_temp() - returns its the temperature of thermal zone
389 * @tz: a valid pointer to a struct thermal_zone_device
390 * @temp: a valid pointer to where to store the resulting temperature.
391 *
392 * When a valid thermal zone reference is passed, it will fetch its
393 * temperature and fill @temp.
394 *
395 * Return: On success returns 0, an error code otherwise
396 */
397int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000398{
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000399 int ret = -EINVAL;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800400#ifdef CONFIG_THERMAL_EMULATION
401 int count;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000402 unsigned long crit_temp = -1UL;
403 enum thermal_trip_type type;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800404#endif
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000405
Eduardo Valentin510a7732013-09-12 19:15:44 -0400406 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000407 goto exit;
408
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000409 mutex_lock(&tz->lock);
410
411 ret = tz->ops->get_temp(tz, temp);
412#ifdef CONFIG_THERMAL_EMULATION
413 if (!tz->emul_temperature)
414 goto skip_emul;
415
416 for (count = 0; count < tz->trips; count++) {
417 ret = tz->ops->get_trip_type(tz, count, &type);
418 if (!ret && type == THERMAL_TRIP_CRITICAL) {
419 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
420 break;
421 }
422 }
423
424 if (ret)
425 goto skip_emul;
426
427 if (*temp < crit_temp)
428 *temp = tz->emul_temperature;
429skip_emul:
430#endif
431 mutex_unlock(&tz->lock);
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000432exit:
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000433 return ret;
434}
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000435EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000436
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530437static void update_temperature(struct thermal_zone_device *tz)
438{
439 long temp;
440 int ret;
441
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000442 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530443 if (ret) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000444 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
445 tz->id);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000446 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530447 }
448
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000449 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530450 tz->last_temperature = tz->temperature;
451 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530452 mutex_unlock(&tz->lock);
Aaron Lu0e0eabe2013-12-02 13:54:26 +0800453
454 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
455 tz->last_temperature, tz->temperature);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530456}
457
458void thermal_zone_device_update(struct thermal_zone_device *tz)
459{
460 int count;
461
Eduardo Valentin510a7732013-09-12 19:15:44 -0400462 if (!tz->ops->get_temp)
463 return;
464
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530465 update_temperature(tz);
466
467 for (count = 0; count < tz->trips; count++)
468 handle_thermal_trip(tz, count);
469}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000470EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530471
472static void thermal_zone_device_check(struct work_struct *work)
473{
474 struct thermal_zone_device *tz = container_of(work, struct
475 thermal_zone_device,
476 poll_queue.work);
477 thermal_zone_device_update(tz);
478}
479
Zhang Rui203d3d42008-01-17 15:51:08 +0800480/* sys I/F for thermal zone */
481
482#define to_thermal_zone(_dev) \
483 container_of(_dev, struct thermal_zone_device, device)
484
485static ssize_t
486type_show(struct device *dev, struct device_attribute *attr, char *buf)
487{
488 struct thermal_zone_device *tz = to_thermal_zone(dev);
489
490 return sprintf(buf, "%s\n", tz->type);
491}
492
493static ssize_t
494temp_show(struct device *dev, struct device_attribute *attr, char *buf)
495{
496 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000497 long temperature;
498 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800499
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000500 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000501
502 if (ret)
503 return ret;
504
505 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800506}
507
508static ssize_t
509mode_show(struct device *dev, struct device_attribute *attr, char *buf)
510{
511 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000512 enum thermal_device_mode mode;
513 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800514
515 if (!tz->ops->get_mode)
516 return -EPERM;
517
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000518 result = tz->ops->get_mode(tz, &mode);
519 if (result)
520 return result;
521
522 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
523 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800524}
525
526static ssize_t
527mode_store(struct device *dev, struct device_attribute *attr,
528 const char *buf, size_t count)
529{
530 struct thermal_zone_device *tz = to_thermal_zone(dev);
531 int result;
532
533 if (!tz->ops->set_mode)
534 return -EPERM;
535
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530536 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000537 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530538 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000539 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
540 else
541 result = -EINVAL;
542
Zhang Rui203d3d42008-01-17 15:51:08 +0800543 if (result)
544 return result;
545
546 return count;
547}
548
549static ssize_t
550trip_point_type_show(struct device *dev, struct device_attribute *attr,
551 char *buf)
552{
553 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000554 enum thermal_trip_type type;
555 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800556
557 if (!tz->ops->get_trip_type)
558 return -EPERM;
559
560 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
561 return -EINVAL;
562
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000563 result = tz->ops->get_trip_type(tz, trip, &type);
564 if (result)
565 return result;
566
567 switch (type) {
568 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300569 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000570 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300571 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000572 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300573 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000574 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300575 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000576 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300577 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000578 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800579}
580
581static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800582trip_point_temp_store(struct device *dev, struct device_attribute *attr,
583 const char *buf, size_t count)
584{
585 struct thermal_zone_device *tz = to_thermal_zone(dev);
586 int trip, ret;
587 unsigned long temperature;
588
589 if (!tz->ops->set_trip_temp)
590 return -EPERM;
591
592 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
593 return -EINVAL;
594
595 if (kstrtoul(buf, 10, &temperature))
596 return -EINVAL;
597
598 ret = tz->ops->set_trip_temp(tz, trip, temperature);
599
600 return ret ? ret : count;
601}
602
603static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800604trip_point_temp_show(struct device *dev, struct device_attribute *attr,
605 char *buf)
606{
607 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000608 int trip, ret;
609 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800610
611 if (!tz->ops->get_trip_temp)
612 return -EPERM;
613
614 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
615 return -EINVAL;
616
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000617 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
618
619 if (ret)
620 return ret;
621
622 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800623}
624
Matthew Garrett03a971a2008-12-03 18:00:38 +0000625static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800626trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
627 const char *buf, size_t count)
628{
629 struct thermal_zone_device *tz = to_thermal_zone(dev);
630 int trip, ret;
631 unsigned long temperature;
632
633 if (!tz->ops->set_trip_hyst)
634 return -EPERM;
635
636 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
637 return -EINVAL;
638
639 if (kstrtoul(buf, 10, &temperature))
640 return -EINVAL;
641
642 /*
643 * We are not doing any check on the 'temperature' value
644 * here. The driver implementing 'set_trip_hyst' has to
645 * take care of this.
646 */
647 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
648
649 return ret ? ret : count;
650}
651
652static ssize_t
653trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
654 char *buf)
655{
656 struct thermal_zone_device *tz = to_thermal_zone(dev);
657 int trip, ret;
658 unsigned long temperature;
659
660 if (!tz->ops->get_trip_hyst)
661 return -EPERM;
662
663 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
664 return -EINVAL;
665
666 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
667
668 return ret ? ret : sprintf(buf, "%ld\n", temperature);
669}
670
671static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000672passive_store(struct device *dev, struct device_attribute *attr,
673 const char *buf, size_t count)
674{
675 struct thermal_zone_device *tz = to_thermal_zone(dev);
676 struct thermal_cooling_device *cdev = NULL;
677 int state;
678
679 if (!sscanf(buf, "%d\n", &state))
680 return -EINVAL;
681
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100682 /* sanity check: values below 1000 millicelcius don't make sense
683 * and can cause the system to go into a thermal heart attack
684 */
685 if (state && state < 1000)
686 return -EINVAL;
687
Matthew Garrett03a971a2008-12-03 18:00:38 +0000688 if (state && !tz->forced_passive) {
689 mutex_lock(&thermal_list_lock);
690 list_for_each_entry(cdev, &thermal_cdev_list, node) {
691 if (!strncmp("Processor", cdev->type,
692 sizeof("Processor")))
693 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800694 THERMAL_TRIPS_NONE, cdev,
695 THERMAL_NO_LIMIT,
696 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000697 }
698 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100699 if (!tz->passive_delay)
700 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000701 } else if (!state && tz->forced_passive) {
702 mutex_lock(&thermal_list_lock);
703 list_for_each_entry(cdev, &thermal_cdev_list, node) {
704 if (!strncmp("Processor", cdev->type,
705 sizeof("Processor")))
706 thermal_zone_unbind_cooling_device(tz,
707 THERMAL_TRIPS_NONE,
708 cdev);
709 }
710 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100711 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000712 }
713
Matthew Garrett03a971a2008-12-03 18:00:38 +0000714 tz->forced_passive = state;
715
716 thermal_zone_device_update(tz);
717
718 return count;
719}
720
721static ssize_t
722passive_show(struct device *dev, struct device_attribute *attr,
723 char *buf)
724{
725 struct thermal_zone_device *tz = to_thermal_zone(dev);
726
727 return sprintf(buf, "%d\n", tz->forced_passive);
728}
729
Durgadoss R5a2c0902012-09-18 11:04:58 +0530730static ssize_t
731policy_store(struct device *dev, struct device_attribute *attr,
732 const char *buf, size_t count)
733{
734 int ret = -EINVAL;
735 struct thermal_zone_device *tz = to_thermal_zone(dev);
736 struct thermal_governor *gov;
737
738 mutex_lock(&thermal_governor_lock);
739
740 gov = __find_governor(buf);
741 if (!gov)
742 goto exit;
743
744 tz->governor = gov;
745 ret = count;
746
747exit:
748 mutex_unlock(&thermal_governor_lock);
749 return ret;
750}
751
752static ssize_t
753policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
754{
755 struct thermal_zone_device *tz = to_thermal_zone(dev);
756
757 return sprintf(buf, "%s\n", tz->governor->name);
758}
759
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000760#ifdef CONFIG_THERMAL_EMULATION
761static ssize_t
762emul_temp_store(struct device *dev, struct device_attribute *attr,
763 const char *buf, size_t count)
764{
765 struct thermal_zone_device *tz = to_thermal_zone(dev);
766 int ret = 0;
767 unsigned long temperature;
768
769 if (kstrtoul(buf, 10, &temperature))
770 return -EINVAL;
771
772 if (!tz->ops->set_emul_temp) {
773 mutex_lock(&tz->lock);
774 tz->emul_temperature = temperature;
775 mutex_unlock(&tz->lock);
776 } else {
777 ret = tz->ops->set_emul_temp(tz, temperature);
778 }
779
lan,Tianyu3a683052014-01-02 15:47:54 +0800780 if (!ret)
781 thermal_zone_device_update(tz);
782
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000783 return ret ? ret : count;
784}
785static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
786#endif/*CONFIG_THERMAL_EMULATION*/
787
Zhang Rui203d3d42008-01-17 15:51:08 +0800788static DEVICE_ATTR(type, 0444, type_show, NULL);
789static DEVICE_ATTR(temp, 0444, temp_show, NULL);
790static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700791static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530792static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800793
Zhang Rui203d3d42008-01-17 15:51:08 +0800794/* sys I/F for cooling device */
795#define to_cooling_device(_dev) \
796 container_of(_dev, struct thermal_cooling_device, device)
797
798static ssize_t
799thermal_cooling_device_type_show(struct device *dev,
800 struct device_attribute *attr, char *buf)
801{
802 struct thermal_cooling_device *cdev = to_cooling_device(dev);
803
804 return sprintf(buf, "%s\n", cdev->type);
805}
806
807static ssize_t
808thermal_cooling_device_max_state_show(struct device *dev,
809 struct device_attribute *attr, char *buf)
810{
811 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000812 unsigned long state;
813 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800814
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000815 ret = cdev->ops->get_max_state(cdev, &state);
816 if (ret)
817 return ret;
818 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800819}
820
821static ssize_t
822thermal_cooling_device_cur_state_show(struct device *dev,
823 struct device_attribute *attr, char *buf)
824{
825 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000826 unsigned long state;
827 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800828
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000829 ret = cdev->ops->get_cur_state(cdev, &state);
830 if (ret)
831 return ret;
832 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800833}
834
835static ssize_t
836thermal_cooling_device_cur_state_store(struct device *dev,
837 struct device_attribute *attr,
838 const char *buf, size_t count)
839{
840 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000841 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800842 int result;
843
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000844 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800845 return -EINVAL;
846
Roel Kluinedb94912009-12-15 22:46:50 +0100847 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800848 return -EINVAL;
849
850 result = cdev->ops->set_cur_state(cdev, state);
851 if (result)
852 return result;
853 return count;
854}
855
856static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500857__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800858static DEVICE_ATTR(max_state, 0444,
859 thermal_cooling_device_max_state_show, NULL);
860static DEVICE_ATTR(cur_state, 0644,
861 thermal_cooling_device_cur_state_show,
862 thermal_cooling_device_cur_state_store);
863
864static ssize_t
865thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500866 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800867{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800868 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800869
870 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800871 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800872
873 if (instance->trip == THERMAL_TRIPS_NONE)
874 return sprintf(buf, "-1\n");
875 else
876 return sprintf(buf, "%d\n", instance->trip);
877}
878
879/* Device management */
880
881/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000882 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
883 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +0800884 * @trip: indicates which trip point the cooling devices is
885 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000886 * @cdev: pointer to struct thermal_cooling_device
887 * @upper: the Maximum cooling state for this trip point.
888 * THERMAL_NO_LIMIT means no upper limit,
889 * and the cooling device can be in max_state.
890 * @lower: the Minimum cooling state can be used for this trip point.
891 * THERMAL_NO_LIMIT means no lower limit,
892 * and the cooling device can be in cooling state 0.
Len Brown543a9562008-02-07 16:55:08 -0500893 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000894 * This interface function bind a thermal cooling device to the certain trip
895 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500896 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000897 *
898 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800899 */
900int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
901 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800902 struct thermal_cooling_device *cdev,
903 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +0800904{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800905 struct thermal_instance *dev;
906 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500907 struct thermal_zone_device *pos1;
908 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800909 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800910 int result;
911
Len Brown543a9562008-02-07 16:55:08 -0500912 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800913 return -EINVAL;
914
Thomas Sujithc7516702008-02-15 00:58:50 -0500915 list_for_each_entry(pos1, &thermal_tz_list, node) {
916 if (pos1 == tz)
917 break;
918 }
919 list_for_each_entry(pos2, &thermal_cdev_list, node) {
920 if (pos2 == cdev)
921 break;
922 }
923
924 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800925 return -EINVAL;
926
Zhang Rui9d998422012-06-26 16:35:57 +0800927 cdev->ops->get_max_state(cdev, &max_state);
928
929 /* lower default 0, upper default max_state */
930 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
931 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
932
933 if (lower > upper || upper > max_state)
934 return -EINVAL;
935
Zhang Rui203d3d42008-01-17 15:51:08 +0800936 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800937 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800938 if (!dev)
939 return -ENOMEM;
940 dev->tz = tz;
941 dev->cdev = cdev;
942 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800943 dev->upper = upper;
944 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800945 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +0800946
Zhang Rui203d3d42008-01-17 15:51:08 +0800947 result = get_idr(&tz->idr, &tz->lock, &dev->id);
948 if (result)
949 goto free_mem;
950
951 sprintf(dev->name, "cdev%d", dev->id);
952 result =
953 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
954 if (result)
955 goto release_idr;
956
957 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700958 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800959 dev->attr.attr.name = dev->attr_name;
960 dev->attr.attr.mode = 0444;
961 dev->attr.show = thermal_cooling_device_trip_point_show;
962 result = device_create_file(&tz->device, &dev->attr);
963 if (result)
964 goto remove_symbol_link;
965
966 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800967 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800968 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +0800969 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
970 result = -EEXIST;
971 break;
972 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800973 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800974 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800975 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
976 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800977 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800978 mutex_unlock(&tz->lock);
979
980 if (!result)
981 return 0;
982
983 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -0700984remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800985 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -0700986release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +0800987 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -0700988free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800989 kfree(dev);
990 return result;
991}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000992EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +0800993
994/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000995 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
996 * thermal zone.
997 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +0800998 * @trip: indicates which trip point the cooling devices is
999 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001000 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -05001001 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001002 * This interface function unbind a thermal cooling device from the certain
1003 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001004 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001005 *
1006 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001007 */
1008int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1009 int trip,
1010 struct thermal_cooling_device *cdev)
1011{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001012 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001013
1014 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001015 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001016 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001017 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001018 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001019 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001020 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001021 mutex_unlock(&tz->lock);
1022 goto unbind;
1023 }
1024 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001025 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001026 mutex_unlock(&tz->lock);
1027
1028 return -ENODEV;
1029
Joe Perchescaca8b82012-03-21 12:55:02 -07001030unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +08001031 device_remove_file(&tz->device, &pos->attr);
1032 sysfs_remove_link(&tz->device.kobj, pos->name);
1033 release_idr(&tz->idr, &tz->lock, pos->id);
1034 kfree(pos);
1035 return 0;
1036}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001037EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001038
1039static void thermal_release(struct device *dev)
1040{
1041 struct thermal_zone_device *tz;
1042 struct thermal_cooling_device *cdev;
1043
Joe Perchescaca8b82012-03-21 12:55:02 -07001044 if (!strncmp(dev_name(dev), "thermal_zone",
1045 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001046 tz = to_thermal_zone(dev);
1047 kfree(tz);
1048 } else {
1049 cdev = to_cooling_device(dev);
1050 kfree(cdev);
1051 }
1052}
1053
1054static struct class thermal_class = {
1055 .name = "thermal",
1056 .dev_release = thermal_release,
1057};
1058
1059/**
Eduardo Valentin0b871482013-09-26 15:55:01 -04001060 * __thermal_cooling_device_register() - register a new thermal cooling device
1061 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +08001062 * @type: the thermal cooling device type.
1063 * @devdata: device private data.
1064 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001065 *
1066 * This interface function adds a new thermal cooling device (fan/processor/...)
1067 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1068 * to all the thermal zone devices registered at the same time.
Eduardo Valentin0b871482013-09-26 15:55:01 -04001069 * It also gives the opportunity to link the cooling device to a device tree
1070 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001071 *
1072 * Return: a pointer to the created struct thermal_cooling_device or an
1073 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001074 */
Eduardo Valentin0b871482013-09-26 15:55:01 -04001075static struct thermal_cooling_device *
1076__thermal_cooling_device_register(struct device_node *np,
1077 char *type, void *devdata,
1078 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001079{
1080 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001081 int result;
1082
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001083 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001084 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001085
1086 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001087 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001088 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001089
1090 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1091 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001092 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001093
1094 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1095 if (result) {
1096 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001097 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001098 }
1099
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001100 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +08001101 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001102 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentin0b871482013-09-26 15:55:01 -04001103 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +08001104 cdev->ops = ops;
Ni Wadeed835be2014-02-17 11:02:55 +08001105 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +08001106 cdev->device.class = &thermal_class;
1107 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001108 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001109 result = device_register(&cdev->device);
1110 if (result) {
1111 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1112 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001113 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001114 }
1115
1116 /* sys I/F */
1117 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001118 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001119 if (result)
1120 goto unregister;
1121 }
1122
1123 result = device_create_file(&cdev->device, &dev_attr_max_state);
1124 if (result)
1125 goto unregister;
1126
1127 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1128 if (result)
1129 goto unregister;
1130
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301131 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001132 mutex_lock(&thermal_list_lock);
1133 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001134 mutex_unlock(&thermal_list_lock);
1135
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301136 /* Update binding information for 'this' new cdev */
1137 bind_cdev(cdev);
1138
1139 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001140
Joe Perchescaca8b82012-03-21 12:55:02 -07001141unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001142 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1143 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001144 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001145}
Eduardo Valentin0b871482013-09-26 15:55:01 -04001146
1147/**
1148 * thermal_cooling_device_register() - register a new thermal cooling device
1149 * @type: the thermal cooling device type.
1150 * @devdata: device private data.
1151 * @ops: standard thermal cooling devices callbacks.
1152 *
1153 * This interface function adds a new thermal cooling device (fan/processor/...)
1154 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1155 * to all the thermal zone devices registered at the same time.
1156 *
1157 * Return: a pointer to the created struct thermal_cooling_device or an
1158 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1159 */
1160struct thermal_cooling_device *
1161thermal_cooling_device_register(char *type, void *devdata,
1162 const struct thermal_cooling_device_ops *ops)
1163{
1164 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1165}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001166EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001167
1168/**
Eduardo Valentin0b871482013-09-26 15:55:01 -04001169 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1170 * @np: a pointer to a device tree node.
1171 * @type: the thermal cooling device type.
1172 * @devdata: device private data.
1173 * @ops: standard thermal cooling devices callbacks.
1174 *
1175 * This function will register a cooling device with device tree node reference.
1176 * This interface function adds a new thermal cooling device (fan/processor/...)
1177 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1178 * to all the thermal zone devices registered at the same time.
1179 *
1180 * Return: a pointer to the created struct thermal_cooling_device or an
1181 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1182 */
1183struct thermal_cooling_device *
1184thermal_of_cooling_device_register(struct device_node *np,
1185 char *type, void *devdata,
1186 const struct thermal_cooling_device_ops *ops)
1187{
1188 return __thermal_cooling_device_register(np, type, devdata, ops);
1189}
1190EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1191
1192/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001193 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001194 * @cdev: the thermal cooling device to remove.
1195 *
1196 * thermal_cooling_device_unregister() must be called when the device is no
1197 * longer needed.
1198 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301199void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001200{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301201 int i;
1202 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001203 struct thermal_zone_device *tz;
1204 struct thermal_cooling_device *pos = NULL;
1205
1206 if (!cdev)
1207 return;
1208
1209 mutex_lock(&thermal_list_lock);
1210 list_for_each_entry(pos, &thermal_cdev_list, node)
1211 if (pos == cdev)
1212 break;
1213 if (pos != cdev) {
1214 /* thermal cooling device not found */
1215 mutex_unlock(&thermal_list_lock);
1216 return;
1217 }
1218 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301219
1220 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001221 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301222 if (tz->ops->unbind) {
1223 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001224 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301225 }
1226
1227 if (!tz->tzp || !tz->tzp->tbp)
1228 continue;
1229
1230 tzp = tz->tzp;
1231 for (i = 0; i < tzp->num_tbps; i++) {
1232 if (tzp->tbp[i].cdev == cdev) {
1233 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1234 tzp->tbp[i].cdev = NULL;
1235 }
1236 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001237 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301238
Zhang Rui203d3d42008-01-17 15:51:08 +08001239 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301240
Zhang Rui203d3d42008-01-17 15:51:08 +08001241 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001242 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001243 device_remove_file(&cdev->device, &dev_attr_max_state);
1244 device_remove_file(&cdev->device, &dev_attr_cur_state);
1245
1246 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1247 device_unregister(&cdev->device);
1248 return;
1249}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001250EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001251
Durgadoss Rdc765482012-09-18 11:05:00 +05301252void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001253{
1254 struct thermal_instance *instance;
1255 unsigned long target = 0;
1256
1257 /* cooling device is updated*/
1258 if (cdev->updated)
1259 return;
1260
Zhang Ruif4a821c2012-07-24 16:56:21 +08001261 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001262 /* Make sure cdev enters the deepest cooling state */
1263 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
Aaron Lu0e0eabe2013-12-02 13:54:26 +08001264 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1265 instance->tz->id, instance->target);
Zhang Ruice119f82012-06-27 14:13:04 +08001266 if (instance->target == THERMAL_NO_TARGET)
1267 continue;
1268 if (instance->target > target)
1269 target = instance->target;
1270 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001271 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001272 cdev->ops->set_cur_state(cdev, target);
1273 cdev->updated = true;
Aaron Lu0e0eabe2013-12-02 13:54:26 +08001274 dev_dbg(&cdev->device, "set to state %lu\n", target);
Zhang Ruice119f82012-06-27 14:13:04 +08001275}
Durgadoss Rdc765482012-09-18 11:05:00 +05301276EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001277
Matthew Garrettb1569e92008-12-03 17:55:32 +00001278/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001279 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301280 * @tz: thermal zone device
1281 * @trip: indicates which trip point has been crossed
1282 *
1283 * This function handles the trip events from sensor drivers. It starts
1284 * throttling the cooling devices according to the policy configured.
1285 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1286 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1287 * The throttling policy is based on the configured platform data; if no
1288 * platform data is provided, this uses the step_wise throttling policy.
1289 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001290void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301291{
1292 handle_thermal_trip(tz, trip);
1293}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001294EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301295
1296/**
Eduardo Valentin269c1742013-04-23 21:48:19 +00001297 * create_trip_attrs() - create attributes for trip points
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001298 * @tz: the thermal zone device
1299 * @mask: Writeable trip point bitmap.
Eduardo Valentin269c1742013-04-23 21:48:19 +00001300 *
1301 * helper function to instantiate sysfs entries for every trip
1302 * point and its properties of a struct thermal_zone_device.
1303 *
1304 * Return: 0 on success, the proper error value otherwise.
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001305 */
1306static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1307{
1308 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001309 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001310
Durgadoss R27365a62012-07-25 10:10:59 +08001311 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001312 if (!tz->trip_type_attrs)
1313 return -ENOMEM;
1314
Durgadoss R27365a62012-07-25 10:10:59 +08001315 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001316 if (!tz->trip_temp_attrs) {
1317 kfree(tz->trip_type_attrs);
1318 return -ENOMEM;
1319 }
1320
Durgadoss R27365a62012-07-25 10:10:59 +08001321 if (tz->ops->get_trip_hyst) {
1322 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1323 if (!tz->trip_hyst_attrs) {
1324 kfree(tz->trip_type_attrs);
1325 kfree(tz->trip_temp_attrs);
1326 return -ENOMEM;
1327 }
1328 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001329
Durgadoss R27365a62012-07-25 10:10:59 +08001330
1331 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001332 /* create trip type attribute */
1333 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1334 "trip_point_%d_type", indx);
1335
1336 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1337 tz->trip_type_attrs[indx].attr.attr.name =
1338 tz->trip_type_attrs[indx].name;
1339 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1340 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1341
1342 device_create_file(&tz->device,
1343 &tz->trip_type_attrs[indx].attr);
1344
1345 /* create trip temp attribute */
1346 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1347 "trip_point_%d_temp", indx);
1348
1349 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1350 tz->trip_temp_attrs[indx].attr.attr.name =
1351 tz->trip_temp_attrs[indx].name;
1352 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1353 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1354 if (mask & (1 << indx)) {
1355 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1356 tz->trip_temp_attrs[indx].attr.store =
1357 trip_point_temp_store;
1358 }
1359
1360 device_create_file(&tz->device,
1361 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001362
1363 /* create Optional trip hyst attribute */
1364 if (!tz->ops->get_trip_hyst)
1365 continue;
1366 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1367 "trip_point_%d_hyst", indx);
1368
1369 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1370 tz->trip_hyst_attrs[indx].attr.attr.name =
1371 tz->trip_hyst_attrs[indx].name;
1372 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1373 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1374 if (tz->ops->set_trip_hyst) {
1375 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1376 tz->trip_hyst_attrs[indx].attr.store =
1377 trip_point_hyst_store;
1378 }
1379
1380 device_create_file(&tz->device,
1381 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001382 }
1383 return 0;
1384}
1385
1386static void remove_trip_attrs(struct thermal_zone_device *tz)
1387{
1388 int indx;
1389
1390 for (indx = 0; indx < tz->trips; indx++) {
1391 device_remove_file(&tz->device,
1392 &tz->trip_type_attrs[indx].attr);
1393 device_remove_file(&tz->device,
1394 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001395 if (tz->ops->get_trip_hyst)
1396 device_remove_file(&tz->device,
1397 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001398 }
1399 kfree(tz->trip_type_attrs);
1400 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001401 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001402}
1403
1404/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001405 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001406 * @type: the thermal zone device type
1407 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001408 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001409 * @devdata: private device data
1410 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301411 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001412 * @passive_delay: number of milliseconds to wait between polls when
1413 * performing passive cooling
1414 * @polling_delay: number of milliseconds to wait between polls when checking
1415 * whether trip points have been crossed (0 for interrupt
1416 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001417 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001418 * This interface function adds a new thermal zone device (sensor) to
1419 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1420 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001421 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001422 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001423 *
1424 * Return: a pointer to the created struct thermal_zone_device or an
1425 * in case of error, an ERR_PTR. Caller must check return value with
1426 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001427 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001428struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001429 int trips, int mask, void *devdata,
Eduardo Valentin1c762ed2013-07-03 15:35:39 -04001430 struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301431 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001432 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001433{
1434 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001435 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001436 int result;
1437 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001438 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001439
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001440 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001441 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001442
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001443 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001444 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001445
Eduardo Valentin510a7732013-09-12 19:15:44 -04001446 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001447 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001448
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001449 if (trips > 0 && !ops->get_trip_type)
1450 return ERR_PTR(-EINVAL);
1451
Zhang Rui203d3d42008-01-17 15:51:08 +08001452 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1453 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001454 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001455
Zhang Rui2d374132012-06-27 10:09:00 +08001456 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001457 idr_init(&tz->idr);
1458 mutex_init(&tz->lock);
1459 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1460 if (result) {
1461 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001462 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001463 }
1464
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001465 strlcpy(tz->type, type ? : "", sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001466 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301467 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001468 tz->device.class = &thermal_class;
1469 tz->devdata = devdata;
1470 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001471 tz->passive_delay = passive_delay;
1472 tz->polling_delay = polling_delay;
1473
Kay Sievers354655e2009-01-06 10:44:37 -08001474 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001475 result = device_register(&tz->device);
1476 if (result) {
1477 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1478 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001479 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001480 }
1481
1482 /* sys I/F */
1483 if (type) {
1484 result = device_create_file(&tz->device, &dev_attr_type);
1485 if (result)
1486 goto unregister;
1487 }
1488
1489 result = device_create_file(&tz->device, &dev_attr_temp);
1490 if (result)
1491 goto unregister;
1492
1493 if (ops->get_mode) {
1494 result = device_create_file(&tz->device, &dev_attr_mode);
1495 if (result)
1496 goto unregister;
1497 }
1498
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001499 result = create_trip_attrs(tz, mask);
1500 if (result)
1501 goto unregister;
1502
Zhang Rui203d3d42008-01-17 15:51:08 +08001503 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001504 tz->ops->get_trip_type(tz, count, &trip_type);
1505 if (trip_type == THERMAL_TRIP_PASSIVE)
1506 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001507 }
1508
Durgadoss R5a2c0902012-09-18 11:04:58 +05301509 if (!passive) {
1510 result = device_create_file(&tz->device, &dev_attr_passive);
1511 if (result)
1512 goto unregister;
1513 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001514
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001515#ifdef CONFIG_THERMAL_EMULATION
1516 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1517 if (result)
1518 goto unregister;
1519#endif
Durgadoss R5a2c0902012-09-18 11:04:58 +05301520 /* Create policy attribute */
1521 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001522 if (result)
1523 goto unregister;
1524
Durgadoss Ra4a15482012-09-18 11:04:57 +05301525 /* Update 'this' zone's governor information */
1526 mutex_lock(&thermal_governor_lock);
1527
1528 if (tz->tzp)
1529 tz->governor = __find_governor(tz->tzp->governor_name);
1530 else
Zhang Ruife174a02014-01-24 10:23:19 +08001531 tz->governor = def_governor;
Durgadoss Ra4a15482012-09-18 11:04:57 +05301532
1533 mutex_unlock(&thermal_governor_lock);
1534
Eduardo Valentin9385d912013-08-15 11:34:17 -04001535 if (!tz->tzp || !tz->tzp->no_hwmon) {
1536 result = thermal_add_hwmon_sysfs(tz);
1537 if (result)
1538 goto unregister;
1539 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08001540
Zhang Rui203d3d42008-01-17 15:51:08 +08001541 mutex_lock(&thermal_list_lock);
1542 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001543 mutex_unlock(&thermal_list_lock);
1544
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301545 /* Bind cooling devices for this zone */
1546 bind_tz(tz);
1547
Matthew Garrettb1569e92008-12-03 17:55:32 +00001548 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1549
Eduardo Valentin510a7732013-09-12 19:15:44 -04001550 if (!tz->ops->get_temp)
1551 thermal_zone_device_set_polling(tz, 0);
1552
Matthew Garrettb1569e92008-12-03 17:55:32 +00001553 thermal_zone_device_update(tz);
1554
Zhang Rui203d3d42008-01-17 15:51:08 +08001555 if (!result)
1556 return tz;
1557
Joe Perchescaca8b82012-03-21 12:55:02 -07001558unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001559 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1560 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001561 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001562}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001563EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001564
1565/**
1566 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001567 * @tz: the thermal zone device to remove
1568 */
1569void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1570{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301571 int i;
1572 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001573 struct thermal_cooling_device *cdev;
1574 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001575
1576 if (!tz)
1577 return;
1578
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301579 tzp = tz->tzp;
1580
Zhang Rui203d3d42008-01-17 15:51:08 +08001581 mutex_lock(&thermal_list_lock);
1582 list_for_each_entry(pos, &thermal_tz_list, node)
1583 if (pos == tz)
1584 break;
1585 if (pos != tz) {
1586 /* thermal zone device not found */
1587 mutex_unlock(&thermal_list_lock);
1588 return;
1589 }
1590 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301591
1592 /* Unbind all cdevs associated with 'this' thermal zone */
1593 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1594 if (tz->ops->unbind) {
1595 tz->ops->unbind(tz, cdev);
1596 continue;
1597 }
1598
1599 if (!tzp || !tzp->tbp)
1600 break;
1601
1602 for (i = 0; i < tzp->num_tbps; i++) {
1603 if (tzp->tbp[i].cdev == cdev) {
1604 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1605 tzp->tbp[i].cdev = NULL;
1606 }
1607 }
1608 }
1609
Zhang Rui203d3d42008-01-17 15:51:08 +08001610 mutex_unlock(&thermal_list_lock);
1611
Matthew Garrettb1569e92008-12-03 17:55:32 +00001612 thermal_zone_device_set_polling(tz, 0);
1613
Zhang Rui203d3d42008-01-17 15:51:08 +08001614 if (tz->type[0])
1615 device_remove_file(&tz->device, &dev_attr_type);
1616 device_remove_file(&tz->device, &dev_attr_temp);
1617 if (tz->ops->get_mode)
1618 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301619 device_remove_file(&tz->device, &dev_attr_policy);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001620 remove_trip_attrs(tz);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301621 tz->governor = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001622
Zhang Ruie68b16a2008-04-21 16:07:52 +08001623 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001624 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1625 idr_destroy(&tz->idr);
1626 mutex_destroy(&tz->lock);
1627 device_unregister(&tz->device);
1628 return;
1629}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001630EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001631
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001632/**
1633 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1634 * @name: thermal zone name to fetch the temperature
1635 *
1636 * When only one zone is found with the passed name, returns a reference to it.
1637 *
1638 * Return: On success returns a reference to an unique thermal zone with
1639 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1640 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1641 */
1642struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1643{
1644 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1645 unsigned int found = 0;
1646
1647 if (!name)
1648 goto exit;
1649
1650 mutex_lock(&thermal_list_lock);
1651 list_for_each_entry(pos, &thermal_tz_list, node)
1652 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1653 found++;
1654 ref = pos;
1655 }
1656 mutex_unlock(&thermal_list_lock);
1657
1658 /* nothing has been found, thus an error code for it */
1659 if (found == 0)
1660 ref = ERR_PTR(-ENODEV);
1661 else if (found > 1)
1662 /* Success only when an unique zone is found */
1663 ref = ERR_PTR(-EEXIST);
1664
1665exit:
1666 return ref;
1667}
1668EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1669
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001670#ifdef CONFIG_NET
1671static struct genl_family thermal_event_genl_family = {
1672 .id = GENL_ID_GENERATE,
1673 .name = THERMAL_GENL_FAMILY_NAME,
1674 .version = THERMAL_GENL_VERSION,
1675 .maxattr = THERMAL_GENL_ATTR_MAX,
1676};
1677
1678static struct genl_multicast_group thermal_event_mcgrp = {
1679 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1680};
1681
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001682int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1683 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301684{
1685 struct sk_buff *skb;
1686 struct nlattr *attr;
1687 struct thermal_genl_event *thermal_event;
1688 void *msg_header;
1689 int size;
1690 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001691 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301692
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001693 if (!tz)
1694 return -EINVAL;
1695
R.Durgadoss4cb18722010-10-27 03:33:29 +05301696 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001697 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1698 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301699
1700 skb = genlmsg_new(size, GFP_ATOMIC);
1701 if (!skb)
1702 return -ENOMEM;
1703
1704 /* add the genetlink message header */
1705 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1706 &thermal_event_genl_family, 0,
1707 THERMAL_GENL_CMD_EVENT);
1708 if (!msg_header) {
1709 nlmsg_free(skb);
1710 return -ENOMEM;
1711 }
1712
1713 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001714 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1715 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301716
1717 if (!attr) {
1718 nlmsg_free(skb);
1719 return -EINVAL;
1720 }
1721
1722 thermal_event = nla_data(attr);
1723 if (!thermal_event) {
1724 nlmsg_free(skb);
1725 return -EINVAL;
1726 }
1727
1728 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1729
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001730 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301731 thermal_event->event = event;
1732
1733 /* send multicast genetlink message */
1734 result = genlmsg_end(skb, msg_header);
1735 if (result < 0) {
1736 nlmsg_free(skb);
1737 return result;
1738 }
1739
1740 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1741 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001742 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301743
1744 return result;
1745}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001746EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301747
1748static int genetlink_init(void)
1749{
1750 int result;
1751
1752 result = genl_register_family(&thermal_event_genl_family);
1753 if (result)
1754 return result;
1755
1756 result = genl_register_mc_group(&thermal_event_genl_family,
1757 &thermal_event_mcgrp);
1758 if (result)
1759 genl_unregister_family(&thermal_event_genl_family);
1760 return result;
1761}
1762
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001763static void genetlink_exit(void)
1764{
1765 genl_unregister_family(&thermal_event_genl_family);
1766}
1767#else /* !CONFIG_NET */
1768static inline int genetlink_init(void) { return 0; }
1769static inline void genetlink_exit(void) {}
1770#endif /* !CONFIG_NET */
1771
Zhang Rui80a26a52013-03-26 16:38:29 +08001772static int __init thermal_register_governors(void)
1773{
1774 int result;
1775
1776 result = thermal_gov_step_wise_register();
1777 if (result)
1778 return result;
1779
1780 result = thermal_gov_fair_share_register();
1781 if (result)
1782 return result;
1783
1784 return thermal_gov_user_space_register();
1785}
1786
1787static void thermal_unregister_governors(void)
1788{
1789 thermal_gov_step_wise_unregister();
1790 thermal_gov_fair_share_unregister();
1791 thermal_gov_user_space_unregister();
1792}
1793
Zhang Rui203d3d42008-01-17 15:51:08 +08001794static int __init thermal_init(void)
1795{
Zhang Rui80a26a52013-03-26 16:38:29 +08001796 int result;
1797
1798 result = thermal_register_governors();
1799 if (result)
1800 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08001801
1802 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001803 if (result)
1804 goto unregister_governors;
1805
R.Durgadoss4cb18722010-10-27 03:33:29 +05301806 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08001807 if (result)
1808 goto unregister_class;
1809
Eduardo Valentin1c762ed2013-07-03 15:35:39 -04001810 result = of_parse_thermal_zones();
1811 if (result)
1812 goto exit_netlink;
1813
Zhang Rui80a26a52013-03-26 16:38:29 +08001814 return 0;
1815
Eduardo Valentin1c762ed2013-07-03 15:35:39 -04001816exit_netlink:
1817 genetlink_exit();
Zhang Rui80a26a52013-03-26 16:38:29 +08001818unregister_governors:
1819 thermal_unregister_governors();
1820unregister_class:
1821 class_unregister(&thermal_class);
1822error:
1823 idr_destroy(&thermal_tz_idr);
1824 idr_destroy(&thermal_cdev_idr);
1825 mutex_destroy(&thermal_idr_lock);
1826 mutex_destroy(&thermal_list_lock);
1827 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001828 return result;
1829}
1830
1831static void __exit thermal_exit(void)
1832{
Eduardo Valentin1c762ed2013-07-03 15:35:39 -04001833 of_thermal_destroy_zones();
Zhang Rui80a26a52013-03-26 16:38:29 +08001834 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001835 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001836 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08001837 idr_destroy(&thermal_tz_idr);
1838 idr_destroy(&thermal_cdev_idr);
1839 mutex_destroy(&thermal_idr_lock);
1840 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08001841 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001842}
1843
R.Durgadoss4cb18722010-10-27 03:33:29 +05301844fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001845module_exit(thermal_exit);