diff options
author | Barry Song <21cnbao@gmail.com> | 2009-08-12 23:59:32 -0400 |
---|---|---|
committer | Mark Brown <broonie@opensource.wolfsonmicro.com> | 2009-08-13 10:18:53 -0400 |
commit | 7eaae41ea54af5516138aef5fd33bc55598cbf48 (patch) | |
tree | 19a98f14694df043d85fd7f36244ca9264dc2e35 /sound/soc/codecs/ad1836.c | |
parent | 9008adf9a9c3a43ef237f6cd416857569beb0029 (diff) |
new ad1836 codec driver based on asoc
There has been an ad1836 driver in sound/blackfin based on traditional alsa.
The new driver is based on asoc. The architecture of ad1836 codec driver is
very much like ad1938.
Signed-off-by: Barry Song <21cnbao@gmail.com>
Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
Diffstat (limited to 'sound/soc/codecs/ad1836.c')
-rw-r--r-- | sound/soc/codecs/ad1836.c | 451 |
1 files changed, 451 insertions, 0 deletions
diff --git a/sound/soc/codecs/ad1836.c b/sound/soc/codecs/ad1836.c new file mode 100644 index 000000000000..79b9d198a61d --- /dev/null +++ b/sound/soc/codecs/ad1836.c | |||
@@ -0,0 +1,451 @@ | |||
1 | /* | ||
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> | ||
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> | ||
32 | #include "ad1836.h" | ||
33 | |||
34 | /* codec private data */ | ||
35 | struct ad1836_priv { | ||
36 | struct snd_soc_codec codec; | ||
37 | u16 reg_cache[AD1836_NUM_REGS]; | ||
38 | }; | ||
39 | |||
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); | ||
44 | |||
45 | /* | ||
46 | * AD1836 volume/mute/de-emphasis etc. controls | ||
47 | */ | ||
48 | static const char *ad1836_deemp[] = {"None", "44.1kHz", "32kHz", "48kHz"}; | ||
49 | |||
50 | static const struct soc_enum ad1836_deemp_enum = | ||
51 | SOC_ENUM_SINGLE(AD1836_DAC_CTRL1, 8, 4, ad1836_deemp); | ||
52 | |||
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), | ||
61 | |||
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), | ||
67 | |||
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), | ||
75 | |||
76 | /* ADC high-pass filter */ | ||
77 | SOC_SINGLE("ADC High Pass Filter Switch", AD1836_ADC_CTRL1, | ||
78 | AD1836_ADC_HIGHPASS_FILTER, 1, 0), | ||
79 | |||
80 | /* DAC de-emphasis */ | ||
81 | SOC_ENUM("Playback Deemphasis", ad1836_deemp_enum), | ||
82 | }; | ||
83 | |||
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"), | ||
95 | }; | ||
96 | |||
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" }, | ||
105 | }; | ||
106 | |||
107 | /* | ||
108 | * DAI ops entries | ||
109 | */ | ||
110 | |||
111 | static int ad1836_set_dai_fmt(struct snd_soc_dai *codec_dai, | ||
112 | unsigned int fmt) | ||
113 | { | ||
114 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | ||
115 | /* at present, we support adc aux mode to interface with | ||
116 | * blackfin sport tdm mode | ||
117 | */ | ||
118 | case SND_SOC_DAIFMT_DSP_A: | ||
119 | break; | ||
120 | default: | ||
121 | return -EINVAL; | ||
122 | } | ||
123 | |||
124 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { | ||
125 | case SND_SOC_DAIFMT_IB_IF: | ||
126 | break; | ||
127 | default: | ||
128 | return -EINVAL; | ||
129 | } | ||
130 | |||
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: | ||
134 | break; | ||
135 | default: | ||
136 | return -EINVAL; | ||
137 | } | ||
138 | |||
139 | return 0; | ||
140 | } | ||
141 | |||
142 | static int ad1836_hw_params(struct snd_pcm_substream *substream, | ||
143 | struct snd_pcm_hw_params *params, | ||
144 | struct snd_soc_dai *dai) | ||
145 | { | ||
146 | int word_len = 0; | ||
147 | |||
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; | ||
151 | |||
152 | /* bit size */ | ||
153 | switch (params_format(params)) { | ||
154 | case SNDRV_PCM_FORMAT_S16_LE: | ||
155 | word_len = 3; | ||
156 | break; | ||
157 | case SNDRV_PCM_FORMAT_S20_3LE: | ||
158 | word_len = 1; | ||
159 | break; | ||
160 | case SNDRV_PCM_FORMAT_S24_LE: | ||
161 | case SNDRV_PCM_FORMAT_S32_LE: | ||
162 | word_len = 0; | ||
163 | break; | ||
164 | } | ||
165 | |||
166 | snd_soc_update_bits(codec, AD1836_DAC_CTRL1, | ||
167 | AD1836_DAC_WORD_LEN_MASK, word_len); | ||
168 | |||
169 | snd_soc_update_bits(codec, AD1836_ADC_CTRL2, | ||
170 | AD1836_ADC_WORD_LEN_MASK, word_len); | ||
171 | |||
172 | return 0; | ||
173 | } | ||
174 | |||
175 | |||
176 | /* | ||
177 | * interface to read/write ad1836 register | ||
178 | */ | ||
179 | #define AD1836_SPI_REG_SHFT 12 | ||
180 | #define AD1836_SPI_READ (1 << 11) | ||
181 | #define AD1836_SPI_VAL_MSK 0x3FF | ||
182 | |||
183 | /* | ||
184 | * write to the ad1836 register space | ||
185 | */ | ||
186 | |||
187 | static int ad1836_write_reg(struct snd_soc_codec *codec, unsigned int reg, | ||
188 | unsigned int value) | ||
189 | { | ||
190 | u16 *reg_cache = codec->reg_cache; | ||
191 | int ret = 0; | ||
192 | |||
193 | if (value != reg_cache[reg]) { | ||
194 | unsigned short buf; | ||
195 | struct spi_transfer t = { | ||
196 | .tx_buf = &buf, | ||
197 | .len = 2, | ||
198 | }; | ||
199 | struct spi_message m; | ||
200 | |||
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); | ||
206 | if (ret == 0) | ||
207 | reg_cache[reg] = value; | ||
208 | } | ||
209 | |||
210 | return ret; | ||
211 | } | ||
212 | |||
213 | /* | ||
214 | * read from the ad1836 register space cache | ||
215 | */ | ||
216 | static unsigned int ad1836_read_reg_cache(struct snd_soc_codec *codec, | ||
217 | unsigned int reg) | ||
218 | { | ||
219 | u16 *reg_cache = codec->reg_cache; | ||
220 | |||
221 | if (reg >= codec->reg_cache_size) | ||
222 | return -EINVAL; | ||
223 | |||
224 | return reg_cache[reg]; | ||
225 | } | ||
226 | |||
227 | static int __devinit ad1836_spi_probe(struct spi_device *spi) | ||
228 | { | ||
229 | struct snd_soc_codec *codec; | ||
230 | struct ad1836_priv *ad1836; | ||
231 | |||
232 | ad1836 = kzalloc(sizeof(struct ad1836_priv), GFP_KERNEL); | ||
233 | if (ad1836 == NULL) | ||
234 | return -ENOMEM; | ||
235 | |||
236 | codec = &ad1836->codec; | ||
237 | codec->control_data = spi; | ||
238 | codec->dev = &spi->dev; | ||
239 | |||
240 | dev_set_drvdata(&spi->dev, ad1836); | ||
241 | |||
242 | return ad1836_register(ad1836); | ||
243 | } | ||
244 | |||
245 | static int __devexit ad1836_spi_remove(struct spi_device *spi) | ||
246 | { | ||
247 | struct ad1836_priv *ad1836 = dev_get_drvdata(&spi->dev); | ||
248 | |||
249 | ad1836_unregister(ad1836); | ||
250 | return 0; | ||
251 | } | ||
252 | |||
253 | static struct spi_driver ad1836_spi_driver = { | ||
254 | .driver = { | ||
255 | .name = "ad1836-spi", | ||
256 | .bus = &spi_bus_type, | ||
257 | .owner = THIS_MODULE, | ||
258 | }, | ||
259 | .probe = ad1836_spi_probe, | ||
260 | .remove = __devexit_p(ad1836_spi_remove), | ||
261 | }; | ||
262 | |||
263 | static struct snd_soc_dai_ops ad1836_dai_ops = { | ||
264 | .hw_params = ad1836_hw_params, | ||
265 | .set_fmt = ad1836_set_dai_fmt, | ||
266 | }; | ||
267 | |||
268 | /* codec DAI instance */ | ||
269 | struct snd_soc_dai ad1836_dai = { | ||
270 | .name = "AD1836", | ||
271 | .playback = { | ||
272 | .stream_name = "Playback", | ||
273 | .channels_min = 2, | ||
274 | .channels_max = 6, | ||
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, | ||
278 | }, | ||
279 | .capture = { | ||
280 | .stream_name = "Capture", | ||
281 | .channels_min = 2, | ||
282 | .channels_max = 4, | ||
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, | ||
286 | }, | ||
287 | .ops = &ad1836_dai_ops, | ||
288 | }; | ||
289 | EXPORT_SYMBOL_GPL(ad1836_dai); | ||
290 | |||
291 | static int ad1836_register(struct ad1836_priv *ad1836) | ||
292 | { | ||
293 | int ret; | ||
294 | struct snd_soc_codec *codec = &ad1836->codec; | ||
295 | |||
296 | if (ad1836_codec) { | ||
297 | dev_err(codec->dev, "Another ad1836 is registered\n"); | ||
298 | return -EINVAL; | ||
299 | } | ||
300 | |||
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; | ||
310 | codec->num_dai = 1; | ||
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); | ||
315 | |||
316 | ad1836_dai.dev = codec->dev; | ||
317 | ad1836_codec = codec; | ||
318 | |||
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); | ||
330 | /* volume */ | ||
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); | ||
337 | |||
338 | ret = snd_soc_register_codec(codec); | ||
339 | if (ret != 0) { | ||
340 | dev_err(codec->dev, "Failed to register codec: %d\n", ret); | ||
341 | kfree(ad1836); | ||
342 | return ret; | ||
343 | } | ||
344 | |||
345 | ret = snd_soc_register_dai(&ad1836_dai); | ||
346 | if (ret != 0) { | ||
347 | dev_err(codec->dev, "Failed to register DAI: %d\n", ret); | ||
348 | snd_soc_unregister_codec(codec); | ||
349 | kfree(ad1836); | ||
350 | return ret; | ||
351 | } | ||
352 | |||
353 | return 0; | ||
354 | } | ||
355 | |||
356 | static void ad1836_unregister(struct ad1836_priv *ad1836) | ||
357 | { | ||
358 | snd_soc_unregister_dai(&ad1836_dai); | ||
359 | snd_soc_unregister_codec(&ad1836->codec); | ||
360 | kfree(ad1836); | ||
361 | ad1836_codec = NULL; | ||
362 | } | ||
363 | |||
364 | static int ad1836_probe(struct platform_device *pdev) | ||
365 | { | ||
366 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | ||
367 | struct snd_soc_codec *codec; | ||
368 | int ret = 0; | ||
369 | |||
370 | if (ad1836_codec == NULL) { | ||
371 | dev_err(&pdev->dev, "Codec device not registered\n"); | ||
372 | return -ENODEV; | ||
373 | } | ||
374 | |||
375 | socdev->card->codec = ad1836_codec; | ||
376 | codec = ad1836_codec; | ||
377 | |||
378 | /* register pcms */ | ||
379 | ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); | ||
380 | if (ret < 0) { | ||
381 | dev_err(codec->dev, "failed to create pcms: %d\n", ret); | ||
382 | goto pcm_err; | ||
383 | } | ||
384 | |||
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); | ||
391 | |||
392 | ret = snd_soc_init_card(socdev); | ||
393 | if (ret < 0) { | ||
394 | dev_err(codec->dev, "failed to register card: %d\n", ret); | ||
395 | goto card_err; | ||
396 | } | ||
397 | |||
398 | return ret; | ||
399 | |||
400 | card_err: | ||
401 | snd_soc_free_pcms(socdev); | ||
402 | snd_soc_dapm_free(socdev); | ||
403 | pcm_err: | ||
404 | return ret; | ||
405 | } | ||
406 | |||
407 | /* power down chip */ | ||
408 | static int ad1836_remove(struct platform_device *pdev) | ||
409 | { | ||
410 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | ||
411 | |||
412 | snd_soc_free_pcms(socdev); | ||
413 | snd_soc_dapm_free(socdev); | ||
414 | |||
415 | return 0; | ||
416 | } | ||
417 | |||
418 | struct snd_soc_codec_device soc_codec_dev_ad1836 = { | ||
419 | .probe = ad1836_probe, | ||
420 | .remove = ad1836_remove, | ||
421 | /* The power management of ad1836 is very simple. There are | ||
422 | * only adc&dac 2 components to control. Dapm handles them. | ||
423 | */ | ||
424 | .suspend = NULL, | ||
425 | .resume = NULL, | ||
426 | }; | ||
427 | EXPORT_SYMBOL_GPL(soc_codec_dev_ad1836); | ||
428 | |||
429 | static int __init ad1836_init(void) | ||
430 | { | ||
431 | int ret; | ||
432 | |||
433 | ret = spi_register_driver(&ad1836_spi_driver); | ||
434 | if (ret != 0) { | ||
435 | printk(KERN_ERR "Failed to register ad1836 SPI driver: %d\n", | ||
436 | ret); | ||
437 | } | ||
438 | |||
439 | return ret; | ||
440 | } | ||
441 | module_init(ad1836_init); | ||
442 | |||
443 | static void __exit ad1836_exit(void) | ||
444 | { | ||
445 | spi_unregister_driver(&ad1836_spi_driver); | ||
446 | } | ||
447 | module_exit(ad1836_exit); | ||
448 | |||
449 | MODULE_DESCRIPTION("ASoC ad1836 driver"); | ||
450 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); | ||
451 | MODULE_LICENSE("GPL"); | ||