Skip to contentSkip to conversation

ASoC: rt5677: Refactor GPIO and use device_get_match_data()

PATCH In review Andy Shevchenko 3 participants 15 messages Jun 30, 2023 – Jul 12, 2023

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

v2 cover ASoC: rt5677: Refactor GPIO and use device_get_match_data()

The code can be simplified with refactored GPIO parts and with use of device_get_match_data(). Besides that couple of additional changes, one for maintenance and one for making IRQ domain agnostic (not being pinned to OF).

Changelog v2:
- refactored GPIO code in (a new) patch 1
- fixed compilation error in patch 2 (LKP)

Andy Shevchenko (4):
ASoC: rt5677: Refactor GPIO support code
ASoC: rt5677: Use agnostic irq_domain_create_linear()
ASoC: rt5677: Use device_get_match_data()
ASoC: rt5677: Sort headers alphabetically

 sound/soc/codecs/rt5677.c | 117 +++++++++++++-------------------------
 sound/soc/codecs/rt5677.h |  92 +++++-------------------------
 2 files changed, 53 insertions(+), 156 deletions(-)
Signature · 1 line
2.40.0.1.gaa8946217a0b
#1 lore raw

PATCH v2 1/4 ASoC: rt5677: Refactor GPIO support code

After compiler complains:

sound/soc/codecs/rt5677.c:4748:30: warning: dubious: x | !y

I looked into the code and realized that we can refactor it for better reading and fixing above issue at the same time. Hence this change. It does not imply any functional changes.

---
 sound/soc/codecs/rt5677.c | 76 +++++++++++----------------------
 sound/soc/codecs/rt5677.h | 88 ++++++---------------------------------
 2 files changed, 37 insertions(+), 127 deletions(-)
sound/soc/codecs/rt5677.c +24 -52
index ad14d18860fc..3a2a6b150cda 100644
@@ -4717,50 +4717,34 @@ static int rt5677_set_bias_level(struct snd_soc_component *component,
 	return 0;
 }
 
+static int rt5677_update_gpio_bits(struct rt5677_priv *rt5677, unsigned offset, int m, int v)
+{
+	unsigned int bank = offset / 5;
+	unsigned int shift = (offset % 5) * 3;
+	unsigned int reg = bank ? RT5677_GPIO_CTRL3 : RT5677_GPIO_CTRL2;
+
+	return regmap_update_bits(rt5677->regmap, reg, m << shift, v << shift);
+}
+
 #ifdef CONFIG_GPIOLIB
 static void rt5677_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
 {
 	struct rt5677_priv *rt5677 = gpiochip_get_data(chip);
+	int level = value ? RT5677_GPIOx_OUT_HI : RT5677_GPIOx_OUT_LO;
+	int m = RT5677_GPIOx_OUT_MASK;
 
-	switch (offset) {
-	case RT5677_GPIO1 ... RT5677_GPIO5:
-		regmap_update_bits(rt5677->regmap, RT5677_GPIO_CTRL2,
-			0x1 << (offset * 3 + 1), !!value << (offset * 3 + 1));
-		break;
-
-	case RT5677_GPIO6:
-		regmap_update_bits(rt5677->regmap, RT5677_GPIO_CTRL3,
-			RT5677_GPIO6_OUT_MASK, !!value << RT5677_GPIO6_OUT_SFT);
-		break;
-
-	default:
-		break;
-	}
+	rt5677_update_gpio_bits(rt5677, offset, m, level);
 }
 
 static int rt5677_gpio_direction_out(struct gpio_chip *chip,
 				     unsigned offset, int value)
 {
 	struct rt5677_priv *rt5677 = gpiochip_get_data(chip);
+	int level = value ? RT5677_GPIOx_OUT_HI : RT5677_GPIOx_OUT_LO;
+	int m = RT5677_GPIOx_DIR_MASK | RT5677_GPIOx_OUT_MASK;
+	int v = RT5677_GPIOx_DIR_OUT | level;
 
-	switch (offset) {
-	case RT5677_GPIO1 ... RT5677_GPIO5:
-		regmap_update_bits(rt5677->regmap, RT5677_GPIO_CTRL2,
-			0x3 << (offset * 3 + 1),
-			(0x2 | !!value) << (offset * 3 + 1));
-		break;
-
-	case RT5677_GPIO6:
-		regmap_update_bits(rt5677->regmap, RT5677_GPIO_CTRL3,
-			RT5677_GPIO6_DIR_MASK | RT5677_GPIO6_OUT_MASK,
-			RT5677_GPIO6_DIR_OUT | !!value << RT5677_GPIO6_OUT_SFT);
-		break;
-
-	default:
-		break;
-	}
-
-	return 0;
+	return rt5677_update_gpio_bits(rt5677, offset, m, v);
 }
 
 static int rt5677_gpio_get(struct gpio_chip *chip, unsigned offset)
@@ -4778,26 +4762,14 @@ static int rt5677_gpio_get(struct gpio_chip *chip, unsigned offset)
 static int rt5677_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
 {
 	struct rt5677_priv *rt5677 = gpiochip_get_data(chip);
+	int m = RT5677_GPIOx_DIR_MASK;
+	int v = RT5677_GPIOx_DIR_IN;
 
-	switch (offset) {
-	case RT5677_GPIO1 ... RT5677_GPIO5:
-		regmap_update_bits(rt5677->regmap, RT5677_GPIO_CTRL2,
-			0x1 << (offset * 3 + 2), 0x0);
-		break;
-
-	case RT5677_GPIO6:
-		regmap_update_bits(rt5677->regmap, RT5677_GPIO_CTRL3,
-			RT5677_GPIO6_DIR_MASK, RT5677_GPIO6_DIR_IN);
-		break;
-
-	default:
-		break;
-	}
-
-	return 0;
+	return rt5677_update_gpio_bits(rt5677, offset, m, v);
 }
 
-/** Configures the gpio as
+/*
+ * Configures the GPIO as
  *   0 - floating
  *   1 - pull down
  *   2 - pull up
@@ -5673,9 +5645,9 @@ static int rt5677_i2c_probe(struct i2c_client *i2c)
 		regmap_update_bits(rt5677->regmap, RT5677_GEN_CTRL2,
 					RT5677_GPIO5_FUNC_MASK,
 					RT5677_GPIO5_FUNC_DMIC);
-		regmap_update_bits(rt5677->regmap, RT5677_GPIO_CTRL2,
-					RT5677_GPIO5_DIR_MASK,
-					RT5677_GPIO5_DIR_OUT);
+		rt5677_update_gpio_bits(rt5677, RT5677_GPIO5,
+					RT5677_GPIOx_DIR_MASK,
+					RT5677_GPIOx_DIR_OUT);
 	}
 
 	if (rt5677->pdata.micbias1_vdd_3v3)
sound/soc/codecs/rt5677.h +13 -75

94 lines not on this page · raw message

Signature · 1 line
2.40.0.1.gaa8946217a0b
#2 lore raw

PATCH v2 2/4 ASoC: rt5677: Use agnostic irq_domain_create_linear()

In reply to Andy Shevchenko

Instead of irq_domain_add_linear() that requires of_node, use irq_domain_create_linear() that works outside of OF world.

---
 sound/soc/codecs/rt5677.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/soc/codecs/rt5677.c +1 -1
index 3a2a6b150cda..17d5dd5d2974 100644
@@ -5511,7 +5511,7 @@ static int rt5677_init_irq(struct i2c_client *i2c)
 			RT5677_GPIO1_PIN_MASK, RT5677_GPIO1_PIN_IRQ);
 
 	/* Ready to listen for interrupts */
-	rt5677->domain = irq_domain_add_linear(i2c->dev.of_node,
+	rt5677->domain = irq_domain_create_linear(dev_fwnode(&i2c->dev),
 			RT5677_IRQ_NUM, &rt5677_domain_ops, rt5677);
 	if (!rt5677->domain) {
 		dev_err(&i2c->dev, "Failed to create IRQ domain\n");
Signature · 1 line
2.40.0.1.gaa8946217a0b
#3 lore raw

PATCH v2 3/4 ASoC: rt5677: Use device_get_match_data()

In reply to Andy Shevchenko

Use device_get_match_data() to simplify the code.

---
 sound/soc/codecs/rt5677.c | 21 ++++-----------------
 sound/soc/codecs/rt5677.h |  4 ++--
 2 files changed, 6 insertions(+), 19 deletions(-)
sound/soc/codecs/rt5677.c +4 -17
index 17d5dd5d2974..b0c15e27c763 100644
@@ -6,7 +6,6 @@
  * Author: Oder Chiou <[email protected]>
  */
 
-#include <linux/acpi.h>
 #include <linux/fs.h>
 #include <linux/module.h>
 #include <linux/moduleparam.h>
@@ -18,7 +17,6 @@
 #include <linux/platform_device.h>
 #include <linux/spi/spi.h>
 #include <linux/firmware.h>
-#include <linux/of_device.h>
 #include <linux/property.h>
 #include <linux/irq.h>
 #include <linux/interrupt.h>
@@ -5531,6 +5529,7 @@ static int rt5677_init_irq(struct i2c_client *i2c)
 
 static int rt5677_i2c_probe(struct i2c_client *i2c)
 {
+	struct device *dev = &i2c->dev;
 	struct rt5677_priv *rt5677;
 	int ret;
 	unsigned int val;
@@ -5545,21 +5544,9 @@ static int rt5677_i2c_probe(struct i2c_client *i2c)
 	INIT_DELAYED_WORK(&rt5677->dsp_work, rt5677_dsp_work);
 	i2c_set_clientdata(i2c, rt5677);
 
-	if (i2c->dev.of_node) {
-		const struct of_device_id *match_id;
-
-		match_id = of_match_device(rt5677_of_match, &i2c->dev);
-		if (match_id)
-			rt5677->type = (enum rt5677_type)match_id->data;
-	} else if (ACPI_HANDLE(&i2c->dev)) {
-		const struct acpi_device_id *acpi_id;
-
-		acpi_id = acpi_match_device(rt5677_acpi_match, &i2c->dev);
-		if (acpi_id)
-			rt5677->type = (enum rt5677_type)acpi_id->driver_data;
-	} else {
+	rt5677->type = (enum rt5677_type)(uintptr_t)device_get_match_data(dev);
+	if (rt5677->type == 0)
 		return -EINVAL;
-	}
 
 	rt5677_read_device_properties(rt5677, &i2c->dev);
 
@@ -5674,7 +5661,7 @@ static struct i2c_driver rt5677_i2c_driver = {
 	.driver = {
 		.name = RT5677_DRV_NAME,
 		.of_match_table = rt5677_of_match,
-		.acpi_match_table = ACPI_PTR(rt5677_acpi_match),
+		.acpi_match_table = rt5677_acpi_match,
 	},
 	.probe    = rt5677_i2c_probe,
 	.remove   = rt5677_i2c_remove,
sound/soc/codecs/rt5677.h +2 -2
index 5932b04cf85e..d67ebae067d9 100644
@@ -1691,8 +1691,8 @@ enum {
 };
 
 enum rt5677_type {
-	RT5677,
-	RT5676,
+	RT5677 = 1,
+	RT5676 = 2,
 };
 
 /* ASRC clock source selection */
Signature · 1 line
2.40.0.1.gaa8946217a0b
#4 lore raw

PATCH v2 4/4 ASoC: rt5677: Sort headers alphabetically

In reply to Andy Shevchenko

It's hard to see what's included and what's not on the glance. Sort headers alphabetically to improve maintenance.

---
 sound/soc/codecs/rt5677.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
sound/soc/codecs/rt5677.c +9 -9
index b0c15e27c763..0e70a3ab42b5 100644
@@ -6,21 +6,21 @@
  * Author: Oder Chiou <[email protected]>
  */
 
+#include <linux/delay.h>
+#include <linux/firmware.h>
 #include <linux/fs.h>
+#include <linux/i2c.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/irqdomain.h>
+#include <linux/irq.h>
 #include <linux/module.h>
 #include <linux/moduleparam.h>
-#include <linux/init.h>
-#include <linux/delay.h>
+#include <linux/platform_device.h>
 #include <linux/pm.h>
+#include <linux/property.h>
 #include <linux/regmap.h>
-#include <linux/i2c.h>
-#include <linux/platform_device.h>
 #include <linux/spi/spi.h>
-#include <linux/firmware.h>
-#include <linux/property.h>
-#include <linux/irq.h>
-#include <linux/interrupt.h>
-#include <linux/irqdomain.h>
 #include <linux/workqueue.h>
 #include <sound/core.h>
 #include <sound/pcm.h>
Signature · 1 line
2.40.0.1.gaa8946217a0b
maintainer
#5 lore raw
In reply to Andy Shevchenko
Quoted 10 lines · Andy Shevchenko

The code can be simplified with refactored GPIO parts and with use of device_get_match_data(). Besides that couple of additional changes, one for maintenance and one for making IRQ domain agnostic (not being pinned to OF).

Changelog v2:
- refactored GPIO code in (a new) patch 1
- fixed compilation error in patch 2 (LKP)

[...]

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-next

Thanks!

[1/4] ASoC: rt5677: Refactor GPIO support code

      commit: 5512ffd9f39832f312b7f903703ac39d6367fe8a
[2/4] ASoC: rt5677: Use agnostic irq_domain_create_linear()
      commit: c3d42d7baf6b4032171270e3df001fb946493452
[3/4] ASoC: rt5677: Use device_get_match_data()
      commit: 043bb9c012ee7d092a477159cc66dbdf62fd2666
[4/4] ASoC: rt5677: Sort headers alphabetically
      commit: ea1c1019a88d88cf0a7d6892f594b72ddb3b8c0b

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark

#6 lore raw
Quoted 9 lines · Mark Brown
Quoted 4 lines · Andy Shevchenko

The code can be simplified with refactored GPIO parts and with use of device_get_match_data(). Besides that couple of additional changes, one for maintenance and one for making IRQ domain agnostic (not being pinned to OF).

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-next

JFYI: You might need to amend your scripts (?) as in the merge commit
the Subject is cut (I believe that your scripts doesn't handle RFC2822 long lines in the email headers).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#7 lore raw
Quoted 3 lines · Andy Shevchenko

JFYI: You might need to amend your scripts (?) as in the merge commit
the Subject is cut (I believe that your scripts doesn't handle RFC2822 long lines in the email headers).

Report that to Konstantin, this is just b4.

#8 lore raw

+Cc: Konstantin.

Konstantin, seems b4 doesn't handle long Subject lines for merge commits (PR mode).

Quoted 7 lines · Mark Brown
Quoted 3 lines · Andy Shevchenko

JFYI: You might need to amend your scripts (?) as in the merge commit
the Subject is cut (I believe that your scripts doesn't handle RFC2822 long lines in the email headers).

Report that to Konstantin, this is just b4.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw
Quoted 4 lines · Andy Shevchenko

+Cc: Konstantin.

Konstantin, seems b4 doesn't handle long Subject lines for merge commits (PR mode).

Could you give me something more to go on? Was this done with "b4 shazam" or via some other way?

-K

maintainer
#10 lore raw
Quoted 7 lines · Konstantin Ryabitsev
Quoted 2 lines · Andy Shevchenko

Konstantin, seems b4 doesn't handle long Subject lines for merge commits (PR mode).

Could you give me something more to go on? Was this done with "b4 shazam" or via some other way?

The patches are applied from mailboxes fetched with b4 am, then the thanks mails go out with b4 ty.

#11 lore raw
Quoted 5 lines · Mark Brown
Quoted 2 lines

Could you give me something more to go on? Was this done with "b4 shazam" or via some other way?

The patches are applied from mailboxes fetched with b4 am, then the thanks mails go out with b4 ty.

But I think the complaint was about the merge commits being cut off:

    https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git/commit/?id=5a043fd5c74c66897ad113e5ee34e9361e3f4c11

When I try "b4 shazam -M", the merge commit looks different to me, so I want to understand how the merge commit is generated in your workflow.

-K

maintainer
#12 lore raw
Quoted 8 lines · Konstantin Ryabitsev
Quoted 2 lines · Mark Brown

The patches are applied from mailboxes fetched with b4 am, then the thanks mails go out with b4 ty.

But I think the complaint was about the merge commits being cut off:

    https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git/commit/?id=5a043fd5c74c66897ad113e5ee34e9361e3f4c11

Oh, I see. The commit in the mail looked like it might be off and it was in reply to the applied mail so I thought that was what was being referenced.

Quoted 2 lines

When I try "b4 shazam -M", the merge commit looks different to me, so I want to understand how the merge commit is generated in your workflow.

That's generated by my scripts based on the .cover that b4 writes out and some sed at the point where a merge is done. There's a bunch of CI happens between applying the patches and me actually merging them so I don't use b4 for that bit. It's just picking the subject line out with sed so there's no way it's going to handle multi-line subjects.

#13 lore raw
Quoted 8 lines · Mark Brown
Quoted 3 lines

But I think the complaint was about the merge commits being cut off:

    https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git/commit/?id=5a043fd5c74c66897ad113e5ee34e9361e3f4c11

Oh, I see. The commit in the mail looked like it might be off and it was in reply to the applied mail so I thought that was what was being referenced.

Quoted 8 lines
Quoted 2 lines

When I try "b4 shazam -M", the merge commit looks different to me, so I want to understand how the merge commit is generated in your workflow.

That's generated by my scripts based on the .cover that b4 writes out and some sed at the point where a merge is done. There's a bunch of CI happens between applying the patches and me actually merging them so I don't use b4 for that bit. It's just picking the subject line out with sed so there's no way it's going to handle multi-line subjects.

Will it help if b4 optionally writes out a foo.merge file in addition to foo.cover in the same format as with "b4 shazam"?

-K

maintainer
#14 lore raw
Quoted 10 lines · Konstantin Ryabitsev
Quoted 5 lines · Mark Brown

That's generated by my scripts based on the .cover that b4 writes out and some sed at the point where a merge is done. There's a bunch of CI happens between applying the patches and me actually merging them so I don't use b4 for that bit. It's just picking the subject line out with sed so there's no way it's going to handle multi-line subjects.

Will it help if b4 optionally writes out a foo.merge file in addition to foo.cover in the same format as with "b4 shazam"?

That sounds like it should be good, yes - I can just use that as the message rather than generating one (not having seen the specific format).