1 // SPDX-License-Identifier: GPL-2.0
3 // rt1016.c -- RT1016 ALSA SoC audio amplifier driver
5 // Copyright 2020 Realtek Semiconductor Corp.
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
15 #include <linux/regmap.h>
16 #include <linux/i2c.h>
17 #include <linux/platform_device.h>
18 #include <linux/firmware.h>
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
23 #include <sound/soc-dapm.h>
24 #include <sound/initval.h>
25 #include <sound/tlv.h>
30 static const struct reg_sequence rt1016_patch[] = {
31 {RT1016_VOL_CTRL_3, 0x8900},
32 {RT1016_ANA_CTRL_1, 0xa002},
33 {RT1016_ANA_CTRL_2, 0x0002},
34 {RT1016_CLOCK_4, 0x6700},
35 {RT1016_CLASSD_3, 0xdc55},
36 {RT1016_CLASSD_4, 0x376a},
37 {RT1016_CLASSD_5, 0x009f},
40 static const struct reg_default rt1016_reg[] = {
91 static bool rt1016_volatile_register(struct device *dev, unsigned int reg)
95 case RT1016_VERSION2_ID:
96 case RT1016_VERSION1_ID:
97 case RT1016_VENDER_ID:
98 case RT1016_DEVICE_ID:
99 case RT1016_TEST_SIGNAL:
100 case RT1016_SC_CTRL_1:
108 static bool rt1016_readable_register(struct device *dev, unsigned int reg)
112 case RT1016_PADS_CTRL_1:
113 case RT1016_PADS_CTRL_2:
114 case RT1016_I2C_CTRL:
115 case RT1016_VOL_CTRL_1:
116 case RT1016_VOL_CTRL_2:
117 case RT1016_VOL_CTRL_3:
118 case RT1016_ANA_CTRL_1:
120 case RT1016_RX_I2S_CTRL:
121 case RT1016_ANA_FLAG:
122 case RT1016_VERSION2_ID:
123 case RT1016_VERSION1_ID:
124 case RT1016_VENDER_ID:
125 case RT1016_DEVICE_ID:
126 case RT1016_ANA_CTRL_2:
127 case RT1016_TEST_SIGNAL:
128 case RT1016_TEST_CTRL_1:
129 case RT1016_TEST_CTRL_2:
130 case RT1016_TEST_CTRL_3:
138 case RT1016_I2S_CTRL:
139 case RT1016_DAC_CTRL_1:
140 case RT1016_SC_CTRL_1:
141 case RT1016_SC_CTRL_2:
142 case RT1016_SC_CTRL_3:
143 case RT1016_SC_CTRL_4:
146 case RT1016_BIAS_CUR:
147 case RT1016_DAC_CTRL_2:
148 case RT1016_LDO_CTRL:
149 case RT1016_CLASSD_1:
153 case RT1016_CLASSD_2:
154 case RT1016_CLASSD_OUT:
155 case RT1016_CLASSD_3:
156 case RT1016_CLASSD_4:
157 case RT1016_CLASSD_5:
158 case RT1016_PWR_CTRL:
166 static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -9550, 50, 0);
168 static const struct snd_kcontrol_new rt1016_snd_controls[] = {
169 SOC_DOUBLE_TLV("DAC Playback Volume", RT1016_VOL_CTRL_2,
170 RT1016_L_VOL_SFT, RT1016_R_VOL_SFT, 191, 0, dac_vol_tlv),
171 SOC_DOUBLE("DAC Playback Switch", RT1016_VOL_CTRL_1,
172 RT1016_DA_MUTE_L_SFT, RT1016_DA_MUTE_R_SFT, 1, 1),
175 static int rt1016_is_sys_clk_from_pll(struct snd_soc_dapm_widget *source,
176 struct snd_soc_dapm_widget *sink)
178 struct snd_soc_component *component =
179 snd_soc_dapm_to_component(source->dapm);
180 struct rt1016_priv *rt1016 = snd_soc_component_get_drvdata(component);
182 if (rt1016->sysclk_src == RT1016_SCLK_S_PLL)
188 /* Interface data select */
189 static const char * const rt1016_data_select[] = {
190 "L/R", "R/L", "L/L", "R/R"
193 static SOC_ENUM_SINGLE_DECL(rt1016_if_data_swap_enum,
194 RT1016_I2S_CTRL, RT1016_I2S_DATA_SWAP_SFT, rt1016_data_select);
196 static const struct snd_kcontrol_new rt1016_if_data_swap_mux =
197 SOC_DAPM_ENUM("Data Swap Mux", rt1016_if_data_swap_enum);
199 static const struct snd_soc_dapm_widget rt1016_dapm_widgets[] = {
200 SND_SOC_DAPM_MUX("Data Swap Mux", SND_SOC_NOPM, 0, 0,
201 &rt1016_if_data_swap_mux),
203 SND_SOC_DAPM_SUPPLY("DAC Filter", RT1016_CLOCK_3,
204 RT1016_PWR_DAC_FILTER_BIT, 0, NULL, 0),
205 SND_SOC_DAPM_SUPPLY("DAMOD", RT1016_CLOCK_3, RT1016_PWR_DACMOD_BIT, 0,
207 SND_SOC_DAPM_SUPPLY("FIFO", RT1016_CLOCK_3, RT1016_PWR_CLK_FIFO_BIT, 0,
209 SND_SOC_DAPM_SUPPLY("Pure DC", RT1016_CLOCK_3,
210 RT1016_PWR_CLK_PUREDC_BIT, 0, NULL, 0),
211 SND_SOC_DAPM_SUPPLY("CLK Silence Det", RT1016_CLOCK_3,
212 RT1016_PWR_SIL_DET_BIT, 0, NULL, 0),
213 SND_SOC_DAPM_SUPPLY("RC 25M", RT1016_CLOCK_3, RT1016_PWR_RC_25M_BIT, 0,
215 SND_SOC_DAPM_SUPPLY("PLL1", RT1016_CLOCK_3, RT1016_PWR_PLL1_BIT, 0,
217 SND_SOC_DAPM_SUPPLY("ANA CTRL", RT1016_CLOCK_3, RT1016_PWR_ANA_CTRL_BIT,
219 SND_SOC_DAPM_SUPPLY("CLK SYS", RT1016_CLOCK_3, RT1016_PWR_CLK_SYS_BIT,
222 SND_SOC_DAPM_SUPPLY("LRCK Det", RT1016_CLOCK_4, RT1016_PWR_LRCK_DET_BIT,
224 SND_SOC_DAPM_SUPPLY("BCLK Det", RT1016_CLOCK_4, RT1016_PWR_BCLK_DET_BIT,
227 SND_SOC_DAPM_SUPPLY("CKGEN DAC", RT1016_DAC_CTRL_2,
228 RT1016_CKGEN_DAC_BIT, 0, NULL, 0),
229 SND_SOC_DAPM_SUPPLY("VCM SLOW", RT1016_CLASSD_1, RT1016_VCM_SLOW_BIT, 0,
231 SND_SOC_DAPM_SUPPLY("Silence Det", RT1016_SIL_DET,
232 RT1016_SIL_DET_EN_BIT, 0, NULL, 0),
233 SND_SOC_DAPM_SUPPLY("PLL2", RT1016_PLL2, RT1016_PLL2_EN_BIT, 0, NULL,
236 SND_SOC_DAPM_SUPPLY_S("BG1 BG2", 1, RT1016_PWR_CTRL,
237 RT1016_PWR_BG_1_2_BIT, 0, NULL, 0),
238 SND_SOC_DAPM_SUPPLY_S("MBIAS BG", 1, RT1016_PWR_CTRL,
239 RT1016_PWR_MBIAS_BG_BIT, 0, NULL, 0),
240 SND_SOC_DAPM_SUPPLY_S("PLL", 1, RT1016_PWR_CTRL, RT1016_PWR_PLL_BIT, 0,
242 SND_SOC_DAPM_SUPPLY_S("BASIC", 1, RT1016_PWR_CTRL, RT1016_PWR_BASIC_BIT,
244 SND_SOC_DAPM_SUPPLY_S("CLASS D", 1, RT1016_PWR_CTRL,
245 RT1016_PWR_CLSD_BIT, 0, NULL, 0),
246 SND_SOC_DAPM_SUPPLY_S("25M", 1, RT1016_PWR_CTRL, RT1016_PWR_25M_BIT, 0,
248 SND_SOC_DAPM_SUPPLY_S("DACL", 1, RT1016_PWR_CTRL, RT1016_PWR_DACL_BIT,
250 SND_SOC_DAPM_SUPPLY_S("DACR", 1, RT1016_PWR_CTRL, RT1016_PWR_DACR_BIT,
252 SND_SOC_DAPM_SUPPLY_S("LDO2", 1, RT1016_PWR_CTRL, RT1016_PWR_LDO2_BIT,
254 SND_SOC_DAPM_SUPPLY_S("VREF", 1, RT1016_PWR_CTRL, RT1016_PWR_VREF_BIT,
256 SND_SOC_DAPM_SUPPLY_S("MBIAS", 1, RT1016_PWR_CTRL, RT1016_PWR_MBIAS_BIT,
259 SND_SOC_DAPM_AIF_IN("AIFRX", "AIF Playback", 0, SND_SOC_NOPM, 0, 0),
260 SND_SOC_DAPM_DAC("DAC", NULL, SND_SOC_NOPM, 0, 0),
262 SND_SOC_DAPM_OUTPUT("SPO"),
265 static const struct snd_soc_dapm_route rt1016_dapm_routes[] = {
266 { "Data Swap Mux", "L/R", "AIFRX" },
267 { "Data Swap Mux", "R/L", "AIFRX" },
268 { "Data Swap Mux", "L/L", "AIFRX" },
269 { "Data Swap Mux", "R/R", "AIFRX" },
271 { "DAC", NULL, "DAC Filter" },
272 { "DAC", NULL, "DAMOD" },
273 { "DAC", NULL, "FIFO" },
274 { "DAC", NULL, "Pure DC" },
275 { "DAC", NULL, "Silence Det" },
276 { "DAC", NULL, "ANA CTRL" },
277 { "DAC", NULL, "CLK SYS" },
278 { "DAC", NULL, "LRCK Det" },
279 { "DAC", NULL, "BCLK Det" },
280 { "DAC", NULL, "CKGEN DAC" },
281 { "DAC", NULL, "VCM SLOW" },
283 { "PLL", NULL, "PLL1" },
284 { "PLL", NULL, "PLL2" },
285 { "25M", NULL, "RC 25M" },
286 { "Silence Det", NULL, "CLK Silence Det" },
288 { "DAC", NULL, "Data Swap Mux" },
289 { "DAC", NULL, "BG1 BG2" },
290 { "DAC", NULL, "MBIAS BG" },
291 { "DAC", NULL, "PLL", rt1016_is_sys_clk_from_pll},
292 { "DAC", NULL, "BASIC" },
293 { "DAC", NULL, "CLASS D" },
294 { "DAC", NULL, "25M" },
295 { "DAC", NULL, "DACL" },
296 { "DAC", NULL, "DACR" },
297 { "DAC", NULL, "LDO2" },
298 { "DAC", NULL, "VREF" },
299 { "DAC", NULL, "MBIAS" },
301 { "SPO", NULL, "DAC" },
304 static int rt1016_hw_params(struct snd_pcm_substream *substream,
305 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
307 struct snd_soc_component *component = dai->component;
308 struct rt1016_priv *rt1016 = snd_soc_component_get_drvdata(component);
309 int pre_div, bclk_ms, frame_size;
310 unsigned int val_len = 0;
312 rt1016->lrck = params_rate(params);
313 pre_div = rl6231_get_clk_info(rt1016->sysclk, rt1016->lrck);
315 dev_err(component->dev, "Unsupported clock rate\n");
319 frame_size = snd_soc_params_to_frame_size(params);
320 if (frame_size < 0) {
321 dev_err(component->dev, "Unsupported frame size: %d\n",
326 bclk_ms = frame_size > 32;
327 rt1016->bclk = rt1016->lrck * (32 << bclk_ms);
329 if (bclk_ms && rt1016->master)
330 snd_soc_component_update_bits(component, RT1016_I2S_CTRL,
331 RT1016_I2S_BCLK_MS_MASK, RT1016_I2S_BCLK_MS_64);
333 dev_dbg(component->dev, "lrck is %dHz and pre_div is %d for iis %d\n",
334 rt1016->lrck, pre_div, dai->id);
336 switch (params_width(params)) {
338 val_len = RT1016_I2S_DL_16;
341 val_len = RT1016_I2S_DL_20;
344 val_len = RT1016_I2S_DL_24;
347 val_len = RT1016_I2S_DL_32;
353 snd_soc_component_update_bits(component, RT1016_I2S_CTRL,
354 RT1016_I2S_DL_MASK, val_len);
355 snd_soc_component_update_bits(component, RT1016_CLOCK_2,
356 RT1016_FS_PD_MASK | RT1016_OSR_PD_MASK,
357 ((pre_div + 3) << RT1016_FS_PD_SFT) |
358 (pre_div << RT1016_OSR_PD_SFT));
363 static int rt1016_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
365 struct snd_soc_component *component = dai->component;
366 struct rt1016_priv *rt1016 = snd_soc_component_get_drvdata(component);
367 unsigned int reg_val = 0;
369 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
370 case SND_SOC_DAIFMT_CBM_CFM:
371 reg_val |= RT1016_I2S_MS_M;
374 case SND_SOC_DAIFMT_CBS_CFS:
375 reg_val |= RT1016_I2S_MS_S;
381 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
382 case SND_SOC_DAIFMT_NB_NF:
384 case SND_SOC_DAIFMT_IB_NF:
385 reg_val |= RT1016_I2S_BCLK_POL_INV;
391 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
392 case SND_SOC_DAIFMT_I2S:
395 case SND_SOC_DAIFMT_LEFT_J:
396 reg_val |= RT1016_I2S_DF_LEFT;
399 case SND_SOC_DAIFMT_DSP_A:
400 reg_val |= RT1016_I2S_DF_PCM_A;
403 case SND_SOC_DAIFMT_DSP_B:
404 reg_val |= RT1016_I2S_DF_PCM_B;
411 snd_soc_component_update_bits(component, RT1016_I2S_CTRL,
412 RT1016_I2S_MS_MASK | RT1016_I2S_BCLK_POL_MASK |
413 RT1016_I2S_DF_MASK, reg_val);
418 static int rt1016_set_component_sysclk(struct snd_soc_component *component,
419 int clk_id, int source, unsigned int freq, int dir)
421 struct rt1016_priv *rt1016 = snd_soc_component_get_drvdata(component);
422 unsigned int reg_val = 0;
424 if (freq == rt1016->sysclk && clk_id == rt1016->sysclk_src)
428 case RT1016_SCLK_S_MCLK:
429 reg_val |= RT1016_CLK_SYS_SEL_MCLK;
432 case RT1016_SCLK_S_PLL:
433 reg_val |= RT1016_CLK_SYS_SEL_PLL;
437 dev_err(component->dev, "Invalid clock id (%d)\n", clk_id);
441 rt1016->sysclk = freq;
442 rt1016->sysclk_src = clk_id;
444 dev_dbg(component->dev, "Sysclk is %dHz and clock id is %d\n",
447 snd_soc_component_update_bits(component, RT1016_CLOCK_1,
448 RT1016_CLK_SYS_SEL_MASK, reg_val);
453 static int rt1016_set_component_pll(struct snd_soc_component *component,
454 int pll_id, int source, unsigned int freq_in,
455 unsigned int freq_out)
457 struct rt1016_priv *rt1016 = snd_soc_component_get_drvdata(component);
458 struct rl6231_pll_code pll_code;
461 if (!freq_in || !freq_out) {
462 dev_dbg(component->dev, "PLL disabled\n");
470 if (source == rt1016->pll_src && freq_in == rt1016->pll_in &&
471 freq_out == rt1016->pll_out)
475 case RT1016_PLL_S_MCLK:
476 snd_soc_component_update_bits(component, RT1016_CLOCK_1,
477 RT1016_PLL_SEL_MASK, RT1016_PLL_SEL_MCLK);
480 case RT1016_PLL_S_BCLK:
481 snd_soc_component_update_bits(component, RT1016_CLOCK_1,
482 RT1016_PLL_SEL_MASK, RT1016_PLL_SEL_BCLK);
486 dev_err(component->dev, "Unknown PLL Source %d\n", source);
490 ret = rl6231_pll_calc(freq_in, freq_out * 4, &pll_code);
492 dev_err(component->dev, "Unsupported input clock %d\n", freq_in);
496 dev_dbg(component->dev, "mbypass=%d m=%d n=%d kbypass=%d k=%d\n",
497 pll_code.m_bp, (pll_code.m_bp ? 0 : pll_code.m_code),
498 pll_code.n_code, pll_code.k_bp,
499 (pll_code.k_bp ? 0 : pll_code.k_code));
501 snd_soc_component_write(component, RT1016_PLL1,
502 ((pll_code.m_bp ? 0 : pll_code.m_code) << RT1016_PLL_M_SFT) |
503 (pll_code.m_bp << RT1016_PLL_M_BP_SFT) |
505 snd_soc_component_write(component, RT1016_PLL2,
506 (pll_code.k_bp << RT1016_PLL_K_BP_SFT) |
507 (pll_code.k_bp ? 0 : pll_code.k_code));
509 rt1016->pll_in = freq_in;
510 rt1016->pll_out = freq_out;
511 rt1016->pll_src = source;
516 static int rt1016_probe(struct snd_soc_component *component)
518 struct rt1016_priv *rt1016 =
519 snd_soc_component_get_drvdata(component);
521 rt1016->component = component;
526 static void rt1016_remove(struct snd_soc_component *component)
528 struct rt1016_priv *rt1016 = snd_soc_component_get_drvdata(component);
530 regmap_write(rt1016->regmap, RT1016_RESET, 0);
533 #define RT1016_STEREO_RATES SNDRV_PCM_RATE_8000_48000
534 #define RT1016_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
535 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
537 static const struct snd_soc_dai_ops rt1016_aif_dai_ops = {
538 .hw_params = rt1016_hw_params,
539 .set_fmt = rt1016_set_dai_fmt,
542 static struct snd_soc_dai_driver rt1016_dai[] = {
544 .name = "rt1016-aif",
547 .stream_name = "AIF Playback",
550 .rates = RT1016_STEREO_RATES,
551 .formats = RT1016_FORMATS,
553 .ops = &rt1016_aif_dai_ops,
558 static int rt1016_suspend(struct snd_soc_component *component)
560 struct rt1016_priv *rt1016 = snd_soc_component_get_drvdata(component);
562 regcache_cache_only(rt1016->regmap, true);
563 regcache_mark_dirty(rt1016->regmap);
568 static int rt1016_resume(struct snd_soc_component *component)
570 struct rt1016_priv *rt1016 = snd_soc_component_get_drvdata(component);
572 regcache_cache_only(rt1016->regmap, false);
573 regcache_sync(rt1016->regmap);
578 #define rt1016_suspend NULL
579 #define rt1016_resume NULL
582 static const struct snd_soc_component_driver soc_component_dev_rt1016 = {
583 .probe = rt1016_probe,
584 .remove = rt1016_remove,
585 .suspend = rt1016_suspend,
586 .resume = rt1016_resume,
587 .controls = rt1016_snd_controls,
588 .num_controls = ARRAY_SIZE(rt1016_snd_controls),
589 .dapm_widgets = rt1016_dapm_widgets,
590 .num_dapm_widgets = ARRAY_SIZE(rt1016_dapm_widgets),
591 .dapm_routes = rt1016_dapm_routes,
592 .num_dapm_routes = ARRAY_SIZE(rt1016_dapm_routes),
593 .set_sysclk = rt1016_set_component_sysclk,
594 .set_pll = rt1016_set_component_pll,
595 .use_pmdown_time = 1,
599 static const struct regmap_config rt1016_regmap = {
602 .max_register = RT1016_PWR_CTRL,
603 .volatile_reg = rt1016_volatile_register,
604 .readable_reg = rt1016_readable_register,
605 .cache_type = REGCACHE_RBTREE,
606 .reg_defaults = rt1016_reg,
607 .num_reg_defaults = ARRAY_SIZE(rt1016_reg),
610 static const struct i2c_device_id rt1016_i2c_id[] = {
614 MODULE_DEVICE_TABLE(i2c, rt1016_i2c_id);
616 #if defined(CONFIG_OF)
617 static const struct of_device_id rt1016_of_match[] = {
618 { .compatible = "realtek,rt1016", },
621 MODULE_DEVICE_TABLE(of, rt1016_of_match);
625 static const struct acpi_device_id rt1016_acpi_match[] = {
629 MODULE_DEVICE_TABLE(acpi, rt1016_acpi_match);
632 static int rt1016_i2c_probe(struct i2c_client *i2c)
634 struct rt1016_priv *rt1016;
638 rt1016 = devm_kzalloc(&i2c->dev, sizeof(struct rt1016_priv),
643 i2c_set_clientdata(i2c, rt1016);
645 rt1016->regmap = devm_regmap_init_i2c(i2c, &rt1016_regmap);
646 if (IS_ERR(rt1016->regmap)) {
647 ret = PTR_ERR(rt1016->regmap);
648 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
653 regmap_read(rt1016->regmap, RT1016_DEVICE_ID, &val);
654 if (val != RT1016_DEVICE_ID_VAL) {
656 "Device with ID register %x is not rt1016\n", val);
660 regmap_write(rt1016->regmap, RT1016_RESET, 0);
662 ret = regmap_register_patch(rt1016->regmap, rt1016_patch,
663 ARRAY_SIZE(rt1016_patch));
665 dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
667 return devm_snd_soc_register_component(&i2c->dev,
668 &soc_component_dev_rt1016,
669 rt1016_dai, ARRAY_SIZE(rt1016_dai));
672 static void rt1016_i2c_shutdown(struct i2c_client *client)
674 struct rt1016_priv *rt1016 = i2c_get_clientdata(client);
676 regmap_write(rt1016->regmap, RT1016_RESET, 0);
679 static struct i2c_driver rt1016_i2c_driver = {
682 .of_match_table = of_match_ptr(rt1016_of_match),
683 .acpi_match_table = ACPI_PTR(rt1016_acpi_match),
685 .probe = rt1016_i2c_probe,
686 .shutdown = rt1016_i2c_shutdown,
687 .id_table = rt1016_i2c_id,
689 module_i2c_driver(rt1016_i2c_driver);
691 MODULE_DESCRIPTION("ASoC RT1016 driver");
693 MODULE_LICENSE("GPL v2");