Merge branch 'stable/xen-swiotlb.bugfix' of git://git.kernel.org/pub/scm/linux/kernel...
[deliverable/linux.git] / 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/irq.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>
20
21 #include "../iio.h"
22 #include "../sysfs.h"
23 #include "../ring_sw.h"
24 #include "../accel/accel.h"
25 #include "../trigger.h"
26 #include "ade7758.h"
27
28 /**
29 * ade7758_spi_read_burst() - read data registers
30 * @dev: device associated with child of actual device (iio_dev or iio_trig)
31 **/
32 static int ade7758_spi_read_burst(struct device *dev)
33 {
34 struct iio_dev *indio_dev = dev_get_drvdata(dev);
35 struct ade7758_state *st = iio_priv(indio_dev);
36 int ret;
37
38 ret = spi_sync(st->us, &st->ring_msg);
39 if (ret)
40 dev_err(&st->us->dev, "problem when reading WFORM value\n");
41
42 return ret;
43 }
44
45 static int ade7758_write_waveform_type(struct device *dev, unsigned type)
46 {
47 int ret;
48 u8 reg;
49
50 ret = ade7758_spi_read_reg_8(dev,
51 ADE7758_WAVMODE,
52 &reg);
53 if (ret)
54 goto out;
55
56 reg &= ~0x1F;
57 reg |= type & 0x1F;
58
59 ret = ade7758_spi_write_reg_8(dev,
60 ADE7758_WAVMODE,
61 reg);
62 out:
63 return ret;
64 }
65
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.
68 */
69 static irqreturn_t ade7758_trigger_handler(int irq, void *p)
70 {
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_priv(indio_dev);
75 s64 dat64[2];
76 u32 *dat32 = (u32 *)dat64;
77
78 if (ring->scan_count)
79 if (ade7758_spi_read_burst(&indio_dev->dev) >= 0)
80 *dat32 = get_unaligned_be32(&st->rx_buf[5]) & 0xFFFFFF;
81
82 /* Guaranteed to be aligned with 8 byte boundary */
83 if (ring->scan_timestamp)
84 dat64[1] = pf->timestamp;
85
86 ring->access->store_to(ring, (u8 *)dat64, pf->timestamp);
87
88 iio_trigger_notify_done(indio_dev->trig);
89
90 return IRQ_HANDLED;
91 }
92
93 /**
94 * ade7758_ring_preenable() setup the parameters of the ring before enabling
95 *
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
98 * byte boundary.
99 **/
100 static int ade7758_ring_preenable(struct iio_dev *indio_dev)
101 {
102 struct ade7758_state *st = iio_priv(indio_dev);
103 struct iio_ring_buffer *ring = indio_dev->ring;
104 size_t d_size;
105 unsigned channel;
106
107 if (!ring->scan_count)
108 return -EINVAL;
109
110 channel = __ffs(ring->scan_mask);
111
112 d_size = st->ade7758_ring_channels[channel].scan_type.storagebits / 8;
113
114 if (ring->scan_timestamp) {
115 d_size += sizeof(s64);
116
117 if (d_size % sizeof(s64))
118 d_size += sizeof(s64) - (d_size % sizeof(s64));
119 }
120
121 if (indio_dev->ring->access->set_bytes_per_datum)
122 indio_dev->ring->access->set_bytes_per_datum(indio_dev->ring,
123 d_size);
124
125 ade7758_write_waveform_type(&indio_dev->dev,
126 st->ade7758_ring_channels[channel].address);
127
128 return 0;
129 }
130
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,
135 };
136
137 void ade7758_unconfigure_ring(struct iio_dev *indio_dev)
138 {
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);
144 }
145 iio_dealloc_pollfunc(indio_dev->pollfunc);
146 iio_sw_rb_free(indio_dev->ring);
147 }
148
149 int ade7758_configure_ring(struct iio_dev *indio_dev)
150 {
151 struct ade7758_state *st = iio_priv(indio_dev);
152 int ret = 0;
153
154 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
155 if (!indio_dev->ring) {
156 ret = -ENOMEM;
157 return ret;
158 }
159
160 /* Effectively select the ring buffer implementation */
161 indio_dev->ring->access = &ring_sw_access_funcs;
162 indio_dev->ring->setup_ops = &ade7758_ring_setup_ops;
163 indio_dev->ring->owner = THIS_MODULE;
164
165 indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
166 &ade7758_trigger_handler,
167 0,
168 indio_dev,
169 "ade7759_consumer%d",
170 indio_dev->id);
171 if (indio_dev->pollfunc == NULL) {
172 ret = -ENOMEM;
173 goto error_iio_sw_rb_free;
174 }
175
176 indio_dev->modes |= INDIO_RING_TRIGGERED;
177
178 st->tx_buf[0] = ADE7758_READ_REG(ADE7758_RSTATUS);
179 st->tx_buf[1] = 0;
180 st->tx_buf[2] = 0;
181 st->tx_buf[3] = 0;
182 st->tx_buf[4] = ADE7758_READ_REG(ADE7758_WFORM);
183 st->tx_buf[5] = 0;
184 st->tx_buf[6] = 0;
185 st->tx_buf[7] = 0;
186
187 /* build spi ring message */
188 st->ring_xfer[0].tx_buf = &st->tx_buf[0];
189 st->ring_xfer[0].len = 1;
190 st->ring_xfer[0].bits_per_word = 8;
191 st->ring_xfer[0].delay_usecs = 4;
192 st->ring_xfer[1].rx_buf = &st->rx_buf[1];
193 st->ring_xfer[1].len = 3;
194 st->ring_xfer[1].bits_per_word = 8;
195 st->ring_xfer[1].cs_change = 1;
196
197 st->ring_xfer[2].tx_buf = &st->tx_buf[4];
198 st->ring_xfer[2].len = 1;
199 st->ring_xfer[2].bits_per_word = 8;
200 st->ring_xfer[2].delay_usecs = 1;
201 st->ring_xfer[3].rx_buf = &st->rx_buf[5];
202 st->ring_xfer[3].len = 3;
203 st->ring_xfer[3].bits_per_word = 8;
204
205 spi_message_init(&st->ring_msg);
206 spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
207 spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);
208 spi_message_add_tail(&st->ring_xfer[2], &st->ring_msg);
209 spi_message_add_tail(&st->ring_xfer[3], &st->ring_msg);
210
211 return 0;
212
213 error_iio_sw_rb_free:
214 iio_sw_rb_free(indio_dev->ring);
215 return ret;
216 }
217
218 void ade7758_uninitialize_ring(struct iio_ring_buffer *ring)
219 {
220 iio_ring_buffer_unregister(ring);
221 }
This page took 0.037876 seconds and 6 git commands to generate.