IIO: Move core headers to include/linux/iio
[linux-block.git] / drivers / staging / iio / adc / ad7887_ring.c
CommitLineData
2b4756aa 1/*
596d0609 2 * Copyright 2010-2011 Analog Devices Inc.
2b4756aa
MH
3 * Copyright (C) 2008 Jonathan Cameron
4 *
596d0609 5 * Licensed under the GPL-2.
2b4756aa
MH
6 *
7 * ad7887_ring.c
8 */
9
10#include <linux/interrupt.h>
2b4756aa
MH
11#include <linux/kernel.h>
12#include <linux/slab.h>
2b4756aa
MH
13#include <linux/spi/spi.h>
14
06458e27
JC
15#include <linux/iio/iio.h>
16#include <linux/iio/buffer.h>
2b4756aa 17#include "../ring_sw.h"
06458e27 18#include <linux/iio/trigger_consumer.h>
2b4756aa
MH
19
20#include "ad7887.h"
21
2b4756aa
MH
22/**
23 * ad7887_ring_preenable() setup the parameters of the ring before enabling
24 *
25 * The complex nature of the setting of the nuber of bytes per datum is due
26 * to this driver currently ensuring that the timestamp is stored at an 8
27 * byte boundary.
28 **/
29static int ad7887_ring_preenable(struct iio_dev *indio_dev)
30{
f490f42b 31 struct ad7887_state *st = iio_priv(indio_dev);
a64c0634 32 int ret;
2b4756aa 33
a64c0634
JC
34 ret = iio_sw_buffer_preenable(indio_dev);
35 if (ret < 0)
36 return ret;
e08d0265 37
32b5eeca 38 /* We know this is a single long so can 'cheat' */
550268ca 39 switch (*indio_dev->active_scan_mask) {
2b4756aa
MH
40 case (1 << 0):
41 st->ring_msg = &st->msg[AD7887_CH0];
42 break;
43 case (1 << 1):
44 st->ring_msg = &st->msg[AD7887_CH1];
45 /* Dummy read: push CH1 setting down to hardware */
46 spi_sync(st->spi, st->ring_msg);
47 break;
48 case ((1 << 1) | (1 << 0)):
49 st->ring_msg = &st->msg[AD7887_CH0_CH1];
50 break;
51 }
52
53 return 0;
54}
55
56static int ad7887_ring_postdisable(struct iio_dev *indio_dev)
57{
f490f42b 58 struct ad7887_state *st = iio_priv(indio_dev);
2b4756aa
MH
59
60 /* dummy read: restore default CH0 settin */
61 return spi_sync(st->spi, &st->msg[AD7887_CH0]);
62}
63
64/**
e362dfbb 65 * ad7887_trigger_handler() bh of trigger launched polling to ring buffer
2b4756aa
MH
66 *
67 * Currently there is no option in this driver to disable the saving of
68 * timestamps within the ring.
2b4756aa 69 **/
e362dfbb 70static irqreturn_t ad7887_trigger_handler(int irq, void *p)
2b4756aa 71{
e362dfbb 72 struct iio_poll_func *pf = p;
e65bc6ac 73 struct iio_dev *indio_dev = pf->indio_dev;
f490f42b 74 struct ad7887_state *st = iio_priv(indio_dev);
2b4756aa
MH
75 s64 time_ns;
76 __u8 *buf;
77 int b_sent;
2b4756aa 78
550268ca
JC
79 unsigned int bytes = bitmap_weight(indio_dev->active_scan_mask,
80 indio_dev->masklength) *
596d0609 81 st->chip_info->channel[0].scan_type.storagebits / 8;
2b4756aa 82
a64c0634 83 buf = kzalloc(indio_dev->scan_bytes, GFP_KERNEL);
2b4756aa 84 if (buf == NULL)
e362dfbb 85 return -ENOMEM;
2b4756aa
MH
86
87 b_sent = spi_sync(st->spi, st->ring_msg);
88 if (b_sent)
89 goto done;
90
91 time_ns = iio_get_time_ns();
92
93 memcpy(buf, st->data, bytes);
fd6487f8 94 if (indio_dev->scan_timestamp)
a64c0634 95 memcpy(buf + indio_dev->scan_bytes - sizeof(s64),
e362dfbb 96 &time_ns, sizeof(time_ns));
2b4756aa 97
14555b14 98 indio_dev->buffer->access->store_to(indio_dev->buffer, buf, time_ns);
2b4756aa
MH
99done:
100 kfree(buf);
596d0609 101 iio_trigger_notify_done(indio_dev->trig);
e362dfbb
JC
102
103 return IRQ_HANDLED;
2b4756aa
MH
104}
105
14555b14 106static const struct iio_buffer_setup_ops ad7887_ring_setup_ops = {
5565a450 107 .preenable = &ad7887_ring_preenable,
3b99fb76
JC
108 .postenable = &iio_triggered_buffer_postenable,
109 .predisable = &iio_triggered_buffer_predisable,
5565a450
JC
110 .postdisable = &ad7887_ring_postdisable,
111};
112
2b4756aa
MH
113int ad7887_register_ring_funcs_and_init(struct iio_dev *indio_dev)
114{
2b4756aa
MH
115 int ret;
116
14555b14
JC
117 indio_dev->buffer = iio_sw_rb_allocate(indio_dev);
118 if (!indio_dev->buffer) {
2b4756aa
MH
119 ret = -ENOMEM;
120 goto error_ret;
121 }
0ed731d2
JC
122 indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
123 &ad7887_trigger_handler,
124 IRQF_ONESHOT,
125 indio_dev,
126 "ad7887_consumer%d",
127 indio_dev->id);
e362dfbb
JC
128 if (indio_dev->pollfunc == NULL) {
129 ret = -ENOMEM;
2b4756aa 130 goto error_deallocate_sw_rb;
e362dfbb 131 }
2b4756aa 132 /* Ring buffer functions - here trigger setup related */
1612244f 133 indio_dev->setup_ops = &ad7887_ring_setup_ops;
2b4756aa 134
2b4756aa 135 /* Flag that polled ring buffering is possible */
ec3afa40 136 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
2b4756aa 137 return 0;
0ed731d2 138
2b4756aa 139error_deallocate_sw_rb:
14555b14 140 iio_sw_rb_free(indio_dev->buffer);
2b4756aa
MH
141error_ret:
142 return ret;
143}
144
145void ad7887_ring_cleanup(struct iio_dev *indio_dev)
146{
0ed731d2 147 iio_dealloc_pollfunc(indio_dev->pollfunc);
14555b14 148 iio_sw_rb_free(indio_dev->buffer);
2b4756aa 149}