pinctrl_single.c revision 1.2
11.2Sthorpej/* $NetBSD: pinctrl_single.c,v 1.2 2021/01/18 02:35:49 thorpej Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2019 Jared McNeill <jmcneill@invisible.ca>
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * Redistribution and use in source and binary forms, with or without
81.1Sjmcneill * modification, are permitted provided that the following conditions
91.1Sjmcneill * are met:
101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
111.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
121.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
131.1Sjmcneill *    notice, this list of conditions and the following disclaimer in the
141.1Sjmcneill *    documentation and/or other materials provided with the distribution.
151.1Sjmcneill *
161.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211.1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
221.1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231.1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241.1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Sjmcneill * SUCH DAMAGE.
271.1Sjmcneill */
281.1Sjmcneill
291.1Sjmcneill#include <sys/cdefs.h>
301.2Sthorpej__KERNEL_RCSID(0, "$NetBSD: pinctrl_single.c,v 1.2 2021/01/18 02:35:49 thorpej Exp $");
311.1Sjmcneill
321.1Sjmcneill#include <sys/param.h>
331.1Sjmcneill#include <sys/bus.h>
341.1Sjmcneill#include <sys/device.h>
351.1Sjmcneill#include <sys/systm.h>
361.1Sjmcneill#include <sys/gpio.h>
371.1Sjmcneill
381.1Sjmcneill#include <dev/fdt/fdtvar.h>
391.1Sjmcneill
401.1Sjmcneill#define	PINCTRL_FLAG_PINCONF	__BIT(0)	/* supports generic pinconf */
411.1Sjmcneill
421.1Sjmcneillstruct pinctrl_single_config {
431.1Sjmcneill	uint32_t	flags;
441.1Sjmcneill};
451.1Sjmcneill
461.1Sjmcneillstatic const struct pinctrl_single_config pinctrl_config = {
471.1Sjmcneill};
481.1Sjmcneill
491.1Sjmcneillstatic const struct pinctrl_single_config pinconf_config = {
501.1Sjmcneill	.flags = PINCTRL_FLAG_PINCONF
511.1Sjmcneill};
521.1Sjmcneill
531.2Sthorpejstatic const struct device_compatible_entry compat_data[] = {
541.2Sthorpej	{ .compat = "pinctrl-single",	.data = &pinctrl_config },
551.2Sthorpej	{ .compat = "pinconf-single",	.data = &pinconf_config },
561.2Sthorpej
571.2Sthorpej	{ 0 }
581.1Sjmcneill};
591.1Sjmcneill
601.1Sjmcneillstruct pinctrl_single_softc {
611.1Sjmcneill	device_t			sc_dev;
621.1Sjmcneill	int				sc_phandle;
631.1Sjmcneill	bus_space_tag_t			sc_bst;
641.1Sjmcneill	bus_space_handle_t		sc_bsh;
651.1Sjmcneill	uint32_t			sc_flags;
661.1Sjmcneill	u_int				sc_regwidth;
671.1Sjmcneill	u_int				sc_funcmask;
681.1Sjmcneill};
691.1Sjmcneill
701.1Sjmcneillstatic void
711.1Sjmcneillpinctrl_single_pins_write(struct pinctrl_single_softc *sc, u_int off, u_int val)
721.1Sjmcneill{
731.1Sjmcneill	union {
741.1Sjmcneill		uint32_t reg32;
751.1Sjmcneill		uint16_t reg16;
761.1Sjmcneill		uint8_t reg8;
771.1Sjmcneill	} u;
781.1Sjmcneill
791.1Sjmcneill	aprint_debug_dev(sc->sc_dev, "writing %#x with %#x\n", off, val);
801.1Sjmcneill
811.1Sjmcneill	switch (sc->sc_regwidth) {
821.1Sjmcneill	case 8:
831.1Sjmcneill		u.reg8 = bus_space_read_1(sc->sc_bst, sc->sc_bsh, off);
841.1Sjmcneill		u.reg8 &= ~sc->sc_funcmask;
851.1Sjmcneill		u.reg8 |= val;
861.1Sjmcneill		bus_space_write_1(sc->sc_bst, sc->sc_bsh, off, u.reg8);
871.1Sjmcneill		break;
881.1Sjmcneill	case 16:
891.1Sjmcneill		u.reg16 = bus_space_read_2(sc->sc_bst, sc->sc_bsh, off);
901.1Sjmcneill		u.reg16 &= ~sc->sc_funcmask;
911.1Sjmcneill		u.reg16 |= val;
921.1Sjmcneill		bus_space_write_2(sc->sc_bst, sc->sc_bsh, off, u.reg16);
931.1Sjmcneill		break;
941.1Sjmcneill	case 32:
951.1Sjmcneill		u.reg32 = bus_space_read_4(sc->sc_bst, sc->sc_bsh, off);
961.1Sjmcneill		u.reg32 &= ~sc->sc_funcmask;
971.1Sjmcneill		u.reg32 |= val;
981.1Sjmcneill		bus_space_write_4(sc->sc_bst, sc->sc_bsh, off, u.reg32);
991.1Sjmcneill		break;
1001.1Sjmcneill	default:
1011.1Sjmcneill		device_printf(sc->sc_dev, "%s: unsupported reg width %d\n",
1021.1Sjmcneill		    __func__, sc->sc_regwidth);
1031.1Sjmcneill		break;
1041.1Sjmcneill	}
1051.1Sjmcneill}
1061.1Sjmcneill
1071.1Sjmcneillstatic int
1081.1Sjmcneillpinctrl_single_pins_set_config(device_t dev, const void *data, size_t len)
1091.1Sjmcneill{
1101.1Sjmcneill	struct pinctrl_single_softc * const sc = device_private(dev);
1111.1Sjmcneill	const u_int *pins;
1121.1Sjmcneill	int pinslen;
1131.1Sjmcneill
1141.1Sjmcneill	if (len != 4)
1151.1Sjmcneill		return -1;
1161.1Sjmcneill
1171.1Sjmcneill	const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
1181.1Sjmcneill
1191.1Sjmcneill	pins = fdtbus_get_prop(phandle, "pinctrl-single,pins", &pinslen);
1201.1Sjmcneill	if (pins == NULL)
1211.1Sjmcneill		return -1;
1221.1Sjmcneill
1231.1Sjmcneill	while (pinslen >= 8) {
1241.1Sjmcneill		const int off = be32toh(pins[0]);
1251.1Sjmcneill		const int val = be32toh(pins[1]);
1261.1Sjmcneill
1271.1Sjmcneill		pinctrl_single_pins_write(sc, off, val);
1281.1Sjmcneill		pins += 2;
1291.1Sjmcneill		pinslen -= 8;
1301.1Sjmcneill	}
1311.1Sjmcneill
1321.1Sjmcneill	return 0;
1331.1Sjmcneill}
1341.1Sjmcneill
1351.1Sjmcneillstatic struct fdtbus_pinctrl_controller_func pinctrl_single_pins_funcs = {
1361.1Sjmcneill	.set_config = pinctrl_single_pins_set_config,
1371.1Sjmcneill};
1381.1Sjmcneill
1391.1Sjmcneillstatic int
1401.1Sjmcneillpinctrl_single_match(device_t parent, cfdata_t cf, void *aux)
1411.1Sjmcneill{
1421.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1431.1Sjmcneill
1441.1Sjmcneill	return of_match_compat_data(faa->faa_phandle, compat_data);
1451.1Sjmcneill}
1461.1Sjmcneill
1471.1Sjmcneillstatic void
1481.1Sjmcneillpinctrl_single_attach(device_t parent, device_t self, void *aux)
1491.1Sjmcneill{
1501.1Sjmcneill	struct pinctrl_single_softc * const sc = device_private(self);
1511.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1521.1Sjmcneill	const int phandle = faa->faa_phandle;
1531.1Sjmcneill	const struct pinctrl_single_config *conf;
1541.1Sjmcneill	bus_addr_t addr;
1551.1Sjmcneill	bus_size_t size;
1561.1Sjmcneill	int child;
1571.1Sjmcneill
1581.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
1591.1Sjmcneill		aprint_error(": couldn't get registers\n");
1601.1Sjmcneill		return;
1611.1Sjmcneill	}
1621.1Sjmcneill
1631.2Sthorpej	conf = of_search_compatible(phandle, compat_data)->data;
1641.1Sjmcneill
1651.1Sjmcneill	sc->sc_dev = self;
1661.1Sjmcneill	sc->sc_phandle = phandle;
1671.1Sjmcneill	sc->sc_bst = faa->faa_bst;
1681.1Sjmcneill	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
1691.1Sjmcneill		aprint_error(": couldn't map registers\n");
1701.1Sjmcneill		return;
1711.1Sjmcneill	}
1721.1Sjmcneill	sc->sc_flags = conf->flags;
1731.1Sjmcneill
1741.1Sjmcneill	if (of_getprop_uint32(phandle, "pinctrl-single,register-width", &sc->sc_regwidth) != 0) {
1751.1Sjmcneill		aprint_error(": missing 'pinctrl-single,register-width' property\n");
1761.1Sjmcneill		return;
1771.1Sjmcneill	}
1781.1Sjmcneill	if (of_getprop_uint32(phandle, "pinctrl-single,function-mask", &sc->sc_funcmask) != 0) {
1791.1Sjmcneill		aprint_error(": missing 'pinctrl-single,function-mask' property\n");
1801.1Sjmcneill		return;
1811.1Sjmcneill	}
1821.1Sjmcneill
1831.1Sjmcneill	switch (sc->sc_regwidth) {
1841.1Sjmcneill	case 8:
1851.1Sjmcneill	case 16:
1861.1Sjmcneill	case 32:
1871.1Sjmcneill		break;
1881.1Sjmcneill	default:
1891.1Sjmcneill		aprint_error(": register width %d not supported\n", sc->sc_regwidth);
1901.1Sjmcneill		return;
1911.1Sjmcneill	}
1921.1Sjmcneill
1931.1Sjmcneill	aprint_naive("\n");
1941.1Sjmcneill	aprint_normal("\n");
1951.1Sjmcneill
1961.1Sjmcneill	for (child = OF_child(phandle); child; child = OF_peer(child)) {
1971.1Sjmcneill		if (of_hasprop(child, "pinctrl-single,pins"))
1981.1Sjmcneill			fdtbus_register_pinctrl_config(self, child, &pinctrl_single_pins_funcs);
1991.1Sjmcneill	}
2001.1Sjmcneill
2011.1Sjmcneill	fdtbus_pinctrl_set_config(phandle, "default");
2021.1Sjmcneill}
2031.1Sjmcneill
2041.1SjmcneillCFATTACH_DECL_NEW(pinctrl_single, sizeof(struct pinctrl_single_softc),
2051.1Sjmcneill	pinctrl_single_match, pinctrl_single_attach, NULL, NULL);
206