18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Driver for Linear Technology LTC4222 Dual Hot Swap controller
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2014 Guenter Roeck
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci#include <linux/err.h>
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci#include <linux/bitops.h>
138c2ecf20Sopenharmony_ci#include <linux/i2c.h>
148c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
158c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
168c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
178c2ecf20Sopenharmony_ci#include <linux/regmap.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/* chip registers */
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define LTC4222_CONTROL1	0xd0
228c2ecf20Sopenharmony_ci#define LTC4222_ALERT1		0xd1
238c2ecf20Sopenharmony_ci#define LTC4222_STATUS1		0xd2
248c2ecf20Sopenharmony_ci#define LTC4222_FAULT1		0xd3
258c2ecf20Sopenharmony_ci#define LTC4222_CONTROL2	0xd4
268c2ecf20Sopenharmony_ci#define LTC4222_ALERT2		0xd5
278c2ecf20Sopenharmony_ci#define LTC4222_STATUS2		0xd6
288c2ecf20Sopenharmony_ci#define LTC4222_FAULT2		0xd7
298c2ecf20Sopenharmony_ci#define LTC4222_SOURCE1		0xd8
308c2ecf20Sopenharmony_ci#define LTC4222_SOURCE2		0xda
318c2ecf20Sopenharmony_ci#define LTC4222_ADIN1		0xdc
328c2ecf20Sopenharmony_ci#define LTC4222_ADIN2		0xde
338c2ecf20Sopenharmony_ci#define LTC4222_SENSE1		0xe0
348c2ecf20Sopenharmony_ci#define LTC4222_SENSE2		0xe2
358c2ecf20Sopenharmony_ci#define LTC4222_ADC_CONTROL	0xe4
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/*
388c2ecf20Sopenharmony_ci * Fault register bits
398c2ecf20Sopenharmony_ci */
408c2ecf20Sopenharmony_ci#define FAULT_OV	BIT(0)
418c2ecf20Sopenharmony_ci#define FAULT_UV	BIT(1)
428c2ecf20Sopenharmony_ci#define FAULT_OC	BIT(2)
438c2ecf20Sopenharmony_ci#define FAULT_POWER_BAD	BIT(3)
448c2ecf20Sopenharmony_ci#define FAULT_FET_BAD	BIT(5)
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/* Return the voltage from the given register in mV or mA */
478c2ecf20Sopenharmony_cistatic int ltc4222_get_value(struct device *dev, u8 reg)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	struct regmap *regmap = dev_get_drvdata(dev);
508c2ecf20Sopenharmony_ci	unsigned int val;
518c2ecf20Sopenharmony_ci	u8 buf[2];
528c2ecf20Sopenharmony_ci	int ret;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	ret = regmap_bulk_read(regmap, reg, buf, 2);
558c2ecf20Sopenharmony_ci	if (ret < 0)
568c2ecf20Sopenharmony_ci		return ret;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	val = ((buf[0] << 8) + buf[1]) >> 6;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	switch (reg) {
618c2ecf20Sopenharmony_ci	case LTC4222_ADIN1:
628c2ecf20Sopenharmony_ci	case LTC4222_ADIN2:
638c2ecf20Sopenharmony_ci		/* 1.25 mV resolution. Convert to mV. */
648c2ecf20Sopenharmony_ci		val = DIV_ROUND_CLOSEST(val * 5, 4);
658c2ecf20Sopenharmony_ci		break;
668c2ecf20Sopenharmony_ci	case LTC4222_SOURCE1:
678c2ecf20Sopenharmony_ci	case LTC4222_SOURCE2:
688c2ecf20Sopenharmony_ci		/* 31.25 mV resolution. Convert to mV. */
698c2ecf20Sopenharmony_ci		val = DIV_ROUND_CLOSEST(val * 125, 4);
708c2ecf20Sopenharmony_ci		break;
718c2ecf20Sopenharmony_ci	case LTC4222_SENSE1:
728c2ecf20Sopenharmony_ci	case LTC4222_SENSE2:
738c2ecf20Sopenharmony_ci		/*
748c2ecf20Sopenharmony_ci		 * 62.5 uV resolution. Convert to current as measured with
758c2ecf20Sopenharmony_ci		 * an 1 mOhm sense resistor, in mA. If a different sense
768c2ecf20Sopenharmony_ci		 * resistor is installed, calculate the actual current by
778c2ecf20Sopenharmony_ci		 * dividing the reported current by the sense resistor value
788c2ecf20Sopenharmony_ci		 * in mOhm.
798c2ecf20Sopenharmony_ci		 */
808c2ecf20Sopenharmony_ci		val = DIV_ROUND_CLOSEST(val * 125, 2);
818c2ecf20Sopenharmony_ci		break;
828c2ecf20Sopenharmony_ci	default:
838c2ecf20Sopenharmony_ci		return -EINVAL;
848c2ecf20Sopenharmony_ci	}
858c2ecf20Sopenharmony_ci	return val;
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic ssize_t ltc4222_value_show(struct device *dev,
898c2ecf20Sopenharmony_ci				  struct device_attribute *da, char *buf)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
928c2ecf20Sopenharmony_ci	int value;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	value = ltc4222_get_value(dev, attr->index);
958c2ecf20Sopenharmony_ci	if (value < 0)
968c2ecf20Sopenharmony_ci		return value;
978c2ecf20Sopenharmony_ci	return snprintf(buf, PAGE_SIZE, "%d\n", value);
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic ssize_t ltc4222_bool_show(struct device *dev,
1018c2ecf20Sopenharmony_ci				 struct device_attribute *da, char *buf)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
1048c2ecf20Sopenharmony_ci	struct regmap *regmap = dev_get_drvdata(dev);
1058c2ecf20Sopenharmony_ci	unsigned int fault;
1068c2ecf20Sopenharmony_ci	int ret;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	ret = regmap_read(regmap, attr->nr, &fault);
1098c2ecf20Sopenharmony_ci	if (ret < 0)
1108c2ecf20Sopenharmony_ci		return ret;
1118c2ecf20Sopenharmony_ci	fault &= attr->index;
1128c2ecf20Sopenharmony_ci	if (fault)		/* Clear reported faults in chip register */
1138c2ecf20Sopenharmony_ci		regmap_update_bits(regmap, attr->nr, attr->index, 0);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	return snprintf(buf, PAGE_SIZE, "%d\n", !!fault);
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci/* Voltages */
1198c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in1_input, ltc4222_value, LTC4222_SOURCE1);
1208c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in2_input, ltc4222_value, LTC4222_ADIN1);
1218c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in3_input, ltc4222_value, LTC4222_SOURCE2);
1228c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in4_input, ltc4222_value, LTC4222_ADIN2);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci/*
1258c2ecf20Sopenharmony_ci * Voltage alarms
1268c2ecf20Sopenharmony_ci * UV/OV faults are associated with the input voltage, and power bad and fet
1278c2ecf20Sopenharmony_ci * faults are associated with the output voltage.
1288c2ecf20Sopenharmony_ci */
1298c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in1_min_alarm, ltc4222_bool, LTC4222_FAULT1,
1308c2ecf20Sopenharmony_ci			       FAULT_UV);
1318c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in1_max_alarm, ltc4222_bool, LTC4222_FAULT1,
1328c2ecf20Sopenharmony_ci			       FAULT_OV);
1338c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in2_alarm, ltc4222_bool, LTC4222_FAULT1,
1348c2ecf20Sopenharmony_ci			       FAULT_POWER_BAD | FAULT_FET_BAD);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in3_min_alarm, ltc4222_bool, LTC4222_FAULT2,
1378c2ecf20Sopenharmony_ci			       FAULT_UV);
1388c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in3_max_alarm, ltc4222_bool, LTC4222_FAULT2,
1398c2ecf20Sopenharmony_ci			       FAULT_OV);
1408c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in4_alarm, ltc4222_bool, LTC4222_FAULT2,
1418c2ecf20Sopenharmony_ci			       FAULT_POWER_BAD | FAULT_FET_BAD);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci/* Current (via sense resistor) */
1448c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(curr1_input, ltc4222_value, LTC4222_SENSE1);
1458c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(curr2_input, ltc4222_value, LTC4222_SENSE2);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci/* Overcurrent alarm */
1488c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(curr1_max_alarm, ltc4222_bool, LTC4222_FAULT1,
1498c2ecf20Sopenharmony_ci			       FAULT_OC);
1508c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(curr2_max_alarm, ltc4222_bool, LTC4222_FAULT2,
1518c2ecf20Sopenharmony_ci			       FAULT_OC);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic struct attribute *ltc4222_attrs[] = {
1548c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_input.dev_attr.attr,
1558c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_min_alarm.dev_attr.attr,
1568c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_max_alarm.dev_attr.attr,
1578c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_input.dev_attr.attr,
1588c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_alarm.dev_attr.attr,
1598c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_input.dev_attr.attr,
1608c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_min_alarm.dev_attr.attr,
1618c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_max_alarm.dev_attr.attr,
1628c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_input.dev_attr.attr,
1638c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_alarm.dev_attr.attr,
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	&sensor_dev_attr_curr1_input.dev_attr.attr,
1668c2ecf20Sopenharmony_ci	&sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
1678c2ecf20Sopenharmony_ci	&sensor_dev_attr_curr2_input.dev_attr.attr,
1688c2ecf20Sopenharmony_ci	&sensor_dev_attr_curr2_max_alarm.dev_attr.attr,
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	NULL,
1718c2ecf20Sopenharmony_ci};
1728c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(ltc4222);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistatic const struct regmap_config ltc4222_regmap_config = {
1758c2ecf20Sopenharmony_ci	.reg_bits = 8,
1768c2ecf20Sopenharmony_ci	.val_bits = 8,
1778c2ecf20Sopenharmony_ci	.max_register = LTC4222_ADC_CONTROL,
1788c2ecf20Sopenharmony_ci};
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistatic int ltc4222_probe(struct i2c_client *client)
1818c2ecf20Sopenharmony_ci{
1828c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
1838c2ecf20Sopenharmony_ci	struct device *hwmon_dev;
1848c2ecf20Sopenharmony_ci	struct regmap *regmap;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	regmap = devm_regmap_init_i2c(client, &ltc4222_regmap_config);
1878c2ecf20Sopenharmony_ci	if (IS_ERR(regmap)) {
1888c2ecf20Sopenharmony_ci		dev_err(dev, "failed to allocate register map\n");
1898c2ecf20Sopenharmony_ci		return PTR_ERR(regmap);
1908c2ecf20Sopenharmony_ci	}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	/* Clear faults */
1938c2ecf20Sopenharmony_ci	regmap_write(regmap, LTC4222_FAULT1, 0x00);
1948c2ecf20Sopenharmony_ci	regmap_write(regmap, LTC4222_FAULT2, 0x00);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1978c2ecf20Sopenharmony_ci							   regmap,
1988c2ecf20Sopenharmony_ci							   ltc4222_groups);
1998c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(hwmon_dev);
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic const struct i2c_device_id ltc4222_id[] = {
2038c2ecf20Sopenharmony_ci	{"ltc4222", 0},
2048c2ecf20Sopenharmony_ci	{ }
2058c2ecf20Sopenharmony_ci};
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, ltc4222_id);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cistatic struct i2c_driver ltc4222_driver = {
2108c2ecf20Sopenharmony_ci	.driver = {
2118c2ecf20Sopenharmony_ci		   .name = "ltc4222",
2128c2ecf20Sopenharmony_ci		   },
2138c2ecf20Sopenharmony_ci	.probe_new = ltc4222_probe,
2148c2ecf20Sopenharmony_ci	.id_table = ltc4222_id,
2158c2ecf20Sopenharmony_ci};
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_cimodule_i2c_driver(ltc4222_driver);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ciMODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
2208c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("LTC4222 driver");
2218c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
222