2 * ac10x.c -- ac10x ALSA SoC Audio driver
5 * Author: Baozhu Zuo<zuobaozhu@gmail.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.
13 * use 'make DEBUG=1' to enable debugging
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
20 #include <linux/clk.h>
21 #include <linux/i2c.h>
22 #include <linux/slab.h>
23 #include <linux/workqueue.h>
24 #include <linux/regmap.h>
25 #include <linux/gpio/consumer.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 #include <sound/initval.h>
31 #include <sound/tlv.h>
35 #define _USE_CAPTURE 1
36 #define _MASTER_INDEX 0
40 * 1, add PM API: ac108_suspend,ac108_resume
42 * 3,0x76-0x79 high 4bit
46 unsigned int freq_out;
54 static struct ac10x_priv *ac10x;
56 struct real_val_to_reg_val {
57 unsigned int real_val;
61 static const struct real_val_to_reg_val ac108_sample_rate[] = {
74 /* Sample resolution */
75 static const struct real_val_to_reg_val ac108_samp_res[] = {
85 static const unsigned ac108_bclkdivs[] = {
92 /* FOUT =(FIN * N) / [(M1+1) * (M2+1)*(K1+1)*(K2+1)] ; M1[0,31], M2[0,1], N[0,1023], K1[0,31], K2[0,1] */
93 static const struct pll_div ac108_pll_div_list[] = {
94 { 400000, _FREQ_24_576K, 0, 0, 614, 4, 1 },
95 { 512000, _FREQ_24_576K, 0, 0, 960, 9, 1 }, //_FREQ_24_576K/48
96 { 768000, _FREQ_24_576K, 0, 0, 640, 9, 1 }, //_FREQ_24_576K/32
97 { 800000, _FREQ_24_576K, 0, 0, 614, 9, 1 },
98 { 1024000, _FREQ_24_576K, 0, 0, 480, 9, 1 }, //_FREQ_24_576K/24
99 { 1600000, _FREQ_24_576K, 0, 0, 307, 9, 1 },
100 { 2048000, _FREQ_24_576K, 0, 0, 240, 9, 1 }, /* accurate, 8000 * 256 */
101 { 3072000, _FREQ_24_576K, 0, 0, 160, 9, 1 }, /* accurate, 12000 * 256 */
102 { 4096000, _FREQ_24_576K, 2, 0, 360, 9, 1 }, /* accurate, 16000 * 256 */
103 { 6000000, _FREQ_24_576K, 4, 0, 410, 9, 1 },
104 { 12000000, _FREQ_24_576K, 9, 0, 410, 9, 1 },
105 { 13000000, _FREQ_24_576K, 8, 0, 340, 9, 1 },
106 { 15360000, _FREQ_24_576K, 12, 0, 415, 9, 1 },
107 { 16000000, _FREQ_24_576K, 12, 0, 400, 9, 1 },
108 { 19200000, _FREQ_24_576K, 15, 0, 410, 9, 1 },
109 { 19680000, _FREQ_24_576K, 15, 0, 400, 9, 1 },
110 { 24000000, _FREQ_24_576K, 4, 0, 128,24, 0 }, // accurate, 24M -> 24.576M */
112 { 400000, _FREQ_22_579K, 0, 0, 566, 4, 1 },
113 { 512000, _FREQ_22_579K, 0, 0, 880, 9, 1 },
114 { 768000, _FREQ_22_579K, 0, 0, 587, 9, 1 },
115 { 800000, _FREQ_22_579K, 0, 0, 567, 9, 1 },
116 { 1024000, _FREQ_22_579K, 0, 0, 440, 9, 1 },
117 { 1600000, _FREQ_22_579K, 1, 0, 567, 9, 1 },
118 { 2048000, _FREQ_22_579K, 0, 0, 220, 9, 1 },
119 { 3072000, _FREQ_22_579K, 0, 0, 148, 9, 1 },
120 { 4096000, _FREQ_22_579K, 2, 0, 330, 9, 1 },
121 { 6000000, _FREQ_22_579K, 2, 0, 227, 9, 1 },
122 { 12000000, _FREQ_22_579K, 8, 0, 340, 9, 1 },
123 { 13000000, _FREQ_22_579K, 9, 0, 350, 9, 1 },
124 { 15360000, _FREQ_22_579K, 10, 0, 325, 9, 1 },
125 { 16000000, _FREQ_22_579K, 11, 0, 340, 9, 1 },
126 { 19200000, _FREQ_22_579K, 13, 0, 330, 9, 1 },
127 { 19680000, _FREQ_22_579K, 14, 0, 345, 9, 1 },
128 { 24000000, _FREQ_22_579K, 24, 0, 588,24, 0 }, // accurate, 24M -> 22.5792M */
131 { _FREQ_24_576K / 1, _FREQ_24_576K, 9, 0, 200, 9, 1 }, //_FREQ_24_576K
132 { _FREQ_24_576K / 2, _FREQ_24_576K, 9, 0, 400, 9, 1 }, /*12288000,accurate, 48000 * 256 */
133 { _FREQ_24_576K / 4, _FREQ_24_576K, 4, 0, 400, 9, 1 }, /*6144000, accurate, 24000 * 256 */
134 { _FREQ_24_576K / 16, _FREQ_24_576K, 0, 0, 320, 9, 1 }, //1536000
135 { _FREQ_24_576K / 64, _FREQ_24_576K, 0, 0, 640, 4, 1 }, //384000
136 { _FREQ_24_576K / 96, _FREQ_24_576K, 0, 0, 960, 4, 1 }, //256000
137 { _FREQ_24_576K / 128, _FREQ_24_576K, 0, 0, 512, 1, 1 }, //192000
138 { _FREQ_24_576K / 176, _FREQ_24_576K, 0, 0, 880, 4, 0 }, //140000
139 { _FREQ_24_576K / 192, _FREQ_24_576K, 0, 0, 960, 4, 0 }, //128000
141 { _FREQ_22_579K / 1, _FREQ_22_579K, 9, 0, 200, 9, 1 }, //_FREQ_22_579K
142 { _FREQ_22_579K / 2, _FREQ_22_579K, 9, 0, 400, 9, 1 }, /*11289600,accurate, 44100 * 256 */
143 { _FREQ_22_579K / 4, _FREQ_22_579K, 4, 0, 400, 9, 1 }, /*5644800, accurate, 22050 * 256 */
144 { _FREQ_22_579K / 16, _FREQ_22_579K, 0, 0, 320, 9, 1 }, //1411200
145 { _FREQ_22_579K / 64, _FREQ_22_579K, 0, 0, 640, 4, 1 }, //352800
146 { _FREQ_22_579K / 96, _FREQ_22_579K, 0, 0, 960, 4, 1 }, //235200
147 { _FREQ_22_579K / 128, _FREQ_22_579K, 0, 0, 512, 1, 1 }, //176400
148 { _FREQ_22_579K / 176, _FREQ_22_579K, 0, 0, 880, 4, 0 }, //128290
149 { _FREQ_22_579K / 192, _FREQ_22_579K, 0, 0, 960, 4, 0 }, //117600
151 { _FREQ_22_579K / 6, _FREQ_22_579K, 2, 0, 360, 9, 1 }, //3763200
152 { _FREQ_22_579K / 8, _FREQ_22_579K, 0, 0, 160, 9, 1 }, /*2822400, accurate, 11025 * 256 */
153 { _FREQ_22_579K / 12, _FREQ_22_579K, 0, 0, 240, 9, 1 }, //1881600
154 { _FREQ_22_579K / 24, _FREQ_22_579K, 0, 0, 480, 9, 1 }, //940800
155 { _FREQ_22_579K / 32, _FREQ_22_579K, 0, 0, 640, 9, 1 }, //705600
156 { _FREQ_22_579K / 48, _FREQ_22_579K, 0, 0, 960, 9, 1 }, //470400
160 /* AC108 definition */
161 #define AC108_CHANNELS_MAX 8 /* range[1, 16] */
162 #define AC108_RATES (SNDRV_PCM_RATE_8000_96000 & \
163 ~(SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_64000 | \
164 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000))
165 #define AC108_FORMATS (/*SNDRV_PCM_FMTBIT_S16_LE | \
166 SNDRV_PCM_FMTBIT_S20_3LE | \
167 SNDRV_PCM_FMTBIT_S24_LE |*/ \
168 SNDRV_PCM_FMTBIT_S32_LE)
170 static const DECLARE_TLV_DB_SCALE(tlv_adc_pga_gain, 0, 100, 0);
171 static const DECLARE_TLV_DB_SCALE(tlv_ch_digital_vol, -11925,75,0);
173 int ac10x_read(u8 reg, u8* rt_val, struct regmap* i2cm) {
176 if ((r = regmap_read(i2cm, reg, &v)) < 0) {
177 pr_err("ac10x_read error->[REG-0x%02x]\n", reg);
184 int ac10x_write(u8 reg, u8 val, struct regmap* i2cm) {
187 if ((r = regmap_write(i2cm, reg, val)) < 0) {
188 pr_err("ac10x_write error->[REG-0x%02x,val-0x%02x]\n", reg, val);
193 int ac10x_update_bits(u8 reg, u8 mask, u8 val, struct regmap* i2cm) {
196 if ((r = regmap_update_bits(i2cm, reg, mask, val)) < 0) {
197 pr_err("%s() error->[REG-0x%02x,val-0x%02x]\n", __func__, reg, val);
203 * snd_ac108_get_volsw - single mixer get callback
204 * @kcontrol: mixer control
205 * @ucontrol: control element information
207 * Callback to get the value of a single mixer control, or a double mixer
208 * control that spans 2 registers.
210 * Returns 0 for success.
212 static int snd_ac108_get_volsw(struct snd_kcontrol *kcontrol,
213 struct snd_ctl_elem_value *ucontrol
215 struct soc_mixer_control *mc =
216 (struct soc_mixer_control *)kcontrol->private_value;
217 unsigned int mask = (1 << fls(mc->max)) - 1;
218 unsigned int invert = mc->invert;
219 int ret, chip = mc->autodisable;
222 if ((ret = ac10x_read(mc->reg, &val, ac10x->i2cmap[chip])) < 0)
225 val = ((val >> mc->shift) & mask) - mc->min;
229 ucontrol->value.integer.value[0] = val;
234 * snd_ac108_put_volsw - single mixer put callback
235 * @kcontrol: mixer control
236 * @ucontrol: control element information
238 * Callback to set the value of a single mixer control, or a double mixer
239 * control that spans 2 registers.
241 * Returns 0 for success.
243 static int snd_ac108_put_volsw(struct snd_kcontrol *kcontrol,
244 struct snd_ctl_elem_value *ucontrol
246 struct soc_mixer_control *mc =
247 (struct soc_mixer_control *)kcontrol->private_value;
248 unsigned int sign_bit = mc->sign_bit;
249 unsigned int val, mask = (1 << fls(mc->max)) - 1;
250 unsigned int invert = mc->invert;
251 int ret, chip = mc->autodisable;
254 mask = BIT(sign_bit + 1) - 1;
256 val = ((ucontrol->value.integer.value[0] + mc->min) & mask);
261 mask = mask << mc->shift;
262 val = val << mc->shift;
264 ret = ac10x_update_bits(mc->reg, mask, val, ac10x->i2cmap[chip]);
268 #define SOC_AC108_SINGLE_TLV(xname, reg, shift, max, invert, chip, tlv_array) \
269 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
270 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
271 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
272 .tlv.p = (tlv_array), \
273 .info = snd_soc_info_volsw, .get = snd_ac108_get_volsw,\
274 .put = snd_ac108_put_volsw, \
275 .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert, chip) }
278 static const struct snd_kcontrol_new ac108_snd_controls[] = {
280 /*0x70: ADC1 Digital Channel Volume Control Register*/
281 SOC_AC108_SINGLE_TLV("CH1 digital volume", ADC1_DVOL_CTRL, 0, 0xff, 0, 0, tlv_ch_digital_vol),
282 /*0x71: ADC2 Digital Channel Volume Control Register*/
283 SOC_AC108_SINGLE_TLV("CH2 digital volume", ADC2_DVOL_CTRL, 0, 0xff, 0, 0, tlv_ch_digital_vol),
284 /*0x72: ADC3 Digital Channel Volume Control Register*/
285 SOC_AC108_SINGLE_TLV("CH3 digital volume", ADC3_DVOL_CTRL, 0, 0xff, 0, 0, tlv_ch_digital_vol),
286 /*0x73: ADC4 Digital Channel Volume Control Register*/
287 SOC_AC108_SINGLE_TLV("CH4 digital volume", ADC4_DVOL_CTRL, 0, 0xff, 0, 0, tlv_ch_digital_vol),
289 /*0x90: Analog PGA1 Control Register*/
290 SOC_AC108_SINGLE_TLV("ADC1 PGA gain", ANA_PGA1_CTRL, ADC1_ANALOG_PGA, 0x1f, 0, 0, tlv_adc_pga_gain),
291 /*0x91: Analog PGA2 Control Register*/
292 SOC_AC108_SINGLE_TLV("ADC2 PGA gain", ANA_PGA2_CTRL, ADC2_ANALOG_PGA, 0x1f, 0, 0, tlv_adc_pga_gain),
293 /*0x92: Analog PGA3 Control Register*/
294 SOC_AC108_SINGLE_TLV("ADC3 PGA gain", ANA_PGA3_CTRL, ADC3_ANALOG_PGA, 0x1f, 0, 0, tlv_adc_pga_gain),
295 /*0x93: Analog PGA4 Control Register*/
296 SOC_AC108_SINGLE_TLV("ADC4 PGA gain", ANA_PGA4_CTRL, ADC4_ANALOG_PGA, 0x1f, 0, 0, tlv_adc_pga_gain),
298 /* multiple ac108s */
299 static const struct snd_kcontrol_new ac108tdm_snd_controls[] = {
301 /*0x70: ADC1 Digital Channel Volume Control Register*/
302 SOC_AC108_SINGLE_TLV("CH1 digital volume", ADC1_DVOL_CTRL, 0, 0xff, 0, 1, tlv_ch_digital_vol),
303 /*0x71: ADC2 Digital Channel Volume Control Register*/
304 SOC_AC108_SINGLE_TLV("CH2 digital volume", ADC2_DVOL_CTRL, 0, 0xff, 0, 1, tlv_ch_digital_vol),
305 /*0x72: ADC3 Digital Channel Volume Control Register*/
306 SOC_AC108_SINGLE_TLV("CH3 digital volume", ADC3_DVOL_CTRL, 0, 0xff, 0, 1, tlv_ch_digital_vol),
307 /*0x73: ADC4 Digital Channel Volume Control Register*/
308 SOC_AC108_SINGLE_TLV("CH4 digital volume", ADC4_DVOL_CTRL, 0, 0xff, 0, 1, tlv_ch_digital_vol),
310 /*0x90: Analog PGA1 Control Register*/
311 SOC_AC108_SINGLE_TLV("ADC1 PGA gain", ANA_PGA1_CTRL, ADC1_ANALOG_PGA, 0x1f, 0, 1, tlv_adc_pga_gain),
312 /*0x91: Analog PGA2 Control Register*/
313 SOC_AC108_SINGLE_TLV("ADC2 PGA gain", ANA_PGA2_CTRL, ADC2_ANALOG_PGA, 0x1f, 0, 1, tlv_adc_pga_gain),
314 /*0x92: Analog PGA3 Control Register*/
315 SOC_AC108_SINGLE_TLV("ADC3 PGA gain", ANA_PGA3_CTRL, ADC3_ANALOG_PGA, 0x1f, 0, 1, tlv_adc_pga_gain),
316 /*0x93: Analog PGA4 Control Register*/
317 SOC_AC108_SINGLE_TLV("ADC4 PGA gain", ANA_PGA4_CTRL, ADC4_ANALOG_PGA, 0x1f, 0, 1, tlv_adc_pga_gain),
320 /*0x70: ADC1 Digital Channel Volume Control Register*/
321 SOC_AC108_SINGLE_TLV("CH5 digital volume", ADC1_DVOL_CTRL, 0, 0xff, 0, 0, tlv_ch_digital_vol),
322 /*0x71: ADC2 Digital Channel Volume Control Register*/
323 SOC_AC108_SINGLE_TLV("CH6 digital volume", ADC2_DVOL_CTRL, 0, 0xff, 0, 0, tlv_ch_digital_vol),
324 /*0x72: ADC3 Digital Channel Volume Control Register*/
325 SOC_AC108_SINGLE_TLV("CH7 digital volume", ADC3_DVOL_CTRL, 0, 0xff, 0, 0, tlv_ch_digital_vol),
326 /*0x73: ADC4 Digital Channel Volume Control Register*/
327 SOC_AC108_SINGLE_TLV("CH8 digital volume", ADC4_DVOL_CTRL, 0, 0xff, 0, 0, tlv_ch_digital_vol),
329 /*0x90: Analog PGA1 Control Register*/
330 SOC_AC108_SINGLE_TLV("ADC5 PGA gain", ANA_PGA1_CTRL, ADC1_ANALOG_PGA, 0x1f, 0, 0, tlv_adc_pga_gain),
331 /*0x91: Analog PGA2 Control Register*/
332 SOC_AC108_SINGLE_TLV("ADC6 PGA gain", ANA_PGA2_CTRL, ADC2_ANALOG_PGA, 0x1f, 0, 0, tlv_adc_pga_gain),
333 /*0x92: Analog PGA3 Control Register*/
334 SOC_AC108_SINGLE_TLV("ADC7 PGA gain", ANA_PGA3_CTRL, ADC3_ANALOG_PGA, 0x1f, 0, 0, tlv_adc_pga_gain),
335 /*0x93: Analog PGA4 Control Register*/
336 SOC_AC108_SINGLE_TLV("ADC8 PGA gain", ANA_PGA4_CTRL, ADC4_ANALOG_PGA, 0x1f, 0, 0, tlv_adc_pga_gain),
340 static const struct snd_soc_dapm_widget ac108_dapm_widgets[] = {
342 SND_SOC_DAPM_INPUT("MIC1P"),
343 SND_SOC_DAPM_INPUT("MIC1N"),
345 SND_SOC_DAPM_INPUT("MIC2P"),
346 SND_SOC_DAPM_INPUT("MIC2N"),
348 SND_SOC_DAPM_INPUT("MIC3P"),
349 SND_SOC_DAPM_INPUT("MIC3N"),
351 SND_SOC_DAPM_INPUT("MIC4P"),
352 SND_SOC_DAPM_INPUT("MIC4N"),
354 SND_SOC_DAPM_INPUT("DMIC1"),
355 SND_SOC_DAPM_INPUT("DMIC2"),
357 /*0xa0: ADC1 Analog Control 1 Register*/
358 /*0xa1-0xa6:use the defualt value*/
359 SND_SOC_DAPM_AIF_IN("Channel 1 AAF", "Capture", 0, ANA_ADC1_CTRL1, ADC1_DSM_ENABLE, 1),
360 SND_SOC_DAPM_SUPPLY("Channel 1 EN", ANA_ADC1_CTRL1, ADC1_PGA_ENABLE, 1, NULL, 0),
361 SND_SOC_DAPM_MICBIAS("MIC1BIAS", ANA_ADC1_CTRL1, ADC1_MICBIAS_EN, 1),
363 /*0xa7: ADC2 Analog Control 1 Register*/
364 /*0xa8-0xad:use the defualt value*/
365 SND_SOC_DAPM_AIF_IN("Channel 2 AAF", "Capture", 0, ANA_ADC2_CTRL1, ADC2_DSM_ENABLE, 1),
366 SND_SOC_DAPM_SUPPLY("Channel 2 EN", ANA_ADC2_CTRL1, ADC2_PGA_ENABLE, 1, NULL, 0),
367 SND_SOC_DAPM_MICBIAS("MIC2BIAS", ANA_ADC2_CTRL1, ADC2_MICBIAS_EN, 1),
369 /*0xae: ADC3 Analog Control 1 Register*/
370 /*0xaf-0xb4:use the defualt value*/
371 SND_SOC_DAPM_AIF_IN("Channel 3 AAF", "Capture", 0, ANA_ADC3_CTRL1, ADC3_DSM_ENABLE, 1),
372 SND_SOC_DAPM_SUPPLY("Channel 3 EN", ANA_ADC3_CTRL1, ADC3_PGA_ENABLE, 1, NULL, 0),
373 SND_SOC_DAPM_MICBIAS("MIC3BIAS", ANA_ADC3_CTRL1, ADC3_MICBIAS_EN, 1),
375 /*0xb5: ADC4 Analog Control 1 Register*/
376 /*0xb6-0xbb:use the defualt value*/
377 SND_SOC_DAPM_AIF_IN("Channel 4 AAF", "Capture", 0, ANA_ADC4_CTRL1, ADC4_DSM_ENABLE, 1),
378 SND_SOC_DAPM_SUPPLY("Channel 4 EN", ANA_ADC4_CTRL1, ADC4_PGA_ENABLE, 1, NULL, 0),
379 SND_SOC_DAPM_MICBIAS("MIC4BIAS", ANA_ADC4_CTRL1, ADC4_MICBIAS_EN, 1),
382 /*0x61: ADC Digital Part Enable Register*/
383 SND_SOC_DAPM_SUPPLY("ADC EN", ADC_DIG_EN, 4, 1, NULL, 0),
384 SND_SOC_DAPM_ADC("ADC1", "Capture", ADC_DIG_EN, 0, 1),
385 SND_SOC_DAPM_ADC("ADC2", "Capture", ADC_DIG_EN, 1, 1),
386 SND_SOC_DAPM_ADC("ADC3", "Capture", ADC_DIG_EN, 2, 1),
387 SND_SOC_DAPM_ADC("ADC4", "Capture", ADC_DIG_EN, 3, 1),
389 SND_SOC_DAPM_SUPPLY("ADC1 CLK", ANA_ADC4_CTRL7, ADC1_CLK_GATING, 1, NULL, 0),
390 SND_SOC_DAPM_SUPPLY("ADC2 CLK", ANA_ADC4_CTRL7, ADC2_CLK_GATING, 1, NULL, 0),
391 SND_SOC_DAPM_SUPPLY("ADC3 CLK", ANA_ADC4_CTRL7, ADC3_CLK_GATING, 1, NULL, 0),
392 SND_SOC_DAPM_SUPPLY("ADC4 CLK", ANA_ADC4_CTRL7, ADC4_CLK_GATING, 1, NULL, 0),
394 SND_SOC_DAPM_SUPPLY("DSM EN", ANA_ADC4_CTRL6, DSM_DEMOFF, 1, NULL, 0),
396 /*0x62:Digital MIC Enable Register*/
397 SND_SOC_DAPM_MICBIAS("DMIC1 enable", DMIC_EN, 0, 0),
398 SND_SOC_DAPM_MICBIAS("DMIC2 enable", DMIC_EN, 1, 0),
401 static const struct snd_soc_dapm_route ac108_dapm_routes[] = {
403 { "ADC1", NULL, "Channel 1 AAF" },
404 { "ADC2", NULL, "Channel 2 AAF" },
405 { "ADC3", NULL, "Channel 3 AAF" },
406 { "ADC4", NULL, "Channel 4 AAF" },
408 { "Channel 1 AAF", NULL, "MIC1BIAS" },
409 { "Channel 2 AAF", NULL, "MIC2BIAS" },
410 { "Channel 3 AAF", NULL, "MIC3BIAS" },
411 { "Channel 4 AAF", NULL, "MIC4BIAS" },
413 { "MIC1BIAS", NULL, "ADC1 CLK" },
414 { "MIC2BIAS", NULL, "ADC2 CLK" },
415 { "MIC3BIAS", NULL, "ADC3 CLK" },
416 { "MIC4BIAS", NULL, "ADC4 CLK" },
419 { "ADC1 CLK", NULL, "DSM EN" },
420 { "ADC2 CLK", NULL, "DSM EN" },
421 { "ADC3 CLK", NULL, "DSM EN" },
422 { "ADC4 CLK", NULL, "DSM EN" },
425 { "DSM EN", NULL, "ADC EN" },
427 { "Channel 1 EN", NULL, "DSM EN" },
428 { "Channel 2 EN", NULL, "DSM EN" },
429 { "Channel 3 EN", NULL, "DSM EN" },
430 { "Channel 4 EN", NULL, "DSM EN" },
433 { "MIC1P", NULL, "Channel 1 EN" },
434 { "MIC1N", NULL, "Channel 1 EN" },
436 { "MIC2P", NULL, "Channel 2 EN" },
437 { "MIC2N", NULL, "Channel 2 EN" },
439 { "MIC3P", NULL, "Channel 3 EN" },
440 { "MIC3N", NULL, "Channel 3 EN" },
442 { "MIC4P", NULL, "Channel 4 EN" },
443 { "MIC4N", NULL, "Channel 4 EN" },
447 static int ac108_multi_write(u8 reg, u8 val, struct ac10x_priv *ac10x) {
449 for (i = 0; i < ac10x->codec_cnt; i++) {
450 ac10x_write(reg, val, ac10x->i2cmap[i]);
455 static int ac108_multi_update_bits(u8 reg, u8 mask, u8 val, struct ac10x_priv *ac10x) {
459 for (i = 0; i < ac10x->codec_cnt; i++) {
460 r |= ac10x_update_bits(reg, mask, val, ac10x->i2cmap[i]);
465 static unsigned int ac108_codec_read(struct snd_soc_codec *codec, unsigned int reg) {
467 struct ac10x_priv *ac10x = dev_get_drvdata(codec->dev);
468 /*read one chip is fine*/
469 ac10x_read(reg, &val_r, ac10x->i2cmap[_MASTER_INDEX]);
473 static int ac108_codec_write(struct snd_soc_codec *codec, unsigned int reg, unsigned int val) {
474 struct ac10x_priv *ac10x = dev_get_drvdata(codec->dev);
475 ac108_multi_write(reg, val, ac10x);
480 * The Power management related registers are Reg01h~Reg09h
481 * 0x01-0x05,0x08,use the default value
482 * @author baozhu (17-6-21)
486 static void ac108_configure_power(struct ac10x_priv *ac10x) {
488 * 0x06:Enable Analog LDO
490 ac108_multi_update_bits(PWR_CTRL6, 0x01 << LDO33ANA_ENABLE, 0x01 << LDO33ANA_ENABLE, ac10x);
493 * Control VREF output and micbias voltage ?
494 * REF faststart disable, enable Enable VREF (needed for Analog
497 ac108_multi_update_bits(PWR_CTRL7, 0x1f << VREF_SEL | 0x01 << VREF_FASTSTART_ENABLE | 0x01 << VREF_ENABLE,
498 0x13 << VREF_SEL | 0x00 << VREF_FASTSTART_ENABLE | 0x01 << VREF_ENABLE, ac10x);
501 * Disable fast-start circuit on VREFP
502 * VREFP_RESCTRL=00=1 MOhm
504 * Enable VREFP (needed by all audio input channels)
506 ac108_multi_update_bits(PWR_CTRL9, 0x01 << VREFP_FASTSTART_ENABLE | 0x03 << VREFP_RESCTRL | 0x07 << IGEN_TRIM | 0x01 << VREFP_ENABLE,
507 0x00 << VREFP_FASTSTART_ENABLE | 0x00 << VREFP_RESCTRL | 0x04 << IGEN_TRIM | 0x01 << VREFP_ENABLE,
512 * The clock management related registers are Reg20h~Reg25h
513 * The PLL management related registers are Reg10h~Reg18h.
514 * @author baozhu (17-6-20)
517 * @param rate : sample rate
519 * @return int : fail or success
521 static int ac108_config_pll(struct ac10x_priv *ac10x, unsigned rate, unsigned lrck_ratio) {
523 struct pll_div ac108_pll_div = { 0 };
525 if (ac10x->clk_id == SYSCLK_SRC_PLL) {
526 unsigned pll_src, pll_freq_in;
528 if (lrck_ratio == 0) {
529 /* PLL clock source from MCLK */
530 pll_freq_in = ac10x->sysclk;
533 /* PLL clock source from BCLK */
534 pll_freq_in = rate * lrck_ratio;
538 /* FOUT =(FIN * N) / [(M1+1) * (M2+1)*(K1+1)*(K2+1)] */
539 for (i = 0; i < ARRAY_SIZE(ac108_pll_div_list); i++) {
540 if (ac108_pll_div_list[i].freq_in == pll_freq_in && ac108_pll_div_list[i].freq_out % rate == 0) {
541 ac108_pll_div = ac108_pll_div_list[i];
542 dev_dbg(&ac10x->i2c[_MASTER_INDEX]->dev, "AC108 PLL freq_in match:%u, freq_out:%u\n\n",
543 ac108_pll_div.freq_in, ac108_pll_div.freq_out);
547 /* 0x11,0x12,0x13,0x14: Config PLL DIV param M1/M2/N/K1/K2 */
548 ac108_multi_update_bits(PLL_CTRL5, 0x1f << PLL_POSTDIV1 | 0x01 << PLL_POSTDIV2,
549 ac108_pll_div.k1 << PLL_POSTDIV1 | ac108_pll_div.k2 << PLL_POSTDIV2, ac10x);
550 ac108_multi_update_bits(PLL_CTRL4, 0xff << PLL_LOOPDIV_LSB, (unsigned char)ac108_pll_div.n << PLL_LOOPDIV_LSB, ac10x);
551 ac108_multi_update_bits(PLL_CTRL3, 0x03 << PLL_LOOPDIV_MSB, (ac108_pll_div.n >> 8) << PLL_LOOPDIV_MSB, ac10x);
552 ac108_multi_update_bits(PLL_CTRL2, 0x1f << PLL_PREDIV1 | 0x01 << PLL_PREDIV2,
553 ac108_pll_div.m1 << PLL_PREDIV1 | ac108_pll_div.m2 << PLL_PREDIV2, ac10x);
555 /*0x18: PLL clk lock enable*/
556 ac108_multi_update_bits(PLL_LOCK_CTRL, 0x1 << PLL_LOCK_EN, 0x1 << PLL_LOCK_EN, ac10x);
559 * 0x20: enable pll, pll source from mclk/bclk, sysclk source from pll, enable sysclk
561 ac108_multi_update_bits(SYSCLK_CTRL, 0x01 << PLLCLK_EN | 0x03 << PLLCLK_SRC | 0x01 << SYSCLK_SRC | 0x01 << SYSCLK_EN,
562 0x01 << PLLCLK_EN |pll_src<< PLLCLK_SRC | 0x01 << SYSCLK_SRC | 0x01 << SYSCLK_EN, ac10x);
563 ac10x->mclk = ac108_pll_div.freq_out;
565 if (ac10x->clk_id == SYSCLK_SRC_MCLK) {
567 *0x20: sysclk source from mclk, enable sysclk
569 ac108_multi_update_bits(SYSCLK_CTRL, 0x01 << PLLCLK_EN | 0x01 << SYSCLK_SRC | 0x01 << SYSCLK_EN,
570 0x00 << PLLCLK_EN | 0x00 << SYSCLK_SRC | 0x01 << SYSCLK_EN, ac10x);
571 ac10x->mclk = ac10x->sysclk;
578 * support no more than 16 slots.
580 static int ac108_multi_chips_slots(struct ac10x_priv *ac, int slots) {
584 * codec0 enable slots 2,3,0,1 when 1 codec
586 * codec0 enable slots 6,7,0,1 when 2 codec
587 * codec1 enable slots 2,3,4,5
591 for (i = 0; i < ac->codec_cnt; i++) {
592 /* rotate map, due to channels rotated by CPU_DAI */
593 const unsigned vec_mask[] = {
594 0x3 << 6 | 0x3, // slots 6,7,0,1
595 0xF << 2, // slots 2,3,4,5
599 const unsigned vec_maps[] = {
602 * mic 0 sample -> slot 6
603 * mic 1 sample -> slot 7
604 * mic 2 sample -> slot 0
605 * mic 3 sample -> slot 1
607 0x0 << 12 | 0x1 << 14 | 0x2 << 0 | 0x3 << 2,
610 * mic 0 sample -> slot 2
611 * mic 1 sample -> slot 3
612 * mic 2 sample -> slot 4
613 * mic 3 sample -> slot 5
615 0x0 << 4 | 0x1 << 6 | 0x2 << 8 | 0x3 << 10,
621 /* 0x38-0x3A I2S_TX1_CTRLx */
622 if (ac->codec_cnt == 1) {
627 ac10x_write(I2S_TX1_CTRL1, slots - 1, ac->i2cmap[i]);
628 ac10x_write(I2S_TX1_CTRL2, (vec >> 0) & 0xFF, ac->i2cmap[i]);
629 ac10x_write(I2S_TX1_CTRL3, (vec >> 8) & 0xFF, ac->i2cmap[i]);
631 /* 0x3C-0x3F I2S_TX1_CHMP_CTRLx */
632 if (ac->codec_cnt == 1) {
633 vec = (0x2 << 0 | 0x3 << 2 | 0x0 << 4 | 0x1 << 6);
634 } else if (ac->codec_cnt == 2) {
638 ac10x_write(I2S_TX1_CHMP_CTRL1, (vec >> 0) & 0xFF, ac->i2cmap[i]);
639 ac10x_write(I2S_TX1_CHMP_CTRL2, (vec >> 8) & 0xFF, ac->i2cmap[i]);
640 ac10x_write(I2S_TX1_CHMP_CTRL3, (vec >> 16) & 0xFF, ac->i2cmap[i]);
641 ac10x_write(I2S_TX1_CHMP_CTRL4, (vec >> 24) & 0xFF, ac->i2cmap[i]);
646 static int ac108_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params, struct snd_soc_dai *dai) {
647 unsigned int i, channels, samp_res, rate;
648 struct snd_soc_codec *codec = dai->codec;
649 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
654 dev_dbg(dai->dev, "%s() stream=%s play:%d capt:%d +++\n", __func__,
655 snd_pcm_stream_str(substream),
656 dai->playback_active, dai->capture_active);
659 ret = ac101_hw_params(substream, params, dai);
661 dev_dbg(dai->dev, "%s() L%d returned\n", __func__, __LINE__);
662 /* not configure hw_param twice */
667 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE && dai->playback_active)
668 || (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && dai->capture_active)) {
669 /* not configure hw_param twice */
673 channels = params_channels(params);
675 /* Master mode, to clear cpu_dai fifos, output bclk without lrck */
676 ac10x_read(I2S_CTRL, &v, ac10x->i2cmap[_MASTER_INDEX]);
677 if (v & (0x01 << BCLK_IOEN)) {
678 ac10x_update_bits(I2S_CTRL, 0x1 << LRCK_IOEN, 0x0 << LRCK_IOEN, ac10x->i2cmap[_MASTER_INDEX]);
681 switch (params_format(params)) {
682 case SNDRV_PCM_FORMAT_S8:
685 case SNDRV_PCM_FORMAT_S16_LE:
688 case SNDRV_PCM_FORMAT_S20_3LE:
691 case SNDRV_PCM_FORMAT_S24_LE:
694 case SNDRV_PCM_FORMAT_S32_LE:
698 pr_err("AC108 don't supported the sample resolution: %u\n", params_format(params));
702 for (i = 0; i < ARRAY_SIZE(ac108_sample_rate); i++) {
703 if (ac108_sample_rate[i].real_val == params_rate(params) / (ac10x->data_protocol + 1UL)) {
708 if (i >= ARRAY_SIZE(ac108_sample_rate)) {
712 if (channels == 8 && ac108_sample_rate[rate].real_val == 96000) {
713 /* 24.576M bit clock is not support by ac108 */
717 dev_dbg(dai->dev, "rate: %d , channels: %d , samp_res: %d",
718 ac108_sample_rate[rate].real_val,
720 ac108_samp_res[samp_res].real_val);
724 * The 8-Low bit of LRCK period value. It is used to program
725 * the number of BCLKs per channel of sample frame. This value
726 * is interpreted as follow:
727 * The 8-Low bit of LRCK period value. It is used to program
728 * the number of BCLKs per channel of sample frame. This value
729 * is interpreted as follow: PCM mode: Number of BCLKs within
730 * (Left + Right) channel width I2S / Left-Justified /
731 * Right-Justified mode: Number of BCLKs within each individual
732 * channel width (Left or Right) N+1
734 * n = 7: 8 BCLK width
736 * n = 1023: 1024 BCLKs width
738 * The 2-High bit of LRCK period value.
740 if (ac10x->i2s_mode != PCM_FORMAT) {
741 if (ac10x->data_protocol) {
742 ac108_multi_write(I2S_LRCK_CTRL2, ac108_samp_res[samp_res].real_val - 1, ac10x);
743 /*encoding mode, the max LRCK period value < 32,so the 2-High bit is zero*/
744 ac108_multi_update_bits(I2S_LRCK_CTRL1, 0x03 << 0, 0x00, ac10x);
746 /*TDM mode or normal mode*/
747 ac108_multi_update_bits(I2S_LRCK_CTRL1, 0x03 << 0, 0x00, ac10x);
753 /*TDM mode or normal mode*/
754 div = ac108_samp_res[samp_res].real_val * channels - 1;
755 ac108_multi_write(I2S_LRCK_CTRL2, (div & 0xFF), ac10x);
756 ac108_multi_update_bits(I2S_LRCK_CTRL1, 0x03 << 0, (div >> 8) << 0, ac10x);
761 * TX Encoding mode will add 4bits to mark channel number
762 * TODO: need a chat to explain this
764 ac108_multi_update_bits(I2S_FMT_CTRL2, 0x07 << SAMPLE_RESOLUTION | 0x07 << SLOT_WIDTH_SEL,
765 ac108_samp_res[samp_res].reg_val << SAMPLE_RESOLUTION
766 | ac108_samp_res[samp_res].reg_val << SLOT_WIDTH_SEL, ac10x);
770 * ADC Sample Rate synchronised with I2S1 clock zone
772 ac108_multi_update_bits(ADC_SPRC, 0x0f << ADC_FS_I2S1, ac108_sample_rate[rate].reg_val << ADC_FS_I2S1, ac10x);
773 ac108_multi_write(HPF_EN, 0x0F, ac10x);
775 if (ac10x->i2c101 && _MASTER_MULTI_CODEC == _MASTER_AC101) {
776 ac108_config_pll(ac10x, ac108_sample_rate[rate].real_val, ac108_samp_res[samp_res].real_val * channels);
778 ac108_config_pll(ac10x, ac108_sample_rate[rate].real_val, 0);
784 bclkdiv = ac10x->mclk / (ac108_sample_rate[rate].real_val * channels * ac108_samp_res[samp_res].real_val);
785 for (i = 0; i < ARRAY_SIZE(ac108_bclkdivs) - 1; i++) {
786 if (ac108_bclkdivs[i] >= bclkdiv) {
790 ac108_multi_update_bits(I2S_BCLK_CTRL, 0x0F << BCLKDIV, i << BCLKDIV, ac10x);
793 * slots allocation for each chip
795 ac108_multi_chips_slots(ac10x, channels);
797 /*0x21: Module clock enable<I2S, ADC digital, MIC offset Calibration, ADC analog>*/
798 ac108_multi_write(MOD_CLK_EN, 1 << I2S | 1 << ADC_DIGITAL | 1 << MIC_OFFSET_CALIBRATION | 1 << ADC_ANALOG, ac10x);
799 /*0x22: Module reset de-asserted<I2S, ADC digital, MIC offset Calibration, ADC analog>*/
800 ac108_multi_write(MOD_RST_CTRL, 1 << I2S | 1 << ADC_DIGITAL | 1 << MIC_OFFSET_CALIBRATION | 1 << ADC_ANALOG, ac10x);
803 dev_dbg(dai->dev, "%s() stream=%s ---\n", __func__,
804 snd_pcm_stream_str(substream));
809 static int ac108_set_sysclk(struct snd_soc_dai *dai, int clk_id, unsigned int freq, int dir) {
811 struct ac10x_priv *ac10x = snd_soc_dai_get_drvdata(dai);
814 clk_id = SYSCLK_SRC_PLL;
816 pr_info("%s :%d\n", __FUNCTION__, freq);
819 case SYSCLK_SRC_MCLK:
820 ac108_multi_update_bits(SYSCLK_CTRL, 0x1 << SYSCLK_SRC, SYSCLK_SRC_MCLK << SYSCLK_SRC, ac10x);
823 ac108_multi_update_bits(SYSCLK_CTRL, 0x1 << SYSCLK_SRC, SYSCLK_SRC_PLL << SYSCLK_SRC, ac10x);
828 ac10x->sysclk = freq;
829 ac10x->clk_id = clk_id;
835 * The i2s format management related registers are Reg
837 * 33h,35h will be set in ac108_hw_params, It's BCLK width and
839 * @author baozhu (17-6-20)
846 static int ac108_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) {
847 unsigned char tx_offset, lrck_polarity, brck_polarity;
848 struct ac10x_priv *ac10x = dev_get_drvdata(dai->dev);
850 dev_dbg(dai->dev, "%s\n", __FUNCTION__);
852 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
853 case SND_SOC_DAIFMT_CBM_CFM: /*AC108 Master*/
854 if (! ac10x->i2c101 || _MASTER_MULTI_CODEC == _MASTER_AC108) {
855 dev_dbg(dai->dev, "AC108 set to work as Master\n");
857 * 0x30:chip is master mode ,BCLK & LRCK output
859 ac108_multi_update_bits(I2S_CTRL, 0x03 << LRCK_IOEN | 0x03 << SDO1_EN | 0x1 << TXEN | 0x1 << GEN,
860 0x00 << LRCK_IOEN | 0x03 << SDO1_EN | 0x1 << TXEN | 0x1 << GEN, ac10x);
861 /* multi_chips: only one chip set as Master, and the others also need to set as Slave */
862 ac10x_update_bits(I2S_CTRL, 0x3 << LRCK_IOEN, 0x01 << BCLK_IOEN, ac10x->i2cmap[_MASTER_INDEX]);
865 /* TODO: Both cpu_dai and codec_dai(AC108) be set as slave in DTS */
866 dev_dbg(dai->dev, "used as slave when AC101 is master\n");
868 case SND_SOC_DAIFMT_CBS_CFS: /*AC108 Slave*/
869 dev_dbg(dai->dev, "AC108 set to work as Slave\n");
871 * 0x30:chip is slave mode, BCLK & LRCK input,enable SDO1_EN and
872 * SDO2_EN, Transmitter Block Enable, Globe Enable
874 ac108_multi_update_bits(I2S_CTRL, 0x03 << LRCK_IOEN | 0x03 << SDO1_EN | 0x1 << TXEN | 0x1 << GEN,
875 0x00 << LRCK_IOEN | 0x03 << SDO1_EN | 0x0 << TXEN | 0x0 << GEN, ac10x);
878 pr_err("AC108 Master/Slave mode config error:%u\n\n", (fmt & SND_SOC_DAIFMT_MASTER_MASK) >> 12);
882 /*AC108 config I2S/LJ/RJ/PCM format*/
883 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
884 case SND_SOC_DAIFMT_I2S:
885 dev_dbg(dai->dev, "AC108 config I2S format\n");
886 ac10x->i2s_mode = LEFT_JUSTIFIED_FORMAT;
889 case SND_SOC_DAIFMT_RIGHT_J:
890 dev_dbg(dai->dev, "AC108 config RIGHT-JUSTIFIED format\n");
891 ac10x->i2s_mode = RIGHT_JUSTIFIED_FORMAT;
894 case SND_SOC_DAIFMT_LEFT_J:
895 dev_dbg(dai->dev, "AC108 config LEFT-JUSTIFIED format\n");
896 ac10x->i2s_mode = LEFT_JUSTIFIED_FORMAT;
899 case SND_SOC_DAIFMT_DSP_A:
900 dev_dbg(dai->dev, "AC108 config PCM-A format\n");
901 ac10x->i2s_mode = PCM_FORMAT;
904 case SND_SOC_DAIFMT_DSP_B:
905 dev_dbg(dai->dev, "AC108 config PCM-B format\n");
906 ac10x->i2s_mode = PCM_FORMAT;
910 pr_err("AC108 I2S format config error:%u\n\n", fmt & SND_SOC_DAIFMT_FORMAT_MASK);
914 /*AC108 config BCLK&LRCK polarity*/
915 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
916 case SND_SOC_DAIFMT_NB_NF:
917 dev_dbg(dai->dev, "AC108 config BCLK&LRCK polarity: BCLK_normal,LRCK_normal\n");
918 brck_polarity = BCLK_NORMAL_DRIVE_N_SAMPLE_P;
919 lrck_polarity = LRCK_LEFT_HIGH_RIGHT_LOW;
921 case SND_SOC_DAIFMT_NB_IF:
922 dev_dbg(dai->dev, "AC108 config BCLK&LRCK polarity: BCLK_normal,LRCK_invert\n");
923 brck_polarity = BCLK_NORMAL_DRIVE_N_SAMPLE_P;
924 lrck_polarity = LRCK_LEFT_LOW_RIGHT_HIGH;
926 case SND_SOC_DAIFMT_IB_NF:
927 dev_dbg(dai->dev, "AC108 config BCLK&LRCK polarity: BCLK_invert,LRCK_normal\n");
928 brck_polarity = BCLK_INVERT_DRIVE_P_SAMPLE_N;
929 lrck_polarity = LRCK_LEFT_HIGH_RIGHT_LOW;
931 case SND_SOC_DAIFMT_IB_IF:
932 dev_dbg(dai->dev, "AC108 config BCLK&LRCK polarity: BCLK_invert,LRCK_invert\n");
933 brck_polarity = BCLK_INVERT_DRIVE_P_SAMPLE_N;
934 lrck_polarity = LRCK_LEFT_LOW_RIGHT_HIGH;
937 pr_err("AC108 config BCLK/LRCLK polarity error:%u\n\n", (fmt & SND_SOC_DAIFMT_INV_MASK) >> 8);
941 ac108_configure_power(ac10x);
944 *0x31: 0: normal mode, negative edge drive and positive edge sample
945 1: invert mode, positive edge drive and negative edge sample
947 ac108_multi_update_bits(I2S_BCLK_CTRL, 0x01 << BCLK_POLARITY, brck_polarity << BCLK_POLARITY, ac10x);
951 ac108_multi_update_bits(I2S_LRCK_CTRL1, 0x01 << LRCK_POLARITY, lrck_polarity << LRCK_POLARITY, ac10x);
953 * 0x34:Encoding Mode Selection,Mode
954 * Selection,data is offset by 1 BCLKs to LRCK
955 * normal mode for the last half cycle of BCLK in the slot ?
956 * turn to hi-z state (TDM) when not transferring slot ?
958 ac108_multi_update_bits(I2S_FMT_CTRL1, 0x01 << ENCD_SEL | 0x03 << MODE_SEL | 0x01 << TX2_OFFSET |
959 0x01 << TX1_OFFSET | 0x01 << TX_SLOT_HIZ | 0x01 << TX_STATE,
960 ac10x->data_protocol << ENCD_SEL |
961 ac10x->i2s_mode << MODE_SEL |
962 tx_offset << TX2_OFFSET |
963 tx_offset << TX1_OFFSET |
964 0x00 << TX_SLOT_HIZ |
965 0x01 << TX_STATE, ac10x);
969 * MSB / LSB First Select: This driver only support MSB First Select .
970 * OUT2_MUTE,OUT1_MUTE shoule be set in widget.
971 * LRCK = 1 BCLK width
974 * TODO:pcm mode, bit[0:1] and bit[2] is special
976 ac108_multi_update_bits(I2S_FMT_CTRL3, 0x01 << TX_MLS | 0x03 << SEXT | 0x01 << LRCK_WIDTH | 0x03 << TX_PDM,
977 0x00 << TX_MLS | 0x03 << SEXT | 0x00 << LRCK_WIDTH | 0x00 << TX_PDM, ac10x);
979 ac108_multi_write(HPF_EN, 0x00, ac10x);
982 return ac101_set_dai_fmt(dai, fmt);
988 * due to miss channels order in cpu_dai, we meed defer the clock starting.
990 static int ac108_set_clock(int y_start_n_stop) {
994 dev_dbg(ac10x->codec->dev, "%s() L%d cmd:%d\n", __func__, __LINE__, y_start_n_stop);
996 /* spin_lock move to machine trigger */
998 if (y_start_n_stop && ac10x->sysclk_en == 0) {
999 /* enable lrck clock */
1000 ac10x_read(I2S_CTRL, ®, ac10x->i2cmap[_MASTER_INDEX]);
1001 if (reg & (0x01 << BCLK_IOEN)) {
1002 ret = ret || ac10x_update_bits(I2S_CTRL, 0x03 << LRCK_IOEN, 0x03 << LRCK_IOEN, ac10x->i2cmap[_MASTER_INDEX]);
1005 /*0x10: PLL Common voltage enable, PLL enable */
1006 ret = ret || ac108_multi_update_bits(PLL_CTRL1, 0x01 << PLL_EN | 0x01 << PLL_COM_EN,
1007 0x01 << PLL_EN | 0x01 << PLL_COM_EN, ac10x);
1008 /* enable global clock */
1009 ret = ret || ac108_multi_update_bits(I2S_CTRL, 0x1 << TXEN | 0x1 << GEN, 0x1 << TXEN | 0x1 << GEN, ac10x);
1011 ac10x->sysclk_en = 1UL;
1012 } else if (!y_start_n_stop && ac10x->sysclk_en != 0) {
1013 /* disable global clock */
1014 ret = ret || ac108_multi_update_bits(I2S_CTRL, 0x1 << TXEN | 0x1 << GEN, 0x0 << TXEN | 0x0 << GEN, ac10x);
1016 /*0x10: PLL Common voltage disable, PLL disable */
1017 ret = ret || ac108_multi_update_bits(PLL_CTRL1, 0x01 << PLL_EN | 0x01 << PLL_COM_EN,
1018 0x00 << PLL_EN | 0x00 << PLL_COM_EN, ac10x);
1020 /* disable lrck clock if it's enabled */
1021 ac10x_read(I2S_CTRL, ®, ac10x->i2cmap[_MASTER_INDEX]);
1022 if (reg & (0x01 << LRCK_IOEN)) {
1023 ret = ret || ac10x_update_bits(I2S_CTRL, 0x03 << LRCK_IOEN, 0x01 << BCLK_IOEN, ac10x->i2cmap[_MASTER_INDEX]);
1026 ac10x->sysclk_en = 0UL;
1033 static int ac108_prepare(struct snd_pcm_substream *substream,
1034 struct snd_soc_dai *dai)
1036 dev_dbg(dai->dev, "%s() stream=%s\n",
1038 snd_pcm_stream_str(substream));
1043 static int ac108_trigger(struct snd_pcm_substream *substream, int cmd,
1044 struct snd_soc_dai *dai)
1046 struct snd_soc_codec *codec = dai->codec;
1047 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1048 unsigned long flags;
1052 dev_dbg(dai->dev, "%s() stream=%s cmd=%d\n",
1054 snd_pcm_stream_str(substream),
1057 spin_lock_irqsave(&ac10x->lock, flags);
1059 if (ac10x->i2c101 && _MASTER_MULTI_CODEC == _MASTER_AC101) {
1060 ac101_trigger(substream, cmd, dai);
1061 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1067 case SNDRV_PCM_TRIGGER_START:
1068 case SNDRV_PCM_TRIGGER_RESUME:
1069 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1070 /* disable global clock if lrck disabled */
1071 ac10x_read(I2S_CTRL, &r, ac10x->i2cmap[_MASTER_INDEX]);
1072 if ((r & (0x01 << BCLK_IOEN)) && (r & (0x01 << LRCK_IOEN)) == 0) {
1073 /* disable global clock */
1074 ac108_multi_update_bits(I2S_CTRL, 0x1 << TXEN | 0x1 << GEN, 0x0 << TXEN | 0x0 << GEN, ac10x);
1077 /* delayed clock starting, move to machine trigger() */
1079 case SNDRV_PCM_TRIGGER_STOP:
1080 case SNDRV_PCM_TRIGGER_SUSPEND:
1081 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1088 spin_unlock_irqrestore(&ac10x->lock, flags);
1093 int ac108_audio_startup(struct snd_pcm_substream *substream,
1094 struct snd_soc_dai *dai
1096 struct snd_soc_codec *codec = dai->codec;
1097 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1099 if (ac10x->i2c101) {
1100 return ac101_audio_startup(substream, dai);
1105 void ac108_aif_shutdown(struct snd_pcm_substream *substream,
1106 struct snd_soc_dai *dai
1108 struct snd_soc_codec *codec = dai->codec;
1109 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1111 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
1112 /*0x21: Module clock disable <I2S, ADC digital, MIC offset Calibration, ADC analog>*/
1113 ac108_multi_write(MOD_CLK_EN, 0x0, ac10x);
1114 /*0x22: Module reset asserted <I2S, ADC digital, MIC offset Calibration, ADC analog>*/
1115 ac108_multi_write(MOD_RST_CTRL, 0x0, ac10x);
1118 if (ac10x->i2c101) {
1119 ac101_aif_shutdown(substream, dai);
1123 int ac108_aif_mute(struct snd_soc_dai *dai, int mute) {
1124 struct snd_soc_codec *codec = dai->codec;
1125 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1127 if (ac10x->i2c101) {
1128 return ac101_aif_mute(dai, mute);
1133 static const struct snd_soc_dai_ops ac108_dai_ops = {
1134 .startup = ac108_audio_startup,
1135 .shutdown = ac108_aif_shutdown,
1137 /*DAI clocking configuration*/
1138 .set_sysclk = ac108_set_sysclk,
1140 /*ALSA PCM audio operations*/
1141 .hw_params = ac108_hw_params,
1142 .prepare = ac108_prepare,
1143 .trigger = ac108_trigger,
1144 .digital_mute = ac108_aif_mute,
1146 /*DAI format configuration*/
1147 .set_fmt = ac108_set_fmt,
1149 // .hw_free = ac108_hw_free,
1152 static struct snd_soc_dai_driver ac108_dai0 = {
1153 .name = "ac10x-codec0",
1156 .stream_name = "Playback",
1158 .channels_max = AC108_CHANNELS_MAX,
1159 .rates = AC108_RATES,
1160 .formats = AC108_FORMATS,
1164 .stream_name = "Capture",
1166 .channels_max = AC108_CHANNELS_MAX,
1167 .rates = AC108_RATES,
1168 .formats = AC108_FORMATS,
1170 .ops = &ac108_dai_ops,
1173 static struct snd_soc_dai_driver ac108_dai1 = {
1174 .name = "ac10x-codec1",
1177 .stream_name = "Playback",
1179 .channels_max = AC108_CHANNELS_MAX,
1180 .rates = AC108_RATES,
1181 .formats = AC108_FORMATS,
1185 .stream_name = "Capture",
1187 .channels_max = AC108_CHANNELS_MAX,
1188 .rates = AC108_RATES,
1189 .formats = AC108_FORMATS,
1191 .ops = &ac108_dai_ops,
1194 static struct snd_soc_dai_driver *ac108_dai[] = {
1199 static int ac108_add_widgets(struct snd_soc_codec *codec) {
1200 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1201 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
1202 const struct snd_kcontrol_new* snd_kcntl = ac108_snd_controls;
1203 int ctrl_cnt = ARRAY_SIZE(ac108_snd_controls);
1205 /* only register controls correspond to exist chips */
1206 if (ac10x->tdm_chips_cnt >= 2) {
1207 snd_kcntl = ac108tdm_snd_controls;
1208 ctrl_cnt = ARRAY_SIZE(ac108tdm_snd_controls);
1210 snd_soc_add_codec_controls(codec, snd_kcntl, ctrl_cnt);
1212 snd_soc_dapm_new_controls(dapm, ac108_dapm_widgets,ARRAY_SIZE(ac108_dapm_widgets));
1213 snd_soc_dapm_add_routes(dapm, ac108_dapm_routes, ARRAY_SIZE(ac108_dapm_routes));
1218 static int ac108_codec_probe(struct snd_soc_codec *codec) {
1219 spin_lock_init(&ac10x->lock);
1221 ac10x->codec = codec;
1222 dev_set_drvdata(codec->dev, ac10x);
1223 ac108_add_widgets(codec);
1225 if (ac10x->i2c101) {
1226 ac101_codec_probe(codec);
1232 static int ac108_set_bias_level(struct snd_soc_codec *codec, enum snd_soc_bias_level level) {
1233 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1235 dev_dbg(codec->dev, "AC108 level:%d\n", level);
1238 case SND_SOC_BIAS_ON:
1239 ac108_multi_update_bits(ANA_ADC1_CTRL1, 0x01 << ADC1_MICBIAS_EN, 0x01 << ADC1_MICBIAS_EN, ac10x);
1240 ac108_multi_update_bits(ANA_ADC2_CTRL1, 0x01 << ADC2_MICBIAS_EN, 0x01 << ADC2_MICBIAS_EN, ac10x);
1241 ac108_multi_update_bits(ANA_ADC3_CTRL1, 0x01 << ADC3_MICBIAS_EN, 0x01 << ADC3_MICBIAS_EN, ac10x);
1242 ac108_multi_update_bits(ANA_ADC4_CTRL1, 0x01 << ADC4_MICBIAS_EN, 0x01 << ADC4_MICBIAS_EN, ac10x);
1245 case SND_SOC_BIAS_PREPARE:
1246 /* Put the MICBIASes into regulating mode */
1249 case SND_SOC_BIAS_STANDBY:
1252 case SND_SOC_BIAS_OFF:
1253 ac108_multi_update_bits(ANA_ADC1_CTRL1, 0x01 << ADC1_MICBIAS_EN, 0x00 << ADC1_MICBIAS_EN, ac10x);
1254 ac108_multi_update_bits(ANA_ADC2_CTRL1, 0x01 << ADC2_MICBIAS_EN, 0x00 << ADC2_MICBIAS_EN, ac10x);
1255 ac108_multi_update_bits(ANA_ADC3_CTRL1, 0x01 << ADC3_MICBIAS_EN, 0x00 << ADC3_MICBIAS_EN, ac10x);
1256 ac108_multi_update_bits(ANA_ADC4_CTRL1, 0x01 << ADC4_MICBIAS_EN, 0x00 << ADC4_MICBIAS_EN, ac10x);
1260 if (ac10x->i2c101) {
1261 ac101_set_bias_level(codec, level);
1266 int ac108_codec_remove(struct snd_soc_codec *codec) {
1267 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1269 if (! ac10x->i2c101) {
1272 return ac101_codec_remove(codec);
1274 #if __NO_SND_SOC_CODEC_DRV
1275 void ac108_codec_remove_void(struct snd_soc_codec *codec) {
1276 ac108_codec_remove(codec);
1278 #define ac108_codec_remove ac108_codec_remove_void
1281 int ac108_codec_suspend(struct snd_soc_codec *codec) {
1282 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1285 for (i = 0; i < ac10x->codec_cnt; i++) {
1286 regcache_cache_only(ac10x->i2cmap[i], true);
1289 if (! ac10x->i2c101) {
1292 return ac101_codec_suspend(codec);
1295 int ac108_codec_resume(struct snd_soc_codec *codec) {
1296 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1299 /* Sync reg_cache with the hardware */
1300 for (i = 0; i < ac10x->codec_cnt; i++) {
1301 regcache_cache_only(ac10x->i2cmap[i], false);
1302 ret = regcache_sync(ac10x->i2cmap[i]);
1304 dev_err(codec->dev, "Failed to sync i2cmap%d register cache: %d\n", i, ret);
1305 regcache_cache_only(ac10x->i2cmap[i], true);
1309 if (! ac10x->i2c101) {
1312 return ac101_codec_resume(codec);
1315 static struct snd_soc_codec_driver ac10x_soc_codec_driver = {
1316 .probe = ac108_codec_probe,
1317 .remove = ac108_codec_remove,
1318 .suspend = ac108_codec_suspend,
1319 .resume = ac108_codec_resume,
1320 .set_bias_level = ac108_set_bias_level,
1321 .read = ac108_codec_read,
1322 .write = ac108_codec_write,
1325 static ssize_t ac108_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) {
1326 int val = 0, flag = 0;
1327 u8 i = 0, reg, num, value_w, value_r[4];
1329 val = simple_strtol(buf, NULL, 16);
1330 flag = (val >> 16) & 0xF;
1333 reg = (val >> 8) & 0xFF;
1334 value_w = val & 0xFF;
1335 ac108_multi_write(reg, value_w, ac10x);
1336 printk("Write 0x%02x to REG:0x%02x\n", value_w, reg);
1340 reg = (val >> 8) & 0xFF;
1342 printk("\nRead: start REG:0x%02x,count:0x%02x\n", reg, num);
1344 for (k = 0; k < ac10x->codec_cnt; k++) {
1345 regcache_cache_bypass(ac10x->i2cmap[k], true);
1349 memset(value_r, 0, sizeof value_r);
1351 for (k = 0; k < ac10x->codec_cnt; k++) {
1352 ac10x_read(reg, &value_r[k], ac10x->i2cmap[k]);
1354 if (ac10x->codec_cnt >= 2) {
1355 printk("REG[0x%02x]: 0x%02x 0x%02x", reg, value_r[0], value_r[1]);
1357 printk("REG[0x%02x]: 0x%02x", reg, value_r[0]);
1361 if ((++i == num) || (i % 4 == 0)) {
1365 for (k = 0; k < ac10x->codec_cnt; k++) {
1366 regcache_cache_bypass(ac10x->i2cmap[k], false);
1373 static ssize_t ac108_show(struct device *dev, struct device_attribute *attr, char *buf) {
1375 printk("echo flag|reg|val > ac108\n");
1376 printk("eg read star addres=0x06,count 0x10:echo 0610 >ac108\n");
1377 printk("eg write value:0xfe to address:0x06 :echo 106fe > ac108\n");
1380 return snprintf(buf, PAGE_SIZE,"echo flag|reg|val > ac108\n"
1381 "eg read star addres=0x06,count 0x10:echo 0610 >ac108\n"
1382 "eg write value:0xfe to address:0x06 :echo 106fe > ac108\n");
1386 static DEVICE_ATTR(ac108, 0644, ac108_show, ac108_store);
1387 static struct attribute *ac108_debug_attrs[] = {
1388 &dev_attr_ac108.attr,
1391 static struct attribute_group ac108_debug_attr_group = {
1392 .name = "ac108_debug",
1393 .attrs = ac108_debug_attrs,
1396 static const struct regmap_config ac108_regmap = {
1400 .max_register = 0xDF,
1401 .cache_type = REGCACHE_FLAT,
1403 static int ac108_i2c_probe(struct i2c_client *i2c, const struct i2c_device_id *i2c_id) {
1404 struct device_node *np = i2c->dev.of_node;
1405 unsigned int val = 0;
1408 if (ac10x == NULL) {
1409 ac10x = kzalloc(sizeof(struct ac10x_priv), GFP_KERNEL);
1410 if (ac10x == NULL) {
1411 dev_err(&i2c->dev, "Unable to allocate ac10x private data\n");
1416 index = (int)i2c_id->driver_data;
1417 if (index == AC101_I2C_ID) {
1418 ac10x->i2c101 = i2c;
1419 i2c_set_clientdata(i2c, ac10x);
1420 ret = ac101_probe(i2c, i2c_id);
1422 ac10x->i2c101 = NULL;
1428 ret = of_property_read_u32(np, "data-protocol", &val);
1430 pr_err("Please set data-protocol.\n");
1433 ac10x->data_protocol = val;
1435 if (of_property_read_u32(np, "tdm-chips-count", &val)) val = 1;
1436 ac10x->tdm_chips_cnt = val;
1438 pr_err(" ac10x i2c_id number: %d\n", index);
1439 pr_err(" ac10x data protocol: %d\n", ac10x->data_protocol);
1441 ac10x->i2c[index] = i2c;
1442 ac10x->i2cmap[index] = devm_regmap_init_i2c(i2c, &ac108_regmap);
1443 if (IS_ERR(ac10x->i2cmap[index])) {
1444 ret = PTR_ERR(ac10x->i2cmap[index]);
1445 dev_err(&i2c->dev, "Fail to initialize i2cmap%d I/O: %d\n", index, ret);
1450 * Writing this register with 0x12
1451 * will resets all register to their default state.
1453 regcache_cache_only(ac10x->i2cmap[index], false);
1454 ret = regmap_write(ac10x->i2cmap[index], CHIP_RST, CHIP_RST_VAL);
1457 /* sync regcache for FLAT type */
1458 ac10x_fill_regcache(&i2c->dev, ac10x->i2cmap[index]);
1461 pr_err(" ac10x codec count : %d\n", ac10x->codec_cnt);
1463 ret = sysfs_create_group(&i2c->dev.kobj, &ac108_debug_attr_group);
1465 pr_err("failed to create attr group\n");
1469 /* It's time to bind codec to i2c[_MASTER_INDEX] when all i2c are ready */
1470 if ((ac10x->codec_cnt != 0 && ac10x->tdm_chips_cnt < 2)
1471 || (ac10x->i2c[0] && ac10x->i2c[1] && ac10x->i2c101)) {
1472 seeed_voice_card_register_set_clock(SNDRV_PCM_STREAM_CAPTURE, ac108_set_clock);
1473 /* no playback stream */
1474 if (! ac10x->i2c101) {
1475 memset(&ac108_dai[_MASTER_INDEX]->playback, '\0', sizeof ac108_dai[_MASTER_INDEX]->playback);
1477 ret = snd_soc_register_codec(&ac10x->i2c[_MASTER_INDEX]->dev, &ac10x_soc_codec_driver,
1478 ac108_dai[_MASTER_INDEX], 1);
1480 dev_err(&i2c->dev, "Failed to register ac10x codec: %d\n", ret);
1486 static int ac108_i2c_remove(struct i2c_client *i2c) {
1487 if (ac10x->codec != NULL) {
1488 snd_soc_unregister_codec(&ac10x->i2c[_MASTER_INDEX]->dev);
1489 ac10x->codec = NULL;
1491 if (i2c == ac10x->i2c101) {
1492 ac101_remove(ac10x->i2c101);
1493 ac10x->i2c101 = NULL;
1497 if (i2c == ac10x->i2c[0]) {
1498 ac10x->i2c[0] = NULL;
1500 if (i2c == ac10x->i2c[1]) {
1501 ac10x->i2c[1] = NULL;
1504 sysfs_remove_group(&i2c->dev.kobj, &ac108_debug_attr_group);
1507 if (!ac10x->i2c[0] && !ac10x->i2c[1] && !ac10x->i2c101) {
1514 static const struct i2c_device_id ac108_i2c_id[] = {
1519 { "ac101", AC101_I2C_ID },
1522 MODULE_DEVICE_TABLE(i2c, ac108_i2c_id);
1524 static const struct of_device_id ac108_of_match[] = {
1525 { .compatible = "x-power,ac108_0", },
1526 { .compatible = "x-power,ac108_1", },
1527 { .compatible = "x-power,ac108_2", },
1528 { .compatible = "x-power,ac108_3", },
1529 { .compatible = "x-power,ac101", },
1532 MODULE_DEVICE_TABLE(of, ac108_of_match);
1534 static struct i2c_driver ac108_i2c_driver = {
1536 .name = "ac10x-codec",
1537 .of_match_table = ac108_of_match,
1539 .probe = ac108_i2c_probe,
1540 .remove = ac108_i2c_remove,
1541 .id_table = ac108_i2c_id,
1544 module_i2c_driver(ac108_i2c_driver);
1546 MODULE_DESCRIPTION("ASoC AC108 driver");
1547 MODULE_AUTHOR("Baozhu Zuo<zuobaozhu@gmail.com>");
1548 MODULE_LICENSE("GPL");