blob: 72a84f76aa572b1d631ddf8025df8ad8e2d538f9 [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 Tsai8d9e4c9e2016-11-03 15:55:48 +0800779};
780
781/* headphone controls */
782static const char * const sun6i_codec_hp_src_enum_text[] = {
783 "DAC", "Mixer",
784};
785
786static SOC_ENUM_DOUBLE_DECL(sun6i_codec_hp_src_enum,
787 SUN6I_CODEC_OM_DACA_CTRL,
788 SUN6I_CODEC_OM_DACA_CTRL_LHPIS,
789 SUN6I_CODEC_OM_DACA_CTRL_RHPIS,
790 sun6i_codec_hp_src_enum_text);
791
792static const struct snd_kcontrol_new sun6i_codec_hp_src[] = {
793 SOC_DAPM_ENUM("Headphone Source Playback Route",
794 sun6i_codec_hp_src_enum),
795};
796
797/* volume / mute controls */
798static const DECLARE_TLV_DB_SCALE(sun6i_codec_dvol_scale, -7308, 116, 0);
799static const DECLARE_TLV_DB_SCALE(sun6i_codec_hp_vol_scale, -6300, 100, 1);
Chen-Yu Tsaidff50512016-11-03 15:55:49 +0800800static const DECLARE_TLV_DB_SCALE(sun6i_codec_out_mixer_pregain_scale,
801 -450, 150, 0);
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800802
803static const struct snd_kcontrol_new sun6i_codec_codec_widgets[] = {
804 SOC_SINGLE_TLV("DAC Playback Volume", SUN4I_CODEC_DAC_DPC,
805 SUN4I_CODEC_DAC_DPC_DVOL, 0x3f, 1,
806 sun6i_codec_dvol_scale),
807 SOC_SINGLE_TLV("Headphone Playback Volume",
808 SUN6I_CODEC_OM_DACA_CTRL,
809 SUN6I_CODEC_OM_DACA_CTRL_HPVOL, 0x3f, 0,
810 sun6i_codec_hp_vol_scale),
811 SOC_DOUBLE("Headphone Playback Switch",
812 SUN6I_CODEC_OM_DACA_CTRL,
813 SUN6I_CODEC_OM_DACA_CTRL_LHPPAMUTE,
814 SUN6I_CODEC_OM_DACA_CTRL_RHPPAMUTE, 1, 0),
Chen-Yu Tsaidff50512016-11-03 15:55:49 +0800815 /* Mixer pre-gains */
816 SOC_SINGLE_TLV("Line In Playback Volume",
817 SUN6I_CODEC_OM_PA_CTRL, SUN6I_CODEC_OM_PA_CTRL_LINEING,
818 0x7, 0, sun6i_codec_out_mixer_pregain_scale),
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800819};
820
821static const struct snd_soc_dapm_widget sun6i_codec_codec_dapm_widgets[] = {
Chen-Yu Tsaidff50512016-11-03 15:55:49 +0800822 /* Line In */
823 SND_SOC_DAPM_INPUT("LINEIN"),
824
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800825 /* Digital parts of the DACs */
826 SND_SOC_DAPM_SUPPLY("DAC Enable", SUN4I_CODEC_DAC_DPC,
827 SUN4I_CODEC_DAC_DPC_EN_DA, 0,
828 NULL, 0),
829
830 /* Analog parts of the DACs */
831 SND_SOC_DAPM_DAC("Left DAC", "Codec Playback",
832 SUN6I_CODEC_OM_DACA_CTRL,
833 SUN6I_CODEC_OM_DACA_CTRL_DACALEN, 0),
834 SND_SOC_DAPM_DAC("Right DAC", "Codec Playback",
835 SUN6I_CODEC_OM_DACA_CTRL,
836 SUN6I_CODEC_OM_DACA_CTRL_DACAREN, 0),
837
838 /* Mixers */
839 SOC_MIXER_ARRAY("Left Mixer", SUN6I_CODEC_OM_DACA_CTRL,
840 SUN6I_CODEC_OM_DACA_CTRL_LMIXEN, 0,
841 sun6i_codec_mixer_controls),
842 SOC_MIXER_ARRAY("Right Mixer", SUN6I_CODEC_OM_DACA_CTRL,
843 SUN6I_CODEC_OM_DACA_CTRL_RMIXEN, 0,
844 sun6i_codec_mixer_controls),
845
846 /* Headphone output path */
847 SND_SOC_DAPM_MUX("Headphone Source Playback Route",
848 SND_SOC_NOPM, 0, 0, sun6i_codec_hp_src),
849 SND_SOC_DAPM_OUT_DRV("Headphone Amp", SUN6I_CODEC_OM_PA_CTRL,
850 SUN6I_CODEC_OM_PA_CTRL_HPPAEN, 0, NULL, 0),
851 SND_SOC_DAPM_SUPPLY("HPCOM Protection", SUN6I_CODEC_OM_PA_CTRL,
852 SUN6I_CODEC_OM_PA_CTRL_COMPTEN, 0, NULL, 0),
853 SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPCOM", SUN6I_CODEC_OM_PA_CTRL,
854 SUN6I_CODEC_OM_PA_CTRL_HPCOM_CTL, 0x3, 0x3, 0),
855 SND_SOC_DAPM_OUTPUT("HP"),
856};
857
858static const struct snd_soc_dapm_route sun6i_codec_codec_dapm_routes[] = {
859 /* DAC Routes */
860 { "Left DAC", NULL, "DAC Enable" },
861 { "Right DAC", NULL, "DAC Enable" },
862
863 /* Left Mixer Routes */
864 { "Left Mixer", "DAC Playback Switch", "Left DAC" },
865 { "Left Mixer", "DAC Reversed Playback Switch", "Right DAC" },
Chen-Yu Tsaidff50512016-11-03 15:55:49 +0800866 { "Left Mixer", "Line In Playback Switch", "LINEIN" },
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800867
868 /* Right Mixer Routes */
869 { "Right Mixer", "DAC Playback Switch", "Right DAC" },
870 { "Right Mixer", "DAC Reversed Playback Switch", "Left DAC" },
Chen-Yu Tsaidff50512016-11-03 15:55:49 +0800871 { "Right Mixer", "Line In Playback Switch", "LINEIN" },
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800872
873 /* Headphone Routes */
874 { "Headphone Source Playback Route", "DAC", "Left DAC" },
875 { "Headphone Source Playback Route", "DAC", "Right DAC" },
876 { "Headphone Source Playback Route", "Mixer", "Left Mixer" },
877 { "Headphone Source Playback Route", "Mixer", "Right Mixer" },
878 { "Headphone Amp", NULL, "Headphone Source Playback Route" },
879 { "HP", NULL, "Headphone Amp" },
880 { "HPCOM", NULL, "HPCOM Protection" },
881};
882
883static struct snd_soc_codec_driver sun6i_codec_codec = {
884 .component_driver = {
885 .controls = sun6i_codec_codec_widgets,
886 .num_controls = ARRAY_SIZE(sun6i_codec_codec_widgets),
887 .dapm_widgets = sun6i_codec_codec_dapm_widgets,
888 .num_dapm_widgets = ARRAY_SIZE(sun6i_codec_codec_dapm_widgets),
889 .dapm_routes = sun6i_codec_codec_dapm_routes,
890 .num_dapm_routes = ARRAY_SIZE(sun6i_codec_codec_dapm_routes),
891 },
892};
893
Emilio López45fb6b62015-09-12 15:26:24 +0200894static const struct snd_soc_component_driver sun4i_codec_component = {
895 .name = "sun4i-codec",
896};
897
898#define SUN4I_CODEC_RATES SNDRV_PCM_RATE_8000_192000
899#define SUN4I_CODEC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
900 SNDRV_PCM_FMTBIT_S32_LE)
901
902static int sun4i_codec_dai_probe(struct snd_soc_dai *dai)
903{
904 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
905 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
906
907 snd_soc_dai_init_dma_data(dai, &scodec->playback_dma_data,
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100908 &scodec->capture_dma_data);
Emilio López45fb6b62015-09-12 15:26:24 +0200909
910 return 0;
911}
912
913static struct snd_soc_dai_driver dummy_cpu_dai = {
914 .name = "sun4i-codec-cpu-dai",
915 .probe = sun4i_codec_dai_probe,
916 .playback = {
917 .stream_name = "Playback",
918 .channels_min = 1,
919 .channels_max = 2,
920 .rates = SUN4I_CODEC_RATES,
921 .formats = SUN4I_CODEC_FORMATS,
922 .sig_bits = 24,
923 },
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100924 .capture = {
925 .stream_name = "Capture",
926 .channels_min = 1,
927 .channels_max = 2,
928 .rates = SUN4I_CODEC_RATES,
929 .formats = SUN4I_CODEC_FORMATS,
930 .sig_bits = 24,
931 },
Emilio López45fb6b62015-09-12 15:26:24 +0200932};
933
Emilio López45fb6b62015-09-12 15:26:24 +0200934static struct snd_soc_dai_link *sun4i_codec_create_link(struct device *dev,
935 int *num_links)
936{
937 struct snd_soc_dai_link *link = devm_kzalloc(dev, sizeof(*link),
938 GFP_KERNEL);
939 if (!link)
940 return NULL;
941
942 link->name = "cdc";
943 link->stream_name = "CDC PCM";
944 link->codec_dai_name = "Codec";
945 link->cpu_dai_name = dev_name(dev);
946 link->codec_name = dev_name(dev);
947 link->platform_name = dev_name(dev);
948 link->dai_fmt = SND_SOC_DAIFMT_I2S;
949
950 *num_links = 1;
951
952 return link;
953};
954
Hans de Goede40592622015-12-11 19:43:57 +0100955static int sun4i_codec_spk_event(struct snd_soc_dapm_widget *w,
956 struct snd_kcontrol *k, int event)
957{
958 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(w->dapm->card);
959
960 if (scodec->gpio_pa)
961 gpiod_set_value_cansleep(scodec->gpio_pa,
962 !!SND_SOC_DAPM_EVENT_ON(event));
963
964 return 0;
965}
966
967static const struct snd_soc_dapm_widget sun4i_codec_card_dapm_widgets[] = {
968 SND_SOC_DAPM_SPK("Speaker", sun4i_codec_spk_event),
969};
970
971static const struct snd_soc_dapm_route sun4i_codec_card_dapm_routes[] = {
Hans de Goede6b803c62015-12-22 23:00:17 +0100972 { "Speaker", NULL, "HP Right" },
973 { "Speaker", NULL, "HP Left" },
Hans de Goede40592622015-12-11 19:43:57 +0100974};
975
Emilio López45fb6b62015-09-12 15:26:24 +0200976static struct snd_soc_card *sun4i_codec_create_card(struct device *dev)
977{
978 struct snd_soc_card *card;
Emilio López45fb6b62015-09-12 15:26:24 +0200979
980 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
981 if (!card)
Chen-Yu Tsai85915b62016-10-31 14:42:09 +0800982 return ERR_PTR(-ENOMEM);
Emilio López45fb6b62015-09-12 15:26:24 +0200983
984 card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
985 if (!card->dai_link)
Chen-Yu Tsai85915b62016-10-31 14:42:09 +0800986 return ERR_PTR(-ENOMEM);
Emilio López45fb6b62015-09-12 15:26:24 +0200987
988 card->dev = dev;
989 card->name = "sun4i-codec";
Hans de Goede40592622015-12-11 19:43:57 +0100990 card->dapm_widgets = sun4i_codec_card_dapm_widgets;
991 card->num_dapm_widgets = ARRAY_SIZE(sun4i_codec_card_dapm_widgets);
992 card->dapm_routes = sun4i_codec_card_dapm_routes;
993 card->num_dapm_routes = ARRAY_SIZE(sun4i_codec_card_dapm_routes);
Emilio López45fb6b62015-09-12 15:26:24 +0200994
Emilio López45fb6b62015-09-12 15:26:24 +0200995 return card;
996};
997
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800998static struct snd_soc_card *sun6i_codec_create_card(struct device *dev)
999{
1000 struct snd_soc_card *card;
1001
1002 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
1003 if (!card)
1004 return ERR_PTR(-ENOMEM);
1005
1006 card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
1007 if (!card->dai_link)
1008 return ERR_PTR(-ENOMEM);
1009
1010 card->dev = dev;
1011 card->name = "A31 Audio Codec";
1012
1013 return card;
1014};
1015
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001016static const struct regmap_config sun4i_codec_regmap_config = {
1017 .reg_bits = 32,
1018 .reg_stride = 4,
1019 .val_bits = 32,
1020 .max_register = SUN4I_CODEC_ADC_RXCNT,
1021};
1022
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001023static const struct regmap_config sun6i_codec_regmap_config = {
1024 .reg_bits = 32,
1025 .reg_stride = 4,
1026 .val_bits = 32,
1027 .max_register = SUN6I_CODEC_HMIC_DATA,
1028};
1029
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001030static const struct regmap_config sun7i_codec_regmap_config = {
1031 .reg_bits = 32,
1032 .reg_stride = 4,
1033 .val_bits = 32,
1034 .max_register = SUN7I_CODEC_AC_MIC_PHONE_CAL,
1035};
1036
1037struct sun4i_codec_quirks {
1038 const struct regmap_config *regmap_config;
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001039 const struct snd_soc_codec_driver *codec;
1040 struct snd_soc_card * (*create_card)(struct device *dev);
1041 struct reg_field reg_adc_fifoc; /* used for regmap_field */
1042 unsigned int reg_dac_txdata; /* TX FIFO offset for DMA config */
1043 unsigned int reg_adc_rxdata; /* RX FIFO offset for DMA config */
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001044};
1045
1046static const struct sun4i_codec_quirks sun4i_codec_quirks = {
1047 .regmap_config = &sun4i_codec_regmap_config,
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001048 .codec = &sun4i_codec_codec,
1049 .create_card = sun4i_codec_create_card,
1050 .reg_adc_fifoc = REG_FIELD(SUN4I_CODEC_ADC_FIFOC, 0, 31),
1051 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
1052 .reg_adc_rxdata = SUN4I_CODEC_ADC_RXDATA,
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001053};
1054
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001055static const struct sun4i_codec_quirks sun6i_a31_codec_quirks = {
1056 .regmap_config = &sun6i_codec_regmap_config,
1057 .codec = &sun6i_codec_codec,
1058 .create_card = sun6i_codec_create_card,
1059 .reg_adc_fifoc = REG_FIELD(SUN6I_CODEC_ADC_FIFOC, 0, 31),
1060 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
1061 .reg_adc_rxdata = SUN6I_CODEC_ADC_RXDATA,
1062 .has_reset = true,
1063};
1064
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001065static const struct sun4i_codec_quirks sun7i_codec_quirks = {
1066 .regmap_config = &sun7i_codec_regmap_config,
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001067 .codec = &sun4i_codec_codec,
1068 .create_card = sun4i_codec_create_card,
1069 .reg_adc_fifoc = REG_FIELD(SUN4I_CODEC_ADC_FIFOC, 0, 31),
1070 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
1071 .reg_adc_rxdata = SUN4I_CODEC_ADC_RXDATA,
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001072};
1073
1074static const struct of_device_id sun4i_codec_of_match[] = {
1075 {
1076 .compatible = "allwinner,sun4i-a10-codec",
1077 .data = &sun4i_codec_quirks,
1078 },
1079 {
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001080 .compatible = "allwinner,sun6i-a31-codec",
1081 .data = &sun6i_a31_codec_quirks,
1082 },
1083 {
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001084 .compatible = "allwinner,sun7i-a20-codec",
1085 .data = &sun7i_codec_quirks,
1086 },
1087 {}
1088};
1089MODULE_DEVICE_TABLE(of, sun4i_codec_of_match);
1090
Emilio López45fb6b62015-09-12 15:26:24 +02001091static int sun4i_codec_probe(struct platform_device *pdev)
1092{
1093 struct snd_soc_card *card;
1094 struct sun4i_codec *scodec;
Danny Milosavljevicc1d50652016-09-22 09:13:13 +02001095 const struct sun4i_codec_quirks *quirks;
Emilio López45fb6b62015-09-12 15:26:24 +02001096 struct resource *res;
1097 void __iomem *base;
1098 int ret;
1099
1100 scodec = devm_kzalloc(&pdev->dev, sizeof(*scodec), GFP_KERNEL);
1101 if (!scodec)
1102 return -ENOMEM;
1103
1104 scodec->dev = &pdev->dev;
1105
1106 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1107 base = devm_ioremap_resource(&pdev->dev, res);
1108 if (IS_ERR(base)) {
1109 dev_err(&pdev->dev, "Failed to map the registers\n");
1110 return PTR_ERR(base);
1111 }
1112
Danny Milosavljevicc1d50652016-09-22 09:13:13 +02001113 quirks = of_device_get_match_data(&pdev->dev);
1114 if (quirks == NULL) {
1115 dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
1116 return -ENODEV;
1117 }
1118
Emilio López45fb6b62015-09-12 15:26:24 +02001119 scodec->regmap = devm_regmap_init_mmio(&pdev->dev, base,
Danny Milosavljevicc1d50652016-09-22 09:13:13 +02001120 quirks->regmap_config);
Emilio López45fb6b62015-09-12 15:26:24 +02001121 if (IS_ERR(scodec->regmap)) {
1122 dev_err(&pdev->dev, "Failed to create our regmap\n");
1123 return PTR_ERR(scodec->regmap);
1124 }
1125
1126 /* Get the clocks from the DT */
1127 scodec->clk_apb = devm_clk_get(&pdev->dev, "apb");
1128 if (IS_ERR(scodec->clk_apb)) {
1129 dev_err(&pdev->dev, "Failed to get the APB clock\n");
1130 return PTR_ERR(scodec->clk_apb);
1131 }
1132
1133 scodec->clk_module = devm_clk_get(&pdev->dev, "codec");
1134 if (IS_ERR(scodec->clk_module)) {
1135 dev_err(&pdev->dev, "Failed to get the module clock\n");
1136 return PTR_ERR(scodec->clk_module);
1137 }
1138
Hans de Goede40592622015-12-11 19:43:57 +01001139 scodec->gpio_pa = devm_gpiod_get_optional(&pdev->dev, "allwinner,pa",
1140 GPIOD_OUT_LOW);
1141 if (IS_ERR(scodec->gpio_pa)) {
1142 ret = PTR_ERR(scodec->gpio_pa);
1143 if (ret != -EPROBE_DEFER)
1144 dev_err(&pdev->dev, "Failed to get pa gpio: %d\n", ret);
1145 return ret;
1146 }
1147
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001148 /* reg_field setup */
1149 scodec->reg_adc_fifoc = devm_regmap_field_alloc(&pdev->dev,
1150 scodec->regmap,
1151 quirks->reg_adc_fifoc);
1152 if (IS_ERR(scodec->reg_adc_fifoc)) {
1153 ret = PTR_ERR(scodec->reg_adc_fifoc);
1154 dev_err(&pdev->dev, "Failed to create regmap fields: %d\n",
1155 ret);
1156 return ret;
1157 }
1158
Chen-Yu Tsai3716a892016-11-01 14:31:55 +08001159 /* Enable the bus clock */
1160 if (clk_prepare_enable(scodec->clk_apb)) {
1161 dev_err(&pdev->dev, "Failed to enable the APB clock\n");
1162 return -EINVAL;
1163 }
1164
Emilio López45fb6b62015-09-12 15:26:24 +02001165 /* DMA configuration for TX FIFO */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001166 scodec->playback_dma_data.addr = res->start + quirks->reg_dac_txdata;
Chen-Yu Tsai730e2dd2016-11-03 15:55:46 +08001167 scodec->playback_dma_data.maxburst = 8;
Emilio López45fb6b62015-09-12 15:26:24 +02001168 scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1169
Maxime Ripard1fb34b42015-11-30 16:37:47 +01001170 /* DMA configuration for RX FIFO */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001171 scodec->capture_dma_data.addr = res->start + quirks->reg_adc_rxdata;
Chen-Yu Tsai730e2dd2016-11-03 15:55:46 +08001172 scodec->capture_dma_data.maxburst = 8;
Maxime Ripard1fb34b42015-11-30 16:37:47 +01001173 scodec->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1174
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001175 ret = snd_soc_register_codec(&pdev->dev, quirks->codec,
Emilio López45fb6b62015-09-12 15:26:24 +02001176 &sun4i_codec_dai, 1);
1177 if (ret) {
1178 dev_err(&pdev->dev, "Failed to register our codec\n");
1179 goto err_clk_disable;
1180 }
1181
1182 ret = devm_snd_soc_register_component(&pdev->dev,
1183 &sun4i_codec_component,
1184 &dummy_cpu_dai, 1);
1185 if (ret) {
1186 dev_err(&pdev->dev, "Failed to register our DAI\n");
1187 goto err_unregister_codec;
1188 }
1189
1190 ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
1191 if (ret) {
1192 dev_err(&pdev->dev, "Failed to register against DMAEngine\n");
1193 goto err_unregister_codec;
1194 }
1195
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001196 card = quirks->create_card(&pdev->dev);
Chen-Yu Tsai85915b62016-10-31 14:42:09 +08001197 if (IS_ERR(card)) {
1198 ret = PTR_ERR(card);
Emilio López45fb6b62015-09-12 15:26:24 +02001199 dev_err(&pdev->dev, "Failed to create our card\n");
1200 goto err_unregister_codec;
1201 }
1202
1203 platform_set_drvdata(pdev, card);
1204 snd_soc_card_set_drvdata(card, scodec);
1205
1206 ret = snd_soc_register_card(card);
1207 if (ret) {
1208 dev_err(&pdev->dev, "Failed to register our card\n");
1209 goto err_unregister_codec;
1210 }
1211
1212 return 0;
1213
1214err_unregister_codec:
1215 snd_soc_unregister_codec(&pdev->dev);
1216err_clk_disable:
1217 clk_disable_unprepare(scodec->clk_apb);
1218 return ret;
1219}
1220
1221static int sun4i_codec_remove(struct platform_device *pdev)
1222{
1223 struct snd_soc_card *card = platform_get_drvdata(pdev);
1224 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
1225
1226 snd_soc_unregister_card(card);
1227 snd_soc_unregister_codec(&pdev->dev);
1228 clk_disable_unprepare(scodec->clk_apb);
1229
1230 return 0;
1231}
1232
1233static struct platform_driver sun4i_codec_driver = {
1234 .driver = {
1235 .name = "sun4i-codec",
Emilio López45fb6b62015-09-12 15:26:24 +02001236 .of_match_table = sun4i_codec_of_match,
1237 },
1238 .probe = sun4i_codec_probe,
1239 .remove = sun4i_codec_remove,
1240};
1241module_platform_driver(sun4i_codec_driver);
1242
1243MODULE_DESCRIPTION("Allwinner A10 codec driver");
1244MODULE_AUTHOR("Emilio López <emilio@elopez.com.ar>");
1245MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
1246MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001247MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
Emilio López45fb6b62015-09-12 15:26:24 +02001248MODULE_LICENSE("GPL");