forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 1f93a7dfd1f8d5ff7a5c53246c7534fe2332d6f4
kernel/sound/soc/codecs/tlv320aic32x4.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * linux/sound/soc/codecs/tlv320aic32x4.c
34 *
....@@ -6,21 +7,6 @@
67 * Author: Javier Martin <javier.martin@vista-silicon.com>
78 *
89 * Based on sound/soc/codecs/wm8974 and TI driver for kernel 2.6.27.
9
- *
10
- * This program is free software; you can redistribute it and/or modify
11
- * it under the terms of the GNU General Public License as published by
12
- * the Free Software Foundation; either version 2 of the License, or
13
- * (at your option) any later version.
14
- *
15
- * This program is distributed in the hope that it will be useful,
16
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
- * GNU General Public License for more details.
19
- *
20
- * You should have received a copy of the GNU General Public License
21
- * along with this program; if not, write to the Free Software
22
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23
- * MA 02110-1301, USA.
2410 */
2511
2612 #include <linux/module.h>
....@@ -33,6 +19,7 @@
3319 #include <linux/cdev.h>
3420 #include <linux/slab.h>
3521 #include <linux/clk.h>
22
+#include <linux/of_clk.h>
3623 #include <linux/regulator/consumer.h>
3724
3825 #include <sound/tlv320aic32x4.h>
....@@ -46,29 +33,13 @@
4633
4734 #include "tlv320aic32x4.h"
4835
49
-struct aic32x4_rate_divs {
50
- u32 mclk;
51
- u32 rate;
52
- u8 p_val;
53
- u8 pll_j;
54
- u16 pll_d;
55
- u16 dosr;
56
- u8 ndac;
57
- u8 mdac;
58
- u8 aosr;
59
- u8 nadc;
60
- u8 madc;
61
- u8 blck_N;
62
-};
63
-
6436 struct aic32x4_priv {
6537 struct regmap *regmap;
66
- u32 sysclk;
6738 u32 power_cfg;
6839 u32 micpga_routing;
6940 bool swapdacs;
7041 int rstn_gpio;
71
- struct clk *mclk;
42
+ const char *mclk_name;
7243
7344 struct regulator *supply_ldo;
7445 struct regulator *supply_iov;
....@@ -79,13 +50,61 @@
7950 struct device *dev;
8051 };
8152
53
+static int aic32x4_reset_adc(struct snd_soc_dapm_widget *w,
54
+ struct snd_kcontrol *kcontrol, int event)
55
+{
56
+ struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
57
+ u32 adc_reg;
58
+
59
+ /*
60
+ * Workaround: the datasheet does not mention a required programming
61
+ * sequence but experiments show the ADC needs to be reset after each
62
+ * capture to avoid audible artifacts.
63
+ */
64
+ switch (event) {
65
+ case SND_SOC_DAPM_POST_PMD:
66
+ adc_reg = snd_soc_component_read(component, AIC32X4_ADCSETUP);
67
+ snd_soc_component_write(component, AIC32X4_ADCSETUP, adc_reg |
68
+ AIC32X4_LADC_EN | AIC32X4_RADC_EN);
69
+ snd_soc_component_write(component, AIC32X4_ADCSETUP, adc_reg);
70
+ break;
71
+ }
72
+ return 0;
73
+};
74
+
75
+static int mic_bias_event(struct snd_soc_dapm_widget *w,
76
+ struct snd_kcontrol *kcontrol, int event)
77
+{
78
+ struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
79
+
80
+ switch (event) {
81
+ case SND_SOC_DAPM_POST_PMU:
82
+ /* Change Mic Bias Registor */
83
+ snd_soc_component_update_bits(component, AIC32X4_MICBIAS,
84
+ AIC32x4_MICBIAS_MASK,
85
+ AIC32X4_MICBIAS_LDOIN |
86
+ AIC32X4_MICBIAS_2075V);
87
+ printk(KERN_DEBUG "%s: Mic Bias will be turned ON\n", __func__);
88
+ break;
89
+ case SND_SOC_DAPM_PRE_PMD:
90
+ snd_soc_component_update_bits(component, AIC32X4_MICBIAS,
91
+ AIC32x4_MICBIAS_MASK, 0);
92
+ printk(KERN_DEBUG "%s: Mic Bias will be turned OFF\n",
93
+ __func__);
94
+ break;
95
+ }
96
+
97
+ return 0;
98
+}
99
+
100
+
82101 static int aic32x4_get_mfp1_gpio(struct snd_kcontrol *kcontrol,
83102 struct snd_ctl_elem_value *ucontrol)
84103 {
85104 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
86105 u8 val;
87106
88
- val = snd_soc_component_read32(component, AIC32X4_DINCTL);
107
+ val = snd_soc_component_read(component, AIC32X4_DINCTL);
89108
90109 ucontrol->value.integer.value[0] = (val & 0x01);
91110
....@@ -99,7 +118,7 @@
99118 u8 val;
100119 u8 gpio_check;
101120
102
- val = snd_soc_component_read32(component, AIC32X4_DOUTCTL);
121
+ val = snd_soc_component_read(component, AIC32X4_DOUTCTL);
103122 gpio_check = (val & AIC32X4_MFP_GPIO_ENABLED);
104123 if (gpio_check != AIC32X4_MFP_GPIO_ENABLED) {
105124 printk(KERN_ERR "%s: MFP2 is not configure as a GPIO output\n",
....@@ -126,7 +145,7 @@
126145 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
127146 u8 val;
128147
129
- val = snd_soc_component_read32(component, AIC32X4_SCLKCTL);
148
+ val = snd_soc_component_read(component, AIC32X4_SCLKCTL);
130149
131150 ucontrol->value.integer.value[0] = (val & 0x01);
132151
....@@ -140,7 +159,7 @@
140159 u8 val;
141160 u8 gpio_check;
142161
143
- val = snd_soc_component_read32(component, AIC32X4_MISOCTL);
162
+ val = snd_soc_component_read(component, AIC32X4_MISOCTL);
144163 gpio_check = (val & AIC32X4_MFP_GPIO_ENABLED);
145164 if (gpio_check != AIC32X4_MFP_GPIO_ENABLED) {
146165 printk(KERN_ERR "%s: MFP4 is not configure as a GPIO output\n",
....@@ -167,7 +186,7 @@
167186 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
168187 u8 val;
169188
170
- val = snd_soc_component_read32(component, AIC32X4_GPIOCTL);
189
+ val = snd_soc_component_read(component, AIC32X4_GPIOCTL);
171190 ucontrol->value.integer.value[0] = ((val & 0x2) >> 1);
172191
173192 return 0;
....@@ -180,7 +199,7 @@
180199 u8 val;
181200 u8 gpio_check;
182201
183
- val = snd_soc_component_read32(component, AIC32X4_GPIOCTL);
202
+ val = snd_soc_component_read(component, AIC32X4_GPIOCTL);
184203 gpio_check = (val & AIC32X4_MFP5_GPIO_OUTPUT);
185204 if (gpio_check != AIC32X4_MFP5_GPIO_OUTPUT) {
186205 printk(KERN_ERR "%s: MFP5 is not configure as a GPIO output\n",
....@@ -231,9 +250,24 @@
231250 /* -12dB min, 0.5dB steps */
232251 static DECLARE_TLV_DB_SCALE(tlv_adc_vol, -1200, 50, 0);
233252
253
+static const char * const lo_cm_text[] = {
254
+ "Full Chip", "1.65V",
255
+};
256
+
257
+static SOC_ENUM_SINGLE_DECL(lo_cm_enum, AIC32X4_CMMODE, 3, lo_cm_text);
258
+
259
+static const char * const ptm_text[] = {
260
+ "P3", "P2", "P1",
261
+};
262
+
263
+static SOC_ENUM_SINGLE_DECL(l_ptm_enum, AIC32X4_LPLAYBACK, 2, ptm_text);
264
+static SOC_ENUM_SINGLE_DECL(r_ptm_enum, AIC32X4_RPLAYBACK, 2, ptm_text);
265
+
234266 static const struct snd_kcontrol_new aic32x4_snd_controls[] = {
235267 SOC_DOUBLE_R_S_TLV("PCM Playback Volume", AIC32X4_LDACVOL,
236268 AIC32X4_RDACVOL, 0, -0x7f, 0x30, 7, 0, tlv_pcm),
269
+ SOC_ENUM("DAC Left Playback PowerTune Switch", l_ptm_enum),
270
+ SOC_ENUM("DAC Right Playback PowerTune Switch", r_ptm_enum),
237271 SOC_DOUBLE_R_S_TLV("HP Driver Gain Volume", AIC32X4_HPLGAIN,
238272 AIC32X4_HPRGAIN, 0, -0x6, 0x1d, 5, 0,
239273 tlv_driver_gain),
....@@ -244,6 +278,7 @@
244278 AIC32X4_HPRGAIN, 6, 0x01, 1),
245279 SOC_DOUBLE_R("LO DAC Playback Switch", AIC32X4_LOLGAIN,
246280 AIC32X4_LORGAIN, 6, 0x01, 1),
281
+ SOC_ENUM("LO Playback Common Mode Switch", lo_cm_enum),
247282 SOC_DOUBLE_R("Mic PGA Switch", AIC32X4_LMICPGAVOL,
248283 AIC32X4_RMICPGAVOL, 7, 0x01, 1),
249284
....@@ -277,38 +312,6 @@
277312 0, 0x1F, 0),
278313 SOC_DOUBLE_R("AGC Signal Debounce", AIC32X4_LAGC7, AIC32X4_RAGC7,
279314 0, 0x0F, 0),
280
-};
281
-
282
-static const struct aic32x4_rate_divs aic32x4_divs[] = {
283
- /* 8k rate */
284
- {12000000, 8000, 1, 7, 6800, 768, 5, 3, 128, 5, 18, 24},
285
- {24000000, 8000, 2, 7, 6800, 768, 15, 1, 64, 45, 4, 24},
286
- {25000000, 8000, 2, 7, 3728, 768, 15, 1, 64, 45, 4, 24},
287
- /* 11.025k rate */
288
- {12000000, 11025, 1, 7, 5264, 512, 8, 2, 128, 8, 8, 16},
289
- {24000000, 11025, 2, 7, 5264, 512, 16, 1, 64, 32, 4, 16},
290
- /* 16k rate */
291
- {12000000, 16000, 1, 7, 6800, 384, 5, 3, 128, 5, 9, 12},
292
- {24000000, 16000, 2, 7, 6800, 384, 15, 1, 64, 18, 5, 12},
293
- {25000000, 16000, 2, 7, 3728, 384, 15, 1, 64, 18, 5, 12},
294
- /* 22.05k rate */
295
- {12000000, 22050, 1, 7, 5264, 256, 4, 4, 128, 4, 8, 8},
296
- {24000000, 22050, 2, 7, 5264, 256, 16, 1, 64, 16, 4, 8},
297
- {25000000, 22050, 2, 7, 2253, 256, 16, 1, 64, 16, 4, 8},
298
- /* 32k rate */
299
- {12000000, 32000, 1, 7, 1680, 192, 2, 7, 64, 2, 21, 6},
300
- {24000000, 32000, 2, 7, 1680, 192, 7, 2, 64, 7, 6, 6},
301
- /* 44.1k rate */
302
- {12000000, 44100, 1, 7, 5264, 128, 2, 8, 128, 2, 8, 4},
303
- {24000000, 44100, 2, 7, 5264, 128, 8, 2, 64, 8, 4, 4},
304
- {25000000, 44100, 2, 7, 2253, 128, 8, 2, 64, 8, 4, 4},
305
- /* 48k rate */
306
- {12000000, 48000, 1, 8, 1920, 128, 2, 8, 128, 2, 8, 4},
307
- {24000000, 48000, 2, 8, 1920, 128, 8, 2, 64, 8, 4, 4},
308
- {25000000, 48000, 2, 7, 8643, 128, 8, 2, 64, 8, 4, 4},
309
-
310
- /* 96k rate */
311
- {25000000, 96000, 2, 7, 8643, 64, 4, 4, 64, 4, 4, 1},
312315 };
313316
314317 static const struct snd_kcontrol_new hpl_output_mixer_controls[] = {
....@@ -365,7 +368,7 @@
365368 SOC_DAPM_ENUM("IN3_R L- Switch", in3r_lpga_n_enum),
366369 };
367370
368
-/* Right mixer pins */
371
+/* Right mixer pins */
369372 static SOC_ENUM_SINGLE_DECL(in1r_rpga_p_enum, AIC32X4_RMICPGAPIN, 6, resistor_text);
370373 static SOC_ENUM_SINGLE_DECL(in2r_rpga_p_enum, AIC32X4_RMICPGAPIN, 4, resistor_text);
371374 static SOC_ENUM_SINGLE_DECL(in3r_rpga_p_enum, AIC32X4_RMICPGAPIN, 2, resistor_text);
....@@ -450,7 +453,10 @@
450453 SND_SOC_DAPM_MUX("IN3_R to Left Mixer Negative Resistor", SND_SOC_NOPM, 0, 0,
451454 in3r_to_lmixer_controls),
452455
453
- SND_SOC_DAPM_MICBIAS("Mic Bias", AIC32X4_MICBIAS, 6, 0),
456
+ SND_SOC_DAPM_SUPPLY("Mic Bias", AIC32X4_MICBIAS, 6, 0, mic_bias_event,
457
+ SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
458
+
459
+ SND_SOC_DAPM_POST("ADC Reset", aic32x4_reset_adc),
454460
455461 SND_SOC_DAPM_OUTPUT("HPL"),
456462 SND_SOC_DAPM_OUTPUT("HPR"),
....@@ -567,50 +573,35 @@
567573 static const struct regmap_range_cfg aic32x4_regmap_pages[] = {
568574 {
569575 .selector_reg = 0,
570
- .selector_mask = 0xff,
576
+ .selector_mask = 0xff,
571577 .window_start = 0,
572578 .window_len = 128,
573579 .range_min = 0,
574
- .range_max = AIC32X4_RMICPGAVOL,
580
+ .range_max = AIC32X4_REFPOWERUP,
575581 },
576582 };
577583
578584 const struct regmap_config aic32x4_regmap_config = {
579
- .max_register = AIC32X4_RMICPGAVOL,
585
+ .max_register = AIC32X4_REFPOWERUP,
580586 .ranges = aic32x4_regmap_pages,
581587 .num_ranges = ARRAY_SIZE(aic32x4_regmap_pages),
582588 };
583589 EXPORT_SYMBOL(aic32x4_regmap_config);
584590
585
-static inline int aic32x4_get_divs(int mclk, int rate)
586
-{
587
- int i;
588
-
589
- for (i = 0; i < ARRAY_SIZE(aic32x4_divs); i++) {
590
- if ((aic32x4_divs[i].rate == rate)
591
- && (aic32x4_divs[i].mclk == mclk)) {
592
- return i;
593
- }
594
- }
595
- printk(KERN_ERR "aic32x4: master clock and sample rate is not supported\n");
596
- return -EINVAL;
597
-}
598
-
599591 static int aic32x4_set_dai_sysclk(struct snd_soc_dai *codec_dai,
600592 int clk_id, unsigned int freq, int dir)
601593 {
602594 struct snd_soc_component *component = codec_dai->component;
603
- struct aic32x4_priv *aic32x4 = snd_soc_component_get_drvdata(component);
595
+ struct clk *mclk;
596
+ struct clk *pll;
604597
605
- switch (freq) {
606
- case 12000000:
607
- case 24000000:
608
- case 25000000:
609
- aic32x4->sysclk = freq;
610
- return 0;
611
- }
612
- printk(KERN_ERR "aic32x4: invalid frequency to set DAI system clock\n");
613
- return -EINVAL;
598
+ pll = devm_clk_get(component->dev, "pll");
599
+ if (IS_ERR(pll))
600
+ return PTR_ERR(pll);
601
+
602
+ mclk = clk_get_parent(pll);
603
+
604
+ return clk_set_rate(mclk, freq);
614605 }
615606
616607 static int aic32x4_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
....@@ -660,103 +651,180 @@
660651 }
661652
662653 snd_soc_component_update_bits(component, AIC32X4_IFACE1,
663
- AIC32X4_IFACE1_DATATYPE_MASK |
664
- AIC32X4_IFACE1_MASTER_MASK, iface_reg_1);
654
+ AIC32X4_IFACE1_DATATYPE_MASK |
655
+ AIC32X4_IFACE1_MASTER_MASK, iface_reg_1);
665656 snd_soc_component_update_bits(component, AIC32X4_IFACE2,
666
- AIC32X4_DATA_OFFSET_MASK, iface_reg_2);
657
+ AIC32X4_DATA_OFFSET_MASK, iface_reg_2);
667658 snd_soc_component_update_bits(component, AIC32X4_IFACE3,
668
- AIC32X4_BCLKINV_MASK, iface_reg_3);
659
+ AIC32X4_BCLKINV_MASK, iface_reg_3);
669660
670661 return 0;
671662 }
672663
664
+static int aic32x4_set_aosr(struct snd_soc_component *component, u8 aosr)
665
+{
666
+ return snd_soc_component_write(component, AIC32X4_AOSR, aosr);
667
+}
668
+
669
+static int aic32x4_set_dosr(struct snd_soc_component *component, u16 dosr)
670
+{
671
+ snd_soc_component_write(component, AIC32X4_DOSRMSB, dosr >> 8);
672
+ snd_soc_component_write(component, AIC32X4_DOSRLSB,
673
+ (dosr & 0xff));
674
+
675
+ return 0;
676
+}
677
+
678
+static int aic32x4_set_processing_blocks(struct snd_soc_component *component,
679
+ u8 r_block, u8 p_block)
680
+{
681
+ if (r_block > 18 || p_block > 25)
682
+ return -EINVAL;
683
+
684
+ snd_soc_component_write(component, AIC32X4_ADCSPB, r_block);
685
+ snd_soc_component_write(component, AIC32X4_DACSPB, p_block);
686
+
687
+ return 0;
688
+}
689
+
690
+static int aic32x4_setup_clocks(struct snd_soc_component *component,
691
+ unsigned int sample_rate, unsigned int channels,
692
+ unsigned int bit_depth)
693
+{
694
+ u8 aosr;
695
+ u16 dosr;
696
+ u8 adc_resource_class, dac_resource_class;
697
+ u8 madc, nadc, mdac, ndac, max_nadc, min_mdac, max_ndac;
698
+ u8 dosr_increment;
699
+ u16 max_dosr, min_dosr;
700
+ unsigned long adc_clock_rate, dac_clock_rate;
701
+ int ret;
702
+
703
+ struct clk_bulk_data clocks[] = {
704
+ { .id = "pll" },
705
+ { .id = "nadc" },
706
+ { .id = "madc" },
707
+ { .id = "ndac" },
708
+ { .id = "mdac" },
709
+ { .id = "bdiv" },
710
+ };
711
+ ret = devm_clk_bulk_get(component->dev, ARRAY_SIZE(clocks), clocks);
712
+ if (ret)
713
+ return ret;
714
+
715
+ if (sample_rate <= 48000) {
716
+ aosr = 128;
717
+ adc_resource_class = 6;
718
+ dac_resource_class = 8;
719
+ dosr_increment = 8;
720
+ aic32x4_set_processing_blocks(component, 1, 1);
721
+ } else if (sample_rate <= 96000) {
722
+ aosr = 64;
723
+ adc_resource_class = 6;
724
+ dac_resource_class = 8;
725
+ dosr_increment = 4;
726
+ aic32x4_set_processing_blocks(component, 1, 9);
727
+ } else if (sample_rate == 192000) {
728
+ aosr = 32;
729
+ adc_resource_class = 3;
730
+ dac_resource_class = 4;
731
+ dosr_increment = 2;
732
+ aic32x4_set_processing_blocks(component, 13, 19);
733
+ } else {
734
+ dev_err(component->dev, "Sampling rate not supported\n");
735
+ return -EINVAL;
736
+ }
737
+
738
+ madc = DIV_ROUND_UP((32 * adc_resource_class), aosr);
739
+ max_dosr = (AIC32X4_MAX_DOSR_FREQ / sample_rate / dosr_increment) *
740
+ dosr_increment;
741
+ min_dosr = (AIC32X4_MIN_DOSR_FREQ / sample_rate / dosr_increment) *
742
+ dosr_increment;
743
+ max_nadc = AIC32X4_MAX_CODEC_CLKIN_FREQ / (madc * aosr * sample_rate);
744
+
745
+ for (nadc = max_nadc; nadc > 0; --nadc) {
746
+ adc_clock_rate = nadc * madc * aosr * sample_rate;
747
+ for (dosr = max_dosr; dosr >= min_dosr;
748
+ dosr -= dosr_increment) {
749
+ min_mdac = DIV_ROUND_UP((32 * dac_resource_class), dosr);
750
+ max_ndac = AIC32X4_MAX_CODEC_CLKIN_FREQ /
751
+ (min_mdac * dosr * sample_rate);
752
+ for (mdac = min_mdac; mdac <= 128; ++mdac) {
753
+ for (ndac = max_ndac; ndac > 0; --ndac) {
754
+ dac_clock_rate = ndac * mdac * dosr *
755
+ sample_rate;
756
+ if (dac_clock_rate == adc_clock_rate) {
757
+ if (clk_round_rate(clocks[0].clk, dac_clock_rate) == 0)
758
+ continue;
759
+
760
+ clk_set_rate(clocks[0].clk,
761
+ dac_clock_rate);
762
+
763
+ clk_set_rate(clocks[1].clk,
764
+ sample_rate * aosr *
765
+ madc);
766
+ clk_set_rate(clocks[2].clk,
767
+ sample_rate * aosr);
768
+ aic32x4_set_aosr(component,
769
+ aosr);
770
+
771
+ clk_set_rate(clocks[3].clk,
772
+ sample_rate * dosr *
773
+ mdac);
774
+ clk_set_rate(clocks[4].clk,
775
+ sample_rate * dosr);
776
+ aic32x4_set_dosr(component,
777
+ dosr);
778
+
779
+ clk_set_rate(clocks[5].clk,
780
+ sample_rate * channels *
781
+ bit_depth);
782
+
783
+ return 0;
784
+ }
785
+ }
786
+ }
787
+ }
788
+ }
789
+
790
+ dev_err(component->dev,
791
+ "Could not set clocks to support sample rate.\n");
792
+ return -EINVAL;
793
+}
794
+
673795 static int aic32x4_hw_params(struct snd_pcm_substream *substream,
674
- struct snd_pcm_hw_params *params,
675
- struct snd_soc_dai *dai)
796
+ struct snd_pcm_hw_params *params,
797
+ struct snd_soc_dai *dai)
676798 {
677799 struct snd_soc_component *component = dai->component;
678800 struct aic32x4_priv *aic32x4 = snd_soc_component_get_drvdata(component);
679801 u8 iface1_reg = 0;
680802 u8 dacsetup_reg = 0;
681
- int i;
682803
683
- i = aic32x4_get_divs(aic32x4->sysclk, params_rate(params));
684
- if (i < 0) {
685
- printk(KERN_ERR "aic32x4: sampling rate not supported\n");
686
- return i;
687
- }
804
+ aic32x4_setup_clocks(component, params_rate(params),
805
+ params_channels(params),
806
+ params_physical_width(params));
688807
689
- /* MCLK as PLL_CLKIN */
690
- snd_soc_component_update_bits(component, AIC32X4_CLKMUX, AIC32X4_PLL_CLKIN_MASK,
691
- AIC32X4_PLL_CLKIN_MCLK << AIC32X4_PLL_CLKIN_SHIFT);
692
- /* PLL as CODEC_CLKIN */
693
- snd_soc_component_update_bits(component, AIC32X4_CLKMUX, AIC32X4_CODEC_CLKIN_MASK,
694
- AIC32X4_CODEC_CLKIN_PLL << AIC32X4_CODEC_CLKIN_SHIFT);
695
- /* DAC_MOD_CLK as BDIV_CLKIN */
696
- snd_soc_component_update_bits(component, AIC32X4_IFACE3, AIC32X4_BDIVCLK_MASK,
697
- AIC32X4_DACMOD2BCLK << AIC32X4_BDIVCLK_SHIFT);
698
-
699
- /* We will fix R value to 1 and will make P & J=K.D as variable */
700
- snd_soc_component_update_bits(component, AIC32X4_PLLPR, AIC32X4_PLL_R_MASK, 0x01);
701
-
702
- /* PLL P value */
703
- snd_soc_component_update_bits(component, AIC32X4_PLLPR, AIC32X4_PLL_P_MASK,
704
- aic32x4_divs[i].p_val << AIC32X4_PLL_P_SHIFT);
705
-
706
- /* PLL J value */
707
- snd_soc_component_write(component, AIC32X4_PLLJ, aic32x4_divs[i].pll_j);
708
-
709
- /* PLL D value */
710
- snd_soc_component_write(component, AIC32X4_PLLDMSB, (aic32x4_divs[i].pll_d >> 8));
711
- snd_soc_component_write(component, AIC32X4_PLLDLSB, (aic32x4_divs[i].pll_d & 0xff));
712
-
713
- /* NDAC divider value */
714
- snd_soc_component_update_bits(component, AIC32X4_NDAC,
715
- AIC32X4_NDAC_MASK, aic32x4_divs[i].ndac);
716
-
717
- /* MDAC divider value */
718
- snd_soc_component_update_bits(component, AIC32X4_MDAC,
719
- AIC32X4_MDAC_MASK, aic32x4_divs[i].mdac);
720
-
721
- /* DOSR MSB & LSB values */
722
- snd_soc_component_write(component, AIC32X4_DOSRMSB, aic32x4_divs[i].dosr >> 8);
723
- snd_soc_component_write(component, AIC32X4_DOSRLSB, (aic32x4_divs[i].dosr & 0xff));
724
-
725
- /* NADC divider value */
726
- snd_soc_component_update_bits(component, AIC32X4_NADC,
727
- AIC32X4_NADC_MASK, aic32x4_divs[i].nadc);
728
-
729
- /* MADC divider value */
730
- snd_soc_component_update_bits(component, AIC32X4_MADC,
731
- AIC32X4_MADC_MASK, aic32x4_divs[i].madc);
732
-
733
- /* AOSR value */
734
- snd_soc_component_write(component, AIC32X4_AOSR, aic32x4_divs[i].aosr);
735
-
736
- /* BCLK N divider */
737
- snd_soc_component_update_bits(component, AIC32X4_BCLKN,
738
- AIC32X4_BCLK_MASK, aic32x4_divs[i].blck_N);
739
-
740
- switch (params_width(params)) {
808
+ switch (params_physical_width(params)) {
741809 case 16:
742810 iface1_reg |= (AIC32X4_WORD_LEN_16BITS <<
743
- AIC32X4_IFACE1_DATALEN_SHIFT);
811
+ AIC32X4_IFACE1_DATALEN_SHIFT);
744812 break;
745813 case 20:
746814 iface1_reg |= (AIC32X4_WORD_LEN_20BITS <<
747
- AIC32X4_IFACE1_DATALEN_SHIFT);
815
+ AIC32X4_IFACE1_DATALEN_SHIFT);
748816 break;
749817 case 24:
750818 iface1_reg |= (AIC32X4_WORD_LEN_24BITS <<
751
- AIC32X4_IFACE1_DATALEN_SHIFT);
819
+ AIC32X4_IFACE1_DATALEN_SHIFT);
752820 break;
753821 case 32:
754822 iface1_reg |= (AIC32X4_WORD_LEN_32BITS <<
755
- AIC32X4_IFACE1_DATALEN_SHIFT);
823
+ AIC32X4_IFACE1_DATALEN_SHIFT);
756824 break;
757825 }
758826 snd_soc_component_update_bits(component, AIC32X4_IFACE1,
759
- AIC32X4_IFACE1_DATALEN_MASK, iface1_reg);
827
+ AIC32X4_IFACE1_DATALEN_MASK, iface1_reg);
760828
761829 if (params_channels(params) == 1) {
762830 dacsetup_reg = AIC32X4_RDAC2LCHN | AIC32X4_LDAC2LCHN;
....@@ -767,17 +835,17 @@
767835 dacsetup_reg = AIC32X4_LDAC2LCHN | AIC32X4_RDAC2RCHN;
768836 }
769837 snd_soc_component_update_bits(component, AIC32X4_DACSETUP,
770
- AIC32X4_DAC_CHAN_MASK, dacsetup_reg);
838
+ AIC32X4_DAC_CHAN_MASK, dacsetup_reg);
771839
772840 return 0;
773841 }
774842
775
-static int aic32x4_mute(struct snd_soc_dai *dai, int mute)
843
+static int aic32x4_mute(struct snd_soc_dai *dai, int mute, int direction)
776844 {
777845 struct snd_soc_component *component = dai->component;
778846
779847 snd_soc_component_update_bits(component, AIC32X4_DACMUTE,
780
- AIC32X4_MUTEON, mute ? AIC32X4_MUTEON : 0);
848
+ AIC32X4_MUTEON, mute ? AIC32X4_MUTEON : 0);
781849
782850 return 0;
783851 }
....@@ -785,41 +853,25 @@
785853 static int aic32x4_set_bias_level(struct snd_soc_component *component,
786854 enum snd_soc_bias_level level)
787855 {
788
- struct aic32x4_priv *aic32x4 = snd_soc_component_get_drvdata(component);
789856 int ret;
857
+
858
+ struct clk_bulk_data clocks[] = {
859
+ { .id = "madc" },
860
+ { .id = "mdac" },
861
+ { .id = "bdiv" },
862
+ };
863
+
864
+ ret = devm_clk_bulk_get(component->dev, ARRAY_SIZE(clocks), clocks);
865
+ if (ret)
866
+ return ret;
790867
791868 switch (level) {
792869 case SND_SOC_BIAS_ON:
793
- /* Switch on master clock */
794
- ret = clk_prepare_enable(aic32x4->mclk);
870
+ ret = clk_bulk_prepare_enable(ARRAY_SIZE(clocks), clocks);
795871 if (ret) {
796
- dev_err(component->dev, "Failed to enable master clock\n");
872
+ dev_err(component->dev, "Failed to enable clocks\n");
797873 return ret;
798874 }
799
-
800
- /* Switch on PLL */
801
- snd_soc_component_update_bits(component, AIC32X4_PLLPR,
802
- AIC32X4_PLLEN, AIC32X4_PLLEN);
803
-
804
- /* Switch on NDAC Divider */
805
- snd_soc_component_update_bits(component, AIC32X4_NDAC,
806
- AIC32X4_NDACEN, AIC32X4_NDACEN);
807
-
808
- /* Switch on MDAC Divider */
809
- snd_soc_component_update_bits(component, AIC32X4_MDAC,
810
- AIC32X4_MDACEN, AIC32X4_MDACEN);
811
-
812
- /* Switch on NADC Divider */
813
- snd_soc_component_update_bits(component, AIC32X4_NADC,
814
- AIC32X4_NADCEN, AIC32X4_NADCEN);
815
-
816
- /* Switch on MADC Divider */
817
- snd_soc_component_update_bits(component, AIC32X4_MADC,
818
- AIC32X4_MADCEN, AIC32X4_MADCEN);
819
-
820
- /* Switch on BCLK_N Divider */
821
- snd_soc_component_update_bits(component, AIC32X4_BCLKN,
822
- AIC32X4_BCLKEN, AIC32X4_BCLKEN);
823875 break;
824876 case SND_SOC_BIAS_PREPARE:
825877 break;
....@@ -828,32 +880,7 @@
828880 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF)
829881 break;
830882
831
- /* Switch off BCLK_N Divider */
832
- snd_soc_component_update_bits(component, AIC32X4_BCLKN,
833
- AIC32X4_BCLKEN, 0);
834
-
835
- /* Switch off MADC Divider */
836
- snd_soc_component_update_bits(component, AIC32X4_MADC,
837
- AIC32X4_MADCEN, 0);
838
-
839
- /* Switch off NADC Divider */
840
- snd_soc_component_update_bits(component, AIC32X4_NADC,
841
- AIC32X4_NADCEN, 0);
842
-
843
- /* Switch off MDAC Divider */
844
- snd_soc_component_update_bits(component, AIC32X4_MDAC,
845
- AIC32X4_MDACEN, 0);
846
-
847
- /* Switch off NDAC Divider */
848
- snd_soc_component_update_bits(component, AIC32X4_NDAC,
849
- AIC32X4_NDACEN, 0);
850
-
851
- /* Switch off PLL */
852
- snd_soc_component_update_bits(component, AIC32X4_PLLPR,
853
- AIC32X4_PLLEN, 0);
854
-
855
- /* Switch off master clock */
856
- clk_disable_unprepare(aic32x4->mclk);
883
+ clk_bulk_disable_unprepare(ARRAY_SIZE(clocks), clocks);
857884 break;
858885 case SND_SOC_BIAS_OFF:
859886 break;
....@@ -861,31 +888,33 @@
861888 return 0;
862889 }
863890
864
-#define AIC32X4_RATES SNDRV_PCM_RATE_8000_96000
865
-#define AIC32X4_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE \
866
- | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
891
+#define AIC32X4_RATES SNDRV_PCM_RATE_8000_192000
892
+#define AIC32X4_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE \
893
+ | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_3LE \
894
+ | SNDRV_PCM_FMTBIT_S32_LE)
867895
868896 static const struct snd_soc_dai_ops aic32x4_ops = {
869897 .hw_params = aic32x4_hw_params,
870
- .digital_mute = aic32x4_mute,
898
+ .mute_stream = aic32x4_mute,
871899 .set_fmt = aic32x4_set_dai_fmt,
872900 .set_sysclk = aic32x4_set_dai_sysclk,
901
+ .no_capture_mute = 1,
873902 };
874903
875904 static struct snd_soc_dai_driver aic32x4_dai = {
876905 .name = "tlv320aic32x4-hifi",
877906 .playback = {
878
- .stream_name = "Playback",
879
- .channels_min = 1,
880
- .channels_max = 2,
881
- .rates = AIC32X4_RATES,
882
- .formats = AIC32X4_FORMATS,},
907
+ .stream_name = "Playback",
908
+ .channels_min = 1,
909
+ .channels_max = 2,
910
+ .rates = AIC32X4_RATES,
911
+ .formats = AIC32X4_FORMATS,},
883912 .capture = {
884
- .stream_name = "Capture",
885
- .channels_min = 1,
886
- .channels_max = 2,
887
- .rates = AIC32X4_RATES,
888
- .formats = AIC32X4_FORMATS,},
913
+ .stream_name = "Capture",
914
+ .channels_min = 1,
915
+ .channels_max = 8,
916
+ .rates = AIC32X4_RATES,
917
+ .formats = AIC32X4_FORMATS,},
889918 .ops = &aic32x4_ops,
890919 .symmetric_rates = 1,
891920 };
....@@ -898,7 +927,7 @@
898927 /* MFP1 */
899928 if (aic32x4->setup->gpio_func[0] != AIC32X4_MFPX_DEFAULT_VALUE) {
900929 snd_soc_component_write(component, AIC32X4_DINCTL,
901
- aic32x4->setup->gpio_func[0]);
930
+ aic32x4->setup->gpio_func[0]);
902931 snd_soc_add_component_controls(component, aic32x4_mfp1,
903932 ARRAY_SIZE(aic32x4_mfp1));
904933 }
....@@ -906,7 +935,7 @@
906935 /* MFP2 */
907936 if (aic32x4->setup->gpio_func[1] != AIC32X4_MFPX_DEFAULT_VALUE) {
908937 snd_soc_component_write(component, AIC32X4_DOUTCTL,
909
- aic32x4->setup->gpio_func[1]);
938
+ aic32x4->setup->gpio_func[1]);
910939 snd_soc_add_component_controls(component, aic32x4_mfp2,
911940 ARRAY_SIZE(aic32x4_mfp2));
912941 }
....@@ -914,7 +943,7 @@
914943 /* MFP3 */
915944 if (aic32x4->setup->gpio_func[2] != AIC32X4_MFPX_DEFAULT_VALUE) {
916945 snd_soc_component_write(component, AIC32X4_SCLKCTL,
917
- aic32x4->setup->gpio_func[2]);
946
+ aic32x4->setup->gpio_func[2]);
918947 snd_soc_add_component_controls(component, aic32x4_mfp3,
919948 ARRAY_SIZE(aic32x4_mfp3));
920949 }
....@@ -922,7 +951,7 @@
922951 /* MFP4 */
923952 if (aic32x4->setup->gpio_func[3] != AIC32X4_MFPX_DEFAULT_VALUE) {
924953 snd_soc_component_write(component, AIC32X4_MISOCTL,
925
- aic32x4->setup->gpio_func[3]);
954
+ aic32x4->setup->gpio_func[3]);
926955 snd_soc_add_component_controls(component, aic32x4_mfp4,
927956 ARRAY_SIZE(aic32x4_mfp4));
928957 }
....@@ -930,7 +959,7 @@
930959 /* MFP5 */
931960 if (aic32x4->setup->gpio_func[4] != AIC32X4_MFPX_DEFAULT_VALUE) {
932961 snd_soc_component_write(component, AIC32X4_GPIOCTL,
933
- aic32x4->setup->gpio_func[4]);
962
+ aic32x4->setup->gpio_func[4]);
934963 snd_soc_add_component_controls(component, aic32x4_mfp5,
935964 ARRAY_SIZE(aic32x4_mfp5));
936965 }
....@@ -940,21 +969,29 @@
940969 {
941970 struct aic32x4_priv *aic32x4 = snd_soc_component_get_drvdata(component);
942971 u32 tmp_reg;
972
+ int ret;
943973
944
- if (gpio_is_valid(aic32x4->rstn_gpio)) {
945
- ndelay(10);
946
- gpio_set_value(aic32x4->rstn_gpio, 1);
947
- }
974
+ struct clk_bulk_data clocks[] = {
975
+ { .id = "codec_clkin" },
976
+ { .id = "pll" },
977
+ { .id = "bdiv" },
978
+ { .id = "mdac" },
979
+ };
948980
949
- snd_soc_component_write(component, AIC32X4_RESET, 0x01);
981
+ ret = devm_clk_bulk_get(component->dev, ARRAY_SIZE(clocks), clocks);
982
+ if (ret)
983
+ return ret;
950984
951985 if (aic32x4->setup)
952986 aic32x4_setup_gpios(component);
953987
988
+ clk_set_parent(clocks[0].clk, clocks[1].clk);
989
+ clk_set_parent(clocks[2].clk, clocks[3].clk);
990
+
954991 /* Power platform configuration */
955992 if (aic32x4->power_cfg & AIC32X4_PWR_MICBIAS_2075_LDOIN) {
956
- snd_soc_component_write(component, AIC32X4_MICBIAS, AIC32X4_MICBIAS_LDOIN |
957
- AIC32X4_MICBIAS_2075V);
993
+ snd_soc_component_write(component, AIC32X4_MICBIAS,
994
+ AIC32X4_MICBIAS_LDOIN | AIC32X4_MICBIAS_2075V);
958995 }
959996 if (aic32x4->power_cfg & AIC32X4_PWR_AVDD_DVDD_WEAK_DISABLE)
960997 snd_soc_component_write(component, AIC32X4_PWRCFG, AIC32X4_AVDDWEAKDISABLE);
....@@ -963,7 +1000,7 @@
9631000 AIC32X4_LDOCTLEN : 0;
9641001 snd_soc_component_write(component, AIC32X4_LDOCTL, tmp_reg);
9651002
966
- tmp_reg = snd_soc_component_read32(component, AIC32X4_CMMODE);
1003
+ tmp_reg = snd_soc_component_read(component, AIC32X4_CMMODE);
9671004 if (aic32x4->power_cfg & AIC32X4_PWR_CMMODE_LDOIN_RANGE_18_36)
9681005 tmp_reg |= AIC32X4_LDOIN_18_36;
9691006 if (aic32x4->power_cfg & AIC32X4_PWR_CMMODE_HP_LDOIN_POWERED)
....@@ -989,10 +1026,18 @@
9891026 * and down for the first capture to work properly. It seems related to
9901027 * a HW BUG or some kind of behavior not documented in the datasheet.
9911028 */
992
- tmp_reg = snd_soc_component_read32(component, AIC32X4_ADCSETUP);
1029
+ tmp_reg = snd_soc_component_read(component, AIC32X4_ADCSETUP);
9931030 snd_soc_component_write(component, AIC32X4_ADCSETUP, tmp_reg |
9941031 AIC32X4_LADC_EN | AIC32X4_RADC_EN);
9951032 snd_soc_component_write(component, AIC32X4_ADCSETUP, tmp_reg);
1033
+
1034
+ /*
1035
+ * Enable the fast charging feature and ensure the needed 40ms ellapsed
1036
+ * before using the analog circuits.
1037
+ */
1038
+ snd_soc_component_write(component, AIC32X4_REFPOWERUP,
1039
+ AIC32X4_REFPOWERUP_40MS);
1040
+ msleep(40);
9961041
9971042 return 0;
9981043 }
....@@ -1017,11 +1062,17 @@
10171062 struct device_node *np)
10181063 {
10191064 struct aic32x4_setup_data *aic32x4_setup;
1065
+ int ret;
10201066
10211067 aic32x4_setup = devm_kzalloc(aic32x4->dev, sizeof(*aic32x4_setup),
10221068 GFP_KERNEL);
10231069 if (!aic32x4_setup)
10241070 return -ENOMEM;
1071
+
1072
+ ret = of_property_match_string(np, "clock-names", "mclk");
1073
+ if (ret < 0)
1074
+ return -EINVAL;
1075
+ aic32x4->mclk_name = of_clk_get_parent_name(np, ret);
10251076
10261077 aic32x4->swapdacs = false;
10271078 aic32x4->micpga_routing = 0;
....@@ -1077,11 +1128,9 @@
10771128 return PTR_ERR(aic32x4->supply_av);
10781129 }
10791130 } else {
1080
- if (IS_ERR(aic32x4->supply_dv) &&
1081
- PTR_ERR(aic32x4->supply_dv) == -EPROBE_DEFER)
1131
+ if (PTR_ERR(aic32x4->supply_dv) == -EPROBE_DEFER)
10821132 return -EPROBE_DEFER;
1083
- if (IS_ERR(aic32x4->supply_av) &&
1084
- PTR_ERR(aic32x4->supply_av) == -EPROBE_DEFER)
1133
+ if (PTR_ERR(aic32x4->supply_av) == -EPROBE_DEFER)
10851134 return -EPROBE_DEFER;
10861135 }
10871136
....@@ -1144,7 +1193,7 @@
11441193 return PTR_ERR(regmap);
11451194
11461195 aic32x4 = devm_kzalloc(dev, sizeof(struct aic32x4_priv),
1147
- GFP_KERNEL);
1196
+ GFP_KERNEL);
11481197 if (aic32x4 == NULL)
11491198 return -ENOMEM;
11501199
....@@ -1156,6 +1205,7 @@
11561205 aic32x4->swapdacs = pdata->swapdacs;
11571206 aic32x4->micpga_routing = pdata->micpga_routing;
11581207 aic32x4->rstn_gpio = pdata->rstn_gpio;
1208
+ aic32x4->mclk_name = "mclk";
11591209 } else if (np) {
11601210 ret = aic32x4_parse_dt(aic32x4, np);
11611211 if (ret) {
....@@ -1167,12 +1217,7 @@
11671217 aic32x4->swapdacs = false;
11681218 aic32x4->micpga_routing = 0;
11691219 aic32x4->rstn_gpio = -1;
1170
- }
1171
-
1172
- aic32x4->mclk = devm_clk_get(dev, "mclk");
1173
- if (IS_ERR(aic32x4->mclk)) {
1174
- dev_err(dev, "Failed getting the mclk. The current implementation does not support the usage of this codec without mclk\n");
1175
- return PTR_ERR(aic32x4->mclk);
1220
+ aic32x4->mclk_name = "mclk";
11761221 }
11771222
11781223 if (gpio_is_valid(aic32x4->rstn_gpio)) {
....@@ -1188,15 +1233,33 @@
11881233 return ret;
11891234 }
11901235
1236
+ if (gpio_is_valid(aic32x4->rstn_gpio)) {
1237
+ ndelay(10);
1238
+ gpio_set_value_cansleep(aic32x4->rstn_gpio, 1);
1239
+ mdelay(1);
1240
+ }
1241
+
1242
+ ret = regmap_write(regmap, AIC32X4_RESET, 0x01);
1243
+ if (ret)
1244
+ goto err_disable_regulators;
1245
+
1246
+ ret = aic32x4_register_clocks(dev, aic32x4->mclk_name);
1247
+ if (ret)
1248
+ goto err_disable_regulators;
1249
+
11911250 ret = devm_snd_soc_register_component(dev,
11921251 &soc_component_dev_aic32x4, &aic32x4_dai, 1);
11931252 if (ret) {
11941253 dev_err(dev, "Failed to register component\n");
1195
- aic32x4_disable_regulators(aic32x4);
1196
- return ret;
1254
+ goto err_disable_regulators;
11971255 }
11981256
11991257 return 0;
1258
+
1259
+err_disable_regulators:
1260
+ aic32x4_disable_regulators(aic32x4);
1261
+
1262
+ return ret;
12001263 }
12011264 EXPORT_SYMBOL(aic32x4_probe);
12021265