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 policy_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);
224 char name[THERMAL_NAME_LENGTH];
227 snprintf(name, sizeof(name), "%s", buf);
229 ret = thermal_zone_device_set_policy(tz, name);
237 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
239 struct thermal_zone_device *tz = to_thermal_zone(dev);
241 return sprintf(buf, "%s\n", tz->governor->name);
245 available_policies_show(struct device *dev, struct device_attribute *devattr,
248 return thermal_build_list_of_policies(buf);
251 #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
253 emul_temp_store(struct device *dev, struct device_attribute *attr,
254 const char *buf, size_t count)
256 struct thermal_zone_device *tz = to_thermal_zone(dev);
260 if (kstrtoint(buf, 10, &temperature))
263 if (!tz->ops->set_emul_temp) {
264 mutex_lock(&tz->lock);
265 tz->emul_temperature = temperature;
266 mutex_unlock(&tz->lock);
268 ret = tz->ops->set_emul_temp(tz, temperature);
272 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
274 return ret ? ret : count;
276 static DEVICE_ATTR_WO(emul_temp);
280 sustainable_power_show(struct device *dev, struct device_attribute *devattr,
283 struct thermal_zone_device *tz = to_thermal_zone(dev);
286 return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
292 sustainable_power_store(struct device *dev, struct device_attribute *devattr,
293 const char *buf, size_t count)
295 struct thermal_zone_device *tz = to_thermal_zone(dev);
296 u32 sustainable_power;
301 if (kstrtou32(buf, 10, &sustainable_power))
304 tz->tzp->sustainable_power = sustainable_power;
309 #define create_s32_tzp_attr(name) \
311 name##_show(struct device *dev, struct device_attribute *devattr, \
314 struct thermal_zone_device *tz = to_thermal_zone(dev); \
317 return sprintf(buf, "%d\n", tz->tzp->name); \
323 name##_store(struct device *dev, struct device_attribute *devattr, \
324 const char *buf, size_t count) \
326 struct thermal_zone_device *tz = to_thermal_zone(dev); \
332 if (kstrtos32(buf, 10, &value)) \
335 tz->tzp->name = value; \
339 static DEVICE_ATTR_RW(name)
341 create_s32_tzp_attr(k_po);
342 create_s32_tzp_attr(k_pu);
343 create_s32_tzp_attr(k_i);
344 create_s32_tzp_attr(k_d);
345 create_s32_tzp_attr(integral_cutoff);
346 create_s32_tzp_attr(slope);
347 create_s32_tzp_attr(offset);
348 #undef create_s32_tzp_attr
351 * These are thermal zone device attributes that will always be present.
352 * All the attributes created for tzp (create_s32_tzp_attr) also are always
353 * present on the sysfs interface.
355 static DEVICE_ATTR_RO(type);
356 static DEVICE_ATTR_RO(temp);
357 static DEVICE_ATTR_RW(policy);
358 static DEVICE_ATTR_RO(available_policies);
359 static DEVICE_ATTR_RW(sustainable_power);
361 /* These thermal zone device attributes are created based on conditions */
362 static DEVICE_ATTR_RW(mode);
364 /* These attributes are unconditionally added to a thermal zone */
365 static struct attribute *thermal_zone_dev_attrs[] = {
368 #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
369 &dev_attr_emul_temp.attr,
371 &dev_attr_policy.attr,
372 &dev_attr_available_policies.attr,
373 &dev_attr_sustainable_power.attr,
378 &dev_attr_integral_cutoff.attr,
379 &dev_attr_slope.attr,
380 &dev_attr_offset.attr,
384 static const struct attribute_group thermal_zone_attribute_group = {
385 .attrs = thermal_zone_dev_attrs,
388 static struct attribute *thermal_zone_mode_attrs[] = {
393 static const struct attribute_group thermal_zone_mode_attribute_group = {
394 .attrs = thermal_zone_mode_attrs,
397 static const struct attribute_group *thermal_zone_attribute_groups[] = {
398 &thermal_zone_attribute_group,
399 &thermal_zone_mode_attribute_group,
400 /* This is not NULL terminated as we create the group dynamically */
404 * create_trip_attrs() - create attributes for trip points
405 * @tz: the thermal zone device
406 * @mask: Writeable trip point bitmap.
408 * helper function to instantiate sysfs entries for every trip
409 * point and its properties of a struct thermal_zone_device.
411 * Return: 0 on success, the proper error value otherwise.
413 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
415 struct attribute **attrs;
418 /* This function works only for zones with at least one trip */
422 tz->trip_type_attrs = kcalloc(tz->trips, sizeof(*tz->trip_type_attrs),
424 if (!tz->trip_type_attrs)
427 tz->trip_temp_attrs = kcalloc(tz->trips, sizeof(*tz->trip_temp_attrs),
429 if (!tz->trip_temp_attrs) {
430 kfree(tz->trip_type_attrs);
434 if (tz->ops->get_trip_hyst) {
435 tz->trip_hyst_attrs = kcalloc(tz->trips,
436 sizeof(*tz->trip_hyst_attrs),
438 if (!tz->trip_hyst_attrs) {
439 kfree(tz->trip_type_attrs);
440 kfree(tz->trip_temp_attrs);
445 attrs = kcalloc(tz->trips * 3 + 1, sizeof(*attrs), GFP_KERNEL);
447 kfree(tz->trip_type_attrs);
448 kfree(tz->trip_temp_attrs);
449 if (tz->ops->get_trip_hyst)
450 kfree(tz->trip_hyst_attrs);
454 for (indx = 0; indx < tz->trips; indx++) {
455 /* create trip type attribute */
456 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
457 "trip_point_%d_type", indx);
459 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
460 tz->trip_type_attrs[indx].attr.attr.name =
461 tz->trip_type_attrs[indx].name;
462 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
463 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
464 attrs[indx] = &tz->trip_type_attrs[indx].attr.attr;
466 /* create trip temp attribute */
467 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
468 "trip_point_%d_temp", indx);
470 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
471 tz->trip_temp_attrs[indx].attr.attr.name =
472 tz->trip_temp_attrs[indx].name;
473 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
474 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
475 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
476 mask & (1 << indx)) {
477 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
478 tz->trip_temp_attrs[indx].attr.store =
479 trip_point_temp_store;
481 attrs[indx + tz->trips] = &tz->trip_temp_attrs[indx].attr.attr;
483 /* create Optional trip hyst attribute */
484 if (!tz->ops->get_trip_hyst)
486 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
487 "trip_point_%d_hyst", indx);
489 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
490 tz->trip_hyst_attrs[indx].attr.attr.name =
491 tz->trip_hyst_attrs[indx].name;
492 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
493 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
494 if (tz->ops->set_trip_hyst) {
495 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
496 tz->trip_hyst_attrs[indx].attr.store =
497 trip_point_hyst_store;
499 attrs[indx + tz->trips * 2] =
500 &tz->trip_hyst_attrs[indx].attr.attr;
502 attrs[tz->trips * 3] = NULL;
504 tz->trips_attribute_group.attrs = attrs;
510 * destroy_trip_attrs() - destroy attributes for trip points
511 * @tz: the thermal zone device
513 * helper function to free resources allocated by create_trip_attrs()
515 static void destroy_trip_attrs(struct thermal_zone_device *tz)
520 kfree(tz->trip_type_attrs);
521 kfree(tz->trip_temp_attrs);
522 if (tz->ops->get_trip_hyst)
523 kfree(tz->trip_hyst_attrs);
524 kfree(tz->trips_attribute_group.attrs);
527 int thermal_zone_create_device_groups(struct thermal_zone_device *tz,
530 const struct attribute_group **groups;
533 /* we need one extra for trips and the NULL to terminate the array */
534 size = ARRAY_SIZE(thermal_zone_attribute_groups) + 2;
535 /* This also takes care of API requirement to be NULL terminated */
536 groups = kcalloc(size, sizeof(*groups), GFP_KERNEL);
540 for (i = 0; i < size - 2; i++)
541 groups[i] = thermal_zone_attribute_groups[i];
544 result = create_trip_attrs(tz, mask);
551 groups[size - 2] = &tz->trips_attribute_group;
554 tz->device.groups = groups;
559 void thermal_zone_destroy_device_groups(struct thermal_zone_device *tz)
565 destroy_trip_attrs(tz);
567 kfree(tz->device.groups);
570 /* sys I/F for cooling device */
572 cdev_type_show(struct device *dev, struct device_attribute *attr, char *buf)
574 struct thermal_cooling_device *cdev = to_cooling_device(dev);
576 return sprintf(buf, "%s\n", cdev->type);
579 static ssize_t max_state_show(struct device *dev, struct device_attribute *attr,
582 struct thermal_cooling_device *cdev = to_cooling_device(dev);
586 ret = cdev->ops->get_max_state(cdev, &state);
589 return sprintf(buf, "%ld\n", state);
592 static ssize_t cur_state_show(struct device *dev, struct device_attribute *attr,
595 struct thermal_cooling_device *cdev = to_cooling_device(dev);
599 ret = cdev->ops->get_cur_state(cdev, &state);
602 return sprintf(buf, "%ld\n", state);
606 cur_state_store(struct device *dev, struct device_attribute *attr,
607 const char *buf, size_t count)
609 struct thermal_cooling_device *cdev = to_cooling_device(dev);
613 if (sscanf(buf, "%ld\n", &state) != 1)
619 mutex_lock(&cdev->lock);
621 result = cdev->ops->set_cur_state(cdev, state);
623 thermal_cooling_device_stats_update(cdev, state);
625 mutex_unlock(&cdev->lock);
626 return result ? result : count;
629 static struct device_attribute
630 dev_attr_cdev_type = __ATTR(type, 0444, cdev_type_show, NULL);
631 static DEVICE_ATTR_RO(max_state);
632 static DEVICE_ATTR_RW(cur_state);
634 static struct attribute *cooling_device_attrs[] = {
635 &dev_attr_cdev_type.attr,
636 &dev_attr_max_state.attr,
637 &dev_attr_cur_state.attr,
641 static const struct attribute_group cooling_device_attr_group = {
642 .attrs = cooling_device_attrs,
645 static const struct attribute_group *cooling_device_attr_groups[] = {
646 &cooling_device_attr_group,
647 NULL, /* Space allocated for cooling_device_stats_attr_group */
651 #ifdef CONFIG_THERMAL_STATISTICS
652 struct cooling_dev_stats {
654 unsigned int total_trans;
656 unsigned long max_states;
658 ktime_t *time_in_state;
659 unsigned int *trans_table;
662 static void update_time_in_state(struct cooling_dev_stats *stats)
664 ktime_t now = ktime_get(), delta;
666 delta = ktime_sub(now, stats->last_time);
667 stats->time_in_state[stats->state] =
668 ktime_add(stats->time_in_state[stats->state], delta);
669 stats->last_time = now;
672 void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
673 unsigned long new_state)
675 struct cooling_dev_stats *stats = cdev->stats;
680 spin_lock(&stats->lock);
682 if (stats->state == new_state)
685 update_time_in_state(stats);
686 stats->trans_table[stats->state * stats->max_states + new_state]++;
687 stats->state = new_state;
688 stats->total_trans++;
691 spin_unlock(&stats->lock);
694 static ssize_t total_trans_show(struct device *dev,
695 struct device_attribute *attr, char *buf)
697 struct thermal_cooling_device *cdev = to_cooling_device(dev);
698 struct cooling_dev_stats *stats = cdev->stats;
701 spin_lock(&stats->lock);
702 ret = sprintf(buf, "%u\n", stats->total_trans);
703 spin_unlock(&stats->lock);
709 time_in_state_ms_show(struct device *dev, struct device_attribute *attr,
712 struct thermal_cooling_device *cdev = to_cooling_device(dev);
713 struct cooling_dev_stats *stats = cdev->stats;
717 spin_lock(&stats->lock);
718 update_time_in_state(stats);
720 for (i = 0; i < stats->max_states; i++) {
721 len += sprintf(buf + len, "state%u\t%llu\n", i,
722 ktime_to_ms(stats->time_in_state[i]));
724 spin_unlock(&stats->lock);
730 reset_store(struct device *dev, struct device_attribute *attr, const char *buf,
733 struct thermal_cooling_device *cdev = to_cooling_device(dev);
734 struct cooling_dev_stats *stats = cdev->stats;
735 int i, states = stats->max_states;
737 spin_lock(&stats->lock);
739 stats->total_trans = 0;
740 stats->last_time = ktime_get();
741 memset(stats->trans_table, 0,
742 states * states * sizeof(*stats->trans_table));
744 for (i = 0; i < stats->max_states; i++)
745 stats->time_in_state[i] = ktime_set(0, 0);
747 spin_unlock(&stats->lock);
752 static ssize_t trans_table_show(struct device *dev,
753 struct device_attribute *attr, char *buf)
755 struct thermal_cooling_device *cdev = to_cooling_device(dev);
756 struct cooling_dev_stats *stats = cdev->stats;
760 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
761 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
762 for (i = 0; i < stats->max_states; i++) {
763 if (len >= PAGE_SIZE)
765 len += snprintf(buf + len, PAGE_SIZE - len, "state%2u ", i);
767 if (len >= PAGE_SIZE)
770 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
772 for (i = 0; i < stats->max_states; i++) {
773 if (len >= PAGE_SIZE)
776 len += snprintf(buf + len, PAGE_SIZE - len, "state%2u:", i);
778 for (j = 0; j < stats->max_states; j++) {
779 if (len >= PAGE_SIZE)
781 len += snprintf(buf + len, PAGE_SIZE - len, "%8u ",
782 stats->trans_table[i * stats->max_states + j]);
784 if (len >= PAGE_SIZE)
786 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
789 if (len >= PAGE_SIZE) {
790 pr_warn_once("Thermal transition table exceeds PAGE_SIZE. Disabling\n");
796 static DEVICE_ATTR_RO(total_trans);
797 static DEVICE_ATTR_RO(time_in_state_ms);
798 static DEVICE_ATTR_WO(reset);
799 static DEVICE_ATTR_RO(trans_table);
801 static struct attribute *cooling_device_stats_attrs[] = {
802 &dev_attr_total_trans.attr,
803 &dev_attr_time_in_state_ms.attr,
804 &dev_attr_reset.attr,
805 &dev_attr_trans_table.attr,
809 static const struct attribute_group cooling_device_stats_attr_group = {
810 .attrs = cooling_device_stats_attrs,
814 static void cooling_device_stats_setup(struct thermal_cooling_device *cdev)
816 struct cooling_dev_stats *stats;
817 unsigned long states;
820 if (cdev->ops->get_max_state(cdev, &states))
823 states++; /* Total number of states is highest state + 1 */
825 var = sizeof(*stats);
826 var += sizeof(*stats->time_in_state) * states;
827 var += sizeof(*stats->trans_table) * states * states;
829 stats = kzalloc(var, GFP_KERNEL);
833 stats->time_in_state = (ktime_t *)(stats + 1);
834 stats->trans_table = (unsigned int *)(stats->time_in_state + states);
836 stats->last_time = ktime_get();
837 stats->max_states = states;
839 spin_lock_init(&stats->lock);
841 /* Fill the empty slot left in cooling_device_attr_groups */
842 var = ARRAY_SIZE(cooling_device_attr_groups) - 2;
843 cooling_device_attr_groups[var] = &cooling_device_stats_attr_group;
846 static void cooling_device_stats_destroy(struct thermal_cooling_device *cdev)
855 cooling_device_stats_setup(struct thermal_cooling_device *cdev) {}
857 cooling_device_stats_destroy(struct thermal_cooling_device *cdev) {}
859 #endif /* CONFIG_THERMAL_STATISTICS */
861 void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *cdev)
863 cooling_device_stats_setup(cdev);
864 cdev->device.groups = cooling_device_attr_groups;
867 void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev)
869 cooling_device_stats_destroy(cdev);
872 /* these helper will be used only at the time of bindig */
874 trip_point_show(struct device *dev, struct device_attribute *attr, char *buf)
876 struct thermal_instance *instance;
879 container_of(attr, struct thermal_instance, attr);
881 return sprintf(buf, "%d\n", instance->trip);
885 weight_show(struct device *dev, struct device_attribute *attr, char *buf)
887 struct thermal_instance *instance;
889 instance = container_of(attr, struct thermal_instance, weight_attr);
891 return sprintf(buf, "%d\n", instance->weight);
894 ssize_t weight_store(struct device *dev, struct device_attribute *attr,
895 const char *buf, size_t count)
897 struct thermal_instance *instance;
900 ret = kstrtoint(buf, 0, &weight);
904 instance = container_of(attr, struct thermal_instance, weight_attr);
905 instance->weight = weight;