1 // SPDX-License-Identifier: GPL-2.0-only
3 // rt1019.c -- RT1019 ALSA SoC audio amplifier driver
4 // Author: Jack Yu <jack.yu@realtek.com>
6 // Copyright(c) 2021 Realtek Semiconductor Corp.
10 #include <linux/acpi.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
17 #include <linux/regmap.h>
18 #include <linux/i2c.h>
19 #include <linux/platform_device.h>
20 #include <linux/firmware.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include <sound/soc-dapm.h>
26 #include <sound/initval.h>
27 #include <sound/tlv.h>
32 static const struct reg_default rt1019_reg
[] = {
59 static bool rt1019_volatile_register(struct device
*dev
, unsigned int reg
)
62 case RT1019_PWR_STRP_2
:
64 case RT1019_VEND_ID_1
:
65 case RT1019_VEND_ID_2
:
75 static bool rt1019_readable_register(struct device
*dev
, unsigned int reg
)
80 case RT1019_ASEL_CTRL
:
81 case RT1019_PWR_STRP_2
:
82 case RT1019_BEEP_TONE
:
84 case RT1019_VEND_ID_1
:
85 case RT1019_VEND_ID_2
:
89 case RT1019_CLK_TREE_1
:
90 case RT1019_CLK_TREE_2
:
91 case RT1019_CLK_TREE_3
:
98 case RT1019_DMIX_MONO_1
:
99 case RT1019_DMIX_MONO_2
:
108 static const DECLARE_TLV_DB_SCALE(dac_vol_tlv
, -9525, 75, 0);
110 static const char * const rt1019_din_source_select
[] = {
113 "Left + Right average",
116 static SOC_ENUM_SINGLE_DECL(rt1019_mono_lr_sel
, RT1019_IDS_CTRL
, 0,
117 rt1019_din_source_select
);
119 static const struct snd_kcontrol_new rt1019_snd_controls
[] = {
120 SOC_SINGLE_TLV("DAC Playback Volume", RT1019_DMIX_MONO_1
, 0,
121 127, 0, dac_vol_tlv
),
122 SOC_ENUM("Mono LR Select", rt1019_mono_lr_sel
),
125 static int r1019_dac_event(struct snd_soc_dapm_widget
*w
,
126 struct snd_kcontrol
*kcontrol
, int event
)
128 struct snd_soc_component
*component
= snd_soc_dapm_to_component(w
->dapm
);
131 case SND_SOC_DAPM_PRE_PMU
:
132 snd_soc_component_write(component
, RT1019_SDB_CTRL
, 0xb);
134 case SND_SOC_DAPM_POST_PMD
:
135 snd_soc_component_write(component
, RT1019_SDB_CTRL
, 0xa);
144 static const struct snd_soc_dapm_widget rt1019_dapm_widgets
[] = {
145 SND_SOC_DAPM_AIF_IN("AIFRX", "AIF Playback", 0, SND_SOC_NOPM
, 0, 0),
146 SND_SOC_DAPM_DAC_E("DAC", NULL
, SND_SOC_NOPM
, 0, 0,
147 r1019_dac_event
, SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMD
),
148 SND_SOC_DAPM_OUTPUT("SPO"),
151 static const struct snd_soc_dapm_route rt1019_dapm_routes
[] = {
152 { "DAC", NULL
, "AIFRX" },
153 { "SPO", NULL
, "DAC" },
156 static int rt1019_hw_params(struct snd_pcm_substream
*substream
,
157 struct snd_pcm_hw_params
*params
, struct snd_soc_dai
*dai
)
159 struct snd_soc_component
*component
= dai
->component
;
160 struct rt1019_priv
*rt1019
= snd_soc_component_get_drvdata(component
);
161 int pre_div
, bclk_ms
, frame_size
;
162 unsigned int val_len
= 0, sys_div_da_filter
= 0;
163 unsigned int sys_dac_osr
= 0, sys_fifo_clk
= 0;
164 unsigned int sys_clk_cal
= 0, sys_asrc_in
= 0;
166 rt1019
->lrck
= params_rate(params
);
167 pre_div
= rl6231_get_clk_info(rt1019
->sysclk
, rt1019
->lrck
);
169 dev_err(component
->dev
, "Unsupported clock setting\n");
173 frame_size
= snd_soc_params_to_frame_size(params
);
174 if (frame_size
< 0) {
175 dev_err(component
->dev
, "Unsupported frame size: %d\n", frame_size
);
179 bclk_ms
= frame_size
> 32;
180 rt1019
->bclk
= rt1019
->lrck
* (32 << bclk_ms
);
182 dev_dbg(dai
->dev
, "bclk is %dHz and lrck is %dHz\n",
183 rt1019
->bclk
, rt1019
->lrck
);
184 dev_dbg(dai
->dev
, "bclk_ms is %d and pre_div is %d for iis %d\n",
185 bclk_ms
, pre_div
, dai
->id
);
189 sys_div_da_filter
= RT1019_SYS_DIV_DA_FIL_DIV1
;
190 sys_dac_osr
= RT1019_SYS_DA_OSR_DIV1
;
191 sys_asrc_in
= RT1019_ASRC_256FS_DIV1
;
192 sys_fifo_clk
= RT1019_SEL_FIFO_DIV1
;
193 sys_clk_cal
= RT1019_SEL_CLK_CAL_DIV1
;
196 sys_div_da_filter
= RT1019_SYS_DIV_DA_FIL_DIV2
;
197 sys_dac_osr
= RT1019_SYS_DA_OSR_DIV2
;
198 sys_asrc_in
= RT1019_ASRC_256FS_DIV2
;
199 sys_fifo_clk
= RT1019_SEL_FIFO_DIV2
;
200 sys_clk_cal
= RT1019_SEL_CLK_CAL_DIV2
;
203 sys_div_da_filter
= RT1019_SYS_DIV_DA_FIL_DIV4
;
204 sys_dac_osr
= RT1019_SYS_DA_OSR_DIV4
;
205 sys_asrc_in
= RT1019_ASRC_256FS_DIV4
;
206 sys_fifo_clk
= RT1019_SEL_FIFO_DIV4
;
207 sys_clk_cal
= RT1019_SEL_CLK_CAL_DIV4
;
213 switch (params_width(params
)) {
217 val_len
= RT1019_I2S_DL_20
;
220 val_len
= RT1019_I2S_DL_24
;
223 val_len
= RT1019_I2S_DL_32
;
226 val_len
= RT1019_I2S_DL_8
;
232 snd_soc_component_update_bits(component
, RT1019_TDM_2
, RT1019_I2S_DL_MASK
,
234 snd_soc_component_update_bits(component
, RT1019_CLK_TREE_1
,
235 RT1019_SEL_FIFO_MASK
, sys_fifo_clk
);
236 snd_soc_component_update_bits(component
, RT1019_CLK_TREE_2
,
237 RT1019_SYS_DIV_DA_FIL_MASK
| RT1019_SYS_DA_OSR_MASK
|
238 RT1019_ASRC_256FS_MASK
, sys_div_da_filter
| sys_dac_osr
|
240 snd_soc_component_update_bits(component
, RT1019_CLK_TREE_3
,
241 RT1019_SEL_CLK_CAL_MASK
, sys_clk_cal
);
246 static int rt1019_set_dai_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
248 struct snd_soc_component
*component
= dai
->component
;
249 unsigned int reg_val
= 0, reg_val2
= 0;
251 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
252 case SND_SOC_DAIFMT_NB_NF
:
254 case SND_SOC_DAIFMT_IB_NF
:
255 reg_val2
|= RT1019_TDM_BCLK_INV
;
261 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
262 case SND_SOC_DAIFMT_I2S
:
265 case SND_SOC_DAIFMT_LEFT_J
:
266 reg_val
|= RT1019_I2S_DF_LEFT
;
269 case SND_SOC_DAIFMT_DSP_A
:
270 reg_val
|= RT1019_I2S_DF_PCM_A_R
;
273 case SND_SOC_DAIFMT_DSP_B
:
274 reg_val
|= RT1019_I2S_DF_PCM_B_R
;
281 snd_soc_component_update_bits(component
, RT1019_TDM_2
,
282 RT1019_I2S_DF_MASK
, reg_val
);
283 snd_soc_component_update_bits(component
, RT1019_TDM_1
,
284 RT1019_TDM_BCLK_MASK
, reg_val2
);
289 static int rt1019_set_dai_sysclk(struct snd_soc_dai
*dai
,
290 int clk_id
, unsigned int freq
, int dir
)
292 struct snd_soc_component
*component
= dai
->component
;
293 struct rt1019_priv
*rt1019
= snd_soc_component_get_drvdata(component
);
294 unsigned int reg_val
= 0;
296 if (freq
== rt1019
->sysclk
&& clk_id
== rt1019
->sysclk_src
)
300 case RT1019_SCLK_S_BCLK
:
301 reg_val
|= RT1019_CLK_SYS_PRE_SEL_BCLK
;
304 case RT1019_SCLK_S_PLL
:
305 reg_val
|= RT1019_CLK_SYS_PRE_SEL_PLL
;
309 dev_err(component
->dev
, "Invalid clock id (%d)\n", clk_id
);
313 rt1019
->sysclk
= freq
;
314 rt1019
->sysclk_src
= clk_id
;
316 dev_dbg(dai
->dev
, "Sysclk is %dHz and clock id is %d\n", freq
, clk_id
);
318 snd_soc_component_update_bits(component
, RT1019_CLK_TREE_1
,
319 RT1019_CLK_SYS_PRE_SEL_MASK
, reg_val
);
324 static int rt1019_set_dai_pll(struct snd_soc_dai
*dai
, int pll_id
, int source
,
325 unsigned int freq_in
, unsigned int freq_out
)
327 struct snd_soc_component
*component
= dai
->component
;
328 struct rt1019_priv
*rt1019
= snd_soc_component_get_drvdata(component
);
329 struct rl6231_pll_code pll_code
;
332 if (!freq_in
|| !freq_out
) {
333 dev_dbg(component
->dev
, "PLL disabled\n");
339 if (source
== rt1019
->pll_src
&& freq_in
== rt1019
->pll_in
&&
340 freq_out
== rt1019
->pll_out
)
344 case RT1019_PLL_S_BCLK
:
345 snd_soc_component_update_bits(component
, RT1019_CLK_TREE_1
,
346 RT1019_PLL_SRC_MASK
, RT1019_PLL_SRC_SEL_BCLK
);
349 case RT1019_PLL_S_RC25M
:
350 snd_soc_component_update_bits(component
, RT1019_CLK_TREE_1
,
351 RT1019_PLL_SRC_MASK
, RT1019_PLL_SRC_SEL_RC
);
355 dev_err(component
->dev
, "Unknown PLL source %d\n", source
);
359 ret
= rl6231_pll_calc(freq_in
, freq_out
, &pll_code
);
361 dev_err(component
->dev
, "Unsupported input clock %d\n", freq_in
);
365 dev_dbg(component
->dev
, "bypass=%d m=%d n=%d k=%d\n",
366 pll_code
.m_bp
, (pll_code
.m_bp
? 0 : pll_code
.m_code
),
367 pll_code
.n_code
, pll_code
.k_code
);
369 snd_soc_component_update_bits(component
, RT1019_PWR_STRP_2
,
370 RT1019_AUTO_BITS_SEL_MASK
| RT1019_AUTO_CLK_SEL_MASK
,
371 RT1019_AUTO_BITS_SEL_MANU
| RT1019_AUTO_CLK_SEL_MANU
);
372 snd_soc_component_update_bits(component
, RT1019_PLL_1
,
373 RT1019_PLL_M_MASK
| RT1019_PLL_M_BP_MASK
| RT1019_PLL_Q_8_8_MASK
,
374 ((pll_code
.m_bp
? 0 : pll_code
.m_code
) << RT1019_PLL_M_SFT
) |
375 (pll_code
.m_bp
<< RT1019_PLL_M_BP_SFT
) |
376 ((pll_code
.n_code
>> 8) & RT1019_PLL_Q_8_8_MASK
));
377 snd_soc_component_update_bits(component
, RT1019_PLL_2
,
378 RT1019_PLL_Q_7_0_MASK
, pll_code
.n_code
& RT1019_PLL_Q_7_0_MASK
);
379 snd_soc_component_update_bits(component
, RT1019_PLL_3
,
380 RT1019_PLL_K_MASK
, pll_code
.k_code
);
382 rt1019
->pll_in
= freq_in
;
383 rt1019
->pll_out
= freq_out
;
384 rt1019
->pll_src
= source
;
389 static int rt1019_set_tdm_slot(struct snd_soc_dai
*dai
, unsigned int tx_mask
,
390 unsigned int rx_mask
, int slots
, int slot_width
)
392 struct snd_soc_component
*component
= dai
->component
;
393 unsigned int cn
= 0, cl
= 0, rx_slotnum
;
394 int ret
= 0, first_bit
;
398 cn
= RT1019_I2S_TX_4CH
;
401 cn
= RT1019_I2S_TX_6CH
;
404 cn
= RT1019_I2S_TX_8CH
;
412 switch (slot_width
) {
414 cl
= RT1019_TDM_CL_20
;
417 cl
= RT1019_TDM_CL_24
;
420 cl
= RT1019_TDM_CL_32
;
423 cl
= RT1019_TDM_CL_8
;
431 /* Rx slot configuration */
432 rx_slotnum
= hweight_long(rx_mask
);
433 if (rx_slotnum
!= 1) {
435 dev_err(component
->dev
, "too many rx slots or zero slot\n");
438 /* This is an assumption that the system sends stereo audio to the
439 * amplifier typically. And the stereo audio is placed in slot 0/2/4/6
440 * as the starting slot. The users could select the channel from
441 * L/R/L+R by "Mono LR Select" control.
443 first_bit
= __ffs(rx_mask
);
449 snd_soc_component_update_bits(component
,
451 RT1019_TDM_I2S_TX_L_DAC1_1_MASK
|
452 RT1019_TDM_I2S_TX_R_DAC1_1_MASK
,
453 (first_bit
<< RT1019_TDM_I2S_TX_L_DAC1_1_SFT
) |
454 ((first_bit
+ 1) << RT1019_TDM_I2S_TX_R_DAC1_1_SFT
));
460 snd_soc_component_update_bits(component
,
462 RT1019_TDM_I2S_TX_L_DAC1_1_MASK
|
463 RT1019_TDM_I2S_TX_R_DAC1_1_MASK
,
464 ((first_bit
- 1) << RT1019_TDM_I2S_TX_L_DAC1_1_SFT
) |
465 (first_bit
<< RT1019_TDM_I2S_TX_R_DAC1_1_SFT
));
472 snd_soc_component_update_bits(component
, RT1019_TDM_1
,
473 RT1019_TDM_CL_MASK
, cl
);
474 snd_soc_component_update_bits(component
, RT1019_TDM_2
,
475 RT1019_I2S_CH_TX_MASK
, cn
);
481 static int rt1019_probe(struct snd_soc_component
*component
)
483 struct rt1019_priv
*rt1019
= snd_soc_component_get_drvdata(component
);
485 rt1019
->component
= component
;
486 snd_soc_component_write(component
, RT1019_SDB_CTRL
, 0xa);
491 #define RT1019_STEREO_RATES SNDRV_PCM_RATE_8000_192000
492 #define RT1019_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
493 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
495 static const struct snd_soc_dai_ops rt1019_aif_dai_ops
= {
496 .hw_params
= rt1019_hw_params
,
497 .set_fmt
= rt1019_set_dai_fmt
,
498 .set_sysclk
= rt1019_set_dai_sysclk
,
499 .set_pll
= rt1019_set_dai_pll
,
500 .set_tdm_slot
= rt1019_set_tdm_slot
,
503 static struct snd_soc_dai_driver rt1019_dai
[] = {
505 .name
= "rt1019-aif",
508 .stream_name
= "AIF Playback",
511 .rates
= RT1019_STEREO_RATES
,
512 .formats
= RT1019_FORMATS
,
514 .ops
= &rt1019_aif_dai_ops
,
518 static const struct snd_soc_component_driver soc_component_dev_rt1019
= {
519 .probe
= rt1019_probe
,
520 .controls
= rt1019_snd_controls
,
521 .num_controls
= ARRAY_SIZE(rt1019_snd_controls
),
522 .dapm_widgets
= rt1019_dapm_widgets
,
523 .num_dapm_widgets
= ARRAY_SIZE(rt1019_dapm_widgets
),
524 .dapm_routes
= rt1019_dapm_routes
,
525 .num_dapm_routes
= ARRAY_SIZE(rt1019_dapm_routes
),
529 static const struct regmap_config rt1019_regmap
= {
532 .use_single_read
= true,
533 .use_single_write
= true,
534 .max_register
= RT1019_BEEP_2
,
535 .volatile_reg
= rt1019_volatile_register
,
536 .readable_reg
= rt1019_readable_register
,
537 .cache_type
= REGCACHE_MAPLE
,
538 .reg_defaults
= rt1019_reg
,
539 .num_reg_defaults
= ARRAY_SIZE(rt1019_reg
),
542 static const struct i2c_device_id rt1019_i2c_id
[] = {
546 MODULE_DEVICE_TABLE(i2c
, rt1019_i2c_id
);
548 static const struct of_device_id rt1019_of_match
[] __maybe_unused
= {
549 { .compatible
= "realtek,rt1019", },
552 MODULE_DEVICE_TABLE(of
, rt1019_of_match
);
555 static const struct acpi_device_id rt1019_acpi_match
[] = {
559 MODULE_DEVICE_TABLE(acpi
, rt1019_acpi_match
);
562 static int rt1019_i2c_probe(struct i2c_client
*i2c
)
564 struct rt1019_priv
*rt1019
;
566 unsigned int val
, val2
, dev_id
;
568 rt1019
= devm_kzalloc(&i2c
->dev
, sizeof(struct rt1019_priv
),
573 i2c_set_clientdata(i2c
, rt1019
);
575 rt1019
->regmap
= devm_regmap_init_i2c(i2c
, &rt1019_regmap
);
576 if (IS_ERR(rt1019
->regmap
)) {
577 ret
= PTR_ERR(rt1019
->regmap
);
578 dev_err(&i2c
->dev
, "Failed to allocate register map: %d\n",
583 regmap_read(rt1019
->regmap
, RT1019_DEV_ID_1
, &val
);
584 regmap_read(rt1019
->regmap
, RT1019_DEV_ID_2
, &val2
);
585 dev_id
= val
<< 8 | val2
;
586 if (dev_id
!= RT1019_DEVICE_ID_VAL
&& dev_id
!= RT1019_DEVICE_ID_VAL2
) {
588 "Device with ID register 0x%x is not rt1019\n", dev_id
);
592 return devm_snd_soc_register_component(&i2c
->dev
,
593 &soc_component_dev_rt1019
, rt1019_dai
, ARRAY_SIZE(rt1019_dai
));
596 static struct i2c_driver rt1019_i2c_driver
= {
599 .of_match_table
= of_match_ptr(rt1019_of_match
),
600 .acpi_match_table
= ACPI_PTR(rt1019_acpi_match
),
602 .probe
= rt1019_i2c_probe
,
603 .id_table
= rt1019_i2c_id
,
605 module_i2c_driver(rt1019_i2c_driver
);
607 MODULE_DESCRIPTION("ASoC RT1019 driver");
608 MODULE_AUTHOR("Jack Yu <jack.yu@realtek.com>");
609 MODULE_LICENSE("GPL v2");