blob: eedbf05b8e96778cb50dfad7643bbb5b63cc9e2f [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{
109 struct snd_soc_pcm_runtime *rtd = substream->private_data;
110 struct snd_soc_codec *codec = rtd->codec;
111 unsigned int rate = params_rate(params);
112 int i;
113
114 for (i = 0; i < ARRAY_SIZE(mc13783_rates); i++) {
115 if (rate == mc13783_rates[i]) {
116 snd_soc_update_bits(codec, MC13783_AUDIO_DAC,
117 0xf << 17, i << 17);
118 return 0;
119 }
120 }
121
122 return -EINVAL;
123}
124
125static int mc13783_pcm_hw_params_codec(struct snd_pcm_substream *substream,
126 struct snd_pcm_hw_params *params,
127 struct snd_soc_dai *dai)
128{
129 struct snd_soc_pcm_runtime *rtd = substream->private_data;
130 struct snd_soc_codec *codec = rtd->codec;
131 unsigned int rate = params_rate(params);
132 unsigned int val;
133
134 switch (rate) {
135 case 8000:
136 val = 0;
137 break;
138 case 16000:
139 val = AUDIO_CODEC_CDCFS8K16K;
140 break;
141 default:
142 return -EINVAL;
143 }
144
145 snd_soc_update_bits(codec, MC13783_AUDIO_CODEC, AUDIO_CODEC_CDCFS8K16K,
146 val);
147
148 return 0;
149}
150
151static int mc13783_pcm_hw_params_sync(struct snd_pcm_substream *substream,
152 struct snd_pcm_hw_params *params,
153 struct snd_soc_dai *dai)
154{
155 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
156 return mc13783_pcm_hw_params_dac(substream, params, dai);
157 else
158 return mc13783_pcm_hw_params_codec(substream, params, dai);
159}
160
161static int mc13783_set_fmt(struct snd_soc_dai *dai, unsigned int fmt,
162 unsigned int reg)
163{
164 struct snd_soc_codec *codec = dai->codec;
165 unsigned int val = 0;
166 unsigned int mask = AUDIO_CFS(3) | AUDIO_BCL_INV | AUDIO_CFS_INV |
167 AUDIO_CSM | AUDIO_C_CLK_EN | AUDIO_C_RESET;
168
169
170 /* DAI mode */
171 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
172 case SND_SOC_DAIFMT_I2S:
173 val |= AUDIO_CFS(2);
174 break;
175 case SND_SOC_DAIFMT_DSP_A:
176 val |= AUDIO_CFS(1);
177 break;
178 default:
179 return -EINVAL;
180 }
181
182 /* DAI clock inversion */
183 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
184 case SND_SOC_DAIFMT_NB_NF:
185 val |= AUDIO_BCL_INV;
186 break;
187 case SND_SOC_DAIFMT_NB_IF:
188 val |= AUDIO_BCL_INV | AUDIO_CFS_INV;
189 break;
190 case SND_SOC_DAIFMT_IB_NF:
191 break;
192 case SND_SOC_DAIFMT_IB_IF:
193 val |= AUDIO_CFS_INV;
194 break;
195 }
196
197 /* DAI clock master masks */
198 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
199 case SND_SOC_DAIFMT_CBM_CFM:
200 val |= AUDIO_C_CLK_EN;
201 break;
202 case SND_SOC_DAIFMT_CBS_CFS:
203 val |= AUDIO_CSM;
204 break;
205 case SND_SOC_DAIFMT_CBM_CFS:
206 case SND_SOC_DAIFMT_CBS_CFM:
207 return -EINVAL;
208 }
209
210 val |= AUDIO_C_RESET;
211
212 snd_soc_update_bits(codec, reg, mask, val);
213
214 return 0;
215}
216
217static int mc13783_set_fmt_async(struct snd_soc_dai *dai, unsigned int fmt)
218{
219 if (dai->id == MC13783_ID_STEREO_DAC)
220 return mc13783_set_fmt(dai, fmt, MC13783_AUDIO_DAC);
221 else
222 return mc13783_set_fmt(dai, fmt, MC13783_AUDIO_CODEC);
223}
224
225static int mc13783_set_fmt_sync(struct snd_soc_dai *dai, unsigned int fmt)
226{
227 int ret;
228
229 ret = mc13783_set_fmt(dai, fmt, MC13783_AUDIO_DAC);
230 if (ret)
231 return ret;
232
233 /*
234 * In synchronous mode force the voice codec into slave mode
235 * so that the clock / framesync from the stereo DAC is used
236 */
237 fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
238 fmt |= SND_SOC_DAIFMT_CBS_CFS;
239 ret = mc13783_set_fmt(dai, fmt, MC13783_AUDIO_CODEC);
240
241 return ret;
242}
243
244static int mc13783_sysclk[] = {
245 13000000,
246 15360000,
247 16800000,
248 -1,
249 26000000,
250 -1, /* 12000000, invalid for voice codec */
251 -1, /* 3686400, invalid for voice codec */
252 33600000,
253};
254
255static int mc13783_set_sysclk(struct snd_soc_dai *dai,
256 int clk_id, unsigned int freq, int dir,
257 unsigned int reg)
258{
259 struct snd_soc_codec *codec = dai->codec;
260 int clk;
261 unsigned int val = 0;
262 unsigned int mask = AUDIO_CLK(0x7) | AUDIO_CLK_SEL;
263
264 for (clk = 0; clk < ARRAY_SIZE(mc13783_sysclk); clk++) {
265 if (mc13783_sysclk[clk] < 0)
266 continue;
267 if (mc13783_sysclk[clk] == freq)
268 break;
269 }
270
271 if (clk == ARRAY_SIZE(mc13783_sysclk))
272 return -EINVAL;
273
274 if (clk_id == MC13783_CLK_CLIB)
275 val |= AUDIO_CLK_SEL;
276
277 val |= AUDIO_CLK(clk);
278
279 snd_soc_update_bits(codec, reg, mask, val);
280
281 return 0;
282}
283
284static int mc13783_set_sysclk_dac(struct snd_soc_dai *dai,
285 int clk_id, unsigned int freq, int dir)
286{
287 return mc13783_set_sysclk(dai, clk_id, freq, dir, MC13783_AUDIO_DAC);
288}
289
290static int mc13783_set_sysclk_codec(struct snd_soc_dai *dai,
291 int clk_id, unsigned int freq, int dir)
292{
293 return mc13783_set_sysclk(dai, clk_id, freq, dir, MC13783_AUDIO_CODEC);
294}
295
296static int mc13783_set_sysclk_sync(struct snd_soc_dai *dai,
297 int clk_id, unsigned int freq, int dir)
298{
299 int ret;
300
301 ret = mc13783_set_sysclk(dai, clk_id, freq, dir, MC13783_AUDIO_DAC);
302 if (ret)
303 return ret;
304
305 return mc13783_set_sysclk(dai, clk_id, freq, dir, MC13783_AUDIO_CODEC);
306}
307
308static int mc13783_set_tdm_slot_dac(struct snd_soc_dai *dai,
309 unsigned int tx_mask, unsigned int rx_mask, int slots,
310 int slot_width)
311{
312 struct snd_soc_codec *codec = dai->codec;
313 unsigned int val = 0;
314 unsigned int mask = SSI_NETWORK_DAC_SLOT_MASK |
315 SSI_NETWORK_DAC_RXSLOT_MASK;
316
317 switch (slots) {
318 case 2:
319 val |= SSI_NETWORK_DAC_SLOTS_2;
320 break;
321 case 4:
322 val |= SSI_NETWORK_DAC_SLOTS_4;
323 break;
324 case 8:
325 val |= SSI_NETWORK_DAC_SLOTS_8;
326 break;
327 default:
328 return -EINVAL;
329 }
330
331 switch (rx_mask) {
332 case 0xfffffffc:
333 val |= SSI_NETWORK_DAC_RXSLOT_0_1;
334 break;
335 case 0xfffffff3:
336 val |= SSI_NETWORK_DAC_RXSLOT_2_3;
337 break;
338 case 0xffffffcf:
339 val |= SSI_NETWORK_DAC_RXSLOT_4_5;
340 break;
341 case 0xffffff3f:
342 val |= SSI_NETWORK_DAC_RXSLOT_6_7;
343 break;
344 default:
345 return -EINVAL;
346 };
347
348 snd_soc_update_bits(codec, MC13783_SSI_NETWORK, mask, val);
349
350 return 0;
351}
352
353static int mc13783_set_tdm_slot_codec(struct snd_soc_dai *dai,
354 unsigned int tx_mask, unsigned int rx_mask, int slots,
355 int slot_width)
356{
357 struct snd_soc_codec *codec = dai->codec;
358 unsigned int val = 0;
359 unsigned int mask = 0x3f;
360
361 if (slots != 4)
362 return -EINVAL;
363
364 if (tx_mask != 0xfffffffc)
365 return -EINVAL;
366
367 val |= (0x00 << 2); /* primary timeslot RX/TX(?) is 0 */
368 val |= (0x01 << 4); /* secondary timeslot TX is 1 */
369
370 snd_soc_update_bits(codec, MC13783_SSI_NETWORK, mask, val);
371
372 return 0;
373}
374
375static int mc13783_set_tdm_slot_sync(struct snd_soc_dai *dai,
376 unsigned int tx_mask, unsigned int rx_mask, int slots,
377 int slot_width)
378{
379 int ret;
380
381 ret = mc13783_set_tdm_slot_dac(dai, tx_mask, rx_mask, slots,
382 slot_width);
383 if (ret)
384 return ret;
385
386 ret = mc13783_set_tdm_slot_codec(dai, tx_mask, rx_mask, slots,
387 slot_width);
388
389 return ret;
390}
391
392static const struct snd_kcontrol_new mc1l_amp_ctl =
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200393 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_TX, 7, 1, 0);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200394
395static const struct snd_kcontrol_new mc1r_amp_ctl =
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200396 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_TX, 5, 1, 0);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200397
398static const struct snd_kcontrol_new mc2_amp_ctl =
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200399 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_TX, 9, 1, 0);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200400
401static const struct snd_kcontrol_new atx_amp_ctl =
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200402 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_TX, 11, 1, 0);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200403
404
405/* Virtual mux. The chip does the input selection automatically
406 * as soon as we enable one input. */
407static const char * const adcl_enum_text[] = {
408 "MC1L", "RXINL",
409};
410
411static const struct soc_enum adcl_enum =
412 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(adcl_enum_text), adcl_enum_text);
413
414static const struct snd_kcontrol_new left_input_mux =
415 SOC_DAPM_ENUM_VIRT("Route", adcl_enum);
416
417static const char * const adcr_enum_text[] = {
418 "MC1R", "MC2", "RXINR", "TXIN",
419};
420
421static const struct soc_enum adcr_enum =
422 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(adcr_enum_text), adcr_enum_text);
423
424static const struct snd_kcontrol_new right_input_mux =
425 SOC_DAPM_ENUM_VIRT("Route", adcr_enum);
426
427static const struct snd_kcontrol_new samp_ctl =
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200428 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 3, 1, 0);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200429
430static const struct snd_kcontrol_new lamp_ctl =
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200431 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 5, 1, 0);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200432
433static const struct snd_kcontrol_new hlamp_ctl =
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200434 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 10, 1, 0);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200435
436static const struct snd_kcontrol_new hramp_ctl =
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200437 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 9, 1, 0);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200438
439static const struct snd_kcontrol_new llamp_ctl =
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200440 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 16, 1, 0);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200441
442static const struct snd_kcontrol_new lramp_ctl =
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200443 SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 15, 1, 0);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200444
445static const struct snd_soc_dapm_widget mc13783_dapm_widgets[] = {
446/* Input */
447 SND_SOC_DAPM_INPUT("MC1LIN"),
448 SND_SOC_DAPM_INPUT("MC1RIN"),
449 SND_SOC_DAPM_INPUT("MC2IN"),
450 SND_SOC_DAPM_INPUT("RXINR"),
451 SND_SOC_DAPM_INPUT("RXINL"),
452 SND_SOC_DAPM_INPUT("TXIN"),
453
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200454 SND_SOC_DAPM_SUPPLY("MC1 Bias", MC13783_AUDIO_TX, 0, 0, NULL, 0),
455 SND_SOC_DAPM_SUPPLY("MC2 Bias", MC13783_AUDIO_TX, 1, 0, NULL, 0),
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200456
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200457 SND_SOC_DAPM_SWITCH("MC1L Amp", MC13783_AUDIO_TX, 7, 0, &mc1l_amp_ctl),
458 SND_SOC_DAPM_SWITCH("MC1R Amp", MC13783_AUDIO_TX, 5, 0, &mc1r_amp_ctl),
459 SND_SOC_DAPM_SWITCH("MC2 Amp", MC13783_AUDIO_TX, 9, 0, &mc2_amp_ctl),
460 SND_SOC_DAPM_SWITCH("TXIN Amp", MC13783_AUDIO_TX, 11, 0, &atx_amp_ctl),
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200461
462 SND_SOC_DAPM_VIRT_MUX("PGA Left Input Mux", SND_SOC_NOPM, 0, 0,
463 &left_input_mux),
464 SND_SOC_DAPM_VIRT_MUX("PGA Right Input Mux", SND_SOC_NOPM, 0, 0,
465 &right_input_mux),
466
467 SND_SOC_DAPM_PGA("PGA Left Input", SND_SOC_NOPM, 0, 0, NULL, 0),
468 SND_SOC_DAPM_PGA("PGA Right Input", SND_SOC_NOPM, 0, 0, NULL, 0),
469
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200470 SND_SOC_DAPM_ADC("ADC", "Capture", MC13783_AUDIO_CODEC, 11, 0),
471 SND_SOC_DAPM_SUPPLY("ADC_Reset", MC13783_AUDIO_CODEC, 15, 0, NULL, 0),
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200472
473/* Output */
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200474 SND_SOC_DAPM_SUPPLY("DAC_E", MC13783_AUDIO_DAC, 11, 0, NULL, 0),
475 SND_SOC_DAPM_SUPPLY("DAC_Reset", MC13783_AUDIO_DAC, 15, 0, NULL, 0),
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200476 SND_SOC_DAPM_OUTPUT("RXOUTL"),
477 SND_SOC_DAPM_OUTPUT("RXOUTR"),
478 SND_SOC_DAPM_OUTPUT("HSL"),
479 SND_SOC_DAPM_OUTPUT("HSR"),
480 SND_SOC_DAPM_OUTPUT("LSP"),
481 SND_SOC_DAPM_OUTPUT("SP"),
482
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200483 SND_SOC_DAPM_SWITCH("Speaker Amp", MC13783_AUDIO_RX0, 3, 0, &samp_ctl),
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200484 SND_SOC_DAPM_SWITCH("Loudspeaker Amp", SND_SOC_NOPM, 0, 0, &lamp_ctl),
Gaëtan Carlier6d97c092012-09-06 09:40:12 +0200485 SND_SOC_DAPM_SWITCH("Headset Amp Left", MC13783_AUDIO_RX0, 10, 0,
486 &hlamp_ctl),
487 SND_SOC_DAPM_SWITCH("Headset Amp Right", MC13783_AUDIO_RX0, 9, 0,
488 &hramp_ctl),
489 SND_SOC_DAPM_SWITCH("Line out Amp Left", MC13783_AUDIO_RX0, 16, 0,
490 &llamp_ctl),
491 SND_SOC_DAPM_SWITCH("Line out Amp Right", MC13783_AUDIO_RX0, 15, 0,
492 &lramp_ctl),
493 SND_SOC_DAPM_DAC("DAC", "Playback", MC13783_AUDIO_RX0, 22, 0),
494 SND_SOC_DAPM_PGA("DAC PGA", MC13783_AUDIO_RX1, 5, 0, NULL, 0),
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200495};
496
497static struct snd_soc_dapm_route mc13783_routes[] = {
498/* Input */
499 { "MC1L Amp", NULL, "MC1LIN"},
500 { "MC1R Amp", NULL, "MC1RIN" },
501 { "MC2 Amp", NULL, "MC2IN" },
502 { "TXIN Amp", NULL, "TXIN"},
503
504 { "PGA Left Input Mux", "MC1L", "MC1L Amp" },
505 { "PGA Left Input Mux", "RXINL", "RXINL"},
506 { "PGA Right Input Mux", "MC1R", "MC1R Amp" },
507 { "PGA Right Input Mux", "MC2", "MC2 Amp"},
508 { "PGA Right Input Mux", "TXIN", "TXIN Amp"},
509 { "PGA Right Input Mux", "RXINR", "RXINR"},
510
511 { "PGA Left Input", NULL, "PGA Left Input Mux"},
512 { "PGA Right Input", NULL, "PGA Right Input Mux"},
513
514 { "ADC", NULL, "PGA Left Input"},
515 { "ADC", NULL, "PGA Right Input"},
516 { "ADC", NULL, "ADC_Reset"},
517
518/* Output */
519 { "HSL", NULL, "Headset Amp Left" },
520 { "HSR", NULL, "Headset Amp Right"},
521 { "RXOUTL", NULL, "Line out Amp Left"},
522 { "RXOUTR", NULL, "Line out Amp Right"},
523 { "SP", NULL, "Speaker Amp"},
524 { "Speaker Amp", NULL, "DAC PGA"},
525 { "LSP", NULL, "DAC PGA"},
526 { "Headset Amp Left", NULL, "DAC PGA"},
527 { "Headset Amp Right", NULL, "DAC PGA"},
528 { "Line out Amp Left", NULL, "DAC PGA"},
529 { "Line out Amp Right", NULL, "DAC PGA"},
530 { "DAC PGA", NULL, "DAC"},
531 { "DAC", NULL, "DAC_E"},
532};
533
534static const char * const mc13783_3d_mixer[] = {"Stereo", "Phase Mix",
535 "Mono", "Mono Mix"};
536
537static const struct soc_enum mc13783_enum_3d_mixer =
538 SOC_ENUM_SINGLE(MC13783_AUDIO_RX1, 16, ARRAY_SIZE(mc13783_3d_mixer),
539 mc13783_3d_mixer);
540
541static struct snd_kcontrol_new mc13783_control_list[] = {
542 SOC_SINGLE("Loudspeaker enable", MC13783_AUDIO_RX0, 5, 1, 0),
543 SOC_SINGLE("PCM Playback Volume", MC13783_AUDIO_RX1, 6, 15, 0),
544 SOC_DOUBLE("PCM Capture Volume", MC13783_AUDIO_TX, 19, 14, 31, 0),
545 SOC_ENUM("3D Control", mc13783_enum_3d_mixer),
546};
547
548static int mc13783_probe(struct snd_soc_codec *codec)
549{
550 struct mc13783_priv *priv = snd_soc_codec_get_drvdata(codec);
Mark Brown2d9215c2013-09-18 19:04:17 +0100551 int ret;
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200552
Mark Brown2d9215c2013-09-18 19:04:17 +0100553 codec->control_data = dev_get_regmap(codec->dev->parent, NULL);
554 ret = snd_soc_codec_set_cache_io(codec, 8, 24, SND_SOC_REGMAP);
555 if (ret != 0) {
556 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
557 return ret;
558 }
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200559
560 /* these are the reset values */
561 mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_RX0, 0x25893);
562 mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_RX1, 0x00d35A);
563 mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_TX, 0x420000);
564 mc13xxx_reg_write(priv->mc13xxx, MC13783_SSI_NETWORK, 0x013060);
565 mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_CODEC, 0x180027);
566 mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_DAC, 0x0e0004);
567
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200568 if (priv->adc_ssi_port == MC13783_SSI1_PORT)
569 mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_CODEC,
570 AUDIO_SSI_SEL, 0);
571 else
572 mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_CODEC,
573 0, AUDIO_SSI_SEL);
574
575 if (priv->dac_ssi_port == MC13783_SSI1_PORT)
576 mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_DAC,
577 AUDIO_SSI_SEL, 0);
578 else
579 mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_DAC,
580 0, AUDIO_SSI_SEL);
581
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200582 return 0;
583}
584
585static int mc13783_remove(struct snd_soc_codec *codec)
586{
587 struct mc13783_priv *priv = snd_soc_codec_get_drvdata(codec);
588
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200589 /* Make sure VAUDIOON is off */
590 mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_RX0, 0x3, 0);
591
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200592 return 0;
593}
594
595#define MC13783_RATES_RECORD (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000)
596
597#define MC13783_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
598 SNDRV_PCM_FMTBIT_S24_LE)
599
600static struct snd_soc_dai_ops mc13783_ops_dac = {
601 .hw_params = mc13783_pcm_hw_params_dac,
602 .set_fmt = mc13783_set_fmt_async,
603 .set_sysclk = mc13783_set_sysclk_dac,
604 .set_tdm_slot = mc13783_set_tdm_slot_dac,
605};
606
607static struct snd_soc_dai_ops mc13783_ops_codec = {
608 .hw_params = mc13783_pcm_hw_params_codec,
609 .set_fmt = mc13783_set_fmt_async,
610 .set_sysclk = mc13783_set_sysclk_codec,
611 .set_tdm_slot = mc13783_set_tdm_slot_codec,
612};
613
614/*
615 * The mc13783 has two SSI ports, both of them can be routed either
616 * to the voice codec or the stereo DAC. When two different SSI ports
617 * are used for the voice codec and the stereo DAC we can do different
618 * formats and sysclock settings for playback and capture
619 * (mc13783-hifi-playback and mc13783-hifi-capture). Using the same port
620 * forces us to use symmetric rates (mc13783-hifi).
621 */
622static struct snd_soc_dai_driver mc13783_dai_async[] = {
623 {
624 .name = "mc13783-hifi-playback",
625 .id = MC13783_ID_STEREO_DAC,
626 .playback = {
627 .stream_name = "Playback",
Fabio Estevam37f45cc52012-09-03 13:04:13 -0300628 .channels_min = 2,
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200629 .channels_max = 2,
630 .rates = SNDRV_PCM_RATE_8000_96000,
631 .formats = MC13783_FORMATS,
632 },
633 .ops = &mc13783_ops_dac,
634 }, {
635 .name = "mc13783-hifi-capture",
636 .id = MC13783_ID_STEREO_CODEC,
637 .capture = {
638 .stream_name = "Capture",
Fabio Estevam37f45cc52012-09-03 13:04:13 -0300639 .channels_min = 2,
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200640 .channels_max = 2,
641 .rates = MC13783_RATES_RECORD,
642 .formats = MC13783_FORMATS,
643 },
644 .ops = &mc13783_ops_codec,
645 },
646};
647
648static struct snd_soc_dai_ops mc13783_ops_sync = {
649 .hw_params = mc13783_pcm_hw_params_sync,
650 .set_fmt = mc13783_set_fmt_sync,
651 .set_sysclk = mc13783_set_sysclk_sync,
652 .set_tdm_slot = mc13783_set_tdm_slot_sync,
653};
654
655static struct snd_soc_dai_driver mc13783_dai_sync[] = {
656 {
657 .name = "mc13783-hifi",
658 .id = MC13783_ID_SYNC,
659 .playback = {
660 .stream_name = "Playback",
Fabio Estevam37f45cc52012-09-03 13:04:13 -0300661 .channels_min = 2,
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200662 .channels_max = 2,
663 .rates = SNDRV_PCM_RATE_8000_96000,
664 .formats = MC13783_FORMATS,
665 },
666 .capture = {
667 .stream_name = "Capture",
Fabio Estevam37f45cc52012-09-03 13:04:13 -0300668 .channels_min = 2,
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200669 .channels_max = 2,
670 .rates = MC13783_RATES_RECORD,
671 .formats = MC13783_FORMATS,
672 },
673 .ops = &mc13783_ops_sync,
674 .symmetric_rates = 1,
675 }
676};
677
678static struct snd_soc_codec_driver soc_codec_dev_mc13783 = {
679 .probe = mc13783_probe,
680 .remove = mc13783_remove,
Philippe Rétornazb77458d2012-05-16 12:49:19 +0200681 .controls = mc13783_control_list,
682 .num_controls = ARRAY_SIZE(mc13783_control_list),
683 .dapm_widgets = mc13783_dapm_widgets,
684 .num_dapm_widgets = ARRAY_SIZE(mc13783_dapm_widgets),
685 .dapm_routes = mc13783_routes,
686 .num_dapm_routes = ARRAY_SIZE(mc13783_routes),
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200687};
688
689static int mc13783_codec_probe(struct platform_device *pdev)
690{
691 struct mc13xxx *mc13xxx;
692 struct mc13783_priv *priv;
693 struct mc13xxx_codec_platform_data *pdata = pdev->dev.platform_data;
694 int ret;
695
696 mc13xxx = dev_get_drvdata(pdev->dev.parent);
697
698
699 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
700 if (priv == NULL)
701 return -ENOMEM;
702
703 dev_set_drvdata(&pdev->dev, priv);
704 priv->mc13xxx = mc13xxx;
705 if (pdata) {
706 priv->adc_ssi_port = pdata->adc_ssi_port;
707 priv->dac_ssi_port = pdata->dac_ssi_port;
708 } else {
709 priv->adc_ssi_port = MC13783_SSI1_PORT;
710 priv->dac_ssi_port = MC13783_SSI2_PORT;
711 }
712
713 if (priv->adc_ssi_port == priv->dac_ssi_port)
714 ret = snd_soc_register_codec(&pdev->dev, &soc_codec_dev_mc13783,
715 mc13783_dai_sync, ARRAY_SIZE(mc13783_dai_sync));
716 else
717 ret = snd_soc_register_codec(&pdev->dev, &soc_codec_dev_mc13783,
718 mc13783_dai_async, ARRAY_SIZE(mc13783_dai_async));
719
720 if (ret)
721 goto err_register_codec;
722
723 return 0;
724
725err_register_codec:
726 dev_err(&pdev->dev, "register codec failed with %d\n", ret);
727
728 return ret;
729}
730
731static int mc13783_codec_remove(struct platform_device *pdev)
732{
733 snd_soc_unregister_codec(&pdev->dev);
734
735 return 0;
736}
737
738static struct platform_driver mc13783_codec_driver = {
739 .driver = {
740 .name = "mc13783-codec",
741 .owner = THIS_MODULE,
742 },
743 .probe = mc13783_codec_probe,
Bill Pemberton7a79e942012-12-07 09:26:37 -0500744 .remove = mc13783_codec_remove,
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200745};
746
Philippe Rétornazb77458d2012-05-16 12:49:19 +0200747module_platform_driver(mc13783_codec_driver);
Philippe Rétornaz8b908b82012-05-15 13:53:50 +0200748
749MODULE_DESCRIPTION("ASoC MC13783 driver");
750MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
751MODULE_AUTHOR("Philippe Retornaz <philippe.retornaz@epfl.ch>");
752MODULE_LICENSE("GPL");