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>
22 #include <linux/regmap.h>
32 /* codec private data */
34 enum ad1836_type type
;
35 struct regmap
*regmap
;
39 * AD1836 volume/mute/de-emphasis etc. controls
41 static const char *ad1836_deemp
[] = {"None", "44.1kHz", "32kHz", "48kHz"};
43 static SOC_ENUM_SINGLE_DECL(ad1836_deemp_enum
,
44 AD1836_DAC_CTRL1
, 8, ad1836_deemp
);
46 #define AD1836_DAC_VOLUME(x) \
47 SOC_DOUBLE_R("DAC" #x " Playback Volume", AD1836_DAC_L_VOL(x), \
48 AD1836_DAC_R_VOL(x), 0, 0x3FF, 0)
50 #define AD1836_DAC_SWITCH(x) \
51 SOC_DOUBLE("DAC" #x " Playback Switch", AD1836_DAC_CTRL2, \
52 AD1836_MUTE_LEFT(x), AD1836_MUTE_RIGHT(x), 1, 1)
54 #define AD1836_ADC_SWITCH(x) \
55 SOC_DOUBLE("ADC" #x " Capture Switch", AD1836_ADC_CTRL2, \
56 AD1836_MUTE_LEFT(x), AD1836_MUTE_RIGHT(x), 1, 1)
58 static const struct snd_kcontrol_new ad183x_dac_controls
[] = {
69 static const struct snd_soc_dapm_widget ad183x_dac_dapm_widgets
[] = {
70 SND_SOC_DAPM_OUTPUT("DAC1OUT"),
71 SND_SOC_DAPM_OUTPUT("DAC2OUT"),
72 SND_SOC_DAPM_OUTPUT("DAC3OUT"),
73 SND_SOC_DAPM_OUTPUT("DAC4OUT"),
76 static const struct snd_soc_dapm_route ad183x_dac_routes
[] = {
77 { "DAC1OUT", NULL
, "DAC" },
78 { "DAC2OUT", NULL
, "DAC" },
79 { "DAC3OUT", NULL
, "DAC" },
80 { "DAC4OUT", NULL
, "DAC" },
83 static const struct snd_kcontrol_new ad183x_adc_controls
[] = {
89 static const struct snd_soc_dapm_widget ad183x_adc_dapm_widgets
[] = {
90 SND_SOC_DAPM_INPUT("ADC1IN"),
91 SND_SOC_DAPM_INPUT("ADC2IN"),
94 static const struct snd_soc_dapm_route ad183x_adc_routes
[] = {
95 { "ADC", NULL
, "ADC1IN" },
96 { "ADC", NULL
, "ADC2IN" },
99 static const struct snd_kcontrol_new ad183x_controls
[] = {
100 /* ADC high-pass filter */
101 SOC_SINGLE("ADC High Pass Filter Switch", AD1836_ADC_CTRL1
,
102 AD1836_ADC_HIGHPASS_FILTER
, 1, 0),
104 /* DAC de-emphasis */
105 SOC_ENUM("Playback Deemphasis", ad1836_deemp_enum
),
108 static const struct snd_soc_dapm_widget ad183x_dapm_widgets
[] = {
109 SND_SOC_DAPM_DAC("DAC", "Playback", AD1836_DAC_CTRL1
,
110 AD1836_DAC_POWERDOWN
, 1),
111 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM
, 0, 0),
112 SND_SOC_DAPM_SUPPLY("ADC_PWR", AD1836_ADC_CTRL1
,
113 AD1836_ADC_POWERDOWN
, 1, NULL
, 0),
116 static const struct snd_soc_dapm_route ad183x_dapm_routes
[] = {
117 { "DAC", NULL
, "ADC_PWR" },
118 { "ADC", NULL
, "ADC_PWR" },
121 static const DECLARE_TLV_DB_SCALE(ad1836_in_tlv
, 0, 300, 0);
123 static const struct snd_kcontrol_new ad1836_controls
[] = {
124 SOC_DOUBLE_TLV("ADC2 Capture Volume", AD1836_ADC_CTRL1
, 3, 0, 4, 0,
132 static int ad1836_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
135 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
136 /* at present, we support adc aux mode to interface with
137 * blackfin sport tdm mode
139 case SND_SOC_DAIFMT_DSP_A
:
145 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
146 case SND_SOC_DAIFMT_IB_IF
:
152 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
153 /* ALCLK,ABCLK are both output, AD1836 can only be master */
154 case SND_SOC_DAIFMT_CBM_CFM
:
163 static int ad1836_hw_params(struct snd_pcm_substream
*substream
,
164 struct snd_pcm_hw_params
*params
,
165 struct snd_soc_dai
*dai
)
167 struct ad1836_priv
*ad1836
= snd_soc_codec_get_drvdata(dai
->codec
);
171 switch (params_width(params
)) {
173 word_len
= AD1836_WORD_LEN_16
;
176 word_len
= AD1836_WORD_LEN_20
;
180 word_len
= AD1836_WORD_LEN_24
;
186 regmap_update_bits(ad1836
->regmap
, AD1836_DAC_CTRL1
,
187 AD1836_DAC_WORD_LEN_MASK
,
188 word_len
<< AD1836_DAC_WORD_LEN_OFFSET
);
190 regmap_update_bits(ad1836
->regmap
, AD1836_ADC_CTRL2
,
191 AD1836_ADC_WORD_LEN_MASK
,
192 word_len
<< AD1836_ADC_WORD_OFFSET
);
197 static const struct snd_soc_dai_ops ad1836_dai_ops
= {
198 .hw_params
= ad1836_hw_params
,
199 .set_fmt
= ad1836_set_dai_fmt
,
202 #define AD183X_DAI(_name, num_dacs, num_adcs) \
204 .name = _name "-hifi", \
206 .stream_name = "Playback", \
208 .channels_max = (num_dacs) * 2, \
209 .rates = SNDRV_PCM_RATE_48000, \
210 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | \
211 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, \
214 .stream_name = "Capture", \
216 .channels_max = (num_adcs) * 2, \
217 .rates = SNDRV_PCM_RATE_48000, \
218 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | \
219 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, \
221 .ops = &ad1836_dai_ops, \
224 static struct snd_soc_dai_driver ad183x_dais
[] = {
225 [AD1835
] = AD183X_DAI("ad1835", 4, 1),
226 [AD1836
] = AD183X_DAI("ad1836", 3, 2),
227 [AD1838
] = AD183X_DAI("ad1838", 3, 1),
231 static int ad1836_suspend(struct snd_soc_codec
*codec
)
233 struct ad1836_priv
*ad1836
= snd_soc_codec_get_drvdata(codec
);
234 /* reset clock control mode */
235 return regmap_update_bits(ad1836
->regmap
, AD1836_ADC_CTRL2
,
236 AD1836_ADC_SERFMT_MASK
, 0);
239 static int ad1836_resume(struct snd_soc_codec
*codec
)
241 struct ad1836_priv
*ad1836
= snd_soc_codec_get_drvdata(codec
);
242 /* restore clock control mode */
243 return regmap_update_bits(ad1836
->regmap
, AD1836_ADC_CTRL2
,
244 AD1836_ADC_SERFMT_MASK
, AD1836_ADC_AUX
);
247 #define ad1836_suspend NULL
248 #define ad1836_resume NULL
251 static int ad1836_probe(struct snd_soc_codec
*codec
)
253 struct ad1836_priv
*ad1836
= snd_soc_codec_get_drvdata(codec
);
254 struct snd_soc_dapm_context
*dapm
= snd_soc_codec_get_dapm(codec
);
255 int num_dacs
, num_adcs
;
259 num_dacs
= ad183x_dais
[ad1836
->type
].playback
.channels_max
/ 2;
260 num_adcs
= ad183x_dais
[ad1836
->type
].capture
.channels_max
/ 2;
262 /* default setting for ad1836 */
263 /* de-emphasis: 48kHz, power-on dac */
264 regmap_write(ad1836
->regmap
, AD1836_DAC_CTRL1
, 0x300);
265 /* unmute dac channels */
266 regmap_write(ad1836
->regmap
, AD1836_DAC_CTRL2
, 0x0);
267 /* high-pass filter enable, power-on adc */
268 regmap_write(ad1836
->regmap
, AD1836_ADC_CTRL1
, 0x100);
269 /* unmute adc channles, adc aux mode */
270 regmap_write(ad1836
->regmap
, AD1836_ADC_CTRL2
, 0x180);
272 for (i
= 1; i
<= num_dacs
; ++i
) {
273 regmap_write(ad1836
->regmap
, AD1836_DAC_L_VOL(i
), 0x3FF);
274 regmap_write(ad1836
->regmap
, AD1836_DAC_R_VOL(i
), 0x3FF);
277 if (ad1836
->type
== AD1836
) {
278 /* left/right diff:PGA/MUX */
279 regmap_write(ad1836
->regmap
, AD1836_ADC_CTRL3
, 0x3A);
280 ret
= snd_soc_add_codec_controls(codec
, ad1836_controls
,
281 ARRAY_SIZE(ad1836_controls
));
285 regmap_write(ad1836
->regmap
, AD1836_ADC_CTRL3
, 0x00);
288 ret
= snd_soc_add_codec_controls(codec
, ad183x_dac_controls
, num_dacs
* 2);
292 ret
= snd_soc_add_codec_controls(codec
, ad183x_adc_controls
, num_adcs
);
296 ret
= snd_soc_dapm_new_controls(dapm
, ad183x_dac_dapm_widgets
, num_dacs
);
300 ret
= snd_soc_dapm_new_controls(dapm
, ad183x_adc_dapm_widgets
, num_adcs
);
304 ret
= snd_soc_dapm_add_routes(dapm
, ad183x_dac_routes
, num_dacs
);
308 ret
= snd_soc_dapm_add_routes(dapm
, ad183x_adc_routes
, num_adcs
);
315 /* power down chip */
316 static int ad1836_remove(struct snd_soc_codec
*codec
)
318 struct ad1836_priv
*ad1836
= snd_soc_codec_get_drvdata(codec
);
319 /* reset clock control mode */
320 return regmap_update_bits(ad1836
->regmap
, AD1836_ADC_CTRL2
,
321 AD1836_ADC_SERFMT_MASK
, 0);
324 static struct snd_soc_codec_driver soc_codec_dev_ad1836
= {
325 .probe
= ad1836_probe
,
326 .remove
= ad1836_remove
,
327 .suspend
= ad1836_suspend
,
328 .resume
= ad1836_resume
,
330 .component_driver
= {
331 .controls
= ad183x_controls
,
332 .num_controls
= ARRAY_SIZE(ad183x_controls
),
333 .dapm_widgets
= ad183x_dapm_widgets
,
334 .num_dapm_widgets
= ARRAY_SIZE(ad183x_dapm_widgets
),
335 .dapm_routes
= ad183x_dapm_routes
,
336 .num_dapm_routes
= ARRAY_SIZE(ad183x_dapm_routes
),
340 static const struct reg_default ad1836_reg_defaults
[] = {
341 { AD1836_DAC_CTRL1
, 0x0000 },
342 { AD1836_DAC_CTRL2
, 0x0000 },
343 { AD1836_DAC_L_VOL(0), 0x0000 },
344 { AD1836_DAC_R_VOL(0), 0x0000 },
345 { AD1836_DAC_L_VOL(1), 0x0000 },
346 { AD1836_DAC_R_VOL(1), 0x0000 },
347 { AD1836_DAC_L_VOL(2), 0x0000 },
348 { AD1836_DAC_R_VOL(2), 0x0000 },
349 { AD1836_DAC_L_VOL(3), 0x0000 },
350 { AD1836_DAC_R_VOL(3), 0x0000 },
351 { AD1836_ADC_CTRL1
, 0x0000 },
352 { AD1836_ADC_CTRL2
, 0x0000 },
353 { AD1836_ADC_CTRL3
, 0x0000 },
356 static const struct regmap_config ad1836_regmap_config
= {
359 .read_flag_mask
= 0x08,
361 .max_register
= AD1836_ADC_CTRL3
,
362 .reg_defaults
= ad1836_reg_defaults
,
363 .num_reg_defaults
= ARRAY_SIZE(ad1836_reg_defaults
),
364 .cache_type
= REGCACHE_RBTREE
,
367 static int ad1836_spi_probe(struct spi_device
*spi
)
369 struct ad1836_priv
*ad1836
;
372 ad1836
= devm_kzalloc(&spi
->dev
, sizeof(struct ad1836_priv
),
377 ad1836
->regmap
= devm_regmap_init_spi(spi
, &ad1836_regmap_config
);
378 if (IS_ERR(ad1836
->regmap
))
379 return PTR_ERR(ad1836
->regmap
);
381 ad1836
->type
= spi_get_device_id(spi
)->driver_data
;
383 spi_set_drvdata(spi
, ad1836
);
385 ret
= snd_soc_register_codec(&spi
->dev
,
386 &soc_codec_dev_ad1836
, &ad183x_dais
[ad1836
->type
], 1);
390 static int ad1836_spi_remove(struct spi_device
*spi
)
392 snd_soc_unregister_codec(&spi
->dev
);
396 static const struct spi_device_id ad1836_ids
[] = {
397 { "ad1835", AD1835
},
398 { "ad1836", AD1836
},
399 { "ad1837", AD1835
},
400 { "ad1838", AD1838
},
401 { "ad1839", AD1838
},
404 MODULE_DEVICE_TABLE(spi
, ad1836_ids
);
406 static struct spi_driver ad1836_spi_driver
= {
410 .probe
= ad1836_spi_probe
,
411 .remove
= ad1836_spi_remove
,
412 .id_table
= ad1836_ids
,
415 module_spi_driver(ad1836_spi_driver
);
417 MODULE_DESCRIPTION("ASoC ad1836 driver");
418 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
419 MODULE_LICENSE("GPL");