blob: 6379efd21f002d7ab4ccf473ea92b3976d0dc822 [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>
Chen-Yu Tsai9aead152016-11-07 18:06:58 +080033#include <linux/reset.h>
Hans de Goede40592622015-12-11 19:43:57 +010034#include <linux/gpio/consumer.h>
Emilio López45fb6b62015-09-12 15:26:24 +020035
36#include <sound/core.h>
37#include <sound/pcm.h>
38#include <sound/pcm_params.h>
39#include <sound/soc.h>
40#include <sound/tlv.h>
41#include <sound/initval.h>
42#include <sound/dmaengine_pcm.h>
43
Chen-Yu Tsaibd720ec2016-11-03 15:55:45 +080044/* Codec DAC digital controls and FIFO registers */
Emilio López45fb6b62015-09-12 15:26:24 +020045#define SUN4I_CODEC_DAC_DPC (0x00)
46#define SUN4I_CODEC_DAC_DPC_EN_DA (31)
47#define SUN4I_CODEC_DAC_DPC_DVOL (12)
48#define SUN4I_CODEC_DAC_FIFOC (0x04)
49#define SUN4I_CODEC_DAC_FIFOC_DAC_FS (29)
50#define SUN4I_CODEC_DAC_FIFOC_FIR_VERSION (28)
51#define SUN4I_CODEC_DAC_FIFOC_SEND_LASAT (26)
52#define SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE (24)
53#define SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT (21)
54#define SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL (8)
55#define SUN4I_CODEC_DAC_FIFOC_MONO_EN (6)
56#define SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS (5)
57#define SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN (4)
58#define SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH (0)
59#define SUN4I_CODEC_DAC_FIFOS (0x08)
60#define SUN4I_CODEC_DAC_TXDATA (0x0c)
Chen-Yu Tsaibd720ec2016-11-03 15:55:45 +080061
62/* Codec DAC side analog signal controls */
Emilio López45fb6b62015-09-12 15:26:24 +020063#define SUN4I_CODEC_DAC_ACTL (0x10)
64#define SUN4I_CODEC_DAC_ACTL_DACAENR (31)
65#define SUN4I_CODEC_DAC_ACTL_DACAENL (30)
66#define SUN4I_CODEC_DAC_ACTL_MIXEN (29)
67#define SUN4I_CODEC_DAC_ACTL_LDACLMIXS (15)
68#define SUN4I_CODEC_DAC_ACTL_RDACRMIXS (14)
69#define SUN4I_CODEC_DAC_ACTL_LDACRMIXS (13)
70#define SUN4I_CODEC_DAC_ACTL_DACPAS (8)
71#define SUN4I_CODEC_DAC_ACTL_MIXPAS (7)
72#define SUN4I_CODEC_DAC_ACTL_PA_MUTE (6)
73#define SUN4I_CODEC_DAC_ACTL_PA_VOL (0)
74#define SUN4I_CODEC_DAC_TUNE (0x14)
75#define SUN4I_CODEC_DAC_DEBUG (0x18)
76
Chen-Yu Tsaibd720ec2016-11-03 15:55:45 +080077/* Codec ADC digital controls and FIFO registers */
Emilio López45fb6b62015-09-12 15:26:24 +020078#define SUN4I_CODEC_ADC_FIFOC (0x1c)
Maxime Ripard1fb34b42015-11-30 16:37:47 +010079#define SUN4I_CODEC_ADC_FIFOC_ADC_FS (29)
Emilio López45fb6b62015-09-12 15:26:24 +020080#define SUN4I_CODEC_ADC_FIFOC_EN_AD (28)
81#define SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE (24)
82#define SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL (8)
83#define SUN4I_CODEC_ADC_FIFOC_MONO_EN (7)
84#define SUN4I_CODEC_ADC_FIFOC_RX_SAMPLE_BITS (6)
85#define SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN (4)
86#define SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH (0)
87#define SUN4I_CODEC_ADC_FIFOS (0x20)
88#define SUN4I_CODEC_ADC_RXDATA (0x24)
Chen-Yu Tsaibd720ec2016-11-03 15:55:45 +080089
90/* Codec ADC side analog signal controls */
Emilio López45fb6b62015-09-12 15:26:24 +020091#define SUN4I_CODEC_ADC_ACTL (0x28)
92#define SUN4I_CODEC_ADC_ACTL_ADC_R_EN (31)
93#define SUN4I_CODEC_ADC_ACTL_ADC_L_EN (30)
94#define SUN4I_CODEC_ADC_ACTL_PREG1EN (29)
95#define SUN4I_CODEC_ADC_ACTL_PREG2EN (28)
96#define SUN4I_CODEC_ADC_ACTL_VMICEN (27)
97#define SUN4I_CODEC_ADC_ACTL_VADCG (20)
98#define SUN4I_CODEC_ADC_ACTL_ADCIS (17)
99#define SUN4I_CODEC_ADC_ACTL_PA_EN (4)
100#define SUN4I_CODEC_ADC_ACTL_DDE (3)
101#define SUN4I_CODEC_ADC_DEBUG (0x2c)
102
Chen-Yu Tsaibd720ec2016-11-03 15:55:45 +0800103/* FIFO counters */
Emilio López45fb6b62015-09-12 15:26:24 +0200104#define SUN4I_CODEC_DAC_TXCNT (0x30)
105#define SUN4I_CODEC_ADC_RXCNT (0x34)
Chen-Yu Tsaibd720ec2016-11-03 15:55:45 +0800106
107/* Calibration register (sun7i only) */
Danny Milosavljevic4f0c4e92016-09-22 09:13:12 +0200108#define SUN7I_CODEC_AC_DAC_CAL (0x38)
Chen-Yu Tsaibd720ec2016-11-03 15:55:45 +0800109
110/* Microphone controls (sun7i only) */
Danny Milosavljevic4f0c4e92016-09-22 09:13:12 +0200111#define SUN7I_CODEC_AC_MIC_PHONE_CAL (0x3c)
Emilio López45fb6b62015-09-12 15:26:24 +0200112
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800113/*
114 * sun6i specific registers
115 *
116 * sun6i shares the same digital control and FIFO registers as sun4i,
117 * but only the DAC digital controls are at the same offset. The others
118 * have been moved around to accommodate extra analog controls.
119 */
120
121/* Codec DAC digital controls and FIFO registers */
122#define SUN6I_CODEC_ADC_FIFOC (0x10)
123#define SUN6I_CODEC_ADC_FIFOC_EN_AD (28)
124#define SUN6I_CODEC_ADC_FIFOS (0x14)
125#define SUN6I_CODEC_ADC_RXDATA (0x18)
126
127/* Output mixer and gain controls */
128#define SUN6I_CODEC_OM_DACA_CTRL (0x20)
129#define SUN6I_CODEC_OM_DACA_CTRL_DACAREN (31)
130#define SUN6I_CODEC_OM_DACA_CTRL_DACALEN (30)
131#define SUN6I_CODEC_OM_DACA_CTRL_RMIXEN (29)
132#define SUN6I_CODEC_OM_DACA_CTRL_LMIXEN (28)
133#define SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC1 (23)
134#define SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC2 (22)
135#define SUN6I_CODEC_OM_DACA_CTRL_RMIX_PHONE (21)
136#define SUN6I_CODEC_OM_DACA_CTRL_RMIX_PHONEP (20)
137#define SUN6I_CODEC_OM_DACA_CTRL_RMIX_LINEINR (19)
138#define SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACR (18)
139#define SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACL (17)
140#define SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC1 (16)
141#define SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC2 (15)
142#define SUN6I_CODEC_OM_DACA_CTRL_LMIX_PHONE (14)
143#define SUN6I_CODEC_OM_DACA_CTRL_LMIX_PHONEN (13)
144#define SUN6I_CODEC_OM_DACA_CTRL_LMIX_LINEINL (12)
145#define SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACL (11)
146#define SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACR (10)
147#define SUN6I_CODEC_OM_DACA_CTRL_RHPIS (9)
148#define SUN6I_CODEC_OM_DACA_CTRL_LHPIS (8)
149#define SUN6I_CODEC_OM_DACA_CTRL_RHPPAMUTE (7)
150#define SUN6I_CODEC_OM_DACA_CTRL_LHPPAMUTE (6)
151#define SUN6I_CODEC_OM_DACA_CTRL_HPVOL (0)
152#define SUN6I_CODEC_OM_PA_CTRL (0x24)
153#define SUN6I_CODEC_OM_PA_CTRL_HPPAEN (31)
154#define SUN6I_CODEC_OM_PA_CTRL_HPCOM_CTL (29)
155#define SUN6I_CODEC_OM_PA_CTRL_COMPTEN (28)
156#define SUN6I_CODEC_OM_PA_CTRL_MIC1G (15)
157#define SUN6I_CODEC_OM_PA_CTRL_MIC2G (12)
158#define SUN6I_CODEC_OM_PA_CTRL_LINEING (9)
159#define SUN6I_CODEC_OM_PA_CTRL_PHONEG (6)
160#define SUN6I_CODEC_OM_PA_CTRL_PHONEPG (3)
161#define SUN6I_CODEC_OM_PA_CTRL_PHONENG (0)
162
163/* Microphone, line out and phone out controls */
164#define SUN6I_CODEC_MIC_CTRL (0x28)
165#define SUN6I_CODEC_MIC_CTRL_HBIASEN (31)
166#define SUN6I_CODEC_MIC_CTRL_MBIASEN (30)
167#define SUN6I_CODEC_MIC_CTRL_MIC1AMPEN (28)
168#define SUN6I_CODEC_MIC_CTRL_MIC1BOOST (25)
169#define SUN6I_CODEC_MIC_CTRL_MIC2AMPEN (24)
170#define SUN6I_CODEC_MIC_CTRL_MIC2BOOST (21)
171#define SUN6I_CODEC_MIC_CTRL_MIC2SLT (20)
172#define SUN6I_CODEC_MIC_CTRL_LINEOUTLEN (19)
173#define SUN6I_CODEC_MIC_CTRL_LINEOUTREN (18)
174#define SUN6I_CODEC_MIC_CTRL_LINEOUTLSRC (17)
175#define SUN6I_CODEC_MIC_CTRL_LINEOUTRSRC (16)
176#define SUN6I_CODEC_MIC_CTRL_LINEOUTVC (11)
177#define SUN6I_CODEC_MIC_CTRL_PHONEPREG (8)
178
179/* ADC mixer controls */
180#define SUN6I_CODEC_ADC_ACTL (0x2c)
181#define SUN6I_CODEC_ADC_ACTL_ADCREN (31)
182#define SUN6I_CODEC_ADC_ACTL_ADCLEN (30)
183#define SUN6I_CODEC_ADC_ACTL_ADCRG (27)
184#define SUN6I_CODEC_ADC_ACTL_ADCLG (24)
185#define SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC1 (13)
186#define SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC2 (12)
187#define SUN6I_CODEC_ADC_ACTL_RADCMIX_PHONE (11)
188#define SUN6I_CODEC_ADC_ACTL_RADCMIX_PHONEP (10)
189#define SUN6I_CODEC_ADC_ACTL_RADCMIX_LINEINR (9)
190#define SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXR (8)
191#define SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXL (7)
192#define SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC1 (6)
193#define SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC2 (5)
194#define SUN6I_CODEC_ADC_ACTL_LADCMIX_PHONE (4)
195#define SUN6I_CODEC_ADC_ACTL_LADCMIX_PHONEN (3)
196#define SUN6I_CODEC_ADC_ACTL_LADCMIX_LINEINL (2)
197#define SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXL (1)
198#define SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXR (0)
199
200/* Analog performance tuning controls */
201#define SUN6I_CODEC_ADDA_TUNE (0x30)
202
203/* Calibration controls */
204#define SUN6I_CODEC_CALIBRATION (0x34)
205
206/* FIFO counters */
207#define SUN6I_CODEC_DAC_TXCNT (0x40)
208#define SUN6I_CODEC_ADC_RXCNT (0x44)
209
210/* headset jack detection and button support registers */
211#define SUN6I_CODEC_HMIC_CTL (0x50)
212#define SUN6I_CODEC_HMIC_DATA (0x54)
213
214/* TODO sun6i DAP (Digital Audio Processing) bits */
215
Emilio López45fb6b62015-09-12 15:26:24 +0200216struct sun4i_codec {
217 struct device *dev;
218 struct regmap *regmap;
219 struct clk *clk_apb;
220 struct clk *clk_module;
Chen-Yu Tsai9aead152016-11-07 18:06:58 +0800221 struct reset_control *rst;
Hans de Goede40592622015-12-11 19:43:57 +0100222 struct gpio_desc *gpio_pa;
Emilio López45fb6b62015-09-12 15:26:24 +0200223
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800224 /* ADC_FIFOC register is at different offset on different SoCs */
225 struct regmap_field *reg_adc_fifoc;
226
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100227 struct snd_dmaengine_dai_dma_data capture_dma_data;
Emilio López45fb6b62015-09-12 15:26:24 +0200228 struct snd_dmaengine_dai_dma_data playback_dma_data;
229};
230
231static void sun4i_codec_start_playback(struct sun4i_codec *scodec)
232{
Emilio López45fb6b62015-09-12 15:26:24 +0200233 /* Flush TX FIFO */
234 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
235 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH),
236 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH));
237
238 /* Enable DAC DRQ */
239 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
240 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN),
241 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN));
242}
243
244static void sun4i_codec_stop_playback(struct sun4i_codec *scodec)
245{
Emilio López45fb6b62015-09-12 15:26:24 +0200246 /* Disable DAC DRQ */
247 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
248 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN),
249 0);
250}
251
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100252static void sun4i_codec_start_capture(struct sun4i_codec *scodec)
253{
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100254 /* Enable ADC DRQ */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800255 regmap_field_update_bits(scodec->reg_adc_fifoc,
256 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN),
257 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN));
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100258}
259
260static void sun4i_codec_stop_capture(struct sun4i_codec *scodec)
261{
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100262 /* Disable ADC DRQ */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800263 regmap_field_update_bits(scodec->reg_adc_fifoc,
264 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN), 0);
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100265}
266
Emilio López45fb6b62015-09-12 15:26:24 +0200267static int sun4i_codec_trigger(struct snd_pcm_substream *substream, int cmd,
268 struct snd_soc_dai *dai)
269{
270 struct snd_soc_pcm_runtime *rtd = substream->private_data;
271 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
272
Emilio López45fb6b62015-09-12 15:26:24 +0200273 switch (cmd) {
274 case SNDRV_PCM_TRIGGER_START:
275 case SNDRV_PCM_TRIGGER_RESUME:
276 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100277 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
278 sun4i_codec_start_playback(scodec);
279 else
280 sun4i_codec_start_capture(scodec);
Emilio López45fb6b62015-09-12 15:26:24 +0200281 break;
282
283 case SNDRV_PCM_TRIGGER_STOP:
284 case SNDRV_PCM_TRIGGER_SUSPEND:
285 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100286 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
287 sun4i_codec_stop_playback(scodec);
288 else
289 sun4i_codec_stop_capture(scodec);
Emilio López45fb6b62015-09-12 15:26:24 +0200290 break;
291
292 default:
293 return -EINVAL;
294 }
295
296 return 0;
297}
298
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100299static int sun4i_codec_prepare_capture(struct snd_pcm_substream *substream,
300 struct snd_soc_dai *dai)
301{
302 struct snd_soc_pcm_runtime *rtd = substream->private_data;
303 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
304
305
306 /* Flush RX FIFO */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800307 regmap_field_update_bits(scodec->reg_adc_fifoc,
308 BIT(SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH),
309 BIT(SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH));
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100310
311
312 /* Set RX FIFO trigger level */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800313 regmap_field_update_bits(scodec->reg_adc_fifoc,
314 0xf << SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL,
315 0x7 << SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL);
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100316
317 /*
318 * FIXME: Undocumented in the datasheet, but
319 * Allwinner's code mentions that it is related
320 * related to microphone gain
321 */
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800322 if (of_device_is_compatible(scodec->dev->of_node,
323 "allwinner,sun4i-a10-codec") ||
324 of_device_is_compatible(scodec->dev->of_node,
325 "allwinner,sun7i-a20-codec")) {
326 regmap_update_bits(scodec->regmap, SUN4I_CODEC_ADC_ACTL,
327 0x3 << 25,
328 0x1 << 25);
329 }
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100330
331 if (of_device_is_compatible(scodec->dev->of_node,
332 "allwinner,sun7i-a20-codec"))
333 /* FIXME: Undocumented bits */
334 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_TUNE,
335 0x3 << 8,
336 0x1 << 8);
337
338 /* Fill most significant bits with valid data MSB */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800339 regmap_field_update_bits(scodec->reg_adc_fifoc,
340 BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE),
341 BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE));
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100342
343 return 0;
344}
345
346static int sun4i_codec_prepare_playback(struct snd_pcm_substream *substream,
347 struct snd_soc_dai *dai)
Emilio López45fb6b62015-09-12 15:26:24 +0200348{
349 struct snd_soc_pcm_runtime *rtd = substream->private_data;
350 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
351 u32 val;
352
Emilio López45fb6b62015-09-12 15:26:24 +0200353 /* Flush the TX FIFO */
354 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
355 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH),
356 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH));
357
358 /* Set TX FIFO Empty Trigger Level */
359 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
360 0x3f << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL,
361 0xf << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL);
362
363 if (substream->runtime->rate > 32000)
364 /* Use 64 bits FIR filter */
365 val = 0;
366 else
367 /* Use 32 bits FIR filter */
368 val = BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION);
369
370 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
371 BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION),
372 val);
373
374 /* Send zeros when we have an underrun */
375 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
376 BIT(SUN4I_CODEC_DAC_FIFOC_SEND_LASAT),
377 0);
378
379 return 0;
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100380};
381
382static int sun4i_codec_prepare(struct snd_pcm_substream *substream,
383 struct snd_soc_dai *dai)
384{
385 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
386 return sun4i_codec_prepare_playback(substream, dai);
387
388 return sun4i_codec_prepare_capture(substream, dai);
Emilio López45fb6b62015-09-12 15:26:24 +0200389}
390
391static unsigned long sun4i_codec_get_mod_freq(struct snd_pcm_hw_params *params)
392{
393 unsigned int rate = params_rate(params);
394
395 switch (rate) {
396 case 176400:
397 case 88200:
398 case 44100:
399 case 33075:
400 case 22050:
401 case 14700:
402 case 11025:
403 case 7350:
404 return 22579200;
405
406 case 192000:
407 case 96000:
408 case 48000:
409 case 32000:
410 case 24000:
411 case 16000:
412 case 12000:
413 case 8000:
414 return 24576000;
415
416 default:
417 return 0;
418 }
419}
420
421static int sun4i_codec_get_hw_rate(struct snd_pcm_hw_params *params)
422{
423 unsigned int rate = params_rate(params);
424
425 switch (rate) {
426 case 192000:
427 case 176400:
428 return 6;
429
430 case 96000:
431 case 88200:
432 return 7;
433
434 case 48000:
435 case 44100:
436 return 0;
437
438 case 32000:
439 case 33075:
440 return 1;
441
442 case 24000:
443 case 22050:
444 return 2;
445
446 case 16000:
447 case 14700:
448 return 3;
449
450 case 12000:
451 case 11025:
452 return 4;
453
454 case 8000:
455 case 7350:
456 return 5;
457
458 default:
459 return -EINVAL;
460 }
461}
462
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100463static int sun4i_codec_hw_params_capture(struct sun4i_codec *scodec,
464 struct snd_pcm_hw_params *params,
465 unsigned int hwrate)
Emilio López45fb6b62015-09-12 15:26:24 +0200466{
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100467 /* Set ADC sample rate */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800468 regmap_field_update_bits(scodec->reg_adc_fifoc,
469 7 << SUN4I_CODEC_ADC_FIFOC_ADC_FS,
470 hwrate << SUN4I_CODEC_ADC_FIFOC_ADC_FS);
Emilio López45fb6b62015-09-12 15:26:24 +0200471
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100472 /* Set the number of channels we want to use */
473 if (params_channels(params) == 1)
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800474 regmap_field_update_bits(scodec->reg_adc_fifoc,
475 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN),
476 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN));
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100477 else
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800478 regmap_field_update_bits(scodec->reg_adc_fifoc,
479 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN),
480 0);
Emilio López45fb6b62015-09-12 15:26:24 +0200481
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100482 return 0;
483}
Emilio López45fb6b62015-09-12 15:26:24 +0200484
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100485static int sun4i_codec_hw_params_playback(struct sun4i_codec *scodec,
486 struct snd_pcm_hw_params *params,
487 unsigned int hwrate)
488{
489 u32 val;
Emilio López45fb6b62015-09-12 15:26:24 +0200490
491 /* Set DAC sample rate */
492 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
493 7 << SUN4I_CODEC_DAC_FIFOC_DAC_FS,
494 hwrate << SUN4I_CODEC_DAC_FIFOC_DAC_FS);
495
496 /* Set the number of channels we want to use */
497 if (params_channels(params) == 1)
498 val = BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN);
499 else
500 val = 0;
501
502 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
503 BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN),
504 val);
505
506 /* Set the number of sample bits to either 16 or 24 bits */
507 if (hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min == 32) {
508 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
509 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS),
510 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS));
511
512 /* Set TX FIFO mode to padding the LSBs with 0 */
513 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
514 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE),
515 0);
516
517 scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
518 } else {
519 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
520 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS),
521 0);
522
523 /* Set TX FIFO mode to repeat the MSB */
524 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
525 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE),
526 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE));
527
528 scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
529 }
530
531 return 0;
532}
533
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100534static int sun4i_codec_hw_params(struct snd_pcm_substream *substream,
535 struct snd_pcm_hw_params *params,
536 struct snd_soc_dai *dai)
537{
538 struct snd_soc_pcm_runtime *rtd = substream->private_data;
539 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
540 unsigned long clk_freq;
Maxime Ripard8400ddf2015-12-01 12:06:47 +0100541 int ret, hwrate;
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100542
543 clk_freq = sun4i_codec_get_mod_freq(params);
544 if (!clk_freq)
545 return -EINVAL;
546
Maxime Ripard8400ddf2015-12-01 12:06:47 +0100547 ret = clk_set_rate(scodec->clk_module, clk_freq);
548 if (ret)
549 return ret;
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100550
551 hwrate = sun4i_codec_get_hw_rate(params);
552 if (hwrate < 0)
553 return hwrate;
554
555 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
556 return sun4i_codec_hw_params_playback(scodec, params,
557 hwrate);
558
559 return sun4i_codec_hw_params_capture(scodec, params,
560 hwrate);
561}
562
Emilio López45fb6b62015-09-12 15:26:24 +0200563static int sun4i_codec_startup(struct snd_pcm_substream *substream,
564 struct snd_soc_dai *dai)
565{
566 struct snd_soc_pcm_runtime *rtd = substream->private_data;
567 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
568
569 /*
570 * Stop issuing DRQ when we have room for less than 16 samples
571 * in our TX FIFO
572 */
573 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
574 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT,
575 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT);
576
577 return clk_prepare_enable(scodec->clk_module);
578}
579
580static void sun4i_codec_shutdown(struct snd_pcm_substream *substream,
581 struct snd_soc_dai *dai)
582{
583 struct snd_soc_pcm_runtime *rtd = substream->private_data;
584 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
585
586 clk_disable_unprepare(scodec->clk_module);
587}
588
589static const struct snd_soc_dai_ops sun4i_codec_dai_ops = {
590 .startup = sun4i_codec_startup,
591 .shutdown = sun4i_codec_shutdown,
592 .trigger = sun4i_codec_trigger,
593 .hw_params = sun4i_codec_hw_params,
594 .prepare = sun4i_codec_prepare,
595};
596
597static struct snd_soc_dai_driver sun4i_codec_dai = {
598 .name = "Codec",
599 .ops = &sun4i_codec_dai_ops,
600 .playback = {
601 .stream_name = "Codec Playback",
602 .channels_min = 1,
603 .channels_max = 2,
604 .rate_min = 8000,
605 .rate_max = 192000,
606 .rates = SNDRV_PCM_RATE_8000_48000 |
607 SNDRV_PCM_RATE_96000 |
Maxime Riparddebb9722015-09-29 21:43:18 +0200608 SNDRV_PCM_RATE_192000,
Emilio López45fb6b62015-09-12 15:26:24 +0200609 .formats = SNDRV_PCM_FMTBIT_S16_LE |
610 SNDRV_PCM_FMTBIT_S32_LE,
611 .sig_bits = 24,
612 },
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100613 .capture = {
614 .stream_name = "Codec Capture",
615 .channels_min = 1,
616 .channels_max = 2,
617 .rate_min = 8000,
618 .rate_max = 192000,
619 .rates = SNDRV_PCM_RATE_8000_48000 |
620 SNDRV_PCM_RATE_96000 |
621 SNDRV_PCM_RATE_192000 |
622 SNDRV_PCM_RATE_KNOT,
623 .formats = SNDRV_PCM_FMTBIT_S16_LE |
624 SNDRV_PCM_FMTBIT_S32_LE,
625 .sig_bits = 24,
626 },
Emilio López45fb6b62015-09-12 15:26:24 +0200627};
628
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800629/*** sun4i Codec ***/
Emilio López45fb6b62015-09-12 15:26:24 +0200630static const struct snd_kcontrol_new sun4i_codec_pa_mute =
631 SOC_DAPM_SINGLE("Switch", SUN4I_CODEC_DAC_ACTL,
632 SUN4I_CODEC_DAC_ACTL_PA_MUTE, 1, 0);
633
634static DECLARE_TLV_DB_SCALE(sun4i_codec_pa_volume_scale, -6300, 100, 1);
635
Danny Milosavljevicc9e902f2016-09-24 22:05:01 +0200636static const struct snd_kcontrol_new sun4i_codec_controls[] = {
Adam Sampson474d1472015-10-27 21:00:45 +0000637 SOC_SINGLE_TLV("Power Amplifier Volume", SUN4I_CODEC_DAC_ACTL,
Emilio López45fb6b62015-09-12 15:26:24 +0200638 SUN4I_CODEC_DAC_ACTL_PA_VOL, 0x3F, 0,
639 sun4i_codec_pa_volume_scale),
640};
641
642static const struct snd_kcontrol_new sun4i_codec_left_mixer_controls[] = {
643 SOC_DAPM_SINGLE("Left DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
644 SUN4I_CODEC_DAC_ACTL_LDACLMIXS, 1, 0),
645};
646
647static const struct snd_kcontrol_new sun4i_codec_right_mixer_controls[] = {
648 SOC_DAPM_SINGLE("Right DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
649 SUN4I_CODEC_DAC_ACTL_RDACRMIXS, 1, 0),
650 SOC_DAPM_SINGLE("Left DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
651 SUN4I_CODEC_DAC_ACTL_LDACRMIXS, 1, 0),
652};
653
654static const struct snd_kcontrol_new sun4i_codec_pa_mixer_controls[] = {
655 SOC_DAPM_SINGLE("DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
656 SUN4I_CODEC_DAC_ACTL_DACPAS, 1, 0),
657 SOC_DAPM_SINGLE("Mixer Playback Switch", SUN4I_CODEC_DAC_ACTL,
658 SUN4I_CODEC_DAC_ACTL_MIXPAS, 1, 0),
659};
660
Hans de Goedee6415b42015-12-11 19:43:56 +0100661static const struct snd_soc_dapm_widget sun4i_codec_codec_dapm_widgets[] = {
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100662 /* Digital parts of the ADCs */
663 SND_SOC_DAPM_SUPPLY("ADC", SUN4I_CODEC_ADC_FIFOC,
664 SUN4I_CODEC_ADC_FIFOC_EN_AD, 0,
665 NULL, 0),
666
Emilio López45fb6b62015-09-12 15:26:24 +0200667 /* Digital parts of the DACs */
668 SND_SOC_DAPM_SUPPLY("DAC", SUN4I_CODEC_DAC_DPC,
669 SUN4I_CODEC_DAC_DPC_EN_DA, 0,
670 NULL, 0),
671
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100672 /* Analog parts of the ADCs */
673 SND_SOC_DAPM_ADC("Left ADC", "Codec Capture", SUN4I_CODEC_ADC_ACTL,
674 SUN4I_CODEC_ADC_ACTL_ADC_L_EN, 0),
675 SND_SOC_DAPM_ADC("Right ADC", "Codec Capture", SUN4I_CODEC_ADC_ACTL,
676 SUN4I_CODEC_ADC_ACTL_ADC_R_EN, 0),
677
Emilio López45fb6b62015-09-12 15:26:24 +0200678 /* Analog parts of the DACs */
679 SND_SOC_DAPM_DAC("Left DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL,
680 SUN4I_CODEC_DAC_ACTL_DACAENL, 0),
681 SND_SOC_DAPM_DAC("Right DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL,
682 SUN4I_CODEC_DAC_ACTL_DACAENR, 0),
683
684 /* Mixers */
685 SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
686 sun4i_codec_left_mixer_controls,
687 ARRAY_SIZE(sun4i_codec_left_mixer_controls)),
688 SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
689 sun4i_codec_right_mixer_controls,
690 ARRAY_SIZE(sun4i_codec_right_mixer_controls)),
691
692 /* Global Mixer Enable */
693 SND_SOC_DAPM_SUPPLY("Mixer Enable", SUN4I_CODEC_DAC_ACTL,
694 SUN4I_CODEC_DAC_ACTL_MIXEN, 0, NULL, 0),
695
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100696 /* VMIC */
697 SND_SOC_DAPM_SUPPLY("VMIC", SUN4I_CODEC_ADC_ACTL,
698 SUN4I_CODEC_ADC_ACTL_VMICEN, 0, NULL, 0),
699
700 /* Mic Pre-Amplifiers */
701 SND_SOC_DAPM_PGA("MIC1 Pre-Amplifier", SUN4I_CODEC_ADC_ACTL,
702 SUN4I_CODEC_ADC_ACTL_PREG1EN, 0, NULL, 0),
703
Adam Sampson474d1472015-10-27 21:00:45 +0000704 /* Power Amplifier */
705 SND_SOC_DAPM_MIXER("Power Amplifier", SUN4I_CODEC_ADC_ACTL,
Emilio López45fb6b62015-09-12 15:26:24 +0200706 SUN4I_CODEC_ADC_ACTL_PA_EN, 0,
707 sun4i_codec_pa_mixer_controls,
708 ARRAY_SIZE(sun4i_codec_pa_mixer_controls)),
Adam Sampson474d1472015-10-27 21:00:45 +0000709 SND_SOC_DAPM_SWITCH("Power Amplifier Mute", SND_SOC_NOPM, 0, 0,
Emilio López45fb6b62015-09-12 15:26:24 +0200710 &sun4i_codec_pa_mute),
711
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100712 SND_SOC_DAPM_INPUT("Mic1"),
713
Emilio López45fb6b62015-09-12 15:26:24 +0200714 SND_SOC_DAPM_OUTPUT("HP Right"),
715 SND_SOC_DAPM_OUTPUT("HP Left"),
716};
717
Hans de Goedee6415b42015-12-11 19:43:56 +0100718static const struct snd_soc_dapm_route sun4i_codec_codec_dapm_routes[] = {
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100719 /* Left ADC / DAC Routes */
720 { "Left ADC", NULL, "ADC" },
Emilio López45fb6b62015-09-12 15:26:24 +0200721 { "Left DAC", NULL, "DAC" },
722
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100723 /* Right ADC / DAC Routes */
724 { "Right ADC", NULL, "ADC" },
Emilio López45fb6b62015-09-12 15:26:24 +0200725 { "Right DAC", NULL, "DAC" },
726
727 /* Right Mixer Routes */
728 { "Right Mixer", NULL, "Mixer Enable" },
729 { "Right Mixer", "Left DAC Playback Switch", "Left DAC" },
730 { "Right Mixer", "Right DAC Playback Switch", "Right DAC" },
731
732 /* Left Mixer Routes */
733 { "Left Mixer", NULL, "Mixer Enable" },
734 { "Left Mixer", "Left DAC Playback Switch", "Left DAC" },
735
Adam Sampson474d1472015-10-27 21:00:45 +0000736 /* Power Amplifier Routes */
737 { "Power Amplifier", "Mixer Playback Switch", "Left Mixer" },
738 { "Power Amplifier", "Mixer Playback Switch", "Right Mixer" },
739 { "Power Amplifier", "DAC Playback Switch", "Left DAC" },
740 { "Power Amplifier", "DAC Playback Switch", "Right DAC" },
Emilio López45fb6b62015-09-12 15:26:24 +0200741
Adam Sampson474d1472015-10-27 21:00:45 +0000742 /* Headphone Output Routes */
743 { "Power Amplifier Mute", "Switch", "Power Amplifier" },
744 { "HP Right", NULL, "Power Amplifier Mute" },
745 { "HP Left", NULL, "Power Amplifier Mute" },
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100746
747 /* Mic1 Routes */
748 { "Left ADC", NULL, "MIC1 Pre-Amplifier" },
749 { "Right ADC", NULL, "MIC1 Pre-Amplifier" },
750 { "MIC1 Pre-Amplifier", NULL, "Mic1"},
751 { "Mic1", NULL, "VMIC" },
Emilio López45fb6b62015-09-12 15:26:24 +0200752};
753
754static struct snd_soc_codec_driver sun4i_codec_codec = {
Kuninori Morimoto180f58f2016-08-08 08:46:41 +0000755 .component_driver = {
Danny Milosavljevicc9e902f2016-09-24 22:05:01 +0200756 .controls = sun4i_codec_controls,
757 .num_controls = ARRAY_SIZE(sun4i_codec_controls),
Kuninori Morimoto180f58f2016-08-08 08:46:41 +0000758 .dapm_widgets = sun4i_codec_codec_dapm_widgets,
759 .num_dapm_widgets = ARRAY_SIZE(sun4i_codec_codec_dapm_widgets),
760 .dapm_routes = sun4i_codec_codec_dapm_routes,
761 .num_dapm_routes = ARRAY_SIZE(sun4i_codec_codec_dapm_routes),
762 },
Emilio López45fb6b62015-09-12 15:26:24 +0200763};
764
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800765/*** sun6i Codec ***/
766
767/* mixer controls */
768static const struct snd_kcontrol_new sun6i_codec_mixer_controls[] = {
769 SOC_DAPM_DOUBLE("DAC Playback Switch",
770 SUN6I_CODEC_OM_DACA_CTRL,
771 SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACL,
772 SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACR, 1, 0),
773 SOC_DAPM_DOUBLE("DAC Reversed Playback Switch",
774 SUN6I_CODEC_OM_DACA_CTRL,
775 SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACR,
776 SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACL, 1, 0),
Chen-Yu Tsaidff50512016-11-03 15:55:49 +0800777 SOC_DAPM_DOUBLE("Line In Playback Switch",
778 SUN6I_CODEC_OM_DACA_CTRL,
779 SUN6I_CODEC_OM_DACA_CTRL_LMIX_LINEINL,
780 SUN6I_CODEC_OM_DACA_CTRL_RMIX_LINEINR, 1, 0),
Chen-Yu Tsaiecd5cdb2016-11-03 15:55:51 +0800781 SOC_DAPM_DOUBLE("Mic1 Playback Switch",
782 SUN6I_CODEC_OM_DACA_CTRL,
783 SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC1,
784 SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC1, 1, 0),
785 SOC_DAPM_DOUBLE("Mic2 Playback Switch",
786 SUN6I_CODEC_OM_DACA_CTRL,
787 SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC2,
788 SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC2, 1, 0),
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800789};
790
Chen-Yu Tsai24c99f82016-11-07 18:06:59 +0800791/* ADC mixer controls */
792static const struct snd_kcontrol_new sun6i_codec_adc_mixer_controls[] = {
793 SOC_DAPM_DOUBLE("Mixer Capture Switch",
794 SUN6I_CODEC_ADC_ACTL,
795 SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXL,
796 SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXR, 1, 0),
797 SOC_DAPM_DOUBLE("Mixer Reversed Capture Switch",
798 SUN6I_CODEC_ADC_ACTL,
799 SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXR,
800 SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXL, 1, 0),
801 SOC_DAPM_DOUBLE("Line In Capture Switch",
802 SUN6I_CODEC_ADC_ACTL,
803 SUN6I_CODEC_ADC_ACTL_LADCMIX_LINEINL,
804 SUN6I_CODEC_ADC_ACTL_RADCMIX_LINEINR, 1, 0),
805 SOC_DAPM_DOUBLE("Mic1 Capture Switch",
806 SUN6I_CODEC_ADC_ACTL,
807 SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC1,
808 SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC1, 1, 0),
809 SOC_DAPM_DOUBLE("Mic2 Capture Switch",
810 SUN6I_CODEC_ADC_ACTL,
811 SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC2,
812 SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC2, 1, 0),
813};
814
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800815/* headphone controls */
816static const char * const sun6i_codec_hp_src_enum_text[] = {
817 "DAC", "Mixer",
818};
819
820static SOC_ENUM_DOUBLE_DECL(sun6i_codec_hp_src_enum,
821 SUN6I_CODEC_OM_DACA_CTRL,
822 SUN6I_CODEC_OM_DACA_CTRL_LHPIS,
823 SUN6I_CODEC_OM_DACA_CTRL_RHPIS,
824 sun6i_codec_hp_src_enum_text);
825
826static const struct snd_kcontrol_new sun6i_codec_hp_src[] = {
827 SOC_DAPM_ENUM("Headphone Source Playback Route",
828 sun6i_codec_hp_src_enum),
829};
830
Chen-Yu Tsaiecd5cdb2016-11-03 15:55:51 +0800831/* microphone controls */
832static const char * const sun6i_codec_mic2_src_enum_text[] = {
833 "Mic2", "Mic3",
834};
835
836static SOC_ENUM_SINGLE_DECL(sun6i_codec_mic2_src_enum,
837 SUN6I_CODEC_MIC_CTRL,
838 SUN6I_CODEC_MIC_CTRL_MIC2SLT,
839 sun6i_codec_mic2_src_enum_text);
840
841static const struct snd_kcontrol_new sun6i_codec_mic2_src[] = {
842 SOC_DAPM_ENUM("Mic2 Amplifier Source Route",
843 sun6i_codec_mic2_src_enum),
844};
845
Chen-Yu Tsai0f909f92016-11-03 15:55:50 +0800846/* line out controls */
847static const char * const sun6i_codec_lineout_src_enum_text[] = {
848 "Stereo", "Mono Differential",
849};
850
851static SOC_ENUM_DOUBLE_DECL(sun6i_codec_lineout_src_enum,
852 SUN6I_CODEC_MIC_CTRL,
853 SUN6I_CODEC_MIC_CTRL_LINEOUTLSRC,
854 SUN6I_CODEC_MIC_CTRL_LINEOUTRSRC,
855 sun6i_codec_lineout_src_enum_text);
856
857static const struct snd_kcontrol_new sun6i_codec_lineout_src[] = {
858 SOC_DAPM_ENUM("Line Out Source Playback Route",
859 sun6i_codec_lineout_src_enum),
860};
861
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800862/* volume / mute controls */
863static const DECLARE_TLV_DB_SCALE(sun6i_codec_dvol_scale, -7308, 116, 0);
864static const DECLARE_TLV_DB_SCALE(sun6i_codec_hp_vol_scale, -6300, 100, 1);
Chen-Yu Tsaidff50512016-11-03 15:55:49 +0800865static const DECLARE_TLV_DB_SCALE(sun6i_codec_out_mixer_pregain_scale,
866 -450, 150, 0);
Chen-Yu Tsai0f909f92016-11-03 15:55:50 +0800867static const DECLARE_TLV_DB_RANGE(sun6i_codec_lineout_vol_scale,
868 0, 1, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
869 2, 31, TLV_DB_SCALE_ITEM(-4350, 150, 0),
870);
Chen-Yu Tsaiecd5cdb2016-11-03 15:55:51 +0800871static const DECLARE_TLV_DB_RANGE(sun6i_codec_mic_gain_scale,
872 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
873 1, 7, TLV_DB_SCALE_ITEM(2400, 300, 0),
874);
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800875
876static const struct snd_kcontrol_new sun6i_codec_codec_widgets[] = {
877 SOC_SINGLE_TLV("DAC Playback Volume", SUN4I_CODEC_DAC_DPC,
878 SUN4I_CODEC_DAC_DPC_DVOL, 0x3f, 1,
879 sun6i_codec_dvol_scale),
880 SOC_SINGLE_TLV("Headphone Playback Volume",
881 SUN6I_CODEC_OM_DACA_CTRL,
882 SUN6I_CODEC_OM_DACA_CTRL_HPVOL, 0x3f, 0,
883 sun6i_codec_hp_vol_scale),
Chen-Yu Tsai0f909f92016-11-03 15:55:50 +0800884 SOC_SINGLE_TLV("Line Out Playback Volume",
885 SUN6I_CODEC_MIC_CTRL,
886 SUN6I_CODEC_MIC_CTRL_LINEOUTVC, 0x1f, 0,
887 sun6i_codec_lineout_vol_scale),
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800888 SOC_DOUBLE("Headphone Playback Switch",
889 SUN6I_CODEC_OM_DACA_CTRL,
890 SUN6I_CODEC_OM_DACA_CTRL_LHPPAMUTE,
891 SUN6I_CODEC_OM_DACA_CTRL_RHPPAMUTE, 1, 0),
Chen-Yu Tsai0f909f92016-11-03 15:55:50 +0800892 SOC_DOUBLE("Line Out Playback Switch",
893 SUN6I_CODEC_MIC_CTRL,
894 SUN6I_CODEC_MIC_CTRL_LINEOUTLEN,
895 SUN6I_CODEC_MIC_CTRL_LINEOUTREN, 1, 0),
Chen-Yu Tsaidff50512016-11-03 15:55:49 +0800896 /* Mixer pre-gains */
897 SOC_SINGLE_TLV("Line In Playback Volume",
898 SUN6I_CODEC_OM_PA_CTRL, SUN6I_CODEC_OM_PA_CTRL_LINEING,
899 0x7, 0, sun6i_codec_out_mixer_pregain_scale),
Chen-Yu Tsaiecd5cdb2016-11-03 15:55:51 +0800900 SOC_SINGLE_TLV("Mic1 Playback Volume",
901 SUN6I_CODEC_OM_PA_CTRL, SUN6I_CODEC_OM_PA_CTRL_MIC1G,
902 0x7, 0, sun6i_codec_out_mixer_pregain_scale),
903 SOC_SINGLE_TLV("Mic2 Playback Volume",
904 SUN6I_CODEC_OM_PA_CTRL, SUN6I_CODEC_OM_PA_CTRL_MIC2G,
905 0x7, 0, sun6i_codec_out_mixer_pregain_scale),
906
907 /* Microphone Amp boost gains */
908 SOC_SINGLE_TLV("Mic1 Boost Volume", SUN6I_CODEC_MIC_CTRL,
909 SUN6I_CODEC_MIC_CTRL_MIC1BOOST, 0x7, 0,
910 sun6i_codec_mic_gain_scale),
911 SOC_SINGLE_TLV("Mic2 Boost Volume", SUN6I_CODEC_MIC_CTRL,
912 SUN6I_CODEC_MIC_CTRL_MIC2BOOST, 0x7, 0,
913 sun6i_codec_mic_gain_scale),
Chen-Yu Tsai24c99f82016-11-07 18:06:59 +0800914 SOC_DOUBLE_TLV("ADC Capture Volume",
915 SUN6I_CODEC_ADC_ACTL, SUN6I_CODEC_ADC_ACTL_ADCLG,
916 SUN6I_CODEC_ADC_ACTL_ADCRG, 0x7, 0,
917 sun6i_codec_out_mixer_pregain_scale),
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800918};
919
920static const struct snd_soc_dapm_widget sun6i_codec_codec_dapm_widgets[] = {
Chen-Yu Tsaiecd5cdb2016-11-03 15:55:51 +0800921 /* Microphone inputs */
922 SND_SOC_DAPM_INPUT("MIC1"),
923 SND_SOC_DAPM_INPUT("MIC2"),
924 SND_SOC_DAPM_INPUT("MIC3"),
925
926 /* Microphone Bias */
927 SND_SOC_DAPM_SUPPLY("HBIAS", SUN6I_CODEC_MIC_CTRL,
928 SUN6I_CODEC_MIC_CTRL_HBIASEN, 0, NULL, 0),
929 SND_SOC_DAPM_SUPPLY("MBIAS", SUN6I_CODEC_MIC_CTRL,
930 SUN6I_CODEC_MIC_CTRL_MBIASEN, 0, NULL, 0),
931
932 /* Mic input path */
933 SND_SOC_DAPM_MUX("Mic2 Amplifier Source Route",
934 SND_SOC_NOPM, 0, 0, sun6i_codec_mic2_src),
935 SND_SOC_DAPM_PGA("Mic1 Amplifier", SUN6I_CODEC_MIC_CTRL,
936 SUN6I_CODEC_MIC_CTRL_MIC1AMPEN, 0, NULL, 0),
937 SND_SOC_DAPM_PGA("Mic2 Amplifier", SUN6I_CODEC_MIC_CTRL,
938 SUN6I_CODEC_MIC_CTRL_MIC2AMPEN, 0, NULL, 0),
939
Chen-Yu Tsaidff50512016-11-03 15:55:49 +0800940 /* Line In */
941 SND_SOC_DAPM_INPUT("LINEIN"),
942
Chen-Yu Tsai24c99f82016-11-07 18:06:59 +0800943 /* Digital parts of the ADCs */
944 SND_SOC_DAPM_SUPPLY("ADC Enable", SUN6I_CODEC_ADC_FIFOC,
945 SUN6I_CODEC_ADC_FIFOC_EN_AD, 0,
946 NULL, 0),
947
948 /* Analog parts of the ADCs */
949 SND_SOC_DAPM_ADC("Left ADC", "Codec Capture", SUN6I_CODEC_ADC_ACTL,
950 SUN6I_CODEC_ADC_ACTL_ADCLEN, 0),
951 SND_SOC_DAPM_ADC("Right ADC", "Codec Capture", SUN6I_CODEC_ADC_ACTL,
952 SUN6I_CODEC_ADC_ACTL_ADCREN, 0),
953
954 /* ADC Mixers */
955 SOC_MIXER_ARRAY("Left ADC Mixer", SND_SOC_NOPM, 0, 0,
956 sun6i_codec_adc_mixer_controls),
957 SOC_MIXER_ARRAY("Right ADC Mixer", SND_SOC_NOPM, 0, 0,
958 sun6i_codec_adc_mixer_controls),
959
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800960 /* Digital parts of the DACs */
961 SND_SOC_DAPM_SUPPLY("DAC Enable", SUN4I_CODEC_DAC_DPC,
962 SUN4I_CODEC_DAC_DPC_EN_DA, 0,
963 NULL, 0),
964
965 /* Analog parts of the DACs */
966 SND_SOC_DAPM_DAC("Left DAC", "Codec Playback",
967 SUN6I_CODEC_OM_DACA_CTRL,
968 SUN6I_CODEC_OM_DACA_CTRL_DACALEN, 0),
969 SND_SOC_DAPM_DAC("Right DAC", "Codec Playback",
970 SUN6I_CODEC_OM_DACA_CTRL,
971 SUN6I_CODEC_OM_DACA_CTRL_DACAREN, 0),
972
973 /* Mixers */
974 SOC_MIXER_ARRAY("Left Mixer", SUN6I_CODEC_OM_DACA_CTRL,
975 SUN6I_CODEC_OM_DACA_CTRL_LMIXEN, 0,
976 sun6i_codec_mixer_controls),
977 SOC_MIXER_ARRAY("Right Mixer", SUN6I_CODEC_OM_DACA_CTRL,
978 SUN6I_CODEC_OM_DACA_CTRL_RMIXEN, 0,
979 sun6i_codec_mixer_controls),
980
981 /* Headphone output path */
982 SND_SOC_DAPM_MUX("Headphone Source Playback Route",
983 SND_SOC_NOPM, 0, 0, sun6i_codec_hp_src),
984 SND_SOC_DAPM_OUT_DRV("Headphone Amp", SUN6I_CODEC_OM_PA_CTRL,
985 SUN6I_CODEC_OM_PA_CTRL_HPPAEN, 0, NULL, 0),
986 SND_SOC_DAPM_SUPPLY("HPCOM Protection", SUN6I_CODEC_OM_PA_CTRL,
987 SUN6I_CODEC_OM_PA_CTRL_COMPTEN, 0, NULL, 0),
988 SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPCOM", SUN6I_CODEC_OM_PA_CTRL,
989 SUN6I_CODEC_OM_PA_CTRL_HPCOM_CTL, 0x3, 0x3, 0),
990 SND_SOC_DAPM_OUTPUT("HP"),
Chen-Yu Tsai0f909f92016-11-03 15:55:50 +0800991
992 /* Line Out path */
993 SND_SOC_DAPM_MUX("Line Out Source Playback Route",
994 SND_SOC_NOPM, 0, 0, sun6i_codec_lineout_src),
995 SND_SOC_DAPM_OUTPUT("LINEOUT"),
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800996};
997
998static const struct snd_soc_dapm_route sun6i_codec_codec_dapm_routes[] = {
999 /* DAC Routes */
1000 { "Left DAC", NULL, "DAC Enable" },
1001 { "Right DAC", NULL, "DAC Enable" },
1002
Chen-Yu Tsaiecd5cdb2016-11-03 15:55:51 +08001003 /* Microphone Routes */
1004 { "Mic1 Amplifier", NULL, "MIC1"},
1005 { "Mic2 Amplifier Source Route", "Mic2", "MIC2" },
1006 { "Mic2 Amplifier Source Route", "Mic3", "MIC3" },
1007 { "Mic2 Amplifier", NULL, "Mic2 Amplifier Source Route"},
1008
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001009 /* Left Mixer Routes */
1010 { "Left Mixer", "DAC Playback Switch", "Left DAC" },
1011 { "Left Mixer", "DAC Reversed Playback Switch", "Right DAC" },
Chen-Yu Tsaidff50512016-11-03 15:55:49 +08001012 { "Left Mixer", "Line In Playback Switch", "LINEIN" },
Chen-Yu Tsaiecd5cdb2016-11-03 15:55:51 +08001013 { "Left Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
1014 { "Left Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001015
1016 /* Right Mixer Routes */
1017 { "Right Mixer", "DAC Playback Switch", "Right DAC" },
1018 { "Right Mixer", "DAC Reversed Playback Switch", "Left DAC" },
Chen-Yu Tsaidff50512016-11-03 15:55:49 +08001019 { "Right Mixer", "Line In Playback Switch", "LINEIN" },
Chen-Yu Tsaiecd5cdb2016-11-03 15:55:51 +08001020 { "Right Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
1021 { "Right Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001022
Chen-Yu Tsai24c99f82016-11-07 18:06:59 +08001023 /* Left ADC Mixer Routes */
1024 { "Left ADC Mixer", "Mixer Capture Switch", "Left Mixer" },
1025 { "Left ADC Mixer", "Mixer Reversed Capture Switch", "Right Mixer" },
1026 { "Left ADC Mixer", "Line In Capture Switch", "LINEIN" },
1027 { "Left ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
1028 { "Left ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
1029
1030 /* Right ADC Mixer Routes */
1031 { "Right ADC Mixer", "Mixer Capture Switch", "Right Mixer" },
1032 { "Right ADC Mixer", "Mixer Reversed Capture Switch", "Left Mixer" },
1033 { "Right ADC Mixer", "Line In Capture Switch", "LINEIN" },
1034 { "Right ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
1035 { "Right ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
1036
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001037 /* Headphone Routes */
1038 { "Headphone Source Playback Route", "DAC", "Left DAC" },
1039 { "Headphone Source Playback Route", "DAC", "Right DAC" },
1040 { "Headphone Source Playback Route", "Mixer", "Left Mixer" },
1041 { "Headphone Source Playback Route", "Mixer", "Right Mixer" },
1042 { "Headphone Amp", NULL, "Headphone Source Playback Route" },
1043 { "HP", NULL, "Headphone Amp" },
1044 { "HPCOM", NULL, "HPCOM Protection" },
Chen-Yu Tsai0f909f92016-11-03 15:55:50 +08001045
1046 /* Line Out Routes */
1047 { "Line Out Source Playback Route", "Stereo", "Left Mixer" },
1048 { "Line Out Source Playback Route", "Stereo", "Right Mixer" },
1049 { "Line Out Source Playback Route", "Mono Differential", "Left Mixer" },
1050 { "LINEOUT", NULL, "Line Out Source Playback Route" },
Chen-Yu Tsai24c99f82016-11-07 18:06:59 +08001051
1052 /* ADC Routes */
1053 { "Left ADC", NULL, "ADC Enable" },
1054 { "Right ADC", NULL, "ADC Enable" },
1055 { "Left ADC", NULL, "Left ADC Mixer" },
1056 { "Right ADC", NULL, "Right ADC Mixer" },
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001057};
1058
1059static struct snd_soc_codec_driver sun6i_codec_codec = {
1060 .component_driver = {
1061 .controls = sun6i_codec_codec_widgets,
1062 .num_controls = ARRAY_SIZE(sun6i_codec_codec_widgets),
1063 .dapm_widgets = sun6i_codec_codec_dapm_widgets,
1064 .num_dapm_widgets = ARRAY_SIZE(sun6i_codec_codec_dapm_widgets),
1065 .dapm_routes = sun6i_codec_codec_dapm_routes,
1066 .num_dapm_routes = ARRAY_SIZE(sun6i_codec_codec_dapm_routes),
1067 },
1068};
1069
Emilio López45fb6b62015-09-12 15:26:24 +02001070static const struct snd_soc_component_driver sun4i_codec_component = {
1071 .name = "sun4i-codec",
1072};
1073
1074#define SUN4I_CODEC_RATES SNDRV_PCM_RATE_8000_192000
1075#define SUN4I_CODEC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
1076 SNDRV_PCM_FMTBIT_S32_LE)
1077
1078static int sun4i_codec_dai_probe(struct snd_soc_dai *dai)
1079{
1080 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
1081 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
1082
1083 snd_soc_dai_init_dma_data(dai, &scodec->playback_dma_data,
Maxime Ripard1fb34b42015-11-30 16:37:47 +01001084 &scodec->capture_dma_data);
Emilio López45fb6b62015-09-12 15:26:24 +02001085
1086 return 0;
1087}
1088
1089static struct snd_soc_dai_driver dummy_cpu_dai = {
1090 .name = "sun4i-codec-cpu-dai",
1091 .probe = sun4i_codec_dai_probe,
1092 .playback = {
1093 .stream_name = "Playback",
1094 .channels_min = 1,
1095 .channels_max = 2,
1096 .rates = SUN4I_CODEC_RATES,
1097 .formats = SUN4I_CODEC_FORMATS,
1098 .sig_bits = 24,
1099 },
Maxime Ripard1fb34b42015-11-30 16:37:47 +01001100 .capture = {
1101 .stream_name = "Capture",
1102 .channels_min = 1,
1103 .channels_max = 2,
1104 .rates = SUN4I_CODEC_RATES,
1105 .formats = SUN4I_CODEC_FORMATS,
1106 .sig_bits = 24,
1107 },
Emilio López45fb6b62015-09-12 15:26:24 +02001108};
1109
Emilio López45fb6b62015-09-12 15:26:24 +02001110static struct snd_soc_dai_link *sun4i_codec_create_link(struct device *dev,
1111 int *num_links)
1112{
1113 struct snd_soc_dai_link *link = devm_kzalloc(dev, sizeof(*link),
1114 GFP_KERNEL);
1115 if (!link)
1116 return NULL;
1117
1118 link->name = "cdc";
1119 link->stream_name = "CDC PCM";
1120 link->codec_dai_name = "Codec";
1121 link->cpu_dai_name = dev_name(dev);
1122 link->codec_name = dev_name(dev);
1123 link->platform_name = dev_name(dev);
1124 link->dai_fmt = SND_SOC_DAIFMT_I2S;
1125
1126 *num_links = 1;
1127
1128 return link;
1129};
1130
Hans de Goede40592622015-12-11 19:43:57 +01001131static int sun4i_codec_spk_event(struct snd_soc_dapm_widget *w,
1132 struct snd_kcontrol *k, int event)
1133{
1134 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(w->dapm->card);
1135
1136 if (scodec->gpio_pa)
1137 gpiod_set_value_cansleep(scodec->gpio_pa,
1138 !!SND_SOC_DAPM_EVENT_ON(event));
1139
1140 return 0;
1141}
1142
1143static const struct snd_soc_dapm_widget sun4i_codec_card_dapm_widgets[] = {
1144 SND_SOC_DAPM_SPK("Speaker", sun4i_codec_spk_event),
1145};
1146
1147static const struct snd_soc_dapm_route sun4i_codec_card_dapm_routes[] = {
Hans de Goede6b803c62015-12-22 23:00:17 +01001148 { "Speaker", NULL, "HP Right" },
1149 { "Speaker", NULL, "HP Left" },
Hans de Goede40592622015-12-11 19:43:57 +01001150};
1151
Emilio López45fb6b62015-09-12 15:26:24 +02001152static struct snd_soc_card *sun4i_codec_create_card(struct device *dev)
1153{
1154 struct snd_soc_card *card;
Emilio López45fb6b62015-09-12 15:26:24 +02001155
1156 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
1157 if (!card)
Chen-Yu Tsai85915b62016-10-31 14:42:09 +08001158 return ERR_PTR(-ENOMEM);
Emilio López45fb6b62015-09-12 15:26:24 +02001159
1160 card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
1161 if (!card->dai_link)
Chen-Yu Tsai85915b62016-10-31 14:42:09 +08001162 return ERR_PTR(-ENOMEM);
Emilio López45fb6b62015-09-12 15:26:24 +02001163
1164 card->dev = dev;
1165 card->name = "sun4i-codec";
Hans de Goede40592622015-12-11 19:43:57 +01001166 card->dapm_widgets = sun4i_codec_card_dapm_widgets;
1167 card->num_dapm_widgets = ARRAY_SIZE(sun4i_codec_card_dapm_widgets);
1168 card->dapm_routes = sun4i_codec_card_dapm_routes;
1169 card->num_dapm_routes = ARRAY_SIZE(sun4i_codec_card_dapm_routes);
Emilio López45fb6b62015-09-12 15:26:24 +02001170
Emilio López45fb6b62015-09-12 15:26:24 +02001171 return card;
1172};
1173
Chen-Yu Tsai300a18d2016-11-03 15:55:53 +08001174static const struct snd_soc_dapm_widget sun6i_codec_card_dapm_widgets[] = {
1175 SND_SOC_DAPM_HP("Headphone", NULL),
1176 SND_SOC_DAPM_LINE("Line In", NULL),
1177 SND_SOC_DAPM_LINE("Line Out", NULL),
1178 SND_SOC_DAPM_MIC("Headset Mic", NULL),
1179 SND_SOC_DAPM_MIC("Mic", NULL),
1180 SND_SOC_DAPM_SPK("Speaker", sun4i_codec_spk_event),
1181};
1182
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001183static struct snd_soc_card *sun6i_codec_create_card(struct device *dev)
1184{
1185 struct snd_soc_card *card;
Chen-Yu Tsai300a18d2016-11-03 15:55:53 +08001186 int ret;
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001187
1188 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
1189 if (!card)
1190 return ERR_PTR(-ENOMEM);
1191
1192 card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
1193 if (!card->dai_link)
1194 return ERR_PTR(-ENOMEM);
1195
Chen-Yu Tsai300a18d2016-11-03 15:55:53 +08001196 card->dev = dev;
1197 card->name = "A31 Audio Codec";
1198 card->dapm_widgets = sun6i_codec_card_dapm_widgets;
1199 card->num_dapm_widgets = ARRAY_SIZE(sun6i_codec_card_dapm_widgets);
1200 card->fully_routed = true;
1201
1202 ret = snd_soc_of_parse_audio_routing(card, "allwinner,audio-routing");
1203 if (ret)
1204 dev_warn(dev, "failed to parse audio-routing: %d\n", ret);
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001205
1206 return card;
1207};
1208
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001209static const struct regmap_config sun4i_codec_regmap_config = {
1210 .reg_bits = 32,
1211 .reg_stride = 4,
1212 .val_bits = 32,
1213 .max_register = SUN4I_CODEC_ADC_RXCNT,
1214};
1215
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001216static const struct regmap_config sun6i_codec_regmap_config = {
1217 .reg_bits = 32,
1218 .reg_stride = 4,
1219 .val_bits = 32,
1220 .max_register = SUN6I_CODEC_HMIC_DATA,
1221};
1222
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001223static const struct regmap_config sun7i_codec_regmap_config = {
1224 .reg_bits = 32,
1225 .reg_stride = 4,
1226 .val_bits = 32,
1227 .max_register = SUN7I_CODEC_AC_MIC_PHONE_CAL,
1228};
1229
1230struct sun4i_codec_quirks {
1231 const struct regmap_config *regmap_config;
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001232 const struct snd_soc_codec_driver *codec;
1233 struct snd_soc_card * (*create_card)(struct device *dev);
1234 struct reg_field reg_adc_fifoc; /* used for regmap_field */
1235 unsigned int reg_dac_txdata; /* TX FIFO offset for DMA config */
1236 unsigned int reg_adc_rxdata; /* RX FIFO offset for DMA config */
Chen-Yu Tsai9aead152016-11-07 18:06:58 +08001237 bool has_reset;
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001238};
1239
1240static const struct sun4i_codec_quirks sun4i_codec_quirks = {
1241 .regmap_config = &sun4i_codec_regmap_config,
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001242 .codec = &sun4i_codec_codec,
1243 .create_card = sun4i_codec_create_card,
1244 .reg_adc_fifoc = REG_FIELD(SUN4I_CODEC_ADC_FIFOC, 0, 31),
1245 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
1246 .reg_adc_rxdata = SUN4I_CODEC_ADC_RXDATA,
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001247};
1248
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001249static const struct sun4i_codec_quirks sun6i_a31_codec_quirks = {
1250 .regmap_config = &sun6i_codec_regmap_config,
1251 .codec = &sun6i_codec_codec,
1252 .create_card = sun6i_codec_create_card,
1253 .reg_adc_fifoc = REG_FIELD(SUN6I_CODEC_ADC_FIFOC, 0, 31),
1254 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
1255 .reg_adc_rxdata = SUN6I_CODEC_ADC_RXDATA,
1256 .has_reset = true,
1257};
1258
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001259static const struct sun4i_codec_quirks sun7i_codec_quirks = {
1260 .regmap_config = &sun7i_codec_regmap_config,
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001261 .codec = &sun4i_codec_codec,
1262 .create_card = sun4i_codec_create_card,
1263 .reg_adc_fifoc = REG_FIELD(SUN4I_CODEC_ADC_FIFOC, 0, 31),
1264 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
1265 .reg_adc_rxdata = SUN4I_CODEC_ADC_RXDATA,
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001266};
1267
1268static const struct of_device_id sun4i_codec_of_match[] = {
1269 {
1270 .compatible = "allwinner,sun4i-a10-codec",
1271 .data = &sun4i_codec_quirks,
1272 },
1273 {
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001274 .compatible = "allwinner,sun6i-a31-codec",
1275 .data = &sun6i_a31_codec_quirks,
1276 },
1277 {
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001278 .compatible = "allwinner,sun7i-a20-codec",
1279 .data = &sun7i_codec_quirks,
1280 },
1281 {}
1282};
1283MODULE_DEVICE_TABLE(of, sun4i_codec_of_match);
1284
Emilio López45fb6b62015-09-12 15:26:24 +02001285static int sun4i_codec_probe(struct platform_device *pdev)
1286{
1287 struct snd_soc_card *card;
1288 struct sun4i_codec *scodec;
Danny Milosavljevicc1d50652016-09-22 09:13:13 +02001289 const struct sun4i_codec_quirks *quirks;
Emilio López45fb6b62015-09-12 15:26:24 +02001290 struct resource *res;
1291 void __iomem *base;
1292 int ret;
1293
1294 scodec = devm_kzalloc(&pdev->dev, sizeof(*scodec), GFP_KERNEL);
1295 if (!scodec)
1296 return -ENOMEM;
1297
1298 scodec->dev = &pdev->dev;
1299
1300 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1301 base = devm_ioremap_resource(&pdev->dev, res);
1302 if (IS_ERR(base)) {
1303 dev_err(&pdev->dev, "Failed to map the registers\n");
1304 return PTR_ERR(base);
1305 }
1306
Danny Milosavljevicc1d50652016-09-22 09:13:13 +02001307 quirks = of_device_get_match_data(&pdev->dev);
1308 if (quirks == NULL) {
1309 dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
1310 return -ENODEV;
1311 }
1312
Emilio López45fb6b62015-09-12 15:26:24 +02001313 scodec->regmap = devm_regmap_init_mmio(&pdev->dev, base,
Danny Milosavljevicc1d50652016-09-22 09:13:13 +02001314 quirks->regmap_config);
Emilio López45fb6b62015-09-12 15:26:24 +02001315 if (IS_ERR(scodec->regmap)) {
1316 dev_err(&pdev->dev, "Failed to create our regmap\n");
1317 return PTR_ERR(scodec->regmap);
1318 }
1319
1320 /* Get the clocks from the DT */
1321 scodec->clk_apb = devm_clk_get(&pdev->dev, "apb");
1322 if (IS_ERR(scodec->clk_apb)) {
1323 dev_err(&pdev->dev, "Failed to get the APB clock\n");
1324 return PTR_ERR(scodec->clk_apb);
1325 }
1326
1327 scodec->clk_module = devm_clk_get(&pdev->dev, "codec");
1328 if (IS_ERR(scodec->clk_module)) {
1329 dev_err(&pdev->dev, "Failed to get the module clock\n");
1330 return PTR_ERR(scodec->clk_module);
1331 }
1332
Chen-Yu Tsai9aead152016-11-07 18:06:58 +08001333 if (quirks->has_reset) {
1334 scodec->rst = devm_reset_control_get(&pdev->dev, NULL);
1335 if (IS_ERR(scodec->rst)) {
1336 dev_err(&pdev->dev, "Failed to get reset control\n");
1337 return PTR_ERR(scodec->rst);
1338 }
1339 };
1340
Hans de Goede40592622015-12-11 19:43:57 +01001341 scodec->gpio_pa = devm_gpiod_get_optional(&pdev->dev, "allwinner,pa",
1342 GPIOD_OUT_LOW);
1343 if (IS_ERR(scodec->gpio_pa)) {
1344 ret = PTR_ERR(scodec->gpio_pa);
1345 if (ret != -EPROBE_DEFER)
1346 dev_err(&pdev->dev, "Failed to get pa gpio: %d\n", ret);
1347 return ret;
1348 }
1349
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001350 /* reg_field setup */
1351 scodec->reg_adc_fifoc = devm_regmap_field_alloc(&pdev->dev,
1352 scodec->regmap,
1353 quirks->reg_adc_fifoc);
1354 if (IS_ERR(scodec->reg_adc_fifoc)) {
1355 ret = PTR_ERR(scodec->reg_adc_fifoc);
1356 dev_err(&pdev->dev, "Failed to create regmap fields: %d\n",
1357 ret);
1358 return ret;
1359 }
1360
Chen-Yu Tsai3716a892016-11-01 14:31:55 +08001361 /* Enable the bus clock */
1362 if (clk_prepare_enable(scodec->clk_apb)) {
1363 dev_err(&pdev->dev, "Failed to enable the APB clock\n");
1364 return -EINVAL;
1365 }
1366
Chen-Yu Tsai9aead152016-11-07 18:06:58 +08001367 /* Deassert the reset control */
1368 if (scodec->rst) {
1369 ret = reset_control_deassert(scodec->rst);
1370 if (ret) {
1371 dev_err(&pdev->dev,
1372 "Failed to deassert the reset control\n");
1373 goto err_clk_disable;
1374 }
1375 }
1376
Emilio López45fb6b62015-09-12 15:26:24 +02001377 /* DMA configuration for TX FIFO */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001378 scodec->playback_dma_data.addr = res->start + quirks->reg_dac_txdata;
Chen-Yu Tsai730e2dd2016-11-03 15:55:46 +08001379 scodec->playback_dma_data.maxburst = 8;
Emilio López45fb6b62015-09-12 15:26:24 +02001380 scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1381
Maxime Ripard1fb34b42015-11-30 16:37:47 +01001382 /* DMA configuration for RX FIFO */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001383 scodec->capture_dma_data.addr = res->start + quirks->reg_adc_rxdata;
Chen-Yu Tsai730e2dd2016-11-03 15:55:46 +08001384 scodec->capture_dma_data.maxburst = 8;
Maxime Ripard1fb34b42015-11-30 16:37:47 +01001385 scodec->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1386
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001387 ret = snd_soc_register_codec(&pdev->dev, quirks->codec,
Emilio López45fb6b62015-09-12 15:26:24 +02001388 &sun4i_codec_dai, 1);
1389 if (ret) {
1390 dev_err(&pdev->dev, "Failed to register our codec\n");
Chen-Yu Tsai9aead152016-11-07 18:06:58 +08001391 goto err_assert_reset;
Emilio López45fb6b62015-09-12 15:26:24 +02001392 }
1393
1394 ret = devm_snd_soc_register_component(&pdev->dev,
1395 &sun4i_codec_component,
1396 &dummy_cpu_dai, 1);
1397 if (ret) {
1398 dev_err(&pdev->dev, "Failed to register our DAI\n");
1399 goto err_unregister_codec;
1400 }
1401
1402 ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
1403 if (ret) {
1404 dev_err(&pdev->dev, "Failed to register against DMAEngine\n");
1405 goto err_unregister_codec;
1406 }
1407
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001408 card = quirks->create_card(&pdev->dev);
Chen-Yu Tsai85915b62016-10-31 14:42:09 +08001409 if (IS_ERR(card)) {
1410 ret = PTR_ERR(card);
Emilio López45fb6b62015-09-12 15:26:24 +02001411 dev_err(&pdev->dev, "Failed to create our card\n");
1412 goto err_unregister_codec;
1413 }
1414
1415 platform_set_drvdata(pdev, card);
1416 snd_soc_card_set_drvdata(card, scodec);
1417
1418 ret = snd_soc_register_card(card);
1419 if (ret) {
1420 dev_err(&pdev->dev, "Failed to register our card\n");
1421 goto err_unregister_codec;
1422 }
1423
1424 return 0;
1425
1426err_unregister_codec:
1427 snd_soc_unregister_codec(&pdev->dev);
Chen-Yu Tsai9aead152016-11-07 18:06:58 +08001428err_assert_reset:
1429 if (scodec->rst)
1430 reset_control_assert(scodec->rst);
Emilio López45fb6b62015-09-12 15:26:24 +02001431err_clk_disable:
1432 clk_disable_unprepare(scodec->clk_apb);
1433 return ret;
1434}
1435
1436static int sun4i_codec_remove(struct platform_device *pdev)
1437{
1438 struct snd_soc_card *card = platform_get_drvdata(pdev);
1439 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
1440
1441 snd_soc_unregister_card(card);
1442 snd_soc_unregister_codec(&pdev->dev);
Chen-Yu Tsai9aead152016-11-07 18:06:58 +08001443 if (scodec->rst)
1444 reset_control_assert(scodec->rst);
Emilio López45fb6b62015-09-12 15:26:24 +02001445 clk_disable_unprepare(scodec->clk_apb);
1446
1447 return 0;
1448}
1449
1450static struct platform_driver sun4i_codec_driver = {
1451 .driver = {
1452 .name = "sun4i-codec",
Emilio López45fb6b62015-09-12 15:26:24 +02001453 .of_match_table = sun4i_codec_of_match,
1454 },
1455 .probe = sun4i_codec_probe,
1456 .remove = sun4i_codec_remove,
1457};
1458module_platform_driver(sun4i_codec_driver);
1459
1460MODULE_DESCRIPTION("Allwinner A10 codec driver");
1461MODULE_AUTHOR("Emilio López <emilio@elopez.com.ar>");
1462MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
1463MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001464MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
Emilio López45fb6b62015-09-12 15:26:24 +02001465MODULE_LICENSE("GPL");