1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * This driver supports the digital controls for the internal codec
4 * found in Allwinner's A33 SoCs.
6 * (C) Copyright 2010-2016
7 * Reuuimlla Technology Co., Ltd. <www.reuuimllatech.com>
8 * huangxin <huangxin@Reuuimllatech.com>
9 * Mylène Josserand <mylene.josserand@free-electrons.com>
12 #include <linux/module.h>
13 #include <linux/delay.h>
14 #include <linux/clk.h>
16 #include <linux/of_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/regmap.h>
19 #include <linux/log2.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
23 #include <sound/soc-dapm.h>
25 #define SUN8I_SYSCLK_CTL 0x00c
26 #define SUN8I_SYSCLK_CTL_AIF1CLK_ENA 11
27 #define SUN8I_SYSCLK_CTL_AIF1CLK_SRC_PLL (0x2 << 8)
28 #define SUN8I_SYSCLK_CTL_AIF2CLK_ENA 7
29 #define SUN8I_SYSCLK_CTL_AIF2CLK_SRC_PLL (0x2 << 4)
30 #define SUN8I_SYSCLK_CTL_SYSCLK_ENA 3
31 #define SUN8I_SYSCLK_CTL_SYSCLK_SRC 0
32 #define SUN8I_SYSCLK_CTL_SYSCLK_SRC_AIF1CLK (0x0 << 0)
33 #define SUN8I_SYSCLK_CTL_SYSCLK_SRC_AIF2CLK (0x1 << 0)
34 #define SUN8I_MOD_CLK_ENA 0x010
35 #define SUN8I_MOD_CLK_ENA_AIF1 15
36 #define SUN8I_MOD_CLK_ENA_ADC 3
37 #define SUN8I_MOD_CLK_ENA_DAC 2
38 #define SUN8I_MOD_RST_CTL 0x014
39 #define SUN8I_MOD_RST_CTL_AIF1 15
40 #define SUN8I_MOD_RST_CTL_ADC 3
41 #define SUN8I_MOD_RST_CTL_DAC 2
42 #define SUN8I_SYS_SR_CTRL 0x018
43 #define SUN8I_SYS_SR_CTRL_AIF1_FS 12
44 #define SUN8I_SYS_SR_CTRL_AIF2_FS 8
45 #define SUN8I_AIF1CLK_CTRL 0x040
46 #define SUN8I_AIF1CLK_CTRL_AIF1_MSTR_MOD 15
47 #define SUN8I_AIF1CLK_CTRL_AIF1_BCLK_INV 14
48 #define SUN8I_AIF1CLK_CTRL_AIF1_LRCK_INV 13
49 #define SUN8I_AIF1CLK_CTRL_AIF1_BCLK_DIV 9
50 #define SUN8I_AIF1CLK_CTRL_AIF1_LRCK_DIV 6
51 #define SUN8I_AIF1CLK_CTRL_AIF1_WORD_SIZ 4
52 #define SUN8I_AIF1CLK_CTRL_AIF1_WORD_SIZ_16 (1 << 4)
53 #define SUN8I_AIF1CLK_CTRL_AIF1_DATA_FMT 2
54 #define SUN8I_AIF1_ADCDAT_CTRL 0x044
55 #define SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0L_ENA 15
56 #define SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0R_ENA 14
57 #define SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0L_SRC 10
58 #define SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0R_SRC 8
59 #define SUN8I_AIF1_DACDAT_CTRL 0x048
60 #define SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0L_ENA 15
61 #define SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0R_ENA 14
62 #define SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0L_SRC 10
63 #define SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0R_SRC 8
64 #define SUN8I_AIF1_MXR_SRC 0x04c
65 #define SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_AIF1DA0L 15
66 #define SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_AIF2DACL 14
67 #define SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_ADCL 13
68 #define SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_AIF2DACR 12
69 #define SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF1DA0R 11
70 #define SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF2DACR 10
71 #define SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_ADCR 9
72 #define SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF2DACL 8
73 #define SUN8I_ADC_DIG_CTRL 0x100
74 #define SUN8I_ADC_DIG_CTRL_ENAD 15
75 #define SUN8I_ADC_DIG_CTRL_ADOUT_DTS 2
76 #define SUN8I_ADC_DIG_CTRL_ADOUT_DLY 1
77 #define SUN8I_DAC_DIG_CTRL 0x120
78 #define SUN8I_DAC_DIG_CTRL_ENDA 15
79 #define SUN8I_DAC_MXR_SRC 0x130
80 #define SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF1DA0L 15
81 #define SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF1DA1L 14
82 #define SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF2DACL 13
83 #define SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_ADCL 12
84 #define SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF1DA0R 11
85 #define SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF1DA1R 10
86 #define SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF2DACR 9
87 #define SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_ADCR 8
89 #define SUN8I_SYSCLK_CTL_AIF1CLK_SRC_MASK GENMASK(9, 8)
90 #define SUN8I_SYSCLK_CTL_AIF2CLK_SRC_MASK GENMASK(5, 4)
91 #define SUN8I_SYS_SR_CTRL_AIF1_FS_MASK GENMASK(15, 12)
92 #define SUN8I_SYS_SR_CTRL_AIF2_FS_MASK GENMASK(11, 8)
93 #define SUN8I_AIF1CLK_CTRL_AIF1_BCLK_DIV_MASK GENMASK(12, 9)
94 #define SUN8I_AIF1CLK_CTRL_AIF1_LRCK_DIV_MASK GENMASK(8, 6)
95 #define SUN8I_AIF1CLK_CTRL_AIF1_WORD_SIZ_MASK GENMASK(5, 4)
96 #define SUN8I_AIF1CLK_CTRL_AIF1_DATA_FMT_MASK GENMASK(3, 2)
98 struct sun8i_codec_quirks {
99 bool legacy_widgets : 1;
100 bool lrck_inversion : 1;
104 struct regmap *regmap;
105 struct clk *clk_module;
106 const struct sun8i_codec_quirks *quirks;
109 static int sun8i_codec_runtime_resume(struct device *dev)
111 struct sun8i_codec *scodec = dev_get_drvdata(dev);
114 regcache_cache_only(scodec->regmap, false);
116 ret = regcache_sync(scodec->regmap);
118 dev_err(dev, "Failed to sync regmap cache\n");
125 static int sun8i_codec_runtime_suspend(struct device *dev)
127 struct sun8i_codec *scodec = dev_get_drvdata(dev);
129 regcache_cache_only(scodec->regmap, true);
130 regcache_mark_dirty(scodec->regmap);
135 static int sun8i_codec_get_hw_rate(struct snd_pcm_hw_params *params)
137 unsigned int rate = params_rate(params);
168 static int sun8i_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
170 struct sun8i_codec *scodec = snd_soc_dai_get_drvdata(dai);
174 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
175 case SND_SOC_DAIFMT_CBS_CFS: /* Codec slave, DAI master */
178 case SND_SOC_DAIFMT_CBM_CFM: /* Codec Master, DAI slave */
184 regmap_update_bits(scodec->regmap, SUN8I_AIF1CLK_CTRL,
185 BIT(SUN8I_AIF1CLK_CTRL_AIF1_MSTR_MOD),
186 value << SUN8I_AIF1CLK_CTRL_AIF1_MSTR_MOD);
188 /* clock inversion */
189 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
190 case SND_SOC_DAIFMT_NB_NF: /* Normal */
193 case SND_SOC_DAIFMT_IB_IF: /* Inversion */
199 regmap_update_bits(scodec->regmap, SUN8I_AIF1CLK_CTRL,
200 BIT(SUN8I_AIF1CLK_CTRL_AIF1_BCLK_INV),
201 value << SUN8I_AIF1CLK_CTRL_AIF1_BCLK_INV);
204 * It appears that the DAI and the codec in the A33 SoC don't
205 * share the same polarity for the LRCK signal when they mean
206 * 'normal' and 'inverted' in the datasheet.
208 * Since the DAI here is our regular i2s driver that have been
209 * tested with way more codecs than just this one, it means
210 * that the codec probably gets it backward, and we have to
211 * invert the value here.
213 value ^= scodec->quirks->lrck_inversion;
214 regmap_update_bits(scodec->regmap, SUN8I_AIF1CLK_CTRL,
215 BIT(SUN8I_AIF1CLK_CTRL_AIF1_LRCK_INV),
216 value << SUN8I_AIF1CLK_CTRL_AIF1_LRCK_INV);
219 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
220 case SND_SOC_DAIFMT_I2S:
223 case SND_SOC_DAIFMT_LEFT_J:
226 case SND_SOC_DAIFMT_RIGHT_J:
229 case SND_SOC_DAIFMT_DSP_A:
230 case SND_SOC_DAIFMT_DSP_B:
236 regmap_update_bits(scodec->regmap, SUN8I_AIF1CLK_CTRL,
237 SUN8I_AIF1CLK_CTRL_AIF1_DATA_FMT_MASK,
238 value << SUN8I_AIF1CLK_CTRL_AIF1_DATA_FMT);
243 struct sun8i_codec_clk_div {
248 static const struct sun8i_codec_clk_div sun8i_codec_bclk_div[] = {
249 { .div = 1, .val = 0 },
250 { .div = 2, .val = 1 },
251 { .div = 4, .val = 2 },
252 { .div = 6, .val = 3 },
253 { .div = 8, .val = 4 },
254 { .div = 12, .val = 5 },
255 { .div = 16, .val = 6 },
256 { .div = 24, .val = 7 },
257 { .div = 32, .val = 8 },
258 { .div = 48, .val = 9 },
259 { .div = 64, .val = 10 },
260 { .div = 96, .val = 11 },
261 { .div = 128, .val = 12 },
262 { .div = 192, .val = 13 },
265 static u8 sun8i_codec_get_bclk_div(struct sun8i_codec *scodec,
267 unsigned int word_size)
269 unsigned long clk_rate = clk_get_rate(scodec->clk_module);
270 unsigned int div = clk_rate / rate / word_size / 2;
271 unsigned int best_val = 0, best_diff = ~0;
274 for (i = 0; i < ARRAY_SIZE(sun8i_codec_bclk_div); i++) {
275 const struct sun8i_codec_clk_div *bdiv = &sun8i_codec_bclk_div[i];
276 unsigned int diff = abs(bdiv->div - div);
278 if (diff < best_diff) {
280 best_val = bdiv->val;
287 static int sun8i_codec_get_lrck_div(unsigned int channels,
288 unsigned int word_size)
290 unsigned int div = word_size * channels;
292 if (div < 16 || div > 256)
295 return ilog2(div) - 4;
298 static int sun8i_codec_hw_params(struct snd_pcm_substream *substream,
299 struct snd_pcm_hw_params *params,
300 struct snd_soc_dai *dai)
302 struct sun8i_codec *scodec = snd_soc_dai_get_drvdata(dai);
303 int sample_rate, lrck_div;
307 * The CPU DAI handles only a sample of 16 bits. Configure the
308 * codec to handle this type of sample resolution.
310 regmap_update_bits(scodec->regmap, SUN8I_AIF1CLK_CTRL,
311 SUN8I_AIF1CLK_CTRL_AIF1_WORD_SIZ_MASK,
312 SUN8I_AIF1CLK_CTRL_AIF1_WORD_SIZ_16);
314 bclk_div = sun8i_codec_get_bclk_div(scodec, params_rate(params), 16);
315 regmap_update_bits(scodec->regmap, SUN8I_AIF1CLK_CTRL,
316 SUN8I_AIF1CLK_CTRL_AIF1_BCLK_DIV_MASK,
317 bclk_div << SUN8I_AIF1CLK_CTRL_AIF1_BCLK_DIV);
319 lrck_div = sun8i_codec_get_lrck_div(params_channels(params),
320 params_physical_width(params));
324 regmap_update_bits(scodec->regmap, SUN8I_AIF1CLK_CTRL,
325 SUN8I_AIF1CLK_CTRL_AIF1_LRCK_DIV_MASK,
326 lrck_div << SUN8I_AIF1CLK_CTRL_AIF1_LRCK_DIV);
328 sample_rate = sun8i_codec_get_hw_rate(params);
332 regmap_update_bits(scodec->regmap, SUN8I_SYS_SR_CTRL,
333 SUN8I_SYS_SR_CTRL_AIF1_FS_MASK,
334 sample_rate << SUN8I_SYS_SR_CTRL_AIF1_FS);
339 static const char *const sun8i_aif_stereo_mux_enum_values[] = {
340 "Stereo", "Reverse Stereo", "Sum Mono", "Mix Mono"
343 static SOC_ENUM_DOUBLE_DECL(sun8i_aif1_ad0_stereo_mux_enum,
344 SUN8I_AIF1_ADCDAT_CTRL,
345 SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0L_SRC,
346 SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0R_SRC,
347 sun8i_aif_stereo_mux_enum_values);
349 static const struct snd_kcontrol_new sun8i_aif1_ad0_stereo_mux_control =
350 SOC_DAPM_ENUM("AIF1 AD0 Stereo Capture Route",
351 sun8i_aif1_ad0_stereo_mux_enum);
353 static const struct snd_kcontrol_new sun8i_aif1_ad0_mixer_controls[] = {
354 SOC_DAPM_DOUBLE("AIF1 Slot 0 Digital ADC Capture Switch",
356 SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_AIF1DA0L,
357 SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF1DA0R, 1, 0),
358 SOC_DAPM_DOUBLE("AIF2 Digital ADC Capture Switch",
360 SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_AIF2DACL,
361 SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF2DACR, 1, 0),
362 SOC_DAPM_DOUBLE("AIF1 Data Digital ADC Capture Switch",
364 SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_ADCL,
365 SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_ADCR, 1, 0),
366 SOC_DAPM_DOUBLE("AIF2 Inv Digital ADC Capture Switch",
368 SUN8I_AIF1_MXR_SRC_AD0L_MXR_SRC_AIF2DACR,
369 SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF2DACL, 1, 0),
372 static SOC_ENUM_DOUBLE_DECL(sun8i_aif1_da0_stereo_mux_enum,
373 SUN8I_AIF1_DACDAT_CTRL,
374 SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0L_SRC,
375 SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0R_SRC,
376 sun8i_aif_stereo_mux_enum_values);
378 static const struct snd_kcontrol_new sun8i_aif1_da0_stereo_mux_control =
379 SOC_DAPM_ENUM("AIF1 DA0 Stereo Playback Route",
380 sun8i_aif1_da0_stereo_mux_enum);
382 static const struct snd_kcontrol_new sun8i_dac_mixer_controls[] = {
383 SOC_DAPM_DOUBLE("AIF1 Slot 0 Digital DAC Playback Switch",
385 SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF1DA0L,
386 SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF1DA0R, 1, 0),
387 SOC_DAPM_DOUBLE("AIF1 Slot 1 Digital DAC Playback Switch",
389 SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF1DA1L,
390 SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF1DA1R, 1, 0),
391 SOC_DAPM_DOUBLE("AIF2 Digital DAC Playback Switch", SUN8I_DAC_MXR_SRC,
392 SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF2DACL,
393 SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF2DACR, 1, 0),
394 SOC_DAPM_DOUBLE("ADC Digital DAC Playback Switch", SUN8I_DAC_MXR_SRC,
395 SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_ADCL,
396 SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_ADCR, 1, 0),
399 static const struct snd_soc_dapm_widget sun8i_codec_dapm_widgets[] = {
401 SND_SOC_DAPM_CLOCK_SUPPLY("mod"),
403 SND_SOC_DAPM_SUPPLY("AIF1CLK",
405 SUN8I_SYSCLK_CTL_AIF1CLK_ENA, 0, NULL, 0),
406 SND_SOC_DAPM_SUPPLY("SYSCLK",
408 SUN8I_SYSCLK_CTL_SYSCLK_ENA, 0, NULL, 0),
411 SND_SOC_DAPM_SUPPLY("CLK AIF1",
413 SUN8I_MOD_CLK_ENA_AIF1, 0, NULL, 0),
414 SND_SOC_DAPM_SUPPLY("CLK ADC",
416 SUN8I_MOD_CLK_ENA_ADC, 0, NULL, 0),
417 SND_SOC_DAPM_SUPPLY("CLK DAC",
419 SUN8I_MOD_CLK_ENA_DAC, 0, NULL, 0),
422 SND_SOC_DAPM_SUPPLY("RST AIF1",
424 SUN8I_MOD_RST_CTL_AIF1, 0, NULL, 0),
425 SND_SOC_DAPM_SUPPLY("RST ADC",
427 SUN8I_MOD_RST_CTL_ADC, 0, NULL, 0),
428 SND_SOC_DAPM_SUPPLY("RST DAC",
430 SUN8I_MOD_RST_CTL_DAC, 0, NULL, 0),
432 /* Module Supplies */
433 SND_SOC_DAPM_SUPPLY("ADC",
435 SUN8I_ADC_DIG_CTRL_ENAD, 0, NULL, 0),
436 SND_SOC_DAPM_SUPPLY("DAC",
438 SUN8I_DAC_DIG_CTRL_ENDA, 0, NULL, 0),
440 /* AIF "ADC" Outputs */
441 SND_SOC_DAPM_AIF_OUT("AIF1 AD0L", "Capture", 0,
442 SUN8I_AIF1_ADCDAT_CTRL,
443 SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0L_ENA, 0),
444 SND_SOC_DAPM_AIF_OUT("AIF1 AD0R", "Capture", 1,
445 SUN8I_AIF1_ADCDAT_CTRL,
446 SUN8I_AIF1_ADCDAT_CTRL_AIF1_AD0R_ENA, 0),
448 /* AIF "ADC" Mono/Stereo Muxes */
449 SND_SOC_DAPM_MUX("AIF1 AD0L Stereo Mux", SND_SOC_NOPM, 0, 0,
450 &sun8i_aif1_ad0_stereo_mux_control),
451 SND_SOC_DAPM_MUX("AIF1 AD0R Stereo Mux", SND_SOC_NOPM, 0, 0,
452 &sun8i_aif1_ad0_stereo_mux_control),
454 /* AIF "ADC" Mixers */
455 SOC_MIXER_ARRAY("AIF1 AD0L Mixer", SND_SOC_NOPM, 0, 0,
456 sun8i_aif1_ad0_mixer_controls),
457 SOC_MIXER_ARRAY("AIF1 AD0R Mixer", SND_SOC_NOPM, 0, 0,
458 sun8i_aif1_ad0_mixer_controls),
460 /* AIF "DAC" Mono/Stereo Muxes */
461 SND_SOC_DAPM_MUX("AIF1 DA0L Stereo Mux", SND_SOC_NOPM, 0, 0,
462 &sun8i_aif1_da0_stereo_mux_control),
463 SND_SOC_DAPM_MUX("AIF1 DA0R Stereo Mux", SND_SOC_NOPM, 0, 0,
464 &sun8i_aif1_da0_stereo_mux_control),
466 /* AIF "DAC" Inputs */
467 SND_SOC_DAPM_AIF_IN("AIF1 DA0L", "Playback", 0,
468 SUN8I_AIF1_DACDAT_CTRL,
469 SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0L_ENA, 0),
470 SND_SOC_DAPM_AIF_IN("AIF1 DA0R", "Playback", 1,
471 SUN8I_AIF1_DACDAT_CTRL,
472 SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0R_ENA, 0),
474 /* ADC Inputs (connected to analog codec DAPM context) */
475 SND_SOC_DAPM_ADC("ADCL", NULL, SND_SOC_NOPM, 0, 0),
476 SND_SOC_DAPM_ADC("ADCR", NULL, SND_SOC_NOPM, 0, 0),
478 /* DAC Outputs (connected to analog codec DAPM context) */
479 SND_SOC_DAPM_DAC("DACL", NULL, SND_SOC_NOPM, 0, 0),
480 SND_SOC_DAPM_DAC("DACR", NULL, SND_SOC_NOPM, 0, 0),
483 SOC_MIXER_ARRAY("DACL Mixer", SND_SOC_NOPM, 0, 0,
484 sun8i_dac_mixer_controls),
485 SOC_MIXER_ARRAY("DACR Mixer", SND_SOC_NOPM, 0, 0,
486 sun8i_dac_mixer_controls),
489 static const struct snd_soc_dapm_route sun8i_codec_dapm_routes[] = {
491 { "AIF1CLK", NULL, "mod" },
493 { "SYSCLK", NULL, "AIF1CLK" },
495 { "CLK AIF1", NULL, "AIF1CLK" },
496 { "CLK AIF1", NULL, "SYSCLK" },
497 { "RST AIF1", NULL, "CLK AIF1" },
498 { "AIF1 AD0L", NULL, "RST AIF1" },
499 { "AIF1 AD0R", NULL, "RST AIF1" },
500 { "AIF1 DA0L", NULL, "RST AIF1" },
501 { "AIF1 DA0R", NULL, "RST AIF1" },
503 { "CLK ADC", NULL, "SYSCLK" },
504 { "RST ADC", NULL, "CLK ADC" },
505 { "ADC", NULL, "RST ADC" },
506 { "ADCL", NULL, "ADC" },
507 { "ADCR", NULL, "ADC" },
509 { "CLK DAC", NULL, "SYSCLK" },
510 { "RST DAC", NULL, "CLK DAC" },
511 { "DAC", NULL, "RST DAC" },
512 { "DACL", NULL, "DAC" },
513 { "DACR", NULL, "DAC" },
515 /* AIF "ADC" Output Routes */
516 { "AIF1 AD0L", NULL, "AIF1 AD0L Stereo Mux" },
517 { "AIF1 AD0R", NULL, "AIF1 AD0R Stereo Mux" },
519 /* AIF "ADC" Mono/Stereo Mux Routes */
520 { "AIF1 AD0L Stereo Mux", "Stereo", "AIF1 AD0L Mixer" },
521 { "AIF1 AD0L Stereo Mux", "Reverse Stereo", "AIF1 AD0R Mixer" },
522 { "AIF1 AD0L Stereo Mux", "Sum Mono", "AIF1 AD0L Mixer" },
523 { "AIF1 AD0L Stereo Mux", "Sum Mono", "AIF1 AD0R Mixer" },
524 { "AIF1 AD0L Stereo Mux", "Mix Mono", "AIF1 AD0L Mixer" },
525 { "AIF1 AD0L Stereo Mux", "Mix Mono", "AIF1 AD0R Mixer" },
527 { "AIF1 AD0R Stereo Mux", "Stereo", "AIF1 AD0R Mixer" },
528 { "AIF1 AD0R Stereo Mux", "Reverse Stereo", "AIF1 AD0L Mixer" },
529 { "AIF1 AD0R Stereo Mux", "Sum Mono", "AIF1 AD0L Mixer" },
530 { "AIF1 AD0R Stereo Mux", "Sum Mono", "AIF1 AD0R Mixer" },
531 { "AIF1 AD0R Stereo Mux", "Mix Mono", "AIF1 AD0L Mixer" },
532 { "AIF1 AD0R Stereo Mux", "Mix Mono", "AIF1 AD0R Mixer" },
534 /* AIF "ADC" Mixer Routes */
535 { "AIF1 AD0L Mixer", "AIF1 Slot 0 Digital ADC Capture Switch", "AIF1 DA0L Stereo Mux" },
536 { "AIF1 AD0L Mixer", "AIF1 Data Digital ADC Capture Switch", "ADCL" },
538 { "AIF1 AD0R Mixer", "AIF1 Slot 0 Digital ADC Capture Switch", "AIF1 DA0R Stereo Mux" },
539 { "AIF1 AD0R Mixer", "AIF1 Data Digital ADC Capture Switch", "ADCR" },
541 /* AIF "DAC" Mono/Stereo Mux Routes */
542 { "AIF1 DA0L Stereo Mux", "Stereo", "AIF1 DA0L" },
543 { "AIF1 DA0L Stereo Mux", "Reverse Stereo", "AIF1 DA0R" },
544 { "AIF1 DA0L Stereo Mux", "Sum Mono", "AIF1 DA0L" },
545 { "AIF1 DA0L Stereo Mux", "Sum Mono", "AIF1 DA0R" },
546 { "AIF1 DA0L Stereo Mux", "Mix Mono", "AIF1 DA0L" },
547 { "AIF1 DA0L Stereo Mux", "Mix Mono", "AIF1 DA0R" },
549 { "AIF1 DA0R Stereo Mux", "Stereo", "AIF1 DA0R" },
550 { "AIF1 DA0R Stereo Mux", "Reverse Stereo", "AIF1 DA0L" },
551 { "AIF1 DA0R Stereo Mux", "Sum Mono", "AIF1 DA0L" },
552 { "AIF1 DA0R Stereo Mux", "Sum Mono", "AIF1 DA0R" },
553 { "AIF1 DA0R Stereo Mux", "Mix Mono", "AIF1 DA0L" },
554 { "AIF1 DA0R Stereo Mux", "Mix Mono", "AIF1 DA0R" },
556 /* DAC Output Routes */
557 { "DACL", NULL, "DACL Mixer" },
558 { "DACR", NULL, "DACR Mixer" },
560 /* DAC Mixer Routes */
561 { "DACL Mixer", "AIF1 Slot 0 Digital DAC Playback Switch", "AIF1 DA0L Stereo Mux" },
562 { "DACL Mixer", "ADC Digital DAC Playback Switch", "ADCL" },
564 { "DACR Mixer", "AIF1 Slot 0 Digital DAC Playback Switch", "AIF1 DA0R Stereo Mux" },
565 { "DACR Mixer", "ADC Digital DAC Playback Switch", "ADCR" },
568 static const struct snd_soc_dapm_widget sun8i_codec_legacy_widgets[] = {
569 /* Legacy ADC Inputs (connected to analog codec DAPM context) */
570 SND_SOC_DAPM_ADC("AIF1 Slot 0 Left ADC", NULL, SND_SOC_NOPM, 0, 0),
571 SND_SOC_DAPM_ADC("AIF1 Slot 0 Right ADC", NULL, SND_SOC_NOPM, 0, 0),
573 /* Legacy DAC Outputs (connected to analog codec DAPM context) */
574 SND_SOC_DAPM_DAC("AIF1 Slot 0 Left", NULL, SND_SOC_NOPM, 0, 0),
575 SND_SOC_DAPM_DAC("AIF1 Slot 0 Right", NULL, SND_SOC_NOPM, 0, 0),
578 static const struct snd_soc_dapm_route sun8i_codec_legacy_routes[] = {
579 /* Legacy ADC Routes */
580 { "ADCL", NULL, "AIF1 Slot 0 Left ADC" },
581 { "ADCR", NULL, "AIF1 Slot 0 Right ADC" },
583 /* Legacy DAC Routes */
584 { "AIF1 Slot 0 Left", NULL, "DACL" },
585 { "AIF1 Slot 0 Right", NULL, "DACR" },
588 static int sun8i_codec_component_probe(struct snd_soc_component *component)
590 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
591 struct sun8i_codec *scodec = snd_soc_component_get_drvdata(component);
594 /* Add widgets for backward compatibility with old device trees. */
595 if (scodec->quirks->legacy_widgets) {
596 ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_legacy_widgets,
597 ARRAY_SIZE(sun8i_codec_legacy_widgets));
601 ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_legacy_routes,
602 ARRAY_SIZE(sun8i_codec_legacy_routes));
608 * AIF1CLK and AIF2CLK share a pair of clock parents: PLL_AUDIO ("mod")
609 * and MCLK (from the CPU DAI connected to AIF1). MCLK's parent is also
610 * PLL_AUDIO, so using it adds no additional flexibility. Use PLL_AUDIO
611 * directly to simplify the clock tree.
613 regmap_update_bits(scodec->regmap, SUN8I_SYSCLK_CTL,
614 SUN8I_SYSCLK_CTL_AIF1CLK_SRC_MASK |
615 SUN8I_SYSCLK_CTL_AIF2CLK_SRC_MASK,
616 SUN8I_SYSCLK_CTL_AIF1CLK_SRC_PLL |
617 SUN8I_SYSCLK_CTL_AIF2CLK_SRC_PLL);
619 /* Use AIF1CLK as the SYSCLK parent since AIF1 is used most often. */
620 regmap_update_bits(scodec->regmap, SUN8I_SYSCLK_CTL,
621 BIT(SUN8I_SYSCLK_CTL_SYSCLK_SRC),
622 SUN8I_SYSCLK_CTL_SYSCLK_SRC_AIF1CLK);
627 static const struct snd_soc_dai_ops sun8i_codec_dai_ops = {
628 .hw_params = sun8i_codec_hw_params,
629 .set_fmt = sun8i_set_fmt,
632 static struct snd_soc_dai_driver sun8i_codec_dai = {
634 /* playback capabilities */
636 .stream_name = "Playback",
639 .rates = SNDRV_PCM_RATE_8000_192000,
640 .formats = SNDRV_PCM_FMTBIT_S16_LE,
642 /* capture capabilities */
644 .stream_name = "Capture",
647 .rates = SNDRV_PCM_RATE_8000_192000,
648 .formats = SNDRV_PCM_FMTBIT_S16_LE,
652 .ops = &sun8i_codec_dai_ops,
655 static const struct snd_soc_component_driver sun8i_soc_component = {
656 .dapm_widgets = sun8i_codec_dapm_widgets,
657 .num_dapm_widgets = ARRAY_SIZE(sun8i_codec_dapm_widgets),
658 .dapm_routes = sun8i_codec_dapm_routes,
659 .num_dapm_routes = ARRAY_SIZE(sun8i_codec_dapm_routes),
660 .probe = sun8i_codec_component_probe,
662 .use_pmdown_time = 1,
664 .non_legacy_dai_naming = 1,
667 static const struct regmap_config sun8i_codec_regmap_config = {
671 .max_register = SUN8I_DAC_MXR_SRC,
673 .cache_type = REGCACHE_FLAT,
676 static int sun8i_codec_probe(struct platform_device *pdev)
678 struct sun8i_codec *scodec;
682 scodec = devm_kzalloc(&pdev->dev, sizeof(*scodec), GFP_KERNEL);
686 scodec->clk_module = devm_clk_get(&pdev->dev, "mod");
687 if (IS_ERR(scodec->clk_module)) {
688 dev_err(&pdev->dev, "Failed to get the module clock\n");
689 return PTR_ERR(scodec->clk_module);
692 base = devm_platform_ioremap_resource(pdev, 0);
694 dev_err(&pdev->dev, "Failed to map the registers\n");
695 return PTR_ERR(base);
698 scodec->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "bus", base,
699 &sun8i_codec_regmap_config);
700 if (IS_ERR(scodec->regmap)) {
701 dev_err(&pdev->dev, "Failed to create our regmap\n");
702 return PTR_ERR(scodec->regmap);
705 scodec->quirks = of_device_get_match_data(&pdev->dev);
707 platform_set_drvdata(pdev, scodec);
709 pm_runtime_enable(&pdev->dev);
710 if (!pm_runtime_enabled(&pdev->dev)) {
711 ret = sun8i_codec_runtime_resume(&pdev->dev);
716 ret = devm_snd_soc_register_component(&pdev->dev, &sun8i_soc_component,
717 &sun8i_codec_dai, 1);
719 dev_err(&pdev->dev, "Failed to register codec\n");
726 if (!pm_runtime_status_suspended(&pdev->dev))
727 sun8i_codec_runtime_suspend(&pdev->dev);
730 pm_runtime_disable(&pdev->dev);
735 static int sun8i_codec_remove(struct platform_device *pdev)
737 pm_runtime_disable(&pdev->dev);
738 if (!pm_runtime_status_suspended(&pdev->dev))
739 sun8i_codec_runtime_suspend(&pdev->dev);
744 static const struct sun8i_codec_quirks sun8i_a33_quirks = {
745 .legacy_widgets = true,
746 .lrck_inversion = true,
749 static const struct sun8i_codec_quirks sun50i_a64_quirks = {
752 static const struct of_device_id sun8i_codec_of_match[] = {
753 { .compatible = "allwinner,sun8i-a33-codec", .data = &sun8i_a33_quirks },
754 { .compatible = "allwinner,sun50i-a64-codec", .data = &sun50i_a64_quirks },
757 MODULE_DEVICE_TABLE(of, sun8i_codec_of_match);
759 static const struct dev_pm_ops sun8i_codec_pm_ops = {
760 SET_RUNTIME_PM_OPS(sun8i_codec_runtime_suspend,
761 sun8i_codec_runtime_resume, NULL)
764 static struct platform_driver sun8i_codec_driver = {
766 .name = "sun8i-codec",
767 .of_match_table = sun8i_codec_of_match,
768 .pm = &sun8i_codec_pm_ops,
770 .probe = sun8i_codec_probe,
771 .remove = sun8i_codec_remove,
773 module_platform_driver(sun8i_codec_driver);
775 MODULE_DESCRIPTION("Allwinner A33 (sun8i) codec driver");
776 MODULE_AUTHOR("Mylène Josserand <mylene.josserand@free-electrons.com>");
777 MODULE_LICENSE("GPL");
778 MODULE_ALIAS("platform:sun8i-codec");