4494c8dea6801c7fc38624f9046e9cd9bc43ad23
[openwrt/staging/blocktrron.git] /
1 From 471f12c73cf73db88695d5161ec5c29f94fde1d0 Mon Sep 17 00:00:00 2001
2 From: Matt Flax <flatmax@flatmax.org>
3 Date: Wed, 8 Mar 2017 20:04:13 +1100
4 Subject: [PATCH] Add support for the AudioInjector.net Octo sound card
5
6 AudioInjector Octo: sample rates, regulators, reset
7
8 This patch adds new sample rates to the Audioinjector Octo sound card. The
9 new supported rates are (in kHz) :
10 96, 48, 32, 24, 16, 8, 88.2, 44.1, 29.4, 22.05, 14.7
11
12 Reference the bcm270x DT regulators in the overlay.
13
14 This patch adds a reset GPIO for the AudioInjector.net octo sound card.
15
16 Audioinjector octo : Make the playback and capture symmetric
17
18 This patch ensures that the sample rate and channel count of the audioinjector
19 octo sound card are symmetric.
20
21 audioinjector-octo: Add continuous clock feature
22
23 By user request, add a switch to prevent the clocks being stopped when
24 the stream is paused, stopped or shutdown. Provide access to the switch
25 by adding a 'non-stop-clocks' parameter to the audioinjector-addons
26 overlay.
27
28 See: https://github.com/raspberrypi/linux/issues/2409
29
30 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
31
32 sound: Fixes for audioinjector-octo under 4.19
33
34 1. Move the DT alias declaration to the I2C shim in the cases
35 where the shim is enabled. This works around a problem caused by a
36 4.19 commit [1] that generates DT/OF uevents for I2C drivers.
37
38 2. Fix the diagnostics in an error path of the soundcard driver to
39 correctly identify the reason for the failure to load.
40
41 3. Move the declaration of the clock node in the overlay outside
42 the I2C node to avoid warnings.
43
44 4. Sort the overlay nodes so that dependencies are only to earlier
45 fragments, in an attempt to get runtime dtoverlay application to
46 work (it still doesn't...)
47
48 See: https://github.com/Audio-Injector/Octo/issues/14
49 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
50
51 [1] af503716ac14 ("i2c: core: report OF style module alias for devices registered via OF")
52
53 ASoC: audioinjector-octo-soundcard: use modern dai_link style
54
55 Signed-off-by: Hui Wang <hui.wang@canonical.com>
56 ---
57 sound/soc/bcm/audioinjector-octo-soundcard.c | 341 +++++++++++++++++++
58 sound/soc/codecs/cs42xx8-i2c.c | 7 +
59 sound/soc/codecs/cs42xx8.c | 2 +
60 3 files changed, 350 insertions(+)
61 create mode 100644 sound/soc/bcm/audioinjector-octo-soundcard.c
62
63 --- /dev/null
64 +++ b/sound/soc/bcm/audioinjector-octo-soundcard.c
65 @@ -0,0 +1,341 @@
66 +/*
67 + * ASoC Driver for AudioInjector Pi octo channel soundcard (hat)
68 + *
69 + * Created on: 27-October-2016
70 + * Author: flatmax@flatmax.org
71 + * based on audioinjector-pi-soundcard.c
72 + *
73 + * Copyright (C) 2016 Flatmax Pty. Ltd.
74 + *
75 + * This program is free software; you can redistribute it and/or
76 + * modify it under the terms of the GNU General Public License
77 + * version 2 as published by the Free Software Foundation.
78 + *
79 + * This program is distributed in the hope that it will be useful, but
80 + * WITHOUT ANY WARRANTY; without even the implied warranty of
81 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
82 + * General Public License for more details.
83 + */
84 +
85 +#include <linux/module.h>
86 +#include <linux/types.h>
87 +#include <linux/gpio/consumer.h>
88 +
89 +#include <sound/core.h>
90 +#include <sound/soc.h>
91 +#include <sound/pcm_params.h>
92 +#include <sound/control.h>
93 +
94 +static struct gpio_descs *mult_gpios;
95 +static struct gpio_desc *codec_rst_gpio;
96 +static unsigned int audioinjector_octo_rate;
97 +static bool non_stop_clocks;
98 +
99 +static const unsigned int audioinjector_octo_rates[] = {
100 + 96000, 48000, 32000, 24000, 16000, 8000, 88200, 44100, 29400, 22050, 14700,
101 +};
102 +
103 +static struct snd_pcm_hw_constraint_list audioinjector_octo_constraints = {
104 + .list = audioinjector_octo_rates,
105 + .count = ARRAY_SIZE(audioinjector_octo_rates),
106 +};
107 +
108 +static int audioinjector_octo_dai_init(struct snd_soc_pcm_runtime *rtd)
109 +{
110 + return snd_soc_dai_set_bclk_ratio(rtd->cpu_dai, 64);
111 +}
112 +
113 +static int audioinjector_octo_startup(struct snd_pcm_substream *substream)
114 +{
115 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
116 + rtd->cpu_dai->driver->playback.channels_min = 8;
117 + rtd->cpu_dai->driver->playback.channels_max = 8;
118 + rtd->cpu_dai->driver->capture.channels_min = 8;
119 + rtd->cpu_dai->driver->capture.channels_max = 8;
120 + rtd->codec_dai->driver->capture.channels_max = 8;
121 +
122 + snd_pcm_hw_constraint_list(substream->runtime, 0,
123 + SNDRV_PCM_HW_PARAM_RATE,
124 + &audioinjector_octo_constraints);
125 +
126 + return 0;
127 +}
128 +
129 +static void audioinjector_octo_shutdown(struct snd_pcm_substream *substream)
130 +{
131 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
132 + rtd->cpu_dai->driver->playback.channels_min = 2;
133 + rtd->cpu_dai->driver->playback.channels_max = 2;
134 + rtd->cpu_dai->driver->capture.channels_min = 2;
135 + rtd->cpu_dai->driver->capture.channels_max = 2;
136 + rtd->codec_dai->driver->capture.channels_max = 6;
137 +}
138 +
139 +static int audioinjector_octo_hw_params(struct snd_pcm_substream *substream,
140 + struct snd_pcm_hw_params *params)
141 +{
142 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
143 +
144 + // set codec DAI configuration
145 + int ret = snd_soc_dai_set_fmt(rtd->codec_dai,
146 + SND_SOC_DAIFMT_CBS_CFS|SND_SOC_DAIFMT_DSP_A|
147 + SND_SOC_DAIFMT_NB_NF);
148 + if (ret < 0)
149 + return ret;
150 +
151 + // set cpu DAI configuration
152 + ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
153 + SND_SOC_DAIFMT_CBM_CFM|SND_SOC_DAIFMT_I2S|
154 + SND_SOC_DAIFMT_NB_NF);
155 + if (ret < 0)
156 + return ret;
157 +
158 + audioinjector_octo_rate = params_rate(params);
159 +
160 + // Set the correct sysclock for the codec
161 + switch (audioinjector_octo_rate) {
162 + case 96000:
163 + case 48000:
164 + return snd_soc_dai_set_sysclk(rtd->codec_dai, 0, 49152000,
165 + 0);
166 + break;
167 + case 24000:
168 + return snd_soc_dai_set_sysclk(rtd->codec_dai, 0, 49152000/2,
169 + 0);
170 + break;
171 + case 32000:
172 + case 16000:
173 + return snd_soc_dai_set_sysclk(rtd->codec_dai, 0, 49152000/3,
174 + 0);
175 + break;
176 + case 8000:
177 + return snd_soc_dai_set_sysclk(rtd->codec_dai, 0, 49152000/6,
178 + 0);
179 + break;
180 + case 88200:
181 + case 44100:
182 + return snd_soc_dai_set_sysclk(rtd->codec_dai, 0, 45185400,
183 + 0);
184 + break;
185 + case 22050:
186 + return snd_soc_dai_set_sysclk(rtd->codec_dai, 0, 45185400/2,
187 + 0);
188 + break;
189 + case 29400:
190 + case 14700:
191 + return snd_soc_dai_set_sysclk(rtd->codec_dai, 0, 45185400/3,
192 + 0);
193 + break;
194 + default:
195 + return -EINVAL;
196 + }
197 +}
198 +
199 +static int audioinjector_octo_trigger(struct snd_pcm_substream *substream,
200 + int cmd){
201 + DECLARE_BITMAP(mult, 4);
202 +
203 + memset(mult, 0, sizeof(mult));
204 +
205 + switch (cmd) {
206 + case SNDRV_PCM_TRIGGER_STOP:
207 + case SNDRV_PCM_TRIGGER_SUSPEND:
208 + case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
209 + if (!non_stop_clocks)
210 + break;
211 + /* Drop through... */
212 + case SNDRV_PCM_TRIGGER_START:
213 + case SNDRV_PCM_TRIGGER_RESUME:
214 + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
215 + switch (audioinjector_octo_rate) {
216 + case 96000:
217 + __assign_bit(3, mult, 1);
218 + case 88200:
219 + __assign_bit(1, mult, 1);
220 + __assign_bit(2, mult, 1);
221 + break;
222 + case 48000:
223 + __assign_bit(3, mult, 1);
224 + case 44100:
225 + __assign_bit(2, mult, 1);
226 + break;
227 + case 32000:
228 + __assign_bit(3, mult, 1);
229 + case 29400:
230 + __assign_bit(0, mult, 1);
231 + __assign_bit(1, mult, 1);
232 + break;
233 + case 24000:
234 + __assign_bit(3, mult, 1);
235 + case 22050:
236 + __assign_bit(1, mult, 1);
237 + break;
238 + case 16000:
239 + __assign_bit(3, mult, 1);
240 + case 14700:
241 + __assign_bit(0, mult, 1);
242 + break;
243 + case 8000:
244 + __assign_bit(3, mult, 1);
245 + break;
246 + default:
247 + return -EINVAL;
248 + }
249 + break;
250 + default:
251 + return -EINVAL;
252 + }
253 + gpiod_set_array_value_cansleep(mult_gpios->ndescs, mult_gpios->desc,
254 + NULL, mult);
255 +
256 + return 0;
257 +}
258 +
259 +static struct snd_soc_ops audioinjector_octo_ops = {
260 + .startup = audioinjector_octo_startup,
261 + .shutdown = audioinjector_octo_shutdown,
262 + .hw_params = audioinjector_octo_hw_params,
263 + .trigger = audioinjector_octo_trigger,
264 +};
265 +
266 +SND_SOC_DAILINK_DEFS(audioinjector_octo,
267 + DAILINK_COMP_ARRAY(COMP_EMPTY()),
268 + DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "cs42448")),
269 + DAILINK_COMP_ARRAY(COMP_EMPTY()));
270 +
271 +static struct snd_soc_dai_link audioinjector_octo_dai[] = {
272 + {
273 + .name = "AudioInjector Octo",
274 + .stream_name = "AudioInject-HIFI",
275 + .ops = &audioinjector_octo_ops,
276 + .init = audioinjector_octo_dai_init,
277 + .symmetric_rates = 1,
278 + .symmetric_channels = 1,
279 + SND_SOC_DAILINK_REG(audioinjector_octo),
280 + },
281 +};
282 +
283 +static const struct snd_soc_dapm_widget audioinjector_octo_widgets[] = {
284 + SND_SOC_DAPM_OUTPUT("OUTPUTS0"),
285 + SND_SOC_DAPM_OUTPUT("OUTPUTS1"),
286 + SND_SOC_DAPM_OUTPUT("OUTPUTS2"),
287 + SND_SOC_DAPM_OUTPUT("OUTPUTS3"),
288 + SND_SOC_DAPM_INPUT("INPUTS0"),
289 + SND_SOC_DAPM_INPUT("INPUTS1"),
290 + SND_SOC_DAPM_INPUT("INPUTS2"),
291 +};
292 +
293 +static const struct snd_soc_dapm_route audioinjector_octo_route[] = {
294 + /* Balanced outputs */
295 + {"OUTPUTS0", NULL, "AOUT1L"},
296 + {"OUTPUTS0", NULL, "AOUT1R"},
297 + {"OUTPUTS1", NULL, "AOUT2L"},
298 + {"OUTPUTS1", NULL, "AOUT2R"},
299 + {"OUTPUTS2", NULL, "AOUT3L"},
300 + {"OUTPUTS2", NULL, "AOUT3R"},
301 + {"OUTPUTS3", NULL, "AOUT4L"},
302 + {"OUTPUTS3", NULL, "AOUT4R"},
303 +
304 + /* Balanced inputs */
305 + {"AIN1L", NULL, "INPUTS0"},
306 + {"AIN1R", NULL, "INPUTS0"},
307 + {"AIN2L", NULL, "INPUTS1"},
308 + {"AIN2R", NULL, "INPUTS1"},
309 + {"AIN3L", NULL, "INPUTS2"},
310 + {"AIN3R", NULL, "INPUTS2"},
311 +};
312 +
313 +static struct snd_soc_card snd_soc_audioinjector_octo = {
314 + .name = "audioinjector-octo-soundcard",
315 + .dai_link = audioinjector_octo_dai,
316 + .num_links = ARRAY_SIZE(audioinjector_octo_dai),
317 +
318 + .dapm_widgets = audioinjector_octo_widgets,
319 + .num_dapm_widgets = ARRAY_SIZE(audioinjector_octo_widgets),
320 + .dapm_routes = audioinjector_octo_route,
321 + .num_dapm_routes = ARRAY_SIZE(audioinjector_octo_route),
322 +};
323 +
324 +static int audioinjector_octo_probe(struct platform_device *pdev)
325 +{
326 + struct snd_soc_card *card = &snd_soc_audioinjector_octo;
327 + int ret;
328 +
329 + card->dev = &pdev->dev;
330 +
331 + if (pdev->dev.of_node) {
332 + struct snd_soc_dai_link *dai = &audioinjector_octo_dai[0];
333 + struct device_node *i2s_node =
334 + of_parse_phandle(pdev->dev.of_node,
335 + "i2s-controller", 0);
336 + struct device_node *codec_node =
337 + of_parse_phandle(pdev->dev.of_node,
338 + "codec", 0);
339 +
340 + mult_gpios = devm_gpiod_get_array_optional(&pdev->dev, "mult",
341 + GPIOD_OUT_LOW);
342 + if (IS_ERR(mult_gpios))
343 + return PTR_ERR(mult_gpios);
344 +
345 + codec_rst_gpio = devm_gpiod_get_optional(&pdev->dev, "reset",
346 + GPIOD_OUT_LOW);
347 + if (IS_ERR(codec_rst_gpio))
348 + return PTR_ERR(codec_rst_gpio);
349 +
350 + non_stop_clocks = of_property_read_bool(pdev->dev.of_node, "non-stop-clocks");
351 +
352 + if (codec_rst_gpio)
353 + gpiod_set_value(codec_rst_gpio, 1);
354 + msleep(500);
355 + if (codec_rst_gpio)
356 + gpiod_set_value(codec_rst_gpio, 0);
357 + msleep(500);
358 + if (codec_rst_gpio)
359 + gpiod_set_value(codec_rst_gpio, 1);
360 + msleep(500);
361 +
362 + if (i2s_node && codec_node) {
363 + dai->cpus->dai_name = NULL;
364 + dai->cpus->of_node = i2s_node;
365 + dai->platforms->name = NULL;
366 + dai->platforms->of_node = i2s_node;
367 + dai->codecs->name = NULL;
368 + dai->codecs->of_node = codec_node;
369 + } else
370 + if (!i2s_node) {
371 + dev_err(&pdev->dev,
372 + "i2s-controller missing or invalid in DT\n");
373 + return -EINVAL;
374 + } else {
375 + dev_err(&pdev->dev,
376 + "Property 'codec' missing or invalid\n");
377 + return -EINVAL;
378 + }
379 + }
380 +
381 + ret = devm_snd_soc_register_card(&pdev->dev, card);
382 + if (ret != 0)
383 + dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
384 + return ret;
385 +}
386 +
387 +static const struct of_device_id audioinjector_octo_of_match[] = {
388 + { .compatible = "ai,audioinjector-octo-soundcard", },
389 + {},
390 +};
391 +MODULE_DEVICE_TABLE(of, audioinjector_octo_of_match);
392 +
393 +static struct platform_driver audioinjector_octo_driver = {
394 + .driver = {
395 + .name = "audioinjector-octo",
396 + .owner = THIS_MODULE,
397 + .of_match_table = audioinjector_octo_of_match,
398 + },
399 + .probe = audioinjector_octo_probe,
400 +};
401 +
402 +module_platform_driver(audioinjector_octo_driver);
403 +MODULE_AUTHOR("Matt Flax <flatmax@flatmax.org>");
404 +MODULE_DESCRIPTION("AudioInjector.net octo Soundcard");
405 +MODULE_LICENSE("GPL v2");
406 +MODULE_ALIAS("platform:audioinjector-octo-soundcard");
407 --- a/sound/soc/codecs/cs42xx8-i2c.c
408 +++ b/sound/soc/codecs/cs42xx8-i2c.c
409 @@ -45,6 +45,13 @@ static struct i2c_device_id cs42xx8_i2c_
410 };
411 MODULE_DEVICE_TABLE(i2c, cs42xx8_i2c_id);
412
413 +const struct of_device_id cs42xx8_of_match[] = {
414 + { .compatible = "cirrus,cs42448", .data = &cs42448_data, },
415 + { .compatible = "cirrus,cs42888", .data = &cs42888_data, },
416 + { /* sentinel */ }
417 +};
418 +MODULE_DEVICE_TABLE(of, cs42xx8_of_match);
419 +
420 static struct i2c_driver cs42xx8_i2c_driver = {
421 .driver = {
422 .name = "cs42xx8",
423 --- a/sound/soc/codecs/cs42xx8.c
424 +++ b/sound/soc/codecs/cs42xx8.c
425 @@ -516,8 +516,10 @@ const struct of_device_id cs42xx8_of_mat
426 { .compatible = "cirrus,cs42888", .data = &cs42888_data, },
427 { /* sentinel */ }
428 };
429 +#if !IS_ENABLED(CONFIG_SND_SOC_CS42XX8_I2C)
430 MODULE_DEVICE_TABLE(of, cs42xx8_of_match);
431 EXPORT_SYMBOL_GPL(cs42xx8_of_match);
432 +#endif
433
434 int cs42xx8_probe(struct device *dev, struct regmap *regmap)
435 {