1 1.7 thorpej /* $NetBSD: nbpiic.c,v 1.7 2025/09/15 13:23:01 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.7 thorpej __KERNEL_RCSID(0, "$NetBSD: nbpiic.c,v 1.7 2025/09/15 13:23:01 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.6 andvar /* functions 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 void *ih; 95 1.1 kiyohara 96 1.1 kiyohara aprint_normal("\n"); 97 1.1 kiyohara aprint_naive("\n"); 98 1.1 kiyohara 99 1.1 kiyohara sc->sc_pxa_i2c.sc_dev = self; 100 1.1 kiyohara sc->sc_pxa_i2c.sc_iot = pxa->pxa_iot; 101 1.1 kiyohara sc->sc_pxa_i2c.sc_addr = pxa->pxa_addr; 102 1.1 kiyohara sc->sc_pxa_i2c.sc_size = pxa->pxa_size; 103 1.1 kiyohara sc->sc_pxa_i2c.sc_isar = NBPIIC_SLAVE_ADDR; 104 1.1 kiyohara sc->sc_pxa_i2c.sc_stat = PI2C_STAT_INIT; 105 1.1 kiyohara if (pxa2x0_i2c_attach_sub(&sc->sc_pxa_i2c)) { 106 1.1 kiyohara aprint_error_dev(self, "unable to attach PXA I2C\n"); 107 1.1 kiyohara return; 108 1.1 kiyohara } 109 1.1 kiyohara 110 1.3 thorpej /* 111 1.3 thorpej * Initialize mutex with IPL_HIGH. Keyboard was connected to us. 112 1.3 thorpej * This is orthogonal to the lock held at the i2c layer; this 113 1.3 thorpej * is just to interlock us with the keyboard interrupt. 114 1.3 thorpej */ 115 1.1 kiyohara mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH); 116 1.1 kiyohara 117 1.1 kiyohara ih = pxa2x0_intr_establish(pxa->pxa_intr, IPL_HIGH, nbpiic_intr, sc); 118 1.1 kiyohara if (ih == NULL) { 119 1.1 kiyohara aprint_error_dev(self, "intr_establish failed\n"); 120 1.1 kiyohara return; 121 1.1 kiyohara } 122 1.1 kiyohara 123 1.1 kiyohara /* Initialize i2c_controller */ 124 1.3 thorpej iic_tag_init(&sc->sc_i2c); 125 1.1 kiyohara sc->sc_i2c.ic_cookie = sc; 126 1.1 kiyohara sc->sc_i2c.ic_exec = nbpiic_exec; 127 1.1 kiyohara 128 1.1 kiyohara pxa2x0_i2c_open(&sc->sc_pxa_i2c); 129 1.7 thorpej 130 1.7 thorpej iicbus_attach(self, &sc->sc_i2c); 131 1.1 kiyohara 132 1.1 kiyohara sc->sc_pcon = device_find_by_xname("nbppcon0"); 133 1.1 kiyohara 134 1.1 kiyohara /* Don't close I2C-bus. We are slave device. */ 135 1.1 kiyohara } 136 1.1 kiyohara 137 1.1 kiyohara static int 138 1.1 kiyohara nbpiic_intr(void *arg) 139 1.1 kiyohara { 140 1.1 kiyohara struct nbpiic_softc *sc = arg; 141 1.1 kiyohara struct pxa2x0_i2c_softc *pi2c = &sc->sc_pxa_i2c; 142 1.1 kiyohara int handled, len; 143 1.1 kiyohara 144 1.1 kiyohara handled = pxa2x0_i2c_intr_sub(pi2c, &len, &sc->sc_buf[sc->sc_idx]); 145 1.1 kiyohara if (len != 0) 146 1.1 kiyohara sc->sc_idx += len; 147 1.1 kiyohara if (pi2c->sc_stat == PI2C_STAT_STOP) { 148 1.1 kiyohara #if NNBPPCON > 0 149 1.1 kiyohara nbppcon_intr(sc->sc_pcon, sc->sc_idx, sc->sc_buf); 150 1.1 kiyohara #endif 151 1.1 kiyohara sc->sc_idx = 0; 152 1.1 kiyohara pi2c->sc_stat = PI2C_STAT_INIT; 153 1.1 kiyohara } 154 1.1 kiyohara 155 1.1 kiyohara return handled; 156 1.1 kiyohara } 157 1.1 kiyohara 158 1.1 kiyohara int 159 1.1 kiyohara nbpiic_poll(void *cookie, int buflen, char *buf) 160 1.1 kiyohara { 161 1.1 kiyohara struct nbpiic_softc *sc = cookie; 162 1.1 kiyohara int rv; 163 1.1 kiyohara 164 1.1 kiyohara if (sc->sc_idx > 0) { 165 1.1 kiyohara if (buflen < sc->sc_idx) { 166 1.1 kiyohara (void) pxa2x0_i2c_poll(&sc->sc_pxa_i2c, 167 1.1 kiyohara sizeof(sc->sc_buf) - sc->sc_idx, buf + sc->sc_idx, 168 1.1 kiyohara I2C_F_READ); 169 1.1 kiyohara sc->sc_idx = 0; 170 1.1 kiyohara } else 171 1.1 kiyohara memcpy(buf, sc->sc_buf, sc->sc_idx); 172 1.1 kiyohara } 173 1.1 kiyohara rv = pxa2x0_i2c_poll(&sc->sc_pxa_i2c, 174 1.1 kiyohara buflen - sc->sc_idx, buf + sc->sc_idx, I2C_F_READ); 175 1.1 kiyohara sc->sc_idx = 0; 176 1.1 kiyohara 177 1.1 kiyohara return rv; 178 1.1 kiyohara } 179 1.1 kiyohara 180 1.1 kiyohara static int 181 1.1 kiyohara nbpiic_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd, 182 1.1 kiyohara size_t cmdlen, void *vbuf, size_t buflen, int flags) 183 1.1 kiyohara { 184 1.1 kiyohara struct nbpiic_softc *sc = cookie; 185 1.1 kiyohara int rv = -1; 186 1.1 kiyohara const u_char cmd = *(const u_char *)vcmd; 187 1.1 kiyohara 188 1.3 thorpej mutex_enter(&sc->sc_lock); 189 1.3 thorpej 190 1.1 kiyohara if (I2C_OP_READ_P(op) && (cmdlen == 0) && (buflen == 1)) 191 1.1 kiyohara rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c, addr, (u_char *)vbuf); 192 1.1 kiyohara 193 1.1 kiyohara if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 1)) { 194 1.1 kiyohara rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, cmd); 195 1.1 kiyohara if (rv == 0) 196 1.1 kiyohara rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c, 197 1.1 kiyohara addr, (u_char *)vbuf); 198 1.1 kiyohara } 199 1.1 kiyohara 200 1.1 kiyohara if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 2)) { 201 1.1 kiyohara printf("%s: read cmdlen=1, buflen=2: Ooops, maybe error...\n", __func__); 202 1.1 kiyohara rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, cmd); 203 1.1 kiyohara if (rv == 0) 204 1.1 kiyohara rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c, 205 1.1 kiyohara addr, &((u_char *)vbuf)[0]); 206 1.1 kiyohara if (rv == 0) 207 1.1 kiyohara rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c, 208 1.1 kiyohara addr, &((u_char *)vbuf)[1]); 209 1.1 kiyohara } 210 1.1 kiyohara 211 1.1 kiyohara if ((I2C_OP_WRITE_P(op)) && (cmdlen == 0) && (buflen == 1)) 212 1.1 kiyohara rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, *(u_char *)vbuf); 213 1.1 kiyohara 214 1.1 kiyohara if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 1)) { 215 1.1 kiyohara u_short v = (cmd << 8) | ((u_char *)vbuf)[0]; 216 1.1 kiyohara 217 1.1 kiyohara rv = pxa2x0_i2c_write_2(&sc->sc_pxa_i2c, addr, v); 218 1.1 kiyohara } 219 1.1 kiyohara 220 1.1 kiyohara if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 2)) { 221 1.1 kiyohara printf("%s: write cmdlen=1, buflen=2: Ooops, maybe error...\n", __func__); 222 1.1 kiyohara rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, cmd); 223 1.1 kiyohara if (rv == 0) { 224 1.1 kiyohara u_short v = 225 1.1 kiyohara (((u_char *)vbuf)[0] << 8) | ((u_char *)vbuf)[1]; 226 1.1 kiyohara 227 1.1 kiyohara rv = pxa2x0_i2c_write_2(&sc->sc_pxa_i2c, addr, v); 228 1.1 kiyohara } 229 1.1 kiyohara } 230 1.1 kiyohara 231 1.1 kiyohara /* Handle quick_read/quick_write ops - XXX Untested XXX */ 232 1.1 kiyohara if ((cmdlen == 0) && (buflen == 0)) 233 1.1 kiyohara rv = pxa2x0_i2c_quick(&sc->sc_pxa_i2c, addr, 234 1.1 kiyohara I2C_OP_READ_P(op) ? 1 : 0); 235 1.1 kiyohara 236 1.3 thorpej mutex_exit(&sc->sc_lock); 237 1.3 thorpej 238 1.1 kiyohara return rv; 239 1.1 kiyohara } 240