1 // SPDX-License-Identifier: GPL-2.0-only
3 // rt711-sdca.c -- rt711 SDCA ALSA SoC audio driver
5 // Copyright(c) 2021 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>
15 #include <linux/soundwire/sdw_registers.h>
16 #include <linux/slab.h>
17 #include <linux/bitops.h>
18 #include <sound/core.h>
19 #include <sound/pcm.h>
20 #include <sound/pcm_params.h>
21 #include <sound/sdw.h>
22 #include <sound/soc-dapm.h>
23 #include <sound/initval.h>
24 #include <sound/tlv.h>
25 #include <sound/jack.h>
27 #include "rt711-sdca.h"
29 static int rt711_sdca_index_write(struct rt711_sdca_priv
*rt711
,
30 unsigned int nid
, unsigned int reg
, unsigned int value
)
33 struct regmap
*regmap
= rt711
->mbq_regmap
;
34 unsigned int addr
= (nid
<< 20) | reg
;
36 ret
= regmap_write(regmap
, addr
, value
);
38 dev_err(&rt711
->slave
->dev
,
39 "%s: Failed to set private value: %06x <= %04x ret=%d\n",
40 __func__
, addr
, value
, ret
);
45 static int rt711_sdca_index_read(struct rt711_sdca_priv
*rt711
,
46 unsigned int nid
, unsigned int reg
, unsigned int *value
)
49 struct regmap
*regmap
= rt711
->mbq_regmap
;
50 unsigned int addr
= (nid
<< 20) | reg
;
52 ret
= regmap_read(regmap
, addr
, value
);
54 dev_err(&rt711
->slave
->dev
,
55 "%s: Failed to get private value: %06x => %04x ret=%d\n",
56 __func__
, addr
, *value
, ret
);
61 static int rt711_sdca_index_update_bits(struct rt711_sdca_priv
*rt711
,
62 unsigned int nid
, unsigned int reg
, unsigned int mask
, unsigned int val
)
67 ret
= rt711_sdca_index_read(rt711
, nid
, reg
, &tmp
);
71 set_mask_bits(&tmp
, mask
, val
);
72 return rt711_sdca_index_write(rt711
, nid
, reg
, tmp
);
75 static void rt711_sdca_reset(struct rt711_sdca_priv
*rt711
)
77 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_REG
,
78 RT711_PARA_VERB_CTL
, RT711_HIDDEN_REG_SW_RESET
,
79 RT711_HIDDEN_REG_SW_RESET
);
80 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_HDA_CTL
,
81 RT711_HDA_LEGACY_RESET_CTL
, 0x1, 0x1);
84 static void rt711_sdca_ge_force_jack_type(struct rt711_sdca_priv
*rt711
, unsigned int det_mode
)
88 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_REG
, RT711_COMBO_JACK_AUTO_CTL1
, 0x8400, 0x0000);
89 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_HDA_CTL
, RT711_PUSH_BTN_INT_CTL0
, 0x10, 0x00);
92 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_REG
, RT711_COMBO_JACK_AUTO_CTL1
, 0x8400, 0x8000);
93 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_HDA_CTL
, RT711_PUSH_BTN_INT_CTL0
, 0x17, 0x13);
96 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_REG
, RT711_COMBO_JACK_AUTO_CTL1
, 0x8400, 0x8400);
97 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_HDA_CTL
, RT711_PUSH_BTN_INT_CTL0
, 0x17, 0x15);
102 static int rt711_sdca_calibration(struct rt711_sdca_priv
*rt711
)
104 unsigned int val
, loop_rc
= 0, loop_dc
= 0;
106 struct regmap
*regmap
= rt711
->regmap
;
110 mutex_lock(&rt711
->calibrate_mutex
);
111 dev
= regmap_get_device(regmap
);
113 regmap_read(rt711
->regmap
, RT711_RC_CAL_STATUS
, &val
);
116 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_ANALOG_CTL
,
117 RT711_MISC_POWER_CTL0
, 0x0010, 0x0010);
119 for (loop_rc
= 0; loop_rc
< chk_cnt
&& !(val
& 0x40); loop_rc
++) {
120 usleep_range(10000, 11000);
121 ret
= regmap_read(rt711
->regmap
, RT711_RC_CAL_STATUS
, &val
);
125 if (loop_rc
== chk_cnt
)
126 dev_err(dev
, "%s, RC calibration time-out!\n", __func__
);
128 /* HP calibration by manual mode setting */
129 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_REG
,
130 RT711_FSM_CTL
, 0x2000, 0x2000);
132 /* Calibration manual mode */
133 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_REG
,
134 RT711_FSM_CTL
, 0xf, RT711_CALI_CTL
);
136 /* reset HP calibration */
137 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_CALI
,
138 RT711_DAC_DC_CALI_CTL1
, RT711_DAC_DC_FORCE_CALI_RST
, 0x00);
139 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_CALI
,
140 RT711_DAC_DC_CALI_CTL1
, RT711_DAC_DC_FORCE_CALI_RST
,
141 RT711_DAC_DC_FORCE_CALI_RST
);
144 if (rt711
->hw_ver
== RT711_VER_VD0
)
145 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_CALI
,
146 RT711_DAC_DC_CALI_CTL1
, RT711_DAC_DC_CALI_CLK_EN
,
147 RT711_DAC_DC_CALI_CLK_EN
);
150 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_CALI
,
151 RT711_DAC_DC_CALI_CTL1
, RT711_DAC_DC_CALI_TRIGGER
,
152 RT711_DAC_DC_CALI_TRIGGER
);
154 /* wait for calibration process */
155 rt711_sdca_index_read(rt711
, RT711_VENDOR_CALI
,
156 RT711_DAC_DC_CALI_CTL1
, &val
);
158 for (loop_dc
= 0; loop_dc
< chk_cnt
&&
159 (val
& RT711_DAC_DC_CALI_TRIGGER
); loop_dc
++) {
160 usleep_range(10000, 11000);
161 ret
= rt711_sdca_index_read(rt711
, RT711_VENDOR_CALI
,
162 RT711_DAC_DC_CALI_CTL1
, &val
);
166 if (loop_dc
== chk_cnt
)
167 dev_err(dev
, "%s, calibration time-out!\n", __func__
);
169 if (loop_dc
== chk_cnt
|| loop_rc
== chk_cnt
)
173 /* enable impedance sense */
174 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_REG
,
175 RT711_FSM_CTL
, RT711_FSM_IMP_EN
, RT711_FSM_IMP_EN
);
177 /* release HP-JD and trigger FSM */
178 rt711_sdca_index_write(rt711
, RT711_VENDOR_REG
,
179 RT711_DIGITAL_MISC_CTRL4
, 0x201b);
181 mutex_unlock(&rt711
->calibrate_mutex
);
182 dev_dbg(dev
, "%s calibration complete, ret=%d\n", __func__
, ret
);
186 static unsigned int rt711_sdca_button_detect(struct rt711_sdca_priv
*rt711
)
188 unsigned int btn_type
= 0, offset
, idx
, val
, owner
;
190 unsigned char buf
[3];
192 /* get current UMP message owner */
193 ret
= regmap_read(rt711
->regmap
,
194 SDW_SDCA_CTL(FUNC_NUM_HID
, RT711_SDCA_ENT_HID01
, RT711_SDCA_CTL_HIDTX_CURRENT_OWNER
, 0),
199 /* if owner is device then there is no button event from device */
203 /* read UMP message offset */
204 ret
= regmap_read(rt711
->regmap
,
205 SDW_SDCA_CTL(FUNC_NUM_HID
, RT711_SDCA_ENT_HID01
, RT711_SDCA_CTL_HIDTX_MESSAGE_OFFSET
, 0),
210 for (idx
= 0; idx
< sizeof(buf
); idx
++) {
211 ret
= regmap_read(rt711
->regmap
,
212 RT711_BUF_ADDR_HID1
+ offset
+ idx
, &val
);
215 buf
[idx
] = val
& 0xff;
218 if (buf
[0] == 0x11) {
219 switch (buf
[1] & 0xf0) {
221 btn_type
|= SND_JACK_BTN_2
;
224 btn_type
|= SND_JACK_BTN_3
;
227 btn_type
|= SND_JACK_BTN_0
;
230 btn_type
|= SND_JACK_BTN_1
;
236 btn_type
|= SND_JACK_BTN_2
;
240 btn_type
|= SND_JACK_BTN_3
;
244 btn_type
|= SND_JACK_BTN_0
;
248 btn_type
|= SND_JACK_BTN_1
;
254 /* Host is owner, so set back to device */
256 /* set owner to device */
257 regmap_write(rt711
->regmap
,
258 SDW_SDCA_CTL(FUNC_NUM_HID
, RT711_SDCA_ENT_HID01
,
259 RT711_SDCA_CTL_HIDTX_SET_OWNER_TO_DEVICE
, 0), 0x01);
264 static int rt711_sdca_headset_detect(struct rt711_sdca_priv
*rt711
)
266 unsigned int det_mode
;
269 rt711_sdca_ge_force_jack_type(rt711
, rt711
->ge_mode_override
);
271 /* get detected_mode */
272 ret
= regmap_read(rt711
->regmap
,
273 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_GE49
, RT711_SDCA_CTL_DETECTED_MODE
, 0),
280 rt711
->jack_type
= 0;
283 rt711
->jack_type
= SND_JACK_HEADPHONE
;
286 rt711
->jack_type
= SND_JACK_HEADSET
;
290 /* write selected_mode */
292 ret
= regmap_write(rt711
->regmap
,
293 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_GE49
, RT711_SDCA_CTL_SELECTED_MODE
, 0),
299 dev_dbg(&rt711
->slave
->dev
,
300 "%s, detected_mode=0x%x\n", __func__
, det_mode
);
305 pr_err_ratelimited("IO error in %s, ret %d\n", __func__
, ret
);
309 static void rt711_sdca_jack_detect_handler(struct work_struct
*work
)
311 struct rt711_sdca_priv
*rt711
=
312 container_of(work
, struct rt711_sdca_priv
, jack_detect_work
.work
);
313 int btn_type
= 0, ret
;
318 if (!snd_soc_card_is_instantiated(rt711
->component
->card
))
321 /* SDW_SCP_SDCA_INT_SDCA_0 is used for jack detection */
322 if (rt711
->scp_sdca_stat1
& SDW_SCP_SDCA_INT_SDCA_0
) {
323 ret
= rt711_sdca_headset_detect(rt711
);
328 /* SDW_SCP_SDCA_INT_SDCA_8 is used for button detection */
329 if (rt711
->scp_sdca_stat2
& SDW_SCP_SDCA_INT_SDCA_8
)
330 btn_type
= rt711_sdca_button_detect(rt711
);
332 if (rt711
->jack_type
== 0)
335 dev_dbg(&rt711
->slave
->dev
,
336 "in %s, jack_type=0x%x\n", __func__
, rt711
->jack_type
);
337 dev_dbg(&rt711
->slave
->dev
,
338 "in %s, btn_type=0x%x\n", __func__
, btn_type
);
339 dev_dbg(&rt711
->slave
->dev
,
340 "in %s, scp_sdca_stat1=0x%x, scp_sdca_stat2=0x%x\n", __func__
,
341 rt711
->scp_sdca_stat1
, rt711
->scp_sdca_stat2
);
343 snd_soc_jack_report(rt711
->hs_jack
, rt711
->jack_type
| btn_type
,
345 SND_JACK_BTN_0
| SND_JACK_BTN_1
|
346 SND_JACK_BTN_2
| SND_JACK_BTN_3
);
349 /* button released */
350 snd_soc_jack_report(rt711
->hs_jack
, rt711
->jack_type
,
352 SND_JACK_BTN_0
| SND_JACK_BTN_1
|
353 SND_JACK_BTN_2
| SND_JACK_BTN_3
);
355 mod_delayed_work(system_power_efficient_wq
,
356 &rt711
->jack_btn_check_work
, msecs_to_jiffies(200));
360 static void rt711_sdca_btn_check_handler(struct work_struct
*work
)
362 struct rt711_sdca_priv
*rt711
=
363 container_of(work
, struct rt711_sdca_priv
, jack_btn_check_work
.work
);
364 int btn_type
= 0, ret
, idx
;
365 unsigned int det_mode
, offset
, val
;
366 unsigned char buf
[3];
368 ret
= regmap_read(rt711
->regmap
,
369 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_GE49
, RT711_SDCA_CTL_DETECTED_MODE
, 0),
376 /* read UMP message offset */
377 ret
= regmap_read(rt711
->regmap
,
378 SDW_SDCA_CTL(FUNC_NUM_HID
, RT711_SDCA_ENT_HID01
, RT711_SDCA_CTL_HIDTX_MESSAGE_OFFSET
, 0),
383 for (idx
= 0; idx
< sizeof(buf
); idx
++) {
384 ret
= regmap_read(rt711
->regmap
,
385 RT711_BUF_ADDR_HID1
+ offset
+ idx
, &val
);
388 buf
[idx
] = val
& 0xff;
391 if (buf
[0] == 0x11) {
392 switch (buf
[1] & 0xf0) {
394 btn_type
|= SND_JACK_BTN_2
;
397 btn_type
|= SND_JACK_BTN_3
;
400 btn_type
|= SND_JACK_BTN_0
;
403 btn_type
|= SND_JACK_BTN_1
;
409 btn_type
|= SND_JACK_BTN_2
;
413 btn_type
|= SND_JACK_BTN_3
;
417 btn_type
|= SND_JACK_BTN_0
;
421 btn_type
|= SND_JACK_BTN_1
;
426 rt711
->jack_type
= 0;
428 dev_dbg(&rt711
->slave
->dev
, "%s, btn_type=0x%x\n", __func__
, btn_type
);
429 snd_soc_jack_report(rt711
->hs_jack
, rt711
->jack_type
| btn_type
,
431 SND_JACK_BTN_0
| SND_JACK_BTN_1
|
432 SND_JACK_BTN_2
| SND_JACK_BTN_3
);
435 /* button released */
436 snd_soc_jack_report(rt711
->hs_jack
, rt711
->jack_type
,
438 SND_JACK_BTN_0
| SND_JACK_BTN_1
|
439 SND_JACK_BTN_2
| SND_JACK_BTN_3
);
441 mod_delayed_work(system_power_efficient_wq
,
442 &rt711
->jack_btn_check_work
, msecs_to_jiffies(200));
448 pr_err_ratelimited("IO error in %s, ret %d\n", __func__
, ret
);
451 static void rt711_sdca_jack_init(struct rt711_sdca_priv
*rt711
)
453 mutex_lock(&rt711
->calibrate_mutex
);
455 if (rt711
->hs_jack
) {
456 /* Enable HID1 event & set button RTC mode */
457 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_HDA_CTL
,
458 RT711_PUSH_BTN_INT_CTL6
, 0x80f0, 0x8000);
459 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_HDA_CTL
,
460 RT711_PUSH_BTN_INT_CTL2
, 0x11dd, 0x11dd);
461 rt711_sdca_index_write(rt711
, RT711_VENDOR_HDA_CTL
,
462 RT711_PUSH_BTN_INT_CTL7
, 0xffff);
463 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_HDA_CTL
,
464 RT711_PUSH_BTN_INT_CTL9
, 0xf000, 0x0000);
466 /* GE_mode_change_event_en & Hid1_push_button_event_en */
467 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_HDA_CTL
,
468 RT711_GE_MODE_RELATED_CTL
, 0x0c00, 0x0c00);
470 switch (rt711
->jd_src
) {
472 /* default settings was already for JD1 */
475 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_REG
,
476 RT711_JD_CTL1
, RT711_JD2_DIGITAL_MODE_SEL
,
477 RT711_JD2_DIGITAL_MODE_SEL
);
478 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_REG
,
479 RT711_JD_CTL2
, RT711_JD2_2PORT_200K_DECODE_HP
| RT711_HP_JD_SEL_JD2
,
480 RT711_JD2_2PORT_200K_DECODE_HP
| RT711_HP_JD_SEL_JD2
);
481 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_REG
,
483 RT711_HP_JD_FINAL_RESULT_CTL_JD12
,
484 RT711_HP_JD_FINAL_RESULT_CTL_JD12
);
487 rt711_sdca_index_write(rt711
, RT711_VENDOR_REG
,
488 RT711_COMBO_JACK_AUTO_CTL3
, 0xa47e);
489 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_REG
,
490 RT711_JD_CTL1
, RT711_JD2_DIGITAL_MODE_SEL
,
491 RT711_JD2_DIGITAL_MODE_SEL
);
492 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_REG
,
493 RT711_JD_CTL2
, RT711_JD2_2PORT_200K_DECODE_HP
|
494 RT711_JD2_2PORT_100K_DECODE_MASK
| RT711_HP_JD_SEL_JD2
,
495 RT711_JD2_2PORT_100K_DECODE_HP
| RT711_HP_JD_SEL_JD2
);
496 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_REG
,
498 RT711_HP_JD_FINAL_RESULT_CTL_JD12
| RT711_POW_CC1_AGPI
,
499 RT711_HP_JD_FINAL_RESULT_CTL_JD12
| RT711_POW_CC1_AGPI_OFF
);
502 dev_warn(rt711
->component
->dev
, "Wrong JD source\n");
506 /* set SCP_SDCA_IntMask1[0]=1 */
507 sdw_write_no_pm(rt711
->slave
, SDW_SCP_SDCA_INTMASK1
, SDW_SCP_SDCA_INTMASK_SDCA_0
);
508 /* set SCP_SDCA_IntMask2[0]=1 */
509 sdw_write_no_pm(rt711
->slave
, SDW_SCP_SDCA_INTMASK2
, SDW_SCP_SDCA_INTMASK_SDCA_8
);
510 dev_dbg(&rt711
->slave
->dev
, "in %s enable\n", __func__
);
512 /* disable HID 1/2 event */
513 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_HDA_CTL
,
514 RT711_GE_MODE_RELATED_CTL
, 0x0c00, 0x0000);
516 dev_dbg(&rt711
->slave
->dev
, "in %s disable\n", __func__
);
519 mutex_unlock(&rt711
->calibrate_mutex
);
522 static int rt711_sdca_set_jack_detect(struct snd_soc_component
*component
,
523 struct snd_soc_jack
*hs_jack
, void *data
)
525 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
528 rt711
->hs_jack
= hs_jack
;
530 /* we can only resume if the device was initialized at least once */
531 if (!rt711
->first_hw_init
)
534 ret
= pm_runtime_resume_and_get(component
->dev
);
536 if (ret
!= -EACCES
) {
537 dev_err(component
->dev
, "%s: failed to resume %d\n", __func__
, ret
);
541 /* pm_runtime not enabled yet */
542 dev_dbg(component
->dev
, "%s: skipping jack init for now\n", __func__
);
546 rt711_sdca_jack_init(rt711
);
548 pm_runtime_mark_last_busy(component
->dev
);
549 pm_runtime_put_autosuspend(component
->dev
);
554 /* For SDCA control DAC/ADC Gain */
555 static int rt711_sdca_set_gain_put(struct snd_kcontrol
*kcontrol
,
556 struct snd_ctl_elem_value
*ucontrol
)
558 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
559 struct soc_mixer_control
*mc
=
560 (struct soc_mixer_control
*)kcontrol
->private_value
;
561 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
562 unsigned int read_l
, read_r
, gain_l_val
, gain_r_val
;
563 unsigned int i
, adc_vol_flag
= 0, changed
= 0;
564 unsigned int lvalue
, rvalue
;
566 if (strstr(ucontrol
->id
.name
, "FU1E Capture Volume") ||
567 strstr(ucontrol
->id
.name
, "FU0F Capture Volume"))
570 regmap_read(rt711
->mbq_regmap
, mc
->reg
, &lvalue
);
571 regmap_read(rt711
->mbq_regmap
, mc
->rreg
, &rvalue
);
573 /* control value to 2's complement value */
575 gain_l_val
= ucontrol
->value
.integer
.value
[0];
576 if (gain_l_val
> mc
->max
)
577 gain_l_val
= mc
->max
;
580 if (mc
->shift
== 8) /* boost gain */
581 gain_l_val
= (gain_l_val
* 10) << mc
->shift
;
582 else { /* ADC/DAC gain */
583 if (adc_vol_flag
&& gain_l_val
> mc
->shift
)
584 gain_l_val
= (gain_l_val
- mc
->shift
) * 75;
586 gain_l_val
= (mc
->shift
- gain_l_val
) * 75;
589 if (!(adc_vol_flag
&& read_l
> mc
->shift
)) {
590 gain_l_val
= ~gain_l_val
;
593 gain_l_val
&= 0xffff;
597 gain_r_val
= ucontrol
->value
.integer
.value
[1];
598 if (gain_r_val
> mc
->max
)
599 gain_r_val
= mc
->max
;
602 if (mc
->shift
== 8) /* boost gain */
603 gain_r_val
= (gain_r_val
* 10) << mc
->shift
;
604 else { /* ADC/DAC gain */
605 if (adc_vol_flag
&& gain_r_val
> mc
->shift
)
606 gain_r_val
= (gain_r_val
- mc
->shift
) * 75;
608 gain_r_val
= (mc
->shift
- gain_r_val
) * 75;
611 if (!(adc_vol_flag
&& read_r
> mc
->shift
)) {
612 gain_r_val
= ~gain_r_val
;
615 gain_r_val
&= 0xffff;
618 if (lvalue
!= gain_l_val
|| rvalue
!= gain_r_val
)
623 for (i
= 0; i
< 3; i
++) { /* retry 3 times at most */
625 regmap_write(rt711
->mbq_regmap
, mc
->reg
, gain_l_val
);
628 regmap_write(rt711
->mbq_regmap
, mc
->rreg
, gain_r_val
);
630 regmap_read(rt711
->mbq_regmap
, mc
->reg
, &read_l
);
631 regmap_read(rt711
->mbq_regmap
, mc
->rreg
, &read_r
);
632 if (read_r
== gain_r_val
&& read_l
== gain_l_val
)
636 return i
== 3 ? -EIO
: changed
;
639 static int rt711_sdca_set_gain_get(struct snd_kcontrol
*kcontrol
,
640 struct snd_ctl_elem_value
*ucontrol
)
642 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
643 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
644 struct soc_mixer_control
*mc
=
645 (struct soc_mixer_control
*)kcontrol
->private_value
;
646 unsigned int read_l
, read_r
, ctl_l
= 0, ctl_r
= 0;
647 unsigned int adc_vol_flag
= 0, neg_flag
= 0;
649 if (strstr(ucontrol
->id
.name
, "FU1E Capture Volume") ||
650 strstr(ucontrol
->id
.name
, "FU0F Capture Volume"))
653 regmap_read(rt711
->mbq_regmap
, mc
->reg
, &read_l
);
654 regmap_read(rt711
->mbq_regmap
, mc
->rreg
, &read_r
);
656 /* 2's complement value to control value */
657 if (mc
->shift
== 8) /* boost gain */
658 ctl_l
= (read_l
>> mc
->shift
) / 10;
659 else { /* ADC/DAC gain */
661 if (read_l
& BIT(15)) {
662 ctl_l
= 0xffff & ~(read_l
- 1);
669 ctl_l
= mc
->shift
- (ctl_l
/ 75);
671 ctl_l
= mc
->shift
+ (ctl_l
/ 75);
673 ctl_l
= mc
->max
- (ctl_l
/ 75);
677 if (read_l
!= read_r
) {
678 if (mc
->shift
== 8) /* boost gain */
679 ctl_r
= (read_r
>> mc
->shift
) / 10;
680 else { /* ADC/DAC gain */
682 if (read_r
& BIT(15)) {
683 ctl_r
= 0xffff & ~(read_r
- 1);
690 ctl_r
= mc
->shift
- (ctl_r
/ 75);
692 ctl_r
= mc
->shift
+ (ctl_r
/ 75);
694 ctl_r
= mc
->max
- (ctl_r
/ 75);
699 ucontrol
->value
.integer
.value
[0] = ctl_l
;
700 ucontrol
->value
.integer
.value
[1] = ctl_r
;
705 static int rt711_sdca_set_fu0f_capture_ctl(struct rt711_sdca_priv
*rt711
)
708 unsigned int ch_l
, ch_r
;
710 ch_l
= (rt711
->fu0f_dapm_mute
|| rt711
->fu0f_mixer_l_mute
) ? 0x01 : 0x00;
711 ch_r
= (rt711
->fu0f_dapm_mute
|| rt711
->fu0f_mixer_r_mute
) ? 0x01 : 0x00;
713 err
= regmap_write(rt711
->regmap
,
714 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_USER_FU0F
,
715 RT711_SDCA_CTL_FU_MUTE
, CH_L
), ch_l
);
719 err
= regmap_write(rt711
->regmap
,
720 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_USER_FU0F
,
721 RT711_SDCA_CTL_FU_MUTE
, CH_R
), ch_r
);
728 static int rt711_sdca_set_fu1e_capture_ctl(struct rt711_sdca_priv
*rt711
)
731 unsigned int ch_l
, ch_r
;
733 ch_l
= (rt711
->fu1e_dapm_mute
|| rt711
->fu1e_mixer_l_mute
) ? 0x01 : 0x00;
734 ch_r
= (rt711
->fu1e_dapm_mute
|| rt711
->fu1e_mixer_r_mute
) ? 0x01 : 0x00;
736 err
= regmap_write(rt711
->regmap
,
737 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY
, RT711_SDCA_ENT_USER_FU1E
,
738 RT711_SDCA_CTL_FU_MUTE
, CH_L
), ch_l
);
742 err
= regmap_write(rt711
->regmap
,
743 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY
, RT711_SDCA_ENT_USER_FU1E
,
744 RT711_SDCA_CTL_FU_MUTE
, CH_R
), ch_r
);
751 static int rt711_sdca_fu1e_capture_get(struct snd_kcontrol
*kcontrol
,
752 struct snd_ctl_elem_value
*ucontrol
)
754 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
755 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
757 ucontrol
->value
.integer
.value
[0] = !rt711
->fu1e_mixer_l_mute
;
758 ucontrol
->value
.integer
.value
[1] = !rt711
->fu1e_mixer_r_mute
;
762 static int rt711_sdca_fu1e_capture_put(struct snd_kcontrol
*kcontrol
,
763 struct snd_ctl_elem_value
*ucontrol
)
765 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
766 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
767 int err
, changed
= 0;
769 if (rt711
->fu1e_mixer_l_mute
!= !ucontrol
->value
.integer
.value
[0] ||
770 rt711
->fu1e_mixer_r_mute
!= !ucontrol
->value
.integer
.value
[1])
773 rt711
->fu1e_mixer_l_mute
= !ucontrol
->value
.integer
.value
[0];
774 rt711
->fu1e_mixer_r_mute
= !ucontrol
->value
.integer
.value
[1];
775 err
= rt711_sdca_set_fu1e_capture_ctl(rt711
);
782 static int rt711_sdca_fu0f_capture_get(struct snd_kcontrol
*kcontrol
,
783 struct snd_ctl_elem_value
*ucontrol
)
785 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
786 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
788 ucontrol
->value
.integer
.value
[0] = !rt711
->fu0f_mixer_l_mute
;
789 ucontrol
->value
.integer
.value
[1] = !rt711
->fu0f_mixer_r_mute
;
793 static int rt711_sdca_fu0f_capture_put(struct snd_kcontrol
*kcontrol
,
794 struct snd_ctl_elem_value
*ucontrol
)
796 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
797 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
798 int err
, changed
= 0;
800 if (rt711
->fu0f_mixer_l_mute
!= !ucontrol
->value
.integer
.value
[0] ||
801 rt711
->fu0f_mixer_r_mute
!= !ucontrol
->value
.integer
.value
[1])
804 rt711
->fu0f_mixer_l_mute
= !ucontrol
->value
.integer
.value
[0];
805 rt711
->fu0f_mixer_r_mute
= !ucontrol
->value
.integer
.value
[1];
806 err
= rt711_sdca_set_fu0f_capture_ctl(rt711
);
813 static int rt711_sdca_ge_select_get(struct snd_kcontrol
*kcontrol
,
814 struct snd_ctl_elem_value
*ucontrol
)
816 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
817 struct snd_soc_component
*component
= snd_soc_kcontrol_component(kcontrol
);
818 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
819 unsigned int val
, item
;
821 val
= (rt711
->ge_mode_override
>> e
->shift_l
) & e
->mask
;
822 item
= snd_soc_enum_val_to_item(e
, val
);
823 ucontrol
->value
.enumerated
.item
[0] = item
;
827 static int rt711_sdca_ge_select_put(struct snd_kcontrol
*kcontrol
,
828 struct snd_ctl_elem_value
*ucontrol
)
830 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
831 unsigned int *item
= ucontrol
->value
.enumerated
.item
;
832 struct snd_soc_component
*component
= snd_soc_kcontrol_component(kcontrol
);
833 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
834 unsigned int val
, change
= 0;
836 if (item
[0] >= e
->items
)
839 val
= snd_soc_enum_item_to_val(e
, item
[0]) << e
->shift_l
;
840 if (rt711
->ge_mode_override
!= val
) {
841 rt711
->ge_mode_override
= val
;
848 static const char * const rt711_sdca_ge_select
[] = {
854 static int rt711_sdca_ge_select_values
[] = {
860 static SOC_VALUE_ENUM_SINGLE_DECL(rt711_sdca_ge_mode_enum
, SND_SOC_NOPM
,
861 0, 0x7, rt711_sdca_ge_select
, rt711_sdca_ge_select_values
);
863 static const DECLARE_TLV_DB_SCALE(out_vol_tlv
, -6525, 75, 0);
864 static const DECLARE_TLV_DB_SCALE(in_vol_tlv
, -1725, 75, 0);
865 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv
, 0, 1000, 0);
867 static const struct snd_kcontrol_new rt711_sdca_snd_controls
[] = {
868 SOC_DOUBLE_R_EXT_TLV("FU05 Playback Volume",
869 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_USER_FU05
, RT711_SDCA_CTL_FU_VOLUME
, CH_L
),
870 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_USER_FU05
, RT711_SDCA_CTL_FU_VOLUME
, CH_R
),
872 rt711_sdca_set_gain_get
, rt711_sdca_set_gain_put
, out_vol_tlv
),
873 SOC_DOUBLE_EXT("FU1E Capture Switch", SND_SOC_NOPM
, 0, 1, 1, 0,
874 rt711_sdca_fu1e_capture_get
, rt711_sdca_fu1e_capture_put
),
875 SOC_DOUBLE_EXT("FU0F Capture Switch", SND_SOC_NOPM
, 0, 1, 1, 0,
876 rt711_sdca_fu0f_capture_get
, rt711_sdca_fu0f_capture_put
),
877 SOC_DOUBLE_R_EXT_TLV("FU1E Capture Volume",
878 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY
, RT711_SDCA_ENT_USER_FU1E
, RT711_SDCA_CTL_FU_VOLUME
, CH_L
),
879 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY
, RT711_SDCA_ENT_USER_FU1E
, RT711_SDCA_CTL_FU_VOLUME
, CH_R
),
881 rt711_sdca_set_gain_get
, rt711_sdca_set_gain_put
, in_vol_tlv
),
882 SOC_DOUBLE_R_EXT_TLV("FU0F Capture Volume",
883 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_USER_FU0F
, RT711_SDCA_CTL_FU_VOLUME
, CH_L
),
884 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_USER_FU0F
, RT711_SDCA_CTL_FU_VOLUME
, CH_R
),
886 rt711_sdca_set_gain_get
, rt711_sdca_set_gain_put
, in_vol_tlv
),
887 SOC_DOUBLE_R_EXT_TLV("FU44 Gain Volume",
888 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_PLATFORM_FU44
, RT711_SDCA_CTL_FU_CH_GAIN
, CH_L
),
889 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_PLATFORM_FU44
, RT711_SDCA_CTL_FU_CH_GAIN
, CH_R
),
891 rt711_sdca_set_gain_get
, rt711_sdca_set_gain_put
, mic_vol_tlv
),
892 SOC_DOUBLE_R_EXT_TLV("FU15 Gain Volume",
893 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY
, RT711_SDCA_ENT_PLATFORM_FU15
, RT711_SDCA_CTL_FU_CH_GAIN
, CH_L
),
894 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY
, RT711_SDCA_ENT_PLATFORM_FU15
, RT711_SDCA_CTL_FU_CH_GAIN
, CH_R
),
896 rt711_sdca_set_gain_get
, rt711_sdca_set_gain_put
, mic_vol_tlv
),
897 SOC_ENUM_EXT("GE49 Selected Mode", rt711_sdca_ge_mode_enum
,
898 rt711_sdca_ge_select_get
, rt711_sdca_ge_select_put
),
901 static int rt711_sdca_mux_get(struct snd_kcontrol
*kcontrol
,
902 struct snd_ctl_elem_value
*ucontrol
)
904 struct snd_soc_component
*component
=
905 snd_soc_dapm_kcontrol_component(kcontrol
);
906 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
907 unsigned int val
= 0, mask_sft
;
909 if (strstr(ucontrol
->id
.name
, "ADC 22 Mux"))
911 else if (strstr(ucontrol
->id
.name
, "ADC 23 Mux"))
916 rt711_sdca_index_read(rt711
, RT711_VENDOR_HDA_CTL
,
917 RT711_HDA_LEGACY_MUX_CTL1
, &val
);
919 ucontrol
->value
.enumerated
.item
[0] = (val
>> mask_sft
) & 0x7;
924 static int rt711_sdca_mux_put(struct snd_kcontrol
*kcontrol
,
925 struct snd_ctl_elem_value
*ucontrol
)
927 struct snd_soc_component
*component
=
928 snd_soc_dapm_kcontrol_component(kcontrol
);
929 struct snd_soc_dapm_context
*dapm
=
930 snd_soc_dapm_kcontrol_dapm(kcontrol
);
931 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
932 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
933 unsigned int *item
= ucontrol
->value
.enumerated
.item
;
934 unsigned int val
, val2
= 0, change
, mask_sft
;
936 if (item
[0] >= e
->items
)
939 if (strstr(ucontrol
->id
.name
, "ADC 22 Mux"))
941 else if (strstr(ucontrol
->id
.name
, "ADC 23 Mux"))
946 val
= snd_soc_enum_item_to_val(e
, item
[0]) << e
->shift_l
;
948 rt711_sdca_index_read(rt711
, RT711_VENDOR_HDA_CTL
,
949 RT711_HDA_LEGACY_MUX_CTL1
, &val2
);
950 val2
= (val2
>> mask_sft
) & 0x7;
958 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_HDA_CTL
,
959 RT711_HDA_LEGACY_MUX_CTL1
, 0x7 << mask_sft
,
962 snd_soc_dapm_mux_update_power(dapm
, kcontrol
,
968 static const char * const adc_mux_text
[] = {
975 static SOC_ENUM_SINGLE_DECL(
976 rt711_adc22_enum
, SND_SOC_NOPM
, 0, adc_mux_text
);
978 static SOC_ENUM_SINGLE_DECL(
979 rt711_adc23_enum
, SND_SOC_NOPM
, 0, adc_mux_text
);
981 static const struct snd_kcontrol_new rt711_sdca_adc22_mux
=
982 SOC_DAPM_ENUM_EXT("ADC 22 Mux", rt711_adc22_enum
,
983 rt711_sdca_mux_get
, rt711_sdca_mux_put
);
985 static const struct snd_kcontrol_new rt711_sdca_adc23_mux
=
986 SOC_DAPM_ENUM_EXT("ADC 23 Mux", rt711_adc23_enum
,
987 rt711_sdca_mux_get
, rt711_sdca_mux_put
);
989 static int rt711_sdca_fu05_event(struct snd_soc_dapm_widget
*w
,
990 struct snd_kcontrol
*kcontrol
, int event
)
992 struct snd_soc_component
*component
=
993 snd_soc_dapm_to_component(w
->dapm
);
994 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
995 unsigned char unmute
= 0x0, mute
= 0x1;
998 case SND_SOC_DAPM_POST_PMU
:
999 regmap_write(rt711
->regmap
,
1000 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_USER_FU05
,
1001 RT711_SDCA_CTL_FU_MUTE
, CH_L
),
1003 regmap_write(rt711
->regmap
,
1004 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_USER_FU05
,
1005 RT711_SDCA_CTL_FU_MUTE
, CH_R
),
1008 case SND_SOC_DAPM_PRE_PMD
:
1009 regmap_write(rt711
->regmap
,
1010 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_USER_FU05
,
1011 RT711_SDCA_CTL_FU_MUTE
, CH_L
),
1013 regmap_write(rt711
->regmap
,
1014 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_USER_FU05
,
1015 RT711_SDCA_CTL_FU_MUTE
, CH_R
),
1022 static int rt711_sdca_fu0f_event(struct snd_soc_dapm_widget
*w
,
1023 struct snd_kcontrol
*kcontrol
, int event
)
1025 struct snd_soc_component
*component
=
1026 snd_soc_dapm_to_component(w
->dapm
);
1027 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
1030 case SND_SOC_DAPM_POST_PMU
:
1031 rt711
->fu0f_dapm_mute
= false;
1032 rt711_sdca_set_fu0f_capture_ctl(rt711
);
1034 case SND_SOC_DAPM_PRE_PMD
:
1035 rt711
->fu0f_dapm_mute
= true;
1036 rt711_sdca_set_fu0f_capture_ctl(rt711
);
1042 static int rt711_sdca_fu1e_event(struct snd_soc_dapm_widget
*w
,
1043 struct snd_kcontrol
*kcontrol
, int event
)
1045 struct snd_soc_component
*component
=
1046 snd_soc_dapm_to_component(w
->dapm
);
1047 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
1050 case SND_SOC_DAPM_POST_PMU
:
1051 rt711
->fu1e_dapm_mute
= false;
1052 rt711_sdca_set_fu1e_capture_ctl(rt711
);
1054 case SND_SOC_DAPM_PRE_PMD
:
1055 rt711
->fu1e_dapm_mute
= true;
1056 rt711_sdca_set_fu1e_capture_ctl(rt711
);
1062 static int rt711_sdca_pde28_event(struct snd_soc_dapm_widget
*w
,
1063 struct snd_kcontrol
*kcontrol
, int event
)
1065 struct snd_soc_component
*component
=
1066 snd_soc_dapm_to_component(w
->dapm
);
1067 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
1068 unsigned char ps0
= 0x0, ps3
= 0x3;
1071 case SND_SOC_DAPM_POST_PMU
:
1072 regmap_write(rt711
->regmap
,
1073 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_PDE28
,
1074 RT711_SDCA_CTL_REQ_POWER_STATE
, 0),
1077 case SND_SOC_DAPM_PRE_PMD
:
1078 regmap_write(rt711
->regmap
,
1079 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_PDE28
,
1080 RT711_SDCA_CTL_REQ_POWER_STATE
, 0),
1087 static int rt711_sdca_pde29_event(struct snd_soc_dapm_widget
*w
,
1088 struct snd_kcontrol
*kcontrol
, int event
)
1090 struct snd_soc_component
*component
=
1091 snd_soc_dapm_to_component(w
->dapm
);
1092 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
1093 unsigned char ps0
= 0x0, ps3
= 0x3;
1096 case SND_SOC_DAPM_POST_PMU
:
1097 regmap_write(rt711
->regmap
,
1098 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_PDE29
,
1099 RT711_SDCA_CTL_REQ_POWER_STATE
, 0),
1102 case SND_SOC_DAPM_PRE_PMD
:
1103 regmap_write(rt711
->regmap
,
1104 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_PDE29
,
1105 RT711_SDCA_CTL_REQ_POWER_STATE
, 0),
1112 static int rt711_sdca_pde2a_event(struct snd_soc_dapm_widget
*w
,
1113 struct snd_kcontrol
*kcontrol
, int event
)
1115 struct snd_soc_component
*component
=
1116 snd_soc_dapm_to_component(w
->dapm
);
1117 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
1118 unsigned char ps0
= 0x0, ps3
= 0x3;
1121 case SND_SOC_DAPM_POST_PMU
:
1122 regmap_write(rt711
->regmap
,
1123 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY
, RT711_SDCA_ENT_PDE2A
,
1124 RT711_SDCA_CTL_REQ_POWER_STATE
, 0),
1127 case SND_SOC_DAPM_PRE_PMD
:
1128 regmap_write(rt711
->regmap
,
1129 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY
, RT711_SDCA_ENT_PDE2A
,
1130 RT711_SDCA_CTL_REQ_POWER_STATE
, 0),
1137 static int rt711_sdca_line1_power_event(struct snd_soc_dapm_widget
*w
,
1138 struct snd_kcontrol
*kcontrol
, int event
)
1140 struct snd_soc_component
*component
=
1141 snd_soc_dapm_to_component(w
->dapm
);
1142 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
1143 static unsigned int sel_mode
= 0xffff;
1146 case SND_SOC_DAPM_POST_PMU
:
1147 regmap_read(rt711
->regmap
,
1148 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_GE49
,
1149 RT711_SDCA_CTL_SELECTED_MODE
, 0),
1151 regmap_write(rt711
->regmap
,
1152 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_LINE1
,
1153 RT711_SDCA_CTL_VENDOR_DEF
, 0),
1155 regmap_write(rt711
->regmap
,
1156 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_GE49
,
1157 RT711_SDCA_CTL_SELECTED_MODE
, 0),
1160 case SND_SOC_DAPM_PRE_PMD
:
1161 regmap_write(rt711
->regmap
,
1162 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_LINE1
,
1163 RT711_SDCA_CTL_VENDOR_DEF
, 0),
1165 if (sel_mode
!= 0xffff)
1166 regmap_write(rt711
->regmap
,
1167 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_GE49
,
1168 RT711_SDCA_CTL_SELECTED_MODE
, 0),
1176 static int rt711_sdca_line2_power_event(struct snd_soc_dapm_widget
*w
,
1177 struct snd_kcontrol
*kcontrol
, int event
)
1179 struct snd_soc_component
*component
=
1180 snd_soc_dapm_to_component(w
->dapm
);
1181 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
1182 unsigned char ps0
= 0x0, ps3
= 0x3;
1185 case SND_SOC_DAPM_POST_PMU
:
1186 regmap_write(rt711
->regmap
,
1187 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_PDELINE2
,
1188 RT711_SDCA_CTL_REQ_POWER_STATE
, 0),
1190 regmap_write(rt711
->regmap
,
1191 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_LINE2
,
1192 RT711_SDCA_CTL_VENDOR_DEF
, 0),
1195 case SND_SOC_DAPM_PRE_PMD
:
1196 regmap_write(rt711
->regmap
,
1197 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_LINE2
,
1198 RT711_SDCA_CTL_VENDOR_DEF
, 0),
1200 regmap_write(rt711
->regmap
,
1201 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_PDELINE2
,
1202 RT711_SDCA_CTL_REQ_POWER_STATE
, 0),
1210 static const struct snd_soc_dapm_widget rt711_sdca_dapm_widgets
[] = {
1211 SND_SOC_DAPM_OUTPUT("HP"),
1212 SND_SOC_DAPM_INPUT("MIC2"),
1213 SND_SOC_DAPM_INPUT("DMIC1"),
1214 SND_SOC_DAPM_INPUT("DMIC2"),
1215 SND_SOC_DAPM_INPUT("LINE1"),
1216 SND_SOC_DAPM_INPUT("LINE2"),
1218 SND_SOC_DAPM_PGA_E("LINE1 Power", SND_SOC_NOPM
,
1219 0, 0, NULL
, 0, rt711_sdca_line1_power_event
,
1220 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
1221 SND_SOC_DAPM_PGA_E("LINE2 Power", SND_SOC_NOPM
,
1222 0, 0, NULL
, 0, rt711_sdca_line2_power_event
,
1223 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
1225 SND_SOC_DAPM_SUPPLY("PDE 28", SND_SOC_NOPM
, 0, 0,
1226 rt711_sdca_pde28_event
,
1227 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
1228 SND_SOC_DAPM_SUPPLY("PDE 29", SND_SOC_NOPM
, 0, 0,
1229 rt711_sdca_pde29_event
,
1230 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
1231 SND_SOC_DAPM_SUPPLY("PDE 2A", SND_SOC_NOPM
, 0, 0,
1232 rt711_sdca_pde2a_event
,
1233 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
1235 SND_SOC_DAPM_DAC_E("FU 05", NULL
, SND_SOC_NOPM
, 0, 0,
1236 rt711_sdca_fu05_event
,
1237 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
1238 SND_SOC_DAPM_ADC_E("FU 0F", NULL
, SND_SOC_NOPM
, 0, 0,
1239 rt711_sdca_fu0f_event
,
1240 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
1241 SND_SOC_DAPM_ADC_E("FU 1E", NULL
, SND_SOC_NOPM
, 0, 0,
1242 rt711_sdca_fu1e_event
,
1243 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
1244 SND_SOC_DAPM_MUX("ADC 22 Mux", SND_SOC_NOPM
, 0, 0,
1245 &rt711_sdca_adc22_mux
),
1246 SND_SOC_DAPM_MUX("ADC 23 Mux", SND_SOC_NOPM
, 0, 0,
1247 &rt711_sdca_adc23_mux
),
1249 SND_SOC_DAPM_AIF_IN("DP3RX", "DP3 Playback", 0, SND_SOC_NOPM
, 0, 0),
1250 SND_SOC_DAPM_AIF_OUT("DP2TX", "DP2 Capture", 0, SND_SOC_NOPM
, 0, 0),
1251 SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM
, 0, 0),
1254 static const struct snd_soc_dapm_route rt711_sdca_audio_map
[] = {
1255 {"FU 05", NULL
, "DP3RX"},
1256 {"DP2TX", NULL
, "FU 0F"},
1257 {"DP4TX", NULL
, "FU 1E"},
1259 {"LINE1 Power", NULL
, "LINE1"},
1260 {"LINE2 Power", NULL
, "LINE2"},
1261 {"HP", NULL
, "PDE 28"},
1262 {"FU 0F", NULL
, "PDE 29"},
1263 {"FU 1E", NULL
, "PDE 2A"},
1265 {"FU 0F", NULL
, "ADC 22 Mux"},
1266 {"FU 1E", NULL
, "ADC 23 Mux"},
1267 {"ADC 22 Mux", "DMIC", "DMIC1"},
1268 {"ADC 22 Mux", "LINE1", "LINE1 Power"},
1269 {"ADC 22 Mux", "LINE2", "LINE2 Power"},
1270 {"ADC 22 Mux", "MIC2", "MIC2"},
1271 {"ADC 23 Mux", "DMIC", "DMIC2"},
1272 {"ADC 23 Mux", "LINE1", "LINE1 Power"},
1273 {"ADC 23 Mux", "LINE2", "LINE2 Power"},
1274 {"ADC 23 Mux", "MIC2", "MIC2"},
1276 {"HP", NULL
, "FU 05"},
1279 static int rt711_sdca_parse_dt(struct rt711_sdca_priv
*rt711
, struct device
*dev
)
1281 device_property_read_u32(dev
, "realtek,jd-src", &rt711
->jd_src
);
1286 static int rt711_sdca_probe(struct snd_soc_component
*component
)
1288 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
1291 rt711_sdca_parse_dt(rt711
, &rt711
->slave
->dev
);
1292 rt711
->component
= component
;
1294 if (!rt711
->first_hw_init
)
1297 ret
= pm_runtime_resume(component
->dev
);
1298 if (ret
< 0 && ret
!= -EACCES
)
1304 static const struct snd_soc_component_driver soc_sdca_dev_rt711
= {
1305 .probe
= rt711_sdca_probe
,
1306 .controls
= rt711_sdca_snd_controls
,
1307 .num_controls
= ARRAY_SIZE(rt711_sdca_snd_controls
),
1308 .dapm_widgets
= rt711_sdca_dapm_widgets
,
1309 .num_dapm_widgets
= ARRAY_SIZE(rt711_sdca_dapm_widgets
),
1310 .dapm_routes
= rt711_sdca_audio_map
,
1311 .num_dapm_routes
= ARRAY_SIZE(rt711_sdca_audio_map
),
1312 .set_jack
= rt711_sdca_set_jack_detect
,
1316 static int rt711_sdca_set_sdw_stream(struct snd_soc_dai
*dai
, void *sdw_stream
,
1319 snd_soc_dai_dma_data_set(dai
, direction
, sdw_stream
);
1324 static void rt711_sdca_shutdown(struct snd_pcm_substream
*substream
,
1325 struct snd_soc_dai
*dai
)
1327 snd_soc_dai_set_dma_data(dai
, substream
, NULL
);
1330 static int rt711_sdca_pcm_hw_params(struct snd_pcm_substream
*substream
,
1331 struct snd_pcm_hw_params
*params
,
1332 struct snd_soc_dai
*dai
)
1334 struct snd_soc_component
*component
= dai
->component
;
1335 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
1336 struct sdw_stream_config stream_config
= {0};
1337 struct sdw_port_config port_config
= {0};
1338 struct sdw_stream_runtime
*sdw_stream
;
1340 unsigned int sampling_rate
;
1342 dev_dbg(dai
->dev
, "%s %s", __func__
, dai
->name
);
1343 sdw_stream
= snd_soc_dai_get_dma_data(dai
, substream
);
1351 /* SoundWire specific configuration */
1352 snd_sdw_params_to_config(substream
, params
, &stream_config
, &port_config
);
1354 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1355 port_config
.num
= 3;
1357 if (dai
->id
== RT711_AIF1
)
1358 port_config
.num
= 2;
1359 else if (dai
->id
== RT711_AIF2
)
1360 port_config
.num
= 4;
1365 retval
= sdw_stream_add_slave(rt711
->slave
, &stream_config
,
1366 &port_config
, 1, sdw_stream
);
1368 dev_err(dai
->dev
, "%s: Unable to configure port\n", __func__
);
1372 if (params_channels(params
) > 16) {
1373 dev_err(component
->dev
, "%s: Unsupported channels %d\n",
1374 __func__
, params_channels(params
));
1378 /* sampling rate configuration */
1379 switch (params_rate(params
)) {
1381 sampling_rate
= RT711_SDCA_RATE_44100HZ
;
1384 sampling_rate
= RT711_SDCA_RATE_48000HZ
;
1387 sampling_rate
= RT711_SDCA_RATE_96000HZ
;
1390 sampling_rate
= RT711_SDCA_RATE_192000HZ
;
1393 dev_err(component
->dev
, "%s: Rate %d is not supported\n",
1394 __func__
, params_rate(params
));
1398 /* set sampling frequency */
1399 regmap_write(rt711
->regmap
,
1400 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_CS01
, RT711_SDCA_CTL_SAMPLE_FREQ_INDEX
, 0),
1402 regmap_write(rt711
->regmap
,
1403 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_CS11
, RT711_SDCA_CTL_SAMPLE_FREQ_INDEX
, 0),
1405 regmap_write(rt711
->regmap
,
1406 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY
, RT711_SDCA_ENT_CS1F
, RT711_SDCA_CTL_SAMPLE_FREQ_INDEX
, 0),
1412 static int rt711_sdca_pcm_hw_free(struct snd_pcm_substream
*substream
,
1413 struct snd_soc_dai
*dai
)
1415 struct snd_soc_component
*component
= dai
->component
;
1416 struct rt711_sdca_priv
*rt711
= snd_soc_component_get_drvdata(component
);
1417 struct sdw_stream_runtime
*sdw_stream
=
1418 snd_soc_dai_get_dma_data(dai
, substream
);
1423 sdw_stream_remove_slave(rt711
->slave
, sdw_stream
);
1427 #define RT711_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | \
1428 SNDRV_PCM_RATE_192000)
1429 #define RT711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1430 SNDRV_PCM_FMTBIT_S24_LE)
1432 static const struct snd_soc_dai_ops rt711_sdca_ops
= {
1433 .hw_params
= rt711_sdca_pcm_hw_params
,
1434 .hw_free
= rt711_sdca_pcm_hw_free
,
1435 .set_stream
= rt711_sdca_set_sdw_stream
,
1436 .shutdown
= rt711_sdca_shutdown
,
1439 static struct snd_soc_dai_driver rt711_sdca_dai
[] = {
1441 .name
= "rt711-sdca-aif1",
1444 .stream_name
= "DP3 Playback",
1447 .rates
= RT711_STEREO_RATES
,
1448 .formats
= RT711_FORMATS
,
1451 .stream_name
= "DP2 Capture",
1454 .rates
= RT711_STEREO_RATES
,
1455 .formats
= RT711_FORMATS
,
1457 .ops
= &rt711_sdca_ops
,
1460 .name
= "rt711-sdca-aif2",
1463 .stream_name
= "DP4 Capture",
1466 .rates
= RT711_STEREO_RATES
,
1467 .formats
= RT711_FORMATS
,
1469 .ops
= &rt711_sdca_ops
,
1473 int rt711_sdca_init(struct device
*dev
, struct regmap
*regmap
,
1474 struct regmap
*mbq_regmap
, struct sdw_slave
*slave
)
1476 struct rt711_sdca_priv
*rt711
;
1479 rt711
= devm_kzalloc(dev
, sizeof(*rt711
), GFP_KERNEL
);
1483 dev_set_drvdata(dev
, rt711
);
1484 rt711
->slave
= slave
;
1485 rt711
->regmap
= regmap
;
1486 rt711
->mbq_regmap
= mbq_regmap
;
1488 regcache_cache_only(rt711
->regmap
, true);
1489 regcache_cache_only(rt711
->mbq_regmap
, true);
1491 mutex_init(&rt711
->calibrate_mutex
);
1492 mutex_init(&rt711
->disable_irq_lock
);
1494 INIT_DELAYED_WORK(&rt711
->jack_detect_work
, rt711_sdca_jack_detect_handler
);
1495 INIT_DELAYED_WORK(&rt711
->jack_btn_check_work
, rt711_sdca_btn_check_handler
);
1498 * Mark hw_init to false
1499 * HW init will be performed when device reports present
1501 rt711
->hw_init
= false;
1502 rt711
->first_hw_init
= false;
1503 rt711
->fu0f_dapm_mute
= true;
1504 rt711
->fu1e_dapm_mute
= true;
1505 rt711
->fu0f_mixer_l_mute
= rt711
->fu0f_mixer_r_mute
= true;
1506 rt711
->fu1e_mixer_l_mute
= rt711
->fu1e_mixer_r_mute
= true;
1508 /* JD source uses JD2 in default */
1509 rt711
->jd_src
= RT711_JD2
;
1511 ret
= devm_snd_soc_register_component(dev
,
1512 &soc_sdca_dev_rt711
,
1514 ARRAY_SIZE(rt711_sdca_dai
));
1519 /* set autosuspend parameters */
1520 pm_runtime_set_autosuspend_delay(dev
, 3000);
1521 pm_runtime_use_autosuspend(dev
);
1523 /* make sure the device does not suspend immediately */
1524 pm_runtime_mark_last_busy(dev
);
1526 pm_runtime_enable(dev
);
1528 /* important note: the device is NOT tagged as 'active' and will remain
1529 * 'suspended' until the hardware is enumerated/initialized. This is required
1530 * to make sure the ASoC framework use of pm_runtime_get_sync() does not silently
1531 * fail with -EACCESS because of race conditions between card creation and enumeration
1534 dev_dbg(dev
, "%s\n", __func__
);
1539 static void rt711_sdca_vd0_io_init(struct rt711_sdca_priv
*rt711
)
1541 rt711_sdca_index_write(rt711
, RT711_VENDOR_REG
,
1542 RT711_GPIO_TEST_MODE_CTL2
, 0x0e00);
1543 rt711_sdca_index_write(rt711
, RT711_VENDOR_HDA_CTL
,
1544 RT711_HDA_LEGACY_GPIO_CTL
, 0x0008);
1546 regmap_write(rt711
->regmap
, 0x2f5a, 0x01);
1548 rt711_sdca_index_write(rt711
, RT711_VENDOR_REG
,
1549 RT711_ADC27_VOL_SET
, 0x8728);
1551 rt711_sdca_index_write(rt711
, RT711_VENDOR_REG
,
1552 RT711_COMBO_JACK_AUTO_CTL3
, 0xa472);
1554 regmap_write(rt711
->regmap
, 0x2f50, 0x02);
1556 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_ANALOG_CTL
,
1557 RT711_MISC_POWER_CTL4
, 0x6000, 0x6000);
1559 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_REG
,
1560 RT711_COMBO_JACK_AUTO_CTL3
, 0x000c, 0x000c);
1562 rt711_sdca_index_write(rt711
, RT711_VENDOR_HDA_CTL
,
1563 RT711_HDA_LEGACY_CONFIG_CTL
, 0x0000);
1565 rt711_sdca_index_write(rt711
, RT711_VENDOR_VAD
,
1566 RT711_VAD_SRAM_CTL1
, 0x0050);
1569 static void rt711_sdca_vd1_io_init(struct rt711_sdca_priv
*rt711
)
1571 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_HDA_CTL
,
1572 RT711_HDA_LEGACY_UNSOLICITED_CTL
, 0x0300, 0x0000);
1574 rt711_sdca_index_write(rt711
, RT711_VENDOR_REG
,
1575 RT711_COMBO_JACK_AUTO_CTL3
, 0xa43e);
1577 regmap_write(rt711
->regmap
, 0x2f5a, 0x05);
1579 rt711_sdca_index_write(rt711
, RT711_VENDOR_REG
,
1580 RT711_JD_CTRL6
, 0x0500);
1582 rt711_sdca_index_write(rt711
, RT711_VENDOR_REG
,
1583 RT711_DMIC_CTL1
, 0x6173);
1585 rt711_sdca_index_write(rt711
, RT711_VENDOR_HDA_CTL
,
1586 RT711_HDA_LEGACY_CONFIG_CTL
, 0x0000);
1588 rt711_sdca_index_write(rt711
, RT711_VENDOR_VAD
,
1589 RT711_VAD_SRAM_CTL1
, 0x0050);
1592 int rt711_sdca_io_init(struct device
*dev
, struct sdw_slave
*slave
)
1594 struct rt711_sdca_priv
*rt711
= dev_get_drvdata(dev
);
1598 rt711
->disable_irq
= false;
1603 regcache_cache_only(rt711
->regmap
, false);
1604 regcache_cache_only(rt711
->mbq_regmap
, false);
1606 if (rt711
->first_hw_init
) {
1607 regcache_cache_bypass(rt711
->regmap
, true);
1608 regcache_cache_bypass(rt711
->mbq_regmap
, true);
1611 * PM runtime status is marked as 'active' only when a Slave reports as Attached
1614 /* update count of parent 'active' children */
1615 pm_runtime_set_active(&slave
->dev
);
1618 pm_runtime_get_noresume(&slave
->dev
);
1620 rt711_sdca_reset(rt711
);
1622 rt711_sdca_index_read(rt711
, RT711_VENDOR_REG
, RT711_JD_PRODUCT_NUM
, &val
);
1623 rt711
->hw_ver
= val
& 0xf;
1625 if (rt711
->hw_ver
== RT711_VER_VD0
)
1626 rt711_sdca_vd0_io_init(rt711
);
1628 rt711_sdca_vd1_io_init(rt711
);
1630 /* DP4 mux select from 08_filter_Out_pri */
1631 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_REG
,
1632 RT711_FILTER_SRC_SEL
, 0x1800, 0x0800);
1634 /* ge_exclusive_inbox_en disable */
1635 rt711_sdca_index_update_bits(rt711
, RT711_VENDOR_HDA_CTL
,
1636 RT711_PUSH_BTN_INT_CTL0
, 0x20, 0x00);
1639 ret
= rt711_sdca_calibration(rt711
);
1641 dev_err(dev
, "%s, calibration failed!\n", __func__
);
1643 /* HP output enable */
1644 regmap_write(rt711
->regmap
,
1645 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC
, RT711_SDCA_ENT_OT1
, RT711_SDCA_CTL_VENDOR_DEF
, 0), 0x4);
1648 * if set_jack callback occurred early than io_init,
1649 * we set up the jack detection function now
1652 rt711_sdca_jack_init(rt711
);
1654 if (rt711
->first_hw_init
) {
1655 regcache_cache_bypass(rt711
->regmap
, false);
1656 regcache_mark_dirty(rt711
->regmap
);
1657 regcache_cache_bypass(rt711
->mbq_regmap
, false);
1658 regcache_mark_dirty(rt711
->mbq_regmap
);
1660 rt711
->first_hw_init
= true;
1662 /* Mark Slave initialization complete */
1663 rt711
->hw_init
= true;
1665 pm_runtime_mark_last_busy(&slave
->dev
);
1666 pm_runtime_put_autosuspend(&slave
->dev
);
1668 dev_dbg(&slave
->dev
, "%s hw_init complete\n", __func__
);
1672 MODULE_DESCRIPTION("ASoC RT711 SDCA SDW driver");
1673 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
1674 MODULE_LICENSE("GPL");