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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Driver for Linear Technology LTC4260 I2C Positive Voltage Hot Swap Controller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2014 Guenter Roeck
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) /* chip registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define LTC4260_CONTROL	0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define LTC4260_ALERT	0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define LTC4260_STATUS	0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define LTC4260_FAULT	0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define LTC4260_SENSE	0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define LTC4260_SOURCE	0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define LTC4260_ADIN	0x06
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * Fault register bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define FAULT_OV	(1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define FAULT_UV	(1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define FAULT_OC	(1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define FAULT_POWER_BAD	(1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define FAULT_FET_SHORT	(1 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) /* Return the voltage from the given register in mV or mA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static int ltc4260_get_value(struct device *dev, u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct regmap *regmap = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	ret = regmap_read(regmap, reg, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	switch (reg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	case LTC4260_ADIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		/* 10 mV resolution. Convert to mV. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		val = val * 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	case LTC4260_SOURCE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		/* 400 mV resolution. Convert to mV. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		val = val * 400;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	case LTC4260_SENSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		 * 300 uV resolution. Convert to current as measured with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		 * an 1 mOhm sense resistor, in mA. If a different sense
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		 * resistor is installed, calculate the actual current by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		 * dividing the reported current by the sense resistor value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		 * in mOhm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		val = val * 300;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static ssize_t ltc4260_value_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 				  struct device_attribute *da, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	value = ltc4260_get_value(dev, attr->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	if (value < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	return snprintf(buf, PAGE_SIZE, "%d\n", value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static ssize_t ltc4260_bool_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 				 struct device_attribute *da, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	struct regmap *regmap = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	unsigned int fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	ret = regmap_read(regmap, LTC4260_FAULT, &fault);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	fault &= attr->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (fault)		/* Clear reported faults in chip register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		regmap_update_bits(regmap, LTC4260_FAULT, attr->index, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	return snprintf(buf, PAGE_SIZE, "%d\n", !!fault);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /* Voltages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static SENSOR_DEVICE_ATTR_RO(in1_input, ltc4260_value, LTC4260_SOURCE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static SENSOR_DEVICE_ATTR_RO(in2_input, ltc4260_value, LTC4260_ADIN);
^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)  * Voltage alarms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)  * UV/OV faults are associated with the input voltage, and the POWER BAD and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)  * FET SHORT faults are associated with the output voltage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static SENSOR_DEVICE_ATTR_RO(in1_min_alarm, ltc4260_bool, FAULT_UV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static SENSOR_DEVICE_ATTR_RO(in1_max_alarm, ltc4260_bool, FAULT_OV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static SENSOR_DEVICE_ATTR_RO(in2_alarm, ltc4260_bool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			     FAULT_POWER_BAD | FAULT_FET_SHORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /* Current (via sense resistor) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc4260_value, LTC4260_SENSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /* Overcurrent alarm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static SENSOR_DEVICE_ATTR_RO(curr1_max_alarm, ltc4260_bool, FAULT_OC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static struct attribute *ltc4260_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	&sensor_dev_attr_in1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	&sensor_dev_attr_in1_min_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	&sensor_dev_attr_in1_max_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	&sensor_dev_attr_in2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	&sensor_dev_attr_curr1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	&sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) ATTRIBUTE_GROUPS(ltc4260);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static const struct regmap_config ltc4260_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	.reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	.val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	.max_register = LTC4260_ADIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static int ltc4260_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	regmap = devm_regmap_init_i2c(client, &ltc4260_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (IS_ERR(regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		dev_err(dev, "failed to allocate register map\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		return PTR_ERR(regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	/* Clear faults */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	regmap_write(regmap, LTC4260_FAULT, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 							   regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 							   ltc4260_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	return PTR_ERR_OR_ZERO(hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static const struct i2c_device_id ltc4260_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	{"ltc4260", 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	{ }
^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) MODULE_DEVICE_TABLE(i2c, ltc4260_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static struct i2c_driver ltc4260_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		   .name = "ltc4260",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		   },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	.probe_new = ltc4260_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	.id_table = ltc4260_id,
^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) module_i2c_driver(ltc4260_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) MODULE_DESCRIPTION("LTC4260 driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) MODULE_LICENSE("GPL");