blob: a69f24c4414af31b976d131e14ead3448628e084 [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 Perchescaca8b82012-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;
255 if (!tzp->tbp)
256 return;
257
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
292 if (!tzp->tbp)
293 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{
339 tz->governor->throttle(tz, trip);
340}
341
342static void handle_critical_trips(struct thermal_zone_device *tz,
343 int trip, enum thermal_trip_type trip_type)
344{
345 long trip_temp;
346
347 tz->ops->get_trip_temp(tz, trip, &trip_temp);
348
349 /* If we have not crossed the trip_temp, we do not care. */
350 if (tz->temperature < trip_temp)
351 return;
352
353 if (tz->ops->notify)
354 tz->ops->notify(tz, trip, trip_type);
355
356 if (trip_type == THERMAL_TRIP_CRITICAL) {
357 pr_emerg("Critical temperature reached(%d C),shutting down\n",
358 tz->temperature / 1000);
359 orderly_poweroff(true);
360 }
361}
362
363static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
364{
365 enum thermal_trip_type type;
366
367 tz->ops->get_trip_type(tz, trip, &type);
368
369 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
370 handle_critical_trips(tz, trip, type);
371 else
372 handle_non_critical_trips(tz, trip, type);
373 /*
374 * Alright, we handled this trip successfully.
375 * So, start monitoring again.
376 */
377 monitor_thermal_zone(tz);
378}
379
380static void update_temperature(struct thermal_zone_device *tz)
381{
382 long temp;
383 int ret;
384
385 mutex_lock(&tz->lock);
386
387 ret = tz->ops->get_temp(tz, &temp);
388 if (ret) {
389 pr_warn("failed to read out thermal zone %d\n", tz->id);
390 return;
391 }
392
393 tz->last_temperature = tz->temperature;
394 tz->temperature = temp;
395
396 mutex_unlock(&tz->lock);
397}
398
399void thermal_zone_device_update(struct thermal_zone_device *tz)
400{
401 int count;
402
403 update_temperature(tz);
404
405 for (count = 0; count < tz->trips; count++)
406 handle_thermal_trip(tz, count);
407}
408EXPORT_SYMBOL(thermal_zone_device_update);
409
410static void thermal_zone_device_check(struct work_struct *work)
411{
412 struct thermal_zone_device *tz = container_of(work, struct
413 thermal_zone_device,
414 poll_queue.work);
415 thermal_zone_device_update(tz);
416}
417
Zhang Rui203d3d42008-01-17 15:51:08 +0800418/* sys I/F for thermal zone */
419
420#define to_thermal_zone(_dev) \
421 container_of(_dev, struct thermal_zone_device, device)
422
423static ssize_t
424type_show(struct device *dev, struct device_attribute *attr, char *buf)
425{
426 struct thermal_zone_device *tz = to_thermal_zone(dev);
427
428 return sprintf(buf, "%s\n", tz->type);
429}
430
431static ssize_t
432temp_show(struct device *dev, struct device_attribute *attr, char *buf)
433{
434 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000435 long temperature;
436 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800437
438 if (!tz->ops->get_temp)
439 return -EPERM;
440
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000441 ret = tz->ops->get_temp(tz, &temperature);
442
443 if (ret)
444 return ret;
445
446 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800447}
448
449static ssize_t
450mode_show(struct device *dev, struct device_attribute *attr, char *buf)
451{
452 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000453 enum thermal_device_mode mode;
454 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800455
456 if (!tz->ops->get_mode)
457 return -EPERM;
458
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000459 result = tz->ops->get_mode(tz, &mode);
460 if (result)
461 return result;
462
463 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
464 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800465}
466
467static ssize_t
468mode_store(struct device *dev, struct device_attribute *attr,
469 const char *buf, size_t count)
470{
471 struct thermal_zone_device *tz = to_thermal_zone(dev);
472 int result;
473
474 if (!tz->ops->set_mode)
475 return -EPERM;
476
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530477 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000478 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530479 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000480 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
481 else
482 result = -EINVAL;
483
Zhang Rui203d3d42008-01-17 15:51:08 +0800484 if (result)
485 return result;
486
487 return count;
488}
489
490static ssize_t
491trip_point_type_show(struct device *dev, struct device_attribute *attr,
492 char *buf)
493{
494 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000495 enum thermal_trip_type type;
496 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800497
498 if (!tz->ops->get_trip_type)
499 return -EPERM;
500
501 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
502 return -EINVAL;
503
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000504 result = tz->ops->get_trip_type(tz, trip, &type);
505 if (result)
506 return result;
507
508 switch (type) {
509 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300510 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000511 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300512 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000513 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300514 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000515 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300516 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000517 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300518 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000519 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800520}
521
522static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800523trip_point_temp_store(struct device *dev, struct device_attribute *attr,
524 const char *buf, size_t count)
525{
526 struct thermal_zone_device *tz = to_thermal_zone(dev);
527 int trip, ret;
528 unsigned long temperature;
529
530 if (!tz->ops->set_trip_temp)
531 return -EPERM;
532
533 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
534 return -EINVAL;
535
536 if (kstrtoul(buf, 10, &temperature))
537 return -EINVAL;
538
539 ret = tz->ops->set_trip_temp(tz, trip, temperature);
540
541 return ret ? ret : count;
542}
543
544static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800545trip_point_temp_show(struct device *dev, struct device_attribute *attr,
546 char *buf)
547{
548 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000549 int trip, ret;
550 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800551
552 if (!tz->ops->get_trip_temp)
553 return -EPERM;
554
555 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
556 return -EINVAL;
557
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000558 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
559
560 if (ret)
561 return ret;
562
563 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800564}
565
Matthew Garrett03a971a2008-12-03 18:00:38 +0000566static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800567trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
568 const char *buf, size_t count)
569{
570 struct thermal_zone_device *tz = to_thermal_zone(dev);
571 int trip, ret;
572 unsigned long temperature;
573
574 if (!tz->ops->set_trip_hyst)
575 return -EPERM;
576
577 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
578 return -EINVAL;
579
580 if (kstrtoul(buf, 10, &temperature))
581 return -EINVAL;
582
583 /*
584 * We are not doing any check on the 'temperature' value
585 * here. The driver implementing 'set_trip_hyst' has to
586 * take care of this.
587 */
588 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
589
590 return ret ? ret : count;
591}
592
593static ssize_t
594trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
595 char *buf)
596{
597 struct thermal_zone_device *tz = to_thermal_zone(dev);
598 int trip, ret;
599 unsigned long temperature;
600
601 if (!tz->ops->get_trip_hyst)
602 return -EPERM;
603
604 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
605 return -EINVAL;
606
607 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
608
609 return ret ? ret : sprintf(buf, "%ld\n", temperature);
610}
611
612static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000613passive_store(struct device *dev, struct device_attribute *attr,
614 const char *buf, size_t count)
615{
616 struct thermal_zone_device *tz = to_thermal_zone(dev);
617 struct thermal_cooling_device *cdev = NULL;
618 int state;
619
620 if (!sscanf(buf, "%d\n", &state))
621 return -EINVAL;
622
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100623 /* sanity check: values below 1000 millicelcius don't make sense
624 * and can cause the system to go into a thermal heart attack
625 */
626 if (state && state < 1000)
627 return -EINVAL;
628
Matthew Garrett03a971a2008-12-03 18:00:38 +0000629 if (state && !tz->forced_passive) {
630 mutex_lock(&thermal_list_lock);
631 list_for_each_entry(cdev, &thermal_cdev_list, node) {
632 if (!strncmp("Processor", cdev->type,
633 sizeof("Processor")))
634 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800635 THERMAL_TRIPS_NONE, cdev,
636 THERMAL_NO_LIMIT,
637 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000638 }
639 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100640 if (!tz->passive_delay)
641 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000642 } else if (!state && tz->forced_passive) {
643 mutex_lock(&thermal_list_lock);
644 list_for_each_entry(cdev, &thermal_cdev_list, node) {
645 if (!strncmp("Processor", cdev->type,
646 sizeof("Processor")))
647 thermal_zone_unbind_cooling_device(tz,
648 THERMAL_TRIPS_NONE,
649 cdev);
650 }
651 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100652 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000653 }
654
Matthew Garrett03a971a2008-12-03 18:00:38 +0000655 tz->forced_passive = state;
656
657 thermal_zone_device_update(tz);
658
659 return count;
660}
661
662static ssize_t
663passive_show(struct device *dev, struct device_attribute *attr,
664 char *buf)
665{
666 struct thermal_zone_device *tz = to_thermal_zone(dev);
667
668 return sprintf(buf, "%d\n", tz->forced_passive);
669}
670
Durgadoss R5a2c0902012-09-18 11:04:58 +0530671static ssize_t
672policy_store(struct device *dev, struct device_attribute *attr,
673 const char *buf, size_t count)
674{
675 int ret = -EINVAL;
676 struct thermal_zone_device *tz = to_thermal_zone(dev);
677 struct thermal_governor *gov;
678
679 mutex_lock(&thermal_governor_lock);
680
681 gov = __find_governor(buf);
682 if (!gov)
683 goto exit;
684
685 tz->governor = gov;
686 ret = count;
687
688exit:
689 mutex_unlock(&thermal_governor_lock);
690 return ret;
691}
692
693static ssize_t
694policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
695{
696 struct thermal_zone_device *tz = to_thermal_zone(dev);
697
698 return sprintf(buf, "%s\n", tz->governor->name);
699}
700
Zhang Rui203d3d42008-01-17 15:51:08 +0800701static DEVICE_ATTR(type, 0444, type_show, NULL);
702static DEVICE_ATTR(temp, 0444, temp_show, NULL);
703static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700704static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530705static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800706
Zhang Rui203d3d42008-01-17 15:51:08 +0800707/* sys I/F for cooling device */
708#define to_cooling_device(_dev) \
709 container_of(_dev, struct thermal_cooling_device, device)
710
711static ssize_t
712thermal_cooling_device_type_show(struct device *dev,
713 struct device_attribute *attr, char *buf)
714{
715 struct thermal_cooling_device *cdev = to_cooling_device(dev);
716
717 return sprintf(buf, "%s\n", cdev->type);
718}
719
720static ssize_t
721thermal_cooling_device_max_state_show(struct device *dev,
722 struct device_attribute *attr, char *buf)
723{
724 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000725 unsigned long state;
726 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800727
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000728 ret = cdev->ops->get_max_state(cdev, &state);
729 if (ret)
730 return ret;
731 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800732}
733
734static ssize_t
735thermal_cooling_device_cur_state_show(struct device *dev,
736 struct device_attribute *attr, char *buf)
737{
738 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000739 unsigned long state;
740 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800741
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000742 ret = cdev->ops->get_cur_state(cdev, &state);
743 if (ret)
744 return ret;
745 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800746}
747
748static ssize_t
749thermal_cooling_device_cur_state_store(struct device *dev,
750 struct device_attribute *attr,
751 const char *buf, size_t count)
752{
753 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000754 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800755 int result;
756
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000757 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800758 return -EINVAL;
759
Roel Kluinedb94912009-12-15 22:46:50 +0100760 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800761 return -EINVAL;
762
763 result = cdev->ops->set_cur_state(cdev, state);
764 if (result)
765 return result;
766 return count;
767}
768
769static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500770__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800771static DEVICE_ATTR(max_state, 0444,
772 thermal_cooling_device_max_state_show, NULL);
773static DEVICE_ATTR(cur_state, 0644,
774 thermal_cooling_device_cur_state_show,
775 thermal_cooling_device_cur_state_store);
776
777static ssize_t
778thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500779 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800780{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800781 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800782
783 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800784 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800785
786 if (instance->trip == THERMAL_TRIPS_NONE)
787 return sprintf(buf, "-1\n");
788 else
789 return sprintf(buf, "%d\n", instance->trip);
790}
791
792/* Device management */
793
Rene Herman16d75232008-06-24 19:38:56 +0200794#if defined(CONFIG_THERMAL_HWMON)
795
Zhang Ruie68b16a2008-04-21 16:07:52 +0800796/* hwmon sys I/F */
797#include <linux/hwmon.h>
Jean Delvare31f53962011-07-28 13:48:42 -0700798
799/* thermal zone devices with the same type share one hwmon device */
800struct thermal_hwmon_device {
801 char type[THERMAL_NAME_LENGTH];
802 struct device *device;
803 int count;
804 struct list_head tz_list;
805 struct list_head node;
806};
807
808struct thermal_hwmon_attr {
809 struct device_attribute attr;
810 char name[16];
811};
812
813/* one temperature input for each thermal zone */
814struct thermal_hwmon_temp {
815 struct list_head hwmon_node;
816 struct thermal_zone_device *tz;
817 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
818 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
819};
820
Zhang Ruie68b16a2008-04-21 16:07:52 +0800821static LIST_HEAD(thermal_hwmon_list);
822
823static ssize_t
824name_show(struct device *dev, struct device_attribute *attr, char *buf)
825{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700826 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800827 return sprintf(buf, "%s\n", hwmon->type);
828}
829static DEVICE_ATTR(name, 0444, name_show, NULL);
830
831static ssize_t
832temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
833{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000834 long temperature;
835 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800836 struct thermal_hwmon_attr *hwmon_attr
837 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700838 struct thermal_hwmon_temp *temp
839 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800840 temp_input);
Jean Delvare31f53962011-07-28 13:48:42 -0700841 struct thermal_zone_device *tz = temp->tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800842
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000843 ret = tz->ops->get_temp(tz, &temperature);
844
845 if (ret)
846 return ret;
847
848 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800849}
850
851static ssize_t
852temp_crit_show(struct device *dev, struct device_attribute *attr,
853 char *buf)
854{
855 struct thermal_hwmon_attr *hwmon_attr
856 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700857 struct thermal_hwmon_temp *temp
858 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800859 temp_crit);
Jean Delvare31f53962011-07-28 13:48:42 -0700860 struct thermal_zone_device *tz = temp->tz;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000861 long temperature;
862 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800863
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000864 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
865 if (ret)
866 return ret;
867
868 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800869}
870
871
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700872static struct thermal_hwmon_device *
873thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
874{
875 struct thermal_hwmon_device *hwmon;
876
877 mutex_lock(&thermal_list_lock);
878 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
879 if (!strcmp(hwmon->type, tz->type)) {
880 mutex_unlock(&thermal_list_lock);
881 return hwmon;
882 }
883 mutex_unlock(&thermal_list_lock);
884
885 return NULL;
886}
887
Jean Delvare31f53962011-07-28 13:48:42 -0700888/* Find the temperature input matching a given thermal zone */
889static struct thermal_hwmon_temp *
890thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
891 const struct thermal_zone_device *tz)
892{
893 struct thermal_hwmon_temp *temp;
894
895 mutex_lock(&thermal_list_lock);
896 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
897 if (temp->tz == tz) {
898 mutex_unlock(&thermal_list_lock);
899 return temp;
900 }
901 mutex_unlock(&thermal_list_lock);
902
903 return NULL;
904}
905
Zhang Ruie68b16a2008-04-21 16:07:52 +0800906static int
907thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
908{
909 struct thermal_hwmon_device *hwmon;
Jean Delvare31f53962011-07-28 13:48:42 -0700910 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800911 int new_hwmon_device = 1;
912 int result;
913
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700914 hwmon = thermal_hwmon_lookup_by_type(tz);
915 if (hwmon) {
916 new_hwmon_device = 0;
917 goto register_sys_interface;
918 }
Zhang Ruie68b16a2008-04-21 16:07:52 +0800919
920 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
921 if (!hwmon)
922 return -ENOMEM;
923
924 INIT_LIST_HEAD(&hwmon->tz_list);
925 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
926 hwmon->device = hwmon_device_register(NULL);
927 if (IS_ERR(hwmon->device)) {
928 result = PTR_ERR(hwmon->device);
929 goto free_mem;
930 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700931 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800932 result = device_create_file(hwmon->device, &dev_attr_name);
933 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530934 goto free_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800935
936 register_sys_interface:
Jean Delvare31f53962011-07-28 13:48:42 -0700937 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
938 if (!temp) {
939 result = -ENOMEM;
940 goto unregister_name;
941 }
942
943 temp->tz = tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800944 hwmon->count++;
945
Guenter Roeck79a49162012-07-21 10:53:48 +1000946 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +0800947 "temp%d_input", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700948 temp->temp_input.attr.attr.name = temp->temp_input.name;
949 temp->temp_input.attr.attr.mode = 0444;
950 temp->temp_input.attr.show = temp_input_show;
951 sysfs_attr_init(&temp->temp_input.attr.attr);
952 result = device_create_file(hwmon->device, &temp->temp_input.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800953 if (result)
Jean Delvare31f53962011-07-28 13:48:42 -0700954 goto free_temp_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800955
956 if (tz->ops->get_crit_temp) {
957 unsigned long temperature;
958 if (!tz->ops->get_crit_temp(tz, &temperature)) {
Guenter Roeck79a49162012-07-21 10:53:48 +1000959 snprintf(temp->temp_crit.name,
960 sizeof(temp->temp_crit.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +0800961 "temp%d_crit", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700962 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
963 temp->temp_crit.attr.attr.mode = 0444;
964 temp->temp_crit.attr.show = temp_crit_show;
965 sysfs_attr_init(&temp->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800966 result = device_create_file(hwmon->device,
Jean Delvare31f53962011-07-28 13:48:42 -0700967 &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800968 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530969 goto unregister_input;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800970 }
971 }
972
973 mutex_lock(&thermal_list_lock);
974 if (new_hwmon_device)
975 list_add_tail(&hwmon->node, &thermal_hwmon_list);
Jean Delvare31f53962011-07-28 13:48:42 -0700976 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800977 mutex_unlock(&thermal_list_lock);
978
979 return 0;
980
Durgadoss Rb299eb52011-03-03 04:30:13 +0530981 unregister_input:
Jean Delvare31f53962011-07-28 13:48:42 -0700982 device_remove_file(hwmon->device, &temp->temp_input.attr);
983 free_temp_mem:
984 kfree(temp);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530985 unregister_name:
Zhang Ruie68b16a2008-04-21 16:07:52 +0800986 if (new_hwmon_device) {
987 device_remove_file(hwmon->device, &dev_attr_name);
988 hwmon_device_unregister(hwmon->device);
989 }
990 free_mem:
991 if (new_hwmon_device)
992 kfree(hwmon);
993
994 return result;
995}
996
997static void
998thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
999{
Jean Delvare31f53962011-07-28 13:48:42 -07001000 struct thermal_hwmon_device *hwmon;
1001 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001002
Jean Delvare31f53962011-07-28 13:48:42 -07001003 hwmon = thermal_hwmon_lookup_by_type(tz);
1004 if (unlikely(!hwmon)) {
1005 /* Should never happen... */
1006 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
1007 return;
1008 }
1009
1010 temp = thermal_hwmon_lookup_temp(hwmon, tz);
1011 if (unlikely(!temp)) {
1012 /* Should never happen... */
1013 dev_dbg(&tz->device, "temperature input lookup failed!\n");
1014 return;
1015 }
1016
1017 device_remove_file(hwmon->device, &temp->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +05301018 if (tz->ops->get_crit_temp)
Jean Delvare31f53962011-07-28 13:48:42 -07001019 device_remove_file(hwmon->device, &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001020
1021 mutex_lock(&thermal_list_lock);
Jean Delvare31f53962011-07-28 13:48:42 -07001022 list_del(&temp->hwmon_node);
1023 kfree(temp);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001024 if (!list_empty(&hwmon->tz_list)) {
1025 mutex_unlock(&thermal_list_lock);
1026 return;
1027 }
1028 list_del(&hwmon->node);
1029 mutex_unlock(&thermal_list_lock);
1030
1031 device_remove_file(hwmon->device, &dev_attr_name);
1032 hwmon_device_unregister(hwmon->device);
1033 kfree(hwmon);
1034}
1035#else
1036static int
1037thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
1038{
1039 return 0;
1040}
1041
1042static void
1043thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1044{
1045}
1046#endif
1047
Zhang Rui203d3d42008-01-17 15:51:08 +08001048/**
1049 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +08001050 * @tz: thermal zone device
1051 * @trip: indicates which trip point the cooling devices is
1052 * associated with in this thermal zone.
1053 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -05001054 *
1055 * This function is usually called in the thermal zone device .bind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +08001056 */
1057int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1058 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +08001059 struct thermal_cooling_device *cdev,
1060 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +08001061{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001062 struct thermal_instance *dev;
1063 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -05001064 struct thermal_zone_device *pos1;
1065 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +08001066 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +08001067 int result;
1068
Len Brown543a9562008-02-07 16:55:08 -05001069 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +08001070 return -EINVAL;
1071
Thomas Sujithc7516702008-02-15 00:58:50 -05001072 list_for_each_entry(pos1, &thermal_tz_list, node) {
1073 if (pos1 == tz)
1074 break;
1075 }
1076 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1077 if (pos2 == cdev)
1078 break;
1079 }
1080
1081 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +08001082 return -EINVAL;
1083
Zhang Rui9d998422012-06-26 16:35:57 +08001084 cdev->ops->get_max_state(cdev, &max_state);
1085
1086 /* lower default 0, upper default max_state */
1087 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1088 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1089
1090 if (lower > upper || upper > max_state)
1091 return -EINVAL;
1092
Zhang Rui203d3d42008-01-17 15:51:08 +08001093 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001094 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001095 if (!dev)
1096 return -ENOMEM;
1097 dev->tz = tz;
1098 dev->cdev = cdev;
1099 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +08001100 dev->upper = upper;
1101 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +08001102 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +08001103
Zhang Rui203d3d42008-01-17 15:51:08 +08001104 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1105 if (result)
1106 goto free_mem;
1107
1108 sprintf(dev->name, "cdev%d", dev->id);
1109 result =
1110 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1111 if (result)
1112 goto release_idr;
1113
1114 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -07001115 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001116 dev->attr.attr.name = dev->attr_name;
1117 dev->attr.attr.mode = 0444;
1118 dev->attr.show = thermal_cooling_device_trip_point_show;
1119 result = device_create_file(&tz->device, &dev->attr);
1120 if (result)
1121 goto remove_symbol_link;
1122
1123 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001124 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001125 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +08001126 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1127 result = -EEXIST;
1128 break;
1129 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001130 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001131 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001132 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1133 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001134 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001135 mutex_unlock(&tz->lock);
1136
1137 if (!result)
1138 return 0;
1139
1140 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -07001141remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001142 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -07001143release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001144 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -07001145free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001146 kfree(dev);
1147 return result;
1148}
1149EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
1150
1151/**
1152 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +08001153 * @tz: thermal zone device
1154 * @trip: indicates which trip point the cooling devices is
1155 * associated with in this thermal zone.
1156 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -05001157 *
1158 * This function is usually called in the thermal zone device .unbind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +08001159 */
1160int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1161 int trip,
1162 struct thermal_cooling_device *cdev)
1163{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001164 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001165
1166 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001167 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001168 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001169 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001170 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001171 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001172 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001173 mutex_unlock(&tz->lock);
1174 goto unbind;
1175 }
1176 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001177 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001178 mutex_unlock(&tz->lock);
1179
1180 return -ENODEV;
1181
Joe Perchescaca8b82012-03-21 12:55:02 -07001182unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +08001183 device_remove_file(&tz->device, &pos->attr);
1184 sysfs_remove_link(&tz->device.kobj, pos->name);
1185 release_idr(&tz->idr, &tz->lock, pos->id);
1186 kfree(pos);
1187 return 0;
1188}
1189EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
1190
1191static void thermal_release(struct device *dev)
1192{
1193 struct thermal_zone_device *tz;
1194 struct thermal_cooling_device *cdev;
1195
Joe Perchescaca8b82012-03-21 12:55:02 -07001196 if (!strncmp(dev_name(dev), "thermal_zone",
1197 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001198 tz = to_thermal_zone(dev);
1199 kfree(tz);
1200 } else {
1201 cdev = to_cooling_device(dev);
1202 kfree(cdev);
1203 }
1204}
1205
1206static struct class thermal_class = {
1207 .name = "thermal",
1208 .dev_release = thermal_release,
1209};
1210
1211/**
1212 * thermal_cooling_device_register - register a new thermal cooling device
1213 * @type: the thermal cooling device type.
1214 * @devdata: device private data.
1215 * @ops: standard thermal cooling devices callbacks.
1216 */
Joe Perchescaca8b82012-03-21 12:55:02 -07001217struct thermal_cooling_device *
1218thermal_cooling_device_register(char *type, void *devdata,
1219 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001220{
1221 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001222 int result;
1223
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001224 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001225 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001226
1227 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001228 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001229 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001230
1231 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1232 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001233 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001234
1235 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1236 if (result) {
1237 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001238 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001239 }
1240
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001241 strcpy(cdev->type, type ? : "");
Zhang Ruif4a821c2012-07-24 16:56:21 +08001242 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001243 INIT_LIST_HEAD(&cdev->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001244 cdev->ops = ops;
Zhang Ruice119f82012-06-27 14:13:04 +08001245 cdev->updated = true;
Zhang Rui203d3d42008-01-17 15:51:08 +08001246 cdev->device.class = &thermal_class;
1247 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001248 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001249 result = device_register(&cdev->device);
1250 if (result) {
1251 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1252 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001253 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001254 }
1255
1256 /* sys I/F */
1257 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001258 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001259 if (result)
1260 goto unregister;
1261 }
1262
1263 result = device_create_file(&cdev->device, &dev_attr_max_state);
1264 if (result)
1265 goto unregister;
1266
1267 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1268 if (result)
1269 goto unregister;
1270
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301271 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001272 mutex_lock(&thermal_list_lock);
1273 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001274 mutex_unlock(&thermal_list_lock);
1275
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301276 /* Update binding information for 'this' new cdev */
1277 bind_cdev(cdev);
1278
1279 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001280
Joe Perchescaca8b82012-03-21 12:55:02 -07001281unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001282 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1283 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001284 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001285}
1286EXPORT_SYMBOL(thermal_cooling_device_register);
1287
1288/**
1289 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001290 * @cdev: the thermal cooling device to remove.
1291 *
1292 * thermal_cooling_device_unregister() must be called when the device is no
1293 * longer needed.
1294 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301295void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001296{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301297 int i;
1298 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001299 struct thermal_zone_device *tz;
1300 struct thermal_cooling_device *pos = NULL;
1301
1302 if (!cdev)
1303 return;
1304
1305 mutex_lock(&thermal_list_lock);
1306 list_for_each_entry(pos, &thermal_cdev_list, node)
1307 if (pos == cdev)
1308 break;
1309 if (pos != cdev) {
1310 /* thermal cooling device not found */
1311 mutex_unlock(&thermal_list_lock);
1312 return;
1313 }
1314 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301315
1316 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001317 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301318 if (tz->ops->unbind) {
1319 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001320 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301321 }
1322
1323 if (!tz->tzp || !tz->tzp->tbp)
1324 continue;
1325
1326 tzp = tz->tzp;
1327 for (i = 0; i < tzp->num_tbps; i++) {
1328 if (tzp->tbp[i].cdev == cdev) {
1329 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1330 tzp->tbp[i].cdev = NULL;
1331 }
1332 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001333 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301334
Zhang Rui203d3d42008-01-17 15:51:08 +08001335 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301336
Zhang Rui203d3d42008-01-17 15:51:08 +08001337 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001338 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001339 device_remove_file(&cdev->device, &dev_attr_max_state);
1340 device_remove_file(&cdev->device, &dev_attr_cur_state);
1341
1342 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1343 device_unregister(&cdev->device);
1344 return;
1345}
1346EXPORT_SYMBOL(thermal_cooling_device_unregister);
1347
Durgadoss Rdc765482012-09-18 11:05:00 +05301348void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001349{
1350 struct thermal_instance *instance;
1351 unsigned long target = 0;
1352
1353 /* cooling device is updated*/
1354 if (cdev->updated)
1355 return;
1356
Zhang Ruif4a821c2012-07-24 16:56:21 +08001357 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001358 /* Make sure cdev enters the deepest cooling state */
1359 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1360 if (instance->target == THERMAL_NO_TARGET)
1361 continue;
1362 if (instance->target > target)
1363 target = instance->target;
1364 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001365 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001366 cdev->ops->set_cur_state(cdev, target);
1367 cdev->updated = true;
1368}
Durgadoss Rdc765482012-09-18 11:05:00 +05301369EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001370
Matthew Garrettb1569e92008-12-03 17:55:32 +00001371/**
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301372 * notify_thermal_framework - Sensor drivers use this API to notify framework
1373 * @tz: thermal zone device
1374 * @trip: indicates which trip point has been crossed
1375 *
1376 * This function handles the trip events from sensor drivers. It starts
1377 * throttling the cooling devices according to the policy configured.
1378 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1379 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1380 * The throttling policy is based on the configured platform data; if no
1381 * platform data is provided, this uses the step_wise throttling policy.
1382 */
1383void notify_thermal_framework(struct thermal_zone_device *tz, int trip)
1384{
1385 handle_thermal_trip(tz, trip);
1386}
1387EXPORT_SYMBOL(notify_thermal_framework);
1388
1389/**
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001390 * create_trip_attrs - create attributes for trip points
1391 * @tz: the thermal zone device
1392 * @mask: Writeable trip point bitmap.
1393 */
1394static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1395{
1396 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001397 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001398
Durgadoss R27365a62012-07-25 10:10:59 +08001399 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001400 if (!tz->trip_type_attrs)
1401 return -ENOMEM;
1402
Durgadoss R27365a62012-07-25 10:10:59 +08001403 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001404 if (!tz->trip_temp_attrs) {
1405 kfree(tz->trip_type_attrs);
1406 return -ENOMEM;
1407 }
1408
Durgadoss R27365a62012-07-25 10:10:59 +08001409 if (tz->ops->get_trip_hyst) {
1410 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1411 if (!tz->trip_hyst_attrs) {
1412 kfree(tz->trip_type_attrs);
1413 kfree(tz->trip_temp_attrs);
1414 return -ENOMEM;
1415 }
1416 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001417
Durgadoss R27365a62012-07-25 10:10:59 +08001418
1419 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001420 /* create trip type attribute */
1421 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1422 "trip_point_%d_type", indx);
1423
1424 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1425 tz->trip_type_attrs[indx].attr.attr.name =
1426 tz->trip_type_attrs[indx].name;
1427 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1428 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1429
1430 device_create_file(&tz->device,
1431 &tz->trip_type_attrs[indx].attr);
1432
1433 /* create trip temp attribute */
1434 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1435 "trip_point_%d_temp", indx);
1436
1437 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1438 tz->trip_temp_attrs[indx].attr.attr.name =
1439 tz->trip_temp_attrs[indx].name;
1440 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1441 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1442 if (mask & (1 << indx)) {
1443 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1444 tz->trip_temp_attrs[indx].attr.store =
1445 trip_point_temp_store;
1446 }
1447
1448 device_create_file(&tz->device,
1449 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001450
1451 /* create Optional trip hyst attribute */
1452 if (!tz->ops->get_trip_hyst)
1453 continue;
1454 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1455 "trip_point_%d_hyst", indx);
1456
1457 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1458 tz->trip_hyst_attrs[indx].attr.attr.name =
1459 tz->trip_hyst_attrs[indx].name;
1460 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1461 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1462 if (tz->ops->set_trip_hyst) {
1463 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1464 tz->trip_hyst_attrs[indx].attr.store =
1465 trip_point_hyst_store;
1466 }
1467
1468 device_create_file(&tz->device,
1469 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001470 }
1471 return 0;
1472}
1473
1474static void remove_trip_attrs(struct thermal_zone_device *tz)
1475{
1476 int indx;
1477
1478 for (indx = 0; indx < tz->trips; indx++) {
1479 device_remove_file(&tz->device,
1480 &tz->trip_type_attrs[indx].attr);
1481 device_remove_file(&tz->device,
1482 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001483 if (tz->ops->get_trip_hyst)
1484 device_remove_file(&tz->device,
1485 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001486 }
1487 kfree(tz->trip_type_attrs);
1488 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001489 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001490}
1491
1492/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001493 * thermal_zone_device_register - register a new thermal zone device
1494 * @type: the thermal zone device type
1495 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001496 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001497 * @devdata: private device data
1498 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301499 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001500 * @passive_delay: number of milliseconds to wait between polls when
1501 * performing passive cooling
1502 * @polling_delay: number of milliseconds to wait between polls when checking
1503 * whether trip points have been crossed (0 for interrupt
1504 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001505 *
1506 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001507 * longer needed. The passive cooling depends on the .get_trend() return value.
Zhang Rui203d3d42008-01-17 15:51:08 +08001508 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001509struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001510 int trips, int mask, void *devdata,
Alan Cox5b275ce2010-11-11 15:27:29 +00001511 const struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301512 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001513 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001514{
1515 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001516 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001517 int result;
1518 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001519 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001520
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001521 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001522 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001523
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001524 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001525 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001526
1527 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001528 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001529
1530 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1531 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001532 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001533
Zhang Rui2d374132012-06-27 10:09:00 +08001534 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001535 idr_init(&tz->idr);
1536 mutex_init(&tz->lock);
1537 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1538 if (result) {
1539 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001540 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001541 }
1542
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001543 strcpy(tz->type, type ? : "");
Zhang Rui203d3d42008-01-17 15:51:08 +08001544 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301545 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001546 tz->device.class = &thermal_class;
1547 tz->devdata = devdata;
1548 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001549 tz->passive_delay = passive_delay;
1550 tz->polling_delay = polling_delay;
1551
Kay Sievers354655e2009-01-06 10:44:37 -08001552 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001553 result = device_register(&tz->device);
1554 if (result) {
1555 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1556 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001557 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001558 }
1559
1560 /* sys I/F */
1561 if (type) {
1562 result = device_create_file(&tz->device, &dev_attr_type);
1563 if (result)
1564 goto unregister;
1565 }
1566
1567 result = device_create_file(&tz->device, &dev_attr_temp);
1568 if (result)
1569 goto unregister;
1570
1571 if (ops->get_mode) {
1572 result = device_create_file(&tz->device, &dev_attr_mode);
1573 if (result)
1574 goto unregister;
1575 }
1576
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001577 result = create_trip_attrs(tz, mask);
1578 if (result)
1579 goto unregister;
1580
Zhang Rui203d3d42008-01-17 15:51:08 +08001581 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001582 tz->ops->get_trip_type(tz, count, &trip_type);
1583 if (trip_type == THERMAL_TRIP_PASSIVE)
1584 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001585 }
1586
Durgadoss R5a2c0902012-09-18 11:04:58 +05301587 if (!passive) {
1588 result = device_create_file(&tz->device, &dev_attr_passive);
1589 if (result)
1590 goto unregister;
1591 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001592
Durgadoss R5a2c0902012-09-18 11:04:58 +05301593 /* Create policy attribute */
1594 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001595 if (result)
1596 goto unregister;
1597
Durgadoss Ra4a15482012-09-18 11:04:57 +05301598 /* Update 'this' zone's governor information */
1599 mutex_lock(&thermal_governor_lock);
1600
1601 if (tz->tzp)
1602 tz->governor = __find_governor(tz->tzp->governor_name);
1603 else
1604 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1605
1606 mutex_unlock(&thermal_governor_lock);
1607
Zhang Ruie68b16a2008-04-21 16:07:52 +08001608 result = thermal_add_hwmon_sysfs(tz);
1609 if (result)
1610 goto unregister;
1611
Zhang Rui203d3d42008-01-17 15:51:08 +08001612 mutex_lock(&thermal_list_lock);
1613 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001614 mutex_unlock(&thermal_list_lock);
1615
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301616 /* Bind cooling devices for this zone */
1617 bind_tz(tz);
1618
Matthew Garrettb1569e92008-12-03 17:55:32 +00001619 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1620
1621 thermal_zone_device_update(tz);
1622
Zhang Rui203d3d42008-01-17 15:51:08 +08001623 if (!result)
1624 return tz;
1625
Joe Perchescaca8b82012-03-21 12:55:02 -07001626unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001627 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1628 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001629 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001630}
1631EXPORT_SYMBOL(thermal_zone_device_register);
1632
1633/**
1634 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001635 * @tz: the thermal zone device to remove
1636 */
1637void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1638{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301639 int i;
1640 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001641 struct thermal_cooling_device *cdev;
1642 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001643
1644 if (!tz)
1645 return;
1646
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301647 tzp = tz->tzp;
1648
Zhang Rui203d3d42008-01-17 15:51:08 +08001649 mutex_lock(&thermal_list_lock);
1650 list_for_each_entry(pos, &thermal_tz_list, node)
1651 if (pos == tz)
1652 break;
1653 if (pos != tz) {
1654 /* thermal zone device not found */
1655 mutex_unlock(&thermal_list_lock);
1656 return;
1657 }
1658 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301659
1660 /* Unbind all cdevs associated with 'this' thermal zone */
1661 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1662 if (tz->ops->unbind) {
1663 tz->ops->unbind(tz, cdev);
1664 continue;
1665 }
1666
1667 if (!tzp || !tzp->tbp)
1668 break;
1669
1670 for (i = 0; i < tzp->num_tbps; i++) {
1671 if (tzp->tbp[i].cdev == cdev) {
1672 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1673 tzp->tbp[i].cdev = NULL;
1674 }
1675 }
1676 }
1677
Zhang Rui203d3d42008-01-17 15:51:08 +08001678 mutex_unlock(&thermal_list_lock);
1679
Matthew Garrettb1569e92008-12-03 17:55:32 +00001680 thermal_zone_device_set_polling(tz, 0);
1681
Zhang Rui203d3d42008-01-17 15:51:08 +08001682 if (tz->type[0])
1683 device_remove_file(&tz->device, &dev_attr_type);
1684 device_remove_file(&tz->device, &dev_attr_temp);
1685 if (tz->ops->get_mode)
1686 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301687 device_remove_file(&tz->device, &dev_attr_policy);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001688 remove_trip_attrs(tz);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301689 tz->governor = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001690
Zhang Ruie68b16a2008-04-21 16:07:52 +08001691 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001692 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1693 idr_destroy(&tz->idr);
1694 mutex_destroy(&tz->lock);
1695 device_unregister(&tz->device);
1696 return;
1697}
Zhang Rui203d3d42008-01-17 15:51:08 +08001698EXPORT_SYMBOL(thermal_zone_device_unregister);
1699
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001700#ifdef CONFIG_NET
1701static struct genl_family thermal_event_genl_family = {
1702 .id = GENL_ID_GENERATE,
1703 .name = THERMAL_GENL_FAMILY_NAME,
1704 .version = THERMAL_GENL_VERSION,
1705 .maxattr = THERMAL_GENL_ATTR_MAX,
1706};
1707
1708static struct genl_multicast_group thermal_event_mcgrp = {
1709 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1710};
1711
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001712int thermal_generate_netlink_event(u32 orig, enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301713{
1714 struct sk_buff *skb;
1715 struct nlattr *attr;
1716 struct thermal_genl_event *thermal_event;
1717 void *msg_header;
1718 int size;
1719 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001720 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301721
1722 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001723 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1724 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301725
1726 skb = genlmsg_new(size, GFP_ATOMIC);
1727 if (!skb)
1728 return -ENOMEM;
1729
1730 /* add the genetlink message header */
1731 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1732 &thermal_event_genl_family, 0,
1733 THERMAL_GENL_CMD_EVENT);
1734 if (!msg_header) {
1735 nlmsg_free(skb);
1736 return -ENOMEM;
1737 }
1738
1739 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001740 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1741 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301742
1743 if (!attr) {
1744 nlmsg_free(skb);
1745 return -EINVAL;
1746 }
1747
1748 thermal_event = nla_data(attr);
1749 if (!thermal_event) {
1750 nlmsg_free(skb);
1751 return -EINVAL;
1752 }
1753
1754 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1755
1756 thermal_event->orig = orig;
1757 thermal_event->event = event;
1758
1759 /* send multicast genetlink message */
1760 result = genlmsg_end(skb, msg_header);
1761 if (result < 0) {
1762 nlmsg_free(skb);
1763 return result;
1764 }
1765
1766 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1767 if (result)
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001768 pr_info("failed to send netlink event:%d\n", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301769
1770 return result;
1771}
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001772EXPORT_SYMBOL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301773
1774static int genetlink_init(void)
1775{
1776 int result;
1777
1778 result = genl_register_family(&thermal_event_genl_family);
1779 if (result)
1780 return result;
1781
1782 result = genl_register_mc_group(&thermal_event_genl_family,
1783 &thermal_event_mcgrp);
1784 if (result)
1785 genl_unregister_family(&thermal_event_genl_family);
1786 return result;
1787}
1788
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001789static void genetlink_exit(void)
1790{
1791 genl_unregister_family(&thermal_event_genl_family);
1792}
1793#else /* !CONFIG_NET */
1794static inline int genetlink_init(void) { return 0; }
1795static inline void genetlink_exit(void) {}
1796#endif /* !CONFIG_NET */
1797
Zhang Rui203d3d42008-01-17 15:51:08 +08001798static int __init thermal_init(void)
1799{
1800 int result = 0;
1801
1802 result = class_register(&thermal_class);
1803 if (result) {
1804 idr_destroy(&thermal_tz_idr);
1805 idr_destroy(&thermal_cdev_idr);
1806 mutex_destroy(&thermal_idr_lock);
1807 mutex_destroy(&thermal_list_lock);
1808 }
R.Durgadoss4cb18722010-10-27 03:33:29 +05301809 result = genetlink_init();
Zhang Rui203d3d42008-01-17 15:51:08 +08001810 return result;
1811}
1812
1813static void __exit thermal_exit(void)
1814{
1815 class_unregister(&thermal_class);
1816 idr_destroy(&thermal_tz_idr);
1817 idr_destroy(&thermal_cdev_idr);
1818 mutex_destroy(&thermal_idr_lock);
1819 mutex_destroy(&thermal_list_lock);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301820 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001821}
1822
R.Durgadoss4cb18722010-10-27 03:33:29 +05301823fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001824module_exit(thermal_exit);