1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // rt711-sdca.c -- rt711 SDCA ALSA SoC audio driver
4 //
5 // Copyright(c) 2021 Realtek Semiconductor Corp.
6 //
7 //
8
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>
26
27 #include "rt711-sdca.h"
28
rt711_sdca_index_write(struct rt711_sdca_priv * rt711,unsigned int nid,unsigned int reg,unsigned int value)29 static int rt711_sdca_index_write(struct rt711_sdca_priv *rt711,
30 unsigned int nid, unsigned int reg, unsigned int value)
31 {
32 int ret;
33 struct regmap *regmap = rt711->mbq_regmap;
34 unsigned int addr = (nid << 20) | reg;
35
36 ret = regmap_write(regmap, addr, value);
37 if (ret < 0)
38 dev_err(&rt711->slave->dev,
39 "%s: Failed to set private value: %06x <= %04x ret=%d\n",
40 __func__, addr, value, ret);
41
42 return ret;
43 }
44
rt711_sdca_index_read(struct rt711_sdca_priv * rt711,unsigned int nid,unsigned int reg,unsigned int * value)45 static int rt711_sdca_index_read(struct rt711_sdca_priv *rt711,
46 unsigned int nid, unsigned int reg, unsigned int *value)
47 {
48 int ret;
49 struct regmap *regmap = rt711->mbq_regmap;
50 unsigned int addr = (nid << 20) | reg;
51
52 ret = regmap_read(regmap, addr, value);
53 if (ret < 0)
54 dev_err(&rt711->slave->dev,
55 "%s: Failed to get private value: %06x => %04x ret=%d\n",
56 __func__, addr, *value, ret);
57
58 return ret;
59 }
60
rt711_sdca_index_update_bits(struct rt711_sdca_priv * rt711,unsigned int nid,unsigned int reg,unsigned int mask,unsigned int val)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)
63 {
64 unsigned int tmp;
65 int ret;
66
67 ret = rt711_sdca_index_read(rt711, nid, reg, &tmp);
68 if (ret < 0)
69 return ret;
70
71 set_mask_bits(&tmp, mask, val);
72 return rt711_sdca_index_write(rt711, nid, reg, tmp);
73 }
74
rt711_sdca_reset(struct rt711_sdca_priv * rt711)75 static void rt711_sdca_reset(struct rt711_sdca_priv *rt711)
76 {
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);
82 }
83
rt711_sdca_ge_force_jack_type(struct rt711_sdca_priv * rt711,unsigned int det_mode)84 static void rt711_sdca_ge_force_jack_type(struct rt711_sdca_priv *rt711, unsigned int det_mode)
85 {
86 switch (det_mode) {
87 case 0x00:
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);
90 break;
91 case 0x03:
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);
94 break;
95 case 0x05:
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);
98 break;
99 }
100 }
101
rt711_sdca_calibration(struct rt711_sdca_priv * rt711)102 static int rt711_sdca_calibration(struct rt711_sdca_priv *rt711)
103 {
104 unsigned int val, loop_rc = 0, loop_dc = 0;
105 struct device *dev;
106 struct regmap *regmap = rt711->regmap;
107 int chk_cnt = 100;
108 int ret = 0;
109
110 mutex_lock(&rt711->calibrate_mutex);
111 dev = regmap_get_device(regmap);
112
113 regmap_read(rt711->regmap, RT711_RC_CAL_STATUS, &val);
114 /* RC calibration */
115 if (!(val & 0x40))
116 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_ANALOG_CTL,
117 RT711_MISC_POWER_CTL0, 0x0010, 0x0010);
118
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);
122 if (ret < 0)
123 goto _cali_fail_;
124 }
125 if (loop_rc == chk_cnt)
126 dev_err(dev, "%s, RC calibration time-out!\n", __func__);
127
128 /* HP calibration by manual mode setting */
129 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG,
130 RT711_FSM_CTL, 0x2000, 0x2000);
131
132 /* Calibration manual mode */
133 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG,
134 RT711_FSM_CTL, 0xf, RT711_CALI_CTL);
135
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);
142
143 /* cal_clk_en_reg */
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);
148
149 /* trigger */
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);
153
154 /* wait for calibration process */
155 rt711_sdca_index_read(rt711, RT711_VENDOR_CALI,
156 RT711_DAC_DC_CALI_CTL1, &val);
157
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);
163 if (ret < 0)
164 goto _cali_fail_;
165 }
166 if (loop_dc == chk_cnt)
167 dev_err(dev, "%s, calibration time-out!\n", __func__);
168
169 if (loop_dc == chk_cnt || loop_rc == chk_cnt)
170 ret = -ETIMEDOUT;
171
172 _cali_fail_:
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);
176
177 /* release HP-JD and trigger FSM */
178 rt711_sdca_index_write(rt711, RT711_VENDOR_REG,
179 RT711_DIGITAL_MISC_CTRL4, 0x201b);
180
181 mutex_unlock(&rt711->calibrate_mutex);
182 dev_dbg(dev, "%s calibration complete, ret=%d\n", __func__, ret);
183 return ret;
184 }
185
rt711_sdca_button_detect(struct rt711_sdca_priv * rt711)186 static unsigned int rt711_sdca_button_detect(struct rt711_sdca_priv *rt711)
187 {
188 unsigned int btn_type = 0, offset, idx, val, owner;
189 int ret;
190 unsigned char buf[3];
191
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),
195 &owner);
196 if (ret < 0)
197 return 0;
198
199 /* if owner is device then there is no button event from device */
200 if (owner == 1)
201 return 0;
202
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),
206 &offset);
207 if (ret < 0)
208 goto _end_btn_det_;
209
210 for (idx = 0; idx < sizeof(buf); idx++) {
211 ret = regmap_read(rt711->regmap,
212 RT711_BUF_ADDR_HID1 + offset + idx, &val);
213 if (ret < 0)
214 goto _end_btn_det_;
215 buf[idx] = val & 0xff;
216 }
217
218 if (buf[0] == 0x11) {
219 switch (buf[1] & 0xf0) {
220 case 0x10:
221 btn_type |= SND_JACK_BTN_2;
222 break;
223 case 0x20:
224 btn_type |= SND_JACK_BTN_3;
225 break;
226 case 0x40:
227 btn_type |= SND_JACK_BTN_0;
228 break;
229 case 0x80:
230 btn_type |= SND_JACK_BTN_1;
231 break;
232 }
233 switch (buf[2]) {
234 case 0x01:
235 case 0x10:
236 btn_type |= SND_JACK_BTN_2;
237 break;
238 case 0x02:
239 case 0x20:
240 btn_type |= SND_JACK_BTN_3;
241 break;
242 case 0x04:
243 case 0x40:
244 btn_type |= SND_JACK_BTN_0;
245 break;
246 case 0x08:
247 case 0x80:
248 btn_type |= SND_JACK_BTN_1;
249 break;
250 }
251 }
252
253 _end_btn_det_:
254 /* Host is owner, so set back to device */
255 if (owner == 0)
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);
260
261 return btn_type;
262 }
263
rt711_sdca_headset_detect(struct rt711_sdca_priv * rt711)264 static int rt711_sdca_headset_detect(struct rt711_sdca_priv *rt711)
265 {
266 unsigned int det_mode;
267 int ret;
268
269 rt711_sdca_ge_force_jack_type(rt711, rt711->ge_mode_override);
270
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),
274 &det_mode);
275 if (ret < 0)
276 goto io_error;
277
278 switch (det_mode) {
279 case 0x00:
280 rt711->jack_type = 0;
281 break;
282 case 0x03:
283 rt711->jack_type = SND_JACK_HEADPHONE;
284 break;
285 case 0x05:
286 rt711->jack_type = SND_JACK_HEADSET;
287 break;
288 }
289
290 /* write selected_mode */
291 if (det_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),
294 det_mode);
295 if (ret < 0)
296 goto io_error;
297 }
298
299 dev_dbg(&rt711->slave->dev,
300 "%s, detected_mode=0x%x\n", __func__, det_mode);
301
302 return 0;
303
304 io_error:
305 pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
306 return ret;
307 }
308
rt711_sdca_jack_detect_handler(struct work_struct * work)309 static void rt711_sdca_jack_detect_handler(struct work_struct *work)
310 {
311 struct rt711_sdca_priv *rt711 =
312 container_of(work, struct rt711_sdca_priv, jack_detect_work.work);
313 int btn_type = 0, ret;
314
315 if (!rt711->hs_jack)
316 return;
317
318 if (!snd_soc_card_is_instantiated(rt711->component->card))
319 return;
320
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);
324 if (ret < 0)
325 return;
326 }
327
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);
331
332 if (rt711->jack_type == 0)
333 btn_type = 0;
334
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);
342
343 snd_soc_jack_report(rt711->hs_jack, rt711->jack_type | btn_type,
344 SND_JACK_HEADSET |
345 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
346 SND_JACK_BTN_2 | SND_JACK_BTN_3);
347
348 if (btn_type) {
349 /* button released */
350 snd_soc_jack_report(rt711->hs_jack, rt711->jack_type,
351 SND_JACK_HEADSET |
352 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
353 SND_JACK_BTN_2 | SND_JACK_BTN_3);
354
355 mod_delayed_work(system_power_efficient_wq,
356 &rt711->jack_btn_check_work, msecs_to_jiffies(200));
357 }
358 }
359
rt711_sdca_btn_check_handler(struct work_struct * work)360 static void rt711_sdca_btn_check_handler(struct work_struct *work)
361 {
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];
367
368 ret = regmap_read(rt711->regmap,
369 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, RT711_SDCA_CTL_DETECTED_MODE, 0),
370 &det_mode);
371 if (ret < 0)
372 goto io_error;
373
374 /* pin attached */
375 if (det_mode) {
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),
379 &offset);
380 if (ret < 0)
381 goto io_error;
382
383 for (idx = 0; idx < sizeof(buf); idx++) {
384 ret = regmap_read(rt711->regmap,
385 RT711_BUF_ADDR_HID1 + offset + idx, &val);
386 if (ret < 0)
387 goto io_error;
388 buf[idx] = val & 0xff;
389 }
390
391 if (buf[0] == 0x11) {
392 switch (buf[1] & 0xf0) {
393 case 0x10:
394 btn_type |= SND_JACK_BTN_2;
395 break;
396 case 0x20:
397 btn_type |= SND_JACK_BTN_3;
398 break;
399 case 0x40:
400 btn_type |= SND_JACK_BTN_0;
401 break;
402 case 0x80:
403 btn_type |= SND_JACK_BTN_1;
404 break;
405 }
406 switch (buf[2]) {
407 case 0x01:
408 case 0x10:
409 btn_type |= SND_JACK_BTN_2;
410 break;
411 case 0x02:
412 case 0x20:
413 btn_type |= SND_JACK_BTN_3;
414 break;
415 case 0x04:
416 case 0x40:
417 btn_type |= SND_JACK_BTN_0;
418 break;
419 case 0x08:
420 case 0x80:
421 btn_type |= SND_JACK_BTN_1;
422 break;
423 }
424 }
425 } else
426 rt711->jack_type = 0;
427
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,
430 SND_JACK_HEADSET |
431 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
432 SND_JACK_BTN_2 | SND_JACK_BTN_3);
433
434 if (btn_type) {
435 /* button released */
436 snd_soc_jack_report(rt711->hs_jack, rt711->jack_type,
437 SND_JACK_HEADSET |
438 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
439 SND_JACK_BTN_2 | SND_JACK_BTN_3);
440
441 mod_delayed_work(system_power_efficient_wq,
442 &rt711->jack_btn_check_work, msecs_to_jiffies(200));
443 }
444
445 return;
446
447 io_error:
448 pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
449 }
450
rt711_sdca_jack_init(struct rt711_sdca_priv * rt711)451 static void rt711_sdca_jack_init(struct rt711_sdca_priv *rt711)
452 {
453 mutex_lock(&rt711->calibrate_mutex);
454
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);
465
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);
469
470 switch (rt711->jd_src) {
471 case RT711_JD1:
472 /* default settings was already for JD1 */
473 break;
474 case RT711_JD2:
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,
482 RT711_CC_DET1,
483 RT711_HP_JD_FINAL_RESULT_CTL_JD12,
484 RT711_HP_JD_FINAL_RESULT_CTL_JD12);
485 break;
486 case RT711_JD2_100K:
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,
497 RT711_CC_DET1,
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);
500 break;
501 default:
502 dev_warn(rt711->component->dev, "Wrong JD source\n");
503 break;
504 }
505
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__);
511 } else {
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);
515
516 dev_dbg(&rt711->slave->dev, "in %s disable\n", __func__);
517 }
518
519 mutex_unlock(&rt711->calibrate_mutex);
520 }
521
rt711_sdca_set_jack_detect(struct snd_soc_component * component,struct snd_soc_jack * hs_jack,void * data)522 static int rt711_sdca_set_jack_detect(struct snd_soc_component *component,
523 struct snd_soc_jack *hs_jack, void *data)
524 {
525 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
526 int ret;
527
528 rt711->hs_jack = hs_jack;
529
530 /* we can only resume if the device was initialized at least once */
531 if (!rt711->first_hw_init)
532 return 0;
533
534 ret = pm_runtime_resume_and_get(component->dev);
535 if (ret < 0) {
536 if (ret != -EACCES) {
537 dev_err(component->dev, "%s: failed to resume %d\n", __func__, ret);
538 return ret;
539 }
540
541 /* pm_runtime not enabled yet */
542 dev_dbg(component->dev, "%s: skipping jack init for now\n", __func__);
543 return 0;
544 }
545
546 rt711_sdca_jack_init(rt711);
547
548 pm_runtime_mark_last_busy(component->dev);
549 pm_runtime_put_autosuspend(component->dev);
550
551 return 0;
552 }
553
554 /* For SDCA control DAC/ADC Gain */
rt711_sdca_set_gain_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)555 static int rt711_sdca_set_gain_put(struct snd_kcontrol *kcontrol,
556 struct snd_ctl_elem_value *ucontrol)
557 {
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;
565
566 if (strstr(ucontrol->id.name, "FU1E Capture Volume") ||
567 strstr(ucontrol->id.name, "FU0F Capture Volume"))
568 adc_vol_flag = 1;
569
570 regmap_read(rt711->mbq_regmap, mc->reg, &lvalue);
571 regmap_read(rt711->mbq_regmap, mc->rreg, &rvalue);
572
573 /* control value to 2's complement value */
574 /* L Channel */
575 gain_l_val = ucontrol->value.integer.value[0];
576 if (gain_l_val > mc->max)
577 gain_l_val = mc->max;
578 read_l = gain_l_val;
579
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;
585 else
586 gain_l_val = (mc->shift - gain_l_val) * 75;
587 gain_l_val <<= 8;
588 gain_l_val /= 100;
589 if (!(adc_vol_flag && read_l > mc->shift)) {
590 gain_l_val = ~gain_l_val;
591 gain_l_val += 1;
592 }
593 gain_l_val &= 0xffff;
594 }
595
596 /* R Channel */
597 gain_r_val = ucontrol->value.integer.value[1];
598 if (gain_r_val > mc->max)
599 gain_r_val = mc->max;
600 read_r = gain_r_val;
601
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;
607 else
608 gain_r_val = (mc->shift - gain_r_val) * 75;
609 gain_r_val <<= 8;
610 gain_r_val /= 100;
611 if (!(adc_vol_flag && read_r > mc->shift)) {
612 gain_r_val = ~gain_r_val;
613 gain_r_val += 1;
614 }
615 gain_r_val &= 0xffff;
616 }
617
618 if (lvalue != gain_l_val || rvalue != gain_r_val)
619 changed = 1;
620 else
621 return 0;
622
623 for (i = 0; i < 3; i++) { /* retry 3 times at most */
624 /* Lch*/
625 regmap_write(rt711->mbq_regmap, mc->reg, gain_l_val);
626
627 /* Rch */
628 regmap_write(rt711->mbq_regmap, mc->rreg, gain_r_val);
629
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)
633 break;
634 }
635
636 return i == 3 ? -EIO : changed;
637 }
638
rt711_sdca_set_gain_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)639 static int rt711_sdca_set_gain_get(struct snd_kcontrol *kcontrol,
640 struct snd_ctl_elem_value *ucontrol)
641 {
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;
648
649 if (strstr(ucontrol->id.name, "FU1E Capture Volume") ||
650 strstr(ucontrol->id.name, "FU0F Capture Volume"))
651 adc_vol_flag = 1;
652
653 regmap_read(rt711->mbq_regmap, mc->reg, &read_l);
654 regmap_read(rt711->mbq_regmap, mc->rreg, &read_r);
655
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 */
660 ctl_l = read_l;
661 if (read_l & BIT(15)) {
662 ctl_l = 0xffff & ~(read_l - 1);
663 neg_flag = 1;
664 }
665 ctl_l *= 100;
666 ctl_l >>= 8;
667 if (adc_vol_flag) {
668 if (neg_flag)
669 ctl_l = mc->shift - (ctl_l / 75);
670 else
671 ctl_l = mc->shift + (ctl_l / 75);
672 } else
673 ctl_l = mc->max - (ctl_l / 75);
674 }
675
676 neg_flag = 0;
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 */
681 ctl_r = read_r;
682 if (read_r & BIT(15)) {
683 ctl_r = 0xffff & ~(read_r - 1);
684 neg_flag = 1;
685 }
686 ctl_r *= 100;
687 ctl_r >>= 8;
688 if (adc_vol_flag) {
689 if (neg_flag)
690 ctl_r = mc->shift - (ctl_r / 75);
691 else
692 ctl_r = mc->shift + (ctl_r / 75);
693 } else
694 ctl_r = mc->max - (ctl_r / 75);
695 }
696 } else
697 ctl_r = ctl_l;
698
699 ucontrol->value.integer.value[0] = ctl_l;
700 ucontrol->value.integer.value[1] = ctl_r;
701
702 return 0;
703 }
704
rt711_sdca_set_fu0f_capture_ctl(struct rt711_sdca_priv * rt711)705 static int rt711_sdca_set_fu0f_capture_ctl(struct rt711_sdca_priv *rt711)
706 {
707 int err;
708 unsigned int ch_l, ch_r;
709
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;
712
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);
716 if (err < 0)
717 return err;
718
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);
722 if (err < 0)
723 return err;
724
725 return 0;
726 }
727
rt711_sdca_set_fu1e_capture_ctl(struct rt711_sdca_priv * rt711)728 static int rt711_sdca_set_fu1e_capture_ctl(struct rt711_sdca_priv *rt711)
729 {
730 int err;
731 unsigned int ch_l, ch_r;
732
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;
735
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);
739 if (err < 0)
740 return err;
741
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);
745 if (err < 0)
746 return err;
747
748 return 0;
749 }
750
rt711_sdca_fu1e_capture_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)751 static int rt711_sdca_fu1e_capture_get(struct snd_kcontrol *kcontrol,
752 struct snd_ctl_elem_value *ucontrol)
753 {
754 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
755 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
756
757 ucontrol->value.integer.value[0] = !rt711->fu1e_mixer_l_mute;
758 ucontrol->value.integer.value[1] = !rt711->fu1e_mixer_r_mute;
759 return 0;
760 }
761
rt711_sdca_fu1e_capture_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)762 static int rt711_sdca_fu1e_capture_put(struct snd_kcontrol *kcontrol,
763 struct snd_ctl_elem_value *ucontrol)
764 {
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;
768
769 if (rt711->fu1e_mixer_l_mute != !ucontrol->value.integer.value[0] ||
770 rt711->fu1e_mixer_r_mute != !ucontrol->value.integer.value[1])
771 changed = 1;
772
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);
776 if (err < 0)
777 return err;
778
779 return changed;
780 }
781
rt711_sdca_fu0f_capture_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)782 static int rt711_sdca_fu0f_capture_get(struct snd_kcontrol *kcontrol,
783 struct snd_ctl_elem_value *ucontrol)
784 {
785 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
786 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
787
788 ucontrol->value.integer.value[0] = !rt711->fu0f_mixer_l_mute;
789 ucontrol->value.integer.value[1] = !rt711->fu0f_mixer_r_mute;
790 return 0;
791 }
792
rt711_sdca_fu0f_capture_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)793 static int rt711_sdca_fu0f_capture_put(struct snd_kcontrol *kcontrol,
794 struct snd_ctl_elem_value *ucontrol)
795 {
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;
799
800 if (rt711->fu0f_mixer_l_mute != !ucontrol->value.integer.value[0] ||
801 rt711->fu0f_mixer_r_mute != !ucontrol->value.integer.value[1])
802 changed = 1;
803
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);
807 if (err < 0)
808 return err;
809
810 return changed;
811 }
812
rt711_sdca_ge_select_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)813 static int rt711_sdca_ge_select_get(struct snd_kcontrol *kcontrol,
814 struct snd_ctl_elem_value *ucontrol)
815 {
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;
820
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;
824 return 0;
825 }
826
rt711_sdca_ge_select_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)827 static int rt711_sdca_ge_select_put(struct snd_kcontrol *kcontrol,
828 struct snd_ctl_elem_value *ucontrol)
829 {
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;
835
836 if (item[0] >= e->items)
837 return -EINVAL;
838
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;
842 change = 1;
843 }
844
845 return change;
846 }
847
848 static const char * const rt711_sdca_ge_select[] = {
849 "Auto",
850 "Headphone",
851 "Headset",
852 };
853
854 static int rt711_sdca_ge_select_values[] = {
855 0,
856 3,
857 5,
858 };
859
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);
862
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);
866
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),
871 0x57, 0x57, 0,
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),
880 0x17, 0x3f, 0,
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),
885 0x17, 0x3f, 0,
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),
890 8, 3, 0,
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),
895 8, 3, 0,
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),
899 };
900
rt711_sdca_mux_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)901 static int rt711_sdca_mux_get(struct snd_kcontrol *kcontrol,
902 struct snd_ctl_elem_value *ucontrol)
903 {
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;
908
909 if (strstr(ucontrol->id.name, "ADC 22 Mux"))
910 mask_sft = 10;
911 else if (strstr(ucontrol->id.name, "ADC 23 Mux"))
912 mask_sft = 13;
913 else
914 return -EINVAL;
915
916 rt711_sdca_index_read(rt711, RT711_VENDOR_HDA_CTL,
917 RT711_HDA_LEGACY_MUX_CTL1, &val);
918
919 ucontrol->value.enumerated.item[0] = (val >> mask_sft) & 0x7;
920
921 return 0;
922 }
923
rt711_sdca_mux_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)924 static int rt711_sdca_mux_put(struct snd_kcontrol *kcontrol,
925 struct snd_ctl_elem_value *ucontrol)
926 {
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;
935
936 if (item[0] >= e->items)
937 return -EINVAL;
938
939 if (strstr(ucontrol->id.name, "ADC 22 Mux"))
940 mask_sft = 10;
941 else if (strstr(ucontrol->id.name, "ADC 23 Mux"))
942 mask_sft = 13;
943 else
944 return -EINVAL;
945
946 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
947
948 rt711_sdca_index_read(rt711, RT711_VENDOR_HDA_CTL,
949 RT711_HDA_LEGACY_MUX_CTL1, &val2);
950 val2 = (val2 >> mask_sft) & 0x7;
951
952 if (val == val2)
953 change = 0;
954 else
955 change = 1;
956
957 if (change)
958 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL,
959 RT711_HDA_LEGACY_MUX_CTL1, 0x7 << mask_sft,
960 val << mask_sft);
961
962 snd_soc_dapm_mux_update_power(dapm, kcontrol,
963 item[0], e, NULL);
964
965 return change;
966 }
967
968 static const char * const adc_mux_text[] = {
969 "MIC2",
970 "LINE1",
971 "LINE2",
972 "DMIC",
973 };
974
975 static SOC_ENUM_SINGLE_DECL(
976 rt711_adc22_enum, SND_SOC_NOPM, 0, adc_mux_text);
977
978 static SOC_ENUM_SINGLE_DECL(
979 rt711_adc23_enum, SND_SOC_NOPM, 0, adc_mux_text);
980
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);
984
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);
988
rt711_sdca_fu05_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)989 static int rt711_sdca_fu05_event(struct snd_soc_dapm_widget *w,
990 struct snd_kcontrol *kcontrol, int event)
991 {
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;
996
997 switch (event) {
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),
1002 unmute);
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),
1006 unmute);
1007 break;
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),
1012 mute);
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),
1016 mute);
1017 break;
1018 }
1019 return 0;
1020 }
1021
rt711_sdca_fu0f_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)1022 static int rt711_sdca_fu0f_event(struct snd_soc_dapm_widget *w,
1023 struct snd_kcontrol *kcontrol, int event)
1024 {
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);
1028
1029 switch (event) {
1030 case SND_SOC_DAPM_POST_PMU:
1031 rt711->fu0f_dapm_mute = false;
1032 rt711_sdca_set_fu0f_capture_ctl(rt711);
1033 break;
1034 case SND_SOC_DAPM_PRE_PMD:
1035 rt711->fu0f_dapm_mute = true;
1036 rt711_sdca_set_fu0f_capture_ctl(rt711);
1037 break;
1038 }
1039 return 0;
1040 }
1041
rt711_sdca_fu1e_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)1042 static int rt711_sdca_fu1e_event(struct snd_soc_dapm_widget *w,
1043 struct snd_kcontrol *kcontrol, int event)
1044 {
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);
1048
1049 switch (event) {
1050 case SND_SOC_DAPM_POST_PMU:
1051 rt711->fu1e_dapm_mute = false;
1052 rt711_sdca_set_fu1e_capture_ctl(rt711);
1053 break;
1054 case SND_SOC_DAPM_PRE_PMD:
1055 rt711->fu1e_dapm_mute = true;
1056 rt711_sdca_set_fu1e_capture_ctl(rt711);
1057 break;
1058 }
1059 return 0;
1060 }
1061
rt711_sdca_pde28_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)1062 static int rt711_sdca_pde28_event(struct snd_soc_dapm_widget *w,
1063 struct snd_kcontrol *kcontrol, int event)
1064 {
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;
1069
1070 switch (event) {
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),
1075 ps0);
1076 break;
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),
1081 ps3);
1082 break;
1083 }
1084 return 0;
1085 }
1086
rt711_sdca_pde29_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)1087 static int rt711_sdca_pde29_event(struct snd_soc_dapm_widget *w,
1088 struct snd_kcontrol *kcontrol, int event)
1089 {
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;
1094
1095 switch (event) {
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),
1100 ps0);
1101 break;
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),
1106 ps3);
1107 break;
1108 }
1109 return 0;
1110 }
1111
rt711_sdca_pde2a_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)1112 static int rt711_sdca_pde2a_event(struct snd_soc_dapm_widget *w,
1113 struct snd_kcontrol *kcontrol, int event)
1114 {
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;
1119
1120 switch (event) {
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),
1125 ps0);
1126 break;
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),
1131 ps3);
1132 break;
1133 }
1134 return 0;
1135 }
1136
rt711_sdca_line1_power_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)1137 static int rt711_sdca_line1_power_event(struct snd_soc_dapm_widget *w,
1138 struct snd_kcontrol *kcontrol, int event)
1139 {
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;
1144
1145 switch (event) {
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),
1150 &sel_mode);
1151 regmap_write(rt711->regmap,
1152 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_LINE1,
1153 RT711_SDCA_CTL_VENDOR_DEF, 0),
1154 0x1);
1155 regmap_write(rt711->regmap,
1156 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49,
1157 RT711_SDCA_CTL_SELECTED_MODE, 0),
1158 0x7);
1159 break;
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),
1164 0x0);
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),
1169 sel_mode);
1170 break;
1171 }
1172
1173 return 0;
1174 }
1175
rt711_sdca_line2_power_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)1176 static int rt711_sdca_line2_power_event(struct snd_soc_dapm_widget *w,
1177 struct snd_kcontrol *kcontrol, int event)
1178 {
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;
1183
1184 switch (event) {
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),
1189 ps0);
1190 regmap_write(rt711->regmap,
1191 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_LINE2,
1192 RT711_SDCA_CTL_VENDOR_DEF, 0),
1193 0x1);
1194 break;
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),
1199 0x0);
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),
1203 ps3);
1204 break;
1205 }
1206
1207 return 0;
1208 }
1209
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"),
1217
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),
1224
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),
1234
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),
1248
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),
1252 };
1253
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"},
1258
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"},
1264
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"},
1275
1276 {"HP", NULL, "FU 05"},
1277 };
1278
rt711_sdca_parse_dt(struct rt711_sdca_priv * rt711,struct device * dev)1279 static int rt711_sdca_parse_dt(struct rt711_sdca_priv *rt711, struct device *dev)
1280 {
1281 device_property_read_u32(dev, "realtek,jd-src", &rt711->jd_src);
1282
1283 return 0;
1284 }
1285
rt711_sdca_probe(struct snd_soc_component * component)1286 static int rt711_sdca_probe(struct snd_soc_component *component)
1287 {
1288 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component);
1289 int ret;
1290
1291 rt711_sdca_parse_dt(rt711, &rt711->slave->dev);
1292 rt711->component = component;
1293
1294 if (!rt711->first_hw_init)
1295 return 0;
1296
1297 ret = pm_runtime_resume(component->dev);
1298 if (ret < 0 && ret != -EACCES)
1299 return ret;
1300
1301 return 0;
1302 }
1303
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,
1313 .endianness = 1,
1314 };
1315
rt711_sdca_set_sdw_stream(struct snd_soc_dai * dai,void * sdw_stream,int direction)1316 static int rt711_sdca_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
1317 int direction)
1318 {
1319 snd_soc_dai_dma_data_set(dai, direction, sdw_stream);
1320
1321 return 0;
1322 }
1323
rt711_sdca_shutdown(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)1324 static void rt711_sdca_shutdown(struct snd_pcm_substream *substream,
1325 struct snd_soc_dai *dai)
1326 {
1327 snd_soc_dai_set_dma_data(dai, substream, NULL);
1328 }
1329
rt711_sdca_pcm_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)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)
1333 {
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;
1339 int retval;
1340 unsigned int sampling_rate;
1341
1342 dev_dbg(dai->dev, "%s %s", __func__, dai->name);
1343 sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
1344
1345 if (!sdw_stream)
1346 return -EINVAL;
1347
1348 if (!rt711->slave)
1349 return -EINVAL;
1350
1351 /* SoundWire specific configuration */
1352 snd_sdw_params_to_config(substream, params, &stream_config, &port_config);
1353
1354 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1355 port_config.num = 3;
1356 } else {
1357 if (dai->id == RT711_AIF1)
1358 port_config.num = 2;
1359 else if (dai->id == RT711_AIF2)
1360 port_config.num = 4;
1361 else
1362 return -EINVAL;
1363 }
1364
1365 retval = sdw_stream_add_slave(rt711->slave, &stream_config,
1366 &port_config, 1, sdw_stream);
1367 if (retval) {
1368 dev_err(dai->dev, "%s: Unable to configure port\n", __func__);
1369 return retval;
1370 }
1371
1372 if (params_channels(params) > 16) {
1373 dev_err(component->dev, "%s: Unsupported channels %d\n",
1374 __func__, params_channels(params));
1375 return -EINVAL;
1376 }
1377
1378 /* sampling rate configuration */
1379 switch (params_rate(params)) {
1380 case 44100:
1381 sampling_rate = RT711_SDCA_RATE_44100HZ;
1382 break;
1383 case 48000:
1384 sampling_rate = RT711_SDCA_RATE_48000HZ;
1385 break;
1386 case 96000:
1387 sampling_rate = RT711_SDCA_RATE_96000HZ;
1388 break;
1389 case 192000:
1390 sampling_rate = RT711_SDCA_RATE_192000HZ;
1391 break;
1392 default:
1393 dev_err(component->dev, "%s: Rate %d is not supported\n",
1394 __func__, params_rate(params));
1395 return -EINVAL;
1396 }
1397
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),
1401 sampling_rate);
1402 regmap_write(rt711->regmap,
1403 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_CS11, RT711_SDCA_CTL_SAMPLE_FREQ_INDEX, 0),
1404 sampling_rate);
1405 regmap_write(rt711->regmap,
1406 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_CS1F, RT711_SDCA_CTL_SAMPLE_FREQ_INDEX, 0),
1407 sampling_rate);
1408
1409 return 0;
1410 }
1411
rt711_sdca_pcm_hw_free(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)1412 static int rt711_sdca_pcm_hw_free(struct snd_pcm_substream *substream,
1413 struct snd_soc_dai *dai)
1414 {
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);
1419
1420 if (!rt711->slave)
1421 return -EINVAL;
1422
1423 sdw_stream_remove_slave(rt711->slave, sdw_stream);
1424 return 0;
1425 }
1426
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)
1431
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,
1437 };
1438
1439 static struct snd_soc_dai_driver rt711_sdca_dai[] = {
1440 {
1441 .name = "rt711-sdca-aif1",
1442 .id = RT711_AIF1,
1443 .playback = {
1444 .stream_name = "DP3 Playback",
1445 .channels_min = 1,
1446 .channels_max = 2,
1447 .rates = RT711_STEREO_RATES,
1448 .formats = RT711_FORMATS,
1449 },
1450 .capture = {
1451 .stream_name = "DP2 Capture",
1452 .channels_min = 1,
1453 .channels_max = 2,
1454 .rates = RT711_STEREO_RATES,
1455 .formats = RT711_FORMATS,
1456 },
1457 .ops = &rt711_sdca_ops,
1458 },
1459 {
1460 .name = "rt711-sdca-aif2",
1461 .id = RT711_AIF2,
1462 .capture = {
1463 .stream_name = "DP4 Capture",
1464 .channels_min = 1,
1465 .channels_max = 2,
1466 .rates = RT711_STEREO_RATES,
1467 .formats = RT711_FORMATS,
1468 },
1469 .ops = &rt711_sdca_ops,
1470 }
1471 };
1472
rt711_sdca_init(struct device * dev,struct regmap * regmap,struct regmap * mbq_regmap,struct sdw_slave * slave)1473 int rt711_sdca_init(struct device *dev, struct regmap *regmap,
1474 struct regmap *mbq_regmap, struct sdw_slave *slave)
1475 {
1476 struct rt711_sdca_priv *rt711;
1477 int ret;
1478
1479 rt711 = devm_kzalloc(dev, sizeof(*rt711), GFP_KERNEL);
1480 if (!rt711)
1481 return -ENOMEM;
1482
1483 dev_set_drvdata(dev, rt711);
1484 rt711->slave = slave;
1485 rt711->regmap = regmap;
1486 rt711->mbq_regmap = mbq_regmap;
1487
1488 regcache_cache_only(rt711->regmap, true);
1489 regcache_cache_only(rt711->mbq_regmap, true);
1490
1491 mutex_init(&rt711->calibrate_mutex);
1492 mutex_init(&rt711->disable_irq_lock);
1493
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);
1496
1497 /*
1498 * Mark hw_init to false
1499 * HW init will be performed when device reports present
1500 */
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;
1507
1508 /* JD source uses JD2 in default */
1509 rt711->jd_src = RT711_JD2;
1510
1511 ret = devm_snd_soc_register_component(dev,
1512 &soc_sdca_dev_rt711,
1513 rt711_sdca_dai,
1514 ARRAY_SIZE(rt711_sdca_dai));
1515
1516 if (ret < 0)
1517 return ret;
1518
1519 /* set autosuspend parameters */
1520 pm_runtime_set_autosuspend_delay(dev, 3000);
1521 pm_runtime_use_autosuspend(dev);
1522
1523 /* make sure the device does not suspend immediately */
1524 pm_runtime_mark_last_busy(dev);
1525
1526 pm_runtime_enable(dev);
1527
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
1532 */
1533
1534 dev_dbg(dev, "%s\n", __func__);
1535
1536 return 0;
1537 }
1538
rt711_sdca_vd0_io_init(struct rt711_sdca_priv * rt711)1539 static void rt711_sdca_vd0_io_init(struct rt711_sdca_priv *rt711)
1540 {
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);
1545
1546 regmap_write(rt711->regmap, 0x2f5a, 0x01);
1547
1548 rt711_sdca_index_write(rt711, RT711_VENDOR_REG,
1549 RT711_ADC27_VOL_SET, 0x8728);
1550
1551 rt711_sdca_index_write(rt711, RT711_VENDOR_REG,
1552 RT711_COMBO_JACK_AUTO_CTL3, 0xa472);
1553
1554 regmap_write(rt711->regmap, 0x2f50, 0x02);
1555
1556 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_ANALOG_CTL,
1557 RT711_MISC_POWER_CTL4, 0x6000, 0x6000);
1558
1559 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG,
1560 RT711_COMBO_JACK_AUTO_CTL3, 0x000c, 0x000c);
1561
1562 rt711_sdca_index_write(rt711, RT711_VENDOR_HDA_CTL,
1563 RT711_HDA_LEGACY_CONFIG_CTL, 0x0000);
1564
1565 rt711_sdca_index_write(rt711, RT711_VENDOR_VAD,
1566 RT711_VAD_SRAM_CTL1, 0x0050);
1567 }
1568
rt711_sdca_vd1_io_init(struct rt711_sdca_priv * rt711)1569 static void rt711_sdca_vd1_io_init(struct rt711_sdca_priv *rt711)
1570 {
1571 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL,
1572 RT711_HDA_LEGACY_UNSOLICITED_CTL, 0x0300, 0x0000);
1573
1574 rt711_sdca_index_write(rt711, RT711_VENDOR_REG,
1575 RT711_COMBO_JACK_AUTO_CTL3, 0xa43e);
1576
1577 regmap_write(rt711->regmap, 0x2f5a, 0x05);
1578
1579 rt711_sdca_index_write(rt711, RT711_VENDOR_REG,
1580 RT711_JD_CTRL6, 0x0500);
1581
1582 rt711_sdca_index_write(rt711, RT711_VENDOR_REG,
1583 RT711_DMIC_CTL1, 0x6173);
1584
1585 rt711_sdca_index_write(rt711, RT711_VENDOR_HDA_CTL,
1586 RT711_HDA_LEGACY_CONFIG_CTL, 0x0000);
1587
1588 rt711_sdca_index_write(rt711, RT711_VENDOR_VAD,
1589 RT711_VAD_SRAM_CTL1, 0x0050);
1590 }
1591
rt711_sdca_io_init(struct device * dev,struct sdw_slave * slave)1592 int rt711_sdca_io_init(struct device *dev, struct sdw_slave *slave)
1593 {
1594 struct rt711_sdca_priv *rt711 = dev_get_drvdata(dev);
1595 int ret = 0;
1596 unsigned int val;
1597
1598 rt711->disable_irq = false;
1599
1600 if (rt711->hw_init)
1601 return 0;
1602
1603 regcache_cache_only(rt711->regmap, false);
1604 regcache_cache_only(rt711->mbq_regmap, false);
1605
1606 if (rt711->first_hw_init) {
1607 regcache_cache_bypass(rt711->regmap, true);
1608 regcache_cache_bypass(rt711->mbq_regmap, true);
1609 } else {
1610 /*
1611 * PM runtime status is marked as 'active' only when a Slave reports as Attached
1612 */
1613
1614 /* update count of parent 'active' children */
1615 pm_runtime_set_active(&slave->dev);
1616 }
1617
1618 pm_runtime_get_noresume(&slave->dev);
1619
1620 rt711_sdca_reset(rt711);
1621
1622 rt711_sdca_index_read(rt711, RT711_VENDOR_REG, RT711_JD_PRODUCT_NUM, &val);
1623 rt711->hw_ver = val & 0xf;
1624
1625 if (rt711->hw_ver == RT711_VER_VD0)
1626 rt711_sdca_vd0_io_init(rt711);
1627 else
1628 rt711_sdca_vd1_io_init(rt711);
1629
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);
1633
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);
1637
1638 /* calibration */
1639 ret = rt711_sdca_calibration(rt711);
1640 if (ret < 0)
1641 dev_err(dev, "%s, calibration failed!\n", __func__);
1642
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);
1646
1647 /*
1648 * if set_jack callback occurred early than io_init,
1649 * we set up the jack detection function now
1650 */
1651 if (rt711->hs_jack)
1652 rt711_sdca_jack_init(rt711);
1653
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);
1659 } else
1660 rt711->first_hw_init = true;
1661
1662 /* Mark Slave initialization complete */
1663 rt711->hw_init = true;
1664
1665 pm_runtime_mark_last_busy(&slave->dev);
1666 pm_runtime_put_autosuspend(&slave->dev);
1667
1668 dev_dbg(&slave->dev, "%s hw_init complete\n", __func__);
1669 return 0;
1670 }
1671
1672 MODULE_DESCRIPTION("ASoC RT711 SDCA SDW driver");
1673 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
1674 MODULE_LICENSE("GPL");
1675