1 /*
2  * ADE7854/58/68/78 Polyphase Multifunction Energy Metering IC Driver (SPI Bus)
3  *
4  * Copyright 2010 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later.
7  */
8 
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>
14 
15 #include <linux/iio/iio.h>
16 #include "ade7854.h"
17 
ade7854_spi_write_reg(struct device * dev,u16 reg_address,u32 val,int bits)18 static int ade7854_spi_write_reg(struct device *dev,
19 				 u16 reg_address,
20 				 u32 val,
21 				 int bits)
22 {
23 	int ret;
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 = {
27 		.tx_buf = st->tx,
28 		.bits_per_word = 8,
29 		.len = 4,
30 	};
31 
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 	switch (bits) {
37 	case 8:
38 		st->tx[3] = val & 0xFF;
39 		break;
40 	case 16:
41 		xfer.len = 5;
42 		st->tx[3] = (val >> 8) & 0xFF;
43 		st->tx[4] = val & 0xFF;
44 		break;
45 	case 24:
46 		xfer.len = 6;
47 		st->tx[3] = (val >> 16) & 0xFF;
48 		st->tx[4] = (val >> 8) & 0xFF;
49 		st->tx[5] = val & 0xFF;
50 		break;
51 	case 32:
52 		xfer.len = 7;
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;
57 		break;
58 	default:
59 		ret = -EINVAL;
60 		goto unlock;
61 	}
62 
63 	ret = spi_sync_transfer(st->spi, &xfer, 1);
64 unlock:
65 	mutex_unlock(&st->buf_lock);
66 
67 	return ret;
68 }
69 
ade7854_spi_read_reg(struct device * dev,u16 reg_address,u32 * val,int bits)70 static int ade7854_spi_read_reg(struct device *dev,
71 				u16 reg_address,
72 				u32 *val,
73 				int bits)
74 {
75 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
76 	struct ade7854_state *st = iio_priv(indio_dev);
77 	int ret;
78 	struct spi_transfer xfers[] = {
79 		{
80 			.tx_buf = st->tx,
81 			.bits_per_word = 8,
82 			.len = 3,
83 		}, {
84 			.rx_buf = st->rx,
85 			.bits_per_word = 8,
86 			.len = bits,
87 		}
88 	};
89 
90 	mutex_lock(&st->buf_lock);
91 
92 	st->tx[0] = ADE7854_READ_REG;
93 	st->tx[1] = (reg_address >> 8) & 0xFF;
94 	st->tx[2] = reg_address & 0xFF;
95 
96 	ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers));
97 	if (ret < 0) {
98 		dev_err(&st->spi->dev, "problem when reading register 0x%02X",
99 			reg_address);
100 		goto unlock;
101 	}
102 
103 	switch (bits) {
104 	case 8:
105 		*val = st->rx[0];
106 		break;
107 	case 16:
108 		*val = be16_to_cpup((const __be16 *)st->rx);
109 		break;
110 	case 24:
111 		*val = (st->rx[0] << 16) | (st->rx[1] << 8) | st->rx[2];
112 		break;
113 	case 32:
114 		*val = be32_to_cpup((const __be32 *)st->rx);
115 		break;
116 	}
117 
118 unlock:
119 	mutex_unlock(&st->buf_lock);
120 	return ret;
121 }
122 
ade7854_spi_probe(struct spi_device * spi)123 static int ade7854_spi_probe(struct spi_device *spi)
124 {
125 	struct ade7854_state *st;
126 	struct iio_dev *indio_dev;
127 
128 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
129 	if (!indio_dev)
130 		return -ENOMEM;
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;
135 	st->irq = spi->irq;
136 	st->spi = spi;
137 
138 	return ade7854_probe(indio_dev, &spi->dev);
139 }
140 
141 static const struct spi_device_id ade7854_id[] = {
142 	{ "ade7854", 0 },
143 	{ "ade7858", 0 },
144 	{ "ade7868", 0 },
145 	{ "ade7878", 0 },
146 	{ }
147 };
148 MODULE_DEVICE_TABLE(spi, ade7854_id);
149 
150 static struct spi_driver ade7854_driver = {
151 	.driver = {
152 		.name = "ade7854",
153 	},
154 	.probe = ade7854_spi_probe,
155 	.id_table = ade7854_id,
156 };
157 module_spi_driver(ade7854_driver);
158 
159 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
160 MODULE_DESCRIPTION("Analog Devices ADE7854/58/68/78 SPI Driver");
161 MODULE_LICENSE("GPL v2");
162