1 // SPDX-License-Identifier: GPL-2.0
3 * rpi--wm8804.c -- ALSA SoC Raspberry Pi soundcard.
5 * Authors: Johannes Krude <johannes@krude.de
7 * Driver for when connecting simultaneously justboom-digi and justboom-dac
9 * Based upon code from:
11 * by Milan Neskovic <info@justboom.co>
13 * by Milan Neskovic <info@justboom.co>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * version 2 as published by the Free Software Foundation.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/jack.h>
34 #include "../codecs/wm8804.h"
35 #include "../codecs/pcm512x.h"
38 static bool digital_gain_0db_limit = true;
40 static int snd_rpi_justboom_both_init(struct snd_soc_pcm_runtime *rtd)
42 struct snd_soc_component *digi = asoc_rtd_to_codec(rtd, 0)->component;
43 struct snd_soc_component *dac = asoc_rtd_to_codec(rtd, 1)->component;
45 /* enable TX output */
46 snd_soc_component_update_bits(digi, WM8804_PWRDN, 0x4, 0x0);
48 snd_soc_component_update_bits(dac, PCM512x_GPIO_EN, 0x08, 0x08);
49 snd_soc_component_update_bits(dac, PCM512x_GPIO_OUTPUT_4, 0xf, 0x02);
50 snd_soc_component_update_bits(dac, PCM512x_GPIO_CONTROL_1, 0x08, 0x08);
52 if (digital_gain_0db_limit) {
54 struct snd_soc_card *card = rtd->card;
56 ret = snd_soc_limit_volume(card, "Digital Playback Volume",
59 dev_warn(card->dev, "Failed to set volume limit: %d\n",
66 static int snd_rpi_justboom_both_hw_params(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params *params)
69 struct snd_soc_pcm_runtime *rtd = substream->private_data;
70 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
71 struct snd_soc_component *digi = asoc_rtd_to_codec(rtd, 0)->component;
72 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
74 int sysclk = 27000000; /* This is fixed on this board */
78 int sampling_freq = 1;
82 int samplerate = params_rate(params);
84 if (samplerate <= 96000) {
85 mclk_freq = samplerate*256;
86 mclk_div = WM8804_MCLKDIV_256FS;
88 mclk_freq = samplerate*128;
89 mclk_div = WM8804_MCLKDIV_128FS;
100 sampling_freq = 0x02;
103 sampling_freq = 0x08;
106 sampling_freq = 0x0a;
109 sampling_freq = 0x0c;
112 sampling_freq = 0x0e;
115 dev_err(rtd->card->dev,
116 "Failed to set WM8804 SYSCLK, unsupported samplerate %d\n",
120 snd_soc_dai_set_clkdiv(codec_dai, WM8804_MCLK_DIV, mclk_div);
121 snd_soc_dai_set_pll(codec_dai, 0, 0, sysclk, mclk_freq);
123 ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
124 sysclk, SND_SOC_CLOCK_OUT);
126 dev_err(rtd->card->dev,
127 "Failed to set WM8804 SYSCLK: %d\n", ret);
131 /* Enable TX output */
132 snd_soc_component_update_bits(digi, WM8804_PWRDN, 0x4, 0x0);
135 snd_soc_component_update_bits(digi, WM8804_PWRDN, 0x9, 0);
137 /* set sampling frequency status bits */
138 snd_soc_component_update_bits(digi, WM8804_SPDTX4, 0x0f, sampling_freq);
140 return snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
143 static int snd_rpi_justboom_both_startup(struct snd_pcm_substream *substream)
145 struct snd_soc_pcm_runtime *rtd = substream->private_data;
146 struct snd_soc_component *digi = asoc_rtd_to_codec(rtd, 0)->component;
147 struct snd_soc_component *dac = asoc_rtd_to_codec(rtd, 1)->component;
149 /* turn on digital output */
150 snd_soc_component_update_bits(digi, WM8804_PWRDN, 0x3c, 0x00);
152 snd_soc_component_update_bits(dac, PCM512x_GPIO_CONTROL_1, 0x08, 0x08);
157 static void snd_rpi_justboom_both_shutdown(struct snd_pcm_substream *substream)
159 struct snd_soc_pcm_runtime *rtd = substream->private_data;
160 struct snd_soc_component *digi = asoc_rtd_to_codec(rtd, 0)->component;
161 struct snd_soc_component *dac = asoc_rtd_to_codec(rtd, 1)->component;
163 snd_soc_component_update_bits(dac, PCM512x_GPIO_CONTROL_1, 0x08, 0x00);
165 /* turn off output */
166 snd_soc_component_update_bits(digi, WM8804_PWRDN, 0x3c, 0x3c);
169 /* machine stream operations */
170 static struct snd_soc_ops snd_rpi_justboom_both_ops = {
171 .hw_params = snd_rpi_justboom_both_hw_params,
172 .startup = snd_rpi_justboom_both_startup,
173 .shutdown = snd_rpi_justboom_both_shutdown,
176 SND_SOC_DAILINK_DEFS(rpi_justboom_both,
177 DAILINK_COMP_ARRAY(COMP_CPU("bcm2708-i2s.0")),
178 DAILINK_COMP_ARRAY(COMP_CODEC("pcm512x.1-004d", "pcm512x-hifi"),
179 COMP_CODEC("wm8804.1-003b", "wm8804-spdif")),
180 DAILINK_COMP_ARRAY(COMP_PLATFORM("bcm2708-i2s.0")));
182 static struct snd_soc_dai_link snd_rpi_justboom_both_dai[] = {
184 .name = "JustBoom Digi",
185 .stream_name = "JustBoom Digi HiFi",
186 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
187 SND_SOC_DAIFMT_CBM_CFM,
188 .ops = &snd_rpi_justboom_both_ops,
189 .init = snd_rpi_justboom_both_init,
190 SND_SOC_DAILINK_REG(rpi_justboom_both),
194 /* audio machine driver */
195 static struct snd_soc_card snd_rpi_justboom_both = {
196 .name = "snd_rpi_justboom_both",
197 .driver_name = "JustBoomBoth",
198 .owner = THIS_MODULE,
199 .dai_link = snd_rpi_justboom_both_dai,
200 .num_links = ARRAY_SIZE(snd_rpi_justboom_both_dai),
203 static int snd_rpi_justboom_both_probe(struct platform_device *pdev)
206 struct snd_soc_card *card = &snd_rpi_justboom_both;
208 snd_rpi_justboom_both.dev = &pdev->dev;
210 if (pdev->dev.of_node) {
211 struct device_node *i2s_node;
212 struct snd_soc_dai_link *dai = &snd_rpi_justboom_both_dai[0];
214 i2s_node = of_parse_phandle(pdev->dev.of_node,
215 "i2s-controller", 0);
220 for (i = 0; i < card->num_links; i++) {
221 dai->cpus->dai_name = NULL;
222 dai->cpus->of_node = i2s_node;
223 dai->platforms->name = NULL;
224 dai->platforms->of_node = i2s_node;
228 digital_gain_0db_limit = !of_property_read_bool(
229 pdev->dev.of_node, "justboom,24db_digital_gain");
232 ret = snd_soc_register_card(card);
233 if (ret && ret != -EPROBE_DEFER) {
235 "snd_soc_register_card() failed: %d\n", ret);
241 static int snd_rpi_justboom_both_remove(struct platform_device *pdev)
243 return snd_soc_unregister_card(&snd_rpi_justboom_both);
246 static const struct of_device_id snd_rpi_justboom_both_of_match[] = {
247 { .compatible = "justboom,justboom-both", },
250 MODULE_DEVICE_TABLE(of, snd_rpi_justboom_both_of_match);
252 static struct platform_driver snd_rpi_justboom_both_driver = {
254 .name = "snd-rpi-justboom-both",
255 .owner = THIS_MODULE,
256 .of_match_table = snd_rpi_justboom_both_of_match,
258 .probe = snd_rpi_justboom_both_probe,
259 .remove = snd_rpi_justboom_both_remove,
262 module_platform_driver(snd_rpi_justboom_both_driver);
264 MODULE_AUTHOR("Johannes Krude <johannes@krude.de>");
265 MODULE_DESCRIPTION("ASoC Driver for simultaneous use of JustBoom PI Digi & DAC HAT Sound Cards");
266 MODULE_LICENSE("GPL v2");