Extend the MDF filter driver to support digital microphones and provide the interface required by an audio consumer.
Signed-off-by: Olivier Moysan <[email protected]> --- drivers/iio/adc/stm32-mdf-adc.c | 164 +++++++++++++++++++++++++++++++++- include/linux/iio/adc/stm32-mdf-adc.h | 19 ++++ 2 files changed, 182 insertions(+), 1 deletion(-) diff --git a/drivers/iio/adc/stm32-mdf-adc.c b/drivers/iio/adc/stm32-mdf-adc.c index 97099b4adffc..27c47bef71a4 100644 --- a/drivers/iio/adc/stm32-mdf-adc.c +++ b/drivers/iio/adc/stm32-mdf-adc.c @@ -9,6 +9,7 @@ #include <linux/clk-provider.h> #include <linux/dmaengine.h> #include <linux/dma-mapping.h> +#include <linux/iio/adc/stm32-mdf-adc.h> #include <linux/iio/backend.h> #include <linux/iio/buffer.h> #include <linux/iio/hw-consumer.h> @@ -1128,6 +1129,42 @@ static const struct iio_buffer_setup_ops stm32_mdf_buffer_setup_ops = { .predisable = &stm32_mdf_adc_predisable, }; +/** + * stm32_mdf_get_sub_channels_nb() - Get the number of MDF sub-channels + * @indio_dev: MDF IIO device + * + * Return: 0 on success, or a negative error code on failure. + */ +unsigned int stm32_mdf_get_sub_channels_nb(struct iio_dev *indio_dev) +{ + struct stm32_mdf_adc *adc = iio_priv(indio_dev); + + if (MDF_IS_FILTER0(adc) && adc->mdf->nb_interleave) + return adc->mdf->nb_interleave; + + return 1; +} +EXPORT_SYMBOL_GPL(stm32_mdf_get_sub_channels_nb); + +static ssize_t stm32_mdf_adc_audio_get_channels(struct iio_dev *indio_dev, uintptr_t priv, + const struct iio_chan_spec *chan, char *buf) +{ + return sysfs_emit(buf, "%u\n", stm32_mdf_get_sub_channels_nb(indio_dev)); +} + +/* + * IIO channel extended info used by the audio device IIO channel consumer. + * sub_channels_nb: provides the number of audio channels associated to the IIO channel. + */ +static const struct iio_chan_spec_ext_info stm32_mdf_adc_audio_ext_info[] = { + { + .name = "sub_channels_nb", + .shared = IIO_SHARED_BY_TYPE, + .read = stm32_mdf_adc_audio_get_channels, + }, + {}, +}; + static void stm32_mdf_dma_release(struct iio_dev *indio_dev) { struct stm32_mdf_adc *adc = iio_priv(indio_dev); @@ -1240,6 +1277,9 @@ static int stm32_mdf_adc_chan_init_one(struct iio_dev *indio_dev, struct fwnode_ ch->scan_type.shift = 8; } + if (adc->dev_data->type == STM32_MDF_AUDIO) + ch->ext_info = stm32_mdf_adc_audio_ext_info; + ch->info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ); ch->scan_type.sign = 's'; ch->scan_type.realbits = STM32_MDF_DATA_RES; @@ -1474,6 +1514,13 @@ static int stm32_mdf_adc_read_label(struct iio_dev *indio_dev, const struct iio_ return sysfs_emit(label, "%s\n", name); } +static const struct iio_info stm32_mdf_info_audio = { + .hwfifo_set_watermark = stm32_mdf_set_watermark, + .write_raw = stm32_mdf_adc_write_raw, + .read_raw = stm32_mdf_adc_read_raw, + .read_label = stm32_mdf_adc_read_label, +}; + static const struct iio_info stm32_mdf_info_adc = { .hwfifo_set_watermark = stm32_mdf_set_watermark, .write_raw = stm32_mdf_adc_write_raw, @@ -1530,6 +1577,38 @@ static irqreturn_t stm32_mdf_irq(int irq, void *arg) return IRQ_HANDLED; } +static int stm32_mdf_audio_init(struct device *dev, struct iio_dev *indio_dev) +{ + struct stm32_mdf_adc *adc = iio_priv(indio_dev); + struct iio_chan_spec *ch; + int ret; + + ch = devm_kzalloc(&indio_dev->dev, sizeof(*ch), GFP_KERNEL); + if (!ch) + return -ENOMEM; + + adc->channels = devm_kzalloc(&indio_dev->dev, sizeof(*adc->channels), GFP_KERNEL); + if (!adc->channels) + return -ENOMEM; + + ret = stm32_mdf_adc_chan_init(indio_dev, ch); + if (ret < 0) + return dev_err_probe(dev, ret, "Channels init failed\n"); + + indio_dev->num_channels = 1; + indio_dev->channels = ch; + + ret = stm32_mdf_dma_request(dev, indio_dev); + if (ret) + return dev_err_probe(dev, ret, "Failed to get DMA\n"); + + ret = stm32_mdf_adc_filter_set_mode(adc, true); + if (ret) + stm32_mdf_dma_release(indio_dev); + + return ret; +} + static int stm32_mdf_adc_init(struct device *dev, struct iio_dev *indio_dev) { struct stm32_mdf_adc *adc = iio_priv(indio_dev); @@ -1599,11 +1678,77 @@ static const struct stm32_mdf_dev_data stm32h7_mdf_adc_data = { .init = stm32_mdf_adc_init, }; +static const struct stm32_mdf_dev_data stm32_mdf_audio_data = { + .type = STM32_MDF_AUDIO, + .init = stm32_mdf_audio_init, +}; + +/** + * stm32_mdf_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. + * + * Return: 0 on success, or a negative error code on failure. + */ +int stm32_mdf_get_buff_cb(struct iio_dev *iio_dev, + int (*cb)(const void *data, size_t size, void *private), void *private) +{ + struct stm32_mdf_adc *adc; + + if (!iio_dev) + return -EINVAL; + adc = iio_priv(iio_dev); + + if (!adc) + return -EINVAL; + + adc->cb = cb; + adc->cb_priv = private; + + return 0; +} +EXPORT_SYMBOL_GPL(stm32_mdf_get_buff_cb); + +/** + * stm32_mdf_release_buff_cb() - Unregister the buffer callback + * + * @iio_dev: Handle to IIO device. + * + * Return: 0 on success, or a negative error code on failure. + */ +int stm32_mdf_release_buff_cb(struct iio_dev *iio_dev) +{ + struct stm32_mdf_adc *adc; + + if (!iio_dev) + return -EINVAL; + adc = iio_priv(iio_dev); + + if (!adc) + return -EINVAL; + + adc->cb = NULL; + adc->cb_priv = NULL; + + return 0; +} +EXPORT_SYMBOL_GPL(stm32_mdf_release_buff_cb); + static const struct of_device_id stm32_mdf_adc_match[] = { { .compatible = "st,stm32mp25-mdf-adc", .data = &stm32h7_mdf_adc_data, }, + { + .compatible = "st,stm32mp25-mdf-dmic", + .data = &stm32_mdf_audio_data, + }, {} }; MODULE_DEVICE_TABLE(of, stm32_mdf_adc_match); @@ -1788,6 +1933,7 @@ static int stm32_mdf_adc_parse_of(struct platform_device *pdev, struct stm32_mdf static int stm32_mdf_adc_probe(struct platform_device *pdev) { + struct device_node *node = pdev->dev.of_node; struct device *dev = &pdev->dev; struct stm32_mdf_adc *adc; const struct stm32_mdf_dev_data *dev_data; @@ -1830,7 +1976,10 @@ static int stm32_mdf_adc_probe(struct platform_device *pdev) if (ret < 0) return ret; - iio->info = &stm32_mdf_info_adc; + if (dev_data->type == STM32_MDF_AUDIO) + iio->info = &stm32_mdf_info_audio; + else + iio->info = &stm32_mdf_info_adc; iio->name = dev_name(&pdev->dev); adc->dev = dev; @@ -1847,8 +1996,19 @@ static int stm32_mdf_adc_probe(struct platform_device *pdev) } } + if (dev_data->type == STM32_MDF_AUDIO) { + ret = of_platform_populate(node, NULL, NULL, dev); + if (ret < 0) { + dev_err_probe(dev, ret, "Failed to find an audio DAI\n"); + goto err_unregister; + } + } + return 0; +err_unregister: + if (!MDF_IS_INTERLEAVED_FILT_NOT_0(adc)) + iio_device_unregister(iio); err_cleanup: stm32_mdf_dma_release(iio); err_clean_list: @@ -1862,6 +2022,8 @@ static void stm32_mdf_adc_remove(struct platform_device *pdev) struct iio_dev *indio_dev = platform_get_drvdata(pdev); struct stm32_mdf_adc *adc = iio_priv(indio_dev); + if (adc->dev_data->type == STM32_MDF_AUDIO) + of_platform_depopulate(&pdev->dev); if (!MDF_IS_INTERLEAVED_FILT_NOT_0(adc)) iio_device_unregister(indio_dev); stm32_mdf_dma_release(indio_dev); diff --git a/include/linux/iio/adc/stm32-mdf-adc.h b/include/linux/iio/adc/stm32-mdf-adc.h new file mode 100644 index 000000000000..95e3f4afdb78 --- /dev/null +++ b/include/linux/iio/adc/stm32-mdf-adc.h @@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * This file describe the STM32 MDF IIO driver API for audio part + * + * Copyright (C) 2023, STMicroelectronics. + * Author(s): Olivier Moysan <[email protected]>. + */ + +#ifndef STM32_MDF_ADC_H +#define STM32_MDF_ADC_H + +#include <linux/iio/iio.h> + +int stm32_mdf_get_buff_cb(struct iio_dev *iio_dev, + int (*cb)(const void *data, size_t size, void *private), void *private); +int stm32_mdf_release_buff_cb(struct iio_dev *iio_dev); +unsigned int stm32_mdf_get_sub_channels_nb(struct iio_dev *iio_dev); + +#endif -- 2.43.0
