Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / sound / soc / bcm / justboom-both.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * rpi--wm8804.c -- ALSA SoC Raspberry Pi soundcard.
4  *
5  * Authors: Johannes Krude <johannes@krude.de
6  *
7  * Driver for when connecting simultaneously justboom-digi and justboom-dac
8  *
9  * Based upon code from:
10  * justboom-digi.c
11  * by Milan Neskovic <info@justboom.co>
12  * justboom-dac.c
13  * by Milan Neskovic <info@justboom.co>
14  *
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.
18  *
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.
23  */
24
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27
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>
33
34 #include "../codecs/wm8804.h"
35 #include "../codecs/pcm512x.h"
36
37
38 static bool digital_gain_0db_limit = true;
39
40 static int snd_rpi_justboom_both_init(struct snd_soc_pcm_runtime *rtd)
41 {
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;
44
45         /* enable  TX output */
46         snd_soc_component_update_bits(digi, WM8804_PWRDN, 0x4, 0x0);
47
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);
51
52         if (digital_gain_0db_limit) {
53                 int ret;
54                 struct snd_soc_card *card = rtd->card;
55
56                 ret = snd_soc_limit_volume(card, "Digital Playback Volume",
57                                                                         207);
58                 if (ret < 0)
59                         dev_warn(card->dev, "Failed to set volume limit: %d\n",
60                                                                         ret);
61         }
62
63         return 0;
64 }
65
66 static int snd_rpi_justboom_both_hw_params(struct snd_pcm_substream *substream,
67                                        struct snd_pcm_hw_params *params)
68 {
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);
73
74         int sysclk = 27000000; /* This is fixed on this board */
75
76         long mclk_freq    = 0;
77         int mclk_div      = 1;
78         int sampling_freq = 1;
79
80         int ret;
81
82         int samplerate = params_rate(params);
83
84         if (samplerate <= 96000) {
85                 mclk_freq = samplerate*256;
86                 mclk_div  = WM8804_MCLKDIV_256FS;
87         } else {
88                 mclk_freq = samplerate*128;
89                 mclk_div  = WM8804_MCLKDIV_128FS;
90         }
91
92         switch (samplerate) {
93         case 32000:
94                 sampling_freq = 0x03;
95                 break;
96         case 44100:
97                 sampling_freq = 0x00;
98                 break;
99         case 48000:
100                 sampling_freq = 0x02;
101                 break;
102         case 88200:
103                 sampling_freq = 0x08;
104                 break;
105         case 96000:
106                 sampling_freq = 0x0a;
107                 break;
108         case 176400:
109                 sampling_freq = 0x0c;
110                 break;
111         case 192000:
112                 sampling_freq = 0x0e;
113                 break;
114         default:
115                 dev_err(rtd->card->dev,
116                 "Failed to set WM8804 SYSCLK, unsupported samplerate %d\n",
117                 samplerate);
118         }
119
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);
122
123         ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
124                                         sysclk, SND_SOC_CLOCK_OUT);
125         if (ret < 0) {
126                 dev_err(rtd->card->dev,
127                 "Failed to set WM8804 SYSCLK: %d\n", ret);
128                 return ret;
129         }
130
131         /* Enable TX output */
132         snd_soc_component_update_bits(digi, WM8804_PWRDN, 0x4, 0x0);
133
134         /* Power on */
135         snd_soc_component_update_bits(digi, WM8804_PWRDN, 0x9, 0);
136
137         /* set sampling frequency status bits */
138         snd_soc_component_update_bits(digi, WM8804_SPDTX4, 0x0f, sampling_freq);
139
140         return snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
141 }
142
143 static int snd_rpi_justboom_both_startup(struct snd_pcm_substream *substream)
144 {
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;
148
149         /* turn on digital output */
150         snd_soc_component_update_bits(digi, WM8804_PWRDN, 0x3c, 0x00);
151
152         snd_soc_component_update_bits(dac, PCM512x_GPIO_CONTROL_1, 0x08, 0x08);
153
154         return 0;
155 }
156
157 static void snd_rpi_justboom_both_shutdown(struct snd_pcm_substream *substream)
158 {
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;
162
163         snd_soc_component_update_bits(dac, PCM512x_GPIO_CONTROL_1, 0x08, 0x00);
164
165         /* turn off output */
166         snd_soc_component_update_bits(digi, WM8804_PWRDN, 0x3c, 0x3c);
167 }
168
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,
174 };
175
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")));
181
182 static struct snd_soc_dai_link snd_rpi_justboom_both_dai[] = {
183 {
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),
191 },
192 };
193
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),
201 };
202
203 static int snd_rpi_justboom_both_probe(struct platform_device *pdev)
204 {
205         int ret = 0;
206         struct snd_soc_card *card = &snd_rpi_justboom_both;
207
208         snd_rpi_justboom_both.dev = &pdev->dev;
209
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];
213
214                 i2s_node = of_parse_phandle(pdev->dev.of_node,
215                                             "i2s-controller", 0);
216
217                 if (i2s_node) {
218                         int i;
219
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;
225                         }
226                 }
227
228                 digital_gain_0db_limit = !of_property_read_bool(
229                         pdev->dev.of_node, "justboom,24db_digital_gain");
230         }
231
232         ret = snd_soc_register_card(card);
233         if (ret && ret != -EPROBE_DEFER) {
234                 dev_err(&pdev->dev,
235                         "snd_soc_register_card() failed: %d\n", ret);
236         }
237
238         return ret;
239 }
240
241 static int snd_rpi_justboom_both_remove(struct platform_device *pdev)
242 {
243         return snd_soc_unregister_card(&snd_rpi_justboom_both);
244 }
245
246 static const struct of_device_id snd_rpi_justboom_both_of_match[] = {
247         { .compatible = "justboom,justboom-both", },
248         {},
249 };
250 MODULE_DEVICE_TABLE(of, snd_rpi_justboom_both_of_match);
251
252 static struct platform_driver snd_rpi_justboom_both_driver = {
253         .driver = {
254                 .name   = "snd-rpi-justboom-both",
255                 .owner  = THIS_MODULE,
256                 .of_match_table = snd_rpi_justboom_both_of_match,
257         },
258         .probe          = snd_rpi_justboom_both_probe,
259         .remove         = snd_rpi_justboom_both_remove,
260 };
261
262 module_platform_driver(snd_rpi_justboom_both_driver);
263
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");