2 * ADE7854/58/68/78 Polyphase Multifunction Energy Metering IC Driver (I2C Bus)
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/i2c.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
18 static int ade7854_i2c_write_reg_8(struct device
*dev
,
23 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
24 struct ade7854_state
*st
= iio_priv(indio_dev
);
26 mutex_lock(&st
->buf_lock
);
27 st
->tx
[0] = (reg_address
>> 8) & 0xFF;
28 st
->tx
[1] = reg_address
& 0xFF;
31 ret
= i2c_master_send(st
->i2c
, st
->tx
, 3);
32 mutex_unlock(&st
->buf_lock
);
37 static int ade7854_i2c_write_reg_16(struct device
*dev
,
42 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
43 struct ade7854_state
*st
= iio_priv(indio_dev
);
45 mutex_lock(&st
->buf_lock
);
46 st
->tx
[0] = (reg_address
>> 8) & 0xFF;
47 st
->tx
[1] = reg_address
& 0xFF;
48 st
->tx
[2] = (value
>> 8) & 0xFF;
49 st
->tx
[3] = value
& 0xFF;
51 ret
= i2c_master_send(st
->i2c
, st
->tx
, 4);
52 mutex_unlock(&st
->buf_lock
);
57 static int ade7854_i2c_write_reg_24(struct device
*dev
,
62 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
63 struct ade7854_state
*st
= iio_priv(indio_dev
);
65 mutex_lock(&st
->buf_lock
);
66 st
->tx
[0] = (reg_address
>> 8) & 0xFF;
67 st
->tx
[1] = reg_address
& 0xFF;
68 st
->tx
[2] = (value
>> 16) & 0xFF;
69 st
->tx
[3] = (value
>> 8) & 0xFF;
70 st
->tx
[4] = value
& 0xFF;
72 ret
= i2c_master_send(st
->i2c
, st
->tx
, 5);
73 mutex_unlock(&st
->buf_lock
);
78 static int ade7854_i2c_write_reg_32(struct device
*dev
,
83 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
84 struct ade7854_state
*st
= iio_priv(indio_dev
);
86 mutex_lock(&st
->buf_lock
);
87 st
->tx
[0] = (reg_address
>> 8) & 0xFF;
88 st
->tx
[1] = reg_address
& 0xFF;
89 st
->tx
[2] = (value
>> 24) & 0xFF;
90 st
->tx
[3] = (value
>> 16) & 0xFF;
91 st
->tx
[4] = (value
>> 8) & 0xFF;
92 st
->tx
[5] = value
& 0xFF;
94 ret
= i2c_master_send(st
->i2c
, st
->tx
, 6);
95 mutex_unlock(&st
->buf_lock
);
100 static int ade7854_i2c_read_reg_8(struct device
*dev
,
104 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
105 struct ade7854_state
*st
= iio_priv(indio_dev
);
108 mutex_lock(&st
->buf_lock
);
109 st
->tx
[0] = (reg_address
>> 8) & 0xFF;
110 st
->tx
[1] = reg_address
& 0xFF;
112 ret
= i2c_master_send(st
->i2c
, st
->tx
, 2);
116 ret
= i2c_master_recv(st
->i2c
, st
->rx
, 1);
122 mutex_unlock(&st
->buf_lock
);
126 static int ade7854_i2c_read_reg_16(struct device
*dev
,
130 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
131 struct ade7854_state
*st
= iio_priv(indio_dev
);
134 mutex_lock(&st
->buf_lock
);
135 st
->tx
[0] = (reg_address
>> 8) & 0xFF;
136 st
->tx
[1] = reg_address
& 0xFF;
138 ret
= i2c_master_send(st
->i2c
, st
->tx
, 2);
142 ret
= i2c_master_recv(st
->i2c
, st
->rx
, 2);
146 *val
= (st
->rx
[0] << 8) | st
->rx
[1];
148 mutex_unlock(&st
->buf_lock
);
152 static int ade7854_i2c_read_reg_24(struct device
*dev
,
156 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
157 struct ade7854_state
*st
= iio_priv(indio_dev
);
160 mutex_lock(&st
->buf_lock
);
161 st
->tx
[0] = (reg_address
>> 8) & 0xFF;
162 st
->tx
[1] = reg_address
& 0xFF;
164 ret
= i2c_master_send(st
->i2c
, st
->tx
, 2);
168 ret
= i2c_master_recv(st
->i2c
, st
->rx
, 3);
172 *val
= (st
->rx
[0] << 16) | (st
->rx
[1] << 8) | st
->rx
[2];
174 mutex_unlock(&st
->buf_lock
);
178 static int ade7854_i2c_read_reg_32(struct device
*dev
,
182 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
183 struct ade7854_state
*st
= iio_priv(indio_dev
);
186 mutex_lock(&st
->buf_lock
);
187 st
->tx
[0] = (reg_address
>> 8) & 0xFF;
188 st
->tx
[1] = reg_address
& 0xFF;
190 ret
= i2c_master_send(st
->i2c
, st
->tx
, 2);
194 ret
= i2c_master_recv(st
->i2c
, st
->rx
, 3);
198 *val
= (st
->rx
[0] << 24) | (st
->rx
[1] << 16) | (st
->rx
[2] << 8) | st
->rx
[3];
200 mutex_unlock(&st
->buf_lock
);
204 static int __devinit
ade7854_i2c_probe(struct i2c_client
*client
,
205 const struct i2c_device_id
*id
)
208 struct ade7854_state
*st
;
209 struct iio_dev
*indio_dev
;
211 indio_dev
= iio_allocate_device(sizeof(*st
));
212 if (indio_dev
== NULL
)
214 st
= iio_priv(indio_dev
);
215 i2c_set_clientdata(client
, indio_dev
);
216 st
->read_reg_8
= ade7854_i2c_read_reg_8
;
217 st
->read_reg_16
= ade7854_i2c_read_reg_16
;
218 st
->read_reg_24
= ade7854_i2c_read_reg_24
;
219 st
->read_reg_32
= ade7854_i2c_read_reg_32
;
220 st
->write_reg_8
= ade7854_i2c_write_reg_8
;
221 st
->write_reg_16
= ade7854_i2c_write_reg_16
;
222 st
->write_reg_24
= ade7854_i2c_write_reg_24
;
223 st
->write_reg_32
= ade7854_i2c_write_reg_32
;
225 st
->irq
= client
->irq
;
227 ret
= ade7854_probe(indio_dev
, &client
->dev
);
229 iio_free_device(indio_dev
);
234 static int __devexit
ade7854_i2c_remove(struct i2c_client
*client
)
236 return ade7854_remove(i2c_get_clientdata(client
));
239 static const struct i2c_device_id ade7854_id
[] = {
246 MODULE_DEVICE_TABLE(i2c
, ade7854_id
);
248 static struct i2c_driver ade7854_i2c_driver
= {
252 .probe
= ade7854_i2c_probe
,
253 .remove
= __devexit_p(ade7854_i2c_remove
),
254 .id_table
= ade7854_id
,
256 module_i2c_driver(ade7854_i2c_driver
);
258 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
259 MODULE_DESCRIPTION("Analog Devices ADE7854/58/68/78 Polyphase Multifunction Energy Metering IC I2C Driver");
260 MODULE_LICENSE("GPL v2");