2 * Copyright (C) 2010 Michael Hennerich, Analog Devices Inc.
3 * Copyright (C) 2008-2010 Jonathan Cameron
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
12 #include <linux/interrupt.h>
13 #include <linux/slab.h>
14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/i2c.h>
17 #include <linux/bitops.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/buffer.h>
21 #include "../ring_sw.h"
22 #include <linux/iio/trigger_consumer.h>
27 * ad799x_ring_preenable() setup the parameters of the ring before enabling
29 * The complex nature of the setting of the number of bytes per datum is due
30 * to this driver currently ensuring that the timestamp is stored at an 8
33 static int ad799x_ring_preenable(struct iio_dev
*indio_dev
)
35 struct ad799x_state
*st
= iio_priv(indio_dev
);
37 * Need to figure out the current mode based upon the requested
38 * scan mask in iio_dev
41 if (st
->id
== ad7997
|| st
->id
== ad7998
)
42 ad7997_8_set_scan_mode(st
, *indio_dev
->active_scan_mask
);
44 return iio_sw_buffer_preenable(indio_dev
);
48 * ad799x_trigger_handler() bh of trigger launched polling to ring buffer
50 * Currently there is no option in this driver to disable the saving of
51 * timestamps within the ring.
54 static irqreturn_t
ad799x_trigger_handler(int irq
, void *p
)
56 struct iio_poll_func
*pf
= p
;
57 struct iio_dev
*indio_dev
= pf
->indio_dev
;
58 struct ad799x_state
*st
= iio_priv(indio_dev
);
59 struct iio_buffer
*ring
= indio_dev
->buffer
;
65 rxbuf
= kmalloc(indio_dev
->scan_bytes
, GFP_KERNEL
);
74 (*indio_dev
->active_scan_mask
<< AD799X_CHANNEL_SHIFT
);
79 cmd
= (*indio_dev
->active_scan_mask
<< AD799X_CHANNEL_SHIFT
) |
84 cmd
= AD7997_8_READ_SEQUENCE
| AD7998_CONV_RES_REG
;
90 b_sent
= i2c_smbus_read_i2c_block_data(st
->client
,
91 cmd
, bitmap_weight(indio_dev
->active_scan_mask
,
92 indio_dev
->masklength
) * 2, rxbuf
);
96 time_ns
= iio_get_time_ns();
98 if (indio_dev
->scan_timestamp
)
99 memcpy(rxbuf
+ indio_dev
->scan_bytes
- sizeof(s64
),
100 &time_ns
, sizeof(time_ns
));
102 ring
->access
->store_to(indio_dev
->buffer
, rxbuf
, time_ns
);
108 iio_trigger_notify_done(indio_dev
->trig
);
113 static const struct iio_buffer_setup_ops ad799x_buf_setup_ops
= {
114 .preenable
= &ad799x_ring_preenable
,
115 .postenable
= &iio_triggered_buffer_postenable
,
116 .predisable
= &iio_triggered_buffer_predisable
,
119 int ad799x_register_ring_funcs_and_init(struct iio_dev
*indio_dev
)
123 indio_dev
->buffer
= iio_sw_rb_allocate(indio_dev
);
124 if (!indio_dev
->buffer
) {
128 indio_dev
->pollfunc
= iio_alloc_pollfunc(NULL
,
129 &ad799x_trigger_handler
,
135 if (indio_dev
->pollfunc
== NULL
) {
137 goto error_deallocate_sw_rb
;
140 /* Ring buffer functions - here trigger setup related */
141 indio_dev
->setup_ops
= &ad799x_buf_setup_ops
;
142 indio_dev
->buffer
->scan_timestamp
= true;
144 /* Flag that polled ring buffering is possible */
145 indio_dev
->modes
|= INDIO_BUFFER_TRIGGERED
;
148 error_deallocate_sw_rb
:
149 iio_sw_rb_free(indio_dev
->buffer
);
154 void ad799x_ring_cleanup(struct iio_dev
*indio_dev
)
156 iio_dealloc_pollfunc(indio_dev
->pollfunc
);
157 iio_sw_rb_free(indio_dev
->buffer
);