2 * AD193X Audio Codec driver supporting AD1936/7/8/9
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/device.h>
13 #include <linux/i2c.h>
14 #include <linux/spi/spi.h>
15 #include <linux/slab.h>
16 #include <sound/core.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/initval.h>
20 #include <sound/soc.h>
21 #include <sound/tlv.h>
24 /* codec private data */
26 enum snd_soc_control_type bus_type
;
31 /* ad193x register cache & default register settings */
32 static const u8 ad193x_reg
[AD193X_NUM_REGS
] = {
33 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0,
37 * AD193X volume/mute/de-emphasis etc. controls
39 static const char *ad193x_deemp
[] = {"None", "48kHz", "44.1kHz", "32kHz"};
41 static const struct soc_enum ad193x_deemp_enum
=
42 SOC_ENUM_SINGLE(AD193X_DAC_CTRL2
, 1, 4, ad193x_deemp
);
44 static const struct snd_kcontrol_new ad193x_snd_controls
[] = {
45 /* DAC volume control */
46 SOC_DOUBLE_R("DAC1 Volume", AD193X_DAC_L1_VOL
,
47 AD193X_DAC_R1_VOL
, 0, 0xFF, 1),
48 SOC_DOUBLE_R("DAC2 Volume", AD193X_DAC_L2_VOL
,
49 AD193X_DAC_R2_VOL
, 0, 0xFF, 1),
50 SOC_DOUBLE_R("DAC3 Volume", AD193X_DAC_L3_VOL
,
51 AD193X_DAC_R3_VOL
, 0, 0xFF, 1),
52 SOC_DOUBLE_R("DAC4 Volume", AD193X_DAC_L4_VOL
,
53 AD193X_DAC_R4_VOL
, 0, 0xFF, 1),
55 /* ADC switch control */
56 SOC_DOUBLE("ADC1 Switch", AD193X_ADC_CTRL0
, AD193X_ADCL1_MUTE
,
57 AD193X_ADCR1_MUTE
, 1, 1),
58 SOC_DOUBLE("ADC2 Switch", AD193X_ADC_CTRL0
, AD193X_ADCL2_MUTE
,
59 AD193X_ADCR2_MUTE
, 1, 1),
61 /* DAC switch control */
62 SOC_DOUBLE("DAC1 Switch", AD193X_DAC_CHNL_MUTE
, AD193X_DACL1_MUTE
,
63 AD193X_DACR1_MUTE
, 1, 1),
64 SOC_DOUBLE("DAC2 Switch", AD193X_DAC_CHNL_MUTE
, AD193X_DACL2_MUTE
,
65 AD193X_DACR2_MUTE
, 1, 1),
66 SOC_DOUBLE("DAC3 Switch", AD193X_DAC_CHNL_MUTE
, AD193X_DACL3_MUTE
,
67 AD193X_DACR3_MUTE
, 1, 1),
68 SOC_DOUBLE("DAC4 Switch", AD193X_DAC_CHNL_MUTE
, AD193X_DACL4_MUTE
,
69 AD193X_DACR4_MUTE
, 1, 1),
71 /* ADC high-pass filter */
72 SOC_SINGLE("ADC High Pass Filter Switch", AD193X_ADC_CTRL0
,
73 AD193X_ADC_HIGHPASS_FILTER
, 1, 0),
76 SOC_ENUM("Playback Deemphasis", ad193x_deemp_enum
),
79 static const struct snd_soc_dapm_widget ad193x_dapm_widgets
[] = {
80 SND_SOC_DAPM_DAC("DAC", "Playback", AD193X_DAC_CTRL0
, 0, 1),
81 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM
, 0, 0),
82 SND_SOC_DAPM_SUPPLY("PLL_PWR", AD193X_PLL_CLK_CTRL0
, 0, 1, NULL
, 0),
83 SND_SOC_DAPM_SUPPLY("ADC_PWR", AD193X_ADC_CTRL0
, 0, 1, NULL
, 0),
84 SND_SOC_DAPM_OUTPUT("DAC1OUT"),
85 SND_SOC_DAPM_OUTPUT("DAC2OUT"),
86 SND_SOC_DAPM_OUTPUT("DAC3OUT"),
87 SND_SOC_DAPM_OUTPUT("DAC4OUT"),
88 SND_SOC_DAPM_INPUT("ADC1IN"),
89 SND_SOC_DAPM_INPUT("ADC2IN"),
92 static const struct snd_soc_dapm_route audio_paths
[] = {
93 { "DAC", NULL
, "PLL_PWR" },
94 { "ADC", NULL
, "PLL_PWR" },
95 { "DAC", NULL
, "ADC_PWR" },
96 { "ADC", NULL
, "ADC_PWR" },
97 { "DAC1OUT", "DAC1 Switch", "DAC" },
98 { "DAC2OUT", "DAC2 Switch", "DAC" },
99 { "DAC3OUT", "DAC3 Switch", "DAC" },
100 { "DAC4OUT", "DAC4 Switch", "DAC" },
101 { "ADC", "ADC1 Switch", "ADC1IN" },
102 { "ADC", "ADC2 Switch", "ADC2IN" },
109 static int ad193x_mute(struct snd_soc_dai
*dai
, int mute
)
111 struct snd_soc_codec
*codec
= dai
->codec
;
114 reg
= snd_soc_read(codec
, AD193X_DAC_CTRL2
);
115 reg
= (mute
> 0) ? reg
| AD193X_DAC_MASTER_MUTE
: reg
&
116 (~AD193X_DAC_MASTER_MUTE
);
117 snd_soc_write(codec
, AD193X_DAC_CTRL2
, reg
);
122 static int ad193x_set_tdm_slot(struct snd_soc_dai
*dai
, unsigned int tx_mask
,
123 unsigned int rx_mask
, int slots
, int width
)
125 struct snd_soc_codec
*codec
= dai
->codec
;
126 int dac_reg
= snd_soc_read(codec
, AD193X_DAC_CTRL1
);
127 int adc_reg
= snd_soc_read(codec
, AD193X_ADC_CTRL2
);
129 dac_reg
&= ~AD193X_DAC_CHAN_MASK
;
130 adc_reg
&= ~AD193X_ADC_CHAN_MASK
;
134 dac_reg
|= AD193X_DAC_2_CHANNELS
<< AD193X_DAC_CHAN_SHFT
;
135 adc_reg
|= AD193X_ADC_2_CHANNELS
<< AD193X_ADC_CHAN_SHFT
;
138 dac_reg
|= AD193X_DAC_4_CHANNELS
<< AD193X_DAC_CHAN_SHFT
;
139 adc_reg
|= AD193X_ADC_4_CHANNELS
<< AD193X_ADC_CHAN_SHFT
;
142 dac_reg
|= AD193X_DAC_8_CHANNELS
<< AD193X_DAC_CHAN_SHFT
;
143 adc_reg
|= AD193X_ADC_8_CHANNELS
<< AD193X_ADC_CHAN_SHFT
;
146 dac_reg
|= AD193X_DAC_16_CHANNELS
<< AD193X_DAC_CHAN_SHFT
;
147 adc_reg
|= AD193X_ADC_16_CHANNELS
<< AD193X_ADC_CHAN_SHFT
;
153 snd_soc_write(codec
, AD193X_DAC_CTRL1
, dac_reg
);
154 snd_soc_write(codec
, AD193X_ADC_CTRL2
, adc_reg
);
159 static int ad193x_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
162 struct snd_soc_codec
*codec
= codec_dai
->codec
;
163 int adc_reg1
, adc_reg2
, dac_reg
;
165 adc_reg1
= snd_soc_read(codec
, AD193X_ADC_CTRL1
);
166 adc_reg2
= snd_soc_read(codec
, AD193X_ADC_CTRL2
);
167 dac_reg
= snd_soc_read(codec
, AD193X_DAC_CTRL1
);
169 /* At present, the driver only support AUX ADC mode(SND_SOC_DAIFMT_I2S
170 * with TDM) and ADC&DAC TDM mode(SND_SOC_DAIFMT_DSP_A)
172 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
173 case SND_SOC_DAIFMT_I2S
:
174 adc_reg1
&= ~AD193X_ADC_SERFMT_MASK
;
175 adc_reg1
|= AD193X_ADC_SERFMT_TDM
;
177 case SND_SOC_DAIFMT_DSP_A
:
178 adc_reg1
&= ~AD193X_ADC_SERFMT_MASK
;
179 adc_reg1
|= AD193X_ADC_SERFMT_AUX
;
185 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
186 case SND_SOC_DAIFMT_NB_NF
: /* normal bit clock + frame */
187 adc_reg2
&= ~AD193X_ADC_LEFT_HIGH
;
188 adc_reg2
&= ~AD193X_ADC_BCLK_INV
;
189 dac_reg
&= ~AD193X_DAC_LEFT_HIGH
;
190 dac_reg
&= ~AD193X_DAC_BCLK_INV
;
192 case SND_SOC_DAIFMT_NB_IF
: /* normal bclk + invert frm */
193 adc_reg2
|= AD193X_ADC_LEFT_HIGH
;
194 adc_reg2
&= ~AD193X_ADC_BCLK_INV
;
195 dac_reg
|= AD193X_DAC_LEFT_HIGH
;
196 dac_reg
&= ~AD193X_DAC_BCLK_INV
;
198 case SND_SOC_DAIFMT_IB_NF
: /* invert bclk + normal frm */
199 adc_reg2
&= ~AD193X_ADC_LEFT_HIGH
;
200 adc_reg2
|= AD193X_ADC_BCLK_INV
;
201 dac_reg
&= ~AD193X_DAC_LEFT_HIGH
;
202 dac_reg
|= AD193X_DAC_BCLK_INV
;
205 case SND_SOC_DAIFMT_IB_IF
: /* invert bclk + frm */
206 adc_reg2
|= AD193X_ADC_LEFT_HIGH
;
207 adc_reg2
|= AD193X_ADC_BCLK_INV
;
208 dac_reg
|= AD193X_DAC_LEFT_HIGH
;
209 dac_reg
|= AD193X_DAC_BCLK_INV
;
215 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
216 case SND_SOC_DAIFMT_CBM_CFM
: /* codec clk & frm master */
217 adc_reg2
|= AD193X_ADC_LCR_MASTER
;
218 adc_reg2
|= AD193X_ADC_BCLK_MASTER
;
219 dac_reg
|= AD193X_DAC_LCR_MASTER
;
220 dac_reg
|= AD193X_DAC_BCLK_MASTER
;
222 case SND_SOC_DAIFMT_CBS_CFM
: /* codec clk slave & frm master */
223 adc_reg2
|= AD193X_ADC_LCR_MASTER
;
224 adc_reg2
&= ~AD193X_ADC_BCLK_MASTER
;
225 dac_reg
|= AD193X_DAC_LCR_MASTER
;
226 dac_reg
&= ~AD193X_DAC_BCLK_MASTER
;
228 case SND_SOC_DAIFMT_CBM_CFS
: /* codec clk master & frame slave */
229 adc_reg2
&= ~AD193X_ADC_LCR_MASTER
;
230 adc_reg2
|= AD193X_ADC_BCLK_MASTER
;
231 dac_reg
&= ~AD193X_DAC_LCR_MASTER
;
232 dac_reg
|= AD193X_DAC_BCLK_MASTER
;
234 case SND_SOC_DAIFMT_CBS_CFS
: /* codec clk & frm slave */
235 adc_reg2
&= ~AD193X_ADC_LCR_MASTER
;
236 adc_reg2
&= ~AD193X_ADC_BCLK_MASTER
;
237 dac_reg
&= ~AD193X_DAC_LCR_MASTER
;
238 dac_reg
&= ~AD193X_DAC_BCLK_MASTER
;
244 snd_soc_write(codec
, AD193X_ADC_CTRL1
, adc_reg1
);
245 snd_soc_write(codec
, AD193X_ADC_CTRL2
, adc_reg2
);
246 snd_soc_write(codec
, AD193X_DAC_CTRL1
, dac_reg
);
251 static int ad193x_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
252 int clk_id
, unsigned int freq
, int dir
)
254 struct snd_soc_codec
*codec
= codec_dai
->codec
;
255 struct ad193x_priv
*ad193x
= snd_soc_codec_get_drvdata(codec
);
261 ad193x
->sysclk
= freq
;
267 static int ad193x_hw_params(struct snd_pcm_substream
*substream
,
268 struct snd_pcm_hw_params
*params
,
269 struct snd_soc_dai
*dai
)
271 int word_len
= 0, reg
= 0, master_rate
= 0;
273 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
274 struct snd_soc_codec
*codec
= rtd
->codec
;
275 struct ad193x_priv
*ad193x
= snd_soc_codec_get_drvdata(codec
);
278 switch (params_format(params
)) {
279 case SNDRV_PCM_FORMAT_S16_LE
:
282 case SNDRV_PCM_FORMAT_S20_3LE
:
285 case SNDRV_PCM_FORMAT_S24_LE
:
286 case SNDRV_PCM_FORMAT_S32_LE
:
291 switch (ad193x
->sysclk
) {
293 master_rate
= AD193X_PLL_INPUT_256
;
296 master_rate
= AD193X_PLL_INPUT_384
;
299 master_rate
= AD193X_PLL_INPUT_512
;
302 master_rate
= AD193X_PLL_INPUT_768
;
306 reg
= snd_soc_read(codec
, AD193X_PLL_CLK_CTRL0
);
307 reg
= (reg
& AD193X_PLL_INPUT_MASK
) | master_rate
;
308 snd_soc_write(codec
, AD193X_PLL_CLK_CTRL0
, reg
);
310 reg
= snd_soc_read(codec
, AD193X_DAC_CTRL2
);
311 reg
= (reg
& (~AD193X_DAC_WORD_LEN_MASK
)) | word_len
;
312 snd_soc_write(codec
, AD193X_DAC_CTRL2
, reg
);
314 reg
= snd_soc_read(codec
, AD193X_ADC_CTRL1
);
315 reg
= (reg
& (~AD193X_ADC_WORD_LEN_MASK
)) | word_len
;
316 snd_soc_write(codec
, AD193X_ADC_CTRL1
, reg
);
321 static struct snd_soc_dai_ops ad193x_dai_ops
= {
322 .hw_params
= ad193x_hw_params
,
323 .digital_mute
= ad193x_mute
,
324 .set_tdm_slot
= ad193x_set_tdm_slot
,
325 .set_sysclk
= ad193x_set_dai_sysclk
,
326 .set_fmt
= ad193x_set_dai_fmt
,
329 /* codec DAI instance */
330 static struct snd_soc_dai_driver ad193x_dai
= {
331 .name
= "ad193x-hifi",
333 .stream_name
= "Playback",
336 .rates
= SNDRV_PCM_RATE_48000
,
337 .formats
= SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S16_LE
|
338 SNDRV_PCM_FMTBIT_S20_3LE
| SNDRV_PCM_FMTBIT_S24_LE
,
341 .stream_name
= "Capture",
344 .rates
= SNDRV_PCM_RATE_48000
,
345 .formats
= SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S16_LE
|
346 SNDRV_PCM_FMTBIT_S20_3LE
| SNDRV_PCM_FMTBIT_S24_LE
,
348 .ops
= &ad193x_dai_ops
,
351 static int ad193x_probe(struct snd_soc_codec
*codec
)
353 struct ad193x_priv
*ad193x
= snd_soc_codec_get_drvdata(codec
);
354 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
357 codec
->control_data
= ad193x
->control_data
;
358 if (ad193x
->bus_type
== SND_SOC_I2C
)
359 ret
= snd_soc_codec_set_cache_io(codec
, 8, 8, ad193x
->bus_type
);
361 ret
= snd_soc_codec_set_cache_io(codec
, 16, 8, ad193x
->bus_type
);
363 dev_err(codec
->dev
, "failed to set cache I/O: %d\n",
368 /* default setting for ad193x */
370 /* unmute dac channels */
371 snd_soc_write(codec
, AD193X_DAC_CHNL_MUTE
, 0x0);
372 /* de-emphasis: 48kHz, powedown dac */
373 snd_soc_write(codec
, AD193X_DAC_CTRL2
, 0x1A);
374 /* powerdown dac, dac in tdm mode */
375 snd_soc_write(codec
, AD193X_DAC_CTRL0
, 0x41);
376 /* high-pass filter enable */
377 snd_soc_write(codec
, AD193X_ADC_CTRL0
, 0x3);
378 /* sata delay=1, adc aux mode */
379 snd_soc_write(codec
, AD193X_ADC_CTRL1
, 0x43);
380 /* pll input: mclki/xi */
381 snd_soc_write(codec
, AD193X_PLL_CLK_CTRL0
, 0x99); /* mclk=24.576Mhz: 0x9D; mclk=12.288Mhz: 0x99 */
382 snd_soc_write(codec
, AD193X_PLL_CLK_CTRL1
, 0x04);
384 snd_soc_add_controls(codec
, ad193x_snd_controls
,
385 ARRAY_SIZE(ad193x_snd_controls
));
386 snd_soc_dapm_new_controls(dapm
, ad193x_dapm_widgets
,
387 ARRAY_SIZE(ad193x_dapm_widgets
));
388 snd_soc_dapm_add_routes(dapm
, audio_paths
, ARRAY_SIZE(audio_paths
));
393 static struct snd_soc_codec_driver soc_codec_dev_ad193x
= {
394 .probe
= ad193x_probe
,
395 .reg_cache_default
= ad193x_reg
,
396 .reg_cache_size
= AD193X_NUM_REGS
,
397 .reg_word_size
= sizeof(u16
),
400 #if defined(CONFIG_SPI_MASTER)
401 static int __devinit
ad193x_spi_probe(struct spi_device
*spi
)
403 struct ad193x_priv
*ad193x
;
406 ad193x
= kzalloc(sizeof(struct ad193x_priv
), GFP_KERNEL
);
410 spi_set_drvdata(spi
, ad193x
);
411 ad193x
->control_data
= spi
;
412 ad193x
->bus_type
= SND_SOC_SPI
;
414 ret
= snd_soc_register_codec(&spi
->dev
,
415 &soc_codec_dev_ad193x
, &ad193x_dai
, 1);
421 static int __devexit
ad193x_spi_remove(struct spi_device
*spi
)
423 snd_soc_unregister_codec(&spi
->dev
);
424 kfree(spi_get_drvdata(spi
));
428 static struct spi_driver ad193x_spi_driver
= {
430 .name
= "ad193x-codec",
431 .owner
= THIS_MODULE
,
433 .probe
= ad193x_spi_probe
,
434 .remove
= __devexit_p(ad193x_spi_remove
),
438 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
439 static const struct i2c_device_id ad193x_id
[] = {
444 MODULE_DEVICE_TABLE(i2c
, ad193x_id
);
446 static int __devinit
ad193x_i2c_probe(struct i2c_client
*client
,
447 const struct i2c_device_id
*id
)
449 struct ad193x_priv
*ad193x
;
452 ad193x
= kzalloc(sizeof(struct ad193x_priv
), GFP_KERNEL
);
456 i2c_set_clientdata(client
, ad193x
);
457 ad193x
->control_data
= client
;
458 ad193x
->bus_type
= SND_SOC_I2C
;
460 ret
= snd_soc_register_codec(&client
->dev
,
461 &soc_codec_dev_ad193x
, &ad193x_dai
, 1);
467 static int __devexit
ad193x_i2c_remove(struct i2c_client
*client
)
469 snd_soc_unregister_codec(&client
->dev
);
470 kfree(i2c_get_clientdata(client
));
474 static struct i2c_driver ad193x_i2c_driver
= {
476 .name
= "ad193x-codec",
478 .probe
= ad193x_i2c_probe
,
479 .remove
= __devexit_p(ad193x_i2c_remove
),
480 .id_table
= ad193x_id
,
484 static int __init
ad193x_modinit(void)
488 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
489 ret
= i2c_add_driver(&ad193x_i2c_driver
);
491 printk(KERN_ERR
"Failed to register AD193X I2C driver: %d\n",
496 #if defined(CONFIG_SPI_MASTER)
497 ret
= spi_register_driver(&ad193x_spi_driver
);
499 printk(KERN_ERR
"Failed to register AD193X SPI driver: %d\n",
505 module_init(ad193x_modinit
);
507 static void __exit
ad193x_modexit(void)
509 #if defined(CONFIG_SPI_MASTER)
510 spi_unregister_driver(&ad193x_spi_driver
);
513 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
514 i2c_del_driver(&ad193x_i2c_driver
);
517 module_exit(ad193x_modexit
);
519 MODULE_DESCRIPTION("ASoC ad193x driver");
520 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
521 MODULE_LICENSE("GPL");