1 From 235989cb582a29be0bea6a7e306f7b839789053c 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] New AudioInjector.net Pi soundcard with low jitter audio in
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.
12 Added support for headphones, microphone and bclk_ratio settings.
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.
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
22 --- a/sound/soc/bcm/Kconfig
23 +++ b/sound/soc/bcm/Kconfig
24 @@ -94,3 +94,10 @@ config SND_BCM2708_SOC_ADAU1977_ADC
25 select SND_SOC_ADAU1977_I2C
27 Say Y or M if you want to add support for ADAU1977 ADC.
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
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
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
52 +++ b/sound/soc/bcm/audioinjector-pi-soundcard.c
55 + * ASoC Driver for AudioInjector Pi add on soundcard
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.
63 + * Copyright (C) 2016 Flatmax Pty. Ltd.
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.
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.
75 +#include <linux/module.h>
76 +#include <linux/types.h>
78 +#include <sound/core.h>
79 +#include <sound/soc.h>
80 +#include <sound/pcm_params.h>
81 +#include <sound/control.h>
83 +#include "../codecs/wm8731.h"
85 +static const unsigned int bcm2835_rates_12000000[] = {
86 + 8000, 16000, 32000, 44100, 48000, 96000, 88200,
89 +static struct snd_pcm_hw_constraint_list bcm2835_constraints_12000000 = {
90 + .list = bcm2835_rates_12000000,
91 + .count = ARRAY_SIZE(bcm2835_rates_12000000),
94 +static int snd_audioinjector_pi_soundcard_startup(struct snd_pcm_substream *substream)
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);
103 +static int snd_audioinjector_pi_soundcard_hw_params(struct snd_pcm_substream *substream,
104 + struct snd_pcm_hw_params *params)
106 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
107 + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
109 + switch (params_rate(params)){
111 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 1);
113 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 750);
115 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 375);
117 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 272);
119 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 250);
121 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 136);
123 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 125);
125 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 125);
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,
135 +static int audioinjector_pi_soundcard_dai_init(struct snd_soc_pcm_runtime *rtd)
137 + return snd_soc_dai_set_sysclk(rtd->codec_dai, WM8731_SYSCLK_XTAL, 12000000, SND_SOC_CLOCK_IN);
140 +static struct snd_soc_dai_link audioinjector_pi_soundcard_dai[] = {
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,
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),
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"},
166 + /* speaker connected to LOUT, ROUT */
167 + {"Ext Spk", NULL, "ROUT"},
168 + {"Ext Spk", NULL, "LOUT"},
171 + {"Line In Jacks", NULL, "Line Input"},
173 + /* mic is connected to Mic Jack, with WM8731 Mic Bias */
174 + {"Microphone", NULL, "Mic Bias"},
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),
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),
188 +static int audioinjector_pi_soundcard_probe(struct platform_device *pdev)
190 + struct snd_soc_card *card = &snd_soc_audioinjector;
193 + card->dev = &pdev->dev;
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);
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;
206 + if (!dai->cpu_of_node) {
207 + dev_err(&pdev->dev, "Property 'i2s-controller' missing or invalid\n");
212 + if ((ret = snd_soc_register_card(card))) {
213 + dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
218 +static int audioinjector_pi_soundcard_remove(struct platform_device *pdev)
220 + struct snd_soc_card *card = platform_get_drvdata(pdev);
221 + return snd_soc_unregister_card(card);
225 +static const struct of_device_id audioinjector_pi_soundcard_of_match[] = {
226 + { .compatible = "ai,audioinjector-pi-soundcard", },
229 +MODULE_DEVICE_TABLE(of, audioinjector_pi_soundcard_of_match);
231 +static struct platform_driver audioinjector_pi_soundcard_driver = {
233 + .name = "audioinjector-audio",
234 + .owner = THIS_MODULE,
235 + .of_match_table = audioinjector_pi_soundcard_of_match,
237 + .probe = audioinjector_pi_soundcard_probe,
238 + .remove = audioinjector_pi_soundcard_remove,
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");