blob: ad4c0676044001e751aa4189a62c6554e7ea703d [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Barry Song7eaae412009-08-13 11:59:32 +080021#include <linux/module.h>
Barry Song7eaae412009-08-13 11:59:32 +080022#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>
Barry Song7eaae412009-08-13 11:59:32 +080030#include <linux/spi/spi.h>
31#include "ad1836.h"
32
Lars-Peter Clausen874ce772011-06-06 13:38:38 +020033enum ad1836_type {
34 AD1835,
35 AD1836,
36 AD1838,
37};
38
Barry Song7eaae412009-08-13 11:59:32 +080039/* codec private data */
40struct ad1836_priv {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000041 enum snd_soc_control_type control_type;
42 void *control_data;
Lars-Peter Clausen874ce772011-06-06 13:38:38 +020043 enum ad1836_type type;
Barry Song7eaae412009-08-13 11:59:32 +080044};
45
Barry Song7eaae412009-08-13 11:59:32 +080046/*
47 * AD1836 volume/mute/de-emphasis etc. controls
48 */
49static const char *ad1836_deemp[] = {"None", "44.1kHz", "32kHz", "48kHz"};
50
51static const struct soc_enum ad1836_deemp_enum =
52 SOC_ENUM_SINGLE(AD1836_DAC_CTRL1, 8, 4, ad1836_deemp);
53
Lars-Peter Clausen90bc11d2011-06-06 13:38:36 +020054#define AD1836_DAC_VOLUME(x) \
55 SOC_DOUBLE_R("DAC" #x " Playback Volume", AD1836_DAC_L_VOL(x), \
56 AD1836_DAC_R_VOL(x), 0, 0x3FF, 0)
57
58#define AD1836_DAC_SWITCH(x) \
59 SOC_DOUBLE("DAC" #x " Playback Switch", AD1836_DAC_CTRL2, \
60 AD1836_MUTE_LEFT(x), AD1836_MUTE_RIGHT(x), 1, 1)
61
62#define AD1836_ADC_SWITCH(x) \
63 SOC_DOUBLE("ADC" #x " Capture Switch", AD1836_ADC_CTRL2, \
64 AD1836_MUTE_LEFT(x), AD1836_MUTE_RIGHT(x), 1, 1)
65
Lars-Peter Clausen874ce772011-06-06 13:38:38 +020066static const struct snd_kcontrol_new ad183x_dac_controls[] = {
Lars-Peter Clausen90bc11d2011-06-06 13:38:36 +020067 AD1836_DAC_VOLUME(1),
Lars-Peter Clausen874ce772011-06-06 13:38:38 +020068 AD1836_DAC_SWITCH(1),
Lars-Peter Clausen90bc11d2011-06-06 13:38:36 +020069 AD1836_DAC_VOLUME(2),
Lars-Peter Clausen874ce772011-06-06 13:38:38 +020070 AD1836_DAC_SWITCH(2),
Lars-Peter Clausen90bc11d2011-06-06 13:38:36 +020071 AD1836_DAC_VOLUME(3),
Lars-Peter Clausen874ce772011-06-06 13:38:38 +020072 AD1836_DAC_SWITCH(3),
73 AD1836_DAC_VOLUME(4),
74 AD1836_DAC_SWITCH(4),
75};
Barry Song7eaae412009-08-13 11:59:32 +080076
Lars-Peter Clausen874ce772011-06-06 13:38:38 +020077static const struct snd_soc_dapm_widget ad183x_dac_dapm_widgets[] = {
78 SND_SOC_DAPM_OUTPUT("DAC1OUT"),
79 SND_SOC_DAPM_OUTPUT("DAC2OUT"),
80 SND_SOC_DAPM_OUTPUT("DAC3OUT"),
81 SND_SOC_DAPM_OUTPUT("DAC4OUT"),
82};
83
84static const struct snd_soc_dapm_route ad183x_dac_routes[] = {
85 { "DAC1OUT", NULL, "DAC" },
86 { "DAC2OUT", NULL, "DAC" },
87 { "DAC3OUT", NULL, "DAC" },
88 { "DAC4OUT", NULL, "DAC" },
89};
90
91static const struct snd_kcontrol_new ad183x_adc_controls[] = {
Lars-Peter Clausen90bc11d2011-06-06 13:38:36 +020092 AD1836_ADC_SWITCH(1),
93 AD1836_ADC_SWITCH(2),
Lars-Peter Clausen874ce772011-06-06 13:38:38 +020094 AD1836_ADC_SWITCH(3),
95};
Barry Song7eaae412009-08-13 11:59:32 +080096
Lars-Peter Clausen874ce772011-06-06 13:38:38 +020097static const struct snd_soc_dapm_widget ad183x_adc_dapm_widgets[] = {
98 SND_SOC_DAPM_INPUT("ADC1IN"),
99 SND_SOC_DAPM_INPUT("ADC2IN"),
100};
Barry Song7eaae412009-08-13 11:59:32 +0800101
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200102static const struct snd_soc_dapm_route ad183x_adc_routes[] = {
103 { "ADC", NULL, "ADC1IN" },
104 { "ADC", NULL, "ADC2IN" },
105};
106
107static const struct snd_kcontrol_new ad183x_controls[] = {
Barry Song7eaae412009-08-13 11:59:32 +0800108 /* ADC high-pass filter */
109 SOC_SINGLE("ADC High Pass Filter Switch", AD1836_ADC_CTRL1,
110 AD1836_ADC_HIGHPASS_FILTER, 1, 0),
111
112 /* DAC de-emphasis */
113 SOC_ENUM("Playback Deemphasis", ad1836_deemp_enum),
114};
115
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200116static const struct snd_soc_dapm_widget ad183x_dapm_widgets[] = {
Barry Song7eaae412009-08-13 11:59:32 +0800117 SND_SOC_DAPM_DAC("DAC", "Playback", AD1836_DAC_CTRL1,
118 AD1836_DAC_POWERDOWN, 1),
119 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
120 SND_SOC_DAPM_SUPPLY("ADC_PWR", AD1836_ADC_CTRL1,
121 AD1836_ADC_POWERDOWN, 1, NULL, 0),
Barry Song7eaae412009-08-13 11:59:32 +0800122};
123
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200124static const struct snd_soc_dapm_route ad183x_dapm_routes[] = {
Barry Song7eaae412009-08-13 11:59:32 +0800125 { "DAC", NULL, "ADC_PWR" },
126 { "ADC", NULL, "ADC_PWR" },
Barry Song7eaae412009-08-13 11:59:32 +0800127};
128
129/*
130 * DAI ops entries
131 */
132
133static int ad1836_set_dai_fmt(struct snd_soc_dai *codec_dai,
134 unsigned int fmt)
135{
136 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
137 /* at present, we support adc aux mode to interface with
138 * blackfin sport tdm mode
139 */
140 case SND_SOC_DAIFMT_DSP_A:
141 break;
142 default:
143 return -EINVAL;
144 }
145
146 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
147 case SND_SOC_DAIFMT_IB_IF:
148 break;
149 default:
150 return -EINVAL;
151 }
152
153 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
154 /* ALCLK,ABCLK are both output, AD1836 can only be master */
155 case SND_SOC_DAIFMT_CBM_CFM:
156 break;
157 default:
158 return -EINVAL;
159 }
160
161 return 0;
162}
163
164static int ad1836_hw_params(struct snd_pcm_substream *substream,
165 struct snd_pcm_hw_params *params,
166 struct snd_soc_dai *dai)
167{
168 int word_len = 0;
169
170 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000171 struct snd_soc_codec *codec = rtd->codec;
Barry Song7eaae412009-08-13 11:59:32 +0800172
173 /* bit size */
174 switch (params_format(params)) {
175 case SNDRV_PCM_FORMAT_S16_LE:
176 word_len = 3;
177 break;
178 case SNDRV_PCM_FORMAT_S20_3LE:
179 word_len = 1;
180 break;
181 case SNDRV_PCM_FORMAT_S24_LE:
182 case SNDRV_PCM_FORMAT_S32_LE:
183 word_len = 0;
184 break;
185 }
186
187 snd_soc_update_bits(codec, AD1836_DAC_CTRL1,
188 AD1836_DAC_WORD_LEN_MASK, word_len);
189
190 snd_soc_update_bits(codec, AD1836_ADC_CTRL2,
191 AD1836_ADC_WORD_LEN_MASK, word_len);
192
193 return 0;
194}
195
Barry Song84549d22010-01-25 16:42:25 +0800196#ifdef CONFIG_PM
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000197static int ad1836_soc_suspend(struct snd_soc_codec *codec,
Barry Song84549d22010-01-25 16:42:25 +0800198 pm_message_t state)
199{
Barry Song84549d22010-01-25 16:42:25 +0800200 /* reset clock control mode */
Lars-Peter Clausen2cf03422011-06-06 13:38:37 +0200201 return snd_soc_update_bits(codec, AD1836_ADC_CTRL2,
202 AD1836_ADC_SERFMT_MASK, 0);
Barry Song84549d22010-01-25 16:42:25 +0800203}
204
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000205static int ad1836_soc_resume(struct snd_soc_codec *codec)
Barry Song84549d22010-01-25 16:42:25 +0800206{
Barry Song84549d22010-01-25 16:42:25 +0800207 /* restore clock control mode */
Lars-Peter Clausen2cf03422011-06-06 13:38:37 +0200208 return snd_soc_update_bits(codec, AD1836_ADC_CTRL2,
209 AD1836_ADC_SERFMT_MASK, AD1836_ADC_AUX);
Barry Song84549d22010-01-25 16:42:25 +0800210}
211#else
212#define ad1836_soc_suspend NULL
213#define ad1836_soc_resume NULL
214#endif
215
Barry Song7eaae412009-08-13 11:59:32 +0800216static struct snd_soc_dai_ops ad1836_dai_ops = {
217 .hw_params = ad1836_hw_params,
218 .set_fmt = ad1836_set_dai_fmt,
219};
220
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200221#define AD183X_DAI(_name, num_dacs, num_adcs) \
222{ \
223 .name = _name "-hifi", \
224 .playback = { \
225 .stream_name = "Playback", \
226 .channels_min = 2, \
227 .channels_max = (num_dacs) * 2, \
228 .rates = SNDRV_PCM_RATE_48000, \
229 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | \
230 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, \
231 }, \
232 .capture = { \
233 .stream_name = "Capture", \
234 .channels_min = 2, \
235 .channels_max = (num_adcs) * 2, \
236 .rates = SNDRV_PCM_RATE_48000, \
237 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | \
238 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, \
239 }, \
240 .ops = &ad1836_dai_ops, \
241}
242
243static struct snd_soc_dai_driver ad183x_dais[] = {
244 [AD1835] = AD183X_DAI("ad1835", 4, 1),
245 [AD1836] = AD183X_DAI("ad1836", 3, 2),
246 [AD1838] = AD183X_DAI("ad1838", 3, 1),
Barry Song7eaae412009-08-13 11:59:32 +0800247};
Barry Song7eaae412009-08-13 11:59:32 +0800248
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000249static int ad1836_probe(struct snd_soc_codec *codec)
Barry Song7eaae412009-08-13 11:59:32 +0800250{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000251 struct ad1836_priv *ad1836 = snd_soc_codec_get_drvdata(codec);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200252 struct snd_soc_dapm_context *dapm = &codec->dapm;
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200253 int num_dacs, num_adcs;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000254 int ret = 0;
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200255 int i;
256
257 num_dacs = ad183x_dais[ad1836->type].playback.channels_max / 2;
258 num_adcs = ad183x_dais[ad1836->type].capture.channels_max / 2;
Barry Song7eaae412009-08-13 11:59:32 +0800259
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000260 codec->control_data = ad1836->control_data;
Barry Song63b62ab2010-01-27 11:46:17 +0800261 ret = snd_soc_codec_set_cache_io(codec, 4, 12, SND_SOC_SPI);
262 if (ret < 0) {
263 dev_err(codec->dev, "failed to set cache I/O: %d\n",
264 ret);
Barry Song63b62ab2010-01-27 11:46:17 +0800265 return ret;
266 }
267
Barry Song7eaae412009-08-13 11:59:32 +0800268 /* default setting for ad1836 */
269 /* de-emphasis: 48kHz, power-on dac */
Barry Song63b62ab2010-01-27 11:46:17 +0800270 snd_soc_write(codec, AD1836_DAC_CTRL1, 0x300);
Barry Song7eaae412009-08-13 11:59:32 +0800271 /* unmute dac channels */
Barry Song63b62ab2010-01-27 11:46:17 +0800272 snd_soc_write(codec, AD1836_DAC_CTRL2, 0x0);
Barry Song7eaae412009-08-13 11:59:32 +0800273 /* high-pass filter enable, power-on adc */
Barry Song63b62ab2010-01-27 11:46:17 +0800274 snd_soc_write(codec, AD1836_ADC_CTRL1, 0x100);
Barry Song7eaae412009-08-13 11:59:32 +0800275 /* unmute adc channles, adc aux mode */
Barry Song63b62ab2010-01-27 11:46:17 +0800276 snd_soc_write(codec, AD1836_ADC_CTRL2, 0x180);
Barry Song7eaae412009-08-13 11:59:32 +0800277 /* volume */
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200278 for (i = 1; i <= num_dacs; ++i) {
279 snd_soc_write(codec, AD1836_DAC_L_VOL(i), 0x3FF);
280 snd_soc_write(codec, AD1836_DAC_R_VOL(i), 0x3FF);
281 }
Barry Song7eaae412009-08-13 11:59:32 +0800282
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200283 if (ad1836->type == AD1836) {
284 /* left/right diff:PGA/MUX */
285 snd_soc_write(codec, AD1836_ADC_CTRL3, 0x3A);
286 } else {
287 snd_soc_write(codec, AD1836_ADC_CTRL3, 0x00);
288 }
289
290 ret = snd_soc_add_controls(codec, ad183x_dac_controls, num_dacs * 2);
291 if (ret)
292 return ret;
293
294 ret = snd_soc_add_controls(codec, ad183x_adc_controls, num_adcs);
295 if (ret)
296 return ret;
297
298 ret = snd_soc_dapm_new_controls(dapm, ad183x_dac_dapm_widgets, num_dacs);
299 if (ret)
300 return ret;
301
302 ret = snd_soc_dapm_new_controls(dapm, ad183x_adc_dapm_widgets, num_adcs);
303 if (ret)
304 return ret;
305
306 ret = snd_soc_dapm_add_routes(dapm, ad183x_dac_routes, num_dacs);
307 if (ret)
308 return ret;
309
310 ret = snd_soc_dapm_add_routes(dapm, ad183x_adc_routes, num_adcs);
311 if (ret)
312 return ret;
Barry Song7eaae412009-08-13 11:59:32 +0800313
Barry Song7eaae412009-08-13 11:59:32 +0800314 return ret;
315}
316
317/* power down chip */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000318static int ad1836_remove(struct snd_soc_codec *codec)
Barry Song7eaae412009-08-13 11:59:32 +0800319{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000320 /* reset clock control mode */
Lars-Peter Clausen2cf03422011-06-06 13:38:37 +0200321 return snd_soc_update_bits(codec, AD1836_ADC_CTRL2,
322 AD1836_ADC_SERFMT_MASK, 0);
Barry Song7eaae412009-08-13 11:59:32 +0800323}
324
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000325static struct snd_soc_codec_driver soc_codec_dev_ad1836 = {
Barry Song7eaae412009-08-13 11:59:32 +0800326 .probe = ad1836_probe,
327 .remove = ad1836_remove,
Barry Song84549d22010-01-25 16:42:25 +0800328 .suspend = ad1836_soc_suspend,
329 .resume = ad1836_soc_resume,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000330 .reg_cache_size = AD1836_NUM_REGS,
331 .reg_word_size = sizeof(u16),
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200332
333 .controls = ad183x_controls,
334 .num_controls = ARRAY_SIZE(ad183x_controls),
335 .dapm_widgets = ad183x_dapm_widgets,
336 .num_dapm_widgets = ARRAY_SIZE(ad183x_dapm_widgets),
337 .dapm_routes = ad183x_dapm_routes,
338 .num_dapm_routes = ARRAY_SIZE(ad183x_dapm_routes),
Barry Song7eaae412009-08-13 11:59:32 +0800339};
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000340
341static int __devinit ad1836_spi_probe(struct spi_device *spi)
342{
343 struct ad1836_priv *ad1836;
344 int ret;
345
346 ad1836 = kzalloc(sizeof(struct ad1836_priv), GFP_KERNEL);
347 if (ad1836 == NULL)
348 return -ENOMEM;
349
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200350 ad1836->type = spi_get_device_id(spi)->driver_data;
351
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000352 spi_set_drvdata(spi, ad1836);
353 ad1836->control_data = spi;
354 ad1836->control_type = SND_SOC_SPI;
355
356 ret = snd_soc_register_codec(&spi->dev,
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200357 &soc_codec_dev_ad1836, &ad183x_dais[ad1836->type], 1);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000358 if (ret < 0)
359 kfree(ad1836);
360 return ret;
361}
362
363static int __devexit ad1836_spi_remove(struct spi_device *spi)
364{
365 snd_soc_unregister_codec(&spi->dev);
366 kfree(spi_get_drvdata(spi));
367 return 0;
368}
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200369static const struct spi_device_id ad1836_ids[] = {
370 { "ad1835", AD1835 },
371 { "ad1836", AD1836 },
372 { "ad1837", AD1835 },
373 { "ad1838", AD1838 },
374 { "ad1839", AD1838 },
375 { },
376};
377MODULE_DEVICE_TABLE(spi, ad1836_ids);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000378
379static struct spi_driver ad1836_spi_driver = {
380 .driver = {
381 .name = "ad1836-codec",
382 .owner = THIS_MODULE,
383 },
384 .probe = ad1836_spi_probe,
385 .remove = __devexit_p(ad1836_spi_remove),
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200386 .id_table = ad1836_ids,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000387};
Barry Song7eaae412009-08-13 11:59:32 +0800388
389static int __init ad1836_init(void)
390{
391 int ret;
392
393 ret = spi_register_driver(&ad1836_spi_driver);
394 if (ret != 0) {
395 printk(KERN_ERR "Failed to register ad1836 SPI driver: %d\n",
396 ret);
397 }
398
399 return ret;
400}
401module_init(ad1836_init);
402
403static void __exit ad1836_exit(void)
404{
405 spi_unregister_driver(&ad1836_spi_driver);
406}
407module_exit(ad1836_exit);
408
409MODULE_DESCRIPTION("ASoC ad1836 driver");
410MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
411MODULE_LICENSE("GPL");