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 control_type
;
30 /* ad193x register cache & default register settings */
31 static const u8 ad193x_reg
[AD193X_NUM_REGS
] = {
32 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0,
36 * AD193X volume/mute/de-emphasis etc. controls
38 static const char *ad193x_deemp
[] = {"None", "48kHz", "44.1kHz", "32kHz"};
40 static const struct soc_enum ad193x_deemp_enum
=
41 SOC_ENUM_SINGLE(AD193X_DAC_CTRL2
, 1, 4, ad193x_deemp
);
43 static const struct snd_kcontrol_new ad193x_snd_controls
[] = {
44 /* DAC volume control */
45 SOC_DOUBLE_R("DAC1 Volume", AD193X_DAC_L1_VOL
,
46 AD193X_DAC_R1_VOL
, 0, 0xFF, 1),
47 SOC_DOUBLE_R("DAC2 Volume", AD193X_DAC_L2_VOL
,
48 AD193X_DAC_R2_VOL
, 0, 0xFF, 1),
49 SOC_DOUBLE_R("DAC3 Volume", AD193X_DAC_L3_VOL
,
50 AD193X_DAC_R3_VOL
, 0, 0xFF, 1),
51 SOC_DOUBLE_R("DAC4 Volume", AD193X_DAC_L4_VOL
,
52 AD193X_DAC_R4_VOL
, 0, 0xFF, 1),
54 /* ADC switch control */
55 SOC_DOUBLE("ADC1 Switch", AD193X_ADC_CTRL0
, AD193X_ADCL1_MUTE
,
56 AD193X_ADCR1_MUTE
, 1, 1),
57 SOC_DOUBLE("ADC2 Switch", AD193X_ADC_CTRL0
, AD193X_ADCL2_MUTE
,
58 AD193X_ADCR2_MUTE
, 1, 1),
60 /* DAC switch control */
61 SOC_DOUBLE("DAC1 Switch", AD193X_DAC_CHNL_MUTE
, AD193X_DACL1_MUTE
,
62 AD193X_DACR1_MUTE
, 1, 1),
63 SOC_DOUBLE("DAC2 Switch", AD193X_DAC_CHNL_MUTE
, AD193X_DACL2_MUTE
,
64 AD193X_DACR2_MUTE
, 1, 1),
65 SOC_DOUBLE("DAC3 Switch", AD193X_DAC_CHNL_MUTE
, AD193X_DACL3_MUTE
,
66 AD193X_DACR3_MUTE
, 1, 1),
67 SOC_DOUBLE("DAC4 Switch", AD193X_DAC_CHNL_MUTE
, AD193X_DACL4_MUTE
,
68 AD193X_DACR4_MUTE
, 1, 1),
70 /* ADC high-pass filter */
71 SOC_SINGLE("ADC High Pass Filter Switch", AD193X_ADC_CTRL0
,
72 AD193X_ADC_HIGHPASS_FILTER
, 1, 0),
75 SOC_ENUM("Playback Deemphasis", ad193x_deemp_enum
),
78 static const struct snd_soc_dapm_widget ad193x_dapm_widgets
[] = {
79 SND_SOC_DAPM_DAC("DAC", "Playback", AD193X_DAC_CTRL0
, 0, 1),
80 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM
, 0, 0),
81 SND_SOC_DAPM_SUPPLY("PLL_PWR", AD193X_PLL_CLK_CTRL0
, 0, 1, NULL
, 0),
82 SND_SOC_DAPM_SUPPLY("ADC_PWR", AD193X_ADC_CTRL0
, 0, 1, NULL
, 0),
83 SND_SOC_DAPM_OUTPUT("DAC1OUT"),
84 SND_SOC_DAPM_OUTPUT("DAC2OUT"),
85 SND_SOC_DAPM_OUTPUT("DAC3OUT"),
86 SND_SOC_DAPM_OUTPUT("DAC4OUT"),
87 SND_SOC_DAPM_INPUT("ADC1IN"),
88 SND_SOC_DAPM_INPUT("ADC2IN"),
91 static const struct snd_soc_dapm_route audio_paths
[] = {
92 { "DAC", NULL
, "PLL_PWR" },
93 { "ADC", NULL
, "PLL_PWR" },
94 { "DAC", NULL
, "ADC_PWR" },
95 { "ADC", NULL
, "ADC_PWR" },
96 { "DAC1OUT", "DAC1 Switch", "DAC" },
97 { "DAC2OUT", "DAC2 Switch", "DAC" },
98 { "DAC3OUT", "DAC3 Switch", "DAC" },
99 { "DAC4OUT", "DAC4 Switch", "DAC" },
100 { "ADC", "ADC1 Switch", "ADC1IN" },
101 { "ADC", "ADC2 Switch", "ADC2IN" },
108 static int ad193x_mute(struct snd_soc_dai
*dai
, int mute
)
110 struct snd_soc_codec
*codec
= dai
->codec
;
113 reg
= snd_soc_read(codec
, AD193X_DAC_CTRL2
);
114 reg
= (mute
> 0) ? reg
| AD193X_DAC_MASTER_MUTE
: reg
&
115 (~AD193X_DAC_MASTER_MUTE
);
116 snd_soc_write(codec
, AD193X_DAC_CTRL2
, reg
);
121 static int ad193x_set_tdm_slot(struct snd_soc_dai
*dai
, unsigned int tx_mask
,
122 unsigned int rx_mask
, int slots
, int width
)
124 struct snd_soc_codec
*codec
= dai
->codec
;
125 int dac_reg
= snd_soc_read(codec
, AD193X_DAC_CTRL1
);
126 int adc_reg
= snd_soc_read(codec
, AD193X_ADC_CTRL2
);
128 dac_reg
&= ~AD193X_DAC_CHAN_MASK
;
129 adc_reg
&= ~AD193X_ADC_CHAN_MASK
;
133 dac_reg
|= AD193X_DAC_2_CHANNELS
<< AD193X_DAC_CHAN_SHFT
;
134 adc_reg
|= AD193X_ADC_2_CHANNELS
<< AD193X_ADC_CHAN_SHFT
;
137 dac_reg
|= AD193X_DAC_4_CHANNELS
<< AD193X_DAC_CHAN_SHFT
;
138 adc_reg
|= AD193X_ADC_4_CHANNELS
<< AD193X_ADC_CHAN_SHFT
;
141 dac_reg
|= AD193X_DAC_8_CHANNELS
<< AD193X_DAC_CHAN_SHFT
;
142 adc_reg
|= AD193X_ADC_8_CHANNELS
<< AD193X_ADC_CHAN_SHFT
;
145 dac_reg
|= AD193X_DAC_16_CHANNELS
<< AD193X_DAC_CHAN_SHFT
;
146 adc_reg
|= AD193X_ADC_16_CHANNELS
<< AD193X_ADC_CHAN_SHFT
;
152 snd_soc_write(codec
, AD193X_DAC_CTRL1
, dac_reg
);
153 snd_soc_write(codec
, AD193X_ADC_CTRL2
, adc_reg
);
158 static int ad193x_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
161 struct snd_soc_codec
*codec
= codec_dai
->codec
;
162 int adc_reg1
, adc_reg2
, dac_reg
;
164 adc_reg1
= snd_soc_read(codec
, AD193X_ADC_CTRL1
);
165 adc_reg2
= snd_soc_read(codec
, AD193X_ADC_CTRL2
);
166 dac_reg
= snd_soc_read(codec
, AD193X_DAC_CTRL1
);
168 /* At present, the driver only support AUX ADC mode(SND_SOC_DAIFMT_I2S
169 * with TDM) and ADC&DAC TDM mode(SND_SOC_DAIFMT_DSP_A)
171 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
172 case SND_SOC_DAIFMT_I2S
:
173 adc_reg1
&= ~AD193X_ADC_SERFMT_MASK
;
174 adc_reg1
|= AD193X_ADC_SERFMT_TDM
;
176 case SND_SOC_DAIFMT_DSP_A
:
177 adc_reg1
&= ~AD193X_ADC_SERFMT_MASK
;
178 adc_reg1
|= AD193X_ADC_SERFMT_AUX
;
184 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
185 case SND_SOC_DAIFMT_NB_NF
: /* normal bit clock + frame */
186 adc_reg2
&= ~AD193X_ADC_LEFT_HIGH
;
187 adc_reg2
&= ~AD193X_ADC_BCLK_INV
;
188 dac_reg
&= ~AD193X_DAC_LEFT_HIGH
;
189 dac_reg
&= ~AD193X_DAC_BCLK_INV
;
191 case SND_SOC_DAIFMT_NB_IF
: /* normal bclk + invert frm */
192 adc_reg2
|= AD193X_ADC_LEFT_HIGH
;
193 adc_reg2
&= ~AD193X_ADC_BCLK_INV
;
194 dac_reg
|= AD193X_DAC_LEFT_HIGH
;
195 dac_reg
&= ~AD193X_DAC_BCLK_INV
;
197 case SND_SOC_DAIFMT_IB_NF
: /* invert bclk + normal frm */
198 adc_reg2
&= ~AD193X_ADC_LEFT_HIGH
;
199 adc_reg2
|= AD193X_ADC_BCLK_INV
;
200 dac_reg
&= ~AD193X_DAC_LEFT_HIGH
;
201 dac_reg
|= AD193X_DAC_BCLK_INV
;
204 case SND_SOC_DAIFMT_IB_IF
: /* invert bclk + frm */
205 adc_reg2
|= AD193X_ADC_LEFT_HIGH
;
206 adc_reg2
|= AD193X_ADC_BCLK_INV
;
207 dac_reg
|= AD193X_DAC_LEFT_HIGH
;
208 dac_reg
|= AD193X_DAC_BCLK_INV
;
214 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
215 case SND_SOC_DAIFMT_CBM_CFM
: /* codec clk & frm master */
216 adc_reg2
|= AD193X_ADC_LCR_MASTER
;
217 adc_reg2
|= AD193X_ADC_BCLK_MASTER
;
218 dac_reg
|= AD193X_DAC_LCR_MASTER
;
219 dac_reg
|= AD193X_DAC_BCLK_MASTER
;
221 case SND_SOC_DAIFMT_CBS_CFM
: /* codec clk slave & frm master */
222 adc_reg2
|= AD193X_ADC_LCR_MASTER
;
223 adc_reg2
&= ~AD193X_ADC_BCLK_MASTER
;
224 dac_reg
|= AD193X_DAC_LCR_MASTER
;
225 dac_reg
&= ~AD193X_DAC_BCLK_MASTER
;
227 case SND_SOC_DAIFMT_CBM_CFS
: /* codec clk master & frame slave */
228 adc_reg2
&= ~AD193X_ADC_LCR_MASTER
;
229 adc_reg2
|= AD193X_ADC_BCLK_MASTER
;
230 dac_reg
&= ~AD193X_DAC_LCR_MASTER
;
231 dac_reg
|= AD193X_DAC_BCLK_MASTER
;
233 case SND_SOC_DAIFMT_CBS_CFS
: /* codec clk & frm slave */
234 adc_reg2
&= ~AD193X_ADC_LCR_MASTER
;
235 adc_reg2
&= ~AD193X_ADC_BCLK_MASTER
;
236 dac_reg
&= ~AD193X_DAC_LCR_MASTER
;
237 dac_reg
&= ~AD193X_DAC_BCLK_MASTER
;
243 snd_soc_write(codec
, AD193X_ADC_CTRL1
, adc_reg1
);
244 snd_soc_write(codec
, AD193X_ADC_CTRL2
, adc_reg2
);
245 snd_soc_write(codec
, AD193X_DAC_CTRL1
, dac_reg
);
250 static int ad193x_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
251 int clk_id
, unsigned int freq
, int dir
)
253 struct snd_soc_codec
*codec
= codec_dai
->codec
;
254 struct ad193x_priv
*ad193x
= snd_soc_codec_get_drvdata(codec
);
260 ad193x
->sysclk
= freq
;
266 static int ad193x_hw_params(struct snd_pcm_substream
*substream
,
267 struct snd_pcm_hw_params
*params
,
268 struct snd_soc_dai
*dai
)
270 int word_len
= 0, reg
= 0, master_rate
= 0;
272 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
273 struct snd_soc_codec
*codec
= rtd
->codec
;
274 struct ad193x_priv
*ad193x
= snd_soc_codec_get_drvdata(codec
);
277 switch (params_format(params
)) {
278 case SNDRV_PCM_FORMAT_S16_LE
:
281 case SNDRV_PCM_FORMAT_S20_3LE
:
284 case SNDRV_PCM_FORMAT_S24_LE
:
285 case SNDRV_PCM_FORMAT_S32_LE
:
290 switch (ad193x
->sysclk
) {
292 master_rate
= AD193X_PLL_INPUT_256
;
295 master_rate
= AD193X_PLL_INPUT_384
;
298 master_rate
= AD193X_PLL_INPUT_512
;
301 master_rate
= AD193X_PLL_INPUT_768
;
305 reg
= snd_soc_read(codec
, AD193X_PLL_CLK_CTRL0
);
306 reg
= (reg
& AD193X_PLL_INPUT_MASK
) | master_rate
;
307 snd_soc_write(codec
, AD193X_PLL_CLK_CTRL0
, reg
);
309 reg
= snd_soc_read(codec
, AD193X_DAC_CTRL2
);
310 reg
= (reg
& (~AD193X_DAC_WORD_LEN_MASK
)) | word_len
;
311 snd_soc_write(codec
, AD193X_DAC_CTRL2
, reg
);
313 reg
= snd_soc_read(codec
, AD193X_ADC_CTRL1
);
314 reg
= (reg
& (~AD193X_ADC_WORD_LEN_MASK
)) | word_len
;
315 snd_soc_write(codec
, AD193X_ADC_CTRL1
, reg
);
320 static struct snd_soc_dai_ops ad193x_dai_ops
= {
321 .hw_params
= ad193x_hw_params
,
322 .digital_mute
= ad193x_mute
,
323 .set_tdm_slot
= ad193x_set_tdm_slot
,
324 .set_sysclk
= ad193x_set_dai_sysclk
,
325 .set_fmt
= ad193x_set_dai_fmt
,
328 /* codec DAI instance */
329 static struct snd_soc_dai_driver ad193x_dai
= {
330 .name
= "ad193x-hifi",
332 .stream_name
= "Playback",
335 .rates
= SNDRV_PCM_RATE_48000
,
336 .formats
= SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S16_LE
|
337 SNDRV_PCM_FMTBIT_S20_3LE
| SNDRV_PCM_FMTBIT_S24_LE
,
340 .stream_name
= "Capture",
343 .rates
= SNDRV_PCM_RATE_48000
,
344 .formats
= SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S16_LE
|
345 SNDRV_PCM_FMTBIT_S20_3LE
| SNDRV_PCM_FMTBIT_S24_LE
,
347 .ops
= &ad193x_dai_ops
,
350 static int ad193x_probe(struct snd_soc_codec
*codec
)
352 struct ad193x_priv
*ad193x
= snd_soc_codec_get_drvdata(codec
);
353 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
356 if (ad193x
->control_type
== SND_SOC_I2C
)
357 ret
= snd_soc_codec_set_cache_io(codec
, 8, 8, ad193x
->control_type
);
359 ret
= snd_soc_codec_set_cache_io(codec
, 16, 8, ad193x
->control_type
);
361 dev_err(codec
->dev
, "failed to set cache I/O: %d\n", ret
);
365 /* default setting for ad193x */
367 /* unmute dac channels */
368 snd_soc_write(codec
, AD193X_DAC_CHNL_MUTE
, 0x0);
369 /* de-emphasis: 48kHz, powedown dac */
370 snd_soc_write(codec
, AD193X_DAC_CTRL2
, 0x1A);
371 /* powerdown dac, dac in tdm mode */
372 snd_soc_write(codec
, AD193X_DAC_CTRL0
, 0x41);
373 /* high-pass filter enable */
374 snd_soc_write(codec
, AD193X_ADC_CTRL0
, 0x3);
375 /* sata delay=1, adc aux mode */
376 snd_soc_write(codec
, AD193X_ADC_CTRL1
, 0x43);
377 /* pll input: mclki/xi */
378 snd_soc_write(codec
, AD193X_PLL_CLK_CTRL0
, 0x99); /* mclk=24.576Mhz: 0x9D; mclk=12.288Mhz: 0x99 */
379 snd_soc_write(codec
, AD193X_PLL_CLK_CTRL1
, 0x04);
381 snd_soc_add_controls(codec
, ad193x_snd_controls
,
382 ARRAY_SIZE(ad193x_snd_controls
));
383 snd_soc_dapm_new_controls(dapm
, ad193x_dapm_widgets
,
384 ARRAY_SIZE(ad193x_dapm_widgets
));
385 snd_soc_dapm_add_routes(dapm
, audio_paths
, ARRAY_SIZE(audio_paths
));
390 static struct snd_soc_codec_driver soc_codec_dev_ad193x
= {
391 .probe
= ad193x_probe
,
392 .reg_cache_default
= ad193x_reg
,
393 .reg_cache_size
= AD193X_NUM_REGS
,
394 .reg_word_size
= sizeof(u16
),
397 #if defined(CONFIG_SPI_MASTER)
398 static int __devinit
ad193x_spi_probe(struct spi_device
*spi
)
400 struct ad193x_priv
*ad193x
;
403 ad193x
= kzalloc(sizeof(struct ad193x_priv
), GFP_KERNEL
);
407 spi_set_drvdata(spi
, ad193x
);
408 ad193x
->control_type
= SND_SOC_SPI
;
410 ret
= snd_soc_register_codec(&spi
->dev
,
411 &soc_codec_dev_ad193x
, &ad193x_dai
, 1);
417 static int __devexit
ad193x_spi_remove(struct spi_device
*spi
)
419 snd_soc_unregister_codec(&spi
->dev
);
420 kfree(spi_get_drvdata(spi
));
424 static struct spi_driver ad193x_spi_driver
= {
427 .owner
= THIS_MODULE
,
429 .probe
= ad193x_spi_probe
,
430 .remove
= __devexit_p(ad193x_spi_remove
),
434 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
435 static const struct i2c_device_id ad193x_id
[] = {
440 MODULE_DEVICE_TABLE(i2c
, ad193x_id
);
442 static int __devinit
ad193x_i2c_probe(struct i2c_client
*client
,
443 const struct i2c_device_id
*id
)
445 struct ad193x_priv
*ad193x
;
448 ad193x
= kzalloc(sizeof(struct ad193x_priv
), GFP_KERNEL
);
452 i2c_set_clientdata(client
, ad193x
);
453 ad193x
->control_type
= SND_SOC_I2C
;
455 ret
= snd_soc_register_codec(&client
->dev
,
456 &soc_codec_dev_ad193x
, &ad193x_dai
, 1);
462 static int __devexit
ad193x_i2c_remove(struct i2c_client
*client
)
464 snd_soc_unregister_codec(&client
->dev
);
465 kfree(i2c_get_clientdata(client
));
469 static struct i2c_driver ad193x_i2c_driver
= {
473 .probe
= ad193x_i2c_probe
,
474 .remove
= __devexit_p(ad193x_i2c_remove
),
475 .id_table
= ad193x_id
,
479 static int __init
ad193x_modinit(void)
483 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
484 ret
= i2c_add_driver(&ad193x_i2c_driver
);
486 printk(KERN_ERR
"Failed to register AD193X I2C driver: %d\n",
491 #if defined(CONFIG_SPI_MASTER)
492 ret
= spi_register_driver(&ad193x_spi_driver
);
494 printk(KERN_ERR
"Failed to register AD193X SPI driver: %d\n",
500 module_init(ad193x_modinit
);
502 static void __exit
ad193x_modexit(void)
504 #if defined(CONFIG_SPI_MASTER)
505 spi_unregister_driver(&ad193x_spi_driver
);
508 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
509 i2c_del_driver(&ad193x_i2c_driver
);
512 module_exit(ad193x_modexit
);
514 MODULE_DESCRIPTION("ASoC ad193x driver");
515 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
516 MODULE_LICENSE("GPL");