Skip to contentSkip to conversation

ASoC: CS4271 codec support

PATCH In review Alexander 3 participants 4 messages Jan 16, 2011 – Jan 17, 2011

Subsystems (3)
lore
Share & tools
lore

Alexander

author
#0 lore raw

PATCH 1/1 ASoC: CS4271 codec support

From: Alexander Sverdlin <[email protected]>

Added support for CS4271 codec to ASoC.
Update: De-emphasis is managed automatically with on-off control.

---
 include/sound/cs4271.h    |   25 ++
 sound/soc/codecs/Kconfig  |    4 +
 sound/soc/codecs/Makefile |    2 +
 sound/soc/codecs/cs4271.c |  641 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 672 insertions(+), 0 deletions(-)
include/sound/cs4271.h new file +25 -0
new file mode 100644
index 0000000..16f8d32
@@ -0,0 +1,25 @@
+/*
+ * Definitions for CS4271 ASoC codec driver
+ *
+ * Copyright (c) 2010 Alexander Sverdlin <[email protected]>
+ *
+ * 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.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __CS4271_H
+#define __CS4271_H
+
+struct cs4271_platform_data {
+	int gpio_nreset;	/* GPIO driving Reset pin, if any */
+	int gpio_disable;	/* GPIO that disable serial bus, if any */
+};
+
+#endif /* __CS4271_H */
sound/soc/codecs/Kconfig +4 -0
index c48b23c..c3056bd 100644
@@ -26,6 +26,7 @@ config SND_SOC_ALL_CODECS
 	select SND_SOC_CQ0093VC if MFD_DAVINCI_VOICECODEC
 	select SND_SOC_CS42L51 if I2C
 	select SND_SOC_CS4270 if I2C
+	select SND_SOC_CS4271 if SND_SOC_I2C_AND_SPI
 	select SND_SOC_CX20442
 	select SND_SOC_DA7210 if I2C
 	select SND_SOC_JZ4740_CODEC if SOC_JZ4740
@@ -155,6 +156,9 @@ config SND_SOC_CS4270_VD33_ERRATA
 	bool
 	depends on SND_SOC_CS4270
 
+config SND_SOC_CS4271
+	tristate
+
 config SND_SOC_CX20442
 	tristate
 
sound/soc/codecs/Makefile +2 -0
index 579af9c..ece05e1 100644
@@ -12,6 +12,7 @@ snd-soc-ak4671-objs := ak4671.o
 snd-soc-cq93vc-objs := cq93vc.o
 snd-soc-cs42l51-objs := cs42l51.o
 snd-soc-cs4270-objs := cs4270.o
+snd-soc-cs4271-objs := cs4271.o
 snd-soc-cx20442-objs := cx20442.o
 snd-soc-da7210-objs := da7210.o
 snd-soc-dmic-objs := dmic.o
@@ -91,6 +92,7 @@ obj-$(CONFIG_SND_SOC_AK4671)	+= snd-soc-ak4671.o
 obj-$(CONFIG_SND_SOC_CQ0093VC) += snd-soc-cq93vc.o
 obj-$(CONFIG_SND_SOC_CS42L51)	+= snd-soc-cs42l51.o
 obj-$(CONFIG_SND_SOC_CS4270)	+= snd-soc-cs4270.o
+obj-$(CONFIG_SND_SOC_CS4271)	+= snd-soc-cs4271.o
 obj-$(CONFIG_SND_SOC_CX20442)	+= snd-soc-cx20442.o
 obj-$(CONFIG_SND_SOC_DA7210)	+= snd-soc-da7210.o
 obj-$(CONFIG_SND_SOC_DMIC)	+= snd-soc-dmic.o
sound/soc/codecs/cs4271.c new file +641 -0

641 lines not on this page · raw message

#1 lore raw

sound/soc/codecs/cs4271.c line 142

Quoted 5 lines · Alexander
sound/soc/codecs/cs4271.c new file +5 -0
+/* CS4271 default register settings, except auto-mute is off */
+static const u8 cs4271_dflt_reg[CS4271_NR_REGS] = {
+	0, 0, 0, CS4271_DACVOL_ATAPI_AL_BR, 0, 0, 0,
+	CS4271_MODE2_CPEN | CS4271_MODE2_PDN,
+};

I'd be better to leave these as defaults, and perform any initialization in the the cs4271_probe() function.

sound/soc/codecs/cs4271.c line 287

Quoted 7 lines
sound/soc/codecs/cs4271.c new file +7 -0
+	/* Configure DAC */
+	val = cs4271_clk_tab[i].speed_mode;
+
+	if (cs4271->master)
+		val |= cs4271_clk_tab[i].mclk_master | CS4271_MODE1_MASTER;
+	else
+		val |= cs4271_clk_tab[i].mclk_slave;

This should ideally live in cs4271_set_dai_fmt().

sound/soc/codecs/cs4271.c line 299

Quoted 11 lines
sound/soc/codecs/cs4271.c new file +11 -0
+	switch (cs4271->mode) {
+	case SND_SOC_DAIFMT_LEFT_J:
+		val |= CS4271_MODE1_DAC_DIF_LJ;
+		break;
+	case SND_SOC_DAIFMT_I2S:
+		val |= CS4271_MODE1_DAC_DIF_I2S;
+		break;
+	default:
+		dev_err(codec->dev, "Invalid DAI format\n");
+		return -EINVAL;
+	}

Same here.

sound/soc/codecs/cs4271.c line 332

Quoted 2 lines
sound/soc/codecs/cs4271.c new file +2 -0
+/* CS4271 controls */
+static DECLARE_TLV_DB_SCALE(cs4271_dac_tlv, -12700, 100, 0);

Are you use this doesn't mute the DAC? If so the the last parameter should be 1.

Quoted 18 lines
sound/soc/codecs/cs4271.c new file +18 -0
+#ifdef CONFIG_PM
+static int cs4271_soc_suspend(struct snd_soc_codec *codec, pm_message_t mesg)
+{
+	/* Set power-down bit */
+	snd_soc_update_bits(codec, CS4271_MODE2, CS4271_MODE2_PDN,
+		CS4271_MODE2_PDN);
+
+	return 0;
+}
+#else
+#define cs4271_soc_suspend	NULL
+#endif /* CONFIG_PM */
+
+/* This function used also in codec probe function and not only for PM */
+static int cs4271_soc_resume(struct snd_soc_codec *codec)
+{
+	int ret;
+

sound/soc/codecs/cs4271.c line 430

Quoted 10 lines
sound/soc/codecs/cs4271.c new file +10 -0
+	/* Restore codec state */
+	ret = cs4271_write_cache(codec);
+	if (ret < 0)
+		return ret;
+
+	/* then disable the power-down bit */
+	snd_soc_update_bits(codec, CS4271_MODE2, CS4271_MODE2_PDN, 0);
+
+	return 0;
+}

Consider setting the set_bias_level callback and performing the required work in there. You can then trigger a change in the bias levels by calling your cs4271_set_bias_level() function from within your suspend() and resume() functions.

sound/soc/codecs/cs4271.c line 474

Quoted 8 lines
sound/soc/codecs/cs4271.c new file +8 -0
+	/*
+	 * In case of I2C, chip address specified in board data.
+	 * So cache IO operations use 8 bit codec register address.
+	 * In case of SPI, chip address and register address
+	 * passed together as 16 bit value.
+	 * Anyway, register address is masked with 0xFF inside
+	 * soc-cache code.
+	 */

Have you tested your driver using both SPI and I2C?

sound/soc/codecs/cs4271.c line 475

Quoted 1 line
sound/soc/codecs/cs4271.c new file +1 -0
+	ret = (cs4271->bus_type == SND_SOC_SPI) ? 16 : 8;

Please avoid using the ternary operator like this.

sound/soc/codecs/cs4271.c line 484

Quoted 9 lines
sound/soc/codecs/cs4271.c new file +9 -0
+	ret = snd_soc_codec_set_cache_io(codec, ret, 8, cs4271->bus_type);
+	if (ret) {
+		dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
+		return ret;
+	}
+
+	ret = cs4271_soc_resume(codec);
+	if (ret < 0)
+		return ret;

This should also changed to a call to cs4271_set_bias_level(codec, SND_SOC_BIAS_STANDBY);

sound/soc/codecs/cs4271.c line 515

Quoted 1 line
sound/soc/codecs/cs4271.c new file +1 -0
+	.reg_cache_default	= &cs4271_dflt_reg,

No need to use address-of operator here.

sound/soc/codecs/cs4271.c line 519

Quoted 1 line
sound/soc/codecs/cs4271.c new file +1 -0
+	.reg_cache_step		= 1,

No need to initialize reg_cache_step.

Thanks,
Dimitris

author
#2 lore raw

Dear Dimitris,

Quoted 9 lines · Dimitris Papastamos

sound/soc/codecs/cs4271.c line 142

Quoted 5 lines · Alexander
sound/soc/codecs/cs4271.c new file +5 -0
+/* CS4271 default register settings, except auto-mute is off */
+static const u8 cs4271_dflt_reg[CS4271_NR_REGS] = {
+	0, 0, 0, CS4271_DACVOL_ATAPI_AL_BR, 0, 0, 0,
+	CS4271_MODE2_CPEN | CS4271_MODE2_PDN,
+};

I'd be better to leave these as defaults, and perform any initialization in the the cs4271_probe() function.

It's space optimized without affecting readability. I can rename it to cs4271_init_reg[].

Quoted 10 lines

sound/soc/codecs/cs4271.c line 287

Quoted 8 lines
sound/soc/codecs/cs4271.c new file +7 -0
+	/* Configure DAC */
+	val = cs4271_clk_tab[i].speed_mode;
+
+	if (cs4271->master)
+		val |= cs4271_clk_tab[i].mclk_master | CS4271_MODE1_MASTER;
+	else
+		val |= cs4271_clk_tab[i].mclk_slave;

This should ideally live in cs4271_set_dai_fmt().

It's space optimized for particular codec hardware without affecting readability.

Quoted 14 lines

sound/soc/codecs/cs4271.c line 299

Quoted 12 lines
sound/soc/codecs/cs4271.c new file +11 -0
+	switch (cs4271->mode) {
+	case SND_SOC_DAIFMT_LEFT_J:
+		val |= CS4271_MODE1_DAC_DIF_LJ;
+		break;
+	case SND_SOC_DAIFMT_I2S:
+		val |= CS4271_MODE1_DAC_DIF_I2S;
+		break;
+	default:
+		dev_err(codec->dev, "Invalid DAI format\n");
+		return -EINVAL;
+	}

Same here.

Same here.

Quoted 6 lines

sound/soc/codecs/cs4271.c line 332

Quoted 3 lines
sound/soc/codecs/cs4271.c new file +2 -0
+/* CS4271 controls */
+static DECLARE_TLV_DB_SCALE(cs4271_dac_tlv, -12700, 100, 0);

Are you use this doesn't mute the DAC? If so the the last parameter should be 1.

No I do not use this, some people asked for this last time I've tried to submit this code. And no, this doesn't mute the DAC.

Quoted 35 lines
Quoted 19 lines
sound/soc/codecs/cs4271.c new file +18 -0
+#ifdef CONFIG_PM
+static int cs4271_soc_suspend(struct snd_soc_codec *codec, pm_message_t mesg)
+{
+	/* Set power-down bit */
+	snd_soc_update_bits(codec, CS4271_MODE2, CS4271_MODE2_PDN,
+		CS4271_MODE2_PDN);
+
+	return 0;
+}
+#else
+#define cs4271_soc_suspend	NULL
+#endif /* CONFIG_PM */
+
+/* This function used also in codec probe function and not only for PM */
+static int cs4271_soc_resume(struct snd_soc_codec *codec)
+{
+	int ret;
+

sound/soc/codecs/cs4271.c line 430

Quoted 10 lines
sound/soc/codecs/cs4271.c new file +10 -0
+	/* Restore codec state */
+	ret = cs4271_write_cache(codec);
+	if (ret < 0)
+		return ret;
+
+	/* then disable the power-down bit */
+	snd_soc_update_bits(codec, CS4271_MODE2, CS4271_MODE2_PDN, 0);
+
+	return 0;
+}

Consider setting the set_bias_level callback and performing the required work in there. You can then trigger a change in the bias levels by calling your cs4271_set_bias_level() function from within your suspend() and resume() functions.

Quoted 10 lines

sound/soc/codecs/cs4271.c line 474

Quoted 8 lines
sound/soc/codecs/cs4271.c new file +8 -0
+	/*
+	 * In case of I2C, chip address specified in board data.
+	 * So cache IO operations use 8 bit codec register address.
+	 * In case of SPI, chip address and register address
+	 * passed together as 16 bit value.
+	 * Anyway, register address is masked with 0xFF inside
+	 * soc-cache code.
+	 */

Have you tested your driver using both SPI and I2C?

Yes.

Quoted 4 lines

sound/soc/codecs/cs4271.c line 475

Quoted 2 lines
sound/soc/codecs/cs4271.c new file +1 -0
+	ret = (cs4271->bus_type == SND_SOC_SPI) ? 16 : 8;

Please avoid using the ternary operator like this.

What's wrong with it?

Quoted 14 lines

sound/soc/codecs/cs4271.c line 484

Quoted 10 lines
sound/soc/codecs/cs4271.c new file +9 -0
+	ret = snd_soc_codec_set_cache_io(codec, ret, 8, cs4271->bus_type);
+	if (ret) {
+		dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
+		return ret;
+	}
+
+	ret = cs4271_soc_resume(codec);
+	if (ret < 0)
+		return ret;

This should also changed to a call to cs4271_set_bias_level(codec, SND_SOC_BIAS_STANDBY);

There is no need for DAPM on hardware I consider. I can even remove PM stuff. Someone may implement it later, if CS4271 would appear on something except Cirrus dev. boards.

Quoted 4 lines

Thanks,
Dimitris

Thanks,
Alexander.

#3 lore raw
Quoted 12 lines · Alexander
Quoted 9 lines · Dimitris Papastamos

sound/soc/codecs/cs4271.c line 142

Quoted 5 lines · Alexander
sound/soc/codecs/cs4271.c new file +5 -0
+/* CS4271 default register settings, except auto-mute is off */
+static const u8 cs4271_dflt_reg[CS4271_NR_REGS] = {
+	0, 0, 0, CS4271_DACVOL_ATAPI_AL_BR, 0, 0, 0,
+	CS4271_MODE2_CPEN | CS4271_MODE2_PDN,
+};

I'd be better to leave these as defaults, and perform any initialization in the the cs4271_probe() function.

Quoted 2 lines

It's space optimized without affecting readability. I can rename it to cs4271_init_reg[].

You're missing Dimitris' point here. There's an expectation that the register defaults will be the physical register defaults, if you need to change them do it with an explicit write during startup.

There's several other places where you're responding to review with similar comments about space optimisation which don't really seem to engage at all with what Dimitris has said.

Quoted 8 lines
Quoted 5 lines

sound/soc/codecs/cs4271.c line 332

Quoted 2 lines
sound/soc/codecs/cs4271.c new file +2 -0
+/* CS4271 controls */
+static DECLARE_TLV_DB_SCALE(cs4271_dac_tlv, -12700, 100, 0);

Are you use this doesn't mute the DAC? If so the the last parameter should be 1.

No I do not use this, some people asked for this last time I've tried to submit this code. And no, this doesn't mute the DAC.

Then again -127dB is more attenuation than many actual mutes.

Quoted 5 lines
Quoted 3 lines

sound/soc/codecs/cs4271.c line 475

Quoted 1 line
sound/soc/codecs/cs4271.c new file +1 -0
+	ret = (cs4271->bus_type == SND_SOC_SPI) ? 16 : 8;

Please avoid using the ternary operator like this.

What's wrong with it?

It's not great for legibility.