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 int enabled
= thermal_zone_device_is_enabled(tz
);
54 return sprintf(buf
, "%s\n", enabled
? "enabled" : "disabled");
58 mode_store(struct device
*dev
, struct device_attribute
*attr
,
59 const char *buf
, size_t count
)
61 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
64 if (!strncmp(buf
, "enabled", sizeof("enabled") - 1))
65 result
= thermal_zone_device_enable(tz
);
66 else if (!strncmp(buf
, "disabled", sizeof("disabled") - 1))
67 result
= thermal_zone_device_disable(tz
);
78 trip_point_type_show(struct device
*dev
, struct device_attribute
*attr
,
81 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
82 enum thermal_trip_type type
;
85 if (!tz
->ops
->get_trip_type
)
88 if (sscanf(attr
->attr
.name
, "trip_point_%d_type", &trip
) != 1)
91 result
= tz
->ops
->get_trip_type(tz
, trip
, &type
);
96 case THERMAL_TRIP_CRITICAL
:
97 return sprintf(buf
, "critical\n");
98 case THERMAL_TRIP_HOT
:
99 return sprintf(buf
, "hot\n");
100 case THERMAL_TRIP_PASSIVE
:
101 return sprintf(buf
, "passive\n");
102 case THERMAL_TRIP_ACTIVE
:
103 return sprintf(buf
, "active\n");
105 return sprintf(buf
, "unknown\n");
110 trip_point_temp_store(struct device
*dev
, struct device_attribute
*attr
,
111 const char *buf
, size_t count
)
113 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
115 int temperature
, hyst
= 0;
116 enum thermal_trip_type type
;
118 if (!tz
->ops
->set_trip_temp
)
121 if (sscanf(attr
->attr
.name
, "trip_point_%d_temp", &trip
) != 1)
124 if (kstrtoint(buf
, 10, &temperature
))
127 ret
= tz
->ops
->set_trip_temp(tz
, trip
, temperature
);
131 if (tz
->ops
->get_trip_hyst
) {
132 ret
= tz
->ops
->get_trip_hyst(tz
, trip
, &hyst
);
137 ret
= tz
->ops
->get_trip_type(tz
, trip
, &type
);
141 thermal_notify_tz_trip_change(tz
->id
, trip
, type
, temperature
, hyst
);
143 thermal_zone_device_update(tz
, THERMAL_EVENT_UNSPECIFIED
);
149 trip_point_temp_show(struct device
*dev
, struct device_attribute
*attr
,
152 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
156 if (!tz
->ops
->get_trip_temp
)
159 if (sscanf(attr
->attr
.name
, "trip_point_%d_temp", &trip
) != 1)
162 ret
= tz
->ops
->get_trip_temp(tz
, trip
, &temperature
);
167 return sprintf(buf
, "%d\n", temperature
);
171 trip_point_hyst_store(struct device
*dev
, struct device_attribute
*attr
,
172 const char *buf
, size_t count
)
174 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
178 if (!tz
->ops
->set_trip_hyst
)
181 if (sscanf(attr
->attr
.name
, "trip_point_%d_hyst", &trip
) != 1)
184 if (kstrtoint(buf
, 10, &temperature
))
188 * We are not doing any check on the 'temperature' value
189 * here. The driver implementing 'set_trip_hyst' has to
192 ret
= tz
->ops
->set_trip_hyst(tz
, trip
, temperature
);
195 thermal_zone_set_trips(tz
);
197 return ret
? ret
: count
;
201 trip_point_hyst_show(struct device
*dev
, struct device_attribute
*attr
,
204 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
208 if (!tz
->ops
->get_trip_hyst
)
211 if (sscanf(attr
->attr
.name
, "trip_point_%d_hyst", &trip
) != 1)
214 ret
= tz
->ops
->get_trip_hyst(tz
, trip
, &temperature
);
216 return ret
? ret
: sprintf(buf
, "%d\n", temperature
);
220 passive_store(struct device
*dev
, struct device_attribute
*attr
,
221 const char *buf
, size_t count
)
223 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
226 if (sscanf(buf
, "%d\n", &state
) != 1)
229 /* sanity check: values below 1000 millicelcius don't make sense
230 * and can cause the system to go into a thermal heart attack
232 if (state
&& state
< 1000)
235 if (state
&& !tz
->forced_passive
) {
236 if (!tz
->passive_delay
)
237 tz
->passive_delay
= 1000;
238 thermal_zone_device_rebind_exception(tz
, "Processor",
239 sizeof("Processor"));
240 } else if (!state
&& tz
->forced_passive
) {
241 tz
->passive_delay
= 0;
242 thermal_zone_device_unbind_exception(tz
, "Processor",
243 sizeof("Processor"));
246 tz
->forced_passive
= state
;
248 thermal_zone_device_update(tz
, THERMAL_EVENT_UNSPECIFIED
);
254 passive_show(struct device
*dev
, struct device_attribute
*attr
,
257 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
259 return sprintf(buf
, "%d\n", tz
->forced_passive
);
263 policy_store(struct device
*dev
, struct device_attribute
*attr
,
264 const char *buf
, size_t count
)
266 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
267 char name
[THERMAL_NAME_LENGTH
];
270 snprintf(name
, sizeof(name
), "%s", buf
);
272 ret
= thermal_zone_device_set_policy(tz
, name
);
280 policy_show(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
282 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
284 return sprintf(buf
, "%s\n", tz
->governor
->name
);
288 available_policies_show(struct device
*dev
, struct device_attribute
*devattr
,
291 return thermal_build_list_of_policies(buf
);
294 #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
296 emul_temp_store(struct device
*dev
, struct device_attribute
*attr
,
297 const char *buf
, size_t count
)
299 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
303 if (kstrtoint(buf
, 10, &temperature
))
306 if (!tz
->ops
->set_emul_temp
) {
307 mutex_lock(&tz
->lock
);
308 tz
->emul_temperature
= temperature
;
309 mutex_unlock(&tz
->lock
);
311 ret
= tz
->ops
->set_emul_temp(tz
, temperature
);
315 thermal_zone_device_update(tz
, THERMAL_EVENT_UNSPECIFIED
);
317 return ret
? ret
: count
;
319 static DEVICE_ATTR_WO(emul_temp
);
323 sustainable_power_show(struct device
*dev
, struct device_attribute
*devattr
,
326 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
329 return sprintf(buf
, "%u\n", tz
->tzp
->sustainable_power
);
335 sustainable_power_store(struct device
*dev
, struct device_attribute
*devattr
,
336 const char *buf
, size_t count
)
338 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
339 u32 sustainable_power
;
344 if (kstrtou32(buf
, 10, &sustainable_power
))
347 tz
->tzp
->sustainable_power
= sustainable_power
;
352 #define create_s32_tzp_attr(name) \
354 name##_show(struct device *dev, struct device_attribute *devattr, \
357 struct thermal_zone_device *tz = to_thermal_zone(dev); \
360 return sprintf(buf, "%d\n", tz->tzp->name); \
366 name##_store(struct device *dev, struct device_attribute *devattr, \
367 const char *buf, size_t count) \
369 struct thermal_zone_device *tz = to_thermal_zone(dev); \
375 if (kstrtos32(buf, 10, &value)) \
378 tz->tzp->name = value; \
382 static DEVICE_ATTR_RW(name)
384 create_s32_tzp_attr(k_po
);
385 create_s32_tzp_attr(k_pu
);
386 create_s32_tzp_attr(k_i
);
387 create_s32_tzp_attr(k_d
);
388 create_s32_tzp_attr(integral_cutoff
);
389 create_s32_tzp_attr(slope
);
390 create_s32_tzp_attr(offset
);
391 #undef create_s32_tzp_attr
394 * These are thermal zone device attributes that will always be present.
395 * All the attributes created for tzp (create_s32_tzp_attr) also are always
396 * present on the sysfs interface.
398 static DEVICE_ATTR_RO(type
);
399 static DEVICE_ATTR_RO(temp
);
400 static DEVICE_ATTR_RW(policy
);
401 static DEVICE_ATTR_RO(available_policies
);
402 static DEVICE_ATTR_RW(sustainable_power
);
404 /* These thermal zone device attributes are created based on conditions */
405 static DEVICE_ATTR_RW(mode
);
406 static DEVICE_ATTR_RW(passive
);
408 /* These attributes are unconditionally added to a thermal zone */
409 static struct attribute
*thermal_zone_dev_attrs
[] = {
412 #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
413 &dev_attr_emul_temp
.attr
,
415 &dev_attr_policy
.attr
,
416 &dev_attr_available_policies
.attr
,
417 &dev_attr_sustainable_power
.attr
,
422 &dev_attr_integral_cutoff
.attr
,
423 &dev_attr_slope
.attr
,
424 &dev_attr_offset
.attr
,
428 static const struct attribute_group thermal_zone_attribute_group
= {
429 .attrs
= thermal_zone_dev_attrs
,
432 static struct attribute
*thermal_zone_mode_attrs
[] = {
437 static const struct attribute_group thermal_zone_mode_attribute_group
= {
438 .attrs
= thermal_zone_mode_attrs
,
441 /* We expose passive only if passive trips are present */
442 static struct attribute
*thermal_zone_passive_attrs
[] = {
443 &dev_attr_passive
.attr
,
447 static umode_t
thermal_zone_passive_is_visible(struct kobject
*kobj
,
448 struct attribute
*attr
,
451 struct device
*dev
= kobj_to_dev(kobj
);
452 struct thermal_zone_device
*tz
;
453 enum thermal_trip_type trip_type
;
454 int count
, passive
= 0;
456 tz
= container_of(dev
, struct thermal_zone_device
, device
);
458 for (count
= 0; count
< tz
->trips
&& !passive
; count
++) {
459 tz
->ops
->get_trip_type(tz
, count
, &trip_type
);
461 if (trip_type
== THERMAL_TRIP_PASSIVE
)
471 static const struct attribute_group thermal_zone_passive_attribute_group
= {
472 .attrs
= thermal_zone_passive_attrs
,
473 .is_visible
= thermal_zone_passive_is_visible
,
476 static const struct attribute_group
*thermal_zone_attribute_groups
[] = {
477 &thermal_zone_attribute_group
,
478 &thermal_zone_mode_attribute_group
,
479 &thermal_zone_passive_attribute_group
,
480 /* This is not NULL terminated as we create the group dynamically */
484 * create_trip_attrs() - create attributes for trip points
485 * @tz: the thermal zone device
486 * @mask: Writeable trip point bitmap.
488 * helper function to instantiate sysfs entries for every trip
489 * point and its properties of a struct thermal_zone_device.
491 * Return: 0 on success, the proper error value otherwise.
493 static int create_trip_attrs(struct thermal_zone_device
*tz
, int mask
)
495 struct attribute
**attrs
;
498 /* This function works only for zones with at least one trip */
502 tz
->trip_type_attrs
= kcalloc(tz
->trips
, sizeof(*tz
->trip_type_attrs
),
504 if (!tz
->trip_type_attrs
)
507 tz
->trip_temp_attrs
= kcalloc(tz
->trips
, sizeof(*tz
->trip_temp_attrs
),
509 if (!tz
->trip_temp_attrs
) {
510 kfree(tz
->trip_type_attrs
);
514 if (tz
->ops
->get_trip_hyst
) {
515 tz
->trip_hyst_attrs
= kcalloc(tz
->trips
,
516 sizeof(*tz
->trip_hyst_attrs
),
518 if (!tz
->trip_hyst_attrs
) {
519 kfree(tz
->trip_type_attrs
);
520 kfree(tz
->trip_temp_attrs
);
525 attrs
= kcalloc(tz
->trips
* 3 + 1, sizeof(*attrs
), GFP_KERNEL
);
527 kfree(tz
->trip_type_attrs
);
528 kfree(tz
->trip_temp_attrs
);
529 if (tz
->ops
->get_trip_hyst
)
530 kfree(tz
->trip_hyst_attrs
);
534 for (indx
= 0; indx
< tz
->trips
; indx
++) {
535 /* create trip type attribute */
536 snprintf(tz
->trip_type_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
537 "trip_point_%d_type", indx
);
539 sysfs_attr_init(&tz
->trip_type_attrs
[indx
].attr
.attr
);
540 tz
->trip_type_attrs
[indx
].attr
.attr
.name
=
541 tz
->trip_type_attrs
[indx
].name
;
542 tz
->trip_type_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
543 tz
->trip_type_attrs
[indx
].attr
.show
= trip_point_type_show
;
544 attrs
[indx
] = &tz
->trip_type_attrs
[indx
].attr
.attr
;
546 /* create trip temp attribute */
547 snprintf(tz
->trip_temp_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
548 "trip_point_%d_temp", indx
);
550 sysfs_attr_init(&tz
->trip_temp_attrs
[indx
].attr
.attr
);
551 tz
->trip_temp_attrs
[indx
].attr
.attr
.name
=
552 tz
->trip_temp_attrs
[indx
].name
;
553 tz
->trip_temp_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
554 tz
->trip_temp_attrs
[indx
].attr
.show
= trip_point_temp_show
;
555 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS
) &&
556 mask
& (1 << indx
)) {
557 tz
->trip_temp_attrs
[indx
].attr
.attr
.mode
|= S_IWUSR
;
558 tz
->trip_temp_attrs
[indx
].attr
.store
=
559 trip_point_temp_store
;
561 attrs
[indx
+ tz
->trips
] = &tz
->trip_temp_attrs
[indx
].attr
.attr
;
563 /* create Optional trip hyst attribute */
564 if (!tz
->ops
->get_trip_hyst
)
566 snprintf(tz
->trip_hyst_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
567 "trip_point_%d_hyst", indx
);
569 sysfs_attr_init(&tz
->trip_hyst_attrs
[indx
].attr
.attr
);
570 tz
->trip_hyst_attrs
[indx
].attr
.attr
.name
=
571 tz
->trip_hyst_attrs
[indx
].name
;
572 tz
->trip_hyst_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
573 tz
->trip_hyst_attrs
[indx
].attr
.show
= trip_point_hyst_show
;
574 if (tz
->ops
->set_trip_hyst
) {
575 tz
->trip_hyst_attrs
[indx
].attr
.attr
.mode
|= S_IWUSR
;
576 tz
->trip_hyst_attrs
[indx
].attr
.store
=
577 trip_point_hyst_store
;
579 attrs
[indx
+ tz
->trips
* 2] =
580 &tz
->trip_hyst_attrs
[indx
].attr
.attr
;
582 attrs
[tz
->trips
* 3] = NULL
;
584 tz
->trips_attribute_group
.attrs
= attrs
;
590 * destroy_trip_attrs() - destroy attributes for trip points
591 * @tz: the thermal zone device
593 * helper function to free resources allocated by create_trip_attrs()
595 static void destroy_trip_attrs(struct thermal_zone_device
*tz
)
600 kfree(tz
->trip_type_attrs
);
601 kfree(tz
->trip_temp_attrs
);
602 if (tz
->ops
->get_trip_hyst
)
603 kfree(tz
->trip_hyst_attrs
);
604 kfree(tz
->trips_attribute_group
.attrs
);
607 int thermal_zone_create_device_groups(struct thermal_zone_device
*tz
,
610 const struct attribute_group
**groups
;
613 /* we need one extra for trips and the NULL to terminate the array */
614 size
= ARRAY_SIZE(thermal_zone_attribute_groups
) + 2;
615 /* This also takes care of API requirement to be NULL terminated */
616 groups
= kcalloc(size
, sizeof(*groups
), GFP_KERNEL
);
620 for (i
= 0; i
< size
- 2; i
++)
621 groups
[i
] = thermal_zone_attribute_groups
[i
];
624 result
= create_trip_attrs(tz
, mask
);
631 groups
[size
- 2] = &tz
->trips_attribute_group
;
634 tz
->device
.groups
= groups
;
639 void thermal_zone_destroy_device_groups(struct thermal_zone_device
*tz
)
645 destroy_trip_attrs(tz
);
647 kfree(tz
->device
.groups
);
650 /* sys I/F for cooling device */
652 cdev_type_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
654 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
656 return sprintf(buf
, "%s\n", cdev
->type
);
659 static ssize_t
max_state_show(struct device
*dev
, struct device_attribute
*attr
,
662 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
666 ret
= cdev
->ops
->get_max_state(cdev
, &state
);
669 return sprintf(buf
, "%ld\n", state
);
672 static ssize_t
cur_state_show(struct device
*dev
, struct device_attribute
*attr
,
675 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
679 ret
= cdev
->ops
->get_cur_state(cdev
, &state
);
682 return sprintf(buf
, "%ld\n", state
);
686 cur_state_store(struct device
*dev
, struct device_attribute
*attr
,
687 const char *buf
, size_t count
)
689 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
693 if (sscanf(buf
, "%ld\n", &state
) != 1)
699 mutex_lock(&cdev
->lock
);
701 result
= cdev
->ops
->set_cur_state(cdev
, state
);
703 thermal_cooling_device_stats_update(cdev
, state
);
705 mutex_unlock(&cdev
->lock
);
706 return result
? result
: count
;
709 static struct device_attribute
710 dev_attr_cdev_type
= __ATTR(type
, 0444, cdev_type_show
, NULL
);
711 static DEVICE_ATTR_RO(max_state
);
712 static DEVICE_ATTR_RW(cur_state
);
714 static struct attribute
*cooling_device_attrs
[] = {
715 &dev_attr_cdev_type
.attr
,
716 &dev_attr_max_state
.attr
,
717 &dev_attr_cur_state
.attr
,
721 static const struct attribute_group cooling_device_attr_group
= {
722 .attrs
= cooling_device_attrs
,
725 static const struct attribute_group
*cooling_device_attr_groups
[] = {
726 &cooling_device_attr_group
,
727 NULL
, /* Space allocated for cooling_device_stats_attr_group */
731 #ifdef CONFIG_THERMAL_STATISTICS
732 struct cooling_dev_stats
{
734 unsigned int total_trans
;
736 unsigned long max_states
;
738 ktime_t
*time_in_state
;
739 unsigned int *trans_table
;
742 static void update_time_in_state(struct cooling_dev_stats
*stats
)
744 ktime_t now
= ktime_get(), delta
;
746 delta
= ktime_sub(now
, stats
->last_time
);
747 stats
->time_in_state
[stats
->state
] =
748 ktime_add(stats
->time_in_state
[stats
->state
], delta
);
749 stats
->last_time
= now
;
752 void thermal_cooling_device_stats_update(struct thermal_cooling_device
*cdev
,
753 unsigned long new_state
)
755 struct cooling_dev_stats
*stats
= cdev
->stats
;
757 spin_lock(&stats
->lock
);
759 if (stats
->state
== new_state
)
762 update_time_in_state(stats
);
763 stats
->trans_table
[stats
->state
* stats
->max_states
+ new_state
]++;
764 stats
->state
= new_state
;
765 stats
->total_trans
++;
768 spin_unlock(&stats
->lock
);
771 static ssize_t
total_trans_show(struct device
*dev
,
772 struct device_attribute
*attr
, char *buf
)
774 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
775 struct cooling_dev_stats
*stats
= cdev
->stats
;
778 spin_lock(&stats
->lock
);
779 ret
= sprintf(buf
, "%u\n", stats
->total_trans
);
780 spin_unlock(&stats
->lock
);
786 time_in_state_ms_show(struct device
*dev
, struct device_attribute
*attr
,
789 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
790 struct cooling_dev_stats
*stats
= cdev
->stats
;
794 spin_lock(&stats
->lock
);
795 update_time_in_state(stats
);
797 for (i
= 0; i
< stats
->max_states
; i
++) {
798 len
+= sprintf(buf
+ len
, "state%u\t%llu\n", i
,
799 ktime_to_ms(stats
->time_in_state
[i
]));
801 spin_unlock(&stats
->lock
);
807 reset_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
,
810 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
811 struct cooling_dev_stats
*stats
= cdev
->stats
;
812 int i
, states
= stats
->max_states
;
814 spin_lock(&stats
->lock
);
816 stats
->total_trans
= 0;
817 stats
->last_time
= ktime_get();
818 memset(stats
->trans_table
, 0,
819 states
* states
* sizeof(*stats
->trans_table
));
821 for (i
= 0; i
< stats
->max_states
; i
++)
822 stats
->time_in_state
[i
] = ktime_set(0, 0);
824 spin_unlock(&stats
->lock
);
829 static ssize_t
trans_table_show(struct device
*dev
,
830 struct device_attribute
*attr
, char *buf
)
832 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
833 struct cooling_dev_stats
*stats
= cdev
->stats
;
837 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
, " From : To\n");
838 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
, " : ");
839 for (i
= 0; i
< stats
->max_states
; i
++) {
840 if (len
>= PAGE_SIZE
)
842 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
, "state%2u ", i
);
844 if (len
>= PAGE_SIZE
)
847 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
, "\n");
849 for (i
= 0; i
< stats
->max_states
; i
++) {
850 if (len
>= PAGE_SIZE
)
853 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
, "state%2u:", i
);
855 for (j
= 0; j
< stats
->max_states
; j
++) {
856 if (len
>= PAGE_SIZE
)
858 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
, "%8u ",
859 stats
->trans_table
[i
* stats
->max_states
+ j
]);
861 if (len
>= PAGE_SIZE
)
863 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
, "\n");
866 if (len
>= PAGE_SIZE
) {
867 pr_warn_once("Thermal transition table exceeds PAGE_SIZE. Disabling\n");
873 static DEVICE_ATTR_RO(total_trans
);
874 static DEVICE_ATTR_RO(time_in_state_ms
);
875 static DEVICE_ATTR_WO(reset
);
876 static DEVICE_ATTR_RO(trans_table
);
878 static struct attribute
*cooling_device_stats_attrs
[] = {
879 &dev_attr_total_trans
.attr
,
880 &dev_attr_time_in_state_ms
.attr
,
881 &dev_attr_reset
.attr
,
882 &dev_attr_trans_table
.attr
,
886 static const struct attribute_group cooling_device_stats_attr_group
= {
887 .attrs
= cooling_device_stats_attrs
,
891 static void cooling_device_stats_setup(struct thermal_cooling_device
*cdev
)
893 struct cooling_dev_stats
*stats
;
894 unsigned long states
;
897 if (cdev
->ops
->get_max_state(cdev
, &states
))
900 states
++; /* Total number of states is highest state + 1 */
902 var
= sizeof(*stats
);
903 var
+= sizeof(*stats
->time_in_state
) * states
;
904 var
+= sizeof(*stats
->trans_table
) * states
* states
;
906 stats
= kzalloc(var
, GFP_KERNEL
);
910 stats
->time_in_state
= (ktime_t
*)(stats
+ 1);
911 stats
->trans_table
= (unsigned int *)(stats
->time_in_state
+ states
);
913 stats
->last_time
= ktime_get();
914 stats
->max_states
= states
;
916 spin_lock_init(&stats
->lock
);
918 /* Fill the empty slot left in cooling_device_attr_groups */
919 var
= ARRAY_SIZE(cooling_device_attr_groups
) - 2;
920 cooling_device_attr_groups
[var
] = &cooling_device_stats_attr_group
;
923 static void cooling_device_stats_destroy(struct thermal_cooling_device
*cdev
)
932 cooling_device_stats_setup(struct thermal_cooling_device
*cdev
) {}
934 cooling_device_stats_destroy(struct thermal_cooling_device
*cdev
) {}
936 #endif /* CONFIG_THERMAL_STATISTICS */
938 void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device
*cdev
)
940 cooling_device_stats_setup(cdev
);
941 cdev
->device
.groups
= cooling_device_attr_groups
;
944 void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device
*cdev
)
946 cooling_device_stats_destroy(cdev
);
949 /* these helper will be used only at the time of bindig */
951 trip_point_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
953 struct thermal_instance
*instance
;
956 container_of(attr
, struct thermal_instance
, attr
);
958 if (instance
->trip
== THERMAL_TRIPS_NONE
)
959 return sprintf(buf
, "-1\n");
961 return sprintf(buf
, "%d\n", instance
->trip
);
965 weight_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
967 struct thermal_instance
*instance
;
969 instance
= container_of(attr
, struct thermal_instance
, weight_attr
);
971 return sprintf(buf
, "%d\n", instance
->weight
);
974 ssize_t
weight_store(struct device
*dev
, struct device_attribute
*attr
,
975 const char *buf
, size_t count
)
977 struct thermal_instance
*instance
;
980 ret
= kstrtoint(buf
, 0, &weight
);
984 instance
= container_of(attr
, struct thermal_instance
, weight_attr
);
985 instance
->weight
= weight
;