1 // SPDX-License-Identifier: GPL-2.0-only
3 // ALSA SoC glue to use IIO devices as audio components
5 // Copyright 2023 CS GROUP France
7 // Author: Herve Codina <herve.codina@bootlin.com>
9 #include <linux/iio/consumer.h>
10 #include <linux/minmax.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
14 #include <linux/string_helpers.h>
16 #include <sound/soc.h>
17 #include <sound/tlv.h>
19 struct audio_iio_aux_chan {
20 struct iio_channel *iio_chan;
27 struct audio_iio_aux {
29 unsigned int num_chans;
30 struct audio_iio_aux_chan chans[] __counted_by(num_chans);
33 static int audio_iio_aux_info_volsw(struct snd_kcontrol *kcontrol,
34 struct snd_ctl_elem_info *uinfo)
36 struct audio_iio_aux_chan *chan = (struct audio_iio_aux_chan *)kcontrol->private_value;
39 uinfo->value.integer.min = 0;
40 uinfo->value.integer.max = chan->max - chan->min;
41 uinfo->type = (uinfo->value.integer.max == 1) ?
42 SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
46 static int audio_iio_aux_get_volsw(struct snd_kcontrol *kcontrol,
47 struct snd_ctl_elem_value *ucontrol)
49 struct audio_iio_aux_chan *chan = (struct audio_iio_aux_chan *)kcontrol->private_value;
52 bool invert_range = chan->is_invert_range;
56 ret = iio_read_channel_raw(chan->iio_chan, &val);
60 ucontrol->value.integer.value[0] = val - min;
62 ucontrol->value.integer.value[0] = max - ucontrol->value.integer.value[0];
67 static int audio_iio_aux_put_volsw(struct snd_kcontrol *kcontrol,
68 struct snd_ctl_elem_value *ucontrol)
70 struct audio_iio_aux_chan *chan = (struct audio_iio_aux_chan *)kcontrol->private_value;
73 bool invert_range = chan->is_invert_range;
78 val = ucontrol->value.integer.value[0];
88 ret = iio_read_channel_raw(chan->iio_chan, &tmp);
95 ret = iio_write_channel_raw(chan->iio_chan, val);
99 return 1; /* The value changed */
102 static int audio_iio_aux_add_controls(struct snd_soc_component *component,
103 struct audio_iio_aux_chan *chan)
105 struct snd_kcontrol_new control = {
106 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
108 .info = audio_iio_aux_info_volsw,
109 .get = audio_iio_aux_get_volsw,
110 .put = audio_iio_aux_put_volsw,
111 .private_value = (unsigned long)chan,
114 return snd_soc_add_component_controls(component, &control, 1);
118 * These data could be on stack but they are pretty big.
119 * As ASoC internally copy them and protect them against concurrent accesses
120 * (snd_soc_bind_card() protects using client_mutex), keep them in the global
123 static struct snd_soc_dapm_widget widgets[3];
124 static struct snd_soc_dapm_route routes[2];
126 /* Be sure sizes are correct (need 3 widgets and 2 routes) */
127 static_assert(ARRAY_SIZE(widgets) >= 3, "3 widgets are needed");
128 static_assert(ARRAY_SIZE(routes) >= 2, "2 routes are needed");
130 static int audio_iio_aux_add_dapms(struct snd_soc_component *component,
131 struct audio_iio_aux_chan *chan)
133 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
139 input_name = kasprintf(GFP_KERNEL, "%s IN", chan->name);
143 output_name = kasprintf(GFP_KERNEL, "%s OUT", chan->name);
146 goto out_free_input_name;
149 pga_name = kasprintf(GFP_KERNEL, "%s PGA", chan->name);
152 goto out_free_output_name;
155 widgets[0] = SND_SOC_DAPM_INPUT(input_name);
156 widgets[1] = SND_SOC_DAPM_OUTPUT(output_name);
157 widgets[2] = SND_SOC_DAPM_PGA(pga_name, SND_SOC_NOPM, 0, 0, NULL, 0);
158 ret = snd_soc_dapm_new_controls(dapm, widgets, 3);
160 goto out_free_pga_name;
162 routes[0].sink = pga_name;
163 routes[0].control = NULL;
164 routes[0].source = input_name;
165 routes[1].sink = output_name;
166 routes[1].control = NULL;
167 routes[1].source = pga_name;
168 ret = snd_soc_dapm_add_routes(dapm, routes, 2);
170 /* Allocated names are no more needed (duplicated in ASoC internals) */
174 out_free_output_name:
181 static int audio_iio_aux_component_probe(struct snd_soc_component *component)
183 struct audio_iio_aux *iio_aux = snd_soc_component_get_drvdata(component);
184 struct audio_iio_aux_chan *chan;
188 for (i = 0; i < iio_aux->num_chans; i++) {
189 chan = iio_aux->chans + i;
191 ret = iio_read_max_channel_raw(chan->iio_chan, &chan->max);
193 return dev_err_probe(component->dev, ret,
194 "chan[%d] %s: Cannot get max raw value\n",
197 ret = iio_read_min_channel_raw(chan->iio_chan, &chan->min);
199 return dev_err_probe(component->dev, ret,
200 "chan[%d] %s: Cannot get min raw value\n",
203 if (chan->min > chan->max) {
205 * This should never happen but to avoid any check
206 * later, just swap values here to ensure that the
207 * minimum value is lower than the maximum value.
209 dev_dbg(component->dev, "chan[%d] %s: Swap min and max\n",
211 swap(chan->min, chan->max);
214 /* Set initial value */
215 ret = iio_write_channel_raw(chan->iio_chan,
216 chan->is_invert_range ? chan->max : chan->min);
218 return dev_err_probe(component->dev, ret,
219 "chan[%d] %s: Cannot set initial value\n",
222 ret = audio_iio_aux_add_controls(component, chan);
226 ret = audio_iio_aux_add_dapms(component, chan);
230 dev_dbg(component->dev, "chan[%d]: Added %s (min=%d, max=%d, invert=%s)\n",
231 i, chan->name, chan->min, chan->max,
232 str_on_off(chan->is_invert_range));
238 static const struct snd_soc_component_driver audio_iio_aux_component_driver = {
239 .probe = audio_iio_aux_component_probe,
242 static int audio_iio_aux_probe(struct platform_device *pdev)
244 struct audio_iio_aux_chan *iio_aux_chan;
245 struct device *dev = &pdev->dev;
246 struct audio_iio_aux *iio_aux;
253 count = device_property_string_array_count(dev, "io-channel-names");
255 return dev_err_probe(dev, count, "failed to count io-channel-names\n");
257 iio_aux = devm_kzalloc(dev, struct_size(iio_aux, chans, count), GFP_KERNEL);
263 iio_aux->num_chans = count;
265 names = kcalloc(iio_aux->num_chans, sizeof(*names), GFP_KERNEL);
269 invert_ranges = kcalloc(iio_aux->num_chans, sizeof(*invert_ranges), GFP_KERNEL);
270 if (!invert_ranges) {
275 ret = device_property_read_string_array(dev, "io-channel-names",
276 names, iio_aux->num_chans);
278 dev_err_probe(dev, ret, "failed to read io-channel-names\n");
279 goto out_free_invert_ranges;
283 * snd-control-invert-range is optional and can contain fewer items
284 * than the number of channels. Unset values default to 0.
286 count = device_property_count_u32(dev, "snd-control-invert-range");
288 count = min_t(unsigned int, count, iio_aux->num_chans);
289 ret = device_property_read_u32_array(dev, "snd-control-invert-range",
290 invert_ranges, count);
292 dev_err_probe(dev, ret, "failed to read snd-control-invert-range\n");
293 goto out_free_invert_ranges;
297 for (i = 0; i < iio_aux->num_chans; i++) {
298 iio_aux_chan = iio_aux->chans + i;
299 iio_aux_chan->name = names[i];
300 iio_aux_chan->is_invert_range = invert_ranges[i];
302 iio_aux_chan->iio_chan = devm_iio_channel_get(dev, iio_aux_chan->name);
303 if (IS_ERR(iio_aux_chan->iio_chan)) {
304 ret = PTR_ERR(iio_aux_chan->iio_chan);
305 dev_err_probe(dev, ret, "get IIO channel '%s' failed\n",
307 goto out_free_invert_ranges;
311 platform_set_drvdata(pdev, iio_aux);
313 ret = devm_snd_soc_register_component(dev, &audio_iio_aux_component_driver,
315 out_free_invert_ranges:
316 kfree(invert_ranges);
322 static const struct of_device_id audio_iio_aux_ids[] = {
323 { .compatible = "audio-iio-aux" },
326 MODULE_DEVICE_TABLE(of, audio_iio_aux_ids);
328 static struct platform_driver audio_iio_aux_driver = {
330 .name = "audio-iio-aux",
331 .of_match_table = audio_iio_aux_ids,
333 .probe = audio_iio_aux_probe,
335 module_platform_driver(audio_iio_aux_driver);
337 MODULE_AUTHOR("Herve Codina <herve.codina@bootlin.com>");
338 MODULE_DESCRIPTION("IIO ALSA SoC aux driver");
339 MODULE_LICENSE("GPL");