Home | History | Annotate | Line # | Download | only in usb
emdtv_i2c.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: emdtv_i2c.c,v 1.1 2011/07/11 18:02:04 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2008, 2010 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: emdtv_i2c.c,v 1.1 2011/07/11 18:02:04 jmcneill Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/systm.h>
     34  1.1  jmcneill #include <sys/device.h>
     35  1.1  jmcneill #include <sys/conf.h>
     36  1.1  jmcneill 
     37  1.1  jmcneill #include <dev/usb/usb.h>
     38  1.1  jmcneill #include <dev/usb/usbdi.h>
     39  1.1  jmcneill #include <dev/usb/usbdi_util.h>
     40  1.1  jmcneill #include <dev/usb/usbdevs.h>
     41  1.1  jmcneill 
     42  1.1  jmcneill #include <dev/i2c/i2cvar.h>
     43  1.1  jmcneill 
     44  1.1  jmcneill #include <dev/usb/emdtvvar.h>
     45  1.1  jmcneill #include <dev/usb/emdtvreg.h>
     46  1.1  jmcneill 
     47  1.1  jmcneill static int	emdtv_i2c_acquire_bus(void *, int);
     48  1.1  jmcneill static void	emdtv_i2c_release_bus(void *, int);
     49  1.1  jmcneill static int	emdtv_i2c_exec(void *, i2c_op_t, i2c_addr_t,
     50  1.1  jmcneill 			       const void *, size_t, void *, size_t, int);
     51  1.1  jmcneill 
     52  1.1  jmcneill static int	emdtv_i2c_check(struct emdtv_softc *, i2c_addr_t);
     53  1.1  jmcneill static int	emdtv_i2c_recv(struct emdtv_softc *, i2c_addr_t,
     54  1.1  jmcneill 			       uint8_t *, size_t);
     55  1.1  jmcneill static int	emdtv_i2c_send(struct emdtv_softc *, i2c_addr_t,
     56  1.1  jmcneill 				const uint8_t *, size_t, bool);
     57  1.1  jmcneill 
     58  1.1  jmcneill int
     59  1.1  jmcneill emdtv_i2c_attach(struct emdtv_softc *sc)
     60  1.1  jmcneill {
     61  1.1  jmcneill 	mutex_init(&sc->sc_i2c_lock, MUTEX_DEFAULT, IPL_VM);
     62  1.1  jmcneill 	sc->sc_i2c.ic_cookie = sc;
     63  1.1  jmcneill 	sc->sc_i2c.ic_acquire_bus = emdtv_i2c_acquire_bus;
     64  1.1  jmcneill 	sc->sc_i2c.ic_release_bus = emdtv_i2c_release_bus;
     65  1.1  jmcneill 	sc->sc_i2c.ic_exec = emdtv_i2c_exec;
     66  1.1  jmcneill 
     67  1.1  jmcneill 	return 0;
     68  1.1  jmcneill }
     69  1.1  jmcneill 
     70  1.1  jmcneill int
     71  1.1  jmcneill emdtv_i2c_detach(struct emdtv_softc *sc, int flags)
     72  1.1  jmcneill {
     73  1.1  jmcneill 	mutex_destroy(&sc->sc_i2c_lock);
     74  1.1  jmcneill 
     75  1.1  jmcneill 	return 0;
     76  1.1  jmcneill }
     77  1.1  jmcneill 
     78  1.1  jmcneill static int
     79  1.1  jmcneill emdtv_i2c_acquire_bus(void *opaque, int flags)
     80  1.1  jmcneill {
     81  1.1  jmcneill 	struct emdtv_softc *sc = opaque;
     82  1.1  jmcneill 
     83  1.1  jmcneill 	mutex_enter(&sc->sc_i2c_lock);
     84  1.1  jmcneill 
     85  1.1  jmcneill 	return 0;
     86  1.1  jmcneill }
     87  1.1  jmcneill 
     88  1.1  jmcneill static int
     89  1.1  jmcneill emdtv_i2c_exec(void *opaque, i2c_op_t op, i2c_addr_t addr,
     90  1.1  jmcneill     const void *cmd, size_t cmdlen, void *vbuf, size_t buflen, int flags)
     91  1.1  jmcneill {
     92  1.1  jmcneill 	struct emdtv_softc *sc = opaque;
     93  1.1  jmcneill 	int error;
     94  1.1  jmcneill 
     95  1.1  jmcneill 	if (I2C_OP_READ_P(op)) {
     96  1.1  jmcneill 		if (buflen == 0)
     97  1.1  jmcneill 			error = emdtv_i2c_check(sc, addr);
     98  1.1  jmcneill 		else
     99  1.1  jmcneill 			error = emdtv_i2c_recv(sc, addr, vbuf, buflen);
    100  1.1  jmcneill 	} else {
    101  1.1  jmcneill 		error = emdtv_i2c_send(sc, addr, cmd, cmdlen,
    102  1.1  jmcneill 		    I2C_OP_STOP_P(op));
    103  1.1  jmcneill 		error = 0;
    104  1.1  jmcneill 	}
    105  1.1  jmcneill 
    106  1.1  jmcneill 	return error;
    107  1.1  jmcneill }
    108  1.1  jmcneill 
    109  1.1  jmcneill static void
    110  1.1  jmcneill emdtv_i2c_release_bus(void *opaque, int flags)
    111  1.1  jmcneill {
    112  1.1  jmcneill 	struct emdtv_softc *sc = opaque;
    113  1.1  jmcneill 
    114  1.1  jmcneill 	mutex_exit(&sc->sc_i2c_lock);
    115  1.1  jmcneill }
    116  1.1  jmcneill 
    117  1.1  jmcneill static int
    118  1.1  jmcneill emdtv_i2c_check(struct emdtv_softc *sc, i2c_addr_t addr)
    119  1.1  jmcneill {
    120  1.1  jmcneill 	emdtv_read_1(sc, EM28XX_UR_I2C, addr);
    121  1.1  jmcneill 	if (emdtv_read_1(sc, UR_GET_STATUS, EM28XX_REG_I2C_STATUS) != 0) {
    122  1.1  jmcneill 		device_printf(sc->sc_dev, "%s failed\n", __func__);
    123  1.1  jmcneill 		return ENXIO;
    124  1.1  jmcneill 	}
    125  1.1  jmcneill 	return 0;
    126  1.1  jmcneill }
    127  1.1  jmcneill 
    128  1.1  jmcneill static int
    129  1.1  jmcneill emdtv_i2c_recv(struct emdtv_softc *sc, i2c_addr_t addr, uint8_t *datap,
    130  1.1  jmcneill     size_t len)
    131  1.1  jmcneill {
    132  1.1  jmcneill 	emdtv_read_multi_1(sc, EM28XX_UR_I2C, addr, datap, len);
    133  1.1  jmcneill 	if (emdtv_read_1(sc, UR_GET_STATUS, EM28XX_REG_I2C_STATUS) != 0) {
    134  1.1  jmcneill 		device_printf(sc->sc_dev, "%s failed\n", __func__);
    135  1.1  jmcneill 		return ENXIO;
    136  1.1  jmcneill 	}
    137  1.1  jmcneill 	return 0;
    138  1.1  jmcneill }
    139  1.1  jmcneill 
    140  1.1  jmcneill static int
    141  1.1  jmcneill emdtv_i2c_send(struct emdtv_softc *sc, i2c_addr_t addr, const uint8_t *datap,
    142  1.1  jmcneill     size_t len, bool stop)
    143  1.1  jmcneill {
    144  1.1  jmcneill 	int off = (stop == false ? 1 : 0);
    145  1.1  jmcneill 	emdtv_write_multi_1(sc, EM28XX_UR_I2C + off, addr, datap, len);
    146  1.1  jmcneill 	if (emdtv_read_1(sc, UR_GET_STATUS, EM28XX_REG_I2C_STATUS) != 0) {
    147  1.1  jmcneill 		device_printf(sc->sc_dev, "%s failed\n", __func__);
    148  1.1  jmcneill 		return ENXIO;
    149  1.1  jmcneill 	}
    150  1.1  jmcneill 	return 0;
    151  1.1  jmcneill }
    152