Skip to contentSkip to conversation

gpiolib: add request and free support

PATCH Superseded Jean-Christophe PLAGNIOL-VILLARD 2 participants 5 messages Dec 20, 2012 – Dec 21, 2012 GPIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover gpiolib: add request and free support

HI,

This allow to trac which gpio is used for what and allow drivers to have custom request and free

as example on at91 mux the pin as gpio on request

The following changes since commit b6accb5e560c3b39e738fad619a140f6247d27c6:

commands: flash: add missing newlines to error messages (2012-12-14 08:43:44 +0100)

are available in the git repository at:

git://git.jcrosoft.org/barebox.git delivery/gpiolib

for you to fetch changes up to b1e244b763731fb16fee15a93f45fc8c5455628a:

gpiolib: add command to dump the current gpio status (2012-12-18 23:09:05 +0800)

----------------------------------------------------------------
Jean-Christophe PLAGNIOL-VILLARD (2):
gpiolib: add gpio_request and gpio_free support
gpiolib: add command to dump the current gpio status

 drivers/gpio/gpio.c |  124 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 include/gpio.h      |    7 +++++++
 2 files changed, 124 insertions(+), 7 deletions(-)

Best Regards,
J.

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#1 lore raw

PATCH 1/2 gpiolib: add gpio_request and gpio_free support

as today if no request or free provided do not complain

if the gpio is not request auto requested at first use

---
 drivers/gpio/gpio.c |   87 ++++++++++++++++++++++++++++++++++++++++++++++-----
 include/gpio.h      |    7 +++++
 2 files changed, 87 insertions(+), 7 deletions(-)
drivers/gpio/gpio.c +80 -7
index 042a062..e29f0ba 100644
@@ -1,19 +1,71 @@
 #include <common.h>
 #include <gpio.h>
 #include <errno.h>
+#include <malloc.h>
 
 static LIST_HEAD(chip_list);
 
-static struct gpio_chip *gpio_desc[ARCH_NR_GPIOS];
+struct gpio_info {
+	struct gpio_chip *chip;
+	bool requested;
+	char *label;
+};
+
+static struct gpio_info gpio_desc[ARCH_NR_GPIOS];
+
+int gpio_request(unsigned gpio, const char *label)
+{
+	struct gpio_info *gi = &gpio_desc[gpio];
+	struct gpio_chip *chip = gi->chip;
+	int ret;
+
+	if (!gpio_is_valid(gpio))
+		return -EINVAL;
+	if (!chip)
+		return -EINVAL;
+	if (gi->requested)
+		return -ENODEV;
+	if (chip->ops->request) {
+		ret = chip->ops->request(chip, gpio - chip->base);
+		if (ret)
+			return ret;
+	}
+
+	gi->requested = true;
+	gi->label = xstrdup(label);
+
+	return 0;
+}
+
+void gpio_free(unsigned gpio)
+{
+	struct gpio_info *gi = &gpio_desc[gpio];
+	struct gpio_chip *chip = gi->chip;
+
+	if (!gpio_is_valid(gpio))
+		return;
+	if (!chip)
+		return;
+	if (!gi->requested)
+		return;
+	if (chip->ops->free)
+		chip->ops->free(chip, gpio - chip->base);
+
+	gi->requested = false;
+	free(gi->label);
+}
 
 void gpio_set_value(unsigned gpio, int value)
 {
-	struct gpio_chip *chip = gpio_desc[gpio];
+	struct gpio_info *gi = &gpio_desc[gpio];
+	struct gpio_chip *chip = gi->chip;
 
 	if (!gpio_is_valid(gpio))
 		return;
 	if (!chip)
 		return;
+	if (!gi->requested && gpio_request(gpio, "gpio"))
+		return;
 	if (!chip->ops->set)
 		return;
 	chip->ops->set(chip, gpio - chip->base, value);
@@ -22,12 +74,19 @@ EXPORT_SYMBOL(gpio_set_value);
 
 int gpio_get_value(unsigned gpio)
 {
-	struct gpio_chip *chip = gpio_desc[gpio];
+	struct gpio_info *gi = &gpio_desc[gpio];
+	struct gpio_chip *chip = gi->chip;
+	int ret;
 
 	if (!gpio_is_valid(gpio))
 		return -EINVAL;
 	if (!chip)
 		return -ENODEV;
+	if (!gi->requested) {
+		ret = gpio_request(gpio, "gpio");
+		if (ret)
+			return ret;
+	}
 	if (!chip->ops->get)
 		return -ENOSYS;
 	return chip->ops->get(chip, gpio - chip->base);
@@ -36,12 +95,19 @@ EXPORT_SYMBOL(gpio_get_value);
 
 int gpio_direction_output(unsigned gpio, int value)
 {
-	struct gpio_chip *chip = gpio_desc[gpio];
+	struct gpio_info *gi = &gpio_desc[gpio];
+	struct gpio_chip *chip = gi->chip;
+	int ret;
 
 	if (!gpio_is_valid(gpio))
 		return -EINVAL;
 	if (!chip)
 		return -ENODEV;
+	if (!gi->requested) {
+		ret = gpio_request(gpio, "gpio");
+		if (ret)
+			return ret;
+	}
 	if (!chip->ops->direction_output)
 		return -ENOSYS;
 	return chip->ops->direction_output(chip, gpio - chip->base, value);
@@ -50,12 +116,19 @@ EXPORT_SYMBOL(gpio_direction_output);
 
 int gpio_direction_input(unsigned gpio)
 {
-	struct gpio_chip *chip = gpio_desc[gpio];
+	struct gpio_info *gi = &gpio_desc[gpio];
+	struct gpio_chip *chip = gi->chip;
+	int ret;
 
 	if (!gpio_is_valid(gpio))
 		return -EINVAL;
 	if (!chip)
 		return -ENODEV;
+	if (!gi->requested) {
+		ret = gpio_request(gpio, "gpio");
+		if (ret)
+			return ret;
+	}
 	if (!chip->ops->direction_input)
 		return -ENOSYS;
 	return chip->ops->direction_input(chip, gpio - chip->base);
@@ -72,7 +145,7 @@ static int gpiochip_find_base(int start, int ngpio)
 		start = 0;
 
 	for (i = start; i < ARCH_NR_GPIOS; i++) {
-		struct gpio_chip *chip = gpio_desc[i];
+		struct gpio_chip *chip = gpio_desc[i].chip;
 
 		if (!chip) {
 			spare++;
@@ -107,7 +180,7 @@ int gpiochip_add(struct gpio_chip *chip)
 	list_add_tail(&chip->list, &chip_list);
 
 	for (i = chip->base; i < chip->base + chip->ngpio; i++)
-		gpio_desc[i] = chip;
+		gpio_desc[i].chip = chip;
 
 	return 0;
 }
include/gpio.h +7 -0
index 9fb11c3..eedb980 100644
@@ -3,6 +3,7 @@
 
 #include <asm/gpio.h>
 
+#ifndef CONFIG_GPIOLIB
 static inline int gpio_request(unsigned gpio, const char *label)
 {
        return 0;
@@ -11,10 +12,16 @@ static inline int gpio_request(unsigned gpio, const char *label)
 static inline void gpio_free(unsigned gpio)
 {
 }
+#else
+int gpio_request(unsigned gpio, const char *label);
+void gpio_free(unsigned gpio);
+#endif
 
 struct gpio_chip;
 
 struct gpio_ops {
+	int (*request)(struct gpio_chip *chip, unsigned offset);
+	void (*free)(struct gpio_chip *chip, unsigned offset);
 	int (*direction_input)(struct gpio_chip *chip, unsigned offset);
 	int (*direction_output)(struct gpio_chip *chip, unsigned offset, int value);
 	int (*get)(struct gpio_chip *chip, unsigned offset);
Signature · 7 lines
1.7.10.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw

PATCH 2/2 gpiolib: add command to dump the current gpio status

This will allow to known which gpio is requested by what

---
 drivers/gpio/gpio.c |   37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
drivers/gpio/gpio.c +37 -0
index e29f0ba..4c68819 100644
@@ -1,4 +1,6 @@
 #include <common.h>
+#include <command.h>
+#include <complete.h>
 #include <gpio.h>
 #include <errno.h>
 #include <malloc.h>
@@ -196,3 +198,38 @@ int gpio_get_num(struct device_d *dev, int gpio)
 
 	return -ENODEV;
 }
+
+#ifdef CONFIG_CMD_GPIO
+static int do_gpiolib(int argc, char *argv[])
+{
+	int i;
+
+	printf("gpiolib: gpio lists\n");
+	printf("%*crequested  label\n", 11, ' ');
+
+	for (i = 0; i < ARCH_NR_GPIOS; i++) {
+		struct gpio_info *gi = &gpio_desc[i];
+
+		if (!gi->chip)
+			continue;
+
+		printf("gpio %*d: %*s  %s\n", 4,
+			i, 9, gi->requested ? "true" : "false",
+			gi->label ? gi->label : "");
+	}
+
+	return 0;
+}
+
+BAREBOX_CMD_HELP_START(gpiolib)
+BAREBOX_CMD_HELP_USAGE("gpiolib\n")
+BAREBOX_CMD_HELP_SHORT("dump current registered gpio\n");
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(gpiolib)
+	.cmd		= do_gpiolib,
+	.usage		= "dump current registered gpio",
+	BAREBOX_CMD_HELP(cmd_gpiolib_help)
+	BAREBOX_CMD_COMPLETE(empty_complete)
+BAREBOX_CMD_END
+#endif
Signature · 7 lines
1.7.10.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw

Re: [PATCH 1/2] gpiolib: add gpio_request and gpio_free support

In reply to Jean-Christophe PLAGNIOL-VILLARD
Quoted 33 lines · Jean-Christophe PLAGNIOL-VILLARD

as today if no request or free provided do not complain

if the gpio is not request auto requested at first use

---
 drivers/gpio/gpio.c |   87 ++++++++++++++++++++++++++++++++++++++++++++++-----
 include/gpio.h      |    7 +++++
 2 files changed, 87 insertions(+), 7 deletions(-)
drivers/gpio/gpio.c +11 -1
index 042a062..e29f0ba 100644
@@ -1,19 +1,71 @@
 #include <common.h>
 #include <gpio.h>
 #include <errno.h>
+#include <malloc.h>
 
 static LIST_HEAD(chip_list);
 
-static struct gpio_chip *gpio_desc[ARCH_NR_GPIOS];
+struct gpio_info {
+	struct gpio_chip *chip;
+	bool requested;
+	char *label;
+};
+
+static struct gpio_info gpio_desc[ARCH_NR_GPIOS];
+
+int gpio_request(unsigned gpio, const char *label)
+{

drivers/gpio/gpio.c line 27

Quoted 10 lines
drivers/gpio/gpio.c +10 -0
+	struct gpio_info *gi = &gpio_desc[gpio];
+	struct gpio_chip *chip = gi->chip;
+	int ret;
+
+	if (!gpio_is_valid(gpio))
+		return -EINVAL;
+	if (!chip)
+		return -EINVAL;
+	if (gi->requested)
+		return -ENODEV;

-EBUSY?

drivers/gpio/gpio.c line 68

Quoted 12 lines
drivers/gpio/gpio.c +4 -1
 void gpio_set_value(unsigned gpio, int value)
 {
-	struct gpio_chip *chip = gpio_desc[gpio];
+	struct gpio_info *gi = &gpio_desc[gpio];
+	struct gpio_chip *chip = gi->chip;
 
 	if (!gpio_is_valid(gpio))
 		return;
 	if (!chip)
 		return;
+	if (!gi->requested && gpio_request(gpio, "gpio"))
+		return;

Could you factor out this into a gpio_ensure_requested function? This way we would have a single point where the implicit gpio_request calls are handled.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#4 lore raw

Re: [PATCH 1/2] gpiolib: add gpio_request and gpio_free support

Quoted 53 lines · Sascha Hauer
Quoted 33 lines · Jean-Christophe PLAGNIOL-VILLARD

as today if no request or free provided do not complain

if the gpio is not request auto requested at first use

---
 drivers/gpio/gpio.c |   87 ++++++++++++++++++++++++++++++++++++++++++++++-----
 include/gpio.h      |    7 +++++
 2 files changed, 87 insertions(+), 7 deletions(-)
drivers/gpio/gpio.c +11 -1
index 042a062..e29f0ba 100644
@@ -1,19 +1,71 @@
 #include <common.h>
 #include <gpio.h>
 #include <errno.h>
+#include <malloc.h>
 
 static LIST_HEAD(chip_list);
 
-static struct gpio_chip *gpio_desc[ARCH_NR_GPIOS];
+struct gpio_info {
+	struct gpio_chip *chip;
+	bool requested;
+	char *label;
+};
+
+static struct gpio_info gpio_desc[ARCH_NR_GPIOS];
+
+int gpio_request(unsigned gpio, const char *label)
+{

drivers/gpio/gpio.c line 27

Quoted 10 lines
drivers/gpio/gpio.c +10 -0
+	struct gpio_info *gi = &gpio_desc[gpio];
+	struct gpio_chip *chip = gi->chip;
+	int ret;
+
+	if (!gpio_is_valid(gpio))
+		return -EINVAL;
+	if (!chip)
+		return -EINVAL;
+	if (gi->requested)
+		return -ENODEV;

-EBUSY?

Quoted 6 lines
drivers/gpio/gpio.c +2 -1
 void gpio_set_value(unsigned gpio, int value)
 {
-	struct gpio_chip *chip = gpio_desc[gpio];
+	struct gpio_info *gi = &gpio_desc[gpio];
+	struct gpio_chip *chip = gi->chip;
 
Quoted 10 lines

drivers/gpio/gpio.c line 68

Quoted 6 lines
drivers/gpio/gpio.c +2 -0
 	if (!gpio_is_valid(gpio))
 		return;
 	if (!chip)
 		return;
+	if (!gi->requested && gpio_request(gpio, "gpio"))
+		return;

Could you factor out this into a gpio_ensure_requested function? This way we would have a single point where the implicit gpio_request calls are handled.

ok

Best Regards,
J.

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox