2 * ASoC Driver for IQaudIO Raspberry Pi Codec board
4 * Author: Gordon Garrity <gordon@iqaudio.com>
5 * (C) Copyright IQaudio Limited, 2017-2019
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
17 #include <linux/module.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/platform_device.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include <sound/jack.h>
27 #include <linux/acpi.h>
28 #include <linux/slab.h>
29 #include "../codecs/da7213.h"
31 static int pll_out = DA7213_PLL_FREQ_OUT_90316800;
33 static int snd_rpi_iqaudio_pll_control(struct snd_soc_dapm_widget *w,
34 struct snd_kcontrol *k, int event)
37 struct snd_soc_dapm_context *dapm = w->dapm;
38 struct snd_soc_card *card = dapm->card;
39 struct snd_soc_pcm_runtime *rtd =
40 snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
41 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
43 if (SND_SOC_DAPM_EVENT_OFF(event)) {
44 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7213_SYSCLK_MCLK, 0,
47 dev_err(card->dev, "Failed to bypass PLL: %d\n", ret);
48 /* Allow PLL time to bypass */
50 } else if (SND_SOC_DAPM_EVENT_ON(event)) {
51 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7213_SYSCLK_PLL, 0,
54 dev_err(card->dev, "Failed to enable PLL: %d\n", ret);
55 /* Allow PLL time to lock */
62 static int snd_rpi_iqaudio_post_dapm_event(struct snd_soc_dapm_widget *w,
63 struct snd_kcontrol *kcontrol,
67 case SND_SOC_DAPM_POST_PMU:
68 /* Delay for mic bias ramp */
78 static const struct snd_kcontrol_new dapm_controls[] = {
79 SOC_DAPM_PIN_SWITCH("HP Jack"),
80 SOC_DAPM_PIN_SWITCH("MIC Jack"),
81 SOC_DAPM_PIN_SWITCH("Onboard MIC"),
82 SOC_DAPM_PIN_SWITCH("AUX Jack"),
85 static const struct snd_soc_dapm_widget dapm_widgets[] = {
86 SND_SOC_DAPM_HP("HP Jack", NULL),
87 SND_SOC_DAPM_MIC("MIC Jack", NULL),
88 SND_SOC_DAPM_MIC("Onboard MIC", NULL),
89 SND_SOC_DAPM_LINE("AUX Jack", NULL),
90 SND_SOC_DAPM_SUPPLY("PLL Control", SND_SOC_NOPM, 0, 0,
91 snd_rpi_iqaudio_pll_control,
92 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
93 SND_SOC_DAPM_POST("Post Power Up Event", snd_rpi_iqaudio_post_dapm_event),
96 static const struct snd_soc_dapm_route audio_map[] = {
97 {"HP Jack", NULL, "HPL"},
98 {"HP Jack", NULL, "HPR"},
99 {"HP Jack", NULL, "PLL Control"},
101 {"AUXR", NULL, "AUX Jack"},
102 {"AUXL", NULL, "AUX Jack"},
103 {"AUX Jack", NULL, "PLL Control"},
105 /* Assume Mic1 is linked to Headset and Mic2 to on-board mic */
106 {"MIC1", NULL, "MIC Jack"},
107 {"MIC Jack", NULL, "PLL Control"},
108 {"MIC2", NULL, "Onboard MIC"},
109 {"Onboard MIC", NULL, "PLL Control"},
112 /* machine stream operations */
114 static int snd_rpi_iqaudio_codec_init(struct snd_soc_pcm_runtime *rtd)
116 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
117 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
121 * Disable AUX Jack Pin by default to prevent PLL being enabled at
122 * startup. This avoids holding the PLL to a fixed SR config for
123 * subsequent streams.
125 * This pin can still be enabled later, as required by user-space.
127 snd_soc_dapm_disable_pin(&rtd->card->dapm, "AUX Jack");
128 snd_soc_dapm_sync(&rtd->card->dapm);
130 /* Set bclk ratio to align with codec's BCLK rate */
131 ret = snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
133 dev_err(rtd->dev, "Failed to set CPU BLCK ratio\n");
137 /* Set MCLK frequency to codec, onboard 11.2896MHz clock */
138 return snd_soc_dai_set_sysclk(codec_dai, DA7213_CLKSRC_MCLK, 11289600,
142 static int snd_rpi_iqaudio_codec_hw_params(struct snd_pcm_substream *substream,
143 struct snd_pcm_hw_params *params)
145 struct snd_soc_pcm_runtime *rtd = substream->private_data;
146 unsigned int samplerate = params_rate(params);
148 switch (samplerate) {
154 pll_out = DA7213_PLL_FREQ_OUT_98304000;
158 pll_out = DA7213_PLL_FREQ_OUT_90316800;
161 dev_err(rtd->dev,"Unsupported samplerate %d\n", samplerate);
166 static const struct snd_soc_ops snd_rpi_iqaudio_codec_ops = {
167 .hw_params = snd_rpi_iqaudio_codec_hw_params,
170 SND_SOC_DAILINK_DEFS(rpi_iqaudio,
171 DAILINK_COMP_ARRAY(COMP_CPU("bcm2708-i2s.0")),
172 DAILINK_COMP_ARRAY(COMP_CODEC("da7213.1-001a", "da7213-hifi")),
173 DAILINK_COMP_ARRAY(COMP_PLATFORM("bcm2835-i2s.0")));
175 static struct snd_soc_dai_link snd_rpi_iqaudio_codec_dai[] = {
177 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
178 SND_SOC_DAIFMT_CBM_CFM,
179 .init = snd_rpi_iqaudio_codec_init,
180 .ops = &snd_rpi_iqaudio_codec_ops,
182 .symmetric_channels = 1,
183 .symmetric_sample_bits = 1,
184 SND_SOC_DAILINK_REG(rpi_iqaudio),
188 /* audio machine driver */
189 static struct snd_soc_card snd_rpi_iqaudio_codec = {
190 .owner = THIS_MODULE,
191 .dai_link = snd_rpi_iqaudio_codec_dai,
192 .num_links = ARRAY_SIZE(snd_rpi_iqaudio_codec_dai),
193 .controls = dapm_controls,
194 .num_controls = ARRAY_SIZE(dapm_controls),
195 .dapm_widgets = dapm_widgets,
196 .num_dapm_widgets = ARRAY_SIZE(dapm_widgets),
197 .dapm_routes = audio_map,
198 .num_dapm_routes = ARRAY_SIZE(audio_map),
201 static int snd_rpi_iqaudio_codec_probe(struct platform_device *pdev)
205 snd_rpi_iqaudio_codec.dev = &pdev->dev;
207 if (pdev->dev.of_node) {
208 struct device_node *i2s_node;
209 struct snd_soc_card *card = &snd_rpi_iqaudio_codec;
210 struct snd_soc_dai_link *dai = &snd_rpi_iqaudio_codec_dai[0];
212 i2s_node = of_parse_phandle(pdev->dev.of_node,
213 "i2s-controller", 0);
215 dai->cpus->dai_name = NULL;
216 dai->cpus->of_node = i2s_node;
217 dai->platforms->name = NULL;
218 dai->platforms->of_node = i2s_node;
221 if (of_property_read_string(pdev->dev.of_node, "card_name",
223 card->name = "IQaudIOCODEC";
225 if (of_property_read_string(pdev->dev.of_node, "dai_name",
227 dai->name = "IQaudIO CODEC";
229 if (of_property_read_string(pdev->dev.of_node,
230 "dai_stream_name", &dai->stream_name))
231 dai->stream_name = "IQaudIO CODEC HiFi v1.2";
235 ret = snd_soc_register_card(&snd_rpi_iqaudio_codec);
237 if (ret != -EPROBE_DEFER)
239 "snd_soc_register_card() failed: %d\n", ret);
246 static int snd_rpi_iqaudio_codec_remove(struct platform_device *pdev)
248 return snd_soc_unregister_card(&snd_rpi_iqaudio_codec);
251 static const struct of_device_id iqaudio_of_match[] = {
252 { .compatible = "iqaudio,iqaudio-codec", },
256 MODULE_DEVICE_TABLE(of, iqaudio_of_match);
258 static struct platform_driver snd_rpi_iqaudio_codec_driver = {
260 .name = "snd-rpi-iqaudio-codec",
261 .owner = THIS_MODULE,
262 .of_match_table = iqaudio_of_match,
264 .probe = snd_rpi_iqaudio_codec_probe,
265 .remove = snd_rpi_iqaudio_codec_remove,
270 module_platform_driver(snd_rpi_iqaudio_codec_driver);
272 MODULE_AUTHOR("Gordon Garrity <gordon@iqaudio.com>");
273 MODULE_DESCRIPTION("ASoC Driver for IQaudIO CODEC");
274 MODULE_LICENSE("GPL v2");