Patchwork i2c-mux-gpio: Check gpio_direction_output return value

login
register
mail settings
Submitter Jean Delvare
Date March 3, 2013, 6:58 p.m.
Message ID <20130303195815.10ba20b1@endymion.delvare>
Download mbox | patch
Permalink /patch/224564/
State Superseded
Headers show

Comments

Jean Delvare - March 3, 2013, 6:58 p.m.
gpio_direction_output() may fail, check for that and deal with it
appropriately. Also log an error message if gpio_request() fails.

Signed-off-by: Jean Delvare <khali@linux-fr.org>
Cc: Peter Korsgaard <peter.korsgaard@barco.com>
Cc: Wolfram Sang <wsa@the-dreams.de>
---
 drivers/i2c/muxes/i2c-mux-gpio.c |   17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
Peter Korsgaard - March 3, 2013, 7:37 p.m.
>>>>> "JD" == Jean Delvare <khali@linux-fr.org> writes:

JD> gpio_direction_output() may fail, check for that and deal with it
JD> appropriately. Also log an error message if gpio_request() fails.

JD> Signed-off-by: Jean Delvare <khali@linux-fr.org>
JD> Cc: Peter Korsgaard <peter.korsgaard@barco.com>
JD> Cc: Wolfram Sang <wsa@the-dreams.de>
JD> ---
JD>  drivers/i2c/muxes/i2c-mux-gpio.c |   17 ++++++++++++++---
JD>  1 file changed, 14 insertions(+), 3 deletions(-)

JD> --- linux-3.7.orig/drivers/i2c/muxes/i2c-mux-gpio.c	2012-12-15 14:52:47.368991730 +0100
JD> +++ linux-3.7/drivers/i2c/muxes/i2c-mux-gpio.c	2013-03-03 14:27:21.003905620 +0100
JD> @@ -123,10 +123,21 @@ static int __devinit i2c_mux_gpio_probe(
 
JD>  	for (i = 0; i < pdata->n_gpios; i++) {
JD>  		ret = gpio_request(gpio_base + pdata->gpios[i], "i2c-mux-gpio");
JD> -		if (ret)
JD> +		if (ret) {
JD> +			dev_err(&pdev->dev, "Failed to request GPIO %d\n",
JD> +				pdata->gpios[i]);
JD>  			goto err_request_gpio;
JD> -		gpio_direction_output(gpio_base + pdata->gpios[i],
JD> -				      initial_state & (1 << i));
JD> +		}
JD> +
JD> +		ret = gpio_direction_output(gpio_base + pdata->gpios[i],
JD> +					    initial_state & (1 << i));
JD> +		if (ret) {
JD> +			dev_err(&pdev->dev,
JD> +				"Failed to set direction of GPIO %d to output\n",
JD> +				pdata->gpios[i]);
JD> +			i++;
JD> +			goto err_request_gpio;

The i++ is a bit nonobvious, so a comment would be good. Other than that
it looks fine.

Acked-by: Peter Korsgaard <peter.korsgaard@barco.com>

Patch

--- linux-3.7.orig/drivers/i2c/muxes/i2c-mux-gpio.c	2012-12-15 14:52:47.368991730 +0100
+++ linux-3.7/drivers/i2c/muxes/i2c-mux-gpio.c	2013-03-03 14:27:21.003905620 +0100
@@ -123,10 +123,21 @@  static int __devinit i2c_mux_gpio_probe(
 
 	for (i = 0; i < pdata->n_gpios; i++) {
 		ret = gpio_request(gpio_base + pdata->gpios[i], "i2c-mux-gpio");
-		if (ret)
+		if (ret) {
+			dev_err(&pdev->dev, "Failed to request GPIO %d\n",
+				pdata->gpios[i]);
 			goto err_request_gpio;
-		gpio_direction_output(gpio_base + pdata->gpios[i],
-				      initial_state & (1 << i));
+		}
+
+		ret = gpio_direction_output(gpio_base + pdata->gpios[i],
+					    initial_state & (1 << i));
+		if (ret) {
+			dev_err(&pdev->dev,
+				"Failed to set direction of GPIO %d to output\n",
+				pdata->gpios[i]);
+			i++;
+			goto err_request_gpio;
+		}
 	}
 
 	for (i = 0; i < pdata->n_values; i++) {