xref: /openbmc/linux/sound/soc/sunxi/sun8i-codec.c (revision a2f6d303)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
236c68493SMylène Josserand /*
336c68493SMylène Josserand  * This driver supports the digital controls for the internal codec
436c68493SMylène Josserand  * found in Allwinner's A33 SoCs.
536c68493SMylène Josserand  *
636c68493SMylène Josserand  * (C) Copyright 2010-2016
736c68493SMylène Josserand  * Reuuimlla Technology Co., Ltd. <www.reuuimllatech.com>
836c68493SMylène Josserand  * huangxin <huangxin@Reuuimllatech.com>
936c68493SMylène Josserand  * Mylène Josserand <mylene.josserand@free-electrons.com>
1036c68493SMylène Josserand  */
1136c68493SMylène Josserand 
1236c68493SMylène Josserand #include <linux/module.h>
1336c68493SMylène Josserand #include <linux/delay.h>
1436c68493SMylène Josserand #include <linux/clk.h>
1536c68493SMylène Josserand #include <linux/io.h>
1690cac932SSamuel Holland #include <linux/of_device.h>
1736c68493SMylène Josserand #include <linux/pm_runtime.h>
1836c68493SMylène Josserand #include <linux/regmap.h>
1913c3bf17SVasily Khoruzhick #include <linux/log2.h>
2036c68493SMylène Josserand 
2136c68493SMylène Josserand #include <sound/pcm_params.h>
2236c68493SMylène Josserand #include <sound/soc.h>
2336c68493SMylène Josserand #include <sound/soc-dapm.h>
2436c68493SMylène Josserand 
2536c68493SMylène Josserand #define SUN8I_SYSCLK_CTL				0x00c
2636c68493SMylène Josserand #define SUN8I_SYSCLK_CTL_AIF1CLK_ENA			11
2736c68493SMylène Josserand #define SUN8I_SYSCLK_CTL_AIF1CLK_SRC_PLL		9
2836c68493SMylène Josserand #define SUN8I_SYSCLK_CTL_AIF1CLK_SRC			8
2936c68493SMylène Josserand #define SUN8I_SYSCLK_CTL_SYSCLK_ENA			3
3036c68493SMylène Josserand #define SUN8I_SYSCLK_CTL_SYSCLK_SRC			0
3136c68493SMylène Josserand #define SUN8I_MOD_CLK_ENA				0x010
3236c68493SMylène Josserand #define SUN8I_MOD_CLK_ENA_AIF1				15
33eda85d1fSMylene JOSSERAND #define SUN8I_MOD_CLK_ENA_ADC				3
3436c68493SMylène Josserand #define SUN8I_MOD_CLK_ENA_DAC				2
3536c68493SMylène Josserand #define SUN8I_MOD_RST_CTL				0x014
3636c68493SMylène Josserand #define SUN8I_MOD_RST_CTL_AIF1				15
37eda85d1fSMylene JOSSERAND #define SUN8I_MOD_RST_CTL_ADC				3
3836c68493SMylène Josserand #define SUN8I_MOD_RST_CTL_DAC				2
3936c68493SMylène Josserand #define SUN8I_SYS_SR_CTRL				0x018
4036c68493SMylène Josserand #define SUN8I_SYS_SR_CTRL_AIF1_FS			12
4136c68493SMylène Josserand #define SUN8I_SYS_SR_CTRL_AIF2_FS			8
4236c68493SMylène Josserand #define SUN8I_AIF1CLK_CTRL				0x040
4336c68493SMylène Josserand #define SUN8I_AIF1CLK_CTRL_AIF1_MSTR_MOD		15
4436c68493SMylène Josserand #define SUN8I_AIF1CLK_CTRL_AIF1_BCLK_INV		14
4536c68493SMylène Josserand #define SUN8I_AIF1CLK_CTRL_AIF1_LRCK_INV		13
4636c68493SMylène Josserand #define SUN8I_AIF1CLK_CTRL_AIF1_BCLK_DIV		9
4736c68493SMylène Josserand #define SUN8I_AIF1CLK_CTRL_AIF1_LRCK_DIV		6
4836c68493SMylène Josserand #define SUN8I_AIF1CLK_CTRL_AIF1_WORD_SIZ		4
4936c68493SMylène Josserand #define SUN8I_AIF1CLK_CTRL_AIF1_WORD_SIZ_16		(1 << 4)
5036c68493SMylène Josserand #define SUN8I_AIF1CLK_CTRL_AIF1_DATA_FMT		2
51eda85d1fSMylene JOSSERAND #define SUN8I_AIF1_ADCDAT_CTRL				0x044
52eda85d1fSMylene JOSSERAND #define SUN8I_AIF1_ADCDAT_CTRL_AIF1_DA0L_ENA		15
53eda85d1fSMylene JOSSERAND #define SUN8I_AIF1_ADCDAT_CTRL_AIF1_DA0R_ENA		14
5436c68493SMylène Josserand #define SUN8I_AIF1_DACDAT_CTRL				0x048
5536c68493SMylène Josserand #define SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0L_ENA		15
5636c68493SMylène Josserand #define SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0R_ENA		14
57eda85d1fSMylene JOSSERAND #define SUN8I_AIF1_MXR_SRC				0x04c
58eda85d1fSMylene JOSSERAND #define SUN8I_AIF1_MXR_SRC_AD0L_MXL_SRC_AIF1DA0L	15
59eda85d1fSMylene JOSSERAND #define SUN8I_AIF1_MXR_SRC_AD0L_MXL_SRC_AIF2DACL	14
60eda85d1fSMylene JOSSERAND #define SUN8I_AIF1_MXR_SRC_AD0L_MXL_SRC_ADCL		13
61eda85d1fSMylene JOSSERAND #define SUN8I_AIF1_MXR_SRC_AD0L_MXL_SRC_AIF2DACR	12
62eda85d1fSMylene JOSSERAND #define SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF1DA0R	11
63eda85d1fSMylene JOSSERAND #define SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF2DACR	10
64eda85d1fSMylene JOSSERAND #define SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_ADCR		9
65eda85d1fSMylene JOSSERAND #define SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF2DACL	8
66eda85d1fSMylene JOSSERAND #define SUN8I_ADC_DIG_CTRL				0x100
67eda85d1fSMylene JOSSERAND #define SUN8I_ADC_DIG_CTRL_ENDA			15
68eda85d1fSMylene JOSSERAND #define SUN8I_ADC_DIG_CTRL_ADOUT_DTS			2
69eda85d1fSMylene JOSSERAND #define SUN8I_ADC_DIG_CTRL_ADOUT_DLY			1
7036c68493SMylène Josserand #define SUN8I_DAC_DIG_CTRL				0x120
7136c68493SMylène Josserand #define SUN8I_DAC_DIG_CTRL_ENDA			15
7236c68493SMylène Josserand #define SUN8I_DAC_MXR_SRC				0x130
7336c68493SMylène Josserand #define SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF1DA0L	15
7436c68493SMylène Josserand #define SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF1DA1L	14
7536c68493SMylène Josserand #define SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF2DACL	13
7636c68493SMylène Josserand #define SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_ADCL		12
7736c68493SMylène Josserand #define SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF1DA0R	11
7836c68493SMylène Josserand #define SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF1DA1R	10
7936c68493SMylène Josserand #define SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF2DACR	9
8036c68493SMylène Josserand #define SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_ADCR		8
8136c68493SMylène Josserand 
8236c68493SMylène Josserand #define SUN8I_SYS_SR_CTRL_AIF1_FS_MASK		GENMASK(15, 12)
8336c68493SMylène Josserand #define SUN8I_SYS_SR_CTRL_AIF2_FS_MASK		GENMASK(11, 8)
8496781fd9SSamuel Holland #define SUN8I_AIF1CLK_CTRL_AIF1_DATA_FMT_MASK	GENMASK(3, 2)
8536c68493SMylène Josserand #define SUN8I_AIF1CLK_CTRL_AIF1_WORD_SIZ_MASK	GENMASK(5, 4)
8636c68493SMylène Josserand #define SUN8I_AIF1CLK_CTRL_AIF1_LRCK_DIV_MASK	GENMASK(8, 6)
87316b7758SMaxime Ripard #define SUN8I_AIF1CLK_CTRL_AIF1_BCLK_DIV_MASK	GENMASK(12, 9)
8836c68493SMylène Josserand 
8990cac932SSamuel Holland struct sun8i_codec_quirks {
9090cac932SSamuel Holland 	bool legacy_widgets	: 1;
917518805fSSamuel Holland 	bool lrck_inversion	: 1;
9290cac932SSamuel Holland };
9390cac932SSamuel Holland 
9436c68493SMylène Josserand struct sun8i_codec {
9536c68493SMylène Josserand 	struct regmap			*regmap;
9636c68493SMylène Josserand 	struct clk			*clk_module;
9736c68493SMylène Josserand 	struct clk			*clk_bus;
9890cac932SSamuel Holland 	const struct sun8i_codec_quirks	*quirks;
9936c68493SMylène Josserand };
10036c68493SMylène Josserand 
10136c68493SMylène Josserand static int sun8i_codec_runtime_resume(struct device *dev)
10236c68493SMylène Josserand {
10336c68493SMylène Josserand 	struct sun8i_codec *scodec = dev_get_drvdata(dev);
10436c68493SMylène Josserand 	int ret;
10536c68493SMylène Josserand 
10636c68493SMylène Josserand 	ret = clk_prepare_enable(scodec->clk_module);
10736c68493SMylène Josserand 	if (ret) {
10836c68493SMylène Josserand 		dev_err(dev, "Failed to enable the module clock\n");
10936c68493SMylène Josserand 		return ret;
11036c68493SMylène Josserand 	}
11136c68493SMylène Josserand 
11236c68493SMylène Josserand 	ret = clk_prepare_enable(scodec->clk_bus);
11336c68493SMylène Josserand 	if (ret) {
11436c68493SMylène Josserand 		dev_err(dev, "Failed to enable the bus clock\n");
11536c68493SMylène Josserand 		goto err_disable_modclk;
11636c68493SMylène Josserand 	}
11736c68493SMylène Josserand 
11836c68493SMylène Josserand 	regcache_cache_only(scodec->regmap, false);
11936c68493SMylène Josserand 
12036c68493SMylène Josserand 	ret = regcache_sync(scodec->regmap);
12136c68493SMylène Josserand 	if (ret) {
12236c68493SMylène Josserand 		dev_err(dev, "Failed to sync regmap cache\n");
12336c68493SMylène Josserand 		goto err_disable_clk;
12436c68493SMylène Josserand 	}
12536c68493SMylène Josserand 
12636c68493SMylène Josserand 	return 0;
12736c68493SMylène Josserand 
12836c68493SMylène Josserand err_disable_clk:
12936c68493SMylène Josserand 	clk_disable_unprepare(scodec->clk_bus);
13036c68493SMylène Josserand 
13136c68493SMylène Josserand err_disable_modclk:
13236c68493SMylène Josserand 	clk_disable_unprepare(scodec->clk_module);
13336c68493SMylène Josserand 
13436c68493SMylène Josserand 	return ret;
13536c68493SMylène Josserand }
13636c68493SMylène Josserand 
13736c68493SMylène Josserand static int sun8i_codec_runtime_suspend(struct device *dev)
13836c68493SMylène Josserand {
13936c68493SMylène Josserand 	struct sun8i_codec *scodec = dev_get_drvdata(dev);
14036c68493SMylène Josserand 
14136c68493SMylène Josserand 	regcache_cache_only(scodec->regmap, true);
14236c68493SMylène Josserand 	regcache_mark_dirty(scodec->regmap);
14336c68493SMylène Josserand 
14436c68493SMylène Josserand 	clk_disable_unprepare(scodec->clk_module);
14536c68493SMylène Josserand 	clk_disable_unprepare(scodec->clk_bus);
14636c68493SMylène Josserand 
14736c68493SMylène Josserand 	return 0;
14836c68493SMylène Josserand }
14936c68493SMylène Josserand 
15036c68493SMylène Josserand static int sun8i_codec_get_hw_rate(struct snd_pcm_hw_params *params)
15136c68493SMylène Josserand {
15236c68493SMylène Josserand 	unsigned int rate = params_rate(params);
15336c68493SMylène Josserand 
15436c68493SMylène Josserand 	switch (rate) {
15536c68493SMylène Josserand 	case 8000:
15636c68493SMylène Josserand 	case 7350:
15736c68493SMylène Josserand 		return 0x0;
15836c68493SMylène Josserand 	case 11025:
15936c68493SMylène Josserand 		return 0x1;
16036c68493SMylène Josserand 	case 12000:
16136c68493SMylène Josserand 		return 0x2;
16236c68493SMylène Josserand 	case 16000:
16336c68493SMylène Josserand 		return 0x3;
16436c68493SMylène Josserand 	case 22050:
16536c68493SMylène Josserand 		return 0x4;
16636c68493SMylène Josserand 	case 24000:
16736c68493SMylène Josserand 		return 0x5;
16836c68493SMylène Josserand 	case 32000:
16936c68493SMylène Josserand 		return 0x6;
17036c68493SMylène Josserand 	case 44100:
17136c68493SMylène Josserand 		return 0x7;
17236c68493SMylène Josserand 	case 48000:
17336c68493SMylène Josserand 		return 0x8;
17436c68493SMylène Josserand 	case 96000:
17536c68493SMylène Josserand 		return 0x9;
17636c68493SMylène Josserand 	case 192000:
17736c68493SMylène Josserand 		return 0xa;
17836c68493SMylène Josserand 	default:
17936c68493SMylène Josserand 		return -EINVAL;
18036c68493SMylène Josserand 	}
18136c68493SMylène Josserand }
18236c68493SMylène Josserand 
18336c68493SMylène Josserand static int sun8i_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
18436c68493SMylène Josserand {
1857ec9b872SKuninori Morimoto 	struct sun8i_codec *scodec = snd_soc_component_get_drvdata(dai->component);
18636c68493SMylène Josserand 	u32 value;
18736c68493SMylène Josserand 
18836c68493SMylène Josserand 	/* clock masters */
18936c68493SMylène Josserand 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
190560bfe77SMaxime Ripard 	case SND_SOC_DAIFMT_CBS_CFS: /* Codec slave, DAI master */
191560bfe77SMaxime Ripard 		value = 0x1;
19236c68493SMylène Josserand 		break;
193560bfe77SMaxime Ripard 	case SND_SOC_DAIFMT_CBM_CFM: /* Codec Master, DAI slave */
194560bfe77SMaxime Ripard 		value = 0x0;
19536c68493SMylène Josserand 		break;
19636c68493SMylène Josserand 	default:
19736c68493SMylène Josserand 		return -EINVAL;
19836c68493SMylène Josserand 	}
19936c68493SMylène Josserand 	regmap_update_bits(scodec->regmap, SUN8I_AIF1CLK_CTRL,
20036c68493SMylène Josserand 			   BIT(SUN8I_AIF1CLK_CTRL_AIF1_MSTR_MOD),
20136c68493SMylène Josserand 			   value << SUN8I_AIF1CLK_CTRL_AIF1_MSTR_MOD);
20236c68493SMylène Josserand 
20336c68493SMylène Josserand 	/* clock inversion */
20436c68493SMylène Josserand 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
20536c68493SMylène Josserand 	case SND_SOC_DAIFMT_NB_NF: /* Normal */
20636c68493SMylène Josserand 		value = 0x0;
20736c68493SMylène Josserand 		break;
20836c68493SMylène Josserand 	case SND_SOC_DAIFMT_IB_IF: /* Inversion */
20936c68493SMylène Josserand 		value = 0x1;
21036c68493SMylène Josserand 		break;
21136c68493SMylène Josserand 	default:
21236c68493SMylène Josserand 		return -EINVAL;
21336c68493SMylène Josserand 	}
21436c68493SMylène Josserand 	regmap_update_bits(scodec->regmap, SUN8I_AIF1CLK_CTRL,
21536c68493SMylène Josserand 			   BIT(SUN8I_AIF1CLK_CTRL_AIF1_BCLK_INV),
21636c68493SMylène Josserand 			   value << SUN8I_AIF1CLK_CTRL_AIF1_BCLK_INV);
217e7b8a6d3SMaxime Ripard 
218e7b8a6d3SMaxime Ripard 	/*
2197518805fSSamuel Holland 	 * It appears that the DAI and the codec in the A33 SoC don't
2207518805fSSamuel Holland 	 * share the same polarity for the LRCK signal when they mean
2217518805fSSamuel Holland 	 * 'normal' and 'inverted' in the datasheet.
222e7b8a6d3SMaxime Ripard 	 *
223e7b8a6d3SMaxime Ripard 	 * Since the DAI here is our regular i2s driver that have been
224e7b8a6d3SMaxime Ripard 	 * tested with way more codecs than just this one, it means
225e7b8a6d3SMaxime Ripard 	 * that the codec probably gets it backward, and we have to
226e7b8a6d3SMaxime Ripard 	 * invert the value here.
227e7b8a6d3SMaxime Ripard 	 */
2287518805fSSamuel Holland 	value ^= scodec->quirks->lrck_inversion;
22936c68493SMylène Josserand 	regmap_update_bits(scodec->regmap, SUN8I_AIF1CLK_CTRL,
23036c68493SMylène Josserand 			   BIT(SUN8I_AIF1CLK_CTRL_AIF1_LRCK_INV),
2317518805fSSamuel Holland 			   value << SUN8I_AIF1CLK_CTRL_AIF1_LRCK_INV);
23236c68493SMylène Josserand 
23336c68493SMylène Josserand 	/* DAI format */
23436c68493SMylène Josserand 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
23536c68493SMylène Josserand 	case SND_SOC_DAIFMT_I2S:
23636c68493SMylène Josserand 		value = 0x0;
23736c68493SMylène Josserand 		break;
23836c68493SMylène Josserand 	case SND_SOC_DAIFMT_LEFT_J:
23936c68493SMylène Josserand 		value = 0x1;
24036c68493SMylène Josserand 		break;
24136c68493SMylène Josserand 	case SND_SOC_DAIFMT_RIGHT_J:
24236c68493SMylène Josserand 		value = 0x2;
24336c68493SMylène Josserand 		break;
24436c68493SMylène Josserand 	case SND_SOC_DAIFMT_DSP_A:
24536c68493SMylène Josserand 	case SND_SOC_DAIFMT_DSP_B:
24636c68493SMylène Josserand 		value = 0x3;
24736c68493SMylène Josserand 		break;
24836c68493SMylène Josserand 	default:
24936c68493SMylène Josserand 		return -EINVAL;
25036c68493SMylène Josserand 	}
25136c68493SMylène Josserand 	regmap_update_bits(scodec->regmap, SUN8I_AIF1CLK_CTRL,
25296781fd9SSamuel Holland 			   SUN8I_AIF1CLK_CTRL_AIF1_DATA_FMT_MASK,
25336c68493SMylène Josserand 			   value << SUN8I_AIF1CLK_CTRL_AIF1_DATA_FMT);
25436c68493SMylène Josserand 
25536c68493SMylène Josserand 	return 0;
25636c68493SMylène Josserand }
25736c68493SMylène Josserand 
258316b7758SMaxime Ripard struct sun8i_codec_clk_div {
259316b7758SMaxime Ripard 	u8	div;
260316b7758SMaxime Ripard 	u8	val;
261316b7758SMaxime Ripard };
262316b7758SMaxime Ripard 
263316b7758SMaxime Ripard static const struct sun8i_codec_clk_div sun8i_codec_bclk_div[] = {
264316b7758SMaxime Ripard 	{ .div = 1,	.val = 0 },
265316b7758SMaxime Ripard 	{ .div = 2,	.val = 1 },
266316b7758SMaxime Ripard 	{ .div = 4,	.val = 2 },
267316b7758SMaxime Ripard 	{ .div = 6,	.val = 3 },
268316b7758SMaxime Ripard 	{ .div = 8,	.val = 4 },
269316b7758SMaxime Ripard 	{ .div = 12,	.val = 5 },
270316b7758SMaxime Ripard 	{ .div = 16,	.val = 6 },
271316b7758SMaxime Ripard 	{ .div = 24,	.val = 7 },
272316b7758SMaxime Ripard 	{ .div = 32,	.val = 8 },
273316b7758SMaxime Ripard 	{ .div = 48,	.val = 9 },
274316b7758SMaxime Ripard 	{ .div = 64,	.val = 10 },
275316b7758SMaxime Ripard 	{ .div = 96,	.val = 11 },
276316b7758SMaxime Ripard 	{ .div = 128,	.val = 12 },
277316b7758SMaxime Ripard 	{ .div = 192,	.val = 13 },
278316b7758SMaxime Ripard };
279316b7758SMaxime Ripard 
280316b7758SMaxime Ripard static u8 sun8i_codec_get_bclk_div(struct sun8i_codec *scodec,
281316b7758SMaxime Ripard 				   unsigned int rate,
282316b7758SMaxime Ripard 				   unsigned int word_size)
283316b7758SMaxime Ripard {
284316b7758SMaxime Ripard 	unsigned long clk_rate = clk_get_rate(scodec->clk_module);
285316b7758SMaxime Ripard 	unsigned int div = clk_rate / rate / word_size / 2;
286316b7758SMaxime Ripard 	unsigned int best_val = 0, best_diff = ~0;
287316b7758SMaxime Ripard 	int i;
288316b7758SMaxime Ripard 
289316b7758SMaxime Ripard 	for (i = 0; i < ARRAY_SIZE(sun8i_codec_bclk_div); i++) {
290316b7758SMaxime Ripard 		const struct sun8i_codec_clk_div *bdiv = &sun8i_codec_bclk_div[i];
291316b7758SMaxime Ripard 		unsigned int diff = abs(bdiv->div - div);
292316b7758SMaxime Ripard 
293316b7758SMaxime Ripard 		if (diff < best_diff) {
294316b7758SMaxime Ripard 			best_diff = diff;
295316b7758SMaxime Ripard 			best_val = bdiv->val;
296316b7758SMaxime Ripard 		}
297316b7758SMaxime Ripard 	}
298316b7758SMaxime Ripard 
299316b7758SMaxime Ripard 	return best_val;
300316b7758SMaxime Ripard }
301316b7758SMaxime Ripard 
30213c3bf17SVasily Khoruzhick static int sun8i_codec_get_lrck_div(unsigned int channels,
30313c3bf17SVasily Khoruzhick 				    unsigned int word_size)
30413c3bf17SVasily Khoruzhick {
30513c3bf17SVasily Khoruzhick 	unsigned int div = word_size * channels;
30613c3bf17SVasily Khoruzhick 
30713c3bf17SVasily Khoruzhick 	if (div < 16 || div > 256)
30813c3bf17SVasily Khoruzhick 		return -EINVAL;
30913c3bf17SVasily Khoruzhick 
31013c3bf17SVasily Khoruzhick 	return ilog2(div) - 4;
31113c3bf17SVasily Khoruzhick }
31213c3bf17SVasily Khoruzhick 
31336c68493SMylène Josserand static int sun8i_codec_hw_params(struct snd_pcm_substream *substream,
31436c68493SMylène Josserand 				 struct snd_pcm_hw_params *params,
31536c68493SMylène Josserand 				 struct snd_soc_dai *dai)
31636c68493SMylène Josserand {
3177ec9b872SKuninori Morimoto 	struct sun8i_codec *scodec = snd_soc_component_get_drvdata(dai->component);
31813c3bf17SVasily Khoruzhick 	int sample_rate, lrck_div;
319316b7758SMaxime Ripard 	u8 bclk_div;
32036c68493SMylène Josserand 
32136c68493SMylène Josserand 	/*
32236c68493SMylène Josserand 	 * The CPU DAI handles only a sample of 16 bits. Configure the
32336c68493SMylène Josserand 	 * codec to handle this type of sample resolution.
32436c68493SMylène Josserand 	 */
32536c68493SMylène Josserand 	regmap_update_bits(scodec->regmap, SUN8I_AIF1CLK_CTRL,
32636c68493SMylène Josserand 			   SUN8I_AIF1CLK_CTRL_AIF1_WORD_SIZ_MASK,
32736c68493SMylène Josserand 			   SUN8I_AIF1CLK_CTRL_AIF1_WORD_SIZ_16);
32836c68493SMylène Josserand 
329316b7758SMaxime Ripard 	bclk_div = sun8i_codec_get_bclk_div(scodec, params_rate(params), 16);
330316b7758SMaxime Ripard 	regmap_update_bits(scodec->regmap, SUN8I_AIF1CLK_CTRL,
331316b7758SMaxime Ripard 			   SUN8I_AIF1CLK_CTRL_AIF1_BCLK_DIV_MASK,
332316b7758SMaxime Ripard 			   bclk_div << SUN8I_AIF1CLK_CTRL_AIF1_BCLK_DIV);
333316b7758SMaxime Ripard 
33413c3bf17SVasily Khoruzhick 	lrck_div = sun8i_codec_get_lrck_div(params_channels(params),
33513c3bf17SVasily Khoruzhick 					    params_physical_width(params));
33613c3bf17SVasily Khoruzhick 	if (lrck_div < 0)
33713c3bf17SVasily Khoruzhick 		return lrck_div;
33813c3bf17SVasily Khoruzhick 
33936c68493SMylène Josserand 	regmap_update_bits(scodec->regmap, SUN8I_AIF1CLK_CTRL,
34036c68493SMylène Josserand 			   SUN8I_AIF1CLK_CTRL_AIF1_LRCK_DIV_MASK,
34113c3bf17SVasily Khoruzhick 			   lrck_div << SUN8I_AIF1CLK_CTRL_AIF1_LRCK_DIV);
34236c68493SMylène Josserand 
34336c68493SMylène Josserand 	sample_rate = sun8i_codec_get_hw_rate(params);
34436c68493SMylène Josserand 	if (sample_rate < 0)
34536c68493SMylène Josserand 		return sample_rate;
34636c68493SMylène Josserand 
34736c68493SMylène Josserand 	regmap_update_bits(scodec->regmap, SUN8I_SYS_SR_CTRL,
34836c68493SMylène Josserand 			   SUN8I_SYS_SR_CTRL_AIF1_FS_MASK,
34936c68493SMylène Josserand 			   sample_rate << SUN8I_SYS_SR_CTRL_AIF1_FS);
35036c68493SMylène Josserand 	regmap_update_bits(scodec->regmap, SUN8I_SYS_SR_CTRL,
35136c68493SMylène Josserand 			   SUN8I_SYS_SR_CTRL_AIF2_FS_MASK,
35236c68493SMylène Josserand 			   sample_rate << SUN8I_SYS_SR_CTRL_AIF2_FS);
35336c68493SMylène Josserand 
35436c68493SMylène Josserand 	return 0;
35536c68493SMylène Josserand }
35636c68493SMylène Josserand 
357ca14da6eSMylène Josserand static const struct snd_kcontrol_new sun8i_dac_mixer_controls[] = {
358ca14da6eSMylène Josserand 	SOC_DAPM_DOUBLE("AIF1 Slot 0 Digital DAC Playback Switch",
359ca14da6eSMylène Josserand 			SUN8I_DAC_MXR_SRC,
360ca14da6eSMylène Josserand 			SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF1DA0L,
36136c68493SMylène Josserand 			SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF1DA0R, 1, 0),
362ca14da6eSMylène Josserand 	SOC_DAPM_DOUBLE("AIF1 Slot 1 Digital DAC Playback Switch",
363ca14da6eSMylène Josserand 			SUN8I_DAC_MXR_SRC,
364ca14da6eSMylène Josserand 			SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF1DA1L,
36536c68493SMylène Josserand 			SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF1DA1R, 1, 0),
366ca14da6eSMylène Josserand 	SOC_DAPM_DOUBLE("AIF2 Digital DAC Playback Switch", SUN8I_DAC_MXR_SRC,
367ca14da6eSMylène Josserand 			SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_AIF2DACL,
36836c68493SMylène Josserand 			SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_AIF2DACR, 1, 0),
369ca14da6eSMylène Josserand 	SOC_DAPM_DOUBLE("ADC Digital DAC Playback Switch", SUN8I_DAC_MXR_SRC,
370ca14da6eSMylène Josserand 			SUN8I_DAC_MXR_SRC_DACL_MXR_SRC_ADCL,
37136c68493SMylène Josserand 			SUN8I_DAC_MXR_SRC_DACR_MXR_SRC_ADCR, 1, 0),
37236c68493SMylène Josserand };
37336c68493SMylène Josserand 
374eda85d1fSMylene JOSSERAND static const struct snd_kcontrol_new sun8i_input_mixer_controls[] = {
375eda85d1fSMylene JOSSERAND 	SOC_DAPM_DOUBLE("AIF1 Slot 0 Digital ADC Capture Switch",
376eda85d1fSMylene JOSSERAND 			SUN8I_AIF1_MXR_SRC,
377eda85d1fSMylene JOSSERAND 			SUN8I_AIF1_MXR_SRC_AD0L_MXL_SRC_AIF1DA0L,
378eda85d1fSMylene JOSSERAND 			SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF1DA0R, 1, 0),
379eda85d1fSMylene JOSSERAND 	SOC_DAPM_DOUBLE("AIF2 Digital ADC Capture Switch", SUN8I_AIF1_MXR_SRC,
380eda85d1fSMylene JOSSERAND 			SUN8I_AIF1_MXR_SRC_AD0L_MXL_SRC_AIF2DACL,
381eda85d1fSMylene JOSSERAND 			SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF2DACR, 1, 0),
382eda85d1fSMylene JOSSERAND 	SOC_DAPM_DOUBLE("AIF1 Data Digital ADC Capture Switch",
383eda85d1fSMylene JOSSERAND 			SUN8I_AIF1_MXR_SRC,
384eda85d1fSMylene JOSSERAND 			SUN8I_AIF1_MXR_SRC_AD0L_MXL_SRC_ADCL,
385eda85d1fSMylene JOSSERAND 			SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_ADCR, 1, 0),
386eda85d1fSMylene JOSSERAND 	SOC_DAPM_DOUBLE("AIF2 Inv Digital ADC Capture Switch",
387eda85d1fSMylene JOSSERAND 			SUN8I_AIF1_MXR_SRC,
388eda85d1fSMylene JOSSERAND 			SUN8I_AIF1_MXR_SRC_AD0L_MXL_SRC_AIF2DACR,
389eda85d1fSMylene JOSSERAND 			SUN8I_AIF1_MXR_SRC_AD0R_MXR_SRC_AIF2DACL, 1, 0),
390eda85d1fSMylene JOSSERAND };
391eda85d1fSMylene JOSSERAND 
39236c68493SMylène Josserand static const struct snd_soc_dapm_widget sun8i_codec_dapm_widgets[] = {
393eda85d1fSMylene JOSSERAND 	/* Digital parts of the DACs and ADC */
39436c68493SMylène Josserand 	SND_SOC_DAPM_SUPPLY("DAC", SUN8I_DAC_DIG_CTRL, SUN8I_DAC_DIG_CTRL_ENDA,
39536c68493SMylène Josserand 			    0, NULL, 0),
396eda85d1fSMylene JOSSERAND 	SND_SOC_DAPM_SUPPLY("ADC", SUN8I_ADC_DIG_CTRL, SUN8I_ADC_DIG_CTRL_ENDA,
397eda85d1fSMylene JOSSERAND 			    0, NULL, 0),
39836c68493SMylène Josserand 
39990cac932SSamuel Holland 	/* AIF "DAC" Inputs */
40090cac932SSamuel Holland 	SND_SOC_DAPM_AIF_IN("AIF1 DA0L", "Playback", 0,
4019123aa86SMylène Josserand 			    SUN8I_AIF1_DACDAT_CTRL,
40236c68493SMylène Josserand 			    SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0L_ENA, 0),
40390cac932SSamuel Holland 	SND_SOC_DAPM_AIF_IN("AIF1 DA0R", "Playback", 0,
4049123aa86SMylène Josserand 			    SUN8I_AIF1_DACDAT_CTRL,
40536c68493SMylène Josserand 			    SUN8I_AIF1_DACDAT_CTRL_AIF1_DA0R_ENA, 0),
40636c68493SMylène Josserand 
40790cac932SSamuel Holland 	/* AIF "ADC" Outputs */
40890cac932SSamuel Holland 	SND_SOC_DAPM_AIF_IN("AIF1 AD0L", "Capture", 0,
409eda85d1fSMylene JOSSERAND 			    SUN8I_AIF1_ADCDAT_CTRL,
410eda85d1fSMylene JOSSERAND 			    SUN8I_AIF1_ADCDAT_CTRL_AIF1_DA0L_ENA, 0),
41190cac932SSamuel Holland 	SND_SOC_DAPM_AIF_IN("AIF1 AD0R", "Capture", 0,
412eda85d1fSMylene JOSSERAND 			    SUN8I_AIF1_ADCDAT_CTRL,
413eda85d1fSMylene JOSSERAND 			    SUN8I_AIF1_ADCDAT_CTRL_AIF1_DA0R_ENA, 0),
414eda85d1fSMylene JOSSERAND 
41590cac932SSamuel Holland 	/* ADC Inputs (connected to analog codec DAPM context) */
41690cac932SSamuel Holland 	SND_SOC_DAPM_ADC("ADCL", NULL, SND_SOC_NOPM, 0, 0),
41790cac932SSamuel Holland 	SND_SOC_DAPM_ADC("ADCR", NULL, SND_SOC_NOPM, 0, 0),
41890cac932SSamuel Holland 
41990cac932SSamuel Holland 	/* DAC Outputs (connected to analog codec DAPM context) */
42090cac932SSamuel Holland 	SND_SOC_DAPM_DAC("DACL", NULL, SND_SOC_NOPM, 0, 0),
42190cac932SSamuel Holland 	SND_SOC_DAPM_DAC("DACR", NULL, SND_SOC_NOPM, 0, 0),
42290cac932SSamuel Holland 
423eda85d1fSMylene JOSSERAND 	/* DAC and ADC Mixers */
424fa22ca4fSMylène Josserand 	SOC_MIXER_ARRAY("Left Digital DAC Mixer", SND_SOC_NOPM, 0, 0,
425fa22ca4fSMylène Josserand 			sun8i_dac_mixer_controls),
426fa22ca4fSMylène Josserand 	SOC_MIXER_ARRAY("Right Digital DAC Mixer", SND_SOC_NOPM, 0, 0,
427fa22ca4fSMylène Josserand 			sun8i_dac_mixer_controls),
428eda85d1fSMylene JOSSERAND 	SOC_MIXER_ARRAY("Left Digital ADC Mixer", SND_SOC_NOPM, 0, 0,
429eda85d1fSMylene JOSSERAND 			sun8i_input_mixer_controls),
430eda85d1fSMylene JOSSERAND 	SOC_MIXER_ARRAY("Right Digital ADC Mixer", SND_SOC_NOPM, 0, 0,
431eda85d1fSMylene JOSSERAND 			sun8i_input_mixer_controls),
43236c68493SMylène Josserand 
43336c68493SMylène Josserand 	/* Clocks */
43436c68493SMylène Josserand 	SND_SOC_DAPM_SUPPLY("MODCLK AFI1", SUN8I_MOD_CLK_ENA,
43536c68493SMylène Josserand 			    SUN8I_MOD_CLK_ENA_AIF1, 0, NULL, 0),
43636c68493SMylène Josserand 	SND_SOC_DAPM_SUPPLY("MODCLK DAC", SUN8I_MOD_CLK_ENA,
43736c68493SMylène Josserand 			    SUN8I_MOD_CLK_ENA_DAC, 0, NULL, 0),
438eda85d1fSMylene JOSSERAND 	SND_SOC_DAPM_SUPPLY("MODCLK ADC", SUN8I_MOD_CLK_ENA,
439eda85d1fSMylene JOSSERAND 			    SUN8I_MOD_CLK_ENA_ADC, 0, NULL, 0),
44036c68493SMylène Josserand 	SND_SOC_DAPM_SUPPLY("AIF1", SUN8I_SYSCLK_CTL,
44136c68493SMylène Josserand 			    SUN8I_SYSCLK_CTL_AIF1CLK_ENA, 0, NULL, 0),
44236c68493SMylène Josserand 	SND_SOC_DAPM_SUPPLY("SYSCLK", SUN8I_SYSCLK_CTL,
44336c68493SMylène Josserand 			    SUN8I_SYSCLK_CTL_SYSCLK_ENA, 0, NULL, 0),
44436c68493SMylène Josserand 
44536c68493SMylène Josserand 	SND_SOC_DAPM_SUPPLY("AIF1 PLL", SUN8I_SYSCLK_CTL,
44636c68493SMylène Josserand 			    SUN8I_SYSCLK_CTL_AIF1CLK_SRC_PLL, 0, NULL, 0),
44736c68493SMylène Josserand 	/* Inversion as 0=AIF1, 1=AIF2 */
44836c68493SMylène Josserand 	SND_SOC_DAPM_SUPPLY("SYSCLK AIF1", SUN8I_SYSCLK_CTL,
44936c68493SMylène Josserand 			    SUN8I_SYSCLK_CTL_SYSCLK_SRC, 1, NULL, 0),
45036c68493SMylène Josserand 
45136c68493SMylène Josserand 	/* Module reset */
45236c68493SMylène Josserand 	SND_SOC_DAPM_SUPPLY("RST AIF1", SUN8I_MOD_RST_CTL,
45336c68493SMylène Josserand 			    SUN8I_MOD_RST_CTL_AIF1, 0, NULL, 0),
45436c68493SMylène Josserand 	SND_SOC_DAPM_SUPPLY("RST DAC", SUN8I_MOD_RST_CTL,
45536c68493SMylène Josserand 			    SUN8I_MOD_RST_CTL_DAC, 0, NULL, 0),
456eda85d1fSMylene JOSSERAND 	SND_SOC_DAPM_SUPPLY("RST ADC", SUN8I_MOD_RST_CTL,
457eda85d1fSMylene JOSSERAND 			    SUN8I_MOD_RST_CTL_ADC, 0, NULL, 0),
458eda85d1fSMylene JOSSERAND 
459eda85d1fSMylene JOSSERAND 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
460eda85d1fSMylene JOSSERAND 	SND_SOC_DAPM_MIC("Mic", NULL),
461eda85d1fSMylene JOSSERAND 
46236c68493SMylène Josserand };
46336c68493SMylène Josserand 
46436c68493SMylène Josserand static const struct snd_soc_dapm_route sun8i_codec_dapm_routes[] = {
46536c68493SMylène Josserand 	/* Clock Routes */
46636c68493SMylène Josserand 	{ "AIF1", NULL, "SYSCLK AIF1" },
46736c68493SMylène Josserand 	{ "AIF1 PLL", NULL, "AIF1" },
46890cac932SSamuel Holland 	{ "SYSCLK", NULL, "AIF1 PLL" },
46990cac932SSamuel Holland 
47090cac932SSamuel Holland 	{ "RST AIF1", NULL, "SYSCLK" },
47136c68493SMylène Josserand 	{ "MODCLK AFI1", NULL, "RST AIF1" },
47290cac932SSamuel Holland 	{ "AIF1 AD0L", NULL, "MODCLK AFI1" },
47390cac932SSamuel Holland 	{ "AIF1 AD0R", NULL, "MODCLK AFI1" },
47490cac932SSamuel Holland 	{ "AIF1 DA0L", NULL, "MODCLK AFI1" },
47590cac932SSamuel Holland 	{ "AIF1 DA0R", NULL, "MODCLK AFI1" },
47636c68493SMylène Josserand 
47736c68493SMylène Josserand 	{ "RST DAC", NULL, "SYSCLK" },
47836c68493SMylène Josserand 	{ "MODCLK DAC", NULL, "RST DAC" },
47936c68493SMylène Josserand 	{ "DAC", NULL, "MODCLK DAC" },
48090cac932SSamuel Holland 	{ "DACL", NULL, "DAC" },
48190cac932SSamuel Holland 	{ "DACR", NULL, "DAC" },
48236c68493SMylène Josserand 
483eda85d1fSMylene JOSSERAND 	{ "RST ADC", NULL, "SYSCLK" },
484eda85d1fSMylene JOSSERAND 	{ "MODCLK ADC", NULL, "RST ADC" },
485eda85d1fSMylene JOSSERAND 	{ "ADC", NULL, "MODCLK ADC" },
48690cac932SSamuel Holland 	{ "ADCL", NULL, "ADC" },
48790cac932SSamuel Holland 	{ "ADCR", NULL, "ADC" },
488eda85d1fSMylene JOSSERAND 
48936c68493SMylène Josserand 	/* DAC Routes */
49090cac932SSamuel Holland 	{ "DACL", NULL, "Left Digital DAC Mixer" },
49190cac932SSamuel Holland 	{ "DACR", NULL, "Right Digital DAC Mixer" },
49236c68493SMylène Josserand 
49336c68493SMylène Josserand 	/* DAC Mixer Routes */
49490cac932SSamuel Holland 	{ "Left Digital DAC Mixer", "AIF1 Slot 0 Digital DAC Playback Switch", "AIF1 DA0L" },
495e47d2dcdSSamuel Holland 	{ "Left Digital DAC Mixer", "ADC Digital DAC Playback Switch", "ADCL" },
496e47d2dcdSSamuel Holland 
49790cac932SSamuel Holland 	{ "Right Digital DAC Mixer", "AIF1 Slot 0 Digital DAC Playback Switch", "AIF1 DA0R" },
498e47d2dcdSSamuel Holland 	{ "Right Digital DAC Mixer", "ADC Digital DAC Playback Switch", "ADCR" },
499eda85d1fSMylene JOSSERAND 
5009ee325d0SVasily Khoruzhick 	/* ADC Routes */
50190cac932SSamuel Holland 	{ "AIF1 AD0L", NULL, "Left Digital ADC Mixer" },
50290cac932SSamuel Holland 	{ "AIF1 AD0R", NULL, "Right Digital ADC Mixer" },
5039ee325d0SVasily Khoruzhick 
5049ee325d0SVasily Khoruzhick 	/* ADC Mixer Routes */
505e47d2dcdSSamuel Holland 	{ "Left Digital ADC Mixer", "AIF1 Slot 0 Digital ADC Capture Switch", "AIF1 DA0L" },
50690cac932SSamuel Holland 	{ "Left Digital ADC Mixer", "AIF1 Data Digital ADC Capture Switch", "ADCL" },
507e47d2dcdSSamuel Holland 
508e47d2dcdSSamuel Holland 	{ "Right Digital ADC Mixer", "AIF1 Slot 0 Digital ADC Capture Switch", "AIF1 DA0R" },
50990cac932SSamuel Holland 	{ "Right Digital ADC Mixer", "AIF1 Data Digital ADC Capture Switch", "ADCR" },
51036c68493SMylène Josserand };
51136c68493SMylène Josserand 
51290cac932SSamuel Holland static const struct snd_soc_dapm_widget sun8i_codec_legacy_widgets[] = {
51390cac932SSamuel Holland 	/* Legacy ADC Inputs (connected to analog codec DAPM context) */
51490cac932SSamuel Holland 	SND_SOC_DAPM_ADC("AIF1 Slot 0 Left ADC", NULL, SND_SOC_NOPM, 0, 0),
51590cac932SSamuel Holland 	SND_SOC_DAPM_ADC("AIF1 Slot 0 Right ADC", NULL, SND_SOC_NOPM, 0, 0),
51690cac932SSamuel Holland 
51790cac932SSamuel Holland 	/* Legacy DAC Outputs (connected to analog codec DAPM context) */
51890cac932SSamuel Holland 	SND_SOC_DAPM_DAC("AIF1 Slot 0 Left", NULL, SND_SOC_NOPM, 0, 0),
51990cac932SSamuel Holland 	SND_SOC_DAPM_DAC("AIF1 Slot 0 Right", NULL, SND_SOC_NOPM, 0, 0),
52090cac932SSamuel Holland };
52190cac932SSamuel Holland 
52290cac932SSamuel Holland static const struct snd_soc_dapm_route sun8i_codec_legacy_routes[] = {
52390cac932SSamuel Holland 	/* Legacy ADC Routes */
52490cac932SSamuel Holland 	{ "ADCL", NULL, "AIF1 Slot 0 Left ADC" },
52590cac932SSamuel Holland 	{ "ADCR", NULL, "AIF1 Slot 0 Right ADC" },
52690cac932SSamuel Holland 
52790cac932SSamuel Holland 	/* Legacy DAC Routes */
52890cac932SSamuel Holland 	{ "AIF1 Slot 0 Left", NULL, "DACL" },
52990cac932SSamuel Holland 	{ "AIF1 Slot 0 Right", NULL, "DACR" },
53090cac932SSamuel Holland };
53190cac932SSamuel Holland 
53290cac932SSamuel Holland static int sun8i_codec_component_probe(struct snd_soc_component *component)
53390cac932SSamuel Holland {
53490cac932SSamuel Holland 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
53590cac932SSamuel Holland 	struct sun8i_codec *scodec = snd_soc_component_get_drvdata(component);
53690cac932SSamuel Holland 	int ret;
53790cac932SSamuel Holland 
53890cac932SSamuel Holland 	/* Add widgets for backward compatibility with old device trees. */
53990cac932SSamuel Holland 	if (scodec->quirks->legacy_widgets) {
54090cac932SSamuel Holland 		ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_legacy_widgets,
54190cac932SSamuel Holland 						ARRAY_SIZE(sun8i_codec_legacy_widgets));
54290cac932SSamuel Holland 		if (ret)
54390cac932SSamuel Holland 			return ret;
54490cac932SSamuel Holland 
54590cac932SSamuel Holland 		ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_legacy_routes,
54690cac932SSamuel Holland 					      ARRAY_SIZE(sun8i_codec_legacy_routes));
54790cac932SSamuel Holland 		if (ret)
54890cac932SSamuel Holland 			return ret;
54990cac932SSamuel Holland 	}
55090cac932SSamuel Holland 
55190cac932SSamuel Holland 	return 0;
55290cac932SSamuel Holland }
55390cac932SSamuel Holland 
554fe49cd98SGustavo A. R. Silva static const struct snd_soc_dai_ops sun8i_codec_dai_ops = {
55536c68493SMylène Josserand 	.hw_params = sun8i_codec_hw_params,
55636c68493SMylène Josserand 	.set_fmt = sun8i_set_fmt,
55736c68493SMylène Josserand };
55836c68493SMylène Josserand 
55936c68493SMylène Josserand static struct snd_soc_dai_driver sun8i_codec_dai = {
56036c68493SMylène Josserand 	.name = "sun8i",
56136c68493SMylène Josserand 	/* playback capabilities */
56236c68493SMylène Josserand 	.playback = {
56336c68493SMylène Josserand 		.stream_name = "Playback",
56436c68493SMylène Josserand 		.channels_min = 1,
56536c68493SMylène Josserand 		.channels_max = 2,
56636c68493SMylène Josserand 		.rates = SNDRV_PCM_RATE_8000_192000,
56736c68493SMylène Josserand 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
56836c68493SMylène Josserand 	},
569eda85d1fSMylene JOSSERAND 	/* capture capabilities */
570eda85d1fSMylene JOSSERAND 	.capture = {
571eda85d1fSMylene JOSSERAND 		.stream_name = "Capture",
572eda85d1fSMylene JOSSERAND 		.channels_min = 1,
573eda85d1fSMylene JOSSERAND 		.channels_max = 2,
574eda85d1fSMylene JOSSERAND 		.rates = SNDRV_PCM_RATE_8000_192000,
575eda85d1fSMylene JOSSERAND 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
576eda85d1fSMylene JOSSERAND 		.sig_bits = 24,
577eda85d1fSMylene JOSSERAND 	},
57836c68493SMylène Josserand 	/* pcm operations */
57936c68493SMylène Josserand 	.ops = &sun8i_codec_dai_ops,
58036c68493SMylène Josserand };
58136c68493SMylène Josserand 
5827ec9b872SKuninori Morimoto static const struct snd_soc_component_driver sun8i_soc_component = {
58336c68493SMylène Josserand 	.dapm_widgets		= sun8i_codec_dapm_widgets,
58436c68493SMylène Josserand 	.num_dapm_widgets	= ARRAY_SIZE(sun8i_codec_dapm_widgets),
58536c68493SMylène Josserand 	.dapm_routes		= sun8i_codec_dapm_routes,
58636c68493SMylène Josserand 	.num_dapm_routes	= ARRAY_SIZE(sun8i_codec_dapm_routes),
587a2f6d303SSamuel Holland 	.probe			= sun8i_codec_component_probe,
5887ec9b872SKuninori Morimoto 	.idle_bias_on		= 1,
5897ec9b872SKuninori Morimoto 	.use_pmdown_time	= 1,
5907ec9b872SKuninori Morimoto 	.endianness		= 1,
5917ec9b872SKuninori Morimoto 	.non_legacy_dai_naming	= 1,
59236c68493SMylène Josserand };
59336c68493SMylène Josserand 
59436c68493SMylène Josserand static const struct regmap_config sun8i_codec_regmap_config = {
59536c68493SMylène Josserand 	.reg_bits	= 32,
59636c68493SMylène Josserand 	.reg_stride	= 4,
59736c68493SMylène Josserand 	.val_bits	= 32,
59836c68493SMylène Josserand 	.max_register	= SUN8I_DAC_MXR_SRC,
59936c68493SMylène Josserand 
60036c68493SMylène Josserand 	.cache_type	= REGCACHE_FLAT,
60136c68493SMylène Josserand };
60236c68493SMylène Josserand 
60336c68493SMylène Josserand static int sun8i_codec_probe(struct platform_device *pdev)
60436c68493SMylène Josserand {
60536c68493SMylène Josserand 	struct sun8i_codec *scodec;
60636c68493SMylène Josserand 	void __iomem *base;
60736c68493SMylène Josserand 	int ret;
60836c68493SMylène Josserand 
60936c68493SMylène Josserand 	scodec = devm_kzalloc(&pdev->dev, sizeof(*scodec), GFP_KERNEL);
61036c68493SMylène Josserand 	if (!scodec)
61136c68493SMylène Josserand 		return -ENOMEM;
61236c68493SMylène Josserand 
61336c68493SMylène Josserand 	scodec->clk_module = devm_clk_get(&pdev->dev, "mod");
61436c68493SMylène Josserand 	if (IS_ERR(scodec->clk_module)) {
61536c68493SMylène Josserand 		dev_err(&pdev->dev, "Failed to get the module clock\n");
61636c68493SMylène Josserand 		return PTR_ERR(scodec->clk_module);
61736c68493SMylène Josserand 	}
61836c68493SMylène Josserand 
61936c68493SMylène Josserand 	scodec->clk_bus = devm_clk_get(&pdev->dev, "bus");
62036c68493SMylène Josserand 	if (IS_ERR(scodec->clk_bus)) {
62136c68493SMylène Josserand 		dev_err(&pdev->dev, "Failed to get the bus clock\n");
62236c68493SMylène Josserand 		return PTR_ERR(scodec->clk_bus);
62336c68493SMylène Josserand 	}
62436c68493SMylène Josserand 
625790b3657SYueHaibing 	base = devm_platform_ioremap_resource(pdev, 0);
62636c68493SMylène Josserand 	if (IS_ERR(base)) {
62736c68493SMylène Josserand 		dev_err(&pdev->dev, "Failed to map the registers\n");
62836c68493SMylène Josserand 		return PTR_ERR(base);
62936c68493SMylène Josserand 	}
63036c68493SMylène Josserand 
63136c68493SMylène Josserand 	scodec->regmap = devm_regmap_init_mmio(&pdev->dev, base,
63236c68493SMylène Josserand 					       &sun8i_codec_regmap_config);
63336c68493SMylène Josserand 	if (IS_ERR(scodec->regmap)) {
63436c68493SMylène Josserand 		dev_err(&pdev->dev, "Failed to create our regmap\n");
63536c68493SMylène Josserand 		return PTR_ERR(scodec->regmap);
63636c68493SMylène Josserand 	}
63736c68493SMylène Josserand 
63890cac932SSamuel Holland 	scodec->quirks = of_device_get_match_data(&pdev->dev);
63990cac932SSamuel Holland 
64036c68493SMylène Josserand 	platform_set_drvdata(pdev, scodec);
64136c68493SMylène Josserand 
64236c68493SMylène Josserand 	pm_runtime_enable(&pdev->dev);
64336c68493SMylène Josserand 	if (!pm_runtime_enabled(&pdev->dev)) {
64436c68493SMylène Josserand 		ret = sun8i_codec_runtime_resume(&pdev->dev);
64536c68493SMylène Josserand 		if (ret)
64636c68493SMylène Josserand 			goto err_pm_disable;
64736c68493SMylène Josserand 	}
64836c68493SMylène Josserand 
6497ec9b872SKuninori Morimoto 	ret = devm_snd_soc_register_component(&pdev->dev, &sun8i_soc_component,
65036c68493SMylène Josserand 				     &sun8i_codec_dai, 1);
65136c68493SMylène Josserand 	if (ret) {
65236c68493SMylène Josserand 		dev_err(&pdev->dev, "Failed to register codec\n");
65336c68493SMylène Josserand 		goto err_suspend;
65436c68493SMylène Josserand 	}
65536c68493SMylène Josserand 
65636c68493SMylène Josserand 	return ret;
65736c68493SMylène Josserand 
65836c68493SMylène Josserand err_suspend:
65936c68493SMylène Josserand 	if (!pm_runtime_status_suspended(&pdev->dev))
66036c68493SMylène Josserand 		sun8i_codec_runtime_suspend(&pdev->dev);
66136c68493SMylène Josserand 
66236c68493SMylène Josserand err_pm_disable:
66336c68493SMylène Josserand 	pm_runtime_disable(&pdev->dev);
66436c68493SMylène Josserand 
66536c68493SMylène Josserand 	return ret;
66636c68493SMylène Josserand }
66736c68493SMylène Josserand 
66836c68493SMylène Josserand static int sun8i_codec_remove(struct platform_device *pdev)
66936c68493SMylène Josserand {
67036c68493SMylène Josserand 	pm_runtime_disable(&pdev->dev);
67136c68493SMylène Josserand 	if (!pm_runtime_status_suspended(&pdev->dev))
67236c68493SMylène Josserand 		sun8i_codec_runtime_suspend(&pdev->dev);
67336c68493SMylène Josserand 
67436c68493SMylène Josserand 	return 0;
67536c68493SMylène Josserand }
67636c68493SMylène Josserand 
67790cac932SSamuel Holland static const struct sun8i_codec_quirks sun8i_a33_quirks = {
67890cac932SSamuel Holland 	.legacy_widgets	= true,
6797518805fSSamuel Holland 	.lrck_inversion	= true,
68090cac932SSamuel Holland };
68190cac932SSamuel Holland 
68290cac932SSamuel Holland static const struct sun8i_codec_quirks sun50i_a64_quirks = {
68390cac932SSamuel Holland };
68490cac932SSamuel Holland 
68536c68493SMylène Josserand static const struct of_device_id sun8i_codec_of_match[] = {
68690cac932SSamuel Holland 	{ .compatible = "allwinner,sun8i-a33-codec", .data = &sun8i_a33_quirks },
68790cac932SSamuel Holland 	{ .compatible = "allwinner,sun50i-a64-codec", .data = &sun50i_a64_quirks },
68836c68493SMylène Josserand 	{}
68936c68493SMylène Josserand };
69036c68493SMylène Josserand MODULE_DEVICE_TABLE(of, sun8i_codec_of_match);
69136c68493SMylène Josserand 
69236c68493SMylène Josserand static const struct dev_pm_ops sun8i_codec_pm_ops = {
69336c68493SMylène Josserand 	SET_RUNTIME_PM_OPS(sun8i_codec_runtime_suspend,
69436c68493SMylène Josserand 			   sun8i_codec_runtime_resume, NULL)
69536c68493SMylène Josserand };
69636c68493SMylène Josserand 
69736c68493SMylène Josserand static struct platform_driver sun8i_codec_driver = {
69836c68493SMylène Josserand 	.driver = {
69936c68493SMylène Josserand 		.name = "sun8i-codec",
70036c68493SMylène Josserand 		.of_match_table = sun8i_codec_of_match,
70136c68493SMylène Josserand 		.pm = &sun8i_codec_pm_ops,
70236c68493SMylène Josserand 	},
70336c68493SMylène Josserand 	.probe = sun8i_codec_probe,
70436c68493SMylène Josserand 	.remove = sun8i_codec_remove,
70536c68493SMylène Josserand };
70636c68493SMylène Josserand module_platform_driver(sun8i_codec_driver);
70736c68493SMylène Josserand 
70836c68493SMylène Josserand MODULE_DESCRIPTION("Allwinner A33 (sun8i) codec driver");
70936c68493SMylène Josserand MODULE_AUTHOR("Mylène Josserand <mylene.josserand@free-electrons.com>");
71036c68493SMylène Josserand MODULE_LICENSE("GPL");
71136c68493SMylène Josserand MODULE_ALIAS("platform:sun8i-codec");
712