xref: /openbmc/linux/sound/soc/codecs/mc13783.c (revision 3df4fce7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
4  * Copyright 2009 Sascha Hauer, s.hauer@pengutronix.de
5  * Copyright 2012 Philippe Retornaz, philippe.retornaz@epfl.ch
6  *
7  * Initial development of this code was funded by
8  * Phytec Messtechnik GmbH, https://www.phytec.de
9  */
10 #include <linux/module.h>
11 #include <linux/device.h>
12 #include <linux/of.h>
13 #include <linux/mfd/mc13xxx.h>
14 #include <linux/slab.h>
15 #include <sound/core.h>
16 #include <sound/control.h>
17 #include <sound/pcm.h>
18 #include <sound/soc.h>
19 #include <sound/initval.h>
20 #include <sound/soc-dapm.h>
21 #include <linux/regmap.h>
22 
23 #include "mc13783.h"
24 
25 #define AUDIO_RX0_ALSPEN		(1 << 5)
26 #define AUDIO_RX0_ALSPSEL		(1 << 7)
27 #define AUDIO_RX0_ADDCDC		(1 << 21)
28 #define AUDIO_RX0_ADDSTDC		(1 << 22)
29 #define AUDIO_RX0_ADDRXIN		(1 << 23)
30 
31 #define AUDIO_RX1_PGARXEN		(1 << 0);
32 #define AUDIO_RX1_PGASTEN		(1 << 5)
33 #define AUDIO_RX1_ARXINEN		(1 << 10)
34 
35 #define AUDIO_TX_AMC1REN		(1 << 5)
36 #define AUDIO_TX_AMC1LEN		(1 << 7)
37 #define AUDIO_TX_AMC2EN			(1 << 9)
38 #define AUDIO_TX_ATXINEN		(1 << 11)
39 #define AUDIO_TX_RXINREC		(1 << 13)
40 
41 #define SSI_NETWORK_CDCTXRXSLOT(x)	(((x) & 0x3) << 2)
42 #define SSI_NETWORK_CDCTXSECSLOT(x)	(((x) & 0x3) << 4)
43 #define SSI_NETWORK_CDCRXSECSLOT(x)	(((x) & 0x3) << 6)
44 #define SSI_NETWORK_CDCRXSECGAIN(x)	(((x) & 0x3) << 8)
45 #define SSI_NETWORK_CDCSUMGAIN(x)	(1 << 10)
46 #define SSI_NETWORK_CDCFSDLY(x)		(1 << 11)
47 #define SSI_NETWORK_DAC_SLOTS_8		(1 << 12)
48 #define SSI_NETWORK_DAC_SLOTS_4		(2 << 12)
49 #define SSI_NETWORK_DAC_SLOTS_2		(3 << 12)
50 #define SSI_NETWORK_DAC_SLOT_MASK	(3 << 12)
51 #define SSI_NETWORK_DAC_RXSLOT_0_1	(0 << 14)
52 #define SSI_NETWORK_DAC_RXSLOT_2_3	(1 << 14)
53 #define SSI_NETWORK_DAC_RXSLOT_4_5	(2 << 14)
54 #define SSI_NETWORK_DAC_RXSLOT_6_7	(3 << 14)
55 #define SSI_NETWORK_DAC_RXSLOT_MASK	(3 << 14)
56 #define SSI_NETWORK_STDCRXSECSLOT(x)	(((x) & 0x3) << 16)
57 #define SSI_NETWORK_STDCRXSECGAIN(x)	(((x) & 0x3) << 18)
58 #define SSI_NETWORK_STDCSUMGAIN		(1 << 20)
59 
60 /*
61  * MC13783_AUDIO_CODEC and MC13783_AUDIO_DAC mostly share the same
62  * register layout
63  */
64 #define AUDIO_SSI_SEL			(1 << 0)
65 #define AUDIO_CLK_SEL			(1 << 1)
66 #define AUDIO_CSM			(1 << 2)
67 #define AUDIO_BCL_INV			(1 << 3)
68 #define AUDIO_CFS_INV			(1 << 4)
69 #define AUDIO_CFS(x)			(((x) & 0x3) << 5)
70 #define AUDIO_CLK(x)			(((x) & 0x7) << 7)
71 #define AUDIO_C_EN			(1 << 11)
72 #define AUDIO_C_CLK_EN			(1 << 12)
73 #define AUDIO_C_RESET			(1 << 15)
74 
75 #define AUDIO_CODEC_CDCFS8K16K		(1 << 10)
76 #define AUDIO_DAC_CFS_DLY_B		(1 << 10)
77 
78 struct mc13783_priv {
79 	struct mc13xxx *mc13xxx;
80 	struct regmap *regmap;
81 
82 	enum mc13783_ssi_port adc_ssi_port;
83 	enum mc13783_ssi_port dac_ssi_port;
84 };
85 
86 /* Mapping between sample rates and register value */
87 static unsigned int mc13783_rates[] = {
88 	8000, 11025, 12000, 16000,
89 	22050, 24000, 32000, 44100,
90 	48000, 64000, 96000
91 };
92 
93 static int mc13783_pcm_hw_params_dac(struct snd_pcm_substream *substream,
94 				struct snd_pcm_hw_params *params,
95 				struct snd_soc_dai *dai)
96 {
97 	struct snd_soc_component *component = dai->component;
98 	unsigned int rate = params_rate(params);
99 	int i;
100 
101 	for (i = 0; i < ARRAY_SIZE(mc13783_rates); i++) {
102 		if (rate == mc13783_rates[i]) {
103 			snd_soc_component_update_bits(component, MC13783_AUDIO_DAC,
104 					0xf << 17, i << 17);
105 			return 0;
106 		}
107 	}
108 
109 	return -EINVAL;
110 }
111 
112 static int mc13783_pcm_hw_params_codec(struct snd_pcm_substream *substream,
113 				struct snd_pcm_hw_params *params,
114 				struct snd_soc_dai *dai)
115 {
116 	struct snd_soc_component *component = dai->component;
117 	unsigned int rate = params_rate(params);
118 	unsigned int val;
119 
120 	switch (rate) {
121 	case 8000:
122 		val = 0;
123 		break;
124 	case 16000:
125 		val = AUDIO_CODEC_CDCFS8K16K;
126 		break;
127 	default:
128 		return -EINVAL;
129 	}
130 
131 	snd_soc_component_update_bits(component, MC13783_AUDIO_CODEC, AUDIO_CODEC_CDCFS8K16K,
132 			val);
133 
134 	return 0;
135 }
136 
137 static int mc13783_pcm_hw_params_sync(struct snd_pcm_substream *substream,
138 				struct snd_pcm_hw_params *params,
139 				struct snd_soc_dai *dai)
140 {
141 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
142 		return mc13783_pcm_hw_params_dac(substream, params, dai);
143 	else
144 		return mc13783_pcm_hw_params_codec(substream, params, dai);
145 }
146 
147 static int mc13783_set_fmt(struct snd_soc_dai *dai, unsigned int fmt,
148 			unsigned int reg)
149 {
150 	struct snd_soc_component *component = dai->component;
151 	unsigned int val = 0;
152 	unsigned int mask = AUDIO_CFS(3) | AUDIO_BCL_INV | AUDIO_CFS_INV |
153 				AUDIO_CSM | AUDIO_C_CLK_EN | AUDIO_C_RESET;
154 
155 
156 	/* DAI mode */
157 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
158 	case SND_SOC_DAIFMT_I2S:
159 		val |= AUDIO_CFS(2);
160 		break;
161 	case SND_SOC_DAIFMT_DSP_A:
162 		val |= AUDIO_CFS(1);
163 		break;
164 	default:
165 		return -EINVAL;
166 	}
167 
168 	/* DAI clock inversion */
169 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
170 	case SND_SOC_DAIFMT_NB_NF:
171 		val |= AUDIO_BCL_INV;
172 		break;
173 	case SND_SOC_DAIFMT_NB_IF:
174 		val |= AUDIO_BCL_INV | AUDIO_CFS_INV;
175 		break;
176 	case SND_SOC_DAIFMT_IB_NF:
177 		break;
178 	case SND_SOC_DAIFMT_IB_IF:
179 		val |= AUDIO_CFS_INV;
180 		break;
181 	}
182 
183 	/* DAI clock master masks */
184 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
185 	case SND_SOC_DAIFMT_CBM_CFM:
186 		val |= AUDIO_C_CLK_EN;
187 		break;
188 	case SND_SOC_DAIFMT_CBS_CFS:
189 		val |= AUDIO_CSM;
190 		break;
191 	case SND_SOC_DAIFMT_CBM_CFS:
192 	case SND_SOC_DAIFMT_CBS_CFM:
193 		return -EINVAL;
194 	}
195 
196 	val |= AUDIO_C_RESET;
197 
198 	snd_soc_component_update_bits(component, reg, mask, val);
199 
200 	return 0;
201 }
202 
203 static int mc13783_set_fmt_async(struct snd_soc_dai *dai, unsigned int fmt)
204 {
205 	if (dai->id == MC13783_ID_STEREO_DAC)
206 		return mc13783_set_fmt(dai, fmt, MC13783_AUDIO_DAC);
207 	else
208 		return mc13783_set_fmt(dai, fmt, MC13783_AUDIO_CODEC);
209 }
210 
211 static int mc13783_set_fmt_sync(struct snd_soc_dai *dai, unsigned int fmt)
212 {
213 	int ret;
214 
215 	ret = mc13783_set_fmt(dai, fmt, MC13783_AUDIO_DAC);
216 	if (ret)
217 		return ret;
218 
219 	/*
220 	 * In synchronous mode force the voice codec into slave mode
221 	 * so that the clock / framesync from the stereo DAC is used
222 	 */
223 	fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
224 	fmt |= SND_SOC_DAIFMT_CBS_CFS;
225 	ret = mc13783_set_fmt(dai, fmt, MC13783_AUDIO_CODEC);
226 
227 	return ret;
228 }
229 
230 static int mc13783_sysclk[] = {
231 	13000000,
232 	15360000,
233 	16800000,
234 	-1,
235 	26000000,
236 	-1, /* 12000000, invalid for voice codec */
237 	-1, /* 3686400, invalid for voice codec */
238 	33600000,
239 };
240 
241 static int mc13783_set_sysclk(struct snd_soc_dai *dai,
242 				  int clk_id, unsigned int freq, int dir,
243 				  unsigned int reg)
244 {
245 	struct snd_soc_component *component = dai->component;
246 	int clk;
247 	unsigned int val = 0;
248 	unsigned int mask = AUDIO_CLK(0x7) | AUDIO_CLK_SEL;
249 
250 	for (clk = 0; clk < ARRAY_SIZE(mc13783_sysclk); clk++) {
251 		if (mc13783_sysclk[clk] < 0)
252 			continue;
253 		if (mc13783_sysclk[clk] == freq)
254 			break;
255 	}
256 
257 	if (clk == ARRAY_SIZE(mc13783_sysclk))
258 		return -EINVAL;
259 
260 	if (clk_id == MC13783_CLK_CLIB)
261 		val |= AUDIO_CLK_SEL;
262 
263 	val |= AUDIO_CLK(clk);
264 
265 	snd_soc_component_update_bits(component, reg, mask, val);
266 
267 	return 0;
268 }
269 
270 static int mc13783_set_sysclk_dac(struct snd_soc_dai *dai,
271 				  int clk_id, unsigned int freq, int dir)
272 {
273 	return mc13783_set_sysclk(dai, clk_id, freq, dir, MC13783_AUDIO_DAC);
274 }
275 
276 static int mc13783_set_sysclk_codec(struct snd_soc_dai *dai,
277 				  int clk_id, unsigned int freq, int dir)
278 {
279 	return mc13783_set_sysclk(dai, clk_id, freq, dir, MC13783_AUDIO_CODEC);
280 }
281 
282 static int mc13783_set_sysclk_sync(struct snd_soc_dai *dai,
283 				  int clk_id, unsigned int freq, int dir)
284 {
285 	int ret;
286 
287 	ret = mc13783_set_sysclk(dai, clk_id, freq, dir, MC13783_AUDIO_DAC);
288 	if (ret)
289 		return ret;
290 
291 	return mc13783_set_sysclk(dai, clk_id, freq, dir, MC13783_AUDIO_CODEC);
292 }
293 
294 static int mc13783_set_tdm_slot_dac(struct snd_soc_dai *dai,
295 	unsigned int tx_mask, unsigned int rx_mask, int slots,
296 	int slot_width)
297 {
298 	struct snd_soc_component *component = dai->component;
299 	unsigned int val = 0;
300 	unsigned int mask = SSI_NETWORK_DAC_SLOT_MASK |
301 				SSI_NETWORK_DAC_RXSLOT_MASK;
302 
303 	switch (slots) {
304 	case 2:
305 		val |= SSI_NETWORK_DAC_SLOTS_2;
306 		break;
307 	case 4:
308 		val |= SSI_NETWORK_DAC_SLOTS_4;
309 		break;
310 	case 8:
311 		val |= SSI_NETWORK_DAC_SLOTS_8;
312 		break;
313 	default:
314 		return -EINVAL;
315 	}
316 
317 	switch (rx_mask) {
318 	case 0x03:
319 		val |= SSI_NETWORK_DAC_RXSLOT_0_1;
320 		break;
321 	case 0x0c:
322 		val |= SSI_NETWORK_DAC_RXSLOT_2_3;
323 		break;
324 	case 0x30:
325 		val |= SSI_NETWORK_DAC_RXSLOT_4_5;
326 		break;
327 	case 0xc0:
328 		val |= SSI_NETWORK_DAC_RXSLOT_6_7;
329 		break;
330 	default:
331 		return -EINVAL;
332 	}
333 
334 	snd_soc_component_update_bits(component, MC13783_SSI_NETWORK, mask, val);
335 
336 	return 0;
337 }
338 
339 static int mc13783_set_tdm_slot_codec(struct snd_soc_dai *dai,
340 	unsigned int tx_mask, unsigned int rx_mask, int slots,
341 	int slot_width)
342 {
343 	struct snd_soc_component *component = dai->component;
344 	unsigned int val = 0;
345 	unsigned int mask = 0x3f;
346 
347 	if (slots != 4)
348 		return -EINVAL;
349 
350 	if (tx_mask != 0x3)
351 		return -EINVAL;
352 
353 	val |= (0x00 << 2);	/* primary timeslot RX/TX(?) is 0 */
354 	val |= (0x01 << 4);	/* secondary timeslot TX is 1 */
355 
356 	snd_soc_component_update_bits(component, MC13783_SSI_NETWORK, mask, val);
357 
358 	return 0;
359 }
360 
361 static int mc13783_set_tdm_slot_sync(struct snd_soc_dai *dai,
362 	unsigned int tx_mask, unsigned int rx_mask, int slots,
363 	int slot_width)
364 {
365 	int ret;
366 
367 	ret = mc13783_set_tdm_slot_dac(dai, tx_mask, rx_mask, slots,
368 			slot_width);
369 	if (ret)
370 		return ret;
371 
372 	ret = mc13783_set_tdm_slot_codec(dai, tx_mask, rx_mask, slots,
373 			slot_width);
374 
375 	return ret;
376 }
377 
378 static const struct snd_kcontrol_new mc1l_amp_ctl =
379 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_TX, 7, 1, 0);
380 
381 static const struct snd_kcontrol_new mc1r_amp_ctl =
382 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_TX, 5, 1, 0);
383 
384 static const struct snd_kcontrol_new mc2_amp_ctl =
385 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_TX, 9, 1, 0);
386 
387 static const struct snd_kcontrol_new atx_amp_ctl =
388 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_TX, 11, 1, 0);
389 
390 
391 /* Virtual mux. The chip does the input selection automatically
392  * as soon as we enable one input. */
393 static const char * const adcl_enum_text[] = {
394 	"MC1L", "RXINL",
395 };
396 
397 static SOC_ENUM_SINGLE_VIRT_DECL(adcl_enum, adcl_enum_text);
398 
399 static const struct snd_kcontrol_new left_input_mux =
400 	SOC_DAPM_ENUM("Route", adcl_enum);
401 
402 static const char * const adcr_enum_text[] = {
403 	"MC1R", "MC2", "RXINR", "TXIN",
404 };
405 
406 static SOC_ENUM_SINGLE_VIRT_DECL(adcr_enum, adcr_enum_text);
407 
408 static const struct snd_kcontrol_new right_input_mux =
409 	SOC_DAPM_ENUM("Route", adcr_enum);
410 
411 static const struct snd_kcontrol_new samp_ctl =
412 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 3, 1, 0);
413 
414 static const char * const speaker_amp_source_text[] = {
415 	"CODEC", "Right"
416 };
417 static SOC_ENUM_SINGLE_DECL(speaker_amp_source, MC13783_AUDIO_RX0, 4,
418 			    speaker_amp_source_text);
419 static const struct snd_kcontrol_new speaker_amp_source_mux =
420 	SOC_DAPM_ENUM("Speaker Amp Source MUX", speaker_amp_source);
421 
422 static const char * const headset_amp_source_text[] = {
423 	"CODEC", "Mixer"
424 };
425 
426 static SOC_ENUM_SINGLE_DECL(headset_amp_source, MC13783_AUDIO_RX0, 11,
427 			    headset_amp_source_text);
428 static const struct snd_kcontrol_new headset_amp_source_mux =
429 	SOC_DAPM_ENUM("Headset Amp Source MUX", headset_amp_source);
430 
431 static const struct snd_kcontrol_new cdcout_ctl =
432 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 18, 1, 0);
433 
434 static const struct snd_kcontrol_new adc_bypass_ctl =
435 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_CODEC, 16, 1, 0);
436 
437 static const struct snd_kcontrol_new lamp_ctl =
438 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 5, 1, 0);
439 
440 static const struct snd_kcontrol_new hlamp_ctl =
441 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 10, 1, 0);
442 
443 static const struct snd_kcontrol_new hramp_ctl =
444 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 9, 1, 0);
445 
446 static const struct snd_kcontrol_new llamp_ctl =
447 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 16, 1, 0);
448 
449 static const struct snd_kcontrol_new lramp_ctl =
450 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 15, 1, 0);
451 
452 static const struct snd_soc_dapm_widget mc13783_dapm_widgets[] = {
453 /* Input */
454 	SND_SOC_DAPM_INPUT("MC1LIN"),
455 	SND_SOC_DAPM_INPUT("MC1RIN"),
456 	SND_SOC_DAPM_INPUT("MC2IN"),
457 	SND_SOC_DAPM_INPUT("RXINR"),
458 	SND_SOC_DAPM_INPUT("RXINL"),
459 	SND_SOC_DAPM_INPUT("TXIN"),
460 
461 	SND_SOC_DAPM_SUPPLY("MC1 Bias", MC13783_AUDIO_TX, 0, 0, NULL, 0),
462 	SND_SOC_DAPM_SUPPLY("MC2 Bias", MC13783_AUDIO_TX, 1, 0, NULL, 0),
463 
464 	SND_SOC_DAPM_SWITCH("MC1L Amp", MC13783_AUDIO_TX, 7, 0, &mc1l_amp_ctl),
465 	SND_SOC_DAPM_SWITCH("MC1R Amp", MC13783_AUDIO_TX, 5, 0, &mc1r_amp_ctl),
466 	SND_SOC_DAPM_SWITCH("MC2 Amp", MC13783_AUDIO_TX, 9, 0, &mc2_amp_ctl),
467 	SND_SOC_DAPM_SWITCH("TXIN Amp", MC13783_AUDIO_TX, 11, 0, &atx_amp_ctl),
468 
469 	SND_SOC_DAPM_MUX("PGA Left Input Mux", SND_SOC_NOPM, 0, 0,
470 			      &left_input_mux),
471 	SND_SOC_DAPM_MUX("PGA Right Input Mux", SND_SOC_NOPM, 0, 0,
472 			      &right_input_mux),
473 
474 	SND_SOC_DAPM_MUX("Speaker Amp Source MUX", SND_SOC_NOPM, 0, 0,
475 			 &speaker_amp_source_mux),
476 
477 	SND_SOC_DAPM_MUX("Headset Amp Source MUX", SND_SOC_NOPM, 0, 0,
478 			 &headset_amp_source_mux),
479 
480 	SND_SOC_DAPM_PGA("PGA Left Input", SND_SOC_NOPM, 0, 0, NULL, 0),
481 	SND_SOC_DAPM_PGA("PGA Right Input", SND_SOC_NOPM, 0, 0, NULL, 0),
482 
483 	SND_SOC_DAPM_ADC("ADC", "Capture", MC13783_AUDIO_CODEC, 11, 0),
484 	SND_SOC_DAPM_SUPPLY("ADC_Reset", MC13783_AUDIO_CODEC, 15, 0, NULL, 0),
485 
486 	SND_SOC_DAPM_PGA("Voice CODEC PGA", MC13783_AUDIO_RX1, 0, 0, NULL, 0),
487 	SND_SOC_DAPM_SWITCH("Voice CODEC Bypass", MC13783_AUDIO_CODEC, 16, 0,
488 			&adc_bypass_ctl),
489 
490 /* Output */
491 	SND_SOC_DAPM_SUPPLY("DAC_E", MC13783_AUDIO_DAC, 11, 0, NULL, 0),
492 	SND_SOC_DAPM_SUPPLY("DAC_Reset", MC13783_AUDIO_DAC, 15, 0, NULL, 0),
493 	SND_SOC_DAPM_OUTPUT("RXOUTL"),
494 	SND_SOC_DAPM_OUTPUT("RXOUTR"),
495 	SND_SOC_DAPM_OUTPUT("HSL"),
496 	SND_SOC_DAPM_OUTPUT("HSR"),
497 	SND_SOC_DAPM_OUTPUT("LSPL"),
498 	SND_SOC_DAPM_OUTPUT("LSP"),
499 	SND_SOC_DAPM_OUTPUT("SP"),
500 	SND_SOC_DAPM_OUTPUT("CDCOUT"),
501 
502 	SND_SOC_DAPM_SWITCH("CDCOUT Switch", MC13783_AUDIO_RX0, 18, 0,
503 			&cdcout_ctl),
504 	SND_SOC_DAPM_SWITCH("Speaker Amp Switch", MC13783_AUDIO_RX0, 3, 0,
505 			&samp_ctl),
506 	SND_SOC_DAPM_SWITCH("Loudspeaker Amp", SND_SOC_NOPM, 0, 0, &lamp_ctl),
507 	SND_SOC_DAPM_SWITCH("Headset Amp Left", MC13783_AUDIO_RX0, 10, 0,
508 			&hlamp_ctl),
509 	SND_SOC_DAPM_SWITCH("Headset Amp Right", MC13783_AUDIO_RX0, 9, 0,
510 			&hramp_ctl),
511 	SND_SOC_DAPM_SWITCH("Line out Amp Left", MC13783_AUDIO_RX0, 16, 0,
512 			&llamp_ctl),
513 	SND_SOC_DAPM_SWITCH("Line out Amp Right", MC13783_AUDIO_RX0, 15, 0,
514 			&lramp_ctl),
515 	SND_SOC_DAPM_DAC("DAC", "Playback", MC13783_AUDIO_RX0, 22, 0),
516 	SND_SOC_DAPM_PGA("DAC PGA", MC13783_AUDIO_RX1, 5, 0, NULL, 0),
517 };
518 
519 static struct snd_soc_dapm_route mc13783_routes[] = {
520 /* Input */
521 	{ "MC1L Amp", NULL, "MC1LIN"},
522 	{ "MC1R Amp", NULL, "MC1RIN" },
523 	{ "MC2 Amp", NULL, "MC2IN" },
524 	{ "TXIN Amp", NULL, "TXIN"},
525 
526 	{ "PGA Left Input Mux", "MC1L", "MC1L Amp" },
527 	{ "PGA Left Input Mux", "RXINL", "RXINL"},
528 	{ "PGA Right Input Mux", "MC1R", "MC1R Amp" },
529 	{ "PGA Right Input Mux", "MC2",  "MC2 Amp"},
530 	{ "PGA Right Input Mux", "TXIN", "TXIN Amp"},
531 	{ "PGA Right Input Mux", "RXINR", "RXINR"},
532 
533 	{ "PGA Left Input", NULL, "PGA Left Input Mux"},
534 	{ "PGA Right Input", NULL, "PGA Right Input Mux"},
535 
536 	{ "ADC", NULL, "PGA Left Input"},
537 	{ "ADC", NULL, "PGA Right Input"},
538 	{ "ADC", NULL, "ADC_Reset"},
539 
540 	{ "Voice CODEC PGA", "Voice CODEC Bypass", "ADC" },
541 
542 	{ "Speaker Amp Source MUX", "CODEC", "Voice CODEC PGA"},
543 	{ "Speaker Amp Source MUX", "Right", "DAC PGA"},
544 
545 	{ "Headset Amp Source MUX", "CODEC", "Voice CODEC PGA"},
546 	{ "Headset Amp Source MUX", "Mixer", "DAC PGA"},
547 
548 /* Output */
549 	{ "HSL", NULL, "Headset Amp Left" },
550 	{ "HSR", NULL, "Headset Amp Right"},
551 	{ "RXOUTL", NULL, "Line out Amp Left"},
552 	{ "RXOUTR", NULL, "Line out Amp Right"},
553 	{ "SP", "Speaker Amp Switch", "Speaker Amp Source MUX"},
554 	{ "LSP", "Loudspeaker Amp", "Speaker Amp Source MUX"},
555 	{ "HSL", "Headset Amp Left", "Headset Amp Source MUX"},
556 	{ "HSR", "Headset Amp Right", "Headset Amp Source MUX"},
557 	{ "Line out Amp Left", NULL, "DAC PGA"},
558 	{ "Line out Amp Right", NULL, "DAC PGA"},
559 	{ "DAC PGA", NULL, "DAC"},
560 	{ "DAC", NULL, "DAC_E"},
561 	{ "CDCOUT", "CDCOUT Switch", "Voice CODEC PGA"},
562 };
563 
564 static const char * const mc13783_3d_mixer[] = {"Stereo", "Phase Mix",
565 						"Mono", "Mono Mix"};
566 
567 static SOC_ENUM_SINGLE_DECL(mc13783_enum_3d_mixer,
568 			    MC13783_AUDIO_RX1, 16,
569 			    mc13783_3d_mixer);
570 
571 static struct snd_kcontrol_new mc13783_control_list[] = {
572 	SOC_SINGLE("Loudspeaker enable", MC13783_AUDIO_RX0, 5, 1, 0),
573 	SOC_SINGLE("PCM Playback Volume", MC13783_AUDIO_RX1, 6, 15, 0),
574 	SOC_SINGLE("PCM Playback Switch", MC13783_AUDIO_RX1, 5, 1, 0),
575 	SOC_DOUBLE("PCM Capture Volume", MC13783_AUDIO_TX, 19, 14, 31, 0),
576 	SOC_ENUM("3D Control", mc13783_enum_3d_mixer),
577 
578 	SOC_SINGLE("CDCOUT Switch", MC13783_AUDIO_RX0, 18, 1, 0),
579 	SOC_SINGLE("Earpiece Amp Switch", MC13783_AUDIO_RX0, 3, 1, 0),
580 	SOC_DOUBLE("Headset Amp Switch", MC13783_AUDIO_RX0, 10, 9, 1, 0),
581 	SOC_DOUBLE("Line out Amp Switch", MC13783_AUDIO_RX0, 16, 15, 1, 0),
582 
583 	SOC_SINGLE("PCM Capture Mixin Switch", MC13783_AUDIO_RX0, 22, 1, 0),
584 	SOC_SINGLE("Line in Capture Mixin Switch", MC13783_AUDIO_RX0, 23, 1, 0),
585 
586 	SOC_SINGLE("CODEC Capture Volume", MC13783_AUDIO_RX1, 1, 15, 0),
587 	SOC_SINGLE("CODEC Capture Mixin Switch", MC13783_AUDIO_RX0, 21, 1, 0),
588 
589 	SOC_SINGLE("Line in Capture Volume", MC13783_AUDIO_RX1, 12, 15, 0),
590 	SOC_SINGLE("Line in Capture Switch", MC13783_AUDIO_RX1, 10, 1, 0),
591 
592 	SOC_SINGLE("MC1 Capture Bias Switch", MC13783_AUDIO_TX, 0, 1, 0),
593 	SOC_SINGLE("MC2 Capture Bias Switch", MC13783_AUDIO_TX, 1, 1, 0),
594 };
595 
596 static int mc13783_probe(struct snd_soc_component *component)
597 {
598 	struct mc13783_priv *priv = snd_soc_component_get_drvdata(component);
599 
600 	snd_soc_component_init_regmap(component,
601 				  dev_get_regmap(component->dev->parent, NULL));
602 
603 	/* these are the reset values */
604 	mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_RX0, 0x25893);
605 	mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_RX1, 0x00d35A);
606 	mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_TX, 0x420000);
607 	mc13xxx_reg_write(priv->mc13xxx, MC13783_SSI_NETWORK, 0x013060);
608 	mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_CODEC, 0x180027);
609 	mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_DAC, 0x0e0004);
610 
611 	if (priv->adc_ssi_port == MC13783_SSI1_PORT)
612 		mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_CODEC,
613 				AUDIO_SSI_SEL, 0);
614 	else
615 		mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_CODEC,
616 				AUDIO_SSI_SEL, AUDIO_SSI_SEL);
617 
618 	if (priv->dac_ssi_port == MC13783_SSI1_PORT)
619 		mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_DAC,
620 				AUDIO_SSI_SEL, 0);
621 	else
622 		mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_DAC,
623 				AUDIO_SSI_SEL, AUDIO_SSI_SEL);
624 
625 	return 0;
626 }
627 
628 static void mc13783_remove(struct snd_soc_component *component)
629 {
630 	struct mc13783_priv *priv = snd_soc_component_get_drvdata(component);
631 
632 	/* Make sure VAUDIOON is off */
633 	mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_RX0, 0x3, 0);
634 }
635 
636 #define MC13783_RATES_RECORD (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000)
637 
638 #define MC13783_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
639 	SNDRV_PCM_FMTBIT_S24_LE)
640 
641 static const struct snd_soc_dai_ops mc13783_ops_dac = {
642 	.hw_params	= mc13783_pcm_hw_params_dac,
643 	.set_fmt	= mc13783_set_fmt_async,
644 	.set_sysclk	= mc13783_set_sysclk_dac,
645 	.set_tdm_slot	= mc13783_set_tdm_slot_dac,
646 };
647 
648 static const struct snd_soc_dai_ops mc13783_ops_codec = {
649 	.hw_params	= mc13783_pcm_hw_params_codec,
650 	.set_fmt	= mc13783_set_fmt_async,
651 	.set_sysclk	= mc13783_set_sysclk_codec,
652 	.set_tdm_slot	= mc13783_set_tdm_slot_codec,
653 };
654 
655 /*
656  * The mc13783 has two SSI ports, both of them can be routed either
657  * to the voice codec or the stereo DAC. When two different SSI ports
658  * are used for the voice codec and the stereo DAC we can do different
659  * formats and sysclock settings for playback and capture
660  * (mc13783-hifi-playback and mc13783-hifi-capture). Using the same port
661  * forces us to use symmetric rates (mc13783-hifi).
662  */
663 static struct snd_soc_dai_driver mc13783_dai_async[] = {
664 	{
665 		.name = "mc13783-hifi-playback",
666 		.id = MC13783_ID_STEREO_DAC,
667 		.playback = {
668 			.stream_name = "Playback",
669 			.channels_min = 2,
670 			.channels_max = 2,
671 			.rates = SNDRV_PCM_RATE_8000_96000,
672 			.formats = MC13783_FORMATS,
673 		},
674 		.ops = &mc13783_ops_dac,
675 	}, {
676 		.name = "mc13783-hifi-capture",
677 		.id = MC13783_ID_STEREO_CODEC,
678 		.capture = {
679 			.stream_name = "Capture",
680 			.channels_min = 2,
681 			.channels_max = 2,
682 			.rates = MC13783_RATES_RECORD,
683 			.formats = MC13783_FORMATS,
684 		},
685 		.ops = &mc13783_ops_codec,
686 	},
687 };
688 
689 static const struct snd_soc_dai_ops mc13783_ops_sync = {
690 	.hw_params	= mc13783_pcm_hw_params_sync,
691 	.set_fmt	= mc13783_set_fmt_sync,
692 	.set_sysclk	= mc13783_set_sysclk_sync,
693 	.set_tdm_slot	= mc13783_set_tdm_slot_sync,
694 };
695 
696 static struct snd_soc_dai_driver mc13783_dai_sync[] = {
697 	{
698 		.name = "mc13783-hifi",
699 		.id = MC13783_ID_SYNC,
700 		.playback = {
701 			.stream_name = "Playback",
702 			.channels_min = 2,
703 			.channels_max = 2,
704 			.rates = SNDRV_PCM_RATE_8000_96000,
705 			.formats = MC13783_FORMATS,
706 		},
707 		.capture = {
708 			.stream_name = "Capture",
709 			.channels_min = 2,
710 			.channels_max = 2,
711 			.rates = MC13783_RATES_RECORD,
712 			.formats = MC13783_FORMATS,
713 		},
714 		.ops = &mc13783_ops_sync,
715 		.symmetric_rate = 1,
716 	}
717 };
718 
719 static const struct snd_soc_component_driver soc_component_dev_mc13783 = {
720 	.probe			= mc13783_probe,
721 	.remove			= mc13783_remove,
722 	.controls		= mc13783_control_list,
723 	.num_controls		= ARRAY_SIZE(mc13783_control_list),
724 	.dapm_widgets		= mc13783_dapm_widgets,
725 	.num_dapm_widgets	= ARRAY_SIZE(mc13783_dapm_widgets),
726 	.dapm_routes		= mc13783_routes,
727 	.num_dapm_routes	= ARRAY_SIZE(mc13783_routes),
728 	.idle_bias_on		= 1,
729 	.use_pmdown_time	= 1,
730 	.endianness		= 1,
731 	.non_legacy_dai_naming	= 1,
732 };
733 
734 static int __init mc13783_codec_probe(struct platform_device *pdev)
735 {
736 	struct mc13783_priv *priv;
737 	struct mc13xxx_codec_platform_data *pdata = pdev->dev.platform_data;
738 	struct device_node *np;
739 	int ret;
740 
741 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
742 	if (!priv)
743 		return -ENOMEM;
744 
745 	if (pdata) {
746 		priv->adc_ssi_port = pdata->adc_ssi_port;
747 		priv->dac_ssi_port = pdata->dac_ssi_port;
748 	} else {
749 		np = of_get_child_by_name(pdev->dev.parent->of_node, "codec");
750 		if (!np)
751 			return -ENOSYS;
752 
753 		ret = of_property_read_u32(np, "adc-port", &priv->adc_ssi_port);
754 		if (ret) {
755 			of_node_put(np);
756 			return ret;
757 		}
758 
759 		ret = of_property_read_u32(np, "dac-port", &priv->dac_ssi_port);
760 		if (ret) {
761 			of_node_put(np);
762 			return ret;
763 		}
764 
765 		of_node_put(np);
766 	}
767 
768 	dev_set_drvdata(&pdev->dev, priv);
769 	priv->mc13xxx = dev_get_drvdata(pdev->dev.parent);
770 
771 	if (priv->adc_ssi_port == priv->dac_ssi_port)
772 		ret = devm_snd_soc_register_component(&pdev->dev, &soc_component_dev_mc13783,
773 			mc13783_dai_sync, ARRAY_SIZE(mc13783_dai_sync));
774 	else
775 		ret = devm_snd_soc_register_component(&pdev->dev, &soc_component_dev_mc13783,
776 			mc13783_dai_async, ARRAY_SIZE(mc13783_dai_async));
777 
778 	return ret;
779 }
780 
781 static int mc13783_codec_remove(struct platform_device *pdev)
782 {
783 	return 0;
784 }
785 
786 static struct platform_driver mc13783_codec_driver = {
787 	.driver = {
788 		.name	= "mc13783-codec",
789 	},
790 	.remove = mc13783_codec_remove,
791 };
792 module_platform_driver_probe(mc13783_codec_driver, mc13783_codec_probe);
793 
794 MODULE_DESCRIPTION("ASoC MC13783 driver");
795 MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
796 MODULE_AUTHOR("Philippe Retornaz <philippe.retornaz@epfl.ch>");
797 MODULE_LICENSE("GPL");
798