1
2
3
4
5
6
7
8
9
10
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/err.h>
15#include <linux/slab.h>
16#include <linux/i2c.h>
17#include <linux/hwmon.h>
18#include <linux/hwmon-sysfs.h>
19#include <linux/jiffies.h>
20
21
22enum ltc4215_cmd {
23 LTC4215_CONTROL = 0x00,
24 LTC4215_ALERT = 0x01,
25 LTC4215_STATUS = 0x02,
26 LTC4215_FAULT = 0x03,
27 LTC4215_SENSE = 0x04,
28 LTC4215_SOURCE = 0x05,
29 LTC4215_ADIN = 0x06,
30};
31
32struct ltc4215_data {
33 struct i2c_client *client;
34
35 struct mutex update_lock;
36 bool valid;
37 unsigned long last_updated;
38
39
40 u8 regs[7];
41};
42
43static struct ltc4215_data *ltc4215_update_device(struct device *dev)
44{
45 struct ltc4215_data *data = dev_get_drvdata(dev);
46 struct i2c_client *client = data->client;
47 s32 val;
48 int i;
49
50 mutex_lock(&data->update_lock);
51
52
53 if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {
54
55 dev_dbg(&client->dev, "Starting ltc4215 update\n");
56
57
58 for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
59 val = i2c_smbus_read_byte_data(client, i);
60 if (unlikely(val < 0))
61 data->regs[i] = 0;
62 else
63 data->regs[i] = val;
64 }
65
66 data->last_updated = jiffies;
67 data->valid = 1;
68 }
69
70 mutex_unlock(&data->update_lock);
71
72 return data;
73}
74
75
76static int ltc4215_get_voltage(struct device *dev, u8 reg)
77{
78 struct ltc4215_data *data = ltc4215_update_device(dev);
79 const u8 regval = data->regs[reg];
80 u32 voltage = 0;
81
82 switch (reg) {
83 case LTC4215_SENSE:
84
85 voltage = regval * 151 / 1000;
86 break;
87 case LTC4215_SOURCE:
88
89 voltage = regval * 605 / 10;
90 break;
91 case LTC4215_ADIN:
92
93
94
95
96 voltage = regval * 482 * 125 / 1000;
97 break;
98 default:
99
100 WARN_ON_ONCE(1);
101 break;
102 }
103
104 return voltage;
105}
106
107
108static unsigned int ltc4215_get_current(struct device *dev)
109{
110 struct ltc4215_data *data = ltc4215_update_device(dev);
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 const unsigned int voltage = data->regs[LTC4215_SENSE] * 151;
129
130
131 const unsigned int curr = voltage / 4;
132
133 return curr;
134}
135
136static ssize_t ltc4215_voltage_show(struct device *dev,
137 struct device_attribute *da, char *buf)
138{
139 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
140 const int voltage = ltc4215_get_voltage(dev, attr->index);
141
142 return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
143}
144
145static ssize_t ltc4215_current_show(struct device *dev,
146 struct device_attribute *da, char *buf)
147{
148 const unsigned int curr = ltc4215_get_current(dev);
149
150 return snprintf(buf, PAGE_SIZE, "%u\n", curr);
151}
152
153static ssize_t ltc4215_power_show(struct device *dev,
154 struct device_attribute *da, char *buf)
155{
156 const unsigned int curr = ltc4215_get_current(dev);
157 const int output_voltage = ltc4215_get_voltage(dev, LTC4215_ADIN);
158
159
160 const unsigned int power = abs(output_voltage * curr);
161
162 return snprintf(buf, PAGE_SIZE, "%u\n", power);
163}
164
165static ssize_t ltc4215_alarm_show(struct device *dev,
166 struct device_attribute *da, char *buf)
167{
168 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
169 struct ltc4215_data *data = ltc4215_update_device(dev);
170 const u8 reg = data->regs[LTC4215_STATUS];
171 const u32 mask = attr->index;
172
173 return snprintf(buf, PAGE_SIZE, "%u\n", !!(reg & mask));
174}
175
176
177
178
179
180
181
182
183
184
185static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc4215_current, 0);
186static SENSOR_DEVICE_ATTR_RO(curr1_max_alarm, ltc4215_alarm, 1 << 2);
187
188
189static SENSOR_DEVICE_ATTR_RO(power1_input, ltc4215_power, 0);
190
191
192static SENSOR_DEVICE_ATTR_RO(in1_input, ltc4215_voltage, LTC4215_ADIN);
193static SENSOR_DEVICE_ATTR_RO(in1_max_alarm, ltc4215_alarm, 1 << 0);
194static SENSOR_DEVICE_ATTR_RO(in1_min_alarm, ltc4215_alarm, 1 << 1);
195
196
197static SENSOR_DEVICE_ATTR_RO(in2_input, ltc4215_voltage, LTC4215_SOURCE);
198static SENSOR_DEVICE_ATTR_RO(in2_min_alarm, ltc4215_alarm, 1 << 3);
199
200
201
202
203
204static struct attribute *ltc4215_attrs[] = {
205 &sensor_dev_attr_curr1_input.dev_attr.attr,
206 &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
207
208 &sensor_dev_attr_power1_input.dev_attr.attr,
209
210 &sensor_dev_attr_in1_input.dev_attr.attr,
211 &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
212 &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
213
214 &sensor_dev_attr_in2_input.dev_attr.attr,
215 &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
216
217 NULL,
218};
219ATTRIBUTE_GROUPS(ltc4215);
220
221static int ltc4215_probe(struct i2c_client *client,
222 const struct i2c_device_id *id)
223{
224 struct i2c_adapter *adapter = client->adapter;
225 struct device *dev = &client->dev;
226 struct ltc4215_data *data;
227 struct device *hwmon_dev;
228
229 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
230 return -ENODEV;
231
232 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
233 if (!data)
234 return -ENOMEM;
235
236 data->client = client;
237 mutex_init(&data->update_lock);
238
239
240 i2c_smbus_write_byte_data(client, LTC4215_FAULT, 0x00);
241
242 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
243 data,
244 ltc4215_groups);
245 return PTR_ERR_OR_ZERO(hwmon_dev);
246}
247
248static const struct i2c_device_id ltc4215_id[] = {
249 { "ltc4215", 0 },
250 { }
251};
252MODULE_DEVICE_TABLE(i2c, ltc4215_id);
253
254
255static struct i2c_driver ltc4215_driver = {
256 .driver = {
257 .name = "ltc4215",
258 },
259 .probe = ltc4215_probe,
260 .id_table = ltc4215_id,
261};
262
263module_i2c_driver(ltc4215_driver);
264
265MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
266MODULE_DESCRIPTION("LTC4215 driver");
267MODULE_LICENSE("GPL");
268