Changeset View
Changeset View
Standalone View
Standalone View
head/sys/mips/mediatek/mtk_gpio_v1.c
Show First 20 Lines • Show All 284 Lines • ▼ Show 20 Lines | mtk_gpio_attach(device_t dev) | ||||
} | } | ||||
if (OF_hasprop(node, "ralink,num-gpios") && (OF_getencprop(node, | if (OF_hasprop(node, "ralink,num-gpios") && (OF_getencprop(node, | ||||
"ralink,num-gpios", &num_pins, sizeof(num_pins)) >= 0)) | "ralink,num-gpios", &num_pins, sizeof(num_pins)) >= 0)) | ||||
sc->num_pins = num_pins; | sc->num_pins = num_pins; | ||||
else | else | ||||
sc->num_pins = MTK_GPIO_PINS; | sc->num_pins = MTK_GPIO_PINS; | ||||
for (i = 0; i < num_pins; i++) { | for (i = 0; i < sc->num_pins; i++) { | ||||
sc->pins[i].pin_caps |= GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | | sc->pins[i].pin_caps |= GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | | ||||
GPIO_PIN_INVIN | GPIO_PIN_INVOUT; | GPIO_PIN_INVIN | GPIO_PIN_INVOUT; | ||||
sc->pins[i].intr_polarity = INTR_POLARITY_HIGH; | sc->pins[i].intr_polarity = INTR_POLARITY_HIGH; | ||||
sc->pins[i].intr_trigger = INTR_TRIGGER_EDGE; | sc->pins[i].intr_trigger = INTR_TRIGGER_EDGE; | ||||
snprintf(sc->pins[i].pin_name, GPIOMAXNAME - 1, "gpio%c%d", | snprintf(sc->pins[i].pin_name, GPIOMAXNAME - 1, "gpio%c%d", | ||||
device_get_unit(dev) + 'a', i); | device_get_unit(dev) + 'a', i); | ||||
sc->pins[i].pin_name[GPIOMAXNAME - 1] = '\0'; | sc->pins[i].pin_name[GPIOMAXNAME - 1] = '\0'; | ||||
▲ Show 20 Lines • Show All 137 Lines • ▼ Show 20 Lines | mtk_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value) | ||||
sc = device_get_softc(dev); | sc = device_get_softc(dev); | ||||
ret = 0; | ret = 0; | ||||
if (pin >= sc->num_pins) | if (pin >= sc->num_pins) | ||||
return (EINVAL); | return (EINVAL); | ||||
MTK_GPIO_LOCK(sc); | MTK_GPIO_LOCK(sc); | ||||
if(!(sc->pins[pin].pin_flags & GPIO_PIN_OUTPUT)) { | |||||
ret = EINVAL; | |||||
goto out; | |||||
} | |||||
if (value) | if (value) | ||||
MTK_WRITE_4(sc, GPIO_PIOSET, (1u << pin)); | MTK_WRITE_4(sc, GPIO_PIOSET, (1u << pin)); | ||||
else | else | ||||
MTK_WRITE_4(sc, GPIO_PIORESET, (1u << pin)); | MTK_WRITE_4(sc, GPIO_PIORESET, (1u << pin)); | ||||
out: | |||||
MTK_GPIO_UNLOCK(sc); | MTK_GPIO_UNLOCK(sc); | ||||
return (ret); | return (ret); | ||||
} | } | ||||
static int | static int | ||||
mtk_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val) | mtk_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val) | ||||
{ | { | ||||
struct mtk_gpio_softc *sc; | struct mtk_gpio_softc *sc; | ||||
uint32_t data; | uint32_t data; | ||||
int ret; | int ret; | ||||
sc = device_get_softc(dev); | sc = device_get_softc(dev); | ||||
ret = 0; | ret = 0; | ||||
if (pin >= sc->num_pins) | if (pin >= sc->num_pins) | ||||
return (EINVAL); | return (EINVAL); | ||||
MTK_GPIO_LOCK(sc); | MTK_GPIO_LOCK(sc); | ||||
if(!(sc->pins[pin].pin_flags & GPIO_PIN_INPUT)) { | |||||
ret = EINVAL; | |||||
goto out; | |||||
} | |||||
data = MTK_READ_4(sc, GPIO_PIODATA); | data = MTK_READ_4(sc, GPIO_PIODATA); | ||||
*val = (data & (1u << pin)) ? 1 : 0; | *val = (data & (1u << pin)) ? 1 : 0; | ||||
out: | |||||
MTK_GPIO_UNLOCK(sc); | MTK_GPIO_UNLOCK(sc); | ||||
return (ret); | return (ret); | ||||
} | } | ||||
static int | static int | ||||
mtk_gpio_pin_toggle(device_t dev, uint32_t pin) | mtk_gpio_pin_toggle(device_t dev, uint32_t pin) | ||||
{ | { | ||||
struct mtk_gpio_softc *sc; | struct mtk_gpio_softc *sc; | ||||
int ret; | int ret; | ||||
if (pin >= sc->num_pins) | |||||
return (EINVAL); | |||||
sc = device_get_softc(dev); | sc = device_get_softc(dev); | ||||
ret = 0; | ret = 0; | ||||
if (pin >= sc->num_pins) | |||||
return (EINVAL); | |||||
MTK_GPIO_LOCK(sc); | MTK_GPIO_LOCK(sc); | ||||
if (!(sc->pins[pin].pin_flags & GPIO_PIN_OUTPUT)) { | if (!(sc->pins[pin].pin_flags & GPIO_PIN_OUTPUT)) { | ||||
ret = EINVAL; | ret = EINVAL; | ||||
goto out; | goto out; | ||||
} | } | ||||
MTK_WRITE_4(sc, GPIO_PIOTOG, (1u << pin)); | MTK_WRITE_4(sc, GPIO_PIOTOG, (1u << pin)); | ||||
▲ Show 20 Lines • Show All 176 Lines • Show Last 20 Lines |