1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
56static 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
73 val = DIV_ROUND_CLOSEST(val * 5, 4);
74 break;
75 case LTC4222_SOURCE1:
76 case LTC4222_SOURCE2:
77
78 val = DIV_ROUND_CLOSEST(val * 125, 4);
79 break;
80 case LTC4222_SENSE1:
81 case LTC4222_SENSE2:
82
83
84
85
86
87
88
89 val = DIV_ROUND_CLOSEST(val * 125, 2);
90 break;
91 default:
92 return -EINVAL;
93 }
94 return val;
95}
96
97static ssize_t ltc4222_show_value(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
109static ssize_t ltc4222_show_bool(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)
122 regmap_update_bits(regmap, attr->nr, attr->index, 0);
123
124 return snprintf(buf, PAGE_SIZE, "%d\n", !!fault);
125}
126
127
128static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ltc4222_show_value, NULL,
129 LTC4222_SOURCE1);
130static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ltc4222_show_value, NULL,
131 LTC4222_ADIN1);
132static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, ltc4222_show_value, NULL,
133 LTC4222_SOURCE2);
134static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, ltc4222_show_value, NULL,
135 LTC4222_ADIN2);
136
137
138
139
140
141
142static SENSOR_DEVICE_ATTR_2(in1_min_alarm, S_IRUGO, ltc4222_show_bool, NULL,
143 LTC4222_FAULT1, FAULT_UV);
144static SENSOR_DEVICE_ATTR_2(in1_max_alarm, S_IRUGO, ltc4222_show_bool, NULL,
145 LTC4222_FAULT1, FAULT_OV);
146static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, ltc4222_show_bool, NULL,
147 LTC4222_FAULT1, FAULT_POWER_BAD | FAULT_FET_BAD);
148
149static SENSOR_DEVICE_ATTR_2(in3_min_alarm, S_IRUGO, ltc4222_show_bool, NULL,
150 LTC4222_FAULT2, FAULT_UV);
151static SENSOR_DEVICE_ATTR_2(in3_max_alarm, S_IRUGO, ltc4222_show_bool, NULL,
152 LTC4222_FAULT2, FAULT_OV);
153static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, ltc4222_show_bool, NULL,
154 LTC4222_FAULT2, FAULT_POWER_BAD | FAULT_FET_BAD);
155
156
157static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ltc4222_show_value, NULL,
158 LTC4222_SENSE1);
159static SENSOR_DEVICE_ATTR(curr2_input, S_IRUGO, ltc4222_show_value, NULL,
160 LTC4222_SENSE2);
161
162
163static SENSOR_DEVICE_ATTR_2(curr1_max_alarm, S_IRUGO, ltc4222_show_bool, NULL,
164 LTC4222_FAULT1, FAULT_OC);
165static SENSOR_DEVICE_ATTR_2(curr2_max_alarm, S_IRUGO, ltc4222_show_bool, NULL,
166 LTC4222_FAULT2, FAULT_OC);
167
168static struct attribute *ltc4222_attrs[] = {
169 &sensor_dev_attr_in1_input.dev_attr.attr,
170 &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
171 &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
172 &sensor_dev_attr_in2_input.dev_attr.attr,
173 &sensor_dev_attr_in2_alarm.dev_attr.attr,
174 &sensor_dev_attr_in3_input.dev_attr.attr,
175 &sensor_dev_attr_in3_min_alarm.dev_attr.attr,
176 &sensor_dev_attr_in3_max_alarm.dev_attr.attr,
177 &sensor_dev_attr_in4_input.dev_attr.attr,
178 &sensor_dev_attr_in4_alarm.dev_attr.attr,
179
180 &sensor_dev_attr_curr1_input.dev_attr.attr,
181 &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
182 &sensor_dev_attr_curr2_input.dev_attr.attr,
183 &sensor_dev_attr_curr2_max_alarm.dev_attr.attr,
184
185 NULL,
186};
187ATTRIBUTE_GROUPS(ltc4222);
188
189static const struct regmap_config ltc4222_regmap_config = {
190 .reg_bits = 8,
191 .val_bits = 8,
192 .max_register = LTC4222_ADC_CONTROL,
193};
194
195static int ltc4222_probe(struct i2c_client *client,
196 const struct i2c_device_id *id)
197{
198 struct device *dev = &client->dev;
199 struct device *hwmon_dev;
200 struct regmap *regmap;
201
202 regmap = devm_regmap_init_i2c(client, <c4222_regmap_config);
203 if (IS_ERR(regmap)) {
204 dev_err(dev, "failed to allocate register map\n");
205 return PTR_ERR(regmap);
206 }
207
208
209 regmap_write(regmap, LTC4222_FAULT1, 0x00);
210 regmap_write(regmap, LTC4222_FAULT2, 0x00);
211
212 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
213 regmap,
214 ltc4222_groups);
215 return PTR_ERR_OR_ZERO(hwmon_dev);
216}
217
218static const struct i2c_device_id ltc4222_id[] = {
219 {"ltc4222", 0},
220 { }
221};
222
223MODULE_DEVICE_TABLE(i2c, ltc4222_id);
224
225static struct i2c_driver ltc4222_driver = {
226 .driver = {
227 .name = "ltc4222",
228 },
229 .probe = ltc4222_probe,
230 .id_table = ltc4222_id,
231};
232
233module_i2c_driver(ltc4222_driver);
234
235MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
236MODULE_DESCRIPTION("LTC4222 driver");
237MODULE_LICENSE("GPL");
238