Home | History | Annotate | Line # | Download | only in ti
      1  1.1  yurix /* $NetBSD: am18xx_pupd.c,v 1.1 2026/09/27 19:55:02 yurix Exp $ */
      2  1.1  yurix 
      3  1.1  yurix /*-
      4  1.1  yurix  * Copyright (c) 2026 The NetBSD Foundation, Inc.
      5  1.1  yurix  * All rights reserved.
      6  1.1  yurix  *
      7  1.1  yurix  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  yurix  * by Yuri Honegger.
      9  1.1  yurix  *
     10  1.1  yurix  * Redistribution and use in source and binary forms, with or without
     11  1.1  yurix  * modification, are permitted provided that the following conditions
     12  1.1  yurix  * are met:
     13  1.1  yurix  * 1. Redistributions of source code must retain the above copyright
     14  1.1  yurix  *    notice, this list of conditions and the following disclaimer.
     15  1.1  yurix  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  yurix  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  yurix  *    documentation and/or other materials provided with the distribution.
     18  1.1  yurix  *
     19  1.1  yurix  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  yurix  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  yurix  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  yurix  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  yurix  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  yurix  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  yurix  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  yurix  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  yurix  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  yurix  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  yurix  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  yurix  */
     31  1.1  yurix 
     32  1.1  yurix /*
     33  1.1  yurix  * Pull-up/pull-down pin configuration on the TI AM18XX SoC.
     34  1.1  yurix  */
     35  1.1  yurix 
     36  1.1  yurix #include <sys/param.h>
     37  1.1  yurix #include <sys/cdefs.h>
     38  1.1  yurix #include <sys/device.h>
     39  1.1  yurix #include <sys/mutex.h>
     40  1.1  yurix 
     41  1.1  yurix #include <dev/fdt/fdtvar.h>
     42  1.1  yurix 
     43  1.1  yurix struct am18xx_pupd_softc {
     44  1.1  yurix 	bus_space_tag_t sc_bst;
     45  1.1  yurix 	bus_space_handle_t sc_bsh;
     46  1.1  yurix 	kmutex_t sc_lock;
     47  1.1  yurix };
     48  1.1  yurix 
     49  1.1  yurix static int 	am18xx_pupd_match(device_t, cfdata_t, void *);
     50  1.1  yurix static void	am18xx_pupd_attach(device_t, device_t, void *);
     51  1.1  yurix static int	am18xx_pupd_set_config(device_t, const void *, size_t);
     52  1.1  yurix static void	am18xx_pupd_configure_group(struct am18xx_pupd_softc *, int);
     53  1.1  yurix 
     54  1.1  yurix CFATTACH_DECL_NEW(am18xxpupd, sizeof(struct am18xx_pupd_softc),
     55  1.1  yurix 		  am18xx_pupd_match, am18xx_pupd_attach, NULL, NULL);
     56  1.1  yurix 
     57  1.1  yurix #define AM18XX_PUPD_ENA	0
     58  1.1  yurix #define AM18XX_PUPD_SEL	4
     59  1.1  yurix 
     60  1.1  yurix #define	PUPD_READ(sc, reg)					\
     61  1.1  yurix 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, reg)
     62  1.1  yurix #define	PUPD_WRITE(sc, reg, val)				\
     63  1.1  yurix 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, reg, val)
     64  1.1  yurix 
     65  1.1  yurix static const struct device_compatible_entry compat_data[] = {
     66  1.1  yurix 	{ .compat = "ti,da850-pupd" },
     67  1.1  yurix 	DEVICE_COMPAT_EOL
     68  1.1  yurix };
     69  1.1  yurix 
     70  1.1  yurix static struct fdtbus_pinctrl_controller_func am18xx_pupd_pinctrl_funcs = {
     71  1.1  yurix 	.set_config = am18xx_pupd_set_config,
     72  1.1  yurix };
     73  1.1  yurix 
     74  1.1  yurix static int
     75  1.1  yurix am18xx_pupd_set_config(device_t dev, const void *data, size_t len)
     76  1.1  yurix {
     77  1.1  yurix 	struct am18xx_pupd_softc * const sc = device_private(dev);
     78  1.1  yurix 
     79  1.1  yurix 	if (len != 4) {
     80  1.1  yurix 		return -1;
     81  1.1  yurix 	}
     82  1.1  yurix 
     83  1.1  yurix 	const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
     84  1.1  yurix 
     85  1.1  yurix 	for (int child = OF_child(phandle); child; child = OF_peer(child)) {
     86  1.1  yurix 		am18xx_pupd_configure_group(sc, child);
     87  1.1  yurix 	}
     88  1.1  yurix 
     89  1.1  yurix 	return 0;
     90  1.1  yurix }
     91  1.1  yurix 
     92  1.1  yurix static void
     93  1.1  yurix am18xx_pupd_configure_group(struct am18xx_pupd_softc *sc, int phandle)
     94  1.1  yurix {
     95  1.1  yurix 	int groups_len;
     96  1.1  yurix 	const char *groups = fdtbus_pinctrl_parse_groups(phandle, &groups_len);
     97  1.1  yurix 
     98  1.1  yurix 	if (groups == NULL) {
     99  1.1  yurix 		return;
    100  1.1  yurix 	}
    101  1.1  yurix 
    102  1.1  yurix 	uint32_t active_groups = 0;
    103  1.1  yurix 	while (groups_len > 0) {
    104  1.1  yurix 		for (int j = 0; j < 32; j++) {
    105  1.1  yurix 			char buf[16];
    106  1.1  yurix 			snprintf(buf, sizeof(buf), "cp%d", j);
    107  1.1  yurix 			if (strcmp(groups, buf) == 0) {
    108  1.1  yurix 				active_groups |= __BIT(j);
    109  1.1  yurix 			}
    110  1.1  yurix 		}
    111  1.1  yurix 		int len = strlen(groups) + 1;
    112  1.1  yurix 		groups += len;
    113  1.1  yurix 		groups_len -= len;
    114  1.1  yurix 	}
    115  1.1  yurix 
    116  1.1  yurix 	int pull_strength;
    117  1.1  yurix 	int bias = fdtbus_pinctrl_parse_bias(phandle, &pull_strength);
    118  1.1  yurix 
    119  1.1  yurix 	mutex_enter(&sc->sc_lock);
    120  1.1  yurix 
    121  1.1  yurix 	if (bias == 0) {
    122  1.1  yurix 		/* no bias */
    123  1.1  yurix 		uint32_t ena = PUPD_READ(sc, AM18XX_PUPD_ENA);
    124  1.1  yurix 		ena &= ~active_groups;
    125  1.1  yurix 		PUPD_WRITE(sc, AM18XX_PUPD_ENA, ena);
    126  1.1  yurix 	} else if (bias == GPIO_PIN_PULLDOWN) {
    127  1.1  yurix 		uint32_t sel = PUPD_READ(sc, AM18XX_PUPD_SEL);
    128  1.1  yurix 		uint32_t ena = PUPD_READ(sc, AM18XX_PUPD_ENA);
    129  1.1  yurix 		sel &= ~active_groups;
    130  1.1  yurix 		ena |= active_groups;
    131  1.1  yurix 		PUPD_WRITE(sc, AM18XX_PUPD_SEL, sel);
    132  1.1  yurix 		PUPD_WRITE(sc, AM18XX_PUPD_ENA, ena);
    133  1.1  yurix 	} else if (bias == GPIO_PIN_PULLUP) {
    134  1.1  yurix 		uint32_t sel = PUPD_READ(sc, AM18XX_PUPD_SEL);
    135  1.1  yurix 		uint32_t ena = PUPD_READ(sc, AM18XX_PUPD_ENA);
    136  1.1  yurix 		sel |= active_groups;
    137  1.1  yurix 		ena |= active_groups;
    138  1.1  yurix 		PUPD_WRITE(sc, AM18XX_PUPD_SEL, sel);
    139  1.1  yurix 		PUPD_WRITE(sc, AM18XX_PUPD_ENA, ena);
    140  1.1  yurix 	}
    141  1.1  yurix 
    142  1.1  yurix 	mutex_exit(&sc->sc_lock);
    143  1.1  yurix }
    144  1.1  yurix 
    145  1.1  yurix int
    146  1.1  yurix am18xx_pupd_match(device_t parent, cfdata_t cf, void *aux)
    147  1.1  yurix {
    148  1.1  yurix 	struct fdt_attach_args * const faa = aux;
    149  1.1  yurix 
    150  1.1  yurix 	return of_compatible_match(faa->faa_phandle, compat_data);
    151  1.1  yurix }
    152  1.1  yurix 
    153  1.1  yurix void
    154  1.1  yurix am18xx_pupd_attach(device_t parent, device_t self, void *aux)
    155  1.1  yurix {
    156  1.1  yurix 	struct am18xx_pupd_softc * const sc = device_private(self);
    157  1.1  yurix 	struct fdt_attach_args * const faa = aux;
    158  1.1  yurix 	const int phandle = faa->faa_phandle;
    159  1.1  yurix 	bus_addr_t addr;
    160  1.1  yurix 	bus_size_t size;
    161  1.1  yurix 
    162  1.1  yurix 	sc->sc_bst = faa->faa_bst;
    163  1.1  yurix 
    164  1.1  yurix 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    165  1.1  yurix 
    166  1.1  yurix 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    167  1.1  yurix 		aprint_error(": couldn't get registers\n");
    168  1.1  yurix 		return;
    169  1.1  yurix 	}
    170  1.1  yurix 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh)) {
    171  1.1  yurix 		aprint_error(": couldn't map registers\n");
    172  1.1  yurix 		return;
    173  1.1  yurix 	}
    174  1.1  yurix 
    175  1.1  yurix 	for (int child = OF_child(phandle); child; child = OF_peer(child)) {
    176  1.1  yurix 		fdtbus_register_pinctrl_config(self, child,
    177  1.1  yurix 		    &am18xx_pupd_pinctrl_funcs);
    178  1.1  yurix 	}
    179  1.1  yurix 
    180  1.1  yurix 	aprint_naive("\n");
    181  1.1  yurix 	aprint_normal("\n");
    182  1.1  yurix }
    183  1.1  yurix 
    184