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