2 * Common code for ADAU1X61 and ADAU1X81 codecs
4 * Copyright 2011-2014 Analog Devices Inc.
5 * Author: Lars-Peter Clausen <lars@metafoo.de>
7 * Licensed under the GPL-2 or later.
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <sound/core.h>
16 #include <sound/pcm.h>
17 #include <sound/pcm_params.h>
18 #include <sound/soc.h>
19 #include <sound/tlv.h>
20 #include <linux/gcd.h>
21 #include <linux/i2c.h>
22 #include <linux/spi/spi.h>
23 #include <linux/regmap.h>
27 #include "adau-utils.h"
29 static const char * const adau17x1_capture_mixer_boost_text
[] = {
30 "Normal operation", "Boost Level 1", "Boost Level 2", "Boost Level 3",
33 static SOC_ENUM_SINGLE_DECL(adau17x1_capture_boost_enum
,
34 ADAU17X1_REC_POWER_MGMT
, 5, adau17x1_capture_mixer_boost_text
);
36 static const char * const adau17x1_mic_bias_mode_text
[] = {
37 "Normal operation", "High performance",
40 static SOC_ENUM_SINGLE_DECL(adau17x1_mic_bias_mode_enum
,
41 ADAU17X1_MICBIAS
, 3, adau17x1_mic_bias_mode_text
);
43 static const DECLARE_TLV_DB_MINMAX(adau17x1_digital_tlv
, -9563, 0);
45 static const struct snd_kcontrol_new adau17x1_controls
[] = {
46 SOC_DOUBLE_R_TLV("Digital Capture Volume",
47 ADAU17X1_LEFT_INPUT_DIGITAL_VOL
,
48 ADAU17X1_RIGHT_INPUT_DIGITAL_VOL
,
49 0, 0xff, 1, adau17x1_digital_tlv
),
50 SOC_DOUBLE_R_TLV("Digital Playback Volume", ADAU17X1_DAC_CONTROL1
,
51 ADAU17X1_DAC_CONTROL2
, 0, 0xff, 1, adau17x1_digital_tlv
),
53 SOC_SINGLE("ADC High Pass Filter Switch", ADAU17X1_ADC_CONTROL
,
55 SOC_SINGLE("Playback De-emphasis Switch", ADAU17X1_DAC_CONTROL0
,
58 SOC_ENUM("Capture Boost", adau17x1_capture_boost_enum
),
60 SOC_ENUM("Mic Bias Mode", adau17x1_mic_bias_mode_enum
),
63 static int adau17x1_pll_event(struct snd_soc_dapm_widget
*w
,
64 struct snd_kcontrol
*kcontrol
, int event
)
66 struct snd_soc_codec
*codec
= snd_soc_dapm_to_codec(w
->dapm
);
67 struct adau
*adau
= snd_soc_codec_get_drvdata(codec
);
70 if (SND_SOC_DAPM_EVENT_ON(event
)) {
71 adau
->pll_regs
[5] = 1;
73 adau
->pll_regs
[5] = 0;
74 /* Bypass the PLL when disabled, otherwise registers will become
76 regmap_update_bits(adau
->regmap
, ADAU17X1_CLOCK_CONTROL
,
77 ADAU17X1_CLOCK_CONTROL_CORECLK_SRC_PLL
, 0);
80 /* The PLL register is 6 bytes long and can only be written at once. */
81 ret
= regmap_raw_write(adau
->regmap
, ADAU17X1_PLL_CONTROL
,
82 adau
->pll_regs
, ARRAY_SIZE(adau
->pll_regs
));
84 if (SND_SOC_DAPM_EVENT_ON(event
)) {
86 regmap_update_bits(adau
->regmap
, ADAU17X1_CLOCK_CONTROL
,
87 ADAU17X1_CLOCK_CONTROL_CORECLK_SRC_PLL
,
88 ADAU17X1_CLOCK_CONTROL_CORECLK_SRC_PLL
);
94 static const char * const adau17x1_mono_stereo_text
[] = {
96 "Mono Left Channel (L+R)",
97 "Mono Right Channel (L+R)",
101 static SOC_ENUM_SINGLE_DECL(adau17x1_dac_mode_enum
,
102 ADAU17X1_DAC_CONTROL0
, 6, adau17x1_mono_stereo_text
);
104 static const struct snd_kcontrol_new adau17x1_dac_mode_mux
=
105 SOC_DAPM_ENUM("DAC Mono-Stereo-Mode", adau17x1_dac_mode_enum
);
107 static const struct snd_soc_dapm_widget adau17x1_dapm_widgets
[] = {
108 SND_SOC_DAPM_SUPPLY_S("PLL", 3, SND_SOC_NOPM
, 0, 0, adau17x1_pll_event
,
109 SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMD
),
111 SND_SOC_DAPM_SUPPLY("AIFCLK", SND_SOC_NOPM
, 0, 0, NULL
, 0),
113 SND_SOC_DAPM_SUPPLY("MICBIAS", ADAU17X1_MICBIAS
, 0, 0, NULL
, 0),
115 SND_SOC_DAPM_SUPPLY("Left Playback Enable", ADAU17X1_PLAY_POWER_MGMT
,
117 SND_SOC_DAPM_SUPPLY("Right Playback Enable", ADAU17X1_PLAY_POWER_MGMT
,
120 SND_SOC_DAPM_MUX("Left DAC Mode Mux", SND_SOC_NOPM
, 0, 0,
121 &adau17x1_dac_mode_mux
),
122 SND_SOC_DAPM_MUX("Right DAC Mode Mux", SND_SOC_NOPM
, 0, 0,
123 &adau17x1_dac_mode_mux
),
125 SND_SOC_DAPM_ADC("Left Decimator", NULL
, ADAU17X1_ADC_CONTROL
, 0, 0),
126 SND_SOC_DAPM_ADC("Right Decimator", NULL
, ADAU17X1_ADC_CONTROL
, 1, 0),
127 SND_SOC_DAPM_DAC("Left DAC", NULL
, ADAU17X1_DAC_CONTROL0
, 0, 0),
128 SND_SOC_DAPM_DAC("Right DAC", NULL
, ADAU17X1_DAC_CONTROL0
, 1, 0),
131 static const struct snd_soc_dapm_route adau17x1_dapm_routes
[] = {
132 { "Left Decimator", NULL
, "SYSCLK" },
133 { "Right Decimator", NULL
, "SYSCLK" },
134 { "Left DAC", NULL
, "SYSCLK" },
135 { "Right DAC", NULL
, "SYSCLK" },
136 { "Capture", NULL
, "SYSCLK" },
137 { "Playback", NULL
, "SYSCLK" },
139 { "Left DAC", NULL
, "Left DAC Mode Mux" },
140 { "Right DAC", NULL
, "Right DAC Mode Mux" },
142 { "Capture", NULL
, "AIFCLK" },
143 { "Playback", NULL
, "AIFCLK" },
146 static const struct snd_soc_dapm_route adau17x1_dapm_pll_route
= {
147 "SYSCLK", NULL
, "PLL",
151 * The MUX register for the Capture and Playback MUXs selects either DSP as
152 * source/destination or one of the TDM slots. The TDM slot is selected via
153 * snd_soc_dai_set_tdm_slot(), so we only expose whether to go to the DSP or
154 * directly to the DAI interface with this control.
156 static int adau17x1_dsp_mux_enum_put(struct snd_kcontrol
*kcontrol
,
157 struct snd_ctl_elem_value
*ucontrol
)
159 struct snd_soc_codec
*codec
= snd_soc_dapm_kcontrol_codec(kcontrol
);
160 struct snd_soc_dapm_context
*dapm
= snd_soc_codec_get_dapm(codec
);
161 struct adau
*adau
= snd_soc_codec_get_drvdata(codec
);
162 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
163 struct snd_soc_dapm_update update
= { 0 };
164 unsigned int stream
= e
->shift_l
;
165 unsigned int val
, change
;
168 if (ucontrol
->value
.enumerated
.item
[0] >= e
->items
)
171 switch (ucontrol
->value
.enumerated
.item
[0]) {
174 adau
->dsp_bypass
[stream
] = false;
177 val
= (adau
->tdm_slot
[stream
] * 2) + 1;
178 adau
->dsp_bypass
[stream
] = true;
182 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
183 reg
= ADAU17X1_SERIAL_INPUT_ROUTE
;
185 reg
= ADAU17X1_SERIAL_OUTPUT_ROUTE
;
187 change
= snd_soc_test_bits(codec
, reg
, 0xff, val
);
189 update
.kcontrol
= kcontrol
;
194 snd_soc_dapm_mux_update_power(dapm
, kcontrol
,
195 ucontrol
->value
.enumerated
.item
[0], e
, &update
);
201 static int adau17x1_dsp_mux_enum_get(struct snd_kcontrol
*kcontrol
,
202 struct snd_ctl_elem_value
*ucontrol
)
204 struct snd_soc_codec
*codec
= snd_soc_dapm_kcontrol_codec(kcontrol
);
205 struct adau
*adau
= snd_soc_codec_get_drvdata(codec
);
206 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
207 unsigned int stream
= e
->shift_l
;
208 unsigned int reg
, val
;
211 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
212 reg
= ADAU17X1_SERIAL_INPUT_ROUTE
;
214 reg
= ADAU17X1_SERIAL_OUTPUT_ROUTE
;
216 ret
= regmap_read(adau
->regmap
, reg
, &val
);
222 ucontrol
->value
.enumerated
.item
[0] = val
;
227 #define DECLARE_ADAU17X1_DSP_MUX_CTRL(_name, _label, _stream, _text) \
228 const struct snd_kcontrol_new _name = \
229 SOC_DAPM_ENUM_EXT(_label, (const struct soc_enum)\
230 SOC_ENUM_SINGLE(SND_SOC_NOPM, _stream, \
231 ARRAY_SIZE(_text), _text), \
232 adau17x1_dsp_mux_enum_get, adau17x1_dsp_mux_enum_put)
234 static const char * const adau17x1_dac_mux_text
[] = {
239 static const char * const adau17x1_capture_mux_text
[] = {
244 static DECLARE_ADAU17X1_DSP_MUX_CTRL(adau17x1_dac_mux
, "DAC Playback Mux",
245 SNDRV_PCM_STREAM_PLAYBACK
, adau17x1_dac_mux_text
);
247 static DECLARE_ADAU17X1_DSP_MUX_CTRL(adau17x1_capture_mux
, "Capture Mux",
248 SNDRV_PCM_STREAM_CAPTURE
, adau17x1_capture_mux_text
);
250 static const struct snd_soc_dapm_widget adau17x1_dsp_dapm_widgets
[] = {
251 SND_SOC_DAPM_PGA("DSP", ADAU17X1_DSP_RUN
, 0, 0, NULL
, 0),
252 SND_SOC_DAPM_SIGGEN("DSP Siggen"),
254 SND_SOC_DAPM_MUX("DAC Playback Mux", SND_SOC_NOPM
, 0, 0,
256 SND_SOC_DAPM_MUX("Capture Mux", SND_SOC_NOPM
, 0, 0,
257 &adau17x1_capture_mux
),
260 static const struct snd_soc_dapm_route adau17x1_dsp_dapm_routes
[] = {
261 { "DAC Playback Mux", "DSP", "DSP" },
262 { "DAC Playback Mux", "AIFIN", "Playback" },
264 { "Left DAC Mode Mux", "Stereo", "DAC Playback Mux" },
265 { "Left DAC Mode Mux", "Mono (L+R)", "DAC Playback Mux" },
266 { "Left DAC Mode Mux", "Mono Left Channel (L+R)", "DAC Playback Mux" },
267 { "Right DAC Mode Mux", "Stereo", "DAC Playback Mux" },
268 { "Right DAC Mode Mux", "Mono (L+R)", "DAC Playback Mux" },
269 { "Right DAC Mode Mux", "Mono Right Channel (L+R)", "DAC Playback Mux" },
271 { "Capture Mux", "DSP", "DSP" },
272 { "Capture Mux", "Decimator", "Left Decimator" },
273 { "Capture Mux", "Decimator", "Right Decimator" },
275 { "Capture", NULL
, "Capture Mux" },
277 { "DSP", NULL
, "DSP Siggen" },
279 { "DSP", NULL
, "Left Decimator" },
280 { "DSP", NULL
, "Right Decimator" },
283 static const struct snd_soc_dapm_route adau17x1_no_dsp_dapm_routes
[] = {
284 { "Left DAC Mode Mux", "Stereo", "Playback" },
285 { "Left DAC Mode Mux", "Mono (L+R)", "Playback" },
286 { "Left DAC Mode Mux", "Mono Left Channel (L+R)", "Playback" },
287 { "Right DAC Mode Mux", "Stereo", "Playback" },
288 { "Right DAC Mode Mux", "Mono (L+R)", "Playback" },
289 { "Right DAC Mode Mux", "Mono Right Channel (L+R)", "Playback" },
290 { "Capture", NULL
, "Left Decimator" },
291 { "Capture", NULL
, "Right Decimator" },
294 bool adau17x1_has_dsp(struct adau
*adau
)
296 switch (adau
->type
) {
305 EXPORT_SYMBOL_GPL(adau17x1_has_dsp
);
307 static int adau17x1_set_dai_pll(struct snd_soc_dai
*dai
, int pll_id
,
308 int source
, unsigned int freq_in
, unsigned int freq_out
)
310 struct snd_soc_codec
*codec
= dai
->codec
;
311 struct adau
*adau
= snd_soc_codec_get_drvdata(codec
);
314 if (freq_in
< 8000000 || freq_in
> 27000000)
317 ret
= adau_calc_pll_cfg(freq_in
, freq_out
, adau
->pll_regs
);
321 /* The PLL register is 6 bytes long and can only be written at once. */
322 ret
= regmap_raw_write(adau
->regmap
, ADAU17X1_PLL_CONTROL
,
323 adau
->pll_regs
, ARRAY_SIZE(adau
->pll_regs
));
327 adau
->pll_freq
= freq_out
;
332 static int adau17x1_set_dai_sysclk(struct snd_soc_dai
*dai
,
333 int clk_id
, unsigned int freq
, int dir
)
335 struct snd_soc_dapm_context
*dapm
= snd_soc_codec_get_dapm(dai
->codec
);
336 struct adau
*adau
= snd_soc_codec_get_drvdata(dai
->codec
);
341 case ADAU17X1_CLK_SRC_MCLK
:
344 case ADAU17X1_CLK_SRC_PLL_AUTO
:
348 case ADAU17X1_CLK_SRC_PLL
:
355 switch (adau
->clk_src
) {
356 case ADAU17X1_CLK_SRC_MCLK
:
359 case ADAU17X1_CLK_SRC_PLL
:
360 case ADAU17X1_CLK_SRC_PLL_AUTO
:
369 if (is_pll
!= was_pll
) {
371 snd_soc_dapm_add_routes(dapm
,
372 &adau17x1_dapm_pll_route
, 1);
374 snd_soc_dapm_del_routes(dapm
,
375 &adau17x1_dapm_pll_route
, 1);
379 adau
->clk_src
= clk_id
;
384 static int adau17x1_auto_pll(struct snd_soc_dai
*dai
,
385 struct snd_pcm_hw_params
*params
)
387 struct adau
*adau
= snd_soc_dai_get_drvdata(dai
);
388 unsigned int pll_rate
;
390 switch (params_rate(params
)) {
398 pll_rate
= 48000 * 1024;
407 pll_rate
= 44100 * 1024;
413 return adau17x1_set_dai_pll(dai
, ADAU17X1_PLL
, ADAU17X1_PLL_SRC_MCLK
,
414 clk_get_rate(adau
->mclk
), pll_rate
);
417 static int adau17x1_hw_params(struct snd_pcm_substream
*substream
,
418 struct snd_pcm_hw_params
*params
, struct snd_soc_dai
*dai
)
420 struct snd_soc_codec
*codec
= dai
->codec
;
421 struct adau
*adau
= snd_soc_codec_get_drvdata(codec
);
422 unsigned int val
, div
, dsp_div
;
426 switch (adau
->clk_src
) {
427 case ADAU17X1_CLK_SRC_PLL_AUTO
:
428 ret
= adau17x1_auto_pll(dai
, params
);
432 case ADAU17X1_CLK_SRC_PLL
:
433 freq
= adau
->pll_freq
;
440 if (freq
% params_rate(params
) != 0)
443 switch (freq
/ params_rate(params
)) {
448 case 6144: /* fs / 6 */
452 case 4096: /* fs / 4 */
456 case 3072: /* fs / 3 */
460 case 2048: /* fs / 2 */
464 case 1536: /* fs / 1.5 */
468 case 512: /* fs / 0.5 */
476 regmap_update_bits(adau
->regmap
, ADAU17X1_CONVERTER0
,
477 ADAU17X1_CONVERTER0_CONVSR_MASK
, div
);
478 if (adau17x1_has_dsp(adau
)) {
479 regmap_write(adau
->regmap
, ADAU17X1_SERIAL_SAMPLING_RATE
, div
);
480 regmap_write(adau
->regmap
, ADAU17X1_DSP_SAMPLING_RATE
, dsp_div
);
483 if (adau
->sigmadsp
) {
484 ret
= adau17x1_setup_firmware(adau
, params_rate(params
));
489 if (adau
->dai_fmt
!= SND_SOC_DAIFMT_RIGHT_J
)
492 switch (params_width(params
)) {
494 val
= ADAU17X1_SERIAL_PORT1_DELAY16
;
497 val
= ADAU17X1_SERIAL_PORT1_DELAY8
;
500 val
= ADAU17X1_SERIAL_PORT1_DELAY0
;
506 return regmap_update_bits(adau
->regmap
, ADAU17X1_SERIAL_PORT1
,
507 ADAU17X1_SERIAL_PORT1_DELAY_MASK
, val
);
510 static int adau17x1_set_dai_fmt(struct snd_soc_dai
*dai
,
513 struct adau
*adau
= snd_soc_codec_get_drvdata(dai
->codec
);
514 unsigned int ctrl0
, ctrl1
;
517 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
518 case SND_SOC_DAIFMT_CBM_CFM
:
519 ctrl0
= ADAU17X1_SERIAL_PORT0_MASTER
;
522 case SND_SOC_DAIFMT_CBS_CFS
:
524 adau
->master
= false;
530 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
531 case SND_SOC_DAIFMT_I2S
:
533 ctrl1
= ADAU17X1_SERIAL_PORT1_DELAY1
;
535 case SND_SOC_DAIFMT_LEFT_J
:
536 case SND_SOC_DAIFMT_RIGHT_J
:
538 ctrl1
= ADAU17X1_SERIAL_PORT1_DELAY0
;
540 case SND_SOC_DAIFMT_DSP_A
:
542 ctrl0
|= ADAU17X1_SERIAL_PORT0_PULSE_MODE
;
543 ctrl1
= ADAU17X1_SERIAL_PORT1_DELAY1
;
545 case SND_SOC_DAIFMT_DSP_B
:
547 ctrl0
|= ADAU17X1_SERIAL_PORT0_PULSE_MODE
;
548 ctrl1
= ADAU17X1_SERIAL_PORT1_DELAY0
;
554 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
555 case SND_SOC_DAIFMT_NB_NF
:
557 case SND_SOC_DAIFMT_IB_NF
:
558 ctrl0
|= ADAU17X1_SERIAL_PORT0_BCLK_POL
;
560 case SND_SOC_DAIFMT_NB_IF
:
561 lrclk_pol
= !lrclk_pol
;
563 case SND_SOC_DAIFMT_IB_IF
:
564 ctrl0
|= ADAU17X1_SERIAL_PORT0_BCLK_POL
;
565 lrclk_pol
= !lrclk_pol
;
572 ctrl0
|= ADAU17X1_SERIAL_PORT0_LRCLK_POL
;
574 regmap_write(adau
->regmap
, ADAU17X1_SERIAL_PORT0
, ctrl0
);
575 regmap_write(adau
->regmap
, ADAU17X1_SERIAL_PORT1
, ctrl1
);
577 adau
->dai_fmt
= fmt
& SND_SOC_DAIFMT_FORMAT_MASK
;
582 static int adau17x1_set_dai_tdm_slot(struct snd_soc_dai
*dai
,
583 unsigned int tx_mask
, unsigned int rx_mask
, int slots
, int slot_width
)
585 struct adau
*adau
= snd_soc_codec_get_drvdata(dai
->codec
);
586 unsigned int ser_ctrl0
, ser_ctrl1
;
587 unsigned int conv_ctrl0
, conv_ctrl1
;
599 ser_ctrl0
= ADAU17X1_SERIAL_PORT0_STEREO
;
602 ser_ctrl0
= ADAU17X1_SERIAL_PORT0_TDM4
;
605 if (adau
->type
== ADAU1361
)
608 ser_ctrl0
= ADAU17X1_SERIAL_PORT0_TDM8
;
614 switch (slot_width
* slots
) {
616 if (adau
->type
== ADAU1761
)
619 ser_ctrl1
= ADAU17X1_SERIAL_PORT1_BCLK32
;
622 ser_ctrl1
= ADAU17X1_SERIAL_PORT1_BCLK64
;
625 ser_ctrl1
= ADAU17X1_SERIAL_PORT1_BCLK48
;
628 ser_ctrl1
= ADAU17X1_SERIAL_PORT1_BCLK128
;
631 if (adau
->type
== ADAU1361
)
634 ser_ctrl1
= ADAU17X1_SERIAL_PORT1_BCLK256
;
642 conv_ctrl1
= ADAU17X1_CONVERTER1_ADC_PAIR(1);
643 adau
->tdm_slot
[SNDRV_PCM_STREAM_CAPTURE
] = 0;
646 conv_ctrl1
= ADAU17X1_CONVERTER1_ADC_PAIR(2);
647 adau
->tdm_slot
[SNDRV_PCM_STREAM_CAPTURE
] = 1;
650 conv_ctrl1
= ADAU17X1_CONVERTER1_ADC_PAIR(3);
651 adau
->tdm_slot
[SNDRV_PCM_STREAM_CAPTURE
] = 2;
654 conv_ctrl1
= ADAU17X1_CONVERTER1_ADC_PAIR(4);
655 adau
->tdm_slot
[SNDRV_PCM_STREAM_CAPTURE
] = 3;
663 conv_ctrl0
= ADAU17X1_CONVERTER0_DAC_PAIR(1);
664 adau
->tdm_slot
[SNDRV_PCM_STREAM_PLAYBACK
] = 0;
667 conv_ctrl0
= ADAU17X1_CONVERTER0_DAC_PAIR(2);
668 adau
->tdm_slot
[SNDRV_PCM_STREAM_PLAYBACK
] = 1;
671 conv_ctrl0
= ADAU17X1_CONVERTER0_DAC_PAIR(3);
672 adau
->tdm_slot
[SNDRV_PCM_STREAM_PLAYBACK
] = 2;
675 conv_ctrl0
= ADAU17X1_CONVERTER0_DAC_PAIR(4);
676 adau
->tdm_slot
[SNDRV_PCM_STREAM_PLAYBACK
] = 3;
682 regmap_update_bits(adau
->regmap
, ADAU17X1_CONVERTER0
,
683 ADAU17X1_CONVERTER0_DAC_PAIR_MASK
, conv_ctrl0
);
684 regmap_update_bits(adau
->regmap
, ADAU17X1_CONVERTER1
,
685 ADAU17X1_CONVERTER1_ADC_PAIR_MASK
, conv_ctrl1
);
686 regmap_update_bits(adau
->regmap
, ADAU17X1_SERIAL_PORT0
,
687 ADAU17X1_SERIAL_PORT0_TDM_MASK
, ser_ctrl0
);
688 regmap_update_bits(adau
->regmap
, ADAU17X1_SERIAL_PORT1
,
689 ADAU17X1_SERIAL_PORT1_BCLK_MASK
, ser_ctrl1
);
691 if (!adau17x1_has_dsp(adau
))
694 if (adau
->dsp_bypass
[SNDRV_PCM_STREAM_PLAYBACK
]) {
695 regmap_write(adau
->regmap
, ADAU17X1_SERIAL_INPUT_ROUTE
,
696 (adau
->tdm_slot
[SNDRV_PCM_STREAM_PLAYBACK
] * 2) + 1);
699 if (adau
->dsp_bypass
[SNDRV_PCM_STREAM_CAPTURE
]) {
700 regmap_write(adau
->regmap
, ADAU17X1_SERIAL_OUTPUT_ROUTE
,
701 (adau
->tdm_slot
[SNDRV_PCM_STREAM_CAPTURE
] * 2) + 1);
707 static int adau17x1_startup(struct snd_pcm_substream
*substream
,
708 struct snd_soc_dai
*dai
)
710 struct adau
*adau
= snd_soc_codec_get_drvdata(dai
->codec
);
713 return sigmadsp_restrict_params(adau
->sigmadsp
, substream
);
718 const struct snd_soc_dai_ops adau17x1_dai_ops
= {
719 .hw_params
= adau17x1_hw_params
,
720 .set_sysclk
= adau17x1_set_dai_sysclk
,
721 .set_fmt
= adau17x1_set_dai_fmt
,
722 .set_pll
= adau17x1_set_dai_pll
,
723 .set_tdm_slot
= adau17x1_set_dai_tdm_slot
,
724 .startup
= adau17x1_startup
,
726 EXPORT_SYMBOL_GPL(adau17x1_dai_ops
);
728 int adau17x1_set_micbias_voltage(struct snd_soc_codec
*codec
,
729 enum adau17x1_micbias_voltage micbias
)
731 struct adau
*adau
= snd_soc_codec_get_drvdata(codec
);
734 case ADAU17X1_MICBIAS_0_90_AVDD
:
735 case ADAU17X1_MICBIAS_0_65_AVDD
:
741 return regmap_write(adau
->regmap
, ADAU17X1_MICBIAS
, micbias
<< 2);
743 EXPORT_SYMBOL_GPL(adau17x1_set_micbias_voltage
);
745 bool adau17x1_precious_register(struct device
*dev
, unsigned int reg
)
747 /* SigmaDSP parameter memory */
753 EXPORT_SYMBOL_GPL(adau17x1_precious_register
);
755 bool adau17x1_readable_register(struct device
*dev
, unsigned int reg
)
757 /* SigmaDSP parameter memory */
762 case ADAU17X1_CLOCK_CONTROL
:
763 case ADAU17X1_PLL_CONTROL
:
764 case ADAU17X1_REC_POWER_MGMT
:
765 case ADAU17X1_MICBIAS
:
766 case ADAU17X1_SERIAL_PORT0
:
767 case ADAU17X1_SERIAL_PORT1
:
768 case ADAU17X1_CONVERTER0
:
769 case ADAU17X1_CONVERTER1
:
770 case ADAU17X1_LEFT_INPUT_DIGITAL_VOL
:
771 case ADAU17X1_RIGHT_INPUT_DIGITAL_VOL
:
772 case ADAU17X1_ADC_CONTROL
:
773 case ADAU17X1_PLAY_POWER_MGMT
:
774 case ADAU17X1_DAC_CONTROL0
:
775 case ADAU17X1_DAC_CONTROL1
:
776 case ADAU17X1_DAC_CONTROL2
:
777 case ADAU17X1_SERIAL_PORT_PAD
:
778 case ADAU17X1_CONTROL_PORT_PAD0
:
779 case ADAU17X1_CONTROL_PORT_PAD1
:
780 case ADAU17X1_DSP_SAMPLING_RATE
:
781 case ADAU17X1_SERIAL_INPUT_ROUTE
:
782 case ADAU17X1_SERIAL_OUTPUT_ROUTE
:
783 case ADAU17X1_DSP_ENABLE
:
784 case ADAU17X1_DSP_RUN
:
785 case ADAU17X1_SERIAL_SAMPLING_RATE
:
792 EXPORT_SYMBOL_GPL(adau17x1_readable_register
);
794 bool adau17x1_volatile_register(struct device
*dev
, unsigned int reg
)
796 /* SigmaDSP parameter and program memory */
801 /* The PLL register is 6 bytes long */
802 case ADAU17X1_PLL_CONTROL
:
803 case ADAU17X1_PLL_CONTROL
+ 1:
804 case ADAU17X1_PLL_CONTROL
+ 2:
805 case ADAU17X1_PLL_CONTROL
+ 3:
806 case ADAU17X1_PLL_CONTROL
+ 4:
807 case ADAU17X1_PLL_CONTROL
+ 5:
815 EXPORT_SYMBOL_GPL(adau17x1_volatile_register
);
817 int adau17x1_setup_firmware(struct adau
*adau
, unsigned int rate
)
822 ret
= regmap_read(adau
->regmap
, ADAU17X1_DSP_SAMPLING_RATE
, &dspsr
);
826 regmap_write(adau
->regmap
, ADAU17X1_DSP_ENABLE
, 1);
827 regmap_write(adau
->regmap
, ADAU17X1_DSP_SAMPLING_RATE
, 0xf);
829 ret
= sigmadsp_setup(adau
->sigmadsp
, rate
);
831 regmap_write(adau
->regmap
, ADAU17X1_DSP_ENABLE
, 0);
834 regmap_write(adau
->regmap
, ADAU17X1_DSP_SAMPLING_RATE
, dspsr
);
838 EXPORT_SYMBOL_GPL(adau17x1_setup_firmware
);
840 int adau17x1_add_widgets(struct snd_soc_codec
*codec
)
842 struct snd_soc_dapm_context
*dapm
= snd_soc_codec_get_dapm(codec
);
843 struct adau
*adau
= snd_soc_codec_get_drvdata(codec
);
846 ret
= snd_soc_add_codec_controls(codec
, adau17x1_controls
,
847 ARRAY_SIZE(adau17x1_controls
));
850 ret
= snd_soc_dapm_new_controls(dapm
, adau17x1_dapm_widgets
,
851 ARRAY_SIZE(adau17x1_dapm_widgets
));
855 if (adau17x1_has_dsp(adau
)) {
856 ret
= snd_soc_dapm_new_controls(dapm
, adau17x1_dsp_dapm_widgets
,
857 ARRAY_SIZE(adau17x1_dsp_dapm_widgets
));
864 ret
= sigmadsp_attach(adau
->sigmadsp
, &codec
->component
);
866 dev_err(codec
->dev
, "Failed to attach firmware: %d\n",
874 EXPORT_SYMBOL_GPL(adau17x1_add_widgets
);
876 int adau17x1_add_routes(struct snd_soc_codec
*codec
)
878 struct snd_soc_dapm_context
*dapm
= snd_soc_codec_get_dapm(codec
);
879 struct adau
*adau
= snd_soc_codec_get_drvdata(codec
);
882 ret
= snd_soc_dapm_add_routes(dapm
, adau17x1_dapm_routes
,
883 ARRAY_SIZE(adau17x1_dapm_routes
));
887 if (adau17x1_has_dsp(adau
)) {
888 ret
= snd_soc_dapm_add_routes(dapm
, adau17x1_dsp_dapm_routes
,
889 ARRAY_SIZE(adau17x1_dsp_dapm_routes
));
891 ret
= snd_soc_dapm_add_routes(dapm
, adau17x1_no_dsp_dapm_routes
,
892 ARRAY_SIZE(adau17x1_no_dsp_dapm_routes
));
895 if (adau
->clk_src
!= ADAU17X1_CLK_SRC_MCLK
)
896 snd_soc_dapm_add_routes(dapm
, &adau17x1_dapm_pll_route
, 1);
900 EXPORT_SYMBOL_GPL(adau17x1_add_routes
);
902 int adau17x1_resume(struct snd_soc_codec
*codec
)
904 struct adau
*adau
= snd_soc_codec_get_drvdata(codec
);
906 if (adau
->switch_mode
)
907 adau
->switch_mode(codec
->dev
);
909 regcache_sync(adau
->regmap
);
913 EXPORT_SYMBOL_GPL(adau17x1_resume
);
915 int adau17x1_probe(struct device
*dev
, struct regmap
*regmap
,
916 enum adau17x1_type type
, void (*switch_mode
)(struct device
*dev
),
917 const char *firmware_name
)
923 return PTR_ERR(regmap
);
925 adau
= devm_kzalloc(dev
, sizeof(*adau
), GFP_KERNEL
);
929 adau
->mclk
= devm_clk_get(dev
, "mclk");
930 if (IS_ERR(adau
->mclk
)) {
931 if (PTR_ERR(adau
->mclk
) != -ENOENT
)
932 return PTR_ERR(adau
->mclk
);
933 /* Clock is optional (for the driver) */
935 } else if (adau
->mclk
) {
936 adau
->clk_src
= ADAU17X1_CLK_SRC_PLL_AUTO
;
939 * Any valid PLL output rate will work at this point, use one
940 * that is likely to be chosen later as well. The register will
941 * be written when the PLL is powered up for the first time.
943 ret
= adau_calc_pll_cfg(clk_get_rate(adau
->mclk
), 48000 * 1024,
948 ret
= clk_prepare_enable(adau
->mclk
);
953 adau
->regmap
= regmap
;
954 adau
->switch_mode
= switch_mode
;
957 dev_set_drvdata(dev
, adau
);
960 adau
->sigmadsp
= devm_sigmadsp_init_regmap(dev
, regmap
, NULL
,
962 if (IS_ERR(adau
->sigmadsp
)) {
963 dev_warn(dev
, "Could not find firmware file: %ld\n",
964 PTR_ERR(adau
->sigmadsp
));
965 adau
->sigmadsp
= NULL
;
974 EXPORT_SYMBOL_GPL(adau17x1_probe
);
976 void adau17x1_remove(struct device
*dev
)
978 struct adau
*adau
= dev_get_drvdata(dev
);
980 snd_soc_unregister_codec(dev
);
982 clk_disable_unprepare(adau
->mclk
);
984 EXPORT_SYMBOL_GPL(adau17x1_remove
);
986 MODULE_DESCRIPTION("ASoC ADAU1X61/ADAU1X81 common code");
987 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
988 MODULE_LICENSE("GPL");