xref: /netbsd-src/sys/dev/usb/auvitek_i2c.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /* $NetBSD: auvitek_i2c.c,v 1.1 2010/12/27 15:42:11 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 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 /*
30  * Auvitek AU0828 USB controller - I2C access ops
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: auvitek_i2c.c,v 1.1 2010/12/27 15:42:11 jmcneill Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/conf.h>
40 #include <sys/bus.h>
41 #include <sys/module.h>
42 
43 #include <dev/usb/usb.h>
44 #include <dev/usb/usbdi.h>
45 #include <dev/usb/usbdi_util.h>
46 #include <dev/usb/usbdevs.h>
47 
48 #include <dev/i2c/i2cvar.h>
49 
50 #include <dev/usb/auvitekreg.h>
51 #include <dev/usb/auvitekvar.h>
52 
53 static int	auvitek_i2c_acquire_bus(void *, int);
54 static void	auvitek_i2c_release_bus(void *, int);
55 static int	auvitek_i2c_exec(void *, i2c_op_t, i2c_addr_t,
56 				 const void *, size_t, void *, size_t, int);
57 
58 static int	auvitek_i2c_read(struct auvitek_softc *, i2c_addr_t,
59 				 uint8_t *, size_t);
60 static int	auvitek_i2c_write(struct auvitek_softc *, i2c_addr_t,
61 				  const uint8_t *, size_t);
62 static bool	auvitek_i2c_wait(struct auvitek_softc *);
63 static bool	auvitek_i2c_wait_rdack(struct auvitek_softc *);
64 static bool	auvitek_i2c_wait_rddone(struct auvitek_softc *);
65 static bool	auvitek_i2c_wait_wrdone(struct auvitek_softc *);
66 
67 int
68 auvitek_i2c_attach(struct auvitek_softc *sc)
69 {
70 	mutex_init(&sc->sc_i2c_lock, MUTEX_DEFAULT, IPL_VM);
71 	sc->sc_i2c.ic_cookie = sc;
72 	sc->sc_i2c.ic_acquire_bus = auvitek_i2c_acquire_bus;
73 	sc->sc_i2c.ic_release_bus = auvitek_i2c_release_bus;
74 	sc->sc_i2c.ic_exec = auvitek_i2c_exec;
75 
76 	return 0;
77 }
78 
79 int
80 auvitek_i2c_detach(struct auvitek_softc *sc, int flags)
81 {
82 	mutex_destroy(&sc->sc_i2c_lock);
83 
84 	return 0;
85 }
86 
87 static int
88 auvitek_i2c_acquire_bus(void *opaque, int flags)
89 {
90 	struct auvitek_softc *sc = opaque;
91 
92 	mutex_enter(&sc->sc_i2c_lock);
93 
94 	return 0;
95 }
96 
97 static void
98 auvitek_i2c_release_bus(void *opaque, int flags)
99 {
100 	struct auvitek_softc *sc = opaque;
101 
102 	mutex_exit(&sc->sc_i2c_lock);
103 }
104 
105 static int
106 auvitek_i2c_exec(void *opaque, i2c_op_t op, i2c_addr_t addr,
107     const void *cmd, size_t cmdlen, void *vbuf, size_t buflen, int flags)
108 {
109 	struct auvitek_softc *sc = opaque;
110 
111 	if (I2C_OP_READ_P(op))
112 		return auvitek_i2c_read(sc, addr, vbuf, buflen);
113 	else
114 		return auvitek_i2c_write(sc, addr, cmd, cmdlen);
115 }
116 
117 static int
118 auvitek_i2c_read(struct auvitek_softc *sc, i2c_addr_t addr,
119     uint8_t *buf, size_t buflen)
120 {
121 	uint8_t v;
122 	unsigned int i;
123 
124 	KASSERT(mutex_owned(&sc->sc_i2c_lock));
125 
126 	auvitek_write_1(sc, AU0828_REG_I2C_MBMODE, 1);
127 	auvitek_write_1(sc, AU0828_REG_I2C_CLKDIV, sc->sc_i2c_clkdiv);
128 	auvitek_write_1(sc, AU0828_REG_I2C_DSTADDR, addr << 1);
129 
130 	if (buflen == 0) {
131 		auvitek_write_1(sc, AU0828_REG_I2C_TRIGGER,
132 		    AU0828_I2C_TRIGGER_RD);
133 		if (auvitek_i2c_wait_rdack(sc) == false)
134 			return EBUSY;
135 		return 0;
136 	}
137 
138 	for (i = 0; i < buflen; i++) {
139 		v = AU0828_I2C_TRIGGER_RD;
140 		if (i < (buflen - 1))
141 			v |= AU0828_I2C_TRIGGER_HOLD;
142 		auvitek_write_1(sc, AU0828_REG_I2C_TRIGGER, v);
143 
144 		if (auvitek_i2c_wait_rddone(sc) == false)
145 			return EBUSY;
146 
147 		buf[i] = auvitek_read_1(sc, AU0828_REG_I2C_FIFORD);
148 	}
149 
150 	if (auvitek_i2c_wait(sc) == false)
151 		return EBUSY;
152 
153 	return 0;
154 }
155 
156 static int
157 auvitek_i2c_write(struct auvitek_softc *sc, i2c_addr_t addr,
158     const uint8_t *buf, size_t buflen)
159 {
160 	uint8_t v;
161 	unsigned int i, fifolen;
162 
163 	KASSERT(mutex_owned(&sc->sc_i2c_lock));
164 
165 	auvitek_write_1(sc, AU0828_REG_I2C_MBMODE, 1);
166 	auvitek_write_1(sc, AU0828_REG_I2C_CLKDIV, sc->sc_i2c_clkdiv);
167 	auvitek_write_1(sc, AU0828_REG_I2C_DSTADDR, addr << 1);
168 
169 	if (buflen == 0) {
170 		auvitek_write_1(sc, AU0828_REG_I2C_TRIGGER,
171 		    AU0828_I2C_TRIGGER_RD);
172 		if (auvitek_i2c_wait(sc) == false)
173 			return EBUSY;
174 		if (auvitek_i2c_wait_rdack(sc) == false)
175 			return EBUSY;
176 		return 0;
177 	}
178 
179 	fifolen = 0;
180 	for (i = 0; i < buflen; i++) {
181 		v = AU0828_I2C_TRIGGER_WR;
182 		if (i < (buflen - 1))
183 			v |= AU0828_I2C_TRIGGER_HOLD;
184 
185 		auvitek_write_1(sc, AU0828_REG_I2C_FIFOWR, buf[i]);
186 		++fifolen;
187 
188 		if (fifolen == 4 || i == (buflen - 1)) {
189 			auvitek_write_1(sc, AU0828_REG_I2C_TRIGGER, v);
190 			fifolen = 0;
191 
192 			if (auvitek_i2c_wait_wrdone(sc) == false)
193 				return EBUSY;
194 		}
195 	}
196 
197 	if (auvitek_i2c_wait(sc) == false)
198 		return EBUSY;
199 
200 	return 0;
201 }
202 
203 static bool
204 auvitek_i2c_wait(struct auvitek_softc *sc)
205 {
206 	uint8_t status;
207 	int retry = 1000;
208 
209 	while (--retry > 0) {
210 		status = auvitek_read_1(sc, AU0828_REG_I2C_STATUS);
211 		if (!(status & AU0828_I2C_STATUS_BUSY))
212 			break;
213 		delay(10);
214 	}
215 	if (retry == 0)
216 		return false;
217 
218 	return true;
219 }
220 
221 static bool
222 auvitek_i2c_wait_rdack(struct auvitek_softc *sc)
223 {
224 	uint8_t status;
225 	int retry = 1000;
226 
227 	while (--retry > 0) {
228 		status = auvitek_read_1(sc, AU0828_REG_I2C_STATUS);
229 		if (!(status & AU0828_I2C_STATUS_NO_RD_ACK))
230 			break;
231 		delay(10);
232 	}
233 	if (retry == 0)
234 		return false;
235 
236 	return true;
237 }
238 
239 static bool
240 auvitek_i2c_wait_rddone(struct auvitek_softc *sc)
241 {
242 	uint8_t status;
243 	int retry = 1000;
244 
245 	while (--retry > 0) {
246 		status = auvitek_read_1(sc, AU0828_REG_I2C_STATUS);
247 		if (status & AU0828_I2C_STATUS_RD_DONE)
248 			break;
249 		delay(10);
250 	}
251 	if (retry == 0)
252 		return false;
253 
254 	return true;
255 }
256 
257 static bool
258 auvitek_i2c_wait_wrdone(struct auvitek_softc *sc)
259 {
260 	uint8_t status;
261 	int retry = 1000;
262 
263 	while (--retry > 0) {
264 		status = auvitek_read_1(sc, AU0828_REG_I2C_STATUS);
265 		if (status & AU0828_I2C_STATUS_WR_DONE)
266 			break;
267 		delay(10);
268 	}
269 	if (retry == 0)
270 		return false;
271 
272 	return true;
273 }
274