1 From 1f912d3042032514c33736723300b3ed618ddd08 Mon Sep 17 00:00:00 2001
2 From: IQaudIO <gordon@iqaudio.com>
3 Date: Mon, 13 May 2019 21:53:05 +0100
4 Subject: [PATCH] Added IQaudIO Pi-Codec board support (#2969)
6 Add support for the IQaudIO Pi-Codec board.
8 Signed-off-by: Gordon <gordon@iqaudio.com>
10 Fixed 48k timing issue
12 ASoC: iqaudio-codec: use modern dai_link style
14 Signed-off-by: Hui Wang <hui.wang@canonical.com>
16 sound/soc/bcm/Kconfig | 7 +
17 sound/soc/bcm/Makefile | 2 +
18 sound/soc/bcm/iqaudio-codec.c | 274 ++++++++++++++++++++++++++++++++++
19 3 files changed, 283 insertions(+)
20 create mode 100644 sound/soc/bcm/iqaudio-codec.c
22 --- a/sound/soc/bcm/Kconfig
23 +++ b/sound/soc/bcm/Kconfig
24 @@ -104,6 +104,13 @@ config SND_BCM2708_SOC_JUSTBOOM_DIGI
26 Say Y or M if you want to add support for JustBoom Digi.
28 +config SND_BCM2708_SOC_IQAUDIO_CODEC
29 + tristate "Support for IQaudIO-CODEC"
30 + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
31 + select SND_SOC_DA7213
33 + Say Y or M if you want to add support for IQaudIO-CODEC.
35 config SND_BCM2708_SOC_IQAUDIO_DAC
36 tristate "Support for IQaudIO-DAC"
37 depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
38 --- a/sound/soc/bcm/Makefile
39 +++ b/sound/soc/bcm/Makefile
40 @@ -18,6 +18,7 @@ snd-soc-hifiberry-dacplusadc-objs := hif
41 snd-soc-justboom-dac-objs := justboom-dac.o
42 snd-soc-rpi-cirrus-objs := rpi-cirrus.o
43 snd-soc-rpi-proto-objs := rpi-proto.o
44 +snd-soc-iqaudio-codec-objs := iqaudio-codec.o
45 snd-soc-iqaudio-dac-objs := iqaudio-dac.o
46 snd-soc-i-sabre-q2m-objs := i-sabre-q2m.o
47 snd-soc-audioinjector-pi-soundcard-objs := audioinjector-pi-soundcard.o
48 @@ -41,6 +42,7 @@ obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_D
49 obj-$(CONFIG_SND_BCM2708_SOC_JUSTBOOM_DAC) += snd-soc-justboom-dac.o
50 obj-$(CONFIG_SND_BCM2708_SOC_RPI_CIRRUS) += snd-soc-rpi-cirrus.o
51 obj-$(CONFIG_SND_BCM2708_SOC_RPI_PROTO) += snd-soc-rpi-proto.o
52 +obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_CODEC) += snd-soc-iqaudio-codec.o
53 obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) += snd-soc-iqaudio-dac.o
54 obj-$(CONFIG_SND_BCM2708_SOC_I_SABRE_Q2M) += snd-soc-i-sabre-q2m.o
55 obj-$(CONFIG_SND_AUDIOINJECTOR_PI_SOUNDCARD) += snd-soc-audioinjector-pi-soundcard.o
57 +++ b/sound/soc/bcm/iqaudio-codec.c
60 + * ASoC Driver for IQaudIO Raspberry Pi Codec board
62 + * Author: Gordon Garrity <gordon@iqaudio.com>
63 + * (C) Copyright IQaudio Limited, 2017-2019
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/gpio/consumer.h>
77 +#include <linux/platform_device.h>
79 +#include <sound/core.h>
80 +#include <sound/pcm.h>
81 +#include <sound/pcm_params.h>
82 +#include <sound/soc.h>
83 +#include <sound/jack.h>
85 +#include <linux/acpi.h>
86 +#include <linux/slab.h>
87 +#include "../codecs/da7213.h"
89 +static int pll_out = DA7213_PLL_FREQ_OUT_90316800;
91 +static int snd_rpi_iqaudio_pll_control(struct snd_soc_dapm_widget *w,
92 + struct snd_kcontrol *k, int event)
95 + struct snd_soc_dapm_context *dapm = w->dapm;
96 + struct snd_soc_card *card = dapm->card;
97 + struct snd_soc_pcm_runtime *rtd =
98 + snd_soc_get_pcm_runtime(card, card->dai_link[0].name);
99 + struct snd_soc_dai *codec_dai = rtd->codec_dai;
101 + if (SND_SOC_DAPM_EVENT_OFF(event)) {
102 + ret = snd_soc_dai_set_pll(codec_dai, 0, DA7213_SYSCLK_MCLK, 0,
105 + dev_err(card->dev, "Failed to bypass PLL: %d\n", ret);
106 + /* Allow PLL time to bypass */
108 + } else if (SND_SOC_DAPM_EVENT_ON(event)) {
109 + ret = snd_soc_dai_set_pll(codec_dai, 0, DA7213_SYSCLK_PLL, 0,
112 + dev_err(card->dev, "Failed to enable PLL: %d\n", ret);
113 + /* Allow PLL time to lock */
120 +static int snd_rpi_iqaudio_post_dapm_event(struct snd_soc_dapm_widget *w,
121 + struct snd_kcontrol *kcontrol,
125 + case SND_SOC_DAPM_POST_PMU:
126 + /* Delay for mic bias ramp */
136 +static const struct snd_kcontrol_new dapm_controls[] = {
137 + SOC_DAPM_PIN_SWITCH("HP Jack"),
138 + SOC_DAPM_PIN_SWITCH("MIC Jack"),
139 + SOC_DAPM_PIN_SWITCH("Onboard MIC"),
140 + SOC_DAPM_PIN_SWITCH("AUX Jack"),
143 +static const struct snd_soc_dapm_widget dapm_widgets[] = {
144 + SND_SOC_DAPM_HP("HP Jack", NULL),
145 + SND_SOC_DAPM_MIC("MIC Jack", NULL),
146 + SND_SOC_DAPM_MIC("Onboard MIC", NULL),
147 + SND_SOC_DAPM_LINE("AUX Jack", NULL),
148 + SND_SOC_DAPM_SUPPLY("PLL Control", SND_SOC_NOPM, 0, 0,
149 + snd_rpi_iqaudio_pll_control,
150 + SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
151 + SND_SOC_DAPM_POST("Post Power Up Event", snd_rpi_iqaudio_post_dapm_event),
154 +static const struct snd_soc_dapm_route audio_map[] = {
155 + {"HP Jack", NULL, "HPL"},
156 + {"HP Jack", NULL, "HPR"},
157 + {"HP Jack", NULL, "PLL Control"},
159 + {"AUXR", NULL, "AUX Jack"},
160 + {"AUXL", NULL, "AUX Jack"},
161 + {"AUX Jack", NULL, "PLL Control"},
163 + /* Assume Mic1 is linked to Headset and Mic2 to on-board mic */
164 + {"MIC1", NULL, "MIC Jack"},
165 + {"MIC Jack", NULL, "PLL Control"},
166 + {"MIC2", NULL, "Onboard MIC"},
167 + {"Onboard MIC", NULL, "PLL Control"},
170 +/* machine stream operations */
172 +static int snd_rpi_iqaudio_codec_init(struct snd_soc_pcm_runtime *rtd)
174 + struct snd_soc_dai *codec_dai = rtd->codec_dai;
175 + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
179 + * Disable AUX Jack Pin by default to prevent PLL being enabled at
180 + * startup. This avoids holding the PLL to a fixed SR config for
181 + * subsequent streams.
183 + * This pin can still be enabled later, as required by user-space.
185 + snd_soc_dapm_disable_pin(&rtd->card->dapm, "AUX Jack");
186 + snd_soc_dapm_sync(&rtd->card->dapm);
188 + /* Set bclk ratio to align with codec's BCLK rate */
189 + ret = snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
191 + dev_err(rtd->dev, "Failed to set CPU BLCK ratio\n");
195 + /* Set MCLK frequency to codec, onboard 11.2896MHz clock */
196 + return snd_soc_dai_set_sysclk(codec_dai, DA7213_CLKSRC_MCLK, 11289600,
197 + SND_SOC_CLOCK_OUT);
200 +static int snd_rpi_iqaudio_codec_hw_params(struct snd_pcm_substream *substream,
201 + struct snd_pcm_hw_params *params)
203 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
204 + unsigned int samplerate = params_rate(params);
206 + switch (samplerate) {
212 + pll_out = DA7213_PLL_FREQ_OUT_98304000;
216 + pll_out = DA7213_PLL_FREQ_OUT_90316800;
219 + dev_err(rtd->dev,"Unsupported samplerate %d\n", samplerate);
224 +static const struct snd_soc_ops snd_rpi_iqaudio_codec_ops = {
225 + .hw_params = snd_rpi_iqaudio_codec_hw_params,
228 +SND_SOC_DAILINK_DEFS(rpi_iqaudio,
229 + DAILINK_COMP_ARRAY(COMP_CPU("bcm2708-i2s.0")),
230 + DAILINK_COMP_ARRAY(COMP_CODEC("da7213.1-001a", "da7213-hifi")),
231 + DAILINK_COMP_ARRAY(COMP_PLATFORM("bcm2835-i2s.0")));
233 +static struct snd_soc_dai_link snd_rpi_iqaudio_codec_dai[] = {
235 + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
236 + SND_SOC_DAIFMT_CBM_CFM,
237 + .init = snd_rpi_iqaudio_codec_init,
238 + .ops = &snd_rpi_iqaudio_codec_ops,
239 + .symmetric_rates = 1,
240 + .symmetric_channels = 1,
241 + .symmetric_samplebits = 1,
242 + SND_SOC_DAILINK_REG(rpi_iqaudio),
246 +/* audio machine driver */
247 +static struct snd_soc_card snd_rpi_iqaudio_codec = {
248 + .owner = THIS_MODULE,
249 + .dai_link = snd_rpi_iqaudio_codec_dai,
250 + .num_links = ARRAY_SIZE(snd_rpi_iqaudio_codec_dai),
251 + .controls = dapm_controls,
252 + .num_controls = ARRAY_SIZE(dapm_controls),
253 + .dapm_widgets = dapm_widgets,
254 + .num_dapm_widgets = ARRAY_SIZE(dapm_widgets),
255 + .dapm_routes = audio_map,
256 + .num_dapm_routes = ARRAY_SIZE(audio_map),
259 +static int snd_rpi_iqaudio_codec_probe(struct platform_device *pdev)
263 + snd_rpi_iqaudio_codec.dev = &pdev->dev;
265 + if (pdev->dev.of_node) {
266 + struct device_node *i2s_node;
267 + struct snd_soc_card *card = &snd_rpi_iqaudio_codec;
268 + struct snd_soc_dai_link *dai = &snd_rpi_iqaudio_codec_dai[0];
270 + i2s_node = of_parse_phandle(pdev->dev.of_node,
271 + "i2s-controller", 0);
273 + dai->cpus->dai_name = NULL;
274 + dai->cpus->of_node = i2s_node;
275 + dai->platforms->name = NULL;
276 + dai->platforms->of_node = i2s_node;
279 + if (of_property_read_string(pdev->dev.of_node, "card_name",
281 + card->name = "IQaudIOCODEC";
283 + if (of_property_read_string(pdev->dev.of_node, "dai_name",
285 + dai->name = "IQaudIO CODEC";
287 + if (of_property_read_string(pdev->dev.of_node,
288 + "dai_stream_name", &dai->stream_name))
289 + dai->stream_name = "IQaudIO CODEC HiFi v1.2";
293 + ret = snd_soc_register_card(&snd_rpi_iqaudio_codec);
295 + if (ret != -EPROBE_DEFER)
296 + dev_err(&pdev->dev,
297 + "snd_soc_register_card() failed: %d\n", ret);
304 +static int snd_rpi_iqaudio_codec_remove(struct platform_device *pdev)
306 + return snd_soc_unregister_card(&snd_rpi_iqaudio_codec);
309 +static const struct of_device_id iqaudio_of_match[] = {
310 + { .compatible = "iqaudio,iqaudio-codec", },
314 +MODULE_DEVICE_TABLE(of, iqaudio_of_match);
316 +static struct platform_driver snd_rpi_iqaudio_codec_driver = {
318 + .name = "snd-rpi-iqaudio-codec",
319 + .owner = THIS_MODULE,
320 + .of_match_table = iqaudio_of_match,
322 + .probe = snd_rpi_iqaudio_codec_probe,
323 + .remove = snd_rpi_iqaudio_codec_remove,
328 +module_platform_driver(snd_rpi_iqaudio_codec_driver);
330 +MODULE_AUTHOR("Gordon Garrity <gordon@iqaudio.com>");
331 +MODULE_DESCRIPTION("ASoC Driver for IQaudIO CODEC");
332 +MODULE_LICENSE("GPL v2");