diff options
-rw-r--r-- | common/gpio.c | 46 | ||||
-rw-r--r-- | include/gpio.h | 10 |
2 files changed, 31 insertions, 25 deletions
diff --git a/common/gpio.c b/common/gpio.c index b9c687fd7c..44b77c337e 100644 --- a/common/gpio.c +++ b/common/gpio.c @@ -63,31 +63,15 @@ static int last_val_changed(int i, int v) void gpio_config_module(enum module_id id, int enable) { - const struct gpio_alt_func *af = gpio_alt_funcs; - int count = gpio_alt_funcs_count; - int i; - - /* Set module's pins to alternate functions */ - for (i = 0; i < count; i++, af++) { - if (id != af->module_id) - continue; /* Pins for some other module */ - - if (!(af->flags & GPIO_DEFAULT)) - gpio_set_flags_by_mask( - af->port, - af->mask, - enable ? af->flags : GPIO_INPUT); - gpio_set_alternate_function( - af->port, - af->mask, - enable ? af->func : -1); - } + /* Set all the alternate functions for this module. */ + gpio_config_pins(id, GPIO_CONFIG_ALL_PORTS, 0, enable); } int gpio_config_pins(enum module_id id, - uint32_t port, uint32_t pin_mask, int enable) + uint32_t port, uint32_t pin_mask, int enable) { const struct gpio_alt_func *af; + int rv = EC_ERROR_INVAL; /* Find pins and set to alternate functions */ for (af = gpio_alt_funcs; af < gpio_alt_funcs + gpio_alt_funcs_count; @@ -95,20 +79,32 @@ int gpio_config_pins(enum module_id id, if (af->module_id != id) continue; /* Pins for some other module */ - if (af->port == port && (af->mask & pin_mask) == pin_mask) { + /* Check to see if the requested port matches. */ + if ((port != GPIO_CONFIG_ALL_PORTS) && (port != af->port)) + continue; + + /* If we don't care which port, enable all applicable pins. */ + if (port == GPIO_CONFIG_ALL_PORTS) + pin_mask = af->mask; + + if ((af->mask & pin_mask) == pin_mask) { if (!(af->flags & GPIO_DEFAULT)) gpio_set_flags_by_mask( af->port, - pin_mask, + (af->mask & pin_mask), enable ? af->flags : GPIO_INPUT); gpio_set_alternate_function( af->port, - pin_mask, + (af->mask & pin_mask), enable ? af->func : -1); - return EC_SUCCESS; + rv = EC_SUCCESS; + /* We're done here if we were just setting one port. */ + if (port != GPIO_CONFIG_ALL_PORTS) + break; } } - return EC_ERROR_INVAL; + + return rv; } void gpio_set_flags(enum gpio_signal signal, int flags) diff --git a/include/gpio.h b/include/gpio.h index 31f68b1c4b..6b76f29d04 100644 --- a/include/gpio.h +++ b/include/gpio.h @@ -10,6 +10,13 @@ #include "common.h" +/* + * GPIO_CONFIG_ALL_PORTS signifies a "don't care" for the GPIO port. This is + * used in gpio_config_pins(). When the port parameter is set to this, the + * pin_mask parameter is ignored. + */ +#define GPIO_CONFIG_ALL_PORTS 0xFFFFFFFF + /* Flag definitions for gpio_info and gpio_alt_func */ /* The following are valid for both gpio_info and gpio_alt_func: */ #define GPIO_OPEN_DRAIN (1 << 0) /* Output type is open-drain */ @@ -129,6 +136,9 @@ void gpio_config_module(enum module_id id, int enable); /** * Enable/disable alternate function for pins * + * Note, you can also configure an entire module by setting the port parameter + * equal to GPIO_CONFIG_ALL_PORTS. + * * @param id module ID of pins * @param port Port of pins * @param pin_mask Bit mask of pins |