Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e80beb27 authored by Grant Likely's avatar Grant Likely
Browse files

gpio: Make of_count_named_gpios() use new of_count_phandle_with_args()



This patch replaces the horribly coded of_count_named_gpios() with a
call to of_count_phandle_with_args() which is far more efficient. This
also changes the return value of of_gpio_count() & of_gpio_named_count()
from 'unsigned int' to 'int' so that it can return an error code. All
the users of that function are fixed up to correctly handle a negative
return value.

v2: Split GPIO portion into a separate patch

Tested-by: default avatarAndreas Larsson <andreas@gaisler.com>
Signed-off-by: default avatarGrant Likely <grant.likely@secretlab.ca>
Cc: Linus Walleij <linus.walleij@linaro.org>
Cc: Rob Herring <rob.herring@calxeda.com>
parent bd69f73f
Loading
Loading
Loading
Loading
+0 −35
Original line number Original line Diff line number Diff line
@@ -88,41 +88,6 @@ int of_get_named_gpio_flags(struct device_node *np, const char *propname,
}
}
EXPORT_SYMBOL(of_get_named_gpio_flags);
EXPORT_SYMBOL(of_get_named_gpio_flags);


/**
 * of_gpio_named_count - Count GPIOs for a device
 * @np:		device node to count GPIOs for
 * @propname:	property name containing gpio specifier(s)
 *
 * The function returns the count of GPIOs specified for a node.
 *
 * Note that the empty GPIO specifiers counts too. For example,
 *
 * gpios = <0
 *          &pio1 1 2
 *          0
 *          &pio2 3 4>;
 *
 * defines four GPIOs (so this function will return 4), two of which
 * are not specified.
 */
unsigned int of_gpio_named_count(struct device_node *np, const char* propname)
{
	unsigned int cnt = 0;

	do {
		int ret;

		ret = of_parse_phandle_with_args(np, propname, "#gpio-cells",
						 cnt, NULL);
		/* A hole in the gpios = <> counts anyway. */
		if (ret < 0 && ret != -EEXIST)
			break;
	} while (++cnt);

	return cnt;
}
EXPORT_SYMBOL(of_gpio_named_count);

/**
/**
 * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
 * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
 * @gc:		pointer to the gpio_chip structure
 * @gc:		pointer to the gpio_chip structure
+2 −2
Original line number Original line Diff line number Diff line
@@ -422,7 +422,7 @@ static int gpio_fan_get_of_pdata(struct device *dev,


	/* Fill GPIO pin array */
	/* Fill GPIO pin array */
	pdata->num_ctrl = of_gpio_count(node);
	pdata->num_ctrl = of_gpio_count(node);
	if (!pdata->num_ctrl) {
	if (pdata->num_ctrl <= 0) {
		dev_err(dev, "gpios DT property empty / missing");
		dev_err(dev, "gpios DT property empty / missing");
		return -ENODEV;
		return -ENODEV;
	}
	}
@@ -477,7 +477,7 @@ static int gpio_fan_get_of_pdata(struct device *dev,
	pdata->speed = speed;
	pdata->speed = speed;


	/* Alarm GPIO if one exists */
	/* Alarm GPIO if one exists */
	if (of_gpio_named_count(node, "alarm-gpios")) {
	if (of_gpio_named_count(node, "alarm-gpios") > 0) {
		struct gpio_fan_alarm *alarm;
		struct gpio_fan_alarm *alarm;
		int val;
		int val;
		enum of_gpio_flags flags;
		enum of_gpio_flags flags;
+4 −4
Original line number Original line Diff line number Diff line
@@ -403,7 +403,7 @@ matrix_keypad_parse_dt(struct device *dev)
	struct matrix_keypad_platform_data *pdata;
	struct matrix_keypad_platform_data *pdata;
	struct device_node *np = dev->of_node;
	struct device_node *np = dev->of_node;
	unsigned int *gpios;
	unsigned int *gpios;
	int i;
	int i, nrow, ncol;


	if (!np) {
	if (!np) {
		dev_err(dev, "device lacks DT data\n");
		dev_err(dev, "device lacks DT data\n");
@@ -416,9 +416,9 @@ matrix_keypad_parse_dt(struct device *dev)
		return ERR_PTR(-ENOMEM);
		return ERR_PTR(-ENOMEM);
	}
	}


	pdata->num_row_gpios = of_gpio_named_count(np, "row-gpios");
	pdata->num_row_gpios = nrow = of_gpio_named_count(np, "row-gpios");
	pdata->num_col_gpios = of_gpio_named_count(np, "col-gpios");
	pdata->num_col_gpios = ncol = of_gpio_named_count(np, "col-gpios");
	if (!pdata->num_row_gpios || !pdata->num_col_gpios) {
	if (nrow <= 0 || ncol <= 0) {
		dev_err(dev, "number of keypad rows/columns not specified\n");
		dev_err(dev, "number of keypad rows/columns not specified\n");
		return ERR_PTR(-EINVAL);
		return ERR_PTR(-EINVAL);
	}
	}
+2 −2
Original line number Original line Diff line number Diff line
@@ -53,7 +53,7 @@ static int mdio_mux_gpio_probe(struct platform_device *pdev)
{
{
	enum of_gpio_flags f;
	enum of_gpio_flags f;
	struct mdio_mux_gpio_state *s;
	struct mdio_mux_gpio_state *s;
	unsigned int num_gpios;
	int num_gpios;
	unsigned int n;
	unsigned int n;
	int r;
	int r;


@@ -61,7 +61,7 @@ static int mdio_mux_gpio_probe(struct platform_device *pdev)
		return -ENODEV;
		return -ENODEV;


	num_gpios = of_gpio_count(pdev->dev.of_node);
	num_gpios = of_gpio_count(pdev->dev.of_node);
	if (num_gpios == 0 || num_gpios > MDIO_MUX_GPIO_MAX_BITS)
	if (num_gpios <= 0 || num_gpios > MDIO_MUX_GPIO_MAX_BITS)
		return -ENODEV;
		return -ENODEV;


	s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
	s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
+2 −2
Original line number Original line Diff line number Diff line
@@ -947,12 +947,12 @@ static int of_fsl_spi_get_chipselects(struct device *dev)
	struct device_node *np = dev->of_node;
	struct device_node *np = dev->of_node;
	struct fsl_spi_platform_data *pdata = dev->platform_data;
	struct fsl_spi_platform_data *pdata = dev->platform_data;
	struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
	struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
	unsigned int ngpios;
	int ngpios;
	int i = 0;
	int i = 0;
	int ret;
	int ret;


	ngpios = of_gpio_count(np);
	ngpios = of_gpio_count(np);
	if (!ngpios) {
	if (ngpios <= 0) {
		/*
		/*
		 * SPI w/o chip-select line. One SPI device is still permitted
		 * SPI w/o chip-select line. One SPI device is still permitted
		 * though.
		 * though.
Loading