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