1 // SPDX-License-Identifier: GPL-2.0
3 * thermal.c - sysfs interface of thermal devices
5 * Copyright (C) 2016 Eduardo Valentin <edubezval@gmail.com>
7 * Highly based on original thermal_core.c
8 * Copyright (C) 2008 Intel Corp
9 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
10 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/sysfs.h>
16 #include <linux/device.h>
17 #include <linux/err.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/jiffies.h>
22 #include "thermal_core.h"
24 /* sys I/F for thermal zone */
27 type_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
29 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
31 return sprintf(buf
, "%s\n", tz
->type
);
35 temp_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
37 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
40 ret
= thermal_zone_get_temp(tz
, &temperature
);
45 return sprintf(buf
, "%d\n", temperature
);
49 mode_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
51 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
52 enum thermal_device_mode mode
;
55 if (!tz
->ops
->get_mode
)
58 result
= tz
->ops
->get_mode(tz
, &mode
);
62 return sprintf(buf
, "%s\n", mode
== THERMAL_DEVICE_ENABLED
? "enabled"
67 mode_store(struct device
*dev
, struct device_attribute
*attr
,
68 const char *buf
, size_t count
)
70 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
73 if (!tz
->ops
->set_mode
)
76 if (!strncmp(buf
, "enabled", sizeof("enabled") - 1))
77 result
= tz
->ops
->set_mode(tz
, THERMAL_DEVICE_ENABLED
);
78 else if (!strncmp(buf
, "disabled", sizeof("disabled") - 1))
79 result
= tz
->ops
->set_mode(tz
, THERMAL_DEVICE_DISABLED
);
90 trip_point_type_show(struct device
*dev
, struct device_attribute
*attr
,
93 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
94 enum thermal_trip_type type
;
97 if (!tz
->ops
->get_trip_type
)
100 if (sscanf(attr
->attr
.name
, "trip_point_%d_type", &trip
) != 1)
103 result
= tz
->ops
->get_trip_type(tz
, trip
, &type
);
108 case THERMAL_TRIP_CRITICAL
:
109 return sprintf(buf
, "critical\n");
110 case THERMAL_TRIP_HOT
:
111 return sprintf(buf
, "hot\n");
112 case THERMAL_TRIP_PASSIVE
:
113 return sprintf(buf
, "passive\n");
114 case THERMAL_TRIP_ACTIVE
:
115 return sprintf(buf
, "active\n");
117 return sprintf(buf
, "unknown\n");
122 trip_point_temp_store(struct device
*dev
, struct device_attribute
*attr
,
123 const char *buf
, size_t count
)
125 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
129 if (!tz
->ops
->set_trip_temp
)
132 if (sscanf(attr
->attr
.name
, "trip_point_%d_temp", &trip
) != 1)
135 if (kstrtoint(buf
, 10, &temperature
))
138 ret
= tz
->ops
->set_trip_temp(tz
, trip
, temperature
);
142 thermal_zone_device_update(tz
, THERMAL_EVENT_UNSPECIFIED
);
148 trip_point_temp_show(struct device
*dev
, struct device_attribute
*attr
,
151 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
155 if (!tz
->ops
->get_trip_temp
)
158 if (sscanf(attr
->attr
.name
, "trip_point_%d_temp", &trip
) != 1)
161 ret
= tz
->ops
->get_trip_temp(tz
, trip
, &temperature
);
166 return sprintf(buf
, "%d\n", temperature
);
170 trip_point_hyst_store(struct device
*dev
, struct device_attribute
*attr
,
171 const char *buf
, size_t count
)
173 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
177 if (!tz
->ops
->set_trip_hyst
)
180 if (sscanf(attr
->attr
.name
, "trip_point_%d_hyst", &trip
) != 1)
183 if (kstrtoint(buf
, 10, &temperature
))
187 * We are not doing any check on the 'temperature' value
188 * here. The driver implementing 'set_trip_hyst' has to
191 ret
= tz
->ops
->set_trip_hyst(tz
, trip
, temperature
);
194 thermal_zone_set_trips(tz
);
196 return ret
? ret
: count
;
200 trip_point_hyst_show(struct device
*dev
, struct device_attribute
*attr
,
203 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
207 if (!tz
->ops
->get_trip_hyst
)
210 if (sscanf(attr
->attr
.name
, "trip_point_%d_hyst", &trip
) != 1)
213 ret
= tz
->ops
->get_trip_hyst(tz
, trip
, &temperature
);
215 return ret
? ret
: sprintf(buf
, "%d\n", temperature
);
219 passive_store(struct device
*dev
, struct device_attribute
*attr
,
220 const char *buf
, size_t count
)
222 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
225 if (sscanf(buf
, "%d\n", &state
) != 1)
228 /* sanity check: values below 1000 millicelcius don't make sense
229 * and can cause the system to go into a thermal heart attack
231 if (state
&& state
< 1000)
234 if (state
&& !tz
->forced_passive
) {
235 if (!tz
->passive_delay
)
236 tz
->passive_delay
= 1000;
237 thermal_zone_device_rebind_exception(tz
, "Processor",
238 sizeof("Processor"));
239 } else if (!state
&& tz
->forced_passive
) {
240 tz
->passive_delay
= 0;
241 thermal_zone_device_unbind_exception(tz
, "Processor",
242 sizeof("Processor"));
245 tz
->forced_passive
= state
;
247 thermal_zone_device_update(tz
, THERMAL_EVENT_UNSPECIFIED
);
253 passive_show(struct device
*dev
, struct device_attribute
*attr
,
256 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
258 return sprintf(buf
, "%d\n", tz
->forced_passive
);
262 policy_store(struct device
*dev
, struct device_attribute
*attr
,
263 const char *buf
, size_t count
)
265 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
266 char name
[THERMAL_NAME_LENGTH
];
269 snprintf(name
, sizeof(name
), "%s", buf
);
271 ret
= thermal_zone_device_set_policy(tz
, name
);
279 policy_show(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
281 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
283 return sprintf(buf
, "%s\n", tz
->governor
->name
);
287 available_policies_show(struct device
*dev
, struct device_attribute
*devattr
,
290 return thermal_build_list_of_policies(buf
);
293 #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
295 emul_temp_store(struct device
*dev
, struct device_attribute
*attr
,
296 const char *buf
, size_t count
)
298 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
302 if (kstrtoint(buf
, 10, &temperature
))
305 if (!tz
->ops
->set_emul_temp
) {
306 mutex_lock(&tz
->lock
);
307 tz
->emul_temperature
= temperature
;
308 mutex_unlock(&tz
->lock
);
310 ret
= tz
->ops
->set_emul_temp(tz
, temperature
);
314 thermal_zone_device_update(tz
, THERMAL_EVENT_UNSPECIFIED
);
316 return ret
? ret
: count
;
318 static DEVICE_ATTR_WO(emul_temp
);
322 sustainable_power_show(struct device
*dev
, struct device_attribute
*devattr
,
325 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
328 return sprintf(buf
, "%u\n", tz
->tzp
->sustainable_power
);
334 sustainable_power_store(struct device
*dev
, struct device_attribute
*devattr
,
335 const char *buf
, size_t count
)
337 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
338 u32 sustainable_power
;
343 if (kstrtou32(buf
, 10, &sustainable_power
))
346 tz
->tzp
->sustainable_power
= sustainable_power
;
351 #define create_s32_tzp_attr(name) \
353 name##_show(struct device *dev, struct device_attribute *devattr, \
356 struct thermal_zone_device *tz = to_thermal_zone(dev); \
359 return sprintf(buf, "%d\n", tz->tzp->name); \
365 name##_store(struct device *dev, struct device_attribute *devattr, \
366 const char *buf, size_t count) \
368 struct thermal_zone_device *tz = to_thermal_zone(dev); \
374 if (kstrtos32(buf, 10, &value)) \
377 tz->tzp->name = value; \
381 static DEVICE_ATTR_RW(name)
383 create_s32_tzp_attr(k_po
);
384 create_s32_tzp_attr(k_pu
);
385 create_s32_tzp_attr(k_i
);
386 create_s32_tzp_attr(k_d
);
387 create_s32_tzp_attr(integral_cutoff
);
388 create_s32_tzp_attr(slope
);
389 create_s32_tzp_attr(offset
);
390 #undef create_s32_tzp_attr
393 * These are thermal zone device attributes that will always be present.
394 * All the attributes created for tzp (create_s32_tzp_attr) also are always
395 * present on the sysfs interface.
397 static DEVICE_ATTR_RO(type
);
398 static DEVICE_ATTR_RO(temp
);
399 static DEVICE_ATTR_RW(policy
);
400 static DEVICE_ATTR_RO(available_policies
);
401 static DEVICE_ATTR_RW(sustainable_power
);
403 /* These thermal zone device attributes are created based on conditions */
404 static DEVICE_ATTR_RW(mode
);
405 static DEVICE_ATTR_RW(passive
);
407 /* These attributes are unconditionally added to a thermal zone */
408 static struct attribute
*thermal_zone_dev_attrs
[] = {
411 #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
412 &dev_attr_emul_temp
.attr
,
414 &dev_attr_policy
.attr
,
415 &dev_attr_available_policies
.attr
,
416 &dev_attr_sustainable_power
.attr
,
421 &dev_attr_integral_cutoff
.attr
,
422 &dev_attr_slope
.attr
,
423 &dev_attr_offset
.attr
,
427 static struct attribute_group thermal_zone_attribute_group
= {
428 .attrs
= thermal_zone_dev_attrs
,
431 /* We expose mode only if .get_mode is present */
432 static struct attribute
*thermal_zone_mode_attrs
[] = {
437 static umode_t
thermal_zone_mode_is_visible(struct kobject
*kobj
,
438 struct attribute
*attr
,
441 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
442 struct thermal_zone_device
*tz
;
444 tz
= container_of(dev
, struct thermal_zone_device
, device
);
446 if (tz
->ops
->get_mode
)
452 static struct attribute_group thermal_zone_mode_attribute_group
= {
453 .attrs
= thermal_zone_mode_attrs
,
454 .is_visible
= thermal_zone_mode_is_visible
,
457 /* We expose passive only if passive trips are present */
458 static struct attribute
*thermal_zone_passive_attrs
[] = {
459 &dev_attr_passive
.attr
,
463 static umode_t
thermal_zone_passive_is_visible(struct kobject
*kobj
,
464 struct attribute
*attr
,
467 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
468 struct thermal_zone_device
*tz
;
469 enum thermal_trip_type trip_type
;
470 int count
, passive
= 0;
472 tz
= container_of(dev
, struct thermal_zone_device
, device
);
474 for (count
= 0; count
< tz
->trips
&& !passive
; count
++) {
475 tz
->ops
->get_trip_type(tz
, count
, &trip_type
);
477 if (trip_type
== THERMAL_TRIP_PASSIVE
)
487 static struct attribute_group thermal_zone_passive_attribute_group
= {
488 .attrs
= thermal_zone_passive_attrs
,
489 .is_visible
= thermal_zone_passive_is_visible
,
492 static const struct attribute_group
*thermal_zone_attribute_groups
[] = {
493 &thermal_zone_attribute_group
,
494 &thermal_zone_mode_attribute_group
,
495 &thermal_zone_passive_attribute_group
,
496 /* This is not NULL terminated as we create the group dynamically */
500 * create_trip_attrs() - create attributes for trip points
501 * @tz: the thermal zone device
502 * @mask: Writeable trip point bitmap.
504 * helper function to instantiate sysfs entries for every trip
505 * point and its properties of a struct thermal_zone_device.
507 * Return: 0 on success, the proper error value otherwise.
509 static int create_trip_attrs(struct thermal_zone_device
*tz
, int mask
)
511 struct attribute
**attrs
;
514 /* This function works only for zones with at least one trip */
518 tz
->trip_type_attrs
= kcalloc(tz
->trips
, sizeof(*tz
->trip_type_attrs
),
520 if (!tz
->trip_type_attrs
)
523 tz
->trip_temp_attrs
= kcalloc(tz
->trips
, sizeof(*tz
->trip_temp_attrs
),
525 if (!tz
->trip_temp_attrs
) {
526 kfree(tz
->trip_type_attrs
);
530 if (tz
->ops
->get_trip_hyst
) {
531 tz
->trip_hyst_attrs
= kcalloc(tz
->trips
,
532 sizeof(*tz
->trip_hyst_attrs
),
534 if (!tz
->trip_hyst_attrs
) {
535 kfree(tz
->trip_type_attrs
);
536 kfree(tz
->trip_temp_attrs
);
541 attrs
= kcalloc(tz
->trips
* 3 + 1, sizeof(*attrs
), GFP_KERNEL
);
543 kfree(tz
->trip_type_attrs
);
544 kfree(tz
->trip_temp_attrs
);
545 if (tz
->ops
->get_trip_hyst
)
546 kfree(tz
->trip_hyst_attrs
);
550 for (indx
= 0; indx
< tz
->trips
; indx
++) {
551 /* create trip type attribute */
552 snprintf(tz
->trip_type_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
553 "trip_point_%d_type", indx
);
555 sysfs_attr_init(&tz
->trip_type_attrs
[indx
].attr
.attr
);
556 tz
->trip_type_attrs
[indx
].attr
.attr
.name
=
557 tz
->trip_type_attrs
[indx
].name
;
558 tz
->trip_type_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
559 tz
->trip_type_attrs
[indx
].attr
.show
= trip_point_type_show
;
560 attrs
[indx
] = &tz
->trip_type_attrs
[indx
].attr
.attr
;
562 /* create trip temp attribute */
563 snprintf(tz
->trip_temp_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
564 "trip_point_%d_temp", indx
);
566 sysfs_attr_init(&tz
->trip_temp_attrs
[indx
].attr
.attr
);
567 tz
->trip_temp_attrs
[indx
].attr
.attr
.name
=
568 tz
->trip_temp_attrs
[indx
].name
;
569 tz
->trip_temp_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
570 tz
->trip_temp_attrs
[indx
].attr
.show
= trip_point_temp_show
;
571 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS
) &&
572 mask
& (1 << indx
)) {
573 tz
->trip_temp_attrs
[indx
].attr
.attr
.mode
|= S_IWUSR
;
574 tz
->trip_temp_attrs
[indx
].attr
.store
=
575 trip_point_temp_store
;
577 attrs
[indx
+ tz
->trips
] = &tz
->trip_temp_attrs
[indx
].attr
.attr
;
579 /* create Optional trip hyst attribute */
580 if (!tz
->ops
->get_trip_hyst
)
582 snprintf(tz
->trip_hyst_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
583 "trip_point_%d_hyst", indx
);
585 sysfs_attr_init(&tz
->trip_hyst_attrs
[indx
].attr
.attr
);
586 tz
->trip_hyst_attrs
[indx
].attr
.attr
.name
=
587 tz
->trip_hyst_attrs
[indx
].name
;
588 tz
->trip_hyst_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
589 tz
->trip_hyst_attrs
[indx
].attr
.show
= trip_point_hyst_show
;
590 if (tz
->ops
->set_trip_hyst
) {
591 tz
->trip_hyst_attrs
[indx
].attr
.attr
.mode
|= S_IWUSR
;
592 tz
->trip_hyst_attrs
[indx
].attr
.store
=
593 trip_point_hyst_store
;
595 attrs
[indx
+ tz
->trips
* 2] =
596 &tz
->trip_hyst_attrs
[indx
].attr
.attr
;
598 attrs
[tz
->trips
* 3] = NULL
;
600 tz
->trips_attribute_group
.attrs
= attrs
;
606 * destroy_trip_attrs() - destroy attributes for trip points
607 * @tz: the thermal zone device
609 * helper function to free resources allocated by create_trip_attrs()
611 static void destroy_trip_attrs(struct thermal_zone_device
*tz
)
616 kfree(tz
->trip_type_attrs
);
617 kfree(tz
->trip_temp_attrs
);
618 if (tz
->ops
->get_trip_hyst
)
619 kfree(tz
->trip_hyst_attrs
);
620 kfree(tz
->trips_attribute_group
.attrs
);
623 int thermal_zone_create_device_groups(struct thermal_zone_device
*tz
,
626 const struct attribute_group
**groups
;
629 /* we need one extra for trips and the NULL to terminate the array */
630 size
= ARRAY_SIZE(thermal_zone_attribute_groups
) + 2;
631 /* This also takes care of API requirement to be NULL terminated */
632 groups
= kcalloc(size
, sizeof(*groups
), GFP_KERNEL
);
636 for (i
= 0; i
< size
- 2; i
++)
637 groups
[i
] = thermal_zone_attribute_groups
[i
];
640 result
= create_trip_attrs(tz
, mask
);
647 groups
[size
- 2] = &tz
->trips_attribute_group
;
650 tz
->device
.groups
= groups
;
655 void thermal_zone_destroy_device_groups(struct thermal_zone_device
*tz
)
661 destroy_trip_attrs(tz
);
663 kfree(tz
->device
.groups
);
666 /* sys I/F for cooling device */
668 cdev_type_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
670 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
672 return sprintf(buf
, "%s\n", cdev
->type
);
675 static ssize_t
max_state_show(struct device
*dev
, struct device_attribute
*attr
,
678 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
682 ret
= cdev
->ops
->get_max_state(cdev
, &state
);
685 return sprintf(buf
, "%ld\n", state
);
688 static ssize_t
cur_state_show(struct device
*dev
, struct device_attribute
*attr
,
691 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
695 ret
= cdev
->ops
->get_cur_state(cdev
, &state
);
698 return sprintf(buf
, "%ld\n", state
);
702 cur_state_store(struct device
*dev
, struct device_attribute
*attr
,
703 const char *buf
, size_t count
)
705 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
709 if (sscanf(buf
, "%ld\n", &state
) != 1)
715 mutex_lock(&cdev
->lock
);
717 result
= cdev
->ops
->set_cur_state(cdev
, state
);
719 thermal_cooling_device_stats_update(cdev
, state
);
721 mutex_unlock(&cdev
->lock
);
722 return result
? result
: count
;
725 static struct device_attribute
726 dev_attr_cdev_type
= __ATTR(type
, 0444, cdev_type_show
, NULL
);
727 static DEVICE_ATTR_RO(max_state
);
728 static DEVICE_ATTR_RW(cur_state
);
730 static struct attribute
*cooling_device_attrs
[] = {
731 &dev_attr_cdev_type
.attr
,
732 &dev_attr_max_state
.attr
,
733 &dev_attr_cur_state
.attr
,
737 static const struct attribute_group cooling_device_attr_group
= {
738 .attrs
= cooling_device_attrs
,
741 static const struct attribute_group
*cooling_device_attr_groups
[] = {
742 &cooling_device_attr_group
,
743 NULL
, /* Space allocated for cooling_device_stats_attr_group */
747 #ifdef CONFIG_THERMAL_STATISTICS
748 struct cooling_dev_stats
{
750 unsigned int total_trans
;
752 unsigned long max_states
;
754 ktime_t
*time_in_state
;
755 unsigned int *trans_table
;
758 static void update_time_in_state(struct cooling_dev_stats
*stats
)
760 ktime_t now
= ktime_get(), delta
;
762 delta
= ktime_sub(now
, stats
->last_time
);
763 stats
->time_in_state
[stats
->state
] =
764 ktime_add(stats
->time_in_state
[stats
->state
], delta
);
765 stats
->last_time
= now
;
768 void thermal_cooling_device_stats_update(struct thermal_cooling_device
*cdev
,
769 unsigned long new_state
)
771 struct cooling_dev_stats
*stats
= cdev
->stats
;
773 spin_lock(&stats
->lock
);
775 if (stats
->state
== new_state
)
778 update_time_in_state(stats
);
779 stats
->trans_table
[stats
->state
* stats
->max_states
+ new_state
]++;
780 stats
->state
= new_state
;
781 stats
->total_trans
++;
784 spin_unlock(&stats
->lock
);
787 static ssize_t
total_trans_show(struct device
*dev
,
788 struct device_attribute
*attr
, char *buf
)
790 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
791 struct cooling_dev_stats
*stats
= cdev
->stats
;
794 spin_lock(&stats
->lock
);
795 ret
= sprintf(buf
, "%u\n", stats
->total_trans
);
796 spin_unlock(&stats
->lock
);
802 time_in_state_ms_show(struct device
*dev
, struct device_attribute
*attr
,
805 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
806 struct cooling_dev_stats
*stats
= cdev
->stats
;
810 spin_lock(&stats
->lock
);
811 update_time_in_state(stats
);
813 for (i
= 0; i
< stats
->max_states
; i
++) {
814 len
+= sprintf(buf
+ len
, "state%u\t%llu\n", i
,
815 ktime_to_ms(stats
->time_in_state
[i
]));
817 spin_unlock(&stats
->lock
);
823 reset_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
,
826 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
827 struct cooling_dev_stats
*stats
= cdev
->stats
;
828 int i
, states
= stats
->max_states
;
830 spin_lock(&stats
->lock
);
832 stats
->total_trans
= 0;
833 stats
->last_time
= ktime_get();
834 memset(stats
->trans_table
, 0,
835 states
* states
* sizeof(*stats
->trans_table
));
837 for (i
= 0; i
< stats
->max_states
; i
++)
838 stats
->time_in_state
[i
] = ktime_set(0, 0);
840 spin_unlock(&stats
->lock
);
845 static ssize_t
trans_table_show(struct device
*dev
,
846 struct device_attribute
*attr
, char *buf
)
848 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
849 struct cooling_dev_stats
*stats
= cdev
->stats
;
853 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
, " From : To\n");
854 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
, " : ");
855 for (i
= 0; i
< stats
->max_states
; i
++) {
856 if (len
>= PAGE_SIZE
)
858 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
, "state%2u ", i
);
860 if (len
>= PAGE_SIZE
)
863 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
, "\n");
865 for (i
= 0; i
< stats
->max_states
; i
++) {
866 if (len
>= PAGE_SIZE
)
869 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
, "state%2u:", i
);
871 for (j
= 0; j
< stats
->max_states
; j
++) {
872 if (len
>= PAGE_SIZE
)
874 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
, "%8u ",
875 stats
->trans_table
[i
* stats
->max_states
+ j
]);
877 if (len
>= PAGE_SIZE
)
879 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
, "\n");
882 if (len
>= PAGE_SIZE
) {
883 pr_warn_once("Thermal transition table exceeds PAGE_SIZE. Disabling\n");
889 static DEVICE_ATTR_RO(total_trans
);
890 static DEVICE_ATTR_RO(time_in_state_ms
);
891 static DEVICE_ATTR_WO(reset
);
892 static DEVICE_ATTR_RO(trans_table
);
894 static struct attribute
*cooling_device_stats_attrs
[] = {
895 &dev_attr_total_trans
.attr
,
896 &dev_attr_time_in_state_ms
.attr
,
897 &dev_attr_reset
.attr
,
898 &dev_attr_trans_table
.attr
,
902 static const struct attribute_group cooling_device_stats_attr_group
= {
903 .attrs
= cooling_device_stats_attrs
,
907 static void cooling_device_stats_setup(struct thermal_cooling_device
*cdev
)
909 struct cooling_dev_stats
*stats
;
910 unsigned long states
;
913 if (cdev
->ops
->get_max_state(cdev
, &states
))
916 states
++; /* Total number of states is highest state + 1 */
918 var
= sizeof(*stats
);
919 var
+= sizeof(*stats
->time_in_state
) * states
;
920 var
+= sizeof(*stats
->trans_table
) * states
* states
;
922 stats
= kzalloc(var
, GFP_KERNEL
);
926 stats
->time_in_state
= (ktime_t
*)(stats
+ 1);
927 stats
->trans_table
= (unsigned int *)(stats
->time_in_state
+ states
);
929 stats
->last_time
= ktime_get();
930 stats
->max_states
= states
;
932 spin_lock_init(&stats
->lock
);
934 /* Fill the empty slot left in cooling_device_attr_groups */
935 var
= ARRAY_SIZE(cooling_device_attr_groups
) - 2;
936 cooling_device_attr_groups
[var
] = &cooling_device_stats_attr_group
;
939 static void cooling_device_stats_destroy(struct thermal_cooling_device
*cdev
)
948 cooling_device_stats_setup(struct thermal_cooling_device
*cdev
) {}
950 cooling_device_stats_destroy(struct thermal_cooling_device
*cdev
) {}
952 #endif /* CONFIG_THERMAL_STATISTICS */
954 void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device
*cdev
)
956 cooling_device_stats_setup(cdev
);
957 cdev
->device
.groups
= cooling_device_attr_groups
;
960 void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device
*cdev
)
962 cooling_device_stats_destroy(cdev
);
965 /* these helper will be used only at the time of bindig */
967 trip_point_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
969 struct thermal_instance
*instance
;
972 container_of(attr
, struct thermal_instance
, attr
);
974 if (instance
->trip
== THERMAL_TRIPS_NONE
)
975 return sprintf(buf
, "-1\n");
977 return sprintf(buf
, "%d\n", instance
->trip
);
981 weight_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
983 struct thermal_instance
*instance
;
985 instance
= container_of(attr
, struct thermal_instance
, weight_attr
);
987 return sprintf(buf
, "%d\n", instance
->weight
);
990 ssize_t
weight_store(struct device
*dev
, struct device_attribute
*attr
,
991 const char *buf
, size_t count
)
993 struct thermal_instance
*instance
;
996 ret
= kstrtoint(buf
, 0, &weight
);
1000 instance
= container_of(attr
, struct thermal_instance
, weight_attr
);
1001 instance
->weight
= weight
;