Skip to contentSkip to conversation

ASoC: Add FreeScale SGTL5000 codec suppor

PATCH In review Zeng Zhaoming 3 participants 4 messages Jan 17, 2011 – Jan 18, 2011

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ASoC: Add FreeScale SGTL5000 codec suppor

v1 url:
http://mailman.alsa-project.org/pipermail/alsa-devel/2010-December/034615.html

Changes since v1:
- Add DAPM widges as Mark and Arnaud point out.
- cleanup regulator code
- new implement dai_ops functions
- sort configs in Kconfig and Makefile

Quoted 1 line

From 1704fd5e7cf2540e247aa8a4f69d81ae5e6cbd4e Mon Sep 17 00:00:00 2001

From: Zeng Zhaoming <[email protected]>
Date: Mon, 17 Jan 2011 10:34:42 +0800
Subject: [PATCH] ASoC: Add FreeScale SGTL5000 codec support

add FreeScale SGTL5000 codec support

---
 sound/soc/codecs/Kconfig    |    4 +
 sound/soc/codecs/Makefile   |    1 +
 sound/soc/codecs/sgtl5000.c | 1016 +++++++++++++++++++++++++++++++++++++++++++
 sound/soc/codecs/sgtl5000.h |  403 +++++++++++++++++
 4 files changed, 1424 insertions(+), 0 deletions(-)
 create mode 100644 sound/soc/codecs/sgtl5000.c
 create mode 100644 sound/soc/codecs/sgtl5000.h
sound/soc/codecs/Kconfig +4 -0
index 883a312..19bbb73 100644
@@ -32,6 +32,7 @@ config SND_SOC_ALL_CODECS
 	select SND_SOC_MAX98088 if I2C
 	select SND_SOC_MAX9877 if I2C
 	select SND_SOC_PCM3008
+	select SND_SOC_SGTL5000 if I2C
 	select SND_SOC_SPDIF
 	select SND_SOC_SSM2602 if I2C
 	select SND_SOC_STAC9766 if SND_SOC_AC97_BUS
@@ -176,6 +177,9 @@ config SND_SOC_MAX98088
 config SND_SOC_PCM3008
        tristate
 
+config SND_SOC_SGTL5000
+	tristate
+
 config SND_SOC_SPDIF
 	tristate
 
sound/soc/codecs/Makefile +1 -0
index 579af9c..78e36cc 100644
@@ -18,6 +18,7 @@ snd-soc-dmic-objs := dmic.o
 snd-soc-l3-objs := l3.o
 snd-soc-max98088-objs := max98088.o
 snd-soc-pcm3008-objs := pcm3008.o
+snd-soc-sgtl5000-objs := sgtl5000.o
 snd-soc-alc5623-objs := alc5623.o
 snd-soc-spdif-objs := spdif_transciever.o
 snd-soc-ssm2602-objs := ssm2602.o
sound/soc/codecs/sgtl5000.c new file +1016 -0

1,016 lines not on this page · raw message

sound/soc/codecs/sgtl5000.h new file +403 -0

403 lines not on this page · raw message

Signature · 1 line
1.7.0.4

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#1 lore raw
Quoted 9 lines · Zeng Zhaoming

v1 url:
http://mailman.alsa-project.org/pipermail/alsa-devel/2010-December/034615.html

Changes since v1:
- Add DAPM widges as Mark and Arnaud point out.
- cleanup regulator code
- new implement dai_ops functions
- sort configs in Kconfig and Makefile

Quoted 3 lines

From 1704fd5e7cf2540e247aa8a4f69d81ae5e6cbd4e Mon Sep 17 00:00:00 2001
From: Zeng Zhaoming <[email protected]>
Date: Mon, 17 Jan 2011 10:34:42 +0800

Please submit patches in the form documented in SubmittingPatches. In particular please don't send as an attachement, particularly not with extraneous stuff before the changelog.

sound/soc/codecs/sgtl5000.c line 67

Quoted 7 lines
sound/soc/codecs/sgtl5000.c new file +7 -0
+struct sgtl5000_priv {
+	int sysclk;	/* sysclk rate */
+	int master;	/* i2s master or not? */
+	int fmt;	/* i2s data format */
+	int lrclk;	/* frame clock rate */
+	int capture_channels;	/* the num of channels for capture. */
+	int small_pop;	/* hw assistant to eliminate pop */

It's not clear why you're storing some of this stuff in the private data
- for example, capture_channels is only referred to in the place where it is set (so you may as well use the original value) and small_pop is set unconditionally (it looks like the intention was to have platform data for it?).

sound/soc/codecs/sgtl5000.c line 78

Quoted 8 lines
sound/soc/codecs/sgtl5000.c new file +8 -0
+static int mic_bias_event(struct snd_soc_dapm_widget *w,
+	struct snd_kcontrol *kcontrol, int event)
+{
+	/* change mic bias resistor to 4Kohm */
+	snd_soc_write(w->codec, SGTL5000_CHIP_MIC_CTRL, SGTL5000_BIAS_R_4k);
+
+	return 0;
+}

This seems really odd - this does the write unconditionally regardless of event and never turns anything off. If this can't just be set once at startup then a comment explaining why an event is needed would be good.

Quoted 4 lines
sound/soc/codecs/sgtl5000.c new file +4 -0
+static int dac_event(struct snd_soc_dapm_widget *w,
+	struct snd_kcontrol *kcontrol, int event)
+{
+	switch (event) {

sound/soc/codecs/sgtl5000.c line 93

Quoted 10 lines
sound/soc/codecs/sgtl5000.c new file +10 -0
+	case SND_SOC_DAPM_PRE_PMU:
+		snd_soc_update_bits(w->codec, SGTL5000_CHIP_ANA_POWER,
+			SGTL5000_VAG_POWERUP|SGTL5000_DAC_POWERUP,
+			SGTL5000_VAG_POWERUP|SGTL5000_DAC_POWERUP);
+		break;
+
+	case SND_SOC_DAPM_POST_PMD:
+		snd_soc_update_bits(w->codec, SGTL5000_CHIP_ANA_POWER,
+			SGTL5000_DAC_POWERUP, 0);
+		break;

Why does the powerdown not disable VAG_POWERUP? My first thought was that VAG_POWERUP ought to be a supply widget...

sound/soc/codecs/sgtl5000.c line 131

Quoted 7 lines
sound/soc/codecs/sgtl5000.c new file +7 -0
+static int adc_event(struct snd_soc_dapm_widget *w,
+	struct snd_kcontrol *kcontrol, int event)
+{
+	snd_soc_update_bits(w->codec, SGTL5000_CHIP_ANA_POWER,
+		SGTL5000_DAC_POWERUP,
+		SGTL5000_DAC_POWERUP);
+

Again, this is really unclear.

Quoted 8 lines
sound/soc/codecs/sgtl5000.c new file +8 -0
+/* custom function to get of PCM playback volume */
+static int dac_get_volsw(struct snd_kcontrol *kcontrol,
+			 struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
+	int reg, l, r;
+
+	reg = snd_soc_read(codec, SGTL5000_CHIP_DAC_VOL);

sound/soc/codecs/sgtl5000.c line 238

Quoted 10 lines
sound/soc/codecs/sgtl5000.c new file +10 -0
+	l = (reg & SGTL5000_DAC_VOL_LEFT_MASK) >> SGTL5000_DAC_VOL_LEFT_SHIFT;
+	r = (reg & SGTL5000_DAC_VOL_RIGHT_MASK) >> SGTL5000_DAC_VOL_RIGHT_SHIFT;
+	l = l < 0x3c ? 0x3c : l;
+	l = l > 0xfc ? 0xfc : l;
+	r = r < 0x3c ? 0x3c : r;
+	r = r > 0xfc ? 0xfc : r;
+
+	/* revert it */
+	l = 0xfc - l;
+	r = 0xfc - r;

This is really unclear. The lack of any comments and use of the ternery operator isn't helping... I'd suggest describing the register semantics in a comment.

It also looks like you're missing some locking.

sound/soc/codecs/sgtl5000.c line 261

Quoted 9 lines
sound/soc/codecs/sgtl5000.c new file +9 -0
+	l = ucontrol->value.integer.value[0];
+	r = ucontrol->value.integer.value[1];
+
+	l = l < 0 ? 0 : l;
+	l = l > 0xfc - 0x3c ? 0xfc - 0x3c : l;
+	r = r < 0 ? 0 : r;
+	r = r > 0xfc - 0x3c ? 0xfc - 0x3c : r;
+	l = 0xfc - l;
+	r = 0xfc - r;

Likewise.

sound/soc/codecs/sgtl5000.c line 296

Quoted 10 lines
sound/soc/codecs/sgtl5000.c new file +10 -0
+	/* we need SOC_DOUBLE_S8_TLV with invert */
+	{
+		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
+		.name = "PCM Playback Volume",
+		.access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
+			SNDRV_CTL_ELEM_ACCESS_READWRITE,
+		.info = dac_info_volsw,
+		.get = dac_get_volsw,
+		.put = dac_put_volsw,
+	},

Perhaps implementing the abstract type would help with the clarity issues?

sound/soc/codecs/sgtl5000.c line 353

Quoted 7 lines
sound/soc/codecs/sgtl5000.c new file +7 -0
+	case SND_SOC_DAIFMT_CBM_CFS:
+	case SND_SOC_DAIFMT_CBS_CFM:
+		return -EINVAL;
+		break;
+	default:
+		return -EINVAL;
+	}

Could just have the default: case.

sound/soc/codecs/sgtl5000.c line 452

Quoted 13 lines
sound/soc/codecs/sgtl5000.c new file +13 -0
+	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
+		sgtl5000->capture_channels = channels;
+
+		reg = snd_soc_read(codec, SGTL5000_CHIP_ANA_POWER);
+		reg |= SGTL5000_ADC_POWERUP;
+
+		if (sgtl5000->capture_channels == 1)
+			reg &= ~SGTL5000_ADC_STEREO;
+		else
+			reg |= SGTL5000_ADC_STEREO;
+
+		snd_soc_write(codec, SGTL5000_CHIP_ANA_POWER, reg);
+	}

snd_soc_update_bits().

sound/soc/codecs/sgtl5000.c line 486

Quoted 11 lines
sound/soc/codecs/sgtl5000.c new file +11 -0
+	/* get devided factor of frame clock */
+	switch (sys_fs / sgtl5000->lrclk) {
+	case 4:
+		clk_ctl |= SGTL5000_RATE_MODE_DIV_4 << SGTL5000_RATE_MODE_SHIFT;
+		break;
+	case 2:
+		clk_ctl |= SGTL5000_RATE_MODE_DIV_2 << SGTL5000_RATE_MODE_SHIFT;
+		break;
+	default:
+		break;
+	}

I'd expect to see this error out if it can't find an appropriate divider?

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if ((clk_ctl & SGTL5000_MCLK_FREQ_MASK) == SGTL5000_MCLK_FREQ_PLL) {
+		snd_soc_write(codec, SGTL5000_CHIP_PLL_CTRL, pll_ctl);

This looks very odd - why has pll_ctrl been precomputed if it's only going to be used here?

Quoted 6 lines
sound/soc/codecs/sgtl5000.c new file +6 -0
+/*
+ * set dac bias
+ * common state changes:
+ * startup:
+ * off --> standby --> prepare --> on
+ * standby --> prepare --> on

sound/soc/codecs/sgtl5000.c line 626

Quoted 10 lines
sound/soc/codecs/sgtl5000.c new file +10 -0
+ *
+ * stop:
+ * on --> prepare --> standby
+ */
+static int sgtl5000_set_bias_level(struct snd_soc_codec *codec,
+				   enum snd_soc_bias_level level)
+{
+	codec->bias_level = level;
+	return 0;
+}

May as well remove this, it does nothing.

sound/soc/codecs/sgtl5000.c line 630

Quoted 3 lines
sound/soc/codecs/sgtl5000.c new file +3 -0
+#define SGTL5000_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
+			SNDRV_PCM_FMTBIT_S20_3LE |\
+			SNDRV_PCM_FMTBIT_S24_LE)

Your hw_params() claimed 32 bit support too.

sound/soc/codecs/sgtl5000.c line 727

Quoted 6 lines
sound/soc/codecs/sgtl5000.c new file +6 -0
+	/* setup i2c data ops */
+	ret = snd_soc_codec_set_cache_io(codec, 16, 16, SND_SOC_I2C);
+	if (ret < 0) {
+		dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
+		return ret;
+	}

Your resume code might get a bit clearer if you were able to use the standard cache sync functionality that the generic register cache code has.

Quoted 9 lines
sound/soc/codecs/sgtl5000.c new file +9 -0
+	/* get and enable all regulators */
+	for (i = 0; i < SGTL5000_SUPPLY_NUM; i++) {
+		struct regulator *reg;
+		reg = regulator_get(codec->dev, supply_names[i]);
+
+		if (IS_ERR(reg))
+			continue;
+
+		regulator_enable(reg);

sound/soc/codecs/sgtl5000.c line 747

Quoted 10 lines
sound/soc/codecs/sgtl5000.c new file +10 -0
+		sgtl5000->supplies[i] = reg;
+	}
+
+	/* vdda and vddio regulator must configed */
+	if (!sgtl5000->supplies[VDDA] || !sgtl5000->supplies[VDDIO]) {
+		dev_err(codec->dev,
+			"Not set vdda or vddio regulator correctly\n");
+		/* FIXME, no such platform regulator configed */
+		return -ENODEV;
+	}

This looks rather wrong; you're not clearing up any regulators you did get, you're not logging any errors if you fail to get regulators and it's *very* unusual to see any supplies being optional. You certainly need comments here.

Quoted 9 lines
sound/soc/codecs/sgtl5000.c new file +9 -0
+	vdda  = regulator_get_voltage(sgtl5000->supplies[VDDA]) / 1000;
+	vddio = regulator_get_voltage(sgtl5000->supplies[VDDIO]) / 1000;
+
+	if (regulator_get_voltage(sgtl5000->supplies[VDDD]))
+		vddd = regulator_get_voltage(sgtl5000->supplies[VDDD]) / 1000;
+	else
+		vddd = 0;
+
+	/* workaround for rev 0x11: use vddd linear regulator */

sound/soc/codecs/sgtl5000.c line 787

Quoted 10 lines
sound/soc/codecs/sgtl5000.c new file +10 -0
+	if (!vddd || (rev >= 0x11)) {
+		/* set VDDD to 1.2v */
+		lreg_ctrl |= 0x8 << SGTL5000_LINREG_VDDD_SHIFT;
+		/* power internal linear regulator */
+		ana_pwr |= SGTL5000_LINEREG_D_POWERUP;
+	} else {
+		/* turn of startup power */
+		ana_pwr &= ~SGTL5000_STARTUP_POWERUP;
+		ana_pwr &= ~SGTL5000_LINREG_SIMPLE_POWERUP;
+	}

If you have an internal LDO that can optinally be used it'd probably be better to have it visible via the regulator API. Also, if the internal regulator is in use surely an external supply should be left off?

sound/soc/codecs/sgtl5000.c line 816

Quoted 12 lines
sound/soc/codecs/sgtl5000.c new file +12 -0
+	/* set ADC/DAC ref voltage to vdda / 2 */
+	vag = vdda / 2;
+	if (vag <= SGTL5000_ANA_GND_BASE)
+		vag = 0;
+	else if (vag >= SGTL5000_ANA_GND_BASE + SGTL5000_ANA_GND_STP *
+		 (SGTL5000_ANA_GND_MASK >> SGTL5000_ANA_GND_SHIFT))
+		vag = SGTL5000_ANA_GND_MASK >> SGTL5000_ANA_GND_SHIFT;
+	else
+		vag = (vag - SGTL5000_ANA_GND_BASE) / SGTL5000_ANA_GND_STP;
+
+	/* set line out ref voltage to vddio / 2 */
+	vag = vddio / 2;

The above block of code calculates a value for vag but does nothing with it before it's overwritten here...

sound/soc/codecs/sgtl5000.c line 824

Quoted 8 lines
sound/soc/codecs/sgtl5000.c new file +8 -0
+	if (vag <= SGTL5000_LINE_OUT_GND_BASE)
+		vag = 0;
+	else if (vag >= SGTL5000_LINE_OUT_GND_BASE + SGTL5000_LINE_OUT_GND_STP *
+		 SGTL5000_LINE_OUT_GND_MAX)
+		vag = SGTL5000_LINE_OUT_GND_MAX;
+	else
+		vag = (vag - SGTL5000_LINE_OUT_GND_BASE) /
+		    SGTL5000_LINE_OUT_GND_STP;

...then there's no immediate use of this calculation either, instead we start doing...

sound/soc/codecs/sgtl5000.c line 829

Quoted 5 lines
sound/soc/codecs/sgtl5000.c new file +5 -0
+
+	snd_soc_write(codec, SGTL5000_CHIP_LINREG_CTRL, lreg_ctrl);
+	snd_soc_write(codec, SGTL5000_CHIP_ANA_POWER, ana_pwr);
+	msleep(10);
+

...this. It'd most likely be *much* clearer if the calculations and register writes were kept together.

sound/soc/codecs/sgtl5000.c line 988

Quoted 4 lines
sound/soc/codecs/sgtl5000.c new file +4 -0
+static const struct i2c_device_id sgtl5000_id[] = {
+	{"sgtl5000-codec", 0},
+	{},
+};

Remove the -codec, it's not at all idiomatic.

sound/soc/codecs/sgtl5000.c line 995

Quoted 3 lines
sound/soc/codecs/sgtl5000.c new file +3 -0
+	.driver = {
+		   .name = "sgtl5000-codec",
+		   .owner = THIS_MODULE,

Please remove it here too.

#2 lore raw
Quoted 26 lines · Mark Brown
Quoted 9 lines · Zeng Zhaoming

v1 url:
http://mailman.alsa-project.org/pipermail/alsa-devel/2010-December/034615.html

Changes since v1:
- Add DAPM widges as Mark and Arnaud point out.
- cleanup regulator code
- new implement dai_ops functions
- sort configs in Kconfig and Makefile

Quoted 3 lines

From 1704fd5e7cf2540e247aa8a4f69d81ae5e6cbd4e Mon Sep 17 00:00:00 2001
From: Zeng Zhaoming <[email protected]>
Date: Mon, 17 Jan 2011 10:34:42 +0800

Please submit patches in the form documented in SubmittingPatches. In particular please don't send as an attachement, particularly not with extraneous stuff before the changelog.

sound/soc/codecs/sgtl5000.c line 67

Quoted 7 lines
sound/soc/codecs/sgtl5000.c new file +7 -0
+struct sgtl5000_priv {
+     int sysclk;     /* sysclk rate */
+     int master;     /* i2s master or not? */
+     int fmt;        /* i2s data format */
+     int lrclk;      /* frame clock rate */
+     int capture_channels;   /* the num of channels for capture. */
+     int small_pop;  /* hw assistant to eliminate pop */
Quoted 6 lines

It's not clear why you're storing some of this stuff in the private data
- for example, capture_channels is only referred to in the place where it is set (so you may as well use the original value) and small_pop is set unconditionally (it looks like the intention was to have platform data for it?).

yes, you are right. lrclk and capture_channels fields can be remove. small_pop is a configurable option of platform data.

Quoted 5 lines
Quoted 5 lines
sound/soc/codecs/sgtl5000.c new file +5 -0
+static int mic_bias_event(struct snd_soc_dapm_widget *w,
+     struct snd_kcontrol *kcontrol, int event)
+{
+     /* change mic bias resistor to 4Kohm */
+     snd_soc_write(w->codec, SGTL5000_CHIP_MIC_CTRL, SGTL5000_BIAS_R_4k);
Quoted 9 lines

sound/soc/codecs/sgtl5000.c line 78

Quoted 3 lines
sound/soc/codecs/sgtl5000.c new file +3 -0
+
+     return 0;
+}

This seems really odd - this does the write unconditionally regardless of event and never turns anything off. If this can't just be set once at startup then a comment explaining why an event is needed would be good.

the odd code is cuased by:
If this is set to zero the micbias block is powered off, and 4Kohm is common case for enabled micbias.
0x0 = Powered off
0x1 = 2Kohm
0x2 = 4Kohm
0x3 = 8Kohm
when turn on micbias, it will set to 2Kohm, so we need to change it to 4Kohm after widget powerup.

Quoted 9 lines
Quoted 9 lines
sound/soc/codecs/sgtl5000.c new file +9 -0
+static int dac_event(struct snd_soc_dapm_widget *w,
+     struct snd_kcontrol *kcontrol, int event)
+{
+     switch (event) {
+     case SND_SOC_DAPM_PRE_PMU:
+             snd_soc_update_bits(w->codec, SGTL5000_CHIP_ANA_POWER,
+                     SGTL5000_VAG_POWERUP|SGTL5000_DAC_POWERUP,
+                     SGTL5000_VAG_POWERUP|SGTL5000_DAC_POWERUP);
+             break;
Quoted 9 lines

sound/soc/codecs/sgtl5000.c line 93

Quoted 5 lines
sound/soc/codecs/sgtl5000.c new file +5 -0
+
+     case SND_SOC_DAPM_POST_PMD:
+             snd_soc_update_bits(w->codec, SGTL5000_CHIP_ANA_POWER,
+                     SGTL5000_DAC_POWERUP, 0);
+             break;

Why does the powerdown not disable VAG_POWERUP? My first thought was that VAG_POWERUP ought to be a supply widget...

My first version, VAG_POWERUP is a supply widget, but spec says:
The headphone (and/or lineout) powerup should be set BEFORE clearing this bit.

But the powerdown sequence go against it.
So I turn to powerup VAG with DAC, and powerdown it before HP and Line_out.

Quoted 10 lines

sound/soc/codecs/sgtl5000.c line 131

Quoted 7 lines
sound/soc/codecs/sgtl5000.c new file +7 -0
+static int adc_event(struct snd_soc_dapm_widget *w,
+     struct snd_kcontrol *kcontrol, int event)
+{
+     snd_soc_update_bits(w->codec, SGTL5000_CHIP_ANA_POWER,
+             SGTL5000_ADC_POWERUP,
+             SGTL5000_ADC_POWERUP);
+

Again, this is really unclear.

ADC powerup be controlled by both CHIP_ANA_POWER and CHIP_DIG_POWER. It seems we need to set both of them.

Quoted 14 lines
Quoted 14 lines
sound/soc/codecs/sgtl5000.c new file +14 -0
+/* custom function to get of PCM playback volume */
+static int dac_get_volsw(struct snd_kcontrol *kcontrol,
+                      struct snd_ctl_elem_value *ucontrol)
+{
+     struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
+     int reg, l, r;
+
+     reg = snd_soc_read(codec, SGTL5000_CHIP_DAC_VOL);
+     l = (reg & SGTL5000_DAC_VOL_LEFT_MASK) >> SGTL5000_DAC_VOL_LEFT_SHIFT;
+     r = (reg & SGTL5000_DAC_VOL_RIGHT_MASK) >> SGTL5000_DAC_VOL_RIGHT_SHIFT;
+     l = l < 0x3c ? 0x3c : l;
+     l = l > 0xfc ? 0xfc : l;
+     r = r < 0x3c ? 0x3c : r;
+     r = r > 0xfc ? 0xfc : r;
Quoted 9 lines

sound/soc/codecs/sgtl5000.c line 238

Quoted 4 lines
sound/soc/codecs/sgtl5000.c new file +4 -0
+
+     /* revert it */
+     l = 0xfc - l;
+     r = 0xfc - r;

This is really unclear. The lack of any comments and use of the ternery operator isn't helping... I'd suggest describing the register semantics in a comment.

Ok, I will add more useful comment for this.
spec says:
Set the Right channel DAC volume with 0.5017 dB steps from 0 to -90 dB 0x3B and less = Reserved
0x3C = 0 dB
0x3D = -0.5 dB
0xF0 = -90 dB
0xFC and greater = Muted

Quoted 66 lines

It also looks like you're missing some locking.

sound/soc/codecs/sgtl5000.c line 261

Quoted 9 lines
sound/soc/codecs/sgtl5000.c new file +9 -0
+     l = ucontrol->value.integer.value[0];
+     r = ucontrol->value.integer.value[1];
+
+     l = l < 0 ? 0 : l;
+     l = l > 0xfc - 0x3c ? 0xfc - 0x3c : l;
+     r = r < 0 ? 0 : r;
+     r = r > 0xfc - 0x3c ? 0xfc - 0x3c : r;
+     l = 0xfc - l;
+     r = 0xfc - r;

Likewise.

sound/soc/codecs/sgtl5000.c line 296

Quoted 10 lines
sound/soc/codecs/sgtl5000.c new file +10 -0
+     /* we need SOC_DOUBLE_S8_TLV with invert */
+     {
+             .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
+             .name = "PCM Playback Volume",
+             .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
+                     SNDRV_CTL_ELEM_ACCESS_READWRITE,
+             .info = dac_info_volsw,
+             .get = dac_get_volsw,
+             .put = dac_put_volsw,
+     },

Perhaps implementing the abstract type would help with the clarity issues?

sound/soc/codecs/sgtl5000.c line 353

Quoted 7 lines
sound/soc/codecs/sgtl5000.c new file +7 -0
+     case SND_SOC_DAIFMT_CBM_CFS:
+     case SND_SOC_DAIFMT_CBS_CFM:
+             return -EINVAL;
+             break;
+     default:
+             return -EINVAL;
+     }

Could just have the default: case.

sound/soc/codecs/sgtl5000.c line 452

Quoted 13 lines
sound/soc/codecs/sgtl5000.c new file +13 -0
+     if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
+             sgtl5000->capture_channels = channels;
+
+             reg = snd_soc_read(codec, SGTL5000_CHIP_ANA_POWER);
+             reg |= SGTL5000_ADC_POWERUP;
+
+             if (sgtl5000->capture_channels == 1)
+                     reg &= ~SGTL5000_ADC_STEREO;
+             else
+                     reg |= SGTL5000_ADC_STEREO;
+
+             snd_soc_write(codec, SGTL5000_CHIP_ANA_POWER, reg);
+     }

snd_soc_update_bits().

sound/soc/codecs/sgtl5000.c line 486

Quoted 11 lines
sound/soc/codecs/sgtl5000.c new file +11 -0
+     /* get devided factor of frame clock */
+     switch (sys_fs / sgtl5000->lrclk) {
+     case 4:
+             clk_ctl |= SGTL5000_RATE_MODE_DIV_4 << SGTL5000_RATE_MODE_SHIFT;
+             break;
+     case 2:
+             clk_ctl |= SGTL5000_RATE_MODE_DIV_2 << SGTL5000_RATE_MODE_SHIFT;
+             break;
+     default:
+             break;
+     }
Quoted 2 lines

I'd expect to see this error out if it can't find an appropriate divider?

the default means sys_fs / lrclk = 1, not a error.

#3 lore raw
Quoted 10 lines · Zeng Zhaoming
Quoted 5 lines · Mark Brown

It's not clear why you're storing some of this stuff in the private data
- for example, capture_channels is only referred to in the place where it is set (so you may as well use the original value) and small_pop is set unconditionally (it looks like the intention was to have platform data for it?).

yes, you are right. lrclk and capture_channels fields can be remove. small_pop is a configurable option of platform data.

Your patch unconditionally sets small_pop, there's no configuration via platform data or otherwise.

Quoted 5 lines
Quoted 3 lines

of event and never turns anything off. If this can't just be set once at startup then a comment explaining why an event is needed would be good.

the odd code is cuased by:

Please note what I'm saying about making the code clear.

Quoted 11 lines
Quoted 10 lines
Quoted 10 lines
sound/soc/codecs/sgtl5000.c new file +10 -0
+     case SND_SOC_DAPM_PRE_PMU:
+             snd_soc_update_bits(w->codec, SGTL5000_CHIP_ANA_POWER,
+                     SGTL5000_VAG_POWERUP|SGTL5000_DAC_POWERUP,
+                     SGTL5000_VAG_POWERUP|SGTL5000_DAC_POWERUP);
+             break;
+
+     case SND_SOC_DAPM_POST_PMD:
+             snd_soc_update_bits(w->codec, SGTL5000_CHIP_ANA_POWER,
+                     SGTL5000_DAC_POWERUP, 0);
+             break;
Quoted 8 lines
Quoted 2 lines

Why does the powerdown not disable VAG_POWERUP? My first thought was that VAG_POWERUP ought to be a supply widget...

My first version, VAG_POWERUP is a supply widget, but spec says:
The headphone (and/or lineout) powerup should be set BEFORE clearing this bit.

But the powerdown sequence go against it.
So I turn to powerup VAG with DAC, and powerdown it before HP and Line_out.

The above doesn't seem to tie in with your code terribly well. A frequent issue throughout this code is that you're doing a bunch of unusual stuff and it's really not clear what the code is supposed to do, please work to make the code more comprehensible. This is a problem both from a legibility point of view and from the point of view of being likely to break if the core changes.

The same issue applies to a lot of the other concerns I raised, for brevity I've cut most of the discussion - in general the major issue with the code is that it's really hard to read.

Quoted 11 lines
Quoted 10 lines
Quoted 10 lines
sound/soc/codecs/sgtl5000.c new file +10 -0
+     switch (sys_fs / sgtl5000->lrclk) {
+     case 4:
+             clk_ctl |= SGTL5000_RATE_MODE_DIV_4 << SGTL5000_RATE_MODE_SHIFT;
+             break;
+     case 2:
+             clk_ctl |= SGTL5000_RATE_MODE_DIV_2 << SGTL5000_RATE_MODE_SHIFT;
+             break;
+     default:
+             break;
+     }
Quoted 4 lines
Quoted 2 lines

I'd expect to see this error out if it can't find an appropriate divider?

the default means sys_fs / lrclk = 1, not a error.

So what happens when sys_fs / lrclk is not 1? If nothing else the above code needs to be *much* clearer.