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