Skip to contentSkip to conversation

ASoC: rt5668: add rt5668 codec driver

PATCH In review John Lin 2 participants 3 messages May 9, 2016 – Jun 20, 2016 1 message not in the archive

Subsystems (4)
lore
Share & tools
lore

John Lin

author
#0 lore raw

PATCH 1/1 ASoC: rt5668: add rt5668 codec driver

This is the initial codec driver for rt5668.

---
 Documentation/devicetree/bindings/sound/rt5668.txt |   28 +
 sound/soc/codecs/Kconfig                           |    6 +
 sound/soc/codecs/Makefile                          |    2 +
 sound/soc/codecs/rt5668.c                          | 3280 ++++++++++++++++++++
 sound/soc/codecs/rt5668.h                          |  781 +++++
 5 files changed, 4097 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/sound/rt5668.txt
 create mode 100644 sound/soc/codecs/rt5668.c
 create mode 100644 sound/soc/codecs/rt5668.h
Documentation/devicetree/bindings/sound/rt5668.txt new file +28 -0
new file mode 100644
index 0000000..50d7759
@@ -0,0 +1,28 @@
+RT5668/RT5663 audio CODECs
+
+These devices support I2C only.
+
+Required properties:
+
+- compatible : "realtek,rt5668" or "realtek,rt5663"
+
+- reg : The I2C address of the device.
+
+- interrupts : The CODEC's interrupt output.
+
+Pins on the device (for linking into audio routes) for RT5668/RT5663:
+
+  * IN1P
+  * IN1N
+  * IN2P
+  * IN2N
+  * HPOL
+  * HPOR
+
+Example:
+
+codec: rt5668@1a {
+	compatible = "realtek,rt5668";
+	reg = <0x1a>;
+	interrupts = <347>;
+};
sound/soc/codecs/Kconfig +6 -0
index ac7317e..283c4bd 100644
@@ -107,6 +107,7 @@ config SND_SOC_ALL_CODECS
 	select SND_SOC_RT5645 if I2C
 	select SND_SOC_RT5651 if I2C
 	select SND_SOC_RT5659 if I2C
+	select SND_SOC_RT5668 if I2C
 	select SND_SOC_RT5670 if I2C
 	select SND_SOC_RT5677 if I2C && SPI_MASTER
 	select SND_SOC_SGTL5000 if I2C
@@ -610,6 +611,7 @@ config SND_SOC_RL6231
 	default y if SND_SOC_RT5645=y
 	default y if SND_SOC_RT5651=y
 	default y if SND_SOC_RT5659=y
+	default y if SND_SOC_RT5668=y
 	default y if SND_SOC_RT5670=y
 	default y if SND_SOC_RT5677=y
 	default m if SND_SOC_RT5514=m
@@ -618,6 +620,7 @@ config SND_SOC_RL6231
 	default m if SND_SOC_RT5645=m
 	default m if SND_SOC_RT5651=m
 	default m if SND_SOC_RT5659=m
+	default m if SND_SOC_RT5668=m
 	default m if SND_SOC_RT5670=m
 	default m if SND_SOC_RT5677=m
 
@@ -660,6 +663,9 @@ config SND_SOC_RT5651
 config SND_SOC_RT5659
 	tristate
 
+config SND_SOC_RT5668
+	tristate
+
 config SND_SOC_RT5670
 	tristate
 
sound/soc/codecs/Makefile +2 -0
index 94873ee..88b16d9 100644
@@ -105,6 +105,7 @@ snd-soc-rt5640-objs := rt5640.o
 snd-soc-rt5645-objs := rt5645.o
 snd-soc-rt5651-objs := rt5651.o
 snd-soc-rt5659-objs := rt5659.o
+snd-soc-rt5668-objs := rt5668.o
 snd-soc-rt5670-objs := rt5670.o
 snd-soc-rt5677-objs := rt5677.o
 snd-soc-rt5677-spi-objs := rt5677-spi.o
@@ -318,6 +319,7 @@ obj-$(CONFIG_SND_SOC_RT5640)	+= snd-soc-rt5640.o
 obj-$(CONFIG_SND_SOC_RT5645)	+= snd-soc-rt5645.o
 obj-$(CONFIG_SND_SOC_RT5651)	+= snd-soc-rt5651.o
 obj-$(CONFIG_SND_SOC_RT5659)	+= snd-soc-rt5659.o
+obj-$(CONFIG_SND_SOC_RT5668)	+= snd-soc-rt5668.o
 obj-$(CONFIG_SND_SOC_RT5670)	+= snd-soc-rt5670.o
 obj-$(CONFIG_SND_SOC_RT5677)	+= snd-soc-rt5677.o
 obj-$(CONFIG_SND_SOC_RT5677_SPI)	+= snd-soc-rt5677-spi.o
sound/soc/codecs/rt5668.c new file +3280 -0

2,928 lines not on this page · raw message

sound/soc/codecs/rt5668.h new file +781 -0

781 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

sound/soc/codecs/rt5668.c line 725

Quoted 6 lines · John Lin
sound/soc/codecs/rt5668.c new file +6 -0
+static bool rt5668_volatile_register(struct device *dev, unsigned int reg)
+{
+	switch (reg) {
+	case 0x0000:
+	case 0x0011:
+	case 0x0082:

I'm not a big fan of all the magic numbers for the registers here, it's OK if not ideal for the defaults tables since I can understand that they're probably generated but here it's making things harder to read and maintain.

sound/soc/codecs/rt5668.c line 1495

Quoted 4 lines
sound/soc/codecs/rt5668.c new file +4 -0
+static void rt5668_enable_push_button_irq(struct snd_soc_codec *codec,
+	bool enable)
+{
+	struct rt5668_priv *rt5668 = snd_soc_codec_get_drvdata(codec);

This is two separate functions, there's nothing shared between the enable and disable paths except the private data structure here.

sound/soc/codecs/rt5668.c line 1514

Quoted 8 lines
sound/soc/codecs/rt5668.c new file +8 -0
+		if (rt5668->codec_type == CODEC_TYPE_RT5668)
+			snd_soc_update_bits(codec, RT5668_IRQ_3,
+				RT5668_EN_IRQ_INLINE_MASK,
+				RT5668_EN_IRQ_INLINE_NOR);
+		else
+			snd_soc_update_bits(codec, RT5668_IRQ_2,
+				RT5663_EN_IRQ_INLINE_MASK,
+				RT5663_EN_IRQ_INLINE_NOR);

Write this as a switch statement so future variants can be more easily handled. This is a common pattern in this driver.

sound/soc/codecs/rt5668.c line 1620

Quoted 3 lines

+ */

sound/soc/codecs/rt5668.c new file +2 -0
+
+static int rt5663_jack_detect(struct snd_soc_codec *codec, int jack_insert)

Extra blank line.

Quoted 11 lines
sound/soc/codecs/rt5668.c new file +11 -0
+{
+	struct rt5668_priv *rt5668 = snd_soc_codec_get_drvdata(codec);
+	int val, i = 0, sleep_time[5] = {300, 150, 100, 50, 30};
+
+	dev_dbg(codec->dev, "%s jack_insert:%d\n", __func__, jack_insert);
+
+	if (jack_insert) {
+		while (i < 5) {
+			msleep(sleep_time[i]);
+			val = snd_soc_read(codec, RT5663_EM_JACK_TYPE_2) &
+				0x0003;

sound/soc/codecs/rt5668.c line 1641

Quoted 10 lines
sound/soc/codecs/rt5668.c new file +10 -0
+			i++;
+			if (val == 0x1 || val == 0x2 || val == 0x3)
+				break;
+			dev_dbg(codec->dev, "%s: MX-00e7 val=%x sleep %d\n",
+				__func__, val, sleep_time[i]);
+		}
+		dev_dbg(codec->dev, "%s val = %d\n", __func__, val);
+		switch (val) {
+		case 1:
+		case 2:

This looks like the same code as was in the detection handler, shouldn't it be shared?

sound/soc/codecs/rt5668.c line 1670

Quoted 12 lines
sound/soc/codecs/rt5668.c new file +12 -0
+int rt5668_button_detect(struct snd_soc_codec *codec)
+{
+	int btn_type, val;
+
+	val = snd_soc_read(codec, RT5668_4BTN_IL_CMD_1);
+	dev_dbg(codec->dev, "%s: val=0x%x\n", __func__, val);
+	btn_type = val & 0xfff0;
+	snd_soc_write(codec, RT5668_4BTN_IL_CMD_1, val);
+
+	return btn_type;
+}
+EXPORT_SYMBOL(rt5668_button_detect);

What is this and why is it exported? Note also that all ASoC and regmap exports are EXPORT_SYMBOL_GPL()...

sound/soc/codecs/rt5668.c line 1682

Quoted 11 lines
sound/soc/codecs/rt5668.c new file +11 -0
+static irqreturn_t rt5668_irq(int irq, void *data)
+{
+	struct rt5668_priv *rt5668 = data;
+
+	dev_dbg(rt5668->codec->dev, "%s IRQ queue work\n", __func__);
+
+	queue_delayed_work(system_wq, &rt5668->jack_detect_work,
+		msecs_to_jiffies(250));
+
+	return IRQ_HANDLED;
+}

Given that the interrupt handler just schedules a work item there's no need for it to be a threaded interrupt, it can just be a normal interrupt and save the bother of masking the hardware interrupt and running a thread.

sound/soc/codecs/rt5668.c line 1703

Quoted 12 lines
sound/soc/codecs/rt5668.c new file +12 -0
+int rt5668_set_jack_detect(struct snd_soc_codec *codec,
+	struct snd_soc_jack *hs_jack)
+{
+	struct rt5668_priv *rt5668 = snd_soc_codec_get_drvdata(codec);
+
+	rt5668->hs_jack = hs_jack;
+
+	rt5668_irq(0, rt5668);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(rt5668_set_jack_detect);

We don't need to do anything in the hardware to enable jack detection?

sound/soc/codecs/rt5668.c line 2761

Quoted 9 lines
sound/soc/codecs/rt5668.c new file +9 -0
+static int rt5668_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
+{
+	struct snd_soc_codec *codec = dai->codec;
+	struct rt5668_priv *rt5668 = snd_soc_codec_get_drvdata(codec);
+
+	dev_dbg(codec->dev, "%s ratio = %d\n", __func__, ratio);
+
+	if (rt5668->codec_type == CODEC_TYPE_RT5663) {
+		switch (ratio) {

No handling for the other CODEC? Should it return an error?

sound/soc/codecs/rt5668.c line 2907

Quoted 6 lines
sound/soc/codecs/rt5668.c new file +6 -0
+static int rt5668_resume(struct snd_soc_codec *codec)
+{
+	struct rt5668_priv *rt5668 = snd_soc_codec_get_drvdata(codec);
+
+	regcache_cache_only(rt5668->regmap, false);
+	regcache_sync(rt5668->regmap);

Should check the return code.

sound/soc/codecs/rt5668.c line 3142

Quoted 10 lines
sound/soc/codecs/rt5668.c new file +10 -0
+	/* reset and calibrate */
+	regmap_write(rt5668->regmap, RT5668_RESET, 0);
+	regcache_cache_bypass(rt5668->regmap, true);
+	if (rt5668->codec_type == CODEC_TYPE_RT5668)
+		rt5668_calibrate(rt5668);
+	else
+		rt5663_calibrate(rt5668);
+	regcache_cache_bypass(rt5668->regmap, false);
+	regmap_write(rt5668->regmap, RT5668_RESET, 0);
+	dev_dbg(&i2c->dev, "calibrate done\n");

Do we not need to do this on resume?

sound/soc/codecs/rt5668.c line 3198

Quoted 7 lines
sound/soc/codecs/rt5668.c new file +7 -0
+		/* BST1 power on for JD */
+		regmap_update_bits(rt5668->regmap, RT5668_PWR_ANLG_2,
+			RT5668_PWR_BST1_MASK, RT5668_PWR_BST1_ON);
+		regmap_update_bits(rt5668->regmap, RT5663_EM_JACK_TYPE_1,
+			RT5663_CBJ_DET_MASK | RT5663_EXT_JD_MASK |
+			RT5663_POL_EXT_JD_MASK, RT5663_CBJ_DET_EN |
+			RT5663_EXT_JD_EN | RT5663_POL_EXT_JD_EN);

I'd expect any enables for jack detection to be done only if the system is actually using jack detection.

sound/soc/codecs/rt5668.c line 3218

Quoted 7 lines
sound/soc/codecs/rt5668.c new file +7 -0
+		/* RECMIX */
+		regmap_update_bits(rt5668->regmap, RT5663_RECMIX,
+			RT5663_RECMIX1_BST1_MASK, RT5663_RECMIX1_BST1_UNMUTE);
+		/* ADCDAT L/L */
+		regmap_update_bits(rt5668->regmap, RT5668_TDM_1,
+			RT5663_DATA_SWAP_ADCDAT1_MASK,
+			RT5663_DATA_SWAP_ADCDAT1_LL);

This looks like routing control?

sound/soc/codecs/rt5668.c line 3222

Quoted 2 lines
sound/soc/codecs/rt5668.c new file +2 -0
+	setup_timer(&rt5668->btn_check_timer, rt5668_btn_check_callback,
+		(unsigned long)rt5668);

Does this really need to be running *all* the time?

A message that is not in the archive (sent off-list or not yet received): [email protected]

maintainer
#3 lore raw
Quoted 7 lines · Jack Yu

[Mark] Given that the interrupt handler just schedules a work item there's no need for it to be a threaded interrupt, it can just be a normal interrupt and save the bother of masking the hardware interrupt and running a thread.

Do you mean that it's not appropriate to use " queue_delayed_work" here, we should use other method for handling work item ?

No, when I said it shouldn't be a threaded interrupt I meant that using request_threaded_irq() doesn't seem appropriate.