Lines Matching +full:pin +full:- +full:count

1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
78 * low-level controller driver in struct gpio_pin. Currently, only pins
80 * the FDT properties. In theory, these flags are defined per-platform. In
81 * practice they are always the flags from the dt-bindings/gpio/gpio.h file.
82 * The only one of those flags we currently support is for handling active-low
88 * XXX -> Move me to better place - gpio_subr.c?
96 gpio_pin_t pin, uint32_t intr_mode) in gpio_alloc_intr_resource() argument
104 gpio_data->gpio_pin_num = pin->pin; in gpio_alloc_intr_resource()
105 gpio_data->gpio_pin_flags = pin->flags; in gpio_alloc_intr_resource()
106 gpio_data->gpio_intr_mode = intr_mode; in gpio_alloc_intr_resource()
108 irq = intr_map_irq(pin->dev, 0, (struct intr_map_data *)gpio_data); in gpio_alloc_intr_resource()
121 gpio_pin_t pin, uint32_t intr_mode) in gpio_alloc_intr_resource() argument
138 /* Cannot mix pull-up/pull-down together. */ in gpio_check_flags()
145 if ((flags & GPIO_INTR_MASK) & ((flags & GPIO_INTR_MASK) - 1)) in gpio_check_flags()
157 gpio_pin_t pin; in gpio_pin_get_by_bus_pinnum() local
164 pin = malloc(sizeof(*pin), M_DEVBUF, M_WAITOK | M_ZERO); in gpio_pin_get_by_bus_pinnum()
166 pin->dev = device_get_parent(busdev); in gpio_pin_get_by_bus_pinnum()
167 pin->pin = pinnum; in gpio_pin_get_by_bus_pinnum()
168 pin->flags = 0; in gpio_pin_get_by_bus_pinnum()
170 *ppin = pin; in gpio_pin_get_by_bus_pinnum()
180 if (idx >= devi->npins) in gpio_pin_get_by_child_index()
184 devi->pins[idx], ppin)); in gpio_pin_get_by_child_index()
188 gpio_pin_getcaps(gpio_pin_t pin, uint32_t *caps) in gpio_pin_getcaps() argument
191 KASSERT(pin != NULL, ("GPIO pin is NULL.")); in gpio_pin_getcaps()
192 KASSERT(pin->dev != NULL, ("GPIO pin device is NULL.")); in gpio_pin_getcaps()
193 return (GPIO_PIN_GETCAPS(pin->dev, pin->pin, caps)); in gpio_pin_getcaps()
197 gpio_pin_is_active(gpio_pin_t pin, bool *active) in gpio_pin_is_active() argument
202 KASSERT(pin != NULL, ("GPIO pin is NULL.")); in gpio_pin_is_active()
203 KASSERT(pin->dev != NULL, ("GPIO pin device is NULL.")); in gpio_pin_is_active()
204 rv = GPIO_PIN_GET(pin->dev, pin->pin, &tmp); in gpio_pin_is_active()
209 if (pin->flags & GPIO_ACTIVE_LOW) in gpio_pin_is_active()
224 KASSERT(gpio->dev != NULL, ("GPIO pin device is NULL.")); in gpio_pin_release()
226 busdev = GPIO_GET_BUS(gpio->dev); in gpio_pin_release()
228 gpiobus_release_pin(busdev, gpio->pin); in gpio_pin_release()
234 gpio_pin_set_active(gpio_pin_t pin, bool active) in gpio_pin_set_active() argument
239 if (pin->flags & GPIO_ACTIVE_LOW) in gpio_pin_set_active()
244 KASSERT(pin != NULL, ("GPIO pin is NULL.")); in gpio_pin_set_active()
245 KASSERT(pin->dev != NULL, ("GPIO pin device is NULL.")); in gpio_pin_set_active()
246 rv = GPIO_PIN_SET(pin->dev, pin->pin, tmp); in gpio_pin_set_active()
251 gpio_pin_setflags(gpio_pin_t pin, uint32_t flags) in gpio_pin_setflags() argument
255 KASSERT(pin != NULL, ("GPIO pin is NULL.")); in gpio_pin_setflags()
256 KASSERT(pin->dev != NULL, ("GPIO pin device is NULL.")); in gpio_pin_setflags()
258 rv = GPIO_PIN_SETFLAGS(pin->dev, pin->pin, flags); in gpio_pin_setflags()
267 if (devi->npins == 0) in gpiobus_print_pins()
271 range_start = range_stop = devi->pins[0]; in gpiobus_print_pins()
272 for (i = 1; i < devi->npins; i++) { in gpiobus_print_pins()
273 if (devi->pins[i] != (range_stop + 1)) { in gpiobus_print_pins()
277 sbuf_printf(sb, "%d-%d", range_start, range_stop); in gpiobus_print_pins()
280 range_start = range_stop = devi->pins[i]; in gpiobus_print_pins()
290 sbuf_printf(sb, "%d-%d", range_start, range_stop); in gpiobus_print_pins()
303 if (device_add_child(dev, "gpioc", -1) == NULL) { in gpiobus_attach_bus()
336 sc->sc_busdev = dev; in gpiobus_init_softc()
337 sc->sc_dev = device_get_parent(dev); in gpiobus_init_softc()
338 sc->sc_intr_rman.rm_type = RMAN_ARRAY; in gpiobus_init_softc()
339 sc->sc_intr_rman.rm_descr = "GPIO Interrupts"; in gpiobus_init_softc()
340 if (rman_init(&sc->sc_intr_rman) != 0 || in gpiobus_init_softc()
341 rman_manage_region(&sc->sc_intr_rman, 0, ~0) != 0) in gpiobus_init_softc()
344 if (GPIO_PIN_MAX(sc->sc_dev, &sc->sc_npins) != 0) in gpiobus_init_softc()
347 KASSERT(sc->sc_npins >= 0, ("GPIO device with no pins")); in gpiobus_init_softc()
350 sc->sc_npins++; in gpiobus_init_softc()
352 sc->sc_pins = malloc(sizeof(*sc->sc_pins) * sc->sc_npins, M_DEVBUF, in gpiobus_init_softc()
354 if (sc->sc_pins == NULL) in gpiobus_init_softc()
368 devi->pins = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF, in gpiobus_alloc_ivars()
370 if (devi->pins == NULL) in gpiobus_alloc_ivars()
379 if (devi->pins) { in gpiobus_free_ivars()
380 free(devi->pins, M_DEVBUF); in gpiobus_free_ivars()
381 devi->pins = NULL; in gpiobus_free_ivars()
383 devi->npins = 0; in gpiobus_free_ivars()
387 gpiobus_acquire_pin(device_t bus, uint32_t pin) in gpiobus_acquire_pin() argument
393 if (pin >= sc->sc_npins) { in gpiobus_acquire_pin()
395 "invalid pin %d, max: %d\n", pin, sc->sc_npins - 1); in gpiobus_acquire_pin()
396 return (-1); in gpiobus_acquire_pin()
398 /* Mark pin as mapped and give warning if it's already mapped. */ in gpiobus_acquire_pin()
399 if (sc->sc_pins[pin].mapped) { in gpiobus_acquire_pin()
400 device_printf(bus, "warning: pin %d is already mapped\n", pin); in gpiobus_acquire_pin()
401 return (-1); in gpiobus_acquire_pin()
403 sc->sc_pins[pin].mapped = 1; in gpiobus_acquire_pin()
408 /* Release mapped pin */
410 gpiobus_release_pin(device_t bus, uint32_t pin) in gpiobus_release_pin() argument
416 if (pin >= sc->sc_npins) { in gpiobus_release_pin()
418 "invalid pin %d, max=%d\n", in gpiobus_release_pin()
419 pin, sc->sc_npins - 1); in gpiobus_release_pin()
420 return (-1); in gpiobus_release_pin()
423 if (!sc->sc_pins[pin].mapped) { in gpiobus_release_pin()
424 device_printf(bus, "pin %d is not mapped\n", pin); in gpiobus_release_pin()
425 return (-1); in gpiobus_release_pin()
427 sc->sc_pins[pin].mapped = 0; in gpiobus_release_pin()
438 for (i = 0; i < devi->npins; i++) { in gpiobus_acquire_child_pins()
439 /* Reserve the GPIO pin. */ in gpiobus_acquire_child_pins()
440 if (gpiobus_acquire_pin(dev, devi->pins[i]) != 0) { in gpiobus_acquire_child_pins()
441 device_printf(child, "cannot acquire pin %d\n", in gpiobus_acquire_child_pins()
442 devi->pins[i]); in gpiobus_acquire_child_pins()
443 while (--i >= 0) { in gpiobus_acquire_child_pins()
445 devi->pins[i]); in gpiobus_acquire_child_pins()
451 for (i = 0; i < devi->npins; i++) { in gpiobus_acquire_child_pins()
452 /* Use the child name as pin name. */ in gpiobus_acquire_child_pins()
453 GPIOBUS_PIN_SETNAME(dev, devi->pins[i], in gpiobus_acquire_child_pins()
472 device_printf(child, "empty pin mask\n"); in gpiobus_parse_pins()
475 devi->npins = npins; in gpiobus_parse_pins()
484 devi->pins[npins++] = i; in gpiobus_parse_pins()
497 unsigned long pin; in gpiobus_parse_pin_list() local
503 pin = strtoul(p, &endp, 0); in gpiobus_parse_pin_list()
513 device_printf(child, "garbage in the pin list: %s\n", endp); in gpiobus_parse_pin_list()
517 device_printf(child, "empty pin list\n"); in gpiobus_parse_pin_list()
521 devi->npins = npins; in gpiobus_parse_pin_list()
530 pin = strtoul(p, &endp, 0); in gpiobus_parse_pin_list()
532 devi->pins[i] = pin; in gpiobus_parse_pin_list()
571 * Since this is not a self-enumerating bus, and since we always add
581 KASSERT(mtx_initialized(&sc->sc_mtx), in gpiobus_detach()
590 rman_fini(&sc->sc_intr_rman); in gpiobus_detach()
591 if (sc->sc_pins) { in gpiobus_detach()
592 for (i = 0; i < sc->sc_npins; i++) { in gpiobus_detach()
593 if (sc->sc_pins[i].name != NULL) in gpiobus_detach()
594 free(sc->sc_pins[i].name, M_DEVBUF); in gpiobus_detach()
595 sc->sc_pins[i].name = NULL; in gpiobus_detach()
597 free(sc->sc_pins, M_DEVBUF); in gpiobus_detach()
598 sc->sc_pins = NULL; in gpiobus_detach()
629 device_printf(dev, "<unknown device> at pin%s %s", in gpiobus_probe_nomatch()
630 devi->npins > 1 ? "s" : "", sbuf_data(&sb)); in gpiobus_probe_nomatch()
631 resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd"); in gpiobus_probe_nomatch()
645 if (devi->npins > 0) { in gpiobus_print_child()
646 if (devi->npins > 1) in gpiobus_print_child()
649 retval += printf(" at pin "); in gpiobus_print_child()
655 resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd"); in gpiobus_print_child()
687 resource_list_init(&devi->rl); in gpiobus_add_child()
702 resource_list_free(&devi->rl); in gpiobus_child_deleted()
711 * Re-scan is supposed to remove and add children, but if someone has in gpiobus_rescan()
761 *result = devi->npins; in gpiobus_read_ivar()
784 if (devi->npins != 0) { in gpiobus_write_ivar()
787 devi->npins = value; in gpiobus_write_ivar()
790 devi->npins = 0; in gpiobus_write_ivar()
796 for (i = 0; i < devi->npins; i++) in gpiobus_write_ivar()
797 devi->pins[i] = ptr[i]; in gpiobus_write_ivar()
816 return (&sc->sc_intr_rman); in gpiobus_get_rman()
824 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) in gpiobus_alloc_resource() argument
830 isdefault = (RMAN_IS_DEFAULT_RANGE(start, end) && count == 1); in gpiobus_alloc_resource()
838 start = rle->start; in gpiobus_alloc_resource()
839 count = rle->count; in gpiobus_alloc_resource()
840 end = rle->end; in gpiobus_alloc_resource()
843 end, count, flags)); in gpiobus_alloc_resource()
853 return (&ivar->rl); in gpiobus_get_resource_list()
864 if (sc->sc_owner != NULL) { in gpiobus_acquire_bus()
865 if (sc->sc_owner == child) in gpiobus_acquire_bus()
873 while (sc->sc_owner != NULL) in gpiobus_acquire_bus()
874 mtx_sleep(sc, &sc->sc_mtx, 0, "gpiobuswait", 0); in gpiobus_acquire_bus()
876 sc->sc_owner = child; in gpiobus_acquire_bus()
890 if (sc->sc_owner == NULL) in gpiobus_release_bus()
894 if (sc->sc_owner != child) in gpiobus_release_bus()
898 device_get_nameunit(sc->sc_owner)); in gpiobus_release_bus()
899 sc->sc_owner = NULL; in gpiobus_release_bus()
905 gpiobus_pin_setflags(device_t dev, device_t child, uint32_t pin, in gpiobus_pin_setflags() argument
912 if (pin >= devi->npins) in gpiobus_pin_setflags()
914 if (GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], &caps) != 0) in gpiobus_pin_setflags()
919 return (GPIO_PIN_SETFLAGS(sc->sc_dev, devi->pins[pin], flags)); in gpiobus_pin_setflags()
923 gpiobus_pin_getflags(device_t dev, device_t child, uint32_t pin, in gpiobus_pin_getflags() argument
929 if (pin >= devi->npins) in gpiobus_pin_getflags()
932 return GPIO_PIN_GETFLAGS(sc->sc_dev, devi->pins[pin], flags); in gpiobus_pin_getflags()
936 gpiobus_pin_getcaps(device_t dev, device_t child, uint32_t pin, in gpiobus_pin_getcaps() argument
942 if (pin >= devi->npins) in gpiobus_pin_getcaps()
945 return GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], caps); in gpiobus_pin_getcaps()
949 gpiobus_pin_set(device_t dev, device_t child, uint32_t pin, in gpiobus_pin_set() argument
955 if (pin >= devi->npins) in gpiobus_pin_set()
958 return GPIO_PIN_SET(sc->sc_dev, devi->pins[pin], value); in gpiobus_pin_set()
962 gpiobus_pin_get(device_t dev, device_t child, uint32_t pin, in gpiobus_pin_get() argument
968 if (pin >= devi->npins) in gpiobus_pin_get()
971 return GPIO_PIN_GET(sc->sc_dev, devi->pins[pin], value); in gpiobus_pin_get()
975 gpiobus_pin_toggle(device_t dev, device_t child, uint32_t pin) in gpiobus_pin_toggle() argument
980 if (pin >= devi->npins) in gpiobus_pin_toggle()
983 return GPIO_PIN_TOGGLE(sc->sc_dev, devi->pins[pin]); in gpiobus_pin_toggle()
987 gpiobus_pin_getname(device_t dev, uint32_t pin, char *name) in gpiobus_pin_getname() argument
992 if (pin > sc->sc_npins) in gpiobus_pin_getname()
994 /* Did we have a name for this pin ? */ in gpiobus_pin_getname()
995 if (sc->sc_pins[pin].name != NULL) { in gpiobus_pin_getname()
996 memcpy(name, sc->sc_pins[pin].name, GPIOMAXNAME); in gpiobus_pin_getname()
1000 /* Return the default pin name. */ in gpiobus_pin_getname()
1001 return (GPIO_PIN_GETNAME(device_get_parent(dev), pin, name)); in gpiobus_pin_getname()
1005 gpiobus_pin_setname(device_t dev, uint32_t pin, const char *name) in gpiobus_pin_setname() argument
1010 if (pin > sc->sc_npins) in gpiobus_pin_setname()
1014 /* Save the pin name. */ in gpiobus_pin_setname()
1015 if (sc->sc_pins[pin].name == NULL) in gpiobus_pin_setname()
1016 sc->sc_pins[pin].name = malloc(GPIOMAXNAME, M_DEVBUF, in gpiobus_pin_setname()
1018 strlcpy(sc->sc_pins[pin].name, name, GPIOMAXNAME); in gpiobus_pin_setname()