1 // SPDX-License-Identifier: GPL-2.0
3 // rt711.c -- rt711 ALSA SoC audio driver
5 // Copyright(c) 2019 Realtek Semiconductor Corp.
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
14 #include <linux/pm_runtime.h>
16 #include <linux/soundwire/sdw.h>
17 #include <linux/regmap.h>
18 #include <linux/slab.h>
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
23 #include <sound/soc-dapm.h>
24 #include <sound/initval.h>
25 #include <sound/tlv.h>
26 #include <sound/hda_verbs.h>
27 #include <sound/jack.h>
31 static int rt711_index_write(struct regmap
*regmap
,
32 unsigned int nid
, unsigned int reg
, unsigned int value
)
35 unsigned int addr
= ((RT711_PRIV_INDEX_W_H
| nid
) << 8) | reg
;
37 ret
= regmap_write(regmap
, addr
, value
);
39 pr_err("Failed to set private value: %06x <= %04x ret=%d\n",
45 static int rt711_index_read(struct regmap
*regmap
,
46 unsigned int nid
, unsigned int reg
, unsigned int *value
)
49 unsigned int addr
= ((RT711_PRIV_INDEX_W_H
| nid
) << 8) | reg
;
52 ret
= regmap_read(regmap
, addr
, value
);
54 pr_err("Failed to get private value: %06x => %04x ret=%d\n",
60 static int rt711_index_update_bits(struct regmap
*regmap
, unsigned int nid
,
61 unsigned int reg
, unsigned int mask
, unsigned int val
)
63 unsigned int tmp
, orig
;
66 ret
= rt711_index_read(regmap
, nid
, reg
, &orig
);
73 return rt711_index_write(regmap
, nid
, reg
, tmp
);
76 static void rt711_reset(struct regmap
*regmap
)
78 regmap_write(regmap
, RT711_FUNC_RESET
, 0);
79 rt711_index_update_bits(regmap
, RT711_VENDOR_REG
,
80 RT711_PARA_VERB_CTL
, RT711_HIDDEN_REG_SW_RESET
,
81 RT711_HIDDEN_REG_SW_RESET
);
84 static int rt711_calibration(struct rt711_priv
*rt711
)
86 unsigned int val
, loop
= 0;
88 struct regmap
*regmap
= rt711
->regmap
;
91 mutex_lock(&rt711
->calibrate_mutex
);
92 regmap_write(rt711
->regmap
,
93 RT711_SET_AUDIO_POWER_STATE
, AC_PWRST_D0
);
95 dev
= regmap_get_device(regmap
);
97 /* Calibration manual mode */
98 rt711_index_update_bits(regmap
, RT711_VENDOR_REG
, RT711_FSM_CTL
,
102 rt711_index_update_bits(regmap
, RT711_VENDOR_CALI
,
103 RT711_DAC_DC_CALI_CTL1
, RT711_DAC_DC_CALI_TRIGGER
,
104 RT711_DAC_DC_CALI_TRIGGER
);
106 /* wait for calibration process */
107 rt711_index_read(regmap
, RT711_VENDOR_CALI
,
108 RT711_DAC_DC_CALI_CTL1
, &val
);
110 while (val
& RT711_DAC_DC_CALI_TRIGGER
) {
112 pr_err("%s, calibration time-out!\n",
119 usleep_range(10000, 11000);
120 rt711_index_read(regmap
, RT711_VENDOR_CALI
,
121 RT711_DAC_DC_CALI_CTL1
, &val
);
125 rt711_index_update_bits(regmap
, RT711_VENDOR_REG
,
126 RT711_FSM_CTL
, 0xf, RT711_DEPOP_CTL
);
128 regmap_write(rt711
->regmap
,
129 RT711_SET_AUDIO_POWER_STATE
, AC_PWRST_D3
);
130 mutex_unlock(&rt711
->calibrate_mutex
);
132 dev_dbg(dev
, "%s calibration complete, ret=%d\n", __func__
, ret
);
136 static unsigned int rt711_button_detect(struct rt711_priv
*rt711
)
138 unsigned int btn_type
= 0, val80
, val81
;
141 ret
= rt711_index_read(rt711
->regmap
, RT711_VENDOR_REG
,
142 RT711_IRQ_FLAG_TABLE1
, &val80
);
145 ret
= rt711_index_read(rt711
->regmap
, RT711_VENDOR_REG
,
146 RT711_IRQ_FLAG_TABLE2
, &val81
);
157 btn_type
|= SND_JACK_BTN_0
;
160 btn_type
|= SND_JACK_BTN_3
;
167 btn_type
|= SND_JACK_BTN_1
;
172 btn_type
|= SND_JACK_BTN_2
;
176 btn_type
|= SND_JACK_BTN_3
;
183 static int rt711_headset_detect(struct rt711_priv
*rt711
)
185 unsigned int buf
, loop
= 0;
187 unsigned int jack_status
= 0, reg
;
189 ret
= rt711_index_read(rt711
->regmap
, RT711_VENDOR_REG
,
190 RT711_COMBO_JACK_AUTO_CTL2
, &buf
);
195 (buf
& RT711_COMBOJACK_AUTO_DET_STATUS
) == 0) {
198 usleep_range(9000, 10000);
199 ret
= rt711_index_read(rt711
->regmap
, RT711_VENDOR_REG
,
200 RT711_COMBO_JACK_AUTO_CTL2
, &buf
);
204 reg
= RT711_VERB_GET_PIN_SENSE
| RT711_HP_OUT
;
205 ret
= regmap_read(rt711
->regmap
, reg
, &jack_status
);
208 if ((jack_status
& (1 << 31)) == 0)
215 if (buf
& RT711_COMBOJACK_AUTO_DET_TRS
)
216 rt711
->jack_type
= SND_JACK_HEADPHONE
;
217 else if ((buf
& RT711_COMBOJACK_AUTO_DET_CTIA
) ||
218 (buf
& RT711_COMBOJACK_AUTO_DET_OMTP
))
219 rt711
->jack_type
= SND_JACK_HEADSET
;
225 pr_err_ratelimited("Time-out error in %s\n", __func__
);
228 pr_err_ratelimited("IO error in %s, ret %d\n", __func__
, ret
);
231 pr_err_ratelimited("Jack removal in %s\n", __func__
);
235 static void rt711_jack_detect_handler(struct work_struct
*work
)
237 struct rt711_priv
*rt711
=
238 container_of(work
, struct rt711_priv
, jack_detect_work
.work
);
239 int btn_type
= 0, ret
;
240 unsigned int jack_status
= 0, reg
;
245 if (!rt711
->component
->card
->instantiated
)
248 reg
= RT711_VERB_GET_PIN_SENSE
| RT711_HP_OUT
;
249 ret
= regmap_read(rt711
->regmap
, reg
, &jack_status
);
254 if (jack_status
& (1 << 31)) {
256 if (rt711
->jack_type
== 0) {
257 ret
= rt711_headset_detect(rt711
);
260 if (rt711
->jack_type
== SND_JACK_HEADSET
)
261 btn_type
= rt711_button_detect(rt711
);
262 } else if (rt711
->jack_type
== SND_JACK_HEADSET
) {
263 /* jack is already in, report button event */
264 btn_type
= rt711_button_detect(rt711
);
268 rt711
->jack_type
= 0;
271 dev_dbg(&rt711
->slave
->dev
,
272 "in %s, jack_type=0x%x\n", __func__
, rt711
->jack_type
);
273 dev_dbg(&rt711
->slave
->dev
,
274 "in %s, btn_type=0x%x\n", __func__
, btn_type
);
276 snd_soc_jack_report(rt711
->hs_jack
, rt711
->jack_type
| btn_type
,
278 SND_JACK_BTN_0
| SND_JACK_BTN_1
|
279 SND_JACK_BTN_2
| SND_JACK_BTN_3
);
282 /* button released */
283 snd_soc_jack_report(rt711
->hs_jack
, rt711
->jack_type
,
285 SND_JACK_BTN_0
| SND_JACK_BTN_1
|
286 SND_JACK_BTN_2
| SND_JACK_BTN_3
);
288 mod_delayed_work(system_power_efficient_wq
,
289 &rt711
->jack_btn_check_work
, msecs_to_jiffies(200));
295 pr_err_ratelimited("IO error in %s, ret %d\n", __func__
, ret
);
298 static void rt711_btn_check_handler(struct work_struct
*work
)
300 struct rt711_priv
*rt711
= container_of(work
, struct rt711_priv
,
301 jack_btn_check_work
.work
);
302 int btn_type
= 0, ret
;
303 unsigned int jack_status
= 0, reg
;
305 reg
= RT711_VERB_GET_PIN_SENSE
| RT711_HP_OUT
;
306 ret
= regmap_read(rt711
->regmap
, reg
, &jack_status
);
311 if (jack_status
& (1 << 31)) {
312 if (rt711
->jack_type
== SND_JACK_HEADSET
) {
313 /* jack is already in, report button event */
314 btn_type
= rt711_button_detect(rt711
);
317 rt711
->jack_type
= 0;
321 ret
= rt711_index_read(rt711
->regmap
, RT711_VENDOR_REG
,
322 RT711_COMBO_JACK_AUTO_CTL2
, ®
);
326 if ((reg
& 0xf0) == 0xf0)
329 dev_dbg(&rt711
->slave
->dev
,
330 "%s, btn_type=0x%x\n", __func__
, btn_type
);
331 snd_soc_jack_report(rt711
->hs_jack
, rt711
->jack_type
| btn_type
,
333 SND_JACK_BTN_0
| SND_JACK_BTN_1
|
334 SND_JACK_BTN_2
| SND_JACK_BTN_3
);
337 /* button released */
338 snd_soc_jack_report(rt711
->hs_jack
, rt711
->jack_type
,
340 SND_JACK_BTN_0
| SND_JACK_BTN_1
|
341 SND_JACK_BTN_2
| SND_JACK_BTN_3
);
343 mod_delayed_work(system_power_efficient_wq
,
344 &rt711
->jack_btn_check_work
, msecs_to_jiffies(200));
350 pr_err_ratelimited("IO error in %s, ret %d\n", __func__
, ret
);
353 static void rt711_jack_init(struct rt711_priv
*rt711
)
355 struct snd_soc_dapm_context
*dapm
=
356 snd_soc_component_get_dapm(rt711
->component
);
358 mutex_lock(&rt711
->calibrate_mutex
);
360 if (dapm
->bias_level
<= SND_SOC_BIAS_STANDBY
)
361 regmap_write(rt711
->regmap
,
362 RT711_SET_AUDIO_POWER_STATE
, AC_PWRST_D0
);
364 if (rt711
->hs_jack
) {
365 /* unsolicited response & IRQ control */
366 regmap_write(rt711
->regmap
,
367 RT711_SET_MIC2_UNSOLICITED_ENABLE
, 0x82);
368 regmap_write(rt711
->regmap
,
369 RT711_SET_HP_UNSOLICITED_ENABLE
, 0x81);
370 regmap_write(rt711
->regmap
,
371 RT711_SET_INLINE_UNSOLICITED_ENABLE
, 0x83);
372 rt711_index_write(rt711
->regmap
, RT711_VENDOR_REG
,
374 rt711_index_write(rt711
->regmap
, RT711_VENDOR_REG
,
377 switch (rt711
->jd_src
) {
379 /* default settings was already for JD1 */
382 rt711_index_update_bits(rt711
->regmap
, RT711_VENDOR_REG
,
383 RT711_JD_CTL2
, RT711_JD2_2PORT_200K_DECODE_HP
|
385 RT711_JD2_2PORT_200K_DECODE_HP
|
386 RT711_HP_JD_SEL_JD2
);
387 rt711_index_update_bits(rt711
->regmap
, RT711_VENDOR_REG
,
389 RT711_HP_JD_FINAL_RESULT_CTL_JD12
,
390 RT711_HP_JD_FINAL_RESULT_CTL_JD12
);
393 dev_warn(rt711
->component
->dev
, "Wrong JD source\n");
397 dev_dbg(&rt711
->slave
->dev
, "in %s enable\n", __func__
);
399 mod_delayed_work(system_power_efficient_wq
,
400 &rt711
->jack_detect_work
, msecs_to_jiffies(250));
402 regmap_write(rt711
->regmap
,
403 RT711_SET_MIC2_UNSOLICITED_ENABLE
, 0x00);
404 regmap_write(rt711
->regmap
,
405 RT711_SET_HP_UNSOLICITED_ENABLE
, 0x00);
406 regmap_write(rt711
->regmap
,
407 RT711_SET_INLINE_UNSOLICITED_ENABLE
, 0x00);
409 dev_dbg(&rt711
->slave
->dev
, "in %s disable\n", __func__
);
413 if (dapm
->bias_level
<= SND_SOC_BIAS_STANDBY
)
414 regmap_write(rt711
->regmap
,
415 RT711_SET_AUDIO_POWER_STATE
, AC_PWRST_D3
);
416 mutex_unlock(&rt711
->calibrate_mutex
);
419 static int rt711_set_jack_detect(struct snd_soc_component
*component
,
420 struct snd_soc_jack
*hs_jack
, void *data
)
422 struct rt711_priv
*rt711
= snd_soc_component_get_drvdata(component
);
424 rt711
->hs_jack
= hs_jack
;
426 if (!rt711
->hw_init
) {
427 dev_dbg(&rt711
->slave
->dev
,
428 "%s hw_init not ready yet\n", __func__
);
432 rt711_jack_init(rt711
);
437 static void rt711_get_gain(struct rt711_priv
*rt711
, unsigned int addr_h
,
438 unsigned int addr_l
, unsigned int val_h
,
439 unsigned int *r_val
, unsigned int *l_val
)
442 *r_val
= (val_h
<< 8);
443 regmap_read(rt711
->regmap
, addr_l
, r_val
);
447 *l_val
= (val_h
<< 8);
448 regmap_read(rt711
->regmap
, addr_h
, l_val
);
451 /* For Verb-Set Amplifier Gain (Verb ID = 3h) */
452 static int rt711_set_amp_gain_put(struct snd_kcontrol
*kcontrol
,
453 struct snd_ctl_elem_value
*ucontrol
)
455 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
456 struct snd_soc_dapm_context
*dapm
=
457 snd_soc_component_get_dapm(component
);
458 struct soc_mixer_control
*mc
=
459 (struct soc_mixer_control
*)kcontrol
->private_value
;
460 struct rt711_priv
*rt711
= snd_soc_component_get_drvdata(component
);
461 unsigned int addr_h
, addr_l
, val_h
, val_ll
, val_lr
;
462 unsigned int read_ll
, read_rl
;
465 /* Can't use update bit function, so read the original value first */
468 if (mc
->shift
== RT711_DIR_OUT_SFT
) /* output */
473 rt711_get_gain(rt711
, addr_h
, addr_l
, val_h
, &read_rl
, &read_ll
);
477 /* for mute/unmute */
478 val_ll
= (mc
->max
- ucontrol
->value
.integer
.value
[0])
481 read_ll
= read_ll
& 0x7f;
485 val_ll
= ((ucontrol
->value
.integer
.value
[0]) & 0x7f);
486 if (val_ll
> mc
->max
)
488 /* keep mute status */
489 read_ll
= read_ll
& (1 << RT711_MUTE_SFT
);
493 if (dapm
->bias_level
<= SND_SOC_BIAS_STANDBY
)
494 regmap_write(rt711
->regmap
,
495 RT711_SET_AUDIO_POWER_STATE
, AC_PWRST_D0
);
499 /* for mute/unmute */
500 val_lr
= (mc
->max
- ucontrol
->value
.integer
.value
[1])
503 read_rl
= read_rl
& 0x7f;
507 val_lr
= ((ucontrol
->value
.integer
.value
[1]) & 0x7f);
508 if (val_lr
> mc
->max
)
510 /* keep mute status */
511 read_rl
= read_rl
& (1 << RT711_MUTE_SFT
);
515 for (i
= 0; i
< 3; i
++) { /* retry 3 times at most */
517 if (val_ll
== val_lr
) {
518 /* Set both L/R channels at the same time */
519 val_h
= (1 << mc
->shift
) | (3 << 4);
520 regmap_write(rt711
->regmap
,
521 addr_h
, (val_h
<< 8 | val_ll
));
522 regmap_write(rt711
->regmap
,
523 addr_l
, (val_h
<< 8 | val_ll
));
526 val_h
= (1 << mc
->shift
) | (1 << 5);
527 regmap_write(rt711
->regmap
,
528 addr_h
, (val_h
<< 8 | val_ll
));
531 val_h
= (1 << mc
->shift
) | (1 << 4);
532 regmap_write(rt711
->regmap
,
533 addr_l
, (val_h
<< 8 | val_lr
));
536 if (mc
->shift
== RT711_DIR_OUT_SFT
) /* output */
541 rt711_get_gain(rt711
, addr_h
, addr_l
, val_h
,
543 if (read_rl
== val_lr
&& read_ll
== val_ll
)
547 if (dapm
->bias_level
<= SND_SOC_BIAS_STANDBY
)
548 regmap_write(rt711
->regmap
,
549 RT711_SET_AUDIO_POWER_STATE
, AC_PWRST_D3
);
553 static int rt711_set_amp_gain_get(struct snd_kcontrol
*kcontrol
,
554 struct snd_ctl_elem_value
*ucontrol
)
556 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
557 struct rt711_priv
*rt711
= snd_soc_component_get_drvdata(component
);
558 struct soc_mixer_control
*mc
=
559 (struct soc_mixer_control
*)kcontrol
->private_value
;
560 unsigned int addr_h
, addr_l
, val_h
;
561 unsigned int read_ll
, read_rl
;
563 /* switch to get command */
566 if (mc
->shift
== RT711_DIR_OUT_SFT
) /* output */
571 rt711_get_gain(rt711
, addr_h
, addr_l
, val_h
, &read_rl
, &read_ll
);
574 /* mute/unmute for switch controls */
575 read_ll
= !((read_ll
& 0x80) >> RT711_MUTE_SFT
);
576 read_rl
= !((read_rl
& 0x80) >> RT711_MUTE_SFT
);
578 /* for gain volume controls */
579 read_ll
= read_ll
& 0x7f;
580 read_rl
= read_rl
& 0x7f;
582 ucontrol
->value
.integer
.value
[0] = read_ll
;
583 ucontrol
->value
.integer
.value
[1] = read_rl
;
588 static const DECLARE_TLV_DB_SCALE(out_vol_tlv
, -6525, 75, 0);
589 static const DECLARE_TLV_DB_SCALE(in_vol_tlv
, -1725, 75, 0);
590 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv
, 0, 1000, 0);
592 static const struct snd_kcontrol_new rt711_snd_controls
[] = {
593 SOC_DOUBLE_R_EXT_TLV("DAC Surr Playback Volume",
594 RT711_SET_GAIN_DAC2_H
, RT711_SET_GAIN_DAC2_L
,
595 RT711_DIR_OUT_SFT
, 0x57, 0,
596 rt711_set_amp_gain_get
, rt711_set_amp_gain_put
, out_vol_tlv
),
597 SOC_DOUBLE_R_EXT("ADC 08 Capture Switch",
598 RT711_SET_GAIN_ADC2_H
, RT711_SET_GAIN_ADC2_L
,
599 RT711_DIR_IN_SFT
, 1, 1,
600 rt711_set_amp_gain_get
, rt711_set_amp_gain_put
),
601 SOC_DOUBLE_R_EXT("ADC 09 Capture Switch",
602 RT711_SET_GAIN_ADC1_H
, RT711_SET_GAIN_ADC1_L
,
603 RT711_DIR_IN_SFT
, 1, 1,
604 rt711_set_amp_gain_get
, rt711_set_amp_gain_put
),
605 SOC_DOUBLE_R_EXT_TLV("ADC 08 Capture Volume",
606 RT711_SET_GAIN_ADC2_H
, RT711_SET_GAIN_ADC2_L
,
607 RT711_DIR_IN_SFT
, 0x3f, 0,
608 rt711_set_amp_gain_get
, rt711_set_amp_gain_put
, in_vol_tlv
),
609 SOC_DOUBLE_R_EXT_TLV("ADC 09 Capture Volume",
610 RT711_SET_GAIN_ADC1_H
, RT711_SET_GAIN_ADC1_L
,
611 RT711_DIR_IN_SFT
, 0x3f, 0,
612 rt711_set_amp_gain_get
, rt711_set_amp_gain_put
, in_vol_tlv
),
613 SOC_DOUBLE_R_EXT_TLV("AMIC Volume",
614 RT711_SET_GAIN_AMIC_H
, RT711_SET_GAIN_AMIC_L
,
615 RT711_DIR_IN_SFT
, 3, 0,
616 rt711_set_amp_gain_get
, rt711_set_amp_gain_put
, mic_vol_tlv
),
617 SOC_DOUBLE_R_EXT_TLV("DMIC1 Volume",
618 RT711_SET_GAIN_DMIC1_H
, RT711_SET_GAIN_DMIC1_L
,
619 RT711_DIR_IN_SFT
, 3, 0,
620 rt711_set_amp_gain_get
, rt711_set_amp_gain_put
, mic_vol_tlv
),
621 SOC_DOUBLE_R_EXT_TLV("DMIC2 Volume",
622 RT711_SET_GAIN_DMIC2_H
, RT711_SET_GAIN_DMIC2_L
,
623 RT711_DIR_IN_SFT
, 3, 0,
624 rt711_set_amp_gain_get
, rt711_set_amp_gain_put
, mic_vol_tlv
),
627 static int rt711_mux_get(struct snd_kcontrol
*kcontrol
,
628 struct snd_ctl_elem_value
*ucontrol
)
630 struct snd_soc_component
*component
=
631 snd_soc_dapm_kcontrol_component(kcontrol
);
632 struct rt711_priv
*rt711
= snd_soc_component_get_drvdata(component
);
633 unsigned int reg
, val
= 0, nid
;
636 if (strstr(ucontrol
->id
.name
, "ADC 22 Mux"))
637 nid
= RT711_MIXER_IN1
;
638 else if (strstr(ucontrol
->id
.name
, "ADC 23 Mux"))
639 nid
= RT711_MIXER_IN2
;
644 reg
= RT711_VERB_SET_CONNECT_SEL
| nid
;
645 ret
= regmap_read(rt711
->regmap
, reg
, &val
);
647 dev_err(component
->dev
, "%s: sdw read failed: %d\n",
652 ucontrol
->value
.enumerated
.item
[0] = val
;
657 static int rt711_mux_put(struct snd_kcontrol
*kcontrol
,
658 struct snd_ctl_elem_value
*ucontrol
)
660 struct snd_soc_component
*component
=
661 snd_soc_dapm_kcontrol_component(kcontrol
);
662 struct snd_soc_dapm_context
*dapm
=
663 snd_soc_dapm_kcontrol_dapm(kcontrol
);
664 struct rt711_priv
*rt711
= snd_soc_component_get_drvdata(component
);
665 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
666 unsigned int *item
= ucontrol
->value
.enumerated
.item
;
667 unsigned int val
, val2
= 0, change
, reg
, nid
;
670 if (item
[0] >= e
->items
)
673 if (strstr(ucontrol
->id
.name
, "ADC 22 Mux"))
674 nid
= RT711_MIXER_IN1
;
675 else if (strstr(ucontrol
->id
.name
, "ADC 23 Mux"))
676 nid
= RT711_MIXER_IN2
;
680 /* Verb ID = 0x701h */
681 val
= snd_soc_enum_item_to_val(e
, item
[0]) << e
->shift_l
;
683 reg
= RT711_VERB_SET_CONNECT_SEL
| nid
;
684 ret
= regmap_read(rt711
->regmap
, reg
, &val2
);
686 dev_err(component
->dev
, "%s: sdw read failed: %d\n",
697 reg
= RT711_VERB_SET_CONNECT_SEL
| nid
;
698 regmap_write(rt711
->regmap
, reg
, val
);
701 snd_soc_dapm_mux_update_power(dapm
, kcontrol
,
707 static const char * const adc_mux_text
[] = {
714 static SOC_ENUM_SINGLE_DECL(
715 rt711_adc22_enum
, SND_SOC_NOPM
, 0, adc_mux_text
);
717 static SOC_ENUM_SINGLE_DECL(
718 rt711_adc23_enum
, SND_SOC_NOPM
, 0, adc_mux_text
);
720 static const struct snd_kcontrol_new rt711_adc22_mux
=
721 SOC_DAPM_ENUM_EXT("ADC 22 Mux", rt711_adc22_enum
,
722 rt711_mux_get
, rt711_mux_put
);
724 static const struct snd_kcontrol_new rt711_adc23_mux
=
725 SOC_DAPM_ENUM_EXT("ADC 23 Mux", rt711_adc23_enum
,
726 rt711_mux_get
, rt711_mux_put
);
728 static int rt711_dac_surround_event(struct snd_soc_dapm_widget
*w
,
729 struct snd_kcontrol
*kcontrol
, int event
)
731 struct snd_soc_component
*component
=
732 snd_soc_dapm_to_component(w
->dapm
);
733 struct rt711_priv
*rt711
= snd_soc_component_get_drvdata(component
);
734 unsigned int val_h
= (1 << RT711_DIR_OUT_SFT
) | (0x3 << 4);
738 case SND_SOC_DAPM_POST_PMU
:
739 regmap_write(rt711
->regmap
,
740 RT711_SET_STREAMID_DAC2
, 0x10);
743 regmap_write(rt711
->regmap
,
744 RT711_SET_GAIN_HP_H
, (val_h
<< 8 | val_l
));
746 case SND_SOC_DAPM_PRE_PMD
:
747 val_l
= (1 << RT711_MUTE_SFT
);
748 regmap_write(rt711
->regmap
,
749 RT711_SET_GAIN_HP_H
, (val_h
<< 8 | val_l
));
750 usleep_range(50000, 55000);
752 regmap_write(rt711
->regmap
,
753 RT711_SET_STREAMID_DAC2
, 0x00);
759 static int rt711_adc_09_event(struct snd_soc_dapm_widget
*w
,
760 struct snd_kcontrol
*kcontrol
, int event
)
762 struct snd_soc_component
*component
=
763 snd_soc_dapm_to_component(w
->dapm
);
764 struct rt711_priv
*rt711
= snd_soc_component_get_drvdata(component
);
767 case SND_SOC_DAPM_POST_PMU
:
768 regmap_write(rt711
->regmap
,
769 RT711_SET_STREAMID_ADC1
, 0x10);
771 case SND_SOC_DAPM_PRE_PMD
:
772 regmap_write(rt711
->regmap
,
773 RT711_SET_STREAMID_ADC1
, 0x00);
779 static int rt711_adc_08_event(struct snd_soc_dapm_widget
*w
,
780 struct snd_kcontrol
*kcontrol
, int event
)
782 struct snd_soc_component
*component
=
783 snd_soc_dapm_to_component(w
->dapm
);
784 struct rt711_priv
*rt711
= snd_soc_component_get_drvdata(component
);
787 case SND_SOC_DAPM_POST_PMU
:
788 regmap_write(rt711
->regmap
,
789 RT711_SET_STREAMID_ADC2
, 0x10);
791 case SND_SOC_DAPM_PRE_PMD
:
792 regmap_write(rt711
->regmap
,
793 RT711_SET_STREAMID_ADC2
, 0x00);
799 static const struct snd_soc_dapm_widget rt711_dapm_widgets
[] = {
800 SND_SOC_DAPM_OUTPUT("HP"),
801 SND_SOC_DAPM_INPUT("MIC2"),
802 SND_SOC_DAPM_INPUT("DMIC1"),
803 SND_SOC_DAPM_INPUT("DMIC2"),
804 SND_SOC_DAPM_INPUT("LINE1"),
805 SND_SOC_DAPM_INPUT("LINE2"),
807 SND_SOC_DAPM_DAC_E("DAC Surround", NULL
, SND_SOC_NOPM
, 0, 0,
808 rt711_dac_surround_event
,
809 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
810 SND_SOC_DAPM_ADC_E("ADC 09", NULL
, SND_SOC_NOPM
, 0, 0,
812 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
813 SND_SOC_DAPM_ADC_E("ADC 08", NULL
, SND_SOC_NOPM
, 0, 0,
815 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
816 SND_SOC_DAPM_MUX("ADC 22 Mux", SND_SOC_NOPM
, 0, 0,
818 SND_SOC_DAPM_MUX("ADC 23 Mux", SND_SOC_NOPM
, 0, 0,
821 SND_SOC_DAPM_AIF_IN("DP3RX", "DP3 Playback", 0, SND_SOC_NOPM
, 0, 0),
822 SND_SOC_DAPM_AIF_OUT("DP2TX", "DP2 Capture", 0, SND_SOC_NOPM
, 0, 0),
823 SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM
, 0, 0),
826 static const struct snd_soc_dapm_route rt711_audio_map
[] = {
827 {"DAC Surround", NULL
, "DP3RX"},
828 {"DP2TX", NULL
, "ADC 09"},
829 {"DP4TX", NULL
, "ADC 08"},
831 {"ADC 09", NULL
, "ADC 22 Mux"},
832 {"ADC 08", NULL
, "ADC 23 Mux"},
833 {"ADC 22 Mux", "DMIC", "DMIC1"},
834 {"ADC 22 Mux", "LINE1", "LINE1"},
835 {"ADC 22 Mux", "LINE2", "LINE2"},
836 {"ADC 22 Mux", "MIC2", "MIC2"},
837 {"ADC 23 Mux", "DMIC", "DMIC2"},
838 {"ADC 23 Mux", "LINE1", "LINE1"},
839 {"ADC 23 Mux", "LINE2", "LINE2"},
840 {"ADC 23 Mux", "MIC2", "MIC2"},
842 {"HP", NULL
, "DAC Surround"},
845 static int rt711_set_bias_level(struct snd_soc_component
*component
,
846 enum snd_soc_bias_level level
)
848 struct snd_soc_dapm_context
*dapm
=
849 snd_soc_component_get_dapm(component
);
850 struct rt711_priv
*rt711
= snd_soc_component_get_drvdata(component
);
853 case SND_SOC_BIAS_PREPARE
:
854 if (dapm
->bias_level
== SND_SOC_BIAS_STANDBY
) {
855 regmap_write(rt711
->regmap
,
856 RT711_SET_AUDIO_POWER_STATE
,
861 case SND_SOC_BIAS_STANDBY
:
862 regmap_write(rt711
->regmap
,
863 RT711_SET_AUDIO_POWER_STATE
,
874 static int rt711_parse_dt(struct rt711_priv
*rt711
, struct device
*dev
)
876 device_property_read_u32(dev
, "realtek,jd-src",
882 static int rt711_probe(struct snd_soc_component
*component
)
884 struct rt711_priv
*rt711
= snd_soc_component_get_drvdata(component
);
886 rt711_parse_dt(rt711
, &rt711
->slave
->dev
);
887 rt711
->component
= component
;
892 static const struct snd_soc_component_driver soc_codec_dev_rt711
= {
893 .probe
= rt711_probe
,
894 .set_bias_level
= rt711_set_bias_level
,
895 .controls
= rt711_snd_controls
,
896 .num_controls
= ARRAY_SIZE(rt711_snd_controls
),
897 .dapm_widgets
= rt711_dapm_widgets
,
898 .num_dapm_widgets
= ARRAY_SIZE(rt711_dapm_widgets
),
899 .dapm_routes
= rt711_audio_map
,
900 .num_dapm_routes
= ARRAY_SIZE(rt711_audio_map
),
901 .set_jack
= rt711_set_jack_detect
,
904 static int rt711_set_sdw_stream(struct snd_soc_dai
*dai
, void *sdw_stream
,
907 struct sdw_stream_data
*stream
;
912 stream
= kzalloc(sizeof(*stream
), GFP_KERNEL
);
916 stream
->sdw_stream
= sdw_stream
;
918 /* Use tx_mask or rx_mask to configure stream tag and set dma_data */
919 if (direction
== SNDRV_PCM_STREAM_PLAYBACK
)
920 dai
->playback_dma_data
= stream
;
922 dai
->capture_dma_data
= stream
;
927 static void rt711_shutdown(struct snd_pcm_substream
*substream
,
928 struct snd_soc_dai
*dai
)
930 struct sdw_stream_data
*stream
;
932 stream
= snd_soc_dai_get_dma_data(dai
, substream
);
933 snd_soc_dai_set_dma_data(dai
, substream
, NULL
);
937 static int rt711_pcm_hw_params(struct snd_pcm_substream
*substream
,
938 struct snd_pcm_hw_params
*params
,
939 struct snd_soc_dai
*dai
)
941 struct snd_soc_component
*component
= dai
->component
;
942 struct rt711_priv
*rt711
= snd_soc_component_get_drvdata(component
);
943 struct sdw_stream_config stream_config
;
944 struct sdw_port_config port_config
;
945 enum sdw_data_direction direction
;
946 struct sdw_stream_data
*stream
;
947 int retval
, port
, num_channels
;
948 unsigned int val
= 0;
950 dev_dbg(dai
->dev
, "%s %s", __func__
, dai
->name
);
951 stream
= snd_soc_dai_get_dma_data(dai
, substream
);
959 /* SoundWire specific configuration */
960 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
961 direction
= SDW_DATA_DIR_RX
;
964 direction
= SDW_DATA_DIR_TX
;
965 if (dai
->id
== RT711_AIF1
)
967 else if (dai
->id
== RT711_AIF2
)
973 stream_config
.frame_rate
= params_rate(params
);
974 stream_config
.ch_count
= params_channels(params
);
975 stream_config
.bps
= snd_pcm_format_width(params_format(params
));
976 stream_config
.direction
= direction
;
978 num_channels
= params_channels(params
);
979 port_config
.ch_mask
= (1 << (num_channels
)) - 1;
980 port_config
.num
= port
;
982 retval
= sdw_stream_add_slave(rt711
->slave
, &stream_config
,
983 &port_config
, 1, stream
->sdw_stream
);
985 dev_err(dai
->dev
, "Unable to configure port\n");
989 if (params_channels(params
) <= 16) {
990 /* bit 3:0 Number of Channel */
991 val
|= (params_channels(params
) - 1);
993 dev_err(component
->dev
, "Unsupported channels %d\n",
994 params_channels(params
));
998 switch (params_width(params
)) {
999 /* bit 6:4 Bits per Sample */
1019 regmap_write(rt711
->regmap
, RT711_DAC_FORMAT_H
, val
);
1020 regmap_write(rt711
->regmap
, RT711_ADC1_FORMAT_H
, val
);
1021 regmap_write(rt711
->regmap
, RT711_ADC2_FORMAT_H
, val
);
1026 static int rt711_pcm_hw_free(struct snd_pcm_substream
*substream
,
1027 struct snd_soc_dai
*dai
)
1029 struct snd_soc_component
*component
= dai
->component
;
1030 struct rt711_priv
*rt711
= snd_soc_component_get_drvdata(component
);
1031 struct sdw_stream_data
*stream
=
1032 snd_soc_dai_get_dma_data(dai
, substream
);
1037 sdw_stream_remove_slave(rt711
->slave
, stream
->sdw_stream
);
1041 #define RT711_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
1042 #define RT711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1043 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
1045 static struct snd_soc_dai_ops rt711_ops
= {
1046 .hw_params
= rt711_pcm_hw_params
,
1047 .hw_free
= rt711_pcm_hw_free
,
1048 .set_sdw_stream
= rt711_set_sdw_stream
,
1049 .shutdown
= rt711_shutdown
,
1052 static struct snd_soc_dai_driver rt711_dai
[] = {
1054 .name
= "rt711-aif1",
1057 .stream_name
= "DP3 Playback",
1060 .rates
= RT711_STEREO_RATES
,
1061 .formats
= RT711_FORMATS
,
1064 .stream_name
= "DP4 Capture",
1067 .rates
= RT711_STEREO_RATES
,
1068 .formats
= RT711_FORMATS
,
1073 .name
= "rt711-aif2",
1076 .stream_name
= "DP2 Capture",
1079 .rates
= RT711_STEREO_RATES
,
1080 .formats
= RT711_FORMATS
,
1086 /* Bus clock frequency */
1087 #define RT711_CLK_FREQ_9600000HZ 9600000
1088 #define RT711_CLK_FREQ_12000000HZ 12000000
1089 #define RT711_CLK_FREQ_6000000HZ 6000000
1090 #define RT711_CLK_FREQ_4800000HZ 4800000
1091 #define RT711_CLK_FREQ_2400000HZ 2400000
1092 #define RT711_CLK_FREQ_12288000HZ 12288000
1094 int rt711_clock_config(struct device
*dev
)
1096 struct rt711_priv
*rt711
= dev_get_drvdata(dev
);
1097 unsigned int clk_freq
, value
;
1099 clk_freq
= (rt711
->params
.curr_dr_freq
>> 1);
1102 case RT711_CLK_FREQ_12000000HZ
:
1105 case RT711_CLK_FREQ_6000000HZ
:
1108 case RT711_CLK_FREQ_9600000HZ
:
1111 case RT711_CLK_FREQ_4800000HZ
:
1114 case RT711_CLK_FREQ_2400000HZ
:
1117 case RT711_CLK_FREQ_12288000HZ
:
1124 regmap_write(rt711
->regmap
, 0xe0, value
);
1125 regmap_write(rt711
->regmap
, 0xf0, value
);
1127 dev_dbg(dev
, "%s complete, clk_freq=%d\n", __func__
, clk_freq
);
1132 static void rt711_calibration_work(struct work_struct
*work
)
1134 struct rt711_priv
*rt711
=
1135 container_of(work
, struct rt711_priv
, calibration_work
);
1137 rt711_calibration(rt711
);
1140 int rt711_init(struct device
*dev
, struct regmap
*sdw_regmap
,
1141 struct regmap
*regmap
, struct sdw_slave
*slave
)
1143 struct rt711_priv
*rt711
;
1146 rt711
= devm_kzalloc(dev
, sizeof(*rt711
), GFP_KERNEL
);
1150 dev_set_drvdata(dev
, rt711
);
1151 rt711
->slave
= slave
;
1152 rt711
->sdw_regmap
= sdw_regmap
;
1153 rt711
->regmap
= regmap
;
1156 * Mark hw_init to false
1157 * HW init will be performed when device reports present
1159 rt711
->hw_init
= false;
1160 rt711
->first_hw_init
= false;
1162 /* JD source uses JD2 in default */
1163 rt711
->jd_src
= RT711_JD2
;
1165 ret
= devm_snd_soc_register_component(dev
,
1166 &soc_codec_dev_rt711
,
1168 ARRAY_SIZE(rt711_dai
));
1170 dev_dbg(&slave
->dev
, "%s\n", __func__
);
1175 int rt711_io_init(struct device
*dev
, struct sdw_slave
*slave
)
1177 struct rt711_priv
*rt711
= dev_get_drvdata(dev
);
1182 if (rt711
->first_hw_init
) {
1183 regcache_cache_only(rt711
->regmap
, false);
1184 regcache_cache_bypass(rt711
->regmap
, true);
1188 * PM runtime is only enabled when a Slave reports as Attached
1190 if (!rt711
->first_hw_init
) {
1191 /* set autosuspend parameters */
1192 pm_runtime_set_autosuspend_delay(&slave
->dev
, 3000);
1193 pm_runtime_use_autosuspend(&slave
->dev
);
1195 /* update count of parent 'active' children */
1196 pm_runtime_set_active(&slave
->dev
);
1198 /* make sure the device does not suspend immediately */
1199 pm_runtime_mark_last_busy(&slave
->dev
);
1201 pm_runtime_enable(&slave
->dev
);
1204 pm_runtime_get_noresume(&slave
->dev
);
1206 rt711_reset(rt711
->regmap
);
1209 regmap_write(rt711
->regmap
, RT711_SET_AUDIO_POWER_STATE
, AC_PWRST_D0
);
1211 /* Set Pin Widget */
1212 regmap_write(rt711
->regmap
, RT711_SET_PIN_MIC2
, 0x25);
1213 regmap_write(rt711
->regmap
, RT711_SET_PIN_HP
, 0xc0);
1214 regmap_write(rt711
->regmap
, RT711_SET_PIN_DMIC1
, 0x20);
1215 regmap_write(rt711
->regmap
, RT711_SET_PIN_DMIC2
, 0x20);
1216 regmap_write(rt711
->regmap
, RT711_SET_PIN_LINE1
, 0x20);
1217 regmap_write(rt711
->regmap
, RT711_SET_PIN_LINE2
, 0x20);
1219 /* Mute HP/ADC1/ADC2 */
1220 regmap_write(rt711
->regmap
, RT711_SET_GAIN_HP_H
, 0xa080);
1221 regmap_write(rt711
->regmap
, RT711_SET_GAIN_HP_H
, 0x9080);
1222 regmap_write(rt711
->regmap
, RT711_SET_GAIN_ADC2_H
, 0x6080);
1223 regmap_write(rt711
->regmap
, RT711_SET_GAIN_ADC2_H
, 0x5080);
1224 regmap_write(rt711
->regmap
, RT711_SET_GAIN_ADC1_H
, 0x6080);
1225 regmap_write(rt711
->regmap
, RT711_SET_GAIN_ADC1_H
, 0x5080);
1227 /* Set Configuration Default */
1228 regmap_write(rt711
->regmap
, 0x4f12, 0x91);
1229 regmap_write(rt711
->regmap
, 0x4e12, 0xd6);
1230 regmap_write(rt711
->regmap
, 0x4d12, 0x11);
1231 regmap_write(rt711
->regmap
, 0x4c12, 0x20);
1232 regmap_write(rt711
->regmap
, 0x4f13, 0x91);
1233 regmap_write(rt711
->regmap
, 0x4e13, 0xd6);
1234 regmap_write(rt711
->regmap
, 0x4d13, 0x11);
1235 regmap_write(rt711
->regmap
, 0x4c13, 0x21);
1236 regmap_write(rt711
->regmap
, 0x4c21, 0xf0);
1237 regmap_write(rt711
->regmap
, 0x4d21, 0x11);
1238 regmap_write(rt711
->regmap
, 0x4e21, 0x11);
1239 regmap_write(rt711
->regmap
, 0x4f21, 0x01);
1241 /* Data port arrangement */
1242 rt711_index_write(rt711
->regmap
, RT711_VENDOR_REG
,
1243 RT711_TX_RX_MUX_CTL
, 0x0154);
1246 rt711_index_write(rt711
->regmap
, RT711_VENDOR_REG
,
1247 RT711_DIGITAL_MISC_CTRL4
, 0x201b);
1248 rt711_index_write(rt711
->regmap
, RT711_VENDOR_REG
,
1249 RT711_COMBO_JACK_AUTO_CTL1
, 0x5089);
1250 rt711_index_write(rt711
->regmap
, RT711_VENDOR_REG
,
1251 RT711_VREFOUT_CTL
, 0x5064);
1252 rt711_index_write(rt711
->regmap
, RT711_VENDOR_REG
,
1253 RT711_INLINE_CMD_CTL
, 0xd249);
1255 /* Finish Initial Settings, set power to D3 */
1256 regmap_write(rt711
->regmap
, RT711_SET_AUDIO_POWER_STATE
, AC_PWRST_D3
);
1258 if (rt711
->first_hw_init
)
1259 rt711_calibration(rt711
);
1261 INIT_DELAYED_WORK(&rt711
->jack_detect_work
,
1262 rt711_jack_detect_handler
);
1263 INIT_DELAYED_WORK(&rt711
->jack_btn_check_work
,
1264 rt711_btn_check_handler
);
1265 mutex_init(&rt711
->calibrate_mutex
);
1266 INIT_WORK(&rt711
->calibration_work
, rt711_calibration_work
);
1267 schedule_work(&rt711
->calibration_work
);
1271 * if set_jack callback occurred early than io_init,
1272 * we set up the jack detection function now
1275 rt711_jack_init(rt711
);
1277 if (rt711
->first_hw_init
) {
1278 regcache_cache_bypass(rt711
->regmap
, false);
1279 regcache_mark_dirty(rt711
->regmap
);
1281 rt711
->first_hw_init
= true;
1283 /* Mark Slave initialization complete */
1284 rt711
->hw_init
= true;
1286 pm_runtime_mark_last_busy(&slave
->dev
);
1287 pm_runtime_put_autosuspend(&slave
->dev
);
1289 dev_dbg(&slave
->dev
, "%s hw_init complete\n", __func__
);
1293 MODULE_DESCRIPTION("ASoC RT711 SDW driver");
1294 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
1295 MODULE_LICENSE("GPL");