Skip to contentSkip to conversation

ASoC: add RT5670 CODEC driver

PATCH In review [email protected] 2 participants 4 messages Dec 16, 2013 – Jan 9, 2014

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

PATCH 1/1 ASoC: add RT5670 CODEC driver

From: Bard Liao <[email protected]>

---
This is the initial codec driver of ALC5670
I have tested playback to Headphone, Line out and PDM interface. But not any capture path.

---
 include/sound/rt5670.h        |   20 +
 sound/soc/codecs/Kconfig      |    4 +
 sound/soc/codecs/Makefile     |    2 +
 sound/soc/codecs/rt5670-dsp.c |  817 +++++++++++
 sound/soc/codecs/rt5670-dsp.h |   69 +
 sound/soc/codecs/rt5670.c     | 3053 +++++++++++++++++++++++++++++++++++++++++
 sound/soc/codecs/rt5670.h     | 1966 ++++++++++++++++++++++++++
 7 files changed, 5931 insertions(+)
 create mode 100644 include/sound/rt5670.h
 create mode 100644 sound/soc/codecs/rt5670-dsp.c
 create mode 100644 sound/soc/codecs/rt5670-dsp.h
 create mode 100644 sound/soc/codecs/rt5670.c
 create mode 100644 sound/soc/codecs/rt5670.h
include/sound/rt5670.h new file +20 -0
new file mode 100644
index 0000000..2397fb8
@@ -0,0 +1,20 @@
+/*
+ * linux/sound/rt5670.h -- Platform data for RT5670
+ *
+ * Copyright 2011 Realtek Microelectronics
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef __LINUX_SND_RT5670_H
+#define __LINUX_SND_RT5670_H
+
+struct rt5670_platform_data {
+	bool asrc_en;
+	bool jd1_en;
+	bool in2_diff;
+};
+
+#endif
sound/soc/codecs/Kconfig +4 -0
index 983d087a..4400433 100644
@@ -61,6 +61,7 @@ config SND_SOC_ALL_CODECS
 	select SND_SOC_PCM3008
 	select SND_SOC_RT5631 if I2C
 	select SND_SOC_RT5640 if I2C
+	select SND_SOC_RT5670 if I2C
 	select SND_SOC_SGTL5000 if I2C
 	select SND_SOC_SI476X if MFD_SI476X_CORE
 	select SND_SOC_SN95031 if INTEL_SCU_IPC
@@ -319,6 +320,9 @@ config SND_SOC_RT5631
 config SND_SOC_RT5640
 	tristate
 
+config SND_SOC_RT5670
+	tristate
+
 #Freescale sgtl5000 codec
 config SND_SOC_SGTL5000
 	tristate
sound/soc/codecs/Makefile +2 -0
index bc12676..f95e65a 100644
@@ -48,6 +48,7 @@ snd-soc-pcm1792a-codec-objs := pcm1792a.o
 snd-soc-pcm3008-objs := pcm3008.o
 snd-soc-rt5631-objs := rt5631.o
 snd-soc-rt5640-objs := rt5640.o
+snd-soc-rt5670-objs := rt5670.o rt5670-dsp.o
 snd-soc-sgtl5000-objs := sgtl5000.o
 snd-soc-alc5623-objs := alc5623.o
 snd-soc-alc5632-objs := alc5632.o
@@ -181,6 +182,7 @@ obj-$(CONFIG_SND_SOC_PCM1792A)	+= snd-soc-pcm1792a-codec.o
 obj-$(CONFIG_SND_SOC_PCM3008)	+= snd-soc-pcm3008.o
 obj-$(CONFIG_SND_SOC_RT5631)	+= snd-soc-rt5631.o
 obj-$(CONFIG_SND_SOC_RT5640)	+= snd-soc-rt5640.o
+obj-$(CONFIG_SND_SOC_RT5670)	+= snd-soc-rt5670.o
 obj-$(CONFIG_SND_SOC_SGTL5000)  += snd-soc-sgtl5000.o
 obj-$(CONFIG_SND_SOC_SIGMADSP)	+= snd-soc-sigmadsp.o
 obj-$(CONFIG_SND_SOC_SI476X)	+= snd-soc-si476x.o
sound/soc/codecs/rt5670-dsp.c new file +817 -0

817 lines not on this page · raw message

sound/soc/codecs/rt5670-dsp.h new file +69 -0

69 lines not on this page · raw message

sound/soc/codecs/rt5670.c new file +3053 -0

2,064 lines not on this page · raw message

sound/soc/codecs/rt5670.h new file +1966 -0

1,966 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.8.1.1.439.g50a6b54

Part of the diff is not shown here. View the raw message.

maintainer
#1 lore raw
Quoted 1 line · On Mon, Dec 16, 2013 at 08:11:56PM +0800, [email protected] wrote:

From: Bard Liao <[email protected]>

This patch is far too big, it really needs to be split up into a patch series to make it reviewable. I'd suggest at least doing something like splitting it into one patch adding the CODEC functionality and then a second patch adding the DSP support. Probably the header ought to be a separate patch too.

Overall this looks reasonable on the CODEC side, that can probably get merged relatively easily though some revisions are needed. The DSP code is less clear partly due to lack of documentation.

sound/soc/codecs/rt5670-dsp.c line 32

Quoted 10 lines
sound/soc/codecs/rt5670-dsp.c new file +10 -0
+/* DSP init */
+static unsigned short rt5670_dsp_init[][2] = {
+	{0x2260, 0x30d9}, {0x2261, 0x30d9}, {0x2289, 0x7fff}, {0x2290, 0x7fff},
+	{0x2288, 0x0002}, {0x22b2, 0x0002}, {0x2295, 0x0001}, {0x22b3, 0x0001},
+	{0x22d7, 0x0008}, {0x22d8, 0x0009}, {0x22d9, 0x0000}, {0x22da, 0x0001},
+	{0x22fd, 0x009e}, {0x22c1, 0x1006}, {0x22c2, 0x1006}, {0x22c3, 0x1007},
+	{0x22c4, 0x1007}
+};
+#define RT5670_DSP_INIT_NUM \
+	(sizeof(rt5670_dsp_init) / sizeof(rt5670_dsp_init[0]))

Use ARRAY_SIZE() rather than open coding it.

sound/soc/codecs/rt5670-dsp.c line 164

Quoted 10 lines
sound/soc/codecs/rt5670-dsp.c new file +10 -0
+static unsigned short rt5670_dsp_2mic_handset[][2] = {
+	{0x22f8, 0x8002}, {0x2301, 0x0002}, {0x2302, 0x0002}, {0x2303, 0x0710},
+	{0x2304, 0x4332}, {0x2305, 0x206c}, {0x236e, 0x0000}, {0x236f, 0x0001},
+	{0x237e, 0x0001}, {0x237f, 0x3800}, {0x2380, 0x3000}, {0x2381, 0x0005},
+	{0x2382, 0x0040}, {0x2383, 0x7fff}, {0x2388, 0x2c00}, {0x2389, 0x2800},
+	{0x238b, 0x1800}, {0x238c, 0x1800}, {0x238f, 0x2000}, {0x239b, 0x0002},
+	{0x239c, 0x0a00}, {0x239f, 0x0001}, {0x230c, 0x0200}, {0x22fb, 0x0000}
+};
+#define RT5670_DSP_2MIC_HANDSET_NUM \
+	(sizeof(rt5670_dsp_2mic_handset) / sizeof(rt5670_dsp_2mic_handset[0]))

What are all these things? We shouldn't have huge tables of magic numbers in the kernel...

Quoted 9 lines
sound/soc/codecs/rt5670-dsp.c new file +9 -0
+/**
+ * rt5670_dsp_done - Wait until DSP is ready.
+ * @codec: SoC Audio Codec device.
+ *
+ * To check voice DSP status and confirm it's ready for next work.
+ *
+ * Returns 0 for success or negative error code.
+ */
+static int rt5670_dsp_done(struct snd_soc_codec *codec)

sound/soc/codecs/rt5670-dsp.c line 214

Quoted 10 lines
sound/soc/codecs/rt5670-dsp.c new file +10 -0
+{
+	unsigned int count = 0, dsp_val;
+
+	dsp_val = snd_soc_read(codec, RT5670_DSP_CTRL1);
+	while (dsp_val & RT5670_DSP_BUSY_MASK) {
+		if (count > 10)
+			return -EBUSY;
+		dsp_val = snd_soc_read(codec, RT5670_DSP_CTRL1);
+		count++;
+	}

A do { } while loop would be more idiomatic. Or a for loop with a return.

sound/soc/codecs/rt5670-dsp.c line 231

Quoted 2 lines
sound/soc/codecs/rt5670-dsp.c new file +2 -0
+static int rt5670_dsp_write(struct snd_soc_codec *codec,
+		unsigned int addr, unsigned int data)

This probably wants to be a regmap using the custom read and write callbacks.

sound/soc/codecs/rt5670-dsp.c line 385

Quoted 11 lines
sound/soc/codecs/rt5670-dsp.c new file +11 -0
+static int rt5670_dsp_put(struct snd_kcontrol *kcontrol,
+		struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
+	struct rt5670_priv *rt5670 = snd_soc_codec_get_drvdata(codec);
+
+	if (rt5670->dsp_sw != ucontrol->value.integer.value[0])
+		rt5670->dsp_sw = ucontrol->value.integer.value[0];
+
+	return 0;
+}

I would expect this to be doing something when it sets the value, possibly just returning an error if the DSP is running and this can't be reprogrammed but doing something to interact with the rest of the system.

sound/soc/codecs/rt5670-dsp.c line 419

Quoted 3 lines
sound/soc/codecs/rt5670-dsp.c new file +3 -0
+static int rt5670_dsp_rate_put(struct snd_kcontrol *kcontrol,
+		struct snd_ctl_elem_value *ucontrol)
+{

Why can't this be configured automatically by the device?

sound/soc/codecs/rt5670-dsp.c line 483

Quoted 3 lines
sound/soc/codecs/rt5670-dsp.c new file +3 -0
+static const struct snd_kcontrol_new rt5670_dsp_snd_controls[] = {
+	SOC_ENUM("SRC for RxDP", rt5670_src_rxdp_enum),
+	SOC_ENUM("SRC for TxDP", rt5670_src_txdp_enum),

These should be done with DAPM.

sound/soc/codecs/rt5670-dsp.c line 628

Quoted 2 lines
sound/soc/codecs/rt5670-dsp.c new file +2 -0
+	case RT5670_DSP_AEC:
+		dev_info(codec->dev, "AEC\n");

dev_dbg().

sound/soc/codecs/rt5670-dsp.c line 719

Quoted 7 lines
sound/soc/codecs/rt5670-dsp.c new file +7 -0
+	ret = rt5670_dsp_rate(codec,
+		rt5670->sysclk ?
+		rt5670->sysclk : 11289600,
+		rt5670->dsp_rate ?
+		rt5670->dsp_rate : 16000);
+	if (ret < 0)
+		goto effect_err;

Don't use the ternery operator like this, just set defaults on init.

sound/soc/codecs/rt5670.c line 213

Quoted 4 lines
sound/soc/codecs/rt5670.c new file +3 -0
+unsigned rt5670_pdm1_read(struct snd_soc_codec *codec, unsigned int reg)
+{
 
+EXPORT_SYMBOL(rt5670_pdm1_read);

Why is this exported at all and why is it exported non-GPL?

sound/soc/codecs/rt5670.c line 653

Quoted 12 lines
sound/soc/codecs/rt5670.c new file +12 -0
+static int check_sysclk1_source(struct snd_soc_dapm_widget *source,
+			 struct snd_soc_dapm_widget *sink)
+{
+	unsigned int val;
+
+	val = snd_soc_read(source->codec, RT5670_GLB_CLK);
+	val &= RT5670_SCLK_SRC_MASK;
+	if (val == RT5670_SCLK_SRC_PLL1)
+		return 1;
+	else
+		return 0;
+}

The naming of this function is non-obvious... it looks like it's actually checking that the chip is using the PLL.

Quoted 5 lines
sound/soc/codecs/rt5670.c new file +5 -0
+static int rt5670_mono_adcl_event(struct snd_soc_dapm_widget *w,
+	struct snd_kcontrol *kcontrol, int event)
+{
+	struct snd_soc_codec *codec = w->codec;
+

sound/soc/codecs/rt5670.c line 1309

Quoted 10 lines
sound/soc/codecs/rt5670.c new file +10 -0
+	switch (event) {
+	case SND_SOC_DAPM_POST_PMU:
+		snd_soc_update_bits(codec, RT5670_MONO_ADC_DIG_VOL,
+			RT5670_L_MUTE, 0);
+		break;
+	case SND_SOC_DAPM_PRE_PMD:
+		snd_soc_update_bits(codec, RT5670_MONO_ADC_DIG_VOL,
+			RT5670_L_MUTE,
+			RT5670_L_MUTE);
+		break;

Most of these events look like they should probably be using DAPM _AUTODISABLE controls, this would save a lot of code. If they can't do that for some reason the functionality should definitely be factored out into the core.

Quoted 7 lines
sound/soc/codecs/rt5670.c new file +7 -0
+static int rt5670_asrc_event(struct snd_soc_dapm_widget *w,
+	struct snd_kcontrol *kcontrol, int event)
+{
+
+	switch (event) {
+	case SND_SOC_DAPM_POST_PMU:
+		snd_soc_write(w->codec, RT5670_ASRC_1, 0xffff);

sound/soc/codecs/rt5670.c line 1629

Quoted 10 lines
sound/soc/codecs/rt5670.c new file +10 -0
+		snd_soc_write(w->codec, RT5670_ASRC_2, 0x1221);
+		snd_soc_write(w->codec, RT5670_ASRC_3, 0x0022);
+		break;
+	case SND_SOC_DAPM_PRE_PMD:
+		snd_soc_write(w->codec, RT5670_ASRC_1, 0);
+		snd_soc_write(w->codec, RT5670_ASRC_2, 0);
+		snd_soc_write(w->codec, RT5670_ASRC_3, 0);
+	default:
+		return 0;
+	}

This looks an awful lot like it might be writing some use case dependant parameters... since it's just uncommented magic numbers it's hard to tell but there's no obvious configuration for the ASRC anywhere in the driver.

sound/soc/codecs/rt5670.c line 2388

Quoted 4 lines
sound/soc/codecs/rt5670.c new file +4 -0
+	if (RT5670_AIF2 == dai->id) {
+		snd_soc_update_bits(codec, RT5670_GPIO_CTRL1,
+			RT5670_I2S2_PIN_MASK, RT5670_I2S2_PIN_I2S);
+	}

This looks like it could be done with DAPM, it's just setting this bit while the AIF is in use.

sound/soc/codecs/rt5670.c line 2807

Quoted 6 lines
sound/soc/codecs/rt5670.c new file +6 -0
+	if (rt5670->pdata.asrc_en) {
+		snd_soc_dapm_new_controls(&codec->dapm, rt5670_asrc_widgets,
+				ARRAY_SIZE(rt5670_dapm_widgets));
+		snd_soc_dapm_add_routes(&codec->dapm, rt5670_asrc_routes,
+				ARRAY_SIZE(rt5670_dapm_routes));
+	}

Why is this configured in platform data?

sound/soc/codecs/rt5670.c line 2823

Quoted 6 lines
sound/soc/codecs/rt5670.c new file +6 -0
+	if (rt5670->pdata.jd1_en) {
+		regmap_update_bits(rt5670->regmap,
+			RT5670_IRQ_CTRL2, 0x200, 0x200);
+		snd_soc_dapm_force_enable_pin(&codec->dapm, "JD Power");
+		snd_soc_dapm_sync(&codec->dapm);
+	}

This should use the standard jack detection APIs.

sound/soc/codecs/rt5670.c line 3026

Quoted 8 lines
sound/soc/codecs/rt5670.c new file +8 -0
+void rt5670_i2c_shutdown(struct i2c_client *client)
+{
+	struct rt5670_priv *rt5670 = i2c_get_clientdata(client);
+	struct snd_soc_codec *codec = rt5670->codec;
+
+	if (codec != NULL)
+		rt5670_set_bias_level(codec, SND_SOC_BIAS_OFF);
+}

The core will do this for you, just remove it.

sound/soc/codecs/rt5670.c line 3043

Quoted 5 lines
sound/soc/codecs/rt5670.c new file +5 -0
+static int __init rt5670_modinit(void)
+{
+	return i2c_add_driver(&rt5670_i2c_driver);
+}
+module_init(rt5670_modinit);

module_i2c_driver().

author
#2 lore raw
Quoted 7 lines

-----Original Message-----
From: Mark Brown [mailto:[email protected]]
Sent: Thursday, December 19, 2013 8:25 PM
To: Bard Liao
Cc: [email protected]; [email protected]; Flove; Oder Chiou
Subject: Re: [PATCH] ASoC: add RT5670 CODEC driver

Quoted 7 lines
Quoted 1 line · On Mon, Dec 16, 2013 at 08:11:56PM +0800, [email protected] wrote:

From: Bard Liao <[email protected]>

This patch is far too big, it really needs to be split up into a patch series to make it reviewable. I'd suggest at least doing something like splitting it into one patch adding the CODEC functionality and then a second patch adding the DSP support. Probably the header ought to be a separate patch too.

I will send the audio function part first.

Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +3 -0
+unsigned rt5670_pdm1_read(struct snd_soc_codec *codec, unsigned int
+reg) {
 
+EXPORT_SYMBOL(rt5670_pdm1_read);

Why is this exported at all and why is it exported non-GPL?

If a pdm device connect to RT5670, it may need to be controlled by RT5670's pdm read/write commands. I will export it with GPL.

Quoted 12 lines
Quoted 12 lines
sound/soc/codecs/rt5670.c new file +12 -0
+static int rt5670_asrc_event(struct snd_soc_dapm_widget *w,
+	struct snd_kcontrol *kcontrol, int event) {
+
+	switch (event) {
+	case SND_SOC_DAPM_POST_PMU:
+		snd_soc_write(w->codec, RT5670_ASRC_1, 0xffff);
+		snd_soc_write(w->codec, RT5670_ASRC_2, 0x1221);
+		snd_soc_write(w->codec, RT5670_ASRC_3, 0x0022);
+		break;
+	case SND_SOC_DAPM_PRE_PMD:
+		snd_soc_write(w->codec, RT5670_ASRC_1, 0);
+		snd_soc_write(w->codec, RT5670_ASRC_2, 0);
Quoted 8 lines

sound/soc/codecs/rt5670.c line 1629

Quoted 4 lines
sound/soc/codecs/rt5670.c new file +4 -0
+		snd_soc_write(w->codec, RT5670_ASRC_3, 0);
+	default:
+		return 0;
+	}

This looks an awful lot like it might be writing some use case dependant parameters... since it's just uncommented magic numbers it's hard to tell but there's no obvious configuration for the ASRC anywhere in the driver.

I will add control to configure ASRC's parameters.

Quoted 8 lines

sound/soc/codecs/rt5670.c line 2807

Quoted 6 lines
sound/soc/codecs/rt5670.c new file +6 -0
+	if (rt5670->pdata.asrc_en) {
+		snd_soc_dapm_new_controls(&codec->dapm, rt5670_asrc_widgets,
+				ARRAY_SIZE(rt5670_dapm_widgets));
+		snd_soc_dapm_add_routes(&codec->dapm, rt5670_asrc_routes,
+				ARRAY_SIZE(rt5670_dapm_routes));
+	}

Why is this configured in platform data?

Usually, we don't enable ASRC function.
But in some platforms, they will need to enable ASRC function to get clock properly. And I think this is not needed to configure in run time.

Thanks.

maintainer
#3 lore raw
Quoted 9 lines · Bard Liao
Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +3 -0
+unsigned rt5670_pdm1_read(struct snd_soc_codec *codec, unsigned int
+reg) {
 
+EXPORT_SYMBOL(rt5670_pdm1_read);

Why is this exported at all and why is it exported non-GPL?

If a pdm device connect to RT5670, it may need to be controlled by RT5670's pdm read/write commands. I will export it with GPL.

Please provide more detail on this. Why would a generic PDM device have specific hooks into this driver?

Quoted 9 lines
Quoted 8 lines

sound/soc/codecs/rt5670.c line 2807

Quoted 6 lines
sound/soc/codecs/rt5670.c new file +6 -0
+	if (rt5670->pdata.asrc_en) {
+		snd_soc_dapm_new_controls(&codec->dapm, rt5670_asrc_widgets,
+				ARRAY_SIZE(rt5670_dapm_widgets));
+		snd_soc_dapm_add_routes(&codec->dapm, rt5670_asrc_routes,
+				ARRAY_SIZE(rt5670_dapm_routes));
+	}

Why is this configured in platform data?

Quoted 3 lines

Usually, we don't enable ASRC function.
But in some platforms, they will need to enable ASRC function to get clock properly. And I think this is not needed to configure in run time.

That's not going to be true in the general case, for example systems that support both narrow band and wide band might switch between them.