Skip to contentSkip to conversation

pinctrl: cs42l43: Add GPIO controlled mic/spk mute LEDs

PATCH In review Maciej Strozek 3 participants 3 messages Nov 10 – Nov 11

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

PATCH 1/1 pinctrl: cs42l43: Add GPIO controlled mic/spk mute LEDs

Add ability to use cs42l43's GPIOs to drive speaker and microphone mute LEDs. The LEDs must be described using 01fa-global-mute-led-(spk|mic)-mute-gpio ACPI property, which tells which of the 3 available cs42l43's GPIOs to use for the respective function.

---
 drivers/pinctrl/cirrus/pinctrl-cs42l43.c | 156 +++++++++++++++++++++++
 1 file changed, 156 insertions(+)
drivers/pinctrl/cirrus/pinctrl-cs42l43.c +156 -0
index 8990fab0446cf..7e4e43783ef14 100644
@@ -5,11 +5,15 @@
 // Copyright (c) 2023 Cirrus Logic, Inc. and
 //                    Cirrus Logic International Semiconductor Ltd.
 
+#include <linux/acpi.h>
 #include <linux/array_size.h>
 #include <linux/bits.h>
 #include <linux/build_bug.h>
 #include <linux/err.h>
+#include <linux/gpio/consumer.h>
 #include <linux/gpio/driver.h>
+#include <linux/gpio/machine.h>
+#include <linux/leds.h>
 #include <linux/mfd/cs42l43.h>
 #include <linux/mfd/cs42l43-regs.h>
 #include <linux/module.h>
@@ -29,9 +33,18 @@
 
 #define CS42L43_NUM_GPIOS 3
 
+struct cs42l43_mute_led {
+	struct led_classdev cdev;
+	struct gpio_desc *gpio;
+	u32 gpio_num;
+};
+
 struct cs42l43_pin {
 	struct gpio_chip gpio_chip;
 
+	struct cs42l43_mute_led spk_mute_led;
+	struct cs42l43_mute_led mic_mute_led;
+
 	struct device *dev;
 	struct regmap *regmap;
 	bool shutters_locked;
@@ -535,6 +548,143 @@ static void cs42l43_fwnode_put(void *data)
 	fwnode_handle_put(data);
 }
 
+#if IS_REACHABLE(CONFIG_LEDS_CLASS)
+static int cs42l43_find_mute_gpios(struct cs42l43_pin *priv, struct fwnode_handle *fwnode)
+{
+	struct cs42l43_mute_led *mic = &priv->mic_mute_led;
+	struct cs42l43_mute_led *spk = &priv->spk_mute_led;
+	int ret;
+
+	fwnode_for_each_child_node_scoped(fwnode, child_fwnode) {
+		struct fwnode_handle *ext_fwnode __free(fwnode_handle) =
+			fwnode_get_named_child_node(child_fwnode,
+						    "mipi-sdca-function-expansion-subproperties");
+		bool mic_present, spk_present;
+
+		if (!ext_fwnode)
+			continue;
+
+		mic_present = fwnode_property_present(ext_fwnode,
+						      "01fa-global-mute-led-mic-mute-gpio");
+		spk_present = fwnode_property_present(ext_fwnode,
+						      "01fa-global-mute-led-spk-mute-gpio");
+		if (!mic_present && !spk_present)
+			continue;
+
+		if (mic_present) {
+			ret = fwnode_property_read_u32(ext_fwnode,
+						       "01fa-global-mute-led-mic-mute-gpio",
+						       &mic->gpio_num);
+			if (ret)
+				return dev_err_probe(priv->dev, ret,
+						     "Invalid mic-mute LED GPIO property\n");
+		}
+
+		if (spk_present) {
+			ret = fwnode_property_read_u32(ext_fwnode,
+						       "01fa-global-mute-led-spk-mute-gpio",
+						       &spk->gpio_num);
+			if (ret)
+				return dev_err_probe(priv->dev, ret,
+						     "Invalid spk-mute LED GPIO property\n");
+		}
+
+		return 0;
+	}
+
+	return 0;
+}
+
+static int cs42l43_mute_led_set(struct led_classdev *cdev, enum led_brightness value)
+{
+	struct cs42l43_mute_led *led = container_of(cdev, struct cs42l43_mute_led, cdev);
+
+	return gpiod_set_value_cansleep(led->gpio, value != LED_OFF);
+}
+
+static void cs42l43_mute_led_gpio_free(void *data)
+{
+	struct cs42l43_mute_led *led = data;
+
+	gpiochip_free_own_desc(led->gpio);
+}
+
+static int cs42l43_mute_led_register(struct cs42l43_pin *priv,
+				     struct cs42l43_mute_led *led,
+				     const char *function, const char *trigger)
+{
+	int ret;
+
+	led->gpio = gpiochip_request_own_desc(&priv->gpio_chip, led->gpio_num - 1,
+					      trigger, GPIO_ACTIVE_HIGH,
+					      GPIOD_OUT_LOW);
+	if (IS_ERR(led->gpio))
+		return dev_err_probe(priv->dev, PTR_ERR(led->gpio),
+				     "Failed to request GPIO%u for %s LED\n",
+				     led->gpio_num, trigger);
+
+	ret = devm_add_action_or_reset(priv->dev, cs42l43_mute_led_gpio_free, led);
+	if (ret)
+		return ret;
+
+	led->cdev.name = devm_kasprintf(priv->dev, GFP_KERNEL, "%s::%s",
+					dev_name(priv->dev), function);
+	if (!led->cdev.name)
+		return -ENOMEM;
+
+	led->cdev.default_trigger = trigger;
+	led->cdev.brightness_set_blocking = cs42l43_mute_led_set;
+	led->cdev.max_brightness = 1;
+
+	ret = devm_led_classdev_register(priv->dev, &led->cdev);
+	if (ret)
+		return dev_err_probe(priv->dev, ret, "Failed to register %s LED\n",
+				     trigger);
+
+	dev_dbg(priv->dev, "%s LED uses codec GPIO%u\n", trigger, led->gpio_num);
+
+	return 0;
+}
+
+static int cs42l43_mute_leds_register(struct cs42l43_pin *priv,
+				      struct fwnode_handle *fwnode)
+{
+	struct cs42l43_mute_led *mic = &priv->mic_mute_led;
+	struct cs42l43_mute_led *spk = &priv->spk_mute_led;
+	int ret;
+
+	if (!is_acpi_node(fwnode))
+		return 0;
+
+	ret = cs42l43_find_mute_gpios(priv, fwnode);
+	if (ret)
+		return ret;
+
+	if (mic->gpio_num > CS42L43_NUM_GPIOS ||
+	    spk->gpio_num > CS42L43_NUM_GPIOS ||
+	    (mic->gpio_num && mic->gpio_num == spk->gpio_num))
+		return dev_err_probe(priv->dev, -EINVAL,
+				     "Invalid or shared mute LED GPIOs: mic=%u, spk=%u\n",
+				     mic->gpio_num, spk->gpio_num);
+
+	if (mic->gpio_num) {
+		ret = cs42l43_mute_led_register(priv, mic, LED_FUNCTION_MICMUTE,
+						"audio-micmute");
+		if (ret)
+			return ret;
+	}
+
+	if (spk->gpio_num) {
+		ret = cs42l43_mute_led_register(priv, spk, LED_FUNCTION_MUTE,
+						"audio-mute");
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+#endif
+
 static int cs42l43_pin_probe(struct platform_device *pdev)
 {
 	struct cs42l43 *cs42l43 = dev_get_drvdata(pdev->dev.parent);
@@ -598,6 +748,12 @@ static int cs42l43_pin_probe(struct platform_device *pdev)
 		return dev_err_probe(priv->dev, ret,
 				     "Failed to register gpiochip\n");
 
+#if IS_REACHABLE(CONFIG_LEDS_CLASS)
+	ret = cs42l43_mute_leds_register(priv, dev_fwnode(cs42l43->dev));
+	if (ret)
+		return ret;
+#endif
+
 	return 0;
 }
 
Signature · 1 line
2.47.3
#1 lore raw
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] pinctrl: cirrus: cs42l43: unbalanced…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] pinctrl: cirrus: cs42l43: unbalanced fwnode put in cs42l43_pin_probe() [1]
--

commit 1aa7ff7b2b9b125f7c2e43385c49822080f93214
Author: Maciej Strozek <[email protected]>

pinctrl: cs42l43: Add GPIO controlled mic/spk mute LEDs

This commit adds support for driving speaker and microphone mute LEDs via cs42l43 GPIOs when described by ACPI properties.

Quoted 4 lines
drivers/pinctrl/cirrus/pinctrl-cs42l43.c +0 -0
index a1c839b355578..44a9a759e0f65 100644

[ ... ]

drivers/pinctrl/cirrus/pinctrl-cs42l43.c static int cs42l43_pin_probe(struct platform_device *pdev) · line 758

Quoted 12 lines
drivers/pinctrl/cirrus/pinctrl-cs42l43.c +6 -0
@@ -598,6 +748,12 @@ static int cs42l43_pin_probe(struct platform_device *pdev)
 		return dev_err_probe(priv->dev, ret,
 				     "Failed to register gpiochip\n");
 
+#if IS_REACHABLE(CONFIG_LEDS_CLASS)
+	ret = cs42l43_mute_leds_register(priv, dev_fwnode(cs42l43->dev));
+	if (ret)
+		return ret;
+#endif
+
 	return 0;
 }

[Severity: High]
This problem wasn't introduced by this patch, but does assigning fwnode via device_set_node() in cs42l43_pin_probe() cause an unbalanced fwnode_handle_put() [1] when the platform device is released?

Earlier in cs42l43_pin_probe(), fwnode is initialized from the parent MFD device or looked up from the "pinctrl" child node, and then assigned to priv->dev:

drivers/pinctrl/cirrus/pinctrl-cs42l43.c:cs42l43_pin_probe() {
	...
	if (is_of_node(fwnode)) {
		struct fwnode_handle *child;

		child = fwnode_get_named_child_node(fwnode, "pinctrl");
		if (child) {
			ret = devm_add_action_or_reset(&pdev->dev,
				cs42l43_fwnode_put, child);
			if (ret)
				return ret;

			if (!child->dev)
				child->dev = priv->dev;
			fwnode = child;
		}
	}

	priv->gpio_chip.fwnode = fwnode;

	device_set_node(priv->dev, fwnode);
	...
}

Unlike device_add_software_node(), device_set_node() does not acquire a reference on fwnode, whereas platform_device_release() unconditionally drops a reference on non-software nodes when the platform device is freed:

drivers/base/platform.c:platform_device_release() {
	...
	if (!is_software_node(dev_fwnode(dev)))
		fwnode_handle_put(pa->pdev.dev.fwnode);
	...
}

When the "pinctrl" child node is present, its single reference from fwnode_get_named_child_node() is already released by cs42l43_fwnode_put() via devres; otherwise fwnode belongs to cs42l43->dev with no extra reference taken. When the platform device is later unregistered, platform_device_release() puts pdev->dev.fwnode an extra time, underflowing the fwnode reference count and risking a use-after-free.

[1] https://sashiko.dev/#/bug/linux-a633bdf8-b5fe-4770-b14c-d3532525ae12

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Maciej Strozek
Quoted 8 lines · Maciej Strozek

Add ability to use cs42l43's GPIOs to drive speaker and microphone mute LEDs. The LEDs must be described using 01fa-global-mute-led-(spk|mic)-mute-gpio ACPI property, which tells which of the 3 available cs42l43's GPIOs to use for the respective function.

---

Thanks,
Charles