Staging/vt66*: kconfig, depends on WLAN
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / soc / codecs / ad1836.c
blob2c18e3d1b71e3aa993719f32c6a911ef9295d4f4
1 /*
2 * File: sound/soc/codecs/ad1836.c
3 * Author: Barry Song <Barry.Song@analog.com>
5 * Created: Aug 04 2009
6 * Description: Driver for AD1836 sound chip
8 * Modified:
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/kernel.h>
22 #include <linux/device.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/initval.h>
27 #include <sound/soc.h>
28 #include <sound/tlv.h>
29 #include <sound/soc-dapm.h>
30 #include <linux/spi/spi.h>
31 #include "ad1836.h"
33 /* codec private data */
34 struct ad1836_priv {
35 struct snd_soc_codec codec;
36 u16 reg_cache[AD1836_NUM_REGS];
39 static struct snd_soc_codec *ad1836_codec;
40 struct snd_soc_codec_device soc_codec_dev_ad1836;
41 static int ad1836_register(struct ad1836_priv *ad1836);
42 static void ad1836_unregister(struct ad1836_priv *ad1836);
45 * AD1836 volume/mute/de-emphasis etc. controls
47 static const char *ad1836_deemp[] = {"None", "44.1kHz", "32kHz", "48kHz"};
49 static const struct soc_enum ad1836_deemp_enum =
50 SOC_ENUM_SINGLE(AD1836_DAC_CTRL1, 8, 4, ad1836_deemp);
52 static const struct snd_kcontrol_new ad1836_snd_controls[] = {
53 /* DAC volume control */
54 SOC_DOUBLE_R("DAC1 Volume", AD1836_DAC_L1_VOL,
55 AD1836_DAC_R1_VOL, 0, 0x3FF, 0),
56 SOC_DOUBLE_R("DAC2 Volume", AD1836_DAC_L2_VOL,
57 AD1836_DAC_R2_VOL, 0, 0x3FF, 0),
58 SOC_DOUBLE_R("DAC3 Volume", AD1836_DAC_L3_VOL,
59 AD1836_DAC_R3_VOL, 0, 0x3FF, 0),
61 /* ADC switch control */
62 SOC_DOUBLE("ADC1 Switch", AD1836_ADC_CTRL2, AD1836_ADCL1_MUTE,
63 AD1836_ADCR1_MUTE, 1, 1),
64 SOC_DOUBLE("ADC2 Switch", AD1836_ADC_CTRL2, AD1836_ADCL2_MUTE,
65 AD1836_ADCR2_MUTE, 1, 1),
67 /* DAC switch control */
68 SOC_DOUBLE("DAC1 Switch", AD1836_DAC_CTRL2, AD1836_DACL1_MUTE,
69 AD1836_DACR1_MUTE, 1, 1),
70 SOC_DOUBLE("DAC2 Switch", AD1836_DAC_CTRL2, AD1836_DACL2_MUTE,
71 AD1836_DACR2_MUTE, 1, 1),
72 SOC_DOUBLE("DAC3 Switch", AD1836_DAC_CTRL2, AD1836_DACL3_MUTE,
73 AD1836_DACR3_MUTE, 1, 1),
75 /* ADC high-pass filter */
76 SOC_SINGLE("ADC High Pass Filter Switch", AD1836_ADC_CTRL1,
77 AD1836_ADC_HIGHPASS_FILTER, 1, 0),
79 /* DAC de-emphasis */
80 SOC_ENUM("Playback Deemphasis", ad1836_deemp_enum),
83 static const struct snd_soc_dapm_widget ad1836_dapm_widgets[] = {
84 SND_SOC_DAPM_DAC("DAC", "Playback", AD1836_DAC_CTRL1,
85 AD1836_DAC_POWERDOWN, 1),
86 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
87 SND_SOC_DAPM_SUPPLY("ADC_PWR", AD1836_ADC_CTRL1,
88 AD1836_ADC_POWERDOWN, 1, NULL, 0),
89 SND_SOC_DAPM_OUTPUT("DAC1OUT"),
90 SND_SOC_DAPM_OUTPUT("DAC2OUT"),
91 SND_SOC_DAPM_OUTPUT("DAC3OUT"),
92 SND_SOC_DAPM_INPUT("ADC1IN"),
93 SND_SOC_DAPM_INPUT("ADC2IN"),
96 static const struct snd_soc_dapm_route audio_paths[] = {
97 { "DAC", NULL, "ADC_PWR" },
98 { "ADC", NULL, "ADC_PWR" },
99 { "DAC1OUT", "DAC1 Switch", "DAC" },
100 { "DAC2OUT", "DAC2 Switch", "DAC" },
101 { "DAC3OUT", "DAC3 Switch", "DAC" },
102 { "ADC", "ADC1 Switch", "ADC1IN" },
103 { "ADC", "ADC2 Switch", "ADC2IN" },
107 * DAI ops entries
110 static int ad1836_set_dai_fmt(struct snd_soc_dai *codec_dai,
111 unsigned int fmt)
113 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
114 /* at present, we support adc aux mode to interface with
115 * blackfin sport tdm mode
117 case SND_SOC_DAIFMT_DSP_A:
118 break;
119 default:
120 return -EINVAL;
123 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
124 case SND_SOC_DAIFMT_IB_IF:
125 break;
126 default:
127 return -EINVAL;
130 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
131 /* ALCLK,ABCLK are both output, AD1836 can only be master */
132 case SND_SOC_DAIFMT_CBM_CFM:
133 break;
134 default:
135 return -EINVAL;
138 return 0;
141 static int ad1836_hw_params(struct snd_pcm_substream *substream,
142 struct snd_pcm_hw_params *params,
143 struct snd_soc_dai *dai)
145 int word_len = 0;
147 struct snd_soc_pcm_runtime *rtd = substream->private_data;
148 struct snd_soc_device *socdev = rtd->socdev;
149 struct snd_soc_codec *codec = socdev->card->codec;
151 /* bit size */
152 switch (params_format(params)) {
153 case SNDRV_PCM_FORMAT_S16_LE:
154 word_len = 3;
155 break;
156 case SNDRV_PCM_FORMAT_S20_3LE:
157 word_len = 1;
158 break;
159 case SNDRV_PCM_FORMAT_S24_LE:
160 case SNDRV_PCM_FORMAT_S32_LE:
161 word_len = 0;
162 break;
165 snd_soc_update_bits(codec, AD1836_DAC_CTRL1,
166 AD1836_DAC_WORD_LEN_MASK, word_len);
168 snd_soc_update_bits(codec, AD1836_ADC_CTRL2,
169 AD1836_ADC_WORD_LEN_MASK, word_len);
171 return 0;
176 * interface to read/write ad1836 register
178 #define AD1836_SPI_REG_SHFT 12
179 #define AD1836_SPI_READ (1 << 11)
180 #define AD1836_SPI_VAL_MSK 0x3FF
183 * write to the ad1836 register space
186 static int ad1836_write_reg(struct snd_soc_codec *codec, unsigned int reg,
187 unsigned int value)
189 u16 *reg_cache = codec->reg_cache;
190 int ret = 0;
192 if (value != reg_cache[reg]) {
193 unsigned short buf;
194 struct spi_transfer t = {
195 .tx_buf = &buf,
196 .len = 2,
198 struct spi_message m;
200 buf = (reg << AD1836_SPI_REG_SHFT) |
201 (value & AD1836_SPI_VAL_MSK);
202 spi_message_init(&m);
203 spi_message_add_tail(&t, &m);
204 ret = spi_sync(codec->control_data, &m);
205 if (ret == 0)
206 reg_cache[reg] = value;
209 return ret;
213 * read from the ad1836 register space cache
215 static unsigned int ad1836_read_reg_cache(struct snd_soc_codec *codec,
216 unsigned int reg)
218 u16 *reg_cache = codec->reg_cache;
220 if (reg >= codec->reg_cache_size)
221 return -EINVAL;
223 return reg_cache[reg];
226 static int __devinit ad1836_spi_probe(struct spi_device *spi)
228 struct snd_soc_codec *codec;
229 struct ad1836_priv *ad1836;
231 ad1836 = kzalloc(sizeof(struct ad1836_priv), GFP_KERNEL);
232 if (ad1836 == NULL)
233 return -ENOMEM;
235 codec = &ad1836->codec;
236 codec->control_data = spi;
237 codec->dev = &spi->dev;
239 dev_set_drvdata(&spi->dev, ad1836);
241 return ad1836_register(ad1836);
244 static int __devexit ad1836_spi_remove(struct spi_device *spi)
246 struct ad1836_priv *ad1836 = dev_get_drvdata(&spi->dev);
248 ad1836_unregister(ad1836);
249 return 0;
252 static struct spi_driver ad1836_spi_driver = {
253 .driver = {
254 .name = "ad1836",
255 .owner = THIS_MODULE,
257 .probe = ad1836_spi_probe,
258 .remove = __devexit_p(ad1836_spi_remove),
261 static struct snd_soc_dai_ops ad1836_dai_ops = {
262 .hw_params = ad1836_hw_params,
263 .set_fmt = ad1836_set_dai_fmt,
266 /* codec DAI instance */
267 struct snd_soc_dai ad1836_dai = {
268 .name = "AD1836",
269 .playback = {
270 .stream_name = "Playback",
271 .channels_min = 2,
272 .channels_max = 6,
273 .rates = SNDRV_PCM_RATE_48000,
274 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
275 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
277 .capture = {
278 .stream_name = "Capture",
279 .channels_min = 2,
280 .channels_max = 4,
281 .rates = SNDRV_PCM_RATE_48000,
282 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
283 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
285 .ops = &ad1836_dai_ops,
287 EXPORT_SYMBOL_GPL(ad1836_dai);
289 static int ad1836_register(struct ad1836_priv *ad1836)
291 int ret;
292 struct snd_soc_codec *codec = &ad1836->codec;
294 if (ad1836_codec) {
295 dev_err(codec->dev, "Another ad1836 is registered\n");
296 return -EINVAL;
299 mutex_init(&codec->mutex);
300 INIT_LIST_HEAD(&codec->dapm_widgets);
301 INIT_LIST_HEAD(&codec->dapm_paths);
302 codec->private_data = ad1836;
303 codec->reg_cache = ad1836->reg_cache;
304 codec->reg_cache_size = AD1836_NUM_REGS;
305 codec->name = "AD1836";
306 codec->owner = THIS_MODULE;
307 codec->dai = &ad1836_dai;
308 codec->num_dai = 1;
309 codec->write = ad1836_write_reg;
310 codec->read = ad1836_read_reg_cache;
311 INIT_LIST_HEAD(&codec->dapm_widgets);
312 INIT_LIST_HEAD(&codec->dapm_paths);
314 ad1836_dai.dev = codec->dev;
315 ad1836_codec = codec;
317 /* default setting for ad1836 */
318 /* de-emphasis: 48kHz, power-on dac */
319 codec->write(codec, AD1836_DAC_CTRL1, 0x300);
320 /* unmute dac channels */
321 codec->write(codec, AD1836_DAC_CTRL2, 0x0);
322 /* high-pass filter enable, power-on adc */
323 codec->write(codec, AD1836_ADC_CTRL1, 0x100);
324 /* unmute adc channles, adc aux mode */
325 codec->write(codec, AD1836_ADC_CTRL2, 0x180);
326 /* left/right diff:PGA/MUX */
327 codec->write(codec, AD1836_ADC_CTRL3, 0x3A);
328 /* volume */
329 codec->write(codec, AD1836_DAC_L1_VOL, 0x3FF);
330 codec->write(codec, AD1836_DAC_R1_VOL, 0x3FF);
331 codec->write(codec, AD1836_DAC_L2_VOL, 0x3FF);
332 codec->write(codec, AD1836_DAC_R2_VOL, 0x3FF);
333 codec->write(codec, AD1836_DAC_L3_VOL, 0x3FF);
334 codec->write(codec, AD1836_DAC_R3_VOL, 0x3FF);
336 ret = snd_soc_register_codec(codec);
337 if (ret != 0) {
338 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
339 kfree(ad1836);
340 return ret;
343 ret = snd_soc_register_dai(&ad1836_dai);
344 if (ret != 0) {
345 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
346 snd_soc_unregister_codec(codec);
347 kfree(ad1836);
348 return ret;
351 return 0;
354 static void ad1836_unregister(struct ad1836_priv *ad1836)
356 snd_soc_unregister_dai(&ad1836_dai);
357 snd_soc_unregister_codec(&ad1836->codec);
358 kfree(ad1836);
359 ad1836_codec = NULL;
362 static int ad1836_probe(struct platform_device *pdev)
364 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
365 struct snd_soc_codec *codec;
366 int ret = 0;
368 if (ad1836_codec == NULL) {
369 dev_err(&pdev->dev, "Codec device not registered\n");
370 return -ENODEV;
373 socdev->card->codec = ad1836_codec;
374 codec = ad1836_codec;
376 /* register pcms */
377 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
378 if (ret < 0) {
379 dev_err(codec->dev, "failed to create pcms: %d\n", ret);
380 goto pcm_err;
383 snd_soc_add_controls(codec, ad1836_snd_controls,
384 ARRAY_SIZE(ad1836_snd_controls));
385 snd_soc_dapm_new_controls(codec, ad1836_dapm_widgets,
386 ARRAY_SIZE(ad1836_dapm_widgets));
387 snd_soc_dapm_add_routes(codec, audio_paths, ARRAY_SIZE(audio_paths));
389 pcm_err:
390 return ret;
393 /* power down chip */
394 static int ad1836_remove(struct platform_device *pdev)
396 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
398 snd_soc_free_pcms(socdev);
399 snd_soc_dapm_free(socdev);
401 return 0;
404 struct snd_soc_codec_device soc_codec_dev_ad1836 = {
405 .probe = ad1836_probe,
406 .remove = ad1836_remove,
408 EXPORT_SYMBOL_GPL(soc_codec_dev_ad1836);
410 static int __init ad1836_init(void)
412 int ret;
414 ret = spi_register_driver(&ad1836_spi_driver);
415 if (ret != 0) {
416 printk(KERN_ERR "Failed to register ad1836 SPI driver: %d\n",
417 ret);
420 return ret;
422 module_init(ad1836_init);
424 static void __exit ad1836_exit(void)
426 spi_unregister_driver(&ad1836_spi_driver);
428 module_exit(ad1836_exit);
430 MODULE_DESCRIPTION("ASoC ad1836 driver");
431 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
432 MODULE_LICENSE("GPL");