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);
54 mutex_lock(&tz->lock);
55 enabled = thermal_zone_device_is_enabled(tz);
56 mutex_unlock(&tz->lock);
58 return sprintf(buf, "%s\n", enabled ? "enabled" : "disabled");
62 mode_store(struct device *dev, struct device_attribute *attr,
63 const char *buf, size_t count)
65 struct thermal_zone_device *tz = to_thermal_zone(dev);
68 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
69 result = thermal_zone_device_enable(tz);
70 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
71 result = thermal_zone_device_disable(tz);
82 trip_point_type_show(struct device *dev, struct device_attribute *attr,
85 struct thermal_zone_device *tz = to_thermal_zone(dev);
88 if (sscanf(attr->attr.name, "trip_point_%d_type", &trip_id) != 1)
91 switch (tz->trips[trip_id].type) {
92 case THERMAL_TRIP_CRITICAL:
93 return sprintf(buf, "critical\n");
94 case THERMAL_TRIP_HOT:
95 return sprintf(buf, "hot\n");
96 case THERMAL_TRIP_PASSIVE:
97 return sprintf(buf, "passive\n");
98 case THERMAL_TRIP_ACTIVE:
99 return sprintf(buf, "active\n");
101 return sprintf(buf, "unknown\n");
106 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
107 const char *buf, size_t count)
109 struct thermal_zone_device *tz = to_thermal_zone(dev);
110 struct thermal_trip *trip;
114 ret = kstrtoint(buf, 10, &temp);
118 if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip_id) != 1)
121 mutex_lock(&tz->lock);
123 trip = &tz->trips[trip_id];
125 if (temp != trip->temperature) {
126 if (tz->ops.set_trip_temp) {
127 ret = tz->ops.set_trip_temp(tz, trip_id, temp);
132 thermal_zone_set_trip_temp(tz, trip, temp);
134 __thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED);
138 mutex_unlock(&tz->lock);
140 return ret ? ret : count;
144 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
147 struct thermal_zone_device *tz = to_thermal_zone(dev);
150 if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip_id) != 1)
153 return sprintf(buf, "%d\n", tz->trips[trip_id].temperature);
157 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
158 const char *buf, size_t count)
160 struct thermal_zone_device *tz = to_thermal_zone(dev);
161 struct thermal_trip *trip;
165 ret = kstrtoint(buf, 10, &hyst);
169 if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip_id) != 1)
172 mutex_lock(&tz->lock);
174 trip = &tz->trips[trip_id];
176 if (hyst != trip->hysteresis) {
177 trip->hysteresis = hyst;
179 thermal_zone_trip_updated(tz, trip);
182 mutex_unlock(&tz->lock);
188 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
191 struct thermal_zone_device *tz = to_thermal_zone(dev);
194 if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip_id) != 1)
197 return sprintf(buf, "%d\n", tz->trips[trip_id].hysteresis);
201 policy_store(struct device *dev, struct device_attribute *attr,
202 const char *buf, size_t count)
204 struct thermal_zone_device *tz = to_thermal_zone(dev);
205 char name[THERMAL_NAME_LENGTH];
208 snprintf(name, sizeof(name), "%s", buf);
210 ret = thermal_zone_device_set_policy(tz, name);
218 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
220 struct thermal_zone_device *tz = to_thermal_zone(dev);
222 return sprintf(buf, "%s\n", tz->governor->name);
226 available_policies_show(struct device *dev, struct device_attribute *devattr,
229 return thermal_build_list_of_policies(buf);
232 #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
234 emul_temp_store(struct device *dev, struct device_attribute *attr,
235 const char *buf, size_t count)
237 struct thermal_zone_device *tz = to_thermal_zone(dev);
241 if (kstrtoint(buf, 10, &temperature))
244 mutex_lock(&tz->lock);
246 if (!tz->ops.set_emul_temp)
247 tz->emul_temperature = temperature;
249 ret = tz->ops.set_emul_temp(tz, temperature);
252 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
254 mutex_unlock(&tz->lock);
256 return ret ? ret : count;
258 static DEVICE_ATTR_WO(emul_temp);
262 sustainable_power_show(struct device *dev, struct device_attribute *devattr,
265 struct thermal_zone_device *tz = to_thermal_zone(dev);
268 return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
274 sustainable_power_store(struct device *dev, struct device_attribute *devattr,
275 const char *buf, size_t count)
277 struct thermal_zone_device *tz = to_thermal_zone(dev);
278 u32 sustainable_power;
283 if (kstrtou32(buf, 10, &sustainable_power))
286 tz->tzp->sustainable_power = sustainable_power;
291 #define create_s32_tzp_attr(name) \
293 name##_show(struct device *dev, struct device_attribute *devattr, \
296 struct thermal_zone_device *tz = to_thermal_zone(dev); \
299 return sprintf(buf, "%d\n", tz->tzp->name); \
305 name##_store(struct device *dev, struct device_attribute *devattr, \
306 const char *buf, size_t count) \
308 struct thermal_zone_device *tz = to_thermal_zone(dev); \
314 if (kstrtos32(buf, 10, &value)) \
317 tz->tzp->name = value; \
321 static DEVICE_ATTR_RW(name)
323 create_s32_tzp_attr(k_po);
324 create_s32_tzp_attr(k_pu);
325 create_s32_tzp_attr(k_i);
326 create_s32_tzp_attr(k_d);
327 create_s32_tzp_attr(integral_cutoff);
328 create_s32_tzp_attr(slope);
329 create_s32_tzp_attr(offset);
330 #undef create_s32_tzp_attr
333 * These are thermal zone device attributes that will always be present.
334 * All the attributes created for tzp (create_s32_tzp_attr) also are always
335 * present on the sysfs interface.
337 static DEVICE_ATTR_RO(type);
338 static DEVICE_ATTR_RO(temp);
339 static DEVICE_ATTR_RW(policy);
340 static DEVICE_ATTR_RO(available_policies);
341 static DEVICE_ATTR_RW(sustainable_power);
343 /* These thermal zone device attributes are created based on conditions */
344 static DEVICE_ATTR_RW(mode);
346 /* These attributes are unconditionally added to a thermal zone */
347 static struct attribute *thermal_zone_dev_attrs[] = {
350 #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
351 &dev_attr_emul_temp.attr,
353 &dev_attr_policy.attr,
354 &dev_attr_available_policies.attr,
355 &dev_attr_sustainable_power.attr,
360 &dev_attr_integral_cutoff.attr,
361 &dev_attr_slope.attr,
362 &dev_attr_offset.attr,
366 static const struct attribute_group thermal_zone_attribute_group = {
367 .attrs = thermal_zone_dev_attrs,
370 static struct attribute *thermal_zone_mode_attrs[] = {
375 static const struct attribute_group thermal_zone_mode_attribute_group = {
376 .attrs = thermal_zone_mode_attrs,
379 static const struct attribute_group *thermal_zone_attribute_groups[] = {
380 &thermal_zone_attribute_group,
381 &thermal_zone_mode_attribute_group,
382 /* This is not NULL terminated as we create the group dynamically */
386 * create_trip_attrs() - create attributes for trip points
387 * @tz: the thermal zone device
389 * helper function to instantiate sysfs entries for every trip
390 * point and its properties of a struct thermal_zone_device.
392 * Return: 0 on success, the proper error value otherwise.
394 static int create_trip_attrs(struct thermal_zone_device *tz)
396 const struct thermal_trip *trip;
397 struct attribute **attrs;
399 /* This function works only for zones with at least one trip */
400 if (tz->num_trips <= 0)
403 tz->trip_type_attrs = kcalloc(tz->num_trips, sizeof(*tz->trip_type_attrs),
405 if (!tz->trip_type_attrs)
408 tz->trip_temp_attrs = kcalloc(tz->num_trips, sizeof(*tz->trip_temp_attrs),
410 if (!tz->trip_temp_attrs) {
411 kfree(tz->trip_type_attrs);
415 tz->trip_hyst_attrs = kcalloc(tz->num_trips,
416 sizeof(*tz->trip_hyst_attrs),
418 if (!tz->trip_hyst_attrs) {
419 kfree(tz->trip_type_attrs);
420 kfree(tz->trip_temp_attrs);
424 attrs = kcalloc(tz->num_trips * 3 + 1, sizeof(*attrs), GFP_KERNEL);
426 kfree(tz->trip_type_attrs);
427 kfree(tz->trip_temp_attrs);
428 kfree(tz->trip_hyst_attrs);
432 for_each_trip(tz, trip) {
433 int indx = thermal_zone_trip_id(tz, trip);
435 /* create trip type attribute */
436 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
437 "trip_point_%d_type", indx);
439 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
440 tz->trip_type_attrs[indx].attr.attr.name =
441 tz->trip_type_attrs[indx].name;
442 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
443 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
444 attrs[indx] = &tz->trip_type_attrs[indx].attr.attr;
446 /* create trip temp attribute */
447 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
448 "trip_point_%d_temp", indx);
450 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
451 tz->trip_temp_attrs[indx].attr.attr.name =
452 tz->trip_temp_attrs[indx].name;
453 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
454 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
455 if (trip->flags & THERMAL_TRIP_FLAG_RW_TEMP) {
456 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
457 tz->trip_temp_attrs[indx].attr.store =
458 trip_point_temp_store;
460 attrs[indx + tz->num_trips] = &tz->trip_temp_attrs[indx].attr.attr;
462 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
463 "trip_point_%d_hyst", indx);
465 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
466 tz->trip_hyst_attrs[indx].attr.attr.name =
467 tz->trip_hyst_attrs[indx].name;
468 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
469 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
470 if (trip->flags & THERMAL_TRIP_FLAG_RW_HYST) {
471 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
472 tz->trip_hyst_attrs[indx].attr.store =
473 trip_point_hyst_store;
475 attrs[indx + tz->num_trips * 2] =
476 &tz->trip_hyst_attrs[indx].attr.attr;
478 attrs[tz->num_trips * 3] = NULL;
480 tz->trips_attribute_group.attrs = attrs;
486 * destroy_trip_attrs() - destroy attributes for trip points
487 * @tz: the thermal zone device
489 * helper function to free resources allocated by create_trip_attrs()
491 static void destroy_trip_attrs(struct thermal_zone_device *tz)
496 kfree(tz->trip_type_attrs);
497 kfree(tz->trip_temp_attrs);
498 kfree(tz->trip_hyst_attrs);
499 kfree(tz->trips_attribute_group.attrs);
502 int thermal_zone_create_device_groups(struct thermal_zone_device *tz)
504 const struct attribute_group **groups;
507 /* we need one extra for trips and the NULL to terminate the array */
508 size = ARRAY_SIZE(thermal_zone_attribute_groups) + 2;
509 /* This also takes care of API requirement to be NULL terminated */
510 groups = kcalloc(size, sizeof(*groups), GFP_KERNEL);
514 for (i = 0; i < size - 2; i++)
515 groups[i] = thermal_zone_attribute_groups[i];
518 result = create_trip_attrs(tz);
525 groups[size - 2] = &tz->trips_attribute_group;
528 tz->device.groups = groups;
533 void thermal_zone_destroy_device_groups(struct thermal_zone_device *tz)
539 destroy_trip_attrs(tz);
541 kfree(tz->device.groups);
544 /* sys I/F for cooling device */
546 cdev_type_show(struct device *dev, struct device_attribute *attr, char *buf)
548 struct thermal_cooling_device *cdev = to_cooling_device(dev);
550 return sprintf(buf, "%s\n", cdev->type);
553 static ssize_t max_state_show(struct device *dev, struct device_attribute *attr,
556 struct thermal_cooling_device *cdev = to_cooling_device(dev);
558 return sprintf(buf, "%ld\n", cdev->max_state);
561 static ssize_t cur_state_show(struct device *dev, struct device_attribute *attr,
564 struct thermal_cooling_device *cdev = to_cooling_device(dev);
568 ret = cdev->ops->get_cur_state(cdev, &state);
571 return sprintf(buf, "%ld\n", state);
575 cur_state_store(struct device *dev, struct device_attribute *attr,
576 const char *buf, size_t count)
578 struct thermal_cooling_device *cdev = to_cooling_device(dev);
582 if (sscanf(buf, "%ld\n", &state) != 1)
588 /* Requested state should be less than max_state + 1 */
589 if (state > cdev->max_state)
592 mutex_lock(&cdev->lock);
594 result = cdev->ops->set_cur_state(cdev, state);
596 thermal_cooling_device_stats_update(cdev, state);
598 mutex_unlock(&cdev->lock);
599 return result ? result : count;
602 static struct device_attribute
603 dev_attr_cdev_type = __ATTR(type, 0444, cdev_type_show, NULL);
604 static DEVICE_ATTR_RO(max_state);
605 static DEVICE_ATTR_RW(cur_state);
607 static struct attribute *cooling_device_attrs[] = {
608 &dev_attr_cdev_type.attr,
609 &dev_attr_max_state.attr,
610 &dev_attr_cur_state.attr,
614 static const struct attribute_group cooling_device_attr_group = {
615 .attrs = cooling_device_attrs,
618 static const struct attribute_group *cooling_device_attr_groups[] = {
619 &cooling_device_attr_group,
620 NULL, /* Space allocated for cooling_device_stats_attr_group */
624 #ifdef CONFIG_THERMAL_STATISTICS
625 struct cooling_dev_stats {
627 unsigned int total_trans;
630 ktime_t *time_in_state;
631 unsigned int *trans_table;
634 static void update_time_in_state(struct cooling_dev_stats *stats)
636 ktime_t now = ktime_get(), delta;
638 delta = ktime_sub(now, stats->last_time);
639 stats->time_in_state[stats->state] =
640 ktime_add(stats->time_in_state[stats->state], delta);
641 stats->last_time = now;
644 void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
645 unsigned long new_state)
647 struct cooling_dev_stats *stats = cdev->stats;
649 lockdep_assert_held(&cdev->lock);
654 spin_lock(&stats->lock);
656 if (stats->state == new_state)
659 update_time_in_state(stats);
660 stats->trans_table[stats->state * (cdev->max_state + 1) + new_state]++;
661 stats->state = new_state;
662 stats->total_trans++;
665 spin_unlock(&stats->lock);
668 static ssize_t total_trans_show(struct device *dev,
669 struct device_attribute *attr, char *buf)
671 struct thermal_cooling_device *cdev = to_cooling_device(dev);
672 struct cooling_dev_stats *stats;
675 mutex_lock(&cdev->lock);
681 spin_lock(&stats->lock);
682 ret = sprintf(buf, "%u\n", stats->total_trans);
683 spin_unlock(&stats->lock);
686 mutex_unlock(&cdev->lock);
692 time_in_state_ms_show(struct device *dev, struct device_attribute *attr,
695 struct thermal_cooling_device *cdev = to_cooling_device(dev);
696 struct cooling_dev_stats *stats;
700 mutex_lock(&cdev->lock);
706 spin_lock(&stats->lock);
708 update_time_in_state(stats);
710 for (i = 0; i <= cdev->max_state; i++) {
711 len += sprintf(buf + len, "state%u\t%llu\n", i,
712 ktime_to_ms(stats->time_in_state[i]));
714 spin_unlock(&stats->lock);
717 mutex_unlock(&cdev->lock);
723 reset_store(struct device *dev, struct device_attribute *attr, const char *buf,
726 struct thermal_cooling_device *cdev = to_cooling_device(dev);
727 struct cooling_dev_stats *stats;
730 mutex_lock(&cdev->lock);
736 states = cdev->max_state + 1;
738 spin_lock(&stats->lock);
740 stats->total_trans = 0;
741 stats->last_time = ktime_get();
742 memset(stats->trans_table, 0,
743 states * states * sizeof(*stats->trans_table));
745 for (i = 0; i < states; i++)
746 stats->time_in_state[i] = ktime_set(0, 0);
748 spin_unlock(&stats->lock);
751 mutex_unlock(&cdev->lock);
756 static ssize_t trans_table_show(struct device *dev,
757 struct device_attribute *attr, char *buf)
759 struct thermal_cooling_device *cdev = to_cooling_device(dev);
760 struct cooling_dev_stats *stats;
764 mutex_lock(&cdev->lock);
772 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
773 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
774 for (i = 0; i <= cdev->max_state; i++) {
775 if (len >= PAGE_SIZE)
777 len += snprintf(buf + len, PAGE_SIZE - len, "state%2u ", i);
779 if (len >= PAGE_SIZE) {
784 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
786 for (i = 0; i <= cdev->max_state; i++) {
787 if (len >= PAGE_SIZE)
790 len += snprintf(buf + len, PAGE_SIZE - len, "state%2u:", i);
792 for (j = 0; j <= cdev->max_state; j++) {
793 if (len >= PAGE_SIZE)
795 len += snprintf(buf + len, PAGE_SIZE - len, "%8u ",
796 stats->trans_table[i * (cdev->max_state + 1) + j]);
798 if (len >= PAGE_SIZE)
800 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
803 if (len >= PAGE_SIZE) {
804 pr_warn_once("Thermal transition table exceeds PAGE_SIZE. Disabling\n");
809 mutex_unlock(&cdev->lock);
814 static DEVICE_ATTR_RO(total_trans);
815 static DEVICE_ATTR_RO(time_in_state_ms);
816 static DEVICE_ATTR_WO(reset);
817 static DEVICE_ATTR_RO(trans_table);
819 static struct attribute *cooling_device_stats_attrs[] = {
820 &dev_attr_total_trans.attr,
821 &dev_attr_time_in_state_ms.attr,
822 &dev_attr_reset.attr,
823 &dev_attr_trans_table.attr,
827 static const struct attribute_group cooling_device_stats_attr_group = {
828 .attrs = cooling_device_stats_attrs,
832 static void cooling_device_stats_setup(struct thermal_cooling_device *cdev)
834 const struct attribute_group *stats_attr_group = NULL;
835 struct cooling_dev_stats *stats;
836 /* Total number of states is highest state + 1 */
837 unsigned long states = cdev->max_state + 1;
840 var = sizeof(*stats);
841 var += sizeof(*stats->time_in_state) * states;
842 var += sizeof(*stats->trans_table) * states * states;
844 stats = kzalloc(var, GFP_KERNEL);
848 stats->time_in_state = (ktime_t *)(stats + 1);
849 stats->trans_table = (unsigned int *)(stats->time_in_state + states);
851 stats->last_time = ktime_get();
853 spin_lock_init(&stats->lock);
855 stats_attr_group = &cooling_device_stats_attr_group;
858 /* Fill the empty slot left in cooling_device_attr_groups */
859 var = ARRAY_SIZE(cooling_device_attr_groups) - 2;
860 cooling_device_attr_groups[var] = stats_attr_group;
863 static void cooling_device_stats_destroy(struct thermal_cooling_device *cdev)
872 cooling_device_stats_setup(struct thermal_cooling_device *cdev) {}
874 cooling_device_stats_destroy(struct thermal_cooling_device *cdev) {}
876 #endif /* CONFIG_THERMAL_STATISTICS */
878 void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *cdev)
880 cooling_device_stats_setup(cdev);
881 cdev->device.groups = cooling_device_attr_groups;
884 void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev)
886 cooling_device_stats_destroy(cdev);
889 void thermal_cooling_device_stats_reinit(struct thermal_cooling_device *cdev)
891 lockdep_assert_held(&cdev->lock);
893 cooling_device_stats_destroy(cdev);
894 cooling_device_stats_setup(cdev);
897 /* these helper will be used only at the time of bindig */
899 trip_point_show(struct device *dev, struct device_attribute *attr, char *buf)
901 struct thermal_instance *instance;
904 container_of(attr, struct thermal_instance, attr);
906 return sprintf(buf, "%d\n",
907 thermal_zone_trip_id(instance->tz, instance->trip));
911 weight_show(struct device *dev, struct device_attribute *attr, char *buf)
913 struct thermal_instance *instance;
915 instance = container_of(attr, struct thermal_instance, weight_attr);
917 return sprintf(buf, "%d\n", instance->weight);
920 ssize_t weight_store(struct device *dev, struct device_attribute *attr,
921 const char *buf, size_t count)
923 struct thermal_instance *instance;
926 ret = kstrtoint(buf, 0, &weight);
930 instance = container_of(attr, struct thermal_instance, weight_attr);
932 /* Don't race with governors using the 'weight' value */
933 mutex_lock(&instance->tz->lock);
935 instance->weight = weight;
937 thermal_governor_update_tz(instance->tz,
938 THERMAL_INSTANCE_WEIGHT_CHANGED);
940 mutex_unlock(&instance->tz->lock);