1 // SPDX-License-Identifier: GPL-2.0
3 // TI SRC4xxx Audio Codec driver
5 // Copyright 2021-2022 Deqx Pty Ltd
6 // Author: Matt Flax <flatmax@flatmax.com>
8 #include <linux/module.h>
10 #include <sound/soc.h>
11 #include <sound/tlv.h>
16 struct regmap
*regmap
;
22 enum {SRC4XXX_PORTA
, SRC4XXX_PORTB
};
25 static const DECLARE_TLV_DB_SCALE(src_tlv
, -12750, 50, 0);
27 static const struct snd_kcontrol_new src4xxx_controls
[] = {
28 SOC_DOUBLE_R_TLV("SRC Volume",
29 SRC4XXX_SCR_CTL_30
, SRC4XXX_SCR_CTL_31
, 0, 255, 1, src_tlv
),
32 /* I2S port control */
33 static const char * const port_out_src_text
[] = {
34 "loopback", "other_port", "DIR", "SRC"
36 static SOC_ENUM_SINGLE_DECL(porta_out_src_enum
, SRC4XXX_PORTA_CTL_03
, 4,
38 static SOC_ENUM_SINGLE_DECL(portb_out_src_enum
, SRC4XXX_PORTB_CTL_05
, 4,
40 static const struct snd_kcontrol_new porta_out_control
=
41 SOC_DAPM_ENUM("Port A source select", porta_out_src_enum
);
42 static const struct snd_kcontrol_new portb_out_control
=
43 SOC_DAPM_ENUM("Port B source select", portb_out_src_enum
);
45 /* Digital audio transmitter control */
46 static const char * const dit_mux_text
[] = {"Port A", "Port B", "DIR", "SRC"};
47 static SOC_ENUM_SINGLE_DECL(dit_mux_enum
, SRC4XXX_TX_CTL_07
, 3, dit_mux_text
);
48 static const struct snd_kcontrol_new dit_mux_control
=
49 SOC_DAPM_ENUM("DIT source", dit_mux_enum
);
52 static const char * const src_in_text
[] = {"Port A", "Port B", "DIR"};
53 static SOC_ENUM_SINGLE_DECL(src_in_enum
, SRC4XXX_SCR_CTL_2D
, 0, src_in_text
);
54 static const struct snd_kcontrol_new src_in_control
=
55 SOC_DAPM_ENUM("SRC source select", src_in_enum
);
58 static const char * const dir_in_text
[] = {"Ch 1", "Ch 2", "Ch 3", "Ch 4"};
59 static SOC_ENUM_SINGLE_DECL(dir_in_enum
, SRC4XXX_RCV_CTL_0D
, 0, dir_in_text
);
60 static const struct snd_kcontrol_new dir_in_control
=
61 SOC_DAPM_ENUM("Digital Input", dir_in_enum
);
63 static const struct snd_soc_dapm_widget src4xxx_dapm_widgets
[] = {
64 SND_SOC_DAPM_INPUT("loopback_A"),
65 SND_SOC_DAPM_INPUT("other_port_A"),
66 SND_SOC_DAPM_INPUT("DIR_A"),
67 SND_SOC_DAPM_INPUT("SRC_A"),
68 SND_SOC_DAPM_MUX("Port A source",
69 SND_SOC_NOPM
, 0, 0, &porta_out_control
),
71 SND_SOC_DAPM_INPUT("loopback_B"),
72 SND_SOC_DAPM_INPUT("other_port_B"),
73 SND_SOC_DAPM_INPUT("DIR_B"),
74 SND_SOC_DAPM_INPUT("SRC_B"),
75 SND_SOC_DAPM_MUX("Port B source",
76 SND_SOC_NOPM
, 0, 0, &portb_out_control
),
78 SND_SOC_DAPM_INPUT("Port_A"),
79 SND_SOC_DAPM_INPUT("Port_B"),
80 SND_SOC_DAPM_INPUT("DIR_"),
82 /* Digital audio receivers and transmitters */
83 SND_SOC_DAPM_OUTPUT("DIR_OUT"),
84 SND_SOC_DAPM_OUTPUT("SRC_OUT"),
85 SND_SOC_DAPM_MUX("DIT Out Src", SRC4XXX_PWR_RST_01
,
86 SRC4XXX_ENABLE_DIT_SHIFT
, 1, &dit_mux_control
),
89 SND_SOC_DAPM_AIF_IN("AIF_A_RX", "Playback A", 0,
90 SRC4XXX_PWR_RST_01
, SRC4XXX_ENABLE_PORT_A_SHIFT
, 1),
91 SND_SOC_DAPM_AIF_OUT("AIF_A_TX", "Capture A", 0,
92 SRC4XXX_PWR_RST_01
, SRC4XXX_ENABLE_PORT_A_SHIFT
, 1),
93 SND_SOC_DAPM_AIF_IN("AIF_B_RX", "Playback B", 0,
94 SRC4XXX_PWR_RST_01
, SRC4XXX_ENABLE_PORT_B_SHIFT
, 1),
95 SND_SOC_DAPM_AIF_OUT("AIF_B_TX", "Capture B", 0,
96 SRC4XXX_PWR_RST_01
, SRC4XXX_ENABLE_PORT_B_SHIFT
, 1),
98 SND_SOC_DAPM_MUX("SRC source", SND_SOC_NOPM
, 0, 0, &src_in_control
),
100 SND_SOC_DAPM_INPUT("MCLK"),
101 SND_SOC_DAPM_INPUT("RXMCLKI"),
102 SND_SOC_DAPM_INPUT("RXMCLKO"),
104 SND_SOC_DAPM_INPUT("RX1"),
105 SND_SOC_DAPM_INPUT("RX2"),
106 SND_SOC_DAPM_INPUT("RX3"),
107 SND_SOC_DAPM_INPUT("RX4"),
108 SND_SOC_DAPM_MUX("Digital Input", SRC4XXX_PWR_RST_01
,
109 SRC4XXX_ENABLE_DIR_SHIFT
, 1, &dir_in_control
),
112 static const struct snd_soc_dapm_route src4xxx_audio_routes
[] = {
113 /* I2S Input to Output Routing */
114 {"Port A source", "loopback", "loopback_A"},
115 {"Port A source", "other_port", "other_port_A"},
116 {"Port A source", "DIR", "DIR_A"},
117 {"Port A source", "SRC", "SRC_A"},
118 {"Port B source", "loopback", "loopback_B"},
119 {"Port B source", "other_port", "other_port_B"},
120 {"Port B source", "DIR", "DIR_B"},
121 {"Port B source", "SRC", "SRC_B"},
123 {"DIT Out Src", "Port A", "Capture A"},
124 {"DIT Out Src", "Port B", "Capture B"},
125 {"DIT Out Src", "DIR", "DIR_OUT"},
126 {"DIT Out Src", "SRC", "SRC_OUT"},
128 /* SRC input selection */
129 {"SRC source", "Port A", "Port_A"},
130 {"SRC source", "Port B", "Port_B"},
131 {"SRC source", "DIR", "DIR_"},
132 /* SRC mclk selection */
133 {"SRC mclk source", "Master (MCLK)", "MCLK"},
134 {"SRC mclk source", "Master (RXCLKI)", "RXMCLKI"},
135 {"SRC mclk source", "Recovered receiver clk", "RXMCLKO"},
136 /* DIR input selection */
137 {"Digital Input", "Ch 1", "RX1"},
138 {"Digital Input", "Ch 2", "RX2"},
139 {"Digital Input", "Ch 3", "RX3"},
140 {"Digital Input", "Ch 4", "RX4"},
144 static const struct snd_soc_component_driver src4xxx_driver
= {
145 .controls
= src4xxx_controls
,
146 .num_controls
= ARRAY_SIZE(src4xxx_controls
),
148 .dapm_widgets
= src4xxx_dapm_widgets
,
149 .num_dapm_widgets
= ARRAY_SIZE(src4xxx_dapm_widgets
),
150 .dapm_routes
= src4xxx_audio_routes
,
151 .num_dapm_routes
= ARRAY_SIZE(src4xxx_audio_routes
),
154 static int src4xxx_set_dai_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
156 struct snd_soc_component
*component
= dai
->component
;
157 struct src4xxx
*src4xxx
= snd_soc_component_get_drvdata(component
);
160 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
161 case SND_SOC_DAIFMT_CBM_CFM
:
162 ctrl
= SRC4XXX_BUS_MASTER
;
163 src4xxx
->master
[dai
->id
] = true;
165 case SND_SOC_DAIFMT_CBS_CFS
:
167 src4xxx
->master
[dai
->id
] = false;
174 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
175 case SND_SOC_DAIFMT_I2S
:
176 ctrl
|= SRC4XXX_BUS_I2S
;
178 case SND_SOC_DAIFMT_LEFT_J
:
179 ctrl
|= SRC4XXX_BUS_LEFT_J
;
181 case SND_SOC_DAIFMT_RIGHT_J
:
182 ctrl
|= SRC4XXX_BUS_RIGHT_J_24
;
189 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
190 case SND_SOC_DAIFMT_NB_NF
:
197 regmap_update_bits(src4xxx
->regmap
, SRC4XXX_BUS_FMT(dai
->id
),
198 SRC4XXX_BUS_FMT_MS_MASK
, ctrl
);
203 static int src4xxx_set_mclk_hz(struct snd_soc_dai
*codec_dai
,
204 int clk_id
, unsigned int freq
, int dir
)
206 struct snd_soc_component
*component
= codec_dai
->component
;
207 struct src4xxx
*src4xxx
= snd_soc_component_get_drvdata(component
);
209 dev_info(component
->dev
, "changing mclk rate from %d to %d Hz\n",
210 src4xxx
->mclk_hz
, freq
);
211 src4xxx
->mclk_hz
= freq
;
216 static int src4xxx_hw_params(struct snd_pcm_substream
*substream
,
217 struct snd_pcm_hw_params
*params
,
218 struct snd_soc_dai
*dai
)
220 struct snd_soc_component
*component
= dai
->component
;
221 struct src4xxx
*src4xxx
= snd_soc_component_get_drvdata(component
);
222 unsigned int mclk_div
;
229 reg
= SRC4XXX_PORTB_CTL_06
;
232 reg
= SRC4XXX_PORTA_CTL_04
;
236 if (src4xxx
->master
[dai
->id
]) {
237 mclk_div
= src4xxx
->mclk_hz
/params_rate(params
);
238 if (src4xxx
->mclk_hz
!= mclk_div
*params_rate(params
)) {
239 dev_err(component
->dev
,
240 "mclk %d / rate %d has a remainder.\n",
241 src4xxx
->mclk_hz
, params_rate(params
));
245 val
= ((int)mclk_div
- 128) / 128;
246 if ((val
< 0) | (val
> 3)) {
247 dev_err(component
->dev
,
248 "div register setting %d is out of range\n",
250 dev_err(component
->dev
,
251 "unsupported sample rate %d Hz for the master clock of %d Hz\n",
252 params_rate(params
), src4xxx
->mclk_hz
);
257 ret
= regmap_update_bits(src4xxx
->regmap
,
258 SRC4XXX_TX_CTL_07
, SRC4XXX_TX_MCLK_DIV_MASK
,
259 val
<<SRC4XXX_TX_MCLK_DIV_SHIFT
);
261 dev_err(component
->dev
,
262 "Couldn't set the TX's div register to %d << %d = 0x%x\n",
263 val
, SRC4XXX_TX_MCLK_DIV_SHIFT
,
264 val
<<SRC4XXX_TX_MCLK_DIV_SHIFT
);
268 /* set the PLL for the digital receiver */
269 switch (src4xxx
->mclk_hz
) {
281 /* don't error out here,
282 * other parts of the chip are still functional
283 * Dummy initialize variables to avoid
284 * -Wsometimes-uninitialized from clang.
286 dev_info(component
->dev
,
287 "Couldn't set the RCV PLL as this master clock rate is unknown. Chosen regmap values may not match real world values.\n");
293 ret
= regmap_write(src4xxx
->regmap
, SRC4XXX_RCV_PLL_0F
, pj
);
295 dev_err(component
->dev
,
296 "Failed to update PLL register 0x%x\n",
298 ret
= regmap_write(src4xxx
->regmap
, SRC4XXX_RCV_PLL_10
, jd
);
300 dev_err(component
->dev
,
301 "Failed to update PLL register 0x%x\n",
303 ret
= regmap_write(src4xxx
->regmap
, SRC4XXX_RCV_PLL_11
, d
);
305 dev_err(component
->dev
,
306 "Failed to update PLL register 0x%x\n",
309 ret
= regmap_update_bits(src4xxx
->regmap
,
310 SRC4XXX_TX_CTL_07
, SRC4XXX_TX_MCLK_DIV_MASK
,
311 val
<<SRC4XXX_TX_MCLK_DIV_SHIFT
);
313 dev_err(component
->dev
,
314 "Couldn't set the TX's div register to %d << %d = 0x%x\n",
315 val
, SRC4XXX_TX_MCLK_DIV_SHIFT
,
316 val
<<SRC4XXX_TX_MCLK_DIV_SHIFT
);
320 return regmap_update_bits(src4xxx
->regmap
, reg
,
321 SRC4XXX_MCLK_DIV_MASK
, val
);
323 dev_info(dai
->dev
, "not setting up MCLK as not master\n");
329 static const struct snd_soc_dai_ops src4xxx_dai_ops
= {
330 .hw_params
= src4xxx_hw_params
,
331 .set_sysclk
= src4xxx_set_mclk_hz
,
332 .set_fmt
= src4xxx_set_dai_fmt
,
335 #define SRC4XXX_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
336 #define SRC4XXX_RATES (SNDRV_PCM_RATE_44100|SNDRV_PCM_RATE_48000|\
337 SNDRV_PCM_RATE_88200|\
338 SNDRV_PCM_RATE_96000|\
339 SNDRV_PCM_RATE_176400|\
340 SNDRV_PCM_RATE_192000)
342 static struct snd_soc_dai_driver src4xxx_dai_driver
[] = {
345 .name
= "src4xxx-portA",
347 .stream_name
= "Playback A",
350 .rates
= SRC4XXX_RATES
,
351 .formats
= SRC4XXX_FORMATS
,
354 .stream_name
= "Capture A",
357 .rates
= SRC4XXX_RATES
,
358 .formats
= SRC4XXX_FORMATS
,
360 .ops
= &src4xxx_dai_ops
,
364 .name
= "src4xxx-portB",
366 .stream_name
= "Playback B",
369 .rates
= SRC4XXX_RATES
,
370 .formats
= SRC4XXX_FORMATS
,
373 .stream_name
= "Capture B",
376 .rates
= SRC4XXX_RATES
,
377 .formats
= SRC4XXX_FORMATS
,
379 .ops
= &src4xxx_dai_ops
,
383 static const struct reg_default src4xxx_reg_defaults
[] = {
384 { SRC4XXX_PWR_RST_01
, 0x00 }, /* all powered down intially */
385 { SRC4XXX_PORTA_CTL_03
, 0x00 },
386 { SRC4XXX_PORTA_CTL_04
, 0x00 },
387 { SRC4XXX_PORTB_CTL_05
, 0x00 },
388 { SRC4XXX_PORTB_CTL_06
, 0x00 },
389 { SRC4XXX_TX_CTL_07
, 0x00 },
390 { SRC4XXX_TX_CTL_08
, 0x00 },
391 { SRC4XXX_TX_CTL_09
, 0x00 },
392 { SRC4XXX_SRC_DIT_IRQ_MSK_0B
, 0x00 },
393 { SRC4XXX_SRC_DIT_IRQ_MODE_0C
, 0x00 },
394 { SRC4XXX_RCV_CTL_0D
, 0x00 },
395 { SRC4XXX_RCV_CTL_0E
, 0x00 },
396 { SRC4XXX_RCV_PLL_0F
, 0x00 }, /* not spec. in the datasheet */
397 { SRC4XXX_RCV_PLL_10
, 0xff }, /* not spec. in the datasheet */
398 { SRC4XXX_RCV_PLL_11
, 0xff }, /* not spec. in the datasheet */
399 { SRC4XXX_RVC_IRQ_MSK_16
, 0x00 },
400 { SRC4XXX_RVC_IRQ_MSK_17
, 0x00 },
401 { SRC4XXX_RVC_IRQ_MODE_18
, 0x00 },
402 { SRC4XXX_RVC_IRQ_MODE_19
, 0x00 },
403 { SRC4XXX_RVC_IRQ_MODE_1A
, 0x00 },
404 { SRC4XXX_GPIO_1_1B
, 0x00 },
405 { SRC4XXX_GPIO_2_1C
, 0x00 },
406 { SRC4XXX_GPIO_3_1D
, 0x00 },
407 { SRC4XXX_GPIO_4_1E
, 0x00 },
408 { SRC4XXX_SCR_CTL_2D
, 0x00 },
409 { SRC4XXX_SCR_CTL_2E
, 0x00 },
410 { SRC4XXX_SCR_CTL_2F
, 0x00 },
411 { SRC4XXX_SCR_CTL_30
, 0x00 },
412 { SRC4XXX_SCR_CTL_31
, 0x00 },
415 int src4xxx_probe(struct device
*dev
, struct regmap
*regmap
,
416 void (*switch_mode
)(struct device
*dev
))
418 struct src4xxx
*src4xxx
;
422 return PTR_ERR(regmap
);
424 src4xxx
= devm_kzalloc(dev
, sizeof(*src4xxx
), GFP_KERNEL
);
428 src4xxx
->regmap
= regmap
;
430 src4xxx
->mclk_hz
= 0; /* mclk has not been configured yet */
431 dev_set_drvdata(dev
, src4xxx
);
433 ret
= regmap_write(regmap
, SRC4XXX_PWR_RST_01
, SRC4XXX_RESET
);
435 dev_err(dev
, "Failed to issue reset: %d\n", ret
);
436 usleep_range(1, 500); /* sleep for more then 500 ns */
437 ret
= regmap_write(regmap
, SRC4XXX_PWR_RST_01
, SRC4XXX_POWER_DOWN
);
439 dev_err(dev
, "Failed to decommission reset: %d\n", ret
);
440 usleep_range(500, 1000); /* sleep for 500 us or more */
442 ret
= regmap_update_bits(src4xxx
->regmap
, SRC4XXX_PWR_RST_01
,
443 SRC4XXX_POWER_ENABLE
, SRC4XXX_POWER_ENABLE
);
445 dev_err(dev
, "Failed to port A and B : %d\n", ret
);
447 /* set receiver to use master clock (rcv mclk is most likely jittery) */
448 ret
= regmap_update_bits(src4xxx
->regmap
, SRC4XXX_RCV_CTL_0D
,
449 SRC4XXX_RXCLK_MCLK
, SRC4XXX_RXCLK_MCLK
);
452 "Failed to enable mclk as the PLL1 DIR reference : %d\n", ret
);
454 /* default to leaving the PLL2 running on loss of lock, divide by 8 */
455 ret
= regmap_update_bits(src4xxx
->regmap
, SRC4XXX_RCV_CTL_0E
,
456 SRC4XXX_PLL2_DIV_8
| SRC4XXX_REC_MCLK_EN
| SRC4XXX_PLL2_LOL
,
457 SRC4XXX_PLL2_DIV_8
| SRC4XXX_REC_MCLK_EN
| SRC4XXX_PLL2_LOL
);
459 dev_err(dev
, "Failed to enable mclk rec and div : %d\n", ret
);
461 ret
= devm_snd_soc_register_component(dev
, &src4xxx_driver
,
462 src4xxx_dai_driver
, ARRAY_SIZE(src4xxx_dai_driver
));
464 dev_info(dev
, "src4392 probe ok %d\n", ret
);
467 EXPORT_SYMBOL_GPL(src4xxx_probe
);
469 static bool src4xxx_volatile_register(struct device
*dev
, unsigned int reg
)
473 case SRC4XXX_GLOBAL_ITR_STS_02
:
474 case SRC4XXX_SRC_DIT_STS_0A
:
475 case SRC4XXX_NON_AUDIO_D_12
:
476 case SRC4XXX_RVC_STS_13
:
477 case SRC4XXX_RVC_STS_14
:
478 case SRC4XXX_RVC_STS_15
:
479 case SRC4XXX_SUB_CODE_1F
:
480 case SRC4XXX_SUB_CODE_20
:
481 case SRC4XXX_SUB_CODE_21
:
482 case SRC4XXX_SUB_CODE_22
:
483 case SRC4XXX_SUB_CODE_23
:
484 case SRC4XXX_SUB_CODE_24
:
485 case SRC4XXX_SUB_CODE_25
:
486 case SRC4XXX_SUB_CODE_26
:
487 case SRC4XXX_SUB_CODE_27
:
488 case SRC4XXX_SUB_CODE_28
:
489 case SRC4XXX_PC_PREAMBLE_HI_29
:
490 case SRC4XXX_PC_PREAMBLE_LO_2A
:
491 case SRC4XXX_PD_PREAMBLE_HI_2B
:
492 case SRC4XXX_PC_PREAMBLE_LO_2C
:
493 case SRC4XXX_IO_RATIO_32
:
494 case SRC4XXX_IO_RATIO_33
:
498 if (reg
> SRC4XXX_IO_RATIO_33
&& reg
< SRC4XXX_PAGE_SEL_7F
)
504 const struct regmap_config src4xxx_regmap_config
= {
507 .max_register
= SRC4XXX_IO_RATIO_33
,
509 .reg_defaults
= src4xxx_reg_defaults
,
510 .num_reg_defaults
= ARRAY_SIZE(src4xxx_reg_defaults
),
511 .volatile_reg
= src4xxx_volatile_register
,
512 .cache_type
= REGCACHE_RBTREE
,
514 EXPORT_SYMBOL_GPL(src4xxx_regmap_config
);
516 MODULE_DESCRIPTION("ASoC SRC4XXX CODEC driver");
517 MODULE_AUTHOR("Matt Flax <flatmax@flatmax.com>");
518 MODULE_LICENSE("GPL");