2023-04-21 12:42:43

by Herve Codina

[permalink] [raw]
Subject: [PATCH 0/4] Add support for IIO devices in ASoC

Several weeks ago, I sent a series [1] for adding a potentiometer as an
auxiliary device in ASoC. The feedback was that the potentiometer should
be directly handled in IIO (as other potentiometers) and something more
generic should be present in ASoC in order to have a binding to import
some IIO devices into sound cards.

The series related to the IIO potentiometer device is already under
review [2].

This series introduces simple-iio-aux. Its goal is to offer the binding
between IIO and ASoC.
It exposes attached IIO devices as ASoC auxiliary devices and allows to
control them through mixer controls.

On my system, the IIO device is a potentiometer and it is present in an
amplifier design present in the audio path.

Best regards,
Hervé

[1] https://lore.kernel.org/linux-kernel/[email protected]/
[2] https://lore.kernel.org/linux-kernel/[email protected]/

Herve Codina (4):
dt-bindings: sound: Add simple-iio-aux
iio: inkern: Add a helper to query an available minimum raw value
ASoC: soc-dapm.h: Add a helper to build a DAPM widget dynamically
ASoC: codecs: Add support for the generic IIO auxiliary devices

.../bindings/sound/simple-iio-aux.yaml | 65 ++++
drivers/iio/inkern.c | 67 ++++
include/linux/iio/consumer.h | 11 +
include/sound/soc-dapm.h | 12 +-
sound/soc/codecs/Kconfig | 12 +
sound/soc/codecs/Makefile | 2 +
sound/soc/codecs/simple-iio-aux.c | 307 ++++++++++++++++++
7 files changed, 475 insertions(+), 1 deletion(-)
create mode 100644 Documentation/devicetree/bindings/sound/simple-iio-aux.yaml
create mode 100644 sound/soc/codecs/simple-iio-aux.c

--
2.39.2


2023-04-21 12:42:49

by Herve Codina

[permalink] [raw]
Subject: [PATCH 1/4] dt-bindings: sound: Add simple-iio-aux

Industrial I/O devices can be present in the audio path.
These devices needs to be viewed as audio components in order to be
fully integrated in the audio path.

simple-iio-aux allows to consider these Industrial I/O devices as
auxliary audio devices.

Signed-off-by: Herve Codina <[email protected]>
---
.../bindings/sound/simple-iio-aux.yaml | 65 +++++++++++++++++++
1 file changed, 65 insertions(+)
create mode 100644 Documentation/devicetree/bindings/sound/simple-iio-aux.yaml

diff --git a/Documentation/devicetree/bindings/sound/simple-iio-aux.yaml b/Documentation/devicetree/bindings/sound/simple-iio-aux.yaml
new file mode 100644
index 000000000000..fab128fce4fc
--- /dev/null
+++ b/Documentation/devicetree/bindings/sound/simple-iio-aux.yaml
@@ -0,0 +1,65 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/sound/simple-iio-aux.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Simple IIO auxiliary
+
+maintainers:
+ - Herve Codina <[email protected]>
+
+description: |
+ Auxiliary device based on Industrial I/O device channels
+
+allOf:
+ - $ref: /schemas/iio/iio-consumer.yaml
+ - $ref: dai-common.yaml#
+
+properties:
+ compatible:
+ const: simple-iio-aux
+
+ io-channels:
+ description:
+ Industrial I/O device channels used
+
+ io-channel-names:
+ description:
+ Industrial I/O channel names related to io-channels.
+ These names are used to provides sound controls, widgets and routes names.
+
+ invert:
+ $ref: /schemas/types.yaml#/definitions/uint32-array
+ description: |
+ A list of 0/1 flags defining whether or not the related channel is
+ inverted
+ items:
+ enum: [0, 1]
+ default: 0
+ description: |
+ Invert the sound control value compared to the IIO channel raw value.
+ - 1: The related sound control value is inverted meaning that the
+ minimum sound control value correspond to the maximum IIO channel
+ raw value and the maximum sound control value correspond to the
+ minimum IIO channel raw value.
+ - 0: The related sound control value is not inverted meaning that the
+ minimum (resp maximum) sound control value correspond to the
+ minimum (resp maximum) IIO channel raw value.
+
+required:
+ - compatible
+ - io-channels
+ - io-channel-names
+
+unevaluatedProperties: false
+
+examples:
+ - |
+ aux {
+ compatible = "simple-iio-aux";
+ io-channels = <&iio 0>, <&iio 1>, <&iio 2>, <&iio 3>;
+ io-channel-names = "CH0", "CH1", "CH2", "CH3";
+ /* Invert CH1 and CH2 */
+ invert = <0 1 1>;
+ };
--
2.39.2

2023-04-21 12:42:57

by Herve Codina

[permalink] [raw]
Subject: [PATCH 2/4] iio: inkern: Add a helper to query an available minimum raw value

A helper, iio_read_max_channel_raw() exists to read the available
maximum raw value of a channel but nothing similar exists to read the
available minimum raw value.

This new helper, iio_read_min_channel_raw(), fills the hole and can be
used for reading the available minimum raw value of a channel.
It is fully based on the existing iio_read_max_channel_raw().

Signed-off-by: Herve Codina <[email protected]>
---
drivers/iio/inkern.c | 67 ++++++++++++++++++++++++++++++++++++
include/linux/iio/consumer.h | 11 ++++++
2 files changed, 78 insertions(+)

diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
index 872fd5c24147..914fc69c718a 100644
--- a/drivers/iio/inkern.c
+++ b/drivers/iio/inkern.c
@@ -912,6 +912,73 @@ int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
}
EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);

+static int iio_channel_read_min(struct iio_channel *chan,
+ int *val, int *val2, int *type,
+ enum iio_chan_info_enum info)
+{
+ int unused;
+ const int *vals;
+ int length;
+ int ret;
+
+ if (!val2)
+ val2 = &unused;
+
+ ret = iio_channel_read_avail(chan, &vals, type, &length, info);
+ switch (ret) {
+ case IIO_AVAIL_RANGE:
+ switch (*type) {
+ case IIO_VAL_INT:
+ *val = vals[0];
+ break;
+ default:
+ *val = vals[0];
+ *val2 = vals[1];
+ }
+ return 0;
+
+ case IIO_AVAIL_LIST:
+ if (length <= 0)
+ return -EINVAL;
+ switch (*type) {
+ case IIO_VAL_INT:
+ *val = vals[--length];
+ while (length) {
+ if (vals[--length] < *val)
+ *val = vals[length];
+ }
+ break;
+ default:
+ /* FIXME: learn about min for other iio values */
+ return -EINVAL;
+ }
+ return 0;
+
+ default:
+ return ret;
+ }
+}
+
+int iio_read_min_channel_raw(struct iio_channel *chan, int *val)
+{
+ struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
+ int ret;
+ int type;
+
+ mutex_lock(&iio_dev_opaque->info_exist_lock);
+ if (!chan->indio_dev->info) {
+ ret = -ENODEV;
+ goto err_unlock;
+ }
+
+ ret = iio_channel_read_min(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
+err_unlock:
+ mutex_unlock(&iio_dev_opaque->info_exist_lock);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(iio_read_min_channel_raw);
+
int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
{
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
diff --git a/include/linux/iio/consumer.h b/include/linux/iio/consumer.h
index 6802596b017c..956120d8b5a3 100644
--- a/include/linux/iio/consumer.h
+++ b/include/linux/iio/consumer.h
@@ -297,6 +297,17 @@ int iio_write_channel_raw(struct iio_channel *chan, int val);
*/
int iio_read_max_channel_raw(struct iio_channel *chan, int *val);

+/**
+ * iio_read_min_channel_raw() - read minimum available raw value from a given
+ * channel, i.e. the minimum possible value.
+ * @chan: The channel being queried.
+ * @val: Value read back.
+ *
+ * Note raw reads from iio channels are in adc counts and hence
+ * scale will need to be applied if standard units are required.
+ */
+int iio_read_min_channel_raw(struct iio_channel *chan, int *val);
+
/**
* iio_read_avail_channel_raw() - read available raw values from a given channel
* @chan: The channel being queried.
--
2.39.2

2023-04-21 12:43:13

by Herve Codina

[permalink] [raw]
Subject: [PATCH 3/4] ASoC: soc-dapm.h: Add a helper to build a DAPM widget dynamically

The SND_SOC_DAPM_* helpers family are used to build widgets array in a
static way.

Introduce SND_SOC_DAPM_WIDGET() in order to use the SND_SOC_DAPM_*
helpers family in a dynamic way. The different SND_SOC_DAPM_* parameters
can be computed by the code and the widget can be built based on this
parameter computation.
For instance:
static int create_widget(char *input_name)
{
struct snd_soc_dapm_widget widget;
char name*;
...
name = input_name;
if (!name)
name = "default";

widget = SND_SOC_DAPM_WIDGET(SND_SOC_DAPM_INPUT(name));
...
}

Signed-off-by: Herve Codina <[email protected]>
---
include/sound/soc-dapm.h | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/include/sound/soc-dapm.h b/include/sound/soc-dapm.h
index 64915ebd641e..d6bb97fba8c9 100644
--- a/include/sound/soc-dapm.h
+++ b/include/sound/soc-dapm.h
@@ -276,7 +276,17 @@ struct soc_enum;
.reg = SND_SOC_NOPM, .event = dapm_pinctrl_event, \
.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD }

-
+/*
+ * Helper to create a widget 'dynamically'
+ * This can be used with any of the SND_SOC_DAPM_* widget helper.
+ * For instance:
+ * struct snd_soc_dapm_widget widget;
+ * ...
+ * widget = SND_SOC_DAPM_WIDGET(SND_SOC_DAPM_INPUT(input_name));
+ */
+#define SND_SOC_DAPM_WIDGET(_widget)({ \
+ struct snd_soc_dapm_widget _w[1] = { _widget }; \
+ _w[0]; })

/* dapm kcontrol types */
#define SOC_DAPM_DOUBLE(xname, reg, lshift, rshift, max, invert) \
--
2.39.2

2023-04-21 12:51:11

by Herve Codina

[permalink] [raw]
Subject: [PATCH 4/4] ASoC: codecs: Add support for the generic IIO auxiliary devices

Industrial I/O devices can be present in the audio path.
These devices needs to be used as audio components in order to be fully
integrated in the audio path.

This support allows to consider these Industrial I/O devices as auxliary
audio devices and allows to control them using mixer controls.

Signed-off-by: Herve Codina <[email protected]>
---
sound/soc/codecs/Kconfig | 12 ++
sound/soc/codecs/Makefile | 2 +
sound/soc/codecs/simple-iio-aux.c | 307 ++++++++++++++++++++++++++++++
3 files changed, 321 insertions(+)
create mode 100644 sound/soc/codecs/simple-iio-aux.c

diff --git a/sound/soc/codecs/Kconfig b/sound/soc/codecs/Kconfig
index 4f78da914fc7..ee87e0125bfd 100644
--- a/sound/soc/codecs/Kconfig
+++ b/sound/soc/codecs/Kconfig
@@ -211,6 +211,7 @@ config SND_SOC_ALL_CODECS
imply SND_SOC_SGTL5000
imply SND_SOC_SI476X
imply SND_SOC_SIMPLE_AMPLIFIER
+ imply SND_SOC_SIMPLE_IIO_AUX
imply SND_SOC_SIMPLE_MUX
imply SND_SOC_SMA1303
imply SND_SOC_SPDIF
@@ -1555,6 +1556,17 @@ config SND_SOC_SIGMADSP_REGMAP
config SND_SOC_SIMPLE_AMPLIFIER
tristate "Simple Audio Amplifier"

+config SND_SOC_SIMPLE_IIO_AUX
+ tristate "Simple IIO Auxiliary device"
+ depends on IIO
+ help
+ Enable support for Industrial I/O devices as audio auxiliary devices.
+ This allows to have an IIO device present in the audio path and
+ controlled using mixer controls.
+
+ To compile this driver as a module, choose M here: the module
+ will be called snd-soc-simple-iio-aux.
+
config SND_SOC_SIMPLE_MUX
tristate "Simple Audio Mux"
depends on GPIOLIB
diff --git a/sound/soc/codecs/Makefile b/sound/soc/codecs/Makefile
index 11bd66d46f7b..f2b1ee22b57c 100644
--- a/sound/soc/codecs/Makefile
+++ b/sound/soc/codecs/Makefile
@@ -359,6 +359,7 @@ snd-soc-zl38060-objs := zl38060.o
snd-soc-max9877-objs := max9877.o
snd-soc-max98504-objs := max98504.o
snd-soc-simple-amplifier-objs := simple-amplifier.o
+snd-soc-simple-iio-aux-objs := simple-iio-aux.o
snd-soc-tpa6130a2-objs := tpa6130a2.o
snd-soc-tas2552-objs := tas2552.o
snd-soc-tas2562-objs := tas2562.o
@@ -729,6 +730,7 @@ obj-$(CONFIG_SND_SOC_ZL38060) += snd-soc-zl38060.o
obj-$(CONFIG_SND_SOC_MAX9877) += snd-soc-max9877.o
obj-$(CONFIG_SND_SOC_MAX98504) += snd-soc-max98504.o
obj-$(CONFIG_SND_SOC_SIMPLE_AMPLIFIER) += snd-soc-simple-amplifier.o
+obj-$(CONFIG_SND_SOC_SIMPLE_IIO_AUX) += snd-soc-simple-iio-aux.o
obj-$(CONFIG_SND_SOC_TPA6130A2) += snd-soc-tpa6130a2.o
obj-$(CONFIG_SND_SOC_LPASS_MACRO_COMMON) += snd-soc-lpass-macro-common.o
obj-$(CONFIG_SND_SOC_LPASS_WSA_MACRO) += snd-soc-lpass-wsa-macro.o
diff --git a/sound/soc/codecs/simple-iio-aux.c b/sound/soc/codecs/simple-iio-aux.c
new file mode 100644
index 000000000000..77f7fb11e416
--- /dev/null
+++ b/sound/soc/codecs/simple-iio-aux.c
@@ -0,0 +1,307 @@
+// SPDX-License-Identifier: GPL-2.0-only
+//
+// simple-iio-aux.c -- ALSA SoC glue to use IIO devices as audio components
+//
+// Copyright 2023 CS GROUP France
+//
+// Author: Herve Codina <[email protected]>
+
+#include <linux/iio/consumer.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <sound/soc.h>
+#include <sound/tlv.h>
+
+struct simple_iio_aux_chan {
+ struct iio_channel *iio_chan;
+ const char *name;
+ bool is_inverted;
+ int max;
+ int min;
+};
+
+struct simple_iio_aux {
+ struct device *dev;
+ struct simple_iio_aux_chan *chans;
+ unsigned int num_chans;
+};
+
+static int simple_iio_aux_info_volsw(struct snd_kcontrol *kcontrol,
+ struct snd_ctl_elem_info *uinfo)
+{
+ struct simple_iio_aux_chan *chan =
+ (struct simple_iio_aux_chan *)kcontrol->private_value;
+
+ uinfo->count = 1;
+ uinfo->value.integer.min = 0;
+ uinfo->value.integer.max = chan->max - chan->min;
+ uinfo->type = (uinfo->value.integer.max == 1) ?
+ SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
+ return 0;
+}
+
+static int simple_iio_aux_get_volsw(struct snd_kcontrol *kcontrol,
+ struct snd_ctl_elem_value *ucontrol)
+{
+ struct simple_iio_aux_chan *chan = (struct simple_iio_aux_chan *)kcontrol->private_value;
+ int max = chan->max;
+ int min = chan->min;
+ unsigned int mask = (1 << fls(max)) - 1;
+ unsigned int invert = chan->is_inverted;
+ int ret;
+ int val;
+
+ ret = iio_read_channel_raw(chan->iio_chan, &val);
+ if (ret < 0)
+ return ret;
+
+ ucontrol->value.integer.value[0] = (val & mask) - min;
+ if (invert)
+ ucontrol->value.integer.value[0] = max - ucontrol->value.integer.value[0];
+
+ return 0;
+}
+
+static int simple_iio_aux_put_volsw(struct snd_kcontrol *kcontrol,
+ struct snd_ctl_elem_value *ucontrol)
+{
+ struct simple_iio_aux_chan *chan = (struct simple_iio_aux_chan *)kcontrol->private_value;
+ int max = chan->max;
+ int min = chan->min;
+ unsigned int mask = (1 << fls(max)) - 1;
+ unsigned int invert = chan->is_inverted;
+ int val;
+ int ret;
+ int tmp;
+
+ val = ucontrol->value.integer.value[0];
+ if (val < 0)
+ return -EINVAL;
+ if (val > max - min)
+ return -EINVAL;
+
+ val = (val + min) & mask;
+ if (invert)
+ val = max - val;
+
+ ret = iio_read_channel_raw(chan->iio_chan, &tmp);
+ if (ret < 0)
+ return ret;
+
+ if (tmp == val)
+ return 0;
+
+ ret = iio_write_channel_raw(chan->iio_chan, val);
+ if (ret)
+ return ret;
+
+ return 1; /* The value changed */
+}
+
+static int simple_iio_aux_add_controls(struct snd_soc_component *component,
+ struct simple_iio_aux_chan *chan)
+{
+ struct snd_kcontrol_new control = {0};
+
+ control.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
+ control.name = chan->name;
+ control.info = simple_iio_aux_info_volsw;
+ control.get = simple_iio_aux_get_volsw;
+ control.put = simple_iio_aux_put_volsw;
+ control.private_value = (unsigned long)chan;
+
+ return snd_soc_add_component_controls(component, &control, 1);
+}
+
+/*
+ * These data could be on stack but they are pretty big.
+ * As ASoC internally copy them and protect them against concurrent accesses
+ * (snd_soc_bind_card() protects using client_mutex), keep them in the global
+ * data area.
+ */
+static struct snd_soc_dapm_widget widgets[3] = {0};
+static struct snd_soc_dapm_route routes[2] = {0};
+
+static int simple_iio_aux_add_dapms(struct snd_soc_component *component,
+ struct simple_iio_aux_chan *chan)
+{
+ struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
+ char *input_name = NULL;
+ char *output_name = NULL;
+ char *pga_name = NULL;
+ int ret;
+
+ input_name = kasprintf(GFP_KERNEL, "%s IN", chan->name);
+ if (!input_name) {
+ ret = -ENOMEM;
+ goto end;
+ }
+ output_name = kasprintf(GFP_KERNEL, "%s OUT", chan->name);
+ if (!output_name) {
+ ret = -ENOMEM;
+ goto end;
+ }
+ pga_name = kasprintf(GFP_KERNEL, "%s PGA", chan->name);
+ if (!pga_name) {
+ ret = -ENOMEM;
+ goto end;
+ }
+
+ BUILD_BUG_ON(ARRAY_SIZE(widgets) < 3);
+ widgets[0] = SND_SOC_DAPM_WIDGET(SND_SOC_DAPM_INPUT(input_name));
+ widgets[1] = SND_SOC_DAPM_WIDGET(SND_SOC_DAPM_OUTPUT(output_name));
+ widgets[2] = SND_SOC_DAPM_WIDGET(SND_SOC_DAPM_PGA(pga_name, SND_SOC_NOPM, 0, 0, NULL, 0));
+ ret = snd_soc_dapm_new_controls(dapm, widgets, 3);
+ if (ret)
+ goto end;
+
+ BUILD_BUG_ON(ARRAY_SIZE(routes) < 2);
+ routes[0].sink = pga_name;
+ routes[0].control = NULL;
+ routes[0].source = input_name;
+ routes[1].sink = output_name;
+ routes[1].control = NULL;
+ routes[1].source = pga_name;
+ ret = snd_soc_dapm_add_routes(dapm, routes, 2);
+
+end:
+ /* Allocated names are no more needed (duplicated in ASoC internals) */
+ kfree(pga_name);
+ kfree(output_name);
+ kfree(input_name);
+
+ return ret;
+}
+
+static int simple_iio_aux_component_probe(struct snd_soc_component *component)
+{
+ struct simple_iio_aux *iio_aux = snd_soc_component_get_drvdata(component);
+ struct simple_iio_aux_chan *chan;
+ int ret;
+ int i;
+
+ for (i = 0; i < iio_aux->num_chans; i++) {
+ chan = iio_aux->chans + i;
+
+ ret = iio_read_max_channel_raw(chan->iio_chan, &chan->max);
+ if (ret) {
+ dev_err(component->dev, "chan[%d] %s: Cannot get max raw value (%d)\n",
+ i, chan->name, ret);
+ return ret;
+ }
+
+ ret = iio_read_min_channel_raw(chan->iio_chan, &chan->min);
+ if (ret) {
+ dev_err(component->dev, "chan[%d] %s: Cannot get min raw value (%d)\n",
+ i, chan->name, ret);
+ return ret;
+ }
+
+ /* Set initial value */
+ ret = iio_write_channel_raw(chan->iio_chan,
+ chan->is_inverted ? chan->max : chan->min);
+ if (ret) {
+ dev_err(component->dev, "chan[%d] %s: Cannot set initial value (%d)\n",
+ i, chan->name, ret);
+ return ret;
+ }
+
+ ret = simple_iio_aux_add_controls(component, chan);
+ if (ret)
+ return ret;
+
+ ret = simple_iio_aux_add_dapms(component, chan);
+ if (ret)
+ return ret;
+
+ dev_dbg(component->dev, "chan[%d]: Added %s (min=%d, max=%d, invert=%s)\n",
+ i, chan->name, chan->min, chan->max,
+ chan->is_inverted ? "on" : "off");
+ }
+
+ return 0;
+}
+
+static const struct snd_soc_component_driver simple_iio_aux_component_driver = {
+ .probe = simple_iio_aux_component_probe,
+};
+
+static int simple_iio_aux_probe(struct platform_device *pdev)
+{
+ struct device_node *np = pdev->dev.of_node;
+ struct simple_iio_aux_chan *iio_aux_chan;
+ struct simple_iio_aux *iio_aux;
+ int count;
+ u32 tmp;
+ int ret;
+ int i;
+
+ iio_aux = devm_kzalloc(&pdev->dev, sizeof(*iio_aux), GFP_KERNEL);
+ if (!iio_aux)
+ return -ENOMEM;
+
+ iio_aux->dev = &pdev->dev;
+
+ count = of_property_count_strings(np, "io-channel-names");
+ if (count < 0) {
+ dev_err(iio_aux->dev, "%pOF: failed to read io-channel-names\n", np);
+ return count;
+ }
+
+ iio_aux->chans = devm_kmalloc_array(&pdev->dev, count,
+ sizeof(*iio_aux->chans), GFP_KERNEL);
+ if (!iio_aux->chans)
+ return -ENOMEM;
+ iio_aux->num_chans = count;
+
+ for (i = 0; i < iio_aux->num_chans; i++) {
+ iio_aux_chan = iio_aux->chans + i;
+
+ ret = of_property_read_string_index(np, "io-channel-names", i,
+ &iio_aux_chan->name);
+ if (ret < 0) {
+ dev_err(iio_aux->dev, "%pOF: failed to read io-channel-names[%d]\n", np, i);
+ return ret;
+ }
+
+ iio_aux_chan->iio_chan = devm_iio_channel_get(iio_aux->dev, iio_aux_chan->name);
+ if (IS_ERR(iio_aux_chan->iio_chan)) {
+ ret = PTR_ERR(iio_aux_chan->iio_chan);
+ return dev_err_probe(iio_aux->dev, ret,
+ "get IIO channel '%s' failed (%d)\n",
+ iio_aux_chan->name, ret);
+ }
+
+ tmp = 0;
+ of_property_read_u32_index(np, "invert", i, &tmp);
+ iio_aux_chan->is_inverted = !!tmp;
+ }
+
+ platform_set_drvdata(pdev, iio_aux);
+
+ return devm_snd_soc_register_component(iio_aux->dev,
+ &simple_iio_aux_component_driver,
+ NULL, 0);
+}
+
+#ifdef CONFIG_OF
+static const struct of_device_id simple_iio_aux_ids[] = {
+ { .compatible = "simple-iio-aux", },
+ { }
+};
+MODULE_DEVICE_TABLE(of, simple_iio_aux_ids);
+#endif
+
+static struct platform_driver simple_iio_aux_driver = {
+ .driver = {
+ .name = "simple-iio-aux",
+ .of_match_table = of_match_ptr(simple_iio_aux_ids),
+ },
+ .probe = simple_iio_aux_probe,
+};
+
+module_platform_driver(simple_iio_aux_driver);
+
+MODULE_AUTHOR("Herve Codina <[email protected]>");
+MODULE_DESCRIPTION("IIO ALSA SoC aux driver");
+MODULE_LICENSE("GPL");
--
2.39.2

2023-04-22 16:39:05

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH 2/4] iio: inkern: Add a helper to query an available minimum raw value

On Fri, 21 Apr 2023 14:41:20 +0200
Herve Codina <[email protected]> wrote:

> A helper, iio_read_max_channel_raw() exists to read the available
> maximum raw value of a channel but nothing similar exists to read the
> available minimum raw value.
>
> This new helper, iio_read_min_channel_raw(), fills the hole and can be
> used for reading the available minimum raw value of a channel.
> It is fully based on the existing iio_read_max_channel_raw().
>
> Signed-off-by: Herve Codina <[email protected]>

Hi Herve,

All the comments on this are really comments on the existing code.
If you don't mind fixing the first one about checking the error code whilst
you are here that would be great. Don't worry about the docs comment.
There are lots of instances of that and the point is rather subtle and probably
post dates this code being written. In a few cases raw doesn't mean ADC counts
but rather something slightly modified... Long story for why!

Jonathan

> ---
> drivers/iio/inkern.c | 67 ++++++++++++++++++++++++++++++++++++
> include/linux/iio/consumer.h | 11 ++++++
> 2 files changed, 78 insertions(+)
>
> diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
> index 872fd5c24147..914fc69c718a 100644
> --- a/drivers/iio/inkern.c
> +++ b/drivers/iio/inkern.c
> @@ -912,6 +912,73 @@ int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
> }
> EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);
>
> +static int iio_channel_read_min(struct iio_channel *chan,
> + int *val, int *val2, int *type,
> + enum iio_chan_info_enum info)
> +{
> + int unused;
> + const int *vals;
> + int length;
> + int ret;
> +
> + if (!val2)
> + val2 = &unused;
> +
> + ret = iio_channel_read_avail(chan, &vals, type, &length, info);
Obviously this is copied from *_read_max() but look at it here...

We should check for an error first with
if (ret < 0)
return ret;
then the switch.

Currently a different positive ret would result in that value
being returned which would be odd. Not a problem today, but if we add other
iio_avail_type enum entries in future and don't keep up with all the
utility functions then a mess may result.

If you agree with change and wouldn't mind adding another patch to this series
tidying that up for the _max case that would be great! Otherwise I'll get to
fixing that at some point but not anytime soon.

> + switch (ret) {
> + case IIO_AVAIL_RANGE:
> + switch (*type) {
> + case IIO_VAL_INT:
> + *val = vals[0];
> + break;
> + default:
> + *val = vals[0];
> + *val2 = vals[1];
> + }
> + return 0;
> +
> + case IIO_AVAIL_LIST:
> + if (length <= 0)
> + return -EINVAL;
> + switch (*type) {
> + case IIO_VAL_INT:
> + *val = vals[--length];
> + while (length) {
> + if (vals[--length] < *val)
> + *val = vals[length];
> + }
> + break;
> + default:
> + /* FIXME: learn about min for other iio values */
> + return -EINVAL;
> + }
> + return 0;
> +
> + default:
> + return ret;
> + }
> +}
> +
> +int iio_read_min_channel_raw(struct iio_channel *chan, int *val)
> +{
> + struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
> + int ret;
> + int type;
> +
> + mutex_lock(&iio_dev_opaque->info_exist_lock);
> + if (!chan->indio_dev->info) {
> + ret = -ENODEV;
> + goto err_unlock;
> + }
> +
> + ret = iio_channel_read_min(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
> +err_unlock:
> + mutex_unlock(&iio_dev_opaque->info_exist_lock);
> +
> + return ret;
> +}
> +EXPORT_SYMBOL_GPL(iio_read_min_channel_raw);
> +
> int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
> {
> struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
> diff --git a/include/linux/iio/consumer.h b/include/linux/iio/consumer.h
> index 6802596b017c..956120d8b5a3 100644
> --- a/include/linux/iio/consumer.h
> +++ b/include/linux/iio/consumer.h
> @@ -297,6 +297,17 @@ int iio_write_channel_raw(struct iio_channel *chan, int val);
> */
> int iio_read_max_channel_raw(struct iio_channel *chan, int *val);
>
> +/**
> + * iio_read_min_channel_raw() - read minimum available raw value from a given
> + * channel, i.e. the minimum possible value.
> + * @chan: The channel being queried.
> + * @val: Value read back.
> + *
> + * Note raw reads from iio channels are in adc counts and hence
> + * scale will need to be applied if standard units are required.

Hmm. That comment is almost always true, but not quite. Not related to
your patch but some cleanup of this documentation and pushing it down next
to implementations should be done at some point. If anyone is really
bored and wants to take this on that's fine. If not, another one for the
todo list ;)

> + */
> +int iio_read_min_channel_raw(struct iio_channel *chan, int *val);
> +
> /**
> * iio_read_avail_channel_raw() - read available raw values from a given channel
> * @chan: The channel being queried.

2023-04-22 16:59:02

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH 4/4] ASoC: codecs: Add support for the generic IIO auxiliary devices

On Fri, 21 Apr 2023 14:41:22 +0200
Herve Codina <[email protected]> wrote:

> Industrial I/O devices can be present in the audio path.
> These devices needs to be used as audio components in order to be fully
> integrated in the audio path.
>
> This support allows to consider these Industrial I/O devices as auxliary
> audio devices and allows to control them using mixer controls.
>
> Signed-off-by: Herve Codina <[email protected]>

Hi Herve,

There are some other IIO devices that might turn up in audio paths. In theory
someone might put an IIO supported amplifier in there (though current ones are
far to high frequency and expensive for that to make sense). For now it
probably makes sense to support potentiometers as you are doing here,
though I'm guessing that in many cases they would be used with some other
analog components. Does the transfer function matter at all?

Been many years since I last touched anything in ASoC so questions may
be silly ;)

A few comments inline.

Jonathan

> +static int simple_iio_aux_get_volsw(struct snd_kcontrol *kcontrol,
> + struct snd_ctl_elem_value *ucontrol)
> +{
> + struct simple_iio_aux_chan *chan = (struct simple_iio_aux_chan *)kcontrol->private_value;
> + int max = chan->max;
> + int min = chan->min;
> + unsigned int mask = (1 << fls(max)) - 1;

As below. I'm not following reason for use of mask

> + unsigned int invert = chan->is_inverted;
> + int ret;
> + int val;
> +
> + ret = iio_read_channel_raw(chan->iio_chan, &val);
> + if (ret < 0)
> + return ret;
> +
> + ucontrol->value.integer.value[0] = (val & mask) - min;
> + if (invert)
> + ucontrol->value.integer.value[0] = max - ucontrol->value.integer.value[0];
> +
> + return 0;
> +}
> +
> +static int simple_iio_aux_put_volsw(struct snd_kcontrol *kcontrol,
> + struct snd_ctl_elem_value *ucontrol)
> +{
> + struct simple_iio_aux_chan *chan = (struct simple_iio_aux_chan *)kcontrol->private_value;
> + int max = chan->max;
> + int min = chan->min;
> + unsigned int mask = (1 << fls(max)) - 1;

Why is mask needed? Also seems like handling is making
some strong assumptions on form of max and min.
So at minimum some comments on reasoning needed.

> + unsigned int invert = chan->is_inverted;
> + int val;
> + int ret;
> + int tmp;
> +
> + val = ucontrol->value.integer.value[0];
> + if (val < 0)
> + return -EINVAL;
> + if (val > max - min)
> + return -EINVAL;
> +
> + val = (val + min) & mask;
> + if (invert)
> + val = max - val;
> +
> + ret = iio_read_channel_raw(chan->iio_chan, &tmp);
> + if (ret < 0)
> + return ret;
> +
> + if (tmp == val)
> + return 0;
> +
> + ret = iio_write_channel_raw(chan->iio_chan, val);
> + if (ret)
> + return ret;
> +
> + return 1; /* The value changed */
> +}
> +

...



> +static int simple_iio_aux_probe(struct platform_device *pdev)
> +{
> + struct device_node *np = pdev->dev.of_node;
> + struct simple_iio_aux_chan *iio_aux_chan;
> + struct simple_iio_aux *iio_aux;
> + int count;
> + u32 tmp;
> + int ret;
> + int i;
> +
> + iio_aux = devm_kzalloc(&pdev->dev, sizeof(*iio_aux), GFP_KERNEL);
> + if (!iio_aux)
> + return -ENOMEM;
> +
> + iio_aux->dev = &pdev->dev;
> +
> + count = of_property_count_strings(np, "io-channel-names");
> + if (count < 0) {
> + dev_err(iio_aux->dev, "%pOF: failed to read io-channel-names\n", np);
> + return count;
> + }
> +
> + iio_aux->chans = devm_kmalloc_array(&pdev->dev, count,
> + sizeof(*iio_aux->chans), GFP_KERNEL);
> + if (!iio_aux->chans)
> + return -ENOMEM;
> + iio_aux->num_chans = count;
> +
> + for (i = 0; i < iio_aux->num_chans; i++) {
> + iio_aux_chan = iio_aux->chans + i;
> +
> + ret = of_property_read_string_index(np, "io-channel-names", i,
> + &iio_aux_chan->name);

Whilst today this will be tightly couple with of, if you can use generic firmware
handling where possible (from linux/property.h) it will reduce what needs
to be tidied up if anyone fills in the gaps for IIO consumer bindings in ACPI
and then someone uses PRP0001 based ACPI bindings.

> + if (ret < 0) {
> + dev_err(iio_aux->dev, "%pOF: failed to read io-channel-names[%d]\n", np, i);

dev_err_probe() would simplify these cases a little. Not sure on ASOC view on using
that for cases that won't defer. I tend to take the view it's nicer everywhere
for calls in probe() functions.


> + return ret;
> + }
> +
> + iio_aux_chan->iio_chan = devm_iio_channel_get(iio_aux->dev, iio_aux_chan->name);
> + if (IS_ERR(iio_aux_chan->iio_chan)) {
> + ret = PTR_ERR(iio_aux_chan->iio_chan);

Put that inline instead of setting ret here.

> + return dev_err_probe(iio_aux->dev, ret,
> + "get IIO channel '%s' failed (%d)\n",
> + iio_aux_chan->name, ret);
> + }
> +
> + tmp = 0;
> + of_property_read_u32_index(np, "invert", i, &tmp);
> + iio_aux_chan->is_inverted = !!tmp;

As it's a bool this is the same as
iio_aux_chan->is_inverted = tmp;

> + }
> +
> + platform_set_drvdata(pdev, iio_aux);
> +
> + return devm_snd_soc_register_component(iio_aux->dev,
> + &simple_iio_aux_component_driver,
> + NULL, 0);
> +}
> +
> +#ifdef CONFIG_OF
> +static const struct of_device_id simple_iio_aux_ids[] = {
> + { .compatible = "simple-iio-aux", },
> + { }
> +};
> +MODULE_DEVICE_TABLE(of, simple_iio_aux_ids);
> +#endif
> +
> +static struct platform_driver simple_iio_aux_driver = {
> + .driver = {
> + .name = "simple-iio-aux",
> + .of_match_table = of_match_ptr(simple_iio_aux_ids),

I'd just drop the of_match_ptr() Whilst this won't work today with other
firmwares, we might enable the missing parts at some stage. Also the
driver is somewhat pointless without DT so I'd just assume it's always
built with it. Cost is a tiny array on systems with a weird
.config

> + },
> + .probe = simple_iio_aux_probe,
> +};
> +
> +module_platform_driver(simple_iio_aux_driver);
> +
> +MODULE_AUTHOR("Herve Codina <[email protected]>");
> +MODULE_DESCRIPTION("IIO ALSA SoC aux driver");
> +MODULE_LICENSE("GPL");

2023-04-24 07:56:42

by Herve Codina

[permalink] [raw]
Subject: Re: [PATCH 2/4] iio: inkern: Add a helper to query an available minimum raw value

Hi Jonathan,

On Sat, 22 Apr 2023 17:49:16 +0100
Jonathan Cameron <[email protected]> wrote:

> On Fri, 21 Apr 2023 14:41:20 +0200
> Herve Codina <[email protected]> wrote:
>
> > A helper, iio_read_max_channel_raw() exists to read the available
> > maximum raw value of a channel but nothing similar exists to read the
> > available minimum raw value.
> >
> > This new helper, iio_read_min_channel_raw(), fills the hole and can be
> > used for reading the available minimum raw value of a channel.
> > It is fully based on the existing iio_read_max_channel_raw().
> >
> > Signed-off-by: Herve Codina <[email protected]>
>
> Hi Herve,
>
> All the comments on this are really comments on the existing code.
> If you don't mind fixing the first one about checking the error code whilst
> you are here that would be great. Don't worry about the docs comment.
> There are lots of instances of that and the point is rather subtle and probably
> post dates this code being written. In a few cases raw doesn't mean ADC counts
> but rather something slightly modified... Long story for why!

A next iteration is already planned for this series.
I will fix the 'error checking before switch()' on the iio_channel_read_min()
I introduced and add a new patch (doing the same) on the existing
iio_channel_read_max().


>
> Jonathan
>
> > ---
> > drivers/iio/inkern.c | 67 ++++++++++++++++++++++++++++++++++++
> > include/linux/iio/consumer.h | 11 ++++++
> > 2 files changed, 78 insertions(+)
> >
> > diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
> > index 872fd5c24147..914fc69c718a 100644
> > --- a/drivers/iio/inkern.c
> > +++ b/drivers/iio/inkern.c
> > @@ -912,6 +912,73 @@ int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
> > }
> > EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);
> >
> > +static int iio_channel_read_min(struct iio_channel *chan,
> > + int *val, int *val2, int *type,
> > + enum iio_chan_info_enum info)
> > +{
> > + int unused;
> > + const int *vals;
> > + int length;
> > + int ret;
> > +
> > + if (!val2)
> > + val2 = &unused;
> > +
> > + ret = iio_channel_read_avail(chan, &vals, type, &length, info);
> Obviously this is copied from *_read_max() but look at it here...
>
> We should check for an error first with
> if (ret < 0)
> return ret;
> then the switch.
>
> Currently a different positive ret would result in that value
> being returned which would be odd. Not a problem today, but if we add other
> iio_avail_type enum entries in future and don't keep up with all the
> utility functions then a mess may result.
>
> If you agree with change and wouldn't mind adding another patch to this series
> tidying that up for the _max case that would be great! Otherwise I'll get to
> fixing that at some point but not anytime soon.

I will do in the next iteration.

>
> > + switch (ret) {
> > + case IIO_AVAIL_RANGE:
> > + switch (*type) {
> > + case IIO_VAL_INT:
> > + *val = vals[0];
> > + break;
> > + default:
> > + *val = vals[0];
> > + *val2 = vals[1];
> > + }
> > + return 0;
> > +
> > + case IIO_AVAIL_LIST:
> > + if (length <= 0)
> > + return -EINVAL;
> > + switch (*type) {
> > + case IIO_VAL_INT:
> > + *val = vals[--length];
> > + while (length) {
> > + if (vals[--length] < *val)
> > + *val = vals[length];
> > + }
> > + break;
> > + default:
> > + /* FIXME: learn about min for other iio values */
> > + return -EINVAL;
> > + }
> > + return 0;
> > +
> > + default:
> > + return ret;
> > + }
> > +}
> > +
> > +int iio_read_min_channel_raw(struct iio_channel *chan, int *val)
> > +{
> > + struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
> > + int ret;
> > + int type;
> > +
> > + mutex_lock(&iio_dev_opaque->info_exist_lock);
> > + if (!chan->indio_dev->info) {
> > + ret = -ENODEV;
> > + goto err_unlock;
> > + }
> > +
> > + ret = iio_channel_read_min(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
> > +err_unlock:
> > + mutex_unlock(&iio_dev_opaque->info_exist_lock);
> > +
> > + return ret;
> > +}
> > +EXPORT_SYMBOL_GPL(iio_read_min_channel_raw);
> > +
> > int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
> > {
> > struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
> > diff --git a/include/linux/iio/consumer.h b/include/linux/iio/consumer.h
> > index 6802596b017c..956120d8b5a3 100644
> > --- a/include/linux/iio/consumer.h
> > +++ b/include/linux/iio/consumer.h
> > @@ -297,6 +297,17 @@ int iio_write_channel_raw(struct iio_channel *chan, int val);
> > */
> > int iio_read_max_channel_raw(struct iio_channel *chan, int *val);
> >
> > +/**
> > + * iio_read_min_channel_raw() - read minimum available raw value from a given
> > + * channel, i.e. the minimum possible value.
> > + * @chan: The channel being queried.
> > + * @val: Value read back.
> > + *
> > + * Note raw reads from iio channels are in adc counts and hence
> > + * scale will need to be applied if standard units are required.
>
> Hmm. That comment is almost always true, but not quite. Not related to
> your patch but some cleanup of this documentation and pushing it down next
> to implementations should be done at some point. If anyone is really
> bored and wants to take this on that's fine. If not, another one for the
> todo list ;)

If you are ok, I can change every where in consumer.h the following:
* Note raw reads from iio channels are in adc counts and hence
* scale will need to be applied if standard units required.
by
* Note raw reads from iio channels are not in standards units and
* hence scale will need to be applied if standard units required.

Also the same for raw writes:
* Note raw writes to iio channels are in dac counts and hence
* scale will need to be applied if standard units required.
by
* Note raw writes to iio channels are not in standards units and
* hence scale will need to be applied if standard units required.

>
> > + */
> > +int iio_read_min_channel_raw(struct iio_channel *chan, int *val);
> > +
> > /**
> > * iio_read_avail_channel_raw() - read available raw values from a given channel
> > * @chan: The channel being queried.
>

Thanks for the review,
Hervé

--
Hervé Codina, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

2023-04-24 10:55:07

by Herve Codina

[permalink] [raw]
Subject: Re: [PATCH 4/4] ASoC: codecs: Add support for the generic IIO auxiliary devices

Hi Jonathan, Mark,

On Sat, 22 Apr 2023 18:08:14 +0100
Jonathan Cameron <[email protected]> wrote:

> On Fri, 21 Apr 2023 14:41:22 +0200
> Herve Codina <[email protected]> wrote:
>
> > Industrial I/O devices can be present in the audio path.
> > These devices needs to be used as audio components in order to be fully
> > integrated in the audio path.
> >
> > This support allows to consider these Industrial I/O devices as auxliary
> > audio devices and allows to control them using mixer controls.
> >
> > Signed-off-by: Herve Codina <[email protected]>
>
> Hi Herve,
>
> There are some other IIO devices that might turn up in audio paths. In theory
> someone might put an IIO supported amplifier in there (though current ones are
> far to high frequency and expensive for that to make sense). For now it
> probably makes sense to support potentiometers as you are doing here,
> though I'm guessing that in many cases they would be used with some other
> analog components. Does the transfer function matter at all?
>
> Been many years since I last touched anything in ASoC so questions may
> be silly ;)
>
> A few comments inline.
>
> Jonathan
>
> > +static int simple_iio_aux_get_volsw(struct snd_kcontrol *kcontrol,
> > + struct snd_ctl_elem_value *ucontrol)
> > +{
> > + struct simple_iio_aux_chan *chan = (struct simple_iio_aux_chan *)kcontrol->private_value;
> > + int max = chan->max;
> > + int min = chan->min;
> > + unsigned int mask = (1 << fls(max)) - 1;
>
> As below. I'm not following reason for use of mask
>
> > + unsigned int invert = chan->is_inverted;
> > + int ret;
> > + int val;
> > +
> > + ret = iio_read_channel_raw(chan->iio_chan, &val);
> > + if (ret < 0)
> > + return ret;
> > +
> > + ucontrol->value.integer.value[0] = (val & mask) - min;
> > + if (invert)
> > + ucontrol->value.integer.value[0] = max - ucontrol->value.integer.value[0];
> > +
> > + return 0;
> > +}
> > +
> > +static int simple_iio_aux_put_volsw(struct snd_kcontrol *kcontrol,
> > + struct snd_ctl_elem_value *ucontrol)
> > +{
> > + struct simple_iio_aux_chan *chan = (struct simple_iio_aux_chan *)kcontrol->private_value;
> > + int max = chan->max;
> > + int min = chan->min;
> > + unsigned int mask = (1 << fls(max)) - 1;
>
> Why is mask needed? Also seems like handling is making
> some strong assumptions on form of max and min.
> So at minimum some comments on reasoning needed.

This mask was present in the internal ASoC helpers used when
devices can be accessed using regmap.
The IIO accesses done by simple_iio_aux_get_volsw() and
simple_iio_aux_put_volsw() were based on these internal helpers.
Not sure about the exact reason to this mask. Maybe Mark can answer.

For these particular use-cases using an IIO channel, the mask present in
simple_iio_aux_get_volsw() and simple_iio_aux_put_volsw() can be removed.

I will remove in the next iteration except if Mark tell me to keep them.

>
> > + unsigned int invert = chan->is_inverted;
> > + int val;
> > + int ret;
> > + int tmp;
> > +
> > + val = ucontrol->value.integer.value[0];
> > + if (val < 0)
> > + return -EINVAL;
> > + if (val > max - min)
> > + return -EINVAL;
> > +
> > + val = (val + min) & mask;
> > + if (invert)
> > + val = max - val;
> > +
> > + ret = iio_read_channel_raw(chan->iio_chan, &tmp);
> > + if (ret < 0)
> > + return ret;
> > +
> > + if (tmp == val)
> > + return 0;
> > +
> > + ret = iio_write_channel_raw(chan->iio_chan, val);
> > + if (ret)
> > + return ret;
> > +
> > + return 1; /* The value changed */
> > +}
> > +
>
> ...
>
>
>
> > +static int simple_iio_aux_probe(struct platform_device *pdev)
> > +{
> > + struct device_node *np = pdev->dev.of_node;
> > + struct simple_iio_aux_chan *iio_aux_chan;
> > + struct simple_iio_aux *iio_aux;
> > + int count;
> > + u32 tmp;
> > + int ret;
> > + int i;
> > +
> > + iio_aux = devm_kzalloc(&pdev->dev, sizeof(*iio_aux), GFP_KERNEL);
> > + if (!iio_aux)
> > + return -ENOMEM;
> > +
> > + iio_aux->dev = &pdev->dev;
> > +
> > + count = of_property_count_strings(np, "io-channel-names");
> > + if (count < 0) {
> > + dev_err(iio_aux->dev, "%pOF: failed to read io-channel-names\n", np);
> > + return count;
> > + }
> > +
> > + iio_aux->chans = devm_kmalloc_array(&pdev->dev, count,
> > + sizeof(*iio_aux->chans), GFP_KERNEL);
> > + if (!iio_aux->chans)
> > + return -ENOMEM;
> > + iio_aux->num_chans = count;
> > +
> > + for (i = 0; i < iio_aux->num_chans; i++) {
> > + iio_aux_chan = iio_aux->chans + i;
> > +
> > + ret = of_property_read_string_index(np, "io-channel-names", i,
> > + &iio_aux_chan->name);
>
> Whilst today this will be tightly couple with of, if you can use generic firmware
> handling where possible (from linux/property.h) it will reduce what needs
> to be tidied up if anyone fills in the gaps for IIO consumer bindings in ACPI
> and then someone uses PRP0001 based ACPI bindings.

No device_property_read_*() function family are available to get a value
from an array using an index.

I would prefer to keep the of_property_read_*() function family I use for this
first IIO auxiliary device support.

>
> > + if (ret < 0) {
> > + dev_err(iio_aux->dev, "%pOF: failed to read io-channel-names[%d]\n", np, i);
>
> dev_err_probe() would simplify these cases a little. Not sure on ASOC view on using
> that for cases that won't defer. I tend to take the view it's nicer everywhere
> for calls in probe() functions.

I have the feeling that ASoC uses dev_err_probe() for cases that can defer.
Mark, can you confirm ?

>
>
> > + return ret;
> > + }
> > +
> > + iio_aux_chan->iio_chan = devm_iio_channel_get(iio_aux->dev, iio_aux_chan->name);
> > + if (IS_ERR(iio_aux_chan->iio_chan)) {
> > + ret = PTR_ERR(iio_aux_chan->iio_chan);
>
> Put that inline instead of setting ret here.

Will be done in the next iteration.

>
> > + return dev_err_probe(iio_aux->dev, ret,
> > + "get IIO channel '%s' failed (%d)\n",
> > + iio_aux_chan->name, ret);
> > + }
> > +
> > + tmp = 0;
> > + of_property_read_u32_index(np, "invert", i, &tmp);
> > + iio_aux_chan->is_inverted = !!tmp;
>
> As it's a bool this is the same as
> iio_aux_chan->is_inverted = tmp;

I will remove the '!!' construction.


>
> > + }
> > +
> > + platform_set_drvdata(pdev, iio_aux);
> > +
> > + return devm_snd_soc_register_component(iio_aux->dev,
> > + &simple_iio_aux_component_driver,
> > + NULL, 0);
> > +}
> > +
> > +#ifdef CONFIG_OF
> > +static const struct of_device_id simple_iio_aux_ids[] = {
> > + { .compatible = "simple-iio-aux", },
> > + { }
> > +};
> > +MODULE_DEVICE_TABLE(of, simple_iio_aux_ids);
> > +#endif
> > +
> > +static struct platform_driver simple_iio_aux_driver = {
> > + .driver = {
> > + .name = "simple-iio-aux",
> > + .of_match_table = of_match_ptr(simple_iio_aux_ids),
>
> I'd just drop the of_match_ptr() Whilst this won't work today with other
> firmwares, we might enable the missing parts at some stage. Also the
> driver is somewhat pointless without DT so I'd just assume it's always
> built with it. Cost is a tiny array on systems with a weird
> .config

of_match_ptr will be removed (and the #ifdef CONFIG_OF also).

>
> > + },
> > + .probe = simple_iio_aux_probe,
> > +};
> > +
> > +module_platform_driver(simple_iio_aux_driver);
> > +
> > +MODULE_AUTHOR("Herve Codina <[email protected]>");
> > +MODULE_DESCRIPTION("IIO ALSA SoC aux driver");
> > +MODULE_LICENSE("GPL");
>

Thanks for the review.

Best regards,
Hervé

2023-04-25 17:35:17

by Rob Herring (Arm)

[permalink] [raw]
Subject: Re: [PATCH 1/4] dt-bindings: sound: Add simple-iio-aux

On Fri, Apr 21, 2023 at 02:41:19PM +0200, Herve Codina wrote:
> Industrial I/O devices can be present in the audio path.
> These devices needs to be viewed as audio components in order to be
> fully integrated in the audio path.
>
> simple-iio-aux allows to consider these Industrial I/O devices as
> auxliary audio devices.

What makes it simple? Any binding called simple or generic is a trigger
for me. Best to avoid those terms. :)

Examples of devices would be useful here.

>
> Signed-off-by: Herve Codina <[email protected]>
> ---
> .../bindings/sound/simple-iio-aux.yaml | 65 +++++++++++++++++++
> 1 file changed, 65 insertions(+)
> create mode 100644 Documentation/devicetree/bindings/sound/simple-iio-aux.yaml
>
> diff --git a/Documentation/devicetree/bindings/sound/simple-iio-aux.yaml b/Documentation/devicetree/bindings/sound/simple-iio-aux.yaml
> new file mode 100644
> index 000000000000..fab128fce4fc
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/sound/simple-iio-aux.yaml
> @@ -0,0 +1,65 @@
> +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/sound/simple-iio-aux.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: Simple IIO auxiliary
> +
> +maintainers:
> + - Herve Codina <[email protected]>
> +
> +description: |

Don't need '|'

> + Auxiliary device based on Industrial I/O device channels
> +
> +allOf:
> + - $ref: /schemas/iio/iio-consumer.yaml

You don't need to reference consumer schemas.

> + - $ref: dai-common.yaml#
> +
> +properties:
> + compatible:
> + const: simple-iio-aux
> +
> + io-channels:
> + description:
> + Industrial I/O device channels used
> +
> + io-channel-names:
> + description:
> + Industrial I/O channel names related to io-channels.
> + These names are used to provides sound controls, widgets and routes names.
> +
> + invert:

Property names should globally only have 1 type definition. This is
generic enough I'd be concerned that's not the case.

> + $ref: /schemas/types.yaml#/definitions/uint32-array
> + description: |
> + A list of 0/1 flags defining whether or not the related channel is
> + inverted
> + items:
> + enum: [0, 1]
> + default: 0
> + description: |
> + Invert the sound control value compared to the IIO channel raw value.
> + - 1: The related sound control value is inverted meaning that the
> + minimum sound control value correspond to the maximum IIO channel
> + raw value and the maximum sound control value correspond to the
> + minimum IIO channel raw value.
> + - 0: The related sound control value is not inverted meaning that the
> + minimum (resp maximum) sound control value correspond to the
> + minimum (resp maximum) IIO channel raw value.
> +
> +required:
> + - compatible
> + - io-channels
> + - io-channel-names
> +
> +unevaluatedProperties: false
> +
> +examples:
> + - |
> + aux {
> + compatible = "simple-iio-aux";
> + io-channels = <&iio 0>, <&iio 1>, <&iio 2>, <&iio 3>;
> + io-channel-names = "CH0", "CH1", "CH2", "CH3";

Not really useful names. Do you have a real example?

> + /* Invert CH1 and CH2 */
> + invert = <0 1 1>;

IMO, invert should be same length as io-channels.

> + };

How do support multiple instances? Say you have 2 sound cards (or 1
sound card with multiple audio paths) each with different sets of IIO
channels associated with it. You'd need a link to each 'aux' node. Why
not just add io-channels to the sound card nodes directly? That's
already just a virtual, top-level container node grouping all the
components. I don't see why we need another virtual node grouping a
subset of them.

Rob

2023-04-25 17:37:58

by Mark Brown

[permalink] [raw]
Subject: Re: [PATCH 1/4] dt-bindings: sound: Add simple-iio-aux

On Tue, Apr 25, 2023 at 12:30:29PM -0500, Rob Herring wrote:
> On Fri, Apr 21, 2023 at 02:41:19PM +0200, Herve Codina wrote:

> > + aux {
> > + compatible = "simple-iio-aux";
> > + io-channels = <&iio 0>, <&iio 1>, <&iio 2>, <&iio 3>;
> > + io-channel-names = "CH0", "CH1", "CH2", "CH3";

> Not really useful names. Do you have a real example?

I fear those might be real names for channels on an IIO device...


Attachments:
(No filename) (446.00 B)
signature.asc (499.00 B)
Download all attachments

2023-04-26 07:41:42

by Herve Codina

[permalink] [raw]
Subject: Re: [PATCH 1/4] dt-bindings: sound: Add simple-iio-aux

Hi Rob,

On Tue, 25 Apr 2023 12:30:29 -0500
Rob Herring <[email protected]> wrote:

> On Fri, Apr 21, 2023 at 02:41:19PM +0200, Herve Codina wrote:
> > Industrial I/O devices can be present in the audio path.
> > These devices needs to be viewed as audio components in order to be
> > fully integrated in the audio path.
> >
> > simple-iio-aux allows to consider these Industrial I/O devices as
> > auxliary audio devices.
>
> What makes it simple? Any binding called simple or generic is a trigger
> for me. Best to avoid those terms. :)

I choose simple-iio-aux because some simple-* already exists.
For instance simple-audio-amplifier or simple-audio-mux.

Do you prefer audio-iio-aux ?
Let me know if I should change.

>
> Examples of devices would be useful here.
>
> >
> > Signed-off-by: Herve Codina <[email protected]>
> > ---
> > .../bindings/sound/simple-iio-aux.yaml | 65 +++++++++++++++++++
> > 1 file changed, 65 insertions(+)
> > create mode 100644 Documentation/devicetree/bindings/sound/simple-iio-aux.yaml
> >
> > diff --git a/Documentation/devicetree/bindings/sound/simple-iio-aux.yaml b/Documentation/devicetree/bindings/sound/simple-iio-aux.yaml
> > new file mode 100644
> > index 000000000000..fab128fce4fc
> > --- /dev/null
> > +++ b/Documentation/devicetree/bindings/sound/simple-iio-aux.yaml
> > @@ -0,0 +1,65 @@
> > +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
> > +%YAML 1.2
> > +---
> > +$id: http://devicetree.org/schemas/sound/simple-iio-aux.yaml#
> > +$schema: http://devicetree.org/meta-schemas/core.yaml#
> > +
> > +title: Simple IIO auxiliary
> > +
> > +maintainers:
> > + - Herve Codina <[email protected]>
> > +
> > +description: |
>
> Don't need '|'

Will be fixed.

>
> > + Auxiliary device based on Industrial I/O device channels
> > +
> > +allOf:
> > + - $ref: /schemas/iio/iio-consumer.yaml
>
> You don't need to reference consumer schemas.

Right, will be removed.

>
> > + - $ref: dai-common.yaml#
> > +
> > +properties:
> > + compatible:
> > + const: simple-iio-aux
> > +
> > + io-channels:
> > + description:
> > + Industrial I/O device channels used
> > +
> > + io-channel-names:
> > + description:
> > + Industrial I/O channel names related to io-channels.
> > + These names are used to provides sound controls, widgets and routes names.
> > +
> > + invert:
>
> Property names should globally only have 1 type definition. This is
> generic enough I'd be concerned that's not the case.

What do you mean ?

>
> > + $ref: /schemas/types.yaml#/definitions/uint32-array
> > + description: |
> > + A list of 0/1 flags defining whether or not the related channel is
> > + inverted
> > + items:
> > + enum: [0, 1]
> > + default: 0
> > + description: |
> > + Invert the sound control value compared to the IIO channel raw value.
> > + - 1: The related sound control value is inverted meaning that the
> > + minimum sound control value correspond to the maximum IIO channel
> > + raw value and the maximum sound control value correspond to the
> > + minimum IIO channel raw value.
> > + - 0: The related sound control value is not inverted meaning that the
> > + minimum (resp maximum) sound control value correspond to the
> > + minimum (resp maximum) IIO channel raw value.
> > +
> > +required:
> > + - compatible
> > + - io-channels
> > + - io-channel-names
> > +
> > +unevaluatedProperties: false
> > +
> > +examples:
> > + - |
> > + aux {
> > + compatible = "simple-iio-aux";
> > + io-channels = <&iio 0>, <&iio 1>, <&iio 2>, <&iio 3>;
> > + io-channel-names = "CH0", "CH1", "CH2", "CH3";
>
> Not really useful names. Do you have a real example?

As Mark said, for IIO channel, using CHx makes sense.
See below, I provide a full example.

>
> > + /* Invert CH1 and CH2 */
> > + invert = <0 1 1>;
>
> IMO, invert should be same length as io-channels.

I will update.

Related to this topic, when I wrote this binding, I wanted to add some
rules/constraints in order to:
- Have this property optional
- If present, force to have as many items in the invert array as the
number of items present in the io-channels array.

I never succeed in writing the constraint for the invert property.
It should be possible (it is done for some 'foo' 'foo-names' pair such
as clocks).
Can you tell me if possible in my case and give me some pointers ?

>
> > + };
>
> How do support multiple instances? Say you have 2 sound cards (or 1
> sound card with multiple audio paths) each with different sets of IIO
> channels associated with it. You'd need a link to each 'aux' node. Why
> not just add io-channels to the sound card nodes directly? That's
> already just a virtual, top-level container node grouping all the
> components. I don't see why we need another virtual node grouping a
> subset of them.

I don't see what you mean.
I use a simple-audio-card and here is a full example using several
instances:

spi {
#address-cells = <1>;
#size-cells = <0>;
/* potentiometers present in an input amplifier design */
pot_in: potentiometer@0 {
compatible = "foo,xxx";
reg = <0>;
#io-channel-cells = <1>;
};
/* potentiometers present in an output amplifier design */
pot_out: potentiometer@1 {
compatible = "foo,xxx";
reg = <1>;
#io-channel-cells = <1>;
};
/* A codec */
codec: codec@2 {
compatible = "bar,yyy";
reg = <2>;
sound-name-prefix = "CODEC";
};

};

amp_out: aux-out {
compatible = "simple-iio-aux";
io-channels = <&pot_out 0>, <&pot_out 1>,
io-channel-names = "CH0", "CH1";
invert = <1 1>;
sound-name-prefix = "AMP_OUT";
};

amp_in: aux-in {
compatible = "simple-iio-aux";
io-channels = <&pot_in 0>, <&pot_in 1>;
io-channel-names = "CH0", "CH1";
sound-name-prefix = "AMP_IN";
};

sound {
compatible = "simple-audio-card";
#address-cells = <1>;
#size-cells = <0>;
simple-audio-card,name = "My Sound Card";

simple-audio-card,aux-devs = <&amp_in>, <&amp_out>;
simple-audio-card,routing =
"CODEC IN0", "AMP_IN CH0 OUT",
"CODEC IN1", "AMP_IN CH1 OUT",
"AMP_OUT CH0 IN", "CODEC OUT0",
"AMP_OUT CH1 IN", "CODEC OUT1",

simple-audio-card,dai-link@0 {
...
};
};


Best regards,
Hervé

--
Hervé Codina, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

2023-05-01 15:08:23

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH 2/4] iio: inkern: Add a helper to query an available minimum raw value

> > > diff --git a/include/linux/iio/consumer.h b/include/linux/iio/consumer.h
> > > index 6802596b017c..956120d8b5a3 100644
> > > --- a/include/linux/iio/consumer.h
> > > +++ b/include/linux/iio/consumer.h
> > > @@ -297,6 +297,17 @@ int iio_write_channel_raw(struct iio_channel *chan, int val);
> > > */
> > > int iio_read_max_channel_raw(struct iio_channel *chan, int *val);
> > >
> > > +/**
> > > + * iio_read_min_channel_raw() - read minimum available raw value from a given
> > > + * channel, i.e. the minimum possible value.
> > > + * @chan: The channel being queried.
> > > + * @val: Value read back.
> > > + *
> > > + * Note raw reads from iio channels are in adc counts and hence
> > > + * scale will need to be applied if standard units are required.
> >
> > Hmm. That comment is almost always true, but not quite. Not related to
> > your patch but some cleanup of this documentation and pushing it down next
> > to implementations should be done at some point. If anyone is really
> > bored and wants to take this on that's fine. If not, another one for the
> > todo list ;)
>
> If you are ok, I can change every where in consumer.h the following:
> * Note raw reads from iio channels are in adc counts and hence
> * scale will need to be applied if standard units required.
> by
> * Note raw reads from iio channels are not in standards units and
> * hence scale will need to be applied if standard units required.

If going to the effort, we should include offset and make it clear how
they are applied.

* Note, if standard units are required, raw reads from iio channels
* need the offset (default 0) and scale (default 1) to be applied
* as (raw + offset) * scale.


>
> Also the same for raw writes:
> * Note raw writes to iio channels are in dac counts and hence
> * scale will need to be applied if standard units required.
> by
> * Note raw writes to iio channels are not in standards units and
> * hence scale will need to be applied if standard units required.
This one is more interesting because you kind of need to apply the opposite
logic. Perhaps text such as.

* Note that for raw writes to iio channels, if the value provided is
* in standard units, the affect of the scale and offset must be removed
* as (value / scale) - offset.

My slight concern is that we'll spend longer arguing about these comments
than we spend on the rest of the patch set. Might be worth delaying
fixing the others for a separate series after this one.

Jonathan

>
> >
> > > + */
> > > +int iio_read_min_channel_raw(struct iio_channel *chan, int *val);
> > > +
> > > /**
> > > * iio_read_avail_channel_raw() - read available raw values from a given channel
> > > * @chan: The channel being queried.
> >
>
> Thanks for the review,
> Hervé
>

2023-05-01 15:11:10

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH 4/4] ASoC: codecs: Add support for the generic IIO auxiliary devices


> >
> > > +static int simple_iio_aux_probe(struct platform_device *pdev)
> > > +{
> > > + struct device_node *np = pdev->dev.of_node;
> > > + struct simple_iio_aux_chan *iio_aux_chan;
> > > + struct simple_iio_aux *iio_aux;
> > > + int count;
> > > + u32 tmp;
> > > + int ret;
> > > + int i;
> > > +
> > > + iio_aux = devm_kzalloc(&pdev->dev, sizeof(*iio_aux), GFP_KERNEL);
> > > + if (!iio_aux)
> > > + return -ENOMEM;
> > > +
> > > + iio_aux->dev = &pdev->dev;
> > > +
> > > + count = of_property_count_strings(np, "io-channel-names");
> > > + if (count < 0) {
> > > + dev_err(iio_aux->dev, "%pOF: failed to read io-channel-names\n", np);
> > > + return count;
> > > + }
> > > +
> > > + iio_aux->chans = devm_kmalloc_array(&pdev->dev, count,
> > > + sizeof(*iio_aux->chans), GFP_KERNEL);
> > > + if (!iio_aux->chans)
> > > + return -ENOMEM;
> > > + iio_aux->num_chans = count;
> > > +
> > > + for (i = 0; i < iio_aux->num_chans; i++) {
> > > + iio_aux_chan = iio_aux->chans + i;
> > > +
> > > + ret = of_property_read_string_index(np, "io-channel-names", i,
> > > + &iio_aux_chan->name);
> >
> > Whilst today this will be tightly couple with of, if you can use generic firmware
> > handling where possible (from linux/property.h) it will reduce what needs
> > to be tidied up if anyone fills in the gaps for IIO consumer bindings in ACPI
> > and then someone uses PRP0001 based ACPI bindings.
>
> No device_property_read_*() function family are available to get a value
> from an array using an index.

That feels like it might be a feature gap in the generic property handling that
should be solved. Emtirely reasonable not to do it in this series however!



>
> I would prefer to keep the of_property_read_*() function family I use for this
> first IIO auxiliary device support.
>
> >
> > > + if (ret < 0) {
> > > + dev_err(iio_aux->dev, "%pOF: failed to read io-channel-names[%d]\n", np, i);
> >
> > dev_err_probe() would simplify these cases a little. Not sure on ASOC view on using
> > that for cases that won't defer. I tend to take the view it's nicer everywhere
> > for calls in probe() functions.
>
> I have the feeling that ASoC uses dev_err_probe() for cases that can defer.
> Mark, can you confirm ?
>

Left as needs an answer from Mark.

Jonathan



2023-05-02 07:39:55

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH 1/4] dt-bindings: sound: Add simple-iio-aux

On 26/04/2023 09:36, Herve Codina wrote:
> Hi Rob,
>
> On Tue, 25 Apr 2023 12:30:29 -0500
> Rob Herring <[email protected]> wrote:
>
>> On Fri, Apr 21, 2023 at 02:41:19PM +0200, Herve Codina wrote:
>>> Industrial I/O devices can be present in the audio path.
>>> These devices needs to be viewed as audio components in order to be
>>> fully integrated in the audio path.
>>>
>>> simple-iio-aux allows to consider these Industrial I/O devices as
>>> auxliary audio devices.
>>
>> What makes it simple? Any binding called simple or generic is a trigger
>> for me. Best to avoid those terms. :)
>
> I choose simple-iio-aux because some simple-* already exists.
> For instance simple-audio-amplifier or simple-audio-mux.
>
> Do you prefer audio-iio-aux ?
> Let me know if I should change.

It means that often what people call "simple" and "generic" works only
for their specific case, because it is not really simple and generic.
After some time the "simple" and "generic" becomes "complicated" and
"huge". Conclusion: sometimes simple and generic bindings are bad idea
and you should have something specific.

Your description in the binding also does not help to match it to
specific, real device. Provide the examples, as Rob asked.

...

>>> + io-channels:
>>> + description:
>>> + Industrial I/O device channels used
>>> +
>>> + io-channel-names:
>>> + description:
>>> + Industrial I/O channel names related to io-channels.
>>> + These names are used to provides sound controls, widgets and routes names.
>>> +
>>> + invert:
>>
>> Property names should globally only have 1 type definition. This is
>> generic enough I'd be concerned that's not the case.
>
> What do you mean ?

It is quite likely this will interfere with other properties having the
same name but different type/definition. If you want to keep it named
generic, then please investigate how this would affect any other
bindings. So easier is to make it not that generic, some more specific name.

>
>>
>>> + $ref: /schemas/types.yaml#/definitions/uint32-array
>>> + description: |
>>> + A list of 0/1 flags defining whether or not the related channel is
>>> + inverted
>>> + items:
>>> + enum: [0, 1]
>>> + default: 0
>>> + description: |
>>> + Invert the sound control value compared to the IIO channel raw value.
>>> + - 1: The related sound control value is inverted meaning that the
>>> + minimum sound control value correspond to the maximum IIO channel
>>> + raw value and the maximum sound control value correspond to the
>>> + minimum IIO channel raw value.
>>> + - 0: The related sound control value is not inverted meaning that the
>>> + minimum (resp maximum) sound control value correspond to the
>>> + minimum (resp maximum) IIO channel raw value.
>>> +
>>> +required:
>>> + - compatible
>>> + - io-channels
>>> + - io-channel-names
>>> +
>>> +unevaluatedProperties: false
>>> +
>>> +examples:
>>> + - |
>>> + aux {
>>> + compatible = "simple-iio-aux";
>>> + io-channels = <&iio 0>, <&iio 1>, <&iio 2>, <&iio 3>;
>>> + io-channel-names = "CH0", "CH1", "CH2", "CH3";
>>
>> Not really useful names. Do you have a real example?
>
> As Mark said, for IIO channel, using CHx makes sense.
> See below, I provide a full example.
>
>>
>>> + /* Invert CH1 and CH2 */
>>> + invert = <0 1 1>;
>>
>> IMO, invert should be same length as io-channels.
>
> I will update.
>
> Related to this topic, when I wrote this binding, I wanted to add some
> rules/constraints in order to:
> - Have this property optional
> - If present, force to have as many items in the invert array as the
> number of items present in the io-channels array.
>
> I never succeed in writing the constraint for the invert property.
> It should be possible (it is done for some 'foo' 'foo-names' pair such
> as clocks).
> Can you tell me if possible in my case and give me some pointers ?
>
>>
>>> + };
>>
>> How do support multiple instances? Say you have 2 sound cards (or 1
>> sound card with multiple audio paths) each with different sets of IIO
>> channels associated with it. You'd need a link to each 'aux' node. Why
>> not just add io-channels to the sound card nodes directly? That's
>> already just a virtual, top-level container node grouping all the
>> components. I don't see why we need another virtual node grouping a
>> subset of them.
>
> I don't see what you mean.
> I use a simple-audio-card and here is a full example using several
> instances:

Just like Rob said: "You'd need a link to each 'aux' node"

and you did it...

So now the rest of Rob's answer:

"Why not just add io-channels to the sound card nodes directly? That's
already just a virtual, top-level container node grouping all the
components. I don't see why we need another virtual node grouping a
subset of them."

Why do you need another node if it is not really representing a real,
separate device?

Best regards,
Krzysztof

2023-05-04 04:30:00

by Mark Brown

[permalink] [raw]
Subject: Re: [PATCH 1/4] dt-bindings: sound: Add simple-iio-aux

On Tue, May 02, 2023 at 09:26:32AM +0200, Krzysztof Kozlowski wrote:
> On 26/04/2023 09:36, Herve Codina wrote:
> > Rob Herring <[email protected]> wrote:
> >> On Fri, Apr 21, 2023 at 02:41:19PM +0200, Herve Codina wrote:

> >>> simple-iio-aux allows to consider these Industrial I/O devices as
> >>> auxliary audio devices.

> >> What makes it simple? Any binding called simple or generic is a trigger
> >> for me. Best to avoid those terms. :)

> > I choose simple-iio-aux because some simple-* already exists.
> > For instance simple-audio-amplifier or simple-audio-mux.

> > Do you prefer audio-iio-aux ?
> > Let me know if I should change.

> It means that often what people call "simple" and "generic" works only
> for their specific case, because it is not really simple and generic.
> After some time the "simple" and "generic" becomes "complicated" and
> "huge". Conclusion: sometimes simple and generic bindings are bad idea
> and you should have something specific.

> Your description in the binding also does not help to match it to
> specific, real device. Provide the examples, as Rob asked.

I don't understand what you are looking for here. IIO is a subsystem
which represents generic DACs and ADCs (along with other I/O things).
Audio devices also have DACs and ADCs, somewhat specialised for use in
audio but more limited by specs and interfaces than by anything
fundamental. The goal here is to map DACs and ADCs described as IIO for
use in an audio context.

ADCs are devices that convert analog signals into digital values, DACs
are devices that convert digital values into analog signals.

> >> How do support multiple instances? Say you have 2 sound cards (or 1
> >> sound card with multiple audio paths) each with different sets of IIO
> >> channels associated with it. You'd need a link to each 'aux' node. Why
> >> not just add io-channels to the sound card nodes directly? That's
> >> already just a virtual, top-level container node grouping all the
> >> components. I don't see why we need another virtual node grouping a
> >> subset of them.

> > I don't see what you mean.
> > I use a simple-audio-card and here is a full example using several
> > instances:

> Just like Rob said: "You'd need a link to each 'aux' node"

> and you did it...

> So now the rest of Rob's answer:

> "Why not just add io-channels to the sound card nodes directly? That's
> already just a virtual, top-level container node grouping all the
> components. I don't see why we need another virtual node grouping a
> subset of them."

> Why do you need another node if it is not really representing a real,
> separate device?

If nothing else I would expect it to be useful from a comprehensibility
point of view to bundle multiple IIO devices into a single multi-channel
audio stream, an individual IIO device is likely to only present a
single channel of data but it is common to group multiple channels of
audio data.


Attachments:
(No filename) (2.93 kB)
signature.asc (499.00 B)
Download all attachments

2023-05-11 07:27:05

by Herve Codina

[permalink] [raw]
Subject: Re: [PATCH 1/4] dt-bindings: sound: Add simple-iio-aux

Hi Rob, Krzysztof, Mark,

On Thu, 4 May 2023 13:22:35 +0900
Mark Brown <[email protected]> wrote:

> On Tue, May 02, 2023 at 09:26:32AM +0200, Krzysztof Kozlowski wrote:
> > On 26/04/2023 09:36, Herve Codina wrote:
> > > Rob Herring <[email protected]> wrote:
> > >> On Fri, Apr 21, 2023 at 02:41:19PM +0200, Herve Codina wrote:
>
> > >>> simple-iio-aux allows to consider these Industrial I/O devices as
> > >>> auxliary audio devices.
>
> > >> What makes it simple? Any binding called simple or generic is a trigger
> > >> for me. Best to avoid those terms. :)
>
> > > I choose simple-iio-aux because some simple-* already exists.
> > > For instance simple-audio-amplifier or simple-audio-mux.
>
> > > Do you prefer audio-iio-aux ?
> > > Let me know if I should change.
>
> > It means that often what people call "simple" and "generic" works only
> > for their specific case, because it is not really simple and generic.
> > After some time the "simple" and "generic" becomes "complicated" and
> > "huge". Conclusion: sometimes simple and generic bindings are bad idea
> > and you should have something specific.
>
> > Your description in the binding also does not help to match it to
> > specific, real device. Provide the examples, as Rob asked.
>
> I don't understand what you are looking for here. IIO is a subsystem
> which represents generic DACs and ADCs (along with other I/O things).
> Audio devices also have DACs and ADCs, somewhat specialised for use in
> audio but more limited by specs and interfaces than by anything
> fundamental. The goal here is to map DACs and ADCs described as IIO for
> use in an audio context.
>
> ADCs are devices that convert analog signals into digital values, DACs
> are devices that convert digital values into analog signals.
>
> > >> How do support multiple instances? Say you have 2 sound cards (or 1
> > >> sound card with multiple audio paths) each with different sets of IIO
> > >> channels associated with it. You'd need a link to each 'aux' node. Why
> > >> not just add io-channels to the sound card nodes directly? That's
> > >> already just a virtual, top-level container node grouping all the
> > >> components. I don't see why we need another virtual node grouping a
> > >> subset of them.
>
> > > I don't see what you mean.
> > > I use a simple-audio-card and here is a full example using several
> > > instances:
>
> > Just like Rob said: "You'd need a link to each 'aux' node"
>
> > and you did it...
>
> > So now the rest of Rob's answer:
>
> > "Why not just add io-channels to the sound card nodes directly? That's
> > already just a virtual, top-level container node grouping all the
> > components. I don't see why we need another virtual node grouping a
> > subset of them."
>
> > Why do you need another node if it is not really representing a real,
> > separate device?
>
> If nothing else I would expect it to be useful from a comprehensibility
> point of view to bundle multiple IIO devices into a single multi-channel
> audio stream, an individual IIO device is likely to only present a
> single channel of data but it is common to group multiple channels of
> audio data.

I cannot simply add io-channels to the sound card directly. I need a node
to set at least the sound-name-prefix property. Further more having a node
and a related compatible string can be easier to maintain and add future
evolution related to these "virtual" devices.

As some subnodes are already defined for a sound card node, I propose to
group these "virtual" audio devices node in a specific bundle node.
This lead to the following example:
---- 8< ----
spi {
#address-cells = <1>;
#size-cells = <0>;
/* potentiometers present in an input amplifier design */
pot_in: potentiometer@0 {
compatible = "foo,xxx";
reg = <0>;
#io-channel-cells = <1>;
};
/* potentiometers present in an output amplifier design */
pot_out: potentiometer@1 {
compatible = "foo,xxx";
reg = <1>;
#io-channel-cells = <1>;
};
/* A codec */
codec: codec@2 {
compatible = "bar,yyy";
reg = <2>;
sound-name-prefix = "CODEC";
};

};

sound {
compatible = "simple-audio-card";
#address-cells = <1>;
#size-cells = <0>;
simple-audio-card,name = "My Sound Card";

simple-audio-card,aux-devs = <&amp_in>, <&amp_out>;
simple-audio-card,routing =
"CODEC IN0", "AMP_IN CH0 OUT",
"CODEC IN1", "AMP_IN CH1 OUT",
"AMP_OUT CH0 IN", "CODEC OUT0",
"AMP_OUT CH1 IN", "CODEC OUT1";

simple-audio-card,dai-link@0 {
...
};

...

/* A bundle for the additional devices */
simple-audio-card,additional-devs {
amp_out: aux-out {
compatible = "audio-iio-aux"; /* Instead of "simple-iio-aux */
io-channels = <&pot_out 0>, <&pot_out 1>,
io-channel-names = "CH0", "CH1";
snd-control-invert-range = <1 1>; /* Old 'invert' renamed */
sound-name-prefix = "AMP_OUT";
};

amp_in: aux-in {
compatible = "audio-iio-aux";
io-channels = <&pot_in 0>, <&pot_in 1>;
io-channel-names = "CH0", "CH1";
sound-name-prefix = "AMP_IN";
};
};
};
---- 8< ----

What do you think about this new binding ?

Best regards,
Hervé