blob: 9cc6123cf7ea689fdc8bc3e22112ad8c5ac9065f [file] [log] [blame]
Mike Frysinger42f32c52011-06-15 15:29:23 -04001 /*
2 * Audio Codec driver supporting:
3 * AD1835A, AD1836, AD1837A, AD1838A, AD1839A
Barry Song7eaae412009-08-13 11:59:32 +08004 *
Mike Frysinger42f32c52011-06-15 15:29:23 -04005 * Copyright 2009-2011 Analog Devices Inc.
Barry Song7eaae412009-08-13 11:59:32 +08006 *
Mike Frysinger42f32c52011-06-15 15:29:23 -04007 * Licensed under the GPL-2 or later.
Barry Song7eaae412009-08-13 11:59:32 +08008 */
9
10#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Barry Song7eaae412009-08-13 11:59:32 +080012#include <linux/module.h>
Barry Song7eaae412009-08-13 11:59:32 +080013#include <linux/kernel.h>
14#include <linux/device.h>
15#include <sound/core.h>
16#include <sound/pcm.h>
17#include <sound/pcm_params.h>
18#include <sound/initval.h>
19#include <sound/soc.h>
20#include <sound/tlv.h>
Barry Song7eaae412009-08-13 11:59:32 +080021#include <linux/spi/spi.h>
22#include "ad1836.h"
23
Lars-Peter Clausen874ce772011-06-06 13:38:38 +020024enum ad1836_type {
25 AD1835,
26 AD1836,
27 AD1838,
28};
29
Barry Song7eaae412009-08-13 11:59:32 +080030/* codec private data */
31struct ad1836_priv {
Lars-Peter Clausen874ce772011-06-06 13:38:38 +020032 enum ad1836_type type;
Barry Song7eaae412009-08-13 11:59:32 +080033};
34
Barry Song7eaae412009-08-13 11:59:32 +080035/*
36 * AD1836 volume/mute/de-emphasis etc. controls
37 */
38static const char *ad1836_deemp[] = {"None", "44.1kHz", "32kHz", "48kHz"};
39
40static const struct soc_enum ad1836_deemp_enum =
41 SOC_ENUM_SINGLE(AD1836_DAC_CTRL1, 8, 4, ad1836_deemp);
42
Lars-Peter Clausen90bc11d2011-06-06 13:38:36 +020043#define AD1836_DAC_VOLUME(x) \
44 SOC_DOUBLE_R("DAC" #x " Playback Volume", AD1836_DAC_L_VOL(x), \
45 AD1836_DAC_R_VOL(x), 0, 0x3FF, 0)
46
47#define AD1836_DAC_SWITCH(x) \
48 SOC_DOUBLE("DAC" #x " Playback Switch", AD1836_DAC_CTRL2, \
49 AD1836_MUTE_LEFT(x), AD1836_MUTE_RIGHT(x), 1, 1)
50
51#define AD1836_ADC_SWITCH(x) \
52 SOC_DOUBLE("ADC" #x " Capture Switch", AD1836_ADC_CTRL2, \
53 AD1836_MUTE_LEFT(x), AD1836_MUTE_RIGHT(x), 1, 1)
54
Lars-Peter Clausen874ce772011-06-06 13:38:38 +020055static const struct snd_kcontrol_new ad183x_dac_controls[] = {
Lars-Peter Clausen90bc11d2011-06-06 13:38:36 +020056 AD1836_DAC_VOLUME(1),
Lars-Peter Clausen874ce772011-06-06 13:38:38 +020057 AD1836_DAC_SWITCH(1),
Lars-Peter Clausen90bc11d2011-06-06 13:38:36 +020058 AD1836_DAC_VOLUME(2),
Lars-Peter Clausen874ce772011-06-06 13:38:38 +020059 AD1836_DAC_SWITCH(2),
Lars-Peter Clausen90bc11d2011-06-06 13:38:36 +020060 AD1836_DAC_VOLUME(3),
Lars-Peter Clausen874ce772011-06-06 13:38:38 +020061 AD1836_DAC_SWITCH(3),
62 AD1836_DAC_VOLUME(4),
63 AD1836_DAC_SWITCH(4),
64};
Barry Song7eaae412009-08-13 11:59:32 +080065
Lars-Peter Clausen874ce772011-06-06 13:38:38 +020066static const struct snd_soc_dapm_widget ad183x_dac_dapm_widgets[] = {
67 SND_SOC_DAPM_OUTPUT("DAC1OUT"),
68 SND_SOC_DAPM_OUTPUT("DAC2OUT"),
69 SND_SOC_DAPM_OUTPUT("DAC3OUT"),
70 SND_SOC_DAPM_OUTPUT("DAC4OUT"),
71};
72
73static const struct snd_soc_dapm_route ad183x_dac_routes[] = {
74 { "DAC1OUT", NULL, "DAC" },
75 { "DAC2OUT", NULL, "DAC" },
76 { "DAC3OUT", NULL, "DAC" },
77 { "DAC4OUT", NULL, "DAC" },
78};
79
80static const struct snd_kcontrol_new ad183x_adc_controls[] = {
Lars-Peter Clausen90bc11d2011-06-06 13:38:36 +020081 AD1836_ADC_SWITCH(1),
82 AD1836_ADC_SWITCH(2),
Lars-Peter Clausen874ce772011-06-06 13:38:38 +020083 AD1836_ADC_SWITCH(3),
84};
Barry Song7eaae412009-08-13 11:59:32 +080085
Lars-Peter Clausen874ce772011-06-06 13:38:38 +020086static const struct snd_soc_dapm_widget ad183x_adc_dapm_widgets[] = {
87 SND_SOC_DAPM_INPUT("ADC1IN"),
88 SND_SOC_DAPM_INPUT("ADC2IN"),
89};
Barry Song7eaae412009-08-13 11:59:32 +080090
Lars-Peter Clausen874ce772011-06-06 13:38:38 +020091static const struct snd_soc_dapm_route ad183x_adc_routes[] = {
92 { "ADC", NULL, "ADC1IN" },
93 { "ADC", NULL, "ADC2IN" },
94};
95
96static const struct snd_kcontrol_new ad183x_controls[] = {
Barry Song7eaae412009-08-13 11:59:32 +080097 /* ADC high-pass filter */
98 SOC_SINGLE("ADC High Pass Filter Switch", AD1836_ADC_CTRL1,
99 AD1836_ADC_HIGHPASS_FILTER, 1, 0),
100
101 /* DAC de-emphasis */
102 SOC_ENUM("Playback Deemphasis", ad1836_deemp_enum),
103};
104
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200105static const struct snd_soc_dapm_widget ad183x_dapm_widgets[] = {
Barry Song7eaae412009-08-13 11:59:32 +0800106 SND_SOC_DAPM_DAC("DAC", "Playback", AD1836_DAC_CTRL1,
107 AD1836_DAC_POWERDOWN, 1),
108 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
109 SND_SOC_DAPM_SUPPLY("ADC_PWR", AD1836_ADC_CTRL1,
110 AD1836_ADC_POWERDOWN, 1, NULL, 0),
Barry Song7eaae412009-08-13 11:59:32 +0800111};
112
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200113static const struct snd_soc_dapm_route ad183x_dapm_routes[] = {
Barry Song7eaae412009-08-13 11:59:32 +0800114 { "DAC", NULL, "ADC_PWR" },
115 { "ADC", NULL, "ADC_PWR" },
Barry Song7eaae412009-08-13 11:59:32 +0800116};
117
Lars-Peter Clausenf97d0c62011-06-06 13:38:40 +0200118static const DECLARE_TLV_DB_SCALE(ad1836_in_tlv, 0, 300, 0);
119
120static const struct snd_kcontrol_new ad1836_controls[] = {
Lars-Peter Clausen0c8e2912011-06-07 07:02:59 +0200121 SOC_DOUBLE_TLV("ADC2 Capture Volume", AD1836_ADC_CTRL1, 3, 0, 4, 0,
Lars-Peter Clausenf97d0c62011-06-06 13:38:40 +0200122 ad1836_in_tlv),
123};
124
Barry Song7eaae412009-08-13 11:59:32 +0800125/*
126 * DAI ops entries
127 */
128
129static int ad1836_set_dai_fmt(struct snd_soc_dai *codec_dai,
130 unsigned int fmt)
131{
132 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
133 /* at present, we support adc aux mode to interface with
134 * blackfin sport tdm mode
135 */
136 case SND_SOC_DAIFMT_DSP_A:
137 break;
138 default:
139 return -EINVAL;
140 }
141
142 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
143 case SND_SOC_DAIFMT_IB_IF:
144 break;
145 default:
146 return -EINVAL;
147 }
148
149 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
150 /* ALCLK,ABCLK are both output, AD1836 can only be master */
151 case SND_SOC_DAIFMT_CBM_CFM:
152 break;
153 default:
154 return -EINVAL;
155 }
156
157 return 0;
158}
159
160static int ad1836_hw_params(struct snd_pcm_substream *substream,
161 struct snd_pcm_hw_params *params,
162 struct snd_soc_dai *dai)
163{
164 int word_len = 0;
165
166 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000167 struct snd_soc_codec *codec = rtd->codec;
Barry Song7eaae412009-08-13 11:59:32 +0800168
169 /* bit size */
170 switch (params_format(params)) {
171 case SNDRV_PCM_FORMAT_S16_LE:
Lars-Peter Clausen8ca695f2011-06-06 13:38:35 +0200172 word_len = AD1836_WORD_LEN_16;
Barry Song7eaae412009-08-13 11:59:32 +0800173 break;
174 case SNDRV_PCM_FORMAT_S20_3LE:
Lars-Peter Clausen8ca695f2011-06-06 13:38:35 +0200175 word_len = AD1836_WORD_LEN_20;
Barry Song7eaae412009-08-13 11:59:32 +0800176 break;
177 case SNDRV_PCM_FORMAT_S24_LE:
178 case SNDRV_PCM_FORMAT_S32_LE:
Lars-Peter Clausen8ca695f2011-06-06 13:38:35 +0200179 word_len = AD1836_WORD_LEN_24;
Barry Song7eaae412009-08-13 11:59:32 +0800180 break;
181 }
182
Lars-Peter Clausen8ca695f2011-06-06 13:38:35 +0200183 snd_soc_update_bits(codec, AD1836_DAC_CTRL1, AD1836_DAC_WORD_LEN_MASK,
184 word_len << AD1836_DAC_WORD_LEN_OFFSET);
Barry Song7eaae412009-08-13 11:59:32 +0800185
Lars-Peter Clausen8ca695f2011-06-06 13:38:35 +0200186 snd_soc_update_bits(codec, AD1836_ADC_CTRL2, AD1836_ADC_WORD_LEN_MASK,
187 word_len << AD1836_ADC_WORD_OFFSET);
Barry Song7eaae412009-08-13 11:59:32 +0800188
189 return 0;
190}
191
Barry Song84549d22010-01-25 16:42:25 +0800192#ifdef CONFIG_PM
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000193static int ad1836_soc_suspend(struct snd_soc_codec *codec,
Barry Song84549d22010-01-25 16:42:25 +0800194 pm_message_t state)
195{
Barry Song84549d22010-01-25 16:42:25 +0800196 /* reset clock control mode */
Lars-Peter Clausen2cf03422011-06-06 13:38:37 +0200197 return snd_soc_update_bits(codec, AD1836_ADC_CTRL2,
198 AD1836_ADC_SERFMT_MASK, 0);
Barry Song84549d22010-01-25 16:42:25 +0800199}
200
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000201static int ad1836_soc_resume(struct snd_soc_codec *codec)
Barry Song84549d22010-01-25 16:42:25 +0800202{
Barry Song84549d22010-01-25 16:42:25 +0800203 /* restore clock control mode */
Lars-Peter Clausen2cf03422011-06-06 13:38:37 +0200204 return snd_soc_update_bits(codec, AD1836_ADC_CTRL2,
205 AD1836_ADC_SERFMT_MASK, AD1836_ADC_AUX);
Barry Song84549d22010-01-25 16:42:25 +0800206}
207#else
208#define ad1836_soc_suspend NULL
209#define ad1836_soc_resume NULL
210#endif
211
Barry Song7eaae412009-08-13 11:59:32 +0800212static struct snd_soc_dai_ops ad1836_dai_ops = {
213 .hw_params = ad1836_hw_params,
214 .set_fmt = ad1836_set_dai_fmt,
215};
216
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200217#define AD183X_DAI(_name, num_dacs, num_adcs) \
218{ \
219 .name = _name "-hifi", \
220 .playback = { \
221 .stream_name = "Playback", \
222 .channels_min = 2, \
223 .channels_max = (num_dacs) * 2, \
224 .rates = SNDRV_PCM_RATE_48000, \
225 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | \
226 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, \
227 }, \
228 .capture = { \
229 .stream_name = "Capture", \
230 .channels_min = 2, \
231 .channels_max = (num_adcs) * 2, \
232 .rates = SNDRV_PCM_RATE_48000, \
233 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | \
234 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, \
235 }, \
236 .ops = &ad1836_dai_ops, \
237}
238
239static struct snd_soc_dai_driver ad183x_dais[] = {
240 [AD1835] = AD183X_DAI("ad1835", 4, 1),
241 [AD1836] = AD183X_DAI("ad1836", 3, 2),
242 [AD1838] = AD183X_DAI("ad1838", 3, 1),
Barry Song7eaae412009-08-13 11:59:32 +0800243};
Barry Song7eaae412009-08-13 11:59:32 +0800244
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000245static int ad1836_probe(struct snd_soc_codec *codec)
Barry Song7eaae412009-08-13 11:59:32 +0800246{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000247 struct ad1836_priv *ad1836 = snd_soc_codec_get_drvdata(codec);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200248 struct snd_soc_dapm_context *dapm = &codec->dapm;
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200249 int num_dacs, num_adcs;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000250 int ret = 0;
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200251 int i;
252
253 num_dacs = ad183x_dais[ad1836->type].playback.channels_max / 2;
254 num_adcs = ad183x_dais[ad1836->type].capture.channels_max / 2;
Barry Song7eaae412009-08-13 11:59:32 +0800255
Barry Song63b62ab2010-01-27 11:46:17 +0800256 ret = snd_soc_codec_set_cache_io(codec, 4, 12, SND_SOC_SPI);
257 if (ret < 0) {
258 dev_err(codec->dev, "failed to set cache I/O: %d\n",
259 ret);
Barry Song63b62ab2010-01-27 11:46:17 +0800260 return ret;
261 }
262
Barry Song7eaae412009-08-13 11:59:32 +0800263 /* default setting for ad1836 */
264 /* de-emphasis: 48kHz, power-on dac */
Barry Song63b62ab2010-01-27 11:46:17 +0800265 snd_soc_write(codec, AD1836_DAC_CTRL1, 0x300);
Barry Song7eaae412009-08-13 11:59:32 +0800266 /* unmute dac channels */
Barry Song63b62ab2010-01-27 11:46:17 +0800267 snd_soc_write(codec, AD1836_DAC_CTRL2, 0x0);
Barry Song7eaae412009-08-13 11:59:32 +0800268 /* high-pass filter enable, power-on adc */
Barry Song63b62ab2010-01-27 11:46:17 +0800269 snd_soc_write(codec, AD1836_ADC_CTRL1, 0x100);
Barry Song7eaae412009-08-13 11:59:32 +0800270 /* unmute adc channles, adc aux mode */
Barry Song63b62ab2010-01-27 11:46:17 +0800271 snd_soc_write(codec, AD1836_ADC_CTRL2, 0x180);
Barry Song7eaae412009-08-13 11:59:32 +0800272 /* volume */
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200273 for (i = 1; i <= num_dacs; ++i) {
274 snd_soc_write(codec, AD1836_DAC_L_VOL(i), 0x3FF);
275 snd_soc_write(codec, AD1836_DAC_R_VOL(i), 0x3FF);
276 }
Barry Song7eaae412009-08-13 11:59:32 +0800277
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200278 if (ad1836->type == AD1836) {
279 /* left/right diff:PGA/MUX */
280 snd_soc_write(codec, AD1836_ADC_CTRL3, 0x3A);
Lars-Peter Clausenf97d0c62011-06-06 13:38:40 +0200281 ret = snd_soc_add_controls(codec, ad1836_controls,
282 ARRAY_SIZE(ad1836_controls));
283 if (ret)
284 return ret;
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200285 } else {
286 snd_soc_write(codec, AD1836_ADC_CTRL3, 0x00);
287 }
288
289 ret = snd_soc_add_controls(codec, ad183x_dac_controls, num_dacs * 2);
290 if (ret)
291 return ret;
292
293 ret = snd_soc_add_controls(codec, ad183x_adc_controls, num_adcs);
294 if (ret)
295 return ret;
296
297 ret = snd_soc_dapm_new_controls(dapm, ad183x_dac_dapm_widgets, num_dacs);
298 if (ret)
299 return ret;
300
301 ret = snd_soc_dapm_new_controls(dapm, ad183x_adc_dapm_widgets, num_adcs);
302 if (ret)
303 return ret;
304
305 ret = snd_soc_dapm_add_routes(dapm, ad183x_dac_routes, num_dacs);
306 if (ret)
307 return ret;
308
309 ret = snd_soc_dapm_add_routes(dapm, ad183x_adc_routes, num_adcs);
310 if (ret)
311 return ret;
Barry Song7eaae412009-08-13 11:59:32 +0800312
Barry Song7eaae412009-08-13 11:59:32 +0800313 return ret;
314}
315
316/* power down chip */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000317static int ad1836_remove(struct snd_soc_codec *codec)
Barry Song7eaae412009-08-13 11:59:32 +0800318{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000319 /* reset clock control mode */
Lars-Peter Clausen2cf03422011-06-06 13:38:37 +0200320 return snd_soc_update_bits(codec, AD1836_ADC_CTRL2,
321 AD1836_ADC_SERFMT_MASK, 0);
Barry Song7eaae412009-08-13 11:59:32 +0800322}
323
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000324static struct snd_soc_codec_driver soc_codec_dev_ad1836 = {
Mike Frysingerd4d80f52011-06-15 15:29:20 -0400325 .probe = ad1836_probe,
326 .remove = ad1836_remove,
Barry Song84549d22010-01-25 16:42:25 +0800327 .suspend = ad1836_soc_suspend,
328 .resume = ad1836_soc_resume,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000329 .reg_cache_size = AD1836_NUM_REGS,
330 .reg_word_size = sizeof(u16),
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200331
332 .controls = ad183x_controls,
333 .num_controls = ARRAY_SIZE(ad183x_controls),
334 .dapm_widgets = ad183x_dapm_widgets,
335 .num_dapm_widgets = ARRAY_SIZE(ad183x_dapm_widgets),
336 .dapm_routes = ad183x_dapm_routes,
337 .num_dapm_routes = ARRAY_SIZE(ad183x_dapm_routes),
Barry Song7eaae412009-08-13 11:59:32 +0800338};
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000339
340static int __devinit ad1836_spi_probe(struct spi_device *spi)
341{
342 struct ad1836_priv *ad1836;
343 int ret;
344
345 ad1836 = kzalloc(sizeof(struct ad1836_priv), GFP_KERNEL);
346 if (ad1836 == NULL)
347 return -ENOMEM;
348
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200349 ad1836->type = spi_get_device_id(spi)->driver_data;
350
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000351 spi_set_drvdata(spi, ad1836);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000352
353 ret = snd_soc_register_codec(&spi->dev,
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200354 &soc_codec_dev_ad1836, &ad183x_dais[ad1836->type], 1);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000355 if (ret < 0)
356 kfree(ad1836);
357 return ret;
358}
359
360static int __devexit ad1836_spi_remove(struct spi_device *spi)
361{
362 snd_soc_unregister_codec(&spi->dev);
363 kfree(spi_get_drvdata(spi));
364 return 0;
365}
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200366static const struct spi_device_id ad1836_ids[] = {
367 { "ad1835", AD1835 },
368 { "ad1836", AD1836 },
369 { "ad1837", AD1835 },
370 { "ad1838", AD1838 },
371 { "ad1839", AD1838 },
372 { },
373};
374MODULE_DEVICE_TABLE(spi, ad1836_ids);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000375
376static struct spi_driver ad1836_spi_driver = {
377 .driver = {
Mike Frysinger06790592011-06-15 15:29:21 -0400378 .name = "ad1836",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000379 .owner = THIS_MODULE,
380 },
381 .probe = ad1836_spi_probe,
382 .remove = __devexit_p(ad1836_spi_remove),
Lars-Peter Clausen874ce772011-06-06 13:38:38 +0200383 .id_table = ad1836_ids,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000384};
Barry Song7eaae412009-08-13 11:59:32 +0800385
386static int __init ad1836_init(void)
387{
Mike Frysinger15e87052011-06-15 15:29:19 -0400388 return spi_register_driver(&ad1836_spi_driver);
Barry Song7eaae412009-08-13 11:59:32 +0800389}
390module_init(ad1836_init);
391
392static void __exit ad1836_exit(void)
393{
394 spi_unregister_driver(&ad1836_spi_driver);
395}
396module_exit(ad1836_exit);
397
398MODULE_DESCRIPTION("ASoC ad1836 driver");
399MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
400MODULE_LICENSE("GPL");