2 * ADE7854/58/68/78 Polyphase Multifunction Energy Metering IC Driver (SPI 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/spi/spi.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
18 static int ade7854_spi_write_reg_8(struct device
*dev
,
23 struct spi_message msg
;
24 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
25 struct ade7854_state
*st
= iio_priv(indio_dev
);
26 struct spi_transfer xfer
= {
32 mutex_lock(&st
->buf_lock
);
33 st
->tx
[0] = ADE7854_WRITE_REG
;
34 st
->tx
[1] = (reg_address
>> 8) & 0xFF;
35 st
->tx
[2] = reg_address
& 0xFF;
36 st
->tx
[3] = value
& 0xFF;
38 spi_message_init(&msg
);
39 spi_message_add_tail(&xfer
, &msg
);
40 ret
= spi_sync(st
->spi
, &msg
);
41 mutex_unlock(&st
->buf_lock
);
46 static int ade7854_spi_write_reg_16(struct device
*dev
,
51 struct spi_message msg
;
52 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
53 struct ade7854_state
*st
= iio_priv(indio_dev
);
54 struct spi_transfer xfer
= {
60 mutex_lock(&st
->buf_lock
);
61 st
->tx
[0] = ADE7854_WRITE_REG
;
62 st
->tx
[1] = (reg_address
>> 8) & 0xFF;
63 st
->tx
[2] = reg_address
& 0xFF;
64 st
->tx
[3] = (value
>> 8) & 0xFF;
65 st
->tx
[4] = value
& 0xFF;
67 spi_message_init(&msg
);
68 spi_message_add_tail(&xfer
, &msg
);
69 ret
= spi_sync(st
->spi
, &msg
);
70 mutex_unlock(&st
->buf_lock
);
75 static int ade7854_spi_write_reg_24(struct device
*dev
,
80 struct spi_message msg
;
81 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
82 struct ade7854_state
*st
= iio_priv(indio_dev
);
83 struct spi_transfer xfer
= {
89 mutex_lock(&st
->buf_lock
);
90 st
->tx
[0] = ADE7854_WRITE_REG
;
91 st
->tx
[1] = (reg_address
>> 8) & 0xFF;
92 st
->tx
[2] = reg_address
& 0xFF;
93 st
->tx
[3] = (value
>> 16) & 0xFF;
94 st
->tx
[4] = (value
>> 8) & 0xFF;
95 st
->tx
[5] = value
& 0xFF;
97 spi_message_init(&msg
);
98 spi_message_add_tail(&xfer
, &msg
);
99 ret
= spi_sync(st
->spi
, &msg
);
100 mutex_unlock(&st
->buf_lock
);
105 static int ade7854_spi_write_reg_32(struct device
*dev
,
110 struct spi_message msg
;
111 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
112 struct ade7854_state
*st
= iio_priv(indio_dev
);
113 struct spi_transfer xfer
= {
119 mutex_lock(&st
->buf_lock
);
120 st
->tx
[0] = ADE7854_WRITE_REG
;
121 st
->tx
[1] = (reg_address
>> 8) & 0xFF;
122 st
->tx
[2] = reg_address
& 0xFF;
123 st
->tx
[3] = (value
>> 24) & 0xFF;
124 st
->tx
[4] = (value
>> 16) & 0xFF;
125 st
->tx
[5] = (value
>> 8) & 0xFF;
126 st
->tx
[6] = value
& 0xFF;
128 spi_message_init(&msg
);
129 spi_message_add_tail(&xfer
, &msg
);
130 ret
= spi_sync(st
->spi
, &msg
);
131 mutex_unlock(&st
->buf_lock
);
136 static int ade7854_spi_read_reg_8(struct device
*dev
,
140 struct spi_message msg
;
141 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
142 struct ade7854_state
*st
= iio_priv(indio_dev
);
144 struct spi_transfer xfers
[] = {
156 mutex_lock(&st
->buf_lock
);
158 st
->tx
[0] = ADE7854_READ_REG
;
159 st
->tx
[1] = (reg_address
>> 8) & 0xFF;
160 st
->tx
[2] = reg_address
& 0xFF;
162 spi_message_init(&msg
);
163 spi_message_add_tail(&xfers
[0], &msg
);
164 spi_message_add_tail(&xfers
[1], &msg
);
165 ret
= spi_sync(st
->spi
, &msg
);
167 dev_err(&st
->spi
->dev
, "problem when reading 8 bit register 0x%02X",
174 mutex_unlock(&st
->buf_lock
);
178 static int ade7854_spi_read_reg_16(struct device
*dev
,
182 struct spi_message msg
;
183 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
184 struct ade7854_state
*st
= iio_priv(indio_dev
);
186 struct spi_transfer xfers
[] = {
198 mutex_lock(&st
->buf_lock
);
199 st
->tx
[0] = ADE7854_READ_REG
;
200 st
->tx
[1] = (reg_address
>> 8) & 0xFF;
201 st
->tx
[2] = reg_address
& 0xFF;
203 spi_message_init(&msg
);
204 spi_message_add_tail(&xfers
[0], &msg
);
205 spi_message_add_tail(&xfers
[1], &msg
);
206 ret
= spi_sync(st
->spi
, &msg
);
208 dev_err(&st
->spi
->dev
, "problem when reading 16 bit register 0x%02X",
212 *val
= be16_to_cpup((const __be16
*)st
->rx
);
215 mutex_unlock(&st
->buf_lock
);
219 static int ade7854_spi_read_reg_24(struct device
*dev
,
223 struct spi_message msg
;
224 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
225 struct ade7854_state
*st
= iio_priv(indio_dev
);
227 struct spi_transfer xfers
[] = {
239 mutex_lock(&st
->buf_lock
);
241 st
->tx
[0] = ADE7854_READ_REG
;
242 st
->tx
[1] = (reg_address
>> 8) & 0xFF;
243 st
->tx
[2] = reg_address
& 0xFF;
245 spi_message_init(&msg
);
246 spi_message_add_tail(&xfers
[0], &msg
);
247 spi_message_add_tail(&xfers
[1], &msg
);
248 ret
= spi_sync(st
->spi
, &msg
);
250 dev_err(&st
->spi
->dev
, "problem when reading 24 bit register 0x%02X",
254 *val
= (st
->rx
[0] << 16) | (st
->rx
[1] << 8) | st
->rx
[2];
257 mutex_unlock(&st
->buf_lock
);
261 static int ade7854_spi_read_reg_32(struct device
*dev
,
265 struct spi_message msg
;
266 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
267 struct ade7854_state
*st
= iio_priv(indio_dev
);
269 struct spi_transfer xfers
[] = {
281 mutex_lock(&st
->buf_lock
);
283 st
->tx
[0] = ADE7854_READ_REG
;
284 st
->tx
[1] = (reg_address
>> 8) & 0xFF;
285 st
->tx
[2] = reg_address
& 0xFF;
287 spi_message_init(&msg
);
288 spi_message_add_tail(&xfers
[0], &msg
);
289 spi_message_add_tail(&xfers
[1], &msg
);
290 ret
= spi_sync(st
->spi
, &msg
);
292 dev_err(&st
->spi
->dev
, "problem when reading 32 bit register 0x%02X",
296 *val
= be32_to_cpup((const __be32
*)st
->rx
);
299 mutex_unlock(&st
->buf_lock
);
303 static int __devinit
ade7854_spi_probe(struct spi_device
*spi
)
306 struct ade7854_state
*st
;
307 struct iio_dev
*indio_dev
;
309 indio_dev
= iio_allocate_device(sizeof(*st
));
310 if (indio_dev
== NULL
)
312 st
= iio_priv(indio_dev
);
313 spi_set_drvdata(spi
, indio_dev
);
314 st
->read_reg_8
= ade7854_spi_read_reg_8
;
315 st
->read_reg_16
= ade7854_spi_read_reg_16
;
316 st
->read_reg_24
= ade7854_spi_read_reg_24
;
317 st
->read_reg_32
= ade7854_spi_read_reg_32
;
318 st
->write_reg_8
= ade7854_spi_write_reg_8
;
319 st
->write_reg_16
= ade7854_spi_write_reg_16
;
320 st
->write_reg_24
= ade7854_spi_write_reg_24
;
321 st
->write_reg_32
= ade7854_spi_write_reg_32
;
326 ret
= ade7854_probe(indio_dev
, &spi
->dev
);
328 iio_free_device(indio_dev
);
333 static int ade7854_spi_remove(struct spi_device
*spi
)
335 ade7854_remove(spi_get_drvdata(spi
));
339 static const struct spi_device_id ade7854_id
[] = {
346 MODULE_DEVICE_TABLE(spi
, ade7854_id
);
348 static struct spi_driver ade7854_driver
= {
351 .owner
= THIS_MODULE
,
353 .probe
= ade7854_spi_probe
,
354 .remove
= __devexit_p(ade7854_spi_remove
),
355 .id_table
= ade7854_id
,
357 module_spi_driver(ade7854_driver
);
359 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
360 MODULE_DESCRIPTION("Analog Devices ADE7854/58/68/78 SPI Driver");
361 MODULE_LICENSE("GPL v2");