d3964839855bc3da20b464749348af769c7c0f00
[openwrt/staging/jow.git] /
1 From 9dc67d61a5cd72eb80482aa1540e08a51e37172c 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 086/454] 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 sound/soc/bcm/Kconfig | 7 +
22 sound/soc/bcm/Makefile | 2 +
23 sound/soc/bcm/audioinjector-octo-soundcard.c | 341 +++++++++++++++++++
24 3 files changed, 350 insertions(+)
25 create mode 100644 sound/soc/bcm/audioinjector-octo-soundcard.c
26
27 --- a/sound/soc/bcm/Kconfig
28 +++ b/sound/soc/bcm/Kconfig
29 @@ -119,6 +119,13 @@ config SND_AUDIOINJECTOR_PI_SOUNDCARD
30 help
31 Say Y or M if you want to add support for audioinjector.net Pi Hat
32
33 +config SND_AUDIOINJECTOR_OCTO_SOUNDCARD
34 + tristate "Support for audioinjector.net Octo channel (Hat) soundcard"
35 + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
36 + select SND_SOC_CS42XX8_I2C
37 + help
38 + Say Y or M if you want to add support for audioinjector.net octo add on
39 +
40 config SND_DIGIDAC1_SOUNDCARD
41 tristate "Support for Red Rocks Audio DigiDAC1"
42 depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
43 --- a/sound/soc/bcm/Makefile
44 +++ b/sound/soc/bcm/Makefile
45 @@ -23,6 +23,7 @@ snd-soc-iqaudio-dac-objs := iqaudio-dac.
46 snd-soc-iqaudio-digi-objs := iqaudio_digi.o
47 snd-soc-raspidac3-objs := raspidac3.o
48 snd-soc-audioinjector-pi-soundcard-objs := audioinjector-pi-soundcard.o
49 +snd-soc-audioinjector-octo-soundcard-objs := audioinjector-octo-soundcard.o
50 snd-soc-digidac1-soundcard-objs := digidac1-soundcard.o
51 snd-soc-dionaudio-loco-objs := dionaudio_loco.o
52 snd-soc-dionaudio-loco-v2-objs := dionaudio_loco-v2.o
53 @@ -46,6 +47,7 @@ obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC
54 obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_DIGI) += snd-soc-iqaudio-digi.o
55 obj-$(CONFIG_SND_BCM2708_SOC_RASPIDAC3) += snd-soc-raspidac3.o
56 obj-$(CONFIG_SND_AUDIOINJECTOR_PI_SOUNDCARD) += snd-soc-audioinjector-pi-soundcard.o
57 +obj-$(CONFIG_SND_AUDIOINJECTOR_OCTO_SOUNDCARD) += snd-soc-audioinjector-octo-soundcard.o
58 obj-$(CONFIG_SND_DIGIDAC1_SOUNDCARD) += snd-soc-digidac1-soundcard.o
59 obj-$(CONFIG_SND_BCM2708_SOC_DIONAUDIO_LOCO) += snd-soc-dionaudio-loco.o
60 obj-$(CONFIG_SND_BCM2708_SOC_DIONAUDIO_LOCO_V2) += snd-soc-dionaudio-loco-v2.o
61 --- /dev/null
62 +++ b/sound/soc/bcm/audioinjector-octo-soundcard.c
63 @@ -0,0 +1,341 @@
64 +/*
65 + * ASoC Driver for AudioInjector Pi octo channel soundcard (hat)
66 + *
67 + * Created on: 27-October-2016
68 + * Author: flatmax@flatmax.org
69 + * based on audioinjector-pi-soundcard.c
70 + *
71 + * Copyright (C) 2016 Flatmax Pty. Ltd.
72 + *
73 + * This program is free software; you can redistribute it and/or
74 + * modify it under the terms of the GNU General Public License
75 + * version 2 as published by the Free Software Foundation.
76 + *
77 + * This program is distributed in the hope that it will be useful, but
78 + * WITHOUT ANY WARRANTY; without even the implied warranty of
79 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
80 + * General Public License for more details.
81 + */
82 +
83 +#include <linux/module.h>
84 +#include <linux/types.h>
85 +#include <linux/gpio/consumer.h>
86 +
87 +#include <sound/core.h>
88 +#include <sound/soc.h>
89 +#include <sound/pcm_params.h>
90 +#include <sound/control.h>
91 +
92 +static struct gpio_descs *mult_gpios;
93 +static struct gpio_desc *codec_rst_gpio;
94 +static unsigned int audioinjector_octo_rate;
95 +
96 +static const unsigned int audioinjector_octo_rates[] = {
97 + 96000, 48000, 32000, 24000, 16000, 8000, 88200, 44100, 29400, 22050, 14700,
98 +};
99 +
100 +static struct snd_pcm_hw_constraint_list audioinjector_octo_constraints = {
101 + .list = audioinjector_octo_rates,
102 + .count = ARRAY_SIZE(audioinjector_octo_rates),
103 +};
104 +
105 +static int audioinjector_octo_dai_init(struct snd_soc_pcm_runtime *rtd)
106 +{
107 + return snd_soc_dai_set_bclk_ratio(rtd->cpu_dai, 64);
108 +}
109 +
110 +static int audioinjector_octo_startup(struct snd_pcm_substream *substream)
111 +{
112 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
113 + rtd->cpu_dai->driver->playback.channels_min = 8;
114 + rtd->cpu_dai->driver->playback.channels_max = 8;
115 + rtd->cpu_dai->driver->capture.channels_min = 8;
116 + rtd->cpu_dai->driver->capture.channels_max = 8;
117 + rtd->codec_dai->driver->capture.channels_max = 8;
118 +
119 + snd_pcm_hw_constraint_list(substream->runtime, 0,
120 + SNDRV_PCM_HW_PARAM_RATE,
121 + &audioinjector_octo_constraints);
122 +
123 + return 0;
124 +}
125 +
126 +static void audioinjector_octo_shutdown(struct snd_pcm_substream *substream)
127 +{
128 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
129 + rtd->cpu_dai->driver->playback.channels_min = 2;
130 + rtd->cpu_dai->driver->playback.channels_max = 2;
131 + rtd->cpu_dai->driver->capture.channels_min = 2;
132 + rtd->cpu_dai->driver->capture.channels_max = 2;
133 + rtd->codec_dai->driver->capture.channels_max = 6;
134 +}
135 +
136 +static int audioinjector_octo_hw_params(struct snd_pcm_substream *substream,
137 + struct snd_pcm_hw_params *params)
138 +{
139 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
140 +
141 + // set codec DAI configuration
142 + int ret = snd_soc_dai_set_fmt(rtd->codec_dai,
143 + SND_SOC_DAIFMT_CBS_CFS|SND_SOC_DAIFMT_DSP_A|
144 + SND_SOC_DAIFMT_NB_NF);
145 + if (ret < 0)
146 + return ret;
147 +
148 + // set cpu DAI configuration
149 + ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
150 + SND_SOC_DAIFMT_CBM_CFM|SND_SOC_DAIFMT_I2S|
151 + SND_SOC_DAIFMT_NB_NF);
152 + if (ret < 0)
153 + return ret;
154 +
155 + audioinjector_octo_rate = params_rate(params);
156 +
157 + // Set the correct sysclock for the codec
158 + switch (audioinjector_octo_rate) {
159 + case 96000:
160 + case 48000:
161 + return snd_soc_dai_set_sysclk(rtd->codec_dai, 0, 49152000,
162 + 0);
163 + break;
164 + case 24000:
165 + return snd_soc_dai_set_sysclk(rtd->codec_dai, 0, 49152000/2,
166 + 0);
167 + break;
168 + case 32000:
169 + case 16000:
170 + return snd_soc_dai_set_sysclk(rtd->codec_dai, 0, 49152000/3,
171 + 0);
172 + break;
173 + case 8000:
174 + return snd_soc_dai_set_sysclk(rtd->codec_dai, 0, 49152000/6,
175 + 0);
176 + break;
177 + case 88200:
178 + case 44100:
179 + return snd_soc_dai_set_sysclk(rtd->codec_dai, 0, 45185400,
180 + 0);
181 + break;
182 + case 22050:
183 + return snd_soc_dai_set_sysclk(rtd->codec_dai, 0, 45185400/2,
184 + 0);
185 + break;
186 + case 29400:
187 + case 14700:
188 + return snd_soc_dai_set_sysclk(rtd->codec_dai, 0, 45185400/3,
189 + 0);
190 + break;
191 + default:
192 + return -EINVAL;
193 + }
194 +}
195 +
196 +static int audioinjector_octo_trigger(struct snd_pcm_substream *substream,
197 + int cmd){
198 + int mult[4];
199 + mult[0] = 0;
200 + mult[1] = 0;
201 + mult[2] = 0;
202 + mult[3] = 0;
203 +
204 + switch (cmd) {
205 + case SNDRV_PCM_TRIGGER_START:
206 + case SNDRV_PCM_TRIGGER_RESUME:
207 + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
208 + switch (audioinjector_octo_rate) {
209 + case 96000:
210 + mult[3] = 1;
211 + case 88200:
212 + mult[1] = 1;
213 + mult[2] = 1;
214 + break;
215 + case 48000:
216 + mult[3] = 1;
217 + case 44100:
218 + mult[2] = 1;
219 + break;
220 + case 32000:
221 + mult[3] = 1;
222 + case 29400:
223 + mult[0] = 1;
224 + mult[1] = 1;
225 + break;
226 + case 24000:
227 + mult[3] = 1;
228 + case 22050:
229 + mult[1] = 1;
230 + break;
231 + case 16000:
232 + mult[3] = 1;
233 + case 14700:
234 + mult[0] = 1;
235 + break;
236 + case 8000:
237 + mult[3] = 1;
238 + break;
239 + default:
240 + return -EINVAL;
241 + }
242 + break;
243 + case SNDRV_PCM_TRIGGER_STOP:
244 + case SNDRV_PCM_TRIGGER_SUSPEND:
245 + case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
246 + break;
247 + default:
248 + return -EINVAL;
249 + }
250 + gpiod_set_array_value_cansleep(mult_gpios->ndescs, mult_gpios->desc,
251 + mult);
252 +
253 + return 0;
254 +}
255 +
256 +static struct snd_soc_ops audioinjector_octo_ops = {
257 + .startup = audioinjector_octo_startup,
258 + .shutdown = audioinjector_octo_shutdown,
259 + .hw_params = audioinjector_octo_hw_params,
260 + .trigger = audioinjector_octo_trigger,
261 +};
262 +
263 +static struct snd_soc_dai_link audioinjector_octo_dai[] = {
264 + {
265 + .name = "AudioInjector Octo",
266 + .stream_name = "AudioInject-HIFI",
267 + .codec_dai_name = "cs42448",
268 + .ops = &audioinjector_octo_ops,
269 + .init = audioinjector_octo_dai_init,
270 + .symmetric_rates = 1,
271 + .symmetric_channels = 1,
272 + },
273 +};
274 +
275 +static const struct snd_soc_dapm_widget audioinjector_octo_widgets[] = {
276 + SND_SOC_DAPM_OUTPUT("OUTPUTS0"),
277 + SND_SOC_DAPM_OUTPUT("OUTPUTS1"),
278 + SND_SOC_DAPM_OUTPUT("OUTPUTS2"),
279 + SND_SOC_DAPM_OUTPUT("OUTPUTS3"),
280 + SND_SOC_DAPM_INPUT("INPUTS0"),
281 + SND_SOC_DAPM_INPUT("INPUTS1"),
282 + SND_SOC_DAPM_INPUT("INPUTS2"),
283 +};
284 +
285 +static const struct snd_soc_dapm_route audioinjector_octo_route[] = {
286 + /* Balanced outputs */
287 + {"OUTPUTS0", NULL, "AOUT1L"},
288 + {"OUTPUTS0", NULL, "AOUT1R"},
289 + {"OUTPUTS1", NULL, "AOUT2L"},
290 + {"OUTPUTS1", NULL, "AOUT2R"},
291 + {"OUTPUTS2", NULL, "AOUT3L"},
292 + {"OUTPUTS2", NULL, "AOUT3R"},
293 + {"OUTPUTS3", NULL, "AOUT4L"},
294 + {"OUTPUTS3", NULL, "AOUT4R"},
295 +
296 + /* Balanced inputs */
297 + {"AIN1L", NULL, "INPUTS0"},
298 + {"AIN1R", NULL, "INPUTS0"},
299 + {"AIN2L", NULL, "INPUTS1"},
300 + {"AIN2R", NULL, "INPUTS1"},
301 + {"AIN3L", NULL, "INPUTS2"},
302 + {"AIN3R", NULL, "INPUTS2"},
303 +};
304 +
305 +static struct snd_soc_card snd_soc_audioinjector_octo = {
306 + .name = "audioinjector-octo-soundcard",
307 + .dai_link = audioinjector_octo_dai,
308 + .num_links = ARRAY_SIZE(audioinjector_octo_dai),
309 +
310 + .dapm_widgets = audioinjector_octo_widgets,
311 + .num_dapm_widgets = ARRAY_SIZE(audioinjector_octo_widgets),
312 + .dapm_routes = audioinjector_octo_route,
313 + .num_dapm_routes = ARRAY_SIZE(audioinjector_octo_route),
314 +};
315 +
316 +static int audioinjector_octo_probe(struct platform_device *pdev)
317 +{
318 + struct snd_soc_card *card = &snd_soc_audioinjector_octo;
319 + int ret;
320 +
321 + card->dev = &pdev->dev;
322 +
323 + if (pdev->dev.of_node) {
324 + struct snd_soc_dai_link *dai = &audioinjector_octo_dai[0];
325 + struct device_node *i2s_node =
326 + of_parse_phandle(pdev->dev.of_node,
327 + "i2s-controller", 0);
328 + struct device_node *codec_node =
329 + of_parse_phandle(pdev->dev.of_node,
330 + "codec", 0);
331 +
332 + mult_gpios = devm_gpiod_get_array_optional(&pdev->dev, "mult",
333 + GPIOD_OUT_LOW);
334 + if (IS_ERR(mult_gpios))
335 + return PTR_ERR(mult_gpios);
336 +
337 + codec_rst_gpio = devm_gpiod_get_optional(&pdev->dev, "reset",
338 + GPIOD_OUT_LOW);
339 + if (IS_ERR(codec_rst_gpio))
340 + return PTR_ERR(codec_rst_gpio);
341 +
342 + if (codec_rst_gpio)
343 + gpiod_set_value(codec_rst_gpio, 1);
344 + msleep(500);
345 + if (codec_rst_gpio)
346 + gpiod_set_value(codec_rst_gpio, 0);
347 + msleep(500);
348 + if (codec_rst_gpio)
349 + gpiod_set_value(codec_rst_gpio, 1);
350 + msleep(500);
351 +
352 + if (i2s_node && codec_node) {
353 + dai->cpu_dai_name = NULL;
354 + dai->cpu_of_node = i2s_node;
355 + dai->platform_name = NULL;
356 + dai->platform_of_node = i2s_node;
357 + dai->codec_name = NULL;
358 + dai->codec_of_node = codec_node;
359 + } else
360 + if (!dai->cpu_of_node) {
361 + dev_err(&pdev->dev,
362 + "i2s-controller missing or invalid in DT\n");
363 + return -EINVAL;
364 + } else {
365 + dev_err(&pdev->dev,
366 + "Property 'codec' missing or invalid\n");
367 + return -EINVAL;
368 + }
369 + }
370 +
371 + ret = snd_soc_register_card(card);
372 + if (ret != 0)
373 + dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
374 + return ret;
375 +}
376 +
377 +static int audioinjector_octo_remove(struct platform_device *pdev)
378 +{
379 + struct snd_soc_card *card = platform_get_drvdata(pdev);
380 +
381 + return snd_soc_unregister_card(card);
382 +}
383 +
384 +static const struct of_device_id audioinjector_octo_of_match[] = {
385 + { .compatible = "ai,audioinjector-octo-soundcard", },
386 + {},
387 +};
388 +MODULE_DEVICE_TABLE(of, audioinjector_octo_of_match);
389 +
390 +static struct platform_driver audioinjector_octo_driver = {
391 + .driver = {
392 + .name = "audioinjector-octo",
393 + .owner = THIS_MODULE,
394 + .of_match_table = audioinjector_octo_of_match,
395 + },
396 + .probe = audioinjector_octo_probe,
397 + .remove = audioinjector_octo_remove,
398 +};
399 +
400 +module_platform_driver(audioinjector_octo_driver);
401 +MODULE_AUTHOR("Matt Flax <flatmax@flatmax.org>");
402 +MODULE_DESCRIPTION("AudioInjector.net octo Soundcard");
403 +MODULE_LICENSE("GPL v2");
404 +MODULE_ALIAS("platform:audioinjector-octo-soundcard");