blob: d4b2186b5d84a7fd55aa3afade5cdd7265888dc3 [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),
775};
776
777/* headphone controls */
778static const char * const sun6i_codec_hp_src_enum_text[] = {
779 "DAC", "Mixer",
780};
781
782static SOC_ENUM_DOUBLE_DECL(sun6i_codec_hp_src_enum,
783 SUN6I_CODEC_OM_DACA_CTRL,
784 SUN6I_CODEC_OM_DACA_CTRL_LHPIS,
785 SUN6I_CODEC_OM_DACA_CTRL_RHPIS,
786 sun6i_codec_hp_src_enum_text);
787
788static const struct snd_kcontrol_new sun6i_codec_hp_src[] = {
789 SOC_DAPM_ENUM("Headphone Source Playback Route",
790 sun6i_codec_hp_src_enum),
791};
792
793/* volume / mute controls */
794static const DECLARE_TLV_DB_SCALE(sun6i_codec_dvol_scale, -7308, 116, 0);
795static const DECLARE_TLV_DB_SCALE(sun6i_codec_hp_vol_scale, -6300, 100, 1);
796
797static const struct snd_kcontrol_new sun6i_codec_codec_widgets[] = {
798 SOC_SINGLE_TLV("DAC Playback Volume", SUN4I_CODEC_DAC_DPC,
799 SUN4I_CODEC_DAC_DPC_DVOL, 0x3f, 1,
800 sun6i_codec_dvol_scale),
801 SOC_SINGLE_TLV("Headphone Playback Volume",
802 SUN6I_CODEC_OM_DACA_CTRL,
803 SUN6I_CODEC_OM_DACA_CTRL_HPVOL, 0x3f, 0,
804 sun6i_codec_hp_vol_scale),
805 SOC_DOUBLE("Headphone Playback Switch",
806 SUN6I_CODEC_OM_DACA_CTRL,
807 SUN6I_CODEC_OM_DACA_CTRL_LHPPAMUTE,
808 SUN6I_CODEC_OM_DACA_CTRL_RHPPAMUTE, 1, 0),
809};
810
811static const struct snd_soc_dapm_widget sun6i_codec_codec_dapm_widgets[] = {
812 /* Digital parts of the DACs */
813 SND_SOC_DAPM_SUPPLY("DAC Enable", SUN4I_CODEC_DAC_DPC,
814 SUN4I_CODEC_DAC_DPC_EN_DA, 0,
815 NULL, 0),
816
817 /* Analog parts of the DACs */
818 SND_SOC_DAPM_DAC("Left DAC", "Codec Playback",
819 SUN6I_CODEC_OM_DACA_CTRL,
820 SUN6I_CODEC_OM_DACA_CTRL_DACALEN, 0),
821 SND_SOC_DAPM_DAC("Right DAC", "Codec Playback",
822 SUN6I_CODEC_OM_DACA_CTRL,
823 SUN6I_CODEC_OM_DACA_CTRL_DACAREN, 0),
824
825 /* Mixers */
826 SOC_MIXER_ARRAY("Left Mixer", SUN6I_CODEC_OM_DACA_CTRL,
827 SUN6I_CODEC_OM_DACA_CTRL_LMIXEN, 0,
828 sun6i_codec_mixer_controls),
829 SOC_MIXER_ARRAY("Right Mixer", SUN6I_CODEC_OM_DACA_CTRL,
830 SUN6I_CODEC_OM_DACA_CTRL_RMIXEN, 0,
831 sun6i_codec_mixer_controls),
832
833 /* Headphone output path */
834 SND_SOC_DAPM_MUX("Headphone Source Playback Route",
835 SND_SOC_NOPM, 0, 0, sun6i_codec_hp_src),
836 SND_SOC_DAPM_OUT_DRV("Headphone Amp", SUN6I_CODEC_OM_PA_CTRL,
837 SUN6I_CODEC_OM_PA_CTRL_HPPAEN, 0, NULL, 0),
838 SND_SOC_DAPM_SUPPLY("HPCOM Protection", SUN6I_CODEC_OM_PA_CTRL,
839 SUN6I_CODEC_OM_PA_CTRL_COMPTEN, 0, NULL, 0),
840 SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPCOM", SUN6I_CODEC_OM_PA_CTRL,
841 SUN6I_CODEC_OM_PA_CTRL_HPCOM_CTL, 0x3, 0x3, 0),
842 SND_SOC_DAPM_OUTPUT("HP"),
843};
844
845static const struct snd_soc_dapm_route sun6i_codec_codec_dapm_routes[] = {
846 /* DAC Routes */
847 { "Left DAC", NULL, "DAC Enable" },
848 { "Right DAC", NULL, "DAC Enable" },
849
850 /* Left Mixer Routes */
851 { "Left Mixer", "DAC Playback Switch", "Left DAC" },
852 { "Left Mixer", "DAC Reversed Playback Switch", "Right DAC" },
853
854 /* Right Mixer Routes */
855 { "Right Mixer", "DAC Playback Switch", "Right DAC" },
856 { "Right Mixer", "DAC Reversed Playback Switch", "Left DAC" },
857
858 /* Headphone Routes */
859 { "Headphone Source Playback Route", "DAC", "Left DAC" },
860 { "Headphone Source Playback Route", "DAC", "Right DAC" },
861 { "Headphone Source Playback Route", "Mixer", "Left Mixer" },
862 { "Headphone Source Playback Route", "Mixer", "Right Mixer" },
863 { "Headphone Amp", NULL, "Headphone Source Playback Route" },
864 { "HP", NULL, "Headphone Amp" },
865 { "HPCOM", NULL, "HPCOM Protection" },
866};
867
868static struct snd_soc_codec_driver sun6i_codec_codec = {
869 .component_driver = {
870 .controls = sun6i_codec_codec_widgets,
871 .num_controls = ARRAY_SIZE(sun6i_codec_codec_widgets),
872 .dapm_widgets = sun6i_codec_codec_dapm_widgets,
873 .num_dapm_widgets = ARRAY_SIZE(sun6i_codec_codec_dapm_widgets),
874 .dapm_routes = sun6i_codec_codec_dapm_routes,
875 .num_dapm_routes = ARRAY_SIZE(sun6i_codec_codec_dapm_routes),
876 },
877};
878
Emilio López45fb6b62015-09-12 15:26:24 +0200879static const struct snd_soc_component_driver sun4i_codec_component = {
880 .name = "sun4i-codec",
881};
882
883#define SUN4I_CODEC_RATES SNDRV_PCM_RATE_8000_192000
884#define SUN4I_CODEC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
885 SNDRV_PCM_FMTBIT_S32_LE)
886
887static int sun4i_codec_dai_probe(struct snd_soc_dai *dai)
888{
889 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
890 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
891
892 snd_soc_dai_init_dma_data(dai, &scodec->playback_dma_data,
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100893 &scodec->capture_dma_data);
Emilio López45fb6b62015-09-12 15:26:24 +0200894
895 return 0;
896}
897
898static struct snd_soc_dai_driver dummy_cpu_dai = {
899 .name = "sun4i-codec-cpu-dai",
900 .probe = sun4i_codec_dai_probe,
901 .playback = {
902 .stream_name = "Playback",
903 .channels_min = 1,
904 .channels_max = 2,
905 .rates = SUN4I_CODEC_RATES,
906 .formats = SUN4I_CODEC_FORMATS,
907 .sig_bits = 24,
908 },
Maxime Ripard1fb34b42015-11-30 16:37:47 +0100909 .capture = {
910 .stream_name = "Capture",
911 .channels_min = 1,
912 .channels_max = 2,
913 .rates = SUN4I_CODEC_RATES,
914 .formats = SUN4I_CODEC_FORMATS,
915 .sig_bits = 24,
916 },
Emilio López45fb6b62015-09-12 15:26:24 +0200917};
918
Emilio López45fb6b62015-09-12 15:26:24 +0200919static struct snd_soc_dai_link *sun4i_codec_create_link(struct device *dev,
920 int *num_links)
921{
922 struct snd_soc_dai_link *link = devm_kzalloc(dev, sizeof(*link),
923 GFP_KERNEL);
924 if (!link)
925 return NULL;
926
927 link->name = "cdc";
928 link->stream_name = "CDC PCM";
929 link->codec_dai_name = "Codec";
930 link->cpu_dai_name = dev_name(dev);
931 link->codec_name = dev_name(dev);
932 link->platform_name = dev_name(dev);
933 link->dai_fmt = SND_SOC_DAIFMT_I2S;
934
935 *num_links = 1;
936
937 return link;
938};
939
Hans de Goede40592622015-12-11 19:43:57 +0100940static int sun4i_codec_spk_event(struct snd_soc_dapm_widget *w,
941 struct snd_kcontrol *k, int event)
942{
943 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(w->dapm->card);
944
945 if (scodec->gpio_pa)
946 gpiod_set_value_cansleep(scodec->gpio_pa,
947 !!SND_SOC_DAPM_EVENT_ON(event));
948
949 return 0;
950}
951
952static const struct snd_soc_dapm_widget sun4i_codec_card_dapm_widgets[] = {
953 SND_SOC_DAPM_SPK("Speaker", sun4i_codec_spk_event),
954};
955
956static const struct snd_soc_dapm_route sun4i_codec_card_dapm_routes[] = {
Hans de Goede6b803c62015-12-22 23:00:17 +0100957 { "Speaker", NULL, "HP Right" },
958 { "Speaker", NULL, "HP Left" },
Hans de Goede40592622015-12-11 19:43:57 +0100959};
960
Emilio López45fb6b62015-09-12 15:26:24 +0200961static struct snd_soc_card *sun4i_codec_create_card(struct device *dev)
962{
963 struct snd_soc_card *card;
Emilio López45fb6b62015-09-12 15:26:24 +0200964
965 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
966 if (!card)
Chen-Yu Tsai85915b62016-10-31 14:42:09 +0800967 return ERR_PTR(-ENOMEM);
Emilio López45fb6b62015-09-12 15:26:24 +0200968
969 card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
970 if (!card->dai_link)
Chen-Yu Tsai85915b62016-10-31 14:42:09 +0800971 return ERR_PTR(-ENOMEM);
Emilio López45fb6b62015-09-12 15:26:24 +0200972
973 card->dev = dev;
974 card->name = "sun4i-codec";
Hans de Goede40592622015-12-11 19:43:57 +0100975 card->dapm_widgets = sun4i_codec_card_dapm_widgets;
976 card->num_dapm_widgets = ARRAY_SIZE(sun4i_codec_card_dapm_widgets);
977 card->dapm_routes = sun4i_codec_card_dapm_routes;
978 card->num_dapm_routes = ARRAY_SIZE(sun4i_codec_card_dapm_routes);
Emilio López45fb6b62015-09-12 15:26:24 +0200979
Emilio López45fb6b62015-09-12 15:26:24 +0200980 return card;
981};
982
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +0800983static struct snd_soc_card *sun6i_codec_create_card(struct device *dev)
984{
985 struct snd_soc_card *card;
986
987 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
988 if (!card)
989 return ERR_PTR(-ENOMEM);
990
991 card->dai_link = sun4i_codec_create_link(dev, &card->num_links);
992 if (!card->dai_link)
993 return ERR_PTR(-ENOMEM);
994
995 card->dev = dev;
996 card->name = "A31 Audio Codec";
997
998 return card;
999};
1000
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001001static const struct regmap_config sun4i_codec_regmap_config = {
1002 .reg_bits = 32,
1003 .reg_stride = 4,
1004 .val_bits = 32,
1005 .max_register = SUN4I_CODEC_ADC_RXCNT,
1006};
1007
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001008static const struct regmap_config sun6i_codec_regmap_config = {
1009 .reg_bits = 32,
1010 .reg_stride = 4,
1011 .val_bits = 32,
1012 .max_register = SUN6I_CODEC_HMIC_DATA,
1013};
1014
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001015static const struct regmap_config sun7i_codec_regmap_config = {
1016 .reg_bits = 32,
1017 .reg_stride = 4,
1018 .val_bits = 32,
1019 .max_register = SUN7I_CODEC_AC_MIC_PHONE_CAL,
1020};
1021
1022struct sun4i_codec_quirks {
1023 const struct regmap_config *regmap_config;
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001024 const struct snd_soc_codec_driver *codec;
1025 struct snd_soc_card * (*create_card)(struct device *dev);
1026 struct reg_field reg_adc_fifoc; /* used for regmap_field */
1027 unsigned int reg_dac_txdata; /* TX FIFO offset for DMA config */
1028 unsigned int reg_adc_rxdata; /* RX FIFO offset for DMA config */
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001029};
1030
1031static const struct sun4i_codec_quirks sun4i_codec_quirks = {
1032 .regmap_config = &sun4i_codec_regmap_config,
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001033 .codec = &sun4i_codec_codec,
1034 .create_card = sun4i_codec_create_card,
1035 .reg_adc_fifoc = REG_FIELD(SUN4I_CODEC_ADC_FIFOC, 0, 31),
1036 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
1037 .reg_adc_rxdata = SUN4I_CODEC_ADC_RXDATA,
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001038};
1039
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001040static const struct sun4i_codec_quirks sun6i_a31_codec_quirks = {
1041 .regmap_config = &sun6i_codec_regmap_config,
1042 .codec = &sun6i_codec_codec,
1043 .create_card = sun6i_codec_create_card,
1044 .reg_adc_fifoc = REG_FIELD(SUN6I_CODEC_ADC_FIFOC, 0, 31),
1045 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
1046 .reg_adc_rxdata = SUN6I_CODEC_ADC_RXDATA,
1047 .has_reset = true,
1048};
1049
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001050static const struct sun4i_codec_quirks sun7i_codec_quirks = {
1051 .regmap_config = &sun7i_codec_regmap_config,
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001052 .codec = &sun4i_codec_codec,
1053 .create_card = sun4i_codec_create_card,
1054 .reg_adc_fifoc = REG_FIELD(SUN4I_CODEC_ADC_FIFOC, 0, 31),
1055 .reg_dac_txdata = SUN4I_CODEC_DAC_TXDATA,
1056 .reg_adc_rxdata = SUN4I_CODEC_ADC_RXDATA,
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001057};
1058
1059static const struct of_device_id sun4i_codec_of_match[] = {
1060 {
1061 .compatible = "allwinner,sun4i-a10-codec",
1062 .data = &sun4i_codec_quirks,
1063 },
1064 {
Chen-Yu Tsai8d9e4c9e2016-11-03 15:55:48 +08001065 .compatible = "allwinner,sun6i-a31-codec",
1066 .data = &sun6i_a31_codec_quirks,
1067 },
1068 {
Chen-Yu Tsai2f2a3462016-11-03 15:55:43 +08001069 .compatible = "allwinner,sun7i-a20-codec",
1070 .data = &sun7i_codec_quirks,
1071 },
1072 {}
1073};
1074MODULE_DEVICE_TABLE(of, sun4i_codec_of_match);
1075
Emilio López45fb6b62015-09-12 15:26:24 +02001076static int sun4i_codec_probe(struct platform_device *pdev)
1077{
1078 struct snd_soc_card *card;
1079 struct sun4i_codec *scodec;
Danny Milosavljevicc1d50652016-09-22 09:13:13 +02001080 const struct sun4i_codec_quirks *quirks;
Emilio López45fb6b62015-09-12 15:26:24 +02001081 struct resource *res;
1082 void __iomem *base;
1083 int ret;
1084
1085 scodec = devm_kzalloc(&pdev->dev, sizeof(*scodec), GFP_KERNEL);
1086 if (!scodec)
1087 return -ENOMEM;
1088
1089 scodec->dev = &pdev->dev;
1090
1091 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1092 base = devm_ioremap_resource(&pdev->dev, res);
1093 if (IS_ERR(base)) {
1094 dev_err(&pdev->dev, "Failed to map the registers\n");
1095 return PTR_ERR(base);
1096 }
1097
Danny Milosavljevicc1d50652016-09-22 09:13:13 +02001098 quirks = of_device_get_match_data(&pdev->dev);
1099 if (quirks == NULL) {
1100 dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
1101 return -ENODEV;
1102 }
1103
Emilio López45fb6b62015-09-12 15:26:24 +02001104 scodec->regmap = devm_regmap_init_mmio(&pdev->dev, base,
Danny Milosavljevicc1d50652016-09-22 09:13:13 +02001105 quirks->regmap_config);
Emilio López45fb6b62015-09-12 15:26:24 +02001106 if (IS_ERR(scodec->regmap)) {
1107 dev_err(&pdev->dev, "Failed to create our regmap\n");
1108 return PTR_ERR(scodec->regmap);
1109 }
1110
1111 /* Get the clocks from the DT */
1112 scodec->clk_apb = devm_clk_get(&pdev->dev, "apb");
1113 if (IS_ERR(scodec->clk_apb)) {
1114 dev_err(&pdev->dev, "Failed to get the APB clock\n");
1115 return PTR_ERR(scodec->clk_apb);
1116 }
1117
1118 scodec->clk_module = devm_clk_get(&pdev->dev, "codec");
1119 if (IS_ERR(scodec->clk_module)) {
1120 dev_err(&pdev->dev, "Failed to get the module clock\n");
1121 return PTR_ERR(scodec->clk_module);
1122 }
1123
Hans de Goede40592622015-12-11 19:43:57 +01001124 scodec->gpio_pa = devm_gpiod_get_optional(&pdev->dev, "allwinner,pa",
1125 GPIOD_OUT_LOW);
1126 if (IS_ERR(scodec->gpio_pa)) {
1127 ret = PTR_ERR(scodec->gpio_pa);
1128 if (ret != -EPROBE_DEFER)
1129 dev_err(&pdev->dev, "Failed to get pa gpio: %d\n", ret);
1130 return ret;
1131 }
1132
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001133 /* reg_field setup */
1134 scodec->reg_adc_fifoc = devm_regmap_field_alloc(&pdev->dev,
1135 scodec->regmap,
1136 quirks->reg_adc_fifoc);
1137 if (IS_ERR(scodec->reg_adc_fifoc)) {
1138 ret = PTR_ERR(scodec->reg_adc_fifoc);
1139 dev_err(&pdev->dev, "Failed to create regmap fields: %d\n",
1140 ret);
1141 return ret;
1142 }
1143
Chen-Yu Tsai3716a892016-11-01 14:31:55 +08001144 /* Enable the bus clock */
1145 if (clk_prepare_enable(scodec->clk_apb)) {
1146 dev_err(&pdev->dev, "Failed to enable the APB clock\n");
1147 return -EINVAL;
1148 }
1149
Emilio López45fb6b62015-09-12 15:26:24 +02001150 /* DMA configuration for TX FIFO */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001151 scodec->playback_dma_data.addr = res->start + quirks->reg_dac_txdata;
Chen-Yu Tsai730e2dd2016-11-03 15:55:46 +08001152 scodec->playback_dma_data.maxburst = 8;
Emilio López45fb6b62015-09-12 15:26:24 +02001153 scodec->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1154
Maxime Ripard1fb34b42015-11-30 16:37:47 +01001155 /* DMA configuration for RX FIFO */
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001156 scodec->capture_dma_data.addr = res->start + quirks->reg_adc_rxdata;
Chen-Yu Tsai730e2dd2016-11-03 15:55:46 +08001157 scodec->capture_dma_data.maxburst = 8;
Maxime Ripard1fb34b42015-11-30 16:37:47 +01001158 scodec->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1159
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001160 ret = snd_soc_register_codec(&pdev->dev, quirks->codec,
Emilio López45fb6b62015-09-12 15:26:24 +02001161 &sun4i_codec_dai, 1);
1162 if (ret) {
1163 dev_err(&pdev->dev, "Failed to register our codec\n");
1164 goto err_clk_disable;
1165 }
1166
1167 ret = devm_snd_soc_register_component(&pdev->dev,
1168 &sun4i_codec_component,
1169 &dummy_cpu_dai, 1);
1170 if (ret) {
1171 dev_err(&pdev->dev, "Failed to register our DAI\n");
1172 goto err_unregister_codec;
1173 }
1174
1175 ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
1176 if (ret) {
1177 dev_err(&pdev->dev, "Failed to register against DMAEngine\n");
1178 goto err_unregister_codec;
1179 }
1180
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001181 card = quirks->create_card(&pdev->dev);
Chen-Yu Tsai85915b62016-10-31 14:42:09 +08001182 if (IS_ERR(card)) {
1183 ret = PTR_ERR(card);
Emilio López45fb6b62015-09-12 15:26:24 +02001184 dev_err(&pdev->dev, "Failed to create our card\n");
1185 goto err_unregister_codec;
1186 }
1187
1188 platform_set_drvdata(pdev, card);
1189 snd_soc_card_set_drvdata(card, scodec);
1190
1191 ret = snd_soc_register_card(card);
1192 if (ret) {
1193 dev_err(&pdev->dev, "Failed to register our card\n");
1194 goto err_unregister_codec;
1195 }
1196
1197 return 0;
1198
1199err_unregister_codec:
1200 snd_soc_unregister_codec(&pdev->dev);
1201err_clk_disable:
1202 clk_disable_unprepare(scodec->clk_apb);
1203 return ret;
1204}
1205
1206static int sun4i_codec_remove(struct platform_device *pdev)
1207{
1208 struct snd_soc_card *card = platform_get_drvdata(pdev);
1209 struct sun4i_codec *scodec = snd_soc_card_get_drvdata(card);
1210
1211 snd_soc_unregister_card(card);
1212 snd_soc_unregister_codec(&pdev->dev);
1213 clk_disable_unprepare(scodec->clk_apb);
1214
1215 return 0;
1216}
1217
1218static struct platform_driver sun4i_codec_driver = {
1219 .driver = {
1220 .name = "sun4i-codec",
Emilio López45fb6b62015-09-12 15:26:24 +02001221 .of_match_table = sun4i_codec_of_match,
1222 },
1223 .probe = sun4i_codec_probe,
1224 .remove = sun4i_codec_remove,
1225};
1226module_platform_driver(sun4i_codec_driver);
1227
1228MODULE_DESCRIPTION("Allwinner A10 codec driver");
1229MODULE_AUTHOR("Emilio López <emilio@elopez.com.ar>");
1230MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
1231MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
Chen-Yu Tsaibc03f0d2016-11-03 15:55:44 +08001232MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
Emilio López45fb6b62015-09-12 15:26:24 +02001233MODULE_LICENSE("GPL");