blob: 5b7863a03f98a59d1e3d7bf107a1f434f0082869 [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{
Tejun Heo6deb69f2013-02-27 17:04:46 -0800134 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800135
136 if (lock)
137 mutex_lock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800138 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800139 if (lock)
140 mutex_unlock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800141 if (unlikely(ret < 0))
142 return ret;
143 *id = ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800144 return 0;
145}
146
147static void release_idr(struct idr *idr, struct mutex *lock, int id)
148{
149 if (lock)
150 mutex_lock(lock);
151 idr_remove(idr, id);
152 if (lock)
153 mutex_unlock(lock);
154}
155
Durgadoss R9b4298a2012-09-18 11:04:54 +0530156int get_tz_trend(struct thermal_zone_device *tz, int trip)
157{
158 enum thermal_trend trend;
159
160 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
161 if (tz->temperature > tz->last_temperature)
162 trend = THERMAL_TREND_RAISING;
163 else if (tz->temperature < tz->last_temperature)
164 trend = THERMAL_TREND_DROPPING;
165 else
166 trend = THERMAL_TREND_STABLE;
167 }
168
169 return trend;
170}
171EXPORT_SYMBOL(get_tz_trend);
172
173struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
174 struct thermal_cooling_device *cdev, int trip)
175{
176 struct thermal_instance *pos = NULL;
177 struct thermal_instance *target_instance = NULL;
178
179 mutex_lock(&tz->lock);
180 mutex_lock(&cdev->lock);
181
182 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
183 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
184 target_instance = pos;
185 break;
186 }
187 }
188
189 mutex_unlock(&cdev->lock);
190 mutex_unlock(&tz->lock);
191
192 return target_instance;
193}
194EXPORT_SYMBOL(get_thermal_instance);
195
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530196static void print_bind_err_msg(struct thermal_zone_device *tz,
197 struct thermal_cooling_device *cdev, int ret)
198{
199 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
200 tz->type, cdev->type, ret);
201}
202
203static void __bind(struct thermal_zone_device *tz, int mask,
204 struct thermal_cooling_device *cdev)
205{
206 int i, ret;
207
208 for (i = 0; i < tz->trips; i++) {
209 if (mask & (1 << i)) {
210 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
211 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
212 if (ret)
213 print_bind_err_msg(tz, cdev, ret);
214 }
215 }
216}
217
218static void __unbind(struct thermal_zone_device *tz, int mask,
219 struct thermal_cooling_device *cdev)
220{
221 int i;
222
223 for (i = 0; i < tz->trips; i++)
224 if (mask & (1 << i))
225 thermal_zone_unbind_cooling_device(tz, i, cdev);
226}
227
228static void bind_cdev(struct thermal_cooling_device *cdev)
229{
230 int i, ret;
231 const struct thermal_zone_params *tzp;
232 struct thermal_zone_device *pos = NULL;
233
234 mutex_lock(&thermal_list_lock);
235
236 list_for_each_entry(pos, &thermal_tz_list, node) {
237 if (!pos->tzp && !pos->ops->bind)
238 continue;
239
240 if (!pos->tzp && pos->ops->bind) {
241 ret = pos->ops->bind(pos, cdev);
242 if (ret)
243 print_bind_err_msg(pos, cdev, ret);
244 }
245
246 tzp = pos->tzp;
Hugh Dickins791700c2012-09-27 16:48:28 -0700247 if (!tzp || !tzp->tbp)
248 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530249
250 for (i = 0; i < tzp->num_tbps; i++) {
251 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
252 continue;
253 if (tzp->tbp[i].match(pos, cdev))
254 continue;
255 tzp->tbp[i].cdev = cdev;
256 __bind(pos, tzp->tbp[i].trip_mask, cdev);
257 }
258 }
259
260 mutex_unlock(&thermal_list_lock);
261}
262
263static void bind_tz(struct thermal_zone_device *tz)
264{
265 int i, ret;
266 struct thermal_cooling_device *pos = NULL;
267 const struct thermal_zone_params *tzp = tz->tzp;
268
269 if (!tzp && !tz->ops->bind)
270 return;
271
272 mutex_lock(&thermal_list_lock);
273
274 /* If there is no platform data, try to use ops->bind */
275 if (!tzp && tz->ops->bind) {
276 list_for_each_entry(pos, &thermal_cdev_list, node) {
277 ret = tz->ops->bind(tz, pos);
278 if (ret)
279 print_bind_err_msg(tz, pos, ret);
280 }
281 goto exit;
282 }
283
Hugh Dickins791700c2012-09-27 16:48:28 -0700284 if (!tzp || !tzp->tbp)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530285 goto exit;
286
287 list_for_each_entry(pos, &thermal_cdev_list, node) {
288 for (i = 0; i < tzp->num_tbps; i++) {
289 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
290 continue;
291 if (tzp->tbp[i].match(tz, pos))
292 continue;
293 tzp->tbp[i].cdev = pos;
294 __bind(tz, tzp->tbp[i].trip_mask, pos);
295 }
296 }
297exit:
298 mutex_unlock(&thermal_list_lock);
299}
300
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530301static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
302 int delay)
303{
304 if (delay > 1000)
305 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
306 round_jiffies(msecs_to_jiffies(delay)));
307 else if (delay)
308 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
309 msecs_to_jiffies(delay));
310 else
311 cancel_delayed_work(&tz->poll_queue);
312}
313
314static void monitor_thermal_zone(struct thermal_zone_device *tz)
315{
316 mutex_lock(&tz->lock);
317
318 if (tz->passive)
319 thermal_zone_device_set_polling(tz, tz->passive_delay);
320 else if (tz->polling_delay)
321 thermal_zone_device_set_polling(tz, tz->polling_delay);
322 else
323 thermal_zone_device_set_polling(tz, 0);
324
325 mutex_unlock(&tz->lock);
326}
327
328static void handle_non_critical_trips(struct thermal_zone_device *tz,
329 int trip, enum thermal_trip_type trip_type)
330{
Zhang Ruid567c682012-12-12 15:23:54 +0800331 if (tz->governor)
332 tz->governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530333}
334
335static void handle_critical_trips(struct thermal_zone_device *tz,
336 int trip, enum thermal_trip_type trip_type)
337{
338 long trip_temp;
339
340 tz->ops->get_trip_temp(tz, trip, &trip_temp);
341
342 /* If we have not crossed the trip_temp, we do not care. */
343 if (tz->temperature < trip_temp)
344 return;
345
346 if (tz->ops->notify)
347 tz->ops->notify(tz, trip, trip_type);
348
349 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000350 dev_emerg(&tz->device,
351 "critical temperature reached(%d C),shutting down\n",
352 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530353 orderly_poweroff(true);
354 }
355}
356
357static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
358{
359 enum thermal_trip_type type;
360
361 tz->ops->get_trip_type(tz, trip, &type);
362
363 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
364 handle_critical_trips(tz, trip, type);
365 else
366 handle_non_critical_trips(tz, trip, type);
367 /*
368 * Alright, we handled this trip successfully.
369 * So, start monitoring again.
370 */
371 monitor_thermal_zone(tz);
372}
373
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000374static int thermal_zone_get_temp(struct thermal_zone_device *tz,
375 unsigned long *temp)
376{
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800377 int ret = 0;
378#ifdef CONFIG_THERMAL_EMULATION
379 int count;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000380 unsigned long crit_temp = -1UL;
381 enum thermal_trip_type type;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800382#endif
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000383
384 mutex_lock(&tz->lock);
385
386 ret = tz->ops->get_temp(tz, temp);
387#ifdef CONFIG_THERMAL_EMULATION
388 if (!tz->emul_temperature)
389 goto skip_emul;
390
391 for (count = 0; count < tz->trips; count++) {
392 ret = tz->ops->get_trip_type(tz, count, &type);
393 if (!ret && type == THERMAL_TRIP_CRITICAL) {
394 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
395 break;
396 }
397 }
398
399 if (ret)
400 goto skip_emul;
401
402 if (*temp < crit_temp)
403 *temp = tz->emul_temperature;
404skip_emul:
405#endif
406 mutex_unlock(&tz->lock);
407 return ret;
408}
409
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530410static void update_temperature(struct thermal_zone_device *tz)
411{
412 long temp;
413 int ret;
414
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000415 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530416 if (ret) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000417 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
418 tz->id);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000419 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530420 }
421
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000422 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530423 tz->last_temperature = tz->temperature;
424 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530425 mutex_unlock(&tz->lock);
426}
427
428void thermal_zone_device_update(struct thermal_zone_device *tz)
429{
430 int count;
431
432 update_temperature(tz);
433
434 for (count = 0; count < tz->trips; count++)
435 handle_thermal_trip(tz, count);
436}
437EXPORT_SYMBOL(thermal_zone_device_update);
438
439static void thermal_zone_device_check(struct work_struct *work)
440{
441 struct thermal_zone_device *tz = container_of(work, struct
442 thermal_zone_device,
443 poll_queue.work);
444 thermal_zone_device_update(tz);
445}
446
Zhang Rui203d3d42008-01-17 15:51:08 +0800447/* sys I/F for thermal zone */
448
449#define to_thermal_zone(_dev) \
450 container_of(_dev, struct thermal_zone_device, device)
451
452static ssize_t
453type_show(struct device *dev, struct device_attribute *attr, char *buf)
454{
455 struct thermal_zone_device *tz = to_thermal_zone(dev);
456
457 return sprintf(buf, "%s\n", tz->type);
458}
459
460static ssize_t
461temp_show(struct device *dev, struct device_attribute *attr, char *buf)
462{
463 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000464 long temperature;
465 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800466
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000467 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000468
469 if (ret)
470 return ret;
471
472 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800473}
474
475static ssize_t
476mode_show(struct device *dev, struct device_attribute *attr, char *buf)
477{
478 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000479 enum thermal_device_mode mode;
480 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800481
482 if (!tz->ops->get_mode)
483 return -EPERM;
484
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000485 result = tz->ops->get_mode(tz, &mode);
486 if (result)
487 return result;
488
489 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
490 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800491}
492
493static ssize_t
494mode_store(struct device *dev, struct device_attribute *attr,
495 const char *buf, size_t count)
496{
497 struct thermal_zone_device *tz = to_thermal_zone(dev);
498 int result;
499
500 if (!tz->ops->set_mode)
501 return -EPERM;
502
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530503 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000504 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530505 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000506 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
507 else
508 result = -EINVAL;
509
Zhang Rui203d3d42008-01-17 15:51:08 +0800510 if (result)
511 return result;
512
513 return count;
514}
515
516static ssize_t
517trip_point_type_show(struct device *dev, struct device_attribute *attr,
518 char *buf)
519{
520 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000521 enum thermal_trip_type type;
522 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800523
524 if (!tz->ops->get_trip_type)
525 return -EPERM;
526
527 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
528 return -EINVAL;
529
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000530 result = tz->ops->get_trip_type(tz, trip, &type);
531 if (result)
532 return result;
533
534 switch (type) {
535 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300536 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000537 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300538 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000539 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300540 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000541 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300542 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000543 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300544 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000545 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800546}
547
548static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800549trip_point_temp_store(struct device *dev, struct device_attribute *attr,
550 const char *buf, size_t count)
551{
552 struct thermal_zone_device *tz = to_thermal_zone(dev);
553 int trip, ret;
554 unsigned long temperature;
555
556 if (!tz->ops->set_trip_temp)
557 return -EPERM;
558
559 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
560 return -EINVAL;
561
562 if (kstrtoul(buf, 10, &temperature))
563 return -EINVAL;
564
565 ret = tz->ops->set_trip_temp(tz, trip, temperature);
566
567 return ret ? ret : count;
568}
569
570static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800571trip_point_temp_show(struct device *dev, struct device_attribute *attr,
572 char *buf)
573{
574 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000575 int trip, ret;
576 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800577
578 if (!tz->ops->get_trip_temp)
579 return -EPERM;
580
581 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
582 return -EINVAL;
583
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000584 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
585
586 if (ret)
587 return ret;
588
589 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800590}
591
Matthew Garrett03a971a2008-12-03 18:00:38 +0000592static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800593trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
594 const char *buf, size_t count)
595{
596 struct thermal_zone_device *tz = to_thermal_zone(dev);
597 int trip, ret;
598 unsigned long temperature;
599
600 if (!tz->ops->set_trip_hyst)
601 return -EPERM;
602
603 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
604 return -EINVAL;
605
606 if (kstrtoul(buf, 10, &temperature))
607 return -EINVAL;
608
609 /*
610 * We are not doing any check on the 'temperature' value
611 * here. The driver implementing 'set_trip_hyst' has to
612 * take care of this.
613 */
614 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
615
616 return ret ? ret : count;
617}
618
619static ssize_t
620trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
621 char *buf)
622{
623 struct thermal_zone_device *tz = to_thermal_zone(dev);
624 int trip, ret;
625 unsigned long temperature;
626
627 if (!tz->ops->get_trip_hyst)
628 return -EPERM;
629
630 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
631 return -EINVAL;
632
633 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
634
635 return ret ? ret : sprintf(buf, "%ld\n", temperature);
636}
637
638static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000639passive_store(struct device *dev, struct device_attribute *attr,
640 const char *buf, size_t count)
641{
642 struct thermal_zone_device *tz = to_thermal_zone(dev);
643 struct thermal_cooling_device *cdev = NULL;
644 int state;
645
646 if (!sscanf(buf, "%d\n", &state))
647 return -EINVAL;
648
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100649 /* sanity check: values below 1000 millicelcius don't make sense
650 * and can cause the system to go into a thermal heart attack
651 */
652 if (state && state < 1000)
653 return -EINVAL;
654
Matthew Garrett03a971a2008-12-03 18:00:38 +0000655 if (state && !tz->forced_passive) {
656 mutex_lock(&thermal_list_lock);
657 list_for_each_entry(cdev, &thermal_cdev_list, node) {
658 if (!strncmp("Processor", cdev->type,
659 sizeof("Processor")))
660 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800661 THERMAL_TRIPS_NONE, cdev,
662 THERMAL_NO_LIMIT,
663 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000664 }
665 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100666 if (!tz->passive_delay)
667 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000668 } else if (!state && tz->forced_passive) {
669 mutex_lock(&thermal_list_lock);
670 list_for_each_entry(cdev, &thermal_cdev_list, node) {
671 if (!strncmp("Processor", cdev->type,
672 sizeof("Processor")))
673 thermal_zone_unbind_cooling_device(tz,
674 THERMAL_TRIPS_NONE,
675 cdev);
676 }
677 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100678 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000679 }
680
Matthew Garrett03a971a2008-12-03 18:00:38 +0000681 tz->forced_passive = state;
682
683 thermal_zone_device_update(tz);
684
685 return count;
686}
687
688static ssize_t
689passive_show(struct device *dev, struct device_attribute *attr,
690 char *buf)
691{
692 struct thermal_zone_device *tz = to_thermal_zone(dev);
693
694 return sprintf(buf, "%d\n", tz->forced_passive);
695}
696
Durgadoss R5a2c0902012-09-18 11:04:58 +0530697static ssize_t
698policy_store(struct device *dev, struct device_attribute *attr,
699 const char *buf, size_t count)
700{
701 int ret = -EINVAL;
702 struct thermal_zone_device *tz = to_thermal_zone(dev);
703 struct thermal_governor *gov;
704
705 mutex_lock(&thermal_governor_lock);
706
707 gov = __find_governor(buf);
708 if (!gov)
709 goto exit;
710
711 tz->governor = gov;
712 ret = count;
713
714exit:
715 mutex_unlock(&thermal_governor_lock);
716 return ret;
717}
718
719static ssize_t
720policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
721{
722 struct thermal_zone_device *tz = to_thermal_zone(dev);
723
724 return sprintf(buf, "%s\n", tz->governor->name);
725}
726
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000727#ifdef CONFIG_THERMAL_EMULATION
728static ssize_t
729emul_temp_store(struct device *dev, struct device_attribute *attr,
730 const char *buf, size_t count)
731{
732 struct thermal_zone_device *tz = to_thermal_zone(dev);
733 int ret = 0;
734 unsigned long temperature;
735
736 if (kstrtoul(buf, 10, &temperature))
737 return -EINVAL;
738
739 if (!tz->ops->set_emul_temp) {
740 mutex_lock(&tz->lock);
741 tz->emul_temperature = temperature;
742 mutex_unlock(&tz->lock);
743 } else {
744 ret = tz->ops->set_emul_temp(tz, temperature);
745 }
746
747 return ret ? ret : count;
748}
749static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
750#endif/*CONFIG_THERMAL_EMULATION*/
751
Zhang Rui203d3d42008-01-17 15:51:08 +0800752static DEVICE_ATTR(type, 0444, type_show, NULL);
753static DEVICE_ATTR(temp, 0444, temp_show, NULL);
754static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700755static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530756static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800757
Zhang Rui203d3d42008-01-17 15:51:08 +0800758/* sys I/F for cooling device */
759#define to_cooling_device(_dev) \
760 container_of(_dev, struct thermal_cooling_device, device)
761
762static ssize_t
763thermal_cooling_device_type_show(struct device *dev,
764 struct device_attribute *attr, char *buf)
765{
766 struct thermal_cooling_device *cdev = to_cooling_device(dev);
767
768 return sprintf(buf, "%s\n", cdev->type);
769}
770
771static ssize_t
772thermal_cooling_device_max_state_show(struct device *dev,
773 struct device_attribute *attr, char *buf)
774{
775 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000776 unsigned long state;
777 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800778
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000779 ret = cdev->ops->get_max_state(cdev, &state);
780 if (ret)
781 return ret;
782 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800783}
784
785static ssize_t
786thermal_cooling_device_cur_state_show(struct device *dev,
787 struct device_attribute *attr, char *buf)
788{
789 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000790 unsigned long state;
791 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800792
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000793 ret = cdev->ops->get_cur_state(cdev, &state);
794 if (ret)
795 return ret;
796 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800797}
798
799static ssize_t
800thermal_cooling_device_cur_state_store(struct device *dev,
801 struct device_attribute *attr,
802 const char *buf, size_t count)
803{
804 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000805 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800806 int result;
807
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000808 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800809 return -EINVAL;
810
Roel Kluinedb94912009-12-15 22:46:50 +0100811 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800812 return -EINVAL;
813
814 result = cdev->ops->set_cur_state(cdev, state);
815 if (result)
816 return result;
817 return count;
818}
819
820static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500821__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800822static DEVICE_ATTR(max_state, 0444,
823 thermal_cooling_device_max_state_show, NULL);
824static DEVICE_ATTR(cur_state, 0644,
825 thermal_cooling_device_cur_state_show,
826 thermal_cooling_device_cur_state_store);
827
828static ssize_t
829thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500830 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800831{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800832 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800833
834 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800835 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800836
837 if (instance->trip == THERMAL_TRIPS_NONE)
838 return sprintf(buf, "-1\n");
839 else
840 return sprintf(buf, "%d\n", instance->trip);
841}
842
843/* Device management */
844
Rene Herman16d75232008-06-24 19:38:56 +0200845#if defined(CONFIG_THERMAL_HWMON)
846
Zhang Ruie68b16a2008-04-21 16:07:52 +0800847/* hwmon sys I/F */
848#include <linux/hwmon.h>
Jean Delvare31f53962011-07-28 13:48:42 -0700849
850/* thermal zone devices with the same type share one hwmon device */
851struct thermal_hwmon_device {
852 char type[THERMAL_NAME_LENGTH];
853 struct device *device;
854 int count;
855 struct list_head tz_list;
856 struct list_head node;
857};
858
859struct thermal_hwmon_attr {
860 struct device_attribute attr;
861 char name[16];
862};
863
864/* one temperature input for each thermal zone */
865struct thermal_hwmon_temp {
866 struct list_head hwmon_node;
867 struct thermal_zone_device *tz;
868 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
869 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
870};
871
Zhang Ruie68b16a2008-04-21 16:07:52 +0800872static LIST_HEAD(thermal_hwmon_list);
873
874static ssize_t
875name_show(struct device *dev, struct device_attribute *attr, char *buf)
876{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700877 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800878 return sprintf(buf, "%s\n", hwmon->type);
879}
880static DEVICE_ATTR(name, 0444, name_show, NULL);
881
882static ssize_t
883temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
884{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000885 long temperature;
886 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800887 struct thermal_hwmon_attr *hwmon_attr
888 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700889 struct thermal_hwmon_temp *temp
890 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800891 temp_input);
Jean Delvare31f53962011-07-28 13:48:42 -0700892 struct thermal_zone_device *tz = temp->tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800893
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000894 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000895
896 if (ret)
897 return ret;
898
899 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800900}
901
902static ssize_t
903temp_crit_show(struct device *dev, struct device_attribute *attr,
904 char *buf)
905{
906 struct thermal_hwmon_attr *hwmon_attr
907 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700908 struct thermal_hwmon_temp *temp
909 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800910 temp_crit);
Jean Delvare31f53962011-07-28 13:48:42 -0700911 struct thermal_zone_device *tz = temp->tz;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000912 long temperature;
913 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800914
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000915 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
916 if (ret)
917 return ret;
918
919 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800920}
921
922
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700923static struct thermal_hwmon_device *
924thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
925{
926 struct thermal_hwmon_device *hwmon;
927
928 mutex_lock(&thermal_list_lock);
929 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
930 if (!strcmp(hwmon->type, tz->type)) {
931 mutex_unlock(&thermal_list_lock);
932 return hwmon;
933 }
934 mutex_unlock(&thermal_list_lock);
935
936 return NULL;
937}
938
Jean Delvare31f53962011-07-28 13:48:42 -0700939/* Find the temperature input matching a given thermal zone */
940static struct thermal_hwmon_temp *
941thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
942 const struct thermal_zone_device *tz)
943{
944 struct thermal_hwmon_temp *temp;
945
946 mutex_lock(&thermal_list_lock);
947 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
948 if (temp->tz == tz) {
949 mutex_unlock(&thermal_list_lock);
950 return temp;
951 }
952 mutex_unlock(&thermal_list_lock);
953
954 return NULL;
955}
956
Zhang Ruie68b16a2008-04-21 16:07:52 +0800957static int
958thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
959{
960 struct thermal_hwmon_device *hwmon;
Jean Delvare31f53962011-07-28 13:48:42 -0700961 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800962 int new_hwmon_device = 1;
963 int result;
964
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700965 hwmon = thermal_hwmon_lookup_by_type(tz);
966 if (hwmon) {
967 new_hwmon_device = 0;
968 goto register_sys_interface;
969 }
Zhang Ruie68b16a2008-04-21 16:07:52 +0800970
971 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
972 if (!hwmon)
973 return -ENOMEM;
974
975 INIT_LIST_HEAD(&hwmon->tz_list);
976 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
977 hwmon->device = hwmon_device_register(NULL);
978 if (IS_ERR(hwmon->device)) {
979 result = PTR_ERR(hwmon->device);
980 goto free_mem;
981 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700982 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800983 result = device_create_file(hwmon->device, &dev_attr_name);
984 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530985 goto free_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800986
987 register_sys_interface:
Jean Delvare31f53962011-07-28 13:48:42 -0700988 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
989 if (!temp) {
990 result = -ENOMEM;
991 goto unregister_name;
992 }
993
994 temp->tz = tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800995 hwmon->count++;
996
Guenter Roeck79a49162012-07-21 10:53:48 +1000997 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +0800998 "temp%d_input", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700999 temp->temp_input.attr.attr.name = temp->temp_input.name;
1000 temp->temp_input.attr.attr.mode = 0444;
1001 temp->temp_input.attr.show = temp_input_show;
1002 sysfs_attr_init(&temp->temp_input.attr.attr);
1003 result = device_create_file(hwmon->device, &temp->temp_input.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001004 if (result)
Jean Delvare31f53962011-07-28 13:48:42 -07001005 goto free_temp_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001006
1007 if (tz->ops->get_crit_temp) {
1008 unsigned long temperature;
1009 if (!tz->ops->get_crit_temp(tz, &temperature)) {
Guenter Roeck79a49162012-07-21 10:53:48 +10001010 snprintf(temp->temp_crit.name,
1011 sizeof(temp->temp_crit.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +08001012 "temp%d_crit", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -07001013 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
1014 temp->temp_crit.attr.attr.mode = 0444;
1015 temp->temp_crit.attr.show = temp_crit_show;
1016 sysfs_attr_init(&temp->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001017 result = device_create_file(hwmon->device,
Jean Delvare31f53962011-07-28 13:48:42 -07001018 &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001019 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +05301020 goto unregister_input;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001021 }
1022 }
1023
1024 mutex_lock(&thermal_list_lock);
1025 if (new_hwmon_device)
1026 list_add_tail(&hwmon->node, &thermal_hwmon_list);
Jean Delvare31f53962011-07-28 13:48:42 -07001027 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001028 mutex_unlock(&thermal_list_lock);
1029
1030 return 0;
1031
Durgadoss Rb299eb52011-03-03 04:30:13 +05301032 unregister_input:
Jean Delvare31f53962011-07-28 13:48:42 -07001033 device_remove_file(hwmon->device, &temp->temp_input.attr);
1034 free_temp_mem:
1035 kfree(temp);
Durgadoss Rb299eb52011-03-03 04:30:13 +05301036 unregister_name:
Zhang Ruie68b16a2008-04-21 16:07:52 +08001037 if (new_hwmon_device) {
1038 device_remove_file(hwmon->device, &dev_attr_name);
1039 hwmon_device_unregister(hwmon->device);
1040 }
1041 free_mem:
1042 if (new_hwmon_device)
1043 kfree(hwmon);
1044
1045 return result;
1046}
1047
1048static void
1049thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1050{
Jean Delvare31f53962011-07-28 13:48:42 -07001051 struct thermal_hwmon_device *hwmon;
1052 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001053
Jean Delvare31f53962011-07-28 13:48:42 -07001054 hwmon = thermal_hwmon_lookup_by_type(tz);
1055 if (unlikely(!hwmon)) {
1056 /* Should never happen... */
1057 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
1058 return;
1059 }
1060
1061 temp = thermal_hwmon_lookup_temp(hwmon, tz);
1062 if (unlikely(!temp)) {
1063 /* Should never happen... */
1064 dev_dbg(&tz->device, "temperature input lookup failed!\n");
1065 return;
1066 }
1067
1068 device_remove_file(hwmon->device, &temp->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +05301069 if (tz->ops->get_crit_temp)
Jean Delvare31f53962011-07-28 13:48:42 -07001070 device_remove_file(hwmon->device, &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001071
1072 mutex_lock(&thermal_list_lock);
Jean Delvare31f53962011-07-28 13:48:42 -07001073 list_del(&temp->hwmon_node);
1074 kfree(temp);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001075 if (!list_empty(&hwmon->tz_list)) {
1076 mutex_unlock(&thermal_list_lock);
1077 return;
1078 }
1079 list_del(&hwmon->node);
1080 mutex_unlock(&thermal_list_lock);
1081
1082 device_remove_file(hwmon->device, &dev_attr_name);
1083 hwmon_device_unregister(hwmon->device);
1084 kfree(hwmon);
1085}
1086#else
1087static int
1088thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
1089{
1090 return 0;
1091}
1092
1093static void
1094thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1095{
1096}
1097#endif
1098
Zhang Rui203d3d42008-01-17 15:51:08 +08001099/**
1100 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +08001101 * @tz: thermal zone device
1102 * @trip: indicates which trip point the cooling devices is
1103 * associated with in this thermal zone.
1104 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -05001105 *
1106 * This function is usually called in the thermal zone device .bind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +08001107 */
1108int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1109 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +08001110 struct thermal_cooling_device *cdev,
1111 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +08001112{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001113 struct thermal_instance *dev;
1114 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -05001115 struct thermal_zone_device *pos1;
1116 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +08001117 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +08001118 int result;
1119
Len Brown543a9562008-02-07 16:55:08 -05001120 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +08001121 return -EINVAL;
1122
Thomas Sujithc7516702008-02-15 00:58:50 -05001123 list_for_each_entry(pos1, &thermal_tz_list, node) {
1124 if (pos1 == tz)
1125 break;
1126 }
1127 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1128 if (pos2 == cdev)
1129 break;
1130 }
1131
1132 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +08001133 return -EINVAL;
1134
Zhang Rui9d998422012-06-26 16:35:57 +08001135 cdev->ops->get_max_state(cdev, &max_state);
1136
1137 /* lower default 0, upper default max_state */
1138 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1139 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1140
1141 if (lower > upper || upper > max_state)
1142 return -EINVAL;
1143
Zhang Rui203d3d42008-01-17 15:51:08 +08001144 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001145 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001146 if (!dev)
1147 return -ENOMEM;
1148 dev->tz = tz;
1149 dev->cdev = cdev;
1150 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +08001151 dev->upper = upper;
1152 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +08001153 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +08001154
Zhang Rui203d3d42008-01-17 15:51:08 +08001155 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1156 if (result)
1157 goto free_mem;
1158
1159 sprintf(dev->name, "cdev%d", dev->id);
1160 result =
1161 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1162 if (result)
1163 goto release_idr;
1164
1165 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -07001166 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001167 dev->attr.attr.name = dev->attr_name;
1168 dev->attr.attr.mode = 0444;
1169 dev->attr.show = thermal_cooling_device_trip_point_show;
1170 result = device_create_file(&tz->device, &dev->attr);
1171 if (result)
1172 goto remove_symbol_link;
1173
1174 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001175 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001176 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +08001177 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1178 result = -EEXIST;
1179 break;
1180 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001181 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001182 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001183 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1184 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001185 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001186 mutex_unlock(&tz->lock);
1187
1188 if (!result)
1189 return 0;
1190
1191 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -07001192remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001193 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -07001194release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001195 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -07001196free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001197 kfree(dev);
1198 return result;
1199}
1200EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
1201
1202/**
1203 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +08001204 * @tz: thermal zone device
1205 * @trip: indicates which trip point the cooling devices is
1206 * associated with in this thermal zone.
1207 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -05001208 *
1209 * This function is usually called in the thermal zone device .unbind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +08001210 */
1211int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1212 int trip,
1213 struct thermal_cooling_device *cdev)
1214{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001215 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001216
1217 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001218 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001219 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001220 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001221 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001222 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001223 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001224 mutex_unlock(&tz->lock);
1225 goto unbind;
1226 }
1227 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001228 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001229 mutex_unlock(&tz->lock);
1230
1231 return -ENODEV;
1232
Joe Perchescaca8b82012-03-21 12:55:02 -07001233unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +08001234 device_remove_file(&tz->device, &pos->attr);
1235 sysfs_remove_link(&tz->device.kobj, pos->name);
1236 release_idr(&tz->idr, &tz->lock, pos->id);
1237 kfree(pos);
1238 return 0;
1239}
1240EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
1241
1242static void thermal_release(struct device *dev)
1243{
1244 struct thermal_zone_device *tz;
1245 struct thermal_cooling_device *cdev;
1246
Joe Perchescaca8b82012-03-21 12:55:02 -07001247 if (!strncmp(dev_name(dev), "thermal_zone",
1248 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001249 tz = to_thermal_zone(dev);
1250 kfree(tz);
1251 } else {
1252 cdev = to_cooling_device(dev);
1253 kfree(cdev);
1254 }
1255}
1256
1257static struct class thermal_class = {
1258 .name = "thermal",
1259 .dev_release = thermal_release,
1260};
1261
1262/**
1263 * thermal_cooling_device_register - register a new thermal cooling device
1264 * @type: the thermal cooling device type.
1265 * @devdata: device private data.
1266 * @ops: standard thermal cooling devices callbacks.
1267 */
Joe Perchescaca8b82012-03-21 12:55:02 -07001268struct thermal_cooling_device *
1269thermal_cooling_device_register(char *type, void *devdata,
1270 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001271{
1272 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001273 int result;
1274
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001275 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001276 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001277
1278 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001279 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001280 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001281
1282 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1283 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001284 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001285
1286 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1287 if (result) {
1288 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001289 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001290 }
1291
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001292 strcpy(cdev->type, type ? : "");
Zhang Ruif4a821c2012-07-24 16:56:21 +08001293 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001294 INIT_LIST_HEAD(&cdev->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001295 cdev->ops = ops;
Zhang Ruice119f82012-06-27 14:13:04 +08001296 cdev->updated = true;
Zhang Rui203d3d42008-01-17 15:51:08 +08001297 cdev->device.class = &thermal_class;
1298 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001299 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001300 result = device_register(&cdev->device);
1301 if (result) {
1302 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1303 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001304 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001305 }
1306
1307 /* sys I/F */
1308 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001309 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001310 if (result)
1311 goto unregister;
1312 }
1313
1314 result = device_create_file(&cdev->device, &dev_attr_max_state);
1315 if (result)
1316 goto unregister;
1317
1318 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1319 if (result)
1320 goto unregister;
1321
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301322 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001323 mutex_lock(&thermal_list_lock);
1324 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001325 mutex_unlock(&thermal_list_lock);
1326
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301327 /* Update binding information for 'this' new cdev */
1328 bind_cdev(cdev);
1329
1330 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001331
Joe Perchescaca8b82012-03-21 12:55:02 -07001332unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001333 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1334 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001335 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001336}
1337EXPORT_SYMBOL(thermal_cooling_device_register);
1338
1339/**
1340 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001341 * @cdev: the thermal cooling device to remove.
1342 *
1343 * thermal_cooling_device_unregister() must be called when the device is no
1344 * longer needed.
1345 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301346void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001347{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301348 int i;
1349 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001350 struct thermal_zone_device *tz;
1351 struct thermal_cooling_device *pos = NULL;
1352
1353 if (!cdev)
1354 return;
1355
1356 mutex_lock(&thermal_list_lock);
1357 list_for_each_entry(pos, &thermal_cdev_list, node)
1358 if (pos == cdev)
1359 break;
1360 if (pos != cdev) {
1361 /* thermal cooling device not found */
1362 mutex_unlock(&thermal_list_lock);
1363 return;
1364 }
1365 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301366
1367 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001368 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301369 if (tz->ops->unbind) {
1370 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001371 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301372 }
1373
1374 if (!tz->tzp || !tz->tzp->tbp)
1375 continue;
1376
1377 tzp = tz->tzp;
1378 for (i = 0; i < tzp->num_tbps; i++) {
1379 if (tzp->tbp[i].cdev == cdev) {
1380 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1381 tzp->tbp[i].cdev = NULL;
1382 }
1383 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001384 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301385
Zhang Rui203d3d42008-01-17 15:51:08 +08001386 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301387
Zhang Rui203d3d42008-01-17 15:51:08 +08001388 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001389 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001390 device_remove_file(&cdev->device, &dev_attr_max_state);
1391 device_remove_file(&cdev->device, &dev_attr_cur_state);
1392
1393 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1394 device_unregister(&cdev->device);
1395 return;
1396}
1397EXPORT_SYMBOL(thermal_cooling_device_unregister);
1398
Durgadoss Rdc765482012-09-18 11:05:00 +05301399void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001400{
1401 struct thermal_instance *instance;
1402 unsigned long target = 0;
1403
1404 /* cooling device is updated*/
1405 if (cdev->updated)
1406 return;
1407
Zhang Ruif4a821c2012-07-24 16:56:21 +08001408 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001409 /* Make sure cdev enters the deepest cooling state */
1410 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1411 if (instance->target == THERMAL_NO_TARGET)
1412 continue;
1413 if (instance->target > target)
1414 target = instance->target;
1415 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001416 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001417 cdev->ops->set_cur_state(cdev, target);
1418 cdev->updated = true;
1419}
Durgadoss Rdc765482012-09-18 11:05:00 +05301420EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001421
Matthew Garrettb1569e92008-12-03 17:55:32 +00001422/**
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301423 * notify_thermal_framework - Sensor drivers use this API to notify framework
1424 * @tz: thermal zone device
1425 * @trip: indicates which trip point has been crossed
1426 *
1427 * This function handles the trip events from sensor drivers. It starts
1428 * throttling the cooling devices according to the policy configured.
1429 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1430 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1431 * The throttling policy is based on the configured platform data; if no
1432 * platform data is provided, this uses the step_wise throttling policy.
1433 */
1434void notify_thermal_framework(struct thermal_zone_device *tz, int trip)
1435{
1436 handle_thermal_trip(tz, trip);
1437}
1438EXPORT_SYMBOL(notify_thermal_framework);
1439
1440/**
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001441 * create_trip_attrs - create attributes for trip points
1442 * @tz: the thermal zone device
1443 * @mask: Writeable trip point bitmap.
1444 */
1445static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1446{
1447 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001448 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001449
Durgadoss R27365a62012-07-25 10:10:59 +08001450 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001451 if (!tz->trip_type_attrs)
1452 return -ENOMEM;
1453
Durgadoss R27365a62012-07-25 10:10:59 +08001454 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001455 if (!tz->trip_temp_attrs) {
1456 kfree(tz->trip_type_attrs);
1457 return -ENOMEM;
1458 }
1459
Durgadoss R27365a62012-07-25 10:10:59 +08001460 if (tz->ops->get_trip_hyst) {
1461 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1462 if (!tz->trip_hyst_attrs) {
1463 kfree(tz->trip_type_attrs);
1464 kfree(tz->trip_temp_attrs);
1465 return -ENOMEM;
1466 }
1467 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001468
Durgadoss R27365a62012-07-25 10:10:59 +08001469
1470 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001471 /* create trip type attribute */
1472 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1473 "trip_point_%d_type", indx);
1474
1475 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1476 tz->trip_type_attrs[indx].attr.attr.name =
1477 tz->trip_type_attrs[indx].name;
1478 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1479 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1480
1481 device_create_file(&tz->device,
1482 &tz->trip_type_attrs[indx].attr);
1483
1484 /* create trip temp attribute */
1485 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1486 "trip_point_%d_temp", indx);
1487
1488 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1489 tz->trip_temp_attrs[indx].attr.attr.name =
1490 tz->trip_temp_attrs[indx].name;
1491 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1492 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1493 if (mask & (1 << indx)) {
1494 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1495 tz->trip_temp_attrs[indx].attr.store =
1496 trip_point_temp_store;
1497 }
1498
1499 device_create_file(&tz->device,
1500 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001501
1502 /* create Optional trip hyst attribute */
1503 if (!tz->ops->get_trip_hyst)
1504 continue;
1505 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1506 "trip_point_%d_hyst", indx);
1507
1508 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1509 tz->trip_hyst_attrs[indx].attr.attr.name =
1510 tz->trip_hyst_attrs[indx].name;
1511 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1512 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1513 if (tz->ops->set_trip_hyst) {
1514 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1515 tz->trip_hyst_attrs[indx].attr.store =
1516 trip_point_hyst_store;
1517 }
1518
1519 device_create_file(&tz->device,
1520 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001521 }
1522 return 0;
1523}
1524
1525static void remove_trip_attrs(struct thermal_zone_device *tz)
1526{
1527 int indx;
1528
1529 for (indx = 0; indx < tz->trips; indx++) {
1530 device_remove_file(&tz->device,
1531 &tz->trip_type_attrs[indx].attr);
1532 device_remove_file(&tz->device,
1533 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001534 if (tz->ops->get_trip_hyst)
1535 device_remove_file(&tz->device,
1536 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001537 }
1538 kfree(tz->trip_type_attrs);
1539 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001540 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001541}
1542
1543/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001544 * thermal_zone_device_register - register a new thermal zone device
1545 * @type: the thermal zone device type
1546 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001547 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001548 * @devdata: private device data
1549 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301550 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001551 * @passive_delay: number of milliseconds to wait between polls when
1552 * performing passive cooling
1553 * @polling_delay: number of milliseconds to wait between polls when checking
1554 * whether trip points have been crossed (0 for interrupt
1555 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001556 *
1557 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001558 * longer needed. The passive cooling depends on the .get_trend() return value.
Zhang Rui203d3d42008-01-17 15:51:08 +08001559 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001560struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001561 int trips, int mask, void *devdata,
Alan Cox5b275ce2010-11-11 15:27:29 +00001562 const struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301563 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001564 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001565{
1566 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001567 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001568 int result;
1569 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001570 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001571
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001572 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001573 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001574
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001575 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001576 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001577
1578 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001579 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001580
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001581 if (trips > 0 && !ops->get_trip_type)
1582 return ERR_PTR(-EINVAL);
1583
Zhang Rui203d3d42008-01-17 15:51:08 +08001584 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1585 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001586 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001587
Zhang Rui2d374132012-06-27 10:09:00 +08001588 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001589 idr_init(&tz->idr);
1590 mutex_init(&tz->lock);
1591 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1592 if (result) {
1593 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001594 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001595 }
1596
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001597 strcpy(tz->type, type ? : "");
Zhang Rui203d3d42008-01-17 15:51:08 +08001598 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301599 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001600 tz->device.class = &thermal_class;
1601 tz->devdata = devdata;
1602 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001603 tz->passive_delay = passive_delay;
1604 tz->polling_delay = polling_delay;
1605
Kay Sievers354655e2009-01-06 10:44:37 -08001606 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001607 result = device_register(&tz->device);
1608 if (result) {
1609 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1610 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001611 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001612 }
1613
1614 /* sys I/F */
1615 if (type) {
1616 result = device_create_file(&tz->device, &dev_attr_type);
1617 if (result)
1618 goto unregister;
1619 }
1620
1621 result = device_create_file(&tz->device, &dev_attr_temp);
1622 if (result)
1623 goto unregister;
1624
1625 if (ops->get_mode) {
1626 result = device_create_file(&tz->device, &dev_attr_mode);
1627 if (result)
1628 goto unregister;
1629 }
1630
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001631 result = create_trip_attrs(tz, mask);
1632 if (result)
1633 goto unregister;
1634
Zhang Rui203d3d42008-01-17 15:51:08 +08001635 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001636 tz->ops->get_trip_type(tz, count, &trip_type);
1637 if (trip_type == THERMAL_TRIP_PASSIVE)
1638 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001639 }
1640
Durgadoss R5a2c0902012-09-18 11:04:58 +05301641 if (!passive) {
1642 result = device_create_file(&tz->device, &dev_attr_passive);
1643 if (result)
1644 goto unregister;
1645 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001646
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001647#ifdef CONFIG_THERMAL_EMULATION
1648 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1649 if (result)
1650 goto unregister;
1651#endif
Durgadoss R5a2c0902012-09-18 11:04:58 +05301652 /* Create policy attribute */
1653 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001654 if (result)
1655 goto unregister;
1656
Durgadoss Ra4a15482012-09-18 11:04:57 +05301657 /* Update 'this' zone's governor information */
1658 mutex_lock(&thermal_governor_lock);
1659
1660 if (tz->tzp)
1661 tz->governor = __find_governor(tz->tzp->governor_name);
1662 else
1663 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1664
1665 mutex_unlock(&thermal_governor_lock);
1666
Zhang Ruie68b16a2008-04-21 16:07:52 +08001667 result = thermal_add_hwmon_sysfs(tz);
1668 if (result)
1669 goto unregister;
1670
Zhang Rui203d3d42008-01-17 15:51:08 +08001671 mutex_lock(&thermal_list_lock);
1672 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001673 mutex_unlock(&thermal_list_lock);
1674
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301675 /* Bind cooling devices for this zone */
1676 bind_tz(tz);
1677
Matthew Garrettb1569e92008-12-03 17:55:32 +00001678 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1679
1680 thermal_zone_device_update(tz);
1681
Zhang Rui203d3d42008-01-17 15:51:08 +08001682 if (!result)
1683 return tz;
1684
Joe Perchescaca8b82012-03-21 12:55:02 -07001685unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001686 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1687 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001688 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001689}
1690EXPORT_SYMBOL(thermal_zone_device_register);
1691
1692/**
1693 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001694 * @tz: the thermal zone device to remove
1695 */
1696void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1697{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301698 int i;
1699 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001700 struct thermal_cooling_device *cdev;
1701 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001702
1703 if (!tz)
1704 return;
1705
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301706 tzp = tz->tzp;
1707
Zhang Rui203d3d42008-01-17 15:51:08 +08001708 mutex_lock(&thermal_list_lock);
1709 list_for_each_entry(pos, &thermal_tz_list, node)
1710 if (pos == tz)
1711 break;
1712 if (pos != tz) {
1713 /* thermal zone device not found */
1714 mutex_unlock(&thermal_list_lock);
1715 return;
1716 }
1717 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301718
1719 /* Unbind all cdevs associated with 'this' thermal zone */
1720 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1721 if (tz->ops->unbind) {
1722 tz->ops->unbind(tz, cdev);
1723 continue;
1724 }
1725
1726 if (!tzp || !tzp->tbp)
1727 break;
1728
1729 for (i = 0; i < tzp->num_tbps; i++) {
1730 if (tzp->tbp[i].cdev == cdev) {
1731 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1732 tzp->tbp[i].cdev = NULL;
1733 }
1734 }
1735 }
1736
Zhang Rui203d3d42008-01-17 15:51:08 +08001737 mutex_unlock(&thermal_list_lock);
1738
Matthew Garrettb1569e92008-12-03 17:55:32 +00001739 thermal_zone_device_set_polling(tz, 0);
1740
Zhang Rui203d3d42008-01-17 15:51:08 +08001741 if (tz->type[0])
1742 device_remove_file(&tz->device, &dev_attr_type);
1743 device_remove_file(&tz->device, &dev_attr_temp);
1744 if (tz->ops->get_mode)
1745 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301746 device_remove_file(&tz->device, &dev_attr_policy);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001747 remove_trip_attrs(tz);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301748 tz->governor = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001749
Zhang Ruie68b16a2008-04-21 16:07:52 +08001750 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001751 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1752 idr_destroy(&tz->idr);
1753 mutex_destroy(&tz->lock);
1754 device_unregister(&tz->device);
1755 return;
1756}
Zhang Rui203d3d42008-01-17 15:51:08 +08001757EXPORT_SYMBOL(thermal_zone_device_unregister);
1758
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001759#ifdef CONFIG_NET
1760static struct genl_family thermal_event_genl_family = {
1761 .id = GENL_ID_GENERATE,
1762 .name = THERMAL_GENL_FAMILY_NAME,
1763 .version = THERMAL_GENL_VERSION,
1764 .maxattr = THERMAL_GENL_ATTR_MAX,
1765};
1766
1767static struct genl_multicast_group thermal_event_mcgrp = {
1768 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1769};
1770
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001771int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1772 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301773{
1774 struct sk_buff *skb;
1775 struct nlattr *attr;
1776 struct thermal_genl_event *thermal_event;
1777 void *msg_header;
1778 int size;
1779 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001780 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301781
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001782 if (!tz)
1783 return -EINVAL;
1784
R.Durgadoss4cb18722010-10-27 03:33:29 +05301785 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001786 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1787 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301788
1789 skb = genlmsg_new(size, GFP_ATOMIC);
1790 if (!skb)
1791 return -ENOMEM;
1792
1793 /* add the genetlink message header */
1794 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1795 &thermal_event_genl_family, 0,
1796 THERMAL_GENL_CMD_EVENT);
1797 if (!msg_header) {
1798 nlmsg_free(skb);
1799 return -ENOMEM;
1800 }
1801
1802 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001803 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1804 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301805
1806 if (!attr) {
1807 nlmsg_free(skb);
1808 return -EINVAL;
1809 }
1810
1811 thermal_event = nla_data(attr);
1812 if (!thermal_event) {
1813 nlmsg_free(skb);
1814 return -EINVAL;
1815 }
1816
1817 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1818
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001819 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301820 thermal_event->event = event;
1821
1822 /* send multicast genetlink message */
1823 result = genlmsg_end(skb, msg_header);
1824 if (result < 0) {
1825 nlmsg_free(skb);
1826 return result;
1827 }
1828
1829 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1830 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001831 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301832
1833 return result;
1834}
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001835EXPORT_SYMBOL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301836
1837static int genetlink_init(void)
1838{
1839 int result;
1840
1841 result = genl_register_family(&thermal_event_genl_family);
1842 if (result)
1843 return result;
1844
1845 result = genl_register_mc_group(&thermal_event_genl_family,
1846 &thermal_event_mcgrp);
1847 if (result)
1848 genl_unregister_family(&thermal_event_genl_family);
1849 return result;
1850}
1851
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001852static void genetlink_exit(void)
1853{
1854 genl_unregister_family(&thermal_event_genl_family);
1855}
1856#else /* !CONFIG_NET */
1857static inline int genetlink_init(void) { return 0; }
1858static inline void genetlink_exit(void) {}
1859#endif /* !CONFIG_NET */
1860
Zhang Rui203d3d42008-01-17 15:51:08 +08001861static int __init thermal_init(void)
1862{
1863 int result = 0;
1864
1865 result = class_register(&thermal_class);
1866 if (result) {
1867 idr_destroy(&thermal_tz_idr);
1868 idr_destroy(&thermal_cdev_idr);
1869 mutex_destroy(&thermal_idr_lock);
1870 mutex_destroy(&thermal_list_lock);
Richard Guy Briggsda28d962013-02-12 19:39:44 +00001871 return result;
Zhang Rui203d3d42008-01-17 15:51:08 +08001872 }
R.Durgadoss4cb18722010-10-27 03:33:29 +05301873 result = genetlink_init();
Zhang Rui203d3d42008-01-17 15:51:08 +08001874 return result;
1875}
1876
1877static void __exit thermal_exit(void)
1878{
1879 class_unregister(&thermal_class);
1880 idr_destroy(&thermal_tz_idr);
1881 idr_destroy(&thermal_cdev_idr);
1882 mutex_destroy(&thermal_idr_lock);
1883 mutex_destroy(&thermal_list_lock);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301884 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001885}
1886
R.Durgadoss4cb18722010-10-27 03:33:29 +05301887fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001888module_exit(thermal_exit);