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>
17 static int ade7854_i2c_write_reg_8(struct device
*dev
,
22 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
23 struct ade7854_state
*st
= iio_priv(indio_dev
);
25 mutex_lock(&st
->buf_lock
);
26 st
->tx
[0] = (reg_address
>> 8) & 0xFF;
27 st
->tx
[1] = reg_address
& 0xFF;
30 ret
= i2c_master_send(st
->i2c
, st
->tx
, 3);
31 mutex_unlock(&st
->buf_lock
);
36 static int ade7854_i2c_write_reg_16(struct device
*dev
,
41 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
42 struct ade7854_state
*st
= iio_priv(indio_dev
);
44 mutex_lock(&st
->buf_lock
);
45 st
->tx
[0] = (reg_address
>> 8) & 0xFF;
46 st
->tx
[1] = reg_address
& 0xFF;
47 st
->tx
[2] = (value
>> 8) & 0xFF;
48 st
->tx
[3] = value
& 0xFF;
50 ret
= i2c_master_send(st
->i2c
, st
->tx
, 4);
51 mutex_unlock(&st
->buf_lock
);
56 static int ade7854_i2c_write_reg_24(struct device
*dev
,
61 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
62 struct ade7854_state
*st
= iio_priv(indio_dev
);
64 mutex_lock(&st
->buf_lock
);
65 st
->tx
[0] = (reg_address
>> 8) & 0xFF;
66 st
->tx
[1] = reg_address
& 0xFF;
67 st
->tx
[2] = (value
>> 16) & 0xFF;
68 st
->tx
[3] = (value
>> 8) & 0xFF;
69 st
->tx
[4] = value
& 0xFF;
71 ret
= i2c_master_send(st
->i2c
, st
->tx
, 5);
72 mutex_unlock(&st
->buf_lock
);
77 static int ade7854_i2c_write_reg_32(struct device
*dev
,
82 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
83 struct ade7854_state
*st
= iio_priv(indio_dev
);
85 mutex_lock(&st
->buf_lock
);
86 st
->tx
[0] = (reg_address
>> 8) & 0xFF;
87 st
->tx
[1] = reg_address
& 0xFF;
88 st
->tx
[2] = (value
>> 24) & 0xFF;
89 st
->tx
[3] = (value
>> 16) & 0xFF;
90 st
->tx
[4] = (value
>> 8) & 0xFF;
91 st
->tx
[5] = value
& 0xFF;
93 ret
= i2c_master_send(st
->i2c
, st
->tx
, 6);
94 mutex_unlock(&st
->buf_lock
);
99 static int ade7854_i2c_read_reg_8(struct device
*dev
,
103 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
104 struct ade7854_state
*st
= iio_priv(indio_dev
);
107 mutex_lock(&st
->buf_lock
);
108 st
->tx
[0] = (reg_address
>> 8) & 0xFF;
109 st
->tx
[1] = reg_address
& 0xFF;
111 ret
= i2c_master_send(st
->i2c
, st
->tx
, 2);
115 ret
= i2c_master_recv(st
->i2c
, st
->rx
, 1);
121 mutex_unlock(&st
->buf_lock
);
125 static int ade7854_i2c_read_reg_16(struct device
*dev
,
129 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
130 struct ade7854_state
*st
= iio_priv(indio_dev
);
133 mutex_lock(&st
->buf_lock
);
134 st
->tx
[0] = (reg_address
>> 8) & 0xFF;
135 st
->tx
[1] = reg_address
& 0xFF;
137 ret
= i2c_master_send(st
->i2c
, st
->tx
, 2);
141 ret
= i2c_master_recv(st
->i2c
, st
->rx
, 2);
145 *val
= (st
->rx
[0] << 8) | st
->rx
[1];
147 mutex_unlock(&st
->buf_lock
);
151 static int ade7854_i2c_read_reg_24(struct device
*dev
,
155 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
156 struct ade7854_state
*st
= iio_priv(indio_dev
);
159 mutex_lock(&st
->buf_lock
);
160 st
->tx
[0] = (reg_address
>> 8) & 0xFF;
161 st
->tx
[1] = reg_address
& 0xFF;
163 ret
= i2c_master_send(st
->i2c
, st
->tx
, 2);
167 ret
= i2c_master_recv(st
->i2c
, st
->rx
, 3);
171 *val
= (st
->rx
[0] << 16) | (st
->rx
[1] << 8) | st
->rx
[2];
173 mutex_unlock(&st
->buf_lock
);
177 static int ade7854_i2c_read_reg_32(struct device
*dev
,
181 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
182 struct ade7854_state
*st
= iio_priv(indio_dev
);
185 mutex_lock(&st
->buf_lock
);
186 st
->tx
[0] = (reg_address
>> 8) & 0xFF;
187 st
->tx
[1] = reg_address
& 0xFF;
189 ret
= i2c_master_send(st
->i2c
, st
->tx
, 2);
193 ret
= i2c_master_recv(st
->i2c
, st
->rx
, 3);
197 *val
= (st
->rx
[0] << 24) | (st
->rx
[1] << 16) | (st
->rx
[2] << 8) | st
->rx
[3];
199 mutex_unlock(&st
->buf_lock
);
203 static int __devinit
ade7854_i2c_probe(struct i2c_client
*client
,
204 const struct i2c_device_id
*id
)
207 struct ade7854_state
*st
;
208 struct iio_dev
*indio_dev
;
210 indio_dev
= iio_allocate_device(sizeof(*st
));
211 if (indio_dev
== NULL
)
213 st
= iio_priv(indio_dev
);
214 i2c_set_clientdata(client
, indio_dev
);
215 st
->read_reg_8
= ade7854_i2c_read_reg_8
;
216 st
->read_reg_16
= ade7854_i2c_read_reg_16
;
217 st
->read_reg_24
= ade7854_i2c_read_reg_24
;
218 st
->read_reg_32
= ade7854_i2c_read_reg_32
;
219 st
->write_reg_8
= ade7854_i2c_write_reg_8
;
220 st
->write_reg_16
= ade7854_i2c_write_reg_16
;
221 st
->write_reg_24
= ade7854_i2c_write_reg_24
;
222 st
->write_reg_32
= ade7854_i2c_write_reg_32
;
224 st
->irq
= client
->irq
;
226 ret
= ade7854_probe(indio_dev
, &client
->dev
);
228 iio_free_device(indio_dev
);
233 static int __devexit
ade7854_i2c_remove(struct i2c_client
*client
)
235 return ade7854_remove(i2c_get_clientdata(client
));
238 static const struct i2c_device_id ade7854_id
[] = {
245 MODULE_DEVICE_TABLE(i2c
, ade7854_id
);
247 static struct i2c_driver ade7854_i2c_driver
= {
251 .probe
= ade7854_i2c_probe
,
252 .remove
= __devexit_p(ade7854_i2c_remove
),
253 .id_table
= ade7854_id
,
256 static __init
int ade7854_i2c_init(void)
258 return i2c_add_driver(&ade7854_i2c_driver
);
260 module_init(ade7854_i2c_init
);
262 static __exit
void ade7854_i2c_exit(void)
264 i2c_del_driver(&ade7854_i2c_driver
);
266 module_exit(ade7854_i2c_exit
);
269 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
270 MODULE_DESCRIPTION("Analog Devices ADE7854/58/68/78 Polyphase Multifunction Energy Metering IC I2C Driver");
271 MODULE_LICENSE("GPL v2");