iio: Add reference counting for buffers
[linux-2.6/btrfs-unstable.git] / drivers / staging / iio / meter / ade7758_ring.c
blob7a448ffaeb14edceff803f58fec35dfd0fcca9f4
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/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>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/kfifo_buf.h>
17 #include <linux/iio/trigger_consumer.h>
18 #include "ade7758.h"
20 /**
21 * ade7758_spi_read_burst() - read data registers
22 * @indio_dev: the IIO device
23 **/
24 static int ade7758_spi_read_burst(struct iio_dev *indio_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 too 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 ade7758_state *st = iio_priv(indio_dev);
65 s64 dat64[2];
66 u32 *dat32 = (u32 *)dat64;
68 if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
69 if (ade7758_spi_read_burst(indio_dev) >= 0)
70 *dat32 = get_unaligned_be32(&st->rx_buf[5]) & 0xFFFFFF;
72 iio_push_to_buffers_with_timestamp(indio_dev, dat64, pf->timestamp);
74 iio_trigger_notify_done(indio_dev->trig);
76 return IRQ_HANDLED;
79 /**
80 * ade7758_ring_preenable() setup the parameters of the ring before enabling
82 * The complex nature of the setting of the number of bytes per datum is due
83 * to this driver currently ensuring that the timestamp is stored at an 8
84 * byte boundary.
85 **/
86 static int ade7758_ring_preenable(struct iio_dev *indio_dev)
88 struct ade7758_state *st = iio_priv(indio_dev);
89 unsigned channel;
90 int ret;
92 if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
93 return -EINVAL;
95 ret = iio_sw_buffer_preenable(indio_dev);
96 if (ret < 0)
97 return ret;
99 channel = find_first_bit(indio_dev->active_scan_mask,
100 indio_dev->masklength);
102 ade7758_write_waveform_type(&indio_dev->dev,
103 st->ade7758_ring_channels[channel].address);
105 return 0;
108 static const struct iio_buffer_setup_ops ade7758_ring_setup_ops = {
109 .preenable = &ade7758_ring_preenable,
110 .postenable = &iio_triggered_buffer_postenable,
111 .predisable = &iio_triggered_buffer_predisable,
112 .validate_scan_mask = &iio_validate_scan_mask_onehot,
115 void ade7758_unconfigure_ring(struct iio_dev *indio_dev)
117 iio_dealloc_pollfunc(indio_dev->pollfunc);
118 iio_kfifo_free(indio_dev->buffer);
121 int ade7758_configure_ring(struct iio_dev *indio_dev)
123 struct ade7758_state *st = iio_priv(indio_dev);
124 struct iio_buffer *buffer;
125 int ret = 0;
127 buffer = iio_kfifo_allocate(indio_dev);
128 if (!buffer) {
129 ret = -ENOMEM;
130 return ret;
133 iio_device_attach_buffer(indio_dev, buffer);
135 indio_dev->setup_ops = &ade7758_ring_setup_ops;
137 indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
138 &ade7758_trigger_handler,
140 indio_dev,
141 "ade7759_consumer%d",
142 indio_dev->id);
143 if (indio_dev->pollfunc == NULL) {
144 ret = -ENOMEM;
145 goto error_iio_kfifo_free;
148 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
150 st->tx_buf[0] = ADE7758_READ_REG(ADE7758_RSTATUS);
151 st->tx_buf[1] = 0;
152 st->tx_buf[2] = 0;
153 st->tx_buf[3] = 0;
154 st->tx_buf[4] = ADE7758_READ_REG(ADE7758_WFORM);
155 st->tx_buf[5] = 0;
156 st->tx_buf[6] = 0;
157 st->tx_buf[7] = 0;
159 /* build spi ring message */
160 st->ring_xfer[0].tx_buf = &st->tx_buf[0];
161 st->ring_xfer[0].len = 1;
162 st->ring_xfer[0].bits_per_word = 8;
163 st->ring_xfer[0].delay_usecs = 4;
164 st->ring_xfer[1].rx_buf = &st->rx_buf[1];
165 st->ring_xfer[1].len = 3;
166 st->ring_xfer[1].bits_per_word = 8;
167 st->ring_xfer[1].cs_change = 1;
169 st->ring_xfer[2].tx_buf = &st->tx_buf[4];
170 st->ring_xfer[2].len = 1;
171 st->ring_xfer[2].bits_per_word = 8;
172 st->ring_xfer[2].delay_usecs = 1;
173 st->ring_xfer[3].rx_buf = &st->rx_buf[5];
174 st->ring_xfer[3].len = 3;
175 st->ring_xfer[3].bits_per_word = 8;
177 spi_message_init(&st->ring_msg);
178 spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
179 spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);
180 spi_message_add_tail(&st->ring_xfer[2], &st->ring_msg);
181 spi_message_add_tail(&st->ring_xfer[3], &st->ring_msg);
183 return 0;
185 error_iio_kfifo_free:
186 iio_kfifo_free(indio_dev->buffer);
187 return ret;
190 void ade7758_uninitialize_ring(struct iio_dev *indio_dev)
192 iio_buffer_unregister(indio_dev);