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 if (tz->ops->set_trip_temp) {
132 ret = tz->ops->set_trip_temp(tz, trip, temperature);
138 tz->trips[trip].temperature = temperature;
140 if (tz->ops->get_trip_hyst) {
141 ret = tz->ops->get_trip_hyst(tz, trip, &hyst);
146 ret = tz->ops->get_trip_type(tz, trip, &type);
150 thermal_notify_tz_trip_change(tz->id, trip, type, temperature, hyst);
152 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
158 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
161 struct thermal_zone_device *tz = to_thermal_zone(dev);
165 if (!tz->ops->get_trip_temp)
168 if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1)
171 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
176 return sprintf(buf, "%d\n", temperature);
180 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
181 const char *buf, size_t count)
183 struct thermal_zone_device *tz = to_thermal_zone(dev);
187 if (!tz->ops->set_trip_hyst)
190 if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1)
193 if (kstrtoint(buf, 10, &temperature))
197 * We are not doing any check on the 'temperature' value
198 * here. The driver implementing 'set_trip_hyst' has to
201 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
204 thermal_zone_set_trips(tz);
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);
217 if (!tz->ops->get_trip_hyst)
220 if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1)
223 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
225 return ret ? ret : sprintf(buf, "%d\n", temperature);
229 policy_store(struct device *dev, struct device_attribute *attr,
230 const char *buf, size_t count)
232 struct thermal_zone_device *tz = to_thermal_zone(dev);
233 char name[THERMAL_NAME_LENGTH];
236 snprintf(name, sizeof(name), "%s", buf);
238 ret = thermal_zone_device_set_policy(tz, name);
246 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
248 struct thermal_zone_device *tz = to_thermal_zone(dev);
250 return sprintf(buf, "%s\n", tz->governor->name);
254 available_policies_show(struct device *dev, struct device_attribute *devattr,
257 return thermal_build_list_of_policies(buf);
260 #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
262 emul_temp_store(struct device *dev, struct device_attribute *attr,
263 const char *buf, size_t count)
265 struct thermal_zone_device *tz = to_thermal_zone(dev);
269 if (kstrtoint(buf, 10, &temperature))
272 if (!tz->ops->set_emul_temp) {
273 mutex_lock(&tz->lock);
274 tz->emul_temperature = temperature;
275 mutex_unlock(&tz->lock);
277 ret = tz->ops->set_emul_temp(tz, temperature);
281 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
283 return ret ? ret : count;
285 static DEVICE_ATTR_WO(emul_temp);
289 sustainable_power_show(struct device *dev, struct device_attribute *devattr,
292 struct thermal_zone_device *tz = to_thermal_zone(dev);
295 return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
301 sustainable_power_store(struct device *dev, struct device_attribute *devattr,
302 const char *buf, size_t count)
304 struct thermal_zone_device *tz = to_thermal_zone(dev);
305 u32 sustainable_power;
310 if (kstrtou32(buf, 10, &sustainable_power))
313 tz->tzp->sustainable_power = sustainable_power;
318 #define create_s32_tzp_attr(name) \
320 name##_show(struct device *dev, struct device_attribute *devattr, \
323 struct thermal_zone_device *tz = to_thermal_zone(dev); \
326 return sprintf(buf, "%d\n", tz->tzp->name); \
332 name##_store(struct device *dev, struct device_attribute *devattr, \
333 const char *buf, size_t count) \
335 struct thermal_zone_device *tz = to_thermal_zone(dev); \
341 if (kstrtos32(buf, 10, &value)) \
344 tz->tzp->name = value; \
348 static DEVICE_ATTR_RW(name)
350 create_s32_tzp_attr(k_po);
351 create_s32_tzp_attr(k_pu);
352 create_s32_tzp_attr(k_i);
353 create_s32_tzp_attr(k_d);
354 create_s32_tzp_attr(integral_cutoff);
355 create_s32_tzp_attr(slope);
356 create_s32_tzp_attr(offset);
357 #undef create_s32_tzp_attr
360 * These are thermal zone device attributes that will always be present.
361 * All the attributes created for tzp (create_s32_tzp_attr) also are always
362 * present on the sysfs interface.
364 static DEVICE_ATTR_RO(type);
365 static DEVICE_ATTR_RO(temp);
366 static DEVICE_ATTR_RW(policy);
367 static DEVICE_ATTR_RO(available_policies);
368 static DEVICE_ATTR_RW(sustainable_power);
370 /* These thermal zone device attributes are created based on conditions */
371 static DEVICE_ATTR_RW(mode);
373 /* These attributes are unconditionally added to a thermal zone */
374 static struct attribute *thermal_zone_dev_attrs[] = {
377 #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
378 &dev_attr_emul_temp.attr,
380 &dev_attr_policy.attr,
381 &dev_attr_available_policies.attr,
382 &dev_attr_sustainable_power.attr,
387 &dev_attr_integral_cutoff.attr,
388 &dev_attr_slope.attr,
389 &dev_attr_offset.attr,
393 static const struct attribute_group thermal_zone_attribute_group = {
394 .attrs = thermal_zone_dev_attrs,
397 static struct attribute *thermal_zone_mode_attrs[] = {
402 static const struct attribute_group thermal_zone_mode_attribute_group = {
403 .attrs = thermal_zone_mode_attrs,
406 static const struct attribute_group *thermal_zone_attribute_groups[] = {
407 &thermal_zone_attribute_group,
408 &thermal_zone_mode_attribute_group,
409 /* This is not NULL terminated as we create the group dynamically */
413 * create_trip_attrs() - create attributes for trip points
414 * @tz: the thermal zone device
415 * @mask: Writeable trip point bitmap.
417 * helper function to instantiate sysfs entries for every trip
418 * point and its properties of a struct thermal_zone_device.
420 * Return: 0 on success, the proper error value otherwise.
422 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
424 struct attribute **attrs;
427 /* This function works only for zones with at least one trip */
428 if (tz->num_trips <= 0)
431 tz->trip_type_attrs = kcalloc(tz->num_trips, sizeof(*tz->trip_type_attrs),
433 if (!tz->trip_type_attrs)
436 tz->trip_temp_attrs = kcalloc(tz->num_trips, sizeof(*tz->trip_temp_attrs),
438 if (!tz->trip_temp_attrs) {
439 kfree(tz->trip_type_attrs);
443 if (tz->ops->get_trip_hyst) {
444 tz->trip_hyst_attrs = kcalloc(tz->num_trips,
445 sizeof(*tz->trip_hyst_attrs),
447 if (!tz->trip_hyst_attrs) {
448 kfree(tz->trip_type_attrs);
449 kfree(tz->trip_temp_attrs);
454 attrs = kcalloc(tz->num_trips * 3 + 1, sizeof(*attrs), GFP_KERNEL);
456 kfree(tz->trip_type_attrs);
457 kfree(tz->trip_temp_attrs);
458 if (tz->ops->get_trip_hyst)
459 kfree(tz->trip_hyst_attrs);
463 for (indx = 0; indx < tz->num_trips; indx++) {
464 /* create trip type attribute */
465 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
466 "trip_point_%d_type", indx);
468 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
469 tz->trip_type_attrs[indx].attr.attr.name =
470 tz->trip_type_attrs[indx].name;
471 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
472 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
473 attrs[indx] = &tz->trip_type_attrs[indx].attr.attr;
475 /* create trip temp attribute */
476 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
477 "trip_point_%d_temp", indx);
479 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
480 tz->trip_temp_attrs[indx].attr.attr.name =
481 tz->trip_temp_attrs[indx].name;
482 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
483 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
484 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
485 mask & (1 << indx)) {
486 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
487 tz->trip_temp_attrs[indx].attr.store =
488 trip_point_temp_store;
490 attrs[indx + tz->num_trips] = &tz->trip_temp_attrs[indx].attr.attr;
492 /* create Optional trip hyst attribute */
493 if (!tz->ops->get_trip_hyst)
495 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
496 "trip_point_%d_hyst", indx);
498 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
499 tz->trip_hyst_attrs[indx].attr.attr.name =
500 tz->trip_hyst_attrs[indx].name;
501 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
502 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
503 if (tz->ops->set_trip_hyst) {
504 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
505 tz->trip_hyst_attrs[indx].attr.store =
506 trip_point_hyst_store;
508 attrs[indx + tz->num_trips * 2] =
509 &tz->trip_hyst_attrs[indx].attr.attr;
511 attrs[tz->num_trips * 3] = NULL;
513 tz->trips_attribute_group.attrs = attrs;
519 * destroy_trip_attrs() - destroy attributes for trip points
520 * @tz: the thermal zone device
522 * helper function to free resources allocated by create_trip_attrs()
524 static void destroy_trip_attrs(struct thermal_zone_device *tz)
529 kfree(tz->trip_type_attrs);
530 kfree(tz->trip_temp_attrs);
531 if (tz->ops->get_trip_hyst)
532 kfree(tz->trip_hyst_attrs);
533 kfree(tz->trips_attribute_group.attrs);
536 int thermal_zone_create_device_groups(struct thermal_zone_device *tz,
539 const struct attribute_group **groups;
542 /* we need one extra for trips and the NULL to terminate the array */
543 size = ARRAY_SIZE(thermal_zone_attribute_groups) + 2;
544 /* This also takes care of API requirement to be NULL terminated */
545 groups = kcalloc(size, sizeof(*groups), GFP_KERNEL);
549 for (i = 0; i < size - 2; i++)
550 groups[i] = thermal_zone_attribute_groups[i];
553 result = create_trip_attrs(tz, mask);
560 groups[size - 2] = &tz->trips_attribute_group;
563 tz->device.groups = groups;
568 void thermal_zone_destroy_device_groups(struct thermal_zone_device *tz)
574 destroy_trip_attrs(tz);
576 kfree(tz->device.groups);
579 /* sys I/F for cooling device */
581 cdev_type_show(struct device *dev, struct device_attribute *attr, char *buf)
583 struct thermal_cooling_device *cdev = to_cooling_device(dev);
585 return sprintf(buf, "%s\n", cdev->type);
588 static ssize_t max_state_show(struct device *dev, struct device_attribute *attr,
591 struct thermal_cooling_device *cdev = to_cooling_device(dev);
595 ret = cdev->ops->get_max_state(cdev, &state);
598 return sprintf(buf, "%ld\n", state);
601 static ssize_t cur_state_show(struct device *dev, struct device_attribute *attr,
604 struct thermal_cooling_device *cdev = to_cooling_device(dev);
608 ret = cdev->ops->get_cur_state(cdev, &state);
611 return sprintf(buf, "%ld\n", state);
615 cur_state_store(struct device *dev, struct device_attribute *attr,
616 const char *buf, size_t count)
618 struct thermal_cooling_device *cdev = to_cooling_device(dev);
622 if (sscanf(buf, "%ld\n", &state) != 1)
628 mutex_lock(&cdev->lock);
630 result = cdev->ops->set_cur_state(cdev, state);
632 thermal_cooling_device_stats_update(cdev, state);
634 mutex_unlock(&cdev->lock);
635 return result ? result : count;
638 static struct device_attribute
639 dev_attr_cdev_type = __ATTR(type, 0444, cdev_type_show, NULL);
640 static DEVICE_ATTR_RO(max_state);
641 static DEVICE_ATTR_RW(cur_state);
643 static struct attribute *cooling_device_attrs[] = {
644 &dev_attr_cdev_type.attr,
645 &dev_attr_max_state.attr,
646 &dev_attr_cur_state.attr,
650 static const struct attribute_group cooling_device_attr_group = {
651 .attrs = cooling_device_attrs,
654 static const struct attribute_group *cooling_device_attr_groups[] = {
655 &cooling_device_attr_group,
656 NULL, /* Space allocated for cooling_device_stats_attr_group */
660 #ifdef CONFIG_THERMAL_STATISTICS
661 struct cooling_dev_stats {
663 unsigned int total_trans;
665 unsigned long max_states;
667 ktime_t *time_in_state;
668 unsigned int *trans_table;
671 static void update_time_in_state(struct cooling_dev_stats *stats)
673 ktime_t now = ktime_get(), delta;
675 delta = ktime_sub(now, stats->last_time);
676 stats->time_in_state[stats->state] =
677 ktime_add(stats->time_in_state[stats->state], delta);
678 stats->last_time = now;
681 void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
682 unsigned long new_state)
684 struct cooling_dev_stats *stats = cdev->stats;
689 spin_lock(&stats->lock);
691 if (stats->state == new_state)
694 update_time_in_state(stats);
695 stats->trans_table[stats->state * stats->max_states + new_state]++;
696 stats->state = new_state;
697 stats->total_trans++;
700 spin_unlock(&stats->lock);
703 static ssize_t total_trans_show(struct device *dev,
704 struct device_attribute *attr, char *buf)
706 struct thermal_cooling_device *cdev = to_cooling_device(dev);
707 struct cooling_dev_stats *stats = cdev->stats;
710 spin_lock(&stats->lock);
711 ret = sprintf(buf, "%u\n", stats->total_trans);
712 spin_unlock(&stats->lock);
718 time_in_state_ms_show(struct device *dev, struct device_attribute *attr,
721 struct thermal_cooling_device *cdev = to_cooling_device(dev);
722 struct cooling_dev_stats *stats = cdev->stats;
726 spin_lock(&stats->lock);
727 update_time_in_state(stats);
729 for (i = 0; i < stats->max_states; i++) {
730 len += sprintf(buf + len, "state%u\t%llu\n", i,
731 ktime_to_ms(stats->time_in_state[i]));
733 spin_unlock(&stats->lock);
739 reset_store(struct device *dev, struct device_attribute *attr, const char *buf,
742 struct thermal_cooling_device *cdev = to_cooling_device(dev);
743 struct cooling_dev_stats *stats = cdev->stats;
744 int i, states = stats->max_states;
746 spin_lock(&stats->lock);
748 stats->total_trans = 0;
749 stats->last_time = ktime_get();
750 memset(stats->trans_table, 0,
751 states * states * sizeof(*stats->trans_table));
753 for (i = 0; i < stats->max_states; i++)
754 stats->time_in_state[i] = ktime_set(0, 0);
756 spin_unlock(&stats->lock);
761 static ssize_t trans_table_show(struct device *dev,
762 struct device_attribute *attr, char *buf)
764 struct thermal_cooling_device *cdev = to_cooling_device(dev);
765 struct cooling_dev_stats *stats = cdev->stats;
769 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
770 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
771 for (i = 0; i < stats->max_states; i++) {
772 if (len >= PAGE_SIZE)
774 len += snprintf(buf + len, PAGE_SIZE - len, "state%2u ", i);
776 if (len >= PAGE_SIZE)
779 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
781 for (i = 0; i < stats->max_states; i++) {
782 if (len >= PAGE_SIZE)
785 len += snprintf(buf + len, PAGE_SIZE - len, "state%2u:", i);
787 for (j = 0; j < stats->max_states; j++) {
788 if (len >= PAGE_SIZE)
790 len += snprintf(buf + len, PAGE_SIZE - len, "%8u ",
791 stats->trans_table[i * stats->max_states + j]);
793 if (len >= PAGE_SIZE)
795 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
798 if (len >= PAGE_SIZE) {
799 pr_warn_once("Thermal transition table exceeds PAGE_SIZE. Disabling\n");
805 static DEVICE_ATTR_RO(total_trans);
806 static DEVICE_ATTR_RO(time_in_state_ms);
807 static DEVICE_ATTR_WO(reset);
808 static DEVICE_ATTR_RO(trans_table);
810 static struct attribute *cooling_device_stats_attrs[] = {
811 &dev_attr_total_trans.attr,
812 &dev_attr_time_in_state_ms.attr,
813 &dev_attr_reset.attr,
814 &dev_attr_trans_table.attr,
818 static const struct attribute_group cooling_device_stats_attr_group = {
819 .attrs = cooling_device_stats_attrs,
823 static void cooling_device_stats_setup(struct thermal_cooling_device *cdev)
825 const struct attribute_group *stats_attr_group = NULL;
826 struct cooling_dev_stats *stats;
827 unsigned long states;
830 if (cdev->ops->get_max_state(cdev, &states))
833 states++; /* Total number of states is highest state + 1 */
835 var = sizeof(*stats);
836 var += sizeof(*stats->time_in_state) * states;
837 var += sizeof(*stats->trans_table) * states * states;
839 stats = kzalloc(var, GFP_KERNEL);
843 stats->time_in_state = (ktime_t *)(stats + 1);
844 stats->trans_table = (unsigned int *)(stats->time_in_state + states);
846 stats->last_time = ktime_get();
847 stats->max_states = states;
849 spin_lock_init(&stats->lock);
851 stats_attr_group = &cooling_device_stats_attr_group;
854 /* Fill the empty slot left in cooling_device_attr_groups */
855 var = ARRAY_SIZE(cooling_device_attr_groups) - 2;
856 cooling_device_attr_groups[var] = stats_attr_group;
859 static void cooling_device_stats_destroy(struct thermal_cooling_device *cdev)
868 cooling_device_stats_setup(struct thermal_cooling_device *cdev) {}
870 cooling_device_stats_destroy(struct thermal_cooling_device *cdev) {}
872 #endif /* CONFIG_THERMAL_STATISTICS */
874 void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *cdev)
876 cooling_device_stats_setup(cdev);
877 cdev->device.groups = cooling_device_attr_groups;
880 void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev)
882 cooling_device_stats_destroy(cdev);
885 /* these helper will be used only at the time of bindig */
887 trip_point_show(struct device *dev, struct device_attribute *attr, char *buf)
889 struct thermal_instance *instance;
892 container_of(attr, struct thermal_instance, attr);
894 return sprintf(buf, "%d\n", instance->trip);
898 weight_show(struct device *dev, struct device_attribute *attr, char *buf)
900 struct thermal_instance *instance;
902 instance = container_of(attr, struct thermal_instance, weight_attr);
904 return sprintf(buf, "%d\n", instance->weight);
907 ssize_t weight_store(struct device *dev, struct device_attribute *attr,
908 const char *buf, size_t count)
910 struct thermal_instance *instance;
913 ret = kstrtoint(buf, 0, &weight);
917 instance = container_of(attr, struct thermal_instance, weight_attr);
918 instance->weight = weight;