1 From 3f06b2f7c04d263a3bb0569e6f2c341549c21154 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
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/audioinjector-pi-soundcard.c | 185 +++++++++++++++++++++
17 1 file changed, 185 insertions(+)
18 create mode 100644 sound/soc/bcm/audioinjector-pi-soundcard.c
21 +++ b/sound/soc/bcm/audioinjector-pi-soundcard.c
24 + * ASoC Driver for AudioInjector Pi add on soundcard
26 + * Created on: 13-May-2016
27 + * Author: flatmax@flatmax.org
28 + * based on code by Cliff Cai <Cliff.Cai@analog.com> for the ssm2602 machine blackfin.
29 + * with help from Lars-Peter Clausen for simplifying the original code to use the dai_fmt field.
30 + * i2s_node code taken from the other sound/soc/bcm machine drivers.
32 + * Copyright (C) 2016 Flatmax Pty. Ltd.
34 + * This program is free software; you can redistribute it and/or
35 + * modify it under the terms of the GNU General Public License
36 + * version 2 as published by the Free Software Foundation.
38 + * This program is distributed in the hope that it will be useful, but
39 + * WITHOUT ANY WARRANTY; without even the implied warranty of
40 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41 + * General Public License for more details.
44 +#include <linux/module.h>
45 +#include <linux/types.h>
47 +#include <sound/core.h>
48 +#include <sound/soc.h>
49 +#include <sound/pcm_params.h>
50 +#include <sound/control.h>
52 +#include "../codecs/wm8731.h"
54 +static const unsigned int bcm2835_rates_12000000[] = {
55 + 8000, 16000, 32000, 44100, 48000, 96000, 88200,
58 +static struct snd_pcm_hw_constraint_list bcm2835_constraints_12000000 = {
59 + .list = bcm2835_rates_12000000,
60 + .count = ARRAY_SIZE(bcm2835_rates_12000000),
63 +static int snd_audioinjector_pi_soundcard_startup(struct snd_pcm_substream *substream)
65 + /* Setup constraints, because there is a 12 MHz XTAL on the board */
66 + snd_pcm_hw_constraint_list(substream->runtime, 0,
67 + SNDRV_PCM_HW_PARAM_RATE,
68 + &bcm2835_constraints_12000000);
72 +static int snd_audioinjector_pi_soundcard_hw_params(struct snd_pcm_substream *substream,
73 + struct snd_pcm_hw_params *params)
75 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
76 + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
78 + switch (params_rate(params)){
80 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 1);
82 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 750);
84 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 375);
86 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 272);
88 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 250);
90 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 136);
92 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 125);
94 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 125);
98 +/* machine stream operations */
99 +static struct snd_soc_ops snd_audioinjector_pi_soundcard_ops = {
100 + .startup = snd_audioinjector_pi_soundcard_startup,
101 + .hw_params = snd_audioinjector_pi_soundcard_hw_params,
104 +static int audioinjector_pi_soundcard_dai_init(struct snd_soc_pcm_runtime *rtd)
106 + return snd_soc_dai_set_sysclk(rtd->codec_dai, WM8731_SYSCLK_XTAL, 12000000, SND_SOC_CLOCK_IN);
109 +static struct snd_soc_dai_link audioinjector_pi_soundcard_dai[] = {
111 + .name = "AudioInjector audio",
112 + .stream_name = "AudioInjector audio",
113 + .cpu_dai_name = "bcm2708-i2s.0",
114 + .codec_dai_name = "wm8731-hifi",
115 + .platform_name = "bcm2835-i2s.0",
116 + .codec_name = "wm8731.1-001a",
117 + .ops = &snd_audioinjector_pi_soundcard_ops,
118 + .init = audioinjector_pi_soundcard_dai_init,
119 + .dai_fmt = SND_SOC_DAIFMT_CBM_CFM|SND_SOC_DAIFMT_I2S|SND_SOC_DAIFMT_NB_NF,
123 +static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
124 + SND_SOC_DAPM_HP("Headphone Jack", NULL),
125 + SND_SOC_DAPM_SPK("Ext Spk", NULL),
126 + SND_SOC_DAPM_LINE("Line In Jacks", NULL),
127 + SND_SOC_DAPM_MIC("Microphone", NULL),
130 +static const struct snd_soc_dapm_route audioinjector_audio_map[] = {
131 + /* headphone connected to LHPOUT, RHPOUT */
132 + {"Headphone Jack", NULL, "LHPOUT"},
133 + {"Headphone Jack", NULL, "RHPOUT"},
135 + /* speaker connected to LOUT, ROUT */
136 + {"Ext Spk", NULL, "ROUT"},
137 + {"Ext Spk", NULL, "LOUT"},
140 + {"Line In Jacks", NULL, "Line Input"},
142 + /* mic is connected to Mic Jack, with WM8731 Mic Bias */
143 + {"Microphone", NULL, "Mic Bias"},
146 +static struct snd_soc_card snd_soc_audioinjector = {
147 + .name = "audioinjector-pi-soundcard",
148 + .dai_link = audioinjector_pi_soundcard_dai,
149 + .num_links = ARRAY_SIZE(audioinjector_pi_soundcard_dai),
151 + .dapm_widgets = wm8731_dapm_widgets,
152 + .num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets),
153 + .dapm_routes = audioinjector_audio_map,
154 + .num_dapm_routes = ARRAY_SIZE(audioinjector_audio_map),
157 +static int audioinjector_pi_soundcard_probe(struct platform_device *pdev)
159 + struct snd_soc_card *card = &snd_soc_audioinjector;
162 + card->dev = &pdev->dev;
164 + if (pdev->dev.of_node) {
165 + struct snd_soc_dai_link *dai = &audioinjector_pi_soundcard_dai[0];
166 + struct device_node *i2s_node = of_parse_phandle(pdev->dev.of_node,
167 + "i2s-controller", 0);
170 + dai->cpu_dai_name = NULL;
171 + dai->cpu_of_node = i2s_node;
172 + dai->platform_name = NULL;
173 + dai->platform_of_node = i2s_node;
175 + if (!dai->cpu_of_node) {
176 + dev_err(&pdev->dev, "Property 'i2s-controller' missing or invalid\n");
181 + if ((ret = devm_snd_soc_register_card(&pdev->dev, card))) {
182 + dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
187 +static const struct of_device_id audioinjector_pi_soundcard_of_match[] = {
188 + { .compatible = "ai,audioinjector-pi-soundcard", },
191 +MODULE_DEVICE_TABLE(of, audioinjector_pi_soundcard_of_match);
193 +static struct platform_driver audioinjector_pi_soundcard_driver = {
195 + .name = "audioinjector-stereo",
196 + .owner = THIS_MODULE,
197 + .of_match_table = audioinjector_pi_soundcard_of_match,
199 + .probe = audioinjector_pi_soundcard_probe,
202 +module_platform_driver(audioinjector_pi_soundcard_driver);
203 +MODULE_AUTHOR("Matt Flax <flatmax@flatmax.org>");
204 +MODULE_DESCRIPTION("AudioInjector.net Pi Soundcard");
205 +MODULE_LICENSE("GPL v2");
206 +MODULE_ALIAS("platform:audioinjector-pi-soundcard");