1 From 7c7641a4796fa9740ed038f96484f1088587b22d Mon Sep 17 00:00:00 2001
2 From: Matthias Reichl <hias@horus.com>
3 Date: Sun, 22 Jan 2017 12:49:37 +0100
4 Subject: [PATCH 083/454] ASoC: Add driver for Cirrus Logic Audio Card
6 Note: due to problems with deferred probing of regulators
7 the following softdep should be added to a modprobe.d file
9 softdep arizona-spi pre: arizona-ldo1
11 Signed-off-by: Matthias Reichl <hias@horus.com>
13 sound/soc/bcm/Kconfig | 9 +
14 sound/soc/bcm/Makefile | 2 +
15 sound/soc/bcm/rpi-cirrus.c | 1003 ++++++++++++++++++++++++++++++++++++
16 3 files changed, 1014 insertions(+)
17 create mode 100644 sound/soc/bcm/rpi-cirrus.c
19 --- a/sound/soc/bcm/Kconfig
20 +++ b/sound/soc/bcm/Kconfig
21 @@ -46,6 +46,15 @@ config SND_BCM2708_SOC_HIFIBERRY_AMP
23 Say Y or M if you want to add support for the HifiBerry Amp amplifier board.
25 +config SND_BCM2708_SOC_RPI_CIRRUS
26 + tristate "Support for Cirrus Logic Audio Card"
27 + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
28 + select SND_SOC_WM5102
29 + select SND_SOC_WM8804
31 + Say Y or M if you want to add support for the Wolfson and
32 + Cirrus Logic audio cards.
34 config SND_BCM2708_SOC_RPI_DAC
35 tristate "Support for RPi-DAC"
36 depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
37 --- a/sound/soc/bcm/Makefile
38 +++ b/sound/soc/bcm/Makefile
39 @@ -16,6 +16,7 @@ snd-soc-hifiberry-dacplus-objs := hifibe
40 snd-soc-hifiberry-digi-objs := hifiberry_digi.o
41 snd-soc-justboom-dac-objs := justboom-dac.o
42 snd-soc-justboom-digi-objs := justboom-digi.o
43 +snd-soc-rpi-cirrus-objs := rpi-cirrus.o
44 snd-soc-rpi-dac-objs := rpi-dac.o
45 snd-soc-rpi-proto-objs := rpi-proto.o
46 snd-soc-iqaudio-dac-objs := iqaudio-dac.o
47 @@ -36,6 +37,7 @@ obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_D
48 obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) += snd-soc-hifiberry-digi.o
49 obj-$(CONFIG_SND_BCM2708_SOC_JUSTBOOM_DAC) += snd-soc-justboom-dac.o
50 obj-$(CONFIG_SND_BCM2708_SOC_JUSTBOOM_DIGI) += snd-soc-justboom-digi.o
51 +obj-$(CONFIG_SND_BCM2708_SOC_RPI_CIRRUS) += snd-soc-rpi-cirrus.o
52 obj-$(CONFIG_SND_BCM2708_SOC_RPI_DAC) += snd-soc-rpi-dac.o
53 obj-$(CONFIG_SND_BCM2708_SOC_RPI_PROTO) += snd-soc-rpi-proto.o
54 obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) += snd-soc-iqaudio-dac.o
56 +++ b/sound/soc/bcm/rpi-cirrus.c
59 + * ASoC machine driver for Cirrus Logic Audio Card
60 + * (with WM5102 and WM8804 codecs)
62 + * Copyright 2015-2017 Matthias Reichl <hias@horus.com>
64 + * Based on rpi-cirrus-sound-pi driver (c) Wolfson / Cirrus Logic Inc.
66 + * This program is free software; you can redistribute it and/or modify
67 + * it under the terms of the GNU General Public License version 2 as
68 + * published by the Free Software Foundation.
71 +#include <linux/module.h>
72 +#include <linux/mutex.h>
73 +#include <linux/slab.h>
74 +#include <linux/list.h>
75 +#include <linux/delay.h>
76 +#include <sound/pcm_params.h>
78 +#include <linux/mfd/arizona/registers.h>
80 +#include "../codecs/wm5102.h"
81 +#include "../codecs/wm8804.h"
83 +#define WM8804_CLKOUT_HZ 12000000
85 +#define RPI_CIRRUS_DEFAULT_RATE 44100
86 +#define WM5102_MAX_SYSCLK_1 49152000 /* max sysclk for 4K family */
87 +#define WM5102_MAX_SYSCLK_2 45158400 /* max sysclk for 11.025K family */
89 +static inline unsigned int calc_sysclk(unsigned int rate)
91 + return (rate % 4000) ? WM5102_MAX_SYSCLK_2 : WM5102_MAX_SYSCLK_1;
99 +struct rpi_cirrus_priv {
100 + /* mutex for synchronzing FLL1 access with DAPM */
102 + unsigned int card_rate;
103 + int sync_path_enable;
104 + int fll1_freq; /* negative means RefClock in spdif rx case */
106 + /* track hw params/free for substreams */
107 + unsigned int params_set;
108 + unsigned int min_rate_idx, max_rate_idx;
109 + unsigned char iec958_status[4];
112 +/* helper functions */
113 +static inline struct snd_soc_pcm_runtime *get_wm5102_runtime(
114 + struct snd_soc_card *card) {
115 + return snd_soc_get_pcm_runtime(card, card->dai_link[DAI_WM5102].name);
118 +static inline struct snd_soc_pcm_runtime *get_wm8804_runtime(
119 + struct snd_soc_card *card) {
120 + return snd_soc_get_pcm_runtime(card, card->dai_link[DAI_WM8804].name);
125 + unsigned int value;
129 +static struct rate_info min_rates[] = {
132 + { 44100, "44.1kHz"}
135 +#define NUM_MIN_RATES ARRAY_SIZE(min_rates)
137 +static int rpi_cirrus_min_rate_info(struct snd_kcontrol *kcontrol,
138 + struct snd_ctl_elem_info *uinfo)
140 + uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
142 + uinfo->value.enumerated.items = NUM_MIN_RATES;
144 + if (uinfo->value.enumerated.item >= NUM_MIN_RATES)
145 + uinfo->value.enumerated.item = NUM_MIN_RATES - 1;
146 + strcpy(uinfo->value.enumerated.name,
147 + min_rates[uinfo->value.enumerated.item].text);
151 +static int rpi_cirrus_min_rate_get(struct snd_kcontrol *kcontrol,
152 + struct snd_ctl_elem_value *ucontrol)
154 + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
155 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
157 + ucontrol->value.enumerated.item[0] = priv->min_rate_idx;
161 +static int rpi_cirrus_min_rate_put(struct snd_kcontrol *kcontrol,
162 + struct snd_ctl_elem_value *ucontrol)
164 + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
165 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
168 + if (priv->min_rate_idx != ucontrol->value.enumerated.item[0]) {
170 + priv->min_rate_idx = ucontrol->value.enumerated.item[0];
176 +static struct rate_info max_rates[] = {
182 +#define NUM_MAX_RATES ARRAY_SIZE(max_rates)
184 +static int rpi_cirrus_max_rate_info(struct snd_kcontrol *kcontrol,
185 + struct snd_ctl_elem_info *uinfo)
187 + uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
189 + uinfo->value.enumerated.items = NUM_MAX_RATES;
190 + if (uinfo->value.enumerated.item >= NUM_MAX_RATES)
191 + uinfo->value.enumerated.item = NUM_MAX_RATES - 1;
192 + strcpy(uinfo->value.enumerated.name,
193 + max_rates[uinfo->value.enumerated.item].text);
197 +static int rpi_cirrus_max_rate_get(struct snd_kcontrol *kcontrol,
198 + struct snd_ctl_elem_value *ucontrol)
200 + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
201 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
203 + ucontrol->value.enumerated.item[0] = priv->max_rate_idx;
207 +static int rpi_cirrus_max_rate_put(struct snd_kcontrol *kcontrol,
208 + struct snd_ctl_elem_value *ucontrol)
210 + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
211 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
214 + if (priv->max_rate_idx != ucontrol->value.enumerated.item[0]) {
216 + priv->max_rate_idx = ucontrol->value.enumerated.item[0];
222 +static int rpi_cirrus_spdif_info(struct snd_kcontrol *kcontrol,
223 + struct snd_ctl_elem_info *uinfo)
225 + uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
230 +static int rpi_cirrus_spdif_playback_get(struct snd_kcontrol *kcontrol,
231 + struct snd_ctl_elem_value *ucontrol)
233 + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
234 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
237 + for (i = 0; i < 4; i++)
238 + ucontrol->value.iec958.status[i] = priv->iec958_status[i];
243 +static int rpi_cirrus_spdif_playback_put(struct snd_kcontrol *kcontrol,
244 + struct snd_ctl_elem_value *ucontrol)
246 + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
247 + struct snd_soc_codec *wm8804_codec = get_wm8804_runtime(card)->codec;
248 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
249 + unsigned char *stat = priv->iec958_status;
250 + unsigned char *ctrl_stat = ucontrol->value.iec958.status;
252 + int i, changed = 0;
254 + for (i = 0; i < 4; i++) {
255 + mask = (i == 3) ? 0x3f : 0xff;
256 + if ((ctrl_stat[i] & mask) != (stat[i] & mask)) {
258 + stat[i] = ctrl_stat[i] & mask;
259 + snd_soc_update_bits(wm8804_codec,
260 + WM8804_SPDTX1 + i, mask, stat[i]);
267 +static int rpi_cirrus_spdif_mask_get(struct snd_kcontrol *kcontrol,
268 + struct snd_ctl_elem_value *ucontrol)
270 + ucontrol->value.iec958.status[0] = 0xff;
271 + ucontrol->value.iec958.status[1] = 0xff;
272 + ucontrol->value.iec958.status[2] = 0xff;
273 + ucontrol->value.iec958.status[3] = 0x3f;
278 +static int rpi_cirrus_spdif_capture_get(struct snd_kcontrol *kcontrol,
279 + struct snd_ctl_elem_value *ucontrol)
281 + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
282 + struct snd_soc_codec *wm8804_codec = get_wm8804_runtime(card)->codec;
286 + for (i = 0; i < 4; i++) {
287 + mask = (i == 3) ? 0x3f : 0xff;
288 + ucontrol->value.iec958.status[i] =
289 + snd_soc_read(wm8804_codec, WM8804_RXCHAN1 + i) & mask;
295 +#define SPDIF_FLAG_CTRL(desc, reg, bit, invert) \
297 + .access = SNDRV_CTL_ELEM_ACCESS_READ \
298 + | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
299 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
300 + .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, NONE) \
302 + .info = snd_ctl_boolean_mono_info, \
303 + .get = rpi_cirrus_spdif_status_flag_get, \
305 + (bit) | ((reg) << 8) | ((invert) << 16) \
308 +static int rpi_cirrus_spdif_status_flag_get(struct snd_kcontrol *kcontrol,
309 + struct snd_ctl_elem_value *ucontrol)
311 + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
312 + struct snd_soc_codec *wm8804_codec = get_wm8804_runtime(card)->codec;
314 + unsigned int bit = kcontrol->private_value & 0xff;
315 + unsigned int reg = (kcontrol->private_value >> 8) & 0xff;
316 + unsigned int invert = (kcontrol->private_value >> 16) & 0xff;
318 + bool flag = snd_soc_read(wm8804_codec, reg) & (1 << bit);
320 + ucontrol->value.integer.value[0] = invert ? !flag : flag;
325 +static const char * const recovered_frequency_texts[] = {
332 +#define NUM_RECOVERED_FREQUENCIES \
333 + ARRAY_SIZE(recovered_frequency_texts)
335 +static int rpi_cirrus_recovered_frequency_info(struct snd_kcontrol *kcontrol,
336 + struct snd_ctl_elem_info *uinfo)
338 + uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
340 + uinfo->value.enumerated.items = NUM_RECOVERED_FREQUENCIES;
341 + if (uinfo->value.enumerated.item >= NUM_RECOVERED_FREQUENCIES)
342 + uinfo->value.enumerated.item = NUM_RECOVERED_FREQUENCIES - 1;
343 + strcpy(uinfo->value.enumerated.name,
344 + recovered_frequency_texts[uinfo->value.enumerated.item]);
348 +static int rpi_cirrus_recovered_frequency_get(struct snd_kcontrol *kcontrol,
349 + struct snd_ctl_elem_value *ucontrol)
351 + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
352 + struct snd_soc_codec *wm8804_codec = get_wm8804_runtime(card)->codec;
354 + ucontrol->value.enumerated.item[0] =
355 + (snd_soc_read(wm8804_codec, WM8804_SPDSTAT) >> 4) & 0x03;
359 +static const struct snd_kcontrol_new rpi_cirrus_controls[] = {
361 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
362 + .name = "Min Sample Rate",
363 + .info = rpi_cirrus_min_rate_info,
364 + .get = rpi_cirrus_min_rate_get,
365 + .put = rpi_cirrus_min_rate_put,
368 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
369 + .name = "Max Sample Rate",
370 + .info = rpi_cirrus_max_rate_info,
371 + .get = rpi_cirrus_max_rate_get,
372 + .put = rpi_cirrus_max_rate_put,
375 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
376 + .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
377 + .info = rpi_cirrus_spdif_info,
378 + .get = rpi_cirrus_spdif_playback_get,
379 + .put = rpi_cirrus_spdif_playback_put,
382 + .access = SNDRV_CTL_ELEM_ACCESS_READ
383 + | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
384 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
385 + .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
386 + .info = rpi_cirrus_spdif_info,
387 + .get = rpi_cirrus_spdif_capture_get,
390 + .access = SNDRV_CTL_ELEM_ACCESS_READ,
391 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
392 + .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
393 + .info = rpi_cirrus_spdif_info,
394 + .get = rpi_cirrus_spdif_mask_get,
397 + .access = SNDRV_CTL_ELEM_ACCESS_READ
398 + | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
399 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
400 + .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, NONE)
401 + "Recovered Frequency",
402 + .info = rpi_cirrus_recovered_frequency_info,
403 + .get = rpi_cirrus_recovered_frequency_get,
405 + SPDIF_FLAG_CTRL("Audio", WM8804_SPDSTAT, 0, 1),
406 + SPDIF_FLAG_CTRL("Non-PCM", WM8804_SPDSTAT, 1, 0),
407 + SPDIF_FLAG_CTRL("Copyright", WM8804_SPDSTAT, 2, 1),
408 + SPDIF_FLAG_CTRL("De-Emphasis", WM8804_SPDSTAT, 3, 0),
409 + SPDIF_FLAG_CTRL("Lock", WM8804_SPDSTAT, 6, 1),
410 + SPDIF_FLAG_CTRL("Invalid", WM8804_INTSTAT, 1, 0),
411 + SPDIF_FLAG_CTRL("TransErr", WM8804_INTSTAT, 3, 0),
414 +static const char * const linein_micbias_texts[] = {
418 +static SOC_ENUM_SINGLE_VIRT_DECL(linein_micbias_enum,
419 + linein_micbias_texts);
421 +static const struct snd_kcontrol_new linein_micbias_mux =
422 + SOC_DAPM_ENUM("Route", linein_micbias_enum);
424 +static int rpi_cirrus_spdif_rx_enable_event(struct snd_soc_dapm_widget *w,
425 + struct snd_kcontrol *kcontrol, int event);
427 +const struct snd_soc_dapm_widget rpi_cirrus_dapm_widgets[] = {
428 + SND_SOC_DAPM_MIC("DMIC", NULL),
429 + SND_SOC_DAPM_MIC("Headset Mic", NULL),
430 + SND_SOC_DAPM_INPUT("Line Input"),
431 + SND_SOC_DAPM_MIC("Line Input with Micbias", NULL),
432 + SND_SOC_DAPM_MUX("Line Input Micbias", SND_SOC_NOPM, 0, 0,
433 + &linein_micbias_mux),
434 + SND_SOC_DAPM_INPUT("dummy SPDIF in"),
435 + SND_SOC_DAPM_PGA_E("dummy SPDIFRX", SND_SOC_NOPM, 0, 0, NULL, 0,
436 + rpi_cirrus_spdif_rx_enable_event,
437 + SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
438 + SND_SOC_DAPM_INPUT("Dummy Input"),
439 + SND_SOC_DAPM_OUTPUT("Dummy Output"),
442 +const struct snd_soc_dapm_route rpi_cirrus_dapm_routes[] = {
443 + { "IN1L", NULL, "Headset Mic" },
444 + { "IN1R", NULL, "Headset Mic" },
445 + { "Headset Mic", NULL, "MICBIAS1" },
447 + { "IN2L", NULL, "DMIC" },
448 + { "IN2R", NULL, "DMIC" },
449 + { "DMIC", NULL, "MICBIAS2" },
451 + { "IN3L", NULL, "Line Input Micbias" },
452 + { "IN3R", NULL, "Line Input Micbias" },
454 + { "Line Input Micbias", "off", "Line Input" },
455 + { "Line Input Micbias", "on", "Line Input with Micbias" },
457 + /* Make sure MICVDD is enabled, otherwise we get noise */
458 + { "Line Input", NULL, "MICVDD" },
459 + { "Line Input with Micbias", NULL, "MICBIAS3" },
461 + /* Dummy routes to check whether SPDIF RX is enabled or not */
462 + {"dummy SPDIFRX", NULL, "dummy SPDIF in"},
463 + {"AIFTX", NULL, "dummy SPDIFRX"},
466 + * Dummy routes to keep wm5102 from staying off on
467 + * playback/capture if all mixers are off.
469 + { "Dummy Output", NULL, "AIF1RX1" },
470 + { "Dummy Output", NULL, "AIF1RX2" },
471 + { "AIF1TX1", NULL, "Dummy Input" },
472 + { "AIF1TX2", NULL, "Dummy Input" },
475 +static int rpi_cirrus_clear_flls(struct snd_soc_card *card,
476 + struct snd_soc_codec *wm5102_codec) {
480 + ret1 = snd_soc_codec_set_pll(wm5102_codec,
481 + WM5102_FLL1, ARIZONA_FLL_SRC_NONE, 0, 0);
482 + ret2 = snd_soc_codec_set_pll(wm5102_codec,
483 + WM5102_FLL1_REFCLK, ARIZONA_FLL_SRC_NONE, 0, 0);
486 + dev_warn(card->dev,
487 + "setting FLL1 to zero failed: %d\n", ret1);
491 + dev_warn(card->dev,
492 + "setting FLL1_REFCLK to zero failed: %d\n", ret2);
498 +static int rpi_cirrus_set_fll(struct snd_soc_card *card,
499 + struct snd_soc_codec *wm5102_codec, unsigned int clk_freq)
501 + int ret = snd_soc_codec_set_pll(wm5102_codec,
503 + ARIZONA_CLK_SRC_MCLK1,
507 + dev_err(card->dev, "Failed to set FLL1 to %d: %d\n",
510 + usleep_range(1000, 2000);
514 +static int rpi_cirrus_set_fll_refclk(struct snd_soc_card *card,
515 + struct snd_soc_codec *wm5102_codec,
516 + unsigned int clk_freq, unsigned int aif2_freq)
518 + int ret = snd_soc_codec_set_pll(wm5102_codec,
519 + WM5102_FLL1_REFCLK,
520 + ARIZONA_CLK_SRC_MCLK1,
525 + "Failed to set FLL1_REFCLK to %d: %d\n",
530 + ret = snd_soc_codec_set_pll(wm5102_codec,
532 + ARIZONA_CLK_SRC_AIF2BCLK,
533 + aif2_freq, clk_freq);
536 + "Failed to set FLL1 with Sync Clock %d to %d: %d\n",
537 + aif2_freq, clk_freq, ret);
539 + usleep_range(1000, 2000);
543 +static int rpi_cirrus_spdif_rx_enable_event(struct snd_soc_dapm_widget *w,
544 + struct snd_kcontrol *kcontrol, int event)
546 + struct snd_soc_card *card = w->dapm->card;
547 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
548 + struct snd_soc_codec *wm5102_codec = get_wm5102_runtime(card)->codec;
550 + unsigned int clk_freq, aif2_freq;
554 + case SND_SOC_DAPM_POST_PMU:
555 + mutex_lock(&priv->lock);
557 + /* Enable sync path in case of SPDIF capture use case */
559 + clk_freq = calc_sysclk(priv->card_rate);
560 + aif2_freq = 64 * priv->card_rate;
563 + "spdif_rx: changing FLL1 to use Ref Clock clk: %d spdif: %d\n",
564 + clk_freq, aif2_freq);
566 + ret = rpi_cirrus_clear_flls(card, wm5102_codec);
568 + dev_err(card->dev, "spdif_rx: failed to clear FLLs\n");
572 + ret = rpi_cirrus_set_fll_refclk(card, wm5102_codec,
573 + clk_freq, aif2_freq);
576 + dev_err(card->dev, "spdif_rx: failed to set FLLs\n");
580 + /* set to negative to indicate we're doing spdif rx */
581 + priv->fll1_freq = -clk_freq;
582 + priv->sync_path_enable = 1;
585 + case SND_SOC_DAPM_POST_PMD:
586 + mutex_lock(&priv->lock);
587 + priv->sync_path_enable = 0;
595 + mutex_unlock(&priv->lock);
599 +static int rpi_cirrus_set_bias_level(struct snd_soc_card *card,
600 + struct snd_soc_dapm_context *dapm,
601 + enum snd_soc_bias_level level)
603 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
604 + struct snd_soc_pcm_runtime *wm5102_runtime = get_wm5102_runtime(card);
605 + struct snd_soc_codec *wm5102_codec = wm5102_runtime->codec;
608 + unsigned int clk_freq;
610 + if (dapm->dev != wm5102_runtime->codec_dai->dev)
614 + case SND_SOC_BIAS_PREPARE:
615 + if (dapm->bias_level == SND_SOC_BIAS_ON)
618 + mutex_lock(&priv->lock);
620 + if (!priv->sync_path_enable) {
621 + clk_freq = calc_sysclk(priv->card_rate);
624 + "set_bias: changing FLL1 from %d to %d\n",
625 + priv->fll1_freq, clk_freq);
627 + ret = rpi_cirrus_set_fll(card, wm5102_codec, clk_freq);
630 + "set_bias: Failed to set FLL1\n");
632 + priv->fll1_freq = clk_freq;
634 + mutex_unlock(&priv->lock);
643 +static int rpi_cirrus_set_bias_level_post(struct snd_soc_card *card,
644 + struct snd_soc_dapm_context *dapm,
645 + enum snd_soc_bias_level level)
647 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
648 + struct snd_soc_pcm_runtime *wm5102_runtime = get_wm5102_runtime(card);
649 + struct snd_soc_codec *wm5102_codec = wm5102_runtime->codec;
651 + if (dapm->dev != wm5102_runtime->codec_dai->dev)
655 + case SND_SOC_BIAS_STANDBY:
656 + mutex_lock(&priv->lock);
659 + "set_bias_post: changing FLL1 from %d to off\n",
662 + if (rpi_cirrus_clear_flls(card, wm5102_codec))
664 + "set_bias_post: failed to clear FLLs\n");
666 + priv->fll1_freq = 0;
668 + mutex_unlock(&priv->lock);
678 +static int rpi_cirrus_set_wm8804_pll(struct snd_soc_card *card,
679 + struct snd_soc_dai *wm8804_dai, unsigned int rate)
684 + unsigned int clk_freq = rate * 256;
686 + ret = snd_soc_dai_set_pll(wm8804_dai, 0, 0,
687 + WM8804_CLKOUT_HZ, clk_freq);
690 + "Failed to set WM8804 PLL to %d: %d\n", clk_freq, ret);
694 + /* Set MCLK as PLL Output */
695 + ret = snd_soc_dai_set_sysclk(wm8804_dai,
696 + WM8804_TX_CLKSRC_PLL, clk_freq, 0);
699 + "Failed to set MCLK as PLL Output: %d\n", ret);
706 +static int rpi_cirrus_startup(struct snd_pcm_substream *substream)
708 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
709 + struct snd_soc_card *card = rtd->card;
710 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
711 + unsigned int min_rate = min_rates[priv->min_rate_idx].value;
712 + unsigned int max_rate = max_rates[priv->max_rate_idx].value;
714 + if (min_rate || max_rate) {
716 + max_rate = UINT_MAX;
719 + "startup: limiting rate to %u-%u\n",
720 + min_rate, max_rate);
722 + snd_pcm_hw_constraint_minmax(substream->runtime,
723 + SNDRV_PCM_HW_PARAM_RATE, min_rate, max_rate);
729 +static struct snd_soc_pcm_stream rpi_cirrus_dai_link2_params = {
730 + .formats = SNDRV_PCM_FMTBIT_S24_LE,
733 + .rate_min = RPI_CIRRUS_DEFAULT_RATE,
734 + .rate_max = RPI_CIRRUS_DEFAULT_RATE,
737 +static int rpi_cirrus_hw_params(struct snd_pcm_substream *substream,
738 + struct snd_pcm_hw_params *params)
740 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
741 + struct snd_soc_card *card = rtd->card;
742 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
743 + struct snd_soc_dai *bcm_i2s_dai = rtd->cpu_dai;
744 + struct snd_soc_codec *wm5102_codec = rtd->codec;
745 + struct snd_soc_dai *wm8804_dai = get_wm8804_runtime(card)->codec_dai;
749 + unsigned int width = snd_pcm_format_physical_width(
750 + params_format(params));
751 + unsigned int rate = params_rate(params);
752 + unsigned int clk_freq = calc_sysclk(rate);
754 + mutex_lock(&priv->lock);
756 + dev_dbg(card->dev, "hw_params: setting rate to %d\n", rate);
758 + ret = snd_soc_dai_set_bclk_ratio(bcm_i2s_dai, 2 * width);
760 + dev_err(card->dev, "set_bclk_ratio failed: %d\n", ret);
764 + ret = snd_soc_dai_set_tdm_slot(rtd->codec_dai, 0x03, 0x03, 2, width);
766 + dev_err(card->dev, "set_tdm_slot failed: %d\n", ret);
770 + /* WM8804 supports sample rates from 32k only */
771 + if (rate >= 32000) {
772 + ret = rpi_cirrus_set_wm8804_pll(card, wm8804_dai, rate);
777 + ret = snd_soc_codec_set_sysclk(wm5102_codec,
778 + ARIZONA_CLK_SYSCLK,
779 + ARIZONA_CLK_SRC_FLL1,
783 + dev_err(card->dev, "Failed to set SYSCLK: %d\n", ret);
787 + if ((priv->fll1_freq > 0) && (priv->fll1_freq != clk_freq)) {
789 + "hw_params: changing FLL1 from %d to %d\n",
790 + priv->fll1_freq, clk_freq);
792 + if (rpi_cirrus_clear_flls(card, wm5102_codec)) {
793 + dev_err(card->dev, "hw_params: failed to clear FLLs\n");
797 + if (rpi_cirrus_set_fll(card, wm5102_codec, clk_freq)) {
798 + dev_err(card->dev, "hw_params: failed to set FLL\n");
802 + priv->fll1_freq = clk_freq;
805 + priv->card_rate = rate;
806 + rpi_cirrus_dai_link2_params.rate_min = rate;
807 + rpi_cirrus_dai_link2_params.rate_max = rate;
809 + priv->params_set |= 1 << substream->stream;
812 + mutex_unlock(&priv->lock);
817 +static int rpi_cirrus_hw_free(struct snd_pcm_substream *substream)
819 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
820 + struct snd_soc_card *card = rtd->card;
821 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
822 + struct snd_soc_codec *wm5102_codec = rtd->codec;
824 + unsigned int old_params_set = priv->params_set;
826 + priv->params_set &= ~(1 << substream->stream);
828 + /* disable sysclk if this was the last open stream */
829 + if (priv->params_set == 0 && old_params_set) {
831 + "hw_free: Setting SYSCLK to Zero\n");
833 + ret = snd_soc_codec_set_sysclk(wm5102_codec,
834 + ARIZONA_CLK_SYSCLK,
835 + ARIZONA_CLK_SRC_FLL1,
840 + "hw_free: Failed to set SYSCLK to Zero: %d\n",
846 +static int rpi_cirrus_init_wm5102(struct snd_soc_pcm_runtime *rtd)
848 + struct snd_soc_codec *codec = rtd->codec;
851 + /* no 32kHz input, derive it from sysclk if needed */
852 + snd_soc_update_bits(codec,
853 + ARIZONA_CLOCK_32K_1, ARIZONA_CLK_32K_SRC_MASK, 2);
855 + if (rpi_cirrus_clear_flls(rtd->card, codec))
856 + dev_warn(rtd->card->dev,
857 + "init_wm5102: failed to clear FLLs\n");
859 + ret = snd_soc_codec_set_sysclk(codec,
860 + ARIZONA_CLK_SYSCLK, ARIZONA_CLK_SRC_FLL1,
861 + 0, SND_SOC_CLOCK_IN);
863 + dev_err(rtd->card->dev,
864 + "Failed to set SYSCLK to Zero: %d\n", ret);
871 +static int rpi_cirrus_init_wm8804(struct snd_soc_pcm_runtime *rtd)
873 + struct snd_soc_codec *codec = rtd->codec;
874 + struct snd_soc_dai *codec_dai = rtd->codec_dai;
875 + struct snd_soc_card *card = rtd->card;
876 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
880 + for (i = 0; i < 4; i++) {
881 + mask = (i == 3) ? 0x3f : 0xff;
882 + priv->iec958_status[i] =
883 + snd_soc_read(codec, WM8804_SPDTX1 + i) & mask;
886 + /* Setup for 256fs */
887 + ret = snd_soc_dai_set_clkdiv(codec_dai,
888 + WM8804_MCLK_DIV, WM8804_MCLKDIV_256FS);
891 + "init_wm8804: Failed to set MCLK_DIV to 256fs: %d\n",
896 + /* Output OSC on CLKOUT */
897 + ret = snd_soc_dai_set_sysclk(codec_dai,
898 + WM8804_CLKOUT_SRC_OSCCLK, WM8804_CLKOUT_HZ, 0);
901 + "init_wm8804: Failed to set CLKOUT as OSC Frequency: %d\n",
904 + /* Init PLL with default samplerate */
905 + ret = rpi_cirrus_set_wm8804_pll(card, codec_dai,
906 + RPI_CIRRUS_DEFAULT_RATE);
909 + "init_wm8804: Failed to setup PLL for %dHz: %d\n",
910 + RPI_CIRRUS_DEFAULT_RATE, ret);
915 +static struct snd_soc_ops rpi_cirrus_ops = {
916 + .startup = rpi_cirrus_startup,
917 + .hw_params = rpi_cirrus_hw_params,
918 + .hw_free = rpi_cirrus_hw_free,
921 +static struct snd_soc_dai_link rpi_cirrus_dai[] = {
924 + .stream_name = "WM5102 AiFi",
925 + .codec_dai_name = "wm5102-aif1",
926 + .codec_name = "wm5102-codec",
927 + .dai_fmt = SND_SOC_DAIFMT_I2S
928 + | SND_SOC_DAIFMT_NB_NF
929 + | SND_SOC_DAIFMT_CBM_CFM,
930 + .ops = &rpi_cirrus_ops,
931 + .init = rpi_cirrus_init_wm5102,
934 + .name = "WM5102 SPDIF",
935 + .stream_name = "SPDIF Tx/Rx",
936 + .cpu_dai_name = "wm5102-aif2",
937 + .codec_dai_name = "wm8804-spdif",
938 + .codec_name = "wm8804.1-003b",
939 + .dai_fmt = SND_SOC_DAIFMT_I2S
940 + | SND_SOC_DAIFMT_NB_NF
941 + | SND_SOC_DAIFMT_CBM_CFM,
942 + .ignore_suspend = 1,
943 + .params = &rpi_cirrus_dai_link2_params,
944 + .init = rpi_cirrus_init_wm8804,
949 +static int rpi_cirrus_late_probe(struct snd_soc_card *card)
951 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
952 + struct snd_soc_pcm_runtime *wm5102_runtime = get_wm5102_runtime(card);
953 + struct snd_soc_pcm_runtime *wm8804_runtime = get_wm8804_runtime(card);
956 + dev_dbg(card->dev, "iec958_bits: %02x %02x %02x %02x\n",
957 + priv->iec958_status[0],
958 + priv->iec958_status[1],
959 + priv->iec958_status[2],
960 + priv->iec958_status[3]);
962 + ret = snd_soc_dai_set_sysclk(
963 + wm5102_runtime->codec_dai, ARIZONA_CLK_SYSCLK, 0, 0);
966 + "Failed to set WM5102 codec dai clk domain: %d\n", ret);
970 + ret = snd_soc_dai_set_sysclk(
971 + wm8804_runtime->cpu_dai, ARIZONA_CLK_SYSCLK, 0, 0);
974 + "Failed to set WM8804 codec dai clk domain: %d\n", ret);
979 +/* audio machine driver */
980 +static struct snd_soc_card rpi_cirrus_card = {
981 + .name = "RPi-Cirrus",
982 + .driver_name = "RPiCirrus",
983 + .owner = THIS_MODULE,
984 + .dai_link = rpi_cirrus_dai,
985 + .num_links = ARRAY_SIZE(rpi_cirrus_dai),
986 + .late_probe = rpi_cirrus_late_probe,
987 + .controls = rpi_cirrus_controls,
988 + .num_controls = ARRAY_SIZE(rpi_cirrus_controls),
989 + .dapm_widgets = rpi_cirrus_dapm_widgets,
990 + .num_dapm_widgets = ARRAY_SIZE(rpi_cirrus_dapm_widgets),
991 + .dapm_routes = rpi_cirrus_dapm_routes,
992 + .num_dapm_routes = ARRAY_SIZE(rpi_cirrus_dapm_routes),
993 + .set_bias_level = rpi_cirrus_set_bias_level,
994 + .set_bias_level_post = rpi_cirrus_set_bias_level_post,
997 +static int rpi_cirrus_probe(struct platform_device *pdev)
1000 + struct rpi_cirrus_priv *priv;
1001 + struct device_node *i2s_node;
1003 + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1007 + priv->min_rate_idx = 1; /* min samplerate 32kHz */
1008 + priv->card_rate = RPI_CIRRUS_DEFAULT_RATE;
1010 + mutex_init(&priv->lock);
1012 + snd_soc_card_set_drvdata(&rpi_cirrus_card, priv);
1014 + if (!pdev->dev.of_node)
1017 + i2s_node = of_parse_phandle(
1018 + pdev->dev.of_node, "i2s-controller", 0);
1020 + dev_err(&pdev->dev, "i2s-controller missing in DT\n");
1024 + rpi_cirrus_dai[DAI_WM5102].cpu_of_node = i2s_node;
1025 + rpi_cirrus_dai[DAI_WM5102].platform_of_node = i2s_node;
1027 + rpi_cirrus_card.dev = &pdev->dev;
1029 + ret = devm_snd_soc_register_card(&pdev->dev, &rpi_cirrus_card);
1031 + if (ret == -EPROBE_DEFER)
1032 + dev_dbg(&pdev->dev,
1033 + "register card requested probe deferral\n");
1035 + dev_err(&pdev->dev,
1036 + "Failed to register card: %d\n", ret);
1042 +static const struct of_device_id rpi_cirrus_of_match[] = {
1043 + { .compatible = "wlf,rpi-cirrus", },
1046 +MODULE_DEVICE_TABLE(of, rpi_cirrus_of_match);
1048 +static struct platform_driver rpi_cirrus_driver = {
1050 + .name = "snd-rpi-cirrus",
1051 + .of_match_table = of_match_ptr(rpi_cirrus_of_match),
1053 + .probe = rpi_cirrus_probe,
1056 +module_platform_driver(rpi_cirrus_driver);
1058 +MODULE_AUTHOR("Matthias Reichl <hias@horus.com>");
1059 +MODULE_DESCRIPTION("ASoC driver for Cirrus Logic Audio Card");
1060 +MODULE_LICENSE("GPL");