blob: 1067fb0107b96e9ebb7e4e724440fa15813c6720 [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
159 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
160 if (tz->temperature > tz->last_temperature)
161 trend = THERMAL_TREND_RAISING;
162 else if (tz->temperature < tz->last_temperature)
163 trend = THERMAL_TREND_DROPPING;
164 else
165 trend = THERMAL_TREND_STABLE;
166 }
167
168 return trend;
169}
170EXPORT_SYMBOL(get_tz_trend);
171
172struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
173 struct thermal_cooling_device *cdev, int trip)
174{
175 struct thermal_instance *pos = NULL;
176 struct thermal_instance *target_instance = NULL;
177
178 mutex_lock(&tz->lock);
179 mutex_lock(&cdev->lock);
180
181 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
182 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
183 target_instance = pos;
184 break;
185 }
186 }
187
188 mutex_unlock(&cdev->lock);
189 mutex_unlock(&tz->lock);
190
191 return target_instance;
192}
193EXPORT_SYMBOL(get_thermal_instance);
194
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530195static void print_bind_err_msg(struct thermal_zone_device *tz,
196 struct thermal_cooling_device *cdev, int ret)
197{
198 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
199 tz->type, cdev->type, ret);
200}
201
202static void __bind(struct thermal_zone_device *tz, int mask,
203 struct thermal_cooling_device *cdev)
204{
205 int i, ret;
206
207 for (i = 0; i < tz->trips; i++) {
208 if (mask & (1 << i)) {
209 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
210 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
211 if (ret)
212 print_bind_err_msg(tz, cdev, ret);
213 }
214 }
215}
216
217static void __unbind(struct thermal_zone_device *tz, int mask,
218 struct thermal_cooling_device *cdev)
219{
220 int i;
221
222 for (i = 0; i < tz->trips; i++)
223 if (mask & (1 << i))
224 thermal_zone_unbind_cooling_device(tz, i, cdev);
225}
226
227static void bind_cdev(struct thermal_cooling_device *cdev)
228{
229 int i, ret;
230 const struct thermal_zone_params *tzp;
231 struct thermal_zone_device *pos = NULL;
232
233 mutex_lock(&thermal_list_lock);
234
235 list_for_each_entry(pos, &thermal_tz_list, node) {
236 if (!pos->tzp && !pos->ops->bind)
237 continue;
238
239 if (!pos->tzp && pos->ops->bind) {
240 ret = pos->ops->bind(pos, cdev);
241 if (ret)
242 print_bind_err_msg(pos, cdev, ret);
243 }
244
245 tzp = pos->tzp;
Hugh Dickins791700c2012-09-27 16:48:28 -0700246 if (!tzp || !tzp->tbp)
247 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530248
249 for (i = 0; i < tzp->num_tbps; i++) {
250 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
251 continue;
252 if (tzp->tbp[i].match(pos, cdev))
253 continue;
254 tzp->tbp[i].cdev = cdev;
255 __bind(pos, tzp->tbp[i].trip_mask, cdev);
256 }
257 }
258
259 mutex_unlock(&thermal_list_lock);
260}
261
262static void bind_tz(struct thermal_zone_device *tz)
263{
264 int i, ret;
265 struct thermal_cooling_device *pos = NULL;
266 const struct thermal_zone_params *tzp = tz->tzp;
267
268 if (!tzp && !tz->ops->bind)
269 return;
270
271 mutex_lock(&thermal_list_lock);
272
273 /* If there is no platform data, try to use ops->bind */
274 if (!tzp && tz->ops->bind) {
275 list_for_each_entry(pos, &thermal_cdev_list, node) {
276 ret = tz->ops->bind(tz, pos);
277 if (ret)
278 print_bind_err_msg(tz, pos, ret);
279 }
280 goto exit;
281 }
282
Hugh Dickins791700c2012-09-27 16:48:28 -0700283 if (!tzp || !tzp->tbp)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530284 goto exit;
285
286 list_for_each_entry(pos, &thermal_cdev_list, node) {
287 for (i = 0; i < tzp->num_tbps; i++) {
288 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
289 continue;
290 if (tzp->tbp[i].match(tz, pos))
291 continue;
292 tzp->tbp[i].cdev = pos;
293 __bind(tz, tzp->tbp[i].trip_mask, pos);
294 }
295 }
296exit:
297 mutex_unlock(&thermal_list_lock);
298}
299
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530300static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
301 int delay)
302{
303 if (delay > 1000)
304 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
305 round_jiffies(msecs_to_jiffies(delay)));
306 else if (delay)
307 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
308 msecs_to_jiffies(delay));
309 else
310 cancel_delayed_work(&tz->poll_queue);
311}
312
313static void monitor_thermal_zone(struct thermal_zone_device *tz)
314{
315 mutex_lock(&tz->lock);
316
317 if (tz->passive)
318 thermal_zone_device_set_polling(tz, tz->passive_delay);
319 else if (tz->polling_delay)
320 thermal_zone_device_set_polling(tz, tz->polling_delay);
321 else
322 thermal_zone_device_set_polling(tz, 0);
323
324 mutex_unlock(&tz->lock);
325}
326
327static void handle_non_critical_trips(struct thermal_zone_device *tz,
328 int trip, enum thermal_trip_type trip_type)
329{
Zhang Ruid567c682012-12-12 15:23:54 +0800330 if (tz->governor)
331 tz->governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530332}
333
334static void handle_critical_trips(struct thermal_zone_device *tz,
335 int trip, enum thermal_trip_type trip_type)
336{
337 long trip_temp;
338
339 tz->ops->get_trip_temp(tz, trip, &trip_temp);
340
341 /* If we have not crossed the trip_temp, we do not care. */
342 if (tz->temperature < trip_temp)
343 return;
344
345 if (tz->ops->notify)
346 tz->ops->notify(tz, trip, trip_type);
347
348 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000349 dev_emerg(&tz->device,
350 "critical temperature reached(%d C),shutting down\n",
351 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530352 orderly_poweroff(true);
353 }
354}
355
356static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
357{
358 enum thermal_trip_type type;
359
360 tz->ops->get_trip_type(tz, trip, &type);
361
362 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
363 handle_critical_trips(tz, trip, type);
364 else
365 handle_non_critical_trips(tz, trip, type);
366 /*
367 * Alright, we handled this trip successfully.
368 * So, start monitoring again.
369 */
370 monitor_thermal_zone(tz);
371}
372
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000373/**
374 * thermal_zone_get_temp() - returns its the temperature of thermal zone
375 * @tz: a valid pointer to a struct thermal_zone_device
376 * @temp: a valid pointer to where to store the resulting temperature.
377 *
378 * When a valid thermal zone reference is passed, it will fetch its
379 * temperature and fill @temp.
380 *
381 * Return: On success returns 0, an error code otherwise
382 */
383int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000384{
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000385 int ret = -EINVAL;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800386#ifdef CONFIG_THERMAL_EMULATION
387 int count;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000388 unsigned long crit_temp = -1UL;
389 enum thermal_trip_type type;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800390#endif
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000391
Eduardo Valentin9b19ec32013-04-25 14:13:33 +0000392 if (!tz || IS_ERR(tz))
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000393 goto exit;
394
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000395 mutex_lock(&tz->lock);
396
397 ret = tz->ops->get_temp(tz, temp);
398#ifdef CONFIG_THERMAL_EMULATION
399 if (!tz->emul_temperature)
400 goto skip_emul;
401
402 for (count = 0; count < tz->trips; count++) {
403 ret = tz->ops->get_trip_type(tz, count, &type);
404 if (!ret && type == THERMAL_TRIP_CRITICAL) {
405 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
406 break;
407 }
408 }
409
410 if (ret)
411 goto skip_emul;
412
413 if (*temp < crit_temp)
414 *temp = tz->emul_temperature;
415skip_emul:
416#endif
417 mutex_unlock(&tz->lock);
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000418exit:
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000419 return ret;
420}
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000421EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000422
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530423static void update_temperature(struct thermal_zone_device *tz)
424{
425 long temp;
426 int ret;
427
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000428 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530429 if (ret) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000430 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
431 tz->id);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000432 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530433 }
434
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000435 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530436 tz->last_temperature = tz->temperature;
437 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530438 mutex_unlock(&tz->lock);
439}
440
441void thermal_zone_device_update(struct thermal_zone_device *tz)
442{
443 int count;
444
445 update_temperature(tz);
446
447 for (count = 0; count < tz->trips; count++)
448 handle_thermal_trip(tz, count);
449}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000450EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530451
452static void thermal_zone_device_check(struct work_struct *work)
453{
454 struct thermal_zone_device *tz = container_of(work, struct
455 thermal_zone_device,
456 poll_queue.work);
457 thermal_zone_device_update(tz);
458}
459
Zhang Rui203d3d42008-01-17 15:51:08 +0800460/* sys I/F for thermal zone */
461
462#define to_thermal_zone(_dev) \
463 container_of(_dev, struct thermal_zone_device, device)
464
465static ssize_t
466type_show(struct device *dev, struct device_attribute *attr, char *buf)
467{
468 struct thermal_zone_device *tz = to_thermal_zone(dev);
469
470 return sprintf(buf, "%s\n", tz->type);
471}
472
473static ssize_t
474temp_show(struct device *dev, struct device_attribute *attr, char *buf)
475{
476 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000477 long temperature;
478 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800479
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000480 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000481
482 if (ret)
483 return ret;
484
485 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800486}
487
488static ssize_t
489mode_show(struct device *dev, struct device_attribute *attr, char *buf)
490{
491 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000492 enum thermal_device_mode mode;
493 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800494
495 if (!tz->ops->get_mode)
496 return -EPERM;
497
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000498 result = tz->ops->get_mode(tz, &mode);
499 if (result)
500 return result;
501
502 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
503 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800504}
505
506static ssize_t
507mode_store(struct device *dev, struct device_attribute *attr,
508 const char *buf, size_t count)
509{
510 struct thermal_zone_device *tz = to_thermal_zone(dev);
511 int result;
512
513 if (!tz->ops->set_mode)
514 return -EPERM;
515
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530516 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000517 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530518 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000519 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
520 else
521 result = -EINVAL;
522
Zhang Rui203d3d42008-01-17 15:51:08 +0800523 if (result)
524 return result;
525
526 return count;
527}
528
529static ssize_t
530trip_point_type_show(struct device *dev, struct device_attribute *attr,
531 char *buf)
532{
533 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000534 enum thermal_trip_type type;
535 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800536
537 if (!tz->ops->get_trip_type)
538 return -EPERM;
539
540 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
541 return -EINVAL;
542
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000543 result = tz->ops->get_trip_type(tz, trip, &type);
544 if (result)
545 return result;
546
547 switch (type) {
548 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300549 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000550 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300551 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000552 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300553 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000554 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300555 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000556 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300557 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000558 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800559}
560
561static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800562trip_point_temp_store(struct device *dev, struct device_attribute *attr,
563 const char *buf, size_t count)
564{
565 struct thermal_zone_device *tz = to_thermal_zone(dev);
566 int trip, ret;
567 unsigned long temperature;
568
569 if (!tz->ops->set_trip_temp)
570 return -EPERM;
571
572 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
573 return -EINVAL;
574
575 if (kstrtoul(buf, 10, &temperature))
576 return -EINVAL;
577
578 ret = tz->ops->set_trip_temp(tz, trip, temperature);
579
580 return ret ? ret : count;
581}
582
583static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800584trip_point_temp_show(struct device *dev, struct device_attribute *attr,
585 char *buf)
586{
587 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000588 int trip, ret;
589 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800590
591 if (!tz->ops->get_trip_temp)
592 return -EPERM;
593
594 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
595 return -EINVAL;
596
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000597 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
598
599 if (ret)
600 return ret;
601
602 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800603}
604
Matthew Garrett03a971a2008-12-03 18:00:38 +0000605static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800606trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
607 const char *buf, size_t count)
608{
609 struct thermal_zone_device *tz = to_thermal_zone(dev);
610 int trip, ret;
611 unsigned long temperature;
612
613 if (!tz->ops->set_trip_hyst)
614 return -EPERM;
615
616 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
617 return -EINVAL;
618
619 if (kstrtoul(buf, 10, &temperature))
620 return -EINVAL;
621
622 /*
623 * We are not doing any check on the 'temperature' value
624 * here. The driver implementing 'set_trip_hyst' has to
625 * take care of this.
626 */
627 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
628
629 return ret ? ret : count;
630}
631
632static ssize_t
633trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
634 char *buf)
635{
636 struct thermal_zone_device *tz = to_thermal_zone(dev);
637 int trip, ret;
638 unsigned long temperature;
639
640 if (!tz->ops->get_trip_hyst)
641 return -EPERM;
642
643 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
644 return -EINVAL;
645
646 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
647
648 return ret ? ret : sprintf(buf, "%ld\n", temperature);
649}
650
651static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000652passive_store(struct device *dev, struct device_attribute *attr,
653 const char *buf, size_t count)
654{
655 struct thermal_zone_device *tz = to_thermal_zone(dev);
656 struct thermal_cooling_device *cdev = NULL;
657 int state;
658
659 if (!sscanf(buf, "%d\n", &state))
660 return -EINVAL;
661
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100662 /* sanity check: values below 1000 millicelcius don't make sense
663 * and can cause the system to go into a thermal heart attack
664 */
665 if (state && state < 1000)
666 return -EINVAL;
667
Matthew Garrett03a971a2008-12-03 18:00:38 +0000668 if (state && !tz->forced_passive) {
669 mutex_lock(&thermal_list_lock);
670 list_for_each_entry(cdev, &thermal_cdev_list, node) {
671 if (!strncmp("Processor", cdev->type,
672 sizeof("Processor")))
673 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800674 THERMAL_TRIPS_NONE, cdev,
675 THERMAL_NO_LIMIT,
676 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000677 }
678 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100679 if (!tz->passive_delay)
680 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000681 } else if (!state && tz->forced_passive) {
682 mutex_lock(&thermal_list_lock);
683 list_for_each_entry(cdev, &thermal_cdev_list, node) {
684 if (!strncmp("Processor", cdev->type,
685 sizeof("Processor")))
686 thermal_zone_unbind_cooling_device(tz,
687 THERMAL_TRIPS_NONE,
688 cdev);
689 }
690 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100691 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000692 }
693
Matthew Garrett03a971a2008-12-03 18:00:38 +0000694 tz->forced_passive = state;
695
696 thermal_zone_device_update(tz);
697
698 return count;
699}
700
701static ssize_t
702passive_show(struct device *dev, struct device_attribute *attr,
703 char *buf)
704{
705 struct thermal_zone_device *tz = to_thermal_zone(dev);
706
707 return sprintf(buf, "%d\n", tz->forced_passive);
708}
709
Durgadoss R5a2c0902012-09-18 11:04:58 +0530710static ssize_t
711policy_store(struct device *dev, struct device_attribute *attr,
712 const char *buf, size_t count)
713{
714 int ret = -EINVAL;
715 struct thermal_zone_device *tz = to_thermal_zone(dev);
716 struct thermal_governor *gov;
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000717 char name[THERMAL_NAME_LENGTH];
718
719 snprintf(name, sizeof(name), "%s", buf);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530720
721 mutex_lock(&thermal_governor_lock);
722
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000723 gov = __find_governor(strim(name));
Durgadoss R5a2c0902012-09-18 11:04:58 +0530724 if (!gov)
725 goto exit;
726
727 tz->governor = gov;
728 ret = count;
729
730exit:
731 mutex_unlock(&thermal_governor_lock);
732 return ret;
733}
734
735static ssize_t
736policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
737{
738 struct thermal_zone_device *tz = to_thermal_zone(dev);
739
740 return sprintf(buf, "%s\n", tz->governor->name);
741}
742
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000743#ifdef CONFIG_THERMAL_EMULATION
744static ssize_t
745emul_temp_store(struct device *dev, struct device_attribute *attr,
746 const char *buf, size_t count)
747{
748 struct thermal_zone_device *tz = to_thermal_zone(dev);
749 int ret = 0;
750 unsigned long temperature;
751
752 if (kstrtoul(buf, 10, &temperature))
753 return -EINVAL;
754
755 if (!tz->ops->set_emul_temp) {
756 mutex_lock(&tz->lock);
757 tz->emul_temperature = temperature;
758 mutex_unlock(&tz->lock);
759 } else {
760 ret = tz->ops->set_emul_temp(tz, temperature);
761 }
762
763 return ret ? ret : count;
764}
765static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
766#endif/*CONFIG_THERMAL_EMULATION*/
767
Zhang Rui203d3d42008-01-17 15:51:08 +0800768static DEVICE_ATTR(type, 0444, type_show, NULL);
769static DEVICE_ATTR(temp, 0444, temp_show, NULL);
770static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700771static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530772static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800773
Zhang Rui203d3d42008-01-17 15:51:08 +0800774/* sys I/F for cooling device */
775#define to_cooling_device(_dev) \
776 container_of(_dev, struct thermal_cooling_device, device)
777
778static ssize_t
779thermal_cooling_device_type_show(struct device *dev,
780 struct device_attribute *attr, char *buf)
781{
782 struct thermal_cooling_device *cdev = to_cooling_device(dev);
783
784 return sprintf(buf, "%s\n", cdev->type);
785}
786
787static ssize_t
788thermal_cooling_device_max_state_show(struct device *dev,
789 struct device_attribute *attr, char *buf)
790{
791 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000792 unsigned long state;
793 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800794
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000795 ret = cdev->ops->get_max_state(cdev, &state);
796 if (ret)
797 return ret;
798 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800799}
800
801static ssize_t
802thermal_cooling_device_cur_state_show(struct device *dev,
803 struct device_attribute *attr, char *buf)
804{
805 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000806 unsigned long state;
807 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800808
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000809 ret = cdev->ops->get_cur_state(cdev, &state);
810 if (ret)
811 return ret;
812 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800813}
814
815static ssize_t
816thermal_cooling_device_cur_state_store(struct device *dev,
817 struct device_attribute *attr,
818 const char *buf, size_t count)
819{
820 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000821 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800822 int result;
823
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000824 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800825 return -EINVAL;
826
Roel Kluinedb94912009-12-15 22:46:50 +0100827 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800828 return -EINVAL;
829
830 result = cdev->ops->set_cur_state(cdev, state);
831 if (result)
832 return result;
833 return count;
834}
835
836static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500837__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800838static DEVICE_ATTR(max_state, 0444,
839 thermal_cooling_device_max_state_show, NULL);
840static DEVICE_ATTR(cur_state, 0644,
841 thermal_cooling_device_cur_state_show,
842 thermal_cooling_device_cur_state_store);
843
844static ssize_t
845thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500846 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800847{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800848 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800849
850 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800851 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800852
853 if (instance->trip == THERMAL_TRIPS_NONE)
854 return sprintf(buf, "-1\n");
855 else
856 return sprintf(buf, "%d\n", instance->trip);
857}
858
859/* Device management */
860
Rene Herman16d75232008-06-24 19:38:56 +0200861#if defined(CONFIG_THERMAL_HWMON)
862
Zhang Ruie68b16a2008-04-21 16:07:52 +0800863/* hwmon sys I/F */
864#include <linux/hwmon.h>
Jean Delvare31f5396a2011-07-28 13:48:42 -0700865
866/* thermal zone devices with the same type share one hwmon device */
867struct thermal_hwmon_device {
868 char type[THERMAL_NAME_LENGTH];
869 struct device *device;
870 int count;
871 struct list_head tz_list;
872 struct list_head node;
873};
874
875struct thermal_hwmon_attr {
876 struct device_attribute attr;
877 char name[16];
878};
879
880/* one temperature input for each thermal zone */
881struct thermal_hwmon_temp {
882 struct list_head hwmon_node;
883 struct thermal_zone_device *tz;
884 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
885 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
886};
887
Zhang Ruie68b16a2008-04-21 16:07:52 +0800888static LIST_HEAD(thermal_hwmon_list);
889
890static ssize_t
891name_show(struct device *dev, struct device_attribute *attr, char *buf)
892{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700893 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800894 return sprintf(buf, "%s\n", hwmon->type);
895}
896static DEVICE_ATTR(name, 0444, name_show, NULL);
897
898static ssize_t
899temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
900{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000901 long temperature;
902 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800903 struct thermal_hwmon_attr *hwmon_attr
904 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f5396a2011-07-28 13:48:42 -0700905 struct thermal_hwmon_temp *temp
906 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800907 temp_input);
Jean Delvare31f5396a2011-07-28 13:48:42 -0700908 struct thermal_zone_device *tz = temp->tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800909
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000910 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000911
912 if (ret)
913 return ret;
914
915 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800916}
917
918static ssize_t
919temp_crit_show(struct device *dev, struct device_attribute *attr,
920 char *buf)
921{
922 struct thermal_hwmon_attr *hwmon_attr
923 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f5396a2011-07-28 13:48:42 -0700924 struct thermal_hwmon_temp *temp
925 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800926 temp_crit);
Jean Delvare31f5396a2011-07-28 13:48:42 -0700927 struct thermal_zone_device *tz = temp->tz;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000928 long temperature;
929 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800930
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000931 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
932 if (ret)
933 return ret;
934
935 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800936}
937
938
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700939static struct thermal_hwmon_device *
940thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
941{
942 struct thermal_hwmon_device *hwmon;
943
944 mutex_lock(&thermal_list_lock);
945 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
946 if (!strcmp(hwmon->type, tz->type)) {
947 mutex_unlock(&thermal_list_lock);
948 return hwmon;
949 }
950 mutex_unlock(&thermal_list_lock);
951
952 return NULL;
953}
954
Jean Delvare31f5396a2011-07-28 13:48:42 -0700955/* Find the temperature input matching a given thermal zone */
956static struct thermal_hwmon_temp *
957thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
958 const struct thermal_zone_device *tz)
959{
960 struct thermal_hwmon_temp *temp;
961
962 mutex_lock(&thermal_list_lock);
963 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
964 if (temp->tz == tz) {
965 mutex_unlock(&thermal_list_lock);
966 return temp;
967 }
968 mutex_unlock(&thermal_list_lock);
969
970 return NULL;
971}
972
Zhang Ruie68b16a2008-04-21 16:07:52 +0800973static int
974thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
975{
976 struct thermal_hwmon_device *hwmon;
Jean Delvare31f5396a2011-07-28 13:48:42 -0700977 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800978 int new_hwmon_device = 1;
979 int result;
980
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700981 hwmon = thermal_hwmon_lookup_by_type(tz);
982 if (hwmon) {
983 new_hwmon_device = 0;
984 goto register_sys_interface;
985 }
Zhang Ruie68b16a2008-04-21 16:07:52 +0800986
987 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
988 if (!hwmon)
989 return -ENOMEM;
990
991 INIT_LIST_HEAD(&hwmon->tz_list);
992 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
993 hwmon->device = hwmon_device_register(NULL);
994 if (IS_ERR(hwmon->device)) {
995 result = PTR_ERR(hwmon->device);
996 goto free_mem;
997 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700998 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800999 result = device_create_file(hwmon->device, &dev_attr_name);
1000 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +05301001 goto free_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001002
1003 register_sys_interface:
Jean Delvare31f5396a2011-07-28 13:48:42 -07001004 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
1005 if (!temp) {
1006 result = -ENOMEM;
1007 goto unregister_name;
1008 }
1009
1010 temp->tz = tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001011 hwmon->count++;
1012
Guenter Roeck79a49162012-07-21 10:53:48 +10001013 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +08001014 "temp%d_input", hwmon->count);
Jean Delvare31f5396a2011-07-28 13:48:42 -07001015 temp->temp_input.attr.attr.name = temp->temp_input.name;
1016 temp->temp_input.attr.attr.mode = 0444;
1017 temp->temp_input.attr.show = temp_input_show;
1018 sysfs_attr_init(&temp->temp_input.attr.attr);
1019 result = device_create_file(hwmon->device, &temp->temp_input.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001020 if (result)
Jean Delvare31f5396a2011-07-28 13:48:42 -07001021 goto free_temp_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001022
1023 if (tz->ops->get_crit_temp) {
1024 unsigned long temperature;
1025 if (!tz->ops->get_crit_temp(tz, &temperature)) {
Guenter Roeck79a49162012-07-21 10:53:48 +10001026 snprintf(temp->temp_crit.name,
1027 sizeof(temp->temp_crit.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +08001028 "temp%d_crit", hwmon->count);
Jean Delvare31f5396a2011-07-28 13:48:42 -07001029 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
1030 temp->temp_crit.attr.attr.mode = 0444;
1031 temp->temp_crit.attr.show = temp_crit_show;
1032 sysfs_attr_init(&temp->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001033 result = device_create_file(hwmon->device,
Jean Delvare31f5396a2011-07-28 13:48:42 -07001034 &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001035 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +05301036 goto unregister_input;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001037 }
1038 }
1039
1040 mutex_lock(&thermal_list_lock);
1041 if (new_hwmon_device)
1042 list_add_tail(&hwmon->node, &thermal_hwmon_list);
Jean Delvare31f5396a2011-07-28 13:48:42 -07001043 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001044 mutex_unlock(&thermal_list_lock);
1045
1046 return 0;
1047
Durgadoss Rb299eb52011-03-03 04:30:13 +05301048 unregister_input:
Jean Delvare31f5396a2011-07-28 13:48:42 -07001049 device_remove_file(hwmon->device, &temp->temp_input.attr);
1050 free_temp_mem:
1051 kfree(temp);
Durgadoss Rb299eb52011-03-03 04:30:13 +05301052 unregister_name:
Zhang Ruie68b16a2008-04-21 16:07:52 +08001053 if (new_hwmon_device) {
1054 device_remove_file(hwmon->device, &dev_attr_name);
1055 hwmon_device_unregister(hwmon->device);
1056 }
1057 free_mem:
1058 if (new_hwmon_device)
1059 kfree(hwmon);
1060
1061 return result;
1062}
1063
1064static void
1065thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1066{
Jean Delvare31f5396a2011-07-28 13:48:42 -07001067 struct thermal_hwmon_device *hwmon;
1068 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001069
Jean Delvare31f5396a2011-07-28 13:48:42 -07001070 hwmon = thermal_hwmon_lookup_by_type(tz);
1071 if (unlikely(!hwmon)) {
1072 /* Should never happen... */
1073 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
1074 return;
1075 }
1076
1077 temp = thermal_hwmon_lookup_temp(hwmon, tz);
1078 if (unlikely(!temp)) {
1079 /* Should never happen... */
1080 dev_dbg(&tz->device, "temperature input lookup failed!\n");
1081 return;
1082 }
1083
1084 device_remove_file(hwmon->device, &temp->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +05301085 if (tz->ops->get_crit_temp)
Jean Delvare31f5396a2011-07-28 13:48:42 -07001086 device_remove_file(hwmon->device, &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001087
1088 mutex_lock(&thermal_list_lock);
Jean Delvare31f5396a2011-07-28 13:48:42 -07001089 list_del(&temp->hwmon_node);
1090 kfree(temp);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001091 if (!list_empty(&hwmon->tz_list)) {
1092 mutex_unlock(&thermal_list_lock);
1093 return;
1094 }
1095 list_del(&hwmon->node);
1096 mutex_unlock(&thermal_list_lock);
1097
1098 device_remove_file(hwmon->device, &dev_attr_name);
1099 hwmon_device_unregister(hwmon->device);
1100 kfree(hwmon);
1101}
1102#else
1103static int
1104thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
1105{
1106 return 0;
1107}
1108
1109static void
1110thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1111{
1112}
1113#endif
1114
Zhang Rui203d3d42008-01-17 15:51:08 +08001115/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001116 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1117 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +08001118 * @trip: indicates which trip point the cooling devices is
1119 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001120 * @cdev: pointer to struct thermal_cooling_device
1121 * @upper: the Maximum cooling state for this trip point.
1122 * THERMAL_NO_LIMIT means no upper limit,
1123 * and the cooling device can be in max_state.
1124 * @lower: the Minimum cooling state can be used for this trip point.
1125 * THERMAL_NO_LIMIT means no lower limit,
1126 * and the cooling device can be in cooling state 0.
Len Brown543a9562008-02-07 16:55:08 -05001127 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001128 * This interface function bind a thermal cooling device to the certain trip
1129 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001130 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001131 *
1132 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001133 */
1134int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1135 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +08001136 struct thermal_cooling_device *cdev,
1137 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +08001138{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001139 struct thermal_instance *dev;
1140 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -05001141 struct thermal_zone_device *pos1;
1142 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +08001143 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +08001144 int result;
1145
Len Brown543a9562008-02-07 16:55:08 -05001146 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +08001147 return -EINVAL;
1148
Thomas Sujithc7516702008-02-15 00:58:50 -05001149 list_for_each_entry(pos1, &thermal_tz_list, node) {
1150 if (pos1 == tz)
1151 break;
1152 }
1153 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1154 if (pos2 == cdev)
1155 break;
1156 }
1157
1158 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +08001159 return -EINVAL;
1160
Zhang Rui9d998422012-06-26 16:35:57 +08001161 cdev->ops->get_max_state(cdev, &max_state);
1162
1163 /* lower default 0, upper default max_state */
1164 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1165 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1166
1167 if (lower > upper || upper > max_state)
1168 return -EINVAL;
1169
Zhang Rui203d3d42008-01-17 15:51:08 +08001170 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001171 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001172 if (!dev)
1173 return -ENOMEM;
1174 dev->tz = tz;
1175 dev->cdev = cdev;
1176 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +08001177 dev->upper = upper;
1178 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +08001179 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +08001180
Zhang Rui203d3d42008-01-17 15:51:08 +08001181 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1182 if (result)
1183 goto free_mem;
1184
1185 sprintf(dev->name, "cdev%d", dev->id);
1186 result =
1187 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1188 if (result)
1189 goto release_idr;
1190
1191 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -07001192 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001193 dev->attr.attr.name = dev->attr_name;
1194 dev->attr.attr.mode = 0444;
1195 dev->attr.show = thermal_cooling_device_trip_point_show;
1196 result = device_create_file(&tz->device, &dev->attr);
1197 if (result)
1198 goto remove_symbol_link;
1199
1200 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001201 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001202 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +08001203 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1204 result = -EEXIST;
1205 break;
1206 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001207 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001208 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001209 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1210 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001211 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001212 mutex_unlock(&tz->lock);
1213
1214 if (!result)
1215 return 0;
1216
1217 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b802012-03-21 12:55:02 -07001218remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001219 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b802012-03-21 12:55:02 -07001220release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001221 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b802012-03-21 12:55:02 -07001222free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001223 kfree(dev);
1224 return result;
1225}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001226EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001227
1228/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001229 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1230 * thermal zone.
1231 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +08001232 * @trip: indicates which trip point the cooling devices is
1233 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001234 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -05001235 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001236 * This interface function unbind a thermal cooling device from the certain
1237 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001238 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001239 *
1240 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001241 */
1242int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1243 int trip,
1244 struct thermal_cooling_device *cdev)
1245{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001246 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001247
1248 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001249 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001250 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001251 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001252 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001253 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001254 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001255 mutex_unlock(&tz->lock);
1256 goto unbind;
1257 }
1258 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001259 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001260 mutex_unlock(&tz->lock);
1261
1262 return -ENODEV;
1263
Joe Perchescaca8b802012-03-21 12:55:02 -07001264unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +08001265 device_remove_file(&tz->device, &pos->attr);
1266 sysfs_remove_link(&tz->device.kobj, pos->name);
1267 release_idr(&tz->idr, &tz->lock, pos->id);
1268 kfree(pos);
1269 return 0;
1270}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001271EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001272
1273static void thermal_release(struct device *dev)
1274{
1275 struct thermal_zone_device *tz;
1276 struct thermal_cooling_device *cdev;
1277
Joe Perchescaca8b802012-03-21 12:55:02 -07001278 if (!strncmp(dev_name(dev), "thermal_zone",
1279 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001280 tz = to_thermal_zone(dev);
1281 kfree(tz);
1282 } else {
1283 cdev = to_cooling_device(dev);
1284 kfree(cdev);
1285 }
1286}
1287
1288static struct class thermal_class = {
1289 .name = "thermal",
1290 .dev_release = thermal_release,
1291};
1292
1293/**
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001294 * thermal_cooling_device_register() - register a new thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001295 * @type: the thermal cooling device type.
1296 * @devdata: device private data.
1297 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001298 *
1299 * This interface function adds a new thermal cooling device (fan/processor/...)
1300 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1301 * to all the thermal zone devices registered at the same time.
1302 *
1303 * Return: a pointer to the created struct thermal_cooling_device or an
1304 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001305 */
Joe Perchescaca8b802012-03-21 12:55:02 -07001306struct thermal_cooling_device *
1307thermal_cooling_device_register(char *type, void *devdata,
1308 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001309{
1310 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001311 int result;
1312
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001313 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001314 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001315
1316 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001317 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001318 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001319
1320 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1321 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001322 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001323
1324 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1325 if (result) {
1326 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001327 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001328 }
1329
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001330 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +08001331 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001332 INIT_LIST_HEAD(&cdev->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001333 cdev->ops = ops;
Zhang Ruice119f82012-06-27 14:13:04 +08001334 cdev->updated = true;
Zhang Rui203d3d42008-01-17 15:51:08 +08001335 cdev->device.class = &thermal_class;
1336 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001337 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001338 result = device_register(&cdev->device);
1339 if (result) {
1340 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1341 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001342 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001343 }
1344
1345 /* sys I/F */
1346 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001347 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001348 if (result)
1349 goto unregister;
1350 }
1351
1352 result = device_create_file(&cdev->device, &dev_attr_max_state);
1353 if (result)
1354 goto unregister;
1355
1356 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1357 if (result)
1358 goto unregister;
1359
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301360 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001361 mutex_lock(&thermal_list_lock);
1362 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001363 mutex_unlock(&thermal_list_lock);
1364
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301365 /* Update binding information for 'this' new cdev */
1366 bind_cdev(cdev);
1367
1368 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001369
Joe Perchescaca8b802012-03-21 12:55:02 -07001370unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001371 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1372 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001373 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001374}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001375EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001376
1377/**
1378 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001379 * @cdev: the thermal cooling device to remove.
1380 *
1381 * thermal_cooling_device_unregister() must be called when the device is no
1382 * longer needed.
1383 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301384void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001385{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301386 int i;
1387 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001388 struct thermal_zone_device *tz;
1389 struct thermal_cooling_device *pos = NULL;
1390
1391 if (!cdev)
1392 return;
1393
1394 mutex_lock(&thermal_list_lock);
1395 list_for_each_entry(pos, &thermal_cdev_list, node)
1396 if (pos == cdev)
1397 break;
1398 if (pos != cdev) {
1399 /* thermal cooling device not found */
1400 mutex_unlock(&thermal_list_lock);
1401 return;
1402 }
1403 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301404
1405 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001406 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301407 if (tz->ops->unbind) {
1408 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001409 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301410 }
1411
1412 if (!tz->tzp || !tz->tzp->tbp)
1413 continue;
1414
1415 tzp = tz->tzp;
1416 for (i = 0; i < tzp->num_tbps; i++) {
1417 if (tzp->tbp[i].cdev == cdev) {
1418 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1419 tzp->tbp[i].cdev = NULL;
1420 }
1421 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001422 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301423
Zhang Rui203d3d42008-01-17 15:51:08 +08001424 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301425
Zhang Rui203d3d42008-01-17 15:51:08 +08001426 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001427 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001428 device_remove_file(&cdev->device, &dev_attr_max_state);
1429 device_remove_file(&cdev->device, &dev_attr_cur_state);
1430
1431 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1432 device_unregister(&cdev->device);
1433 return;
1434}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001435EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001436
Durgadoss Rdc765482012-09-18 11:05:00 +05301437void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001438{
1439 struct thermal_instance *instance;
1440 unsigned long target = 0;
1441
1442 /* cooling device is updated*/
1443 if (cdev->updated)
1444 return;
1445
Zhang Ruif4a821c2012-07-24 16:56:21 +08001446 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001447 /* Make sure cdev enters the deepest cooling state */
1448 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1449 if (instance->target == THERMAL_NO_TARGET)
1450 continue;
1451 if (instance->target > target)
1452 target = instance->target;
1453 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001454 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001455 cdev->ops->set_cur_state(cdev, target);
1456 cdev->updated = true;
1457}
Durgadoss Rdc765482012-09-18 11:05:00 +05301458EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001459
Matthew Garrettb1569e92008-12-03 17:55:32 +00001460/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001461 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301462 * @tz: thermal zone device
1463 * @trip: indicates which trip point has been crossed
1464 *
1465 * This function handles the trip events from sensor drivers. It starts
1466 * throttling the cooling devices according to the policy configured.
1467 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1468 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1469 * The throttling policy is based on the configured platform data; if no
1470 * platform data is provided, this uses the step_wise throttling policy.
1471 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001472void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301473{
1474 handle_thermal_trip(tz, trip);
1475}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001476EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301477
1478/**
Eduardo Valentin269c1742013-04-23 21:48:19 +00001479 * create_trip_attrs() - create attributes for trip points
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001480 * @tz: the thermal zone device
1481 * @mask: Writeable trip point bitmap.
Eduardo Valentin269c1742013-04-23 21:48:19 +00001482 *
1483 * helper function to instantiate sysfs entries for every trip
1484 * point and its properties of a struct thermal_zone_device.
1485 *
1486 * Return: 0 on success, the proper error value otherwise.
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001487 */
1488static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1489{
1490 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001491 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001492
Durgadoss R27365a62012-07-25 10:10:59 +08001493 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001494 if (!tz->trip_type_attrs)
1495 return -ENOMEM;
1496
Durgadoss R27365a62012-07-25 10:10:59 +08001497 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001498 if (!tz->trip_temp_attrs) {
1499 kfree(tz->trip_type_attrs);
1500 return -ENOMEM;
1501 }
1502
Durgadoss R27365a62012-07-25 10:10:59 +08001503 if (tz->ops->get_trip_hyst) {
1504 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1505 if (!tz->trip_hyst_attrs) {
1506 kfree(tz->trip_type_attrs);
1507 kfree(tz->trip_temp_attrs);
1508 return -ENOMEM;
1509 }
1510 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001511
Durgadoss R27365a62012-07-25 10:10:59 +08001512
1513 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001514 /* create trip type attribute */
1515 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1516 "trip_point_%d_type", indx);
1517
1518 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1519 tz->trip_type_attrs[indx].attr.attr.name =
1520 tz->trip_type_attrs[indx].name;
1521 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1522 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1523
1524 device_create_file(&tz->device,
1525 &tz->trip_type_attrs[indx].attr);
1526
1527 /* create trip temp attribute */
1528 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1529 "trip_point_%d_temp", indx);
1530
1531 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1532 tz->trip_temp_attrs[indx].attr.attr.name =
1533 tz->trip_temp_attrs[indx].name;
1534 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1535 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1536 if (mask & (1 << indx)) {
1537 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1538 tz->trip_temp_attrs[indx].attr.store =
1539 trip_point_temp_store;
1540 }
1541
1542 device_create_file(&tz->device,
1543 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001544
1545 /* create Optional trip hyst attribute */
1546 if (!tz->ops->get_trip_hyst)
1547 continue;
1548 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1549 "trip_point_%d_hyst", indx);
1550
1551 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1552 tz->trip_hyst_attrs[indx].attr.attr.name =
1553 tz->trip_hyst_attrs[indx].name;
1554 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1555 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1556 if (tz->ops->set_trip_hyst) {
1557 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1558 tz->trip_hyst_attrs[indx].attr.store =
1559 trip_point_hyst_store;
1560 }
1561
1562 device_create_file(&tz->device,
1563 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001564 }
1565 return 0;
1566}
1567
1568static void remove_trip_attrs(struct thermal_zone_device *tz)
1569{
1570 int indx;
1571
1572 for (indx = 0; indx < tz->trips; indx++) {
1573 device_remove_file(&tz->device,
1574 &tz->trip_type_attrs[indx].attr);
1575 device_remove_file(&tz->device,
1576 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001577 if (tz->ops->get_trip_hyst)
1578 device_remove_file(&tz->device,
1579 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001580 }
1581 kfree(tz->trip_type_attrs);
1582 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001583 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001584}
1585
1586/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001587 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001588 * @type: the thermal zone device type
1589 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001590 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001591 * @devdata: private device data
1592 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301593 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001594 * @passive_delay: number of milliseconds to wait between polls when
1595 * performing passive cooling
1596 * @polling_delay: number of milliseconds to wait between polls when checking
1597 * whether trip points have been crossed (0 for interrupt
1598 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001599 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001600 * This interface function adds a new thermal zone device (sensor) to
1601 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1602 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001603 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001604 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001605 *
1606 * Return: a pointer to the created struct thermal_zone_device or an
1607 * in case of error, an ERR_PTR. Caller must check return value with
1608 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001609 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001610struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001611 int trips, int mask, void *devdata,
Alan Cox5b275ce2010-11-11 15:27:29 +00001612 const struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301613 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001614 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001615{
1616 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001617 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001618 int result;
1619 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001620 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001621
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001622 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001623 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001624
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001625 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001626 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001627
1628 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001629 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001630
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001631 if (trips > 0 && !ops->get_trip_type)
1632 return ERR_PTR(-EINVAL);
1633
Zhang Rui203d3d42008-01-17 15:51:08 +08001634 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1635 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001636 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001637
Zhang Rui2d374132012-06-27 10:09:00 +08001638 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001639 idr_init(&tz->idr);
1640 mutex_init(&tz->lock);
1641 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1642 if (result) {
1643 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001644 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001645 }
1646
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001647 strlcpy(tz->type, type ? : "", sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001648 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301649 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001650 tz->device.class = &thermal_class;
1651 tz->devdata = devdata;
1652 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001653 tz->passive_delay = passive_delay;
1654 tz->polling_delay = polling_delay;
1655
Kay Sievers354655e2009-01-06 10:44:37 -08001656 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001657 result = device_register(&tz->device);
1658 if (result) {
1659 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1660 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001661 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001662 }
1663
1664 /* sys I/F */
1665 if (type) {
1666 result = device_create_file(&tz->device, &dev_attr_type);
1667 if (result)
1668 goto unregister;
1669 }
1670
1671 result = device_create_file(&tz->device, &dev_attr_temp);
1672 if (result)
1673 goto unregister;
1674
1675 if (ops->get_mode) {
1676 result = device_create_file(&tz->device, &dev_attr_mode);
1677 if (result)
1678 goto unregister;
1679 }
1680
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001681 result = create_trip_attrs(tz, mask);
1682 if (result)
1683 goto unregister;
1684
Zhang Rui203d3d42008-01-17 15:51:08 +08001685 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001686 tz->ops->get_trip_type(tz, count, &trip_type);
1687 if (trip_type == THERMAL_TRIP_PASSIVE)
1688 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001689 }
1690
Durgadoss R5a2c0902012-09-18 11:04:58 +05301691 if (!passive) {
1692 result = device_create_file(&tz->device, &dev_attr_passive);
1693 if (result)
1694 goto unregister;
1695 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001696
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001697#ifdef CONFIG_THERMAL_EMULATION
1698 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1699 if (result)
1700 goto unregister;
1701#endif
Durgadoss R5a2c0902012-09-18 11:04:58 +05301702 /* Create policy attribute */
1703 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001704 if (result)
1705 goto unregister;
1706
Durgadoss Ra4a15482012-09-18 11:04:57 +05301707 /* Update 'this' zone's governor information */
1708 mutex_lock(&thermal_governor_lock);
1709
1710 if (tz->tzp)
1711 tz->governor = __find_governor(tz->tzp->governor_name);
1712 else
1713 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1714
1715 mutex_unlock(&thermal_governor_lock);
1716
Zhang Ruie68b16a2008-04-21 16:07:52 +08001717 result = thermal_add_hwmon_sysfs(tz);
1718 if (result)
1719 goto unregister;
1720
Zhang Rui203d3d42008-01-17 15:51:08 +08001721 mutex_lock(&thermal_list_lock);
1722 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001723 mutex_unlock(&thermal_list_lock);
1724
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301725 /* Bind cooling devices for this zone */
1726 bind_tz(tz);
1727
Matthew Garrettb1569e92008-12-03 17:55:32 +00001728 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1729
1730 thermal_zone_device_update(tz);
1731
Zhang Rui203d3d42008-01-17 15:51:08 +08001732 if (!result)
1733 return tz;
1734
Joe Perchescaca8b802012-03-21 12:55:02 -07001735unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001736 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1737 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001738 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001739}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001740EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001741
1742/**
1743 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001744 * @tz: the thermal zone device to remove
1745 */
1746void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1747{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301748 int i;
1749 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001750 struct thermal_cooling_device *cdev;
1751 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001752
1753 if (!tz)
1754 return;
1755
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301756 tzp = tz->tzp;
1757
Zhang Rui203d3d42008-01-17 15:51:08 +08001758 mutex_lock(&thermal_list_lock);
1759 list_for_each_entry(pos, &thermal_tz_list, node)
1760 if (pos == tz)
1761 break;
1762 if (pos != tz) {
1763 /* thermal zone device not found */
1764 mutex_unlock(&thermal_list_lock);
1765 return;
1766 }
1767 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301768
1769 /* Unbind all cdevs associated with 'this' thermal zone */
1770 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1771 if (tz->ops->unbind) {
1772 tz->ops->unbind(tz, cdev);
1773 continue;
1774 }
1775
1776 if (!tzp || !tzp->tbp)
1777 break;
1778
1779 for (i = 0; i < tzp->num_tbps; i++) {
1780 if (tzp->tbp[i].cdev == cdev) {
1781 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1782 tzp->tbp[i].cdev = NULL;
1783 }
1784 }
1785 }
1786
Zhang Rui203d3d42008-01-17 15:51:08 +08001787 mutex_unlock(&thermal_list_lock);
1788
Matthew Garrettb1569e92008-12-03 17:55:32 +00001789 thermal_zone_device_set_polling(tz, 0);
1790
Zhang Rui203d3d42008-01-17 15:51:08 +08001791 if (tz->type[0])
1792 device_remove_file(&tz->device, &dev_attr_type);
1793 device_remove_file(&tz->device, &dev_attr_temp);
1794 if (tz->ops->get_mode)
1795 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301796 device_remove_file(&tz->device, &dev_attr_policy);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001797 remove_trip_attrs(tz);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301798 tz->governor = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001799
Zhang Ruie68b16a2008-04-21 16:07:52 +08001800 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001801 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1802 idr_destroy(&tz->idr);
1803 mutex_destroy(&tz->lock);
1804 device_unregister(&tz->device);
1805 return;
1806}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001807EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001808
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001809/**
1810 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1811 * @name: thermal zone name to fetch the temperature
1812 *
1813 * When only one zone is found with the passed name, returns a reference to it.
1814 *
1815 * Return: On success returns a reference to an unique thermal zone with
1816 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1817 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1818 */
1819struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1820{
1821 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1822 unsigned int found = 0;
1823
1824 if (!name)
1825 goto exit;
1826
1827 mutex_lock(&thermal_list_lock);
1828 list_for_each_entry(pos, &thermal_tz_list, node)
1829 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1830 found++;
1831 ref = pos;
1832 }
1833 mutex_unlock(&thermal_list_lock);
1834
1835 /* nothing has been found, thus an error code for it */
1836 if (found == 0)
1837 ref = ERR_PTR(-ENODEV);
1838 else if (found > 1)
1839 /* Success only when an unique zone is found */
1840 ref = ERR_PTR(-EEXIST);
1841
1842exit:
1843 return ref;
1844}
1845EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1846
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001847#ifdef CONFIG_NET
1848static struct genl_family thermal_event_genl_family = {
1849 .id = GENL_ID_GENERATE,
1850 .name = THERMAL_GENL_FAMILY_NAME,
1851 .version = THERMAL_GENL_VERSION,
1852 .maxattr = THERMAL_GENL_ATTR_MAX,
1853};
1854
1855static struct genl_multicast_group thermal_event_mcgrp = {
1856 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1857};
1858
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001859int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1860 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301861{
1862 struct sk_buff *skb;
1863 struct nlattr *attr;
1864 struct thermal_genl_event *thermal_event;
1865 void *msg_header;
1866 int size;
1867 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001868 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301869
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001870 if (!tz)
1871 return -EINVAL;
1872
R.Durgadoss4cb18722010-10-27 03:33:29 +05301873 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001874 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1875 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301876
1877 skb = genlmsg_new(size, GFP_ATOMIC);
1878 if (!skb)
1879 return -ENOMEM;
1880
1881 /* add the genetlink message header */
1882 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1883 &thermal_event_genl_family, 0,
1884 THERMAL_GENL_CMD_EVENT);
1885 if (!msg_header) {
1886 nlmsg_free(skb);
1887 return -ENOMEM;
1888 }
1889
1890 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001891 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1892 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301893
1894 if (!attr) {
1895 nlmsg_free(skb);
1896 return -EINVAL;
1897 }
1898
1899 thermal_event = nla_data(attr);
1900 if (!thermal_event) {
1901 nlmsg_free(skb);
1902 return -EINVAL;
1903 }
1904
1905 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1906
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001907 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301908 thermal_event->event = event;
1909
1910 /* send multicast genetlink message */
1911 result = genlmsg_end(skb, msg_header);
1912 if (result < 0) {
1913 nlmsg_free(skb);
1914 return result;
1915 }
1916
1917 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1918 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001919 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301920
1921 return result;
1922}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001923EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301924
1925static int genetlink_init(void)
1926{
1927 int result;
1928
1929 result = genl_register_family(&thermal_event_genl_family);
1930 if (result)
1931 return result;
1932
1933 result = genl_register_mc_group(&thermal_event_genl_family,
1934 &thermal_event_mcgrp);
1935 if (result)
1936 genl_unregister_family(&thermal_event_genl_family);
1937 return result;
1938}
1939
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001940static void genetlink_exit(void)
1941{
1942 genl_unregister_family(&thermal_event_genl_family);
1943}
1944#else /* !CONFIG_NET */
1945static inline int genetlink_init(void) { return 0; }
1946static inline void genetlink_exit(void) {}
1947#endif /* !CONFIG_NET */
1948
Zhang Rui80a26a52013-03-26 16:38:29 +08001949static int __init thermal_register_governors(void)
1950{
1951 int result;
1952
1953 result = thermal_gov_step_wise_register();
1954 if (result)
1955 return result;
1956
1957 result = thermal_gov_fair_share_register();
1958 if (result)
1959 return result;
1960
1961 return thermal_gov_user_space_register();
1962}
1963
1964static void thermal_unregister_governors(void)
1965{
1966 thermal_gov_step_wise_unregister();
1967 thermal_gov_fair_share_unregister();
1968 thermal_gov_user_space_unregister();
1969}
1970
Zhang Rui203d3d42008-01-17 15:51:08 +08001971static int __init thermal_init(void)
1972{
Zhang Rui80a26a52013-03-26 16:38:29 +08001973 int result;
1974
1975 result = thermal_register_governors();
1976 if (result)
1977 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08001978
1979 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001980 if (result)
1981 goto unregister_governors;
1982
R.Durgadoss4cb18722010-10-27 03:33:29 +05301983 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08001984 if (result)
1985 goto unregister_class;
1986
1987 return 0;
1988
1989unregister_governors:
1990 thermal_unregister_governors();
1991unregister_class:
1992 class_unregister(&thermal_class);
1993error:
1994 idr_destroy(&thermal_tz_idr);
1995 idr_destroy(&thermal_cdev_idr);
1996 mutex_destroy(&thermal_idr_lock);
1997 mutex_destroy(&thermal_list_lock);
1998 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001999 return result;
2000}
2001
2002static void __exit thermal_exit(void)
2003{
Zhang Rui80a26a52013-03-26 16:38:29 +08002004 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08002005 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08002006 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08002007 idr_destroy(&thermal_tz_idr);
2008 idr_destroy(&thermal_cdev_idr);
2009 mutex_destroy(&thermal_idr_lock);
2010 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08002011 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002012}
2013
R.Durgadoss4cb18722010-10-27 03:33:29 +05302014fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08002015module_exit(thermal_exit);