GNU Linux-libre 4.9.337-gnu1
[releases.git] / sound / soc / codecs / tlv320aic32x4.c
1 /*
2  * linux/sound/soc/codecs/tlv320aic32x4.c
3  *
4  * Copyright 2011 Vista Silicon S.L.
5  *
6  * Author: Javier Martin <javier.martin@vista-silicon.com>
7  *
8  * 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.
24  */
25
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
29 #include <linux/delay.h>
30 #include <linux/pm.h>
31 #include <linux/gpio.h>
32 #include <linux/of_gpio.h>
33 #include <linux/cdev.h>
34 #include <linux/slab.h>
35 #include <linux/clk.h>
36 #include <linux/regulator/consumer.h>
37
38 #include <sound/tlv320aic32x4.h>
39 #include <sound/core.h>
40 #include <sound/pcm.h>
41 #include <sound/pcm_params.h>
42 #include <sound/soc.h>
43 #include <sound/soc-dapm.h>
44 #include <sound/initval.h>
45 #include <sound/tlv.h>
46
47 #include "tlv320aic32x4.h"
48
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
64 struct aic32x4_priv {
65         struct regmap *regmap;
66         u32 sysclk;
67         u32 power_cfg;
68         u32 micpga_routing;
69         bool swapdacs;
70         int rstn_gpio;
71         struct clk *mclk;
72
73         struct regulator *supply_ldo;
74         struct regulator *supply_iov;
75         struct regulator *supply_dv;
76         struct regulator *supply_av;
77 };
78
79 /* 0dB min, 0.5dB steps */
80 static DECLARE_TLV_DB_SCALE(tlv_step_0_5, 0, 50, 0);
81 /* -63.5dB min, 0.5dB steps */
82 static DECLARE_TLV_DB_SCALE(tlv_pcm, -6350, 50, 0);
83 /* -6dB min, 1dB steps */
84 static DECLARE_TLV_DB_SCALE(tlv_driver_gain, -600, 100, 0);
85 /* -12dB min, 0.5dB steps */
86 static DECLARE_TLV_DB_SCALE(tlv_adc_vol, -1200, 50, 0);
87
88 static const struct snd_kcontrol_new aic32x4_snd_controls[] = {
89         SOC_DOUBLE_R_S_TLV("PCM Playback Volume", AIC32X4_LDACVOL,
90                         AIC32X4_RDACVOL, 0, -0x7f, 0x30, 7, 0, tlv_pcm),
91         SOC_DOUBLE_R_S_TLV("HP Driver Gain Volume", AIC32X4_HPLGAIN,
92                         AIC32X4_HPRGAIN, 0, -0x6, 0x1d, 5, 0,
93                         tlv_driver_gain),
94         SOC_DOUBLE_R_S_TLV("LO Driver Gain Volume", AIC32X4_LOLGAIN,
95                         AIC32X4_LORGAIN, 0, -0x6, 0x1d, 5, 0,
96                         tlv_driver_gain),
97         SOC_DOUBLE_R("HP DAC Playback Switch", AIC32X4_HPLGAIN,
98                         AIC32X4_HPRGAIN, 6, 0x01, 1),
99         SOC_DOUBLE_R("LO DAC Playback Switch", AIC32X4_LOLGAIN,
100                         AIC32X4_LORGAIN, 6, 0x01, 1),
101         SOC_DOUBLE_R("Mic PGA Switch", AIC32X4_LMICPGAVOL,
102                         AIC32X4_RMICPGAVOL, 7, 0x01, 1),
103
104         SOC_SINGLE("ADCFGA Left Mute Switch", AIC32X4_ADCFGA, 7, 1, 0),
105         SOC_SINGLE("ADCFGA Right Mute Switch", AIC32X4_ADCFGA, 3, 1, 0),
106
107         SOC_DOUBLE_R_S_TLV("ADC Level Volume", AIC32X4_LADCVOL,
108                         AIC32X4_RADCVOL, 0, -0x18, 0x28, 6, 0, tlv_adc_vol),
109         SOC_DOUBLE_R_TLV("PGA Level Volume", AIC32X4_LMICPGAVOL,
110                         AIC32X4_RMICPGAVOL, 0, 0x5f, 0, tlv_step_0_5),
111
112         SOC_SINGLE("Auto-mute Switch", AIC32X4_DACMUTE, 4, 7, 0),
113
114         SOC_SINGLE("AGC Left Switch", AIC32X4_LAGC1, 7, 1, 0),
115         SOC_SINGLE("AGC Right Switch", AIC32X4_RAGC1, 7, 1, 0),
116         SOC_DOUBLE_R("AGC Target Level", AIC32X4_LAGC1, AIC32X4_RAGC1,
117                         4, 0x07, 0),
118         SOC_DOUBLE_R("AGC Gain Hysteresis", AIC32X4_LAGC1, AIC32X4_RAGC1,
119                         0, 0x03, 0),
120         SOC_DOUBLE_R("AGC Hysteresis", AIC32X4_LAGC2, AIC32X4_RAGC2,
121                         6, 0x03, 0),
122         SOC_DOUBLE_R("AGC Noise Threshold", AIC32X4_LAGC2, AIC32X4_RAGC2,
123                         1, 0x1F, 0),
124         SOC_DOUBLE_R("AGC Max PGA", AIC32X4_LAGC3, AIC32X4_RAGC3,
125                         0, 0x7F, 0),
126         SOC_DOUBLE_R("AGC Attack Time", AIC32X4_LAGC4, AIC32X4_RAGC4,
127                         3, 0x1F, 0),
128         SOC_DOUBLE_R("AGC Decay Time", AIC32X4_LAGC5, AIC32X4_RAGC5,
129                         3, 0x1F, 0),
130         SOC_DOUBLE_R("AGC Noise Debounce", AIC32X4_LAGC6, AIC32X4_RAGC6,
131                         0, 0x1F, 0),
132         SOC_DOUBLE_R("AGC Signal Debounce", AIC32X4_LAGC7, AIC32X4_RAGC7,
133                         0, 0x0F, 0),
134 };
135
136 static const struct aic32x4_rate_divs aic32x4_divs[] = {
137         /* 8k rate */
138         {AIC32X4_FREQ_12000000, 8000, 1, 7, 6800, 768, 5, 3, 128, 5, 18, 24},
139         {AIC32X4_FREQ_24000000, 8000, 2, 7, 6800, 768, 15, 1, 64, 45, 4, 24},
140         {AIC32X4_FREQ_25000000, 8000, 2, 7, 3728, 768, 15, 1, 64, 45, 4, 24},
141         /* 11.025k rate */
142         {AIC32X4_FREQ_12000000, 11025, 1, 7, 5264, 512, 8, 2, 128, 8, 8, 16},
143         {AIC32X4_FREQ_24000000, 11025, 2, 7, 5264, 512, 16, 1, 64, 32, 4, 16},
144         /* 16k rate */
145         {AIC32X4_FREQ_12000000, 16000, 1, 7, 6800, 384, 5, 3, 128, 5, 9, 12},
146         {AIC32X4_FREQ_24000000, 16000, 2, 7, 6800, 384, 15, 1, 64, 18, 5, 12},
147         {AIC32X4_FREQ_25000000, 16000, 2, 7, 3728, 384, 15, 1, 64, 18, 5, 12},
148         /* 22.05k rate */
149         {AIC32X4_FREQ_12000000, 22050, 1, 7, 5264, 256, 4, 4, 128, 4, 8, 8},
150         {AIC32X4_FREQ_24000000, 22050, 2, 7, 5264, 256, 16, 1, 64, 16, 4, 8},
151         {AIC32X4_FREQ_25000000, 22050, 2, 7, 2253, 256, 16, 1, 64, 16, 4, 8},
152         /* 32k rate */
153         {AIC32X4_FREQ_12000000, 32000, 1, 7, 1680, 192, 2, 7, 64, 2, 21, 6},
154         {AIC32X4_FREQ_24000000, 32000, 2, 7, 1680, 192, 7, 2, 64, 7, 6, 6},
155         /* 44.1k rate */
156         {AIC32X4_FREQ_12000000, 44100, 1, 7, 5264, 128, 2, 8, 128, 2, 8, 4},
157         {AIC32X4_FREQ_24000000, 44100, 2, 7, 5264, 128, 8, 2, 64, 8, 4, 4},
158         {AIC32X4_FREQ_25000000, 44100, 2, 7, 2253, 128, 8, 2, 64, 8, 4, 4},
159         /* 48k rate */
160         {AIC32X4_FREQ_12000000, 48000, 1, 8, 1920, 128, 2, 8, 128, 2, 8, 4},
161         {AIC32X4_FREQ_24000000, 48000, 2, 8, 1920, 128, 8, 2, 64, 8, 4, 4},
162         {AIC32X4_FREQ_25000000, 48000, 2, 7, 8643, 128, 8, 2, 64, 8, 4, 4},
163
164         /* 96k rate */
165         {AIC32X4_FREQ_25000000, 96000, 2, 7, 8643, 64, 4, 4, 64, 4, 4, 1},
166 };
167
168 static const struct snd_kcontrol_new hpl_output_mixer_controls[] = {
169         SOC_DAPM_SINGLE("L_DAC Switch", AIC32X4_HPLROUTE, 3, 1, 0),
170         SOC_DAPM_SINGLE("IN1_L Switch", AIC32X4_HPLROUTE, 2, 1, 0),
171 };
172
173 static const struct snd_kcontrol_new hpr_output_mixer_controls[] = {
174         SOC_DAPM_SINGLE("R_DAC Switch", AIC32X4_HPRROUTE, 3, 1, 0),
175         SOC_DAPM_SINGLE("IN1_R Switch", AIC32X4_HPRROUTE, 2, 1, 0),
176 };
177
178 static const struct snd_kcontrol_new lol_output_mixer_controls[] = {
179         SOC_DAPM_SINGLE("L_DAC Switch", AIC32X4_LOLROUTE, 3, 1, 0),
180 };
181
182 static const struct snd_kcontrol_new lor_output_mixer_controls[] = {
183         SOC_DAPM_SINGLE("R_DAC Switch", AIC32X4_LORROUTE, 3, 1, 0),
184 };
185
186 static const char * const resistor_text[] = {
187         "Off", "10 kOhm", "20 kOhm", "40 kOhm",
188 };
189
190 /* Left mixer pins */
191 static SOC_ENUM_SINGLE_DECL(in1l_lpga_p_enum, AIC32X4_LMICPGAPIN, 6, resistor_text);
192 static SOC_ENUM_SINGLE_DECL(in2l_lpga_p_enum, AIC32X4_LMICPGAPIN, 4, resistor_text);
193 static SOC_ENUM_SINGLE_DECL(in3l_lpga_p_enum, AIC32X4_LMICPGAPIN, 2, resistor_text);
194 static SOC_ENUM_SINGLE_DECL(in1r_lpga_p_enum, AIC32X4_LMICPGAPIN, 0, resistor_text);
195
196 static SOC_ENUM_SINGLE_DECL(cml_lpga_n_enum, AIC32X4_LMICPGANIN, 6, resistor_text);
197 static SOC_ENUM_SINGLE_DECL(in2r_lpga_n_enum, AIC32X4_LMICPGANIN, 4, resistor_text);
198 static SOC_ENUM_SINGLE_DECL(in3r_lpga_n_enum, AIC32X4_LMICPGANIN, 2, resistor_text);
199
200 static const struct snd_kcontrol_new in1l_to_lmixer_controls[] = {
201         SOC_DAPM_ENUM("IN1_L L+ Switch", in1l_lpga_p_enum),
202 };
203 static const struct snd_kcontrol_new in2l_to_lmixer_controls[] = {
204         SOC_DAPM_ENUM("IN2_L L+ Switch", in2l_lpga_p_enum),
205 };
206 static const struct snd_kcontrol_new in3l_to_lmixer_controls[] = {
207         SOC_DAPM_ENUM("IN3_L L+ Switch", in3l_lpga_p_enum),
208 };
209 static const struct snd_kcontrol_new in1r_to_lmixer_controls[] = {
210         SOC_DAPM_ENUM("IN1_R L+ Switch", in1r_lpga_p_enum),
211 };
212 static const struct snd_kcontrol_new cml_to_lmixer_controls[] = {
213         SOC_DAPM_ENUM("CM_L L- Switch", cml_lpga_n_enum),
214 };
215 static const struct snd_kcontrol_new in2r_to_lmixer_controls[] = {
216         SOC_DAPM_ENUM("IN2_R L- Switch", in2r_lpga_n_enum),
217 };
218 static const struct snd_kcontrol_new in3r_to_lmixer_controls[] = {
219         SOC_DAPM_ENUM("IN3_R L- Switch", in3r_lpga_n_enum),
220 };
221
222 /*  Right mixer pins */
223 static SOC_ENUM_SINGLE_DECL(in1r_rpga_p_enum, AIC32X4_RMICPGAPIN, 6, resistor_text);
224 static SOC_ENUM_SINGLE_DECL(in2r_rpga_p_enum, AIC32X4_RMICPGAPIN, 4, resistor_text);
225 static SOC_ENUM_SINGLE_DECL(in3r_rpga_p_enum, AIC32X4_RMICPGAPIN, 2, resistor_text);
226 static SOC_ENUM_SINGLE_DECL(in2l_rpga_p_enum, AIC32X4_RMICPGAPIN, 0, resistor_text);
227 static SOC_ENUM_SINGLE_DECL(cmr_rpga_n_enum, AIC32X4_RMICPGANIN, 6, resistor_text);
228 static SOC_ENUM_SINGLE_DECL(in1l_rpga_n_enum, AIC32X4_RMICPGANIN, 4, resistor_text);
229 static SOC_ENUM_SINGLE_DECL(in3l_rpga_n_enum, AIC32X4_RMICPGANIN, 2, resistor_text);
230
231 static const struct snd_kcontrol_new in1r_to_rmixer_controls[] = {
232         SOC_DAPM_ENUM("IN1_R R+ Switch", in1r_rpga_p_enum),
233 };
234 static const struct snd_kcontrol_new in2r_to_rmixer_controls[] = {
235         SOC_DAPM_ENUM("IN2_R R+ Switch", in2r_rpga_p_enum),
236 };
237 static const struct snd_kcontrol_new in3r_to_rmixer_controls[] = {
238         SOC_DAPM_ENUM("IN3_R R+ Switch", in3r_rpga_p_enum),
239 };
240 static const struct snd_kcontrol_new in2l_to_rmixer_controls[] = {
241         SOC_DAPM_ENUM("IN2_L R+ Switch", in2l_rpga_p_enum),
242 };
243 static const struct snd_kcontrol_new cmr_to_rmixer_controls[] = {
244         SOC_DAPM_ENUM("CM_R R- Switch", cmr_rpga_n_enum),
245 };
246 static const struct snd_kcontrol_new in1l_to_rmixer_controls[] = {
247         SOC_DAPM_ENUM("IN1_L R- Switch", in1l_rpga_n_enum),
248 };
249 static const struct snd_kcontrol_new in3l_to_rmixer_controls[] = {
250         SOC_DAPM_ENUM("IN3_L R- Switch", in3l_rpga_n_enum),
251 };
252
253 static const struct snd_soc_dapm_widget aic32x4_dapm_widgets[] = {
254         SND_SOC_DAPM_DAC("Left DAC", "Left Playback", AIC32X4_DACSETUP, 7, 0),
255         SND_SOC_DAPM_MIXER("HPL Output Mixer", SND_SOC_NOPM, 0, 0,
256                            &hpl_output_mixer_controls[0],
257                            ARRAY_SIZE(hpl_output_mixer_controls)),
258         SND_SOC_DAPM_PGA("HPL Power", AIC32X4_OUTPWRCTL, 5, 0, NULL, 0),
259
260         SND_SOC_DAPM_MIXER("LOL Output Mixer", SND_SOC_NOPM, 0, 0,
261                            &lol_output_mixer_controls[0],
262                            ARRAY_SIZE(lol_output_mixer_controls)),
263         SND_SOC_DAPM_PGA("LOL Power", AIC32X4_OUTPWRCTL, 3, 0, NULL, 0),
264
265         SND_SOC_DAPM_DAC("Right DAC", "Right Playback", AIC32X4_DACSETUP, 6, 0),
266         SND_SOC_DAPM_MIXER("HPR Output Mixer", SND_SOC_NOPM, 0, 0,
267                            &hpr_output_mixer_controls[0],
268                            ARRAY_SIZE(hpr_output_mixer_controls)),
269         SND_SOC_DAPM_PGA("HPR Power", AIC32X4_OUTPWRCTL, 4, 0, NULL, 0),
270         SND_SOC_DAPM_MIXER("LOR Output Mixer", SND_SOC_NOPM, 0, 0,
271                            &lor_output_mixer_controls[0],
272                            ARRAY_SIZE(lor_output_mixer_controls)),
273         SND_SOC_DAPM_PGA("LOR Power", AIC32X4_OUTPWRCTL, 2, 0, NULL, 0),
274
275         SND_SOC_DAPM_ADC("Right ADC", "Right Capture", AIC32X4_ADCSETUP, 6, 0),
276         SND_SOC_DAPM_MUX("IN1_R to Right Mixer Positive Resistor", SND_SOC_NOPM, 0, 0,
277                         in1r_to_rmixer_controls),
278         SND_SOC_DAPM_MUX("IN2_R to Right Mixer Positive Resistor", SND_SOC_NOPM, 0, 0,
279                         in2r_to_rmixer_controls),
280         SND_SOC_DAPM_MUX("IN3_R to Right Mixer Positive Resistor", SND_SOC_NOPM, 0, 0,
281                         in3r_to_rmixer_controls),
282         SND_SOC_DAPM_MUX("IN2_L to Right Mixer Positive Resistor", SND_SOC_NOPM, 0, 0,
283                         in2l_to_rmixer_controls),
284         SND_SOC_DAPM_MUX("CM_R to Right Mixer Negative Resistor", SND_SOC_NOPM, 0, 0,
285                         cmr_to_rmixer_controls),
286         SND_SOC_DAPM_MUX("IN1_L to Right Mixer Negative Resistor", SND_SOC_NOPM, 0, 0,
287                         in1l_to_rmixer_controls),
288         SND_SOC_DAPM_MUX("IN3_L to Right Mixer Negative Resistor", SND_SOC_NOPM, 0, 0,
289                         in3l_to_rmixer_controls),
290
291         SND_SOC_DAPM_ADC("Left ADC", "Left Capture", AIC32X4_ADCSETUP, 7, 0),
292         SND_SOC_DAPM_MUX("IN1_L to Left Mixer Positive Resistor", SND_SOC_NOPM, 0, 0,
293                         in1l_to_lmixer_controls),
294         SND_SOC_DAPM_MUX("IN2_L to Left Mixer Positive Resistor", SND_SOC_NOPM, 0, 0,
295                         in2l_to_lmixer_controls),
296         SND_SOC_DAPM_MUX("IN3_L to Left Mixer Positive Resistor", SND_SOC_NOPM, 0, 0,
297                         in3l_to_lmixer_controls),
298         SND_SOC_DAPM_MUX("IN1_R to Left Mixer Positive Resistor", SND_SOC_NOPM, 0, 0,
299                         in1r_to_lmixer_controls),
300         SND_SOC_DAPM_MUX("CM_L to Left Mixer Negative Resistor", SND_SOC_NOPM, 0, 0,
301                         cml_to_lmixer_controls),
302         SND_SOC_DAPM_MUX("IN2_R to Left Mixer Negative Resistor", SND_SOC_NOPM, 0, 0,
303                         in2r_to_lmixer_controls),
304         SND_SOC_DAPM_MUX("IN3_R to Left Mixer Negative Resistor", SND_SOC_NOPM, 0, 0,
305                         in3r_to_lmixer_controls),
306
307         SND_SOC_DAPM_MICBIAS("Mic Bias", AIC32X4_MICBIAS, 6, 0),
308
309         SND_SOC_DAPM_OUTPUT("HPL"),
310         SND_SOC_DAPM_OUTPUT("HPR"),
311         SND_SOC_DAPM_OUTPUT("LOL"),
312         SND_SOC_DAPM_OUTPUT("LOR"),
313         SND_SOC_DAPM_INPUT("IN1_L"),
314         SND_SOC_DAPM_INPUT("IN1_R"),
315         SND_SOC_DAPM_INPUT("IN2_L"),
316         SND_SOC_DAPM_INPUT("IN2_R"),
317         SND_SOC_DAPM_INPUT("IN3_L"),
318         SND_SOC_DAPM_INPUT("IN3_R"),
319         SND_SOC_DAPM_INPUT("CM_L"),
320         SND_SOC_DAPM_INPUT("CM_R"),
321 };
322
323 static const struct snd_soc_dapm_route aic32x4_dapm_routes[] = {
324         /* Left Output */
325         {"HPL Output Mixer", "L_DAC Switch", "Left DAC"},
326         {"HPL Output Mixer", "IN1_L Switch", "IN1_L"},
327
328         {"HPL Power", NULL, "HPL Output Mixer"},
329         {"HPL", NULL, "HPL Power"},
330
331         {"LOL Output Mixer", "L_DAC Switch", "Left DAC"},
332
333         {"LOL Power", NULL, "LOL Output Mixer"},
334         {"LOL", NULL, "LOL Power"},
335
336         /* Right Output */
337         {"HPR Output Mixer", "R_DAC Switch", "Right DAC"},
338         {"HPR Output Mixer", "IN1_R Switch", "IN1_R"},
339
340         {"HPR Power", NULL, "HPR Output Mixer"},
341         {"HPR", NULL, "HPR Power"},
342
343         {"LOR Output Mixer", "R_DAC Switch", "Right DAC"},
344
345         {"LOR Power", NULL, "LOR Output Mixer"},
346         {"LOR", NULL, "LOR Power"},
347
348         /* Right Input */
349         {"Right ADC", NULL, "IN1_R to Right Mixer Positive Resistor"},
350         {"IN1_R to Right Mixer Positive Resistor", "10 kOhm", "IN1_R"},
351         {"IN1_R to Right Mixer Positive Resistor", "20 kOhm", "IN1_R"},
352         {"IN1_R to Right Mixer Positive Resistor", "40 kOhm", "IN1_R"},
353
354         {"Right ADC", NULL, "IN2_R to Right Mixer Positive Resistor"},
355         {"IN2_R to Right Mixer Positive Resistor", "10 kOhm", "IN2_R"},
356         {"IN2_R to Right Mixer Positive Resistor", "20 kOhm", "IN2_R"},
357         {"IN2_R to Right Mixer Positive Resistor", "40 kOhm", "IN2_R"},
358
359         {"Right ADC", NULL, "IN3_R to Right Mixer Positive Resistor"},
360         {"IN3_R to Right Mixer Positive Resistor", "10 kOhm", "IN3_R"},
361         {"IN3_R to Right Mixer Positive Resistor", "20 kOhm", "IN3_R"},
362         {"IN3_R to Right Mixer Positive Resistor", "40 kOhm", "IN3_R"},
363
364         {"Right ADC", NULL, "IN2_L to Right Mixer Positive Resistor"},
365         {"IN2_L to Right Mixer Positive Resistor", "10 kOhm", "IN2_L"},
366         {"IN2_L to Right Mixer Positive Resistor", "20 kOhm", "IN2_L"},
367         {"IN2_L to Right Mixer Positive Resistor", "40 kOhm", "IN2_L"},
368
369         {"Right ADC", NULL, "CM_R to Right Mixer Negative Resistor"},
370         {"CM_R to Right Mixer Negative Resistor", "10 kOhm", "CM_R"},
371         {"CM_R to Right Mixer Negative Resistor", "20 kOhm", "CM_R"},
372         {"CM_R to Right Mixer Negative Resistor", "40 kOhm", "CM_R"},
373
374         {"Right ADC", NULL, "IN1_L to Right Mixer Negative Resistor"},
375         {"IN1_L to Right Mixer Negative Resistor", "10 kOhm", "IN1_L"},
376         {"IN1_L to Right Mixer Negative Resistor", "20 kOhm", "IN1_L"},
377         {"IN1_L to Right Mixer Negative Resistor", "40 kOhm", "IN1_L"},
378
379         {"Right ADC", NULL, "IN3_L to Right Mixer Negative Resistor"},
380         {"IN3_L to Right Mixer Negative Resistor", "10 kOhm", "IN3_L"},
381         {"IN3_L to Right Mixer Negative Resistor", "20 kOhm", "IN3_L"},
382         {"IN3_L to Right Mixer Negative Resistor", "40 kOhm", "IN3_L"},
383
384         /* Left Input */
385         {"Left ADC", NULL, "IN1_L to Left Mixer Positive Resistor"},
386         {"IN1_L to Left Mixer Positive Resistor", "10 kOhm", "IN1_L"},
387         {"IN1_L to Left Mixer Positive Resistor", "20 kOhm", "IN1_L"},
388         {"IN1_L to Left Mixer Positive Resistor", "40 kOhm", "IN1_L"},
389
390         {"Left ADC", NULL, "IN2_L to Left Mixer Positive Resistor"},
391         {"IN2_L to Left Mixer Positive Resistor", "10 kOhm", "IN2_L"},
392         {"IN2_L to Left Mixer Positive Resistor", "20 kOhm", "IN2_L"},
393         {"IN2_L to Left Mixer Positive Resistor", "40 kOhm", "IN2_L"},
394
395         {"Left ADC", NULL, "IN3_L to Left Mixer Positive Resistor"},
396         {"IN3_L to Left Mixer Positive Resistor", "10 kOhm", "IN3_L"},
397         {"IN3_L to Left Mixer Positive Resistor", "20 kOhm", "IN3_L"},
398         {"IN3_L to Left Mixer Positive Resistor", "40 kOhm", "IN3_L"},
399
400         {"Left ADC", NULL, "IN1_R to Left Mixer Positive Resistor"},
401         {"IN1_R to Left Mixer Positive Resistor", "10 kOhm", "IN1_R"},
402         {"IN1_R to Left Mixer Positive Resistor", "20 kOhm", "IN1_R"},
403         {"IN1_R to Left Mixer Positive Resistor", "40 kOhm", "IN1_R"},
404
405         {"Left ADC", NULL, "CM_L to Left Mixer Negative Resistor"},
406         {"CM_L to Left Mixer Negative Resistor", "10 kOhm", "CM_L"},
407         {"CM_L to Left Mixer Negative Resistor", "20 kOhm", "CM_L"},
408         {"CM_L to Left Mixer Negative Resistor", "40 kOhm", "CM_L"},
409
410         {"Left ADC", NULL, "IN2_R to Left Mixer Negative Resistor"},
411         {"IN2_R to Left Mixer Negative Resistor", "10 kOhm", "IN2_R"},
412         {"IN2_R to Left Mixer Negative Resistor", "20 kOhm", "IN2_R"},
413         {"IN2_R to Left Mixer Negative Resistor", "40 kOhm", "IN2_R"},
414
415         {"Left ADC", NULL, "IN3_R to Left Mixer Negative Resistor"},
416         {"IN3_R to Left Mixer Negative Resistor", "10 kOhm", "IN3_R"},
417         {"IN3_R to Left Mixer Negative Resistor", "20 kOhm", "IN3_R"},
418         {"IN3_R to Left Mixer Negative Resistor", "40 kOhm", "IN3_R"},
419 };
420
421 static const struct regmap_range_cfg aic32x4_regmap_pages[] = {
422         {
423                 .selector_reg = 0,
424                 .selector_mask  = 0xff,
425                 .window_start = 0,
426                 .window_len = 128,
427                 .range_min = 0,
428                 .range_max = AIC32X4_RMICPGAVOL,
429         },
430 };
431
432 const struct regmap_config aic32x4_regmap_config = {
433         .max_register = AIC32X4_RMICPGAVOL,
434         .ranges = aic32x4_regmap_pages,
435         .num_ranges = ARRAY_SIZE(aic32x4_regmap_pages),
436 };
437 EXPORT_SYMBOL(aic32x4_regmap_config);
438
439 static inline int aic32x4_get_divs(int mclk, int rate)
440 {
441         int i;
442
443         for (i = 0; i < ARRAY_SIZE(aic32x4_divs); i++) {
444                 if ((aic32x4_divs[i].rate == rate)
445                     && (aic32x4_divs[i].mclk == mclk)) {
446                         return i;
447                 }
448         }
449         printk(KERN_ERR "aic32x4: master clock and sample rate is not supported\n");
450         return -EINVAL;
451 }
452
453 static int aic32x4_set_dai_sysclk(struct snd_soc_dai *codec_dai,
454                                   int clk_id, unsigned int freq, int dir)
455 {
456         struct snd_soc_codec *codec = codec_dai->codec;
457         struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
458
459         switch (freq) {
460         case AIC32X4_FREQ_12000000:
461         case AIC32X4_FREQ_24000000:
462         case AIC32X4_FREQ_25000000:
463                 aic32x4->sysclk = freq;
464                 return 0;
465         }
466         printk(KERN_ERR "aic32x4: invalid frequency to set DAI system clock\n");
467         return -EINVAL;
468 }
469
470 static int aic32x4_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
471 {
472         struct snd_soc_codec *codec = codec_dai->codec;
473         u8 iface_reg_1;
474         u8 iface_reg_2;
475         u8 iface_reg_3;
476
477         iface_reg_1 = snd_soc_read(codec, AIC32X4_IFACE1);
478         iface_reg_1 = iface_reg_1 & ~(3 << 6 | 3 << 2);
479         iface_reg_2 = snd_soc_read(codec, AIC32X4_IFACE2);
480         iface_reg_2 = 0;
481         iface_reg_3 = snd_soc_read(codec, AIC32X4_IFACE3);
482         iface_reg_3 = iface_reg_3 & ~(1 << 3);
483
484         /* set master/slave audio interface */
485         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
486         case SND_SOC_DAIFMT_CBM_CFM:
487                 iface_reg_1 |= AIC32X4_BCLKMASTER | AIC32X4_WCLKMASTER;
488                 break;
489         case SND_SOC_DAIFMT_CBS_CFS:
490                 break;
491         default:
492                 printk(KERN_ERR "aic32x4: invalid DAI master/slave interface\n");
493                 return -EINVAL;
494         }
495
496         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
497         case SND_SOC_DAIFMT_I2S:
498                 break;
499         case SND_SOC_DAIFMT_DSP_A:
500                 iface_reg_1 |= (AIC32X4_DSP_MODE << AIC32X4_PLLJ_SHIFT);
501                 iface_reg_3 |= (1 << 3); /* invert bit clock */
502                 iface_reg_2 = 0x01; /* add offset 1 */
503                 break;
504         case SND_SOC_DAIFMT_DSP_B:
505                 iface_reg_1 |= (AIC32X4_DSP_MODE << AIC32X4_PLLJ_SHIFT);
506                 iface_reg_3 |= (1 << 3); /* invert bit clock */
507                 break;
508         case SND_SOC_DAIFMT_RIGHT_J:
509                 iface_reg_1 |=
510                         (AIC32X4_RIGHT_JUSTIFIED_MODE << AIC32X4_PLLJ_SHIFT);
511                 break;
512         case SND_SOC_DAIFMT_LEFT_J:
513                 iface_reg_1 |=
514                         (AIC32X4_LEFT_JUSTIFIED_MODE << AIC32X4_PLLJ_SHIFT);
515                 break;
516         default:
517                 printk(KERN_ERR "aic32x4: invalid DAI interface format\n");
518                 return -EINVAL;
519         }
520
521         snd_soc_write(codec, AIC32X4_IFACE1, iface_reg_1);
522         snd_soc_write(codec, AIC32X4_IFACE2, iface_reg_2);
523         snd_soc_write(codec, AIC32X4_IFACE3, iface_reg_3);
524         return 0;
525 }
526
527 static int aic32x4_hw_params(struct snd_pcm_substream *substream,
528                              struct snd_pcm_hw_params *params,
529                              struct snd_soc_dai *dai)
530 {
531         struct snd_soc_codec *codec = dai->codec;
532         struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
533         u8 data;
534         int i;
535
536         i = aic32x4_get_divs(aic32x4->sysclk, params_rate(params));
537         if (i < 0) {
538                 printk(KERN_ERR "aic32x4: sampling rate not supported\n");
539                 return i;
540         }
541
542         /* Use PLL as CODEC_CLKIN and DAC_MOD_CLK as BDIV_CLKIN */
543         snd_soc_write(codec, AIC32X4_CLKMUX, AIC32X4_PLLCLKIN);
544         snd_soc_write(codec, AIC32X4_IFACE3, AIC32X4_DACMOD2BCLK);
545
546         /* We will fix R value to 1 and will make P & J=K.D as varialble */
547         data = snd_soc_read(codec, AIC32X4_PLLPR);
548         data &= ~(7 << 4);
549         snd_soc_write(codec, AIC32X4_PLLPR,
550                       (data | (aic32x4_divs[i].p_val << 4) | 0x01));
551
552         snd_soc_write(codec, AIC32X4_PLLJ, aic32x4_divs[i].pll_j);
553
554         snd_soc_write(codec, AIC32X4_PLLDMSB, (aic32x4_divs[i].pll_d >> 8));
555         snd_soc_write(codec, AIC32X4_PLLDLSB,
556                       (aic32x4_divs[i].pll_d & 0xff));
557
558         /* NDAC divider value */
559         data = snd_soc_read(codec, AIC32X4_NDAC);
560         data &= ~(0x7f);
561         snd_soc_write(codec, AIC32X4_NDAC, data | aic32x4_divs[i].ndac);
562
563         /* MDAC divider value */
564         data = snd_soc_read(codec, AIC32X4_MDAC);
565         data &= ~(0x7f);
566         snd_soc_write(codec, AIC32X4_MDAC, data | aic32x4_divs[i].mdac);
567
568         /* DOSR MSB & LSB values */
569         snd_soc_write(codec, AIC32X4_DOSRMSB, aic32x4_divs[i].dosr >> 8);
570         snd_soc_write(codec, AIC32X4_DOSRLSB,
571                       (aic32x4_divs[i].dosr & 0xff));
572
573         /* NADC divider value */
574         data = snd_soc_read(codec, AIC32X4_NADC);
575         data &= ~(0x7f);
576         snd_soc_write(codec, AIC32X4_NADC, data | aic32x4_divs[i].nadc);
577
578         /* MADC divider value */
579         data = snd_soc_read(codec, AIC32X4_MADC);
580         data &= ~(0x7f);
581         snd_soc_write(codec, AIC32X4_MADC, data | aic32x4_divs[i].madc);
582
583         /* AOSR value */
584         snd_soc_write(codec, AIC32X4_AOSR, aic32x4_divs[i].aosr);
585
586         /* BCLK N divider */
587         data = snd_soc_read(codec, AIC32X4_BCLKN);
588         data &= ~(0x7f);
589         snd_soc_write(codec, AIC32X4_BCLKN, data | aic32x4_divs[i].blck_N);
590
591         data = snd_soc_read(codec, AIC32X4_IFACE1);
592         data = data & ~(3 << 4);
593         switch (params_width(params)) {
594         case 16:
595                 break;
596         case 20:
597                 data |= (AIC32X4_WORD_LEN_20BITS << AIC32X4_DOSRMSB_SHIFT);
598                 break;
599         case 24:
600                 data |= (AIC32X4_WORD_LEN_24BITS << AIC32X4_DOSRMSB_SHIFT);
601                 break;
602         case 32:
603                 data |= (AIC32X4_WORD_LEN_32BITS << AIC32X4_DOSRMSB_SHIFT);
604                 break;
605         }
606         snd_soc_write(codec, AIC32X4_IFACE1, data);
607
608         if (params_channels(params) == 1) {
609                 data = AIC32X4_RDAC2LCHN | AIC32X4_LDAC2LCHN;
610         } else {
611                 if (aic32x4->swapdacs)
612                         data = AIC32X4_RDAC2LCHN | AIC32X4_LDAC2RCHN;
613                 else
614                         data = AIC32X4_LDAC2LCHN | AIC32X4_RDAC2RCHN;
615         }
616         snd_soc_update_bits(codec, AIC32X4_DACSETUP, AIC32X4_DAC_CHAN_MASK,
617                         data);
618
619         return 0;
620 }
621
622 static int aic32x4_mute(struct snd_soc_dai *dai, int mute)
623 {
624         struct snd_soc_codec *codec = dai->codec;
625         u8 dac_reg;
626
627         dac_reg = snd_soc_read(codec, AIC32X4_DACMUTE) & ~AIC32X4_MUTEON;
628         if (mute)
629                 snd_soc_write(codec, AIC32X4_DACMUTE, dac_reg | AIC32X4_MUTEON);
630         else
631                 snd_soc_write(codec, AIC32X4_DACMUTE, dac_reg);
632         return 0;
633 }
634
635 static int aic32x4_set_bias_level(struct snd_soc_codec *codec,
636                                   enum snd_soc_bias_level level)
637 {
638         struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
639         int ret;
640
641         switch (level) {
642         case SND_SOC_BIAS_ON:
643                 /* Switch on master clock */
644                 ret = clk_prepare_enable(aic32x4->mclk);
645                 if (ret) {
646                         dev_err(codec->dev, "Failed to enable master clock\n");
647                         return ret;
648                 }
649
650                 /* Switch on PLL */
651                 snd_soc_update_bits(codec, AIC32X4_PLLPR,
652                                     AIC32X4_PLLEN, AIC32X4_PLLEN);
653
654                 /* Switch on NDAC Divider */
655                 snd_soc_update_bits(codec, AIC32X4_NDAC,
656                                     AIC32X4_NDACEN, AIC32X4_NDACEN);
657
658                 /* Switch on MDAC Divider */
659                 snd_soc_update_bits(codec, AIC32X4_MDAC,
660                                     AIC32X4_MDACEN, AIC32X4_MDACEN);
661
662                 /* Switch on NADC Divider */
663                 snd_soc_update_bits(codec, AIC32X4_NADC,
664                                     AIC32X4_NADCEN, AIC32X4_NADCEN);
665
666                 /* Switch on MADC Divider */
667                 snd_soc_update_bits(codec, AIC32X4_MADC,
668                                     AIC32X4_MADCEN, AIC32X4_MADCEN);
669
670                 /* Switch on BCLK_N Divider */
671                 snd_soc_update_bits(codec, AIC32X4_BCLKN,
672                                     AIC32X4_BCLKEN, AIC32X4_BCLKEN);
673                 break;
674         case SND_SOC_BIAS_PREPARE:
675                 break;
676         case SND_SOC_BIAS_STANDBY:
677                 /* Switch off BCLK_N Divider */
678                 snd_soc_update_bits(codec, AIC32X4_BCLKN,
679                                     AIC32X4_BCLKEN, 0);
680
681                 /* Switch off MADC Divider */
682                 snd_soc_update_bits(codec, AIC32X4_MADC,
683                                     AIC32X4_MADCEN, 0);
684
685                 /* Switch off NADC Divider */
686                 snd_soc_update_bits(codec, AIC32X4_NADC,
687                                     AIC32X4_NADCEN, 0);
688
689                 /* Switch off MDAC Divider */
690                 snd_soc_update_bits(codec, AIC32X4_MDAC,
691                                     AIC32X4_MDACEN, 0);
692
693                 /* Switch off NDAC Divider */
694                 snd_soc_update_bits(codec, AIC32X4_NDAC,
695                                     AIC32X4_NDACEN, 0);
696
697                 /* Switch off PLL */
698                 snd_soc_update_bits(codec, AIC32X4_PLLPR,
699                                     AIC32X4_PLLEN, 0);
700
701                 /* Switch off master clock */
702                 clk_disable_unprepare(aic32x4->mclk);
703                 break;
704         case SND_SOC_BIAS_OFF:
705                 break;
706         }
707         return 0;
708 }
709
710 #define AIC32X4_RATES   SNDRV_PCM_RATE_8000_96000
711 #define AIC32X4_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE \
712                          | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
713
714 static const struct snd_soc_dai_ops aic32x4_ops = {
715         .hw_params = aic32x4_hw_params,
716         .digital_mute = aic32x4_mute,
717         .set_fmt = aic32x4_set_dai_fmt,
718         .set_sysclk = aic32x4_set_dai_sysclk,
719 };
720
721 static struct snd_soc_dai_driver aic32x4_dai = {
722         .name = "tlv320aic32x4-hifi",
723         .playback = {
724                      .stream_name = "Playback",
725                      .channels_min = 1,
726                      .channels_max = 2,
727                      .rates = AIC32X4_RATES,
728                      .formats = AIC32X4_FORMATS,},
729         .capture = {
730                     .stream_name = "Capture",
731                     .channels_min = 1,
732                     .channels_max = 2,
733                     .rates = AIC32X4_RATES,
734                     .formats = AIC32X4_FORMATS,},
735         .ops = &aic32x4_ops,
736         .symmetric_rates = 1,
737 };
738
739 static int aic32x4_codec_probe(struct snd_soc_codec *codec)
740 {
741         struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
742         u32 tmp_reg;
743
744         if (gpio_is_valid(aic32x4->rstn_gpio)) {
745                 ndelay(10);
746                 gpio_set_value(aic32x4->rstn_gpio, 1);
747         }
748
749         snd_soc_write(codec, AIC32X4_RESET, 0x01);
750
751         /* Power platform configuration */
752         if (aic32x4->power_cfg & AIC32X4_PWR_MICBIAS_2075_LDOIN) {
753                 snd_soc_write(codec, AIC32X4_MICBIAS, AIC32X4_MICBIAS_LDOIN |
754                                                       AIC32X4_MICBIAS_2075V);
755         }
756         if (aic32x4->power_cfg & AIC32X4_PWR_AVDD_DVDD_WEAK_DISABLE)
757                 snd_soc_write(codec, AIC32X4_PWRCFG, AIC32X4_AVDDWEAKDISABLE);
758
759         tmp_reg = (aic32x4->power_cfg & AIC32X4_PWR_AIC32X4_LDO_ENABLE) ?
760                         AIC32X4_LDOCTLEN : 0;
761         snd_soc_write(codec, AIC32X4_LDOCTL, tmp_reg);
762
763         tmp_reg = snd_soc_read(codec, AIC32X4_CMMODE);
764         if (aic32x4->power_cfg & AIC32X4_PWR_CMMODE_LDOIN_RANGE_18_36)
765                 tmp_reg |= AIC32X4_LDOIN_18_36;
766         if (aic32x4->power_cfg & AIC32X4_PWR_CMMODE_HP_LDOIN_POWERED)
767                 tmp_reg |= AIC32X4_LDOIN2HP;
768         snd_soc_write(codec, AIC32X4_CMMODE, tmp_reg);
769
770         /* Mic PGA routing */
771         if (aic32x4->micpga_routing & AIC32X4_MICPGA_ROUTE_LMIC_IN2R_10K)
772                 snd_soc_write(codec, AIC32X4_LMICPGANIN,
773                                 AIC32X4_LMICPGANIN_IN2R_10K);
774         else
775                 snd_soc_write(codec, AIC32X4_LMICPGANIN,
776                                 AIC32X4_LMICPGANIN_CM1L_10K);
777         if (aic32x4->micpga_routing & AIC32X4_MICPGA_ROUTE_RMIC_IN1L_10K)
778                 snd_soc_write(codec, AIC32X4_RMICPGANIN,
779                                 AIC32X4_RMICPGANIN_IN1L_10K);
780         else
781                 snd_soc_write(codec, AIC32X4_RMICPGANIN,
782                                 AIC32X4_RMICPGANIN_CM1R_10K);
783
784         /*
785          * Workaround: for an unknown reason, the ADC needs to be powered up
786          * and down for the first capture to work properly. It seems related to
787          * a HW BUG or some kind of behavior not documented in the datasheet.
788          */
789         tmp_reg = snd_soc_read(codec, AIC32X4_ADCSETUP);
790         snd_soc_write(codec, AIC32X4_ADCSETUP, tmp_reg |
791                                 AIC32X4_LADC_EN | AIC32X4_RADC_EN);
792         snd_soc_write(codec, AIC32X4_ADCSETUP, tmp_reg);
793
794         return 0;
795 }
796
797 static struct snd_soc_codec_driver soc_codec_dev_aic32x4 = {
798         .probe = aic32x4_codec_probe,
799         .set_bias_level = aic32x4_set_bias_level,
800         .suspend_bias_off = true,
801
802         .component_driver = {
803                 .controls               = aic32x4_snd_controls,
804                 .num_controls           = ARRAY_SIZE(aic32x4_snd_controls),
805                 .dapm_widgets           = aic32x4_dapm_widgets,
806                 .num_dapm_widgets       = ARRAY_SIZE(aic32x4_dapm_widgets),
807                 .dapm_routes            = aic32x4_dapm_routes,
808                 .num_dapm_routes        = ARRAY_SIZE(aic32x4_dapm_routes),
809         },
810 };
811
812 static int aic32x4_parse_dt(struct aic32x4_priv *aic32x4,
813                 struct device_node *np)
814 {
815         aic32x4->swapdacs = false;
816         aic32x4->micpga_routing = 0;
817         aic32x4->rstn_gpio = of_get_named_gpio(np, "reset-gpios", 0);
818
819         return 0;
820 }
821
822 static void aic32x4_disable_regulators(struct aic32x4_priv *aic32x4)
823 {
824         regulator_disable(aic32x4->supply_iov);
825
826         if (!IS_ERR(aic32x4->supply_ldo))
827                 regulator_disable(aic32x4->supply_ldo);
828
829         if (!IS_ERR(aic32x4->supply_dv))
830                 regulator_disable(aic32x4->supply_dv);
831
832         if (!IS_ERR(aic32x4->supply_av))
833                 regulator_disable(aic32x4->supply_av);
834 }
835
836 static int aic32x4_setup_regulators(struct device *dev,
837                 struct aic32x4_priv *aic32x4)
838 {
839         int ret = 0;
840
841         aic32x4->supply_ldo = devm_regulator_get_optional(dev, "ldoin");
842         aic32x4->supply_iov = devm_regulator_get(dev, "iov");
843         aic32x4->supply_dv = devm_regulator_get_optional(dev, "dv");
844         aic32x4->supply_av = devm_regulator_get_optional(dev, "av");
845
846         /* Check if the regulator requirements are fulfilled */
847
848         if (IS_ERR(aic32x4->supply_iov)) {
849                 dev_err(dev, "Missing supply 'iov'\n");
850                 return PTR_ERR(aic32x4->supply_iov);
851         }
852
853         if (IS_ERR(aic32x4->supply_ldo)) {
854                 if (PTR_ERR(aic32x4->supply_ldo) == -EPROBE_DEFER)
855                         return -EPROBE_DEFER;
856
857                 if (IS_ERR(aic32x4->supply_dv)) {
858                         dev_err(dev, "Missing supply 'dv' or 'ldoin'\n");
859                         return PTR_ERR(aic32x4->supply_dv);
860                 }
861                 if (IS_ERR(aic32x4->supply_av)) {
862                         dev_err(dev, "Missing supply 'av' or 'ldoin'\n");
863                         return PTR_ERR(aic32x4->supply_av);
864                 }
865         } else {
866                 if (IS_ERR(aic32x4->supply_dv) &&
867                                 PTR_ERR(aic32x4->supply_dv) == -EPROBE_DEFER)
868                         return -EPROBE_DEFER;
869                 if (IS_ERR(aic32x4->supply_av) &&
870                                 PTR_ERR(aic32x4->supply_av) == -EPROBE_DEFER)
871                         return -EPROBE_DEFER;
872         }
873
874         ret = regulator_enable(aic32x4->supply_iov);
875         if (ret) {
876                 dev_err(dev, "Failed to enable regulator iov\n");
877                 return ret;
878         }
879
880         if (!IS_ERR(aic32x4->supply_ldo)) {
881                 ret = regulator_enable(aic32x4->supply_ldo);
882                 if (ret) {
883                         dev_err(dev, "Failed to enable regulator ldo\n");
884                         goto error_ldo;
885                 }
886         }
887
888         if (!IS_ERR(aic32x4->supply_dv)) {
889                 ret = regulator_enable(aic32x4->supply_dv);
890                 if (ret) {
891                         dev_err(dev, "Failed to enable regulator dv\n");
892                         goto error_dv;
893                 }
894         }
895
896         if (!IS_ERR(aic32x4->supply_av)) {
897                 ret = regulator_enable(aic32x4->supply_av);
898                 if (ret) {
899                         dev_err(dev, "Failed to enable regulator av\n");
900                         goto error_av;
901                 }
902         }
903
904         if (!IS_ERR(aic32x4->supply_ldo) && IS_ERR(aic32x4->supply_av))
905                 aic32x4->power_cfg |= AIC32X4_PWR_AIC32X4_LDO_ENABLE;
906
907         return 0;
908
909 error_av:
910         if (!IS_ERR(aic32x4->supply_dv))
911                 regulator_disable(aic32x4->supply_dv);
912
913 error_dv:
914         if (!IS_ERR(aic32x4->supply_ldo))
915                 regulator_disable(aic32x4->supply_ldo);
916
917 error_ldo:
918         regulator_disable(aic32x4->supply_iov);
919         return ret;
920 }
921
922 int aic32x4_probe(struct device *dev, struct regmap *regmap)
923 {
924         struct aic32x4_priv *aic32x4;
925         struct aic32x4_pdata *pdata = dev->platform_data;
926         struct device_node *np = dev->of_node;
927         int ret;
928
929         if (IS_ERR(regmap))
930                 return PTR_ERR(regmap);
931
932         aic32x4 = devm_kzalloc(dev, sizeof(struct aic32x4_priv),
933                                GFP_KERNEL);
934         if (aic32x4 == NULL)
935                 return -ENOMEM;
936
937         dev_set_drvdata(dev, aic32x4);
938
939         if (pdata) {
940                 aic32x4->power_cfg = pdata->power_cfg;
941                 aic32x4->swapdacs = pdata->swapdacs;
942                 aic32x4->micpga_routing = pdata->micpga_routing;
943                 aic32x4->rstn_gpio = pdata->rstn_gpio;
944         } else if (np) {
945                 ret = aic32x4_parse_dt(aic32x4, np);
946                 if (ret) {
947                         dev_err(dev, "Failed to parse DT node\n");
948                         return ret;
949                 }
950         } else {
951                 aic32x4->power_cfg = 0;
952                 aic32x4->swapdacs = false;
953                 aic32x4->micpga_routing = 0;
954                 aic32x4->rstn_gpio = -1;
955         }
956
957         aic32x4->mclk = devm_clk_get(dev, "mclk");
958         if (IS_ERR(aic32x4->mclk)) {
959                 dev_err(dev, "Failed getting the mclk. The current implementation does not support the usage of this codec without mclk\n");
960                 return PTR_ERR(aic32x4->mclk);
961         }
962
963         if (gpio_is_valid(aic32x4->rstn_gpio)) {
964                 ret = devm_gpio_request_one(dev, aic32x4->rstn_gpio,
965                                 GPIOF_OUT_INIT_LOW, "tlv320aic32x4 rstn");
966                 if (ret != 0)
967                         return ret;
968         }
969
970         ret = aic32x4_setup_regulators(dev, aic32x4);
971         if (ret) {
972                 dev_err(dev, "Failed to setup regulators\n");
973                 return ret;
974         }
975
976         ret = snd_soc_register_codec(dev,
977                         &soc_codec_dev_aic32x4, &aic32x4_dai, 1);
978         if (ret) {
979                 dev_err(dev, "Failed to register codec\n");
980                 aic32x4_disable_regulators(aic32x4);
981                 return ret;
982         }
983
984         return 0;
985 }
986 EXPORT_SYMBOL(aic32x4_probe);
987
988 int aic32x4_remove(struct device *dev)
989 {
990         struct aic32x4_priv *aic32x4 = dev_get_drvdata(dev);
991
992         aic32x4_disable_regulators(aic32x4);
993
994         snd_soc_unregister_codec(dev);
995
996         return 0;
997 }
998 EXPORT_SYMBOL(aic32x4_remove);
999
1000 MODULE_DESCRIPTION("ASoC tlv320aic32x4 codec driver");
1001 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
1002 MODULE_LICENSE("GPL");