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);
86 struct thermal_trip trip;
89 if (sscanf(attr->attr.name, "trip_point_%d_type", &trip_id) != 1)
92 mutex_lock(&tz->lock);
94 if (device_is_registered(dev))
95 result = __thermal_zone_get_trip(tz, trip_id, &trip);
99 mutex_unlock(&tz->lock);
105 case THERMAL_TRIP_CRITICAL:
106 return sprintf(buf, "critical\n");
107 case THERMAL_TRIP_HOT:
108 return sprintf(buf, "hot\n");
109 case THERMAL_TRIP_PASSIVE:
110 return sprintf(buf, "passive\n");
111 case THERMAL_TRIP_ACTIVE:
112 return sprintf(buf, "active\n");
114 return sprintf(buf, "unknown\n");
119 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
120 const char *buf, size_t count)
122 struct thermal_zone_device *tz = to_thermal_zone(dev);
123 struct thermal_trip trip;
126 if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip_id) != 1)
129 mutex_lock(&tz->lock);
131 if (!device_is_registered(dev)) {
136 ret = __thermal_zone_get_trip(tz, trip_id, &trip);
140 ret = kstrtoint(buf, 10, &trip.temperature);
144 ret = thermal_zone_set_trip(tz, trip_id, &trip);
146 mutex_unlock(&tz->lock);
148 return ret ? ret : count;
152 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
155 struct thermal_zone_device *tz = to_thermal_zone(dev);
156 struct thermal_trip trip;
159 if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip_id) != 1)
162 mutex_lock(&tz->lock);
164 if (device_is_registered(dev))
165 ret = __thermal_zone_get_trip(tz, trip_id, &trip);
169 mutex_unlock(&tz->lock);
174 return sprintf(buf, "%d\n", trip.temperature);
178 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
179 const char *buf, size_t count)
181 struct thermal_zone_device *tz = to_thermal_zone(dev);
182 struct thermal_trip trip;
185 if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip_id) != 1)
188 if (kstrtoint(buf, 10, &trip.hysteresis))
191 mutex_lock(&tz->lock);
193 if (!device_is_registered(dev)) {
198 ret = __thermal_zone_get_trip(tz, trip_id, &trip);
202 ret = thermal_zone_set_trip(tz, trip_id, &trip);
204 mutex_unlock(&tz->lock);
206 return ret ? ret : count;
210 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
213 struct thermal_zone_device *tz = to_thermal_zone(dev);
214 struct thermal_trip trip;
217 if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip_id) != 1)
220 mutex_lock(&tz->lock);
222 if (device_is_registered(dev))
223 ret = __thermal_zone_get_trip(tz, trip_id, &trip);
227 mutex_unlock(&tz->lock);
229 return ret ? ret : sprintf(buf, "%d\n", trip.hysteresis);
233 policy_store(struct device *dev, struct device_attribute *attr,
234 const char *buf, size_t count)
236 struct thermal_zone_device *tz = to_thermal_zone(dev);
237 char name[THERMAL_NAME_LENGTH];
240 snprintf(name, sizeof(name), "%s", buf);
242 ret = thermal_zone_device_set_policy(tz, name);
250 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
252 struct thermal_zone_device *tz = to_thermal_zone(dev);
254 return sprintf(buf, "%s\n", tz->governor->name);
258 available_policies_show(struct device *dev, struct device_attribute *devattr,
261 return thermal_build_list_of_policies(buf);
264 #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
266 emul_temp_store(struct device *dev, struct device_attribute *attr,
267 const char *buf, size_t count)
269 struct thermal_zone_device *tz = to_thermal_zone(dev);
273 if (kstrtoint(buf, 10, &temperature))
276 mutex_lock(&tz->lock);
278 if (!device_is_registered(dev)) {
283 if (!tz->ops->set_emul_temp)
284 tz->emul_temperature = temperature;
286 ret = tz->ops->set_emul_temp(tz, temperature);
289 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
292 mutex_unlock(&tz->lock);
294 return ret ? ret : count;
296 static DEVICE_ATTR_WO(emul_temp);
300 sustainable_power_show(struct device *dev, struct device_attribute *devattr,
303 struct thermal_zone_device *tz = to_thermal_zone(dev);
306 return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
312 sustainable_power_store(struct device *dev, struct device_attribute *devattr,
313 const char *buf, size_t count)
315 struct thermal_zone_device *tz = to_thermal_zone(dev);
316 u32 sustainable_power;
321 if (kstrtou32(buf, 10, &sustainable_power))
324 tz->tzp->sustainable_power = sustainable_power;
329 #define create_s32_tzp_attr(name) \
331 name##_show(struct device *dev, struct device_attribute *devattr, \
334 struct thermal_zone_device *tz = to_thermal_zone(dev); \
337 return sprintf(buf, "%d\n", tz->tzp->name); \
343 name##_store(struct device *dev, struct device_attribute *devattr, \
344 const char *buf, size_t count) \
346 struct thermal_zone_device *tz = to_thermal_zone(dev); \
352 if (kstrtos32(buf, 10, &value)) \
355 tz->tzp->name = value; \
359 static DEVICE_ATTR_RW(name)
361 create_s32_tzp_attr(k_po);
362 create_s32_tzp_attr(k_pu);
363 create_s32_tzp_attr(k_i);
364 create_s32_tzp_attr(k_d);
365 create_s32_tzp_attr(integral_cutoff);
366 create_s32_tzp_attr(slope);
367 create_s32_tzp_attr(offset);
368 #undef create_s32_tzp_attr
371 * These are thermal zone device attributes that will always be present.
372 * All the attributes created for tzp (create_s32_tzp_attr) also are always
373 * present on the sysfs interface.
375 static DEVICE_ATTR_RO(type);
376 static DEVICE_ATTR_RO(temp);
377 static DEVICE_ATTR_RW(policy);
378 static DEVICE_ATTR_RO(available_policies);
379 static DEVICE_ATTR_RW(sustainable_power);
381 /* These thermal zone device attributes are created based on conditions */
382 static DEVICE_ATTR_RW(mode);
384 /* These attributes are unconditionally added to a thermal zone */
385 static struct attribute *thermal_zone_dev_attrs[] = {
388 #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
389 &dev_attr_emul_temp.attr,
391 &dev_attr_policy.attr,
392 &dev_attr_available_policies.attr,
393 &dev_attr_sustainable_power.attr,
398 &dev_attr_integral_cutoff.attr,
399 &dev_attr_slope.attr,
400 &dev_attr_offset.attr,
404 static const struct attribute_group thermal_zone_attribute_group = {
405 .attrs = thermal_zone_dev_attrs,
408 static struct attribute *thermal_zone_mode_attrs[] = {
413 static const struct attribute_group thermal_zone_mode_attribute_group = {
414 .attrs = thermal_zone_mode_attrs,
417 static const struct attribute_group *thermal_zone_attribute_groups[] = {
418 &thermal_zone_attribute_group,
419 &thermal_zone_mode_attribute_group,
420 /* This is not NULL terminated as we create the group dynamically */
424 * create_trip_attrs() - create attributes for trip points
425 * @tz: the thermal zone device
426 * @mask: Writeable trip point bitmap.
428 * helper function to instantiate sysfs entries for every trip
429 * point and its properties of a struct thermal_zone_device.
431 * Return: 0 on success, the proper error value otherwise.
433 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
435 struct attribute **attrs;
438 /* This function works only for zones with at least one trip */
439 if (tz->num_trips <= 0)
442 tz->trip_type_attrs = kcalloc(tz->num_trips, sizeof(*tz->trip_type_attrs),
444 if (!tz->trip_type_attrs)
447 tz->trip_temp_attrs = kcalloc(tz->num_trips, sizeof(*tz->trip_temp_attrs),
449 if (!tz->trip_temp_attrs) {
450 kfree(tz->trip_type_attrs);
454 tz->trip_hyst_attrs = kcalloc(tz->num_trips,
455 sizeof(*tz->trip_hyst_attrs),
457 if (!tz->trip_hyst_attrs) {
458 kfree(tz->trip_type_attrs);
459 kfree(tz->trip_temp_attrs);
463 attrs = kcalloc(tz->num_trips * 3 + 1, sizeof(*attrs), GFP_KERNEL);
465 kfree(tz->trip_type_attrs);
466 kfree(tz->trip_temp_attrs);
467 kfree(tz->trip_hyst_attrs);
471 for (indx = 0; indx < tz->num_trips; indx++) {
472 /* create trip type attribute */
473 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
474 "trip_point_%d_type", indx);
476 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
477 tz->trip_type_attrs[indx].attr.attr.name =
478 tz->trip_type_attrs[indx].name;
479 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
480 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
481 attrs[indx] = &tz->trip_type_attrs[indx].attr.attr;
483 /* create trip temp attribute */
484 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
485 "trip_point_%d_temp", indx);
487 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
488 tz->trip_temp_attrs[indx].attr.attr.name =
489 tz->trip_temp_attrs[indx].name;
490 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
491 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
492 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
493 mask & (1 << indx)) {
494 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
495 tz->trip_temp_attrs[indx].attr.store =
496 trip_point_temp_store;
498 attrs[indx + tz->num_trips] = &tz->trip_temp_attrs[indx].attr.attr;
500 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
501 "trip_point_%d_hyst", indx);
503 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
504 tz->trip_hyst_attrs[indx].attr.attr.name =
505 tz->trip_hyst_attrs[indx].name;
506 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
507 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
508 if (tz->ops->set_trip_hyst) {
509 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
510 tz->trip_hyst_attrs[indx].attr.store =
511 trip_point_hyst_store;
513 attrs[indx + tz->num_trips * 2] =
514 &tz->trip_hyst_attrs[indx].attr.attr;
516 attrs[tz->num_trips * 3] = NULL;
518 tz->trips_attribute_group.attrs = attrs;
524 * destroy_trip_attrs() - destroy attributes for trip points
525 * @tz: the thermal zone device
527 * helper function to free resources allocated by create_trip_attrs()
529 static void destroy_trip_attrs(struct thermal_zone_device *tz)
534 kfree(tz->trip_type_attrs);
535 kfree(tz->trip_temp_attrs);
536 kfree(tz->trip_hyst_attrs);
537 kfree(tz->trips_attribute_group.attrs);
540 int thermal_zone_create_device_groups(struct thermal_zone_device *tz,
543 const struct attribute_group **groups;
546 /* we need one extra for trips and the NULL to terminate the array */
547 size = ARRAY_SIZE(thermal_zone_attribute_groups) + 2;
548 /* This also takes care of API requirement to be NULL terminated */
549 groups = kcalloc(size, sizeof(*groups), GFP_KERNEL);
553 for (i = 0; i < size - 2; i++)
554 groups[i] = thermal_zone_attribute_groups[i];
557 result = create_trip_attrs(tz, mask);
564 groups[size - 2] = &tz->trips_attribute_group;
567 tz->device.groups = groups;
572 void thermal_zone_destroy_device_groups(struct thermal_zone_device *tz)
578 destroy_trip_attrs(tz);
580 kfree(tz->device.groups);
583 /* sys I/F for cooling device */
585 cdev_type_show(struct device *dev, struct device_attribute *attr, char *buf)
587 struct thermal_cooling_device *cdev = to_cooling_device(dev);
589 return sprintf(buf, "%s\n", cdev->type);
592 static ssize_t max_state_show(struct device *dev, struct device_attribute *attr,
595 struct thermal_cooling_device *cdev = to_cooling_device(dev);
597 return sprintf(buf, "%ld\n", cdev->max_state);
600 static ssize_t cur_state_show(struct device *dev, struct device_attribute *attr,
603 struct thermal_cooling_device *cdev = to_cooling_device(dev);
607 ret = cdev->ops->get_cur_state(cdev, &state);
610 return sprintf(buf, "%ld\n", state);
614 cur_state_store(struct device *dev, struct device_attribute *attr,
615 const char *buf, size_t count)
617 struct thermal_cooling_device *cdev = to_cooling_device(dev);
621 if (sscanf(buf, "%ld\n", &state) != 1)
627 /* Requested state should be less than max_state + 1 */
628 if (state > cdev->max_state)
631 mutex_lock(&cdev->lock);
633 result = cdev->ops->set_cur_state(cdev, state);
635 thermal_cooling_device_stats_update(cdev, state);
637 mutex_unlock(&cdev->lock);
638 return result ? result : count;
641 static struct device_attribute
642 dev_attr_cdev_type = __ATTR(type, 0444, cdev_type_show, NULL);
643 static DEVICE_ATTR_RO(max_state);
644 static DEVICE_ATTR_RW(cur_state);
646 static struct attribute *cooling_device_attrs[] = {
647 &dev_attr_cdev_type.attr,
648 &dev_attr_max_state.attr,
649 &dev_attr_cur_state.attr,
653 static const struct attribute_group cooling_device_attr_group = {
654 .attrs = cooling_device_attrs,
657 static const struct attribute_group *cooling_device_attr_groups[] = {
658 &cooling_device_attr_group,
659 NULL, /* Space allocated for cooling_device_stats_attr_group */
663 #ifdef CONFIG_THERMAL_STATISTICS
664 struct cooling_dev_stats {
666 unsigned int total_trans;
669 ktime_t *time_in_state;
670 unsigned int *trans_table;
673 static void update_time_in_state(struct cooling_dev_stats *stats)
675 ktime_t now = ktime_get(), delta;
677 delta = ktime_sub(now, stats->last_time);
678 stats->time_in_state[stats->state] =
679 ktime_add(stats->time_in_state[stats->state], delta);
680 stats->last_time = now;
683 void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
684 unsigned long new_state)
686 struct cooling_dev_stats *stats = cdev->stats;
688 lockdep_assert_held(&cdev->lock);
693 spin_lock(&stats->lock);
695 if (stats->state == new_state)
698 update_time_in_state(stats);
699 stats->trans_table[stats->state * (cdev->max_state + 1) + new_state]++;
700 stats->state = new_state;
701 stats->total_trans++;
704 spin_unlock(&stats->lock);
707 static ssize_t total_trans_show(struct device *dev,
708 struct device_attribute *attr, char *buf)
710 struct thermal_cooling_device *cdev = to_cooling_device(dev);
711 struct cooling_dev_stats *stats;
714 mutex_lock(&cdev->lock);
720 spin_lock(&stats->lock);
721 ret = sprintf(buf, "%u\n", stats->total_trans);
722 spin_unlock(&stats->lock);
725 mutex_unlock(&cdev->lock);
731 time_in_state_ms_show(struct device *dev, struct device_attribute *attr,
734 struct thermal_cooling_device *cdev = to_cooling_device(dev);
735 struct cooling_dev_stats *stats;
739 mutex_lock(&cdev->lock);
745 spin_lock(&stats->lock);
747 update_time_in_state(stats);
749 for (i = 0; i <= cdev->max_state; i++) {
750 len += sprintf(buf + len, "state%u\t%llu\n", i,
751 ktime_to_ms(stats->time_in_state[i]));
753 spin_unlock(&stats->lock);
756 mutex_unlock(&cdev->lock);
762 reset_store(struct device *dev, struct device_attribute *attr, const char *buf,
765 struct thermal_cooling_device *cdev = to_cooling_device(dev);
766 struct cooling_dev_stats *stats;
769 mutex_lock(&cdev->lock);
775 states = cdev->max_state + 1;
777 spin_lock(&stats->lock);
779 stats->total_trans = 0;
780 stats->last_time = ktime_get();
781 memset(stats->trans_table, 0,
782 states * states * sizeof(*stats->trans_table));
784 for (i = 0; i < states; i++)
785 stats->time_in_state[i] = ktime_set(0, 0);
787 spin_unlock(&stats->lock);
790 mutex_unlock(&cdev->lock);
795 static ssize_t trans_table_show(struct device *dev,
796 struct device_attribute *attr, char *buf)
798 struct thermal_cooling_device *cdev = to_cooling_device(dev);
799 struct cooling_dev_stats *stats;
803 mutex_lock(&cdev->lock);
811 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
812 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
813 for (i = 0; i <= cdev->max_state; i++) {
814 if (len >= PAGE_SIZE)
816 len += snprintf(buf + len, PAGE_SIZE - len, "state%2u ", i);
818 if (len >= PAGE_SIZE) {
823 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
825 for (i = 0; i <= cdev->max_state; i++) {
826 if (len >= PAGE_SIZE)
829 len += snprintf(buf + len, PAGE_SIZE - len, "state%2u:", i);
831 for (j = 0; j <= cdev->max_state; j++) {
832 if (len >= PAGE_SIZE)
834 len += snprintf(buf + len, PAGE_SIZE - len, "%8u ",
835 stats->trans_table[i * (cdev->max_state + 1) + j]);
837 if (len >= PAGE_SIZE)
839 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
842 if (len >= PAGE_SIZE) {
843 pr_warn_once("Thermal transition table exceeds PAGE_SIZE. Disabling\n");
848 mutex_unlock(&cdev->lock);
853 static DEVICE_ATTR_RO(total_trans);
854 static DEVICE_ATTR_RO(time_in_state_ms);
855 static DEVICE_ATTR_WO(reset);
856 static DEVICE_ATTR_RO(trans_table);
858 static struct attribute *cooling_device_stats_attrs[] = {
859 &dev_attr_total_trans.attr,
860 &dev_attr_time_in_state_ms.attr,
861 &dev_attr_reset.attr,
862 &dev_attr_trans_table.attr,
866 static const struct attribute_group cooling_device_stats_attr_group = {
867 .attrs = cooling_device_stats_attrs,
871 static void cooling_device_stats_setup(struct thermal_cooling_device *cdev)
873 const struct attribute_group *stats_attr_group = NULL;
874 struct cooling_dev_stats *stats;
875 /* Total number of states is highest state + 1 */
876 unsigned long states = cdev->max_state + 1;
879 var = sizeof(*stats);
880 var += sizeof(*stats->time_in_state) * states;
881 var += sizeof(*stats->trans_table) * states * states;
883 stats = kzalloc(var, GFP_KERNEL);
887 stats->time_in_state = (ktime_t *)(stats + 1);
888 stats->trans_table = (unsigned int *)(stats->time_in_state + states);
890 stats->last_time = ktime_get();
892 spin_lock_init(&stats->lock);
894 stats_attr_group = &cooling_device_stats_attr_group;
897 /* Fill the empty slot left in cooling_device_attr_groups */
898 var = ARRAY_SIZE(cooling_device_attr_groups) - 2;
899 cooling_device_attr_groups[var] = stats_attr_group;
902 static void cooling_device_stats_destroy(struct thermal_cooling_device *cdev)
911 cooling_device_stats_setup(struct thermal_cooling_device *cdev) {}
913 cooling_device_stats_destroy(struct thermal_cooling_device *cdev) {}
915 #endif /* CONFIG_THERMAL_STATISTICS */
917 void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *cdev)
919 cooling_device_stats_setup(cdev);
920 cdev->device.groups = cooling_device_attr_groups;
923 void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev)
925 cooling_device_stats_destroy(cdev);
928 void thermal_cooling_device_stats_reinit(struct thermal_cooling_device *cdev)
930 lockdep_assert_held(&cdev->lock);
932 cooling_device_stats_destroy(cdev);
933 cooling_device_stats_setup(cdev);
936 /* these helper will be used only at the time of bindig */
938 trip_point_show(struct device *dev, struct device_attribute *attr, char *buf)
940 struct thermal_instance *instance;
943 container_of(attr, struct thermal_instance, attr);
945 return sprintf(buf, "%d\n", instance->trip);
949 weight_show(struct device *dev, struct device_attribute *attr, char *buf)
951 struct thermal_instance *instance;
953 instance = container_of(attr, struct thermal_instance, weight_attr);
955 return sprintf(buf, "%d\n", instance->weight);
958 ssize_t weight_store(struct device *dev, struct device_attribute *attr,
959 const char *buf, size_t count)
961 struct thermal_instance *instance;
964 ret = kstrtoint(buf, 0, &weight);
968 instance = container_of(attr, struct thermal_instance, weight_attr);
969 instance->weight = weight;