blob: 1f02e8edb45c9f6b36dae08a563b47efecf4c99d [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"
41
Zhang Rui63c4ec92008-04-21 16:07:13 +080042MODULE_AUTHOR("Zhang Rui");
Zhang Rui203d3d42008-01-17 15:51:08 +080043MODULE_DESCRIPTION("Generic thermal management sysfs support");
Eduardo Valentin6d8d4972013-04-23 21:48:13 +000044MODULE_LICENSE("GPL v2");
Zhang Rui203d3d42008-01-17 15:51:08 +080045
Zhang Rui203d3d42008-01-17 15:51:08 +080046static DEFINE_IDR(thermal_tz_idr);
47static DEFINE_IDR(thermal_cdev_idr);
48static DEFINE_MUTEX(thermal_idr_lock);
49
50static LIST_HEAD(thermal_tz_list);
51static LIST_HEAD(thermal_cdev_list);
Durgadoss Ra4a15482012-09-18 11:04:57 +053052static LIST_HEAD(thermal_governor_list);
53
Zhang Rui203d3d42008-01-17 15:51:08 +080054static DEFINE_MUTEX(thermal_list_lock);
Durgadoss Ra4a15482012-09-18 11:04:57 +053055static DEFINE_MUTEX(thermal_governor_lock);
56
57static struct thermal_governor *__find_governor(const char *name)
58{
59 struct thermal_governor *pos;
60
61 list_for_each_entry(pos, &thermal_governor_list, governor_list)
62 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
63 return pos;
64
65 return NULL;
66}
67
68int thermal_register_governor(struct thermal_governor *governor)
69{
70 int err;
71 const char *name;
72 struct thermal_zone_device *pos;
73
74 if (!governor)
75 return -EINVAL;
76
77 mutex_lock(&thermal_governor_lock);
78
79 err = -EBUSY;
80 if (__find_governor(governor->name) == NULL) {
81 err = 0;
82 list_add(&governor->governor_list, &thermal_governor_list);
83 }
84
85 mutex_lock(&thermal_list_lock);
86
87 list_for_each_entry(pos, &thermal_tz_list, node) {
88 if (pos->governor)
89 continue;
90 if (pos->tzp)
91 name = pos->tzp->governor_name;
92 else
93 name = DEFAULT_THERMAL_GOVERNOR;
94 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
95 pos->governor = governor;
96 }
97
98 mutex_unlock(&thermal_list_lock);
99 mutex_unlock(&thermal_governor_lock);
100
101 return err;
102}
Durgadoss Ra4a15482012-09-18 11:04:57 +0530103
104void thermal_unregister_governor(struct thermal_governor *governor)
105{
106 struct thermal_zone_device *pos;
107
108 if (!governor)
109 return;
110
111 mutex_lock(&thermal_governor_lock);
112
113 if (__find_governor(governor->name) == NULL)
114 goto exit;
115
116 mutex_lock(&thermal_list_lock);
117
118 list_for_each_entry(pos, &thermal_tz_list, node) {
119 if (!strnicmp(pos->governor->name, governor->name,
120 THERMAL_NAME_LENGTH))
121 pos->governor = NULL;
122 }
123
124 mutex_unlock(&thermal_list_lock);
125 list_del(&governor->governor_list);
126exit:
127 mutex_unlock(&thermal_governor_lock);
128 return;
129}
Zhang Rui203d3d42008-01-17 15:51:08 +0800130
131static int get_idr(struct idr *idr, struct mutex *lock, int *id)
132{
Tejun Heo6deb69f2013-02-27 17:04:46 -0800133 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800134
135 if (lock)
136 mutex_lock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800137 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800138 if (lock)
139 mutex_unlock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800140 if (unlikely(ret < 0))
141 return ret;
142 *id = ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800143 return 0;
144}
145
146static void release_idr(struct idr *idr, struct mutex *lock, int id)
147{
148 if (lock)
149 mutex_lock(lock);
150 idr_remove(idr, id);
151 if (lock)
152 mutex_unlock(lock);
153}
154
Durgadoss R9b4298a2012-09-18 11:04:54 +0530155int get_tz_trend(struct thermal_zone_device *tz, int trip)
156{
157 enum thermal_trend trend;
158
Eduardo Valentin0c872502013-05-29 21:37:00 +0000159 if (tz->emul_temperature || !tz->ops->get_trend ||
160 tz->ops->get_trend(tz, trip, &trend)) {
Durgadoss R9b4298a2012-09-18 11:04:54 +0530161 if (tz->temperature > tz->last_temperature)
162 trend = THERMAL_TREND_RAISING;
163 else if (tz->temperature < tz->last_temperature)
164 trend = THERMAL_TREND_DROPPING;
165 else
166 trend = THERMAL_TREND_STABLE;
167 }
168
169 return trend;
170}
171EXPORT_SYMBOL(get_tz_trend);
172
173struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
174 struct thermal_cooling_device *cdev, int trip)
175{
176 struct thermal_instance *pos = NULL;
177 struct thermal_instance *target_instance = NULL;
178
179 mutex_lock(&tz->lock);
180 mutex_lock(&cdev->lock);
181
182 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
183 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
184 target_instance = pos;
185 break;
186 }
187 }
188
189 mutex_unlock(&cdev->lock);
190 mutex_unlock(&tz->lock);
191
192 return target_instance;
193}
194EXPORT_SYMBOL(get_thermal_instance);
195
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530196static void print_bind_err_msg(struct thermal_zone_device *tz,
197 struct thermal_cooling_device *cdev, int ret)
198{
199 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
200 tz->type, cdev->type, ret);
201}
202
203static void __bind(struct thermal_zone_device *tz, int mask,
204 struct thermal_cooling_device *cdev)
205{
206 int i, ret;
207
208 for (i = 0; i < tz->trips; i++) {
209 if (mask & (1 << i)) {
210 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
211 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
212 if (ret)
213 print_bind_err_msg(tz, cdev, ret);
214 }
215 }
216}
217
218static void __unbind(struct thermal_zone_device *tz, int mask,
219 struct thermal_cooling_device *cdev)
220{
221 int i;
222
223 for (i = 0; i < tz->trips; i++)
224 if (mask & (1 << i))
225 thermal_zone_unbind_cooling_device(tz, i, cdev);
226}
227
228static void bind_cdev(struct thermal_cooling_device *cdev)
229{
230 int i, ret;
231 const struct thermal_zone_params *tzp;
232 struct thermal_zone_device *pos = NULL;
233
234 mutex_lock(&thermal_list_lock);
235
236 list_for_each_entry(pos, &thermal_tz_list, node) {
237 if (!pos->tzp && !pos->ops->bind)
238 continue;
239
240 if (!pos->tzp && pos->ops->bind) {
241 ret = pos->ops->bind(pos, cdev);
242 if (ret)
243 print_bind_err_msg(pos, cdev, ret);
244 }
245
246 tzp = pos->tzp;
Hugh Dickins791700c2012-09-27 16:48:28 -0700247 if (!tzp || !tzp->tbp)
248 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530249
250 for (i = 0; i < tzp->num_tbps; i++) {
251 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
252 continue;
253 if (tzp->tbp[i].match(pos, cdev))
254 continue;
255 tzp->tbp[i].cdev = cdev;
256 __bind(pos, tzp->tbp[i].trip_mask, cdev);
257 }
258 }
259
260 mutex_unlock(&thermal_list_lock);
261}
262
263static void bind_tz(struct thermal_zone_device *tz)
264{
265 int i, ret;
266 struct thermal_cooling_device *pos = NULL;
267 const struct thermal_zone_params *tzp = tz->tzp;
268
269 if (!tzp && !tz->ops->bind)
270 return;
271
272 mutex_lock(&thermal_list_lock);
273
274 /* If there is no platform data, try to use ops->bind */
275 if (!tzp && tz->ops->bind) {
276 list_for_each_entry(pos, &thermal_cdev_list, node) {
277 ret = tz->ops->bind(tz, pos);
278 if (ret)
279 print_bind_err_msg(tz, pos, ret);
280 }
281 goto exit;
282 }
283
Hugh Dickins791700c2012-09-27 16:48:28 -0700284 if (!tzp || !tzp->tbp)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530285 goto exit;
286
287 list_for_each_entry(pos, &thermal_cdev_list, node) {
288 for (i = 0; i < tzp->num_tbps; i++) {
289 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
290 continue;
291 if (tzp->tbp[i].match(tz, pos))
292 continue;
293 tzp->tbp[i].cdev = pos;
294 __bind(tz, tzp->tbp[i].trip_mask, pos);
295 }
296 }
297exit:
298 mutex_unlock(&thermal_list_lock);
299}
300
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530301static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
302 int delay)
303{
304 if (delay > 1000)
305 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
306 round_jiffies(msecs_to_jiffies(delay)));
307 else if (delay)
308 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
309 msecs_to_jiffies(delay));
310 else
311 cancel_delayed_work(&tz->poll_queue);
312}
313
314static void monitor_thermal_zone(struct thermal_zone_device *tz)
315{
316 mutex_lock(&tz->lock);
317
318 if (tz->passive)
319 thermal_zone_device_set_polling(tz, tz->passive_delay);
320 else if (tz->polling_delay)
321 thermal_zone_device_set_polling(tz, tz->polling_delay);
322 else
323 thermal_zone_device_set_polling(tz, 0);
324
325 mutex_unlock(&tz->lock);
326}
327
328static void handle_non_critical_trips(struct thermal_zone_device *tz,
329 int trip, enum thermal_trip_type trip_type)
330{
Zhang Ruid567c682012-12-12 15:23:54 +0800331 if (tz->governor)
332 tz->governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530333}
334
335static void handle_critical_trips(struct thermal_zone_device *tz,
336 int trip, enum thermal_trip_type trip_type)
337{
338 long trip_temp;
339
340 tz->ops->get_trip_temp(tz, trip, &trip_temp);
341
342 /* If we have not crossed the trip_temp, we do not care. */
343 if (tz->temperature < trip_temp)
344 return;
345
346 if (tz->ops->notify)
347 tz->ops->notify(tz, trip, trip_type);
348
349 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000350 dev_emerg(&tz->device,
351 "critical temperature reached(%d C),shutting down\n",
352 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530353 orderly_poweroff(true);
354 }
355}
356
357static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
358{
359 enum thermal_trip_type type;
360
361 tz->ops->get_trip_type(tz, trip, &type);
362
363 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
364 handle_critical_trips(tz, trip, type);
365 else
366 handle_non_critical_trips(tz, trip, type);
367 /*
368 * Alright, we handled this trip successfully.
369 * So, start monitoring again.
370 */
371 monitor_thermal_zone(tz);
372}
373
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000374/**
375 * thermal_zone_get_temp() - returns its the temperature of thermal zone
376 * @tz: a valid pointer to a struct thermal_zone_device
377 * @temp: a valid pointer to where to store the resulting temperature.
378 *
379 * When a valid thermal zone reference is passed, it will fetch its
380 * temperature and fill @temp.
381 *
382 * Return: On success returns 0, an error code otherwise
383 */
384int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000385{
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000386 int ret = -EINVAL;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800387#ifdef CONFIG_THERMAL_EMULATION
388 int count;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000389 unsigned long crit_temp = -1UL;
390 enum thermal_trip_type type;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800391#endif
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000392
Eduardo Valentin9b19ec32013-04-25 14:13:33 +0000393 if (!tz || IS_ERR(tz))
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000394 goto exit;
395
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000396 mutex_lock(&tz->lock);
397
398 ret = tz->ops->get_temp(tz, temp);
399#ifdef CONFIG_THERMAL_EMULATION
400 if (!tz->emul_temperature)
401 goto skip_emul;
402
403 for (count = 0; count < tz->trips; count++) {
404 ret = tz->ops->get_trip_type(tz, count, &type);
405 if (!ret && type == THERMAL_TRIP_CRITICAL) {
406 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
407 break;
408 }
409 }
410
411 if (ret)
412 goto skip_emul;
413
414 if (*temp < crit_temp)
415 *temp = tz->emul_temperature;
416skip_emul:
417#endif
418 mutex_unlock(&tz->lock);
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000419exit:
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000420 return ret;
421}
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000422EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000423
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530424static void update_temperature(struct thermal_zone_device *tz)
425{
426 long temp;
427 int ret;
428
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000429 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530430 if (ret) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000431 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
432 tz->id);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000433 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530434 }
435
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000436 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530437 tz->last_temperature = tz->temperature;
438 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530439 mutex_unlock(&tz->lock);
440}
441
442void thermal_zone_device_update(struct thermal_zone_device *tz)
443{
444 int count;
445
446 update_temperature(tz);
447
448 for (count = 0; count < tz->trips; count++)
449 handle_thermal_trip(tz, count);
450}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000451EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530452
453static void thermal_zone_device_check(struct work_struct *work)
454{
455 struct thermal_zone_device *tz = container_of(work, struct
456 thermal_zone_device,
457 poll_queue.work);
458 thermal_zone_device_update(tz);
459}
460
Zhang Rui203d3d42008-01-17 15:51:08 +0800461/* sys I/F for thermal zone */
462
463#define to_thermal_zone(_dev) \
464 container_of(_dev, struct thermal_zone_device, device)
465
466static ssize_t
467type_show(struct device *dev, struct device_attribute *attr, char *buf)
468{
469 struct thermal_zone_device *tz = to_thermal_zone(dev);
470
471 return sprintf(buf, "%s\n", tz->type);
472}
473
474static ssize_t
475temp_show(struct device *dev, struct device_attribute *attr, char *buf)
476{
477 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000478 long temperature;
479 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800480
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000481 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000482
483 if (ret)
484 return ret;
485
486 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800487}
488
489static ssize_t
490mode_show(struct device *dev, struct device_attribute *attr, char *buf)
491{
492 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000493 enum thermal_device_mode mode;
494 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800495
496 if (!tz->ops->get_mode)
497 return -EPERM;
498
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000499 result = tz->ops->get_mode(tz, &mode);
500 if (result)
501 return result;
502
503 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
504 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800505}
506
507static ssize_t
508mode_store(struct device *dev, struct device_attribute *attr,
509 const char *buf, size_t count)
510{
511 struct thermal_zone_device *tz = to_thermal_zone(dev);
512 int result;
513
514 if (!tz->ops->set_mode)
515 return -EPERM;
516
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530517 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000518 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530519 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000520 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
521 else
522 result = -EINVAL;
523
Zhang Rui203d3d42008-01-17 15:51:08 +0800524 if (result)
525 return result;
526
527 return count;
528}
529
530static ssize_t
531trip_point_type_show(struct device *dev, struct device_attribute *attr,
532 char *buf)
533{
534 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000535 enum thermal_trip_type type;
536 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800537
538 if (!tz->ops->get_trip_type)
539 return -EPERM;
540
541 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
542 return -EINVAL;
543
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000544 result = tz->ops->get_trip_type(tz, trip, &type);
545 if (result)
546 return result;
547
548 switch (type) {
549 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300550 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000551 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300552 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000553 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300554 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000555 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300556 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000557 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300558 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000559 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800560}
561
562static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800563trip_point_temp_store(struct device *dev, struct device_attribute *attr,
564 const char *buf, size_t count)
565{
566 struct thermal_zone_device *tz = to_thermal_zone(dev);
567 int trip, ret;
568 unsigned long temperature;
569
570 if (!tz->ops->set_trip_temp)
571 return -EPERM;
572
573 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
574 return -EINVAL;
575
576 if (kstrtoul(buf, 10, &temperature))
577 return -EINVAL;
578
579 ret = tz->ops->set_trip_temp(tz, trip, temperature);
580
581 return ret ? ret : count;
582}
583
584static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800585trip_point_temp_show(struct device *dev, struct device_attribute *attr,
586 char *buf)
587{
588 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000589 int trip, ret;
590 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800591
592 if (!tz->ops->get_trip_temp)
593 return -EPERM;
594
595 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
596 return -EINVAL;
597
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000598 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
599
600 if (ret)
601 return ret;
602
603 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800604}
605
Matthew Garrett03a971a2008-12-03 18:00:38 +0000606static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800607trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
608 const char *buf, size_t count)
609{
610 struct thermal_zone_device *tz = to_thermal_zone(dev);
611 int trip, ret;
612 unsigned long temperature;
613
614 if (!tz->ops->set_trip_hyst)
615 return -EPERM;
616
617 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
618 return -EINVAL;
619
620 if (kstrtoul(buf, 10, &temperature))
621 return -EINVAL;
622
623 /*
624 * We are not doing any check on the 'temperature' value
625 * here. The driver implementing 'set_trip_hyst' has to
626 * take care of this.
627 */
628 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
629
630 return ret ? ret : count;
631}
632
633static ssize_t
634trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
635 char *buf)
636{
637 struct thermal_zone_device *tz = to_thermal_zone(dev);
638 int trip, ret;
639 unsigned long temperature;
640
641 if (!tz->ops->get_trip_hyst)
642 return -EPERM;
643
644 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
645 return -EINVAL;
646
647 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
648
649 return ret ? ret : sprintf(buf, "%ld\n", temperature);
650}
651
652static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000653passive_store(struct device *dev, struct device_attribute *attr,
654 const char *buf, size_t count)
655{
656 struct thermal_zone_device *tz = to_thermal_zone(dev);
657 struct thermal_cooling_device *cdev = NULL;
658 int state;
659
660 if (!sscanf(buf, "%d\n", &state))
661 return -EINVAL;
662
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100663 /* sanity check: values below 1000 millicelcius don't make sense
664 * and can cause the system to go into a thermal heart attack
665 */
666 if (state && state < 1000)
667 return -EINVAL;
668
Matthew Garrett03a971a2008-12-03 18:00:38 +0000669 if (state && !tz->forced_passive) {
670 mutex_lock(&thermal_list_lock);
671 list_for_each_entry(cdev, &thermal_cdev_list, node) {
672 if (!strncmp("Processor", cdev->type,
673 sizeof("Processor")))
674 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800675 THERMAL_TRIPS_NONE, cdev,
676 THERMAL_NO_LIMIT,
677 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000678 }
679 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100680 if (!tz->passive_delay)
681 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000682 } else if (!state && tz->forced_passive) {
683 mutex_lock(&thermal_list_lock);
684 list_for_each_entry(cdev, &thermal_cdev_list, node) {
685 if (!strncmp("Processor", cdev->type,
686 sizeof("Processor")))
687 thermal_zone_unbind_cooling_device(tz,
688 THERMAL_TRIPS_NONE,
689 cdev);
690 }
691 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100692 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000693 }
694
Matthew Garrett03a971a2008-12-03 18:00:38 +0000695 tz->forced_passive = state;
696
697 thermal_zone_device_update(tz);
698
699 return count;
700}
701
702static ssize_t
703passive_show(struct device *dev, struct device_attribute *attr,
704 char *buf)
705{
706 struct thermal_zone_device *tz = to_thermal_zone(dev);
707
708 return sprintf(buf, "%d\n", tz->forced_passive);
709}
710
Durgadoss R5a2c0902012-09-18 11:04:58 +0530711static ssize_t
712policy_store(struct device *dev, struct device_attribute *attr,
713 const char *buf, size_t count)
714{
715 int ret = -EINVAL;
716 struct thermal_zone_device *tz = to_thermal_zone(dev);
717 struct thermal_governor *gov;
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000718 char name[THERMAL_NAME_LENGTH];
719
720 snprintf(name, sizeof(name), "%s", buf);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530721
722 mutex_lock(&thermal_governor_lock);
723
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000724 gov = __find_governor(strim(name));
Durgadoss R5a2c0902012-09-18 11:04:58 +0530725 if (!gov)
726 goto exit;
727
728 tz->governor = gov;
729 ret = count;
730
731exit:
732 mutex_unlock(&thermal_governor_lock);
733 return ret;
734}
735
736static ssize_t
737policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
738{
739 struct thermal_zone_device *tz = to_thermal_zone(dev);
740
741 return sprintf(buf, "%s\n", tz->governor->name);
742}
743
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000744#ifdef CONFIG_THERMAL_EMULATION
745static ssize_t
746emul_temp_store(struct device *dev, struct device_attribute *attr,
747 const char *buf, size_t count)
748{
749 struct thermal_zone_device *tz = to_thermal_zone(dev);
750 int ret = 0;
751 unsigned long temperature;
752
753 if (kstrtoul(buf, 10, &temperature))
754 return -EINVAL;
755
756 if (!tz->ops->set_emul_temp) {
757 mutex_lock(&tz->lock);
758 tz->emul_temperature = temperature;
759 mutex_unlock(&tz->lock);
760 } else {
761 ret = tz->ops->set_emul_temp(tz, temperature);
762 }
763
764 return ret ? ret : count;
765}
766static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
767#endif/*CONFIG_THERMAL_EMULATION*/
768
Zhang Rui203d3d42008-01-17 15:51:08 +0800769static DEVICE_ATTR(type, 0444, type_show, NULL);
770static DEVICE_ATTR(temp, 0444, temp_show, NULL);
771static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700772static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530773static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800774
Zhang Rui203d3d42008-01-17 15:51:08 +0800775/* sys I/F for cooling device */
776#define to_cooling_device(_dev) \
777 container_of(_dev, struct thermal_cooling_device, device)
778
779static ssize_t
780thermal_cooling_device_type_show(struct device *dev,
781 struct device_attribute *attr, char *buf)
782{
783 struct thermal_cooling_device *cdev = to_cooling_device(dev);
784
785 return sprintf(buf, "%s\n", cdev->type);
786}
787
788static ssize_t
789thermal_cooling_device_max_state_show(struct device *dev,
790 struct device_attribute *attr, char *buf)
791{
792 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000793 unsigned long state;
794 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800795
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000796 ret = cdev->ops->get_max_state(cdev, &state);
797 if (ret)
798 return ret;
799 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800800}
801
802static ssize_t
803thermal_cooling_device_cur_state_show(struct device *dev,
804 struct device_attribute *attr, char *buf)
805{
806 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000807 unsigned long state;
808 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800809
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000810 ret = cdev->ops->get_cur_state(cdev, &state);
811 if (ret)
812 return ret;
813 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800814}
815
816static ssize_t
817thermal_cooling_device_cur_state_store(struct device *dev,
818 struct device_attribute *attr,
819 const char *buf, size_t count)
820{
821 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000822 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800823 int result;
824
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000825 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800826 return -EINVAL;
827
Roel Kluinedb94912009-12-15 22:46:50 +0100828 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800829 return -EINVAL;
830
831 result = cdev->ops->set_cur_state(cdev, state);
832 if (result)
833 return result;
834 return count;
835}
836
837static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500838__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800839static DEVICE_ATTR(max_state, 0444,
840 thermal_cooling_device_max_state_show, NULL);
841static DEVICE_ATTR(cur_state, 0644,
842 thermal_cooling_device_cur_state_show,
843 thermal_cooling_device_cur_state_store);
844
845static ssize_t
846thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500847 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800848{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800849 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800850
851 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800852 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800853
854 if (instance->trip == THERMAL_TRIPS_NONE)
855 return sprintf(buf, "-1\n");
856 else
857 return sprintf(buf, "%d\n", instance->trip);
858}
859
860/* Device management */
861
Rene Herman16d75232008-06-24 19:38:56 +0200862#if defined(CONFIG_THERMAL_HWMON)
863
Zhang Ruie68b16a2008-04-21 16:07:52 +0800864/* hwmon sys I/F */
865#include <linux/hwmon.h>
Jean Delvare31f53962011-07-28 13:48:42 -0700866
867/* thermal zone devices with the same type share one hwmon device */
868struct thermal_hwmon_device {
869 char type[THERMAL_NAME_LENGTH];
870 struct device *device;
871 int count;
872 struct list_head tz_list;
873 struct list_head node;
874};
875
876struct thermal_hwmon_attr {
877 struct device_attribute attr;
878 char name[16];
879};
880
881/* one temperature input for each thermal zone */
882struct thermal_hwmon_temp {
883 struct list_head hwmon_node;
884 struct thermal_zone_device *tz;
885 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
886 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
887};
888
Zhang Ruie68b16a2008-04-21 16:07:52 +0800889static LIST_HEAD(thermal_hwmon_list);
890
891static ssize_t
892name_show(struct device *dev, struct device_attribute *attr, char *buf)
893{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700894 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800895 return sprintf(buf, "%s\n", hwmon->type);
896}
897static DEVICE_ATTR(name, 0444, name_show, NULL);
898
899static ssize_t
900temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
901{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000902 long temperature;
903 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800904 struct thermal_hwmon_attr *hwmon_attr
905 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700906 struct thermal_hwmon_temp *temp
907 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800908 temp_input);
Jean Delvare31f53962011-07-28 13:48:42 -0700909 struct thermal_zone_device *tz = temp->tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800910
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000911 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000912
913 if (ret)
914 return ret;
915
916 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800917}
918
919static ssize_t
920temp_crit_show(struct device *dev, struct device_attribute *attr,
921 char *buf)
922{
923 struct thermal_hwmon_attr *hwmon_attr
924 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700925 struct thermal_hwmon_temp *temp
926 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800927 temp_crit);
Jean Delvare31f53962011-07-28 13:48:42 -0700928 struct thermal_zone_device *tz = temp->tz;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000929 long temperature;
930 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800931
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000932 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
933 if (ret)
934 return ret;
935
936 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800937}
938
939
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700940static struct thermal_hwmon_device *
941thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
942{
943 struct thermal_hwmon_device *hwmon;
944
945 mutex_lock(&thermal_list_lock);
946 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
947 if (!strcmp(hwmon->type, tz->type)) {
948 mutex_unlock(&thermal_list_lock);
949 return hwmon;
950 }
951 mutex_unlock(&thermal_list_lock);
952
953 return NULL;
954}
955
Jean Delvare31f53962011-07-28 13:48:42 -0700956/* Find the temperature input matching a given thermal zone */
957static struct thermal_hwmon_temp *
958thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
959 const struct thermal_zone_device *tz)
960{
961 struct thermal_hwmon_temp *temp;
962
963 mutex_lock(&thermal_list_lock);
964 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
965 if (temp->tz == tz) {
966 mutex_unlock(&thermal_list_lock);
967 return temp;
968 }
969 mutex_unlock(&thermal_list_lock);
970
971 return NULL;
972}
973
Zhang Ruie68b16a2008-04-21 16:07:52 +0800974static int
975thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
976{
977 struct thermal_hwmon_device *hwmon;
Jean Delvare31f53962011-07-28 13:48:42 -0700978 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800979 int new_hwmon_device = 1;
980 int result;
981
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700982 hwmon = thermal_hwmon_lookup_by_type(tz);
983 if (hwmon) {
984 new_hwmon_device = 0;
985 goto register_sys_interface;
986 }
Zhang Ruie68b16a2008-04-21 16:07:52 +0800987
988 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
989 if (!hwmon)
990 return -ENOMEM;
991
992 INIT_LIST_HEAD(&hwmon->tz_list);
993 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
994 hwmon->device = hwmon_device_register(NULL);
995 if (IS_ERR(hwmon->device)) {
996 result = PTR_ERR(hwmon->device);
997 goto free_mem;
998 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700999 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001000 result = device_create_file(hwmon->device, &dev_attr_name);
1001 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +05301002 goto free_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001003
1004 register_sys_interface:
Jean Delvare31f53962011-07-28 13:48:42 -07001005 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
1006 if (!temp) {
1007 result = -ENOMEM;
1008 goto unregister_name;
1009 }
1010
1011 temp->tz = tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001012 hwmon->count++;
1013
Guenter Roeck79a49162012-07-21 10:53:48 +10001014 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +08001015 "temp%d_input", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -07001016 temp->temp_input.attr.attr.name = temp->temp_input.name;
1017 temp->temp_input.attr.attr.mode = 0444;
1018 temp->temp_input.attr.show = temp_input_show;
1019 sysfs_attr_init(&temp->temp_input.attr.attr);
1020 result = device_create_file(hwmon->device, &temp->temp_input.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001021 if (result)
Jean Delvare31f53962011-07-28 13:48:42 -07001022 goto free_temp_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001023
1024 if (tz->ops->get_crit_temp) {
1025 unsigned long temperature;
1026 if (!tz->ops->get_crit_temp(tz, &temperature)) {
Guenter Roeck79a49162012-07-21 10:53:48 +10001027 snprintf(temp->temp_crit.name,
1028 sizeof(temp->temp_crit.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +08001029 "temp%d_crit", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -07001030 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
1031 temp->temp_crit.attr.attr.mode = 0444;
1032 temp->temp_crit.attr.show = temp_crit_show;
1033 sysfs_attr_init(&temp->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001034 result = device_create_file(hwmon->device,
Jean Delvare31f53962011-07-28 13:48:42 -07001035 &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001036 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +05301037 goto unregister_input;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001038 }
1039 }
1040
1041 mutex_lock(&thermal_list_lock);
1042 if (new_hwmon_device)
1043 list_add_tail(&hwmon->node, &thermal_hwmon_list);
Jean Delvare31f53962011-07-28 13:48:42 -07001044 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001045 mutex_unlock(&thermal_list_lock);
1046
1047 return 0;
1048
Durgadoss Rb299eb52011-03-03 04:30:13 +05301049 unregister_input:
Jean Delvare31f53962011-07-28 13:48:42 -07001050 device_remove_file(hwmon->device, &temp->temp_input.attr);
1051 free_temp_mem:
1052 kfree(temp);
Durgadoss Rb299eb52011-03-03 04:30:13 +05301053 unregister_name:
Zhang Ruie68b16a2008-04-21 16:07:52 +08001054 if (new_hwmon_device) {
1055 device_remove_file(hwmon->device, &dev_attr_name);
1056 hwmon_device_unregister(hwmon->device);
1057 }
1058 free_mem:
1059 if (new_hwmon_device)
1060 kfree(hwmon);
1061
1062 return result;
1063}
1064
1065static void
1066thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1067{
Jean Delvare31f53962011-07-28 13:48:42 -07001068 struct thermal_hwmon_device *hwmon;
1069 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001070
Jean Delvare31f53962011-07-28 13:48:42 -07001071 hwmon = thermal_hwmon_lookup_by_type(tz);
1072 if (unlikely(!hwmon)) {
1073 /* Should never happen... */
1074 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
1075 return;
1076 }
1077
1078 temp = thermal_hwmon_lookup_temp(hwmon, tz);
1079 if (unlikely(!temp)) {
1080 /* Should never happen... */
1081 dev_dbg(&tz->device, "temperature input lookup failed!\n");
1082 return;
1083 }
1084
1085 device_remove_file(hwmon->device, &temp->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +05301086 if (tz->ops->get_crit_temp)
Jean Delvare31f53962011-07-28 13:48:42 -07001087 device_remove_file(hwmon->device, &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001088
1089 mutex_lock(&thermal_list_lock);
Jean Delvare31f53962011-07-28 13:48:42 -07001090 list_del(&temp->hwmon_node);
1091 kfree(temp);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001092 if (!list_empty(&hwmon->tz_list)) {
1093 mutex_unlock(&thermal_list_lock);
1094 return;
1095 }
1096 list_del(&hwmon->node);
1097 mutex_unlock(&thermal_list_lock);
1098
1099 device_remove_file(hwmon->device, &dev_attr_name);
1100 hwmon_device_unregister(hwmon->device);
1101 kfree(hwmon);
1102}
1103#else
1104static int
1105thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
1106{
1107 return 0;
1108}
1109
1110static void
1111thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1112{
1113}
1114#endif
1115
Zhang Rui203d3d42008-01-17 15:51:08 +08001116/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001117 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1118 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +08001119 * @trip: indicates which trip point the cooling devices is
1120 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001121 * @cdev: pointer to struct thermal_cooling_device
1122 * @upper: the Maximum cooling state for this trip point.
1123 * THERMAL_NO_LIMIT means no upper limit,
1124 * and the cooling device can be in max_state.
1125 * @lower: the Minimum cooling state can be used for this trip point.
1126 * THERMAL_NO_LIMIT means no lower limit,
1127 * and the cooling device can be in cooling state 0.
Len Brown543a9562008-02-07 16:55:08 -05001128 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001129 * This interface function bind a thermal cooling device to the certain trip
1130 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001131 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001132 *
1133 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001134 */
1135int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1136 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +08001137 struct thermal_cooling_device *cdev,
1138 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +08001139{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001140 struct thermal_instance *dev;
1141 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -05001142 struct thermal_zone_device *pos1;
1143 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +08001144 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +08001145 int result;
1146
Len Brown543a9562008-02-07 16:55:08 -05001147 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +08001148 return -EINVAL;
1149
Thomas Sujithc7516702008-02-15 00:58:50 -05001150 list_for_each_entry(pos1, &thermal_tz_list, node) {
1151 if (pos1 == tz)
1152 break;
1153 }
1154 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1155 if (pos2 == cdev)
1156 break;
1157 }
1158
1159 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +08001160 return -EINVAL;
1161
Zhang Rui9d998422012-06-26 16:35:57 +08001162 cdev->ops->get_max_state(cdev, &max_state);
1163
1164 /* lower default 0, upper default max_state */
1165 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1166 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1167
1168 if (lower > upper || upper > max_state)
1169 return -EINVAL;
1170
Zhang Rui203d3d42008-01-17 15:51:08 +08001171 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001172 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001173 if (!dev)
1174 return -ENOMEM;
1175 dev->tz = tz;
1176 dev->cdev = cdev;
1177 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +08001178 dev->upper = upper;
1179 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +08001180 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +08001181
Zhang Rui203d3d42008-01-17 15:51:08 +08001182 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1183 if (result)
1184 goto free_mem;
1185
1186 sprintf(dev->name, "cdev%d", dev->id);
1187 result =
1188 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1189 if (result)
1190 goto release_idr;
1191
1192 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -07001193 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001194 dev->attr.attr.name = dev->attr_name;
1195 dev->attr.attr.mode = 0444;
1196 dev->attr.show = thermal_cooling_device_trip_point_show;
1197 result = device_create_file(&tz->device, &dev->attr);
1198 if (result)
1199 goto remove_symbol_link;
1200
1201 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001202 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001203 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +08001204 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1205 result = -EEXIST;
1206 break;
1207 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001208 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001209 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001210 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1211 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001212 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001213 mutex_unlock(&tz->lock);
1214
1215 if (!result)
1216 return 0;
1217
1218 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -07001219remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001220 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -07001221release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001222 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -07001223free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001224 kfree(dev);
1225 return result;
1226}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001227EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001228
1229/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001230 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1231 * thermal zone.
1232 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +08001233 * @trip: indicates which trip point the cooling devices is
1234 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001235 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -05001236 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001237 * This interface function unbind a thermal cooling device from the certain
1238 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001239 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001240 *
1241 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001242 */
1243int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1244 int trip,
1245 struct thermal_cooling_device *cdev)
1246{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001247 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001248
1249 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001250 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001251 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001252 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001253 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001254 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001255 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001256 mutex_unlock(&tz->lock);
1257 goto unbind;
1258 }
1259 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001260 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001261 mutex_unlock(&tz->lock);
1262
1263 return -ENODEV;
1264
Joe Perchescaca8b82012-03-21 12:55:02 -07001265unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +08001266 device_remove_file(&tz->device, &pos->attr);
1267 sysfs_remove_link(&tz->device.kobj, pos->name);
1268 release_idr(&tz->idr, &tz->lock, pos->id);
1269 kfree(pos);
1270 return 0;
1271}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001272EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001273
1274static void thermal_release(struct device *dev)
1275{
1276 struct thermal_zone_device *tz;
1277 struct thermal_cooling_device *cdev;
1278
Joe Perchescaca8b82012-03-21 12:55:02 -07001279 if (!strncmp(dev_name(dev), "thermal_zone",
1280 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001281 tz = to_thermal_zone(dev);
1282 kfree(tz);
1283 } else {
1284 cdev = to_cooling_device(dev);
1285 kfree(cdev);
1286 }
1287}
1288
1289static struct class thermal_class = {
1290 .name = "thermal",
1291 .dev_release = thermal_release,
1292};
1293
1294/**
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001295 * thermal_cooling_device_register() - register a new thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001296 * @type: the thermal cooling device type.
1297 * @devdata: device private data.
1298 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001299 *
1300 * This interface function adds a new thermal cooling device (fan/processor/...)
1301 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1302 * to all the thermal zone devices registered at the same time.
1303 *
1304 * Return: a pointer to the created struct thermal_cooling_device or an
1305 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001306 */
Joe Perchescaca8b82012-03-21 12:55:02 -07001307struct thermal_cooling_device *
1308thermal_cooling_device_register(char *type, void *devdata,
1309 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001310{
1311 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001312 int result;
1313
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001314 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001315 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001316
1317 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001318 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001319 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001320
1321 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1322 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001323 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001324
1325 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1326 if (result) {
1327 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001328 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001329 }
1330
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001331 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +08001332 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001333 INIT_LIST_HEAD(&cdev->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001334 cdev->ops = ops;
Zhang Ruice119f82012-06-27 14:13:04 +08001335 cdev->updated = true;
Zhang Rui203d3d42008-01-17 15:51:08 +08001336 cdev->device.class = &thermal_class;
1337 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001338 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001339 result = device_register(&cdev->device);
1340 if (result) {
1341 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1342 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001343 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001344 }
1345
1346 /* sys I/F */
1347 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001348 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001349 if (result)
1350 goto unregister;
1351 }
1352
1353 result = device_create_file(&cdev->device, &dev_attr_max_state);
1354 if (result)
1355 goto unregister;
1356
1357 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1358 if (result)
1359 goto unregister;
1360
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301361 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001362 mutex_lock(&thermal_list_lock);
1363 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001364 mutex_unlock(&thermal_list_lock);
1365
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301366 /* Update binding information for 'this' new cdev */
1367 bind_cdev(cdev);
1368
1369 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001370
Joe Perchescaca8b82012-03-21 12:55:02 -07001371unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001372 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1373 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001374 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001375}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001376EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001377
1378/**
1379 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001380 * @cdev: the thermal cooling device to remove.
1381 *
1382 * thermal_cooling_device_unregister() must be called when the device is no
1383 * longer needed.
1384 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301385void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001386{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301387 int i;
1388 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001389 struct thermal_zone_device *tz;
1390 struct thermal_cooling_device *pos = NULL;
1391
1392 if (!cdev)
1393 return;
1394
1395 mutex_lock(&thermal_list_lock);
1396 list_for_each_entry(pos, &thermal_cdev_list, node)
1397 if (pos == cdev)
1398 break;
1399 if (pos != cdev) {
1400 /* thermal cooling device not found */
1401 mutex_unlock(&thermal_list_lock);
1402 return;
1403 }
1404 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301405
1406 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001407 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301408 if (tz->ops->unbind) {
1409 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001410 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301411 }
1412
1413 if (!tz->tzp || !tz->tzp->tbp)
1414 continue;
1415
1416 tzp = tz->tzp;
1417 for (i = 0; i < tzp->num_tbps; i++) {
1418 if (tzp->tbp[i].cdev == cdev) {
1419 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1420 tzp->tbp[i].cdev = NULL;
1421 }
1422 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001423 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301424
Zhang Rui203d3d42008-01-17 15:51:08 +08001425 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301426
Zhang Rui203d3d42008-01-17 15:51:08 +08001427 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001428 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001429 device_remove_file(&cdev->device, &dev_attr_max_state);
1430 device_remove_file(&cdev->device, &dev_attr_cur_state);
1431
1432 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1433 device_unregister(&cdev->device);
1434 return;
1435}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001436EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001437
Durgadoss Rdc765482012-09-18 11:05:00 +05301438void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001439{
1440 struct thermal_instance *instance;
1441 unsigned long target = 0;
1442
1443 /* cooling device is updated*/
1444 if (cdev->updated)
1445 return;
1446
Zhang Ruif4a821c2012-07-24 16:56:21 +08001447 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001448 /* Make sure cdev enters the deepest cooling state */
1449 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1450 if (instance->target == THERMAL_NO_TARGET)
1451 continue;
1452 if (instance->target > target)
1453 target = instance->target;
1454 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001455 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001456 cdev->ops->set_cur_state(cdev, target);
1457 cdev->updated = true;
1458}
Durgadoss Rdc765482012-09-18 11:05:00 +05301459EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001460
Matthew Garrettb1569e92008-12-03 17:55:32 +00001461/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001462 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301463 * @tz: thermal zone device
1464 * @trip: indicates which trip point has been crossed
1465 *
1466 * This function handles the trip events from sensor drivers. It starts
1467 * throttling the cooling devices according to the policy configured.
1468 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1469 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1470 * The throttling policy is based on the configured platform data; if no
1471 * platform data is provided, this uses the step_wise throttling policy.
1472 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001473void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301474{
1475 handle_thermal_trip(tz, trip);
1476}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001477EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301478
1479/**
Eduardo Valentin269c1742013-04-23 21:48:19 +00001480 * create_trip_attrs() - create attributes for trip points
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001481 * @tz: the thermal zone device
1482 * @mask: Writeable trip point bitmap.
Eduardo Valentin269c1742013-04-23 21:48:19 +00001483 *
1484 * helper function to instantiate sysfs entries for every trip
1485 * point and its properties of a struct thermal_zone_device.
1486 *
1487 * Return: 0 on success, the proper error value otherwise.
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001488 */
1489static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1490{
1491 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001492 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001493
Durgadoss R27365a62012-07-25 10:10:59 +08001494 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001495 if (!tz->trip_type_attrs)
1496 return -ENOMEM;
1497
Durgadoss R27365a62012-07-25 10:10:59 +08001498 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001499 if (!tz->trip_temp_attrs) {
1500 kfree(tz->trip_type_attrs);
1501 return -ENOMEM;
1502 }
1503
Durgadoss R27365a62012-07-25 10:10:59 +08001504 if (tz->ops->get_trip_hyst) {
1505 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1506 if (!tz->trip_hyst_attrs) {
1507 kfree(tz->trip_type_attrs);
1508 kfree(tz->trip_temp_attrs);
1509 return -ENOMEM;
1510 }
1511 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001512
Durgadoss R27365a62012-07-25 10:10:59 +08001513
1514 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001515 /* create trip type attribute */
1516 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1517 "trip_point_%d_type", indx);
1518
1519 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1520 tz->trip_type_attrs[indx].attr.attr.name =
1521 tz->trip_type_attrs[indx].name;
1522 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1523 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1524
1525 device_create_file(&tz->device,
1526 &tz->trip_type_attrs[indx].attr);
1527
1528 /* create trip temp attribute */
1529 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1530 "trip_point_%d_temp", indx);
1531
1532 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1533 tz->trip_temp_attrs[indx].attr.attr.name =
1534 tz->trip_temp_attrs[indx].name;
1535 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1536 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1537 if (mask & (1 << indx)) {
1538 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1539 tz->trip_temp_attrs[indx].attr.store =
1540 trip_point_temp_store;
1541 }
1542
1543 device_create_file(&tz->device,
1544 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001545
1546 /* create Optional trip hyst attribute */
1547 if (!tz->ops->get_trip_hyst)
1548 continue;
1549 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1550 "trip_point_%d_hyst", indx);
1551
1552 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1553 tz->trip_hyst_attrs[indx].attr.attr.name =
1554 tz->trip_hyst_attrs[indx].name;
1555 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1556 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1557 if (tz->ops->set_trip_hyst) {
1558 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1559 tz->trip_hyst_attrs[indx].attr.store =
1560 trip_point_hyst_store;
1561 }
1562
1563 device_create_file(&tz->device,
1564 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001565 }
1566 return 0;
1567}
1568
1569static void remove_trip_attrs(struct thermal_zone_device *tz)
1570{
1571 int indx;
1572
1573 for (indx = 0; indx < tz->trips; indx++) {
1574 device_remove_file(&tz->device,
1575 &tz->trip_type_attrs[indx].attr);
1576 device_remove_file(&tz->device,
1577 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001578 if (tz->ops->get_trip_hyst)
1579 device_remove_file(&tz->device,
1580 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001581 }
1582 kfree(tz->trip_type_attrs);
1583 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001584 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001585}
1586
1587/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001588 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001589 * @type: the thermal zone device type
1590 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001591 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001592 * @devdata: private device data
1593 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301594 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001595 * @passive_delay: number of milliseconds to wait between polls when
1596 * performing passive cooling
1597 * @polling_delay: number of milliseconds to wait between polls when checking
1598 * whether trip points have been crossed (0 for interrupt
1599 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001600 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001601 * This interface function adds a new thermal zone device (sensor) to
1602 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1603 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001604 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001605 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001606 *
1607 * Return: a pointer to the created struct thermal_zone_device or an
1608 * in case of error, an ERR_PTR. Caller must check return value with
1609 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001610 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001611struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001612 int trips, int mask, void *devdata,
Alan Cox5b275ce2010-11-11 15:27:29 +00001613 const struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301614 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001615 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001616{
1617 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001618 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001619 int result;
1620 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001621 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001622
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001623 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001624 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001625
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001626 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001627 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001628
1629 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001630 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001631
Jonghwa Lee83720d02013-05-18 09:50:26 +00001632 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001633 return ERR_PTR(-EINVAL);
1634
Zhang Rui203d3d42008-01-17 15:51:08 +08001635 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1636 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001637 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001638
Zhang Rui2d374132012-06-27 10:09:00 +08001639 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001640 idr_init(&tz->idr);
1641 mutex_init(&tz->lock);
1642 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1643 if (result) {
1644 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001645 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001646 }
1647
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001648 strlcpy(tz->type, type ? : "", sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001649 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301650 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001651 tz->device.class = &thermal_class;
1652 tz->devdata = devdata;
1653 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001654 tz->passive_delay = passive_delay;
1655 tz->polling_delay = polling_delay;
1656
Kay Sievers354655e2009-01-06 10:44:37 -08001657 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001658 result = device_register(&tz->device);
1659 if (result) {
1660 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1661 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001662 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001663 }
1664
1665 /* sys I/F */
1666 if (type) {
1667 result = device_create_file(&tz->device, &dev_attr_type);
1668 if (result)
1669 goto unregister;
1670 }
1671
1672 result = device_create_file(&tz->device, &dev_attr_temp);
1673 if (result)
1674 goto unregister;
1675
1676 if (ops->get_mode) {
1677 result = device_create_file(&tz->device, &dev_attr_mode);
1678 if (result)
1679 goto unregister;
1680 }
1681
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001682 result = create_trip_attrs(tz, mask);
1683 if (result)
1684 goto unregister;
1685
Zhang Rui203d3d42008-01-17 15:51:08 +08001686 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001687 tz->ops->get_trip_type(tz, count, &trip_type);
1688 if (trip_type == THERMAL_TRIP_PASSIVE)
1689 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001690 }
1691
Durgadoss R5a2c0902012-09-18 11:04:58 +05301692 if (!passive) {
1693 result = device_create_file(&tz->device, &dev_attr_passive);
1694 if (result)
1695 goto unregister;
1696 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001697
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001698#ifdef CONFIG_THERMAL_EMULATION
1699 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1700 if (result)
1701 goto unregister;
1702#endif
Durgadoss R5a2c0902012-09-18 11:04:58 +05301703 /* Create policy attribute */
1704 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001705 if (result)
1706 goto unregister;
1707
Durgadoss Ra4a15482012-09-18 11:04:57 +05301708 /* Update 'this' zone's governor information */
1709 mutex_lock(&thermal_governor_lock);
1710
1711 if (tz->tzp)
1712 tz->governor = __find_governor(tz->tzp->governor_name);
1713 else
1714 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1715
1716 mutex_unlock(&thermal_governor_lock);
1717
Zhang Ruie68b16a2008-04-21 16:07:52 +08001718 result = thermal_add_hwmon_sysfs(tz);
1719 if (result)
1720 goto unregister;
1721
Zhang Rui203d3d42008-01-17 15:51:08 +08001722 mutex_lock(&thermal_list_lock);
1723 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001724 mutex_unlock(&thermal_list_lock);
1725
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301726 /* Bind cooling devices for this zone */
1727 bind_tz(tz);
1728
Matthew Garrettb1569e92008-12-03 17:55:32 +00001729 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1730
1731 thermal_zone_device_update(tz);
1732
Zhang Rui203d3d42008-01-17 15:51:08 +08001733 if (!result)
1734 return tz;
1735
Joe Perchescaca8b82012-03-21 12:55:02 -07001736unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001737 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1738 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001739 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001740}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001741EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001742
1743/**
1744 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001745 * @tz: the thermal zone device to remove
1746 */
1747void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1748{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301749 int i;
1750 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001751 struct thermal_cooling_device *cdev;
1752 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001753
1754 if (!tz)
1755 return;
1756
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301757 tzp = tz->tzp;
1758
Zhang Rui203d3d42008-01-17 15:51:08 +08001759 mutex_lock(&thermal_list_lock);
1760 list_for_each_entry(pos, &thermal_tz_list, node)
1761 if (pos == tz)
1762 break;
1763 if (pos != tz) {
1764 /* thermal zone device not found */
1765 mutex_unlock(&thermal_list_lock);
1766 return;
1767 }
1768 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301769
1770 /* Unbind all cdevs associated with 'this' thermal zone */
1771 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1772 if (tz->ops->unbind) {
1773 tz->ops->unbind(tz, cdev);
1774 continue;
1775 }
1776
1777 if (!tzp || !tzp->tbp)
1778 break;
1779
1780 for (i = 0; i < tzp->num_tbps; i++) {
1781 if (tzp->tbp[i].cdev == cdev) {
1782 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1783 tzp->tbp[i].cdev = NULL;
1784 }
1785 }
1786 }
1787
Zhang Rui203d3d42008-01-17 15:51:08 +08001788 mutex_unlock(&thermal_list_lock);
1789
Matthew Garrettb1569e92008-12-03 17:55:32 +00001790 thermal_zone_device_set_polling(tz, 0);
1791
Zhang Rui203d3d42008-01-17 15:51:08 +08001792 if (tz->type[0])
1793 device_remove_file(&tz->device, &dev_attr_type);
1794 device_remove_file(&tz->device, &dev_attr_temp);
1795 if (tz->ops->get_mode)
1796 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301797 device_remove_file(&tz->device, &dev_attr_policy);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001798 remove_trip_attrs(tz);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301799 tz->governor = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001800
Zhang Ruie68b16a2008-04-21 16:07:52 +08001801 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001802 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1803 idr_destroy(&tz->idr);
1804 mutex_destroy(&tz->lock);
1805 device_unregister(&tz->device);
1806 return;
1807}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001808EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001809
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001810/**
1811 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1812 * @name: thermal zone name to fetch the temperature
1813 *
1814 * When only one zone is found with the passed name, returns a reference to it.
1815 *
1816 * Return: On success returns a reference to an unique thermal zone with
1817 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1818 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1819 */
1820struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1821{
1822 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1823 unsigned int found = 0;
1824
1825 if (!name)
1826 goto exit;
1827
1828 mutex_lock(&thermal_list_lock);
1829 list_for_each_entry(pos, &thermal_tz_list, node)
1830 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1831 found++;
1832 ref = pos;
1833 }
1834 mutex_unlock(&thermal_list_lock);
1835
1836 /* nothing has been found, thus an error code for it */
1837 if (found == 0)
1838 ref = ERR_PTR(-ENODEV);
1839 else if (found > 1)
1840 /* Success only when an unique zone is found */
1841 ref = ERR_PTR(-EEXIST);
1842
1843exit:
1844 return ref;
1845}
1846EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1847
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001848#ifdef CONFIG_NET
1849static struct genl_family thermal_event_genl_family = {
1850 .id = GENL_ID_GENERATE,
1851 .name = THERMAL_GENL_FAMILY_NAME,
1852 .version = THERMAL_GENL_VERSION,
1853 .maxattr = THERMAL_GENL_ATTR_MAX,
1854};
1855
1856static struct genl_multicast_group thermal_event_mcgrp = {
1857 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1858};
1859
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001860int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1861 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301862{
1863 struct sk_buff *skb;
1864 struct nlattr *attr;
1865 struct thermal_genl_event *thermal_event;
1866 void *msg_header;
1867 int size;
1868 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001869 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301870
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001871 if (!tz)
1872 return -EINVAL;
1873
R.Durgadoss4cb18722010-10-27 03:33:29 +05301874 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001875 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1876 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301877
1878 skb = genlmsg_new(size, GFP_ATOMIC);
1879 if (!skb)
1880 return -ENOMEM;
1881
1882 /* add the genetlink message header */
1883 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1884 &thermal_event_genl_family, 0,
1885 THERMAL_GENL_CMD_EVENT);
1886 if (!msg_header) {
1887 nlmsg_free(skb);
1888 return -ENOMEM;
1889 }
1890
1891 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001892 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1893 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301894
1895 if (!attr) {
1896 nlmsg_free(skb);
1897 return -EINVAL;
1898 }
1899
1900 thermal_event = nla_data(attr);
1901 if (!thermal_event) {
1902 nlmsg_free(skb);
1903 return -EINVAL;
1904 }
1905
1906 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1907
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001908 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301909 thermal_event->event = event;
1910
1911 /* send multicast genetlink message */
1912 result = genlmsg_end(skb, msg_header);
1913 if (result < 0) {
1914 nlmsg_free(skb);
1915 return result;
1916 }
1917
1918 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1919 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001920 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301921
1922 return result;
1923}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001924EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301925
1926static int genetlink_init(void)
1927{
1928 int result;
1929
1930 result = genl_register_family(&thermal_event_genl_family);
1931 if (result)
1932 return result;
1933
1934 result = genl_register_mc_group(&thermal_event_genl_family,
1935 &thermal_event_mcgrp);
1936 if (result)
1937 genl_unregister_family(&thermal_event_genl_family);
1938 return result;
1939}
1940
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001941static void genetlink_exit(void)
1942{
1943 genl_unregister_family(&thermal_event_genl_family);
1944}
1945#else /* !CONFIG_NET */
1946static inline int genetlink_init(void) { return 0; }
1947static inline void genetlink_exit(void) {}
1948#endif /* !CONFIG_NET */
1949
Zhang Rui80a26a52013-03-26 16:38:29 +08001950static int __init thermal_register_governors(void)
1951{
1952 int result;
1953
1954 result = thermal_gov_step_wise_register();
1955 if (result)
1956 return result;
1957
1958 result = thermal_gov_fair_share_register();
1959 if (result)
1960 return result;
1961
1962 return thermal_gov_user_space_register();
1963}
1964
1965static void thermal_unregister_governors(void)
1966{
1967 thermal_gov_step_wise_unregister();
1968 thermal_gov_fair_share_unregister();
1969 thermal_gov_user_space_unregister();
1970}
1971
Zhang Rui203d3d42008-01-17 15:51:08 +08001972static int __init thermal_init(void)
1973{
Zhang Rui80a26a52013-03-26 16:38:29 +08001974 int result;
1975
1976 result = thermal_register_governors();
1977 if (result)
1978 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08001979
1980 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001981 if (result)
1982 goto unregister_governors;
1983
R.Durgadoss4cb18722010-10-27 03:33:29 +05301984 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08001985 if (result)
1986 goto unregister_class;
1987
1988 return 0;
1989
1990unregister_governors:
1991 thermal_unregister_governors();
1992unregister_class:
1993 class_unregister(&thermal_class);
1994error:
1995 idr_destroy(&thermal_tz_idr);
1996 idr_destroy(&thermal_cdev_idr);
1997 mutex_destroy(&thermal_idr_lock);
1998 mutex_destroy(&thermal_list_lock);
1999 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002000 return result;
2001}
2002
2003static void __exit thermal_exit(void)
2004{
Zhang Rui80a26a52013-03-26 16:38:29 +08002005 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08002006 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08002007 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08002008 idr_destroy(&thermal_tz_idr);
2009 idr_destroy(&thermal_cdev_idr);
2010 mutex_destroy(&thermal_idr_lock);
2011 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08002012 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002013}
2014
R.Durgadoss4cb18722010-10-27 03:33:29 +05302015fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08002016module_exit(thermal_exit);