Simplify the use of locks where possible, remove the locking when it is not

required.

Simplify the code a little bit.

Reviewed by:	andrew (previous version)
This commit is contained in:
loos 2014-12-22 16:12:55 +00:00
parent cca3a2b04d
commit 922c7aafd7

View File

@ -290,7 +290,7 @@ ti_gpio_intr_clr(struct ti_gpio_softc *sc, unsigned int bank, uint32_t mask)
* *
* *
* LOCKING: * LOCKING:
* Internally locks the context * No locking required, returns static data.
* *
* RETURNS: * RETURNS:
* Returns 0 on success otherwise an error code * Returns 0 on success otherwise an error code
@ -302,8 +302,6 @@ ti_gpio_pin_max(device_t dev, int *maxpin)
unsigned int i; unsigned int i;
unsigned int banks = 0; unsigned int banks = 0;
TI_GPIO_LOCK(sc);
/* Calculate how many valid banks we have and then multiply that by 32 to /* Calculate how many valid banks we have and then multiply that by 32 to
* give use the total number of pins. * give use the total number of pins.
*/ */
@ -314,8 +312,6 @@ ti_gpio_pin_max(device_t dev, int *maxpin)
*maxpin = (banks * PINS_PER_BANK) - 1; *maxpin = (banks * PINS_PER_BANK) - 1;
TI_GPIO_UNLOCK(sc);
return (0); return (0);
} }
@ -332,7 +328,7 @@ ti_gpio_pin_max(device_t dev, int *maxpin)
* - GPIO_PIN_PULLDOWN * - GPIO_PIN_PULLDOWN
* *
* LOCKING: * LOCKING:
* Internally locks the context * No locking required, returns static data.
* *
* RETURNS: * RETURNS:
* Returns 0 on success otherwise an error code * Returns 0 on success otherwise an error code
@ -343,19 +339,13 @@ ti_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
struct ti_gpio_softc *sc = device_get_softc(dev); struct ti_gpio_softc *sc = device_get_softc(dev);
uint32_t bank = (pin / PINS_PER_BANK); uint32_t bank = (pin / PINS_PER_BANK);
TI_GPIO_LOCK(sc);
/* Sanity check the pin number is valid */ /* Sanity check the pin number is valid */
if ((bank >= ti_max_gpio_banks()) || (sc->sc_mem_res[bank] == NULL)) { if ((bank >= ti_max_gpio_banks()) || (sc->sc_mem_res[bank] == NULL))
TI_GPIO_UNLOCK(sc);
return (EINVAL); return (EINVAL);
}
*caps = (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT |GPIO_PIN_PULLUP | *caps = (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_PULLUP |
GPIO_PIN_PULLDOWN); GPIO_PIN_PULLDOWN);
TI_GPIO_UNLOCK(sc);
return (0); return (0);
} }
@ -381,17 +371,13 @@ ti_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
struct ti_gpio_softc *sc = device_get_softc(dev); struct ti_gpio_softc *sc = device_get_softc(dev);
uint32_t bank = (pin / PINS_PER_BANK); uint32_t bank = (pin / PINS_PER_BANK);
TI_GPIO_LOCK(sc);
/* Sanity check the pin number is valid */ /* Sanity check the pin number is valid */
if ((bank >= ti_max_gpio_banks()) || (sc->sc_mem_res[bank] == NULL)) { if ((bank >= ti_max_gpio_banks()) || (sc->sc_mem_res[bank] == NULL))
TI_GPIO_UNLOCK(sc);
return (EINVAL); return (EINVAL);
}
/* Get the current pin state */ /* Get the current pin state */
TI_GPIO_LOCK(sc);
TI_GPIO_GET_FLAGS(dev, pin, flags); TI_GPIO_GET_FLAGS(dev, pin, flags);
TI_GPIO_UNLOCK(sc); TI_GPIO_UNLOCK(sc);
return (0); return (0);
@ -407,7 +393,7 @@ ti_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
* of the pin. * of the pin.
* *
* LOCKING: * LOCKING:
* Internally locks the context * No locking required, returns static data.
* *
* RETURNS: * RETURNS:
* Returns 0 on success otherwise an error code * Returns 0 on success otherwise an error code
@ -418,20 +404,14 @@ ti_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
struct ti_gpio_softc *sc = device_get_softc(dev); struct ti_gpio_softc *sc = device_get_softc(dev);
uint32_t bank = (pin / PINS_PER_BANK); uint32_t bank = (pin / PINS_PER_BANK);
TI_GPIO_LOCK(sc);
/* Sanity check the pin number is valid */ /* Sanity check the pin number is valid */
if ((bank >= ti_max_gpio_banks()) || (sc->sc_mem_res[bank] == NULL)) { if ((bank >= ti_max_gpio_banks()) || (sc->sc_mem_res[bank] == NULL))
TI_GPIO_UNLOCK(sc);
return (EINVAL); return (EINVAL);
}
/* Set a very simple name */ /* Set a very simple name */
snprintf(name, GPIOMAXNAME, "gpio_%u", pin); snprintf(name, GPIOMAXNAME, "gpio_%u", pin);
name[GPIOMAXNAME - 1] = '\0'; name[GPIOMAXNAME - 1] = '\0';
TI_GPIO_UNLOCK(sc);
return (0); return (0);
} }
@ -460,30 +440,26 @@ ti_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
struct ti_gpio_softc *sc = device_get_softc(dev); struct ti_gpio_softc *sc = device_get_softc(dev);
uint32_t bank = (pin / PINS_PER_BANK); uint32_t bank = (pin / PINS_PER_BANK);
uint32_t mask = (1UL << (pin % PINS_PER_BANK)); uint32_t mask = (1UL << (pin % PINS_PER_BANK));
uint32_t reg_val; uint32_t oe;
TI_GPIO_LOCK(sc);
/* Sanity check the pin number is valid */ /* Sanity check the pin number is valid */
if ((bank >= ti_max_gpio_banks()) || (sc->sc_mem_res[bank] == NULL)) { if ((bank >= ti_max_gpio_banks()) || (sc->sc_mem_res[bank] == NULL))
TI_GPIO_UNLOCK(sc);
return (EINVAL); return (EINVAL);
}
/* Set the GPIO mode and state */ /* Set the GPIO mode and state */
TI_GPIO_LOCK(sc);
if (TI_GPIO_SET_FLAGS(dev, pin, flags) != 0) { if (TI_GPIO_SET_FLAGS(dev, pin, flags) != 0) {
TI_GPIO_UNLOCK(sc); TI_GPIO_UNLOCK(sc);
return (EINVAL); return (EINVAL);
} }
/* If configuring as an output set the "output enable" bit */ /* If configuring as an output set the "output enable" bit */
reg_val = ti_gpio_read_4(sc, bank, TI_GPIO_OE); oe = ti_gpio_read_4(sc, bank, TI_GPIO_OE);
if (flags & GPIO_PIN_INPUT) if (flags & GPIO_PIN_INPUT)
reg_val |= mask; oe |= mask;
else else
reg_val &= ~mask; oe &= ~mask;
ti_gpio_write_4(sc, bank, TI_GPIO_OE, reg_val); ti_gpio_write_4(sc, bank, TI_GPIO_OE, oe);
TI_GPIO_UNLOCK(sc); TI_GPIO_UNLOCK(sc);
return (0); return (0);
@ -509,18 +485,18 @@ ti_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
struct ti_gpio_softc *sc = device_get_softc(dev); struct ti_gpio_softc *sc = device_get_softc(dev);
uint32_t bank = (pin / PINS_PER_BANK); uint32_t bank = (pin / PINS_PER_BANK);
uint32_t mask = (1UL << (pin % PINS_PER_BANK)); uint32_t mask = (1UL << (pin % PINS_PER_BANK));
uint32_t reg;
TI_GPIO_LOCK(sc);
/* Sanity check the pin number is valid */ /* Sanity check the pin number is valid */
if ((bank >= ti_max_gpio_banks()) || (sc->sc_mem_res[bank] == NULL)) { if ((bank >= ti_max_gpio_banks()) || (sc->sc_mem_res[bank] == NULL))
TI_GPIO_UNLOCK(sc);
return (EINVAL); return (EINVAL);
}
ti_gpio_write_4(sc, bank, (value == GPIO_PIN_LOW) ? TI_GPIO_CLEARDATAOUT
: TI_GPIO_SETDATAOUT, mask);
TI_GPIO_LOCK(sc);
if (value == GPIO_PIN_LOW)
reg = TI_GPIO_CLEARDATAOUT;
else
reg = TI_GPIO_SETDATAOUT;
ti_gpio_write_4(sc, bank, reg, mask);
TI_GPIO_UNLOCK(sc); TI_GPIO_UNLOCK(sc);
return (0); return (0);
@ -547,25 +523,23 @@ ti_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *value)
struct ti_gpio_softc *sc = device_get_softc(dev); struct ti_gpio_softc *sc = device_get_softc(dev);
uint32_t bank = (pin / PINS_PER_BANK); uint32_t bank = (pin / PINS_PER_BANK);
uint32_t mask = (1UL << (pin % PINS_PER_BANK)); uint32_t mask = (1UL << (pin % PINS_PER_BANK));
uint32_t val = 0; uint32_t oe, reg;
TI_GPIO_LOCK(sc);
/* Sanity check the pin number is valid */ /* Sanity check the pin number is valid */
if ((bank >= ti_max_gpio_banks()) || (sc->sc_mem_res[bank] == NULL)) { if ((bank >= ti_max_gpio_banks()) || (sc->sc_mem_res[bank] == NULL))
TI_GPIO_UNLOCK(sc);
return (EINVAL); return (EINVAL);
}
/* Sanity check the pin is not configured as an output */ /*
val = ti_gpio_read_4(sc, bank, TI_GPIO_OE); * Return data from output latch when set as output and from the
* input register otherwise.
/* Read the value on the pin */ */
if (val & mask) TI_GPIO_LOCK(sc);
*value = (ti_gpio_read_4(sc, bank, TI_GPIO_DATAIN) & mask) ? 1 : 0; oe = ti_gpio_read_4(sc, bank, TI_GPIO_OE);
if (oe & mask)
reg = TI_GPIO_DATAIN;
else else
*value = (ti_gpio_read_4(sc, bank, TI_GPIO_DATAOUT) & mask) ? 1 : 0; reg = TI_GPIO_DATAOUT;
*value = (ti_gpio_read_4(sc, bank, reg) & mask) ? 1 : 0;
TI_GPIO_UNLOCK(sc); TI_GPIO_UNLOCK(sc);
return (0); return (0);
@ -589,23 +563,20 @@ ti_gpio_pin_toggle(device_t dev, uint32_t pin)
struct ti_gpio_softc *sc = device_get_softc(dev); struct ti_gpio_softc *sc = device_get_softc(dev);
uint32_t bank = (pin / PINS_PER_BANK); uint32_t bank = (pin / PINS_PER_BANK);
uint32_t mask = (1UL << (pin % PINS_PER_BANK)); uint32_t mask = (1UL << (pin % PINS_PER_BANK));
uint32_t val; uint32_t reg, val;
TI_GPIO_LOCK(sc);
/* Sanity check the pin number is valid */ /* Sanity check the pin number is valid */
if ((bank >= ti_max_gpio_banks()) || (sc->sc_mem_res[bank] == NULL)) { if ((bank >= ti_max_gpio_banks()) || (sc->sc_mem_res[bank] == NULL))
TI_GPIO_UNLOCK(sc);
return (EINVAL); return (EINVAL);
}
/* Toggle the pin */ /* Toggle the pin */
TI_GPIO_LOCK(sc);
val = ti_gpio_read_4(sc, bank, TI_GPIO_DATAOUT); val = ti_gpio_read_4(sc, bank, TI_GPIO_DATAOUT);
if (val & mask) if (val & mask)
ti_gpio_write_4(sc, bank, TI_GPIO_CLEARDATAOUT, mask); reg = TI_GPIO_CLEARDATAOUT;
else else
ti_gpio_write_4(sc, bank, TI_GPIO_SETDATAOUT, mask); reg = TI_GPIO_SETDATAOUT;
ti_gpio_write_4(sc, bank, reg, mask);
TI_GPIO_UNLOCK(sc); TI_GPIO_UNLOCK(sc);
return (0); return (0);