2 * ADAV80X Audio Codec driver supporting ADAV801, ADAV803
4 * Copyright 2011 Analog Devices Inc.
5 * Author: Yi Li <yi.li@analog.com>
6 * Author: Lars-Peter Clausen <lars@metafoo.de>
8 * Licensed under the GPL-2 or later.
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/i2c.h>
15 #include <linux/spi/spi.h>
16 #include <linux/slab.h>
17 #include <sound/core.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/tlv.h>
21 #include <sound/soc.h>
25 #define ADAV80X_PLAYBACK_CTRL 0x04
26 #define ADAV80X_AUX_IN_CTRL 0x05
27 #define ADAV80X_REC_CTRL 0x06
28 #define ADAV80X_AUX_OUT_CTRL 0x07
29 #define ADAV80X_DPATH_CTRL1 0x62
30 #define ADAV80X_DPATH_CTRL2 0x63
31 #define ADAV80X_DAC_CTRL1 0x64
32 #define ADAV80X_DAC_CTRL2 0x65
33 #define ADAV80X_DAC_CTRL3 0x66
34 #define ADAV80X_DAC_L_VOL 0x68
35 #define ADAV80X_DAC_R_VOL 0x69
36 #define ADAV80X_PGA_L_VOL 0x6c
37 #define ADAV80X_PGA_R_VOL 0x6d
38 #define ADAV80X_ADC_CTRL1 0x6e
39 #define ADAV80X_ADC_CTRL2 0x6f
40 #define ADAV80X_ADC_L_VOL 0x70
41 #define ADAV80X_ADC_R_VOL 0x71
42 #define ADAV80X_PLL_CTRL1 0x74
43 #define ADAV80X_PLL_CTRL2 0x75
44 #define ADAV80X_ICLK_CTRL1 0x76
45 #define ADAV80X_ICLK_CTRL2 0x77
46 #define ADAV80X_PLL_CLK_SRC 0x78
47 #define ADAV80X_PLL_OUTE 0x7a
49 #define ADAV80X_PLL_CLK_SRC_PLL_XIN(pll) 0x00
50 #define ADAV80X_PLL_CLK_SRC_PLL_MCLKI(pll) (0x40 << (pll))
51 #define ADAV80X_PLL_CLK_SRC_PLL_MASK(pll) (0x40 << (pll))
53 #define ADAV80X_ICLK_CTRL1_DAC_SRC(src) ((src) << 5)
54 #define ADAV80X_ICLK_CTRL1_ADC_SRC(src) ((src) << 2)
55 #define ADAV80X_ICLK_CTRL1_ICLK2_SRC(src) (src)
56 #define ADAV80X_ICLK_CTRL2_ICLK1_SRC(src) ((src) << 3)
58 #define ADAV80X_PLL_CTRL1_PLLDIV 0x10
59 #define ADAV80X_PLL_CTRL1_PLLPD(pll) (0x04 << (pll))
60 #define ADAV80X_PLL_CTRL1_XTLPD 0x02
62 #define ADAV80X_PLL_CTRL2_FIELD(pll, x) ((x) << ((pll) * 4))
64 #define ADAV80X_PLL_CTRL2_FS_48(pll) ADAV80X_PLL_CTRL2_FIELD((pll), 0x00)
65 #define ADAV80X_PLL_CTRL2_FS_32(pll) ADAV80X_PLL_CTRL2_FIELD((pll), 0x08)
66 #define ADAV80X_PLL_CTRL2_FS_44(pll) ADAV80X_PLL_CTRL2_FIELD((pll), 0x0c)
68 #define ADAV80X_PLL_CTRL2_SEL(pll) ADAV80X_PLL_CTRL2_FIELD((pll), 0x02)
69 #define ADAV80X_PLL_CTRL2_DOUB(pll) ADAV80X_PLL_CTRL2_FIELD((pll), 0x01)
70 #define ADAV80X_PLL_CTRL2_PLL_MASK(pll) ADAV80X_PLL_CTRL2_FIELD((pll), 0x0f)
72 #define ADAV80X_ADC_CTRL1_MODULATOR_MASK 0x80
73 #define ADAV80X_ADC_CTRL1_MODULATOR_128FS 0x00
74 #define ADAV80X_ADC_CTRL1_MODULATOR_64FS 0x80
76 #define ADAV80X_DAC_CTRL1_PD 0x80
78 #define ADAV80X_DAC_CTRL2_DIV1 0x00
79 #define ADAV80X_DAC_CTRL2_DIV1_5 0x10
80 #define ADAV80X_DAC_CTRL2_DIV2 0x20
81 #define ADAV80X_DAC_CTRL2_DIV3 0x30
82 #define ADAV80X_DAC_CTRL2_DIV_MASK 0x30
84 #define ADAV80X_DAC_CTRL2_INTERPOL_256FS 0x00
85 #define ADAV80X_DAC_CTRL2_INTERPOL_128FS 0x40
86 #define ADAV80X_DAC_CTRL2_INTERPOL_64FS 0x80
87 #define ADAV80X_DAC_CTRL2_INTERPOL_MASK 0xc0
89 #define ADAV80X_DAC_CTRL2_DEEMPH_NONE 0x00
90 #define ADAV80X_DAC_CTRL2_DEEMPH_44 0x01
91 #define ADAV80X_DAC_CTRL2_DEEMPH_32 0x02
92 #define ADAV80X_DAC_CTRL2_DEEMPH_48 0x03
93 #define ADAV80X_DAC_CTRL2_DEEMPH_MASK 0x01
95 #define ADAV80X_CAPTURE_MODE_MASTER 0x20
96 #define ADAV80X_CAPTURE_WORD_LEN24 0x00
97 #define ADAV80X_CAPTURE_WORD_LEN20 0x04
98 #define ADAV80X_CAPTRUE_WORD_LEN18 0x08
99 #define ADAV80X_CAPTURE_WORD_LEN16 0x0c
100 #define ADAV80X_CAPTURE_WORD_LEN_MASK 0x0c
102 #define ADAV80X_CAPTURE_MODE_LEFT_J 0x00
103 #define ADAV80X_CAPTURE_MODE_I2S 0x01
104 #define ADAV80X_CAPTURE_MODE_RIGHT_J 0x03
105 #define ADAV80X_CAPTURE_MODE_MASK 0x03
107 #define ADAV80X_PLAYBACK_MODE_MASTER 0x10
108 #define ADAV80X_PLAYBACK_MODE_LEFT_J 0x00
109 #define ADAV80X_PLAYBACK_MODE_I2S 0x01
110 #define ADAV80X_PLAYBACK_MODE_RIGHT_J_24 0x04
111 #define ADAV80X_PLAYBACK_MODE_RIGHT_J_20 0x05
112 #define ADAV80X_PLAYBACK_MODE_RIGHT_J_18 0x06
113 #define ADAV80X_PLAYBACK_MODE_RIGHT_J_16 0x07
114 #define ADAV80X_PLAYBACK_MODE_MASK 0x07
116 #define ADAV80X_PLL_OUTE_SYSCLKPD(x) BIT(2 - (x))
118 static struct reg_default adav80x_reg_defaults[] = {
119 { ADAV80X_PLAYBACK_CTRL, 0x01 },
120 { ADAV80X_AUX_IN_CTRL, 0x01 },
121 { ADAV80X_REC_CTRL, 0x02 },
122 { ADAV80X_AUX_OUT_CTRL, 0x01 },
123 { ADAV80X_DPATH_CTRL1, 0xc0 },
124 { ADAV80X_DPATH_CTRL2, 0x11 },
125 { ADAV80X_DAC_CTRL1, 0x00 },
126 { ADAV80X_DAC_CTRL2, 0x00 },
127 { ADAV80X_DAC_CTRL3, 0x00 },
128 { ADAV80X_DAC_L_VOL, 0xff },
129 { ADAV80X_DAC_R_VOL, 0xff },
130 { ADAV80X_PGA_L_VOL, 0x00 },
131 { ADAV80X_PGA_R_VOL, 0x00 },
132 { ADAV80X_ADC_CTRL1, 0x00 },
133 { ADAV80X_ADC_CTRL2, 0x00 },
134 { ADAV80X_ADC_L_VOL, 0xff },
135 { ADAV80X_ADC_R_VOL, 0xff },
136 { ADAV80X_PLL_CTRL1, 0x00 },
137 { ADAV80X_PLL_CTRL2, 0x00 },
138 { ADAV80X_ICLK_CTRL1, 0x00 },
139 { ADAV80X_ICLK_CTRL2, 0x00 },
140 { ADAV80X_PLL_CLK_SRC, 0x00 },
141 { ADAV80X_PLL_OUTE, 0x00 },
145 struct regmap *regmap;
147 enum adav80x_clk_src clk_src;
149 enum adav80x_pll_src pll_src;
151 unsigned int dai_fmt[2];
157 static const char *adav80x_mux_text[] = {
163 static const unsigned int adav80x_mux_values[] = {
167 #define ADAV80X_MUX_ENUM_DECL(name, reg, shift) \
168 SOC_VALUE_ENUM_DOUBLE_DECL(name, reg, shift, 7, \
169 ARRAY_SIZE(adav80x_mux_text), adav80x_mux_text, \
172 static ADAV80X_MUX_ENUM_DECL(adav80x_aux_capture_enum, ADAV80X_DPATH_CTRL1, 0);
173 static ADAV80X_MUX_ENUM_DECL(adav80x_capture_enum, ADAV80X_DPATH_CTRL1, 3);
174 static ADAV80X_MUX_ENUM_DECL(adav80x_dac_enum, ADAV80X_DPATH_CTRL2, 3);
176 static const struct snd_kcontrol_new adav80x_aux_capture_mux_ctrl =
177 SOC_DAPM_VALUE_ENUM("Route", adav80x_aux_capture_enum);
178 static const struct snd_kcontrol_new adav80x_capture_mux_ctrl =
179 SOC_DAPM_VALUE_ENUM("Route", adav80x_capture_enum);
180 static const struct snd_kcontrol_new adav80x_dac_mux_ctrl =
181 SOC_DAPM_VALUE_ENUM("Route", adav80x_dac_enum);
183 #define ADAV80X_MUX(name, ctrl) \
184 SND_SOC_DAPM_VALUE_MUX(name, SND_SOC_NOPM, 0, 0, ctrl)
186 static const struct snd_soc_dapm_widget adav80x_dapm_widgets[] = {
187 SND_SOC_DAPM_DAC("DAC", NULL, ADAV80X_DAC_CTRL1, 7, 1),
188 SND_SOC_DAPM_ADC("ADC", NULL, ADAV80X_ADC_CTRL1, 5, 1),
190 SND_SOC_DAPM_PGA("Right PGA", ADAV80X_ADC_CTRL1, 0, 1, NULL, 0),
191 SND_SOC_DAPM_PGA("Left PGA", ADAV80X_ADC_CTRL1, 1, 1, NULL, 0),
193 SND_SOC_DAPM_AIF_OUT("AIFOUT", "HiFi Capture", 0, SND_SOC_NOPM, 0, 0),
194 SND_SOC_DAPM_AIF_IN("AIFIN", "HiFi Playback", 0, SND_SOC_NOPM, 0, 0),
196 SND_SOC_DAPM_AIF_OUT("AIFAUXOUT", "Aux Capture", 0, SND_SOC_NOPM, 0, 0),
197 SND_SOC_DAPM_AIF_IN("AIFAUXIN", "Aux Playback", 0, SND_SOC_NOPM, 0, 0),
199 ADAV80X_MUX("Aux Capture Select", &adav80x_aux_capture_mux_ctrl),
200 ADAV80X_MUX("Capture Select", &adav80x_capture_mux_ctrl),
201 ADAV80X_MUX("DAC Select", &adav80x_dac_mux_ctrl),
203 SND_SOC_DAPM_INPUT("VINR"),
204 SND_SOC_DAPM_INPUT("VINL"),
205 SND_SOC_DAPM_OUTPUT("VOUTR"),
206 SND_SOC_DAPM_OUTPUT("VOUTL"),
208 SND_SOC_DAPM_SUPPLY("SYSCLK", SND_SOC_NOPM, 0, 0, NULL, 0),
209 SND_SOC_DAPM_SUPPLY("PLL1", ADAV80X_PLL_CTRL1, 2, 1, NULL, 0),
210 SND_SOC_DAPM_SUPPLY("PLL2", ADAV80X_PLL_CTRL1, 3, 1, NULL, 0),
211 SND_SOC_DAPM_SUPPLY("OSC", ADAV80X_PLL_CTRL1, 1, 1, NULL, 0),
214 static int adav80x_dapm_sysclk_check(struct snd_soc_dapm_widget *source,
215 struct snd_soc_dapm_widget *sink)
217 struct snd_soc_codec *codec = source->codec;
218 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
221 switch (adav80x->clk_src) {
222 case ADAV80X_CLK_PLL1:
225 case ADAV80X_CLK_PLL2:
228 case ADAV80X_CLK_XTAL:
235 return strcmp(source->name, clk) == 0;
238 static int adav80x_dapm_pll_check(struct snd_soc_dapm_widget *source,
239 struct snd_soc_dapm_widget *sink)
241 struct snd_soc_codec *codec = source->codec;
242 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
244 return adav80x->pll_src == ADAV80X_PLL_SRC_XTAL;
248 static const struct snd_soc_dapm_route adav80x_dapm_routes[] = {
249 { "DAC Select", "ADC", "ADC" },
250 { "DAC Select", "Playback", "AIFIN" },
251 { "DAC Select", "Aux Playback", "AIFAUXIN" },
252 { "DAC", NULL, "DAC Select" },
254 { "Capture Select", "ADC", "ADC" },
255 { "Capture Select", "Playback", "AIFIN" },
256 { "Capture Select", "Aux Playback", "AIFAUXIN" },
257 { "AIFOUT", NULL, "Capture Select" },
259 { "Aux Capture Select", "ADC", "ADC" },
260 { "Aux Capture Select", "Playback", "AIFIN" },
261 { "Aux Capture Select", "Aux Playback", "AIFAUXIN" },
262 { "AIFAUXOUT", NULL, "Aux Capture Select" },
264 { "VOUTR", NULL, "DAC" },
265 { "VOUTL", NULL, "DAC" },
267 { "Left PGA", NULL, "VINL" },
268 { "Right PGA", NULL, "VINR" },
269 { "ADC", NULL, "Left PGA" },
270 { "ADC", NULL, "Right PGA" },
272 { "SYSCLK", NULL, "PLL1", adav80x_dapm_sysclk_check },
273 { "SYSCLK", NULL, "PLL2", adav80x_dapm_sysclk_check },
274 { "SYSCLK", NULL, "OSC", adav80x_dapm_sysclk_check },
275 { "PLL1", NULL, "OSC", adav80x_dapm_pll_check },
276 { "PLL2", NULL, "OSC", adav80x_dapm_pll_check },
278 { "ADC", NULL, "SYSCLK" },
279 { "DAC", NULL, "SYSCLK" },
280 { "AIFOUT", NULL, "SYSCLK" },
281 { "AIFAUXOUT", NULL, "SYSCLK" },
282 { "AIFIN", NULL, "SYSCLK" },
283 { "AIFAUXIN", NULL, "SYSCLK" },
286 static int adav80x_set_deemph(struct snd_soc_codec *codec)
288 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
291 if (adav80x->deemph) {
292 switch (adav80x->rate) {
294 val = ADAV80X_DAC_CTRL2_DEEMPH_32;
297 val = ADAV80X_DAC_CTRL2_DEEMPH_44;
303 val = ADAV80X_DAC_CTRL2_DEEMPH_48;
306 val = ADAV80X_DAC_CTRL2_DEEMPH_NONE;
310 val = ADAV80X_DAC_CTRL2_DEEMPH_NONE;
313 return regmap_update_bits(adav80x->regmap, ADAV80X_DAC_CTRL2,
314 ADAV80X_DAC_CTRL2_DEEMPH_MASK, val);
317 static int adav80x_put_deemph(struct snd_kcontrol *kcontrol,
318 struct snd_ctl_elem_value *ucontrol)
320 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
321 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
322 unsigned int deemph = ucontrol->value.enumerated.item[0];
327 adav80x->deemph = deemph;
329 return adav80x_set_deemph(codec);
332 static int adav80x_get_deemph(struct snd_kcontrol *kcontrol,
333 struct snd_ctl_elem_value *ucontrol)
335 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
336 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
338 ucontrol->value.enumerated.item[0] = adav80x->deemph;
342 static const DECLARE_TLV_DB_SCALE(adav80x_inpga_tlv, 0, 50, 0);
343 static const DECLARE_TLV_DB_MINMAX(adav80x_digital_tlv, -9563, 0);
345 static const struct snd_kcontrol_new adav80x_controls[] = {
346 SOC_DOUBLE_R_TLV("Master Playback Volume", ADAV80X_DAC_L_VOL,
347 ADAV80X_DAC_R_VOL, 0, 0xff, 0, adav80x_digital_tlv),
348 SOC_DOUBLE_R_TLV("Master Capture Volume", ADAV80X_ADC_L_VOL,
349 ADAV80X_ADC_R_VOL, 0, 0xff, 0, adav80x_digital_tlv),
351 SOC_DOUBLE_R_TLV("PGA Capture Volume", ADAV80X_PGA_L_VOL,
352 ADAV80X_PGA_R_VOL, 0, 0x30, 0, adav80x_inpga_tlv),
354 SOC_DOUBLE("Master Playback Switch", ADAV80X_DAC_CTRL1, 0, 1, 1, 0),
355 SOC_DOUBLE("Master Capture Switch", ADAV80X_ADC_CTRL1, 2, 3, 1, 1),
357 SOC_SINGLE("ADC High Pass Filter Switch", ADAV80X_ADC_CTRL1, 6, 1, 0),
359 SOC_SINGLE_BOOL_EXT("Playback De-emphasis Switch", 0,
360 adav80x_get_deemph, adav80x_put_deemph),
363 static unsigned int adav80x_port_ctrl_regs[2][2] = {
364 { ADAV80X_REC_CTRL, ADAV80X_PLAYBACK_CTRL, },
365 { ADAV80X_AUX_OUT_CTRL, ADAV80X_AUX_IN_CTRL },
368 static int adav80x_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
370 struct snd_soc_codec *codec = dai->codec;
371 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
372 unsigned int capture = 0x00;
373 unsigned int playback = 0x00;
375 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
376 case SND_SOC_DAIFMT_CBM_CFM:
377 capture |= ADAV80X_CAPTURE_MODE_MASTER;
378 playback |= ADAV80X_PLAYBACK_MODE_MASTER;
379 case SND_SOC_DAIFMT_CBS_CFS:
385 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
386 case SND_SOC_DAIFMT_I2S:
387 capture |= ADAV80X_CAPTURE_MODE_I2S;
388 playback |= ADAV80X_PLAYBACK_MODE_I2S;
390 case SND_SOC_DAIFMT_LEFT_J:
391 capture |= ADAV80X_CAPTURE_MODE_LEFT_J;
392 playback |= ADAV80X_PLAYBACK_MODE_LEFT_J;
394 case SND_SOC_DAIFMT_RIGHT_J:
395 capture |= ADAV80X_CAPTURE_MODE_RIGHT_J;
396 playback |= ADAV80X_PLAYBACK_MODE_RIGHT_J_24;
402 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
403 case SND_SOC_DAIFMT_NB_NF:
409 regmap_update_bits(adav80x->regmap, adav80x_port_ctrl_regs[dai->id][0],
410 ADAV80X_CAPTURE_MODE_MASK | ADAV80X_CAPTURE_MODE_MASTER,
412 regmap_write(adav80x->regmap, adav80x_port_ctrl_regs[dai->id][1],
415 adav80x->dai_fmt[dai->id] = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
420 static int adav80x_set_adc_clock(struct snd_soc_codec *codec,
421 unsigned int sample_rate)
423 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
426 if (sample_rate <= 48000)
427 val = ADAV80X_ADC_CTRL1_MODULATOR_128FS;
429 val = ADAV80X_ADC_CTRL1_MODULATOR_64FS;
431 regmap_update_bits(adav80x->regmap, ADAV80X_ADC_CTRL1,
432 ADAV80X_ADC_CTRL1_MODULATOR_MASK, val);
437 static int adav80x_set_dac_clock(struct snd_soc_codec *codec,
438 unsigned int sample_rate)
440 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
443 if (sample_rate <= 48000)
444 val = ADAV80X_DAC_CTRL2_DIV1 | ADAV80X_DAC_CTRL2_INTERPOL_256FS;
446 val = ADAV80X_DAC_CTRL2_DIV2 | ADAV80X_DAC_CTRL2_INTERPOL_128FS;
448 regmap_update_bits(adav80x->regmap, ADAV80X_DAC_CTRL2,
449 ADAV80X_DAC_CTRL2_DIV_MASK | ADAV80X_DAC_CTRL2_INTERPOL_MASK,
455 static int adav80x_set_capture_pcm_format(struct snd_soc_codec *codec,
456 struct snd_soc_dai *dai, snd_pcm_format_t format)
458 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
462 case SNDRV_PCM_FORMAT_S16_LE:
463 val = ADAV80X_CAPTURE_WORD_LEN16;
465 case SNDRV_PCM_FORMAT_S18_3LE:
466 val = ADAV80X_CAPTRUE_WORD_LEN18;
468 case SNDRV_PCM_FORMAT_S20_3LE:
469 val = ADAV80X_CAPTURE_WORD_LEN20;
471 case SNDRV_PCM_FORMAT_S24_LE:
472 val = ADAV80X_CAPTURE_WORD_LEN24;
478 regmap_update_bits(adav80x->regmap, adav80x_port_ctrl_regs[dai->id][0],
479 ADAV80X_CAPTURE_WORD_LEN_MASK, val);
484 static int adav80x_set_playback_pcm_format(struct snd_soc_codec *codec,
485 struct snd_soc_dai *dai, snd_pcm_format_t format)
487 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
490 if (adav80x->dai_fmt[dai->id] != SND_SOC_DAIFMT_RIGHT_J)
494 case SNDRV_PCM_FORMAT_S16_LE:
495 val = ADAV80X_PLAYBACK_MODE_RIGHT_J_16;
497 case SNDRV_PCM_FORMAT_S18_3LE:
498 val = ADAV80X_PLAYBACK_MODE_RIGHT_J_18;
500 case SNDRV_PCM_FORMAT_S20_3LE:
501 val = ADAV80X_PLAYBACK_MODE_RIGHT_J_20;
503 case SNDRV_PCM_FORMAT_S24_LE:
504 val = ADAV80X_PLAYBACK_MODE_RIGHT_J_24;
510 regmap_update_bits(adav80x->regmap, adav80x_port_ctrl_regs[dai->id][1],
511 ADAV80X_PLAYBACK_MODE_MASK, val);
516 static int adav80x_hw_params(struct snd_pcm_substream *substream,
517 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
519 struct snd_soc_codec *codec = dai->codec;
520 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
521 unsigned int rate = params_rate(params);
523 if (rate * 256 != adav80x->sysclk)
526 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
527 adav80x_set_playback_pcm_format(codec, dai,
528 params_format(params));
529 adav80x_set_dac_clock(codec, rate);
531 adav80x_set_capture_pcm_format(codec, dai,
532 params_format(params));
533 adav80x_set_adc_clock(codec, rate);
535 adav80x->rate = rate;
536 adav80x_set_deemph(codec);
541 static int adav80x_set_sysclk(struct snd_soc_codec *codec,
542 int clk_id, int source,
543 unsigned int freq, int dir)
545 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
547 if (dir == SND_SOC_CLOCK_IN) {
549 case ADAV80X_CLK_XIN:
550 case ADAV80X_CLK_XTAL:
551 case ADAV80X_CLK_MCLKI:
552 case ADAV80X_CLK_PLL1:
553 case ADAV80X_CLK_PLL2:
559 adav80x->sysclk = freq;
561 if (adav80x->clk_src != clk_id) {
562 unsigned int iclk_ctrl1, iclk_ctrl2;
564 adav80x->clk_src = clk_id;
565 if (clk_id == ADAV80X_CLK_XTAL)
566 clk_id = ADAV80X_CLK_XIN;
568 iclk_ctrl1 = ADAV80X_ICLK_CTRL1_DAC_SRC(clk_id) |
569 ADAV80X_ICLK_CTRL1_ADC_SRC(clk_id) |
570 ADAV80X_ICLK_CTRL1_ICLK2_SRC(clk_id);
571 iclk_ctrl2 = ADAV80X_ICLK_CTRL2_ICLK1_SRC(clk_id);
573 regmap_write(adav80x->regmap, ADAV80X_ICLK_CTRL1,
575 regmap_write(adav80x->regmap, ADAV80X_ICLK_CTRL2,
578 snd_soc_dapm_sync(&codec->dapm);
584 case ADAV80X_CLK_SYSCLK1:
585 case ADAV80X_CLK_SYSCLK2:
586 case ADAV80X_CLK_SYSCLK3:
592 clk_id -= ADAV80X_CLK_SYSCLK1;
593 mask = ADAV80X_PLL_OUTE_SYSCLKPD(clk_id);
596 regmap_update_bits(adav80x->regmap, ADAV80X_PLL_OUTE,
598 adav80x->sysclk_pd[clk_id] = true;
600 regmap_update_bits(adav80x->regmap, ADAV80X_PLL_OUTE,
602 adav80x->sysclk_pd[clk_id] = false;
605 if (adav80x->sysclk_pd[0])
606 snd_soc_dapm_disable_pin(&codec->dapm, "PLL1");
608 snd_soc_dapm_force_enable_pin(&codec->dapm, "PLL1");
610 if (adav80x->sysclk_pd[1] || adav80x->sysclk_pd[2])
611 snd_soc_dapm_disable_pin(&codec->dapm, "PLL2");
613 snd_soc_dapm_force_enable_pin(&codec->dapm, "PLL2");
615 snd_soc_dapm_sync(&codec->dapm);
621 static int adav80x_set_pll(struct snd_soc_codec *codec, int pll_id,
622 int source, unsigned int freq_in, unsigned int freq_out)
624 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
625 unsigned int pll_ctrl1 = 0;
626 unsigned int pll_ctrl2 = 0;
627 unsigned int pll_src;
630 case ADAV80X_PLL_SRC_XTAL:
631 case ADAV80X_PLL_SRC_XIN:
632 case ADAV80X_PLL_SRC_MCLKI:
645 if (source == ADAV80X_PLL_SRC_XIN) {
646 pll_ctrl1 |= ADAV80X_PLL_CTRL1_PLLDIV;
653 if (freq_out > 12288000) {
654 pll_ctrl2 |= ADAV80X_PLL_CTRL2_DOUB(pll_id);
658 /* freq_out = sample_rate * 256 */
661 pll_ctrl2 |= ADAV80X_PLL_CTRL2_FS_32(pll_id);
664 pll_ctrl2 |= ADAV80X_PLL_CTRL2_FS_44(pll_id);
667 pll_ctrl2 |= ADAV80X_PLL_CTRL2_FS_48(pll_id);
673 regmap_update_bits(adav80x->regmap, ADAV80X_PLL_CTRL1,
674 ADAV80X_PLL_CTRL1_PLLDIV, pll_ctrl1);
675 regmap_update_bits(adav80x->regmap, ADAV80X_PLL_CTRL2,
676 ADAV80X_PLL_CTRL2_PLL_MASK(pll_id), pll_ctrl2);
678 if (source != adav80x->pll_src) {
679 if (source == ADAV80X_PLL_SRC_MCLKI)
680 pll_src = ADAV80X_PLL_CLK_SRC_PLL_MCLKI(pll_id);
682 pll_src = ADAV80X_PLL_CLK_SRC_PLL_XIN(pll_id);
684 regmap_update_bits(adav80x->regmap, ADAV80X_PLL_CLK_SRC,
685 ADAV80X_PLL_CLK_SRC_PLL_MASK(pll_id), pll_src);
687 adav80x->pll_src = source;
689 snd_soc_dapm_sync(&codec->dapm);
695 static int adav80x_set_bias_level(struct snd_soc_codec *codec,
696 enum snd_soc_bias_level level)
698 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
699 unsigned int mask = ADAV80X_DAC_CTRL1_PD;
702 case SND_SOC_BIAS_ON:
704 case SND_SOC_BIAS_PREPARE:
706 case SND_SOC_BIAS_STANDBY:
707 regmap_update_bits(adav80x->regmap, ADAV80X_DAC_CTRL1, mask,
710 case SND_SOC_BIAS_OFF:
711 regmap_update_bits(adav80x->regmap, ADAV80X_DAC_CTRL1, mask,
716 codec->dapm.bias_level = level;
720 /* Enforce the same sample rate on all audio interfaces */
721 static int adav80x_dai_startup(struct snd_pcm_substream *substream,
722 struct snd_soc_dai *dai)
724 struct snd_soc_codec *codec = dai->codec;
725 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
727 if (!codec->active || !adav80x->rate)
730 return snd_pcm_hw_constraint_minmax(substream->runtime,
731 SNDRV_PCM_HW_PARAM_RATE, adav80x->rate, adav80x->rate);
734 static void adav80x_dai_shutdown(struct snd_pcm_substream *substream,
735 struct snd_soc_dai *dai)
737 struct snd_soc_codec *codec = dai->codec;
738 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
744 static const struct snd_soc_dai_ops adav80x_dai_ops = {
745 .set_fmt = adav80x_set_dai_fmt,
746 .hw_params = adav80x_hw_params,
747 .startup = adav80x_dai_startup,
748 .shutdown = adav80x_dai_shutdown,
751 #define ADAV80X_PLAYBACK_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
752 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_64000 | SNDRV_PCM_RATE_88200 | \
753 SNDRV_PCM_RATE_96000)
755 #define ADAV80X_CAPTURE_RATES (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000)
757 #define ADAV80X_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S18_3LE | \
758 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE)
760 static struct snd_soc_dai_driver adav80x_dais[] = {
762 .name = "adav80x-hifi",
765 .stream_name = "HiFi Playback",
768 .rates = ADAV80X_PLAYBACK_RATES,
769 .formats = ADAV80X_FORMATS,
772 .stream_name = "HiFi Capture",
775 .rates = ADAV80X_CAPTURE_RATES,
776 .formats = ADAV80X_FORMATS,
778 .ops = &adav80x_dai_ops,
781 .name = "adav80x-aux",
784 .stream_name = "Aux Playback",
787 .rates = ADAV80X_PLAYBACK_RATES,
788 .formats = ADAV80X_FORMATS,
791 .stream_name = "Aux Capture",
794 .rates = ADAV80X_CAPTURE_RATES,
795 .formats = ADAV80X_FORMATS,
797 .ops = &adav80x_dai_ops,
801 static int adav80x_probe(struct snd_soc_codec *codec)
804 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
806 ret = snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
808 dev_err(codec->dev, "failed to set cache I/O: %d\n", ret);
812 /* Force PLLs on for SYSCLK output */
813 snd_soc_dapm_force_enable_pin(&codec->dapm, "PLL1");
814 snd_soc_dapm_force_enable_pin(&codec->dapm, "PLL2");
816 /* Power down S/PDIF receiver, since it is currently not supported */
817 regmap_write(adav80x->regmap, ADAV80X_PLL_OUTE, 0x20);
818 /* Disable DAC zero flag */
819 regmap_write(adav80x->regmap, ADAV80X_DAC_CTRL3, 0x6);
821 return adav80x_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
824 static int adav80x_suspend(struct snd_soc_codec *codec)
826 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
829 ret = adav80x_set_bias_level(codec, SND_SOC_BIAS_OFF);
830 regcache_cache_only(adav80x->regmap, true);
835 static int adav80x_resume(struct snd_soc_codec *codec)
837 struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
839 regcache_cache_only(adav80x->regmap, false);
840 adav80x_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
841 regcache_sync(adav80x->regmap);
846 static int adav80x_remove(struct snd_soc_codec *codec)
848 return adav80x_set_bias_level(codec, SND_SOC_BIAS_OFF);
851 static struct snd_soc_codec_driver adav80x_codec_driver = {
852 .probe = adav80x_probe,
853 .remove = adav80x_remove,
854 .suspend = adav80x_suspend,
855 .resume = adav80x_resume,
856 .set_bias_level = adav80x_set_bias_level,
858 .set_pll = adav80x_set_pll,
859 .set_sysclk = adav80x_set_sysclk,
861 .controls = adav80x_controls,
862 .num_controls = ARRAY_SIZE(adav80x_controls),
863 .dapm_widgets = adav80x_dapm_widgets,
864 .num_dapm_widgets = ARRAY_SIZE(adav80x_dapm_widgets),
865 .dapm_routes = adav80x_dapm_routes,
866 .num_dapm_routes = ARRAY_SIZE(adav80x_dapm_routes),
869 static int adav80x_bus_probe(struct device *dev, struct regmap *regmap)
871 struct adav80x *adav80x;
875 return PTR_ERR(regmap);
877 adav80x = kzalloc(sizeof(*adav80x), GFP_KERNEL);
882 dev_set_drvdata(dev, adav80x);
883 adav80x->regmap = regmap;
885 ret = snd_soc_register_codec(dev, &adav80x_codec_driver,
886 adav80x_dais, ARRAY_SIZE(adav80x_dais));
893 static int adav80x_bus_remove(struct device *dev)
895 snd_soc_unregister_codec(dev);
896 kfree(dev_get_drvdata(dev));
900 #if defined(CONFIG_SPI_MASTER)
901 static const struct regmap_config adav80x_spi_regmap_config = {
905 .read_flag_mask = 0x01,
907 .max_register = ADAV80X_PLL_OUTE,
909 .cache_type = REGCACHE_RBTREE,
910 .reg_defaults = adav80x_reg_defaults,
911 .num_reg_defaults = ARRAY_SIZE(adav80x_reg_defaults),
914 static const struct spi_device_id adav80x_spi_id[] = {
918 MODULE_DEVICE_TABLE(spi, adav80x_spi_id);
920 static int adav80x_spi_probe(struct spi_device *spi)
922 return adav80x_bus_probe(&spi->dev,
923 devm_regmap_init_spi(spi, &adav80x_spi_regmap_config));
926 static int adav80x_spi_remove(struct spi_device *spi)
928 return adav80x_bus_remove(&spi->dev);
931 static struct spi_driver adav80x_spi_driver = {
934 .owner = THIS_MODULE,
936 .probe = adav80x_spi_probe,
937 .remove = adav80x_spi_remove,
938 .id_table = adav80x_spi_id,
942 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
943 static const struct regmap_config adav80x_i2c_regmap_config = {
948 .max_register = ADAV80X_PLL_OUTE,
950 .cache_type = REGCACHE_RBTREE,
951 .reg_defaults = adav80x_reg_defaults,
952 .num_reg_defaults = ARRAY_SIZE(adav80x_reg_defaults),
955 static const struct i2c_device_id adav80x_i2c_id[] = {
959 MODULE_DEVICE_TABLE(i2c, adav80x_i2c_id);
961 static int adav80x_i2c_probe(struct i2c_client *client,
962 const struct i2c_device_id *id)
964 return adav80x_bus_probe(&client->dev,
965 devm_regmap_init_i2c(client, &adav80x_i2c_regmap_config));
968 static int adav80x_i2c_remove(struct i2c_client *client)
970 return adav80x_bus_remove(&client->dev);
973 static struct i2c_driver adav80x_i2c_driver = {
976 .owner = THIS_MODULE,
978 .probe = adav80x_i2c_probe,
979 .remove = adav80x_i2c_remove,
980 .id_table = adav80x_i2c_id,
984 static int __init adav80x_init(void)
988 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
989 ret = i2c_add_driver(&adav80x_i2c_driver);
994 #if defined(CONFIG_SPI_MASTER)
995 ret = spi_register_driver(&adav80x_spi_driver);
1000 module_init(adav80x_init);
1002 static void __exit adav80x_exit(void)
1004 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1005 i2c_del_driver(&adav80x_i2c_driver);
1007 #if defined(CONFIG_SPI_MASTER)
1008 spi_unregister_driver(&adav80x_spi_driver);
1011 module_exit(adav80x_exit);
1013 MODULE_DESCRIPTION("ASoC ADAV80x driver");
1014 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1015 MODULE_AUTHOR("Yi Li <yi.li@analog.com>>");
1016 MODULE_LICENSE("GPL");