2 * This driver supports the analog controls for the internal codec
3 * found in Allwinner's A31s, A23, A33 and H3 SoCs.
5 * Copyright 2016 Chen-Yu Tsai <wens@csie.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 #include <linux/kernel.h>
20 #include <linux/module.h>
22 #include <linux/of_device.h>
23 #include <linux/platform_device.h>
24 #include <linux/regmap.h>
26 #include <sound/soc.h>
27 #include <sound/soc-dapm.h>
28 #include <sound/tlv.h>
30 #include "sun8i-adda-pr-regmap.h"
32 /* Codec analog control register offsets and bit fields */
33 #define SUN8I_ADDA_HP_VOLC 0x00
34 #define SUN8I_ADDA_HP_VOLC_PA_CLK_GATE 7
35 #define SUN8I_ADDA_HP_VOLC_HP_VOL 0
36 #define SUN8I_ADDA_LOMIXSC 0x01
37 #define SUN8I_ADDA_LOMIXSC_MIC1 6
38 #define SUN8I_ADDA_LOMIXSC_MIC2 5
39 #define SUN8I_ADDA_LOMIXSC_PHONE 4
40 #define SUN8I_ADDA_LOMIXSC_PHONEN 3
41 #define SUN8I_ADDA_LOMIXSC_LINEINL 2
42 #define SUN8I_ADDA_LOMIXSC_DACL 1
43 #define SUN8I_ADDA_LOMIXSC_DACR 0
44 #define SUN8I_ADDA_ROMIXSC 0x02
45 #define SUN8I_ADDA_ROMIXSC_MIC1 6
46 #define SUN8I_ADDA_ROMIXSC_MIC2 5
47 #define SUN8I_ADDA_ROMIXSC_PHONE 4
48 #define SUN8I_ADDA_ROMIXSC_PHONEP 3
49 #define SUN8I_ADDA_ROMIXSC_LINEINR 2
50 #define SUN8I_ADDA_ROMIXSC_DACR 1
51 #define SUN8I_ADDA_ROMIXSC_DACL 0
52 #define SUN8I_ADDA_DAC_PA_SRC 0x03
53 #define SUN8I_ADDA_DAC_PA_SRC_DACAREN 7
54 #define SUN8I_ADDA_DAC_PA_SRC_DACALEN 6
55 #define SUN8I_ADDA_DAC_PA_SRC_RMIXEN 5
56 #define SUN8I_ADDA_DAC_PA_SRC_LMIXEN 4
57 #define SUN8I_ADDA_DAC_PA_SRC_RHPPAMUTE 3
58 #define SUN8I_ADDA_DAC_PA_SRC_LHPPAMUTE 2
59 #define SUN8I_ADDA_DAC_PA_SRC_RHPIS 1
60 #define SUN8I_ADDA_DAC_PA_SRC_LHPIS 0
61 #define SUN8I_ADDA_PHONEIN_GCTRL 0x04
62 #define SUN8I_ADDA_PHONEIN_GCTRL_PHONEPG 4
63 #define SUN8I_ADDA_PHONEIN_GCTRL_PHONENG 0
64 #define SUN8I_ADDA_LINEIN_GCTRL 0x05
65 #define SUN8I_ADDA_LINEIN_GCTRL_LINEING 4
66 #define SUN8I_ADDA_LINEIN_GCTRL_PHONEG 0
67 #define SUN8I_ADDA_MICIN_GCTRL 0x06
68 #define SUN8I_ADDA_MICIN_GCTRL_MIC1G 4
69 #define SUN8I_ADDA_MICIN_GCTRL_MIC2G 0
70 #define SUN8I_ADDA_PAEN_HP_CTRL 0x07
71 #define SUN8I_ADDA_PAEN_HP_CTRL_HPPAEN 7
72 #define SUN8I_ADDA_PAEN_HP_CTRL_LINEOUTEN 7 /* H3 specific */
73 #define SUN8I_ADDA_PAEN_HP_CTRL_HPCOM_FC 5
74 #define SUN8I_ADDA_PAEN_HP_CTRL_COMPTEN 4
75 #define SUN8I_ADDA_PAEN_HP_CTRL_PA_ANTI_POP_CTRL 2
76 #define SUN8I_ADDA_PAEN_HP_CTRL_LTRNMUTE 1
77 #define SUN8I_ADDA_PAEN_HP_CTRL_RTLNMUTE 0
78 #define SUN8I_ADDA_PHONEOUT_CTRL 0x08
79 #define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUTG 5
80 #define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUTEN 4
81 #define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUT_MIC1 3
82 #define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUT_MIC2 2
83 #define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUT_RMIX 1
84 #define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUT_LMIX 0
85 #define SUN8I_ADDA_PHONE_GAIN_CTRL 0x09
86 #define SUN8I_ADDA_PHONE_GAIN_CTRL_LINEOUT_VOL 3
87 #define SUN8I_ADDA_PHONE_GAIN_CTRL_PHONEPREG 0
88 #define SUN8I_ADDA_MIC2G_CTRL 0x0a
89 #define SUN8I_ADDA_MIC2G_CTRL_MIC2AMPEN 7
90 #define SUN8I_ADDA_MIC2G_CTRL_MIC2BOOST 4
91 #define SUN8I_ADDA_MIC2G_CTRL_LINEOUTLEN 3
92 #define SUN8I_ADDA_MIC2G_CTRL_LINEOUTREN 2
93 #define SUN8I_ADDA_MIC2G_CTRL_LINEOUTLSRC 1
94 #define SUN8I_ADDA_MIC2G_CTRL_LINEOUTRSRC 0
95 #define SUN8I_ADDA_MIC1G_MICBIAS_CTRL 0x0b
96 #define SUN8I_ADDA_MIC1G_MICBIAS_CTRL_HMICBIASEN 7
97 #define SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MMICBIASEN 6
98 #define SUN8I_ADDA_MIC1G_MICBIAS_CTRL_HMICBIAS_MODE 5
99 #define SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MIC1AMPEN 3
100 #define SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MIC1BOOST 0
101 #define SUN8I_ADDA_LADCMIXSC 0x0c
102 #define SUN8I_ADDA_LADCMIXSC_MIC1 6
103 #define SUN8I_ADDA_LADCMIXSC_MIC2 5
104 #define SUN8I_ADDA_LADCMIXSC_PHONE 4
105 #define SUN8I_ADDA_LADCMIXSC_PHONEN 3
106 #define SUN8I_ADDA_LADCMIXSC_LINEINL 2
107 #define SUN8I_ADDA_LADCMIXSC_OMIXRL 1
108 #define SUN8I_ADDA_LADCMIXSC_OMIXRR 0
109 #define SUN8I_ADDA_RADCMIXSC 0x0d
110 #define SUN8I_ADDA_RADCMIXSC_MIC1 6
111 #define SUN8I_ADDA_RADCMIXSC_MIC2 5
112 #define SUN8I_ADDA_RADCMIXSC_PHONE 4
113 #define SUN8I_ADDA_RADCMIXSC_PHONEP 3
114 #define SUN8I_ADDA_RADCMIXSC_LINEINR 2
115 #define SUN8I_ADDA_RADCMIXSC_OMIXR 1
116 #define SUN8I_ADDA_RADCMIXSC_OMIXL 0
117 #define SUN8I_ADDA_RES 0x0e
118 #define SUN8I_ADDA_RES_MMICBIAS_SEL 4
119 #define SUN8I_ADDA_RES_PA_ANTI_POP_CTRL 0
120 #define SUN8I_ADDA_ADC_AP_EN 0x0f
121 #define SUN8I_ADDA_ADC_AP_EN_ADCREN 7
122 #define SUN8I_ADDA_ADC_AP_EN_ADCLEN 6
123 #define SUN8I_ADDA_ADC_AP_EN_ADCG 0
126 static const struct snd_kcontrol_new sun8i_codec_mixer_controls[] = {
127 SOC_DAPM_DOUBLE_R("DAC Playback Switch",
130 SUN8I_ADDA_LOMIXSC_DACL, 1, 0),
131 SOC_DAPM_DOUBLE_R("DAC Reversed Playback Switch",
134 SUN8I_ADDA_LOMIXSC_DACR, 1, 0),
135 SOC_DAPM_DOUBLE_R("Line In Playback Switch",
138 SUN8I_ADDA_LOMIXSC_LINEINL, 1, 0),
139 SOC_DAPM_DOUBLE_R("Mic1 Playback Switch",
142 SUN8I_ADDA_LOMIXSC_MIC1, 1, 0),
143 SOC_DAPM_DOUBLE_R("Mic2 Playback Switch",
146 SUN8I_ADDA_LOMIXSC_MIC2, 1, 0),
150 static const struct snd_kcontrol_new sun8i_v3s_codec_mixer_controls[] = {
151 SOC_DAPM_DOUBLE_R("DAC Playback Switch",
154 SUN8I_ADDA_LOMIXSC_DACL, 1, 0),
155 SOC_DAPM_DOUBLE_R("DAC Reversed Playback Switch",
158 SUN8I_ADDA_LOMIXSC_DACR, 1, 0),
159 SOC_DAPM_DOUBLE_R("Mic1 Playback Switch",
162 SUN8I_ADDA_LOMIXSC_MIC1, 1, 0),
165 /* ADC mixer controls */
166 static const struct snd_kcontrol_new sun8i_codec_adc_mixer_controls[] = {
167 SOC_DAPM_DOUBLE_R("Mixer Capture Switch",
168 SUN8I_ADDA_LADCMIXSC,
169 SUN8I_ADDA_RADCMIXSC,
170 SUN8I_ADDA_LADCMIXSC_OMIXRL, 1, 0),
171 SOC_DAPM_DOUBLE_R("Mixer Reversed Capture Switch",
172 SUN8I_ADDA_LADCMIXSC,
173 SUN8I_ADDA_RADCMIXSC,
174 SUN8I_ADDA_LADCMIXSC_OMIXRR, 1, 0),
175 SOC_DAPM_DOUBLE_R("Line In Capture Switch",
176 SUN8I_ADDA_LADCMIXSC,
177 SUN8I_ADDA_RADCMIXSC,
178 SUN8I_ADDA_LADCMIXSC_LINEINL, 1, 0),
179 SOC_DAPM_DOUBLE_R("Mic1 Capture Switch",
180 SUN8I_ADDA_LADCMIXSC,
181 SUN8I_ADDA_RADCMIXSC,
182 SUN8I_ADDA_LADCMIXSC_MIC1, 1, 0),
183 SOC_DAPM_DOUBLE_R("Mic2 Capture Switch",
184 SUN8I_ADDA_LADCMIXSC,
185 SUN8I_ADDA_RADCMIXSC,
186 SUN8I_ADDA_LADCMIXSC_MIC2, 1, 0),
189 /* ADC mixer controls */
190 static const struct snd_kcontrol_new sun8i_v3s_codec_adc_mixer_controls[] = {
191 SOC_DAPM_DOUBLE_R("Mixer Capture Switch",
192 SUN8I_ADDA_LADCMIXSC,
193 SUN8I_ADDA_RADCMIXSC,
194 SUN8I_ADDA_LADCMIXSC_OMIXRL, 1, 0),
195 SOC_DAPM_DOUBLE_R("Mixer Reversed Capture Switch",
196 SUN8I_ADDA_LADCMIXSC,
197 SUN8I_ADDA_RADCMIXSC,
198 SUN8I_ADDA_LADCMIXSC_OMIXRR, 1, 0),
199 SOC_DAPM_DOUBLE_R("Mic1 Capture Switch",
200 SUN8I_ADDA_LADCMIXSC,
201 SUN8I_ADDA_RADCMIXSC,
202 SUN8I_ADDA_LADCMIXSC_MIC1, 1, 0),
205 /* volume / mute controls */
206 static const DECLARE_TLV_DB_SCALE(sun8i_codec_out_mixer_pregain_scale,
208 static const DECLARE_TLV_DB_RANGE(sun8i_codec_mic_gain_scale,
209 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
210 1, 7, TLV_DB_SCALE_ITEM(2400, 300, 0),
213 static const struct snd_kcontrol_new sun8i_codec_common_controls[] = {
215 SOC_SINGLE_TLV("Mic1 Playback Volume", SUN8I_ADDA_MICIN_GCTRL,
216 SUN8I_ADDA_MICIN_GCTRL_MIC1G,
217 0x7, 0, sun8i_codec_out_mixer_pregain_scale),
219 /* Microphone Amp boost gain */
220 SOC_SINGLE_TLV("Mic1 Boost Volume", SUN8I_ADDA_MIC1G_MICBIAS_CTRL,
221 SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MIC1BOOST, 0x7, 0,
222 sun8i_codec_mic_gain_scale),
225 SOC_SINGLE_TLV("ADC Gain Capture Volume", SUN8I_ADDA_ADC_AP_EN,
226 SUN8I_ADDA_ADC_AP_EN_ADCG, 0x7, 0,
227 sun8i_codec_out_mixer_pregain_scale),
230 static const struct snd_soc_dapm_widget sun8i_codec_common_widgets[] = {
232 SND_SOC_DAPM_ADC("Left ADC", NULL, SUN8I_ADDA_ADC_AP_EN,
233 SUN8I_ADDA_ADC_AP_EN_ADCLEN, 0),
234 SND_SOC_DAPM_ADC("Right ADC", NULL, SUN8I_ADDA_ADC_AP_EN,
235 SUN8I_ADDA_ADC_AP_EN_ADCREN, 0),
238 SND_SOC_DAPM_DAC("Left DAC", NULL, SUN8I_ADDA_DAC_PA_SRC,
239 SUN8I_ADDA_DAC_PA_SRC_DACALEN, 0),
240 SND_SOC_DAPM_DAC("Right DAC", NULL, SUN8I_ADDA_DAC_PA_SRC,
241 SUN8I_ADDA_DAC_PA_SRC_DACAREN, 0),
243 * Due to this component and the codec belonging to separate DAPM
244 * contexts, we need to manually link the above widgets to their
245 * stream widgets at the card level.
248 /* Microphone input */
249 SND_SOC_DAPM_INPUT("MIC1"),
252 SND_SOC_DAPM_PGA("Mic1 Amplifier", SUN8I_ADDA_MIC1G_MICBIAS_CTRL,
253 SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MIC1AMPEN, 0, NULL, 0),
256 static const struct snd_soc_dapm_widget sun8i_codec_mixer_widgets[] = {
257 SND_SOC_DAPM_MIXER("Left Mixer", SUN8I_ADDA_DAC_PA_SRC,
258 SUN8I_ADDA_DAC_PA_SRC_LMIXEN, 0,
259 sun8i_codec_mixer_controls,
260 ARRAY_SIZE(sun8i_codec_mixer_controls)),
261 SND_SOC_DAPM_MIXER("Right Mixer", SUN8I_ADDA_DAC_PA_SRC,
262 SUN8I_ADDA_DAC_PA_SRC_RMIXEN, 0,
263 sun8i_codec_mixer_controls,
264 ARRAY_SIZE(sun8i_codec_mixer_controls)),
265 SND_SOC_DAPM_MIXER("Left ADC Mixer", SUN8I_ADDA_ADC_AP_EN,
266 SUN8I_ADDA_ADC_AP_EN_ADCLEN, 0,
267 sun8i_codec_adc_mixer_controls,
268 ARRAY_SIZE(sun8i_codec_adc_mixer_controls)),
269 SND_SOC_DAPM_MIXER("Right ADC Mixer", SUN8I_ADDA_ADC_AP_EN,
270 SUN8I_ADDA_ADC_AP_EN_ADCREN, 0,
271 sun8i_codec_adc_mixer_controls,
272 ARRAY_SIZE(sun8i_codec_adc_mixer_controls)),
275 static const struct snd_soc_dapm_widget sun8i_v3s_codec_mixer_widgets[] = {
276 SND_SOC_DAPM_MIXER("Left Mixer", SUN8I_ADDA_DAC_PA_SRC,
277 SUN8I_ADDA_DAC_PA_SRC_LMIXEN, 0,
278 sun8i_v3s_codec_mixer_controls,
279 ARRAY_SIZE(sun8i_v3s_codec_mixer_controls)),
280 SND_SOC_DAPM_MIXER("Right Mixer", SUN8I_ADDA_DAC_PA_SRC,
281 SUN8I_ADDA_DAC_PA_SRC_RMIXEN, 0,
282 sun8i_v3s_codec_mixer_controls,
283 ARRAY_SIZE(sun8i_v3s_codec_mixer_controls)),
284 SND_SOC_DAPM_MIXER("Left ADC Mixer", SUN8I_ADDA_ADC_AP_EN,
285 SUN8I_ADDA_ADC_AP_EN_ADCLEN, 0,
286 sun8i_v3s_codec_adc_mixer_controls,
287 ARRAY_SIZE(sun8i_v3s_codec_adc_mixer_controls)),
288 SND_SOC_DAPM_MIXER("Right ADC Mixer", SUN8I_ADDA_ADC_AP_EN,
289 SUN8I_ADDA_ADC_AP_EN_ADCREN, 0,
290 sun8i_v3s_codec_adc_mixer_controls,
291 ARRAY_SIZE(sun8i_v3s_codec_adc_mixer_controls)),
294 static const struct snd_soc_dapm_route sun8i_codec_common_routes[] = {
295 /* Microphone Routes */
296 { "Mic1 Amplifier", NULL, "MIC1"},
299 static const struct snd_soc_dapm_route sun8i_codec_mixer_routes[] = {
300 /* Left Mixer Routes */
301 { "Left Mixer", "DAC Playback Switch", "Left DAC" },
302 { "Left Mixer", "DAC Reversed Playback Switch", "Right DAC" },
303 { "Left Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
305 /* Right Mixer Routes */
306 { "Right Mixer", "DAC Playback Switch", "Right DAC" },
307 { "Right Mixer", "DAC Reversed Playback Switch", "Left DAC" },
308 { "Right Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
310 /* Left ADC Mixer Routes */
311 { "Left ADC Mixer", "Mixer Capture Switch", "Left Mixer" },
312 { "Left ADC Mixer", "Mixer Reversed Capture Switch", "Right Mixer" },
313 { "Left ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
315 /* Right ADC Mixer Routes */
316 { "Right ADC Mixer", "Mixer Capture Switch", "Right Mixer" },
317 { "Right ADC Mixer", "Mixer Reversed Capture Switch", "Left Mixer" },
318 { "Right ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
321 { "Left ADC", NULL, "Left ADC Mixer" },
322 { "Right ADC", NULL, "Right ADC Mixer" },
325 /* headphone specific controls, widgets, and routes */
326 static const DECLARE_TLV_DB_SCALE(sun8i_codec_hp_vol_scale, -6300, 100, 1);
327 static const struct snd_kcontrol_new sun8i_codec_headphone_controls[] = {
328 SOC_SINGLE_TLV("Headphone Playback Volume",
330 SUN8I_ADDA_HP_VOLC_HP_VOL, 0x3f, 0,
331 sun8i_codec_hp_vol_scale),
332 SOC_DOUBLE("Headphone Playback Switch",
333 SUN8I_ADDA_DAC_PA_SRC,
334 SUN8I_ADDA_DAC_PA_SRC_LHPPAMUTE,
335 SUN8I_ADDA_DAC_PA_SRC_RHPPAMUTE, 1, 0),
338 static const char * const sun8i_codec_hp_src_enum_text[] = {
342 static SOC_ENUM_DOUBLE_DECL(sun8i_codec_hp_src_enum,
343 SUN8I_ADDA_DAC_PA_SRC,
344 SUN8I_ADDA_DAC_PA_SRC_LHPIS,
345 SUN8I_ADDA_DAC_PA_SRC_RHPIS,
346 sun8i_codec_hp_src_enum_text);
348 static const struct snd_kcontrol_new sun8i_codec_hp_src[] = {
349 SOC_DAPM_ENUM("Headphone Source Playback Route",
350 sun8i_codec_hp_src_enum),
353 static int sun8i_headphone_amp_event(struct snd_soc_dapm_widget *w,
354 struct snd_kcontrol *k, int event)
356 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
358 if (SND_SOC_DAPM_EVENT_ON(event)) {
359 snd_soc_component_update_bits(component, SUN8I_ADDA_PAEN_HP_CTRL,
360 BIT(SUN8I_ADDA_PAEN_HP_CTRL_HPPAEN),
361 BIT(SUN8I_ADDA_PAEN_HP_CTRL_HPPAEN));
363 * Need a delay to have the amplifier up. 700ms seems the best
364 * compromise between the time to let the amplifier up and the
365 * time not to feel this delay while playing a sound.
368 } else if (SND_SOC_DAPM_EVENT_OFF(event)) {
369 snd_soc_component_update_bits(component, SUN8I_ADDA_PAEN_HP_CTRL,
370 BIT(SUN8I_ADDA_PAEN_HP_CTRL_HPPAEN),
377 static const struct snd_soc_dapm_widget sun8i_codec_headphone_widgets[] = {
378 SND_SOC_DAPM_MUX("Headphone Source Playback Route",
379 SND_SOC_NOPM, 0, 0, sun8i_codec_hp_src),
380 SND_SOC_DAPM_OUT_DRV_E("Headphone Amp", SUN8I_ADDA_PAEN_HP_CTRL,
381 SUN8I_ADDA_PAEN_HP_CTRL_HPPAEN, 0, NULL, 0,
382 sun8i_headphone_amp_event,
383 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
384 SND_SOC_DAPM_SUPPLY("HPCOM Protection", SUN8I_ADDA_PAEN_HP_CTRL,
385 SUN8I_ADDA_PAEN_HP_CTRL_COMPTEN, 0, NULL, 0),
386 SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPCOM", SUN8I_ADDA_PAEN_HP_CTRL,
387 SUN8I_ADDA_PAEN_HP_CTRL_HPCOM_FC, 0x3, 0x3, 0),
388 SND_SOC_DAPM_OUTPUT("HP"),
391 static const struct snd_soc_dapm_route sun8i_codec_headphone_routes[] = {
392 { "Headphone Source Playback Route", "DAC", "Left DAC" },
393 { "Headphone Source Playback Route", "DAC", "Right DAC" },
394 { "Headphone Source Playback Route", "Mixer", "Left Mixer" },
395 { "Headphone Source Playback Route", "Mixer", "Right Mixer" },
396 { "Headphone Amp", NULL, "Headphone Source Playback Route" },
397 { "HPCOM", NULL, "HPCOM Protection" },
398 { "HP", NULL, "Headphone Amp" },
401 static int sun8i_codec_add_headphone(struct snd_soc_component *cmpnt)
403 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
404 struct device *dev = cmpnt->dev;
407 ret = snd_soc_add_component_controls(cmpnt,
408 sun8i_codec_headphone_controls,
409 ARRAY_SIZE(sun8i_codec_headphone_controls));
411 dev_err(dev, "Failed to add Headphone controls: %d\n", ret);
415 ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_headphone_widgets,
416 ARRAY_SIZE(sun8i_codec_headphone_widgets));
418 dev_err(dev, "Failed to add Headphone DAPM widgets: %d\n", ret);
422 ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_headphone_routes,
423 ARRAY_SIZE(sun8i_codec_headphone_routes));
425 dev_err(dev, "Failed to add Headphone DAPM routes: %d\n", ret);
432 /* mbias specific widget */
433 static const struct snd_soc_dapm_widget sun8i_codec_mbias_widgets[] = {
434 SND_SOC_DAPM_SUPPLY("MBIAS", SUN8I_ADDA_MIC1G_MICBIAS_CTRL,
435 SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MMICBIASEN,
439 static int sun8i_codec_add_mbias(struct snd_soc_component *cmpnt)
441 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
442 struct device *dev = cmpnt->dev;
445 ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_mbias_widgets,
446 ARRAY_SIZE(sun8i_codec_mbias_widgets));
448 dev_err(dev, "Failed to add MBIAS DAPM widgets: %d\n", ret);
453 /* hmic specific widget */
454 static const struct snd_soc_dapm_widget sun8i_codec_hmic_widgets[] = {
455 SND_SOC_DAPM_SUPPLY("HBIAS", SUN8I_ADDA_MIC1G_MICBIAS_CTRL,
456 SUN8I_ADDA_MIC1G_MICBIAS_CTRL_HMICBIASEN,
460 static int sun8i_codec_add_hmic(struct snd_soc_component *cmpnt)
462 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
463 struct device *dev = cmpnt->dev;
466 ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_hmic_widgets,
467 ARRAY_SIZE(sun8i_codec_hmic_widgets));
469 dev_err(dev, "Failed to add Mic3 DAPM widgets: %d\n", ret);
474 /* line in specific controls, widgets and rines */
475 static const struct snd_kcontrol_new sun8i_codec_linein_controls[] = {
477 SOC_SINGLE_TLV("Line In Playback Volume", SUN8I_ADDA_LINEIN_GCTRL,
478 SUN8I_ADDA_LINEIN_GCTRL_LINEING,
479 0x7, 0, sun8i_codec_out_mixer_pregain_scale),
482 static const struct snd_soc_dapm_widget sun8i_codec_linein_widgets[] = {
484 SND_SOC_DAPM_INPUT("LINEIN"),
487 static const struct snd_soc_dapm_route sun8i_codec_linein_routes[] = {
488 { "Left Mixer", "Line In Playback Switch", "LINEIN" },
490 { "Right Mixer", "Line In Playback Switch", "LINEIN" },
492 { "Left ADC Mixer", "Line In Capture Switch", "LINEIN" },
494 { "Right ADC Mixer", "Line In Capture Switch", "LINEIN" },
497 static int sun8i_codec_add_linein(struct snd_soc_component *cmpnt)
499 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
500 struct device *dev = cmpnt->dev;
503 ret = snd_soc_add_component_controls(cmpnt,
504 sun8i_codec_linein_controls,
505 ARRAY_SIZE(sun8i_codec_linein_controls));
507 dev_err(dev, "Failed to add Line In controls: %d\n", ret);
511 ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_linein_widgets,
512 ARRAY_SIZE(sun8i_codec_linein_widgets));
514 dev_err(dev, "Failed to add Line In DAPM widgets: %d\n", ret);
518 ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_linein_routes,
519 ARRAY_SIZE(sun8i_codec_linein_routes));
521 dev_err(dev, "Failed to add Line In DAPM routes: %d\n", ret);
529 /* line out specific controls, widgets and routes */
530 static const DECLARE_TLV_DB_RANGE(sun8i_codec_lineout_vol_scale,
531 0, 1, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
532 2, 31, TLV_DB_SCALE_ITEM(-4350, 150, 0),
534 static const struct snd_kcontrol_new sun8i_codec_lineout_controls[] = {
535 SOC_SINGLE_TLV("Line Out Playback Volume",
536 SUN8I_ADDA_PHONE_GAIN_CTRL,
537 SUN8I_ADDA_PHONE_GAIN_CTRL_LINEOUT_VOL, 0x1f, 0,
538 sun8i_codec_lineout_vol_scale),
539 SOC_DOUBLE("Line Out Playback Switch",
540 SUN8I_ADDA_MIC2G_CTRL,
541 SUN8I_ADDA_MIC2G_CTRL_LINEOUTLEN,
542 SUN8I_ADDA_MIC2G_CTRL_LINEOUTREN, 1, 0),
545 static const char * const sun8i_codec_lineout_src_enum_text[] = {
546 "Stereo", "Mono Differential",
549 static SOC_ENUM_DOUBLE_DECL(sun8i_codec_lineout_src_enum,
550 SUN8I_ADDA_MIC2G_CTRL,
551 SUN8I_ADDA_MIC2G_CTRL_LINEOUTLSRC,
552 SUN8I_ADDA_MIC2G_CTRL_LINEOUTRSRC,
553 sun8i_codec_lineout_src_enum_text);
555 static const struct snd_kcontrol_new sun8i_codec_lineout_src[] = {
556 SOC_DAPM_ENUM("Line Out Source Playback Route",
557 sun8i_codec_lineout_src_enum),
560 static const struct snd_soc_dapm_widget sun8i_codec_lineout_widgets[] = {
561 SND_SOC_DAPM_MUX("Line Out Source Playback Route",
562 SND_SOC_NOPM, 0, 0, sun8i_codec_lineout_src),
563 /* It is unclear if this is a buffer or gate, model it as a supply */
564 SND_SOC_DAPM_SUPPLY("Line Out Enable", SUN8I_ADDA_PAEN_HP_CTRL,
565 SUN8I_ADDA_PAEN_HP_CTRL_LINEOUTEN, 0, NULL, 0),
566 SND_SOC_DAPM_OUTPUT("LINEOUT"),
569 static const struct snd_soc_dapm_route sun8i_codec_lineout_routes[] = {
570 { "Line Out Source Playback Route", "Stereo", "Left Mixer" },
571 { "Line Out Source Playback Route", "Stereo", "Right Mixer" },
572 { "Line Out Source Playback Route", "Mono Differential", "Left Mixer" },
573 { "Line Out Source Playback Route", "Mono Differential", "Right Mixer" },
574 { "LINEOUT", NULL, "Line Out Source Playback Route" },
575 { "LINEOUT", NULL, "Line Out Enable", },
578 static int sun8i_codec_add_lineout(struct snd_soc_component *cmpnt)
580 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
581 struct device *dev = cmpnt->dev;
584 ret = snd_soc_add_component_controls(cmpnt,
585 sun8i_codec_lineout_controls,
586 ARRAY_SIZE(sun8i_codec_lineout_controls));
588 dev_err(dev, "Failed to add Line Out controls: %d\n", ret);
592 ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_lineout_widgets,
593 ARRAY_SIZE(sun8i_codec_lineout_widgets));
595 dev_err(dev, "Failed to add Line Out DAPM widgets: %d\n", ret);
599 ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_lineout_routes,
600 ARRAY_SIZE(sun8i_codec_lineout_routes));
602 dev_err(dev, "Failed to add Line Out DAPM routes: %d\n", ret);
609 /* mic2 specific controls, widgets and routes */
610 static const struct snd_kcontrol_new sun8i_codec_mic2_controls[] = {
612 SOC_SINGLE_TLV("Mic2 Playback Volume",
613 SUN8I_ADDA_MICIN_GCTRL, SUN8I_ADDA_MICIN_GCTRL_MIC2G,
614 0x7, 0, sun8i_codec_out_mixer_pregain_scale),
616 /* Microphone Amp boost gain */
617 SOC_SINGLE_TLV("Mic2 Boost Volume", SUN8I_ADDA_MIC2G_CTRL,
618 SUN8I_ADDA_MIC2G_CTRL_MIC2BOOST, 0x7, 0,
619 sun8i_codec_mic_gain_scale),
622 static const struct snd_soc_dapm_widget sun8i_codec_mic2_widgets[] = {
623 /* Microphone input */
624 SND_SOC_DAPM_INPUT("MIC2"),
627 SND_SOC_DAPM_PGA("Mic2 Amplifier", SUN8I_ADDA_MIC2G_CTRL,
628 SUN8I_ADDA_MIC2G_CTRL_MIC2AMPEN, 0, NULL, 0),
631 static const struct snd_soc_dapm_route sun8i_codec_mic2_routes[] = {
632 { "Mic2 Amplifier", NULL, "MIC2"},
634 { "Left Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
636 { "Right Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
638 { "Left ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
640 { "Right ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
643 static int sun8i_codec_add_mic2(struct snd_soc_component *cmpnt)
645 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
646 struct device *dev = cmpnt->dev;
649 ret = snd_soc_add_component_controls(cmpnt,
650 sun8i_codec_mic2_controls,
651 ARRAY_SIZE(sun8i_codec_mic2_controls));
653 dev_err(dev, "Failed to add MIC2 controls: %d\n", ret);
657 ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_mic2_widgets,
658 ARRAY_SIZE(sun8i_codec_mic2_widgets));
660 dev_err(dev, "Failed to add MIC2 DAPM widgets: %d\n", ret);
664 ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_mic2_routes,
665 ARRAY_SIZE(sun8i_codec_mic2_routes));
667 dev_err(dev, "Failed to add MIC2 DAPM routes: %d\n", ret);
674 struct sun8i_codec_analog_quirks {
683 static const struct sun8i_codec_analog_quirks sun8i_a23_quirks = {
684 .has_headphone = true,
691 static const struct sun8i_codec_analog_quirks sun8i_h3_quirks = {
698 static int sun8i_codec_analog_add_mixer(struct snd_soc_component *cmpnt,
699 const struct sun8i_codec_analog_quirks *quirks)
701 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
702 struct device *dev = cmpnt->dev;
705 if (!quirks->has_mic2 && !quirks->has_linein) {
707 * Apply the special widget set which has uses a control
708 * without MIC2 and Line In, for SoCs without these.
709 * TODO: not all special cases are supported now, this case
710 * is present because it's the case of V3s.
712 ret = snd_soc_dapm_new_controls(dapm,
713 sun8i_v3s_codec_mixer_widgets,
714 ARRAY_SIZE(sun8i_v3s_codec_mixer_widgets));
716 dev_err(dev, "Failed to add V3s Mixer DAPM widgets: %d\n", ret);
720 /* Apply the generic mixer widget set. */
721 ret = snd_soc_dapm_new_controls(dapm,
722 sun8i_codec_mixer_widgets,
723 ARRAY_SIZE(sun8i_codec_mixer_widgets));
725 dev_err(dev, "Failed to add Mixer DAPM widgets: %d\n", ret);
730 ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_mixer_routes,
731 ARRAY_SIZE(sun8i_codec_mixer_routes));
733 dev_err(dev, "Failed to add Mixer DAPM routes: %d\n", ret);
740 static const struct sun8i_codec_analog_quirks sun8i_v3s_quirks = {
741 .has_headphone = true,
745 static int sun8i_codec_analog_cmpnt_probe(struct snd_soc_component *cmpnt)
747 struct device *dev = cmpnt->dev;
748 const struct sun8i_codec_analog_quirks *quirks;
752 * This would never return NULL unless someone directly registers a
753 * platform device matching this driver's name, without specifying a
756 quirks = of_device_get_match_data(dev);
758 /* Add controls, widgets, and routes for individual features */
759 ret = sun8i_codec_analog_add_mixer(cmpnt, quirks);
763 if (quirks->has_headphone) {
764 ret = sun8i_codec_add_headphone(cmpnt);
769 if (quirks->has_hmic) {
770 ret = sun8i_codec_add_hmic(cmpnt);
775 if (quirks->has_linein) {
776 ret = sun8i_codec_add_linein(cmpnt);
781 if (quirks->has_lineout) {
782 ret = sun8i_codec_add_lineout(cmpnt);
787 if (quirks->has_mbias) {
788 ret = sun8i_codec_add_mbias(cmpnt);
793 if (quirks->has_mic2) {
794 ret = sun8i_codec_add_mic2(cmpnt);
802 static const struct snd_soc_component_driver sun8i_codec_analog_cmpnt_drv = {
803 .controls = sun8i_codec_common_controls,
804 .num_controls = ARRAY_SIZE(sun8i_codec_common_controls),
805 .dapm_widgets = sun8i_codec_common_widgets,
806 .num_dapm_widgets = ARRAY_SIZE(sun8i_codec_common_widgets),
807 .dapm_routes = sun8i_codec_common_routes,
808 .num_dapm_routes = ARRAY_SIZE(sun8i_codec_common_routes),
809 .probe = sun8i_codec_analog_cmpnt_probe,
812 static const struct of_device_id sun8i_codec_analog_of_match[] = {
814 .compatible = "allwinner,sun8i-a23-codec-analog",
815 .data = &sun8i_a23_quirks,
818 .compatible = "allwinner,sun8i-h3-codec-analog",
819 .data = &sun8i_h3_quirks,
822 .compatible = "allwinner,sun8i-v3s-codec-analog",
823 .data = &sun8i_v3s_quirks,
827 MODULE_DEVICE_TABLE(of, sun8i_codec_analog_of_match);
829 static int sun8i_codec_analog_probe(struct platform_device *pdev)
831 struct resource *res;
832 struct regmap *regmap;
835 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
836 base = devm_ioremap_resource(&pdev->dev, res);
838 dev_err(&pdev->dev, "Failed to map the registers\n");
839 return PTR_ERR(base);
842 regmap = sun8i_adda_pr_regmap_init(&pdev->dev, base);
843 if (IS_ERR(regmap)) {
844 dev_err(&pdev->dev, "Failed to create regmap\n");
845 return PTR_ERR(regmap);
848 return devm_snd_soc_register_component(&pdev->dev,
849 &sun8i_codec_analog_cmpnt_drv,
853 static struct platform_driver sun8i_codec_analog_driver = {
855 .name = "sun8i-codec-analog",
856 .of_match_table = sun8i_codec_analog_of_match,
858 .probe = sun8i_codec_analog_probe,
860 module_platform_driver(sun8i_codec_analog_driver);
862 MODULE_DESCRIPTION("Allwinner internal codec analog controls driver");
863 MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
864 MODULE_LICENSE("GPL");
865 MODULE_ALIAS("platform:sun8i-codec-analog");