aboutsummaryrefslogtreecommitdiffstats
path: root/sound/soc/codecs/ad1938.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/soc/codecs/ad1938.c')
-rw-r--r--sound/soc/codecs/ad1938.c521
1 files changed, 0 insertions, 521 deletions
diff --git a/sound/soc/codecs/ad1938.c b/sound/soc/codecs/ad1938.c
deleted file mode 100644
index c233810d463d..000000000000
--- a/sound/soc/codecs/ad1938.c
+++ /dev/null
@@ -1,521 +0,0 @@
1/*
2 * File: sound/soc/codecs/ad1938.c
3 * Author: Barry Song <Barry.Song@analog.com>
4 *
5 * Created: June 04 2009
6 * Description: Driver for AD1938 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 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, see the file COPYING, or write
25 * to the Free Software Foundation, Inc.,
26 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29#include <linux/init.h>
30#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/device.h>
33#include <sound/core.h>
34#include <sound/pcm.h>
35#include <sound/pcm_params.h>
36#include <sound/initval.h>
37#include <sound/soc.h>
38#include <sound/tlv.h>
39#include <sound/soc-dapm.h>
40#include <linux/spi/spi.h>
41#include "ad1938.h"
42
43/* codec private data */
44struct ad1938_priv {
45 struct snd_soc_codec codec;
46 u8 reg_cache[AD1938_NUM_REGS];
47};
48
49/* ad1938 register cache & default register settings */
50static const u8 ad1938_reg[AD1938_NUM_REGS] = {
51 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0,
52};
53
54static struct snd_soc_codec *ad1938_codec;
55struct snd_soc_codec_device soc_codec_dev_ad1938;
56static int ad1938_register(struct ad1938_priv *ad1938);
57static void ad1938_unregister(struct ad1938_priv *ad1938);
58
59/*
60 * AD1938 volume/mute/de-emphasis etc. controls
61 */
62static const char *ad1938_deemp[] = {"None", "48kHz", "44.1kHz", "32kHz"};
63
64static const struct soc_enum ad1938_deemp_enum =
65 SOC_ENUM_SINGLE(AD1938_DAC_CTRL2, 1, 4, ad1938_deemp);
66
67static const struct snd_kcontrol_new ad1938_snd_controls[] = {
68 /* DAC volume control */
69 SOC_DOUBLE_R("DAC1 Volume", AD1938_DAC_L1_VOL,
70 AD1938_DAC_R1_VOL, 0, 0xFF, 1),
71 SOC_DOUBLE_R("DAC2 Volume", AD1938_DAC_L2_VOL,
72 AD1938_DAC_R2_VOL, 0, 0xFF, 1),
73 SOC_DOUBLE_R("DAC3 Volume", AD1938_DAC_L3_VOL,
74 AD1938_DAC_R3_VOL, 0, 0xFF, 1),
75 SOC_DOUBLE_R("DAC4 Volume", AD1938_DAC_L4_VOL,
76 AD1938_DAC_R4_VOL, 0, 0xFF, 1),
77
78 /* ADC switch control */
79 SOC_DOUBLE("ADC1 Switch", AD1938_ADC_CTRL0, AD1938_ADCL1_MUTE,
80 AD1938_ADCR1_MUTE, 1, 1),
81 SOC_DOUBLE("ADC2 Switch", AD1938_ADC_CTRL0, AD1938_ADCL2_MUTE,
82 AD1938_ADCR2_MUTE, 1, 1),
83
84 /* DAC switch control */
85 SOC_DOUBLE("DAC1 Switch", AD1938_DAC_CHNL_MUTE, AD1938_DACL1_MUTE,
86 AD1938_DACR1_MUTE, 1, 1),
87 SOC_DOUBLE("DAC2 Switch", AD1938_DAC_CHNL_MUTE, AD1938_DACL2_MUTE,
88 AD1938_DACR2_MUTE, 1, 1),
89 SOC_DOUBLE("DAC3 Switch", AD1938_DAC_CHNL_MUTE, AD1938_DACL3_MUTE,
90 AD1938_DACR3_MUTE, 1, 1),
91 SOC_DOUBLE("DAC4 Switch", AD1938_DAC_CHNL_MUTE, AD1938_DACL4_MUTE,
92 AD1938_DACR4_MUTE, 1, 1),
93
94 /* ADC high-pass filter */
95 SOC_SINGLE("ADC High Pass Filter Switch", AD1938_ADC_CTRL0,
96 AD1938_ADC_HIGHPASS_FILTER, 1, 0),
97
98 /* DAC de-emphasis */
99 SOC_ENUM("Playback Deemphasis", ad1938_deemp_enum),
100};
101
102static const struct snd_soc_dapm_widget ad1938_dapm_widgets[] = {
103 SND_SOC_DAPM_DAC("DAC", "Playback", AD1938_DAC_CTRL0, 0, 1),
104 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
105 SND_SOC_DAPM_SUPPLY("PLL_PWR", AD1938_PLL_CLK_CTRL0, 0, 1, NULL, 0),
106 SND_SOC_DAPM_SUPPLY("ADC_PWR", AD1938_ADC_CTRL0, 0, 1, NULL, 0),
107 SND_SOC_DAPM_OUTPUT("DAC1OUT"),
108 SND_SOC_DAPM_OUTPUT("DAC2OUT"),
109 SND_SOC_DAPM_OUTPUT("DAC3OUT"),
110 SND_SOC_DAPM_OUTPUT("DAC4OUT"),
111 SND_SOC_DAPM_INPUT("ADC1IN"),
112 SND_SOC_DAPM_INPUT("ADC2IN"),
113};
114
115static const struct snd_soc_dapm_route audio_paths[] = {
116 { "DAC", NULL, "PLL_PWR" },
117 { "ADC", NULL, "PLL_PWR" },
118 { "DAC", NULL, "ADC_PWR" },
119 { "ADC", NULL, "ADC_PWR" },
120 { "DAC1OUT", "DAC1 Switch", "DAC" },
121 { "DAC2OUT", "DAC2 Switch", "DAC" },
122 { "DAC3OUT", "DAC3 Switch", "DAC" },
123 { "DAC4OUT", "DAC4 Switch", "DAC" },
124 { "ADC", "ADC1 Switch", "ADC1IN" },
125 { "ADC", "ADC2 Switch", "ADC2IN" },
126};
127
128/*
129 * DAI ops entries
130 */
131
132static int ad1938_mute(struct snd_soc_dai *dai, int mute)
133{
134 struct snd_soc_codec *codec = dai->codec;
135 int reg;
136
137 reg = snd_soc_read(codec, AD1938_DAC_CTRL2);
138 reg = (mute > 0) ? reg | AD1938_DAC_MASTER_MUTE : reg &
139 (~AD1938_DAC_MASTER_MUTE);
140 snd_soc_write(codec, AD1938_DAC_CTRL2, reg);
141
142 return 0;
143}
144
145static int ad1938_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
146 unsigned int rx_mask, int slots, int width)
147{
148 struct snd_soc_codec *codec = dai->codec;
149 int dac_reg = snd_soc_read(codec, AD1938_DAC_CTRL1);
150 int adc_reg = snd_soc_read(codec, AD1938_ADC_CTRL2);
151
152 dac_reg &= ~AD1938_DAC_CHAN_MASK;
153 adc_reg &= ~AD1938_ADC_CHAN_MASK;
154
155 switch (slots) {
156 case 2:
157 dac_reg |= AD1938_DAC_2_CHANNELS << AD1938_DAC_CHAN_SHFT;
158 adc_reg |= AD1938_ADC_2_CHANNELS << AD1938_ADC_CHAN_SHFT;
159 break;
160 case 4:
161 dac_reg |= AD1938_DAC_4_CHANNELS << AD1938_DAC_CHAN_SHFT;
162 adc_reg |= AD1938_ADC_4_CHANNELS << AD1938_ADC_CHAN_SHFT;
163 break;
164 case 8:
165 dac_reg |= AD1938_DAC_8_CHANNELS << AD1938_DAC_CHAN_SHFT;
166 adc_reg |= AD1938_ADC_8_CHANNELS << AD1938_ADC_CHAN_SHFT;
167 break;
168 case 16:
169 dac_reg |= AD1938_DAC_16_CHANNELS << AD1938_DAC_CHAN_SHFT;
170 adc_reg |= AD1938_ADC_16_CHANNELS << AD1938_ADC_CHAN_SHFT;
171 break;
172 default:
173 return -EINVAL;
174 }
175
176 snd_soc_write(codec, AD1938_DAC_CTRL1, dac_reg);
177 snd_soc_write(codec, AD1938_ADC_CTRL2, adc_reg);
178
179 return 0;
180}
181
182static int ad1938_set_dai_fmt(struct snd_soc_dai *codec_dai,
183 unsigned int fmt)
184{
185 struct snd_soc_codec *codec = codec_dai->codec;
186 int adc_reg, dac_reg;
187
188 adc_reg = snd_soc_read(codec, AD1938_ADC_CTRL2);
189 dac_reg = snd_soc_read(codec, AD1938_DAC_CTRL1);
190
191 /* At present, the driver only support AUX ADC mode(SND_SOC_DAIFMT_I2S
192 * with TDM) and ADC&DAC TDM mode(SND_SOC_DAIFMT_DSP_A)
193 */
194 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
195 case SND_SOC_DAIFMT_I2S:
196 adc_reg &= ~AD1938_ADC_SERFMT_MASK;
197 adc_reg |= AD1938_ADC_SERFMT_TDM;
198 break;
199 case SND_SOC_DAIFMT_DSP_A:
200 adc_reg &= ~AD1938_ADC_SERFMT_MASK;
201 adc_reg |= AD1938_ADC_SERFMT_AUX;
202 break;
203 default:
204 return -EINVAL;
205 }
206
207 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
208 case SND_SOC_DAIFMT_NB_NF: /* normal bit clock + frame */
209 adc_reg &= ~AD1938_ADC_LEFT_HIGH;
210 adc_reg &= ~AD1938_ADC_BCLK_INV;
211 dac_reg &= ~AD1938_DAC_LEFT_HIGH;
212 dac_reg &= ~AD1938_DAC_BCLK_INV;
213 break;
214 case SND_SOC_DAIFMT_NB_IF: /* normal bclk + invert frm */
215 adc_reg |= AD1938_ADC_LEFT_HIGH;
216 adc_reg &= ~AD1938_ADC_BCLK_INV;
217 dac_reg |= AD1938_DAC_LEFT_HIGH;
218 dac_reg &= ~AD1938_DAC_BCLK_INV;
219 break;
220 case SND_SOC_DAIFMT_IB_NF: /* invert bclk + normal frm */
221 adc_reg &= ~AD1938_ADC_LEFT_HIGH;
222 adc_reg |= AD1938_ADC_BCLK_INV;
223 dac_reg &= ~AD1938_DAC_LEFT_HIGH;
224 dac_reg |= AD1938_DAC_BCLK_INV;
225 break;
226
227 case SND_SOC_DAIFMT_IB_IF: /* invert bclk + frm */
228 adc_reg |= AD1938_ADC_LEFT_HIGH;
229 adc_reg |= AD1938_ADC_BCLK_INV;
230 dac_reg |= AD1938_DAC_LEFT_HIGH;
231 dac_reg |= AD1938_DAC_BCLK_INV;
232 break;
233 default:
234 return -EINVAL;
235 }
236
237 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
238 case SND_SOC_DAIFMT_CBM_CFM: /* codec clk & frm master */
239 adc_reg |= AD1938_ADC_LCR_MASTER;
240 adc_reg |= AD1938_ADC_BCLK_MASTER;
241 dac_reg |= AD1938_DAC_LCR_MASTER;
242 dac_reg |= AD1938_DAC_BCLK_MASTER;
243 break;
244 case SND_SOC_DAIFMT_CBS_CFM: /* codec clk slave & frm master */
245 adc_reg |= AD1938_ADC_LCR_MASTER;
246 adc_reg &= ~AD1938_ADC_BCLK_MASTER;
247 dac_reg |= AD1938_DAC_LCR_MASTER;
248 dac_reg &= ~AD1938_DAC_BCLK_MASTER;
249 break;
250 case SND_SOC_DAIFMT_CBM_CFS: /* codec clk master & frame slave */
251 adc_reg &= ~AD1938_ADC_LCR_MASTER;
252 adc_reg |= AD1938_ADC_BCLK_MASTER;
253 dac_reg &= ~AD1938_DAC_LCR_MASTER;
254 dac_reg |= AD1938_DAC_BCLK_MASTER;
255 break;
256 case SND_SOC_DAIFMT_CBS_CFS: /* codec clk & frm slave */
257 adc_reg &= ~AD1938_ADC_LCR_MASTER;
258 adc_reg &= ~AD1938_ADC_BCLK_MASTER;
259 dac_reg &= ~AD1938_DAC_LCR_MASTER;
260 dac_reg &= ~AD1938_DAC_BCLK_MASTER;
261 break;
262 default:
263 return -EINVAL;
264 }
265
266 snd_soc_write(codec, AD1938_ADC_CTRL2, adc_reg);
267 snd_soc_write(codec, AD1938_DAC_CTRL1, dac_reg);
268
269 return 0;
270}
271
272static int ad1938_hw_params(struct snd_pcm_substream *substream,
273 struct snd_pcm_hw_params *params,
274 struct snd_soc_dai *dai)
275{
276 int word_len = 0, reg = 0;
277
278 struct snd_soc_pcm_runtime *rtd = substream->private_data;
279 struct snd_soc_device *socdev = rtd->socdev;
280 struct snd_soc_codec *codec = socdev->card->codec;
281
282 /* bit size */
283 switch (params_format(params)) {
284 case SNDRV_PCM_FORMAT_S16_LE:
285 word_len = 3;
286 break;
287 case SNDRV_PCM_FORMAT_S20_3LE:
288 word_len = 1;
289 break;
290 case SNDRV_PCM_FORMAT_S24_LE:
291 case SNDRV_PCM_FORMAT_S32_LE:
292 word_len = 0;
293 break;
294 }
295
296 reg = snd_soc_read(codec, AD1938_DAC_CTRL2);
297 reg = (reg & (~AD1938_DAC_WORD_LEN_MASK)) | word_len;
298 snd_soc_write(codec, AD1938_DAC_CTRL2, reg);
299
300 reg = snd_soc_read(codec, AD1938_ADC_CTRL1);
301 reg = (reg & (~AD1938_ADC_WORD_LEN_MASK)) | word_len;
302 snd_soc_write(codec, AD1938_ADC_CTRL1, reg);
303
304 return 0;
305}
306
307static int __devinit ad1938_spi_probe(struct spi_device *spi)
308{
309 struct snd_soc_codec *codec;
310 struct ad1938_priv *ad1938;
311
312 ad1938 = kzalloc(sizeof(struct ad1938_priv), GFP_KERNEL);
313 if (ad1938 == NULL)
314 return -ENOMEM;
315
316 codec = &ad1938->codec;
317 codec->control_data = spi;
318 codec->dev = &spi->dev;
319
320 dev_set_drvdata(&spi->dev, ad1938);
321
322 return ad1938_register(ad1938);
323}
324
325static int __devexit ad1938_spi_remove(struct spi_device *spi)
326{
327 struct ad1938_priv *ad1938 = dev_get_drvdata(&spi->dev);
328
329 ad1938_unregister(ad1938);
330 return 0;
331}
332
333static struct spi_driver ad1938_spi_driver = {
334 .driver = {
335 .name = "ad1938",
336 .owner = THIS_MODULE,
337 },
338 .probe = ad1938_spi_probe,
339 .remove = __devexit_p(ad1938_spi_remove),
340};
341
342static struct snd_soc_dai_ops ad1938_dai_ops = {
343 .hw_params = ad1938_hw_params,
344 .digital_mute = ad1938_mute,
345 .set_tdm_slot = ad1938_set_tdm_slot,
346 .set_fmt = ad1938_set_dai_fmt,
347};
348
349/* codec DAI instance */
350struct snd_soc_dai ad1938_dai = {
351 .name = "AD1938",
352 .playback = {
353 .stream_name = "Playback",
354 .channels_min = 2,
355 .channels_max = 8,
356 .rates = SNDRV_PCM_RATE_48000,
357 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
358 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
359 },
360 .capture = {
361 .stream_name = "Capture",
362 .channels_min = 2,
363 .channels_max = 4,
364 .rates = SNDRV_PCM_RATE_48000,
365 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
366 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
367 },
368 .ops = &ad1938_dai_ops,
369};
370EXPORT_SYMBOL_GPL(ad1938_dai);
371
372static int ad1938_register(struct ad1938_priv *ad1938)
373{
374 int ret;
375 struct snd_soc_codec *codec = &ad1938->codec;
376
377 if (ad1938_codec) {
378 dev_err(codec->dev, "Another ad1938 is registered\n");
379 return -EINVAL;
380 }
381
382 mutex_init(&codec->mutex);
383 INIT_LIST_HEAD(&codec->dapm_widgets);
384 INIT_LIST_HEAD(&codec->dapm_paths);
385 codec->private_data = ad1938;
386 codec->reg_cache = ad1938->reg_cache;
387 codec->reg_cache_size = AD1938_NUM_REGS;
388 codec->name = "AD1938";
389 codec->owner = THIS_MODULE;
390 codec->dai = &ad1938_dai;
391 codec->num_dai = 1;
392 INIT_LIST_HEAD(&codec->dapm_widgets);
393 INIT_LIST_HEAD(&codec->dapm_paths);
394
395 ad1938_dai.dev = codec->dev;
396 ad1938_codec = codec;
397
398 memcpy(codec->reg_cache, ad1938_reg, AD1938_NUM_REGS);
399
400 ret = snd_soc_codec_set_cache_io(codec, 16, 8, SND_SOC_SPI);
401 if (ret < 0) {
402 dev_err(codec->dev, "failed to set cache I/O: %d\n",
403 ret);
404 kfree(ad1938);
405 return ret;
406 }
407
408 /* default setting for ad1938 */
409
410 /* unmute dac channels */
411 snd_soc_write(codec, AD1938_DAC_CHNL_MUTE, 0x0);
412 /* de-emphasis: 48kHz, powedown dac */
413 snd_soc_write(codec, AD1938_DAC_CTRL2, 0x1A);
414 /* powerdown dac, dac in tdm mode */
415 snd_soc_write(codec, AD1938_DAC_CTRL0, 0x41);
416 /* high-pass filter enable */
417 snd_soc_write(codec, AD1938_ADC_CTRL0, 0x3);
418 /* sata delay=1, adc aux mode */
419 snd_soc_write(codec, AD1938_ADC_CTRL1, 0x43);
420 /* pll input: mclki/xi */
421 snd_soc_write(codec, AD1938_PLL_CLK_CTRL0, 0x9D);
422 snd_soc_write(codec, AD1938_PLL_CLK_CTRL1, 0x04);
423
424 ret = snd_soc_register_codec(codec);
425 if (ret != 0) {
426 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
427 kfree(ad1938);
428 return ret;
429 }
430
431 ret = snd_soc_register_dai(&ad1938_dai);
432 if (ret != 0) {
433 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
434 snd_soc_unregister_codec(codec);
435 kfree(ad1938);
436 return ret;
437 }
438
439 return 0;
440}
441
442static void ad1938_unregister(struct ad1938_priv *ad1938)
443{
444 snd_soc_unregister_dai(&ad1938_dai);
445 snd_soc_unregister_codec(&ad1938->codec);
446 kfree(ad1938);
447 ad1938_codec = NULL;
448}
449
450static int ad1938_probe(struct platform_device *pdev)
451{
452 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
453 struct snd_soc_codec *codec;
454 int ret = 0;
455
456 if (ad1938_codec == NULL) {
457 dev_err(&pdev->dev, "Codec device not registered\n");
458 return -ENODEV;
459 }
460
461 socdev->card->codec = ad1938_codec;
462 codec = ad1938_codec;
463
464 /* register pcms */
465 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
466 if (ret < 0) {
467 dev_err(codec->dev, "failed to create pcms: %d\n", ret);
468 goto pcm_err;
469 }
470
471 snd_soc_add_controls(codec, ad1938_snd_controls,
472 ARRAY_SIZE(ad1938_snd_controls));
473 snd_soc_dapm_new_controls(codec, ad1938_dapm_widgets,
474 ARRAY_SIZE(ad1938_dapm_widgets));
475 snd_soc_dapm_add_routes(codec, audio_paths, ARRAY_SIZE(audio_paths));
476
477
478pcm_err:
479 return ret;
480}
481
482/* power down chip */
483static int ad1938_remove(struct platform_device *pdev)
484{
485 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
486
487 snd_soc_free_pcms(socdev);
488 snd_soc_dapm_free(socdev);
489
490 return 0;
491}
492
493struct snd_soc_codec_device soc_codec_dev_ad1938 = {
494 .probe = ad1938_probe,
495 .remove = ad1938_remove,
496};
497EXPORT_SYMBOL_GPL(soc_codec_dev_ad1938);
498
499static int __init ad1938_init(void)
500{
501 int ret;
502
503 ret = spi_register_driver(&ad1938_spi_driver);
504 if (ret != 0) {
505 printk(KERN_ERR "Failed to register ad1938 SPI driver: %d\n",
506 ret);
507 }
508
509 return ret;
510}
511module_init(ad1938_init);
512
513static void __exit ad1938_exit(void)
514{
515 spi_unregister_driver(&ad1938_spi_driver);
516}
517module_exit(ad1938_exit);
518
519MODULE_DESCRIPTION("ASoC ad1938 driver");
520MODULE_AUTHOR("Barry Song ");
521MODULE_LICENSE("GPL");