xref: /openbmc/linux/sound/soc/codecs/adau1761.c (revision 3d3337de)
1 /*
2  * Driver for ADAU1761/ADAU1461/ADAU1761/ADAU1961 codec
3  *
4  * Copyright 2011-2013 Analog Devices Inc.
5  * Author: Lars-Peter Clausen <lars@metafoo.de>
6  *
7  * Licensed under the GPL-2 or later.
8  */
9 
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/i2c.h>
13 #include <linux/spi/spi.h>
14 #include <linux/slab.h>
15 #include <sound/core.h>
16 #include <sound/pcm.h>
17 #include <sound/pcm_params.h>
18 #include <sound/soc.h>
19 #include <sound/tlv.h>
20 #include <linux/platform_data/adau17x1.h>
21 
22 #include "adau17x1.h"
23 #include "adau1761.h"
24 
25 #define ADAU1761_DIGMIC_JACKDETECT	0x4008
26 #define ADAU1761_REC_MIXER_LEFT0	0x400a
27 #define ADAU1761_REC_MIXER_LEFT1	0x400b
28 #define ADAU1761_REC_MIXER_RIGHT0	0x400c
29 #define ADAU1761_REC_MIXER_RIGHT1	0x400d
30 #define ADAU1761_LEFT_DIFF_INPUT_VOL	0x400e
31 #define ADAU1761_RIGHT_DIFF_INPUT_VOL	0x400f
32 #define ADAU1761_PLAY_LR_MIXER_LEFT	0x4020
33 #define ADAU1761_PLAY_MIXER_LEFT0	0x401c
34 #define ADAU1761_PLAY_MIXER_LEFT1	0x401d
35 #define ADAU1761_PLAY_MIXER_RIGHT0	0x401e
36 #define ADAU1761_PLAY_MIXER_RIGHT1	0x401f
37 #define ADAU1761_PLAY_LR_MIXER_RIGHT	0x4021
38 #define ADAU1761_PLAY_MIXER_MONO	0x4022
39 #define ADAU1761_PLAY_HP_LEFT_VOL	0x4023
40 #define ADAU1761_PLAY_HP_RIGHT_VOL	0x4024
41 #define ADAU1761_PLAY_LINE_LEFT_VOL	0x4025
42 #define ADAU1761_PLAY_LINE_RIGHT_VOL	0x4026
43 #define ADAU1761_PLAY_MONO_OUTPUT_VOL	0x4027
44 #define ADAU1761_POP_CLICK_SUPPRESS	0x4028
45 #define ADAU1761_JACK_DETECT_PIN	0x4031
46 #define ADAU1761_DEJITTER		0x4036
47 #define ADAU1761_CLK_ENABLE0		0x40f9
48 #define ADAU1761_CLK_ENABLE1		0x40fa
49 
50 #define ADAU1761_DIGMIC_JACKDETECT_ACTIVE_LOW	BIT(0)
51 #define ADAU1761_DIGMIC_JACKDETECT_DIGMIC	BIT(5)
52 
53 #define ADAU1761_DIFF_INPUT_VOL_LDEN		BIT(0)
54 
55 #define ADAU1761_PLAY_MONO_OUTPUT_VOL_MODE_HP	BIT(0)
56 #define ADAU1761_PLAY_MONO_OUTPUT_VOL_UNMUTE	BIT(1)
57 
58 #define ADAU1761_PLAY_HP_RIGHT_VOL_MODE_HP	BIT(0)
59 
60 #define ADAU1761_PLAY_LINE_LEFT_VOL_MODE_HP	BIT(0)
61 
62 #define ADAU1761_PLAY_LINE_RIGHT_VOL_MODE_HP	BIT(0)
63 
64 
65 #define ADAU1761_FIRMWARE "adau1761.bin"
66 
67 static const struct reg_default adau1761_reg_defaults[] = {
68 	{ ADAU1761_DEJITTER,			0x03 },
69 	{ ADAU1761_DIGMIC_JACKDETECT,		0x00 },
70 	{ ADAU1761_REC_MIXER_LEFT0,		0x00 },
71 	{ ADAU1761_REC_MIXER_LEFT1,		0x00 },
72 	{ ADAU1761_REC_MIXER_RIGHT0,		0x00 },
73 	{ ADAU1761_REC_MIXER_RIGHT1,		0x00 },
74 	{ ADAU1761_LEFT_DIFF_INPUT_VOL,		0x00 },
75 	{ ADAU1761_RIGHT_DIFF_INPUT_VOL,	0x00 },
76 	{ ADAU1761_PLAY_LR_MIXER_LEFT,		0x00 },
77 	{ ADAU1761_PLAY_MIXER_LEFT0,		0x00 },
78 	{ ADAU1761_PLAY_MIXER_LEFT1,		0x00 },
79 	{ ADAU1761_PLAY_MIXER_RIGHT0,		0x00 },
80 	{ ADAU1761_PLAY_MIXER_RIGHT1,		0x00 },
81 	{ ADAU1761_PLAY_LR_MIXER_RIGHT,		0x00 },
82 	{ ADAU1761_PLAY_MIXER_MONO,		0x00 },
83 	{ ADAU1761_PLAY_HP_LEFT_VOL,		0x00 },
84 	{ ADAU1761_PLAY_HP_RIGHT_VOL,		0x00 },
85 	{ ADAU1761_PLAY_LINE_LEFT_VOL,		0x00 },
86 	{ ADAU1761_PLAY_LINE_RIGHT_VOL,		0x00 },
87 	{ ADAU1761_PLAY_MONO_OUTPUT_VOL,	0x00 },
88 	{ ADAU1761_POP_CLICK_SUPPRESS,		0x00 },
89 	{ ADAU1761_JACK_DETECT_PIN,		0x00 },
90 	{ ADAU1761_CLK_ENABLE0,			0x00 },
91 	{ ADAU1761_CLK_ENABLE1,			0x00 },
92 	{ ADAU17X1_CLOCK_CONTROL,		0x00 },
93 	{ ADAU17X1_PLL_CONTROL,			0x00 },
94 	{ ADAU17X1_REC_POWER_MGMT,		0x00 },
95 	{ ADAU17X1_MICBIAS,			0x00 },
96 	{ ADAU17X1_SERIAL_PORT0,		0x00 },
97 	{ ADAU17X1_SERIAL_PORT1,		0x00 },
98 	{ ADAU17X1_CONVERTER0,			0x00 },
99 	{ ADAU17X1_CONVERTER1,			0x00 },
100 	{ ADAU17X1_LEFT_INPUT_DIGITAL_VOL,	0x00 },
101 	{ ADAU17X1_RIGHT_INPUT_DIGITAL_VOL,	0x00 },
102 	{ ADAU17X1_ADC_CONTROL,			0x00 },
103 	{ ADAU17X1_PLAY_POWER_MGMT,		0x00 },
104 	{ ADAU17X1_DAC_CONTROL0,		0x00 },
105 	{ ADAU17X1_DAC_CONTROL1,		0x00 },
106 	{ ADAU17X1_DAC_CONTROL2,		0x00 },
107 	{ ADAU17X1_SERIAL_PORT_PAD,		0xaa },
108 	{ ADAU17X1_CONTROL_PORT_PAD0,		0xaa },
109 	{ ADAU17X1_CONTROL_PORT_PAD1,		0x00 },
110 	{ ADAU17X1_DSP_SAMPLING_RATE,		0x01 },
111 	{ ADAU17X1_SERIAL_INPUT_ROUTE,		0x00 },
112 	{ ADAU17X1_SERIAL_OUTPUT_ROUTE,		0x00 },
113 	{ ADAU17X1_DSP_ENABLE,			0x00 },
114 	{ ADAU17X1_DSP_RUN,			0x00 },
115 	{ ADAU17X1_SERIAL_SAMPLING_RATE,	0x00 },
116 };
117 
118 static const DECLARE_TLV_DB_SCALE(adau1761_sing_in_tlv, -1500, 300, 1);
119 static const DECLARE_TLV_DB_SCALE(adau1761_diff_in_tlv, -1200, 75, 0);
120 static const DECLARE_TLV_DB_SCALE(adau1761_out_tlv, -5700, 100, 0);
121 static const DECLARE_TLV_DB_SCALE(adau1761_sidetone_tlv, -1800, 300, 1);
122 static const DECLARE_TLV_DB_SCALE(adau1761_boost_tlv, -600, 600, 1);
123 static const DECLARE_TLV_DB_SCALE(adau1761_pga_boost_tlv, -2000, 2000, 1);
124 
125 static const unsigned int adau1761_bias_select_values[] = {
126 	0, 2, 3,
127 };
128 
129 static const char * const adau1761_bias_select_text[] = {
130 	"Normal operation", "Enhanced performance", "Power saving",
131 };
132 
133 static const char * const adau1761_bias_select_extreme_text[] = {
134 	"Normal operation", "Extreme power saving", "Enhanced performance",
135 	"Power saving",
136 };
137 
138 static SOC_ENUM_SINGLE_DECL(adau1761_adc_bias_enum,
139 		ADAU17X1_REC_POWER_MGMT, 3, adau1761_bias_select_extreme_text);
140 static SOC_ENUM_SINGLE_DECL(adau1761_hp_bias_enum,
141 		ADAU17X1_PLAY_POWER_MGMT, 6, adau1761_bias_select_extreme_text);
142 static SOC_ENUM_SINGLE_DECL(adau1761_dac_bias_enum,
143 		ADAU17X1_PLAY_POWER_MGMT, 4, adau1761_bias_select_extreme_text);
144 static SOC_VALUE_ENUM_SINGLE_DECL(adau1761_playback_bias_enum,
145 		ADAU17X1_PLAY_POWER_MGMT, 2, 0x3, adau1761_bias_select_text,
146 		adau1761_bias_select_values);
147 static SOC_VALUE_ENUM_SINGLE_DECL(adau1761_capture_bias_enum,
148 		ADAU17X1_REC_POWER_MGMT, 1, 0x3, adau1761_bias_select_text,
149 		adau1761_bias_select_values);
150 
151 static const struct snd_kcontrol_new adau1761_jack_detect_controls[] = {
152 	SOC_SINGLE("Speaker Auto-mute Switch", ADAU1761_DIGMIC_JACKDETECT,
153 		4, 1, 0),
154 };
155 
156 static const struct snd_kcontrol_new adau1761_differential_mode_controls[] = {
157 	SOC_DOUBLE_R_TLV("Capture Volume", ADAU1761_LEFT_DIFF_INPUT_VOL,
158 		ADAU1761_RIGHT_DIFF_INPUT_VOL, 2, 0x3f, 0,
159 		adau1761_diff_in_tlv),
160 	SOC_DOUBLE_R("Capture Switch", ADAU1761_LEFT_DIFF_INPUT_VOL,
161 		ADAU1761_RIGHT_DIFF_INPUT_VOL, 1, 1, 0),
162 
163 	SOC_DOUBLE_R_TLV("PGA Boost Capture Volume", ADAU1761_REC_MIXER_LEFT1,
164 		ADAU1761_REC_MIXER_RIGHT1, 3, 2, 0, adau1761_pga_boost_tlv),
165 };
166 
167 static const struct snd_kcontrol_new adau1761_single_mode_controls[] = {
168 	SOC_SINGLE_TLV("Input 1 Capture Volume", ADAU1761_REC_MIXER_LEFT0,
169 		4, 7, 0, adau1761_sing_in_tlv),
170 	SOC_SINGLE_TLV("Input 2 Capture Volume", ADAU1761_REC_MIXER_LEFT0,
171 		1, 7, 0, adau1761_sing_in_tlv),
172 	SOC_SINGLE_TLV("Input 3 Capture Volume", ADAU1761_REC_MIXER_RIGHT0,
173 		4, 7, 0, adau1761_sing_in_tlv),
174 	SOC_SINGLE_TLV("Input 4 Capture Volume", ADAU1761_REC_MIXER_RIGHT0,
175 		1, 7, 0, adau1761_sing_in_tlv),
176 };
177 
178 static const struct snd_kcontrol_new adau1761_controls[] = {
179 	SOC_DOUBLE_R_TLV("Aux Capture Volume", ADAU1761_REC_MIXER_LEFT1,
180 		ADAU1761_REC_MIXER_RIGHT1, 0, 7, 0, adau1761_sing_in_tlv),
181 
182 	SOC_DOUBLE_R_TLV("Headphone Playback Volume", ADAU1761_PLAY_HP_LEFT_VOL,
183 		ADAU1761_PLAY_HP_RIGHT_VOL, 2, 0x3f, 0, adau1761_out_tlv),
184 	SOC_DOUBLE_R("Headphone Playback Switch", ADAU1761_PLAY_HP_LEFT_VOL,
185 		ADAU1761_PLAY_HP_RIGHT_VOL, 1, 1, 0),
186 	SOC_DOUBLE_R_TLV("Lineout Playback Volume", ADAU1761_PLAY_LINE_LEFT_VOL,
187 		ADAU1761_PLAY_LINE_RIGHT_VOL, 2, 0x3f, 0, adau1761_out_tlv),
188 	SOC_DOUBLE_R("Lineout Playback Switch", ADAU1761_PLAY_LINE_LEFT_VOL,
189 		ADAU1761_PLAY_LINE_RIGHT_VOL, 1, 1, 0),
190 
191 	SOC_ENUM("ADC Bias", adau1761_adc_bias_enum),
192 	SOC_ENUM("DAC Bias", adau1761_dac_bias_enum),
193 	SOC_ENUM("Capture Bias", adau1761_capture_bias_enum),
194 	SOC_ENUM("Playback Bias", adau1761_playback_bias_enum),
195 	SOC_ENUM("Headphone Bias", adau1761_hp_bias_enum),
196 };
197 
198 static const struct snd_kcontrol_new adau1761_mono_controls[] = {
199 	SOC_SINGLE_TLV("Mono Playback Volume", ADAU1761_PLAY_MONO_OUTPUT_VOL,
200 		2, 0x3f, 0, adau1761_out_tlv),
201 	SOC_SINGLE("Mono Playback Switch", ADAU1761_PLAY_MONO_OUTPUT_VOL,
202 		1, 1, 0),
203 };
204 
205 static const struct snd_kcontrol_new adau1761_left_mixer_controls[] = {
206 	SOC_DAPM_SINGLE_AUTODISABLE("Left DAC Switch",
207 		ADAU1761_PLAY_MIXER_LEFT0, 5, 1, 0),
208 	SOC_DAPM_SINGLE_AUTODISABLE("Right DAC Switch",
209 		ADAU1761_PLAY_MIXER_LEFT0, 6, 1, 0),
210 	SOC_DAPM_SINGLE_TLV("Aux Bypass Volume",
211 		ADAU1761_PLAY_MIXER_LEFT0, 1, 8, 0, adau1761_sidetone_tlv),
212 	SOC_DAPM_SINGLE_TLV("Right Bypass Volume",
213 		ADAU1761_PLAY_MIXER_LEFT1, 4, 8, 0, adau1761_sidetone_tlv),
214 	SOC_DAPM_SINGLE_TLV("Left Bypass Volume",
215 		ADAU1761_PLAY_MIXER_LEFT1, 0, 8, 0, adau1761_sidetone_tlv),
216 };
217 
218 static const struct snd_kcontrol_new adau1761_right_mixer_controls[] = {
219 	SOC_DAPM_SINGLE_AUTODISABLE("Left DAC Switch",
220 		ADAU1761_PLAY_MIXER_RIGHT0, 5, 1, 0),
221 	SOC_DAPM_SINGLE_AUTODISABLE("Right DAC Switch",
222 		ADAU1761_PLAY_MIXER_RIGHT0, 6, 1, 0),
223 	SOC_DAPM_SINGLE_TLV("Aux Bypass Volume",
224 		ADAU1761_PLAY_MIXER_RIGHT0, 1, 8, 0, adau1761_sidetone_tlv),
225 	SOC_DAPM_SINGLE_TLV("Right Bypass Volume",
226 		ADAU1761_PLAY_MIXER_RIGHT1, 4, 8, 0, adau1761_sidetone_tlv),
227 	SOC_DAPM_SINGLE_TLV("Left Bypass Volume",
228 		ADAU1761_PLAY_MIXER_RIGHT1, 0, 8, 0, adau1761_sidetone_tlv),
229 };
230 
231 static const struct snd_kcontrol_new adau1761_left_lr_mixer_controls[] = {
232 	SOC_DAPM_SINGLE_TLV("Left Volume",
233 		ADAU1761_PLAY_LR_MIXER_LEFT, 1, 2, 0, adau1761_boost_tlv),
234 	SOC_DAPM_SINGLE_TLV("Right Volume",
235 		ADAU1761_PLAY_LR_MIXER_LEFT, 3, 2, 0, adau1761_boost_tlv),
236 };
237 
238 static const struct snd_kcontrol_new adau1761_right_lr_mixer_controls[] = {
239 	SOC_DAPM_SINGLE_TLV("Left Volume",
240 		ADAU1761_PLAY_LR_MIXER_RIGHT, 1, 2, 0, adau1761_boost_tlv),
241 	SOC_DAPM_SINGLE_TLV("Right Volume",
242 		ADAU1761_PLAY_LR_MIXER_RIGHT, 3, 2, 0, adau1761_boost_tlv),
243 };
244 
245 static const char * const adau1761_input_mux_text[] = {
246 	"ADC", "DMIC",
247 };
248 
249 static SOC_ENUM_SINGLE_DECL(adau1761_input_mux_enum,
250 	ADAU17X1_ADC_CONTROL, 2, adau1761_input_mux_text);
251 
252 static const struct snd_kcontrol_new adau1761_input_mux_control =
253 	SOC_DAPM_ENUM("Input Select", adau1761_input_mux_enum);
254 
255 static int adau1761_dejitter_fixup(struct snd_soc_dapm_widget *w,
256 	struct snd_kcontrol *kcontrol, int event)
257 {
258 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
259 	struct adau *adau = snd_soc_codec_get_drvdata(codec);
260 
261 	/* After any power changes have been made the dejitter circuit
262 	 * has to be reinitialized. */
263 	regmap_write(adau->regmap, ADAU1761_DEJITTER, 0);
264 	if (!adau->master)
265 		regmap_write(adau->regmap, ADAU1761_DEJITTER, 3);
266 
267 	return 0;
268 }
269 
270 static const struct snd_soc_dapm_widget adau1x61_dapm_widgets[] = {
271 	SND_SOC_DAPM_MIXER("Left Input Mixer", ADAU1761_REC_MIXER_LEFT0, 0, 0,
272 		NULL, 0),
273 	SND_SOC_DAPM_MIXER("Right Input Mixer", ADAU1761_REC_MIXER_RIGHT0, 0, 0,
274 		NULL, 0),
275 
276 	SOC_MIXER_ARRAY("Left Playback Mixer", ADAU1761_PLAY_MIXER_LEFT0,
277 		0, 0, adau1761_left_mixer_controls),
278 	SOC_MIXER_ARRAY("Right Playback Mixer", ADAU1761_PLAY_MIXER_RIGHT0,
279 		0, 0, adau1761_right_mixer_controls),
280 	SOC_MIXER_ARRAY("Left LR Playback Mixer", ADAU1761_PLAY_LR_MIXER_LEFT,
281 		0, 0, adau1761_left_lr_mixer_controls),
282 	SOC_MIXER_ARRAY("Right LR Playback Mixer", ADAU1761_PLAY_LR_MIXER_RIGHT,
283 		0, 0, adau1761_right_lr_mixer_controls),
284 
285 	SND_SOC_DAPM_SUPPLY("Headphone", ADAU1761_PLAY_HP_LEFT_VOL,
286 		0, 0, NULL, 0),
287 
288 	SND_SOC_DAPM_SUPPLY_S("SYSCLK", 2, SND_SOC_NOPM, 0, 0, NULL, 0),
289 
290 	SND_SOC_DAPM_POST("Dejitter fixup", adau1761_dejitter_fixup),
291 
292 	SND_SOC_DAPM_INPUT("LAUX"),
293 	SND_SOC_DAPM_INPUT("RAUX"),
294 	SND_SOC_DAPM_INPUT("LINP"),
295 	SND_SOC_DAPM_INPUT("LINN"),
296 	SND_SOC_DAPM_INPUT("RINP"),
297 	SND_SOC_DAPM_INPUT("RINN"),
298 
299 	SND_SOC_DAPM_OUTPUT("LOUT"),
300 	SND_SOC_DAPM_OUTPUT("ROUT"),
301 	SND_SOC_DAPM_OUTPUT("LHP"),
302 	SND_SOC_DAPM_OUTPUT("RHP"),
303 };
304 
305 static const struct snd_soc_dapm_widget adau1761_mono_dapm_widgets[] = {
306 	SND_SOC_DAPM_MIXER("Mono Playback Mixer", ADAU1761_PLAY_MIXER_MONO,
307 		0, 0, NULL, 0),
308 
309 	SND_SOC_DAPM_OUTPUT("MONOOUT"),
310 };
311 
312 static const struct snd_soc_dapm_widget adau1761_capless_dapm_widgets[] = {
313 	SND_SOC_DAPM_SUPPLY_S("Headphone VGND", 1, ADAU1761_PLAY_MIXER_MONO,
314 		0, 0, NULL, 0),
315 };
316 
317 static const struct snd_soc_dapm_route adau1x61_dapm_routes[] = {
318 	{ "Left Input Mixer", NULL, "LINP" },
319 	{ "Left Input Mixer", NULL, "LINN" },
320 	{ "Left Input Mixer", NULL, "LAUX" },
321 
322 	{ "Right Input Mixer", NULL, "RINP" },
323 	{ "Right Input Mixer", NULL, "RINN" },
324 	{ "Right Input Mixer", NULL, "RAUX" },
325 
326 	{ "Left Playback Mixer", NULL, "Left Playback Enable"},
327 	{ "Right Playback Mixer", NULL, "Right Playback Enable"},
328 	{ "Left LR Playback Mixer", NULL, "Left Playback Enable"},
329 	{ "Right LR Playback Mixer", NULL, "Right Playback Enable"},
330 
331 	{ "Left Playback Mixer", "Left DAC Switch", "Left DAC" },
332 	{ "Left Playback Mixer", "Right DAC Switch", "Right DAC" },
333 
334 	{ "Right Playback Mixer", "Left DAC Switch", "Left DAC" },
335 	{ "Right Playback Mixer", "Right DAC Switch", "Right DAC" },
336 
337 	{ "Left LR Playback Mixer", "Left Volume", "Left Playback Mixer" },
338 	{ "Left LR Playback Mixer", "Right Volume", "Right Playback Mixer" },
339 
340 	{ "Right LR Playback Mixer", "Left Volume", "Left Playback Mixer" },
341 	{ "Right LR Playback Mixer", "Right Volume", "Right Playback Mixer" },
342 
343 	{ "LHP", NULL, "Left Playback Mixer" },
344 	{ "RHP", NULL, "Right Playback Mixer" },
345 
346 	{ "LHP", NULL, "Headphone" },
347 	{ "RHP", NULL, "Headphone" },
348 
349 	{ "LOUT", NULL, "Left LR Playback Mixer" },
350 	{ "ROUT", NULL, "Right LR Playback Mixer" },
351 
352 	{ "Left Playback Mixer", "Aux Bypass Volume", "LAUX" },
353 	{ "Left Playback Mixer", "Left Bypass Volume", "Left Input Mixer" },
354 	{ "Left Playback Mixer", "Right Bypass Volume", "Right Input Mixer" },
355 	{ "Right Playback Mixer", "Aux Bypass Volume", "RAUX" },
356 	{ "Right Playback Mixer", "Left Bypass Volume", "Left Input Mixer" },
357 	{ "Right Playback Mixer", "Right Bypass Volume", "Right Input Mixer" },
358 };
359 
360 static const struct snd_soc_dapm_route adau1761_mono_dapm_routes[] = {
361 	{ "Mono Playback Mixer", NULL, "Left Playback Mixer" },
362 	{ "Mono Playback Mixer", NULL, "Right Playback Mixer" },
363 
364 	{ "MONOOUT", NULL, "Mono Playback Mixer" },
365 };
366 
367 static const struct snd_soc_dapm_route adau1761_capless_dapm_routes[] = {
368 	{ "Headphone", NULL, "Headphone VGND" },
369 };
370 
371 static const struct snd_soc_dapm_widget adau1761_dmic_widgets[] = {
372 	SND_SOC_DAPM_MUX("Left Decimator Mux", SND_SOC_NOPM, 0, 0,
373 		&adau1761_input_mux_control),
374 	SND_SOC_DAPM_MUX("Right Decimator Mux", SND_SOC_NOPM, 0, 0,
375 		&adau1761_input_mux_control),
376 
377 	SND_SOC_DAPM_INPUT("DMIC"),
378 };
379 
380 static const struct snd_soc_dapm_route adau1761_dmic_routes[] = {
381 	{ "Left Decimator Mux", "ADC", "Left Input Mixer" },
382 	{ "Left Decimator Mux", "DMIC", "DMIC" },
383 	{ "Right Decimator Mux", "ADC", "Right Input Mixer" },
384 	{ "Right Decimator Mux", "DMIC", "DMIC" },
385 
386 	{ "Left Decimator", NULL, "Left Decimator Mux" },
387 	{ "Right Decimator", NULL, "Right Decimator Mux" },
388 };
389 
390 static const struct snd_soc_dapm_route adau1761_no_dmic_routes[] = {
391 	{ "Left Decimator", NULL, "Left Input Mixer" },
392 	{ "Right Decimator", NULL, "Right Input Mixer" },
393 };
394 
395 static const struct snd_soc_dapm_widget adau1761_dapm_widgets[] = {
396 	SND_SOC_DAPM_SUPPLY("Serial Port Clock", ADAU1761_CLK_ENABLE0,
397 		0, 0, NULL, 0),
398 	SND_SOC_DAPM_SUPPLY("Serial Input Routing Clock", ADAU1761_CLK_ENABLE0,
399 		1, 0, NULL, 0),
400 	SND_SOC_DAPM_SUPPLY("Serial Output Routing Clock", ADAU1761_CLK_ENABLE0,
401 		3, 0, NULL, 0),
402 
403 	SND_SOC_DAPM_SUPPLY("Decimator Resync Clock", ADAU1761_CLK_ENABLE0,
404 		4, 0, NULL, 0),
405 	SND_SOC_DAPM_SUPPLY("Interpolator Resync Clock", ADAU1761_CLK_ENABLE0,
406 		2, 0, NULL, 0),
407 
408 	SND_SOC_DAPM_SUPPLY("Slew Clock", ADAU1761_CLK_ENABLE0, 6, 0, NULL, 0),
409 	SND_SOC_DAPM_SUPPLY("ALC Clock", ADAU1761_CLK_ENABLE0, 5, 0, NULL, 0),
410 
411 	SND_SOC_DAPM_SUPPLY_S("Digital Clock 0", 1, ADAU1761_CLK_ENABLE1,
412 		0, 0, NULL, 0),
413 	SND_SOC_DAPM_SUPPLY_S("Digital Clock 1", 1, ADAU1761_CLK_ENABLE1,
414 		1, 0, NULL, 0),
415 };
416 
417 static const struct snd_soc_dapm_route adau1761_dapm_routes[] = {
418 	{ "Left Decimator", NULL, "Digital Clock 0", },
419 	{ "Right Decimator", NULL, "Digital Clock 0", },
420 	{ "Left DAC", NULL, "Digital Clock 0", },
421 	{ "Right DAC", NULL, "Digital Clock 0", },
422 
423 	{ "AIFCLK", NULL, "Digital Clock 1" },
424 
425 	{ "Playback", NULL, "Serial Port Clock" },
426 	{ "Capture", NULL, "Serial Port Clock" },
427 	{ "Playback", NULL, "Serial Input Routing Clock" },
428 	{ "Capture", NULL, "Serial Output Routing Clock" },
429 
430 	{ "Left Decimator", NULL, "Decimator Resync Clock" },
431 	{ "Right Decimator", NULL, "Decimator Resync Clock" },
432 	{ "Left DAC", NULL, "Interpolator Resync Clock" },
433 	{ "Right DAC", NULL, "Interpolator Resync Clock" },
434 
435 	{ "DSP", NULL, "Digital Clock 0" },
436 
437 	{ "Slew Clock", NULL, "Digital Clock 0" },
438 	{ "Right Playback Mixer", NULL, "Slew Clock" },
439 	{ "Left Playback Mixer", NULL, "Slew Clock" },
440 
441 	{ "Left Input Mixer", NULL, "ALC Clock" },
442 	{ "Right Input Mixer", NULL, "ALC Clock" },
443 
444 	{ "Digital Clock 0", NULL, "SYSCLK" },
445 	{ "Digital Clock 1", NULL, "SYSCLK" },
446 };
447 
448 static int adau1761_set_bias_level(struct snd_soc_codec *codec,
449 				 enum snd_soc_bias_level level)
450 {
451 	struct adau *adau = snd_soc_codec_get_drvdata(codec);
452 
453 	switch (level) {
454 	case SND_SOC_BIAS_ON:
455 		break;
456 	case SND_SOC_BIAS_PREPARE:
457 		break;
458 	case SND_SOC_BIAS_STANDBY:
459 		regmap_update_bits(adau->regmap, ADAU17X1_CLOCK_CONTROL,
460 			ADAU17X1_CLOCK_CONTROL_SYSCLK_EN,
461 			ADAU17X1_CLOCK_CONTROL_SYSCLK_EN);
462 		break;
463 	case SND_SOC_BIAS_OFF:
464 		regmap_update_bits(adau->regmap, ADAU17X1_CLOCK_CONTROL,
465 			ADAU17X1_CLOCK_CONTROL_SYSCLK_EN, 0);
466 		break;
467 
468 	}
469 	codec->dapm.bias_level = level;
470 	return 0;
471 }
472 
473 static enum adau1761_output_mode adau1761_get_lineout_mode(
474 	struct snd_soc_codec *codec)
475 {
476 	struct adau1761_platform_data *pdata = codec->dev->platform_data;
477 
478 	if (pdata)
479 		return pdata->lineout_mode;
480 
481 	return ADAU1761_OUTPUT_MODE_LINE;
482 }
483 
484 static int adau1761_setup_digmic_jackdetect(struct snd_soc_codec *codec)
485 {
486 	struct adau1761_platform_data *pdata = codec->dev->platform_data;
487 	struct adau *adau = snd_soc_codec_get_drvdata(codec);
488 	enum adau1761_digmic_jackdet_pin_mode mode;
489 	unsigned int val = 0;
490 	int ret;
491 
492 	if (pdata)
493 		mode = pdata->digmic_jackdetect_pin_mode;
494 	else
495 		mode = ADAU1761_DIGMIC_JACKDET_PIN_MODE_NONE;
496 
497 	switch (mode) {
498 	case ADAU1761_DIGMIC_JACKDET_PIN_MODE_JACKDETECT:
499 		switch (pdata->jackdetect_debounce_time) {
500 		case ADAU1761_JACKDETECT_DEBOUNCE_5MS:
501 		case ADAU1761_JACKDETECT_DEBOUNCE_10MS:
502 		case ADAU1761_JACKDETECT_DEBOUNCE_20MS:
503 		case ADAU1761_JACKDETECT_DEBOUNCE_40MS:
504 			val |= pdata->jackdetect_debounce_time << 6;
505 			break;
506 		default:
507 			return -EINVAL;
508 		}
509 		if (pdata->jackdetect_active_low)
510 			val |= ADAU1761_DIGMIC_JACKDETECT_ACTIVE_LOW;
511 
512 		ret = snd_soc_add_codec_controls(codec,
513 			adau1761_jack_detect_controls,
514 			ARRAY_SIZE(adau1761_jack_detect_controls));
515 		if (ret)
516 			return ret;
517 	case ADAU1761_DIGMIC_JACKDET_PIN_MODE_NONE: /* fallthrough */
518 		ret = snd_soc_dapm_add_routes(&codec->dapm,
519 			adau1761_no_dmic_routes,
520 			ARRAY_SIZE(adau1761_no_dmic_routes));
521 		if (ret)
522 			return ret;
523 		break;
524 	case ADAU1761_DIGMIC_JACKDET_PIN_MODE_DIGMIC:
525 		ret = snd_soc_dapm_new_controls(&codec->dapm,
526 			adau1761_dmic_widgets,
527 			ARRAY_SIZE(adau1761_dmic_widgets));
528 		if (ret)
529 			return ret;
530 
531 		ret = snd_soc_dapm_add_routes(&codec->dapm,
532 			adau1761_dmic_routes,
533 			ARRAY_SIZE(adau1761_dmic_routes));
534 		if (ret)
535 			return ret;
536 
537 		val |= ADAU1761_DIGMIC_JACKDETECT_DIGMIC;
538 		break;
539 	default:
540 		return -EINVAL;
541 	}
542 
543 	regmap_write(adau->regmap, ADAU1761_DIGMIC_JACKDETECT, val);
544 
545 	return 0;
546 }
547 
548 static int adau1761_setup_headphone_mode(struct snd_soc_codec *codec)
549 {
550 	struct adau *adau = snd_soc_codec_get_drvdata(codec);
551 	struct adau1761_platform_data *pdata = codec->dev->platform_data;
552 	enum adau1761_output_mode mode;
553 	int ret;
554 
555 	if (pdata)
556 		mode = pdata->headphone_mode;
557 	else
558 		mode = ADAU1761_OUTPUT_MODE_HEADPHONE;
559 
560 	switch (mode) {
561 	case ADAU1761_OUTPUT_MODE_LINE:
562 		break;
563 	case ADAU1761_OUTPUT_MODE_HEADPHONE_CAPLESS:
564 		regmap_update_bits(adau->regmap, ADAU1761_PLAY_MONO_OUTPUT_VOL,
565 			ADAU1761_PLAY_MONO_OUTPUT_VOL_MODE_HP |
566 			ADAU1761_PLAY_MONO_OUTPUT_VOL_UNMUTE,
567 			ADAU1761_PLAY_MONO_OUTPUT_VOL_MODE_HP |
568 			ADAU1761_PLAY_MONO_OUTPUT_VOL_UNMUTE);
569 		/* fallthrough */
570 	case ADAU1761_OUTPUT_MODE_HEADPHONE:
571 		regmap_update_bits(adau->regmap, ADAU1761_PLAY_HP_RIGHT_VOL,
572 			ADAU1761_PLAY_HP_RIGHT_VOL_MODE_HP,
573 			ADAU1761_PLAY_HP_RIGHT_VOL_MODE_HP);
574 		break;
575 	default:
576 		return -EINVAL;
577 	}
578 
579 	if (mode == ADAU1761_OUTPUT_MODE_HEADPHONE_CAPLESS) {
580 		ret = snd_soc_dapm_new_controls(&codec->dapm,
581 			adau1761_capless_dapm_widgets,
582 			ARRAY_SIZE(adau1761_capless_dapm_widgets));
583 		if (ret)
584 			return ret;
585 		ret = snd_soc_dapm_add_routes(&codec->dapm,
586 			adau1761_capless_dapm_routes,
587 			ARRAY_SIZE(adau1761_capless_dapm_routes));
588 	} else {
589 		ret = snd_soc_add_codec_controls(codec, adau1761_mono_controls,
590 			ARRAY_SIZE(adau1761_mono_controls));
591 		if (ret)
592 			return ret;
593 		ret = snd_soc_dapm_new_controls(&codec->dapm,
594 			adau1761_mono_dapm_widgets,
595 			ARRAY_SIZE(adau1761_mono_dapm_widgets));
596 		if (ret)
597 			return ret;
598 		ret = snd_soc_dapm_add_routes(&codec->dapm,
599 			adau1761_mono_dapm_routes,
600 			ARRAY_SIZE(adau1761_mono_dapm_routes));
601 	}
602 
603 	return ret;
604 }
605 
606 static bool adau1761_readable_register(struct device *dev, unsigned int reg)
607 {
608 	switch (reg) {
609 	case ADAU1761_DIGMIC_JACKDETECT:
610 	case ADAU1761_REC_MIXER_LEFT0:
611 	case ADAU1761_REC_MIXER_LEFT1:
612 	case ADAU1761_REC_MIXER_RIGHT0:
613 	case ADAU1761_REC_MIXER_RIGHT1:
614 	case ADAU1761_LEFT_DIFF_INPUT_VOL:
615 	case ADAU1761_RIGHT_DIFF_INPUT_VOL:
616 	case ADAU1761_PLAY_LR_MIXER_LEFT:
617 	case ADAU1761_PLAY_MIXER_LEFT0:
618 	case ADAU1761_PLAY_MIXER_LEFT1:
619 	case ADAU1761_PLAY_MIXER_RIGHT0:
620 	case ADAU1761_PLAY_MIXER_RIGHT1:
621 	case ADAU1761_PLAY_LR_MIXER_RIGHT:
622 	case ADAU1761_PLAY_MIXER_MONO:
623 	case ADAU1761_PLAY_HP_LEFT_VOL:
624 	case ADAU1761_PLAY_HP_RIGHT_VOL:
625 	case ADAU1761_PLAY_LINE_LEFT_VOL:
626 	case ADAU1761_PLAY_LINE_RIGHT_VOL:
627 	case ADAU1761_PLAY_MONO_OUTPUT_VOL:
628 	case ADAU1761_POP_CLICK_SUPPRESS:
629 	case ADAU1761_JACK_DETECT_PIN:
630 	case ADAU1761_DEJITTER:
631 	case ADAU1761_CLK_ENABLE0:
632 	case ADAU1761_CLK_ENABLE1:
633 		return true;
634 	default:
635 		break;
636 	}
637 
638 	return adau17x1_readable_register(dev, reg);
639 }
640 
641 static int adau1761_codec_probe(struct snd_soc_codec *codec)
642 {
643 	struct adau1761_platform_data *pdata = codec->dev->platform_data;
644 	struct adau *adau = snd_soc_codec_get_drvdata(codec);
645 	int ret;
646 
647 	ret = adau17x1_add_widgets(codec);
648 	if (ret < 0)
649 		return ret;
650 
651 	if (pdata && pdata->input_differential) {
652 		regmap_update_bits(adau->regmap, ADAU1761_LEFT_DIFF_INPUT_VOL,
653 			ADAU1761_DIFF_INPUT_VOL_LDEN,
654 			ADAU1761_DIFF_INPUT_VOL_LDEN);
655 		regmap_update_bits(adau->regmap, ADAU1761_RIGHT_DIFF_INPUT_VOL,
656 			ADAU1761_DIFF_INPUT_VOL_LDEN,
657 			ADAU1761_DIFF_INPUT_VOL_LDEN);
658 		ret = snd_soc_add_codec_controls(codec,
659 			adau1761_differential_mode_controls,
660 			ARRAY_SIZE(adau1761_differential_mode_controls));
661 		if (ret)
662 			return ret;
663 	} else {
664 		ret = snd_soc_add_codec_controls(codec,
665 			adau1761_single_mode_controls,
666 			ARRAY_SIZE(adau1761_single_mode_controls));
667 		if (ret)
668 			return ret;
669 	}
670 
671 	switch (adau1761_get_lineout_mode(codec)) {
672 	case ADAU1761_OUTPUT_MODE_LINE:
673 		break;
674 	case ADAU1761_OUTPUT_MODE_HEADPHONE:
675 		regmap_update_bits(adau->regmap, ADAU1761_PLAY_LINE_LEFT_VOL,
676 			ADAU1761_PLAY_LINE_LEFT_VOL_MODE_HP,
677 			ADAU1761_PLAY_LINE_LEFT_VOL_MODE_HP);
678 		regmap_update_bits(adau->regmap, ADAU1761_PLAY_LINE_RIGHT_VOL,
679 			ADAU1761_PLAY_LINE_RIGHT_VOL_MODE_HP,
680 			ADAU1761_PLAY_LINE_RIGHT_VOL_MODE_HP);
681 		break;
682 	default:
683 		return -EINVAL;
684 	}
685 
686 	ret = adau1761_setup_headphone_mode(codec);
687 	if (ret)
688 		return ret;
689 
690 	ret = adau1761_setup_digmic_jackdetect(codec);
691 	if (ret)
692 		return ret;
693 
694 	if (adau->type == ADAU1761) {
695 		ret = snd_soc_dapm_new_controls(&codec->dapm,
696 			adau1761_dapm_widgets,
697 			ARRAY_SIZE(adau1761_dapm_widgets));
698 		if (ret)
699 			return ret;
700 
701 		ret = snd_soc_dapm_add_routes(&codec->dapm,
702 			adau1761_dapm_routes,
703 			ARRAY_SIZE(adau1761_dapm_routes));
704 		if (ret)
705 			return ret;
706 	}
707 
708 	ret = adau17x1_add_routes(codec);
709 	if (ret < 0)
710 		return ret;
711 
712 	return 0;
713 }
714 
715 static const struct snd_soc_codec_driver adau1761_codec_driver = {
716 	.probe = adau1761_codec_probe,
717 	.resume	= adau17x1_resume,
718 	.set_bias_level	= adau1761_set_bias_level,
719 	.suspend_bias_off = true,
720 
721 	.controls = adau1761_controls,
722 	.num_controls = ARRAY_SIZE(adau1761_controls),
723 	.dapm_widgets = adau1x61_dapm_widgets,
724 	.num_dapm_widgets = ARRAY_SIZE(adau1x61_dapm_widgets),
725 	.dapm_routes = adau1x61_dapm_routes,
726 	.num_dapm_routes = ARRAY_SIZE(adau1x61_dapm_routes),
727 };
728 
729 #define ADAU1761_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE | \
730 	SNDRV_PCM_FMTBIT_S32_LE)
731 
732 static struct snd_soc_dai_driver adau1361_dai_driver = {
733 	.name = "adau-hifi",
734 	.playback = {
735 		.stream_name = "Playback",
736 		.channels_min = 2,
737 		.channels_max = 4,
738 		.rates = SNDRV_PCM_RATE_8000_96000,
739 		.formats = ADAU1761_FORMATS,
740 	},
741 	.capture = {
742 		.stream_name = "Capture",
743 		.channels_min = 2,
744 		.channels_max = 4,
745 		.rates = SNDRV_PCM_RATE_8000_96000,
746 		.formats = ADAU1761_FORMATS,
747 	},
748 	.ops = &adau17x1_dai_ops,
749 };
750 
751 static struct snd_soc_dai_driver adau1761_dai_driver = {
752 	.name = "adau-hifi",
753 	.playback = {
754 		.stream_name = "Playback",
755 		.channels_min = 2,
756 		.channels_max = 8,
757 		.rates = SNDRV_PCM_RATE_8000_96000,
758 		.formats = ADAU1761_FORMATS,
759 	},
760 	.capture = {
761 		.stream_name = "Capture",
762 		.channels_min = 2,
763 		.channels_max = 8,
764 		.rates = SNDRV_PCM_RATE_8000_96000,
765 		.formats = ADAU1761_FORMATS,
766 	},
767 	.ops = &adau17x1_dai_ops,
768 };
769 
770 int adau1761_probe(struct device *dev, struct regmap *regmap,
771 	enum adau17x1_type type, void (*switch_mode)(struct device *dev))
772 {
773 	struct snd_soc_dai_driver *dai_drv;
774 	const char *firmware_name;
775 	int ret;
776 
777 	if (type == ADAU1361) {
778 		dai_drv = &adau1361_dai_driver;
779 		firmware_name = NULL;
780 	} else {
781 		dai_drv = &adau1761_dai_driver;
782 		firmware_name = ADAU1761_FIRMWARE;
783 	}
784 
785 	ret = adau17x1_probe(dev, regmap, type, switch_mode, firmware_name);
786 	if (ret)
787 		return ret;
788 
789 	return snd_soc_register_codec(dev, &adau1761_codec_driver, dai_drv, 1);
790 }
791 EXPORT_SYMBOL_GPL(adau1761_probe);
792 
793 const struct regmap_config adau1761_regmap_config = {
794 	.val_bits = 8,
795 	.reg_bits = 16,
796 	.max_register = 0x40fa,
797 	.reg_defaults = adau1761_reg_defaults,
798 	.num_reg_defaults = ARRAY_SIZE(adau1761_reg_defaults),
799 	.readable_reg = adau1761_readable_register,
800 	.volatile_reg = adau17x1_volatile_register,
801 	.precious_reg = adau17x1_precious_register,
802 	.cache_type = REGCACHE_RBTREE,
803 };
804 EXPORT_SYMBOL_GPL(adau1761_regmap_config);
805 
806 MODULE_DESCRIPTION("ASoC ADAU1361/ADAU1461/ADAU1761/ADAU1961 CODEC driver");
807 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
808 MODULE_LICENSE("GPL");
809