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