i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / staging / iio / meter / ade7854-spi.c
blob81121862c1bd405c66da08131fe678a53647765d
1 /*
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.
7 */
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 "../iio.h"
16 #include "ade7854.h"
18 static int ade7854_spi_write_reg_8(struct device *dev,
19 u16 reg_address,
20 u8 value)
22 int ret;
23 struct spi_message msg;
24 struct iio_dev *indio_dev = dev_get_drvdata(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,
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 st->tx[3] = value & 0xFF;
38 spi_message_init(&msg);
39 spi_message_add_tail(&xfer, &msg);
40 ret = spi_sync(st->spi, &msg);
41 mutex_unlock(&st->buf_lock);
43 return ret;
46 static int ade7854_spi_write_reg_16(struct device *dev,
47 u16 reg_address,
48 u16 value)
50 int ret;
51 struct spi_message msg;
52 struct iio_dev *indio_dev = dev_get_drvdata(dev);
53 struct ade7854_state *st = iio_priv(indio_dev);
54 struct spi_transfer xfer = {
55 .tx_buf = st->tx,
56 .bits_per_word = 8,
57 .len = 5,
60 mutex_lock(&st->buf_lock);
61 st->tx[0] = ADE7854_WRITE_REG;
62 st->tx[1] = (reg_address >> 8) & 0xFF;
63 st->tx[2] = reg_address & 0xFF;
64 st->tx[3] = (value >> 8) & 0xFF;
65 st->tx[4] = value & 0xFF;
67 spi_message_init(&msg);
68 spi_message_add_tail(&xfer, &msg);
69 ret = spi_sync(st->spi, &msg);
70 mutex_unlock(&st->buf_lock);
72 return ret;
75 static int ade7854_spi_write_reg_24(struct device *dev,
76 u16 reg_address,
77 u32 value)
79 int ret;
80 struct spi_message msg;
81 struct iio_dev *indio_dev = dev_get_drvdata(dev);
82 struct ade7854_state *st = iio_priv(indio_dev);
83 struct spi_transfer xfer = {
84 .tx_buf = st->tx,
85 .bits_per_word = 8,
86 .len = 6,
89 mutex_lock(&st->buf_lock);
90 st->tx[0] = ADE7854_WRITE_REG;
91 st->tx[1] = (reg_address >> 8) & 0xFF;
92 st->tx[2] = reg_address & 0xFF;
93 st->tx[3] = (value >> 16) & 0xFF;
94 st->tx[4] = (value >> 8) & 0xFF;
95 st->tx[5] = value & 0xFF;
97 spi_message_init(&msg);
98 spi_message_add_tail(&xfer, &msg);
99 ret = spi_sync(st->spi, &msg);
100 mutex_unlock(&st->buf_lock);
102 return ret;
105 static int ade7854_spi_write_reg_32(struct device *dev,
106 u16 reg_address,
107 u32 value)
109 int ret;
110 struct spi_message msg;
111 struct iio_dev *indio_dev = dev_get_drvdata(dev);
112 struct ade7854_state *st = iio_priv(indio_dev);
113 struct spi_transfer xfer = {
114 .tx_buf = st->tx,
115 .bits_per_word = 8,
116 .len = 7,
119 mutex_lock(&st->buf_lock);
120 st->tx[0] = ADE7854_WRITE_REG;
121 st->tx[1] = (reg_address >> 8) & 0xFF;
122 st->tx[2] = reg_address & 0xFF;
123 st->tx[3] = (value >> 24) & 0xFF;
124 st->tx[4] = (value >> 16) & 0xFF;
125 st->tx[5] = (value >> 8) & 0xFF;
126 st->tx[6] = value & 0xFF;
128 spi_message_init(&msg);
129 spi_message_add_tail(&xfer, &msg);
130 ret = spi_sync(st->spi, &msg);
131 mutex_unlock(&st->buf_lock);
133 return ret;
136 static int ade7854_spi_read_reg_8(struct device *dev,
137 u16 reg_address,
138 u8 *val)
140 struct spi_message msg;
141 struct iio_dev *indio_dev = dev_get_drvdata(dev);
142 struct ade7854_state *st = iio_priv(indio_dev);
143 int ret;
144 struct spi_transfer xfers[] = {
146 .tx_buf = st->tx,
147 .bits_per_word = 8,
148 .len = 3,
149 }, {
150 .rx_buf = st->rx,
151 .bits_per_word = 8,
152 .len = 1,
156 mutex_lock(&st->buf_lock);
158 st->tx[0] = ADE7854_READ_REG;
159 st->tx[1] = (reg_address >> 8) & 0xFF;
160 st->tx[2] = reg_address & 0xFF;
162 spi_message_init(&msg);
163 spi_message_add_tail(&xfers[0], &msg);
164 spi_message_add_tail(&xfers[1], &msg);
165 ret = spi_sync(st->spi, &msg);
166 if (ret) {
167 dev_err(&st->spi->dev, "problem when reading 8 bit register 0x%02X",
168 reg_address);
169 goto error_ret;
171 *val = st->rx[0];
173 error_ret:
174 mutex_unlock(&st->buf_lock);
175 return ret;
178 static int ade7854_spi_read_reg_16(struct device *dev,
179 u16 reg_address,
180 u16 *val)
182 struct spi_message msg;
183 struct iio_dev *indio_dev = dev_get_drvdata(dev);
184 struct ade7854_state *st = iio_priv(indio_dev);
185 int ret;
186 struct spi_transfer xfers[] = {
188 .tx_buf = st->tx,
189 .bits_per_word = 8,
190 .len = 3,
191 }, {
192 .rx_buf = st->rx,
193 .bits_per_word = 8,
194 .len = 2,
198 mutex_lock(&st->buf_lock);
199 st->tx[0] = ADE7854_READ_REG;
200 st->tx[1] = (reg_address >> 8) & 0xFF;
201 st->tx[2] = reg_address & 0xFF;
203 spi_message_init(&msg);
204 spi_message_add_tail(&xfers[0], &msg);
205 spi_message_add_tail(&xfers[1], &msg);
206 ret = spi_sync(st->spi, &msg);
207 if (ret) {
208 dev_err(&st->spi->dev, "problem when reading 16 bit register 0x%02X",
209 reg_address);
210 goto error_ret;
212 *val = be16_to_cpup((const __be16 *)st->rx);
214 error_ret:
215 mutex_unlock(&st->buf_lock);
216 return ret;
219 static int ade7854_spi_read_reg_24(struct device *dev,
220 u16 reg_address,
221 u32 *val)
223 struct spi_message msg;
224 struct iio_dev *indio_dev = dev_get_drvdata(dev);
225 struct ade7854_state *st = iio_priv(indio_dev);
226 int ret;
227 struct spi_transfer xfers[] = {
229 .tx_buf = st->tx,
230 .bits_per_word = 8,
231 .len = 3,
232 }, {
233 .rx_buf = st->rx,
234 .bits_per_word = 8,
235 .len = 3,
239 mutex_lock(&st->buf_lock);
241 st->tx[0] = ADE7854_READ_REG;
242 st->tx[1] = (reg_address >> 8) & 0xFF;
243 st->tx[2] = reg_address & 0xFF;
245 spi_message_init(&msg);
246 spi_message_add_tail(&xfers[0], &msg);
247 spi_message_add_tail(&xfers[1], &msg);
248 ret = spi_sync(st->spi, &msg);
249 if (ret) {
250 dev_err(&st->spi->dev, "problem when reading 24 bit register 0x%02X",
251 reg_address);
252 goto error_ret;
254 *val = (st->rx[0] << 16) | (st->rx[1] << 8) | st->rx[2];
256 error_ret:
257 mutex_unlock(&st->buf_lock);
258 return ret;
261 static int ade7854_spi_read_reg_32(struct device *dev,
262 u16 reg_address,
263 u32 *val)
265 struct spi_message msg;
266 struct iio_dev *indio_dev = dev_get_drvdata(dev);
267 struct ade7854_state *st = iio_priv(indio_dev);
268 int ret;
269 struct spi_transfer xfers[] = {
271 .tx_buf = st->tx,
272 .bits_per_word = 8,
273 .len = 3,
274 }, {
275 .rx_buf = st->rx,
276 .bits_per_word = 8,
277 .len = 4,
281 mutex_lock(&st->buf_lock);
283 st->tx[0] = ADE7854_READ_REG;
284 st->tx[1] = (reg_address >> 8) & 0xFF;
285 st->tx[2] = reg_address & 0xFF;
287 spi_message_init(&msg);
288 spi_message_add_tail(&xfers[0], &msg);
289 spi_message_add_tail(&xfers[1], &msg);
290 ret = spi_sync(st->spi, &msg);
291 if (ret) {
292 dev_err(&st->spi->dev, "problem when reading 32 bit register 0x%02X",
293 reg_address);
294 goto error_ret;
296 *val = be32_to_cpup((const __be32 *)st->rx);
298 error_ret:
299 mutex_unlock(&st->buf_lock);
300 return ret;
303 static int __devinit ade7854_spi_probe(struct spi_device *spi)
305 int ret;
306 struct ade7854_state *st;
307 struct iio_dev *indio_dev;
309 indio_dev = iio_allocate_device(sizeof(*st));
310 if (indio_dev == NULL)
311 return -ENOMEM;
312 st = iio_priv(indio_dev);
313 spi_set_drvdata(spi, indio_dev);
314 st->read_reg_8 = ade7854_spi_read_reg_8;
315 st->read_reg_16 = ade7854_spi_read_reg_16;
316 st->read_reg_24 = ade7854_spi_read_reg_24;
317 st->read_reg_32 = ade7854_spi_read_reg_32;
318 st->write_reg_8 = ade7854_spi_write_reg_8;
319 st->write_reg_16 = ade7854_spi_write_reg_16;
320 st->write_reg_24 = ade7854_spi_write_reg_24;
321 st->write_reg_32 = ade7854_spi_write_reg_32;
322 st->irq = spi->irq;
323 st->spi = spi;
326 ret = ade7854_probe(indio_dev, &spi->dev);
327 if (ret)
328 iio_free_device(indio_dev);
330 return 0;
333 static int ade7854_spi_remove(struct spi_device *spi)
335 ade7854_remove(spi_get_drvdata(spi));
337 return 0;
339 static const struct spi_device_id ade7854_id[] = {
340 { "ade7854", 0 },
341 { "ade7858", 0 },
342 { "ade7868", 0 },
343 { "ade7878", 0 },
346 MODULE_DEVICE_TABLE(spi, ade7854_id);
348 static struct spi_driver ade7854_driver = {
349 .driver = {
350 .name = "ade7854",
351 .owner = THIS_MODULE,
353 .probe = ade7854_spi_probe,
354 .remove = __devexit_p(ade7854_spi_remove),
355 .id_table = ade7854_id,
357 module_spi_driver(ade7854_driver);
359 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
360 MODULE_DESCRIPTION("Analog Devices ADE7854/58/68/78 SPI Driver");
361 MODULE_LICENSE("GPL v2");