1 /* $NetBSD: ziic.c,v 1.3 2016/02/14 19:54:21 chs Exp $ */ 2 3 /*- 4 * Copyright (c) 2011 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by NONAKA Kimihiro. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: ziic.c,v 1.3 2016/02/14 19:54:21 chs Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/device.h> 38 #include <sys/bus.h> 39 #include <sys/mutex.h> 40 41 #include <dev/i2c/i2cvar.h> 42 43 #include <arm/xscale/pxa2x0reg.h> 44 #include <arm/xscale/pxa2x0var.h> 45 #include <arm/xscale/pxa2x0_i2c.h> 46 47 #ifdef PXAIIC_DEBUG 48 #define DPRINTF(s) printf s 49 #else 50 #define DPRINTF(s) do { } while (/*CONSTCOND*/0) 51 #endif 52 53 struct pxaiic_softc { 54 struct pxa2x0_i2c_softc sc_pxa_i2c; 55 void * sc_ih; 56 57 struct i2c_controller sc_i2c; 58 kmutex_t sc_buslock; 59 }; 60 61 static int pxaiic_match(device_t, cfdata_t, void *); 62 static void pxaiic_attach(device_t, device_t, void *); 63 64 CFATTACH_DECL_NEW(pxaiic, sizeof(struct pxaiic_softc), 65 pxaiic_match, pxaiic_attach, NULL, NULL); 66 67 static int pxaiic_acquire_bus(void *, int); 68 static void pxaiic_release_bus(void *, int); 69 static int pxaiic_send_start(void *, int); 70 static int pxaiic_send_stop(void *, int); 71 static int pxaiic_initiate_xfer(void *, uint16_t, int); 72 static int pxaiic_read_byte(void *, uint8_t *, int); 73 static int pxaiic_write_byte(void *, uint8_t, int); 74 75 static int 76 pxaiic_match(device_t parent, cfdata_t cf, void *aux) 77 { 78 struct pxaip_attach_args *pxa = aux; 79 80 if (strcmp(cf->cf_name, pxa->pxa_name)) 81 return 0; 82 83 pxa->pxa_addr = PXA2X0_I2C_BASE; 84 pxa->pxa_size = PXA2X0_I2C_SIZE; 85 return 1; 86 } 87 88 static void 89 pxaiic_attach(device_t parent, device_t self, void *aux) 90 { 91 struct pxaiic_softc *sc = device_private(self); 92 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c; 93 struct pxaip_attach_args *pxa = aux; 94 struct i2cbus_attach_args iba; 95 96 aprint_normal(": I2C controller\n"); 97 aprint_naive("\n"); 98 99 psc->sc_dev = self; 100 psc->sc_iot = pxa->pxa_iot; 101 psc->sc_addr = pxa->pxa_addr; 102 psc->sc_size = pxa->pxa_size; 103 psc->sc_flags = 0; 104 if (pxa2x0_i2c_attach_sub(psc)) { 105 aprint_error_dev(self, "unable to attach PXA I2C controller\n"); 106 return; 107 } 108 109 mutex_init(&sc->sc_buslock, MUTEX_DEFAULT, IPL_TTY); 110 111 #if 0 112 sc->sc_ih = pxa2x0_intr_establish(PXA2X0_INT_I2C, IPL_TTY, 113 pxa2x0_i2c_intr, &psc); 114 if (sc->sc_ih == NULL) { 115 aprint_error_dev(self, "unable to establish intr\n"); 116 return; /* XXX: mutex_destroy, bus_space_unmap */ 117 } 118 #endif 119 120 /* Initialize i2c_controller */ 121 sc->sc_i2c.ic_cookie = sc; 122 sc->sc_i2c.ic_acquire_bus = pxaiic_acquire_bus; 123 sc->sc_i2c.ic_release_bus = pxaiic_release_bus; 124 sc->sc_i2c.ic_send_start = pxaiic_send_start; 125 sc->sc_i2c.ic_send_stop = pxaiic_send_stop; 126 sc->sc_i2c.ic_initiate_xfer = pxaiic_initiate_xfer; 127 sc->sc_i2c.ic_read_byte = pxaiic_read_byte; 128 sc->sc_i2c.ic_write_byte = pxaiic_write_byte; 129 sc->sc_i2c.ic_exec = NULL; 130 131 memset(&iba, 0, sizeof(iba)); 132 iba.iba_tag = &sc->sc_i2c; 133 (void)config_found_ia(psc->sc_dev, "i2cbus", &iba, iicbus_print); 134 } 135 136 static int 137 pxaiic_acquire_bus(void *cookie, int flags) 138 { 139 struct pxaiic_softc *sc = cookie; 140 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c; 141 142 mutex_enter(&sc->sc_buslock); 143 pxa2x0_i2c_open(psc); 144 145 return 0; 146 } 147 148 static void 149 pxaiic_release_bus(void *cookie, int flags) 150 { 151 struct pxaiic_softc *sc = cookie; 152 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c; 153 154 pxa2x0_i2c_close(psc); 155 mutex_exit(&sc->sc_buslock); 156 } 157 158 static int 159 pxaiic_send_start(void *cookie, int flags) 160 { 161 struct pxaiic_softc *sc = cookie; 162 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c; 163 164 return pxa2x0_i2c_send_start(psc, flags); 165 } 166 167 static int 168 pxaiic_send_stop(void *cookie, int flags) 169 { 170 struct pxaiic_softc *sc = cookie; 171 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c; 172 173 return pxa2x0_i2c_send_stop(psc, flags); 174 } 175 176 static int 177 pxaiic_initiate_xfer(void *cookie, uint16_t addr, int flags) 178 { 179 struct pxaiic_softc *sc = cookie; 180 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c; 181 182 return pxa2x0_i2c_initiate_xfer(psc, addr, flags); 183 } 184 185 static int 186 pxaiic_read_byte(void *cookie, uint8_t *bytep, int flags) 187 { 188 struct pxaiic_softc *sc = cookie; 189 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c; 190 191 return pxa2x0_i2c_read_byte(psc, bytep, flags); 192 } 193 194 static int 195 pxaiic_write_byte(void *cookie, uint8_t byte, int flags) 196 { 197 struct pxaiic_softc *sc = cookie; 198 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c; 199 200 return pxa2x0_i2c_write_byte(psc, byte, flags); 201 } 202