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