Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 22 Dec 2014 16:12:56 +0000 (UTC)
From:      Luiz Otavio O Souza <loos@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r276059 - head/sys/arm/ti
Message-ID:  <201412221612.sBMGCuvf015958@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: loos
Date: Mon Dec 22 16:12:55 2014
New Revision: 276059
URL: https://svnweb.freebsd.org/changeset/base/276059

Log:
  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)

Modified:
  head/sys/arm/ti/ti_gpio.c

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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201412221612.sBMGCuvf015958>