Thermal: rename thermal_sys.c to thermal_core.c
[linux-2.6-block.git] / drivers / thermal / thermal_core.c
CommitLineData
203d3d4a
ZR
1/*
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
c5a01dd5
JP
26#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
203d3d4a
ZR
28#include <linux/module.h>
29#include <linux/device.h>
30#include <linux/err.h>
5a0e3ad6 31#include <linux/slab.h>
203d3d4a
ZR
32#include <linux/kdev_t.h>
33#include <linux/idr.h>
34#include <linux/thermal.h>
b1569e99 35#include <linux/reboot.h>
4cb18728
D
36#include <net/netlink.h>
37#include <net/genetlink.h>
203d3d4a 38
71350db4
D
39#include "thermal_core.h"
40
63c4ec90 41MODULE_AUTHOR("Zhang Rui");
203d3d4a
ZR
42MODULE_DESCRIPTION("Generic thermal management sysfs support");
43MODULE_LICENSE("GPL");
44
203d3d4a
ZR
45static 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);
a4a15485
D
51static LIST_HEAD(thermal_governor_list);
52
203d3d4a 53static DEFINE_MUTEX(thermal_list_lock);
a4a15485
D
54static 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);
203d3d4a
ZR
131
132static int get_idr(struct idr *idr, struct mutex *lock, int *id)
133{
6deb69fa 134 int ret;
203d3d4a
ZR
135
136 if (lock)
137 mutex_lock(lock);
6deb69fa 138 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
203d3d4a
ZR
139 if (lock)
140 mutex_unlock(lock);
6deb69fa
TH
141 if (unlikely(ret < 0))
142 return ret;
143 *id = ret;
203d3d4a
ZR
144 return 0;
145}
146
147static void release_idr(struct idr *idr, struct mutex *lock, int id)
148{
149 if (lock)
150 mutex_lock(lock);
151 idr_remove(idr, id);
152 if (lock)
153 mutex_unlock(lock);
154}
155
9b4298a0
D
156int get_tz_trend(struct thermal_zone_device *tz, int trip)
157{
158 enum thermal_trend trend;
159
160 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
161 if (tz->temperature > tz->last_temperature)
162 trend = THERMAL_TREND_RAISING;
163 else if (tz->temperature < tz->last_temperature)
164 trend = THERMAL_TREND_DROPPING;
165 else
166 trend = THERMAL_TREND_STABLE;
167 }
168
169 return trend;
170}
171EXPORT_SYMBOL(get_tz_trend);
172
173struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
174 struct thermal_cooling_device *cdev, int trip)
175{
176 struct thermal_instance *pos = NULL;
177 struct thermal_instance *target_instance = NULL;
178
179 mutex_lock(&tz->lock);
180 mutex_lock(&cdev->lock);
181
182 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
183 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
184 target_instance = pos;
185 break;
186 }
187 }
188
189 mutex_unlock(&cdev->lock);
190 mutex_unlock(&tz->lock);
191
192 return target_instance;
193}
194EXPORT_SYMBOL(get_thermal_instance);
195
7e8ee1e9
D
196static void print_bind_err_msg(struct thermal_zone_device *tz,
197 struct thermal_cooling_device *cdev, int ret)
198{
199 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
200 tz->type, cdev->type, ret);
201}
202
203static void __bind(struct thermal_zone_device *tz, int mask,
204 struct thermal_cooling_device *cdev)
205{
206 int i, ret;
207
208 for (i = 0; i < tz->trips; i++) {
209 if (mask & (1 << i)) {
210 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
211 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
212 if (ret)
213 print_bind_err_msg(tz, cdev, ret);
214 }
215 }
216}
217
218static void __unbind(struct thermal_zone_device *tz, int mask,
219 struct thermal_cooling_device *cdev)
220{
221 int i;
222
223 for (i = 0; i < tz->trips; i++)
224 if (mask & (1 << i))
225 thermal_zone_unbind_cooling_device(tz, i, cdev);
226}
227
228static void bind_cdev(struct thermal_cooling_device *cdev)
229{
230 int i, ret;
231 const struct thermal_zone_params *tzp;
232 struct thermal_zone_device *pos = NULL;
233
234 mutex_lock(&thermal_list_lock);
235
236 list_for_each_entry(pos, &thermal_tz_list, node) {
237 if (!pos->tzp && !pos->ops->bind)
238 continue;
239
240 if (!pos->tzp && pos->ops->bind) {
241 ret = pos->ops->bind(pos, cdev);
242 if (ret)
243 print_bind_err_msg(pos, cdev, ret);
244 }
245
246 tzp = pos->tzp;
791700cd
HD
247 if (!tzp || !tzp->tbp)
248 continue;
7e8ee1e9
D
249
250 for (i = 0; i < tzp->num_tbps; i++) {
251 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
252 continue;
253 if (tzp->tbp[i].match(pos, cdev))
254 continue;
255 tzp->tbp[i].cdev = cdev;
256 __bind(pos, tzp->tbp[i].trip_mask, cdev);
257 }
258 }
259
260 mutex_unlock(&thermal_list_lock);
261}
262
263static void bind_tz(struct thermal_zone_device *tz)
264{
265 int i, ret;
266 struct thermal_cooling_device *pos = NULL;
267 const struct thermal_zone_params *tzp = tz->tzp;
268
269 if (!tzp && !tz->ops->bind)
270 return;
271
272 mutex_lock(&thermal_list_lock);
273
274 /* If there is no platform data, try to use ops->bind */
275 if (!tzp && tz->ops->bind) {
276 list_for_each_entry(pos, &thermal_cdev_list, node) {
277 ret = tz->ops->bind(tz, pos);
278 if (ret)
279 print_bind_err_msg(tz, pos, ret);
280 }
281 goto exit;
282 }
283
791700cd 284 if (!tzp || !tzp->tbp)
7e8ee1e9
D
285 goto exit;
286
287 list_for_each_entry(pos, &thermal_cdev_list, node) {
288 for (i = 0; i < tzp->num_tbps; i++) {
289 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
290 continue;
291 if (tzp->tbp[i].match(tz, pos))
292 continue;
293 tzp->tbp[i].cdev = pos;
294 __bind(tz, tzp->tbp[i].trip_mask, pos);
295 }
296 }
297exit:
298 mutex_unlock(&thermal_list_lock);
299}
300
0c01ebbf
D
301static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
302 int delay)
303{
304 if (delay > 1000)
305 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
306 round_jiffies(msecs_to_jiffies(delay)));
307 else if (delay)
308 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
309 msecs_to_jiffies(delay));
310 else
311 cancel_delayed_work(&tz->poll_queue);
312}
313
314static void monitor_thermal_zone(struct thermal_zone_device *tz)
315{
316 mutex_lock(&tz->lock);
317
318 if (tz->passive)
319 thermal_zone_device_set_polling(tz, tz->passive_delay);
320 else if (tz->polling_delay)
321 thermal_zone_device_set_polling(tz, tz->polling_delay);
322 else
323 thermal_zone_device_set_polling(tz, 0);
324
325 mutex_unlock(&tz->lock);
326}
327
328static void handle_non_critical_trips(struct thermal_zone_device *tz,
329 int trip, enum thermal_trip_type trip_type)
330{
d567c686
ZR
331 if (tz->governor)
332 tz->governor->throttle(tz, trip);
0c01ebbf
D
333}
334
335static void handle_critical_trips(struct thermal_zone_device *tz,
336 int trip, enum thermal_trip_type trip_type)
337{
338 long trip_temp;
339
340 tz->ops->get_trip_temp(tz, trip, &trip_temp);
341
342 /* If we have not crossed the trip_temp, we do not care. */
343 if (tz->temperature < trip_temp)
344 return;
345
346 if (tz->ops->notify)
347 tz->ops->notify(tz, trip, trip_type);
348
349 if (trip_type == THERMAL_TRIP_CRITICAL) {
923e0b1e
EV
350 dev_emerg(&tz->device,
351 "critical temperature reached(%d C),shutting down\n",
352 tz->temperature / 1000);
0c01ebbf
D
353 orderly_poweroff(true);
354 }
355}
356
357static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
358{
359 enum thermal_trip_type type;
360
361 tz->ops->get_trip_type(tz, trip, &type);
362
363 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
364 handle_critical_trips(tz, trip, type);
365 else
366 handle_non_critical_trips(tz, trip, type);
367 /*
368 * Alright, we handled this trip successfully.
369 * So, start monitoring again.
370 */
371 monitor_thermal_zone(tz);
372}
373
e6e238c3
ADK
374static int thermal_zone_get_temp(struct thermal_zone_device *tz,
375 unsigned long *temp)
376{
5e20b2e5
ZR
377 int ret = 0;
378#ifdef CONFIG_THERMAL_EMULATION
379 int count;
e6e238c3
ADK
380 unsigned long crit_temp = -1UL;
381 enum thermal_trip_type type;
5e20b2e5 382#endif
e6e238c3
ADK
383
384 mutex_lock(&tz->lock);
385
386 ret = tz->ops->get_temp(tz, temp);
387#ifdef CONFIG_THERMAL_EMULATION
388 if (!tz->emul_temperature)
389 goto skip_emul;
390
391 for (count = 0; count < tz->trips; count++) {
392 ret = tz->ops->get_trip_type(tz, count, &type);
393 if (!ret && type == THERMAL_TRIP_CRITICAL) {
394 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
395 break;
396 }
397 }
398
399 if (ret)
400 goto skip_emul;
401
402 if (*temp < crit_temp)
403 *temp = tz->emul_temperature;
404skip_emul:
405#endif
406 mutex_unlock(&tz->lock);
407 return ret;
408}
409
0c01ebbf
D
410static void update_temperature(struct thermal_zone_device *tz)
411{
412 long temp;
413 int ret;
414
e6e238c3 415 ret = thermal_zone_get_temp(tz, &temp);
0c01ebbf 416 if (ret) {
923e0b1e
EV
417 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
418 tz->id);
e6e238c3 419 return;
0c01ebbf
D
420 }
421
e6e238c3 422 mutex_lock(&tz->lock);
0c01ebbf
D
423 tz->last_temperature = tz->temperature;
424 tz->temperature = temp;
0c01ebbf
D
425 mutex_unlock(&tz->lock);
426}
427
428void thermal_zone_device_update(struct thermal_zone_device *tz)
429{
430 int count;
431
432 update_temperature(tz);
433
434 for (count = 0; count < tz->trips; count++)
435 handle_thermal_trip(tz, count);
436}
437EXPORT_SYMBOL(thermal_zone_device_update);
438
439static void thermal_zone_device_check(struct work_struct *work)
440{
441 struct thermal_zone_device *tz = container_of(work, struct
442 thermal_zone_device,
443 poll_queue.work);
444 thermal_zone_device_update(tz);
445}
446
203d3d4a
ZR
447/* sys I/F for thermal zone */
448
449#define to_thermal_zone(_dev) \
450 container_of(_dev, struct thermal_zone_device, device)
451
452static ssize_t
453type_show(struct device *dev, struct device_attribute *attr, char *buf)
454{
455 struct thermal_zone_device *tz = to_thermal_zone(dev);
456
457 return sprintf(buf, "%s\n", tz->type);
458}
459
460static ssize_t
461temp_show(struct device *dev, struct device_attribute *attr, char *buf)
462{
463 struct thermal_zone_device *tz = to_thermal_zone(dev);
6503e5df
MG
464 long temperature;
465 int ret;
203d3d4a 466
e6e238c3 467 ret = thermal_zone_get_temp(tz, &temperature);
6503e5df
MG
468
469 if (ret)
470 return ret;
471
472 return sprintf(buf, "%ld\n", temperature);
203d3d4a
ZR
473}
474
475static ssize_t
476mode_show(struct device *dev, struct device_attribute *attr, char *buf)
477{
478 struct thermal_zone_device *tz = to_thermal_zone(dev);
6503e5df
MG
479 enum thermal_device_mode mode;
480 int result;
203d3d4a
ZR
481
482 if (!tz->ops->get_mode)
483 return -EPERM;
484
6503e5df
MG
485 result = tz->ops->get_mode(tz, &mode);
486 if (result)
487 return result;
488
489 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
490 : "disabled");
203d3d4a
ZR
491}
492
493static ssize_t
494mode_store(struct device *dev, struct device_attribute *attr,
495 const char *buf, size_t count)
496{
497 struct thermal_zone_device *tz = to_thermal_zone(dev);
498 int result;
499
500 if (!tz->ops->set_mode)
501 return -EPERM;
502
f1f0e2ac 503 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
6503e5df 504 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
f1f0e2ac 505 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
6503e5df
MG
506 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
507 else
508 result = -EINVAL;
509
203d3d4a
ZR
510 if (result)
511 return result;
512
513 return count;
514}
515
516static ssize_t
517trip_point_type_show(struct device *dev, struct device_attribute *attr,
518 char *buf)
519{
520 struct thermal_zone_device *tz = to_thermal_zone(dev);
6503e5df
MG
521 enum thermal_trip_type type;
522 int trip, result;
203d3d4a
ZR
523
524 if (!tz->ops->get_trip_type)
525 return -EPERM;
526
527 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
528 return -EINVAL;
529
6503e5df
MG
530 result = tz->ops->get_trip_type(tz, trip, &type);
531 if (result)
532 return result;
533
534 switch (type) {
535 case THERMAL_TRIP_CRITICAL:
625120a4 536 return sprintf(buf, "critical\n");
6503e5df 537 case THERMAL_TRIP_HOT:
625120a4 538 return sprintf(buf, "hot\n");
6503e5df 539 case THERMAL_TRIP_PASSIVE:
625120a4 540 return sprintf(buf, "passive\n");
6503e5df 541 case THERMAL_TRIP_ACTIVE:
625120a4 542 return sprintf(buf, "active\n");
6503e5df 543 default:
625120a4 544 return sprintf(buf, "unknown\n");
6503e5df 545 }
203d3d4a
ZR
546}
547
c56f5c03
D
548static ssize_t
549trip_point_temp_store(struct device *dev, struct device_attribute *attr,
550 const char *buf, size_t count)
551{
552 struct thermal_zone_device *tz = to_thermal_zone(dev);
553 int trip, ret;
554 unsigned long temperature;
555
556 if (!tz->ops->set_trip_temp)
557 return -EPERM;
558
559 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
560 return -EINVAL;
561
562 if (kstrtoul(buf, 10, &temperature))
563 return -EINVAL;
564
565 ret = tz->ops->set_trip_temp(tz, trip, temperature);
566
567 return ret ? ret : count;
568}
569
203d3d4a
ZR
570static ssize_t
571trip_point_temp_show(struct device *dev, struct device_attribute *attr,
572 char *buf)
573{
574 struct thermal_zone_device *tz = to_thermal_zone(dev);
6503e5df
MG
575 int trip, ret;
576 long temperature;
203d3d4a
ZR
577
578 if (!tz->ops->get_trip_temp)
579 return -EPERM;
580
581 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
582 return -EINVAL;
583
6503e5df
MG
584 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
585
586 if (ret)
587 return ret;
588
589 return sprintf(buf, "%ld\n", temperature);
203d3d4a
ZR
590}
591
27365a6c
D
592static ssize_t
593trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
594 const char *buf, size_t count)
595{
596 struct thermal_zone_device *tz = to_thermal_zone(dev);
597 int trip, ret;
598 unsigned long temperature;
599
600 if (!tz->ops->set_trip_hyst)
601 return -EPERM;
602
603 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
604 return -EINVAL;
605
606 if (kstrtoul(buf, 10, &temperature))
607 return -EINVAL;
608
609 /*
610 * We are not doing any check on the 'temperature' value
611 * here. The driver implementing 'set_trip_hyst' has to
612 * take care of this.
613 */
614 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
615
616 return ret ? ret : count;
617}
618
619static ssize_t
620trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
621 char *buf)
622{
623 struct thermal_zone_device *tz = to_thermal_zone(dev);
624 int trip, ret;
625 unsigned long temperature;
626
627 if (!tz->ops->get_trip_hyst)
628 return -EPERM;
629
630 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
631 return -EINVAL;
632
633 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
634
635 return ret ? ret : sprintf(buf, "%ld\n", temperature);
636}
637
03a971a2
MG
638static ssize_t
639passive_store(struct device *dev, struct device_attribute *attr,
640 const char *buf, size_t count)
641{
642 struct thermal_zone_device *tz = to_thermal_zone(dev);
643 struct thermal_cooling_device *cdev = NULL;
644 int state;
645
646 if (!sscanf(buf, "%d\n", &state))
647 return -EINVAL;
648
3d8e3ad8
FP
649 /* sanity check: values below 1000 millicelcius don't make sense
650 * and can cause the system to go into a thermal heart attack
651 */
652 if (state && state < 1000)
653 return -EINVAL;
654
03a971a2
MG
655 if (state && !tz->forced_passive) {
656 mutex_lock(&thermal_list_lock);
657 list_for_each_entry(cdev, &thermal_cdev_list, node) {
658 if (!strncmp("Processor", cdev->type,
659 sizeof("Processor")))
660 thermal_zone_bind_cooling_device(tz,
9d99842f
ZR
661 THERMAL_TRIPS_NONE, cdev,
662 THERMAL_NO_LIMIT,
663 THERMAL_NO_LIMIT);
03a971a2
MG
664 }
665 mutex_unlock(&thermal_list_lock);
e4143b03
FP
666 if (!tz->passive_delay)
667 tz->passive_delay = 1000;
03a971a2
MG
668 } else if (!state && tz->forced_passive) {
669 mutex_lock(&thermal_list_lock);
670 list_for_each_entry(cdev, &thermal_cdev_list, node) {
671 if (!strncmp("Processor", cdev->type,
672 sizeof("Processor")))
673 thermal_zone_unbind_cooling_device(tz,
674 THERMAL_TRIPS_NONE,
675 cdev);
676 }
677 mutex_unlock(&thermal_list_lock);
e4143b03 678 tz->passive_delay = 0;
03a971a2
MG
679 }
680
03a971a2
MG
681 tz->forced_passive = state;
682
683 thermal_zone_device_update(tz);
684
685 return count;
686}
687
688static ssize_t
689passive_show(struct device *dev, struct device_attribute *attr,
690 char *buf)
691{
692 struct thermal_zone_device *tz = to_thermal_zone(dev);
693
694 return sprintf(buf, "%d\n", tz->forced_passive);
695}
696
5a2c090b
D
697static ssize_t
698policy_store(struct device *dev, struct device_attribute *attr,
699 const char *buf, size_t count)
700{
701 int ret = -EINVAL;
702 struct thermal_zone_device *tz = to_thermal_zone(dev);
703 struct thermal_governor *gov;
704
705 mutex_lock(&thermal_governor_lock);
706
707 gov = __find_governor(buf);
708 if (!gov)
709 goto exit;
710
711 tz->governor = gov;
712 ret = count;
713
714exit:
715 mutex_unlock(&thermal_governor_lock);
716 return ret;
717}
718
719static ssize_t
720policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
721{
722 struct thermal_zone_device *tz = to_thermal_zone(dev);
723
724 return sprintf(buf, "%s\n", tz->governor->name);
725}
726
e6e238c3
ADK
727#ifdef CONFIG_THERMAL_EMULATION
728static ssize_t
729emul_temp_store(struct device *dev, struct device_attribute *attr,
730 const char *buf, size_t count)
731{
732 struct thermal_zone_device *tz = to_thermal_zone(dev);
733 int ret = 0;
734 unsigned long temperature;
735
736 if (kstrtoul(buf, 10, &temperature))
737 return -EINVAL;
738
739 if (!tz->ops->set_emul_temp) {
740 mutex_lock(&tz->lock);
741 tz->emul_temperature = temperature;
742 mutex_unlock(&tz->lock);
743 } else {
744 ret = tz->ops->set_emul_temp(tz, temperature);
745 }
746
747 return ret ? ret : count;
748}
749static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
750#endif/*CONFIG_THERMAL_EMULATION*/
751
203d3d4a
ZR
752static DEVICE_ATTR(type, 0444, type_show, NULL);
753static DEVICE_ATTR(temp, 0444, temp_show, NULL);
754static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
886ee546 755static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
5a2c090b 756static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
203d3d4a 757
203d3d4a
ZR
758/* sys I/F for cooling device */
759#define to_cooling_device(_dev) \
760 container_of(_dev, struct thermal_cooling_device, device)
761
762static ssize_t
763thermal_cooling_device_type_show(struct device *dev,
764 struct device_attribute *attr, char *buf)
765{
766 struct thermal_cooling_device *cdev = to_cooling_device(dev);
767
768 return sprintf(buf, "%s\n", cdev->type);
769}
770
771static ssize_t
772thermal_cooling_device_max_state_show(struct device *dev,
773 struct device_attribute *attr, char *buf)
774{
775 struct thermal_cooling_device *cdev = to_cooling_device(dev);
6503e5df
MG
776 unsigned long state;
777 int ret;
203d3d4a 778
6503e5df
MG
779 ret = cdev->ops->get_max_state(cdev, &state);
780 if (ret)
781 return ret;
782 return sprintf(buf, "%ld\n", state);
203d3d4a
ZR
783}
784
785static ssize_t
786thermal_cooling_device_cur_state_show(struct device *dev,
787 struct device_attribute *attr, char *buf)
788{
789 struct thermal_cooling_device *cdev = to_cooling_device(dev);
6503e5df
MG
790 unsigned long state;
791 int ret;
203d3d4a 792
6503e5df
MG
793 ret = cdev->ops->get_cur_state(cdev, &state);
794 if (ret)
795 return ret;
796 return sprintf(buf, "%ld\n", state);
203d3d4a
ZR
797}
798
799static ssize_t
800thermal_cooling_device_cur_state_store(struct device *dev,
801 struct device_attribute *attr,
802 const char *buf, size_t count)
803{
804 struct thermal_cooling_device *cdev = to_cooling_device(dev);
6503e5df 805 unsigned long state;
203d3d4a
ZR
806 int result;
807
6503e5df 808 if (!sscanf(buf, "%ld\n", &state))
203d3d4a
ZR
809 return -EINVAL;
810
edb94918 811 if ((long)state < 0)
203d3d4a
ZR
812 return -EINVAL;
813
814 result = cdev->ops->set_cur_state(cdev, state);
815 if (result)
816 return result;
817 return count;
818}
819
820static struct device_attribute dev_attr_cdev_type =
543a9561 821__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
203d3d4a
ZR
822static DEVICE_ATTR(max_state, 0444,
823 thermal_cooling_device_max_state_show, NULL);
824static DEVICE_ATTR(cur_state, 0644,
825 thermal_cooling_device_cur_state_show,
826 thermal_cooling_device_cur_state_store);
827
828static ssize_t
829thermal_cooling_device_trip_point_show(struct device *dev,
543a9561 830 struct device_attribute *attr, char *buf)
203d3d4a 831{
b81b6ba3 832 struct thermal_instance *instance;
203d3d4a
ZR
833
834 instance =
b81b6ba3 835 container_of(attr, struct thermal_instance, attr);
203d3d4a
ZR
836
837 if (instance->trip == THERMAL_TRIPS_NONE)
838 return sprintf(buf, "-1\n");
839 else
840 return sprintf(buf, "%d\n", instance->trip);
841}
842
843/* Device management */
844
16d75239
RH
845#if defined(CONFIG_THERMAL_HWMON)
846
e68b16ab
ZR
847/* hwmon sys I/F */
848#include <linux/hwmon.h>
31f5396a
JD
849
850/* thermal zone devices with the same type share one hwmon device */
851struct thermal_hwmon_device {
852 char type[THERMAL_NAME_LENGTH];
853 struct device *device;
854 int count;
855 struct list_head tz_list;
856 struct list_head node;
857};
858
859struct thermal_hwmon_attr {
860 struct device_attribute attr;
861 char name[16];
862};
863
864/* one temperature input for each thermal zone */
865struct thermal_hwmon_temp {
866 struct list_head hwmon_node;
867 struct thermal_zone_device *tz;
868 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
869 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
870};
871
e68b16ab
ZR
872static LIST_HEAD(thermal_hwmon_list);
873
874static ssize_t
875name_show(struct device *dev, struct device_attribute *attr, char *buf)
876{
0e968a3b 877 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
e68b16ab
ZR
878 return sprintf(buf, "%s\n", hwmon->type);
879}
880static DEVICE_ATTR(name, 0444, name_show, NULL);
881
882static ssize_t
883temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
884{
6503e5df
MG
885 long temperature;
886 int ret;
e68b16ab
ZR
887 struct thermal_hwmon_attr *hwmon_attr
888 = container_of(attr, struct thermal_hwmon_attr, attr);
31f5396a
JD
889 struct thermal_hwmon_temp *temp
890 = container_of(hwmon_attr, struct thermal_hwmon_temp,
e68b16ab 891 temp_input);
31f5396a 892 struct thermal_zone_device *tz = temp->tz;
e68b16ab 893
e6e238c3 894 ret = thermal_zone_get_temp(tz, &temperature);
6503e5df
MG
895
896 if (ret)
897 return ret;
898
899 return sprintf(buf, "%ld\n", temperature);
e68b16ab
ZR
900}
901
902static ssize_t
903temp_crit_show(struct device *dev, struct device_attribute *attr,
904 char *buf)
905{
906 struct thermal_hwmon_attr *hwmon_attr
907 = container_of(attr, struct thermal_hwmon_attr, attr);
31f5396a
JD
908 struct thermal_hwmon_temp *temp
909 = container_of(hwmon_attr, struct thermal_hwmon_temp,
e68b16ab 910 temp_crit);
31f5396a 911 struct thermal_zone_device *tz = temp->tz;
6503e5df
MG
912 long temperature;
913 int ret;
914
915 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
916 if (ret)
917 return ret;
e68b16ab 918
6503e5df 919 return sprintf(buf, "%ld\n", temperature);
e68b16ab
ZR
920}
921
922
0d97d7a4
JD
923static struct thermal_hwmon_device *
924thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
e68b16ab
ZR
925{
926 struct thermal_hwmon_device *hwmon;
e68b16ab
ZR
927
928 mutex_lock(&thermal_list_lock);
929 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
930 if (!strcmp(hwmon->type, tz->type)) {
e68b16ab 931 mutex_unlock(&thermal_list_lock);
0d97d7a4 932 return hwmon;
e68b16ab
ZR
933 }
934 mutex_unlock(&thermal_list_lock);
935
0d97d7a4
JD
936 return NULL;
937}
938
31f5396a
JD
939/* Find the temperature input matching a given thermal zone */
940static struct thermal_hwmon_temp *
941thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
942 const struct thermal_zone_device *tz)
943{
944 struct thermal_hwmon_temp *temp;
945
946 mutex_lock(&thermal_list_lock);
947 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
948 if (temp->tz == tz) {
949 mutex_unlock(&thermal_list_lock);
950 return temp;
951 }
952 mutex_unlock(&thermal_list_lock);
953
954 return NULL;
955}
956
0d97d7a4
JD
957static int
958thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
959{
960 struct thermal_hwmon_device *hwmon;
31f5396a 961 struct thermal_hwmon_temp *temp;
0d97d7a4
JD
962 int new_hwmon_device = 1;
963 int result;
964
965 hwmon = thermal_hwmon_lookup_by_type(tz);
966 if (hwmon) {
967 new_hwmon_device = 0;
968 goto register_sys_interface;
969 }
970
e68b16ab
ZR
971 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
972 if (!hwmon)
973 return -ENOMEM;
974
975 INIT_LIST_HEAD(&hwmon->tz_list);
976 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
977 hwmon->device = hwmon_device_register(NULL);
978 if (IS_ERR(hwmon->device)) {
979 result = PTR_ERR(hwmon->device);
980 goto free_mem;
981 }
0e968a3b 982 dev_set_drvdata(hwmon->device, hwmon);
e68b16ab
ZR
983 result = device_create_file(hwmon->device, &dev_attr_name);
984 if (result)
b299eb5c 985 goto free_mem;
e68b16ab
ZR
986
987 register_sys_interface:
31f5396a
JD
988 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
989 if (!temp) {
990 result = -ENOMEM;
991 goto unregister_name;
992 }
993
994 temp->tz = tz;
e68b16ab
ZR
995 hwmon->count++;
996
79a49168 997 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
e68b16ab 998 "temp%d_input", hwmon->count);
31f5396a
JD
999 temp->temp_input.attr.attr.name = temp->temp_input.name;
1000 temp->temp_input.attr.attr.mode = 0444;
1001 temp->temp_input.attr.show = temp_input_show;
1002 sysfs_attr_init(&temp->temp_input.attr.attr);
1003 result = device_create_file(hwmon->device, &temp->temp_input.attr);
e68b16ab 1004 if (result)
31f5396a 1005 goto free_temp_mem;
e68b16ab
ZR
1006
1007 if (tz->ops->get_crit_temp) {
1008 unsigned long temperature;
1009 if (!tz->ops->get_crit_temp(tz, &temperature)) {
79a49168
GR
1010 snprintf(temp->temp_crit.name,
1011 sizeof(temp->temp_crit.name),
e68b16ab 1012 "temp%d_crit", hwmon->count);
31f5396a
JD
1013 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
1014 temp->temp_crit.attr.attr.mode = 0444;
1015 temp->temp_crit.attr.show = temp_crit_show;
1016 sysfs_attr_init(&temp->temp_crit.attr.attr);
e68b16ab 1017 result = device_create_file(hwmon->device,
31f5396a 1018 &temp->temp_crit.attr);
e68b16ab 1019 if (result)
b299eb5c 1020 goto unregister_input;
e68b16ab
ZR
1021 }
1022 }
1023
1024 mutex_lock(&thermal_list_lock);
1025 if (new_hwmon_device)
1026 list_add_tail(&hwmon->node, &thermal_hwmon_list);
31f5396a 1027 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
e68b16ab
ZR
1028 mutex_unlock(&thermal_list_lock);
1029
1030 return 0;
1031
b299eb5c 1032 unregister_input:
31f5396a
JD
1033 device_remove_file(hwmon->device, &temp->temp_input.attr);
1034 free_temp_mem:
1035 kfree(temp);
b299eb5c 1036 unregister_name:
e68b16ab
ZR
1037 if (new_hwmon_device) {
1038 device_remove_file(hwmon->device, &dev_attr_name);
1039 hwmon_device_unregister(hwmon->device);
1040 }
1041 free_mem:
1042 if (new_hwmon_device)
1043 kfree(hwmon);
1044
1045 return result;
1046}
1047
1048static void
1049thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1050{
31f5396a
JD
1051 struct thermal_hwmon_device *hwmon;
1052 struct thermal_hwmon_temp *temp;
1053
1054 hwmon = thermal_hwmon_lookup_by_type(tz);
1055 if (unlikely(!hwmon)) {
1056 /* Should never happen... */
1057 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
1058 return;
1059 }
1060
1061 temp = thermal_hwmon_lookup_temp(hwmon, tz);
1062 if (unlikely(!temp)) {
1063 /* Should never happen... */
1064 dev_dbg(&tz->device, "temperature input lookup failed!\n");
1065 return;
1066 }
e68b16ab 1067
31f5396a 1068 device_remove_file(hwmon->device, &temp->temp_input.attr);
b299eb5c 1069 if (tz->ops->get_crit_temp)
31f5396a 1070 device_remove_file(hwmon->device, &temp->temp_crit.attr);
e68b16ab
ZR
1071
1072 mutex_lock(&thermal_list_lock);
31f5396a
JD
1073 list_del(&temp->hwmon_node);
1074 kfree(temp);
e68b16ab
ZR
1075 if (!list_empty(&hwmon->tz_list)) {
1076 mutex_unlock(&thermal_list_lock);
1077 return;
1078 }
1079 list_del(&hwmon->node);
1080 mutex_unlock(&thermal_list_lock);
1081
1082 device_remove_file(hwmon->device, &dev_attr_name);
1083 hwmon_device_unregister(hwmon->device);
1084 kfree(hwmon);
1085}
1086#else
1087static int
1088thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
1089{
1090 return 0;
1091}
1092
1093static void
1094thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1095{
1096}
1097#endif
1098
203d3d4a
ZR
1099/**
1100 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
203d3d4a
ZR
1101 * @tz: thermal zone device
1102 * @trip: indicates which trip point the cooling devices is
1103 * associated with in this thermal zone.
1104 * @cdev: thermal cooling device
543a9561
LB
1105 *
1106 * This function is usually called in the thermal zone device .bind callback.
203d3d4a
ZR
1107 */
1108int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1109 int trip,
9d99842f
ZR
1110 struct thermal_cooling_device *cdev,
1111 unsigned long upper, unsigned long lower)
203d3d4a 1112{
b81b6ba3
ZR
1113 struct thermal_instance *dev;
1114 struct thermal_instance *pos;
c7516709
TS
1115 struct thermal_zone_device *pos1;
1116 struct thermal_cooling_device *pos2;
74051ba5 1117 unsigned long max_state;
203d3d4a
ZR
1118 int result;
1119
543a9561 1120 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
203d3d4a
ZR
1121 return -EINVAL;
1122
c7516709
TS
1123 list_for_each_entry(pos1, &thermal_tz_list, node) {
1124 if (pos1 == tz)
1125 break;
1126 }
1127 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1128 if (pos2 == cdev)
1129 break;
1130 }
1131
1132 if (tz != pos1 || cdev != pos2)
203d3d4a
ZR
1133 return -EINVAL;
1134
9d99842f
ZR
1135 cdev->ops->get_max_state(cdev, &max_state);
1136
1137 /* lower default 0, upper default max_state */
1138 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1139 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1140
1141 if (lower > upper || upper > max_state)
1142 return -EINVAL;
1143
203d3d4a 1144 dev =
b81b6ba3 1145 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
203d3d4a
ZR
1146 if (!dev)
1147 return -ENOMEM;
1148 dev->tz = tz;
1149 dev->cdev = cdev;
1150 dev->trip = trip;
9d99842f
ZR
1151 dev->upper = upper;
1152 dev->lower = lower;
ce119f83 1153 dev->target = THERMAL_NO_TARGET;
74051ba5 1154
203d3d4a
ZR
1155 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1156 if (result)
1157 goto free_mem;
1158
1159 sprintf(dev->name, "cdev%d", dev->id);
1160 result =
1161 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1162 if (result)
1163 goto release_idr;
1164
1165 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
975f8c56 1166 sysfs_attr_init(&dev->attr.attr);
203d3d4a
ZR
1167 dev->attr.attr.name = dev->attr_name;
1168 dev->attr.attr.mode = 0444;
1169 dev->attr.show = thermal_cooling_device_trip_point_show;
1170 result = device_create_file(&tz->device, &dev->attr);
1171 if (result)
1172 goto remove_symbol_link;
1173
1174 mutex_lock(&tz->lock);
f4a821ce 1175 mutex_lock(&cdev->lock);
cddf31b3 1176 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
203d3d4a
ZR
1177 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1178 result = -EEXIST;
1179 break;
1180 }
b5e4ae62 1181 if (!result) {
cddf31b3 1182 list_add_tail(&dev->tz_node, &tz->thermal_instances);
b5e4ae62
ZR
1183 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1184 }
f4a821ce 1185 mutex_unlock(&cdev->lock);
203d3d4a
ZR
1186 mutex_unlock(&tz->lock);
1187
1188 if (!result)
1189 return 0;
1190
1191 device_remove_file(&tz->device, &dev->attr);
caca8b80 1192remove_symbol_link:
203d3d4a 1193 sysfs_remove_link(&tz->device.kobj, dev->name);
caca8b80 1194release_idr:
203d3d4a 1195 release_idr(&tz->idr, &tz->lock, dev->id);
caca8b80 1196free_mem:
203d3d4a
ZR
1197 kfree(dev);
1198 return result;
1199}
1200EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
1201
1202/**
1203 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
203d3d4a
ZR
1204 * @tz: thermal zone device
1205 * @trip: indicates which trip point the cooling devices is
1206 * associated with in this thermal zone.
1207 * @cdev: thermal cooling device
543a9561
LB
1208 *
1209 * This function is usually called in the thermal zone device .unbind callback.
203d3d4a
ZR
1210 */
1211int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1212 int trip,
1213 struct thermal_cooling_device *cdev)
1214{
b81b6ba3 1215 struct thermal_instance *pos, *next;
203d3d4a
ZR
1216
1217 mutex_lock(&tz->lock);
f4a821ce 1218 mutex_lock(&cdev->lock);
cddf31b3 1219 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
543a9561 1220 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
cddf31b3 1221 list_del(&pos->tz_node);
b5e4ae62 1222 list_del(&pos->cdev_node);
f4a821ce 1223 mutex_unlock(&cdev->lock);
203d3d4a
ZR
1224 mutex_unlock(&tz->lock);
1225 goto unbind;
1226 }
1227 }
f4a821ce 1228 mutex_unlock(&cdev->lock);
203d3d4a
ZR
1229 mutex_unlock(&tz->lock);
1230
1231 return -ENODEV;
1232
caca8b80 1233unbind:
203d3d4a
ZR
1234 device_remove_file(&tz->device, &pos->attr);
1235 sysfs_remove_link(&tz->device.kobj, pos->name);
1236 release_idr(&tz->idr, &tz->lock, pos->id);
1237 kfree(pos);
1238 return 0;
1239}
1240EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
1241
1242static void thermal_release(struct device *dev)
1243{
1244 struct thermal_zone_device *tz;
1245 struct thermal_cooling_device *cdev;
1246
caca8b80
JP
1247 if (!strncmp(dev_name(dev), "thermal_zone",
1248 sizeof("thermal_zone") - 1)) {
203d3d4a
ZR
1249 tz = to_thermal_zone(dev);
1250 kfree(tz);
1251 } else {
1252 cdev = to_cooling_device(dev);
1253 kfree(cdev);
1254 }
1255}
1256
1257static struct class thermal_class = {
1258 .name = "thermal",
1259 .dev_release = thermal_release,
1260};
1261
1262/**
1263 * thermal_cooling_device_register - register a new thermal cooling device
1264 * @type: the thermal cooling device type.
1265 * @devdata: device private data.
1266 * @ops: standard thermal cooling devices callbacks.
1267 */
caca8b80
JP
1268struct thermal_cooling_device *
1269thermal_cooling_device_register(char *type, void *devdata,
1270 const struct thermal_cooling_device_ops *ops)
203d3d4a
ZR
1271{
1272 struct thermal_cooling_device *cdev;
203d3d4a
ZR
1273 int result;
1274
204dd1d3 1275 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
3e6fda5c 1276 return ERR_PTR(-EINVAL);
203d3d4a
ZR
1277
1278 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
543a9561 1279 !ops->set_cur_state)
3e6fda5c 1280 return ERR_PTR(-EINVAL);
203d3d4a
ZR
1281
1282 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1283 if (!cdev)
3e6fda5c 1284 return ERR_PTR(-ENOMEM);
203d3d4a
ZR
1285
1286 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1287 if (result) {
1288 kfree(cdev);
3e6fda5c 1289 return ERR_PTR(result);
203d3d4a
ZR
1290 }
1291
204dd1d3 1292 strcpy(cdev->type, type ? : "");
f4a821ce 1293 mutex_init(&cdev->lock);
b5e4ae62 1294 INIT_LIST_HEAD(&cdev->thermal_instances);
203d3d4a 1295 cdev->ops = ops;
ce119f83 1296 cdev->updated = true;
203d3d4a
ZR
1297 cdev->device.class = &thermal_class;
1298 cdev->devdata = devdata;
354655ea 1299 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
203d3d4a
ZR
1300 result = device_register(&cdev->device);
1301 if (result) {
1302 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1303 kfree(cdev);
3e6fda5c 1304 return ERR_PTR(result);
203d3d4a
ZR
1305 }
1306
1307 /* sys I/F */
1308 if (type) {
543a9561 1309 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
203d3d4a
ZR
1310 if (result)
1311 goto unregister;
1312 }
1313
1314 result = device_create_file(&cdev->device, &dev_attr_max_state);
1315 if (result)
1316 goto unregister;
1317
1318 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1319 if (result)
1320 goto unregister;
1321
7e8ee1e9 1322 /* Add 'this' new cdev to the global cdev list */
203d3d4a
ZR
1323 mutex_lock(&thermal_list_lock);
1324 list_add(&cdev->node, &thermal_cdev_list);
203d3d4a
ZR
1325 mutex_unlock(&thermal_list_lock);
1326
7e8ee1e9
D
1327 /* Update binding information for 'this' new cdev */
1328 bind_cdev(cdev);
1329
1330 return cdev;
203d3d4a 1331
caca8b80 1332unregister:
203d3d4a
ZR
1333 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1334 device_unregister(&cdev->device);
3e6fda5c 1335 return ERR_PTR(result);
203d3d4a
ZR
1336}
1337EXPORT_SYMBOL(thermal_cooling_device_register);
1338
1339/**
1340 * thermal_cooling_device_unregister - removes the registered thermal cooling device
203d3d4a
ZR
1341 * @cdev: the thermal cooling device to remove.
1342 *
1343 * thermal_cooling_device_unregister() must be called when the device is no
1344 * longer needed.
1345 */
7e8ee1e9 1346void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
203d3d4a 1347{
7e8ee1e9
D
1348 int i;
1349 const struct thermal_zone_params *tzp;
203d3d4a
ZR
1350 struct thermal_zone_device *tz;
1351 struct thermal_cooling_device *pos = NULL;
1352
1353 if (!cdev)
1354 return;
1355
1356 mutex_lock(&thermal_list_lock);
1357 list_for_each_entry(pos, &thermal_cdev_list, node)
1358 if (pos == cdev)
1359 break;
1360 if (pos != cdev) {
1361 /* thermal cooling device not found */
1362 mutex_unlock(&thermal_list_lock);
1363 return;
1364 }
1365 list_del(&cdev->node);
7e8ee1e9
D
1366
1367 /* Unbind all thermal zones associated with 'this' cdev */
203d3d4a 1368 list_for_each_entry(tz, &thermal_tz_list, node) {
7e8ee1e9
D
1369 if (tz->ops->unbind) {
1370 tz->ops->unbind(tz, cdev);
1371 continue;
1372 }
1373
1374 if (!tz->tzp || !tz->tzp->tbp)
203d3d4a 1375 continue;
7e8ee1e9
D
1376
1377 tzp = tz->tzp;
1378 for (i = 0; i < tzp->num_tbps; i++) {
1379 if (tzp->tbp[i].cdev == cdev) {
1380 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1381 tzp->tbp[i].cdev = NULL;
1382 }
1383 }
203d3d4a 1384 }
7e8ee1e9 1385
203d3d4a 1386 mutex_unlock(&thermal_list_lock);
7e8ee1e9 1387
203d3d4a 1388 if (cdev->type[0])
543a9561 1389 device_remove_file(&cdev->device, &dev_attr_cdev_type);
203d3d4a
ZR
1390 device_remove_file(&cdev->device, &dev_attr_max_state);
1391 device_remove_file(&cdev->device, &dev_attr_cur_state);
1392
1393 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1394 device_unregister(&cdev->device);
1395 return;
1396}
1397EXPORT_SYMBOL(thermal_cooling_device_unregister);
1398
dc765482 1399void thermal_cdev_update(struct thermal_cooling_device *cdev)
ce119f83
ZR
1400{
1401 struct thermal_instance *instance;
1402 unsigned long target = 0;
1403
1404 /* cooling device is updated*/
1405 if (cdev->updated)
1406 return;
1407
f4a821ce 1408 mutex_lock(&cdev->lock);
ce119f83
ZR
1409 /* Make sure cdev enters the deepest cooling state */
1410 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1411 if (instance->target == THERMAL_NO_TARGET)
1412 continue;
1413 if (instance->target > target)
1414 target = instance->target;
1415 }
f4a821ce 1416 mutex_unlock(&cdev->lock);
ce119f83
ZR
1417 cdev->ops->set_cur_state(cdev, target);
1418 cdev->updated = true;
1419}
dc765482 1420EXPORT_SYMBOL(thermal_cdev_update);
ce119f83 1421
f2b4caaf
D
1422/**
1423 * notify_thermal_framework - Sensor drivers use this API to notify framework
1424 * @tz: thermal zone device
1425 * @trip: indicates which trip point has been crossed
1426 *
1427 * This function handles the trip events from sensor drivers. It starts
1428 * throttling the cooling devices according to the policy configured.
1429 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1430 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1431 * The throttling policy is based on the configured platform data; if no
1432 * platform data is provided, this uses the step_wise throttling policy.
1433 */
1434void notify_thermal_framework(struct thermal_zone_device *tz, int trip)
1435{
1436 handle_thermal_trip(tz, trip);
1437}
1438EXPORT_SYMBOL(notify_thermal_framework);
1439
c56f5c03
D
1440/**
1441 * create_trip_attrs - create attributes for trip points
1442 * @tz: the thermal zone device
1443 * @mask: Writeable trip point bitmap.
1444 */
1445static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1446{
1447 int indx;
27365a6c 1448 int size = sizeof(struct thermal_attr) * tz->trips;
c56f5c03 1449
27365a6c 1450 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
c56f5c03
D
1451 if (!tz->trip_type_attrs)
1452 return -ENOMEM;
1453
27365a6c 1454 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
c56f5c03
D
1455 if (!tz->trip_temp_attrs) {
1456 kfree(tz->trip_type_attrs);
1457 return -ENOMEM;
1458 }
1459
27365a6c
D
1460 if (tz->ops->get_trip_hyst) {
1461 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1462 if (!tz->trip_hyst_attrs) {
1463 kfree(tz->trip_type_attrs);
1464 kfree(tz->trip_temp_attrs);
1465 return -ENOMEM;
1466 }
1467 }
c56f5c03 1468
27365a6c
D
1469
1470 for (indx = 0; indx < tz->trips; indx++) {
c56f5c03
D
1471 /* create trip type attribute */
1472 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1473 "trip_point_%d_type", indx);
1474
1475 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1476 tz->trip_type_attrs[indx].attr.attr.name =
1477 tz->trip_type_attrs[indx].name;
1478 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1479 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1480
1481 device_create_file(&tz->device,
1482 &tz->trip_type_attrs[indx].attr);
1483
1484 /* create trip temp attribute */
1485 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1486 "trip_point_%d_temp", indx);
1487
1488 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1489 tz->trip_temp_attrs[indx].attr.attr.name =
1490 tz->trip_temp_attrs[indx].name;
1491 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1492 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1493 if (mask & (1 << indx)) {
1494 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1495 tz->trip_temp_attrs[indx].attr.store =
1496 trip_point_temp_store;
1497 }
1498
1499 device_create_file(&tz->device,
1500 &tz->trip_temp_attrs[indx].attr);
27365a6c
D
1501
1502 /* create Optional trip hyst attribute */
1503 if (!tz->ops->get_trip_hyst)
1504 continue;
1505 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1506 "trip_point_%d_hyst", indx);
1507
1508 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1509 tz->trip_hyst_attrs[indx].attr.attr.name =
1510 tz->trip_hyst_attrs[indx].name;
1511 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1512 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1513 if (tz->ops->set_trip_hyst) {
1514 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1515 tz->trip_hyst_attrs[indx].attr.store =
1516 trip_point_hyst_store;
1517 }
1518
1519 device_create_file(&tz->device,
1520 &tz->trip_hyst_attrs[indx].attr);
c56f5c03
D
1521 }
1522 return 0;
1523}
1524
1525static void remove_trip_attrs(struct thermal_zone_device *tz)
1526{
1527 int indx;
1528
1529 for (indx = 0; indx < tz->trips; indx++) {
1530 device_remove_file(&tz->device,
1531 &tz->trip_type_attrs[indx].attr);
1532 device_remove_file(&tz->device,
1533 &tz->trip_temp_attrs[indx].attr);
27365a6c
D
1534 if (tz->ops->get_trip_hyst)
1535 device_remove_file(&tz->device,
1536 &tz->trip_hyst_attrs[indx].attr);
c56f5c03
D
1537 }
1538 kfree(tz->trip_type_attrs);
1539 kfree(tz->trip_temp_attrs);
27365a6c 1540 kfree(tz->trip_hyst_attrs);
c56f5c03
D
1541}
1542
203d3d4a
ZR
1543/**
1544 * thermal_zone_device_register - register a new thermal zone device
1545 * @type: the thermal zone device type
1546 * @trips: the number of trip points the thermal zone support
c56f5c03 1547 * @mask: a bit string indicating the writeablility of trip points
203d3d4a
ZR
1548 * @devdata: private device data
1549 * @ops: standard thermal zone device callbacks
50125a9b 1550 * @tzp: thermal zone platform parameters
b1569e99
MG
1551 * @passive_delay: number of milliseconds to wait between polls when
1552 * performing passive cooling
1553 * @polling_delay: number of milliseconds to wait between polls when checking
1554 * whether trip points have been crossed (0 for interrupt
1555 * driven systems)
203d3d4a
ZR
1556 *
1557 * thermal_zone_device_unregister() must be called when the device is no
1b7ddb84 1558 * longer needed. The passive cooling depends on the .get_trend() return value.
203d3d4a 1559 */
4b1bf587 1560struct thermal_zone_device *thermal_zone_device_register(const char *type,
c56f5c03 1561 int trips, int mask, void *devdata,
5b275ce2 1562 const struct thermal_zone_device_ops *ops,
50125a9b 1563 const struct thermal_zone_params *tzp,
1b7ddb84 1564 int passive_delay, int polling_delay)
203d3d4a
ZR
1565{
1566 struct thermal_zone_device *tz;
03a971a2 1567 enum thermal_trip_type trip_type;
203d3d4a
ZR
1568 int result;
1569 int count;
03a971a2 1570 int passive = 0;
203d3d4a 1571
204dd1d3 1572 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
3e6fda5c 1573 return ERR_PTR(-EINVAL);
203d3d4a 1574
c56f5c03 1575 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
3e6fda5c 1576 return ERR_PTR(-EINVAL);
203d3d4a
ZR
1577
1578 if (!ops || !ops->get_temp)
3e6fda5c 1579 return ERR_PTR(-EINVAL);
203d3d4a 1580
6b2aa51d
EV
1581 if (trips > 0 && !ops->get_trip_type)
1582 return ERR_PTR(-EINVAL);
1583
203d3d4a
ZR
1584 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1585 if (!tz)
3e6fda5c 1586 return ERR_PTR(-ENOMEM);
203d3d4a 1587
2d374139 1588 INIT_LIST_HEAD(&tz->thermal_instances);
203d3d4a
ZR
1589 idr_init(&tz->idr);
1590 mutex_init(&tz->lock);
1591 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1592 if (result) {
1593 kfree(tz);
3e6fda5c 1594 return ERR_PTR(result);
203d3d4a
ZR
1595 }
1596
204dd1d3 1597 strcpy(tz->type, type ? : "");
203d3d4a 1598 tz->ops = ops;
50125a9b 1599 tz->tzp = tzp;
203d3d4a
ZR
1600 tz->device.class = &thermal_class;
1601 tz->devdata = devdata;
1602 tz->trips = trips;
b1569e99
MG
1603 tz->passive_delay = passive_delay;
1604 tz->polling_delay = polling_delay;
1605
354655ea 1606 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
203d3d4a
ZR
1607 result = device_register(&tz->device);
1608 if (result) {
1609 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1610 kfree(tz);
3e6fda5c 1611 return ERR_PTR(result);
203d3d4a
ZR
1612 }
1613
1614 /* sys I/F */
1615 if (type) {
1616 result = device_create_file(&tz->device, &dev_attr_type);
1617 if (result)
1618 goto unregister;
1619 }
1620
1621 result = device_create_file(&tz->device, &dev_attr_temp);
1622 if (result)
1623 goto unregister;
1624
1625 if (ops->get_mode) {
1626 result = device_create_file(&tz->device, &dev_attr_mode);
1627 if (result)
1628 goto unregister;
1629 }
1630
c56f5c03
D
1631 result = create_trip_attrs(tz, mask);
1632 if (result)
1633 goto unregister;
1634
203d3d4a 1635 for (count = 0; count < trips; count++) {
03a971a2
MG
1636 tz->ops->get_trip_type(tz, count, &trip_type);
1637 if (trip_type == THERMAL_TRIP_PASSIVE)
1638 passive = 1;
203d3d4a
ZR
1639 }
1640
5a2c090b
D
1641 if (!passive) {
1642 result = device_create_file(&tz->device, &dev_attr_passive);
1643 if (result)
1644 goto unregister;
1645 }
03a971a2 1646
e6e238c3
ADK
1647#ifdef CONFIG_THERMAL_EMULATION
1648 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1649 if (result)
1650 goto unregister;
1651#endif
5a2c090b
D
1652 /* Create policy attribute */
1653 result = device_create_file(&tz->device, &dev_attr_policy);
03a971a2
MG
1654 if (result)
1655 goto unregister;
1656
a4a15485
D
1657 /* Update 'this' zone's governor information */
1658 mutex_lock(&thermal_governor_lock);
1659
1660 if (tz->tzp)
1661 tz->governor = __find_governor(tz->tzp->governor_name);
1662 else
1663 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1664
1665 mutex_unlock(&thermal_governor_lock);
1666
e68b16ab
ZR
1667 result = thermal_add_hwmon_sysfs(tz);
1668 if (result)
1669 goto unregister;
1670
203d3d4a
ZR
1671 mutex_lock(&thermal_list_lock);
1672 list_add_tail(&tz->node, &thermal_tz_list);
203d3d4a
ZR
1673 mutex_unlock(&thermal_list_lock);
1674
7e8ee1e9
D
1675 /* Bind cooling devices for this zone */
1676 bind_tz(tz);
1677
b1569e99
MG
1678 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1679
1680 thermal_zone_device_update(tz);
1681
203d3d4a
ZR
1682 if (!result)
1683 return tz;
1684
caca8b80 1685unregister:
203d3d4a
ZR
1686 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1687 device_unregister(&tz->device);
3e6fda5c 1688 return ERR_PTR(result);
203d3d4a
ZR
1689}
1690EXPORT_SYMBOL(thermal_zone_device_register);
1691
1692/**
1693 * thermal_device_unregister - removes the registered thermal zone device
203d3d4a
ZR
1694 * @tz: the thermal zone device to remove
1695 */
1696void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1697{
7e8ee1e9
D
1698 int i;
1699 const struct thermal_zone_params *tzp;
203d3d4a
ZR
1700 struct thermal_cooling_device *cdev;
1701 struct thermal_zone_device *pos = NULL;
203d3d4a
ZR
1702
1703 if (!tz)
1704 return;
1705
7e8ee1e9
D
1706 tzp = tz->tzp;
1707
203d3d4a
ZR
1708 mutex_lock(&thermal_list_lock);
1709 list_for_each_entry(pos, &thermal_tz_list, node)
1710 if (pos == tz)
1711 break;
1712 if (pos != tz) {
1713 /* thermal zone device not found */
1714 mutex_unlock(&thermal_list_lock);
1715 return;
1716 }
1717 list_del(&tz->node);
7e8ee1e9
D
1718
1719 /* Unbind all cdevs associated with 'this' thermal zone */
1720 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1721 if (tz->ops->unbind) {
1722 tz->ops->unbind(tz, cdev);
1723 continue;
1724 }
1725
1726 if (!tzp || !tzp->tbp)
1727 break;
1728
1729 for (i = 0; i < tzp->num_tbps; i++) {
1730 if (tzp->tbp[i].cdev == cdev) {
1731 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1732 tzp->tbp[i].cdev = NULL;
1733 }
1734 }
1735 }
1736
203d3d4a
ZR
1737 mutex_unlock(&thermal_list_lock);
1738
b1569e99
MG
1739 thermal_zone_device_set_polling(tz, 0);
1740
203d3d4a
ZR
1741 if (tz->type[0])
1742 device_remove_file(&tz->device, &dev_attr_type);
1743 device_remove_file(&tz->device, &dev_attr_temp);
1744 if (tz->ops->get_mode)
1745 device_remove_file(&tz->device, &dev_attr_mode);
5a2c090b 1746 device_remove_file(&tz->device, &dev_attr_policy);
c56f5c03 1747 remove_trip_attrs(tz);
a4a15485 1748 tz->governor = NULL;
203d3d4a 1749
e68b16ab 1750 thermal_remove_hwmon_sysfs(tz);
203d3d4a
ZR
1751 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1752 idr_destroy(&tz->idr);
1753 mutex_destroy(&tz->lock);
1754 device_unregister(&tz->device);
1755 return;
1756}
1757EXPORT_SYMBOL(thermal_zone_device_unregister);
1758
af06216a
RW
1759#ifdef CONFIG_NET
1760static struct genl_family thermal_event_genl_family = {
1761 .id = GENL_ID_GENERATE,
1762 .name = THERMAL_GENL_FAMILY_NAME,
1763 .version = THERMAL_GENL_VERSION,
1764 .maxattr = THERMAL_GENL_ATTR_MAX,
1765};
1766
1767static struct genl_multicast_group thermal_event_mcgrp = {
1768 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1769};
1770
8ab3e6a0
EV
1771int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1772 enum events event)
4cb18728
D
1773{
1774 struct sk_buff *skb;
1775 struct nlattr *attr;
1776 struct thermal_genl_event *thermal_event;
1777 void *msg_header;
1778 int size;
1779 int result;
b11de07c 1780 static unsigned int thermal_event_seqnum;
4cb18728 1781
8ab3e6a0
EV
1782 if (!tz)
1783 return -EINVAL;
1784
4cb18728 1785 /* allocate memory */
886ee546
JP
1786 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1787 nla_total_size(0);
4cb18728
D
1788
1789 skb = genlmsg_new(size, GFP_ATOMIC);
1790 if (!skb)
1791 return -ENOMEM;
1792
1793 /* add the genetlink message header */
1794 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1795 &thermal_event_genl_family, 0,
1796 THERMAL_GENL_CMD_EVENT);
1797 if (!msg_header) {
1798 nlmsg_free(skb);
1799 return -ENOMEM;
1800 }
1801
1802 /* fill the data */
886ee546
JP
1803 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1804 sizeof(struct thermal_genl_event));
4cb18728
D
1805
1806 if (!attr) {
1807 nlmsg_free(skb);
1808 return -EINVAL;
1809 }
1810
1811 thermal_event = nla_data(attr);
1812 if (!thermal_event) {
1813 nlmsg_free(skb);
1814 return -EINVAL;
1815 }
1816
1817 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1818
8ab3e6a0 1819 thermal_event->orig = tz->id;
4cb18728
D
1820 thermal_event->event = event;
1821
1822 /* send multicast genetlink message */
1823 result = genlmsg_end(skb, msg_header);
1824 if (result < 0) {
1825 nlmsg_free(skb);
1826 return result;
1827 }
1828
1829 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1830 if (result)
923e0b1e 1831 dev_err(&tz->device, "Failed to send netlink event:%d", result);
4cb18728
D
1832
1833 return result;
1834}
2d58d7ea 1835EXPORT_SYMBOL(thermal_generate_netlink_event);
4cb18728
D
1836
1837static int genetlink_init(void)
1838{
1839 int result;
1840
1841 result = genl_register_family(&thermal_event_genl_family);
1842 if (result)
1843 return result;
1844
1845 result = genl_register_mc_group(&thermal_event_genl_family,
1846 &thermal_event_mcgrp);
1847 if (result)
1848 genl_unregister_family(&thermal_event_genl_family);
1849 return result;
1850}
1851
af06216a
RW
1852static void genetlink_exit(void)
1853{
1854 genl_unregister_family(&thermal_event_genl_family);
1855}
1856#else /* !CONFIG_NET */
1857static inline int genetlink_init(void) { return 0; }
1858static inline void genetlink_exit(void) {}
1859#endif /* !CONFIG_NET */
1860
203d3d4a
ZR
1861static int __init thermal_init(void)
1862{
1863 int result = 0;
1864
1865 result = class_register(&thermal_class);
1866 if (result) {
1867 idr_destroy(&thermal_tz_idr);
1868 idr_destroy(&thermal_cdev_idr);
1869 mutex_destroy(&thermal_idr_lock);
1870 mutex_destroy(&thermal_list_lock);
da28d966 1871 return result;
203d3d4a 1872 }
4cb18728 1873 result = genetlink_init();
203d3d4a
ZR
1874 return result;
1875}
1876
1877static void __exit thermal_exit(void)
1878{
1879 class_unregister(&thermal_class);
1880 idr_destroy(&thermal_tz_idr);
1881 idr_destroy(&thermal_cdev_idr);
1882 mutex_destroy(&thermal_idr_lock);
1883 mutex_destroy(&thermal_list_lock);
4cb18728 1884 genetlink_exit();
203d3d4a
ZR
1885}
1886
4cb18728 1887fs_initcall(thermal_init);
203d3d4a 1888module_exit(thermal_exit);