brcm2708: rename target to bcm27xx
[openwrt/staging/wigyori.git] / target / linux / bcm27xx / patches-4.19 / 950-0086-ASoC-Create-a-generic-Pi-Hat-WM8804-driver.patch
1 From 6af5a0adda5b782ad2e134b405ca168895c89b0c Mon Sep 17 00:00:00 2001
2 From: Tim Gover <tim.gover@raspberrypi.org>
3 Date: Sat, 21 Jul 2018 20:07:46 +0100
4 Subject: [PATCH] ASoC: Create a generic Pi Hat WM8804 driver
5
6 Reduce the amount of duplicated code by creating a generic driver for
7 Pi Hat digi cards using the WM8804 codec.
8
9 This replaces the
10 Allo DigiOne, Hifiberry Digi/Pro, JustBoom Digi and IQAudIO Digi
11 dedicate soundcard drivers with a generic driver.
12
13 There are no significant changes to the runtime behavior of the drivers
14 and end users should not have to change any configuration settings
15 after upgrading.
16
17 Minor changes
18 * Check the return value of snd_soc_component_update_bits
19 * Added some pr_debug tracing
20 * Various checkpatch tidyups
21 * Updated allodigi-one to use use 128FS at > 96 Khz. This appears to
22 be an omission in the original driver code so followed the Hifiberry
23 DAC driver approach.
24 ---
25 sound/soc/bcm/rpi-wm8804-soundcard.c | 428 +++++++++++++++++++++++++++
26 1 file changed, 428 insertions(+)
27 create mode 100644 sound/soc/bcm/rpi-wm8804-soundcard.c
28
29 --- /dev/null
30 +++ b/sound/soc/bcm/rpi-wm8804-soundcard.c
31 @@ -0,0 +1,428 @@
32 +// SPDX-License-Identifier: GPL-2.0
33 +/*
34 + * rpi--wm8804.c -- ALSA SoC Raspberry Pi soundcard.
35 + *
36 + * Copyright (C) 2018 Raspberry Pi.
37 + *
38 + * Authors: Tim Gover <tim.gover@raspberrypi.org>
39 + *
40 + * Generic driver for Pi Hat WM8804 digi sounds cards
41 + *
42 + * Based upon code from:
43 + * justboom-digi.c
44 + * by Milan Neskovic <info@justboom.co>
45 + *
46 + * iqaudio_digi.c
47 + * by Daniel Matuschek <info@crazy-audio.com>
48 + *
49 + * allo-digione.c
50 + * by Baswaraj <jaikumar@cem-solutions.net>
51 + *
52 + * hifiberry-digi.c
53 + * Daniel Matuschek <info@crazy-audio.com>
54 + *
55 + * This program is free software; you can redistribute it and/or
56 + * modify it under the terms of the GNU General Public License
57 + * version 2 as published by the Free Software Foundation.
58 + *
59 + * This program is distributed in the hope that it will be useful, but
60 + * WITHOUT ANY WARRANTY; without even the implied warranty of
61 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
62 + * General Public License for more details.
63 + */
64 +
65 +#include <linux/gpio/consumer.h>
66 +#include <linux/platform_device.h>
67 +#include <linux/module.h>
68 +
69 +#include <sound/core.h>
70 +#include <sound/pcm.h>
71 +#include <sound/pcm_params.h>
72 +#include <sound/soc.h>
73 +
74 +#include "../codecs/wm8804.h"
75 +
76 +struct wm8804_clk_cfg {
77 + unsigned int sysclk_freq;
78 + unsigned int mclk_freq;
79 + unsigned int mclk_div;
80 +};
81 +
82 +/* Parameters for generic functions */
83 +struct snd_rpi_wm8804_drvdata {
84 + /* Required - pointer to the DAI structure */
85 + struct snd_soc_dai_link *dai;
86 + /* Required - snd_soc_card name */
87 + const char *card_name;
88 + /* Optional- Overrides the module paramter */
89 + unsigned short auto_shutdown_output;
90 + /* Optional DT node names if card info is defined in DT */
91 + const char *card_name_dt;
92 + const char *dai_name_dt;
93 + const char *dai_stream_name_dt;
94 + /* Optional probe extension - called prior to register_card */
95 + int (*probe)(struct platform_device *pdev);
96 +};
97 +
98 +static short int auto_shutdown_output;
99 +module_param(auto_shutdown_output, short, 0660);
100 +MODULE_PARM_DESC(auto_shutdown_output, "Shutdown SP/DIF output if playback is stopped");
101 +
102 +static struct gpio_desc *snd_clk44gpio;
103 +static struct gpio_desc *snd_clk48gpio;
104 +
105 +#define CLK_44EN_RATE 22579200UL
106 +#define CLK_48EN_RATE 24576000UL
107 +
108 +static int snd_rpi_wm8804_init(struct snd_soc_pcm_runtime *rtd)
109 +{
110 + struct snd_soc_component *component = rtd->codec_dai->component;
111 + int rc;
112 +
113 + pr_debug("%s\n", __func__);
114 +
115 + rc = snd_soc_component_update_bits(component, WM8804_PWRDN, 0x4, 0x0);
116 + return rc < 0 ? rc : 0;
117 +}
118 +
119 +static int snd_rpi_wm8804_digi_startup(struct snd_pcm_substream *substream)
120 +{
121 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
122 + struct snd_soc_component *component = rtd->codec_dai->component;
123 + int rc;
124 +
125 + pr_debug("%s\n", __func__);
126 +
127 + rc = snd_soc_component_update_bits(component, WM8804_PWRDN, 0x3c, 0x00);
128 + return rc < 0 ? rc : 0;
129 +}
130 +
131 +static void snd_rpi_wm8804_digi_shutdown(struct snd_pcm_substream *substream)
132 +{
133 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
134 + struct snd_soc_component *component = rtd->codec_dai->component;
135 +
136 + pr_debug("%s %d\n", __func__, auto_shutdown_output);
137 +
138 + if (auto_shutdown_output)
139 + snd_soc_component_update_bits(component, WM8804_PWRDN,
140 + 0x3c, 0x3c);
141 +}
142 +
143 +static unsigned int snd_rpi_wm8804_enable_clock(unsigned int samplerate)
144 +{
145 + switch (samplerate) {
146 + case 11025:
147 + case 22050:
148 + case 44100:
149 + case 88200:
150 + case 176400:
151 + gpiod_set_value_cansleep(snd_clk44gpio, 1);
152 + gpiod_set_value_cansleep(snd_clk48gpio, 0);
153 + return CLK_44EN_RATE;
154 + default:
155 + gpiod_set_value_cansleep(snd_clk48gpio, 1);
156 + gpiod_set_value_cansleep(snd_clk44gpio, 0);
157 + return CLK_48EN_RATE;
158 + }
159 +}
160 +
161 +static void snd_rpi_wm8804_clk_cfg(unsigned int samplerate,
162 + struct wm8804_clk_cfg *clk_cfg)
163 +{
164 + clk_cfg->mclk_freq = 0;
165 + clk_cfg->mclk_div = 1;
166 + clk_cfg->sysclk_freq = 27000000;
167 +
168 + if (samplerate <= 96000) {
169 + clk_cfg->mclk_freq = samplerate * 256;
170 + clk_cfg->mclk_div = WM8804_MCLKDIV_256FS;
171 + } else {
172 + clk_cfg->mclk_freq = samplerate * 128;
173 + clk_cfg->mclk_div = WM8804_MCLKDIV_128FS;
174 + }
175 +
176 + if (!(IS_ERR(snd_clk44gpio) || IS_ERR(snd_clk48gpio)))
177 + clk_cfg->sysclk_freq = snd_rpi_wm8804_enable_clock(samplerate);
178 +}
179 +
180 +static int snd_rpi_wm8804_hw_params(struct snd_pcm_substream *substream,
181 + struct snd_pcm_hw_params *params)
182 +{
183 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
184 + struct snd_soc_dai *codec_dai = rtd->codec_dai;
185 + struct snd_soc_component *component = rtd->codec_dai->component;
186 + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
187 + int sampling_freq = 1;
188 + int ret;
189 + struct wm8804_clk_cfg clk_cfg;
190 + int samplerate = params_rate(params);
191 +
192 + snd_rpi_wm8804_clk_cfg(samplerate, &clk_cfg);
193 +
194 + pr_debug("%s samplerate: %d mclk_freq: %u mclk_div: %u sysclk: %u\n",
195 + __func__, samplerate, clk_cfg.mclk_freq,
196 + clk_cfg.mclk_div, clk_cfg.sysclk_freq);
197 +
198 + switch (samplerate) {
199 + case 32000:
200 + sampling_freq = 0x03;
201 + break;
202 + case 44100:
203 + sampling_freq = 0x00;
204 + break;
205 + case 48000:
206 + sampling_freq = 0x02;
207 + break;
208 + case 88200:
209 + sampling_freq = 0x08;
210 + break;
211 + case 96000:
212 + sampling_freq = 0x0a;
213 + break;
214 + case 176400:
215 + sampling_freq = 0x0c;
216 + break;
217 + case 192000:
218 + sampling_freq = 0x0e;
219 + break;
220 + default:
221 + dev_err(rtd->card->dev,
222 + "Failed to set WM8804 SYSCLK, unsupported samplerate %d\n",
223 + samplerate);
224 + }
225 +
226 + snd_soc_dai_set_clkdiv(codec_dai, WM8804_MCLK_DIV, clk_cfg.mclk_div);
227 + snd_soc_dai_set_pll(codec_dai, 0, 0,
228 + clk_cfg.sysclk_freq, clk_cfg.mclk_freq);
229 +
230 + ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
231 + clk_cfg.sysclk_freq, SND_SOC_CLOCK_OUT);
232 + if (ret < 0) {
233 + dev_err(rtd->card->dev,
234 + "Failed to set WM8804 SYSCLK: %d\n", ret);
235 + return ret;
236 + }
237 +
238 + /* Enable TX output */
239 + snd_soc_component_update_bits(component, WM8804_PWRDN, 0x4, 0x0);
240 +
241 + /* Power on */
242 + snd_soc_component_update_bits(component, WM8804_PWRDN, 0x9, 0);
243 +
244 + /* set sampling frequency status bits */
245 + snd_soc_component_update_bits(component, WM8804_SPDTX4, 0x0f,
246 + sampling_freq);
247 +
248 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
249 +}
250 +
251 +static struct snd_soc_ops snd_rpi_wm8804_ops = {
252 + .hw_params = snd_rpi_wm8804_hw_params,
253 + .startup = snd_rpi_wm8804_digi_startup,
254 + .shutdown = snd_rpi_wm8804_digi_shutdown,
255 +};
256 +
257 +static struct snd_soc_dai_link snd_justboom_digi_dai[] = {
258 +{
259 + .name = "JustBoom Digi",
260 + .stream_name = "JustBoom Digi HiFi",
261 +},
262 +};
263 +
264 +static struct snd_rpi_wm8804_drvdata drvdata_justboom_digi = {
265 + .card_name = "snd_rpi_justboom_digi",
266 + .dai = snd_justboom_digi_dai,
267 + .auto_shutdown_output = 1,
268 +};
269 +
270 +static struct snd_soc_dai_link snd_iqaudio_digi_dai[] = {
271 +{
272 + .name = "IQAudIO Digi",
273 + .stream_name = "IQAudIO Digi HiFi",
274 +},
275 +};
276 +
277 +static struct snd_rpi_wm8804_drvdata drvdata_iqaudio_digi = {
278 + .card_name = "IQAudIODigi",
279 + .dai = snd_iqaudio_digi_dai,
280 + .card_name_dt = "wm8804-digi,card-name",
281 + .dai_name_dt = "wm8804-digi,dai-name",
282 + .dai_stream_name_dt = "wm8804-digi,dai-stream-name",
283 +};
284 +
285 +static int snd_allo_digione_probe(struct platform_device *pdev)
286 +{
287 + pr_debug("%s\n", __func__);
288 +
289 + if (IS_ERR(snd_clk44gpio) || IS_ERR(snd_clk48gpio)) {
290 + dev_err(&pdev->dev, "devm_gpiod_get() failed\n");
291 + return -EINVAL;
292 + }
293 + return 0;
294 +}
295 +
296 +static struct snd_soc_dai_link snd_allo_digione_dai[] = {
297 +{
298 + .name = "Allo DigiOne",
299 + .stream_name = "Allo DigiOne HiFi",
300 +},
301 +};
302 +
303 +static struct snd_rpi_wm8804_drvdata drvdata_allo_digione = {
304 + .card_name = "snd_allo_digione",
305 + .dai = snd_allo_digione_dai,
306 + .probe = snd_allo_digione_probe,
307 +};
308 +
309 +static struct snd_soc_dai_link snd_hifiberry_digi_dai[] = {
310 +{
311 + .name = "HifiBerry Digi",
312 + .stream_name = "HifiBerry Digi HiFi",
313 +},
314 +};
315 +
316 +static int snd_hifiberry_digi_probe(struct platform_device *pdev)
317 +{
318 + pr_debug("%s\n", __func__);
319 +
320 + if (IS_ERR(snd_clk44gpio) || IS_ERR(snd_clk48gpio))
321 + return 0;
322 +
323 + snd_hifiberry_digi_dai->name = "HiFiBerry Digi+ Pro";
324 + snd_hifiberry_digi_dai->stream_name = "HiFiBerry Digi+ Pro HiFi";
325 + return 0;
326 +}
327 +
328 +static struct snd_rpi_wm8804_drvdata drvdata_hifiberry_digi = {
329 + .card_name = "snd_rpi_hifiberry_digi",
330 + .dai = snd_hifiberry_digi_dai,
331 + .probe = snd_hifiberry_digi_probe,
332 +};
333 +
334 +static const struct of_device_id snd_rpi_wm8804_of_match[] = {
335 + { .compatible = "justboom,justboom-digi",
336 + .data = (void *) &drvdata_justboom_digi },
337 + { .compatible = "iqaudio,wm8804-digi",
338 + .data = (void *) &drvdata_iqaudio_digi },
339 + { .compatible = "allo,allo-digione",
340 + .data = (void *) &drvdata_allo_digione },
341 + { .compatible = "hifiberry,hifiberry-digi",
342 + .data = (void *) &drvdata_hifiberry_digi },
343 + {},
344 +};
345 +
346 +static struct snd_soc_card snd_rpi_wm8804 = {
347 + .driver_name = "RPi-WM8804",
348 + .owner = THIS_MODULE,
349 + .dai_link = NULL,
350 + .num_links = 1,
351 +};
352 +
353 +static int snd_rpi_wm8804_probe(struct platform_device *pdev)
354 +{
355 + int ret = 0;
356 + const struct of_device_id *of_id;
357 +
358 + snd_rpi_wm8804.dev = &pdev->dev;
359 + of_id = of_match_node(snd_rpi_wm8804_of_match, pdev->dev.of_node);
360 +
361 + if (pdev->dev.of_node && of_id->data) {
362 + struct device_node *i2s_node;
363 + struct snd_rpi_wm8804_drvdata *drvdata =
364 + (struct snd_rpi_wm8804_drvdata *) of_id->data;
365 + struct snd_soc_dai_link *dai = drvdata->dai;
366 +
367 + snd_soc_card_set_drvdata(&snd_rpi_wm8804, drvdata);
368 +
369 + if (!dai->init)
370 + dai->init = snd_rpi_wm8804_init;
371 + if (!dai->ops)
372 + dai->ops = &snd_rpi_wm8804_ops;
373 + if (!dai->codec_dai_name)
374 + dai->codec_dai_name = "wm8804-spdif";
375 + if (!dai->codec_name)
376 + dai->codec_name = "wm8804.1-003b";
377 + if (!dai->dai_fmt)
378 + dai->dai_fmt = SND_SOC_DAIFMT_I2S |
379 + SND_SOC_DAIFMT_NB_NF |
380 + SND_SOC_DAIFMT_CBM_CFM;
381 +
382 + if (drvdata->auto_shutdown_output)
383 + auto_shutdown_output = 1;
384 +
385 + snd_rpi_wm8804.dai_link = dai;
386 + i2s_node = of_parse_phandle(pdev->dev.of_node,
387 + "i2s-controller", 0);
388 + if (!i2s_node) {
389 + pr_err("Failed to find i2s-controller DT node\n");
390 + return -ENODEV;
391 + }
392 +
393 + snd_rpi_wm8804.name = drvdata->card_name;
394 +
395 + /* If requested by in drvdata get card & DAI names from DT */
396 + if (drvdata->card_name_dt)
397 + of_property_read_string(i2s_node,
398 + drvdata->card_name_dt,
399 + &snd_rpi_wm8804.name);
400 +
401 + if (drvdata->dai_name_dt)
402 + of_property_read_string(i2s_node,
403 + drvdata->dai_name_dt,
404 + &dai->name);
405 +
406 + if (drvdata->dai_stream_name_dt)
407 + of_property_read_string(i2s_node,
408 + drvdata->dai_stream_name_dt,
409 + &dai->stream_name);
410 +
411 + dai->cpu_of_node = i2s_node;
412 + dai->platform_of_node = i2s_node;
413 +
414 + /*
415 + * clk44gpio and clk48gpio are not required by all cards so
416 + * don't check the error status.
417 + */
418 + snd_clk44gpio =
419 + devm_gpiod_get(&pdev->dev, "clock44", GPIOD_OUT_LOW);
420 +
421 + snd_clk48gpio =
422 + devm_gpiod_get(&pdev->dev, "clock48", GPIOD_OUT_LOW);
423 +
424 + if (drvdata->probe) {
425 + ret = drvdata->probe(pdev);
426 + if (ret < 0) {
427 + dev_err(&pdev->dev, "Custom probe failed %d\n",
428 + ret);
429 + return ret;
430 + }
431 + }
432 +
433 + pr_debug("%s card: %s dai: %s stream: %s\n", __func__,
434 + snd_rpi_wm8804.name,
435 + dai->name, dai->stream_name);
436 + }
437 +
438 + ret = devm_snd_soc_register_card(&pdev->dev, &snd_rpi_wm8804);
439 + if (ret && ret != -EPROBE_DEFER)
440 + dev_err(&pdev->dev, "Failed to register card %d\n", ret);
441 +
442 + return ret;
443 +}
444 +
445 +static struct platform_driver snd_rpi_wm8804_driver = {
446 + .driver = {
447 + .name = "snd-rpi-wm8804",
448 + .owner = THIS_MODULE,
449 + .of_match_table = snd_rpi_wm8804_of_match,
450 + },
451 + .probe = snd_rpi_wm8804_probe,
452 +};
453 +MODULE_DEVICE_TABLE(of, snd_rpi_wm8804_of_match);
454 +
455 +module_platform_driver(snd_rpi_wm8804_driver);
456 +
457 +MODULE_AUTHOR("Tim Gover <tim.gover@raspberrypi.org>");
458 +MODULE_DESCRIPTION("ASoC Raspberry Pi Hat generic digi driver for WM8804 based cards");
459 +MODULE_LICENSE("GPL v2");