4 * (C) Copyright 2017-2018
5 * Seeed Technology Co., Ltd. <www.seeedstudio.com>
7 * PeterYang <linsheng.yang@seeed.cc>
9 * (C) Copyright 2014-2017
10 * Reuuimlla Technology Co., Ltd. <www.reuuimllatech.com>
12 * huangxin <huangxin@Reuuimllatech.com>
13 * liushaohua <liushaohua@allwinnertech.com>
15 * X-Powers AC101 codec driver
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of
20 * the License, or (at your option) any later version.
25 * use 'make DEBUG=1' to enable debugging
27 #include <linux/module.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
31 #include <sound/soc-dapm.h>
32 #include <sound/tlv.h>
33 #include <linux/i2c.h>
34 #include <linux/irq.h>
35 #include <linux/workqueue.h>
36 #include <linux/clk.h>
37 #include <linux/gpio/consumer.h>
38 #include <linux/regmap.h>
39 #include <linux/input.h>
40 #include <linux/delay.h>
41 #include "ac101_regs.h"
45 * *** To sync channels ***
47 * 1. disable clock in codec hw_params()
48 * 2. clear fifo in bcm2835 hw_params()
49 * 3. clear fifo in bcm2385 prepare()
50 * 4. enable RX in bcm2835 trigger()
51 * 5. enable clock in machine trigger()
54 /*Default initialize configuration*/
55 static bool speaker_double_used = 1;
56 static int double_speaker_val = 0x1B;
57 static int single_speaker_val = 0x19;
58 static int headset_val = 0x3B;
59 static int mainmic_val = 0x4;
60 static int headsetmic_val = 0x4;
61 static bool dmic_used = 0;
62 static int adc_digital_val = 0xb0b0;
63 static bool drc_used = false;
65 #define AC101_RATES (SNDRV_PCM_RATE_8000_96000 & \
66 ~(SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_64000 | \
67 SNDRV_PCM_RATE_88200))
68 #define AC101_FORMATS (/*SNDRV_PCM_FMTBIT_S16_LE | \
69 SNDRV_PCM_FMTBIT_S24_LE |*/ \
70 SNDRV_PCM_FMTBIT_S32_LE | \
73 static struct ac10x_priv* static_ac10x;
76 int ac101_read(struct snd_soc_codec *codec, unsigned reg) {
77 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
80 if ((r = regmap_read(ac10x->regmap101, reg, &v)) < 0) {
81 dev_err(codec->dev, "read reg %02X fail\n",
88 int ac101_write(struct snd_soc_codec *codec, unsigned reg, unsigned val) {
89 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
92 v = regmap_write(ac10x->regmap101, reg, val);
96 int ac101_update_bits(struct snd_soc_codec *codec, unsigned reg,
97 unsigned mask, unsigned value
99 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
102 v = regmap_update_bits(ac10x->regmap101, reg, mask, value);
108 #ifdef CONFIG_AC101_SWITCH_DETECT
109 /******************************************************************************/
110 /********************************switch****************************************/
111 /******************************************************************************/
112 #define KEY_HEADSETHOOK 226 /* key define */
113 #define HEADSET_FILTER_CNT (10)
116 * switch_hw_config:config the 53 codec register
118 static void switch_hw_config(struct snd_soc_codec *codec)
124 /*HMIC/MMIC BIAS voltage level select:2.5v*/
125 ac101_update_bits(codec, OMIXER_BST1_CTRL, (0xf<<BIASVOLTAGE), (0xf<<BIASVOLTAGE));
126 /*debounce when Key down or keyup*/
127 ac101_update_bits(codec, HMIC_CTRL1, (0xf<<HMIC_M), (0x0<<HMIC_M));
128 /*debounce when earphone plugin or pullout*/
129 ac101_update_bits(codec, HMIC_CTRL1, (0xf<<HMIC_N), (0x0<<HMIC_N));
130 /*Down Sample Setting Select: Downby 4,32Hz*/
131 ac101_update_bits(codec, HMIC_CTRL2, (0x3<<HMIC_SAMPLE_SELECT), (0x02<<HMIC_SAMPLE_SELECT));
132 /*Hmic_th2 for detecting Keydown or Keyup.*/
133 ac101_update_bits(codec, HMIC_CTRL2, (0x1f<<HMIC_TH2), (0x8<<HMIC_TH2));
134 /*Hmic_th1[4:0],detecting eraphone plugin or pullout*/
135 ac101_update_bits(codec, HMIC_CTRL2, (0x1f<<HMIC_TH1), (0x1<<HMIC_TH1));
136 /*Headset microphone BIAS working mode: when HBIASEN = 1 */
137 ac101_update_bits(codec, ADC_APC_CTRL, (0x1<<HBIASMOD), (0x1<<HBIASMOD));
138 /*Headset microphone BIAS Enable*/
139 ac101_update_bits(codec, ADC_APC_CTRL, (0x1<<HBIASEN), (0x1<<HBIASEN));
140 /*Headset microphone BIAS Current sensor & ADC Enable*/
141 ac101_update_bits(codec, ADC_APC_CTRL, (0x1<<HBIASADCEN), (0x1<<HBIASADCEN));
142 /*Earphone Plugin/out Irq Enable*/
143 ac101_update_bits(codec, HMIC_CTRL1, (0x1<<HMIC_PULLOUT_IRQ), (0x1<<HMIC_PULLOUT_IRQ));
144 ac101_update_bits(codec, HMIC_CTRL1, (0x1<<HMIC_PLUGIN_IRQ), (0x1<<HMIC_PLUGIN_IRQ));
146 /*Hmic KeyUp/key down Irq Enable*/
147 ac101_update_bits(codec, HMIC_CTRL1, (0x1<<HMIC_KEYDOWN_IRQ), (0x1<<HMIC_KEYDOWN_IRQ));
148 ac101_update_bits(codec, HMIC_CTRL1, (0x1<<HMIC_KEYUP_IRQ), (0x1<<HMIC_KEYUP_IRQ));
150 /*headphone calibration clock frequency select*/
151 ac101_update_bits(codec, SPKOUT_CTRL, (0x7<<HPCALICKS), (0x7<<HPCALICKS));
153 /*clear hmic interrupt */
155 ac101_write(codec, HMIC_STS, r);
161 * switch_status_update: update the switch state.
163 static void switch_status_update(struct ac10x_priv *ac10x)
165 AC101_DBG("ac10x->state:%d\n", ac10x->state);
167 input_report_switch(ac10x->inpdev, SW_HEADPHONE_INSERT, ac10x->state);
168 input_sync(ac10x->inpdev);
173 * work_cb_clear_irq: clear audiocodec pending and Record the interrupt.
175 static void work_cb_clear_irq(struct work_struct *work)
178 struct ac10x_priv *ac10x = container_of(work, struct ac10x_priv, work_clear_irq);
179 struct snd_soc_codec *codec = ac10x->codec;
183 reg_val = ac101_read(codec, HMIC_STS);
184 if (BIT(HMIC_PULLOUT_PEND) & reg_val) {
185 ac10x->pullout_cntr++;
186 AC101_DBG("ac10x->pullout_cntr: %d\n", ac10x->pullout_cntr);
189 reg_val |= HMIC_PEND_ALL;
190 ac101_write(codec, HMIC_STS, reg_val);
192 reg_val = ac101_read(codec, HMIC_STS);
193 if ((reg_val & HMIC_PEND_ALL) != 0){
194 reg_val |= HMIC_PEND_ALL;
195 ac101_write(codec, HMIC_STS, reg_val);
198 if (cancel_work_sync(&ac10x->work_switch) != 0) {
202 if (0 == schedule_work(&ac10x->work_switch)) {
204 AC101_DBG("[work_cb_clear_irq] add work struct failed!\n");
209 HBIAS_LEVEL_1 = 0x02,
210 HBIAS_LEVEL_2 = 0x0B,
211 HBIAS_LEVEL_3 = 0x13,
212 HBIAS_LEVEL_4 = 0x17,
213 HBIAS_LEVEL_5 = 0x19,
216 static int __ac101_get_hmic_data(struct snd_soc_codec *codec) {
222 d = GET_HMIC_DATA(ac101_read(codec, HMIC_STS));
224 r = 0x1 << HMIC_DATA_PEND;
225 ac101_write(codec, HMIC_STS, r);
227 /* prevent i2c accessing too frequently */
228 usleep_range(1500, 3000);
230 AC101_DBG("HMIC_DATA(%3ld): %02X\n", counter++, d);
235 * work_cb_earphone_switch: judge the status of the headphone
237 static void work_cb_earphone_switch(struct work_struct *work)
239 struct ac10x_priv *ac10x = container_of(work, struct ac10x_priv, work_switch);
240 struct snd_soc_codec *codec = ac10x->codec;
242 static int hook_flag1 = 0, hook_flag2 = 0;
243 static int KEY_VOLUME_FLAG = 0;
245 unsigned filter_buf = 0;
252 t = __ac101_get_hmic_data(codec);
254 if ((t >= HBIAS_LEVEL_2) && (ac10x->mode == FOUR_HEADPHONE_PLUGIN)) {
255 t = __ac101_get_hmic_data(codec);
257 if (t >= HBIAS_LEVEL_5){
259 t = __ac101_get_hmic_data(codec);
260 if (((t < HBIAS_LEVEL_2 && t >= HBIAS_LEVEL_1 - 1) || t >= HBIAS_LEVEL_5)
261 && (ac10x->pullout_cntr == 0)) {
262 input_report_key(ac10x->inpdev, KEY_HEADSETHOOK, 1);
263 input_sync(ac10x->inpdev);
265 AC101_DBG("KEY_HEADSETHOOK1\n");
267 if (hook_flag1 != hook_flag2)
268 hook_flag1 = hook_flag2 = 0;
271 if (ac10x->pullout_cntr)
272 ac10x->pullout_cntr--;
273 } else if (t >= HBIAS_LEVEL_4) {
275 t = __ac101_get_hmic_data(codec);
276 if (t < HBIAS_LEVEL_5 && t >= HBIAS_LEVEL_4 && (ac10x->pullout_cntr == 0)) {
278 input_report_key(ac10x->inpdev, KEY_VOLUMEUP, 1);
279 input_sync(ac10x->inpdev);
280 input_report_key(ac10x->inpdev, KEY_VOLUMEUP, 0);
281 input_sync(ac10x->inpdev);
283 AC101_DBG("HMIC_DATA: %d KEY_VOLUMEUP\n", t);
285 if (ac10x->pullout_cntr)
286 ac10x->pullout_cntr--;
287 } else if (t >= HBIAS_LEVEL_3){
289 t = __ac101_get_hmic_data(codec);
290 if (t < HBIAS_LEVEL_4 && t >= HBIAS_LEVEL_3 && (ac10x->pullout_cntr == 0)){
292 input_report_key(ac10x->inpdev, KEY_VOLUMEDOWN, 1);
293 input_sync(ac10x->inpdev);
294 input_report_key(ac10x->inpdev, KEY_VOLUMEDOWN, 0);
295 input_sync(ac10x->inpdev);
296 AC101_DBG("KEY_VOLUMEDOWN\n");
298 if (ac10x->pullout_cntr)
299 ac10x->pullout_cntr--;
301 } else if ((t < HBIAS_LEVEL_2 && t >= HBIAS_LEVEL_1) && (ac10x->mode == FOUR_HEADPHONE_PLUGIN)) {
302 t = __ac101_get_hmic_data(codec);
303 if (t < HBIAS_LEVEL_2 && t >= HBIAS_LEVEL_1) {
304 if (KEY_VOLUME_FLAG) {
307 if (hook_flag1 == (++hook_flag2)) {
308 hook_flag1 = hook_flag2 = 0;
309 input_report_key(ac10x->inpdev, KEY_HEADSETHOOK, 0);
310 input_sync(ac10x->inpdev);
312 AC101_DBG("KEY_HEADSETHOOK0\n");
316 while (ac10x->irq_cntr == 0 && ac10x->irq != 0) {
319 t = __ac101_get_hmic_data(codec);
321 if (filt_index <= HEADSET_FILTER_CNT) {
322 if (filt_index++ == 0) {
324 } else if (filter_buf != t) {
331 if (filter_buf >= HBIAS_LEVEL_2) {
332 ac10x->mode = THREE_HEADPHONE_PLUGIN;
334 } else if (filter_buf >= HBIAS_LEVEL_1 - 1) {
335 ac10x->mode = FOUR_HEADPHONE_PLUGIN;
338 ac10x->mode = HEADPHONE_IDLE;
341 switch_status_update(ac10x);
342 ac10x->pullout_cntr = 0;
349 * audio_hmic_irq: the interrupt handlers
351 static irqreturn_t audio_hmic_irq(int irq, void *para)
353 struct ac10x_priv *ac10x = (struct ac10x_priv *)para;
358 if (0 == schedule_work(&ac10x->work_clear_irq)){
359 AC101_DBG("[audio_hmic_irq] work already in queue_codec_irq, adding failed!\n");
364 static int ac101_switch_probe(struct ac10x_priv *ac10x) {
365 struct i2c_client *i2c = ac10x->i2c101;
368 ac10x->gpiod_irq = devm_gpiod_get_optional(&i2c->dev, "switch-irq", GPIOD_IN);
369 if (IS_ERR(ac10x->gpiod_irq)) {
370 ac10x->gpiod_irq = NULL;
371 dev_err(&i2c->dev, "failed get switch-irq in device tree\n");
375 gpiod_direction_input(ac10x->gpiod_irq);
377 ac10x->irq = gpiod_to_irq(ac10x->gpiod_irq);
378 if (IS_ERR_VALUE(ac10x->irq)) {
379 pr_info("[ac101] map gpio to irq failed, errno = %ld\n", ac10x->irq);
384 /* request irq, set irq type to falling edge trigger */
385 ret = devm_request_irq(ac10x->codec->dev, ac10x->irq, audio_hmic_irq, IRQF_TRIGGER_FALLING, "SWTICH_EINT", ac10x);
386 if (IS_ERR_VALUE(ret)) {
387 pr_info("[ac101] request virq %ld failed, errno = %ld\n", ac10x->irq, ret);
391 ac10x->mode = HEADPHONE_IDLE;
394 /*use for judge the state of switch*/
395 INIT_WORK(&ac10x->work_switch, work_cb_earphone_switch);
396 INIT_WORK(&ac10x->work_clear_irq, work_cb_clear_irq);
398 /********************create input device************************/
399 ac10x->inpdev = devm_input_allocate_device(ac10x->codec->dev);
400 if (!ac10x->inpdev) {
401 AC101_DBG("input_allocate_device: not enough memory for input device\n");
403 goto _err_input_allocate_device;
406 ac10x->inpdev->name = "seed-voicecard-headset";
407 ac10x->inpdev->phys = dev_name(ac10x->codec->dev);
408 ac10x->inpdev->id.bustype = BUS_I2C;
409 ac10x->inpdev->dev.parent = ac10x->codec->dev;
410 input_set_drvdata(ac10x->inpdev, ac10x->codec);
412 ac10x->inpdev->evbit[0] = BIT_MASK(EV_KEY) | BIT(EV_SW);
414 set_bit(KEY_HEADSETHOOK, ac10x->inpdev->keybit);
415 set_bit(KEY_VOLUMEUP, ac10x->inpdev->keybit);
416 set_bit(KEY_VOLUMEDOWN, ac10x->inpdev->keybit);
417 input_set_capability(ac10x->inpdev, EV_SW, SW_HEADPHONE_INSERT);
419 ret = input_register_device(ac10x->inpdev);
421 AC101_DBG("input_register_device: input_register_device failed\n");
422 goto _err_input_register_device;
425 /* the first headset state checking */
426 switch_hw_config(ac10x->codec);
428 schedule_work(&ac10x->work_switch);
432 _err_input_register_device:
433 _err_input_allocate_device:
436 devm_free_irq(&i2c->dev, ac10x->irq, ac10x);
442 /******************************************************************************/
443 /********************************switch****************************************/
444 /******************************************************************************/
449 void drc_config(struct snd_soc_codec *codec)
452 reg_val = ac101_read(codec, 0xa3);
453 reg_val &= ~(0x7ff<<0);
455 ac101_write(codec, 0xa3, reg_val);
456 ac101_write(codec, 0xa4, 0x2baf);
458 reg_val = ac101_read(codec, 0xa5);
459 reg_val &= ~(0x7ff<<0);
461 ac101_write(codec, 0xa5, reg_val);
462 ac101_write(codec, 0xa6, 0x2baf);
464 reg_val = ac101_read(codec, 0xa7);
465 reg_val &= ~(0x7ff<<0);
466 ac101_write(codec, 0xa7, reg_val);
467 ac101_write(codec, 0xa8, 0x44a);
469 reg_val = ac101_read(codec, 0xa9);
470 reg_val &= ~(0x7ff<<0);
471 ac101_write(codec, 0xa9, reg_val);
472 ac101_write(codec, 0xaa, 0x1e06);
474 reg_val = ac101_read(codec, 0xab);
475 reg_val &= ~(0x7ff<<0);
476 reg_val |= (0x352<<0);
477 ac101_write(codec, 0xab, reg_val);
478 ac101_write(codec, 0xac, 0x6910);
480 reg_val = ac101_read(codec, 0xad);
481 reg_val &= ~(0x7ff<<0);
482 reg_val |= (0x77a<<0);
483 ac101_write(codec, 0xad, reg_val);
484 ac101_write(codec, 0xae, 0xaaaa);
486 reg_val = ac101_read(codec, 0xaf);
487 reg_val &= ~(0x7ff<<0);
488 reg_val |= (0x2de<<0);
489 ac101_write(codec, 0xaf, reg_val);
490 ac101_write(codec, 0xb0, 0xc982);
492 ac101_write(codec, 0x16, 0x9f9f);
496 void drc_enable(struct snd_soc_codec *codec,bool on)
500 ac101_write(codec, 0xb5, 0xA080);
501 reg_val = ac101_read(codec, MOD_CLK_ENA);
503 ac101_write(codec, MOD_CLK_ENA, reg_val);
504 reg_val = ac101_read(codec, MOD_RST_CTRL);
506 ac101_write(codec, MOD_RST_CTRL, reg_val);
508 reg_val = ac101_read(codec, 0xa0);
510 ac101_write(codec, 0xa0, reg_val);
512 ac101_write(codec, 0xb5, 0x0);
513 reg_val = ac101_read(codec, MOD_CLK_ENA);
514 reg_val &= ~(0x1<<6);
515 ac101_write(codec, MOD_CLK_ENA, reg_val);
516 reg_val = ac101_read(codec, MOD_RST_CTRL);
517 reg_val &= ~(0x1<<6);
518 ac101_write(codec, MOD_RST_CTRL, reg_val);
520 reg_val = ac101_read(codec, 0xa0);
521 reg_val &= ~(0x7<<0);
522 ac101_write(codec, 0xa0, reg_val);
526 void set_configuration(struct snd_soc_codec *codec)
528 if (speaker_double_used) {
529 ac101_update_bits(codec, SPKOUT_CTRL, (0x1f<<SPK_VOL), (double_speaker_val<<SPK_VOL));
531 ac101_update_bits(codec, SPKOUT_CTRL, (0x1f<<SPK_VOL), (single_speaker_val<<SPK_VOL));
533 ac101_update_bits(codec, HPOUT_CTRL, (0x3f<<HP_VOL), (headset_val<<HP_VOL));
534 ac101_update_bits(codec, ADC_SRCBST_CTRL, (0x7<<ADC_MIC1G), (mainmic_val<<ADC_MIC1G));
535 ac101_update_bits(codec, ADC_SRCBST_CTRL, (0x7<<ADC_MIC2G), (headsetmic_val<<ADC_MIC2G));
537 ac101_write(codec, ADC_VOL_CTRL, adc_digital_val);
542 /*headphone calibration clock frequency select*/
543 ac101_update_bits(codec, SPKOUT_CTRL, (0x7<<HPCALICKS), (0x7<<HPCALICKS));
545 /* I2S1 DAC Timeslot 0 data <- I2S1 DAC channel 0 */
546 // "AIF1IN0L Mux" <= "AIF1DACL"
547 // "AIF1IN0R Mux" <= "AIF1DACR"
548 ac101_update_bits(codec, AIF1_DACDAT_CTRL, 0x3 << AIF1_DA0L_SRC, 0x0 << AIF1_DA0L_SRC);
549 ac101_update_bits(codec, AIF1_DACDAT_CTRL, 0x3 << AIF1_DA0R_SRC, 0x0 << AIF1_DA0R_SRC);
550 /* Timeslot 0 Left & Right Channel enable */
551 ac101_update_bits(codec, AIF1_DACDAT_CTRL, 0x3 << AIF1_DA0R_ENA, 0x3 << AIF1_DA0R_ENA);
553 /* DAC Digital Mixer Source Select <- I2S1 DA0 */
554 // "DACL Mixer" += "AIF1IN0L Mux"
555 // "DACR Mixer" += "AIF1IN0R Mux"
556 ac101_update_bits(codec, DAC_MXR_SRC, 0xF << DACL_MXR_ADCL, 0x8 << DACL_MXR_ADCL);
557 ac101_update_bits(codec, DAC_MXR_SRC, 0xF << DACR_MXR_ADCR, 0x8 << DACR_MXR_ADCR);
558 /* Internal DAC Analog Left & Right Channel enable */
559 ac101_update_bits(codec, OMIXER_DACA_CTRL, 0x3 << DACALEN, 0x3 << DACALEN);
561 /* Output Mixer Source Select */
562 // "Left Output Mixer" += "DACL Mixer"
563 // "Right Output Mixer" += "DACR Mixer"
564 ac101_update_bits(codec, OMIXER_SR, 0x1 << LMIXMUTEDACL, 0x1 << LMIXMUTEDACL);
565 ac101_update_bits(codec, OMIXER_SR, 0x1 << RMIXMUTEDACR, 0x1 << RMIXMUTEDACR);
566 /* Left & Right Analog Output Mixer enable */
567 ac101_update_bits(codec, OMIXER_DACA_CTRL, 0x3 << LMIXEN, 0x3 << LMIXEN);
569 /* Headphone Ouput Control */
570 // "HP_R Mux" <= "DACR Mixer"
571 // "HP_L Mux" <= "DACL Mixer"
572 ac101_update_bits(codec, HPOUT_CTRL, 0x1 << LHPS, 0x0 << LHPS);
573 ac101_update_bits(codec, HPOUT_CTRL, 0x1 << RHPS, 0x0 << RHPS);
575 /* Speaker Output Control */
576 // "SPK_L Mux" <= "SPK_LR Adder"
577 // "SPK_R Mux" <= "SPK_LR Adder"
578 ac101_update_bits(codec, SPKOUT_CTRL, (0x1 << LSPKS) | (0x1 << RSPKS), (0x1 << LSPKS) | (0x1 << RSPKS));
579 /* Enable Left & Right Speaker */
580 ac101_update_bits(codec, SPKOUT_CTRL, (0x1 << LSPK_EN) | (0x1 << RSPK_EN), (0x1 << LSPK_EN) | (0x1 << RSPK_EN));
584 static int late_enable_dac(struct snd_soc_codec* codec, int event) {
585 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
587 mutex_lock(&ac10x->dac_mutex);
589 case SND_SOC_DAPM_PRE_PMU:
591 if (ac10x->dac_enable == 0){
592 /*enable dac module clk*/
593 ac101_update_bits(codec, MOD_CLK_ENA, (0x1<<MOD_CLK_DAC_DIG), (0x1<<MOD_CLK_DAC_DIG));
594 ac101_update_bits(codec, MOD_RST_CTRL, (0x1<<MOD_RESET_DAC_DIG), (0x1<<MOD_RESET_DAC_DIG));
595 ac101_update_bits(codec, DAC_DIG_CTRL, (0x1<<ENDA), (0x1<<ENDA));
596 ac101_update_bits(codec, DAC_DIG_CTRL, (0x1<<ENHPF),(0x1<<ENHPF));
600 case SND_SOC_DAPM_POST_PMD:
601 if (ac10x->dac_enable != 0){
602 ac10x->dac_enable = 0;
604 ac101_update_bits(codec, DAC_DIG_CTRL, (0x1<<ENHPF),(0x0<<ENHPF));
605 ac101_update_bits(codec, DAC_DIG_CTRL, (0x1<<ENDA), (0x0<<ENDA));
606 /*disable dac module clk*/
607 ac101_update_bits(codec, MOD_CLK_ENA, (0x1<<MOD_CLK_DAC_DIG), (0x0<<MOD_CLK_DAC_DIG));
608 ac101_update_bits(codec, MOD_RST_CTRL, (0x1<<MOD_RESET_DAC_DIG), (0x0<<MOD_RESET_DAC_DIG));
612 mutex_unlock(&ac10x->dac_mutex);
616 static int ac101_headphone_event(struct snd_soc_codec* codec, int event) {
618 case SND_SOC_DAPM_POST_PMU:
620 AC101_DBG("post:open\n");
621 ac101_update_bits(codec, OMIXER_DACA_CTRL, (0xf<<HPOUTPUTENABLE), (0xf<<HPOUTPUTENABLE));
623 ac101_update_bits(codec, HPOUT_CTRL, (0x1<<HPPA_EN), (0x1<<HPPA_EN));
624 ac101_update_bits(codec, HPOUT_CTRL, (0x3<<LHPPA_MUTE), (0x3<<LHPPA_MUTE));
626 case SND_SOC_DAPM_PRE_PMD:
628 AC101_DBG("pre:close\n");
629 ac101_update_bits(codec, HPOUT_CTRL, (0x3<<LHPPA_MUTE), (0x0<<LHPPA_MUTE));
631 ac101_update_bits(codec, OMIXER_DACA_CTRL, (0xf<<HPOUTPUTENABLE), (0x0<<HPOUTPUTENABLE));
632 ac101_update_bits(codec, HPOUT_CTRL, (0x1<<HPPA_EN), (0x0<<HPPA_EN));
638 static int ac101_sysclk_started(void) {
641 reg_val = ac101_read(static_ac10x->codec, SYSCLK_CTRL);
642 return (reg_val & (0x1<<SYSCLK_ENA));
645 static int ac101_aif1clk(struct snd_soc_codec* codec, int event, int quick) {
646 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
650 case SND_SOC_DAPM_PRE_PMU:
651 if (ac10x->aif1_clken == 0){
652 ret = ac101_update_bits(codec, SYSCLK_CTRL, (0x1<<AIF1CLK_ENA), (0x1<<AIF1CLK_ENA));
653 if(!quick || _MASTER_MULTI_CODEC != _MASTER_AC101) {
654 /* enable aif1clk & sysclk */
655 ret = ret || ac101_update_bits(codec, MOD_CLK_ENA, (0x1<<MOD_CLK_AIF1), (0x1<<MOD_CLK_AIF1));
656 ret = ret || ac101_update_bits(codec, MOD_RST_CTRL, (0x1<<MOD_RESET_AIF1), (0x1<<MOD_RESET_AIF1));
658 ret = ret || ac101_update_bits(codec, SYSCLK_CTRL, (0x1<<SYSCLK_ENA), (0x1<<SYSCLK_ENA));
661 AC101_DBG("start sysclk failed\n");
663 AC101_DBG("hw sysclk enable\n");
668 case SND_SOC_DAPM_POST_PMD:
669 if (ac10x->aif1_clken != 0) {
670 /* disable aif1clk & sysclk */
671 ret = ac101_update_bits(codec, SYSCLK_CTRL, (0x1<<AIF1CLK_ENA),(0x0<<AIF1CLK_ENA));
672 ret = ret || ac101_update_bits(codec, MOD_CLK_ENA, (0x1<<MOD_CLK_AIF1), (0x0<<MOD_CLK_AIF1));
673 ret = ret || ac101_update_bits(codec, MOD_RST_CTRL, (0x1<<MOD_RESET_AIF1), (0x0<<MOD_RESET_AIF1));
674 ret = ret || ac101_update_bits(codec, SYSCLK_CTRL, (0x1<<SYSCLK_ENA), (0x0<<SYSCLK_ENA));
677 AC101_DBG("stop sysclk failed\n");
679 AC101_DBG("hw sysclk disable\n");
680 ac10x->aif1_clken = 0;
686 AC101_DBG("event=%d pre_up/%d post_down/%d\n", event, SND_SOC_DAPM_PRE_PMU, SND_SOC_DAPM_POST_PMD);
692 * snd_ac101_get_volsw - single mixer get callback
693 * @kcontrol: mixer control
694 * @ucontrol: control element information
696 * Callback to get the value of a single mixer control, or a double mixer
697 * control that spans 2 registers.
699 * Returns 0 for success.
701 static int snd_ac101_get_volsw(struct snd_kcontrol *kcontrol,
702 struct snd_ctl_elem_value *ucontrol
704 struct soc_mixer_control *mc =
705 (struct soc_mixer_control *)kcontrol->private_value;
706 unsigned int val, mask = (1 << fls(mc->max)) - 1;
707 unsigned int invert = mc->invert;
710 if ((ret = ac101_read(static_ac10x->codec, mc->reg)) < 0)
713 val = (ret >> mc->shift) & mask;
714 ucontrol->value.integer.value[0] = val - mc->min;
716 ucontrol->value.integer.value[0] =
717 mc->max - ucontrol->value.integer.value[0];
720 if (snd_soc_volsw_is_stereo(mc)) {
721 val = (ret >> mc->rshift) & mask;
722 ucontrol->value.integer.value[1] = val - mc->min;
724 ucontrol->value.integer.value[1] =
725 mc->max - ucontrol->value.integer.value[1];
732 * snd_ac101_put_volsw - single mixer put callback
733 * @kcontrol: mixer control
734 * @ucontrol: control element information
736 * Callback to set the value of a single mixer control, or a double mixer
737 * control that spans 2 registers.
739 * Returns 0 for success.
741 static int snd_ac101_put_volsw(struct snd_kcontrol *kcontrol,
742 struct snd_ctl_elem_value *ucontrol
744 struct soc_mixer_control *mc =
745 (struct soc_mixer_control *)kcontrol->private_value;
746 unsigned int sign_bit = mc->sign_bit;
747 unsigned int val, mask = (1 << fls(mc->max)) - 1;
748 unsigned int invert = mc->invert;
752 mask = BIT(sign_bit + 1) - 1;
754 val = ((ucontrol->value.integer.value[0] + mc->min) & mask);
759 ret = ac101_update_bits(static_ac10x->codec, mc->reg, mask << mc->shift, val << mc->shift);
761 if (! snd_soc_volsw_is_stereo(mc)) {
764 val = ((ucontrol->value.integer.value[1] + mc->min) & mask);
769 ret = ac101_update_bits(static_ac10x->codec, mc->reg, mask << mc->rshift, val << mc->rshift);
774 static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -11925, 75, 0);
775 static const DECLARE_TLV_DB_SCALE(dac_mix_vol_tlv, -600, 600, 0);
776 static const DECLARE_TLV_DB_SCALE(dig_vol_tlv, -7308, 116, 0);
777 static const DECLARE_TLV_DB_SCALE(speaker_vol_tlv, -4800, 150, 0);
778 static const DECLARE_TLV_DB_SCALE(headphone_vol_tlv, -6300, 100, 0);
780 static struct snd_kcontrol_new ac101_controls[] = {
782 SOC_DOUBLE_TLV("DAC volume", DAC_VOL_CTRL, DAC_VOL_L, DAC_VOL_R, 0xff, 0, dac_vol_tlv),
783 SOC_DOUBLE_TLV("DAC mixer gain", DAC_MXR_GAIN, DACL_MXR_GAIN, DACR_MXR_GAIN, 0xf, 0, dac_mix_vol_tlv),
784 SOC_SINGLE_TLV("digital volume", DAC_DBG_CTRL, DVC, 0x3f, 1, dig_vol_tlv),
785 SOC_SINGLE_TLV("speaker volume", SPKOUT_CTRL, SPK_VOL, 0x1f, 0, speaker_vol_tlv),
786 SOC_SINGLE_TLV("headphone volume", HPOUT_CTRL, HP_VOL, 0x3f, 0, headphone_vol_tlv),
792 unsigned int pll_out;
802 #define _SERIES_24_576K 0
803 #define _SERIES_22_579K 1
813 * Note : pll code from original tdm/i2s driver.
814 * freq_out = freq_in * N/(M*(2k+1)) , k=1,N=N_i+N_f,N_f=factor*0.2;
815 * N_i[0,1023], N_f_factor[0,7], m[1,64]=REG_VAL[1-63,0]
817 static const struct pll_div codec_pll_div[] = {
818 {128000, _FREQ_22_579K, 1, 529, 1},
819 {192000, _FREQ_22_579K, 1, 352, 4},
820 {256000, _FREQ_22_579K, 1, 264, 3},
821 {384000, _FREQ_22_579K, 1, 176, 2}, /*((176+2*0.2)*6000000)/(38*(2*1+1))*/
822 {1411200, _FREQ_22_579K, 1, 48, 0},
823 {2822400, _FREQ_22_579K, 1, 24, 0}, /* accurate, 11025 * 256 */
824 {5644800, _FREQ_22_579K, 1, 12, 0}, /* accurate, 22050 * 256 */
825 {6000000, _FREQ_22_579K, 38, 429, 0}, /*((429+0*0.2)*6000000)/(38*(2*1+1))*/
826 {11289600, _FREQ_22_579K, 1, 6, 0}, /* accurate, 44100 * 256 */
827 {13000000, _FREQ_22_579K, 19, 99, 0},
828 {19200000, _FREQ_22_579K, 25, 88, 1},
829 {24000000, _FREQ_22_579K, 63, 177, 4}, /* 22577778 Hz */
831 {128000, _FREQ_24_576K, 1, 576, 0},
832 {192000, _FREQ_24_576K, 1, 384, 0},
833 {256000, _FREQ_24_576K, 1, 288, 0},
834 {384000, _FREQ_24_576K, 1, 192, 0},
835 {2048000, _FREQ_24_576K, 1, 36, 0}, /* accurate, 8000 * 256 */
836 {3072000, _FREQ_24_576K, 1, 24, 0}, /* accurate, 12000 * 256 */
837 {4096000, _FREQ_24_576K, 1, 18, 0}, /* accurate, 16000 * 256 */
838 {6000000, _FREQ_24_576K, 25, 307, 1},
839 {6144000, _FREQ_24_576K, 4, 48, 0}, /* accurate, 24000 * 256 */
840 {12288000, _FREQ_24_576K, 8, 48, 0}, /* accurate, 48000 * 256 */
841 {13000000, _FREQ_24_576K, 42, 238, 1},
842 {19200000, _FREQ_24_576K, 25, 96, 0},
843 {24000000, _FREQ_24_576K, 25, 76, 4}, /* accurate */
845 {_FREQ_22_579K, _FREQ_22_579K, 8, 24, 0}, /* accurate, 88200 * 256 */
846 {_FREQ_24_576K, _FREQ_24_576K, 8, 24, 0}, /* accurate, 96000 * 256 */
849 static const struct aif1_fs codec_aif1_fs[] = {
851 {11025, 8, 1, _SERIES_22_579K},
854 {22050, 4, 4, _SERIES_22_579K},
856 /* {32000, 3, 6}, dividing by 3 is not support */
857 {44100, 2, 7, _SERIES_22_579K},
862 static const struct kv_map codec_aif1_lrck[] = {
870 static const struct kv_map codec_aif1_wsize[] = {
878 static const unsigned ac101_bclkdivs[] = {
885 static int ac101_aif_play(struct ac10x_priv* ac10x) {
886 struct snd_soc_codec * codec = ac10x->codec;
888 late_enable_dac(codec, SND_SOC_DAPM_PRE_PMU);
889 ac101_headphone_event(codec, SND_SOC_DAPM_POST_PMU);
891 drc_enable(codec, 1);
894 /* Enable Left & Right Speaker */
895 ac101_update_bits(codec, SPKOUT_CTRL, (0x1 << LSPK_EN) | (0x1 << RSPK_EN), (0x1 << LSPK_EN) | (0x1 << RSPK_EN));
896 if (ac10x->gpiod_spk_amp_gate) {
897 gpiod_set_value(ac10x->gpiod_spk_amp_gate, 1);
902 static void ac10x_work_aif_play(struct work_struct *work) {
903 struct ac10x_priv *ac10x = container_of(work, struct ac10x_priv, dlywork.work);
905 ac101_aif_play(ac10x);
909 int ac101_aif_mute(struct snd_soc_dai *codec_dai, int mute)
911 struct snd_soc_codec *codec = codec_dai->codec;
912 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
914 AC101_DBG("mute=%d\n", mute);
916 ac101_write(codec, DAC_VOL_CTRL, mute? 0: 0xA0A0);
919 #if _MASTER_MULTI_CODEC != _MASTER_AC101
920 /* enable global clock */
921 ac10x->aif1_clken = 0;
922 ac101_aif1clk(codec, SND_SOC_DAPM_PRE_PMU, 0);
923 ac101_aif_play(ac10x);
925 schedule_delayed_work(&ac10x->dlywork, msecs_to_jiffies(50));
928 #if _MASTER_MULTI_CODEC == _MASTER_AC101
929 cancel_delayed_work_sync(&ac10x->dlywork);
932 if (ac10x->gpiod_spk_amp_gate) {
933 gpiod_set_value(ac10x->gpiod_spk_amp_gate, 0);
935 /* Disable Left & Right Speaker */
936 ac101_update_bits(codec, SPKOUT_CTRL, (0x1 << LSPK_EN) | (0x1 << RSPK_EN), (0x0 << LSPK_EN) | (0x0 << RSPK_EN));
938 drc_enable(codec, 0);
940 ac101_headphone_event(codec, SND_SOC_DAPM_PRE_PMD);
941 late_enable_dac(codec, SND_SOC_DAPM_POST_PMD);
943 #if _MASTER_MULTI_CODEC != _MASTER_AC101
944 ac10x->aif1_clken = 1;
945 ac101_aif1clk(codec, SND_SOC_DAPM_POST_PMD, 0);
951 void ac101_aif_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *codec_dai)
953 struct snd_soc_codec *codec = codec_dai->codec;
954 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
956 AC101_DBG("stream = %s, play: %d, capt: %d, active: %d\n",
957 snd_pcm_stream_str(substream),
958 codec_dai->playback_active, codec_dai->capture_active,
961 if (!codec_dai->active) {
962 ac10x->aif1_clken = 1;
963 ac101_aif1clk(codec, SND_SOC_DAPM_POST_PMD, 0);
965 ac101_aif1clk(codec, SND_SOC_DAPM_PRE_PMU, 0);
969 static int ac101_set_pll(struct snd_soc_dai *codec_dai, int pll_id, int source,
970 unsigned int freq_in, unsigned int freq_out)
972 struct snd_soc_codec *codec = codec_dai->codec;
975 AC101_DBG("pll_id:%d\n", pll_id);
977 /* clear volatile reserved bits*/
978 ac101_update_bits(codec, SYSCLK_CTRL, 0xFF & ~(0x1 << SYSCLK_ENA), 0x0);
980 /* select aif1 clk srouce from mclk1 */
981 ac101_update_bits(codec, SYSCLK_CTRL, (0x3<<AIF1CLK_SRC), (0x0<<AIF1CLK_SRC));
983 ac101_update_bits(codec, PLL_CTRL2, (0x1<<PLL_EN), (0<<PLL_EN));
987 if ((freq_in < 128000) || (freq_in > _FREQ_24_576K)) {
989 } else if ((freq_in == _FREQ_24_576K) || (freq_in == _FREQ_22_579K)) {
990 if (pll_id == AC101_MCLK1) {
991 /*select aif1 clk source from mclk1*/
992 ac101_update_bits(codec, SYSCLK_CTRL, (0x3<<AIF1CLK_SRC), (0x0<<AIF1CLK_SRC));
999 /*pll source from MCLK1*/
1000 ac101_update_bits(codec, SYSCLK_CTRL, (0x3<<PLLCLK_SRC), (0x0<<PLLCLK_SRC));
1003 /*pll source from BCLK1*/
1004 ac101_update_bits(codec, SYSCLK_CTRL, (0x3<<PLLCLK_SRC), (0x2<<PLLCLK_SRC));
1010 /* freq_out = freq_in * n/(m*(2k+1)) , k=1,N=N_i+N_f */
1011 for (i = m = n_i = n_f = 0; i < ARRAY_SIZE(codec_pll_div); i++) {
1012 if ((codec_pll_div[i].pll_in == freq_in) && (codec_pll_div[i].pll_out == freq_out)) {
1013 m = codec_pll_div[i].m;
1014 n_i = codec_pll_div[i].n_i;
1015 n_f = codec_pll_div[i].n_f;
1021 ac101_update_bits(codec, PLL_CTRL1, (0x3f<<PLL_POSTDIV_M), (m<<PLL_POSTDIV_M));
1023 ac101_update_bits(codec, PLL_CTRL2, (0x3ff<<PLL_PREDIV_NI), (n_i<<PLL_PREDIV_NI));
1024 ac101_update_bits(codec, PLL_CTRL2, (0x7<<PLL_POSTDIV_NF), (n_f<<PLL_POSTDIV_NF));
1026 ac101_update_bits(codec, PLL_CTRL2, (0x1<<PLL_EN), (1<<PLL_EN));
1027 ac101_update_bits(codec, SYSCLK_CTRL, (0x1<<PLLCLK_ENA), (0x1<<PLLCLK_ENA));
1028 ac101_update_bits(codec, SYSCLK_CTRL, (0x3<<AIF1CLK_SRC), (0x3<<AIF1CLK_SRC));
1033 int ac101_hw_params(struct snd_pcm_substream *substream,
1034 struct snd_pcm_hw_params *params,
1035 struct snd_soc_dai *codec_dai)
1038 int AIF_CLK_CTRL = AIF1_CLK_CTRL;
1039 int aif1_word_size = 24;
1040 int aif1_slot_size = 32;
1042 struct snd_soc_codec *codec = codec_dai->codec;
1043 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1044 int reg_val, freq_out;
1049 if (_MASTER_MULTI_CODEC == _MASTER_AC101 && ac101_sysclk_started()) {
1050 /* not configure hw_param twice if stream is playback, tell the caller it's started */
1051 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1056 /* get channels count & slot size */
1057 channels = params_channels(params);
1059 switch (params_format(params)) {
1060 case SNDRV_PCM_FORMAT_S24_LE:
1061 case SNDRV_PCM_FORMAT_S32_LE:
1062 aif1_slot_size = 32;
1064 case SNDRV_PCM_FORMAT_S16_LE:
1066 aif1_slot_size = 16;
1070 /* set LRCK/BCLK ratio */
1071 aif1_lrck_div = aif1_slot_size * channels;
1072 for (i = 0; i < ARRAY_SIZE(codec_aif1_lrck); i++) {
1073 if (codec_aif1_lrck[i].val == aif1_lrck_div) {
1077 ac101_update_bits(codec, AIF_CLK_CTRL, (0x7<<AIF1_LRCK_DIV), codec_aif1_lrck[i].bit<<AIF1_LRCK_DIV);
1079 /* set PLL output freq */
1080 freq_out = _FREQ_24_576K;
1081 for (i = 0; i < ARRAY_SIZE(codec_aif1_fs); i++) {
1082 if (codec_aif1_fs[i].samp_rate == params_rate(params)) {
1083 if (codec_dai->capture_active && dmic_used && codec_aif1_fs[i].samp_rate == 44100) {
1084 ac101_update_bits(codec, AIF_SR_CTRL, (0xf<<AIF1_FS), (0x4<<AIF1_FS));
1086 ac101_update_bits(codec, AIF_SR_CTRL, (0xf<<AIF1_FS), ((codec_aif1_fs[i].srbit)<<AIF1_FS));
1088 if (codec_aif1_fs[i].series == _SERIES_22_579K)
1089 freq_out = _FREQ_22_579K;
1094 /* set I2S word size */
1095 for (i = 0; i < ARRAY_SIZE(codec_aif1_wsize); i++) {
1096 if (codec_aif1_wsize[i].val == aif1_word_size) {
1100 ac101_update_bits(codec, AIF_CLK_CTRL, (0x3<<AIF1_WORK_SIZ), ((codec_aif1_wsize[i].bit)<<AIF1_WORK_SIZ));
1102 /* set TDM slot size */
1103 if ((reg_val = codec_aif1_wsize[i].bit) > 2) reg_val = 2;
1104 ac101_update_bits(codec, AIF1_ADCDAT_CTRL, 0x3 << AIF1_SLOT_SIZ, reg_val << AIF1_SLOT_SIZ);
1106 /* setting pll if it's master mode */
1107 reg_val = ac101_read(codec, AIF_CLK_CTRL);
1108 if ((reg_val & (0x1 << AIF1_MSTR_MOD)) == 0) {
1111 ac101_set_pll(codec_dai, AC101_MCLK1, 0, ac10x->sysclk, freq_out);
1113 bclkdiv = freq_out / (aif1_lrck_div * params_rate(params));
1114 for (i = 0; i < ARRAY_SIZE(ac101_bclkdivs) - 1; i++) {
1115 if (ac101_bclkdivs[i] >= bclkdiv) {
1119 ac101_update_bits(codec, AIF_CLK_CTRL, (0xf<<AIF1_BCLK_DIV), i<<AIF1_BCLK_DIV);
1121 /* set pll clock source to BCLK if slave mode */
1122 ac101_set_pll(codec_dai, AC101_BCLK1, 0, aif1_lrck_div * params_rate(params), freq_out);
1125 #if _MASTER_MULTI_CODEC == _MASTER_AC101
1126 /* Master mode, to clear cpu_dai fifos, disable output bclk & lrck */
1127 ac101_aif1clk(codec, SND_SOC_DAPM_POST_PMD, 0);
1130 AC101_DBG("rate: %d , channels: %d , samp_res: %d",
1131 params_rate(params), channels, aif1_slot_size);
1137 int ac101_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
1140 int AIF_CLK_CTRL = AIF1_CLK_CTRL;
1141 struct snd_soc_codec *codec = codec_dai->codec;
1146 * master or slave selection
1150 reg_val = ac101_read(codec, AIF_CLK_CTRL);
1151 reg_val &= ~(0x1<<AIF1_MSTR_MOD);
1152 switch(fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1153 case SND_SOC_DAIFMT_CBM_CFM: /* codec clk & frm master, ap is slave*/
1154 #if _MASTER_MULTI_CODEC == _MASTER_AC101
1155 pr_warn("AC101 as Master\n");
1156 reg_val |= (0x0<<AIF1_MSTR_MOD);
1159 pr_warn("AC108 as Master\n");
1161 case SND_SOC_DAIFMT_CBS_CFS: /* codec clk & frm slave, ap is master*/
1162 pr_warn("AC101 as Slave\n");
1163 reg_val |= (0x1<<AIF1_MSTR_MOD);
1166 pr_err("unknwon master/slave format\n");
1173 reg_val |= (0x1 << AIF1_TDMM_ENA);
1174 ac101_write(codec, AIF_CLK_CTRL, reg_val);
1176 /* i2s mode selection */
1177 reg_val = ac101_read(codec, AIF_CLK_CTRL);
1178 reg_val&=~(3<<AIF1_DATA_FMT);
1179 switch(fmt & SND_SOC_DAIFMT_FORMAT_MASK){
1180 case SND_SOC_DAIFMT_I2S: /* I2S1 mode */
1181 reg_val |= (0x0<<AIF1_DATA_FMT);
1183 case SND_SOC_DAIFMT_RIGHT_J: /* Right Justified mode */
1184 reg_val |= (0x2<<AIF1_DATA_FMT);
1186 case SND_SOC_DAIFMT_LEFT_J: /* Left Justified mode */
1187 reg_val |= (0x1<<AIF1_DATA_FMT);
1189 case SND_SOC_DAIFMT_DSP_A: /* L reg_val msb after FRM LRC */
1190 reg_val |= (0x3<<AIF1_DATA_FMT);
1192 case SND_SOC_DAIFMT_DSP_B:
1193 /* TODO: data offset set to 0 */
1194 reg_val |= (0x3<<AIF1_DATA_FMT);
1197 pr_err("%s, line:%d\n", __func__, __LINE__);
1200 ac101_write(codec, AIF_CLK_CTRL, reg_val);
1202 /* DAI signal inversions */
1203 reg_val = ac101_read(codec, AIF_CLK_CTRL);
1204 switch(fmt & SND_SOC_DAIFMT_INV_MASK){
1205 case SND_SOC_DAIFMT_NB_NF: /* normal bit clock + nor frame */
1206 reg_val &= ~(0x1<<AIF1_LRCK_INV);
1207 reg_val &= ~(0x1<<AIF1_BCLK_INV);
1209 case SND_SOC_DAIFMT_NB_IF: /* normal bclk + inv frm */
1210 reg_val |= (0x1<<AIF1_LRCK_INV);
1211 reg_val &= ~(0x1<<AIF1_BCLK_INV);
1213 case SND_SOC_DAIFMT_IB_NF: /* invert bclk + nor frm */
1214 reg_val &= ~(0x1<<AIF1_LRCK_INV);
1215 reg_val |= (0x1<<AIF1_BCLK_INV);
1217 case SND_SOC_DAIFMT_IB_IF: /* invert bclk + inv frm */
1218 reg_val |= (0x1<<AIF1_LRCK_INV);
1219 reg_val |= (0x1<<AIF1_BCLK_INV);
1222 ac101_write(codec, AIF_CLK_CTRL, reg_val);
1227 int ac101_audio_startup(struct snd_pcm_substream *substream,
1228 struct snd_soc_dai *codec_dai)
1230 // struct snd_soc_codec *codec = codec_dai->codec;
1232 AC101_DBG("\n\n\n");
1234 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
1239 #if _MASTER_MULTI_CODEC == _MASTER_AC101
1240 static int ac101_set_clock(int y_start_n_stop) {
1243 if (y_start_n_stop) {
1244 /* enable global clock */
1245 r = ac101_aif1clk(static_ac10x->codec, SND_SOC_DAPM_PRE_PMU, 1);
1247 /* disable global clock */
1248 static_ac10x->aif1_clken = 1;
1249 r = ac101_aif1clk(static_ac10x->codec, SND_SOC_DAPM_POST_PMD, 0);
1255 int ac101_trigger(struct snd_pcm_substream *substream, int cmd,
1256 struct snd_soc_dai *dai)
1258 struct snd_soc_codec *codec = dai->codec;
1259 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1262 AC101_DBG("stream=%s cmd=%d\n",
1263 snd_pcm_stream_str(substream),
1267 case SNDRV_PCM_TRIGGER_START:
1268 case SNDRV_PCM_TRIGGER_RESUME:
1269 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1270 #if _MASTER_MULTI_CODEC == _MASTER_AC101
1271 if (ac10x->aif1_clken == 0){
1273 * enable aif1clk, it' here due to reduce time between 'AC108 Sysclk Enable' and 'AC101 Sysclk Enable'
1274 * Or else the two AC108 chips lost the sync.
1277 ret = ret || ac101_update_bits(codec, MOD_CLK_ENA, (0x1<<MOD_CLK_AIF1), (0x1<<MOD_CLK_AIF1));
1278 ret = ret || ac101_update_bits(codec, MOD_RST_CTRL, (0x1<<MOD_RESET_AIF1), (0x1<<MOD_RESET_AIF1));
1282 case SNDRV_PCM_TRIGGER_STOP:
1283 case SNDRV_PCM_TRIGGER_SUSPEND:
1284 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1293 static int ac101_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1294 int clk_id, unsigned int freq, int dir)
1296 struct snd_soc_codec *codec = codec_dai->codec;
1297 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1299 AC101_DBG("id=%d freq=%d, dir=%d\n",
1302 ac10x->sysclk = freq;
1307 static const struct snd_soc_dai_ops ac101_aif1_dai_ops = {
1308 //.startup = ac101_audio_startup,
1309 //.shutdown = ac101_aif_shutdown,
1310 //.set_sysclk = ac101_set_dai_sysclk,
1311 //.set_pll = ac101_set_pll,
1312 //.set_fmt = ac101_set_dai_fmt,
1313 //.hw_params = ac101_hw_params,
1314 //.trigger = ac101_trigger,
1315 //.digital_mute = ac101_aif_mute,
1318 static struct snd_soc_dai_driver ac101_dai[] = {
1320 .name = "ac10x-aif1",
1323 .stream_name = "Playback",
1326 .rates = AC101_RATES,
1327 .formats = AC101_FORMATS,
1331 .stream_name = "Capture",
1334 .rates = AC101_RATES,
1335 .formats = AC101_FORMATS,
1338 .ops = &ac101_aif1_dai_ops,
1343 static void codec_resume_work(struct work_struct *work)
1345 struct ac10x_priv *ac10x = container_of(work, struct ac10x_priv, codec_resume);
1346 struct snd_soc_codec *codec = ac10x->codec;
1350 set_configuration(codec);
1354 /*enable this bit to prevent leakage from ldoin*/
1355 ac101_update_bits(codec, ADDA_TUNE3, (0x1<<OSCEN), (0x1<<OSCEN));
1361 int ac101_set_bias_level(struct snd_soc_codec *codec, enum snd_soc_bias_level level)
1364 case SND_SOC_BIAS_ON:
1365 AC101_DBG("SND_SOC_BIAS_ON\n");
1367 case SND_SOC_BIAS_PREPARE:
1368 AC101_DBG("SND_SOC_BIAS_PREPARE\n");
1370 case SND_SOC_BIAS_STANDBY:
1371 AC101_DBG("SND_SOC_BIAS_STANDBY\n");
1372 #ifdef CONFIG_AC101_SWITCH_DETECT
1373 switch_hw_config(codec);
1376 case SND_SOC_BIAS_OFF:
1377 #ifdef CONFIG_AC101_SWITCH_DETECT
1378 ac101_update_bits(codec, ADC_APC_CTRL, (0x1<<HBIASEN), (0<<HBIASEN));
1379 ac101_update_bits(codec, ADC_APC_CTRL, (0x1<<HBIASADCEN), (0<<HBIASADCEN));
1381 ac101_update_bits(codec, OMIXER_DACA_CTRL, (0xf<<HPOUTPUTENABLE), (0<<HPOUTPUTENABLE));
1382 ac101_update_bits(codec, ADDA_TUNE3, (0x1<<OSCEN), (0<<OSCEN));
1383 AC101_DBG("SND_SOC_BIAS_OFF\n");
1386 snd_soc_codec_get_dapm(codec)->bias_level = level;
1390 int ac101_codec_probe(struct snd_soc_codec *codec)
1393 struct ac10x_priv *ac10x;
1395 ac10x = dev_get_drvdata(codec->dev);
1396 if (ac10x == NULL) {
1397 AC101_DBG("not set client data!\n");
1400 ac10x->codec = codec;
1402 INIT_DELAYED_WORK(&ac10x->dlywork, ac10x_work_aif_play);
1403 INIT_WORK(&ac10x->codec_resume, codec_resume_work);
1404 ac10x->dac_enable = 0;
1405 ac10x->aif1_clken = 0;
1406 mutex_init(&ac10x->dac_mutex);
1408 #if _MASTER_MULTI_CODEC == _MASTER_AC101
1409 seeed_voice_card_register_set_clock(SNDRV_PCM_STREAM_PLAYBACK, ac101_set_clock);
1412 set_configuration(ac10x->codec);
1414 /*enable this bit to prevent leakage from ldoin*/
1415 ac101_update_bits(codec, ADDA_TUNE3, (0x1<<OSCEN), (0x1<<OSCEN));
1416 ac101_write(codec, DAC_VOL_CTRL, 0);
1418 /* customized get/put inteface */
1419 for (ret = 0; ret < ARRAY_SIZE(ac101_controls); ret++) {
1420 struct snd_kcontrol_new* skn = &ac101_controls[ret];
1422 skn->get = snd_ac101_get_volsw;
1423 skn->put = snd_ac101_put_volsw;
1425 ret = snd_soc_add_codec_controls(codec, ac101_controls, ARRAY_SIZE(ac101_controls));
1427 pr_err("[ac10x] Failed to register audio mode control, "
1428 "will continue without it.\n");
1431 #ifdef CONFIG_AC101_SWITCH_DETECT
1432 ret = ac101_switch_probe(ac10x);
1434 // not care the switch return value
1441 /* power down chip */
1442 int ac101_codec_remove(struct snd_soc_codec *codec)
1444 #ifdef CONFIG_AC101_SWITCH_DETECT
1445 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1448 devm_free_irq(codec->dev, ac10x->irq, ac10x);
1452 if (cancel_work_sync(&ac10x->work_switch) != 0) {
1455 if (cancel_work_sync(&ac10x->work_clear_irq) != 0) {
1458 if (ac10x->inpdev) {
1459 input_unregister_device(ac10x->inpdev);
1460 ac10x->inpdev = NULL;
1467 int ac101_codec_suspend(struct snd_soc_codec *codec)
1469 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1471 AC101_DBG("[codec]:suspend\n");
1472 regcache_cache_only(ac10x->regmap101, true);
1476 int ac101_codec_resume(struct snd_soc_codec *codec)
1478 struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1481 AC101_DBG("[codec]:resume");
1483 /* Sync reg_cache with the hardware */
1484 regcache_cache_only(ac10x->regmap101, false);
1485 ret = regcache_sync(ac10x->regmap101);
1487 dev_err(codec->dev, "Failed to sync register cache: %d\n", ret);
1488 regcache_cache_only(ac10x->regmap101, true);
1492 #ifdef CONFIG_AC101_SWITCH_DETECT
1493 ac10x->mode = HEADPHONE_IDLE;
1497 ac101_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1498 schedule_work(&ac10x->codec_resume);
1502 /***************************************************************************/
1503 static ssize_t ac101_debug_store(struct device *dev,
1504 struct device_attribute *attr, const char *buf, size_t count)
1506 struct ac10x_priv *ac10x = dev_get_drvdata(dev);
1507 int val = 0, flag = 0;
1508 u16 value_w, value_r;
1511 val = simple_strtol(buf, NULL, 16);
1512 flag = (val >> 24) & 0xF;
1514 reg = (val >> 16) & 0xFF;
1515 value_w = val & 0xFFFF;
1516 ac101_write(ac10x->codec, reg, value_w);
1517 printk("write 0x%x to reg:0x%x\n", value_w, reg);
1519 reg = (val >> 8) & 0xFF;
1522 printk("read:start add:0x%x,count:0x%x\n", reg, num);
1524 regcache_cache_bypass(ac10x->regmap101, true);
1526 value_r = ac101_read(ac10x->codec, reg);
1527 printk("0x%x: 0x%04x ", reg++, value_r);
1528 if (++i % 4 == 0 || i == num)
1531 regcache_cache_bypass(ac10x->regmap101, false);
1535 static ssize_t ac101_debug_show(struct device *dev,
1536 struct device_attribute *attr, char *buf)
1538 printk("echo flag|reg|val > ac10x\n");
1539 printk("eg read star addres=0x06,count 0x10:echo 0610 >ac10x\n");
1540 printk("eg write value:0x13fe to address:0x06 :echo 10613fe > ac10x\n");
1543 static DEVICE_ATTR(ac10x, 0644, ac101_debug_show, ac101_debug_store);
1545 static struct attribute *audio_debug_attrs[] = {
1546 &dev_attr_ac10x.attr,
1550 static struct attribute_group audio_debug_attr_group = {
1551 .name = "ac101_debug",
1552 .attrs = audio_debug_attrs,
1554 /***************************************************************************/
1556 /************************************************************/
1557 static bool ac101_volatile_reg(struct device *dev, unsigned int reg)
1567 static const struct regmap_config ac101_regmap = {
1571 .max_register = 0xB5,
1572 .cache_type = REGCACHE_FLAT,
1573 .volatile_reg = ac101_volatile_reg,
1576 /* Sync reg_cache from the hardware */
1577 int ac10x_fill_regcache(struct device* dev, struct regmap* map) {
1581 n = regmap_get_max_register(map);
1582 for (i = 0; i < n; i++) {
1583 regcache_cache_bypass(map, true);
1584 r = regmap_read(map, i, &v);
1586 dev_err(dev, "failed to read register %d\n", i);
1589 regcache_cache_bypass(map, false);
1591 regcache_cache_only(map, true);
1592 r = regmap_write(map, i, v);
1593 regcache_cache_only(map, false);
1595 regcache_cache_bypass(map, false);
1596 regcache_cache_only(map, false);
1601 int ac101_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
1603 struct ac10x_priv *ac10x = i2c_get_clientdata(i2c);
1609 static_ac10x = ac10x;
1611 ac10x->regmap101 = devm_regmap_init_i2c(i2c, &ac101_regmap);
1612 if (IS_ERR(ac10x->regmap101)) {
1613 ret = PTR_ERR(ac10x->regmap101);
1614 dev_err(&i2c->dev, "Fail to initialize I/O: %d\n", ret);
1619 regcache_cache_only(ac10x->regmap101, false);
1620 ret = regmap_write(ac10x->regmap101, CHIP_AUDIO_RST, 0);
1623 /* sync regcache for FLAT type */
1624 ac10x_fill_regcache(&i2c->dev, ac10x->regmap101);
1626 ret = regmap_read(ac10x->regmap101, CHIP_AUDIO_RST, &v);
1628 dev_err(&i2c->dev, "failed to read vendor ID: %d\n", ret);
1632 if (v != AC101_CHIP_ID) {
1633 dev_err(&i2c->dev, "chip is not AC101 (%X)\n", v);
1634 dev_err(&i2c->dev, "Expected %X\n", AC101_CHIP_ID);
1638 ret = sysfs_create_group(&i2c->dev.kobj, &audio_debug_attr_group);
1640 pr_err("failed to create attr group\n");
1643 ac10x->gpiod_spk_amp_gate = devm_gpiod_get_optional(&i2c->dev, "spk-amp-switch", GPIOD_OUT_LOW);
1644 if (IS_ERR(ac10x->gpiod_spk_amp_gate)) {
1645 ac10x->gpiod_spk_amp_gate = NULL;
1646 dev_err(&i2c->dev, "failed get spk-amp-switch in device tree\n");
1652 void ac101_shutdown(struct i2c_client *i2c)
1654 struct ac10x_priv *ac10x = i2c_get_clientdata(i2c);
1655 struct snd_soc_codec *codec = ac10x->codec;
1658 if (codec == NULL) {
1659 pr_err(": no sound card.\n");
1663 /*set headphone volume to 0*/
1664 reg_val = ac101_read(codec, HPOUT_CTRL);
1665 reg_val &= ~(0x3f<<HP_VOL);
1666 ac101_write(codec, HPOUT_CTRL, reg_val);
1669 reg_val = ac101_read(codec, HPOUT_CTRL);
1670 reg_val &= ~(0x1<<HPPA_EN);
1671 ac101_write(codec, HPOUT_CTRL, reg_val);
1673 /*hardware xzh support*/
1674 reg_val = ac101_read(codec, OMIXER_DACA_CTRL);
1675 reg_val &= ~(0xf<<HPOUTPUTENABLE);
1676 ac101_write(codec, OMIXER_DACA_CTRL, reg_val);
1678 /*unmute l/r headphone pa*/
1679 reg_val = ac101_read(codec, HPOUT_CTRL);
1680 reg_val &= ~((0x1<<RHPPA_MUTE)|(0x1<<LHPPA_MUTE));
1681 ac101_write(codec, HPOUT_CTRL, reg_val);
1685 int ac101_remove(struct i2c_client *i2c)
1687 sysfs_remove_group(&i2c->dev.kobj, &audio_debug_attr_group);
1691 MODULE_DESCRIPTION("ASoC ac10x driver");
1692 MODULE_AUTHOR("huangxin,liushaohua");
1693 MODULE_AUTHOR("PeterYang<linsheng.yang@seeed.cc>");