Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: add support for hardware fifo

Some devices have hardware buffers that can store a number of samples
for later consumption. Hardware usually provides interrupts to notify
the processor when the FIFO is full or when it has reached a certain
watermark level. This helps with reducing the number of interrupts to
the host processor and thus it helps decreasing the power consumption.

This patch enables usage of hardware FIFOs for IIO devices in
conjunction with software device buffers. When the hardware FIFO is
enabled the samples are stored in the hardware FIFO. The samples are
later flushed to the device software buffer when the number of entries
in the hardware FIFO reaches the hardware watermark or when a flush
operation is triggered by the user when doing a non-blocking read
on an empty software device buffer.

In order to implement hardware FIFO support the device drivers must
implement the following new operations: setting and getting the
hardware FIFO watermark level, flushing the hardware FIFO to the
software device buffer. The device must also expose information about
the hardware FIFO such it's minimum and maximum watermark and if
necessary a list of supported watermark values. Finally, the device
driver must activate the hardware FIFO when the device buffer is
enabled, if the current device settings allows it.

The software device buffer watermark is passed by the IIO core to the
device driver as a hint for the hardware FIFO watermark. The device
driver can adjust this value to allow for hardware limitations (such
as capping it to the maximum hardware watermark or adjust it to a
value that is supported by the hardware). It can also disable the
hardware watermark (and implicitly the hardware FIFO) it this value is
below the minimum hardware watermark.

Since a driver may support hardware FIFO only when not in triggered
buffer mode (due to different semantics of hardware FIFO sampling and
triggered sampling) this patch changes the IIO core code to allow
falling back to non-triggered buffered mode if no trigger is enabled.

Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
Reviewed-by: Lars-Peter Clausen <lars@metafoo.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>

authored by

Octavian Purdila and committed by
Jonathan Cameron
f4f4673b 37d34556

+128 -14
+69
Documentation/ABI/testing/sysfs-bus-iio
··· 1295 1295 allows the application to block on poll with a timeout and read 1296 1296 the available samples after the timeout expires and thus have a 1297 1297 maximum delay guarantee. 1298 + 1299 + What: /sys/bus/iio/devices/iio:deviceX/buffer/hwfifo_enabled 1300 + KernelVersion: 4.2 1301 + Contact: linux-iio@vger.kernel.org 1302 + Description: 1303 + A read-only boolean value that indicates if the hardware fifo is 1304 + currently enabled or disabled. If the device does not have a 1305 + hardware fifo this entry is not present. 1306 + The hardware fifo is enabled when the buffer is enabled if the 1307 + current hardware fifo watermark level is set and other current 1308 + device settings allows it (e.g. if a trigger is set that samples 1309 + data differently that the hardware fifo does then hardware fifo 1310 + will not enabled). 1311 + If the hardware fifo is enabled and the level of the hardware 1312 + fifo reaches the hardware fifo watermark level the device will 1313 + flush its hardware fifo to the device buffer. Doing a non 1314 + blocking read on the device when no samples are present in the 1315 + device buffer will also force a flush. 1316 + When the hardware fifo is enabled there is no need to use a 1317 + trigger to use buffer mode since the watermark settings 1318 + guarantees that the hardware fifo is flushed to the device 1319 + buffer. 1320 + 1321 + What: /sys/bus/iio/devices/iio:deviceX/buffer/hwfifo_watermark 1322 + KernelVersion: 4.2 1323 + Contact: linux-iio@vger.kernel.org 1324 + Description: 1325 + Read-only entry that contains a single integer specifying the 1326 + current watermark level for the hardware fifo. If the device 1327 + does not have a hardware fifo this entry is not present. 1328 + The watermark level for the hardware fifo is set by the driver 1329 + based on the value set by the user in buffer/watermark but 1330 + taking into account hardware limitations (e.g. most hardware 1331 + buffers are limited to 32-64 samples, some hardware buffers 1332 + watermarks are fixed or have minimum levels). A value of 0 1333 + means that the hardware watermark is unset. 1334 + 1335 + What: /sys/bus/iio/devices/iio:deviceX/buffer/hwfifo_watermark_min 1336 + KernelVersion: 4.2 1337 + Contact: linux-iio@vger.kernel.org 1338 + Description: 1339 + A single positive integer specifying the minimum watermark level 1340 + for the hardware fifo of this device. If the device does not 1341 + have a hardware fifo this entry is not present. 1342 + If the user sets buffer/watermark to a value less than this one, 1343 + then the hardware watermark will remain unset. 1344 + 1345 + What: /sys/bus/iio/devices/iio:deviceX/buffer/hwfifo_watermark_max 1346 + KernelVersion: 4.2 1347 + Contact: linux-iio@vger.kernel.org 1348 + Description: 1349 + A single positive integer specifying the maximum watermark level 1350 + for the hardware fifo of this device. If the device does not 1351 + have a hardware fifo this entry is not present. 1352 + If the user sets buffer/watermark to a value greater than this 1353 + one, then the hardware watermark will be capped at this value. 1354 + 1355 + What: /sys/bus/iio/devices/iio:deviceX/buffer/hwfifo_watermark_available 1356 + KernelVersion: 4.2 1357 + Contact: linux-iio@vger.kernel.org 1358 + Description: 1359 + A list of positive integers specifying the available watermark 1360 + levels for the hardware fifo. This entry is optional and if it 1361 + is not present it means that all the values between 1362 + hwfifo_watermark_min and hwfifo_watermark_max are supported. 1363 + If the user sets buffer/watermark to a value greater than 1364 + hwfifo_watermak_min but not equal to any of the values in this 1365 + list, the driver will chose an appropriate value for the 1366 + hardware fifo watermark level.
+46 -14
drivers/iio/industrialio-buffer.c
··· 42 42 return buf->access->data_available(buf); 43 43 } 44 44 45 - static bool iio_buffer_ready(struct iio_dev *indio_dev, struct iio_buffer *buf, 46 - size_t to_wait) 45 + static int iio_buffer_flush_hwfifo(struct iio_dev *indio_dev, 46 + struct iio_buffer *buf, size_t required) 47 47 { 48 + if (!indio_dev->info->hwfifo_flush_to_buffer) 49 + return -ENODEV; 50 + 51 + return indio_dev->info->hwfifo_flush_to_buffer(indio_dev, required); 52 + } 53 + 54 + static bool iio_buffer_ready(struct iio_dev *indio_dev, struct iio_buffer *buf, 55 + size_t to_wait, int to_flush) 56 + { 57 + size_t avail; 58 + int flushed = 0; 59 + 48 60 /* wakeup if the device was unregistered */ 49 61 if (!indio_dev->info) 50 62 return true; 51 63 52 64 /* drain the buffer if it was disabled */ 53 - if (!iio_buffer_is_active(buf)) 65 + if (!iio_buffer_is_active(buf)) { 54 66 to_wait = min_t(size_t, to_wait, 1); 67 + to_flush = 0; 68 + } 55 69 56 - if (iio_buffer_data_available(buf) >= to_wait) 70 + avail = iio_buffer_data_available(buf); 71 + 72 + if (avail >= to_wait) { 73 + /* force a flush for non-blocking reads */ 74 + if (!to_wait && !avail && to_flush) 75 + iio_buffer_flush_hwfifo(indio_dev, buf, to_flush); 76 + return true; 77 + } 78 + 79 + if (to_flush) 80 + flushed = iio_buffer_flush_hwfifo(indio_dev, buf, 81 + to_wait - avail); 82 + if (flushed <= 0) 83 + return false; 84 + 85 + if (avail + flushed >= to_wait) 57 86 return true; 58 87 59 88 return false; ··· 101 72 struct iio_buffer *rb = indio_dev->buffer; 102 73 size_t datum_size; 103 74 size_t to_wait = 0; 75 + size_t to_read; 104 76 int ret; 105 77 106 78 if (!indio_dev->info) ··· 119 89 if (!datum_size) 120 90 return 0; 121 91 92 + to_read = min_t(size_t, n / datum_size, rb->watermark); 93 + 122 94 if (!(filp->f_flags & O_NONBLOCK)) 123 - to_wait = min_t(size_t, n / datum_size, rb->watermark); 95 + to_wait = to_read; 124 96 125 97 do { 126 98 ret = wait_event_interruptible(rb->pollq, 127 - iio_buffer_ready(indio_dev, rb, to_wait)); 99 + iio_buffer_ready(indio_dev, rb, to_wait, to_read)); 128 100 if (ret) 129 101 return ret; 130 102 ··· 154 122 return -ENODEV; 155 123 156 124 poll_wait(filp, &rb->pollq, wait); 157 - if (iio_buffer_ready(indio_dev, rb, rb->watermark)) 125 + if (iio_buffer_ready(indio_dev, rb, rb->watermark, 0)) 158 126 return POLLIN | POLLRDNORM; 159 127 return 0; 160 128 } ··· 693 661 } 694 662 } 695 663 /* Definitely possible for devices to support both of these. */ 696 - if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) { 697 - if (!indio_dev->trig) { 698 - printk(KERN_INFO "Buffer not started: no trigger\n"); 699 - ret = -EINVAL; 700 - /* Can only occur on first buffer */ 701 - goto error_run_postdisable; 702 - } 664 + if ((indio_dev->modes & INDIO_BUFFER_TRIGGERED) && indio_dev->trig) { 703 665 indio_dev->currentmode = INDIO_BUFFER_TRIGGERED; 704 666 } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) { 705 667 indio_dev->currentmode = INDIO_BUFFER_HARDWARE; 706 668 } else if (indio_dev->modes & INDIO_BUFFER_SOFTWARE) { 707 669 indio_dev->currentmode = INDIO_BUFFER_SOFTWARE; 708 670 } else { /* Should never be reached */ 671 + /* Can only occur on first buffer */ 672 + if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) 673 + pr_info("Buffer not started: no trigger\n"); 709 674 ret = -EINVAL; 710 675 goto error_run_postdisable; 711 676 } ··· 854 825 } 855 826 856 827 buffer->watermark = val; 828 + 829 + if (indio_dev->info->hwfifo_set_watermark) 830 + indio_dev->info->hwfifo_set_watermark(indio_dev, val); 857 831 out: 858 832 mutex_unlock(&indio_dev->mlock); 859 833
+13
include/linux/iio/iio.h
··· 338 338 * provide a custom of_xlate function that reads the 339 339 * *args* and returns the appropriate index in registered 340 340 * IIO channels array. 341 + * @hwfifo_set_watermark: function pointer to set the current hardware 342 + * fifo watermark level; see hwfifo_* entries in 343 + * Documentation/ABI/testing/sysfs-bus-iio for details on 344 + * how the hardware fifo operates 345 + * @hwfifo_flush_to_buffer: function pointer to flush the samples stored 346 + * in the hardware fifo to the device buffer. The driver 347 + * should not flush more than count samples. The function 348 + * must return the number of samples flushed, 0 if no 349 + * samples were flushed or a negative integer if no samples 350 + * were flushed and there was an error. 341 351 **/ 342 352 struct iio_info { 343 353 struct module *driver_module; ··· 409 399 unsigned *readval); 410 400 int (*of_xlate)(struct iio_dev *indio_dev, 411 401 const struct of_phandle_args *iiospec); 402 + int (*hwfifo_set_watermark)(struct iio_dev *indio_dev, unsigned val); 403 + int (*hwfifo_flush_to_buffer)(struct iio_dev *indio_dev, 404 + unsigned count); 412 405 }; 413 406 414 407 /**