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