Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2011 Jonathan Cameron
0004  *
0005  * Buffer handling elements of industrial I/O reference driver.
0006  * Uses the kfifo buffer.
0007  *
0008  * To test without hardware use the sysfs trigger.
0009  */
0010 
0011 #include <linux/kernel.h>
0012 #include <linux/export.h>
0013 #include <linux/slab.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/irq.h>
0016 #include <linux/bitmap.h>
0017 
0018 #include <linux/iio/iio.h>
0019 #include <linux/iio/buffer.h>
0020 #include <linux/iio/trigger_consumer.h>
0021 #include <linux/iio/triggered_buffer.h>
0022 
0023 #include "iio_simple_dummy.h"
0024 
0025 /* Some fake data */
0026 
0027 static const s16 fakedata[] = {
0028     [DUMMY_INDEX_VOLTAGE_0] = 7,
0029     [DUMMY_INDEX_DIFFVOLTAGE_1M2] = -33,
0030     [DUMMY_INDEX_DIFFVOLTAGE_3M4] = -2,
0031     [DUMMY_INDEX_ACCELX] = 344,
0032 };
0033 
0034 /**
0035  * iio_simple_dummy_trigger_h() - the trigger handler function
0036  * @irq: the interrupt number
0037  * @p: private data - always a pointer to the poll func.
0038  *
0039  * This is the guts of buffered capture. On a trigger event occurring,
0040  * if the pollfunc is attached then this handler is called as a threaded
0041  * interrupt (and hence may sleep). It is responsible for grabbing data
0042  * from the device and pushing it into the associated buffer.
0043  */
0044 static irqreturn_t iio_simple_dummy_trigger_h(int irq, void *p)
0045 {
0046     struct iio_poll_func *pf = p;
0047     struct iio_dev *indio_dev = pf->indio_dev;
0048     int i = 0, j;
0049     u16 *data;
0050 
0051     data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
0052     if (!data)
0053         goto done;
0054 
0055     /*
0056      * Three common options here:
0057      * hardware scans:
0058      *   certain combinations of channels make up a fast read. The capture
0059      *   will consist of all of them. Hence we just call the grab data
0060      *   function and fill the buffer without processing.
0061      * software scans:
0062      *   can be considered to be random access so efficient reading is just
0063      *   a case of minimal bus transactions.
0064      * software culled hardware scans:
0065      *   occasionally a driver may process the nearest hardware scan to avoid
0066      *   storing elements that are not desired. This is the fiddliest option
0067      *   by far.
0068      * Here let's pretend we have random access. And the values are in the
0069      * constant table fakedata.
0070      */
0071     for_each_set_bit(j, indio_dev->active_scan_mask, indio_dev->masklength)
0072         data[i++] = fakedata[j];
0073 
0074     iio_push_to_buffers_with_timestamp(indio_dev, data,
0075                        iio_get_time_ns(indio_dev));
0076 
0077     kfree(data);
0078 
0079 done:
0080     /*
0081      * Tell the core we are done with this trigger and ready for the
0082      * next one.
0083      */
0084     iio_trigger_notify_done(indio_dev->trig);
0085 
0086     return IRQ_HANDLED;
0087 }
0088 
0089 static const struct iio_buffer_setup_ops iio_simple_dummy_buffer_setup_ops = {
0090 };
0091 
0092 int iio_simple_dummy_configure_buffer(struct iio_dev *indio_dev)
0093 {
0094     return iio_triggered_buffer_setup(indio_dev, NULL,
0095                       iio_simple_dummy_trigger_h,
0096                       &iio_simple_dummy_buffer_setup_ops);
0097 }
0098 
0099 /**
0100  * iio_simple_dummy_unconfigure_buffer() - release buffer resources
0101  * @indio_dev: device instance state
0102  */
0103 void iio_simple_dummy_unconfigure_buffer(struct iio_dev *indio_dev)
0104 {
0105     iio_triggered_buffer_cleanup(indio_dev);
0106 }