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/module.h>
21 #include <linux/version.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
);
177 * interface to read/write ad1836 register
179 #define AD1836_SPI_REG_SHFT 12
180 #define AD1836_SPI_READ (1 << 11)
181 #define AD1836_SPI_VAL_MSK 0x3FF
184 * write to the ad1836 register space
187 static int ad1836_write_reg(struct snd_soc_codec
*codec
, unsigned int reg
,
190 u16
*reg_cache
= codec
->reg_cache
;
193 if (value
!= reg_cache
[reg
]) {
195 struct spi_transfer t
= {
199 struct spi_message m
;
201 buf
= (reg
<< AD1836_SPI_REG_SHFT
) |
202 (value
& AD1836_SPI_VAL_MSK
);
203 spi_message_init(&m
);
204 spi_message_add_tail(&t
, &m
);
205 ret
= spi_sync(codec
->control_data
, &m
);
207 reg_cache
[reg
] = value
;
214 * read from the ad1836 register space cache
216 static unsigned int ad1836_read_reg_cache(struct snd_soc_codec
*codec
,
219 u16
*reg_cache
= codec
->reg_cache
;
221 if (reg
>= codec
->reg_cache_size
)
224 return reg_cache
[reg
];
227 static int __devinit
ad1836_spi_probe(struct spi_device
*spi
)
229 struct snd_soc_codec
*codec
;
230 struct ad1836_priv
*ad1836
;
232 ad1836
= kzalloc(sizeof(struct ad1836_priv
), GFP_KERNEL
);
236 codec
= &ad1836
->codec
;
237 codec
->control_data
= spi
;
238 codec
->dev
= &spi
->dev
;
240 dev_set_drvdata(&spi
->dev
, ad1836
);
242 return ad1836_register(ad1836
);
245 static int __devexit
ad1836_spi_remove(struct spi_device
*spi
)
247 struct ad1836_priv
*ad1836
= dev_get_drvdata(&spi
->dev
);
249 ad1836_unregister(ad1836
);
253 static struct spi_driver ad1836_spi_driver
= {
255 .name
= "ad1836-spi",
256 .bus
= &spi_bus_type
,
257 .owner
= THIS_MODULE
,
259 .probe
= ad1836_spi_probe
,
260 .remove
= __devexit_p(ad1836_spi_remove
),
263 static struct snd_soc_dai_ops ad1836_dai_ops
= {
264 .hw_params
= ad1836_hw_params
,
265 .set_fmt
= ad1836_set_dai_fmt
,
268 /* codec DAI instance */
269 struct snd_soc_dai ad1836_dai
= {
272 .stream_name
= "Playback",
275 .rates
= SNDRV_PCM_RATE_48000
,
276 .formats
= SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S16_LE
|
277 SNDRV_PCM_FMTBIT_S20_3LE
| SNDRV_PCM_FMTBIT_S24_LE
,
280 .stream_name
= "Capture",
283 .rates
= SNDRV_PCM_RATE_48000
,
284 .formats
= SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S16_LE
|
285 SNDRV_PCM_FMTBIT_S20_3LE
| SNDRV_PCM_FMTBIT_S24_LE
,
287 .ops
= &ad1836_dai_ops
,
289 EXPORT_SYMBOL_GPL(ad1836_dai
);
291 static int ad1836_register(struct ad1836_priv
*ad1836
)
294 struct snd_soc_codec
*codec
= &ad1836
->codec
;
297 dev_err(codec
->dev
, "Another ad1836 is registered\n");
301 mutex_init(&codec
->mutex
);
302 INIT_LIST_HEAD(&codec
->dapm_widgets
);
303 INIT_LIST_HEAD(&codec
->dapm_paths
);
304 codec
->private_data
= ad1836
;
305 codec
->reg_cache
= ad1836
->reg_cache
;
306 codec
->reg_cache_size
= AD1836_NUM_REGS
;
307 codec
->name
= "AD1836";
308 codec
->owner
= THIS_MODULE
;
309 codec
->dai
= &ad1836_dai
;
311 codec
->write
= ad1836_write_reg
;
312 codec
->read
= ad1836_read_reg_cache
;
313 INIT_LIST_HEAD(&codec
->dapm_widgets
);
314 INIT_LIST_HEAD(&codec
->dapm_paths
);
316 ad1836_dai
.dev
= codec
->dev
;
317 ad1836_codec
= codec
;
319 /* default setting for ad1836 */
320 /* de-emphasis: 48kHz, power-on dac */
321 codec
->write(codec
, AD1836_DAC_CTRL1
, 0x300);
322 /* unmute dac channels */
323 codec
->write(codec
, AD1836_DAC_CTRL2
, 0x0);
324 /* high-pass filter enable, power-on adc */
325 codec
->write(codec
, AD1836_ADC_CTRL1
, 0x100);
326 /* unmute adc channles, adc aux mode */
327 codec
->write(codec
, AD1836_ADC_CTRL2
, 0x180);
328 /* left/right diff:PGA/MUX */
329 codec
->write(codec
, AD1836_ADC_CTRL3
, 0x3A);
331 codec
->write(codec
, AD1836_DAC_L1_VOL
, 0x3FF);
332 codec
->write(codec
, AD1836_DAC_R1_VOL
, 0x3FF);
333 codec
->write(codec
, AD1836_DAC_L2_VOL
, 0x3FF);
334 codec
->write(codec
, AD1836_DAC_R2_VOL
, 0x3FF);
335 codec
->write(codec
, AD1836_DAC_L3_VOL
, 0x3FF);
336 codec
->write(codec
, AD1836_DAC_R3_VOL
, 0x3FF);
338 ret
= snd_soc_register_codec(codec
);
340 dev_err(codec
->dev
, "Failed to register codec: %d\n", ret
);
345 ret
= snd_soc_register_dai(&ad1836_dai
);
347 dev_err(codec
->dev
, "Failed to register DAI: %d\n", ret
);
348 snd_soc_unregister_codec(codec
);
356 static void ad1836_unregister(struct ad1836_priv
*ad1836
)
358 snd_soc_unregister_dai(&ad1836_dai
);
359 snd_soc_unregister_codec(&ad1836
->codec
);
364 static int ad1836_probe(struct platform_device
*pdev
)
366 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
367 struct snd_soc_codec
*codec
;
370 if (ad1836_codec
== NULL
) {
371 dev_err(&pdev
->dev
, "Codec device not registered\n");
375 socdev
->card
->codec
= ad1836_codec
;
376 codec
= ad1836_codec
;
379 ret
= snd_soc_new_pcms(socdev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
);
381 dev_err(codec
->dev
, "failed to create pcms: %d\n", ret
);
385 snd_soc_add_controls(codec
, ad1836_snd_controls
,
386 ARRAY_SIZE(ad1836_snd_controls
));
387 snd_soc_dapm_new_controls(codec
, ad1836_dapm_widgets
,
388 ARRAY_SIZE(ad1836_dapm_widgets
));
389 snd_soc_dapm_add_routes(codec
, audio_paths
, ARRAY_SIZE(audio_paths
));
390 snd_soc_dapm_new_widgets(codec
);
392 ret
= snd_soc_init_card(socdev
);
394 dev_err(codec
->dev
, "failed to register card: %d\n", ret
);
401 snd_soc_free_pcms(socdev
);
402 snd_soc_dapm_free(socdev
);
407 /* power down chip */
408 static int ad1836_remove(struct platform_device
*pdev
)
410 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
412 snd_soc_free_pcms(socdev
);
413 snd_soc_dapm_free(socdev
);
418 struct snd_soc_codec_device soc_codec_dev_ad1836
= {
419 .probe
= ad1836_probe
,
420 .remove
= ad1836_remove
,
422 EXPORT_SYMBOL_GPL(soc_codec_dev_ad1836
);
424 static int __init
ad1836_init(void)
428 ret
= spi_register_driver(&ad1836_spi_driver
);
430 printk(KERN_ERR
"Failed to register ad1836 SPI driver: %d\n",
436 module_init(ad1836_init
);
438 static void __exit
ad1836_exit(void)
440 spi_unregister_driver(&ad1836_spi_driver
);
442 module_exit(ad1836_exit
);
444 MODULE_DESCRIPTION("ASoC ad1836 driver");
445 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
446 MODULE_LICENSE("GPL");