blob: d867b96d367bd47145fa4d041ebfd80615f1fc85 [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>
Adam Sampson474d1472015-10-27 21:00:45 +00005 * Copyright 2015 Adam Sampson <ats@offog.org>
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08006 * Copyright 2016 Chen-Yu Tsai <wens@csie.org>
Emilio López45fb6b62015-09-12 15:26:24 +02007 *
8 * Based on the Allwinner SDK driver, released under the GPL.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 */
20
21#include <linux/init.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
25#include <linux/delay.h>
26#include <linux/slab.h>
27#include <linux/of.h>
Emilio López45fb6b62015-09-12 15:26:24 +020028#include <linux/of_address.h>
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +080029#include <linux/of_device.h>
30#include <linux/of_platform.h>
Emilio López45fb6b62015-09-12 15:26:24 +020031#include <linux/clk.h>
32#include <linux/regmap.h>
Hans de Goede40592622015-12-11 19:43:57 +010033#include <linux/gpio/consumer.h>
Emilio López45fb6b62015-09-12 15:26:24 +020034
35#include <sound/core.h>
36#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
39#include <sound/tlv.h>
40#include <sound/initval.h>
41#include <sound/dmaengine_pcm.h>
42
Chen-Yu Tsaibd720ec2016-11-03 15:55:45 +080043/* Codec DAC digital controls and FIFO registers */
Emilio López45fb6b62015-09-12 15:26:24 +020044#define SUN4I_CODEC_DAC_DPC (0x00)
45#define SUN4I_CODEC_DAC_DPC_EN_DA (31)
46#define SUN4I_CODEC_DAC_DPC_DVOL (12)
47#define SUN4I_CODEC_DAC_FIFOC (0x04)
48#define SUN4I_CODEC_DAC_FIFOC_DAC_FS (29)
49#define SUN4I_CODEC_DAC_FIFOC_FIR_VERSION (28)
50#define SUN4I_CODEC_DAC_FIFOC_SEND_LASAT (26)
51#define SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE (24)
52#define SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT (21)
53#define SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL (8)
54#define SUN4I_CODEC_DAC_FIFOC_MONO_EN (6)
55#define SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS (5)
56#define SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN (4)
57#define SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH (0)
58#define SUN4I_CODEC_DAC_FIFOS (0x08)
59#define SUN4I_CODEC_DAC_TXDATA (0x0c)
Chen-Yu Tsaibd720ec2016-11-03 15:55:45 +080060
61/* Codec DAC side analog signal controls */
Emilio López45fb6b62015-09-12 15:26:24 +020062#define SUN4I_CODEC_DAC_ACTL (0x10)
63#define SUN4I_CODEC_DAC_ACTL_DACAENR (31)
64#define SUN4I_CODEC_DAC_ACTL_DACAENL (30)
65#define SUN4I_CODEC_DAC_ACTL_MIXEN (29)
66#define SUN4I_CODEC_DAC_ACTL_LDACLMIXS (15)
67#define SUN4I_CODEC_DAC_ACTL_RDACRMIXS (14)
68#define SUN4I_CODEC_DAC_ACTL_LDACRMIXS (13)
69#define SUN4I_CODEC_DAC_ACTL_DACPAS (8)
70#define SUN4I_CODEC_DAC_ACTL_MIXPAS (7)
71#define SUN4I_CODEC_DAC_ACTL_PA_MUTE (6)
72#define SUN4I_CODEC_DAC_ACTL_PA_VOL (0)
73#define SUN4I_CODEC_DAC_TUNE (0x14)
74#define SUN4I_CODEC_DAC_DEBUG (0x18)
75
Chen-Yu Tsaibd720ec2016-11-03 15:55:45 +080076/* Codec ADC digital controls and FIFO registers */
Emilio López45fb6b62015-09-12 15:26:24 +020077#define SUN4I_CODEC_ADC_FIFOC (0x1c)
Maxime Ripard1fb34b42015-11-30 16:37:47 +010078#define SUN4I_CODEC_ADC_FIFOC_ADC_FS (29)
Emilio López45fb6b62015-09-12 15:26:24 +020079#define SUN4I_CODEC_ADC_FIFOC_EN_AD (28)
80#define SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE (24)
81#define SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL (8)
82#define SUN4I_CODEC_ADC_FIFOC_MONO_EN (7)
83#define SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS (6)
84#define SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN (4)
85#define SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH (0)
86#define SUN4I_CODEC_ADC_FIFOS (0x20)
87#define SUN4I_CODEC_ADC_RXDATA (0x24)
Chen-Yu Tsaibd720ec2016-11-03 15:55:45 +080088
89/* Codec ADC side analog signal controls */
Emilio López45fb6b62015-09-12 15:26:24 +020090#define SUN4I_CODEC_ADC_ACTL (0x28)
91#define SUN4I_CODEC_ADC_ACTL_ADC_R_EN (31)
92#define SUN4I_CODEC_ADC_ACTL_ADC_L_EN (30)
93#define SUN4I_CODEC_ADC_ACTL_PREG1EN (29)
94#define SUN4I_CODEC_ADC_ACTL_PREG2EN (28)
95#define SUN4I_CODEC_ADC_ACTL_VMICEN (27)
96#define SUN4I_CODEC_ADC_ACTL_VADCG (20)
97#define SUN4I_CODEC_ADC_ACTL_ADCIS (17)
98#define SUN4I_CODEC_ADC_ACTL_PA_EN (4)
99#define SUN4I_CODEC_ADC_ACTL_DDE (3)
100#define SUN4I_CODEC_ADC_DEBUG (0x2c)
101
Chen-Yu Tsaibd720ec2016-11-03 15:55:45 +0800102/* FIFO counters */
Emilio López45fb6b62015-09-12 15:26:24 +0200103#define SUN4I_CODEC_DAC_TXCNT (0x30)
104#define SUN4I_CODEC_ADC_RXCNT (0x34)
Chen-Yu Tsaibd720ec2016-11-03 15:55:45 +0800105
106/* Calibration register (sun7i only) */
Danny Milosavljevic4f0c4e92016-09-22 09:13:12 +0200107#define SUN7I_CODEC_AC_DAC_CAL (0x38)
Chen-Yu Tsaibd720ec2016-11-03 15:55:45 +0800108
109/* Microphone controls (sun7i only) */
Danny Milosavljevic4f0c4e92016-09-22 09:13:12 +0200110#define SUN7I_CODEC_AC_MIC_PHONE_CAL (0x3c)
Emilio López45fb6b62015-09-12 15:26:24 +0200111
112struct sun4i_codec {
113 struct device *dev;
114 struct regmap *regmap;
115 struct clk *clk_apb;
116 struct clk *clk_module;
Hans de Goede40592622015-12-11 19:43:57 +0100117 struct gpio_desc *gpio_pa;
Emilio López45fb6b62015-09-12 15:26:24 +0200118
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800119 /* ADC_FIFOC register is at different offset on different SoCs */
120 struct regmap_field *reg_adc_fifoc;
121
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100122 struct snd_dmaengine_dai_dma_data capture_dma_data;
Emilio López45fb6b62015-09-12 15:26:24 +0200123 struct snd_dmaengine_dai_dma_data playback_dma_data;
124};
125
126static void sun4i_codec_start_playback(struct sun4i_codec *scodec)
127{
Emilio López45fb6b62015-09-12 15:26:24 +0200128 /* Flush TX FIFO */
129 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
130 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH),
131 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH));
132
133 /* Enable DAC DRQ */
134 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
135 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN),
136 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN));
137}
138
139static void sun4i_codec_stop_playback(struct sun4i_codec *scodec)
140{
Emilio López45fb6b62015-09-12 15:26:24 +0200141 /* Disable DAC DRQ */
142 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
143 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN),
144 0);
145}
146
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100147static void sun4i_codec_start_capture(struct sun4i_codec *scodec)
148{
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100149 /* Enable ADC DRQ */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800150 regmap_field_update_bits(scodec->reg_adc_fifoc,
151 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN),
152 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN));
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100153}
154
155static void sun4i_codec_stop_capture(struct sun4i_codec *scodec)
156{
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100157 /* Disable ADC DRQ */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800158 regmap_field_update_bits(scodec->reg_adc_fifoc,
159 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN), 0);
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100160}
161
Emilio López45fb6b62015-09-12 15:26:24 +0200162static int sun4i_codec_trigger(struct snd_pcm_substream *substream, int cmd,
163 struct snd_soc_dai *dai)
164{
165 struct snd_soc_pcm_runtime *rtd = substream->private_data;
166 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
167
Emilio López45fb6b62015-09-12 15:26:24 +0200168 switch (cmd) {
169 case SNDRV_PCM_TRIGGER_START:
170 case SNDRV_PCM_TRIGGER_RESUME:
171 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100172 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
173 sun4i_codec_start_playback(scodec);
174 else
175 sun4i_codec_start_capture(scodec);
Emilio López45fb6b62015-09-12 15:26:24 +0200176 break;
177
178 case SNDRV_PCM_TRIGGER_STOP:
179 case SNDRV_PCM_TRIGGER_SUSPEND:
180 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100181 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
182 sun4i_codec_stop_playback(scodec);
183 else
184 sun4i_codec_stop_capture(scodec);
Emilio López45fb6b62015-09-12 15:26:24 +0200185 break;
186
187 default:
188 return -EINVAL;
189 }
190
191 return 0;
192}
193
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100194static int sun4i_codec_prepare_capture(struct snd_pcm_substream *substream,
195 struct snd_soc_dai *dai)
196{
197 struct snd_soc_pcm_runtime *rtd = substream->private_data;
198 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
199
200
201 /* Flush RX FIFO */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800202 regmap_field_update_bits(scodec->reg_adc_fifoc,
203 BIT(SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH),
204 BIT(SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH));
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100205
206
207 /* Set RX FIFO trigger level */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800208 regmap_field_update_bits(scodec->reg_adc_fifoc,
209 0xf << SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL,
210 0x7 << SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL);
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100211
212 /*
213 * FIXME: Undocumented in the datasheet, but
214 * Allwinner's code mentions that it is related
215 * related to microphone gain
216 */
217 regmap_update_bits(scodec->regmap, SUN4I_CODEC_ADC_ACTL,
218 0x3 << 25,
219 0x1 << 25);
220
221 if (of_device_is_compatible(scodec->dev->of_node,
222 "allwinner,sun7i-a20-codec"))
223 /* FIXME: Undocumented bits */
224 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_TUNE,
225 0x3 << 8,
226 0x1 << 8);
227
228 /* Fill most significant bits with valid data MSB */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800229 regmap_field_update_bits(scodec->reg_adc_fifoc,
230 BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE),
231 BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE));
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100232
233 return 0;
234}
235
236static int sun4i_codec_prepare_playback(struct snd_pcm_substream *substream,
237 struct snd_soc_dai *dai)
Emilio López45fb6b62015-09-12 15:26:24 +0200238{
239 struct snd_soc_pcm_runtime *rtd = substream->private_data;
240 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
241 u32 val;
242
Emilio López45fb6b62015-09-12 15:26:24 +0200243 /* Flush the TX FIFO */
244 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
245 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH),
246 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH));
247
248 /* Set TX FIFO Empty Trigger Level */
249 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
250 0x3f << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL,
251 0xf << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL);
252
253 if (substream->runtime->rate > 32000)
254 /* Use 64 bits FIR filter */
255 val = 0;
256 else
257 /* Use 32 bits FIR filter */
258 val = BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION);
259
260 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
261 BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION),
262 val);
263
264 /* Send zeros when we have an underrun */
265 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
266 BIT(SUN4I_CODEC_DAC_FIFOC_SEND_LASAT),
267 0);
268
269 return 0;
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100270};
271
272static int sun4i_codec_prepare(struct snd_pcm_substream *substream,
273 struct snd_soc_dai *dai)
274{
275 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
276 return sun4i_codec_prepare_playback(substream, dai);
277
278 return sun4i_codec_prepare_capture(substream, dai);
Emilio López45fb6b62015-09-12 15:26:24 +0200279}
280
281static unsigned long sun4i_codec_get_mod_freq(struct snd_pcm_hw_params *params)
282{
283 unsigned int rate = params_rate(params);
284
285 switch (rate) {
286 case 176400:
287 case 88200:
288 case 44100:
289 case 33075:
290 case 22050:
291 case 14700:
292 case 11025:
293 case 7350:
294 return 22579200;
295
296 case 192000:
297 case 96000:
298 case 48000:
299 case 32000:
300 case 24000:
301 case 16000:
302 case 12000:
303 case 8000:
304 return 24576000;
305
306 default:
307 return 0;
308 }
309}
310
311static int sun4i_codec_get_hw_rate(struct snd_pcm_hw_params *params)
312{
313 unsigned int rate = params_rate(params);
314
315 switch (rate) {
316 case 192000:
317 case 176400:
318 return 6;
319
320 case 96000:
321 case 88200:
322 return 7;
323
324 case 48000:
325 case 44100:
326 return 0;
327
328 case 32000:
329 case 33075:
330 return 1;
331
332 case 24000:
333 case 22050:
334 return 2;
335
336 case 16000:
337 case 14700:
338 return 3;
339
340 case 12000:
341 case 11025:
342 return 4;
343
344 case 8000:
345 case 7350:
346 return 5;
347
348 default:
349 return -EINVAL;
350 }
351}
352
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100353static int sun4i_codec_hw_params_capture(struct sun4i_codec *scodec,
354 struct snd_pcm_hw_params *params,
355 unsigned int hwrate)
Emilio López45fb6b62015-09-12 15:26:24 +0200356{
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100357 /* Set ADC sample rate */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800358 regmap_field_update_bits(scodec->reg_adc_fifoc,
359 7 << SUN4I_CODEC_ADC_FIFOC_ADC_FS,
360 hwrate << SUN4I_CODEC_ADC_FIFOC_ADC_FS);
Emilio López45fb6b62015-09-12 15:26:24 +0200361
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100362 /* Set the number of channels we want to use */
363 if (params_channels(params) == 1)
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800364 regmap_field_update_bits(scodec->reg_adc_fifoc,
365 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN),
366 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN));
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100367 else
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800368 regmap_field_update_bits(scodec->reg_adc_fifoc,
369 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN),
370 0);
Emilio López45fb6b62015-09-12 15:26:24 +0200371
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100372 return 0;
373}
Emilio López45fb6b62015-09-12 15:26:24 +0200374
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100375static int sun4i_codec_hw_params_playback(struct sun4i_codec *scodec,
376 struct snd_pcm_hw_params *params,
377 unsigned int hwrate)
378{
379 u32 val;
Emilio López45fb6b62015-09-12 15:26:24 +0200380
381 /* Set DAC sample rate */
382 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
383 7 << SUN4I_CODEC_DAC_FIFOC_DAC_FS,
384 hwrate << SUN4I_CODEC_DAC_FIFOC_DAC_FS);
385
386 /* Set the number of channels we want to use */
387 if (params_channels(params) == 1)
388 val = BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN);
389 else
390 val = 0;
391
392 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
393 BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN),
394 val);
395
396 /* Set the number of sample bits to either 16 or 24 bits */
397 if (hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min == 32) {
398 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
399 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS),
400 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS));
401
402 /* Set TX FIFO mode to padding the LSBs with 0 */
403 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
404 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE),
405 0);
406
407 scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
408 } else {
409 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
410 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS),
411 0);
412
413 /* Set TX FIFO mode to repeat the MSB */
414 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
415 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE),
416 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE));
417
418 scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
419 }
420
421 return 0;
422}
423
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100424static int sun4i_codec_hw_params(struct snd_pcm_substream *substream,
425 struct snd_pcm_hw_params *params,
426 struct snd_soc_dai *dai)
427{
428 struct snd_soc_pcm_runtime *rtd = substream->private_data;
429 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
430 unsigned long clk_freq;
Maxime Ripard8400ddf2015-12-01 12:06:47 +0100431 int ret, hwrate;
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100432
433 clk_freq = sun4i_codec_get_mod_freq(params);
434 if (!clk_freq)
435 return -EINVAL;
436
Maxime Ripard8400ddf2015-12-01 12:06:47 +0100437 ret = clk_set_rate(scodec->clk_module, clk_freq);
438 if (ret)
439 return ret;
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100440
441 hwrate = sun4i_codec_get_hw_rate(params);
442 if (hwrate < 0)
443 return hwrate;
444
445 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
446 return sun4i_codec_hw_params_playback(scodec, params,
447 hwrate);
448
449 return sun4i_codec_hw_params_capture(scodec, params,
450 hwrate);
451}
452
Emilio López45fb6b62015-09-12 15:26:24 +0200453static int sun4i_codec_startup(struct snd_pcm_substream *substream,
454 struct snd_soc_dai *dai)
455{
456 struct snd_soc_pcm_runtime *rtd = substream->private_data;
457 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
458
459 /*
460 * Stop issuing DRQ when we have room for less than 16 samples
461 * in our TX FIFO
462 */
463 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
464 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT,
465 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT);
466
467 return clk_prepare_enable(scodec->clk_module);
468}
469
470static void sun4i_codec_shutdown(struct snd_pcm_substream *substream,
471 struct snd_soc_dai *dai)
472{
473 struct snd_soc_pcm_runtime *rtd = substream->private_data;
474 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
475
476 clk_disable_unprepare(scodec->clk_module);
477}
478
479static const struct snd_soc_dai_ops sun4i_codec_dai_ops = {
480 .startup = sun4i_codec_startup,
481 .shutdown = sun4i_codec_shutdown,
482 .trigger = sun4i_codec_trigger,
483 .hw_params = sun4i_codec_hw_params,
484 .prepare = sun4i_codec_prepare,
485};
486
487static struct snd_soc_dai_driver sun4i_codec_dai = {
488 .name = "Codec",
489 .ops = &sun4i_codec_dai_ops,
490 .playback = {
491 .stream_name = "Codec Playback",
492 .channels_min = 1,
493 .channels_max = 2,
494 .rate_min = 8000,
495 .rate_max = 192000,
496 .rates = SNDRV_PCM_RATE_8000_48000 |
497 SNDRV_PCM_RATE_96000 |
Maxime Riparddebb9722015-09-29 21:43:18 +0200498 SNDRV_PCM_RATE_192000,
Emilio López45fb6b62015-09-12 15:26:24 +0200499 .formats = SNDRV_PCM_FMTBIT_S16_LE |
500 SNDRV_PCM_FMTBIT_S32_LE,
501 .sig_bits = 24,
502 },
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100503 .capture = {
504 .stream_name = "Codec Capture",
505 .channels_min = 1,
506 .channels_max = 2,
507 .rate_min = 8000,
508 .rate_max = 192000,
509 .rates = SNDRV_PCM_RATE_8000_48000 |
510 SNDRV_PCM_RATE_96000 |
511 SNDRV_PCM_RATE_192000 |
512 SNDRV_PCM_RATE_KNOT,
513 .formats = SNDRV_PCM_FMTBIT_S16_LE |
514 SNDRV_PCM_FMTBIT_S32_LE,
515 .sig_bits = 24,
516 },
Emilio López45fb6b62015-09-12 15:26:24 +0200517};
518
519/*** Codec ***/
520static const struct snd_kcontrol_new sun4i_codec_pa_mute =
521 SOC_DAPM_SINGLE("Switch", SUN4I_CODEC_DAC_ACTL,
522 SUN4I_CODEC_DAC_ACTL_PA_MUTE, 1, 0);
523
524static DECLARE_TLV_DB_SCALE(sun4i_codec_pa_volume_scale, -6300, 100, 1);
525
Danny Milosavljevicc9e902f2016-09-24 22:05:01 +0200526static const struct snd_kcontrol_new sun4i_codec_controls[] = {
Adam Sampson474d1472015-10-27 21:00:45 +0000527 SOC_SINGLE_TLV("Power Amplifier Volume", SUN4I_CODEC_DAC_ACTL,
Emilio López45fb6b62015-09-12 15:26:24 +0200528 SUN4I_CODEC_DAC_ACTL_PA_VOL, 0x3F, 0,
529 sun4i_codec_pa_volume_scale),
530};
531
532static const struct snd_kcontrol_new sun4i_codec_left_mixer_controls[] = {
533 SOC_DAPM_SINGLE("Left DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
534 SUN4I_CODEC_DAC_ACTL_LDACLMIXS, 1, 0),
535};
536
537static const struct snd_kcontrol_new sun4i_codec_right_mixer_controls[] = {
538 SOC_DAPM_SINGLE("Right DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
539 SUN4I_CODEC_DAC_ACTL_RDACRMIXS, 1, 0),
540 SOC_DAPM_SINGLE("Left DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
541 SUN4I_CODEC_DAC_ACTL_LDACRMIXS, 1, 0),
542};
543
544static const struct snd_kcontrol_new sun4i_codec_pa_mixer_controls[] = {
545 SOC_DAPM_SINGLE("DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
546 SUN4I_CODEC_DAC_ACTL_DACPAS, 1, 0),
547 SOC_DAPM_SINGLE("Mixer Playback Switch", SUN4I_CODEC_DAC_ACTL,
548 SUN4I_CODEC_DAC_ACTL_MIXPAS, 1, 0),
549};
550
Hans de Goedee6415b42015-12-11 19:43:56 +0100551static const struct snd_soc_dapm_widget sun4i_codec_codec_dapm_widgets[] = {
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100552 /* Digital parts of the ADCs */
553 SND_SOC_DAPM_SUPPLY("ADC", SUN4I_CODEC_ADC_FIFOC,
554 SUN4I_CODEC_ADC_FIFOC_EN_AD, 0,
555 NULL, 0),
556
Emilio López45fb6b62015-09-12 15:26:24 +0200557 /* Digital parts of the DACs */
558 SND_SOC_DAPM_SUPPLY("DAC", SUN4I_CODEC_DAC_DPC,
559 SUN4I_CODEC_DAC_DPC_EN_DA, 0,
560 NULL, 0),
561
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100562 /* Analog parts of the ADCs */
563 SND_SOC_DAPM_ADC("Left ADC", "Codec Capture", SUN4I_CODEC_ADC_ACTL,
564 SUN4I_CODEC_ADC_ACTL_ADC_L_EN, 0),
565 SND_SOC_DAPM_ADC("Right ADC", "Codec Capture", SUN4I_CODEC_ADC_ACTL,
566 SUN4I_CODEC_ADC_ACTL_ADC_R_EN, 0),
567
Emilio López45fb6b62015-09-12 15:26:24 +0200568 /* Analog parts of the DACs */
569 SND_SOC_DAPM_DAC("Left DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL,
570 SUN4I_CODEC_DAC_ACTL_DACAENL, 0),
571 SND_SOC_DAPM_DAC("Right DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL,
572 SUN4I_CODEC_DAC_ACTL_DACAENR, 0),
573
574 /* Mixers */
575 SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
576 sun4i_codec_left_mixer_controls,
577 ARRAY_SIZE(sun4i_codec_left_mixer_controls)),
578 SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
579 sun4i_codec_right_mixer_controls,
580 ARRAY_SIZE(sun4i_codec_right_mixer_controls)),
581
582 /* Global Mixer Enable */
583 SND_SOC_DAPM_SUPPLY("Mixer Enable", SUN4I_CODEC_DAC_ACTL,
584 SUN4I_CODEC_DAC_ACTL_MIXEN, 0, NULL, 0),
585
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100586 /* VMIC */
587 SND_SOC_DAPM_SUPPLY("VMIC", SUN4I_CODEC_ADC_ACTL,
588 SUN4I_CODEC_ADC_ACTL_VMICEN, 0, NULL, 0),
589
590 /* Mic Pre-Amplifiers */
591 SND_SOC_DAPM_PGA("MIC1 Pre-Amplifier", SUN4I_CODEC_ADC_ACTL,
592 SUN4I_CODEC_ADC_ACTL_PREG1EN, 0, NULL, 0),
593
Adam Sampson474d1472015-10-27 21:00:45 +0000594 /* Power Amplifier */
595 SND_SOC_DAPM_MIXER("Power Amplifier", SUN4I_CODEC_ADC_ACTL,
Emilio López45fb6b62015-09-12 15:26:24 +0200596 SUN4I_CODEC_ADC_ACTL_PA_EN, 0,
597 sun4i_codec_pa_mixer_controls,
598 ARRAY_SIZE(sun4i_codec_pa_mixer_controls)),
Adam Sampson474d1472015-10-27 21:00:45 +0000599 SND_SOC_DAPM_SWITCH("Power Amplifier Mute", SND_SOC_NOPM, 0, 0,
Emilio López45fb6b62015-09-12 15:26:24 +0200600 &sun4i_codec_pa_mute),
601
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100602 SND_SOC_DAPM_INPUT("Mic1"),
603
Emilio López45fb6b62015-09-12 15:26:24 +0200604 SND_SOC_DAPM_OUTPUT("HP Right"),
605 SND_SOC_DAPM_OUTPUT("HP Left"),
606};
607
Hans de Goedee6415b42015-12-11 19:43:56 +0100608static const struct snd_soc_dapm_route sun4i_codec_codec_dapm_routes[] = {
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100609 /* Left ADC / DAC Routes */
610 { "Left ADC", NULL, "ADC" },
Emilio López45fb6b62015-09-12 15:26:24 +0200611 { "Left DAC", NULL, "DAC" },
612
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100613 /* Right ADC / DAC Routes */
614 { "Right ADC", NULL, "ADC" },
Emilio López45fb6b62015-09-12 15:26:24 +0200615 { "Right DAC", NULL, "DAC" },
616
617 /* Right Mixer Routes */
618 { "Right Mixer", NULL, "Mixer Enable" },
619 { "Right Mixer", "Left DAC Playback Switch", "Left DAC" },
620 { "Right Mixer", "Right DAC Playback Switch", "Right DAC" },
621
622 /* Left Mixer Routes */
623 { "Left Mixer", NULL, "Mixer Enable" },
624 { "Left Mixer", "Left DAC Playback Switch", "Left DAC" },
625
Adam Sampson474d1472015-10-27 21:00:45 +0000626 /* Power Amplifier Routes */
627 { "Power Amplifier", "Mixer Playback Switch", "Left Mixer" },
628 { "Power Amplifier", "Mixer Playback Switch", "Right Mixer" },
629 { "Power Amplifier", "DAC Playback Switch", "Left DAC" },
630 { "Power Amplifier", "DAC Playback Switch", "Right DAC" },
Emilio López45fb6b62015-09-12 15:26:24 +0200631
Adam Sampson474d1472015-10-27 21:00:45 +0000632 /* Headphone Output Routes */
633 { "Power Amplifier Mute", "Switch", "Power Amplifier" },
634 { "HP Right", NULL, "Power Amplifier Mute" },
635 { "HP Left", NULL, "Power Amplifier Mute" },
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100636
637 /* Mic1 Routes */
638 { "Left ADC", NULL, "MIC1 Pre-Amplifier" },
639 { "Right ADC", NULL, "MIC1 Pre-Amplifier" },
640 { "MIC1 Pre-Amplifier", NULL, "Mic1"},
641 { "Mic1", NULL, "VMIC" },
Emilio López45fb6b62015-09-12 15:26:24 +0200642};
643
644static struct snd_soc_codec_driver sun4i_codec_codec = {
Kuninori Morimoto180f58f2016-08-08 08:46:41 +0000645 .component_driver = {
Danny Milosavljevicc9e902f2016-09-24 22:05:01 +0200646 .controls = sun4i_codec_controls,
647 .num_controls = ARRAY_SIZE(sun4i_codec_controls),
Kuninori Morimoto180f58f2016-08-08 08:46:41 +0000648 .dapm_widgets = sun4i_codec_codec_dapm_widgets,
649 .num_dapm_widgets = ARRAY_SIZE(sun4i_codec_codec_dapm_widgets),
650 .dapm_routes = sun4i_codec_codec_dapm_routes,
651 .num_dapm_routes = ARRAY_SIZE(sun4i_codec_codec_dapm_routes),
652 },
Emilio López45fb6b62015-09-12 15:26:24 +0200653};
654
655static const struct snd_soc_component_driver sun4i_codec_component = {
656 .name = "sun4i-codec",
657};
658
659#define SUN4I_CODEC_RATES SNDRV_PCM_RATE_8000_192000
660#define SUN4I_CODEC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
661 SNDRV_PCM_FMTBIT_S32_LE)
662
663static int sun4i_codec_dai_probe(struct snd_soc_dai *dai)
664{
665 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
666 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
667
668 snd_soc_dai_init_dma_data(dai, &scodec->playback_dma_data,
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100669 &scodec->capture_dma_data);
Emilio López45fb6b62015-09-12 15:26:24 +0200670
671 return 0;
672}
673
674static struct snd_soc_dai_driver dummy_cpu_dai = {
675 .name = "sun4i-codec-cpu-dai",
676 .probe = sun4i_codec_dai_probe,
677 .playback = {
678 .stream_name = "Playback",
679 .channels_min = 1,
680 .channels_max = 2,
681 .rates = SUN4I_CODEC_RATES,
682 .formats = SUN4I_CODEC_FORMATS,
683 .sig_bits = 24,
684 },
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100685 .capture = {
686 .stream_name = "Capture",
687 .channels_min = 1,
688 .channels_max = 2,
689 .rates = SUN4I_CODEC_RATES,
690 .formats = SUN4I_CODEC_FORMATS,
691 .sig_bits = 24,
692 },
Emilio López45fb6b62015-09-12 15:26:24 +0200693};
694
Emilio López45fb6b62015-09-12 15:26:24 +0200695static struct snd_soc_dai_link *sun4i_codec_create_link(struct device *dev,
696 int *num_links)
697{
698 struct snd_soc_dai_link *link = devm_kzalloc(dev, sizeof(*link),
699 GFP_KERNEL);
700 if (!link)
701 return NULL;
702
703 link->name = "cdc";
704 link->stream_name = "CDC PCM";
705 link->codec_dai_name = "Codec";
706 link->cpu_dai_name = dev_name(dev);
707 link->codec_name = dev_name(dev);
708 link->platform_name = dev_name(dev);
709 link->dai_fmt = SND_SOC_DAIFMT_I2S;
710
711 *num_links = 1;
712
713 return link;
714};
715
Hans de Goede40592622015-12-11 19:43:57 +0100716static int sun4i_codec_spk_event(struct snd_soc_dapm_widget *w,
717 struct snd_kcontrol *k, int event)
718{
719 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(w->dapm->card);
720
721 if (scodec->gpio_pa)
722 gpiod_set_value_cansleep(scodec->gpio_pa,
723 !!SND_SOC_DAPM_EVENT_ON(event));
724
725 return 0;
726}
727
728static const struct snd_soc_dapm_widget sun4i_codec_card_dapm_widgets[] = {
729 SND_SOC_DAPM_SPK("Speaker", sun4i_codec_spk_event),
730};
731
732static const struct snd_soc_dapm_route sun4i_codec_card_dapm_routes[] = {
Hans de Goede6b803c62015-12-22 23:00:17 +0100733 { "Speaker", NULL, "HP Right" },
734 { "Speaker", NULL, "HP Left" },
Hans de Goede40592622015-12-11 19:43:57 +0100735};
736
Emilio López45fb6b62015-09-12 15:26:24 +0200737static struct snd_soc_card *sun4i_codec_create_card(struct device *dev)
738{
739 struct snd_soc_card *card;
Emilio López45fb6b62015-09-12 15:26:24 +0200740
741 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
742 if (!card)
Chen-Yu Tsai85915b62016-10-31 14:42:09 +0800743 return ERR_PTR(-ENOMEM);
Emilio López45fb6b62015-09-12 15:26:24 +0200744
745 card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
746 if (!card->dai_link)
Chen-Yu Tsai85915b62016-10-31 14:42:09 +0800747 return ERR_PTR(-ENOMEM);
Emilio López45fb6b62015-09-12 15:26:24 +0200748
749 card->dev = dev;
750 card->name = "sun4i-codec";
Hans de Goede40592622015-12-11 19:43:57 +0100751 card->dapm_widgets = sun4i_codec_card_dapm_widgets;
752 card->num_dapm_widgets = ARRAY_SIZE(sun4i_codec_card_dapm_widgets);
753 card->dapm_routes = sun4i_codec_card_dapm_routes;
754 card->num_dapm_routes = ARRAY_SIZE(sun4i_codec_card_dapm_routes);
Emilio López45fb6b62015-09-12 15:26:24 +0200755
Emilio López45fb6b62015-09-12 15:26:24 +0200756 return card;
757};
758
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +0800759static const struct regmap_config sun4i_codec_regmap_config = {
760 .reg_bits = 32,
761 .reg_stride = 4,
762 .val_bits = 32,
763 .max_register = SUN4I_CODEC_ADC_RXCNT,
764};
765
766static const struct regmap_config sun7i_codec_regmap_config = {
767 .reg_bits = 32,
768 .reg_stride = 4,
769 .val_bits = 32,
770 .max_register = SUN7I_CODEC_AC_MIC_PHONE_CAL,
771};
772
773struct sun4i_codec_quirks {
774 const struct regmap_config *regmap_config;
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800775 const struct snd_soc_codec_driver *codec;
776 struct snd_soc_card * (*create_card)(struct device *dev);
777 struct reg_field reg_adc_fifoc; /* used for regmap_field */
778 unsigned int reg_dac_txdata; /* TX FIFO offset for DMA config */
779 unsigned int reg_adc_rxdata; /* RX FIFO offset for DMA config */
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +0800780};
781
782static const struct sun4i_codec_quirks sun4i_codec_quirks = {
783 .regmap_config = &sun4i_codec_regmap_config,
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800784 .codec = &sun4i_codec_codec,
785 .create_card = sun4i_codec_create_card,
786 .reg_adc_fifoc = REG_FIELD(SUN4I_CODEC_ADC_FIFOC, 0, 31),
787 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
788 .reg_adc_rxdata = SUN4I_CODEC_ADC_RXDATA,
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +0800789};
790
791static const struct sun4i_codec_quirks sun7i_codec_quirks = {
792 .regmap_config = &sun7i_codec_regmap_config,
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800793 .codec = &sun4i_codec_codec,
794 .create_card = sun4i_codec_create_card,
795 .reg_adc_fifoc = REG_FIELD(SUN4I_CODEC_ADC_FIFOC, 0, 31),
796 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
797 .reg_adc_rxdata = SUN4I_CODEC_ADC_RXDATA,
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +0800798};
799
800static const struct of_device_id sun4i_codec_of_match[] = {
801 {
802 .compatible = "allwinner,sun4i-a10-codec",
803 .data = &sun4i_codec_quirks,
804 },
805 {
806 .compatible = "allwinner,sun7i-a20-codec",
807 .data = &sun7i_codec_quirks,
808 },
809 {}
810};
811MODULE_DEVICE_TABLE(of, sun4i_codec_of_match);
812
Emilio López45fb6b62015-09-12 15:26:24 +0200813static int sun4i_codec_probe(struct platform_device *pdev)
814{
815 struct snd_soc_card *card;
816 struct sun4i_codec *scodec;
Danny Milosavljevicc1d50652016-09-22 09:13:13 +0200817 const struct sun4i_codec_quirks *quirks;
Emilio López45fb6b62015-09-12 15:26:24 +0200818 struct resource *res;
819 void __iomem *base;
820 int ret;
821
822 scodec = devm_kzalloc(&pdev->dev, sizeof(*scodec), GFP_KERNEL);
823 if (!scodec)
824 return -ENOMEM;
825
826 scodec->dev = &pdev->dev;
827
828 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
829 base = devm_ioremap_resource(&pdev->dev, res);
830 if (IS_ERR(base)) {
831 dev_err(&pdev->dev, "Failed to map the registers\n");
832 return PTR_ERR(base);
833 }
834
Danny Milosavljevicc1d50652016-09-22 09:13:13 +0200835 quirks = of_device_get_match_data(&pdev->dev);
836 if (quirks == NULL) {
837 dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
838 return -ENODEV;
839 }
840
Emilio López45fb6b62015-09-12 15:26:24 +0200841 scodec->regmap = devm_regmap_init_mmio(&pdev->dev, base,
Danny Milosavljevicc1d50652016-09-22 09:13:13 +0200842 quirks->regmap_config);
Emilio López45fb6b62015-09-12 15:26:24 +0200843 if (IS_ERR(scodec->regmap)) {
844 dev_err(&pdev->dev, "Failed to create our regmap\n");
845 return PTR_ERR(scodec->regmap);
846 }
847
848 /* Get the clocks from the DT */
849 scodec->clk_apb = devm_clk_get(&pdev->dev, "apb");
850 if (IS_ERR(scodec->clk_apb)) {
851 dev_err(&pdev->dev, "Failed to get the APB clock\n");
852 return PTR_ERR(scodec->clk_apb);
853 }
854
855 scodec->clk_module = devm_clk_get(&pdev->dev, "codec");
856 if (IS_ERR(scodec->clk_module)) {
857 dev_err(&pdev->dev, "Failed to get the module clock\n");
858 return PTR_ERR(scodec->clk_module);
859 }
860
Hans de Goede40592622015-12-11 19:43:57 +0100861 scodec->gpio_pa = devm_gpiod_get_optional(&pdev->dev, "allwinner,pa",
862 GPIOD_OUT_LOW);
863 if (IS_ERR(scodec->gpio_pa)) {
864 ret = PTR_ERR(scodec->gpio_pa);
865 if (ret != -EPROBE_DEFER)
866 dev_err(&pdev->dev, "Failed to get pa gpio: %d\n", ret);
867 return ret;
868 }
869
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800870 /* reg_field setup */
871 scodec->reg_adc_fifoc = devm_regmap_field_alloc(&pdev->dev,
872 scodec->regmap,
873 quirks->reg_adc_fifoc);
874 if (IS_ERR(scodec->reg_adc_fifoc)) {
875 ret = PTR_ERR(scodec->reg_adc_fifoc);
876 dev_err(&pdev->dev, "Failed to create regmap fields: %d\n",
877 ret);
878 return ret;
879 }
880
Chen-Yu Tsai3716a892016-11-01 14:31:55 +0800881 /* Enable the bus clock */
882 if (clk_prepare_enable(scodec->clk_apb)) {
883 dev_err(&pdev->dev, "Failed to enable the APB clock\n");
884 return -EINVAL;
885 }
886
Emilio López45fb6b62015-09-12 15:26:24 +0200887 /* DMA configuration for TX FIFO */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800888 scodec->playback_dma_data.addr = res->start + quirks->reg_dac_txdata;
Chen-Yu Tsai730e2dd2016-11-03 15:55:46 +0800889 scodec->playback_dma_data.maxburst = 8;
Emilio López45fb6b62015-09-12 15:26:24 +0200890 scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
891
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100892 /* DMA configuration for RX FIFO */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800893 scodec->capture_dma_data.addr = res->start + quirks->reg_adc_rxdata;
Chen-Yu Tsai730e2dd2016-11-03 15:55:46 +0800894 scodec->capture_dma_data.maxburst = 8;
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100895 scodec->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
896
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800897 ret = snd_soc_register_codec(&pdev->dev, quirks->codec,
Emilio López45fb6b62015-09-12 15:26:24 +0200898 &sun4i_codec_dai, 1);
899 if (ret) {
900 dev_err(&pdev->dev, "Failed to register our codec\n");
901 goto err_clk_disable;
902 }
903
904 ret = devm_snd_soc_register_component(&pdev->dev,
905 &sun4i_codec_component,
906 &dummy_cpu_dai, 1);
907 if (ret) {
908 dev_err(&pdev->dev, "Failed to register our DAI\n");
909 goto err_unregister_codec;
910 }
911
912 ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
913 if (ret) {
914 dev_err(&pdev->dev, "Failed to register against DMAEngine\n");
915 goto err_unregister_codec;
916 }
917
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800918 card = quirks->create_card(&pdev->dev);
Chen-Yu Tsai85915b62016-10-31 14:42:09 +0800919 if (IS_ERR(card)) {
920 ret = PTR_ERR(card);
Emilio López45fb6b62015-09-12 15:26:24 +0200921 dev_err(&pdev->dev, "Failed to create our card\n");
922 goto err_unregister_codec;
923 }
924
925 platform_set_drvdata(pdev, card);
926 snd_soc_card_set_drvdata(card, scodec);
927
928 ret = snd_soc_register_card(card);
929 if (ret) {
930 dev_err(&pdev->dev, "Failed to register our card\n");
931 goto err_unregister_codec;
932 }
933
934 return 0;
935
936err_unregister_codec:
937 snd_soc_unregister_codec(&pdev->dev);
938err_clk_disable:
939 clk_disable_unprepare(scodec->clk_apb);
940 return ret;
941}
942
943static int sun4i_codec_remove(struct platform_device *pdev)
944{
945 struct snd_soc_card *card = platform_get_drvdata(pdev);
946 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
947
948 snd_soc_unregister_card(card);
949 snd_soc_unregister_codec(&pdev->dev);
950 clk_disable_unprepare(scodec->clk_apb);
951
952 return 0;
953}
954
955static struct platform_driver sun4i_codec_driver = {
956 .driver = {
957 .name = "sun4i-codec",
Emilio López45fb6b62015-09-12 15:26:24 +0200958 .of_match_table = sun4i_codec_of_match,
959 },
960 .probe = sun4i_codec_probe,
961 .remove = sun4i_codec_remove,
962};
963module_platform_driver(sun4i_codec_driver);
964
965MODULE_DESCRIPTION("Allwinner A10 codec driver");
966MODULE_AUTHOR("Emilio López <emilio@elopez.com.ar>");
967MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
968MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800969MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
Emilio López45fb6b62015-09-12 15:26:24 +0200970MODULE_LICENSE("GPL");