blob: 247528bbf00cb57ccc0737eb4225f4708b68f41a [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,
205 struct thermal_cooling_device *cdev)
206{
207 int i, ret;
208
209 for (i = 0; i < tz->trips; i++) {
210 if (mask & (1 << i)) {
211 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
212 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
213 if (ret)
214 print_bind_err_msg(tz, cdev, ret);
215 }
216 }
217}
218
219static void __unbind(struct thermal_zone_device *tz, int mask,
220 struct thermal_cooling_device *cdev)
221{
222 int i;
223
224 for (i = 0; i < tz->trips; i++)
225 if (mask & (1 << i))
226 thermal_zone_unbind_cooling_device(tz, i, cdev);
227}
228
229static void bind_cdev(struct thermal_cooling_device *cdev)
230{
231 int i, ret;
232 const struct thermal_zone_params *tzp;
233 struct thermal_zone_device *pos = NULL;
234
235 mutex_lock(&thermal_list_lock);
236
237 list_for_each_entry(pos, &thermal_tz_list, node) {
238 if (!pos->tzp && !pos->ops->bind)
239 continue;
240
241 if (!pos->tzp && pos->ops->bind) {
242 ret = pos->ops->bind(pos, cdev);
243 if (ret)
244 print_bind_err_msg(pos, cdev, ret);
245 }
246
247 tzp = pos->tzp;
Hugh Dickins791700c2012-09-27 16:48:28 -0700248 if (!tzp || !tzp->tbp)
249 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530250
251 for (i = 0; i < tzp->num_tbps; i++) {
252 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
253 continue;
254 if (tzp->tbp[i].match(pos, cdev))
255 continue;
256 tzp->tbp[i].cdev = cdev;
257 __bind(pos, tzp->tbp[i].trip_mask, cdev);
258 }
259 }
260
261 mutex_unlock(&thermal_list_lock);
262}
263
264static void bind_tz(struct thermal_zone_device *tz)
265{
266 int i, ret;
267 struct thermal_cooling_device *pos = NULL;
268 const struct thermal_zone_params *tzp = tz->tzp;
269
270 if (!tzp && !tz->ops->bind)
271 return;
272
273 mutex_lock(&thermal_list_lock);
274
275 /* If there is no platform data, try to use ops->bind */
276 if (!tzp && tz->ops->bind) {
277 list_for_each_entry(pos, &thermal_cdev_list, node) {
278 ret = tz->ops->bind(tz, pos);
279 if (ret)
280 print_bind_err_msg(tz, pos, ret);
281 }
282 goto exit;
283 }
284
Hugh Dickins791700c2012-09-27 16:48:28 -0700285 if (!tzp || !tzp->tbp)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530286 goto exit;
287
288 list_for_each_entry(pos, &thermal_cdev_list, node) {
289 for (i = 0; i < tzp->num_tbps; i++) {
290 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
291 continue;
292 if (tzp->tbp[i].match(tz, pos))
293 continue;
294 tzp->tbp[i].cdev = pos;
295 __bind(tz, tzp->tbp[i].trip_mask, pos);
296 }
297 }
298exit:
299 mutex_unlock(&thermal_list_lock);
300}
301
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530302static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
303 int delay)
304{
305 if (delay > 1000)
306 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
307 round_jiffies(msecs_to_jiffies(delay)));
308 else if (delay)
309 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
310 msecs_to_jiffies(delay));
311 else
312 cancel_delayed_work(&tz->poll_queue);
313}
314
315static void monitor_thermal_zone(struct thermal_zone_device *tz)
316{
317 mutex_lock(&tz->lock);
318
319 if (tz->passive)
320 thermal_zone_device_set_polling(tz, tz->passive_delay);
321 else if (tz->polling_delay)
322 thermal_zone_device_set_polling(tz, tz->polling_delay);
323 else
324 thermal_zone_device_set_polling(tz, 0);
325
326 mutex_unlock(&tz->lock);
327}
328
329static void handle_non_critical_trips(struct thermal_zone_device *tz,
330 int trip, enum thermal_trip_type trip_type)
331{
Zhang Ruid567c682012-12-12 15:23:54 +0800332 if (tz->governor)
333 tz->governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530334}
335
336static void handle_critical_trips(struct thermal_zone_device *tz,
337 int trip, enum thermal_trip_type trip_type)
338{
339 long trip_temp;
340
341 tz->ops->get_trip_temp(tz, trip, &trip_temp);
342
343 /* If we have not crossed the trip_temp, we do not care. */
344 if (tz->temperature < trip_temp)
345 return;
346
347 if (tz->ops->notify)
348 tz->ops->notify(tz, trip, trip_type);
349
350 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000351 dev_emerg(&tz->device,
352 "critical temperature reached(%d C),shutting down\n",
353 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530354 orderly_poweroff(true);
355 }
356}
357
358static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
359{
360 enum thermal_trip_type type;
361
362 tz->ops->get_trip_type(tz, trip, &type);
363
364 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
365 handle_critical_trips(tz, trip, type);
366 else
367 handle_non_critical_trips(tz, trip, type);
368 /*
369 * Alright, we handled this trip successfully.
370 * So, start monitoring again.
371 */
372 monitor_thermal_zone(tz);
373}
374
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000375/**
376 * thermal_zone_get_temp() - returns its the temperature of thermal zone
377 * @tz: a valid pointer to a struct thermal_zone_device
378 * @temp: a valid pointer to where to store the resulting temperature.
379 *
380 * When a valid thermal zone reference is passed, it will fetch its
381 * temperature and fill @temp.
382 *
383 * Return: On success returns 0, an error code otherwise
384 */
385int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000386{
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000387 int ret = -EINVAL;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800388#ifdef CONFIG_THERMAL_EMULATION
389 int count;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000390 unsigned long crit_temp = -1UL;
391 enum thermal_trip_type type;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800392#endif
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000393
Eduardo Valentin9b19ec32013-04-25 14:13:33 +0000394 if (!tz || IS_ERR(tz))
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000395 goto exit;
396
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000397 mutex_lock(&tz->lock);
398
399 ret = tz->ops->get_temp(tz, temp);
400#ifdef CONFIG_THERMAL_EMULATION
401 if (!tz->emul_temperature)
402 goto skip_emul;
403
404 for (count = 0; count < tz->trips; count++) {
405 ret = tz->ops->get_trip_type(tz, count, &type);
406 if (!ret && type == THERMAL_TRIP_CRITICAL) {
407 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
408 break;
409 }
410 }
411
412 if (ret)
413 goto skip_emul;
414
415 if (*temp < crit_temp)
416 *temp = tz->emul_temperature;
417skip_emul:
418#endif
419 mutex_unlock(&tz->lock);
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000420exit:
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000421 return ret;
422}
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000423EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000424
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530425static void update_temperature(struct thermal_zone_device *tz)
426{
427 long temp;
428 int ret;
429
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000430 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530431 if (ret) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000432 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
433 tz->id);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000434 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530435 }
436
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000437 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530438 tz->last_temperature = tz->temperature;
439 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530440 mutex_unlock(&tz->lock);
441}
442
443void thermal_zone_device_update(struct thermal_zone_device *tz)
444{
445 int count;
446
447 update_temperature(tz);
448
449 for (count = 0; count < tz->trips; count++)
450 handle_thermal_trip(tz, count);
451}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000452EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530453
454static void thermal_zone_device_check(struct work_struct *work)
455{
456 struct thermal_zone_device *tz = container_of(work, struct
457 thermal_zone_device,
458 poll_queue.work);
459 thermal_zone_device_update(tz);
460}
461
Zhang Rui203d3d42008-01-17 15:51:08 +0800462/* sys I/F for thermal zone */
463
464#define to_thermal_zone(_dev) \
465 container_of(_dev, struct thermal_zone_device, device)
466
467static ssize_t
468type_show(struct device *dev, struct device_attribute *attr, char *buf)
469{
470 struct thermal_zone_device *tz = to_thermal_zone(dev);
471
472 return sprintf(buf, "%s\n", tz->type);
473}
474
475static ssize_t
476temp_show(struct device *dev, struct device_attribute *attr, char *buf)
477{
478 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000479 long temperature;
480 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800481
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000482 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000483
484 if (ret)
485 return ret;
486
487 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800488}
489
490static ssize_t
491mode_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 enum thermal_device_mode mode;
495 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800496
497 if (!tz->ops->get_mode)
498 return -EPERM;
499
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000500 result = tz->ops->get_mode(tz, &mode);
501 if (result)
502 return result;
503
504 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
505 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800506}
507
508static ssize_t
509mode_store(struct device *dev, struct device_attribute *attr,
510 const char *buf, size_t count)
511{
512 struct thermal_zone_device *tz = to_thermal_zone(dev);
513 int result;
514
515 if (!tz->ops->set_mode)
516 return -EPERM;
517
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530518 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000519 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530520 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000521 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
522 else
523 result = -EINVAL;
524
Zhang Rui203d3d42008-01-17 15:51:08 +0800525 if (result)
526 return result;
527
528 return count;
529}
530
531static ssize_t
532trip_point_type_show(struct device *dev, struct device_attribute *attr,
533 char *buf)
534{
535 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000536 enum thermal_trip_type type;
537 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800538
539 if (!tz->ops->get_trip_type)
540 return -EPERM;
541
542 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
543 return -EINVAL;
544
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000545 result = tz->ops->get_trip_type(tz, trip, &type);
546 if (result)
547 return result;
548
549 switch (type) {
550 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300551 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000552 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300553 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000554 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300555 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000556 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300557 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000558 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300559 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000560 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800561}
562
563static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800564trip_point_temp_store(struct device *dev, struct device_attribute *attr,
565 const char *buf, size_t count)
566{
567 struct thermal_zone_device *tz = to_thermal_zone(dev);
568 int trip, ret;
569 unsigned long temperature;
570
571 if (!tz->ops->set_trip_temp)
572 return -EPERM;
573
574 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
575 return -EINVAL;
576
577 if (kstrtoul(buf, 10, &temperature))
578 return -EINVAL;
579
580 ret = tz->ops->set_trip_temp(tz, trip, temperature);
581
582 return ret ? ret : count;
583}
584
585static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800586trip_point_temp_show(struct device *dev, struct device_attribute *attr,
587 char *buf)
588{
589 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000590 int trip, ret;
591 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800592
593 if (!tz->ops->get_trip_temp)
594 return -EPERM;
595
596 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
597 return -EINVAL;
598
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000599 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
600
601 if (ret)
602 return ret;
603
604 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800605}
606
Matthew Garrett03a971a2008-12-03 18:00:38 +0000607static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800608trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
609 const char *buf, size_t count)
610{
611 struct thermal_zone_device *tz = to_thermal_zone(dev);
612 int trip, ret;
613 unsigned long temperature;
614
615 if (!tz->ops->set_trip_hyst)
616 return -EPERM;
617
618 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
619 return -EINVAL;
620
621 if (kstrtoul(buf, 10, &temperature))
622 return -EINVAL;
623
624 /*
625 * We are not doing any check on the 'temperature' value
626 * here. The driver implementing 'set_trip_hyst' has to
627 * take care of this.
628 */
629 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
630
631 return ret ? ret : count;
632}
633
634static ssize_t
635trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
636 char *buf)
637{
638 struct thermal_zone_device *tz = to_thermal_zone(dev);
639 int trip, ret;
640 unsigned long temperature;
641
642 if (!tz->ops->get_trip_hyst)
643 return -EPERM;
644
645 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
646 return -EINVAL;
647
648 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
649
650 return ret ? ret : sprintf(buf, "%ld\n", temperature);
651}
652
653static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000654passive_store(struct device *dev, struct device_attribute *attr,
655 const char *buf, size_t count)
656{
657 struct thermal_zone_device *tz = to_thermal_zone(dev);
658 struct thermal_cooling_device *cdev = NULL;
659 int state;
660
661 if (!sscanf(buf, "%d\n", &state))
662 return -EINVAL;
663
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100664 /* sanity check: values below 1000 millicelcius don't make sense
665 * and can cause the system to go into a thermal heart attack
666 */
667 if (state && state < 1000)
668 return -EINVAL;
669
Matthew Garrett03a971a2008-12-03 18:00:38 +0000670 if (state && !tz->forced_passive) {
671 mutex_lock(&thermal_list_lock);
672 list_for_each_entry(cdev, &thermal_cdev_list, node) {
673 if (!strncmp("Processor", cdev->type,
674 sizeof("Processor")))
675 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800676 THERMAL_TRIPS_NONE, cdev,
677 THERMAL_NO_LIMIT,
678 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000679 }
680 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100681 if (!tz->passive_delay)
682 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000683 } else if (!state && tz->forced_passive) {
684 mutex_lock(&thermal_list_lock);
685 list_for_each_entry(cdev, &thermal_cdev_list, node) {
686 if (!strncmp("Processor", cdev->type,
687 sizeof("Processor")))
688 thermal_zone_unbind_cooling_device(tz,
689 THERMAL_TRIPS_NONE,
690 cdev);
691 }
692 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100693 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000694 }
695
Matthew Garrett03a971a2008-12-03 18:00:38 +0000696 tz->forced_passive = state;
697
698 thermal_zone_device_update(tz);
699
700 return count;
701}
702
703static ssize_t
704passive_show(struct device *dev, struct device_attribute *attr,
705 char *buf)
706{
707 struct thermal_zone_device *tz = to_thermal_zone(dev);
708
709 return sprintf(buf, "%d\n", tz->forced_passive);
710}
711
Durgadoss R5a2c0902012-09-18 11:04:58 +0530712static ssize_t
713policy_store(struct device *dev, struct device_attribute *attr,
714 const char *buf, size_t count)
715{
716 int ret = -EINVAL;
717 struct thermal_zone_device *tz = to_thermal_zone(dev);
718 struct thermal_governor *gov;
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000719 char name[THERMAL_NAME_LENGTH];
720
721 snprintf(name, sizeof(name), "%s", buf);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530722
723 mutex_lock(&thermal_governor_lock);
724
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000725 gov = __find_governor(strim(name));
Durgadoss R5a2c0902012-09-18 11:04:58 +0530726 if (!gov)
727 goto exit;
728
729 tz->governor = gov;
730 ret = count;
731
732exit:
733 mutex_unlock(&thermal_governor_lock);
734 return ret;
735}
736
737static ssize_t
738policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
739{
740 struct thermal_zone_device *tz = to_thermal_zone(dev);
741
742 return sprintf(buf, "%s\n", tz->governor->name);
743}
744
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000745#ifdef CONFIG_THERMAL_EMULATION
746static ssize_t
747emul_temp_store(struct device *dev, struct device_attribute *attr,
748 const char *buf, size_t count)
749{
750 struct thermal_zone_device *tz = to_thermal_zone(dev);
751 int ret = 0;
752 unsigned long temperature;
753
754 if (kstrtoul(buf, 10, &temperature))
755 return -EINVAL;
756
757 if (!tz->ops->set_emul_temp) {
758 mutex_lock(&tz->lock);
759 tz->emul_temperature = temperature;
760 mutex_unlock(&tz->lock);
761 } else {
762 ret = tz->ops->set_emul_temp(tz, temperature);
763 }
764
765 return ret ? ret : count;
766}
767static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
768#endif/*CONFIG_THERMAL_EMULATION*/
769
Zhang Rui203d3d42008-01-17 15:51:08 +0800770static DEVICE_ATTR(type, 0444, type_show, NULL);
771static DEVICE_ATTR(temp, 0444, temp_show, NULL);
772static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700773static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530774static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800775
Zhang Rui203d3d42008-01-17 15:51:08 +0800776/* sys I/F for cooling device */
777#define to_cooling_device(_dev) \
778 container_of(_dev, struct thermal_cooling_device, device)
779
780static ssize_t
781thermal_cooling_device_type_show(struct device *dev,
782 struct device_attribute *attr, char *buf)
783{
784 struct thermal_cooling_device *cdev = to_cooling_device(dev);
785
786 return sprintf(buf, "%s\n", cdev->type);
787}
788
789static ssize_t
790thermal_cooling_device_max_state_show(struct device *dev,
791 struct device_attribute *attr, char *buf)
792{
793 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000794 unsigned long state;
795 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800796
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000797 ret = cdev->ops->get_max_state(cdev, &state);
798 if (ret)
799 return ret;
800 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800801}
802
803static ssize_t
804thermal_cooling_device_cur_state_show(struct device *dev,
805 struct device_attribute *attr, char *buf)
806{
807 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000808 unsigned long state;
809 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800810
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000811 ret = cdev->ops->get_cur_state(cdev, &state);
812 if (ret)
813 return ret;
814 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800815}
816
817static ssize_t
818thermal_cooling_device_cur_state_store(struct device *dev,
819 struct device_attribute *attr,
820 const char *buf, size_t count)
821{
822 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000823 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800824 int result;
825
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000826 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800827 return -EINVAL;
828
Roel Kluinedb94912009-12-15 22:46:50 +0100829 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800830 return -EINVAL;
831
832 result = cdev->ops->set_cur_state(cdev, state);
833 if (result)
834 return result;
835 return count;
836}
837
838static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500839__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800840static DEVICE_ATTR(max_state, 0444,
841 thermal_cooling_device_max_state_show, NULL);
842static DEVICE_ATTR(cur_state, 0644,
843 thermal_cooling_device_cur_state_show,
844 thermal_cooling_device_cur_state_store);
845
846static ssize_t
847thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500848 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800849{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800850 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800851
852 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800853 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800854
855 if (instance->trip == THERMAL_TRIPS_NONE)
856 return sprintf(buf, "-1\n");
857 else
858 return sprintf(buf, "%d\n", instance->trip);
859}
860
861/* Device management */
862
863/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000864 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
865 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +0800866 * @trip: indicates which trip point the cooling devices is
867 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000868 * @cdev: pointer to struct thermal_cooling_device
869 * @upper: the Maximum cooling state for this trip point.
870 * THERMAL_NO_LIMIT means no upper limit,
871 * and the cooling device can be in max_state.
872 * @lower: the Minimum cooling state can be used for this trip point.
873 * THERMAL_NO_LIMIT means no lower limit,
874 * and the cooling device can be in cooling state 0.
Len Brown543a9562008-02-07 16:55:08 -0500875 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000876 * This interface function bind a thermal cooling device to the certain trip
877 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500878 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000879 *
880 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800881 */
882int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
883 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800884 struct thermal_cooling_device *cdev,
885 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +0800886{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800887 struct thermal_instance *dev;
888 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500889 struct thermal_zone_device *pos1;
890 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800891 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800892 int result;
893
Len Brown543a9562008-02-07 16:55:08 -0500894 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800895 return -EINVAL;
896
Thomas Sujithc7516702008-02-15 00:58:50 -0500897 list_for_each_entry(pos1, &thermal_tz_list, node) {
898 if (pos1 == tz)
899 break;
900 }
901 list_for_each_entry(pos2, &thermal_cdev_list, node) {
902 if (pos2 == cdev)
903 break;
904 }
905
906 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800907 return -EINVAL;
908
Zhang Rui9d998422012-06-26 16:35:57 +0800909 cdev->ops->get_max_state(cdev, &max_state);
910
911 /* lower default 0, upper default max_state */
912 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
913 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
914
915 if (lower > upper || upper > max_state)
916 return -EINVAL;
917
Zhang Rui203d3d42008-01-17 15:51:08 +0800918 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800919 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800920 if (!dev)
921 return -ENOMEM;
922 dev->tz = tz;
923 dev->cdev = cdev;
924 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800925 dev->upper = upper;
926 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800927 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +0800928
Zhang Rui203d3d42008-01-17 15:51:08 +0800929 result = get_idr(&tz->idr, &tz->lock, &dev->id);
930 if (result)
931 goto free_mem;
932
933 sprintf(dev->name, "cdev%d", dev->id);
934 result =
935 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
936 if (result)
937 goto release_idr;
938
939 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700940 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800941 dev->attr.attr.name = dev->attr_name;
942 dev->attr.attr.mode = 0444;
943 dev->attr.show = thermal_cooling_device_trip_point_show;
944 result = device_create_file(&tz->device, &dev->attr);
945 if (result)
946 goto remove_symbol_link;
947
948 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800949 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800950 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +0800951 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
952 result = -EEXIST;
953 break;
954 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800955 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800956 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800957 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
958 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800959 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800960 mutex_unlock(&tz->lock);
961
962 if (!result)
963 return 0;
964
965 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b802012-03-21 12:55:02 -0700966remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800967 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b802012-03-21 12:55:02 -0700968release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +0800969 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b802012-03-21 12:55:02 -0700970free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800971 kfree(dev);
972 return result;
973}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000974EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +0800975
976/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000977 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
978 * thermal zone.
979 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +0800980 * @trip: indicates which trip point the cooling devices is
981 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000982 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -0500983 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000984 * This interface function unbind a thermal cooling device from the certain
985 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500986 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000987 *
988 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800989 */
990int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
991 int trip,
992 struct thermal_cooling_device *cdev)
993{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800994 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +0800995
996 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800997 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800998 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -0500999 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001000 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001001 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001002 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001003 mutex_unlock(&tz->lock);
1004 goto unbind;
1005 }
1006 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001007 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001008 mutex_unlock(&tz->lock);
1009
1010 return -ENODEV;
1011
Joe Perchescaca8b802012-03-21 12:55:02 -07001012unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +08001013 device_remove_file(&tz->device, &pos->attr);
1014 sysfs_remove_link(&tz->device.kobj, pos->name);
1015 release_idr(&tz->idr, &tz->lock, pos->id);
1016 kfree(pos);
1017 return 0;
1018}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001019EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001020
1021static void thermal_release(struct device *dev)
1022{
1023 struct thermal_zone_device *tz;
1024 struct thermal_cooling_device *cdev;
1025
Joe Perchescaca8b802012-03-21 12:55:02 -07001026 if (!strncmp(dev_name(dev), "thermal_zone",
1027 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001028 tz = to_thermal_zone(dev);
1029 kfree(tz);
1030 } else {
1031 cdev = to_cooling_device(dev);
1032 kfree(cdev);
1033 }
1034}
1035
1036static struct class thermal_class = {
1037 .name = "thermal",
1038 .dev_release = thermal_release,
1039};
1040
1041/**
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001042 * thermal_cooling_device_register() - register a new thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001043 * @type: the thermal cooling device type.
1044 * @devdata: device private data.
1045 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001046 *
1047 * This interface function adds a new thermal cooling device (fan/processor/...)
1048 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1049 * to all the thermal zone devices registered at the same time.
1050 *
1051 * Return: a pointer to the created struct thermal_cooling_device or an
1052 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001053 */
Joe Perchescaca8b802012-03-21 12:55:02 -07001054struct thermal_cooling_device *
1055thermal_cooling_device_register(char *type, void *devdata,
1056 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001057{
1058 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001059 int result;
1060
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001061 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001062 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001063
1064 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001065 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001066 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001067
1068 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1069 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001070 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001071
1072 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1073 if (result) {
1074 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001075 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001076 }
1077
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001078 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +08001079 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001080 INIT_LIST_HEAD(&cdev->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001081 cdev->ops = ops;
Zhang Ruice119f82012-06-27 14:13:04 +08001082 cdev->updated = true;
Zhang Rui203d3d42008-01-17 15:51:08 +08001083 cdev->device.class = &thermal_class;
1084 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001085 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001086 result = device_register(&cdev->device);
1087 if (result) {
1088 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1089 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001090 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001091 }
1092
1093 /* sys I/F */
1094 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001095 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001096 if (result)
1097 goto unregister;
1098 }
1099
1100 result = device_create_file(&cdev->device, &dev_attr_max_state);
1101 if (result)
1102 goto unregister;
1103
1104 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1105 if (result)
1106 goto unregister;
1107
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301108 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001109 mutex_lock(&thermal_list_lock);
1110 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001111 mutex_unlock(&thermal_list_lock);
1112
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301113 /* Update binding information for 'this' new cdev */
1114 bind_cdev(cdev);
1115
1116 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001117
Joe Perchescaca8b802012-03-21 12:55:02 -07001118unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001119 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1120 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001121 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001122}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001123EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001124
1125/**
1126 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001127 * @cdev: the thermal cooling device to remove.
1128 *
1129 * thermal_cooling_device_unregister() must be called when the device is no
1130 * longer needed.
1131 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301132void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001133{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301134 int i;
1135 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001136 struct thermal_zone_device *tz;
1137 struct thermal_cooling_device *pos = NULL;
1138
1139 if (!cdev)
1140 return;
1141
1142 mutex_lock(&thermal_list_lock);
1143 list_for_each_entry(pos, &thermal_cdev_list, node)
1144 if (pos == cdev)
1145 break;
1146 if (pos != cdev) {
1147 /* thermal cooling device not found */
1148 mutex_unlock(&thermal_list_lock);
1149 return;
1150 }
1151 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301152
1153 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001154 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301155 if (tz->ops->unbind) {
1156 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001157 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301158 }
1159
1160 if (!tz->tzp || !tz->tzp->tbp)
1161 continue;
1162
1163 tzp = tz->tzp;
1164 for (i = 0; i < tzp->num_tbps; i++) {
1165 if (tzp->tbp[i].cdev == cdev) {
1166 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1167 tzp->tbp[i].cdev = NULL;
1168 }
1169 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001170 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301171
Zhang Rui203d3d42008-01-17 15:51:08 +08001172 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301173
Zhang Rui203d3d42008-01-17 15:51:08 +08001174 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001175 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001176 device_remove_file(&cdev->device, &dev_attr_max_state);
1177 device_remove_file(&cdev->device, &dev_attr_cur_state);
1178
1179 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1180 device_unregister(&cdev->device);
1181 return;
1182}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001183EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001184
Durgadoss Rdc765482012-09-18 11:05:00 +05301185void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001186{
1187 struct thermal_instance *instance;
1188 unsigned long target = 0;
1189
1190 /* cooling device is updated*/
1191 if (cdev->updated)
1192 return;
1193
Zhang Ruif4a821c2012-07-24 16:56:21 +08001194 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001195 /* Make sure cdev enters the deepest cooling state */
1196 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1197 if (instance->target == THERMAL_NO_TARGET)
1198 continue;
1199 if (instance->target > target)
1200 target = instance->target;
1201 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001202 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001203 cdev->ops->set_cur_state(cdev, target);
1204 cdev->updated = true;
1205}
Durgadoss Rdc765482012-09-18 11:05:00 +05301206EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001207
Matthew Garrettb1569e92008-12-03 17:55:32 +00001208/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001209 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301210 * @tz: thermal zone device
1211 * @trip: indicates which trip point has been crossed
1212 *
1213 * This function handles the trip events from sensor drivers. It starts
1214 * throttling the cooling devices according to the policy configured.
1215 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1216 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1217 * The throttling policy is based on the configured platform data; if no
1218 * platform data is provided, this uses the step_wise throttling policy.
1219 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001220void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301221{
1222 handle_thermal_trip(tz, trip);
1223}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001224EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301225
1226/**
Eduardo Valentin269c1742013-04-23 21:48:19 +00001227 * create_trip_attrs() - create attributes for trip points
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001228 * @tz: the thermal zone device
1229 * @mask: Writeable trip point bitmap.
Eduardo Valentin269c1742013-04-23 21:48:19 +00001230 *
1231 * helper function to instantiate sysfs entries for every trip
1232 * point and its properties of a struct thermal_zone_device.
1233 *
1234 * Return: 0 on success, the proper error value otherwise.
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001235 */
1236static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1237{
1238 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001239 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001240
Durgadoss R27365a62012-07-25 10:10:59 +08001241 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001242 if (!tz->trip_type_attrs)
1243 return -ENOMEM;
1244
Durgadoss R27365a62012-07-25 10:10:59 +08001245 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001246 if (!tz->trip_temp_attrs) {
1247 kfree(tz->trip_type_attrs);
1248 return -ENOMEM;
1249 }
1250
Durgadoss R27365a62012-07-25 10:10:59 +08001251 if (tz->ops->get_trip_hyst) {
1252 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1253 if (!tz->trip_hyst_attrs) {
1254 kfree(tz->trip_type_attrs);
1255 kfree(tz->trip_temp_attrs);
1256 return -ENOMEM;
1257 }
1258 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001259
Durgadoss R27365a62012-07-25 10:10:59 +08001260
1261 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001262 /* create trip type attribute */
1263 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1264 "trip_point_%d_type", indx);
1265
1266 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1267 tz->trip_type_attrs[indx].attr.attr.name =
1268 tz->trip_type_attrs[indx].name;
1269 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1270 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1271
1272 device_create_file(&tz->device,
1273 &tz->trip_type_attrs[indx].attr);
1274
1275 /* create trip temp attribute */
1276 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1277 "trip_point_%d_temp", indx);
1278
1279 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1280 tz->trip_temp_attrs[indx].attr.attr.name =
1281 tz->trip_temp_attrs[indx].name;
1282 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1283 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1284 if (mask & (1 << indx)) {
1285 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1286 tz->trip_temp_attrs[indx].attr.store =
1287 trip_point_temp_store;
1288 }
1289
1290 device_create_file(&tz->device,
1291 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001292
1293 /* create Optional trip hyst attribute */
1294 if (!tz->ops->get_trip_hyst)
1295 continue;
1296 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1297 "trip_point_%d_hyst", indx);
1298
1299 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1300 tz->trip_hyst_attrs[indx].attr.attr.name =
1301 tz->trip_hyst_attrs[indx].name;
1302 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1303 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1304 if (tz->ops->set_trip_hyst) {
1305 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1306 tz->trip_hyst_attrs[indx].attr.store =
1307 trip_point_hyst_store;
1308 }
1309
1310 device_create_file(&tz->device,
1311 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001312 }
1313 return 0;
1314}
1315
1316static void remove_trip_attrs(struct thermal_zone_device *tz)
1317{
1318 int indx;
1319
1320 for (indx = 0; indx < tz->trips; indx++) {
1321 device_remove_file(&tz->device,
1322 &tz->trip_type_attrs[indx].attr);
1323 device_remove_file(&tz->device,
1324 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001325 if (tz->ops->get_trip_hyst)
1326 device_remove_file(&tz->device,
1327 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001328 }
1329 kfree(tz->trip_type_attrs);
1330 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001331 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001332}
1333
1334/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001335 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001336 * @type: the thermal zone device type
1337 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001338 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001339 * @devdata: private device data
1340 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301341 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001342 * @passive_delay: number of milliseconds to wait between polls when
1343 * performing passive cooling
1344 * @polling_delay: number of milliseconds to wait between polls when checking
1345 * whether trip points have been crossed (0 for interrupt
1346 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001347 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001348 * This interface function adds a new thermal zone device (sensor) to
1349 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1350 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001351 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001352 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001353 *
1354 * Return: a pointer to the created struct thermal_zone_device or an
1355 * in case of error, an ERR_PTR. Caller must check return value with
1356 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001357 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001358struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001359 int trips, int mask, void *devdata,
Alan Cox5b275ce2010-11-11 15:27:29 +00001360 const struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301361 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001362 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001363{
1364 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001365 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001366 int result;
1367 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001368 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001369
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001370 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001371 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001372
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001373 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001374 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001375
1376 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001377 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001378
Jonghwa Lee83720d02013-05-18 09:50:26 +00001379 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001380 return ERR_PTR(-EINVAL);
1381
Zhang Rui203d3d42008-01-17 15:51:08 +08001382 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1383 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001384 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001385
Zhang Rui2d374132012-06-27 10:09:00 +08001386 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001387 idr_init(&tz->idr);
1388 mutex_init(&tz->lock);
1389 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1390 if (result) {
1391 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001392 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001393 }
1394
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001395 strlcpy(tz->type, type ? : "", sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001396 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301397 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001398 tz->device.class = &thermal_class;
1399 tz->devdata = devdata;
1400 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001401 tz->passive_delay = passive_delay;
1402 tz->polling_delay = polling_delay;
1403
Kay Sievers354655e2009-01-06 10:44:37 -08001404 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001405 result = device_register(&tz->device);
1406 if (result) {
1407 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1408 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001409 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001410 }
1411
1412 /* sys I/F */
1413 if (type) {
1414 result = device_create_file(&tz->device, &dev_attr_type);
1415 if (result)
1416 goto unregister;
1417 }
1418
1419 result = device_create_file(&tz->device, &dev_attr_temp);
1420 if (result)
1421 goto unregister;
1422
1423 if (ops->get_mode) {
1424 result = device_create_file(&tz->device, &dev_attr_mode);
1425 if (result)
1426 goto unregister;
1427 }
1428
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001429 result = create_trip_attrs(tz, mask);
1430 if (result)
1431 goto unregister;
1432
Zhang Rui203d3d42008-01-17 15:51:08 +08001433 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001434 tz->ops->get_trip_type(tz, count, &trip_type);
1435 if (trip_type == THERMAL_TRIP_PASSIVE)
1436 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001437 }
1438
Durgadoss R5a2c0902012-09-18 11:04:58 +05301439 if (!passive) {
1440 result = device_create_file(&tz->device, &dev_attr_passive);
1441 if (result)
1442 goto unregister;
1443 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001444
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001445#ifdef CONFIG_THERMAL_EMULATION
1446 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1447 if (result)
1448 goto unregister;
1449#endif
Durgadoss R5a2c0902012-09-18 11:04:58 +05301450 /* Create policy attribute */
1451 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001452 if (result)
1453 goto unregister;
1454
Durgadoss Ra4a15482012-09-18 11:04:57 +05301455 /* Update 'this' zone's governor information */
1456 mutex_lock(&thermal_governor_lock);
1457
1458 if (tz->tzp)
1459 tz->governor = __find_governor(tz->tzp->governor_name);
1460 else
1461 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1462
1463 mutex_unlock(&thermal_governor_lock);
1464
Zhang Ruie68b16a2008-04-21 16:07:52 +08001465 result = thermal_add_hwmon_sysfs(tz);
1466 if (result)
1467 goto unregister;
1468
Zhang Rui203d3d42008-01-17 15:51:08 +08001469 mutex_lock(&thermal_list_lock);
1470 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001471 mutex_unlock(&thermal_list_lock);
1472
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301473 /* Bind cooling devices for this zone */
1474 bind_tz(tz);
1475
Matthew Garrettb1569e92008-12-03 17:55:32 +00001476 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1477
1478 thermal_zone_device_update(tz);
1479
Zhang Rui203d3d42008-01-17 15:51:08 +08001480 if (!result)
1481 return tz;
1482
Joe Perchescaca8b802012-03-21 12:55:02 -07001483unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001484 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1485 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001486 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001487}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001488EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001489
1490/**
1491 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001492 * @tz: the thermal zone device to remove
1493 */
1494void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1495{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301496 int i;
1497 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001498 struct thermal_cooling_device *cdev;
1499 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001500
1501 if (!tz)
1502 return;
1503
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301504 tzp = tz->tzp;
1505
Zhang Rui203d3d42008-01-17 15:51:08 +08001506 mutex_lock(&thermal_list_lock);
1507 list_for_each_entry(pos, &thermal_tz_list, node)
1508 if (pos == tz)
1509 break;
1510 if (pos != tz) {
1511 /* thermal zone device not found */
1512 mutex_unlock(&thermal_list_lock);
1513 return;
1514 }
1515 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301516
1517 /* Unbind all cdevs associated with 'this' thermal zone */
1518 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1519 if (tz->ops->unbind) {
1520 tz->ops->unbind(tz, cdev);
1521 continue;
1522 }
1523
1524 if (!tzp || !tzp->tbp)
1525 break;
1526
1527 for (i = 0; i < tzp->num_tbps; i++) {
1528 if (tzp->tbp[i].cdev == cdev) {
1529 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1530 tzp->tbp[i].cdev = NULL;
1531 }
1532 }
1533 }
1534
Zhang Rui203d3d42008-01-17 15:51:08 +08001535 mutex_unlock(&thermal_list_lock);
1536
Matthew Garrettb1569e92008-12-03 17:55:32 +00001537 thermal_zone_device_set_polling(tz, 0);
1538
Zhang Rui203d3d42008-01-17 15:51:08 +08001539 if (tz->type[0])
1540 device_remove_file(&tz->device, &dev_attr_type);
1541 device_remove_file(&tz->device, &dev_attr_temp);
1542 if (tz->ops->get_mode)
1543 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301544 device_remove_file(&tz->device, &dev_attr_policy);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001545 remove_trip_attrs(tz);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301546 tz->governor = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001547
Zhang Ruie68b16a2008-04-21 16:07:52 +08001548 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001549 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1550 idr_destroy(&tz->idr);
1551 mutex_destroy(&tz->lock);
1552 device_unregister(&tz->device);
1553 return;
1554}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001555EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001556
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001557/**
1558 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1559 * @name: thermal zone name to fetch the temperature
1560 *
1561 * When only one zone is found with the passed name, returns a reference to it.
1562 *
1563 * Return: On success returns a reference to an unique thermal zone with
1564 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1565 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1566 */
1567struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1568{
1569 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1570 unsigned int found = 0;
1571
1572 if (!name)
1573 goto exit;
1574
1575 mutex_lock(&thermal_list_lock);
1576 list_for_each_entry(pos, &thermal_tz_list, node)
1577 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1578 found++;
1579 ref = pos;
1580 }
1581 mutex_unlock(&thermal_list_lock);
1582
1583 /* nothing has been found, thus an error code for it */
1584 if (found == 0)
1585 ref = ERR_PTR(-ENODEV);
1586 else if (found > 1)
1587 /* Success only when an unique zone is found */
1588 ref = ERR_PTR(-EEXIST);
1589
1590exit:
1591 return ref;
1592}
1593EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1594
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001595#ifdef CONFIG_NET
1596static struct genl_family thermal_event_genl_family = {
1597 .id = GENL_ID_GENERATE,
1598 .name = THERMAL_GENL_FAMILY_NAME,
1599 .version = THERMAL_GENL_VERSION,
1600 .maxattr = THERMAL_GENL_ATTR_MAX,
1601};
1602
1603static struct genl_multicast_group thermal_event_mcgrp = {
1604 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1605};
1606
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001607int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1608 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301609{
1610 struct sk_buff *skb;
1611 struct nlattr *attr;
1612 struct thermal_genl_event *thermal_event;
1613 void *msg_header;
1614 int size;
1615 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001616 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301617
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001618 if (!tz)
1619 return -EINVAL;
1620
R.Durgadoss4cb18722010-10-27 03:33:29 +05301621 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001622 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1623 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301624
1625 skb = genlmsg_new(size, GFP_ATOMIC);
1626 if (!skb)
1627 return -ENOMEM;
1628
1629 /* add the genetlink message header */
1630 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1631 &thermal_event_genl_family, 0,
1632 THERMAL_GENL_CMD_EVENT);
1633 if (!msg_header) {
1634 nlmsg_free(skb);
1635 return -ENOMEM;
1636 }
1637
1638 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001639 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1640 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301641
1642 if (!attr) {
1643 nlmsg_free(skb);
1644 return -EINVAL;
1645 }
1646
1647 thermal_event = nla_data(attr);
1648 if (!thermal_event) {
1649 nlmsg_free(skb);
1650 return -EINVAL;
1651 }
1652
1653 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1654
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001655 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301656 thermal_event->event = event;
1657
1658 /* send multicast genetlink message */
1659 result = genlmsg_end(skb, msg_header);
1660 if (result < 0) {
1661 nlmsg_free(skb);
1662 return result;
1663 }
1664
1665 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1666 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001667 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301668
1669 return result;
1670}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001671EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301672
1673static int genetlink_init(void)
1674{
1675 int result;
1676
1677 result = genl_register_family(&thermal_event_genl_family);
1678 if (result)
1679 return result;
1680
1681 result = genl_register_mc_group(&thermal_event_genl_family,
1682 &thermal_event_mcgrp);
1683 if (result)
1684 genl_unregister_family(&thermal_event_genl_family);
1685 return result;
1686}
1687
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001688static void genetlink_exit(void)
1689{
1690 genl_unregister_family(&thermal_event_genl_family);
1691}
1692#else /* !CONFIG_NET */
1693static inline int genetlink_init(void) { return 0; }
1694static inline void genetlink_exit(void) {}
1695#endif /* !CONFIG_NET */
1696
Zhang Rui80a26a52013-03-26 16:38:29 +08001697static int __init thermal_register_governors(void)
1698{
1699 int result;
1700
1701 result = thermal_gov_step_wise_register();
1702 if (result)
1703 return result;
1704
1705 result = thermal_gov_fair_share_register();
1706 if (result)
1707 return result;
1708
1709 return thermal_gov_user_space_register();
1710}
1711
1712static void thermal_unregister_governors(void)
1713{
1714 thermal_gov_step_wise_unregister();
1715 thermal_gov_fair_share_unregister();
1716 thermal_gov_user_space_unregister();
1717}
1718
Zhang Rui203d3d42008-01-17 15:51:08 +08001719static int __init thermal_init(void)
1720{
Zhang Rui80a26a52013-03-26 16:38:29 +08001721 int result;
1722
1723 result = thermal_register_governors();
1724 if (result)
1725 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08001726
1727 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001728 if (result)
1729 goto unregister_governors;
1730
R.Durgadoss4cb18722010-10-27 03:33:29 +05301731 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08001732 if (result)
1733 goto unregister_class;
1734
1735 return 0;
1736
1737unregister_governors:
1738 thermal_unregister_governors();
1739unregister_class:
1740 class_unregister(&thermal_class);
1741error:
1742 idr_destroy(&thermal_tz_idr);
1743 idr_destroy(&thermal_cdev_idr);
1744 mutex_destroy(&thermal_idr_lock);
1745 mutex_destroy(&thermal_list_lock);
1746 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001747 return result;
1748}
1749
1750static void __exit thermal_exit(void)
1751{
Zhang Rui80a26a52013-03-26 16:38:29 +08001752 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001753 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001754 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08001755 idr_destroy(&thermal_tz_idr);
1756 idr_destroy(&thermal_cdev_idr);
1757 mutex_destroy(&thermal_idr_lock);
1758 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08001759 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001760}
1761
R.Durgadoss4cb18722010-10-27 03:33:29 +05301762fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001763module_exit(thermal_exit);