1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // rt711.c -- rt711 ALSA SoC audio driver
4 //
5 // Copyright(c) 2019 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/pm.h>
16 #include <linux/soundwire/sdw.h>
17 #include <linux/regmap.h>
18 #include <linux/slab.h>
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
23 #include <sound/soc-dapm.h>
24 #include <sound/initval.h>
25 #include <sound/tlv.h>
26 #include <sound/hda_verbs.h>
27 #include <sound/jack.h>
28
29 #include "rt711.h"
30
rt711_index_write(struct regmap * regmap,unsigned int nid,unsigned int reg,unsigned int value)31 static int rt711_index_write(struct regmap *regmap,
32 unsigned int nid, unsigned int reg, unsigned int value)
33 {
34 int ret;
35 unsigned int addr = ((RT711_PRIV_INDEX_W_H | nid) << 8) | reg;
36
37 ret = regmap_write(regmap, addr, value);
38 if (ret < 0)
39 pr_err("Failed to set private value: %06x <= %04x ret=%d\n",
40 addr, value, ret);
41
42 return ret;
43 }
44
rt711_index_read(struct regmap * regmap,unsigned int nid,unsigned int reg,unsigned int * value)45 static int rt711_index_read(struct regmap *regmap,
46 unsigned int nid, unsigned int reg, unsigned int *value)
47 {
48 int ret;
49 unsigned int addr = ((RT711_PRIV_INDEX_W_H | nid) << 8) | reg;
50
51 *value = 0;
52 ret = regmap_read(regmap, addr, value);
53 if (ret < 0)
54 pr_err("Failed to get private value: %06x => %04x ret=%d\n",
55 addr, *value, ret);
56
57 return ret;
58 }
59
rt711_index_update_bits(struct regmap * regmap,unsigned int nid,unsigned int reg,unsigned int mask,unsigned int val)60 static int rt711_index_update_bits(struct regmap *regmap, unsigned int nid,
61 unsigned int reg, unsigned int mask, unsigned int val)
62 {
63 unsigned int tmp, orig;
64 int ret;
65
66 ret = rt711_index_read(regmap, nid, reg, &orig);
67 if (ret < 0)
68 return ret;
69
70 tmp = orig & ~mask;
71 tmp |= val & mask;
72
73 return rt711_index_write(regmap, nid, reg, tmp);
74 }
75
rt711_reset(struct regmap * regmap)76 static void rt711_reset(struct regmap *regmap)
77 {
78 regmap_write(regmap, RT711_FUNC_RESET, 0);
79 rt711_index_update_bits(regmap, RT711_VENDOR_REG,
80 RT711_PARA_VERB_CTL, RT711_HIDDEN_REG_SW_RESET,
81 RT711_HIDDEN_REG_SW_RESET);
82 }
83
rt711_calibration(struct rt711_priv * rt711)84 static int rt711_calibration(struct rt711_priv *rt711)
85 {
86 unsigned int val, loop = 0;
87 struct device *dev;
88 struct regmap *regmap = rt711->regmap;
89 int ret = 0;
90
91 mutex_lock(&rt711->calibrate_mutex);
92 regmap_write(rt711->regmap,
93 RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
94
95 dev = regmap_get_device(regmap);
96
97 /* Calibration manual mode */
98 rt711_index_update_bits(regmap, RT711_VENDOR_REG, RT711_FSM_CTL,
99 0xf, 0x0);
100
101 /* trigger */
102 rt711_index_update_bits(regmap, RT711_VENDOR_CALI,
103 RT711_DAC_DC_CALI_CTL1, RT711_DAC_DC_CALI_TRIGGER,
104 RT711_DAC_DC_CALI_TRIGGER);
105
106 /* wait for calibration process */
107 rt711_index_read(regmap, RT711_VENDOR_CALI,
108 RT711_DAC_DC_CALI_CTL1, &val);
109
110 while (val & RT711_DAC_DC_CALI_TRIGGER) {
111 if (loop >= 500) {
112 pr_err("%s, calibration time-out!\n",
113 __func__);
114 ret = -ETIMEDOUT;
115 break;
116 }
117 loop++;
118
119 usleep_range(10000, 11000);
120 rt711_index_read(regmap, RT711_VENDOR_CALI,
121 RT711_DAC_DC_CALI_CTL1, &val);
122 }
123
124 /* depop mode */
125 rt711_index_update_bits(regmap, RT711_VENDOR_REG,
126 RT711_FSM_CTL, 0xf, RT711_DEPOP_CTL);
127
128 regmap_write(rt711->regmap,
129 RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
130 mutex_unlock(&rt711->calibrate_mutex);
131
132 dev_dbg(dev, "%s calibration complete, ret=%d\n", __func__, ret);
133 return ret;
134 }
135
rt711_button_detect(struct rt711_priv * rt711)136 static unsigned int rt711_button_detect(struct rt711_priv *rt711)
137 {
138 unsigned int btn_type = 0, val80, val81;
139 int ret;
140
141 ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG,
142 RT711_IRQ_FLAG_TABLE1, &val80);
143 if (ret < 0)
144 goto read_error;
145 ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG,
146 RT711_IRQ_FLAG_TABLE2, &val81);
147 if (ret < 0)
148 goto read_error;
149
150 val80 &= 0x0381;
151 val81 &= 0xff00;
152
153 switch (val80) {
154 case 0x0200:
155 case 0x0100:
156 case 0x0080:
157 btn_type |= SND_JACK_BTN_0;
158 break;
159 case 0x0001:
160 btn_type |= SND_JACK_BTN_3;
161 break;
162 }
163 switch (val81) {
164 case 0x8000:
165 case 0x4000:
166 case 0x2000:
167 btn_type |= SND_JACK_BTN_1;
168 break;
169 case 0x1000:
170 case 0x0800:
171 case 0x0400:
172 btn_type |= SND_JACK_BTN_2;
173 break;
174 case 0x0200:
175 case 0x0100:
176 btn_type |= SND_JACK_BTN_3;
177 break;
178 }
179 read_error:
180 return btn_type;
181 }
182
rt711_headset_detect(struct rt711_priv * rt711)183 static int rt711_headset_detect(struct rt711_priv *rt711)
184 {
185 unsigned int buf, loop = 0;
186 int ret;
187 unsigned int jack_status = 0, reg;
188
189 ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG,
190 RT711_COMBO_JACK_AUTO_CTL2, &buf);
191 if (ret < 0)
192 goto io_error;
193
194 while (loop < 500 &&
195 (buf & RT711_COMBOJACK_AUTO_DET_STATUS) == 0) {
196 loop++;
197
198 usleep_range(9000, 10000);
199 ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG,
200 RT711_COMBO_JACK_AUTO_CTL2, &buf);
201 if (ret < 0)
202 goto io_error;
203
204 reg = RT711_VERB_GET_PIN_SENSE | RT711_HP_OUT;
205 ret = regmap_read(rt711->regmap, reg, &jack_status);
206 if (ret < 0)
207 goto io_error;
208 if ((jack_status & (1 << 31)) == 0)
209 goto remove_error;
210 }
211
212 if (loop >= 500)
213 goto to_error;
214
215 if (buf & RT711_COMBOJACK_AUTO_DET_TRS)
216 rt711->jack_type = SND_JACK_HEADPHONE;
217 else if ((buf & RT711_COMBOJACK_AUTO_DET_CTIA) ||
218 (buf & RT711_COMBOJACK_AUTO_DET_OMTP))
219 rt711->jack_type = SND_JACK_HEADSET;
220
221 return 0;
222
223 to_error:
224 ret = -ETIMEDOUT;
225 pr_err_ratelimited("Time-out error in %s\n", __func__);
226 return ret;
227 io_error:
228 pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
229 return ret;
230 remove_error:
231 pr_err_ratelimited("Jack removal in %s\n", __func__);
232 return -ENODEV;
233 }
234
rt711_jack_detect_handler(struct work_struct * work)235 static void rt711_jack_detect_handler(struct work_struct *work)
236 {
237 struct rt711_priv *rt711 =
238 container_of(work, struct rt711_priv, jack_detect_work.work);
239 int btn_type = 0, ret;
240 unsigned int jack_status = 0, reg;
241
242 if (!rt711->hs_jack)
243 return;
244
245 if (!rt711->component->card || !rt711->component->card->instantiated)
246 return;
247
248 if (pm_runtime_status_suspended(rt711->slave->dev.parent)) {
249 dev_dbg(&rt711->slave->dev,
250 "%s: parent device is pm_runtime_status_suspended, skipping jack detection\n",
251 __func__);
252 return;
253 }
254
255 reg = RT711_VERB_GET_PIN_SENSE | RT711_HP_OUT;
256 ret = regmap_read(rt711->regmap, reg, &jack_status);
257 if (ret < 0)
258 goto io_error;
259
260 /* pin attached */
261 if (jack_status & (1 << 31)) {
262 /* jack in */
263 if (rt711->jack_type == 0) {
264 ret = rt711_headset_detect(rt711);
265 if (ret < 0)
266 return;
267 if (rt711->jack_type == SND_JACK_HEADSET)
268 btn_type = rt711_button_detect(rt711);
269 } else if (rt711->jack_type == SND_JACK_HEADSET) {
270 /* jack is already in, report button event */
271 btn_type = rt711_button_detect(rt711);
272 }
273 } else {
274 /* jack out */
275 rt711->jack_type = 0;
276 }
277
278 dev_dbg(&rt711->slave->dev,
279 "in %s, jack_type=0x%x\n", __func__, rt711->jack_type);
280 dev_dbg(&rt711->slave->dev,
281 "in %s, btn_type=0x%x\n", __func__, btn_type);
282
283 snd_soc_jack_report(rt711->hs_jack, rt711->jack_type | btn_type,
284 SND_JACK_HEADSET |
285 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
286 SND_JACK_BTN_2 | SND_JACK_BTN_3);
287
288 if (btn_type) {
289 /* button released */
290 snd_soc_jack_report(rt711->hs_jack, rt711->jack_type,
291 SND_JACK_HEADSET |
292 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
293 SND_JACK_BTN_2 | SND_JACK_BTN_3);
294
295 mod_delayed_work(system_power_efficient_wq,
296 &rt711->jack_btn_check_work, msecs_to_jiffies(200));
297 }
298
299 return;
300
301 io_error:
302 pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
303 }
304
rt711_btn_check_handler(struct work_struct * work)305 static void rt711_btn_check_handler(struct work_struct *work)
306 {
307 struct rt711_priv *rt711 = container_of(work, struct rt711_priv,
308 jack_btn_check_work.work);
309 int btn_type = 0, ret;
310 unsigned int jack_status = 0, reg;
311
312 reg = RT711_VERB_GET_PIN_SENSE | RT711_HP_OUT;
313 ret = regmap_read(rt711->regmap, reg, &jack_status);
314 if (ret < 0)
315 goto io_error;
316
317 /* pin attached */
318 if (jack_status & (1 << 31)) {
319 if (rt711->jack_type == SND_JACK_HEADSET) {
320 /* jack is already in, report button event */
321 btn_type = rt711_button_detect(rt711);
322 }
323 } else {
324 rt711->jack_type = 0;
325 }
326
327 /* cbj comparator */
328 ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG,
329 RT711_COMBO_JACK_AUTO_CTL2, ®);
330 if (ret < 0)
331 goto io_error;
332
333 if ((reg & 0xf0) == 0xf0)
334 btn_type = 0;
335
336 dev_dbg(&rt711->slave->dev,
337 "%s, btn_type=0x%x\n", __func__, btn_type);
338 snd_soc_jack_report(rt711->hs_jack, rt711->jack_type | btn_type,
339 SND_JACK_HEADSET |
340 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
341 SND_JACK_BTN_2 | SND_JACK_BTN_3);
342
343 if (btn_type) {
344 /* button released */
345 snd_soc_jack_report(rt711->hs_jack, rt711->jack_type,
346 SND_JACK_HEADSET |
347 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
348 SND_JACK_BTN_2 | SND_JACK_BTN_3);
349
350 mod_delayed_work(system_power_efficient_wq,
351 &rt711->jack_btn_check_work, msecs_to_jiffies(200));
352 }
353
354 return;
355
356 io_error:
357 pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
358 }
359
rt711_jack_init(struct rt711_priv * rt711)360 static void rt711_jack_init(struct rt711_priv *rt711)
361 {
362 struct snd_soc_dapm_context *dapm =
363 snd_soc_component_get_dapm(rt711->component);
364
365 mutex_lock(&rt711->calibrate_mutex);
366 /* power on */
367 if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
368 regmap_write(rt711->regmap,
369 RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
370
371 if (rt711->hs_jack) {
372 /* unsolicited response & IRQ control */
373 regmap_write(rt711->regmap,
374 RT711_SET_MIC2_UNSOLICITED_ENABLE, 0x82);
375 regmap_write(rt711->regmap,
376 RT711_SET_HP_UNSOLICITED_ENABLE, 0x81);
377 regmap_write(rt711->regmap,
378 RT711_SET_INLINE_UNSOLICITED_ENABLE, 0x83);
379 rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
380 0x10, 0x2420);
381 rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
382 0x19, 0x2e11);
383
384 switch (rt711->jd_src) {
385 case RT711_JD1:
386 /* default settings was already for JD1 */
387 break;
388 case RT711_JD2:
389 rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
390 RT711_JD_CTL2, RT711_JD2_2PORT_200K_DECODE_HP |
391 RT711_HP_JD_SEL_JD2,
392 RT711_JD2_2PORT_200K_DECODE_HP |
393 RT711_HP_JD_SEL_JD2);
394 rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
395 RT711_CC_DET1,
396 RT711_HP_JD_FINAL_RESULT_CTL_JD12,
397 RT711_HP_JD_FINAL_RESULT_CTL_JD12);
398 break;
399 case RT711_JD2_100K:
400 rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
401 RT711_JD_CTL2, RT711_JD2_2PORT_100K_DECODE | RT711_JD2_1PORT_TYPE_DECODE |
402 RT711_HP_JD_SEL_JD2 | RT711_JD1_2PORT_TYPE_100K_DECODE,
403 RT711_JD2_2PORT_100K_DECODE_HP | RT711_JD2_1PORT_JD_HP |
404 RT711_HP_JD_SEL_JD2 | RT711_JD1_2PORT_JD_RESERVED);
405 rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
406 RT711_CC_DET1,
407 RT711_HP_JD_FINAL_RESULT_CTL_JD12,
408 RT711_HP_JD_FINAL_RESULT_CTL_JD12);
409 break;
410 case RT711_JD2_1P8V_1PORT:
411 rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
412 RT711_JD_CTL1, RT711_JD2_DIGITAL_JD_MODE_SEL,
413 RT711_JD2_1_JD_MODE);
414 rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
415 RT711_JD_CTL2, RT711_JD2_1PORT_TYPE_DECODE |
416 RT711_HP_JD_SEL_JD2,
417 RT711_JD2_1PORT_JD_HP |
418 RT711_HP_JD_SEL_JD2);
419 rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
420 RT711_JD_CTL4, RT711_JD2_PAD_PULL_UP_MASK |
421 RT711_JD2_MODE_SEL_MASK,
422 RT711_JD2_PAD_PULL_UP |
423 RT711_JD2_MODE2_1P8V_1PORT);
424 rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
425 RT711_CC_DET1,
426 RT711_HP_JD_FINAL_RESULT_CTL_JD12,
427 RT711_HP_JD_FINAL_RESULT_CTL_JD12);
428 break;
429 default:
430 dev_warn(rt711->component->dev, "Wrong JD source\n");
431 break;
432 }
433
434 dev_dbg(&rt711->slave->dev, "in %s enable\n", __func__);
435
436 mod_delayed_work(system_power_efficient_wq,
437 &rt711->jack_detect_work, msecs_to_jiffies(250));
438 } else {
439 regmap_write(rt711->regmap,
440 RT711_SET_MIC2_UNSOLICITED_ENABLE, 0x00);
441 regmap_write(rt711->regmap,
442 RT711_SET_HP_UNSOLICITED_ENABLE, 0x00);
443 regmap_write(rt711->regmap,
444 RT711_SET_INLINE_UNSOLICITED_ENABLE, 0x00);
445
446 dev_dbg(&rt711->slave->dev, "in %s disable\n", __func__);
447 }
448
449 /* power off */
450 if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
451 regmap_write(rt711->regmap,
452 RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
453 mutex_unlock(&rt711->calibrate_mutex);
454 }
455
rt711_set_jack_detect(struct snd_soc_component * component,struct snd_soc_jack * hs_jack,void * data)456 static int rt711_set_jack_detect(struct snd_soc_component *component,
457 struct snd_soc_jack *hs_jack, void *data)
458 {
459 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
460 int ret;
461
462 rt711->hs_jack = hs_jack;
463
464 ret = pm_runtime_resume_and_get(component->dev);
465 if (ret < 0) {
466 if (ret != -EACCES) {
467 dev_err(component->dev, "%s: failed to resume %d\n", __func__, ret);
468 return ret;
469 }
470
471 /* pm_runtime not enabled yet */
472 dev_dbg(component->dev, "%s: skipping jack init for now\n", __func__);
473 return 0;
474 }
475
476 rt711_jack_init(rt711);
477
478 pm_runtime_mark_last_busy(component->dev);
479 pm_runtime_put_autosuspend(component->dev);
480
481 return 0;
482 }
483
rt711_get_gain(struct rt711_priv * rt711,unsigned int addr_h,unsigned int addr_l,unsigned int val_h,unsigned int * r_val,unsigned int * l_val)484 static void rt711_get_gain(struct rt711_priv *rt711, unsigned int addr_h,
485 unsigned int addr_l, unsigned int val_h,
486 unsigned int *r_val, unsigned int *l_val)
487 {
488 /* R Channel */
489 *r_val = (val_h << 8);
490 regmap_read(rt711->regmap, addr_l, r_val);
491
492 /* L Channel */
493 val_h |= 0x20;
494 *l_val = (val_h << 8);
495 regmap_read(rt711->regmap, addr_h, l_val);
496 }
497
498 /* For Verb-Set Amplifier Gain (Verb ID = 3h) */
rt711_set_amp_gain_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)499 static int rt711_set_amp_gain_put(struct snd_kcontrol *kcontrol,
500 struct snd_ctl_elem_value *ucontrol)
501 {
502 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
503 struct snd_soc_dapm_context *dapm =
504 snd_soc_component_get_dapm(component);
505 struct soc_mixer_control *mc =
506 (struct soc_mixer_control *)kcontrol->private_value;
507 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
508 unsigned int addr_h, addr_l, val_h, val_ll, val_lr;
509 unsigned int read_ll, read_rl;
510 int i;
511
512 mutex_lock(&rt711->calibrate_mutex);
513
514 /* Can't use update bit function, so read the original value first */
515 addr_h = mc->reg;
516 addr_l = mc->rreg;
517 if (mc->shift == RT711_DIR_OUT_SFT) /* output */
518 val_h = 0x80;
519 else /* input */
520 val_h = 0x0;
521
522 rt711_get_gain(rt711, addr_h, addr_l, val_h, &read_rl, &read_ll);
523
524 /* L Channel */
525 if (mc->invert) {
526 /* for mute/unmute */
527 val_ll = (mc->max - ucontrol->value.integer.value[0])
528 << RT711_MUTE_SFT;
529 /* keep gain */
530 read_ll = read_ll & 0x7f;
531 val_ll |= read_ll;
532 } else {
533 /* for gain */
534 val_ll = ((ucontrol->value.integer.value[0]) & 0x7f);
535 if (val_ll > mc->max)
536 val_ll = mc->max;
537 /* keep mute status */
538 read_ll = read_ll & (1 << RT711_MUTE_SFT);
539 val_ll |= read_ll;
540 }
541
542 if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
543 regmap_write(rt711->regmap,
544 RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
545
546 /* R Channel */
547 if (mc->invert) {
548 /* for mute/unmute */
549 val_lr = (mc->max - ucontrol->value.integer.value[1])
550 << RT711_MUTE_SFT;
551 /* keep gain */
552 read_rl = read_rl & 0x7f;
553 val_lr |= read_rl;
554 } else {
555 /* for gain */
556 val_lr = ((ucontrol->value.integer.value[1]) & 0x7f);
557 if (val_lr > mc->max)
558 val_lr = mc->max;
559 /* keep mute status */
560 read_rl = read_rl & (1 << RT711_MUTE_SFT);
561 val_lr |= read_rl;
562 }
563
564 for (i = 0; i < 3; i++) { /* retry 3 times at most */
565
566 if (val_ll == val_lr) {
567 /* Set both L/R channels at the same time */
568 val_h = (1 << mc->shift) | (3 << 4);
569 regmap_write(rt711->regmap,
570 addr_h, (val_h << 8 | val_ll));
571 regmap_write(rt711->regmap,
572 addr_l, (val_h << 8 | val_ll));
573 } else {
574 /* Lch*/
575 val_h = (1 << mc->shift) | (1 << 5);
576 regmap_write(rt711->regmap,
577 addr_h, (val_h << 8 | val_ll));
578
579 /* Rch */
580 val_h = (1 << mc->shift) | (1 << 4);
581 regmap_write(rt711->regmap,
582 addr_l, (val_h << 8 | val_lr));
583 }
584 /* check result */
585 if (mc->shift == RT711_DIR_OUT_SFT) /* output */
586 val_h = 0x80;
587 else /* input */
588 val_h = 0x0;
589
590 rt711_get_gain(rt711, addr_h, addr_l, val_h,
591 &read_rl, &read_ll);
592 if (read_rl == val_lr && read_ll == val_ll)
593 break;
594 }
595
596 if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
597 regmap_write(rt711->regmap,
598 RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
599
600 mutex_unlock(&rt711->calibrate_mutex);
601 return 0;
602 }
603
rt711_set_amp_gain_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)604 static int rt711_set_amp_gain_get(struct snd_kcontrol *kcontrol,
605 struct snd_ctl_elem_value *ucontrol)
606 {
607 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
608 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
609 struct soc_mixer_control *mc =
610 (struct soc_mixer_control *)kcontrol->private_value;
611 unsigned int addr_h, addr_l, val_h;
612 unsigned int read_ll, read_rl;
613
614 /* switch to get command */
615 addr_h = mc->reg;
616 addr_l = mc->rreg;
617 if (mc->shift == RT711_DIR_OUT_SFT) /* output */
618 val_h = 0x80;
619 else /* input */
620 val_h = 0x0;
621
622 rt711_get_gain(rt711, addr_h, addr_l, val_h, &read_rl, &read_ll);
623
624 if (mc->invert) {
625 /* mute/unmute for switch controls */
626 read_ll = !((read_ll & 0x80) >> RT711_MUTE_SFT);
627 read_rl = !((read_rl & 0x80) >> RT711_MUTE_SFT);
628 } else {
629 /* for gain volume controls */
630 read_ll = read_ll & 0x7f;
631 read_rl = read_rl & 0x7f;
632 }
633 ucontrol->value.integer.value[0] = read_ll;
634 ucontrol->value.integer.value[1] = read_rl;
635
636 return 0;
637 }
638
639 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6525, 75, 0);
640 static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -1725, 75, 0);
641 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, 0, 1000, 0);
642
643 static const struct snd_kcontrol_new rt711_snd_controls[] = {
644 SOC_DOUBLE_R_EXT_TLV("DAC Surr Playback Volume",
645 RT711_SET_GAIN_DAC2_H, RT711_SET_GAIN_DAC2_L,
646 RT711_DIR_OUT_SFT, 0x57, 0,
647 rt711_set_amp_gain_get, rt711_set_amp_gain_put, out_vol_tlv),
648 SOC_DOUBLE_R_EXT("ADC 08 Capture Switch",
649 RT711_SET_GAIN_ADC2_H, RT711_SET_GAIN_ADC2_L,
650 RT711_DIR_IN_SFT, 1, 1,
651 rt711_set_amp_gain_get, rt711_set_amp_gain_put),
652 SOC_DOUBLE_R_EXT("ADC 09 Capture Switch",
653 RT711_SET_GAIN_ADC1_H, RT711_SET_GAIN_ADC1_L,
654 RT711_DIR_IN_SFT, 1, 1,
655 rt711_set_amp_gain_get, rt711_set_amp_gain_put),
656 SOC_DOUBLE_R_EXT_TLV("ADC 08 Capture Volume",
657 RT711_SET_GAIN_ADC2_H, RT711_SET_GAIN_ADC2_L,
658 RT711_DIR_IN_SFT, 0x3f, 0,
659 rt711_set_amp_gain_get, rt711_set_amp_gain_put, in_vol_tlv),
660 SOC_DOUBLE_R_EXT_TLV("ADC 09 Capture Volume",
661 RT711_SET_GAIN_ADC1_H, RT711_SET_GAIN_ADC1_L,
662 RT711_DIR_IN_SFT, 0x3f, 0,
663 rt711_set_amp_gain_get, rt711_set_amp_gain_put, in_vol_tlv),
664 SOC_DOUBLE_R_EXT_TLV("AMIC Volume",
665 RT711_SET_GAIN_AMIC_H, RT711_SET_GAIN_AMIC_L,
666 RT711_DIR_IN_SFT, 3, 0,
667 rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv),
668 SOC_DOUBLE_R_EXT_TLV("DMIC1 Volume",
669 RT711_SET_GAIN_DMIC1_H, RT711_SET_GAIN_DMIC1_L,
670 RT711_DIR_IN_SFT, 3, 0,
671 rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv),
672 SOC_DOUBLE_R_EXT_TLV("DMIC2 Volume",
673 RT711_SET_GAIN_DMIC2_H, RT711_SET_GAIN_DMIC2_L,
674 RT711_DIR_IN_SFT, 3, 0,
675 rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv),
676 };
677
rt711_mux_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)678 static int rt711_mux_get(struct snd_kcontrol *kcontrol,
679 struct snd_ctl_elem_value *ucontrol)
680 {
681 struct snd_soc_component *component =
682 snd_soc_dapm_kcontrol_component(kcontrol);
683 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
684 unsigned int reg, val = 0, nid;
685 int ret;
686
687 if (strstr(ucontrol->id.name, "ADC 22 Mux"))
688 nid = RT711_MIXER_IN1;
689 else if (strstr(ucontrol->id.name, "ADC 23 Mux"))
690 nid = RT711_MIXER_IN2;
691 else
692 return -EINVAL;
693
694 /* vid = 0xf01 */
695 reg = RT711_VERB_SET_CONNECT_SEL | nid;
696 ret = regmap_read(rt711->regmap, reg, &val);
697 if (ret < 0) {
698 dev_err(component->dev, "%s: sdw read failed: %d\n",
699 __func__, ret);
700 return ret;
701 }
702
703 ucontrol->value.enumerated.item[0] = val;
704
705 return 0;
706 }
707
rt711_mux_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)708 static int rt711_mux_put(struct snd_kcontrol *kcontrol,
709 struct snd_ctl_elem_value *ucontrol)
710 {
711 struct snd_soc_component *component =
712 snd_soc_dapm_kcontrol_component(kcontrol);
713 struct snd_soc_dapm_context *dapm =
714 snd_soc_dapm_kcontrol_dapm(kcontrol);
715 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
716 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
717 unsigned int *item = ucontrol->value.enumerated.item;
718 unsigned int val, val2 = 0, change, reg, nid;
719 int ret;
720
721 if (item[0] >= e->items)
722 return -EINVAL;
723
724 if (strstr(ucontrol->id.name, "ADC 22 Mux"))
725 nid = RT711_MIXER_IN1;
726 else if (strstr(ucontrol->id.name, "ADC 23 Mux"))
727 nid = RT711_MIXER_IN2;
728 else
729 return -EINVAL;
730
731 /* Verb ID = 0x701h */
732 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
733
734 reg = RT711_VERB_SET_CONNECT_SEL | nid;
735 ret = regmap_read(rt711->regmap, reg, &val2);
736 if (ret < 0) {
737 dev_err(component->dev, "%s: sdw read failed: %d\n",
738 __func__, ret);
739 return ret;
740 }
741
742 if (val == val2)
743 change = 0;
744 else
745 change = 1;
746
747 if (change) {
748 reg = RT711_VERB_SET_CONNECT_SEL | nid;
749 regmap_write(rt711->regmap, reg, val);
750 }
751
752 snd_soc_dapm_mux_update_power(dapm, kcontrol,
753 item[0], e, NULL);
754
755 return change;
756 }
757
758 static const char * const adc_mux_text[] = {
759 "MIC2",
760 "LINE1",
761 "LINE2",
762 "DMIC",
763 };
764
765 static SOC_ENUM_SINGLE_DECL(
766 rt711_adc22_enum, SND_SOC_NOPM, 0, adc_mux_text);
767
768 static SOC_ENUM_SINGLE_DECL(
769 rt711_adc23_enum, SND_SOC_NOPM, 0, adc_mux_text);
770
771 static const struct snd_kcontrol_new rt711_adc22_mux =
772 SOC_DAPM_ENUM_EXT("ADC 22 Mux", rt711_adc22_enum,
773 rt711_mux_get, rt711_mux_put);
774
775 static const struct snd_kcontrol_new rt711_adc23_mux =
776 SOC_DAPM_ENUM_EXT("ADC 23 Mux", rt711_adc23_enum,
777 rt711_mux_get, rt711_mux_put);
778
rt711_dac_surround_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)779 static int rt711_dac_surround_event(struct snd_soc_dapm_widget *w,
780 struct snd_kcontrol *kcontrol, int event)
781 {
782 struct snd_soc_component *component =
783 snd_soc_dapm_to_component(w->dapm);
784 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
785 unsigned int val_h = (1 << RT711_DIR_OUT_SFT) | (0x3 << 4);
786 unsigned int val_l;
787
788 switch (event) {
789 case SND_SOC_DAPM_POST_PMU:
790 regmap_write(rt711->regmap,
791 RT711_SET_STREAMID_DAC2, 0x10);
792
793 val_l = 0x00;
794 regmap_write(rt711->regmap,
795 RT711_SET_GAIN_HP_H, (val_h << 8 | val_l));
796 break;
797 case SND_SOC_DAPM_PRE_PMD:
798 val_l = (1 << RT711_MUTE_SFT);
799 regmap_write(rt711->regmap,
800 RT711_SET_GAIN_HP_H, (val_h << 8 | val_l));
801 usleep_range(50000, 55000);
802
803 regmap_write(rt711->regmap,
804 RT711_SET_STREAMID_DAC2, 0x00);
805 break;
806 }
807 return 0;
808 }
809
rt711_adc_09_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)810 static int rt711_adc_09_event(struct snd_soc_dapm_widget *w,
811 struct snd_kcontrol *kcontrol, int event)
812 {
813 struct snd_soc_component *component =
814 snd_soc_dapm_to_component(w->dapm);
815 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
816
817 switch (event) {
818 case SND_SOC_DAPM_POST_PMU:
819 regmap_write(rt711->regmap,
820 RT711_SET_STREAMID_ADC1, 0x10);
821 break;
822 case SND_SOC_DAPM_PRE_PMD:
823 regmap_write(rt711->regmap,
824 RT711_SET_STREAMID_ADC1, 0x00);
825 break;
826 }
827 return 0;
828 }
829
rt711_adc_08_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)830 static int rt711_adc_08_event(struct snd_soc_dapm_widget *w,
831 struct snd_kcontrol *kcontrol, int event)
832 {
833 struct snd_soc_component *component =
834 snd_soc_dapm_to_component(w->dapm);
835 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
836
837 switch (event) {
838 case SND_SOC_DAPM_POST_PMU:
839 regmap_write(rt711->regmap,
840 RT711_SET_STREAMID_ADC2, 0x10);
841 break;
842 case SND_SOC_DAPM_PRE_PMD:
843 regmap_write(rt711->regmap,
844 RT711_SET_STREAMID_ADC2, 0x00);
845 break;
846 }
847 return 0;
848 }
849
850 static const struct snd_soc_dapm_widget rt711_dapm_widgets[] = {
851 SND_SOC_DAPM_OUTPUT("HP"),
852 SND_SOC_DAPM_INPUT("MIC2"),
853 SND_SOC_DAPM_INPUT("DMIC1"),
854 SND_SOC_DAPM_INPUT("DMIC2"),
855 SND_SOC_DAPM_INPUT("LINE1"),
856 SND_SOC_DAPM_INPUT("LINE2"),
857
858 SND_SOC_DAPM_DAC_E("DAC Surround", NULL, SND_SOC_NOPM, 0, 0,
859 rt711_dac_surround_event,
860 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
861 SND_SOC_DAPM_ADC_E("ADC 09", NULL, SND_SOC_NOPM, 0, 0,
862 rt711_adc_09_event,
863 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
864 SND_SOC_DAPM_ADC_E("ADC 08", NULL, SND_SOC_NOPM, 0, 0,
865 rt711_adc_08_event,
866 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
867 SND_SOC_DAPM_MUX("ADC 22 Mux", SND_SOC_NOPM, 0, 0,
868 &rt711_adc22_mux),
869 SND_SOC_DAPM_MUX("ADC 23 Mux", SND_SOC_NOPM, 0, 0,
870 &rt711_adc23_mux),
871
872 SND_SOC_DAPM_AIF_IN("DP3RX", "DP3 Playback", 0, SND_SOC_NOPM, 0, 0),
873 SND_SOC_DAPM_AIF_OUT("DP2TX", "DP2 Capture", 0, SND_SOC_NOPM, 0, 0),
874 SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM, 0, 0),
875 };
876
877 static const struct snd_soc_dapm_route rt711_audio_map[] = {
878 {"DAC Surround", NULL, "DP3RX"},
879 {"DP2TX", NULL, "ADC 09"},
880 {"DP4TX", NULL, "ADC 08"},
881
882 {"ADC 09", NULL, "ADC 22 Mux"},
883 {"ADC 08", NULL, "ADC 23 Mux"},
884 {"ADC 22 Mux", "DMIC", "DMIC1"},
885 {"ADC 22 Mux", "LINE1", "LINE1"},
886 {"ADC 22 Mux", "LINE2", "LINE2"},
887 {"ADC 22 Mux", "MIC2", "MIC2"},
888 {"ADC 23 Mux", "DMIC", "DMIC2"},
889 {"ADC 23 Mux", "LINE1", "LINE1"},
890 {"ADC 23 Mux", "LINE2", "LINE2"},
891 {"ADC 23 Mux", "MIC2", "MIC2"},
892
893 {"HP", NULL, "DAC Surround"},
894 };
895
rt711_set_bias_level(struct snd_soc_component * component,enum snd_soc_bias_level level)896 static int rt711_set_bias_level(struct snd_soc_component *component,
897 enum snd_soc_bias_level level)
898 {
899 struct snd_soc_dapm_context *dapm =
900 snd_soc_component_get_dapm(component);
901 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
902
903 switch (level) {
904 case SND_SOC_BIAS_PREPARE:
905 if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
906 regmap_write(rt711->regmap,
907 RT711_SET_AUDIO_POWER_STATE,
908 AC_PWRST_D0);
909 }
910 break;
911
912 case SND_SOC_BIAS_STANDBY:
913 mutex_lock(&rt711->calibrate_mutex);
914 regmap_write(rt711->regmap,
915 RT711_SET_AUDIO_POWER_STATE,
916 AC_PWRST_D3);
917 mutex_unlock(&rt711->calibrate_mutex);
918 break;
919
920 default:
921 break;
922 }
923
924 return 0;
925 }
926
rt711_parse_dt(struct rt711_priv * rt711,struct device * dev)927 static int rt711_parse_dt(struct rt711_priv *rt711, struct device *dev)
928 {
929 device_property_read_u32(dev, "realtek,jd-src",
930 &rt711->jd_src);
931
932 return 0;
933 }
934
rt711_probe(struct snd_soc_component * component)935 static int rt711_probe(struct snd_soc_component *component)
936 {
937 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
938 int ret;
939
940 rt711_parse_dt(rt711, &rt711->slave->dev);
941 rt711->component = component;
942
943 ret = pm_runtime_resume(component->dev);
944 if (ret < 0 && ret != -EACCES)
945 return ret;
946
947 return 0;
948 }
949
950 static const struct snd_soc_component_driver soc_codec_dev_rt711 = {
951 .probe = rt711_probe,
952 .set_bias_level = rt711_set_bias_level,
953 .controls = rt711_snd_controls,
954 .num_controls = ARRAY_SIZE(rt711_snd_controls),
955 .dapm_widgets = rt711_dapm_widgets,
956 .num_dapm_widgets = ARRAY_SIZE(rt711_dapm_widgets),
957 .dapm_routes = rt711_audio_map,
958 .num_dapm_routes = ARRAY_SIZE(rt711_audio_map),
959 .set_jack = rt711_set_jack_detect,
960 .endianness = 1,
961 };
962
rt711_set_sdw_stream(struct snd_soc_dai * dai,void * sdw_stream,int direction)963 static int rt711_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
964 int direction)
965 {
966 struct sdw_stream_data *stream;
967
968 if (!sdw_stream)
969 return 0;
970
971 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
972 if (!stream)
973 return -ENOMEM;
974
975 stream->sdw_stream = sdw_stream;
976
977 /* Use tx_mask or rx_mask to configure stream tag and set dma_data */
978 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
979 dai->playback_dma_data = stream;
980 else
981 dai->capture_dma_data = stream;
982
983 return 0;
984 }
985
rt711_shutdown(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)986 static void rt711_shutdown(struct snd_pcm_substream *substream,
987 struct snd_soc_dai *dai)
988 {
989 struct sdw_stream_data *stream;
990
991 stream = snd_soc_dai_get_dma_data(dai, substream);
992 snd_soc_dai_set_dma_data(dai, substream, NULL);
993 kfree(stream);
994 }
995
rt711_pcm_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)996 static int rt711_pcm_hw_params(struct snd_pcm_substream *substream,
997 struct snd_pcm_hw_params *params,
998 struct snd_soc_dai *dai)
999 {
1000 struct snd_soc_component *component = dai->component;
1001 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
1002 struct sdw_stream_config stream_config;
1003 struct sdw_port_config port_config;
1004 enum sdw_data_direction direction;
1005 struct sdw_stream_data *stream;
1006 int retval, port, num_channels;
1007 unsigned int val = 0;
1008
1009 dev_dbg(dai->dev, "%s %s", __func__, dai->name);
1010 stream = snd_soc_dai_get_dma_data(dai, substream);
1011
1012 if (!stream)
1013 return -EINVAL;
1014
1015 if (!rt711->slave)
1016 return -EINVAL;
1017
1018 /* SoundWire specific configuration */
1019 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1020 direction = SDW_DATA_DIR_RX;
1021 port = 3;
1022 } else {
1023 direction = SDW_DATA_DIR_TX;
1024 if (dai->id == RT711_AIF1)
1025 port = 4;
1026 else if (dai->id == RT711_AIF2)
1027 port = 2;
1028 else
1029 return -EINVAL;
1030 }
1031
1032 stream_config.frame_rate = params_rate(params);
1033 stream_config.ch_count = params_channels(params);
1034 stream_config.bps = snd_pcm_format_width(params_format(params));
1035 stream_config.direction = direction;
1036
1037 num_channels = params_channels(params);
1038 port_config.ch_mask = (1 << (num_channels)) - 1;
1039 port_config.num = port;
1040
1041 retval = sdw_stream_add_slave(rt711->slave, &stream_config,
1042 &port_config, 1, stream->sdw_stream);
1043 if (retval) {
1044 dev_err(dai->dev, "Unable to configure port\n");
1045 return retval;
1046 }
1047
1048 if (params_channels(params) <= 16) {
1049 /* bit 3:0 Number of Channel */
1050 val |= (params_channels(params) - 1);
1051 } else {
1052 dev_err(component->dev, "Unsupported channels %d\n",
1053 params_channels(params));
1054 return -EINVAL;
1055 }
1056
1057 switch (params_width(params)) {
1058 /* bit 6:4 Bits per Sample */
1059 case 8:
1060 break;
1061 case 16:
1062 val |= (0x1 << 4);
1063 break;
1064 case 20:
1065 val |= (0x2 << 4);
1066 break;
1067 case 24:
1068 val |= (0x3 << 4);
1069 break;
1070 case 32:
1071 val |= (0x4 << 4);
1072 break;
1073 default:
1074 return -EINVAL;
1075 }
1076
1077 /* 48Khz */
1078 regmap_write(rt711->regmap, RT711_DAC_FORMAT_H, val);
1079 regmap_write(rt711->regmap, RT711_ADC1_FORMAT_H, val);
1080 regmap_write(rt711->regmap, RT711_ADC2_FORMAT_H, val);
1081
1082 return retval;
1083 }
1084
rt711_pcm_hw_free(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)1085 static int rt711_pcm_hw_free(struct snd_pcm_substream *substream,
1086 struct snd_soc_dai *dai)
1087 {
1088 struct snd_soc_component *component = dai->component;
1089 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
1090 struct sdw_stream_data *stream =
1091 snd_soc_dai_get_dma_data(dai, substream);
1092
1093 if (!rt711->slave)
1094 return -EINVAL;
1095
1096 sdw_stream_remove_slave(rt711->slave, stream->sdw_stream);
1097 return 0;
1098 }
1099
1100 #define RT711_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
1101 #define RT711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1102 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
1103
1104 static const struct snd_soc_dai_ops rt711_ops = {
1105 .hw_params = rt711_pcm_hw_params,
1106 .hw_free = rt711_pcm_hw_free,
1107 .set_stream = rt711_set_sdw_stream,
1108 .shutdown = rt711_shutdown,
1109 };
1110
1111 static struct snd_soc_dai_driver rt711_dai[] = {
1112 {
1113 .name = "rt711-aif1",
1114 .id = RT711_AIF1,
1115 .playback = {
1116 .stream_name = "DP3 Playback",
1117 .channels_min = 1,
1118 .channels_max = 2,
1119 .rates = RT711_STEREO_RATES,
1120 .formats = RT711_FORMATS,
1121 },
1122 .capture = {
1123 .stream_name = "DP4 Capture",
1124 .channels_min = 1,
1125 .channels_max = 2,
1126 .rates = RT711_STEREO_RATES,
1127 .formats = RT711_FORMATS,
1128 },
1129 .ops = &rt711_ops,
1130 },
1131 {
1132 .name = "rt711-aif2",
1133 .id = RT711_AIF2,
1134 .capture = {
1135 .stream_name = "DP2 Capture",
1136 .channels_min = 1,
1137 .channels_max = 2,
1138 .rates = RT711_STEREO_RATES,
1139 .formats = RT711_FORMATS,
1140 },
1141 .ops = &rt711_ops,
1142 }
1143 };
1144
1145 /* Bus clock frequency */
1146 #define RT711_CLK_FREQ_9600000HZ 9600000
1147 #define RT711_CLK_FREQ_12000000HZ 12000000
1148 #define RT711_CLK_FREQ_6000000HZ 6000000
1149 #define RT711_CLK_FREQ_4800000HZ 4800000
1150 #define RT711_CLK_FREQ_2400000HZ 2400000
1151 #define RT711_CLK_FREQ_12288000HZ 12288000
1152
rt711_clock_config(struct device * dev)1153 int rt711_clock_config(struct device *dev)
1154 {
1155 struct rt711_priv *rt711 = dev_get_drvdata(dev);
1156 unsigned int clk_freq, value;
1157
1158 clk_freq = (rt711->params.curr_dr_freq >> 1);
1159
1160 switch (clk_freq) {
1161 case RT711_CLK_FREQ_12000000HZ:
1162 value = 0x0;
1163 break;
1164 case RT711_CLK_FREQ_6000000HZ:
1165 value = 0x1;
1166 break;
1167 case RT711_CLK_FREQ_9600000HZ:
1168 value = 0x2;
1169 break;
1170 case RT711_CLK_FREQ_4800000HZ:
1171 value = 0x3;
1172 break;
1173 case RT711_CLK_FREQ_2400000HZ:
1174 value = 0x4;
1175 break;
1176 case RT711_CLK_FREQ_12288000HZ:
1177 value = 0x5;
1178 break;
1179 default:
1180 return -EINVAL;
1181 }
1182
1183 regmap_write(rt711->regmap, 0xe0, value);
1184 regmap_write(rt711->regmap, 0xf0, value);
1185
1186 dev_dbg(dev, "%s complete, clk_freq=%d\n", __func__, clk_freq);
1187
1188 return 0;
1189 }
1190
rt711_calibration_work(struct work_struct * work)1191 static void rt711_calibration_work(struct work_struct *work)
1192 {
1193 struct rt711_priv *rt711 =
1194 container_of(work, struct rt711_priv, calibration_work);
1195
1196 rt711_calibration(rt711);
1197 }
1198
rt711_init(struct device * dev,struct regmap * sdw_regmap,struct regmap * regmap,struct sdw_slave * slave)1199 int rt711_init(struct device *dev, struct regmap *sdw_regmap,
1200 struct regmap *regmap, struct sdw_slave *slave)
1201 {
1202 struct rt711_priv *rt711;
1203 int ret;
1204
1205 rt711 = devm_kzalloc(dev, sizeof(*rt711), GFP_KERNEL);
1206 if (!rt711)
1207 return -ENOMEM;
1208
1209 dev_set_drvdata(dev, rt711);
1210 rt711->slave = slave;
1211 rt711->sdw_regmap = sdw_regmap;
1212 rt711->regmap = regmap;
1213
1214 mutex_init(&rt711->calibrate_mutex);
1215 mutex_init(&rt711->disable_irq_lock);
1216
1217 INIT_DELAYED_WORK(&rt711->jack_detect_work, rt711_jack_detect_handler);
1218 INIT_DELAYED_WORK(&rt711->jack_btn_check_work, rt711_btn_check_handler);
1219 INIT_WORK(&rt711->calibration_work, rt711_calibration_work);
1220
1221 /*
1222 * Mark hw_init to false
1223 * HW init will be performed when device reports present
1224 */
1225 rt711->hw_init = false;
1226 rt711->first_hw_init = false;
1227
1228 /* JD source uses JD2 in default */
1229 rt711->jd_src = RT711_JD2;
1230
1231 ret = devm_snd_soc_register_component(dev,
1232 &soc_codec_dev_rt711,
1233 rt711_dai,
1234 ARRAY_SIZE(rt711_dai));
1235
1236 dev_dbg(&slave->dev, "%s\n", __func__);
1237
1238 return ret;
1239 }
1240
rt711_io_init(struct device * dev,struct sdw_slave * slave)1241 int rt711_io_init(struct device *dev, struct sdw_slave *slave)
1242 {
1243 struct rt711_priv *rt711 = dev_get_drvdata(dev);
1244
1245 rt711->disable_irq = false;
1246
1247 if (rt711->hw_init)
1248 return 0;
1249
1250 if (rt711->first_hw_init) {
1251 regcache_cache_only(rt711->regmap, false);
1252 regcache_cache_bypass(rt711->regmap, true);
1253 }
1254
1255 /*
1256 * PM runtime is only enabled when a Slave reports as Attached
1257 */
1258 if (!rt711->first_hw_init) {
1259 /* set autosuspend parameters */
1260 pm_runtime_set_autosuspend_delay(&slave->dev, 3000);
1261 pm_runtime_use_autosuspend(&slave->dev);
1262
1263 /* update count of parent 'active' children */
1264 pm_runtime_set_active(&slave->dev);
1265
1266 /* make sure the device does not suspend immediately */
1267 pm_runtime_mark_last_busy(&slave->dev);
1268
1269 pm_runtime_enable(&slave->dev);
1270 }
1271
1272 pm_runtime_get_noresume(&slave->dev);
1273
1274 rt711_reset(rt711->regmap);
1275
1276 /* power on */
1277 regmap_write(rt711->regmap, RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
1278
1279 /* Set Pin Widget */
1280 regmap_write(rt711->regmap, RT711_SET_PIN_MIC2, 0x25);
1281 regmap_write(rt711->regmap, RT711_SET_PIN_HP, 0xc0);
1282 regmap_write(rt711->regmap, RT711_SET_PIN_DMIC1, 0x20);
1283 regmap_write(rt711->regmap, RT711_SET_PIN_DMIC2, 0x20);
1284 regmap_write(rt711->regmap, RT711_SET_PIN_LINE1, 0x20);
1285 regmap_write(rt711->regmap, RT711_SET_PIN_LINE2, 0x20);
1286
1287 /* Mute HP/ADC1/ADC2 */
1288 regmap_write(rt711->regmap, RT711_SET_GAIN_HP_H, 0xa080);
1289 regmap_write(rt711->regmap, RT711_SET_GAIN_HP_H, 0x9080);
1290 regmap_write(rt711->regmap, RT711_SET_GAIN_ADC2_H, 0x6080);
1291 regmap_write(rt711->regmap, RT711_SET_GAIN_ADC2_H, 0x5080);
1292 regmap_write(rt711->regmap, RT711_SET_GAIN_ADC1_H, 0x6080);
1293 regmap_write(rt711->regmap, RT711_SET_GAIN_ADC1_H, 0x5080);
1294
1295 /* Set Configuration Default */
1296 regmap_write(rt711->regmap, 0x4f12, 0x91);
1297 regmap_write(rt711->regmap, 0x4e12, 0xd6);
1298 regmap_write(rt711->regmap, 0x4d12, 0x11);
1299 regmap_write(rt711->regmap, 0x4c12, 0x20);
1300 regmap_write(rt711->regmap, 0x4f13, 0x91);
1301 regmap_write(rt711->regmap, 0x4e13, 0xd6);
1302 regmap_write(rt711->regmap, 0x4d13, 0x11);
1303 regmap_write(rt711->regmap, 0x4c13, 0x21);
1304 regmap_write(rt711->regmap, 0x4c21, 0xf0);
1305 regmap_write(rt711->regmap, 0x4d21, 0x11);
1306 regmap_write(rt711->regmap, 0x4e21, 0x11);
1307 regmap_write(rt711->regmap, 0x4f21, 0x01);
1308
1309 /* Data port arrangement */
1310 rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
1311 RT711_TX_RX_MUX_CTL, 0x0154);
1312
1313 /* Set index */
1314 rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
1315 RT711_DIGITAL_MISC_CTRL4, 0x201b);
1316 rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
1317 RT711_COMBO_JACK_AUTO_CTL1, 0x5089);
1318 rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
1319 RT711_VREFOUT_CTL, 0x5064);
1320 rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
1321 RT711_INLINE_CMD_CTL, 0xd249);
1322
1323 /* Finish Initial Settings, set power to D3 */
1324 regmap_write(rt711->regmap, RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
1325
1326 if (rt711->first_hw_init)
1327 rt711_calibration(rt711);
1328 else
1329 schedule_work(&rt711->calibration_work);
1330
1331 /*
1332 * if set_jack callback occurred early than io_init,
1333 * we set up the jack detection function now
1334 */
1335 if (rt711->hs_jack)
1336 rt711_jack_init(rt711);
1337
1338 if (rt711->first_hw_init) {
1339 regcache_cache_bypass(rt711->regmap, false);
1340 regcache_mark_dirty(rt711->regmap);
1341 } else
1342 rt711->first_hw_init = true;
1343
1344 /* Mark Slave initialization complete */
1345 rt711->hw_init = true;
1346
1347 pm_runtime_mark_last_busy(&slave->dev);
1348 pm_runtime_put_autosuspend(&slave->dev);
1349
1350 dev_dbg(&slave->dev, "%s hw_init complete\n", __func__);
1351 return 0;
1352 }
1353
1354 MODULE_DESCRIPTION("ASoC RT711 SDW driver");
1355 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
1356 MODULE_LICENSE("GPL");
1357