2 * ADE7758 Poly Phase Multifunction Energy Metering IC driver
4 * Copyright 2010-2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
8 #include <linux/export.h>
9 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/spi/spi.h>
12 #include <linux/slab.h>
13 #include <asm/unaligned.h>
16 #include "../ring_sw.h"
17 #include "../trigger_consumer.h"
21 * ade7758_spi_read_burst() - read data registers
22 * @dev: device associated with child of actual device (iio_dev or iio_trig)
24 static int ade7758_spi_read_burst(struct device *dev)
26 struct iio_dev *indio_dev = dev_get_drvdata(dev);
27 struct ade7758_state *st = iio_priv(indio_dev);
30 ret = spi_sync(st->us, &st->ring_msg);
32 dev_err(&st->us->dev, "problem when reading WFORM value\n");
37 static int ade7758_write_waveform_type(struct device *dev, unsigned type)
42 ret = ade7758_spi_read_reg_8(dev,
51 ret = ade7758_spi_write_reg_8(dev,
58 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
59 * specific to be rolled into the core.
61 static irqreturn_t ade7758_trigger_handler(int irq, void *p)
63 struct iio_poll_func *pf = p;
64 struct iio_dev *indio_dev = pf->indio_dev;
65 struct iio_buffer *ring = indio_dev->buffer;
66 struct ade7758_state *st = iio_priv(indio_dev);
68 u32 *dat32 = (u32 *)dat64;
70 if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
71 if (ade7758_spi_read_burst(&indio_dev->dev) >= 0)
72 *dat32 = get_unaligned_be32(&st->rx_buf[5]) & 0xFFFFFF;
74 /* Guaranteed to be aligned with 8 byte boundary */
75 if (ring->scan_timestamp)
76 dat64[1] = pf->timestamp;
78 ring->access->store_to(ring, (u8 *)dat64, pf->timestamp);
80 iio_trigger_notify_done(indio_dev->trig);
86 * ade7758_ring_preenable() setup the parameters of the ring before enabling
88 * The complex nature of the setting of the nuber of bytes per datum is due
89 * to this driver currently ensuring that the timestamp is stored at an 8
92 static int ade7758_ring_preenable(struct iio_dev *indio_dev)
94 struct ade7758_state *st = iio_priv(indio_dev);
95 struct iio_buffer *ring = indio_dev->buffer;
99 if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
102 channel = find_first_bit(indio_dev->active_scan_mask,
103 indio_dev->masklength);
105 d_size = st->ade7758_ring_channels[channel].scan_type.storagebits / 8;
107 if (ring->scan_timestamp) {
108 d_size += sizeof(s64);
110 if (d_size % sizeof(s64))
111 d_size += sizeof(s64) - (d_size % sizeof(s64));
114 if (indio_dev->buffer->access->set_bytes_per_datum)
115 indio_dev->buffer->access->
116 set_bytes_per_datum(indio_dev->buffer, d_size);
118 ade7758_write_waveform_type(&indio_dev->dev,
119 st->ade7758_ring_channels[channel].address);
124 static const struct iio_buffer_setup_ops ade7758_ring_setup_ops = {
125 .preenable = &ade7758_ring_preenable,
126 .postenable = &iio_triggered_buffer_postenable,
127 .predisable = &iio_triggered_buffer_predisable,
130 void ade7758_unconfigure_ring(struct iio_dev *indio_dev)
132 iio_dealloc_pollfunc(indio_dev->pollfunc);
133 iio_sw_rb_free(indio_dev->buffer);
136 int ade7758_configure_ring(struct iio_dev *indio_dev)
138 struct ade7758_state *st = iio_priv(indio_dev);
141 indio_dev->buffer = iio_sw_rb_allocate(indio_dev);
142 if (!indio_dev->buffer) {
147 /* Effectively select the ring buffer implementation */
148 indio_dev->buffer->access = &ring_sw_access_funcs;
149 indio_dev->setup_ops = &ade7758_ring_setup_ops;
151 indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
152 &ade7758_trigger_handler,
155 "ade7759_consumer%d",
157 if (indio_dev->pollfunc == NULL) {
159 goto error_iio_sw_rb_free;
162 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
164 st->tx_buf[0] = ADE7758_READ_REG(ADE7758_RSTATUS);
168 st->tx_buf[4] = ADE7758_READ_REG(ADE7758_WFORM);
173 /* build spi ring message */
174 st->ring_xfer[0].tx_buf = &st->tx_buf[0];
175 st->ring_xfer[0].len = 1;
176 st->ring_xfer[0].bits_per_word = 8;
177 st->ring_xfer[0].delay_usecs = 4;
178 st->ring_xfer[1].rx_buf = &st->rx_buf[1];
179 st->ring_xfer[1].len = 3;
180 st->ring_xfer[1].bits_per_word = 8;
181 st->ring_xfer[1].cs_change = 1;
183 st->ring_xfer[2].tx_buf = &st->tx_buf[4];
184 st->ring_xfer[2].len = 1;
185 st->ring_xfer[2].bits_per_word = 8;
186 st->ring_xfer[2].delay_usecs = 1;
187 st->ring_xfer[3].rx_buf = &st->rx_buf[5];
188 st->ring_xfer[3].len = 3;
189 st->ring_xfer[3].bits_per_word = 8;
191 spi_message_init(&st->ring_msg);
192 spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
193 spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);
194 spi_message_add_tail(&st->ring_xfer[2], &st->ring_msg);
195 spi_message_add_tail(&st->ring_xfer[3], &st->ring_msg);
199 error_iio_sw_rb_free:
200 iio_sw_rb_free(indio_dev->buffer);
204 void ade7758_uninitialize_ring(struct iio_dev *indio_dev)
206 iio_buffer_unregister(indio_dev);