Home | History | Annotate | Line # | Download | only in i2c
twl4030.c revision 1.6
      1  1.6   thorpej /* $NetBSD: twl4030.c,v 1.6 2021/01/27 03:10:21 thorpej Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2019 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.3  jmcneill #include "opt_fdt.h"
     30  1.3  jmcneill 
     31  1.1  jmcneill #include <sys/cdefs.h>
     32  1.6   thorpej __KERNEL_RCSID(0, "$NetBSD: twl4030.c,v 1.6 2021/01/27 03:10:21 thorpej Exp $");
     33  1.1  jmcneill 
     34  1.1  jmcneill #include <sys/param.h>
     35  1.1  jmcneill #include <sys/systm.h>
     36  1.1  jmcneill #include <sys/kernel.h>
     37  1.1  jmcneill #include <sys/device.h>
     38  1.1  jmcneill #include <sys/conf.h>
     39  1.1  jmcneill #include <sys/bus.h>
     40  1.1  jmcneill #include <sys/kmem.h>
     41  1.1  jmcneill #include <sys/gpio.h>
     42  1.1  jmcneill 
     43  1.1  jmcneill #include <dev/i2c/i2cvar.h>
     44  1.1  jmcneill 
     45  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     46  1.1  jmcneill 
     47  1.1  jmcneill #define	TWL_PIN_COUNT	16
     48  1.1  jmcneill 
     49  1.1  jmcneill /* TWL4030 is a multi-function IC. Each module is at a separate I2C address */
     50  1.1  jmcneill #define	ADDR_USB	0x00
     51  1.1  jmcneill #define	ADDR_INT	0x01
     52  1.1  jmcneill #define	ADDR_AUX	0x02
     53  1.1  jmcneill #define	ADDR_POWER	0x03
     54  1.1  jmcneill 
     55  1.2  jmcneill /* INTBR registers */
     56  1.2  jmcneill #define	IDCODE_7_0	0x85
     57  1.2  jmcneill #define	IDCODE_15_8	0x86
     58  1.2  jmcneill #define	IDCODE_23_16	0x87
     59  1.2  jmcneill #define	IDCODE_31_24	0x88
     60  1.2  jmcneill 
     61  1.1  jmcneill /* GPIO registers */
     62  1.1  jmcneill #define	GPIOBASE		0x98
     63  1.1  jmcneill #define	GPIODATAIN(pin)		(GPIOBASE + 0x00 + (pin) / 8)
     64  1.1  jmcneill #define	GPIODATADIR(pin)	(GPIOBASE + 0x03 + (pin) / 8)
     65  1.1  jmcneill #define	CLEARGPIODATAOUT(pin)	(GPIOBASE + 0x09 + (pin) / 8)
     66  1.1  jmcneill #define	SETGPIODATAOUT(pin)	(GPIOBASE + 0x0c + (pin) / 8)
     67  1.1  jmcneill #define	 PIN_BIT(pin)		__BIT((pin) % 8)
     68  1.1  jmcneill #define	GPIOPUPDCTR(pin)	(GPIOBASE + 0x13 + (n) / 4)
     69  1.1  jmcneill #define	 PUPD_BITS(pin)		__BITS((pin) % 4 + 1, (pin) % 4)
     70  1.1  jmcneill 
     71  1.2  jmcneill /* POWER registers */
     72  1.2  jmcneill #define	SECONDS_REG		0x1c
     73  1.2  jmcneill #define	MINUTES_REG		0x1d
     74  1.2  jmcneill #define	HOURS_REG		0x1e
     75  1.2  jmcneill #define	DAYS_REG		0x1f
     76  1.2  jmcneill #define	MONTHS_REG		0x20
     77  1.2  jmcneill #define	YEARS_REG		0x21
     78  1.2  jmcneill #define	WEEKS_REG		0x22
     79  1.2  jmcneill #define	RTC_CTRL_REG		0x29
     80  1.2  jmcneill #define	 GET_TIME		__BIT(6)
     81  1.2  jmcneill #define	 STOP_RTC		__BIT(0)
     82  1.2  jmcneill 
     83  1.1  jmcneill struct twl_softc {
     84  1.1  jmcneill 	device_t	sc_dev;
     85  1.1  jmcneill 	i2c_tag_t	sc_i2c;
     86  1.1  jmcneill 	i2c_addr_t	sc_addr;
     87  1.1  jmcneill 	int		sc_phandle;
     88  1.1  jmcneill 
     89  1.1  jmcneill 	int		sc_npins;
     90  1.2  jmcneill 
     91  1.2  jmcneill 	struct todr_chip_handle sc_todr;
     92  1.1  jmcneill };
     93  1.1  jmcneill 
     94  1.1  jmcneill struct twl_pin {
     95  1.1  jmcneill 	struct twl_softc	*pin_sc;
     96  1.1  jmcneill 	int			pin_num;
     97  1.1  jmcneill 	int			pin_flags;
     98  1.1  jmcneill 	bool			pin_actlo;
     99  1.1  jmcneill };
    100  1.1  jmcneill 
    101  1.1  jmcneill static const struct device_compatible_entry compat_data[] = {
    102  1.4   thorpej 	{ .compat = "ti,twl4030" },
    103  1.5   thorpej 	DEVICE_COMPAT_EOL
    104  1.5   thorpej };
    105  1.4   thorpej 
    106  1.5   thorpej #ifdef FDT
    107  1.5   thorpej static const struct device_compatible_entry rtc_compat_data[] = {
    108  1.5   thorpej 	{ .compat = "ti,twl4030-rtc" },
    109  1.5   thorpej 	DEVICE_COMPAT_EOL
    110  1.1  jmcneill };
    111  1.1  jmcneill 
    112  1.5   thorpej static const struct device_compatible_entry gpio_compat_data[] = {
    113  1.5   thorpej 	{ .compat = "ti,twl4030-gpio" },
    114  1.5   thorpej 	DEVICE_COMPAT_EOL
    115  1.5   thorpej };
    116  1.3  jmcneill #endif
    117  1.1  jmcneill 
    118  1.1  jmcneill static uint8_t
    119  1.1  jmcneill twl_read(struct twl_softc *sc, uint8_t mod, uint8_t reg, int flags)
    120  1.1  jmcneill {
    121  1.1  jmcneill 	uint8_t val = 0;
    122  1.1  jmcneill 	int error;
    123  1.1  jmcneill 
    124  1.1  jmcneill 	error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr + mod,
    125  1.1  jmcneill 	    &reg, 1, &val, 1, flags);
    126  1.1  jmcneill 	if (error != 0)
    127  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "error reading reg %#x: %d\n", reg, error);
    128  1.1  jmcneill 
    129  1.1  jmcneill 	return val;
    130  1.1  jmcneill }
    131  1.1  jmcneill 
    132  1.1  jmcneill static void
    133  1.1  jmcneill twl_write(struct twl_softc *sc, uint8_t mod, uint8_t reg, uint8_t val, int flags)
    134  1.1  jmcneill {
    135  1.1  jmcneill 	uint8_t buf[2];
    136  1.1  jmcneill 	int error;
    137  1.1  jmcneill 
    138  1.1  jmcneill 	buf[0] = reg;
    139  1.1  jmcneill 	buf[1] = val;
    140  1.1  jmcneill 
    141  1.1  jmcneill 	error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr + mod,
    142  1.1  jmcneill 	    NULL, 0, buf, 2, flags);
    143  1.1  jmcneill 	if (error != 0)
    144  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "error writing reg %#x: %d\n", reg, error);
    145  1.1  jmcneill }
    146  1.1  jmcneill 
    147  1.1  jmcneill #define	I2C_LOCK(sc)		iic_acquire_bus((sc)->sc_i2c, I2C_F_POLL)
    148  1.1  jmcneill #define	I2C_UNLOCK(sc)		iic_release_bus((sc)->sc_i2c, I2C_F_POLL)
    149  1.1  jmcneill 
    150  1.1  jmcneill #define	INT_READ(sc, reg)	twl_read((sc), ADDR_INT, (reg), I2C_F_POLL)
    151  1.1  jmcneill #define	INT_WRITE(sc, reg, val)	twl_write((sc), ADDR_INT, (reg), (val), I2C_F_POLL)
    152  1.1  jmcneill 
    153  1.2  jmcneill #define	POWER_READ(sc, reg)	twl_read((sc), ADDR_POWER, (reg), I2C_F_POLL)
    154  1.2  jmcneill #define	POWER_WRITE(sc, reg, val) twl_write((sc), ADDR_POWER, (reg), (val), I2C_F_POLL)
    155  1.2  jmcneill 
    156  1.2  jmcneill static void
    157  1.2  jmcneill twl_rtc_enable(struct twl_softc *sc, bool onoff)
    158  1.2  jmcneill {
    159  1.2  jmcneill 	uint8_t rtc_ctrl;
    160  1.2  jmcneill 
    161  1.2  jmcneill 	rtc_ctrl = POWER_READ(sc, RTC_CTRL_REG);
    162  1.2  jmcneill 	if (onoff)
    163  1.2  jmcneill 		rtc_ctrl |= STOP_RTC;	/* 1: RTC is running */
    164  1.2  jmcneill 	else
    165  1.2  jmcneill 		rtc_ctrl &= ~STOP_RTC;	/* 0: RTC is frozen */
    166  1.2  jmcneill 	POWER_WRITE(sc, RTC_CTRL_REG, rtc_ctrl);
    167  1.2  jmcneill }
    168  1.2  jmcneill 
    169  1.2  jmcneill static int
    170  1.2  jmcneill twl_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    171  1.2  jmcneill {
    172  1.2  jmcneill 	struct twl_softc *sc = tch->cookie;
    173  1.2  jmcneill 	uint8_t seconds_reg, minutes_reg, hours_reg,
    174  1.2  jmcneill 	    days_reg, months_reg, years_reg, weeks_reg;
    175  1.2  jmcneill 
    176  1.2  jmcneill 	iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
    177  1.2  jmcneill 	seconds_reg = POWER_READ(sc, SECONDS_REG);
    178  1.2  jmcneill 	minutes_reg = POWER_READ(sc, MINUTES_REG);
    179  1.2  jmcneill 	hours_reg = POWER_READ(sc, HOURS_REG);
    180  1.2  jmcneill 	days_reg = POWER_READ(sc, DAYS_REG);
    181  1.2  jmcneill 	months_reg = POWER_READ(sc, MONTHS_REG);
    182  1.2  jmcneill 	years_reg = POWER_READ(sc, YEARS_REG);
    183  1.2  jmcneill 	weeks_reg = POWER_READ(sc, WEEKS_REG);
    184  1.2  jmcneill 	iic_release_bus(sc->sc_i2c, I2C_F_POLL);
    185  1.2  jmcneill 
    186  1.2  jmcneill 	dt->dt_sec = bcdtobin(seconds_reg);
    187  1.2  jmcneill 	dt->dt_min = bcdtobin(minutes_reg);
    188  1.2  jmcneill 	dt->dt_hour = bcdtobin(hours_reg);
    189  1.2  jmcneill 	dt->dt_day = bcdtobin(days_reg);
    190  1.2  jmcneill 	dt->dt_mon = bcdtobin(months_reg);
    191  1.2  jmcneill 	dt->dt_year = bcdtobin(years_reg) + 2000;
    192  1.2  jmcneill 	dt->dt_wday = bcdtobin(weeks_reg);
    193  1.2  jmcneill 
    194  1.2  jmcneill 	return 0;
    195  1.2  jmcneill }
    196  1.2  jmcneill 
    197  1.2  jmcneill static int
    198  1.2  jmcneill twl_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    199  1.2  jmcneill {
    200  1.2  jmcneill 	struct twl_softc *sc = tch->cookie;
    201  1.2  jmcneill 
    202  1.2  jmcneill 	iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
    203  1.2  jmcneill 	twl_rtc_enable(sc, false);
    204  1.2  jmcneill         POWER_WRITE(sc, SECONDS_REG, bintobcd(dt->dt_sec));
    205  1.2  jmcneill         POWER_WRITE(sc, MINUTES_REG, bintobcd(dt->dt_min));
    206  1.2  jmcneill         POWER_WRITE(sc, HOURS_REG, bintobcd(dt->dt_hour));
    207  1.2  jmcneill         POWER_WRITE(sc, DAYS_REG, bintobcd(dt->dt_day));
    208  1.2  jmcneill         POWER_WRITE(sc, MONTHS_REG, bintobcd(dt->dt_mon));
    209  1.2  jmcneill         POWER_WRITE(sc, YEARS_REG, bintobcd(dt->dt_year % 100));
    210  1.2  jmcneill         POWER_WRITE(sc, WEEKS_REG, bintobcd(dt->dt_wday));
    211  1.2  jmcneill 	twl_rtc_enable(sc, true);
    212  1.2  jmcneill 	iic_release_bus(sc->sc_i2c, I2C_F_POLL);
    213  1.2  jmcneill 
    214  1.2  jmcneill 	return 0;
    215  1.2  jmcneill }
    216  1.2  jmcneill 
    217  1.3  jmcneill #ifdef FDT
    218  1.1  jmcneill static int
    219  1.1  jmcneill twl_gpio_config(struct twl_softc *sc, int pin, int flags)
    220  1.1  jmcneill {
    221  1.1  jmcneill 	uint8_t dir;
    222  1.1  jmcneill 
    223  1.1  jmcneill 	KASSERT(pin >= 0 && pin < sc->sc_npins);
    224  1.1  jmcneill 
    225  1.1  jmcneill 	dir = INT_READ(sc, GPIODATADIR(pin));
    226  1.1  jmcneill 
    227  1.1  jmcneill 	switch (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
    228  1.1  jmcneill 	case GPIO_PIN_INPUT:
    229  1.1  jmcneill 		dir &= ~PIN_BIT(pin);
    230  1.1  jmcneill 		break;
    231  1.1  jmcneill 	case GPIO_PIN_OUTPUT:
    232  1.1  jmcneill 		dir |= PIN_BIT(pin);
    233  1.1  jmcneill 		break;
    234  1.1  jmcneill 	default:
    235  1.1  jmcneill 		return EINVAL;
    236  1.1  jmcneill 	}
    237  1.1  jmcneill 
    238  1.1  jmcneill 	INT_WRITE(sc, GPIODATADIR(pin), dir);
    239  1.1  jmcneill 
    240  1.1  jmcneill 	return 0;
    241  1.1  jmcneill }
    242  1.1  jmcneill 
    243  1.1  jmcneill static void *
    244  1.1  jmcneill twl_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
    245  1.1  jmcneill {
    246  1.1  jmcneill 	struct twl_softc * const sc = device_private(dev);
    247  1.1  jmcneill 	struct twl_pin *gpin;
    248  1.1  jmcneill 	const u_int *gpio = data;
    249  1.1  jmcneill 	int error;
    250  1.1  jmcneill 
    251  1.1  jmcneill 	if (len != 12)
    252  1.1  jmcneill 		return NULL;
    253  1.1  jmcneill 
    254  1.1  jmcneill 	const uint8_t pin = be32toh(gpio[1]) & 0xff;
    255  1.1  jmcneill 	const bool actlo = (be32toh(gpio[2]) & __BIT(0)) != 0;
    256  1.1  jmcneill 
    257  1.1  jmcneill 	if (pin >= sc->sc_npins)
    258  1.1  jmcneill 		return NULL;
    259  1.1  jmcneill 
    260  1.1  jmcneill 	I2C_LOCK(sc);
    261  1.1  jmcneill 	error = twl_gpio_config(sc, pin, flags);
    262  1.1  jmcneill 	I2C_UNLOCK(sc);
    263  1.1  jmcneill 
    264  1.1  jmcneill 	if (error != 0) {
    265  1.1  jmcneill 		device_printf(dev, "bad pin %d config %#x\n", pin, flags);
    266  1.1  jmcneill 		return NULL;
    267  1.1  jmcneill 	}
    268  1.1  jmcneill 
    269  1.1  jmcneill 	gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
    270  1.1  jmcneill 	gpin->pin_sc = sc;
    271  1.1  jmcneill 	gpin->pin_num = pin;
    272  1.1  jmcneill 	gpin->pin_flags = flags;
    273  1.1  jmcneill 	gpin->pin_actlo = actlo;
    274  1.1  jmcneill 
    275  1.1  jmcneill 	return gpin;
    276  1.1  jmcneill }
    277  1.1  jmcneill 
    278  1.1  jmcneill static void
    279  1.1  jmcneill twl_gpio_release(device_t dev, void *priv)
    280  1.1  jmcneill {
    281  1.1  jmcneill 	struct twl_softc * const sc = device_private(dev);
    282  1.1  jmcneill 	struct twl_pin *gpin = priv;
    283  1.1  jmcneill 
    284  1.1  jmcneill 	I2C_LOCK(sc);
    285  1.1  jmcneill 	twl_gpio_config(sc, gpin->pin_num, GPIO_PIN_INPUT);
    286  1.1  jmcneill 	I2C_UNLOCK(sc);
    287  1.1  jmcneill 
    288  1.1  jmcneill 	kmem_free(gpin, sizeof(*gpin));
    289  1.1  jmcneill }
    290  1.1  jmcneill 
    291  1.1  jmcneill static int
    292  1.1  jmcneill twl_gpio_read(device_t dev, void *priv, bool raw)
    293  1.1  jmcneill {
    294  1.1  jmcneill 	struct twl_softc * const sc = device_private(dev);
    295  1.1  jmcneill 	struct twl_pin *gpin = priv;
    296  1.1  jmcneill 	uint8_t gpio;
    297  1.1  jmcneill 	int val;
    298  1.1  jmcneill 
    299  1.1  jmcneill 	I2C_LOCK(sc);
    300  1.1  jmcneill 	gpio = INT_READ(sc, GPIODATAIN(gpin->pin_num));
    301  1.1  jmcneill 	I2C_UNLOCK(sc);
    302  1.1  jmcneill 
    303  1.1  jmcneill 	val = __SHIFTOUT(gpio, PIN_BIT(gpin->pin_num));
    304  1.1  jmcneill 	if (!raw && gpin->pin_actlo)
    305  1.1  jmcneill 		val = !val;
    306  1.1  jmcneill 
    307  1.1  jmcneill 	return val;
    308  1.1  jmcneill }
    309  1.1  jmcneill 
    310  1.1  jmcneill static void
    311  1.1  jmcneill twl_gpio_write(device_t dev, void *priv, int val, bool raw)
    312  1.1  jmcneill {
    313  1.1  jmcneill 	struct twl_softc * const sc = device_private(dev);
    314  1.1  jmcneill 	struct twl_pin *gpin = priv;
    315  1.1  jmcneill 
    316  1.1  jmcneill 	if (!raw && gpin->pin_actlo)
    317  1.1  jmcneill 		val = !val;
    318  1.1  jmcneill 
    319  1.1  jmcneill 	I2C_LOCK(sc);
    320  1.1  jmcneill 	if (val)
    321  1.1  jmcneill 		INT_WRITE(sc, SETGPIODATAOUT(gpin->pin_num), PIN_BIT(gpin->pin_num));
    322  1.1  jmcneill 	else
    323  1.1  jmcneill 		INT_WRITE(sc, CLEARGPIODATAOUT(gpin->pin_num), PIN_BIT(gpin->pin_num));
    324  1.1  jmcneill 	I2C_UNLOCK(sc);
    325  1.1  jmcneill }
    326  1.1  jmcneill 
    327  1.1  jmcneill static struct fdtbus_gpio_controller_func twl_gpio_funcs = {
    328  1.1  jmcneill 	.acquire = twl_gpio_acquire,
    329  1.1  jmcneill 	.release = twl_gpio_release,
    330  1.1  jmcneill 	.read = twl_gpio_read,
    331  1.1  jmcneill 	.write = twl_gpio_write,
    332  1.1  jmcneill };
    333  1.3  jmcneill #endif /* !FDT */
    334  1.1  jmcneill 
    335  1.1  jmcneill static void
    336  1.2  jmcneill twl_rtc_attach(struct twl_softc *sc, const int phandle)
    337  1.2  jmcneill {
    338  1.2  jmcneill 	iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
    339  1.2  jmcneill 	twl_rtc_enable(sc, true);
    340  1.2  jmcneill 	iic_release_bus(sc->sc_i2c, I2C_F_POLL);
    341  1.2  jmcneill 
    342  1.2  jmcneill 	sc->sc_todr.todr_gettime_ymdhms = twl_rtc_gettime;
    343  1.2  jmcneill 	sc->sc_todr.todr_settime_ymdhms = twl_rtc_settime;
    344  1.2  jmcneill 	sc->sc_todr.cookie = sc;
    345  1.3  jmcneill #ifdef FDT
    346  1.2  jmcneill 	fdtbus_todr_attach(sc->sc_dev, phandle, &sc->sc_todr);
    347  1.3  jmcneill #else
    348  1.3  jmcneill 	todr_attach(&sc->sc_todr);
    349  1.3  jmcneill #endif
    350  1.2  jmcneill }
    351  1.2  jmcneill 
    352  1.2  jmcneill static void
    353  1.1  jmcneill twl_gpio_attach(struct twl_softc *sc, const int phandle)
    354  1.1  jmcneill {
    355  1.3  jmcneill #ifdef FDT
    356  1.1  jmcneill 	fdtbus_register_gpio_controller(sc->sc_dev, phandle, &twl_gpio_funcs);
    357  1.3  jmcneill #endif
    358  1.1  jmcneill }
    359  1.1  jmcneill 
    360  1.1  jmcneill static int
    361  1.1  jmcneill twl_match(device_t parent, cfdata_t match, void *aux)
    362  1.1  jmcneill {
    363  1.1  jmcneill 	struct i2c_attach_args *ia = aux;
    364  1.1  jmcneill 	int match_result;
    365  1.1  jmcneill 
    366  1.1  jmcneill 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
    367  1.1  jmcneill 		return match_result;
    368  1.1  jmcneill 
    369  1.3  jmcneill 	if (ia->ia_addr == 0x48)
    370  1.3  jmcneill 		return I2C_MATCH_ADDRESS_ONLY;
    371  1.3  jmcneill 
    372  1.1  jmcneill 	return 0;
    373  1.1  jmcneill }
    374  1.1  jmcneill 
    375  1.1  jmcneill static void
    376  1.1  jmcneill twl_attach(device_t parent, device_t self, void *aux)
    377  1.1  jmcneill {
    378  1.1  jmcneill 	struct twl_softc * const sc = device_private(self);
    379  1.1  jmcneill 	struct i2c_attach_args *ia = aux;
    380  1.2  jmcneill 	uint32_t idcode;
    381  1.1  jmcneill 
    382  1.1  jmcneill 	sc->sc_dev = self;
    383  1.1  jmcneill 	sc->sc_i2c = ia->ia_tag;
    384  1.1  jmcneill 	sc->sc_addr = ia->ia_addr;
    385  1.1  jmcneill 	sc->sc_phandle = ia->ia_cookie;
    386  1.1  jmcneill 	sc->sc_npins = TWL_PIN_COUNT;
    387  1.1  jmcneill 
    388  1.1  jmcneill 	aprint_naive("\n");
    389  1.1  jmcneill 	aprint_normal(": TWL4030");
    390  1.1  jmcneill 
    391  1.3  jmcneill #ifdef FDT
    392  1.3  jmcneill 	for (int child = OF_child(sc->sc_phandle); child; child = OF_peer(child)) {
    393  1.6   thorpej 		if (of_compatible_match(child, gpio_compat_data)) {
    394  1.1  jmcneill 			aprint_normal(", GPIO");
    395  1.1  jmcneill 			twl_gpio_attach(sc, child);
    396  1.6   thorpej 		} else if (of_compatible_match(child, rtc_compat_data)) {
    397  1.2  jmcneill 			aprint_normal(", RTC");
    398  1.2  jmcneill 			twl_rtc_attach(sc, child);
    399  1.1  jmcneill 		}
    400  1.1  jmcneill 	}
    401  1.3  jmcneill #else
    402  1.3  jmcneill 	aprint_normal("\n");
    403  1.3  jmcneill 	twl_gpio_attach(sc, -1);
    404  1.3  jmcneill 	twl_rtc_attach(sc, -1);
    405  1.3  jmcneill #endif
    406  1.2  jmcneill 
    407  1.2  jmcneill 	I2C_LOCK(sc);
    408  1.2  jmcneill 	idcode = INT_READ(sc, IDCODE_7_0);
    409  1.2  jmcneill 	idcode |= (uint32_t)INT_READ(sc, IDCODE_15_8) << 8;
    410  1.2  jmcneill 	idcode |= (uint32_t)INT_READ(sc, IDCODE_23_16) << 16;
    411  1.2  jmcneill 	idcode |= (uint32_t)INT_READ(sc, IDCODE_31_24) << 24;
    412  1.2  jmcneill 	I2C_UNLOCK(sc);
    413  1.2  jmcneill 
    414  1.2  jmcneill 	aprint_normal(", IDCODE 0x%08x\n", idcode);
    415  1.1  jmcneill }
    416  1.1  jmcneill 
    417  1.1  jmcneill CFATTACH_DECL_NEW(twl, sizeof(struct twl_softc),
    418  1.1  jmcneill     twl_match, twl_attach, NULL, NULL);
    419