xref: /netbsd-src/sys/dev/usb/emdtv_i2c.c (revision 601e1783806444af16ed66b5dc3a6fef98f1fbec)
1 /* $NetBSD: emdtv_i2c.c,v 1.2 2019/12/22 23:23:32 thorpej 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.2 2019/12/22 23:23:32 thorpej 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_exec(void *, i2c_op_t, i2c_addr_t,
48 			       const void *, size_t, void *, size_t, int);
49 
50 static int	emdtv_i2c_check(struct emdtv_softc *, i2c_addr_t);
51 static int	emdtv_i2c_recv(struct emdtv_softc *, i2c_addr_t,
52 			       uint8_t *, size_t);
53 static int	emdtv_i2c_send(struct emdtv_softc *, i2c_addr_t,
54 				const uint8_t *, size_t, bool);
55 
56 int
emdtv_i2c_attach(struct emdtv_softc * sc)57 emdtv_i2c_attach(struct emdtv_softc *sc)
58 {
59 	iic_tag_init(&sc->sc_i2c);
60 	sc->sc_i2c.ic_cookie = sc;
61 	sc->sc_i2c.ic_exec = emdtv_i2c_exec;
62 
63 	return 0;
64 }
65 
66 int
emdtv_i2c_detach(struct emdtv_softc * sc,int flags)67 emdtv_i2c_detach(struct emdtv_softc *sc, int flags)
68 {
69 	iic_tag_fini(&sc->sc_i2c);
70 
71 	return 0;
72 }
73 
74 static int
emdtv_i2c_exec(void * opaque,i2c_op_t op,i2c_addr_t addr,const void * cmd,size_t cmdlen,void * vbuf,size_t buflen,int flags)75 emdtv_i2c_exec(void *opaque, i2c_op_t op, i2c_addr_t addr,
76     const void *cmd, size_t cmdlen, void *vbuf, size_t buflen, int flags)
77 {
78 	struct emdtv_softc *sc = opaque;
79 	int error;
80 
81 	if (I2C_OP_READ_P(op)) {
82 		if (buflen == 0)
83 			error = emdtv_i2c_check(sc, addr);
84 		else
85 			error = emdtv_i2c_recv(sc, addr, vbuf, buflen);
86 	} else {
87 		error = emdtv_i2c_send(sc, addr, cmd, cmdlen,
88 		    I2C_OP_STOP_P(op));
89 		error = 0;
90 	}
91 
92 	return error;
93 }
94 
95 static int
emdtv_i2c_check(struct emdtv_softc * sc,i2c_addr_t addr)96 emdtv_i2c_check(struct emdtv_softc *sc, i2c_addr_t addr)
97 {
98 	emdtv_read_1(sc, EM28XX_UR_I2C, addr);
99 	if (emdtv_read_1(sc, UR_GET_STATUS, EM28XX_REG_I2C_STATUS) != 0) {
100 		device_printf(sc->sc_dev, "%s failed\n", __func__);
101 		return ENXIO;
102 	}
103 	return 0;
104 }
105 
106 static int
emdtv_i2c_recv(struct emdtv_softc * sc,i2c_addr_t addr,uint8_t * datap,size_t len)107 emdtv_i2c_recv(struct emdtv_softc *sc, i2c_addr_t addr, uint8_t *datap,
108     size_t len)
109 {
110 	emdtv_read_multi_1(sc, EM28XX_UR_I2C, addr, datap, len);
111 	if (emdtv_read_1(sc, UR_GET_STATUS, EM28XX_REG_I2C_STATUS) != 0) {
112 		device_printf(sc->sc_dev, "%s failed\n", __func__);
113 		return ENXIO;
114 	}
115 	return 0;
116 }
117 
118 static int
emdtv_i2c_send(struct emdtv_softc * sc,i2c_addr_t addr,const uint8_t * datap,size_t len,bool stop)119 emdtv_i2c_send(struct emdtv_softc *sc, i2c_addr_t addr, const uint8_t *datap,
120     size_t len, bool stop)
121 {
122 	int off = (stop == false ? 1 : 0);
123 	emdtv_write_multi_1(sc, EM28XX_UR_I2C + off, addr, datap, len);
124 	if (emdtv_read_1(sc, UR_GET_STATUS, EM28XX_REG_I2C_STATUS) != 0) {
125 		device_printf(sc->sc_dev, "%s failed\n", __func__);
126 		return ENXIO;
127 	}
128 	return 0;
129 }
130