Skip to contentSkip to conversation

ASoC: Add Freescale SGTL5000 codec support

PATCH Superseded [email protected] 4 participants 12 messages Feb 16, 2011 – Feb 17, 2011

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 ASoC: Add Freescale SGTL5000 codec support

From: Zeng Zhaoming <[email protected]>

Add Freescale SGTL5000 codec support

---
changes since v2:
1. clean up register default values
2. rewrite codec power up code, add sgtl5000_set_power_regs()
3. rewrite codec clock configure code sgtl5000_set_clock()
4. reimplement PM hooks, restore register by particular order.
5. clean up dapm code, remove dac and adc event hooks.
6. clean up codec private structure, remove unnecessary fields.
7. add comments for uncommon code.

Thanks for Mark's review.

---
 sound/soc/codecs/Kconfig    |    4 +
 sound/soc/codecs/Makefile   |    1 +
 sound/soc/codecs/sgtl5000.c | 1229 +++++++++++++++++++++++++++++++++++++++++++
 sound/soc/codecs/sgtl5000.h |  403 ++++++++++++++
 4 files changed, 1637 insertions(+), 0 deletions(-)
sound/soc/codecs/Kconfig +4 -0
index c48b23c..57b909a 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 +1229 -0

1,229 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
#1 lore raw

<[email protected]> writes:

Hi,

Quoted 50 lines

From: Zeng Zhaoming <[email protected]>

Add Freescale SGTL5000 codec support

---
changes since v2:
1. clean up register default values
2. rewrite codec power up code, add sgtl5000_set_power_regs()
3. rewrite codec clock configure code sgtl5000_set_clock()
4. reimplement PM hooks, restore register by particular order.
5. clean up dapm code, remove dac and adc event hooks.
6. clean up codec private structure, remove unnecessary fields.
7. add comments for uncommon code.

Thanks for Mark's review.

---
 sound/soc/codecs/Kconfig    |    4 +
 sound/soc/codecs/Makefile   |    1 +
 sound/soc/codecs/sgtl5000.c | 1229 +++++++++++++++++++++++++++++++++++++++++++
 sound/soc/codecs/sgtl5000.h |  403 ++++++++++++++
 4 files changed, 1637 insertions(+), 0 deletions(-)
sound/soc/codecs/Kconfig +4 -0
index c48b23c..57b909a 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 +0 -0
index 579af9c..78e36cc 100644

sound/soc/codecs/Makefile snd-soc-dmic-objs := dmic.o · line 24

Quoted 7 lines
sound/soc/codecs/Makefile +1 -0
@@ -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

you're actually missing something like :
obj-$(CONFIG_SND_SOC_SGTL5000) += snd-soc-sgtl5000.o

Quoted 5 lines
sound/soc/codecs/sgtl5000.c new file +0 -0
new file mode 100644
index 0000000..2646ecb

[...]

sound/soc/codecs/sgtl5000.c line 719

Quoted 12 lines
sound/soc/codecs/sgtl5000.c new file +12 -0
+static int sgtl5000_set_bias_level(struct snd_soc_codec *codec,
+				   enum snd_soc_bias_level level)
+{
+	int i;
+	struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
+
+	switch (level) {
+	case SND_SOC_BIAS_ON:
+	case SND_SOC_BIAS_PREPARE:
+		break;
+	case SND_SOC_BIAS_STANDBY:
+		if (codec->bias_level == SND_SOC_BIAS_OFF) {

I don't see a member 'bias_level' in snd_soc_codec in the trees I have on my drive. Which tree did you use ?

Quoted 373 lines
sound/soc/codecs/sgtl5000.c new file +373 -0
+			for (i = 0; i < SGTL5000_SUPPLY_NUM; i++) {
+				if (!sgtl5000->supplies[i])
+					continue;
+				regulator_enable(sgtl5000->supplies[i]);
+			}
+		}
+		break;
+	case SND_SOC_BIAS_OFF:
+		for (i = 0; i < SGTL5000_SUPPLY_NUM; i++) {
+			if (!sgtl5000->supplies[i])
+				continue;
+			regulator_disable(sgtl5000->supplies[i]);
+		}
+		break;
+	}
+
+	codec->bias_level = level;
+	return 0;
+}
+
+#define SGTL5000_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
+			SNDRV_PCM_FMTBIT_S20_3LE |\
+			SNDRV_PCM_FMTBIT_S24_LE |\
+			SNDRV_PCM_FMTBIT_S32_LE)
+
+struct snd_soc_dai_ops sgtl5000_ops = {
+	.hw_params = sgtl5000_pcm_hw_params,
+	.digital_mute = sgtl5000_digital_mute,
+	.set_fmt = sgtl5000_set_dai_fmt,
+	.set_sysclk = sgtl5000_set_dai_sysclk,
+};
+
+static struct snd_soc_dai_driver sgtl5000_dai = {
+	.name = "sgtl5000",
+	.playback = {
+		.stream_name = "Playback",
+		.channels_min = 1,
+		.channels_max = 2,
+		/*
+		 * only support 8~48K + 96K,
+		 * TODO modify hw_param to support more
+		 */
+		.rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000,
+		.formats = SGTL5000_FORMATS,
+	},
+	.capture = {
+		.stream_name = "Capture",
+		.channels_min = 1,
+		.channels_max = 2,
+		.rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000,
+		.formats = SGTL5000_FORMATS,
+	},
+	.ops = &sgtl5000_ops,
+	.symmetric_rates = 1,
+};
+
+static int sgtl5000_volatile_register(unsigned int reg)
+{
+	switch (reg) {
+	case SGTL5000_CHIP_ID:
+	case SGTL5000_CHIP_ADCDAC_CTRL:
+	case SGTL5000_CHIP_ANA_STATUS:
+		return 1;
+	}
+
+	return 0;
+}
+
+static int sgtl5000_suspend(struct snd_soc_codec *codec, pm_message_t state)
+{
+	sgtl5000_set_bias_level(codec, SND_SOC_BIAS_OFF);
+
+	return 0;
+}
+
+/*
+ * restore all sgtl5000 registers,
+ * since a big hole between dap and regular registers,
+ * we will restore them respectively.
+ */
+static int sgtl5000_restore_regs(struct snd_soc_codec *codec)
+{
+	u16 *cache = codec->reg_cache;
+	int i;
+	int regular_regs = SGTL5000_CHIP_SHORT_CTRL >> 1;
+
+	/* restore regular registers */
+	for (i = 0; i < regular_regs; i++) {
+		int reg = i << 1;
+
+		/* this regs depends on the others */
+		if (reg == SGTL5000_CHIP_ANA_POWER ||
+			reg == SGTL5000_CHIP_CLK_CTRL ||
+			reg == SGTL5000_CHIP_LINREG_CTRL ||
+			reg == SGTL5000_CHIP_LINE_OUT_CTRL ||
+			reg == SGTL5000_CHIP_CLK_CTRL)
+			continue;
+
+		snd_soc_write(codec, reg, cache[i]);
+	}
+
+	/* restore dap registers */
+	for (i = SGTL5000_DAP_REG_OFFSET >> 1;
+			i < SGTL5000_MAX_REG_OFFSET >> 1; i++) {
+		int reg = i << 1;
+
+		snd_soc_write(codec, reg, cache[i]);
+	}
+
+	/*
+	 * restore power and other regs accroding
+	 * to set_power() and set_clock()
+	 */
+	snd_soc_write(codec, SGTL5000_CHIP_LINREG_CTRL,
+			cache[SGTL5000_CHIP_LINREG_CTRL >> 1]);
+
+	snd_soc_write(codec, SGTL5000_CHIP_ANA_POWER,
+			cache[SGTL5000_CHIP_ANA_POWER >> 1]);
+	mdelay(1);
+
+	snd_soc_write(codec, SGTL5000_CHIP_CLK_CTRL,
+			cache[SGTL5000_CHIP_CLK_CTRL >> 1]);
+
+	snd_soc_write(codec, SGTL5000_CHIP_REF_CTRL,
+			cache[SGTL5000_CHIP_REF_CTRL >> 1]);
+
+	snd_soc_write(codec, SGTL5000_CHIP_LINE_OUT_CTRL,
+			cache[SGTL5000_CHIP_LINE_OUT_CTRL >> 1]);
+	return 0;
+}
+
+static int sgtl5000_resume(struct snd_soc_codec *codec)
+{
+	/* Bring the codec back up to standby to enable regulators */
+	sgtl5000_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
+
+	/* Restore registers by cached in memory */
+	sgtl5000_restore_regs(codec);
+	return 0;
+}
+
+/*
+ * sgtl5000 has 3 internal power supplies:
+ * 1. VAG, normally set to vdda/2
+ * 2. chargepump, set to different value
+ *	according to voltage of vdda and vddio
+ * 3. line out VAG, normally set to vddio/2
+ *
+ * and should be set accroding to:
+ * 1. vddd provided by external or not
+ * 2. vdda and vddio voltage value. > 3.1v or not
+ * 3. chip revision >=0x11 or not. If >=0x11, not use external vddd.
+ */
+static int sgtl5000_set_power_regs(struct snd_soc_codec *codec)
+{
+	int vddd, vdda, vddio;
+	u16 ana_pwr, lreg_ctrl;
+	int vag;
+	struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
+
+	vdda  = regulator_get_voltage(sgtl5000->supplies[VDDA]) / 1000;
+	vddio = regulator_get_voltage(sgtl5000->supplies[VDDIO]) / 1000;
+
+	if (sgtl5000->supplies[VDDD])
+		vddd = regulator_get_voltage(sgtl5000->supplies[VDDD]) / 1000;
+	else
+		vddd = 0;
+
+	/* reset value */
+	ana_pwr = SGTL5000_DAC_STEREO |
+			SGTL5000_ADC_STEREO |
+			SGTL5000_REFTOP_POWERUP;
+	lreg_ctrl = 0;
+
+	/* if no external vddd, use internal vddd */
+	if (!vddd) {
+		/* set VDDD to 1.2v */
+		lreg_ctrl |= 0x8 << SGTL5000_LINREG_VDDD_SHIFT;
+		/* power up internal linear regulator */
+		ana_pwr |= SGTL5000_LINEREG_D_POWERUP |
+				SGTL5000_LINREG_SIMPLE_POWERUP |
+				SGTL5000_STARTUP_POWERUP;
+	}
+
+	if (vddio < 3100 && vdda < 3100) {
+		/* enable internal oscillator used for charge pump */
+		snd_soc_update_bits(codec, SGTL5000_CHIP_CLK_TOP_CTRL,
+					SGTL5000_INT_OSC_EN,
+					SGTL5000_INT_OSC_EN);
+		/* Enable VDDC charge pump */
+		ana_pwr |= SGTL5000_VDDC_CHRGPMP_POWERUP;
+	} else if (vddio >= 3100 && vdda >= 3100) {
+		/*
+		 * if vddio and vddd > 3.1v,
+		 * charge pump should be clean before set ana_pwr
+		 */
+		snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
+				SGTL5000_VDDC_CHRGPMP_POWERUP, 0);
+
+		/* VDDC use VDDIO rail */
+		lreg_ctrl |= SGTL5000_VDDC_ASSN_OVRD;
+		lreg_ctrl |= SGTL5000_VDDC_MAN_ASSN_VDDIO <<
+			    SGTL5000_VDDC_MAN_ASSN_SHIFT;
+	}
+
+	snd_soc_write(codec, SGTL5000_CHIP_ANA_POWER, ana_pwr);
+	mdelay(1);
+
+	snd_soc_write(codec, SGTL5000_CHIP_LINREG_CTRL, lreg_ctrl);
+
+	/*
+	 * if vddd linear reg has been enabled,
+	 * simple digital supply should be clear to get
+	 * proper VDDD voltage.
+	 */
+	if (ana_pwr & SGTL5000_LINEREG_D_POWERUP) {
+		ana_pwr &= ~SGTL5000_LINREG_SIMPLE_POWERUP;
+		snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
+				SGTL5000_LINREG_SIMPLE_POWERUP |
+				SGTL5000_STARTUP_POWERUP,
+				0);
+		mdelay(1);
+	}
+
+	/*
+	 * set ADC/DAC VAG to vdda / 2,
+	 * should stay in range (0.8v, 1.575v)
+	 */
+	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;
+
+	snd_soc_update_bits(codec, SGTL5000_CHIP_REF_CTRL,
+			vag << SGTL5000_ANA_GND_SHIFT,
+			vag << SGTL5000_ANA_GND_SHIFT);
+
+	/* set line out VAG to vddio / 2, in range (0.8v, 1.675v) */
+	vag = vddio / 2;
+	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;
+
+	snd_soc_update_bits(codec, SGTL5000_CHIP_LINE_OUT_CTRL,
+			vag << SGTL5000_LINE_OUT_GND_SHIFT |
+			SGTL5000_LINE_OUT_CURRENT_360u <<
+				SGTL5000_LINE_OUT_CURRENT_SHIFT,
+			vag << SGTL5000_LINE_OUT_GND_SHIFT |
+			SGTL5000_LINE_OUT_CURRENT_360u <<
+				SGTL5000_LINE_OUT_CURRENT_SHIFT);
+
+	return 0;
+}
+
+static int sgtl5000_probe(struct snd_soc_codec *codec)
+{
+	struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
+	u16 reg;
+	int ret;
+	int rev;
+	int i;
+
+	/* 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;
+	}
+
+	/* read chip information */
+	reg = snd_soc_read(codec, SGTL5000_CHIP_ID);
+	if (((reg & SGTL5000_PARTID_MASK) >> SGTL5000_PARTID_SHIFT) !=
+	    SGTL5000_PARTID_PART_ID) {
+		dev_err(codec->dev,
+			"Device with ID register %x is not a sgtl5000\n", reg);
+		ret = -ENODEV;
+		goto err_out;
+	}
+
+	rev = (reg & SGTL5000_REVID_MASK) >> SGTL5000_REVID_SHIFT;
+	dev_info(codec->dev, "sgtl5000 revision %d\n", rev);
+
+	/* get and enable all regulators */
+	for (i = 0; i < SGTL5000_SUPPLY_NUM; i++) {
+		struct regulator *reg;
+
+		/* workaround for revision 0x11, using internal LDO */
+		if (i == VDDD && rev == 0x11)
+			continue;
+
+		reg = regulator_get(codec->dev, supply_names[i]);
+
+		if (IS_ERR(reg))
+			continue;
+
+		regulator_enable(reg);
+		sgtl5000->supplies[i] = reg;
+	}
+
+	/*
+	 * vdda and vddio regulator must configured,
+	 * vddd is an optional regulator, if vddd not supply externally,
+	 * internal vddd will be enabled in sgtl5000_set_power_regs()
+	 */
+	if (!sgtl5000->supplies[VDDA] || !sgtl5000->supplies[VDDIO]) {
+		dev_err(codec->dev,
+			"Not set vdda or vddio regulator correctly\n");
+
+		/* platform regulator not configured correctly */
+		ret = -ENODEV;
+		goto err_out;
+	}
+
+	/* power up sgtl5000 */
+	ret = sgtl5000_set_power_regs(codec);
+	if (ret)
+		goto err_out;
+
+	/* enable small pop, introduce 400ms delay in turning off */
+	snd_soc_update_bits(codec, SGTL5000_CHIP_REF_CTRL,
+				SGTL5000_SMALL_POP,
+				SGTL5000_SMALL_POP);
+
+	/* enable short cut detect */
+	snd_soc_write(codec, SGTL5000_CHIP_SHORT_CTRL, 0);
+
+	/*
+	 * set i2s as default input of sound switch
+	 * TODO: add sound switch to control and dapm widge.
+	 */
+	snd_soc_write(codec, SGTL5000_CHIP_SSS_CTRL,
+			SGTL5000_DAC_SEL_I2S_IN << SGTL5000_DAC_SEL_SHIFT);
+	snd_soc_write(codec, SGTL5000_CHIP_DIG_POWER,
+			SGTL5000_ADC_EN | SGTL5000_DAC_EN);
+
+	/* enable dac vol ramp by default */
+	snd_soc_write(codec, SGTL5000_CHIP_ADCDAC_CTRL,
+			SGTL5000_DAC_VOL_RAMP_EN |
+			SGTL5000_DAC_MUTE_RIGHT |
+			SGTL5000_DAC_MUTE_LEFT);
+
+	snd_soc_write(codec, SGTL5000_CHIP_PAD_STRENGTH, 0x015f);
+
+	/* set default volume of adc */
+	reg = snd_soc_read(codec, SGTL5000_CHIP_ANA_ADC_CTRL);
+	reg &= ~SGTL5000_ADC_VOL_M6DB;
+	reg &= ~(SGTL5000_ADC_VOL_LEFT_MASK | SGTL5000_ADC_VOL_RIGHT_MASK);
+	reg |= (0xf << SGTL5000_ADC_VOL_LEFT_SHIFT)
+	    | (0xf << SGTL5000_ADC_VOL_RIGHT_SHIFT);
+	snd_soc_write(codec, SGTL5000_CHIP_ANA_ADC_CTRL, reg);
+
+	snd_soc_write(codec, SGTL5000_CHIP_ANA_CTRL,
+			SGTL5000_HP_ZCD_EN |
+			SGTL5000_ADC_ZCD_EN);
+
+	snd_soc_write(codec, SGTL5000_CHIP_MIC_CTRL, 0);
+
+	/*
+	 * disable DAP
+	 * TODO:
+	 * Enable DAP in control and dapm.
+	 */
+	snd_soc_write(codec, SGTL5000_DAP_CTRL, 0);
+

sound/soc/codecs/sgtl5000.c line 1102

Quoted 10 lines
sound/soc/codecs/sgtl5000.c new file +10 -0
+	/* leading to standby state */
+	ret = sgtl5000_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
+	if (ret)
+		goto err_out;
+
+	snd_soc_add_controls(codec, sgtl5000_snd_controls,
+			     ARRAY_SIZE(sgtl5000_snd_controls));
+
+	snd_soc_dapm_new_controls(codec, sgtl5000_dapm_widgets,
+				  ARRAY_SIZE(sgtl5000_dapm_widgets));

same kind of probleme here. on my drive, it's :
int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
const struct snd_soc_dapm_widget *widget,
int num);

sound/soc/codecs/sgtl5000.c line 1106

Quoted 4 lines
sound/soc/codecs/sgtl5000.c new file +4 -0
+
+	snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
+
+	snd_soc_dapm_new_widgets(codec);

same here.

Quoted 13 lines
sound/soc/codecs/sgtl5000.c new file +13 -0
+
+	return 0;
+
+err_out:
+	for (i = 0; i < SGTL5000_SUPPLY_NUM; i++) {
+		if (!sgtl5000->supplies[i])
+			continue;
+
+		regulator_disable(sgtl5000->supplies[i]);
+		regulator_put(sgtl5000->supplies[i]);
+	}
+
+	return ret;

sound/soc/codecs/sgtl5000.c line 1129

Quoted 10 lines
sound/soc/codecs/sgtl5000.c new file +10 -0
+}
+
+static int sgtl5000_remove(struct snd_soc_codec *codec)
+{
+	struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
+	int i;
+
+	sgtl5000_set_bias_level(codec, SND_SOC_BIAS_OFF);
+
+	snd_soc_dapm_free(codec);

same here.

[...]

Arnaud

#2 lore raw
Quoted 49 lines · Arnaud Patard

<[email protected]> writes:

Hi,

Quoted 45 lines

From: Zeng Zhaoming <[email protected]>

Add Freescale SGTL5000 codec support

---
changes since v2:
1. clean up register default values
2. rewrite codec power up code, add sgtl5000_set_power_regs()
3. rewrite codec clock configure code sgtl5000_set_clock()
4. reimplement PM hooks, restore register by particular order.
5. clean up dapm code, remove dac and adc event hooks.
6. clean up codec private structure, remove unnecessary fields.
7. add comments for uncommon code.

Thanks for Mark's review.

---
 sound/soc/codecs/Kconfig    |    4 +
 sound/soc/codecs/Makefile   |    1 +
 sound/soc/codecs/sgtl5000.c | 1229 +++++++++++++++++++++++++++++++++++++++++++
 sound/soc/codecs/sgtl5000.h |  403 ++++++++++++++
 4 files changed, 1637 insertions(+), 0 deletions(-)
sound/soc/codecs/Kconfig +4 -0
index c48b23c..57b909a 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
 
Quoted 16 lines

sound/soc/codecs/Makefile snd-soc-dmic-objs := dmic.o · line 24

Quoted 12 lines
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

you're actually missing something like :
obj-$(CONFIG_SND_SOC_SGTL5000) += snd-soc-sgtl5000.o

my mistake, I test it on 2.6.37 branch, forget to merge this line.

Quoted 9 lines
Quoted 6 lines
sound/soc/codecs/sgtl5000.c new file +0 -0
new file mode 100644
index 0000000..2646ecb

[...]

Quoted 15 lines

sound/soc/codecs/sgtl5000.c line 719

Quoted 12 lines
sound/soc/codecs/sgtl5000.c new file +12 -0
+static int sgtl5000_set_bias_level(struct snd_soc_codec *codec,
+				   enum snd_soc_bias_level level)
+{
+	int i;
+	struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
+
+	switch (level) {
+	case SND_SOC_BIAS_ON:
+	case SND_SOC_BIAS_PREPARE:
+		break;
+	case SND_SOC_BIAS_STANDBY:
+		if (codec->bias_level == SND_SOC_BIAS_OFF) {

I don't see a member 'bias_level' in snd_soc_codec in the trees I have on my drive. Which tree did you use ?

I will check the ASoC changes of 2.6.38, and rebase to it.

Quoted 8 lines
Quoted 8 lines
sound/soc/codecs/sgtl5000.c new file +7 -0
+			for (i = 0; i < SGTL5000_SUPPLY_NUM; i++) {
+				if (!sgtl5000->supplies[i])
+					continue;
+				regulator_enable(sgtl5000->supplies[i]);
+			}
+		}
+		break;

.....

Quoted 11 lines

sound/soc/codecs/sgtl5000.c line 1129

Quoted 8 lines
sound/soc/codecs/sgtl5000.c new file +8 -0
+static int sgtl5000_remove(struct snd_soc_codec *codec)
+{
+	struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
+	int i;
+
+	sgtl5000_set_bias_level(codec, SND_SOC_BIAS_OFF);
+
+	snd_soc_dapm_free(codec);

same here.

Quoted 4 lines

[...]

Arnaud

Thanks.

#3 lore raw
Quoted 6 lines · Zeng Zhaoming
Quoted 2 lines · Arnaud Patard

I don't see a member 'bias_level' in snd_soc_codec in the trees I have on my drive. Which tree did you use ?

I will check the ASoC changes of 2.6.38, and rebase to it.

You should always develop against the latest development versions of code, not released kernels. -next includes pretty much the right stuff, in the case of ASoC you want

git://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound-2.6.git for-2.6.39

(pick the highest numbered branch).

#4 lore raw
In reply to [email protected]

sound/soc/codecs/sgtl5000.c line 144

Quoted 9 lines · On Wed, Feb 16, 2011 at 06:56:16AM +0800, [email protected] wrote:
sound/soc/codecs/sgtl5000.c new file +9 -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_update_bits(w->codec, SGTL5000_CHIP_MIC_CTRL,
+			SGTL5000_BIAS_R_4k, SGTL5000_BIAS_R_4k);
+
+	return 0;
+}

I'd expect something to power off the mic bias when there's a power off event.

sound/soc/codecs/sgtl5000.c line 280

Quoted 7 lines
sound/soc/codecs/sgtl5000.c new file +7 -0
+	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;

My previous comments about the lebility of this still stand.

sound/soc/codecs/sgtl5000.c line 335

Quoted 11 lines
sound/soc/codecs/sgtl5000.c new file +11 -0
+/* need SOC_DOUBLE_S8_TLV with invert */
+#define SGTL5000_PCM_PLAYBACK_CONTROL	\
+	 {			\
+		.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,				\
+	}

Since there's no macro arguments this could just be defined directly in line.

sound/soc/codecs/sgtl5000.c line 353

Quoted 3 lines
sound/soc/codecs/sgtl5000.c new file +3 -0
+	SOC_SINGLE("Capture Attenuate Switch (-6db)",
+		SGTL5000_CHIP_ANA_ADC_CTRL,
+		8, 1, 0),

This should be a TLV control (-6 to 0 in steps of 6 dB) with Volume rather than Switch.

Quoted 6 lines
sound/soc/codecs/sgtl5000.c new file +6 -0
+	/* set devided factor of frame clock */
+	switch (sys_fs / frame_rate) {
+	case 4:
+		clk_ctl |= SGTL5000_RATE_MODE_DIV_4 << SGTL5000_RATE_MODE_SHIFT;
+		break;
+	case 2:

sound/soc/codecs/sgtl5000.c line 523

Quoted 10 lines
sound/soc/codecs/sgtl5000.c new file +10 -0
+		clk_ctl |= SGTL5000_RATE_MODE_DIV_2 << SGTL5000_RATE_MODE_SHIFT;
+		break;
+	/*
+	 * this implict case 1:
+	 * because default: sys_fs = lrclk;
+	 */
+	default:
+		clk_ctl |= SGTL5000_RATE_MODE_DIV_1 << SGTL5000_RATE_MODE_SHIFT;
+		break;
+	}

This would be much better off as case 1: with a default returning an error in case the user has misclocked the device. For example, if the ratio were to wind up as 8 then we'd accept that.

sound/soc/codecs/sgtl5000.c line 557

Quoted 13 lines
sound/soc/codecs/sgtl5000.c new file +13 -0
+	/*
+	 * calculate the divider of mclk/sample_freq,
+	 * factor of freq =96k can only be 256, since mclk in range (12m,27m)
+	 */
+	if (sys_fs * 256 == sgtl5000->sysclk)
+		clk_ctl |= SGTL5000_MCLK_FREQ_256FS <<
+			SGTL5000_MCLK_FREQ_SHIFT;
+	else if (sys_fs * 384 == sgtl5000->sysclk)
+		clk_ctl |= SGTL5000_MCLK_FREQ_384FS <<
+			SGTL5000_MCLK_FREQ_SHIFT;
+	else if (sys_fs * 512 == sgtl5000->sysclk)
+		clk_ctl |= SGTL5000_MCLK_FREQ_512FS <<
+			SGTL5000_MCLK_FREQ_SHIFT;

This'd be clearer as a switch on sysfs / sysclk with this:

sound/soc/codecs/sgtl5000.c line 567

Quoted 10 lines
sound/soc/codecs/sgtl5000.c new file +10 -0
+	else {
+		/* if mclk not satisify the divider, using pll */
+		if (sgtl5000->master)
+			clk_ctl |= SGTL5000_MCLK_FREQ_PLL <<
+				SGTL5000_MCLK_FREQ_SHIFT;
+		else {
+			pr_err("%s: PLL not supported in slave mode\n",
+				__func__);
+			return -EINVAL;
+		}

as the default.

sound/soc/codecs/sgtl5000.c line 616

Quoted 5 lines
sound/soc/codecs/sgtl5000.c new file +5 -0
+	} else
+		/* power down pll */
+		snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
+			SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP,
+			0);

Use { } for the else case if the main case uses them.

sound/soc/codecs/sgtl5000.c line 723

Quoted 6 lines
sound/soc/codecs/sgtl5000.c new file +6 -0
+	case SND_SOC_BIAS_STANDBY:
+		if (codec->bias_level == SND_SOC_BIAS_OFF) {
+			for (i = 0; i < SGTL5000_SUPPLY_NUM; i++) {
+				if (!sgtl5000->supplies[i])
+					continue;
+				regulator_enable(sgtl5000->supplies[i]);

Why are the supplies optional?

sound/soc/codecs/sgtl5000.c line 881

Quoted 2 lines
sound/soc/codecs/sgtl5000.c new file +2 -0
+	vdda  = regulator_get_voltage(sgtl5000->supplies[VDDA]) / 1000;
+	vddio = regulator_get_voltage(sgtl5000->supplies[VDDIO]) / 1000;

You're not checking these for errors.

sound/soc/codecs/sgtl5000.c line 886

Quoted 4 lines
sound/soc/codecs/sgtl5000.c new file +4 -0
+	if (sgtl5000->supplies[VDDD])
+		vddd = regulator_get_voltage(sgtl5000->supplies[VDDD]) / 1000;
+	else
+		vddd = 0;

If VDD is optional it'd seem easier to substitute in a fixed voltage regulator when it's not detected. It'd make the code much more idiomatic and save having to special case missing regulators everywhere.

sound/soc/codecs/sgtl5000.c line 902

Quoted 8 lines
sound/soc/codecs/sgtl5000.c new file +8 -0
+	if (!vddd) {
+		/* set VDDD to 1.2v */
+		lreg_ctrl |= 0x8 << SGTL5000_LINREG_VDDD_SHIFT;
+		/* power up internal linear regulator */
+		ana_pwr |= SGTL5000_LINEREG_D_POWERUP |
+				SGTL5000_LINREG_SIMPLE_POWERUP |
+				SGTL5000_STARTUP_POWERUP;
+	}

Or even implement an actual regulator driver for it.

sound/soc/codecs/sgtl5000.c line 1078

Quoted 7 lines
sound/soc/codecs/sgtl5000.c new file +7 -0
+	/* set default volume of adc */
+	reg = snd_soc_read(codec, SGTL5000_CHIP_ANA_ADC_CTRL);
+	reg &= ~SGTL5000_ADC_VOL_M6DB;
+	reg &= ~(SGTL5000_ADC_VOL_LEFT_MASK | SGTL5000_ADC_VOL_RIGHT_MASK);
+	reg |= (0xf << SGTL5000_ADC_VOL_LEFT_SHIFT)
+	    | (0xf << SGTL5000_ADC_VOL_RIGHT_SHIFT);
+	snd_soc_write(codec, SGTL5000_CHIP_ANA_ADC_CTRL, reg);

Leave this up to userspace.

#5 lore raw
Quoted 15 lines · Mark Brown

sound/soc/codecs/sgtl5000.c line 144

Quoted 9 lines · On Wed, Feb 16, 2011 at 06:56:16AM +0800, [email protected] wrote:
sound/soc/codecs/sgtl5000.c new file +9 -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_update_bits(w->codec, SGTL5000_CHIP_MIC_CTRL,
+			SGTL5000_BIAS_R_4k, SGTL5000_BIAS_R_4k);
+
+	return 0;
+}

I'd expect something to power off the mic bias when there's a power off event.

I will add it in the next version.

Quoted 10 lines

sound/soc/codecs/sgtl5000.c line 280

Quoted 7 lines
sound/soc/codecs/sgtl5000.c new file +7 -0
+	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;

My previous comments about the lebility of this still stand.

Sorry, I don't catch you means quite well, you means more comments to make it clearer?

Quoted 6 lines
Quoted 6 lines
sound/soc/codecs/sgtl5000.c new file +6 -0
+/* need SOC_DOUBLE_S8_TLV with invert */
+#define SGTL5000_PCM_PLAYBACK_CONTROL	\
+	 {			\
+		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,	\
+		.name = "PCM Playback Volume",		\
+		.access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |	\
Quoted 9 lines

sound/soc/codecs/sgtl5000.c line 335

Quoted 5 lines
sound/soc/codecs/sgtl5000.c new file +5 -0
+			SNDRV_CTL_ELEM_ACCESS_READWRITE,	\
+		.info = dac_info_volsw,				\
+		.get = dac_get_volsw,				\
+		.put = dac_put_volsw,				\
+	}

Since there's no macro arguments this could just be defined directly in line.

you means add it to kcontrol array directly instead of define a macro for it?

Quoted 44 lines

sound/soc/codecs/sgtl5000.c line 353

Quoted 3 lines
sound/soc/codecs/sgtl5000.c new file +3 -0
+	SOC_SINGLE("Capture Attenuate Switch (-6db)",
+		SGTL5000_CHIP_ANA_ADC_CTRL,
+		8, 1, 0),

This should be a TLV control (-6 to 0 in steps of 6 dB) with Volume rather than Switch.

Quoted 6 lines
sound/soc/codecs/sgtl5000.c new file +6 -0
+	/* set devided factor of frame clock */
+	switch (sys_fs / frame_rate) {
+	case 4:
+		clk_ctl |= SGTL5000_RATE_MODE_DIV_4 << SGTL5000_RATE_MODE_SHIFT;
+		break;
+	case 2:

sound/soc/codecs/sgtl5000.c line 523

Quoted 10 lines
sound/soc/codecs/sgtl5000.c new file +10 -0
+		clk_ctl |= SGTL5000_RATE_MODE_DIV_2 << SGTL5000_RATE_MODE_SHIFT;
+		break;
+	/*
+	 * this implict case 1:
+	 * because default: sys_fs = lrclk;
+	 */
+	default:
+		clk_ctl |= SGTL5000_RATE_MODE_DIV_1 << SGTL5000_RATE_MODE_SHIFT;
+		break;
+	}

This would be much better off as case 1: with a default returning an error in case the user has misclocked the device. For example, if the ratio were to wind up as 8 then we'd accept that.

sound/soc/codecs/sgtl5000.c line 557

Quoted 13 lines
sound/soc/codecs/sgtl5000.c new file +13 -0
+	/*
+	 * calculate the divider of mclk/sample_freq,
+	 * factor of freq =96k can only be 256, since mclk in range (12m,27m)
+	 */
+	if (sys_fs * 256 == sgtl5000->sysclk)
+		clk_ctl |= SGTL5000_MCLK_FREQ_256FS <<
+			SGTL5000_MCLK_FREQ_SHIFT;
+	else if (sys_fs * 384 == sgtl5000->sysclk)
+		clk_ctl |= SGTL5000_MCLK_FREQ_384FS <<
+			SGTL5000_MCLK_FREQ_SHIFT;
+	else if (sys_fs * 512 == sgtl5000->sysclk)
+		clk_ctl |= SGTL5000_MCLK_FREQ_512FS <<
+			SGTL5000_MCLK_FREQ_SHIFT;

This'd be clearer as a switch on sysfs / sysclk with this:

Quoted 13 lines

sound/soc/codecs/sgtl5000.c line 567

Quoted 10 lines
sound/soc/codecs/sgtl5000.c new file +10 -0
+	else {
+		/* if mclk not satisify the divider, using pll */
+		if (sgtl5000->master)
+			clk_ctl |= SGTL5000_MCLK_FREQ_PLL <<
+				SGTL5000_MCLK_FREQ_SHIFT;
+		else {
+			pr_err("%s: PLL not supported in slave mode\n",
+				__func__);
+			return -EINVAL;
+		}

as the default.

Yes, it is clearer. thanks for the suggestion.

Quoted 22 lines

sound/soc/codecs/sgtl5000.c line 616

Quoted 5 lines
sound/soc/codecs/sgtl5000.c new file +5 -0
+	} else
+		/* power down pll */
+		snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
+			SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP,
+			0);

Use { } for the else case if the main case uses them.

sound/soc/codecs/sgtl5000.c line 723

Quoted 6 lines
sound/soc/codecs/sgtl5000.c new file +6 -0
+	case SND_SOC_BIAS_STANDBY:
+		if (codec->bias_level == SND_SOC_BIAS_OFF) {
+			for (i = 0; i < SGTL5000_SUPPLY_NUM; i++) {
+				if (!sgtl5000->supplies[i])
+					continue;
+				regulator_enable(sgtl5000->supplies[i]);

Why are the supplies optional?

sound/soc/codecs/sgtl5000.c line 881

Quoted 2 lines
sound/soc/codecs/sgtl5000.c new file +2 -0
+	vdda  = regulator_get_voltage(sgtl5000->supplies[VDDA]) / 1000;
+	vddio = regulator_get_voltage(sgtl5000->supplies[VDDIO]) / 1000;

You're not checking these for errors.

Quoted 9 lines

sound/soc/codecs/sgtl5000.c line 886

Quoted 4 lines
sound/soc/codecs/sgtl5000.c new file +4 -0
+	if (sgtl5000->supplies[VDDD])
+		vddd = regulator_get_voltage(sgtl5000->supplies[VDDD]) / 1000;
+	else
+		vddd = 0;

If VDD is optional it'd seem easier to substitute in a fixed voltage regulator when it's not detected. It'd make the code much more idiomatic and save having to special case missing regulators everywhere.

Ok, I will try to add a fixed regulator if VDDD not provided externally.

Quoted 11 lines

sound/soc/codecs/sgtl5000.c line 902

Quoted 8 lines
sound/soc/codecs/sgtl5000.c new file +8 -0
+	if (!vddd) {
+		/* set VDDD to 1.2v */
+		lreg_ctrl |= 0x8 << SGTL5000_LINREG_VDDD_SHIFT;
+		/* power up internal linear regulator */
+		ana_pwr |= SGTL5000_LINEREG_D_POWERUP |
+				SGTL5000_LINREG_SIMPLE_POWERUP |
+				SGTL5000_STARTUP_POWERUP;
+	}

Or even implement an actual regulator driver for it.

Quoted 9 lines

sound/soc/codecs/sgtl5000.c line 1078

Quoted 7 lines
sound/soc/codecs/sgtl5000.c new file +7 -0
+	/* set default volume of adc */
+	reg = snd_soc_read(codec, SGTL5000_CHIP_ANA_ADC_CTRL);
+	reg &= ~SGTL5000_ADC_VOL_M6DB;
+	reg &= ~(SGTL5000_ADC_VOL_LEFT_MASK | SGTL5000_ADC_VOL_RIGHT_MASK);
+	reg |= (0xf << SGTL5000_ADC_VOL_LEFT_SHIFT)
+	    | (0xf << SGTL5000_ADC_VOL_RIGHT_SHIFT);
+	snd_soc_write(codec, SGTL5000_CHIP_ANA_ADC_CTRL, reg);

Leave this up to userspace.

Thanks.

#6 lore raw
Quoted 12 lines · Zeng Zhaoming
Quoted 10 lines · Mark Brown

sound/soc/codecs/sgtl5000.c line 280

Quoted 6 lines · On Wed, Feb 16, 2011 at 06:56:16AM +0800, [email protected] wrote:
sound/soc/codecs/sgtl5000.c new file +6 -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;

My previous comments about the lebility of this still stand.

Quoted 1 line

Sorry, I don't catch you means quite well, you means more comments to make it clearer?

It needs to be clearer and I'm pretty certain that comments aren't enough to deal with the issue - the stack of ternery operators and magic numbers just isn't good for comprehensibility.

Quoted 4 lines
Quoted 2 lines

Since there's no macro arguments this could just be defined directly in line.

you means add it to kcontrol array directly instead of define a macro for it?

Yes.

#7 lore raw
In reply to [email protected]
Quoted 3 lines · On Tue, Feb 15, 2011 at 4:56 PM, <[email protected]> wrote:

From: Zeng Zhaoming <[email protected]>

Add Freescale SGTL5000 codec support

Please provide a description of this part. What is it, what features does it have, etc.

sound/soc/codecs/Kconfig config SND_SOC_MAX98088 · line 182

Quoted 7 lines
(quoted diff) +0 -0
@@ -176,6 +177,9 @@ config SND_SOC_MAX98088

 config SND_SOC_PCM3008
       tristate

sound/soc/codecs/Kconfig +3 -0
+config SND_SOC_SGTL5000
+       tristate
+

Please provide some kind of descriptive text here.

Quoted 4 lines

+       l = l < 0x3c ? 0x3c : l;
+       l = l > 0xfc ? 0xfc : l;
+       r = r < 0x3c ? 0x3c : r;
+       r = r > 0xfc ? 0xfc : r;

Use the clamp() macro instead.

Quoted 4 lines

+
+       /* revert it */
+       l = 0xfc - l;
+       r = 0xfc - r;

I think you mean "invert it". "revert" means to undo a change.

Quoted 6 lines

+       if (mute)
+               snd_soc_update_bits(codec, SGTL5000_CHIP_ADCDAC_CTRL,
+                               adcdac_ctrl, adcdac_ctrl);
+       else
+               snd_soc_update_bits(codec, SGTL5000_CHIP_ADCDAC_CTRL,
+                               adcdac_ctrl, 0);

How about:

snd_soc_update_bits(codec, SGTL5000_CHIP_ADCDAC_CTRL, adcdac_ctrl, mute ? adcdac_ctrl : 0);

sound/soc/codecs/sgtl5000.c line 793

Quoted 6 lines
sound/soc/codecs/sgtl5000.c new file +6 -0
+static int sgtl5000_suspend(struct snd_soc_codec *codec, pm_message_t state)
+{
+       sgtl5000_set_bias_level(codec, SND_SOC_BIAS_OFF);
+
+       return 0;
+}

#ifdef CONFIG_PM ?

Quoted 9 lines

+       /*
+        * copy DAP default values to default value array.
+        * sgtl5000 register space has a big hole, merge it
+        * at init phase makes life easy.
+        * FIXME: should we drop 'const' of sgtl5000_regs?
+        */
+       memcpy((void *)(&sgtl5000_regs[0] + (SGTL5000_DAP_REG_OFFSET >> 1)),
+                       sgtl5000_dap_regs,
+                       0x3a);

I don't think you need the "(void *)"

And what is 0x3a?

sound/soc/codecs/sgtl5000.c line 1228

Quoted 2 lines
sound/soc/codecs/sgtl5000.c new file +2 -0
+MODULE_DESCRIPTION("ASoC sgtl5000 driver");
+MODULE_AUTHOR("Freescale Semiconductor, Inc.");

Please consider putting your name here, so that people know who the real author is.

sound/soc/codecs/sgtl5000.h line 2

Quoted 8 lines
sound/soc/codecs/sgtl5000.h new file +2 -0
new file mode 100644
index 0000000..7f24655
@@ -0,0 +1,403 @@
+/*
+ * sgtl5000.h - SGTL5000 audio codec interface

This whole file should probably be merged into sgtl5000.c, since that file is the only file that #includes sgtl5000.h. No point in having a header file that is only included by one other file.

sound/soc/codecs/sgtl5000.h line 4

Quoted 2 lines
sound/soc/codecs/sgtl5000.h new file +2 -0
+ *
+ * Copyright 2008-2009 Freescale Semiconductor, Inc.

So the header file is copyright 2009, but the source file is copyright 2011?

sound/soc/codecs/sgtl5000.h line 9

Quoted 5 lines
sound/soc/codecs/sgtl5000.h new file +5 -0
+ *
+ *  This program is free software; you can redistribute  it and/or modify it
+ *  under  the terms of  the GNU General  Public License as published by the
+ *  Free Software Foundation;  either version 2 of the  License, or (at your
+ *  option) any later version.

Also, the header file is "GPL v2 or later", but the source file is "GPL v2 only". Pick one or the other, but don't use both. I'm surprised your patch was approved by the PPP legal review.

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale
#8 lore raw
Quoted 8 lines · Timur Tabi

sound/soc/codecs/sgtl5000.c line 793

Quoted 6 lines
sound/soc/codecs/sgtl5000.c new file +6 -0
+static int sgtl5000_suspend(struct snd_soc_codec *codec, pm_message_t state)
+{
+       sgtl5000_set_bias_level(codec, SND_SOC_BIAS_OFF);
+
+       return 0;
+}

#ifdef CONFIG_PM ?

Should be CONFIG_SUSPEND.

Quoted 5 lines
Quoted 3 lines

+       memcpy((void *)(&sgtl5000_regs[0] + (SGTL5000_DAP_REG_OFFSET >> 1)),
+                       sgtl5000_dap_regs,
+                       0x3a);

I don't think you need the "(void *)"

You never need to cast to void in C. C++ requires this but not C.

#9 lore raw
In reply to Timur Tabi
Quoted 18 lines · Timur Tabi
Quoted 3 lines · On Tue, Feb 15, 2011 at 4:56 PM, <[email protected]> wrote:

From: Zeng Zhaoming <[email protected]>

Add Freescale SGTL5000 codec support

Please provide a description of this part. What is it, what features does it have, etc.

sound/soc/codecs/Kconfig config SND_SOC_MAX98088 · line 182

Quoted 7 lines
(quoted diff) +0 -0
@@ -176,6 +177,9 @@ config SND_SOC_MAX98088

 config SND_SOC_PCM3008
       tristate

sound/soc/codecs/Kconfig +3 -0
+config SND_SOC_SGTL5000
+       tristate
+

Please provide some kind of descriptive text here.

Quoted 6 lines
Quoted 4 lines

+       l = l < 0x3c ? 0x3c : l;
+       l = l > 0xfc ? 0xfc : l;
+       r = r < 0x3c ? 0x3c : r;
+       r = r > 0xfc ? 0xfc : r;

Use the clamp() macro instead.

thanks for the hint.

Quoted 8 lines
Quoted 5 lines

+
+       /* revert it */
+       l = 0xfc - l;
+       r = 0xfc - r;

I think you mean "invert it". "revert" means to undo a change.

yes, invert it. ;)

Quoted 7 lines
Quoted 6 lines

+       if (mute)
+               snd_soc_update_bits(codec, SGTL5000_CHIP_ADCDAC_CTRL,
+                               adcdac_ctrl, adcdac_ctrl);
+       else
+               snd_soc_update_bits(codec, SGTL5000_CHIP_ADCDAC_CTRL,
+                               adcdac_ctrl, 0);

Quoted 5 lines

How about:

snd_soc_update_bits(codec, SGTL5000_CHIP_ADCDAC_CTRL, adcdac_ctrl, mute ? adcdac_ctrl : 0);

Yes, this one is better. I will change to this style in the whole file.

Quoted 30 lines

sound/soc/codecs/sgtl5000.c line 793

Quoted 6 lines
sound/soc/codecs/sgtl5000.c new file +6 -0
+static int sgtl5000_suspend(struct snd_soc_codec *codec, pm_message_t state)
+{
+       sgtl5000_set_bias_level(codec, SND_SOC_BIAS_OFF);
+
+       return 0;
+}

#ifdef CONFIG_PM ?

Quoted 9 lines

+       /*
+        * copy DAP default values to default value array.
+        * sgtl5000 register space has a big hole, merge it
+        * at init phase makes life easy.
+        * FIXME: should we drop 'const' of sgtl5000_regs?
+        */
+       memcpy((void *)(&sgtl5000_regs[0] + (SGTL5000_DAP_REG_OFFSET >> 1)),
+                       sgtl5000_dap_regs,
+                       0x3a);

I don't think you need the "(void *)"

And what is 0x3a?

sound/soc/codecs/sgtl5000.c line 1228

Quoted 2 lines
sound/soc/codecs/sgtl5000.c new file +2 -0
+MODULE_DESCRIPTION("ASoC sgtl5000 driver");
+MODULE_AUTHOR("Freescale Semiconductor, Inc.");

Please consider putting your name here, so that people know who the real author is.

Quoted 13 lines

sound/soc/codecs/sgtl5000.h line 2

Quoted 8 lines
sound/soc/codecs/sgtl5000.h new file +2 -0
new file mode 100644
index 0000000..7f24655
@@ -0,0 +1,403 @@
+/*
+ * sgtl5000.h - SGTL5000 audio codec interface

This whole file should probably be merged into sgtl5000.c, since that file is the only file that #includes sgtl5000.h. No point in having a header file that is only included by one other file.

This is not common on ASoC codec drivers, many drivers have their own header file. I think it make the code more clear.

Quoted 11 lines

sound/soc/codecs/sgtl5000.h line 4

Quoted 2 lines
sound/soc/codecs/sgtl5000.h new file +2 -0
+ *
+ * Copyright 2008-2009 Freescale Semiconductor, Inc.

So the header file is copyright 2009, but the source file is copyright 2011?

sound/soc/codecs/sgtl5000.h line 9

Quoted 5 lines
sound/soc/codecs/sgtl5000.h new file +5 -0
+ *
+ *  This program is free software; you can redistribute  it and/or modify it
+ *  under  the terms of  the GNU General  Public License as published by the
+ *  Free Software Foundation;  either version 2 of the  License, or (at your
+ *  option) any later version.
Quoted 4 lines

Also, the header file is "GPL v2 or later", but the source file is "GPL v2 only". Pick one or the other, but don't use both. I'm surprised your patch was approved by the PPP legal review.

Ok, I will change it to GPL v2 only, thanks to point it out.

Quoted 7 lines
Signature · 6 lines
Timur Tabi
Linux kernel developer at Freescale
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

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

#10 lore raw
In reply to [email protected]

<[email protected]> writes:

Hi,

Quoted 16 lines

From: Zeng Zhaoming <[email protected]>

Add Freescale SGTL5000 codec support

---
changes since v2:
1. clean up register default values
2. rewrite codec power up code, add sgtl5000_set_power_regs()
3. rewrite codec clock configure code sgtl5000_set_clock()
4. reimplement PM hooks, restore register by particular order.
5. clean up dapm code, remove dac and adc event hooks.
6. clean up codec private structure, remove unnecessary fields.
7. add comments for uncommon code.

Thanks for Mark's review.

[...]

Quoted 19 lines
sound/soc/codecs/sgtl5000.c new file +19 -0
+/*
+ * custom put function for PCM playback volume
+ * PCM 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
+ */
+static int dac_put_volsw(struct snd_kcontrol *kcontrol,
+			 struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
+	int reg, l, r;
+
+	l = ucontrol->value.integer.value[0];
+	r = ucontrol->value.integer.value[1];
+
+	l = l < 0 ? 0 : l;

sound/soc/codecs/sgtl5000.c line 320

Quoted 10 lines
sound/soc/codecs/sgtl5000.c new file +10 -0
+	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;
+
+	reg = l << SGTL5000_DAC_VOL_LEFT_SHIFT |
+	    r << SGTL5000_DAC_VOL_RIGHT_SHIFT;
+
+	snd_soc_update_bits_locked(codec, SGTL5000_CHIP_DAC_VOL, reg, reg);

Why are you using update_bits_locked and not snd_soc_write ? Here snd_soc_update_bits_locked is just good at making the PCM playback mixer acting weird (I even got to a point that with a 0 value in the mixer, I was unable to raise the volume). snd_soc_write just works.

[...]

Quoted 8 lines
sound/soc/codecs/sgtl5000.c new file +8 -0
+
+static int sgtl5000_probe(struct snd_soc_codec *codec)
+{
+	struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
+	u16 reg;
+	int ret;
+	int rev;
+	int i;

sound/soc/codecs/sgtl5000.c line 999

Quoted 10 lines
sound/soc/codecs/sgtl5000.c new file +10 -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;
+	}
+
+	/* read chip information */
+	reg = snd_soc_read(codec, SGTL5000_CHIP_ID);

I've a problem with doing that here. You do assume that the regulators are enabled at this point which is not always the case. This results on a timeout on i2c and then reg contains 0. Please enable regultors firsts And no, I don't see having the regulators for things like vddio set to "always on" in the machine file as a good idea. If the driver is not loaded, having the codec off is good for power consumption.

Arnaud

#11 lore raw
Quoted 59 lines · Arnaud Patard

<[email protected]> writes:

Hi,

Quoted 16 lines

From: Zeng Zhaoming <[email protected]>

Add Freescale SGTL5000 codec support

---
changes since v2:
1. clean up register default values
2. rewrite codec power up code, add sgtl5000_set_power_regs()
3. rewrite codec clock configure code sgtl5000_set_clock()
4. reimplement PM hooks, restore register by particular order.
5. clean up dapm code, remove dac and adc event hooks.
6. clean up codec private structure, remove unnecessary fields.
7. add comments for uncommon code.

Thanks for Mark's review.

[...]

Quoted 19 lines
sound/soc/codecs/sgtl5000.c new file +19 -0
+/*
+ * custom put function for PCM playback volume
+ * PCM 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
+ */
+static int dac_put_volsw(struct snd_kcontrol *kcontrol,
+			 struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
+	int reg, l, r;
+
+	l = ucontrol->value.integer.value[0];
+	r = ucontrol->value.integer.value[1];
+
+	l = l < 0 ? 0 : l;

sound/soc/codecs/sgtl5000.c line 320

Quoted 10 lines
sound/soc/codecs/sgtl5000.c new file +10 -0
+	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;
+
+	reg = l << SGTL5000_DAC_VOL_LEFT_SHIFT |
+	    r << SGTL5000_DAC_VOL_RIGHT_SHIFT;
+
+	snd_soc_update_bits_locked(codec, SGTL5000_CHIP_DAC_VOL, reg, reg);

Why are you using update_bits_locked and not snd_soc_write ? Here snd_soc_update_bits_locked is just good at making the PCM playback mixer acting weird (I even got to a point that with a 0 value in the mixer, I was unable to raise the volume). snd_soc_write just works.

Quoted 2 lines

[...]

hi, Arnaud, thanks. I just find this problem too.
It will be changed to snd_soc_write() in v4.

Quoted 17 lines
Quoted 17 lines
sound/soc/codecs/sgtl5000.c new file +17 -0
+
+static int sgtl5000_probe(struct snd_soc_codec *codec)
+{
+	struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
+	u16 reg;
+	int ret;
+	int rev;
+	int i;
+
+	/* 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;
+	}
+
+	/* read chip information */
Quoted 9 lines

sound/soc/codecs/sgtl5000.c line 999

Quoted 1 line
sound/soc/codecs/sgtl5000.c new file +1 -0
+	reg = snd_soc_read(codec, SGTL5000_CHIP_ID);

I've a problem with doing that here. You do assume that the regulators are enabled at this point which is not always the case. This results on a timeout on i2c and then reg contains 0. Please enable regultors firsts And no, I don't see having the regulators for things like vddio set to "always on" in the machine file as a good idea. If the driver is not loaded, having the codec off is good for power consumption.

you are right, I always enable regulator in platform code, it is better to enable it when driver is loading. I will fix it in v4.

Quoted 5 lines

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