1 // SPDX-License-Identifier: GPL-2.0
3 * rt715.c -- rt715 ALSA SoC audio driver
5 * Copyright(c) 2019 Realtek Semiconductor Corp.
7 * ALC715 ASoC Codec Driver based Intel Dummy SdW codec driver
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/delay.h>
15 #include <linux/i2c.h>
16 #include <linux/pm_runtime.h>
18 #include <linux/soundwire/sdw.h>
19 #include <linux/gpio.h>
20 #include <linux/regmap.h>
21 #include <linux/slab.h>
22 #include <linux/platform_device.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/gpio/consumer.h>
26 #include <linux/of_gpio.h>
27 #include <linux/of_device.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/soc-dapm.h>
33 #include <sound/initval.h>
34 #include <sound/tlv.h>
35 #include <sound/hda_verbs.h>
39 static int rt715_index_write(struct regmap
*regmap
, unsigned int reg
,
43 unsigned int addr
= ((RT715_PRIV_INDEX_W_H
) << 8) | reg
;
45 ret
= regmap_write(regmap
, addr
, value
);
47 pr_err("Failed to set private value: %08x <= %04x %d\n", ret
,
54 static void rt715_get_gain(struct rt715_priv
*rt715
, unsigned int addr_h
,
55 unsigned int addr_l
, unsigned int val_h
,
56 unsigned int *r_val
, unsigned int *l_val
)
60 *r_val
= (val_h
<< 8);
61 ret
= regmap_read(rt715
->regmap
, addr_l
, r_val
);
63 pr_err("Failed to get R channel gain.\n");
67 *l_val
= (val_h
<< 8);
68 ret
= regmap_read(rt715
->regmap
, addr_h
, l_val
);
70 pr_err("Failed to get L channel gain.\n");
73 /* For Verb-Set Amplifier Gain (Verb ID = 3h) */
74 static int rt715_set_amp_gain_put(struct snd_kcontrol
*kcontrol
,
75 struct snd_ctl_elem_value
*ucontrol
)
77 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
78 struct snd_soc_dapm_context
*dapm
=
79 snd_soc_component_get_dapm(component
);
80 struct soc_mixer_control
*mc
=
81 (struct soc_mixer_control
*)kcontrol
->private_value
;
82 struct rt715_priv
*rt715
= snd_soc_component_get_drvdata(component
);
83 unsigned int addr_h
, addr_l
, val_h
, val_ll
, val_lr
;
84 unsigned int read_ll
, read_rl
;
87 /* Can't use update bit function, so read the original value first */
90 if (mc
->shift
== RT715_DIR_OUT_SFT
) /* output */
95 rt715_get_gain(rt715
, addr_h
, addr_l
, val_h
, &read_rl
, &read_ll
);
100 val_ll
= (mc
->max
- ucontrol
->value
.integer
.value
[0]) << 7;
102 read_ll
= read_ll
& 0x7f;
106 val_ll
= ((ucontrol
->value
.integer
.value
[0]) & 0x7f);
107 if (val_ll
> mc
->max
)
109 /* keep mute status */
110 read_ll
= read_ll
& 0x80;
116 regmap_write(rt715
->regmap
,
117 RT715_SET_AUDIO_POWER_STATE
, AC_PWRST_D0
);
119 val_lr
= (mc
->max
- ucontrol
->value
.integer
.value
[1]) << 7;
121 read_rl
= read_rl
& 0x7f;
125 val_lr
= ((ucontrol
->value
.integer
.value
[1]) & 0x7f);
126 if (val_lr
> mc
->max
)
128 /* keep mute status */
129 read_rl
= read_rl
& 0x80;
133 for (i
= 0; i
< 3; i
++) { /* retry 3 times at most */
135 if (val_ll
== val_lr
) {
136 /* Set both L/R channels at the same time */
137 val_h
= (1 << mc
->shift
) | (3 << 4);
138 regmap_write(rt715
->regmap
, addr_h
,
139 (val_h
<< 8 | val_ll
));
140 regmap_write(rt715
->regmap
, addr_l
,
141 (val_h
<< 8 | val_ll
));
144 val_h
= (1 << mc
->shift
) | (1 << 5);
145 regmap_write(rt715
->regmap
, addr_h
,
146 (val_h
<< 8 | val_ll
));
148 val_h
= (1 << mc
->shift
) | (1 << 4);
149 regmap_write(rt715
->regmap
, addr_l
,
150 (val_h
<< 8 | val_lr
));
153 if (mc
->shift
== RT715_DIR_OUT_SFT
) /* output */
158 rt715_get_gain(rt715
, addr_h
, addr_l
, val_h
,
160 if (read_rl
== val_lr
&& read_ll
== val_ll
)
163 /* D0:power on state, D3: power saving mode */
164 if (dapm
->bias_level
<= SND_SOC_BIAS_STANDBY
)
165 regmap_write(rt715
->regmap
,
166 RT715_SET_AUDIO_POWER_STATE
, AC_PWRST_D3
);
170 static int rt715_set_amp_gain_get(struct snd_kcontrol
*kcontrol
,
171 struct snd_ctl_elem_value
*ucontrol
)
173 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
174 struct rt715_priv
*rt715
= snd_soc_component_get_drvdata(component
);
175 struct soc_mixer_control
*mc
=
176 (struct soc_mixer_control
*)kcontrol
->private_value
;
177 unsigned int addr_h
, addr_l
, val_h
;
178 unsigned int read_ll
, read_rl
;
182 if (mc
->shift
== RT715_DIR_OUT_SFT
) /* output */
187 rt715_get_gain(rt715
, addr_h
, addr_l
, val_h
, &read_rl
, &read_ll
);
190 /* for mute status */
191 read_ll
= !((read_ll
& 0x80) >> RT715_MUTE_SFT
);
192 read_rl
= !((read_rl
& 0x80) >> RT715_MUTE_SFT
);
195 read_ll
= read_ll
& 0x7f;
196 read_rl
= read_rl
& 0x7f;
198 ucontrol
->value
.integer
.value
[0] = read_ll
;
199 ucontrol
->value
.integer
.value
[1] = read_rl
;
204 static const DECLARE_TLV_DB_SCALE(in_vol_tlv
, -1725, 75, 0);
205 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv
, 0, 1000, 0);
207 #define SOC_DOUBLE_R_EXT(xname, reg_left, reg_right, xshift, xmax, xinvert,\
208 xhandler_get, xhandler_put) \
209 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
210 .info = snd_soc_info_volsw, \
211 .get = xhandler_get, .put = xhandler_put, \
212 .private_value = SOC_DOUBLE_R_VALUE(reg_left, reg_right, xshift, \
215 static const struct snd_kcontrol_new rt715_snd_controls
[] = {
217 SOC_DOUBLE_R_EXT("ADC 07 Capture Switch", RT715_SET_GAIN_MIC_ADC_H
,
218 RT715_SET_GAIN_MIC_ADC_L
, RT715_DIR_IN_SFT
, 1, 1,
219 rt715_set_amp_gain_get
, rt715_set_amp_gain_put
),
220 SOC_DOUBLE_R_EXT("ADC 08 Capture Switch", RT715_SET_GAIN_LINE_ADC_H
,
221 RT715_SET_GAIN_LINE_ADC_L
, RT715_DIR_IN_SFT
, 1, 1,
222 rt715_set_amp_gain_get
, rt715_set_amp_gain_put
),
223 SOC_DOUBLE_R_EXT("ADC 09 Capture Switch", RT715_SET_GAIN_MIX_ADC_H
,
224 RT715_SET_GAIN_MIX_ADC_L
, RT715_DIR_IN_SFT
, 1, 1,
225 rt715_set_amp_gain_get
, rt715_set_amp_gain_put
),
226 SOC_DOUBLE_R_EXT("ADC 27 Capture Switch", RT715_SET_GAIN_MIX_ADC2_H
,
227 RT715_SET_GAIN_MIX_ADC2_L
, RT715_DIR_IN_SFT
, 1, 1,
228 rt715_set_amp_gain_get
, rt715_set_amp_gain_put
),
230 SOC_DOUBLE_R_EXT_TLV("ADC 07 Capture Volume", RT715_SET_GAIN_MIC_ADC_H
,
231 RT715_SET_GAIN_MIC_ADC_L
, RT715_DIR_IN_SFT
, 0x3f, 0,
232 rt715_set_amp_gain_get
, rt715_set_amp_gain_put
,
234 SOC_DOUBLE_R_EXT_TLV("ADC 08 Capture Volume", RT715_SET_GAIN_LINE_ADC_H
,
235 RT715_SET_GAIN_LINE_ADC_L
, RT715_DIR_IN_SFT
, 0x3f, 0,
236 rt715_set_amp_gain_get
, rt715_set_amp_gain_put
,
238 SOC_DOUBLE_R_EXT_TLV("ADC 09 Capture Volume", RT715_SET_GAIN_MIX_ADC_H
,
239 RT715_SET_GAIN_MIX_ADC_L
, RT715_DIR_IN_SFT
, 0x3f, 0,
240 rt715_set_amp_gain_get
, rt715_set_amp_gain_put
,
242 SOC_DOUBLE_R_EXT_TLV("ADC 27 Capture Volume", RT715_SET_GAIN_MIX_ADC2_H
,
243 RT715_SET_GAIN_MIX_ADC2_L
, RT715_DIR_IN_SFT
, 0x3f, 0,
244 rt715_set_amp_gain_get
, rt715_set_amp_gain_put
,
246 /* MIC Boost Control */
247 SOC_DOUBLE_R_EXT_TLV("DMIC1 Boost", RT715_SET_GAIN_DMIC1_H
,
248 RT715_SET_GAIN_DMIC1_L
, RT715_DIR_IN_SFT
, 3, 0,
249 rt715_set_amp_gain_get
, rt715_set_amp_gain_put
,
251 SOC_DOUBLE_R_EXT_TLV("DMIC2 Boost", RT715_SET_GAIN_DMIC2_H
,
252 RT715_SET_GAIN_DMIC2_L
, RT715_DIR_IN_SFT
, 3, 0,
253 rt715_set_amp_gain_get
, rt715_set_amp_gain_put
,
255 SOC_DOUBLE_R_EXT_TLV("DMIC3 Boost", RT715_SET_GAIN_DMIC3_H
,
256 RT715_SET_GAIN_DMIC3_L
, RT715_DIR_IN_SFT
, 3, 0,
257 rt715_set_amp_gain_get
, rt715_set_amp_gain_put
,
259 SOC_DOUBLE_R_EXT_TLV("DMIC4 Boost", RT715_SET_GAIN_DMIC4_H
,
260 RT715_SET_GAIN_DMIC4_L
, RT715_DIR_IN_SFT
, 3, 0,
261 rt715_set_amp_gain_get
, rt715_set_amp_gain_put
,
263 SOC_DOUBLE_R_EXT_TLV("MIC1 Boost", RT715_SET_GAIN_MIC1_H
,
264 RT715_SET_GAIN_MIC1_L
, RT715_DIR_IN_SFT
, 3, 0,
265 rt715_set_amp_gain_get
, rt715_set_amp_gain_put
,
267 SOC_DOUBLE_R_EXT_TLV("MIC2 Boost", RT715_SET_GAIN_MIC2_H
,
268 RT715_SET_GAIN_MIC2_L
, RT715_DIR_IN_SFT
, 3, 0,
269 rt715_set_amp_gain_get
, rt715_set_amp_gain_put
,
271 SOC_DOUBLE_R_EXT_TLV("LINE1 Boost", RT715_SET_GAIN_LINE1_H
,
272 RT715_SET_GAIN_LINE1_L
, RT715_DIR_IN_SFT
, 3, 0,
273 rt715_set_amp_gain_get
, rt715_set_amp_gain_put
,
275 SOC_DOUBLE_R_EXT_TLV("LINE2 Boost", RT715_SET_GAIN_LINE2_H
,
276 RT715_SET_GAIN_LINE2_L
, RT715_DIR_IN_SFT
, 3, 0,
277 rt715_set_amp_gain_get
, rt715_set_amp_gain_put
,
281 static int rt715_mux_get(struct snd_kcontrol
*kcontrol
,
282 struct snd_ctl_elem_value
*ucontrol
)
284 struct snd_soc_component
*component
=
285 snd_soc_dapm_kcontrol_component(kcontrol
);
286 struct rt715_priv
*rt715
= snd_soc_component_get_drvdata(component
);
287 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
288 unsigned int reg
, val
;
291 /* nid = e->reg, vid = 0xf01 */
292 reg
= RT715_VERB_SET_CONNECT_SEL
| e
->reg
;
293 ret
= regmap_read(rt715
->regmap
, reg
, &val
);
295 dev_err(component
->dev
, "%s: sdw read failed: %d\n",
301 * The first two indices of ADC Mux 24/25 are routed to the same
302 * hardware source. ie, ADC Mux 24 0/1 will both connect to MIC2.
303 * To have a unique set of inputs, we skip the index1 of the muxes.
305 if ((e
->reg
== RT715_MUX_IN3
|| e
->reg
== RT715_MUX_IN4
) && (val
> 0))
307 ucontrol
->value
.enumerated
.item
[0] = val
;
312 static int rt715_mux_put(struct snd_kcontrol
*kcontrol
,
313 struct snd_ctl_elem_value
*ucontrol
)
315 struct snd_soc_component
*component
=
316 snd_soc_dapm_kcontrol_component(kcontrol
);
317 struct snd_soc_dapm_context
*dapm
=
318 snd_soc_dapm_kcontrol_dapm(kcontrol
);
319 struct rt715_priv
*rt715
= snd_soc_component_get_drvdata(component
);
320 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
321 unsigned int *item
= ucontrol
->value
.enumerated
.item
;
322 unsigned int val
, val2
= 0, change
, reg
;
325 if (item
[0] >= e
->items
)
328 /* Verb ID = 0x701h, nid = e->reg */
329 val
= snd_soc_enum_item_to_val(e
, item
[0]) << e
->shift_l
;
331 reg
= RT715_VERB_SET_CONNECT_SEL
| e
->reg
;
332 ret
= regmap_read(rt715
->regmap
, reg
, &val2
);
334 dev_err(component
->dev
, "%s: sdw read failed: %d\n",
345 reg
= RT715_VERB_SET_CONNECT_SEL
| e
->reg
;
346 regmap_write(rt715
->regmap
, reg
, val
);
349 snd_soc_dapm_mux_update_power(dapm
, kcontrol
,
355 static const char * const adc_22_23_mux_text
[] = {
367 * Due to mux design for nid 24 (MUX_IN3)/25 (MUX_IN4), connection index 0 and
368 * 1 will be connected to the same dmic source, therefore we skip index 1 to
369 * avoid misunderstanding on usage of dapm routing.
371 static const unsigned int rt715_adc_24_25_values
[] = {
379 static const char * const adc_24_mux_text
[] = {
387 static const char * const adc_25_mux_text
[] = {
395 static SOC_ENUM_SINGLE_DECL(
396 rt715_adc22_enum
, RT715_MUX_IN1
, 0, adc_22_23_mux_text
);
398 static SOC_ENUM_SINGLE_DECL(
399 rt715_adc23_enum
, RT715_MUX_IN2
, 0, adc_22_23_mux_text
);
401 static SOC_VALUE_ENUM_SINGLE_DECL(rt715_adc24_enum
,
402 RT715_MUX_IN3
, 0, 0xf,
403 adc_24_mux_text
, rt715_adc_24_25_values
);
405 static SOC_VALUE_ENUM_SINGLE_DECL(rt715_adc25_enum
,
406 RT715_MUX_IN4
, 0, 0xf,
407 adc_25_mux_text
, rt715_adc_24_25_values
);
409 static const struct snd_kcontrol_new rt715_adc22_mux
=
410 SOC_DAPM_ENUM_EXT("ADC 22 Mux", rt715_adc22_enum
,
411 rt715_mux_get
, rt715_mux_put
);
413 static const struct snd_kcontrol_new rt715_adc23_mux
=
414 SOC_DAPM_ENUM_EXT("ADC 23 Mux", rt715_adc23_enum
,
415 rt715_mux_get
, rt715_mux_put
);
417 static const struct snd_kcontrol_new rt715_adc24_mux
=
418 SOC_DAPM_ENUM_EXT("ADC 24 Mux", rt715_adc24_enum
,
419 rt715_mux_get
, rt715_mux_put
);
421 static const struct snd_kcontrol_new rt715_adc25_mux
=
422 SOC_DAPM_ENUM_EXT("ADC 25 Mux", rt715_adc25_enum
,
423 rt715_mux_get
, rt715_mux_put
);
425 static const struct snd_soc_dapm_widget rt715_dapm_widgets
[] = {
426 SND_SOC_DAPM_INPUT("DMIC1"),
427 SND_SOC_DAPM_INPUT("DMIC2"),
428 SND_SOC_DAPM_INPUT("DMIC3"),
429 SND_SOC_DAPM_INPUT("DMIC4"),
430 SND_SOC_DAPM_INPUT("MIC1"),
431 SND_SOC_DAPM_INPUT("MIC2"),
432 SND_SOC_DAPM_INPUT("LINE1"),
433 SND_SOC_DAPM_INPUT("LINE2"),
434 SND_SOC_DAPM_ADC("ADC 07", NULL
, RT715_SET_STREAMID_MIC_ADC
, 4, 0),
435 SND_SOC_DAPM_ADC("ADC 08", NULL
, RT715_SET_STREAMID_LINE_ADC
, 4, 0),
436 SND_SOC_DAPM_ADC("ADC 09", NULL
, RT715_SET_STREAMID_MIX_ADC
, 4, 0),
437 SND_SOC_DAPM_ADC("ADC 27", NULL
, RT715_SET_STREAMID_MIX_ADC2
, 4, 0),
438 SND_SOC_DAPM_MUX("ADC 22 Mux", SND_SOC_NOPM
, 0, 0,
440 SND_SOC_DAPM_MUX("ADC 23 Mux", SND_SOC_NOPM
, 0, 0,
442 SND_SOC_DAPM_MUX("ADC 24 Mux", SND_SOC_NOPM
, 0, 0,
444 SND_SOC_DAPM_MUX("ADC 25 Mux", SND_SOC_NOPM
, 0, 0,
446 SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM
, 0, 0),
447 SND_SOC_DAPM_AIF_OUT("DP6TX", "DP6 Capture", 0, SND_SOC_NOPM
, 0, 0),
450 static const struct snd_soc_dapm_route rt715_audio_map
[] = {
451 {"DP6TX", NULL
, "ADC 09"},
452 {"DP6TX", NULL
, "ADC 08"},
453 {"DP4TX", NULL
, "ADC 07"},
454 {"DP4TX", NULL
, "ADC 27"},
455 {"ADC 09", NULL
, "ADC 22 Mux"},
456 {"ADC 08", NULL
, "ADC 23 Mux"},
457 {"ADC 07", NULL
, "ADC 24 Mux"},
458 {"ADC 27", NULL
, "ADC 25 Mux"},
459 {"ADC 22 Mux", "MIC1", "MIC1"},
460 {"ADC 22 Mux", "MIC2", "MIC2"},
461 {"ADC 22 Mux", "LINE1", "LINE1"},
462 {"ADC 22 Mux", "LINE2", "LINE2"},
463 {"ADC 22 Mux", "DMIC1", "DMIC1"},
464 {"ADC 22 Mux", "DMIC2", "DMIC2"},
465 {"ADC 22 Mux", "DMIC3", "DMIC3"},
466 {"ADC 22 Mux", "DMIC4", "DMIC4"},
467 {"ADC 23 Mux", "MIC1", "MIC1"},
468 {"ADC 23 Mux", "MIC2", "MIC2"},
469 {"ADC 23 Mux", "LINE1", "LINE1"},
470 {"ADC 23 Mux", "LINE2", "LINE2"},
471 {"ADC 23 Mux", "DMIC1", "DMIC1"},
472 {"ADC 23 Mux", "DMIC2", "DMIC2"},
473 {"ADC 23 Mux", "DMIC3", "DMIC3"},
474 {"ADC 23 Mux", "DMIC4", "DMIC4"},
475 {"ADC 24 Mux", "MIC2", "MIC2"},
476 {"ADC 24 Mux", "DMIC1", "DMIC1"},
477 {"ADC 24 Mux", "DMIC2", "DMIC2"},
478 {"ADC 24 Mux", "DMIC3", "DMIC3"},
479 {"ADC 24 Mux", "DMIC4", "DMIC4"},
480 {"ADC 25 Mux", "MIC1", "MIC1"},
481 {"ADC 25 Mux", "DMIC1", "DMIC1"},
482 {"ADC 25 Mux", "DMIC2", "DMIC2"},
483 {"ADC 25 Mux", "DMIC3", "DMIC3"},
484 {"ADC 25 Mux", "DMIC4", "DMIC4"},
487 static int rt715_set_bias_level(struct snd_soc_component
*component
,
488 enum snd_soc_bias_level level
)
490 struct snd_soc_dapm_context
*dapm
=
491 snd_soc_component_get_dapm(component
);
492 struct rt715_priv
*rt715
= snd_soc_component_get_drvdata(component
);
495 case SND_SOC_BIAS_PREPARE
:
496 if (dapm
->bias_level
== SND_SOC_BIAS_STANDBY
) {
497 regmap_write(rt715
->regmap
,
498 RT715_SET_AUDIO_POWER_STATE
,
500 msleep(RT715_POWER_UP_DELAY_MS
);
504 case SND_SOC_BIAS_STANDBY
:
505 regmap_write(rt715
->regmap
,
506 RT715_SET_AUDIO_POWER_STATE
,
513 dapm
->bias_level
= level
;
517 static const struct snd_soc_component_driver soc_codec_dev_rt715
= {
518 .set_bias_level
= rt715_set_bias_level
,
519 .controls
= rt715_snd_controls
,
520 .num_controls
= ARRAY_SIZE(rt715_snd_controls
),
521 .dapm_widgets
= rt715_dapm_widgets
,
522 .num_dapm_widgets
= ARRAY_SIZE(rt715_dapm_widgets
),
523 .dapm_routes
= rt715_audio_map
,
524 .num_dapm_routes
= ARRAY_SIZE(rt715_audio_map
),
527 static int rt715_set_sdw_stream(struct snd_soc_dai
*dai
, void *sdw_stream
,
531 struct sdw_stream_data
*stream
;
536 stream
= kzalloc(sizeof(*stream
), GFP_KERNEL
);
540 stream
->sdw_stream
= sdw_stream
;
542 /* Use tx_mask or rx_mask to configure stream tag and set dma_data */
543 if (direction
== SNDRV_PCM_STREAM_PLAYBACK
)
544 dai
->playback_dma_data
= stream
;
546 dai
->capture_dma_data
= stream
;
551 static void rt715_shutdown(struct snd_pcm_substream
*substream
,
552 struct snd_soc_dai
*dai
)
555 struct sdw_stream_data
*stream
;
557 stream
= snd_soc_dai_get_dma_data(dai
, substream
);
558 snd_soc_dai_set_dma_data(dai
, substream
, NULL
);
562 static int rt715_pcm_hw_params(struct snd_pcm_substream
*substream
,
563 struct snd_pcm_hw_params
*params
,
564 struct snd_soc_dai
*dai
)
566 struct snd_soc_component
*component
= dai
->component
;
567 struct rt715_priv
*rt715
= snd_soc_component_get_drvdata(component
);
568 struct sdw_stream_config stream_config
;
569 struct sdw_port_config port_config
;
570 enum sdw_data_direction direction
;
571 struct sdw_stream_data
*stream
;
572 int retval
, port
, num_channels
;
573 unsigned int val
= 0;
575 stream
= snd_soc_dai_get_dma_data(dai
, substream
);
585 direction
= SDW_DATA_DIR_TX
;
587 rt715_index_write(rt715
->regmap
, RT715_SDW_INPUT_SEL
, 0xa500);
590 direction
= SDW_DATA_DIR_TX
;
592 rt715_index_write(rt715
->regmap
, RT715_SDW_INPUT_SEL
, 0xa000);
595 dev_err(component
->dev
, "Invalid DAI id %d\n", dai
->id
);
599 stream_config
.frame_rate
= params_rate(params
);
600 stream_config
.ch_count
= params_channels(params
);
601 stream_config
.bps
= snd_pcm_format_width(params_format(params
));
602 stream_config
.direction
= direction
;
604 num_channels
= params_channels(params
);
605 port_config
.ch_mask
= (1 << (num_channels
)) - 1;
606 port_config
.num
= port
;
608 retval
= sdw_stream_add_slave(rt715
->slave
, &stream_config
,
609 &port_config
, 1, stream
->sdw_stream
);
611 dev_err(dai
->dev
, "Unable to configure port\n");
615 switch (params_rate(params
)) {
616 /* bit 14 0:48K 1:44.1K */
617 /* bit 15 Stream Type 0:PCM 1:Non-PCM, should always be PCM */
625 dev_err(component
->dev
, "Unsupported sample rate %d\n",
626 params_rate(params
));
630 if (params_channels(params
) <= 16) {
631 /* bit 3:0 Number of Channel */
632 val
|= (params_channels(params
) - 1);
634 dev_err(component
->dev
, "Unsupported channels %d\n",
635 params_channels(params
));
639 switch (params_width(params
)) {
640 /* bit 6:4 Bits per Sample */
659 regmap_write(rt715
->regmap
, RT715_MIC_ADC_FORMAT_H
, val
);
660 regmap_write(rt715
->regmap
, RT715_MIC_LINE_FORMAT_H
, val
);
661 regmap_write(rt715
->regmap
, RT715_MIX_ADC_FORMAT_H
, val
);
662 regmap_write(rt715
->regmap
, RT715_MIX_ADC2_FORMAT_H
, val
);
667 static int rt715_pcm_hw_free(struct snd_pcm_substream
*substream
,
668 struct snd_soc_dai
*dai
)
670 struct snd_soc_component
*component
= dai
->component
;
671 struct rt715_priv
*rt715
= snd_soc_component_get_drvdata(component
);
672 struct sdw_stream_data
*stream
=
673 snd_soc_dai_get_dma_data(dai
, substream
);
678 sdw_stream_remove_slave(rt715
->slave
, stream
->sdw_stream
);
682 #define RT715_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
683 #define RT715_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
684 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
686 static struct snd_soc_dai_ops rt715_ops
= {
687 .hw_params
= rt715_pcm_hw_params
,
688 .hw_free
= rt715_pcm_hw_free
,
689 .set_sdw_stream
= rt715_set_sdw_stream
,
690 .shutdown
= rt715_shutdown
,
693 static struct snd_soc_dai_driver rt715_dai
[] = {
695 .name
= "rt715-aif1",
698 .stream_name
= "DP6 Capture",
701 .rates
= RT715_STEREO_RATES
,
702 .formats
= RT715_FORMATS
,
707 .name
= "rt715-aif2",
710 .stream_name
= "DP4 Capture",
713 .rates
= RT715_STEREO_RATES
,
714 .formats
= RT715_FORMATS
,
720 /* Bus clock frequency */
721 #define RT715_CLK_FREQ_9600000HZ 9600000
722 #define RT715_CLK_FREQ_12000000HZ 12000000
723 #define RT715_CLK_FREQ_6000000HZ 6000000
724 #define RT715_CLK_FREQ_4800000HZ 4800000
725 #define RT715_CLK_FREQ_2400000HZ 2400000
726 #define RT715_CLK_FREQ_12288000HZ 12288000
728 int rt715_clock_config(struct device
*dev
)
730 struct rt715_priv
*rt715
= dev_get_drvdata(dev
);
731 unsigned int clk_freq
, value
;
733 clk_freq
= (rt715
->params
.curr_dr_freq
>> 1);
736 case RT715_CLK_FREQ_12000000HZ
:
739 case RT715_CLK_FREQ_6000000HZ
:
742 case RT715_CLK_FREQ_9600000HZ
:
745 case RT715_CLK_FREQ_4800000HZ
:
748 case RT715_CLK_FREQ_2400000HZ
:
751 case RT715_CLK_FREQ_12288000HZ
:
758 regmap_write(rt715
->regmap
, 0xe0, value
);
759 regmap_write(rt715
->regmap
, 0xf0, value
);
764 int rt715_init(struct device
*dev
, struct regmap
*sdw_regmap
,
765 struct regmap
*regmap
, struct sdw_slave
*slave
)
767 struct rt715_priv
*rt715
;
770 rt715
= devm_kzalloc(dev
, sizeof(*rt715
), GFP_KERNEL
);
774 dev_set_drvdata(dev
, rt715
);
775 rt715
->slave
= slave
;
776 rt715
->regmap
= regmap
;
777 rt715
->sdw_regmap
= sdw_regmap
;
780 * Mark hw_init to false
781 * HW init will be performed when device reports present
783 rt715
->hw_init
= false;
784 rt715
->first_hw_init
= false;
786 ret
= devm_snd_soc_register_component(dev
,
787 &soc_codec_dev_rt715
,
789 ARRAY_SIZE(rt715_dai
));
794 int rt715_io_init(struct device
*dev
, struct sdw_slave
*slave
)
796 struct rt715_priv
*rt715
= dev_get_drvdata(dev
);
802 * PM runtime is only enabled when a Slave reports as Attached
804 if (!rt715
->first_hw_init
) {
805 /* set autosuspend parameters */
806 pm_runtime_set_autosuspend_delay(&slave
->dev
, 3000);
807 pm_runtime_use_autosuspend(&slave
->dev
);
809 /* update count of parent 'active' children */
810 pm_runtime_set_active(&slave
->dev
);
812 /* make sure the device does not suspend immediately */
813 pm_runtime_mark_last_busy(&slave
->dev
);
815 pm_runtime_enable(&slave
->dev
);
818 pm_runtime_get_noresume(&slave
->dev
);
820 /* Mute nid=08h/09h */
821 regmap_write(rt715
->regmap
, RT715_SET_GAIN_LINE_ADC_H
, 0xb080);
822 regmap_write(rt715
->regmap
, RT715_SET_GAIN_MIX_ADC_H
, 0xb080);
823 /* Mute nid=07h/27h */
824 regmap_write(rt715
->regmap
, RT715_SET_GAIN_MIC_ADC_H
, 0xb080);
825 regmap_write(rt715
->regmap
, RT715_SET_GAIN_MIX_ADC2_H
, 0xb080);
828 regmap_write(rt715
->regmap
, RT715_SET_PIN_DMIC1
, 0x20);
829 regmap_write(rt715
->regmap
, RT715_SET_PIN_DMIC2
, 0x20);
830 regmap_write(rt715
->regmap
, RT715_SET_PIN_DMIC3
, 0x20);
831 regmap_write(rt715
->regmap
, RT715_SET_PIN_DMIC4
, 0x20);
832 /* Set Converter Stream */
833 regmap_write(rt715
->regmap
, RT715_SET_STREAMID_LINE_ADC
, 0x10);
834 regmap_write(rt715
->regmap
, RT715_SET_STREAMID_MIX_ADC
, 0x10);
835 regmap_write(rt715
->regmap
, RT715_SET_STREAMID_MIC_ADC
, 0x10);
836 regmap_write(rt715
->regmap
, RT715_SET_STREAMID_MIX_ADC2
, 0x10);
837 /* Set Configuration Default */
838 regmap_write(rt715
->regmap
, RT715_SET_DMIC1_CONFIG_DEFAULT1
, 0xd0);
839 regmap_write(rt715
->regmap
, RT715_SET_DMIC1_CONFIG_DEFAULT2
, 0x11);
840 regmap_write(rt715
->regmap
, RT715_SET_DMIC1_CONFIG_DEFAULT3
, 0xa1);
841 regmap_write(rt715
->regmap
, RT715_SET_DMIC1_CONFIG_DEFAULT4
, 0x81);
842 regmap_write(rt715
->regmap
, RT715_SET_DMIC2_CONFIG_DEFAULT1
, 0xd1);
843 regmap_write(rt715
->regmap
, RT715_SET_DMIC2_CONFIG_DEFAULT2
, 0x11);
844 regmap_write(rt715
->regmap
, RT715_SET_DMIC2_CONFIG_DEFAULT3
, 0xa1);
845 regmap_write(rt715
->regmap
, RT715_SET_DMIC2_CONFIG_DEFAULT4
, 0x81);
846 regmap_write(rt715
->regmap
, RT715_SET_DMIC3_CONFIG_DEFAULT1
, 0xd0);
847 regmap_write(rt715
->regmap
, RT715_SET_DMIC3_CONFIG_DEFAULT2
, 0x11);
848 regmap_write(rt715
->regmap
, RT715_SET_DMIC3_CONFIG_DEFAULT3
, 0xa1);
849 regmap_write(rt715
->regmap
, RT715_SET_DMIC3_CONFIG_DEFAULT4
, 0x81);
850 regmap_write(rt715
->regmap
, RT715_SET_DMIC4_CONFIG_DEFAULT1
, 0xd1);
851 regmap_write(rt715
->regmap
, RT715_SET_DMIC4_CONFIG_DEFAULT2
, 0x11);
852 regmap_write(rt715
->regmap
, RT715_SET_DMIC4_CONFIG_DEFAULT3
, 0xa1);
853 regmap_write(rt715
->regmap
, RT715_SET_DMIC4_CONFIG_DEFAULT4
, 0x81);
855 /* Finish Initial Settings, set power to D3 */
856 regmap_write(rt715
->regmap
, RT715_SET_AUDIO_POWER_STATE
, AC_PWRST_D3
);
858 if (rt715
->first_hw_init
)
859 regcache_mark_dirty(rt715
->regmap
);
861 rt715
->first_hw_init
= true;
863 /* Mark Slave initialization complete */
864 rt715
->hw_init
= true;
866 pm_runtime_mark_last_busy(&slave
->dev
);
867 pm_runtime_put_autosuspend(&slave
->dev
);
872 MODULE_DESCRIPTION("ASoC rt715 driver");
873 MODULE_DESCRIPTION("ASoC rt715 driver SDW");
874 MODULE_AUTHOR("Jack Yu <jack.yu@realtek.com>");
875 MODULE_LICENSE("GPL v2");