1 // SPDX-License-Identifier: GPL-2.0-only
3 // rt1017-sdca-sdw.c -- rt1017 SDCA ALSA SoC amplifier audio driver
5 // Copyright(c) 2023 Realtek Semiconductor Corp.
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/pm_runtime.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 #include <sound/core.h>
15 #include <sound/pcm.h>
16 #include <sound/pcm_params.h>
17 #include <sound/soc.h>
18 #include <sound/soc-dapm.h>
19 #include <sound/initval.h>
20 #include <sound/tlv.h>
22 #include "rt1017-sdca-sdw.h"
24 static bool rt1017_sdca_readable_register(struct device *dev, unsigned int reg)
41 case 0xc325 ... 0xc328:
44 case 0xc350 ... 0xc351:
56 case 0xc540 ... 0xc542:
57 case 0xc550 ... 0xc552:
66 case 0xca09 ... 0xca0c:
67 case 0xca0e ... 0xca0f:
68 case 0xca10 ... 0xca11:
69 case 0xca16 ... 0xca17:
74 case 0xd01a ... 0xd01c:
80 case 0xd545 ... 0xd548:
81 case 0xd5a5 ... 0xd5a8:
82 case 0xd5aa ... 0xd5ad:
83 case 0xda04 ... 0xda07:
84 case 0xda09 ... 0xda0a:
85 case 0xda0c ... 0xda0f:
86 case 0xda11 ... 0xda14:
87 case 0xda16 ... 0xda19:
88 case 0xdab6 ... 0xdabb:
89 case 0xdb09 ... 0xdb0a:
92 case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1017_SDCA_ENT_UDMPU21,
93 RT1017_SDCA_CTL_UDMPU_CLUSTER, 0):
94 case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1017_SDCA_ENT_FU,
95 RT1017_SDCA_CTL_FU_MUTE, 0x01):
96 case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1017_SDCA_ENT_XU22,
97 RT1017_SDCA_CTL_BYPASS, 0):
98 case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1017_SDCA_ENT_SAPU29,
99 RT1017_SDCA_CTL_PROT_STAT, 0):
100 case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1017_SDCA_ENT_CS21,
101 RT1017_SDCA_CTL_FS_INDEX, 0):
102 case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1017_SDCA_ENT_PDE23,
103 RT1017_SDCA_CTL_REQ_POWER_STATE, 0):
104 case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1017_SDCA_ENT_PDE22,
105 RT1017_SDCA_CTL_REQ_POWER_STATE, 0):
112 static bool rt1017_sdca_volatile_register(struct device *dev, unsigned int reg)
120 case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1017_SDCA_ENT_SAPU29,
121 RT1017_SDCA_CTL_PROT_STAT, 0):
128 static const struct reg_sequence rt1017_blind_write[] = {
242 #define RT1017_MAX_REG_NUM 0x4108ffff
244 static const struct regmap_config rt1017_sdca_regmap = {
247 .readable_reg = rt1017_sdca_readable_register,
248 .volatile_reg = rt1017_sdca_volatile_register,
249 .max_register = RT1017_MAX_REG_NUM,
250 .reg_defaults = rt1017_sdca_reg_defaults,
251 .num_reg_defaults = ARRAY_SIZE(rt1017_sdca_reg_defaults),
252 .cache_type = REGCACHE_MAPLE,
253 .use_single_read = true,
254 .use_single_write = true,
257 static int rt1017_sdca_read_prop(struct sdw_slave *slave)
259 struct sdw_slave_prop *prop = &slave->prop;
264 struct sdw_dpn_prop *dpn;
266 prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY;
267 prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY;
269 prop->paging_support = true;
271 /* first we need to allocate memory for set bits in port lists
272 * port = 1 for AMP playback
273 * port = 2 for IV capture
275 prop->source_ports = BIT(2); /* BITMAP: 00000100 */
276 prop->sink_ports = BIT(1); /* BITMAP: 00000010 */
278 nval = hweight32(prop->source_ports);
279 prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval,
280 sizeof(*prop->src_dpn_prop), GFP_KERNEL);
281 if (!prop->src_dpn_prop)
285 dpn = prop->src_dpn_prop;
286 addr = prop->source_ports;
287 for_each_set_bit(bit, &addr, 32) {
289 dpn[i].type = SDW_DPN_FULL;
290 dpn[i].simple_ch_prep_sm = true;
291 dpn[i].ch_prep_timeout = 10;
295 /* do this again for sink now */
296 nval = hweight32(prop->sink_ports);
297 prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
298 sizeof(*prop->sink_dpn_prop), GFP_KERNEL);
299 if (!prop->sink_dpn_prop)
303 dpn = prop->sink_dpn_prop;
304 addr = prop->sink_ports;
305 for_each_set_bit(bit, &addr, 32) {
307 dpn[j].type = SDW_DPN_FULL;
308 dpn[j].simple_ch_prep_sm = true;
309 dpn[j].ch_prep_timeout = 10;
313 /* set the timeout values */
314 prop->clk_stop_timeout = 64;
319 static int rt1017_sdca_io_init(struct device *dev, struct sdw_slave *slave)
321 struct rt1017_sdca_priv *rt1017 = dev_get_drvdata(dev);
326 if (rt1017->first_hw_init) {
327 regcache_cache_only(rt1017->regmap, false);
328 regcache_cache_bypass(rt1017->regmap, true);
331 * PM runtime is only enabled when a Slave reports as Attached
334 /* set autosuspend parameters */
335 pm_runtime_set_autosuspend_delay(&slave->dev, 3000);
336 pm_runtime_use_autosuspend(&slave->dev);
338 /* update count of parent 'active' children */
339 pm_runtime_set_active(&slave->dev);
341 /* make sure the device does not suspend immediately */
342 pm_runtime_mark_last_busy(&slave->dev);
344 pm_runtime_enable(&slave->dev);
347 pm_runtime_get_noresume(&slave->dev);
350 regmap_write(rt1017->regmap, 0xc000, 0x02);
352 /* initial settings - blind write */
353 regmap_multi_reg_write(rt1017->regmap, rt1017_blind_write,
354 ARRAY_SIZE(rt1017_blind_write));
356 if (rt1017->first_hw_init) {
357 regcache_cache_bypass(rt1017->regmap, false);
358 regcache_mark_dirty(rt1017->regmap);
360 rt1017->first_hw_init = true;
362 /* Mark Slave initialization complete */
363 rt1017->hw_init = true;
365 pm_runtime_mark_last_busy(&slave->dev);
366 pm_runtime_put_autosuspend(&slave->dev);
368 dev_dbg(&slave->dev, "hw_init complete\n");
372 static int rt1017_sdca_update_status(struct sdw_slave *slave,
373 enum sdw_slave_status status)
375 struct rt1017_sdca_priv *rt1017 = dev_get_drvdata(&slave->dev);
377 if (status == SDW_SLAVE_UNATTACHED)
378 rt1017->hw_init = false;
381 * Perform initialization only if slave status is present and
382 * hw_init flag is false
384 if (rt1017->hw_init || status != SDW_SLAVE_ATTACHED)
387 /* perform I/O transfers required for Slave initialization */
388 return rt1017_sdca_io_init(&slave->dev, slave);
391 static const char * const rt1017_rx_data_ch_select[] = {
405 static SOC_ENUM_SINGLE_DECL(rt1017_rx_data_ch_enum,
406 SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1017_SDCA_ENT_UDMPU21,
407 RT1017_SDCA_CTL_UDMPU_CLUSTER, 0),
408 0, rt1017_rx_data_ch_select);
410 static const struct snd_kcontrol_new rt1017_sdca_controls[] = {
411 /* UDMPU Cluster Selection */
412 SOC_ENUM("RX Channel Select", rt1017_rx_data_ch_enum),
415 static const struct snd_kcontrol_new rt1017_sto_dac =
416 SOC_DAPM_SINGLE("Switch",
417 SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1017_SDCA_ENT_FU, RT1017_SDCA_CTL_FU_MUTE, 0x1),
420 static int rt1017_sdca_pde23_event(struct snd_soc_dapm_widget *w,
421 struct snd_kcontrol *kcontrol, int event)
423 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
424 struct rt1017_sdca_priv *rt1017 = snd_soc_component_get_drvdata(component);
425 unsigned char ps0 = 0x0, ps3 = 0x3;
428 case SND_SOC_DAPM_POST_PMU:
429 regmap_write(rt1017->regmap,
430 SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1017_SDCA_ENT_PDE23,
431 RT1017_SDCA_CTL_REQ_POWER_STATE, 0),
434 case SND_SOC_DAPM_PRE_PMD:
435 regmap_write(rt1017->regmap,
436 SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1017_SDCA_ENT_PDE23,
437 RT1017_SDCA_CTL_REQ_POWER_STATE, 0),
446 static int rt1017_sdca_classd_event(struct snd_soc_dapm_widget *w,
447 struct snd_kcontrol *kcontrol, int event)
449 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
450 struct rt1017_sdca_priv *rt1017 = snd_soc_component_get_drvdata(component);
453 case SND_SOC_DAPM_POST_PMU:
454 regmap_update_bits(rt1017->regmap, RT1017_PWM_TRIM_1,
455 RT1017_PWM_FREQ_CTL_SRC_SEL_MASK, RT1017_PWM_FREQ_CTL_SRC_SEL_REG);
456 regmap_write(rt1017->regmap, RT1017_CLASSD_INT_1, 0x10);
465 static int rt1017_sdca_feedback_event(struct snd_soc_dapm_widget *w,
466 struct snd_kcontrol *kcontrol, int event)
468 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
469 struct rt1017_sdca_priv *rt1017 = snd_soc_component_get_drvdata(component);
472 case SND_SOC_DAPM_PRE_PMU:
473 regmap_update_bits(rt1017->regmap, 0xd017, 0x1f, 0x08);
475 case SND_SOC_DAPM_POST_PMD:
476 regmap_update_bits(rt1017->regmap, 0xd017, 0x1f, 0x09);
485 static const struct snd_soc_dapm_widget rt1017_sdca_dapm_widgets[] = {
486 /* Audio Interface */
487 SND_SOC_DAPM_AIF_IN("DP1RX", "DP1 Playback", 0, SND_SOC_NOPM, 0, 0),
488 SND_SOC_DAPM_AIF_OUT_E("DP2TX", "DP2 Capture", 0, SND_SOC_NOPM, 0, 0,
489 rt1017_sdca_feedback_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
491 /* Digital Interface */
492 SND_SOC_DAPM_SWITCH("DAC", SND_SOC_NOPM, 0, 0, &rt1017_sto_dac),
495 SND_SOC_DAPM_PGA_E("CLASS D", SND_SOC_NOPM, 0, 0, NULL, 0,
496 rt1017_sdca_classd_event, SND_SOC_DAPM_POST_PMU),
497 SND_SOC_DAPM_OUTPUT("SPO"),
499 SND_SOC_DAPM_SUPPLY("PDE23", SND_SOC_NOPM, 0, 0,
500 rt1017_sdca_pde23_event, SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
502 SND_SOC_DAPM_PGA("I Sense", SND_SOC_NOPM, 0, 0, NULL, 0),
503 SND_SOC_DAPM_PGA("V Sense", SND_SOC_NOPM, 0, 0, NULL, 0),
504 SND_SOC_DAPM_SIGGEN("I Gen"),
505 SND_SOC_DAPM_SIGGEN("V Gen"),
508 static const struct snd_soc_dapm_route rt1017_sdca_dapm_routes[] = {
510 { "DAC", "Switch", "DP1RX" },
511 { "CLASS D", NULL, "DAC" },
512 { "CLASS D", NULL, "PDE23" },
513 { "SPO", NULL, "CLASS D" },
515 { "I Sense", NULL, "I Gen" },
516 { "V Sense", NULL, "V Gen" },
517 { "I Sense", NULL, "PDE23" },
518 { "V Sense", NULL, "PDE23" },
519 { "DP2TX", NULL, "I Sense" },
520 { "DP2TX", NULL, "V Sense" },
523 static const struct sdw_slave_ops rt1017_sdca_slave_ops = {
524 .read_prop = rt1017_sdca_read_prop,
525 .update_status = rt1017_sdca_update_status,
528 static int rt1017_sdca_component_probe(struct snd_soc_component *component)
532 ret = pm_runtime_resume(component->dev);
533 if (ret < 0 && ret != -EACCES)
539 static void rt1017_sdca_component_remove(struct snd_soc_component *component)
541 struct rt1017_sdca_priv *rt1017 = snd_soc_component_get_drvdata(component);
543 regcache_cache_only(rt1017->regmap, true);
546 static const struct snd_soc_component_driver soc_sdca_component_rt1017 = {
547 .probe = rt1017_sdca_component_probe,
548 .remove = rt1017_sdca_component_remove,
549 .controls = rt1017_sdca_controls,
550 .num_controls = ARRAY_SIZE(rt1017_sdca_controls),
551 .dapm_widgets = rt1017_sdca_dapm_widgets,
552 .num_dapm_widgets = ARRAY_SIZE(rt1017_sdca_dapm_widgets),
553 .dapm_routes = rt1017_sdca_dapm_routes,
554 .num_dapm_routes = ARRAY_SIZE(rt1017_sdca_dapm_routes),
558 static int rt1017_sdca_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
561 snd_soc_dai_dma_data_set(dai, direction, sdw_stream);
566 static void rt1017_sdca_shutdown(struct snd_pcm_substream *substream,
567 struct snd_soc_dai *dai)
569 snd_soc_dai_set_dma_data(dai, substream, NULL);
572 static int rt1017_sdca_pcm_hw_params(struct snd_pcm_substream *substream,
573 struct snd_pcm_hw_params *params,
574 struct snd_soc_dai *dai)
576 struct snd_soc_component *component = dai->component;
577 struct rt1017_sdca_priv *rt1017 = snd_soc_component_get_drvdata(component);
578 struct sdw_stream_config stream_config;
579 struct sdw_port_config port_config;
580 enum sdw_data_direction direction;
581 struct sdw_stream_runtime *sdw_stream;
582 int retval, port, num_channels, ch_mask;
583 unsigned int sampling_rate;
585 dev_dbg(dai->dev, "%s %s", __func__, dai->name);
586 sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
591 if (!rt1017->sdw_slave)
594 /* SoundWire specific configuration */
595 /* port 1 for playback */
596 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
597 direction = SDW_DATA_DIR_RX;
600 direction = SDW_DATA_DIR_TX;
604 num_channels = params_channels(params);
605 ch_mask = (1 << num_channels) - 1;
607 stream_config.frame_rate = params_rate(params);
608 stream_config.ch_count = num_channels;
609 stream_config.bps = snd_pcm_format_width(params_format(params));
610 stream_config.direction = direction;
612 port_config.ch_mask = ch_mask;
613 port_config.num = port;
615 dev_dbg(dai->dev, "frame_rate %d, ch_count %d, bps %d, direction %d, ch_mask %d, port: %d\n",
616 params_rate(params), num_channels, snd_pcm_format_width(params_format(params)),
617 direction, ch_mask, port);
619 retval = sdw_stream_add_slave(rt1017->sdw_slave, &stream_config,
620 &port_config, 1, sdw_stream);
622 dev_err(dai->dev, "Unable to configure port\n");
626 /* sampling rate configuration */
627 switch (params_rate(params)) {
629 sampling_rate = RT1017_SDCA_RATE_44100HZ;
632 sampling_rate = RT1017_SDCA_RATE_48000HZ;
635 sampling_rate = RT1017_SDCA_RATE_96000HZ;
638 sampling_rate = RT1017_SDCA_RATE_192000HZ;
641 dev_err(component->dev, "Rate %d is not supported\n",
642 params_rate(params));
646 /* set sampling frequency */
647 regmap_write(rt1017->regmap,
648 SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1017_SDCA_ENT_CS21,
649 RT1017_SDCA_CTL_FS_INDEX, 0),
655 static int rt1017_sdca_pcm_hw_free(struct snd_pcm_substream *substream,
656 struct snd_soc_dai *dai)
658 struct snd_soc_component *component = dai->component;
659 struct rt1017_sdca_priv *rt1017 = snd_soc_component_get_drvdata(component);
660 struct sdw_stream_runtime *sdw_stream =
661 snd_soc_dai_get_dma_data(dai, substream);
663 if (!rt1017->sdw_slave)
666 sdw_stream_remove_slave(rt1017->sdw_slave, sdw_stream);
670 static const struct snd_soc_dai_ops rt1017_sdca_ops = {
671 .hw_params = rt1017_sdca_pcm_hw_params,
672 .hw_free = rt1017_sdca_pcm_hw_free,
673 .set_stream = rt1017_sdca_set_sdw_stream,
674 .shutdown = rt1017_sdca_shutdown,
677 #define RT1017_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \
678 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000)
679 #define RT1017_FORMATS (SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S16_LE | \
680 SNDRV_PCM_FMTBIT_S24_LE)
682 static struct snd_soc_dai_driver rt1017_sdca_dai[] = {
684 .name = "rt1017-aif",
686 .stream_name = "DP1 Playback",
689 .rates = RT1017_STEREO_RATES,
690 .formats = RT1017_FORMATS,
693 .stream_name = "DP2 Capture",
696 .rates = RT1017_STEREO_RATES,
697 .formats = RT1017_FORMATS,
699 .ops = &rt1017_sdca_ops,
703 static int rt1017_sdca_init(struct device *dev, struct regmap *regmap,
704 struct sdw_slave *slave)
706 struct rt1017_sdca_priv *rt1017;
709 rt1017 = devm_kzalloc(dev, sizeof(*rt1017), GFP_KERNEL);
713 dev_set_drvdata(dev, rt1017);
714 rt1017->sdw_slave = slave;
715 rt1017->regmap = regmap;
718 * Mark hw_init to false
719 * HW init will be performed when device reports present
721 rt1017->hw_init = false;
722 rt1017->first_hw_init = false;
724 ret = devm_snd_soc_register_component(dev,
725 &soc_sdca_component_rt1017,
727 ARRAY_SIZE(rt1017_sdca_dai));
732 static int rt1017_sdca_sdw_probe(struct sdw_slave *slave,
733 const struct sdw_device_id *id)
735 struct regmap *regmap;
737 /* Regmap Initialization */
738 regmap = devm_regmap_init_sdw(slave, &rt1017_sdca_regmap);
740 return PTR_ERR(regmap);
742 return rt1017_sdca_init(&slave->dev, regmap, slave);
745 static int rt1017_sdca_sdw_remove(struct sdw_slave *slave)
747 struct rt1017_sdca_priv *rt1017 = dev_get_drvdata(&slave->dev);
749 if (rt1017->first_hw_init)
750 pm_runtime_disable(&slave->dev);
755 static const struct sdw_device_id rt1017_sdca_id[] = {
756 SDW_SLAVE_ENTRY_EXT(0x025d, 0x1017, 0x3, 0x1, 0),
759 MODULE_DEVICE_TABLE(sdw, rt1017_sdca_id);
761 static int __maybe_unused rt1017_sdca_dev_suspend(struct device *dev)
763 struct rt1017_sdca_priv *rt1017 = dev_get_drvdata(dev);
765 if (!rt1017->hw_init)
768 regcache_cache_only(rt1017->regmap, true);
773 #define RT1017_PROBE_TIMEOUT 5000
775 static int __maybe_unused rt1017_sdca_dev_resume(struct device *dev)
777 struct sdw_slave *slave = dev_to_sdw_dev(dev);
778 struct rt1017_sdca_priv *rt1017 = dev_get_drvdata(dev);
781 if (!rt1017->first_hw_init)
784 if (!slave->unattach_request)
787 time = wait_for_completion_timeout(&slave->initialization_complete,
788 msecs_to_jiffies(RT1017_PROBE_TIMEOUT));
790 dev_err(&slave->dev, "Initialization not complete, timed out\n");
791 sdw_show_ping_status(slave->bus, true);
797 slave->unattach_request = 0;
798 regcache_cache_only(rt1017->regmap, false);
799 regcache_sync(rt1017->regmap);
804 static const struct dev_pm_ops rt1017_sdca_pm = {
805 SET_SYSTEM_SLEEP_PM_OPS(rt1017_sdca_dev_suspend, rt1017_sdca_dev_resume)
806 SET_RUNTIME_PM_OPS(rt1017_sdca_dev_suspend, rt1017_sdca_dev_resume, NULL)
809 static struct sdw_driver rt1017_sdca_sdw_driver = {
811 .name = "rt1017-sdca",
812 .owner = THIS_MODULE,
813 .pm = &rt1017_sdca_pm,
815 .probe = rt1017_sdca_sdw_probe,
816 .remove = rt1017_sdca_sdw_remove,
817 .ops = &rt1017_sdca_slave_ops,
818 .id_table = rt1017_sdca_id,
820 module_sdw_driver(rt1017_sdca_sdw_driver);
822 MODULE_DESCRIPTION("ASoC RT1017 driver SDCA SDW");
823 MODULE_AUTHOR("Derek Fang <derek.fang@realtek.com>");
824 MODULE_LICENSE("GPL");