1 // SPDX-License-Identifier: GPL-2.0
3 // rt1308-sdw.c -- rt1308 ALSA SoC audio driver
5 // Copyright(c) 2019 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/soundwire/sdw.h>
13 #include <linux/soundwire/sdw_type.h>
14 #include <linux/soundwire/sdw_registers.h>
15 #include <linux/module.h>
16 #include <linux/regmap.h>
17 #include <sound/core.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/soc.h>
21 #include <sound/soc-dapm.h>
22 #include <sound/initval.h>
25 #include "rt1308-sdw.h"
27 static bool rt1308_readable_register(struct device *dev, unsigned int reg)
32 case 0x2f01 ... 0x2f07:
33 case 0x3000 ... 0x3001:
34 case 0x3004 ... 0x3005:
37 case 0xc000 ... 0xcff3:
44 static bool rt1308_volatile_register(struct device *dev, unsigned int reg)
47 case 0x2f01 ... 0x2f07:
48 case 0x3000 ... 0x3001:
49 case 0x3004 ... 0x3005:
54 case 0xc860 ... 0xc863:
55 case 0xc870 ... 0xc873:
62 static const struct regmap_config rt1308_sdw_regmap = {
65 .readable_reg = rt1308_readable_register,
66 .volatile_reg = rt1308_volatile_register,
67 .max_register = 0xcfff,
68 .reg_defaults = rt1308_reg_defaults,
69 .num_reg_defaults = ARRAY_SIZE(rt1308_reg_defaults),
70 .cache_type = REGCACHE_RBTREE,
71 .use_single_read = true,
72 .use_single_write = true,
75 /* Bus clock frequency */
76 #define RT1308_CLK_FREQ_9600000HZ 9600000
77 #define RT1308_CLK_FREQ_12000000HZ 12000000
78 #define RT1308_CLK_FREQ_6000000HZ 6000000
79 #define RT1308_CLK_FREQ_4800000HZ 4800000
80 #define RT1308_CLK_FREQ_2400000HZ 2400000
81 #define RT1308_CLK_FREQ_12288000HZ 12288000
83 static int rt1308_clock_config(struct device *dev)
85 struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(dev);
86 unsigned int clk_freq, value;
88 clk_freq = (rt1308->params.curr_dr_freq >> 1);
91 case RT1308_CLK_FREQ_12000000HZ:
94 case RT1308_CLK_FREQ_6000000HZ:
97 case RT1308_CLK_FREQ_9600000HZ:
100 case RT1308_CLK_FREQ_4800000HZ:
103 case RT1308_CLK_FREQ_2400000HZ:
106 case RT1308_CLK_FREQ_12288000HZ:
113 regmap_write(rt1308->regmap, 0xe0, value);
114 regmap_write(rt1308->regmap, 0xf0, value);
116 dev_dbg(dev, "%s complete, clk_freq=%d\n", __func__, clk_freq);
121 static int rt1308_read_prop(struct sdw_slave *slave)
123 struct sdw_slave_prop *prop = &slave->prop;
127 struct sdw_dpn_prop *dpn;
129 prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY;
130 prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY;
132 prop->paging_support = true;
134 /* first we need to allocate memory for set bits in port lists */
135 prop->source_ports = 0x00; /* BITMAP: 00010100 (not enable yet) */
136 prop->sink_ports = 0x2; /* BITMAP: 00000010 */
139 nval = hweight32(prop->sink_ports);
140 prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
141 sizeof(*prop->sink_dpn_prop),
143 if (!prop->sink_dpn_prop)
147 dpn = prop->sink_dpn_prop;
148 addr = prop->sink_ports;
149 for_each_set_bit(bit, &addr, 32) {
151 dpn[i].type = SDW_DPN_FULL;
152 dpn[i].simple_ch_prep_sm = true;
153 dpn[i].ch_prep_timeout = 10;
157 /* set the timeout values */
158 prop->clk_stop_timeout = 20;
160 dev_dbg(&slave->dev, "%s\n", __func__);
165 static void rt1308_apply_calib_params(struct rt1308_sdw_priv *rt1308)
167 unsigned int efuse_m_btl_l, efuse_m_btl_r, tmp;
168 unsigned int efuse_c_btl_l, efuse_c_btl_r;
170 /* read efuse to apply calibration parameters */
171 regmap_write(rt1308->regmap, 0xc7f0, 0x04);
172 regmap_write(rt1308->regmap, 0xc7f1, 0xfe);
174 regmap_write(rt1308->regmap, 0xc7f0, 0x44);
176 regmap_write(rt1308->regmap, 0xc240, 0x10);
178 regmap_read(rt1308->regmap, 0xc861, &tmp);
180 regmap_read(rt1308->regmap, 0xc860, &tmp);
181 efuse_m_btl_l = efuse_m_btl_l | (tmp << 8);
182 regmap_read(rt1308->regmap, 0xc863, &tmp);
184 regmap_read(rt1308->regmap, 0xc862, &tmp);
185 efuse_c_btl_l = efuse_c_btl_l | (tmp << 8);
186 regmap_read(rt1308->regmap, 0xc871, &tmp);
188 regmap_read(rt1308->regmap, 0xc870, &tmp);
189 efuse_m_btl_r = efuse_m_btl_r | (tmp << 8);
190 regmap_read(rt1308->regmap, 0xc873, &tmp);
192 regmap_read(rt1308->regmap, 0xc872, &tmp);
193 efuse_c_btl_r = efuse_c_btl_r | (tmp << 8);
194 dev_dbg(&rt1308->sdw_slave->dev, "%s m_btl_l=0x%x, m_btl_r=0x%x\n", __func__,
195 efuse_m_btl_l, efuse_m_btl_r);
196 dev_dbg(&rt1308->sdw_slave->dev, "%s c_btl_l=0x%x, c_btl_r=0x%x\n", __func__,
197 efuse_c_btl_l, efuse_c_btl_r);
200 static void rt1308_apply_bq_params(struct rt1308_sdw_priv *rt1308)
202 unsigned int i, reg, data;
204 for (i = 0; i < rt1308->bq_params_cnt; i += 3) {
205 reg = rt1308->bq_params[i] | (rt1308->bq_params[i + 1] << 8);
206 data = rt1308->bq_params[i + 2];
207 regmap_write(rt1308->regmap, reg, data);
211 static int rt1308_io_init(struct device *dev, struct sdw_slave *slave)
213 struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(dev);
220 if (rt1308->first_hw_init) {
221 regcache_cache_only(rt1308->regmap, false);
222 regcache_cache_bypass(rt1308->regmap, true);
226 * PM runtime is only enabled when a Slave reports as Attached
228 if (!rt1308->first_hw_init) {
229 /* set autosuspend parameters */
230 pm_runtime_set_autosuspend_delay(&slave->dev, 3000);
231 pm_runtime_use_autosuspend(&slave->dev);
233 /* update count of parent 'active' children */
234 pm_runtime_set_active(&slave->dev);
236 /* make sure the device does not suspend immediately */
237 pm_runtime_mark_last_busy(&slave->dev);
239 pm_runtime_enable(&slave->dev);
242 pm_runtime_get_noresume(&slave->dev);
245 regmap_write(rt1308->regmap, RT1308_SDW_RESET, 0);
247 regmap_read(rt1308->regmap, 0xc710, &tmp);
248 rt1308->hw_ver = tmp;
249 dev_dbg(dev, "%s, hw_ver=0x%x\n", __func__, rt1308->hw_ver);
251 /* initial settings */
252 regmap_write(rt1308->regmap, 0xc103, 0xc0);
253 regmap_write(rt1308->regmap, 0xc030, 0x17);
254 regmap_write(rt1308->regmap, 0xc031, 0x81);
255 regmap_write(rt1308->regmap, 0xc032, 0x26);
256 regmap_write(rt1308->regmap, 0xc040, 0x80);
257 regmap_write(rt1308->regmap, 0xc041, 0x80);
258 regmap_write(rt1308->regmap, 0xc042, 0x06);
259 regmap_write(rt1308->regmap, 0xc052, 0x0a);
260 regmap_write(rt1308->regmap, 0xc080, 0x0a);
261 regmap_write(rt1308->regmap, 0xc060, 0x02);
262 regmap_write(rt1308->regmap, 0xc061, 0x75);
263 regmap_write(rt1308->regmap, 0xc062, 0x05);
264 regmap_write(rt1308->regmap, 0xc171, 0x07);
265 regmap_write(rt1308->regmap, 0xc173, 0x0d);
266 if (rt1308->hw_ver == RT1308_VER_C) {
267 regmap_write(rt1308->regmap, 0xc311, 0x7f);
268 regmap_write(rt1308->regmap, 0xc300, 0x09);
270 regmap_write(rt1308->regmap, 0xc311, 0x4f);
271 regmap_write(rt1308->regmap, 0xc300, 0x0b);
273 regmap_write(rt1308->regmap, 0xc900, 0x5a);
274 regmap_write(rt1308->regmap, 0xc1a0, 0x84);
275 regmap_write(rt1308->regmap, 0xc1a1, 0x01);
276 regmap_write(rt1308->regmap, 0xc360, 0x78);
277 regmap_write(rt1308->regmap, 0xc361, 0x87);
278 regmap_write(rt1308->regmap, 0xc0a1, 0x71);
279 regmap_write(rt1308->regmap, 0xc210, 0x00);
280 regmap_write(rt1308->regmap, 0xc070, 0x00);
281 regmap_write(rt1308->regmap, 0xc100, 0xd7);
282 regmap_write(rt1308->regmap, 0xc101, 0xd7);
284 if (rt1308->first_hw_init) {
285 regcache_cache_bypass(rt1308->regmap, false);
286 regcache_mark_dirty(rt1308->regmap);
288 rt1308->first_hw_init = true;
290 /* Mark Slave initialization complete */
291 rt1308->hw_init = true;
293 pm_runtime_mark_last_busy(&slave->dev);
294 pm_runtime_put_autosuspend(&slave->dev);
296 dev_dbg(&slave->dev, "%s hw_init complete\n", __func__);
301 static int rt1308_update_status(struct sdw_slave *slave,
302 enum sdw_slave_status status)
304 struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(&slave->dev);
306 /* Update the status */
307 rt1308->status = status;
309 if (status == SDW_SLAVE_UNATTACHED)
310 rt1308->hw_init = false;
313 * Perform initialization only if slave status is present and
314 * hw_init flag is false
316 if (rt1308->hw_init || rt1308->status != SDW_SLAVE_ATTACHED)
319 /* perform I/O transfers required for Slave initialization */
320 return rt1308_io_init(&slave->dev, slave);
323 static int rt1308_bus_config(struct sdw_slave *slave,
324 struct sdw_bus_params *params)
326 struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(&slave->dev);
329 memcpy(&rt1308->params, params, sizeof(*params));
331 ret = rt1308_clock_config(&slave->dev);
333 dev_err(&slave->dev, "Invalid clk config");
338 static int rt1308_interrupt_callback(struct sdw_slave *slave,
339 struct sdw_slave_intr_status *status)
342 "%s control_port_stat=%x", __func__, status->control_port);
347 static int rt1308_classd_event(struct snd_soc_dapm_widget *w,
348 struct snd_kcontrol *kcontrol, int event)
350 struct snd_soc_component *component =
351 snd_soc_dapm_to_component(w->dapm);
352 struct rt1308_sdw_priv *rt1308 =
353 snd_soc_component_get_drvdata(component);
356 case SND_SOC_DAPM_POST_PMU:
358 snd_soc_component_update_bits(component,
359 RT1308_SDW_OFFSET | (RT1308_POWER_STATUS << 4),
362 rt1308_apply_calib_params(rt1308);
364 case SND_SOC_DAPM_PRE_PMD:
365 snd_soc_component_update_bits(component,
366 RT1308_SDW_OFFSET | (RT1308_POWER_STATUS << 4),
368 usleep_range(150000, 200000);
378 static const char * const rt1308_rx_data_ch_select[] = {
385 static SOC_ENUM_SINGLE_DECL(rt1308_rx_data_ch_enum,
386 RT1308_SDW_OFFSET | (RT1308_DATA_PATH << 4), 0,
387 rt1308_rx_data_ch_select);
389 static const struct snd_kcontrol_new rt1308_snd_controls[] = {
391 /* I2S Data Channel Selection */
392 SOC_ENUM("RX Channel Select", rt1308_rx_data_ch_enum),
395 static const struct snd_kcontrol_new rt1308_sto_dac_l =
396 SOC_DAPM_SINGLE_AUTODISABLE("Switch",
397 RT1308_SDW_OFFSET_BYTE3 | (RT1308_DAC_SET << 4),
398 RT1308_DVOL_MUTE_L_EN_SFT, 1, 1);
400 static const struct snd_kcontrol_new rt1308_sto_dac_r =
401 SOC_DAPM_SINGLE_AUTODISABLE("Switch",
402 RT1308_SDW_OFFSET_BYTE3 | (RT1308_DAC_SET << 4),
403 RT1308_DVOL_MUTE_R_EN_SFT, 1, 1);
405 static const struct snd_soc_dapm_widget rt1308_dapm_widgets[] = {
406 /* Audio Interface */
407 SND_SOC_DAPM_AIF_IN("AIF1RX", "DP1 Playback", 0, SND_SOC_NOPM, 0, 0),
410 SND_SOC_DAPM_SUPPLY("MBIAS20U",
411 RT1308_SDW_OFFSET | (RT1308_POWER << 4), 7, 0, NULL, 0),
412 SND_SOC_DAPM_SUPPLY("ALDO",
413 RT1308_SDW_OFFSET | (RT1308_POWER << 4), 6, 0, NULL, 0),
414 SND_SOC_DAPM_SUPPLY("DBG",
415 RT1308_SDW_OFFSET | (RT1308_POWER << 4), 5, 0, NULL, 0),
416 SND_SOC_DAPM_SUPPLY("DACL",
417 RT1308_SDW_OFFSET | (RT1308_POWER << 4), 4, 0, NULL, 0),
418 SND_SOC_DAPM_SUPPLY("CLK25M",
419 RT1308_SDW_OFFSET | (RT1308_POWER << 4), 2, 0, NULL, 0),
420 SND_SOC_DAPM_SUPPLY("ADC_R",
421 RT1308_SDW_OFFSET | (RT1308_POWER << 4), 1, 0, NULL, 0),
422 SND_SOC_DAPM_SUPPLY("ADC_L",
423 RT1308_SDW_OFFSET | (RT1308_POWER << 4), 0, 0, NULL, 0),
424 SND_SOC_DAPM_SUPPLY("DAC Power",
425 RT1308_SDW_OFFSET | (RT1308_POWER << 4), 3, 0, NULL, 0),
427 SND_SOC_DAPM_SUPPLY("DLDO",
428 RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4), 5, 0, NULL, 0),
429 SND_SOC_DAPM_SUPPLY("VREF",
430 RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4), 4, 0, NULL, 0),
431 SND_SOC_DAPM_SUPPLY("MIXER_R",
432 RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4), 2, 0, NULL, 0),
433 SND_SOC_DAPM_SUPPLY("MIXER_L",
434 RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4), 1, 0, NULL, 0),
435 SND_SOC_DAPM_SUPPLY("MBIAS4U",
436 RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4), 0, 0, NULL, 0),
438 SND_SOC_DAPM_SUPPLY("PLL2_LDO",
439 RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 4, 0, NULL, 0),
440 SND_SOC_DAPM_SUPPLY("PLL2B",
441 RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 3, 0, NULL, 0),
442 SND_SOC_DAPM_SUPPLY("PLL2F",
443 RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 2, 0, NULL, 0),
444 SND_SOC_DAPM_SUPPLY("PLL2F2",
445 RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 1, 0, NULL, 0),
446 SND_SOC_DAPM_SUPPLY("PLL2B2",
447 RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 0, 0, NULL, 0),
449 /* Digital Interface */
450 SND_SOC_DAPM_DAC("DAC", NULL, SND_SOC_NOPM, 0, 0),
451 SND_SOC_DAPM_SWITCH("DAC L", SND_SOC_NOPM, 0, 0, &rt1308_sto_dac_l),
452 SND_SOC_DAPM_SWITCH("DAC R", SND_SOC_NOPM, 0, 0, &rt1308_sto_dac_r),
455 SND_SOC_DAPM_PGA_E("CLASS D", SND_SOC_NOPM, 0, 0, NULL, 0,
457 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
458 SND_SOC_DAPM_OUTPUT("SPOL"),
459 SND_SOC_DAPM_OUTPUT("SPOR"),
462 static const struct snd_soc_dapm_route rt1308_dapm_routes[] = {
464 { "DAC", NULL, "AIF1RX" },
466 { "DAC", NULL, "MBIAS20U" },
467 { "DAC", NULL, "ALDO" },
468 { "DAC", NULL, "DBG" },
469 { "DAC", NULL, "DACL" },
470 { "DAC", NULL, "CLK25M" },
471 { "DAC", NULL, "ADC_R" },
472 { "DAC", NULL, "ADC_L" },
473 { "DAC", NULL, "DLDO" },
474 { "DAC", NULL, "VREF" },
475 { "DAC", NULL, "MIXER_R" },
476 { "DAC", NULL, "MIXER_L" },
477 { "DAC", NULL, "MBIAS4U" },
478 { "DAC", NULL, "PLL2_LDO" },
479 { "DAC", NULL, "PLL2B" },
480 { "DAC", NULL, "PLL2F" },
481 { "DAC", NULL, "PLL2F2" },
482 { "DAC", NULL, "PLL2B2" },
484 { "DAC L", "Switch", "DAC" },
485 { "DAC R", "Switch", "DAC" },
486 { "DAC L", NULL, "DAC Power" },
487 { "DAC R", NULL, "DAC Power" },
489 { "CLASS D", NULL, "DAC L" },
490 { "CLASS D", NULL, "DAC R" },
491 { "SPOL", NULL, "CLASS D" },
492 { "SPOR", NULL, "CLASS D" },
495 static int rt1308_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
498 struct sdw_stream_data *stream;
503 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
507 stream->sdw_stream = sdw_stream;
509 /* Use tx_mask or rx_mask to configure stream tag and set dma_data */
510 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
511 dai->playback_dma_data = stream;
513 dai->capture_dma_data = stream;
518 static void rt1308_sdw_shutdown(struct snd_pcm_substream *substream,
519 struct snd_soc_dai *dai)
521 struct sdw_stream_data *stream;
523 stream = snd_soc_dai_get_dma_data(dai, substream);
524 snd_soc_dai_set_dma_data(dai, substream, NULL);
528 static int rt1308_sdw_set_tdm_slot(struct snd_soc_dai *dai,
529 unsigned int tx_mask,
530 unsigned int rx_mask,
531 int slots, int slot_width)
533 struct snd_soc_component *component = dai->component;
534 struct rt1308_sdw_priv *rt1308 =
535 snd_soc_component_get_drvdata(component);
543 rt1308->rx_mask = rx_mask;
544 rt1308->slots = slots;
545 /* slot_width is not used since it's irrelevant for SoundWire */
550 static int rt1308_sdw_hw_params(struct snd_pcm_substream *substream,
551 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
553 struct snd_soc_component *component = dai->component;
554 struct rt1308_sdw_priv *rt1308 =
555 snd_soc_component_get_drvdata(component);
556 struct sdw_stream_config stream_config;
557 struct sdw_port_config port_config;
558 enum sdw_data_direction direction;
559 struct sdw_stream_data *stream;
560 int retval, port, num_channels, ch_mask;
562 dev_dbg(dai->dev, "%s %s", __func__, dai->name);
563 stream = snd_soc_dai_get_dma_data(dai, substream);
568 if (!rt1308->sdw_slave)
571 /* SoundWire specific configuration */
572 /* port 1 for playback */
573 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
574 direction = SDW_DATA_DIR_RX;
581 num_channels = rt1308->slots;
582 ch_mask = rt1308->rx_mask;
584 num_channels = params_channels(params);
585 ch_mask = (1 << num_channels) - 1;
588 stream_config.frame_rate = params_rate(params);
589 stream_config.ch_count = num_channels;
590 stream_config.bps = snd_pcm_format_width(params_format(params));
591 stream_config.direction = direction;
593 port_config.ch_mask = ch_mask;
594 port_config.num = port;
596 retval = sdw_stream_add_slave(rt1308->sdw_slave, &stream_config,
597 &port_config, 1, stream->sdw_stream);
599 dev_err(dai->dev, "Unable to configure port\n");
606 static int rt1308_sdw_pcm_hw_free(struct snd_pcm_substream *substream,
607 struct snd_soc_dai *dai)
609 struct snd_soc_component *component = dai->component;
610 struct rt1308_sdw_priv *rt1308 =
611 snd_soc_component_get_drvdata(component);
612 struct sdw_stream_data *stream =
613 snd_soc_dai_get_dma_data(dai, substream);
615 if (!rt1308->sdw_slave)
618 sdw_stream_remove_slave(rt1308->sdw_slave, stream->sdw_stream);
623 * slave_ops: callbacks for get_clock_stop_mode, clock_stop and
624 * port_prep are not defined for now
626 static const struct sdw_slave_ops rt1308_slave_ops = {
627 .read_prop = rt1308_read_prop,
628 .interrupt_callback = rt1308_interrupt_callback,
629 .update_status = rt1308_update_status,
630 .bus_config = rt1308_bus_config,
633 static int rt1308_sdw_parse_dt(struct rt1308_sdw_priv *rt1308, struct device *dev)
637 device_property_read_u32(dev, "realtek,bq-params-cnt", &rt1308->bq_params_cnt);
638 if (rt1308->bq_params_cnt) {
639 rt1308->bq_params = devm_kzalloc(dev, rt1308->bq_params_cnt, GFP_KERNEL);
640 if (!rt1308->bq_params) {
641 dev_err(dev, "Could not allocate bq_params memory\n");
644 ret = device_property_read_u8_array(dev, "realtek,bq-params", rt1308->bq_params, rt1308->bq_params_cnt);
646 dev_err(dev, "Could not read list of realtek,bq-params\n");
650 dev_dbg(dev, "bq_params_cnt=%d\n", rt1308->bq_params_cnt);
654 static int rt1308_sdw_component_probe(struct snd_soc_component *component)
656 struct rt1308_sdw_priv *rt1308 = snd_soc_component_get_drvdata(component);
659 rt1308->component = component;
660 rt1308_sdw_parse_dt(rt1308, &rt1308->sdw_slave->dev);
662 ret = pm_runtime_resume(component->dev);
663 if (ret < 0 && ret != -EACCES)
666 /* apply BQ params */
667 rt1308_apply_bq_params(rt1308);
672 static const struct snd_soc_component_driver soc_component_sdw_rt1308 = {
673 .probe = rt1308_sdw_component_probe,
674 .controls = rt1308_snd_controls,
675 .num_controls = ARRAY_SIZE(rt1308_snd_controls),
676 .dapm_widgets = rt1308_dapm_widgets,
677 .num_dapm_widgets = ARRAY_SIZE(rt1308_dapm_widgets),
678 .dapm_routes = rt1308_dapm_routes,
679 .num_dapm_routes = ARRAY_SIZE(rt1308_dapm_routes),
683 static const struct snd_soc_dai_ops rt1308_aif_dai_ops = {
684 .hw_params = rt1308_sdw_hw_params,
685 .hw_free = rt1308_sdw_pcm_hw_free,
686 .set_stream = rt1308_set_sdw_stream,
687 .shutdown = rt1308_sdw_shutdown,
688 .set_tdm_slot = rt1308_sdw_set_tdm_slot,
691 #define RT1308_STEREO_RATES SNDRV_PCM_RATE_48000
692 #define RT1308_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
693 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S16_LE | \
694 SNDRV_PCM_FMTBIT_S24_LE)
696 static struct snd_soc_dai_driver rt1308_sdw_dai[] = {
698 .name = "rt1308-aif",
700 .stream_name = "DP1 Playback",
703 .rates = RT1308_STEREO_RATES,
704 .formats = RT1308_FORMATS,
706 .ops = &rt1308_aif_dai_ops,
710 static int rt1308_sdw_init(struct device *dev, struct regmap *regmap,
711 struct sdw_slave *slave)
713 struct rt1308_sdw_priv *rt1308;
716 rt1308 = devm_kzalloc(dev, sizeof(*rt1308), GFP_KERNEL);
720 dev_set_drvdata(dev, rt1308);
721 rt1308->sdw_slave = slave;
722 rt1308->regmap = regmap;
725 * Mark hw_init to false
726 * HW init will be performed when device reports present
728 rt1308->hw_init = false;
729 rt1308->first_hw_init = false;
731 ret = devm_snd_soc_register_component(dev,
732 &soc_component_sdw_rt1308,
734 ARRAY_SIZE(rt1308_sdw_dai));
736 dev_dbg(&slave->dev, "%s\n", __func__);
741 static int rt1308_sdw_probe(struct sdw_slave *slave,
742 const struct sdw_device_id *id)
744 struct regmap *regmap;
746 /* Regmap Initialization */
747 regmap = devm_regmap_init_sdw(slave, &rt1308_sdw_regmap);
749 return PTR_ERR(regmap);
751 rt1308_sdw_init(&slave->dev, regmap, slave);
756 static int rt1308_sdw_remove(struct sdw_slave *slave)
758 struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(&slave->dev);
760 if (rt1308->first_hw_init)
761 pm_runtime_disable(&slave->dev);
766 static const struct sdw_device_id rt1308_id[] = {
767 SDW_SLAVE_ENTRY_EXT(0x025d, 0x1308, 0x2, 0, 0),
770 MODULE_DEVICE_TABLE(sdw, rt1308_id);
772 static int __maybe_unused rt1308_dev_suspend(struct device *dev)
774 struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(dev);
776 if (!rt1308->hw_init)
779 regcache_cache_only(rt1308->regmap, true);
784 #define RT1308_PROBE_TIMEOUT 5000
786 static int __maybe_unused rt1308_dev_resume(struct device *dev)
788 struct sdw_slave *slave = dev_to_sdw_dev(dev);
789 struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(dev);
792 if (!rt1308->first_hw_init)
795 if (!slave->unattach_request)
798 time = wait_for_completion_timeout(&slave->initialization_complete,
799 msecs_to_jiffies(RT1308_PROBE_TIMEOUT));
801 dev_err(&slave->dev, "Initialization not complete, timed out\n");
802 sdw_show_ping_status(slave->bus, true);
808 slave->unattach_request = 0;
809 regcache_cache_only(rt1308->regmap, false);
810 regcache_sync_region(rt1308->regmap, 0xc000, 0xcfff);
815 static const struct dev_pm_ops rt1308_pm = {
816 SET_SYSTEM_SLEEP_PM_OPS(rt1308_dev_suspend, rt1308_dev_resume)
817 SET_RUNTIME_PM_OPS(rt1308_dev_suspend, rt1308_dev_resume, NULL)
820 static struct sdw_driver rt1308_sdw_driver = {
823 .owner = THIS_MODULE,
826 .probe = rt1308_sdw_probe,
827 .remove = rt1308_sdw_remove,
828 .ops = &rt1308_slave_ops,
829 .id_table = rt1308_id,
831 module_sdw_driver(rt1308_sdw_driver);
833 MODULE_DESCRIPTION("ASoC RT1308 driver SDW");
834 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
835 MODULE_LICENSE("GPL v2");