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_dev_get_devdata(indio_dev);
25 struct spi_transfer xfers[] = {
33 mutex_lock(&st->buf_lock);
34 st->tx[0] = ADE7854_WRITE_REG;
35 st->tx[1] = (reg_address >> 8) & 0xFF;
36 st->tx[2] = reg_address & 0xFF;
37 st->tx[3] = value & 0xFF;
39 spi_message_init(&msg);
40 spi_message_add_tail(xfers, &msg);
41 ret = spi_sync(st->spi, &msg);
42 mutex_unlock(&st->buf_lock);
47 static int ade7854_spi_write_reg_16(struct device *dev,
52 struct spi_message msg;
53 struct iio_dev *indio_dev = dev_get_drvdata(dev);
54 struct ade7854_state *st = iio_dev_get_devdata(indio_dev);
55 struct spi_transfer xfers[] = {
63 mutex_lock(&st->buf_lock);
64 st->tx[0] = ADE7854_WRITE_REG;
65 st->tx[1] = (reg_address >> 8) & 0xFF;
66 st->tx[2] = reg_address & 0xFF;
67 st->tx[3] = (value >> 8) & 0xFF;
68 st->tx[4] = value & 0xFF;
70 spi_message_init(&msg);
71 spi_message_add_tail(xfers, &msg);
72 ret = spi_sync(st->spi, &msg);
73 mutex_unlock(&st->buf_lock);
78 static int ade7854_spi_write_reg_24(struct device *dev,
83 struct spi_message msg;
84 struct iio_dev *indio_dev = dev_get_drvdata(dev);
85 struct ade7854_state *st = iio_dev_get_devdata(indio_dev);
86 struct spi_transfer xfers[] = {
94 mutex_lock(&st->buf_lock);
95 st->tx[0] = ADE7854_WRITE_REG;
96 st->tx[1] = (reg_address >> 8) & 0xFF;
97 st->tx[2] = reg_address & 0xFF;
98 st->tx[3] = (value >> 16) & 0xFF;
99 st->tx[4] = (value >> 8) & 0xFF;
100 st->tx[5] = value & 0xFF;
102 spi_message_init(&msg);
103 spi_message_add_tail(xfers, &msg);
104 ret = spi_sync(st->spi, &msg);
105 mutex_unlock(&st->buf_lock);
110 static int ade7854_spi_write_reg_32(struct device *dev,
115 struct spi_message msg;
116 struct iio_dev *indio_dev = dev_get_drvdata(dev);
117 struct ade7854_state *st = iio_dev_get_devdata(indio_dev);
118 struct spi_transfer xfers[] = {
126 mutex_lock(&st->buf_lock);
127 st->tx[0] = ADE7854_WRITE_REG;
128 st->tx[1] = (reg_address >> 8) & 0xFF;
129 st->tx[2] = reg_address & 0xFF;
130 st->tx[3] = (value >> 24) & 0xFF;
131 st->tx[4] = (value >> 16) & 0xFF;
132 st->tx[5] = (value >> 8) & 0xFF;
133 st->tx[6] = value & 0xFF;
135 spi_message_init(&msg);
136 spi_message_add_tail(xfers, &msg);
137 ret = spi_sync(st->spi, &msg);
138 mutex_unlock(&st->buf_lock);
143 static int ade7854_spi_read_reg_8(struct device *dev,
147 struct spi_message msg;
148 struct iio_dev *indio_dev = dev_get_drvdata(dev);
149 struct ade7854_state *st = iio_dev_get_devdata(indio_dev);
151 struct spi_transfer xfers[] = {
159 mutex_lock(&st->buf_lock);
161 st->tx[0] = ADE7854_READ_REG;
162 st->tx[1] = (reg_address >> 8) & 0xFF;
163 st->tx[2] = reg_address & 0xFF;
166 spi_message_init(&msg);
167 spi_message_add_tail(xfers, &msg);
168 ret = spi_sync(st->spi, &msg);
170 dev_err(&st->spi->dev, "problem when reading 8 bit register 0x%02X",
177 mutex_unlock(&st->buf_lock);
181 static int ade7854_spi_read_reg_16(struct device *dev,
185 struct spi_message msg;
186 struct iio_dev *indio_dev = dev_get_drvdata(dev);
187 struct ade7854_state *st = iio_dev_get_devdata(indio_dev);
189 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;
204 spi_message_init(&msg);
205 spi_message_add_tail(xfers, &msg);
206 ret = spi_sync(st->spi, &msg);
208 dev_err(&st->spi->dev, "problem when reading 16 bit register 0x%02X",
212 *val = (st->rx[3] << 8) | st->rx[4];
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_dev_get_devdata(indio_dev);
227 struct spi_transfer xfers[] = {
235 mutex_lock(&st->buf_lock);
237 st->tx[0] = ADE7854_READ_REG;
238 st->tx[1] = (reg_address >> 8) & 0xFF;
239 st->tx[2] = reg_address & 0xFF;
244 spi_message_init(&msg);
245 spi_message_add_tail(xfers, &msg);
246 ret = spi_sync(st->spi, &msg);
248 dev_err(&st->spi->dev, "problem when reading 24 bit register 0x%02X",
252 *val = (st->rx[3] << 16) | (st->rx[4] << 8) | st->rx[5];
255 mutex_unlock(&st->buf_lock);
259 static int ade7854_spi_read_reg_32(struct device *dev,
263 struct spi_message msg;
264 struct iio_dev *indio_dev = dev_get_drvdata(dev);
265 struct ade7854_state *st = iio_dev_get_devdata(indio_dev);
267 struct spi_transfer xfers[] = {
275 mutex_lock(&st->buf_lock);
277 st->tx[0] = ADE7854_READ_REG;
278 st->tx[1] = (reg_address >> 8) & 0xFF;
279 st->tx[2] = reg_address & 0xFF;
285 spi_message_init(&msg);
286 spi_message_add_tail(xfers, &msg);
287 ret = spi_sync(st->spi, &msg);
289 dev_err(&st->spi->dev, "problem when reading 32 bit register 0x%02X",
293 *val = (st->rx[3] << 24) | (st->rx[4] << 16) | (st->rx[5] << 8) | st->rx[6];
296 mutex_unlock(&st->buf_lock);
300 static int __devinit ade7854_spi_probe(struct spi_device *spi)
303 struct ade7854_state *st = kzalloc(sizeof *st, GFP_KERNEL);
309 spi_set_drvdata(spi, st);
310 st->read_reg_8 = ade7854_spi_read_reg_8;
311 st->read_reg_16 = ade7854_spi_read_reg_16;
312 st->read_reg_24 = ade7854_spi_read_reg_24;
313 st->read_reg_32 = ade7854_spi_read_reg_32;
314 st->write_reg_8 = ade7854_spi_write_reg_8;
315 st->write_reg_16 = ade7854_spi_write_reg_16;
316 st->write_reg_24 = ade7854_spi_write_reg_24;
317 st->write_reg_32 = ade7854_spi_write_reg_32;
321 ret = ade7854_probe(st, &spi->dev);
330 static int ade7854_spi_remove(struct spi_device *spi)
332 ade7854_remove(spi_get_drvdata(spi));
337 static struct spi_driver ade7854_driver = {
340 .owner = THIS_MODULE,
342 .probe = ade7854_spi_probe,
343 .remove = __devexit_p(ade7854_spi_remove),
346 static __init int ade7854_init(void)
348 return spi_register_driver(&ade7854_driver);
350 module_init(ade7854_init);
352 static __exit void ade7854_exit(void)
354 spi_unregister_driver(&ade7854_driver);
356 module_exit(ade7854_exit);
358 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
359 MODULE_DESCRIPTION("Analog Devices ADE7854/58/68/78 Polyphase Multifunction Energy Metering IC SPI Driver");
360 MODULE_LICENSE("GPL v2");