blob: 8c8ce806180fc1d56988273175ac899741449cad [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>
35#include <linux/spinlock.h>
Matthew Garrettb1569e92008-12-03 17:55:32 +000036#include <linux/reboot.h>
R.Durgadoss4cb18722010-10-27 03:33:29 +053037#include <net/netlink.h>
38#include <net/genetlink.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080039
Durgadoss R71350db2012-09-18 11:04:53 +053040#include "thermal_core.h"
41
Zhang Rui63c4ec92008-04-21 16:07:13 +080042MODULE_AUTHOR("Zhang Rui");
Zhang Rui203d3d42008-01-17 15:51:08 +080043MODULE_DESCRIPTION("Generic thermal management sysfs support");
44MODULE_LICENSE("GPL");
45
Zhang Rui203d3d42008-01-17 15:51:08 +080046static DEFINE_IDR(thermal_tz_idr);
47static DEFINE_IDR(thermal_cdev_idr);
48static DEFINE_MUTEX(thermal_idr_lock);
49
50static LIST_HEAD(thermal_tz_list);
51static LIST_HEAD(thermal_cdev_list);
Durgadoss Ra4a15482012-09-18 11:04:57 +053052static LIST_HEAD(thermal_governor_list);
53
Zhang Rui203d3d42008-01-17 15:51:08 +080054static DEFINE_MUTEX(thermal_list_lock);
Durgadoss Ra4a15482012-09-18 11:04:57 +053055static DEFINE_MUTEX(thermal_governor_lock);
56
57static struct thermal_governor *__find_governor(const char *name)
58{
59 struct thermal_governor *pos;
60
61 list_for_each_entry(pos, &thermal_governor_list, governor_list)
62 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
63 return pos;
64
65 return NULL;
66}
67
68int thermal_register_governor(struct thermal_governor *governor)
69{
70 int err;
71 const char *name;
72 struct thermal_zone_device *pos;
73
74 if (!governor)
75 return -EINVAL;
76
77 mutex_lock(&thermal_governor_lock);
78
79 err = -EBUSY;
80 if (__find_governor(governor->name) == NULL) {
81 err = 0;
82 list_add(&governor->governor_list, &thermal_governor_list);
83 }
84
85 mutex_lock(&thermal_list_lock);
86
87 list_for_each_entry(pos, &thermal_tz_list, node) {
88 if (pos->governor)
89 continue;
90 if (pos->tzp)
91 name = pos->tzp->governor_name;
92 else
93 name = DEFAULT_THERMAL_GOVERNOR;
94 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
95 pos->governor = governor;
96 }
97
98 mutex_unlock(&thermal_list_lock);
99 mutex_unlock(&thermal_governor_lock);
100
101 return err;
102}
103EXPORT_SYMBOL_GPL(thermal_register_governor);
104
105void thermal_unregister_governor(struct thermal_governor *governor)
106{
107 struct thermal_zone_device *pos;
108
109 if (!governor)
110 return;
111
112 mutex_lock(&thermal_governor_lock);
113
114 if (__find_governor(governor->name) == NULL)
115 goto exit;
116
117 mutex_lock(&thermal_list_lock);
118
119 list_for_each_entry(pos, &thermal_tz_list, node) {
120 if (!strnicmp(pos->governor->name, governor->name,
121 THERMAL_NAME_LENGTH))
122 pos->governor = NULL;
123 }
124
125 mutex_unlock(&thermal_list_lock);
126 list_del(&governor->governor_list);
127exit:
128 mutex_unlock(&thermal_governor_lock);
129 return;
130}
131EXPORT_SYMBOL_GPL(thermal_unregister_governor);
Zhang Rui203d3d42008-01-17 15:51:08 +0800132
133static int get_idr(struct idr *idr, struct mutex *lock, int *id)
134{
135 int err;
136
Joe Perchescaca8b802012-03-21 12:55:02 -0700137again:
Zhang Rui203d3d42008-01-17 15:51:08 +0800138 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
139 return -ENOMEM;
140
141 if (lock)
142 mutex_lock(lock);
143 err = idr_get_new(idr, NULL, id);
144 if (lock)
145 mutex_unlock(lock);
146 if (unlikely(err == -EAGAIN))
147 goto again;
148 else if (unlikely(err))
149 return err;
150
Fengguang Wu125c4c72012-10-04 17:13:15 -0700151 *id = *id & MAX_IDR_MASK;
Zhang Rui203d3d42008-01-17 15:51:08 +0800152 return 0;
153}
154
155static void release_idr(struct idr *idr, struct mutex *lock, int id)
156{
157 if (lock)
158 mutex_lock(lock);
159 idr_remove(idr, id);
160 if (lock)
161 mutex_unlock(lock);
162}
163
Durgadoss R9b4298a2012-09-18 11:04:54 +0530164int get_tz_trend(struct thermal_zone_device *tz, int trip)
165{
166 enum thermal_trend trend;
167
168 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
169 if (tz->temperature > tz->last_temperature)
170 trend = THERMAL_TREND_RAISING;
171 else if (tz->temperature < tz->last_temperature)
172 trend = THERMAL_TREND_DROPPING;
173 else
174 trend = THERMAL_TREND_STABLE;
175 }
176
177 return trend;
178}
179EXPORT_SYMBOL(get_tz_trend);
180
181struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
182 struct thermal_cooling_device *cdev, int trip)
183{
184 struct thermal_instance *pos = NULL;
185 struct thermal_instance *target_instance = NULL;
186
187 mutex_lock(&tz->lock);
188 mutex_lock(&cdev->lock);
189
190 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
191 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
192 target_instance = pos;
193 break;
194 }
195 }
196
197 mutex_unlock(&cdev->lock);
198 mutex_unlock(&tz->lock);
199
200 return target_instance;
201}
202EXPORT_SYMBOL(get_thermal_instance);
203
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530204static void print_bind_err_msg(struct thermal_zone_device *tz,
205 struct thermal_cooling_device *cdev, int ret)
206{
207 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
208 tz->type, cdev->type, ret);
209}
210
211static void __bind(struct thermal_zone_device *tz, int mask,
212 struct thermal_cooling_device *cdev)
213{
214 int i, ret;
215
216 for (i = 0; i < tz->trips; i++) {
217 if (mask & (1 << i)) {
218 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
219 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
220 if (ret)
221 print_bind_err_msg(tz, cdev, ret);
222 }
223 }
224}
225
226static void __unbind(struct thermal_zone_device *tz, int mask,
227 struct thermal_cooling_device *cdev)
228{
229 int i;
230
231 for (i = 0; i < tz->trips; i++)
232 if (mask & (1 << i))
233 thermal_zone_unbind_cooling_device(tz, i, cdev);
234}
235
236static void bind_cdev(struct thermal_cooling_device *cdev)
237{
238 int i, ret;
239 const struct thermal_zone_params *tzp;
240 struct thermal_zone_device *pos = NULL;
241
242 mutex_lock(&thermal_list_lock);
243
244 list_for_each_entry(pos, &thermal_tz_list, node) {
245 if (!pos->tzp && !pos->ops->bind)
246 continue;
247
248 if (!pos->tzp && pos->ops->bind) {
249 ret = pos->ops->bind(pos, cdev);
250 if (ret)
251 print_bind_err_msg(pos, cdev, ret);
252 }
253
254 tzp = pos->tzp;
Hugh Dickins791700c2012-09-27 16:48:28 -0700255 if (!tzp || !tzp->tbp)
256 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530257
258 for (i = 0; i < tzp->num_tbps; i++) {
259 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
260 continue;
261 if (tzp->tbp[i].match(pos, cdev))
262 continue;
263 tzp->tbp[i].cdev = cdev;
264 __bind(pos, tzp->tbp[i].trip_mask, cdev);
265 }
266 }
267
268 mutex_unlock(&thermal_list_lock);
269}
270
271static void bind_tz(struct thermal_zone_device *tz)
272{
273 int i, ret;
274 struct thermal_cooling_device *pos = NULL;
275 const struct thermal_zone_params *tzp = tz->tzp;
276
277 if (!tzp && !tz->ops->bind)
278 return;
279
280 mutex_lock(&thermal_list_lock);
281
282 /* If there is no platform data, try to use ops->bind */
283 if (!tzp && tz->ops->bind) {
284 list_for_each_entry(pos, &thermal_cdev_list, node) {
285 ret = tz->ops->bind(tz, pos);
286 if (ret)
287 print_bind_err_msg(tz, pos, ret);
288 }
289 goto exit;
290 }
291
Hugh Dickins791700c2012-09-27 16:48:28 -0700292 if (!tzp || !tzp->tbp)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530293 goto exit;
294
295 list_for_each_entry(pos, &thermal_cdev_list, node) {
296 for (i = 0; i < tzp->num_tbps; i++) {
297 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
298 continue;
299 if (tzp->tbp[i].match(tz, pos))
300 continue;
301 tzp->tbp[i].cdev = pos;
302 __bind(tz, tzp->tbp[i].trip_mask, pos);
303 }
304 }
305exit:
306 mutex_unlock(&thermal_list_lock);
307}
308
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530309static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
310 int delay)
311{
312 if (delay > 1000)
313 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
314 round_jiffies(msecs_to_jiffies(delay)));
315 else if (delay)
316 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
317 msecs_to_jiffies(delay));
318 else
319 cancel_delayed_work(&tz->poll_queue);
320}
321
322static void monitor_thermal_zone(struct thermal_zone_device *tz)
323{
324 mutex_lock(&tz->lock);
325
326 if (tz->passive)
327 thermal_zone_device_set_polling(tz, tz->passive_delay);
328 else if (tz->polling_delay)
329 thermal_zone_device_set_polling(tz, tz->polling_delay);
330 else
331 thermal_zone_device_set_polling(tz, 0);
332
333 mutex_unlock(&tz->lock);
334}
335
336static void handle_non_critical_trips(struct thermal_zone_device *tz,
337 int trip, enum thermal_trip_type trip_type)
338{
Zhang Ruid567c682012-12-12 15:23:54 +0800339 if (tz->governor)
340 tz->governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530341}
342
343static void handle_critical_trips(struct thermal_zone_device *tz,
344 int trip, enum thermal_trip_type trip_type)
345{
346 long trip_temp;
347
348 tz->ops->get_trip_temp(tz, trip, &trip_temp);
349
350 /* If we have not crossed the trip_temp, we do not care. */
351 if (tz->temperature < trip_temp)
352 return;
353
354 if (tz->ops->notify)
355 tz->ops->notify(tz, trip, trip_type);
356
357 if (trip_type == THERMAL_TRIP_CRITICAL) {
358 pr_emerg("Critical temperature reached(%d C),shutting down\n",
359 tz->temperature / 1000);
360 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
381static void update_temperature(struct thermal_zone_device *tz)
382{
383 long temp;
384 int ret;
385
386 mutex_lock(&tz->lock);
387
388 ret = tz->ops->get_temp(tz, &temp);
389 if (ret) {
390 pr_warn("failed to read out thermal zone %d\n", tz->id);
Hugh Dickins791700c2012-09-27 16:48:28 -0700391 goto exit;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530392 }
393
394 tz->last_temperature = tz->temperature;
395 tz->temperature = temp;
396
Hugh Dickins791700c2012-09-27 16:48:28 -0700397exit:
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530398 mutex_unlock(&tz->lock);
399}
400
401void thermal_zone_device_update(struct thermal_zone_device *tz)
402{
403 int count;
404
405 update_temperature(tz);
406
407 for (count = 0; count < tz->trips; count++)
408 handle_thermal_trip(tz, count);
409}
410EXPORT_SYMBOL(thermal_zone_device_update);
411
412static void thermal_zone_device_check(struct work_struct *work)
413{
414 struct thermal_zone_device *tz = container_of(work, struct
415 thermal_zone_device,
416 poll_queue.work);
417 thermal_zone_device_update(tz);
418}
419
Zhang Rui203d3d42008-01-17 15:51:08 +0800420/* sys I/F for thermal zone */
421
422#define to_thermal_zone(_dev) \
423 container_of(_dev, struct thermal_zone_device, device)
424
425static ssize_t
426type_show(struct device *dev, struct device_attribute *attr, char *buf)
427{
428 struct thermal_zone_device *tz = to_thermal_zone(dev);
429
430 return sprintf(buf, "%s\n", tz->type);
431}
432
433static ssize_t
434temp_show(struct device *dev, struct device_attribute *attr, char *buf)
435{
436 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000437 long temperature;
438 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800439
440 if (!tz->ops->get_temp)
441 return -EPERM;
442
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000443 ret = tz->ops->get_temp(tz, &temperature);
444
445 if (ret)
446 return ret;
447
448 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800449}
450
451static ssize_t
452mode_show(struct device *dev, struct device_attribute *attr, char *buf)
453{
454 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000455 enum thermal_device_mode mode;
456 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800457
458 if (!tz->ops->get_mode)
459 return -EPERM;
460
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000461 result = tz->ops->get_mode(tz, &mode);
462 if (result)
463 return result;
464
465 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
466 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800467}
468
469static ssize_t
470mode_store(struct device *dev, struct device_attribute *attr,
471 const char *buf, size_t count)
472{
473 struct thermal_zone_device *tz = to_thermal_zone(dev);
474 int result;
475
476 if (!tz->ops->set_mode)
477 return -EPERM;
478
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530479 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000480 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530481 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000482 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
483 else
484 result = -EINVAL;
485
Zhang Rui203d3d42008-01-17 15:51:08 +0800486 if (result)
487 return result;
488
489 return count;
490}
491
492static ssize_t
493trip_point_type_show(struct device *dev, struct device_attribute *attr,
494 char *buf)
495{
496 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000497 enum thermal_trip_type type;
498 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800499
500 if (!tz->ops->get_trip_type)
501 return -EPERM;
502
503 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
504 return -EINVAL;
505
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000506 result = tz->ops->get_trip_type(tz, trip, &type);
507 if (result)
508 return result;
509
510 switch (type) {
511 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300512 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000513 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300514 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000515 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300516 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000517 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300518 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000519 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300520 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000521 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800522}
523
524static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800525trip_point_temp_store(struct device *dev, struct device_attribute *attr,
526 const char *buf, size_t count)
527{
528 struct thermal_zone_device *tz = to_thermal_zone(dev);
529 int trip, ret;
530 unsigned long temperature;
531
532 if (!tz->ops->set_trip_temp)
533 return -EPERM;
534
535 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
536 return -EINVAL;
537
538 if (kstrtoul(buf, 10, &temperature))
539 return -EINVAL;
540
541 ret = tz->ops->set_trip_temp(tz, trip, temperature);
542
543 return ret ? ret : count;
544}
545
546static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800547trip_point_temp_show(struct device *dev, struct device_attribute *attr,
548 char *buf)
549{
550 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000551 int trip, ret;
552 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800553
554 if (!tz->ops->get_trip_temp)
555 return -EPERM;
556
557 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
558 return -EINVAL;
559
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000560 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
561
562 if (ret)
563 return ret;
564
565 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800566}
567
Matthew Garrett03a971a2008-12-03 18:00:38 +0000568static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800569trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
570 const char *buf, size_t count)
571{
572 struct thermal_zone_device *tz = to_thermal_zone(dev);
573 int trip, ret;
574 unsigned long temperature;
575
576 if (!tz->ops->set_trip_hyst)
577 return -EPERM;
578
579 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
580 return -EINVAL;
581
582 if (kstrtoul(buf, 10, &temperature))
583 return -EINVAL;
584
585 /*
586 * We are not doing any check on the 'temperature' value
587 * here. The driver implementing 'set_trip_hyst' has to
588 * take care of this.
589 */
590 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
591
592 return ret ? ret : count;
593}
594
595static ssize_t
596trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
597 char *buf)
598{
599 struct thermal_zone_device *tz = to_thermal_zone(dev);
600 int trip, ret;
601 unsigned long temperature;
602
603 if (!tz->ops->get_trip_hyst)
604 return -EPERM;
605
606 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
607 return -EINVAL;
608
609 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
610
611 return ret ? ret : sprintf(buf, "%ld\n", temperature);
612}
613
614static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000615passive_store(struct device *dev, struct device_attribute *attr,
616 const char *buf, size_t count)
617{
618 struct thermal_zone_device *tz = to_thermal_zone(dev);
619 struct thermal_cooling_device *cdev = NULL;
620 int state;
621
622 if (!sscanf(buf, "%d\n", &state))
623 return -EINVAL;
624
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100625 /* sanity check: values below 1000 millicelcius don't make sense
626 * and can cause the system to go into a thermal heart attack
627 */
628 if (state && state < 1000)
629 return -EINVAL;
630
Matthew Garrett03a971a2008-12-03 18:00:38 +0000631 if (state && !tz->forced_passive) {
632 mutex_lock(&thermal_list_lock);
633 list_for_each_entry(cdev, &thermal_cdev_list, node) {
634 if (!strncmp("Processor", cdev->type,
635 sizeof("Processor")))
636 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800637 THERMAL_TRIPS_NONE, cdev,
638 THERMAL_NO_LIMIT,
639 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000640 }
641 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100642 if (!tz->passive_delay)
643 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000644 } else if (!state && tz->forced_passive) {
645 mutex_lock(&thermal_list_lock);
646 list_for_each_entry(cdev, &thermal_cdev_list, node) {
647 if (!strncmp("Processor", cdev->type,
648 sizeof("Processor")))
649 thermal_zone_unbind_cooling_device(tz,
650 THERMAL_TRIPS_NONE,
651 cdev);
652 }
653 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100654 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000655 }
656
Matthew Garrett03a971a2008-12-03 18:00:38 +0000657 tz->forced_passive = state;
658
659 thermal_zone_device_update(tz);
660
661 return count;
662}
663
664static ssize_t
665passive_show(struct device *dev, struct device_attribute *attr,
666 char *buf)
667{
668 struct thermal_zone_device *tz = to_thermal_zone(dev);
669
670 return sprintf(buf, "%d\n", tz->forced_passive);
671}
672
Durgadoss R5a2c0902012-09-18 11:04:58 +0530673static ssize_t
674policy_store(struct device *dev, struct device_attribute *attr,
675 const char *buf, size_t count)
676{
677 int ret = -EINVAL;
678 struct thermal_zone_device *tz = to_thermal_zone(dev);
679 struct thermal_governor *gov;
680
681 mutex_lock(&thermal_governor_lock);
682
683 gov = __find_governor(buf);
684 if (!gov)
685 goto exit;
686
687 tz->governor = gov;
688 ret = count;
689
690exit:
691 mutex_unlock(&thermal_governor_lock);
692 return ret;
693}
694
695static ssize_t
696policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
697{
698 struct thermal_zone_device *tz = to_thermal_zone(dev);
699
700 return sprintf(buf, "%s\n", tz->governor->name);
701}
702
Zhang Rui203d3d42008-01-17 15:51:08 +0800703static DEVICE_ATTR(type, 0444, type_show, NULL);
704static DEVICE_ATTR(temp, 0444, temp_show, NULL);
705static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700706static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530707static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800708
Zhang Rui203d3d42008-01-17 15:51:08 +0800709/* sys I/F for cooling device */
710#define to_cooling_device(_dev) \
711 container_of(_dev, struct thermal_cooling_device, device)
712
713static ssize_t
714thermal_cooling_device_type_show(struct device *dev,
715 struct device_attribute *attr, char *buf)
716{
717 struct thermal_cooling_device *cdev = to_cooling_device(dev);
718
719 return sprintf(buf, "%s\n", cdev->type);
720}
721
722static ssize_t
723thermal_cooling_device_max_state_show(struct device *dev,
724 struct device_attribute *attr, char *buf)
725{
726 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000727 unsigned long state;
728 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800729
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000730 ret = cdev->ops->get_max_state(cdev, &state);
731 if (ret)
732 return ret;
733 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800734}
735
736static ssize_t
737thermal_cooling_device_cur_state_show(struct device *dev,
738 struct device_attribute *attr, char *buf)
739{
740 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000741 unsigned long state;
742 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800743
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000744 ret = cdev->ops->get_cur_state(cdev, &state);
745 if (ret)
746 return ret;
747 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800748}
749
750static ssize_t
751thermal_cooling_device_cur_state_store(struct device *dev,
752 struct device_attribute *attr,
753 const char *buf, size_t count)
754{
755 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000756 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800757 int result;
758
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000759 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800760 return -EINVAL;
761
Roel Kluinedb94912009-12-15 22:46:50 +0100762 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800763 return -EINVAL;
764
765 result = cdev->ops->set_cur_state(cdev, state);
766 if (result)
767 return result;
768 return count;
769}
770
771static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500772__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800773static DEVICE_ATTR(max_state, 0444,
774 thermal_cooling_device_max_state_show, NULL);
775static DEVICE_ATTR(cur_state, 0644,
776 thermal_cooling_device_cur_state_show,
777 thermal_cooling_device_cur_state_store);
778
779static ssize_t
780thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500781 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800782{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800783 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800784
785 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800786 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800787
788 if (instance->trip == THERMAL_TRIPS_NONE)
789 return sprintf(buf, "-1\n");
790 else
791 return sprintf(buf, "%d\n", instance->trip);
792}
793
794/* Device management */
795
Rene Herman16d75232008-06-24 19:38:56 +0200796#if defined(CONFIG_THERMAL_HWMON)
797
Zhang Ruie68b16a2008-04-21 16:07:52 +0800798/* hwmon sys I/F */
799#include <linux/hwmon.h>
Jean Delvare31f5396a2011-07-28 13:48:42 -0700800
801/* thermal zone devices with the same type share one hwmon device */
802struct thermal_hwmon_device {
803 char type[THERMAL_NAME_LENGTH];
804 struct device *device;
805 int count;
806 struct list_head tz_list;
807 struct list_head node;
808};
809
810struct thermal_hwmon_attr {
811 struct device_attribute attr;
812 char name[16];
813};
814
815/* one temperature input for each thermal zone */
816struct thermal_hwmon_temp {
817 struct list_head hwmon_node;
818 struct thermal_zone_device *tz;
819 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
820 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
821};
822
Zhang Ruie68b16a2008-04-21 16:07:52 +0800823static LIST_HEAD(thermal_hwmon_list);
824
825static ssize_t
826name_show(struct device *dev, struct device_attribute *attr, char *buf)
827{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700828 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800829 return sprintf(buf, "%s\n", hwmon->type);
830}
831static DEVICE_ATTR(name, 0444, name_show, NULL);
832
833static ssize_t
834temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
835{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000836 long temperature;
837 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800838 struct thermal_hwmon_attr *hwmon_attr
839 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f5396a2011-07-28 13:48:42 -0700840 struct thermal_hwmon_temp *temp
841 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800842 temp_input);
Jean Delvare31f5396a2011-07-28 13:48:42 -0700843 struct thermal_zone_device *tz = temp->tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800844
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000845 ret = tz->ops->get_temp(tz, &temperature);
846
847 if (ret)
848 return ret;
849
850 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800851}
852
853static ssize_t
854temp_crit_show(struct device *dev, struct device_attribute *attr,
855 char *buf)
856{
857 struct thermal_hwmon_attr *hwmon_attr
858 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f5396a2011-07-28 13:48:42 -0700859 struct thermal_hwmon_temp *temp
860 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800861 temp_crit);
Jean Delvare31f5396a2011-07-28 13:48:42 -0700862 struct thermal_zone_device *tz = temp->tz;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000863 long temperature;
864 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800865
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000866 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
867 if (ret)
868 return ret;
869
870 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800871}
872
873
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700874static struct thermal_hwmon_device *
875thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
876{
877 struct thermal_hwmon_device *hwmon;
878
879 mutex_lock(&thermal_list_lock);
880 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
881 if (!strcmp(hwmon->type, tz->type)) {
882 mutex_unlock(&thermal_list_lock);
883 return hwmon;
884 }
885 mutex_unlock(&thermal_list_lock);
886
887 return NULL;
888}
889
Jean Delvare31f5396a2011-07-28 13:48:42 -0700890/* Find the temperature input matching a given thermal zone */
891static struct thermal_hwmon_temp *
892thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
893 const struct thermal_zone_device *tz)
894{
895 struct thermal_hwmon_temp *temp;
896
897 mutex_lock(&thermal_list_lock);
898 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
899 if (temp->tz == tz) {
900 mutex_unlock(&thermal_list_lock);
901 return temp;
902 }
903 mutex_unlock(&thermal_list_lock);
904
905 return NULL;
906}
907
Zhang Ruie68b16a2008-04-21 16:07:52 +0800908static int
909thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
910{
911 struct thermal_hwmon_device *hwmon;
Jean Delvare31f5396a2011-07-28 13:48:42 -0700912 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800913 int new_hwmon_device = 1;
914 int result;
915
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700916 hwmon = thermal_hwmon_lookup_by_type(tz);
917 if (hwmon) {
918 new_hwmon_device = 0;
919 goto register_sys_interface;
920 }
Zhang Ruie68b16a2008-04-21 16:07:52 +0800921
922 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
923 if (!hwmon)
924 return -ENOMEM;
925
926 INIT_LIST_HEAD(&hwmon->tz_list);
927 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
928 hwmon->device = hwmon_device_register(NULL);
929 if (IS_ERR(hwmon->device)) {
930 result = PTR_ERR(hwmon->device);
931 goto free_mem;
932 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700933 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800934 result = device_create_file(hwmon->device, &dev_attr_name);
935 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530936 goto free_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800937
938 register_sys_interface:
Jean Delvare31f5396a2011-07-28 13:48:42 -0700939 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
940 if (!temp) {
941 result = -ENOMEM;
942 goto unregister_name;
943 }
944
945 temp->tz = tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800946 hwmon->count++;
947
Guenter Roeck79a49162012-07-21 10:53:48 +1000948 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +0800949 "temp%d_input", hwmon->count);
Jean Delvare31f5396a2011-07-28 13:48:42 -0700950 temp->temp_input.attr.attr.name = temp->temp_input.name;
951 temp->temp_input.attr.attr.mode = 0444;
952 temp->temp_input.attr.show = temp_input_show;
953 sysfs_attr_init(&temp->temp_input.attr.attr);
954 result = device_create_file(hwmon->device, &temp->temp_input.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800955 if (result)
Jean Delvare31f5396a2011-07-28 13:48:42 -0700956 goto free_temp_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800957
958 if (tz->ops->get_crit_temp) {
959 unsigned long temperature;
960 if (!tz->ops->get_crit_temp(tz, &temperature)) {
Guenter Roeck79a49162012-07-21 10:53:48 +1000961 snprintf(temp->temp_crit.name,
962 sizeof(temp->temp_crit.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +0800963 "temp%d_crit", hwmon->count);
Jean Delvare31f5396a2011-07-28 13:48:42 -0700964 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
965 temp->temp_crit.attr.attr.mode = 0444;
966 temp->temp_crit.attr.show = temp_crit_show;
967 sysfs_attr_init(&temp->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800968 result = device_create_file(hwmon->device,
Jean Delvare31f5396a2011-07-28 13:48:42 -0700969 &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800970 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530971 goto unregister_input;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800972 }
973 }
974
975 mutex_lock(&thermal_list_lock);
976 if (new_hwmon_device)
977 list_add_tail(&hwmon->node, &thermal_hwmon_list);
Jean Delvare31f5396a2011-07-28 13:48:42 -0700978 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800979 mutex_unlock(&thermal_list_lock);
980
981 return 0;
982
Durgadoss Rb299eb52011-03-03 04:30:13 +0530983 unregister_input:
Jean Delvare31f5396a2011-07-28 13:48:42 -0700984 device_remove_file(hwmon->device, &temp->temp_input.attr);
985 free_temp_mem:
986 kfree(temp);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530987 unregister_name:
Zhang Ruie68b16a2008-04-21 16:07:52 +0800988 if (new_hwmon_device) {
989 device_remove_file(hwmon->device, &dev_attr_name);
990 hwmon_device_unregister(hwmon->device);
991 }
992 free_mem:
993 if (new_hwmon_device)
994 kfree(hwmon);
995
996 return result;
997}
998
999static void
1000thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1001{
Jean Delvare31f5396a2011-07-28 13:48:42 -07001002 struct thermal_hwmon_device *hwmon;
1003 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001004
Jean Delvare31f5396a2011-07-28 13:48:42 -07001005 hwmon = thermal_hwmon_lookup_by_type(tz);
1006 if (unlikely(!hwmon)) {
1007 /* Should never happen... */
1008 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
1009 return;
1010 }
1011
1012 temp = thermal_hwmon_lookup_temp(hwmon, tz);
1013 if (unlikely(!temp)) {
1014 /* Should never happen... */
1015 dev_dbg(&tz->device, "temperature input lookup failed!\n");
1016 return;
1017 }
1018
1019 device_remove_file(hwmon->device, &temp->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +05301020 if (tz->ops->get_crit_temp)
Jean Delvare31f5396a2011-07-28 13:48:42 -07001021 device_remove_file(hwmon->device, &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001022
1023 mutex_lock(&thermal_list_lock);
Jean Delvare31f5396a2011-07-28 13:48:42 -07001024 list_del(&temp->hwmon_node);
1025 kfree(temp);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001026 if (!list_empty(&hwmon->tz_list)) {
1027 mutex_unlock(&thermal_list_lock);
1028 return;
1029 }
1030 list_del(&hwmon->node);
1031 mutex_unlock(&thermal_list_lock);
1032
1033 device_remove_file(hwmon->device, &dev_attr_name);
1034 hwmon_device_unregister(hwmon->device);
1035 kfree(hwmon);
1036}
1037#else
1038static int
1039thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
1040{
1041 return 0;
1042}
1043
1044static void
1045thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1046{
1047}
1048#endif
1049
Zhang Rui203d3d42008-01-17 15:51:08 +08001050/**
1051 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +08001052 * @tz: thermal zone device
1053 * @trip: indicates which trip point the cooling devices is
1054 * associated with in this thermal zone.
1055 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -05001056 *
1057 * This function is usually called in the thermal zone device .bind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +08001058 */
1059int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1060 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +08001061 struct thermal_cooling_device *cdev,
1062 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +08001063{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001064 struct thermal_instance *dev;
1065 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -05001066 struct thermal_zone_device *pos1;
1067 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +08001068 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +08001069 int result;
1070
Len Brown543a9562008-02-07 16:55:08 -05001071 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +08001072 return -EINVAL;
1073
Thomas Sujithc7516702008-02-15 00:58:50 -05001074 list_for_each_entry(pos1, &thermal_tz_list, node) {
1075 if (pos1 == tz)
1076 break;
1077 }
1078 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1079 if (pos2 == cdev)
1080 break;
1081 }
1082
1083 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +08001084 return -EINVAL;
1085
Zhang Rui9d998422012-06-26 16:35:57 +08001086 cdev->ops->get_max_state(cdev, &max_state);
1087
1088 /* lower default 0, upper default max_state */
1089 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1090 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1091
1092 if (lower > upper || upper > max_state)
1093 return -EINVAL;
1094
Zhang Rui203d3d42008-01-17 15:51:08 +08001095 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001096 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001097 if (!dev)
1098 return -ENOMEM;
1099 dev->tz = tz;
1100 dev->cdev = cdev;
1101 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +08001102 dev->upper = upper;
1103 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +08001104 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +08001105
Zhang Rui203d3d42008-01-17 15:51:08 +08001106 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1107 if (result)
1108 goto free_mem;
1109
1110 sprintf(dev->name, "cdev%d", dev->id);
1111 result =
1112 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1113 if (result)
1114 goto release_idr;
1115
1116 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -07001117 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001118 dev->attr.attr.name = dev->attr_name;
1119 dev->attr.attr.mode = 0444;
1120 dev->attr.show = thermal_cooling_device_trip_point_show;
1121 result = device_create_file(&tz->device, &dev->attr);
1122 if (result)
1123 goto remove_symbol_link;
1124
1125 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001126 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001127 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +08001128 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1129 result = -EEXIST;
1130 break;
1131 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001132 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001133 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001134 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1135 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001136 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001137 mutex_unlock(&tz->lock);
1138
1139 if (!result)
1140 return 0;
1141
1142 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b802012-03-21 12:55:02 -07001143remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001144 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b802012-03-21 12:55:02 -07001145release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001146 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b802012-03-21 12:55:02 -07001147free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001148 kfree(dev);
1149 return result;
1150}
1151EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
1152
1153/**
1154 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +08001155 * @tz: thermal zone device
1156 * @trip: indicates which trip point the cooling devices is
1157 * associated with in this thermal zone.
1158 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -05001159 *
1160 * This function is usually called in the thermal zone device .unbind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +08001161 */
1162int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1163 int trip,
1164 struct thermal_cooling_device *cdev)
1165{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001166 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001167
1168 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001169 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001170 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001171 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001172 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001173 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001174 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001175 mutex_unlock(&tz->lock);
1176 goto unbind;
1177 }
1178 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001179 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001180 mutex_unlock(&tz->lock);
1181
1182 return -ENODEV;
1183
Joe Perchescaca8b802012-03-21 12:55:02 -07001184unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +08001185 device_remove_file(&tz->device, &pos->attr);
1186 sysfs_remove_link(&tz->device.kobj, pos->name);
1187 release_idr(&tz->idr, &tz->lock, pos->id);
1188 kfree(pos);
1189 return 0;
1190}
1191EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
1192
1193static void thermal_release(struct device *dev)
1194{
1195 struct thermal_zone_device *tz;
1196 struct thermal_cooling_device *cdev;
1197
Joe Perchescaca8b802012-03-21 12:55:02 -07001198 if (!strncmp(dev_name(dev), "thermal_zone",
1199 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001200 tz = to_thermal_zone(dev);
1201 kfree(tz);
1202 } else {
1203 cdev = to_cooling_device(dev);
1204 kfree(cdev);
1205 }
1206}
1207
1208static struct class thermal_class = {
1209 .name = "thermal",
1210 .dev_release = thermal_release,
1211};
1212
1213/**
1214 * thermal_cooling_device_register - register a new thermal cooling device
1215 * @type: the thermal cooling device type.
1216 * @devdata: device private data.
1217 * @ops: standard thermal cooling devices callbacks.
1218 */
Joe Perchescaca8b802012-03-21 12:55:02 -07001219struct thermal_cooling_device *
1220thermal_cooling_device_register(char *type, void *devdata,
1221 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001222{
1223 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001224 int result;
1225
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001226 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001227 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001228
1229 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001230 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001231 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001232
1233 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1234 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001235 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001236
1237 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1238 if (result) {
1239 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001240 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001241 }
1242
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001243 strcpy(cdev->type, type ? : "");
Zhang Ruif4a821c2012-07-24 16:56:21 +08001244 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001245 INIT_LIST_HEAD(&cdev->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001246 cdev->ops = ops;
Zhang Ruice119f82012-06-27 14:13:04 +08001247 cdev->updated = true;
Zhang Rui203d3d42008-01-17 15:51:08 +08001248 cdev->device.class = &thermal_class;
1249 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001250 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001251 result = device_register(&cdev->device);
1252 if (result) {
1253 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1254 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001255 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001256 }
1257
1258 /* sys I/F */
1259 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001260 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001261 if (result)
1262 goto unregister;
1263 }
1264
1265 result = device_create_file(&cdev->device, &dev_attr_max_state);
1266 if (result)
1267 goto unregister;
1268
1269 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1270 if (result)
1271 goto unregister;
1272
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301273 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001274 mutex_lock(&thermal_list_lock);
1275 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001276 mutex_unlock(&thermal_list_lock);
1277
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301278 /* Update binding information for 'this' new cdev */
1279 bind_cdev(cdev);
1280
1281 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001282
Joe Perchescaca8b802012-03-21 12:55:02 -07001283unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001284 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1285 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001286 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001287}
1288EXPORT_SYMBOL(thermal_cooling_device_register);
1289
1290/**
1291 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001292 * @cdev: the thermal cooling device to remove.
1293 *
1294 * thermal_cooling_device_unregister() must be called when the device is no
1295 * longer needed.
1296 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301297void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001298{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301299 int i;
1300 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001301 struct thermal_zone_device *tz;
1302 struct thermal_cooling_device *pos = NULL;
1303
1304 if (!cdev)
1305 return;
1306
1307 mutex_lock(&thermal_list_lock);
1308 list_for_each_entry(pos, &thermal_cdev_list, node)
1309 if (pos == cdev)
1310 break;
1311 if (pos != cdev) {
1312 /* thermal cooling device not found */
1313 mutex_unlock(&thermal_list_lock);
1314 return;
1315 }
1316 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301317
1318 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001319 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301320 if (tz->ops->unbind) {
1321 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001322 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301323 }
1324
1325 if (!tz->tzp || !tz->tzp->tbp)
1326 continue;
1327
1328 tzp = tz->tzp;
1329 for (i = 0; i < tzp->num_tbps; i++) {
1330 if (tzp->tbp[i].cdev == cdev) {
1331 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1332 tzp->tbp[i].cdev = NULL;
1333 }
1334 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001335 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301336
Zhang Rui203d3d42008-01-17 15:51:08 +08001337 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301338
Zhang Rui203d3d42008-01-17 15:51:08 +08001339 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001340 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001341 device_remove_file(&cdev->device, &dev_attr_max_state);
1342 device_remove_file(&cdev->device, &dev_attr_cur_state);
1343
1344 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1345 device_unregister(&cdev->device);
1346 return;
1347}
1348EXPORT_SYMBOL(thermal_cooling_device_unregister);
1349
Durgadoss Rdc765482012-09-18 11:05:00 +05301350void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001351{
1352 struct thermal_instance *instance;
1353 unsigned long target = 0;
1354
1355 /* cooling device is updated*/
1356 if (cdev->updated)
1357 return;
1358
Zhang Ruif4a821c2012-07-24 16:56:21 +08001359 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001360 /* Make sure cdev enters the deepest cooling state */
1361 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1362 if (instance->target == THERMAL_NO_TARGET)
1363 continue;
1364 if (instance->target > target)
1365 target = instance->target;
1366 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001367 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001368 cdev->ops->set_cur_state(cdev, target);
1369 cdev->updated = true;
1370}
Durgadoss Rdc765482012-09-18 11:05:00 +05301371EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001372
Matthew Garrettb1569e92008-12-03 17:55:32 +00001373/**
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301374 * notify_thermal_framework - Sensor drivers use this API to notify framework
1375 * @tz: thermal zone device
1376 * @trip: indicates which trip point has been crossed
1377 *
1378 * This function handles the trip events from sensor drivers. It starts
1379 * throttling the cooling devices according to the policy configured.
1380 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1381 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1382 * The throttling policy is based on the configured platform data; if no
1383 * platform data is provided, this uses the step_wise throttling policy.
1384 */
1385void notify_thermal_framework(struct thermal_zone_device *tz, int trip)
1386{
1387 handle_thermal_trip(tz, trip);
1388}
1389EXPORT_SYMBOL(notify_thermal_framework);
1390
1391/**
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001392 * create_trip_attrs - create attributes for trip points
1393 * @tz: the thermal zone device
1394 * @mask: Writeable trip point bitmap.
1395 */
1396static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1397{
1398 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001399 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001400
Durgadoss R27365a62012-07-25 10:10:59 +08001401 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001402 if (!tz->trip_type_attrs)
1403 return -ENOMEM;
1404
Durgadoss R27365a62012-07-25 10:10:59 +08001405 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001406 if (!tz->trip_temp_attrs) {
1407 kfree(tz->trip_type_attrs);
1408 return -ENOMEM;
1409 }
1410
Durgadoss R27365a62012-07-25 10:10:59 +08001411 if (tz->ops->get_trip_hyst) {
1412 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1413 if (!tz->trip_hyst_attrs) {
1414 kfree(tz->trip_type_attrs);
1415 kfree(tz->trip_temp_attrs);
1416 return -ENOMEM;
1417 }
1418 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001419
Durgadoss R27365a62012-07-25 10:10:59 +08001420
1421 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001422 /* create trip type attribute */
1423 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1424 "trip_point_%d_type", indx);
1425
1426 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1427 tz->trip_type_attrs[indx].attr.attr.name =
1428 tz->trip_type_attrs[indx].name;
1429 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1430 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1431
1432 device_create_file(&tz->device,
1433 &tz->trip_type_attrs[indx].attr);
1434
1435 /* create trip temp attribute */
1436 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1437 "trip_point_%d_temp", indx);
1438
1439 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1440 tz->trip_temp_attrs[indx].attr.attr.name =
1441 tz->trip_temp_attrs[indx].name;
1442 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1443 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1444 if (mask & (1 << indx)) {
1445 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1446 tz->trip_temp_attrs[indx].attr.store =
1447 trip_point_temp_store;
1448 }
1449
1450 device_create_file(&tz->device,
1451 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001452
1453 /* create Optional trip hyst attribute */
1454 if (!tz->ops->get_trip_hyst)
1455 continue;
1456 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1457 "trip_point_%d_hyst", indx);
1458
1459 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1460 tz->trip_hyst_attrs[indx].attr.attr.name =
1461 tz->trip_hyst_attrs[indx].name;
1462 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1463 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1464 if (tz->ops->set_trip_hyst) {
1465 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1466 tz->trip_hyst_attrs[indx].attr.store =
1467 trip_point_hyst_store;
1468 }
1469
1470 device_create_file(&tz->device,
1471 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001472 }
1473 return 0;
1474}
1475
1476static void remove_trip_attrs(struct thermal_zone_device *tz)
1477{
1478 int indx;
1479
1480 for (indx = 0; indx < tz->trips; indx++) {
1481 device_remove_file(&tz->device,
1482 &tz->trip_type_attrs[indx].attr);
1483 device_remove_file(&tz->device,
1484 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001485 if (tz->ops->get_trip_hyst)
1486 device_remove_file(&tz->device,
1487 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001488 }
1489 kfree(tz->trip_type_attrs);
1490 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001491 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001492}
1493
1494/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001495 * thermal_zone_device_register - register a new thermal zone device
1496 * @type: the thermal zone device type
1497 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001498 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001499 * @devdata: private device data
1500 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301501 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001502 * @passive_delay: number of milliseconds to wait between polls when
1503 * performing passive cooling
1504 * @polling_delay: number of milliseconds to wait between polls when checking
1505 * whether trip points have been crossed (0 for interrupt
1506 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001507 *
1508 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001509 * longer needed. The passive cooling depends on the .get_trend() return value.
Zhang Rui203d3d42008-01-17 15:51:08 +08001510 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001511struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001512 int trips, int mask, void *devdata,
Alan Cox5b275ce2010-11-11 15:27:29 +00001513 const struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301514 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001515 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001516{
1517 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001518 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001519 int result;
1520 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001521 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001522
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001523 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001524 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001525
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001526 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001527 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001528
1529 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001530 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001531
1532 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1533 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001534 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001535
Zhang Rui2d374132012-06-27 10:09:00 +08001536 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001537 idr_init(&tz->idr);
1538 mutex_init(&tz->lock);
1539 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1540 if (result) {
1541 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001542 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001543 }
1544
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001545 strcpy(tz->type, type ? : "");
Zhang Rui203d3d42008-01-17 15:51:08 +08001546 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301547 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001548 tz->device.class = &thermal_class;
1549 tz->devdata = devdata;
1550 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001551 tz->passive_delay = passive_delay;
1552 tz->polling_delay = polling_delay;
1553
Kay Sievers354655e2009-01-06 10:44:37 -08001554 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001555 result = device_register(&tz->device);
1556 if (result) {
1557 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1558 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001559 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001560 }
1561
1562 /* sys I/F */
1563 if (type) {
1564 result = device_create_file(&tz->device, &dev_attr_type);
1565 if (result)
1566 goto unregister;
1567 }
1568
1569 result = device_create_file(&tz->device, &dev_attr_temp);
1570 if (result)
1571 goto unregister;
1572
1573 if (ops->get_mode) {
1574 result = device_create_file(&tz->device, &dev_attr_mode);
1575 if (result)
1576 goto unregister;
1577 }
1578
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001579 result = create_trip_attrs(tz, mask);
1580 if (result)
1581 goto unregister;
1582
Zhang Rui203d3d42008-01-17 15:51:08 +08001583 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001584 tz->ops->get_trip_type(tz, count, &trip_type);
1585 if (trip_type == THERMAL_TRIP_PASSIVE)
1586 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001587 }
1588
Durgadoss R5a2c0902012-09-18 11:04:58 +05301589 if (!passive) {
1590 result = device_create_file(&tz->device, &dev_attr_passive);
1591 if (result)
1592 goto unregister;
1593 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001594
Durgadoss R5a2c0902012-09-18 11:04:58 +05301595 /* Create policy attribute */
1596 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001597 if (result)
1598 goto unregister;
1599
Durgadoss Ra4a15482012-09-18 11:04:57 +05301600 /* Update 'this' zone's governor information */
1601 mutex_lock(&thermal_governor_lock);
1602
1603 if (tz->tzp)
1604 tz->governor = __find_governor(tz->tzp->governor_name);
1605 else
1606 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1607
1608 mutex_unlock(&thermal_governor_lock);
1609
Zhang Ruie68b16a2008-04-21 16:07:52 +08001610 result = thermal_add_hwmon_sysfs(tz);
1611 if (result)
1612 goto unregister;
1613
Zhang Rui203d3d42008-01-17 15:51:08 +08001614 mutex_lock(&thermal_list_lock);
1615 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001616 mutex_unlock(&thermal_list_lock);
1617
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301618 /* Bind cooling devices for this zone */
1619 bind_tz(tz);
1620
Matthew Garrettb1569e92008-12-03 17:55:32 +00001621 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1622
1623 thermal_zone_device_update(tz);
1624
Zhang Rui203d3d42008-01-17 15:51:08 +08001625 if (!result)
1626 return tz;
1627
Joe Perchescaca8b802012-03-21 12:55:02 -07001628unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001629 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1630 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001631 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001632}
1633EXPORT_SYMBOL(thermal_zone_device_register);
1634
1635/**
1636 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001637 * @tz: the thermal zone device to remove
1638 */
1639void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1640{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301641 int i;
1642 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001643 struct thermal_cooling_device *cdev;
1644 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001645
1646 if (!tz)
1647 return;
1648
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301649 tzp = tz->tzp;
1650
Zhang Rui203d3d42008-01-17 15:51:08 +08001651 mutex_lock(&thermal_list_lock);
1652 list_for_each_entry(pos, &thermal_tz_list, node)
1653 if (pos == tz)
1654 break;
1655 if (pos != tz) {
1656 /* thermal zone device not found */
1657 mutex_unlock(&thermal_list_lock);
1658 return;
1659 }
1660 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301661
1662 /* Unbind all cdevs associated with 'this' thermal zone */
1663 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1664 if (tz->ops->unbind) {
1665 tz->ops->unbind(tz, cdev);
1666 continue;
1667 }
1668
1669 if (!tzp || !tzp->tbp)
1670 break;
1671
1672 for (i = 0; i < tzp->num_tbps; i++) {
1673 if (tzp->tbp[i].cdev == cdev) {
1674 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1675 tzp->tbp[i].cdev = NULL;
1676 }
1677 }
1678 }
1679
Zhang Rui203d3d42008-01-17 15:51:08 +08001680 mutex_unlock(&thermal_list_lock);
1681
Matthew Garrettb1569e92008-12-03 17:55:32 +00001682 thermal_zone_device_set_polling(tz, 0);
1683
Zhang Rui203d3d42008-01-17 15:51:08 +08001684 if (tz->type[0])
1685 device_remove_file(&tz->device, &dev_attr_type);
1686 device_remove_file(&tz->device, &dev_attr_temp);
1687 if (tz->ops->get_mode)
1688 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301689 device_remove_file(&tz->device, &dev_attr_policy);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001690 remove_trip_attrs(tz);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301691 tz->governor = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001692
Zhang Ruie68b16a2008-04-21 16:07:52 +08001693 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001694 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1695 idr_destroy(&tz->idr);
1696 mutex_destroy(&tz->lock);
1697 device_unregister(&tz->device);
1698 return;
1699}
Zhang Rui203d3d42008-01-17 15:51:08 +08001700EXPORT_SYMBOL(thermal_zone_device_unregister);
1701
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001702#ifdef CONFIG_NET
1703static struct genl_family thermal_event_genl_family = {
1704 .id = GENL_ID_GENERATE,
1705 .name = THERMAL_GENL_FAMILY_NAME,
1706 .version = THERMAL_GENL_VERSION,
1707 .maxattr = THERMAL_GENL_ATTR_MAX,
1708};
1709
1710static struct genl_multicast_group thermal_event_mcgrp = {
1711 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1712};
1713
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001714int thermal_generate_netlink_event(u32 orig, enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301715{
1716 struct sk_buff *skb;
1717 struct nlattr *attr;
1718 struct thermal_genl_event *thermal_event;
1719 void *msg_header;
1720 int size;
1721 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001722 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301723
1724 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001725 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1726 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301727
1728 skb = genlmsg_new(size, GFP_ATOMIC);
1729 if (!skb)
1730 return -ENOMEM;
1731
1732 /* add the genetlink message header */
1733 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1734 &thermal_event_genl_family, 0,
1735 THERMAL_GENL_CMD_EVENT);
1736 if (!msg_header) {
1737 nlmsg_free(skb);
1738 return -ENOMEM;
1739 }
1740
1741 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001742 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1743 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301744
1745 if (!attr) {
1746 nlmsg_free(skb);
1747 return -EINVAL;
1748 }
1749
1750 thermal_event = nla_data(attr);
1751 if (!thermal_event) {
1752 nlmsg_free(skb);
1753 return -EINVAL;
1754 }
1755
1756 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1757
1758 thermal_event->orig = orig;
1759 thermal_event->event = event;
1760
1761 /* send multicast genetlink message */
1762 result = genlmsg_end(skb, msg_header);
1763 if (result < 0) {
1764 nlmsg_free(skb);
1765 return result;
1766 }
1767
1768 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1769 if (result)
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001770 pr_info("failed to send netlink event:%d\n", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301771
1772 return result;
1773}
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001774EXPORT_SYMBOL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301775
1776static int genetlink_init(void)
1777{
1778 int result;
1779
1780 result = genl_register_family(&thermal_event_genl_family);
1781 if (result)
1782 return result;
1783
1784 result = genl_register_mc_group(&thermal_event_genl_family,
1785 &thermal_event_mcgrp);
1786 if (result)
1787 genl_unregister_family(&thermal_event_genl_family);
1788 return result;
1789}
1790
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001791static void genetlink_exit(void)
1792{
1793 genl_unregister_family(&thermal_event_genl_family);
1794}
1795#else /* !CONFIG_NET */
1796static inline int genetlink_init(void) { return 0; }
1797static inline void genetlink_exit(void) {}
1798#endif /* !CONFIG_NET */
1799
Zhang Rui203d3d42008-01-17 15:51:08 +08001800static int __init thermal_init(void)
1801{
1802 int result = 0;
1803
1804 result = class_register(&thermal_class);
1805 if (result) {
1806 idr_destroy(&thermal_tz_idr);
1807 idr_destroy(&thermal_cdev_idr);
1808 mutex_destroy(&thermal_idr_lock);
1809 mutex_destroy(&thermal_list_lock);
1810 }
R.Durgadoss4cb18722010-10-27 03:33:29 +05301811 result = genetlink_init();
Zhang Rui203d3d42008-01-17 15:51:08 +08001812 return result;
1813}
1814
1815static void __exit thermal_exit(void)
1816{
1817 class_unregister(&thermal_class);
1818 idr_destroy(&thermal_tz_idr);
1819 idr_destroy(&thermal_cdev_idr);
1820 mutex_destroy(&thermal_idr_lock);
1821 mutex_destroy(&thermal_list_lock);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301822 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001823}
1824
R.Durgadoss4cb18722010-10-27 03:33:29 +05301825fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001826module_exit(thermal_exit);