1 From 759d6a02f75342beff69ec1c07389a151a1cacd2 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] 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 ASoC: rpi-cirrus: use modern dai_link style
15 Signed-off-by: Matthias Reichl <hias@horus.com>
17 sound/soc/bcm/rpi-cirrus.c | 1035 ++++++++++++++++++++++++++++++++++++
18 1 file changed, 1035 insertions(+)
19 create mode 100644 sound/soc/bcm/rpi-cirrus.c
22 +++ b/sound/soc/bcm/rpi-cirrus.c
25 + * ASoC machine driver for Cirrus Logic Audio Card
26 + * (with WM5102 and WM8804 codecs)
28 + * Copyright 2015-2017 Matthias Reichl <hias@horus.com>
30 + * Based on rpi-cirrus-sound-pi driver (c) Wolfson / Cirrus Logic Inc.
32 + * This program is free software; you can redistribute it and/or modify
33 + * it under the terms of the GNU General Public License version 2 as
34 + * published by the Free Software Foundation.
37 +#include <linux/module.h>
38 +#include <linux/mutex.h>
39 +#include <linux/slab.h>
40 +#include <linux/list.h>
41 +#include <linux/delay.h>
42 +#include <sound/pcm_params.h>
44 +#include <linux/mfd/arizona/registers.h>
46 +#include "../codecs/wm5102.h"
47 +#include "../codecs/wm8804.h"
49 +#define WM8804_CLKOUT_HZ 12000000
51 +#define RPI_CIRRUS_DEFAULT_RATE 44100
52 +#define WM5102_MAX_SYSCLK_1 49152000 /* max sysclk for 4K family */
53 +#define WM5102_MAX_SYSCLK_2 45158400 /* max sysclk for 11.025K family */
55 +static inline unsigned int calc_sysclk(unsigned int rate)
57 + return (rate % 4000) ? WM5102_MAX_SYSCLK_2 : WM5102_MAX_SYSCLK_1;
65 +struct rpi_cirrus_priv {
66 + /* mutex for synchronzing FLL1 access with DAPM */
68 + unsigned int card_rate;
69 + int sync_path_enable;
70 + int fll1_freq; /* negative means RefClock in spdif rx case */
72 + /* track hw params/free for substreams */
73 + unsigned int params_set;
74 + unsigned int min_rate_idx, max_rate_idx;
75 + unsigned char iec958_status[4];
78 +/* helper functions */
79 +static inline struct snd_soc_pcm_runtime *get_wm5102_runtime(
80 + struct snd_soc_card *card) {
81 + return snd_soc_get_pcm_runtime(card, card->dai_link[DAI_WM5102].name);
84 +static inline struct snd_soc_pcm_runtime *get_wm8804_runtime(
85 + struct snd_soc_card *card) {
86 + return snd_soc_get_pcm_runtime(card, card->dai_link[DAI_WM8804].name);
95 +static struct rate_info min_rates[] = {
101 +#define NUM_MIN_RATES ARRAY_SIZE(min_rates)
103 +static int rpi_cirrus_min_rate_info(struct snd_kcontrol *kcontrol,
104 + struct snd_ctl_elem_info *uinfo)
106 + uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
108 + uinfo->value.enumerated.items = NUM_MIN_RATES;
110 + if (uinfo->value.enumerated.item >= NUM_MIN_RATES)
111 + uinfo->value.enumerated.item = NUM_MIN_RATES - 1;
112 + strcpy(uinfo->value.enumerated.name,
113 + min_rates[uinfo->value.enumerated.item].text);
117 +static int rpi_cirrus_min_rate_get(struct snd_kcontrol *kcontrol,
118 + struct snd_ctl_elem_value *ucontrol)
120 + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
121 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
123 + ucontrol->value.enumerated.item[0] = priv->min_rate_idx;
127 +static int rpi_cirrus_min_rate_put(struct snd_kcontrol *kcontrol,
128 + struct snd_ctl_elem_value *ucontrol)
130 + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
131 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
134 + if (priv->min_rate_idx != ucontrol->value.enumerated.item[0]) {
136 + priv->min_rate_idx = ucontrol->value.enumerated.item[0];
142 +static struct rate_info max_rates[] = {
148 +#define NUM_MAX_RATES ARRAY_SIZE(max_rates)
150 +static int rpi_cirrus_max_rate_info(struct snd_kcontrol *kcontrol,
151 + struct snd_ctl_elem_info *uinfo)
153 + uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
155 + uinfo->value.enumerated.items = NUM_MAX_RATES;
156 + if (uinfo->value.enumerated.item >= NUM_MAX_RATES)
157 + uinfo->value.enumerated.item = NUM_MAX_RATES - 1;
158 + strcpy(uinfo->value.enumerated.name,
159 + max_rates[uinfo->value.enumerated.item].text);
163 +static int rpi_cirrus_max_rate_get(struct snd_kcontrol *kcontrol,
164 + struct snd_ctl_elem_value *ucontrol)
166 + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
167 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
169 + ucontrol->value.enumerated.item[0] = priv->max_rate_idx;
173 +static int rpi_cirrus_max_rate_put(struct snd_kcontrol *kcontrol,
174 + struct snd_ctl_elem_value *ucontrol)
176 + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
177 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
180 + if (priv->max_rate_idx != ucontrol->value.enumerated.item[0]) {
182 + priv->max_rate_idx = ucontrol->value.enumerated.item[0];
188 +static int rpi_cirrus_spdif_info(struct snd_kcontrol *kcontrol,
189 + struct snd_ctl_elem_info *uinfo)
191 + uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
196 +static int rpi_cirrus_spdif_playback_get(struct snd_kcontrol *kcontrol,
197 + struct snd_ctl_elem_value *ucontrol)
199 + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
200 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
203 + for (i = 0; i < 4; i++)
204 + ucontrol->value.iec958.status[i] = priv->iec958_status[i];
209 +static int rpi_cirrus_spdif_playback_put(struct snd_kcontrol *kcontrol,
210 + struct snd_ctl_elem_value *ucontrol)
212 + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
213 + struct snd_soc_component *wm8804_component =
214 + get_wm8804_runtime(card)->codec_dai->component;
215 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
216 + unsigned char *stat = priv->iec958_status;
217 + unsigned char *ctrl_stat = ucontrol->value.iec958.status;
219 + int i, changed = 0;
221 + for (i = 0; i < 4; i++) {
222 + mask = (i == 3) ? 0x3f : 0xff;
223 + if ((ctrl_stat[i] & mask) != (stat[i] & mask)) {
225 + stat[i] = ctrl_stat[i] & mask;
226 + snd_soc_component_update_bits(wm8804_component,
227 + WM8804_SPDTX1 + i, mask, stat[i]);
234 +static int rpi_cirrus_spdif_mask_get(struct snd_kcontrol *kcontrol,
235 + struct snd_ctl_elem_value *ucontrol)
237 + ucontrol->value.iec958.status[0] = 0xff;
238 + ucontrol->value.iec958.status[1] = 0xff;
239 + ucontrol->value.iec958.status[2] = 0xff;
240 + ucontrol->value.iec958.status[3] = 0x3f;
245 +static int rpi_cirrus_spdif_capture_get(struct snd_kcontrol *kcontrol,
246 + struct snd_ctl_elem_value *ucontrol)
248 + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
249 + struct snd_soc_component *wm8804_component =
250 + get_wm8804_runtime(card)->codec_dai->component;
251 + unsigned int val, mask;
254 + for (i = 0; i < 4; i++) {
255 + ret = snd_soc_component_read(wm8804_component,
256 + WM8804_RXCHAN1 + i, &val);
259 + mask = (i == 3) ? 0x3f : 0xff;
260 + ucontrol->value.iec958.status[i] = val & mask;
266 +#define SPDIF_FLAG_CTRL(desc, reg, bit, invert) \
268 + .access = SNDRV_CTL_ELEM_ACCESS_READ \
269 + | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
270 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
271 + .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, NONE) \
273 + .info = snd_ctl_boolean_mono_info, \
274 + .get = rpi_cirrus_spdif_status_flag_get, \
276 + (bit) | ((reg) << 8) | ((invert) << 16) \
279 +static int rpi_cirrus_spdif_status_flag_get(struct snd_kcontrol *kcontrol,
280 + struct snd_ctl_elem_value *ucontrol)
282 + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
283 + struct snd_soc_component *wm8804_component =
284 + get_wm8804_runtime(card)->codec_dai->component;
286 + unsigned int bit = kcontrol->private_value & 0xff;
287 + unsigned int reg = (kcontrol->private_value >> 8) & 0xff;
288 + unsigned int invert = (kcontrol->private_value >> 16) & 0xff;
293 + ret = snd_soc_component_read(wm8804_component, reg, &val);
297 + flag = val & (1 << bit);
299 + ucontrol->value.integer.value[0] = invert ? !flag : flag;
304 +static const char * const recovered_frequency_texts[] = {
311 +#define NUM_RECOVERED_FREQUENCIES \
312 + ARRAY_SIZE(recovered_frequency_texts)
314 +static int rpi_cirrus_recovered_frequency_info(struct snd_kcontrol *kcontrol,
315 + struct snd_ctl_elem_info *uinfo)
317 + uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
319 + uinfo->value.enumerated.items = NUM_RECOVERED_FREQUENCIES;
320 + if (uinfo->value.enumerated.item >= NUM_RECOVERED_FREQUENCIES)
321 + uinfo->value.enumerated.item = NUM_RECOVERED_FREQUENCIES - 1;
322 + strcpy(uinfo->value.enumerated.name,
323 + recovered_frequency_texts[uinfo->value.enumerated.item]);
327 +static int rpi_cirrus_recovered_frequency_get(struct snd_kcontrol *kcontrol,
328 + struct snd_ctl_elem_value *ucontrol)
330 + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
331 + struct snd_soc_component *wm8804_component =
332 + get_wm8804_runtime(card)->codec_dai->component;
336 + ret = snd_soc_component_read(wm8804_component, WM8804_SPDSTAT, &val);
340 + ucontrol->value.enumerated.item[0] = (val >> 4) & 0x03;
344 +static const struct snd_kcontrol_new rpi_cirrus_controls[] = {
346 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
347 + .name = "Min Sample Rate",
348 + .info = rpi_cirrus_min_rate_info,
349 + .get = rpi_cirrus_min_rate_get,
350 + .put = rpi_cirrus_min_rate_put,
353 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
354 + .name = "Max Sample Rate",
355 + .info = rpi_cirrus_max_rate_info,
356 + .get = rpi_cirrus_max_rate_get,
357 + .put = rpi_cirrus_max_rate_put,
360 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
361 + .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
362 + .info = rpi_cirrus_spdif_info,
363 + .get = rpi_cirrus_spdif_playback_get,
364 + .put = rpi_cirrus_spdif_playback_put,
367 + .access = SNDRV_CTL_ELEM_ACCESS_READ
368 + | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
369 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
370 + .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
371 + .info = rpi_cirrus_spdif_info,
372 + .get = rpi_cirrus_spdif_capture_get,
375 + .access = SNDRV_CTL_ELEM_ACCESS_READ,
376 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
377 + .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
378 + .info = rpi_cirrus_spdif_info,
379 + .get = rpi_cirrus_spdif_mask_get,
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, NONE)
386 + "Recovered Frequency",
387 + .info = rpi_cirrus_recovered_frequency_info,
388 + .get = rpi_cirrus_recovered_frequency_get,
390 + SPDIF_FLAG_CTRL("Audio", WM8804_SPDSTAT, 0, 1),
391 + SPDIF_FLAG_CTRL("Non-PCM", WM8804_SPDSTAT, 1, 0),
392 + SPDIF_FLAG_CTRL("Copyright", WM8804_SPDSTAT, 2, 1),
393 + SPDIF_FLAG_CTRL("De-Emphasis", WM8804_SPDSTAT, 3, 0),
394 + SPDIF_FLAG_CTRL("Lock", WM8804_SPDSTAT, 6, 1),
395 + SPDIF_FLAG_CTRL("Invalid", WM8804_INTSTAT, 1, 0),
396 + SPDIF_FLAG_CTRL("TransErr", WM8804_INTSTAT, 3, 0),
399 +static const char * const linein_micbias_texts[] = {
403 +static SOC_ENUM_SINGLE_VIRT_DECL(linein_micbias_enum,
404 + linein_micbias_texts);
406 +static const struct snd_kcontrol_new linein_micbias_mux =
407 + SOC_DAPM_ENUM("Route", linein_micbias_enum);
409 +static int rpi_cirrus_spdif_rx_enable_event(struct snd_soc_dapm_widget *w,
410 + struct snd_kcontrol *kcontrol, int event);
412 +const struct snd_soc_dapm_widget rpi_cirrus_dapm_widgets[] = {
413 + SND_SOC_DAPM_MIC("DMIC", NULL),
414 + SND_SOC_DAPM_MIC("Headset Mic", NULL),
415 + SND_SOC_DAPM_INPUT("Line Input"),
416 + SND_SOC_DAPM_MIC("Line Input with Micbias", NULL),
417 + SND_SOC_DAPM_MUX("Line Input Micbias", SND_SOC_NOPM, 0, 0,
418 + &linein_micbias_mux),
419 + SND_SOC_DAPM_INPUT("dummy SPDIF in"),
420 + SND_SOC_DAPM_PGA_E("dummy SPDIFRX", SND_SOC_NOPM, 0, 0, NULL, 0,
421 + rpi_cirrus_spdif_rx_enable_event,
422 + SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
423 + SND_SOC_DAPM_INPUT("Dummy Input"),
424 + SND_SOC_DAPM_OUTPUT("Dummy Output"),
427 +const struct snd_soc_dapm_route rpi_cirrus_dapm_routes[] = {
428 + { "IN1L", NULL, "Headset Mic" },
429 + { "IN1R", NULL, "Headset Mic" },
430 + { "Headset Mic", NULL, "MICBIAS1" },
432 + { "IN2L", NULL, "DMIC" },
433 + { "IN2R", NULL, "DMIC" },
434 + { "DMIC", NULL, "MICBIAS2" },
436 + { "IN3L", NULL, "Line Input Micbias" },
437 + { "IN3R", NULL, "Line Input Micbias" },
439 + { "Line Input Micbias", "off", "Line Input" },
440 + { "Line Input Micbias", "on", "Line Input with Micbias" },
442 + /* Make sure MICVDD is enabled, otherwise we get noise */
443 + { "Line Input", NULL, "MICVDD" },
444 + { "Line Input with Micbias", NULL, "MICBIAS3" },
446 + /* Dummy routes to check whether SPDIF RX is enabled or not */
447 + {"dummy SPDIFRX", NULL, "dummy SPDIF in"},
448 + {"AIFTX", NULL, "dummy SPDIFRX"},
451 + * Dummy routes to keep wm5102 from staying off on
452 + * playback/capture if all mixers are off.
454 + { "Dummy Output", NULL, "AIF1RX1" },
455 + { "Dummy Output", NULL, "AIF1RX2" },
456 + { "AIF1TX1", NULL, "Dummy Input" },
457 + { "AIF1TX2", NULL, "Dummy Input" },
460 +static int rpi_cirrus_clear_flls(struct snd_soc_card *card,
461 + struct snd_soc_component *wm5102_component) {
465 + ret1 = snd_soc_component_set_pll(wm5102_component,
466 + WM5102_FLL1, ARIZONA_FLL_SRC_NONE, 0, 0);
467 + ret2 = snd_soc_component_set_pll(wm5102_component,
468 + WM5102_FLL1_REFCLK, ARIZONA_FLL_SRC_NONE, 0, 0);
471 + dev_warn(card->dev,
472 + "setting FLL1 to zero failed: %d\n", ret1);
476 + dev_warn(card->dev,
477 + "setting FLL1_REFCLK to zero failed: %d\n", ret2);
483 +static int rpi_cirrus_set_fll(struct snd_soc_card *card,
484 + struct snd_soc_component *wm5102_component, unsigned int clk_freq)
486 + int ret = snd_soc_component_set_pll(wm5102_component,
488 + ARIZONA_CLK_SRC_MCLK1,
492 + dev_err(card->dev, "Failed to set FLL1 to %d: %d\n",
495 + usleep_range(1000, 2000);
499 +static int rpi_cirrus_set_fll_refclk(struct snd_soc_card *card,
500 + struct snd_soc_component *wm5102_component,
501 + unsigned int clk_freq, unsigned int aif2_freq)
503 + int ret = snd_soc_component_set_pll(wm5102_component,
504 + WM5102_FLL1_REFCLK,
505 + ARIZONA_CLK_SRC_MCLK1,
510 + "Failed to set FLL1_REFCLK to %d: %d\n",
515 + ret = snd_soc_component_set_pll(wm5102_component,
517 + ARIZONA_CLK_SRC_AIF2BCLK,
518 + aif2_freq, clk_freq);
521 + "Failed to set FLL1 with Sync Clock %d to %d: %d\n",
522 + aif2_freq, clk_freq, ret);
524 + usleep_range(1000, 2000);
528 +static int rpi_cirrus_spdif_rx_enable_event(struct snd_soc_dapm_widget *w,
529 + struct snd_kcontrol *kcontrol, int event)
531 + struct snd_soc_card *card = w->dapm->card;
532 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
533 + struct snd_soc_component *wm5102_component =
534 + get_wm5102_runtime(card)->codec_dai->component;
536 + unsigned int clk_freq, aif2_freq;
540 + case SND_SOC_DAPM_POST_PMU:
541 + mutex_lock(&priv->lock);
543 + /* Enable sync path in case of SPDIF capture use case */
545 + clk_freq = calc_sysclk(priv->card_rate);
546 + aif2_freq = 64 * priv->card_rate;
549 + "spdif_rx: changing FLL1 to use Ref Clock clk: %d spdif: %d\n",
550 + clk_freq, aif2_freq);
552 + ret = rpi_cirrus_clear_flls(card, wm5102_component);
554 + dev_err(card->dev, "spdif_rx: failed to clear FLLs\n");
558 + ret = rpi_cirrus_set_fll_refclk(card, wm5102_component,
559 + clk_freq, aif2_freq);
562 + dev_err(card->dev, "spdif_rx: failed to set FLLs\n");
566 + /* set to negative to indicate we're doing spdif rx */
567 + priv->fll1_freq = -clk_freq;
568 + priv->sync_path_enable = 1;
571 + case SND_SOC_DAPM_POST_PMD:
572 + mutex_lock(&priv->lock);
573 + priv->sync_path_enable = 0;
581 + mutex_unlock(&priv->lock);
585 +static int rpi_cirrus_set_bias_level(struct snd_soc_card *card,
586 + struct snd_soc_dapm_context *dapm,
587 + enum snd_soc_bias_level level)
589 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
590 + struct snd_soc_pcm_runtime *wm5102_runtime = get_wm5102_runtime(card);
591 + struct snd_soc_component *wm5102_component =
592 + wm5102_runtime->codec_dai->component;
595 + unsigned int clk_freq;
597 + if (dapm->dev != wm5102_runtime->codec_dai->dev)
601 + case SND_SOC_BIAS_PREPARE:
602 + if (dapm->bias_level == SND_SOC_BIAS_ON)
605 + mutex_lock(&priv->lock);
607 + if (!priv->sync_path_enable) {
608 + clk_freq = calc_sysclk(priv->card_rate);
611 + "set_bias: changing FLL1 from %d to %d\n",
612 + priv->fll1_freq, clk_freq);
614 + ret = rpi_cirrus_set_fll(card,
615 + wm5102_component, clk_freq);
618 + "set_bias: Failed to set FLL1\n");
620 + priv->fll1_freq = clk_freq;
622 + mutex_unlock(&priv->lock);
631 +static int rpi_cirrus_set_bias_level_post(struct snd_soc_card *card,
632 + struct snd_soc_dapm_context *dapm,
633 + enum snd_soc_bias_level level)
635 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
636 + struct snd_soc_pcm_runtime *wm5102_runtime = get_wm5102_runtime(card);
637 + struct snd_soc_component *wm5102_component =
638 + wm5102_runtime->codec_dai->component;
640 + if (dapm->dev != wm5102_runtime->codec_dai->dev)
644 + case SND_SOC_BIAS_STANDBY:
645 + mutex_lock(&priv->lock);
648 + "set_bias_post: changing FLL1 from %d to off\n",
651 + if (rpi_cirrus_clear_flls(card, wm5102_component))
653 + "set_bias_post: failed to clear FLLs\n");
655 + priv->fll1_freq = 0;
657 + mutex_unlock(&priv->lock);
667 +static int rpi_cirrus_set_wm8804_pll(struct snd_soc_card *card,
668 + struct snd_soc_dai *wm8804_dai, unsigned int rate)
673 + unsigned int clk_freq = rate * 256;
675 + ret = snd_soc_dai_set_pll(wm8804_dai, 0, 0,
676 + WM8804_CLKOUT_HZ, clk_freq);
679 + "Failed to set WM8804 PLL to %d: %d\n", clk_freq, ret);
683 + /* Set MCLK as PLL Output */
684 + ret = snd_soc_dai_set_sysclk(wm8804_dai,
685 + WM8804_TX_CLKSRC_PLL, clk_freq, 0);
688 + "Failed to set MCLK as PLL Output: %d\n", ret);
695 +static int rpi_cirrus_startup(struct snd_pcm_substream *substream)
697 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
698 + struct snd_soc_card *card = rtd->card;
699 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
700 + unsigned int min_rate = min_rates[priv->min_rate_idx].value;
701 + unsigned int max_rate = max_rates[priv->max_rate_idx].value;
703 + if (min_rate || max_rate) {
705 + max_rate = UINT_MAX;
708 + "startup: limiting rate to %u-%u\n",
709 + min_rate, max_rate);
711 + snd_pcm_hw_constraint_minmax(substream->runtime,
712 + SNDRV_PCM_HW_PARAM_RATE, min_rate, max_rate);
718 +static struct snd_soc_pcm_stream rpi_cirrus_dai_link2_params = {
719 + .formats = SNDRV_PCM_FMTBIT_S24_LE,
722 + .rate_min = RPI_CIRRUS_DEFAULT_RATE,
723 + .rate_max = RPI_CIRRUS_DEFAULT_RATE,
726 +static int rpi_cirrus_hw_params(struct snd_pcm_substream *substream,
727 + struct snd_pcm_hw_params *params)
729 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
730 + struct snd_soc_card *card = rtd->card;
731 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
732 + struct snd_soc_dai *bcm_i2s_dai = rtd->cpu_dai;
733 + struct snd_soc_component *wm5102_component = rtd->codec_dai->component;
734 + struct snd_soc_dai *wm8804_dai = get_wm8804_runtime(card)->codec_dai;
738 + unsigned int width = snd_pcm_format_physical_width(
739 + params_format(params));
740 + unsigned int rate = params_rate(params);
741 + unsigned int clk_freq = calc_sysclk(rate);
743 + mutex_lock(&priv->lock);
745 + dev_dbg(card->dev, "hw_params: setting rate to %d\n", rate);
747 + ret = snd_soc_dai_set_bclk_ratio(bcm_i2s_dai, 2 * width);
749 + dev_err(card->dev, "set_bclk_ratio failed: %d\n", ret);
753 + ret = snd_soc_dai_set_tdm_slot(rtd->codec_dai, 0x03, 0x03, 2, width);
755 + dev_err(card->dev, "set_tdm_slot failed: %d\n", ret);
759 + /* WM8804 supports sample rates from 32k only */
760 + if (rate >= 32000) {
761 + ret = rpi_cirrus_set_wm8804_pll(card, wm8804_dai, rate);
766 + ret = snd_soc_component_set_sysclk(wm5102_component,
767 + ARIZONA_CLK_SYSCLK,
768 + ARIZONA_CLK_SRC_FLL1,
772 + dev_err(card->dev, "Failed to set SYSCLK: %d\n", ret);
776 + if ((priv->fll1_freq > 0) && (priv->fll1_freq != clk_freq)) {
778 + "hw_params: changing FLL1 from %d to %d\n",
779 + priv->fll1_freq, clk_freq);
781 + if (rpi_cirrus_clear_flls(card, wm5102_component)) {
782 + dev_err(card->dev, "hw_params: failed to clear FLLs\n");
786 + if (rpi_cirrus_set_fll(card, wm5102_component, clk_freq)) {
787 + dev_err(card->dev, "hw_params: failed to set FLL\n");
791 + priv->fll1_freq = clk_freq;
794 + priv->card_rate = rate;
795 + rpi_cirrus_dai_link2_params.rate_min = rate;
796 + rpi_cirrus_dai_link2_params.rate_max = rate;
798 + priv->params_set |= 1 << substream->stream;
801 + mutex_unlock(&priv->lock);
806 +static int rpi_cirrus_hw_free(struct snd_pcm_substream *substream)
808 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
809 + struct snd_soc_card *card = rtd->card;
810 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
811 + struct snd_soc_component *wm5102_component = rtd->codec_dai->component;
813 + unsigned int old_params_set = priv->params_set;
815 + priv->params_set &= ~(1 << substream->stream);
817 + /* disable sysclk if this was the last open stream */
818 + if (priv->params_set == 0 && old_params_set) {
820 + "hw_free: Setting SYSCLK to Zero\n");
822 + ret = snd_soc_component_set_sysclk(wm5102_component,
823 + ARIZONA_CLK_SYSCLK,
824 + ARIZONA_CLK_SRC_FLL1,
829 + "hw_free: Failed to set SYSCLK to Zero: %d\n",
835 +static int rpi_cirrus_init_wm5102(struct snd_soc_pcm_runtime *rtd)
837 + struct snd_soc_component *component = rtd->codec_dai->component;
840 + /* no 32kHz input, derive it from sysclk if needed */
841 + snd_soc_component_update_bits(component,
842 + ARIZONA_CLOCK_32K_1, ARIZONA_CLK_32K_SRC_MASK, 2);
844 + if (rpi_cirrus_clear_flls(rtd->card, component))
845 + dev_warn(rtd->card->dev,
846 + "init_wm5102: failed to clear FLLs\n");
848 + ret = snd_soc_component_set_sysclk(component,
849 + ARIZONA_CLK_SYSCLK, ARIZONA_CLK_SRC_FLL1,
850 + 0, SND_SOC_CLOCK_IN);
852 + dev_err(rtd->card->dev,
853 + "Failed to set SYSCLK to Zero: %d\n", ret);
860 +static int rpi_cirrus_init_wm8804(struct snd_soc_pcm_runtime *rtd)
862 + struct snd_soc_dai *codec_dai = rtd->codec_dai;
863 + struct snd_soc_component *component = codec_dai->component;
864 + struct snd_soc_card *card = rtd->card;
865 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
866 + unsigned int val, mask;
869 + for (i = 0; i < 4; i++) {
870 + ret = snd_soc_component_read(component,
871 + WM8804_SPDTX1 + i, &val);
874 + mask = (i == 3) ? 0x3f : 0xff;
875 + priv->iec958_status[i] = val & mask;
878 + /* Setup for 256fs */
879 + ret = snd_soc_dai_set_clkdiv(codec_dai,
880 + WM8804_MCLK_DIV, WM8804_MCLKDIV_256FS);
883 + "init_wm8804: Failed to set MCLK_DIV to 256fs: %d\n",
888 + /* Output OSC on CLKOUT */
889 + ret = snd_soc_dai_set_sysclk(codec_dai,
890 + WM8804_CLKOUT_SRC_OSCCLK, WM8804_CLKOUT_HZ, 0);
893 + "init_wm8804: Failed to set CLKOUT as OSC Frequency: %d\n",
896 + /* Init PLL with default samplerate */
897 + ret = rpi_cirrus_set_wm8804_pll(card, codec_dai,
898 + RPI_CIRRUS_DEFAULT_RATE);
901 + "init_wm8804: Failed to setup PLL for %dHz: %d\n",
902 + RPI_CIRRUS_DEFAULT_RATE, ret);
907 +static struct snd_soc_ops rpi_cirrus_ops = {
908 + .startup = rpi_cirrus_startup,
909 + .hw_params = rpi_cirrus_hw_params,
910 + .hw_free = rpi_cirrus_hw_free,
913 +SND_SOC_DAILINK_DEFS(wm5102,
914 + DAILINK_COMP_ARRAY(COMP_EMPTY()),
915 + DAILINK_COMP_ARRAY(COMP_CODEC("wm5102-codec", "wm5102-aif1")),
916 + DAILINK_COMP_ARRAY(COMP_EMPTY()));
918 +SND_SOC_DAILINK_DEFS(wm8804,
919 + DAILINK_COMP_ARRAY(COMP_CPU("wm5102-aif2")),
920 + DAILINK_COMP_ARRAY(COMP_CODEC("wm8804.1-003b", "wm8804-spdif")));
922 +static struct snd_soc_dai_link rpi_cirrus_dai[] = {
925 + .stream_name = "WM5102 AiFi",
926 + .dai_fmt = SND_SOC_DAIFMT_I2S
927 + | SND_SOC_DAIFMT_NB_NF
928 + | SND_SOC_DAIFMT_CBM_CFM,
929 + .ops = &rpi_cirrus_ops,
930 + .init = rpi_cirrus_init_wm5102,
931 + SND_SOC_DAILINK_REG(wm5102),
934 + .name = "WM5102 SPDIF",
935 + .stream_name = "SPDIF Tx/Rx",
936 + .dai_fmt = SND_SOC_DAIFMT_I2S
937 + | SND_SOC_DAIFMT_NB_NF
938 + | SND_SOC_DAIFMT_CBM_CFM,
939 + .ignore_suspend = 1,
940 + .params = &rpi_cirrus_dai_link2_params,
941 + .init = rpi_cirrus_init_wm8804,
942 + SND_SOC_DAILINK_REG(wm8804),
947 +static int rpi_cirrus_late_probe(struct snd_soc_card *card)
949 + struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
950 + struct snd_soc_pcm_runtime *wm5102_runtime = get_wm5102_runtime(card);
951 + struct snd_soc_pcm_runtime *wm8804_runtime = get_wm8804_runtime(card);
954 + dev_dbg(card->dev, "iec958_bits: %02x %02x %02x %02x\n",
955 + priv->iec958_status[0],
956 + priv->iec958_status[1],
957 + priv->iec958_status[2],
958 + priv->iec958_status[3]);
960 + ret = snd_soc_dai_set_sysclk(
961 + wm5102_runtime->codec_dai, ARIZONA_CLK_SYSCLK, 0, 0);
964 + "Failed to set WM5102 codec dai clk domain: %d\n", ret);
968 + ret = snd_soc_dai_set_sysclk(
969 + wm8804_runtime->cpu_dai, ARIZONA_CLK_SYSCLK, 0, 0);
972 + "Failed to set WM8804 codec dai clk domain: %d\n", ret);
977 +/* audio machine driver */
978 +static struct snd_soc_card rpi_cirrus_card = {
979 + .name = "RPi-Cirrus",
980 + .driver_name = "RPiCirrus",
981 + .owner = THIS_MODULE,
982 + .dai_link = rpi_cirrus_dai,
983 + .num_links = ARRAY_SIZE(rpi_cirrus_dai),
984 + .late_probe = rpi_cirrus_late_probe,
985 + .controls = rpi_cirrus_controls,
986 + .num_controls = ARRAY_SIZE(rpi_cirrus_controls),
987 + .dapm_widgets = rpi_cirrus_dapm_widgets,
988 + .num_dapm_widgets = ARRAY_SIZE(rpi_cirrus_dapm_widgets),
989 + .dapm_routes = rpi_cirrus_dapm_routes,
990 + .num_dapm_routes = ARRAY_SIZE(rpi_cirrus_dapm_routes),
991 + .set_bias_level = rpi_cirrus_set_bias_level,
992 + .set_bias_level_post = rpi_cirrus_set_bias_level_post,
995 +static int rpi_cirrus_probe(struct platform_device *pdev)
998 + struct rpi_cirrus_priv *priv;
999 + struct device_node *i2s_node;
1001 + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1005 + priv->min_rate_idx = 1; /* min samplerate 32kHz */
1006 + priv->card_rate = RPI_CIRRUS_DEFAULT_RATE;
1008 + mutex_init(&priv->lock);
1010 + snd_soc_card_set_drvdata(&rpi_cirrus_card, priv);
1012 + if (!pdev->dev.of_node)
1015 + i2s_node = of_parse_phandle(
1016 + pdev->dev.of_node, "i2s-controller", 0);
1018 + dev_err(&pdev->dev, "i2s-controller missing in DT\n");
1022 + rpi_cirrus_dai[DAI_WM5102].cpus->of_node = i2s_node;
1023 + rpi_cirrus_dai[DAI_WM5102].platforms->of_node = i2s_node;
1025 + rpi_cirrus_card.dev = &pdev->dev;
1027 + ret = devm_snd_soc_register_card(&pdev->dev, &rpi_cirrus_card);
1029 + if (ret == -EPROBE_DEFER)
1030 + dev_dbg(&pdev->dev,
1031 + "register card requested probe deferral\n");
1033 + dev_err(&pdev->dev,
1034 + "Failed to register card: %d\n", ret);
1040 +static const struct of_device_id rpi_cirrus_of_match[] = {
1041 + { .compatible = "wlf,rpi-cirrus", },
1044 +MODULE_DEVICE_TABLE(of, rpi_cirrus_of_match);
1046 +static struct platform_driver rpi_cirrus_driver = {
1048 + .name = "snd-rpi-cirrus",
1049 + .of_match_table = of_match_ptr(rpi_cirrus_of_match),
1051 + .probe = rpi_cirrus_probe,
1054 +module_platform_driver(rpi_cirrus_driver);
1056 +MODULE_AUTHOR("Matthias Reichl <hias@horus.com>");
1057 +MODULE_DESCRIPTION("ASoC driver for Cirrus Logic Audio Card");
1058 +MODULE_LICENSE("GPL");