staging:iio: treewide rename iio_triggered_ring_* to iio_triggered_buffer_*
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / iio / meter / ade7758_ring.c
blobece2a4076eeed54346785a8c4818d7f149a346fa
1 /*
2 * ADE7758 Poly Phase Multifunction Energy Metering IC driver
4 * Copyright 2010-2011 Analog Devices Inc.
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>
14 #include "../iio.h"
15 #include "../ring_sw.h"
16 #include "../trigger_consumer.h"
17 #include "ade7758.h"
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)
25 struct iio_dev *indio_dev = dev_get_drvdata(dev);
26 struct ade7758_state *st = iio_priv(indio_dev);
27 int ret;
29 ret = spi_sync(st->us, &st->ring_msg);
30 if (ret)
31 dev_err(&st->us->dev, "problem when reading WFORM value\n");
33 return ret;
36 static int ade7758_write_waveform_type(struct device *dev, unsigned type)
38 int ret;
39 u8 reg;
41 ret = ade7758_spi_read_reg_8(dev,
42 ADE7758_WAVMODE,
43 &reg);
44 if (ret)
45 goto out;
47 reg &= ~0x1F;
48 reg |= type & 0x1F;
50 ret = ade7758_spi_write_reg_8(dev,
51 ADE7758_WAVMODE,
52 reg);
53 out:
54 return ret;
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.
60 static irqreturn_t ade7758_trigger_handler(int irq, void *p)
62 struct iio_poll_func *pf = p;
63 struct iio_dev *indio_dev = pf->indio_dev;
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;
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;
73 /* Guaranteed to be aligned with 8 byte boundary */
74 if (ring->scan_timestamp)
75 dat64[1] = pf->timestamp;
77 ring->access->store_to(ring, (u8 *)dat64, pf->timestamp);
79 iio_trigger_notify_done(indio_dev->trig);
81 return IRQ_HANDLED;
84 /**
85 * ade7758_ring_preenable() setup the parameters of the ring before enabling
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)
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;
98 if (!ring->scan_count)
99 return -EINVAL;
101 channel = find_first_bit(ring->scan_mask, indio_dev->masklength);
103 d_size = st->ade7758_ring_channels[channel].scan_type.storagebits / 8;
105 if (ring->scan_timestamp) {
106 d_size += sizeof(s64);
108 if (d_size % sizeof(s64))
109 d_size += sizeof(s64) - (d_size % sizeof(s64));
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);
116 ade7758_write_waveform_type(&indio_dev->dev,
117 st->ade7758_ring_channels[channel].address);
119 return 0;
122 static const struct iio_ring_setup_ops ade7758_ring_setup_ops = {
123 .preenable = &ade7758_ring_preenable,
124 .postenable = &iio_triggered_buffer_postenable,
125 .predisable = &iio_triggered_buffer_predisable,
128 void ade7758_unconfigure_ring(struct iio_dev *indio_dev)
130 iio_dealloc_pollfunc(indio_dev->pollfunc);
131 iio_sw_rb_free(indio_dev->ring);
134 int ade7758_configure_ring(struct iio_dev *indio_dev)
136 struct ade7758_state *st = iio_priv(indio_dev);
137 int ret = 0;
139 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
140 if (!indio_dev->ring) {
141 ret = -ENOMEM;
142 return ret;
145 /* Effectively select the ring buffer implementation */
146 indio_dev->ring->access = &ring_sw_access_funcs;
147 indio_dev->ring->setup_ops = &ade7758_ring_setup_ops;
148 indio_dev->ring->owner = THIS_MODULE;
150 indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
151 &ade7758_trigger_handler,
153 indio_dev,
154 "ade7759_consumer%d",
155 indio_dev->id);
156 if (indio_dev->pollfunc == NULL) {
157 ret = -ENOMEM;
158 goto error_iio_sw_rb_free;
161 indio_dev->modes |= INDIO_RING_TRIGGERED;
163 st->tx_buf[0] = ADE7758_READ_REG(ADE7758_RSTATUS);
164 st->tx_buf[1] = 0;
165 st->tx_buf[2] = 0;
166 st->tx_buf[3] = 0;
167 st->tx_buf[4] = ADE7758_READ_REG(ADE7758_WFORM);
168 st->tx_buf[5] = 0;
169 st->tx_buf[6] = 0;
170 st->tx_buf[7] = 0;
172 /* build spi ring message */
173 st->ring_xfer[0].tx_buf = &st->tx_buf[0];
174 st->ring_xfer[0].len = 1;
175 st->ring_xfer[0].bits_per_word = 8;
176 st->ring_xfer[0].delay_usecs = 4;
177 st->ring_xfer[1].rx_buf = &st->rx_buf[1];
178 st->ring_xfer[1].len = 3;
179 st->ring_xfer[1].bits_per_word = 8;
180 st->ring_xfer[1].cs_change = 1;
182 st->ring_xfer[2].tx_buf = &st->tx_buf[4];
183 st->ring_xfer[2].len = 1;
184 st->ring_xfer[2].bits_per_word = 8;
185 st->ring_xfer[2].delay_usecs = 1;
186 st->ring_xfer[3].rx_buf = &st->rx_buf[5];
187 st->ring_xfer[3].len = 3;
188 st->ring_xfer[3].bits_per_word = 8;
190 spi_message_init(&st->ring_msg);
191 spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
192 spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);
193 spi_message_add_tail(&st->ring_xfer[2], &st->ring_msg);
194 spi_message_add_tail(&st->ring_xfer[3], &st->ring_msg);
196 return 0;
198 error_iio_sw_rb_free:
199 iio_sw_rb_free(indio_dev->ring);
200 return ret;
203 void ade7758_uninitialize_ring(struct iio_dev *indio_dev)
205 iio_ring_buffer_unregister(indio_dev);