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 enum thermal_trip_type type;
89 if (!tz->ops->get_trip_type)
92 if (sscanf(attr->attr.name, "trip_point_%d_type", &trip) != 1)
95 result = tz->ops->get_trip_type(tz, trip, &type);
100 case THERMAL_TRIP_CRITICAL:
101 return sprintf(buf, "critical\n");
102 case THERMAL_TRIP_HOT:
103 return sprintf(buf, "hot\n");
104 case THERMAL_TRIP_PASSIVE:
105 return sprintf(buf, "passive\n");
106 case THERMAL_TRIP_ACTIVE:
107 return sprintf(buf, "active\n");
109 return sprintf(buf, "unknown\n");
114 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
115 const char *buf, size_t count)
117 struct thermal_zone_device *tz = to_thermal_zone(dev);
119 int temperature, hyst = 0;
120 enum thermal_trip_type type;
122 if (!tz->ops->set_trip_temp && !tz->trips)
125 if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1)
128 if (kstrtoint(buf, 10, &temperature))
131 ret = tz->ops->set_trip_temp(tz, trip, temperature);
136 tz->trips[trip].temperature = temperature;
138 if (tz->ops->get_trip_hyst) {
139 ret = tz->ops->get_trip_hyst(tz, trip, &hyst);
144 ret = tz->ops->get_trip_type(tz, trip, &type);
148 thermal_notify_tz_trip_change(tz->id, trip, type, temperature, hyst);
150 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
156 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
159 struct thermal_zone_device *tz = to_thermal_zone(dev);
163 if (!tz->ops->get_trip_temp)
166 if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1)
169 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
174 return sprintf(buf, "%d\n", 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);
185 if (!tz->ops->set_trip_hyst)
188 if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1)
191 if (kstrtoint(buf, 10, &temperature))
195 * We are not doing any check on the 'temperature' value
196 * here. The driver implementing 'set_trip_hyst' has to
199 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
202 thermal_zone_set_trips(tz);
204 return ret ? ret : count;
208 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
211 struct thermal_zone_device *tz = to_thermal_zone(dev);
215 if (!tz->ops->get_trip_hyst)
218 if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1)
221 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
223 return ret ? ret : sprintf(buf, "%d\n", temperature);
227 policy_store(struct device *dev, struct device_attribute *attr,
228 const char *buf, size_t count)
230 struct thermal_zone_device *tz = to_thermal_zone(dev);
231 char name[THERMAL_NAME_LENGTH];
234 snprintf(name, sizeof(name), "%s", buf);
236 ret = thermal_zone_device_set_policy(tz, name);
244 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
246 struct thermal_zone_device *tz = to_thermal_zone(dev);
248 return sprintf(buf, "%s\n", tz->governor->name);
252 available_policies_show(struct device *dev, struct device_attribute *devattr,
255 return thermal_build_list_of_policies(buf);
258 #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
260 emul_temp_store(struct device *dev, struct device_attribute *attr,
261 const char *buf, size_t count)
263 struct thermal_zone_device *tz = to_thermal_zone(dev);
267 if (kstrtoint(buf, 10, &temperature))
270 if (!tz->ops->set_emul_temp) {
271 mutex_lock(&tz->lock);
272 tz->emul_temperature = temperature;
273 mutex_unlock(&tz->lock);
275 ret = tz->ops->set_emul_temp(tz, temperature);
279 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
281 return ret ? ret : count;
283 static DEVICE_ATTR_WO(emul_temp);
287 sustainable_power_show(struct device *dev, struct device_attribute *devattr,
290 struct thermal_zone_device *tz = to_thermal_zone(dev);
293 return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
299 sustainable_power_store(struct device *dev, struct device_attribute *devattr,
300 const char *buf, size_t count)
302 struct thermal_zone_device *tz = to_thermal_zone(dev);
303 u32 sustainable_power;
308 if (kstrtou32(buf, 10, &sustainable_power))
311 tz->tzp->sustainable_power = sustainable_power;
316 #define create_s32_tzp_attr(name) \
318 name##_show(struct device *dev, struct device_attribute *devattr, \
321 struct thermal_zone_device *tz = to_thermal_zone(dev); \
324 return sprintf(buf, "%d\n", tz->tzp->name); \
330 name##_store(struct device *dev, struct device_attribute *devattr, \
331 const char *buf, size_t count) \
333 struct thermal_zone_device *tz = to_thermal_zone(dev); \
339 if (kstrtos32(buf, 10, &value)) \
342 tz->tzp->name = value; \
346 static DEVICE_ATTR_RW(name)
348 create_s32_tzp_attr(k_po);
349 create_s32_tzp_attr(k_pu);
350 create_s32_tzp_attr(k_i);
351 create_s32_tzp_attr(k_d);
352 create_s32_tzp_attr(integral_cutoff);
353 create_s32_tzp_attr(slope);
354 create_s32_tzp_attr(offset);
355 #undef create_s32_tzp_attr
358 * These are thermal zone device attributes that will always be present.
359 * All the attributes created for tzp (create_s32_tzp_attr) also are always
360 * present on the sysfs interface.
362 static DEVICE_ATTR_RO(type);
363 static DEVICE_ATTR_RO(temp);
364 static DEVICE_ATTR_RW(policy);
365 static DEVICE_ATTR_RO(available_policies);
366 static DEVICE_ATTR_RW(sustainable_power);
368 /* These thermal zone device attributes are created based on conditions */
369 static DEVICE_ATTR_RW(mode);
371 /* These attributes are unconditionally added to a thermal zone */
372 static struct attribute *thermal_zone_dev_attrs[] = {
375 #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
376 &dev_attr_emul_temp.attr,
378 &dev_attr_policy.attr,
379 &dev_attr_available_policies.attr,
380 &dev_attr_sustainable_power.attr,
385 &dev_attr_integral_cutoff.attr,
386 &dev_attr_slope.attr,
387 &dev_attr_offset.attr,
391 static const struct attribute_group thermal_zone_attribute_group = {
392 .attrs = thermal_zone_dev_attrs,
395 static struct attribute *thermal_zone_mode_attrs[] = {
400 static const struct attribute_group thermal_zone_mode_attribute_group = {
401 .attrs = thermal_zone_mode_attrs,
404 static const struct attribute_group *thermal_zone_attribute_groups[] = {
405 &thermal_zone_attribute_group,
406 &thermal_zone_mode_attribute_group,
407 /* This is not NULL terminated as we create the group dynamically */
411 * create_trip_attrs() - create attributes for trip points
412 * @tz: the thermal zone device
413 * @mask: Writeable trip point bitmap.
415 * helper function to instantiate sysfs entries for every trip
416 * point and its properties of a struct thermal_zone_device.
418 * Return: 0 on success, the proper error value otherwise.
420 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
422 struct attribute **attrs;
425 /* This function works only for zones with at least one trip */
426 if (tz->num_trips <= 0)
429 tz->trip_type_attrs = kcalloc(tz->num_trips, sizeof(*tz->trip_type_attrs),
431 if (!tz->trip_type_attrs)
434 tz->trip_temp_attrs = kcalloc(tz->num_trips, sizeof(*tz->trip_temp_attrs),
436 if (!tz->trip_temp_attrs) {
437 kfree(tz->trip_type_attrs);
441 if (tz->ops->get_trip_hyst) {
442 tz->trip_hyst_attrs = kcalloc(tz->num_trips,
443 sizeof(*tz->trip_hyst_attrs),
445 if (!tz->trip_hyst_attrs) {
446 kfree(tz->trip_type_attrs);
447 kfree(tz->trip_temp_attrs);
452 attrs = kcalloc(tz->num_trips * 3 + 1, sizeof(*attrs), GFP_KERNEL);
454 kfree(tz->trip_type_attrs);
455 kfree(tz->trip_temp_attrs);
456 if (tz->ops->get_trip_hyst)
457 kfree(tz->trip_hyst_attrs);
461 for (indx = 0; indx < tz->num_trips; indx++) {
462 /* create trip type attribute */
463 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
464 "trip_point_%d_type", indx);
466 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
467 tz->trip_type_attrs[indx].attr.attr.name =
468 tz->trip_type_attrs[indx].name;
469 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
470 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
471 attrs[indx] = &tz->trip_type_attrs[indx].attr.attr;
473 /* create trip temp attribute */
474 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
475 "trip_point_%d_temp", indx);
477 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
478 tz->trip_temp_attrs[indx].attr.attr.name =
479 tz->trip_temp_attrs[indx].name;
480 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
481 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
482 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
483 mask & (1 << indx)) {
484 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
485 tz->trip_temp_attrs[indx].attr.store =
486 trip_point_temp_store;
488 attrs[indx + tz->num_trips] = &tz->trip_temp_attrs[indx].attr.attr;
490 /* create Optional trip hyst attribute */
491 if (!tz->ops->get_trip_hyst)
493 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
494 "trip_point_%d_hyst", indx);
496 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
497 tz->trip_hyst_attrs[indx].attr.attr.name =
498 tz->trip_hyst_attrs[indx].name;
499 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
500 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
501 if (tz->ops->set_trip_hyst) {
502 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
503 tz->trip_hyst_attrs[indx].attr.store =
504 trip_point_hyst_store;
506 attrs[indx + tz->num_trips * 2] =
507 &tz->trip_hyst_attrs[indx].attr.attr;
509 attrs[tz->num_trips * 3] = NULL;
511 tz->trips_attribute_group.attrs = attrs;
517 * destroy_trip_attrs() - destroy attributes for trip points
518 * @tz: the thermal zone device
520 * helper function to free resources allocated by create_trip_attrs()
522 static void destroy_trip_attrs(struct thermal_zone_device *tz)
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);
531 kfree(tz->trips_attribute_group.attrs);
534 int thermal_zone_create_device_groups(struct thermal_zone_device *tz,
537 const struct attribute_group **groups;
540 /* we need one extra for trips and the NULL to terminate the array */
541 size = ARRAY_SIZE(thermal_zone_attribute_groups) + 2;
542 /* This also takes care of API requirement to be NULL terminated */
543 groups = kcalloc(size, sizeof(*groups), GFP_KERNEL);
547 for (i = 0; i < size - 2; i++)
548 groups[i] = thermal_zone_attribute_groups[i];
551 result = create_trip_attrs(tz, mask);
558 groups[size - 2] = &tz->trips_attribute_group;
561 tz->device.groups = groups;
566 void thermal_zone_destroy_device_groups(struct thermal_zone_device *tz)
572 destroy_trip_attrs(tz);
574 kfree(tz->device.groups);
577 /* sys I/F for cooling device */
579 cdev_type_show(struct device *dev, struct device_attribute *attr, char *buf)
581 struct thermal_cooling_device *cdev = to_cooling_device(dev);
583 return sprintf(buf, "%s\n", cdev->type);
586 static ssize_t max_state_show(struct device *dev, struct device_attribute *attr,
589 struct thermal_cooling_device *cdev = to_cooling_device(dev);
593 ret = cdev->ops->get_max_state(cdev, &state);
596 return sprintf(buf, "%ld\n", state);
599 static ssize_t cur_state_show(struct device *dev, struct device_attribute *attr,
602 struct thermal_cooling_device *cdev = to_cooling_device(dev);
606 ret = cdev->ops->get_cur_state(cdev, &state);
609 return sprintf(buf, "%ld\n", state);
613 cur_state_store(struct device *dev, struct device_attribute *attr,
614 const char *buf, size_t count)
616 struct thermal_cooling_device *cdev = to_cooling_device(dev);
620 if (sscanf(buf, "%ld\n", &state) != 1)
626 mutex_lock(&cdev->lock);
628 result = cdev->ops->set_cur_state(cdev, state);
630 thermal_cooling_device_stats_update(cdev, state);
632 mutex_unlock(&cdev->lock);
633 return result ? result : count;
636 static struct device_attribute
637 dev_attr_cdev_type = __ATTR(type, 0444, cdev_type_show, NULL);
638 static DEVICE_ATTR_RO(max_state);
639 static DEVICE_ATTR_RW(cur_state);
641 static struct attribute *cooling_device_attrs[] = {
642 &dev_attr_cdev_type.attr,
643 &dev_attr_max_state.attr,
644 &dev_attr_cur_state.attr,
648 static const struct attribute_group cooling_device_attr_group = {
649 .attrs = cooling_device_attrs,
652 static const struct attribute_group *cooling_device_attr_groups[] = {
653 &cooling_device_attr_group,
654 NULL, /* Space allocated for cooling_device_stats_attr_group */
658 #ifdef CONFIG_THERMAL_STATISTICS
659 struct cooling_dev_stats {
661 unsigned int total_trans;
663 unsigned long max_states;
665 ktime_t *time_in_state;
666 unsigned int *trans_table;
669 static void update_time_in_state(struct cooling_dev_stats *stats)
671 ktime_t now = ktime_get(), delta;
673 delta = ktime_sub(now, stats->last_time);
674 stats->time_in_state[stats->state] =
675 ktime_add(stats->time_in_state[stats->state], delta);
676 stats->last_time = now;
679 void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
680 unsigned long new_state)
682 struct cooling_dev_stats *stats = cdev->stats;
687 spin_lock(&stats->lock);
689 if (stats->state == new_state)
692 update_time_in_state(stats);
693 stats->trans_table[stats->state * stats->max_states + new_state]++;
694 stats->state = new_state;
695 stats->total_trans++;
698 spin_unlock(&stats->lock);
701 static ssize_t total_trans_show(struct device *dev,
702 struct device_attribute *attr, char *buf)
704 struct thermal_cooling_device *cdev = to_cooling_device(dev);
705 struct cooling_dev_stats *stats = cdev->stats;
708 spin_lock(&stats->lock);
709 ret = sprintf(buf, "%u\n", stats->total_trans);
710 spin_unlock(&stats->lock);
716 time_in_state_ms_show(struct device *dev, struct device_attribute *attr,
719 struct thermal_cooling_device *cdev = to_cooling_device(dev);
720 struct cooling_dev_stats *stats = cdev->stats;
724 spin_lock(&stats->lock);
725 update_time_in_state(stats);
727 for (i = 0; i < stats->max_states; i++) {
728 len += sprintf(buf + len, "state%u\t%llu\n", i,
729 ktime_to_ms(stats->time_in_state[i]));
731 spin_unlock(&stats->lock);
737 reset_store(struct device *dev, struct device_attribute *attr, const char *buf,
740 struct thermal_cooling_device *cdev = to_cooling_device(dev);
741 struct cooling_dev_stats *stats = cdev->stats;
742 int i, states = stats->max_states;
744 spin_lock(&stats->lock);
746 stats->total_trans = 0;
747 stats->last_time = ktime_get();
748 memset(stats->trans_table, 0,
749 states * states * sizeof(*stats->trans_table));
751 for (i = 0; i < stats->max_states; i++)
752 stats->time_in_state[i] = ktime_set(0, 0);
754 spin_unlock(&stats->lock);
759 static ssize_t trans_table_show(struct device *dev,
760 struct device_attribute *attr, char *buf)
762 struct thermal_cooling_device *cdev = to_cooling_device(dev);
763 struct cooling_dev_stats *stats = cdev->stats;
767 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
768 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
769 for (i = 0; i < stats->max_states; i++) {
770 if (len >= PAGE_SIZE)
772 len += snprintf(buf + len, PAGE_SIZE - len, "state%2u ", i);
774 if (len >= PAGE_SIZE)
777 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
779 for (i = 0; i < stats->max_states; i++) {
780 if (len >= PAGE_SIZE)
783 len += snprintf(buf + len, PAGE_SIZE - len, "state%2u:", i);
785 for (j = 0; j < stats->max_states; j++) {
786 if (len >= PAGE_SIZE)
788 len += snprintf(buf + len, PAGE_SIZE - len, "%8u ",
789 stats->trans_table[i * stats->max_states + j]);
791 if (len >= PAGE_SIZE)
793 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
796 if (len >= PAGE_SIZE) {
797 pr_warn_once("Thermal transition table exceeds PAGE_SIZE. Disabling\n");
803 static DEVICE_ATTR_RO(total_trans);
804 static DEVICE_ATTR_RO(time_in_state_ms);
805 static DEVICE_ATTR_WO(reset);
806 static DEVICE_ATTR_RO(trans_table);
808 static struct attribute *cooling_device_stats_attrs[] = {
809 &dev_attr_total_trans.attr,
810 &dev_attr_time_in_state_ms.attr,
811 &dev_attr_reset.attr,
812 &dev_attr_trans_table.attr,
816 static const struct attribute_group cooling_device_stats_attr_group = {
817 .attrs = cooling_device_stats_attrs,
821 static void cooling_device_stats_setup(struct thermal_cooling_device *cdev)
823 const struct attribute_group *stats_attr_group = NULL;
824 struct cooling_dev_stats *stats;
825 unsigned long states;
828 if (cdev->ops->get_max_state(cdev, &states))
831 states++; /* Total number of states is highest state + 1 */
833 var = sizeof(*stats);
834 var += sizeof(*stats->time_in_state) * states;
835 var += sizeof(*stats->trans_table) * states * states;
837 stats = kzalloc(var, GFP_KERNEL);
841 stats->time_in_state = (ktime_t *)(stats + 1);
842 stats->trans_table = (unsigned int *)(stats->time_in_state + states);
844 stats->last_time = ktime_get();
845 stats->max_states = states;
847 spin_lock_init(&stats->lock);
849 stats_attr_group = &cooling_device_stats_attr_group;
852 /* Fill the empty slot left in cooling_device_attr_groups */
853 var = ARRAY_SIZE(cooling_device_attr_groups) - 2;
854 cooling_device_attr_groups[var] = stats_attr_group;
857 static void cooling_device_stats_destroy(struct thermal_cooling_device *cdev)
866 cooling_device_stats_setup(struct thermal_cooling_device *cdev) {}
868 cooling_device_stats_destroy(struct thermal_cooling_device *cdev) {}
870 #endif /* CONFIG_THERMAL_STATISTICS */
872 void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *cdev)
874 cooling_device_stats_setup(cdev);
875 cdev->device.groups = cooling_device_attr_groups;
878 void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev)
880 cooling_device_stats_destroy(cdev);
883 /* these helper will be used only at the time of bindig */
885 trip_point_show(struct device *dev, struct device_attribute *attr, char *buf)
887 struct thermal_instance *instance;
890 container_of(attr, struct thermal_instance, attr);
892 return sprintf(buf, "%d\n", instance->trip);
896 weight_show(struct device *dev, struct device_attribute *attr, char *buf)
898 struct thermal_instance *instance;
900 instance = container_of(attr, struct thermal_instance, weight_attr);
902 return sprintf(buf, "%d\n", instance->weight);
905 ssize_t weight_store(struct device *dev, struct device_attribute *attr,
906 const char *buf, size_t count)
908 struct thermal_instance *instance;
911 ret = kstrtoint(buf, 0, &weight);
915 instance = container_of(attr, struct thermal_instance, weight_attr);
916 instance->weight = weight;