gpiolib: Add "unknown" direction support
Subsystems (2)
PATCH v4 1/4 gpiolib: Add "unknown" direction support
Previously, gpiolib would unconditionally flag all GPIO pins as inputs, regardless of their true state. This resulted in all GPIO output pins initially being incorrectly identified as "input" in the GPIO sysfs.
Since the direction of GPIOs is not known prior to having their direction set, instead set the default direction to "unknown" to prevent user confusion. A pin with an "unknown" direction can not be written or read via sysfs; it must first be configured as an input or output before it can be used.
While we're playing with the direction flag in/out defines, rename them to a more descriptive FLAG_DIR_* format.
---
Change since V1:
- This patch is new and is based on feedback from Alan to support a new "unknown" direction.
- Rename FLAG_IS_OUT to FLAG_DIR_OUT, and add FLAG_DIR_IN
Change since V2:
- None
Change since V3:
- None
drivers/gpio/gpiolib.c | 91 ++++++++++++++++++++++++++--------------------- 1 files changed, 50 insertions(+), 41 deletions(-)
drivers/gpio/gpiolib.c +50 -41
@@ -49,13 +49,14 @@ struct gpio_desc {unsigned long flags;/* flag symbols are bit numbers */ #define FLAG_REQUESTED 0 -#define FLAG_IS_OUT 1 -#define FLAG_RESERVED 2 -#define FLAG_EXPORT 3/* protected by sysfs_lock */ -#define FLAG_SYSFS 4/* exported via /sys/class/gpio/control */ -#define FLAG_TRIG_FALL 5/* trigger on falling edge */ -#define FLAG_TRIG_RISE 6/* trigger on rising edge */ -#define FLAG_ACTIVE_LOW 7/* sysfs value has active low */ +#define FLAG_DIR_OUT 1/* pin is an output */ +#define FLAG_DIR_IN 2/* pin is an input */ +#define FLAG_RESERVED 3 +#define FLAG_EXPORT 4/* protected by sysfs_lock */ +#define FLAG_SYSFS 5/* exported via /sys/class/gpio/control */ +#define FLAG_TRIG_FALL 6/* trigger on falling edge */ +#define FLAG_TRIG_RISE 7/* trigger on rising edge */ +#define FLAG_ACTIVE_LOW 8/* sysfs value has active low */ #define ID_SHIFT 16/* add new flags before this one */
@@ -220,15 +221,22 @@ static ssize_t gpio_direction_show(struct device *dev, {const struct gpio_desc *desc =dev_get_drvdata (dev);ssize_t status; +const char *dir_str;mutex_lock (&sysfs_lock); -if (!test_bit (FLAG_EXPORT , &desc->flags)) +if (!test_bit (FLAG_EXPORT , &desc->flags)) { status = -EIO ; -else - status =sprintf (buf,"%s\n" , -test_bit (FLAG_IS_OUT , &desc->flags) - ?"out" :"in" ); + }else { +if (test_bit (FLAG_DIR_OUT , &desc->flags)) + dir_str ="out" ; +else if (test_bit (FLAG_DIR_IN , &desc->flags)) + dir_str ="in" ; +else + dir_str ="unknown" ; + + status =sprintf (buf,"%s\n" , dir_str); + }mutex_unlock (&sysfs_lock);return status;
@@ -272,6 +280,9 @@ static ssize_t gpio_value_show(struct device *dev,if (!test_bit (FLAG_EXPORT , &desc->flags)) { status = -EIO ; + }else if ((!test_bit (FLAG_DIR_OUT , &desc->flags)) && + (!test_bit (FLAG_DIR_IN , &desc->flags))) { + status = -EPERM ; }else {int value;
@@ -297,7 +308,7 @@ static ssize_t gpio_value_store(struct device *dev,if (!test_bit (FLAG_EXPORT , &desc->flags)) status = -EIO ; -else if (!test_bit (FLAG_IS_OUT , &desc->flags)) +else if (!test_bit (FLAG_DIR_OUT , &desc->flags)) status = -EPERM ;else {long value;
@@ -741,7 +752,7 @@ int gpio_export(unsigned gpio, bool direction_may_change)if (!status &&gpio_to_irq (gpio) >=0 && (direction_may_change - || !test_bit (FLAG_IS_OUT , + ||test_bit (FLAG_DIR_IN , &desc->flags))) status =device_create_file (dev, &dev_attr_edge);
@@ -1059,22 +1070,10 @@ int gpiochip_add(struct gpio_chip *chip)break ; } } -if (status ==0 ) { -for (id = base; id < base + chip->ngpio; id++) { +if (status ==0 ) +for (id = base; id < base + chip->ngpio; id++) gpio_desc[id].chip = chip; -/* REVISIT: most hardware initializes GPIOs as -* inputs (often with pullups enabled) so power -* usage is minimized. Linux code should set the -* gpio direction first thing; but until it does, -* we may expose the wrong direction in sysfs. -*/ - gpio_desc[id].flags = !chip->direction_input - ? (1 <<FLAG_IS_OUT ) - :0 ; - } - } -of_gpiochip_add (chip); unlock:
@@ -1402,8 +1401,10 @@ int gpio_direction_input(unsigned gpio) } status = chip->direction_input (chip, gpio); -if (status ==0 ) -clear_bit (FLAG_IS_OUT , &desc->flags); +if (status ==0 ) { +clear_bit (FLAG_DIR_OUT , &desc->flags); +set_bit (FLAG_DIR_IN , &desc->flags); + } lose:return status; fail:
@@ -1455,8 +1456,10 @@ int gpio_direction_output(unsigned gpio, int value) } status = chip->direction_output (chip, gpio, value); -if (status ==0 ) -set_bit (FLAG_IS_OUT , &desc->flags); +if (status ==0 ) { +clear_bit (FLAG_DIR_IN , &desc->flags); +set_bit (FLAG_DIR_OUT , &desc->flags); + } lose:return status; fail:
@@ -1643,21 +1646,27 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)unsigned i;unsigned gpio = chip->base;struct gpio_desc *gdesc = &gpio_desc[gpio]; -int is_out; +const char *dir_str; +int unknown =0 ;for (i =0 ; i < chip->ngpio; i++, gpio++, gdesc++) {if (!test_bit (FLAG_REQUESTED , &gdesc->flags))continue ; - is_out =test_bit (FLAG_IS_OUT , &gdesc->flags); -seq_printf (s," gpio-%-3d (%-20.20s) %s %s" , - gpio, gdesc->label, - is_out ?"out" :"in " , - chip->get - ? (chip->get (chip, i) ?"hi" :"lo" ) - :"? " ); +if (test_bit (FLAG_DIR_IN , &gdesc->flags)) { + dir_str ="in " ; + }else if (test_bit (FLAG_DIR_OUT , &gdesc->flags)) { + dir_str ="out" ; + }else { + dir_str ="? " ; + unknown =1 ; + } + +seq_printf (s," gpio-%-3d (%-20.20s) %s %s" , gpio, gdesc->label, + dir_str, (chip->get && !unknown) ? + (chip->get (chip, i) ?"hi" :"lo" ) :"?" ); -if (!is_out) { +if (test_bit (FLAG_DIR_IN , &gdesc->flags)) {int irq =gpio_to_irq (gpio);struct irq_desc *desc =irq_to_desc (irq);
Signature · 1 line
1.7.0.4
PATCH v4 2/4 gpiolib: Add ability to get GPIO direction
Add a new get_direction() function to the gpio_chip structure. This is useful so that the direction of a GPIO can be determined when its initially exported. Previously, the direction defaulted to "unknown" regardless of the actual configuration of the GPIO.
If a GPIO driver implements get_direction(), it is called in gpio_request() to set the initial direction of the GPIO accurately.
---
Changes since v1:
- Add support for "unknown" direction
Changes since v2:
Based on Wolfram's feedback:
- Use GPIOF_DIR_* flags as returns from get_direction()
- Call spin_lock_irqsave() to before setting flags
Changes since v3:
- Removed "pin" references from the commit message
- Added might_sleep() call
- Made the direction always be set in gpio_request()
drivers/gpio/gpiolib.c | 25 +++++++++++++++++++++++++ include/asm-generic/gpio.h | 4 ++++ 2 files changed, 29 insertions(+), 0 deletions(-)
drivers/gpio/gpiolib.c +25 -0
@@ -1174,6 +1174,7 @@ int gpio_request(unsigned gpio, const char *label)struct gpio_desc *desc;struct gpio_chip *chip;int status = -EINVAL ; +int dir =-1 ;/* Default to unknown direction */ unsigned long flags;spin_lock_irqsave (&gpio_lock, flags);
@@ -1214,6 +1215,30 @@ int gpio_request(unsigned gpio, const char *label) } } +if (chip->get_direction) { +/* chip->get_direction may sleep */ +spin_unlock_irqrestore (&gpio_lock, flags); +might_sleep_if (chip->can_sleep); + dir = chip->get_direction (chip, gpio - chip->base); +spin_lock_irqsave (&gpio_lock, flags); + } + +switch (dir) { +case GPIOF_DIR_OUT : +set_bit (FLAG_DIR_OUT , &desc->flags); +clear_bit (FLAG_DIR_IN , &desc->flags); +break ; +case GPIOF_DIR_IN : +set_bit (FLAG_DIR_IN , &desc->flags); +clear_bit (FLAG_DIR_OUT , &desc->flags); +break ; +default : +/* Direction isn't known */ +clear_bit (FLAG_DIR_OUT , &desc->flags); +clear_bit (FLAG_DIR_IN , &desc->flags); +break ; + } + done:if (status) pr_debug("gpio_request: gpio-%d (%s) status %d\n",
include/asm-generic/gpio.h +4 -0
@@ -57,6 +57,8 @@ struct device_node;* @direction_input: configures signal "offset" as input, or returns error * @get: returns value for signal "offset"; for output signals this * returns either the value actually sensed, or zero +* @get_direction: optional hook to determine if a GPIO signal is configured +* as an input, output, or unknown * @direction_output: configures signal "offset" as output, or returns error * @set: assigns output value for signal "offset" * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
@@ -101,6 +103,8 @@ struct gpio_chip { unsigned offset);int (*get)(struct gpio_chip *chip,unsigned offset); +int (*get_direction)(struct gpio_chip *chip, +unsigned offset);int (*direction_output)(struct gpio_chip *chip,unsigned offset,int value); int (*set_debounce)(struct gpio_chip *chip,
Signature · 1 line
1.7.0.4
PATCH v4 3/4 gpio: pca953x: Implement get_direction() hook
In reply to Peter TyserAdding the get_direction() function allows the accurate GPIO pin direction data to be shown in sysfs. Previously, the direction was was always initialized to 'unknown'.
---
Changes since v1:
- None
Changes since v2:
- Use GPIOF_DIR_* defines
Changes since v3:
- None
drivers/gpio/pca953x.c | 10 ++++++++++ 1 files changed, 10 insertions(+), 0 deletions(-)
drivers/gpio/pca953x.c +10 -0
@@ -168,6 +168,15 @@ exit:return ret; } +static int pca953x_gpio_get_direction (struct gpio_chip *gc,unsigned off) +{ +struct pca953x_chip *chip; + + chip = container_of(gc, struct pca953x_chip, gpio_chip); + +return chip->reg_direction & (1u << off) ?GPIOF_DIR_IN :GPIOF_DIR_OUT ; +} +static int pca953x_gpio_get_value (struct gpio_chip *gc,unsigned off) {struct pca953x_chip *chip;
@@ -221,6 +230,7 @@ static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios) gc->direction_input = pca953x_gpio_direction_input; gc->direction_output = pca953x_gpio_direction_output; + gc->get_direction = pca953x_gpio_get_direction; gc->get = pca953x_gpio_get_value; gc->set = pca953x_gpio_set_value; gc->can_sleep =1 ;
Signature · 1 line
1.7.0.4
PATCH v4 4/4 gpio: Add support for Intel ICHx/3100/Series[56] GPIO
In reply to Peter TyserThis driver works on many Intel chipsets, including the ICH6, ICH7, ICH8, ICH9, ICH10, 3100, Series 5/3400 (Ibex Peak), Series 6/C200 (Cougar Point).
Additional Intel chipsets should be easily supported if needed, eg the ICH1-5, EP80579, etc.
Tested on a QM57 (Ibex Peak) and 3100.
---
Changes since v1:
- Fixed grammar in Kconfig menu entry
Changes since v2:
- Use GPIOF_DIR_* defines
Changes since v3:
- Added Vincent's signed-off/tested-by
- Added Tigerpoint PCI ID
MAINTAINERS | 5 + drivers/gpio/Kconfig | 11 + drivers/gpio/Makefile | 1 + drivers/gpio/ichx_gpio.c | 551 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 568 insertions(+), 0 deletions(-) create mode 100644 drivers/gpio/ichx_gpio.c
MAINTAINERS +5 -0
@@ -3108,6 +3108,11 @@ W: http://www.developer.ibm.com/welcome/netfinity/serveraid.html S: Supported F: drivers/scsi/ips.* +ICHX GPIO DRIVER +M: Peter Tyser <[email protected]> +S: Maintained +F: drivers/gpio/ichx_gpio.c + IDE SUBSYSTEM M: "David S. Miller" <[email protected]> L: [email protected]
drivers/gpio/Kconfig +11 -0
@@ -319,6 +319,17 @@ config GPIO_BT8XX If unsure, say N. +config GPIO_ICHX + tristate "Intel ICHx GPIO" + depends on PCI && GPIOLIB && X86 + help + Say yes here to support the GPIO functionality of a number of Intel + ICH-based chipsets. Currently supported devices: ICH6, ICH7, ICH8 + ICH9, ICH10, Series 5/3400 (eg Ibex Peak), Series 6/C200 (eg + Cougar Point), and 3100. + + If unsure, say N. + config GPIO_LANGWELL bool "Intel Langwell/Penwell GPIO support" depends on PCI
drivers/gpio/Makefile +1 -0
@@ -11,6 +11,7 @@ obj-$(CONFIG_GPIOLIB) += gpiolib.o obj-$(CONFIG_GPIO_ADP5520) += adp5520-gpio.o obj-$(CONFIG_GPIO_ADP5588) += adp5588-gpio.o obj-$(CONFIG_GPIO_BASIC_MMIO) += basic_mmio_gpio.o +obj-$(CONFIG_GPIO_ICHX) += ichx_gpio.o obj-$(CONFIG_GPIO_LANGWELL) += langwell_gpio.o obj-$(CONFIG_GPIO_MAX730X) += max730x.o obj-$(CONFIG_GPIO_MAX7300) += max7300.o
drivers/gpio/ichx_gpio.c new file +551 -0
551 lines not on this page · raw message
Signature · 1 line
1.7.0.4
Re: [PATCH v4 4/4] gpio: Add support for Intel ICHx/3100/Series[56] GPIO
Quoted 3 lines · Peter Tyser
This driver works on many Intel chipsets, including the ICH6, ICH7, ICH8, ICH9, ICH10, 3100, Series 5/3400 (Ibex Peak), Series 6/C200 (Cougar Point).
Hi Peter.
just trivia...
drivers/gpio/ichx_gpio.c
Quoted 1 line
drivers/gpio/ichx_gpio.c +0
-0
[]
drivers/gpio/ichx_gpio.c line 2
Quoted 3 lines
drivers/gpio/ichx_gpio.c +2
-0@@ -0,0 +1,551 @@ +/* +* Intel ICH6-10, Series 5 and 6 GPIO driver
[]
drivers/gpio/ichx_gpio.c line 4
Quoted 2 lines
drivers/gpio/ichx_gpio.c +2
-0+*/ +
You could use
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
before any #include
so that the uses of
printk(KERN_<level> "%s: foo...\n", DRV_NAME, bar...);
can be
pr_<level>("foo...\n", bar...);drivers/gpio/ichx_gpio.c line 24
Quoted 4 lines
drivers/gpio/ichx_gpio.c +4
-0+#include <linux/module.h> +#include <linux/pci.h> +#include <linux/gpio.h> +#include <linux/platform_device.h>
[]
drivers/gpio/ichx_gpio.c line 96
Quoted 1 line
drivers/gpio/ichx_gpio.c +1
-0+static int modparam_gpiobase =-1 /* dynamic */ ;
Pretty unusual commenting style and too easy to lose sight of the terminating semicolon.
It's more usually written as:
static int modparam_gpiobase = -1; /* dynamic */
drivers/gpio/ichx_gpio.c line 258
Quoted 4 lines
drivers/gpio/ichx_gpio.c +4
-0+static int __devinit ichx_gpio_init (struct pci_dev *pdev, +const struct pci_device_id *ent, +struct platform_device *dev) +{
[]
drivers/gpio/ichx_gpio.c line 270
Quoted 2 lines
drivers/gpio/ichx_gpio.c +2
-0+if (!ichx_priv.gpio_base) { +printk (KERN_ERR "%s: GPIO BAR not enumerated\n" ,DRV_NAME );
print->pr_<level> like:
pr_err("GPIO BAR not enumerated\n");drivers/gpio/ichx_gpio.c line 285
Quoted 7 lines
drivers/gpio/ichx_gpio.c +7
-0+if (ichx_priv.desc->gpe0_sts_ofs) { +pci_read_config_dword (pdev,PCI_ICHX_ACPI_BAR , + (u32 *)&ichx_priv.pm_base); + ichx_priv.pm_base &=PCI_BASE_ADDRESS_IO_MASK ; +if (!ichx_priv.pm_base) +printk (KERN_WARNING "%s: ACPI BAR not enumerated, " +"GPI 0 - 15 unusable\n" ,DRV_NAME );
pr_warn("ACPI BAR not enumerated, GPI 0 - 15 unusable\n");and so forth for all the printks.
One other typo:
drivers/gpio/ichx_gpio.c line 219
Quoted 6 lines
drivers/gpio/ichx_gpio.c +5
-0+static int ich6_gpio_request (struct gpio_chip *chip,unsigned nr) +{ +/* +* Fixups for bits 16 and 17 are necessary on the Intel ICH6/3100 +* brdige as they are controlled by USE register bits 0 and 1. See
bridge