1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2011 Jonathan Cameron 4 * 5 * Buffer handling elements of industrial I/O reference driver. 6 * Uses the kfifo buffer. 7 * 8 * To test without hardware use the sysfs trigger. 9 */ 10 11 #include <linux/kernel.h> 12 #include <linux/export.h> 13 #include <linux/slab.h> 14 #include <linux/interrupt.h> 15 #include <linux/irq.h> 16 #include <linux/bitmap.h> 17 18 #include <linux/iio/iio.h> 19 #include <linux/iio/buffer.h> 20 #include <linux/iio/trigger_consumer.h> 21 #include <linux/iio/triggered_buffer.h> 22 23 #include "iio_simple_dummy.h" 24 25 /* Some fake data */ 26 27 static const s16 fakedata[] = { 28 [DUMMY_INDEX_VOLTAGE_0] = 7, 29 [DUMMY_INDEX_DIFFVOLTAGE_1M2] = -33, 30 [DUMMY_INDEX_DIFFVOLTAGE_3M4] = -2, 31 [DUMMY_INDEX_ACCELX] = 344, 32 }; 33 34 struct dummy_scan { 35 s16 data[ARRAY_SIZE(fakedata)]; 36 aligned_s64 timestamp; 37 }; 38 39 /** 40 * iio_simple_dummy_trigger_h() - the trigger handler function 41 * @irq: the interrupt number 42 * @p: private data - always a pointer to the poll func. 43 * 44 * This is the guts of buffered capture. On a trigger event occurring, 45 * if the pollfunc is attached then this handler is called as a threaded 46 * interrupt (and hence may sleep). It is responsible for grabbing data 47 * from the device and pushing it into the associated buffer. 48 */ 49 static irqreturn_t iio_simple_dummy_trigger_h(int irq, void *p) 50 { 51 struct iio_poll_func *pf = p; 52 struct iio_dev *indio_dev = pf->indio_dev; 53 struct dummy_scan *scan; 54 int i = 0, j; 55 56 /* 57 * Note that some buses such as SPI require DMA safe buffers which 58 * cannot be on the stack. Two easy ways to do this: 59 * - Local kzalloc (as done here) 60 * - A buffer at the end of the structure accessed via iio_priv() 61 * that is marked __aligned(IIO_DMA_MINALIGN). 62 */ 63 scan = kzalloc(sizeof(*scan), GFP_KERNEL); 64 if (!scan) 65 goto done; 66 67 /* 68 * Three common options here: 69 * hardware scans: 70 * certain combinations of channels make up a fast read. The capture 71 * will consist of all of them. Hence we just call the grab data 72 * function and fill the buffer without processing. 73 * software scans: 74 * can be considered to be random access so efficient reading is just 75 * a case of minimal bus transactions. 76 * software culled hardware scans: 77 * occasionally a driver may process the nearest hardware scan to avoid 78 * storing elements that are not desired. This is the fiddliest option 79 * by far. 80 * Here let's pretend we have random access. And the values are in the 81 * constant table fakedata. 82 */ 83 iio_for_each_active_channel(indio_dev, j) 84 scan->data[i++] = fakedata[j]; 85 86 iio_push_to_buffers_with_ts(indio_dev, scan, sizeof(*scan), 87 iio_get_time_ns(indio_dev)); 88 89 kfree(scan); 90 done: 91 /* 92 * Tell the core we are done with this trigger and ready for the 93 * next one. 94 */ 95 iio_trigger_notify_done(indio_dev->trig); 96 97 return IRQ_HANDLED; 98 } 99 100 static const struct iio_buffer_setup_ops iio_simple_dummy_buffer_setup_ops = { 101 }; 102 103 int iio_simple_dummy_configure_buffer(struct iio_dev *indio_dev) 104 { 105 return iio_triggered_buffer_setup(indio_dev, NULL, 106 iio_simple_dummy_trigger_h, 107 &iio_simple_dummy_buffer_setup_ops); 108 } 109 110 /** 111 * iio_simple_dummy_unconfigure_buffer() - release buffer resources 112 * @indio_dev: device instance state 113 */ 114 void iio_simple_dummy_unconfigure_buffer(struct iio_dev *indio_dev) 115 { 116 iio_triggered_buffer_cleanup(indio_dev); 117 } 118