2 * Audio Codec driver supporting:
3 * AD1835A, AD1836, AD1837A, AD1838A, AD1839A
5 * Copyright 2009-2011 Analog Devices Inc.
7 * Licensed under the GPL-2 or later.
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #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>
21 #include <linux/spi/spi.h>
30 /* codec private data */
32 enum ad1836_type type
;
36 * AD1836 volume/mute/de-emphasis etc. controls
38 static const char *ad1836_deemp
[] = {"None", "44.1kHz", "32kHz", "48kHz"};
40 static const struct soc_enum ad1836_deemp_enum
=
41 SOC_ENUM_SINGLE(AD1836_DAC_CTRL1
, 8, 4, ad1836_deemp
);
43 #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)
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)
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)
55 static const struct snd_kcontrol_new ad183x_dac_controls
[] = {
66 static 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"),
73 static 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" },
80 static const struct snd_kcontrol_new ad183x_adc_controls
[] = {
86 static const struct snd_soc_dapm_widget ad183x_adc_dapm_widgets
[] = {
87 SND_SOC_DAPM_INPUT("ADC1IN"),
88 SND_SOC_DAPM_INPUT("ADC2IN"),
91 static const struct snd_soc_dapm_route ad183x_adc_routes
[] = {
92 { "ADC", NULL
, "ADC1IN" },
93 { "ADC", NULL
, "ADC2IN" },
96 static const struct snd_kcontrol_new ad183x_controls
[] = {
97 /* ADC high-pass filter */
98 SOC_SINGLE("ADC High Pass Filter Switch", AD1836_ADC_CTRL1
,
99 AD1836_ADC_HIGHPASS_FILTER
, 1, 0),
101 /* DAC de-emphasis */
102 SOC_ENUM("Playback Deemphasis", ad1836_deemp_enum
),
105 static const struct snd_soc_dapm_widget ad183x_dapm_widgets
[] = {
106 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),
113 static const struct snd_soc_dapm_route ad183x_dapm_routes
[] = {
114 { "DAC", NULL
, "ADC_PWR" },
115 { "ADC", NULL
, "ADC_PWR" },
118 static const DECLARE_TLV_DB_SCALE(ad1836_in_tlv
, 0, 300, 0);
120 static const struct snd_kcontrol_new ad1836_controls
[] = {
121 SOC_DOUBLE_TLV("ADC2 Capture Volume", AD1836_ADC_CTRL1
, 3, 0, 4, 0,
129 static int ad1836_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
132 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
133 /* at present, we support adc aux mode to interface with
134 * blackfin sport tdm mode
136 case SND_SOC_DAIFMT_DSP_A
:
142 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
143 case SND_SOC_DAIFMT_IB_IF
:
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
:
160 static int ad1836_hw_params(struct snd_pcm_substream
*substream
,
161 struct snd_pcm_hw_params
*params
,
162 struct snd_soc_dai
*dai
)
165 struct snd_soc_codec
*codec
= dai
->codec
;
168 switch (params_format(params
)) {
169 case SNDRV_PCM_FORMAT_S16_LE
:
170 word_len
= AD1836_WORD_LEN_16
;
172 case SNDRV_PCM_FORMAT_S20_3LE
:
173 word_len
= AD1836_WORD_LEN_20
;
175 case SNDRV_PCM_FORMAT_S24_LE
:
176 case SNDRV_PCM_FORMAT_S32_LE
:
177 word_len
= AD1836_WORD_LEN_24
;
181 snd_soc_update_bits(codec
, AD1836_DAC_CTRL1
, AD1836_DAC_WORD_LEN_MASK
,
182 word_len
<< AD1836_DAC_WORD_LEN_OFFSET
);
184 snd_soc_update_bits(codec
, AD1836_ADC_CTRL2
, AD1836_ADC_WORD_LEN_MASK
,
185 word_len
<< AD1836_ADC_WORD_OFFSET
);
190 static const struct snd_soc_dai_ops ad1836_dai_ops
= {
191 .hw_params
= ad1836_hw_params
,
192 .set_fmt
= ad1836_set_dai_fmt
,
195 #define AD183X_DAI(_name, num_dacs, num_adcs) \
197 .name = _name "-hifi", \
199 .stream_name = "Playback", \
201 .channels_max = (num_dacs) * 2, \
202 .rates = SNDRV_PCM_RATE_48000, \
203 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | \
204 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, \
207 .stream_name = "Capture", \
209 .channels_max = (num_adcs) * 2, \
210 .rates = SNDRV_PCM_RATE_48000, \
211 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | \
212 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, \
214 .ops = &ad1836_dai_ops, \
217 static struct snd_soc_dai_driver ad183x_dais
[] = {
218 [AD1835
] = AD183X_DAI("ad1835", 4, 1),
219 [AD1836
] = AD183X_DAI("ad1836", 3, 2),
220 [AD1838
] = AD183X_DAI("ad1838", 3, 1),
224 static int ad1836_suspend(struct snd_soc_codec
*codec
)
226 /* reset clock control mode */
227 return snd_soc_update_bits(codec
, AD1836_ADC_CTRL2
,
228 AD1836_ADC_SERFMT_MASK
, 0);
231 static int ad1836_resume(struct snd_soc_codec
*codec
)
233 /* restore clock control mode */
234 return snd_soc_update_bits(codec
, AD1836_ADC_CTRL2
,
235 AD1836_ADC_SERFMT_MASK
, AD1836_ADC_AUX
);
238 #define ad1836_suspend NULL
239 #define ad1836_resume NULL
242 static int ad1836_probe(struct snd_soc_codec
*codec
)
244 struct ad1836_priv
*ad1836
= snd_soc_codec_get_drvdata(codec
);
245 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
246 int num_dacs
, num_adcs
;
250 num_dacs
= ad183x_dais
[ad1836
->type
].playback
.channels_max
/ 2;
251 num_adcs
= ad183x_dais
[ad1836
->type
].capture
.channels_max
/ 2;
253 ret
= snd_soc_codec_set_cache_io(codec
, 4, 12, SND_SOC_SPI
);
255 dev_err(codec
->dev
, "failed to set cache I/O: %d\n",
260 /* default setting for ad1836 */
261 /* de-emphasis: 48kHz, power-on dac */
262 snd_soc_write(codec
, AD1836_DAC_CTRL1
, 0x300);
263 /* unmute dac channels */
264 snd_soc_write(codec
, AD1836_DAC_CTRL2
, 0x0);
265 /* high-pass filter enable, power-on adc */
266 snd_soc_write(codec
, AD1836_ADC_CTRL1
, 0x100);
267 /* unmute adc channles, adc aux mode */
268 snd_soc_write(codec
, AD1836_ADC_CTRL2
, 0x180);
270 for (i
= 1; i
<= num_dacs
; ++i
) {
271 snd_soc_write(codec
, AD1836_DAC_L_VOL(i
), 0x3FF);
272 snd_soc_write(codec
, AD1836_DAC_R_VOL(i
), 0x3FF);
275 if (ad1836
->type
== AD1836
) {
276 /* left/right diff:PGA/MUX */
277 snd_soc_write(codec
, AD1836_ADC_CTRL3
, 0x3A);
278 ret
= snd_soc_add_codec_controls(codec
, ad1836_controls
,
279 ARRAY_SIZE(ad1836_controls
));
283 snd_soc_write(codec
, AD1836_ADC_CTRL3
, 0x00);
286 ret
= snd_soc_add_codec_controls(codec
, ad183x_dac_controls
, num_dacs
* 2);
290 ret
= snd_soc_add_codec_controls(codec
, ad183x_adc_controls
, num_adcs
);
294 ret
= snd_soc_dapm_new_controls(dapm
, ad183x_dac_dapm_widgets
, num_dacs
);
298 ret
= snd_soc_dapm_new_controls(dapm
, ad183x_adc_dapm_widgets
, num_adcs
);
302 ret
= snd_soc_dapm_add_routes(dapm
, ad183x_dac_routes
, num_dacs
);
306 ret
= snd_soc_dapm_add_routes(dapm
, ad183x_adc_routes
, num_adcs
);
313 /* power down chip */
314 static int ad1836_remove(struct snd_soc_codec
*codec
)
316 /* reset clock control mode */
317 return snd_soc_update_bits(codec
, AD1836_ADC_CTRL2
,
318 AD1836_ADC_SERFMT_MASK
, 0);
321 static struct snd_soc_codec_driver soc_codec_dev_ad1836
= {
322 .probe
= ad1836_probe
,
323 .remove
= ad1836_remove
,
324 .suspend
= ad1836_suspend
,
325 .resume
= ad1836_resume
,
326 .reg_cache_size
= AD1836_NUM_REGS
,
327 .reg_word_size
= sizeof(u16
),
329 .controls
= ad183x_controls
,
330 .num_controls
= ARRAY_SIZE(ad183x_controls
),
331 .dapm_widgets
= ad183x_dapm_widgets
,
332 .num_dapm_widgets
= ARRAY_SIZE(ad183x_dapm_widgets
),
333 .dapm_routes
= ad183x_dapm_routes
,
334 .num_dapm_routes
= ARRAY_SIZE(ad183x_dapm_routes
),
337 static int __devinit
ad1836_spi_probe(struct spi_device
*spi
)
339 struct ad1836_priv
*ad1836
;
342 ad1836
= devm_kzalloc(&spi
->dev
, sizeof(struct ad1836_priv
),
347 ad1836
->type
= spi_get_device_id(spi
)->driver_data
;
349 spi_set_drvdata(spi
, ad1836
);
351 ret
= snd_soc_register_codec(&spi
->dev
,
352 &soc_codec_dev_ad1836
, &ad183x_dais
[ad1836
->type
], 1);
356 static int __devexit
ad1836_spi_remove(struct spi_device
*spi
)
358 snd_soc_unregister_codec(&spi
->dev
);
362 static const struct spi_device_id ad1836_ids
[] = {
363 { "ad1835", AD1835
},
364 { "ad1836", AD1836
},
365 { "ad1837", AD1835
},
366 { "ad1838", AD1838
},
367 { "ad1839", AD1838
},
370 MODULE_DEVICE_TABLE(spi
, ad1836_ids
);
372 static struct spi_driver ad1836_spi_driver
= {
375 .owner
= THIS_MODULE
,
377 .probe
= ad1836_spi_probe
,
378 .remove
= __devexit_p(ad1836_spi_remove
),
379 .id_table
= ad1836_ids
,
382 static int __init
ad1836_init(void)
384 return spi_register_driver(&ad1836_spi_driver
);
386 module_init(ad1836_init
);
388 static void __exit
ad1836_exit(void)
390 spi_unregister_driver(&ad1836_spi_driver
);
392 module_exit(ad1836_exit
);
394 MODULE_DESCRIPTION("ASoC ad1836 driver");
395 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
396 MODULE_LICENSE("GPL");