Home | History | Annotate | Line # | Download | only in gumstix
      1 /*	$NetBSD: gxiic.c,v 1.13 2025/09/15 13:23:01 thorpej Exp $ */
      2 /*
      3  * Copyright (c) 2007 KIYOHARA Takashi
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  * POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: gxiic.c,v 1.13 2025/09/15 13:23:01 thorpej Exp $");
     29 
     30 #include <sys/param.h>
     31 #include <sys/device.h>
     32 #include <sys/errno.h>
     33 #include <sys/mutex.h>
     34 
     35 #include <arm/xscale/pxa2x0var.h>
     36 #include <arm/xscale/pxa2x0_i2c.h>
     37 
     38 #include <evbarm/gumstix/gumstixvar.h>
     39 
     40 #include <dev/i2c/i2cvar.h>
     41 
     42 
     43 struct gxiic_softc {
     44 	struct pxa2x0_i2c_softc sc_pxa_i2c;
     45 
     46 	struct i2c_controller sc_i2c;
     47 };
     48 
     49 
     50 static int gxiicmatch(device_t, cfdata_t, void *);
     51 static void gxiicattach(device_t, device_t, void *);
     52 
     53 /* functions for i2c_controller */
     54 static int gxiic_acquire_bus(void *, int);
     55 static void gxiic_release_bus(void *, int);
     56 static int gxiic_exec(void *cookie, i2c_op_t, i2c_addr_t, const void *, size_t,
     57     void *, size_t, int);
     58 
     59 
     60 CFATTACH_DECL_NEW(gxiic, sizeof(struct gxiic_softc),
     61     gxiicmatch, gxiicattach, NULL, NULL);
     62 
     63 
     64 /* ARGSUSED */
     65 static int
     66 gxiicmatch(device_t parent, cfdata_t match, void *aux)
     67 {
     68 	struct pxaip_attach_args *pxa = aux;
     69 
     70 	if (strcmp(pxa->pxa_name, match->cf_name) != 0)
     71 		 return 0;
     72 
     73 	pxa->pxa_size = PXA2X0_I2C_SIZE;
     74 	return 1;
     75 }
     76 
     77 /* ARGSUSED */
     78 static void
     79 gxiicattach(device_t parent, device_t self, void *aux)
     80 {
     81 	struct pxaip_attach_args *pxa = aux;
     82 	struct gxiic_softc *sc = device_private(self);
     83 
     84 	aprint_normal("\n");
     85 	aprint_naive("\n");
     86 
     87 	sc->sc_pxa_i2c.sc_dev = self;
     88 	sc->sc_pxa_i2c.sc_iot = pxa->pxa_iot;
     89 	sc->sc_pxa_i2c.sc_addr = pxa->pxa_addr;
     90 	sc->sc_pxa_i2c.sc_size = pxa->pxa_size;
     91 	sc->sc_pxa_i2c.sc_flags = 0;
     92 	if (pxa2x0_i2c_attach_sub(&sc->sc_pxa_i2c)) {
     93 		aprint_error_dev(self, "unable to attach PXA I2C\n");
     94 		return;
     95 	}
     96 
     97 	/* Initialize i2c_controller  */
     98 	iic_tag_init(&sc->sc_i2c);
     99 	sc->sc_i2c.ic_cookie = sc;
    100 	sc->sc_i2c.ic_acquire_bus = gxiic_acquire_bus;
    101 	sc->sc_i2c.ic_release_bus = gxiic_release_bus;
    102 	sc->sc_i2c.ic_exec = gxiic_exec;
    103 
    104 	pxa2x0_i2c_open(&sc->sc_pxa_i2c);
    105 	iicbus_attach(sc->sc_pxa_i2c.sc_dev, &sc->sc_i2c);
    106 	pxa2x0_i2c_close(&sc->sc_pxa_i2c);
    107 }
    108 
    109 static int
    110 gxiic_acquire_bus(void *cookie, int flags)
    111 {
    112 	struct gxiic_softc *sc = cookie;
    113 
    114 	pxa2x0_i2c_open(&sc->sc_pxa_i2c);
    115 
    116 	return 0;
    117 }
    118 
    119 static void
    120 gxiic_release_bus(void *cookie, int flags)
    121 {
    122 	struct gxiic_softc *sc = cookie;
    123 
    124 	pxa2x0_i2c_close(&sc->sc_pxa_i2c);
    125 }
    126 
    127 static int
    128 gxiic_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
    129 	   size_t cmdlen, void *vbuf, size_t buflen, int flags)
    130 {
    131 	struct gxiic_softc *sc = cookie;
    132 	int rv = -1;
    133 
    134 	if (I2C_OP_READ_P(op) && (cmdlen == 0) && (buflen == 1))
    135 		rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c, addr, (u_char *)vbuf);
    136 
    137 	if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 1)) {
    138 		rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, *(u_char *)vbuf);
    139 		if (rv == 0)
    140 			rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c,
    141 			    addr, (u_char *)vbuf);
    142 	}
    143 
    144 	if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 2)) {
    145 		rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, *(u_char *)vbuf);
    146 		if (rv == 0)
    147 			rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c,
    148 			    addr, (u_char *)vbuf);
    149 		if (rv == 0)
    150 			rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c,
    151 			    addr, (u_char *)(vbuf) + 1);
    152 	}
    153 
    154 	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 0) && (buflen == 1))
    155 		rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, *(u_char *)vbuf);
    156 
    157 	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 1)) {
    158 		rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c,
    159 		    addr, *(const u_char *)vcmd);
    160 		if (rv == 0)
    161 			rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c,
    162 			    addr, *(u_char *)vbuf);
    163 	}
    164 
    165 	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 2)) {
    166 		rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c,
    167 		    addr, *(const u_char *)vcmd);
    168 		if (rv == 0)
    169 			rv = pxa2x0_i2c_write_2(&sc->sc_pxa_i2c,
    170 			    addr, *(u_short *)vbuf);
    171 	}
    172 
    173 	/* Handle quick_read/quick_write ops - XXX Untested XXX */
    174 	if ((cmdlen == 0) && (buflen == 0))
    175 		rv = pxa2x0_i2c_quick(&sc->sc_pxa_i2c, addr,
    176 			I2C_OP_READ_P(op)?1:0);
    177 
    178 	return rv;
    179 }
    180