81ca0b52d1f57ea6ed56f36c78ef183ca504a394
[openwrt/staging/luka.git] /
1 From ef421b47c67af36f31730fe5e6088cfb672369ea Mon Sep 17 00:00:00 2001
2 From: Matt Flax <flatmax@flatmax.org>
3 Date: Mon, 16 May 2016 21:36:31 +1000
4 Subject: [PATCH 075/454] New AudioInjector.net Pi soundcard with low jitter
5 audio in and out.
6
7 Contains the sound/soc/bcm ALSA machine driver and necessary alterations to the Kconfig and Makefile.
8 Adds the dts overlay and updates the Makefile and README.
9 Updates the relevant defconfig files to enable building for the Raspberry Pi.
10 Thanks to Phil Elwell (pelwell) for the review, simple-card concepts and discussion. Thanks to Clive Messer for overlay naming suggestions.
11
12 Added support for headphones, microphone and bclk_ratio settings.
13
14 This patch adds headphone and microphone capability to the Audio Injector sound card. The patch also sets the bit clock ratio for use in the bcm2835-i2s driver. The bcm2835-i2s can't handle an 8 kHz sample rate when the bit clock is at 12 MHz because its register is only 10 bits wide which can't represent the ch2 offset of 1508. For that reason, the rate constraint is added.
15 ---
16 sound/soc/bcm/Kconfig | 7 +
17 sound/soc/bcm/Makefile | 3 +
18 sound/soc/bcm/audioinjector-pi-soundcard.c | 193 +++++++++++++++++++++
19 3 files changed, 203 insertions(+)
20 create mode 100644 sound/soc/bcm/audioinjector-pi-soundcard.c
21
22 --- a/sound/soc/bcm/Kconfig
23 +++ b/sound/soc/bcm/Kconfig
24 @@ -95,3 +95,10 @@ config SND_BCM2708_SOC_ADAU1977_ADC
25 select SND_SOC_ADAU1977_I2C
26 help
27 Say Y or M if you want to add support for ADAU1977 ADC.
28 +
29 +config SND_AUDIOINJECTOR_PI_SOUNDCARD
30 + tristate "Support for audioinjector.net Pi add on soundcard"
31 + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
32 + select SND_SOC_WM8731
33 + help
34 + Say Y or M if you want to add support for audioinjector.net Pi Hat
35 --- a/sound/soc/bcm/Makefile
36 +++ b/sound/soc/bcm/Makefile
37 @@ -20,6 +20,7 @@ snd-soc-rpi-dac-objs := rpi-dac.o
38 snd-soc-rpi-proto-objs := rpi-proto.o
39 snd-soc-iqaudio-dac-objs := iqaudio-dac.o
40 snd-soc-raspidac3-objs := raspidac3.o
41 +snd-soc-audioinjector-pi-soundcard-objs := audioinjector-pi-soundcard.o
42
43 obj-$(CONFIG_SND_BCM2708_SOC_ADAU1977_ADC) += snd-soc-adau1977-adc.o
44 obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_AMP) += snd-soc-hifiberry-amp.o
45 @@ -32,3 +33,5 @@ obj-$(CONFIG_SND_BCM2708_SOC_RPI_DAC) +=
46 obj-$(CONFIG_SND_BCM2708_SOC_RPI_PROTO) += snd-soc-rpi-proto.o
47 obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) += snd-soc-iqaudio-dac.o
48 obj-$(CONFIG_SND_BCM2708_SOC_RASPIDAC3) += snd-soc-raspidac3.o
49 +obj-$(CONFIG_SND_AUDIOINJECTOR_PI_SOUNDCARD) += snd-soc-audioinjector-pi-soundcard.o
50 +
51 --- /dev/null
52 +++ b/sound/soc/bcm/audioinjector-pi-soundcard.c
53 @@ -0,0 +1,193 @@
54 +/*
55 + * ASoC Driver for AudioInjector Pi add on soundcard
56 + *
57 + * Created on: 13-May-2016
58 + * Author: flatmax@flatmax.org
59 + * based on code by Cliff Cai <Cliff.Cai@analog.com> for the ssm2602 machine blackfin.
60 + * with help from Lars-Peter Clausen for simplifying the original code to use the dai_fmt field.
61 + * i2s_node code taken from the other sound/soc/bcm machine drivers.
62 + *
63 + * Copyright (C) 2016 Flatmax Pty. Ltd.
64 + *
65 + * This program is free software; you can redistribute it and/or
66 + * modify it under the terms of the GNU General Public License
67 + * version 2 as published by the Free Software Foundation.
68 + *
69 + * This program is distributed in the hope that it will be useful, but
70 + * WITHOUT ANY WARRANTY; without even the implied warranty of
71 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
72 + * General Public License for more details.
73 + */
74 +
75 +#include <linux/module.h>
76 +#include <linux/types.h>
77 +
78 +#include <sound/core.h>
79 +#include <sound/soc.h>
80 +#include <sound/pcm_params.h>
81 +#include <sound/control.h>
82 +
83 +#include "../codecs/wm8731.h"
84 +
85 +static const unsigned int bcm2835_rates_12000000[] = {
86 + 8000, 16000, 32000, 44100, 48000, 96000, 88200,
87 +};
88 +
89 +static struct snd_pcm_hw_constraint_list bcm2835_constraints_12000000 = {
90 + .list = bcm2835_rates_12000000,
91 + .count = ARRAY_SIZE(bcm2835_rates_12000000),
92 +};
93 +
94 +static int snd_audioinjector_pi_soundcard_startup(struct snd_pcm_substream *substream)
95 +{
96 + /* Setup constraints, because there is a 12 MHz XTAL on the board */
97 + snd_pcm_hw_constraint_list(substream->runtime, 0,
98 + SNDRV_PCM_HW_PARAM_RATE,
99 + &bcm2835_constraints_12000000);
100 + return 0;
101 +}
102 +
103 +static int snd_audioinjector_pi_soundcard_hw_params(struct snd_pcm_substream *substream,
104 + struct snd_pcm_hw_params *params)
105 +{
106 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
107 + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
108 +
109 + switch (params_rate(params)){
110 + case 8000:
111 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 1);
112 + case 16000:
113 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 750);
114 + case 32000:
115 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 375);
116 + case 44100:
117 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 272);
118 + case 48000:
119 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 250);
120 + case 88200:
121 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 136);
122 + case 96000:
123 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 125);
124 + default:
125 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 125);
126 + }
127 +}
128 +
129 +/* machine stream operations */
130 +static struct snd_soc_ops snd_audioinjector_pi_soundcard_ops = {
131 + .startup = snd_audioinjector_pi_soundcard_startup,
132 + .hw_params = snd_audioinjector_pi_soundcard_hw_params,
133 +};
134 +
135 +static int audioinjector_pi_soundcard_dai_init(struct snd_soc_pcm_runtime *rtd)
136 +{
137 + return snd_soc_dai_set_sysclk(rtd->codec_dai, WM8731_SYSCLK_XTAL, 12000000, SND_SOC_CLOCK_IN);
138 +}
139 +
140 +static struct snd_soc_dai_link audioinjector_pi_soundcard_dai[] = {
141 + {
142 + .name = "AudioInjector audio",
143 + .stream_name = "AudioInjector audio",
144 + .cpu_dai_name = "bcm2708-i2s.0",
145 + .codec_dai_name = "wm8731-hifi",
146 + .platform_name = "bcm2835-i2s.0",
147 + .codec_name = "wm8731.1-001a",
148 + .ops = &snd_audioinjector_pi_soundcard_ops,
149 + .init = audioinjector_pi_soundcard_dai_init,
150 + .dai_fmt = SND_SOC_DAIFMT_CBM_CFM|SND_SOC_DAIFMT_I2S|SND_SOC_DAIFMT_NB_NF,
151 + },
152 +};
153 +
154 +static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
155 + SND_SOC_DAPM_HP("Headphone Jack", NULL),
156 + SND_SOC_DAPM_SPK("Ext Spk", NULL),
157 + SND_SOC_DAPM_LINE("Line In Jacks", NULL),
158 + SND_SOC_DAPM_MIC("Microphone", NULL),
159 +};
160 +
161 +static const struct snd_soc_dapm_route audioinjector_audio_map[] = {
162 + /* headphone connected to LHPOUT, RHPOUT */
163 + {"Headphone Jack", NULL, "LHPOUT"},
164 + {"Headphone Jack", NULL, "RHPOUT"},
165 +
166 + /* speaker connected to LOUT, ROUT */
167 + {"Ext Spk", NULL, "ROUT"},
168 + {"Ext Spk", NULL, "LOUT"},
169 +
170 + /* line inputs */
171 + {"Line In Jacks", NULL, "Line Input"},
172 +
173 + /* mic is connected to Mic Jack, with WM8731 Mic Bias */
174 + {"Microphone", NULL, "Mic Bias"},
175 +};
176 +
177 +static struct snd_soc_card snd_soc_audioinjector = {
178 + .name = "audioinjector-pi-soundcard",
179 + .dai_link = audioinjector_pi_soundcard_dai,
180 + .num_links = ARRAY_SIZE(audioinjector_pi_soundcard_dai),
181 +
182 + .dapm_widgets = wm8731_dapm_widgets,
183 + .num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets),
184 + .dapm_routes = audioinjector_audio_map,
185 + .num_dapm_routes = ARRAY_SIZE(audioinjector_audio_map),
186 +};
187 +
188 +static int audioinjector_pi_soundcard_probe(struct platform_device *pdev)
189 +{
190 + struct snd_soc_card *card = &snd_soc_audioinjector;
191 + int ret;
192 +
193 + card->dev = &pdev->dev;
194 +
195 + if (pdev->dev.of_node) {
196 + struct snd_soc_dai_link *dai = &audioinjector_pi_soundcard_dai[0];
197 + struct device_node *i2s_node = of_parse_phandle(pdev->dev.of_node,
198 + "i2s-controller", 0);
199 +
200 + if (i2s_node) {
201 + dai->cpu_dai_name = NULL;
202 + dai->cpu_of_node = i2s_node;
203 + dai->platform_name = NULL;
204 + dai->platform_of_node = i2s_node;
205 + } else
206 + if (!dai->cpu_of_node) {
207 + dev_err(&pdev->dev, "Property 'i2s-controller' missing or invalid\n");
208 + return -EINVAL;
209 + }
210 + }
211 +
212 + if ((ret = snd_soc_register_card(card))) {
213 + dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
214 + }
215 + return ret;
216 +}
217 +
218 +static int audioinjector_pi_soundcard_remove(struct platform_device *pdev)
219 +{
220 + struct snd_soc_card *card = platform_get_drvdata(pdev);
221 + return snd_soc_unregister_card(card);
222 +
223 +}
224 +
225 +static const struct of_device_id audioinjector_pi_soundcard_of_match[] = {
226 + { .compatible = "ai,audioinjector-pi-soundcard", },
227 + {},
228 +};
229 +MODULE_DEVICE_TABLE(of, audioinjector_pi_soundcard_of_match);
230 +
231 +static struct platform_driver audioinjector_pi_soundcard_driver = {
232 + .driver = {
233 + .name = "audioinjector-stereo",
234 + .owner = THIS_MODULE,
235 + .of_match_table = audioinjector_pi_soundcard_of_match,
236 + },
237 + .probe = audioinjector_pi_soundcard_probe,
238 + .remove = audioinjector_pi_soundcard_remove,
239 +};
240 +
241 +module_platform_driver(audioinjector_pi_soundcard_driver);
242 +MODULE_AUTHOR("Matt Flax <flatmax@flatmax.org>");
243 +MODULE_DESCRIPTION("AudioInjector.net Pi Soundcard");
244 +MODULE_LICENSE("GPL v2");
245 +MODULE_ALIAS("platform:audioinjector-pi-soundcard");
246 +