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/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/regulator/consumer.h>
23 #include <sound/core.h>
24 #include <sound/tlv.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
28 #include <sound/soc-dapm.h>
29 #include <sound/initval.h>
33 #define SGTL5000_DAP_REG_OFFSET 0x0100
34 #define SGTL5000_MAX_REG_OFFSET 0x013A
36 /* default value of sgtl5000 registers except DAP */
37 static const u16 sgtl5000_regs
[SGTL5000_MAX_REG_OFFSET
>> 1] = {
38 0xa011, /* 0x0000, CHIP_ID. 11 stand for revison 17 */
39 0x0000, /* 0x0002, CHIP_DIG_POWER. */
40 0x0008, /* 0x0004, CHIP_CKL_CTRL */
41 0x0010, /* 0x0006, CHIP_I2S_CTRL */
42 0x0000, /* 0x0008, reserved */
43 0x0008, /* 0x000A, CHIP_SSS_CTRL */
44 0x0000, /* 0x000C, reserved */
45 0x020c, /* 0x000E, CHIP_ADCDAC_CTRL */
46 0x3c3c, /* 0x0010, CHIP_DAC_VOL */
47 0x0000, /* 0x0012, reserved */
48 0x015f, /* 0x0014, CHIP_PAD_STRENGTH */
49 0x0000, /* 0x0016, reserved */
50 0x0000, /* 0x0018, reserved */
51 0x0000, /* 0x001A, reserved */
52 0x0000, /* 0x001E, reserved */
53 0x0000, /* 0x0020, CHIP_ANA_ADC_CTRL */
54 0x1818, /* 0x0022, CHIP_ANA_HP_CTRL */
55 0x0111, /* 0x0024, CHIP_ANN_CTRL */
56 0x0000, /* 0x0026, CHIP_LINREG_CTRL */
57 0x0000, /* 0x0028, CHIP_REF_CTRL */
58 0x0000, /* 0x002A, CHIP_MIC_CTRL */
59 0x0000, /* 0x002C, CHIP_LINE_OUT_CTRL */
60 0x0404, /* 0x002E, CHIP_LINE_OUT_VOL */
61 0x7060, /* 0x0030, CHIP_ANA_POWER */
62 0x5000, /* 0x0032, CHIP_PLL_CTRL */
63 0x0000, /* 0x0034, CHIP_CLK_TOP_CTRL */
64 0x0000, /* 0x0036, CHIP_ANA_STATUS */
65 0x0000, /* 0x0038, reserved */
66 0x0000, /* 0x003A, CHIP_ANA_TEST2 */
67 0x0000, /* 0x003C, CHIP_SHORT_CTRL */
68 0x0000, /* reserved */
71 /* default value of dap registers */
72 static const u16 sgtl5000_dap_regs
[] = {
73 0x0000, /* 0x0100, DAP_CONTROL */
74 0x0000, /* 0x0102, DAP_PEQ */
75 0x0040, /* 0x0104, DAP_BASS_ENHANCE */
76 0x051f, /* 0x0106, DAP_BASS_ENHANCE_CTRL */
77 0x0000, /* 0x0108, DAP_AUDIO_EQ */
78 0x0040, /* 0x010A, DAP_SGTL_SURROUND */
79 0x0000, /* 0x010C, DAP_FILTER_COEF_ACCESS */
80 0x0000, /* 0x010E, DAP_COEF_WR_B0_MSB */
81 0x0000, /* 0x0110, DAP_COEF_WR_B0_LSB */
82 0x0000, /* 0x0112, reserved */
83 0x0000, /* 0x0114, reserved */
84 0x002f, /* 0x0116, DAP_AUDIO_EQ_BASS_BAND0 */
85 0x002f, /* 0x0118, DAP_AUDIO_EQ_BAND0 */
86 0x002f, /* 0x011A, DAP_AUDIO_EQ_BAND2 */
87 0x002f, /* 0x011C, DAP_AUDIO_EQ_BAND3 */
88 0x002f, /* 0x011E, DAP_AUDIO_EQ_TREBLE_BAND4 */
89 0x8000, /* 0x0120, DAP_MAIN_CHAN */
90 0x0000, /* 0x0122, DAP_MIX_CHAN */
91 0x0510, /* 0x0124, DAP_AVC_CTRL */
92 0x1473, /* 0x0126, DAP_AVC_THRESHOLD */
93 0x0028, /* 0x0128, DAP_AVC_ATTACK */
94 0x0050, /* 0x012A, DAP_AVC_DECAY */
95 0x0000, /* 0x012C, DAP_COEF_WR_B1_MSB */
96 0x0000, /* 0x012E, DAP_COEF_WR_B1_LSB */
97 0x0000, /* 0x0130, DAP_COEF_WR_B2_MSB */
98 0x0000, /* 0x0132, DAP_COEF_WR_B2_LSB */
99 0x0000, /* 0x0134, DAP_COEF_WR_A1_MSB */
100 0x0000, /* 0x0136, DAP_COEF_WR_A1_LSB */
101 0x0000, /* 0x0138, DAP_COEF_WR_A2_MSB */
102 0x0000, /* 0x013A, DAP_COEF_WR_A2_LSB */
105 /* regulator supplies for sgtl5000, VDDD is an optional external supply */
106 enum sgtl5000_regulator_supplies
{
113 /* vddd is optional supply */
114 static const char *supply_names
[SGTL5000_SUPPLY_NUM
] = {
120 #define LDO_CONSUMER_NAME "VDDD_LDO"
121 #define LDO_VOLTAGE 1200000
123 static struct regulator_consumer_supply ldo_consumer
[] = {
124 REGULATOR_SUPPLY(LDO_CONSUMER_NAME
, NULL
),
127 static struct regulator_init_data ldo_init_data
= {
131 .valid_modes_mask
= REGULATOR_MODE_NORMAL
,
132 .valid_ops_mask
= REGULATOR_CHANGE_STATUS
,
134 .num_consumer_supplies
= 1,
135 .consumer_supplies
= &ldo_consumer
[0],
139 * sgtl5000 internal ldo regulator,
140 * enabled when VDDD not provided
142 struct ldo_regulator
{
143 struct regulator_desc desc
;
144 struct regulator_dev
*dev
;
150 /* sgtl5000 private structure in codec */
151 struct sgtl5000_priv
{
152 int sysclk
; /* sysclk rate */
153 int master
; /* i2s master or not */
154 int fmt
; /* i2s data format */
155 struct regulator_bulk_data supplies
[SGTL5000_SUPPLY_NUM
];
156 struct ldo_regulator
*ldo
;
160 * mic_bias power on/off share the same register bits with
161 * output impedance of mic bias, when power on mic bias, we
162 * need reclaim it to impedance value.
168 static int mic_bias_event(struct snd_soc_dapm_widget
*w
,
169 struct snd_kcontrol
*kcontrol
, int event
)
172 case SND_SOC_DAPM_POST_PMU
:
173 /* change mic bias resistor to 4Kohm */
174 snd_soc_update_bits(w
->codec
, SGTL5000_CHIP_MIC_CTRL
,
175 SGTL5000_BIAS_R_4k
, SGTL5000_BIAS_R_4k
);
178 case SND_SOC_DAPM_PRE_PMD
:
180 * SGTL5000_BIAS_R_8k as mask to clean the two bits
181 * of mic bias and output impedance
183 snd_soc_update_bits(w
->codec
, SGTL5000_CHIP_MIC_CTRL
,
184 SGTL5000_BIAS_R_8k
, 0);
191 * using codec assist to small pop, hp_powerup or lineout_powerup
192 * should stay setting until vag_powerup is fully ramped down,
193 * vag fully ramped down require 400ms.
195 static int small_pop_event(struct snd_soc_dapm_widget
*w
,
196 struct snd_kcontrol
*kcontrol
, int event
)
199 case SND_SOC_DAPM_PRE_PMU
:
200 snd_soc_update_bits(w
->codec
, SGTL5000_CHIP_ANA_POWER
,
201 SGTL5000_VAG_POWERUP
, SGTL5000_VAG_POWERUP
);
204 case SND_SOC_DAPM_PRE_PMD
:
205 snd_soc_update_bits(w
->codec
, SGTL5000_CHIP_ANA_POWER
,
206 SGTL5000_VAG_POWERUP
, 0);
216 /* input sources for ADC */
217 static const char *adc_mux_text
[] = {
221 static const struct soc_enum adc_enum
=
222 SOC_ENUM_SINGLE(SGTL5000_CHIP_ANA_CTRL
, 2, 2, adc_mux_text
);
224 static const struct snd_kcontrol_new adc_mux
=
225 SOC_DAPM_ENUM("Capture Mux", adc_enum
);
227 /* input sources for DAC */
228 static const char *dac_mux_text
[] = {
232 static const struct soc_enum dac_enum
=
233 SOC_ENUM_SINGLE(SGTL5000_CHIP_ANA_CTRL
, 6, 2, dac_mux_text
);
235 static const struct snd_kcontrol_new dac_mux
=
236 SOC_DAPM_ENUM("Headphone Mux", dac_enum
);
238 static const struct snd_soc_dapm_widget sgtl5000_dapm_widgets
[] = {
239 SND_SOC_DAPM_INPUT("LINE_IN"),
240 SND_SOC_DAPM_INPUT("MIC_IN"),
242 SND_SOC_DAPM_OUTPUT("HP_OUT"),
243 SND_SOC_DAPM_OUTPUT("LINE_OUT"),
245 SND_SOC_DAPM_MICBIAS_E("Mic Bias", SGTL5000_CHIP_MIC_CTRL
, 8, 0,
247 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
249 SND_SOC_DAPM_PGA_E("HP", SGTL5000_CHIP_ANA_POWER
, 4, 0, NULL
, 0,
251 SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_PRE_PMD
),
252 SND_SOC_DAPM_PGA_E("LO", SGTL5000_CHIP_ANA_POWER
, 0, 0, NULL
, 0,
254 SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_PRE_PMD
),
256 SND_SOC_DAPM_MUX("Capture Mux", SND_SOC_NOPM
, 0, 0, &adc_mux
),
257 SND_SOC_DAPM_MUX("Headphone Mux", SND_SOC_NOPM
, 0, 0, &dac_mux
),
259 /* aif for i2s input */
260 SND_SOC_DAPM_AIF_IN("AIFIN", "Playback",
261 0, SGTL5000_CHIP_DIG_POWER
,
264 /* aif for i2s output */
265 SND_SOC_DAPM_AIF_OUT("AIFOUT", "Capture",
266 0, SGTL5000_CHIP_DIG_POWER
,
269 SND_SOC_DAPM_ADC("ADC", "Capture", SGTL5000_CHIP_ANA_POWER
, 1, 0),
271 SND_SOC_DAPM_DAC("DAC", "Playback", SGTL5000_CHIP_ANA_POWER
, 3, 0),
274 /* routes for sgtl5000 */
275 static const struct snd_soc_dapm_route audio_map
[] = {
276 {"Capture Mux", "LINE_IN", "LINE_IN"}, /* line_in --> adc_mux */
277 {"Capture Mux", "MIC_IN", "MIC_IN"}, /* mic_in --> adc_mux */
279 {"ADC", NULL
, "Capture Mux"}, /* adc_mux --> adc */
280 {"AIFOUT", NULL
, "ADC"}, /* adc --> i2s_out */
282 {"DAC", NULL
, "AIFIN"}, /* i2s-->dac,skip audio mux */
283 {"Headphone Mux", "DAC", "DAC"}, /* dac --> hp_mux */
284 {"LO", NULL
, "DAC"}, /* dac --> line_out */
286 {"Headphone Mux", "LINE_IN", "LINE_IN"},/* line_in --> hp_mux */
287 {"HP", NULL
, "Headphone Mux"}, /* hp_mux --> hp */
289 {"LINE_OUT", NULL
, "LO"},
290 {"HP_OUT", NULL
, "HP"},
293 /* custom function to fetch info of PCM playback volume */
294 static int dac_info_volsw(struct snd_kcontrol
*kcontrol
,
295 struct snd_ctl_elem_info
*uinfo
)
297 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
299 uinfo
->value
.integer
.min
= 0;
300 uinfo
->value
.integer
.max
= 0xfc - 0x3c;
305 * custom function to get of PCM playback volume
307 * dac volume register
308 * 15-------------8-7--------------0
309 * | R channel vol | L channel vol |
310 * -------------------------------
312 * PCM volume with 0.5017 dB steps from 0 to -90 dB
314 * register values map to dB
315 * 0x3B and less = Reserved
319 * 0xFC and greater = Muted
321 * register value map to userspace value
323 * register value 0x3c(0dB) 0xf0(-90dB)0xfc
324 * ------------------------------
325 * userspace value 0xc0 0
327 static int dac_get_volsw(struct snd_kcontrol
*kcontrol
,
328 struct snd_ctl_elem_value
*ucontrol
)
330 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
335 reg
= snd_soc_read(codec
, SGTL5000_CHIP_DAC_VOL
);
337 /* get left channel volume */
338 l
= (reg
& SGTL5000_DAC_VOL_LEFT_MASK
) >> SGTL5000_DAC_VOL_LEFT_SHIFT
;
340 /* get right channel volume */
341 r
= (reg
& SGTL5000_DAC_VOL_RIGHT_MASK
) >> SGTL5000_DAC_VOL_RIGHT_SHIFT
;
343 /* make sure value fall in (0x3c,0xfc) */
344 l
= clamp(l
, 0x3c, 0xfc);
345 r
= clamp(r
, 0x3c, 0xfc);
347 /* invert it and map to userspace value */
351 ucontrol
->value
.integer
.value
[0] = l
;
352 ucontrol
->value
.integer
.value
[1] = r
;
358 * custom function to put of PCM playback volume
360 * dac volume register
361 * 15-------------8-7--------------0
362 * | R channel vol | L channel vol |
363 * -------------------------------
365 * PCM volume with 0.5017 dB steps from 0 to -90 dB
367 * register values map to dB
368 * 0x3B and less = Reserved
372 * 0xFC and greater = Muted
374 * userspace value map to register value
376 * userspace value 0xc0 0
377 * ------------------------------
378 * register value 0x3c(0dB) 0xf0(-90dB)0xfc
380 static int dac_put_volsw(struct snd_kcontrol
*kcontrol
,
381 struct snd_ctl_elem_value
*ucontrol
)
383 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
388 l
= ucontrol
->value
.integer
.value
[0];
389 r
= ucontrol
->value
.integer
.value
[1];
391 /* make sure userspace volume fall in (0, 0xfc-0x3c) */
392 l
= clamp(l
, 0, 0xfc - 0x3c);
393 r
= clamp(r
, 0, 0xfc - 0x3c);
395 /* invert it, get the value can be set to register */
399 /* shift to get the register value */
400 reg
= l
<< SGTL5000_DAC_VOL_LEFT_SHIFT
|
401 r
<< SGTL5000_DAC_VOL_RIGHT_SHIFT
;
403 snd_soc_write(codec
, SGTL5000_CHIP_DAC_VOL
, reg
);
408 static const DECLARE_TLV_DB_SCALE(capture_6db_attenuate
, -600, 600, 0);
410 /* tlv for mic gain, 0db 20db 30db 40db */
411 static const unsigned int mic_gain_tlv
[] = {
412 TLV_DB_RANGE_HEAD(4),
413 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
414 1, 3, TLV_DB_SCALE_ITEM(2000, 1000, 0),
417 /* tlv for hp volume, -51.5db to 12.0db, step .5db */
418 static const DECLARE_TLV_DB_SCALE(headphone_volume
, -5150, 50, 0);
420 static const struct snd_kcontrol_new sgtl5000_snd_controls
[] = {
421 /* SOC_DOUBLE_S8_TLV with invert */
423 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
424 .name
= "PCM Playback Volume",
425 .access
= SNDRV_CTL_ELEM_ACCESS_TLV_READ
|
426 SNDRV_CTL_ELEM_ACCESS_READWRITE
,
427 .info
= dac_info_volsw
,
428 .get
= dac_get_volsw
,
429 .put
= dac_put_volsw
,
432 SOC_DOUBLE("Capture Volume", SGTL5000_CHIP_ANA_ADC_CTRL
, 0, 4, 0xf, 0),
433 SOC_SINGLE_TLV("Capture Attenuate Switch (-6dB)",
434 SGTL5000_CHIP_ANA_ADC_CTRL
,
435 8, 2, 0, capture_6db_attenuate
),
436 SOC_SINGLE("Capture ZC Switch", SGTL5000_CHIP_ANA_CTRL
, 1, 1, 0),
438 SOC_DOUBLE_TLV("Headphone Playback Volume",
439 SGTL5000_CHIP_ANA_HP_CTRL
,
443 SOC_SINGLE("Headphone Playback ZC Switch", SGTL5000_CHIP_ANA_CTRL
,
446 SOC_SINGLE_TLV("Mic Volume", SGTL5000_CHIP_MIC_CTRL
,
447 0, 4, 0, mic_gain_tlv
),
450 /* mute the codec used by alsa core */
451 static int sgtl5000_digital_mute(struct snd_soc_dai
*codec_dai
, int mute
)
453 struct snd_soc_codec
*codec
= codec_dai
->codec
;
454 u16 adcdac_ctrl
= SGTL5000_DAC_MUTE_LEFT
| SGTL5000_DAC_MUTE_RIGHT
;
456 snd_soc_update_bits(codec
, SGTL5000_CHIP_ADCDAC_CTRL
,
457 adcdac_ctrl
, mute
? adcdac_ctrl
: 0);
462 /* set codec format */
463 static int sgtl5000_set_dai_fmt(struct snd_soc_dai
*codec_dai
, unsigned int fmt
)
465 struct snd_soc_codec
*codec
= codec_dai
->codec
;
466 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
469 sgtl5000
->master
= 0;
471 * i2s clock and frame master setting.
473 * - clock and frame slave,
474 * - clock and frame master
476 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
477 case SND_SOC_DAIFMT_CBS_CFS
:
479 case SND_SOC_DAIFMT_CBM_CFM
:
480 i2sctl
|= SGTL5000_I2S_MASTER
;
481 sgtl5000
->master
= 1;
487 /* setting i2s data format */
488 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
489 case SND_SOC_DAIFMT_DSP_A
:
490 i2sctl
|= SGTL5000_I2S_MODE_PCM
;
492 case SND_SOC_DAIFMT_DSP_B
:
493 i2sctl
|= SGTL5000_I2S_MODE_PCM
;
494 i2sctl
|= SGTL5000_I2S_LRALIGN
;
496 case SND_SOC_DAIFMT_I2S
:
497 i2sctl
|= SGTL5000_I2S_MODE_I2S_LJ
;
499 case SND_SOC_DAIFMT_RIGHT_J
:
500 i2sctl
|= SGTL5000_I2S_MODE_RJ
;
501 i2sctl
|= SGTL5000_I2S_LRPOL
;
503 case SND_SOC_DAIFMT_LEFT_J
:
504 i2sctl
|= SGTL5000_I2S_MODE_I2S_LJ
;
505 i2sctl
|= SGTL5000_I2S_LRALIGN
;
511 sgtl5000
->fmt
= fmt
& SND_SOC_DAIFMT_FORMAT_MASK
;
513 /* Clock inversion */
514 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
515 case SND_SOC_DAIFMT_NB_NF
:
517 case SND_SOC_DAIFMT_IB_NF
:
518 i2sctl
|= SGTL5000_I2S_SCLK_INV
;
524 snd_soc_write(codec
, SGTL5000_CHIP_I2S_CTRL
, i2sctl
);
529 /* set codec sysclk */
530 static int sgtl5000_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
531 int clk_id
, unsigned int freq
, int dir
)
533 struct snd_soc_codec
*codec
= codec_dai
->codec
;
534 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
537 case SGTL5000_SYSCLK
:
538 sgtl5000
->sysclk
= freq
;
548 * set clock according to i2s frame clock,
549 * sgtl5000 provide 2 clock sources.
550 * 1. sys_mclk. sample freq can only configure to
551 * 1/256, 1/384, 1/512 of sys_mclk.
552 * 2. pll. can derive any audio clocks.
554 * clock setting rules:
555 * 1. in slave mode, only sys_mclk can use.
556 * 2. as constraint by sys_mclk, sample freq should
557 * set to 32k, 44.1k and above.
558 * 3. using sys_mclk prefer to pll to save power.
560 static int sgtl5000_set_clock(struct snd_soc_codec
*codec
, int frame_rate
)
562 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
564 int sys_fs
; /* sample freq */
567 * sample freq should be divided by frame clock,
568 * if frame clock lower than 44.1khz, sample feq should set to
571 switch (frame_rate
) {
585 /* set divided factor of frame clock */
586 switch (sys_fs
/ frame_rate
) {
588 clk_ctl
|= SGTL5000_RATE_MODE_DIV_4
<< SGTL5000_RATE_MODE_SHIFT
;
591 clk_ctl
|= SGTL5000_RATE_MODE_DIV_2
<< SGTL5000_RATE_MODE_SHIFT
;
594 clk_ctl
|= SGTL5000_RATE_MODE_DIV_1
<< SGTL5000_RATE_MODE_SHIFT
;
600 /* set the sys_fs according to frame rate */
603 clk_ctl
|= SGTL5000_SYS_FS_32k
<< SGTL5000_SYS_FS_SHIFT
;
606 clk_ctl
|= SGTL5000_SYS_FS_44_1k
<< SGTL5000_SYS_FS_SHIFT
;
609 clk_ctl
|= SGTL5000_SYS_FS_48k
<< SGTL5000_SYS_FS_SHIFT
;
612 clk_ctl
|= SGTL5000_SYS_FS_96k
<< SGTL5000_SYS_FS_SHIFT
;
615 dev_err(codec
->dev
, "frame rate %d not supported\n",
621 * calculate the divider of mclk/sample_freq,
622 * factor of freq =96k can only be 256, since mclk in range (12m,27m)
624 switch (sgtl5000
->sysclk
/ sys_fs
) {
626 clk_ctl
|= SGTL5000_MCLK_FREQ_256FS
<<
627 SGTL5000_MCLK_FREQ_SHIFT
;
630 clk_ctl
|= SGTL5000_MCLK_FREQ_384FS
<<
631 SGTL5000_MCLK_FREQ_SHIFT
;
634 clk_ctl
|= SGTL5000_MCLK_FREQ_512FS
<<
635 SGTL5000_MCLK_FREQ_SHIFT
;
638 /* if mclk not satisify the divider, use pll */
639 if (sgtl5000
->master
) {
640 clk_ctl
|= SGTL5000_MCLK_FREQ_PLL
<<
641 SGTL5000_MCLK_FREQ_SHIFT
;
644 "PLL not supported in slave mode\n");
649 /* if using pll, please check manual 6.4.2 for detail */
650 if ((clk_ctl
& SGTL5000_MCLK_FREQ_MASK
) == SGTL5000_MCLK_FREQ_PLL
) {
654 unsigned int in
, int_div
, frac_div
;
656 if (sgtl5000
->sysclk
> 17000000) {
658 in
= sgtl5000
->sysclk
/ 2;
661 in
= sgtl5000
->sysclk
;
672 pll_ctl
= int_div
<< SGTL5000_PLL_INT_DIV_SHIFT
|
673 frac_div
<< SGTL5000_PLL_FRAC_DIV_SHIFT
;
675 snd_soc_write(codec
, SGTL5000_CHIP_PLL_CTRL
, pll_ctl
);
677 snd_soc_update_bits(codec
,
678 SGTL5000_CHIP_CLK_TOP_CTRL
,
679 SGTL5000_INPUT_FREQ_DIV2
,
680 SGTL5000_INPUT_FREQ_DIV2
);
682 snd_soc_update_bits(codec
,
683 SGTL5000_CHIP_CLK_TOP_CTRL
,
684 SGTL5000_INPUT_FREQ_DIV2
,
688 snd_soc_update_bits(codec
, SGTL5000_CHIP_ANA_POWER
,
689 SGTL5000_PLL_POWERUP
| SGTL5000_VCOAMP_POWERUP
,
690 SGTL5000_PLL_POWERUP
| SGTL5000_VCOAMP_POWERUP
);
693 snd_soc_update_bits(codec
, SGTL5000_CHIP_ANA_POWER
,
694 SGTL5000_PLL_POWERUP
| SGTL5000_VCOAMP_POWERUP
,
698 /* if using pll, clk_ctrl must be set after pll power up */
699 snd_soc_write(codec
, SGTL5000_CHIP_CLK_CTRL
, clk_ctl
);
705 * Set PCM DAI bit size and sample rate.
706 * input: params_rate, params_fmt
708 static int sgtl5000_pcm_hw_params(struct snd_pcm_substream
*substream
,
709 struct snd_pcm_hw_params
*params
,
710 struct snd_soc_dai
*dai
)
712 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
713 struct snd_soc_codec
*codec
= rtd
->codec
;
714 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
715 int channels
= params_channels(params
);
720 /* sysclk should already set */
721 if (!sgtl5000
->sysclk
) {
722 dev_err(codec
->dev
, "%s: set sysclk first!\n", __func__
);
726 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
727 stereo
= SGTL5000_DAC_STEREO
;
729 stereo
= SGTL5000_ADC_STEREO
;
731 /* set mono to save power */
732 snd_soc_update_bits(codec
, SGTL5000_CHIP_ANA_POWER
, stereo
,
733 channels
== 1 ? 0 : stereo
);
735 /* set codec clock base on lrclk */
736 ret
= sgtl5000_set_clock(codec
, params_rate(params
));
740 /* set i2s data format */
741 switch (params_format(params
)) {
742 case SNDRV_PCM_FORMAT_S16_LE
:
743 if (sgtl5000
->fmt
== SND_SOC_DAIFMT_RIGHT_J
)
745 i2s_ctl
|= SGTL5000_I2S_DLEN_16
<< SGTL5000_I2S_DLEN_SHIFT
;
746 i2s_ctl
|= SGTL5000_I2S_SCLKFREQ_32FS
<<
747 SGTL5000_I2S_SCLKFREQ_SHIFT
;
749 case SNDRV_PCM_FORMAT_S20_3LE
:
750 i2s_ctl
|= SGTL5000_I2S_DLEN_20
<< SGTL5000_I2S_DLEN_SHIFT
;
751 i2s_ctl
|= SGTL5000_I2S_SCLKFREQ_64FS
<<
752 SGTL5000_I2S_SCLKFREQ_SHIFT
;
754 case SNDRV_PCM_FORMAT_S24_LE
:
755 i2s_ctl
|= SGTL5000_I2S_DLEN_24
<< SGTL5000_I2S_DLEN_SHIFT
;
756 i2s_ctl
|= SGTL5000_I2S_SCLKFREQ_64FS
<<
757 SGTL5000_I2S_SCLKFREQ_SHIFT
;
759 case SNDRV_PCM_FORMAT_S32_LE
:
760 if (sgtl5000
->fmt
== SND_SOC_DAIFMT_RIGHT_J
)
762 i2s_ctl
|= SGTL5000_I2S_DLEN_32
<< SGTL5000_I2S_DLEN_SHIFT
;
763 i2s_ctl
|= SGTL5000_I2S_SCLKFREQ_64FS
<<
764 SGTL5000_I2S_SCLKFREQ_SHIFT
;
770 snd_soc_update_bits(codec
, SGTL5000_CHIP_I2S_CTRL
, i2s_ctl
, i2s_ctl
);
775 #ifdef CONFIG_REGULATOR
776 static int ldo_regulator_is_enabled(struct regulator_dev
*dev
)
778 struct ldo_regulator
*ldo
= rdev_get_drvdata(dev
);
783 static int ldo_regulator_enable(struct regulator_dev
*dev
)
785 struct ldo_regulator
*ldo
= rdev_get_drvdata(dev
);
786 struct snd_soc_codec
*codec
= (struct snd_soc_codec
*)ldo
->codec_data
;
789 if (ldo_regulator_is_enabled(dev
))
792 /* set regulator value firstly */
793 reg
= (1600 - ldo
->voltage
/ 1000) / 50;
794 reg
= clamp(reg
, 0x0, 0xf);
796 /* amend the voltage value, unit: uV */
797 ldo
->voltage
= (1600 - reg
* 50) * 1000;
799 /* set voltage to register */
800 snd_soc_update_bits(codec
, SGTL5000_CHIP_LINREG_CTRL
,
801 (0x1 << 4) - 1, reg
);
803 snd_soc_update_bits(codec
, SGTL5000_CHIP_ANA_POWER
,
804 SGTL5000_LINEREG_D_POWERUP
,
805 SGTL5000_LINEREG_D_POWERUP
);
807 /* when internal ldo enabled, simple digital power can be disabled */
808 snd_soc_update_bits(codec
, SGTL5000_CHIP_ANA_POWER
,
809 SGTL5000_LINREG_SIMPLE_POWERUP
,
816 static int ldo_regulator_disable(struct regulator_dev
*dev
)
818 struct ldo_regulator
*ldo
= rdev_get_drvdata(dev
);
819 struct snd_soc_codec
*codec
= (struct snd_soc_codec
*)ldo
->codec_data
;
821 snd_soc_update_bits(codec
, SGTL5000_CHIP_ANA_POWER
,
822 SGTL5000_LINEREG_D_POWERUP
,
825 /* clear voltage info */
826 snd_soc_update_bits(codec
, SGTL5000_CHIP_LINREG_CTRL
,
834 static int ldo_regulator_get_voltage(struct regulator_dev
*dev
)
836 struct ldo_regulator
*ldo
= rdev_get_drvdata(dev
);
841 static struct regulator_ops ldo_regulator_ops
= {
842 .is_enabled
= ldo_regulator_is_enabled
,
843 .enable
= ldo_regulator_enable
,
844 .disable
= ldo_regulator_disable
,
845 .get_voltage
= ldo_regulator_get_voltage
,
848 static int ldo_regulator_register(struct snd_soc_codec
*codec
,
849 struct regulator_init_data
*init_data
,
852 struct ldo_regulator
*ldo
;
854 ldo
= kzalloc(sizeof(struct ldo_regulator
), GFP_KERNEL
);
857 dev_err(codec
->dev
, "failed to allocate ldo_regulator\n");
861 ldo
->desc
.name
= kstrdup(dev_name(codec
->dev
), GFP_KERNEL
);
862 if (!ldo
->desc
.name
) {
864 dev_err(codec
->dev
, "failed to allocate decs name memory\n");
868 ldo
->desc
.type
= REGULATOR_VOLTAGE
;
869 ldo
->desc
.owner
= THIS_MODULE
;
870 ldo
->desc
.ops
= &ldo_regulator_ops
;
871 ldo
->desc
.n_voltages
= 1;
873 ldo
->codec_data
= codec
;
874 ldo
->voltage
= voltage
;
876 ldo
->dev
= regulator_register(&ldo
->desc
, codec
->dev
,
878 if (IS_ERR(ldo
->dev
)) {
879 int ret
= PTR_ERR(ldo
->dev
);
881 dev_err(codec
->dev
, "failed to register regulator\n");
882 kfree(ldo
->desc
.name
);
891 static int ldo_regulator_remove(struct snd_soc_codec
*codec
)
893 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
894 struct ldo_regulator
*ldo
= sgtl5000
->ldo
;
899 regulator_unregister(ldo
->dev
);
900 kfree(ldo
->desc
.name
);
906 static int ldo_regulator_register(struct snd_soc_codec
*codec
,
907 struct regulator_init_data
*init_data
,
910 dev_err(codec
->dev
, "this setup needs regulator support in the kernel\n");
914 static int ldo_regulator_remove(struct snd_soc_codec
*codec
)
922 * common state changes:
924 * off --> standby --> prepare --> on
925 * standby --> prepare --> on
928 * on --> prepare --> standby
930 static int sgtl5000_set_bias_level(struct snd_soc_codec
*codec
,
931 enum snd_soc_bias_level level
)
934 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
937 case SND_SOC_BIAS_ON
:
938 case SND_SOC_BIAS_PREPARE
:
940 case SND_SOC_BIAS_STANDBY
:
941 if (codec
->dapm
.bias_level
== SND_SOC_BIAS_OFF
) {
942 ret
= regulator_bulk_enable(
943 ARRAY_SIZE(sgtl5000
->supplies
),
951 case SND_SOC_BIAS_OFF
:
952 regulator_bulk_disable(ARRAY_SIZE(sgtl5000
->supplies
),
957 codec
->dapm
.bias_level
= level
;
961 #define SGTL5000_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
962 SNDRV_PCM_FMTBIT_S20_3LE |\
963 SNDRV_PCM_FMTBIT_S24_LE |\
964 SNDRV_PCM_FMTBIT_S32_LE)
966 static struct snd_soc_dai_ops sgtl5000_ops
= {
967 .hw_params
= sgtl5000_pcm_hw_params
,
968 .digital_mute
= sgtl5000_digital_mute
,
969 .set_fmt
= sgtl5000_set_dai_fmt
,
970 .set_sysclk
= sgtl5000_set_dai_sysclk
,
973 static struct snd_soc_dai_driver sgtl5000_dai
= {
976 .stream_name
= "Playback",
980 * only support 8~48K + 96K,
981 * TODO modify hw_param to support more
983 .rates
= SNDRV_PCM_RATE_8000_48000
| SNDRV_PCM_RATE_96000
,
984 .formats
= SGTL5000_FORMATS
,
987 .stream_name
= "Capture",
990 .rates
= SNDRV_PCM_RATE_8000_48000
| SNDRV_PCM_RATE_96000
,
991 .formats
= SGTL5000_FORMATS
,
993 .ops
= &sgtl5000_ops
,
994 .symmetric_rates
= 1,
997 static int sgtl5000_volatile_register(struct snd_soc_codec
*codec
,
1001 case SGTL5000_CHIP_ID
:
1002 case SGTL5000_CHIP_ADCDAC_CTRL
:
1003 case SGTL5000_CHIP_ANA_STATUS
:
1010 #ifdef CONFIG_SUSPEND
1011 static int sgtl5000_suspend(struct snd_soc_codec
*codec
, pm_message_t state
)
1013 sgtl5000_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
1019 * restore all sgtl5000 registers,
1020 * since a big hole between dap and regular registers,
1021 * we will restore them respectively.
1023 static int sgtl5000_restore_regs(struct snd_soc_codec
*codec
)
1025 u16
*cache
= codec
->reg_cache
;
1027 int regular_regs
= SGTL5000_CHIP_SHORT_CTRL
>> 1;
1029 /* restore regular registers */
1030 for (i
= 0; i
< regular_regs
; i
++) {
1033 /* this regs depends on the others */
1034 if (reg
== SGTL5000_CHIP_ANA_POWER
||
1035 reg
== SGTL5000_CHIP_CLK_CTRL
||
1036 reg
== SGTL5000_CHIP_LINREG_CTRL
||
1037 reg
== SGTL5000_CHIP_LINE_OUT_CTRL
||
1038 reg
== SGTL5000_CHIP_CLK_CTRL
)
1041 snd_soc_write(codec
, reg
, cache
[i
]);
1044 /* restore dap registers */
1045 for (i
= SGTL5000_DAP_REG_OFFSET
>> 1;
1046 i
< SGTL5000_MAX_REG_OFFSET
>> 1; i
++) {
1049 snd_soc_write(codec
, reg
, cache
[i
]);
1053 * restore power and other regs according
1054 * to set_power() and set_clock()
1056 snd_soc_write(codec
, SGTL5000_CHIP_LINREG_CTRL
,
1057 cache
[SGTL5000_CHIP_LINREG_CTRL
>> 1]);
1059 snd_soc_write(codec
, SGTL5000_CHIP_ANA_POWER
,
1060 cache
[SGTL5000_CHIP_ANA_POWER
>> 1]);
1062 snd_soc_write(codec
, SGTL5000_CHIP_CLK_CTRL
,
1063 cache
[SGTL5000_CHIP_CLK_CTRL
>> 1]);
1065 snd_soc_write(codec
, SGTL5000_CHIP_REF_CTRL
,
1066 cache
[SGTL5000_CHIP_REF_CTRL
>> 1]);
1068 snd_soc_write(codec
, SGTL5000_CHIP_LINE_OUT_CTRL
,
1069 cache
[SGTL5000_CHIP_LINE_OUT_CTRL
>> 1]);
1073 static int sgtl5000_resume(struct snd_soc_codec
*codec
)
1075 /* Bring the codec back up to standby to enable regulators */
1076 sgtl5000_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
1078 /* Restore registers by cached in memory */
1079 sgtl5000_restore_regs(codec
);
1083 #define sgtl5000_suspend NULL
1084 #define sgtl5000_resume NULL
1085 #endif /* CONFIG_SUSPEND */
1088 * sgtl5000 has 3 internal power supplies:
1089 * 1. VAG, normally set to vdda/2
1090 * 2. chargepump, set to different value
1091 * according to voltage of vdda and vddio
1092 * 3. line out VAG, normally set to vddio/2
1094 * and should be set according to:
1095 * 1. vddd provided by external or not
1096 * 2. vdda and vddio voltage value. > 3.1v or not
1097 * 3. chip revision >=0x11 or not. If >=0x11, not use external vddd.
1099 static int sgtl5000_set_power_regs(struct snd_soc_codec
*codec
)
1107 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
1109 vdda
= regulator_get_voltage(sgtl5000
->supplies
[VDDA
].consumer
);
1110 vddio
= regulator_get_voltage(sgtl5000
->supplies
[VDDIO
].consumer
);
1111 vddd
= regulator_get_voltage(sgtl5000
->supplies
[VDDD
].consumer
);
1114 vddio
= vddio
/ 1000;
1117 if (vdda
<= 0 || vddio
<= 0 || vddd
< 0) {
1118 dev_err(codec
->dev
, "regulator voltage not set correctly\n");
1123 /* according to datasheet, maximum voltage of supplies */
1124 if (vdda
> 3600 || vddio
> 3600 || vddd
> 1980) {
1126 "exceed max voltage vdda %dmv vddio %dma vddd %dma\n",
1133 ana_pwr
= snd_soc_read(codec
, SGTL5000_CHIP_ANA_POWER
);
1134 ana_pwr
|= SGTL5000_DAC_STEREO
|
1135 SGTL5000_ADC_STEREO
|
1136 SGTL5000_REFTOP_POWERUP
;
1137 lreg_ctrl
= snd_soc_read(codec
, SGTL5000_CHIP_LINREG_CTRL
);
1139 if (vddio
< 3100 && vdda
< 3100) {
1140 /* enable internal oscillator used for charge pump */
1141 snd_soc_update_bits(codec
, SGTL5000_CHIP_CLK_TOP_CTRL
,
1142 SGTL5000_INT_OSC_EN
,
1143 SGTL5000_INT_OSC_EN
);
1144 /* Enable VDDC charge pump */
1145 ana_pwr
|= SGTL5000_VDDC_CHRGPMP_POWERUP
;
1146 } else if (vddio
>= 3100 && vdda
>= 3100) {
1148 * if vddio and vddd > 3.1v,
1149 * charge pump should be clean before set ana_pwr
1151 snd_soc_update_bits(codec
, SGTL5000_CHIP_ANA_POWER
,
1152 SGTL5000_VDDC_CHRGPMP_POWERUP
, 0);
1154 /* VDDC use VDDIO rail */
1155 lreg_ctrl
|= SGTL5000_VDDC_ASSN_OVRD
;
1156 lreg_ctrl
|= SGTL5000_VDDC_MAN_ASSN_VDDIO
<<
1157 SGTL5000_VDDC_MAN_ASSN_SHIFT
;
1160 snd_soc_write(codec
, SGTL5000_CHIP_LINREG_CTRL
, lreg_ctrl
);
1162 snd_soc_write(codec
, SGTL5000_CHIP_ANA_POWER
, ana_pwr
);
1164 /* set voltage to register */
1165 snd_soc_update_bits(codec
, SGTL5000_CHIP_LINREG_CTRL
,
1166 (0x1 << 4) - 1, 0x8);
1169 * if vddd linear reg has been enabled,
1170 * simple digital supply should be clear to get
1171 * proper VDDD voltage.
1173 if (ana_pwr
& SGTL5000_LINEREG_D_POWERUP
)
1174 snd_soc_update_bits(codec
, SGTL5000_CHIP_ANA_POWER
,
1175 SGTL5000_LINREG_SIMPLE_POWERUP
,
1178 snd_soc_update_bits(codec
, SGTL5000_CHIP_ANA_POWER
,
1179 SGTL5000_LINREG_SIMPLE_POWERUP
|
1180 SGTL5000_STARTUP_POWERUP
,
1184 * set ADC/DAC VAG to vdda / 2,
1185 * should stay in range (0.8v, 1.575v)
1188 if (vag
<= SGTL5000_ANA_GND_BASE
)
1190 else if (vag
>= SGTL5000_ANA_GND_BASE
+ SGTL5000_ANA_GND_STP
*
1191 (SGTL5000_ANA_GND_MASK
>> SGTL5000_ANA_GND_SHIFT
))
1192 vag
= SGTL5000_ANA_GND_MASK
>> SGTL5000_ANA_GND_SHIFT
;
1194 vag
= (vag
- SGTL5000_ANA_GND_BASE
) / SGTL5000_ANA_GND_STP
;
1196 snd_soc_update_bits(codec
, SGTL5000_CHIP_REF_CTRL
,
1197 vag
<< SGTL5000_ANA_GND_SHIFT
,
1198 vag
<< SGTL5000_ANA_GND_SHIFT
);
1200 /* set line out VAG to vddio / 2, in range (0.8v, 1.675v) */
1202 if (vag
<= SGTL5000_LINE_OUT_GND_BASE
)
1204 else if (vag
>= SGTL5000_LINE_OUT_GND_BASE
+
1205 SGTL5000_LINE_OUT_GND_STP
* SGTL5000_LINE_OUT_GND_MAX
)
1206 vag
= SGTL5000_LINE_OUT_GND_MAX
;
1208 vag
= (vag
- SGTL5000_LINE_OUT_GND_BASE
) /
1209 SGTL5000_LINE_OUT_GND_STP
;
1211 snd_soc_update_bits(codec
, SGTL5000_CHIP_LINE_OUT_CTRL
,
1212 vag
<< SGTL5000_LINE_OUT_GND_SHIFT
|
1213 SGTL5000_LINE_OUT_CURRENT_360u
<<
1214 SGTL5000_LINE_OUT_CURRENT_SHIFT
,
1215 vag
<< SGTL5000_LINE_OUT_GND_SHIFT
|
1216 SGTL5000_LINE_OUT_CURRENT_360u
<<
1217 SGTL5000_LINE_OUT_CURRENT_SHIFT
);
1222 static int sgtl5000_replace_vddd_with_ldo(struct snd_soc_codec
*codec
)
1224 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
1227 /* set internal ldo to 1.2v */
1228 ret
= ldo_regulator_register(codec
, &ldo_init_data
, LDO_VOLTAGE
);
1231 "Failed to register vddd internal supplies: %d\n", ret
);
1235 sgtl5000
->supplies
[VDDD
].supply
= LDO_CONSUMER_NAME
;
1237 ret
= regulator_bulk_get(codec
->dev
, ARRAY_SIZE(sgtl5000
->supplies
),
1238 sgtl5000
->supplies
);
1241 ldo_regulator_remove(codec
);
1242 dev_err(codec
->dev
, "Failed to request supplies: %d\n", ret
);
1246 dev_info(codec
->dev
, "Using internal LDO instead of VDDD\n");
1250 static int sgtl5000_enable_regulators(struct snd_soc_codec
*codec
)
1256 int external_vddd
= 0;
1257 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
1259 for (i
= 0; i
< ARRAY_SIZE(sgtl5000
->supplies
); i
++)
1260 sgtl5000
->supplies
[i
].supply
= supply_names
[i
];
1262 ret
= regulator_bulk_get(codec
->dev
, ARRAY_SIZE(sgtl5000
->supplies
),
1263 sgtl5000
->supplies
);
1267 ret
= sgtl5000_replace_vddd_with_ldo(codec
);
1272 ret
= regulator_bulk_enable(ARRAY_SIZE(sgtl5000
->supplies
),
1273 sgtl5000
->supplies
);
1275 goto err_regulator_free
;
1277 /* wait for all power rails bring up */
1280 /* read chip information */
1281 reg
= snd_soc_read(codec
, SGTL5000_CHIP_ID
);
1282 if (((reg
& SGTL5000_PARTID_MASK
) >> SGTL5000_PARTID_SHIFT
) !=
1283 SGTL5000_PARTID_PART_ID
) {
1285 "Device with ID register %x is not a sgtl5000\n", reg
);
1287 goto err_regulator_disable
;
1290 rev
= (reg
& SGTL5000_REVID_MASK
) >> SGTL5000_REVID_SHIFT
;
1291 dev_info(codec
->dev
, "sgtl5000 revision %d\n", rev
);
1294 * workaround for revision 0x11 and later,
1295 * roll back to use internal LDO
1297 if (external_vddd
&& rev
>= 0x11) {
1298 /* disable all regulator first */
1299 regulator_bulk_disable(ARRAY_SIZE(sgtl5000
->supplies
),
1300 sgtl5000
->supplies
);
1301 /* free VDDD regulator */
1302 regulator_bulk_free(ARRAY_SIZE(sgtl5000
->supplies
),
1303 sgtl5000
->supplies
);
1305 ret
= sgtl5000_replace_vddd_with_ldo(codec
);
1309 ret
= regulator_bulk_enable(ARRAY_SIZE(sgtl5000
->supplies
),
1310 sgtl5000
->supplies
);
1312 goto err_regulator_free
;
1314 /* wait for all power rails bring up */
1320 err_regulator_disable
:
1321 regulator_bulk_disable(ARRAY_SIZE(sgtl5000
->supplies
),
1322 sgtl5000
->supplies
);
1324 regulator_bulk_free(ARRAY_SIZE(sgtl5000
->supplies
),
1325 sgtl5000
->supplies
);
1327 ldo_regulator_remove(codec
);
1332 static int sgtl5000_probe(struct snd_soc_codec
*codec
)
1335 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
1337 /* setup i2c data ops */
1338 ret
= snd_soc_codec_set_cache_io(codec
, 16, 16, SND_SOC_I2C
);
1340 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
1344 ret
= sgtl5000_enable_regulators(codec
);
1348 /* power up sgtl5000 */
1349 ret
= sgtl5000_set_power_regs(codec
);
1353 /* enable small pop, introduce 400ms delay in turning off */
1354 snd_soc_update_bits(codec
, SGTL5000_CHIP_REF_CTRL
,
1356 SGTL5000_SMALL_POP
);
1358 /* disable short cut detector */
1359 snd_soc_write(codec
, SGTL5000_CHIP_SHORT_CTRL
, 0);
1362 * set i2s as default input of sound switch
1363 * TODO: add sound switch to control and dapm widge.
1365 snd_soc_write(codec
, SGTL5000_CHIP_SSS_CTRL
,
1366 SGTL5000_DAC_SEL_I2S_IN
<< SGTL5000_DAC_SEL_SHIFT
);
1367 snd_soc_write(codec
, SGTL5000_CHIP_DIG_POWER
,
1368 SGTL5000_ADC_EN
| SGTL5000_DAC_EN
);
1370 /* enable dac volume ramp by default */
1371 snd_soc_write(codec
, SGTL5000_CHIP_ADCDAC_CTRL
,
1372 SGTL5000_DAC_VOL_RAMP_EN
|
1373 SGTL5000_DAC_MUTE_RIGHT
|
1374 SGTL5000_DAC_MUTE_LEFT
);
1376 snd_soc_write(codec
, SGTL5000_CHIP_PAD_STRENGTH
, 0x015f);
1378 snd_soc_write(codec
, SGTL5000_CHIP_ANA_CTRL
,
1379 SGTL5000_HP_ZCD_EN
|
1380 SGTL5000_ADC_ZCD_EN
);
1382 snd_soc_write(codec
, SGTL5000_CHIP_MIC_CTRL
, 0);
1387 * Enable DAP in kcontrol and dapm.
1389 snd_soc_write(codec
, SGTL5000_DAP_CTRL
, 0);
1391 /* leading to standby state */
1392 ret
= sgtl5000_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
1396 snd_soc_add_controls(codec
, sgtl5000_snd_controls
,
1397 ARRAY_SIZE(sgtl5000_snd_controls
));
1399 snd_soc_dapm_new_controls(&codec
->dapm
, sgtl5000_dapm_widgets
,
1400 ARRAY_SIZE(sgtl5000_dapm_widgets
));
1402 snd_soc_dapm_add_routes(&codec
->dapm
, audio_map
,
1403 ARRAY_SIZE(audio_map
));
1405 snd_soc_dapm_new_widgets(&codec
->dapm
);
1410 regulator_bulk_disable(ARRAY_SIZE(sgtl5000
->supplies
),
1411 sgtl5000
->supplies
);
1412 regulator_bulk_free(ARRAY_SIZE(sgtl5000
->supplies
),
1413 sgtl5000
->supplies
);
1414 ldo_regulator_remove(codec
);
1419 static int sgtl5000_remove(struct snd_soc_codec
*codec
)
1421 struct sgtl5000_priv
*sgtl5000
= snd_soc_codec_get_drvdata(codec
);
1423 sgtl5000_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
1425 regulator_bulk_disable(ARRAY_SIZE(sgtl5000
->supplies
),
1426 sgtl5000
->supplies
);
1427 regulator_bulk_free(ARRAY_SIZE(sgtl5000
->supplies
),
1428 sgtl5000
->supplies
);
1429 ldo_regulator_remove(codec
);
1434 static struct snd_soc_codec_driver sgtl5000_driver
= {
1435 .probe
= sgtl5000_probe
,
1436 .remove
= sgtl5000_remove
,
1437 .suspend
= sgtl5000_suspend
,
1438 .resume
= sgtl5000_resume
,
1439 .set_bias_level
= sgtl5000_set_bias_level
,
1440 .reg_cache_size
= ARRAY_SIZE(sgtl5000_regs
),
1441 .reg_word_size
= sizeof(u16
),
1442 .reg_cache_step
= 2,
1443 .reg_cache_default
= sgtl5000_regs
,
1444 .volatile_register
= sgtl5000_volatile_register
,
1447 static __devinit
int sgtl5000_i2c_probe(struct i2c_client
*client
,
1448 const struct i2c_device_id
*id
)
1450 struct sgtl5000_priv
*sgtl5000
;
1453 sgtl5000
= kzalloc(sizeof(struct sgtl5000_priv
), GFP_KERNEL
);
1458 * copy DAP default values to default value array.
1459 * sgtl5000 register space has a big hole, merge it
1460 * at init phase makes life easy.
1461 * FIXME: should we drop 'const' of sgtl5000_regs?
1463 memcpy((void *)(&sgtl5000_regs
[0] + (SGTL5000_DAP_REG_OFFSET
>> 1)),
1465 SGTL5000_MAX_REG_OFFSET
- SGTL5000_DAP_REG_OFFSET
);
1467 i2c_set_clientdata(client
, sgtl5000
);
1469 ret
= snd_soc_register_codec(&client
->dev
,
1470 &sgtl5000_driver
, &sgtl5000_dai
, 1);
1472 dev_err(&client
->dev
, "Failed to register codec: %d\n", ret
);
1480 static __devexit
int sgtl5000_i2c_remove(struct i2c_client
*client
)
1482 struct sgtl5000_priv
*sgtl5000
= i2c_get_clientdata(client
);
1484 snd_soc_unregister_codec(&client
->dev
);
1490 static const struct i2c_device_id sgtl5000_id
[] = {
1495 MODULE_DEVICE_TABLE(i2c
, sgtl5000_id
);
1497 static struct i2c_driver sgtl5000_i2c_driver
= {
1500 .owner
= THIS_MODULE
,
1502 .probe
= sgtl5000_i2c_probe
,
1503 .remove
= __devexit_p(sgtl5000_i2c_remove
),
1504 .id_table
= sgtl5000_id
,
1507 static int __init
sgtl5000_modinit(void)
1509 return i2c_add_driver(&sgtl5000_i2c_driver
);
1511 module_init(sgtl5000_modinit
);
1513 static void __exit
sgtl5000_exit(void)
1515 i2c_del_driver(&sgtl5000_i2c_driver
);
1517 module_exit(sgtl5000_exit
);
1519 MODULE_DESCRIPTION("Freescale SGTL5000 ALSA SoC Codec Driver");
1520 MODULE_AUTHOR("Zeng Zhaoming <zhaoming.zeng@freescale.com>");
1521 MODULE_LICENSE("GPL");