1 // SPDX-License-Identifier: GPL-2.0-only
3 * es8328.c -- ES8328 ALSA SoC Audio driver
5 * Copyright 2014 Sutajio Ko-Usagi PTE LTD
7 * Author: Sean Cross <xobs@kosagi.com>
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/of_device.h>
13 #include <linux/module.h>
15 #include <linux/regmap.h>
16 #include <linux/slab.h>
17 #include <linux/regulator/consumer.h>
18 #include <sound/core.h>
19 #include <sound/initval.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
23 #include <sound/tlv.h>
26 static const unsigned int rates_12288
[] = {
27 8000, 12000, 16000, 24000, 32000, 48000, 96000,
30 static const int ratios_12288
[] = {
34 static const struct snd_pcm_hw_constraint_list constraints_12288
= {
35 .count
= ARRAY_SIZE(rates_12288
),
39 static const unsigned int rates_11289
[] = {
40 8018, 11025, 22050, 44100, 88200,
43 static const int ratios_11289
[] = {
47 static const struct snd_pcm_hw_constraint_list constraints_11289
= {
48 .count
= ARRAY_SIZE(rates_11289
),
52 /* regulator supplies for sgtl5000, VDDD is an optional external supply */
53 enum sgtl5000_regulator_supplies
{
61 /* vddd is optional supply */
62 static const char * const supply_names
[ES8328_SUPPLY_NUM
] = {
69 #define ES8328_RATES (SNDRV_PCM_RATE_192000 | \
70 SNDRV_PCM_RATE_96000 | \
71 SNDRV_PCM_RATE_88200 | \
72 SNDRV_PCM_RATE_8000_48000)
73 #define ES8328_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
74 SNDRV_PCM_FMTBIT_S18_3LE | \
75 SNDRV_PCM_FMTBIT_S20_3LE | \
76 SNDRV_PCM_FMTBIT_S24_LE | \
77 SNDRV_PCM_FMTBIT_S32_LE)
80 struct regmap
*regmap
;
85 const struct snd_pcm_hw_constraint_list
*sysclk_constraints
;
86 const int *mclk_ratios
;
88 struct regulator_bulk_data supplies
[ES8328_SUPPLY_NUM
];
95 static const char * const adcpol_txt
[] = {"Normal", "L Invert", "R Invert",
97 static SOC_ENUM_SINGLE_DECL(adcpol
,
98 ES8328_ADCCONTROL6
, 6, adcpol_txt
);
100 static const DECLARE_TLV_DB_SCALE(play_tlv
, -3000, 100, 0);
101 static const DECLARE_TLV_DB_SCALE(dac_adc_tlv
, -9600, 50, 0);
102 static const DECLARE_TLV_DB_SCALE(pga_tlv
, 0, 300, 0);
103 static const DECLARE_TLV_DB_SCALE(bypass_tlv
, -1500, 300, 0);
104 static const DECLARE_TLV_DB_SCALE(mic_tlv
, 0, 300, 0);
106 static const struct {
109 } deemph_settings
[] = {
110 { 0, ES8328_DACCONTROL6_DEEMPH_OFF
},
111 { 32000, ES8328_DACCONTROL6_DEEMPH_32k
},
112 { 44100, ES8328_DACCONTROL6_DEEMPH_44_1k
},
113 { 48000, ES8328_DACCONTROL6_DEEMPH_48k
},
116 static int es8328_set_deemph(struct snd_soc_component
*component
)
118 struct es8328_priv
*es8328
= snd_soc_component_get_drvdata(component
);
122 * If we're using deemphasis select the nearest available sample
125 if (es8328
->deemph
) {
127 for (i
= 1; i
< ARRAY_SIZE(deemph_settings
); i
++) {
128 if (abs(deemph_settings
[i
].rate
- es8328
->playback_fs
) <
129 abs(deemph_settings
[best
].rate
- es8328
->playback_fs
))
133 val
= deemph_settings
[best
].val
;
135 val
= ES8328_DACCONTROL6_DEEMPH_OFF
;
138 dev_dbg(component
->dev
, "Set deemphasis %d\n", val
);
140 return snd_soc_component_update_bits(component
, ES8328_DACCONTROL6
,
141 ES8328_DACCONTROL6_DEEMPH_MASK
, val
);
144 static int es8328_get_deemph(struct snd_kcontrol
*kcontrol
,
145 struct snd_ctl_elem_value
*ucontrol
)
147 struct snd_soc_component
*component
= snd_soc_kcontrol_component(kcontrol
);
148 struct es8328_priv
*es8328
= snd_soc_component_get_drvdata(component
);
150 ucontrol
->value
.integer
.value
[0] = es8328
->deemph
;
154 static int es8328_put_deemph(struct snd_kcontrol
*kcontrol
,
155 struct snd_ctl_elem_value
*ucontrol
)
157 struct snd_soc_component
*component
= snd_soc_kcontrol_component(kcontrol
);
158 struct es8328_priv
*es8328
= snd_soc_component_get_drvdata(component
);
159 unsigned int deemph
= ucontrol
->value
.integer
.value
[0];
165 ret
= es8328_set_deemph(component
);
169 es8328
->deemph
= deemph
;
176 static const struct snd_kcontrol_new es8328_snd_controls
[] = {
177 SOC_DOUBLE_R_TLV("Capture Digital Volume",
178 ES8328_ADCCONTROL8
, ES8328_ADCCONTROL9
,
179 0, 0xc0, 1, dac_adc_tlv
),
180 SOC_SINGLE("Capture ZC Switch", ES8328_ADCCONTROL7
, 6, 1, 0),
182 SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0,
183 es8328_get_deemph
, es8328_put_deemph
),
185 SOC_ENUM("Capture Polarity", adcpol
),
187 SOC_SINGLE_TLV("Left Mixer Left Bypass Volume",
188 ES8328_DACCONTROL17
, 3, 7, 1, bypass_tlv
),
189 SOC_SINGLE_TLV("Left Mixer Right Bypass Volume",
190 ES8328_DACCONTROL19
, 3, 7, 1, bypass_tlv
),
191 SOC_SINGLE_TLV("Right Mixer Left Bypass Volume",
192 ES8328_DACCONTROL18
, 3, 7, 1, bypass_tlv
),
193 SOC_SINGLE_TLV("Right Mixer Right Bypass Volume",
194 ES8328_DACCONTROL20
, 3, 7, 1, bypass_tlv
),
196 SOC_DOUBLE_R_TLV("PCM Volume",
197 ES8328_LDACVOL
, ES8328_RDACVOL
,
198 0, ES8328_DACVOL_MAX
, 1, dac_adc_tlv
),
200 SOC_DOUBLE_R_TLV("Output 1 Playback Volume",
201 ES8328_LOUT1VOL
, ES8328_ROUT1VOL
,
202 0, ES8328_OUT1VOL_MAX
, 0, play_tlv
),
204 SOC_DOUBLE_R_TLV("Output 2 Playback Volume",
205 ES8328_LOUT2VOL
, ES8328_ROUT2VOL
,
206 0, ES8328_OUT2VOL_MAX
, 0, play_tlv
),
208 SOC_DOUBLE_TLV("Mic PGA Volume", ES8328_ADCCONTROL1
,
209 4, 0, 8, 0, mic_tlv
),
216 static const char * const es8328_line_texts
[] = {
217 "Line 1", "Line 2", "PGA", "Differential"};
219 static const struct soc_enum es8328_lline_enum
=
220 SOC_ENUM_SINGLE(ES8328_DACCONTROL16
, 3,
221 ARRAY_SIZE(es8328_line_texts
),
223 static const struct snd_kcontrol_new es8328_left_line_controls
=
224 SOC_DAPM_ENUM("Route", es8328_lline_enum
);
226 static const struct soc_enum es8328_rline_enum
=
227 SOC_ENUM_SINGLE(ES8328_DACCONTROL16
, 0,
228 ARRAY_SIZE(es8328_line_texts
),
230 static const struct snd_kcontrol_new es8328_right_line_controls
=
231 SOC_DAPM_ENUM("Route", es8328_lline_enum
);
234 static const struct snd_kcontrol_new es8328_left_mixer_controls
[] = {
235 SOC_DAPM_SINGLE("Playback Switch", ES8328_DACCONTROL17
, 7, 1, 0),
236 SOC_DAPM_SINGLE("Left Bypass Switch", ES8328_DACCONTROL17
, 6, 1, 0),
237 SOC_DAPM_SINGLE("Right Playback Switch", ES8328_DACCONTROL18
, 7, 1, 0),
238 SOC_DAPM_SINGLE("Right Bypass Switch", ES8328_DACCONTROL18
, 6, 1, 0),
242 static const struct snd_kcontrol_new es8328_right_mixer_controls
[] = {
243 SOC_DAPM_SINGLE("Left Playback Switch", ES8328_DACCONTROL19
, 7, 1, 0),
244 SOC_DAPM_SINGLE("Left Bypass Switch", ES8328_DACCONTROL19
, 6, 1, 0),
245 SOC_DAPM_SINGLE("Playback Switch", ES8328_DACCONTROL20
, 7, 1, 0),
246 SOC_DAPM_SINGLE("Right Bypass Switch", ES8328_DACCONTROL20
, 6, 1, 0),
249 static const char * const es8328_pga_sel
[] = {
250 "Line 1", "Line 2", "Line 3", "Differential"};
253 static const struct soc_enum es8328_lpga_enum
=
254 SOC_ENUM_SINGLE(ES8328_ADCCONTROL2
, 6,
255 ARRAY_SIZE(es8328_pga_sel
),
257 static const struct snd_kcontrol_new es8328_left_pga_controls
=
258 SOC_DAPM_ENUM("Route", es8328_lpga_enum
);
261 static const struct soc_enum es8328_rpga_enum
=
262 SOC_ENUM_SINGLE(ES8328_ADCCONTROL2
, 4,
263 ARRAY_SIZE(es8328_pga_sel
),
265 static const struct snd_kcontrol_new es8328_right_pga_controls
=
266 SOC_DAPM_ENUM("Route", es8328_rpga_enum
);
268 /* Differential Mux */
269 static const char * const es8328_diff_sel
[] = {"Line 1", "Line 2"};
270 static SOC_ENUM_SINGLE_DECL(diffmux
,
271 ES8328_ADCCONTROL3
, 7, es8328_diff_sel
);
272 static const struct snd_kcontrol_new es8328_diffmux_controls
=
273 SOC_DAPM_ENUM("Route", diffmux
);
276 static const char * const es8328_mono_mux
[] = {"Stereo", "Mono (Left)",
277 "Mono (Right)", "Digital Mono"};
278 static SOC_ENUM_SINGLE_DECL(monomux
,
279 ES8328_ADCCONTROL3
, 3, es8328_mono_mux
);
280 static const struct snd_kcontrol_new es8328_monomux_controls
=
281 SOC_DAPM_ENUM("Route", monomux
);
283 static const struct snd_soc_dapm_widget es8328_dapm_widgets
[] = {
284 SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM
, 0, 0,
285 &es8328_diffmux_controls
),
286 SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM
, 0, 0,
287 &es8328_monomux_controls
),
288 SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM
, 0, 0,
289 &es8328_monomux_controls
),
291 SND_SOC_DAPM_MUX("Left PGA Mux", ES8328_ADCPOWER
,
292 ES8328_ADCPOWER_AINL_OFF
, 1,
293 &es8328_left_pga_controls
),
294 SND_SOC_DAPM_MUX("Right PGA Mux", ES8328_ADCPOWER
,
295 ES8328_ADCPOWER_AINR_OFF
, 1,
296 &es8328_right_pga_controls
),
298 SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM
, 0, 0,
299 &es8328_left_line_controls
),
300 SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM
, 0, 0,
301 &es8328_right_line_controls
),
303 SND_SOC_DAPM_ADC("Right ADC", "Right Capture", ES8328_ADCPOWER
,
304 ES8328_ADCPOWER_ADCR_OFF
, 1),
305 SND_SOC_DAPM_ADC("Left ADC", "Left Capture", ES8328_ADCPOWER
,
306 ES8328_ADCPOWER_ADCL_OFF
, 1),
308 SND_SOC_DAPM_SUPPLY("Mic Bias", ES8328_ADCPOWER
,
309 ES8328_ADCPOWER_MIC_BIAS_OFF
, 1, NULL
, 0),
310 SND_SOC_DAPM_SUPPLY("Mic Bias Gen", ES8328_ADCPOWER
,
311 ES8328_ADCPOWER_ADC_BIAS_GEN_OFF
, 1, NULL
, 0),
313 SND_SOC_DAPM_SUPPLY("DAC STM", ES8328_CHIPPOWER
,
314 ES8328_CHIPPOWER_DACSTM_RESET
, 1, NULL
, 0),
315 SND_SOC_DAPM_SUPPLY("ADC STM", ES8328_CHIPPOWER
,
316 ES8328_CHIPPOWER_ADCSTM_RESET
, 1, NULL
, 0),
318 SND_SOC_DAPM_SUPPLY("DAC DIG", ES8328_CHIPPOWER
,
319 ES8328_CHIPPOWER_DACDIG_OFF
, 1, NULL
, 0),
320 SND_SOC_DAPM_SUPPLY("ADC DIG", ES8328_CHIPPOWER
,
321 ES8328_CHIPPOWER_ADCDIG_OFF
, 1, NULL
, 0),
323 SND_SOC_DAPM_SUPPLY("DAC DLL", ES8328_CHIPPOWER
,
324 ES8328_CHIPPOWER_DACDLL_OFF
, 1, NULL
, 0),
325 SND_SOC_DAPM_SUPPLY("ADC DLL", ES8328_CHIPPOWER
,
326 ES8328_CHIPPOWER_ADCDLL_OFF
, 1, NULL
, 0),
328 SND_SOC_DAPM_SUPPLY("ADC Vref", ES8328_CHIPPOWER
,
329 ES8328_CHIPPOWER_ADCVREF_OFF
, 1, NULL
, 0),
330 SND_SOC_DAPM_SUPPLY("DAC Vref", ES8328_CHIPPOWER
,
331 ES8328_CHIPPOWER_DACVREF_OFF
, 1, NULL
, 0),
333 SND_SOC_DAPM_DAC("Right DAC", "Right Playback", ES8328_DACPOWER
,
334 ES8328_DACPOWER_RDAC_OFF
, 1),
335 SND_SOC_DAPM_DAC("Left DAC", "Left Playback", ES8328_DACPOWER
,
336 ES8328_DACPOWER_LDAC_OFF
, 1),
338 SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM
, 0, 0,
339 &es8328_left_mixer_controls
[0],
340 ARRAY_SIZE(es8328_left_mixer_controls
)),
341 SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM
, 0, 0,
342 &es8328_right_mixer_controls
[0],
343 ARRAY_SIZE(es8328_right_mixer_controls
)),
345 SND_SOC_DAPM_PGA("Right Out 2", ES8328_DACPOWER
,
346 ES8328_DACPOWER_ROUT2_ON
, 0, NULL
, 0),
347 SND_SOC_DAPM_PGA("Left Out 2", ES8328_DACPOWER
,
348 ES8328_DACPOWER_LOUT2_ON
, 0, NULL
, 0),
349 SND_SOC_DAPM_PGA("Right Out 1", ES8328_DACPOWER
,
350 ES8328_DACPOWER_ROUT1_ON
, 0, NULL
, 0),
351 SND_SOC_DAPM_PGA("Left Out 1", ES8328_DACPOWER
,
352 ES8328_DACPOWER_LOUT1_ON
, 0, NULL
, 0),
354 SND_SOC_DAPM_OUTPUT("LOUT1"),
355 SND_SOC_DAPM_OUTPUT("ROUT1"),
356 SND_SOC_DAPM_OUTPUT("LOUT2"),
357 SND_SOC_DAPM_OUTPUT("ROUT2"),
359 SND_SOC_DAPM_INPUT("LINPUT1"),
360 SND_SOC_DAPM_INPUT("LINPUT2"),
361 SND_SOC_DAPM_INPUT("RINPUT1"),
362 SND_SOC_DAPM_INPUT("RINPUT2"),
365 static const struct snd_soc_dapm_route es8328_dapm_routes
[] = {
367 { "Left Line Mux", "Line 1", "LINPUT1" },
368 { "Left Line Mux", "Line 2", "LINPUT2" },
369 { "Left Line Mux", "PGA", "Left PGA Mux" },
370 { "Left Line Mux", "Differential", "Differential Mux" },
372 { "Right Line Mux", "Line 1", "RINPUT1" },
373 { "Right Line Mux", "Line 2", "RINPUT2" },
374 { "Right Line Mux", "PGA", "Right PGA Mux" },
375 { "Right Line Mux", "Differential", "Differential Mux" },
377 { "Left PGA Mux", "Line 1", "LINPUT1" },
378 { "Left PGA Mux", "Line 2", "LINPUT2" },
379 { "Left PGA Mux", "Differential", "Differential Mux" },
381 { "Right PGA Mux", "Line 1", "RINPUT1" },
382 { "Right PGA Mux", "Line 2", "RINPUT2" },
383 { "Right PGA Mux", "Differential", "Differential Mux" },
385 { "Differential Mux", "Line 1", "LINPUT1" },
386 { "Differential Mux", "Line 1", "RINPUT1" },
387 { "Differential Mux", "Line 2", "LINPUT2" },
388 { "Differential Mux", "Line 2", "RINPUT2" },
390 { "Left ADC Mux", "Stereo", "Left PGA Mux" },
391 { "Left ADC Mux", "Mono (Left)", "Left PGA Mux" },
392 { "Left ADC Mux", "Digital Mono", "Left PGA Mux" },
394 { "Right ADC Mux", "Stereo", "Right PGA Mux" },
395 { "Right ADC Mux", "Mono (Right)", "Right PGA Mux" },
396 { "Right ADC Mux", "Digital Mono", "Right PGA Mux" },
398 { "Left ADC", NULL
, "Left ADC Mux" },
399 { "Right ADC", NULL
, "Right ADC Mux" },
401 { "ADC DIG", NULL
, "ADC STM" },
402 { "ADC DIG", NULL
, "ADC Vref" },
403 { "ADC DIG", NULL
, "ADC DLL" },
405 { "Left ADC", NULL
, "ADC DIG" },
406 { "Right ADC", NULL
, "ADC DIG" },
408 { "Mic Bias", NULL
, "Mic Bias Gen" },
410 { "Left Line Mux", "Line 1", "LINPUT1" },
411 { "Left Line Mux", "Line 2", "LINPUT2" },
412 { "Left Line Mux", "PGA", "Left PGA Mux" },
413 { "Left Line Mux", "Differential", "Differential Mux" },
415 { "Right Line Mux", "Line 1", "RINPUT1" },
416 { "Right Line Mux", "Line 2", "RINPUT2" },
417 { "Right Line Mux", "PGA", "Right PGA Mux" },
418 { "Right Line Mux", "Differential", "Differential Mux" },
420 { "Left Out 1", NULL
, "Left DAC" },
421 { "Right Out 1", NULL
, "Right DAC" },
422 { "Left Out 2", NULL
, "Left DAC" },
423 { "Right Out 2", NULL
, "Right DAC" },
425 { "Left Mixer", "Playback Switch", "Left DAC" },
426 { "Left Mixer", "Left Bypass Switch", "Left Line Mux" },
427 { "Left Mixer", "Right Playback Switch", "Right DAC" },
428 { "Left Mixer", "Right Bypass Switch", "Right Line Mux" },
430 { "Right Mixer", "Left Playback Switch", "Left DAC" },
431 { "Right Mixer", "Left Bypass Switch", "Left Line Mux" },
432 { "Right Mixer", "Playback Switch", "Right DAC" },
433 { "Right Mixer", "Right Bypass Switch", "Right Line Mux" },
435 { "DAC DIG", NULL
, "DAC STM" },
436 { "DAC DIG", NULL
, "DAC Vref" },
437 { "DAC DIG", NULL
, "DAC DLL" },
439 { "Left DAC", NULL
, "DAC DIG" },
440 { "Right DAC", NULL
, "DAC DIG" },
442 { "Left Out 1", NULL
, "Left Mixer" },
443 { "LOUT1", NULL
, "Left Out 1" },
444 { "Right Out 1", NULL
, "Right Mixer" },
445 { "ROUT1", NULL
, "Right Out 1" },
447 { "Left Out 2", NULL
, "Left Mixer" },
448 { "LOUT2", NULL
, "Left Out 2" },
449 { "Right Out 2", NULL
, "Right Mixer" },
450 { "ROUT2", NULL
, "Right Out 2" },
453 static int es8328_mute(struct snd_soc_dai
*dai
, int mute
)
455 return snd_soc_component_update_bits(dai
->component
, ES8328_DACCONTROL3
,
456 ES8328_DACCONTROL3_DACMUTE
,
457 mute
? ES8328_DACCONTROL3_DACMUTE
: 0);
460 static int es8328_startup(struct snd_pcm_substream
*substream
,
461 struct snd_soc_dai
*dai
)
463 struct snd_soc_component
*component
= dai
->component
;
464 struct es8328_priv
*es8328
= snd_soc_component_get_drvdata(component
);
466 if (es8328
->master
&& es8328
->sysclk_constraints
)
467 snd_pcm_hw_constraint_list(substream
->runtime
, 0,
468 SNDRV_PCM_HW_PARAM_RATE
,
469 es8328
->sysclk_constraints
);
474 static int es8328_hw_params(struct snd_pcm_substream
*substream
,
475 struct snd_pcm_hw_params
*params
,
476 struct snd_soc_dai
*dai
)
478 struct snd_soc_component
*component
= dai
->component
;
479 struct es8328_priv
*es8328
= snd_soc_component_get_drvdata(component
);
485 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
486 reg
= ES8328_DACCONTROL2
;
488 reg
= ES8328_ADCCONTROL5
;
490 if (es8328
->master
) {
491 if (!es8328
->sysclk_constraints
) {
492 dev_err(component
->dev
, "No MCLK configured\n");
496 for (i
= 0; i
< es8328
->sysclk_constraints
->count
; i
++)
497 if (es8328
->sysclk_constraints
->list
[i
] ==
501 if (i
== es8328
->sysclk_constraints
->count
) {
502 dev_err(component
->dev
,
503 "LRCLK %d unsupported with current clock\n",
504 params_rate(params
));
507 ratio
= es8328
->mclk_ratios
[i
];
510 es8328
->mclkdiv2
= 0;
513 snd_soc_component_update_bits(component
, ES8328_MASTERMODE
,
514 ES8328_MASTERMODE_MCLKDIV2
,
515 es8328
->mclkdiv2
? ES8328_MASTERMODE_MCLKDIV2
: 0);
517 switch (params_width(params
)) {
537 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
538 snd_soc_component_update_bits(component
, ES8328_DACCONTROL1
,
539 ES8328_DACCONTROL1_DACWL_MASK
,
540 wl
<< ES8328_DACCONTROL1_DACWL_SHIFT
);
542 es8328
->playback_fs
= params_rate(params
);
543 es8328_set_deemph(component
);
545 snd_soc_component_update_bits(component
, ES8328_ADCCONTROL4
,
546 ES8328_ADCCONTROL4_ADCWL_MASK
,
547 wl
<< ES8328_ADCCONTROL4_ADCWL_SHIFT
);
549 return snd_soc_component_update_bits(component
, reg
, ES8328_RATEMASK
, ratio
);
552 static int es8328_set_sysclk(struct snd_soc_dai
*codec_dai
,
553 int clk_id
, unsigned int freq
, int dir
)
555 struct snd_soc_component
*component
= codec_dai
->component
;
556 struct es8328_priv
*es8328
= snd_soc_component_get_drvdata(component
);
561 es8328
->sysclk_constraints
= NULL
;
562 es8328
->mclk_ratios
= NULL
;
568 es8328
->sysclk_constraints
= &constraints_11289
;
569 es8328
->mclk_ratios
= ratios_11289
;
575 es8328
->sysclk_constraints
= &constraints_12288
;
576 es8328
->mclk_ratios
= ratios_12288
;
582 es8328
->mclkdiv2
= mclkdiv2
;
586 static int es8328_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
589 struct snd_soc_component
*component
= codec_dai
->component
;
590 struct es8328_priv
*es8328
= snd_soc_component_get_drvdata(component
);
594 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
595 case SND_SOC_DAIFMT_CBM_CFM
:
596 /* Master serial port mode, with BCLK generated automatically */
597 snd_soc_component_update_bits(component
, ES8328_MASTERMODE
,
598 ES8328_MASTERMODE_MSC
,
599 ES8328_MASTERMODE_MSC
);
600 es8328
->master
= true;
602 case SND_SOC_DAIFMT_CBS_CFS
:
603 /* Slave serial port mode */
604 snd_soc_component_update_bits(component
, ES8328_MASTERMODE
,
605 ES8328_MASTERMODE_MSC
, 0);
606 es8328
->master
= false;
612 /* interface format */
613 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
614 case SND_SOC_DAIFMT_I2S
:
615 dac_mode
|= ES8328_DACCONTROL1_DACFORMAT_I2S
;
616 adc_mode
|= ES8328_ADCCONTROL4_ADCFORMAT_I2S
;
618 case SND_SOC_DAIFMT_RIGHT_J
:
619 dac_mode
|= ES8328_DACCONTROL1_DACFORMAT_RJUST
;
620 adc_mode
|= ES8328_ADCCONTROL4_ADCFORMAT_RJUST
;
622 case SND_SOC_DAIFMT_LEFT_J
:
623 dac_mode
|= ES8328_DACCONTROL1_DACFORMAT_LJUST
;
624 adc_mode
|= ES8328_ADCCONTROL4_ADCFORMAT_LJUST
;
630 /* clock inversion */
631 if ((fmt
& SND_SOC_DAIFMT_INV_MASK
) != SND_SOC_DAIFMT_NB_NF
)
634 snd_soc_component_update_bits(component
, ES8328_DACCONTROL1
,
635 ES8328_DACCONTROL1_DACFORMAT_MASK
, dac_mode
);
636 snd_soc_component_update_bits(component
, ES8328_ADCCONTROL4
,
637 ES8328_ADCCONTROL4_ADCFORMAT_MASK
, adc_mode
);
642 static int es8328_set_bias_level(struct snd_soc_component
*component
,
643 enum snd_soc_bias_level level
)
646 case SND_SOC_BIAS_ON
:
649 case SND_SOC_BIAS_PREPARE
:
650 /* VREF, VMID=2x50k, digital enabled */
651 snd_soc_component_write(component
, ES8328_CHIPPOWER
, 0);
652 snd_soc_component_update_bits(component
, ES8328_CONTROL1
,
653 ES8328_CONTROL1_VMIDSEL_MASK
|
654 ES8328_CONTROL1_ENREF
,
655 ES8328_CONTROL1_VMIDSEL_50k
|
656 ES8328_CONTROL1_ENREF
);
659 case SND_SOC_BIAS_STANDBY
:
660 if (snd_soc_component_get_bias_level(component
) == SND_SOC_BIAS_OFF
) {
661 snd_soc_component_update_bits(component
, ES8328_CONTROL1
,
662 ES8328_CONTROL1_VMIDSEL_MASK
|
663 ES8328_CONTROL1_ENREF
,
664 ES8328_CONTROL1_VMIDSEL_5k
|
665 ES8328_CONTROL1_ENREF
);
671 snd_soc_component_write(component
, ES8328_CONTROL2
,
672 ES8328_CONTROL2_OVERCURRENT_ON
|
673 ES8328_CONTROL2_THERMAL_SHUTDOWN_ON
);
675 /* VREF, VMID=2*500k, digital stopped */
676 snd_soc_component_update_bits(component
, ES8328_CONTROL1
,
677 ES8328_CONTROL1_VMIDSEL_MASK
|
678 ES8328_CONTROL1_ENREF
,
679 ES8328_CONTROL1_VMIDSEL_500k
|
680 ES8328_CONTROL1_ENREF
);
683 case SND_SOC_BIAS_OFF
:
684 snd_soc_component_update_bits(component
, ES8328_CONTROL1
,
685 ES8328_CONTROL1_VMIDSEL_MASK
|
686 ES8328_CONTROL1_ENREF
,
693 static const struct snd_soc_dai_ops es8328_dai_ops
= {
694 .startup
= es8328_startup
,
695 .hw_params
= es8328_hw_params
,
696 .digital_mute
= es8328_mute
,
697 .set_sysclk
= es8328_set_sysclk
,
698 .set_fmt
= es8328_set_dai_fmt
,
701 static struct snd_soc_dai_driver es8328_dai
= {
702 .name
= "es8328-hifi-analog",
704 .stream_name
= "Playback",
707 .rates
= ES8328_RATES
,
708 .formats
= ES8328_FORMATS
,
711 .stream_name
= "Capture",
714 .rates
= ES8328_RATES
,
715 .formats
= ES8328_FORMATS
,
717 .ops
= &es8328_dai_ops
,
718 .symmetric_rates
= 1,
721 static int es8328_suspend(struct snd_soc_component
*component
)
723 struct es8328_priv
*es8328
;
726 es8328
= snd_soc_component_get_drvdata(component
);
728 clk_disable_unprepare(es8328
->clk
);
730 ret
= regulator_bulk_disable(ARRAY_SIZE(es8328
->supplies
),
733 dev_err(component
->dev
, "unable to disable regulators\n");
739 static int es8328_resume(struct snd_soc_component
*component
)
741 struct regmap
*regmap
= dev_get_regmap(component
->dev
, NULL
);
742 struct es8328_priv
*es8328
;
745 es8328
= snd_soc_component_get_drvdata(component
);
747 ret
= clk_prepare_enable(es8328
->clk
);
749 dev_err(component
->dev
, "unable to enable clock\n");
753 ret
= regulator_bulk_enable(ARRAY_SIZE(es8328
->supplies
),
756 dev_err(component
->dev
, "unable to enable regulators\n");
760 regcache_mark_dirty(regmap
);
761 ret
= regcache_sync(regmap
);
763 dev_err(component
->dev
, "unable to sync regcache\n");
770 static int es8328_component_probe(struct snd_soc_component
*component
)
772 struct es8328_priv
*es8328
;
775 es8328
= snd_soc_component_get_drvdata(component
);
777 ret
= regulator_bulk_enable(ARRAY_SIZE(es8328
->supplies
),
780 dev_err(component
->dev
, "unable to enable regulators\n");
785 es8328
->clk
= devm_clk_get(component
->dev
, NULL
);
786 if (IS_ERR(es8328
->clk
)) {
787 dev_err(component
->dev
, "codec clock missing or invalid\n");
788 ret
= PTR_ERR(es8328
->clk
);
792 ret
= clk_prepare_enable(es8328
->clk
);
794 dev_err(component
->dev
, "unable to prepare codec clk\n");
801 regulator_bulk_disable(ARRAY_SIZE(es8328
->supplies
),
806 static void es8328_remove(struct snd_soc_component
*component
)
808 struct es8328_priv
*es8328
;
810 es8328
= snd_soc_component_get_drvdata(component
);
813 clk_disable_unprepare(es8328
->clk
);
815 regulator_bulk_disable(ARRAY_SIZE(es8328
->supplies
),
819 const struct regmap_config es8328_regmap_config
= {
822 .max_register
= ES8328_REG_MAX
,
823 .cache_type
= REGCACHE_RBTREE
,
824 .use_single_read
= true,
825 .use_single_write
= true,
827 EXPORT_SYMBOL_GPL(es8328_regmap_config
);
829 static const struct snd_soc_component_driver es8328_component_driver
= {
830 .probe
= es8328_component_probe
,
831 .remove
= es8328_remove
,
832 .suspend
= es8328_suspend
,
833 .resume
= es8328_resume
,
834 .set_bias_level
= es8328_set_bias_level
,
835 .controls
= es8328_snd_controls
,
836 .num_controls
= ARRAY_SIZE(es8328_snd_controls
),
837 .dapm_widgets
= es8328_dapm_widgets
,
838 .num_dapm_widgets
= ARRAY_SIZE(es8328_dapm_widgets
),
839 .dapm_routes
= es8328_dapm_routes
,
840 .num_dapm_routes
= ARRAY_SIZE(es8328_dapm_routes
),
841 .suspend_bias_off
= 1,
843 .use_pmdown_time
= 1,
845 .non_legacy_dai_naming
= 1,
848 int es8328_probe(struct device
*dev
, struct regmap
*regmap
)
850 struct es8328_priv
*es8328
;
855 return PTR_ERR(regmap
);
857 es8328
= devm_kzalloc(dev
, sizeof(*es8328
), GFP_KERNEL
);
861 es8328
->regmap
= regmap
;
863 for (i
= 0; i
< ARRAY_SIZE(es8328
->supplies
); i
++)
864 es8328
->supplies
[i
].supply
= supply_names
[i
];
866 ret
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(es8328
->supplies
),
869 dev_err(dev
, "unable to get regulators\n");
873 dev_set_drvdata(dev
, es8328
);
875 return devm_snd_soc_register_component(dev
,
876 &es8328_component_driver
, &es8328_dai
, 1);
878 EXPORT_SYMBOL_GPL(es8328_probe
);
880 MODULE_DESCRIPTION("ASoC ES8328 driver");
881 MODULE_AUTHOR("Sean Cross <xobs@kosagi.com>");
882 MODULE_LICENSE("GPL");