Add STM32 DFSDM support
Subsystems (7)
v8 cover Add STM32 DFSDM support
Hello
New version based on minor comments from Jonathan.
Main deltas V8 vs V7:
- Few typos fixes.
- Function return optimizations in sound/soc/stm/stm32_adfsdm.c.
Main deltas V7 vs V6:
- Replaces the custom license information text with the appropriate SPDX identifier.
- Few fixes in sound/soc/stm/stm32_adfsdm.c and stm32-dfsdm-core.c.
- Add missing #interrupt-cells in binding examples.
- Integrate last Jonathan's comments.
Main deltas V6 vs V5:
- Fix warning reported by kbuild test in :
include/linux/iio/consumer.h
sound/soc/stm/stm32_adfsdm.c
Main deltas V5 vs V4:
- Integrate ASOC DAI as a subnode of the DFSDM.
- Add in kernel consumer interface to allow to manipulate attribute.
Thanks,
Arnaud
Arnaud Pouliquen (12):
docs: driver-api: add iio hw consumer section
IIO: hw_consumer: add devm_iio_hw_consumer_alloc
IIO: inkern: API for manipulating channel attributes
IIO: Add DT bindings for sigma delta adc modulator
IIO: ADC: add sigma delta modulator support
IIO: add DT bindings for stm32 DFSDM filter
IIO: ADC: add stm32 DFSDM core support
IIO: ADC: add STM32 DFSDM sigma delta ADC support
IIO: ADC: add stm32 DFSDM support for PDM microphone
IIO: consumer: allow to set buffer sizes
ASoC: add bindings for stm32 DFSDM filter
ASoC: stm32: add DFSDM DAI support
Lars-Peter Clausen (1):
iio: Add hardware consumer buffer support
.../ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32 | 16 + .../bindings/iio/adc/sigma-delta-modulator.txt | 13 + .../bindings/iio/adc/st,stm32-dfsdm-adc.txt | 128 ++ .../devicetree/bindings/sound/st,stm32-adfsdm.txt | 63 + Documentation/driver-api/iio/hw-consumer.rst | 51 + Documentation/driver-api/iio/index.rst | 1 + drivers/iio/adc/Kconfig | 37 + drivers/iio/adc/Makefile | 3 + drivers/iio/adc/sd_adc_modulator.c | 68 ++ drivers/iio/adc/stm32-dfsdm-adc.c | 1220 ++++++++++++++++++++ drivers/iio/adc/stm32-dfsdm-core.c | 309 +++++ drivers/iio/adc/stm32-dfsdm.h | 310 +++++ drivers/iio/buffer/Kconfig | 10 + drivers/iio/buffer/Makefile | 1 + drivers/iio/buffer/industrialio-buffer-cb.c | 11 + drivers/iio/buffer/industrialio-hw-consumer.c | 247 ++++ drivers/iio/inkern.c | 17 +- include/linux/iio/adc/stm32-dfsdm-adc.h | 18 + include/linux/iio/consumer.h | 37 + include/linux/iio/hw-consumer.h | 21 + include/linux/iio/iio.h | 28 - include/linux/iio/types.h | 28 + sound/soc/stm/Kconfig | 11 + sound/soc/stm/Makefile | 3 + sound/soc/stm/stm32_adfsdm.c | 347 ++++++ 25 files changed, 2965 insertions(+), 33 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32 create mode 100644 Documentation/devicetree/bindings/iio/adc/sigma-delta-modulator.txt create mode 100644 Documentation/devicetree/bindings/iio/adc/st,stm32-dfsdm-adc.txt create mode 100644 Documentation/devicetree/bindings/sound/st,stm32-adfsdm.txt create mode 100644 Documentation/driver-api/iio/hw-consumer.rst create mode 100644 drivers/iio/adc/sd_adc_modulator.c create mode 100644 drivers/iio/adc/stm32-dfsdm-adc.c create mode 100644 drivers/iio/adc/stm32-dfsdm-core.c create mode 100644 drivers/iio/adc/stm32-dfsdm.h create mode 100644 drivers/iio/buffer/industrialio-hw-consumer.c create mode 100644 include/linux/iio/adc/stm32-dfsdm-adc.h create mode 100644 include/linux/iio/hw-consumer.h create mode 100644 sound/soc/stm/stm32_adfsdm.c
Signature · 1 line
2.7.4
PATCH v8 1/13 iio: Add hardware consumer buffer support
From: Lars-Peter Clausen <[email protected]>
Hardware consumer interface can be used when one IIO device has a direct connection to another device in hardware.
--- drivers/iio/buffer/Kconfig | 10 ++ drivers/iio/buffer/Makefile | 1 + drivers/iio/buffer/industrialio-hw-consumer.c | 181 ++++++++++++++++++++++++++ include/linux/iio/hw-consumer.h | 19 +++ 4 files changed, 211 insertions(+) create mode 100644 drivers/iio/buffer/industrialio-hw-consumer.c create mode 100644 include/linux/iio/hw-consumer.h
drivers/iio/buffer/Kconfig +10 -0
@@ -29,6 +29,16 @@ config IIO_BUFFER_DMAENGINE Should be selected by drivers that want to use this functionality. +config IIO_BUFFER_HW_CONSUMER + tristate "Industrial I/O HW buffering" + help + Provides a way to bonding when an IIO device has a direct connection + to another device in hardware. In this case buffers for data transfers + are handled by hardware. + + Should be selected by drivers that want to use the generic Hw consumer + interface. + config IIO_KFIFO_BUF tristate "Industrial I/O buffering based on kfifo" help
drivers/iio/buffer/Makefile +1 -0
@@ -6,5 +6,6 @@ obj-$(CONFIG_IIO_BUFFER_CB) += industrialio-buffer-cb.o obj-$(CONFIG_IIO_BUFFER_DMA) += industrialio-buffer-dma.o obj-$(CONFIG_IIO_BUFFER_DMAENGINE) += industrialio-buffer-dmaengine.o +obj-$(CONFIG_IIO_BUFFER_HW_CONSUMER) += industrialio-hw-consumer.o obj-$(CONFIG_IIO_TRIGGERED_BUFFER) += industrialio-triggered-buffer.o obj-$(CONFIG_IIO_KFIFO_BUF) += kfifo_buf.o
drivers/iio/buffer/industrialio-hw-consumer.c new file +181 -0
181 lines not on this page · raw message
include/linux/iio/hw-consumer.h new file +19 -0
@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Industrial I/O in kernel hardware consumer interface +* +* Copyright 2017 Analog Devices Inc. +* Author: Lars-Peter Clausen <[email protected]> +*/ + +#ifndef LINUX_IIO_HW_CONSUMER_H +#define LINUX_IIO_HW_CONSUMER_H + +struct iio_hw_consumer ; + +struct iio_hw_consumer *iio_hw_consumer_alloc (struct device *dev); +void iio_hw_consumer_free (struct iio_hw_consumer *hwc); +int iio_hw_consumer_enable (struct iio_hw_consumer *hwc); +void iio_hw_consumer_disable (struct iio_hw_consumer *hwc); + +#endif
Signature · 1 line
2.7.4
PATCH v8 2/13 docs: driver-api: add iio hw consumer section
In reply to Arnaud PouliquenThis adds a section about the Hardware consumer
API of the IIO subsystem to the driver API
documentation.
--- Documentation/driver-api/iio/hw-consumer.rst | 51 ++++++++++++++++++++++++++++ Documentation/driver-api/iio/index.rst | 1 + 2 files changed, 52 insertions(+) create mode 100644 Documentation/driver-api/iio/hw-consumer.rst
Documentation/driver-api/iio/hw-consumer.rst new file +51 -0
@@ -0,0 +1,51 @@ +=========== +HW consumer +=========== +An IIO device can be directly connected to another device in hardware. in this +case the buffers between IIO provider and IIO consumer are handled by hardware. +The Industrial I/O HW consumer offers a way to bond these IIO devices without +software buffer for data. The implementation can be found under +:file:`drivers/iio/buffer/hw-consumer.c` + + +* struct :c:type:`iio_hw_consumer` ? Hardware consumer structure +* :c:func:`iio_hw_consumer_alloc` ? Allocate IIO hardware consumer +* :c:func:`iio_hw_consumer_free` ? Free IIO hardware consumer +* :c:func:`iio_hw_consumer_enable` ? Enable IIO hardware consumer +* :c:func:`iio_hw_consumer_disable` ? Disable IIO hardware consumer + + +HW consumer setup +================= + +As standard IIO device the implementation is based on IIO provider/consumer. +A typical IIO HW consumer setup looks like this:: + + static struct iio_hw_consumer *hwc; + + static const struct iio_info adc_info = { + .read_raw = adc_read_raw, + }; + + static int adc_read_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, int *val, + int *val2, long mask) + { + ret = iio_hw_consumer_enable(hwc); + + /* Acquire data */ + + ret = iio_hw_consumer_disable(hwc); + } + + static int adc_probe(struct platform_device *pdev) + { + hwc = devm_iio_hw_consumer_alloc(&iio->dev); + } + +More details +============ +.. kernel-doc:: include/linux/iio/hw-consumer.h +.. kernel-doc:: drivers/iio/buffer/industrialio-hw-consumer.c + :export: +
Documentation/driver-api/iio/index.rst +1 -0
@@ -15,3 +15,4 @@ Contents: buffers triggers triggered-buffers + hw-consumer
Signature · 1 line
2.7.4
A message that is not in the archive (sent off-list or not yet received): 1512987524-2901-1-git-send-email-arnaud.pouliquen-qxv4g6HH51o@public.gmane.org
PATCH v8 3/13 IIO: hw_consumer: add devm_iio_hw_consumer_alloc
Add devm_iio_hw_consumer_alloc function that calls iio_hw_consumer_free when the device is unbound from the bus.
--- drivers/iio/buffer/industrialio-hw-consumer.c | 66 +++++++++++++++++++++++++++ include/linux/iio/hw-consumer.h | 2 + 2 files changed, 68 insertions(+)
drivers/iio/buffer/industrialio-hw-consumer.c +66 -0
@@ -137,6 +137,72 @@ void iio_hw_consumer_free(struct iio_hw_consumer *hwc) }EXPORT_SYMBOL_GPL (iio_hw_consumer_free); +static void devm_iio_hw_consumer_release (struct device *dev,void *res) +{ +iio_hw_consumer_free (*(struct iio_hw_consumer **)res); +} + +static int devm_iio_hw_consumer_match (struct device *dev,void *res,void *data) +{ +struct iio_hw_consumer **r = res; + +if (!r || !*r) { +WARN_ON (!r || !*r); +return 0 ; + } +return *r == data; +} + +/** +* devm_iio_hw_consumer_alloc - Resource-managed iio_hw_consumer_alloc() +* @dev: Pointer to consumer device. +* +* Managed iio_hw_consumer_alloc. iio_hw_consumer allocated with this function +* is automatically freed on driver detach. +* +* If an iio_hw_consumer allocated with this function needs to be freed +* separately, devm_iio_hw_consumer_free() must be used. +* +* returns pointer to allocated iio_hw_consumer on success, NULL on failure. +*/ +struct iio_hw_consumer *devm_iio_hw_consumer_alloc (struct device *dev) +{ +struct iio_hw_consumer **ptr, *iio_hwc; + + ptr =devres_alloc (devm_iio_hw_consumer_release,sizeof (*ptr), +GFP_KERNEL ); +if (!ptr) +return NULL ; + + iio_hwc =iio_hw_consumer_alloc (dev); +if (IS_ERR (iio_hwc)) { +devres_free (ptr); + }else { + *ptr = iio_hwc; +devres_add (dev, ptr); + } + +return iio_hwc; +} +EXPORT_SYMBOL_GPL (devm_iio_hw_consumer_alloc); + +/** +* devm_iio_hw_consumer_free - Resource-managed iio_hw_consumer_free() +* @dev: Pointer to consumer device. +* @hwc: iio_hw_consumer to free. +* +* Free iio_hw_consumer allocated with devm_iio_hw_consumer_alloc(). +*/ +void devm_iio_hw_consumer_free (struct device *dev,struct iio_hw_consumer *hwc) +{ +int rc; + + rc =devres_release (dev, devm_iio_hw_consumer_release, + devm_iio_hw_consumer_match, hwc); +WARN_ON (rc); +} +EXPORT_SYMBOL_GPL (devm_iio_hw_consumer_free); +/** * iio_hw_consumer_enable() - Enable IIO hardware consumer * @hwc: iio_hw_consumer to enable.
include/linux/iio/hw-consumer.h +2 -0
@@ -13,6 +13,8 @@ struct iio_hw_consumer;struct iio_hw_consumer *iio_hw_consumer_alloc (struct device *dev);void iio_hw_consumer_free (struct iio_hw_consumer *hwc); +struct iio_hw_consumer *devm_iio_hw_consumer_alloc (struct device *dev); +void devm_iio_hw_consumer_free (struct device *dev,struct iio_hw_consumer *hwc);int iio_hw_consumer_enable (struct iio_hw_consumer *hwc);void iio_hw_consumer_disable (struct iio_hw_consumer *hwc);
Signature · 1 line
2.7.4
PATCH v8 4/13 IIO: inkern: API for manipulating channel attributes
In reply to a message not in the archiveExtend the inkern API with functions for reading and writing attribute of iio channels.
---
V7 to V8
- Suppress extra line.
drivers/iio/inkern.c | 17 ++++++++++++----- include/linux/iio/consumer.h | 26 ++++++++++++++++++++++++++ include/linux/iio/iio.h | 28 ---------------------------- include/linux/iio/types.h | 28 ++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 33 deletions(-)
drivers/iio/inkern.c +12 -5
@@ -664,9 +664,8 @@ int iio_convert_raw_to_processed(struct iio_channel *chan, int raw, }EXPORT_SYMBOL_GPL (iio_convert_raw_to_processed); -static int iio_read_channel_attribute (struct iio_channel *chan, -int *val,int *val2, -enum iio_chan_info_enum attribute) +int iio_read_channel_attribute (struct iio_channel *chan,int *val,int *val2, +enum iio_chan_info_enum attribute) {int ret;
@@ -682,6 +681,7 @@ static int iio_read_channel_attribute(struct iio_channel *chan,return ret; } +EXPORT_SYMBOL_GPL (iio_read_channel_attribute);int iio_read_channel_offset (struct iio_channel *chan,int *val,int *val2) {
@@ -850,7 +850,8 @@ static int iio_channel_write(struct iio_channel *chan, int val, int val2, chan->channel, val, val2, info); } -int iio_write_channel_raw (struct iio_channel *chan,int val) +int iio_write_channel_attribute (struct iio_channel *chan,int val,int val2, +enum iio_chan_info_enum attribute) {int ret;
@@ -860,12 +861,18 @@ int iio_write_channel_raw(struct iio_channel *chan, int val) goto err_unlock; } - ret =iio_channel_write (chan, val,0 ,IIO_CHAN_INFO_RAW ); + ret =iio_channel_write (chan, val, val2, attribute); err_unlock:mutex_unlock (&chan->indio_dev->info_exist_lock);return ret; } +EXPORT_SYMBOL_GPL (iio_write_channel_attribute); + +int iio_write_channel_raw (struct iio_channel *chan,int val) +{ +return iio_write_channel_attribute (chan, val,0 ,IIO_CHAN_INFO_RAW ); +}EXPORT_SYMBOL_GPL (iio_write_channel_raw);unsigned int iio_get_channel_ext_info_count (struct iio_channel *chan)
include/linux/iio/consumer.h +26 -0
@@ -216,6 +216,32 @@ int iio_read_channel_average_raw(struct iio_channel *chan, int *val);int iio_read_channel_processed (struct iio_channel *chan,int *val);/** +* iio_write_channel_attribute() - Write values to the device attribute. +* @chan: The channel being queried. +* @val: Value being written. +* @val2: Value being written.val2 use depends on attribute type. +* @attribute: info attribute to be read. +* +* Returns an error code or 0. +*/ +int iio_write_channel_attribute (struct iio_channel *chan,int val, +int val2,enum iio_chan_info_enum attribute); + +/** +* iio_read_channel_attribute() - Read values from the device attribute. +* @chan: The channel being queried. +* @val: Value being written. +* @val2: Value being written.Val2 use depends on attribute type. +* @attribute: info attribute to be written. +* +* Returns an error code if failed. Else returns a description of what is in val +* and val2, such as IIO_VAL_INT_PLUS_MICRO telling us we have a value of val +* + val2/1e6 +*/ +int iio_read_channel_attribute (struct iio_channel *chan,int *val, +int *val2,enum iio_chan_info_enum attribute); + +/** * iio_write_channel_raw() - write to a given channel * @chan: The channel being queried. * @val: Value being written.
include/linux/iio/iio.h +0 -28
@@ -20,34 +20,6 @@* Currently assumes nano seconds. */ -enum iio_chan_info_enum { -IIO_CHAN_INFO_RAW =0 , -IIO_CHAN_INFO_PROCESSED , -IIO_CHAN_INFO_SCALE , -IIO_CHAN_INFO_OFFSET , -IIO_CHAN_INFO_CALIBSCALE , -IIO_CHAN_INFO_CALIBBIAS , -IIO_CHAN_INFO_PEAK , -IIO_CHAN_INFO_PEAK_SCALE , -IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW , -IIO_CHAN_INFO_AVERAGE_RAW , -IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY , -IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY , -IIO_CHAN_INFO_SAMP_FREQ , -IIO_CHAN_INFO_FREQUENCY , -IIO_CHAN_INFO_PHASE , -IIO_CHAN_INFO_HARDWAREGAIN , -IIO_CHAN_INFO_HYSTERESIS , -IIO_CHAN_INFO_INT_TIME , -IIO_CHAN_INFO_ENABLE , -IIO_CHAN_INFO_CALIBHEIGHT , -IIO_CHAN_INFO_CALIBWEIGHT , -IIO_CHAN_INFO_DEBOUNCE_COUNT , -IIO_CHAN_INFO_DEBOUNCE_TIME , -IIO_CHAN_INFO_CALIBEMISSIVITY , -IIO_CHAN_INFO_OVERSAMPLING_RATIO , -}; -enum iio_shared_by {IIO_SEPARATE ,IIO_SHARED_BY_TYPE ,
include/linux/iio/types.h +28 -0
@@ -34,4 +34,32 @@ enum iio_available_type { IIO_AVAIL_RANGE, }; +enum iio_chan_info_enum { +IIO_CHAN_INFO_RAW =0 , +IIO_CHAN_INFO_PROCESSED , +IIO_CHAN_INFO_SCALE , +IIO_CHAN_INFO_OFFSET , +IIO_CHAN_INFO_CALIBSCALE , +IIO_CHAN_INFO_CALIBBIAS , +IIO_CHAN_INFO_PEAK , +IIO_CHAN_INFO_PEAK_SCALE , +IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW , +IIO_CHAN_INFO_AVERAGE_RAW , +IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY , +IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY , +IIO_CHAN_INFO_SAMP_FREQ , +IIO_CHAN_INFO_FREQUENCY , +IIO_CHAN_INFO_PHASE , +IIO_CHAN_INFO_HARDWAREGAIN , +IIO_CHAN_INFO_HYSTERESIS , +IIO_CHAN_INFO_INT_TIME , +IIO_CHAN_INFO_ENABLE , +IIO_CHAN_INFO_CALIBHEIGHT , +IIO_CHAN_INFO_CALIBWEIGHT , +IIO_CHAN_INFO_DEBOUNCE_COUNT , +IIO_CHAN_INFO_DEBOUNCE_TIME , +IIO_CHAN_INFO_CALIBEMISSIVITY , +IIO_CHAN_INFO_OVERSAMPLING_RATIO , +}; +#endif /* _IIO_TYPES_H_ */
Signature · 6 lines
2.7.4 -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v8 7/13 IIO: add DT bindings for stm32 DFSDM filter
In reply to a message not in the archiveAdd bindings that describes STM32 Digital Filter for Sigma Delta Modulators. DFSDM allows to connect sigma delta modulators.
--- .../bindings/iio/adc/st,stm32-dfsdm-adc.txt | 128 +++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/st,stm32-dfsdm-adc.txt
Documentation/devicetree/bindings/iio/adc/st,stm32-dfsdm-adc.txt new file +128 -0
@@ -0,0 +1,128 @@ +STMicroelectronics STM32 DFSDM ADC device driver + + +STM32 DFSDM ADC is a sigma delta analog-to-digital converter dedicated to +interface external sigma delta modulators to STM32 micro controllers. +It is mainly targeted for: +- Sigma delta modulators (motor control, metering...) +- PDM microphones (audio digital microphone) + +It features up to 8 serial digital interfaces (SPI or Manchester) and +up to 4 filters on stm32h7. + +Each child node match with a filter instance. + +Contents of a STM32 DFSDM root node: +------------------------------------ +Required properties: +- compatible: Should be "st,stm32h7-dfsdm". +- reg: Offset and length of the DFSDM block register set. +- clocks: IP and serial interfaces clocking. Should be set according + to rcc clock ID and "clock-names". +- clock-names: Input clock name "dfsdm" must be defined, + "audio" is optional. If defined CLKOUT is based on the audio + clock, else "dfsdm" is used. +- #interrupt-cells = <1>; +- #address-cells = <1>; +- #size-cells = <0>; + +Optional properties: +- spi-max-frequency: Requested only for SPI master mode. + SPI clock OUT frequency (Hz). This clock must be set according + to "clock" property. Frequency must be a multiple of the rcc + clock frequency. If not, SPI CLKOUT frequency will not be + accurate. + +Contents of a STM32 DFSDM child nodes: +-------------------------------------- + +Required properties: +- compatible: Must be: + "st,stm32-dfsdm-adc" for sigma delta ADCs + "st,stm32-dfsdm-dmic" for audio digital microphone. +- reg: Specifies the DFSDM filter instance used. +- interrupts: IRQ lines connected to each DFSDM filter instance. +- st,adc-channels: List of single-ended channels muxed for this ADC. + valid values: + "st,stm32h7-dfsdm" compatibility: 0 to 7. +- st,adc-channel-names: List of single-ended channel names. +- st,filter-order: SinC filter order from 0 to 5. + 0: FastSinC + [1-5]: order 1 to 5. + For audio purpose it is recommended to use order 3 to 5. +- #io-channel-cells = <1>: See the IIO bindings section "IIO consumers". + +Required properties for "st,stm32-dfsdm-adc" compatibility: +- io-channels: From common IIO binding. Used to pipe external sigma delta + modulator or internal ADC output to DFSDM channel. + This is not required for "st,stm32-dfsdm-pdm" compatibility as + PDM microphone is binded in Audio DT node. + +Required properties for "st,stm32-dfsdm-pdm" compatibility: +- #sound-dai-cells: Must be set to 0. +- dma: DMA controller phandle and DMA request line associated to the + filter instance (specified by the field "reg") +- dma-names: Must be "rx" + +Optional properties: +- st,adc-channel-types: Single-ended channel input type. + - "SPI_R": SPI with data on rising edge (default) + - "SPI_F": SPI with data on falling edge + - "MANCH_R": manchester codec, rising edge = logic 0 + - "MANCH_F": manchester codec, falling edge = logic 1 +- st,adc-channel-clk-src: Conversion clock source. + - "CLKIN": external SPI clock (CLKIN x) + - "CLKOUT": internal SPI clock (CLKOUT) (default) + - "CLKOUT_F": internal SPI clock divided by 2 (falling edge). + - "CLKOUT_R": internal SPI clock divided by 2 (rising edge). + +- st,adc-alt-channel: Must be defined if two sigma delta modulator are + connected on same SPI input. + If not set, channel n is connected to SPI input n. + If set, channel n is connected to SPI input n + 1. + +- st,filter0-sync: Set to 1 to synchronize with DFSDM filter instance 0. + Used for multi microphones synchronization. + +Example of a sigma delta adc connected on DFSDM SPI port 0 +and a pdm microphone connected on DFSDM SPI port 1: + + ads1202: simple_sd_adc@0 { + compatible = "ads1202"; + #io-channel-cells = <1>; + }; + + dfsdm: dfsdm@40017000 { + compatible = "st,stm32h7-dfsdm"; + reg = <0x40017000 0x400>; + clocks = <&rcc DFSDM1_CK>; + clock-names = "dfsdm"; + #interrupt-cells = <1>; + #address-cells = <1>; + #size-cells = <0>; + + dfsdm_adc0: filter@0 { + compatible = "st,stm32-dfsdm-adc"; + #io-channel-cells = <1>; + reg = <0>; + interrupts = <110>; + st,adc-channels = <0>; + st,adc-channel-names = "sd_adc0"; + st,adc-channel-types = "SPI_F"; + st,adc-channel-clk-src = "CLKOUT"; + io-channels = <&ads1202 0>; + st,filter-order = <3>; + }; + dfsdm_pdm1: filter@1 { + compatible = "st,stm32-dfsdm-dmic"; + reg = <1>; + interrupts = <111>; + dmas = <&dmamux1 102 0x400 0x00>; + dma-names = "rx"; + st,adc-channels = <1>; + st,adc-channel-names = "dmic1"; + st,adc-channel-types = "SPI_R"; + st,adc-channel-clk-src = "CLKOUT"; + st,filter-order = <5>; + }; + }
Signature · 6 lines
2.7.4 -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v8 8/13 IIO: ADC: add stm32 DFSDM core support
In reply to a message not in the archiveAdd driver for stm32 DFSDM pheripheral. Its converts a sigma delta stream in n bit samples through a low pass filter and an integrator. stm32-dfsdm-core driver is the core part supporting the filter instances dedicated to sigma-delta ADC or audio PDM microphone purpose.
--- drivers/iio/adc/Kconfig | 12 ++ drivers/iio/adc/Makefile | 1 + drivers/iio/adc/stm32-dfsdm-core.c | 309 ++++++++++++++++++++++++++++++++++++ drivers/iio/adc/stm32-dfsdm.h | 310 +++++++++++++++++++++++++++++++++++++ 4 files changed, 632 insertions(+) create mode 100644 drivers/iio/adc/stm32-dfsdm-core.c create mode 100644 drivers/iio/adc/stm32-dfsdm.h
drivers/iio/adc/Kconfig +12 -0
@@ -665,6 +665,18 @@ config STM32_ADC This driver can also be built as a module. If so, the module will be called stm32-adc. +config STM32_DFSDM_CORE + tristate "STMicroelectronics STM32 DFSDM core" + depends on (ARCH_STM32 && OF) || COMPILE_TEST + select REGMAP + select REGMAP_MMIO + help + Select this option to enable the driver for STMicroelectronics + STM32 digital filter for sigma delta converter. + + This driver can also be built as a module. If so, the module + will be called stm32-dfsdm-core. + config STX104 tristate "Apex Embedded Systems STX104 driver" depends on PC104 && X86 && ISA_BUS_API
drivers/iio/adc/Makefile +1 -0
@@ -63,6 +63,7 @@ obj-$(CONFIG_STX104) += stx104.o obj-$(CONFIG_SUN4I_GPADC) += sun4i-gpadc-iio.o obj-$(CONFIG_STM32_ADC_CORE) += stm32-adc-core.o obj-$(CONFIG_STM32_ADC) += stm32-adc.o +obj-$(CONFIG_STM32_DFSDM_CORE) += stm32-dfsdm-core.o obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o obj-$(CONFIG_TI_ADC0832) += ti-adc0832.o obj-$(CONFIG_TI_ADC084S021) += ti-adc084s021.o
drivers/iio/adc/stm32-dfsdm-core.c new file +309 -0
309 lines not on this page · raw message
drivers/iio/adc/stm32-dfsdm.h new file +310 -0
310 lines not on this page · raw message
Signature · 6 lines
2.7.4 -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v8 9/13 IIO: ADC: add STM32 DFSDM sigma delta ADC support
In reply to a message not in the archiveAdd DFSDM driver to handle sigma delta ADC.
--- drivers/iio/adc/Kconfig | 13 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/stm32-dfsdm-adc.c | 728 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 742 insertions(+) create mode 100644 drivers/iio/adc/stm32-dfsdm-adc.c
drivers/iio/adc/Kconfig +13 -0
@@ -677,6 +677,19 @@ config STM32_DFSDM_CORE This driver can also be built as a module. If so, the module will be called stm32-dfsdm-core. +config STM32_DFSDM_ADC + tristate "STMicroelectronics STM32 dfsdm adc" + depends on (ARCH_STM32 && OF) || COMPILE_TEST + select STM32_DFSDM_CORE + select REGMAP_MMIO + select IIO_BUFFER_HW_CONSUMER + help + Select this option to support ADCSigma delta modulator for + STMicroelectronics STM32 digital filter for sigma delta converter. + + This driver can also be built as a module. If so, the module + will be called stm32-dfsdm-adc. + config STX104 tristate "Apex Embedded Systems STX104 driver" depends on PC104 && X86 && ISA_BUS_API
drivers/iio/adc/Makefile +1 -0
@@ -64,6 +64,7 @@ obj-$(CONFIG_SUN4I_GPADC) += sun4i-gpadc-iio.o obj-$(CONFIG_STM32_ADC_CORE) += stm32-adc-core.o obj-$(CONFIG_STM32_ADC) += stm32-adc.o obj-$(CONFIG_STM32_DFSDM_CORE) += stm32-dfsdm-core.o +obj-$(CONFIG_STM32_DFSDM_ADC) += stm32-dfsdm-adc.o obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o obj-$(CONFIG_TI_ADC0832) += ti-adc0832.o obj-$(CONFIG_TI_ADC084S021) += ti-adc084s021.o
drivers/iio/adc/stm32-dfsdm-adc.c new file +728 -0
728 lines not on this page · raw message
Signature · 6 lines
2.7.4 -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v8 10/13 IIO: ADC: add stm32 DFSDM support for PDM microphone
In reply to a message not in the archiveThis code offers a way to handle PDM audio microphones in ASOC framework. Audio driver should use consumer API. A specific management is implemented for DMA, with a callback, to allows to handle audio buffers efficiently.
--- .../ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32 | 16 + drivers/iio/adc/stm32-dfsdm-adc.c | 508 ++++++++++++++++++++- include/linux/iio/adc/stm32-dfsdm-adc.h | 18 + 3 files changed, 534 insertions(+), 8 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32 create mode 100644 include/linux/iio/adc/stm32-dfsdm-adc.h
Documentation/ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32 new file +16 -0
@@ -0,0 +1,16 @@ +What: /sys/bus/iio/devices/iio:deviceX/in_voltage_spi_clk_freq +KernelVersion: 4.14 +Contact: [email protected] +Description: + For audio purpose only. + Used by audio driver to set/get the spi input frequency. + This is mandatory if DFSDM is slave on SPI bus, to + provide information on the SPI clock frequency during runtime + Notice that the SPI frequency should be a multiple of sample + frequency to ensure the precision. + if DFSDM input is SPI master + Reading SPI clkout frequency, + error on writing + If DFSDM input is SPI Slave: + Reading returns value previously set. + Writing value before starting conversions.
drivers/iio/adc/stm32-dfsdm-adc.c +500 -8
625 lines not on this page · raw message
include/linux/iio/adc/stm32-dfsdm-adc.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* This file discribe the STM32 DFSDM IIO driver API for audio part +* +* Copyright (C) 2017, STMicroelectronics - All Rights Reserved +* Author(s): Arnaud Pouliquen <[email protected]>. +*/ + +#ifndef STM32_DFSDM_ADC_H +#define STM32_DFSDM_ADC_H + +int stm32_dfsdm_get_buff_cb (struct iio_dev *iio_dev, +int (*cb)(const void *data,size_t size, +void *private), +void *private); +int stm32_dfsdm_release_buff_cb (struct iio_dev *iio_dev); + +#endif
Signature · 1 line
2.7.4
A message that is not in the archive (sent off-list or not yet received): 1512987524-2901-11-git-send-email-arnaud.pouliquen-qxv4g6HH51o@public.gmane.org
Re: [PATCH v8 10/13] IIO: ADC: add stm32 DFSDM support for PDM microphone
Quoted 6 lines · On Mon, 11 Dec 2017 11:18:41 +0100 Arnaud Pouliquen…
This code offers a way to handle PDM audio microphones in ASOC framework. Audio driver should use consumer API. A specific management is implemented for DMA, with a callback, to allows to handle audio buffers efficiently.
Signed-off-by Arnaud Pouliquen <[email protected]>
Hi Arnaud,
I raise a few queries on v7 of this patch.
https://marc.info/?l=linux-iio&m=151292965915376&w=2
Jonathan
Quoted 698 lines
--- .../ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32 | 16 + drivers/iio/adc/stm32-dfsdm-adc.c | 508 ++++++++++++++++++++- include/linux/iio/adc/stm32-dfsdm-adc.h | 18 + 3 files changed, 534 insertions(+), 8 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32 create mode 100644 include/linux/iio/adc/stm32-dfsdm-adc.hDocumentation/ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32 new file +16
-0@@ -0,0 +1,16 @@ +What: /sys/bus/iio/devices/iio:deviceX/in_voltage_spi_clk_freq +KernelVersion: 4.14 +Contact: [email protected] +Description: + For audio purpose only. + Used by audio driver to set/get the spi input frequency. + This is mandatory if DFSDM is slave on SPI bus, to + provide information on the SPI clock frequency during runtime + Notice that the SPI frequency should be a multiple of sample + frequency to ensure the precision. + if DFSDM input is SPI master + Reading SPI clkout frequency, + error on writing + If DFSDM input is SPI Slave: + Reading returns value previously set. + Writing value before starting conversions.drivers/iio/adc/stm32-dfsdm-adc.c +500
-8@@ -6,19 +6,25 @@* Author: Arnaud Pouliquen <[email protected]>. */ +#include <linux/dmaengine.h> +#include <linux/dma-mapping.h> #include <linux/interrupt.h> #include <linux/iio/buffer.h> #include <linux/iio/hw-consumer.h> #include <linux/iio/iio.h> #include <linux/iio/sysfs.h> +#include <linux/iio/trigger_consumer.h> +#include <linux/iio/triggered_buffer.h> #include <linux/module.h> -#include <linux/of.h> +#include <linux/of_device.h> #include <linux/platform_device.h> #include <linux/regmap.h> #include <linux/slab.h> #include "stm32-dfsdm.h" +#define DFSDM_DMA_BUFFER_SIZE (4 * PAGE_SIZE) +/* Conversion timeout */ #define DFSDM_TIMEOUT_US 100000#define DFSDM_TIMEOUT (msecs_to_jiffies(DFSDM_TIMEOUT_US / 1000))@@ -58,6 +64,18 @@ struct stm32_dfsdm_adc {struct completion completion;u32 *buffer; +/* Audio specific */ +unsigned int spi_freq;/* SPI bus clock frequency */ +unsigned int sample_freq;/* Sample frequency after filter decimation */ +int (*cb)(const void *data,size_t size,void *cb_priv); +void *cb_priv; + +/* DMA */ +u8 *rx_buf; +unsigned int bufi;/* Buffer current position */ +unsigned int buf_sz;/* Buffer size */ +struct dma_chan *dma_chan; +dma_addr_t dma_buf; };struct stm32_dfsdm_str2field {@@ -351,10 +369,63 @@ int stm32_dfsdm_channel_parse_of(struct stm32_dfsdm *dfsdm,return 0 ; } +static ssize_t dfsdm_adc_audio_get_spiclk (struct iio_dev *indio_dev, +uintptr_t priv, +const struct iio_chan_spec *chan, +char *buf) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); + +return snprintf (buf,PAGE_SIZE ,"%d\n" , adc->spi_freq); +} + +static ssize_t dfsdm_adc_audio_set_spiclk (struct iio_dev *indio_dev, +uintptr_t priv, +const struct iio_chan_spec *chan, +const char *buf,size_t len) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id]; +struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[adc->ch_id]; +unsigned int sample_freq = adc->sample_freq; +unsigned int spi_freq; +int ret; + +dev_err (&indio_dev->dev,"enter %s\n" , __func__); +/* If DFSDM is master on SPI, SPI freq can not be updated */ +if (ch->src !=DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL ) +return -EPERM ; + + ret =kstrtoint (buf,0 , &spi_freq); +if (ret) +return ret; + +if (!spi_freq) +return -EINVAL ; + +if (sample_freq) { +if (spi_freq % sample_freq) +dev_warn (&indio_dev->dev, +"Sampling rate not accurate (%d)\n" , + spi_freq / (spi_freq / sample_freq)); + + ret =stm32_dfsdm_set_osrs (fl,0 , (spi_freq / sample_freq)); +if (ret <0 ) { +dev_err (&indio_dev->dev, +"No filter parameters that match!\n" ); +return ret; + } + } + adc->spi_freq = spi_freq; + +return len; +} +static int stm32_dfsdm_start_conv (struct stm32_dfsdm_adc *adc,bool dma) {struct regmap *regmap = adc->dfsdm->regmap;int ret; +unsigned int dma_en =0 , cont_en =0 ; ret =stm32_dfsdm_start_channel (adc->dfsdm, adc->ch_id);if (ret <0 )@@ -365,6 +436,24 @@ static int stm32_dfsdm_start_conv(struct stm32_dfsdm_adc *adc, bool dma)if (ret <0 ) goto stop_channels; +if (dma) { +/* Enable DMA transfer*/ + dma_en =DFSDM_CR1_RDMAEN (1 ); +/* Enable conversion triggered by SPI clock*/ + cont_en =DFSDM_CR1_RCONT (1 ); + } +/* Enable DMA transfer*/ + ret =regmap_update_bits (regmap,DFSDM_CR1 (adc->fl_id), +DFSDM_CR1_RDMAEN_MASK , dma_en); +if (ret <0 ) + goto stop_channels; + +/* Enable conversion triggered by SPI clock*/ + ret =regmap_update_bits (regmap,DFSDM_CR1 (adc->fl_id), +DFSDM_CR1_RCONT_MASK , cont_en); +if (ret <0 ) + goto stop_channels; + ret =stm32_dfsdm_start_filter (adc->dfsdm, adc->fl_id);if (ret <0 ) goto stop_channels;@@ -398,6 +487,231 @@ static void stm32_dfsdm_stop_conv(struct stm32_dfsdm_adc *adc)stm32_dfsdm_stop_channel (adc->dfsdm, adc->ch_id); } +static int stm32_dfsdm_set_watermark (struct iio_dev *indio_dev, +unsigned int val) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +unsigned int watermark =DFSDM_DMA_BUFFER_SIZE /2 ; + +/* +* DMA cyclic transfers are used, buffer is split into two periods. +* There should be : +* - always one buffer (period) DMA is working on +* - one buffer (period) driver pushed to ASoC side. +*/ + watermark =min (watermark, val * (unsigned int )(sizeof (u32))); + adc->buf_sz = watermark *2 ; + +return 0 ; +} + +static unsigned int stm32_dfsdm_adc_dma_residue (struct stm32_dfsdm_adc *adc) +{ +struct dma_tx_state state; +enum dma_status status; + + status =dmaengine_tx_status (adc->dma_chan, + adc->dma_chan->cookie, + &state); +if (status ==DMA_IN_PROGRESS ) { +/* Residue is size in bytes from end of buffer */ +unsigned int i = adc->buf_sz - state.residue; +unsigned int size; + +/* Return available bytes */ +if (i >= adc->bufi) + size = i - adc->bufi; +else + size = adc->buf_sz + i - adc->bufi; + +return size; + } + +return 0 ; +} + +static void stm32_dfsdm_audio_dma_buffer_done (void *data) +{ +struct iio_dev *indio_dev = data; +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +int available =stm32_dfsdm_adc_dma_residue (adc); +size_t old_pos; + +/* +* FIXME: In Kernel interface does not support cyclic DMA buffer,and +* offers only an interface to push data samples per samples. +* For this reason IIO buffer interface is not used and interface is +* bypassed using a private callback registered by ASoC. +* This should be a temporary solution waiting a cyclic DMA engine +* support in IIO. +*/ + +dev_dbg (&indio_dev->dev,"%s: pos = %d, available = %d\n" , __func__, + adc->bufi, available); + old_pos = adc->bufi; + +while (available >= indio_dev->scan_bytes) { +u32 *buffer = (u32 *)&adc->rx_buf[adc->bufi]; + +/* Mask 8 LSB that contains the channel ID */ + *buffer = (*buffer &0xFFFFFF00 ) <<8 ; + available -= indio_dev->scan_bytes; + adc->bufi += indio_dev->scan_bytes; +if (adc->bufi >= adc->buf_sz) { +if (adc->cb) + adc->cb (&adc->rx_buf[old_pos], + adc->buf_sz - old_pos, adc->cb_priv); + adc->bufi =0 ; + old_pos =0 ; + } + } +if (adc->cb) + adc->cb (&adc->rx_buf[old_pos], adc->bufi - old_pos, + adc->cb_priv); +} + +static int stm32_dfsdm_adc_dma_start (struct iio_dev *indio_dev) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +struct dma_async_tx_descriptor *desc; +dma_cookie_t cookie; +int ret; + +if (!adc->dma_chan) +return -EINVAL ; + +dev_dbg (&indio_dev->dev,"%s size=%d watermark=%d\n" , __func__, + adc->buf_sz, adc->buf_sz /2 ); + +/* Prepare a DMA cyclic transaction */ + desc =dmaengine_prep_dma_cyclic (adc->dma_chan, + adc->dma_buf, + adc->buf_sz, adc->buf_sz /2 , +DMA_DEV_TO_MEM , +DMA_PREP_INTERRUPT ); +if (!desc) +return -EBUSY ; + + desc->callback = stm32_dfsdm_audio_dma_buffer_done; + desc->callback_param = indio_dev; + + cookie =dmaengine_submit (desc); + ret =dma_submit_error (cookie); +if (ret) { +dmaengine_terminate_all (adc->dma_chan); +return ret; + } + +/* Issue pending DMA requests */ +dma_async_issue_pending (adc->dma_chan); + +return 0 ; +} + +static int stm32_dfsdm_postenable (struct iio_dev *indio_dev) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +int ret; + +/* Reset adc buffer index */ + adc->bufi =0 ; + + ret =stm32_dfsdm_start_dfsdm (adc->dfsdm); +if (ret <0 ) +return ret; + + ret =stm32_dfsdm_start_conv (adc, true); +if (ret) { +dev_err (&indio_dev->dev,"Can't start conversion\n" ); + goto stop_dfsdm; + } + +if (adc->dma_chan) { + ret =stm32_dfsdm_adc_dma_start (indio_dev); +if (ret) { +dev_err (&indio_dev->dev,"Can't start DMA\n" ); + goto err_stop_conv; + } + } + +return 0 ; + +err_stop_conv: +stm32_dfsdm_stop_conv (adc); +stop_dfsdm: +stm32_dfsdm_stop_dfsdm (adc->dfsdm); + +return ret; +} + +static int stm32_dfsdm_predisable (struct iio_dev *indio_dev) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); + +if (adc->dma_chan) +dmaengine_terminate_all (adc->dma_chan); + +stm32_dfsdm_stop_conv (adc); + +stm32_dfsdm_stop_dfsdm (adc->dfsdm); + +return 0 ; +} + +static const struct iio_buffer_setup_ops stm32_dfsdm_buffer_setup_ops = { + .postenable = &stm32_dfsdm_postenable, + .predisable = &stm32_dfsdm_predisable, +}; + +/** +* stm32_dfsdm_get_buff_cb() - register a callback that will be called when +* DMA transfer period is achieved. +* +* @iio_dev: Handle to IIO device. +* @cb: Pointer to callback function: +* - data: pointer to data buffer +* - size: size in byte of the data buffer +* - private: pointer to consumer private structure. +* @private: Pointer to consumer private structure. +*/ +int stm32_dfsdm_get_buff_cb (struct iio_dev *iio_dev, +int (*cb)(const void *data,size_t size, +void *private), +void *private) +{ +struct stm32_dfsdm_adc *adc; + +if (!iio_dev) +return -EINVAL ; + adc =iio_priv (iio_dev); + + adc->cb = cb; + adc->cb_priv = private; + +return 0 ; +} +EXPORT_SYMBOL_GPL (stm32_dfsdm_get_buff_cb); + +/** +* stm32_dfsdm_release_buff_cb - unregister buffer callback +* +* @iio_dev: Handle to IIO device. +*/ +int stm32_dfsdm_release_buff_cb (struct iio_dev *iio_dev) +{ +struct stm32_dfsdm_adc *adc; + +if (!iio_dev) +return -EINVAL ; + adc =iio_priv (iio_dev); + + adc->cb =NULL ; + adc->cb_priv =NULL ; + +return 0 ; +} +EXPORT_SYMBOL_GPL (stm32_dfsdm_release_buff_cb); +static int stm32_dfsdm_single_conv (struct iio_dev *indio_dev,const struct iio_chan_spec *chan,int *res) {@@ -453,15 +767,41 @@ static int stm32_dfsdm_write_raw(struct iio_dev *indio_dev, {struct stm32_dfsdm_adc *adc =iio_priv (indio_dev);struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id]; +struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[adc->ch_id]; +unsigned int spi_freq = adc->spi_freq;int ret = -EINVAL ; -if (mask ==IIO_CHAN_INFO_OVERSAMPLING_RATIO ) { +switch (mask) { +case IIO_CHAN_INFO_OVERSAMPLING_RATIO : ret =stm32_dfsdm_set_osrs (fl,0 , val);if (!ret) adc->oversamp = val; + +return ret; + +case IIO_CHAN_INFO_SAMP_FREQ : +if (!val) +return -EINVAL ; +if (ch->src !=DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL ) + spi_freq = adc->dfsdm->spi_master_freq; + +if (spi_freq % val) +dev_warn (&indio_dev->dev, +"Sampling rate not accurate (%d)\n" , + spi_freq / (spi_freq / val)); + + ret =stm32_dfsdm_set_osrs (fl,0 , (spi_freq / val)); +if (ret <0 ) { +dev_err (&indio_dev->dev, +"Not able to find parameter that match!\n" ); +return ret; + } + adc->sample_freq = val; + +return 0 ; } -return ret; +return -EINVAL ; } static int stm32_dfsdm_read_raw(struct iio_dev *indio_dev,@@ -494,11 +834,22 @@ static int stm32_dfsdm_read_raw(struct iio_dev *indio_dev, *val = adc->oversamp;return IIO_VAL_INT ; + +case IIO_CHAN_INFO_SAMP_FREQ : + *val = adc->sample_freq; + +return IIO_VAL_INT ; }return -EINVAL ; } +static const struct iio_info stm32_dfsdm_info_audio = { + .hwfifo_set_watermark = stm32_dfsdm_set_watermark, + .read_raw = stm32_dfsdm_read_raw, + .write_raw = stm32_dfsdm_write_raw, +}; +static const struct iio_info stm32_dfsdm_info_adc = { .read_raw = stm32_dfsdm_read_raw, .write_raw = stm32_dfsdm_write_raw,@@ -531,6 +882,60 @@ static irqreturn_t stm32_dfsdm_irq(int irq, void *arg)return IRQ_HANDLED ; } +/* +* Define external info for SPI Frequency and audio sampling rate that can be +* configured by ASoC driver through consumer.h API +*/ +static const struct iio_chan_spec_ext_info dfsdm_adc_audio_ext_info[] = { +/* spi_clk_freq : clock freq on SPI/manchester bus used by channel */ + { + .name ="spi_clk_freq" , + .shared =IIO_SHARED_BY_TYPE , + .read = dfsdm_adc_audio_get_spiclk, + .write = dfsdm_adc_audio_set_spiclk, + }, + {}, +}; + +static int stm32_dfsdm_dma_request (struct iio_dev *indio_dev) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +struct dma_slave_config config; +int ret; + + adc->dma_chan =dma_request_slave_channel (&indio_dev->dev,"rx" ); +if (!adc->dma_chan) +return -EINVAL ; + + adc->rx_buf =dma_alloc_coherent (adc->dma_chan->device->dev, +DFSDM_DMA_BUFFER_SIZE , + &adc->dma_buf,GFP_KERNEL ); +if (!adc->rx_buf) { + ret = -ENOMEM ; + goto err_release; + } + +/* Configure DMA channel to read data register */ +memset (&config,0 ,sizeof (config)); + config.src_addr = (dma_addr_t )adc->dfsdm->phys_base; + config.src_addr +=DFSDM_RDATAR (adc->fl_id); + config.src_addr_width =DMA_SLAVE_BUSWIDTH_4_BYTES ; + + ret =dmaengine_slave_config (adc->dma_chan, &config); +if (ret) + goto err_free; + +return 0 ; + +err_free: +dma_free_coherent (adc->dma_chan->device->dev,DFSDM_DMA_BUFFER_SIZE , + adc->rx_buf, adc->dma_buf); +err_release: +dma_release_channel (adc->dma_chan); + +return ret; +} +static int stm32_dfsdm_adc_chan_init_one (struct iio_dev *indio_dev,struct iio_chan_spec *ch) {@@ -551,7 +956,12 @@ static int stm32_dfsdm_adc_chan_init_one(struct iio_dev *indio_dev, ch->info_mask_separate =BIT (IIO_CHAN_INFO_RAW ); ch->info_mask_shared_by_all =BIT (IIO_CHAN_INFO_OVERSAMPLING_RATIO ); - ch->scan_type.sign ='u' ; +if (adc->dev_data->type ==DFSDM_AUDIO ) { + ch->scan_type.sign ='s' ; + ch->ext_info = dfsdm_adc_audio_ext_info; + }else { + ch->scan_type.sign ='u' ; + } ch->scan_type.realbits =24 ; ch->scan_type.storagebits =32 ; adc->ch_id = ch->channel;@@ -560,6 +970,64 @@ static int stm32_dfsdm_adc_chan_init_one(struct iio_dev *indio_dev, &adc->dfsdm->ch_list[ch->channel]); } +static int stm32_dfsdm_audio_init (struct iio_dev *indio_dev) +{ +struct iio_chan_spec *ch; +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +struct stm32_dfsdm_channel *d_ch; +int ret; + + ret =stm32_dfsdm_dma_request (indio_dev); +if (ret) { +dev_err (&indio_dev->dev,"DMA request failed\n" ); +return ret; + } + + indio_dev->modes |=INDIO_BUFFER_SOFTWARE ; + + ret =iio_triggered_buffer_setup (indio_dev, + &iio_pollfunc_store_time, +NULL , + &stm32_dfsdm_buffer_setup_ops); +if (ret) { +dev_err (&indio_dev->dev,"Buffer setup failed\n" ); + goto err_dma_disable; + } + + ch =devm_kzalloc (&indio_dev->dev,sizeof (*ch),GFP_KERNEL ); +if (!ch) +return -ENOMEM ; + + ch->scan_index =0 ; + ret =stm32_dfsdm_adc_chan_init_one (indio_dev, ch); +if (ret <0 ) { +dev_err (&indio_dev->dev,"channels init failed\n" ); + goto err_buffer_cleanup; + } + ch->info_mask_separate =BIT (IIO_CHAN_INFO_SAMP_FREQ ); + + d_ch = &adc->dfsdm->ch_list[adc->ch_id]; +if (d_ch->src !=DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL ) + adc->spi_freq = adc->dfsdm->spi_master_freq; + + indio_dev->num_channels =1 ; + indio_dev->channels = ch; + +return 0 ; + +err_buffer_cleanup: +iio_triggered_buffer_cleanup (indio_dev); + +err_dma_disable: +if (adc->dma_chan) { +dma_free_coherent (adc->dma_chan->device->dev, +DFSDM_DMA_BUFFER_SIZE , + adc->rx_buf, adc->dma_buf); +dma_release_channel (adc->dma_chan); + } +return ret; +} +static int stm32_dfsdm_adc_init (struct iio_dev *indio_dev) {struct iio_chan_spec *ch;@@ -612,11 +1080,20 @@ static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_adc_data = { .init = stm32_dfsdm_adc_init, }; +static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_audio_data = { + .type =DFSDM_AUDIO , + .init = stm32_dfsdm_audio_init, +}; +static const struct of_device_id stm32_dfsdm_adc_match[] = { { .compatible ="st,stm32-dfsdm-adc" , .data = &stm32h7_dfsdm_adc_data, }, + { + .compatible ="st,stm32-dfsdm-dmic" , + .data = &stm32h7_dfsdm_audio_data, + }, {} };@@ -667,8 +1144,13 @@ static int stm32_dfsdm_adc_probe(struct platform_device *pdev) name =devm_kzalloc (dev,sizeof ("dfsdm-adc0" ),GFP_KERNEL );if (!name)return -ENOMEM ; - iio->info = &stm32_dfsdm_info_adc; -snprintf (name,sizeof ("dfsdm-adc0" ),"dfsdm-adc%d" , adc->fl_id); +if (dev_data->type ==DFSDM_AUDIO ) { + iio->info = &stm32_dfsdm_info_audio; +snprintf (name,sizeof ("dfsdm-pdm0" ),"dfsdm-pdm%d" , adc->fl_id); + }else { + iio->info = &stm32_dfsdm_info_adc; +snprintf (name,sizeof ("dfsdm-adc0" ),"dfsdm-adc%d" , adc->fl_id); + } iio->name = name;/* @@ -700,7 +1182,10 @@ static int stm32_dfsdm_adc_probe(struct platform_device *pdev)if (ret <0 )return ret; -return iio_device_register (iio); +iio_device_register (iio); +if (dev_data->type ==DFSDM_AUDIO ) +return devm_of_platform_populate (&pdev->dev); +return 0 ; }static int stm32_dfsdm_adc_remove (struct platform_device *pdev)@@ -709,7 +1194,14 @@ static int stm32_dfsdm_adc_remove(struct platform_device *pdev)struct iio_dev *indio_dev =iio_priv_to_dev (adc);iio_device_unregister (indio_dev); - +if (indio_dev->pollfunc) +iio_triggered_buffer_cleanup (indio_dev); +if (adc->dma_chan) { +dma_free_coherent (adc->dma_chan->device->dev, +DFSDM_DMA_BUFFER_SIZE , + adc->rx_buf, adc->dma_buf); +dma_release_channel (adc->dma_chan); + }return 0 ; }include/linux/iio/adc/stm32-dfsdm-adc.h new file +18
-0@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* This file discribe the STM32 DFSDM IIO driver API for audio part +* +* Copyright (C) 2017, STMicroelectronics - All Rights Reserved +* Author(s): Arnaud Pouliquen <[email protected]>. +*/ + +#ifndef STM32_DFSDM_ADC_H +#define STM32_DFSDM_ADC_H + +int stm32_dfsdm_get_buff_cb (struct iio_dev *iio_dev, +int (*cb)(const void *data,size_t size, +void *private), +void *private); +int stm32_dfsdm_release_buff_cb (struct iio_dev *iio_dev); + +#endif
Re: [PATCH v8 10/13] IIO: ADC: add stm32 DFSDM support for PDM microphone
Hi Jonathan,
Quoted 8 lines · Jonathan Cameron
Quoted 5 lines · On Mon, 11 Dec 2017 11:18:41 +0100 Arnaud Pouliquen <[email protected]>…
This code offers a way to handle PDM audio microphones in ASOC framework. Audio driver should use consumer API. A specific management is implemented for DMA, with a callback, to allows to handle audio buffers efficiently.
Quoted 8 lines
Quoted 1 line
Signed-off-by Arnaud Pouliquen <[email protected]>Hi Arnaud,
I raise a few queries on v7 of this patch.
Never received the associated mail (and no in my spam list):( ,I just discover it...
Thanks to have highlighted this and sorry for the inconvenience, I will send a v9.
Regards
Arnaud
Quoted 702 lines
Jonathan
Quoted 698 lines
--- .../ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32 | 16 + drivers/iio/adc/stm32-dfsdm-adc.c | 508 ++++++++++++++++++++- include/linux/iio/adc/stm32-dfsdm-adc.h | 18 + 3 files changed, 534 insertions(+), 8 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32 create mode 100644 include/linux/iio/adc/stm32-dfsdm-adc.hDocumentation/ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32 new file +16
-0@@ -0,0 +1,16 @@ +What: /sys/bus/iio/devices/iio:deviceX/in_voltage_spi_clk_freq +KernelVersion: 4.14 +Contact: arnaud.pouliquen at st.com +Description: + For audio purpose only. + Used by audio driver to set/get the spi input frequency. + This is mandatory if DFSDM is slave on SPI bus, to + provide information on the SPI clock frequency during runtime + Notice that the SPI frequency should be a multiple of sample + frequency to ensure the precision. + if DFSDM input is SPI master + Reading SPI clkout frequency, + error on writing + If DFSDM input is SPI Slave: + Reading returns value previously set. + Writing value before starting conversions.drivers/iio/adc/stm32-dfsdm-adc.c +500
-8@@ -6,19 +6,25 @@* Author: Arnaud Pouliquen <[email protected]>. */ +#include <linux/dmaengine.h> +#include <linux/dma-mapping.h> #include <linux/interrupt.h> #include <linux/iio/buffer.h> #include <linux/iio/hw-consumer.h> #include <linux/iio/iio.h> #include <linux/iio/sysfs.h> +#include <linux/iio/trigger_consumer.h> +#include <linux/iio/triggered_buffer.h> #include <linux/module.h> -#include <linux/of.h> +#include <linux/of_device.h> #include <linux/platform_device.h> #include <linux/regmap.h> #include <linux/slab.h> #include "stm32-dfsdm.h" +#define DFSDM_DMA_BUFFER_SIZE (4 * PAGE_SIZE) +/* Conversion timeout */ #define DFSDM_TIMEOUT_US 100000#define DFSDM_TIMEOUT (msecs_to_jiffies(DFSDM_TIMEOUT_US / 1000))@@ -58,6 +64,18 @@ struct stm32_dfsdm_adc {struct completion completion;u32 *buffer; +/* Audio specific */ +unsigned int spi_freq;/* SPI bus clock frequency */ +unsigned int sample_freq;/* Sample frequency after filter decimation */ +int (*cb)(const void *data,size_t size,void *cb_priv); +void *cb_priv; + +/* DMA */ +u8 *rx_buf; +unsigned int bufi;/* Buffer current position */ +unsigned int buf_sz;/* Buffer size */ +struct dma_chan *dma_chan; +dma_addr_t dma_buf; };struct stm32_dfsdm_str2field {@@ -351,10 +369,63 @@ int stm32_dfsdm_channel_parse_of(struct stm32_dfsdm *dfsdm,return 0 ; } +static ssize_t dfsdm_adc_audio_get_spiclk (struct iio_dev *indio_dev, +uintptr_t priv, +const struct iio_chan_spec *chan, +char *buf) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); + +return snprintf (buf,PAGE_SIZE ,"%d\n" , adc->spi_freq); +} + +static ssize_t dfsdm_adc_audio_set_spiclk (struct iio_dev *indio_dev, +uintptr_t priv, +const struct iio_chan_spec *chan, +const char *buf,size_t len) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id]; +struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[adc->ch_id]; +unsigned int sample_freq = adc->sample_freq; +unsigned int spi_freq; +int ret; + +dev_err (&indio_dev->dev,"enter %s\n" , __func__); +/* If DFSDM is master on SPI, SPI freq can not be updated */ +if (ch->src !=DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL ) +return -EPERM ; + + ret =kstrtoint (buf,0 , &spi_freq); +if (ret) +return ret; + +if (!spi_freq) +return -EINVAL ; + +if (sample_freq) { +if (spi_freq % sample_freq) +dev_warn (&indio_dev->dev, +"Sampling rate not accurate (%d)\n" , + spi_freq / (spi_freq / sample_freq)); + + ret =stm32_dfsdm_set_osrs (fl,0 , (spi_freq / sample_freq)); +if (ret <0 ) { +dev_err (&indio_dev->dev, +"No filter parameters that match!\n" ); +return ret; + } + } + adc->spi_freq = spi_freq; + +return len; +} +static int stm32_dfsdm_start_conv (struct stm32_dfsdm_adc *adc,bool dma) {struct regmap *regmap = adc->dfsdm->regmap;int ret; +unsigned int dma_en =0 , cont_en =0 ; ret =stm32_dfsdm_start_channel (adc->dfsdm, adc->ch_id);if (ret <0 )@@ -365,6 +436,24 @@ static int stm32_dfsdm_start_conv(struct stm32_dfsdm_adc *adc, bool dma)if (ret <0 ) goto stop_channels; +if (dma) { +/* Enable DMA transfer*/ + dma_en =DFSDM_CR1_RDMAEN (1 ); +/* Enable conversion triggered by SPI clock*/ + cont_en =DFSDM_CR1_RCONT (1 ); + } +/* Enable DMA transfer*/ + ret =regmap_update_bits (regmap,DFSDM_CR1 (adc->fl_id), +DFSDM_CR1_RDMAEN_MASK , dma_en); +if (ret <0 ) + goto stop_channels; + +/* Enable conversion triggered by SPI clock*/ + ret =regmap_update_bits (regmap,DFSDM_CR1 (adc->fl_id), +DFSDM_CR1_RCONT_MASK , cont_en); +if (ret <0 ) + goto stop_channels; + ret =stm32_dfsdm_start_filter (adc->dfsdm, adc->fl_id);if (ret <0 ) goto stop_channels;@@ -398,6 +487,231 @@ static void stm32_dfsdm_stop_conv(struct stm32_dfsdm_adc *adc)stm32_dfsdm_stop_channel (adc->dfsdm, adc->ch_id); } +static int stm32_dfsdm_set_watermark (struct iio_dev *indio_dev, +unsigned int val) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +unsigned int watermark =DFSDM_DMA_BUFFER_SIZE /2 ; + +/* +* DMA cyclic transfers are used, buffer is split into two periods. +* There should be : +* - always one buffer (period) DMA is working on +* - one buffer (period) driver pushed to ASoC side. +*/ + watermark =min (watermark, val * (unsigned int )(sizeof (u32))); + adc->buf_sz = watermark *2 ; + +return 0 ; +} + +static unsigned int stm32_dfsdm_adc_dma_residue (struct stm32_dfsdm_adc *adc) +{ +struct dma_tx_state state; +enum dma_status status; + + status =dmaengine_tx_status (adc->dma_chan, + adc->dma_chan->cookie, + &state); +if (status ==DMA_IN_PROGRESS ) { +/* Residue is size in bytes from end of buffer */ +unsigned int i = adc->buf_sz - state.residue; +unsigned int size; + +/* Return available bytes */ +if (i >= adc->bufi) + size = i - adc->bufi; +else + size = adc->buf_sz + i - adc->bufi; + +return size; + } + +return 0 ; +} + +static void stm32_dfsdm_audio_dma_buffer_done (void *data) +{ +struct iio_dev *indio_dev = data; +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +int available =stm32_dfsdm_adc_dma_residue (adc); +size_t old_pos; + +/* +* FIXME: In Kernel interface does not support cyclic DMA buffer,and +* offers only an interface to push data samples per samples. +* For this reason IIO buffer interface is not used and interface is +* bypassed using a private callback registered by ASoC. +* This should be a temporary solution waiting a cyclic DMA engine +* support in IIO. +*/ + +dev_dbg (&indio_dev->dev,"%s: pos = %d, available = %d\n" , __func__, + adc->bufi, available); + old_pos = adc->bufi; + +while (available >= indio_dev->scan_bytes) { +u32 *buffer = (u32 *)&adc->rx_buf[adc->bufi]; + +/* Mask 8 LSB that contains the channel ID */ + *buffer = (*buffer &0xFFFFFF00 ) <<8 ; + available -= indio_dev->scan_bytes; + adc->bufi += indio_dev->scan_bytes; +if (adc->bufi >= adc->buf_sz) { +if (adc->cb) + adc->cb (&adc->rx_buf[old_pos], + adc->buf_sz - old_pos, adc->cb_priv); + adc->bufi =0 ; + old_pos =0 ; + } + } +if (adc->cb) + adc->cb (&adc->rx_buf[old_pos], adc->bufi - old_pos, + adc->cb_priv); +} + +static int stm32_dfsdm_adc_dma_start (struct iio_dev *indio_dev) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +struct dma_async_tx_descriptor *desc; +dma_cookie_t cookie; +int ret; + +if (!adc->dma_chan) +return -EINVAL ; + +dev_dbg (&indio_dev->dev,"%s size=%d watermark=%d\n" , __func__, + adc->buf_sz, adc->buf_sz /2 ); + +/* Prepare a DMA cyclic transaction */ + desc =dmaengine_prep_dma_cyclic (adc->dma_chan, + adc->dma_buf, + adc->buf_sz, adc->buf_sz /2 , +DMA_DEV_TO_MEM , +DMA_PREP_INTERRUPT ); +if (!desc) +return -EBUSY ; + + desc->callback = stm32_dfsdm_audio_dma_buffer_done; + desc->callback_param = indio_dev; + + cookie =dmaengine_submit (desc); + ret =dma_submit_error (cookie); +if (ret) { +dmaengine_terminate_all (adc->dma_chan); +return ret; + } + +/* Issue pending DMA requests */ +dma_async_issue_pending (adc->dma_chan); + +return 0 ; +} + +static int stm32_dfsdm_postenable (struct iio_dev *indio_dev) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +int ret; + +/* Reset adc buffer index */ + adc->bufi =0 ; + + ret =stm32_dfsdm_start_dfsdm (adc->dfsdm); +if (ret <0 ) +return ret; + + ret =stm32_dfsdm_start_conv (adc, true); +if (ret) { +dev_err (&indio_dev->dev,"Can't start conversion\n" ); + goto stop_dfsdm; + } + +if (adc->dma_chan) { + ret =stm32_dfsdm_adc_dma_start (indio_dev); +if (ret) { +dev_err (&indio_dev->dev,"Can't start DMA\n" ); + goto err_stop_conv; + } + } + +return 0 ; + +err_stop_conv: +stm32_dfsdm_stop_conv (adc); +stop_dfsdm: +stm32_dfsdm_stop_dfsdm (adc->dfsdm); + +return ret; +} + +static int stm32_dfsdm_predisable (struct iio_dev *indio_dev) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); + +if (adc->dma_chan) +dmaengine_terminate_all (adc->dma_chan); + +stm32_dfsdm_stop_conv (adc); + +stm32_dfsdm_stop_dfsdm (adc->dfsdm); + +return 0 ; +} + +static const struct iio_buffer_setup_ops stm32_dfsdm_buffer_setup_ops = { + .postenable = &stm32_dfsdm_postenable, + .predisable = &stm32_dfsdm_predisable, +}; + +/** +* stm32_dfsdm_get_buff_cb() - register a callback that will be called when +* DMA transfer period is achieved. +* +* @iio_dev: Handle to IIO device. +* @cb: Pointer to callback function: +* - data: pointer to data buffer +* - size: size in byte of the data buffer +* - private: pointer to consumer private structure. +* @private: Pointer to consumer private structure. +*/ +int stm32_dfsdm_get_buff_cb (struct iio_dev *iio_dev, +int (*cb)(const void *data,size_t size, +void *private), +void *private) +{ +struct stm32_dfsdm_adc *adc; + +if (!iio_dev) +return -EINVAL ; + adc =iio_priv (iio_dev); + + adc->cb = cb; + adc->cb_priv = private; + +return 0 ; +} +EXPORT_SYMBOL_GPL (stm32_dfsdm_get_buff_cb); + +/** +* stm32_dfsdm_release_buff_cb - unregister buffer callback +* +* @iio_dev: Handle to IIO device. +*/ +int stm32_dfsdm_release_buff_cb (struct iio_dev *iio_dev) +{ +struct stm32_dfsdm_adc *adc; + +if (!iio_dev) +return -EINVAL ; + adc =iio_priv (iio_dev); + + adc->cb =NULL ; + adc->cb_priv =NULL ; + +return 0 ; +} +EXPORT_SYMBOL_GPL (stm32_dfsdm_release_buff_cb); +static int stm32_dfsdm_single_conv (struct iio_dev *indio_dev,const struct iio_chan_spec *chan,int *res) {@@ -453,15 +767,41 @@ static int stm32_dfsdm_write_raw(struct iio_dev *indio_dev, {struct stm32_dfsdm_adc *adc =iio_priv (indio_dev);struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id]; +struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[adc->ch_id]; +unsigned int spi_freq = adc->spi_freq;int ret = -EINVAL ; -if (mask ==IIO_CHAN_INFO_OVERSAMPLING_RATIO ) { +switch (mask) { +case IIO_CHAN_INFO_OVERSAMPLING_RATIO : ret =stm32_dfsdm_set_osrs (fl,0 , val);if (!ret) adc->oversamp = val; + +return ret; + +case IIO_CHAN_INFO_SAMP_FREQ : +if (!val) +return -EINVAL ; +if (ch->src !=DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL ) + spi_freq = adc->dfsdm->spi_master_freq; + +if (spi_freq % val) +dev_warn (&indio_dev->dev, +"Sampling rate not accurate (%d)\n" , + spi_freq / (spi_freq / val)); + + ret =stm32_dfsdm_set_osrs (fl,0 , (spi_freq / val)); +if (ret <0 ) { +dev_err (&indio_dev->dev, +"Not able to find parameter that match!\n" ); +return ret; + } + adc->sample_freq = val; + +return 0 ; } -return ret; +return -EINVAL ; } static int stm32_dfsdm_read_raw(struct iio_dev *indio_dev,@@ -494,11 +834,22 @@ static int stm32_dfsdm_read_raw(struct iio_dev *indio_dev, *val = adc->oversamp;return IIO_VAL_INT ; + +case IIO_CHAN_INFO_SAMP_FREQ : + *val = adc->sample_freq; + +return IIO_VAL_INT ; }return -EINVAL ; } +static const struct iio_info stm32_dfsdm_info_audio = { + .hwfifo_set_watermark = stm32_dfsdm_set_watermark, + .read_raw = stm32_dfsdm_read_raw, + .write_raw = stm32_dfsdm_write_raw, +}; +static const struct iio_info stm32_dfsdm_info_adc = { .read_raw = stm32_dfsdm_read_raw, .write_raw = stm32_dfsdm_write_raw,@@ -531,6 +882,60 @@ static irqreturn_t stm32_dfsdm_irq(int irq, void *arg)return IRQ_HANDLED ; } +/* +* Define external info for SPI Frequency and audio sampling rate that can be +* configured by ASoC driver through consumer.h API +*/ +static const struct iio_chan_spec_ext_info dfsdm_adc_audio_ext_info[] = { +/* spi_clk_freq : clock freq on SPI/manchester bus used by channel */ + { + .name ="spi_clk_freq" , + .shared =IIO_SHARED_BY_TYPE , + .read = dfsdm_adc_audio_get_spiclk, + .write = dfsdm_adc_audio_set_spiclk, + }, + {}, +}; + +static int stm32_dfsdm_dma_request (struct iio_dev *indio_dev) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +struct dma_slave_config config; +int ret; + + adc->dma_chan =dma_request_slave_channel (&indio_dev->dev,"rx" ); +if (!adc->dma_chan) +return -EINVAL ; + + adc->rx_buf =dma_alloc_coherent (adc->dma_chan->device->dev, +DFSDM_DMA_BUFFER_SIZE , + &adc->dma_buf,GFP_KERNEL ); +if (!adc->rx_buf) { + ret = -ENOMEM ; + goto err_release; + } + +/* Configure DMA channel to read data register */ +memset (&config,0 ,sizeof (config)); + config.src_addr = (dma_addr_t )adc->dfsdm->phys_base; + config.src_addr +=DFSDM_RDATAR (adc->fl_id); + config.src_addr_width =DMA_SLAVE_BUSWIDTH_4_BYTES ; + + ret =dmaengine_slave_config (adc->dma_chan, &config); +if (ret) + goto err_free; + +return 0 ; + +err_free: +dma_free_coherent (adc->dma_chan->device->dev,DFSDM_DMA_BUFFER_SIZE , + adc->rx_buf, adc->dma_buf); +err_release: +dma_release_channel (adc->dma_chan); + +return ret; +} +static int stm32_dfsdm_adc_chan_init_one (struct iio_dev *indio_dev,struct iio_chan_spec *ch) {@@ -551,7 +956,12 @@ static int stm32_dfsdm_adc_chan_init_one(struct iio_dev *indio_dev, ch->info_mask_separate =BIT (IIO_CHAN_INFO_RAW ); ch->info_mask_shared_by_all =BIT (IIO_CHAN_INFO_OVERSAMPLING_RATIO ); - ch->scan_type.sign ='u' ; +if (adc->dev_data->type ==DFSDM_AUDIO ) { + ch->scan_type.sign ='s' ; + ch->ext_info = dfsdm_adc_audio_ext_info; + }else { + ch->scan_type.sign ='u' ; + } ch->scan_type.realbits =24 ; ch->scan_type.storagebits =32 ; adc->ch_id = ch->channel;@@ -560,6 +970,64 @@ static int stm32_dfsdm_adc_chan_init_one(struct iio_dev *indio_dev, &adc->dfsdm->ch_list[ch->channel]); } +static int stm32_dfsdm_audio_init (struct iio_dev *indio_dev) +{ +struct iio_chan_spec *ch; +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +struct stm32_dfsdm_channel *d_ch; +int ret; + + ret =stm32_dfsdm_dma_request (indio_dev); +if (ret) { +dev_err (&indio_dev->dev,"DMA request failed\n" ); +return ret; + } + + indio_dev->modes |=INDIO_BUFFER_SOFTWARE ; + + ret =iio_triggered_buffer_setup (indio_dev, + &iio_pollfunc_store_time, +NULL , + &stm32_dfsdm_buffer_setup_ops); +if (ret) { +dev_err (&indio_dev->dev,"Buffer setup failed\n" ); + goto err_dma_disable; + } + + ch =devm_kzalloc (&indio_dev->dev,sizeof (*ch),GFP_KERNEL ); +if (!ch) +return -ENOMEM ; + + ch->scan_index =0 ; + ret =stm32_dfsdm_adc_chan_init_one (indio_dev, ch); +if (ret <0 ) { +dev_err (&indio_dev->dev,"channels init failed\n" ); + goto err_buffer_cleanup; + } + ch->info_mask_separate =BIT (IIO_CHAN_INFO_SAMP_FREQ ); + + d_ch = &adc->dfsdm->ch_list[adc->ch_id]; +if (d_ch->src !=DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL ) + adc->spi_freq = adc->dfsdm->spi_master_freq; + + indio_dev->num_channels =1 ; + indio_dev->channels = ch; + +return 0 ; + +err_buffer_cleanup: +iio_triggered_buffer_cleanup (indio_dev); + +err_dma_disable: +if (adc->dma_chan) { +dma_free_coherent (adc->dma_chan->device->dev, +DFSDM_DMA_BUFFER_SIZE , + adc->rx_buf, adc->dma_buf); +dma_release_channel (adc->dma_chan); + } +return ret; +} +static int stm32_dfsdm_adc_init (struct iio_dev *indio_dev) {struct iio_chan_spec *ch;@@ -612,11 +1080,20 @@ static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_adc_data = { .init = stm32_dfsdm_adc_init, }; +static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_audio_data = { + .type =DFSDM_AUDIO , + .init = stm32_dfsdm_audio_init, +}; +static const struct of_device_id stm32_dfsdm_adc_match[] = { { .compatible ="st,stm32-dfsdm-adc" , .data = &stm32h7_dfsdm_adc_data, }, + { + .compatible ="st,stm32-dfsdm-dmic" , + .data = &stm32h7_dfsdm_audio_data, + }, {} };@@ -667,8 +1144,13 @@ static int stm32_dfsdm_adc_probe(struct platform_device *pdev) name =devm_kzalloc (dev,sizeof ("dfsdm-adc0" ),GFP_KERNEL );if (!name)return -ENOMEM ; - iio->info = &stm32_dfsdm_info_adc; -snprintf (name,sizeof ("dfsdm-adc0" ),"dfsdm-adc%d" , adc->fl_id); +if (dev_data->type ==DFSDM_AUDIO ) { + iio->info = &stm32_dfsdm_info_audio; +snprintf (name,sizeof ("dfsdm-pdm0" ),"dfsdm-pdm%d" , adc->fl_id); + }else { + iio->info = &stm32_dfsdm_info_adc; +snprintf (name,sizeof ("dfsdm-adc0" ),"dfsdm-adc%d" , adc->fl_id); + } iio->name = name;/* @@ -700,7 +1182,10 @@ static int stm32_dfsdm_adc_probe(struct platform_device *pdev)if (ret <0 )return ret; -return iio_device_register (iio); +iio_device_register (iio); +if (dev_data->type ==DFSDM_AUDIO ) +return devm_of_platform_populate (&pdev->dev); +return 0 ; }static int stm32_dfsdm_adc_remove (struct platform_device *pdev)@@ -709,7 +1194,14 @@ static int stm32_dfsdm_adc_remove(struct platform_device *pdev)struct iio_dev *indio_dev =iio_priv_to_dev (adc);iio_device_unregister (indio_dev); - +if (indio_dev->pollfunc) +iio_triggered_buffer_cleanup (indio_dev); +if (adc->dma_chan) { +dma_free_coherent (adc->dma_chan->device->dev, +DFSDM_DMA_BUFFER_SIZE , + adc->rx_buf, adc->dma_buf); +dma_release_channel (adc->dma_chan); + }return 0 ; }include/linux/iio/adc/stm32-dfsdm-adc.h new file +18
-0@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* This file discribe the STM32 DFSDM IIO driver API for audio part +* +* Copyright (C) 2017, STMicroelectronics - All Rights Reserved +* Author(s): Arnaud Pouliquen <[email protected]>. +*/ + +#ifndef STM32_DFSDM_ADC_H +#define STM32_DFSDM_ADC_H + +int stm32_dfsdm_get_buff_cb (struct iio_dev *iio_dev, +int (*cb)(const void *data,size_t size, +void *private), +void *private); +int stm32_dfsdm_release_buff_cb (struct iio_dev *iio_dev); + +#endif
PATCH v8 10/13 IIO: ADC: add stm32 DFSDM support for PDM microphone
Quoted 17 lines · On Wed, 13 Dec 2017 09:42:02 +0100 Arnaud Pouliquen <[email protected]>…
Hi Jonathan,
Quoted 13 lines · Jonathan Cameron
Quoted 6 lines · On Mon, 11 Dec 2017 11:18:41 +0100 Arnaud Pouliquen <[email protected]>…
This code offers a way to handle PDM audio microphones in ASOC framework. Audio driver should use consumer API. A specific management is implemented for DMA, with a callback, to allows to handle audio buffers efficiently.
Signed-off-by Arnaud Pouliquen <[email protected]>Hi Arnaud,
Quoted 8 lines
Quoted 3 lines
I raise a few queries on v7 of this patch.
Never received the associated mail (and no in my spam list):( ,I just discover it...
Thanks to have highlighted this and sorry for the inconvenience, I will send a v9.
Don't worry about it! Happens to me from time to time as well.
Thanks,
Jonathan
Quoted 712 lines
Regards
ArnaudQuoted 693 lines
Jonathan
Quoted 690 lines
--- .../ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32 | 16 + drivers/iio/adc/stm32-dfsdm-adc.c | 508 ++++++++++++++++++++- include/linux/iio/adc/stm32-dfsdm-adc.h | 18 + 3 files changed, 534 insertions(+), 8 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32 create mode 100644 include/linux/iio/adc/stm32-dfsdm-adc.hDocumentation/ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32 new file +16
-0@@ -0,0 +1,16 @@ +What: /sys/bus/iio/devices/iio:deviceX/in_voltage_spi_clk_freq +KernelVersion: 4.14 +Contact: arnaud.pouliquen at st.com +Description: + For audio purpose only. + Used by audio driver to set/get the spi input frequency. + This is mandatory if DFSDM is slave on SPI bus, to + provide information on the SPI clock frequency during runtime + Notice that the SPI frequency should be a multiple of sample + frequency to ensure the precision. + if DFSDM input is SPI master + Reading SPI clkout frequency, + error on writing + If DFSDM input is SPI Slave: + Reading returns value previously set. + Writing value before starting conversions.drivers/iio/adc/stm32-dfsdm-adc.c +500
-8@@ -6,19 +6,25 @@* Author: Arnaud Pouliquen <[email protected]>. */ +#include <linux/dmaengine.h> +#include <linux/dma-mapping.h> #include <linux/interrupt.h> #include <linux/iio/buffer.h> #include <linux/iio/hw-consumer.h> #include <linux/iio/iio.h> #include <linux/iio/sysfs.h> +#include <linux/iio/trigger_consumer.h> +#include <linux/iio/triggered_buffer.h> #include <linux/module.h> -#include <linux/of.h> +#include <linux/of_device.h> #include <linux/platform_device.h> #include <linux/regmap.h> #include <linux/slab.h> #include "stm32-dfsdm.h" +#define DFSDM_DMA_BUFFER_SIZE (4 * PAGE_SIZE) +/* Conversion timeout */ #define DFSDM_TIMEOUT_US 100000#define DFSDM_TIMEOUT (msecs_to_jiffies(DFSDM_TIMEOUT_US / 1000))@@ -58,6 +64,18 @@ struct stm32_dfsdm_adc {struct completion completion;u32 *buffer; +/* Audio specific */ +unsigned int spi_freq;/* SPI bus clock frequency */ +unsigned int sample_freq;/* Sample frequency after filter decimation */ +int (*cb)(const void *data,size_t size,void *cb_priv); +void *cb_priv; + +/* DMA */ +u8 *rx_buf; +unsigned int bufi;/* Buffer current position */ +unsigned int buf_sz;/* Buffer size */ +struct dma_chan *dma_chan; +dma_addr_t dma_buf; };struct stm32_dfsdm_str2field {@@ -351,10 +369,63 @@ int stm32_dfsdm_channel_parse_of(struct stm32_dfsdm *dfsdm,return 0 ; } +static ssize_t dfsdm_adc_audio_get_spiclk (struct iio_dev *indio_dev, +uintptr_t priv, +const struct iio_chan_spec *chan, +char *buf) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); + +return snprintf (buf,PAGE_SIZE ,"%d\n" , adc->spi_freq); +} + +static ssize_t dfsdm_adc_audio_set_spiclk (struct iio_dev *indio_dev, +uintptr_t priv, +const struct iio_chan_spec *chan, +const char *buf,size_t len) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id]; +struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[adc->ch_id]; +unsigned int sample_freq = adc->sample_freq; +unsigned int spi_freq; +int ret; + +dev_err (&indio_dev->dev,"enter %s\n" , __func__); +/* If DFSDM is master on SPI, SPI freq can not be updated */ +if (ch->src !=DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL ) +return -EPERM ; + + ret =kstrtoint (buf,0 , &spi_freq); +if (ret) +return ret; + +if (!spi_freq) +return -EINVAL ; + +if (sample_freq) { +if (spi_freq % sample_freq) +dev_warn (&indio_dev->dev, +"Sampling rate not accurate (%d)\n" , + spi_freq / (spi_freq / sample_freq)); + + ret =stm32_dfsdm_set_osrs (fl,0 , (spi_freq / sample_freq)); +if (ret <0 ) { +dev_err (&indio_dev->dev, +"No filter parameters that match!\n" ); +return ret; + } + } + adc->spi_freq = spi_freq; + +return len; +} +static int stm32_dfsdm_start_conv (struct stm32_dfsdm_adc *adc,bool dma) {struct regmap *regmap = adc->dfsdm->regmap;int ret; +unsigned int dma_en =0 , cont_en =0 ; ret =stm32_dfsdm_start_channel (adc->dfsdm, adc->ch_id);if (ret <0 )@@ -365,6 +436,24 @@ static int stm32_dfsdm_start_conv(struct stm32_dfsdm_adc *adc, bool dma)if (ret <0 ) goto stop_channels; +if (dma) { +/* Enable DMA transfer*/ + dma_en =DFSDM_CR1_RDMAEN (1 ); +/* Enable conversion triggered by SPI clock*/ + cont_en =DFSDM_CR1_RCONT (1 ); + } +/* Enable DMA transfer*/ + ret =regmap_update_bits (regmap,DFSDM_CR1 (adc->fl_id), +DFSDM_CR1_RDMAEN_MASK , dma_en); +if (ret <0 ) + goto stop_channels; + +/* Enable conversion triggered by SPI clock*/ + ret =regmap_update_bits (regmap,DFSDM_CR1 (adc->fl_id), +DFSDM_CR1_RCONT_MASK , cont_en); +if (ret <0 ) + goto stop_channels; + ret =stm32_dfsdm_start_filter (adc->dfsdm, adc->fl_id);if (ret <0 ) goto stop_channels;@@ -398,6 +487,231 @@ static void stm32_dfsdm_stop_conv(struct stm32_dfsdm_adc *adc)stm32_dfsdm_stop_channel (adc->dfsdm, adc->ch_id); } +static int stm32_dfsdm_set_watermark (struct iio_dev *indio_dev, +unsigned int val) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +unsigned int watermark =DFSDM_DMA_BUFFER_SIZE /2 ; + +/* +* DMA cyclic transfers are used, buffer is split into two periods. +* There should be : +* - always one buffer (period) DMA is working on +* - one buffer (period) driver pushed to ASoC side. +*/ + watermark =min (watermark, val * (unsigned int )(sizeof (u32))); + adc->buf_sz = watermark *2 ; + +return 0 ; +} + +static unsigned int stm32_dfsdm_adc_dma_residue (struct stm32_dfsdm_adc *adc) +{ +struct dma_tx_state state; +enum dma_status status; + + status =dmaengine_tx_status (adc->dma_chan, + adc->dma_chan->cookie, + &state); +if (status ==DMA_IN_PROGRESS ) { +/* Residue is size in bytes from end of buffer */ +unsigned int i = adc->buf_sz - state.residue; +unsigned int size; + +/* Return available bytes */ +if (i >= adc->bufi) + size = i - adc->bufi; +else + size = adc->buf_sz + i - adc->bufi; + +return size; + } + +return 0 ; +} + +static void stm32_dfsdm_audio_dma_buffer_done (void *data) +{ +struct iio_dev *indio_dev = data; +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +int available =stm32_dfsdm_adc_dma_residue (adc); +size_t old_pos; + +/* +* FIXME: In Kernel interface does not support cyclic DMA buffer,and +* offers only an interface to push data samples per samples. +* For this reason IIO buffer interface is not used and interface is +* bypassed using a private callback registered by ASoC. +* This should be a temporary solution waiting a cyclic DMA engine +* support in IIO. +*/ + +dev_dbg (&indio_dev->dev,"%s: pos = %d, available = %d\n" , __func__, + adc->bufi, available); + old_pos = adc->bufi; + +while (available >= indio_dev->scan_bytes) { +u32 *buffer = (u32 *)&adc->rx_buf[adc->bufi]; + +/* Mask 8 LSB that contains the channel ID */ + *buffer = (*buffer &0xFFFFFF00 ) <<8 ; + available -= indio_dev->scan_bytes; + adc->bufi += indio_dev->scan_bytes; +if (adc->bufi >= adc->buf_sz) { +if (adc->cb) + adc->cb (&adc->rx_buf[old_pos], + adc->buf_sz - old_pos, adc->cb_priv); + adc->bufi =0 ; + old_pos =0 ; + } + } +if (adc->cb) + adc->cb (&adc->rx_buf[old_pos], adc->bufi - old_pos, + adc->cb_priv); +} + +static int stm32_dfsdm_adc_dma_start (struct iio_dev *indio_dev) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +struct dma_async_tx_descriptor *desc; +dma_cookie_t cookie; +int ret; + +if (!adc->dma_chan) +return -EINVAL ; + +dev_dbg (&indio_dev->dev,"%s size=%d watermark=%d\n" , __func__, + adc->buf_sz, adc->buf_sz /2 ); + +/* Prepare a DMA cyclic transaction */ + desc =dmaengine_prep_dma_cyclic (adc->dma_chan, + adc->dma_buf, + adc->buf_sz, adc->buf_sz /2 , +DMA_DEV_TO_MEM , +DMA_PREP_INTERRUPT ); +if (!desc) +return -EBUSY ; + + desc->callback = stm32_dfsdm_audio_dma_buffer_done; + desc->callback_param = indio_dev; + + cookie =dmaengine_submit (desc); + ret =dma_submit_error (cookie); +if (ret) { +dmaengine_terminate_all (adc->dma_chan); +return ret; + } + +/* Issue pending DMA requests */ +dma_async_issue_pending (adc->dma_chan); + +return 0 ; +} + +static int stm32_dfsdm_postenable (struct iio_dev *indio_dev) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +int ret; + +/* Reset adc buffer index */ + adc->bufi =0 ; + + ret =stm32_dfsdm_start_dfsdm (adc->dfsdm); +if (ret <0 ) +return ret; + + ret =stm32_dfsdm_start_conv (adc, true); +if (ret) { +dev_err (&indio_dev->dev,"Can't start conversion\n" ); + goto stop_dfsdm; + } + +if (adc->dma_chan) { + ret =stm32_dfsdm_adc_dma_start (indio_dev); +if (ret) { +dev_err (&indio_dev->dev,"Can't start DMA\n" ); + goto err_stop_conv; + } + } + +return 0 ; + +err_stop_conv: +stm32_dfsdm_stop_conv (adc); +stop_dfsdm: +stm32_dfsdm_stop_dfsdm (adc->dfsdm); + +return ret; +} + +static int stm32_dfsdm_predisable (struct iio_dev *indio_dev) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); + +if (adc->dma_chan) +dmaengine_terminate_all (adc->dma_chan); + +stm32_dfsdm_stop_conv (adc); + +stm32_dfsdm_stop_dfsdm (adc->dfsdm); + +return 0 ; +} + +static const struct iio_buffer_setup_ops stm32_dfsdm_buffer_setup_ops = { + .postenable = &stm32_dfsdm_postenable, + .predisable = &stm32_dfsdm_predisable, +}; + +/** +* stm32_dfsdm_get_buff_cb() - register a callback that will be called when +* DMA transfer period is achieved. +* +* @iio_dev: Handle to IIO device. +* @cb: Pointer to callback function: +* - data: pointer to data buffer +* - size: size in byte of the data buffer +* - private: pointer to consumer private structure. +* @private: Pointer to consumer private structure. +*/ +int stm32_dfsdm_get_buff_cb (struct iio_dev *iio_dev, +int (*cb)(const void *data,size_t size, +void *private), +void *private) +{ +struct stm32_dfsdm_adc *adc; + +if (!iio_dev) +return -EINVAL ; + adc =iio_priv (iio_dev); + + adc->cb = cb; + adc->cb_priv = private; + +return 0 ; +} +EXPORT_SYMBOL_GPL (stm32_dfsdm_get_buff_cb); + +/** +* stm32_dfsdm_release_buff_cb - unregister buffer callback +* +* @iio_dev: Handle to IIO device. +*/ +int stm32_dfsdm_release_buff_cb (struct iio_dev *iio_dev) +{ +struct stm32_dfsdm_adc *adc; + +if (!iio_dev) +return -EINVAL ; + adc =iio_priv (iio_dev); + + adc->cb =NULL ; + adc->cb_priv =NULL ; + +return 0 ; +} +EXPORT_SYMBOL_GPL (stm32_dfsdm_release_buff_cb); +static int stm32_dfsdm_single_conv (struct iio_dev *indio_dev,const struct iio_chan_spec *chan,int *res) {@@ -453,15 +767,41 @@ static int stm32_dfsdm_write_raw(struct iio_dev *indio_dev, {struct stm32_dfsdm_adc *adc =iio_priv (indio_dev);struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id]; +struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[adc->ch_id]; +unsigned int spi_freq = adc->spi_freq;int ret = -EINVAL ; -if (mask ==IIO_CHAN_INFO_OVERSAMPLING_RATIO ) { +switch (mask) { +case IIO_CHAN_INFO_OVERSAMPLING_RATIO : ret =stm32_dfsdm_set_osrs (fl,0 , val);if (!ret) adc->oversamp = val; + +return ret; + +case IIO_CHAN_INFO_SAMP_FREQ : +if (!val) +return -EINVAL ; +if (ch->src !=DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL ) + spi_freq = adc->dfsdm->spi_master_freq; + +if (spi_freq % val) +dev_warn (&indio_dev->dev, +"Sampling rate not accurate (%d)\n" , + spi_freq / (spi_freq / val)); + + ret =stm32_dfsdm_set_osrs (fl,0 , (spi_freq / val)); +if (ret <0 ) { +dev_err (&indio_dev->dev, +"Not able to find parameter that match!\n" ); +return ret; + } + adc->sample_freq = val; + +return 0 ; } -return ret; +return -EINVAL ; } static int stm32_dfsdm_read_raw(struct iio_dev *indio_dev,@@ -494,11 +834,22 @@ static int stm32_dfsdm_read_raw(struct iio_dev *indio_dev, *val = adc->oversamp;return IIO_VAL_INT ; + +case IIO_CHAN_INFO_SAMP_FREQ : + *val = adc->sample_freq; + +return IIO_VAL_INT ; }return -EINVAL ; } +static const struct iio_info stm32_dfsdm_info_audio = { + .hwfifo_set_watermark = stm32_dfsdm_set_watermark, + .read_raw = stm32_dfsdm_read_raw, + .write_raw = stm32_dfsdm_write_raw, +}; +static const struct iio_info stm32_dfsdm_info_adc = { .read_raw = stm32_dfsdm_read_raw, .write_raw = stm32_dfsdm_write_raw,@@ -531,6 +882,60 @@ static irqreturn_t stm32_dfsdm_irq(int irq, void *arg)return IRQ_HANDLED ; } +/* +* Define external info for SPI Frequency and audio sampling rate that can be +* configured by ASoC driver through consumer.h API +*/ +static const struct iio_chan_spec_ext_info dfsdm_adc_audio_ext_info[] = { +/* spi_clk_freq : clock freq on SPI/manchester bus used by channel */ + { + .name ="spi_clk_freq" , + .shared =IIO_SHARED_BY_TYPE , + .read = dfsdm_adc_audio_get_spiclk, + .write = dfsdm_adc_audio_set_spiclk, + }, + {}, +}; + +static int stm32_dfsdm_dma_request (struct iio_dev *indio_dev) +{ +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +struct dma_slave_config config; +int ret; + + adc->dma_chan =dma_request_slave_channel (&indio_dev->dev,"rx" ); +if (!adc->dma_chan) +return -EINVAL ; + + adc->rx_buf =dma_alloc_coherent (adc->dma_chan->device->dev, +DFSDM_DMA_BUFFER_SIZE , + &adc->dma_buf,GFP_KERNEL ); +if (!adc->rx_buf) { + ret = -ENOMEM ; + goto err_release; + } + +/* Configure DMA channel to read data register */ +memset (&config,0 ,sizeof (config)); + config.src_addr = (dma_addr_t )adc->dfsdm->phys_base; + config.src_addr +=DFSDM_RDATAR (adc->fl_id); + config.src_addr_width =DMA_SLAVE_BUSWIDTH_4_BYTES ; + + ret =dmaengine_slave_config (adc->dma_chan, &config); +if (ret) + goto err_free; + +return 0 ; + +err_free: +dma_free_coherent (adc->dma_chan->device->dev,DFSDM_DMA_BUFFER_SIZE , + adc->rx_buf, adc->dma_buf); +err_release: +dma_release_channel (adc->dma_chan); + +return ret; +} +static int stm32_dfsdm_adc_chan_init_one (struct iio_dev *indio_dev,struct iio_chan_spec *ch) {@@ -551,7 +956,12 @@ static int stm32_dfsdm_adc_chan_init_one(struct iio_dev *indio_dev, ch->info_mask_separate =BIT (IIO_CHAN_INFO_RAW ); ch->info_mask_shared_by_all =BIT (IIO_CHAN_INFO_OVERSAMPLING_RATIO ); - ch->scan_type.sign ='u' ; +if (adc->dev_data->type ==DFSDM_AUDIO ) { + ch->scan_type.sign ='s' ; + ch->ext_info = dfsdm_adc_audio_ext_info; + }else { + ch->scan_type.sign ='u' ; + } ch->scan_type.realbits =24 ; ch->scan_type.storagebits =32 ; adc->ch_id = ch->channel;@@ -560,6 +970,64 @@ static int stm32_dfsdm_adc_chan_init_one(struct iio_dev *indio_dev, &adc->dfsdm->ch_list[ch->channel]); } +static int stm32_dfsdm_audio_init (struct iio_dev *indio_dev) +{ +struct iio_chan_spec *ch; +struct stm32_dfsdm_adc *adc =iio_priv (indio_dev); +struct stm32_dfsdm_channel *d_ch; +int ret; + + ret =stm32_dfsdm_dma_request (indio_dev); +if (ret) { +dev_err (&indio_dev->dev,"DMA request failed\n" ); +return ret; + } + + indio_dev->modes |=INDIO_BUFFER_SOFTWARE ; + + ret =iio_triggered_buffer_setup (indio_dev, + &iio_pollfunc_store_time, +NULL , + &stm32_dfsdm_buffer_setup_ops); +if (ret) { +dev_err (&indio_dev->dev,"Buffer setup failed\n" ); + goto err_dma_disable; + } + + ch =devm_kzalloc (&indio_dev->dev,sizeof (*ch),GFP_KERNEL ); +if (!ch) +return -ENOMEM ; + + ch->scan_index =0 ; + ret =stm32_dfsdm_adc_chan_init_one (indio_dev, ch); +if (ret <0 ) { +dev_err (&indio_dev->dev,"channels init failed\n" ); + goto err_buffer_cleanup; + } + ch->info_mask_separate =BIT (IIO_CHAN_INFO_SAMP_FREQ ); + + d_ch = &adc->dfsdm->ch_list[adc->ch_id]; +if (d_ch->src !=DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL ) + adc->spi_freq = adc->dfsdm->spi_master_freq; + + indio_dev->num_channels =1 ; + indio_dev->channels = ch; + +return 0 ; + +err_buffer_cleanup: +iio_triggered_buffer_cleanup (indio_dev); + +err_dma_disable: +if (adc->dma_chan) { +dma_free_coherent (adc->dma_chan->device->dev, +DFSDM_DMA_BUFFER_SIZE , + adc->rx_buf, adc->dma_buf); +dma_release_channel (adc->dma_chan); + } +return ret; +} +static int stm32_dfsdm_adc_init (struct iio_dev *indio_dev) {struct iio_chan_spec *ch;@@ -612,11 +1080,20 @@ static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_adc_data = { .init = stm32_dfsdm_adc_init, }; +static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_audio_data = { + .type =DFSDM_AUDIO , + .init = stm32_dfsdm_audio_init, +}; +static const struct of_device_id stm32_dfsdm_adc_match[] = { { .compatible ="st,stm32-dfsdm-adc" , .data = &stm32h7_dfsdm_adc_data, }, + { + .compatible ="st,stm32-dfsdm-dmic" , + .data = &stm32h7_dfsdm_audio_data, + }, {} };@@ -667,8 +1144,13 @@ static int stm32_dfsdm_adc_probe(struct platform_device *pdev) name =devm_kzalloc (dev,sizeof ("dfsdm-adc0" ),GFP_KERNEL );if (!name)return -ENOMEM ; - iio->info = &stm32_dfsdm_info_adc; -snprintf (name,sizeof ("dfsdm-adc0" ),"dfsdm-adc%d" , adc->fl_id); +if (dev_data->type ==DFSDM_AUDIO ) { + iio->info = &stm32_dfsdm_info_audio; +snprintf (name,sizeof ("dfsdm-pdm0" ),"dfsdm-pdm%d" , adc->fl_id); + }else { + iio->info = &stm32_dfsdm_info_adc; +snprintf (name,sizeof ("dfsdm-adc0" ),"dfsdm-adc%d" , adc->fl_id); + } iio->name = name;/* @@ -700,7 +1182,10 @@ static int stm32_dfsdm_adc_probe(struct platform_device *pdev)if (ret <0 )return ret; -return iio_device_register (iio); +iio_device_register (iio); +if (dev_data->type ==DFSDM_AUDIO ) +return devm_of_platform_populate (&pdev->dev); +return 0 ; }static int stm32_dfsdm_adc_remove (struct platform_device *pdev)@@ -709,7 +1194,14 @@ static int stm32_dfsdm_adc_remove(struct platform_device *pdev)struct iio_dev *indio_dev =iio_priv_to_dev (adc);iio_device_unregister (indio_dev); - +if (indio_dev->pollfunc) +iio_triggered_buffer_cleanup (indio_dev); +if (adc->dma_chan) { +dma_free_coherent (adc->dma_chan->device->dev, +DFSDM_DMA_BUFFER_SIZE , + adc->rx_buf, adc->dma_buf); +dma_release_channel (adc->dma_chan); + }return 0 ; }include/linux/iio/adc/stm32-dfsdm-adc.h new file +10
-0@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* This file discribe the STM32 DFSDM IIO driver API for audio part +* +* Copyright (C) 2017, STMicroelectronics - All Rights Reserved +* Author(s): Arnaud Pouliquen <[email protected]>. +*/ + +#ifndef STM32_DFSDM_ADC_H +#define STM32_DFSDM_ADC_H Quoted 9 lines
include/linux/iio/adc/stm32-dfsdm-adc.h line 18
Quoted 8 lines
include/linux/iio/adc/stm32-dfsdm-adc.h new file +8
-0+ +int stm32_dfsdm_get_buff_cb (struct iio_dev *iio_dev, +int (*cb)(const void *data,size_t size, +void *private), +void *private); +int stm32_dfsdm_release_buff_cb (struct iio_dev *iio_dev); + +#endif _______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v8 11/13 IIO: consumer: allow to set buffer sizes
In reply to a message not in the archiveAdd iio consumer API to set buffer size and watermark according to sysfs API.
--- drivers/iio/buffer/industrialio-buffer-cb.c | 11 +++++++++++ include/linux/iio/consumer.h | 11 +++++++++++ 2 files changed, 22 insertions(+)
drivers/iio/buffer/industrialio-buffer-cb.c +11 -0
@@ -104,6 +104,17 @@ struct iio_cb_buffer *iio_channel_get_all_cb(struct device *dev, }EXPORT_SYMBOL_GPL (iio_channel_get_all_cb); +int iio_channel_cb_set_buffer_watermark (struct iio_cb_buffer *cb_buff, +size_t watermark) +{ +if (!watermark) +return -EINVAL ; + cb_buff->buffer.watermark = watermark; + +return 0 ; +} +EXPORT_SYMBOL_GPL (iio_channel_cb_set_buffer_watermark); +int iio_channel_start_all_cb (struct iio_cb_buffer *cb_buff) { return iio_update_buffers(cb_buff->indio_dev, &cb_buff->buffer,
include/linux/iio/consumer.h +11 -0
@@ -134,6 +134,17 @@ struct iio_cb_buffer *iio_channel_get_all_cb(struct device *dev, void *private), void *private);/** +* iio_channel_cb_set_buffer_watermark() - set the buffer watermark. +* @cb_buffer: The callback buffer from whom we want the channel +* information. +* @watermark: buffer watermark in bytes. +* +* This function allows to configure the buffer watermark. +*/ +int iio_channel_cb_set_buffer_watermark (struct iio_cb_buffer *cb_buffer, +size_t watermark); + +/** * iio_channel_release_all_cb() - release and unregister the callback. * @cb_buffer: The callback buffer that was allocated. */
Signature · 1 line
2.7.4
PATCH v8 12/13 ASoC: add bindings for stm32 DFSDM filter
In reply to a message not in the archiveAdd bindings that describes audio settings to support Digital Filter for pulse density modulation(PDM) microphone.
--- .../devicetree/bindings/sound/st,stm32-adfsdm.txt | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Documentation/devicetree/bindings/sound/st,stm32-adfsdm.txt
Documentation/devicetree/bindings/sound/st,stm32-adfsdm.txt new file +63 -0
@@ -0,0 +1,63 @@ +STMicroelectronics Audio Digital Filter Sigma Delta modulators(DFSDM) + +The DFSDM allows PDM microphones capture through SPI interface. The Audio +interface is seems as a sub block of the DFSDM device. +For details on DFSDM bindings refer to ../iio/adc/st,stm32-dfsdm-adc.txt + +Required properties: + - compatible: "st,stm32h7-dfsdm-dai". + + - #sound-dai-cells : Must be equal to 0 + + - io-channels : phandle to iio dfsdm instance node. + +Example of a sound card using audio DFSDM node. + + sound_card { + compatible = "audio-graph-card"; + + dais = <&cpu_port>; + }; + + dfsdm: dfsdm@40017000 { + compatible = "st,stm32h7-dfsdm"; + reg = <0x40017000 0x400>; + clocks = <&rcc DFSDM1_CK>; + clock-names = "dfsdm"; + #interrupt-cells = <1>; + #address-cells = <1>; + #size-cells = <0>; + + dfsdm_adc0: filter@0 { + compatible = "st,stm32-dfsdm-dmic"; + reg = <0>; + interrupts = <110>; + dmas = <&dmamux1 101 0x400 0x00>; + dma-names = "rx"; + st,adc-channels = <1>; + st,adc-channel-names = "dmic0"; + st,adc-channel-types = "SPI_R"; + st,adc-channel-clk-src = "CLKOUT"; + st,filter-order = <5>; + + dfsdm_dai0: dfsdm-dai { + compatible = "st,stm32h7-dfsdm-dai"; + #sound-dai-cells = <0>; + io-channels = <&dfsdm_adc0 0>; + cpu_port: port { + dfsdm_endpoint: endpoint { + remote-endpoint = <&dmic0_endpoint>; + }; + }; + }; + }; + + dmic0: dmic@0 { + compatible = "dmic-codec"; + #sound-dai-cells = <0>; + port { + dmic0_endpoint: endpoint { + remote-endpoint = <&dfsdm_endpoint>; + }; + }; + };
Signature · 1 line
2.7.4
PATCH v8 5/13 IIO: Add DT bindings for sigma delta adc modulator
In reply to Arnaud PouliquenAdd documentation of device tree bindings to support sigma delta modulator in IIO framework.
--- .../devicetree/bindings/iio/adc/sigma-delta-modulator.txt | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/sigma-delta-modulator.txt
Documentation/devicetree/bindings/iio/adc/sigma-delta-modulator.txt new file +13 -0
@@ -0,0 +1,13 @@ +Device-Tree bindings for sigma delta modulator + +Required properties: +- compatible: should be "ads1201", "sd-modulator". "sd-modulator" can be use + as a generic SD modulator if modulator not specified in compatible list. +- #io-channel-cells = <1>: See the IIO bindings section "IIO consumers". + +Example node: + + ads1202: adc at 0 { + compatible = "sd-modulator"; + #io-channel-cells = <1>; + };
Signature · 1 line
2.7.4
PATCH v8 6/13 IIO: ADC: add sigma delta modulator support
In reply to Arnaud PouliquenAdd generic driver to support sigma delta modulators. Typically, this device is hardware connected to an IIO device in charge of the conversion. Devices are bonded through the hardware consumer API.
--- drivers/iio/adc/Kconfig | 12 +++++++ drivers/iio/adc/Makefile | 1 + drivers/iio/adc/sd_adc_modulator.c | 68 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 drivers/iio/adc/sd_adc_modulator.c
drivers/iio/adc/Kconfig +12 -0
@@ -626,6 +626,18 @@ config SPEAR_ADC To compile this driver as a module, choose M here: the module will be called spear_adc. +config SD_ADC_MODULATOR + tristate "Generic sigma delta modulator" + depends on OF + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER + help + Select this option to enables sigma delta modulator. This driver can + support generic sigma delta modulators. + + This driver can also be built as a module. If so, the module + will be called sd_adc_modulator. + config STM32_ADC_CORE tristate "STMicroelectronics STM32 adc core" depends on ARCH_STM32 || COMPILE_TEST
drivers/iio/adc/Makefile +1 -0
@@ -81,3 +81,4 @@ obj-$(CONFIG_VF610_ADC) += vf610_adc.o obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o xilinx-xadc-y := xilinx-xadc-core.o xilinx-xadc-events.o obj-$(CONFIG_XILINX_XADC) += xilinx-xadc.o +obj-$(CONFIG_SD_ADC_MODULATOR) += sd_adc_modulator.o
drivers/iio/adc/sd_adc_modulator.c new file +68 -0
68 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v8 13/13 ASoC: stm32: add DFSDM DAI support
In reply to Arnaud PouliquenAdd driver to handle DAI interface for PDM microphones connected to Digital Filter for Sigma Delta Modulators IP.
---
V7 to V8
- Typo fix
- Function return optimizations.
sound/soc/stm/Kconfig | 11 ++ sound/soc/stm/Makefile | 3 + sound/soc/stm/stm32_adfsdm.c | 347 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 361 insertions(+) create mode 100644 sound/soc/stm/stm32_adfsdm.c
sound/soc/stm/Kconfig +11 -0
@@ -28,4 +28,15 @@ config SND_SOC_STM32_SPDIFRX help Say Y if you want to enable S/PDIF capture for STM32 +config SND_SOC_STM32_DFSDM + tristate "SoC Audio support for STM32 DFSDM" + depends on (ARCH_STM32 && OF && STM32_DFSDM_ADC) || COMPILE_TEST + depends on SND_SOC + select SND_SOC_GENERIC_DMAENGINE_PCM + select SND_SOC_DMIC + select IIO_BUFFER_CB + help + Select this option to enable the STM32 Digital Filter + for Sigma Delta Modulators (DFSDM) driver used + in various STM32 series for digital microphone capture. endmenu
sound/soc/stm/Makefile +3 -0
@@ -12,3 +12,6 @@ obj-$(CONFIG_SND_SOC_STM32_I2S) += snd-soc-stm32-i2s.o # SPDIFRX snd-soc-stm32-spdifrx-objs := stm32_spdifrx.o obj-$(CONFIG_SND_SOC_STM32_SPDIFRX) += snd-soc-stm32-spdifrx.o + +#DFSDM +obj-$(CONFIG_SND_SOC_STM32_DFSDM) += stm32_adfsdm.o
sound/soc/stm/stm32_adfsdm.c new file +347 -0
347 lines not on this page · raw message
Signature · 1 line
2.7.4
A message that is not in the archive (sent off-list or not yet received): 1512987524-2901-14-git-send-email-arnaud.pouliquen-qxv4g6HH51o@public.gmane.org
Applied "ASoC: stm32: add DFSDM DAI support" to the asoc tree
The patch
ASoC: stm32: add DFSDM DAI support
has been applied to the asoc tree at
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git
All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark
Quoted 1 line
From
55da094824c4ef1d50bc591733d79448d00265bbMon Sep 17 00:00:00 2001
From: Arnaud Pouliquen <[email protected]>
Date: Wed, 10 Jan 2018 11:13:15 +0100
Subject: [PATCH] ASoC: stm32: add DFSDM DAI support
Add driver to handle DAI interface for PDM microphones connected to Digital Filter for Sigma Delta Modulators IP.
--- sound/soc/stm/Kconfig | 11 ++ sound/soc/stm/Makefile | 3 + sound/soc/stm/stm32_adfsdm.c | 347 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 361 insertions(+) create mode 100644 sound/soc/stm/stm32_adfsdm.c
sound/soc/stm/Kconfig +11 -0
@@ -28,4 +28,15 @@ config SND_SOC_STM32_SPDIFRX help Say Y if you want to enable S/PDIF capture for STM32 +config SND_SOC_STM32_DFSDM + tristate "SoC Audio support for STM32 DFSDM" + depends on (ARCH_STM32 && OF && STM32_DFSDM_ADC) || COMPILE_TEST + depends on SND_SOC + select SND_SOC_GENERIC_DMAENGINE_PCM + select SND_SOC_DMIC + select IIO_BUFFER_CB + help + Select this option to enable the STM32 Digital Filter + for Sigma Delta Modulators (DFSDM) driver used + in various STM32 series for digital microphone capture. endmenu
sound/soc/stm/Makefile +3 -0
@@ -13,3 +13,6 @@ obj-$(CONFIG_SND_SOC_STM32_I2S) += snd-soc-stm32-i2s.o # SPDIFRX snd-soc-stm32-spdifrx-objs := stm32_spdifrx.o obj-$(CONFIG_SND_SOC_STM32_SPDIFRX) += snd-soc-stm32-spdifrx.o + +#DFSDM +obj-$(CONFIG_SND_SOC_STM32_DFSDM) += stm32_adfsdm.o
sound/soc/stm/stm32_adfsdm.c new file +347 -0
347 lines not on this page · raw message
Signature · 6 lines
2.15.1 -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html