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/interrupt.h>
10 #include <linux/gpio.h>
11 #include <linux/workqueue.h>
12 #include <linux/mutex.h>
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/spi/spi.h>
16 #include <linux/slab.h>
17 #include <linux/sysfs.h>
18 #include <linux/list.h>
19 #include <asm/unaligned.h>
23 #include "../ring_sw.h"
24 #include "../accel/accel.h"
25 #include "../trigger.h"
29 * ade7758_spi_read_burst() - read data registers
30 * @dev: device associated with child of actual device (iio_dev or iio_trig)
32 static int ade7758_spi_read_burst(struct device *dev)
34 struct iio_dev *indio_dev = dev_get_drvdata(dev);
35 struct ade7758_state *st = iio_dev_get_devdata(indio_dev);
38 ret = spi_sync(st->us, &st->ring_msg);
40 dev_err(&st->us->dev, "problem when reading WFORM value\n");
45 static int ade7758_write_waveform_type(struct device *dev, unsigned type)
50 ret = ade7758_spi_read_reg_8(dev,
59 ret = ade7758_spi_write_reg_8(dev,
66 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
67 * specific to be rolled into the core.
69 static irqreturn_t ade7758_trigger_handler(int irq, void *p)
71 struct iio_poll_func *pf = p;
72 struct iio_dev *indio_dev = pf->private_data;
73 struct iio_ring_buffer *ring = indio_dev->ring;
74 struct ade7758_state *st = iio_dev_get_devdata(indio_dev);
76 u32 *dat32 = (u32 *)dat64;
79 if (ade7758_spi_read_burst(&st->indio_dev->dev) >= 0)
80 *dat32 = get_unaligned_be32(&st->rx_buf[5]) & 0xFFFFFF;
82 /* Guaranteed to be aligned with 8 byte boundary */
83 if (ring->scan_timestamp)
84 dat64[1] = pf->timestamp;
86 ring->access->store_to(ring, (u8 *)dat64, pf->timestamp);
88 iio_trigger_notify_done(st->indio_dev->trig);
94 * ade7758_ring_preenable() setup the parameters of the ring before enabling
96 * The complex nature of the setting of the nuber of bytes per datum is due
97 * to this driver currently ensuring that the timestamp is stored at an 8
100 static int ade7758_ring_preenable(struct iio_dev *indio_dev)
102 struct ade7758_state *st = indio_dev->dev_data;
103 struct iio_ring_buffer *ring = indio_dev->ring;
107 if (!ring->scan_count)
110 channel = __ffs(ring->scan_mask);
112 d_size = st->ade7758_ring_channels[channel].scan_type.storagebits / 8;
114 if (ring->scan_timestamp) {
115 d_size += sizeof(s64);
117 if (d_size % sizeof(s64))
118 d_size += sizeof(s64) - (d_size % sizeof(s64));
121 if (indio_dev->ring->access->set_bytes_per_datum)
122 indio_dev->ring->access->set_bytes_per_datum(indio_dev->ring,
125 ade7758_write_waveform_type(&indio_dev->dev,
126 st->ade7758_ring_channels[channel].address);
131 static const struct iio_ring_setup_ops ade7758_ring_setup_ops = {
132 .preenable = &ade7758_ring_preenable,
133 .postenable = &iio_triggered_ring_postenable,
134 .predisable = &iio_triggered_ring_predisable,
137 void ade7758_unconfigure_ring(struct iio_dev *indio_dev)
139 /* ensure that the trigger has been detached */
140 if (indio_dev->trig) {
141 iio_put_trigger(indio_dev->trig);
142 iio_trigger_dettach_poll_func(indio_dev->trig,
143 indio_dev->pollfunc);
145 kfree(indio_dev->pollfunc->name);
146 kfree(indio_dev->pollfunc);
147 iio_sw_rb_free(indio_dev->ring);
150 int ade7758_configure_ring(struct iio_dev *indio_dev)
152 struct ade7758_state *st = indio_dev->dev_data;
155 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
156 if (!indio_dev->ring) {
161 /* Effectively select the ring buffer implementation */
162 indio_dev->ring->access = &ring_sw_access_funcs;
163 indio_dev->ring->setup_ops = &ade7758_ring_setup_ops;
164 indio_dev->ring->owner = THIS_MODULE;
166 indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
167 if (indio_dev->pollfunc == NULL) {
169 goto error_iio_sw_rb_free;
171 indio_dev->pollfunc->private_data = indio_dev;
172 indio_dev->pollfunc->h = &iio_pollfunc_store_time;
173 indio_dev->pollfunc->thread = &ade7758_trigger_handler;
174 indio_dev->pollfunc->name
175 = kasprintf(GFP_KERNEL, "ade7759_consumer%d", indio_dev->id);
176 if (indio_dev->pollfunc->name == NULL) {
178 goto error_free_pollfunc;
180 indio_dev->modes |= INDIO_RING_TRIGGERED;
182 st->tx_buf[0] = ADE7758_READ_REG(ADE7758_RSTATUS);
186 st->tx_buf[4] = ADE7758_READ_REG(ADE7758_WFORM);
191 /* build spi ring message */
192 st->ring_xfer[0].tx_buf = &st->tx_buf[0];
193 st->ring_xfer[0].len = 1;
194 st->ring_xfer[0].bits_per_word = 8;
195 st->ring_xfer[0].delay_usecs = 4;
196 st->ring_xfer[1].rx_buf = &st->rx_buf[1];
197 st->ring_xfer[1].len = 3;
198 st->ring_xfer[1].bits_per_word = 8;
199 st->ring_xfer[1].cs_change = 1;
201 st->ring_xfer[2].tx_buf = &st->tx_buf[4];
202 st->ring_xfer[2].len = 1;
203 st->ring_xfer[2].bits_per_word = 8;
204 st->ring_xfer[2].delay_usecs = 1;
205 st->ring_xfer[3].rx_buf = &st->rx_buf[5];
206 st->ring_xfer[3].len = 3;
207 st->ring_xfer[3].bits_per_word = 8;
209 spi_message_init(&st->ring_msg);
210 spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
211 spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);
212 spi_message_add_tail(&st->ring_xfer[2], &st->ring_msg);
213 spi_message_add_tail(&st->ring_xfer[3], &st->ring_msg);
218 kfree(indio_dev->pollfunc);
219 error_iio_sw_rb_free:
220 iio_sw_rb_free(indio_dev->ring);
224 void ade7758_uninitialize_ring(struct iio_ring_buffer *ring)
226 iio_ring_buffer_unregister(ring);