1 From 058ee9a51d642274c5c934da7718ddc161dff612 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 088/773] ASoC: Create a generic Pi Hat WM8804 driver
6 Reduce the amount of duplicated code by creating a generic driver for
7 Pi Hat digi cards using the WM8804 codec.
10 Allo DigiOne, Hifiberry Digi/Pro, JustBoom Digi and IQAudIO Digi
11 dedicate soundcard drivers with a generic driver.
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
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
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
30 +++ b/sound/soc/bcm/rpi-wm8804-soundcard.c
32 +// SPDX-License-Identifier: GPL-2.0
34 + * rpi--wm8804.c -- ALSA SoC Raspberry Pi soundcard.
36 + * Copyright (C) 2018 Raspberry Pi.
38 + * Authors: Tim Gover <tim.gover@raspberrypi.org>
40 + * Generic driver for Pi Hat WM8804 digi sounds cards
42 + * Based upon code from:
44 + * by Milan Neskovic <info@justboom.co>
47 + * by Daniel Matuschek <info@crazy-audio.com>
50 + * by Baswaraj <jaikumar@cem-solutions.net>
53 + * Daniel Matuschek <info@crazy-audio.com>
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.
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.
65 +#include <linux/gpio/consumer.h>
66 +#include <linux/platform_device.h>
67 +#include <linux/module.h>
69 +#include <sound/core.h>
70 +#include <sound/pcm.h>
71 +#include <sound/pcm_params.h>
72 +#include <sound/soc.h>
74 +#include "../codecs/wm8804.h"
76 +struct wm8804_clk_cfg {
77 + unsigned int sysclk_freq;
78 + unsigned int mclk_freq;
79 + unsigned int mclk_div;
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);
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");
102 +static struct gpio_desc *snd_clk44gpio;
103 +static struct gpio_desc *snd_clk48gpio;
105 +#define CLK_44EN_RATE 22579200UL
106 +#define CLK_48EN_RATE 24576000UL
108 +static int snd_rpi_wm8804_init(struct snd_soc_pcm_runtime *rtd)
110 + struct snd_soc_component *component = rtd->codec_dai->component;
113 + pr_debug("%s\n", __func__);
115 + rc = snd_soc_component_update_bits(component, WM8804_PWRDN, 0x4, 0x0);
116 + return rc < 0 ? rc : 0;
119 +static int snd_rpi_wm8804_digi_startup(struct snd_pcm_substream *substream)
121 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
122 + struct snd_soc_component *component = rtd->codec_dai->component;
125 + pr_debug("%s\n", __func__);
127 + rc = snd_soc_component_update_bits(component, WM8804_PWRDN, 0x3c, 0x00);
128 + return rc < 0 ? rc : 0;
131 +static void snd_rpi_wm8804_digi_shutdown(struct snd_pcm_substream *substream)
133 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
134 + struct snd_soc_component *component = rtd->codec_dai->component;
136 + pr_debug("%s %d\n", __func__, auto_shutdown_output);
138 + if (auto_shutdown_output)
139 + snd_soc_component_update_bits(component, WM8804_PWRDN,
143 +static unsigned int snd_rpi_wm8804_enable_clock(unsigned int samplerate)
145 + switch (samplerate) {
151 + gpiod_set_value_cansleep(snd_clk44gpio, 1);
152 + gpiod_set_value_cansleep(snd_clk48gpio, 0);
153 + return CLK_44EN_RATE;
155 + gpiod_set_value_cansleep(snd_clk48gpio, 1);
156 + gpiod_set_value_cansleep(snd_clk44gpio, 0);
157 + return CLK_48EN_RATE;
161 +static void snd_rpi_wm8804_clk_cfg(unsigned int samplerate,
162 + struct wm8804_clk_cfg *clk_cfg)
164 + clk_cfg->mclk_freq = 0;
165 + clk_cfg->mclk_div = 1;
166 + clk_cfg->sysclk_freq = 27000000;
168 + if (samplerate <= 96000) {
169 + clk_cfg->mclk_freq = samplerate * 256;
170 + clk_cfg->mclk_div = WM8804_MCLKDIV_256FS;
172 + clk_cfg->mclk_freq = samplerate * 128;
173 + clk_cfg->mclk_div = WM8804_MCLKDIV_128FS;
176 + if (!(IS_ERR(snd_clk44gpio) || IS_ERR(snd_clk48gpio)))
177 + clk_cfg->sysclk_freq = snd_rpi_wm8804_enable_clock(samplerate);
180 +static int snd_rpi_wm8804_hw_params(struct snd_pcm_substream *substream,
181 + struct snd_pcm_hw_params *params)
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;
189 + struct wm8804_clk_cfg clk_cfg;
190 + int samplerate = params_rate(params);
192 + snd_rpi_wm8804_clk_cfg(samplerate, &clk_cfg);
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);
198 + switch (samplerate) {
200 + sampling_freq = 0x03;
203 + sampling_freq = 0x00;
206 + sampling_freq = 0x02;
209 + sampling_freq = 0x08;
212 + sampling_freq = 0x0a;
215 + sampling_freq = 0x0c;
218 + sampling_freq = 0x0e;
221 + dev_err(rtd->card->dev,
222 + "Failed to set WM8804 SYSCLK, unsupported samplerate %d\n",
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);
230 + ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
231 + clk_cfg.sysclk_freq, SND_SOC_CLOCK_OUT);
233 + dev_err(rtd->card->dev,
234 + "Failed to set WM8804 SYSCLK: %d\n", ret);
238 + /* Enable TX output */
239 + snd_soc_component_update_bits(component, WM8804_PWRDN, 0x4, 0x0);
242 + snd_soc_component_update_bits(component, WM8804_PWRDN, 0x9, 0);
244 + /* set sampling frequency status bits */
245 + snd_soc_component_update_bits(component, WM8804_SPDTX4, 0x0f,
248 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
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,
257 +static struct snd_soc_dai_link snd_justboom_digi_dai[] = {
259 + .name = "JustBoom Digi",
260 + .stream_name = "JustBoom Digi HiFi",
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,
270 +static struct snd_soc_dai_link snd_iqaudio_digi_dai[] = {
272 + .name = "IQAudIO Digi",
273 + .stream_name = "IQAudIO Digi HiFi",
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",
285 +static int snd_allo_digione_probe(struct platform_device *pdev)
287 + pr_debug("%s\n", __func__);
289 + if (IS_ERR(snd_clk44gpio) || IS_ERR(snd_clk48gpio)) {
290 + dev_err(&pdev->dev, "devm_gpiod_get() failed\n");
296 +static struct snd_soc_dai_link snd_allo_digione_dai[] = {
298 + .name = "Allo DigiOne",
299 + .stream_name = "Allo DigiOne HiFi",
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,
309 +static struct snd_soc_dai_link snd_hifiberry_digi_dai[] = {
311 + .name = "HifiBerry Digi",
312 + .stream_name = "HifiBerry Digi HiFi",
316 +static int snd_hifiberry_digi_probe(struct platform_device *pdev)
318 + pr_debug("%s\n", __func__);
320 + if (IS_ERR(snd_clk44gpio) || IS_ERR(snd_clk48gpio))
323 + snd_hifiberry_digi_dai->name = "HiFiBerry Digi+ Pro";
324 + snd_hifiberry_digi_dai->stream_name = "HiFiBerry Digi+ Pro HiFi";
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,
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 },
346 +static struct snd_soc_card snd_rpi_wm8804 = {
347 + .driver_name = "RPi-WM8804",
348 + .owner = THIS_MODULE,
353 +static int snd_rpi_wm8804_probe(struct platform_device *pdev)
356 + const struct of_device_id *of_id;
358 + snd_rpi_wm8804.dev = &pdev->dev;
359 + of_id = of_match_node(snd_rpi_wm8804_of_match, pdev->dev.of_node);
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;
367 + snd_soc_card_set_drvdata(&snd_rpi_wm8804, drvdata);
370 + dai->init = snd_rpi_wm8804_init;
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";
378 + dai->dai_fmt = SND_SOC_DAIFMT_I2S |
379 + SND_SOC_DAIFMT_NB_NF |
380 + SND_SOC_DAIFMT_CBM_CFM;
382 + if (drvdata->auto_shutdown_output)
383 + auto_shutdown_output = 1;
385 + snd_rpi_wm8804.dai_link = dai;
386 + i2s_node = of_parse_phandle(pdev->dev.of_node,
387 + "i2s-controller", 0);
389 + pr_err("Failed to find i2s-controller DT node\n");
393 + snd_rpi_wm8804.name = drvdata->card_name;
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);
401 + if (drvdata->dai_name_dt)
402 + of_property_read_string(i2s_node,
403 + drvdata->dai_name_dt,
406 + if (drvdata->dai_stream_name_dt)
407 + of_property_read_string(i2s_node,
408 + drvdata->dai_stream_name_dt,
409 + &dai->stream_name);
411 + dai->cpu_of_node = i2s_node;
412 + dai->platform_of_node = i2s_node;
415 + * clk44gpio and clk48gpio are not required by all cards so
416 + * don't check the error status.
419 + devm_gpiod_get(&pdev->dev, "clock44", GPIOD_OUT_LOW);
422 + devm_gpiod_get(&pdev->dev, "clock48", GPIOD_OUT_LOW);
424 + if (drvdata->probe) {
425 + ret = drvdata->probe(pdev);
427 + dev_err(&pdev->dev, "Custom probe failed %d\n",
433 + pr_debug("%s card: %s dai: %s stream: %s\n", __func__,
434 + snd_rpi_wm8804.name,
435 + dai->name, dai->stream_name);
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);
445 +static struct platform_driver snd_rpi_wm8804_driver = {
447 + .name = "snd-rpi-wm8804",
448 + .owner = THIS_MODULE,
449 + .of_match_table = snd_rpi_wm8804_of_match,
451 + .probe = snd_rpi_wm8804_probe,
453 +MODULE_DEVICE_TABLE(of, snd_rpi_wm8804_of_match);
455 +module_platform_driver(snd_rpi_wm8804_driver);
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");