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