Home | History | Annotate | Line # | Download | only in dev
nbpiic.c revision 1.4.8.1
      1  1.4.8.1   thorpej /*	$NetBSD: nbpiic.c,v 1.4.8.1 2021/08/04 01:08:47 thorpej Exp $ */
      2      1.1  kiyohara /*
      3      1.1  kiyohara  * Copyright (c) 2011 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.4.8.1   thorpej __KERNEL_RCSID(0, "$NetBSD: nbpiic.c,v 1.4.8.1 2021/08/04 01:08:47 thorpej 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/mutex.h>
     33      1.1  kiyohara 
     34      1.1  kiyohara #include <machine/platid.h>
     35      1.1  kiyohara #include <machine/platid_mask.h>
     36      1.1  kiyohara 
     37      1.1  kiyohara #include <arm/xscale/pxa2x0var.h>
     38      1.1  kiyohara #include <arm/xscale/pxa2x0_i2c.h>
     39      1.1  kiyohara 
     40      1.1  kiyohara #include <hpcarm/dev/nbppconvar.h>
     41      1.1  kiyohara 
     42      1.1  kiyohara #include <dev/i2c/i2cvar.h>
     43      1.1  kiyohara 
     44      1.1  kiyohara #include "nbppcon.h"
     45      1.1  kiyohara 
     46      1.1  kiyohara #define NBPIIC_SLAVE_ADDR	0x70
     47      1.1  kiyohara 
     48      1.1  kiyohara struct nbpiic_softc {
     49      1.1  kiyohara 	struct pxa2x0_i2c_softc sc_pxa_i2c;
     50      1.1  kiyohara 
     51      1.1  kiyohara 	struct i2c_controller sc_i2c;
     52      1.1  kiyohara 	kmutex_t sc_lock;
     53      1.1  kiyohara 
     54      1.1  kiyohara 	device_t sc_pcon;
     55      1.1  kiyohara 	char sc_buf[16];
     56      1.1  kiyohara 	int sc_idx;
     57      1.1  kiyohara };
     58      1.1  kiyohara 
     59      1.1  kiyohara 
     60      1.1  kiyohara static int pxaiic_match(device_t, cfdata_t, void *);
     61      1.1  kiyohara static void pxaiic_attach(device_t, device_t, void *);
     62      1.1  kiyohara 
     63      1.1  kiyohara static int nbpiic_intr(void *);
     64      1.1  kiyohara int nbpiic_poll(void *, int, char *);
     65      1.1  kiyohara 
     66      1.1  kiyohara /* fuctions for i2c_controller */
     67      1.1  kiyohara static int nbpiic_exec(void *cookie, i2c_op_t, i2c_addr_t, const void *, size_t,
     68      1.1  kiyohara     void *, size_t, int);
     69      1.1  kiyohara 
     70      1.1  kiyohara CFATTACH_DECL_NEW(pxaiic, sizeof(struct nbpiic_softc),
     71      1.1  kiyohara     pxaiic_match, pxaiic_attach, NULL, NULL);
     72      1.1  kiyohara 
     73      1.1  kiyohara 
     74      1.1  kiyohara /* ARGSUSED */
     75      1.1  kiyohara static int
     76      1.1  kiyohara pxaiic_match(device_t parent, cfdata_t match, void *aux)
     77      1.1  kiyohara {
     78      1.1  kiyohara 	struct pxaip_attach_args *pxa = aux;
     79      1.1  kiyohara 
     80      1.1  kiyohara 	if (strcmp(pxa->pxa_name, match->cf_name) != 0 ||
     81      1.1  kiyohara 	    !platid_match(&platid, &platid_mask_MACH_PSIONTEKLOGIX_NETBOOK_PRO))
     82      1.1  kiyohara 		return 0;
     83      1.1  kiyohara 
     84      1.1  kiyohara 	pxa->pxa_size = PXA2X0_I2C_SIZE;
     85      1.1  kiyohara 	return 1;
     86      1.1  kiyohara }
     87      1.1  kiyohara 
     88      1.1  kiyohara /* ARGSUSED */
     89      1.1  kiyohara static void
     90      1.1  kiyohara pxaiic_attach(device_t parent, device_t self, void *aux)
     91      1.1  kiyohara {
     92      1.1  kiyohara 	struct nbpiic_softc *sc = device_private(self);
     93      1.1  kiyohara 	struct pxaip_attach_args *pxa = aux;
     94      1.1  kiyohara 	struct i2cbus_attach_args iba;
     95      1.1  kiyohara 	void *ih;
     96      1.1  kiyohara 
     97      1.1  kiyohara 	aprint_normal("\n");
     98      1.1  kiyohara 	aprint_naive("\n");
     99      1.1  kiyohara 
    100      1.1  kiyohara 	sc->sc_pxa_i2c.sc_dev = self;
    101      1.1  kiyohara 	sc->sc_pxa_i2c.sc_iot = pxa->pxa_iot;
    102      1.1  kiyohara 	sc->sc_pxa_i2c.sc_addr = pxa->pxa_addr;
    103      1.1  kiyohara 	sc->sc_pxa_i2c.sc_size = pxa->pxa_size;
    104      1.1  kiyohara 	sc->sc_pxa_i2c.sc_isar = NBPIIC_SLAVE_ADDR;
    105      1.1  kiyohara 	sc->sc_pxa_i2c.sc_stat = PI2C_STAT_INIT;
    106      1.1  kiyohara 	if (pxa2x0_i2c_attach_sub(&sc->sc_pxa_i2c)) {
    107      1.1  kiyohara 		aprint_error_dev(self, "unable to attach PXA I2C\n");
    108      1.1  kiyohara 		return;
    109      1.1  kiyohara 	}
    110      1.1  kiyohara 
    111      1.3   thorpej 	/*
    112      1.3   thorpej 	 * Initialize mutex with IPL_HIGH.  Keyboard was connected to us.
    113      1.3   thorpej 	 * This is orthogonal to the lock held at the i2c layer; this
    114      1.3   thorpej 	 * is just to interlock us with the keyboard interrupt.
    115      1.3   thorpej 	 */
    116      1.1  kiyohara 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH);
    117      1.1  kiyohara 
    118      1.1  kiyohara 	ih = pxa2x0_intr_establish(pxa->pxa_intr, IPL_HIGH, nbpiic_intr, sc);
    119      1.1  kiyohara 	if (ih == NULL) {
    120      1.1  kiyohara 		aprint_error_dev(self, "intr_establish failed\n");
    121      1.1  kiyohara 		return;
    122      1.1  kiyohara 	}
    123      1.1  kiyohara 
    124      1.1  kiyohara 	/* Initialize i2c_controller  */
    125      1.3   thorpej 	iic_tag_init(&sc->sc_i2c);
    126      1.1  kiyohara 	sc->sc_i2c.ic_cookie = sc;
    127      1.1  kiyohara 	sc->sc_i2c.ic_exec = nbpiic_exec;
    128      1.1  kiyohara 
    129      1.2       chs 	memset(&iba, 0, sizeof(iba));
    130      1.1  kiyohara 	iba.iba_tag = &sc->sc_i2c;
    131      1.1  kiyohara 	pxa2x0_i2c_open(&sc->sc_pxa_i2c);
    132  1.4.8.1   thorpej 	config_found(self, &iba, iicbus_print, CFARGS_NONE);
    133      1.1  kiyohara 
    134      1.1  kiyohara 	sc->sc_pcon = device_find_by_xname("nbppcon0");
    135      1.1  kiyohara 
    136      1.1  kiyohara 	/* Don't close I2C-bus.  We are slave device. */
    137      1.1  kiyohara }
    138      1.1  kiyohara 
    139      1.1  kiyohara static int
    140      1.1  kiyohara nbpiic_intr(void *arg)
    141      1.1  kiyohara {
    142      1.1  kiyohara 	struct nbpiic_softc *sc = arg;
    143      1.1  kiyohara 	struct pxa2x0_i2c_softc *pi2c = &sc->sc_pxa_i2c;
    144      1.1  kiyohara 	int handled, len;
    145      1.1  kiyohara 
    146      1.1  kiyohara 	handled = pxa2x0_i2c_intr_sub(pi2c, &len, &sc->sc_buf[sc->sc_idx]);
    147      1.1  kiyohara 	if (len != 0)
    148      1.1  kiyohara 		sc->sc_idx += len;
    149      1.1  kiyohara 	if (pi2c->sc_stat == PI2C_STAT_STOP) {
    150      1.1  kiyohara #if NNBPPCON > 0
    151      1.1  kiyohara 		nbppcon_intr(sc->sc_pcon, sc->sc_idx, sc->sc_buf);
    152      1.1  kiyohara #endif
    153      1.1  kiyohara 		sc->sc_idx = 0;
    154      1.1  kiyohara 		pi2c->sc_stat = PI2C_STAT_INIT;
    155      1.1  kiyohara 	}
    156      1.1  kiyohara 
    157      1.1  kiyohara 	return handled;
    158      1.1  kiyohara }
    159      1.1  kiyohara 
    160      1.1  kiyohara int
    161      1.1  kiyohara nbpiic_poll(void *cookie, int buflen, char *buf)
    162      1.1  kiyohara {
    163      1.1  kiyohara 	struct nbpiic_softc *sc = cookie;
    164      1.1  kiyohara 	int rv;
    165      1.1  kiyohara 
    166      1.1  kiyohara 	if (sc->sc_idx > 0) {
    167      1.1  kiyohara 		if (buflen < sc->sc_idx) {
    168      1.1  kiyohara 			(void) pxa2x0_i2c_poll(&sc->sc_pxa_i2c,
    169      1.1  kiyohara 			    sizeof(sc->sc_buf) - sc->sc_idx, buf + sc->sc_idx,
    170      1.1  kiyohara 			    I2C_F_READ);
    171      1.1  kiyohara 			sc->sc_idx = 0;
    172      1.1  kiyohara 		} else
    173      1.1  kiyohara 			memcpy(buf, sc->sc_buf, sc->sc_idx);
    174      1.1  kiyohara 	}
    175      1.1  kiyohara 	rv = pxa2x0_i2c_poll(&sc->sc_pxa_i2c,
    176      1.1  kiyohara 	    buflen - sc->sc_idx, buf + sc->sc_idx, I2C_F_READ);
    177      1.1  kiyohara 	sc->sc_idx = 0;
    178      1.1  kiyohara 
    179      1.1  kiyohara 	return rv;
    180      1.1  kiyohara }
    181      1.1  kiyohara 
    182      1.1  kiyohara static int
    183      1.1  kiyohara nbpiic_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
    184      1.1  kiyohara 	    size_t cmdlen, void *vbuf, size_t buflen, int flags)
    185      1.1  kiyohara {
    186      1.1  kiyohara 	struct nbpiic_softc *sc = cookie;
    187      1.1  kiyohara 	int rv = -1;
    188      1.1  kiyohara 	const u_char cmd = *(const u_char *)vcmd;
    189      1.1  kiyohara 
    190      1.3   thorpej 	mutex_enter(&sc->sc_lock);
    191      1.3   thorpej 
    192      1.1  kiyohara 	if (I2C_OP_READ_P(op) && (cmdlen == 0) && (buflen == 1))
    193      1.1  kiyohara 		rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c, addr, (u_char *)vbuf);
    194      1.1  kiyohara 
    195      1.1  kiyohara 	if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 1)) {
    196      1.1  kiyohara 		rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, cmd);
    197      1.1  kiyohara 		if (rv == 0)
    198      1.1  kiyohara 			rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c,
    199      1.1  kiyohara 			    addr, (u_char *)vbuf);
    200      1.1  kiyohara 	}
    201      1.1  kiyohara 
    202      1.1  kiyohara 	if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 2)) {
    203      1.1  kiyohara printf("%s: read cmdlen=1, buflen=2: Ooops, maybe error...\n", __func__);
    204      1.1  kiyohara 		rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, cmd);
    205      1.1  kiyohara 		if (rv == 0)
    206      1.1  kiyohara 			rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c,
    207      1.1  kiyohara 			    addr, &((u_char *)vbuf)[0]);
    208      1.1  kiyohara 		if (rv == 0)
    209      1.1  kiyohara 			rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c,
    210      1.1  kiyohara 			    addr, &((u_char *)vbuf)[1]);
    211      1.1  kiyohara 	}
    212      1.1  kiyohara 
    213      1.1  kiyohara 	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 0) && (buflen == 1))
    214      1.1  kiyohara 		rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, *(u_char *)vbuf);
    215      1.1  kiyohara 
    216      1.1  kiyohara 	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 1)) {
    217      1.1  kiyohara 		u_short v = (cmd << 8) | ((u_char *)vbuf)[0];
    218      1.1  kiyohara 
    219      1.1  kiyohara 		rv = pxa2x0_i2c_write_2(&sc->sc_pxa_i2c, addr, v);
    220      1.1  kiyohara 	}
    221      1.1  kiyohara 
    222      1.1  kiyohara 	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 2)) {
    223      1.1  kiyohara printf("%s: write cmdlen=1, buflen=2: Ooops, maybe error...\n", __func__);
    224      1.1  kiyohara 		rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, cmd);
    225      1.1  kiyohara 		if (rv == 0) {
    226      1.1  kiyohara 			u_short v =
    227      1.1  kiyohara 			    (((u_char *)vbuf)[0] << 8) | ((u_char *)vbuf)[1];
    228      1.1  kiyohara 
    229      1.1  kiyohara 			rv = pxa2x0_i2c_write_2(&sc->sc_pxa_i2c, addr, v);
    230      1.1  kiyohara 		}
    231      1.1  kiyohara 	}
    232      1.1  kiyohara 
    233      1.1  kiyohara 	/* Handle quick_read/quick_write ops - XXX Untested XXX */
    234      1.1  kiyohara 	if ((cmdlen == 0) && (buflen == 0))
    235      1.1  kiyohara 		rv = pxa2x0_i2c_quick(&sc->sc_pxa_i2c, addr,
    236      1.1  kiyohara 			I2C_OP_READ_P(op) ? 1 : 0);
    237      1.1  kiyohara 
    238      1.3   thorpej 	mutex_exit(&sc->sc_lock);
    239      1.3   thorpej 
    240      1.1  kiyohara 	return rv;
    241      1.1  kiyohara }
    242