blob: bebba7fb8819f2547672a9a4d1d7555a613f5443 [file] [log] [blame]
Philippe Rétornaz8b908b82012-05-15 13:53:50 +02001/*
2 * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
3 * Copyright 2009 Sascha Hauer, s.hauer@pengutronix.de
4 * Copyright 2012 Philippe Retornaz, philippe.retornaz@epfl.ch
5 *
6 * Initial development of this code was funded by
7 * Phytec Messtechnik GmbH, http://www.phytec.de
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 * MA 02110-1301, USA.
22 */
23#include <linux/module.h>
24#include <linux/device.h>
25#include <linux/mfd/mc13xxx.h>
26#include <linux/slab.h>
27#include <sound/core.h>
28#include <sound/control.h>
29#include <sound/pcm.h>
30#include <sound/soc.h>
31#include <sound/initval.h>
32#include <sound/soc-dapm.h>
Mark Brown2d9215c2013-09-18 19:04:17 +010033#include <linux/regmap.h>
Philippe Rétornaz8b908b82012-05-15 13:53:50 +020034
35#include "mc13783.h"
36
Philippe Rétornaz8b908b82012-05-15 13:53:50 +020037#define AUDIO_RX0_ALSPEN (1 << 5)
38#define AUDIO_RX0_ALSPSEL (1 << 7)
39#define AUDIO_RX0_ADDCDC (1 << 21)
40#define AUDIO_RX0_ADDSTDC (1 << 22)
41#define AUDIO_RX0_ADDRXIN (1 << 23)
42
43#define AUDIO_RX1_PGARXEN (1 << 0);
44#define AUDIO_RX1_PGASTEN (1 << 5)
45#define AUDIO_RX1_ARXINEN (1 << 10)
46
47#define AUDIO_TX_AMC1REN (1 << 5)
48#define AUDIO_TX_AMC1LEN (1 << 7)
49#define AUDIO_TX_AMC2EN (1 << 9)
50#define AUDIO_TX_ATXINEN (1 << 11)
51#define AUDIO_TX_RXINREC (1 << 13)
52
53#define SSI_NETWORK_CDCTXRXSLOT(x) (((x) & 0x3) << 2)
54#define SSI_NETWORK_CDCTXSECSLOT(x) (((x) & 0x3) << 4)
55#define SSI_NETWORK_CDCRXSECSLOT(x) (((x) & 0x3) << 6)
56#define SSI_NETWORK_CDCRXSECGAIN(x) (((x) & 0x3) << 8)
57#define SSI_NETWORK_CDCSUMGAIN(x) (1 << 10)
58#define SSI_NETWORK_CDCFSDLY(x) (1 << 11)
59#define SSI_NETWORK_DAC_SLOTS_8 (1 << 12)
60#define SSI_NETWORK_DAC_SLOTS_4 (2 << 12)
61#define SSI_NETWORK_DAC_SLOTS_2 (3 << 12)
62#define SSI_NETWORK_DAC_SLOT_MASK (3 << 12)
63#define SSI_NETWORK_DAC_RXSLOT_0_1 (0 << 14)
64#define SSI_NETWORK_DAC_RXSLOT_2_3 (1 << 14)
65#define SSI_NETWORK_DAC_RXSLOT_4_5 (2 << 14)
66#define SSI_NETWORK_DAC_RXSLOT_6_7 (3 << 14)
67#define SSI_NETWORK_DAC_RXSLOT_MASK (3 << 14)
68#define SSI_NETWORK_STDCRXSECSLOT(x) (((x) & 0x3) << 16)
69#define SSI_NETWORK_STDCRXSECGAIN(x) (((x) & 0x3) << 18)
70#define SSI_NETWORK_STDCSUMGAIN (1 << 20)
71
72/*
73 * MC13783_AUDIO_CODEC and MC13783_AUDIO_DAC mostly share the same
74 * register layout
75 */
76#define AUDIO_SSI_SEL (1 << 0)
77#define AUDIO_CLK_SEL (1 << 1)
78#define AUDIO_CSM (1 << 2)
79#define AUDIO_BCL_INV (1 << 3)
80#define AUDIO_CFS_INV (1 << 4)
81#define AUDIO_CFS(x) (((x) & 0x3) << 5)
82#define AUDIO_CLK(x) (((x) & 0x7) << 7)
83#define AUDIO_C_EN (1 << 11)
84#define AUDIO_C_CLK_EN (1 << 12)
85#define AUDIO_C_RESET (1 << 15)
86
87#define AUDIO_CODEC_CDCFS8K16K (1 << 10)
88#define AUDIO_DAC_CFS_DLY_B (1 << 10)
89
90struct mc13783_priv {
Philippe Rétornaz8b908b82012-05-15 13:53:50 +020091 struct mc13xxx *mc13xxx;
Mark Brown2d9215c2013-09-18 19:04:17 +010092 struct regmap *regmap;
Philippe Rétornaz8b908b82012-05-15 13:53:50 +020093
94 enum mc13783_ssi_port adc_ssi_port;
95 enum mc13783_ssi_port dac_ssi_port;
96};
97
Philippe Rétornaz8b908b82012-05-15 13:53:50 +020098/* Mapping between sample rates and register value */
99static unsigned int mc13783_rates[] = {
100 8000, 11025, 12000, 16000,
101 22050, 24000, 32000, 44100,
102 48000, 64000, 96000
103};
104
105static int mc13783_pcm_hw_params_dac(struct snd_pcm_substream *substream,
106 struct snd_pcm_hw_params *params,
107 struct snd_soc_dai *dai)
108{
Lars-Peter Clausenab642462014-03-13 21:24:54 +0100109 struct snd_soc_codec *codec = dai->codec;
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200110 unsigned int rate = params_rate(params);
111 int i;
112
113 for (i = 0; i < ARRAY_SIZE(mc13783_rates); i++) {
114 if (rate == mc13783_rates[i]) {
115 snd_soc_update_bits(codec, MC13783_AUDIO_DAC,
116 0xf << 17, i << 17);
117 return 0;
118 }
119 }
120
121 return -EINVAL;
122}
123
124static int mc13783_pcm_hw_params_codec(struct snd_pcm_substream *substream,
125 struct snd_pcm_hw_params *params,
126 struct snd_soc_dai *dai)
127{
Lars-Peter Clausenab642462014-03-13 21:24:54 +0100128 struct snd_soc_codec *codec = dai->codec;
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200129 unsigned int rate = params_rate(params);
130 unsigned int val;
131
132 switch (rate) {
133 case 8000:
134 val = 0;
135 break;
136 case 16000:
137 val = AUDIO_CODEC_CDCFS8K16K;
138 break;
139 default:
140 return -EINVAL;
141 }
142
143 snd_soc_update_bits(codec, MC13783_AUDIO_CODEC, AUDIO_CODEC_CDCFS8K16K,
144 val);
145
146 return 0;
147}
148
149static int mc13783_pcm_hw_params_sync(struct snd_pcm_substream *substream,
150 struct snd_pcm_hw_params *params,
151 struct snd_soc_dai *dai)
152{
153 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
154 return mc13783_pcm_hw_params_dac(substream, params, dai);
155 else
156 return mc13783_pcm_hw_params_codec(substream, params, dai);
157}
158
159static int mc13783_set_fmt(struct snd_soc_dai *dai, unsigned int fmt,
160 unsigned int reg)
161{
162 struct snd_soc_codec *codec = dai->codec;
163 unsigned int val = 0;
164 unsigned int mask = AUDIO_CFS(3) | AUDIO_BCL_INV | AUDIO_CFS_INV |
165 AUDIO_CSM | AUDIO_C_CLK_EN | AUDIO_C_RESET;
166
167
168 /* DAI mode */
169 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
170 case SND_SOC_DAIFMT_I2S:
171 val |= AUDIO_CFS(2);
172 break;
173 case SND_SOC_DAIFMT_DSP_A:
174 val |= AUDIO_CFS(1);
175 break;
176 default:
177 return -EINVAL;
178 }
179
180 /* DAI clock inversion */
181 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
182 case SND_SOC_DAIFMT_NB_NF:
183 val |= AUDIO_BCL_INV;
184 break;
185 case SND_SOC_DAIFMT_NB_IF:
186 val |= AUDIO_BCL_INV | AUDIO_CFS_INV;
187 break;
188 case SND_SOC_DAIFMT_IB_NF:
189 break;
190 case SND_SOC_DAIFMT_IB_IF:
191 val |= AUDIO_CFS_INV;
192 break;
193 }
194
195 /* DAI clock master masks */
196 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
197 case SND_SOC_DAIFMT_CBM_CFM:
198 val |= AUDIO_C_CLK_EN;
199 break;
200 case SND_SOC_DAIFMT_CBS_CFS:
201 val |= AUDIO_CSM;
202 break;
203 case SND_SOC_DAIFMT_CBM_CFS:
204 case SND_SOC_DAIFMT_CBS_CFM:
205 return -EINVAL;
206 }
207
208 val |= AUDIO_C_RESET;
209
210 snd_soc_update_bits(codec, reg, mask, val);
211
212 return 0;
213}
214
215static int mc13783_set_fmt_async(struct snd_soc_dai *dai, unsigned int fmt)
216{
217 if (dai->id == MC13783_ID_STEREO_DAC)
218 return mc13783_set_fmt(dai, fmt, MC13783_AUDIO_DAC);
219 else
220 return mc13783_set_fmt(dai, fmt, MC13783_AUDIO_CODEC);
221}
222
223static int mc13783_set_fmt_sync(struct snd_soc_dai *dai, unsigned int fmt)
224{
225 int ret;
226
227 ret = mc13783_set_fmt(dai, fmt, MC13783_AUDIO_DAC);
228 if (ret)
229 return ret;
230
231 /*
232 * In synchronous mode force the voice codec into slave mode
233 * so that the clock / framesync from the stereo DAC is used
234 */
235 fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
236 fmt |= SND_SOC_DAIFMT_CBS_CFS;
237 ret = mc13783_set_fmt(dai, fmt, MC13783_AUDIO_CODEC);
238
239 return ret;
240}
241
242static int mc13783_sysclk[] = {
243 13000000,
244 15360000,
245 16800000,
246 -1,
247 26000000,
248 -1, /* 12000000, invalid for voice codec */
249 -1, /* 3686400, invalid for voice codec */
250 33600000,
251};
252
253static int mc13783_set_sysclk(struct snd_soc_dai *dai,
254 int clk_id, unsigned int freq, int dir,
255 unsigned int reg)
256{
257 struct snd_soc_codec *codec = dai->codec;
258 int clk;
259 unsigned int val = 0;
260 unsigned int mask = AUDIO_CLK(0x7) | AUDIO_CLK_SEL;
261
262 for (clk = 0; clk < ARRAY_SIZE(mc13783_sysclk); clk++) {
263 if (mc13783_sysclk[clk] < 0)
264 continue;
265 if (mc13783_sysclk[clk] == freq)
266 break;
267 }
268
269 if (clk == ARRAY_SIZE(mc13783_sysclk))
270 return -EINVAL;
271
272 if (clk_id == MC13783_CLK_CLIB)
273 val |= AUDIO_CLK_SEL;
274
275 val |= AUDIO_CLK(clk);
276
277 snd_soc_update_bits(codec, reg, mask, val);
278
279 return 0;
280}
281
282static int mc13783_set_sysclk_dac(struct snd_soc_dai *dai,
283 int clk_id, unsigned int freq, int dir)
284{
285 return mc13783_set_sysclk(dai, clk_id, freq, dir, MC13783_AUDIO_DAC);
286}
287
288static int mc13783_set_sysclk_codec(struct snd_soc_dai *dai,
289 int clk_id, unsigned int freq, int dir)
290{
291 return mc13783_set_sysclk(dai, clk_id, freq, dir, MC13783_AUDIO_CODEC);
292}
293
294static int mc13783_set_sysclk_sync(struct snd_soc_dai *dai,
295 int clk_id, unsigned int freq, int dir)
296{
297 int ret;
298
299 ret = mc13783_set_sysclk(dai, clk_id, freq, dir, MC13783_AUDIO_DAC);
300 if (ret)
301 return ret;
302
303 return mc13783_set_sysclk(dai, clk_id, freq, dir, MC13783_AUDIO_CODEC);
304}
305
306static int mc13783_set_tdm_slot_dac(struct snd_soc_dai *dai,
307 unsigned int tx_mask, unsigned int rx_mask, int slots,
308 int slot_width)
309{
310 struct snd_soc_codec *codec = dai->codec;
311 unsigned int val = 0;
312 unsigned int mask = SSI_NETWORK_DAC_SLOT_MASK |
313 SSI_NETWORK_DAC_RXSLOT_MASK;
314
315 switch (slots) {
316 case 2:
317 val |= SSI_NETWORK_DAC_SLOTS_2;
318 break;
319 case 4:
320 val |= SSI_NETWORK_DAC_SLOTS_4;
321 break;
322 case 8:
323 val |= SSI_NETWORK_DAC_SLOTS_8;
324 break;
325 default:
326 return -EINVAL;
327 }
328
329 switch (rx_mask) {
330 case 0xfffffffc:
331 val |= SSI_NETWORK_DAC_RXSLOT_0_1;
332 break;
333 case 0xfffffff3:
334 val |= SSI_NETWORK_DAC_RXSLOT_2_3;
335 break;
336 case 0xffffffcf:
337 val |= SSI_NETWORK_DAC_RXSLOT_4_5;
338 break;
339 case 0xffffff3f:
340 val |= SSI_NETWORK_DAC_RXSLOT_6_7;
341 break;
342 default:
343 return -EINVAL;
Joe Perches1d198f22013-10-08 15:55:45 -0700344 }
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200345
346 snd_soc_update_bits(codec, MC13783_SSI_NETWORK, mask, val);
347
348 return 0;
349}
350
351static int mc13783_set_tdm_slot_codec(struct snd_soc_dai *dai,
352 unsigned int tx_mask, unsigned int rx_mask, int slots,
353 int slot_width)
354{
355 struct snd_soc_codec *codec = dai->codec;
356 unsigned int val = 0;
357 unsigned int mask = 0x3f;
358
359 if (slots != 4)
360 return -EINVAL;
361
362 if (tx_mask != 0xfffffffc)
363 return -EINVAL;
364
365 val |= (0x00 << 2); /* primary timeslot RX/TX(?) is 0 */
366 val |= (0x01 << 4); /* secondary timeslot TX is 1 */
367
368 snd_soc_update_bits(codec, MC13783_SSI_NETWORK, mask, val);
369
370 return 0;
371}
372
373static int mc13783_set_tdm_slot_sync(struct snd_soc_dai *dai,
374 unsigned int tx_mask, unsigned int rx_mask, int slots,
375 int slot_width)
376{
377 int ret;
378
379 ret = mc13783_set_tdm_slot_dac(dai, tx_mask, rx_mask, slots,
380 slot_width);
381 if (ret)
382 return ret;
383
384 ret = mc13783_set_tdm_slot_codec(dai, tx_mask, rx_mask, slots,
385 slot_width);
386
387 return ret;
388}
389
390static const struct snd_kcontrol_new mc1l_amp_ctl =
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200391 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_TX, 7, 1, 0);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200392
393static const struct snd_kcontrol_new mc1r_amp_ctl =
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200394 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_TX, 5, 1, 0);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200395
396static const struct snd_kcontrol_new mc2_amp_ctl =
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200397 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_TX, 9, 1, 0);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200398
399static const struct snd_kcontrol_new atx_amp_ctl =
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200400 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_TX, 11, 1, 0);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200401
402
403/* Virtual mux. The chip does the input selection automatically
404 * as soon as we enable one input. */
405static const char * const adcl_enum_text[] = {
406 "MC1L", "RXINL",
407};
408
409static const struct soc_enum adcl_enum =
410 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(adcl_enum_text), adcl_enum_text);
411
412static const struct snd_kcontrol_new left_input_mux =
413 SOC_DAPM_ENUM_VIRT("Route", adcl_enum);
414
415static const char * const adcr_enum_text[] = {
416 "MC1R", "MC2", "RXINR", "TXIN",
417};
418
419static const struct soc_enum adcr_enum =
420 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(adcr_enum_text), adcr_enum_text);
421
422static const struct snd_kcontrol_new right_input_mux =
423 SOC_DAPM_ENUM_VIRT("Route", adcr_enum);
424
425static const struct snd_kcontrol_new samp_ctl =
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200426 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 3, 1, 0);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200427
Steffen Trumtrarbb7838d2013-10-11 12:28:14 +0200428static const char * const speaker_amp_source_text[] = {
429 "CODEC", "Right"
430};
431static const SOC_ENUM_SINGLE_DECL(speaker_amp_source, MC13783_AUDIO_RX0, 4,
432 speaker_amp_source_text);
433static const struct snd_kcontrol_new speaker_amp_source_mux =
434 SOC_DAPM_ENUM("Speaker Amp Source MUX", speaker_amp_source);
435
436static const char * const headset_amp_source_text[] = {
437 "CODEC", "Mixer"
438};
439
440static const SOC_ENUM_SINGLE_DECL(headset_amp_source, MC13783_AUDIO_RX0, 11,
441 headset_amp_source_text);
442static const struct snd_kcontrol_new headset_amp_source_mux =
443 SOC_DAPM_ENUM("Headset Amp Source MUX", headset_amp_source);
444
445static const struct snd_kcontrol_new cdcout_ctl =
446 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 18, 1, 0);
447
448static const struct snd_kcontrol_new adc_bypass_ctl =
449 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_CODEC, 16, 1, 0);
450
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200451static const struct snd_kcontrol_new lamp_ctl =
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200452 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 5, 1, 0);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200453
454static const struct snd_kcontrol_new hlamp_ctl =
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200455 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 10, 1, 0);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200456
457static const struct snd_kcontrol_new hramp_ctl =
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200458 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 9, 1, 0);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200459
460static const struct snd_kcontrol_new llamp_ctl =
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200461 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 16, 1, 0);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200462
463static const struct snd_kcontrol_new lramp_ctl =
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200464 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 15, 1, 0);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200465
466static const struct snd_soc_dapm_widget mc13783_dapm_widgets[] = {
467/* Input */
468 SND_SOC_DAPM_INPUT("MC1LIN"),
469 SND_SOC_DAPM_INPUT("MC1RIN"),
470 SND_SOC_DAPM_INPUT("MC2IN"),
471 SND_SOC_DAPM_INPUT("RXINR"),
472 SND_SOC_DAPM_INPUT("RXINL"),
473 SND_SOC_DAPM_INPUT("TXIN"),
474
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200475 SND_SOC_DAPM_SUPPLY("MC1 Bias", MC13783_AUDIO_TX, 0, 0, NULL, 0),
476 SND_SOC_DAPM_SUPPLY("MC2 Bias", MC13783_AUDIO_TX, 1, 0, NULL, 0),
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200477
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200478 SND_SOC_DAPM_SWITCH("MC1L Amp", MC13783_AUDIO_TX, 7, 0, &mc1l_amp_ctl),
479 SND_SOC_DAPM_SWITCH("MC1R Amp", MC13783_AUDIO_TX, 5, 0, &mc1r_amp_ctl),
480 SND_SOC_DAPM_SWITCH("MC2 Amp", MC13783_AUDIO_TX, 9, 0, &mc2_amp_ctl),
481 SND_SOC_DAPM_SWITCH("TXIN Amp", MC13783_AUDIO_TX, 11, 0, &atx_amp_ctl),
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200482
483 SND_SOC_DAPM_VIRT_MUX("PGA Left Input Mux", SND_SOC_NOPM, 0, 0,
484 &left_input_mux),
485 SND_SOC_DAPM_VIRT_MUX("PGA Right Input Mux", SND_SOC_NOPM, 0, 0,
486 &right_input_mux),
487
Steffen Trumtrarbb7838d2013-10-11 12:28:14 +0200488 SND_SOC_DAPM_MUX("Speaker Amp Source MUX", SND_SOC_NOPM, 0, 0,
489 &speaker_amp_source_mux),
490
491 SND_SOC_DAPM_MUX("Headset Amp Source MUX", SND_SOC_NOPM, 0, 0,
492 &headset_amp_source_mux),
493
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200494 SND_SOC_DAPM_PGA("PGA Left Input", SND_SOC_NOPM, 0, 0, NULL, 0),
495 SND_SOC_DAPM_PGA("PGA Right Input", SND_SOC_NOPM, 0, 0, NULL, 0),
496
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200497 SND_SOC_DAPM_ADC("ADC", "Capture", MC13783_AUDIO_CODEC, 11, 0),
498 SND_SOC_DAPM_SUPPLY("ADC_Reset", MC13783_AUDIO_CODEC, 15, 0, NULL, 0),
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200499
Steffen Trumtrarbb7838d2013-10-11 12:28:14 +0200500 SND_SOC_DAPM_PGA("Voice CODEC PGA", MC13783_AUDIO_RX1, 0, 0, NULL, 0),
501 SND_SOC_DAPM_SWITCH("Voice CODEC Bypass", MC13783_AUDIO_CODEC, 16, 0,
502 &adc_bypass_ctl),
503
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200504/* Output */
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200505 SND_SOC_DAPM_SUPPLY("DAC_E", MC13783_AUDIO_DAC, 11, 0, NULL, 0),
506 SND_SOC_DAPM_SUPPLY("DAC_Reset", MC13783_AUDIO_DAC, 15, 0, NULL, 0),
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200507 SND_SOC_DAPM_OUTPUT("RXOUTL"),
508 SND_SOC_DAPM_OUTPUT("RXOUTR"),
509 SND_SOC_DAPM_OUTPUT("HSL"),
510 SND_SOC_DAPM_OUTPUT("HSR"),
Steffen Trumtrarbb7838d2013-10-11 12:28:14 +0200511 SND_SOC_DAPM_OUTPUT("LSPL"),
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200512 SND_SOC_DAPM_OUTPUT("LSP"),
513 SND_SOC_DAPM_OUTPUT("SP"),
Steffen Trumtrarbb7838d2013-10-11 12:28:14 +0200514 SND_SOC_DAPM_OUTPUT("CDCOUT"),
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200515
Steffen Trumtrarbb7838d2013-10-11 12:28:14 +0200516 SND_SOC_DAPM_SWITCH("CDCOUT Switch", MC13783_AUDIO_RX0, 18, 0,
517 &cdcout_ctl),
518 SND_SOC_DAPM_SWITCH("Speaker Amp Switch", MC13783_AUDIO_RX0, 3, 0,
519 &samp_ctl),
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200520 SND_SOC_DAPM_SWITCH("Loudspeaker Amp", SND_SOC_NOPM, 0, 0, &lamp_ctl),
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200521 SND_SOC_DAPM_SWITCH("Headset Amp Left", MC13783_AUDIO_RX0, 10, 0,
522 &hlamp_ctl),
523 SND_SOC_DAPM_SWITCH("Headset Amp Right", MC13783_AUDIO_RX0, 9, 0,
524 &hramp_ctl),
525 SND_SOC_DAPM_SWITCH("Line out Amp Left", MC13783_AUDIO_RX0, 16, 0,
526 &llamp_ctl),
527 SND_SOC_DAPM_SWITCH("Line out Amp Right", MC13783_AUDIO_RX0, 15, 0,
528 &lramp_ctl),
529 SND_SOC_DAPM_DAC("DAC", "Playback", MC13783_AUDIO_RX0, 22, 0),
530 SND_SOC_DAPM_PGA("DAC PGA", MC13783_AUDIO_RX1, 5, 0, NULL, 0),
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200531};
532
533static struct snd_soc_dapm_route mc13783_routes[] = {
534/* Input */
535 { "MC1L Amp", NULL, "MC1LIN"},
536 { "MC1R Amp", NULL, "MC1RIN" },
537 { "MC2 Amp", NULL, "MC2IN" },
538 { "TXIN Amp", NULL, "TXIN"},
539
540 { "PGA Left Input Mux", "MC1L", "MC1L Amp" },
541 { "PGA Left Input Mux", "RXINL", "RXINL"},
542 { "PGA Right Input Mux", "MC1R", "MC1R Amp" },
543 { "PGA Right Input Mux", "MC2", "MC2 Amp"},
544 { "PGA Right Input Mux", "TXIN", "TXIN Amp"},
545 { "PGA Right Input Mux", "RXINR", "RXINR"},
546
547 { "PGA Left Input", NULL, "PGA Left Input Mux"},
548 { "PGA Right Input", NULL, "PGA Right Input Mux"},
549
550 { "ADC", NULL, "PGA Left Input"},
551 { "ADC", NULL, "PGA Right Input"},
552 { "ADC", NULL, "ADC_Reset"},
553
Steffen Trumtrarbb7838d2013-10-11 12:28:14 +0200554 { "Voice CODEC PGA", "Voice CODEC Bypass", "ADC" },
555
556 { "Speaker Amp Source MUX", "CODEC", "Voice CODEC PGA"},
557 { "Speaker Amp Source MUX", "Right", "DAC PGA"},
558
559 { "Headset Amp Source MUX", "CODEC", "Voice CODEC PGA"},
560 { "Headset Amp Source MUX", "Mixer", "DAC PGA"},
561
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200562/* Output */
563 { "HSL", NULL, "Headset Amp Left" },
564 { "HSR", NULL, "Headset Amp Right"},
565 { "RXOUTL", NULL, "Line out Amp Left"},
566 { "RXOUTR", NULL, "Line out Amp Right"},
Steffen Trumtrarbb7838d2013-10-11 12:28:14 +0200567 { "SP", "Speaker Amp Switch", "Speaker Amp Source MUX"},
568 { "LSP", "Loudspeaker Amp", "Speaker Amp Source MUX"},
569 { "HSL", "Headset Amp Left", "Headset Amp Source MUX"},
570 { "HSR", "Headset Amp Right", "Headset Amp Source MUX"},
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200571 { "Line out Amp Left", NULL, "DAC PGA"},
572 { "Line out Amp Right", NULL, "DAC PGA"},
573 { "DAC PGA", NULL, "DAC"},
574 { "DAC", NULL, "DAC_E"},
Steffen Trumtrarbb7838d2013-10-11 12:28:14 +0200575 { "CDCOUT", "CDCOUT Switch", "Voice CODEC PGA"},
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200576};
577
578static const char * const mc13783_3d_mixer[] = {"Stereo", "Phase Mix",
579 "Mono", "Mono Mix"};
580
581static const struct soc_enum mc13783_enum_3d_mixer =
582 SOC_ENUM_SINGLE(MC13783_AUDIO_RX1, 16, ARRAY_SIZE(mc13783_3d_mixer),
583 mc13783_3d_mixer);
584
585static struct snd_kcontrol_new mc13783_control_list[] = {
586 SOC_SINGLE("Loudspeaker enable", MC13783_AUDIO_RX0, 5, 1, 0),
587 SOC_SINGLE("PCM Playback Volume", MC13783_AUDIO_RX1, 6, 15, 0),
Steffen Trumtrarc6452e32013-10-11 12:28:13 +0200588 SOC_SINGLE("PCM Playback Switch", MC13783_AUDIO_RX1, 5, 1, 0),
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200589 SOC_DOUBLE("PCM Capture Volume", MC13783_AUDIO_TX, 19, 14, 31, 0),
590 SOC_ENUM("3D Control", mc13783_enum_3d_mixer),
Steffen Trumtrarc6452e32013-10-11 12:28:13 +0200591
592 SOC_SINGLE("CDCOUT Switch", MC13783_AUDIO_RX0, 18, 1, 0),
593 SOC_SINGLE("Earpiece Amp Switch", MC13783_AUDIO_RX0, 3, 1, 0),
594 SOC_DOUBLE("Headset Amp Switch", MC13783_AUDIO_RX0, 10, 9, 1, 0),
595 SOC_DOUBLE("Line out Amp Switch", MC13783_AUDIO_RX0, 16, 15, 1, 0),
596
597 SOC_SINGLE("PCM Capture Mixin Switch", MC13783_AUDIO_RX0, 22, 1, 0),
598 SOC_SINGLE("Line in Capture Mixin Switch", MC13783_AUDIO_RX0, 23, 1, 0),
599
600 SOC_SINGLE("CODEC Capture Volume", MC13783_AUDIO_RX1, 1, 15, 0),
601 SOC_SINGLE("CODEC Capture Mixin Switch", MC13783_AUDIO_RX0, 21, 1, 0),
602
603 SOC_SINGLE("Line in Capture Volume", MC13783_AUDIO_RX1, 12, 15, 0),
604 SOC_SINGLE("Line in Capture Switch", MC13783_AUDIO_RX1, 10, 1, 0),
605
606 SOC_SINGLE("MC1 Capture Bias Switch", MC13783_AUDIO_TX, 0, 1, 0),
607 SOC_SINGLE("MC2 Capture Bias Switch", MC13783_AUDIO_TX, 1, 1, 0),
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200608};
609
610static int mc13783_probe(struct snd_soc_codec *codec)
611{
612 struct mc13783_priv *priv = snd_soc_codec_get_drvdata(codec);
Mark Brown2d9215c2013-09-18 19:04:17 +0100613 int ret;
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200614
Mark Brown2d9215c2013-09-18 19:04:17 +0100615 codec->control_data = dev_get_regmap(codec->dev->parent, NULL);
616 ret = snd_soc_codec_set_cache_io(codec, 8, 24, SND_SOC_REGMAP);
617 if (ret != 0) {
618 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
619 return ret;
620 }
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200621
622 /* these are the reset values */
623 mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_RX0, 0x25893);
624 mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_RX1, 0x00d35A);
625 mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_TX, 0x420000);
626 mc13xxx_reg_write(priv->mc13xxx, MC13783_SSI_NETWORK, 0x013060);
627 mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_CODEC, 0x180027);
628 mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_DAC, 0x0e0004);
629
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200630 if (priv->adc_ssi_port == MC13783_SSI1_PORT)
631 mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_CODEC,
632 AUDIO_SSI_SEL, 0);
633 else
634 mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_CODEC,
635 0, AUDIO_SSI_SEL);
636
637 if (priv->dac_ssi_port == MC13783_SSI1_PORT)
638 mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_DAC,
639 AUDIO_SSI_SEL, 0);
640 else
641 mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_DAC,
642 0, AUDIO_SSI_SEL);
643
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200644 return 0;
645}
646
647static int mc13783_remove(struct snd_soc_codec *codec)
648{
649 struct mc13783_priv *priv = snd_soc_codec_get_drvdata(codec);
650
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200651 /* Make sure VAUDIOON is off */
652 mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_RX0, 0x3, 0);
653
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200654 return 0;
655}
656
657#define MC13783_RATES_RECORD (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000)
658
659#define MC13783_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
660 SNDRV_PCM_FMTBIT_S24_LE)
661
662static struct snd_soc_dai_ops mc13783_ops_dac = {
663 .hw_params = mc13783_pcm_hw_params_dac,
664 .set_fmt = mc13783_set_fmt_async,
665 .set_sysclk = mc13783_set_sysclk_dac,
666 .set_tdm_slot = mc13783_set_tdm_slot_dac,
667};
668
669static struct snd_soc_dai_ops mc13783_ops_codec = {
670 .hw_params = mc13783_pcm_hw_params_codec,
671 .set_fmt = mc13783_set_fmt_async,
672 .set_sysclk = mc13783_set_sysclk_codec,
673 .set_tdm_slot = mc13783_set_tdm_slot_codec,
674};
675
676/*
677 * The mc13783 has two SSI ports, both of them can be routed either
678 * to the voice codec or the stereo DAC. When two different SSI ports
679 * are used for the voice codec and the stereo DAC we can do different
680 * formats and sysclock settings for playback and capture
681 * (mc13783-hifi-playback and mc13783-hifi-capture). Using the same port
682 * forces us to use symmetric rates (mc13783-hifi).
683 */
684static struct snd_soc_dai_driver mc13783_dai_async[] = {
685 {
686 .name = "mc13783-hifi-playback",
687 .id = MC13783_ID_STEREO_DAC,
688 .playback = {
689 .stream_name = "Playback",
Fabio Estevam37f45cc52012-09-03 13:04:13 -0300690 .channels_min = 2,
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200691 .channels_max = 2,
692 .rates = SNDRV_PCM_RATE_8000_96000,
693 .formats = MC13783_FORMATS,
694 },
695 .ops = &mc13783_ops_dac,
696 }, {
697 .name = "mc13783-hifi-capture",
698 .id = MC13783_ID_STEREO_CODEC,
699 .capture = {
700 .stream_name = "Capture",
Fabio Estevam37f45cc52012-09-03 13:04:13 -0300701 .channels_min = 2,
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200702 .channels_max = 2,
703 .rates = MC13783_RATES_RECORD,
704 .formats = MC13783_FORMATS,
705 },
706 .ops = &mc13783_ops_codec,
707 },
708};
709
710static struct snd_soc_dai_ops mc13783_ops_sync = {
711 .hw_params = mc13783_pcm_hw_params_sync,
712 .set_fmt = mc13783_set_fmt_sync,
713 .set_sysclk = mc13783_set_sysclk_sync,
714 .set_tdm_slot = mc13783_set_tdm_slot_sync,
715};
716
717static struct snd_soc_dai_driver mc13783_dai_sync[] = {
718 {
719 .name = "mc13783-hifi",
720 .id = MC13783_ID_SYNC,
721 .playback = {
722 .stream_name = "Playback",
Fabio Estevam37f45cc52012-09-03 13:04:13 -0300723 .channels_min = 2,
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200724 .channels_max = 2,
725 .rates = SNDRV_PCM_RATE_8000_96000,
726 .formats = MC13783_FORMATS,
727 },
728 .capture = {
729 .stream_name = "Capture",
Fabio Estevam37f45cc52012-09-03 13:04:13 -0300730 .channels_min = 2,
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200731 .channels_max = 2,
732 .rates = MC13783_RATES_RECORD,
733 .formats = MC13783_FORMATS,
734 },
735 .ops = &mc13783_ops_sync,
736 .symmetric_rates = 1,
737 }
738};
739
740static struct snd_soc_codec_driver soc_codec_dev_mc13783 = {
741 .probe = mc13783_probe,
742 .remove = mc13783_remove,
Philippe Rétornazb77458d2012-05-16 12:49:19 +0200743 .controls = mc13783_control_list,
744 .num_controls = ARRAY_SIZE(mc13783_control_list),
745 .dapm_widgets = mc13783_dapm_widgets,
746 .num_dapm_widgets = ARRAY_SIZE(mc13783_dapm_widgets),
747 .dapm_routes = mc13783_routes,
748 .num_dapm_routes = ARRAY_SIZE(mc13783_routes),
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200749};
750
Alexander Shiyana5d3f6a2014-01-05 11:38:31 +0400751static int __init mc13783_codec_probe(struct platform_device *pdev)
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200752{
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200753 struct mc13783_priv *priv;
754 struct mc13xxx_codec_platform_data *pdata = pdev->dev.platform_data;
755 int ret;
756
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200757 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Alexander Shiyan2b320982014-01-05 11:38:34 +0400758 if (!priv)
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200759 return -ENOMEM;
760
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200761 if (pdata) {
762 priv->adc_ssi_port = pdata->adc_ssi_port;
763 priv->dac_ssi_port = pdata->dac_ssi_port;
764 } else {
Alexander Shiyan295b8422014-01-05 11:38:33 +0400765 return -ENOSYS;
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200766 }
767
Alexander Shiyan2b320982014-01-05 11:38:34 +0400768 dev_set_drvdata(&pdev->dev, priv);
769 priv->mc13xxx = dev_get_drvdata(pdev->dev.parent);
770
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200771 if (priv->adc_ssi_port == priv->dac_ssi_port)
772 ret = snd_soc_register_codec(&pdev->dev, &soc_codec_dev_mc13783,
773 mc13783_dai_sync, ARRAY_SIZE(mc13783_dai_sync));
774 else
775 ret = snd_soc_register_codec(&pdev->dev, &soc_codec_dev_mc13783,
776 mc13783_dai_async, ARRAY_SIZE(mc13783_dai_async));
777
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200778 return ret;
779}
780
781static int mc13783_codec_remove(struct platform_device *pdev)
782{
783 snd_soc_unregister_codec(&pdev->dev);
784
785 return 0;
786}
787
788static struct platform_driver mc13783_codec_driver = {
789 .driver = {
Alexander Shiyan2b320982014-01-05 11:38:34 +0400790 .name = "mc13783-codec",
791 .owner = THIS_MODULE,
792 },
Bill Pemberton7a79e942012-12-07 09:26:37 -0500793 .remove = mc13783_codec_remove,
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200794};
Alexander Shiyana5d3f6a2014-01-05 11:38:31 +0400795module_platform_driver_probe(mc13783_codec_driver, mc13783_codec_probe);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200796
797MODULE_DESCRIPTION("ASoC MC13783 driver");
798MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
799MODULE_AUTHOR("Philippe Retornaz <philippe.retornaz@epfl.ch>");
800MODULE_LICENSE("GPL");