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>
17 static int ade7854_spi_write_reg_8(struct device
*dev
,
22 struct spi_message msg
;
23 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
24 struct ade7854_state
*st
= iio_priv(indio_dev
);
25 struct spi_transfer xfer
= {
31 mutex_lock(&st
->buf_lock
);
32 st
->tx
[0] = ADE7854_WRITE_REG
;
33 st
->tx
[1] = (reg_address
>> 8) & 0xFF;
34 st
->tx
[2] = reg_address
& 0xFF;
35 st
->tx
[3] = value
& 0xFF;
37 spi_message_init(&msg
);
38 spi_message_add_tail(&xfer
, &msg
);
39 ret
= spi_sync(st
->spi
, &msg
);
40 mutex_unlock(&st
->buf_lock
);
45 static int ade7854_spi_write_reg_16(struct device
*dev
,
50 struct spi_message msg
;
51 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
52 struct ade7854_state
*st
= iio_priv(indio_dev
);
53 struct spi_transfer xfer
= {
59 mutex_lock(&st
->buf_lock
);
60 st
->tx
[0] = ADE7854_WRITE_REG
;
61 st
->tx
[1] = (reg_address
>> 8) & 0xFF;
62 st
->tx
[2] = reg_address
& 0xFF;
63 st
->tx
[3] = (value
>> 8) & 0xFF;
64 st
->tx
[4] = value
& 0xFF;
66 spi_message_init(&msg
);
67 spi_message_add_tail(&xfer
, &msg
);
68 ret
= spi_sync(st
->spi
, &msg
);
69 mutex_unlock(&st
->buf_lock
);
74 static int ade7854_spi_write_reg_24(struct device
*dev
,
79 struct spi_message msg
;
80 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
81 struct ade7854_state
*st
= iio_priv(indio_dev
);
82 struct spi_transfer xfer
= {
88 mutex_lock(&st
->buf_lock
);
89 st
->tx
[0] = ADE7854_WRITE_REG
;
90 st
->tx
[1] = (reg_address
>> 8) & 0xFF;
91 st
->tx
[2] = reg_address
& 0xFF;
92 st
->tx
[3] = (value
>> 16) & 0xFF;
93 st
->tx
[4] = (value
>> 8) & 0xFF;
94 st
->tx
[5] = value
& 0xFF;
96 spi_message_init(&msg
);
97 spi_message_add_tail(&xfer
, &msg
);
98 ret
= spi_sync(st
->spi
, &msg
);
99 mutex_unlock(&st
->buf_lock
);
104 static int ade7854_spi_write_reg_32(struct device
*dev
,
109 struct spi_message msg
;
110 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
111 struct ade7854_state
*st
= iio_priv(indio_dev
);
112 struct spi_transfer xfer
= {
118 mutex_lock(&st
->buf_lock
);
119 st
->tx
[0] = ADE7854_WRITE_REG
;
120 st
->tx
[1] = (reg_address
>> 8) & 0xFF;
121 st
->tx
[2] = reg_address
& 0xFF;
122 st
->tx
[3] = (value
>> 24) & 0xFF;
123 st
->tx
[4] = (value
>> 16) & 0xFF;
124 st
->tx
[5] = (value
>> 8) & 0xFF;
125 st
->tx
[6] = value
& 0xFF;
127 spi_message_init(&msg
);
128 spi_message_add_tail(&xfer
, &msg
);
129 ret
= spi_sync(st
->spi
, &msg
);
130 mutex_unlock(&st
->buf_lock
);
135 static int ade7854_spi_read_reg_8(struct device
*dev
,
139 struct spi_message msg
;
140 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
141 struct ade7854_state
*st
= iio_priv(indio_dev
);
143 struct spi_transfer xfers
[] = {
155 mutex_lock(&st
->buf_lock
);
157 st
->tx
[0] = ADE7854_READ_REG
;
158 st
->tx
[1] = (reg_address
>> 8) & 0xFF;
159 st
->tx
[2] = reg_address
& 0xFF;
161 spi_message_init(&msg
);
162 spi_message_add_tail(&xfers
[0], &msg
);
163 spi_message_add_tail(&xfers
[1], &msg
);
164 ret
= spi_sync(st
->spi
, &msg
);
166 dev_err(&st
->spi
->dev
, "problem when reading 8 bit register 0x%02X",
173 mutex_unlock(&st
->buf_lock
);
177 static int ade7854_spi_read_reg_16(struct device
*dev
,
181 struct spi_message msg
;
182 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
183 struct ade7854_state
*st
= iio_priv(indio_dev
);
185 struct spi_transfer xfers
[] = {
197 mutex_lock(&st
->buf_lock
);
198 st
->tx
[0] = ADE7854_READ_REG
;
199 st
->tx
[1] = (reg_address
>> 8) & 0xFF;
200 st
->tx
[2] = reg_address
& 0xFF;
202 spi_message_init(&msg
);
203 spi_message_add_tail(&xfers
[0], &msg
);
204 spi_message_add_tail(&xfers
[1], &msg
);
205 ret
= spi_sync(st
->spi
, &msg
);
207 dev_err(&st
->spi
->dev
, "problem when reading 16 bit register 0x%02X",
211 *val
= be16_to_cpup((const __be16
*)st
->rx
);
214 mutex_unlock(&st
->buf_lock
);
218 static int ade7854_spi_read_reg_24(struct device
*dev
,
222 struct spi_message msg
;
223 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
224 struct ade7854_state
*st
= iio_priv(indio_dev
);
226 struct spi_transfer xfers
[] = {
238 mutex_lock(&st
->buf_lock
);
240 st
->tx
[0] = ADE7854_READ_REG
;
241 st
->tx
[1] = (reg_address
>> 8) & 0xFF;
242 st
->tx
[2] = reg_address
& 0xFF;
244 spi_message_init(&msg
);
245 spi_message_add_tail(&xfers
[0], &msg
);
246 spi_message_add_tail(&xfers
[1], &msg
);
247 ret
= spi_sync(st
->spi
, &msg
);
249 dev_err(&st
->spi
->dev
, "problem when reading 24 bit register 0x%02X",
253 *val
= (st
->rx
[0] << 16) | (st
->rx
[1] << 8) | st
->rx
[2];
256 mutex_unlock(&st
->buf_lock
);
260 static int ade7854_spi_read_reg_32(struct device
*dev
,
264 struct spi_message msg
;
265 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
266 struct ade7854_state
*st
= iio_priv(indio_dev
);
268 struct spi_transfer xfers
[] = {
280 mutex_lock(&st
->buf_lock
);
282 st
->tx
[0] = ADE7854_READ_REG
;
283 st
->tx
[1] = (reg_address
>> 8) & 0xFF;
284 st
->tx
[2] = reg_address
& 0xFF;
286 spi_message_init(&msg
);
287 spi_message_add_tail(&xfers
[0], &msg
);
288 spi_message_add_tail(&xfers
[1], &msg
);
289 ret
= spi_sync(st
->spi
, &msg
);
291 dev_err(&st
->spi
->dev
, "problem when reading 32 bit register 0x%02X",
295 *val
= be32_to_cpup((const __be32
*)st
->rx
);
298 mutex_unlock(&st
->buf_lock
);
302 static int __devinit
ade7854_spi_probe(struct spi_device
*spi
)
305 struct ade7854_state
*st
;
306 struct iio_dev
*indio_dev
;
308 indio_dev
= iio_allocate_device(sizeof(*st
));
309 if (indio_dev
== NULL
)
311 st
= iio_priv(indio_dev
);
312 spi_set_drvdata(spi
, indio_dev
);
313 st
->read_reg_8
= ade7854_spi_read_reg_8
;
314 st
->read_reg_16
= ade7854_spi_read_reg_16
;
315 st
->read_reg_24
= ade7854_spi_read_reg_24
;
316 st
->read_reg_32
= ade7854_spi_read_reg_32
;
317 st
->write_reg_8
= ade7854_spi_write_reg_8
;
318 st
->write_reg_16
= ade7854_spi_write_reg_16
;
319 st
->write_reg_24
= ade7854_spi_write_reg_24
;
320 st
->write_reg_32
= ade7854_spi_write_reg_32
;
325 ret
= ade7854_probe(indio_dev
, &spi
->dev
);
327 iio_free_device(indio_dev
);
332 static int ade7854_spi_remove(struct spi_device
*spi
)
334 ade7854_remove(spi_get_drvdata(spi
));
338 static const struct spi_device_id ade7854_id
[] = {
346 static struct spi_driver ade7854_driver
= {
349 .owner
= THIS_MODULE
,
351 .probe
= ade7854_spi_probe
,
352 .remove
= __devexit_p(ade7854_spi_remove
),
353 .id_table
= ade7854_id
,
356 static __init
int ade7854_init(void)
358 return spi_register_driver(&ade7854_driver
);
360 module_init(ade7854_init
);
362 static __exit
void ade7854_exit(void)
364 spi_unregister_driver(&ade7854_driver
);
366 module_exit(ade7854_exit
);
368 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
369 MODULE_DESCRIPTION("Analog Devices ADE7854/58/68/78 SPI Driver");
370 MODULE_LICENSE("GPL v2");