1/* $NetBSD: ziic.c,v 1.1 2011/06/19 16:20:09 nonaka 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.1 2011/06/19 16:20:09 nonaka 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 53struct 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 61static int pxaiic_match(device_t, cfdata_t, void *); 62static void pxaiic_attach(device_t, device_t, void *); 63 64CFATTACH_DECL_NEW(pxaiic, sizeof(struct pxaiic_softc), 65 pxaiic_match, pxaiic_attach, NULL, NULL); 66 67static int pxaiic_acquire_bus(void *, int); 68static void pxaiic_release_bus(void *, int); 69static int pxaiic_send_start(void *, int); 70static int pxaiic_send_stop(void *, int); 71static int pxaiic_initiate_xfer(void *, uint16_t, int); 72static int pxaiic_read_byte(void *, uint8_t *, int); 73static int pxaiic_write_byte(void *, uint8_t, int); 74 75static int 76pxaiic_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 88static void 89pxaiic_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 iba.iba_tag = &sc->sc_i2c; 132 (void)config_found_ia(psc->sc_dev, "i2cbus", &iba, iicbus_print); 133} 134 135static int 136pxaiic_acquire_bus(void *cookie, int flags) 137{ 138 struct pxaiic_softc *sc = cookie; 139 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c; 140 141 mutex_enter(&sc->sc_buslock); 142 pxa2x0_i2c_open(psc); 143 144 return 0; 145} 146 147static void 148pxaiic_release_bus(void *cookie, int flags) 149{ 150 struct pxaiic_softc *sc = cookie; 151 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c; 152 153 pxa2x0_i2c_close(psc); 154 mutex_exit(&sc->sc_buslock); 155} 156 157static int 158pxaiic_send_start(void *cookie, int flags) 159{ 160 struct pxaiic_softc *sc = cookie; 161 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c; 162 163 return pxa2x0_i2c_send_start(psc, flags); 164} 165 166static int 167pxaiic_send_stop(void *cookie, int flags) 168{ 169 struct pxaiic_softc *sc = cookie; 170 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c; 171 172 return pxa2x0_i2c_send_stop(psc, flags); 173} 174 175static int 176pxaiic_initiate_xfer(void *cookie, uint16_t addr, int flags) 177{ 178 struct pxaiic_softc *sc = cookie; 179 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c; 180 181 return pxa2x0_i2c_initiate_xfer(psc, addr, flags); 182} 183 184static int 185pxaiic_read_byte(void *cookie, uint8_t *bytep, int flags) 186{ 187 struct pxaiic_softc *sc = cookie; 188 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c; 189 190 return pxa2x0_i2c_read_byte(psc, bytep, flags); 191} 192 193static int 194pxaiic_write_byte(void *cookie, uint8_t byte, int flags) 195{ 196 struct pxaiic_softc *sc = cookie; 197 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c; 198 199 return pxa2x0_i2c_write_byte(psc, byte, flags); 200} 201