blob: 45632bc05cc7f127cc5c72eb3769c35ac46c86d7 [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>
R.Durgadoss4cb18722010-10-27 03:33:29 +053037#include <net/netlink.h>
38#include <net/genetlink.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080039
Durgadoss R71350db2012-09-18 11:04:53 +053040#include "thermal_core.h"
Eduardo Valentin0dd88792013-07-03 15:14:28 -040041#include "thermal_hwmon.h"
Durgadoss R71350db2012-09-18 11:04:53 +053042
Zhang Rui63c4ec92008-04-21 16:07:13 +080043MODULE_AUTHOR("Zhang Rui");
Zhang Rui203d3d42008-01-17 15:51:08 +080044MODULE_DESCRIPTION("Generic thermal management sysfs support");
Eduardo Valentin6d8d4972013-04-23 21:48:13 +000045MODULE_LICENSE("GPL v2");
Zhang Rui203d3d42008-01-17 15:51:08 +080046
Zhang Rui203d3d42008-01-17 15:51:08 +080047static DEFINE_IDR(thermal_tz_idr);
48static DEFINE_IDR(thermal_cdev_idr);
49static DEFINE_MUTEX(thermal_idr_lock);
50
51static LIST_HEAD(thermal_tz_list);
52static LIST_HEAD(thermal_cdev_list);
Durgadoss Ra4a15482012-09-18 11:04:57 +053053static LIST_HEAD(thermal_governor_list);
54
Zhang Rui203d3d42008-01-17 15:51:08 +080055static DEFINE_MUTEX(thermal_list_lock);
Durgadoss Ra4a15482012-09-18 11:04:57 +053056static DEFINE_MUTEX(thermal_governor_lock);
57
58static struct thermal_governor *__find_governor(const char *name)
59{
60 struct thermal_governor *pos;
61
62 list_for_each_entry(pos, &thermal_governor_list, governor_list)
63 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
64 return pos;
65
66 return NULL;
67}
68
69int thermal_register_governor(struct thermal_governor *governor)
70{
71 int err;
72 const char *name;
73 struct thermal_zone_device *pos;
74
75 if (!governor)
76 return -EINVAL;
77
78 mutex_lock(&thermal_governor_lock);
79
80 err = -EBUSY;
81 if (__find_governor(governor->name) == NULL) {
82 err = 0;
83 list_add(&governor->governor_list, &thermal_governor_list);
84 }
85
86 mutex_lock(&thermal_list_lock);
87
88 list_for_each_entry(pos, &thermal_tz_list, node) {
89 if (pos->governor)
90 continue;
91 if (pos->tzp)
92 name = pos->tzp->governor_name;
93 else
94 name = DEFAULT_THERMAL_GOVERNOR;
95 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
96 pos->governor = governor;
97 }
98
99 mutex_unlock(&thermal_list_lock);
100 mutex_unlock(&thermal_governor_lock);
101
102 return err;
103}
Durgadoss Ra4a15482012-09-18 11:04:57 +0530104
105void thermal_unregister_governor(struct thermal_governor *governor)
106{
107 struct thermal_zone_device *pos;
108
109 if (!governor)
110 return;
111
112 mutex_lock(&thermal_governor_lock);
113
114 if (__find_governor(governor->name) == NULL)
115 goto exit;
116
117 mutex_lock(&thermal_list_lock);
118
119 list_for_each_entry(pos, &thermal_tz_list, node) {
120 if (!strnicmp(pos->governor->name, governor->name,
121 THERMAL_NAME_LENGTH))
122 pos->governor = NULL;
123 }
124
125 mutex_unlock(&thermal_list_lock);
126 list_del(&governor->governor_list);
127exit:
128 mutex_unlock(&thermal_governor_lock);
129 return;
130}
Zhang Rui203d3d42008-01-17 15:51:08 +0800131
132static int get_idr(struct idr *idr, struct mutex *lock, int *id)
133{
Tejun Heo6deb69f2013-02-27 17:04:46 -0800134 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800135
136 if (lock)
137 mutex_lock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800138 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800139 if (lock)
140 mutex_unlock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800141 if (unlikely(ret < 0))
142 return ret;
143 *id = ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800144 return 0;
145}
146
147static void release_idr(struct idr *idr, struct mutex *lock, int id)
148{
149 if (lock)
150 mutex_lock(lock);
151 idr_remove(idr, id);
152 if (lock)
153 mutex_unlock(lock);
154}
155
Durgadoss R9b4298a2012-09-18 11:04:54 +0530156int get_tz_trend(struct thermal_zone_device *tz, int trip)
157{
158 enum thermal_trend trend;
159
Eduardo Valentin0c872502013-05-29 21:37:00 +0000160 if (tz->emul_temperature || !tz->ops->get_trend ||
161 tz->ops->get_trend(tz, trip, &trend)) {
Durgadoss R9b4298a2012-09-18 11:04:54 +0530162 if (tz->temperature > tz->last_temperature)
163 trend = THERMAL_TREND_RAISING;
164 else if (tz->temperature < tz->last_temperature)
165 trend = THERMAL_TREND_DROPPING;
166 else
167 trend = THERMAL_TREND_STABLE;
168 }
169
170 return trend;
171}
172EXPORT_SYMBOL(get_tz_trend);
173
174struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
175 struct thermal_cooling_device *cdev, int trip)
176{
177 struct thermal_instance *pos = NULL;
178 struct thermal_instance *target_instance = NULL;
179
180 mutex_lock(&tz->lock);
181 mutex_lock(&cdev->lock);
182
183 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
184 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
185 target_instance = pos;
186 break;
187 }
188 }
189
190 mutex_unlock(&cdev->lock);
191 mutex_unlock(&tz->lock);
192
193 return target_instance;
194}
195EXPORT_SYMBOL(get_thermal_instance);
196
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530197static void print_bind_err_msg(struct thermal_zone_device *tz,
198 struct thermal_cooling_device *cdev, int ret)
199{
200 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
201 tz->type, cdev->type, ret);
202}
203
204static void __bind(struct thermal_zone_device *tz, int mask,
Eduardo Valentina8892d832013-07-16 15:26:28 -0400205 struct thermal_cooling_device *cdev,
206 unsigned long *limits)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530207{
208 int i, ret;
209
210 for (i = 0; i < tz->trips; i++) {
211 if (mask & (1 << i)) {
Eduardo Valentina8892d832013-07-16 15:26:28 -0400212 unsigned long upper, lower;
213
214 upper = THERMAL_NO_LIMIT;
215 lower = THERMAL_NO_LIMIT;
216 if (limits) {
217 lower = limits[i * 2];
218 upper = limits[i * 2 + 1];
219 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530220 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
Eduardo Valentina8892d832013-07-16 15:26:28 -0400221 upper, lower);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530222 if (ret)
223 print_bind_err_msg(tz, cdev, ret);
224 }
225 }
226}
227
228static void __unbind(struct thermal_zone_device *tz, int mask,
229 struct thermal_cooling_device *cdev)
230{
231 int i;
232
233 for (i = 0; i < tz->trips; i++)
234 if (mask & (1 << i))
235 thermal_zone_unbind_cooling_device(tz, i, cdev);
236}
237
238static void bind_cdev(struct thermal_cooling_device *cdev)
239{
240 int i, ret;
241 const struct thermal_zone_params *tzp;
242 struct thermal_zone_device *pos = NULL;
243
244 mutex_lock(&thermal_list_lock);
245
246 list_for_each_entry(pos, &thermal_tz_list, node) {
247 if (!pos->tzp && !pos->ops->bind)
248 continue;
249
Ni Wadea9f2d192013-11-06 14:30:13 +0800250 if (pos->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530251 ret = pos->ops->bind(pos, cdev);
252 if (ret)
253 print_bind_err_msg(pos, cdev, ret);
Ni Wadea9f2d192013-11-06 14:30:13 +0800254 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530255 }
256
257 tzp = pos->tzp;
Hugh Dickins791700c2012-09-27 16:48:28 -0700258 if (!tzp || !tzp->tbp)
259 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530260
261 for (i = 0; i < tzp->num_tbps; i++) {
262 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
263 continue;
264 if (tzp->tbp[i].match(pos, cdev))
265 continue;
266 tzp->tbp[i].cdev = cdev;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400267 __bind(pos, tzp->tbp[i].trip_mask, cdev,
268 tzp->tbp[i].binding_limits);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530269 }
270 }
271
272 mutex_unlock(&thermal_list_lock);
273}
274
275static void bind_tz(struct thermal_zone_device *tz)
276{
277 int i, ret;
278 struct thermal_cooling_device *pos = NULL;
279 const struct thermal_zone_params *tzp = tz->tzp;
280
281 if (!tzp && !tz->ops->bind)
282 return;
283
284 mutex_lock(&thermal_list_lock);
285
Ni Wadea9f2d192013-11-06 14:30:13 +0800286 /* If there is ops->bind, try to use ops->bind */
287 if (tz->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530288 list_for_each_entry(pos, &thermal_cdev_list, node) {
289 ret = tz->ops->bind(tz, pos);
290 if (ret)
291 print_bind_err_msg(tz, pos, ret);
292 }
293 goto exit;
294 }
295
Hugh Dickins791700c2012-09-27 16:48:28 -0700296 if (!tzp || !tzp->tbp)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530297 goto exit;
298
299 list_for_each_entry(pos, &thermal_cdev_list, node) {
300 for (i = 0; i < tzp->num_tbps; i++) {
301 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
302 continue;
303 if (tzp->tbp[i].match(tz, pos))
304 continue;
305 tzp->tbp[i].cdev = pos;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400306 __bind(tz, tzp->tbp[i].trip_mask, pos,
307 tzp->tbp[i].binding_limits);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530308 }
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 Ruid567c682012-12-12 15:23:54 +0800344 if (tz->governor)
345 tz->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 Valentin9b19ec32013-04-25 14:13:33 +0000406 if (!tz || IS_ERR(tz))
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 Lu06475b52013-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
462 update_temperature(tz);
463
464 for (count = 0; count < tz->trips; count++)
465 handle_thermal_trip(tz, count);
466}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000467EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530468
469static void thermal_zone_device_check(struct work_struct *work)
470{
471 struct thermal_zone_device *tz = container_of(work, struct
472 thermal_zone_device,
473 poll_queue.work);
474 thermal_zone_device_update(tz);
475}
476
Zhang Rui203d3d42008-01-17 15:51:08 +0800477/* sys I/F for thermal zone */
478
479#define to_thermal_zone(_dev) \
480 container_of(_dev, struct thermal_zone_device, device)
481
482static ssize_t
483type_show(struct device *dev, struct device_attribute *attr, char *buf)
484{
485 struct thermal_zone_device *tz = to_thermal_zone(dev);
486
487 return sprintf(buf, "%s\n", tz->type);
488}
489
490static ssize_t
491temp_show(struct device *dev, struct device_attribute *attr, char *buf)
492{
493 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000494 long temperature;
495 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800496
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000497 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000498
499 if (ret)
500 return ret;
501
502 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800503}
504
505static ssize_t
506mode_show(struct device *dev, struct device_attribute *attr, char *buf)
507{
508 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000509 enum thermal_device_mode mode;
510 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800511
512 if (!tz->ops->get_mode)
513 return -EPERM;
514
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000515 result = tz->ops->get_mode(tz, &mode);
516 if (result)
517 return result;
518
519 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
520 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800521}
522
523static ssize_t
524mode_store(struct device *dev, struct device_attribute *attr,
525 const char *buf, size_t count)
526{
527 struct thermal_zone_device *tz = to_thermal_zone(dev);
528 int result;
529
530 if (!tz->ops->set_mode)
531 return -EPERM;
532
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530533 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000534 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530535 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000536 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
537 else
538 result = -EINVAL;
539
Zhang Rui203d3d42008-01-17 15:51:08 +0800540 if (result)
541 return result;
542
543 return count;
544}
545
546static ssize_t
547trip_point_type_show(struct device *dev, struct device_attribute *attr,
548 char *buf)
549{
550 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000551 enum thermal_trip_type type;
552 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800553
554 if (!tz->ops->get_trip_type)
555 return -EPERM;
556
557 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
558 return -EINVAL;
559
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000560 result = tz->ops->get_trip_type(tz, trip, &type);
561 if (result)
562 return result;
563
564 switch (type) {
565 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300566 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000567 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300568 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000569 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300570 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000571 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300572 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000573 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300574 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000575 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800576}
577
578static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800579trip_point_temp_store(struct device *dev, struct device_attribute *attr,
580 const char *buf, size_t count)
581{
582 struct thermal_zone_device *tz = to_thermal_zone(dev);
583 int trip, ret;
584 unsigned long temperature;
585
586 if (!tz->ops->set_trip_temp)
587 return -EPERM;
588
589 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
590 return -EINVAL;
591
592 if (kstrtoul(buf, 10, &temperature))
593 return -EINVAL;
594
595 ret = tz->ops->set_trip_temp(tz, trip, temperature);
596
597 return ret ? ret : count;
598}
599
600static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800601trip_point_temp_show(struct device *dev, struct device_attribute *attr,
602 char *buf)
603{
604 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000605 int trip, ret;
606 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800607
608 if (!tz->ops->get_trip_temp)
609 return -EPERM;
610
611 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
612 return -EINVAL;
613
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000614 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
615
616 if (ret)
617 return ret;
618
619 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800620}
621
Matthew Garrett03a971a2008-12-03 18:00:38 +0000622static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800623trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
624 const char *buf, size_t count)
625{
626 struct thermal_zone_device *tz = to_thermal_zone(dev);
627 int trip, ret;
628 unsigned long temperature;
629
630 if (!tz->ops->set_trip_hyst)
631 return -EPERM;
632
633 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
634 return -EINVAL;
635
636 if (kstrtoul(buf, 10, &temperature))
637 return -EINVAL;
638
639 /*
640 * We are not doing any check on the 'temperature' value
641 * here. The driver implementing 'set_trip_hyst' has to
642 * take care of this.
643 */
644 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
645
646 return ret ? ret : count;
647}
648
649static ssize_t
650trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
651 char *buf)
652{
653 struct thermal_zone_device *tz = to_thermal_zone(dev);
654 int trip, ret;
655 unsigned long temperature;
656
657 if (!tz->ops->get_trip_hyst)
658 return -EPERM;
659
660 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
661 return -EINVAL;
662
663 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
664
665 return ret ? ret : sprintf(buf, "%ld\n", temperature);
666}
667
668static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000669passive_store(struct device *dev, struct device_attribute *attr,
670 const char *buf, size_t count)
671{
672 struct thermal_zone_device *tz = to_thermal_zone(dev);
673 struct thermal_cooling_device *cdev = NULL;
674 int state;
675
676 if (!sscanf(buf, "%d\n", &state))
677 return -EINVAL;
678
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100679 /* sanity check: values below 1000 millicelcius don't make sense
680 * and can cause the system to go into a thermal heart attack
681 */
682 if (state && state < 1000)
683 return -EINVAL;
684
Matthew Garrett03a971a2008-12-03 18:00:38 +0000685 if (state && !tz->forced_passive) {
686 mutex_lock(&thermal_list_lock);
687 list_for_each_entry(cdev, &thermal_cdev_list, node) {
688 if (!strncmp("Processor", cdev->type,
689 sizeof("Processor")))
690 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800691 THERMAL_TRIPS_NONE, cdev,
692 THERMAL_NO_LIMIT,
693 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000694 }
695 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100696 if (!tz->passive_delay)
697 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000698 } else if (!state && tz->forced_passive) {
699 mutex_lock(&thermal_list_lock);
700 list_for_each_entry(cdev, &thermal_cdev_list, node) {
701 if (!strncmp("Processor", cdev->type,
702 sizeof("Processor")))
703 thermal_zone_unbind_cooling_device(tz,
704 THERMAL_TRIPS_NONE,
705 cdev);
706 }
707 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100708 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000709 }
710
Matthew Garrett03a971a2008-12-03 18:00:38 +0000711 tz->forced_passive = state;
712
713 thermal_zone_device_update(tz);
714
715 return count;
716}
717
718static ssize_t
719passive_show(struct device *dev, struct device_attribute *attr,
720 char *buf)
721{
722 struct thermal_zone_device *tz = to_thermal_zone(dev);
723
724 return sprintf(buf, "%d\n", tz->forced_passive);
725}
726
Durgadoss R5a2c0902012-09-18 11:04:58 +0530727static ssize_t
728policy_store(struct device *dev, struct device_attribute *attr,
729 const char *buf, size_t count)
730{
731 int ret = -EINVAL;
732 struct thermal_zone_device *tz = to_thermal_zone(dev);
733 struct thermal_governor *gov;
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000734 char name[THERMAL_NAME_LENGTH];
735
736 snprintf(name, sizeof(name), "%s", buf);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530737
738 mutex_lock(&thermal_governor_lock);
739
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000740 gov = __find_governor(strim(name));
Durgadoss R5a2c0902012-09-18 11:04:58 +0530741 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,Tianyu800744b2014-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);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +05301048 } else if(!strncmp(dev_name(dev), "cooling_device",
1049 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +08001050 cdev = to_cooling_device(dev);
1051 kfree(cdev);
1052 }
1053}
1054
1055static struct class thermal_class = {
1056 .name = "thermal",
1057 .dev_release = thermal_release,
1058};
1059
1060/**
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001061 * thermal_cooling_device_register() - register a new thermal cooling device
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.
1069 *
1070 * Return: a pointer to the created struct thermal_cooling_device or an
1071 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001072 */
Joe Perchescaca8b82012-03-21 12:55:02 -07001073struct thermal_cooling_device *
1074thermal_cooling_device_register(char *type, void *devdata,
1075 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001076{
1077 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001078 int result;
1079
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001080 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001081 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001082
1083 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001084 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001085 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001086
1087 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1088 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001089 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001090
1091 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1092 if (result) {
1093 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001094 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001095 }
1096
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001097 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +08001098 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001099 INIT_LIST_HEAD(&cdev->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001100 cdev->ops = ops;
Zhang Ruice119f82012-06-27 14:13:04 +08001101 cdev->updated = true;
Zhang Rui203d3d42008-01-17 15:51:08 +08001102 cdev->device.class = &thermal_class;
1103 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001104 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001105 result = device_register(&cdev->device);
1106 if (result) {
1107 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1108 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001109 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001110 }
1111
1112 /* sys I/F */
1113 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001114 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001115 if (result)
1116 goto unregister;
1117 }
1118
1119 result = device_create_file(&cdev->device, &dev_attr_max_state);
1120 if (result)
1121 goto unregister;
1122
1123 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1124 if (result)
1125 goto unregister;
1126
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301127 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001128 mutex_lock(&thermal_list_lock);
1129 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001130 mutex_unlock(&thermal_list_lock);
1131
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301132 /* Update binding information for 'this' new cdev */
1133 bind_cdev(cdev);
1134
1135 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001136
Joe Perchescaca8b82012-03-21 12:55:02 -07001137unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001138 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1139 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001140 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001141}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001142EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001143
1144/**
1145 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001146 * @cdev: the thermal cooling device to remove.
1147 *
1148 * thermal_cooling_device_unregister() must be called when the device is no
1149 * longer needed.
1150 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301151void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001152{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301153 int i;
1154 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001155 struct thermal_zone_device *tz;
1156 struct thermal_cooling_device *pos = NULL;
1157
1158 if (!cdev)
1159 return;
1160
1161 mutex_lock(&thermal_list_lock);
1162 list_for_each_entry(pos, &thermal_cdev_list, node)
1163 if (pos == cdev)
1164 break;
1165 if (pos != cdev) {
1166 /* thermal cooling device not found */
1167 mutex_unlock(&thermal_list_lock);
1168 return;
1169 }
1170 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301171
1172 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001173 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301174 if (tz->ops->unbind) {
1175 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001176 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301177 }
1178
1179 if (!tz->tzp || !tz->tzp->tbp)
1180 continue;
1181
1182 tzp = tz->tzp;
1183 for (i = 0; i < tzp->num_tbps; i++) {
1184 if (tzp->tbp[i].cdev == cdev) {
1185 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1186 tzp->tbp[i].cdev = NULL;
1187 }
1188 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001189 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301190
Zhang Rui203d3d42008-01-17 15:51:08 +08001191 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301192
Zhang Rui203d3d42008-01-17 15:51:08 +08001193 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001194 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001195 device_remove_file(&cdev->device, &dev_attr_max_state);
1196 device_remove_file(&cdev->device, &dev_attr_cur_state);
1197
1198 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1199 device_unregister(&cdev->device);
1200 return;
1201}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001202EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001203
Durgadoss Rdc765482012-09-18 11:05:00 +05301204void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001205{
1206 struct thermal_instance *instance;
1207 unsigned long target = 0;
1208
1209 /* cooling device is updated*/
1210 if (cdev->updated)
1211 return;
1212
Zhang Ruif4a821c2012-07-24 16:56:21 +08001213 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001214 /* Make sure cdev enters the deepest cooling state */
1215 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
Aaron Lu06475b52013-12-02 13:54:26 +08001216 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1217 instance->tz->id, instance->target);
Zhang Ruice119f82012-06-27 14:13:04 +08001218 if (instance->target == THERMAL_NO_TARGET)
1219 continue;
1220 if (instance->target > target)
1221 target = instance->target;
1222 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001223 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001224 cdev->ops->set_cur_state(cdev, target);
1225 cdev->updated = true;
Aaron Lu06475b52013-12-02 13:54:26 +08001226 dev_dbg(&cdev->device, "set to state %lu\n", target);
Zhang Ruice119f82012-06-27 14:13:04 +08001227}
Durgadoss Rdc765482012-09-18 11:05:00 +05301228EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001229
Matthew Garrettb1569e92008-12-03 17:55:32 +00001230/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001231 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301232 * @tz: thermal zone device
1233 * @trip: indicates which trip point has been crossed
1234 *
1235 * This function handles the trip events from sensor drivers. It starts
1236 * throttling the cooling devices according to the policy configured.
1237 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1238 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1239 * The throttling policy is based on the configured platform data; if no
1240 * platform data is provided, this uses the step_wise throttling policy.
1241 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001242void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301243{
1244 handle_thermal_trip(tz, trip);
1245}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001246EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301247
1248/**
Eduardo Valentin269c1742013-04-23 21:48:19 +00001249 * create_trip_attrs() - create attributes for trip points
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001250 * @tz: the thermal zone device
1251 * @mask: Writeable trip point bitmap.
Eduardo Valentin269c1742013-04-23 21:48:19 +00001252 *
1253 * helper function to instantiate sysfs entries for every trip
1254 * point and its properties of a struct thermal_zone_device.
1255 *
1256 * Return: 0 on success, the proper error value otherwise.
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001257 */
1258static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1259{
1260 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001261 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001262
Durgadoss R27365a62012-07-25 10:10:59 +08001263 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001264 if (!tz->trip_type_attrs)
1265 return -ENOMEM;
1266
Durgadoss R27365a62012-07-25 10:10:59 +08001267 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001268 if (!tz->trip_temp_attrs) {
1269 kfree(tz->trip_type_attrs);
1270 return -ENOMEM;
1271 }
1272
Durgadoss R27365a62012-07-25 10:10:59 +08001273 if (tz->ops->get_trip_hyst) {
1274 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1275 if (!tz->trip_hyst_attrs) {
1276 kfree(tz->trip_type_attrs);
1277 kfree(tz->trip_temp_attrs);
1278 return -ENOMEM;
1279 }
1280 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001281
Durgadoss R27365a62012-07-25 10:10:59 +08001282
1283 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001284 /* create trip type attribute */
1285 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1286 "trip_point_%d_type", indx);
1287
1288 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1289 tz->trip_type_attrs[indx].attr.attr.name =
1290 tz->trip_type_attrs[indx].name;
1291 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1292 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1293
1294 device_create_file(&tz->device,
1295 &tz->trip_type_attrs[indx].attr);
1296
1297 /* create trip temp attribute */
1298 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1299 "trip_point_%d_temp", indx);
1300
1301 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1302 tz->trip_temp_attrs[indx].attr.attr.name =
1303 tz->trip_temp_attrs[indx].name;
1304 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1305 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1306 if (mask & (1 << indx)) {
1307 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1308 tz->trip_temp_attrs[indx].attr.store =
1309 trip_point_temp_store;
1310 }
1311
1312 device_create_file(&tz->device,
1313 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001314
1315 /* create Optional trip hyst attribute */
1316 if (!tz->ops->get_trip_hyst)
1317 continue;
1318 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1319 "trip_point_%d_hyst", indx);
1320
1321 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1322 tz->trip_hyst_attrs[indx].attr.attr.name =
1323 tz->trip_hyst_attrs[indx].name;
1324 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1325 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1326 if (tz->ops->set_trip_hyst) {
1327 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1328 tz->trip_hyst_attrs[indx].attr.store =
1329 trip_point_hyst_store;
1330 }
1331
1332 device_create_file(&tz->device,
1333 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001334 }
1335 return 0;
1336}
1337
1338static void remove_trip_attrs(struct thermal_zone_device *tz)
1339{
1340 int indx;
1341
1342 for (indx = 0; indx < tz->trips; indx++) {
1343 device_remove_file(&tz->device,
1344 &tz->trip_type_attrs[indx].attr);
1345 device_remove_file(&tz->device,
1346 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001347 if (tz->ops->get_trip_hyst)
1348 device_remove_file(&tz->device,
1349 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001350 }
1351 kfree(tz->trip_type_attrs);
1352 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001353 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001354}
1355
1356/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001357 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001358 * @type: the thermal zone device type
1359 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001360 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001361 * @devdata: private device data
1362 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301363 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001364 * @passive_delay: number of milliseconds to wait between polls when
1365 * performing passive cooling
1366 * @polling_delay: number of milliseconds to wait between polls when checking
1367 * whether trip points have been crossed (0 for interrupt
1368 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001369 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001370 * This interface function adds a new thermal zone device (sensor) to
1371 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1372 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001373 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001374 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001375 *
1376 * Return: a pointer to the created struct thermal_zone_device or an
1377 * in case of error, an ERR_PTR. Caller must check return value with
1378 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001379 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001380struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001381 int trips, int mask, void *devdata,
Alan Cox5b275ce2010-11-11 15:27:29 +00001382 const struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301383 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001384 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001385{
1386 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001387 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001388 int result;
1389 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001390 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001391
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001392 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001393 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001394
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001395 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001396 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001397
1398 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001399 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001400
Jonghwa Lee83720d02013-05-18 09:50:26 +00001401 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001402 return ERR_PTR(-EINVAL);
1403
Zhang Rui203d3d42008-01-17 15:51:08 +08001404 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1405 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001406 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001407
Zhang Rui2d374132012-06-27 10:09:00 +08001408 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001409 idr_init(&tz->idr);
1410 mutex_init(&tz->lock);
1411 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1412 if (result) {
1413 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001414 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001415 }
1416
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001417 strlcpy(tz->type, type ? : "", sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001418 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301419 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001420 tz->device.class = &thermal_class;
1421 tz->devdata = devdata;
1422 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001423 tz->passive_delay = passive_delay;
1424 tz->polling_delay = polling_delay;
1425
Kay Sievers354655e2009-01-06 10:44:37 -08001426 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001427 result = device_register(&tz->device);
1428 if (result) {
1429 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1430 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001431 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001432 }
1433
1434 /* sys I/F */
1435 if (type) {
1436 result = device_create_file(&tz->device, &dev_attr_type);
1437 if (result)
1438 goto unregister;
1439 }
1440
1441 result = device_create_file(&tz->device, &dev_attr_temp);
1442 if (result)
1443 goto unregister;
1444
1445 if (ops->get_mode) {
1446 result = device_create_file(&tz->device, &dev_attr_mode);
1447 if (result)
1448 goto unregister;
1449 }
1450
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001451 result = create_trip_attrs(tz, mask);
1452 if (result)
1453 goto unregister;
1454
Zhang Rui203d3d42008-01-17 15:51:08 +08001455 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001456 tz->ops->get_trip_type(tz, count, &trip_type);
1457 if (trip_type == THERMAL_TRIP_PASSIVE)
1458 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001459 }
1460
Durgadoss R5a2c0902012-09-18 11:04:58 +05301461 if (!passive) {
1462 result = device_create_file(&tz->device, &dev_attr_passive);
1463 if (result)
1464 goto unregister;
1465 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001466
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001467#ifdef CONFIG_THERMAL_EMULATION
1468 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1469 if (result)
1470 goto unregister;
1471#endif
Durgadoss R5a2c0902012-09-18 11:04:58 +05301472 /* Create policy attribute */
1473 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001474 if (result)
1475 goto unregister;
1476
Durgadoss Ra4a15482012-09-18 11:04:57 +05301477 /* Update 'this' zone's governor information */
1478 mutex_lock(&thermal_governor_lock);
1479
1480 if (tz->tzp)
1481 tz->governor = __find_governor(tz->tzp->governor_name);
1482 else
1483 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1484
1485 mutex_unlock(&thermal_governor_lock);
1486
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04001487 if (!tz->tzp || !tz->tzp->no_hwmon) {
1488 result = thermal_add_hwmon_sysfs(tz);
1489 if (result)
1490 goto unregister;
1491 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08001492
Zhang Rui203d3d42008-01-17 15:51:08 +08001493 mutex_lock(&thermal_list_lock);
1494 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001495 mutex_unlock(&thermal_list_lock);
1496
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301497 /* Bind cooling devices for this zone */
1498 bind_tz(tz);
1499
Matthew Garrettb1569e92008-12-03 17:55:32 +00001500 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1501
1502 thermal_zone_device_update(tz);
1503
Zhang Rui203d3d42008-01-17 15:51:08 +08001504 if (!result)
1505 return tz;
1506
Joe Perchescaca8b82012-03-21 12:55:02 -07001507unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001508 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1509 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001510 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001511}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001512EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001513
1514/**
1515 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001516 * @tz: the thermal zone device to remove
1517 */
1518void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1519{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301520 int i;
1521 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001522 struct thermal_cooling_device *cdev;
1523 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001524
1525 if (!tz)
1526 return;
1527
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301528 tzp = tz->tzp;
1529
Zhang Rui203d3d42008-01-17 15:51:08 +08001530 mutex_lock(&thermal_list_lock);
1531 list_for_each_entry(pos, &thermal_tz_list, node)
1532 if (pos == tz)
1533 break;
1534 if (pos != tz) {
1535 /* thermal zone device not found */
1536 mutex_unlock(&thermal_list_lock);
1537 return;
1538 }
1539 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301540
1541 /* Unbind all cdevs associated with 'this' thermal zone */
1542 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1543 if (tz->ops->unbind) {
1544 tz->ops->unbind(tz, cdev);
1545 continue;
1546 }
1547
1548 if (!tzp || !tzp->tbp)
1549 break;
1550
1551 for (i = 0; i < tzp->num_tbps; i++) {
1552 if (tzp->tbp[i].cdev == cdev) {
1553 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1554 tzp->tbp[i].cdev = NULL;
1555 }
1556 }
1557 }
1558
Zhang Rui203d3d42008-01-17 15:51:08 +08001559 mutex_unlock(&thermal_list_lock);
1560
Matthew Garrettb1569e92008-12-03 17:55:32 +00001561 thermal_zone_device_set_polling(tz, 0);
1562
Zhang Rui203d3d42008-01-17 15:51:08 +08001563 if (tz->type[0])
1564 device_remove_file(&tz->device, &dev_attr_type);
1565 device_remove_file(&tz->device, &dev_attr_temp);
1566 if (tz->ops->get_mode)
1567 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301568 device_remove_file(&tz->device, &dev_attr_policy);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001569 remove_trip_attrs(tz);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301570 tz->governor = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001571
Zhang Ruie68b16a2008-04-21 16:07:52 +08001572 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001573 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1574 idr_destroy(&tz->idr);
1575 mutex_destroy(&tz->lock);
1576 device_unregister(&tz->device);
1577 return;
1578}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001579EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001580
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001581/**
1582 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1583 * @name: thermal zone name to fetch the temperature
1584 *
1585 * When only one zone is found with the passed name, returns a reference to it.
1586 *
1587 * Return: On success returns a reference to an unique thermal zone with
1588 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1589 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1590 */
1591struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1592{
1593 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1594 unsigned int found = 0;
1595
1596 if (!name)
1597 goto exit;
1598
1599 mutex_lock(&thermal_list_lock);
1600 list_for_each_entry(pos, &thermal_tz_list, node)
1601 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1602 found++;
1603 ref = pos;
1604 }
1605 mutex_unlock(&thermal_list_lock);
1606
1607 /* nothing has been found, thus an error code for it */
1608 if (found == 0)
1609 ref = ERR_PTR(-ENODEV);
1610 else if (found > 1)
1611 /* Success only when an unique zone is found */
1612 ref = ERR_PTR(-EEXIST);
1613
1614exit:
1615 return ref;
1616}
1617EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1618
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001619#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01001620static const struct genl_multicast_group thermal_event_mcgrps[] = {
1621 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1622};
1623
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001624static struct genl_family thermal_event_genl_family = {
1625 .id = GENL_ID_GENERATE,
1626 .name = THERMAL_GENL_FAMILY_NAME,
1627 .version = THERMAL_GENL_VERSION,
1628 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001629 .mcgrps = thermal_event_mcgrps,
1630 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001631};
1632
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001633int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1634 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301635{
1636 struct sk_buff *skb;
1637 struct nlattr *attr;
1638 struct thermal_genl_event *thermal_event;
1639 void *msg_header;
1640 int size;
1641 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001642 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301643
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001644 if (!tz)
1645 return -EINVAL;
1646
R.Durgadoss4cb18722010-10-27 03:33:29 +05301647 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001648 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1649 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301650
1651 skb = genlmsg_new(size, GFP_ATOMIC);
1652 if (!skb)
1653 return -ENOMEM;
1654
1655 /* add the genetlink message header */
1656 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1657 &thermal_event_genl_family, 0,
1658 THERMAL_GENL_CMD_EVENT);
1659 if (!msg_header) {
1660 nlmsg_free(skb);
1661 return -ENOMEM;
1662 }
1663
1664 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001665 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1666 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301667
1668 if (!attr) {
1669 nlmsg_free(skb);
1670 return -EINVAL;
1671 }
1672
1673 thermal_event = nla_data(attr);
1674 if (!thermal_event) {
1675 nlmsg_free(skb);
1676 return -EINVAL;
1677 }
1678
1679 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1680
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001681 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301682 thermal_event->event = event;
1683
1684 /* send multicast genetlink message */
1685 result = genlmsg_end(skb, msg_header);
1686 if (result < 0) {
1687 nlmsg_free(skb);
1688 return result;
1689 }
1690
Johannes Berg68eb5502013-11-19 15:19:38 +01001691 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001692 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301693 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001694 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301695
1696 return result;
1697}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001698EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301699
1700static int genetlink_init(void)
1701{
Johannes Berg2a94fe42013-11-19 15:19:39 +01001702 return genl_register_family(&thermal_event_genl_family);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301703}
1704
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001705static void genetlink_exit(void)
1706{
1707 genl_unregister_family(&thermal_event_genl_family);
1708}
1709#else /* !CONFIG_NET */
1710static inline int genetlink_init(void) { return 0; }
1711static inline void genetlink_exit(void) {}
1712#endif /* !CONFIG_NET */
1713
Zhang Rui80a26a52013-03-26 16:38:29 +08001714static int __init thermal_register_governors(void)
1715{
1716 int result;
1717
1718 result = thermal_gov_step_wise_register();
1719 if (result)
1720 return result;
1721
1722 result = thermal_gov_fair_share_register();
1723 if (result)
1724 return result;
1725
1726 return thermal_gov_user_space_register();
1727}
1728
1729static void thermal_unregister_governors(void)
1730{
1731 thermal_gov_step_wise_unregister();
1732 thermal_gov_fair_share_unregister();
1733 thermal_gov_user_space_unregister();
1734}
1735
Zhang Rui203d3d42008-01-17 15:51:08 +08001736static int __init thermal_init(void)
1737{
Zhang Rui80a26a52013-03-26 16:38:29 +08001738 int result;
1739
1740 result = thermal_register_governors();
1741 if (result)
1742 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08001743
1744 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001745 if (result)
1746 goto unregister_governors;
1747
R.Durgadoss4cb18722010-10-27 03:33:29 +05301748 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08001749 if (result)
1750 goto unregister_class;
1751
1752 return 0;
1753
1754unregister_governors:
1755 thermal_unregister_governors();
1756unregister_class:
1757 class_unregister(&thermal_class);
1758error:
1759 idr_destroy(&thermal_tz_idr);
1760 idr_destroy(&thermal_cdev_idr);
1761 mutex_destroy(&thermal_idr_lock);
1762 mutex_destroy(&thermal_list_lock);
1763 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001764 return result;
1765}
1766
1767static void __exit thermal_exit(void)
1768{
Zhang Rui80a26a52013-03-26 16:38:29 +08001769 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001770 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001771 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08001772 idr_destroy(&thermal_tz_idr);
1773 idr_destroy(&thermal_cdev_idr);
1774 mutex_destroy(&thermal_idr_lock);
1775 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08001776 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001777}
1778
R.Durgadoss4cb18722010-10-27 03:33:29 +05301779fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001780module_exit(thermal_exit);