staging:iio:trigger:sysfs trigger: Add a release function to avoid warning on module...
[deliverable/linux.git] / drivers / staging / iio / meter / ade7758_ring.c
CommitLineData
8a27a023
MH
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 */
8210cfe9 8#include <linux/interrupt.h>
8210cfe9
BS
9#include <linux/kernel.h>
10#include <linux/spi/spi.h>
11#include <linux/slab.h>
8a27a023 12#include <asm/unaligned.h>
8210cfe9
BS
13
14#include "../iio.h"
8210cfe9 15#include "../ring_sw.h"
8210cfe9
BS
16#include "../trigger.h"
17#include "ade7758.h"
18
19/**
8a27a023 20 * ade7758_spi_read_burst() - read data registers
8210cfe9 21 * @dev: device associated with child of actual device (iio_dev or iio_trig)
8210cfe9 22 **/
8a27a023 23static int ade7758_spi_read_burst(struct device *dev)
8210cfe9 24{
8210cfe9 25 struct iio_dev *indio_dev = dev_get_drvdata(dev);
a3f02370 26 struct ade7758_state *st = iio_priv(indio_dev);
8210cfe9
BS
27 int ret;
28
8a27a023 29 ret = spi_sync(st->us, &st->ring_msg);
8210cfe9
BS
30 if (ret)
31 dev_err(&st->us->dev, "problem when reading WFORM value\n");
32
8a27a023
MH
33 return ret;
34}
35
36static int ade7758_write_waveform_type(struct device *dev, unsigned type)
37{
38 int ret;
39 u8 reg;
40
41 ret = ade7758_spi_read_reg_8(dev,
42 ADE7758_WAVMODE,
43 &reg);
44 if (ret)
45 goto out;
46
47 reg &= ~0x1F;
48 reg |= type & 0x1F;
8210cfe9 49
8a27a023
MH
50 ret = ade7758_spi_write_reg_8(dev,
51 ADE7758_WAVMODE,
52 reg);
53out:
8210cfe9
BS
54 return ret;
55}
56
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.
59 */
3f00ca47 60static irqreturn_t ade7758_trigger_handler(int irq, void *p)
8210cfe9 61{
3f00ca47
JC
62 struct iio_poll_func *pf = p;
63 struct iio_dev *indio_dev = pf->private_data;
64 struct iio_ring_buffer *ring = indio_dev->ring;
a3f02370 65 struct ade7758_state *st = iio_priv(indio_dev);
8a27a023
MH
66 s64 dat64[2];
67 u32 *dat32 = (u32 *)dat64;
8210cfe9
BS
68
69 if (ring->scan_count)
a3f02370 70 if (ade7758_spi_read_burst(&indio_dev->dev) >= 0)
8a27a023 71 *dat32 = get_unaligned_be32(&st->rx_buf[5]) & 0xFFFFFF;
8210cfe9
BS
72
73 /* Guaranteed to be aligned with 8 byte boundary */
74 if (ring->scan_timestamp)
8a27a023 75 dat64[1] = pf->timestamp;
8210cfe9 76
5565a450 77 ring->access->store_to(ring, (u8 *)dat64, pf->timestamp);
8210cfe9 78
a3f02370 79 iio_trigger_notify_done(indio_dev->trig);
8210cfe9 80
3f00ca47 81 return IRQ_HANDLED;
8210cfe9
BS
82}
83
8a27a023
MH
84/**
85 * ade7758_ring_preenable() setup the parameters of the ring before enabling
86 *
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 **/
91static int ade7758_ring_preenable(struct iio_dev *indio_dev)
92{
a3f02370 93 struct ade7758_state *st = iio_priv(indio_dev);
8a27a023
MH
94 struct iio_ring_buffer *ring = indio_dev->ring;
95 size_t d_size;
96 unsigned channel;
97
98 if (!ring->scan_count)
99 return -EINVAL;
100
101 channel = __ffs(ring->scan_mask);
102
103 d_size = st->ade7758_ring_channels[channel].scan_type.storagebits / 8;
104
105 if (ring->scan_timestamp) {
106 d_size += sizeof(s64);
107
108 if (d_size % sizeof(s64))
109 d_size += sizeof(s64) - (d_size % sizeof(s64));
110 }
111
5565a450
JC
112 if (indio_dev->ring->access->set_bytes_per_datum)
113 indio_dev->ring->access->set_bytes_per_datum(indio_dev->ring,
8a27a023
MH
114 d_size);
115
116 ade7758_write_waveform_type(&indio_dev->dev,
117 st->ade7758_ring_channels[channel].address);
118
119 return 0;
120}
121
5565a450
JC
122static const struct iio_ring_setup_ops ade7758_ring_setup_ops = {
123 .preenable = &ade7758_ring_preenable,
124 .postenable = &iio_triggered_ring_postenable,
125 .predisable = &iio_triggered_ring_predisable,
126};
127
8210cfe9
BS
128void ade7758_unconfigure_ring(struct iio_dev *indio_dev)
129{
8a27a023
MH
130 /* ensure that the trigger has been detached */
131 if (indio_dev->trig) {
132 iio_put_trigger(indio_dev->trig);
133 iio_trigger_dettach_poll_func(indio_dev->trig,
134 indio_dev->pollfunc);
135 }
0ed731d2 136 iio_dealloc_pollfunc(indio_dev->pollfunc);
8210cfe9
BS
137 iio_sw_rb_free(indio_dev->ring);
138}
139
140int ade7758_configure_ring(struct iio_dev *indio_dev)
141{
a3f02370 142 struct ade7758_state *st = iio_priv(indio_dev);
8210cfe9 143 int ret = 0;
8210cfe9 144
8a27a023
MH
145 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
146 if (!indio_dev->ring) {
8210cfe9
BS
147 ret = -ENOMEM;
148 return ret;
149 }
8a27a023 150
8210cfe9 151 /* Effectively select the ring buffer implementation */
5565a450
JC
152 indio_dev->ring->access = &ring_sw_access_funcs;
153 indio_dev->ring->setup_ops = &ade7758_ring_setup_ops;
8a27a023 154 indio_dev->ring->owner = THIS_MODULE;
8210cfe9 155
0ed731d2
JC
156 indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
157 &ade7758_trigger_handler,
158 0,
159 indio_dev,
160 "ade7759_consumer%d",
161 indio_dev->id);
3f00ca47
JC
162 if (indio_dev->pollfunc == NULL) {
163 ret = -ENOMEM;
8210cfe9 164 goto error_iio_sw_rb_free;
3f00ca47 165 }
0ed731d2 166
8210cfe9 167 indio_dev->modes |= INDIO_RING_TRIGGERED;
3f00ca47 168
8a27a023
MH
169 st->tx_buf[0] = ADE7758_READ_REG(ADE7758_RSTATUS);
170 st->tx_buf[1] = 0;
171 st->tx_buf[2] = 0;
172 st->tx_buf[3] = 0;
173 st->tx_buf[4] = ADE7758_READ_REG(ADE7758_WFORM);
174 st->tx_buf[5] = 0;
175 st->tx_buf[6] = 0;
176 st->tx_buf[7] = 0;
177
178 /* build spi ring message */
179 st->ring_xfer[0].tx_buf = &st->tx_buf[0];
180 st->ring_xfer[0].len = 1;
181 st->ring_xfer[0].bits_per_word = 8;
182 st->ring_xfer[0].delay_usecs = 4;
183 st->ring_xfer[1].rx_buf = &st->rx_buf[1];
184 st->ring_xfer[1].len = 3;
185 st->ring_xfer[1].bits_per_word = 8;
186 st->ring_xfer[1].cs_change = 1;
187
188 st->ring_xfer[2].tx_buf = &st->tx_buf[4];
189 st->ring_xfer[2].len = 1;
190 st->ring_xfer[2].bits_per_word = 8;
191 st->ring_xfer[2].delay_usecs = 1;
192 st->ring_xfer[3].rx_buf = &st->rx_buf[5];
193 st->ring_xfer[3].len = 3;
194 st->ring_xfer[3].bits_per_word = 8;
195
196 spi_message_init(&st->ring_msg);
197 spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
198 spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);
199 spi_message_add_tail(&st->ring_xfer[2], &st->ring_msg);
200 spi_message_add_tail(&st->ring_xfer[3], &st->ring_msg);
201
8210cfe9
BS
202 return 0;
203
204error_iio_sw_rb_free:
205 iio_sw_rb_free(indio_dev->ring);
206 return ret;
207}
208
8210cfe9
BS
209void ade7758_uninitialize_ring(struct iio_ring_buffer *ring)
210{
211 iio_ring_buffer_unregister(ring);
212}
This page took 0.109238 seconds and 5 git commands to generate.