Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: ziic.c,v 1.7 2025/09/15 13:23:03 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by NONAKA Kimihiro.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: ziic.c,v 1.7 2025/09/15 13:23:03 thorpej Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/device.h>
     38 #include <sys/bus.h>
     39 #include <sys/mutex.h>
     40 
     41 #include <dev/i2c/i2cvar.h>
     42 
     43 #include <arm/xscale/pxa2x0reg.h>
     44 #include <arm/xscale/pxa2x0var.h>
     45 #include <arm/xscale/pxa2x0_i2c.h>
     46 
     47 #ifdef PXAIIC_DEBUG
     48 #define	DPRINTF(s)	printf s
     49 #else
     50 #define	DPRINTF(s)	do { } while (/*CONSTCOND*/0)
     51 #endif
     52 
     53 struct pxaiic_softc {
     54 	struct pxa2x0_i2c_softc	sc_pxa_i2c;
     55 	void *			sc_ih;
     56 
     57 	struct i2c_controller	sc_i2c;
     58 };
     59 
     60 static int pxaiic_match(device_t, cfdata_t, void *);
     61 static void pxaiic_attach(device_t, device_t, void *);
     62 
     63 CFATTACH_DECL_NEW(pxaiic, sizeof(struct pxaiic_softc),
     64     pxaiic_match, pxaiic_attach, NULL, NULL);
     65 
     66 static int pxaiic_acquire_bus(void *, int);
     67 static void pxaiic_release_bus(void *, int);
     68 static int pxaiic_send_start(void *, int);
     69 static int pxaiic_send_stop(void *, int);
     70 static int pxaiic_initiate_xfer(void *, uint16_t, int);
     71 static int pxaiic_read_byte(void *, uint8_t *, int);
     72 static int pxaiic_write_byte(void *, uint8_t, int);
     73 
     74 static int
     75 pxaiic_match(device_t parent, cfdata_t cf, void *aux)
     76 {
     77 	struct pxaip_attach_args *pxa = aux;
     78 
     79 	if (strcmp(cf->cf_name, pxa->pxa_name))
     80 		return 0;
     81 
     82 	pxa->pxa_addr = PXA2X0_I2C_BASE;
     83 	pxa->pxa_size = PXA2X0_I2C_SIZE;
     84 	return 1;
     85 }
     86 
     87 static void
     88 pxaiic_attach(device_t parent, device_t self, void *aux)
     89 {
     90 	struct pxaiic_softc *sc = device_private(self);
     91 	struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
     92 	struct pxaip_attach_args *pxa = aux;
     93 
     94 	aprint_normal(": I2C controller\n");
     95 	aprint_naive("\n");
     96 
     97 	psc->sc_dev = self;
     98 	psc->sc_iot = pxa->pxa_iot;
     99 	psc->sc_addr = pxa->pxa_addr;
    100 	psc->sc_size = pxa->pxa_size;
    101 	psc->sc_flags = 0;
    102 	if (pxa2x0_i2c_attach_sub(psc)) {
    103 		aprint_error_dev(self, "unable to attach PXA I2C controller\n");
    104 		return;
    105 	}
    106 
    107 #if 0
    108 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_TTY);
    109 
    110 	sc->sc_ih = pxa2x0_intr_establish(PXA2X0_INT_I2C, IPL_TTY,
    111 	    pxa2x0_i2c_intr, &psc);
    112 	if (sc->sc_ih == NULL) {
    113 		aprint_error_dev(self, "unable to establish intr\n");
    114 		return;	/* XXX: mutex_destroy, bus_space_unmap */
    115 	}
    116 #endif
    117 
    118 	/* Initialize i2c_controller */
    119 	iic_tag_init(&sc->sc_i2c);
    120 	sc->sc_i2c.ic_cookie = sc;
    121 	sc->sc_i2c.ic_acquire_bus = pxaiic_acquire_bus;
    122 	sc->sc_i2c.ic_release_bus = pxaiic_release_bus;
    123 	sc->sc_i2c.ic_send_start = pxaiic_send_start;
    124 	sc->sc_i2c.ic_send_stop = pxaiic_send_stop;
    125 	sc->sc_i2c.ic_initiate_xfer = pxaiic_initiate_xfer;
    126 	sc->sc_i2c.ic_read_byte = pxaiic_read_byte;
    127 	sc->sc_i2c.ic_write_byte = pxaiic_write_byte;
    128 
    129 	iicbus_attach(psc->sc_dev, &sc->sc_i2c);
    130 }
    131 
    132 static int
    133 pxaiic_acquire_bus(void *cookie, int flags)
    134 {
    135 	struct pxaiic_softc *sc = cookie;
    136 	struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
    137 
    138 	pxa2x0_i2c_open(psc);
    139 
    140 	return 0;
    141 }
    142 
    143 static void
    144 pxaiic_release_bus(void *cookie, int flags)
    145 {
    146 	struct pxaiic_softc *sc = cookie;
    147 	struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
    148 
    149 	pxa2x0_i2c_close(psc);
    150 }
    151 
    152 static int
    153 pxaiic_send_start(void *cookie, int flags)
    154 {
    155 	struct pxaiic_softc *sc = cookie;
    156 	struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
    157 
    158 	return pxa2x0_i2c_send_start(psc, flags);
    159 }
    160 
    161 static int
    162 pxaiic_send_stop(void *cookie, int flags)
    163 {
    164 	struct pxaiic_softc *sc = cookie;
    165 	struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
    166 
    167 	return pxa2x0_i2c_send_stop(psc, flags);
    168 }
    169 
    170 static int
    171 pxaiic_initiate_xfer(void *cookie, uint16_t addr, int flags)
    172 {
    173 	struct pxaiic_softc *sc = cookie;
    174 	struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
    175 
    176 	return pxa2x0_i2c_initiate_xfer(psc, addr, flags);
    177 }
    178 
    179 static int
    180 pxaiic_read_byte(void *cookie, uint8_t *bytep, int flags)
    181 {
    182 	struct pxaiic_softc *sc = cookie;
    183 	struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
    184 
    185 	return pxa2x0_i2c_read_byte(psc, bytep, flags);
    186 }
    187 
    188 static int
    189 pxaiic_write_byte(void *cookie, uint8_t byte, int flags)
    190 {
    191 	struct pxaiic_softc *sc = cookie;
    192 	struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
    193 
    194 	return pxa2x0_i2c_write_byte(psc, byte, flags);
    195 }
    196