blob: 8f0f37bb2825992bb14ba400975a1d15f5583fa9 [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;
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{
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);
Hugh Dickins791700c2012-09-27 16:48:28 -0700390 goto exit;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530391 }
392
393 tz->last_temperature = tz->temperature;
394 tz->temperature = temp;
395
Hugh Dickins791700c2012-09-27 16:48:28 -0700396exit:
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530397 mutex_unlock(&tz->lock);
398}
399
400void thermal_zone_device_update(struct thermal_zone_device *tz)
401{
402 int count;
403
404 update_temperature(tz);
405
406 for (count = 0; count < tz->trips; count++)
407 handle_thermal_trip(tz, count);
408}
409EXPORT_SYMBOL(thermal_zone_device_update);
410
411static void thermal_zone_device_check(struct work_struct *work)
412{
413 struct thermal_zone_device *tz = container_of(work, struct
414 thermal_zone_device,
415 poll_queue.work);
416 thermal_zone_device_update(tz);
417}
418
Zhang Rui203d3d42008-01-17 15:51:08 +0800419/* sys I/F for thermal zone */
420
421#define to_thermal_zone(_dev) \
422 container_of(_dev, struct thermal_zone_device, device)
423
424static ssize_t
425type_show(struct device *dev, struct device_attribute *attr, char *buf)
426{
427 struct thermal_zone_device *tz = to_thermal_zone(dev);
428
429 return sprintf(buf, "%s\n", tz->type);
430}
431
432static ssize_t
433temp_show(struct device *dev, struct device_attribute *attr, char *buf)
434{
435 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000436 long temperature;
437 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800438
439 if (!tz->ops->get_temp)
440 return -EPERM;
441
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000442 ret = tz->ops->get_temp(tz, &temperature);
443
444 if (ret)
445 return ret;
446
447 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800448}
449
450static ssize_t
451mode_show(struct device *dev, struct device_attribute *attr, char *buf)
452{
453 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000454 enum thermal_device_mode mode;
455 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800456
457 if (!tz->ops->get_mode)
458 return -EPERM;
459
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000460 result = tz->ops->get_mode(tz, &mode);
461 if (result)
462 return result;
463
464 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
465 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800466}
467
468static ssize_t
469mode_store(struct device *dev, struct device_attribute *attr,
470 const char *buf, size_t count)
471{
472 struct thermal_zone_device *tz = to_thermal_zone(dev);
473 int result;
474
475 if (!tz->ops->set_mode)
476 return -EPERM;
477
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530478 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000479 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530480 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000481 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
482 else
483 result = -EINVAL;
484
Zhang Rui203d3d42008-01-17 15:51:08 +0800485 if (result)
486 return result;
487
488 return count;
489}
490
491static ssize_t
492trip_point_type_show(struct device *dev, struct device_attribute *attr,
493 char *buf)
494{
495 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000496 enum thermal_trip_type type;
497 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800498
499 if (!tz->ops->get_trip_type)
500 return -EPERM;
501
502 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
503 return -EINVAL;
504
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000505 result = tz->ops->get_trip_type(tz, trip, &type);
506 if (result)
507 return result;
508
509 switch (type) {
510 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300511 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000512 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300513 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000514 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300515 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000516 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300517 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000518 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300519 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000520 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800521}
522
523static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800524trip_point_temp_store(struct device *dev, struct device_attribute *attr,
525 const char *buf, size_t count)
526{
527 struct thermal_zone_device *tz = to_thermal_zone(dev);
528 int trip, ret;
529 unsigned long temperature;
530
531 if (!tz->ops->set_trip_temp)
532 return -EPERM;
533
534 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
535 return -EINVAL;
536
537 if (kstrtoul(buf, 10, &temperature))
538 return -EINVAL;
539
540 ret = tz->ops->set_trip_temp(tz, trip, temperature);
541
542 return ret ? ret : count;
543}
544
545static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800546trip_point_temp_show(struct device *dev, struct device_attribute *attr,
547 char *buf)
548{
549 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000550 int trip, ret;
551 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800552
553 if (!tz->ops->get_trip_temp)
554 return -EPERM;
555
556 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
557 return -EINVAL;
558
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000559 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
560
561 if (ret)
562 return ret;
563
564 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800565}
566
Matthew Garrett03a971a2008-12-03 18:00:38 +0000567static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800568trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
569 const char *buf, size_t count)
570{
571 struct thermal_zone_device *tz = to_thermal_zone(dev);
572 int trip, ret;
573 unsigned long temperature;
574
575 if (!tz->ops->set_trip_hyst)
576 return -EPERM;
577
578 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
579 return -EINVAL;
580
581 if (kstrtoul(buf, 10, &temperature))
582 return -EINVAL;
583
584 /*
585 * We are not doing any check on the 'temperature' value
586 * here. The driver implementing 'set_trip_hyst' has to
587 * take care of this.
588 */
589 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
590
591 return ret ? ret : count;
592}
593
594static ssize_t
595trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
596 char *buf)
597{
598 struct thermal_zone_device *tz = to_thermal_zone(dev);
599 int trip, ret;
600 unsigned long temperature;
601
602 if (!tz->ops->get_trip_hyst)
603 return -EPERM;
604
605 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
606 return -EINVAL;
607
608 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
609
610 return ret ? ret : sprintf(buf, "%ld\n", temperature);
611}
612
613static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000614passive_store(struct device *dev, struct device_attribute *attr,
615 const char *buf, size_t count)
616{
617 struct thermal_zone_device *tz = to_thermal_zone(dev);
618 struct thermal_cooling_device *cdev = NULL;
619 int state;
620
621 if (!sscanf(buf, "%d\n", &state))
622 return -EINVAL;
623
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100624 /* sanity check: values below 1000 millicelcius don't make sense
625 * and can cause the system to go into a thermal heart attack
626 */
627 if (state && state < 1000)
628 return -EINVAL;
629
Matthew Garrett03a971a2008-12-03 18:00:38 +0000630 if (state && !tz->forced_passive) {
631 mutex_lock(&thermal_list_lock);
632 list_for_each_entry(cdev, &thermal_cdev_list, node) {
633 if (!strncmp("Processor", cdev->type,
634 sizeof("Processor")))
635 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800636 THERMAL_TRIPS_NONE, cdev,
637 THERMAL_NO_LIMIT,
638 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000639 }
640 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100641 if (!tz->passive_delay)
642 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000643 } else if (!state && tz->forced_passive) {
644 mutex_lock(&thermal_list_lock);
645 list_for_each_entry(cdev, &thermal_cdev_list, node) {
646 if (!strncmp("Processor", cdev->type,
647 sizeof("Processor")))
648 thermal_zone_unbind_cooling_device(tz,
649 THERMAL_TRIPS_NONE,
650 cdev);
651 }
652 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100653 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000654 }
655
Matthew Garrett03a971a2008-12-03 18:00:38 +0000656 tz->forced_passive = state;
657
658 thermal_zone_device_update(tz);
659
660 return count;
661}
662
663static ssize_t
664passive_show(struct device *dev, struct device_attribute *attr,
665 char *buf)
666{
667 struct thermal_zone_device *tz = to_thermal_zone(dev);
668
669 return sprintf(buf, "%d\n", tz->forced_passive);
670}
671
Durgadoss R5a2c0902012-09-18 11:04:58 +0530672static ssize_t
673policy_store(struct device *dev, struct device_attribute *attr,
674 const char *buf, size_t count)
675{
676 int ret = -EINVAL;
677 struct thermal_zone_device *tz = to_thermal_zone(dev);
678 struct thermal_governor *gov;
679
680 mutex_lock(&thermal_governor_lock);
681
682 gov = __find_governor(buf);
683 if (!gov)
684 goto exit;
685
686 tz->governor = gov;
687 ret = count;
688
689exit:
690 mutex_unlock(&thermal_governor_lock);
691 return ret;
692}
693
694static ssize_t
695policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
696{
697 struct thermal_zone_device *tz = to_thermal_zone(dev);
698
699 return sprintf(buf, "%s\n", tz->governor->name);
700}
701
Zhang Rui203d3d42008-01-17 15:51:08 +0800702static DEVICE_ATTR(type, 0444, type_show, NULL);
703static DEVICE_ATTR(temp, 0444, temp_show, NULL);
704static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700705static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530706static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800707
Zhang Rui203d3d42008-01-17 15:51:08 +0800708/* sys I/F for cooling device */
709#define to_cooling_device(_dev) \
710 container_of(_dev, struct thermal_cooling_device, device)
711
712static ssize_t
713thermal_cooling_device_type_show(struct device *dev,
714 struct device_attribute *attr, char *buf)
715{
716 struct thermal_cooling_device *cdev = to_cooling_device(dev);
717
718 return sprintf(buf, "%s\n", cdev->type);
719}
720
721static ssize_t
722thermal_cooling_device_max_state_show(struct device *dev,
723 struct device_attribute *attr, char *buf)
724{
725 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000726 unsigned long state;
727 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800728
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000729 ret = cdev->ops->get_max_state(cdev, &state);
730 if (ret)
731 return ret;
732 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800733}
734
735static ssize_t
736thermal_cooling_device_cur_state_show(struct device *dev,
737 struct device_attribute *attr, char *buf)
738{
739 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000740 unsigned long state;
741 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800742
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000743 ret = cdev->ops->get_cur_state(cdev, &state);
744 if (ret)
745 return ret;
746 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800747}
748
749static ssize_t
750thermal_cooling_device_cur_state_store(struct device *dev,
751 struct device_attribute *attr,
752 const char *buf, size_t count)
753{
754 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000755 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800756 int result;
757
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000758 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800759 return -EINVAL;
760
Roel Kluinedb94912009-12-15 22:46:50 +0100761 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800762 return -EINVAL;
763
764 result = cdev->ops->set_cur_state(cdev, state);
765 if (result)
766 return result;
767 return count;
768}
769
770static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500771__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800772static DEVICE_ATTR(max_state, 0444,
773 thermal_cooling_device_max_state_show, NULL);
774static DEVICE_ATTR(cur_state, 0644,
775 thermal_cooling_device_cur_state_show,
776 thermal_cooling_device_cur_state_store);
777
778static ssize_t
779thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500780 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800781{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800782 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800783
784 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800785 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800786
787 if (instance->trip == THERMAL_TRIPS_NONE)
788 return sprintf(buf, "-1\n");
789 else
790 return sprintf(buf, "%d\n", instance->trip);
791}
792
793/* Device management */
794
Rene Herman16d75232008-06-24 19:38:56 +0200795#if defined(CONFIG_THERMAL_HWMON)
796
Zhang Ruie68b16a2008-04-21 16:07:52 +0800797/* hwmon sys I/F */
798#include <linux/hwmon.h>
Jean Delvare31f53962011-07-28 13:48:42 -0700799
800/* thermal zone devices with the same type share one hwmon device */
801struct thermal_hwmon_device {
802 char type[THERMAL_NAME_LENGTH];
803 struct device *device;
804 int count;
805 struct list_head tz_list;
806 struct list_head node;
807};
808
809struct thermal_hwmon_attr {
810 struct device_attribute attr;
811 char name[16];
812};
813
814/* one temperature input for each thermal zone */
815struct thermal_hwmon_temp {
816 struct list_head hwmon_node;
817 struct thermal_zone_device *tz;
818 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
819 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
820};
821
Zhang Ruie68b16a2008-04-21 16:07:52 +0800822static LIST_HEAD(thermal_hwmon_list);
823
824static ssize_t
825name_show(struct device *dev, struct device_attribute *attr, char *buf)
826{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700827 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800828 return sprintf(buf, "%s\n", hwmon->type);
829}
830static DEVICE_ATTR(name, 0444, name_show, NULL);
831
832static ssize_t
833temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
834{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000835 long temperature;
836 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800837 struct thermal_hwmon_attr *hwmon_attr
838 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700839 struct thermal_hwmon_temp *temp
840 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800841 temp_input);
Jean Delvare31f53962011-07-28 13:48:42 -0700842 struct thermal_zone_device *tz = temp->tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800843
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000844 ret = tz->ops->get_temp(tz, &temperature);
845
846 if (ret)
847 return ret;
848
849 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800850}
851
852static ssize_t
853temp_crit_show(struct device *dev, struct device_attribute *attr,
854 char *buf)
855{
856 struct thermal_hwmon_attr *hwmon_attr
857 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700858 struct thermal_hwmon_temp *temp
859 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800860 temp_crit);
Jean Delvare31f53962011-07-28 13:48:42 -0700861 struct thermal_zone_device *tz = temp->tz;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000862 long temperature;
863 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800864
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000865 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
866 if (ret)
867 return ret;
868
869 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800870}
871
872
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700873static struct thermal_hwmon_device *
874thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
875{
876 struct thermal_hwmon_device *hwmon;
877
878 mutex_lock(&thermal_list_lock);
879 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
880 if (!strcmp(hwmon->type, tz->type)) {
881 mutex_unlock(&thermal_list_lock);
882 return hwmon;
883 }
884 mutex_unlock(&thermal_list_lock);
885
886 return NULL;
887}
888
Jean Delvare31f53962011-07-28 13:48:42 -0700889/* Find the temperature input matching a given thermal zone */
890static struct thermal_hwmon_temp *
891thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
892 const struct thermal_zone_device *tz)
893{
894 struct thermal_hwmon_temp *temp;
895
896 mutex_lock(&thermal_list_lock);
897 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
898 if (temp->tz == tz) {
899 mutex_unlock(&thermal_list_lock);
900 return temp;
901 }
902 mutex_unlock(&thermal_list_lock);
903
904 return NULL;
905}
906
Zhang Ruie68b16a2008-04-21 16:07:52 +0800907static int
908thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
909{
910 struct thermal_hwmon_device *hwmon;
Jean Delvare31f53962011-07-28 13:48:42 -0700911 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800912 int new_hwmon_device = 1;
913 int result;
914
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700915 hwmon = thermal_hwmon_lookup_by_type(tz);
916 if (hwmon) {
917 new_hwmon_device = 0;
918 goto register_sys_interface;
919 }
Zhang Ruie68b16a2008-04-21 16:07:52 +0800920
921 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
922 if (!hwmon)
923 return -ENOMEM;
924
925 INIT_LIST_HEAD(&hwmon->tz_list);
926 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
927 hwmon->device = hwmon_device_register(NULL);
928 if (IS_ERR(hwmon->device)) {
929 result = PTR_ERR(hwmon->device);
930 goto free_mem;
931 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700932 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800933 result = device_create_file(hwmon->device, &dev_attr_name);
934 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530935 goto free_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800936
937 register_sys_interface:
Jean Delvare31f53962011-07-28 13:48:42 -0700938 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
939 if (!temp) {
940 result = -ENOMEM;
941 goto unregister_name;
942 }
943
944 temp->tz = tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800945 hwmon->count++;
946
Guenter Roeck79a49162012-07-21 10:53:48 +1000947 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +0800948 "temp%d_input", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700949 temp->temp_input.attr.attr.name = temp->temp_input.name;
950 temp->temp_input.attr.attr.mode = 0444;
951 temp->temp_input.attr.show = temp_input_show;
952 sysfs_attr_init(&temp->temp_input.attr.attr);
953 result = device_create_file(hwmon->device, &temp->temp_input.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800954 if (result)
Jean Delvare31f53962011-07-28 13:48:42 -0700955 goto free_temp_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800956
957 if (tz->ops->get_crit_temp) {
958 unsigned long temperature;
959 if (!tz->ops->get_crit_temp(tz, &temperature)) {
Guenter Roeck79a49162012-07-21 10:53:48 +1000960 snprintf(temp->temp_crit.name,
961 sizeof(temp->temp_crit.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +0800962 "temp%d_crit", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700963 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
964 temp->temp_crit.attr.attr.mode = 0444;
965 temp->temp_crit.attr.show = temp_crit_show;
966 sysfs_attr_init(&temp->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800967 result = device_create_file(hwmon->device,
Jean Delvare31f53962011-07-28 13:48:42 -0700968 &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800969 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530970 goto unregister_input;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800971 }
972 }
973
974 mutex_lock(&thermal_list_lock);
975 if (new_hwmon_device)
976 list_add_tail(&hwmon->node, &thermal_hwmon_list);
Jean Delvare31f53962011-07-28 13:48:42 -0700977 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800978 mutex_unlock(&thermal_list_lock);
979
980 return 0;
981
Durgadoss Rb299eb52011-03-03 04:30:13 +0530982 unregister_input:
Jean Delvare31f53962011-07-28 13:48:42 -0700983 device_remove_file(hwmon->device, &temp->temp_input.attr);
984 free_temp_mem:
985 kfree(temp);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530986 unregister_name:
Zhang Ruie68b16a2008-04-21 16:07:52 +0800987 if (new_hwmon_device) {
988 device_remove_file(hwmon->device, &dev_attr_name);
989 hwmon_device_unregister(hwmon->device);
990 }
991 free_mem:
992 if (new_hwmon_device)
993 kfree(hwmon);
994
995 return result;
996}
997
998static void
999thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1000{
Jean Delvare31f53962011-07-28 13:48:42 -07001001 struct thermal_hwmon_device *hwmon;
1002 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001003
Jean Delvare31f53962011-07-28 13:48:42 -07001004 hwmon = thermal_hwmon_lookup_by_type(tz);
1005 if (unlikely(!hwmon)) {
1006 /* Should never happen... */
1007 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
1008 return;
1009 }
1010
1011 temp = thermal_hwmon_lookup_temp(hwmon, tz);
1012 if (unlikely(!temp)) {
1013 /* Should never happen... */
1014 dev_dbg(&tz->device, "temperature input lookup failed!\n");
1015 return;
1016 }
1017
1018 device_remove_file(hwmon->device, &temp->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +05301019 if (tz->ops->get_crit_temp)
Jean Delvare31f53962011-07-28 13:48:42 -07001020 device_remove_file(hwmon->device, &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001021
1022 mutex_lock(&thermal_list_lock);
Jean Delvare31f53962011-07-28 13:48:42 -07001023 list_del(&temp->hwmon_node);
1024 kfree(temp);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001025 if (!list_empty(&hwmon->tz_list)) {
1026 mutex_unlock(&thermal_list_lock);
1027 return;
1028 }
1029 list_del(&hwmon->node);
1030 mutex_unlock(&thermal_list_lock);
1031
1032 device_remove_file(hwmon->device, &dev_attr_name);
1033 hwmon_device_unregister(hwmon->device);
1034 kfree(hwmon);
1035}
1036#else
1037static int
1038thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
1039{
1040 return 0;
1041}
1042
1043static void
1044thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1045{
1046}
1047#endif
1048
Zhang Rui203d3d42008-01-17 15:51:08 +08001049/**
1050 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +08001051 * @tz: thermal zone device
1052 * @trip: indicates which trip point the cooling devices is
1053 * associated with in this thermal zone.
1054 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -05001055 *
1056 * This function is usually called in the thermal zone device .bind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +08001057 */
1058int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1059 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +08001060 struct thermal_cooling_device *cdev,
1061 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +08001062{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001063 struct thermal_instance *dev;
1064 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -05001065 struct thermal_zone_device *pos1;
1066 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +08001067 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +08001068 int result;
1069
Len Brown543a9562008-02-07 16:55:08 -05001070 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +08001071 return -EINVAL;
1072
Thomas Sujithc7516702008-02-15 00:58:50 -05001073 list_for_each_entry(pos1, &thermal_tz_list, node) {
1074 if (pos1 == tz)
1075 break;
1076 }
1077 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1078 if (pos2 == cdev)
1079 break;
1080 }
1081
1082 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +08001083 return -EINVAL;
1084
Zhang Rui9d998422012-06-26 16:35:57 +08001085 cdev->ops->get_max_state(cdev, &max_state);
1086
1087 /* lower default 0, upper default max_state */
1088 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1089 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1090
1091 if (lower > upper || upper > max_state)
1092 return -EINVAL;
1093
Zhang Rui203d3d42008-01-17 15:51:08 +08001094 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001095 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001096 if (!dev)
1097 return -ENOMEM;
1098 dev->tz = tz;
1099 dev->cdev = cdev;
1100 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +08001101 dev->upper = upper;
1102 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +08001103 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +08001104
Zhang Rui203d3d42008-01-17 15:51:08 +08001105 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1106 if (result)
1107 goto free_mem;
1108
1109 sprintf(dev->name, "cdev%d", dev->id);
1110 result =
1111 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1112 if (result)
1113 goto release_idr;
1114
1115 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -07001116 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001117 dev->attr.attr.name = dev->attr_name;
1118 dev->attr.attr.mode = 0444;
1119 dev->attr.show = thermal_cooling_device_trip_point_show;
1120 result = device_create_file(&tz->device, &dev->attr);
1121 if (result)
1122 goto remove_symbol_link;
1123
1124 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001125 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001126 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +08001127 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1128 result = -EEXIST;
1129 break;
1130 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001131 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001132 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001133 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1134 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001135 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001136 mutex_unlock(&tz->lock);
1137
1138 if (!result)
1139 return 0;
1140
1141 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -07001142remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001143 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -07001144release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001145 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -07001146free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001147 kfree(dev);
1148 return result;
1149}
1150EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
1151
1152/**
1153 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +08001154 * @tz: thermal zone device
1155 * @trip: indicates which trip point the cooling devices is
1156 * associated with in this thermal zone.
1157 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -05001158 *
1159 * This function is usually called in the thermal zone device .unbind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +08001160 */
1161int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1162 int trip,
1163 struct thermal_cooling_device *cdev)
1164{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001165 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001166
1167 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001168 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001169 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001170 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001171 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001172 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001173 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001174 mutex_unlock(&tz->lock);
1175 goto unbind;
1176 }
1177 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001178 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001179 mutex_unlock(&tz->lock);
1180
1181 return -ENODEV;
1182
Joe Perchescaca8b82012-03-21 12:55:02 -07001183unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +08001184 device_remove_file(&tz->device, &pos->attr);
1185 sysfs_remove_link(&tz->device.kobj, pos->name);
1186 release_idr(&tz->idr, &tz->lock, pos->id);
1187 kfree(pos);
1188 return 0;
1189}
1190EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
1191
1192static void thermal_release(struct device *dev)
1193{
1194 struct thermal_zone_device *tz;
1195 struct thermal_cooling_device *cdev;
1196
Joe Perchescaca8b82012-03-21 12:55:02 -07001197 if (!strncmp(dev_name(dev), "thermal_zone",
1198 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001199 tz = to_thermal_zone(dev);
1200 kfree(tz);
1201 } else {
1202 cdev = to_cooling_device(dev);
1203 kfree(cdev);
1204 }
1205}
1206
1207static struct class thermal_class = {
1208 .name = "thermal",
1209 .dev_release = thermal_release,
1210};
1211
1212/**
1213 * thermal_cooling_device_register - register a new thermal cooling device
1214 * @type: the thermal cooling device type.
1215 * @devdata: device private data.
1216 * @ops: standard thermal cooling devices callbacks.
1217 */
Joe Perchescaca8b82012-03-21 12:55:02 -07001218struct thermal_cooling_device *
1219thermal_cooling_device_register(char *type, void *devdata,
1220 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001221{
1222 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001223 int result;
1224
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001225 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001226 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001227
1228 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001229 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001230 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001231
1232 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1233 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001234 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001235
1236 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1237 if (result) {
1238 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001239 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001240 }
1241
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001242 strcpy(cdev->type, type ? : "");
Zhang Ruif4a821c2012-07-24 16:56:21 +08001243 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001244 INIT_LIST_HEAD(&cdev->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001245 cdev->ops = ops;
Zhang Ruice119f82012-06-27 14:13:04 +08001246 cdev->updated = true;
Zhang Rui203d3d42008-01-17 15:51:08 +08001247 cdev->device.class = &thermal_class;
1248 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001249 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001250 result = device_register(&cdev->device);
1251 if (result) {
1252 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1253 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001254 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001255 }
1256
1257 /* sys I/F */
1258 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001259 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001260 if (result)
1261 goto unregister;
1262 }
1263
1264 result = device_create_file(&cdev->device, &dev_attr_max_state);
1265 if (result)
1266 goto unregister;
1267
1268 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1269 if (result)
1270 goto unregister;
1271
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301272 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001273 mutex_lock(&thermal_list_lock);
1274 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001275 mutex_unlock(&thermal_list_lock);
1276
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301277 /* Update binding information for 'this' new cdev */
1278 bind_cdev(cdev);
1279
1280 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001281
Joe Perchescaca8b82012-03-21 12:55:02 -07001282unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001283 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1284 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001285 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001286}
1287EXPORT_SYMBOL(thermal_cooling_device_register);
1288
1289/**
1290 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001291 * @cdev: the thermal cooling device to remove.
1292 *
1293 * thermal_cooling_device_unregister() must be called when the device is no
1294 * longer needed.
1295 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301296void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001297{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301298 int i;
1299 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001300 struct thermal_zone_device *tz;
1301 struct thermal_cooling_device *pos = NULL;
1302
1303 if (!cdev)
1304 return;
1305
1306 mutex_lock(&thermal_list_lock);
1307 list_for_each_entry(pos, &thermal_cdev_list, node)
1308 if (pos == cdev)
1309 break;
1310 if (pos != cdev) {
1311 /* thermal cooling device not found */
1312 mutex_unlock(&thermal_list_lock);
1313 return;
1314 }
1315 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301316
1317 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001318 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301319 if (tz->ops->unbind) {
1320 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001321 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301322 }
1323
1324 if (!tz->tzp || !tz->tzp->tbp)
1325 continue;
1326
1327 tzp = tz->tzp;
1328 for (i = 0; i < tzp->num_tbps; i++) {
1329 if (tzp->tbp[i].cdev == cdev) {
1330 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1331 tzp->tbp[i].cdev = NULL;
1332 }
1333 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001334 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301335
Zhang Rui203d3d42008-01-17 15:51:08 +08001336 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301337
Zhang Rui203d3d42008-01-17 15:51:08 +08001338 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001339 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001340 device_remove_file(&cdev->device, &dev_attr_max_state);
1341 device_remove_file(&cdev->device, &dev_attr_cur_state);
1342
1343 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1344 device_unregister(&cdev->device);
1345 return;
1346}
1347EXPORT_SYMBOL(thermal_cooling_device_unregister);
1348
Durgadoss Rdc765482012-09-18 11:05:00 +05301349void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001350{
1351 struct thermal_instance *instance;
1352 unsigned long target = 0;
1353
1354 /* cooling device is updated*/
1355 if (cdev->updated)
1356 return;
1357
Zhang Ruif4a821c2012-07-24 16:56:21 +08001358 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001359 /* Make sure cdev enters the deepest cooling state */
1360 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1361 if (instance->target == THERMAL_NO_TARGET)
1362 continue;
1363 if (instance->target > target)
1364 target = instance->target;
1365 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001366 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001367 cdev->ops->set_cur_state(cdev, target);
1368 cdev->updated = true;
1369}
Durgadoss Rdc765482012-09-18 11:05:00 +05301370EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001371
Matthew Garrettb1569e92008-12-03 17:55:32 +00001372/**
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301373 * notify_thermal_framework - Sensor drivers use this API to notify framework
1374 * @tz: thermal zone device
1375 * @trip: indicates which trip point has been crossed
1376 *
1377 * This function handles the trip events from sensor drivers. It starts
1378 * throttling the cooling devices according to the policy configured.
1379 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1380 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1381 * The throttling policy is based on the configured platform data; if no
1382 * platform data is provided, this uses the step_wise throttling policy.
1383 */
1384void notify_thermal_framework(struct thermal_zone_device *tz, int trip)
1385{
1386 handle_thermal_trip(tz, trip);
1387}
1388EXPORT_SYMBOL(notify_thermal_framework);
1389
1390/**
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001391 * create_trip_attrs - create attributes for trip points
1392 * @tz: the thermal zone device
1393 * @mask: Writeable trip point bitmap.
1394 */
1395static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1396{
1397 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001398 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001399
Durgadoss R27365a62012-07-25 10:10:59 +08001400 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001401 if (!tz->trip_type_attrs)
1402 return -ENOMEM;
1403
Durgadoss R27365a62012-07-25 10:10:59 +08001404 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001405 if (!tz->trip_temp_attrs) {
1406 kfree(tz->trip_type_attrs);
1407 return -ENOMEM;
1408 }
1409
Durgadoss R27365a62012-07-25 10:10:59 +08001410 if (tz->ops->get_trip_hyst) {
1411 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1412 if (!tz->trip_hyst_attrs) {
1413 kfree(tz->trip_type_attrs);
1414 kfree(tz->trip_temp_attrs);
1415 return -ENOMEM;
1416 }
1417 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001418
Durgadoss R27365a62012-07-25 10:10:59 +08001419
1420 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001421 /* create trip type attribute */
1422 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1423 "trip_point_%d_type", indx);
1424
1425 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1426 tz->trip_type_attrs[indx].attr.attr.name =
1427 tz->trip_type_attrs[indx].name;
1428 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1429 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1430
1431 device_create_file(&tz->device,
1432 &tz->trip_type_attrs[indx].attr);
1433
1434 /* create trip temp attribute */
1435 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1436 "trip_point_%d_temp", indx);
1437
1438 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1439 tz->trip_temp_attrs[indx].attr.attr.name =
1440 tz->trip_temp_attrs[indx].name;
1441 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1442 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1443 if (mask & (1 << indx)) {
1444 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1445 tz->trip_temp_attrs[indx].attr.store =
1446 trip_point_temp_store;
1447 }
1448
1449 device_create_file(&tz->device,
1450 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001451
1452 /* create Optional trip hyst attribute */
1453 if (!tz->ops->get_trip_hyst)
1454 continue;
1455 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1456 "trip_point_%d_hyst", indx);
1457
1458 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1459 tz->trip_hyst_attrs[indx].attr.attr.name =
1460 tz->trip_hyst_attrs[indx].name;
1461 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1462 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1463 if (tz->ops->set_trip_hyst) {
1464 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1465 tz->trip_hyst_attrs[indx].attr.store =
1466 trip_point_hyst_store;
1467 }
1468
1469 device_create_file(&tz->device,
1470 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001471 }
1472 return 0;
1473}
1474
1475static void remove_trip_attrs(struct thermal_zone_device *tz)
1476{
1477 int indx;
1478
1479 for (indx = 0; indx < tz->trips; indx++) {
1480 device_remove_file(&tz->device,
1481 &tz->trip_type_attrs[indx].attr);
1482 device_remove_file(&tz->device,
1483 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001484 if (tz->ops->get_trip_hyst)
1485 device_remove_file(&tz->device,
1486 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001487 }
1488 kfree(tz->trip_type_attrs);
1489 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001490 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001491}
1492
1493/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001494 * thermal_zone_device_register - register a new thermal zone device
1495 * @type: the thermal zone device type
1496 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001497 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001498 * @devdata: private device data
1499 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301500 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001501 * @passive_delay: number of milliseconds to wait between polls when
1502 * performing passive cooling
1503 * @polling_delay: number of milliseconds to wait between polls when checking
1504 * whether trip points have been crossed (0 for interrupt
1505 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001506 *
1507 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001508 * longer needed. The passive cooling depends on the .get_trend() return value.
Zhang Rui203d3d42008-01-17 15:51:08 +08001509 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001510struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001511 int trips, int mask, void *devdata,
Alan Cox5b275ce2010-11-11 15:27:29 +00001512 const struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301513 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001514 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001515{
1516 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001517 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001518 int result;
1519 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001520 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001521
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001522 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001523 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001524
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001525 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001526 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001527
1528 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001529 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001530
1531 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1532 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001533 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001534
Zhang Rui2d374132012-06-27 10:09:00 +08001535 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001536 idr_init(&tz->idr);
1537 mutex_init(&tz->lock);
1538 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1539 if (result) {
1540 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001541 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001542 }
1543
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001544 strcpy(tz->type, type ? : "");
Zhang Rui203d3d42008-01-17 15:51:08 +08001545 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301546 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001547 tz->device.class = &thermal_class;
1548 tz->devdata = devdata;
1549 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001550 tz->passive_delay = passive_delay;
1551 tz->polling_delay = polling_delay;
1552
Kay Sievers354655e2009-01-06 10:44:37 -08001553 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001554 result = device_register(&tz->device);
1555 if (result) {
1556 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1557 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001558 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001559 }
1560
1561 /* sys I/F */
1562 if (type) {
1563 result = device_create_file(&tz->device, &dev_attr_type);
1564 if (result)
1565 goto unregister;
1566 }
1567
1568 result = device_create_file(&tz->device, &dev_attr_temp);
1569 if (result)
1570 goto unregister;
1571
1572 if (ops->get_mode) {
1573 result = device_create_file(&tz->device, &dev_attr_mode);
1574 if (result)
1575 goto unregister;
1576 }
1577
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001578 result = create_trip_attrs(tz, mask);
1579 if (result)
1580 goto unregister;
1581
Zhang Rui203d3d42008-01-17 15:51:08 +08001582 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001583 tz->ops->get_trip_type(tz, count, &trip_type);
1584 if (trip_type == THERMAL_TRIP_PASSIVE)
1585 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001586 }
1587
Durgadoss R5a2c0902012-09-18 11:04:58 +05301588 if (!passive) {
1589 result = device_create_file(&tz->device, &dev_attr_passive);
1590 if (result)
1591 goto unregister;
1592 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001593
Durgadoss R5a2c0902012-09-18 11:04:58 +05301594 /* Create policy attribute */
1595 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001596 if (result)
1597 goto unregister;
1598
Durgadoss Ra4a15482012-09-18 11:04:57 +05301599 /* Update 'this' zone's governor information */
1600 mutex_lock(&thermal_governor_lock);
1601
1602 if (tz->tzp)
1603 tz->governor = __find_governor(tz->tzp->governor_name);
1604 else
1605 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1606
1607 mutex_unlock(&thermal_governor_lock);
1608
Zhang Ruie68b16a2008-04-21 16:07:52 +08001609 result = thermal_add_hwmon_sysfs(tz);
1610 if (result)
1611 goto unregister;
1612
Zhang Rui203d3d42008-01-17 15:51:08 +08001613 mutex_lock(&thermal_list_lock);
1614 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001615 mutex_unlock(&thermal_list_lock);
1616
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301617 /* Bind cooling devices for this zone */
1618 bind_tz(tz);
1619
Matthew Garrettb1569e92008-12-03 17:55:32 +00001620 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1621
1622 thermal_zone_device_update(tz);
1623
Zhang Rui203d3d42008-01-17 15:51:08 +08001624 if (!result)
1625 return tz;
1626
Joe Perchescaca8b82012-03-21 12:55:02 -07001627unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001628 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1629 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001630 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001631}
1632EXPORT_SYMBOL(thermal_zone_device_register);
1633
1634/**
1635 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001636 * @tz: the thermal zone device to remove
1637 */
1638void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1639{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301640 int i;
1641 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001642 struct thermal_cooling_device *cdev;
1643 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001644
1645 if (!tz)
1646 return;
1647
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301648 tzp = tz->tzp;
1649
Zhang Rui203d3d42008-01-17 15:51:08 +08001650 mutex_lock(&thermal_list_lock);
1651 list_for_each_entry(pos, &thermal_tz_list, node)
1652 if (pos == tz)
1653 break;
1654 if (pos != tz) {
1655 /* thermal zone device not found */
1656 mutex_unlock(&thermal_list_lock);
1657 return;
1658 }
1659 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301660
1661 /* Unbind all cdevs associated with 'this' thermal zone */
1662 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1663 if (tz->ops->unbind) {
1664 tz->ops->unbind(tz, cdev);
1665 continue;
1666 }
1667
1668 if (!tzp || !tzp->tbp)
1669 break;
1670
1671 for (i = 0; i < tzp->num_tbps; i++) {
1672 if (tzp->tbp[i].cdev == cdev) {
1673 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1674 tzp->tbp[i].cdev = NULL;
1675 }
1676 }
1677 }
1678
Zhang Rui203d3d42008-01-17 15:51:08 +08001679 mutex_unlock(&thermal_list_lock);
1680
Matthew Garrettb1569e92008-12-03 17:55:32 +00001681 thermal_zone_device_set_polling(tz, 0);
1682
Zhang Rui203d3d42008-01-17 15:51:08 +08001683 if (tz->type[0])
1684 device_remove_file(&tz->device, &dev_attr_type);
1685 device_remove_file(&tz->device, &dev_attr_temp);
1686 if (tz->ops->get_mode)
1687 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301688 device_remove_file(&tz->device, &dev_attr_policy);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001689 remove_trip_attrs(tz);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301690 tz->governor = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001691
Zhang Ruie68b16a2008-04-21 16:07:52 +08001692 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001693 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1694 idr_destroy(&tz->idr);
1695 mutex_destroy(&tz->lock);
1696 device_unregister(&tz->device);
1697 return;
1698}
Zhang Rui203d3d42008-01-17 15:51:08 +08001699EXPORT_SYMBOL(thermal_zone_device_unregister);
1700
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001701#ifdef CONFIG_NET
1702static struct genl_family thermal_event_genl_family = {
1703 .id = GENL_ID_GENERATE,
1704 .name = THERMAL_GENL_FAMILY_NAME,
1705 .version = THERMAL_GENL_VERSION,
1706 .maxattr = THERMAL_GENL_ATTR_MAX,
1707};
1708
1709static struct genl_multicast_group thermal_event_mcgrp = {
1710 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1711};
1712
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001713int thermal_generate_netlink_event(u32 orig, enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301714{
1715 struct sk_buff *skb;
1716 struct nlattr *attr;
1717 struct thermal_genl_event *thermal_event;
1718 void *msg_header;
1719 int size;
1720 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001721 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301722
1723 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001724 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1725 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301726
1727 skb = genlmsg_new(size, GFP_ATOMIC);
1728 if (!skb)
1729 return -ENOMEM;
1730
1731 /* add the genetlink message header */
1732 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1733 &thermal_event_genl_family, 0,
1734 THERMAL_GENL_CMD_EVENT);
1735 if (!msg_header) {
1736 nlmsg_free(skb);
1737 return -ENOMEM;
1738 }
1739
1740 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001741 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1742 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301743
1744 if (!attr) {
1745 nlmsg_free(skb);
1746 return -EINVAL;
1747 }
1748
1749 thermal_event = nla_data(attr);
1750 if (!thermal_event) {
1751 nlmsg_free(skb);
1752 return -EINVAL;
1753 }
1754
1755 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1756
1757 thermal_event->orig = orig;
1758 thermal_event->event = event;
1759
1760 /* send multicast genetlink message */
1761 result = genlmsg_end(skb, msg_header);
1762 if (result < 0) {
1763 nlmsg_free(skb);
1764 return result;
1765 }
1766
1767 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1768 if (result)
Joe Perchesc5a01dd2012-03-21 12:55:02 -07001769 pr_info("failed to send netlink event:%d\n", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301770
1771 return result;
1772}
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001773EXPORT_SYMBOL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301774
1775static int genetlink_init(void)
1776{
1777 int result;
1778
1779 result = genl_register_family(&thermal_event_genl_family);
1780 if (result)
1781 return result;
1782
1783 result = genl_register_mc_group(&thermal_event_genl_family,
1784 &thermal_event_mcgrp);
1785 if (result)
1786 genl_unregister_family(&thermal_event_genl_family);
1787 return result;
1788}
1789
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001790static void genetlink_exit(void)
1791{
1792 genl_unregister_family(&thermal_event_genl_family);
1793}
1794#else /* !CONFIG_NET */
1795static inline int genetlink_init(void) { return 0; }
1796static inline void genetlink_exit(void) {}
1797#endif /* !CONFIG_NET */
1798
Zhang Rui203d3d42008-01-17 15:51:08 +08001799static int __init thermal_init(void)
1800{
1801 int result = 0;
1802
1803 result = class_register(&thermal_class);
1804 if (result) {
1805 idr_destroy(&thermal_tz_idr);
1806 idr_destroy(&thermal_cdev_idr);
1807 mutex_destroy(&thermal_idr_lock);
1808 mutex_destroy(&thermal_list_lock);
1809 }
R.Durgadoss4cb18722010-10-27 03:33:29 +05301810 result = genetlink_init();
Zhang Rui203d3d42008-01-17 15:51:08 +08001811 return result;
1812}
1813
1814static void __exit thermal_exit(void)
1815{
1816 class_unregister(&thermal_class);
1817 idr_destroy(&thermal_tz_idr);
1818 idr_destroy(&thermal_cdev_idr);
1819 mutex_destroy(&thermal_idr_lock);
1820 mutex_destroy(&thermal_list_lock);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301821 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001822}
1823
R.Durgadoss4cb18722010-10-27 03:33:29 +05301824fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001825module_exit(thermal_exit);