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(struct device *dev,
24 struct iio_dev *indio_dev = dev_to_iio_dev(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;
38 st->tx[3] = val & 0xFF;
42 st->tx[3] = (val >> 8) & 0xFF;
43 st->tx[4] = val & 0xFF;
47 st->tx[3] = (val >> 16) & 0xFF;
48 st->tx[4] = (val >> 8) & 0xFF;
49 st->tx[5] = val & 0xFF;
53 st->tx[3] = (val >> 24) & 0xFF;
54 st->tx[4] = (val >> 16) & 0xFF;
55 st->tx[5] = (val >> 8) & 0xFF;
56 st->tx[6] = val & 0xFF;
63 ret = spi_sync_transfer(st->spi, &xfer, 1);
65 mutex_unlock(&st->buf_lock);
70 static int ade7854_spi_read_reg(struct device *dev,
75 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
76 struct ade7854_state *st = iio_priv(indio_dev);
78 struct spi_transfer xfers[] = {
90 mutex_lock(&st->buf_lock);
92 st->tx[0] = ADE7854_READ_REG;
93 st->tx[1] = (reg_address >> 8) & 0xFF;
94 st->tx[2] = reg_address & 0xFF;
96 ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers));
98 dev_err(&st->spi->dev, "problem when reading register 0x%02X",
108 *val = be16_to_cpup((const __be16 *)st->rx);
111 *val = (st->rx[0] << 16) | (st->rx[1] << 8) | st->rx[2];
114 *val = be32_to_cpup((const __be32 *)st->rx);
119 mutex_unlock(&st->buf_lock);
123 static int ade7854_spi_probe(struct spi_device *spi)
125 struct ade7854_state *st;
126 struct iio_dev *indio_dev;
128 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
131 st = iio_priv(indio_dev);
132 spi_set_drvdata(spi, indio_dev);
133 st->read_reg = ade7854_spi_read_reg;
134 st->write_reg = ade7854_spi_write_reg;
138 return ade7854_probe(indio_dev, &spi->dev);
141 static const struct spi_device_id ade7854_id[] = {
148 MODULE_DEVICE_TABLE(spi, ade7854_id);
150 static struct spi_driver ade7854_driver = {
154 .probe = ade7854_spi_probe,
155 .id_table = ade7854_id,
157 module_spi_driver(ade7854_driver);
159 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
160 MODULE_DESCRIPTION("Analog Devices ADE7854/58/68/78 SPI Driver");
161 MODULE_LICENSE("GPL v2");