Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ade7854-spi.c
Go to the documentation of this file.
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 
18 static int ade7854_spi_write_reg_8(struct device *dev,
19  u16 reg_address,
20  u8 value)
21 {
22  int ret;
23  struct spi_message msg;
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  st->tx[3] = value & 0xFF;
37 
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);
42 
43  return ret;
44 }
45 
46 static int ade7854_spi_write_reg_16(struct device *dev,
47  u16 reg_address,
48  u16 value)
49 {
50  int ret;
51  struct spi_message msg;
52  struct iio_dev *indio_dev = dev_to_iio_dev(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,
58  };
59 
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;
66 
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);
71 
72  return ret;
73 }
74 
75 static int ade7854_spi_write_reg_24(struct device *dev,
76  u16 reg_address,
77  u32 value)
78 {
79  int ret;
80  struct spi_message msg;
81  struct iio_dev *indio_dev = dev_to_iio_dev(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,
87  };
88 
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;
96 
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);
101 
102  return ret;
103 }
104 
105 static int ade7854_spi_write_reg_32(struct device *dev,
106  u16 reg_address,
107  u32 value)
108 {
109  int ret;
110  struct spi_message msg;
111  struct iio_dev *indio_dev = dev_to_iio_dev(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,
117  };
118 
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;
127 
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);
132 
133  return ret;
134 }
135 
136 static int ade7854_spi_read_reg_8(struct device *dev,
137  u16 reg_address,
138  u8 *val)
139 {
140  struct spi_message msg;
141  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
142  struct ade7854_state *st = iio_priv(indio_dev);
143  int ret;
144  struct spi_transfer xfers[] = {
145  {
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,
153  }
154  };
155 
156  mutex_lock(&st->buf_lock);
157 
158  st->tx[0] = ADE7854_READ_REG;
159  st->tx[1] = (reg_address >> 8) & 0xFF;
160  st->tx[2] = reg_address & 0xFF;
161 
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;
170  }
171  *val = st->rx[0];
172 
173 error_ret:
174  mutex_unlock(&st->buf_lock);
175  return ret;
176 }
177 
178 static int ade7854_spi_read_reg_16(struct device *dev,
179  u16 reg_address,
180  u16 *val)
181 {
182  struct spi_message msg;
183  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
184  struct ade7854_state *st = iio_priv(indio_dev);
185  int ret;
186  struct spi_transfer xfers[] = {
187  {
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,
195  }
196  };
197 
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;
202 
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;
211  }
212  *val = be16_to_cpup((const __be16 *)st->rx);
213 
214 error_ret:
215  mutex_unlock(&st->buf_lock);
216  return ret;
217 }
218 
219 static int ade7854_spi_read_reg_24(struct device *dev,
220  u16 reg_address,
221  u32 *val)
222 {
223  struct spi_message msg;
224  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
225  struct ade7854_state *st = iio_priv(indio_dev);
226  int ret;
227  struct spi_transfer xfers[] = {
228  {
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,
236  }
237  };
238 
239  mutex_lock(&st->buf_lock);
240 
241  st->tx[0] = ADE7854_READ_REG;
242  st->tx[1] = (reg_address >> 8) & 0xFF;
243  st->tx[2] = reg_address & 0xFF;
244 
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;
253  }
254  *val = (st->rx[0] << 16) | (st->rx[1] << 8) | st->rx[2];
255 
256 error_ret:
257  mutex_unlock(&st->buf_lock);
258  return ret;
259 }
260 
261 static int ade7854_spi_read_reg_32(struct device *dev,
262  u16 reg_address,
263  u32 *val)
264 {
265  struct spi_message msg;
266  struct iio_dev *indio_dev = dev_to_iio_dev(dev);
267  struct ade7854_state *st = iio_priv(indio_dev);
268  int ret;
269  struct spi_transfer xfers[] = {
270  {
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,
278  }
279  };
280 
281  mutex_lock(&st->buf_lock);
282 
283  st->tx[0] = ADE7854_READ_REG;
284  st->tx[1] = (reg_address >> 8) & 0xFF;
285  st->tx[2] = reg_address & 0xFF;
286 
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;
295  }
296  *val = be32_to_cpup((const __be32 *)st->rx);
297 
298 error_ret:
299  mutex_unlock(&st->buf_lock);
300  return ret;
301 }
302 
303 static int __devinit ade7854_spi_probe(struct spi_device *spi)
304 {
305  int ret;
306  struct ade7854_state *st;
307  struct iio_dev *indio_dev;
308 
309  indio_dev = iio_device_alloc(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;
324 
325 
326  ret = ade7854_probe(indio_dev, &spi->dev);
327  if (ret)
328  iio_device_free(indio_dev);
329 
330  return 0;
331 }
332 
333 static int __devexit ade7854_spi_remove(struct spi_device *spi)
334 {
335  ade7854_remove(spi_get_drvdata(spi));
336 
337  return 0;
338 }
339 static const struct spi_device_id ade7854_id[] = {
340  { "ade7854", 0 },
341  { "ade7858", 0 },
342  { "ade7868", 0 },
343  { "ade7878", 0 },
344  { }
345 };
346 MODULE_DEVICE_TABLE(spi, ade7854_id);
347 
348 static struct spi_driver ade7854_driver = {
349  .driver = {
350  .name = "ade7854",
351  .owner = THIS_MODULE,
352  },
353  .probe = ade7854_spi_probe,
354  .remove = __devexit_p(ade7854_spi_remove),
355  .id_table = ade7854_id,
356 };
357 module_spi_driver(ade7854_driver);
358 
359 MODULE_AUTHOR("Barry Song <[email protected]>");
360 MODULE_DESCRIPTION("Analog Devices ADE7854/58/68/78 SPI Driver");
361 MODULE_LICENSE("GPL v2");