]> Pileus Git - ~andy/linux/blob - drivers/staging/iio/meter/ade7758_ring.c
c9271d0131794296c544f9456b15ea5f59fd5e38
[~andy/linux] / drivers / staging / iio / meter / ade7758_ring.c
1 /*
2  * ADE7758 Poly Phase Multifunction Energy Metering IC driver
3  *
4  * Copyright 2010-2011 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2.
7  */
8 #include <linux/interrupt.h>
9 #include <linux/kernel.h>
10 #include <linux/spi/spi.h>
11 #include <linux/slab.h>
12 #include <asm/unaligned.h>
13
14 #include "../iio.h"
15 #include "../ring_sw.h"
16 #include "../trigger.h"
17 #include "ade7758.h"
18
19 /**
20  * ade7758_spi_read_burst() - read data registers
21  * @dev: device associated with child of actual device (iio_dev or iio_trig)
22  **/
23 static int ade7758_spi_read_burst(struct device *dev)
24 {
25         struct iio_dev *indio_dev = dev_get_drvdata(dev);
26         struct ade7758_state *st = iio_priv(indio_dev);
27         int ret;
28
29         ret = spi_sync(st->us, &st->ring_msg);
30         if (ret)
31                 dev_err(&st->us->dev, "problem when reading WFORM value\n");
32
33         return ret;
34 }
35
36 static int ade7758_write_waveform_type(struct device *dev, unsigned type)
37 {
38         int ret;
39         u8 reg;
40
41         ret = ade7758_spi_read_reg_8(dev,
42                         ADE7758_WAVMODE,
43                         &reg);
44         if (ret)
45                 goto out;
46
47         reg &= ~0x1F;
48         reg |= type & 0x1F;
49
50         ret = ade7758_spi_write_reg_8(dev,
51                         ADE7758_WAVMODE,
52                         reg);
53 out:
54         return ret;
55 }
56
57 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
58  * specific to be rolled into the core.
59  */
60 static irqreturn_t ade7758_trigger_handler(int irq, void *p)
61 {
62         struct iio_poll_func *pf = p;
63         struct iio_dev *indio_dev = pf->private_data;
64         struct iio_ring_buffer *ring = indio_dev->ring;
65         struct ade7758_state *st = iio_priv(indio_dev);
66         s64 dat64[2];
67         u32 *dat32 = (u32 *)dat64;
68
69         if (ring->scan_count)
70                 if (ade7758_spi_read_burst(&indio_dev->dev) >= 0)
71                         *dat32 = get_unaligned_be32(&st->rx_buf[5]) & 0xFFFFFF;
72
73         /* Guaranteed to be aligned with 8 byte boundary */
74         if (ring->scan_timestamp)
75                 dat64[1] = pf->timestamp;
76
77         ring->access->store_to(ring, (u8 *)dat64, pf->timestamp);
78
79         iio_trigger_notify_done(indio_dev->trig);
80
81         return IRQ_HANDLED;
82 }
83
84 /**
85  * ade7758_ring_preenable() setup the parameters of the ring before enabling
86  *
87  * The complex nature of the setting of the nuber of bytes per datum is due
88  * to this driver currently ensuring that the timestamp is stored at an 8
89  * byte boundary.
90  **/
91 static int ade7758_ring_preenable(struct iio_dev *indio_dev)
92 {
93         struct ade7758_state *st = iio_priv(indio_dev);
94         struct iio_ring_buffer *ring = indio_dev->ring;
95         size_t d_size;
96         unsigned channel;
97
98         if (!ring->scan_count)
99                 return -EINVAL;
100
101         channel = __ffs(ring->scan_mask);
102
103         d_size = st->ade7758_ring_channels[channel].scan_type.storagebits / 8;
104
105         if (ring->scan_timestamp) {
106                 d_size += sizeof(s64);
107
108                 if (d_size % sizeof(s64))
109                         d_size += sizeof(s64) - (d_size % sizeof(s64));
110         }
111
112         if (indio_dev->ring->access->set_bytes_per_datum)
113                 indio_dev->ring->access->set_bytes_per_datum(indio_dev->ring,
114                                                             d_size);
115
116         ade7758_write_waveform_type(&indio_dev->dev,
117                 st->ade7758_ring_channels[channel].address);
118
119         return 0;
120 }
121
122 static const struct iio_ring_setup_ops ade7758_ring_setup_ops = {
123         .preenable = &ade7758_ring_preenable,
124         .postenable = &iio_triggered_ring_postenable,
125         .predisable = &iio_triggered_ring_predisable,
126 };
127
128 void ade7758_unconfigure_ring(struct iio_dev *indio_dev)
129 {
130         /* ensure that the trigger has been detached */
131         if (indio_dev->trig) {
132                 iio_put_trigger(indio_dev->trig);
133                 iio_trigger_dettach_poll_func(indio_dev->trig,
134                                               indio_dev->pollfunc);
135         }
136         iio_dealloc_pollfunc(indio_dev->pollfunc);
137         iio_sw_rb_free(indio_dev->ring);
138 }
139
140 int ade7758_configure_ring(struct iio_dev *indio_dev)
141 {
142         struct ade7758_state *st = iio_priv(indio_dev);
143         int ret = 0;
144
145         indio_dev->ring = iio_sw_rb_allocate(indio_dev);
146         if (!indio_dev->ring) {
147                 ret = -ENOMEM;
148                 return ret;
149         }
150
151         /* Effectively select the ring buffer implementation */
152         indio_dev->ring->access = &ring_sw_access_funcs;
153         indio_dev->ring->setup_ops = &ade7758_ring_setup_ops;
154         indio_dev->ring->owner = THIS_MODULE;
155
156         indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
157                                                  &ade7758_trigger_handler,
158                                                  0,
159                                                  indio_dev,
160                                                  "ade7759_consumer%d",
161                                                  indio_dev->id);
162         if (indio_dev->pollfunc == NULL) {
163                 ret = -ENOMEM;
164                 goto error_iio_sw_rb_free;
165         }
166
167         indio_dev->modes |= INDIO_RING_TRIGGERED;
168
169         st->tx_buf[0] = ADE7758_READ_REG(ADE7758_RSTATUS);
170         st->tx_buf[1] = 0;
171         st->tx_buf[2] = 0;
172         st->tx_buf[3] = 0;
173         st->tx_buf[4] = ADE7758_READ_REG(ADE7758_WFORM);
174         st->tx_buf[5] = 0;
175         st->tx_buf[6] = 0;
176         st->tx_buf[7] = 0;
177
178         /* build spi ring message */
179         st->ring_xfer[0].tx_buf = &st->tx_buf[0];
180         st->ring_xfer[0].len = 1;
181         st->ring_xfer[0].bits_per_word = 8;
182         st->ring_xfer[0].delay_usecs = 4;
183         st->ring_xfer[1].rx_buf = &st->rx_buf[1];
184         st->ring_xfer[1].len = 3;
185         st->ring_xfer[1].bits_per_word = 8;
186         st->ring_xfer[1].cs_change = 1;
187
188         st->ring_xfer[2].tx_buf = &st->tx_buf[4];
189         st->ring_xfer[2].len = 1;
190         st->ring_xfer[2].bits_per_word = 8;
191         st->ring_xfer[2].delay_usecs = 1;
192         st->ring_xfer[3].rx_buf = &st->rx_buf[5];
193         st->ring_xfer[3].len = 3;
194         st->ring_xfer[3].bits_per_word = 8;
195
196         spi_message_init(&st->ring_msg);
197         spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
198         spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);
199         spi_message_add_tail(&st->ring_xfer[2], &st->ring_msg);
200         spi_message_add_tail(&st->ring_xfer[3], &st->ring_msg);
201
202         return 0;
203
204 error_iio_sw_rb_free:
205         iio_sw_rb_free(indio_dev->ring);
206         return ret;
207 }
208
209 void ade7758_uninitialize_ring(struct iio_ring_buffer *ring)
210 {
211         iio_ring_buffer_unregister(ring);
212 }