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 mutex_lock(&tz->lock);
190 if (!device_is_registered(dev)) {
195 ret = __thermal_zone_get_trip(tz, trip_id, &trip);
199 ret = kstrtoint(buf, 10, &trip.hysteresis);
203 ret = thermal_zone_set_trip(tz, trip_id, &trip);
205 mutex_unlock(&tz->lock);
207 return ret ? ret : count;
211 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
214 struct thermal_zone_device *tz = to_thermal_zone(dev);
215 struct thermal_trip trip;
218 if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip_id) != 1)
221 mutex_lock(&tz->lock);
223 if (device_is_registered(dev))
224 ret = __thermal_zone_get_trip(tz, trip_id, &trip);
228 mutex_unlock(&tz->lock);
230 return ret ? ret : sprintf(buf, "%d\n", trip.hysteresis);
234 policy_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);
238 char name[THERMAL_NAME_LENGTH];
241 snprintf(name, sizeof(name), "%s", buf);
243 ret = thermal_zone_device_set_policy(tz, name);
251 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
253 struct thermal_zone_device *tz = to_thermal_zone(dev);
255 return sprintf(buf, "%s\n", tz->governor->name);
259 available_policies_show(struct device *dev, struct device_attribute *devattr,
262 return thermal_build_list_of_policies(buf);
265 #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
267 emul_temp_store(struct device *dev, struct device_attribute *attr,
268 const char *buf, size_t count)
270 struct thermal_zone_device *tz = to_thermal_zone(dev);
274 if (kstrtoint(buf, 10, &temperature))
277 mutex_lock(&tz->lock);
279 if (!device_is_registered(dev)) {
284 if (!tz->ops->set_emul_temp)
285 tz->emul_temperature = temperature;
287 ret = tz->ops->set_emul_temp(tz, temperature);
290 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
293 mutex_unlock(&tz->lock);
295 return ret ? ret : count;
297 static DEVICE_ATTR_WO(emul_temp);
301 sustainable_power_show(struct device *dev, struct device_attribute *devattr,
304 struct thermal_zone_device *tz = to_thermal_zone(dev);
307 return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
313 sustainable_power_store(struct device *dev, struct device_attribute *devattr,
314 const char *buf, size_t count)
316 struct thermal_zone_device *tz = to_thermal_zone(dev);
317 u32 sustainable_power;
322 if (kstrtou32(buf, 10, &sustainable_power))
325 tz->tzp->sustainable_power = sustainable_power;
330 #define create_s32_tzp_attr(name) \
332 name##_show(struct device *dev, struct device_attribute *devattr, \
335 struct thermal_zone_device *tz = to_thermal_zone(dev); \
338 return sprintf(buf, "%d\n", tz->tzp->name); \
344 name##_store(struct device *dev, struct device_attribute *devattr, \
345 const char *buf, size_t count) \
347 struct thermal_zone_device *tz = to_thermal_zone(dev); \
353 if (kstrtos32(buf, 10, &value)) \
356 tz->tzp->name = value; \
360 static DEVICE_ATTR_RW(name)
362 create_s32_tzp_attr(k_po);
363 create_s32_tzp_attr(k_pu);
364 create_s32_tzp_attr(k_i);
365 create_s32_tzp_attr(k_d);
366 create_s32_tzp_attr(integral_cutoff);
367 create_s32_tzp_attr(slope);
368 create_s32_tzp_attr(offset);
369 #undef create_s32_tzp_attr
372 * These are thermal zone device attributes that will always be present.
373 * All the attributes created for tzp (create_s32_tzp_attr) also are always
374 * present on the sysfs interface.
376 static DEVICE_ATTR_RO(type);
377 static DEVICE_ATTR_RO(temp);
378 static DEVICE_ATTR_RW(policy);
379 static DEVICE_ATTR_RO(available_policies);
380 static DEVICE_ATTR_RW(sustainable_power);
382 /* These thermal zone device attributes are created based on conditions */
383 static DEVICE_ATTR_RW(mode);
385 /* These attributes are unconditionally added to a thermal zone */
386 static struct attribute *thermal_zone_dev_attrs[] = {
389 #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
390 &dev_attr_emul_temp.attr,
392 &dev_attr_policy.attr,
393 &dev_attr_available_policies.attr,
394 &dev_attr_sustainable_power.attr,
399 &dev_attr_integral_cutoff.attr,
400 &dev_attr_slope.attr,
401 &dev_attr_offset.attr,
405 static const struct attribute_group thermal_zone_attribute_group = {
406 .attrs = thermal_zone_dev_attrs,
409 static struct attribute *thermal_zone_mode_attrs[] = {
414 static const struct attribute_group thermal_zone_mode_attribute_group = {
415 .attrs = thermal_zone_mode_attrs,
418 static const struct attribute_group *thermal_zone_attribute_groups[] = {
419 &thermal_zone_attribute_group,
420 &thermal_zone_mode_attribute_group,
421 /* This is not NULL terminated as we create the group dynamically */
425 * create_trip_attrs() - create attributes for trip points
426 * @tz: the thermal zone device
427 * @mask: Writeable trip point bitmap.
429 * helper function to instantiate sysfs entries for every trip
430 * point and its properties of a struct thermal_zone_device.
432 * Return: 0 on success, the proper error value otherwise.
434 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
436 struct attribute **attrs;
439 /* This function works only for zones with at least one trip */
440 if (tz->num_trips <= 0)
443 tz->trip_type_attrs = kcalloc(tz->num_trips, sizeof(*tz->trip_type_attrs),
445 if (!tz->trip_type_attrs)
448 tz->trip_temp_attrs = kcalloc(tz->num_trips, sizeof(*tz->trip_temp_attrs),
450 if (!tz->trip_temp_attrs) {
451 kfree(tz->trip_type_attrs);
455 tz->trip_hyst_attrs = kcalloc(tz->num_trips,
456 sizeof(*tz->trip_hyst_attrs),
458 if (!tz->trip_hyst_attrs) {
459 kfree(tz->trip_type_attrs);
460 kfree(tz->trip_temp_attrs);
464 attrs = kcalloc(tz->num_trips * 3 + 1, sizeof(*attrs), GFP_KERNEL);
466 kfree(tz->trip_type_attrs);
467 kfree(tz->trip_temp_attrs);
468 kfree(tz->trip_hyst_attrs);
472 for (indx = 0; indx < tz->num_trips; indx++) {
473 /* create trip type attribute */
474 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
475 "trip_point_%d_type", indx);
477 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
478 tz->trip_type_attrs[indx].attr.attr.name =
479 tz->trip_type_attrs[indx].name;
480 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
481 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
482 attrs[indx] = &tz->trip_type_attrs[indx].attr.attr;
484 /* create trip temp attribute */
485 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
486 "trip_point_%d_temp", indx);
488 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
489 tz->trip_temp_attrs[indx].attr.attr.name =
490 tz->trip_temp_attrs[indx].name;
491 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
492 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
493 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
494 mask & (1 << indx)) {
495 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
496 tz->trip_temp_attrs[indx].attr.store =
497 trip_point_temp_store;
499 attrs[indx + tz->num_trips] = &tz->trip_temp_attrs[indx].attr.attr;
501 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
502 "trip_point_%d_hyst", indx);
504 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
505 tz->trip_hyst_attrs[indx].attr.attr.name =
506 tz->trip_hyst_attrs[indx].name;
507 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
508 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
509 if (tz->ops->set_trip_hyst) {
510 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
511 tz->trip_hyst_attrs[indx].attr.store =
512 trip_point_hyst_store;
514 attrs[indx + tz->num_trips * 2] =
515 &tz->trip_hyst_attrs[indx].attr.attr;
517 attrs[tz->num_trips * 3] = NULL;
519 tz->trips_attribute_group.attrs = attrs;
525 * destroy_trip_attrs() - destroy attributes for trip points
526 * @tz: the thermal zone device
528 * helper function to free resources allocated by create_trip_attrs()
530 static void destroy_trip_attrs(struct thermal_zone_device *tz)
535 kfree(tz->trip_type_attrs);
536 kfree(tz->trip_temp_attrs);
537 kfree(tz->trip_hyst_attrs);
538 kfree(tz->trips_attribute_group.attrs);
541 int thermal_zone_create_device_groups(struct thermal_zone_device *tz,
544 const struct attribute_group **groups;
547 /* we need one extra for trips and the NULL to terminate the array */
548 size = ARRAY_SIZE(thermal_zone_attribute_groups) + 2;
549 /* This also takes care of API requirement to be NULL terminated */
550 groups = kcalloc(size, sizeof(*groups), GFP_KERNEL);
554 for (i = 0; i < size - 2; i++)
555 groups[i] = thermal_zone_attribute_groups[i];
558 result = create_trip_attrs(tz, mask);
565 groups[size - 2] = &tz->trips_attribute_group;
568 tz->device.groups = groups;
573 void thermal_zone_destroy_device_groups(struct thermal_zone_device *tz)
579 destroy_trip_attrs(tz);
581 kfree(tz->device.groups);
584 /* sys I/F for cooling device */
586 cdev_type_show(struct device *dev, struct device_attribute *attr, char *buf)
588 struct thermal_cooling_device *cdev = to_cooling_device(dev);
590 return sprintf(buf, "%s\n", cdev->type);
593 static ssize_t max_state_show(struct device *dev, struct device_attribute *attr,
596 struct thermal_cooling_device *cdev = to_cooling_device(dev);
598 return sprintf(buf, "%ld\n", cdev->max_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 /* Requested state should be less than max_state + 1 */
629 if (state > cdev->max_state)
632 mutex_lock(&cdev->lock);
634 result = cdev->ops->set_cur_state(cdev, state);
636 thermal_cooling_device_stats_update(cdev, state);
638 mutex_unlock(&cdev->lock);
639 return result ? result : count;
642 static struct device_attribute
643 dev_attr_cdev_type = __ATTR(type, 0444, cdev_type_show, NULL);
644 static DEVICE_ATTR_RO(max_state);
645 static DEVICE_ATTR_RW(cur_state);
647 static struct attribute *cooling_device_attrs[] = {
648 &dev_attr_cdev_type.attr,
649 &dev_attr_max_state.attr,
650 &dev_attr_cur_state.attr,
654 static const struct attribute_group cooling_device_attr_group = {
655 .attrs = cooling_device_attrs,
658 static const struct attribute_group *cooling_device_attr_groups[] = {
659 &cooling_device_attr_group,
660 NULL, /* Space allocated for cooling_device_stats_attr_group */
664 #ifdef CONFIG_THERMAL_STATISTICS
665 struct cooling_dev_stats {
667 unsigned int total_trans;
670 ktime_t *time_in_state;
671 unsigned int *trans_table;
674 static void update_time_in_state(struct cooling_dev_stats *stats)
676 ktime_t now = ktime_get(), delta;
678 delta = ktime_sub(now, stats->last_time);
679 stats->time_in_state[stats->state] =
680 ktime_add(stats->time_in_state[stats->state], delta);
681 stats->last_time = now;
684 void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
685 unsigned long new_state)
687 struct cooling_dev_stats *stats = cdev->stats;
689 lockdep_assert_held(&cdev->lock);
694 spin_lock(&stats->lock);
696 if (stats->state == new_state)
699 update_time_in_state(stats);
700 stats->trans_table[stats->state * (cdev->max_state + 1) + new_state]++;
701 stats->state = new_state;
702 stats->total_trans++;
705 spin_unlock(&stats->lock);
708 static ssize_t total_trans_show(struct device *dev,
709 struct device_attribute *attr, char *buf)
711 struct thermal_cooling_device *cdev = to_cooling_device(dev);
712 struct cooling_dev_stats *stats;
715 mutex_lock(&cdev->lock);
721 spin_lock(&stats->lock);
722 ret = sprintf(buf, "%u\n", stats->total_trans);
723 spin_unlock(&stats->lock);
726 mutex_unlock(&cdev->lock);
732 time_in_state_ms_show(struct device *dev, struct device_attribute *attr,
735 struct thermal_cooling_device *cdev = to_cooling_device(dev);
736 struct cooling_dev_stats *stats;
740 mutex_lock(&cdev->lock);
746 spin_lock(&stats->lock);
748 update_time_in_state(stats);
750 for (i = 0; i <= cdev->max_state; i++) {
751 len += sprintf(buf + len, "state%u\t%llu\n", i,
752 ktime_to_ms(stats->time_in_state[i]));
754 spin_unlock(&stats->lock);
757 mutex_unlock(&cdev->lock);
763 reset_store(struct device *dev, struct device_attribute *attr, const char *buf,
766 struct thermal_cooling_device *cdev = to_cooling_device(dev);
767 struct cooling_dev_stats *stats;
770 mutex_lock(&cdev->lock);
776 states = cdev->max_state + 1;
778 spin_lock(&stats->lock);
780 stats->total_trans = 0;
781 stats->last_time = ktime_get();
782 memset(stats->trans_table, 0,
783 states * states * sizeof(*stats->trans_table));
785 for (i = 0; i < states; i++)
786 stats->time_in_state[i] = ktime_set(0, 0);
788 spin_unlock(&stats->lock);
791 mutex_unlock(&cdev->lock);
796 static ssize_t trans_table_show(struct device *dev,
797 struct device_attribute *attr, char *buf)
799 struct thermal_cooling_device *cdev = to_cooling_device(dev);
800 struct cooling_dev_stats *stats;
804 mutex_lock(&cdev->lock);
812 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
813 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
814 for (i = 0; i <= cdev->max_state; i++) {
815 if (len >= PAGE_SIZE)
817 len += snprintf(buf + len, PAGE_SIZE - len, "state%2u ", i);
819 if (len >= PAGE_SIZE) {
824 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
826 for (i = 0; i <= cdev->max_state; i++) {
827 if (len >= PAGE_SIZE)
830 len += snprintf(buf + len, PAGE_SIZE - len, "state%2u:", i);
832 for (j = 0; j <= cdev->max_state; j++) {
833 if (len >= PAGE_SIZE)
835 len += snprintf(buf + len, PAGE_SIZE - len, "%8u ",
836 stats->trans_table[i * (cdev->max_state + 1) + j]);
838 if (len >= PAGE_SIZE)
840 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
843 if (len >= PAGE_SIZE) {
844 pr_warn_once("Thermal transition table exceeds PAGE_SIZE. Disabling\n");
849 mutex_unlock(&cdev->lock);
854 static DEVICE_ATTR_RO(total_trans);
855 static DEVICE_ATTR_RO(time_in_state_ms);
856 static DEVICE_ATTR_WO(reset);
857 static DEVICE_ATTR_RO(trans_table);
859 static struct attribute *cooling_device_stats_attrs[] = {
860 &dev_attr_total_trans.attr,
861 &dev_attr_time_in_state_ms.attr,
862 &dev_attr_reset.attr,
863 &dev_attr_trans_table.attr,
867 static const struct attribute_group cooling_device_stats_attr_group = {
868 .attrs = cooling_device_stats_attrs,
872 static void cooling_device_stats_setup(struct thermal_cooling_device *cdev)
874 const struct attribute_group *stats_attr_group = NULL;
875 struct cooling_dev_stats *stats;
876 /* Total number of states is highest state + 1 */
877 unsigned long states = cdev->max_state + 1;
880 var = sizeof(*stats);
881 var += sizeof(*stats->time_in_state) * states;
882 var += sizeof(*stats->trans_table) * states * states;
884 stats = kzalloc(var, GFP_KERNEL);
888 stats->time_in_state = (ktime_t *)(stats + 1);
889 stats->trans_table = (unsigned int *)(stats->time_in_state + states);
891 stats->last_time = ktime_get();
893 spin_lock_init(&stats->lock);
895 stats_attr_group = &cooling_device_stats_attr_group;
898 /* Fill the empty slot left in cooling_device_attr_groups */
899 var = ARRAY_SIZE(cooling_device_attr_groups) - 2;
900 cooling_device_attr_groups[var] = stats_attr_group;
903 static void cooling_device_stats_destroy(struct thermal_cooling_device *cdev)
912 cooling_device_stats_setup(struct thermal_cooling_device *cdev) {}
914 cooling_device_stats_destroy(struct thermal_cooling_device *cdev) {}
916 #endif /* CONFIG_THERMAL_STATISTICS */
918 void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *cdev)
920 cooling_device_stats_setup(cdev);
921 cdev->device.groups = cooling_device_attr_groups;
924 void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev)
926 cooling_device_stats_destroy(cdev);
929 void thermal_cooling_device_stats_reinit(struct thermal_cooling_device *cdev)
931 lockdep_assert_held(&cdev->lock);
933 cooling_device_stats_destroy(cdev);
934 cooling_device_stats_setup(cdev);
937 /* these helper will be used only at the time of bindig */
939 trip_point_show(struct device *dev, struct device_attribute *attr, char *buf)
941 struct thermal_instance *instance;
944 container_of(attr, struct thermal_instance, attr);
946 return sprintf(buf, "%d\n", instance->trip);
950 weight_show(struct device *dev, struct device_attribute *attr, char *buf)
952 struct thermal_instance *instance;
954 instance = container_of(attr, struct thermal_instance, weight_attr);
956 return sprintf(buf, "%d\n", instance->weight);
959 ssize_t weight_store(struct device *dev, struct device_attribute *attr,
960 const char *buf, size_t count)
962 struct thermal_instance *instance;
965 ret = kstrtoint(buf, 0, &weight);
969 instance = container_of(attr, struct thermal_instance, weight_attr);
970 instance->weight = weight;