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>
15 #include <linux/iio/iio.h>
18 static int ade7854_spi_write_reg_8(struct device *dev,
23 struct iio_dev *indio_dev = dev_to_iio_dev(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 ret = spi_sync_transfer(st->spi, &xfer, 1);
38 mutex_unlock(&st->buf_lock);
43 static int ade7854_spi_write_reg_16(struct device *dev,
48 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
49 struct ade7854_state *st = iio_priv(indio_dev);
50 struct spi_transfer xfer = {
56 mutex_lock(&st->buf_lock);
57 st->tx[0] = ADE7854_WRITE_REG;
58 st->tx[1] = (reg_address >> 8) & 0xFF;
59 st->tx[2] = reg_address & 0xFF;
60 st->tx[3] = (value >> 8) & 0xFF;
61 st->tx[4] = value & 0xFF;
63 ret = spi_sync_transfer(st->spi, &xfer, 1);
64 mutex_unlock(&st->buf_lock);
69 static int ade7854_spi_write_reg_24(struct device *dev,
74 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
75 struct ade7854_state *st = iio_priv(indio_dev);
76 struct spi_transfer xfer = {
82 mutex_lock(&st->buf_lock);
83 st->tx[0] = ADE7854_WRITE_REG;
84 st->tx[1] = (reg_address >> 8) & 0xFF;
85 st->tx[2] = reg_address & 0xFF;
86 st->tx[3] = (value >> 16) & 0xFF;
87 st->tx[4] = (value >> 8) & 0xFF;
88 st->tx[5] = value & 0xFF;
90 ret = spi_sync_transfer(st->spi, &xfer, 1);
91 mutex_unlock(&st->buf_lock);
96 static int ade7854_spi_write_reg_32(struct device *dev,
101 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
102 struct ade7854_state *st = iio_priv(indio_dev);
103 struct spi_transfer xfer = {
109 mutex_lock(&st->buf_lock);
110 st->tx[0] = ADE7854_WRITE_REG;
111 st->tx[1] = (reg_address >> 8) & 0xFF;
112 st->tx[2] = reg_address & 0xFF;
113 st->tx[3] = (value >> 24) & 0xFF;
114 st->tx[4] = (value >> 16) & 0xFF;
115 st->tx[5] = (value >> 8) & 0xFF;
116 st->tx[6] = value & 0xFF;
118 ret = spi_sync_transfer(st->spi, &xfer, 1);
119 mutex_unlock(&st->buf_lock);
124 static int ade7854_spi_read_reg_8(struct device *dev,
128 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
129 struct ade7854_state *st = iio_priv(indio_dev);
131 struct spi_transfer xfers[] = {
143 mutex_lock(&st->buf_lock);
145 st->tx[0] = ADE7854_READ_REG;
146 st->tx[1] = (reg_address >> 8) & 0xFF;
147 st->tx[2] = reg_address & 0xFF;
149 ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers));
151 dev_err(&st->spi->dev, "problem when reading 8 bit register 0x%02X",
158 mutex_unlock(&st->buf_lock);
162 static int ade7854_spi_read_reg_16(struct device *dev,
166 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
167 struct ade7854_state *st = iio_priv(indio_dev);
169 struct spi_transfer xfers[] = {
181 mutex_lock(&st->buf_lock);
182 st->tx[0] = ADE7854_READ_REG;
183 st->tx[1] = (reg_address >> 8) & 0xFF;
184 st->tx[2] = reg_address & 0xFF;
186 ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers));
188 dev_err(&st->spi->dev, "problem when reading 16 bit register 0x%02X",
192 *val = be16_to_cpup((const __be16 *)st->rx);
195 mutex_unlock(&st->buf_lock);
199 static int ade7854_spi_read_reg_24(struct device *dev,
203 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
204 struct ade7854_state *st = iio_priv(indio_dev);
206 struct spi_transfer xfers[] = {
218 mutex_lock(&st->buf_lock);
220 st->tx[0] = ADE7854_READ_REG;
221 st->tx[1] = (reg_address >> 8) & 0xFF;
222 st->tx[2] = reg_address & 0xFF;
224 ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers));
226 dev_err(&st->spi->dev, "problem when reading 24 bit register 0x%02X",
230 *val = (st->rx[0] << 16) | (st->rx[1] << 8) | st->rx[2];
233 mutex_unlock(&st->buf_lock);
237 static int ade7854_spi_read_reg_32(struct device *dev,
241 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
242 struct ade7854_state *st = iio_priv(indio_dev);
244 struct spi_transfer xfers[] = {
256 mutex_lock(&st->buf_lock);
258 st->tx[0] = ADE7854_READ_REG;
259 st->tx[1] = (reg_address >> 8) & 0xFF;
260 st->tx[2] = reg_address & 0xFF;
262 ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers));
264 dev_err(&st->spi->dev, "problem when reading 32 bit register 0x%02X",
268 *val = be32_to_cpup((const __be32 *)st->rx);
271 mutex_unlock(&st->buf_lock);
275 static int ade7854_spi_probe(struct spi_device *spi)
277 struct ade7854_state *st;
278 struct iio_dev *indio_dev;
280 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
283 st = iio_priv(indio_dev);
284 spi_set_drvdata(spi, indio_dev);
285 st->read_reg_8 = ade7854_spi_read_reg_8;
286 st->read_reg_16 = ade7854_spi_read_reg_16;
287 st->read_reg_24 = ade7854_spi_read_reg_24;
288 st->read_reg_32 = ade7854_spi_read_reg_32;
289 st->write_reg_8 = ade7854_spi_write_reg_8;
290 st->write_reg_16 = ade7854_spi_write_reg_16;
291 st->write_reg_24 = ade7854_spi_write_reg_24;
292 st->write_reg_32 = ade7854_spi_write_reg_32;
296 return ade7854_probe(indio_dev, &spi->dev);
299 static int ade7854_spi_remove(struct spi_device *spi)
301 ade7854_remove(spi_get_drvdata(spi));
305 static const struct spi_device_id ade7854_id[] = {
312 MODULE_DEVICE_TABLE(spi, ade7854_id);
314 static struct spi_driver ade7854_driver = {
318 .probe = ade7854_spi_probe,
319 .remove = ade7854_spi_remove,
320 .id_table = ade7854_id,
322 module_spi_driver(ade7854_driver);
324 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
325 MODULE_DESCRIPTION("Analog Devices ADE7854/58/68/78 SPI Driver");
326 MODULE_LICENSE("GPL v2");