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 /** 35 * iio_simple_dummy_trigger_h() - the trigger handler function 36 * @irq: the interrupt number 37 * @p: private data - always a pointer to the poll func. 38 * 39 * This is the guts of buffered capture. On a trigger event occurring, 40 * if the pollfunc is attached then this handler is called as a threaded 41 * interrupt (and hence may sleep). It is responsible for grabbing data 42 * from the device and pushing it into the associated buffer. 43 */ 44 static irqreturn_t iio_simple_dummy_trigger_h(int irq, void *p) 45 { 46 struct iio_poll_func *pf = p; 47 struct iio_dev *indio_dev = pf->indio_dev; 48 u16 *data; 49 50 data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL); 51 if (!data) 52 goto done; 53 54 if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength)) { 55 /* 56 * Three common options here: 57 * hardware scans: certain combinations of channels make 58 * up a fast read. The capture will consist of all of them. 59 * Hence we just call the grab data function and fill the 60 * buffer without processing. 61 * software scans: can be considered to be random access 62 * so efficient reading is just a case of minimal bus 63 * transactions. 64 * software culled hardware scans: 65 * occasionally a driver may process the nearest hardware 66 * scan to avoid storing elements that are not desired. This 67 * is the fiddliest option by far. 68 * Here let's pretend we have random access. And the values are 69 * in the constant table fakedata. 70 */ 71 int i, j; 72 73 for (i = 0, j = 0; 74 i < bitmap_weight(indio_dev->active_scan_mask, 75 indio_dev->masklength); 76 i++, j++) { 77 j = find_next_bit(indio_dev->active_scan_mask, 78 indio_dev->masklength, j); 79 /* random access read from the 'device' */ 80 data[i] = fakedata[j]; 81 } 82 } 83 84 iio_push_to_buffers_with_timestamp(indio_dev, data, 85 iio_get_time_ns(indio_dev)); 86 87 kfree(data); 88 89 done: 90 /* 91 * Tell the core we are done with this trigger and ready for the 92 * next one. 93 */ 94 iio_trigger_notify_done(indio_dev->trig); 95 96 return IRQ_HANDLED; 97 } 98 99 static const struct iio_buffer_setup_ops iio_simple_dummy_buffer_setup_ops = { 100 }; 101 102 int iio_simple_dummy_configure_buffer(struct iio_dev *indio_dev) 103 { 104 return iio_triggered_buffer_setup(indio_dev, NULL, 105 iio_simple_dummy_trigger_h, 106 &iio_simple_dummy_buffer_setup_ops); 107 } 108 109 /** 110 * iio_simple_dummy_unconfigure_buffer() - release buffer resources 111 * @indio_dev: device instance state 112 */ 113 void iio_simple_dummy_unconfigure_buffer(struct iio_dev *indio_dev) 114 { 115 iio_triggered_buffer_cleanup(indio_dev); 116 } 117