blob: c48485f2c55db3632f2ae18f412a7ab341b13316 [file] [log] [blame]
Barry Song7eaae412009-08-13 11:59:32 +08001/*
2 * File: sound/soc/codecs/ad1836.c
3 * Author: Barry Song <Barry.Song@analog.com>
4 *
5 * Created: Aug 04 2009
6 * Description: Driver for AD1836 sound chip
7 *
8 * Modified:
9 * Copyright 2009 Analog Devices Inc.
10 *
11 * Bugs: Enter bugs at http://blackfin.uclinux.org/
12 *
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.
17 */
18
19#include <linux/init.h>
20#include <linux/module.h>
Barry Song7eaae412009-08-13 11:59:32 +080021#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"
32
33/* codec private data */
34struct ad1836_priv {
35 struct snd_soc_codec codec;
36 u16 reg_cache[AD1836_NUM_REGS];
37};
38
39static struct snd_soc_codec *ad1836_codec;
40struct snd_soc_codec_device soc_codec_dev_ad1836;
41static int ad1836_register(struct ad1836_priv *ad1836);
42static void ad1836_unregister(struct ad1836_priv *ad1836);
43
44/*
45 * AD1836 volume/mute/de-emphasis etc. controls
46 */
47static const char *ad1836_deemp[] = {"None", "44.1kHz", "32kHz", "48kHz"};
48
49static const struct soc_enum ad1836_deemp_enum =
50 SOC_ENUM_SINGLE(AD1836_DAC_CTRL1, 8, 4, ad1836_deemp);
51
52static 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),
60
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),
66
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),
74
75 /* ADC high-pass filter */
76 SOC_SINGLE("ADC High Pass Filter Switch", AD1836_ADC_CTRL1,
77 AD1836_ADC_HIGHPASS_FILTER, 1, 0),
78
79 /* DAC de-emphasis */
80 SOC_ENUM("Playback Deemphasis", ad1836_deemp_enum),
81};
82
83static 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"),
94};
95
96static 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" },
104};
105
106/*
107 * DAI ops entries
108 */
109
110static int ad1836_set_dai_fmt(struct snd_soc_dai *codec_dai,
111 unsigned int fmt)
112{
113 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
114 /* at present, we support adc aux mode to interface with
115 * blackfin sport tdm mode
116 */
117 case SND_SOC_DAIFMT_DSP_A:
118 break;
119 default:
120 return -EINVAL;
121 }
122
123 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
124 case SND_SOC_DAIFMT_IB_IF:
125 break;
126 default:
127 return -EINVAL;
128 }
129
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;
136 }
137
138 return 0;
139}
140
141static int ad1836_hw_params(struct snd_pcm_substream *substream,
142 struct snd_pcm_hw_params *params,
143 struct snd_soc_dai *dai)
144{
145 int word_len = 0;
146
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;
150
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;
163 }
164
165 snd_soc_update_bits(codec, AD1836_DAC_CTRL1,
166 AD1836_DAC_WORD_LEN_MASK, word_len);
167
168 snd_soc_update_bits(codec, AD1836_ADC_CTRL2,
169 AD1836_ADC_WORD_LEN_MASK, word_len);
170
171 return 0;
172}
173
174
175/*
176 * interface to read/write ad1836 register
177 */
178#define AD1836_SPI_REG_SHFT 12
179#define AD1836_SPI_READ (1 << 11)
180#define AD1836_SPI_VAL_MSK 0x3FF
181
182/*
183 * write to the ad1836 register space
184 */
185
186static int ad1836_write_reg(struct snd_soc_codec *codec, unsigned int reg,
187 unsigned int value)
188{
189 u16 *reg_cache = codec->reg_cache;
190 int ret = 0;
191
192 if (value != reg_cache[reg]) {
193 unsigned short buf;
194 struct spi_transfer t = {
195 .tx_buf = &buf,
196 .len = 2,
197 };
198 struct spi_message m;
199
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;
207 }
208
209 return ret;
210}
211
212/*
213 * read from the ad1836 register space cache
214 */
215static unsigned int ad1836_read_reg_cache(struct snd_soc_codec *codec,
216 unsigned int reg)
217{
218 u16 *reg_cache = codec->reg_cache;
219
220 if (reg >= codec->reg_cache_size)
221 return -EINVAL;
222
223 return reg_cache[reg];
224}
225
226static int __devinit ad1836_spi_probe(struct spi_device *spi)
227{
228 struct snd_soc_codec *codec;
229 struct ad1836_priv *ad1836;
230
231 ad1836 = kzalloc(sizeof(struct ad1836_priv), GFP_KERNEL);
232 if (ad1836 == NULL)
233 return -ENOMEM;
234
235 codec = &ad1836->codec;
236 codec->control_data = spi;
237 codec->dev = &spi->dev;
238
239 dev_set_drvdata(&spi->dev, ad1836);
240
241 return ad1836_register(ad1836);
242}
243
244static int __devexit ad1836_spi_remove(struct spi_device *spi)
245{
246 struct ad1836_priv *ad1836 = dev_get_drvdata(&spi->dev);
247
248 ad1836_unregister(ad1836);
249 return 0;
250}
251
252static struct spi_driver ad1836_spi_driver = {
253 .driver = {
Barry Song98235a42009-09-21 11:20:51 +0800254 .name = "ad1836",
Barry Song7eaae412009-08-13 11:59:32 +0800255 .owner = THIS_MODULE,
256 },
257 .probe = ad1836_spi_probe,
258 .remove = __devexit_p(ad1836_spi_remove),
259};
260
261static struct snd_soc_dai_ops ad1836_dai_ops = {
262 .hw_params = ad1836_hw_params,
263 .set_fmt = ad1836_set_dai_fmt,
264};
265
266/* codec DAI instance */
267struct 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,
276 },
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,
284 },
285 .ops = &ad1836_dai_ops,
286};
287EXPORT_SYMBOL_GPL(ad1836_dai);
288
289static int ad1836_register(struct ad1836_priv *ad1836)
290{
291 int ret;
292 struct snd_soc_codec *codec = &ad1836->codec;
293
294 if (ad1836_codec) {
295 dev_err(codec->dev, "Another ad1836 is registered\n");
296 return -EINVAL;
297 }
298
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);
313
314 ad1836_dai.dev = codec->dev;
315 ad1836_codec = codec;
316
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);
335
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;
341 }
342
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;
349 }
350
351 return 0;
352}
353
354static void ad1836_unregister(struct ad1836_priv *ad1836)
355{
356 snd_soc_unregister_dai(&ad1836_dai);
357 snd_soc_unregister_codec(&ad1836->codec);
358 kfree(ad1836);
359 ad1836_codec = NULL;
360}
361
362static int ad1836_probe(struct platform_device *pdev)
363{
364 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
365 struct snd_soc_codec *codec;
366 int ret = 0;
367
368 if (ad1836_codec == NULL) {
369 dev_err(&pdev->dev, "Codec device not registered\n");
370 return -ENODEV;
371 }
372
373 socdev->card->codec = ad1836_codec;
374 codec = ad1836_codec;
375
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;
381 }
382
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));
388 snd_soc_dapm_new_widgets(codec);
389
390 ret = snd_soc_init_card(socdev);
391 if (ret < 0) {
392 dev_err(codec->dev, "failed to register card: %d\n", ret);
393 goto card_err;
394 }
395
396 return ret;
397
398card_err:
399 snd_soc_free_pcms(socdev);
400 snd_soc_dapm_free(socdev);
401pcm_err:
402 return ret;
403}
404
405/* power down chip */
406static int ad1836_remove(struct platform_device *pdev)
407{
408 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
409
410 snd_soc_free_pcms(socdev);
411 snd_soc_dapm_free(socdev);
412
413 return 0;
414}
415
416struct snd_soc_codec_device soc_codec_dev_ad1836 = {
417 .probe = ad1836_probe,
418 .remove = ad1836_remove,
Barry Song7eaae412009-08-13 11:59:32 +0800419};
420EXPORT_SYMBOL_GPL(soc_codec_dev_ad1836);
421
422static int __init ad1836_init(void)
423{
424 int ret;
425
426 ret = spi_register_driver(&ad1836_spi_driver);
427 if (ret != 0) {
428 printk(KERN_ERR "Failed to register ad1836 SPI driver: %d\n",
429 ret);
430 }
431
432 return ret;
433}
434module_init(ad1836_init);
435
436static void __exit ad1836_exit(void)
437{
438 spi_unregister_driver(&ad1836_spi_driver);
439}
440module_exit(ad1836_exit);
441
442MODULE_DESCRIPTION("ASoC ad1836 driver");
443MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
444MODULE_LICENSE("GPL");