2 * File: sound/soc/codecs/ad1836.c
3 * Author: Barry Song <Barry.Song@analog.com>
6 * Description: Driver for AD1836 sound chip
9 * Copyright 2009 Analog Devices Inc.
11 * Bugs: Enter bugs at http://blackfin.uclinux.org/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/device.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/initval.h>
28 #include <sound/soc.h>
29 #include <sound/tlv.h>
30 #include <sound/soc-dapm.h>
31 #include <linux/spi/spi.h>
34 /* codec private data */
36 struct snd_soc_codec codec
;
37 u16 reg_cache
[AD1836_NUM_REGS
];
40 static struct snd_soc_codec
*ad1836_codec
;
41 struct snd_soc_codec_device soc_codec_dev_ad1836
;
42 static int ad1836_register(struct ad1836_priv
*ad1836
);
43 static void ad1836_unregister(struct ad1836_priv
*ad1836
);
46 * AD1836 volume/mute/de-emphasis etc. controls
48 static const char *ad1836_deemp
[] = {"None", "44.1kHz", "32kHz", "48kHz"};
50 static const struct soc_enum ad1836_deemp_enum
=
51 SOC_ENUM_SINGLE(AD1836_DAC_CTRL1
, 8, 4, ad1836_deemp
);
53 static const struct snd_kcontrol_new ad1836_snd_controls
[] = {
54 /* DAC volume control */
55 SOC_DOUBLE_R("DAC1 Volume", AD1836_DAC_L1_VOL
,
56 AD1836_DAC_R1_VOL
, 0, 0x3FF, 0),
57 SOC_DOUBLE_R("DAC2 Volume", AD1836_DAC_L2_VOL
,
58 AD1836_DAC_R2_VOL
, 0, 0x3FF, 0),
59 SOC_DOUBLE_R("DAC3 Volume", AD1836_DAC_L3_VOL
,
60 AD1836_DAC_R3_VOL
, 0, 0x3FF, 0),
62 /* ADC switch control */
63 SOC_DOUBLE("ADC1 Switch", AD1836_ADC_CTRL2
, AD1836_ADCL1_MUTE
,
64 AD1836_ADCR1_MUTE
, 1, 1),
65 SOC_DOUBLE("ADC2 Switch", AD1836_ADC_CTRL2
, AD1836_ADCL2_MUTE
,
66 AD1836_ADCR2_MUTE
, 1, 1),
68 /* DAC switch control */
69 SOC_DOUBLE("DAC1 Switch", AD1836_DAC_CTRL2
, AD1836_DACL1_MUTE
,
70 AD1836_DACR1_MUTE
, 1, 1),
71 SOC_DOUBLE("DAC2 Switch", AD1836_DAC_CTRL2
, AD1836_DACL2_MUTE
,
72 AD1836_DACR2_MUTE
, 1, 1),
73 SOC_DOUBLE("DAC3 Switch", AD1836_DAC_CTRL2
, AD1836_DACL3_MUTE
,
74 AD1836_DACR3_MUTE
, 1, 1),
76 /* ADC high-pass filter */
77 SOC_SINGLE("ADC High Pass Filter Switch", AD1836_ADC_CTRL1
,
78 AD1836_ADC_HIGHPASS_FILTER
, 1, 0),
81 SOC_ENUM("Playback Deemphasis", ad1836_deemp_enum
),
84 static const struct snd_soc_dapm_widget ad1836_dapm_widgets
[] = {
85 SND_SOC_DAPM_DAC("DAC", "Playback", AD1836_DAC_CTRL1
,
86 AD1836_DAC_POWERDOWN
, 1),
87 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM
, 0, 0),
88 SND_SOC_DAPM_SUPPLY("ADC_PWR", AD1836_ADC_CTRL1
,
89 AD1836_ADC_POWERDOWN
, 1, NULL
, 0),
90 SND_SOC_DAPM_OUTPUT("DAC1OUT"),
91 SND_SOC_DAPM_OUTPUT("DAC2OUT"),
92 SND_SOC_DAPM_OUTPUT("DAC3OUT"),
93 SND_SOC_DAPM_INPUT("ADC1IN"),
94 SND_SOC_DAPM_INPUT("ADC2IN"),
97 static const struct snd_soc_dapm_route audio_paths
[] = {
98 { "DAC", NULL
, "ADC_PWR" },
99 { "ADC", NULL
, "ADC_PWR" },
100 { "DAC1OUT", "DAC1 Switch", "DAC" },
101 { "DAC2OUT", "DAC2 Switch", "DAC" },
102 { "DAC3OUT", "DAC3 Switch", "DAC" },
103 { "ADC", "ADC1 Switch", "ADC1IN" },
104 { "ADC", "ADC2 Switch", "ADC2IN" },
111 static int ad1836_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
114 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
115 /* at present, we support adc aux mode to interface with
116 * blackfin sport tdm mode
118 case SND_SOC_DAIFMT_DSP_A
:
124 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
125 case SND_SOC_DAIFMT_IB_IF
:
131 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
132 /* ALCLK,ABCLK are both output, AD1836 can only be master */
133 case SND_SOC_DAIFMT_CBM_CFM
:
142 static int ad1836_hw_params(struct snd_pcm_substream
*substream
,
143 struct snd_pcm_hw_params
*params
,
144 struct snd_soc_dai
*dai
)
148 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
149 struct snd_soc_device
*socdev
= rtd
->socdev
;
150 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
153 switch (params_format(params
)) {
154 case SNDRV_PCM_FORMAT_S16_LE
:
157 case SNDRV_PCM_FORMAT_S20_3LE
:
160 case SNDRV_PCM_FORMAT_S24_LE
:
161 case SNDRV_PCM_FORMAT_S32_LE
:
166 snd_soc_update_bits(codec
, AD1836_DAC_CTRL1
,
167 AD1836_DAC_WORD_LEN_MASK
, word_len
);
169 snd_soc_update_bits(codec
, AD1836_ADC_CTRL2
,
170 AD1836_ADC_WORD_LEN_MASK
, word_len
);
176 static int ad1836_soc_suspend(struct platform_device
*pdev
,
179 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
180 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
182 /* reset clock control mode */
183 u16 adc_ctrl2
= snd_soc_read(codec
, AD1836_ADC_CTRL2
);
184 adc_ctrl2
&= ~AD1836_ADC_SERFMT_MASK
;
186 return snd_soc_write(codec
, AD1836_ADC_CTRL2
, adc_ctrl2
);
189 static int ad1836_soc_resume(struct platform_device
*pdev
)
191 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
192 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
194 /* restore clock control mode */
195 u16 adc_ctrl2
= snd_soc_read(codec
, AD1836_ADC_CTRL2
);
196 adc_ctrl2
|= AD1836_ADC_AUX
;
198 return snd_soc_write(codec
, AD1836_ADC_CTRL2
, adc_ctrl2
);
201 #define ad1836_soc_suspend NULL
202 #define ad1836_soc_resume NULL
205 static int __devinit
ad1836_spi_probe(struct spi_device
*spi
)
207 struct snd_soc_codec
*codec
;
208 struct ad1836_priv
*ad1836
;
210 ad1836
= kzalloc(sizeof(struct ad1836_priv
), GFP_KERNEL
);
214 codec
= &ad1836
->codec
;
215 codec
->control_data
= spi
;
216 codec
->dev
= &spi
->dev
;
218 dev_set_drvdata(&spi
->dev
, ad1836
);
220 return ad1836_register(ad1836
);
223 static int __devexit
ad1836_spi_remove(struct spi_device
*spi
)
225 struct ad1836_priv
*ad1836
= dev_get_drvdata(&spi
->dev
);
227 ad1836_unregister(ad1836
);
231 static struct spi_driver ad1836_spi_driver
= {
234 .owner
= THIS_MODULE
,
236 .probe
= ad1836_spi_probe
,
237 .remove
= __devexit_p(ad1836_spi_remove
),
240 static struct snd_soc_dai_ops ad1836_dai_ops
= {
241 .hw_params
= ad1836_hw_params
,
242 .set_fmt
= ad1836_set_dai_fmt
,
245 /* codec DAI instance */
246 struct snd_soc_dai ad1836_dai
= {
249 .stream_name
= "Playback",
252 .rates
= SNDRV_PCM_RATE_48000
,
253 .formats
= SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S16_LE
|
254 SNDRV_PCM_FMTBIT_S20_3LE
| SNDRV_PCM_FMTBIT_S24_LE
,
257 .stream_name
= "Capture",
260 .rates
= SNDRV_PCM_RATE_48000
,
261 .formats
= SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S16_LE
|
262 SNDRV_PCM_FMTBIT_S20_3LE
| SNDRV_PCM_FMTBIT_S24_LE
,
264 .ops
= &ad1836_dai_ops
,
266 EXPORT_SYMBOL_GPL(ad1836_dai
);
268 static int ad1836_register(struct ad1836_priv
*ad1836
)
271 struct snd_soc_codec
*codec
= &ad1836
->codec
;
274 dev_err(codec
->dev
, "Another ad1836 is registered\n");
278 mutex_init(&codec
->mutex
);
279 INIT_LIST_HEAD(&codec
->dapm_widgets
);
280 INIT_LIST_HEAD(&codec
->dapm_paths
);
281 snd_soc_codec_set_drvdata(codec
, ad1836
);
282 codec
->reg_cache
= ad1836
->reg_cache
;
283 codec
->reg_cache_size
= AD1836_NUM_REGS
;
284 codec
->name
= "AD1836";
285 codec
->owner
= THIS_MODULE
;
286 codec
->dai
= &ad1836_dai
;
288 INIT_LIST_HEAD(&codec
->dapm_widgets
);
289 INIT_LIST_HEAD(&codec
->dapm_paths
);
291 ad1836_dai
.dev
= codec
->dev
;
292 ad1836_codec
= codec
;
294 ret
= snd_soc_codec_set_cache_io(codec
, 4, 12, SND_SOC_SPI
);
296 dev_err(codec
->dev
, "failed to set cache I/O: %d\n",
302 /* default setting for ad1836 */
303 /* de-emphasis: 48kHz, power-on dac */
304 snd_soc_write(codec
, AD1836_DAC_CTRL1
, 0x300);
305 /* unmute dac channels */
306 snd_soc_write(codec
, AD1836_DAC_CTRL2
, 0x0);
307 /* high-pass filter enable, power-on adc */
308 snd_soc_write(codec
, AD1836_ADC_CTRL1
, 0x100);
309 /* unmute adc channles, adc aux mode */
310 snd_soc_write(codec
, AD1836_ADC_CTRL2
, 0x180);
311 /* left/right diff:PGA/MUX */
312 snd_soc_write(codec
, AD1836_ADC_CTRL3
, 0x3A);
314 snd_soc_write(codec
, AD1836_DAC_L1_VOL
, 0x3FF);
315 snd_soc_write(codec
, AD1836_DAC_R1_VOL
, 0x3FF);
316 snd_soc_write(codec
, AD1836_DAC_L2_VOL
, 0x3FF);
317 snd_soc_write(codec
, AD1836_DAC_R2_VOL
, 0x3FF);
318 snd_soc_write(codec
, AD1836_DAC_L3_VOL
, 0x3FF);
319 snd_soc_write(codec
, AD1836_DAC_R3_VOL
, 0x3FF);
321 ret
= snd_soc_register_codec(codec
);
323 dev_err(codec
->dev
, "Failed to register codec: %d\n", ret
);
328 ret
= snd_soc_register_dai(&ad1836_dai
);
330 dev_err(codec
->dev
, "Failed to register DAI: %d\n", ret
);
331 snd_soc_unregister_codec(codec
);
339 static void ad1836_unregister(struct ad1836_priv
*ad1836
)
341 snd_soc_unregister_dai(&ad1836_dai
);
342 snd_soc_unregister_codec(&ad1836
->codec
);
347 static int ad1836_probe(struct platform_device
*pdev
)
349 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
350 struct snd_soc_codec
*codec
;
353 if (ad1836_codec
== NULL
) {
354 dev_err(&pdev
->dev
, "Codec device not registered\n");
358 socdev
->card
->codec
= ad1836_codec
;
359 codec
= ad1836_codec
;
362 ret
= snd_soc_new_pcms(socdev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
);
364 dev_err(codec
->dev
, "failed to create pcms: %d\n", ret
);
368 snd_soc_add_controls(codec
, ad1836_snd_controls
,
369 ARRAY_SIZE(ad1836_snd_controls
));
370 snd_soc_dapm_new_controls(codec
, ad1836_dapm_widgets
,
371 ARRAY_SIZE(ad1836_dapm_widgets
));
372 snd_soc_dapm_add_routes(codec
, audio_paths
, ARRAY_SIZE(audio_paths
));
378 /* power down chip */
379 static int ad1836_remove(struct platform_device
*pdev
)
381 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
383 snd_soc_free_pcms(socdev
);
384 snd_soc_dapm_free(socdev
);
389 struct snd_soc_codec_device soc_codec_dev_ad1836
= {
390 .probe
= ad1836_probe
,
391 .remove
= ad1836_remove
,
392 .suspend
= ad1836_soc_suspend
,
393 .resume
= ad1836_soc_resume
,
395 EXPORT_SYMBOL_GPL(soc_codec_dev_ad1836
);
397 static int __init
ad1836_init(void)
401 ret
= spi_register_driver(&ad1836_spi_driver
);
403 printk(KERN_ERR
"Failed to register ad1836 SPI driver: %d\n",
409 module_init(ad1836_init
);
411 static void __exit
ad1836_exit(void)
413 spi_unregister_driver(&ad1836_spi_driver
);
415 module_exit(ad1836_exit
);
417 MODULE_DESCRIPTION("ASoC ad1836 driver");
418 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
419 MODULE_LICENSE("GPL");