2 * sgtl5000.c -- SGTL5000 ALSA SoC Audio driver
4 * Copyright 2010-2011 Freescale Semiconductor, Inc. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/init.h>
14 #include <linux/delay.h>
15 #include <linux/slab.h>
17 #include <linux/i2c.h>
18 #include <linux/clk.h>
19 #include <linux/regmap.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/of_device.h>
24 #include <sound/core.h>
25 #include <sound/tlv.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/soc-dapm.h>
30 #include <sound/initval.h>
34 #define SGTL5000_DAP_REG_OFFSET 0x0100
35 #define SGTL5000_MAX_REG_OFFSET 0x013A
37 /* default value of sgtl5000 registers */
38 static const struct reg_default sgtl5000_reg_defaults
[] = {
39 { SGTL5000_CHIP_DIG_POWER
, 0x0000 },
40 { SGTL5000_CHIP_CLK_CTRL
, 0x0008 },
41 { SGTL5000_CHIP_I2S_CTRL
, 0x0010 },
42 { SGTL5000_CHIP_SSS_CTRL
, 0x0010 },
43 { SGTL5000_CHIP_ADCDAC_CTRL
, 0x020c },
44 { SGTL5000_CHIP_DAC_VOL
, 0x3c3c },
45 { SGTL5000_CHIP_PAD_STRENGTH
, 0x015f },
46 { SGTL5000_CHIP_ANA_ADC_CTRL
, 0x0000 },
47 { SGTL5000_CHIP_ANA_HP_CTRL
, 0x1818 },
48 { SGTL5000_CHIP_ANA_CTRL
, 0x0111 },
49 { SGTL5000_CHIP_LINREG_CTRL
, 0x0000 },
50 { SGTL5000_CHIP_REF_CTRL
, 0x0000 },
51 { SGTL5000_CHIP_MIC_CTRL
, 0x0000 },
52 { SGTL5000_CHIP_LINE_OUT_CTRL
, 0x0000 },
53 { SGTL5000_CHIP_LINE_OUT_VOL
, 0x0404 },
54 { SGTL5000_CHIP_ANA_POWER
, 0x7060 },
55 { SGTL5000_CHIP_PLL_CTRL
, 0x5000 },
56 { SGTL5000_CHIP_CLK_TOP_CTRL
, 0x0000 },
57 { SGTL5000_CHIP_ANA_STATUS
, 0x0000 },
58 { SGTL5000_CHIP_SHORT_CTRL
, 0x0000 },
59 { SGTL5000_CHIP_ANA_TEST2
, 0x0000 },
60 { SGTL5000_DAP_CTRL
, 0x0000 },
61 { SGTL5000_DAP_PEQ
, 0x0000 },
62 { SGTL5000_DAP_BASS_ENHANCE
, 0x0040 },
63 { SGTL5000_DAP_BASS_ENHANCE_CTRL
, 0x051f },
64 { SGTL5000_DAP_AUDIO_EQ
, 0x0000 },
65 { SGTL5000_DAP_SURROUND
, 0x0040 },
66 { SGTL5000_DAP_EQ_BASS_BAND0
, 0x002f },
67 { SGTL5000_DAP_EQ_BASS_BAND1
, 0x002f },
68 { SGTL5000_DAP_EQ_BASS_BAND2
, 0x002f },
69 { SGTL5000_DAP_EQ_BASS_BAND3
, 0x002f },
70 { SGTL5000_DAP_EQ_BASS_BAND4
, 0x002f },
71 { SGTL5000_DAP_MAIN_CHAN
, 0x8000 },
72 { SGTL5000_DAP_MIX_CHAN
, 0x0000 },
73 { SGTL5000_DAP_AVC_CTRL
, 0x0510 },
74 { SGTL5000_DAP_AVC_THRESHOLD
, 0x1473 },
75 { SGTL5000_DAP_AVC_ATTACK
, 0x0028 },
76 { SGTL5000_DAP_AVC_DECAY
, 0x0050 },
79 /* regulator supplies for sgtl5000, VDDD is an optional external supply */
80 enum sgtl5000_regulator_supplies
{
87 /* vddd is optional supply */
88 static const char *supply_names
[SGTL5000_SUPPLY_NUM
] = {
94 #define LDO_CONSUMER_NAME "VDDD_LDO"
95 #define LDO_VOLTAGE 1200000
97 static struct regulator_consumer_supply ldo_consumer
[] = {
98 REGULATOR_SUPPLY(LDO_CONSUMER_NAME
, NULL
),
101 static struct regulator_init_data ldo_init_data
= {
105 .valid_modes_mask
= REGULATOR_MODE_NORMAL
,
106 .valid_ops_mask
= REGULATOR_CHANGE_STATUS
,
108 .num_consumer_supplies
= 1,
109 .consumer_supplies
= &ldo_consumer
[0],
113 * sgtl5000 internal ldo regulator,
114 * enabled when VDDD not provided
116 struct ldo_regulator
{
117 struct regulator_desc desc
;
118 struct regulator_dev
*dev
;
124 /* sgtl5000 private structure in codec */
125 struct sgtl5000_priv
{
126 int sysclk
; /* sysclk rate */
127 int master
; /* i2s master or not */
128 int fmt
; /* i2s data format */
129 struct regulator_bulk_data supplies
[SGTL5000_SUPPLY_NUM
];
130 struct ldo_regulator
*ldo
;
131 struct regmap
*regmap
;
137 * mic_bias power on/off share the same register bits with
138 * output impedance of mic bias, when power on mic bias, we
139 * need reclaim it to impedance value.
145 static int mic_bias_event(struct snd_soc_dapm_widget
*w
,
146 struct snd_kcontrol
*kcontrol
, int event
)
149 case SND_SOC_DAPM_POST_PMU
:
150 /* change mic bias resistor to 4Kohm */
151 snd_soc_update_bits(w
->codec
, SGTL5000_CHIP_MIC_CTRL
,
152 SGTL5000_BIAS_R_MASK
,
153 SGTL5000_BIAS_R_4k
<< SGTL5000_BIAS_R_SHIFT
);
156 case SND_SOC_DAPM_PRE_PMD
:
157 snd_soc_update_bits(w
->codec
, SGTL5000_CHIP_MIC_CTRL
,
158 SGTL5000_BIAS_R_MASK
, 0);
165 * As manual described, ADC/DAC only works when VAG powerup,
166 * So enabled VAG before ADC/DAC up.
167 * In power down case, we need wait 400ms when vag fully ramped down.
169 static int power_vag_event(struct snd_soc_dapm_widget
*w
,
170 struct snd_kcontrol
*kcontrol
, int event
)
172 const u32 mask
= SGTL5000_DAC_POWERUP
| SGTL5000_ADC_POWERUP
;
175 case SND_SOC_DAPM_POST_PMU
:
176 snd_soc_update_bits(w
->codec
, SGTL5000_CHIP_ANA_POWER
,
177 SGTL5000_VAG_POWERUP
, SGTL5000_VAG_POWERUP
);
180 case SND_SOC_DAPM_PRE_PMD
:
182 * Don't clear VAG_POWERUP, when both DAC and ADC are
183 * operational to prevent inadvertently starving the
186 if ((snd_soc_read(w
->codec
, SGTL5000_CHIP_ANA_POWER
) &
188 snd_soc_update_bits(w
->codec
, SGTL5000_CHIP_ANA_POWER
,
189 SGTL5000_VAG_POWERUP
, 0);
200 /* input sources for ADC */
201 static const char *adc_mux_text
[] = {
205 static SOC_ENUM_SINGLE_DECL(adc_enum
,
206 SGTL5000_CHIP_ANA_CTRL
, 2,
209 static const struct snd_kcontrol_new adc_mux
=
210 SOC_DAPM_ENUM("Capture Mux", adc_enum
);
212 /* input sources for DAC */
213 static const char *dac_mux_text
[] = {
217 static SOC_ENUM_SINGLE_DECL(dac_enum
,
218 SGTL5000_CHIP_ANA_CTRL
, 6,
221 static const struct snd_kcontrol_new dac_mux
=
222 SOC_DAPM_ENUM("Headphone Mux", dac_enum
);
224 static const struct snd_soc_dapm_widget sgtl5000_dapm_widgets
[] = {
225 SND_SOC_DAPM_INPUT("LINE_IN"),
226 SND_SOC_DAPM_INPUT("MIC_IN"),
228 SND_SOC_DAPM_OUTPUT("HP_OUT"),
229 SND_SOC_DAPM_OUTPUT("LINE_OUT"),
231 SND_SOC_DAPM_SUPPLY("Mic Bias", SGTL5000_CHIP_MIC_CTRL
, 8, 0,
233 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
235 SND_SOC_DAPM_PGA("HP", SGTL5000_CHIP_ANA_POWER
, 4, 0, NULL
, 0),
236 SND_SOC_DAPM_PGA("LO", SGTL5000_CHIP_ANA_POWER
, 0, 0, NULL
, 0),
238 SND_SOC_DAPM_MUX("Capture Mux", SND_SOC_NOPM
, 0, 0, &adc_mux
),
239 SND_SOC_DAPM_MUX("Headphone Mux", SND_SOC_NOPM
, 0, 0, &dac_mux
),
241 /* aif for i2s input */
242 SND_SOC_DAPM_AIF_IN("AIFIN", "Playback",
243 0, SGTL5000_CHIP_DIG_POWER
,
246 /* aif for i2s output */
247 SND_SOC_DAPM_AIF_OUT("AIFOUT", "Capture",
248 0, SGTL5000_CHIP_DIG_POWER
,
251 SND_SOC_DAPM_ADC("ADC", "Capture", SGTL5000_CHIP_ANA_POWER
, 1, 0),
252 SND_SOC_DAPM_DAC("DAC", "Playback", SGTL5000_CHIP_ANA_POWER
, 3, 0),
254 SND_SOC_DAPM_PRE("VAG_POWER_PRE", power_vag_event
),
255 SND_SOC_DAPM_POST("VAG_POWER_POST", power_vag_event
),
258 /* routes for sgtl5000 */
259 static const struct snd_soc_dapm_route sgtl5000_dapm_routes
[] = {
260 {"Capture Mux", "LINE_IN", "LINE_IN"}, /* line_in --> adc_mux */
261 {"Capture Mux", "MIC_IN", "MIC_IN"}, /* mic_in --> adc_mux */
263 {"ADC", NULL
, "Capture Mux"}, /* adc_mux --> adc */
264 {"AIFOUT", NULL
, "ADC"}, /* adc --> i2s_out */
266 {"DAC", NULL
, "AIFIN"}, /* i2s-->dac,skip audio mux */
267 {"Headphone Mux", "DAC", "DAC"}, /* dac --> hp_mux */
268 {"LO", NULL
, "DAC"}, /* dac --> line_out */
270 {"Headphone Mux", "LINE_IN", "LINE_IN"},/* line_in --> hp_mux */
271 {"HP", NULL
, "Headphone Mux"}, /* hp_mux --> hp */
273 {"LINE_OUT", NULL
, "LO"},
274 {"HP_OUT", NULL
, "HP"},
277 /* custom function to fetch info of PCM playback volume */
278 static int dac_info_volsw(struct snd_kcontrol
*kcontrol
,
279 struct snd_ctl_elem_info
*uinfo
)
281 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
283 uinfo
->value
.integer
.min
= 0;
284 uinfo
->value
.integer
.max
= 0xfc - 0x3c;
289 * custom function to get of PCM playback volume
291 * dac volume register
292 * 15-------------8-7--------------0
293 * | R channel vol | L channel vol |
294 * -------------------------------
296 * PCM volume with 0.5017 dB steps from 0 to -90 dB
298 * register values map to dB
299 * 0x3B and less = Reserved
303 * 0xFC and greater = Muted
305 * register value map to userspace value
307 * register value 0x3c(0dB) 0xf0(-90dB)0xfc
308 * ------------------------------
309 * userspace value 0xc0 0
311 static int dac_get_volsw(struct snd_kcontrol
*kcontrol
,
312 struct snd_ctl_elem_value
*ucontrol
)
314 struct snd_soc_codec
*codec
= snd_soc_kcontrol_codec(kcontrol
);
319 reg
= snd_soc_read(codec
, SGTL5000_CHIP_DAC_VOL
);
321 /* get left channel volume */
322 l
= (reg
& SGTL5000_DAC_VOL_LEFT_MASK
) >> SGTL5000_DAC_VOL_LEFT_SHIFT
;
324 /* get right channel volume */
325 r
= (reg
& SGTL5000_DAC_VOL_RIGHT_MASK
) >> SGTL5000_DAC_VOL_RIGHT_SHIFT
;
327 /* make sure value fall in (0x3c,0xfc) */
328 l
= clamp(l
, 0x3c, 0xfc);
329 r
= clamp(r
, 0x3c, 0xfc);
331 /* invert it and map to userspace value */
335 ucontrol
->value
.integer
.value
[0] = l
;
336 ucontrol
->value
.integer
.value
[1] = r
;
342 * custom function to put of PCM playback volume
344 * dac volume register
345 * 15-------------8-7--------------0
346 * | R channel vol | L channel vol |
347 * -------------------------------
349 * PCM volume with 0.5017 dB steps from 0 to -90 dB
351 * register values map to dB
352 * 0x3B and less = Reserved
356 * 0xFC and greater = Muted
358 * userspace value map to register value
360 * userspace value 0xc0 0
361 * ------------------------------
362 * register value 0x3c(0dB) 0xf0(-90dB)0xfc
364 static int dac_put_volsw(struct snd_kcontrol
*kcontrol
,
365 struct snd_ctl_elem_value
*ucontrol
)
367 struct snd_soc_codec
*codec
= snd_soc_kcontrol_codec(kcontrol
);
372 l
= ucontrol
->value
.integer
.value
[0];
373 r
= ucontrol
->value
.integer
.value
[1];
375 /* make sure userspace volume fall in (0, 0xfc-0x3c) */
376 l
= clamp(l
, 0, 0xfc - 0x3c);
377 r
= clamp(r
, 0, 0xfc - 0x3c);
379 /* invert it, get the value can be set to register */
383 /* shift to get the register value */
384 reg
= l
<< SGTL5000_DAC_VOL_LEFT_SHIFT
|
385 r
<< SGTL5000_DAC_VOL_RIGHT_SHIFT
;
387 snd_soc_write(codec
, SGTL5000_CHIP_DAC_VOL
, reg
);
392 static const DECLARE_TLV_DB_SCALE(capture_6db_attenuate
, -600, 600, 0);
394 /* tlv for mic gain, 0db 20db 30db 40db */
395 static const unsigned int mic_gain_tlv
[] = {
396 TLV_DB_RANGE_HEAD(2),
397 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
398 1, 3, TLV_DB_SCALE_ITEM(2000, 1000, 0),
401 /* tlv for hp volume, -51.5db to 12.0db, step .5db */
402 static const DECLARE_TLV_DB_SCALE(headphone_volume
, -5150, 50, 0);
404 static const struct snd_kcontrol_new sgtl5000_snd_controls
[] = {
405 /* SOC_DOUBLE_S8_TLV with invert */
407 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
408 .name
= "PCM Playback Volume",
409 .access
= SNDRV_CTL_ELEM_ACCESS_TLV_READ
|
410 SNDRV_CTL_ELEM_ACCESS_READWRITE
,
411 .info
= dac_info_volsw
,
412 .get
= dac_get_volsw
,
413 .put
= dac_put_volsw
,
416 SOC_DOUBLE("Capture Volume", SGTL5000_CHIP_ANA_ADC_CTRL
, 0, 4, 0xf, 0),
417 SOC_SINGLE_TLV("Capture Attenuate Switch (-6dB)",
418 SGTL5000_CHIP_ANA_ADC_CTRL
,
419 8, 1, 0, capture_6db_attenuate
),
420 SOC_SINGLE("Capture ZC Switch", SGTL5000_CHIP_ANA_CTRL
, 1, 1, 0),
422 SOC_DOUBLE_TLV("Headphone Playback Volume",
423 SGTL5000_CHIP_ANA_HP_CTRL
,
427 SOC_SINGLE("Headphone Playback ZC Switch", SGTL5000_CHIP_ANA_CTRL
,
430 SOC_SINGLE_TLV("Mic Volume", SGTL5000_CHIP_MIC_CTRL
,
431 0, 3, 0, mic_gain_tlv
),
434 /* mute the codec used by alsa core */
435 static int sgtl5000_digital_mute(struct snd_soc_dai
*codec_dai
, int mute
)
437 struct snd_soc_codec
*codec
= codec_dai
->codec
;
438 u16 adcdac_ctrl
= SGTL5000_DAC_MUTE_LEFT
| SGTL5000_DAC_MUTE_RIGHT
;
440 snd_soc_update_bits(codec
, SGTL5000_CHIP_ADCDAC_CTRL
,
441 adcdac_ctrl
, mute
? adcdac_ctrl
: 0);
446 /* set codec format */
447 static int sgtl5000_set_dai_fmt(struct snd_soc_dai
*codec_dai
, unsigned int fmt
)
449 struct snd_soc_codec
*codec
= codec_dai
->codec
;
450 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
453 sgtl5000
->master
= 0;
455 * i2s clock and frame master setting.
457 * - clock and frame slave,
458 * - clock and frame master
460 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
461 case SND_SOC_DAIFMT_CBS_CFS
:
463 case SND_SOC_DAIFMT_CBM_CFM
:
464 i2sctl
|= SGTL5000_I2S_MASTER
;
465 sgtl5000
->master
= 1;
471 /* setting i2s data format */
472 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
473 case SND_SOC_DAIFMT_DSP_A
:
474 i2sctl
|= SGTL5000_I2S_MODE_PCM
;
476 case SND_SOC_DAIFMT_DSP_B
:
477 i2sctl
|= SGTL5000_I2S_MODE_PCM
;
478 i2sctl
|= SGTL5000_I2S_LRALIGN
;
480 case SND_SOC_DAIFMT_I2S
:
481 i2sctl
|= SGTL5000_I2S_MODE_I2S_LJ
;
483 case SND_SOC_DAIFMT_RIGHT_J
:
484 i2sctl
|= SGTL5000_I2S_MODE_RJ
;
485 i2sctl
|= SGTL5000_I2S_LRPOL
;
487 case SND_SOC_DAIFMT_LEFT_J
:
488 i2sctl
|= SGTL5000_I2S_MODE_I2S_LJ
;
489 i2sctl
|= SGTL5000_I2S_LRALIGN
;
495 sgtl5000
->fmt
= fmt
& SND_SOC_DAIFMT_FORMAT_MASK
;
497 /* Clock inversion */
498 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
499 case SND_SOC_DAIFMT_NB_NF
:
501 case SND_SOC_DAIFMT_IB_NF
:
502 i2sctl
|= SGTL5000_I2S_SCLK_INV
;
508 snd_soc_write(codec
, SGTL5000_CHIP_I2S_CTRL
, i2sctl
);
513 /* set codec sysclk */
514 static int sgtl5000_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
515 int clk_id
, unsigned int freq
, int dir
)
517 struct snd_soc_codec
*codec
= codec_dai
->codec
;
518 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
521 case SGTL5000_SYSCLK
:
522 sgtl5000
->sysclk
= freq
;
532 * set clock according to i2s frame clock,
533 * sgtl5000 provide 2 clock sources.
534 * 1. sys_mclk. sample freq can only configure to
535 * 1/256, 1/384, 1/512 of sys_mclk.
536 * 2. pll. can derive any audio clocks.
538 * clock setting rules:
539 * 1. in slave mode, only sys_mclk can use.
540 * 2. as constraint by sys_mclk, sample freq should
541 * set to 32k, 44.1k and above.
542 * 3. using sys_mclk prefer to pll to save power.
544 static int sgtl5000_set_clock(struct snd_soc_codec
*codec
, int frame_rate
)
546 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
548 int sys_fs
; /* sample freq */
551 * sample freq should be divided by frame clock,
552 * if frame clock lower than 44.1khz, sample feq should set to
555 switch (frame_rate
) {
569 /* set divided factor of frame clock */
570 switch (sys_fs
/ frame_rate
) {
572 clk_ctl
|= SGTL5000_RATE_MODE_DIV_4
<< SGTL5000_RATE_MODE_SHIFT
;
575 clk_ctl
|= SGTL5000_RATE_MODE_DIV_2
<< SGTL5000_RATE_MODE_SHIFT
;
578 clk_ctl
|= SGTL5000_RATE_MODE_DIV_1
<< SGTL5000_RATE_MODE_SHIFT
;
584 /* set the sys_fs according to frame rate */
587 clk_ctl
|= SGTL5000_SYS_FS_32k
<< SGTL5000_SYS_FS_SHIFT
;
590 clk_ctl
|= SGTL5000_SYS_FS_44_1k
<< SGTL5000_SYS_FS_SHIFT
;
593 clk_ctl
|= SGTL5000_SYS_FS_48k
<< SGTL5000_SYS_FS_SHIFT
;
596 clk_ctl
|= SGTL5000_SYS_FS_96k
<< SGTL5000_SYS_FS_SHIFT
;
599 dev_err(codec
->dev
, "frame rate %d not supported\n",
605 * calculate the divider of mclk/sample_freq,
606 * factor of freq =96k can only be 256, since mclk in range (12m,27m)
608 switch (sgtl5000
->sysclk
/ sys_fs
) {
610 clk_ctl
|= SGTL5000_MCLK_FREQ_256FS
<<
611 SGTL5000_MCLK_FREQ_SHIFT
;
614 clk_ctl
|= SGTL5000_MCLK_FREQ_384FS
<<
615 SGTL5000_MCLK_FREQ_SHIFT
;
618 clk_ctl
|= SGTL5000_MCLK_FREQ_512FS
<<
619 SGTL5000_MCLK_FREQ_SHIFT
;
622 /* if mclk not satisify the divider, use pll */
623 if (sgtl5000
->master
) {
624 clk_ctl
|= SGTL5000_MCLK_FREQ_PLL
<<
625 SGTL5000_MCLK_FREQ_SHIFT
;
628 "PLL not supported in slave mode\n");
633 /* if using pll, please check manual 6.4.2 for detail */
634 if ((clk_ctl
& SGTL5000_MCLK_FREQ_MASK
) == SGTL5000_MCLK_FREQ_PLL
) {
638 unsigned int in
, int_div
, frac_div
;
640 if (sgtl5000
->sysclk
> 17000000) {
642 in
= sgtl5000
->sysclk
/ 2;
645 in
= sgtl5000
->sysclk
;
656 pll_ctl
= int_div
<< SGTL5000_PLL_INT_DIV_SHIFT
|
657 frac_div
<< SGTL5000_PLL_FRAC_DIV_SHIFT
;
659 snd_soc_write(codec
, SGTL5000_CHIP_PLL_CTRL
, pll_ctl
);
661 snd_soc_update_bits(codec
,
662 SGTL5000_CHIP_CLK_TOP_CTRL
,
663 SGTL5000_INPUT_FREQ_DIV2
,
664 SGTL5000_INPUT_FREQ_DIV2
);
666 snd_soc_update_bits(codec
,
667 SGTL5000_CHIP_CLK_TOP_CTRL
,
668 SGTL5000_INPUT_FREQ_DIV2
,
672 snd_soc_update_bits(codec
, SGTL5000_CHIP_ANA_POWER
,
673 SGTL5000_PLL_POWERUP
| SGTL5000_VCOAMP_POWERUP
,
674 SGTL5000_PLL_POWERUP
| SGTL5000_VCOAMP_POWERUP
);
676 /* if using pll, clk_ctrl must be set after pll power up */
677 snd_soc_write(codec
, SGTL5000_CHIP_CLK_CTRL
, clk_ctl
);
679 /* otherwise, clk_ctrl must be set before pll power down */
680 snd_soc_write(codec
, SGTL5000_CHIP_CLK_CTRL
, clk_ctl
);
683 snd_soc_update_bits(codec
, SGTL5000_CHIP_ANA_POWER
,
684 SGTL5000_PLL_POWERUP
| SGTL5000_VCOAMP_POWERUP
,
692 * Set PCM DAI bit size and sample rate.
693 * input: params_rate, params_fmt
695 static int sgtl5000_pcm_hw_params(struct snd_pcm_substream
*substream
,
696 struct snd_pcm_hw_params
*params
,
697 struct snd_soc_dai
*dai
)
699 struct snd_soc_codec
*codec
= dai
->codec
;
700 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
701 int channels
= params_channels(params
);
706 /* sysclk should already set */
707 if (!sgtl5000
->sysclk
) {
708 dev_err(codec
->dev
, "%s: set sysclk first!\n", __func__
);
712 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
713 stereo
= SGTL5000_DAC_STEREO
;
715 stereo
= SGTL5000_ADC_STEREO
;
717 /* set mono to save power */
718 snd_soc_update_bits(codec
, SGTL5000_CHIP_ANA_POWER
, stereo
,
719 channels
== 1 ? 0 : stereo
);
721 /* set codec clock base on lrclk */
722 ret
= sgtl5000_set_clock(codec
, params_rate(params
));
726 /* set i2s data format */
727 switch (params_format(params
)) {
728 case SNDRV_PCM_FORMAT_S16_LE
:
729 if (sgtl5000
->fmt
== SND_SOC_DAIFMT_RIGHT_J
)
731 i2s_ctl
|= SGTL5000_I2S_DLEN_16
<< SGTL5000_I2S_DLEN_SHIFT
;
732 i2s_ctl
|= SGTL5000_I2S_SCLKFREQ_32FS
<<
733 SGTL5000_I2S_SCLKFREQ_SHIFT
;
735 case SNDRV_PCM_FORMAT_S20_3LE
:
736 i2s_ctl
|= SGTL5000_I2S_DLEN_20
<< SGTL5000_I2S_DLEN_SHIFT
;
737 i2s_ctl
|= SGTL5000_I2S_SCLKFREQ_64FS
<<
738 SGTL5000_I2S_SCLKFREQ_SHIFT
;
740 case SNDRV_PCM_FORMAT_S24_LE
:
741 i2s_ctl
|= SGTL5000_I2S_DLEN_24
<< SGTL5000_I2S_DLEN_SHIFT
;
742 i2s_ctl
|= SGTL5000_I2S_SCLKFREQ_64FS
<<
743 SGTL5000_I2S_SCLKFREQ_SHIFT
;
745 case SNDRV_PCM_FORMAT_S32_LE
:
746 if (sgtl5000
->fmt
== SND_SOC_DAIFMT_RIGHT_J
)
748 i2s_ctl
|= SGTL5000_I2S_DLEN_32
<< SGTL5000_I2S_DLEN_SHIFT
;
749 i2s_ctl
|= SGTL5000_I2S_SCLKFREQ_64FS
<<
750 SGTL5000_I2S_SCLKFREQ_SHIFT
;
756 snd_soc_update_bits(codec
, SGTL5000_CHIP_I2S_CTRL
,
757 SGTL5000_I2S_DLEN_MASK
| SGTL5000_I2S_SCLKFREQ_MASK
,
763 #ifdef CONFIG_REGULATOR
764 static int ldo_regulator_is_enabled(struct regulator_dev
*dev
)
766 struct ldo_regulator
*ldo
= rdev_get_drvdata(dev
);
771 static int ldo_regulator_enable(struct regulator_dev
*dev
)
773 struct ldo_regulator
*ldo
= rdev_get_drvdata(dev
);
774 struct snd_soc_codec
*codec
= (struct snd_soc_codec
*)ldo
->codec_data
;
777 if (ldo_regulator_is_enabled(dev
))
780 /* set regulator value firstly */
781 reg
= (1600 - ldo
->voltage
/ 1000) / 50;
782 reg
= clamp(reg
, 0x0, 0xf);
784 /* amend the voltage value, unit: uV */
785 ldo
->voltage
= (1600 - reg
* 50) * 1000;
787 /* set voltage to register */
788 snd_soc_update_bits(codec
, SGTL5000_CHIP_LINREG_CTRL
,
789 SGTL5000_LINREG_VDDD_MASK
, reg
);
791 snd_soc_update_bits(codec
, SGTL5000_CHIP_ANA_POWER
,
792 SGTL5000_LINEREG_D_POWERUP
,
793 SGTL5000_LINEREG_D_POWERUP
);
795 /* when internal ldo enabled, simple digital power can be disabled */
796 snd_soc_update_bits(codec
, SGTL5000_CHIP_ANA_POWER
,
797 SGTL5000_LINREG_SIMPLE_POWERUP
,
804 static int ldo_regulator_disable(struct regulator_dev
*dev
)
806 struct ldo_regulator
*ldo
= rdev_get_drvdata(dev
);
807 struct snd_soc_codec
*codec
= (struct snd_soc_codec
*)ldo
->codec_data
;
809 snd_soc_update_bits(codec
, SGTL5000_CHIP_ANA_POWER
,
810 SGTL5000_LINEREG_D_POWERUP
,
813 /* clear voltage info */
814 snd_soc_update_bits(codec
, SGTL5000_CHIP_LINREG_CTRL
,
815 SGTL5000_LINREG_VDDD_MASK
, 0);
822 static int ldo_regulator_get_voltage(struct regulator_dev
*dev
)
824 struct ldo_regulator
*ldo
= rdev_get_drvdata(dev
);
829 static struct regulator_ops ldo_regulator_ops
= {
830 .is_enabled
= ldo_regulator_is_enabled
,
831 .enable
= ldo_regulator_enable
,
832 .disable
= ldo_regulator_disable
,
833 .get_voltage
= ldo_regulator_get_voltage
,
836 static int ldo_regulator_register(struct snd_soc_codec
*codec
,
837 struct regulator_init_data
*init_data
,
840 struct ldo_regulator
*ldo
;
841 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
842 struct regulator_config config
= { };
844 ldo
= kzalloc(sizeof(struct ldo_regulator
), GFP_KERNEL
);
847 dev_err(codec
->dev
, "failed to allocate ldo_regulator\n");
851 ldo
->desc
.name
= kstrdup(dev_name(codec
->dev
), GFP_KERNEL
);
852 if (!ldo
->desc
.name
) {
854 dev_err(codec
->dev
, "failed to allocate decs name memory\n");
858 ldo
->desc
.type
= REGULATOR_VOLTAGE
;
859 ldo
->desc
.owner
= THIS_MODULE
;
860 ldo
->desc
.ops
= &ldo_regulator_ops
;
861 ldo
->desc
.n_voltages
= 1;
863 ldo
->codec_data
= codec
;
864 ldo
->voltage
= voltage
;
866 config
.dev
= codec
->dev
;
867 config
.driver_data
= ldo
;
868 config
.init_data
= init_data
;
870 ldo
->dev
= regulator_register(&ldo
->desc
, &config
);
871 if (IS_ERR(ldo
->dev
)) {
872 int ret
= PTR_ERR(ldo
->dev
);
874 dev_err(codec
->dev
, "failed to register regulator\n");
875 kfree(ldo
->desc
.name
);
885 static int ldo_regulator_remove(struct snd_soc_codec
*codec
)
887 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
888 struct ldo_regulator
*ldo
= sgtl5000
->ldo
;
893 regulator_unregister(ldo
->dev
);
894 kfree(ldo
->desc
.name
);
900 static int ldo_regulator_register(struct snd_soc_codec
*codec
,
901 struct regulator_init_data
*init_data
,
904 dev_err(codec
->dev
, "this setup needs regulator support in the kernel\n");
908 static int ldo_regulator_remove(struct snd_soc_codec
*codec
)
916 * common state changes:
918 * off --> standby --> prepare --> on
919 * standby --> prepare --> on
922 * on --> prepare --> standby
924 static int sgtl5000_set_bias_level(struct snd_soc_codec
*codec
,
925 enum snd_soc_bias_level level
)
928 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
931 case SND_SOC_BIAS_ON
:
932 case SND_SOC_BIAS_PREPARE
:
934 case SND_SOC_BIAS_STANDBY
:
935 if (codec
->dapm
.bias_level
== SND_SOC_BIAS_OFF
) {
936 ret
= regulator_bulk_enable(
937 ARRAY_SIZE(sgtl5000
->supplies
),
943 regcache_cache_only(sgtl5000
->regmap
, false);
945 ret
= regcache_sync(sgtl5000
->regmap
);
948 "Failed to restore cache: %d\n", ret
);
950 regcache_cache_only(sgtl5000
->regmap
, true);
951 regulator_bulk_disable(ARRAY_SIZE(sgtl5000
->supplies
),
959 case SND_SOC_BIAS_OFF
:
960 regcache_cache_only(sgtl5000
->regmap
, true);
961 regulator_bulk_disable(ARRAY_SIZE(sgtl5000
->supplies
),
966 codec
->dapm
.bias_level
= level
;
970 #define SGTL5000_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
971 SNDRV_PCM_FMTBIT_S20_3LE |\
972 SNDRV_PCM_FMTBIT_S24_LE |\
973 SNDRV_PCM_FMTBIT_S32_LE)
975 static const struct snd_soc_dai_ops sgtl5000_ops
= {
976 .hw_params
= sgtl5000_pcm_hw_params
,
977 .digital_mute
= sgtl5000_digital_mute
,
978 .set_fmt
= sgtl5000_set_dai_fmt
,
979 .set_sysclk
= sgtl5000_set_dai_sysclk
,
982 static struct snd_soc_dai_driver sgtl5000_dai
= {
985 .stream_name
= "Playback",
989 * only support 8~48K + 96K,
990 * TODO modify hw_param to support more
992 .rates
= SNDRV_PCM_RATE_8000_48000
| SNDRV_PCM_RATE_96000
,
993 .formats
= SGTL5000_FORMATS
,
996 .stream_name
= "Capture",
999 .rates
= SNDRV_PCM_RATE_8000_48000
| SNDRV_PCM_RATE_96000
,
1000 .formats
= SGTL5000_FORMATS
,
1002 .ops
= &sgtl5000_ops
,
1003 .symmetric_rates
= 1,
1006 static bool sgtl5000_volatile(struct device
*dev
, unsigned int reg
)
1009 case SGTL5000_CHIP_ID
:
1010 case SGTL5000_CHIP_ADCDAC_CTRL
:
1011 case SGTL5000_CHIP_ANA_STATUS
:
1018 static bool sgtl5000_readable(struct device
*dev
, unsigned int reg
)
1021 case SGTL5000_CHIP_ID
:
1022 case SGTL5000_CHIP_DIG_POWER
:
1023 case SGTL5000_CHIP_CLK_CTRL
:
1024 case SGTL5000_CHIP_I2S_CTRL
:
1025 case SGTL5000_CHIP_SSS_CTRL
:
1026 case SGTL5000_CHIP_ADCDAC_CTRL
:
1027 case SGTL5000_CHIP_DAC_VOL
:
1028 case SGTL5000_CHIP_PAD_STRENGTH
:
1029 case SGTL5000_CHIP_ANA_ADC_CTRL
:
1030 case SGTL5000_CHIP_ANA_HP_CTRL
:
1031 case SGTL5000_CHIP_ANA_CTRL
:
1032 case SGTL5000_CHIP_LINREG_CTRL
:
1033 case SGTL5000_CHIP_REF_CTRL
:
1034 case SGTL5000_CHIP_MIC_CTRL
:
1035 case SGTL5000_CHIP_LINE_OUT_CTRL
:
1036 case SGTL5000_CHIP_LINE_OUT_VOL
:
1037 case SGTL5000_CHIP_ANA_POWER
:
1038 case SGTL5000_CHIP_PLL_CTRL
:
1039 case SGTL5000_CHIP_CLK_TOP_CTRL
:
1040 case SGTL5000_CHIP_ANA_STATUS
:
1041 case SGTL5000_CHIP_SHORT_CTRL
:
1042 case SGTL5000_CHIP_ANA_TEST2
:
1043 case SGTL5000_DAP_CTRL
:
1044 case SGTL5000_DAP_PEQ
:
1045 case SGTL5000_DAP_BASS_ENHANCE
:
1046 case SGTL5000_DAP_BASS_ENHANCE_CTRL
:
1047 case SGTL5000_DAP_AUDIO_EQ
:
1048 case SGTL5000_DAP_SURROUND
:
1049 case SGTL5000_DAP_FLT_COEF_ACCESS
:
1050 case SGTL5000_DAP_COEF_WR_B0_MSB
:
1051 case SGTL5000_DAP_COEF_WR_B0_LSB
:
1052 case SGTL5000_DAP_EQ_BASS_BAND0
:
1053 case SGTL5000_DAP_EQ_BASS_BAND1
:
1054 case SGTL5000_DAP_EQ_BASS_BAND2
:
1055 case SGTL5000_DAP_EQ_BASS_BAND3
:
1056 case SGTL5000_DAP_EQ_BASS_BAND4
:
1057 case SGTL5000_DAP_MAIN_CHAN
:
1058 case SGTL5000_DAP_MIX_CHAN
:
1059 case SGTL5000_DAP_AVC_CTRL
:
1060 case SGTL5000_DAP_AVC_THRESHOLD
:
1061 case SGTL5000_DAP_AVC_ATTACK
:
1062 case SGTL5000_DAP_AVC_DECAY
:
1063 case SGTL5000_DAP_COEF_WR_B1_MSB
:
1064 case SGTL5000_DAP_COEF_WR_B1_LSB
:
1065 case SGTL5000_DAP_COEF_WR_B2_MSB
:
1066 case SGTL5000_DAP_COEF_WR_B2_LSB
:
1067 case SGTL5000_DAP_COEF_WR_A1_MSB
:
1068 case SGTL5000_DAP_COEF_WR_A1_LSB
:
1069 case SGTL5000_DAP_COEF_WR_A2_MSB
:
1070 case SGTL5000_DAP_COEF_WR_A2_LSB
:
1078 #ifdef CONFIG_SUSPEND
1079 static int sgtl5000_suspend(struct snd_soc_codec
*codec
)
1081 sgtl5000_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
1086 static int sgtl5000_resume(struct snd_soc_codec
*codec
)
1088 /* Bring the codec back up to standby to enable regulators */
1089 sgtl5000_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
1094 #define sgtl5000_suspend NULL
1095 #define sgtl5000_resume NULL
1096 #endif /* CONFIG_SUSPEND */
1099 * sgtl5000 has 3 internal power supplies:
1100 * 1. VAG, normally set to vdda/2
1101 * 2. chargepump, set to different value
1102 * according to voltage of vdda and vddio
1103 * 3. line out VAG, normally set to vddio/2
1105 * and should be set according to:
1106 * 1. vddd provided by external or not
1107 * 2. vdda and vddio voltage value. > 3.1v or not
1108 * 3. chip revision >=0x11 or not. If >=0x11, not use external vddd.
1110 static int sgtl5000_set_power_regs(struct snd_soc_codec
*codec
)
1118 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
1120 vdda
= regulator_get_voltage(sgtl5000
->supplies
[VDDA
].consumer
);
1121 vddio
= regulator_get_voltage(sgtl5000
->supplies
[VDDIO
].consumer
);
1122 vddd
= regulator_get_voltage(sgtl5000
->supplies
[VDDD
].consumer
);
1125 vddio
= vddio
/ 1000;
1128 if (vdda
<= 0 || vddio
<= 0 || vddd
< 0) {
1129 dev_err(codec
->dev
, "regulator voltage not set correctly\n");
1134 /* according to datasheet, maximum voltage of supplies */
1135 if (vdda
> 3600 || vddio
> 3600 || vddd
> 1980) {
1137 "exceed max voltage vdda %dmV vddio %dmV vddd %dmV\n",
1144 ana_pwr
= snd_soc_read(codec
, SGTL5000_CHIP_ANA_POWER
);
1145 ana_pwr
|= SGTL5000_DAC_STEREO
|
1146 SGTL5000_ADC_STEREO
|
1147 SGTL5000_REFTOP_POWERUP
;
1148 lreg_ctrl
= snd_soc_read(codec
, SGTL5000_CHIP_LINREG_CTRL
);
1150 if (vddio
< 3100 && vdda
< 3100) {
1151 /* enable internal oscillator used for charge pump */
1152 snd_soc_update_bits(codec
, SGTL5000_CHIP_CLK_TOP_CTRL
,
1153 SGTL5000_INT_OSC_EN
,
1154 SGTL5000_INT_OSC_EN
);
1155 /* Enable VDDC charge pump */
1156 ana_pwr
|= SGTL5000_VDDC_CHRGPMP_POWERUP
;
1157 } else if (vddio
>= 3100 && vdda
>= 3100) {
1158 ana_pwr
&= ~SGTL5000_VDDC_CHRGPMP_POWERUP
;
1159 /* VDDC use VDDIO rail */
1160 lreg_ctrl
|= SGTL5000_VDDC_ASSN_OVRD
;
1161 lreg_ctrl
|= SGTL5000_VDDC_MAN_ASSN_VDDIO
<<
1162 SGTL5000_VDDC_MAN_ASSN_SHIFT
;
1165 snd_soc_write(codec
, SGTL5000_CHIP_LINREG_CTRL
, lreg_ctrl
);
1167 snd_soc_write(codec
, SGTL5000_CHIP_ANA_POWER
, ana_pwr
);
1169 /* set voltage to register */
1170 snd_soc_update_bits(codec
, SGTL5000_CHIP_LINREG_CTRL
,
1171 SGTL5000_LINREG_VDDD_MASK
, 0x8);
1174 * if vddd linear reg has been enabled,
1175 * simple digital supply should be clear to get
1176 * proper VDDD voltage.
1178 if (ana_pwr
& SGTL5000_LINEREG_D_POWERUP
)
1179 snd_soc_update_bits(codec
, SGTL5000_CHIP_ANA_POWER
,
1180 SGTL5000_LINREG_SIMPLE_POWERUP
,
1183 snd_soc_update_bits(codec
, SGTL5000_CHIP_ANA_POWER
,
1184 SGTL5000_LINREG_SIMPLE_POWERUP
|
1185 SGTL5000_STARTUP_POWERUP
,
1189 * set ADC/DAC VAG to vdda / 2,
1190 * should stay in range (0.8v, 1.575v)
1193 if (vag
<= SGTL5000_ANA_GND_BASE
)
1195 else if (vag
>= SGTL5000_ANA_GND_BASE
+ SGTL5000_ANA_GND_STP
*
1196 (SGTL5000_ANA_GND_MASK
>> SGTL5000_ANA_GND_SHIFT
))
1197 vag
= SGTL5000_ANA_GND_MASK
>> SGTL5000_ANA_GND_SHIFT
;
1199 vag
= (vag
- SGTL5000_ANA_GND_BASE
) / SGTL5000_ANA_GND_STP
;
1201 snd_soc_update_bits(codec
, SGTL5000_CHIP_REF_CTRL
,
1202 SGTL5000_ANA_GND_MASK
, vag
<< SGTL5000_ANA_GND_SHIFT
);
1204 /* set line out VAG to vddio / 2, in range (0.8v, 1.675v) */
1206 if (vag
<= SGTL5000_LINE_OUT_GND_BASE
)
1208 else if (vag
>= SGTL5000_LINE_OUT_GND_BASE
+
1209 SGTL5000_LINE_OUT_GND_STP
* SGTL5000_LINE_OUT_GND_MAX
)
1210 vag
= SGTL5000_LINE_OUT_GND_MAX
;
1212 vag
= (vag
- SGTL5000_LINE_OUT_GND_BASE
) /
1213 SGTL5000_LINE_OUT_GND_STP
;
1215 snd_soc_update_bits(codec
, SGTL5000_CHIP_LINE_OUT_CTRL
,
1216 SGTL5000_LINE_OUT_CURRENT_MASK
|
1217 SGTL5000_LINE_OUT_GND_MASK
,
1218 vag
<< SGTL5000_LINE_OUT_GND_SHIFT
|
1219 SGTL5000_LINE_OUT_CURRENT_360u
<<
1220 SGTL5000_LINE_OUT_CURRENT_SHIFT
);
1225 static int sgtl5000_replace_vddd_with_ldo(struct snd_soc_codec
*codec
)
1227 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
1230 /* set internal ldo to 1.2v */
1231 ret
= ldo_regulator_register(codec
, &ldo_init_data
, LDO_VOLTAGE
);
1234 "Failed to register vddd internal supplies: %d\n", ret
);
1238 sgtl5000
->supplies
[VDDD
].supply
= LDO_CONSUMER_NAME
;
1240 dev_info(codec
->dev
, "Using internal LDO instead of VDDD\n");
1244 static int sgtl5000_enable_regulators(struct snd_soc_codec
*codec
)
1248 int external_vddd
= 0;
1249 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
1250 struct regulator
*vddd
;
1252 for (i
= 0; i
< ARRAY_SIZE(sgtl5000
->supplies
); i
++)
1253 sgtl5000
->supplies
[i
].supply
= supply_names
[i
];
1255 /* External VDDD only works before revision 0x11 */
1256 if (sgtl5000
->revision
< 0x11) {
1257 vddd
= regulator_get_optional(codec
->dev
, "VDDD");
1259 /* See if it's just not registered yet */
1260 if (PTR_ERR(vddd
) == -EPROBE_DEFER
)
1261 return -EPROBE_DEFER
;
1264 regulator_put(vddd
);
1268 if (!external_vddd
) {
1269 ret
= sgtl5000_replace_vddd_with_ldo(codec
);
1274 ret
= devm_regulator_bulk_get(codec
->dev
, ARRAY_SIZE(sgtl5000
->supplies
),
1275 sgtl5000
->supplies
);
1277 goto err_ldo_remove
;
1279 ret
= regulator_bulk_enable(ARRAY_SIZE(sgtl5000
->supplies
),
1280 sgtl5000
->supplies
);
1282 goto err_ldo_remove
;
1284 /* wait for all power rails bring up */
1291 ldo_regulator_remove(codec
);
1296 static int sgtl5000_probe(struct snd_soc_codec
*codec
)
1299 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
1301 ret
= sgtl5000_enable_regulators(codec
);
1305 /* power up sgtl5000 */
1306 ret
= sgtl5000_set_power_regs(codec
);
1310 /* enable small pop, introduce 400ms delay in turning off */
1311 snd_soc_update_bits(codec
, SGTL5000_CHIP_REF_CTRL
,
1312 SGTL5000_SMALL_POP
, 1);
1314 /* disable short cut detector */
1315 snd_soc_write(codec
, SGTL5000_CHIP_SHORT_CTRL
, 0);
1318 * set i2s as default input of sound switch
1319 * TODO: add sound switch to control and dapm widge.
1321 snd_soc_write(codec
, SGTL5000_CHIP_SSS_CTRL
,
1322 SGTL5000_DAC_SEL_I2S_IN
<< SGTL5000_DAC_SEL_SHIFT
);
1323 snd_soc_write(codec
, SGTL5000_CHIP_DIG_POWER
,
1324 SGTL5000_ADC_EN
| SGTL5000_DAC_EN
);
1326 /* enable dac volume ramp by default */
1327 snd_soc_write(codec
, SGTL5000_CHIP_ADCDAC_CTRL
,
1328 SGTL5000_DAC_VOL_RAMP_EN
|
1329 SGTL5000_DAC_MUTE_RIGHT
|
1330 SGTL5000_DAC_MUTE_LEFT
);
1332 snd_soc_write(codec
, SGTL5000_CHIP_PAD_STRENGTH
, 0x015f);
1334 snd_soc_write(codec
, SGTL5000_CHIP_ANA_CTRL
,
1335 SGTL5000_HP_ZCD_EN
|
1336 SGTL5000_ADC_ZCD_EN
);
1338 snd_soc_write(codec
, SGTL5000_CHIP_MIC_CTRL
, 2);
1343 * Enable DAP in kcontrol and dapm.
1345 snd_soc_write(codec
, SGTL5000_DAP_CTRL
, 0);
1347 /* leading to standby state */
1348 ret
= sgtl5000_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
1355 regulator_bulk_disable(ARRAY_SIZE(sgtl5000
->supplies
),
1356 sgtl5000
->supplies
);
1357 ldo_regulator_remove(codec
);
1362 static int sgtl5000_remove(struct snd_soc_codec
*codec
)
1364 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
1366 sgtl5000_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
1368 regulator_bulk_disable(ARRAY_SIZE(sgtl5000
->supplies
),
1369 sgtl5000
->supplies
);
1370 ldo_regulator_remove(codec
);
1375 static struct snd_soc_codec_driver sgtl5000_driver
= {
1376 .probe
= sgtl5000_probe
,
1377 .remove
= sgtl5000_remove
,
1378 .suspend
= sgtl5000_suspend
,
1379 .resume
= sgtl5000_resume
,
1380 .set_bias_level
= sgtl5000_set_bias_level
,
1381 .controls
= sgtl5000_snd_controls
,
1382 .num_controls
= ARRAY_SIZE(sgtl5000_snd_controls
),
1383 .dapm_widgets
= sgtl5000_dapm_widgets
,
1384 .num_dapm_widgets
= ARRAY_SIZE(sgtl5000_dapm_widgets
),
1385 .dapm_routes
= sgtl5000_dapm_routes
,
1386 .num_dapm_routes
= ARRAY_SIZE(sgtl5000_dapm_routes
),
1389 static const struct regmap_config sgtl5000_regmap
= {
1394 .max_register
= SGTL5000_MAX_REG_OFFSET
,
1395 .volatile_reg
= sgtl5000_volatile
,
1396 .readable_reg
= sgtl5000_readable
,
1398 .cache_type
= REGCACHE_RBTREE
,
1399 .reg_defaults
= sgtl5000_reg_defaults
,
1400 .num_reg_defaults
= ARRAY_SIZE(sgtl5000_reg_defaults
),
1404 * Write all the default values from sgtl5000_reg_defaults[] array into the
1405 * sgtl5000 registers, to make sure we always start with the sane registers
1406 * values as stated in the datasheet.
1408 * Since sgtl5000 does not have a reset line, nor a reset command in software,
1409 * we follow this approach to guarantee we always start from the default values
1410 * and avoid problems like, not being able to probe after an audio playback
1411 * followed by a system reset or a 'reboot' command in Linux
1413 static int sgtl5000_fill_defaults(struct sgtl5000_priv
*sgtl5000
)
1415 int i
, ret
, val
, index
;
1417 for (i
= 0; i
< ARRAY_SIZE(sgtl5000_reg_defaults
); i
++) {
1418 val
= sgtl5000_reg_defaults
[i
].def
;
1419 index
= sgtl5000_reg_defaults
[i
].reg
;
1420 ret
= regmap_write(sgtl5000
->regmap
, index
, val
);
1428 static int sgtl5000_i2c_probe(struct i2c_client
*client
,
1429 const struct i2c_device_id
*id
)
1431 struct sgtl5000_priv
*sgtl5000
;
1434 sgtl5000
= devm_kzalloc(&client
->dev
, sizeof(struct sgtl5000_priv
),
1439 sgtl5000
->regmap
= devm_regmap_init_i2c(client
, &sgtl5000_regmap
);
1440 if (IS_ERR(sgtl5000
->regmap
)) {
1441 ret
= PTR_ERR(sgtl5000
->regmap
);
1442 dev_err(&client
->dev
, "Failed to allocate regmap: %d\n", ret
);
1446 sgtl5000
->mclk
= devm_clk_get(&client
->dev
, NULL
);
1447 if (IS_ERR(sgtl5000
->mclk
)) {
1448 ret
= PTR_ERR(sgtl5000
->mclk
);
1449 dev_err(&client
->dev
, "Failed to get mclock: %d\n", ret
);
1450 /* Defer the probe to see if the clk will be provided later */
1452 return -EPROBE_DEFER
;
1456 ret
= clk_prepare_enable(sgtl5000
->mclk
);
1460 /* Need 8 clocks before I2C accesses */
1463 /* read chip information */
1464 ret
= regmap_read(sgtl5000
->regmap
, SGTL5000_CHIP_ID
, ®
);
1468 if (((reg
& SGTL5000_PARTID_MASK
) >> SGTL5000_PARTID_SHIFT
) !=
1469 SGTL5000_PARTID_PART_ID
) {
1470 dev_err(&client
->dev
,
1471 "Device with ID register %x is not a sgtl5000\n", reg
);
1476 rev
= (reg
& SGTL5000_REVID_MASK
) >> SGTL5000_REVID_SHIFT
;
1477 dev_info(&client
->dev
, "sgtl5000 revision 0x%x\n", rev
);
1478 sgtl5000
->revision
= rev
;
1480 i2c_set_clientdata(client
, sgtl5000
);
1482 /* Ensure sgtl5000 will start with sane register values */
1483 ret
= sgtl5000_fill_defaults(sgtl5000
);
1487 ret
= snd_soc_register_codec(&client
->dev
,
1488 &sgtl5000_driver
, &sgtl5000_dai
, 1);
1495 clk_disable_unprepare(sgtl5000
->mclk
);
1499 static int sgtl5000_i2c_remove(struct i2c_client
*client
)
1501 struct sgtl5000_priv
*sgtl5000
= i2c_get_clientdata(client
);
1503 snd_soc_unregister_codec(&client
->dev
);
1504 clk_disable_unprepare(sgtl5000
->mclk
);
1508 static const struct i2c_device_id sgtl5000_id
[] = {
1513 MODULE_DEVICE_TABLE(i2c
, sgtl5000_id
);
1515 static const struct of_device_id sgtl5000_dt_ids
[] = {
1516 { .compatible
= "fsl,sgtl5000", },
1519 MODULE_DEVICE_TABLE(of
, sgtl5000_dt_ids
);
1521 static struct i2c_driver sgtl5000_i2c_driver
= {
1524 .owner
= THIS_MODULE
,
1525 .of_match_table
= sgtl5000_dt_ids
,
1527 .probe
= sgtl5000_i2c_probe
,
1528 .remove
= sgtl5000_i2c_remove
,
1529 .id_table
= sgtl5000_id
,
1532 module_i2c_driver(sgtl5000_i2c_driver
);
1534 MODULE_DESCRIPTION("Freescale SGTL5000 ALSA SoC Codec Driver");
1535 MODULE_AUTHOR("Zeng Zhaoming <zengzm.kernel@gmail.com>");
1536 MODULE_LICENSE("GPL");