Linux LED subsystem development
 help / color / mirror / Atom feed
From: Daniil Demakov <t3127430@gmail.com>
To: riku.voipio@iki.fi
Cc: lee@kernel.org, pavel@kernel.org, griffin@kroah.com,
	linux-leds@vger.kernel.org, Daniil Demakov <t3127430@gmail.com>
Subject: [PATCH v3] leds: pca9532: Propagate I2C errors
Date: Sat, 28 Nov 2026 23:41:04 +0000	[thread overview]
Message-ID: <20261128234104.242580-1-t3127430@gmail.com> (raw)

Check the return values of SMBus read and write operations
in pca9532_setled() and propagate errors to callers.

Unregister the LED class device if configuring the LED fails.

Propagate errors from pca9532_gpio_set_value() in
pca9532_gpio_direction_input().

Fixes: e14fa82439d3 ("leds: Add pca9532 led driver")
Signed-off-by: Daniil Demakov <t3127430@gmail.com>
---
 drivers/leds/leds-pca9532.c | 64 +++++++++++++++++++++++++------------
 1 file changed, 44 insertions(+), 20 deletions(-)

diff --git a/drivers/leds/leds-pca9532.c b/drivers/leds/leds-pca9532.c
index c8085427696f..e2772c7b23d9 100644
--- a/drivers/leds/leds-pca9532.c
+++ b/drivers/leds/leds-pca9532.c
@@ -158,22 +158,42 @@ static int pca9532_setpwm(struct i2c_client *client, int pwm)
 	return 0;
 }
 
-/* Set LED routing */
-static void pca9532_setled(struct pca9532_led *led)
+static int __pca9532_setled(struct pca9532_led *led)
 {
 	struct i2c_client *client = led->client;
 	struct pca9532_data *data = i2c_get_clientdata(client);
 	u8 maxleds = data->chip_info->num_leds;
-	char reg;
+	int reg;
+	int ret;
 
-	mutex_lock(&data->update_lock);
 	reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
-	/* zero led bits */
-	reg = reg & ~LED_MASK(led->id);
-	/* set the new value */
-	reg = reg | (led->state << LED_SHIFT(led->id));
-	i2c_smbus_write_byte_data(client, LED_REG(maxleds, led->id), reg);
+	if (reg < 0) {
+		dev_warn(&client->dev, "Failed to read LED register: %d\n", reg);
+		return reg;
+	}
+
+	reg &= ~LED_MASK(led->id);
+	reg |= led->state << LED_SHIFT(led->id);
+
+	ret = i2c_smbus_write_byte_data(client, LED_REG(maxleds, led->id), reg);
+	if (ret < 0)
+		dev_warn(&client->dev,
+			 "Failed to write LED register: %d\n", ret);
+
+	return ret;
+}
+
+static int pca9532_setled(struct pca9532_led *led)
+{
+	struct i2c_client *client = led->client;
+	struct pca9532_data *data = i2c_get_clientdata(client);
+	int ret;
+
+	mutex_lock(&data->update_lock);
+	ret = __pca9532_setled(led);
 	mutex_unlock(&data->update_lock);
+
+	return ret;
 }
 
 static int pca9532_set_brightness(struct led_classdev *led_cdev,
@@ -196,7 +216,7 @@ static int pca9532_set_brightness(struct led_classdev *led_cdev,
 	}
 	if (led->state == PCA9532_PWM0)
 		pca9532_setpwm(led->client, PCA9532_PWM_ID_0);
-	pca9532_setled(led);
+	err = pca9532_setled(led);
 	return err;
 }
 
@@ -257,9 +277,9 @@ static int pca9532_set_blink(struct led_classdev *led_cdev,
 	if (err)
 		return err;
 
-	pca9532_setled(led);
+	err = pca9532_setled(led);
 
-	return 0;
+	return err;
 }
 
 static int pca9532_event(struct input_dev *dev, unsigned int type,
@@ -328,15 +348,15 @@ static int pca9532_gpio_set_value(struct gpio_chip *gc, unsigned int offset,
 {
 	struct pca9532_data *data = gpiochip_get_data(gc);
 	struct pca9532_led *led = &data->leds[offset];
-
+	int err;
 	if (val)
 		led->state = PCA9532_OFF;
 	else
 		led->state = PCA9532_ON;
 
-	pca9532_setled(led);
+	err = pca9532_setled(led);
 
-	return 0;
+	return err;
 }
 
 static int pca9532_gpio_get_value(struct gpio_chip *gc, unsigned offset)
@@ -352,9 +372,7 @@ static int pca9532_gpio_get_value(struct gpio_chip *gc, unsigned offset)
 static int pca9532_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
 {
 	/* To use as input ensure pin is not driven */
-	pca9532_gpio_set_value(gc, offset, 1);
-
-	return 0;
+	return pca9532_gpio_set_value(gc, offset, 1);
 }
 
 static int pca9532_gpio_direction_output(struct gpio_chip *gc, unsigned offset, int val)
@@ -442,14 +460,20 @@ static int pca9532_configure(struct i2c_client *client,
 					led->name);
 				goto exit;
 			}
-			pca9532_setled(led);
+			err = pca9532_setled(led);
+			if (err) {
+				led_classdev_unregister(&led->ldev);
+			    goto exit;
+			}
 			break;
 		case PCA9532_TYPE_N2100_BEEP:
 			/* PWM1 is reserved for beeper so blink will not use hardware */
 			data->hw_blink = false;
 			BUG_ON(data->idev);
 			led->state = PCA9532_PWM1;
-			pca9532_setled(led);
+			err = pca9532_setled(led);
+			if (err)
+			    goto exit;
 			data->idev = devm_input_allocate_device(&client->dev);
 			if (data->idev == NULL) {
 				err = -ENOMEM;
-- 
2.56.1


             reply	other threads:[~2026-11-28 23:42 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-28 23:41 Daniil Demakov [this message]
2026-11-28 23:50 ` [PATCH v3] leds: pca9532: Propagate I2C errors sashiko-bot
  -- strict thread matches above, loose matches on Subject: below --
2026-11-14 17:24 [PATCH v2] " Daniil Demakov
2026-11-14 18:14 ` [PATCH v3] " Daniil Demakov
2026-11-14 18:31   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261128234104.242580-1-t3127430@gmail.com \
    --to=t3127430@gmail.com \
    --cc=griffin@kroah.com \
    --cc=lee@kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=pavel@kernel.org \
    --cc=riku.voipio@iki.fi \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox