2 * rt286.c -- RT286 ALSA SoC audio codec driver
4 * Copyright 2013 Realtek Semiconductor Corp.
5 * Author: Bard Liao <bardliao@realtek.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
17 #include <linux/i2c.h>
18 #include <linux/platform_device.h>
19 #include <linux/spi/spi.h>
20 #include <linux/dmi.h>
21 #include <linux/acpi.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dapm.h>
27 #include <sound/initval.h>
28 #include <sound/tlv.h>
29 #include <sound/jack.h>
30 #include <linux/workqueue.h>
31 #include <sound/rt286.h>
32 #include <sound/hda_verbs.h>
36 #define RT286_VENDOR_ID 0x10ec0286
39 struct regmap *regmap;
40 struct snd_soc_codec *codec;
41 struct rt286_platform_data pdata;
42 struct i2c_client *i2c;
43 struct snd_soc_jack *jack;
44 struct delayed_work jack_detect_work;
47 struct reg_default *index_cache;
50 static struct reg_default rt286_index_def[] = {
72 #define INDEX_CACHE_SIZE ARRAY_SIZE(rt286_index_def)
74 static const struct reg_default rt286_reg[] = {
75 { 0x00170500, 0x00000400 },
76 { 0x00220000, 0x00000031 },
77 { 0x00239000, 0x0000007f },
78 { 0x0023a000, 0x0000007f },
79 { 0x00270500, 0x00000400 },
80 { 0x00370500, 0x00000400 },
81 { 0x00870500, 0x00000400 },
82 { 0x00920000, 0x00000031 },
83 { 0x00935000, 0x000000c3 },
84 { 0x00936000, 0x000000c3 },
85 { 0x00970500, 0x00000400 },
86 { 0x00b37000, 0x00000097 },
87 { 0x00b37200, 0x00000097 },
88 { 0x00b37300, 0x00000097 },
89 { 0x00c37000, 0x00000000 },
90 { 0x00c37100, 0x00000080 },
91 { 0x01270500, 0x00000400 },
92 { 0x01370500, 0x00000400 },
93 { 0x01371f00, 0x411111f0 },
94 { 0x01439000, 0x00000080 },
95 { 0x0143a000, 0x00000080 },
96 { 0x01470700, 0x00000000 },
97 { 0x01470500, 0x00000400 },
98 { 0x01470c00, 0x00000000 },
99 { 0x01470100, 0x00000000 },
100 { 0x01837000, 0x00000000 },
101 { 0x01870500, 0x00000400 },
102 { 0x02050000, 0x00000000 },
103 { 0x02139000, 0x00000080 },
104 { 0x0213a000, 0x00000080 },
105 { 0x02170100, 0x00000000 },
106 { 0x02170500, 0x00000400 },
107 { 0x02170700, 0x00000000 },
108 { 0x02270100, 0x00000000 },
109 { 0x02370100, 0x00000000 },
110 { 0x01870700, 0x00000020 },
111 { 0x00830000, 0x000000c3 },
112 { 0x00930000, 0x000000c3 },
113 { 0x01270700, 0x00000000 },
116 static bool rt286_volatile_register(struct device *dev, unsigned int reg)
120 case RT286_GET_PARAM(AC_NODE_ROOT, AC_PAR_VENDOR_ID):
121 case RT286_GET_HP_SENSE:
122 case RT286_GET_MIC1_SENSE:
123 case RT286_PROC_COEF:
132 static bool rt286_readable_register(struct device *dev, unsigned int reg)
136 case RT286_GET_PARAM(AC_NODE_ROOT, AC_PAR_VENDOR_ID):
137 case RT286_GET_HP_SENSE:
138 case RT286_GET_MIC1_SENSE:
139 case RT286_SET_AUDIO_POWER:
140 case RT286_SET_HPO_POWER:
141 case RT286_SET_SPK_POWER:
142 case RT286_SET_DMIC1_POWER:
148 case RT286_SET_PIN_HPO:
149 case RT286_SET_PIN_SPK:
150 case RT286_SET_PIN_DMIC1:
152 case RT286_SET_AMP_GAIN_HPO:
153 case RT286_SET_DMIC2_DEFAULT:
154 case RT286_DACL_GAIN:
155 case RT286_DACR_GAIN:
156 case RT286_ADCL_GAIN:
157 case RT286_ADCR_GAIN:
159 case RT286_SPOL_GAIN:
160 case RT286_SPOR_GAIN:
161 case RT286_HPOL_GAIN:
162 case RT286_HPOR_GAIN:
163 case RT286_F_DAC_SWITCH:
164 case RT286_F_RECMIX_SWITCH:
165 case RT286_REC_MIC_SWITCH:
166 case RT286_REC_I2S_SWITCH:
167 case RT286_REC_LINE_SWITCH:
168 case RT286_REC_BEEP_SWITCH:
169 case RT286_DAC_FORMAT:
170 case RT286_ADC_FORMAT:
171 case RT286_COEF_INDEX:
172 case RT286_PROC_COEF:
173 case RT286_SET_AMP_GAIN_ADC_IN1:
174 case RT286_SET_AMP_GAIN_ADC_IN2:
175 case RT286_SET_POWER(RT286_DAC_OUT1):
176 case RT286_SET_POWER(RT286_DAC_OUT2):
177 case RT286_SET_POWER(RT286_ADC_IN1):
178 case RT286_SET_POWER(RT286_ADC_IN2):
179 case RT286_SET_POWER(RT286_DMIC2):
180 case RT286_SET_POWER(RT286_MIC1):
187 static int rt286_hw_write(void *context, unsigned int reg, unsigned int value)
189 struct i2c_client *client = context;
190 struct rt286_priv *rt286 = i2c_get_clientdata(client);
194 /* handle index registers */
196 rt286_hw_write(client, RT286_COEF_INDEX, reg);
197 for (i = 0; i < INDEX_CACHE_SIZE; i++) {
198 if (reg == rt286->index_cache[i].reg) {
199 rt286->index_cache[i].def = value;
204 reg = RT286_PROC_COEF;
207 data[0] = (reg >> 24) & 0xff;
208 data[1] = (reg >> 16) & 0xff;
210 * 4 bit VID: reg should be 0
211 * 12 bit VID: value should be 0
212 * So we use an OR operator to handle it rather than use if condition.
214 data[2] = ((reg >> 8) & 0xff) | ((value >> 8) & 0xff);
215 data[3] = value & 0xff;
217 ret = i2c_master_send(client, data, 4);
222 pr_err("ret=%d\n", ret);
229 static int rt286_hw_read(void *context, unsigned int reg, unsigned int *value)
231 struct i2c_client *client = context;
232 struct i2c_msg xfer[2];
235 unsigned int index, vid, buf = 0x0;
237 /* handle index registers */
239 rt286_hw_write(client, RT286_COEF_INDEX, reg);
240 reg = RT286_PROC_COEF;
244 vid = (reg >> 8) & 0xfff;
246 if (AC_VERB_GET_AMP_GAIN_MUTE == (vid & 0xf00)) {
247 index = (reg >> 8) & 0xf;
248 reg = (reg & ~0xf0f) | index;
250 be_reg = cpu_to_be32(reg);
253 xfer[0].addr = client->addr;
256 xfer[0].buf = (u8 *)&be_reg;
259 xfer[1].addr = client->addr;
260 xfer[1].flags = I2C_M_RD;
262 xfer[1].buf = (u8 *)&buf;
264 ret = i2c_transfer(client->adapter, xfer, 2);
270 *value = be32_to_cpu(buf);
276 static void rt286_index_sync(struct snd_soc_codec *codec)
278 struct rt286_priv *rt286 = snd_soc_codec_get_drvdata(codec);
281 for (i = 0; i < INDEX_CACHE_SIZE; i++) {
282 snd_soc_write(codec, rt286->index_cache[i].reg,
283 rt286->index_cache[i].def);
288 static int rt286_support_power_controls[] = {
299 #define RT286_POWER_REG_LEN ARRAY_SIZE(rt286_support_power_controls)
301 static int rt286_jack_detect(struct rt286_priv *rt286, bool *hp, bool *mic)
303 unsigned int val, buf;
310 if (rt286->pdata.cbj_en) {
311 regmap_read(rt286->regmap, RT286_GET_HP_SENSE, &buf);
312 *hp = buf & 0x80000000;
314 /* power on HV,VERF */
315 regmap_update_bits(rt286->regmap,
316 RT286_DC_GAIN, 0x200, 0x200);
318 snd_soc_dapm_force_enable_pin(&rt286->codec->dapm,
320 snd_soc_dapm_force_enable_pin(&rt286->codec->dapm,
323 snd_soc_dapm_force_enable_pin(&rt286->codec->dapm,
325 snd_soc_dapm_sync(&rt286->codec->dapm);
327 regmap_write(rt286->regmap, RT286_SET_MIC1, 0x24);
330 regmap_update_bits(rt286->regmap,
331 RT286_CBJ_CTRL1, 0xfcc0, 0xd400);
333 regmap_read(rt286->regmap, RT286_CBJ_CTRL2, &val);
335 if (0x0070 == (val & 0x0070)) {
338 regmap_update_bits(rt286->regmap,
339 RT286_CBJ_CTRL1, 0xfcc0, 0xe400);
341 regmap_read(rt286->regmap,
342 RT286_CBJ_CTRL2, &val);
343 if (0x0070 == (val & 0x0070))
348 regmap_update_bits(rt286->regmap,
349 RT286_DC_GAIN, 0x200, 0x0);
353 regmap_write(rt286->regmap, RT286_SET_MIC1, 0x20);
356 regmap_read(rt286->regmap, RT286_GET_HP_SENSE, &buf);
357 *hp = buf & 0x80000000;
358 regmap_read(rt286->regmap, RT286_GET_MIC1_SENSE, &buf);
359 *mic = buf & 0x80000000;
362 snd_soc_dapm_disable_pin(&rt286->codec->dapm, "HV");
363 snd_soc_dapm_disable_pin(&rt286->codec->dapm, "VREF");
365 snd_soc_dapm_disable_pin(&rt286->codec->dapm, "LDO1");
366 snd_soc_dapm_sync(&rt286->codec->dapm);
371 static void rt286_jack_detect_work(struct work_struct *work)
373 struct rt286_priv *rt286 =
374 container_of(work, struct rt286_priv, jack_detect_work.work);
379 rt286_jack_detect(rt286, &hp, &mic);
382 status |= SND_JACK_HEADPHONE;
385 status |= SND_JACK_MICROPHONE;
387 snd_soc_jack_report(rt286->jack, status,
388 SND_JACK_MICROPHONE | SND_JACK_HEADPHONE);
391 int rt286_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack)
393 struct rt286_priv *rt286 = snd_soc_codec_get_drvdata(codec);
397 /* Send an initial empty report */
398 snd_soc_jack_report(rt286->jack, 0,
399 SND_JACK_MICROPHONE | SND_JACK_HEADPHONE);
403 EXPORT_SYMBOL_GPL(rt286_mic_detect);
405 static int is_mclk_mode(struct snd_soc_dapm_widget *source,
406 struct snd_soc_dapm_widget *sink)
408 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
409 struct rt286_priv *rt286 = snd_soc_codec_get_drvdata(codec);
411 if (rt286->clk_id == RT286_SCLK_S_MCLK)
417 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6350, 50, 0);
418 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, 0, 1000, 0);
420 static const struct snd_kcontrol_new rt286_snd_controls[] = {
421 SOC_DOUBLE_R_TLV("DAC0 Playback Volume", RT286_DACL_GAIN,
422 RT286_DACR_GAIN, 0, 0x7f, 0, out_vol_tlv),
423 SOC_DOUBLE_R("ADC0 Capture Switch", RT286_ADCL_GAIN,
424 RT286_ADCR_GAIN, 7, 1, 1),
425 SOC_DOUBLE_R_TLV("ADC0 Capture Volume", RT286_ADCL_GAIN,
426 RT286_ADCR_GAIN, 0, 0x7f, 0, out_vol_tlv),
427 SOC_SINGLE_TLV("AMIC Volume", RT286_MIC_GAIN,
428 0, 0x3, 0, mic_vol_tlv),
429 SOC_DOUBLE_R("Speaker Playback Switch", RT286_SPOL_GAIN,
430 RT286_SPOR_GAIN, RT286_MUTE_SFT, 1, 1),
434 static const struct snd_kcontrol_new rt286_front_mix[] = {
435 SOC_DAPM_SINGLE("DAC Switch", RT286_F_DAC_SWITCH,
436 RT286_MUTE_SFT, 1, 1),
437 SOC_DAPM_SINGLE("RECMIX Switch", RT286_F_RECMIX_SWITCH,
438 RT286_MUTE_SFT, 1, 1),
441 /* Analog Input Mixer */
442 static const struct snd_kcontrol_new rt286_rec_mix[] = {
443 SOC_DAPM_SINGLE("Mic1 Switch", RT286_REC_MIC_SWITCH,
444 RT286_MUTE_SFT, 1, 1),
445 SOC_DAPM_SINGLE("I2S Switch", RT286_REC_I2S_SWITCH,
446 RT286_MUTE_SFT, 1, 1),
447 SOC_DAPM_SINGLE("Line1 Switch", RT286_REC_LINE_SWITCH,
448 RT286_MUTE_SFT, 1, 1),
449 SOC_DAPM_SINGLE("Beep Switch", RT286_REC_BEEP_SWITCH,
450 RT286_MUTE_SFT, 1, 1),
453 static const struct snd_kcontrol_new spo_enable_control =
454 SOC_DAPM_SINGLE("Switch", RT286_SET_PIN_SPK,
455 RT286_SET_PIN_SFT, 1, 0);
457 static const struct snd_kcontrol_new hpol_enable_control =
458 SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT286_HPOL_GAIN,
459 RT286_MUTE_SFT, 1, 1);
461 static const struct snd_kcontrol_new hpor_enable_control =
462 SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT286_HPOR_GAIN,
463 RT286_MUTE_SFT, 1, 1);
466 static const char * const rt286_adc_src[] = {
467 "Mic", "RECMIX", "Dmic"
470 static const int rt286_adc_values[] = {
474 static SOC_VALUE_ENUM_SINGLE_DECL(
475 rt286_adc0_enum, RT286_ADC0_MUX, RT286_ADC_SEL_SFT,
476 RT286_ADC_SEL_MASK, rt286_adc_src, rt286_adc_values);
478 static const struct snd_kcontrol_new rt286_adc0_mux =
479 SOC_DAPM_ENUM("ADC 0 source", rt286_adc0_enum);
481 static SOC_VALUE_ENUM_SINGLE_DECL(
482 rt286_adc1_enum, RT286_ADC1_MUX, RT286_ADC_SEL_SFT,
483 RT286_ADC_SEL_MASK, rt286_adc_src, rt286_adc_values);
485 static const struct snd_kcontrol_new rt286_adc1_mux =
486 SOC_DAPM_ENUM("ADC 1 source", rt286_adc1_enum);
488 static const char * const rt286_dac_src[] = {
492 static SOC_ENUM_SINGLE_DECL(rt286_hpo_enum, RT286_HPO_MUX,
495 static const struct snd_kcontrol_new rt286_hpo_mux =
496 SOC_DAPM_ENUM("HPO source", rt286_hpo_enum);
499 static SOC_ENUM_SINGLE_DECL(rt286_spo_enum, RT286_SPK_MUX,
502 static const struct snd_kcontrol_new rt286_spo_mux =
503 SOC_DAPM_ENUM("SPO source", rt286_spo_enum);
505 static int rt286_spk_event(struct snd_soc_dapm_widget *w,
506 struct snd_kcontrol *kcontrol, int event)
508 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
511 case SND_SOC_DAPM_POST_PMU:
513 RT286_SPK_EAPD, RT286_SET_EAPD_HIGH);
515 case SND_SOC_DAPM_PRE_PMD:
517 RT286_SPK_EAPD, RT286_SET_EAPD_LOW);
527 static int rt286_set_dmic1_event(struct snd_soc_dapm_widget *w,
528 struct snd_kcontrol *kcontrol, int event)
530 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
533 case SND_SOC_DAPM_POST_PMU:
534 snd_soc_write(codec, RT286_SET_PIN_DMIC1, 0x20);
536 case SND_SOC_DAPM_PRE_PMD:
537 snd_soc_write(codec, RT286_SET_PIN_DMIC1, 0);
546 static int rt286_vref_event(struct snd_soc_dapm_widget *w,
547 struct snd_kcontrol *kcontrol, int event)
549 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
552 case SND_SOC_DAPM_PRE_PMU:
553 snd_soc_update_bits(codec,
554 RT286_CBJ_CTRL1, 0x0400, 0x0000);
564 static int rt286_ldo2_event(struct snd_soc_dapm_widget *w,
565 struct snd_kcontrol *kcontrol, int event)
567 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
570 case SND_SOC_DAPM_POST_PMU:
571 snd_soc_update_bits(codec, RT286_POWER_CTRL2, 0x38, 0x08);
573 case SND_SOC_DAPM_PRE_PMD:
574 snd_soc_update_bits(codec, RT286_POWER_CTRL2, 0x38, 0x30);
583 static int rt286_mic1_event(struct snd_soc_dapm_widget *w,
584 struct snd_kcontrol *kcontrol, int event)
586 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
589 case SND_SOC_DAPM_PRE_PMU:
590 snd_soc_update_bits(codec,
591 RT286_A_BIAS_CTRL3, 0xc000, 0x8000);
592 snd_soc_update_bits(codec,
593 RT286_A_BIAS_CTRL2, 0xc000, 0x8000);
595 case SND_SOC_DAPM_POST_PMD:
596 snd_soc_update_bits(codec,
597 RT286_A_BIAS_CTRL3, 0xc000, 0x0000);
598 snd_soc_update_bits(codec,
599 RT286_A_BIAS_CTRL2, 0xc000, 0x0000);
608 static const struct snd_soc_dapm_widget rt286_dapm_widgets[] = {
609 SND_SOC_DAPM_SUPPLY_S("HV", 1, RT286_POWER_CTRL1,
611 SND_SOC_DAPM_SUPPLY("VREF", RT286_POWER_CTRL1,
612 0, 1, rt286_vref_event, SND_SOC_DAPM_PRE_PMU),
613 SND_SOC_DAPM_SUPPLY_S("LDO1", 1, RT286_POWER_CTRL2,
615 SND_SOC_DAPM_SUPPLY_S("LDO2", 2, RT286_POWER_CTRL1,
616 13, 1, rt286_ldo2_event, SND_SOC_DAPM_PRE_PMD |
617 SND_SOC_DAPM_POST_PMU),
618 SND_SOC_DAPM_SUPPLY("MCLK MODE", RT286_PLL_CTRL1,
620 SND_SOC_DAPM_SUPPLY("MIC1 Input Buffer", SND_SOC_NOPM,
621 0, 0, rt286_mic1_event, SND_SOC_DAPM_PRE_PMU |
622 SND_SOC_DAPM_POST_PMD),
625 SND_SOC_DAPM_INPUT("DMIC1 Pin"),
626 SND_SOC_DAPM_INPUT("DMIC2 Pin"),
627 SND_SOC_DAPM_INPUT("MIC1"),
628 SND_SOC_DAPM_INPUT("LINE1"),
629 SND_SOC_DAPM_INPUT("Beep"),
632 SND_SOC_DAPM_PGA_E("DMIC1", RT286_SET_POWER(RT286_DMIC1), 0, 1,
633 NULL, 0, rt286_set_dmic1_event,
634 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
635 SND_SOC_DAPM_PGA("DMIC2", RT286_SET_POWER(RT286_DMIC2), 0, 1,
637 SND_SOC_DAPM_SUPPLY("DMIC Receiver", SND_SOC_NOPM,
641 SND_SOC_DAPM_MIXER("RECMIX", SND_SOC_NOPM, 0, 0,
642 rt286_rec_mix, ARRAY_SIZE(rt286_rec_mix)),
645 SND_SOC_DAPM_ADC("ADC 0", NULL, SND_SOC_NOPM, 0, 0),
646 SND_SOC_DAPM_ADC("ADC 1", NULL, SND_SOC_NOPM, 0, 0),
649 SND_SOC_DAPM_MUX("ADC 0 Mux", RT286_SET_POWER(RT286_ADC_IN1), 0, 1,
651 SND_SOC_DAPM_MUX("ADC 1 Mux", RT286_SET_POWER(RT286_ADC_IN2), 0, 1,
654 /* Audio Interface */
655 SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
656 SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
657 SND_SOC_DAPM_AIF_IN("AIF2RX", "AIF2 Playback", 0, SND_SOC_NOPM, 0, 0),
658 SND_SOC_DAPM_AIF_OUT("AIF2TX", "AIF2 Capture", 0, SND_SOC_NOPM, 0, 0),
662 SND_SOC_DAPM_DAC("DAC 0", NULL, SND_SOC_NOPM, 0, 0),
663 SND_SOC_DAPM_DAC("DAC 1", NULL, SND_SOC_NOPM, 0, 0),
666 SND_SOC_DAPM_MUX("SPK Mux", SND_SOC_NOPM, 0, 0, &rt286_spo_mux),
667 SND_SOC_DAPM_MUX("HPO Mux", SND_SOC_NOPM, 0, 0, &rt286_hpo_mux),
669 SND_SOC_DAPM_SUPPLY("HP Power", RT286_SET_PIN_HPO,
670 RT286_SET_PIN_SFT, 0, NULL, 0),
673 SND_SOC_DAPM_MIXER("Front", RT286_SET_POWER(RT286_DAC_OUT1), 0, 1,
674 rt286_front_mix, ARRAY_SIZE(rt286_front_mix)),
675 SND_SOC_DAPM_PGA("Surround", RT286_SET_POWER(RT286_DAC_OUT2), 0, 1,
679 SND_SOC_DAPM_SWITCH_E("SPO", SND_SOC_NOPM, 0, 0,
680 &spo_enable_control, rt286_spk_event,
681 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
682 SND_SOC_DAPM_SWITCH("HPO L", SND_SOC_NOPM, 0, 0,
683 &hpol_enable_control),
684 SND_SOC_DAPM_SWITCH("HPO R", SND_SOC_NOPM, 0, 0,
685 &hpor_enable_control),
688 SND_SOC_DAPM_OUTPUT("SPOL"),
689 SND_SOC_DAPM_OUTPUT("SPOR"),
690 SND_SOC_DAPM_OUTPUT("HPO Pin"),
691 SND_SOC_DAPM_OUTPUT("SPDIF"),
694 static const struct snd_soc_dapm_route rt286_dapm_routes[] = {
695 {"ADC 0", NULL, "MCLK MODE", is_mclk_mode},
696 {"ADC 1", NULL, "MCLK MODE", is_mclk_mode},
697 {"Front", NULL, "MCLK MODE", is_mclk_mode},
698 {"Surround", NULL, "MCLK MODE", is_mclk_mode},
700 {"HP Power", NULL, "LDO1"},
701 {"HP Power", NULL, "LDO2"},
703 {"MIC1", NULL, "LDO1"},
704 {"MIC1", NULL, "LDO2"},
705 {"MIC1", NULL, "HV"},
706 {"MIC1", NULL, "VREF"},
707 {"MIC1", NULL, "MIC1 Input Buffer"},
709 {"SPO", NULL, "LDO1"},
710 {"SPO", NULL, "LDO2"},
712 {"SPO", NULL, "VREF"},
714 {"DMIC1", NULL, "DMIC1 Pin"},
715 {"DMIC2", NULL, "DMIC2 Pin"},
716 {"DMIC1", NULL, "DMIC Receiver"},
717 {"DMIC2", NULL, "DMIC Receiver"},
719 {"RECMIX", "Beep Switch", "Beep"},
720 {"RECMIX", "Line1 Switch", "LINE1"},
721 {"RECMIX", "Mic1 Switch", "MIC1"},
723 {"ADC 0 Mux", "Dmic", "DMIC1"},
724 {"ADC 0 Mux", "RECMIX", "RECMIX"},
725 {"ADC 0 Mux", "Mic", "MIC1"},
726 {"ADC 1 Mux", "Dmic", "DMIC2"},
727 {"ADC 1 Mux", "RECMIX", "RECMIX"},
728 {"ADC 1 Mux", "Mic", "MIC1"},
730 {"ADC 0", NULL, "ADC 0 Mux"},
731 {"ADC 1", NULL, "ADC 1 Mux"},
733 {"AIF1TX", NULL, "ADC 0"},
734 {"AIF2TX", NULL, "ADC 1"},
736 {"DAC 0", NULL, "AIF1RX"},
737 {"DAC 1", NULL, "AIF2RX"},
739 {"Front", "DAC Switch", "DAC 0"},
740 {"Front", "RECMIX Switch", "RECMIX"},
742 {"Surround", NULL, "DAC 1"},
744 {"SPK Mux", "Front", "Front"},
745 {"SPK Mux", "Surround", "Surround"},
747 {"HPO Mux", "Front", "Front"},
748 {"HPO Mux", "Surround", "Surround"},
750 {"SPO", "Switch", "SPK Mux"},
751 {"HPO L", "Switch", "HPO Mux"},
752 {"HPO R", "Switch", "HPO Mux"},
753 {"HPO L", NULL, "HP Power"},
754 {"HPO R", NULL, "HP Power"},
756 {"SPOL", NULL, "SPO"},
757 {"SPOR", NULL, "SPO"},
758 {"HPO Pin", NULL, "HPO L"},
759 {"HPO Pin", NULL, "HPO R"},
762 static int rt286_hw_params(struct snd_pcm_substream *substream,
763 struct snd_pcm_hw_params *params,
764 struct snd_soc_dai *dai)
766 struct snd_soc_codec *codec = dai->codec;
767 struct rt286_priv *rt286 = snd_soc_codec_get_drvdata(codec);
768 unsigned int val = 0;
771 switch (params_rate(params)) {
772 /* bit 14 0:48K 1:44.1K */
779 dev_err(codec->dev, "Unsupported sample rate %d\n",
780 params_rate(params));
783 switch (rt286->sys_clk) {
786 if (params_rate(params) != 48000) {
787 dev_err(codec->dev, "Sys_clk is not matched (%d %d)\n",
788 params_rate(params), rt286->sys_clk);
794 if (params_rate(params) != 44100) {
795 dev_err(codec->dev, "Sys_clk is not matched (%d %d)\n",
796 params_rate(params), rt286->sys_clk);
802 if (params_channels(params) <= 16) {
803 /* bit 3:0 Number of Channel */
804 val |= (params_channels(params) - 1);
806 dev_err(codec->dev, "Unsupported channels %d\n",
807 params_channels(params));
812 switch (params_width(params)) {
813 /* bit 6:4 Bits per Sample */
837 snd_soc_update_bits(codec,
838 RT286_I2S_CTRL1, 0x0018, d_len_code << 3);
839 dev_dbg(codec->dev, "format val = 0x%x\n", val);
841 snd_soc_update_bits(codec, RT286_DAC_FORMAT, 0x407f, val);
842 snd_soc_update_bits(codec, RT286_ADC_FORMAT, 0x407f, val);
847 static int rt286_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
849 struct snd_soc_codec *codec = dai->codec;
851 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
852 case SND_SOC_DAIFMT_CBM_CFM:
853 snd_soc_update_bits(codec,
854 RT286_I2S_CTRL1, 0x800, 0x800);
856 case SND_SOC_DAIFMT_CBS_CFS:
857 snd_soc_update_bits(codec,
858 RT286_I2S_CTRL1, 0x800, 0x0);
864 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
865 case SND_SOC_DAIFMT_I2S:
866 snd_soc_update_bits(codec,
867 RT286_I2S_CTRL1, 0x300, 0x0);
869 case SND_SOC_DAIFMT_LEFT_J:
870 snd_soc_update_bits(codec,
871 RT286_I2S_CTRL1, 0x300, 0x1 << 8);
873 case SND_SOC_DAIFMT_DSP_A:
874 snd_soc_update_bits(codec,
875 RT286_I2S_CTRL1, 0x300, 0x2 << 8);
877 case SND_SOC_DAIFMT_DSP_B:
878 snd_soc_update_bits(codec,
879 RT286_I2S_CTRL1, 0x300, 0x3 << 8);
884 /* bit 15 Stream Type 0:PCM 1:Non-PCM */
885 snd_soc_update_bits(codec, RT286_DAC_FORMAT, 0x8000, 0);
886 snd_soc_update_bits(codec, RT286_ADC_FORMAT, 0x8000, 0);
891 static int rt286_set_dai_sysclk(struct snd_soc_dai *dai,
892 int clk_id, unsigned int freq, int dir)
894 struct snd_soc_codec *codec = dai->codec;
895 struct rt286_priv *rt286 = snd_soc_codec_get_drvdata(codec);
897 dev_dbg(codec->dev, "%s freq=%d\n", __func__, freq);
899 if (RT286_SCLK_S_MCLK == clk_id) {
900 snd_soc_update_bits(codec,
901 RT286_I2S_CTRL2, 0x0100, 0x0);
902 snd_soc_update_bits(codec,
903 RT286_PLL_CTRL1, 0x20, 0x20);
905 snd_soc_update_bits(codec,
906 RT286_I2S_CTRL2, 0x0100, 0x0100);
907 snd_soc_update_bits(codec,
908 RT286_PLL_CTRL, 0x4, 0x4);
909 snd_soc_update_bits(codec,
910 RT286_PLL_CTRL1, 0x20, 0x0);
915 if (RT286_SCLK_S_MCLK == clk_id) {
916 dev_err(codec->dev, "Should not use MCLK\n");
919 snd_soc_update_bits(codec,
920 RT286_I2S_CTRL2, 0x40, 0x40);
923 if (RT286_SCLK_S_MCLK == clk_id) {
924 dev_err(codec->dev, "Should not use MCLK\n");
927 snd_soc_update_bits(codec,
928 RT286_I2S_CTRL2, 0x40, 0x0);
932 snd_soc_update_bits(codec,
933 RT286_I2S_CTRL2, 0x8, 0x0);
934 snd_soc_update_bits(codec,
935 RT286_CLK_DIV, 0xfc1e, 0x0004);
939 snd_soc_update_bits(codec,
940 RT286_I2S_CTRL2, 0x8, 0x8);
941 snd_soc_update_bits(codec,
942 RT286_CLK_DIV, 0xfc1e, 0x5406);
945 dev_err(codec->dev, "Unsupported system clock\n");
949 rt286->sys_clk = freq;
950 rt286->clk_id = clk_id;
955 static int rt286_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
957 struct snd_soc_codec *codec = dai->codec;
959 dev_dbg(codec->dev, "%s ratio=%d\n", __func__, ratio);
961 snd_soc_update_bits(codec,
962 RT286_I2S_CTRL1, 0x1000, 0x1000);
964 snd_soc_update_bits(codec,
965 RT286_I2S_CTRL1, 0x1000, 0x0);
971 static int rt286_set_bias_level(struct snd_soc_codec *codec,
972 enum snd_soc_bias_level level)
975 case SND_SOC_BIAS_PREPARE:
976 if (SND_SOC_BIAS_STANDBY == codec->dapm.bias_level) {
978 RT286_SET_AUDIO_POWER, AC_PWRST_D0);
979 snd_soc_update_bits(codec,
980 RT286_DC_GAIN, 0x200, 0x200);
984 case SND_SOC_BIAS_ON:
986 snd_soc_update_bits(codec,
987 RT286_CBJ_CTRL1, 0x0400, 0x0400);
988 snd_soc_update_bits(codec,
989 RT286_DC_GAIN, 0x200, 0x0);
993 case SND_SOC_BIAS_STANDBY:
995 RT286_SET_AUDIO_POWER, AC_PWRST_D3);
996 snd_soc_update_bits(codec,
997 RT286_CBJ_CTRL1, 0x0400, 0x0000);
1003 codec->dapm.bias_level = level;
1008 static irqreturn_t rt286_irq(int irq, void *data)
1010 struct rt286_priv *rt286 = data;
1015 rt286_jack_detect(rt286, &hp, &mic);
1018 regmap_update_bits(rt286->regmap, RT286_IRQ_CTRL, 0x1, 0x1);
1021 status |= SND_JACK_HEADPHONE;
1024 status |= SND_JACK_MICROPHONE;
1026 snd_soc_jack_report(rt286->jack, status,
1027 SND_JACK_MICROPHONE | SND_JACK_HEADPHONE);
1029 pm_wakeup_event(&rt286->i2c->dev, 300);
1034 static int rt286_probe(struct snd_soc_codec *codec)
1036 struct rt286_priv *rt286 = snd_soc_codec_get_drvdata(codec);
1038 rt286->codec = codec;
1039 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
1041 if (rt286->i2c->irq) {
1042 regmap_update_bits(rt286->regmap,
1043 RT286_IRQ_CTRL, 0x2, 0x2);
1045 INIT_DELAYED_WORK(&rt286->jack_detect_work,
1046 rt286_jack_detect_work);
1047 schedule_delayed_work(&rt286->jack_detect_work,
1048 msecs_to_jiffies(1250));
1054 static int rt286_remove(struct snd_soc_codec *codec)
1056 struct rt286_priv *rt286 = snd_soc_codec_get_drvdata(codec);
1058 cancel_delayed_work_sync(&rt286->jack_detect_work);
1064 static int rt286_suspend(struct snd_soc_codec *codec)
1066 struct rt286_priv *rt286 = snd_soc_codec_get_drvdata(codec);
1068 regcache_cache_only(rt286->regmap, true);
1069 regcache_mark_dirty(rt286->regmap);
1074 static int rt286_resume(struct snd_soc_codec *codec)
1076 struct rt286_priv *rt286 = snd_soc_codec_get_drvdata(codec);
1078 regcache_cache_only(rt286->regmap, false);
1079 rt286_index_sync(codec);
1080 regcache_sync(rt286->regmap);
1085 #define rt286_suspend NULL
1086 #define rt286_resume NULL
1089 #define RT286_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
1090 #define RT286_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1091 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
1093 static const struct snd_soc_dai_ops rt286_aif_dai_ops = {
1094 .hw_params = rt286_hw_params,
1095 .set_fmt = rt286_set_dai_fmt,
1096 .set_sysclk = rt286_set_dai_sysclk,
1097 .set_bclk_ratio = rt286_set_bclk_ratio,
1100 static struct snd_soc_dai_driver rt286_dai[] = {
1102 .name = "rt286-aif1",
1105 .stream_name = "AIF1 Playback",
1108 .rates = RT286_STEREO_RATES,
1109 .formats = RT286_FORMATS,
1112 .stream_name = "AIF1 Capture",
1115 .rates = RT286_STEREO_RATES,
1116 .formats = RT286_FORMATS,
1118 .ops = &rt286_aif_dai_ops,
1119 .symmetric_rates = 1,
1122 .name = "rt286-aif2",
1125 .stream_name = "AIF2 Playback",
1128 .rates = RT286_STEREO_RATES,
1129 .formats = RT286_FORMATS,
1132 .stream_name = "AIF2 Capture",
1135 .rates = RT286_STEREO_RATES,
1136 .formats = RT286_FORMATS,
1138 .ops = &rt286_aif_dai_ops,
1139 .symmetric_rates = 1,
1144 static struct snd_soc_codec_driver soc_codec_dev_rt286 = {
1145 .probe = rt286_probe,
1146 .remove = rt286_remove,
1147 .suspend = rt286_suspend,
1148 .resume = rt286_resume,
1149 .set_bias_level = rt286_set_bias_level,
1150 .idle_bias_off = true,
1151 .controls = rt286_snd_controls,
1152 .num_controls = ARRAY_SIZE(rt286_snd_controls),
1153 .dapm_widgets = rt286_dapm_widgets,
1154 .num_dapm_widgets = ARRAY_SIZE(rt286_dapm_widgets),
1155 .dapm_routes = rt286_dapm_routes,
1156 .num_dapm_routes = ARRAY_SIZE(rt286_dapm_routes),
1159 static const struct regmap_config rt286_regmap = {
1162 .max_register = 0x02370100,
1163 .volatile_reg = rt286_volatile_register,
1164 .readable_reg = rt286_readable_register,
1165 .reg_write = rt286_hw_write,
1166 .reg_read = rt286_hw_read,
1167 .cache_type = REGCACHE_RBTREE,
1168 .reg_defaults = rt286_reg,
1169 .num_reg_defaults = ARRAY_SIZE(rt286_reg),
1172 static const struct i2c_device_id rt286_i2c_id[] = {
1176 MODULE_DEVICE_TABLE(i2c, rt286_i2c_id);
1178 static const struct acpi_device_id rt286_acpi_match[] = {
1182 MODULE_DEVICE_TABLE(acpi, rt286_acpi_match);
1184 static struct dmi_system_id force_combo_jack_table[] = {
1186 .ident = "Intel Wilson Beach",
1188 DMI_MATCH(DMI_BOARD_NAME, "Wilson Beach SDS")
1194 static int rt286_i2c_probe(struct i2c_client *i2c,
1195 const struct i2c_device_id *id)
1197 struct rt286_platform_data *pdata = dev_get_platdata(&i2c->dev);
1198 struct rt286_priv *rt286;
1201 rt286 = devm_kzalloc(&i2c->dev, sizeof(*rt286),
1206 rt286->regmap = devm_regmap_init(&i2c->dev, NULL, i2c, &rt286_regmap);
1207 if (IS_ERR(rt286->regmap)) {
1208 ret = PTR_ERR(rt286->regmap);
1209 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
1214 regmap_read(rt286->regmap,
1215 RT286_GET_PARAM(AC_NODE_ROOT, AC_PAR_VENDOR_ID), &ret);
1216 if (ret != RT286_VENDOR_ID) {
1218 "Device with ID register %x is not rt286\n", ret);
1222 rt286->index_cache = rt286_index_def;
1224 i2c_set_clientdata(i2c, rt286);
1227 rt286->pdata = *pdata;
1229 if (dmi_check_system(force_combo_jack_table))
1230 rt286->pdata.cbj_en = true;
1232 regmap_write(rt286->regmap, RT286_SET_AUDIO_POWER, AC_PWRST_D3);
1234 for (i = 0; i < RT286_POWER_REG_LEN; i++)
1235 regmap_write(rt286->regmap,
1236 RT286_SET_POWER(rt286_support_power_controls[i]),
1239 if (!rt286->pdata.cbj_en) {
1240 regmap_write(rt286->regmap, RT286_CBJ_CTRL2, 0x0000);
1241 regmap_write(rt286->regmap, RT286_MIC1_DET_CTRL, 0x0816);
1242 regmap_update_bits(rt286->regmap,
1243 RT286_CBJ_CTRL1, 0xf000, 0xb000);
1245 regmap_update_bits(rt286->regmap,
1246 RT286_CBJ_CTRL1, 0xf000, 0x5000);
1251 if (!rt286->pdata.gpio2_en)
1252 regmap_write(rt286->regmap, RT286_SET_DMIC2_DEFAULT, 0x4000);
1254 regmap_write(rt286->regmap, RT286_SET_DMIC2_DEFAULT, 0);
1258 regmap_write(rt286->regmap, RT286_MISC_CTRL1, 0x0000);
1259 /* Power down LDO, VREF */
1260 regmap_update_bits(rt286->regmap, RT286_POWER_CTRL2, 0xc, 0x0);
1261 regmap_update_bits(rt286->regmap, RT286_POWER_CTRL1, 0x1001, 0x1001);
1263 /* Set depop parameter */
1264 regmap_update_bits(rt286->regmap, RT286_DEPOP_CTRL2, 0x403a, 0x401a);
1265 regmap_update_bits(rt286->regmap, RT286_DEPOP_CTRL3, 0xf777, 0x4737);
1266 regmap_update_bits(rt286->regmap, RT286_DEPOP_CTRL4, 0x00ff, 0x003f);
1268 if (rt286->i2c->irq) {
1269 ret = request_threaded_irq(rt286->i2c->irq, NULL, rt286_irq,
1270 IRQF_TRIGGER_HIGH | IRQF_ONESHOT, "rt286", rt286);
1273 "Failed to reguest IRQ: %d\n", ret);
1278 ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_rt286,
1279 rt286_dai, ARRAY_SIZE(rt286_dai));
1284 static int rt286_i2c_remove(struct i2c_client *i2c)
1286 struct rt286_priv *rt286 = i2c_get_clientdata(i2c);
1289 free_irq(i2c->irq, rt286);
1290 snd_soc_unregister_codec(&i2c->dev);
1296 static struct i2c_driver rt286_i2c_driver = {
1299 .owner = THIS_MODULE,
1300 .acpi_match_table = ACPI_PTR(rt286_acpi_match),
1302 .probe = rt286_i2c_probe,
1303 .remove = rt286_i2c_remove,
1304 .id_table = rt286_i2c_id,
1307 module_i2c_driver(rt286_i2c_driver);
1309 MODULE_DESCRIPTION("ASoC RT286 driver");
1310 MODULE_AUTHOR("Bard Liao <bardliao@realtek.com>");
1311 MODULE_LICENSE("GPL");