1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2dd2e16cfSLee Jones /*
3415f7924SCristina Opriceana * Copyright (c) 2011 Jonathan Cameron
4415f7924SCristina Opriceana *
5415f7924SCristina Opriceana * Buffer handling elements of industrial I/O reference driver.
6415f7924SCristina Opriceana * Uses the kfifo buffer.
7415f7924SCristina Opriceana *
8415f7924SCristina Opriceana * To test without hardware use the sysfs trigger.
9415f7924SCristina Opriceana */
10415f7924SCristina Opriceana
11415f7924SCristina Opriceana #include <linux/kernel.h>
12415f7924SCristina Opriceana #include <linux/export.h>
13415f7924SCristina Opriceana #include <linux/slab.h>
14415f7924SCristina Opriceana #include <linux/interrupt.h>
15415f7924SCristina Opriceana #include <linux/irq.h>
16415f7924SCristina Opriceana #include <linux/bitmap.h>
17415f7924SCristina Opriceana
18415f7924SCristina Opriceana #include <linux/iio/iio.h>
198abd5ba5SJonathan Cameron #include <linux/iio/buffer.h>
20738f6ba1SAlexandru Ardelean #include <linux/iio/trigger_consumer.h>
21738f6ba1SAlexandru Ardelean #include <linux/iio/triggered_buffer.h>
22415f7924SCristina Opriceana
23415f7924SCristina Opriceana #include "iio_simple_dummy.h"
24415f7924SCristina Opriceana
25415f7924SCristina Opriceana /* Some fake data */
26415f7924SCristina Opriceana
27415f7924SCristina Opriceana static const s16 fakedata[] = {
28f3cf3fb7SGreg Kroah-Hartman [DUMMY_INDEX_VOLTAGE_0] = 7,
29f3cf3fb7SGreg Kroah-Hartman [DUMMY_INDEX_DIFFVOLTAGE_1M2] = -33,
30f3cf3fb7SGreg Kroah-Hartman [DUMMY_INDEX_DIFFVOLTAGE_3M4] = -2,
31f3cf3fb7SGreg Kroah-Hartman [DUMMY_INDEX_ACCELX] = 344,
32415f7924SCristina Opriceana };
33415f7924SCristina Opriceana
34415f7924SCristina Opriceana /**
35415f7924SCristina Opriceana * iio_simple_dummy_trigger_h() - the trigger handler function
36415f7924SCristina Opriceana * @irq: the interrupt number
37415f7924SCristina Opriceana * @p: private data - always a pointer to the poll func.
38415f7924SCristina Opriceana *
39415f7924SCristina Opriceana * This is the guts of buffered capture. On a trigger event occurring,
40415f7924SCristina Opriceana * if the pollfunc is attached then this handler is called as a threaded
41415f7924SCristina Opriceana * interrupt (and hence may sleep). It is responsible for grabbing data
42415f7924SCristina Opriceana * from the device and pushing it into the associated buffer.
43415f7924SCristina Opriceana */
iio_simple_dummy_trigger_h(int irq,void * p)44415f7924SCristina Opriceana static irqreturn_t iio_simple_dummy_trigger_h(int irq, void *p)
45415f7924SCristina Opriceana {
46415f7924SCristina Opriceana struct iio_poll_func *pf = p;
47415f7924SCristina Opriceana struct iio_dev *indio_dev = pf->indio_dev;
48c8f14e2bSYury Norov int i = 0, j;
49415f7924SCristina Opriceana u16 *data;
50415f7924SCristina Opriceana
51415f7924SCristina Opriceana data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
52415f7924SCristina Opriceana if (!data)
53415f7924SCristina Opriceana goto done;
54415f7924SCristina Opriceana
55415f7924SCristina Opriceana /*
56415f7924SCristina Opriceana * Three common options here:
57c8f14e2bSYury Norov * hardware scans:
58c8f14e2bSYury Norov * certain combinations of channels make up a fast read. The capture
59c8f14e2bSYury Norov * will consist of all of them. Hence we just call the grab data
60c8f14e2bSYury Norov * function and fill the buffer without processing.
61c8f14e2bSYury Norov * software scans:
62c8f14e2bSYury Norov * can be considered to be random access so efficient reading is just
63c8f14e2bSYury Norov * a case of minimal bus transactions.
64415f7924SCristina Opriceana * software culled hardware scans:
65c8f14e2bSYury Norov * occasionally a driver may process the nearest hardware scan to avoid
66c8f14e2bSYury Norov * storing elements that are not desired. This is the fiddliest option
67c8f14e2bSYury Norov * by far.
68c8f14e2bSYury Norov * Here let's pretend we have random access. And the values are in the
69c8f14e2bSYury Norov * constant table fakedata.
70415f7924SCristina Opriceana */
71*05552370SNuno Sa iio_for_each_active_channel(indio_dev, j)
72c8f14e2bSYury Norov data[i++] = fakedata[j];
73415f7924SCristina Opriceana
74bc2b7dabSGregor Boirie iio_push_to_buffers_with_timestamp(indio_dev, data,
75bc2b7dabSGregor Boirie iio_get_time_ns(indio_dev));
76415f7924SCristina Opriceana
77415f7924SCristina Opriceana kfree(data);
78415f7924SCristina Opriceana
79415f7924SCristina Opriceana done:
80415f7924SCristina Opriceana /*
81415f7924SCristina Opriceana * Tell the core we are done with this trigger and ready for the
82415f7924SCristina Opriceana * next one.
83415f7924SCristina Opriceana */
84415f7924SCristina Opriceana iio_trigger_notify_done(indio_dev->trig);
85415f7924SCristina Opriceana
86415f7924SCristina Opriceana return IRQ_HANDLED;
87415f7924SCristina Opriceana }
88415f7924SCristina Opriceana
89415f7924SCristina Opriceana static const struct iio_buffer_setup_ops iio_simple_dummy_buffer_setup_ops = {
90415f7924SCristina Opriceana };
91415f7924SCristina Opriceana
iio_simple_dummy_configure_buffer(struct iio_dev * indio_dev)92415f7924SCristina Opriceana int iio_simple_dummy_configure_buffer(struct iio_dev *indio_dev)
93415f7924SCristina Opriceana {
94738f6ba1SAlexandru Ardelean return iio_triggered_buffer_setup(indio_dev, NULL,
95738f6ba1SAlexandru Ardelean iio_simple_dummy_trigger_h,
96738f6ba1SAlexandru Ardelean &iio_simple_dummy_buffer_setup_ops);
97415f7924SCristina Opriceana }
98415f7924SCristina Opriceana
99415f7924SCristina Opriceana /**
100415f7924SCristina Opriceana * iio_simple_dummy_unconfigure_buffer() - release buffer resources
101dd2e16cfSLee Jones * @indio_dev: device instance state
102415f7924SCristina Opriceana */
iio_simple_dummy_unconfigure_buffer(struct iio_dev * indio_dev)103415f7924SCristina Opriceana void iio_simple_dummy_unconfigure_buffer(struct iio_dev *indio_dev)
104415f7924SCristina Opriceana {
105738f6ba1SAlexandru Ardelean iio_triggered_buffer_cleanup(indio_dev);
106415f7924SCristina Opriceana }
107