643d50081e5b088685017d6fffc0e194365db2f8
[oweals/openwrt.git] /
1 From b4dc92bbf8c396012a83eafc01cec115bfb2e502 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 072/782] 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/audioinjector-pi-soundcard.c | 185 +++++++++++++++++++++
17  1 file changed, 185 insertions(+)
18  create mode 100644 sound/soc/bcm/audioinjector-pi-soundcard.c
19
20 --- /dev/null
21 +++ b/sound/soc/bcm/audioinjector-pi-soundcard.c
22 @@ -0,0 +1,185 @@
23 +/*
24 + * ASoC Driver for AudioInjector Pi add on soundcard
25 + *
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.
31 + *
32 + * Copyright (C) 2016 Flatmax Pty. Ltd.
33 + *
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.
37 + *
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.
42 + */
43 +
44 +#include <linux/module.h>
45 +#include <linux/types.h>
46 +
47 +#include <sound/core.h>
48 +#include <sound/soc.h>
49 +#include <sound/pcm_params.h>
50 +#include <sound/control.h>
51 +
52 +#include "../codecs/wm8731.h"
53 +
54 +static const unsigned int bcm2835_rates_12000000[] = {
55 +       8000, 16000, 32000, 44100, 48000, 96000, 88200,
56 +};
57 +
58 +static struct snd_pcm_hw_constraint_list bcm2835_constraints_12000000 = {
59 +       .list = bcm2835_rates_12000000,
60 +       .count = ARRAY_SIZE(bcm2835_rates_12000000),
61 +};
62 +
63 +static int snd_audioinjector_pi_soundcard_startup(struct snd_pcm_substream *substream)
64 +{
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);
69 +       return 0;
70 +}
71 +
72 +static int snd_audioinjector_pi_soundcard_hw_params(struct snd_pcm_substream *substream,
73 +                                      struct snd_pcm_hw_params *params)
74 +{
75 +       struct snd_soc_pcm_runtime *rtd = substream->private_data;
76 +       struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
77 +
78 +       switch (params_rate(params)){
79 +               case 8000:
80 +                       return snd_soc_dai_set_bclk_ratio(cpu_dai, 1);
81 +               case 16000:
82 +                       return snd_soc_dai_set_bclk_ratio(cpu_dai, 750);
83 +               case 32000:
84 +                       return snd_soc_dai_set_bclk_ratio(cpu_dai, 375);
85 +               case 44100:
86 +                       return snd_soc_dai_set_bclk_ratio(cpu_dai, 272);
87 +               case 48000:
88 +                       return snd_soc_dai_set_bclk_ratio(cpu_dai, 250);
89 +               case 88200:
90 +                       return snd_soc_dai_set_bclk_ratio(cpu_dai, 136);
91 +               case 96000:
92 +                       return snd_soc_dai_set_bclk_ratio(cpu_dai, 125);
93 +               default:
94 +                       return snd_soc_dai_set_bclk_ratio(cpu_dai, 125);
95 +       }
96 +}
97 +
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,
102 +};
103 +
104 +static int audioinjector_pi_soundcard_dai_init(struct snd_soc_pcm_runtime *rtd)
105 +{
106 +       return snd_soc_dai_set_sysclk(rtd->codec_dai, WM8731_SYSCLK_XTAL, 12000000, SND_SOC_CLOCK_IN);
107 +}
108 +
109 +static struct snd_soc_dai_link audioinjector_pi_soundcard_dai[] = {
110 +       {
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,
120 +       },
121 +};
122 +
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),
128 +};
129 +
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"},
134 +
135 +       /* speaker connected to LOUT, ROUT */
136 +       {"Ext Spk", NULL, "ROUT"},
137 +       {"Ext Spk", NULL, "LOUT"},
138 +
139 +       /* line inputs */
140 +       {"Line In Jacks", NULL, "Line Input"},
141 +
142 +       /* mic is connected to Mic Jack, with WM8731 Mic Bias */
143 +       {"Microphone", NULL, "Mic Bias"},
144 +};
145 +
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),
150 +
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),
155 +};
156 +
157 +static int audioinjector_pi_soundcard_probe(struct platform_device *pdev)
158 +{
159 +       struct snd_soc_card *card = &snd_soc_audioinjector;
160 +       int ret;
161 +       
162 +       card->dev = &pdev->dev;
163 +
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);
168 +
169 +               if (i2s_node) {
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;
174 +               } else
175 +                       if (!dai->cpu_of_node) {
176 +                               dev_err(&pdev->dev, "Property 'i2s-controller' missing or invalid\n");
177 +                               return -EINVAL;
178 +                       }
179 +       }
180 +
181 +       if ((ret = devm_snd_soc_register_card(&pdev->dev, card))) {
182 +               dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
183 +       }
184 +       return ret;
185 +}
186 +
187 +static const struct of_device_id audioinjector_pi_soundcard_of_match[] = {
188 +       { .compatible = "ai,audioinjector-pi-soundcard", },
189 +       {},
190 +};
191 +MODULE_DEVICE_TABLE(of, audioinjector_pi_soundcard_of_match);
192 +
193 +static struct platform_driver audioinjector_pi_soundcard_driver = {
194 +       .driver         = {
195 +               .name   = "audioinjector-stereo",
196 +               .owner  = THIS_MODULE,
197 +               .of_match_table = audioinjector_pi_soundcard_of_match,
198 +       },
199 +       .probe          = audioinjector_pi_soundcard_probe,
200 +};
201 +
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");
207 +