1ab87d5a54732cd5639ef5d45e54da3ae2687cb4
[openwrt/staging/adrian.git] /
1 From 5705594ae56861cb63e7a3de1854e29ad1e830fd Mon Sep 17 00:00:00 2001
2 From: b-ak <anur.bhargav@gmail.com>
3 Date: Thu, 3 Jan 2019 00:01:08 +0530
4 Subject: [PATCH] ASoC: Add support for AudioSense-Pi add-on soundcard
5
6 AudioSense-Pi is a RPi HAT based on a TI's TLV320AIC32x4 stereo codec
7
8 This hardware provides multiple audio I/O capabilities to the RPi.
9 The codec connects to the RPi's SoC through the I2S Bus.
10
11 The following devices can be connected through a 3.5mm jack
12 1. Line-In: Plain old audio in from mobile phones, PCs, etc.,
13 2. Mic-In: Connect a microphone
14 3. Line-Out: Connect the output to a speaker
15 4. Headphones: Connect a Headphone w or w/o microphones
16
17 Multiple Inputs:
18 It supports the following combinations
19 1. Two stereo Line-Inputs and a microphone
20 2. One stereo Line-Input and two microphones
21 3. Two stereo Line-Inputs, a microphone and
22 one mono line-input (with h/w hack)
23 4. One stereo Line-Input, two microphones and
24 one mono line-input (with h/w hack)
25
26 Multiple Outputs:
27 Audio output can be routed to the headphones or
28 speakers (with additional hardware)
29
30 Signed-off-by: b-ak <anur.bhargav@gmail.com>
31 ---
32 sound/soc/bcm/Kconfig | 7 +
33 sound/soc/bcm/Makefile | 2 +
34 sound/soc/bcm/audiosense-pi.c | 246 ++++++++++++++++++++++++++++++++++
35 3 files changed, 255 insertions(+)
36 create mode 100644 sound/soc/bcm/audiosense-pi.c
37
38 --- a/sound/soc/bcm/Kconfig
39 +++ b/sound/soc/bcm/Kconfig
40 @@ -137,6 +137,13 @@ config SND_AUDIOINJECTOR_OCTO_SOUNDCARD
41 help
42 Say Y or M if you want to add support for audioinjector.net octo add on
43
44 +config SND_AUDIOSENSE_PI
45 + tristate "Support for AudioSense Add-On Soundcard"
46 + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
47 + select SND_SOC_TLV320AIC32X4_I2C
48 + help
49 + Say Y or M if you want to add support for tlv320aic32x4 add-on
50 +
51 config SND_DIGIDAC1_SOUNDCARD
52 tristate "Support for Red Rocks Audio DigiDAC1"
53 depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
54 --- a/sound/soc/bcm/Makefile
55 +++ b/sound/soc/bcm/Makefile
56 @@ -20,6 +20,7 @@ snd-soc-rpi-proto-objs := rpi-proto.o
57 snd-soc-iqaudio-dac-objs := iqaudio-dac.o
58 snd-soc-audioinjector-pi-soundcard-objs := audioinjector-pi-soundcard.o
59 snd-soc-audioinjector-octo-soundcard-objs := audioinjector-octo-soundcard.o
60 +snd-soc-audiosense-pi-objs := audiosense-pi.o
61 snd-soc-digidac1-soundcard-objs := digidac1-soundcard.o
62 snd-soc-dionaudio-loco-objs := dionaudio_loco.o
63 snd-soc-dionaudio-loco-v2-objs := dionaudio_loco-v2.o
64 @@ -41,6 +42,7 @@ obj-$(CONFIG_SND_BCM2708_SOC_RPI_PROTO)
65 obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) += snd-soc-iqaudio-dac.o
66 obj-$(CONFIG_SND_AUDIOINJECTOR_PI_SOUNDCARD) += snd-soc-audioinjector-pi-soundcard.o
67 obj-$(CONFIG_SND_AUDIOINJECTOR_OCTO_SOUNDCARD) += snd-soc-audioinjector-octo-soundcard.o
68 +obj-$(CONFIG_SND_AUDIOSENSE_PI) += snd-soc-audiosense-pi.o
69 obj-$(CONFIG_SND_DIGIDAC1_SOUNDCARD) += snd-soc-digidac1-soundcard.o
70 obj-$(CONFIG_SND_BCM2708_SOC_DIONAUDIO_LOCO) += snd-soc-dionaudio-loco.o
71 obj-$(CONFIG_SND_BCM2708_SOC_DIONAUDIO_LOCO_V2) += snd-soc-dionaudio-loco-v2.o
72 --- /dev/null
73 +++ b/sound/soc/bcm/audiosense-pi.c
74 @@ -0,0 +1,246 @@
75 +/*
76 + * ASoC Driver for AudioSense add on soundcard
77 + * Author:
78 + * Bhargav A K <anur.bhargav@gmail.com>
79 + * Copyright 2017
80 + *
81 + * This program is free software; you can redistribute it and/or
82 + * modify it under the terms of the GNU General Public License
83 + * version 2 as published by the Free Software Foundation.
84 + *
85 + * This program is distributed in the hope that it will be useful, but
86 + * WITHOUT ANY WARRANTY; without even the implied warranty of
87 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
88 + * General Public License for more details.
89 + */
90 +
91 +#include <linux/module.h>
92 +#include <linux/platform_device.h>
93 +#include <linux/clk.h>
94 +#include <linux/i2c.h>
95 +#include <sound/core.h>
96 +#include <sound/pcm.h>
97 +#include <sound/pcm_params.h>
98 +#include <sound/soc.h>
99 +#include <sound/jack.h>
100 +#include <sound/control.h>
101 +
102 +#include <sound/tlv320aic32x4.h>
103 +#include "../codecs/tlv320aic32x4.h"
104 +
105 +#define AIC32X4_SYSCLK_XTAL 0x00
106 +
107 +/*
108 + * Setup Codec Sample Rates and Channels
109 + * Supported Rates:
110 + * 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000, 88200, 96000,
111 + */
112 +static const unsigned int audiosense_pi_rate[] = {
113 + 48000,
114 +};
115 +
116 +static struct snd_pcm_hw_constraint_list audiosense_constraints_rates = {
117 + .list = audiosense_pi_rate,
118 + .count = ARRAY_SIZE(audiosense_pi_rate),
119 +};
120 +
121 +static const unsigned int audiosense_pi_channels[] = {
122 + 2,
123 +};
124 +
125 +static struct snd_pcm_hw_constraint_list audiosense_constraints_ch = {
126 + .count = ARRAY_SIZE(audiosense_pi_channels),
127 + .list = audiosense_pi_channels,
128 + .mask = 0,
129 +};
130 +
131 +/* Setup DAPM widgets and paths */
132 +static const struct snd_soc_dapm_widget audiosense_pi_dapm_widgets[] = {
133 + SND_SOC_DAPM_HP("Headphone Jack", NULL),
134 + SND_SOC_DAPM_LINE("Line Out", NULL),
135 + SND_SOC_DAPM_LINE("Line In", NULL),
136 + SND_SOC_DAPM_INPUT("CM_L"),
137 + SND_SOC_DAPM_INPUT("CM_R"),
138 +};
139 +
140 +static const struct snd_soc_dapm_route audiosense_pi_audio_map[] = {
141 + /* Line Inputs are connected to
142 + * (IN1_L | IN1_R)
143 + * (IN2_L | IN2_R)
144 + * (IN3_L | IN3_R)
145 + */
146 + {"IN1_L", NULL, "Line In"},
147 + {"IN1_R", NULL, "Line In"},
148 + {"IN2_L", NULL, "Line In"},
149 + {"IN2_R", NULL, "Line In"},
150 + {"IN3_L", NULL, "Line In"},
151 + {"IN3_R", NULL, "Line In"},
152 +
153 + /* Mic is connected to IN2_L and IN2_R */
154 + {"Left ADC", NULL, "Mic Bias"},
155 + {"Right ADC", NULL, "Mic Bias"},
156 +
157 + /* Headphone connected to HPL, HPR */
158 + {"Headphone Jack", NULL, "HPL"},
159 + {"Headphone Jack", NULL, "HPR"},
160 +
161 + /* Speakers connected to LOL and LOR */
162 + {"Line Out", NULL, "LOL"},
163 + {"Line Out", NULL, "LOR"},
164 +};
165 +
166 +static int audiosense_pi_card_init(struct snd_soc_pcm_runtime *rtd)
167 +{
168 + /* TODO: init of the codec specific dapm data, ignore suspend/resume */
169 + struct snd_soc_component *component = rtd->codec_dai->component;
170 +
171 + snd_soc_component_update_bits(component, AIC32X4_MICBIAS, 0x78,
172 + AIC32X4_MICBIAS_LDOIN |
173 + AIC32X4_MICBIAS_2075V);
174 + snd_soc_component_update_bits(component, AIC32X4_PWRCFG, 0x08,
175 + AIC32X4_AVDDWEAKDISABLE);
176 + snd_soc_component_update_bits(component, AIC32X4_LDOCTL, 0x01,
177 + AIC32X4_LDOCTLEN);
178 +
179 + return 0;
180 +}
181 +
182 +static int audiosense_pi_card_hw_params(
183 + struct snd_pcm_substream *substream,
184 + struct snd_pcm_hw_params *params)
185 +{
186 + int ret = 0;
187 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
188 + struct snd_soc_dai *codec_dai = rtd->codec_dai;
189 +
190 + /* Set the codec system clock, there is a 12 MHz XTAL on the board */
191 + ret = snd_soc_dai_set_sysclk(codec_dai, AIC32X4_SYSCLK_XTAL,
192 + 12000000, SND_SOC_CLOCK_IN);
193 + if (ret) {
194 + dev_err(rtd->card->dev,
195 + "could not set codec driver clock params\n");
196 + return ret;
197 + }
198 + return 0;
199 +}
200 +
201 +static int audiosense_pi_card_startup(struct snd_pcm_substream *substream)
202 +{
203 + struct snd_pcm_runtime *runtime = substream->runtime;
204 +
205 + /*
206 + * Set codec to 48Khz Sampling, Stereo I/O and 16 bit audio
207 + */
208 + runtime->hw.channels_max = 2;
209 + snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
210 + &audiosense_constraints_ch);
211 +
212 + runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
213 + snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
214 +
215 +
216 + snd_pcm_hw_constraint_list(substream->runtime, 0,
217 + SNDRV_PCM_HW_PARAM_RATE,
218 + &audiosense_constraints_rates);
219 + return 0;
220 +}
221 +
222 +static struct snd_soc_ops audiosense_pi_card_ops = {
223 + .startup = audiosense_pi_card_startup,
224 + .hw_params = audiosense_pi_card_hw_params,
225 +};
226 +
227 +static struct snd_soc_dai_link audiosense_pi_card_dai[] = {
228 + {
229 + .name = "TLV320AIC3204 Audio",
230 + .stream_name = "TLV320AIC3204 Hifi Audio",
231 + .cpu_dai_name = "bcm2708-i2s.0",
232 + .codec_dai_name = "tlv320aic32x4-hifi",
233 + .platform_name = "bcm2708-i2s.0",
234 + .codec_name = "tlv320aic32x4.1-0018",
235 + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
236 + SND_SOC_DAIFMT_CBM_CFM,
237 + .ops = &audiosense_pi_card_ops,
238 + .init = audiosense_pi_card_init,
239 + },
240 +};
241 +
242 +static struct snd_soc_card audiosense_pi_card = {
243 + .name = "audiosense-pi",
244 + .driver_name = "audiosense-pi",
245 + .dai_link = audiosense_pi_card_dai,
246 + .owner = THIS_MODULE,
247 + .num_links = ARRAY_SIZE(audiosense_pi_card_dai),
248 + .dapm_widgets = audiosense_pi_dapm_widgets,
249 + .num_dapm_widgets = ARRAY_SIZE(audiosense_pi_dapm_widgets),
250 + .dapm_routes = audiosense_pi_audio_map,
251 + .num_dapm_routes = ARRAY_SIZE(audiosense_pi_audio_map),
252 +};
253 +
254 +static int audiosense_pi_card_probe(struct platform_device *pdev)
255 +{
256 + int ret = 0;
257 + struct snd_soc_card *card = &audiosense_pi_card;
258 + struct snd_soc_dai_link *dai = &audiosense_pi_card_dai[0];
259 + struct device_node *i2s_node = pdev->dev.of_node;
260 +
261 + card->dev = &pdev->dev;
262 +
263 + if (!dai) {
264 + dev_err(&pdev->dev, "DAI not found. Missing or Invalid\n");
265 + return -EINVAL;
266 + }
267 +
268 + i2s_node = of_parse_phandle(pdev->dev.of_node, "i2s-controller", 0);
269 + if (!i2s_node) {
270 + dev_err(&pdev->dev,
271 + "Property 'i2s-controller' missing or invalid\n");
272 + return -EINVAL;
273 + }
274 +
275 + dai->cpu_dai_name = NULL;
276 + dai->cpu_of_node = i2s_node;
277 + dai->platform_name = NULL;
278 + dai->platform_of_node = i2s_node;
279 +
280 + of_node_put(i2s_node);
281 +
282 + ret = snd_soc_register_card(card);
283 + if (ret && ret != -EPROBE_DEFER)
284 + dev_err(&pdev->dev,
285 + "snd_soc_register_card() failed: %d\n", ret);
286 +
287 + return ret;
288 +}
289 +
290 +static int audiosense_pi_card_remove(struct platform_device *pdev)
291 +{
292 + struct snd_soc_card *card = platform_get_drvdata(pdev);
293 +
294 + return snd_soc_unregister_card(card);
295 +
296 +}
297 +
298 +static const struct of_device_id audiosense_pi_card_of_match[] = {
299 + { .compatible = "as,audiosense-pi", },
300 + {},
301 +};
302 +MODULE_DEVICE_TABLE(of, audiosense_pi_card_of_match);
303 +
304 +static struct platform_driver audiosense_pi_card_driver = {
305 + .driver = {
306 + .name = "audiosense-snd-card",
307 + .owner = THIS_MODULE,
308 + .of_match_table = audiosense_pi_card_of_match,
309 + },
310 + .probe = audiosense_pi_card_probe,
311 + .remove = audiosense_pi_card_remove,
312 +};
313 +
314 +module_platform_driver(audiosense_pi_card_driver);
315 +
316 +MODULE_AUTHOR("Bhargav AK <anur.bhargav@gmail.com>");
317 +MODULE_DESCRIPTION("ASoC Driver for TLV320AIC3204 Audio");
318 +MODULE_LICENSE("GPL v2");
319 +MODULE_ALIAS("platform:audiosense-pi");
320 +