1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
279ffe859SGuenter Roeck /*
379ffe859SGuenter Roeck * Driver for Linear Technology LTC4222 Dual Hot Swap controller
479ffe859SGuenter Roeck *
579ffe859SGuenter Roeck * Copyright (c) 2014 Guenter Roeck
679ffe859SGuenter Roeck */
779ffe859SGuenter Roeck
879ffe859SGuenter Roeck #include <linux/kernel.h>
979ffe859SGuenter Roeck #include <linux/module.h>
1079ffe859SGuenter Roeck #include <linux/err.h>
1179ffe859SGuenter Roeck #include <linux/slab.h>
1279ffe859SGuenter Roeck #include <linux/bitops.h>
1379ffe859SGuenter Roeck #include <linux/i2c.h>
1479ffe859SGuenter Roeck #include <linux/hwmon.h>
1579ffe859SGuenter Roeck #include <linux/hwmon-sysfs.h>
1679ffe859SGuenter Roeck #include <linux/jiffies.h>
1779ffe859SGuenter Roeck #include <linux/regmap.h>
1879ffe859SGuenter Roeck
1979ffe859SGuenter Roeck /* chip registers */
2079ffe859SGuenter Roeck
2179ffe859SGuenter Roeck #define LTC4222_CONTROL1 0xd0
2279ffe859SGuenter Roeck #define LTC4222_ALERT1 0xd1
2379ffe859SGuenter Roeck #define LTC4222_STATUS1 0xd2
2479ffe859SGuenter Roeck #define LTC4222_FAULT1 0xd3
2579ffe859SGuenter Roeck #define LTC4222_CONTROL2 0xd4
2679ffe859SGuenter Roeck #define LTC4222_ALERT2 0xd5
2779ffe859SGuenter Roeck #define LTC4222_STATUS2 0xd6
2879ffe859SGuenter Roeck #define LTC4222_FAULT2 0xd7
2979ffe859SGuenter Roeck #define LTC4222_SOURCE1 0xd8
3079ffe859SGuenter Roeck #define LTC4222_SOURCE2 0xda
3179ffe859SGuenter Roeck #define LTC4222_ADIN1 0xdc
3279ffe859SGuenter Roeck #define LTC4222_ADIN2 0xde
3379ffe859SGuenter Roeck #define LTC4222_SENSE1 0xe0
3479ffe859SGuenter Roeck #define LTC4222_SENSE2 0xe2
3579ffe859SGuenter Roeck #define LTC4222_ADC_CONTROL 0xe4
3679ffe859SGuenter Roeck
3779ffe859SGuenter Roeck /*
3879ffe859SGuenter Roeck * Fault register bits
3979ffe859SGuenter Roeck */
4079ffe859SGuenter Roeck #define FAULT_OV BIT(0)
4179ffe859SGuenter Roeck #define FAULT_UV BIT(1)
4279ffe859SGuenter Roeck #define FAULT_OC BIT(2)
4379ffe859SGuenter Roeck #define FAULT_POWER_BAD BIT(3)
4479ffe859SGuenter Roeck #define FAULT_FET_BAD BIT(5)
4579ffe859SGuenter Roeck
4679ffe859SGuenter Roeck /* Return the voltage from the given register in mV or mA */
ltc4222_get_value(struct device * dev,u8 reg)4779ffe859SGuenter Roeck static int ltc4222_get_value(struct device *dev, u8 reg)
4879ffe859SGuenter Roeck {
4979ffe859SGuenter Roeck struct regmap *regmap = dev_get_drvdata(dev);
5079ffe859SGuenter Roeck unsigned int val;
5179ffe859SGuenter Roeck u8 buf[2];
5279ffe859SGuenter Roeck int ret;
5379ffe859SGuenter Roeck
5479ffe859SGuenter Roeck ret = regmap_bulk_read(regmap, reg, buf, 2);
5579ffe859SGuenter Roeck if (ret < 0)
5679ffe859SGuenter Roeck return ret;
5779ffe859SGuenter Roeck
5879ffe859SGuenter Roeck val = ((buf[0] << 8) + buf[1]) >> 6;
5979ffe859SGuenter Roeck
6079ffe859SGuenter Roeck switch (reg) {
6179ffe859SGuenter Roeck case LTC4222_ADIN1:
6279ffe859SGuenter Roeck case LTC4222_ADIN2:
6379ffe859SGuenter Roeck /* 1.25 mV resolution. Convert to mV. */
6479ffe859SGuenter Roeck val = DIV_ROUND_CLOSEST(val * 5, 4);
6579ffe859SGuenter Roeck break;
6679ffe859SGuenter Roeck case LTC4222_SOURCE1:
6779ffe859SGuenter Roeck case LTC4222_SOURCE2:
6879ffe859SGuenter Roeck /* 31.25 mV resolution. Convert to mV. */
6979ffe859SGuenter Roeck val = DIV_ROUND_CLOSEST(val * 125, 4);
7079ffe859SGuenter Roeck break;
7179ffe859SGuenter Roeck case LTC4222_SENSE1:
7279ffe859SGuenter Roeck case LTC4222_SENSE2:
7379ffe859SGuenter Roeck /*
7479ffe859SGuenter Roeck * 62.5 uV resolution. Convert to current as measured with
7579ffe859SGuenter Roeck * an 1 mOhm sense resistor, in mA. If a different sense
7679ffe859SGuenter Roeck * resistor is installed, calculate the actual current by
7779ffe859SGuenter Roeck * dividing the reported current by the sense resistor value
7879ffe859SGuenter Roeck * in mOhm.
7979ffe859SGuenter Roeck */
8079ffe859SGuenter Roeck val = DIV_ROUND_CLOSEST(val * 125, 2);
8179ffe859SGuenter Roeck break;
8279ffe859SGuenter Roeck default:
8379ffe859SGuenter Roeck return -EINVAL;
8479ffe859SGuenter Roeck }
8579ffe859SGuenter Roeck return val;
8679ffe859SGuenter Roeck }
8779ffe859SGuenter Roeck
ltc4222_value_show(struct device * dev,struct device_attribute * da,char * buf)88ae868a29SGuenter Roeck static ssize_t ltc4222_value_show(struct device *dev,
8979ffe859SGuenter Roeck struct device_attribute *da, char *buf)
9079ffe859SGuenter Roeck {
9179ffe859SGuenter Roeck struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
9279ffe859SGuenter Roeck int value;
9379ffe859SGuenter Roeck
9479ffe859SGuenter Roeck value = ltc4222_get_value(dev, attr->index);
9579ffe859SGuenter Roeck if (value < 0)
9679ffe859SGuenter Roeck return value;
971f4d4af4SGuenter Roeck return sysfs_emit(buf, "%d\n", value);
9879ffe859SGuenter Roeck }
9979ffe859SGuenter Roeck
ltc4222_bool_show(struct device * dev,struct device_attribute * da,char * buf)100ae868a29SGuenter Roeck static ssize_t ltc4222_bool_show(struct device *dev,
10179ffe859SGuenter Roeck struct device_attribute *da, char *buf)
10279ffe859SGuenter Roeck {
10379ffe859SGuenter Roeck struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
10479ffe859SGuenter Roeck struct regmap *regmap = dev_get_drvdata(dev);
10579ffe859SGuenter Roeck unsigned int fault;
10679ffe859SGuenter Roeck int ret;
10779ffe859SGuenter Roeck
10879ffe859SGuenter Roeck ret = regmap_read(regmap, attr->nr, &fault);
10979ffe859SGuenter Roeck if (ret < 0)
11079ffe859SGuenter Roeck return ret;
11179ffe859SGuenter Roeck fault &= attr->index;
11279ffe859SGuenter Roeck if (fault) /* Clear reported faults in chip register */
11379ffe859SGuenter Roeck regmap_update_bits(regmap, attr->nr, attr->index, 0);
11479ffe859SGuenter Roeck
1151f4d4af4SGuenter Roeck return sysfs_emit(buf, "%d\n", !!fault);
11679ffe859SGuenter Roeck }
11779ffe859SGuenter Roeck
11879ffe859SGuenter Roeck /* Voltages */
119ae868a29SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in1_input, ltc4222_value, LTC4222_SOURCE1);
120ae868a29SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in2_input, ltc4222_value, LTC4222_ADIN1);
121ae868a29SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in3_input, ltc4222_value, LTC4222_SOURCE2);
122ae868a29SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in4_input, ltc4222_value, LTC4222_ADIN2);
12379ffe859SGuenter Roeck
12479ffe859SGuenter Roeck /*
12579ffe859SGuenter Roeck * Voltage alarms
12679ffe859SGuenter Roeck * UV/OV faults are associated with the input voltage, and power bad and fet
12779ffe859SGuenter Roeck * faults are associated with the output voltage.
12879ffe859SGuenter Roeck */
129ae868a29SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in1_min_alarm, ltc4222_bool, LTC4222_FAULT1,
130ae868a29SGuenter Roeck FAULT_UV);
131ae868a29SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in1_max_alarm, ltc4222_bool, LTC4222_FAULT1,
132ae868a29SGuenter Roeck FAULT_OV);
133ae868a29SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, ltc4222_bool, LTC4222_FAULT1,
134ae868a29SGuenter Roeck FAULT_POWER_BAD | FAULT_FET_BAD);
13579ffe859SGuenter Roeck
136ae868a29SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in3_min_alarm, ltc4222_bool, LTC4222_FAULT2,
137ae868a29SGuenter Roeck FAULT_UV);
138ae868a29SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in3_max_alarm, ltc4222_bool, LTC4222_FAULT2,
139ae868a29SGuenter Roeck FAULT_OV);
140ae868a29SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in4_alarm, ltc4222_bool, LTC4222_FAULT2,
141ae868a29SGuenter Roeck FAULT_POWER_BAD | FAULT_FET_BAD);
14279ffe859SGuenter Roeck
14379ffe859SGuenter Roeck /* Current (via sense resistor) */
144ae868a29SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc4222_value, LTC4222_SENSE1);
145ae868a29SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(curr2_input, ltc4222_value, LTC4222_SENSE2);
14679ffe859SGuenter Roeck
14779ffe859SGuenter Roeck /* Overcurrent alarm */
148ae868a29SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(curr1_max_alarm, ltc4222_bool, LTC4222_FAULT1,
149ae868a29SGuenter Roeck FAULT_OC);
150ae868a29SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(curr2_max_alarm, ltc4222_bool, LTC4222_FAULT2,
151ae868a29SGuenter Roeck FAULT_OC);
15279ffe859SGuenter Roeck
15379ffe859SGuenter Roeck static struct attribute *ltc4222_attrs[] = {
15479ffe859SGuenter Roeck &sensor_dev_attr_in1_input.dev_attr.attr,
15579ffe859SGuenter Roeck &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
15679ffe859SGuenter Roeck &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
15779ffe859SGuenter Roeck &sensor_dev_attr_in2_input.dev_attr.attr,
15879ffe859SGuenter Roeck &sensor_dev_attr_in2_alarm.dev_attr.attr,
15979ffe859SGuenter Roeck &sensor_dev_attr_in3_input.dev_attr.attr,
16079ffe859SGuenter Roeck &sensor_dev_attr_in3_min_alarm.dev_attr.attr,
16179ffe859SGuenter Roeck &sensor_dev_attr_in3_max_alarm.dev_attr.attr,
16279ffe859SGuenter Roeck &sensor_dev_attr_in4_input.dev_attr.attr,
16379ffe859SGuenter Roeck &sensor_dev_attr_in4_alarm.dev_attr.attr,
16479ffe859SGuenter Roeck
16579ffe859SGuenter Roeck &sensor_dev_attr_curr1_input.dev_attr.attr,
16679ffe859SGuenter Roeck &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
16779ffe859SGuenter Roeck &sensor_dev_attr_curr2_input.dev_attr.attr,
16879ffe859SGuenter Roeck &sensor_dev_attr_curr2_max_alarm.dev_attr.attr,
16979ffe859SGuenter Roeck
17079ffe859SGuenter Roeck NULL,
17179ffe859SGuenter Roeck };
17279ffe859SGuenter Roeck ATTRIBUTE_GROUPS(ltc4222);
17379ffe859SGuenter Roeck
174034b44b4SAxel Lin static const struct regmap_config ltc4222_regmap_config = {
17579ffe859SGuenter Roeck .reg_bits = 8,
17679ffe859SGuenter Roeck .val_bits = 8,
17779ffe859SGuenter Roeck .max_register = LTC4222_ADC_CONTROL,
17879ffe859SGuenter Roeck };
17979ffe859SGuenter Roeck
ltc4222_probe(struct i2c_client * client)18067487038SStephen Kitt static int ltc4222_probe(struct i2c_client *client)
18179ffe859SGuenter Roeck {
18279ffe859SGuenter Roeck struct device *dev = &client->dev;
18379ffe859SGuenter Roeck struct device *hwmon_dev;
18479ffe859SGuenter Roeck struct regmap *regmap;
18579ffe859SGuenter Roeck
18679ffe859SGuenter Roeck regmap = devm_regmap_init_i2c(client, <c4222_regmap_config);
18779ffe859SGuenter Roeck if (IS_ERR(regmap)) {
18879ffe859SGuenter Roeck dev_err(dev, "failed to allocate register map\n");
18979ffe859SGuenter Roeck return PTR_ERR(regmap);
19079ffe859SGuenter Roeck }
19179ffe859SGuenter Roeck
19279ffe859SGuenter Roeck /* Clear faults */
19379ffe859SGuenter Roeck regmap_write(regmap, LTC4222_FAULT1, 0x00);
19479ffe859SGuenter Roeck regmap_write(regmap, LTC4222_FAULT2, 0x00);
19579ffe859SGuenter Roeck
19679ffe859SGuenter Roeck hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
19779ffe859SGuenter Roeck regmap,
19879ffe859SGuenter Roeck ltc4222_groups);
19979ffe859SGuenter Roeck return PTR_ERR_OR_ZERO(hwmon_dev);
20079ffe859SGuenter Roeck }
20179ffe859SGuenter Roeck
20279ffe859SGuenter Roeck static const struct i2c_device_id ltc4222_id[] = {
203*d8a66f36SUwe Kleine-König {"ltc4222"},
20479ffe859SGuenter Roeck { }
20579ffe859SGuenter Roeck };
20679ffe859SGuenter Roeck
20779ffe859SGuenter Roeck MODULE_DEVICE_TABLE(i2c, ltc4222_id);
20879ffe859SGuenter Roeck
20979ffe859SGuenter Roeck static struct i2c_driver ltc4222_driver = {
21079ffe859SGuenter Roeck .driver = {
21179ffe859SGuenter Roeck .name = "ltc4222",
21279ffe859SGuenter Roeck },
2131975d167SUwe Kleine-König .probe = ltc4222_probe,
21479ffe859SGuenter Roeck .id_table = ltc4222_id,
21579ffe859SGuenter Roeck };
21679ffe859SGuenter Roeck
21779ffe859SGuenter Roeck module_i2c_driver(ltc4222_driver);
21879ffe859SGuenter Roeck
21979ffe859SGuenter Roeck MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
22079ffe859SGuenter Roeck MODULE_DESCRIPTION("LTC4222 driver");
22179ffe859SGuenter Roeck MODULE_LICENSE("GPL");
222