blob: fba27c36d707130dc9baee674d75482625ef25c8 [file] [log] [blame]
Zhang Rui203d3d42008-01-17 15:51:08 +08001/*
2 * thermal.c - Generic Thermal Management Sysfs support.
3 *
4 * Copyright (C) 2008 Intel Corp
5 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
6 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
7 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
25
Joe Perchesc5a01dd2012-03-21 12:55:02 -070026#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
Zhang Rui203d3d42008-01-17 15:51:08 +080028#include <linux/module.h>
29#include <linux/device.h>
30#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080032#include <linux/kdev_t.h>
33#include <linux/idr.h>
34#include <linux/thermal.h>
Matthew Garrettb1569e92008-12-03 17:55:32 +000035#include <linux/reboot.h>
R.Durgadoss4cb18722010-10-27 03:33:29 +053036#include <net/netlink.h>
37#include <net/genetlink.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080038
Durgadoss R71350db2012-09-18 11:04:53 +053039#include "thermal_core.h"
40
Zhang Rui63c4ec92008-04-21 16:07:13 +080041MODULE_AUTHOR("Zhang Rui");
Zhang Rui203d3d42008-01-17 15:51:08 +080042MODULE_DESCRIPTION("Generic thermal management sysfs support");
43MODULE_LICENSE("GPL");
44
Zhang Rui203d3d42008-01-17 15:51:08 +080045static DEFINE_IDR(thermal_tz_idr);
46static DEFINE_IDR(thermal_cdev_idr);
47static DEFINE_MUTEX(thermal_idr_lock);
48
49static LIST_HEAD(thermal_tz_list);
50static LIST_HEAD(thermal_cdev_list);
Durgadoss Ra4a15482012-09-18 11:04:57 +053051static LIST_HEAD(thermal_governor_list);
52
Zhang Rui203d3d42008-01-17 15:51:08 +080053static DEFINE_MUTEX(thermal_list_lock);
Durgadoss Ra4a15482012-09-18 11:04:57 +053054static DEFINE_MUTEX(thermal_governor_lock);
55
56static struct thermal_governor *__find_governor(const char *name)
57{
58 struct thermal_governor *pos;
59
60 list_for_each_entry(pos, &thermal_governor_list, governor_list)
61 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
62 return pos;
63
64 return NULL;
65}
66
67int thermal_register_governor(struct thermal_governor *governor)
68{
69 int err;
70 const char *name;
71 struct thermal_zone_device *pos;
72
73 if (!governor)
74 return -EINVAL;
75
76 mutex_lock(&thermal_governor_lock);
77
78 err = -EBUSY;
79 if (__find_governor(governor->name) == NULL) {
80 err = 0;
81 list_add(&governor->governor_list, &thermal_governor_list);
82 }
83
84 mutex_lock(&thermal_list_lock);
85
86 list_for_each_entry(pos, &thermal_tz_list, node) {
87 if (pos->governor)
88 continue;
89 if (pos->tzp)
90 name = pos->tzp->governor_name;
91 else
92 name = DEFAULT_THERMAL_GOVERNOR;
93 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
94 pos->governor = governor;
95 }
96
97 mutex_unlock(&thermal_list_lock);
98 mutex_unlock(&thermal_governor_lock);
99
100 return err;
101}
102EXPORT_SYMBOL_GPL(thermal_register_governor);
103
104void thermal_unregister_governor(struct thermal_governor *governor)
105{
106 struct thermal_zone_device *pos;
107
108 if (!governor)
109 return;
110
111 mutex_lock(&thermal_governor_lock);
112
113 if (__find_governor(governor->name) == NULL)
114 goto exit;
115
116 mutex_lock(&thermal_list_lock);
117
118 list_for_each_entry(pos, &thermal_tz_list, node) {
119 if (!strnicmp(pos->governor->name, governor->name,
120 THERMAL_NAME_LENGTH))
121 pos->governor = NULL;
122 }
123
124 mutex_unlock(&thermal_list_lock);
125 list_del(&governor->governor_list);
126exit:
127 mutex_unlock(&thermal_governor_lock);
128 return;
129}
130EXPORT_SYMBOL_GPL(thermal_unregister_governor);
Zhang Rui203d3d42008-01-17 15:51:08 +0800131
132static int get_idr(struct idr *idr, struct mutex *lock, int *id)
133{
134 int err;
135
Joe Perchescaca8b82012-03-21 12:55:02 -0700136again:
Zhang Rui203d3d42008-01-17 15:51:08 +0800137 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
138 return -ENOMEM;
139
140 if (lock)
141 mutex_lock(lock);
142 err = idr_get_new(idr, NULL, id);
143 if (lock)
144 mutex_unlock(lock);
145 if (unlikely(err == -EAGAIN))
146 goto again;
147 else if (unlikely(err))
148 return err;
149
Fengguang Wu125c4c72012-10-04 17:13:15 -0700150 *id = *id & MAX_IDR_MASK;
Zhang Rui203d3d42008-01-17 15:51:08 +0800151 return 0;
152}
153
154static void release_idr(struct idr *idr, struct mutex *lock, int id)
155{
156 if (lock)
157 mutex_lock(lock);
158 idr_remove(idr, id);
159 if (lock)
160 mutex_unlock(lock);
161}
162
Durgadoss R9b4298a2012-09-18 11:04:54 +0530163int get_tz_trend(struct thermal_zone_device *tz, int trip)
164{
165 enum thermal_trend trend;
166
167 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
168 if (tz->temperature > tz->last_temperature)
169 trend = THERMAL_TREND_RAISING;
170 else if (tz->temperature < tz->last_temperature)
171 trend = THERMAL_TREND_DROPPING;
172 else
173 trend = THERMAL_TREND_STABLE;
174 }
175
176 return trend;
177}
178EXPORT_SYMBOL(get_tz_trend);
179
180struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
181 struct thermal_cooling_device *cdev, int trip)
182{
183 struct thermal_instance *pos = NULL;
184 struct thermal_instance *target_instance = NULL;
185
186 mutex_lock(&tz->lock);
187 mutex_lock(&cdev->lock);
188
189 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
190 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
191 target_instance = pos;
192 break;
193 }
194 }
195
196 mutex_unlock(&cdev->lock);
197 mutex_unlock(&tz->lock);
198
199 return target_instance;
200}
201EXPORT_SYMBOL(get_thermal_instance);
202
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530203static void print_bind_err_msg(struct thermal_zone_device *tz,
204 struct thermal_cooling_device *cdev, int ret)
205{
206 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
207 tz->type, cdev->type, ret);
208}
209
210static void __bind(struct thermal_zone_device *tz, int mask,
211 struct thermal_cooling_device *cdev)
212{
213 int i, ret;
214
215 for (i = 0; i < tz->trips; i++) {
216 if (mask & (1 << i)) {
217 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
218 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
219 if (ret)
220 print_bind_err_msg(tz, cdev, ret);
221 }
222 }
223}
224
225static void __unbind(struct thermal_zone_device *tz, int mask,
226 struct thermal_cooling_device *cdev)
227{
228 int i;
229
230 for (i = 0; i < tz->trips; i++)
231 if (mask & (1 << i))
232 thermal_zone_unbind_cooling_device(tz, i, cdev);
233}
234
235static void bind_cdev(struct thermal_cooling_device *cdev)
236{
237 int i, ret;
238 const struct thermal_zone_params *tzp;
239 struct thermal_zone_device *pos = NULL;
240
241 mutex_lock(&thermal_list_lock);
242
243 list_for_each_entry(pos, &thermal_tz_list, node) {
244 if (!pos->tzp && !pos->ops->bind)
245 continue;
246
247 if (!pos->tzp && pos->ops->bind) {
248 ret = pos->ops->bind(pos, cdev);
249 if (ret)
250 print_bind_err_msg(pos, cdev, ret);
251 }
252
253 tzp = pos->tzp;
Hugh Dickins791700c2012-09-27 16:48:28 -0700254 if (!tzp || !tzp->tbp)
255 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530256
257 for (i = 0; i < tzp->num_tbps; i++) {
258 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
259 continue;
260 if (tzp->tbp[i].match(pos, cdev))
261 continue;
262 tzp->tbp[i].cdev = cdev;
263 __bind(pos, tzp->tbp[i].trip_mask, cdev);
264 }
265 }
266
267 mutex_unlock(&thermal_list_lock);
268}
269
270static void bind_tz(struct thermal_zone_device *tz)
271{
272 int i, ret;
273 struct thermal_cooling_device *pos = NULL;
274 const struct thermal_zone_params *tzp = tz->tzp;
275
276 if (!tzp && !tz->ops->bind)
277 return;
278
279 mutex_lock(&thermal_list_lock);
280
281 /* If there is no platform data, try to use ops->bind */
282 if (!tzp && tz->ops->bind) {
283 list_for_each_entry(pos, &thermal_cdev_list, node) {
284 ret = tz->ops->bind(tz, pos);
285 if (ret)
286 print_bind_err_msg(tz, pos, ret);
287 }
288 goto exit;
289 }
290
Hugh Dickins791700c2012-09-27 16:48:28 -0700291 if (!tzp || !tzp->tbp)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530292 goto exit;
293
294 list_for_each_entry(pos, &thermal_cdev_list, node) {
295 for (i = 0; i < tzp->num_tbps; i++) {
296 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
297 continue;
298 if (tzp->tbp[i].match(tz, pos))
299 continue;
300 tzp->tbp[i].cdev = pos;
301 __bind(tz, tzp->tbp[i].trip_mask, pos);
302 }
303 }
304exit:
305 mutex_unlock(&thermal_list_lock);
306}
307
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530308static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
309 int delay)
310{
311 if (delay > 1000)
312 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
313 round_jiffies(msecs_to_jiffies(delay)));
314 else if (delay)
315 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
316 msecs_to_jiffies(delay));
317 else
318 cancel_delayed_work(&tz->poll_queue);
319}
320
321static void monitor_thermal_zone(struct thermal_zone_device *tz)
322{
323 mutex_lock(&tz->lock);
324
325 if (tz->passive)
326 thermal_zone_device_set_polling(tz, tz->passive_delay);
327 else if (tz->polling_delay)
328 thermal_zone_device_set_polling(tz, tz->polling_delay);
329 else
330 thermal_zone_device_set_polling(tz, 0);
331
332 mutex_unlock(&tz->lock);
333}
334
335static void handle_non_critical_trips(struct thermal_zone_device *tz,
336 int trip, enum thermal_trip_type trip_type)
337{
Zhang Ruid567c682012-12-12 15:23:54 +0800338 if (tz->governor)
339 tz->governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530340}
341
342static void handle_critical_trips(struct thermal_zone_device *tz,
343 int trip, enum thermal_trip_type trip_type)
344{
345 long trip_temp;
346
347 tz->ops->get_trip_temp(tz, trip, &trip_temp);
348
349 /* If we have not crossed the trip_temp, we do not care. */
350 if (tz->temperature < trip_temp)
351 return;
352
353 if (tz->ops->notify)
354 tz->ops->notify(tz, trip, trip_type);
355
356 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000357 dev_emerg(&tz->device,
358 "critical temperature reached(%d C),shutting down\n",
359 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530360 orderly_poweroff(true);
361 }
362}
363
364static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
365{
366 enum thermal_trip_type type;
367
368 tz->ops->get_trip_type(tz, trip, &type);
369
370 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
371 handle_critical_trips(tz, trip, type);
372 else
373 handle_non_critical_trips(tz, trip, type);
374 /*
375 * Alright, we handled this trip successfully.
376 * So, start monitoring again.
377 */
378 monitor_thermal_zone(tz);
379}
380
381static void update_temperature(struct thermal_zone_device *tz)
382{
383 long temp;
384 int ret;
385
386 mutex_lock(&tz->lock);
387
388 ret = tz->ops->get_temp(tz, &temp);
389 if (ret) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000390 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
391 tz->id);
Hugh Dickins791700c2012-09-27 16:48:28 -0700392 goto exit;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530393 }
394
395 tz->last_temperature = tz->temperature;
396 tz->temperature = temp;
397
Hugh Dickins791700c2012-09-27 16:48:28 -0700398exit:
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530399 mutex_unlock(&tz->lock);
400}
401
402void thermal_zone_device_update(struct thermal_zone_device *tz)
403{
404 int count;
405
406 update_temperature(tz);
407
408 for (count = 0; count < tz->trips; count++)
409 handle_thermal_trip(tz, count);
410}
411EXPORT_SYMBOL(thermal_zone_device_update);
412
413static void thermal_zone_device_check(struct work_struct *work)
414{
415 struct thermal_zone_device *tz = container_of(work, struct
416 thermal_zone_device,
417 poll_queue.work);
418 thermal_zone_device_update(tz);
419}
420
Zhang Rui203d3d42008-01-17 15:51:08 +0800421/* sys I/F for thermal zone */
422
423#define to_thermal_zone(_dev) \
424 container_of(_dev, struct thermal_zone_device, device)
425
426static ssize_t
427type_show(struct device *dev, struct device_attribute *attr, char *buf)
428{
429 struct thermal_zone_device *tz = to_thermal_zone(dev);
430
431 return sprintf(buf, "%s\n", tz->type);
432}
433
434static ssize_t
435temp_show(struct device *dev, struct device_attribute *attr, char *buf)
436{
437 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000438 long temperature;
439 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800440
441 if (!tz->ops->get_temp)
442 return -EPERM;
443
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000444 ret = tz->ops->get_temp(tz, &temperature);
445
446 if (ret)
447 return ret;
448
449 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800450}
451
452static ssize_t
453mode_show(struct device *dev, struct device_attribute *attr, char *buf)
454{
455 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000456 enum thermal_device_mode mode;
457 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800458
459 if (!tz->ops->get_mode)
460 return -EPERM;
461
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000462 result = tz->ops->get_mode(tz, &mode);
463 if (result)
464 return result;
465
466 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
467 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800468}
469
470static ssize_t
471mode_store(struct device *dev, struct device_attribute *attr,
472 const char *buf, size_t count)
473{
474 struct thermal_zone_device *tz = to_thermal_zone(dev);
475 int result;
476
477 if (!tz->ops->set_mode)
478 return -EPERM;
479
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530480 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000481 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530482 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000483 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
484 else
485 result = -EINVAL;
486
Zhang Rui203d3d42008-01-17 15:51:08 +0800487 if (result)
488 return result;
489
490 return count;
491}
492
493static ssize_t
494trip_point_type_show(struct device *dev, struct device_attribute *attr,
495 char *buf)
496{
497 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000498 enum thermal_trip_type type;
499 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800500
501 if (!tz->ops->get_trip_type)
502 return -EPERM;
503
504 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
505 return -EINVAL;
506
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000507 result = tz->ops->get_trip_type(tz, trip, &type);
508 if (result)
509 return result;
510
511 switch (type) {
512 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300513 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000514 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300515 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000516 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300517 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000518 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300519 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000520 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300521 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000522 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800523}
524
525static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800526trip_point_temp_store(struct device *dev, struct device_attribute *attr,
527 const char *buf, size_t count)
528{
529 struct thermal_zone_device *tz = to_thermal_zone(dev);
530 int trip, ret;
531 unsigned long temperature;
532
533 if (!tz->ops->set_trip_temp)
534 return -EPERM;
535
536 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
537 return -EINVAL;
538
539 if (kstrtoul(buf, 10, &temperature))
540 return -EINVAL;
541
542 ret = tz->ops->set_trip_temp(tz, trip, temperature);
543
544 return ret ? ret : count;
545}
546
547static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800548trip_point_temp_show(struct device *dev, struct device_attribute *attr,
549 char *buf)
550{
551 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000552 int trip, ret;
553 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800554
555 if (!tz->ops->get_trip_temp)
556 return -EPERM;
557
558 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
559 return -EINVAL;
560
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000561 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
562
563 if (ret)
564 return ret;
565
566 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800567}
568
Matthew Garrett03a971a2008-12-03 18:00:38 +0000569static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800570trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
571 const char *buf, size_t count)
572{
573 struct thermal_zone_device *tz = to_thermal_zone(dev);
574 int trip, ret;
575 unsigned long temperature;
576
577 if (!tz->ops->set_trip_hyst)
578 return -EPERM;
579
580 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
581 return -EINVAL;
582
583 if (kstrtoul(buf, 10, &temperature))
584 return -EINVAL;
585
586 /*
587 * We are not doing any check on the 'temperature' value
588 * here. The driver implementing 'set_trip_hyst' has to
589 * take care of this.
590 */
591 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
592
593 return ret ? ret : count;
594}
595
596static ssize_t
597trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
598 char *buf)
599{
600 struct thermal_zone_device *tz = to_thermal_zone(dev);
601 int trip, ret;
602 unsigned long temperature;
603
604 if (!tz->ops->get_trip_hyst)
605 return -EPERM;
606
607 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
608 return -EINVAL;
609
610 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
611
612 return ret ? ret : sprintf(buf, "%ld\n", temperature);
613}
614
615static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000616passive_store(struct device *dev, struct device_attribute *attr,
617 const char *buf, size_t count)
618{
619 struct thermal_zone_device *tz = to_thermal_zone(dev);
620 struct thermal_cooling_device *cdev = NULL;
621 int state;
622
623 if (!sscanf(buf, "%d\n", &state))
624 return -EINVAL;
625
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100626 /* sanity check: values below 1000 millicelcius don't make sense
627 * and can cause the system to go into a thermal heart attack
628 */
629 if (state && state < 1000)
630 return -EINVAL;
631
Matthew Garrett03a971a2008-12-03 18:00:38 +0000632 if (state && !tz->forced_passive) {
633 mutex_lock(&thermal_list_lock);
634 list_for_each_entry(cdev, &thermal_cdev_list, node) {
635 if (!strncmp("Processor", cdev->type,
636 sizeof("Processor")))
637 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800638 THERMAL_TRIPS_NONE, cdev,
639 THERMAL_NO_LIMIT,
640 THERMAL_NO_LIMIT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000641 }
642 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100643 if (!tz->passive_delay)
644 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000645 } else if (!state && tz->forced_passive) {
646 mutex_lock(&thermal_list_lock);
647 list_for_each_entry(cdev, &thermal_cdev_list, node) {
648 if (!strncmp("Processor", cdev->type,
649 sizeof("Processor")))
650 thermal_zone_unbind_cooling_device(tz,
651 THERMAL_TRIPS_NONE,
652 cdev);
653 }
654 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100655 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000656 }
657
Matthew Garrett03a971a2008-12-03 18:00:38 +0000658 tz->forced_passive = state;
659
660 thermal_zone_device_update(tz);
661
662 return count;
663}
664
665static ssize_t
666passive_show(struct device *dev, struct device_attribute *attr,
667 char *buf)
668{
669 struct thermal_zone_device *tz = to_thermal_zone(dev);
670
671 return sprintf(buf, "%d\n", tz->forced_passive);
672}
673
Durgadoss R5a2c0902012-09-18 11:04:58 +0530674static ssize_t
675policy_store(struct device *dev, struct device_attribute *attr,
676 const char *buf, size_t count)
677{
678 int ret = -EINVAL;
679 struct thermal_zone_device *tz = to_thermal_zone(dev);
680 struct thermal_governor *gov;
681
682 mutex_lock(&thermal_governor_lock);
683
684 gov = __find_governor(buf);
685 if (!gov)
686 goto exit;
687
688 tz->governor = gov;
689 ret = count;
690
691exit:
692 mutex_unlock(&thermal_governor_lock);
693 return ret;
694}
695
696static ssize_t
697policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
698{
699 struct thermal_zone_device *tz = to_thermal_zone(dev);
700
701 return sprintf(buf, "%s\n", tz->governor->name);
702}
703
Zhang Rui203d3d42008-01-17 15:51:08 +0800704static DEVICE_ATTR(type, 0444, type_show, NULL);
705static DEVICE_ATTR(temp, 0444, temp_show, NULL);
706static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -0700707static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530708static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Zhang Rui203d3d42008-01-17 15:51:08 +0800709
Zhang Rui203d3d42008-01-17 15:51:08 +0800710/* sys I/F for cooling device */
711#define to_cooling_device(_dev) \
712 container_of(_dev, struct thermal_cooling_device, device)
713
714static ssize_t
715thermal_cooling_device_type_show(struct device *dev,
716 struct device_attribute *attr, char *buf)
717{
718 struct thermal_cooling_device *cdev = to_cooling_device(dev);
719
720 return sprintf(buf, "%s\n", cdev->type);
721}
722
723static ssize_t
724thermal_cooling_device_max_state_show(struct device *dev,
725 struct device_attribute *attr, char *buf)
726{
727 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000728 unsigned long state;
729 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800730
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000731 ret = cdev->ops->get_max_state(cdev, &state);
732 if (ret)
733 return ret;
734 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800735}
736
737static ssize_t
738thermal_cooling_device_cur_state_show(struct device *dev,
739 struct device_attribute *attr, char *buf)
740{
741 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000742 unsigned long state;
743 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800744
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000745 ret = cdev->ops->get_cur_state(cdev, &state);
746 if (ret)
747 return ret;
748 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +0800749}
750
751static ssize_t
752thermal_cooling_device_cur_state_store(struct device *dev,
753 struct device_attribute *attr,
754 const char *buf, size_t count)
755{
756 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000757 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +0800758 int result;
759
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000760 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +0800761 return -EINVAL;
762
Roel Kluinedb94912009-12-15 22:46:50 +0100763 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800764 return -EINVAL;
765
766 result = cdev->ops->set_cur_state(cdev, state);
767 if (result)
768 return result;
769 return count;
770}
771
772static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -0500773__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800774static DEVICE_ATTR(max_state, 0444,
775 thermal_cooling_device_max_state_show, NULL);
776static DEVICE_ATTR(cur_state, 0644,
777 thermal_cooling_device_cur_state_show,
778 thermal_cooling_device_cur_state_store);
779
780static ssize_t
781thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -0500782 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +0800783{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800784 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +0800785
786 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800787 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800788
789 if (instance->trip == THERMAL_TRIPS_NONE)
790 return sprintf(buf, "-1\n");
791 else
792 return sprintf(buf, "%d\n", instance->trip);
793}
794
795/* Device management */
796
Rene Herman16d75232008-06-24 19:38:56 +0200797#if defined(CONFIG_THERMAL_HWMON)
798
Zhang Ruie68b16a2008-04-21 16:07:52 +0800799/* hwmon sys I/F */
800#include <linux/hwmon.h>
Jean Delvare31f53962011-07-28 13:48:42 -0700801
802/* thermal zone devices with the same type share one hwmon device */
803struct thermal_hwmon_device {
804 char type[THERMAL_NAME_LENGTH];
805 struct device *device;
806 int count;
807 struct list_head tz_list;
808 struct list_head node;
809};
810
811struct thermal_hwmon_attr {
812 struct device_attribute attr;
813 char name[16];
814};
815
816/* one temperature input for each thermal zone */
817struct thermal_hwmon_temp {
818 struct list_head hwmon_node;
819 struct thermal_zone_device *tz;
820 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
821 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
822};
823
Zhang Ruie68b16a2008-04-21 16:07:52 +0800824static LIST_HEAD(thermal_hwmon_list);
825
826static ssize_t
827name_show(struct device *dev, struct device_attribute *attr, char *buf)
828{
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700829 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800830 return sprintf(buf, "%s\n", hwmon->type);
831}
832static DEVICE_ATTR(name, 0444, name_show, NULL);
833
834static ssize_t
835temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
836{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000837 long temperature;
838 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800839 struct thermal_hwmon_attr *hwmon_attr
840 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700841 struct thermal_hwmon_temp *temp
842 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800843 temp_input);
Jean Delvare31f53962011-07-28 13:48:42 -0700844 struct thermal_zone_device *tz = temp->tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800845
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000846 ret = tz->ops->get_temp(tz, &temperature);
847
848 if (ret)
849 return ret;
850
851 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800852}
853
854static ssize_t
855temp_crit_show(struct device *dev, struct device_attribute *attr,
856 char *buf)
857{
858 struct thermal_hwmon_attr *hwmon_attr
859 = container_of(attr, struct thermal_hwmon_attr, attr);
Jean Delvare31f53962011-07-28 13:48:42 -0700860 struct thermal_hwmon_temp *temp
861 = container_of(hwmon_attr, struct thermal_hwmon_temp,
Zhang Ruie68b16a2008-04-21 16:07:52 +0800862 temp_crit);
Jean Delvare31f53962011-07-28 13:48:42 -0700863 struct thermal_zone_device *tz = temp->tz;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000864 long temperature;
865 int ret;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800866
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000867 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
868 if (ret)
869 return ret;
870
871 return sprintf(buf, "%ld\n", temperature);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800872}
873
874
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700875static struct thermal_hwmon_device *
876thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
877{
878 struct thermal_hwmon_device *hwmon;
879
880 mutex_lock(&thermal_list_lock);
881 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
882 if (!strcmp(hwmon->type, tz->type)) {
883 mutex_unlock(&thermal_list_lock);
884 return hwmon;
885 }
886 mutex_unlock(&thermal_list_lock);
887
888 return NULL;
889}
890
Jean Delvare31f53962011-07-28 13:48:42 -0700891/* Find the temperature input matching a given thermal zone */
892static struct thermal_hwmon_temp *
893thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
894 const struct thermal_zone_device *tz)
895{
896 struct thermal_hwmon_temp *temp;
897
898 mutex_lock(&thermal_list_lock);
899 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
900 if (temp->tz == tz) {
901 mutex_unlock(&thermal_list_lock);
902 return temp;
903 }
904 mutex_unlock(&thermal_list_lock);
905
906 return NULL;
907}
908
Zhang Ruie68b16a2008-04-21 16:07:52 +0800909static int
910thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
911{
912 struct thermal_hwmon_device *hwmon;
Jean Delvare31f53962011-07-28 13:48:42 -0700913 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800914 int new_hwmon_device = 1;
915 int result;
916
Jean Delvare0d97d7a2011-07-28 13:48:41 -0700917 hwmon = thermal_hwmon_lookup_by_type(tz);
918 if (hwmon) {
919 new_hwmon_device = 0;
920 goto register_sys_interface;
921 }
Zhang Ruie68b16a2008-04-21 16:07:52 +0800922
923 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
924 if (!hwmon)
925 return -ENOMEM;
926
927 INIT_LIST_HEAD(&hwmon->tz_list);
928 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
929 hwmon->device = hwmon_device_register(NULL);
930 if (IS_ERR(hwmon->device)) {
931 result = PTR_ERR(hwmon->device);
932 goto free_mem;
933 }
Greg Kroah-Hartman0e968a32009-04-30 14:43:31 -0700934 dev_set_drvdata(hwmon->device, hwmon);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800935 result = device_create_file(hwmon->device, &dev_attr_name);
936 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530937 goto free_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800938
939 register_sys_interface:
Jean Delvare31f53962011-07-28 13:48:42 -0700940 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
941 if (!temp) {
942 result = -ENOMEM;
943 goto unregister_name;
944 }
945
946 temp->tz = tz;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800947 hwmon->count++;
948
Guenter Roeck79a49162012-07-21 10:53:48 +1000949 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +0800950 "temp%d_input", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700951 temp->temp_input.attr.attr.name = temp->temp_input.name;
952 temp->temp_input.attr.attr.mode = 0444;
953 temp->temp_input.attr.show = temp_input_show;
954 sysfs_attr_init(&temp->temp_input.attr.attr);
955 result = device_create_file(hwmon->device, &temp->temp_input.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800956 if (result)
Jean Delvare31f53962011-07-28 13:48:42 -0700957 goto free_temp_mem;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800958
959 if (tz->ops->get_crit_temp) {
960 unsigned long temperature;
961 if (!tz->ops->get_crit_temp(tz, &temperature)) {
Guenter Roeck79a49162012-07-21 10:53:48 +1000962 snprintf(temp->temp_crit.name,
963 sizeof(temp->temp_crit.name),
Zhang Ruie68b16a2008-04-21 16:07:52 +0800964 "temp%d_crit", hwmon->count);
Jean Delvare31f53962011-07-28 13:48:42 -0700965 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
966 temp->temp_crit.attr.attr.mode = 0444;
967 temp->temp_crit.attr.show = temp_crit_show;
968 sysfs_attr_init(&temp->temp_crit.attr.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800969 result = device_create_file(hwmon->device,
Jean Delvare31f53962011-07-28 13:48:42 -0700970 &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800971 if (result)
Durgadoss Rb299eb52011-03-03 04:30:13 +0530972 goto unregister_input;
Zhang Ruie68b16a2008-04-21 16:07:52 +0800973 }
974 }
975
976 mutex_lock(&thermal_list_lock);
977 if (new_hwmon_device)
978 list_add_tail(&hwmon->node, &thermal_hwmon_list);
Jean Delvare31f53962011-07-28 13:48:42 -0700979 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
Zhang Ruie68b16a2008-04-21 16:07:52 +0800980 mutex_unlock(&thermal_list_lock);
981
982 return 0;
983
Durgadoss Rb299eb52011-03-03 04:30:13 +0530984 unregister_input:
Jean Delvare31f53962011-07-28 13:48:42 -0700985 device_remove_file(hwmon->device, &temp->temp_input.attr);
986 free_temp_mem:
987 kfree(temp);
Durgadoss Rb299eb52011-03-03 04:30:13 +0530988 unregister_name:
Zhang Ruie68b16a2008-04-21 16:07:52 +0800989 if (new_hwmon_device) {
990 device_remove_file(hwmon->device, &dev_attr_name);
991 hwmon_device_unregister(hwmon->device);
992 }
993 free_mem:
994 if (new_hwmon_device)
995 kfree(hwmon);
996
997 return result;
998}
999
1000static void
1001thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1002{
Jean Delvare31f53962011-07-28 13:48:42 -07001003 struct thermal_hwmon_device *hwmon;
1004 struct thermal_hwmon_temp *temp;
Zhang Ruie68b16a2008-04-21 16:07:52 +08001005
Jean Delvare31f53962011-07-28 13:48:42 -07001006 hwmon = thermal_hwmon_lookup_by_type(tz);
1007 if (unlikely(!hwmon)) {
1008 /* Should never happen... */
1009 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
1010 return;
1011 }
1012
1013 temp = thermal_hwmon_lookup_temp(hwmon, tz);
1014 if (unlikely(!temp)) {
1015 /* Should never happen... */
1016 dev_dbg(&tz->device, "temperature input lookup failed!\n");
1017 return;
1018 }
1019
1020 device_remove_file(hwmon->device, &temp->temp_input.attr);
Durgadoss Rb299eb52011-03-03 04:30:13 +05301021 if (tz->ops->get_crit_temp)
Jean Delvare31f53962011-07-28 13:48:42 -07001022 device_remove_file(hwmon->device, &temp->temp_crit.attr);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001023
1024 mutex_lock(&thermal_list_lock);
Jean Delvare31f53962011-07-28 13:48:42 -07001025 list_del(&temp->hwmon_node);
1026 kfree(temp);
Zhang Ruie68b16a2008-04-21 16:07:52 +08001027 if (!list_empty(&hwmon->tz_list)) {
1028 mutex_unlock(&thermal_list_lock);
1029 return;
1030 }
1031 list_del(&hwmon->node);
1032 mutex_unlock(&thermal_list_lock);
1033
1034 device_remove_file(hwmon->device, &dev_attr_name);
1035 hwmon_device_unregister(hwmon->device);
1036 kfree(hwmon);
1037}
1038#else
1039static int
1040thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
1041{
1042 return 0;
1043}
1044
1045static void
1046thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1047{
1048}
1049#endif
1050
Zhang Rui203d3d42008-01-17 15:51:08 +08001051/**
1052 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +08001053 * @tz: thermal zone device
1054 * @trip: indicates which trip point the cooling devices is
1055 * associated with in this thermal zone.
1056 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -05001057 *
1058 * This function is usually called in the thermal zone device .bind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +08001059 */
1060int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1061 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +08001062 struct thermal_cooling_device *cdev,
1063 unsigned long upper, unsigned long lower)
Zhang Rui203d3d42008-01-17 15:51:08 +08001064{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001065 struct thermal_instance *dev;
1066 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -05001067 struct thermal_zone_device *pos1;
1068 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +08001069 unsigned long max_state;
Zhang Rui203d3d42008-01-17 15:51:08 +08001070 int result;
1071
Len Brown543a9562008-02-07 16:55:08 -05001072 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +08001073 return -EINVAL;
1074
Thomas Sujithc7516702008-02-15 00:58:50 -05001075 list_for_each_entry(pos1, &thermal_tz_list, node) {
1076 if (pos1 == tz)
1077 break;
1078 }
1079 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1080 if (pos2 == cdev)
1081 break;
1082 }
1083
1084 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +08001085 return -EINVAL;
1086
Zhang Rui9d998422012-06-26 16:35:57 +08001087 cdev->ops->get_max_state(cdev, &max_state);
1088
1089 /* lower default 0, upper default max_state */
1090 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1091 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1092
1093 if (lower > upper || upper > max_state)
1094 return -EINVAL;
1095
Zhang Rui203d3d42008-01-17 15:51:08 +08001096 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001097 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001098 if (!dev)
1099 return -ENOMEM;
1100 dev->tz = tz;
1101 dev->cdev = cdev;
1102 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +08001103 dev->upper = upper;
1104 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +08001105 dev->target = THERMAL_NO_TARGET;
Zhang Rui74051ba2012-06-26 16:27:22 +08001106
Zhang Rui203d3d42008-01-17 15:51:08 +08001107 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1108 if (result)
1109 goto free_mem;
1110
1111 sprintf(dev->name, "cdev%d", dev->id);
1112 result =
1113 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1114 if (result)
1115 goto release_idr;
1116
1117 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -07001118 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001119 dev->attr.attr.name = dev->attr_name;
1120 dev->attr.attr.mode = 0444;
1121 dev->attr.show = thermal_cooling_device_trip_point_show;
1122 result = device_create_file(&tz->device, &dev->attr);
1123 if (result)
1124 goto remove_symbol_link;
1125
1126 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001127 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001128 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +08001129 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1130 result = -EEXIST;
1131 break;
1132 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001133 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001134 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001135 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1136 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001137 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001138 mutex_unlock(&tz->lock);
1139
1140 if (!result)
1141 return 0;
1142
1143 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -07001144remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001145 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -07001146release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001147 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -07001148free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001149 kfree(dev);
1150 return result;
1151}
1152EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
1153
1154/**
1155 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
Zhang Rui203d3d42008-01-17 15:51:08 +08001156 * @tz: thermal zone device
1157 * @trip: indicates which trip point the cooling devices is
1158 * associated with in this thermal zone.
1159 * @cdev: thermal cooling device
Len Brown543a9562008-02-07 16:55:08 -05001160 *
1161 * This function is usually called in the thermal zone device .unbind callback.
Zhang Rui203d3d42008-01-17 15:51:08 +08001162 */
1163int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1164 int trip,
1165 struct thermal_cooling_device *cdev)
1166{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001167 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001168
1169 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001170 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001171 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001172 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001173 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001174 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001175 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001176 mutex_unlock(&tz->lock);
1177 goto unbind;
1178 }
1179 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001180 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001181 mutex_unlock(&tz->lock);
1182
1183 return -ENODEV;
1184
Joe Perchescaca8b82012-03-21 12:55:02 -07001185unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +08001186 device_remove_file(&tz->device, &pos->attr);
1187 sysfs_remove_link(&tz->device.kobj, pos->name);
1188 release_idr(&tz->idr, &tz->lock, pos->id);
1189 kfree(pos);
1190 return 0;
1191}
1192EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
1193
1194static void thermal_release(struct device *dev)
1195{
1196 struct thermal_zone_device *tz;
1197 struct thermal_cooling_device *cdev;
1198
Joe Perchescaca8b82012-03-21 12:55:02 -07001199 if (!strncmp(dev_name(dev), "thermal_zone",
1200 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001201 tz = to_thermal_zone(dev);
1202 kfree(tz);
1203 } else {
1204 cdev = to_cooling_device(dev);
1205 kfree(cdev);
1206 }
1207}
1208
1209static struct class thermal_class = {
1210 .name = "thermal",
1211 .dev_release = thermal_release,
1212};
1213
1214/**
1215 * thermal_cooling_device_register - register a new thermal cooling device
1216 * @type: the thermal cooling device type.
1217 * @devdata: device private data.
1218 * @ops: standard thermal cooling devices callbacks.
1219 */
Joe Perchescaca8b82012-03-21 12:55:02 -07001220struct thermal_cooling_device *
1221thermal_cooling_device_register(char *type, void *devdata,
1222 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001223{
1224 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001225 int result;
1226
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001227 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001228 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001229
1230 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001231 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001232 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001233
1234 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1235 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001236 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001237
1238 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1239 if (result) {
1240 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001241 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001242 }
1243
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001244 strcpy(cdev->type, type ? : "");
Zhang Ruif4a821c2012-07-24 16:56:21 +08001245 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001246 INIT_LIST_HEAD(&cdev->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001247 cdev->ops = ops;
Zhang Ruice119f82012-06-27 14:13:04 +08001248 cdev->updated = true;
Zhang Rui203d3d42008-01-17 15:51:08 +08001249 cdev->device.class = &thermal_class;
1250 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001251 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001252 result = device_register(&cdev->device);
1253 if (result) {
1254 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1255 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001256 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001257 }
1258
1259 /* sys I/F */
1260 if (type) {
Len Brown543a9562008-02-07 16:55:08 -05001261 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001262 if (result)
1263 goto unregister;
1264 }
1265
1266 result = device_create_file(&cdev->device, &dev_attr_max_state);
1267 if (result)
1268 goto unregister;
1269
1270 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1271 if (result)
1272 goto unregister;
1273
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301274 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001275 mutex_lock(&thermal_list_lock);
1276 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001277 mutex_unlock(&thermal_list_lock);
1278
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301279 /* Update binding information for 'this' new cdev */
1280 bind_cdev(cdev);
1281
1282 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001283
Joe Perchescaca8b82012-03-21 12:55:02 -07001284unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001285 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1286 device_unregister(&cdev->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001287 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001288}
1289EXPORT_SYMBOL(thermal_cooling_device_register);
1290
1291/**
1292 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001293 * @cdev: the thermal cooling device to remove.
1294 *
1295 * thermal_cooling_device_unregister() must be called when the device is no
1296 * longer needed.
1297 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301298void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001299{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301300 int i;
1301 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001302 struct thermal_zone_device *tz;
1303 struct thermal_cooling_device *pos = NULL;
1304
1305 if (!cdev)
1306 return;
1307
1308 mutex_lock(&thermal_list_lock);
1309 list_for_each_entry(pos, &thermal_cdev_list, node)
1310 if (pos == cdev)
1311 break;
1312 if (pos != cdev) {
1313 /* thermal cooling device not found */
1314 mutex_unlock(&thermal_list_lock);
1315 return;
1316 }
1317 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301318
1319 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001320 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301321 if (tz->ops->unbind) {
1322 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001323 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301324 }
1325
1326 if (!tz->tzp || !tz->tzp->tbp)
1327 continue;
1328
1329 tzp = tz->tzp;
1330 for (i = 0; i < tzp->num_tbps; i++) {
1331 if (tzp->tbp[i].cdev == cdev) {
1332 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1333 tzp->tbp[i].cdev = NULL;
1334 }
1335 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001336 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301337
Zhang Rui203d3d42008-01-17 15:51:08 +08001338 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301339
Zhang Rui203d3d42008-01-17 15:51:08 +08001340 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001341 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001342 device_remove_file(&cdev->device, &dev_attr_max_state);
1343 device_remove_file(&cdev->device, &dev_attr_cur_state);
1344
1345 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1346 device_unregister(&cdev->device);
1347 return;
1348}
1349EXPORT_SYMBOL(thermal_cooling_device_unregister);
1350
Durgadoss Rdc765482012-09-18 11:05:00 +05301351void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001352{
1353 struct thermal_instance *instance;
1354 unsigned long target = 0;
1355
1356 /* cooling device is updated*/
1357 if (cdev->updated)
1358 return;
1359
Zhang Ruif4a821c2012-07-24 16:56:21 +08001360 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001361 /* Make sure cdev enters the deepest cooling state */
1362 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1363 if (instance->target == THERMAL_NO_TARGET)
1364 continue;
1365 if (instance->target > target)
1366 target = instance->target;
1367 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001368 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001369 cdev->ops->set_cur_state(cdev, target);
1370 cdev->updated = true;
1371}
Durgadoss Rdc765482012-09-18 11:05:00 +05301372EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001373
Matthew Garrettb1569e92008-12-03 17:55:32 +00001374/**
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301375 * notify_thermal_framework - Sensor drivers use this API to notify framework
1376 * @tz: thermal zone device
1377 * @trip: indicates which trip point has been crossed
1378 *
1379 * This function handles the trip events from sensor drivers. It starts
1380 * throttling the cooling devices according to the policy configured.
1381 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1382 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1383 * The throttling policy is based on the configured platform data; if no
1384 * platform data is provided, this uses the step_wise throttling policy.
1385 */
1386void notify_thermal_framework(struct thermal_zone_device *tz, int trip)
1387{
1388 handle_thermal_trip(tz, trip);
1389}
1390EXPORT_SYMBOL(notify_thermal_framework);
1391
1392/**
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001393 * create_trip_attrs - create attributes for trip points
1394 * @tz: the thermal zone device
1395 * @mask: Writeable trip point bitmap.
1396 */
1397static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1398{
1399 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001400 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001401
Durgadoss R27365a62012-07-25 10:10:59 +08001402 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001403 if (!tz->trip_type_attrs)
1404 return -ENOMEM;
1405
Durgadoss R27365a62012-07-25 10:10:59 +08001406 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001407 if (!tz->trip_temp_attrs) {
1408 kfree(tz->trip_type_attrs);
1409 return -ENOMEM;
1410 }
1411
Durgadoss R27365a62012-07-25 10:10:59 +08001412 if (tz->ops->get_trip_hyst) {
1413 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1414 if (!tz->trip_hyst_attrs) {
1415 kfree(tz->trip_type_attrs);
1416 kfree(tz->trip_temp_attrs);
1417 return -ENOMEM;
1418 }
1419 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001420
Durgadoss R27365a62012-07-25 10:10:59 +08001421
1422 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001423 /* create trip type attribute */
1424 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1425 "trip_point_%d_type", indx);
1426
1427 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1428 tz->trip_type_attrs[indx].attr.attr.name =
1429 tz->trip_type_attrs[indx].name;
1430 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1431 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1432
1433 device_create_file(&tz->device,
1434 &tz->trip_type_attrs[indx].attr);
1435
1436 /* create trip temp attribute */
1437 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1438 "trip_point_%d_temp", indx);
1439
1440 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1441 tz->trip_temp_attrs[indx].attr.attr.name =
1442 tz->trip_temp_attrs[indx].name;
1443 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1444 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1445 if (mask & (1 << indx)) {
1446 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1447 tz->trip_temp_attrs[indx].attr.store =
1448 trip_point_temp_store;
1449 }
1450
1451 device_create_file(&tz->device,
1452 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001453
1454 /* create Optional trip hyst attribute */
1455 if (!tz->ops->get_trip_hyst)
1456 continue;
1457 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1458 "trip_point_%d_hyst", indx);
1459
1460 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1461 tz->trip_hyst_attrs[indx].attr.attr.name =
1462 tz->trip_hyst_attrs[indx].name;
1463 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1464 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1465 if (tz->ops->set_trip_hyst) {
1466 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1467 tz->trip_hyst_attrs[indx].attr.store =
1468 trip_point_hyst_store;
1469 }
1470
1471 device_create_file(&tz->device,
1472 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001473 }
1474 return 0;
1475}
1476
1477static void remove_trip_attrs(struct thermal_zone_device *tz)
1478{
1479 int indx;
1480
1481 for (indx = 0; indx < tz->trips; indx++) {
1482 device_remove_file(&tz->device,
1483 &tz->trip_type_attrs[indx].attr);
1484 device_remove_file(&tz->device,
1485 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001486 if (tz->ops->get_trip_hyst)
1487 device_remove_file(&tz->device,
1488 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001489 }
1490 kfree(tz->trip_type_attrs);
1491 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001492 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001493}
1494
1495/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001496 * thermal_zone_device_register - register a new thermal zone device
1497 * @type: the thermal zone device type
1498 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001499 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001500 * @devdata: private device data
1501 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301502 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001503 * @passive_delay: number of milliseconds to wait between polls when
1504 * performing passive cooling
1505 * @polling_delay: number of milliseconds to wait between polls when checking
1506 * whether trip points have been crossed (0 for interrupt
1507 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001508 *
1509 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001510 * longer needed. The passive cooling depends on the .get_trend() return value.
Zhang Rui203d3d42008-01-17 15:51:08 +08001511 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001512struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001513 int trips, int mask, void *devdata,
Alan Cox5b275ce2010-11-11 15:27:29 +00001514 const struct thermal_zone_device_ops *ops,
Durgadoss R50125a92012-09-18 11:04:56 +05301515 const struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001516 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001517{
1518 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001519 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001520 int result;
1521 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001522 int passive = 0;
Zhang Rui203d3d42008-01-17 15:51:08 +08001523
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001524 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001525 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001526
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001527 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001528 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001529
1530 if (!ops || !ops->get_temp)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001531 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001532
1533 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1534 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001535 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001536
Zhang Rui2d374132012-06-27 10:09:00 +08001537 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001538 idr_init(&tz->idr);
1539 mutex_init(&tz->lock);
1540 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1541 if (result) {
1542 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001543 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001544 }
1545
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001546 strcpy(tz->type, type ? : "");
Zhang Rui203d3d42008-01-17 15:51:08 +08001547 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301548 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001549 tz->device.class = &thermal_class;
1550 tz->devdata = devdata;
1551 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001552 tz->passive_delay = passive_delay;
1553 tz->polling_delay = polling_delay;
1554
Kay Sievers354655e2009-01-06 10:44:37 -08001555 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001556 result = device_register(&tz->device);
1557 if (result) {
1558 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1559 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001560 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001561 }
1562
1563 /* sys I/F */
1564 if (type) {
1565 result = device_create_file(&tz->device, &dev_attr_type);
1566 if (result)
1567 goto unregister;
1568 }
1569
1570 result = device_create_file(&tz->device, &dev_attr_temp);
1571 if (result)
1572 goto unregister;
1573
1574 if (ops->get_mode) {
1575 result = device_create_file(&tz->device, &dev_attr_mode);
1576 if (result)
1577 goto unregister;
1578 }
1579
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001580 result = create_trip_attrs(tz, mask);
1581 if (result)
1582 goto unregister;
1583
Zhang Rui203d3d42008-01-17 15:51:08 +08001584 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001585 tz->ops->get_trip_type(tz, count, &trip_type);
1586 if (trip_type == THERMAL_TRIP_PASSIVE)
1587 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001588 }
1589
Durgadoss R5a2c0902012-09-18 11:04:58 +05301590 if (!passive) {
1591 result = device_create_file(&tz->device, &dev_attr_passive);
1592 if (result)
1593 goto unregister;
1594 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001595
Durgadoss R5a2c0902012-09-18 11:04:58 +05301596 /* Create policy attribute */
1597 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001598 if (result)
1599 goto unregister;
1600
Durgadoss Ra4a15482012-09-18 11:04:57 +05301601 /* Update 'this' zone's governor information */
1602 mutex_lock(&thermal_governor_lock);
1603
1604 if (tz->tzp)
1605 tz->governor = __find_governor(tz->tzp->governor_name);
1606 else
1607 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1608
1609 mutex_unlock(&thermal_governor_lock);
1610
Zhang Ruie68b16a2008-04-21 16:07:52 +08001611 result = thermal_add_hwmon_sysfs(tz);
1612 if (result)
1613 goto unregister;
1614
Zhang Rui203d3d42008-01-17 15:51:08 +08001615 mutex_lock(&thermal_list_lock);
1616 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001617 mutex_unlock(&thermal_list_lock);
1618
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301619 /* Bind cooling devices for this zone */
1620 bind_tz(tz);
1621
Matthew Garrettb1569e92008-12-03 17:55:32 +00001622 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1623
1624 thermal_zone_device_update(tz);
1625
Zhang Rui203d3d42008-01-17 15:51:08 +08001626 if (!result)
1627 return tz;
1628
Joe Perchescaca8b82012-03-21 12:55:02 -07001629unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001630 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1631 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001632 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001633}
1634EXPORT_SYMBOL(thermal_zone_device_register);
1635
1636/**
1637 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001638 * @tz: the thermal zone device to remove
1639 */
1640void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1641{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301642 int i;
1643 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001644 struct thermal_cooling_device *cdev;
1645 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001646
1647 if (!tz)
1648 return;
1649
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301650 tzp = tz->tzp;
1651
Zhang Rui203d3d42008-01-17 15:51:08 +08001652 mutex_lock(&thermal_list_lock);
1653 list_for_each_entry(pos, &thermal_tz_list, node)
1654 if (pos == tz)
1655 break;
1656 if (pos != tz) {
1657 /* thermal zone device not found */
1658 mutex_unlock(&thermal_list_lock);
1659 return;
1660 }
1661 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301662
1663 /* Unbind all cdevs associated with 'this' thermal zone */
1664 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1665 if (tz->ops->unbind) {
1666 tz->ops->unbind(tz, cdev);
1667 continue;
1668 }
1669
1670 if (!tzp || !tzp->tbp)
1671 break;
1672
1673 for (i = 0; i < tzp->num_tbps; i++) {
1674 if (tzp->tbp[i].cdev == cdev) {
1675 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1676 tzp->tbp[i].cdev = NULL;
1677 }
1678 }
1679 }
1680
Zhang Rui203d3d42008-01-17 15:51:08 +08001681 mutex_unlock(&thermal_list_lock);
1682
Matthew Garrettb1569e92008-12-03 17:55:32 +00001683 thermal_zone_device_set_polling(tz, 0);
1684
Zhang Rui203d3d42008-01-17 15:51:08 +08001685 if (tz->type[0])
1686 device_remove_file(&tz->device, &dev_attr_type);
1687 device_remove_file(&tz->device, &dev_attr_temp);
1688 if (tz->ops->get_mode)
1689 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301690 device_remove_file(&tz->device, &dev_attr_policy);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001691 remove_trip_attrs(tz);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301692 tz->governor = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001693
Zhang Ruie68b16a2008-04-21 16:07:52 +08001694 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001695 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1696 idr_destroy(&tz->idr);
1697 mutex_destroy(&tz->lock);
1698 device_unregister(&tz->device);
1699 return;
1700}
Zhang Rui203d3d42008-01-17 15:51:08 +08001701EXPORT_SYMBOL(thermal_zone_device_unregister);
1702
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001703#ifdef CONFIG_NET
1704static struct genl_family thermal_event_genl_family = {
1705 .id = GENL_ID_GENERATE,
1706 .name = THERMAL_GENL_FAMILY_NAME,
1707 .version = THERMAL_GENL_VERSION,
1708 .maxattr = THERMAL_GENL_ATTR_MAX,
1709};
1710
1711static struct genl_multicast_group thermal_event_mcgrp = {
1712 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1713};
1714
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001715int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1716 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301717{
1718 struct sk_buff *skb;
1719 struct nlattr *attr;
1720 struct thermal_genl_event *thermal_event;
1721 void *msg_header;
1722 int size;
1723 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001724 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301725
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001726 if (!tz)
1727 return -EINVAL;
1728
R.Durgadoss4cb18722010-10-27 03:33:29 +05301729 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001730 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1731 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301732
1733 skb = genlmsg_new(size, GFP_ATOMIC);
1734 if (!skb)
1735 return -ENOMEM;
1736
1737 /* add the genetlink message header */
1738 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1739 &thermal_event_genl_family, 0,
1740 THERMAL_GENL_CMD_EVENT);
1741 if (!msg_header) {
1742 nlmsg_free(skb);
1743 return -ENOMEM;
1744 }
1745
1746 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001747 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1748 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301749
1750 if (!attr) {
1751 nlmsg_free(skb);
1752 return -EINVAL;
1753 }
1754
1755 thermal_event = nla_data(attr);
1756 if (!thermal_event) {
1757 nlmsg_free(skb);
1758 return -EINVAL;
1759 }
1760
1761 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1762
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001763 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301764 thermal_event->event = event;
1765
1766 /* send multicast genetlink message */
1767 result = genlmsg_end(skb, msg_header);
1768 if (result < 0) {
1769 nlmsg_free(skb);
1770 return result;
1771 }
1772
1773 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1774 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001775 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301776
1777 return result;
1778}
Jean Delvare2d58d7e2011-11-04 10:31:04 +01001779EXPORT_SYMBOL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301780
1781static int genetlink_init(void)
1782{
1783 int result;
1784
1785 result = genl_register_family(&thermal_event_genl_family);
1786 if (result)
1787 return result;
1788
1789 result = genl_register_mc_group(&thermal_event_genl_family,
1790 &thermal_event_mcgrp);
1791 if (result)
1792 genl_unregister_family(&thermal_event_genl_family);
1793 return result;
1794}
1795
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001796static void genetlink_exit(void)
1797{
1798 genl_unregister_family(&thermal_event_genl_family);
1799}
1800#else /* !CONFIG_NET */
1801static inline int genetlink_init(void) { return 0; }
1802static inline void genetlink_exit(void) {}
1803#endif /* !CONFIG_NET */
1804
Zhang Rui203d3d42008-01-17 15:51:08 +08001805static int __init thermal_init(void)
1806{
1807 int result = 0;
1808
1809 result = class_register(&thermal_class);
1810 if (result) {
1811 idr_destroy(&thermal_tz_idr);
1812 idr_destroy(&thermal_cdev_idr);
1813 mutex_destroy(&thermal_idr_lock);
1814 mutex_destroy(&thermal_list_lock);
1815 }
R.Durgadoss4cb18722010-10-27 03:33:29 +05301816 result = genetlink_init();
Zhang Rui203d3d42008-01-17 15:51:08 +08001817 return result;
1818}
1819
1820static void __exit thermal_exit(void)
1821{
1822 class_unregister(&thermal_class);
1823 idr_destroy(&thermal_tz_idr);
1824 idr_destroy(&thermal_cdev_idr);
1825 mutex_destroy(&thermal_idr_lock);
1826 mutex_destroy(&thermal_list_lock);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301827 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001828}
1829
R.Durgadoss4cb18722010-10-27 03:33:29 +05301830fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001831module_exit(thermal_exit);