blob: 6472e7e9f969025f565e17d62ad83eaef80eedaf [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>
R.Durgadoss4cb18722010-10-27 03:33:29 +053036#include <net/netlink.h>
37#include <net/genetlink.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080038
Durgadoss R71350db2012-09-18 11:04:53 +053039#include "thermal_core.h"
40
Zhang Rui63c4ec92008-04-21 16:07:13 +080041MODULE_AUTHOR("Zhang Rui");
Zhang Rui203d3d42008-01-17 15:51:08 +080042MODULE_DESCRIPTION("Generic thermal management sysfs support");
43MODULE_LICENSE("GPL");
44
Zhang Rui203d3d42008-01-17 15:51:08 +080045static DEFINE_IDR(thermal_tz_idr);
46static DEFINE_IDR(thermal_cdev_idr);
47static DEFINE_MUTEX(thermal_idr_lock);
48
49static LIST_HEAD(thermal_tz_list);
50static LIST_HEAD(thermal_cdev_list);
Durgadoss Ra4a15482012-09-18 11:04:57 +053051static LIST_HEAD(thermal_governor_list);
52
Zhang Rui203d3d42008-01-17 15:51:08 +080053static DEFINE_MUTEX(thermal_list_lock);
Durgadoss Ra4a15482012-09-18 11:04:57 +053054static DEFINE_MUTEX(thermal_governor_lock);
55
56static struct thermal_governor *__find_governor(const char *name)
57{
58 struct thermal_governor *pos;
59
60 list_for_each_entry(pos, &thermal_governor_list, governor_list)
61 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
62 return pos;
63
64 return NULL;
65}
66
67int thermal_register_governor(struct thermal_governor *governor)
68{
69 int err;
70 const char *name;
71 struct thermal_zone_device *pos;
72
73 if (!governor)
74 return -EINVAL;
75
76 mutex_lock(&thermal_governor_lock);
77
78 err = -EBUSY;
79 if (__find_governor(governor->name) == NULL) {
80 err = 0;
81 list_add(&governor->governor_list, &thermal_governor_list);
82 }
83
84 mutex_lock(&thermal_list_lock);
85
86 list_for_each_entry(pos, &thermal_tz_list, node) {
87 if (pos->governor)
88 continue;
89 if (pos->tzp)
90 name = pos->tzp->governor_name;
91 else
92 name = DEFAULT_THERMAL_GOVERNOR;
93 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
94 pos->governor = governor;
95 }
96
97 mutex_unlock(&thermal_list_lock);
98 mutex_unlock(&thermal_governor_lock);
99
100 return err;
101}
102EXPORT_SYMBOL_GPL(thermal_register_governor);
103
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}
130EXPORT_SYMBOL_GPL(thermal_unregister_governor);
Zhang Rui203d3d42008-01-17 15:51:08 +0800131
132static int get_idr(struct idr *idr, struct mutex *lock, int *id)
133{
134 int err;
135
Joe Perchescaca8b82012-03-21 12:55:02 -0700136again:
Zhang Rui203d3d42008-01-17 15:51:08 +0800137 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
138 return -ENOMEM;
139
140 if (lock)
141 mutex_lock(lock);
142 err = idr_get_new(idr, NULL, id);
143 if (lock)
144 mutex_unlock(lock);
145 if (unlikely(err == -EAGAIN))
146 goto again;
147 else if (unlikely(err))
148 return err;
149
Fengguang Wu125c4c72012-10-04 17:13:15 -0700150 *id = *id & MAX_IDR_MASK;
Zhang Rui203d3d42008-01-17 15:51:08 +0800151 return 0;
152}
153
154static void release_idr(struct idr *idr, struct mutex *lock, int id)
155{
156 if (lock)
157 mutex_lock(lock);
158 idr_remove(idr, id);
159 if (lock)
160 mutex_unlock(lock);
161}
162
Durgadoss R9b4298a2012-09-18 11:04:54 +0530163int get_tz_trend(struct thermal_zone_device *tz, int trip)
164{
165 enum thermal_trend trend;
166
167 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
168 if (tz->temperature > tz->last_temperature)
169 trend = THERMAL_TREND_RAISING;
170 else if (tz->temperature < tz->last_temperature)
171 trend = THERMAL_TREND_DROPPING;
172 else
173 trend = THERMAL_TREND_STABLE;
174 }
175
176 return trend;
177}
178EXPORT_SYMBOL(get_tz_trend);
179
180struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
181 struct thermal_cooling_device *cdev, int trip)
182{
183 struct thermal_instance *pos = NULL;
184 struct thermal_instance *target_instance = NULL;
185
186 mutex_lock(&tz->lock);
187 mutex_lock(&cdev->lock);
188
189 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
190 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
191 target_instance = pos;
192 break;
193 }
194 }
195
196 mutex_unlock(&cdev->lock);
197 mutex_unlock(&tz->lock);
198
199 return target_instance;
200}
201EXPORT_SYMBOL(get_thermal_instance);
202
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530203static void print_bind_err_msg(struct thermal_zone_device *tz,
204 struct thermal_cooling_device *cdev, int ret)
205{
206 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
207 tz->type, cdev->type, ret);
208}
209
210static void __bind(struct thermal_zone_device *tz, int mask,
211 struct thermal_cooling_device *cdev)
212{
213 int i, ret;
214
215 for (i = 0; i < tz->trips; i++) {
216 if (mask & (1 << i)) {
217 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
218 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
219 if (ret)
220 print_bind_err_msg(tz, cdev, ret);
221 }
222 }
223}
224
225static void __unbind(struct thermal_zone_device *tz, int mask,
226 struct thermal_cooling_device *cdev)
227{
228 int i;
229
230 for (i = 0; i < tz->trips; i++)
231 if (mask & (1 << i))
232 thermal_zone_unbind_cooling_device(tz, i, cdev);
233}
234
235static void bind_cdev(struct thermal_cooling_device *cdev)
236{
237 int i, ret;
238 const struct thermal_zone_params *tzp;
239 struct thermal_zone_device *pos = NULL;
240
241 mutex_lock(&thermal_list_lock);
242
243 list_for_each_entry(pos, &thermal_tz_list, node) {
244 if (!pos->tzp && !pos->ops->bind)
245 continue;
246
247 if (!pos->tzp && pos->ops->bind) {
248 ret = pos->ops->bind(pos, cdev);
249 if (ret)
250 print_bind_err_msg(pos, cdev, ret);
251 }
252
253 tzp = pos->tzp;
Hugh Dickins791700c2012-09-27 16:48:28 -0700254 if (!tzp || !tzp->tbp)
255 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530256
257 for (i = 0; i < tzp->num_tbps; i++) {
258 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
259 continue;
260 if (tzp->tbp[i].match(pos, cdev))
261 continue;
262 tzp->tbp[i].cdev = cdev;
263 __bind(pos, tzp->tbp[i].trip_mask, cdev);
264 }
265 }
266
267 mutex_unlock(&thermal_list_lock);
268}
269
270static void bind_tz(struct thermal_zone_device *tz)
271{
272 int i, ret;
273 struct thermal_cooling_device *pos = NULL;
274 const struct thermal_zone_params *tzp = tz->tzp;
275
276 if (!tzp && !tz->ops->bind)
277 return;
278
279 mutex_lock(&thermal_list_lock);
280
281 /* If there is no platform data, try to use ops->bind */
282 if (!tzp && tz->ops->bind) {
283 list_for_each_entry(pos, &thermal_cdev_list, node) {
284 ret = tz->ops->bind(tz, pos);
285 if (ret)
286 print_bind_err_msg(tz, pos, ret);
287 }
288 goto exit;
289 }
290
Hugh Dickins791700c2012-09-27 16:48:28 -0700291 if (!tzp || !tzp->tbp)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530292 goto exit;
293
294 list_for_each_entry(pos, &thermal_cdev_list, node) {
295 for (i = 0; i < tzp->num_tbps; i++) {
296 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
297 continue;
298 if (tzp->tbp[i].match(tz, pos))
299 continue;
300 tzp->tbp[i].cdev = pos;
301 __bind(tz, tzp->tbp[i].trip_mask, pos);
302 }
303 }
304exit:
305 mutex_unlock(&thermal_list_lock);
306}
307
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530308static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
309 int delay)
310{
311 if (delay > 1000)
312 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
313 round_jiffies(msecs_to_jiffies(delay)));
314 else if (delay)
315 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
316 msecs_to_jiffies(delay));
317 else
318 cancel_delayed_work(&tz->poll_queue);
319}
320
321static void monitor_thermal_zone(struct thermal_zone_device *tz)
322{
323 mutex_lock(&tz->lock);
324
325 if (tz->passive)
326 thermal_zone_device_set_polling(tz, tz->passive_delay);
327 else if (tz->polling_delay)
328 thermal_zone_device_set_polling(tz, tz->polling_delay);
329 else
330 thermal_zone_device_set_polling(tz, 0);
331
332 mutex_unlock(&tz->lock);
333}
334
335static void handle_non_critical_trips(struct thermal_zone_device *tz,
336 int trip, enum thermal_trip_type trip_type)
337{
Zhang Ruid567c682012-12-12 15:23:54 +0800338 if (tz->governor)
339 tz->governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530340}
341
342static void handle_critical_trips(struct thermal_zone_device *tz,
343 int trip, enum thermal_trip_type trip_type)
344{
345 long trip_temp;
346
347 tz->ops->get_trip_temp(tz, trip, &trip_temp);
348
349 /* If we have not crossed the trip_temp, we do not care. */
350 if (tz->temperature < trip_temp)
351 return;
352
353 if (tz->ops->notify)
354 tz->ops->notify(tz, trip, trip_type);
355
356 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000357 dev_emerg(&tz->device,
358 "critical temperature reached(%d C),shutting down\n",
359 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530360 orderly_poweroff(true);
361 }
362}
363
364static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
365{
366 enum thermal_trip_type type;
367
368 tz->ops->get_trip_type(tz, trip, &type);
369
370 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
371 handle_critical_trips(tz, trip, type);
372 else
373 handle_non_critical_trips(tz, trip, type);
374 /*
375 * Alright, we handled this trip successfully.
376 * So, start monitoring again.
377 */
378 monitor_thermal_zone(tz);
379}
380
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000381static int thermal_zone_get_temp(struct thermal_zone_device *tz,
382 unsigned long *temp)
383{
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800384 int ret = 0;
385#ifdef CONFIG_THERMAL_EMULATION
386 int count;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000387 unsigned long crit_temp = -1UL;
388 enum thermal_trip_type type;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800389#endif
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000390
391 mutex_lock(&tz->lock);
392
393 ret = tz->ops->get_temp(tz, temp);
394#ifdef CONFIG_THERMAL_EMULATION
395 if (!tz->emul_temperature)
396 goto skip_emul;
397
398 for (count = 0; count < tz->trips; count++) {
399 ret = tz->ops->get_trip_type(tz, count, &type);
400 if (!ret && type == THERMAL_TRIP_CRITICAL) {
401 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
402 break;
403 }
404 }
405
406 if (ret)
407 goto skip_emul;
408
409 if (*temp < crit_temp)
410 *temp = tz->emul_temperature;
411skip_emul:
412#endif
413 mutex_unlock(&tz->lock);
414 return ret;
415}
416
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530417static void update_temperature(struct thermal_zone_device *tz)
418{
419 long temp;
420 int ret;
421
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000422 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530423 if (ret) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000424 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
425 tz->id);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000426 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530427 }
428
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000429 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530430 tz->last_temperature = tz->temperature;
431 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530432 mutex_unlock(&tz->lock);
433}
434
435void thermal_zone_device_update(struct thermal_zone_device *tz)
436{
437 int count;
438
439 update_temperature(tz);
440
441 for (count = 0; count < tz->trips; count++)
442 handle_thermal_trip(tz, count);
443}
444EXPORT_SYMBOL(thermal_zone_device_update);
445
446static void thermal_zone_device_check(struct work_struct *work)
447{
448 struct thermal_zone_device *tz = container_of(work, struct
449 thermal_zone_device,
450 poll_queue.work);
451 thermal_zone_device_update(tz);
452}
453
Zhang Rui203d3d42008-01-17 15:51:08 +0800454/* sys I/F for thermal zone */
455
456#define to_thermal_zone(_dev) \
457 container_of(_dev, struct thermal_zone_device, device)
458
459static ssize_t
460type_show(struct device *dev, struct device_attribute *attr, char *buf)
461{
462 struct thermal_zone_device *tz = to_thermal_zone(dev);
463
464 return sprintf(buf, "%s\n", tz->type);
465}
466
467static ssize_t
468temp_show(struct device *dev, struct device_attribute *attr, char *buf)
469{
470 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000471 long temperature;
472 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800473
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000474 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000475
476 if (ret)
477 return ret;
478
479 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800480}
481
482static ssize_t
483mode_show(struct device *dev, struct device_attribute *attr, char *buf)
484{
485 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000486 enum thermal_device_mode mode;
487 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800488
489 if (!tz->ops->get_mode)
490 return -EPERM;
491
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000492 result = tz->ops->get_mode(tz, &mode);
493 if (result)
494 return result;
495
496 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
497 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800498}
499
500static ssize_t
501mode_store(struct device *dev, struct device_attribute *attr,
502 const char *buf, size_t count)
503{
504 struct thermal_zone_device *tz = to_thermal_zone(dev);
505 int result;
506
507 if (!tz->ops->set_mode)
508 return -EPERM;
509
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530510 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000511 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530512 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000513 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
514 else
515 result = -EINVAL;
516
Zhang Rui203d3d42008-01-17 15:51:08 +0800517 if (result)
518 return result;
519
520 return count;
521}
522
523static ssize_t
524trip_point_type_show(struct device *dev, struct device_attribute *attr,
525 char *buf)
526{
527 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000528 enum thermal_trip_type type;
529 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800530
531 if (!tz->ops->get_trip_type)
532 return -EPERM;
533
534 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
535 return -EINVAL;
536
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000537 result = tz->ops->get_trip_type(tz, trip, &type);
538 if (result)
539 return result;
540
541 switch (type) {
542 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300543 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000544 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300545 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000546 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300547 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000548 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300549 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000550 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300551 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000552 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800553}
554
555static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800556trip_point_temp_store(struct device *dev, struct device_attribute *attr,
557 const char *buf, size_t count)
558{
559 struct thermal_zone_device *tz = to_thermal_zone(dev);
560 int trip, ret;
561 unsigned long temperature;
562
563 if (!tz->ops->set_trip_temp)
564 return -EPERM;
565
566 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
567 return -EINVAL;
568
569 if (kstrtoul(buf, 10, &temperature))
570 return -EINVAL;
571
572 ret = tz->ops->set_trip_temp(tz, trip, temperature);
573
574 return ret ? ret : count;
575}
576
577static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800578trip_point_temp_show(struct device *dev, struct device_attribute *attr,
579 char *buf)
580{
581 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000582 int trip, ret;
583 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800584
585 if (!tz->ops->get_trip_temp)
586 return -EPERM;
587
588 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
589 return -EINVAL;
590
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000591 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
592
593 if (ret)
594 return ret;
595
596 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800597}
598
Matthew Garrett03a971a2008-12-03 18:00:38 +0000599static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800600trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
601 const char *buf, size_t count)
602{
603 struct thermal_zone_device *tz = to_thermal_zone(dev);
604 int trip, ret;
605 unsigned long temperature;
606
607 if (!tz->ops->set_trip_hyst)
608 return -EPERM;
609
610 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
611 return -EINVAL;
612
613 if (kstrtoul(buf, 10, &temperature))
614 return -EINVAL;
615
616 /*
617 * We are not doing any check on the 'temperature' value
618 * here. The driver implementing 'set_trip_hyst' has to
619 * take care of this.
620 */
621 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
622
623 return ret ? ret : count;
624}
625
626static ssize_t
627trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
628 char *buf)
629{
630 struct thermal_zone_device *tz = to_thermal_zone(dev);
631 int trip, ret;
632 unsigned long temperature;
633
634 if (!tz->ops->get_trip_hyst)
635 return -EPERM;
636
637 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
638 return -EINVAL;
639
640 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
641
642 return ret ? ret : sprintf(buf, "%ld\n", temperature);
643}
644
645static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000646passive_store(struct device *dev, struct device_attribute *attr,
647 const char *buf, size_t count)
648{
649 struct thermal_zone_device *tz = to_thermal_zone(dev);
650 struct thermal_cooling_device *cdev = NULL;
651 int state;
652
653 if (!sscanf(buf, "%d\n", &state))
654 return -EINVAL;
655
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100656 /* sanity check: values below 1000 millicelcius don't make sense
657 * and can cause the system to go into a thermal heart attack
658 */
659 if (state && state < 1000)
660 return -EINVAL;
661
Matthew Garrett03a971a2008-12-03 18:00:38 +0000662 if (state && !tz->forced_passive) {
663 mutex_lock(&thermal_list_lock);
664 list_for_each_entry(cdev, &thermal_cdev_list, node) {
665 if (!strncmp("Processor", cdev->type,
666 sizeof("Processor")))
667 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800668 THERMAL_TRIPS_NONE, cdev,
669 THERMAL_NO_LIMIT,
670 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000671 }
672 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100673 if (!tz->passive_delay)
674 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000675 } else if (!state && tz->forced_passive) {
676 mutex_lock(&thermal_list_lock);
677 list_for_each_entry(cdev, &thermal_cdev_list, node) {
678 if (!strncmp("Processor", cdev->type,
679 sizeof("Processor")))
680 thermal_zone_unbind_cooling_device(tz,
681 THERMAL_TRIPS_NONE,
682 cdev);
683 }
684 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100685 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000686 }
687
Matthew Garrett03a971a2008-12-03 18:00:38 +0000688 tz->forced_passive = state;
689
690 thermal_zone_device_update(tz);
691
692 return count;
693}
694
695static ssize_t
696passive_show(struct device *dev, struct device_attribute *attr,
697 char *buf)
698{
699 struct thermal_zone_device *tz = to_thermal_zone(dev);
700
701 return sprintf(buf, "%d\n", tz->forced_passive);
702}
703
Durgadoss R5a2c0902012-09-18 11:04:58 +0530704static ssize_t
705policy_store(struct device *dev, struct device_attribute *attr,
706 const char *buf, size_t count)
707{
708 int ret = -EINVAL;
709 struct thermal_zone_device *tz = to_thermal_zone(dev);
710 struct thermal_governor *gov;
711
712 mutex_lock(&thermal_governor_lock);
713
714 gov = __find_governor(buf);
715 if (!gov)
716 goto exit;
717
718 tz->governor = gov;
719 ret = count;
720
721exit:
722 mutex_unlock(&thermal_governor_lock);
723 return ret;
724}
725
726static ssize_t
727policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
728{
729 struct thermal_zone_device *tz = to_thermal_zone(dev);
730
731 return sprintf(buf, "%s\n", tz->governor->name);
732}
733
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000734#ifdef CONFIG_THERMAL_EMULATION
735static ssize_t
736emul_temp_store(struct device *dev, struct device_attribute *attr,
737 const char *buf, size_t count)
738{
739 struct thermal_zone_device *tz = to_thermal_zone(dev);
740 int ret = 0;
741 unsigned long temperature;
742
743 if (kstrtoul(buf, 10, &temperature))
744 return -EINVAL;
745
746 if (!tz->ops->set_emul_temp) {
747 mutex_lock(&tz->lock);
748 tz->emul_temperature = temperature;
749 mutex_unlock(&tz->lock);
750 } else {
751 ret = tz->ops->set_emul_temp(tz, temperature);
752 }
753
754 return ret ? ret : count;
755}
756static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
757#endif/*CONFIG_THERMAL_EMULATION*/
758
Zhang Rui203d3d42008-01-17 15:51:08 +0800759static DEVICE_ATTR(type, 0444, type_show, NULL);
760static DEVICE_ATTR(temp, 0444, temp_show, NULL);
761static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700762static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530763static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800764
Zhang Rui203d3d42008-01-17 15:51:08 +0800765/* sys I/F for cooling device */
766#define to_cooling_device(_dev) \
767 container_of(_dev, struct thermal_cooling_device, device)
768
769static ssize_t
770thermal_cooling_device_type_show(struct device *dev,
771 struct device_attribute *attr, char *buf)
772{
773 struct thermal_cooling_device *cdev = to_cooling_device(dev);
774
775 return sprintf(buf, "%s\n", cdev->type);
776}
777
778static ssize_t
779thermal_cooling_device_max_state_show(struct device *dev,
780 struct device_attribute *attr, char *buf)
781{
782 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000783 unsigned long state;
784 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800785
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000786 ret = cdev->ops->get_max_state(cdev, &state);
787 if (ret)
788 return ret;
789 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800790}
791
792static ssize_t
793thermal_cooling_device_cur_state_show(struct device *dev,
794 struct device_attribute *attr, char *buf)
795{
796 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000797 unsigned long state;
798 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800799
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000800 ret = cdev->ops->get_cur_state(cdev, &state);
801 if (ret)
802 return ret;
803 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800804}
805
806static ssize_t
807thermal_cooling_device_cur_state_store(struct device *dev,
808 struct device_attribute *attr,
809 const char *buf, size_t count)
810{
811 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000812 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800813 int result;
814
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000815 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800816 return -EINVAL;
817
Roel Kluinedb94912009-12-15 22:46:50 +0100818 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800819 return -EINVAL;
820
821 result = cdev->ops->set_cur_state(cdev, state);
822 if (result)
823 return result;
824 return count;
825}
826
827static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500828__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800829static DEVICE_ATTR(max_state, 0444,
830 thermal_cooling_device_max_state_show, NULL);
831static DEVICE_ATTR(cur_state, 0644,
832 thermal_cooling_device_cur_state_show,
833 thermal_cooling_device_cur_state_store);
834
835static ssize_t
836thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500837 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800838{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800839 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800840
841 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800842 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800843
844 if (instance->trip == THERMAL_TRIPS_NONE)
845 return sprintf(buf, "-1\n");
846 else
847 return sprintf(buf, "%d\n", instance->trip);
848}
849
850/* Device management */
851
Rene Herman16d75232008-06-24 19:38:56 +0200852#if defined(CONFIG_THERMAL_HWMON)
853
Zhang Ruie68b16a2008-04-21 16:07:52 +0800854/* hwmon sys I/F */
855#include <linux/hwmon.h>
Jean Delvare31f53962011-07-28 13:48:42 -0700856
857/* thermal zone devices with the same type share one hwmon device */
858struct thermal_hwmon_device {
859 char type[THERMAL_NAME_LENGTH];
860 struct device *device;
861 int count;
862 struct list_head tz_list;
863 struct list_head node;
864};
865
866struct thermal_hwmon_attr {
867 struct device_attribute attr;
868 char name[16];
869};
870
871/* one temperature input for each thermal zone */
872struct thermal_hwmon_temp {
873 struct list_head hwmon_node;
874 struct thermal_zone_device *tz;
875 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
876 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
877};
878
Zhang Ruie68b16a2008-04-21 16:07:52 +0800879static LIST_HEAD(thermal_hwmon_list);
880
881static ssize_t
882name_show(struct device *dev, struct device_attribute *attr, char *buf)
883{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700884 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800885 return sprintf(buf, "%s\n", hwmon->type);
886}
887static DEVICE_ATTR(name, 0444, name_show, NULL);
888
889static ssize_t
890temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
891{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000892 long temperature;
893 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800894 struct thermal_hwmon_attr *hwmon_attr
895 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700896 struct thermal_hwmon_temp *temp
897 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800898 temp_input);
Jean Delvare31f53962011-07-28 13:48:42 -0700899 struct thermal_zone_device *tz = temp->tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800900
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000901 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000902
903 if (ret)
904 return ret;
905
906 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800907}
908
909static ssize_t
910temp_crit_show(struct device *dev, struct device_attribute *attr,
911 char *buf)
912{
913 struct thermal_hwmon_attr *hwmon_attr
914 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700915 struct thermal_hwmon_temp *temp
916 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800917 temp_crit);
Jean Delvare31f53962011-07-28 13:48:42 -0700918 struct thermal_zone_device *tz = temp->tz;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000919 long temperature;
920 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800921
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000922 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
923 if (ret)
924 return ret;
925
926 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800927}
928
929
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700930static struct thermal_hwmon_device *
931thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
932{
933 struct thermal_hwmon_device *hwmon;
934
935 mutex_lock(&thermal_list_lock);
936 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
937 if (!strcmp(hwmon->type, tz->type)) {
938 mutex_unlock(&thermal_list_lock);
939 return hwmon;
940 }
941 mutex_unlock(&thermal_list_lock);
942
943 return NULL;
944}
945
Jean Delvare31f53962011-07-28 13:48:42 -0700946/* Find the temperature input matching a given thermal zone */
947static struct thermal_hwmon_temp *
948thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
949 const struct thermal_zone_device *tz)
950{
951 struct thermal_hwmon_temp *temp;
952
953 mutex_lock(&thermal_list_lock);
954 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
955 if (temp->tz == tz) {
956 mutex_unlock(&thermal_list_lock);
957 return temp;
958 }
959 mutex_unlock(&thermal_list_lock);
960
961 return NULL;
962}
963
Zhang Ruie68b16a2008-04-21 16:07:52 +0800964static int
965thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
966{
967 struct thermal_hwmon_device *hwmon;
Jean Delvare31f53962011-07-28 13:48:42 -0700968 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800969 int new_hwmon_device = 1;
970 int result;
971
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700972 hwmon = thermal_hwmon_lookup_by_type(tz);
973 if (hwmon) {
974 new_hwmon_device = 0;
975 goto register_sys_interface;
976 }
Zhang Ruie68b16a2008-04-21 16:07:52 +0800977
978 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
979 if (!hwmon)
980 return -ENOMEM;
981
982 INIT_LIST_HEAD(&hwmon->tz_list);
983 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
984 hwmon->device = hwmon_device_register(NULL);
985 if (IS_ERR(hwmon->device)) {
986 result = PTR_ERR(hwmon->device);
987 goto free_mem;
988 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700989 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800990 result = device_create_file(hwmon->device, &dev_attr_name);
991 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530992 goto free_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800993
994 register_sys_interface:
Jean Delvare31f53962011-07-28 13:48:42 -0700995 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
996 if (!temp) {
997 result = -ENOMEM;
998 goto unregister_name;
999 }
1000
1001 temp->tz = tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001002 hwmon->count++;
1003
Guenter Roeck79a49162012-07-21 10:53:48 +10001004 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +08001005 "temp%d_input", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -07001006 temp->temp_input.attr.attr.name = temp->temp_input.name;
1007 temp->temp_input.attr.attr.mode = 0444;
1008 temp->temp_input.attr.show = temp_input_show;
1009 sysfs_attr_init(&temp->temp_input.attr.attr);
1010 result = device_create_file(hwmon->device, &temp->temp_input.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001011 if (result)
Jean Delvare31f53962011-07-28 13:48:42 -07001012 goto free_temp_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001013
1014 if (tz->ops->get_crit_temp) {
1015 unsigned long temperature;
1016 if (!tz->ops->get_crit_temp(tz, &temperature)) {
Guenter Roeck79a49162012-07-21 10:53:48 +10001017 snprintf(temp->temp_crit.name,
1018 sizeof(temp->temp_crit.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +08001019 "temp%d_crit", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -07001020 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
1021 temp->temp_crit.attr.attr.mode = 0444;
1022 temp->temp_crit.attr.show = temp_crit_show;
1023 sysfs_attr_init(&temp->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001024 result = device_create_file(hwmon->device,
Jean Delvare31f53962011-07-28 13:48:42 -07001025 &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001026 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +05301027 goto unregister_input;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001028 }
1029 }
1030
1031 mutex_lock(&thermal_list_lock);
1032 if (new_hwmon_device)
1033 list_add_tail(&hwmon->node, &thermal_hwmon_list);
Jean Delvare31f53962011-07-28 13:48:42 -07001034 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001035 mutex_unlock(&thermal_list_lock);
1036
1037 return 0;
1038
Durgadoss Rb299eb52011-03-03 04:30:13 +05301039 unregister_input:
Jean Delvare31f53962011-07-28 13:48:42 -07001040 device_remove_file(hwmon->device, &temp->temp_input.attr);
1041 free_temp_mem:
1042 kfree(temp);
Durgadoss Rb299eb52011-03-03 04:30:13 +05301043 unregister_name:
Zhang Ruie68b16a2008-04-21 16:07:52 +08001044 if (new_hwmon_device) {
1045 device_remove_file(hwmon->device, &dev_attr_name);
1046 hwmon_device_unregister(hwmon->device);
1047 }
1048 free_mem:
1049 if (new_hwmon_device)
1050 kfree(hwmon);
1051
1052 return result;
1053}
1054
1055static void
1056thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1057{
Jean Delvare31f53962011-07-28 13:48:42 -07001058 struct thermal_hwmon_device *hwmon;
1059 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001060
Jean Delvare31f53962011-07-28 13:48:42 -07001061 hwmon = thermal_hwmon_lookup_by_type(tz);
1062 if (unlikely(!hwmon)) {
1063 /* Should never happen... */
1064 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
1065 return;
1066 }
1067
1068 temp = thermal_hwmon_lookup_temp(hwmon, tz);
1069 if (unlikely(!temp)) {
1070 /* Should never happen... */
1071 dev_dbg(&tz->device, "temperature input lookup failed!\n");
1072 return;
1073 }
1074
1075 device_remove_file(hwmon->device, &temp->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +05301076 if (tz->ops->get_crit_temp)
Jean Delvare31f53962011-07-28 13:48:42 -07001077 device_remove_file(hwmon->device, &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001078
1079 mutex_lock(&thermal_list_lock);
Jean Delvare31f53962011-07-28 13:48:42 -07001080 list_del(&temp->hwmon_node);
1081 kfree(temp);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001082 if (!list_empty(&hwmon->tz_list)) {
1083 mutex_unlock(&thermal_list_lock);
1084 return;
1085 }
1086 list_del(&hwmon->node);
1087 mutex_unlock(&thermal_list_lock);
1088
1089 device_remove_file(hwmon->device, &dev_attr_name);
1090 hwmon_device_unregister(hwmon->device);
1091 kfree(hwmon);
1092}
1093#else
1094static int
1095thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
1096{
1097 return 0;
1098}
1099
1100static void
1101thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1102{
1103}
1104#endif
1105
Zhang Rui203d3d42008-01-17 15:51:08 +08001106/**
1107 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +08001108 * @tz: thermal zone device
1109 * @trip: indicates which trip point the cooling devices is
1110 * associated with in this thermal zone.
1111 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -05001112 *
1113 * This function is usually called in the thermal zone device .bind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +08001114 */
1115int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1116 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +08001117 struct thermal_cooling_device *cdev,
1118 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +08001119{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001120 struct thermal_instance *dev;
1121 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -05001122 struct thermal_zone_device *pos1;
1123 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +08001124 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +08001125 int result;
1126
Len Brown543a9562008-02-07 16:55:08 -05001127 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +08001128 return -EINVAL;
1129
Thomas Sujithc7516702008-02-15 00:58:50 -05001130 list_for_each_entry(pos1, &thermal_tz_list, node) {
1131 if (pos1 == tz)
1132 break;
1133 }
1134 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1135 if (pos2 == cdev)
1136 break;
1137 }
1138
1139 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +08001140 return -EINVAL;
1141
Zhang Rui9d998422012-06-26 16:35:57 +08001142 cdev->ops->get_max_state(cdev, &max_state);
1143
1144 /* lower default 0, upper default max_state */
1145 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1146 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1147
1148 if (lower > upper || upper > max_state)
1149 return -EINVAL;
1150
Zhang Rui203d3d42008-01-17 15:51:08 +08001151 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001152 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001153 if (!dev)
1154 return -ENOMEM;
1155 dev->tz = tz;
1156 dev->cdev = cdev;
1157 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +08001158 dev->upper = upper;
1159 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +08001160 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +08001161
Zhang Rui203d3d42008-01-17 15:51:08 +08001162 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1163 if (result)
1164 goto free_mem;
1165
1166 sprintf(dev->name, "cdev%d", dev->id);
1167 result =
1168 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1169 if (result)
1170 goto release_idr;
1171
1172 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -07001173 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001174 dev->attr.attr.name = dev->attr_name;
1175 dev->attr.attr.mode = 0444;
1176 dev->attr.show = thermal_cooling_device_trip_point_show;
1177 result = device_create_file(&tz->device, &dev->attr);
1178 if (result)
1179 goto remove_symbol_link;
1180
1181 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001182 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001183 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +08001184 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1185 result = -EEXIST;
1186 break;
1187 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001188 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001189 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001190 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1191 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001192 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001193 mutex_unlock(&tz->lock);
1194
1195 if (!result)
1196 return 0;
1197
1198 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -07001199remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001200 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -07001201release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001202 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -07001203free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001204 kfree(dev);
1205 return result;
1206}
1207EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
1208
1209/**
1210 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +08001211 * @tz: thermal zone device
1212 * @trip: indicates which trip point the cooling devices is
1213 * associated with in this thermal zone.
1214 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -05001215 *
1216 * This function is usually called in the thermal zone device .unbind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +08001217 */
1218int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1219 int trip,
1220 struct thermal_cooling_device *cdev)
1221{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001222 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001223
1224 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001225 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001226 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001227 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001228 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001229 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001230 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001231 mutex_unlock(&tz->lock);
1232 goto unbind;
1233 }
1234 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001235 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001236 mutex_unlock(&tz->lock);
1237
1238 return -ENODEV;
1239
Joe Perchescaca8b82012-03-21 12:55:02 -07001240unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +08001241 device_remove_file(&tz->device, &pos->attr);
1242 sysfs_remove_link(&tz->device.kobj, pos->name);
1243 release_idr(&tz->idr, &tz->lock, pos->id);
1244 kfree(pos);
1245 return 0;
1246}
1247EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
1248
1249static void thermal_release(struct device *dev)
1250{
1251 struct thermal_zone_device *tz;
1252 struct thermal_cooling_device *cdev;
1253
Joe Perchescaca8b82012-03-21 12:55:02 -07001254 if (!strncmp(dev_name(dev), "thermal_zone",
1255 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001256 tz = to_thermal_zone(dev);
1257 kfree(tz);
1258 } else {
1259 cdev = to_cooling_device(dev);
1260 kfree(cdev);
1261 }
1262}
1263
1264static struct class thermal_class = {
1265 .name = "thermal",
1266 .dev_release = thermal_release,
1267};
1268
1269/**
1270 * thermal_cooling_device_register - register a new thermal cooling device
1271 * @type: the thermal cooling device type.
1272 * @devdata: device private data.
1273 * @ops: standard thermal cooling devices callbacks.
1274 */
Joe Perchescaca8b82012-03-21 12:55:02 -07001275struct thermal_cooling_device *
1276thermal_cooling_device_register(char *type, void *devdata,
1277 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001278{
1279 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001280 int result;
1281
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001282 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001283 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001284
1285 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001286 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001287 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001288
1289 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1290 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001291 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001292
1293 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1294 if (result) {
1295 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001296 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001297 }
1298
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001299 strcpy(cdev->type, type ? : "");
Zhang Ruif4a821c2012-07-24 16:56:21 +08001300 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001301 INIT_LIST_HEAD(&cdev->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001302 cdev->ops = ops;
Zhang Ruice119f82012-06-27 14:13:04 +08001303 cdev->updated = true;
Zhang Rui203d3d42008-01-17 15:51:08 +08001304 cdev->device.class = &thermal_class;
1305 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001306 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001307 result = device_register(&cdev->device);
1308 if (result) {
1309 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1310 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001311 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001312 }
1313
1314 /* sys I/F */
1315 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001316 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001317 if (result)
1318 goto unregister;
1319 }
1320
1321 result = device_create_file(&cdev->device, &dev_attr_max_state);
1322 if (result)
1323 goto unregister;
1324
1325 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1326 if (result)
1327 goto unregister;
1328
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301329 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001330 mutex_lock(&thermal_list_lock);
1331 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001332 mutex_unlock(&thermal_list_lock);
1333
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301334 /* Update binding information for 'this' new cdev */
1335 bind_cdev(cdev);
1336
1337 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001338
Joe Perchescaca8b82012-03-21 12:55:02 -07001339unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001340 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1341 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001342 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001343}
1344EXPORT_SYMBOL(thermal_cooling_device_register);
1345
1346/**
1347 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001348 * @cdev: the thermal cooling device to remove.
1349 *
1350 * thermal_cooling_device_unregister() must be called when the device is no
1351 * longer needed.
1352 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301353void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001354{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301355 int i;
1356 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001357 struct thermal_zone_device *tz;
1358 struct thermal_cooling_device *pos = NULL;
1359
1360 if (!cdev)
1361 return;
1362
1363 mutex_lock(&thermal_list_lock);
1364 list_for_each_entry(pos, &thermal_cdev_list, node)
1365 if (pos == cdev)
1366 break;
1367 if (pos != cdev) {
1368 /* thermal cooling device not found */
1369 mutex_unlock(&thermal_list_lock);
1370 return;
1371 }
1372 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301373
1374 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001375 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301376 if (tz->ops->unbind) {
1377 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001378 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301379 }
1380
1381 if (!tz->tzp || !tz->tzp->tbp)
1382 continue;
1383
1384 tzp = tz->tzp;
1385 for (i = 0; i < tzp->num_tbps; i++) {
1386 if (tzp->tbp[i].cdev == cdev) {
1387 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1388 tzp->tbp[i].cdev = NULL;
1389 }
1390 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001391 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301392
Zhang Rui203d3d42008-01-17 15:51:08 +08001393 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301394
Zhang Rui203d3d42008-01-17 15:51:08 +08001395 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001396 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001397 device_remove_file(&cdev->device, &dev_attr_max_state);
1398 device_remove_file(&cdev->device, &dev_attr_cur_state);
1399
1400 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1401 device_unregister(&cdev->device);
1402 return;
1403}
1404EXPORT_SYMBOL(thermal_cooling_device_unregister);
1405
Durgadoss Rdc765482012-09-18 11:05:00 +05301406void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001407{
1408 struct thermal_instance *instance;
1409 unsigned long target = 0;
1410
1411 /* cooling device is updated*/
1412 if (cdev->updated)
1413 return;
1414
Zhang Ruif4a821c2012-07-24 16:56:21 +08001415 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001416 /* Make sure cdev enters the deepest cooling state */
1417 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1418 if (instance->target == THERMAL_NO_TARGET)
1419 continue;
1420 if (instance->target > target)
1421 target = instance->target;
1422 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001423 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001424 cdev->ops->set_cur_state(cdev, target);
1425 cdev->updated = true;
1426}
Durgadoss Rdc765482012-09-18 11:05:00 +05301427EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001428
Matthew Garrettb1569e92008-12-03 17:55:32 +00001429/**
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301430 * notify_thermal_framework - Sensor drivers use this API to notify framework
1431 * @tz: thermal zone device
1432 * @trip: indicates which trip point has been crossed
1433 *
1434 * This function handles the trip events from sensor drivers. It starts
1435 * throttling the cooling devices according to the policy configured.
1436 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1437 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1438 * The throttling policy is based on the configured platform data; if no
1439 * platform data is provided, this uses the step_wise throttling policy.
1440 */
1441void notify_thermal_framework(struct thermal_zone_device *tz, int trip)
1442{
1443 handle_thermal_trip(tz, trip);
1444}
1445EXPORT_SYMBOL(notify_thermal_framework);
1446
1447/**
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001448 * create_trip_attrs - create attributes for trip points
1449 * @tz: the thermal zone device
1450 * @mask: Writeable trip point bitmap.
1451 */
1452static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1453{
1454 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001455 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001456
Durgadoss R27365a62012-07-25 10:10:59 +08001457 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001458 if (!tz->trip_type_attrs)
1459 return -ENOMEM;
1460
Durgadoss R27365a62012-07-25 10:10:59 +08001461 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001462 if (!tz->trip_temp_attrs) {
1463 kfree(tz->trip_type_attrs);
1464 return -ENOMEM;
1465 }
1466
Durgadoss R27365a62012-07-25 10:10:59 +08001467 if (tz->ops->get_trip_hyst) {
1468 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1469 if (!tz->trip_hyst_attrs) {
1470 kfree(tz->trip_type_attrs);
1471 kfree(tz->trip_temp_attrs);
1472 return -ENOMEM;
1473 }
1474 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001475
Durgadoss R27365a62012-07-25 10:10:59 +08001476
1477 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001478 /* create trip type attribute */
1479 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1480 "trip_point_%d_type", indx);
1481
1482 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1483 tz->trip_type_attrs[indx].attr.attr.name =
1484 tz->trip_type_attrs[indx].name;
1485 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1486 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1487
1488 device_create_file(&tz->device,
1489 &tz->trip_type_attrs[indx].attr);
1490
1491 /* create trip temp attribute */
1492 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1493 "trip_point_%d_temp", indx);
1494
1495 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1496 tz->trip_temp_attrs[indx].attr.attr.name =
1497 tz->trip_temp_attrs[indx].name;
1498 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1499 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1500 if (mask & (1 << indx)) {
1501 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1502 tz->trip_temp_attrs[indx].attr.store =
1503 trip_point_temp_store;
1504 }
1505
1506 device_create_file(&tz->device,
1507 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001508
1509 /* create Optional trip hyst attribute */
1510 if (!tz->ops->get_trip_hyst)
1511 continue;
1512 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1513 "trip_point_%d_hyst", indx);
1514
1515 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1516 tz->trip_hyst_attrs[indx].attr.attr.name =
1517 tz->trip_hyst_attrs[indx].name;
1518 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1519 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1520 if (tz->ops->set_trip_hyst) {
1521 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1522 tz->trip_hyst_attrs[indx].attr.store =
1523 trip_point_hyst_store;
1524 }
1525
1526 device_create_file(&tz->device,
1527 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001528 }
1529 return 0;
1530}
1531
1532static void remove_trip_attrs(struct thermal_zone_device *tz)
1533{
1534 int indx;
1535
1536 for (indx = 0; indx < tz->trips; indx++) {
1537 device_remove_file(&tz->device,
1538 &tz->trip_type_attrs[indx].attr);
1539 device_remove_file(&tz->device,
1540 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001541 if (tz->ops->get_trip_hyst)
1542 device_remove_file(&tz->device,
1543 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001544 }
1545 kfree(tz->trip_type_attrs);
1546 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001547 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001548}
1549
1550/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001551 * thermal_zone_device_register - register a new thermal zone device
1552 * @type: the thermal zone device type
1553 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001554 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001555 * @devdata: private device data
1556 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301557 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001558 * @passive_delay: number of milliseconds to wait between polls when
1559 * performing passive cooling
1560 * @polling_delay: number of milliseconds to wait between polls when checking
1561 * whether trip points have been crossed (0 for interrupt
1562 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001563 *
1564 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001565 * longer needed. The passive cooling depends on the .get_trend() return value.
Zhang Rui203d3d42008-01-17 15:51:08 +08001566 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001567struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001568 int trips, int mask, void *devdata,
Alan Cox5b275ce2010-11-11 15:27:29 +00001569 const struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301570 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001571 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001572{
1573 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001574 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001575 int result;
1576 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001577 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001578
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001579 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001580 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001581
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001582 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001583 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001584
1585 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001586 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001587
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001588 if (trips > 0 && !ops->get_trip_type)
1589 return ERR_PTR(-EINVAL);
1590
Zhang Rui203d3d42008-01-17 15:51:08 +08001591 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1592 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001593 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001594
Zhang Rui2d374132012-06-27 10:09:00 +08001595 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001596 idr_init(&tz->idr);
1597 mutex_init(&tz->lock);
1598 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1599 if (result) {
1600 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001601 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001602 }
1603
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001604 strcpy(tz->type, type ? : "");
Zhang Rui203d3d42008-01-17 15:51:08 +08001605 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301606 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001607 tz->device.class = &thermal_class;
1608 tz->devdata = devdata;
1609 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001610 tz->passive_delay = passive_delay;
1611 tz->polling_delay = polling_delay;
1612
Kay Sievers354655e2009-01-06 10:44:37 -08001613 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001614 result = device_register(&tz->device);
1615 if (result) {
1616 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1617 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001618 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001619 }
1620
1621 /* sys I/F */
1622 if (type) {
1623 result = device_create_file(&tz->device, &dev_attr_type);
1624 if (result)
1625 goto unregister;
1626 }
1627
1628 result = device_create_file(&tz->device, &dev_attr_temp);
1629 if (result)
1630 goto unregister;
1631
1632 if (ops->get_mode) {
1633 result = device_create_file(&tz->device, &dev_attr_mode);
1634 if (result)
1635 goto unregister;
1636 }
1637
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001638 result = create_trip_attrs(tz, mask);
1639 if (result)
1640 goto unregister;
1641
Zhang Rui203d3d42008-01-17 15:51:08 +08001642 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001643 tz->ops->get_trip_type(tz, count, &trip_type);
1644 if (trip_type == THERMAL_TRIP_PASSIVE)
1645 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001646 }
1647
Durgadoss R5a2c0902012-09-18 11:04:58 +05301648 if (!passive) {
1649 result = device_create_file(&tz->device, &dev_attr_passive);
1650 if (result)
1651 goto unregister;
1652 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001653
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001654#ifdef CONFIG_THERMAL_EMULATION
1655 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1656 if (result)
1657 goto unregister;
1658#endif
Durgadoss R5a2c0902012-09-18 11:04:58 +05301659 /* Create policy attribute */
1660 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001661 if (result)
1662 goto unregister;
1663
Durgadoss Ra4a15482012-09-18 11:04:57 +05301664 /* Update 'this' zone's governor information */
1665 mutex_lock(&thermal_governor_lock);
1666
1667 if (tz->tzp)
1668 tz->governor = __find_governor(tz->tzp->governor_name);
1669 else
1670 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1671
1672 mutex_unlock(&thermal_governor_lock);
1673
Zhang Ruie68b16a2008-04-21 16:07:52 +08001674 result = thermal_add_hwmon_sysfs(tz);
1675 if (result)
1676 goto unregister;
1677
Zhang Rui203d3d42008-01-17 15:51:08 +08001678 mutex_lock(&thermal_list_lock);
1679 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001680 mutex_unlock(&thermal_list_lock);
1681
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301682 /* Bind cooling devices for this zone */
1683 bind_tz(tz);
1684
Matthew Garrettb1569e92008-12-03 17:55:32 +00001685 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1686
1687 thermal_zone_device_update(tz);
1688
Zhang Rui203d3d42008-01-17 15:51:08 +08001689 if (!result)
1690 return tz;
1691
Joe Perchescaca8b82012-03-21 12:55:02 -07001692unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001693 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1694 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001695 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001696}
1697EXPORT_SYMBOL(thermal_zone_device_register);
1698
1699/**
1700 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001701 * @tz: the thermal zone device to remove
1702 */
1703void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1704{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301705 int i;
1706 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001707 struct thermal_cooling_device *cdev;
1708 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001709
1710 if (!tz)
1711 return;
1712
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301713 tzp = tz->tzp;
1714
Zhang Rui203d3d42008-01-17 15:51:08 +08001715 mutex_lock(&thermal_list_lock);
1716 list_for_each_entry(pos, &thermal_tz_list, node)
1717 if (pos == tz)
1718 break;
1719 if (pos != tz) {
1720 /* thermal zone device not found */
1721 mutex_unlock(&thermal_list_lock);
1722 return;
1723 }
1724 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301725
1726 /* Unbind all cdevs associated with 'this' thermal zone */
1727 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1728 if (tz->ops->unbind) {
1729 tz->ops->unbind(tz, cdev);
1730 continue;
1731 }
1732
1733 if (!tzp || !tzp->tbp)
1734 break;
1735
1736 for (i = 0; i < tzp->num_tbps; i++) {
1737 if (tzp->tbp[i].cdev == cdev) {
1738 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1739 tzp->tbp[i].cdev = NULL;
1740 }
1741 }
1742 }
1743
Zhang Rui203d3d42008-01-17 15:51:08 +08001744 mutex_unlock(&thermal_list_lock);
1745
Matthew Garrettb1569e92008-12-03 17:55:32 +00001746 thermal_zone_device_set_polling(tz, 0);
1747
Zhang Rui203d3d42008-01-17 15:51:08 +08001748 if (tz->type[0])
1749 device_remove_file(&tz->device, &dev_attr_type);
1750 device_remove_file(&tz->device, &dev_attr_temp);
1751 if (tz->ops->get_mode)
1752 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301753 device_remove_file(&tz->device, &dev_attr_policy);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001754 remove_trip_attrs(tz);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301755 tz->governor = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001756
Zhang Ruie68b16a2008-04-21 16:07:52 +08001757 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001758 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1759 idr_destroy(&tz->idr);
1760 mutex_destroy(&tz->lock);
1761 device_unregister(&tz->device);
1762 return;
1763}
Zhang Rui203d3d42008-01-17 15:51:08 +08001764EXPORT_SYMBOL(thermal_zone_device_unregister);
1765
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001766#ifdef CONFIG_NET
1767static struct genl_family thermal_event_genl_family = {
1768 .id = GENL_ID_GENERATE,
1769 .name = THERMAL_GENL_FAMILY_NAME,
1770 .version = THERMAL_GENL_VERSION,
1771 .maxattr = THERMAL_GENL_ATTR_MAX,
1772};
1773
1774static struct genl_multicast_group thermal_event_mcgrp = {
1775 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1776};
1777
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001778int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1779 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301780{
1781 struct sk_buff *skb;
1782 struct nlattr *attr;
1783 struct thermal_genl_event *thermal_event;
1784 void *msg_header;
1785 int size;
1786 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001787 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301788
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001789 if (!tz)
1790 return -EINVAL;
1791
R.Durgadoss4cb18722010-10-27 03:33:29 +05301792 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001793 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1794 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301795
1796 skb = genlmsg_new(size, GFP_ATOMIC);
1797 if (!skb)
1798 return -ENOMEM;
1799
1800 /* add the genetlink message header */
1801 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1802 &thermal_event_genl_family, 0,
1803 THERMAL_GENL_CMD_EVENT);
1804 if (!msg_header) {
1805 nlmsg_free(skb);
1806 return -ENOMEM;
1807 }
1808
1809 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001810 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1811 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301812
1813 if (!attr) {
1814 nlmsg_free(skb);
1815 return -EINVAL;
1816 }
1817
1818 thermal_event = nla_data(attr);
1819 if (!thermal_event) {
1820 nlmsg_free(skb);
1821 return -EINVAL;
1822 }
1823
1824 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1825
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001826 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301827 thermal_event->event = event;
1828
1829 /* send multicast genetlink message */
1830 result = genlmsg_end(skb, msg_header);
1831 if (result < 0) {
1832 nlmsg_free(skb);
1833 return result;
1834 }
1835
1836 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1837 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001838 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301839
1840 return result;
1841}
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001842EXPORT_SYMBOL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301843
1844static int genetlink_init(void)
1845{
1846 int result;
1847
1848 result = genl_register_family(&thermal_event_genl_family);
1849 if (result)
1850 return result;
1851
1852 result = genl_register_mc_group(&thermal_event_genl_family,
1853 &thermal_event_mcgrp);
1854 if (result)
1855 genl_unregister_family(&thermal_event_genl_family);
1856 return result;
1857}
1858
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001859static void genetlink_exit(void)
1860{
1861 genl_unregister_family(&thermal_event_genl_family);
1862}
1863#else /* !CONFIG_NET */
1864static inline int genetlink_init(void) { return 0; }
1865static inline void genetlink_exit(void) {}
1866#endif /* !CONFIG_NET */
1867
Zhang Rui203d3d42008-01-17 15:51:08 +08001868static int __init thermal_init(void)
1869{
1870 int result = 0;
1871
1872 result = class_register(&thermal_class);
1873 if (result) {
1874 idr_destroy(&thermal_tz_idr);
1875 idr_destroy(&thermal_cdev_idr);
1876 mutex_destroy(&thermal_idr_lock);
1877 mutex_destroy(&thermal_list_lock);
1878 }
R.Durgadoss4cb18722010-10-27 03:33:29 +05301879 result = genetlink_init();
Zhang Rui203d3d42008-01-17 15:51:08 +08001880 return result;
1881}
1882
1883static void __exit thermal_exit(void)
1884{
1885 class_unregister(&thermal_class);
1886 idr_destroy(&thermal_tz_idr);
1887 idr_destroy(&thermal_cdev_idr);
1888 mutex_destroy(&thermal_idr_lock);
1889 mutex_destroy(&thermal_list_lock);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301890 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001891}
1892
R.Durgadoss4cb18722010-10-27 03:33:29 +05301893fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001894module_exit(thermal_exit);