blob: 0675687c6de85f469710c051407229d8387b8483 [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{
384 int ret = 0, count;
385 unsigned long crit_temp = -1UL;
386 enum thermal_trip_type type;
387
388 mutex_lock(&tz->lock);
389
390 ret = tz->ops->get_temp(tz, temp);
391#ifdef CONFIG_THERMAL_EMULATION
392 if (!tz->emul_temperature)
393 goto skip_emul;
394
395 for (count = 0; count < tz->trips; count++) {
396 ret = tz->ops->get_trip_type(tz, count, &type);
397 if (!ret && type == THERMAL_TRIP_CRITICAL) {
398 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
399 break;
400 }
401 }
402
403 if (ret)
404 goto skip_emul;
405
406 if (*temp < crit_temp)
407 *temp = tz->emul_temperature;
408skip_emul:
409#endif
410 mutex_unlock(&tz->lock);
411 return ret;
412}
413
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530414static void update_temperature(struct thermal_zone_device *tz)
415{
416 long temp;
417 int ret;
418
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000419 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530420 if (ret) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000421 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
422 tz->id);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000423 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530424 }
425
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000426 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530427 tz->last_temperature = tz->temperature;
428 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530429 mutex_unlock(&tz->lock);
430}
431
432void thermal_zone_device_update(struct thermal_zone_device *tz)
433{
434 int count;
435
436 update_temperature(tz);
437
438 for (count = 0; count < tz->trips; count++)
439 handle_thermal_trip(tz, count);
440}
441EXPORT_SYMBOL(thermal_zone_device_update);
442
443static void thermal_zone_device_check(struct work_struct *work)
444{
445 struct thermal_zone_device *tz = container_of(work, struct
446 thermal_zone_device,
447 poll_queue.work);
448 thermal_zone_device_update(tz);
449}
450
Zhang Rui203d3d42008-01-17 15:51:08 +0800451/* sys I/F for thermal zone */
452
453#define to_thermal_zone(_dev) \
454 container_of(_dev, struct thermal_zone_device, device)
455
456static ssize_t
457type_show(struct device *dev, struct device_attribute *attr, char *buf)
458{
459 struct thermal_zone_device *tz = to_thermal_zone(dev);
460
461 return sprintf(buf, "%s\n", tz->type);
462}
463
464static ssize_t
465temp_show(struct device *dev, struct device_attribute *attr, char *buf)
466{
467 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000468 long temperature;
469 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800470
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000471 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000472
473 if (ret)
474 return ret;
475
476 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800477}
478
479static ssize_t
480mode_show(struct device *dev, struct device_attribute *attr, char *buf)
481{
482 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000483 enum thermal_device_mode mode;
484 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800485
486 if (!tz->ops->get_mode)
487 return -EPERM;
488
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000489 result = tz->ops->get_mode(tz, &mode);
490 if (result)
491 return result;
492
493 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
494 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800495}
496
497static ssize_t
498mode_store(struct device *dev, struct device_attribute *attr,
499 const char *buf, size_t count)
500{
501 struct thermal_zone_device *tz = to_thermal_zone(dev);
502 int result;
503
504 if (!tz->ops->set_mode)
505 return -EPERM;
506
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530507 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000508 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530509 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000510 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
511 else
512 result = -EINVAL;
513
Zhang Rui203d3d42008-01-17 15:51:08 +0800514 if (result)
515 return result;
516
517 return count;
518}
519
520static ssize_t
521trip_point_type_show(struct device *dev, struct device_attribute *attr,
522 char *buf)
523{
524 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000525 enum thermal_trip_type type;
526 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800527
528 if (!tz->ops->get_trip_type)
529 return -EPERM;
530
531 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
532 return -EINVAL;
533
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000534 result = tz->ops->get_trip_type(tz, trip, &type);
535 if (result)
536 return result;
537
538 switch (type) {
539 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300540 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000541 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300542 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000543 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300544 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000545 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300546 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000547 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300548 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000549 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800550}
551
552static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800553trip_point_temp_store(struct device *dev, struct device_attribute *attr,
554 const char *buf, size_t count)
555{
556 struct thermal_zone_device *tz = to_thermal_zone(dev);
557 int trip, ret;
558 unsigned long temperature;
559
560 if (!tz->ops->set_trip_temp)
561 return -EPERM;
562
563 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
564 return -EINVAL;
565
566 if (kstrtoul(buf, 10, &temperature))
567 return -EINVAL;
568
569 ret = tz->ops->set_trip_temp(tz, trip, temperature);
570
571 return ret ? ret : count;
572}
573
574static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800575trip_point_temp_show(struct device *dev, struct device_attribute *attr,
576 char *buf)
577{
578 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000579 int trip, ret;
580 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800581
582 if (!tz->ops->get_trip_temp)
583 return -EPERM;
584
585 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
586 return -EINVAL;
587
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000588 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
589
590 if (ret)
591 return ret;
592
593 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800594}
595
Matthew Garrett03a971a2008-12-03 18:00:38 +0000596static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800597trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
598 const char *buf, size_t count)
599{
600 struct thermal_zone_device *tz = to_thermal_zone(dev);
601 int trip, ret;
602 unsigned long temperature;
603
604 if (!tz->ops->set_trip_hyst)
605 return -EPERM;
606
607 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
608 return -EINVAL;
609
610 if (kstrtoul(buf, 10, &temperature))
611 return -EINVAL;
612
613 /*
614 * We are not doing any check on the 'temperature' value
615 * here. The driver implementing 'set_trip_hyst' has to
616 * take care of this.
617 */
618 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
619
620 return ret ? ret : count;
621}
622
623static ssize_t
624trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
625 char *buf)
626{
627 struct thermal_zone_device *tz = to_thermal_zone(dev);
628 int trip, ret;
629 unsigned long temperature;
630
631 if (!tz->ops->get_trip_hyst)
632 return -EPERM;
633
634 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
635 return -EINVAL;
636
637 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
638
639 return ret ? ret : sprintf(buf, "%ld\n", temperature);
640}
641
642static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000643passive_store(struct device *dev, struct device_attribute *attr,
644 const char *buf, size_t count)
645{
646 struct thermal_zone_device *tz = to_thermal_zone(dev);
647 struct thermal_cooling_device *cdev = NULL;
648 int state;
649
650 if (!sscanf(buf, "%d\n", &state))
651 return -EINVAL;
652
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100653 /* sanity check: values below 1000 millicelcius don't make sense
654 * and can cause the system to go into a thermal heart attack
655 */
656 if (state && state < 1000)
657 return -EINVAL;
658
Matthew Garrett03a971a2008-12-03 18:00:38 +0000659 if (state && !tz->forced_passive) {
660 mutex_lock(&thermal_list_lock);
661 list_for_each_entry(cdev, &thermal_cdev_list, node) {
662 if (!strncmp("Processor", cdev->type,
663 sizeof("Processor")))
664 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800665 THERMAL_TRIPS_NONE, cdev,
666 THERMAL_NO_LIMIT,
667 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000668 }
669 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100670 if (!tz->passive_delay)
671 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000672 } else if (!state && tz->forced_passive) {
673 mutex_lock(&thermal_list_lock);
674 list_for_each_entry(cdev, &thermal_cdev_list, node) {
675 if (!strncmp("Processor", cdev->type,
676 sizeof("Processor")))
677 thermal_zone_unbind_cooling_device(tz,
678 THERMAL_TRIPS_NONE,
679 cdev);
680 }
681 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100682 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000683 }
684
Matthew Garrett03a971a2008-12-03 18:00:38 +0000685 tz->forced_passive = state;
686
687 thermal_zone_device_update(tz);
688
689 return count;
690}
691
692static ssize_t
693passive_show(struct device *dev, struct device_attribute *attr,
694 char *buf)
695{
696 struct thermal_zone_device *tz = to_thermal_zone(dev);
697
698 return sprintf(buf, "%d\n", tz->forced_passive);
699}
700
Durgadoss R5a2c0902012-09-18 11:04:58 +0530701static ssize_t
702policy_store(struct device *dev, struct device_attribute *attr,
703 const char *buf, size_t count)
704{
705 int ret = -EINVAL;
706 struct thermal_zone_device *tz = to_thermal_zone(dev);
707 struct thermal_governor *gov;
708
709 mutex_lock(&thermal_governor_lock);
710
711 gov = __find_governor(buf);
712 if (!gov)
713 goto exit;
714
715 tz->governor = gov;
716 ret = count;
717
718exit:
719 mutex_unlock(&thermal_governor_lock);
720 return ret;
721}
722
723static ssize_t
724policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
725{
726 struct thermal_zone_device *tz = to_thermal_zone(dev);
727
728 return sprintf(buf, "%s\n", tz->governor->name);
729}
730
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000731#ifdef CONFIG_THERMAL_EMULATION
732static ssize_t
733emul_temp_store(struct device *dev, struct device_attribute *attr,
734 const char *buf, size_t count)
735{
736 struct thermal_zone_device *tz = to_thermal_zone(dev);
737 int ret = 0;
738 unsigned long temperature;
739
740 if (kstrtoul(buf, 10, &temperature))
741 return -EINVAL;
742
743 if (!tz->ops->set_emul_temp) {
744 mutex_lock(&tz->lock);
745 tz->emul_temperature = temperature;
746 mutex_unlock(&tz->lock);
747 } else {
748 ret = tz->ops->set_emul_temp(tz, temperature);
749 }
750
751 return ret ? ret : count;
752}
753static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
754#endif/*CONFIG_THERMAL_EMULATION*/
755
Zhang Rui203d3d42008-01-17 15:51:08 +0800756static DEVICE_ATTR(type, 0444, type_show, NULL);
757static DEVICE_ATTR(temp, 0444, temp_show, NULL);
758static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700759static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530760static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800761
Zhang Rui203d3d42008-01-17 15:51:08 +0800762/* sys I/F for cooling device */
763#define to_cooling_device(_dev) \
764 container_of(_dev, struct thermal_cooling_device, device)
765
766static ssize_t
767thermal_cooling_device_type_show(struct device *dev,
768 struct device_attribute *attr, char *buf)
769{
770 struct thermal_cooling_device *cdev = to_cooling_device(dev);
771
772 return sprintf(buf, "%s\n", cdev->type);
773}
774
775static ssize_t
776thermal_cooling_device_max_state_show(struct device *dev,
777 struct device_attribute *attr, char *buf)
778{
779 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000780 unsigned long state;
781 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800782
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000783 ret = cdev->ops->get_max_state(cdev, &state);
784 if (ret)
785 return ret;
786 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800787}
788
789static ssize_t
790thermal_cooling_device_cur_state_show(struct device *dev,
791 struct device_attribute *attr, char *buf)
792{
793 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000794 unsigned long state;
795 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800796
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000797 ret = cdev->ops->get_cur_state(cdev, &state);
798 if (ret)
799 return ret;
800 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800801}
802
803static ssize_t
804thermal_cooling_device_cur_state_store(struct device *dev,
805 struct device_attribute *attr,
806 const char *buf, size_t count)
807{
808 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000809 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800810 int result;
811
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000812 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800813 return -EINVAL;
814
Roel Kluinedb94912009-12-15 22:46:50 +0100815 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800816 return -EINVAL;
817
818 result = cdev->ops->set_cur_state(cdev, state);
819 if (result)
820 return result;
821 return count;
822}
823
824static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500825__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800826static DEVICE_ATTR(max_state, 0444,
827 thermal_cooling_device_max_state_show, NULL);
828static DEVICE_ATTR(cur_state, 0644,
829 thermal_cooling_device_cur_state_show,
830 thermal_cooling_device_cur_state_store);
831
832static ssize_t
833thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500834 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800835{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800836 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800837
838 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800839 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800840
841 if (instance->trip == THERMAL_TRIPS_NONE)
842 return sprintf(buf, "-1\n");
843 else
844 return sprintf(buf, "%d\n", instance->trip);
845}
846
847/* Device management */
848
Rene Herman16d75232008-06-24 19:38:56 +0200849#if defined(CONFIG_THERMAL_HWMON)
850
Zhang Ruie68b16a2008-04-21 16:07:52 +0800851/* hwmon sys I/F */
852#include <linux/hwmon.h>
Jean Delvare31f53962011-07-28 13:48:42 -0700853
854/* thermal zone devices with the same type share one hwmon device */
855struct thermal_hwmon_device {
856 char type[THERMAL_NAME_LENGTH];
857 struct device *device;
858 int count;
859 struct list_head tz_list;
860 struct list_head node;
861};
862
863struct thermal_hwmon_attr {
864 struct device_attribute attr;
865 char name[16];
866};
867
868/* one temperature input for each thermal zone */
869struct thermal_hwmon_temp {
870 struct list_head hwmon_node;
871 struct thermal_zone_device *tz;
872 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
873 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
874};
875
Zhang Ruie68b16a2008-04-21 16:07:52 +0800876static LIST_HEAD(thermal_hwmon_list);
877
878static ssize_t
879name_show(struct device *dev, struct device_attribute *attr, char *buf)
880{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700881 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800882 return sprintf(buf, "%s\n", hwmon->type);
883}
884static DEVICE_ATTR(name, 0444, name_show, NULL);
885
886static ssize_t
887temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
888{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000889 long temperature;
890 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800891 struct thermal_hwmon_attr *hwmon_attr
892 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700893 struct thermal_hwmon_temp *temp
894 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800895 temp_input);
Jean Delvare31f53962011-07-28 13:48:42 -0700896 struct thermal_zone_device *tz = temp->tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800897
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000898 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000899
900 if (ret)
901 return ret;
902
903 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800904}
905
906static ssize_t
907temp_crit_show(struct device *dev, struct device_attribute *attr,
908 char *buf)
909{
910 struct thermal_hwmon_attr *hwmon_attr
911 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700912 struct thermal_hwmon_temp *temp
913 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800914 temp_crit);
Jean Delvare31f53962011-07-28 13:48:42 -0700915 struct thermal_zone_device *tz = temp->tz;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000916 long temperature;
917 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800918
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000919 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
920 if (ret)
921 return ret;
922
923 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800924}
925
926
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700927static struct thermal_hwmon_device *
928thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
929{
930 struct thermal_hwmon_device *hwmon;
931
932 mutex_lock(&thermal_list_lock);
933 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
934 if (!strcmp(hwmon->type, tz->type)) {
935 mutex_unlock(&thermal_list_lock);
936 return hwmon;
937 }
938 mutex_unlock(&thermal_list_lock);
939
940 return NULL;
941}
942
Jean Delvare31f53962011-07-28 13:48:42 -0700943/* Find the temperature input matching a given thermal zone */
944static struct thermal_hwmon_temp *
945thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
946 const struct thermal_zone_device *tz)
947{
948 struct thermal_hwmon_temp *temp;
949
950 mutex_lock(&thermal_list_lock);
951 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
952 if (temp->tz == tz) {
953 mutex_unlock(&thermal_list_lock);
954 return temp;
955 }
956 mutex_unlock(&thermal_list_lock);
957
958 return NULL;
959}
960
Zhang Ruie68b16a2008-04-21 16:07:52 +0800961static int
962thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
963{
964 struct thermal_hwmon_device *hwmon;
Jean Delvare31f53962011-07-28 13:48:42 -0700965 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800966 int new_hwmon_device = 1;
967 int result;
968
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700969 hwmon = thermal_hwmon_lookup_by_type(tz);
970 if (hwmon) {
971 new_hwmon_device = 0;
972 goto register_sys_interface;
973 }
Zhang Ruie68b16a2008-04-21 16:07:52 +0800974
975 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
976 if (!hwmon)
977 return -ENOMEM;
978
979 INIT_LIST_HEAD(&hwmon->tz_list);
980 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
981 hwmon->device = hwmon_device_register(NULL);
982 if (IS_ERR(hwmon->device)) {
983 result = PTR_ERR(hwmon->device);
984 goto free_mem;
985 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700986 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800987 result = device_create_file(hwmon->device, &dev_attr_name);
988 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530989 goto free_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800990
991 register_sys_interface:
Jean Delvare31f53962011-07-28 13:48:42 -0700992 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
993 if (!temp) {
994 result = -ENOMEM;
995 goto unregister_name;
996 }
997
998 temp->tz = tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800999 hwmon->count++;
1000
Guenter Roeck79a49162012-07-21 10:53:48 +10001001 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +08001002 "temp%d_input", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -07001003 temp->temp_input.attr.attr.name = temp->temp_input.name;
1004 temp->temp_input.attr.attr.mode = 0444;
1005 temp->temp_input.attr.show = temp_input_show;
1006 sysfs_attr_init(&temp->temp_input.attr.attr);
1007 result = device_create_file(hwmon->device, &temp->temp_input.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001008 if (result)
Jean Delvare31f53962011-07-28 13:48:42 -07001009 goto free_temp_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001010
1011 if (tz->ops->get_crit_temp) {
1012 unsigned long temperature;
1013 if (!tz->ops->get_crit_temp(tz, &temperature)) {
Guenter Roeck79a49162012-07-21 10:53:48 +10001014 snprintf(temp->temp_crit.name,
1015 sizeof(temp->temp_crit.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +08001016 "temp%d_crit", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -07001017 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
1018 temp->temp_crit.attr.attr.mode = 0444;
1019 temp->temp_crit.attr.show = temp_crit_show;
1020 sysfs_attr_init(&temp->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001021 result = device_create_file(hwmon->device,
Jean Delvare31f53962011-07-28 13:48:42 -07001022 &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001023 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +05301024 goto unregister_input;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001025 }
1026 }
1027
1028 mutex_lock(&thermal_list_lock);
1029 if (new_hwmon_device)
1030 list_add_tail(&hwmon->node, &thermal_hwmon_list);
Jean Delvare31f53962011-07-28 13:48:42 -07001031 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001032 mutex_unlock(&thermal_list_lock);
1033
1034 return 0;
1035
Durgadoss Rb299eb52011-03-03 04:30:13 +05301036 unregister_input:
Jean Delvare31f53962011-07-28 13:48:42 -07001037 device_remove_file(hwmon->device, &temp->temp_input.attr);
1038 free_temp_mem:
1039 kfree(temp);
Durgadoss Rb299eb52011-03-03 04:30:13 +05301040 unregister_name:
Zhang Ruie68b16a2008-04-21 16:07:52 +08001041 if (new_hwmon_device) {
1042 device_remove_file(hwmon->device, &dev_attr_name);
1043 hwmon_device_unregister(hwmon->device);
1044 }
1045 free_mem:
1046 if (new_hwmon_device)
1047 kfree(hwmon);
1048
1049 return result;
1050}
1051
1052static void
1053thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1054{
Jean Delvare31f53962011-07-28 13:48:42 -07001055 struct thermal_hwmon_device *hwmon;
1056 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001057
Jean Delvare31f53962011-07-28 13:48:42 -07001058 hwmon = thermal_hwmon_lookup_by_type(tz);
1059 if (unlikely(!hwmon)) {
1060 /* Should never happen... */
1061 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
1062 return;
1063 }
1064
1065 temp = thermal_hwmon_lookup_temp(hwmon, tz);
1066 if (unlikely(!temp)) {
1067 /* Should never happen... */
1068 dev_dbg(&tz->device, "temperature input lookup failed!\n");
1069 return;
1070 }
1071
1072 device_remove_file(hwmon->device, &temp->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +05301073 if (tz->ops->get_crit_temp)
Jean Delvare31f53962011-07-28 13:48:42 -07001074 device_remove_file(hwmon->device, &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001075
1076 mutex_lock(&thermal_list_lock);
Jean Delvare31f53962011-07-28 13:48:42 -07001077 list_del(&temp->hwmon_node);
1078 kfree(temp);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001079 if (!list_empty(&hwmon->tz_list)) {
1080 mutex_unlock(&thermal_list_lock);
1081 return;
1082 }
1083 list_del(&hwmon->node);
1084 mutex_unlock(&thermal_list_lock);
1085
1086 device_remove_file(hwmon->device, &dev_attr_name);
1087 hwmon_device_unregister(hwmon->device);
1088 kfree(hwmon);
1089}
1090#else
1091static int
1092thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
1093{
1094 return 0;
1095}
1096
1097static void
1098thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1099{
1100}
1101#endif
1102
Zhang Rui203d3d42008-01-17 15:51:08 +08001103/**
1104 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +08001105 * @tz: thermal zone device
1106 * @trip: indicates which trip point the cooling devices is
1107 * associated with in this thermal zone.
1108 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -05001109 *
1110 * This function is usually called in the thermal zone device .bind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +08001111 */
1112int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1113 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +08001114 struct thermal_cooling_device *cdev,
1115 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +08001116{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001117 struct thermal_instance *dev;
1118 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -05001119 struct thermal_zone_device *pos1;
1120 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +08001121 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +08001122 int result;
1123
Len Brown543a9562008-02-07 16:55:08 -05001124 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +08001125 return -EINVAL;
1126
Thomas Sujithc7516702008-02-15 00:58:50 -05001127 list_for_each_entry(pos1, &thermal_tz_list, node) {
1128 if (pos1 == tz)
1129 break;
1130 }
1131 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1132 if (pos2 == cdev)
1133 break;
1134 }
1135
1136 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +08001137 return -EINVAL;
1138
Zhang Rui9d998422012-06-26 16:35:57 +08001139 cdev->ops->get_max_state(cdev, &max_state);
1140
1141 /* lower default 0, upper default max_state */
1142 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1143 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1144
1145 if (lower > upper || upper > max_state)
1146 return -EINVAL;
1147
Zhang Rui203d3d42008-01-17 15:51:08 +08001148 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001149 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001150 if (!dev)
1151 return -ENOMEM;
1152 dev->tz = tz;
1153 dev->cdev = cdev;
1154 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +08001155 dev->upper = upper;
1156 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +08001157 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +08001158
Zhang Rui203d3d42008-01-17 15:51:08 +08001159 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1160 if (result)
1161 goto free_mem;
1162
1163 sprintf(dev->name, "cdev%d", dev->id);
1164 result =
1165 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1166 if (result)
1167 goto release_idr;
1168
1169 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -07001170 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001171 dev->attr.attr.name = dev->attr_name;
1172 dev->attr.attr.mode = 0444;
1173 dev->attr.show = thermal_cooling_device_trip_point_show;
1174 result = device_create_file(&tz->device, &dev->attr);
1175 if (result)
1176 goto remove_symbol_link;
1177
1178 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001179 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001180 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +08001181 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1182 result = -EEXIST;
1183 break;
1184 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001185 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001186 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001187 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1188 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001189 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001190 mutex_unlock(&tz->lock);
1191
1192 if (!result)
1193 return 0;
1194
1195 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -07001196remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001197 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -07001198release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001199 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -07001200free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001201 kfree(dev);
1202 return result;
1203}
1204EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
1205
1206/**
1207 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +08001208 * @tz: thermal zone device
1209 * @trip: indicates which trip point the cooling devices is
1210 * associated with in this thermal zone.
1211 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -05001212 *
1213 * This function is usually called in the thermal zone device .unbind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +08001214 */
1215int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1216 int trip,
1217 struct thermal_cooling_device *cdev)
1218{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001219 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001220
1221 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001222 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001223 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001224 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001225 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001226 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001227 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001228 mutex_unlock(&tz->lock);
1229 goto unbind;
1230 }
1231 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001232 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001233 mutex_unlock(&tz->lock);
1234
1235 return -ENODEV;
1236
Joe Perchescaca8b82012-03-21 12:55:02 -07001237unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +08001238 device_remove_file(&tz->device, &pos->attr);
1239 sysfs_remove_link(&tz->device.kobj, pos->name);
1240 release_idr(&tz->idr, &tz->lock, pos->id);
1241 kfree(pos);
1242 return 0;
1243}
1244EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
1245
1246static void thermal_release(struct device *dev)
1247{
1248 struct thermal_zone_device *tz;
1249 struct thermal_cooling_device *cdev;
1250
Joe Perchescaca8b82012-03-21 12:55:02 -07001251 if (!strncmp(dev_name(dev), "thermal_zone",
1252 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001253 tz = to_thermal_zone(dev);
1254 kfree(tz);
1255 } else {
1256 cdev = to_cooling_device(dev);
1257 kfree(cdev);
1258 }
1259}
1260
1261static struct class thermal_class = {
1262 .name = "thermal",
1263 .dev_release = thermal_release,
1264};
1265
1266/**
1267 * thermal_cooling_device_register - register a new thermal cooling device
1268 * @type: the thermal cooling device type.
1269 * @devdata: device private data.
1270 * @ops: standard thermal cooling devices callbacks.
1271 */
Joe Perchescaca8b82012-03-21 12:55:02 -07001272struct thermal_cooling_device *
1273thermal_cooling_device_register(char *type, void *devdata,
1274 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001275{
1276 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001277 int result;
1278
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001279 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001280 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001281
1282 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001283 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001284 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001285
1286 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1287 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001288 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001289
1290 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1291 if (result) {
1292 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001293 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001294 }
1295
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001296 strcpy(cdev->type, type ? : "");
Zhang Ruif4a821c2012-07-24 16:56:21 +08001297 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001298 INIT_LIST_HEAD(&cdev->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001299 cdev->ops = ops;
Zhang Ruice119f82012-06-27 14:13:04 +08001300 cdev->updated = true;
Zhang Rui203d3d42008-01-17 15:51:08 +08001301 cdev->device.class = &thermal_class;
1302 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001303 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001304 result = device_register(&cdev->device);
1305 if (result) {
1306 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1307 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001308 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001309 }
1310
1311 /* sys I/F */
1312 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001313 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001314 if (result)
1315 goto unregister;
1316 }
1317
1318 result = device_create_file(&cdev->device, &dev_attr_max_state);
1319 if (result)
1320 goto unregister;
1321
1322 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1323 if (result)
1324 goto unregister;
1325
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301326 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001327 mutex_lock(&thermal_list_lock);
1328 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001329 mutex_unlock(&thermal_list_lock);
1330
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301331 /* Update binding information for 'this' new cdev */
1332 bind_cdev(cdev);
1333
1334 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001335
Joe Perchescaca8b82012-03-21 12:55:02 -07001336unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001337 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1338 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001339 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001340}
1341EXPORT_SYMBOL(thermal_cooling_device_register);
1342
1343/**
1344 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001345 * @cdev: the thermal cooling device to remove.
1346 *
1347 * thermal_cooling_device_unregister() must be called when the device is no
1348 * longer needed.
1349 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301350void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001351{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301352 int i;
1353 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001354 struct thermal_zone_device *tz;
1355 struct thermal_cooling_device *pos = NULL;
1356
1357 if (!cdev)
1358 return;
1359
1360 mutex_lock(&thermal_list_lock);
1361 list_for_each_entry(pos, &thermal_cdev_list, node)
1362 if (pos == cdev)
1363 break;
1364 if (pos != cdev) {
1365 /* thermal cooling device not found */
1366 mutex_unlock(&thermal_list_lock);
1367 return;
1368 }
1369 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301370
1371 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001372 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301373 if (tz->ops->unbind) {
1374 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001375 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301376 }
1377
1378 if (!tz->tzp || !tz->tzp->tbp)
1379 continue;
1380
1381 tzp = tz->tzp;
1382 for (i = 0; i < tzp->num_tbps; i++) {
1383 if (tzp->tbp[i].cdev == cdev) {
1384 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1385 tzp->tbp[i].cdev = NULL;
1386 }
1387 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001388 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301389
Zhang Rui203d3d42008-01-17 15:51:08 +08001390 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301391
Zhang Rui203d3d42008-01-17 15:51:08 +08001392 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001393 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001394 device_remove_file(&cdev->device, &dev_attr_max_state);
1395 device_remove_file(&cdev->device, &dev_attr_cur_state);
1396
1397 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1398 device_unregister(&cdev->device);
1399 return;
1400}
1401EXPORT_SYMBOL(thermal_cooling_device_unregister);
1402
Durgadoss Rdc765482012-09-18 11:05:00 +05301403void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001404{
1405 struct thermal_instance *instance;
1406 unsigned long target = 0;
1407
1408 /* cooling device is updated*/
1409 if (cdev->updated)
1410 return;
1411
Zhang Ruif4a821c2012-07-24 16:56:21 +08001412 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001413 /* Make sure cdev enters the deepest cooling state */
1414 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1415 if (instance->target == THERMAL_NO_TARGET)
1416 continue;
1417 if (instance->target > target)
1418 target = instance->target;
1419 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001420 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001421 cdev->ops->set_cur_state(cdev, target);
1422 cdev->updated = true;
1423}
Durgadoss Rdc765482012-09-18 11:05:00 +05301424EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001425
Matthew Garrettb1569e92008-12-03 17:55:32 +00001426/**
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301427 * notify_thermal_framework - Sensor drivers use this API to notify framework
1428 * @tz: thermal zone device
1429 * @trip: indicates which trip point has been crossed
1430 *
1431 * This function handles the trip events from sensor drivers. It starts
1432 * throttling the cooling devices according to the policy configured.
1433 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1434 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1435 * The throttling policy is based on the configured platform data; if no
1436 * platform data is provided, this uses the step_wise throttling policy.
1437 */
1438void notify_thermal_framework(struct thermal_zone_device *tz, int trip)
1439{
1440 handle_thermal_trip(tz, trip);
1441}
1442EXPORT_SYMBOL(notify_thermal_framework);
1443
1444/**
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001445 * create_trip_attrs - create attributes for trip points
1446 * @tz: the thermal zone device
1447 * @mask: Writeable trip point bitmap.
1448 */
1449static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1450{
1451 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001452 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001453
Durgadoss R27365a62012-07-25 10:10:59 +08001454 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001455 if (!tz->trip_type_attrs)
1456 return -ENOMEM;
1457
Durgadoss R27365a62012-07-25 10:10:59 +08001458 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001459 if (!tz->trip_temp_attrs) {
1460 kfree(tz->trip_type_attrs);
1461 return -ENOMEM;
1462 }
1463
Durgadoss R27365a62012-07-25 10:10:59 +08001464 if (tz->ops->get_trip_hyst) {
1465 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1466 if (!tz->trip_hyst_attrs) {
1467 kfree(tz->trip_type_attrs);
1468 kfree(tz->trip_temp_attrs);
1469 return -ENOMEM;
1470 }
1471 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001472
Durgadoss R27365a62012-07-25 10:10:59 +08001473
1474 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001475 /* create trip type attribute */
1476 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1477 "trip_point_%d_type", indx);
1478
1479 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1480 tz->trip_type_attrs[indx].attr.attr.name =
1481 tz->trip_type_attrs[indx].name;
1482 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1483 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1484
1485 device_create_file(&tz->device,
1486 &tz->trip_type_attrs[indx].attr);
1487
1488 /* create trip temp attribute */
1489 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1490 "trip_point_%d_temp", indx);
1491
1492 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1493 tz->trip_temp_attrs[indx].attr.attr.name =
1494 tz->trip_temp_attrs[indx].name;
1495 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1496 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1497 if (mask & (1 << indx)) {
1498 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1499 tz->trip_temp_attrs[indx].attr.store =
1500 trip_point_temp_store;
1501 }
1502
1503 device_create_file(&tz->device,
1504 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001505
1506 /* create Optional trip hyst attribute */
1507 if (!tz->ops->get_trip_hyst)
1508 continue;
1509 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1510 "trip_point_%d_hyst", indx);
1511
1512 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1513 tz->trip_hyst_attrs[indx].attr.attr.name =
1514 tz->trip_hyst_attrs[indx].name;
1515 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1516 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1517 if (tz->ops->set_trip_hyst) {
1518 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1519 tz->trip_hyst_attrs[indx].attr.store =
1520 trip_point_hyst_store;
1521 }
1522
1523 device_create_file(&tz->device,
1524 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001525 }
1526 return 0;
1527}
1528
1529static void remove_trip_attrs(struct thermal_zone_device *tz)
1530{
1531 int indx;
1532
1533 for (indx = 0; indx < tz->trips; indx++) {
1534 device_remove_file(&tz->device,
1535 &tz->trip_type_attrs[indx].attr);
1536 device_remove_file(&tz->device,
1537 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001538 if (tz->ops->get_trip_hyst)
1539 device_remove_file(&tz->device,
1540 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001541 }
1542 kfree(tz->trip_type_attrs);
1543 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001544 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001545}
1546
1547/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001548 * thermal_zone_device_register - register a new thermal zone device
1549 * @type: the thermal zone device type
1550 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001551 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001552 * @devdata: private device data
1553 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301554 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001555 * @passive_delay: number of milliseconds to wait between polls when
1556 * performing passive cooling
1557 * @polling_delay: number of milliseconds to wait between polls when checking
1558 * whether trip points have been crossed (0 for interrupt
1559 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001560 *
1561 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001562 * longer needed. The passive cooling depends on the .get_trend() return value.
Zhang Rui203d3d42008-01-17 15:51:08 +08001563 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001564struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001565 int trips, int mask, void *devdata,
Alan Cox5b275ce2010-11-11 15:27:29 +00001566 const struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301567 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001568 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001569{
1570 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001571 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001572 int result;
1573 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001574 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001575
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001576 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001577 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001578
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001579 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001580 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001581
1582 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001583 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001584
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001585 if (trips > 0 && !ops->get_trip_type)
1586 return ERR_PTR(-EINVAL);
1587
Zhang Rui203d3d42008-01-17 15:51:08 +08001588 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1589 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001590 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001591
Zhang Rui2d374132012-06-27 10:09:00 +08001592 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001593 idr_init(&tz->idr);
1594 mutex_init(&tz->lock);
1595 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1596 if (result) {
1597 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001598 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001599 }
1600
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001601 strcpy(tz->type, type ? : "");
Zhang Rui203d3d42008-01-17 15:51:08 +08001602 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301603 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001604 tz->device.class = &thermal_class;
1605 tz->devdata = devdata;
1606 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001607 tz->passive_delay = passive_delay;
1608 tz->polling_delay = polling_delay;
1609
Kay Sievers354655e2009-01-06 10:44:37 -08001610 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001611 result = device_register(&tz->device);
1612 if (result) {
1613 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1614 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001615 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001616 }
1617
1618 /* sys I/F */
1619 if (type) {
1620 result = device_create_file(&tz->device, &dev_attr_type);
1621 if (result)
1622 goto unregister;
1623 }
1624
1625 result = device_create_file(&tz->device, &dev_attr_temp);
1626 if (result)
1627 goto unregister;
1628
1629 if (ops->get_mode) {
1630 result = device_create_file(&tz->device, &dev_attr_mode);
1631 if (result)
1632 goto unregister;
1633 }
1634
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001635 result = create_trip_attrs(tz, mask);
1636 if (result)
1637 goto unregister;
1638
Zhang Rui203d3d42008-01-17 15:51:08 +08001639 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001640 tz->ops->get_trip_type(tz, count, &trip_type);
1641 if (trip_type == THERMAL_TRIP_PASSIVE)
1642 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001643 }
1644
Durgadoss R5a2c0902012-09-18 11:04:58 +05301645 if (!passive) {
1646 result = device_create_file(&tz->device, &dev_attr_passive);
1647 if (result)
1648 goto unregister;
1649 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001650
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001651#ifdef CONFIG_THERMAL_EMULATION
1652 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1653 if (result)
1654 goto unregister;
1655#endif
Durgadoss R5a2c0902012-09-18 11:04:58 +05301656 /* Create policy attribute */
1657 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001658 if (result)
1659 goto unregister;
1660
Durgadoss Ra4a15482012-09-18 11:04:57 +05301661 /* Update 'this' zone's governor information */
1662 mutex_lock(&thermal_governor_lock);
1663
1664 if (tz->tzp)
1665 tz->governor = __find_governor(tz->tzp->governor_name);
1666 else
1667 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1668
1669 mutex_unlock(&thermal_governor_lock);
1670
Zhang Ruie68b16a2008-04-21 16:07:52 +08001671 result = thermal_add_hwmon_sysfs(tz);
1672 if (result)
1673 goto unregister;
1674
Zhang Rui203d3d42008-01-17 15:51:08 +08001675 mutex_lock(&thermal_list_lock);
1676 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001677 mutex_unlock(&thermal_list_lock);
1678
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301679 /* Bind cooling devices for this zone */
1680 bind_tz(tz);
1681
Matthew Garrettb1569e92008-12-03 17:55:32 +00001682 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1683
1684 thermal_zone_device_update(tz);
1685
Zhang Rui203d3d42008-01-17 15:51:08 +08001686 if (!result)
1687 return tz;
1688
Joe Perchescaca8b82012-03-21 12:55:02 -07001689unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001690 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1691 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001692 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001693}
1694EXPORT_SYMBOL(thermal_zone_device_register);
1695
1696/**
1697 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001698 * @tz: the thermal zone device to remove
1699 */
1700void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1701{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301702 int i;
1703 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001704 struct thermal_cooling_device *cdev;
1705 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001706
1707 if (!tz)
1708 return;
1709
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301710 tzp = tz->tzp;
1711
Zhang Rui203d3d42008-01-17 15:51:08 +08001712 mutex_lock(&thermal_list_lock);
1713 list_for_each_entry(pos, &thermal_tz_list, node)
1714 if (pos == tz)
1715 break;
1716 if (pos != tz) {
1717 /* thermal zone device not found */
1718 mutex_unlock(&thermal_list_lock);
1719 return;
1720 }
1721 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301722
1723 /* Unbind all cdevs associated with 'this' thermal zone */
1724 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1725 if (tz->ops->unbind) {
1726 tz->ops->unbind(tz, cdev);
1727 continue;
1728 }
1729
1730 if (!tzp || !tzp->tbp)
1731 break;
1732
1733 for (i = 0; i < tzp->num_tbps; i++) {
1734 if (tzp->tbp[i].cdev == cdev) {
1735 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1736 tzp->tbp[i].cdev = NULL;
1737 }
1738 }
1739 }
1740
Zhang Rui203d3d42008-01-17 15:51:08 +08001741 mutex_unlock(&thermal_list_lock);
1742
Matthew Garrettb1569e92008-12-03 17:55:32 +00001743 thermal_zone_device_set_polling(tz, 0);
1744
Zhang Rui203d3d42008-01-17 15:51:08 +08001745 if (tz->type[0])
1746 device_remove_file(&tz->device, &dev_attr_type);
1747 device_remove_file(&tz->device, &dev_attr_temp);
1748 if (tz->ops->get_mode)
1749 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301750 device_remove_file(&tz->device, &dev_attr_policy);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001751 remove_trip_attrs(tz);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301752 tz->governor = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001753
Zhang Ruie68b16a2008-04-21 16:07:52 +08001754 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001755 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1756 idr_destroy(&tz->idr);
1757 mutex_destroy(&tz->lock);
1758 device_unregister(&tz->device);
1759 return;
1760}
Zhang Rui203d3d42008-01-17 15:51:08 +08001761EXPORT_SYMBOL(thermal_zone_device_unregister);
1762
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001763#ifdef CONFIG_NET
1764static struct genl_family thermal_event_genl_family = {
1765 .id = GENL_ID_GENERATE,
1766 .name = THERMAL_GENL_FAMILY_NAME,
1767 .version = THERMAL_GENL_VERSION,
1768 .maxattr = THERMAL_GENL_ATTR_MAX,
1769};
1770
1771static struct genl_multicast_group thermal_event_mcgrp = {
1772 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1773};
1774
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001775int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1776 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301777{
1778 struct sk_buff *skb;
1779 struct nlattr *attr;
1780 struct thermal_genl_event *thermal_event;
1781 void *msg_header;
1782 int size;
1783 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001784 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301785
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001786 if (!tz)
1787 return -EINVAL;
1788
R.Durgadoss4cb18722010-10-27 03:33:29 +05301789 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001790 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1791 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301792
1793 skb = genlmsg_new(size, GFP_ATOMIC);
1794 if (!skb)
1795 return -ENOMEM;
1796
1797 /* add the genetlink message header */
1798 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1799 &thermal_event_genl_family, 0,
1800 THERMAL_GENL_CMD_EVENT);
1801 if (!msg_header) {
1802 nlmsg_free(skb);
1803 return -ENOMEM;
1804 }
1805
1806 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001807 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1808 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301809
1810 if (!attr) {
1811 nlmsg_free(skb);
1812 return -EINVAL;
1813 }
1814
1815 thermal_event = nla_data(attr);
1816 if (!thermal_event) {
1817 nlmsg_free(skb);
1818 return -EINVAL;
1819 }
1820
1821 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1822
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001823 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301824 thermal_event->event = event;
1825
1826 /* send multicast genetlink message */
1827 result = genlmsg_end(skb, msg_header);
1828 if (result < 0) {
1829 nlmsg_free(skb);
1830 return result;
1831 }
1832
1833 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1834 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001835 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301836
1837 return result;
1838}
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001839EXPORT_SYMBOL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301840
1841static int genetlink_init(void)
1842{
1843 int result;
1844
1845 result = genl_register_family(&thermal_event_genl_family);
1846 if (result)
1847 return result;
1848
1849 result = genl_register_mc_group(&thermal_event_genl_family,
1850 &thermal_event_mcgrp);
1851 if (result)
1852 genl_unregister_family(&thermal_event_genl_family);
1853 return result;
1854}
1855
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001856static void genetlink_exit(void)
1857{
1858 genl_unregister_family(&thermal_event_genl_family);
1859}
1860#else /* !CONFIG_NET */
1861static inline int genetlink_init(void) { return 0; }
1862static inline void genetlink_exit(void) {}
1863#endif /* !CONFIG_NET */
1864
Zhang Rui203d3d42008-01-17 15:51:08 +08001865static int __init thermal_init(void)
1866{
1867 int result = 0;
1868
1869 result = class_register(&thermal_class);
1870 if (result) {
1871 idr_destroy(&thermal_tz_idr);
1872 idr_destroy(&thermal_cdev_idr);
1873 mutex_destroy(&thermal_idr_lock);
1874 mutex_destroy(&thermal_list_lock);
1875 }
R.Durgadoss4cb18722010-10-27 03:33:29 +05301876 result = genetlink_init();
Zhang Rui203d3d42008-01-17 15:51:08 +08001877 return result;
1878}
1879
1880static void __exit thermal_exit(void)
1881{
1882 class_unregister(&thermal_class);
1883 idr_destroy(&thermal_tz_idr);
1884 idr_destroy(&thermal_cdev_idr);
1885 mutex_destroy(&thermal_idr_lock);
1886 mutex_destroy(&thermal_list_lock);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301887 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001888}
1889
R.Durgadoss4cb18722010-10-27 03:33:29 +05301890fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001891module_exit(thermal_exit);