1 /*
2  * Copyright 2010 Analog Devices Inc.
3  * Copyright (C) 2008 Jonathan Cameron
4  *
5  * Licensed under the GPL-2 or later.
6  *
7  * ad7476_ring.c
8  */
9 
10 #include <linux/interrupt.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/spi/spi.h>
15 
16 #include "../iio.h"
17 #include "../buffer.h"
18 #include "../ring_sw.h"
19 #include "../trigger_consumer.h"
20 
21 #include "ad7476.h"
22 
23 /**
24  * ad7476_ring_preenable() setup the parameters of the ring before enabling
25  *
26  * The complex nature of the setting of the nuber of bytes per datum is due
27  * to this driver currently ensuring that the timestamp is stored at an 8
28  * byte boundary.
29  **/
ad7476_ring_preenable(struct iio_dev * indio_dev)30 static int ad7476_ring_preenable(struct iio_dev *indio_dev)
31 {
32 	struct ad7476_state *st = iio_priv(indio_dev);
33 	struct iio_buffer *ring = indio_dev->buffer;
34 
35 	st->d_size = bitmap_weight(indio_dev->active_scan_mask,
36 				   indio_dev->masklength) *
37 		st->chip_info->channel[0].scan_type.storagebits / 8;
38 
39 	if (ring->scan_timestamp) {
40 		st->d_size += sizeof(s64);
41 
42 		if (st->d_size % sizeof(s64))
43 			st->d_size += sizeof(s64) - (st->d_size % sizeof(s64));
44 	}
45 
46 	if (indio_dev->buffer->access->set_bytes_per_datum)
47 		indio_dev->buffer->access->
48 			set_bytes_per_datum(indio_dev->buffer, st->d_size);
49 
50 	return 0;
51 }
52 
ad7476_trigger_handler(int irq,void * p)53 static irqreturn_t ad7476_trigger_handler(int irq, void  *p)
54 {
55 	struct iio_poll_func *pf = p;
56 	struct iio_dev *indio_dev = pf->indio_dev;
57 	struct ad7476_state *st = iio_priv(indio_dev);
58 	s64 time_ns;
59 	__u8 *rxbuf;
60 	int b_sent;
61 
62 	rxbuf = kzalloc(st->d_size, GFP_KERNEL);
63 	if (rxbuf == NULL)
64 		return -ENOMEM;
65 
66 	b_sent = spi_read(st->spi, rxbuf,
67 			  st->chip_info->channel[0].scan_type.storagebits / 8);
68 	if (b_sent < 0)
69 		goto done;
70 
71 	time_ns = iio_get_time_ns();
72 
73 	if (indio_dev->buffer->scan_timestamp)
74 		memcpy(rxbuf + st->d_size - sizeof(s64),
75 			&time_ns, sizeof(time_ns));
76 
77 	indio_dev->buffer->access->store_to(indio_dev->buffer, rxbuf, time_ns);
78 done:
79 	iio_trigger_notify_done(indio_dev->trig);
80 	kfree(rxbuf);
81 
82 	return IRQ_HANDLED;
83 }
84 
85 static const struct iio_buffer_setup_ops ad7476_ring_setup_ops = {
86 	.preenable = &ad7476_ring_preenable,
87 	.postenable = &iio_triggered_buffer_postenable,
88 	.predisable = &iio_triggered_buffer_predisable,
89 };
90 
ad7476_register_ring_funcs_and_init(struct iio_dev * indio_dev)91 int ad7476_register_ring_funcs_and_init(struct iio_dev *indio_dev)
92 {
93 	struct ad7476_state *st = iio_priv(indio_dev);
94 	int ret = 0;
95 
96 	indio_dev->buffer = iio_sw_rb_allocate(indio_dev);
97 	if (!indio_dev->buffer) {
98 		ret = -ENOMEM;
99 		goto error_ret;
100 	}
101 	/* Effectively select the ring buffer implementation */
102 	indio_dev->buffer->access = &ring_sw_access_funcs;
103 	indio_dev->pollfunc
104 		= iio_alloc_pollfunc(NULL,
105 				     &ad7476_trigger_handler,
106 				     IRQF_ONESHOT,
107 				     indio_dev,
108 				     "%s_consumer%d",
109 				     spi_get_device_id(st->spi)->name,
110 				     indio_dev->id);
111 	if (indio_dev->pollfunc == NULL) {
112 		ret = -ENOMEM;
113 		goto error_deallocate_sw_rb;
114 	}
115 
116 	/* Ring buffer functions - here trigger setup related */
117 	indio_dev->setup_ops = &ad7476_ring_setup_ops;
118 	indio_dev->buffer->scan_timestamp = true;
119 
120 	/* Flag that polled ring buffering is possible */
121 	indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
122 	return 0;
123 
124 error_deallocate_sw_rb:
125 	iio_sw_rb_free(indio_dev->buffer);
126 error_ret:
127 	return ret;
128 }
129 
ad7476_ring_cleanup(struct iio_dev * indio_dev)130 void ad7476_ring_cleanup(struct iio_dev *indio_dev)
131 {
132 	iio_dealloc_pollfunc(indio_dev->pollfunc);
133 	iio_sw_rb_free(indio_dev->buffer);
134 }
135