xref: /openbmc/linux/drivers/hwmon/ltc4222.c (revision 151f4e2b)
1 /*
2  * Driver for Linear Technology LTC4222 Dual Hot Swap controller
3  *
4  * Copyright (c) 2014 Guenter Roeck
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16 
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/err.h>
20 #include <linux/slab.h>
21 #include <linux/bitops.h>
22 #include <linux/i2c.h>
23 #include <linux/hwmon.h>
24 #include <linux/hwmon-sysfs.h>
25 #include <linux/jiffies.h>
26 #include <linux/regmap.h>
27 
28 /* chip registers */
29 
30 #define LTC4222_CONTROL1	0xd0
31 #define LTC4222_ALERT1		0xd1
32 #define LTC4222_STATUS1		0xd2
33 #define LTC4222_FAULT1		0xd3
34 #define LTC4222_CONTROL2	0xd4
35 #define LTC4222_ALERT2		0xd5
36 #define LTC4222_STATUS2		0xd6
37 #define LTC4222_FAULT2		0xd7
38 #define LTC4222_SOURCE1		0xd8
39 #define LTC4222_SOURCE2		0xda
40 #define LTC4222_ADIN1		0xdc
41 #define LTC4222_ADIN2		0xde
42 #define LTC4222_SENSE1		0xe0
43 #define LTC4222_SENSE2		0xe2
44 #define LTC4222_ADC_CONTROL	0xe4
45 
46 /*
47  * Fault register bits
48  */
49 #define FAULT_OV	BIT(0)
50 #define FAULT_UV	BIT(1)
51 #define FAULT_OC	BIT(2)
52 #define FAULT_POWER_BAD	BIT(3)
53 #define FAULT_FET_BAD	BIT(5)
54 
55 /* Return the voltage from the given register in mV or mA */
56 static int ltc4222_get_value(struct device *dev, u8 reg)
57 {
58 	struct regmap *regmap = dev_get_drvdata(dev);
59 	unsigned int val;
60 	u8 buf[2];
61 	int ret;
62 
63 	ret = regmap_bulk_read(regmap, reg, buf, 2);
64 	if (ret < 0)
65 		return ret;
66 
67 	val = ((buf[0] << 8) + buf[1]) >> 6;
68 
69 	switch (reg) {
70 	case LTC4222_ADIN1:
71 	case LTC4222_ADIN2:
72 		/* 1.25 mV resolution. Convert to mV. */
73 		val = DIV_ROUND_CLOSEST(val * 5, 4);
74 		break;
75 	case LTC4222_SOURCE1:
76 	case LTC4222_SOURCE2:
77 		/* 31.25 mV resolution. Convert to mV. */
78 		val = DIV_ROUND_CLOSEST(val * 125, 4);
79 		break;
80 	case LTC4222_SENSE1:
81 	case LTC4222_SENSE2:
82 		/*
83 		 * 62.5 uV resolution. Convert to current as measured with
84 		 * an 1 mOhm sense resistor, in mA. If a different sense
85 		 * resistor is installed, calculate the actual current by
86 		 * dividing the reported current by the sense resistor value
87 		 * in mOhm.
88 		 */
89 		val = DIV_ROUND_CLOSEST(val * 125, 2);
90 		break;
91 	default:
92 		return -EINVAL;
93 	}
94 	return val;
95 }
96 
97 static ssize_t ltc4222_value_show(struct device *dev,
98 				  struct device_attribute *da, char *buf)
99 {
100 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
101 	int value;
102 
103 	value = ltc4222_get_value(dev, attr->index);
104 	if (value < 0)
105 		return value;
106 	return snprintf(buf, PAGE_SIZE, "%d\n", value);
107 }
108 
109 static ssize_t ltc4222_bool_show(struct device *dev,
110 				 struct device_attribute *da, char *buf)
111 {
112 	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
113 	struct regmap *regmap = dev_get_drvdata(dev);
114 	unsigned int fault;
115 	int ret;
116 
117 	ret = regmap_read(regmap, attr->nr, &fault);
118 	if (ret < 0)
119 		return ret;
120 	fault &= attr->index;
121 	if (fault)		/* Clear reported faults in chip register */
122 		regmap_update_bits(regmap, attr->nr, attr->index, 0);
123 
124 	return snprintf(buf, PAGE_SIZE, "%d\n", !!fault);
125 }
126 
127 /* Voltages */
128 static SENSOR_DEVICE_ATTR_RO(in1_input, ltc4222_value, LTC4222_SOURCE1);
129 static SENSOR_DEVICE_ATTR_RO(in2_input, ltc4222_value, LTC4222_ADIN1);
130 static SENSOR_DEVICE_ATTR_RO(in3_input, ltc4222_value, LTC4222_SOURCE2);
131 static SENSOR_DEVICE_ATTR_RO(in4_input, ltc4222_value, LTC4222_ADIN2);
132 
133 /*
134  * Voltage alarms
135  * UV/OV faults are associated with the input voltage, and power bad and fet
136  * faults are associated with the output voltage.
137  */
138 static SENSOR_DEVICE_ATTR_2_RO(in1_min_alarm, ltc4222_bool, LTC4222_FAULT1,
139 			       FAULT_UV);
140 static SENSOR_DEVICE_ATTR_2_RO(in1_max_alarm, ltc4222_bool, LTC4222_FAULT1,
141 			       FAULT_OV);
142 static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, ltc4222_bool, LTC4222_FAULT1,
143 			       FAULT_POWER_BAD | FAULT_FET_BAD);
144 
145 static SENSOR_DEVICE_ATTR_2_RO(in3_min_alarm, ltc4222_bool, LTC4222_FAULT2,
146 			       FAULT_UV);
147 static SENSOR_DEVICE_ATTR_2_RO(in3_max_alarm, ltc4222_bool, LTC4222_FAULT2,
148 			       FAULT_OV);
149 static SENSOR_DEVICE_ATTR_2_RO(in4_alarm, ltc4222_bool, LTC4222_FAULT2,
150 			       FAULT_POWER_BAD | FAULT_FET_BAD);
151 
152 /* Current (via sense resistor) */
153 static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc4222_value, LTC4222_SENSE1);
154 static SENSOR_DEVICE_ATTR_RO(curr2_input, ltc4222_value, LTC4222_SENSE2);
155 
156 /* Overcurrent alarm */
157 static SENSOR_DEVICE_ATTR_2_RO(curr1_max_alarm, ltc4222_bool, LTC4222_FAULT1,
158 			       FAULT_OC);
159 static SENSOR_DEVICE_ATTR_2_RO(curr2_max_alarm, ltc4222_bool, LTC4222_FAULT2,
160 			       FAULT_OC);
161 
162 static struct attribute *ltc4222_attrs[] = {
163 	&sensor_dev_attr_in1_input.dev_attr.attr,
164 	&sensor_dev_attr_in1_min_alarm.dev_attr.attr,
165 	&sensor_dev_attr_in1_max_alarm.dev_attr.attr,
166 	&sensor_dev_attr_in2_input.dev_attr.attr,
167 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
168 	&sensor_dev_attr_in3_input.dev_attr.attr,
169 	&sensor_dev_attr_in3_min_alarm.dev_attr.attr,
170 	&sensor_dev_attr_in3_max_alarm.dev_attr.attr,
171 	&sensor_dev_attr_in4_input.dev_attr.attr,
172 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
173 
174 	&sensor_dev_attr_curr1_input.dev_attr.attr,
175 	&sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
176 	&sensor_dev_attr_curr2_input.dev_attr.attr,
177 	&sensor_dev_attr_curr2_max_alarm.dev_attr.attr,
178 
179 	NULL,
180 };
181 ATTRIBUTE_GROUPS(ltc4222);
182 
183 static const struct regmap_config ltc4222_regmap_config = {
184 	.reg_bits = 8,
185 	.val_bits = 8,
186 	.max_register = LTC4222_ADC_CONTROL,
187 };
188 
189 static int ltc4222_probe(struct i2c_client *client,
190 			 const struct i2c_device_id *id)
191 {
192 	struct device *dev = &client->dev;
193 	struct device *hwmon_dev;
194 	struct regmap *regmap;
195 
196 	regmap = devm_regmap_init_i2c(client, &ltc4222_regmap_config);
197 	if (IS_ERR(regmap)) {
198 		dev_err(dev, "failed to allocate register map\n");
199 		return PTR_ERR(regmap);
200 	}
201 
202 	/* Clear faults */
203 	regmap_write(regmap, LTC4222_FAULT1, 0x00);
204 	regmap_write(regmap, LTC4222_FAULT2, 0x00);
205 
206 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
207 							   regmap,
208 							   ltc4222_groups);
209 	return PTR_ERR_OR_ZERO(hwmon_dev);
210 }
211 
212 static const struct i2c_device_id ltc4222_id[] = {
213 	{"ltc4222", 0},
214 	{ }
215 };
216 
217 MODULE_DEVICE_TABLE(i2c, ltc4222_id);
218 
219 static struct i2c_driver ltc4222_driver = {
220 	.driver = {
221 		   .name = "ltc4222",
222 		   },
223 	.probe = ltc4222_probe,
224 	.id_table = ltc4222_id,
225 };
226 
227 module_i2c_driver(ltc4222_driver);
228 
229 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
230 MODULE_DESCRIPTION("LTC4222 driver");
231 MODULE_LICENSE("GPL");
232