blob: 735115244b17c6adf99990a0ef3ad5e7507d9e36 [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
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800112/*
113 * sun6i specific registers
114 *
115 * sun6i shares the same digital control and FIFO registers as sun4i,
116 * but only the DAC digital controls are at the same offset. The others
117 * have been moved around to accommodate extra analog controls.
118 */
119
120/* Codec DAC digital controls and FIFO registers */
121#define SUN6I_CODEC_ADC_FIFOC (0x10)
122#define SUN6I_CODEC_ADC_FIFOC_EN_AD (28)
123#define SUN6I_CODEC_ADC_FIFOS (0x14)
124#define SUN6I_CODEC_ADC_RXDATA (0x18)
125
126/* Output mixer and gain controls */
127#define SUN6I_CODEC_OM_DACA_CTRL (0x20)
128#define SUN6I_CODEC_OM_DACA_CTRL_DACAREN (31)
129#define SUN6I_CODEC_OM_DACA_CTRL_DACALEN (30)
130#define SUN6I_CODEC_OM_DACA_CTRL_RMIXEN (29)
131#define SUN6I_CODEC_OM_DACA_CTRL_LMIXEN (28)
132#define SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC1 (23)
133#define SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC2 (22)
134#define SUN6I_CODEC_OM_DACA_CTRL_RMIX_PHONE (21)
135#define SUN6I_CODEC_OM_DACA_CTRL_RMIX_PHONEP (20)
136#define SUN6I_CODEC_OM_DACA_CTRL_RMIX_LINEINR (19)
137#define SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACR (18)
138#define SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACL (17)
139#define SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC1 (16)
140#define SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC2 (15)
141#define SUN6I_CODEC_OM_DACA_CTRL_LMIX_PHONE (14)
142#define SUN6I_CODEC_OM_DACA_CTRL_LMIX_PHONEN (13)
143#define SUN6I_CODEC_OM_DACA_CTRL_LMIX_LINEINL (12)
144#define SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACL (11)
145#define SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACR (10)
146#define SUN6I_CODEC_OM_DACA_CTRL_RHPIS (9)
147#define SUN6I_CODEC_OM_DACA_CTRL_LHPIS (8)
148#define SUN6I_CODEC_OM_DACA_CTRL_RHPPAMUTE (7)
149#define SUN6I_CODEC_OM_DACA_CTRL_LHPPAMUTE (6)
150#define SUN6I_CODEC_OM_DACA_CTRL_HPVOL (0)
151#define SUN6I_CODEC_OM_PA_CTRL (0x24)
152#define SUN6I_CODEC_OM_PA_CTRL_HPPAEN (31)
153#define SUN6I_CODEC_OM_PA_CTRL_HPCOM_CTL (29)
154#define SUN6I_CODEC_OM_PA_CTRL_COMPTEN (28)
155#define SUN6I_CODEC_OM_PA_CTRL_MIC1G (15)
156#define SUN6I_CODEC_OM_PA_CTRL_MIC2G (12)
157#define SUN6I_CODEC_OM_PA_CTRL_LINEING (9)
158#define SUN6I_CODEC_OM_PA_CTRL_PHONEG (6)
159#define SUN6I_CODEC_OM_PA_CTRL_PHONEPG (3)
160#define SUN6I_CODEC_OM_PA_CTRL_PHONENG (0)
161
162/* Microphone, line out and phone out controls */
163#define SUN6I_CODEC_MIC_CTRL (0x28)
164#define SUN6I_CODEC_MIC_CTRL_HBIASEN (31)
165#define SUN6I_CODEC_MIC_CTRL_MBIASEN (30)
166#define SUN6I_CODEC_MIC_CTRL_MIC1AMPEN (28)
167#define SUN6I_CODEC_MIC_CTRL_MIC1BOOST (25)
168#define SUN6I_CODEC_MIC_CTRL_MIC2AMPEN (24)
169#define SUN6I_CODEC_MIC_CTRL_MIC2BOOST (21)
170#define SUN6I_CODEC_MIC_CTRL_MIC2SLT (20)
171#define SUN6I_CODEC_MIC_CTRL_LINEOUTLEN (19)
172#define SUN6I_CODEC_MIC_CTRL_LINEOUTREN (18)
173#define SUN6I_CODEC_MIC_CTRL_LINEOUTLSRC (17)
174#define SUN6I_CODEC_MIC_CTRL_LINEOUTRSRC (16)
175#define SUN6I_CODEC_MIC_CTRL_LINEOUTVC (11)
176#define SUN6I_CODEC_MIC_CTRL_PHONEPREG (8)
177
178/* ADC mixer controls */
179#define SUN6I_CODEC_ADC_ACTL (0x2c)
180#define SUN6I_CODEC_ADC_ACTL_ADCREN (31)
181#define SUN6I_CODEC_ADC_ACTL_ADCLEN (30)
182#define SUN6I_CODEC_ADC_ACTL_ADCRG (27)
183#define SUN6I_CODEC_ADC_ACTL_ADCLG (24)
184#define SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC1 (13)
185#define SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC2 (12)
186#define SUN6I_CODEC_ADC_ACTL_RADCMIX_PHONE (11)
187#define SUN6I_CODEC_ADC_ACTL_RADCMIX_PHONEP (10)
188#define SUN6I_CODEC_ADC_ACTL_RADCMIX_LINEINR (9)
189#define SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXR (8)
190#define SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXL (7)
191#define SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC1 (6)
192#define SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC2 (5)
193#define SUN6I_CODEC_ADC_ACTL_LADCMIX_PHONE (4)
194#define SUN6I_CODEC_ADC_ACTL_LADCMIX_PHONEN (3)
195#define SUN6I_CODEC_ADC_ACTL_LADCMIX_LINEINL (2)
196#define SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXL (1)
197#define SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXR (0)
198
199/* Analog performance tuning controls */
200#define SUN6I_CODEC_ADDA_TUNE (0x30)
201
202/* Calibration controls */
203#define SUN6I_CODEC_CALIBRATION (0x34)
204
205/* FIFO counters */
206#define SUN6I_CODEC_DAC_TXCNT (0x40)
207#define SUN6I_CODEC_ADC_RXCNT (0x44)
208
209/* headset jack detection and button support registers */
210#define SUN6I_CODEC_HMIC_CTL (0x50)
211#define SUN6I_CODEC_HMIC_DATA (0x54)
212
213/* TODO sun6i DAP (Digital Audio Processing) bits */
214
Emilio López45fb6b62015-09-12 15:26:24 +0200215struct sun4i_codec {
216 struct device *dev;
217 struct regmap *regmap;
218 struct clk *clk_apb;
219 struct clk *clk_module;
Hans de Goede40592622015-12-11 19:43:57 +0100220 struct gpio_desc *gpio_pa;
Emilio López45fb6b62015-09-12 15:26:24 +0200221
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800222 /* ADC_FIFOC register is at different offset on different SoCs */
223 struct regmap_field *reg_adc_fifoc;
224
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100225 struct snd_dmaengine_dai_dma_data capture_dma_data;
Emilio López45fb6b62015-09-12 15:26:24 +0200226 struct snd_dmaengine_dai_dma_data playback_dma_data;
227};
228
229static void sun4i_codec_start_playback(struct sun4i_codec *scodec)
230{
Emilio López45fb6b62015-09-12 15:26:24 +0200231 /* Flush TX FIFO */
232 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
233 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH),
234 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH));
235
236 /* Enable DAC DRQ */
237 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
238 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN),
239 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN));
240}
241
242static void sun4i_codec_stop_playback(struct sun4i_codec *scodec)
243{
Emilio López45fb6b62015-09-12 15:26:24 +0200244 /* Disable DAC DRQ */
245 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
246 BIT(SUN4I_CODEC_DAC_FIFOC_DAC_DRQ_EN),
247 0);
248}
249
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100250static void sun4i_codec_start_capture(struct sun4i_codec *scodec)
251{
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100252 /* Enable ADC DRQ */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800253 regmap_field_update_bits(scodec->reg_adc_fifoc,
254 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN),
255 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN));
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100256}
257
258static void sun4i_codec_stop_capture(struct sun4i_codec *scodec)
259{
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100260 /* Disable ADC DRQ */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800261 regmap_field_update_bits(scodec->reg_adc_fifoc,
262 BIT(SUN4I_CODEC_ADC_FIFOC_ADC_DRQ_EN), 0);
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100263}
264
Emilio López45fb6b62015-09-12 15:26:24 +0200265static int sun4i_codec_trigger(struct snd_pcm_substream *substream, int cmd,
266 struct snd_soc_dai *dai)
267{
268 struct snd_soc_pcm_runtime *rtd = substream->private_data;
269 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
270
Emilio López45fb6b62015-09-12 15:26:24 +0200271 switch (cmd) {
272 case SNDRV_PCM_TRIGGER_START:
273 case SNDRV_PCM_TRIGGER_RESUME:
274 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100275 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
276 sun4i_codec_start_playback(scodec);
277 else
278 sun4i_codec_start_capture(scodec);
Emilio López45fb6b62015-09-12 15:26:24 +0200279 break;
280
281 case SNDRV_PCM_TRIGGER_STOP:
282 case SNDRV_PCM_TRIGGER_SUSPEND:
283 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100284 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
285 sun4i_codec_stop_playback(scodec);
286 else
287 sun4i_codec_stop_capture(scodec);
Emilio López45fb6b62015-09-12 15:26:24 +0200288 break;
289
290 default:
291 return -EINVAL;
292 }
293
294 return 0;
295}
296
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100297static int sun4i_codec_prepare_capture(struct snd_pcm_substream *substream,
298 struct snd_soc_dai *dai)
299{
300 struct snd_soc_pcm_runtime *rtd = substream->private_data;
301 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
302
303
304 /* Flush RX FIFO */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800305 regmap_field_update_bits(scodec->reg_adc_fifoc,
306 BIT(SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH),
307 BIT(SUN4I_CODEC_ADC_FIFOC_FIFO_FLUSH));
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100308
309
310 /* Set RX FIFO trigger level */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800311 regmap_field_update_bits(scodec->reg_adc_fifoc,
312 0xf << SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL,
313 0x7 << SUN4I_CODEC_ADC_FIFOC_RX_TRIG_LEVEL);
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100314
315 /*
316 * FIXME: Undocumented in the datasheet, but
317 * Allwinner's code mentions that it is related
318 * related to microphone gain
319 */
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800320 if (of_device_is_compatible(scodec->dev->of_node,
321 "allwinner,sun4i-a10-codec") ||
322 of_device_is_compatible(scodec->dev->of_node,
323 "allwinner,sun7i-a20-codec")) {
324 regmap_update_bits(scodec->regmap, SUN4I_CODEC_ADC_ACTL,
325 0x3 << 25,
326 0x1 << 25);
327 }
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100328
329 if (of_device_is_compatible(scodec->dev->of_node,
330 "allwinner,sun7i-a20-codec"))
331 /* FIXME: Undocumented bits */
332 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_TUNE,
333 0x3 << 8,
334 0x1 << 8);
335
336 /* Fill most significant bits with valid data MSB */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800337 regmap_field_update_bits(scodec->reg_adc_fifoc,
338 BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE),
339 BIT(SUN4I_CODEC_ADC_FIFOC_RX_FIFO_MODE));
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100340
341 return 0;
342}
343
344static int sun4i_codec_prepare_playback(struct snd_pcm_substream *substream,
345 struct snd_soc_dai *dai)
Emilio López45fb6b62015-09-12 15:26:24 +0200346{
347 struct snd_soc_pcm_runtime *rtd = substream->private_data;
348 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
349 u32 val;
350
Emilio López45fb6b62015-09-12 15:26:24 +0200351 /* Flush the TX FIFO */
352 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
353 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH),
354 BIT(SUN4I_CODEC_DAC_FIFOC_FIFO_FLUSH));
355
356 /* Set TX FIFO Empty Trigger Level */
357 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
358 0x3f << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL,
359 0xf << SUN4I_CODEC_DAC_FIFOC_TX_TRIG_LEVEL);
360
361 if (substream->runtime->rate > 32000)
362 /* Use 64 bits FIR filter */
363 val = 0;
364 else
365 /* Use 32 bits FIR filter */
366 val = BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION);
367
368 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
369 BIT(SUN4I_CODEC_DAC_FIFOC_FIR_VERSION),
370 val);
371
372 /* Send zeros when we have an underrun */
373 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
374 BIT(SUN4I_CODEC_DAC_FIFOC_SEND_LASAT),
375 0);
376
377 return 0;
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100378};
379
380static int sun4i_codec_prepare(struct snd_pcm_substream *substream,
381 struct snd_soc_dai *dai)
382{
383 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
384 return sun4i_codec_prepare_playback(substream, dai);
385
386 return sun4i_codec_prepare_capture(substream, dai);
Emilio López45fb6b62015-09-12 15:26:24 +0200387}
388
389static unsigned long sun4i_codec_get_mod_freq(struct snd_pcm_hw_params *params)
390{
391 unsigned int rate = params_rate(params);
392
393 switch (rate) {
394 case 176400:
395 case 88200:
396 case 44100:
397 case 33075:
398 case 22050:
399 case 14700:
400 case 11025:
401 case 7350:
402 return 22579200;
403
404 case 192000:
405 case 96000:
406 case 48000:
407 case 32000:
408 case 24000:
409 case 16000:
410 case 12000:
411 case 8000:
412 return 24576000;
413
414 default:
415 return 0;
416 }
417}
418
419static int sun4i_codec_get_hw_rate(struct snd_pcm_hw_params *params)
420{
421 unsigned int rate = params_rate(params);
422
423 switch (rate) {
424 case 192000:
425 case 176400:
426 return 6;
427
428 case 96000:
429 case 88200:
430 return 7;
431
432 case 48000:
433 case 44100:
434 return 0;
435
436 case 32000:
437 case 33075:
438 return 1;
439
440 case 24000:
441 case 22050:
442 return 2;
443
444 case 16000:
445 case 14700:
446 return 3;
447
448 case 12000:
449 case 11025:
450 return 4;
451
452 case 8000:
453 case 7350:
454 return 5;
455
456 default:
457 return -EINVAL;
458 }
459}
460
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100461static int sun4i_codec_hw_params_capture(struct sun4i_codec *scodec,
462 struct snd_pcm_hw_params *params,
463 unsigned int hwrate)
Emilio López45fb6b62015-09-12 15:26:24 +0200464{
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100465 /* Set ADC sample rate */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800466 regmap_field_update_bits(scodec->reg_adc_fifoc,
467 7 << SUN4I_CODEC_ADC_FIFOC_ADC_FS,
468 hwrate << SUN4I_CODEC_ADC_FIFOC_ADC_FS);
Emilio López45fb6b62015-09-12 15:26:24 +0200469
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100470 /* Set the number of channels we want to use */
471 if (params_channels(params) == 1)
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800472 regmap_field_update_bits(scodec->reg_adc_fifoc,
473 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN),
474 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN));
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100475 else
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +0800476 regmap_field_update_bits(scodec->reg_adc_fifoc,
477 BIT(SUN4I_CODEC_ADC_FIFOC_MONO_EN),
478 0);
Emilio López45fb6b62015-09-12 15:26:24 +0200479
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100480 return 0;
481}
Emilio López45fb6b62015-09-12 15:26:24 +0200482
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100483static int sun4i_codec_hw_params_playback(struct sun4i_codec *scodec,
484 struct snd_pcm_hw_params *params,
485 unsigned int hwrate)
486{
487 u32 val;
Emilio López45fb6b62015-09-12 15:26:24 +0200488
489 /* Set DAC sample rate */
490 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
491 7 << SUN4I_CODEC_DAC_FIFOC_DAC_FS,
492 hwrate << SUN4I_CODEC_DAC_FIFOC_DAC_FS);
493
494 /* Set the number of channels we want to use */
495 if (params_channels(params) == 1)
496 val = BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN);
497 else
498 val = 0;
499
500 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
501 BIT(SUN4I_CODEC_DAC_FIFOC_MONO_EN),
502 val);
503
504 /* Set the number of sample bits to either 16 or 24 bits */
505 if (hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min == 32) {
506 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
507 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS),
508 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS));
509
510 /* Set TX FIFO mode to padding the LSBs with 0 */
511 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
512 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE),
513 0);
514
515 scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
516 } else {
517 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
518 BIT(SUN4I_CODEC_DAC_FIFOC_TX_SAMPLE_BITS),
519 0);
520
521 /* Set TX FIFO mode to repeat the MSB */
522 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
523 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE),
524 BIT(SUN4I_CODEC_DAC_FIFOC_TX_FIFO_MODE));
525
526 scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
527 }
528
529 return 0;
530}
531
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100532static int sun4i_codec_hw_params(struct snd_pcm_substream *substream,
533 struct snd_pcm_hw_params *params,
534 struct snd_soc_dai *dai)
535{
536 struct snd_soc_pcm_runtime *rtd = substream->private_data;
537 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
538 unsigned long clk_freq;
Maxime Ripard8400ddf2015-12-01 12:06:47 +0100539 int ret, hwrate;
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100540
541 clk_freq = sun4i_codec_get_mod_freq(params);
542 if (!clk_freq)
543 return -EINVAL;
544
Maxime Ripard8400ddf2015-12-01 12:06:47 +0100545 ret = clk_set_rate(scodec->clk_module, clk_freq);
546 if (ret)
547 return ret;
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100548
549 hwrate = sun4i_codec_get_hw_rate(params);
550 if (hwrate < 0)
551 return hwrate;
552
553 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
554 return sun4i_codec_hw_params_playback(scodec, params,
555 hwrate);
556
557 return sun4i_codec_hw_params_capture(scodec, params,
558 hwrate);
559}
560
Emilio López45fb6b62015-09-12 15:26:24 +0200561static int sun4i_codec_startup(struct snd_pcm_substream *substream,
562 struct snd_soc_dai *dai)
563{
564 struct snd_soc_pcm_runtime *rtd = substream->private_data;
565 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
566
567 /*
568 * Stop issuing DRQ when we have room for less than 16 samples
569 * in our TX FIFO
570 */
571 regmap_update_bits(scodec->regmap, SUN4I_CODEC_DAC_FIFOC,
572 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT,
573 3 << SUN4I_CODEC_DAC_FIFOC_DRQ_CLR_CNT);
574
575 return clk_prepare_enable(scodec->clk_module);
576}
577
578static void sun4i_codec_shutdown(struct snd_pcm_substream *substream,
579 struct snd_soc_dai *dai)
580{
581 struct snd_soc_pcm_runtime *rtd = substream->private_data;
582 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(rtd->card);
583
584 clk_disable_unprepare(scodec->clk_module);
585}
586
587static const struct snd_soc_dai_ops sun4i_codec_dai_ops = {
588 .startup = sun4i_codec_startup,
589 .shutdown = sun4i_codec_shutdown,
590 .trigger = sun4i_codec_trigger,
591 .hw_params = sun4i_codec_hw_params,
592 .prepare = sun4i_codec_prepare,
593};
594
595static struct snd_soc_dai_driver sun4i_codec_dai = {
596 .name = "Codec",
597 .ops = &sun4i_codec_dai_ops,
598 .playback = {
599 .stream_name = "Codec Playback",
600 .channels_min = 1,
601 .channels_max = 2,
602 .rate_min = 8000,
603 .rate_max = 192000,
604 .rates = SNDRV_PCM_RATE_8000_48000 |
605 SNDRV_PCM_RATE_96000 |
Maxime Riparddebb9722015-09-29 21:43:18 +0200606 SNDRV_PCM_RATE_192000,
Emilio López45fb6b62015-09-12 15:26:24 +0200607 .formats = SNDRV_PCM_FMTBIT_S16_LE |
608 SNDRV_PCM_FMTBIT_S32_LE,
609 .sig_bits = 24,
610 },
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100611 .capture = {
612 .stream_name = "Codec Capture",
613 .channels_min = 1,
614 .channels_max = 2,
615 .rate_min = 8000,
616 .rate_max = 192000,
617 .rates = SNDRV_PCM_RATE_8000_48000 |
618 SNDRV_PCM_RATE_96000 |
619 SNDRV_PCM_RATE_192000 |
620 SNDRV_PCM_RATE_KNOT,
621 .formats = SNDRV_PCM_FMTBIT_S16_LE |
622 SNDRV_PCM_FMTBIT_S32_LE,
623 .sig_bits = 24,
624 },
Emilio López45fb6b62015-09-12 15:26:24 +0200625};
626
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800627/*** sun4i Codec ***/
Emilio López45fb6b62015-09-12 15:26:24 +0200628static const struct snd_kcontrol_new sun4i_codec_pa_mute =
629 SOC_DAPM_SINGLE("Switch", SUN4I_CODEC_DAC_ACTL,
630 SUN4I_CODEC_DAC_ACTL_PA_MUTE, 1, 0);
631
632static DECLARE_TLV_DB_SCALE(sun4i_codec_pa_volume_scale, -6300, 100, 1);
633
Danny Milosavljevicc9e902f2016-09-24 22:05:01 +0200634static const struct snd_kcontrol_new sun4i_codec_controls[] = {
Adam Sampson474d1472015-10-27 21:00:45 +0000635 SOC_SINGLE_TLV("Power Amplifier Volume", SUN4I_CODEC_DAC_ACTL,
Emilio López45fb6b62015-09-12 15:26:24 +0200636 SUN4I_CODEC_DAC_ACTL_PA_VOL, 0x3F, 0,
637 sun4i_codec_pa_volume_scale),
638};
639
640static const struct snd_kcontrol_new sun4i_codec_left_mixer_controls[] = {
641 SOC_DAPM_SINGLE("Left DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
642 SUN4I_CODEC_DAC_ACTL_LDACLMIXS, 1, 0),
643};
644
645static const struct snd_kcontrol_new sun4i_codec_right_mixer_controls[] = {
646 SOC_DAPM_SINGLE("Right DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
647 SUN4I_CODEC_DAC_ACTL_RDACRMIXS, 1, 0),
648 SOC_DAPM_SINGLE("Left DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
649 SUN4I_CODEC_DAC_ACTL_LDACRMIXS, 1, 0),
650};
651
652static const struct snd_kcontrol_new sun4i_codec_pa_mixer_controls[] = {
653 SOC_DAPM_SINGLE("DAC Playback Switch", SUN4I_CODEC_DAC_ACTL,
654 SUN4I_CODEC_DAC_ACTL_DACPAS, 1, 0),
655 SOC_DAPM_SINGLE("Mixer Playback Switch", SUN4I_CODEC_DAC_ACTL,
656 SUN4I_CODEC_DAC_ACTL_MIXPAS, 1, 0),
657};
658
Hans de Goedee6415b42015-12-11 19:43:56 +0100659static const struct snd_soc_dapm_widget sun4i_codec_codec_dapm_widgets[] = {
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100660 /* Digital parts of the ADCs */
661 SND_SOC_DAPM_SUPPLY("ADC", SUN4I_CODEC_ADC_FIFOC,
662 SUN4I_CODEC_ADC_FIFOC_EN_AD, 0,
663 NULL, 0),
664
Emilio López45fb6b62015-09-12 15:26:24 +0200665 /* Digital parts of the DACs */
666 SND_SOC_DAPM_SUPPLY("DAC", SUN4I_CODEC_DAC_DPC,
667 SUN4I_CODEC_DAC_DPC_EN_DA, 0,
668 NULL, 0),
669
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100670 /* Analog parts of the ADCs */
671 SND_SOC_DAPM_ADC("Left ADC", "Codec Capture", SUN4I_CODEC_ADC_ACTL,
672 SUN4I_CODEC_ADC_ACTL_ADC_L_EN, 0),
673 SND_SOC_DAPM_ADC("Right ADC", "Codec Capture", SUN4I_CODEC_ADC_ACTL,
674 SUN4I_CODEC_ADC_ACTL_ADC_R_EN, 0),
675
Emilio López45fb6b62015-09-12 15:26:24 +0200676 /* Analog parts of the DACs */
677 SND_SOC_DAPM_DAC("Left DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL,
678 SUN4I_CODEC_DAC_ACTL_DACAENL, 0),
679 SND_SOC_DAPM_DAC("Right DAC", "Codec Playback", SUN4I_CODEC_DAC_ACTL,
680 SUN4I_CODEC_DAC_ACTL_DACAENR, 0),
681
682 /* Mixers */
683 SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
684 sun4i_codec_left_mixer_controls,
685 ARRAY_SIZE(sun4i_codec_left_mixer_controls)),
686 SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
687 sun4i_codec_right_mixer_controls,
688 ARRAY_SIZE(sun4i_codec_right_mixer_controls)),
689
690 /* Global Mixer Enable */
691 SND_SOC_DAPM_SUPPLY("Mixer Enable", SUN4I_CODEC_DAC_ACTL,
692 SUN4I_CODEC_DAC_ACTL_MIXEN, 0, NULL, 0),
693
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100694 /* VMIC */
695 SND_SOC_DAPM_SUPPLY("VMIC", SUN4I_CODEC_ADC_ACTL,
696 SUN4I_CODEC_ADC_ACTL_VMICEN, 0, NULL, 0),
697
698 /* Mic Pre-Amplifiers */
699 SND_SOC_DAPM_PGA("MIC1 Pre-Amplifier", SUN4I_CODEC_ADC_ACTL,
700 SUN4I_CODEC_ADC_ACTL_PREG1EN, 0, NULL, 0),
701
Adam Sampson474d1472015-10-27 21:00:45 +0000702 /* Power Amplifier */
703 SND_SOC_DAPM_MIXER("Power Amplifier", SUN4I_CODEC_ADC_ACTL,
Emilio López45fb6b62015-09-12 15:26:24 +0200704 SUN4I_CODEC_ADC_ACTL_PA_EN, 0,
705 sun4i_codec_pa_mixer_controls,
706 ARRAY_SIZE(sun4i_codec_pa_mixer_controls)),
Adam Sampson474d1472015-10-27 21:00:45 +0000707 SND_SOC_DAPM_SWITCH("Power Amplifier Mute", SND_SOC_NOPM, 0, 0,
Emilio López45fb6b62015-09-12 15:26:24 +0200708 &sun4i_codec_pa_mute),
709
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100710 SND_SOC_DAPM_INPUT("Mic1"),
711
Emilio López45fb6b62015-09-12 15:26:24 +0200712 SND_SOC_DAPM_OUTPUT("HP Right"),
713 SND_SOC_DAPM_OUTPUT("HP Left"),
714};
715
Hans de Goedee6415b42015-12-11 19:43:56 +0100716static const struct snd_soc_dapm_route sun4i_codec_codec_dapm_routes[] = {
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100717 /* Left ADC / DAC Routes */
718 { "Left ADC", NULL, "ADC" },
Emilio López45fb6b62015-09-12 15:26:24 +0200719 { "Left DAC", NULL, "DAC" },
720
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100721 /* Right ADC / DAC Routes */
722 { "Right ADC", NULL, "ADC" },
Emilio López45fb6b62015-09-12 15:26:24 +0200723 { "Right DAC", NULL, "DAC" },
724
725 /* Right Mixer Routes */
726 { "Right Mixer", NULL, "Mixer Enable" },
727 { "Right Mixer", "Left DAC Playback Switch", "Left DAC" },
728 { "Right Mixer", "Right DAC Playback Switch", "Right DAC" },
729
730 /* Left Mixer Routes */
731 { "Left Mixer", NULL, "Mixer Enable" },
732 { "Left Mixer", "Left DAC Playback Switch", "Left DAC" },
733
Adam Sampson474d1472015-10-27 21:00:45 +0000734 /* Power Amplifier Routes */
735 { "Power Amplifier", "Mixer Playback Switch", "Left Mixer" },
736 { "Power Amplifier", "Mixer Playback Switch", "Right Mixer" },
737 { "Power Amplifier", "DAC Playback Switch", "Left DAC" },
738 { "Power Amplifier", "DAC Playback Switch", "Right DAC" },
Emilio López45fb6b62015-09-12 15:26:24 +0200739
Adam Sampson474d1472015-10-27 21:00:45 +0000740 /* Headphone Output Routes */
741 { "Power Amplifier Mute", "Switch", "Power Amplifier" },
742 { "HP Right", NULL, "Power Amplifier Mute" },
743 { "HP Left", NULL, "Power Amplifier Mute" },
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100744
745 /* Mic1 Routes */
746 { "Left ADC", NULL, "MIC1 Pre-Amplifier" },
747 { "Right ADC", NULL, "MIC1 Pre-Amplifier" },
748 { "MIC1 Pre-Amplifier", NULL, "Mic1"},
749 { "Mic1", NULL, "VMIC" },
Emilio López45fb6b62015-09-12 15:26:24 +0200750};
751
752static struct snd_soc_codec_driver sun4i_codec_codec = {
Kuninori Morimoto180f58f2016-08-08 08:46:41 +0000753 .component_driver = {
Danny Milosavljevicc9e902f2016-09-24 22:05:01 +0200754 .controls = sun4i_codec_controls,
755 .num_controls = ARRAY_SIZE(sun4i_codec_controls),
Kuninori Morimoto180f58f2016-08-08 08:46:41 +0000756 .dapm_widgets = sun4i_codec_codec_dapm_widgets,
757 .num_dapm_widgets = ARRAY_SIZE(sun4i_codec_codec_dapm_widgets),
758 .dapm_routes = sun4i_codec_codec_dapm_routes,
759 .num_dapm_routes = ARRAY_SIZE(sun4i_codec_codec_dapm_routes),
760 },
Emilio López45fb6b62015-09-12 15:26:24 +0200761};
762
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800763/*** sun6i Codec ***/
764
765/* mixer controls */
766static const struct snd_kcontrol_new sun6i_codec_mixer_controls[] = {
767 SOC_DAPM_DOUBLE("DAC Playback Switch",
768 SUN6I_CODEC_OM_DACA_CTRL,
769 SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACL,
770 SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACR, 1, 0),
771 SOC_DAPM_DOUBLE("DAC Reversed Playback Switch",
772 SUN6I_CODEC_OM_DACA_CTRL,
773 SUN6I_CODEC_OM_DACA_CTRL_LMIX_DACR,
774 SUN6I_CODEC_OM_DACA_CTRL_RMIX_DACL, 1, 0),
Chen-Yu Tsaidff50512016-11-03 15:55:49 +0800775 SOC_DAPM_DOUBLE("Line In Playback Switch",
776 SUN6I_CODEC_OM_DACA_CTRL,
777 SUN6I_CODEC_OM_DACA_CTRL_LMIX_LINEINL,
778 SUN6I_CODEC_OM_DACA_CTRL_RMIX_LINEINR, 1, 0),
Chen-Yu Tsaiecd5cdb2016-11-03 15:55:51 +0800779 SOC_DAPM_DOUBLE("Mic1 Playback Switch",
780 SUN6I_CODEC_OM_DACA_CTRL,
781 SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC1,
782 SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC1, 1, 0),
783 SOC_DAPM_DOUBLE("Mic2 Playback Switch",
784 SUN6I_CODEC_OM_DACA_CTRL,
785 SUN6I_CODEC_OM_DACA_CTRL_LMIX_MIC2,
786 SUN6I_CODEC_OM_DACA_CTRL_RMIX_MIC2, 1, 0),
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800787};
788
Chen-Yu Tsai24c99f82016-11-07 18:06:59 +0800789/* ADC mixer controls */
790static const struct snd_kcontrol_new sun6i_codec_adc_mixer_controls[] = {
791 SOC_DAPM_DOUBLE("Mixer Capture Switch",
792 SUN6I_CODEC_ADC_ACTL,
793 SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXL,
794 SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXR, 1, 0),
795 SOC_DAPM_DOUBLE("Mixer Reversed Capture Switch",
796 SUN6I_CODEC_ADC_ACTL,
797 SUN6I_CODEC_ADC_ACTL_LADCMIX_OMIXR,
798 SUN6I_CODEC_ADC_ACTL_RADCMIX_OMIXL, 1, 0),
799 SOC_DAPM_DOUBLE("Line In Capture Switch",
800 SUN6I_CODEC_ADC_ACTL,
801 SUN6I_CODEC_ADC_ACTL_LADCMIX_LINEINL,
802 SUN6I_CODEC_ADC_ACTL_RADCMIX_LINEINR, 1, 0),
803 SOC_DAPM_DOUBLE("Mic1 Capture Switch",
804 SUN6I_CODEC_ADC_ACTL,
805 SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC1,
806 SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC1, 1, 0),
807 SOC_DAPM_DOUBLE("Mic2 Capture Switch",
808 SUN6I_CODEC_ADC_ACTL,
809 SUN6I_CODEC_ADC_ACTL_LADCMIX_MIC2,
810 SUN6I_CODEC_ADC_ACTL_RADCMIX_MIC2, 1, 0),
811};
812
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800813/* headphone controls */
814static const char * const sun6i_codec_hp_src_enum_text[] = {
815 "DAC", "Mixer",
816};
817
818static SOC_ENUM_DOUBLE_DECL(sun6i_codec_hp_src_enum,
819 SUN6I_CODEC_OM_DACA_CTRL,
820 SUN6I_CODEC_OM_DACA_CTRL_LHPIS,
821 SUN6I_CODEC_OM_DACA_CTRL_RHPIS,
822 sun6i_codec_hp_src_enum_text);
823
824static const struct snd_kcontrol_new sun6i_codec_hp_src[] = {
825 SOC_DAPM_ENUM("Headphone Source Playback Route",
826 sun6i_codec_hp_src_enum),
827};
828
Chen-Yu Tsaiecd5cdb2016-11-03 15:55:51 +0800829/* microphone controls */
830static const char * const sun6i_codec_mic2_src_enum_text[] = {
831 "Mic2", "Mic3",
832};
833
834static SOC_ENUM_SINGLE_DECL(sun6i_codec_mic2_src_enum,
835 SUN6I_CODEC_MIC_CTRL,
836 SUN6I_CODEC_MIC_CTRL_MIC2SLT,
837 sun6i_codec_mic2_src_enum_text);
838
839static const struct snd_kcontrol_new sun6i_codec_mic2_src[] = {
840 SOC_DAPM_ENUM("Mic2 Amplifier Source Route",
841 sun6i_codec_mic2_src_enum),
842};
843
Chen-Yu Tsai0f909f92016-11-03 15:55:50 +0800844/* line out controls */
845static const char * const sun6i_codec_lineout_src_enum_text[] = {
846 "Stereo", "Mono Differential",
847};
848
849static SOC_ENUM_DOUBLE_DECL(sun6i_codec_lineout_src_enum,
850 SUN6I_CODEC_MIC_CTRL,
851 SUN6I_CODEC_MIC_CTRL_LINEOUTLSRC,
852 SUN6I_CODEC_MIC_CTRL_LINEOUTRSRC,
853 sun6i_codec_lineout_src_enum_text);
854
855static const struct snd_kcontrol_new sun6i_codec_lineout_src[] = {
856 SOC_DAPM_ENUM("Line Out Source Playback Route",
857 sun6i_codec_lineout_src_enum),
858};
859
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800860/* volume / mute controls */
861static const DECLARE_TLV_DB_SCALE(sun6i_codec_dvol_scale, -7308, 116, 0);
862static const DECLARE_TLV_DB_SCALE(sun6i_codec_hp_vol_scale, -6300, 100, 1);
Chen-Yu Tsaidff50512016-11-03 15:55:49 +0800863static const DECLARE_TLV_DB_SCALE(sun6i_codec_out_mixer_pregain_scale,
864 -450, 150, 0);
Chen-Yu Tsai0f909f92016-11-03 15:55:50 +0800865static const DECLARE_TLV_DB_RANGE(sun6i_codec_lineout_vol_scale,
866 0, 1, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
867 2, 31, TLV_DB_SCALE_ITEM(-4350, 150, 0),
868);
Chen-Yu Tsaiecd5cdb2016-11-03 15:55:51 +0800869static const DECLARE_TLV_DB_RANGE(sun6i_codec_mic_gain_scale,
870 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
871 1, 7, TLV_DB_SCALE_ITEM(2400, 300, 0),
872);
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800873
874static const struct snd_kcontrol_new sun6i_codec_codec_widgets[] = {
875 SOC_SINGLE_TLV("DAC Playback Volume", SUN4I_CODEC_DAC_DPC,
876 SUN4I_CODEC_DAC_DPC_DVOL, 0x3f, 1,
877 sun6i_codec_dvol_scale),
878 SOC_SINGLE_TLV("Headphone Playback Volume",
879 SUN6I_CODEC_OM_DACA_CTRL,
880 SUN6I_CODEC_OM_DACA_CTRL_HPVOL, 0x3f, 0,
881 sun6i_codec_hp_vol_scale),
Chen-Yu Tsai0f909f92016-11-03 15:55:50 +0800882 SOC_SINGLE_TLV("Line Out Playback Volume",
883 SUN6I_CODEC_MIC_CTRL,
884 SUN6I_CODEC_MIC_CTRL_LINEOUTVC, 0x1f, 0,
885 sun6i_codec_lineout_vol_scale),
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800886 SOC_DOUBLE("Headphone Playback Switch",
887 SUN6I_CODEC_OM_DACA_CTRL,
888 SUN6I_CODEC_OM_DACA_CTRL_LHPPAMUTE,
889 SUN6I_CODEC_OM_DACA_CTRL_RHPPAMUTE, 1, 0),
Chen-Yu Tsai0f909f92016-11-03 15:55:50 +0800890 SOC_DOUBLE("Line Out Playback Switch",
891 SUN6I_CODEC_MIC_CTRL,
892 SUN6I_CODEC_MIC_CTRL_LINEOUTLEN,
893 SUN6I_CODEC_MIC_CTRL_LINEOUTREN, 1, 0),
Chen-Yu Tsaidff50512016-11-03 15:55:49 +0800894 /* Mixer pre-gains */
895 SOC_SINGLE_TLV("Line In Playback Volume",
896 SUN6I_CODEC_OM_PA_CTRL, SUN6I_CODEC_OM_PA_CTRL_LINEING,
897 0x7, 0, sun6i_codec_out_mixer_pregain_scale),
Chen-Yu Tsaiecd5cdb2016-11-03 15:55:51 +0800898 SOC_SINGLE_TLV("Mic1 Playback Volume",
899 SUN6I_CODEC_OM_PA_CTRL, SUN6I_CODEC_OM_PA_CTRL_MIC1G,
900 0x7, 0, sun6i_codec_out_mixer_pregain_scale),
901 SOC_SINGLE_TLV("Mic2 Playback Volume",
902 SUN6I_CODEC_OM_PA_CTRL, SUN6I_CODEC_OM_PA_CTRL_MIC2G,
903 0x7, 0, sun6i_codec_out_mixer_pregain_scale),
904
905 /* Microphone Amp boost gains */
906 SOC_SINGLE_TLV("Mic1 Boost Volume", SUN6I_CODEC_MIC_CTRL,
907 SUN6I_CODEC_MIC_CTRL_MIC1BOOST, 0x7, 0,
908 sun6i_codec_mic_gain_scale),
909 SOC_SINGLE_TLV("Mic2 Boost Volume", SUN6I_CODEC_MIC_CTRL,
910 SUN6I_CODEC_MIC_CTRL_MIC2BOOST, 0x7, 0,
911 sun6i_codec_mic_gain_scale),
Chen-Yu Tsai24c99f82016-11-07 18:06:59 +0800912 SOC_DOUBLE_TLV("ADC Capture Volume",
913 SUN6I_CODEC_ADC_ACTL, SUN6I_CODEC_ADC_ACTL_ADCLG,
914 SUN6I_CODEC_ADC_ACTL_ADCRG, 0x7, 0,
915 sun6i_codec_out_mixer_pregain_scale),
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800916};
917
918static const struct snd_soc_dapm_widget sun6i_codec_codec_dapm_widgets[] = {
Chen-Yu Tsaiecd5cdb2016-11-03 15:55:51 +0800919 /* Microphone inputs */
920 SND_SOC_DAPM_INPUT("MIC1"),
921 SND_SOC_DAPM_INPUT("MIC2"),
922 SND_SOC_DAPM_INPUT("MIC3"),
923
924 /* Microphone Bias */
925 SND_SOC_DAPM_SUPPLY("HBIAS", SUN6I_CODEC_MIC_CTRL,
926 SUN6I_CODEC_MIC_CTRL_HBIASEN, 0, NULL, 0),
927 SND_SOC_DAPM_SUPPLY("MBIAS", SUN6I_CODEC_MIC_CTRL,
928 SUN6I_CODEC_MIC_CTRL_MBIASEN, 0, NULL, 0),
929
930 /* Mic input path */
931 SND_SOC_DAPM_MUX("Mic2 Amplifier Source Route",
932 SND_SOC_NOPM, 0, 0, sun6i_codec_mic2_src),
933 SND_SOC_DAPM_PGA("Mic1 Amplifier", SUN6I_CODEC_MIC_CTRL,
934 SUN6I_CODEC_MIC_CTRL_MIC1AMPEN, 0, NULL, 0),
935 SND_SOC_DAPM_PGA("Mic2 Amplifier", SUN6I_CODEC_MIC_CTRL,
936 SUN6I_CODEC_MIC_CTRL_MIC2AMPEN, 0, NULL, 0),
937
Chen-Yu Tsaidff50512016-11-03 15:55:49 +0800938 /* Line In */
939 SND_SOC_DAPM_INPUT("LINEIN"),
940
Chen-Yu Tsai24c99f82016-11-07 18:06:59 +0800941 /* Digital parts of the ADCs */
942 SND_SOC_DAPM_SUPPLY("ADC Enable", SUN6I_CODEC_ADC_FIFOC,
943 SUN6I_CODEC_ADC_FIFOC_EN_AD, 0,
944 NULL, 0),
945
946 /* Analog parts of the ADCs */
947 SND_SOC_DAPM_ADC("Left ADC", "Codec Capture", SUN6I_CODEC_ADC_ACTL,
948 SUN6I_CODEC_ADC_ACTL_ADCLEN, 0),
949 SND_SOC_DAPM_ADC("Right ADC", "Codec Capture", SUN6I_CODEC_ADC_ACTL,
950 SUN6I_CODEC_ADC_ACTL_ADCREN, 0),
951
952 /* ADC Mixers */
953 SOC_MIXER_ARRAY("Left ADC Mixer", SND_SOC_NOPM, 0, 0,
954 sun6i_codec_adc_mixer_controls),
955 SOC_MIXER_ARRAY("Right ADC Mixer", SND_SOC_NOPM, 0, 0,
956 sun6i_codec_adc_mixer_controls),
957
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800958 /* Digital parts of the DACs */
959 SND_SOC_DAPM_SUPPLY("DAC Enable", SUN4I_CODEC_DAC_DPC,
960 SUN4I_CODEC_DAC_DPC_EN_DA, 0,
961 NULL, 0),
962
963 /* Analog parts of the DACs */
964 SND_SOC_DAPM_DAC("Left DAC", "Codec Playback",
965 SUN6I_CODEC_OM_DACA_CTRL,
966 SUN6I_CODEC_OM_DACA_CTRL_DACALEN, 0),
967 SND_SOC_DAPM_DAC("Right DAC", "Codec Playback",
968 SUN6I_CODEC_OM_DACA_CTRL,
969 SUN6I_CODEC_OM_DACA_CTRL_DACAREN, 0),
970
971 /* Mixers */
972 SOC_MIXER_ARRAY("Left Mixer", SUN6I_CODEC_OM_DACA_CTRL,
973 SUN6I_CODEC_OM_DACA_CTRL_LMIXEN, 0,
974 sun6i_codec_mixer_controls),
975 SOC_MIXER_ARRAY("Right Mixer", SUN6I_CODEC_OM_DACA_CTRL,
976 SUN6I_CODEC_OM_DACA_CTRL_RMIXEN, 0,
977 sun6i_codec_mixer_controls),
978
979 /* Headphone output path */
980 SND_SOC_DAPM_MUX("Headphone Source Playback Route",
981 SND_SOC_NOPM, 0, 0, sun6i_codec_hp_src),
982 SND_SOC_DAPM_OUT_DRV("Headphone Amp", SUN6I_CODEC_OM_PA_CTRL,
983 SUN6I_CODEC_OM_PA_CTRL_HPPAEN, 0, NULL, 0),
984 SND_SOC_DAPM_SUPPLY("HPCOM Protection", SUN6I_CODEC_OM_PA_CTRL,
985 SUN6I_CODEC_OM_PA_CTRL_COMPTEN, 0, NULL, 0),
986 SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPCOM", SUN6I_CODEC_OM_PA_CTRL,
987 SUN6I_CODEC_OM_PA_CTRL_HPCOM_CTL, 0x3, 0x3, 0),
988 SND_SOC_DAPM_OUTPUT("HP"),
Chen-Yu Tsai0f909f92016-11-03 15:55:50 +0800989
990 /* Line Out path */
991 SND_SOC_DAPM_MUX("Line Out Source Playback Route",
992 SND_SOC_NOPM, 0, 0, sun6i_codec_lineout_src),
993 SND_SOC_DAPM_OUTPUT("LINEOUT"),
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800994};
995
996static const struct snd_soc_dapm_route sun6i_codec_codec_dapm_routes[] = {
997 /* DAC Routes */
998 { "Left DAC", NULL, "DAC Enable" },
999 { "Right DAC", NULL, "DAC Enable" },
1000
Chen-Yu Tsaiecd5cdb2016-11-03 15:55:51 +08001001 /* Microphone Routes */
1002 { "Mic1 Amplifier", NULL, "MIC1"},
1003 { "Mic2 Amplifier Source Route", "Mic2", "MIC2" },
1004 { "Mic2 Amplifier Source Route", "Mic3", "MIC3" },
1005 { "Mic2 Amplifier", NULL, "Mic2 Amplifier Source Route"},
1006
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001007 /* Left Mixer Routes */
1008 { "Left Mixer", "DAC Playback Switch", "Left DAC" },
1009 { "Left Mixer", "DAC Reversed Playback Switch", "Right DAC" },
Chen-Yu Tsaidff50512016-11-03 15:55:49 +08001010 { "Left Mixer", "Line In Playback Switch", "LINEIN" },
Chen-Yu Tsaiecd5cdb2016-11-03 15:55:51 +08001011 { "Left Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
1012 { "Left Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001013
1014 /* Right Mixer Routes */
1015 { "Right Mixer", "DAC Playback Switch", "Right DAC" },
1016 { "Right Mixer", "DAC Reversed Playback Switch", "Left DAC" },
Chen-Yu Tsaidff50512016-11-03 15:55:49 +08001017 { "Right Mixer", "Line In Playback Switch", "LINEIN" },
Chen-Yu Tsaiecd5cdb2016-11-03 15:55:51 +08001018 { "Right Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
1019 { "Right Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001020
Chen-Yu Tsai24c99f82016-11-07 18:06:59 +08001021 /* Left ADC Mixer Routes */
1022 { "Left ADC Mixer", "Mixer Capture Switch", "Left Mixer" },
1023 { "Left ADC Mixer", "Mixer Reversed Capture Switch", "Right Mixer" },
1024 { "Left ADC Mixer", "Line In Capture Switch", "LINEIN" },
1025 { "Left ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
1026 { "Left ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
1027
1028 /* Right ADC Mixer Routes */
1029 { "Right ADC Mixer", "Mixer Capture Switch", "Right Mixer" },
1030 { "Right ADC Mixer", "Mixer Reversed Capture Switch", "Left Mixer" },
1031 { "Right ADC Mixer", "Line In Capture Switch", "LINEIN" },
1032 { "Right ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
1033 { "Right ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
1034
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001035 /* Headphone Routes */
1036 { "Headphone Source Playback Route", "DAC", "Left DAC" },
1037 { "Headphone Source Playback Route", "DAC", "Right DAC" },
1038 { "Headphone Source Playback Route", "Mixer", "Left Mixer" },
1039 { "Headphone Source Playback Route", "Mixer", "Right Mixer" },
1040 { "Headphone Amp", NULL, "Headphone Source Playback Route" },
1041 { "HP", NULL, "Headphone Amp" },
1042 { "HPCOM", NULL, "HPCOM Protection" },
Chen-Yu Tsai0f909f92016-11-03 15:55:50 +08001043
1044 /* Line Out Routes */
1045 { "Line Out Source Playback Route", "Stereo", "Left Mixer" },
1046 { "Line Out Source Playback Route", "Stereo", "Right Mixer" },
1047 { "Line Out Source Playback Route", "Mono Differential", "Left Mixer" },
1048 { "LINEOUT", NULL, "Line Out Source Playback Route" },
Chen-Yu Tsai24c99f82016-11-07 18:06:59 +08001049
1050 /* ADC Routes */
1051 { "Left ADC", NULL, "ADC Enable" },
1052 { "Right ADC", NULL, "ADC Enable" },
1053 { "Left ADC", NULL, "Left ADC Mixer" },
1054 { "Right ADC", NULL, "Right ADC Mixer" },
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001055};
1056
1057static struct snd_soc_codec_driver sun6i_codec_codec = {
1058 .component_driver = {
1059 .controls = sun6i_codec_codec_widgets,
1060 .num_controls = ARRAY_SIZE(sun6i_codec_codec_widgets),
1061 .dapm_widgets = sun6i_codec_codec_dapm_widgets,
1062 .num_dapm_widgets = ARRAY_SIZE(sun6i_codec_codec_dapm_widgets),
1063 .dapm_routes = sun6i_codec_codec_dapm_routes,
1064 .num_dapm_routes = ARRAY_SIZE(sun6i_codec_codec_dapm_routes),
1065 },
1066};
1067
Emilio López45fb6b62015-09-12 15:26:24 +02001068static const struct snd_soc_component_driver sun4i_codec_component = {
1069 .name = "sun4i-codec",
1070};
1071
1072#define SUN4I_CODEC_RATES SNDRV_PCM_RATE_8000_192000
1073#define SUN4I_CODEC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
1074 SNDRV_PCM_FMTBIT_S32_LE)
1075
1076static int sun4i_codec_dai_probe(struct snd_soc_dai *dai)
1077{
1078 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
1079 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
1080
1081 snd_soc_dai_init_dma_data(dai, &scodec->playback_dma_data,
Maxime Ripard1fb34b42015-11-30 16:37:47 +01001082 &scodec->capture_dma_data);
Emilio López45fb6b62015-09-12 15:26:24 +02001083
1084 return 0;
1085}
1086
1087static struct snd_soc_dai_driver dummy_cpu_dai = {
1088 .name = "sun4i-codec-cpu-dai",
1089 .probe = sun4i_codec_dai_probe,
1090 .playback = {
1091 .stream_name = "Playback",
1092 .channels_min = 1,
1093 .channels_max = 2,
1094 .rates = SUN4I_CODEC_RATES,
1095 .formats = SUN4I_CODEC_FORMATS,
1096 .sig_bits = 24,
1097 },
Maxime Ripard1fb34b42015-11-30 16:37:47 +01001098 .capture = {
1099 .stream_name = "Capture",
1100 .channels_min = 1,
1101 .channels_max = 2,
1102 .rates = SUN4I_CODEC_RATES,
1103 .formats = SUN4I_CODEC_FORMATS,
1104 .sig_bits = 24,
1105 },
Emilio López45fb6b62015-09-12 15:26:24 +02001106};
1107
Emilio López45fb6b62015-09-12 15:26:24 +02001108static struct snd_soc_dai_link *sun4i_codec_create_link(struct device *dev,
1109 int *num_links)
1110{
1111 struct snd_soc_dai_link *link = devm_kzalloc(dev, sizeof(*link),
1112 GFP_KERNEL);
1113 if (!link)
1114 return NULL;
1115
1116 link->name = "cdc";
1117 link->stream_name = "CDC PCM";
1118 link->codec_dai_name = "Codec";
1119 link->cpu_dai_name = dev_name(dev);
1120 link->codec_name = dev_name(dev);
1121 link->platform_name = dev_name(dev);
1122 link->dai_fmt = SND_SOC_DAIFMT_I2S;
1123
1124 *num_links = 1;
1125
1126 return link;
1127};
1128
Hans de Goede40592622015-12-11 19:43:57 +01001129static int sun4i_codec_spk_event(struct snd_soc_dapm_widget *w,
1130 struct snd_kcontrol *k, int event)
1131{
1132 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(w->dapm->card);
1133
1134 if (scodec->gpio_pa)
1135 gpiod_set_value_cansleep(scodec->gpio_pa,
1136 !!SND_SOC_DAPM_EVENT_ON(event));
1137
1138 return 0;
1139}
1140
1141static const struct snd_soc_dapm_widget sun4i_codec_card_dapm_widgets[] = {
1142 SND_SOC_DAPM_SPK("Speaker", sun4i_codec_spk_event),
1143};
1144
1145static const struct snd_soc_dapm_route sun4i_codec_card_dapm_routes[] = {
Hans de Goede6b803c62015-12-22 23:00:17 +01001146 { "Speaker", NULL, "HP Right" },
1147 { "Speaker", NULL, "HP Left" },
Hans de Goede40592622015-12-11 19:43:57 +01001148};
1149
Emilio López45fb6b62015-09-12 15:26:24 +02001150static struct snd_soc_card *sun4i_codec_create_card(struct device *dev)
1151{
1152 struct snd_soc_card *card;
Emilio López45fb6b62015-09-12 15:26:24 +02001153
1154 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
1155 if (!card)
Chen-Yu Tsai85915b62016-10-31 14:42:09 +08001156 return ERR_PTR(-ENOMEM);
Emilio López45fb6b62015-09-12 15:26:24 +02001157
1158 card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
1159 if (!card->dai_link)
Chen-Yu Tsai85915b62016-10-31 14:42:09 +08001160 return ERR_PTR(-ENOMEM);
Emilio López45fb6b62015-09-12 15:26:24 +02001161
1162 card->dev = dev;
1163 card->name = "sun4i-codec";
Hans de Goede40592622015-12-11 19:43:57 +01001164 card->dapm_widgets = sun4i_codec_card_dapm_widgets;
1165 card->num_dapm_widgets = ARRAY_SIZE(sun4i_codec_card_dapm_widgets);
1166 card->dapm_routes = sun4i_codec_card_dapm_routes;
1167 card->num_dapm_routes = ARRAY_SIZE(sun4i_codec_card_dapm_routes);
Emilio López45fb6b62015-09-12 15:26:24 +02001168
Emilio López45fb6b62015-09-12 15:26:24 +02001169 return card;
1170};
1171
Chen-Yu Tsai300a18d2016-11-03 15:55:53 +08001172static const struct snd_soc_dapm_widget sun6i_codec_card_dapm_widgets[] = {
1173 SND_SOC_DAPM_HP("Headphone", NULL),
1174 SND_SOC_DAPM_LINE("Line In", NULL),
1175 SND_SOC_DAPM_LINE("Line Out", NULL),
1176 SND_SOC_DAPM_MIC("Headset Mic", NULL),
1177 SND_SOC_DAPM_MIC("Mic", NULL),
1178 SND_SOC_DAPM_SPK("Speaker", sun4i_codec_spk_event),
1179};
1180
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001181static struct snd_soc_card *sun6i_codec_create_card(struct device *dev)
1182{
1183 struct snd_soc_card *card;
Chen-Yu Tsai300a18d2016-11-03 15:55:53 +08001184 int ret;
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001185
1186 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
1187 if (!card)
1188 return ERR_PTR(-ENOMEM);
1189
1190 card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
1191 if (!card->dai_link)
1192 return ERR_PTR(-ENOMEM);
1193
Chen-Yu Tsai300a18d2016-11-03 15:55:53 +08001194 card->dev = dev;
1195 card->name = "A31 Audio Codec";
1196 card->dapm_widgets = sun6i_codec_card_dapm_widgets;
1197 card->num_dapm_widgets = ARRAY_SIZE(sun6i_codec_card_dapm_widgets);
1198 card->fully_routed = true;
1199
1200 ret = snd_soc_of_parse_audio_routing(card, "allwinner,audio-routing");
1201 if (ret)
1202 dev_warn(dev, "failed to parse audio-routing: %d\n", ret);
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001203
1204 return card;
1205};
1206
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001207static const struct regmap_config sun4i_codec_regmap_config = {
1208 .reg_bits = 32,
1209 .reg_stride = 4,
1210 .val_bits = 32,
1211 .max_register = SUN4I_CODEC_ADC_RXCNT,
1212};
1213
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001214static const struct regmap_config sun6i_codec_regmap_config = {
1215 .reg_bits = 32,
1216 .reg_stride = 4,
1217 .val_bits = 32,
1218 .max_register = SUN6I_CODEC_HMIC_DATA,
1219};
1220
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001221static const struct regmap_config sun7i_codec_regmap_config = {
1222 .reg_bits = 32,
1223 .reg_stride = 4,
1224 .val_bits = 32,
1225 .max_register = SUN7I_CODEC_AC_MIC_PHONE_CAL,
1226};
1227
1228struct sun4i_codec_quirks {
1229 const struct regmap_config *regmap_config;
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001230 const struct snd_soc_codec_driver *codec;
1231 struct snd_soc_card * (*create_card)(struct device *dev);
1232 struct reg_field reg_adc_fifoc; /* used for regmap_field */
1233 unsigned int reg_dac_txdata; /* TX FIFO offset for DMA config */
1234 unsigned int reg_adc_rxdata; /* RX FIFO offset for DMA config */
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001235};
1236
1237static const struct sun4i_codec_quirks sun4i_codec_quirks = {
1238 .regmap_config = &sun4i_codec_regmap_config,
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001239 .codec = &sun4i_codec_codec,
1240 .create_card = sun4i_codec_create_card,
1241 .reg_adc_fifoc = REG_FIELD(SUN4I_CODEC_ADC_FIFOC, 0, 31),
1242 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
1243 .reg_adc_rxdata = SUN4I_CODEC_ADC_RXDATA,
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001244};
1245
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001246static const struct sun4i_codec_quirks sun6i_a31_codec_quirks = {
1247 .regmap_config = &sun6i_codec_regmap_config,
1248 .codec = &sun6i_codec_codec,
1249 .create_card = sun6i_codec_create_card,
1250 .reg_adc_fifoc = REG_FIELD(SUN6I_CODEC_ADC_FIFOC, 0, 31),
1251 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
1252 .reg_adc_rxdata = SUN6I_CODEC_ADC_RXDATA,
1253 .has_reset = true,
1254};
1255
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001256static const struct sun4i_codec_quirks sun7i_codec_quirks = {
1257 .regmap_config = &sun7i_codec_regmap_config,
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001258 .codec = &sun4i_codec_codec,
1259 .create_card = sun4i_codec_create_card,
1260 .reg_adc_fifoc = REG_FIELD(SUN4I_CODEC_ADC_FIFOC, 0, 31),
1261 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
1262 .reg_adc_rxdata = SUN4I_CODEC_ADC_RXDATA,
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001263};
1264
1265static const struct of_device_id sun4i_codec_of_match[] = {
1266 {
1267 .compatible = "allwinner,sun4i-a10-codec",
1268 .data = &sun4i_codec_quirks,
1269 },
1270 {
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001271 .compatible = "allwinner,sun6i-a31-codec",
1272 .data = &sun6i_a31_codec_quirks,
1273 },
1274 {
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001275 .compatible = "allwinner,sun7i-a20-codec",
1276 .data = &sun7i_codec_quirks,
1277 },
1278 {}
1279};
1280MODULE_DEVICE_TABLE(of, sun4i_codec_of_match);
1281
Emilio López45fb6b62015-09-12 15:26:24 +02001282static int sun4i_codec_probe(struct platform_device *pdev)
1283{
1284 struct snd_soc_card *card;
1285 struct sun4i_codec *scodec;
Danny Milosavljevicc1d50652016-09-22 09:13:13 +02001286 const struct sun4i_codec_quirks *quirks;
Emilio López45fb6b62015-09-12 15:26:24 +02001287 struct resource *res;
1288 void __iomem *base;
1289 int ret;
1290
1291 scodec = devm_kzalloc(&pdev->dev, sizeof(*scodec), GFP_KERNEL);
1292 if (!scodec)
1293 return -ENOMEM;
1294
1295 scodec->dev = &pdev->dev;
1296
1297 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1298 base = devm_ioremap_resource(&pdev->dev, res);
1299 if (IS_ERR(base)) {
1300 dev_err(&pdev->dev, "Failed to map the registers\n");
1301 return PTR_ERR(base);
1302 }
1303
Danny Milosavljevicc1d50652016-09-22 09:13:13 +02001304 quirks = of_device_get_match_data(&pdev->dev);
1305 if (quirks == NULL) {
1306 dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
1307 return -ENODEV;
1308 }
1309
Emilio López45fb6b62015-09-12 15:26:24 +02001310 scodec->regmap = devm_regmap_init_mmio(&pdev->dev, base,
Danny Milosavljevicc1d50652016-09-22 09:13:13 +02001311 quirks->regmap_config);
Emilio López45fb6b62015-09-12 15:26:24 +02001312 if (IS_ERR(scodec->regmap)) {
1313 dev_err(&pdev->dev, "Failed to create our regmap\n");
1314 return PTR_ERR(scodec->regmap);
1315 }
1316
1317 /* Get the clocks from the DT */
1318 scodec->clk_apb = devm_clk_get(&pdev->dev, "apb");
1319 if (IS_ERR(scodec->clk_apb)) {
1320 dev_err(&pdev->dev, "Failed to get the APB clock\n");
1321 return PTR_ERR(scodec->clk_apb);
1322 }
1323
1324 scodec->clk_module = devm_clk_get(&pdev->dev, "codec");
1325 if (IS_ERR(scodec->clk_module)) {
1326 dev_err(&pdev->dev, "Failed to get the module clock\n");
1327 return PTR_ERR(scodec->clk_module);
1328 }
1329
Hans de Goede40592622015-12-11 19:43:57 +01001330 scodec->gpio_pa = devm_gpiod_get_optional(&pdev->dev, "allwinner,pa",
1331 GPIOD_OUT_LOW);
1332 if (IS_ERR(scodec->gpio_pa)) {
1333 ret = PTR_ERR(scodec->gpio_pa);
1334 if (ret != -EPROBE_DEFER)
1335 dev_err(&pdev->dev, "Failed to get pa gpio: %d\n", ret);
1336 return ret;
1337 }
1338
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001339 /* reg_field setup */
1340 scodec->reg_adc_fifoc = devm_regmap_field_alloc(&pdev->dev,
1341 scodec->regmap,
1342 quirks->reg_adc_fifoc);
1343 if (IS_ERR(scodec->reg_adc_fifoc)) {
1344 ret = PTR_ERR(scodec->reg_adc_fifoc);
1345 dev_err(&pdev->dev, "Failed to create regmap fields: %d\n",
1346 ret);
1347 return ret;
1348 }
1349
Chen-Yu Tsai3716a892016-11-01 14:31:55 +08001350 /* Enable the bus clock */
1351 if (clk_prepare_enable(scodec->clk_apb)) {
1352 dev_err(&pdev->dev, "Failed to enable the APB clock\n");
1353 return -EINVAL;
1354 }
1355
Emilio López45fb6b62015-09-12 15:26:24 +02001356 /* DMA configuration for TX FIFO */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001357 scodec->playback_dma_data.addr = res->start + quirks->reg_dac_txdata;
Chen-Yu Tsai730e2dd2016-11-03 15:55:46 +08001358 scodec->playback_dma_data.maxburst = 8;
Emilio López45fb6b62015-09-12 15:26:24 +02001359 scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1360
Maxime Ripard1fb34b42015-11-30 16:37:47 +01001361 /* DMA configuration for RX FIFO */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001362 scodec->capture_dma_data.addr = res->start + quirks->reg_adc_rxdata;
Chen-Yu Tsai730e2dd2016-11-03 15:55:46 +08001363 scodec->capture_dma_data.maxburst = 8;
Maxime Ripard1fb34b42015-11-30 16:37:47 +01001364 scodec->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1365
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001366 ret = snd_soc_register_codec(&pdev->dev, quirks->codec,
Emilio López45fb6b62015-09-12 15:26:24 +02001367 &sun4i_codec_dai, 1);
1368 if (ret) {
1369 dev_err(&pdev->dev, "Failed to register our codec\n");
1370 goto err_clk_disable;
1371 }
1372
1373 ret = devm_snd_soc_register_component(&pdev->dev,
1374 &sun4i_codec_component,
1375 &dummy_cpu_dai, 1);
1376 if (ret) {
1377 dev_err(&pdev->dev, "Failed to register our DAI\n");
1378 goto err_unregister_codec;
1379 }
1380
1381 ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
1382 if (ret) {
1383 dev_err(&pdev->dev, "Failed to register against DMAEngine\n");
1384 goto err_unregister_codec;
1385 }
1386
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001387 card = quirks->create_card(&pdev->dev);
Chen-Yu Tsai85915b62016-10-31 14:42:09 +08001388 if (IS_ERR(card)) {
1389 ret = PTR_ERR(card);
Emilio López45fb6b62015-09-12 15:26:24 +02001390 dev_err(&pdev->dev, "Failed to create our card\n");
1391 goto err_unregister_codec;
1392 }
1393
1394 platform_set_drvdata(pdev, card);
1395 snd_soc_card_set_drvdata(card, scodec);
1396
1397 ret = snd_soc_register_card(card);
1398 if (ret) {
1399 dev_err(&pdev->dev, "Failed to register our card\n");
1400 goto err_unregister_codec;
1401 }
1402
1403 return 0;
1404
1405err_unregister_codec:
1406 snd_soc_unregister_codec(&pdev->dev);
1407err_clk_disable:
1408 clk_disable_unprepare(scodec->clk_apb);
1409 return ret;
1410}
1411
1412static int sun4i_codec_remove(struct platform_device *pdev)
1413{
1414 struct snd_soc_card *card = platform_get_drvdata(pdev);
1415 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
1416
1417 snd_soc_unregister_card(card);
1418 snd_soc_unregister_codec(&pdev->dev);
1419 clk_disable_unprepare(scodec->clk_apb);
1420
1421 return 0;
1422}
1423
1424static struct platform_driver sun4i_codec_driver = {
1425 .driver = {
1426 .name = "sun4i-codec",
Emilio López45fb6b62015-09-12 15:26:24 +02001427 .of_match_table = sun4i_codec_of_match,
1428 },
1429 .probe = sun4i_codec_probe,
1430 .remove = sun4i_codec_remove,
1431};
1432module_platform_driver(sun4i_codec_driver);
1433
1434MODULE_DESCRIPTION("Allwinner A10 codec driver");
1435MODULE_AUTHOR("Emilio López <emilio@elopez.com.ar>");
1436MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
1437MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001438MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
Emilio López45fb6b62015-09-12 15:26:24 +02001439MODULE_LICENSE("GPL");