xref: /linux/sound/soc/codecs/src4xxx.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
14e6bedd3SMatt Flax // SPDX-License-Identifier: GPL-2.0
24e6bedd3SMatt Flax //
34e6bedd3SMatt Flax // TI SRC4xxx Audio Codec driver
44e6bedd3SMatt Flax //
54e6bedd3SMatt Flax // Copyright 2021-2022 Deqx Pty Ltd
64e6bedd3SMatt Flax // Author: Matt Flax <flatmax@flatmax.com>
74e6bedd3SMatt Flax 
84e6bedd3SMatt Flax #include <linux/module.h>
94e6bedd3SMatt Flax 
104e6bedd3SMatt Flax #include <sound/soc.h>
114e6bedd3SMatt Flax #include <sound/tlv.h>
124e6bedd3SMatt Flax 
134e6bedd3SMatt Flax #include "src4xxx.h"
144e6bedd3SMatt Flax 
154e6bedd3SMatt Flax struct src4xxx {
164e6bedd3SMatt Flax 	struct regmap *regmap;
174e6bedd3SMatt Flax 	bool master[2];
184e6bedd3SMatt Flax 	int mclk_hz;
194e6bedd3SMatt Flax 	struct device *dev;
204e6bedd3SMatt Flax };
214e6bedd3SMatt Flax 
224e6bedd3SMatt Flax enum {SRC4XXX_PORTA, SRC4XXX_PORTB};
234e6bedd3SMatt Flax 
244e6bedd3SMatt Flax /* SRC attenuation */
254e6bedd3SMatt Flax static const DECLARE_TLV_DB_SCALE(src_tlv, -12750, 50, 0);
264e6bedd3SMatt Flax 
274e6bedd3SMatt Flax static const struct snd_kcontrol_new src4xxx_controls[] = {
284e6bedd3SMatt Flax 	SOC_DOUBLE_R_TLV("SRC Volume",
294e6bedd3SMatt Flax 		SRC4XXX_SCR_CTL_30, SRC4XXX_SCR_CTL_31, 0, 255, 1, src_tlv),
304e6bedd3SMatt Flax };
314e6bedd3SMatt Flax 
324e6bedd3SMatt Flax /* I2S port control */
334e6bedd3SMatt Flax static const char * const port_out_src_text[] = {
344e6bedd3SMatt Flax 	"loopback", "other_port", "DIR", "SRC"
354e6bedd3SMatt Flax };
364e6bedd3SMatt Flax static SOC_ENUM_SINGLE_DECL(porta_out_src_enum, SRC4XXX_PORTA_CTL_03, 4,
374e6bedd3SMatt Flax 	port_out_src_text);
384e6bedd3SMatt Flax static SOC_ENUM_SINGLE_DECL(portb_out_src_enum, SRC4XXX_PORTB_CTL_05, 4,
394e6bedd3SMatt Flax 	port_out_src_text);
404e6bedd3SMatt Flax static const struct snd_kcontrol_new porta_out_control =
414e6bedd3SMatt Flax 	SOC_DAPM_ENUM("Port A source select", porta_out_src_enum);
424e6bedd3SMatt Flax static const struct snd_kcontrol_new portb_out_control =
434e6bedd3SMatt Flax 	SOC_DAPM_ENUM("Port B source select", portb_out_src_enum);
444e6bedd3SMatt Flax 
454e6bedd3SMatt Flax /* Digital audio transmitter control */
464e6bedd3SMatt Flax static const char * const dit_mux_text[] = {"Port A", "Port B", "DIR", "SRC"};
474e6bedd3SMatt Flax static SOC_ENUM_SINGLE_DECL(dit_mux_enum, SRC4XXX_TX_CTL_07, 3, dit_mux_text);
484e6bedd3SMatt Flax static const struct snd_kcontrol_new dit_mux_control =
494e6bedd3SMatt Flax 	SOC_DAPM_ENUM("DIT source", dit_mux_enum);
504e6bedd3SMatt Flax 
514e6bedd3SMatt Flax /* SRC control */
524e6bedd3SMatt Flax static const char * const src_in_text[] = {"Port A", "Port B", "DIR"};
534e6bedd3SMatt Flax static SOC_ENUM_SINGLE_DECL(src_in_enum, SRC4XXX_SCR_CTL_2D, 0, src_in_text);
544e6bedd3SMatt Flax static const struct snd_kcontrol_new src_in_control =
554e6bedd3SMatt Flax 	SOC_DAPM_ENUM("SRC source select", src_in_enum);
564e6bedd3SMatt Flax 
574e6bedd3SMatt Flax /* DIR control */
584e6bedd3SMatt Flax static const char * const dir_in_text[] = {"Ch 1", "Ch 2", "Ch 3", "Ch 4"};
594e6bedd3SMatt Flax static SOC_ENUM_SINGLE_DECL(dir_in_enum, SRC4XXX_RCV_CTL_0D, 0, dir_in_text);
604e6bedd3SMatt Flax static const struct snd_kcontrol_new dir_in_control =
614e6bedd3SMatt Flax 	SOC_DAPM_ENUM("Digital Input", dir_in_enum);
624e6bedd3SMatt Flax 
634e6bedd3SMatt Flax static const struct snd_soc_dapm_widget src4xxx_dapm_widgets[] = {
644e6bedd3SMatt Flax 	SND_SOC_DAPM_INPUT("loopback_A"),
654e6bedd3SMatt Flax 	SND_SOC_DAPM_INPUT("other_port_A"),
664e6bedd3SMatt Flax 	SND_SOC_DAPM_INPUT("DIR_A"),
674e6bedd3SMatt Flax 	SND_SOC_DAPM_INPUT("SRC_A"),
684e6bedd3SMatt Flax 	SND_SOC_DAPM_MUX("Port A source",
694e6bedd3SMatt Flax 		SND_SOC_NOPM, 0, 0, &porta_out_control),
704e6bedd3SMatt Flax 
714e6bedd3SMatt Flax 	SND_SOC_DAPM_INPUT("loopback_B"),
724e6bedd3SMatt Flax 	SND_SOC_DAPM_INPUT("other_port_B"),
734e6bedd3SMatt Flax 	SND_SOC_DAPM_INPUT("DIR_B"),
744e6bedd3SMatt Flax 	SND_SOC_DAPM_INPUT("SRC_B"),
754e6bedd3SMatt Flax 	SND_SOC_DAPM_MUX("Port B source",
764e6bedd3SMatt Flax 		SND_SOC_NOPM, 0, 0, &portb_out_control),
774e6bedd3SMatt Flax 
784e6bedd3SMatt Flax 	SND_SOC_DAPM_INPUT("Port_A"),
794e6bedd3SMatt Flax 	SND_SOC_DAPM_INPUT("Port_B"),
804e6bedd3SMatt Flax 	SND_SOC_DAPM_INPUT("DIR_"),
814e6bedd3SMatt Flax 
824e6bedd3SMatt Flax 	/* Digital audio receivers and transmitters */
834e6bedd3SMatt Flax 	SND_SOC_DAPM_OUTPUT("DIR_OUT"),
844e6bedd3SMatt Flax 	SND_SOC_DAPM_OUTPUT("SRC_OUT"),
854e6bedd3SMatt Flax 	SND_SOC_DAPM_MUX("DIT Out Src", SRC4XXX_PWR_RST_01,
864e6bedd3SMatt Flax 		SRC4XXX_ENABLE_DIT_SHIFT, 1, &dit_mux_control),
874e6bedd3SMatt Flax 
884e6bedd3SMatt Flax 	/* Audio Interface */
894e6bedd3SMatt Flax 	SND_SOC_DAPM_AIF_IN("AIF_A_RX", "Playback A", 0,
904e6bedd3SMatt Flax 		SRC4XXX_PWR_RST_01, SRC4XXX_ENABLE_PORT_A_SHIFT, 1),
914e6bedd3SMatt Flax 	SND_SOC_DAPM_AIF_OUT("AIF_A_TX", "Capture A", 0,
924e6bedd3SMatt Flax 		SRC4XXX_PWR_RST_01, SRC4XXX_ENABLE_PORT_A_SHIFT, 1),
934e6bedd3SMatt Flax 	SND_SOC_DAPM_AIF_IN("AIF_B_RX", "Playback B", 0,
944e6bedd3SMatt Flax 		SRC4XXX_PWR_RST_01, SRC4XXX_ENABLE_PORT_B_SHIFT, 1),
954e6bedd3SMatt Flax 	SND_SOC_DAPM_AIF_OUT("AIF_B_TX", "Capture B", 0,
964e6bedd3SMatt Flax 		SRC4XXX_PWR_RST_01, SRC4XXX_ENABLE_PORT_B_SHIFT, 1),
974e6bedd3SMatt Flax 
984e6bedd3SMatt Flax 	SND_SOC_DAPM_MUX("SRC source", SND_SOC_NOPM, 0, 0, &src_in_control),
994e6bedd3SMatt Flax 
1004e6bedd3SMatt Flax 	SND_SOC_DAPM_INPUT("MCLK"),
1014e6bedd3SMatt Flax 	SND_SOC_DAPM_INPUT("RXMCLKI"),
1024e6bedd3SMatt Flax 	SND_SOC_DAPM_INPUT("RXMCLKO"),
1034e6bedd3SMatt Flax 
1044e6bedd3SMatt Flax 	SND_SOC_DAPM_INPUT("RX1"),
1054e6bedd3SMatt Flax 	SND_SOC_DAPM_INPUT("RX2"),
1064e6bedd3SMatt Flax 	SND_SOC_DAPM_INPUT("RX3"),
1074e6bedd3SMatt Flax 	SND_SOC_DAPM_INPUT("RX4"),
1084e6bedd3SMatt Flax 	SND_SOC_DAPM_MUX("Digital Input", SRC4XXX_PWR_RST_01,
1094e6bedd3SMatt Flax 		SRC4XXX_ENABLE_DIR_SHIFT, 1, &dir_in_control),
1104e6bedd3SMatt Flax };
1114e6bedd3SMatt Flax 
1124e6bedd3SMatt Flax static const struct snd_soc_dapm_route src4xxx_audio_routes[] = {
1134e6bedd3SMatt Flax 	/* I2S Input to Output Routing */
1144e6bedd3SMatt Flax 	{"Port A source", "loopback", "loopback_A"},
1154e6bedd3SMatt Flax 	{"Port A source", "other_port", "other_port_A"},
1164e6bedd3SMatt Flax 	{"Port A source", "DIR", "DIR_A"},
1174e6bedd3SMatt Flax 	{"Port A source", "SRC", "SRC_A"},
1184e6bedd3SMatt Flax 	{"Port B source", "loopback", "loopback_B"},
1194e6bedd3SMatt Flax 	{"Port B source", "other_port", "other_port_B"},
1204e6bedd3SMatt Flax 	{"Port B source", "DIR", "DIR_B"},
1214e6bedd3SMatt Flax 	{"Port B source", "SRC", "SRC_B"},
1224e6bedd3SMatt Flax 	/* DIT muxing */
1234e6bedd3SMatt Flax 	{"DIT Out Src", "Port A", "Capture A"},
1244e6bedd3SMatt Flax 	{"DIT Out Src", "Port B", "Capture B"},
1254e6bedd3SMatt Flax 	{"DIT Out Src", "DIR", "DIR_OUT"},
1264e6bedd3SMatt Flax 	{"DIT Out Src", "SRC", "SRC_OUT"},
1274e6bedd3SMatt Flax 
1284e6bedd3SMatt Flax 	/* SRC input selection */
1294e6bedd3SMatt Flax 	{"SRC source", "Port A", "Port_A"},
1304e6bedd3SMatt Flax 	{"SRC source", "Port B", "Port_B"},
1314e6bedd3SMatt Flax 	{"SRC source", "DIR", "DIR_"},
1324e6bedd3SMatt Flax 	/* SRC mclk selection */
1334e6bedd3SMatt Flax 	{"SRC mclk source", "Master (MCLK)", "MCLK"},
1344e6bedd3SMatt Flax 	{"SRC mclk source", "Master (RXCLKI)", "RXMCLKI"},
1354e6bedd3SMatt Flax 	{"SRC mclk source", "Recovered receiver clk", "RXMCLKO"},
1364e6bedd3SMatt Flax 	/* DIR input selection */
1374e6bedd3SMatt Flax 	{"Digital Input", "Ch 1", "RX1"},
1384e6bedd3SMatt Flax 	{"Digital Input", "Ch 2", "RX2"},
1394e6bedd3SMatt Flax 	{"Digital Input", "Ch 3", "RX3"},
1404e6bedd3SMatt Flax 	{"Digital Input", "Ch 4", "RX4"},
1414e6bedd3SMatt Flax };
1424e6bedd3SMatt Flax 
1434e6bedd3SMatt Flax 
1444e6bedd3SMatt Flax static const struct snd_soc_component_driver src4xxx_driver = {
1454e6bedd3SMatt Flax 	.controls = src4xxx_controls,
1464e6bedd3SMatt Flax 	.num_controls = ARRAY_SIZE(src4xxx_controls),
1474e6bedd3SMatt Flax 
1484e6bedd3SMatt Flax 	.dapm_widgets = src4xxx_dapm_widgets,
1494e6bedd3SMatt Flax 	.num_dapm_widgets = ARRAY_SIZE(src4xxx_dapm_widgets),
1504e6bedd3SMatt Flax 	.dapm_routes = src4xxx_audio_routes,
1514e6bedd3SMatt Flax 	.num_dapm_routes = ARRAY_SIZE(src4xxx_audio_routes),
1524e6bedd3SMatt Flax };
1534e6bedd3SMatt Flax 
src4xxx_set_dai_fmt(struct snd_soc_dai * dai,unsigned int fmt)1544e6bedd3SMatt Flax static int src4xxx_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1554e6bedd3SMatt Flax {
1564e6bedd3SMatt Flax 	struct snd_soc_component *component = dai->component;
1574e6bedd3SMatt Flax 	struct src4xxx *src4xxx = snd_soc_component_get_drvdata(component);
1584e6bedd3SMatt Flax 	unsigned int ctrl;
1594e6bedd3SMatt Flax 
1604e6bedd3SMatt Flax 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1614e6bedd3SMatt Flax 	case SND_SOC_DAIFMT_CBM_CFM:
1624e6bedd3SMatt Flax 		ctrl = SRC4XXX_BUS_MASTER;
1634e6bedd3SMatt Flax 		src4xxx->master[dai->id] = true;
1644e6bedd3SMatt Flax 		break;
1654e6bedd3SMatt Flax 	case SND_SOC_DAIFMT_CBS_CFS:
1664e6bedd3SMatt Flax 		ctrl = 0;
1674e6bedd3SMatt Flax 		src4xxx->master[dai->id] = false;
1684e6bedd3SMatt Flax 		break;
1694e6bedd3SMatt Flax 	default:
1704e6bedd3SMatt Flax 		return -EINVAL;
1714e6bedd3SMatt Flax 		break;
1724e6bedd3SMatt Flax 	}
1734e6bedd3SMatt Flax 
1744e6bedd3SMatt Flax 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1754e6bedd3SMatt Flax 	case SND_SOC_DAIFMT_I2S:
1764e6bedd3SMatt Flax 		ctrl |= SRC4XXX_BUS_I2S;
1774e6bedd3SMatt Flax 		break;
1784e6bedd3SMatt Flax 	case SND_SOC_DAIFMT_LEFT_J:
1794e6bedd3SMatt Flax 		ctrl |= SRC4XXX_BUS_LEFT_J;
1804e6bedd3SMatt Flax 		break;
1814e6bedd3SMatt Flax 	case SND_SOC_DAIFMT_RIGHT_J:
1824e6bedd3SMatt Flax 		ctrl |= SRC4XXX_BUS_RIGHT_J_24;
1834e6bedd3SMatt Flax 		break;
1844e6bedd3SMatt Flax 	default:
1854e6bedd3SMatt Flax 		return -EINVAL;
1864e6bedd3SMatt Flax 		break;
1874e6bedd3SMatt Flax 	}
1884e6bedd3SMatt Flax 
1894e6bedd3SMatt Flax 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1904e6bedd3SMatt Flax 	case SND_SOC_DAIFMT_NB_NF:
1914e6bedd3SMatt Flax 		break;
1924e6bedd3SMatt Flax 	default:
1934e6bedd3SMatt Flax 		return -EINVAL;
1944e6bedd3SMatt Flax 		break;
1954e6bedd3SMatt Flax 	}
1964e6bedd3SMatt Flax 
1974e6bedd3SMatt Flax 	regmap_update_bits(src4xxx->regmap, SRC4XXX_BUS_FMT(dai->id),
1984e6bedd3SMatt Flax 		SRC4XXX_BUS_FMT_MS_MASK, ctrl);
1994e6bedd3SMatt Flax 
2004e6bedd3SMatt Flax 	return 0;
2014e6bedd3SMatt Flax }
2024e6bedd3SMatt Flax 
src4xxx_set_mclk_hz(struct snd_soc_dai * codec_dai,int clk_id,unsigned int freq,int dir)2034e6bedd3SMatt Flax static int src4xxx_set_mclk_hz(struct snd_soc_dai *codec_dai,
2044e6bedd3SMatt Flax 		int clk_id, unsigned int freq, int dir)
2054e6bedd3SMatt Flax {
2064e6bedd3SMatt Flax 	struct snd_soc_component *component = codec_dai->component;
2074e6bedd3SMatt Flax 	struct src4xxx *src4xxx = snd_soc_component_get_drvdata(component);
2084e6bedd3SMatt Flax 
2094e6bedd3SMatt Flax 	dev_info(component->dev, "changing mclk rate from %d to %d Hz\n",
2104e6bedd3SMatt Flax 		src4xxx->mclk_hz, freq);
2114e6bedd3SMatt Flax 	src4xxx->mclk_hz = freq;
2124e6bedd3SMatt Flax 
2134e6bedd3SMatt Flax 	return 0;
2144e6bedd3SMatt Flax }
2154e6bedd3SMatt Flax 
src4xxx_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)2164e6bedd3SMatt Flax static int src4xxx_hw_params(struct snd_pcm_substream *substream,
2174e6bedd3SMatt Flax 			struct snd_pcm_hw_params *params,
2184e6bedd3SMatt Flax 			struct snd_soc_dai *dai)
2194e6bedd3SMatt Flax {
2204e6bedd3SMatt Flax 	struct snd_soc_component *component = dai->component;
2214e6bedd3SMatt Flax 	struct src4xxx *src4xxx = snd_soc_component_get_drvdata(component);
2224e6bedd3SMatt Flax 	unsigned int mclk_div;
2234e6bedd3SMatt Flax 	int val, pj, jd, d;
2244e6bedd3SMatt Flax 	int reg;
2254e6bedd3SMatt Flax 	int ret;
2264e6bedd3SMatt Flax 
2274e6bedd3SMatt Flax 	switch (dai->id) {
2284e6bedd3SMatt Flax 	case SRC4XXX_PORTB:
2294e6bedd3SMatt Flax 		reg = SRC4XXX_PORTB_CTL_06;
2304e6bedd3SMatt Flax 		break;
2314e6bedd3SMatt Flax 	default:
2324e6bedd3SMatt Flax 		reg = SRC4XXX_PORTA_CTL_04;
2334e6bedd3SMatt Flax 		break;
2344e6bedd3SMatt Flax 	}
2354e6bedd3SMatt Flax 
2364e6bedd3SMatt Flax 	if (src4xxx->master[dai->id]) {
2374e6bedd3SMatt Flax 		mclk_div = src4xxx->mclk_hz/params_rate(params);
2384e6bedd3SMatt Flax 		if (src4xxx->mclk_hz != mclk_div*params_rate(params)) {
2394e6bedd3SMatt Flax 			dev_err(component->dev,
2404e6bedd3SMatt Flax 				"mclk %d / rate %d has a remainder.\n",
2414e6bedd3SMatt Flax 				src4xxx->mclk_hz, params_rate(params));
2424e6bedd3SMatt Flax 			return -EINVAL;
2434e6bedd3SMatt Flax 		}
2444e6bedd3SMatt Flax 
2454e6bedd3SMatt Flax 		val = ((int)mclk_div - 128) / 128;
2464e6bedd3SMatt Flax 		if ((val < 0) | (val > 3)) {
2474e6bedd3SMatt Flax 			dev_err(component->dev,
2484e6bedd3SMatt Flax 				"div register setting %d is out of range\n",
2494e6bedd3SMatt Flax 				val);
2504e6bedd3SMatt Flax 			dev_err(component->dev,
2514e6bedd3SMatt Flax 				"unsupported sample rate %d Hz for the master clock of %d Hz\n",
2524e6bedd3SMatt Flax 				params_rate(params), src4xxx->mclk_hz);
2534e6bedd3SMatt Flax 			return -EINVAL;
2544e6bedd3SMatt Flax 		}
2554e6bedd3SMatt Flax 
2564e6bedd3SMatt Flax 		/* set the TX DIV */
2574e6bedd3SMatt Flax 		ret = regmap_update_bits(src4xxx->regmap,
2584e6bedd3SMatt Flax 			SRC4XXX_TX_CTL_07, SRC4XXX_TX_MCLK_DIV_MASK,
2594e6bedd3SMatt Flax 			val<<SRC4XXX_TX_MCLK_DIV_SHIFT);
2604e6bedd3SMatt Flax 		if (ret) {
2614e6bedd3SMatt Flax 			dev_err(component->dev,
2624e6bedd3SMatt Flax 				"Couldn't set the TX's div register to %d << %d = 0x%x\n",
2634e6bedd3SMatt Flax 				val, SRC4XXX_TX_MCLK_DIV_SHIFT,
2644e6bedd3SMatt Flax 				val<<SRC4XXX_TX_MCLK_DIV_SHIFT);
2654e6bedd3SMatt Flax 			return ret;
2664e6bedd3SMatt Flax 		}
2674e6bedd3SMatt Flax 
2684e6bedd3SMatt Flax 		/* set the PLL for the digital receiver */
2694e6bedd3SMatt Flax 		switch (src4xxx->mclk_hz) {
2704e6bedd3SMatt Flax 		case 24576000:
2714e6bedd3SMatt Flax 			pj = 0x22;
2724e6bedd3SMatt Flax 			jd = 0x00;
2734e6bedd3SMatt Flax 			d = 0x00;
2744e6bedd3SMatt Flax 			break;
2754e6bedd3SMatt Flax 		case 22579200:
2764e6bedd3SMatt Flax 			pj = 0x22;
2774e6bedd3SMatt Flax 			jd = 0x1b;
2784e6bedd3SMatt Flax 			d = 0xa3;
2794e6bedd3SMatt Flax 			break;
2804e6bedd3SMatt Flax 		default:
2814e6bedd3SMatt Flax 			/* don't error out here,
2824e6bedd3SMatt Flax 			 * other parts of the chip are still functional
283*7d3ac70dSNathan Chancellor 			 * Dummy initialize variables to avoid
284*7d3ac70dSNathan Chancellor 			 * -Wsometimes-uninitialized from clang.
2854e6bedd3SMatt Flax 			 */
2864e6bedd3SMatt Flax 			dev_info(component->dev,
287*7d3ac70dSNathan Chancellor 				"Couldn't set the RCV PLL as this master clock rate is unknown. Chosen regmap values may not match real world values.\n");
288*7d3ac70dSNathan Chancellor 			pj = 0x0;
289*7d3ac70dSNathan Chancellor 			jd = 0xff;
290*7d3ac70dSNathan Chancellor 			d = 0xff;
2914e6bedd3SMatt Flax 			break;
2924e6bedd3SMatt Flax 		}
2934e6bedd3SMatt Flax 		ret = regmap_write(src4xxx->regmap, SRC4XXX_RCV_PLL_0F, pj);
2944e6bedd3SMatt Flax 		if (ret < 0)
2954e6bedd3SMatt Flax 			dev_err(component->dev,
2964e6bedd3SMatt Flax 				"Failed to update PLL register 0x%x\n",
2974e6bedd3SMatt Flax 				SRC4XXX_RCV_PLL_0F);
2984e6bedd3SMatt Flax 		ret = regmap_write(src4xxx->regmap, SRC4XXX_RCV_PLL_10, jd);
2994e6bedd3SMatt Flax 		if (ret < 0)
3004e6bedd3SMatt Flax 			dev_err(component->dev,
3014e6bedd3SMatt Flax 				"Failed to update PLL register 0x%x\n",
3024e6bedd3SMatt Flax 				SRC4XXX_RCV_PLL_10);
3034e6bedd3SMatt Flax 		ret = regmap_write(src4xxx->regmap, SRC4XXX_RCV_PLL_11, d);
3044e6bedd3SMatt Flax 		if (ret < 0)
3054e6bedd3SMatt Flax 			dev_err(component->dev,
3064e6bedd3SMatt Flax 				"Failed to update PLL register 0x%x\n",
3074e6bedd3SMatt Flax 				SRC4XXX_RCV_PLL_11);
3084e6bedd3SMatt Flax 
3094e6bedd3SMatt Flax 		ret = regmap_update_bits(src4xxx->regmap,
3104e6bedd3SMatt Flax 			SRC4XXX_TX_CTL_07, SRC4XXX_TX_MCLK_DIV_MASK,
3114e6bedd3SMatt Flax 			val<<SRC4XXX_TX_MCLK_DIV_SHIFT);
3124e6bedd3SMatt Flax 		if (ret < 0) {
3134e6bedd3SMatt Flax 			dev_err(component->dev,
3144e6bedd3SMatt Flax 				"Couldn't set the TX's div register to %d << %d = 0x%x\n",
3154e6bedd3SMatt Flax 				val, SRC4XXX_TX_MCLK_DIV_SHIFT,
3164e6bedd3SMatt Flax 				val<<SRC4XXX_TX_MCLK_DIV_SHIFT);
3174e6bedd3SMatt Flax 			return ret;
3184e6bedd3SMatt Flax 		}
3194e6bedd3SMatt Flax 
3204e6bedd3SMatt Flax 		return regmap_update_bits(src4xxx->regmap, reg,
3214e6bedd3SMatt Flax 					SRC4XXX_MCLK_DIV_MASK, val);
3224e6bedd3SMatt Flax 	} else {
3234e6bedd3SMatt Flax 		dev_info(dai->dev, "not setting up MCLK as not master\n");
3244e6bedd3SMatt Flax 	}
3254e6bedd3SMatt Flax 
3264e6bedd3SMatt Flax 	return 0;
3274e6bedd3SMatt Flax };
3284e6bedd3SMatt Flax 
3294e6bedd3SMatt Flax static const struct snd_soc_dai_ops src4xxx_dai_ops = {
3304e6bedd3SMatt Flax 	.hw_params	= src4xxx_hw_params,
3314e6bedd3SMatt Flax 	.set_sysclk	= src4xxx_set_mclk_hz,
3324e6bedd3SMatt Flax 	.set_fmt	= src4xxx_set_dai_fmt,
3334e6bedd3SMatt Flax };
3344e6bedd3SMatt Flax 
3354e6bedd3SMatt Flax #define SRC4XXX_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |	SNDRV_PCM_FMTBIT_S32_LE)
3364e6bedd3SMatt Flax #define SRC4XXX_RATES (SNDRV_PCM_RATE_44100|SNDRV_PCM_RATE_48000|\
3374e6bedd3SMatt Flax 				SNDRV_PCM_RATE_88200|\
3384e6bedd3SMatt Flax 				SNDRV_PCM_RATE_96000|\
3394e6bedd3SMatt Flax 				SNDRV_PCM_RATE_176400|\
3404e6bedd3SMatt Flax 				SNDRV_PCM_RATE_192000)
3414e6bedd3SMatt Flax 
3424e6bedd3SMatt Flax static struct snd_soc_dai_driver src4xxx_dai_driver[] = {
3434e6bedd3SMatt Flax 	{
3444e6bedd3SMatt Flax 		.id = SRC4XXX_PORTA,
3454e6bedd3SMatt Flax 		.name = "src4xxx-portA",
3464e6bedd3SMatt Flax 		.playback = {
3474e6bedd3SMatt Flax 			.stream_name = "Playback A",
3484e6bedd3SMatt Flax 			.channels_min = 2,
3494e6bedd3SMatt Flax 			.channels_max = 2,
3504e6bedd3SMatt Flax 			.rates = SRC4XXX_RATES,
3514e6bedd3SMatt Flax 			.formats = SRC4XXX_FORMATS,
3524e6bedd3SMatt Flax 		},
3534e6bedd3SMatt Flax 		.capture = {
3544e6bedd3SMatt Flax 			.stream_name = "Capture A",
3554e6bedd3SMatt Flax 			.channels_min = 2,
3564e6bedd3SMatt Flax 			.channels_max = 2,
3574e6bedd3SMatt Flax 			.rates = SRC4XXX_RATES,
3584e6bedd3SMatt Flax 			.formats = SRC4XXX_FORMATS,
3594e6bedd3SMatt Flax 		},
3604e6bedd3SMatt Flax 		.ops = &src4xxx_dai_ops,
3614e6bedd3SMatt Flax 	},
3624e6bedd3SMatt Flax 	{
3634e6bedd3SMatt Flax 		.id = SRC4XXX_PORTB,
3644e6bedd3SMatt Flax 		.name = "src4xxx-portB",
3654e6bedd3SMatt Flax 		.playback = {
3664e6bedd3SMatt Flax 			.stream_name = "Playback B",
3674e6bedd3SMatt Flax 			.channels_min = 2,
3684e6bedd3SMatt Flax 			.channels_max = 2,
3694e6bedd3SMatt Flax 			.rates = SRC4XXX_RATES,
3704e6bedd3SMatt Flax 			.formats = SRC4XXX_FORMATS,
3714e6bedd3SMatt Flax 		},
3724e6bedd3SMatt Flax 		.capture = {
3734e6bedd3SMatt Flax 			.stream_name = "Capture B",
3744e6bedd3SMatt Flax 			.channels_min = 2,
3754e6bedd3SMatt Flax 			.channels_max = 2,
3764e6bedd3SMatt Flax 			.rates = SRC4XXX_RATES,
3774e6bedd3SMatt Flax 			.formats = SRC4XXX_FORMATS,
3784e6bedd3SMatt Flax 		},
3794e6bedd3SMatt Flax 		.ops = &src4xxx_dai_ops,
3804e6bedd3SMatt Flax 	},
3814e6bedd3SMatt Flax };
3824e6bedd3SMatt Flax 
3834e6bedd3SMatt Flax static const struct reg_default src4xxx_reg_defaults[] = {
3844e6bedd3SMatt Flax 	{ SRC4XXX_PWR_RST_01,		0x00 }, /* all powered down intially */
3854e6bedd3SMatt Flax 	{ SRC4XXX_PORTA_CTL_03,		0x00 },
3864e6bedd3SMatt Flax 	{ SRC4XXX_PORTA_CTL_04,		0x00 },
3874e6bedd3SMatt Flax 	{ SRC4XXX_PORTB_CTL_05,		0x00 },
3884e6bedd3SMatt Flax 	{ SRC4XXX_PORTB_CTL_06,		0x00 },
3894e6bedd3SMatt Flax 	{ SRC4XXX_TX_CTL_07,		0x00 },
3904e6bedd3SMatt Flax 	{ SRC4XXX_TX_CTL_08,		0x00 },
3914e6bedd3SMatt Flax 	{ SRC4XXX_TX_CTL_09,		0x00 },
3924e6bedd3SMatt Flax 	{ SRC4XXX_SRC_DIT_IRQ_MSK_0B,	0x00 },
3934e6bedd3SMatt Flax 	{ SRC4XXX_SRC_DIT_IRQ_MODE_0C,	0x00 },
3944e6bedd3SMatt Flax 	{ SRC4XXX_RCV_CTL_0D,		0x00 },
3954e6bedd3SMatt Flax 	{ SRC4XXX_RCV_CTL_0E,		0x00 },
3964e6bedd3SMatt Flax 	{ SRC4XXX_RCV_PLL_0F,		0x00 }, /* not spec. in the datasheet */
3974e6bedd3SMatt Flax 	{ SRC4XXX_RCV_PLL_10,		0xff }, /* not spec. in the datasheet */
3984e6bedd3SMatt Flax 	{ SRC4XXX_RCV_PLL_11,		0xff }, /* not spec. in the datasheet */
3994e6bedd3SMatt Flax 	{ SRC4XXX_RVC_IRQ_MSK_16,	0x00 },
4004e6bedd3SMatt Flax 	{ SRC4XXX_RVC_IRQ_MSK_17,	0x00 },
4014e6bedd3SMatt Flax 	{ SRC4XXX_RVC_IRQ_MODE_18,	0x00 },
4024e6bedd3SMatt Flax 	{ SRC4XXX_RVC_IRQ_MODE_19,	0x00 },
4034e6bedd3SMatt Flax 	{ SRC4XXX_RVC_IRQ_MODE_1A,	0x00 },
4044e6bedd3SMatt Flax 	{ SRC4XXX_GPIO_1_1B,		0x00 },
4054e6bedd3SMatt Flax 	{ SRC4XXX_GPIO_2_1C,		0x00 },
4064e6bedd3SMatt Flax 	{ SRC4XXX_GPIO_3_1D,		0x00 },
4074e6bedd3SMatt Flax 	{ SRC4XXX_GPIO_4_1E,		0x00 },
4084e6bedd3SMatt Flax 	{ SRC4XXX_SCR_CTL_2D,		0x00 },
4094e6bedd3SMatt Flax 	{ SRC4XXX_SCR_CTL_2E,		0x00 },
4104e6bedd3SMatt Flax 	{ SRC4XXX_SCR_CTL_2F,		0x00 },
4114e6bedd3SMatt Flax 	{ SRC4XXX_SCR_CTL_30,		0x00 },
4124e6bedd3SMatt Flax 	{ SRC4XXX_SCR_CTL_31,		0x00 },
4134e6bedd3SMatt Flax };
4144e6bedd3SMatt Flax 
src4xxx_probe(struct device * dev,struct regmap * regmap,void (* switch_mode)(struct device * dev))4154e6bedd3SMatt Flax int src4xxx_probe(struct device *dev, struct regmap *regmap,
4164e6bedd3SMatt Flax 			void (*switch_mode)(struct device *dev))
4174e6bedd3SMatt Flax {
4184e6bedd3SMatt Flax 	struct src4xxx *src4xxx;
4194e6bedd3SMatt Flax 	int ret;
4204e6bedd3SMatt Flax 
4214e6bedd3SMatt Flax 	if (IS_ERR(regmap))
4224e6bedd3SMatt Flax 		return PTR_ERR(regmap);
4234e6bedd3SMatt Flax 
4244e6bedd3SMatt Flax 	src4xxx = devm_kzalloc(dev, sizeof(*src4xxx), GFP_KERNEL);
4254e6bedd3SMatt Flax 	if (!src4xxx)
4264e6bedd3SMatt Flax 		return -ENOMEM;
4274e6bedd3SMatt Flax 
4284e6bedd3SMatt Flax 	src4xxx->regmap = regmap;
4294e6bedd3SMatt Flax 	src4xxx->dev = dev;
4304e6bedd3SMatt Flax 	src4xxx->mclk_hz = 0; /* mclk has not been configured yet */
4314e6bedd3SMatt Flax 	dev_set_drvdata(dev, src4xxx);
4324e6bedd3SMatt Flax 
4334e6bedd3SMatt Flax 	ret = regmap_write(regmap, SRC4XXX_PWR_RST_01, SRC4XXX_RESET);
4344e6bedd3SMatt Flax 	if (ret < 0)
4354e6bedd3SMatt Flax 		dev_err(dev, "Failed to issue reset: %d\n", ret);
4364e6bedd3SMatt Flax 	usleep_range(1, 500); /* sleep for more then 500 ns */
4374e6bedd3SMatt Flax 	ret = regmap_write(regmap, SRC4XXX_PWR_RST_01, SRC4XXX_POWER_DOWN);
4384e6bedd3SMatt Flax 	if (ret < 0)
4394e6bedd3SMatt Flax 		dev_err(dev, "Failed to decommission reset: %d\n", ret);
4404e6bedd3SMatt Flax 	usleep_range(500, 1000); /* sleep for 500 us or more */
4414e6bedd3SMatt Flax 
4424e6bedd3SMatt Flax 	ret = regmap_update_bits(src4xxx->regmap, SRC4XXX_PWR_RST_01,
4434e6bedd3SMatt Flax 		SRC4XXX_POWER_ENABLE, SRC4XXX_POWER_ENABLE);
4444e6bedd3SMatt Flax 	if (ret < 0)
4454e6bedd3SMatt Flax 		dev_err(dev, "Failed to port A and B : %d\n", ret);
4464e6bedd3SMatt Flax 
4474e6bedd3SMatt Flax 	/* set receiver to use master clock (rcv mclk is most likely jittery) */
4484e6bedd3SMatt Flax 	ret = regmap_update_bits(src4xxx->regmap, SRC4XXX_RCV_CTL_0D,
4494e6bedd3SMatt Flax 		SRC4XXX_RXCLK_MCLK,	SRC4XXX_RXCLK_MCLK);
4504e6bedd3SMatt Flax 	if (ret < 0)
4514e6bedd3SMatt Flax 		dev_err(dev,
4524e6bedd3SMatt Flax 			"Failed to enable mclk as the PLL1 DIR reference : %d\n", ret);
4534e6bedd3SMatt Flax 
4544e6bedd3SMatt Flax 	/* default to leaving the PLL2 running on loss of lock, divide by 8 */
4554e6bedd3SMatt Flax 	ret = regmap_update_bits(src4xxx->regmap, SRC4XXX_RCV_CTL_0E,
4564e6bedd3SMatt Flax 		SRC4XXX_PLL2_DIV_8 | SRC4XXX_REC_MCLK_EN | SRC4XXX_PLL2_LOL,
4574e6bedd3SMatt Flax 		SRC4XXX_PLL2_DIV_8 | SRC4XXX_REC_MCLK_EN | SRC4XXX_PLL2_LOL);
4584e6bedd3SMatt Flax 	if (ret < 0)
4594e6bedd3SMatt Flax 		dev_err(dev, "Failed to enable mclk rec and div : %d\n", ret);
4604e6bedd3SMatt Flax 
4614e6bedd3SMatt Flax 	ret = devm_snd_soc_register_component(dev, &src4xxx_driver,
4624e6bedd3SMatt Flax 			src4xxx_dai_driver, ARRAY_SIZE(src4xxx_dai_driver));
4634e6bedd3SMatt Flax 	if (ret == 0)
4644e6bedd3SMatt Flax 		dev_info(dev, "src4392 probe ok %d\n", ret);
4654e6bedd3SMatt Flax 	return ret;
4664e6bedd3SMatt Flax }
4674e6bedd3SMatt Flax EXPORT_SYMBOL_GPL(src4xxx_probe);
4684e6bedd3SMatt Flax 
src4xxx_volatile_register(struct device * dev,unsigned int reg)4694e6bedd3SMatt Flax static bool src4xxx_volatile_register(struct device *dev, unsigned int reg)
4704e6bedd3SMatt Flax {
4714e6bedd3SMatt Flax 	switch (reg) {
4724e6bedd3SMatt Flax 	case SRC4XXX_RES_00:
4734e6bedd3SMatt Flax 	case SRC4XXX_GLOBAL_ITR_STS_02:
4744e6bedd3SMatt Flax 	case SRC4XXX_SRC_DIT_STS_0A:
4754e6bedd3SMatt Flax 	case SRC4XXX_NON_AUDIO_D_12:
4764e6bedd3SMatt Flax 	case SRC4XXX_RVC_STS_13:
4774e6bedd3SMatt Flax 	case SRC4XXX_RVC_STS_14:
4784e6bedd3SMatt Flax 	case SRC4XXX_RVC_STS_15:
4794e6bedd3SMatt Flax 	case SRC4XXX_SUB_CODE_1F:
4804e6bedd3SMatt Flax 	case SRC4XXX_SUB_CODE_20:
4814e6bedd3SMatt Flax 	case SRC4XXX_SUB_CODE_21:
4824e6bedd3SMatt Flax 	case SRC4XXX_SUB_CODE_22:
4834e6bedd3SMatt Flax 	case SRC4XXX_SUB_CODE_23:
4844e6bedd3SMatt Flax 	case SRC4XXX_SUB_CODE_24:
4854e6bedd3SMatt Flax 	case SRC4XXX_SUB_CODE_25:
4864e6bedd3SMatt Flax 	case SRC4XXX_SUB_CODE_26:
4874e6bedd3SMatt Flax 	case SRC4XXX_SUB_CODE_27:
4884e6bedd3SMatt Flax 	case SRC4XXX_SUB_CODE_28:
4894e6bedd3SMatt Flax 	case SRC4XXX_PC_PREAMBLE_HI_29:
4904e6bedd3SMatt Flax 	case SRC4XXX_PC_PREAMBLE_LO_2A:
4914e6bedd3SMatt Flax 	case SRC4XXX_PD_PREAMBLE_HI_2B:
4924e6bedd3SMatt Flax 	case SRC4XXX_PC_PREAMBLE_LO_2C:
4934e6bedd3SMatt Flax 	case SRC4XXX_IO_RATIO_32:
4944e6bedd3SMatt Flax 	case SRC4XXX_IO_RATIO_33:
4954e6bedd3SMatt Flax 		return true;
4964e6bedd3SMatt Flax 	}
4974e6bedd3SMatt Flax 
4984e6bedd3SMatt Flax 	if (reg > SRC4XXX_IO_RATIO_33 && reg < SRC4XXX_PAGE_SEL_7F)
4994e6bedd3SMatt Flax 		return true;
5004e6bedd3SMatt Flax 
5014e6bedd3SMatt Flax 	return false;
5024e6bedd3SMatt Flax }
5034e6bedd3SMatt Flax 
5044e6bedd3SMatt Flax const struct regmap_config src4xxx_regmap_config = {
5054e6bedd3SMatt Flax 	.val_bits = 8,
5064e6bedd3SMatt Flax 	.reg_bits = 8,
5074e6bedd3SMatt Flax 	.max_register = SRC4XXX_IO_RATIO_33,
5084e6bedd3SMatt Flax 
5094e6bedd3SMatt Flax 	.reg_defaults = src4xxx_reg_defaults,
5104e6bedd3SMatt Flax 	.num_reg_defaults = ARRAY_SIZE(src4xxx_reg_defaults),
5114e6bedd3SMatt Flax 	.volatile_reg = src4xxx_volatile_register,
5124e6bedd3SMatt Flax 	.cache_type = REGCACHE_RBTREE,
5134e6bedd3SMatt Flax };
5144e6bedd3SMatt Flax EXPORT_SYMBOL_GPL(src4xxx_regmap_config);
5154e6bedd3SMatt Flax 
5164e6bedd3SMatt Flax MODULE_DESCRIPTION("ASoC SRC4XXX CODEC driver");
5174e6bedd3SMatt Flax MODULE_AUTHOR("Matt Flax <flatmax@flatmax.com>");
5184e6bedd3SMatt Flax MODULE_LICENSE("GPL");
519