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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Power capping class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (c) 2013, Intel Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/powercap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #define to_powercap_zone(n) container_of(n, struct powercap_zone, dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #define to_powercap_control_type(n) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 			container_of(n, struct powercap_control_type, dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) /* Power zone show function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define define_power_zone_show(_attr)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static ssize_t _attr##_show(struct device *dev, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 					struct device_attribute *dev_attr,\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 					char *buf) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	u64 value; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	ssize_t len = -EINVAL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct powercap_zone *power_zone = to_powercap_zone(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	if (power_zone->ops->get_##_attr) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		if (!power_zone->ops->get_##_attr(power_zone, &value)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 			len = sprintf(buf, "%lld\n", value); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	} \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	return len; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) /* The only meaningful input is 0 (reset), others are silently ignored */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define define_power_zone_store(_attr)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static ssize_t _attr##_store(struct device *dev,\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 				struct device_attribute *dev_attr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 				const char *buf, size_t count) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	int err; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct powercap_zone *power_zone = to_powercap_zone(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	u64 value; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	err = kstrtoull(buf, 10, &value); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	if (err) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		return -EINVAL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	if (value) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		return count; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	if (power_zone->ops->reset_##_attr) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		if (!power_zone->ops->reset_##_attr(power_zone)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			return count; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	} \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	return -EINVAL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) /* Power zone constraint show function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #define define_power_zone_constraint_show(_attr) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static ssize_t show_constraint_##_attr(struct device *dev, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 				struct device_attribute *dev_attr,\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 				char *buf) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	u64 value; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	ssize_t len = -ENODATA; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct powercap_zone *power_zone = to_powercap_zone(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	int id; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct powercap_zone_constraint *pconst;\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		return -EINVAL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (id >= power_zone->const_id_cnt)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		return -EINVAL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	pconst = &power_zone->constraints[id]; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	if (pconst && pconst->ops && pconst->ops->get_##_attr) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		if (!pconst->ops->get_##_attr(power_zone, id, &value)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			len = sprintf(buf, "%lld\n", value); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	} \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	return len; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) /* Power zone constraint store function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) #define define_power_zone_constraint_store(_attr) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static ssize_t store_constraint_##_attr(struct device *dev,\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 				struct device_attribute *dev_attr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 				const char *buf, size_t count) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	int err; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	u64 value; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	struct powercap_zone *power_zone = to_powercap_zone(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	int id; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	struct powercap_zone_constraint *pconst;\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		return -EINVAL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	if (id >= power_zone->const_id_cnt)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		return -EINVAL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	pconst = &power_zone->constraints[id]; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	err = kstrtoull(buf, 10, &value); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (err) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		return -EINVAL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (pconst && pconst->ops && pconst->ops->set_##_attr) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		if (!pconst->ops->set_##_attr(power_zone, id, value)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			return count; \
^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) 	return -ENODATA; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /* Power zone information callbacks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) define_power_zone_show(power_uw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) define_power_zone_show(max_power_range_uw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) define_power_zone_show(energy_uj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) define_power_zone_store(energy_uj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) define_power_zone_show(max_energy_range_uj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /* Power zone attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static DEVICE_ATTR_RO(max_power_range_uw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static DEVICE_ATTR_RO(power_uw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static DEVICE_ATTR_RO(max_energy_range_uj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static DEVICE_ATTR_RW(energy_uj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /* Power zone constraint attributes callbacks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) define_power_zone_constraint_show(power_limit_uw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) define_power_zone_constraint_store(power_limit_uw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) define_power_zone_constraint_show(time_window_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) define_power_zone_constraint_store(time_window_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) define_power_zone_constraint_show(max_power_uw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) define_power_zone_constraint_show(min_power_uw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) define_power_zone_constraint_show(max_time_window_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) define_power_zone_constraint_show(min_time_window_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /* For one time seeding of constraint device attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct powercap_constraint_attr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	struct device_attribute power_limit_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	struct device_attribute time_window_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	struct device_attribute max_power_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	struct device_attribute min_power_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	struct device_attribute max_time_window_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	struct device_attribute min_time_window_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	struct device_attribute name_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static struct powercap_constraint_attr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 				constraint_attrs[MAX_CONSTRAINTS_PER_ZONE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /* A list of powercap control_types */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static LIST_HEAD(powercap_cntrl_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /* Mutex to protect list of powercap control_types */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static DEFINE_MUTEX(powercap_cntrl_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #define POWERCAP_CONSTRAINT_NAME_LEN	30 /* Some limit to avoid overflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static ssize_t show_constraint_name(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 				struct device_attribute *dev_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 				char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	struct powercap_zone *power_zone = to_powercap_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	ssize_t len = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	struct powercap_zone_constraint *pconst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (id >= power_zone->const_id_cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	pconst = &power_zone->constraints[id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	if (pconst && pconst->ops && pconst->ops->get_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		name = pconst->ops->get_name(power_zone, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		if (name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			snprintf(buf, POWERCAP_CONSTRAINT_NAME_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 								"%s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 			buf[POWERCAP_CONSTRAINT_NAME_LEN] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			len = strlen(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static int create_constraint_attribute(int id, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 				int mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 				struct device_attribute *dev_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 				ssize_t (*show)(struct device *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 					struct device_attribute *, char *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 				ssize_t (*store)(struct device *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 					struct device_attribute *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 				const char *, size_t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 				)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	dev_attr->attr.name = kasprintf(GFP_KERNEL, "constraint_%d_%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 								id, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if (!dev_attr->attr.name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	dev_attr->attr.mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	dev_attr->show = show;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	dev_attr->store = store;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static void free_constraint_attributes(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	for (i = 0; i < MAX_CONSTRAINTS_PER_ZONE; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		kfree(constraint_attrs[i].power_limit_attr.attr.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		kfree(constraint_attrs[i].time_window_attr.attr.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		kfree(constraint_attrs[i].name_attr.attr.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		kfree(constraint_attrs[i].max_power_attr.attr.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		kfree(constraint_attrs[i].min_power_attr.attr.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		kfree(constraint_attrs[i].max_time_window_attr.attr.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		kfree(constraint_attrs[i].min_time_window_attr.attr.name);
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static int seed_constraint_attributes(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	for (i = 0; i < MAX_CONSTRAINTS_PER_ZONE; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		ret = create_constraint_attribute(i, "power_limit_uw",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 					S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 					&constraint_attrs[i].power_limit_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 					show_constraint_power_limit_uw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 					store_constraint_power_limit_uw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		ret = create_constraint_attribute(i, "time_window_us",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 					S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 					&constraint_attrs[i].time_window_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 					show_constraint_time_window_us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 					store_constraint_time_window_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		ret = create_constraint_attribute(i, "name", S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 				&constraint_attrs[i].name_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 				show_constraint_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 				NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		ret = create_constraint_attribute(i, "max_power_uw", S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 				&constraint_attrs[i].max_power_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 				show_constraint_max_power_uw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 				NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		ret = create_constraint_attribute(i, "min_power_uw", S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 				&constraint_attrs[i].min_power_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 				show_constraint_min_power_uw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 				NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		ret = create_constraint_attribute(i, "max_time_window_us",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 				S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 				&constraint_attrs[i].max_time_window_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 				show_constraint_max_time_window_us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 				NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		ret = create_constraint_attribute(i, "min_time_window_us",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 				S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 				&constraint_attrs[i].min_time_window_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 				show_constraint_min_time_window_us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 				NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 			goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) err_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	free_constraint_attributes();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static int create_constraints(struct powercap_zone *power_zone,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			int nr_constraints,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			const struct powercap_zone_constraint_ops *const_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	struct powercap_zone_constraint *pconst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	if (!power_zone || !const_ops || !const_ops->get_power_limit_uw ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 					!const_ops->set_power_limit_uw ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 					!const_ops->get_time_window_us ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 					!const_ops->set_time_window_us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	count = power_zone->zone_attr_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	for (i = 0; i < nr_constraints; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		pconst = &power_zone->constraints[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		pconst->ops = const_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		pconst->id = power_zone->const_id_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		power_zone->const_id_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		power_zone->zone_dev_attrs[count++] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 				&constraint_attrs[i].power_limit_attr.attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		power_zone->zone_dev_attrs[count++] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 				&constraint_attrs[i].time_window_attr.attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		if (pconst->ops->get_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			power_zone->zone_dev_attrs[count++] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 				&constraint_attrs[i].name_attr.attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		if (pconst->ops->get_max_power_uw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			power_zone->zone_dev_attrs[count++] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 				&constraint_attrs[i].max_power_attr.attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		if (pconst->ops->get_min_power_uw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			power_zone->zone_dev_attrs[count++] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 				&constraint_attrs[i].min_power_attr.attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		if (pconst->ops->get_max_time_window_us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			power_zone->zone_dev_attrs[count++] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 				&constraint_attrs[i].max_time_window_attr.attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		if (pconst->ops->get_min_time_window_us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 			power_zone->zone_dev_attrs[count++] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 				&constraint_attrs[i].min_time_window_attr.attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	power_zone->zone_attr_count = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static bool control_type_valid(void *control_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	struct powercap_control_type *pos = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	bool found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	mutex_lock(&powercap_cntrl_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	list_for_each_entry(pos, &powercap_cntrl_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		if (pos == control_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	mutex_unlock(&powercap_cntrl_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	return found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) static ssize_t name_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 				struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 				char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	struct powercap_zone *power_zone = to_powercap_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	return sprintf(buf, "%s\n", power_zone->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static DEVICE_ATTR_RO(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) /* Create zone and attributes in sysfs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) static void create_power_zone_common_attributes(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 					struct powercap_zone *power_zone)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	power_zone->zone_dev_attrs[count++] = &dev_attr_name.attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	if (power_zone->ops->get_max_energy_range_uj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		power_zone->zone_dev_attrs[count++] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 					&dev_attr_max_energy_range_uj.attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	if (power_zone->ops->get_energy_uj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		if (power_zone->ops->reset_energy_uj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 			dev_attr_energy_uj.attr.mode = S_IWUSR | S_IRUSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			dev_attr_energy_uj.attr.mode = S_IRUSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		power_zone->zone_dev_attrs[count++] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 					&dev_attr_energy_uj.attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	if (power_zone->ops->get_power_uw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		power_zone->zone_dev_attrs[count++] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 					&dev_attr_power_uw.attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	if (power_zone->ops->get_max_power_range_uw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		power_zone->zone_dev_attrs[count++] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 					&dev_attr_max_power_range_uw.attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	power_zone->zone_dev_attrs[count] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	power_zone->zone_attr_count = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) static void powercap_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	bool allocated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	if (dev->parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		struct powercap_zone *power_zone = to_powercap_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		/* Store flag as the release() may free memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		allocated = power_zone->allocated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		/* Remove id from parent idr struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		idr_remove(power_zone->parent_idr, power_zone->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		/* Destroy idrs allocated for this zone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		idr_destroy(&power_zone->idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		kfree(power_zone->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		kfree(power_zone->zone_dev_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		kfree(power_zone->constraints);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		if (power_zone->ops->release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			power_zone->ops->release(power_zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		if (allocated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 			kfree(power_zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		struct powercap_control_type *control_type =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 						to_powercap_control_type(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		/* Store flag as the release() may free memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		allocated = control_type->allocated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		idr_destroy(&control_type->idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		mutex_destroy(&control_type->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		if (control_type->ops && control_type->ops->release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 			control_type->ops->release(control_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		if (allocated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 			kfree(control_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) static ssize_t enabled_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 				struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 				char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	bool mode = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	/* Default is enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	if (dev->parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		struct powercap_zone *power_zone = to_powercap_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		if (power_zone->ops->get_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 			if (power_zone->ops->get_enable(power_zone, &mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 				mode = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		struct powercap_control_type *control_type =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 						to_powercap_control_type(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		if (control_type->ops && control_type->ops->get_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 			if (control_type->ops->get_enable(control_type, &mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 				mode = false;
^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) 	return sprintf(buf, "%d\n", mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) static ssize_t enabled_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 				struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 				const char *buf,  size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	bool mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	if (strtobool(buf, &mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	if (dev->parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		struct powercap_zone *power_zone = to_powercap_zone(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		if (power_zone->ops->set_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 			if (!power_zone->ops->set_enable(power_zone, mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 				return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		struct powercap_control_type *control_type =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 						to_powercap_control_type(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		if (control_type->ops && control_type->ops->set_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 			if (!control_type->ops->set_enable(control_type, mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 				return len;
^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) 	return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) static DEVICE_ATTR_RW(enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) static struct attribute *powercap_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	&dev_attr_enabled.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) ATTRIBUTE_GROUPS(powercap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) static struct class powercap_class = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	.name = "powercap",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	.dev_release = powercap_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	.dev_groups = powercap_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) struct powercap_zone *powercap_register_zone(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 			struct powercap_zone *power_zone,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 			struct powercap_control_type *control_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 			const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 			struct powercap_zone *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 			const struct powercap_zone_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 			int nr_constraints,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 			const struct powercap_zone_constraint_ops *const_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	int nr_attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	if (!name || !control_type || !ops ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 			nr_constraints > MAX_CONSTRAINTS_PER_ZONE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 			(!ops->get_energy_uj && !ops->get_power_uw) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 			!control_type_valid(control_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	if (power_zone) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		if (!ops->release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 			return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		memset(power_zone, 0, sizeof(*power_zone));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		power_zone = kzalloc(sizeof(*power_zone), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		if (!power_zone)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 			return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		power_zone->allocated = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	power_zone->ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	power_zone->control_type_inst = control_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	if (!parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		power_zone->dev.parent = &control_type->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		power_zone->parent_idr = &control_type->idr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		power_zone->dev.parent = &parent->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		power_zone->parent_idr = &parent->idr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	power_zone->dev.class = &powercap_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	mutex_lock(&control_type->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	/* Using idr to get the unique id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	result = idr_alloc(power_zone->parent_idr, NULL, 0, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	if (result < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		goto err_idr_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	power_zone->id = result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	idr_init(&power_zone->idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	result = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	power_zone->name = kstrdup(name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	if (!power_zone->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		goto err_name_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	dev_set_name(&power_zone->dev, "%s:%x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 					dev_name(power_zone->dev.parent),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 					power_zone->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	power_zone->constraints = kcalloc(nr_constraints,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 					  sizeof(*power_zone->constraints),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 					  GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	if (!power_zone->constraints)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		goto err_const_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	nr_attrs = nr_constraints * POWERCAP_CONSTRAINTS_ATTRS +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 						POWERCAP_ZONE_MAX_ATTRS + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	power_zone->zone_dev_attrs = kcalloc(nr_attrs, sizeof(void *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 					     GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	if (!power_zone->zone_dev_attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		goto err_attr_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	create_power_zone_common_attributes(power_zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	result = create_constraints(power_zone, nr_constraints, const_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		goto err_dev_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	power_zone->zone_dev_attrs[power_zone->zone_attr_count] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	power_zone->dev_zone_attr_group.attrs = power_zone->zone_dev_attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	power_zone->dev_attr_groups[0] = &power_zone->dev_zone_attr_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	power_zone->dev_attr_groups[1] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	power_zone->dev.groups = power_zone->dev_attr_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	result = device_register(&power_zone->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		goto err_dev_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	control_type->nr_zones++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	mutex_unlock(&control_type->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	return power_zone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) err_dev_ret:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	kfree(power_zone->zone_dev_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) err_attr_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	kfree(power_zone->constraints);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) err_const_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	kfree(power_zone->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) err_name_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	idr_remove(power_zone->parent_idr, power_zone->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) err_idr_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	if (power_zone->allocated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		kfree(power_zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	mutex_unlock(&control_type->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	return ERR_PTR(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) EXPORT_SYMBOL_GPL(powercap_register_zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) int powercap_unregister_zone(struct powercap_control_type *control_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 				struct powercap_zone *power_zone)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	if (!power_zone || !control_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	mutex_lock(&control_type->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	control_type->nr_zones--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	mutex_unlock(&control_type->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	device_unregister(&power_zone->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) EXPORT_SYMBOL_GPL(powercap_unregister_zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) struct powercap_control_type *powercap_register_control_type(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 				struct powercap_control_type *control_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 				const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 				const struct powercap_control_type_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	if (control_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 		if (!ops || !ops->release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 			return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 		memset(control_type, 0, sizeof(*control_type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 		control_type = kzalloc(sizeof(*control_type), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 		if (!control_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 			return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 		control_type->allocated = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	mutex_init(&control_type->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	control_type->ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	INIT_LIST_HEAD(&control_type->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	control_type->dev.class = &powercap_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	dev_set_name(&control_type->dev, "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	result = device_register(&control_type->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	if (result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 		if (control_type->allocated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 			kfree(control_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 		return ERR_PTR(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	idr_init(&control_type->idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	mutex_lock(&powercap_cntrl_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	list_add_tail(&control_type->node, &powercap_cntrl_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	mutex_unlock(&powercap_cntrl_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	return control_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) EXPORT_SYMBOL_GPL(powercap_register_control_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) int powercap_unregister_control_type(struct powercap_control_type *control_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	struct powercap_control_type *pos = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	if (control_type->nr_zones) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 		dev_err(&control_type->dev, "Zones of this type still not freed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	mutex_lock(&powercap_cntrl_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	list_for_each_entry(pos, &powercap_cntrl_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 		if (pos == control_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 			list_del(&control_type->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 			mutex_unlock(&powercap_cntrl_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 			device_unregister(&control_type->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	mutex_unlock(&powercap_cntrl_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) EXPORT_SYMBOL_GPL(powercap_unregister_control_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) static int __init powercap_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	result = seed_constraint_attributes();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 		return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	return class_register(&powercap_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) fs_initcall(powercap_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) MODULE_DESCRIPTION("PowerCap sysfs Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) MODULE_LICENSE("GPL v2");