2 * thermal.c - sysfs interface of thermal devices
4 * Copyright (C) 2016 Eduardo Valentin <edubezval@gmail.com>
6 * Highly based on original thermal_core.c
7 * Copyright (C) 2008 Intel Corp
8 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
9 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/sysfs.h>
19 #include <linux/device.h>
20 #include <linux/err.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
24 #include "thermal_core.h"
26 /* sys I/F for thermal zone */
29 type_show(struct device *dev, struct device_attribute *attr, char *buf)
31 struct thermal_zone_device *tz = to_thermal_zone(dev);
33 return sprintf(buf, "%s\n", tz->type);
37 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
39 struct thermal_zone_device *tz = to_thermal_zone(dev);
42 ret = thermal_zone_get_temp(tz, &temperature);
47 return sprintf(buf, "%d\n", temperature);
51 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
53 struct thermal_zone_device *tz = to_thermal_zone(dev);
54 enum thermal_device_mode mode;
57 if (!tz->ops->get_mode)
60 result = tz->ops->get_mode(tz, &mode);
64 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
69 mode_store(struct device *dev, struct device_attribute *attr,
70 const char *buf, size_t count)
72 struct thermal_zone_device *tz = to_thermal_zone(dev);
75 if (!tz->ops->set_mode)
78 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
79 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
80 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
81 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
92 trip_point_type_show(struct device *dev, struct device_attribute *attr,
95 struct thermal_zone_device *tz = to_thermal_zone(dev);
96 enum thermal_trip_type type;
99 if (!tz->ops->get_trip_type)
102 if (sscanf(attr->attr.name, "trip_point_%d_type", &trip) != 1)
105 result = tz->ops->get_trip_type(tz, trip, &type);
110 case THERMAL_TRIP_CRITICAL:
111 return sprintf(buf, "critical\n");
112 case THERMAL_TRIP_HOT:
113 return sprintf(buf, "hot\n");
114 case THERMAL_TRIP_PASSIVE:
115 return sprintf(buf, "passive\n");
116 case THERMAL_TRIP_ACTIVE:
117 return sprintf(buf, "active\n");
119 return sprintf(buf, "unknown\n");
124 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
125 const char *buf, size_t count)
127 struct thermal_zone_device *tz = to_thermal_zone(dev);
131 if (!tz->ops->set_trip_temp)
134 if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1)
137 if (kstrtoint(buf, 10, &temperature))
140 ret = tz->ops->set_trip_temp(tz, trip, temperature);
144 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
150 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
153 struct thermal_zone_device *tz = to_thermal_zone(dev);
157 if (!tz->ops->get_trip_temp)
160 if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1)
163 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
168 return sprintf(buf, "%d\n", temperature);
172 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
173 const char *buf, size_t count)
175 struct thermal_zone_device *tz = to_thermal_zone(dev);
179 if (!tz->ops->set_trip_hyst)
182 if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1)
185 if (kstrtoint(buf, 10, &temperature))
189 * We are not doing any check on the 'temperature' value
190 * here. The driver implementing 'set_trip_hyst' has to
193 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
196 thermal_zone_set_trips(tz);
198 return ret ? ret : count;
202 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
205 struct thermal_zone_device *tz = to_thermal_zone(dev);
209 if (!tz->ops->get_trip_hyst)
212 if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1)
215 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
217 return ret ? ret : sprintf(buf, "%d\n", temperature);
221 passive_store(struct device *dev, struct device_attribute *attr,
222 const char *buf, size_t count)
224 struct thermal_zone_device *tz = to_thermal_zone(dev);
227 if (sscanf(buf, "%d\n", &state) != 1)
230 /* sanity check: values below 1000 millicelcius don't make sense
231 * and can cause the system to go into a thermal heart attack
233 if (state && state < 1000)
236 if (state && !tz->forced_passive) {
237 if (!tz->passive_delay)
238 tz->passive_delay = 1000;
239 thermal_zone_device_rebind_exception(tz, "Processor",
240 sizeof("Processor"));
241 } else if (!state && tz->forced_passive) {
242 tz->passive_delay = 0;
243 thermal_zone_device_unbind_exception(tz, "Processor",
244 sizeof("Processor"));
247 tz->forced_passive = state;
249 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
255 passive_show(struct device *dev, struct device_attribute *attr,
258 struct thermal_zone_device *tz = to_thermal_zone(dev);
260 return sprintf(buf, "%d\n", tz->forced_passive);
264 policy_store(struct device *dev, struct device_attribute *attr,
265 const char *buf, size_t count)
267 struct thermal_zone_device *tz = to_thermal_zone(dev);
268 char name[THERMAL_NAME_LENGTH];
271 snprintf(name, sizeof(name), "%s", buf);
273 ret = thermal_zone_device_set_policy(tz, name);
281 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
283 struct thermal_zone_device *tz = to_thermal_zone(dev);
285 return sprintf(buf, "%s\n", tz->governor->name);
289 available_policies_show(struct device *dev, struct device_attribute *devattr,
292 return thermal_build_list_of_policies(buf);
295 #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
297 emul_temp_store(struct device *dev, struct device_attribute *attr,
298 const char *buf, size_t count)
300 struct thermal_zone_device *tz = to_thermal_zone(dev);
304 if (kstrtoint(buf, 10, &temperature))
307 if (!tz->ops->set_emul_temp) {
308 mutex_lock(&tz->lock);
309 tz->emul_temperature = temperature;
310 mutex_unlock(&tz->lock);
312 ret = tz->ops->set_emul_temp(tz, temperature);
316 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
318 return ret ? ret : count;
320 static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
324 sustainable_power_show(struct device *dev, struct device_attribute *devattr,
327 struct thermal_zone_device *tz = to_thermal_zone(dev);
330 return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
336 sustainable_power_store(struct device *dev, struct device_attribute *devattr,
337 const char *buf, size_t count)
339 struct thermal_zone_device *tz = to_thermal_zone(dev);
340 u32 sustainable_power;
345 if (kstrtou32(buf, 10, &sustainable_power))
348 tz->tzp->sustainable_power = sustainable_power;
353 #define create_s32_tzp_attr(name) \
355 name##_show(struct device *dev, struct device_attribute *devattr, \
358 struct thermal_zone_device *tz = to_thermal_zone(dev); \
361 return sprintf(buf, "%d\n", tz->tzp->name); \
367 name##_store(struct device *dev, struct device_attribute *devattr, \
368 const char *buf, size_t count) \
370 struct thermal_zone_device *tz = to_thermal_zone(dev); \
376 if (kstrtos32(buf, 10, &value)) \
379 tz->tzp->name = value; \
383 static DEVICE_ATTR(name, S_IWUSR | S_IRUGO, name##_show, name##_store)
385 create_s32_tzp_attr(k_po);
386 create_s32_tzp_attr(k_pu);
387 create_s32_tzp_attr(k_i);
388 create_s32_tzp_attr(k_d);
389 create_s32_tzp_attr(integral_cutoff);
390 create_s32_tzp_attr(slope);
391 create_s32_tzp_attr(offset);
392 #undef create_s32_tzp_attr
395 * These are thermal zone device attributes that will always be present.
396 * All the attributes created for tzp (create_s32_tzp_attr) also are always
397 * present on the sysfs interface.
399 static DEVICE_ATTR(type, 0444, type_show, NULL);
400 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
401 static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
402 static DEVICE_ATTR(available_policies, S_IRUGO, available_policies_show, NULL);
403 static DEVICE_ATTR(sustainable_power, S_IWUSR | S_IRUGO, sustainable_power_show,
404 sustainable_power_store);
406 /* These thermal zone device attributes are created based on conditions */
407 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
408 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
410 /* These attributes are unconditionally added to a thermal zone */
411 static struct attribute *thermal_zone_dev_attrs[] = {
414 #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
415 &dev_attr_emul_temp.attr,
417 &dev_attr_policy.attr,
418 &dev_attr_available_policies.attr,
419 &dev_attr_sustainable_power.attr,
424 &dev_attr_integral_cutoff.attr,
425 &dev_attr_slope.attr,
426 &dev_attr_offset.attr,
430 static struct attribute_group thermal_zone_attribute_group = {
431 .attrs = thermal_zone_dev_attrs,
434 /* We expose mode only if .get_mode is present */
435 static struct attribute *thermal_zone_mode_attrs[] = {
440 static umode_t thermal_zone_mode_is_visible(struct kobject *kobj,
441 struct attribute *attr,
444 struct device *dev = container_of(kobj, struct device, kobj);
445 struct thermal_zone_device *tz;
447 tz = container_of(dev, struct thermal_zone_device, device);
449 if (tz->ops->get_mode)
455 static struct attribute_group thermal_zone_mode_attribute_group = {
456 .attrs = thermal_zone_mode_attrs,
457 .is_visible = thermal_zone_mode_is_visible,
460 /* We expose passive only if passive trips are present */
461 static struct attribute *thermal_zone_passive_attrs[] = {
462 &dev_attr_passive.attr,
466 static umode_t thermal_zone_passive_is_visible(struct kobject *kobj,
467 struct attribute *attr,
470 struct device *dev = container_of(kobj, struct device, kobj);
471 struct thermal_zone_device *tz;
472 enum thermal_trip_type trip_type;
473 int count, passive = 0;
475 tz = container_of(dev, struct thermal_zone_device, device);
477 for (count = 0; count < tz->trips && !passive; count++) {
478 tz->ops->get_trip_type(tz, count, &trip_type);
480 if (trip_type == THERMAL_TRIP_PASSIVE)
490 static struct attribute_group thermal_zone_passive_attribute_group = {
491 .attrs = thermal_zone_passive_attrs,
492 .is_visible = thermal_zone_passive_is_visible,
495 static const struct attribute_group *thermal_zone_attribute_groups[] = {
496 &thermal_zone_attribute_group,
497 &thermal_zone_mode_attribute_group,
498 &thermal_zone_passive_attribute_group,
499 /* This is not NULL terminated as we create the group dynamically */
503 * create_trip_attrs() - create attributes for trip points
504 * @tz: the thermal zone device
505 * @mask: Writeable trip point bitmap.
507 * helper function to instantiate sysfs entries for every trip
508 * point and its properties of a struct thermal_zone_device.
510 * Return: 0 on success, the proper error value otherwise.
512 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
514 int size = sizeof(struct thermal_attr) * tz->trips;
515 struct attribute **attrs;
518 /* This function works only for zones with at least one trip */
522 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
523 if (!tz->trip_type_attrs)
526 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
527 if (!tz->trip_temp_attrs) {
528 kfree(tz->trip_type_attrs);
532 if (tz->ops->get_trip_hyst) {
533 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
534 if (!tz->trip_hyst_attrs) {
535 kfree(tz->trip_type_attrs);
536 kfree(tz->trip_temp_attrs);
541 attrs = kzalloc(sizeof(*attrs) * tz->trips * 3 + 1, GFP_KERNEL);
543 kfree(tz->trip_type_attrs);
544 kfree(tz->trip_temp_attrs);
545 if (tz->ops->get_trip_hyst)
546 kfree(tz->trip_hyst_attrs);
550 for (indx = 0; indx < tz->trips; indx++) {
551 /* create trip type attribute */
552 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
553 "trip_point_%d_type", indx);
555 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
556 tz->trip_type_attrs[indx].attr.attr.name =
557 tz->trip_type_attrs[indx].name;
558 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
559 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
560 attrs[indx] = &tz->trip_type_attrs[indx].attr.attr;
562 /* create trip temp attribute */
563 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
564 "trip_point_%d_temp", indx);
566 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
567 tz->trip_temp_attrs[indx].attr.attr.name =
568 tz->trip_temp_attrs[indx].name;
569 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
570 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
571 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
572 mask & (1 << indx)) {
573 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
574 tz->trip_temp_attrs[indx].attr.store =
575 trip_point_temp_store;
577 attrs[indx + tz->trips] = &tz->trip_temp_attrs[indx].attr.attr;
579 /* create Optional trip hyst attribute */
580 if (!tz->ops->get_trip_hyst)
582 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
583 "trip_point_%d_hyst", indx);
585 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
586 tz->trip_hyst_attrs[indx].attr.attr.name =
587 tz->trip_hyst_attrs[indx].name;
588 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
589 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
590 if (tz->ops->set_trip_hyst) {
591 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
592 tz->trip_hyst_attrs[indx].attr.store =
593 trip_point_hyst_store;
595 attrs[indx + tz->trips * 2] =
596 &tz->trip_hyst_attrs[indx].attr.attr;
598 attrs[tz->trips * 3] = NULL;
600 tz->trips_attribute_group.attrs = attrs;
605 int thermal_zone_create_device_groups(struct thermal_zone_device *tz,
608 const struct attribute_group **groups;
611 /* we need one extra for trips and the NULL to terminate the array */
612 size = ARRAY_SIZE(thermal_zone_attribute_groups) + 2;
613 /* This also takes care of API requirement to be NULL terminated */
614 groups = kcalloc(size, sizeof(*groups), GFP_KERNEL);
618 for (i = 0; i < size - 2; i++)
619 groups[i] = thermal_zone_attribute_groups[i];
622 result = create_trip_attrs(tz, mask);
629 groups[size - 2] = &tz->trips_attribute_group;
632 tz->device.groups = groups;
637 /* sys I/F for cooling device */
639 thermal_cooling_device_type_show(struct device *dev,
640 struct device_attribute *attr, char *buf)
642 struct thermal_cooling_device *cdev = to_cooling_device(dev);
644 return sprintf(buf, "%s\n", cdev->type);
648 thermal_cooling_device_max_state_show(struct device *dev,
649 struct device_attribute *attr, char *buf)
651 struct thermal_cooling_device *cdev = to_cooling_device(dev);
655 ret = cdev->ops->get_max_state(cdev, &state);
658 return sprintf(buf, "%ld\n", state);
662 thermal_cooling_device_cur_state_show(struct device *dev,
663 struct device_attribute *attr, char *buf)
665 struct thermal_cooling_device *cdev = to_cooling_device(dev);
669 ret = cdev->ops->get_cur_state(cdev, &state);
672 return sprintf(buf, "%ld\n", state);
676 thermal_cooling_device_cur_state_store(struct device *dev,
677 struct device_attribute *attr,
678 const char *buf, size_t count)
680 struct thermal_cooling_device *cdev = to_cooling_device(dev);
684 if (sscanf(buf, "%ld\n", &state) != 1)
690 result = cdev->ops->set_cur_state(cdev, state);
696 static struct device_attribute dev_attr_cdev_type =
697 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
698 static DEVICE_ATTR(max_state, 0444,
699 thermal_cooling_device_max_state_show, NULL);
700 static DEVICE_ATTR(cur_state, 0644,
701 thermal_cooling_device_cur_state_show,
702 thermal_cooling_device_cur_state_store);
704 static struct attribute *cooling_device_attrs[] = {
705 &dev_attr_cdev_type.attr,
706 &dev_attr_max_state.attr,
707 &dev_attr_cur_state.attr,
711 static const struct attribute_group cooling_device_attr_group = {
712 .attrs = cooling_device_attrs,
715 static const struct attribute_group *cooling_device_attr_groups[] = {
716 &cooling_device_attr_group,
720 void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *cdev)
722 cdev->device.groups = cooling_device_attr_groups;
725 /* these helper will be used only at the time of bindig */
727 thermal_cooling_device_trip_point_show(struct device *dev,
728 struct device_attribute *attr, char *buf)
730 struct thermal_instance *instance;
733 container_of(attr, struct thermal_instance, attr);
735 if (instance->trip == THERMAL_TRIPS_NONE)
736 return sprintf(buf, "-1\n");
738 return sprintf(buf, "%d\n", instance->trip);
742 thermal_cooling_device_weight_show(struct device *dev,
743 struct device_attribute *attr, char *buf)
745 struct thermal_instance *instance;
747 instance = container_of(attr, struct thermal_instance, weight_attr);
749 return sprintf(buf, "%d\n", instance->weight);
753 thermal_cooling_device_weight_store(struct device *dev,
754 struct device_attribute *attr,
755 const char *buf, size_t count)
757 struct thermal_instance *instance;
760 ret = kstrtoint(buf, 0, &weight);
764 instance = container_of(attr, struct thermal_instance, weight_attr);
765 instance->weight = weight;