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:
214 case RT1015_VBAT_TEST_OUT1:
215 case RT1015_VBAT_TEST_OUT2:
216 case RT1015_VBAT_PROT_ATT:
217 case RT1015_VBAT_DET_CODE:
218 case RT1015_SMART_BST_CTRL1:
219 case RT1015_SPK_DC_DETECT1:
220 case RT1015_SPK_DC_DETECT4:
221 case RT1015_SPK_DC_DETECT5:
222 case RT1015_DC_CALIB_CLSD1:
223 case RT1015_DC_CALIB_CLSD5:
224 case RT1015_DC_CALIB_CLSD6:
225 case RT1015_DC_CALIB_CLSD7:
226 case RT1015_DC_CALIB_CLSD8:
227 case RT1015_S_BST_TIMING_INTER1:
228 case RT1015_OSCK_STA:
229 case RT1015_MONO_DYNA_CTRL1:
230 case RT1015_MONO_DYNA_CTRL5:
238 static bool rt1015_readable_register(struct device *dev, unsigned int reg)
254 case RT1015_CUSTOMER_ID:
255 case RT1015_PCODE_FWVER:
257 case RT1015_VENDOR_ID:
258 case RT1015_DEVICE_ID:
259 case RT1015_PAD_DRV1:
260 case RT1015_PAD_DRV2:
261 case RT1015_GAT_BOOST:
263 case RT1015_OSCK_STA:
270 case RT1015_TDM_MASTER:
271 case RT1015_TDM_TCON:
279 case RT1015_ANA_PROTECT1:
280 case RT1015_ANA_CTRL_SEQ1:
281 case RT1015_ANA_CTRL_SEQ2:
282 case RT1015_VBAT_DET_DEB:
283 case RT1015_VBAT_VOLT_DET1:
284 case RT1015_VBAT_VOLT_DET2:
285 case RT1015_VBAT_TEST_OUT1:
286 case RT1015_VBAT_TEST_OUT2:
287 case RT1015_VBAT_PROT_ATT:
288 case RT1015_VBAT_DET_CODE:
296 case RT1015_CLASSD_SEQ:
297 case RT1015_SMART_BST_CTRL1:
298 case RT1015_SMART_BST_CTRL2:
299 case RT1015_ANA_CTRL1:
300 case RT1015_ANA_CTRL2:
301 case RT1015_PWR_STATE_CTRL:
302 case RT1015_MONO_DYNA_CTRL:
303 case RT1015_MONO_DYNA_CTRL1:
304 case RT1015_MONO_DYNA_CTRL2:
305 case RT1015_MONO_DYNA_CTRL3:
306 case RT1015_MONO_DYNA_CTRL4:
307 case RT1015_MONO_DYNA_CTRL5:
309 case RT1015_SHORT_DETTOP1:
310 case RT1015_SHORT_DETTOP2:
311 case RT1015_SPK_DC_DETECT1:
312 case RT1015_SPK_DC_DETECT2:
313 case RT1015_SPK_DC_DETECT3:
314 case RT1015_SPK_DC_DETECT4:
315 case RT1015_SPK_DC_DETECT5:
316 case RT1015_BAT_RPO_STEP1:
317 case RT1015_BAT_RPO_STEP2:
318 case RT1015_BAT_RPO_STEP3:
319 case RT1015_BAT_RPO_STEP4:
320 case RT1015_BAT_RPO_STEP5:
321 case RT1015_BAT_RPO_STEP6:
322 case RT1015_BAT_RPO_STEP7:
323 case RT1015_BAT_RPO_STEP8:
324 case RT1015_BAT_RPO_STEP9:
325 case RT1015_BAT_RPO_STEP10:
326 case RT1015_BAT_RPO_STEP11:
327 case RT1015_BAT_RPO_STEP12:
328 case RT1015_SPREAD_SPEC1:
329 case RT1015_SPREAD_SPEC2:
330 case RT1015_PAD_STATUS:
331 case RT1015_PADS_PULLING_CTRL1:
332 case RT1015_PADS_DRIVING:
333 case RT1015_SYS_RST1:
334 case RT1015_SYS_RST2:
335 case RT1015_SYS_GATING1:
336 case RT1015_TEST_MODE1:
337 case RT1015_TEST_MODE2:
338 case RT1015_TIMING_CTRL1:
340 case RT1015_TEST_OUT1:
341 case RT1015_DC_CALIB_CLSD1:
342 case RT1015_DC_CALIB_CLSD2:
343 case RT1015_DC_CALIB_CLSD3:
344 case RT1015_DC_CALIB_CLSD4:
345 case RT1015_DC_CALIB_CLSD5:
346 case RT1015_DC_CALIB_CLSD6:
347 case RT1015_DC_CALIB_CLSD7:
348 case RT1015_DC_CALIB_CLSD8:
349 case RT1015_DC_CALIB_CLSD9:
350 case RT1015_DC_CALIB_CLSD10:
351 case RT1015_CLSD_INTERNAL1:
352 case RT1015_CLSD_INTERNAL2:
353 case RT1015_CLSD_INTERNAL3:
354 case RT1015_CLSD_INTERNAL4:
355 case RT1015_CLSD_INTERNAL5:
356 case RT1015_CLSD_INTERNAL6:
357 case RT1015_CLSD_INTERNAL7:
358 case RT1015_CLSD_INTERNAL8:
359 case RT1015_CLSD_INTERNAL9:
360 case RT1015_CLSD_OCP_CTRL:
366 case RT1015_VREF_LV1:
367 case RT1015_S_BST_TIMING_INTER1:
368 case RT1015_S_BST_TIMING_INTER2:
369 case RT1015_S_BST_TIMING_INTER3:
370 case RT1015_S_BST_TIMING_INTER4:
371 case RT1015_S_BST_TIMING_INTER5:
372 case RT1015_S_BST_TIMING_INTER6:
373 case RT1015_S_BST_TIMING_INTER7:
374 case RT1015_S_BST_TIMING_INTER8:
375 case RT1015_S_BST_TIMING_INTER9:
376 case RT1015_S_BST_TIMING_INTER10:
377 case RT1015_S_BST_TIMING_INTER11:
378 case RT1015_S_BST_TIMING_INTER12:
379 case RT1015_S_BST_TIMING_INTER13:
380 case RT1015_S_BST_TIMING_INTER14:
381 case RT1015_S_BST_TIMING_INTER15:
382 case RT1015_S_BST_TIMING_INTER16:
383 case RT1015_S_BST_TIMING_INTER17:
384 case RT1015_S_BST_TIMING_INTER18:
385 case RT1015_S_BST_TIMING_INTER19:
386 case RT1015_S_BST_TIMING_INTER20:
387 case RT1015_S_BST_TIMING_INTER21:
388 case RT1015_S_BST_TIMING_INTER22:
389 case RT1015_S_BST_TIMING_INTER23:
390 case RT1015_S_BST_TIMING_INTER24:
391 case RT1015_S_BST_TIMING_INTER25:
392 case RT1015_S_BST_TIMING_INTER26:
393 case RT1015_S_BST_TIMING_INTER27:
394 case RT1015_S_BST_TIMING_INTER28:
395 case RT1015_S_BST_TIMING_INTER29:
396 case RT1015_S_BST_TIMING_INTER30:
397 case RT1015_S_BST_TIMING_INTER31:
398 case RT1015_S_BST_TIMING_INTER32:
399 case RT1015_S_BST_TIMING_INTER33:
400 case RT1015_S_BST_TIMING_INTER34:
401 case RT1015_S_BST_TIMING_INTER35:
402 case RT1015_S_BST_TIMING_INTER36:
410 static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -9525, 75, 0);
412 static const char * const rt1015_din_source_select[] = {
415 "Left + Right average",
418 static SOC_ENUM_SINGLE_DECL(rt1015_mono_lr_sel, RT1015_PAD_DRV2, 4,
419 rt1015_din_source_select);
421 static const char * const rt1015_boost_mode[] = {
422 "Bypass", "Adaptive", "Fixed Adaptive"
425 static SOC_ENUM_SINGLE_DECL(rt1015_boost_mode_enum, 0, 0,
428 static int rt1015_boost_mode_get(struct snd_kcontrol *kcontrol,
429 struct snd_ctl_elem_value *ucontrol)
431 struct snd_soc_component *component =
432 snd_soc_kcontrol_component(kcontrol);
433 struct rt1015_priv *rt1015 =
434 snd_soc_component_get_drvdata(component);
436 ucontrol->value.integer.value[0] = rt1015->boost_mode;
441 static int rt1015_boost_mode_put(struct snd_kcontrol *kcontrol,
442 struct snd_ctl_elem_value *ucontrol)
444 struct snd_soc_component *component =
445 snd_soc_kcontrol_component(kcontrol);
446 struct rt1015_priv *rt1015 =
447 snd_soc_component_get_drvdata(component);
449 rt1015->boost_mode = ucontrol->value.integer.value[0];
451 switch (rt1015->boost_mode) {
453 snd_soc_component_update_bits(component,
454 RT1015_SMART_BST_CTRL1, RT1015_ABST_AUTO_EN_MASK |
455 RT1015_ABST_FIX_TGT_MASK | RT1015_BYPASS_SWR_REG_MASK,
456 RT1015_ABST_REG_MODE | RT1015_ABST_FIX_TGT_DIS |
457 RT1015_BYPASS_SWRREG_BYPASS);
460 snd_soc_component_update_bits(component,
461 RT1015_SMART_BST_CTRL1, RT1015_ABST_AUTO_EN_MASK |
462 RT1015_ABST_FIX_TGT_MASK | RT1015_BYPASS_SWR_REG_MASK,
463 RT1015_ABST_AUTO_MODE | RT1015_ABST_FIX_TGT_DIS |
464 RT1015_BYPASS_SWRREG_PASS);
467 snd_soc_component_update_bits(component,
468 RT1015_SMART_BST_CTRL1, RT1015_ABST_AUTO_EN_MASK |
469 RT1015_ABST_FIX_TGT_MASK | RT1015_BYPASS_SWR_REG_MASK,
470 RT1015_ABST_AUTO_MODE | RT1015_ABST_FIX_TGT_EN |
471 RT1015_BYPASS_SWRREG_PASS);
474 dev_err(component->dev, "Unknown boost control.\n");
480 static int rt1015_bypass_boost_get(struct snd_kcontrol *kcontrol,
481 struct snd_ctl_elem_value *ucontrol)
483 struct snd_soc_component *component =
484 snd_soc_kcontrol_component(kcontrol);
485 struct rt1015_priv *rt1015 =
486 snd_soc_component_get_drvdata(component);
488 ucontrol->value.integer.value[0] = rt1015->bypass_boost;
493 static void rt1015_calibrate(struct rt1015_priv *rt1015)
495 struct snd_soc_component *component = rt1015->component;
496 struct regmap *regmap = rt1015->regmap;
498 snd_soc_dapm_mutex_lock(&component->dapm);
499 regcache_cache_bypass(regmap, true);
501 regmap_write(regmap, RT1015_PWR1, 0xd7df);
502 regmap_write(regmap, RT1015_PWR4, 0x00b2);
503 regmap_write(regmap, RT1015_CLSD_INTERNAL8, 0x2008);
504 regmap_write(regmap, RT1015_CLSD_INTERNAL9, 0x0140);
505 regmap_write(regmap, RT1015_GAT_BOOST, 0x0efe);
506 regmap_write(regmap, RT1015_PWR_STATE_CTRL, 0x000d);
507 regmap_write(regmap, RT1015_PWR_STATE_CTRL, 0x000e);
508 regmap_write(regmap, RT1015_DC_CALIB_CLSD1, 0x5a00);
509 regmap_write(regmap, RT1015_DC_CALIB_CLSD1, 0x5a01);
510 regmap_write(regmap, RT1015_DC_CALIB_CLSD1, 0x5a05);
512 regmap_write(regmap, RT1015_PWR1, 0x0);
514 regcache_cache_bypass(regmap, false);
515 regcache_mark_dirty(regmap);
516 regcache_sync(regmap);
517 snd_soc_dapm_mutex_unlock(&component->dapm);
520 static int rt1015_bypass_boost_put(struct snd_kcontrol *kcontrol,
521 struct snd_ctl_elem_value *ucontrol)
523 struct snd_soc_component *component =
524 snd_soc_kcontrol_component(kcontrol);
525 struct rt1015_priv *rt1015 =
526 snd_soc_component_get_drvdata(component);
528 if (!rt1015->dac_is_used) {
529 rt1015->bypass_boost = ucontrol->value.integer.value[0];
530 if (rt1015->bypass_boost == RT1015_Bypass_Boost &&
531 !rt1015->cali_done) {
532 rt1015_calibrate(rt1015);
533 rt1015->cali_done = 1;
535 regmap_write(rt1015->regmap, RT1015_MONO_DYNA_CTRL, 0x0010);
538 dev_err(component->dev, "DAC is being used!\n");
543 static void rt1015_flush_work(struct work_struct *work)
545 struct rt1015_priv *rt1015 = container_of(work, struct rt1015_priv,
547 struct snd_soc_component *component = rt1015->component;
548 unsigned int val, i = 0, count = 200;
551 usleep_range(1000, 1500);
552 dev_dbg(component->dev, "Flush DAC (retry:%u)\n", i);
553 regmap_read(rt1015->regmap, RT1015_CLK_DET, &val);
559 regmap_write(rt1015->regmap, RT1015_SYS_RST1, 0x0597);
560 regmap_write(rt1015->regmap, RT1015_SYS_RST1, 0x05f7);
561 regmap_write(rt1015->regmap, RT1015_MAN_I2C, 0x0028);
564 dev_dbg(component->dev, "Flush DAC completed.\n");
566 dev_warn(component->dev, "Fail to flush DAC data.\n");
569 static const struct snd_kcontrol_new rt1015_snd_controls[] = {
570 SOC_SINGLE_TLV("DAC Playback Volume", RT1015_DAC1, RT1015_DAC_VOL_SFT,
571 127, 0, dac_vol_tlv),
572 SOC_DOUBLE("DAC Playback Switch", RT1015_DAC3,
573 RT1015_DA_MUTE_SFT, RT1015_DVOL_MUTE_FLAG_SFT, 1, 1),
574 SOC_ENUM_EXT("Boost Mode", rt1015_boost_mode_enum,
575 rt1015_boost_mode_get, rt1015_boost_mode_put),
576 SOC_ENUM("Mono LR Select", rt1015_mono_lr_sel),
577 SOC_SINGLE_EXT("Bypass Boost", SND_SOC_NOPM, 0, 1, 0,
578 rt1015_bypass_boost_get, rt1015_bypass_boost_put),
581 static int rt1015_is_sys_clk_from_pll(struct snd_soc_dapm_widget *source,
582 struct snd_soc_dapm_widget *sink)
584 struct snd_soc_component *component =
585 snd_soc_dapm_to_component(source->dapm);
586 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
588 if (rt1015->sysclk_src == RT1015_SCLK_S_PLL)
594 static int r1015_dac_event(struct snd_soc_dapm_widget *w,
595 struct snd_kcontrol *kcontrol, int event)
597 struct snd_soc_component *component =
598 snd_soc_dapm_to_component(w->dapm);
599 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
602 case SND_SOC_DAPM_PRE_PMU:
603 rt1015->dac_is_used = 1;
604 if (rt1015->bypass_boost == RT1015_Enable_Boost) {
605 snd_soc_component_write(component,
606 RT1015_SYS_RST1, 0x05f7);
607 snd_soc_component_write(component,
608 RT1015_GAT_BOOST, 0xacfe);
609 snd_soc_component_write(component,
610 RT1015_PWR9, 0xaa00);
611 snd_soc_component_write(component,
612 RT1015_GAT_BOOST, 0xecfe);
614 snd_soc_component_write(component,
615 RT1015_SYS_RST1, 0x05f7);
616 snd_soc_component_write(component,
617 RT1015_PWR_STATE_CTRL, 0x026e);
621 case SND_SOC_DAPM_POST_PMU:
622 regmap_write(rt1015->regmap, RT1015_MAN_I2C, 0x00a8);
625 case SND_SOC_DAPM_POST_PMD:
626 if (rt1015->bypass_boost == RT1015_Enable_Boost) {
627 snd_soc_component_write(component,
628 RT1015_PWR9, 0xa800);
629 snd_soc_component_write(component,
630 RT1015_SYS_RST1, 0x05f5);
632 snd_soc_component_write(component,
633 RT1015_PWR_STATE_CTRL, 0x0268);
634 snd_soc_component_write(component,
635 RT1015_SYS_RST1, 0x05f5);
637 rt1015->dac_is_used = 0;
639 cancel_delayed_work_sync(&rt1015->flush_work);
648 static int rt1015_amp_drv_event(struct snd_soc_dapm_widget *w,
649 struct snd_kcontrol *kcontrol, int event)
651 struct snd_soc_component *component =
652 snd_soc_dapm_to_component(w->dapm);
653 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
656 case SND_SOC_DAPM_POST_PMU:
657 if (rt1015->hw_config == RT1015_HW_28)
658 schedule_delayed_work(&rt1015->flush_work, msecs_to_jiffies(10));
659 msleep(rt1015->pdata.power_up_delay_ms);
667 static const struct snd_soc_dapm_widget rt1015_dapm_widgets[] = {
668 SND_SOC_DAPM_SUPPLY("LDO2", RT1015_PWR1, RT1015_PWR_LDO2_BIT, 0,
670 SND_SOC_DAPM_SUPPLY("INT RC CLK", RT1015_PWR1, RT1015_PWR_INTCLK_BIT,
672 SND_SOC_DAPM_SUPPLY("ISENSE", RT1015_PWR1, RT1015_PWR_ISENSE_BIT, 0,
674 SND_SOC_DAPM_SUPPLY("VSENSE", RT1015_PWR1, RT1015_PWR_VSENSE_BIT, 0,
676 SND_SOC_DAPM_SUPPLY("PLL", RT1015_PWR1, RT1015_PWR_PLL_BIT, 0,
678 SND_SOC_DAPM_SUPPLY("BG1 BG2", RT1015_PWR1, RT1015_PWR_BG_1_2_BIT, 0,
680 SND_SOC_DAPM_SUPPLY("MBIAS BG", RT1015_PWR1, RT1015_PWR_MBIAS_BG_BIT, 0,
682 SND_SOC_DAPM_SUPPLY("VBAT", RT1015_PWR1, RT1015_PWR_VBAT_BIT, 0, NULL,
684 SND_SOC_DAPM_SUPPLY("MBIAS", RT1015_PWR1, RT1015_PWR_MBIAS_BIT, 0,
686 SND_SOC_DAPM_SUPPLY("ADCV", RT1015_PWR1, RT1015_PWR_ADCV_BIT, 0, NULL,
688 SND_SOC_DAPM_SUPPLY("MIXERV", RT1015_PWR1, RT1015_PWR_MIXERV_BIT, 0,
690 SND_SOC_DAPM_SUPPLY("SUMV", RT1015_PWR1, RT1015_PWR_SUMV_BIT, 0, NULL,
692 SND_SOC_DAPM_SUPPLY("VREFLV", RT1015_PWR1, RT1015_PWR_VREFLV_BIT, 0,
695 SND_SOC_DAPM_AIF_IN("AIFRX", "AIF Playback", 0, SND_SOC_NOPM, 0, 0),
696 SND_SOC_DAPM_DAC_E("DAC", NULL, RT1015_PWR1, RT1015_PWR_DAC_BIT, 0,
697 r1015_dac_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
698 SND_SOC_DAPM_POST_PMD),
700 SND_SOC_DAPM_OUT_DRV_E("Amp Drv", SND_SOC_NOPM, 0, 0, NULL, 0,
701 rt1015_amp_drv_event, SND_SOC_DAPM_POST_PMU),
702 SND_SOC_DAPM_OUTPUT("SPO"),
705 static const struct snd_soc_dapm_route rt1015_dapm_routes[] = {
706 { "DAC", NULL, "AIFRX" },
707 { "DAC", NULL, "LDO2" },
708 { "DAC", NULL, "PLL", rt1015_is_sys_clk_from_pll},
709 { "DAC", NULL, "INT RC CLK" },
710 { "DAC", NULL, "ISENSE" },
711 { "DAC", NULL, "VSENSE" },
712 { "DAC", NULL, "BG1 BG2" },
713 { "DAC", NULL, "MBIAS BG" },
714 { "DAC", NULL, "VBAT" },
715 { "DAC", NULL, "MBIAS" },
716 { "DAC", NULL, "ADCV" },
717 { "DAC", NULL, "MIXERV" },
718 { "DAC", NULL, "SUMV" },
719 { "DAC", NULL, "VREFLV" },
720 { "Amp Drv", NULL, "DAC" },
721 { "SPO", NULL, "Amp Drv" },
724 static int rt1015_hw_params(struct snd_pcm_substream *substream,
725 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
727 struct snd_soc_component *component = dai->component;
728 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
729 int pre_div, bclk_ms, frame_size;
730 unsigned int val_len = 0;
732 rt1015->lrck = params_rate(params);
733 pre_div = rl6231_get_clk_info(rt1015->sysclk, rt1015->lrck);
735 dev_err(component->dev, "Unsupported clock rate\n");
739 frame_size = snd_soc_params_to_frame_size(params);
740 if (frame_size < 0) {
741 dev_err(component->dev, "Unsupported frame size: %d\n",
746 bclk_ms = frame_size > 32;
747 rt1015->bclk = rt1015->lrck * (32 << bclk_ms);
749 dev_dbg(component->dev, "bclk_ms is %d and pre_div is %d for iis %d\n",
750 bclk_ms, pre_div, dai->id);
752 dev_dbg(component->dev, "lrck is %dHz and pre_div is %d for iis %d\n",
753 rt1015->lrck, pre_div, dai->id);
755 switch (params_width(params)) {
759 val_len = RT1015_I2S_DL_20;
762 val_len = RT1015_I2S_DL_24;
765 val_len = RT1015_I2S_DL_8;
771 snd_soc_component_update_bits(component, RT1015_TDM_MASTER,
772 RT1015_I2S_DL_MASK, val_len);
773 snd_soc_component_update_bits(component, RT1015_CLK2,
774 RT1015_FS_PD_MASK, pre_div << RT1015_FS_PD_SFT);
779 static int rt1015_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
781 struct snd_soc_component *component = dai->component;
782 unsigned int reg_val = 0, reg_val2 = 0;
784 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
785 case SND_SOC_DAIFMT_CBM_CFM:
786 reg_val |= RT1015_TCON_TDM_MS_M;
788 case SND_SOC_DAIFMT_CBS_CFS:
789 reg_val |= RT1015_TCON_TDM_MS_S;
795 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
796 case SND_SOC_DAIFMT_NB_NF:
798 case SND_SOC_DAIFMT_IB_NF:
799 reg_val2 |= RT1015_TDM_INV_BCLK;
805 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
806 case SND_SOC_DAIFMT_I2S:
809 case SND_SOC_DAIFMT_LEFT_J:
810 reg_val |= RT1015_I2S_M_DF_LEFT;
813 case SND_SOC_DAIFMT_DSP_A:
814 reg_val |= RT1015_I2S_M_DF_PCM_A;
817 case SND_SOC_DAIFMT_DSP_B:
818 reg_val |= RT1015_I2S_M_DF_PCM_B;
825 snd_soc_component_update_bits(component, RT1015_TDM_MASTER,
826 RT1015_TCON_TDM_MS_MASK | RT1015_I2S_M_DF_MASK,
828 snd_soc_component_update_bits(component, RT1015_TDM1_1,
829 RT1015_TDM_INV_BCLK_MASK, reg_val2);
834 static int rt1015_set_component_sysclk(struct snd_soc_component *component,
835 int clk_id, int source, unsigned int freq, int dir)
837 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
838 unsigned int reg_val = 0;
840 if (freq == rt1015->sysclk && clk_id == rt1015->sysclk_src)
844 case RT1015_SCLK_S_MCLK:
845 reg_val |= RT1015_CLK_SYS_PRE_SEL_MCLK;
848 case RT1015_SCLK_S_PLL:
849 reg_val |= RT1015_CLK_SYS_PRE_SEL_PLL;
853 dev_err(component->dev, "Invalid clock id (%d)\n", clk_id);
857 rt1015->sysclk = freq;
858 rt1015->sysclk_src = clk_id;
860 dev_dbg(component->dev, "Sysclk is %dHz and clock id is %d\n",
863 snd_soc_component_update_bits(component, RT1015_CLK2,
864 RT1015_CLK_SYS_PRE_SEL_MASK, reg_val);
869 static int rt1015_set_component_pll(struct snd_soc_component *component,
870 int pll_id, int source, unsigned int freq_in,
871 unsigned int freq_out)
873 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
874 struct rl6231_pll_code pll_code;
877 if (!freq_in || !freq_out) {
878 dev_dbg(component->dev, "PLL disabled\n");
886 if (source == rt1015->pll_src && freq_in == rt1015->pll_in &&
887 freq_out == rt1015->pll_out)
890 if (source == RT1015_PLL_S_BCLK) {
891 if (rt1015->bclk_ratio == 0) {
892 dev_err(component->dev,
893 "Can not support bclk ratio as 0.\n");
899 case RT1015_PLL_S_MCLK:
900 snd_soc_component_update_bits(component, RT1015_CLK2,
901 RT1015_PLL_SEL_MASK, RT1015_PLL_SEL_PLL_SRC2);
904 case RT1015_PLL_S_BCLK:
905 snd_soc_component_update_bits(component, RT1015_CLK2,
906 RT1015_PLL_SEL_MASK, RT1015_PLL_SEL_BCLK);
910 dev_err(component->dev, "Unknown PLL Source %d\n", source);
914 ret = rl6231_pll_calc(freq_in, freq_out, &pll_code);
916 dev_err(component->dev, "Unsupport input clock %d\n", freq_in);
920 dev_dbg(component->dev, "bypass=%d m=%d n=%d k=%d\n",
921 pll_code.m_bp, (pll_code.m_bp ? 0 : pll_code.m_code),
922 pll_code.n_code, pll_code.k_code);
924 snd_soc_component_write(component, RT1015_PLL1,
925 (pll_code.m_bp ? 0 : pll_code.m_code) << RT1015_PLL_M_SFT |
926 pll_code.m_bp << RT1015_PLL_M_BP_SFT | pll_code.n_code);
927 snd_soc_component_write(component, RT1015_PLL2,
930 rt1015->pll_in = freq_in;
931 rt1015->pll_out = freq_out;
932 rt1015->pll_src = source;
937 static int rt1015_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
939 struct snd_soc_component *component = dai->component;
940 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
942 dev_dbg(component->dev, "%s ratio=%d\n", __func__, ratio);
944 rt1015->bclk_ratio = ratio;
947 dev_dbg(component->dev, "Unsupport bclk ratio\n");
954 static int rt1015_probe(struct snd_soc_component *component)
956 struct rt1015_priv *rt1015 =
957 snd_soc_component_get_drvdata(component);
959 rt1015->component = component;
960 rt1015->bclk_ratio = 0;
961 rt1015->cali_done = 0;
962 snd_soc_component_write(component, RT1015_BAT_RPO_STEP1, 0x061c);
964 INIT_DELAYED_WORK(&rt1015->flush_work, rt1015_flush_work);
969 static void rt1015_remove(struct snd_soc_component *component)
971 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
973 cancel_delayed_work_sync(&rt1015->flush_work);
974 regmap_write(rt1015->regmap, RT1015_RESET, 0);
977 #define RT1015_STEREO_RATES SNDRV_PCM_RATE_8000_192000
978 #define RT1015_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
979 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
981 static struct snd_soc_dai_ops rt1015_aif_dai_ops = {
982 .hw_params = rt1015_hw_params,
983 .set_fmt = rt1015_set_dai_fmt,
984 .set_bclk_ratio = rt1015_set_bclk_ratio,
987 static struct snd_soc_dai_driver rt1015_dai[] = {
989 .name = "rt1015-aif",
992 .stream_name = "AIF Playback",
995 .rates = RT1015_STEREO_RATES,
996 .formats = RT1015_FORMATS,
998 .ops = &rt1015_aif_dai_ops,
1003 static int rt1015_suspend(struct snd_soc_component *component)
1005 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
1007 regcache_cache_only(rt1015->regmap, true);
1008 regcache_mark_dirty(rt1015->regmap);
1013 static int rt1015_resume(struct snd_soc_component *component)
1015 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
1017 regcache_cache_only(rt1015->regmap, false);
1018 regcache_sync(rt1015->regmap);
1022 #define rt1015_suspend NULL
1023 #define rt1015_resume NULL
1026 static const struct snd_soc_component_driver soc_component_dev_rt1015 = {
1027 .probe = rt1015_probe,
1028 .remove = rt1015_remove,
1029 .suspend = rt1015_suspend,
1030 .resume = rt1015_resume,
1031 .controls = rt1015_snd_controls,
1032 .num_controls = ARRAY_SIZE(rt1015_snd_controls),
1033 .dapm_widgets = rt1015_dapm_widgets,
1034 .num_dapm_widgets = ARRAY_SIZE(rt1015_dapm_widgets),
1035 .dapm_routes = rt1015_dapm_routes,
1036 .num_dapm_routes = ARRAY_SIZE(rt1015_dapm_routes),
1037 .set_sysclk = rt1015_set_component_sysclk,
1038 .set_pll = rt1015_set_component_pll,
1039 .use_pmdown_time = 1,
1041 .non_legacy_dai_naming = 1,
1044 static const struct regmap_config rt1015_regmap = {
1047 .max_register = RT1015_S_BST_TIMING_INTER36,
1048 .volatile_reg = rt1015_volatile_register,
1049 .readable_reg = rt1015_readable_register,
1050 .cache_type = REGCACHE_RBTREE,
1051 .reg_defaults = rt1015_reg,
1052 .num_reg_defaults = ARRAY_SIZE(rt1015_reg),
1055 static const struct i2c_device_id rt1015_i2c_id[] = {
1059 MODULE_DEVICE_TABLE(i2c, rt1015_i2c_id);
1061 #if defined(CONFIG_OF)
1062 static const struct of_device_id rt1015_of_match[] = {
1063 { .compatible = "realtek,rt1015", },
1066 MODULE_DEVICE_TABLE(of, rt1015_of_match);
1070 static struct acpi_device_id rt1015_acpi_match[] = {
1074 MODULE_DEVICE_TABLE(acpi, rt1015_acpi_match);
1077 static void rt1015_parse_dt(struct rt1015_priv *rt1015, struct device *dev)
1079 device_property_read_u32(dev, "realtek,power-up-delay-ms",
1080 &rt1015->pdata.power_up_delay_ms);
1083 static int rt1015_i2c_probe(struct i2c_client *i2c,
1084 const struct i2c_device_id *id)
1086 struct rt1015_platform_data *pdata = dev_get_platdata(&i2c->dev);
1087 struct rt1015_priv *rt1015;
1091 rt1015 = devm_kzalloc(&i2c->dev, sizeof(struct rt1015_priv),
1096 i2c_set_clientdata(i2c, rt1015);
1098 rt1015->pdata = i2s_default_platform_data;
1101 rt1015->pdata = *pdata;
1103 rt1015_parse_dt(rt1015, &i2c->dev);
1105 rt1015->regmap = devm_regmap_init_i2c(i2c, &rt1015_regmap);
1106 if (IS_ERR(rt1015->regmap)) {
1107 ret = PTR_ERR(rt1015->regmap);
1108 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
1113 rt1015->hw_config = (i2c->addr == 0x29) ? RT1015_HW_29 : RT1015_HW_28;
1115 regmap_read(rt1015->regmap, RT1015_DEVICE_ID, &val);
1116 if ((val != RT1015_DEVICE_ID_VAL) && (val != RT1015_DEVICE_ID_VAL2)) {
1118 "Device with ID register %x is not rt1015\n", val);
1122 return devm_snd_soc_register_component(&i2c->dev,
1123 &soc_component_dev_rt1015,
1124 rt1015_dai, ARRAY_SIZE(rt1015_dai));
1127 static void rt1015_i2c_shutdown(struct i2c_client *client)
1129 struct rt1015_priv *rt1015 = i2c_get_clientdata(client);
1131 regmap_write(rt1015->regmap, RT1015_RESET, 0);
1134 static struct i2c_driver rt1015_i2c_driver = {
1137 .of_match_table = of_match_ptr(rt1015_of_match),
1138 .acpi_match_table = ACPI_PTR(rt1015_acpi_match),
1140 .probe = rt1015_i2c_probe,
1141 .shutdown = rt1015_i2c_shutdown,
1142 .id_table = rt1015_i2c_id,
1144 module_i2c_driver(rt1015_i2c_driver);
1146 MODULE_DESCRIPTION("ASoC RT1015 driver");
1147 MODULE_AUTHOR("Jack Yu <jack.yu@realtek.com>");
1148 MODULE_LICENSE("GPL v2");