1 // SPDX-License-Identifier: GPL-2.0
3 // rt1015.c -- RT1015 ALSA SoC audio amplifier driver
5 // Copyright 2019 Realtek Semiconductor Corp.
7 // Author: Jack Yu <jack.yu@realtek.com>
11 #include <linux/acpi.h>
12 #include <linux/delay.h>
13 #include <linux/firmware.h>
15 #include <linux/gpio.h>
16 #include <linux/i2c.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/platform_device.h>
22 #include <linux/regmap.h>
23 #include <sound/core.h>
24 #include <sound/initval.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc-dapm.h>
28 #include <sound/soc.h>
29 #include <sound/tlv.h>
30 #include <sound/rt1015.h>
35 static const struct rt1015_platform_data i2s_default_platform_data = {
36 .power_up_delay_ms = 50,
39 static const struct reg_default rt1015_reg[] = {
202 static bool rt1015_volatile_register(struct device *dev, unsigned int reg)
209 case RT1015_VENDOR_ID:
210 case RT1015_DEVICE_ID:
213 case RT1015_VBAT_TEST_OUT1:
214 case RT1015_VBAT_TEST_OUT2:
215 case RT1015_VBAT_PROT_ATT:
216 case RT1015_VBAT_DET_CODE:
217 case RT1015_SMART_BST_CTRL1:
218 case RT1015_SPK_DC_DETECT1:
219 case RT1015_SPK_DC_DETECT4:
220 case RT1015_SPK_DC_DETECT5:
221 case RT1015_DC_CALIB_CLSD1:
222 case RT1015_DC_CALIB_CLSD5:
223 case RT1015_DC_CALIB_CLSD6:
224 case RT1015_DC_CALIB_CLSD7:
225 case RT1015_DC_CALIB_CLSD8:
226 case RT1015_S_BST_TIMING_INTER1:
227 case RT1015_OSCK_STA:
228 case RT1015_MONO_DYNA_CTRL1:
229 case RT1015_MONO_DYNA_CTRL5:
237 static bool rt1015_readable_register(struct device *dev, unsigned int reg)
253 case RT1015_CUSTOMER_ID:
254 case RT1015_PCODE_FWVER:
256 case RT1015_VENDOR_ID:
257 case RT1015_DEVICE_ID:
258 case RT1015_PAD_DRV1:
259 case RT1015_PAD_DRV2:
260 case RT1015_GAT_BOOST:
262 case RT1015_OSCK_STA:
269 case RT1015_TDM_MASTER:
270 case RT1015_TDM_TCON:
278 case RT1015_ANA_PROTECT1:
279 case RT1015_ANA_CTRL_SEQ1:
280 case RT1015_ANA_CTRL_SEQ2:
281 case RT1015_VBAT_DET_DEB:
282 case RT1015_VBAT_VOLT_DET1:
283 case RT1015_VBAT_VOLT_DET2:
284 case RT1015_VBAT_TEST_OUT1:
285 case RT1015_VBAT_TEST_OUT2:
286 case RT1015_VBAT_PROT_ATT:
287 case RT1015_VBAT_DET_CODE:
295 case RT1015_CLASSD_SEQ:
296 case RT1015_SMART_BST_CTRL1:
297 case RT1015_SMART_BST_CTRL2:
298 case RT1015_ANA_CTRL1:
299 case RT1015_ANA_CTRL2:
300 case RT1015_PWR_STATE_CTRL:
301 case RT1015_MONO_DYNA_CTRL:
302 case RT1015_MONO_DYNA_CTRL1:
303 case RT1015_MONO_DYNA_CTRL2:
304 case RT1015_MONO_DYNA_CTRL3:
305 case RT1015_MONO_DYNA_CTRL4:
306 case RT1015_MONO_DYNA_CTRL5:
308 case RT1015_SHORT_DETTOP1:
309 case RT1015_SHORT_DETTOP2:
310 case RT1015_SPK_DC_DETECT1:
311 case RT1015_SPK_DC_DETECT2:
312 case RT1015_SPK_DC_DETECT3:
313 case RT1015_SPK_DC_DETECT4:
314 case RT1015_SPK_DC_DETECT5:
315 case RT1015_BAT_RPO_STEP1:
316 case RT1015_BAT_RPO_STEP2:
317 case RT1015_BAT_RPO_STEP3:
318 case RT1015_BAT_RPO_STEP4:
319 case RT1015_BAT_RPO_STEP5:
320 case RT1015_BAT_RPO_STEP6:
321 case RT1015_BAT_RPO_STEP7:
322 case RT1015_BAT_RPO_STEP8:
323 case RT1015_BAT_RPO_STEP9:
324 case RT1015_BAT_RPO_STEP10:
325 case RT1015_BAT_RPO_STEP11:
326 case RT1015_BAT_RPO_STEP12:
327 case RT1015_SPREAD_SPEC1:
328 case RT1015_SPREAD_SPEC2:
329 case RT1015_PAD_STATUS:
330 case RT1015_PADS_PULLING_CTRL1:
331 case RT1015_PADS_DRIVING:
332 case RT1015_SYS_RST1:
333 case RT1015_SYS_RST2:
334 case RT1015_SYS_GATING1:
335 case RT1015_TEST_MODE1:
336 case RT1015_TEST_MODE2:
337 case RT1015_TIMING_CTRL1:
339 case RT1015_TEST_OUT1:
340 case RT1015_DC_CALIB_CLSD1:
341 case RT1015_DC_CALIB_CLSD2:
342 case RT1015_DC_CALIB_CLSD3:
343 case RT1015_DC_CALIB_CLSD4:
344 case RT1015_DC_CALIB_CLSD5:
345 case RT1015_DC_CALIB_CLSD6:
346 case RT1015_DC_CALIB_CLSD7:
347 case RT1015_DC_CALIB_CLSD8:
348 case RT1015_DC_CALIB_CLSD9:
349 case RT1015_DC_CALIB_CLSD10:
350 case RT1015_CLSD_INTERNAL1:
351 case RT1015_CLSD_INTERNAL2:
352 case RT1015_CLSD_INTERNAL3:
353 case RT1015_CLSD_INTERNAL4:
354 case RT1015_CLSD_INTERNAL5:
355 case RT1015_CLSD_INTERNAL6:
356 case RT1015_CLSD_INTERNAL7:
357 case RT1015_CLSD_INTERNAL8:
358 case RT1015_CLSD_INTERNAL9:
359 case RT1015_CLSD_OCP_CTRL:
365 case RT1015_VREF_LV1:
366 case RT1015_S_BST_TIMING_INTER1:
367 case RT1015_S_BST_TIMING_INTER2:
368 case RT1015_S_BST_TIMING_INTER3:
369 case RT1015_S_BST_TIMING_INTER4:
370 case RT1015_S_BST_TIMING_INTER5:
371 case RT1015_S_BST_TIMING_INTER6:
372 case RT1015_S_BST_TIMING_INTER7:
373 case RT1015_S_BST_TIMING_INTER8:
374 case RT1015_S_BST_TIMING_INTER9:
375 case RT1015_S_BST_TIMING_INTER10:
376 case RT1015_S_BST_TIMING_INTER11:
377 case RT1015_S_BST_TIMING_INTER12:
378 case RT1015_S_BST_TIMING_INTER13:
379 case RT1015_S_BST_TIMING_INTER14:
380 case RT1015_S_BST_TIMING_INTER15:
381 case RT1015_S_BST_TIMING_INTER16:
382 case RT1015_S_BST_TIMING_INTER17:
383 case RT1015_S_BST_TIMING_INTER18:
384 case RT1015_S_BST_TIMING_INTER19:
385 case RT1015_S_BST_TIMING_INTER20:
386 case RT1015_S_BST_TIMING_INTER21:
387 case RT1015_S_BST_TIMING_INTER22:
388 case RT1015_S_BST_TIMING_INTER23:
389 case RT1015_S_BST_TIMING_INTER24:
390 case RT1015_S_BST_TIMING_INTER25:
391 case RT1015_S_BST_TIMING_INTER26:
392 case RT1015_S_BST_TIMING_INTER27:
393 case RT1015_S_BST_TIMING_INTER28:
394 case RT1015_S_BST_TIMING_INTER29:
395 case RT1015_S_BST_TIMING_INTER30:
396 case RT1015_S_BST_TIMING_INTER31:
397 case RT1015_S_BST_TIMING_INTER32:
398 case RT1015_S_BST_TIMING_INTER33:
399 case RT1015_S_BST_TIMING_INTER34:
400 case RT1015_S_BST_TIMING_INTER35:
401 case RT1015_S_BST_TIMING_INTER36:
409 static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -9525, 75, 0);
411 static const char * const rt1015_din_source_select[] = {
414 "Left + Right average",
417 static SOC_ENUM_SINGLE_DECL(rt1015_mono_lr_sel, RT1015_PAD_DRV2, 4,
418 rt1015_din_source_select);
420 static const char * const rt1015_boost_mode[] = {
421 "Bypass", "Adaptive", "Fixed Adaptive"
424 static SOC_ENUM_SINGLE_DECL(rt1015_boost_mode_enum, 0, 0,
427 static int rt1015_boost_mode_get(struct snd_kcontrol *kcontrol,
428 struct snd_ctl_elem_value *ucontrol)
430 struct snd_soc_component *component =
431 snd_soc_kcontrol_component(kcontrol);
432 struct rt1015_priv *rt1015 =
433 snd_soc_component_get_drvdata(component);
435 ucontrol->value.integer.value[0] = rt1015->boost_mode;
440 static int rt1015_boost_mode_put(struct snd_kcontrol *kcontrol,
441 struct snd_ctl_elem_value *ucontrol)
443 struct snd_soc_component *component =
444 snd_soc_kcontrol_component(kcontrol);
445 struct rt1015_priv *rt1015 =
446 snd_soc_component_get_drvdata(component);
448 rt1015->boost_mode = ucontrol->value.integer.value[0];
450 switch (rt1015->boost_mode) {
452 snd_soc_component_update_bits(component,
453 RT1015_SMART_BST_CTRL1, RT1015_ABST_AUTO_EN_MASK |
454 RT1015_ABST_FIX_TGT_MASK | RT1015_BYPASS_SWR_REG_MASK,
455 RT1015_ABST_REG_MODE | RT1015_ABST_FIX_TGT_DIS |
456 RT1015_BYPASS_SWRREG_BYPASS);
459 snd_soc_component_update_bits(component,
460 RT1015_SMART_BST_CTRL1, RT1015_ABST_AUTO_EN_MASK |
461 RT1015_ABST_FIX_TGT_MASK | RT1015_BYPASS_SWR_REG_MASK,
462 RT1015_ABST_AUTO_MODE | RT1015_ABST_FIX_TGT_DIS |
463 RT1015_BYPASS_SWRREG_PASS);
466 snd_soc_component_update_bits(component,
467 RT1015_SMART_BST_CTRL1, RT1015_ABST_AUTO_EN_MASK |
468 RT1015_ABST_FIX_TGT_MASK | RT1015_BYPASS_SWR_REG_MASK,
469 RT1015_ABST_AUTO_MODE | RT1015_ABST_FIX_TGT_EN |
470 RT1015_BYPASS_SWRREG_PASS);
473 dev_err(component->dev, "Unknown boost control.\n");
479 static int rt1015_bypass_boost_get(struct snd_kcontrol *kcontrol,
480 struct snd_ctl_elem_value *ucontrol)
482 struct snd_soc_component *component =
483 snd_soc_kcontrol_component(kcontrol);
484 struct rt1015_priv *rt1015 =
485 snd_soc_component_get_drvdata(component);
487 ucontrol->value.integer.value[0] = rt1015->bypass_boost;
492 static void rt1015_calibrate(struct rt1015_priv *rt1015)
494 struct snd_soc_component *component = rt1015->component;
495 struct regmap *regmap = rt1015->regmap;
497 snd_soc_dapm_mutex_lock(&component->dapm);
498 regcache_cache_bypass(regmap, true);
500 regmap_write(regmap, RT1015_PWR1, 0xd7df);
501 regmap_write(regmap, RT1015_PWR4, 0x00b2);
502 regmap_write(regmap, RT1015_CLSD_INTERNAL8, 0x2008);
503 regmap_write(regmap, RT1015_CLSD_INTERNAL9, 0x0140);
504 regmap_write(regmap, RT1015_GAT_BOOST, 0x0efe);
505 regmap_write(regmap, RT1015_PWR_STATE_CTRL, 0x000d);
506 regmap_write(regmap, RT1015_PWR_STATE_CTRL, 0x000e);
507 regmap_write(regmap, RT1015_DC_CALIB_CLSD1, 0x5a00);
508 regmap_write(regmap, RT1015_DC_CALIB_CLSD1, 0x5a01);
509 regmap_write(regmap, RT1015_DC_CALIB_CLSD1, 0x5a05);
511 regmap_write(regmap, RT1015_PWR1, 0x0);
513 regcache_cache_bypass(regmap, false);
514 regcache_mark_dirty(regmap);
515 regcache_sync(regmap);
516 snd_soc_dapm_mutex_unlock(&component->dapm);
519 static int rt1015_bypass_boost_put(struct snd_kcontrol *kcontrol,
520 struct snd_ctl_elem_value *ucontrol)
522 struct snd_soc_component *component =
523 snd_soc_kcontrol_component(kcontrol);
524 struct rt1015_priv *rt1015 =
525 snd_soc_component_get_drvdata(component);
527 if (!rt1015->dac_is_used) {
528 rt1015->bypass_boost = ucontrol->value.integer.value[0];
529 if (rt1015->bypass_boost == RT1015_Bypass_Boost &&
530 !rt1015->cali_done) {
531 rt1015_calibrate(rt1015);
532 rt1015->cali_done = 1;
534 regmap_write(rt1015->regmap, RT1015_MONO_DYNA_CTRL, 0x0010);
537 dev_err(component->dev, "DAC is being used!\n");
542 static void rt1015_flush_work(struct work_struct *work)
544 struct rt1015_priv *rt1015 = container_of(work, struct rt1015_priv,
546 struct snd_soc_component *component = rt1015->component;
547 unsigned int val, i = 0, count = 200;
550 usleep_range(1000, 1500);
551 dev_dbg(component->dev, "Flush DAC (retry:%u)\n", i);
552 regmap_read(rt1015->regmap, RT1015_CLK_DET, &val);
558 regmap_write(rt1015->regmap, RT1015_SYS_RST1, 0x0597);
559 regmap_write(rt1015->regmap, RT1015_SYS_RST1, 0x05f7);
560 regmap_write(rt1015->regmap, RT1015_MAN_I2C, 0x0028);
563 dev_dbg(component->dev, "Flush DAC completed.\n");
565 dev_warn(component->dev, "Fail to flush DAC data.\n");
568 static const struct snd_kcontrol_new rt1015_snd_controls[] = {
569 SOC_SINGLE_TLV("DAC Playback Volume", RT1015_DAC1, RT1015_DAC_VOL_SFT,
570 127, 0, dac_vol_tlv),
571 SOC_DOUBLE("DAC Playback Switch", RT1015_DAC3,
572 RT1015_DA_MUTE_SFT, RT1015_DVOL_MUTE_FLAG_SFT, 1, 1),
573 SOC_ENUM_EXT("Boost Mode", rt1015_boost_mode_enum,
574 rt1015_boost_mode_get, rt1015_boost_mode_put),
575 SOC_ENUM("Mono LR Select", rt1015_mono_lr_sel),
576 SOC_SINGLE_EXT("Bypass Boost", SND_SOC_NOPM, 0, 1, 0,
577 rt1015_bypass_boost_get, rt1015_bypass_boost_put),
580 static int rt1015_is_sys_clk_from_pll(struct snd_soc_dapm_widget *source,
581 struct snd_soc_dapm_widget *sink)
583 struct snd_soc_component *component =
584 snd_soc_dapm_to_component(source->dapm);
585 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
587 if (rt1015->sysclk_src == RT1015_SCLK_S_PLL)
593 static int r1015_dac_event(struct snd_soc_dapm_widget *w,
594 struct snd_kcontrol *kcontrol, int event)
596 struct snd_soc_component *component =
597 snd_soc_dapm_to_component(w->dapm);
598 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
601 case SND_SOC_DAPM_PRE_PMU:
602 rt1015->dac_is_used = 1;
603 if (rt1015->bypass_boost == RT1015_Enable_Boost) {
604 snd_soc_component_write(component,
605 RT1015_SYS_RST1, 0x05f7);
606 snd_soc_component_write(component,
607 RT1015_GAT_BOOST, 0xacfe);
608 snd_soc_component_write(component,
609 RT1015_PWR9, 0xaa00);
610 snd_soc_component_write(component,
611 RT1015_GAT_BOOST, 0xecfe);
613 snd_soc_component_write(component,
614 RT1015_SYS_RST1, 0x05f7);
615 snd_soc_component_write(component,
616 RT1015_PWR_STATE_CTRL, 0x026e);
620 case SND_SOC_DAPM_POST_PMU:
621 regmap_write(rt1015->regmap, RT1015_MAN_I2C, 0x00a8);
624 case SND_SOC_DAPM_POST_PMD:
625 if (rt1015->bypass_boost == RT1015_Enable_Boost) {
626 snd_soc_component_write(component,
627 RT1015_PWR9, 0xa800);
628 snd_soc_component_write(component,
629 RT1015_SYS_RST1, 0x05f5);
631 snd_soc_component_write(component,
632 RT1015_PWR_STATE_CTRL, 0x0268);
633 snd_soc_component_write(component,
634 RT1015_SYS_RST1, 0x05f5);
636 rt1015->dac_is_used = 0;
638 cancel_delayed_work_sync(&rt1015->flush_work);
647 static int rt1015_amp_drv_event(struct snd_soc_dapm_widget *w,
648 struct snd_kcontrol *kcontrol, int event)
650 struct snd_soc_component *component =
651 snd_soc_dapm_to_component(w->dapm);
652 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
655 case SND_SOC_DAPM_POST_PMU:
656 if (rt1015->hw_config == RT1015_HW_28)
657 schedule_delayed_work(&rt1015->flush_work, msecs_to_jiffies(10));
658 msleep(rt1015->pdata.power_up_delay_ms);
666 static const struct snd_soc_dapm_widget rt1015_dapm_widgets[] = {
667 SND_SOC_DAPM_SUPPLY("LDO2", RT1015_PWR1, RT1015_PWR_LDO2_BIT, 0,
669 SND_SOC_DAPM_SUPPLY("INT RC CLK", RT1015_PWR1, RT1015_PWR_INTCLK_BIT,
671 SND_SOC_DAPM_SUPPLY("ISENSE", RT1015_PWR1, RT1015_PWR_ISENSE_BIT, 0,
673 SND_SOC_DAPM_SUPPLY("VSENSE", RT1015_PWR1, RT1015_PWR_VSENSE_BIT, 0,
675 SND_SOC_DAPM_SUPPLY("PLL", RT1015_PWR1, RT1015_PWR_PLL_BIT, 0,
677 SND_SOC_DAPM_SUPPLY("BG1 BG2", RT1015_PWR1, RT1015_PWR_BG_1_2_BIT, 0,
679 SND_SOC_DAPM_SUPPLY("MBIAS BG", RT1015_PWR1, RT1015_PWR_MBIAS_BG_BIT, 0,
681 SND_SOC_DAPM_SUPPLY("VBAT", RT1015_PWR1, RT1015_PWR_VBAT_BIT, 0, NULL,
683 SND_SOC_DAPM_SUPPLY("MBIAS", RT1015_PWR1, RT1015_PWR_MBIAS_BIT, 0,
685 SND_SOC_DAPM_SUPPLY("ADCV", RT1015_PWR1, RT1015_PWR_ADCV_BIT, 0, NULL,
687 SND_SOC_DAPM_SUPPLY("MIXERV", RT1015_PWR1, RT1015_PWR_MIXERV_BIT, 0,
689 SND_SOC_DAPM_SUPPLY("SUMV", RT1015_PWR1, RT1015_PWR_SUMV_BIT, 0, NULL,
691 SND_SOC_DAPM_SUPPLY("VREFLV", RT1015_PWR1, RT1015_PWR_VREFLV_BIT, 0,
694 SND_SOC_DAPM_AIF_IN("AIFRX", "AIF Playback", 0, SND_SOC_NOPM, 0, 0),
695 SND_SOC_DAPM_DAC_E("DAC", NULL, RT1015_PWR1, RT1015_PWR_DAC_BIT, 0,
696 r1015_dac_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
697 SND_SOC_DAPM_POST_PMD),
699 SND_SOC_DAPM_OUT_DRV_E("Amp Drv", SND_SOC_NOPM, 0, 0, NULL, 0,
700 rt1015_amp_drv_event, SND_SOC_DAPM_POST_PMU),
701 SND_SOC_DAPM_OUTPUT("SPO"),
704 static const struct snd_soc_dapm_route rt1015_dapm_routes[] = {
705 { "DAC", NULL, "AIFRX" },
706 { "DAC", NULL, "LDO2" },
707 { "DAC", NULL, "PLL", rt1015_is_sys_clk_from_pll},
708 { "DAC", NULL, "INT RC CLK" },
709 { "DAC", NULL, "ISENSE" },
710 { "DAC", NULL, "VSENSE" },
711 { "DAC", NULL, "BG1 BG2" },
712 { "DAC", NULL, "MBIAS BG" },
713 { "DAC", NULL, "VBAT" },
714 { "DAC", NULL, "MBIAS" },
715 { "DAC", NULL, "ADCV" },
716 { "DAC", NULL, "MIXERV" },
717 { "DAC", NULL, "SUMV" },
718 { "DAC", NULL, "VREFLV" },
719 { "Amp Drv", NULL, "DAC" },
720 { "SPO", NULL, "Amp Drv" },
723 static int rt1015_hw_params(struct snd_pcm_substream *substream,
724 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
726 struct snd_soc_component *component = dai->component;
727 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
728 int pre_div, bclk_ms, frame_size;
729 unsigned int val_len = 0;
731 rt1015->lrck = params_rate(params);
732 pre_div = rl6231_get_clk_info(rt1015->sysclk, rt1015->lrck);
734 dev_err(component->dev, "Unsupported clock rate\n");
738 frame_size = snd_soc_params_to_frame_size(params);
739 if (frame_size < 0) {
740 dev_err(component->dev, "Unsupported frame size: %d\n",
745 bclk_ms = frame_size > 32;
746 rt1015->bclk = rt1015->lrck * (32 << bclk_ms);
748 dev_dbg(component->dev, "bclk_ms is %d and pre_div is %d for iis %d\n",
749 bclk_ms, pre_div, dai->id);
751 dev_dbg(component->dev, "lrck is %dHz and pre_div is %d for iis %d\n",
752 rt1015->lrck, pre_div, dai->id);
754 switch (params_width(params)) {
758 val_len = RT1015_I2S_DL_20;
761 val_len = RT1015_I2S_DL_24;
764 val_len = RT1015_I2S_DL_8;
770 snd_soc_component_update_bits(component, RT1015_TDM_MASTER,
771 RT1015_I2S_DL_MASK, val_len);
772 snd_soc_component_update_bits(component, RT1015_CLK2,
773 RT1015_FS_PD_MASK, pre_div << RT1015_FS_PD_SFT);
778 static int rt1015_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
780 struct snd_soc_component *component = dai->component;
781 unsigned int reg_val = 0, reg_val2 = 0;
783 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
784 case SND_SOC_DAIFMT_CBM_CFM:
785 reg_val |= RT1015_TCON_TDM_MS_M;
787 case SND_SOC_DAIFMT_CBS_CFS:
788 reg_val |= RT1015_TCON_TDM_MS_S;
794 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
795 case SND_SOC_DAIFMT_NB_NF:
797 case SND_SOC_DAIFMT_IB_NF:
798 reg_val2 |= RT1015_TDM_INV_BCLK;
804 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
805 case SND_SOC_DAIFMT_I2S:
808 case SND_SOC_DAIFMT_LEFT_J:
809 reg_val |= RT1015_I2S_M_DF_LEFT;
812 case SND_SOC_DAIFMT_DSP_A:
813 reg_val |= RT1015_I2S_M_DF_PCM_A;
816 case SND_SOC_DAIFMT_DSP_B:
817 reg_val |= RT1015_I2S_M_DF_PCM_B;
824 snd_soc_component_update_bits(component, RT1015_TDM_MASTER,
825 RT1015_TCON_TDM_MS_MASK | RT1015_I2S_M_DF_MASK,
827 snd_soc_component_update_bits(component, RT1015_TDM1_1,
828 RT1015_TDM_INV_BCLK_MASK, reg_val2);
833 static int rt1015_set_component_sysclk(struct snd_soc_component *component,
834 int clk_id, int source, unsigned int freq, int dir)
836 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
837 unsigned int reg_val = 0;
839 if (freq == rt1015->sysclk && clk_id == rt1015->sysclk_src)
843 case RT1015_SCLK_S_MCLK:
844 reg_val |= RT1015_CLK_SYS_PRE_SEL_MCLK;
847 case RT1015_SCLK_S_PLL:
848 reg_val |= RT1015_CLK_SYS_PRE_SEL_PLL;
852 dev_err(component->dev, "Invalid clock id (%d)\n", clk_id);
856 rt1015->sysclk = freq;
857 rt1015->sysclk_src = clk_id;
859 dev_dbg(component->dev, "Sysclk is %dHz and clock id is %d\n",
862 snd_soc_component_update_bits(component, RT1015_CLK2,
863 RT1015_CLK_SYS_PRE_SEL_MASK, reg_val);
868 static int rt1015_set_component_pll(struct snd_soc_component *component,
869 int pll_id, int source, unsigned int freq_in,
870 unsigned int freq_out)
872 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
873 struct rl6231_pll_code pll_code;
876 if (!freq_in || !freq_out) {
877 dev_dbg(component->dev, "PLL disabled\n");
885 if (source == rt1015->pll_src && freq_in == rt1015->pll_in &&
886 freq_out == rt1015->pll_out)
889 if (source == RT1015_PLL_S_BCLK) {
890 if (rt1015->bclk_ratio == 0) {
891 dev_err(component->dev,
892 "Can not support bclk ratio as 0.\n");
898 case RT1015_PLL_S_MCLK:
899 snd_soc_component_update_bits(component, RT1015_CLK2,
900 RT1015_PLL_SEL_MASK, RT1015_PLL_SEL_PLL_SRC2);
903 case RT1015_PLL_S_BCLK:
904 snd_soc_component_update_bits(component, RT1015_CLK2,
905 RT1015_PLL_SEL_MASK, RT1015_PLL_SEL_BCLK);
909 dev_err(component->dev, "Unknown PLL Source %d\n", source);
913 ret = rl6231_pll_calc(freq_in, freq_out, &pll_code);
915 dev_err(component->dev, "Unsupport input clock %d\n", freq_in);
919 dev_dbg(component->dev, "bypass=%d m=%d n=%d k=%d\n",
920 pll_code.m_bp, (pll_code.m_bp ? 0 : pll_code.m_code),
921 pll_code.n_code, pll_code.k_code);
923 snd_soc_component_write(component, RT1015_PLL1,
924 (pll_code.m_bp ? 0 : pll_code.m_code) << RT1015_PLL_M_SFT |
925 pll_code.m_bp << RT1015_PLL_M_BP_SFT | pll_code.n_code);
926 snd_soc_component_write(component, RT1015_PLL2,
929 rt1015->pll_in = freq_in;
930 rt1015->pll_out = freq_out;
931 rt1015->pll_src = source;
936 static int rt1015_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
938 struct snd_soc_component *component = dai->component;
939 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
941 dev_dbg(component->dev, "%s ratio=%d\n", __func__, ratio);
943 rt1015->bclk_ratio = ratio;
946 dev_dbg(component->dev, "Unsupport bclk ratio\n");
953 static int rt1015_probe(struct snd_soc_component *component)
955 struct rt1015_priv *rt1015 =
956 snd_soc_component_get_drvdata(component);
958 rt1015->component = component;
959 rt1015->bclk_ratio = 0;
960 rt1015->cali_done = 0;
961 snd_soc_component_write(component, RT1015_BAT_RPO_STEP1, 0x061c);
963 INIT_DELAYED_WORK(&rt1015->flush_work, rt1015_flush_work);
968 static void rt1015_remove(struct snd_soc_component *component)
970 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
972 cancel_delayed_work_sync(&rt1015->flush_work);
973 regmap_write(rt1015->regmap, RT1015_RESET, 0);
976 #define RT1015_STEREO_RATES SNDRV_PCM_RATE_8000_192000
977 #define RT1015_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
978 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
980 static struct snd_soc_dai_ops rt1015_aif_dai_ops = {
981 .hw_params = rt1015_hw_params,
982 .set_fmt = rt1015_set_dai_fmt,
983 .set_bclk_ratio = rt1015_set_bclk_ratio,
986 static struct snd_soc_dai_driver rt1015_dai[] = {
988 .name = "rt1015-aif",
991 .stream_name = "AIF Playback",
994 .rates = RT1015_STEREO_RATES,
995 .formats = RT1015_FORMATS,
997 .ops = &rt1015_aif_dai_ops,
1002 static int rt1015_suspend(struct snd_soc_component *component)
1004 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
1006 regcache_cache_only(rt1015->regmap, true);
1007 regcache_mark_dirty(rt1015->regmap);
1012 static int rt1015_resume(struct snd_soc_component *component)
1014 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
1016 regcache_cache_only(rt1015->regmap, false);
1017 regcache_sync(rt1015->regmap);
1021 #define rt1015_suspend NULL
1022 #define rt1015_resume NULL
1025 static const struct snd_soc_component_driver soc_component_dev_rt1015 = {
1026 .probe = rt1015_probe,
1027 .remove = rt1015_remove,
1028 .suspend = rt1015_suspend,
1029 .resume = rt1015_resume,
1030 .controls = rt1015_snd_controls,
1031 .num_controls = ARRAY_SIZE(rt1015_snd_controls),
1032 .dapm_widgets = rt1015_dapm_widgets,
1033 .num_dapm_widgets = ARRAY_SIZE(rt1015_dapm_widgets),
1034 .dapm_routes = rt1015_dapm_routes,
1035 .num_dapm_routes = ARRAY_SIZE(rt1015_dapm_routes),
1036 .set_sysclk = rt1015_set_component_sysclk,
1037 .set_pll = rt1015_set_component_pll,
1038 .use_pmdown_time = 1,
1040 .non_legacy_dai_naming = 1,
1043 static const struct regmap_config rt1015_regmap = {
1046 .max_register = RT1015_S_BST_TIMING_INTER36,
1047 .volatile_reg = rt1015_volatile_register,
1048 .readable_reg = rt1015_readable_register,
1049 .cache_type = REGCACHE_RBTREE,
1050 .reg_defaults = rt1015_reg,
1051 .num_reg_defaults = ARRAY_SIZE(rt1015_reg),
1054 static const struct i2c_device_id rt1015_i2c_id[] = {
1058 MODULE_DEVICE_TABLE(i2c, rt1015_i2c_id);
1060 #if defined(CONFIG_OF)
1061 static const struct of_device_id rt1015_of_match[] = {
1062 { .compatible = "realtek,rt1015", },
1065 MODULE_DEVICE_TABLE(of, rt1015_of_match);
1069 static struct acpi_device_id rt1015_acpi_match[] = {
1073 MODULE_DEVICE_TABLE(acpi, rt1015_acpi_match);
1076 static void rt1015_parse_dt(struct rt1015_priv *rt1015, struct device *dev)
1078 device_property_read_u32(dev, "realtek,power-up-delay-ms",
1079 &rt1015->pdata.power_up_delay_ms);
1082 static int rt1015_i2c_probe(struct i2c_client *i2c,
1083 const struct i2c_device_id *id)
1085 struct rt1015_platform_data *pdata = dev_get_platdata(&i2c->dev);
1086 struct rt1015_priv *rt1015;
1090 rt1015 = devm_kzalloc(&i2c->dev, sizeof(struct rt1015_priv),
1095 i2c_set_clientdata(i2c, rt1015);
1097 rt1015->pdata = i2s_default_platform_data;
1100 rt1015->pdata = *pdata;
1102 rt1015_parse_dt(rt1015, &i2c->dev);
1104 rt1015->regmap = devm_regmap_init_i2c(i2c, &rt1015_regmap);
1105 if (IS_ERR(rt1015->regmap)) {
1106 ret = PTR_ERR(rt1015->regmap);
1107 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
1112 rt1015->hw_config = (i2c->addr == 0x29) ? RT1015_HW_29 : RT1015_HW_28;
1114 regmap_read(rt1015->regmap, RT1015_DEVICE_ID, &val);
1115 if ((val != RT1015_DEVICE_ID_VAL) && (val != RT1015_DEVICE_ID_VAL2)) {
1117 "Device with ID register %x is not rt1015\n", val);
1121 return devm_snd_soc_register_component(&i2c->dev,
1122 &soc_component_dev_rt1015,
1123 rt1015_dai, ARRAY_SIZE(rt1015_dai));
1126 static void rt1015_i2c_shutdown(struct i2c_client *client)
1128 struct rt1015_priv *rt1015 = i2c_get_clientdata(client);
1130 regmap_write(rt1015->regmap, RT1015_RESET, 0);
1133 static struct i2c_driver rt1015_i2c_driver = {
1136 .of_match_table = of_match_ptr(rt1015_of_match),
1137 .acpi_match_table = ACPI_PTR(rt1015_acpi_match),
1139 .probe = rt1015_i2c_probe,
1140 .shutdown = rt1015_i2c_shutdown,
1141 .id_table = rt1015_i2c_id,
1143 module_i2c_driver(rt1015_i2c_driver);
1145 MODULE_DESCRIPTION("ASoC RT1015 driver");
1146 MODULE_AUTHOR("Jack Yu <jack.yu@realtek.com>");
1147 MODULE_LICENSE("GPL v2");