xref: /openbmc/linux/sound/soc/codecs/mc13783.c (revision 140212ae)
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 
mc13783_pcm_hw_params_dac(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)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 
mc13783_pcm_hw_params_codec(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)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 
mc13783_pcm_hw_params_sync(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)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 
mc13783_set_fmt(struct snd_soc_dai * dai,unsigned int fmt,unsigned int reg)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_CLOCK_PROVIDER_MASK) {
185 	case SND_SOC_DAIFMT_CBP_CFP:
186 		val |= AUDIO_C_CLK_EN;
187 		break;
188 	case SND_SOC_DAIFMT_CBC_CFC:
189 		val |= AUDIO_CSM;
190 		break;
191 	default:
192 		return -EINVAL;
193 	}
194 
195 	val |= AUDIO_C_RESET;
196 
197 	snd_soc_component_update_bits(component, reg, mask, val);
198 
199 	return 0;
200 }
201 
mc13783_set_fmt_async(struct snd_soc_dai * dai,unsigned int fmt)202 static int mc13783_set_fmt_async(struct snd_soc_dai *dai, unsigned int fmt)
203 {
204 	if (dai->id == MC13783_ID_STEREO_DAC)
205 		return mc13783_set_fmt(dai, fmt, MC13783_AUDIO_DAC);
206 	else
207 		return mc13783_set_fmt(dai, fmt, MC13783_AUDIO_CODEC);
208 }
209 
mc13783_set_fmt_sync(struct snd_soc_dai * dai,unsigned int fmt)210 static int mc13783_set_fmt_sync(struct snd_soc_dai *dai, unsigned int fmt)
211 {
212 	int ret;
213 
214 	ret = mc13783_set_fmt(dai, fmt, MC13783_AUDIO_DAC);
215 	if (ret)
216 		return ret;
217 
218 	/*
219 	 * In synchronous mode force the voice codec into consumer mode
220 	 * so that the clock / framesync from the stereo DAC is used
221 	 */
222 	fmt &= ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
223 	fmt |= SND_SOC_DAIFMT_CBC_CFC;
224 	ret = mc13783_set_fmt(dai, fmt, MC13783_AUDIO_CODEC);
225 
226 	return ret;
227 }
228 
229 static int mc13783_sysclk[] = {
230 	13000000,
231 	15360000,
232 	16800000,
233 	-1,
234 	26000000,
235 	-1, /* 12000000, invalid for voice codec */
236 	-1, /* 3686400, invalid for voice codec */
237 	33600000,
238 };
239 
mc13783_set_sysclk(struct snd_soc_dai * dai,int clk_id,unsigned int freq,int dir,unsigned int reg)240 static int mc13783_set_sysclk(struct snd_soc_dai *dai,
241 				  int clk_id, unsigned int freq, int dir,
242 				  unsigned int reg)
243 {
244 	struct snd_soc_component *component = dai->component;
245 	int clk;
246 	unsigned int val = 0;
247 	unsigned int mask = AUDIO_CLK(0x7) | AUDIO_CLK_SEL;
248 
249 	for (clk = 0; clk < ARRAY_SIZE(mc13783_sysclk); clk++) {
250 		if (mc13783_sysclk[clk] < 0)
251 			continue;
252 		if (mc13783_sysclk[clk] == freq)
253 			break;
254 	}
255 
256 	if (clk == ARRAY_SIZE(mc13783_sysclk))
257 		return -EINVAL;
258 
259 	if (clk_id == MC13783_CLK_CLIB)
260 		val |= AUDIO_CLK_SEL;
261 
262 	val |= AUDIO_CLK(clk);
263 
264 	snd_soc_component_update_bits(component, reg, mask, val);
265 
266 	return 0;
267 }
268 
mc13783_set_sysclk_dac(struct snd_soc_dai * dai,int clk_id,unsigned int freq,int dir)269 static int mc13783_set_sysclk_dac(struct snd_soc_dai *dai,
270 				  int clk_id, unsigned int freq, int dir)
271 {
272 	return mc13783_set_sysclk(dai, clk_id, freq, dir, MC13783_AUDIO_DAC);
273 }
274 
mc13783_set_sysclk_codec(struct snd_soc_dai * dai,int clk_id,unsigned int freq,int dir)275 static int mc13783_set_sysclk_codec(struct snd_soc_dai *dai,
276 				  int clk_id, unsigned int freq, int dir)
277 {
278 	return mc13783_set_sysclk(dai, clk_id, freq, dir, MC13783_AUDIO_CODEC);
279 }
280 
mc13783_set_sysclk_sync(struct snd_soc_dai * dai,int clk_id,unsigned int freq,int dir)281 static int mc13783_set_sysclk_sync(struct snd_soc_dai *dai,
282 				  int clk_id, unsigned int freq, int dir)
283 {
284 	int ret;
285 
286 	ret = mc13783_set_sysclk(dai, clk_id, freq, dir, MC13783_AUDIO_DAC);
287 	if (ret)
288 		return ret;
289 
290 	return mc13783_set_sysclk(dai, clk_id, freq, dir, MC13783_AUDIO_CODEC);
291 }
292 
mc13783_set_tdm_slot_dac(struct snd_soc_dai * dai,unsigned int tx_mask,unsigned int rx_mask,int slots,int slot_width)293 static int mc13783_set_tdm_slot_dac(struct snd_soc_dai *dai,
294 	unsigned int tx_mask, unsigned int rx_mask, int slots,
295 	int slot_width)
296 {
297 	struct snd_soc_component *component = dai->component;
298 	unsigned int val = 0;
299 	unsigned int mask = SSI_NETWORK_DAC_SLOT_MASK |
300 				SSI_NETWORK_DAC_RXSLOT_MASK;
301 
302 	switch (slots) {
303 	case 2:
304 		val |= SSI_NETWORK_DAC_SLOTS_2;
305 		break;
306 	case 4:
307 		val |= SSI_NETWORK_DAC_SLOTS_4;
308 		break;
309 	case 8:
310 		val |= SSI_NETWORK_DAC_SLOTS_8;
311 		break;
312 	default:
313 		return -EINVAL;
314 	}
315 
316 	switch (rx_mask) {
317 	case 0x03:
318 		val |= SSI_NETWORK_DAC_RXSLOT_0_1;
319 		break;
320 	case 0x0c:
321 		val |= SSI_NETWORK_DAC_RXSLOT_2_3;
322 		break;
323 	case 0x30:
324 		val |= SSI_NETWORK_DAC_RXSLOT_4_5;
325 		break;
326 	case 0xc0:
327 		val |= SSI_NETWORK_DAC_RXSLOT_6_7;
328 		break;
329 	default:
330 		return -EINVAL;
331 	}
332 
333 	snd_soc_component_update_bits(component, MC13783_SSI_NETWORK, mask, val);
334 
335 	return 0;
336 }
337 
mc13783_set_tdm_slot_codec(struct snd_soc_dai * dai,unsigned int tx_mask,unsigned int rx_mask,int slots,int slot_width)338 static int mc13783_set_tdm_slot_codec(struct snd_soc_dai *dai,
339 	unsigned int tx_mask, unsigned int rx_mask, int slots,
340 	int slot_width)
341 {
342 	struct snd_soc_component *component = dai->component;
343 	unsigned int val = 0;
344 	unsigned int mask = 0x3f;
345 
346 	if (slots != 4)
347 		return -EINVAL;
348 
349 	if (tx_mask != 0x3)
350 		return -EINVAL;
351 
352 	val |= (0x00 << 2);	/* primary timeslot RX/TX(?) is 0 */
353 	val |= (0x01 << 4);	/* secondary timeslot TX is 1 */
354 
355 	snd_soc_component_update_bits(component, MC13783_SSI_NETWORK, mask, val);
356 
357 	return 0;
358 }
359 
mc13783_set_tdm_slot_sync(struct snd_soc_dai * dai,unsigned int tx_mask,unsigned int rx_mask,int slots,int slot_width)360 static int mc13783_set_tdm_slot_sync(struct snd_soc_dai *dai,
361 	unsigned int tx_mask, unsigned int rx_mask, int slots,
362 	int slot_width)
363 {
364 	int ret;
365 
366 	ret = mc13783_set_tdm_slot_dac(dai, tx_mask, rx_mask, slots,
367 			slot_width);
368 	if (ret)
369 		return ret;
370 
371 	ret = mc13783_set_tdm_slot_codec(dai, tx_mask, rx_mask, slots,
372 			slot_width);
373 
374 	return ret;
375 }
376 
377 static const struct snd_kcontrol_new mc1l_amp_ctl =
378 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_TX, 7, 1, 0);
379 
380 static const struct snd_kcontrol_new mc1r_amp_ctl =
381 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_TX, 5, 1, 0);
382 
383 static const struct snd_kcontrol_new mc2_amp_ctl =
384 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_TX, 9, 1, 0);
385 
386 static const struct snd_kcontrol_new atx_amp_ctl =
387 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_TX, 11, 1, 0);
388 
389 
390 /* Virtual mux. The chip does the input selection automatically
391  * as soon as we enable one input. */
392 static const char * const adcl_enum_text[] = {
393 	"MC1L", "RXINL",
394 };
395 
396 static SOC_ENUM_SINGLE_VIRT_DECL(adcl_enum, adcl_enum_text);
397 
398 static const struct snd_kcontrol_new left_input_mux =
399 	SOC_DAPM_ENUM("Route", adcl_enum);
400 
401 static const char * const adcr_enum_text[] = {
402 	"MC1R", "MC2", "RXINR", "TXIN",
403 };
404 
405 static SOC_ENUM_SINGLE_VIRT_DECL(adcr_enum, adcr_enum_text);
406 
407 static const struct snd_kcontrol_new right_input_mux =
408 	SOC_DAPM_ENUM("Route", adcr_enum);
409 
410 static const struct snd_kcontrol_new samp_ctl =
411 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 3, 1, 0);
412 
413 static const char * const speaker_amp_source_text[] = {
414 	"CODEC", "Right"
415 };
416 static SOC_ENUM_SINGLE_DECL(speaker_amp_source, MC13783_AUDIO_RX0, 4,
417 			    speaker_amp_source_text);
418 static const struct snd_kcontrol_new speaker_amp_source_mux =
419 	SOC_DAPM_ENUM("Speaker Amp Source MUX", speaker_amp_source);
420 
421 static const char * const headset_amp_source_text[] = {
422 	"CODEC", "Mixer"
423 };
424 
425 static SOC_ENUM_SINGLE_DECL(headset_amp_source, MC13783_AUDIO_RX0, 11,
426 			    headset_amp_source_text);
427 static const struct snd_kcontrol_new headset_amp_source_mux =
428 	SOC_DAPM_ENUM("Headset Amp Source MUX", headset_amp_source);
429 
430 static const struct snd_kcontrol_new cdcout_ctl =
431 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 18, 1, 0);
432 
433 static const struct snd_kcontrol_new adc_bypass_ctl =
434 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_CODEC, 16, 1, 0);
435 
436 static const struct snd_kcontrol_new lamp_ctl =
437 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 5, 1, 0);
438 
439 static const struct snd_kcontrol_new hlamp_ctl =
440 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 10, 1, 0);
441 
442 static const struct snd_kcontrol_new hramp_ctl =
443 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 9, 1, 0);
444 
445 static const struct snd_kcontrol_new llamp_ctl =
446 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 16, 1, 0);
447 
448 static const struct snd_kcontrol_new lramp_ctl =
449 	SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 15, 1, 0);
450 
451 static const struct snd_soc_dapm_widget mc13783_dapm_widgets[] = {
452 /* Input */
453 	SND_SOC_DAPM_INPUT("MC1LIN"),
454 	SND_SOC_DAPM_INPUT("MC1RIN"),
455 	SND_SOC_DAPM_INPUT("MC2IN"),
456 	SND_SOC_DAPM_INPUT("RXINR"),
457 	SND_SOC_DAPM_INPUT("RXINL"),
458 	SND_SOC_DAPM_INPUT("TXIN"),
459 
460 	SND_SOC_DAPM_SUPPLY("MC1 Bias", MC13783_AUDIO_TX, 0, 0, NULL, 0),
461 	SND_SOC_DAPM_SUPPLY("MC2 Bias", MC13783_AUDIO_TX, 1, 0, NULL, 0),
462 
463 	SND_SOC_DAPM_SWITCH("MC1L Amp", MC13783_AUDIO_TX, 7, 0, &mc1l_amp_ctl),
464 	SND_SOC_DAPM_SWITCH("MC1R Amp", MC13783_AUDIO_TX, 5, 0, &mc1r_amp_ctl),
465 	SND_SOC_DAPM_SWITCH("MC2 Amp", MC13783_AUDIO_TX, 9, 0, &mc2_amp_ctl),
466 	SND_SOC_DAPM_SWITCH("TXIN Amp", MC13783_AUDIO_TX, 11, 0, &atx_amp_ctl),
467 
468 	SND_SOC_DAPM_MUX("PGA Left Input Mux", SND_SOC_NOPM, 0, 0,
469 			      &left_input_mux),
470 	SND_SOC_DAPM_MUX("PGA Right Input Mux", SND_SOC_NOPM, 0, 0,
471 			      &right_input_mux),
472 
473 	SND_SOC_DAPM_MUX("Speaker Amp Source MUX", SND_SOC_NOPM, 0, 0,
474 			 &speaker_amp_source_mux),
475 
476 	SND_SOC_DAPM_MUX("Headset Amp Source MUX", SND_SOC_NOPM, 0, 0,
477 			 &headset_amp_source_mux),
478 
479 	SND_SOC_DAPM_PGA("PGA Left Input", SND_SOC_NOPM, 0, 0, NULL, 0),
480 	SND_SOC_DAPM_PGA("PGA Right Input", SND_SOC_NOPM, 0, 0, NULL, 0),
481 
482 	SND_SOC_DAPM_ADC("ADC", "Capture", MC13783_AUDIO_CODEC, 11, 0),
483 	SND_SOC_DAPM_SUPPLY("ADC_Reset", MC13783_AUDIO_CODEC, 15, 0, NULL, 0),
484 
485 	SND_SOC_DAPM_PGA("Voice CODEC PGA", MC13783_AUDIO_RX1, 0, 0, NULL, 0),
486 	SND_SOC_DAPM_SWITCH("Voice CODEC Bypass", MC13783_AUDIO_CODEC, 16, 0,
487 			&adc_bypass_ctl),
488 
489 /* Output */
490 	SND_SOC_DAPM_SUPPLY("DAC_E", MC13783_AUDIO_DAC, 11, 0, NULL, 0),
491 	SND_SOC_DAPM_SUPPLY("DAC_Reset", MC13783_AUDIO_DAC, 15, 0, NULL, 0),
492 	SND_SOC_DAPM_OUTPUT("RXOUTL"),
493 	SND_SOC_DAPM_OUTPUT("RXOUTR"),
494 	SND_SOC_DAPM_OUTPUT("HSL"),
495 	SND_SOC_DAPM_OUTPUT("HSR"),
496 	SND_SOC_DAPM_OUTPUT("LSPL"),
497 	SND_SOC_DAPM_OUTPUT("LSP"),
498 	SND_SOC_DAPM_OUTPUT("SP"),
499 	SND_SOC_DAPM_OUTPUT("CDCOUT"),
500 
501 	SND_SOC_DAPM_SWITCH("CDCOUT Switch", MC13783_AUDIO_RX0, 18, 0,
502 			&cdcout_ctl),
503 	SND_SOC_DAPM_SWITCH("Speaker Amp Switch", MC13783_AUDIO_RX0, 3, 0,
504 			&samp_ctl),
505 	SND_SOC_DAPM_SWITCH("Loudspeaker Amp", SND_SOC_NOPM, 0, 0, &lamp_ctl),
506 	SND_SOC_DAPM_SWITCH("Headset Amp Left", MC13783_AUDIO_RX0, 10, 0,
507 			&hlamp_ctl),
508 	SND_SOC_DAPM_SWITCH("Headset Amp Right", MC13783_AUDIO_RX0, 9, 0,
509 			&hramp_ctl),
510 	SND_SOC_DAPM_SWITCH("Line out Amp Left", MC13783_AUDIO_RX0, 16, 0,
511 			&llamp_ctl),
512 	SND_SOC_DAPM_SWITCH("Line out Amp Right", MC13783_AUDIO_RX0, 15, 0,
513 			&lramp_ctl),
514 	SND_SOC_DAPM_DAC("DAC", "Playback", MC13783_AUDIO_RX0, 22, 0),
515 	SND_SOC_DAPM_PGA("DAC PGA", MC13783_AUDIO_RX1, 5, 0, NULL, 0),
516 };
517 
518 static struct snd_soc_dapm_route mc13783_routes[] = {
519 /* Input */
520 	{ "MC1L Amp", NULL, "MC1LIN"},
521 	{ "MC1R Amp", NULL, "MC1RIN" },
522 	{ "MC2 Amp", NULL, "MC2IN" },
523 	{ "TXIN Amp", NULL, "TXIN"},
524 
525 	{ "PGA Left Input Mux", "MC1L", "MC1L Amp" },
526 	{ "PGA Left Input Mux", "RXINL", "RXINL"},
527 	{ "PGA Right Input Mux", "MC1R", "MC1R Amp" },
528 	{ "PGA Right Input Mux", "MC2",  "MC2 Amp"},
529 	{ "PGA Right Input Mux", "TXIN", "TXIN Amp"},
530 	{ "PGA Right Input Mux", "RXINR", "RXINR"},
531 
532 	{ "PGA Left Input", NULL, "PGA Left Input Mux"},
533 	{ "PGA Right Input", NULL, "PGA Right Input Mux"},
534 
535 	{ "ADC", NULL, "PGA Left Input"},
536 	{ "ADC", NULL, "PGA Right Input"},
537 	{ "ADC", NULL, "ADC_Reset"},
538 
539 	{ "Voice CODEC PGA", "Voice CODEC Bypass", "ADC" },
540 
541 	{ "Speaker Amp Source MUX", "CODEC", "Voice CODEC PGA"},
542 	{ "Speaker Amp Source MUX", "Right", "DAC PGA"},
543 
544 	{ "Headset Amp Source MUX", "CODEC", "Voice CODEC PGA"},
545 	{ "Headset Amp Source MUX", "Mixer", "DAC PGA"},
546 
547 /* Output */
548 	{ "HSL", NULL, "Headset Amp Left" },
549 	{ "HSR", NULL, "Headset Amp Right"},
550 	{ "RXOUTL", NULL, "Line out Amp Left"},
551 	{ "RXOUTR", NULL, "Line out Amp Right"},
552 	{ "SP", "Speaker Amp Switch", "Speaker Amp Source MUX"},
553 	{ "LSP", "Loudspeaker Amp", "Speaker Amp Source MUX"},
554 	{ "HSL", "Headset Amp Left", "Headset Amp Source MUX"},
555 	{ "HSR", "Headset Amp Right", "Headset Amp Source MUX"},
556 	{ "Line out Amp Left", NULL, "DAC PGA"},
557 	{ "Line out Amp Right", NULL, "DAC PGA"},
558 	{ "DAC PGA", NULL, "DAC"},
559 	{ "DAC", NULL, "DAC_E"},
560 	{ "CDCOUT", "CDCOUT Switch", "Voice CODEC PGA"},
561 };
562 
563 static const char * const mc13783_3d_mixer[] = {"Stereo", "Phase Mix",
564 						"Mono", "Mono Mix"};
565 
566 static SOC_ENUM_SINGLE_DECL(mc13783_enum_3d_mixer,
567 			    MC13783_AUDIO_RX1, 16,
568 			    mc13783_3d_mixer);
569 
570 static struct snd_kcontrol_new mc13783_control_list[] = {
571 	SOC_SINGLE("Loudspeaker enable", MC13783_AUDIO_RX0, 5, 1, 0),
572 	SOC_SINGLE("PCM Playback Volume", MC13783_AUDIO_RX1, 6, 15, 0),
573 	SOC_SINGLE("PCM Playback Switch", MC13783_AUDIO_RX1, 5, 1, 0),
574 	SOC_DOUBLE("PCM Capture Volume", MC13783_AUDIO_TX, 19, 14, 31, 0),
575 	SOC_ENUM("3D Control", mc13783_enum_3d_mixer),
576 
577 	SOC_SINGLE("CDCOUT Switch", MC13783_AUDIO_RX0, 18, 1, 0),
578 	SOC_SINGLE("Earpiece Amp Switch", MC13783_AUDIO_RX0, 3, 1, 0),
579 	SOC_DOUBLE("Headset Amp Switch", MC13783_AUDIO_RX0, 10, 9, 1, 0),
580 	SOC_DOUBLE("Line out Amp Switch", MC13783_AUDIO_RX0, 16, 15, 1, 0),
581 
582 	SOC_SINGLE("PCM Capture Mixin Switch", MC13783_AUDIO_RX0, 22, 1, 0),
583 	SOC_SINGLE("Line in Capture Mixin Switch", MC13783_AUDIO_RX0, 23, 1, 0),
584 
585 	SOC_SINGLE("CODEC Capture Volume", MC13783_AUDIO_RX1, 1, 15, 0),
586 	SOC_SINGLE("CODEC Capture Mixin Switch", MC13783_AUDIO_RX0, 21, 1, 0),
587 
588 	SOC_SINGLE("Line in Capture Volume", MC13783_AUDIO_RX1, 12, 15, 0),
589 	SOC_SINGLE("Line in Capture Switch", MC13783_AUDIO_RX1, 10, 1, 0),
590 
591 	SOC_SINGLE("MC1 Capture Bias Switch", MC13783_AUDIO_TX, 0, 1, 0),
592 	SOC_SINGLE("MC2 Capture Bias Switch", MC13783_AUDIO_TX, 1, 1, 0),
593 };
594 
mc13783_probe(struct snd_soc_component * component)595 static int mc13783_probe(struct snd_soc_component *component)
596 {
597 	struct mc13783_priv *priv = snd_soc_component_get_drvdata(component);
598 
599 	snd_soc_component_init_regmap(component,
600 				  dev_get_regmap(component->dev->parent, NULL));
601 
602 	/* these are the reset values */
603 	mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_RX0, 0x25893);
604 	mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_RX1, 0x00d35A);
605 	mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_TX, 0x420000);
606 	mc13xxx_reg_write(priv->mc13xxx, MC13783_SSI_NETWORK, 0x013060);
607 	mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_CODEC, 0x180027);
608 	mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_DAC, 0x0e0004);
609 
610 	if (priv->adc_ssi_port == MC13783_SSI1_PORT)
611 		mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_CODEC,
612 				AUDIO_SSI_SEL, 0);
613 	else
614 		mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_CODEC,
615 				AUDIO_SSI_SEL, AUDIO_SSI_SEL);
616 
617 	if (priv->dac_ssi_port == MC13783_SSI1_PORT)
618 		mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_DAC,
619 				AUDIO_SSI_SEL, 0);
620 	else
621 		mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_DAC,
622 				AUDIO_SSI_SEL, AUDIO_SSI_SEL);
623 
624 	return 0;
625 }
626 
mc13783_remove(struct snd_soc_component * component)627 static void mc13783_remove(struct snd_soc_component *component)
628 {
629 	struct mc13783_priv *priv = snd_soc_component_get_drvdata(component);
630 
631 	/* Make sure VAUDIOON is off */
632 	mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_RX0, 0x3, 0);
633 }
634 
635 #define MC13783_RATES_RECORD (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000)
636 
637 #define MC13783_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
638 	SNDRV_PCM_FMTBIT_S24_LE)
639 
640 static const struct snd_soc_dai_ops mc13783_ops_dac = {
641 	.hw_params	= mc13783_pcm_hw_params_dac,
642 	.set_fmt	= mc13783_set_fmt_async,
643 	.set_sysclk	= mc13783_set_sysclk_dac,
644 	.set_tdm_slot	= mc13783_set_tdm_slot_dac,
645 };
646 
647 static const struct snd_soc_dai_ops mc13783_ops_codec = {
648 	.hw_params	= mc13783_pcm_hw_params_codec,
649 	.set_fmt	= mc13783_set_fmt_async,
650 	.set_sysclk	= mc13783_set_sysclk_codec,
651 	.set_tdm_slot	= mc13783_set_tdm_slot_codec,
652 };
653 
654 /*
655  * The mc13783 has two SSI ports, both of them can be routed either
656  * to the voice codec or the stereo DAC. When two different SSI ports
657  * are used for the voice codec and the stereo DAC we can do different
658  * formats and sysclock settings for playback and capture
659  * (mc13783-hifi-playback and mc13783-hifi-capture). Using the same port
660  * forces us to use symmetric rates (mc13783-hifi).
661  */
662 static struct snd_soc_dai_driver mc13783_dai_async[] = {
663 	{
664 		.name = "mc13783-hifi-playback",
665 		.id = MC13783_ID_STEREO_DAC,
666 		.playback = {
667 			.stream_name = "Playback",
668 			.channels_min = 2,
669 			.channels_max = 2,
670 			.rates = SNDRV_PCM_RATE_8000_96000,
671 			.formats = MC13783_FORMATS,
672 		},
673 		.ops = &mc13783_ops_dac,
674 	}, {
675 		.name = "mc13783-hifi-capture",
676 		.id = MC13783_ID_STEREO_CODEC,
677 		.capture = {
678 			.stream_name = "Capture",
679 			.channels_min = 2,
680 			.channels_max = 2,
681 			.rates = MC13783_RATES_RECORD,
682 			.formats = MC13783_FORMATS,
683 		},
684 		.ops = &mc13783_ops_codec,
685 	},
686 };
687 
688 static const struct snd_soc_dai_ops mc13783_ops_sync = {
689 	.hw_params	= mc13783_pcm_hw_params_sync,
690 	.set_fmt	= mc13783_set_fmt_sync,
691 	.set_sysclk	= mc13783_set_sysclk_sync,
692 	.set_tdm_slot	= mc13783_set_tdm_slot_sync,
693 };
694 
695 static struct snd_soc_dai_driver mc13783_dai_sync[] = {
696 	{
697 		.name = "mc13783-hifi",
698 		.id = MC13783_ID_SYNC,
699 		.playback = {
700 			.stream_name = "Playback",
701 			.channels_min = 2,
702 			.channels_max = 2,
703 			.rates = SNDRV_PCM_RATE_8000_96000,
704 			.formats = MC13783_FORMATS,
705 		},
706 		.capture = {
707 			.stream_name = "Capture",
708 			.channels_min = 2,
709 			.channels_max = 2,
710 			.rates = MC13783_RATES_RECORD,
711 			.formats = MC13783_FORMATS,
712 		},
713 		.ops = &mc13783_ops_sync,
714 		.symmetric_rate = 1,
715 	}
716 };
717 
718 static const struct snd_soc_component_driver soc_component_dev_mc13783 = {
719 	.probe			= mc13783_probe,
720 	.remove			= mc13783_remove,
721 	.controls		= mc13783_control_list,
722 	.num_controls		= ARRAY_SIZE(mc13783_control_list),
723 	.dapm_widgets		= mc13783_dapm_widgets,
724 	.num_dapm_widgets	= ARRAY_SIZE(mc13783_dapm_widgets),
725 	.dapm_routes		= mc13783_routes,
726 	.num_dapm_routes	= ARRAY_SIZE(mc13783_routes),
727 	.idle_bias_on		= 1,
728 	.use_pmdown_time	= 1,
729 	.endianness		= 1,
730 };
731 
mc13783_codec_probe(struct platform_device * pdev)732 static int __init mc13783_codec_probe(struct platform_device *pdev)
733 {
734 	struct mc13783_priv *priv;
735 	struct mc13xxx_codec_platform_data *pdata = pdev->dev.platform_data;
736 	struct device_node *np;
737 	int ret;
738 
739 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
740 	if (!priv)
741 		return -ENOMEM;
742 
743 	if (pdata) {
744 		priv->adc_ssi_port = pdata->adc_ssi_port;
745 		priv->dac_ssi_port = pdata->dac_ssi_port;
746 	} else {
747 		np = of_get_child_by_name(pdev->dev.parent->of_node, "codec");
748 		if (!np)
749 			return -ENOSYS;
750 
751 		ret = of_property_read_u32(np, "adc-port", &priv->adc_ssi_port);
752 		if (ret) {
753 			of_node_put(np);
754 			return ret;
755 		}
756 
757 		ret = of_property_read_u32(np, "dac-port", &priv->dac_ssi_port);
758 		if (ret) {
759 			of_node_put(np);
760 			return ret;
761 		}
762 
763 		of_node_put(np);
764 	}
765 
766 	dev_set_drvdata(&pdev->dev, priv);
767 	priv->mc13xxx = dev_get_drvdata(pdev->dev.parent);
768 
769 	if (priv->adc_ssi_port == priv->dac_ssi_port)
770 		ret = devm_snd_soc_register_component(&pdev->dev, &soc_component_dev_mc13783,
771 			mc13783_dai_sync, ARRAY_SIZE(mc13783_dai_sync));
772 	else
773 		ret = devm_snd_soc_register_component(&pdev->dev, &soc_component_dev_mc13783,
774 			mc13783_dai_async, ARRAY_SIZE(mc13783_dai_async));
775 
776 	return ret;
777 }
778 
779 static struct platform_driver mc13783_codec_driver = {
780 	.driver = {
781 		.name	= "mc13783-codec",
782 	},
783 };
784 module_platform_driver_probe(mc13783_codec_driver, mc13783_codec_probe);
785 
786 MODULE_DESCRIPTION("ASoC MC13783 driver");
787 MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
788 MODULE_AUTHOR("Philippe Retornaz <philippe.retornaz@epfl.ch>");
789 MODULE_LICENSE("GPL");
790