Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  thermal.c - sysfs interface of thermal devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  Copyright (C) 2016 Eduardo Valentin <edubezval@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *  Highly based on original thermal_core.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *  Copyright (C) 2008 Intel Corp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *  Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *  Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "thermal_core.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) /* sys I/F for thermal zone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) type_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct thermal_zone_device *tz = to_thermal_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	return sprintf(buf, "%s\n", tz->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) temp_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	struct thermal_zone_device *tz = to_thermal_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	int temperature, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	ret = thermal_zone_get_temp(tz, &temperature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	return sprintf(buf, "%d\n", temperature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) mode_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct thermal_zone_device *tz = to_thermal_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	int enabled = thermal_zone_device_is_enabled(tz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	return sprintf(buf, "%s\n", enabled ? "enabled" : "disabled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) mode_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	   const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	struct thermal_zone_device *tz = to_thermal_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		result = thermal_zone_device_enable(tz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		result = thermal_zone_device_disable(tz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		result = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) trip_point_type_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		     char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	struct thermal_zone_device *tz = to_thermal_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	enum thermal_trip_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	int trip, result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (!tz->ops->get_trip_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (sscanf(attr->attr.name, "trip_point_%d_type", &trip) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	result = tz->ops->get_trip_type(tz, trip, &type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	case THERMAL_TRIP_CRITICAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		return sprintf(buf, "critical\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	case THERMAL_TRIP_HOT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		return sprintf(buf, "hot\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	case THERMAL_TRIP_PASSIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		return sprintf(buf, "passive\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	case THERMAL_TRIP_ACTIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		return sprintf(buf, "active\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		return sprintf(buf, "unknown\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) trip_point_temp_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		      const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	struct thermal_zone_device *tz = to_thermal_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	int trip, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	int temperature, hyst = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	enum thermal_trip_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (!tz->ops->set_trip_temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (kstrtoint(buf, 10, &temperature))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	ret = tz->ops->set_trip_temp(tz, trip, temperature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (tz->ops->get_trip_hyst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		ret = tz->ops->get_trip_hyst(tz, trip, &hyst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	ret = tz->ops->get_trip_type(tz, trip, &type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	thermal_notify_tz_trip_change(tz->id, trip, type, temperature, hyst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) trip_point_temp_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		     char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	struct thermal_zone_device *tz = to_thermal_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	int trip, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	int temperature;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (!tz->ops->get_trip_temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	ret = tz->ops->get_trip_temp(tz, trip, &temperature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	return sprintf(buf, "%d\n", temperature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		      const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	struct thermal_zone_device *tz = to_thermal_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	int trip, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	int temperature;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	if (!tz->ops->set_trip_hyst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	if (kstrtoint(buf, 10, &temperature))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	 * We are not doing any check on the 'temperature' value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	 * here. The driver implementing 'set_trip_hyst' has to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	 * take care of this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	ret = tz->ops->set_trip_hyst(tz, trip, temperature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		thermal_zone_set_trips(tz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	return ret ? ret : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		     char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	struct thermal_zone_device *tz = to_thermal_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	int trip, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	int temperature;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (!tz->ops->get_trip_hyst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	return ret ? ret : sprintf(buf, "%d\n", temperature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) passive_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	      const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	struct thermal_zone_device *tz = to_thermal_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	int state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	if (sscanf(buf, "%d\n", &state) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	/* sanity check: values below 1000 millicelcius don't make sense
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	 * and can cause the system to go into a thermal heart attack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (state && state < 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (state && !tz->forced_passive) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		if (!tz->passive_delay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			tz->passive_delay = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		thermal_zone_device_rebind_exception(tz, "Processor",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 						     sizeof("Processor"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	} else if (!state && tz->forced_passive) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		tz->passive_delay = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		thermal_zone_device_unbind_exception(tz, "Processor",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 						     sizeof("Processor"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	tz->forced_passive = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) passive_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	     char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	struct thermal_zone_device *tz = to_thermal_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	return sprintf(buf, "%d\n", tz->forced_passive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) policy_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	     const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	struct thermal_zone_device *tz = to_thermal_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	char name[THERMAL_NAME_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	snprintf(name, sizeof(name), "%s", buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	ret = thermal_zone_device_set_policy(tz, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	struct thermal_zone_device *tz = to_thermal_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	return sprintf(buf, "%s\n", tz->governor->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) available_policies_show(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	return thermal_build_list_of_policies(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) emul_temp_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	struct thermal_zone_device *tz = to_thermal_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	int temperature;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	if (kstrtoint(buf, 10, &temperature))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	if (!tz->ops->set_emul_temp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		mutex_lock(&tz->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		tz->emul_temperature = temperature;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		mutex_unlock(&tz->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		ret = tz->ops->set_emul_temp(tz, temperature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	return ret ? ret : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static DEVICE_ATTR_WO(emul_temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) sustainable_power_show(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		       char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	struct thermal_zone_device *tz = to_thermal_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	if (tz->tzp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) sustainable_power_store(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	struct thermal_zone_device *tz = to_thermal_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	u32 sustainable_power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	if (!tz->tzp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	if (kstrtou32(buf, 10, &sustainable_power))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	tz->tzp->sustainable_power = sustainable_power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) #define create_s32_tzp_attr(name)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	static ssize_t							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	name##_show(struct device *dev, struct device_attribute *devattr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		char *buf)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	{								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	struct thermal_zone_device *tz = to_thermal_zone(dev);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	if (tz->tzp)							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		return sprintf(buf, "%d\n", tz->tzp->name);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	else								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		return -EIO;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	}								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	static ssize_t							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	name##_store(struct device *dev, struct device_attribute *devattr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		const char *buf, size_t count)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	{								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		struct thermal_zone_device *tz = to_thermal_zone(dev);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		s32 value;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		if (!tz->tzp)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 			return -EIO;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		if (kstrtos32(buf, 10, &value))				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 			return -EINVAL;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		tz->tzp->name = value;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		return count;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	}								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	static DEVICE_ATTR_RW(name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) create_s32_tzp_attr(k_po);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) create_s32_tzp_attr(k_pu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) create_s32_tzp_attr(k_i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) create_s32_tzp_attr(k_d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) create_s32_tzp_attr(integral_cutoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) create_s32_tzp_attr(slope);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) create_s32_tzp_attr(offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) #undef create_s32_tzp_attr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)  * These are thermal zone device attributes that will always be present.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)  * All the attributes created for tzp (create_s32_tzp_attr) also are always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)  * present on the sysfs interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) static DEVICE_ATTR_RO(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) static DEVICE_ATTR_RO(temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) static DEVICE_ATTR_RW(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) static DEVICE_ATTR_RO(available_policies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) static DEVICE_ATTR_RW(sustainable_power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) /* These thermal zone device attributes are created based on conditions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) static DEVICE_ATTR_RW(mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) static DEVICE_ATTR_RW(passive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) /* These attributes are unconditionally added to a thermal zone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) static struct attribute *thermal_zone_dev_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	&dev_attr_type.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	&dev_attr_temp.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	&dev_attr_emul_temp.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	&dev_attr_policy.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	&dev_attr_available_policies.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	&dev_attr_sustainable_power.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	&dev_attr_k_po.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	&dev_attr_k_pu.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	&dev_attr_k_i.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	&dev_attr_k_d.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	&dev_attr_integral_cutoff.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	&dev_attr_slope.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	&dev_attr_offset.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) static struct attribute_group thermal_zone_attribute_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	.attrs = thermal_zone_dev_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) static struct attribute *thermal_zone_mode_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	&dev_attr_mode.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) static struct attribute_group thermal_zone_mode_attribute_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	.attrs = thermal_zone_mode_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) /* We expose passive only if passive trips are present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) static struct attribute *thermal_zone_passive_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	&dev_attr_passive.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) static umode_t thermal_zone_passive_is_visible(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 					       struct attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 					       int attrno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	struct device *dev = kobj_to_dev(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	struct thermal_zone_device *tz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	enum thermal_trip_type trip_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	int count, passive = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	tz = container_of(dev, struct thermal_zone_device, device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	for (count = 0; count < tz->trips && !passive; count++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		tz->ops->get_trip_type(tz, count, &trip_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		if (trip_type == THERMAL_TRIP_PASSIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 			passive = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	if (!passive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		return attr->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) static struct attribute_group thermal_zone_passive_attribute_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	.attrs = thermal_zone_passive_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	.is_visible = thermal_zone_passive_is_visible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) static const struct attribute_group *thermal_zone_attribute_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	&thermal_zone_attribute_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	&thermal_zone_mode_attribute_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	&thermal_zone_passive_attribute_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	/* This is not NULL terminated as we create the group dynamically */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)  * create_trip_attrs() - create attributes for trip points
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)  * @tz:		the thermal zone device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)  * @mask:	Writeable trip point bitmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)  * helper function to instantiate sysfs entries for every trip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)  * point and its properties of a struct thermal_zone_device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)  * Return: 0 on success, the proper error value otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	struct attribute **attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	int indx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	/* This function works only for zones with at least one trip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	if (tz->trips <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	tz->trip_type_attrs = kcalloc(tz->trips, sizeof(*tz->trip_type_attrs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 				      GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	if (!tz->trip_type_attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	tz->trip_temp_attrs = kcalloc(tz->trips, sizeof(*tz->trip_temp_attrs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 				      GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	if (!tz->trip_temp_attrs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		kfree(tz->trip_type_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	if (tz->ops->get_trip_hyst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		tz->trip_hyst_attrs = kcalloc(tz->trips,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 					      sizeof(*tz->trip_hyst_attrs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 					      GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		if (!tz->trip_hyst_attrs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 			kfree(tz->trip_type_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 			kfree(tz->trip_temp_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	attrs = kcalloc(tz->trips * 3 + 1, sizeof(*attrs), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	if (!attrs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		kfree(tz->trip_type_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		kfree(tz->trip_temp_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		if (tz->ops->get_trip_hyst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 			kfree(tz->trip_hyst_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	for (indx = 0; indx < tz->trips; indx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		/* create trip type attribute */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 			 "trip_point_%d_type", indx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		tz->trip_type_attrs[indx].attr.attr.name =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 						tz->trip_type_attrs[indx].name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		attrs[indx] = &tz->trip_type_attrs[indx].attr.attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		/* create trip temp attribute */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 			 "trip_point_%d_temp", indx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 		sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		tz->trip_temp_attrs[indx].attr.attr.name =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 						tz->trip_temp_attrs[indx].name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 		    mask & (1 << indx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 			tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 			tz->trip_temp_attrs[indx].attr.store =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 							trip_point_temp_store;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		attrs[indx + tz->trips] = &tz->trip_temp_attrs[indx].attr.attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 		/* create Optional trip hyst attribute */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		if (!tz->ops->get_trip_hyst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 			 "trip_point_%d_hyst", indx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 		sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 		tz->trip_hyst_attrs[indx].attr.attr.name =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 					tz->trip_hyst_attrs[indx].name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 		if (tz->ops->set_trip_hyst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 			tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 			tz->trip_hyst_attrs[indx].attr.store =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 					trip_point_hyst_store;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		attrs[indx + tz->trips * 2] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 					&tz->trip_hyst_attrs[indx].attr.attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	attrs[tz->trips * 3] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	tz->trips_attribute_group.attrs = attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)  * destroy_trip_attrs() - destroy attributes for trip points
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)  * @tz:		the thermal zone device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)  * helper function to free resources allocated by create_trip_attrs()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) static void destroy_trip_attrs(struct thermal_zone_device *tz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	if (!tz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	kfree(tz->trip_type_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	kfree(tz->trip_temp_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	if (tz->ops->get_trip_hyst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 		kfree(tz->trip_hyst_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	kfree(tz->trips_attribute_group.attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) int thermal_zone_create_device_groups(struct thermal_zone_device *tz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 				      int mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	const struct attribute_group **groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	int i, size, result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	/* we need one extra for trips and the NULL to terminate the array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	size = ARRAY_SIZE(thermal_zone_attribute_groups) + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	/* This also takes care of API requirement to be NULL terminated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	groups = kcalloc(size, sizeof(*groups), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	if (!groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	for (i = 0; i < size - 2; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 		groups[i] = thermal_zone_attribute_groups[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	if (tz->trips) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 		result = create_trip_attrs(tz, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 		if (result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 			kfree(groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 			return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 		groups[size - 2] = &tz->trips_attribute_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	tz->device.groups = groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) void thermal_zone_destroy_device_groups(struct thermal_zone_device *tz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	if (!tz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	if (tz->trips)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 		destroy_trip_attrs(tz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	kfree(tz->device.groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) /* sys I/F for cooling device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) cdev_type_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	struct thermal_cooling_device *cdev = to_cooling_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	return sprintf(buf, "%s\n", cdev->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) static ssize_t max_state_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 			      char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	struct thermal_cooling_device *cdev = to_cooling_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	unsigned long state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	ret = cdev->ops->get_max_state(cdev, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	return sprintf(buf, "%ld\n", state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) static ssize_t cur_state_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 			      char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	struct thermal_cooling_device *cdev = to_cooling_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	unsigned long state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	ret = cdev->ops->get_cur_state(cdev, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	return sprintf(buf, "%ld\n", state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) cur_state_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 		const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	struct thermal_cooling_device *cdev = to_cooling_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	unsigned long state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	if (sscanf(buf, "%ld\n", &state) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	if ((long)state < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	mutex_lock(&cdev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	result = cdev->ops->set_cur_state(cdev, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	if (!result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 		thermal_cooling_device_stats_update(cdev, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	mutex_unlock(&cdev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	return result ? result : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) static struct device_attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) dev_attr_cdev_type = __ATTR(type, 0444, cdev_type_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) static DEVICE_ATTR_RO(max_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) static DEVICE_ATTR_RW(cur_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) static struct attribute *cooling_device_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	&dev_attr_cdev_type.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	&dev_attr_max_state.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	&dev_attr_cur_state.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) static const struct attribute_group cooling_device_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	.attrs = cooling_device_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) static const struct attribute_group *cooling_device_attr_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 	&cooling_device_attr_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	NULL, /* Space allocated for cooling_device_stats_attr_group */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) #ifdef CONFIG_THERMAL_STATISTICS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) struct cooling_dev_stats {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	unsigned int total_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	unsigned long state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	unsigned long max_states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	ktime_t last_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	ktime_t *time_in_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	unsigned int *trans_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) static void update_time_in_state(struct cooling_dev_stats *stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	ktime_t now = ktime_get(), delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	delta = ktime_sub(now, stats->last_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	stats->time_in_state[stats->state] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 		ktime_add(stats->time_in_state[stats->state], delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 	stats->last_time = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 					 unsigned long new_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	struct cooling_dev_stats *stats = cdev->stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	if (!stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	spin_lock(&stats->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 	if (stats->state == new_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 	update_time_in_state(stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	stats->trans_table[stats->state * stats->max_states + new_state]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 	stats->state = new_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 	stats->total_trans++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 	spin_unlock(&stats->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) static ssize_t total_trans_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 				struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 	struct thermal_cooling_device *cdev = to_cooling_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 	struct cooling_dev_stats *stats = cdev->stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 	spin_lock(&stats->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 	ret = sprintf(buf, "%u\n", stats->total_trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 	spin_unlock(&stats->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) time_in_state_ms_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 		      char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 	struct thermal_cooling_device *cdev = to_cooling_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 	struct cooling_dev_stats *stats = cdev->stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 	ssize_t len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 	spin_lock(&stats->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 	update_time_in_state(stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	for (i = 0; i < stats->max_states; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 		len += sprintf(buf + len, "state%u\t%llu\n", i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 			       ktime_to_ms(stats->time_in_state[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 	spin_unlock(&stats->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) reset_store(struct device *dev, struct device_attribute *attr, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 	    size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 	struct thermal_cooling_device *cdev = to_cooling_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 	struct cooling_dev_stats *stats = cdev->stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 	int i, states = stats->max_states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 	spin_lock(&stats->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 	stats->total_trans = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 	stats->last_time = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 	memset(stats->trans_table, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 	       states * states * sizeof(*stats->trans_table));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 	for (i = 0; i < stats->max_states; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 		stats->time_in_state[i] = ktime_set(0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 	spin_unlock(&stats->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) static ssize_t trans_table_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 				struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 	struct thermal_cooling_device *cdev = to_cooling_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 	struct cooling_dev_stats *stats = cdev->stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 	ssize_t len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 	int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 	len += snprintf(buf + len, PAGE_SIZE - len, " From  :    To\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 	len += snprintf(buf + len, PAGE_SIZE - len, "       : ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 	for (i = 0; i < stats->max_states; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 		if (len >= PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 		len += snprintf(buf + len, PAGE_SIZE - len, "state%2u  ", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 	if (len >= PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 		return PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 	len += snprintf(buf + len, PAGE_SIZE - len, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 	for (i = 0; i < stats->max_states; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 		if (len >= PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 		len += snprintf(buf + len, PAGE_SIZE - len, "state%2u:", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 		for (j = 0; j < stats->max_states; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 			if (len >= PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 			len += snprintf(buf + len, PAGE_SIZE - len, "%8u ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 				stats->trans_table[i * stats->max_states + j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) 		if (len >= PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 		len += snprintf(buf + len, PAGE_SIZE - len, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 	if (len >= PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 		pr_warn_once("Thermal transition table exceeds PAGE_SIZE. Disabling\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 		return -EFBIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) static DEVICE_ATTR_RO(total_trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) static DEVICE_ATTR_RO(time_in_state_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) static DEVICE_ATTR_WO(reset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) static DEVICE_ATTR_RO(trans_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) static struct attribute *cooling_device_stats_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 	&dev_attr_total_trans.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 	&dev_attr_time_in_state_ms.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 	&dev_attr_reset.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 	&dev_attr_trans_table.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) static const struct attribute_group cooling_device_stats_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) 	.attrs = cooling_device_stats_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) 	.name = "stats"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) static void cooling_device_stats_setup(struct thermal_cooling_device *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 	struct cooling_dev_stats *stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) 	unsigned long states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) 	int var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) 	if (cdev->ops->get_max_state(cdev, &states))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) 	states++; /* Total number of states is highest state + 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) 	var = sizeof(*stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) 	var += sizeof(*stats->time_in_state) * states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) 	var += sizeof(*stats->trans_table) * states * states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) 	stats = kzalloc(var, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) 	if (!stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 	stats->time_in_state = (ktime_t *)(stats + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) 	stats->trans_table = (unsigned int *)(stats->time_in_state + states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) 	cdev->stats = stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) 	stats->last_time = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) 	stats->max_states = states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) 	spin_lock_init(&stats->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) 	/* Fill the empty slot left in cooling_device_attr_groups */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) 	var = ARRAY_SIZE(cooling_device_attr_groups) - 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) 	cooling_device_attr_groups[var] = &cooling_device_stats_attr_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) static void cooling_device_stats_destroy(struct thermal_cooling_device *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) 	kfree(cdev->stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) 	cdev->stats = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) cooling_device_stats_setup(struct thermal_cooling_device *cdev) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) cooling_device_stats_destroy(struct thermal_cooling_device *cdev) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) #endif /* CONFIG_THERMAL_STATISTICS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) 	cooling_device_stats_setup(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) 	cdev->device.groups = cooling_device_attr_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) 	cooling_device_stats_destroy(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) /* these helper will be used only at the time of bindig */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) trip_point_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) 	struct thermal_instance *instance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) 	instance =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) 	    container_of(attr, struct thermal_instance, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) 	if (instance->trip == THERMAL_TRIPS_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) 		return sprintf(buf, "-1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) 		return sprintf(buf, "%d\n", instance->trip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) weight_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) 	struct thermal_instance *instance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) 	instance = container_of(attr, struct thermal_instance, weight_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) 	return sprintf(buf, "%d\n", instance->weight);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) ssize_t weight_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) 		     const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) 	struct thermal_instance *instance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) 	int ret, weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) 	ret = kstrtoint(buf, 0, &weight);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) 	instance = container_of(attr, struct thermal_instance, weight_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) 	instance->weight = weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) }