1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/of.h>
34#include <linux/init.h>
35#include <linux/err.h>
36#include <linux/slab.h>
37#include <linux/i2c.h>
38#include <linux/hwmon.h>
39#include <linux/hwmon-sysfs.h>
40#include <linux/jiffies.h>
41
42
43#define LTC4151_SENSE_H 0x00
44#define LTC4151_SENSE_L 0x01
45#define LTC4151_VIN_H 0x02
46#define LTC4151_VIN_L 0x03
47#define LTC4151_ADIN_H 0x04
48#define LTC4151_ADIN_L 0x05
49
50struct ltc4151_data {
51 struct i2c_client *client;
52
53 struct mutex update_lock;
54 bool valid;
55 unsigned long last_updated;
56 unsigned int shunt;
57
58
59 u8 regs[6];
60};
61
62static struct ltc4151_data *ltc4151_update_device(struct device *dev)
63{
64 struct ltc4151_data *data = dev_get_drvdata(dev);
65 struct i2c_client *client = data->client;
66 struct ltc4151_data *ret = data;
67
68 mutex_lock(&data->update_lock);
69
70
71
72
73
74 if (time_after(jiffies, data->last_updated + HZ / 6) || !data->valid) {
75 int i;
76
77 dev_dbg(&client->dev, "Starting ltc4151 update\n");
78
79
80 for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
81 int val;
82
83 val = i2c_smbus_read_byte_data(client, i);
84 if (unlikely(val < 0)) {
85 dev_dbg(dev,
86 "Failed to read ADC value: error %d\n",
87 val);
88 ret = ERR_PTR(val);
89 goto abort;
90 }
91 data->regs[i] = val;
92 }
93 data->last_updated = jiffies;
94 data->valid = 1;
95 }
96abort:
97 mutex_unlock(&data->update_lock);
98 return ret;
99}
100
101
102static int ltc4151_get_value(struct ltc4151_data *data, u8 reg)
103{
104 u32 val;
105
106 val = (data->regs[reg] << 4) + (data->regs[reg + 1] >> 4);
107
108 switch (reg) {
109 case LTC4151_ADIN_H:
110
111 val = val * 500 / 1000;
112 break;
113 case LTC4151_SENSE_H:
114
115
116
117
118 val = val * 20 * 1000 / data->shunt;
119 break;
120 case LTC4151_VIN_H:
121
122 val = val * 25;
123 break;
124 default:
125
126 WARN_ON_ONCE(1);
127 val = 0;
128 break;
129 }
130
131 return val;
132}
133
134static ssize_t ltc4151_show_value(struct device *dev,
135 struct device_attribute *da, char *buf)
136{
137 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
138 struct ltc4151_data *data = ltc4151_update_device(dev);
139 int value;
140
141 if (IS_ERR(data))
142 return PTR_ERR(data);
143
144 value = ltc4151_get_value(data, attr->index);
145 return snprintf(buf, PAGE_SIZE, "%d\n", value);
146}
147
148
149
150
151static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ltc4151_show_value, NULL,
152 LTC4151_VIN_H);
153static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ltc4151_show_value, NULL,
154 LTC4151_ADIN_H);
155
156
157static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ltc4151_show_value, NULL,
158 LTC4151_SENSE_H);
159
160
161
162
163
164static struct attribute *ltc4151_attrs[] = {
165 &sensor_dev_attr_in1_input.dev_attr.attr,
166 &sensor_dev_attr_in2_input.dev_attr.attr,
167
168 &sensor_dev_attr_curr1_input.dev_attr.attr,
169
170 NULL,
171};
172ATTRIBUTE_GROUPS(ltc4151);
173
174static int ltc4151_probe(struct i2c_client *client,
175 const struct i2c_device_id *id)
176{
177 struct i2c_adapter *adapter = client->adapter;
178 struct device *dev = &client->dev;
179 struct ltc4151_data *data;
180 struct device *hwmon_dev;
181 u32 shunt;
182
183 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
184 return -ENODEV;
185
186 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
187 if (!data)
188 return -ENOMEM;
189
190 if (of_property_read_u32(client->dev.of_node,
191 "shunt-resistor-micro-ohms", &shunt))
192 shunt = 1000;
193
194 if (shunt == 0)
195 return -EINVAL;
196
197 data->shunt = shunt;
198
199 data->client = client;
200 mutex_init(&data->update_lock);
201
202 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
203 data,
204 ltc4151_groups);
205 return PTR_ERR_OR_ZERO(hwmon_dev);
206}
207
208static const struct i2c_device_id ltc4151_id[] = {
209 { "ltc4151", 0 },
210 { }
211};
212MODULE_DEVICE_TABLE(i2c, ltc4151_id);
213
214static const struct of_device_id ltc4151_match[] = {
215 { .compatible = "lltc,ltc4151" },
216 {},
217};
218MODULE_DEVICE_TABLE(of, ltc4151_match);
219
220
221static struct i2c_driver ltc4151_driver = {
222 .driver = {
223 .name = "ltc4151",
224 .of_match_table = of_match_ptr(ltc4151_match),
225 },
226 .probe = ltc4151_probe,
227 .id_table = ltc4151_id,
228};
229
230module_i2c_driver(ltc4151_driver);
231
232MODULE_AUTHOR("Per Dalen <per.dalen@appeartv.com>");
233MODULE_DESCRIPTION("LTC4151 driver");
234MODULE_LICENSE("GPL");
235