blob: 77408af5d38b0869bb66fd5f97cbbde7628e88cb [file] [log] [blame]
Emilio López45fb6b62015-09-12 15:26:24 +02001/*
2 * Copyright 2014 Emilio López <emilio@elopez.com.ar>
3 * Copyright 2014 Jon Smirl <jonsmirl@gmail.com>
4 * Copyright 2015 Maxime Ripard <maxime.ripard@free-electrons.com>
5 *
6 * Based on the Allwinner SDK driver, released under the GPL.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
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
19#include <linux/init.h>
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/platform_device.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/of.h>
26#include <linux/of_platform.h>
27#include <linux/of_address.h>
28#include <linux/clk.h>
29#include <linux/regmap.h>
30
31#include <sound/core.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/soc.h>
35#include <sound/tlv.h>
36#include <sound/initval.h>
37#include <sound/dmaengine_pcm.h>
38
39/* Codec DAC register offsets and bit fields */
40#define SUN4I_CODEC_DAC_DPC (0x00)
41#define SUN4I_CODEC_DAC_DPC_EN_DA (31)
42#define SUN4I_CODEC_DAC_DPC_DVOL (12)
43#define SUN4I_CODEC_DAC_FIFOC (0x04)
44#define SUN4I_CODEC_DAC_FIFOC_DAC_FS (29)
45#define SUN4I_CODEC_DAC_FIFOC_FIR_VERSION (28)
46#define SUN4I_CODEC_DAC_FIFOC_SEND_LASAT (26)
47#define SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE (24)
48#define SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT (21)
49#define SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL (8)
50#define SUN4I_CODEC_DAC_FIFOC_MONO_EN (6)
51#define SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS (5)
52#define SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN (4)
53#define SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH (0)
54#define SUN4I_CODEC_DAC_FIFOS (0x08)
55#define SUN4I_CODEC_DAC_TXDATA (0x0c)
56#define SUN4I_CODEC_DAC_ACTL (0x10)
57#define SUN4I_CODEC_DAC_ACTL_DACAENR (31)
58#define SUN4I_CODEC_DAC_ACTL_DACAENL (30)
59#define SUN4I_CODEC_DAC_ACTL_MIXEN (29)
60#define SUN4I_CODEC_DAC_ACTL_LDACLMIXS (15)
61#define SUN4I_CODEC_DAC_ACTL_RDACRMIXS (14)
62#define SUN4I_CODEC_DAC_ACTL_LDACRMIXS (13)
63#define SUN4I_CODEC_DAC_ACTL_DACPAS (8)
64#define SUN4I_CODEC_DAC_ACTL_MIXPAS (7)
65#define SUN4I_CODEC_DAC_ACTL_PA_MUTE (6)
66#define SUN4I_CODEC_DAC_ACTL_PA_VOL (0)
67#define SUN4I_CODEC_DAC_TUNE (0x14)
68#define SUN4I_CODEC_DAC_DEBUG (0x18)
69
70/* Codec ADC register offsets and bit fields */
71#define SUN4I_CODEC_ADC_FIFOC (0x1c)
72#define SUN4I_CODEC_ADC_FIFOC_EN_AD (28)
73#define SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE (24)
74#define SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL (8)
75#define SUN4I_CODEC_ADC_FIFOC_MONO_EN (7)
76#define SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS (6)
77#define SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN (4)
78#define SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH (0)
79#define SUN4I_CODEC_ADC_FIFOS (0x20)
80#define SUN4I_CODEC_ADC_RXDATA (0x24)
81#define SUN4I_CODEC_ADC_ACTL (0x28)
82#define SUN4I_CODEC_ADC_ACTL_ADC_R_EN (31)
83#define SUN4I_CODEC_ADC_ACTL_ADC_L_EN (30)
84#define SUN4I_CODEC_ADC_ACTL_PREG1EN (29)
85#define SUN4I_CODEC_ADC_ACTL_PREG2EN (28)
86#define SUN4I_CODEC_ADC_ACTL_VMICEN (27)
87#define SUN4I_CODEC_ADC_ACTL_VADCG (20)
88#define SUN4I_CODEC_ADC_ACTL_ADCIS (17)
89#define SUN4I_CODEC_ADC_ACTL_PA_EN (4)
90#define SUN4I_CODEC_ADC_ACTL_DDE (3)
91#define SUN4I_CODEC_ADC_DEBUG (0x2c)
92
93/* Other various ADC registers */
94#define SUN4I_CODEC_DAC_TXCNT (0x30)
95#define SUN4I_CODEC_ADC_RXCNT (0x34)
96#define SUN4I_CODEC_AC_SYS_VERI (0x38)
97#define SUN4I_CODEC_AC_MIC_PHONE_CAL (0x3c)
98
99struct sun4i_codec {
100 struct device *dev;
101 struct regmap *regmap;
102 struct clk *clk_apb;
103 struct clk *clk_module;
104
105 struct snd_dmaengine_dai_dma_data playback_dma_data;
106};
107
108static void sun4i_codec_start_playback(struct sun4i_codec *scodec)
109{
110 /*
111 * FIXME: according to the BSP, we might need to drive a PA
112 * GPIO high here on some boards
113 */
114
115 /* Flush TX FIFO */
116 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
117 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH),
118 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH));
119
120 /* Enable DAC DRQ */
121 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
122 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN),
123 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN));
124}
125
126static void sun4i_codec_stop_playback(struct sun4i_codec *scodec)
127{
128 /*
129 * FIXME: according to the BSP, we might need to drive a PA
130 * GPIO low here on some boards
131 */
132
133 /* Disable DAC DRQ */
134 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
135 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN),
136 0);
137}
138
139static int sun4i_codec_trigger(struct snd_pcm_substream *substream, int cmd,
140 struct snd_soc_dai *dai)
141{
142 struct snd_soc_pcm_runtime *rtd = substream->private_data;
143 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
144
145 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
146 return -ENOTSUPP;
147
148 switch (cmd) {
149 case SNDRV_PCM_TRIGGER_START:
150 case SNDRV_PCM_TRIGGER_RESUME:
151 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
152 sun4i_codec_start_playback(scodec);
153 break;
154
155 case SNDRV_PCM_TRIGGER_STOP:
156 case SNDRV_PCM_TRIGGER_SUSPEND:
157 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
158 sun4i_codec_stop_playback(scodec);
159 break;
160
161 default:
162 return -EINVAL;
163 }
164
165 return 0;
166}
167
168static int sun4i_codec_prepare(struct snd_pcm_substream *substream,
169 struct snd_soc_dai *dai)
170{
171 struct snd_soc_pcm_runtime *rtd = substream->private_data;
172 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
173 u32 val;
174
175 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
176 return -ENOTSUPP;
177
178 /* Flush the TX FIFO */
179 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
180 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH),
181 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH));
182
183 /* Set TX FIFO Empty Trigger Level */
184 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
185 0x3f << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL,
186 0xf << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL);
187
188 if (substream->runtime->rate > 32000)
189 /* Use 64 bits FIR filter */
190 val = 0;
191 else
192 /* Use 32 bits FIR filter */
193 val = BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION);
194
195 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
196 BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION),
197 val);
198
199 /* Send zeros when we have an underrun */
200 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
201 BIT(SUN4I_CODEC_DAC_FIFOC_SEND_LASAT),
202 0);
203
204 return 0;
205}
206
207static unsigned long sun4i_codec_get_mod_freq(struct snd_pcm_hw_params *params)
208{
209 unsigned int rate = params_rate(params);
210
211 switch (rate) {
212 case 176400:
213 case 88200:
214 case 44100:
215 case 33075:
216 case 22050:
217 case 14700:
218 case 11025:
219 case 7350:
220 return 22579200;
221
222 case 192000:
223 case 96000:
224 case 48000:
225 case 32000:
226 case 24000:
227 case 16000:
228 case 12000:
229 case 8000:
230 return 24576000;
231
232 default:
233 return 0;
234 }
235}
236
237static int sun4i_codec_get_hw_rate(struct snd_pcm_hw_params *params)
238{
239 unsigned int rate = params_rate(params);
240
241 switch (rate) {
242 case 192000:
243 case 176400:
244 return 6;
245
246 case 96000:
247 case 88200:
248 return 7;
249
250 case 48000:
251 case 44100:
252 return 0;
253
254 case 32000:
255 case 33075:
256 return 1;
257
258 case 24000:
259 case 22050:
260 return 2;
261
262 case 16000:
263 case 14700:
264 return 3;
265
266 case 12000:
267 case 11025:
268 return 4;
269
270 case 8000:
271 case 7350:
272 return 5;
273
274 default:
275 return -EINVAL;
276 }
277}
278
279static int sun4i_codec_hw_params(struct snd_pcm_substream *substream,
280 struct snd_pcm_hw_params *params,
281 struct snd_soc_dai *dai)
282{
283 struct snd_soc_pcm_runtime *rtd = substream->private_data;
284 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
285 unsigned long clk_freq;
Maxime Ripard98a4b662015-09-29 21:43:17 +0200286 int ret, hwrate;
Emilio López45fb6b62015-09-12 15:26:24 +0200287 u32 val;
288
289 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
290 return -ENOTSUPP;
291
292 clk_freq = sun4i_codec_get_mod_freq(params);
293 if (!clk_freq)
294 return -EINVAL;
295
Maxime Ripard98a4b662015-09-29 21:43:17 +0200296 ret = clk_set_rate(scodec->clk_module, clk_freq);
297 if (ret)
298 return ret;
Emilio López45fb6b62015-09-12 15:26:24 +0200299
300 hwrate = sun4i_codec_get_hw_rate(params);
301 if (hwrate < 0)
302 return hwrate;
303
304 /* Set DAC sample rate */
305 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
306 7 << SUN4I_CODEC_DAC_FIFOC_DAC_FS,
307 hwrate << SUN4I_CODEC_DAC_FIFOC_DAC_FS);
308
309 /* Set the number of channels we want to use */
310 if (params_channels(params) == 1)
311 val = BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN);
312 else
313 val = 0;
314
315 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
316 BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN),
317 val);
318
319 /* Set the number of sample bits to either 16 or 24 bits */
320 if (hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min == 32) {
321 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
322 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS),
323 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS));
324
325 /* Set TX FIFO mode to padding the LSBs with 0 */
326 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
327 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE),
328 0);
329
330 scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
331 } else {
332 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
333 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS),
334 0);
335
336 /* Set TX FIFO mode to repeat the MSB */
337 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
338 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE),
339 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE));
340
341 scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
342 }
343
344 return 0;
345}
346
347static int sun4i_codec_startup(struct snd_pcm_substream *substream,
348 struct snd_soc_dai *dai)
349{
350 struct snd_soc_pcm_runtime *rtd = substream->private_data;
351 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
352
353 /*
354 * Stop issuing DRQ when we have room for less than 16 samples
355 * in our TX FIFO
356 */
357 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
358 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT,
359 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT);
360
361 return clk_prepare_enable(scodec->clk_module);
362}
363
364static void sun4i_codec_shutdown(struct snd_pcm_substream *substream,
365 struct snd_soc_dai *dai)
366{
367 struct snd_soc_pcm_runtime *rtd = substream->private_data;
368 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
369
370 clk_disable_unprepare(scodec->clk_module);
371}
372
373static const struct snd_soc_dai_ops sun4i_codec_dai_ops = {
374 .startup = sun4i_codec_startup,
375 .shutdown = sun4i_codec_shutdown,
376 .trigger = sun4i_codec_trigger,
377 .hw_params = sun4i_codec_hw_params,
378 .prepare = sun4i_codec_prepare,
379};
380
381static struct snd_soc_dai_driver sun4i_codec_dai = {
382 .name = "Codec",
383 .ops = &sun4i_codec_dai_ops,
384 .playback = {
385 .stream_name = "Codec Playback",
386 .channels_min = 1,
387 .channels_max = 2,
388 .rate_min = 8000,
389 .rate_max = 192000,
390 .rates = SNDRV_PCM_RATE_8000_48000 |
391 SNDRV_PCM_RATE_96000 |
392 SNDRV_PCM_RATE_192000 |
393 SNDRV_PCM_RATE_KNOT,
394 .formats = SNDRV_PCM_FMTBIT_S16_LE |
395 SNDRV_PCM_FMTBIT_S32_LE,
396 .sig_bits = 24,
397 },
398};
399
400/*** Codec ***/
401static const struct snd_kcontrol_new sun4i_codec_pa_mute =
402 SOC_DAPM_SINGLE("Switch", SUN4I_CODEC_DAC_ACTL,
403 SUN4I_CODEC_DAC_ACTL_PA_MUTE, 1, 0);
404
405static DECLARE_TLV_DB_SCALE(sun4i_codec_pa_volume_scale, -6300, 100, 1);
406
407static const struct snd_kcontrol_new sun4i_codec_widgets[] = {
408 SOC_SINGLE_TLV("PA Volume", SUN4I_CODEC_DAC_ACTL,
409 SUN4I_CODEC_DAC_ACTL_PA_VOL, 0x3F, 0,
410 sun4i_codec_pa_volume_scale),
411};
412
413static const struct snd_kcontrol_new sun4i_codec_left_mixer_controls[] = {
414 SOC_DAPM_SINGLE("Left DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
415 SUN4I_CODEC_DAC_ACTL_LDACLMIXS, 1, 0),
416};
417
418static const struct snd_kcontrol_new sun4i_codec_right_mixer_controls[] = {
419 SOC_DAPM_SINGLE("Right DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
420 SUN4I_CODEC_DAC_ACTL_RDACRMIXS, 1, 0),
421 SOC_DAPM_SINGLE("Left DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
422 SUN4I_CODEC_DAC_ACTL_LDACRMIXS, 1, 0),
423};
424
425static const struct snd_kcontrol_new sun4i_codec_pa_mixer_controls[] = {
426 SOC_DAPM_SINGLE("DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
427 SUN4I_CODEC_DAC_ACTL_DACPAS, 1, 0),
428 SOC_DAPM_SINGLE("Mixer Playback Switch", SUN4I_CODEC_DAC_ACTL,
429 SUN4I_CODEC_DAC_ACTL_MIXPAS, 1, 0),
430};
431
432static const struct snd_soc_dapm_widget sun4i_codec_dapm_widgets[] = {
433 /* Digital parts of the DACs */
434 SND_SOC_DAPM_SUPPLY("DAC", SUN4I_CODEC_DAC_DPC,
435 SUN4I_CODEC_DAC_DPC_EN_DA, 0,
436 NULL, 0),
437
438 /* Analog parts of the DACs */
439 SND_SOC_DAPM_DAC("Left DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL,
440 SUN4I_CODEC_DAC_ACTL_DACAENL, 0),
441 SND_SOC_DAPM_DAC("Right DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL,
442 SUN4I_CODEC_DAC_ACTL_DACAENR, 0),
443
444 /* Mixers */
445 SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
446 sun4i_codec_left_mixer_controls,
447 ARRAY_SIZE(sun4i_codec_left_mixer_controls)),
448 SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
449 sun4i_codec_right_mixer_controls,
450 ARRAY_SIZE(sun4i_codec_right_mixer_controls)),
451
452 /* Global Mixer Enable */
453 SND_SOC_DAPM_SUPPLY("Mixer Enable", SUN4I_CODEC_DAC_ACTL,
454 SUN4I_CODEC_DAC_ACTL_MIXEN, 0, NULL, 0),
455
456 /* Pre-Amplifier */
457 SND_SOC_DAPM_MIXER("Pre-Amplifier", SUN4I_CODEC_ADC_ACTL,
458 SUN4I_CODEC_ADC_ACTL_PA_EN, 0,
459 sun4i_codec_pa_mixer_controls,
460 ARRAY_SIZE(sun4i_codec_pa_mixer_controls)),
461 SND_SOC_DAPM_SWITCH("Pre-Amplifier Mute", SND_SOC_NOPM, 0, 0,
462 &sun4i_codec_pa_mute),
463
464 SND_SOC_DAPM_OUTPUT("HP Right"),
465 SND_SOC_DAPM_OUTPUT("HP Left"),
466};
467
468static const struct snd_soc_dapm_route sun4i_codec_dapm_routes[] = {
469 /* Left DAC Routes */
470 { "Left DAC", NULL, "DAC" },
471
472 /* Right DAC Routes */
473 { "Right DAC", NULL, "DAC" },
474
475 /* Right Mixer Routes */
476 { "Right Mixer", NULL, "Mixer Enable" },
477 { "Right Mixer", "Left DAC Playback Switch", "Left DAC" },
478 { "Right Mixer", "Right DAC Playback Switch", "Right DAC" },
479
480 /* Left Mixer Routes */
481 { "Left Mixer", NULL, "Mixer Enable" },
482 { "Left Mixer", "Left DAC Playback Switch", "Left DAC" },
483
484 /* Pre-Amplifier Mixer Routes */
485 { "Pre-Amplifier", "Mixer Playback Switch", "Left Mixer" },
486 { "Pre-Amplifier", "Mixer Playback Switch", "Right Mixer" },
487 { "Pre-Amplifier", "DAC Playback Switch", "Left DAC" },
488 { "Pre-Amplifier", "DAC Playback Switch", "Right DAC" },
489
490 /* PA -> HP path */
491 { "Pre-Amplifier Mute", "Switch", "Pre-Amplifier" },
492 { "HP Right", NULL, "Pre-Amplifier Mute" },
493 { "HP Left", NULL, "Pre-Amplifier Mute" },
494};
495
496static struct snd_soc_codec_driver sun4i_codec_codec = {
497 .controls = sun4i_codec_widgets,
498 .num_controls = ARRAY_SIZE(sun4i_codec_widgets),
499 .dapm_widgets = sun4i_codec_dapm_widgets,
500 .num_dapm_widgets = ARRAY_SIZE(sun4i_codec_dapm_widgets),
501 .dapm_routes = sun4i_codec_dapm_routes,
502 .num_dapm_routes = ARRAY_SIZE(sun4i_codec_dapm_routes),
503};
504
505static const struct snd_soc_component_driver sun4i_codec_component = {
506 .name = "sun4i-codec",
507};
508
509#define SUN4I_CODEC_RATES SNDRV_PCM_RATE_8000_192000
510#define SUN4I_CODEC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
511 SNDRV_PCM_FMTBIT_S32_LE)
512
513static int sun4i_codec_dai_probe(struct snd_soc_dai *dai)
514{
515 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
516 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
517
518 snd_soc_dai_init_dma_data(dai, &scodec->playback_dma_data,
519 NULL);
520
521 return 0;
522}
523
524static struct snd_soc_dai_driver dummy_cpu_dai = {
525 .name = "sun4i-codec-cpu-dai",
526 .probe = sun4i_codec_dai_probe,
527 .playback = {
528 .stream_name = "Playback",
529 .channels_min = 1,
530 .channels_max = 2,
531 .rates = SUN4I_CODEC_RATES,
532 .formats = SUN4I_CODEC_FORMATS,
533 .sig_bits = 24,
534 },
535};
536
537static const struct regmap_config sun4i_codec_regmap_config = {
538 .reg_bits = 32,
539 .reg_stride = 4,
540 .val_bits = 32,
541 .max_register = SUN4I_CODEC_AC_MIC_PHONE_CAL,
542};
543
544static const struct of_device_id sun4i_codec_of_match[] = {
545 { .compatible = "allwinner,sun4i-a10-codec" },
546 { .compatible = "allwinner,sun7i-a20-codec" },
547 {}
548};
549MODULE_DEVICE_TABLE(of, sun4i_codec_of_match);
550
551static struct snd_soc_dai_link *sun4i_codec_create_link(struct device *dev,
552 int *num_links)
553{
554 struct snd_soc_dai_link *link = devm_kzalloc(dev, sizeof(*link),
555 GFP_KERNEL);
556 if (!link)
557 return NULL;
558
559 link->name = "cdc";
560 link->stream_name = "CDC PCM";
561 link->codec_dai_name = "Codec";
562 link->cpu_dai_name = dev_name(dev);
563 link->codec_name = dev_name(dev);
564 link->platform_name = dev_name(dev);
565 link->dai_fmt = SND_SOC_DAIFMT_I2S;
566
567 *num_links = 1;
568
569 return link;
570};
571
572static struct snd_soc_card *sun4i_codec_create_card(struct device *dev)
573{
574 struct snd_soc_card *card;
575 int ret;
576
577 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
578 if (!card)
579 return NULL;
580
581 card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
582 if (!card->dai_link)
583 return NULL;
584
585 card->dev = dev;
586 card->name = "sun4i-codec";
587
588 ret = snd_soc_of_parse_audio_routing(card, "routing");
589 if (ret) {
590 dev_err(dev, "Failed to create our audio routing\n");
591 return NULL;
592 }
593
594 return card;
595};
596
597static int sun4i_codec_probe(struct platform_device *pdev)
598{
599 struct snd_soc_card *card;
600 struct sun4i_codec *scodec;
601 struct resource *res;
602 void __iomem *base;
603 int ret;
604
605 scodec = devm_kzalloc(&pdev->dev, sizeof(*scodec), GFP_KERNEL);
606 if (!scodec)
607 return -ENOMEM;
608
609 scodec->dev = &pdev->dev;
610
611 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
612 base = devm_ioremap_resource(&pdev->dev, res);
613 if (IS_ERR(base)) {
614 dev_err(&pdev->dev, "Failed to map the registers\n");
615 return PTR_ERR(base);
616 }
617
618 scodec->regmap = devm_regmap_init_mmio(&pdev->dev, base,
619 &sun4i_codec_regmap_config);
620 if (IS_ERR(scodec->regmap)) {
621 dev_err(&pdev->dev, "Failed to create our regmap\n");
622 return PTR_ERR(scodec->regmap);
623 }
624
625 /* Get the clocks from the DT */
626 scodec->clk_apb = devm_clk_get(&pdev->dev, "apb");
627 if (IS_ERR(scodec->clk_apb)) {
628 dev_err(&pdev->dev, "Failed to get the APB clock\n");
629 return PTR_ERR(scodec->clk_apb);
630 }
631
632 scodec->clk_module = devm_clk_get(&pdev->dev, "codec");
633 if (IS_ERR(scodec->clk_module)) {
634 dev_err(&pdev->dev, "Failed to get the module clock\n");
635 return PTR_ERR(scodec->clk_module);
636 }
637
638 /* Enable the bus clock */
639 if (clk_prepare_enable(scodec->clk_apb)) {
640 dev_err(&pdev->dev, "Failed to enable the APB clock\n");
641 return -EINVAL;
642 }
643
644 /* DMA configuration for TX FIFO */
645 scodec->playback_dma_data.addr = res->start + SUN4I_CODEC_DAC_TXDATA;
646 scodec->playback_dma_data.maxburst = 4;
647 scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
648
649 ret = snd_soc_register_codec(&pdev->dev, &sun4i_codec_codec,
650 &sun4i_codec_dai, 1);
651 if (ret) {
652 dev_err(&pdev->dev, "Failed to register our codec\n");
653 goto err_clk_disable;
654 }
655
656 ret = devm_snd_soc_register_component(&pdev->dev,
657 &sun4i_codec_component,
658 &dummy_cpu_dai, 1);
659 if (ret) {
660 dev_err(&pdev->dev, "Failed to register our DAI\n");
661 goto err_unregister_codec;
662 }
663
664 ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
665 if (ret) {
666 dev_err(&pdev->dev, "Failed to register against DMAEngine\n");
667 goto err_unregister_codec;
668 }
669
670 card = sun4i_codec_create_card(&pdev->dev);
671 if (!card) {
672 dev_err(&pdev->dev, "Failed to create our card\n");
673 goto err_unregister_codec;
674 }
675
676 platform_set_drvdata(pdev, card);
677 snd_soc_card_set_drvdata(card, scodec);
678
679 ret = snd_soc_register_card(card);
680 if (ret) {
681 dev_err(&pdev->dev, "Failed to register our card\n");
682 goto err_unregister_codec;
683 }
684
685 return 0;
686
687err_unregister_codec:
688 snd_soc_unregister_codec(&pdev->dev);
689err_clk_disable:
690 clk_disable_unprepare(scodec->clk_apb);
691 return ret;
692}
693
694static int sun4i_codec_remove(struct platform_device *pdev)
695{
696 struct snd_soc_card *card = platform_get_drvdata(pdev);
697 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
698
699 snd_soc_unregister_card(card);
700 snd_soc_unregister_codec(&pdev->dev);
701 clk_disable_unprepare(scodec->clk_apb);
702
703 return 0;
704}
705
706static struct platform_driver sun4i_codec_driver = {
707 .driver = {
708 .name = "sun4i-codec",
Emilio López45fb6b62015-09-12 15:26:24 +0200709 .of_match_table = sun4i_codec_of_match,
710 },
711 .probe = sun4i_codec_probe,
712 .remove = sun4i_codec_remove,
713};
714module_platform_driver(sun4i_codec_driver);
715
716MODULE_DESCRIPTION("Allwinner A10 codec driver");
717MODULE_AUTHOR("Emilio López <emilio@elopez.com.ar>");
718MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
719MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
720MODULE_LICENSE("GPL");