18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * AD193X Audio Codec driver supporting AD1936/7/8/9
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2010 Analog Devices Inc.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/device.h>
118c2ecf20Sopenharmony_ci#include <linux/regmap.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci#include <sound/core.h>
148c2ecf20Sopenharmony_ci#include <sound/pcm.h>
158c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
168c2ecf20Sopenharmony_ci#include <sound/initval.h>
178c2ecf20Sopenharmony_ci#include <sound/soc.h>
188c2ecf20Sopenharmony_ci#include <sound/tlv.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include "ad193x.h"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/* codec private data */
238c2ecf20Sopenharmony_cistruct ad193x_priv {
248c2ecf20Sopenharmony_ci	struct regmap *regmap;
258c2ecf20Sopenharmony_ci	enum ad193x_type type;
268c2ecf20Sopenharmony_ci	int sysclk;
278c2ecf20Sopenharmony_ci};
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/*
308c2ecf20Sopenharmony_ci * AD193X volume/mute/de-emphasis etc. controls
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_cistatic const char * const ad193x_deemp[] = {"None", "48kHz", "44.1kHz", "32kHz"};
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(ad193x_deemp_enum, AD193X_DAC_CTRL2, 1,
358c2ecf20Sopenharmony_ci			    ad193x_deemp);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_MINMAX(adau193x_tlv, -9563, 0);
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic const unsigned int ad193x_sb[] = {32};
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic struct snd_pcm_hw_constraint_list constr = {
428c2ecf20Sopenharmony_ci	.list = ad193x_sb,
438c2ecf20Sopenharmony_ci	.count = ARRAY_SIZE(ad193x_sb),
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new ad193x_snd_controls[] = {
478c2ecf20Sopenharmony_ci	/* DAC volume control */
488c2ecf20Sopenharmony_ci	SOC_DOUBLE_R_TLV("DAC1 Volume", AD193X_DAC_L1_VOL,
498c2ecf20Sopenharmony_ci			AD193X_DAC_R1_VOL, 0, 0xFF, 1, adau193x_tlv),
508c2ecf20Sopenharmony_ci	SOC_DOUBLE_R_TLV("DAC2 Volume", AD193X_DAC_L2_VOL,
518c2ecf20Sopenharmony_ci			AD193X_DAC_R2_VOL, 0, 0xFF, 1, adau193x_tlv),
528c2ecf20Sopenharmony_ci	SOC_DOUBLE_R_TLV("DAC3 Volume", AD193X_DAC_L3_VOL,
538c2ecf20Sopenharmony_ci			AD193X_DAC_R3_VOL, 0, 0xFF, 1, adau193x_tlv),
548c2ecf20Sopenharmony_ci	SOC_DOUBLE_R_TLV("DAC4 Volume", AD193X_DAC_L4_VOL,
558c2ecf20Sopenharmony_ci			AD193X_DAC_R4_VOL, 0, 0xFF, 1, adau193x_tlv),
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	/* DAC switch control */
588c2ecf20Sopenharmony_ci	SOC_DOUBLE("DAC1 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL1_MUTE,
598c2ecf20Sopenharmony_ci		AD193X_DACR1_MUTE, 1, 1),
608c2ecf20Sopenharmony_ci	SOC_DOUBLE("DAC2 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL2_MUTE,
618c2ecf20Sopenharmony_ci		AD193X_DACR2_MUTE, 1, 1),
628c2ecf20Sopenharmony_ci	SOC_DOUBLE("DAC3 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL3_MUTE,
638c2ecf20Sopenharmony_ci		AD193X_DACR3_MUTE, 1, 1),
648c2ecf20Sopenharmony_ci	SOC_DOUBLE("DAC4 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL4_MUTE,
658c2ecf20Sopenharmony_ci		AD193X_DACR4_MUTE, 1, 1),
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	/* DAC de-emphasis */
688c2ecf20Sopenharmony_ci	SOC_ENUM("Playback Deemphasis", ad193x_deemp_enum),
698c2ecf20Sopenharmony_ci};
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new ad193x_adc_snd_controls[] = {
728c2ecf20Sopenharmony_ci	/* ADC switch control */
738c2ecf20Sopenharmony_ci	SOC_DOUBLE("ADC1 Switch", AD193X_ADC_CTRL0, AD193X_ADCL1_MUTE,
748c2ecf20Sopenharmony_ci		AD193X_ADCR1_MUTE, 1, 1),
758c2ecf20Sopenharmony_ci	SOC_DOUBLE("ADC2 Switch", AD193X_ADC_CTRL0, AD193X_ADCL2_MUTE,
768c2ecf20Sopenharmony_ci		AD193X_ADCR2_MUTE, 1, 1),
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	/* ADC high-pass filter */
798c2ecf20Sopenharmony_ci	SOC_SINGLE("ADC High Pass Filter Switch", AD193X_ADC_CTRL0,
808c2ecf20Sopenharmony_ci			AD193X_ADC_HIGHPASS_FILTER, 1, 0),
818c2ecf20Sopenharmony_ci};
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_widget ad193x_dapm_widgets[] = {
848c2ecf20Sopenharmony_ci	SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
858c2ecf20Sopenharmony_ci	SND_SOC_DAPM_PGA("DAC Output", AD193X_DAC_CTRL0, 0, 1, NULL, 0),
868c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("PLL_PWR", AD193X_PLL_CLK_CTRL0, 0, 1, NULL, 0),
878c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("SYSCLK", AD193X_PLL_CLK_CTRL0, 7, 0, NULL, 0),
888c2ecf20Sopenharmony_ci	SND_SOC_DAPM_VMID("VMID"),
898c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("DAC1OUT"),
908c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("DAC2OUT"),
918c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("DAC3OUT"),
928c2ecf20Sopenharmony_ci	SND_SOC_DAPM_OUTPUT("DAC4OUT"),
938c2ecf20Sopenharmony_ci};
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_widget ad193x_adc_widgets[] = {
968c2ecf20Sopenharmony_ci	SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
978c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SUPPLY("ADC_PWR", AD193X_ADC_CTRL0, 0, 1, NULL, 0),
988c2ecf20Sopenharmony_ci	SND_SOC_DAPM_INPUT("ADC1IN"),
998c2ecf20Sopenharmony_ci	SND_SOC_DAPM_INPUT("ADC2IN"),
1008c2ecf20Sopenharmony_ci};
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic int ad193x_check_pll(struct snd_soc_dapm_widget *source,
1038c2ecf20Sopenharmony_ci			    struct snd_soc_dapm_widget *sink)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	struct snd_soc_component *component = snd_soc_dapm_to_component(source->dapm);
1068c2ecf20Sopenharmony_ci	struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	return !!ad193x->sysclk;
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_route audio_paths[] = {
1128c2ecf20Sopenharmony_ci	{ "DAC", NULL, "SYSCLK" },
1138c2ecf20Sopenharmony_ci	{ "DAC Output", NULL, "DAC" },
1148c2ecf20Sopenharmony_ci	{ "DAC Output", NULL, "VMID" },
1158c2ecf20Sopenharmony_ci	{ "DAC1OUT", NULL, "DAC Output" },
1168c2ecf20Sopenharmony_ci	{ "DAC2OUT", NULL, "DAC Output" },
1178c2ecf20Sopenharmony_ci	{ "DAC3OUT", NULL, "DAC Output" },
1188c2ecf20Sopenharmony_ci	{ "DAC4OUT", NULL, "DAC Output" },
1198c2ecf20Sopenharmony_ci	{ "SYSCLK", NULL, "PLL_PWR", &ad193x_check_pll },
1208c2ecf20Sopenharmony_ci};
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_route ad193x_adc_audio_paths[] = {
1238c2ecf20Sopenharmony_ci	{ "ADC", NULL, "SYSCLK" },
1248c2ecf20Sopenharmony_ci	{ "ADC", NULL, "ADC_PWR" },
1258c2ecf20Sopenharmony_ci	{ "ADC", NULL, "ADC1IN" },
1268c2ecf20Sopenharmony_ci	{ "ADC", NULL, "ADC2IN" },
1278c2ecf20Sopenharmony_ci};
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic inline bool ad193x_has_adc(const struct ad193x_priv *ad193x)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	switch (ad193x->type) {
1328c2ecf20Sopenharmony_ci	case AD1933:
1338c2ecf20Sopenharmony_ci	case AD1934:
1348c2ecf20Sopenharmony_ci		return false;
1358c2ecf20Sopenharmony_ci	default:
1368c2ecf20Sopenharmony_ci		break;
1378c2ecf20Sopenharmony_ci	}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	return true;
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci/*
1438c2ecf20Sopenharmony_ci * DAI ops entries
1448c2ecf20Sopenharmony_ci */
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic int ad193x_mute(struct snd_soc_dai *dai, int mute, int direction)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(dai->component);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	if (mute)
1518c2ecf20Sopenharmony_ci		regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2,
1528c2ecf20Sopenharmony_ci				    AD193X_DAC_MASTER_MUTE,
1538c2ecf20Sopenharmony_ci				    AD193X_DAC_MASTER_MUTE);
1548c2ecf20Sopenharmony_ci	else
1558c2ecf20Sopenharmony_ci		regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2,
1568c2ecf20Sopenharmony_ci				    AD193X_DAC_MASTER_MUTE, 0);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	return 0;
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic int ad193x_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
1628c2ecf20Sopenharmony_ci			       unsigned int rx_mask, int slots, int width)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(dai->component);
1658c2ecf20Sopenharmony_ci	unsigned int channels;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	switch (slots) {
1688c2ecf20Sopenharmony_ci	case 2:
1698c2ecf20Sopenharmony_ci		channels = AD193X_2_CHANNELS;
1708c2ecf20Sopenharmony_ci		break;
1718c2ecf20Sopenharmony_ci	case 4:
1728c2ecf20Sopenharmony_ci		channels = AD193X_4_CHANNELS;
1738c2ecf20Sopenharmony_ci		break;
1748c2ecf20Sopenharmony_ci	case 8:
1758c2ecf20Sopenharmony_ci		channels = AD193X_8_CHANNELS;
1768c2ecf20Sopenharmony_ci		break;
1778c2ecf20Sopenharmony_ci	case 16:
1788c2ecf20Sopenharmony_ci		channels = AD193X_16_CHANNELS;
1798c2ecf20Sopenharmony_ci		break;
1808c2ecf20Sopenharmony_ci	default:
1818c2ecf20Sopenharmony_ci		return -EINVAL;
1828c2ecf20Sopenharmony_ci	}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL1,
1858c2ecf20Sopenharmony_ci		AD193X_DAC_CHAN_MASK, channels << AD193X_DAC_CHAN_SHFT);
1868c2ecf20Sopenharmony_ci	if (ad193x_has_adc(ad193x))
1878c2ecf20Sopenharmony_ci		regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL2,
1888c2ecf20Sopenharmony_ci				   AD193X_ADC_CHAN_MASK,
1898c2ecf20Sopenharmony_ci				   channels << AD193X_ADC_CHAN_SHFT);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	return 0;
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistatic int ad193x_set_dai_fmt(struct snd_soc_dai *codec_dai,
1958c2ecf20Sopenharmony_ci		unsigned int fmt)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(codec_dai->component);
1988c2ecf20Sopenharmony_ci	unsigned int adc_serfmt = 0;
1998c2ecf20Sopenharmony_ci	unsigned int dac_serfmt = 0;
2008c2ecf20Sopenharmony_ci	unsigned int adc_fmt = 0;
2018c2ecf20Sopenharmony_ci	unsigned int dac_fmt = 0;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	/* At present, the driver only support AUX ADC mode(SND_SOC_DAIFMT_I2S
2048c2ecf20Sopenharmony_ci	 * with TDM), ADC&DAC TDM mode(SND_SOC_DAIFMT_DSP_A) and DAC I2S mode
2058c2ecf20Sopenharmony_ci	 * (SND_SOC_DAIFMT_I2S)
2068c2ecf20Sopenharmony_ci	 */
2078c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2088c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_I2S:
2098c2ecf20Sopenharmony_ci		adc_serfmt |= AD193X_ADC_SERFMT_TDM;
2108c2ecf20Sopenharmony_ci		dac_serfmt |= AD193X_DAC_SERFMT_STEREO;
2118c2ecf20Sopenharmony_ci		break;
2128c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_DSP_A:
2138c2ecf20Sopenharmony_ci		adc_serfmt |= AD193X_ADC_SERFMT_AUX;
2148c2ecf20Sopenharmony_ci		dac_serfmt |= AD193X_DAC_SERFMT_TDM;
2158c2ecf20Sopenharmony_ci		break;
2168c2ecf20Sopenharmony_ci	default:
2178c2ecf20Sopenharmony_ci		if (ad193x_has_adc(ad193x))
2188c2ecf20Sopenharmony_ci			return -EINVAL;
2198c2ecf20Sopenharmony_ci	}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2228c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_NB_NF: /* normal bit clock + frame */
2238c2ecf20Sopenharmony_ci		break;
2248c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_NB_IF: /* normal bclk + invert frm */
2258c2ecf20Sopenharmony_ci		adc_fmt |= AD193X_ADC_LEFT_HIGH;
2268c2ecf20Sopenharmony_ci		dac_fmt |= AD193X_DAC_LEFT_HIGH;
2278c2ecf20Sopenharmony_ci		break;
2288c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_IB_NF: /* invert bclk + normal frm */
2298c2ecf20Sopenharmony_ci		adc_fmt |= AD193X_ADC_BCLK_INV;
2308c2ecf20Sopenharmony_ci		dac_fmt |= AD193X_DAC_BCLK_INV;
2318c2ecf20Sopenharmony_ci		break;
2328c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_IB_IF: /* invert bclk + frm */
2338c2ecf20Sopenharmony_ci		adc_fmt |= AD193X_ADC_LEFT_HIGH;
2348c2ecf20Sopenharmony_ci		adc_fmt |= AD193X_ADC_BCLK_INV;
2358c2ecf20Sopenharmony_ci		dac_fmt |= AD193X_DAC_LEFT_HIGH;
2368c2ecf20Sopenharmony_ci		dac_fmt |= AD193X_DAC_BCLK_INV;
2378c2ecf20Sopenharmony_ci		break;
2388c2ecf20Sopenharmony_ci	default:
2398c2ecf20Sopenharmony_ci		return -EINVAL;
2408c2ecf20Sopenharmony_ci	}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	/* For DSP_*, LRCLK's polarity must be inverted */
2438c2ecf20Sopenharmony_ci	if (fmt & SND_SOC_DAIFMT_DSP_A)
2448c2ecf20Sopenharmony_ci		dac_fmt ^= AD193X_DAC_LEFT_HIGH;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
2478c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBM_CFM: /* codec clk & frm master */
2488c2ecf20Sopenharmony_ci		adc_fmt |= AD193X_ADC_LCR_MASTER;
2498c2ecf20Sopenharmony_ci		adc_fmt |= AD193X_ADC_BCLK_MASTER;
2508c2ecf20Sopenharmony_ci		dac_fmt |= AD193X_DAC_LCR_MASTER;
2518c2ecf20Sopenharmony_ci		dac_fmt |= AD193X_DAC_BCLK_MASTER;
2528c2ecf20Sopenharmony_ci		break;
2538c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBS_CFM: /* codec clk slave & frm master */
2548c2ecf20Sopenharmony_ci		adc_fmt |= AD193X_ADC_LCR_MASTER;
2558c2ecf20Sopenharmony_ci		dac_fmt |= AD193X_DAC_LCR_MASTER;
2568c2ecf20Sopenharmony_ci		break;
2578c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBM_CFS: /* codec clk master & frame slave */
2588c2ecf20Sopenharmony_ci		adc_fmt |= AD193X_ADC_BCLK_MASTER;
2598c2ecf20Sopenharmony_ci		dac_fmt |= AD193X_DAC_BCLK_MASTER;
2608c2ecf20Sopenharmony_ci		break;
2618c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBS_CFS: /* codec clk & frm slave */
2628c2ecf20Sopenharmony_ci		break;
2638c2ecf20Sopenharmony_ci	default:
2648c2ecf20Sopenharmony_ci		return -EINVAL;
2658c2ecf20Sopenharmony_ci	}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	if (ad193x_has_adc(ad193x)) {
2688c2ecf20Sopenharmony_ci		regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL1,
2698c2ecf20Sopenharmony_ci				   AD193X_ADC_SERFMT_MASK, adc_serfmt);
2708c2ecf20Sopenharmony_ci		regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL2,
2718c2ecf20Sopenharmony_ci				   AD193X_ADC_FMT_MASK, adc_fmt);
2728c2ecf20Sopenharmony_ci	}
2738c2ecf20Sopenharmony_ci	regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL0,
2748c2ecf20Sopenharmony_ci			   AD193X_DAC_SERFMT_MASK, dac_serfmt);
2758c2ecf20Sopenharmony_ci	regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL1,
2768c2ecf20Sopenharmony_ci		AD193X_DAC_FMT_MASK, dac_fmt);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	return 0;
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_cistatic int ad193x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
2828c2ecf20Sopenharmony_ci		int clk_id, unsigned int freq, int dir)
2838c2ecf20Sopenharmony_ci{
2848c2ecf20Sopenharmony_ci	struct snd_soc_component *component = codec_dai->component;
2858c2ecf20Sopenharmony_ci	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
2868c2ecf20Sopenharmony_ci	struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component);
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	if (clk_id == AD193X_SYSCLK_MCLK) {
2898c2ecf20Sopenharmony_ci		/* MCLK must be 512 x fs */
2908c2ecf20Sopenharmony_ci		if (dir == SND_SOC_CLOCK_OUT || freq != 24576000)
2918c2ecf20Sopenharmony_ci			return -EINVAL;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci		regmap_update_bits(ad193x->regmap, AD193X_PLL_CLK_CTRL1,
2948c2ecf20Sopenharmony_ci				   AD193X_PLL_SRC_MASK,
2958c2ecf20Sopenharmony_ci				   AD193X_PLL_DAC_SRC_MCLK |
2968c2ecf20Sopenharmony_ci				   AD193X_PLL_CLK_SRC_MCLK);
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci		snd_soc_dapm_sync(dapm);
2998c2ecf20Sopenharmony_ci		return 0;
3008c2ecf20Sopenharmony_ci	}
3018c2ecf20Sopenharmony_ci	switch (freq) {
3028c2ecf20Sopenharmony_ci	case 12288000:
3038c2ecf20Sopenharmony_ci	case 18432000:
3048c2ecf20Sopenharmony_ci	case 24576000:
3058c2ecf20Sopenharmony_ci	case 36864000:
3068c2ecf20Sopenharmony_ci		ad193x->sysclk = freq;
3078c2ecf20Sopenharmony_ci		return 0;
3088c2ecf20Sopenharmony_ci	}
3098c2ecf20Sopenharmony_ci	return -EINVAL;
3108c2ecf20Sopenharmony_ci}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_cistatic int ad193x_hw_params(struct snd_pcm_substream *substream,
3138c2ecf20Sopenharmony_ci		struct snd_pcm_hw_params *params,
3148c2ecf20Sopenharmony_ci		struct snd_soc_dai *dai)
3158c2ecf20Sopenharmony_ci{
3168c2ecf20Sopenharmony_ci	int word_len = 0, master_rate = 0;
3178c2ecf20Sopenharmony_ci	struct snd_soc_component *component = dai->component;
3188c2ecf20Sopenharmony_ci	struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	/* bit size */
3218c2ecf20Sopenharmony_ci	switch (params_width(params)) {
3228c2ecf20Sopenharmony_ci	case 16:
3238c2ecf20Sopenharmony_ci		word_len = 3;
3248c2ecf20Sopenharmony_ci		break;
3258c2ecf20Sopenharmony_ci	case 20:
3268c2ecf20Sopenharmony_ci		word_len = 1;
3278c2ecf20Sopenharmony_ci		break;
3288c2ecf20Sopenharmony_ci	case 24:
3298c2ecf20Sopenharmony_ci	case 32:
3308c2ecf20Sopenharmony_ci		word_len = 0;
3318c2ecf20Sopenharmony_ci		break;
3328c2ecf20Sopenharmony_ci	}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	switch (ad193x->sysclk) {
3358c2ecf20Sopenharmony_ci	case 12288000:
3368c2ecf20Sopenharmony_ci		master_rate = AD193X_PLL_INPUT_256;
3378c2ecf20Sopenharmony_ci		break;
3388c2ecf20Sopenharmony_ci	case 18432000:
3398c2ecf20Sopenharmony_ci		master_rate = AD193X_PLL_INPUT_384;
3408c2ecf20Sopenharmony_ci		break;
3418c2ecf20Sopenharmony_ci	case 24576000:
3428c2ecf20Sopenharmony_ci		master_rate = AD193X_PLL_INPUT_512;
3438c2ecf20Sopenharmony_ci		break;
3448c2ecf20Sopenharmony_ci	case 36864000:
3458c2ecf20Sopenharmony_ci		master_rate = AD193X_PLL_INPUT_768;
3468c2ecf20Sopenharmony_ci		break;
3478c2ecf20Sopenharmony_ci	}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	regmap_update_bits(ad193x->regmap, AD193X_PLL_CLK_CTRL0,
3508c2ecf20Sopenharmony_ci			    AD193X_PLL_INPUT_MASK, master_rate);
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2,
3538c2ecf20Sopenharmony_ci			    AD193X_DAC_WORD_LEN_MASK,
3548c2ecf20Sopenharmony_ci			    word_len << AD193X_DAC_WORD_LEN_SHFT);
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	if (ad193x_has_adc(ad193x))
3578c2ecf20Sopenharmony_ci		regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL1,
3588c2ecf20Sopenharmony_ci				   AD193X_ADC_WORD_LEN_MASK, word_len);
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	return 0;
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic int ad193x_startup(struct snd_pcm_substream *substream,
3648c2ecf20Sopenharmony_ci			  struct snd_soc_dai *dai)
3658c2ecf20Sopenharmony_ci{
3668c2ecf20Sopenharmony_ci	return snd_pcm_hw_constraint_list(substream->runtime, 0,
3678c2ecf20Sopenharmony_ci				   SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
3688c2ecf20Sopenharmony_ci				   &constr);
3698c2ecf20Sopenharmony_ci}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_cistatic const struct snd_soc_dai_ops ad193x_dai_ops = {
3728c2ecf20Sopenharmony_ci	.startup = ad193x_startup,
3738c2ecf20Sopenharmony_ci	.hw_params = ad193x_hw_params,
3748c2ecf20Sopenharmony_ci	.mute_stream = ad193x_mute,
3758c2ecf20Sopenharmony_ci	.set_tdm_slot = ad193x_set_tdm_slot,
3768c2ecf20Sopenharmony_ci	.set_sysclk	= ad193x_set_dai_sysclk,
3778c2ecf20Sopenharmony_ci	.set_fmt = ad193x_set_dai_fmt,
3788c2ecf20Sopenharmony_ci	.no_capture_mute = 1,
3798c2ecf20Sopenharmony_ci};
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci/* codec DAI instance */
3828c2ecf20Sopenharmony_cistatic struct snd_soc_dai_driver ad193x_dai = {
3838c2ecf20Sopenharmony_ci	.name = "ad193x-hifi",
3848c2ecf20Sopenharmony_ci	.playback = {
3858c2ecf20Sopenharmony_ci		.stream_name = "Playback",
3868c2ecf20Sopenharmony_ci		.channels_min = 2,
3878c2ecf20Sopenharmony_ci		.channels_max = 8,
3888c2ecf20Sopenharmony_ci		.rates = SNDRV_PCM_RATE_48000,
3898c2ecf20Sopenharmony_ci		.formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
3908c2ecf20Sopenharmony_ci			SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
3918c2ecf20Sopenharmony_ci	},
3928c2ecf20Sopenharmony_ci	.capture = {
3938c2ecf20Sopenharmony_ci		.stream_name = "Capture",
3948c2ecf20Sopenharmony_ci		.channels_min = 2,
3958c2ecf20Sopenharmony_ci		.channels_max = 4,
3968c2ecf20Sopenharmony_ci		.rates = SNDRV_PCM_RATE_48000,
3978c2ecf20Sopenharmony_ci		.formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
3988c2ecf20Sopenharmony_ci			SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
3998c2ecf20Sopenharmony_ci	},
4008c2ecf20Sopenharmony_ci	.ops = &ad193x_dai_ops,
4018c2ecf20Sopenharmony_ci};
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci/* codec DAI instance for DAC only */
4048c2ecf20Sopenharmony_cistatic struct snd_soc_dai_driver ad193x_no_adc_dai = {
4058c2ecf20Sopenharmony_ci	.name = "ad193x-hifi",
4068c2ecf20Sopenharmony_ci	.playback = {
4078c2ecf20Sopenharmony_ci		.stream_name = "Playback",
4088c2ecf20Sopenharmony_ci		.channels_min = 2,
4098c2ecf20Sopenharmony_ci		.channels_max = 8,
4108c2ecf20Sopenharmony_ci		.rates = SNDRV_PCM_RATE_48000,
4118c2ecf20Sopenharmony_ci		.formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
4128c2ecf20Sopenharmony_ci			SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
4138c2ecf20Sopenharmony_ci	},
4148c2ecf20Sopenharmony_ci	.ops = &ad193x_dai_ops,
4158c2ecf20Sopenharmony_ci};
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci/* codec register values to set after reset */
4188c2ecf20Sopenharmony_cistatic void ad193x_reg_default_init(struct ad193x_priv *ad193x)
4198c2ecf20Sopenharmony_ci{
4208c2ecf20Sopenharmony_ci	static const struct reg_sequence reg_init[] = {
4218c2ecf20Sopenharmony_ci		{  0, 0x99 },	/* PLL_CLK_CTRL0: pll input: mclki/xi 12.288Mhz */
4228c2ecf20Sopenharmony_ci		{  1, 0x04 },	/* PLL_CLK_CTRL1: no on-chip Vref */
4238c2ecf20Sopenharmony_ci		{  2, 0x40 },	/* DAC_CTRL0: TDM mode */
4248c2ecf20Sopenharmony_ci		{  3, 0x00 },	/* DAC_CTRL1: reset */
4258c2ecf20Sopenharmony_ci		{  4, 0x1A },	/* DAC_CTRL2: 48kHz de-emphasis, unmute dac */
4268c2ecf20Sopenharmony_ci		{  5, 0x00 },	/* DAC_CHNL_MUTE: unmute DAC channels */
4278c2ecf20Sopenharmony_ci		{  6, 0x00 },	/* DAC_L1_VOL: no attenuation */
4288c2ecf20Sopenharmony_ci		{  7, 0x00 },	/* DAC_R1_VOL: no attenuation */
4298c2ecf20Sopenharmony_ci		{  8, 0x00 },	/* DAC_L2_VOL: no attenuation */
4308c2ecf20Sopenharmony_ci		{  9, 0x00 },	/* DAC_R2_VOL: no attenuation */
4318c2ecf20Sopenharmony_ci		{ 10, 0x00 },	/* DAC_L3_VOL: no attenuation */
4328c2ecf20Sopenharmony_ci		{ 11, 0x00 },	/* DAC_R3_VOL: no attenuation */
4338c2ecf20Sopenharmony_ci		{ 12, 0x00 },	/* DAC_L4_VOL: no attenuation */
4348c2ecf20Sopenharmony_ci		{ 13, 0x00 },	/* DAC_R4_VOL: no attenuation */
4358c2ecf20Sopenharmony_ci	};
4368c2ecf20Sopenharmony_ci	static const struct reg_sequence reg_adc_init[] = {
4378c2ecf20Sopenharmony_ci		{ 14, 0x03 },	/* ADC_CTRL0: high-pass filter enable */
4388c2ecf20Sopenharmony_ci		{ 15, 0x43 },	/* ADC_CTRL1: sata delay=1, adc aux mode */
4398c2ecf20Sopenharmony_ci		{ 16, 0x00 },	/* ADC_CTRL2: reset */
4408c2ecf20Sopenharmony_ci	};
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	regmap_multi_reg_write(ad193x->regmap, reg_init, ARRAY_SIZE(reg_init));
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	if (ad193x_has_adc(ad193x)) {
4458c2ecf20Sopenharmony_ci		regmap_multi_reg_write(ad193x->regmap, reg_adc_init,
4468c2ecf20Sopenharmony_ci				       ARRAY_SIZE(reg_adc_init));
4478c2ecf20Sopenharmony_ci	}
4488c2ecf20Sopenharmony_ci}
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_cistatic int ad193x_component_probe(struct snd_soc_component *component)
4518c2ecf20Sopenharmony_ci{
4528c2ecf20Sopenharmony_ci	struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component);
4538c2ecf20Sopenharmony_ci	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
4548c2ecf20Sopenharmony_ci	int num, ret;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	/* default setting for ad193x */
4578c2ecf20Sopenharmony_ci	ad193x_reg_default_init(ad193x);
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	/* adc only */
4608c2ecf20Sopenharmony_ci	if (ad193x_has_adc(ad193x)) {
4618c2ecf20Sopenharmony_ci		/* add adc controls */
4628c2ecf20Sopenharmony_ci		num = ARRAY_SIZE(ad193x_adc_snd_controls);
4638c2ecf20Sopenharmony_ci		ret = snd_soc_add_component_controls(component,
4648c2ecf20Sopenharmony_ci						 ad193x_adc_snd_controls,
4658c2ecf20Sopenharmony_ci						 num);
4668c2ecf20Sopenharmony_ci		if (ret)
4678c2ecf20Sopenharmony_ci			return ret;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci		/* add adc widgets */
4708c2ecf20Sopenharmony_ci		num = ARRAY_SIZE(ad193x_adc_widgets);
4718c2ecf20Sopenharmony_ci		ret = snd_soc_dapm_new_controls(dapm,
4728c2ecf20Sopenharmony_ci						ad193x_adc_widgets,
4738c2ecf20Sopenharmony_ci						num);
4748c2ecf20Sopenharmony_ci		if (ret)
4758c2ecf20Sopenharmony_ci			return ret;
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci		/* add adc routes */
4788c2ecf20Sopenharmony_ci		num = ARRAY_SIZE(ad193x_adc_audio_paths);
4798c2ecf20Sopenharmony_ci		ret = snd_soc_dapm_add_routes(dapm,
4808c2ecf20Sopenharmony_ci					      ad193x_adc_audio_paths,
4818c2ecf20Sopenharmony_ci					      num);
4828c2ecf20Sopenharmony_ci		if (ret)
4838c2ecf20Sopenharmony_ci			return ret;
4848c2ecf20Sopenharmony_ci	}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	return 0;
4878c2ecf20Sopenharmony_ci}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_cistatic const struct snd_soc_component_driver soc_component_dev_ad193x = {
4908c2ecf20Sopenharmony_ci	.probe			= ad193x_component_probe,
4918c2ecf20Sopenharmony_ci	.controls		= ad193x_snd_controls,
4928c2ecf20Sopenharmony_ci	.num_controls		= ARRAY_SIZE(ad193x_snd_controls),
4938c2ecf20Sopenharmony_ci	.dapm_widgets		= ad193x_dapm_widgets,
4948c2ecf20Sopenharmony_ci	.num_dapm_widgets	= ARRAY_SIZE(ad193x_dapm_widgets),
4958c2ecf20Sopenharmony_ci	.dapm_routes		= audio_paths,
4968c2ecf20Sopenharmony_ci	.num_dapm_routes	= ARRAY_SIZE(audio_paths),
4978c2ecf20Sopenharmony_ci	.idle_bias_on		= 1,
4988c2ecf20Sopenharmony_ci	.use_pmdown_time	= 1,
4998c2ecf20Sopenharmony_ci	.endianness		= 1,
5008c2ecf20Sopenharmony_ci	.non_legacy_dai_naming	= 1,
5018c2ecf20Sopenharmony_ci};
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ciconst struct regmap_config ad193x_regmap_config = {
5048c2ecf20Sopenharmony_ci	.max_register = AD193X_NUM_REGS - 1,
5058c2ecf20Sopenharmony_ci};
5068c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ad193x_regmap_config);
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ciint ad193x_probe(struct device *dev, struct regmap *regmap,
5098c2ecf20Sopenharmony_ci		 enum ad193x_type type)
5108c2ecf20Sopenharmony_ci{
5118c2ecf20Sopenharmony_ci	struct ad193x_priv *ad193x;
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	if (IS_ERR(regmap))
5148c2ecf20Sopenharmony_ci		return PTR_ERR(regmap);
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	ad193x = devm_kzalloc(dev, sizeof(*ad193x), GFP_KERNEL);
5178c2ecf20Sopenharmony_ci	if (ad193x == NULL)
5188c2ecf20Sopenharmony_ci		return -ENOMEM;
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	ad193x->regmap = regmap;
5218c2ecf20Sopenharmony_ci	ad193x->type = type;
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	dev_set_drvdata(dev, ad193x);
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	if (ad193x_has_adc(ad193x))
5268c2ecf20Sopenharmony_ci		return devm_snd_soc_register_component(dev, &soc_component_dev_ad193x,
5278c2ecf20Sopenharmony_ci						       &ad193x_dai, 1);
5288c2ecf20Sopenharmony_ci	return devm_snd_soc_register_component(dev, &soc_component_dev_ad193x,
5298c2ecf20Sopenharmony_ci		&ad193x_no_adc_dai, 1);
5308c2ecf20Sopenharmony_ci}
5318c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ad193x_probe);
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ASoC ad193x driver");
5348c2ecf20Sopenharmony_ciMODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
5358c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
536