Home | History | Annotate | Line # | Download | only in i2c
twl4030.c revision 1.4
      1  1.4   thorpej /* $NetBSD: twl4030.c,v 1.4 2021/01/17 21:42:35 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.4   thorpej __KERNEL_RCSID(0, "$NetBSD: twl4030.c,v 1.4 2021/01/17 21:42:35 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.4   thorpej 
    104  1.4   thorpej 	{ 0 }
    105  1.1  jmcneill };
    106  1.1  jmcneill 
    107  1.3  jmcneill #ifdef FDT
    108  1.2  jmcneill static const char * const rtc_compatible[] = { "ti,twl4030-rtc", NULL };
    109  1.1  jmcneill static const char * const gpio_compatible[] = { "ti,twl4030-gpio", NULL };
    110  1.3  jmcneill #endif
    111  1.1  jmcneill 
    112  1.1  jmcneill static uint8_t
    113  1.1  jmcneill twl_read(struct twl_softc *sc, uint8_t mod, uint8_t reg, int flags)
    114  1.1  jmcneill {
    115  1.1  jmcneill 	uint8_t val = 0;
    116  1.1  jmcneill 	int error;
    117  1.1  jmcneill 
    118  1.1  jmcneill 	error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr + mod,
    119  1.1  jmcneill 	    &reg, 1, &val, 1, flags);
    120  1.1  jmcneill 	if (error != 0)
    121  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "error reading reg %#x: %d\n", reg, error);
    122  1.1  jmcneill 
    123  1.1  jmcneill 	return val;
    124  1.1  jmcneill }
    125  1.1  jmcneill 
    126  1.1  jmcneill static void
    127  1.1  jmcneill twl_write(struct twl_softc *sc, uint8_t mod, uint8_t reg, uint8_t val, int flags)
    128  1.1  jmcneill {
    129  1.1  jmcneill 	uint8_t buf[2];
    130  1.1  jmcneill 	int error;
    131  1.1  jmcneill 
    132  1.1  jmcneill 	buf[0] = reg;
    133  1.1  jmcneill 	buf[1] = val;
    134  1.1  jmcneill 
    135  1.1  jmcneill 	error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr + mod,
    136  1.1  jmcneill 	    NULL, 0, buf, 2, flags);
    137  1.1  jmcneill 	if (error != 0)
    138  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "error writing reg %#x: %d\n", reg, error);
    139  1.1  jmcneill }
    140  1.1  jmcneill 
    141  1.1  jmcneill #define	I2C_LOCK(sc)		iic_acquire_bus((sc)->sc_i2c, I2C_F_POLL)
    142  1.1  jmcneill #define	I2C_UNLOCK(sc)		iic_release_bus((sc)->sc_i2c, I2C_F_POLL)
    143  1.1  jmcneill 
    144  1.1  jmcneill #define	INT_READ(sc, reg)	twl_read((sc), ADDR_INT, (reg), I2C_F_POLL)
    145  1.1  jmcneill #define	INT_WRITE(sc, reg, val)	twl_write((sc), ADDR_INT, (reg), (val), I2C_F_POLL)
    146  1.1  jmcneill 
    147  1.2  jmcneill #define	POWER_READ(sc, reg)	twl_read((sc), ADDR_POWER, (reg), I2C_F_POLL)
    148  1.2  jmcneill #define	POWER_WRITE(sc, reg, val) twl_write((sc), ADDR_POWER, (reg), (val), I2C_F_POLL)
    149  1.2  jmcneill 
    150  1.2  jmcneill static void
    151  1.2  jmcneill twl_rtc_enable(struct twl_softc *sc, bool onoff)
    152  1.2  jmcneill {
    153  1.2  jmcneill 	uint8_t rtc_ctrl;
    154  1.2  jmcneill 
    155  1.2  jmcneill 	rtc_ctrl = POWER_READ(sc, RTC_CTRL_REG);
    156  1.2  jmcneill 	if (onoff)
    157  1.2  jmcneill 		rtc_ctrl |= STOP_RTC;	/* 1: RTC is running */
    158  1.2  jmcneill 	else
    159  1.2  jmcneill 		rtc_ctrl &= ~STOP_RTC;	/* 0: RTC is frozen */
    160  1.2  jmcneill 	POWER_WRITE(sc, RTC_CTRL_REG, rtc_ctrl);
    161  1.2  jmcneill }
    162  1.2  jmcneill 
    163  1.2  jmcneill static int
    164  1.2  jmcneill twl_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    165  1.2  jmcneill {
    166  1.2  jmcneill 	struct twl_softc *sc = tch->cookie;
    167  1.2  jmcneill 	uint8_t seconds_reg, minutes_reg, hours_reg,
    168  1.2  jmcneill 	    days_reg, months_reg, years_reg, weeks_reg;
    169  1.2  jmcneill 
    170  1.2  jmcneill 	iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
    171  1.2  jmcneill 	seconds_reg = POWER_READ(sc, SECONDS_REG);
    172  1.2  jmcneill 	minutes_reg = POWER_READ(sc, MINUTES_REG);
    173  1.2  jmcneill 	hours_reg = POWER_READ(sc, HOURS_REG);
    174  1.2  jmcneill 	days_reg = POWER_READ(sc, DAYS_REG);
    175  1.2  jmcneill 	months_reg = POWER_READ(sc, MONTHS_REG);
    176  1.2  jmcneill 	years_reg = POWER_READ(sc, YEARS_REG);
    177  1.2  jmcneill 	weeks_reg = POWER_READ(sc, WEEKS_REG);
    178  1.2  jmcneill 	iic_release_bus(sc->sc_i2c, I2C_F_POLL);
    179  1.2  jmcneill 
    180  1.2  jmcneill 	dt->dt_sec = bcdtobin(seconds_reg);
    181  1.2  jmcneill 	dt->dt_min = bcdtobin(minutes_reg);
    182  1.2  jmcneill 	dt->dt_hour = bcdtobin(hours_reg);
    183  1.2  jmcneill 	dt->dt_day = bcdtobin(days_reg);
    184  1.2  jmcneill 	dt->dt_mon = bcdtobin(months_reg);
    185  1.2  jmcneill 	dt->dt_year = bcdtobin(years_reg) + 2000;
    186  1.2  jmcneill 	dt->dt_wday = bcdtobin(weeks_reg);
    187  1.2  jmcneill 
    188  1.2  jmcneill 	return 0;
    189  1.2  jmcneill }
    190  1.2  jmcneill 
    191  1.2  jmcneill static int
    192  1.2  jmcneill twl_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    193  1.2  jmcneill {
    194  1.2  jmcneill 	struct twl_softc *sc = tch->cookie;
    195  1.2  jmcneill 
    196  1.2  jmcneill 	iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
    197  1.2  jmcneill 	twl_rtc_enable(sc, false);
    198  1.2  jmcneill         POWER_WRITE(sc, SECONDS_REG, bintobcd(dt->dt_sec));
    199  1.2  jmcneill         POWER_WRITE(sc, MINUTES_REG, bintobcd(dt->dt_min));
    200  1.2  jmcneill         POWER_WRITE(sc, HOURS_REG, bintobcd(dt->dt_hour));
    201  1.2  jmcneill         POWER_WRITE(sc, DAYS_REG, bintobcd(dt->dt_day));
    202  1.2  jmcneill         POWER_WRITE(sc, MONTHS_REG, bintobcd(dt->dt_mon));
    203  1.2  jmcneill         POWER_WRITE(sc, YEARS_REG, bintobcd(dt->dt_year % 100));
    204  1.2  jmcneill         POWER_WRITE(sc, WEEKS_REG, bintobcd(dt->dt_wday));
    205  1.2  jmcneill 	twl_rtc_enable(sc, true);
    206  1.2  jmcneill 	iic_release_bus(sc->sc_i2c, I2C_F_POLL);
    207  1.2  jmcneill 
    208  1.2  jmcneill 	return 0;
    209  1.2  jmcneill }
    210  1.2  jmcneill 
    211  1.3  jmcneill #ifdef FDT
    212  1.1  jmcneill static int
    213  1.1  jmcneill twl_gpio_config(struct twl_softc *sc, int pin, int flags)
    214  1.1  jmcneill {
    215  1.1  jmcneill 	uint8_t dir;
    216  1.1  jmcneill 
    217  1.1  jmcneill 	KASSERT(pin >= 0 && pin < sc->sc_npins);
    218  1.1  jmcneill 
    219  1.1  jmcneill 	dir = INT_READ(sc, GPIODATADIR(pin));
    220  1.1  jmcneill 
    221  1.1  jmcneill 	switch (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
    222  1.1  jmcneill 	case GPIO_PIN_INPUT:
    223  1.1  jmcneill 		dir &= ~PIN_BIT(pin);
    224  1.1  jmcneill 		break;
    225  1.1  jmcneill 	case GPIO_PIN_OUTPUT:
    226  1.1  jmcneill 		dir |= PIN_BIT(pin);
    227  1.1  jmcneill 		break;
    228  1.1  jmcneill 	default:
    229  1.1  jmcneill 		return EINVAL;
    230  1.1  jmcneill 	}
    231  1.1  jmcneill 
    232  1.1  jmcneill 	INT_WRITE(sc, GPIODATADIR(pin), dir);
    233  1.1  jmcneill 
    234  1.1  jmcneill 	return 0;
    235  1.1  jmcneill }
    236  1.1  jmcneill 
    237  1.1  jmcneill static void *
    238  1.1  jmcneill twl_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
    239  1.1  jmcneill {
    240  1.1  jmcneill 	struct twl_softc * const sc = device_private(dev);
    241  1.1  jmcneill 	struct twl_pin *gpin;
    242  1.1  jmcneill 	const u_int *gpio = data;
    243  1.1  jmcneill 	int error;
    244  1.1  jmcneill 
    245  1.1  jmcneill 	if (len != 12)
    246  1.1  jmcneill 		return NULL;
    247  1.1  jmcneill 
    248  1.1  jmcneill 	const uint8_t pin = be32toh(gpio[1]) & 0xff;
    249  1.1  jmcneill 	const bool actlo = (be32toh(gpio[2]) & __BIT(0)) != 0;
    250  1.1  jmcneill 
    251  1.1  jmcneill 	if (pin >= sc->sc_npins)
    252  1.1  jmcneill 		return NULL;
    253  1.1  jmcneill 
    254  1.1  jmcneill 	I2C_LOCK(sc);
    255  1.1  jmcneill 	error = twl_gpio_config(sc, pin, flags);
    256  1.1  jmcneill 	I2C_UNLOCK(sc);
    257  1.1  jmcneill 
    258  1.1  jmcneill 	if (error != 0) {
    259  1.1  jmcneill 		device_printf(dev, "bad pin %d config %#x\n", pin, flags);
    260  1.1  jmcneill 		return NULL;
    261  1.1  jmcneill 	}
    262  1.1  jmcneill 
    263  1.1  jmcneill 	gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
    264  1.1  jmcneill 	gpin->pin_sc = sc;
    265  1.1  jmcneill 	gpin->pin_num = pin;
    266  1.1  jmcneill 	gpin->pin_flags = flags;
    267  1.1  jmcneill 	gpin->pin_actlo = actlo;
    268  1.1  jmcneill 
    269  1.1  jmcneill 	return gpin;
    270  1.1  jmcneill }
    271  1.1  jmcneill 
    272  1.1  jmcneill static void
    273  1.1  jmcneill twl_gpio_release(device_t dev, void *priv)
    274  1.1  jmcneill {
    275  1.1  jmcneill 	struct twl_softc * const sc = device_private(dev);
    276  1.1  jmcneill 	struct twl_pin *gpin = priv;
    277  1.1  jmcneill 
    278  1.1  jmcneill 	I2C_LOCK(sc);
    279  1.1  jmcneill 	twl_gpio_config(sc, gpin->pin_num, GPIO_PIN_INPUT);
    280  1.1  jmcneill 	I2C_UNLOCK(sc);
    281  1.1  jmcneill 
    282  1.1  jmcneill 	kmem_free(gpin, sizeof(*gpin));
    283  1.1  jmcneill }
    284  1.1  jmcneill 
    285  1.1  jmcneill static int
    286  1.1  jmcneill twl_gpio_read(device_t dev, void *priv, bool raw)
    287  1.1  jmcneill {
    288  1.1  jmcneill 	struct twl_softc * const sc = device_private(dev);
    289  1.1  jmcneill 	struct twl_pin *gpin = priv;
    290  1.1  jmcneill 	uint8_t gpio;
    291  1.1  jmcneill 	int val;
    292  1.1  jmcneill 
    293  1.1  jmcneill 	I2C_LOCK(sc);
    294  1.1  jmcneill 	gpio = INT_READ(sc, GPIODATAIN(gpin->pin_num));
    295  1.1  jmcneill 	I2C_UNLOCK(sc);
    296  1.1  jmcneill 
    297  1.1  jmcneill 	val = __SHIFTOUT(gpio, PIN_BIT(gpin->pin_num));
    298  1.1  jmcneill 	if (!raw && gpin->pin_actlo)
    299  1.1  jmcneill 		val = !val;
    300  1.1  jmcneill 
    301  1.1  jmcneill 	return val;
    302  1.1  jmcneill }
    303  1.1  jmcneill 
    304  1.1  jmcneill static void
    305  1.1  jmcneill twl_gpio_write(device_t dev, void *priv, int val, bool raw)
    306  1.1  jmcneill {
    307  1.1  jmcneill 	struct twl_softc * const sc = device_private(dev);
    308  1.1  jmcneill 	struct twl_pin *gpin = priv;
    309  1.1  jmcneill 
    310  1.1  jmcneill 	if (!raw && gpin->pin_actlo)
    311  1.1  jmcneill 		val = !val;
    312  1.1  jmcneill 
    313  1.1  jmcneill 	I2C_LOCK(sc);
    314  1.1  jmcneill 	if (val)
    315  1.1  jmcneill 		INT_WRITE(sc, SETGPIODATAOUT(gpin->pin_num), PIN_BIT(gpin->pin_num));
    316  1.1  jmcneill 	else
    317  1.1  jmcneill 		INT_WRITE(sc, CLEARGPIODATAOUT(gpin->pin_num), PIN_BIT(gpin->pin_num));
    318  1.1  jmcneill 	I2C_UNLOCK(sc);
    319  1.1  jmcneill }
    320  1.1  jmcneill 
    321  1.1  jmcneill static struct fdtbus_gpio_controller_func twl_gpio_funcs = {
    322  1.1  jmcneill 	.acquire = twl_gpio_acquire,
    323  1.1  jmcneill 	.release = twl_gpio_release,
    324  1.1  jmcneill 	.read = twl_gpio_read,
    325  1.1  jmcneill 	.write = twl_gpio_write,
    326  1.1  jmcneill };
    327  1.3  jmcneill #endif /* !FDT */
    328  1.1  jmcneill 
    329  1.1  jmcneill static void
    330  1.2  jmcneill twl_rtc_attach(struct twl_softc *sc, const int phandle)
    331  1.2  jmcneill {
    332  1.2  jmcneill 	iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
    333  1.2  jmcneill 	twl_rtc_enable(sc, true);
    334  1.2  jmcneill 	iic_release_bus(sc->sc_i2c, I2C_F_POLL);
    335  1.2  jmcneill 
    336  1.2  jmcneill 	sc->sc_todr.todr_gettime_ymdhms = twl_rtc_gettime;
    337  1.2  jmcneill 	sc->sc_todr.todr_settime_ymdhms = twl_rtc_settime;
    338  1.2  jmcneill 	sc->sc_todr.cookie = sc;
    339  1.3  jmcneill #ifdef FDT
    340  1.2  jmcneill 	fdtbus_todr_attach(sc->sc_dev, phandle, &sc->sc_todr);
    341  1.3  jmcneill #else
    342  1.3  jmcneill 	todr_attach(&sc->sc_todr);
    343  1.3  jmcneill #endif
    344  1.2  jmcneill }
    345  1.2  jmcneill 
    346  1.2  jmcneill static void
    347  1.1  jmcneill twl_gpio_attach(struct twl_softc *sc, const int phandle)
    348  1.1  jmcneill {
    349  1.3  jmcneill #ifdef FDT
    350  1.1  jmcneill 	fdtbus_register_gpio_controller(sc->sc_dev, phandle, &twl_gpio_funcs);
    351  1.3  jmcneill #endif
    352  1.1  jmcneill }
    353  1.1  jmcneill 
    354  1.1  jmcneill static int
    355  1.1  jmcneill twl_match(device_t parent, cfdata_t match, void *aux)
    356  1.1  jmcneill {
    357  1.1  jmcneill 	struct i2c_attach_args *ia = aux;
    358  1.1  jmcneill 	int match_result;
    359  1.1  jmcneill 
    360  1.1  jmcneill 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
    361  1.1  jmcneill 		return match_result;
    362  1.1  jmcneill 
    363  1.3  jmcneill 	if (ia->ia_addr == 0x48)
    364  1.3  jmcneill 		return I2C_MATCH_ADDRESS_ONLY;
    365  1.3  jmcneill 
    366  1.1  jmcneill 	return 0;
    367  1.1  jmcneill }
    368  1.1  jmcneill 
    369  1.1  jmcneill static void
    370  1.1  jmcneill twl_attach(device_t parent, device_t self, void *aux)
    371  1.1  jmcneill {
    372  1.1  jmcneill 	struct twl_softc * const sc = device_private(self);
    373  1.1  jmcneill 	struct i2c_attach_args *ia = aux;
    374  1.2  jmcneill 	uint32_t idcode;
    375  1.1  jmcneill 
    376  1.1  jmcneill 	sc->sc_dev = self;
    377  1.1  jmcneill 	sc->sc_i2c = ia->ia_tag;
    378  1.1  jmcneill 	sc->sc_addr = ia->ia_addr;
    379  1.1  jmcneill 	sc->sc_phandle = ia->ia_cookie;
    380  1.1  jmcneill 	sc->sc_npins = TWL_PIN_COUNT;
    381  1.1  jmcneill 
    382  1.1  jmcneill 	aprint_naive("\n");
    383  1.1  jmcneill 	aprint_normal(": TWL4030");
    384  1.1  jmcneill 
    385  1.3  jmcneill #ifdef FDT
    386  1.3  jmcneill 	for (int child = OF_child(sc->sc_phandle); child; child = OF_peer(child)) {
    387  1.1  jmcneill 		if (of_match_compatible(child, gpio_compatible)) {
    388  1.1  jmcneill 			aprint_normal(", GPIO");
    389  1.1  jmcneill 			twl_gpio_attach(sc, child);
    390  1.2  jmcneill 		} else if (of_match_compatible(child, rtc_compatible)) {
    391  1.2  jmcneill 			aprint_normal(", RTC");
    392  1.2  jmcneill 			twl_rtc_attach(sc, child);
    393  1.1  jmcneill 		}
    394  1.1  jmcneill 	}
    395  1.3  jmcneill #else
    396  1.3  jmcneill 	aprint_normal("\n");
    397  1.3  jmcneill 	twl_gpio_attach(sc, -1);
    398  1.3  jmcneill 	twl_rtc_attach(sc, -1);
    399  1.3  jmcneill #endif
    400  1.2  jmcneill 
    401  1.2  jmcneill 	I2C_LOCK(sc);
    402  1.2  jmcneill 	idcode = INT_READ(sc, IDCODE_7_0);
    403  1.2  jmcneill 	idcode |= (uint32_t)INT_READ(sc, IDCODE_15_8) << 8;
    404  1.2  jmcneill 	idcode |= (uint32_t)INT_READ(sc, IDCODE_23_16) << 16;
    405  1.2  jmcneill 	idcode |= (uint32_t)INT_READ(sc, IDCODE_31_24) << 24;
    406  1.2  jmcneill 	I2C_UNLOCK(sc);
    407  1.2  jmcneill 
    408  1.2  jmcneill 	aprint_normal(", IDCODE 0x%08x\n", idcode);
    409  1.1  jmcneill }
    410  1.1  jmcneill 
    411  1.1  jmcneill CFATTACH_DECL_NEW(twl, sizeof(struct twl_softc),
    412  1.1  jmcneill     twl_match, twl_attach, NULL, NULL);
    413