auvitek_i2c.c revision 1.1.6.2 1 1.1.6.2 rmind /* $NetBSD: auvitek_i2c.c,v 1.1.6.2 2011/03/05 20:54:10 rmind Exp $ */
2 1.1.6.2 rmind
3 1.1.6.2 rmind /*-
4 1.1.6.2 rmind * Copyright (c) 2010 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1.6.2 rmind * All rights reserved.
6 1.1.6.2 rmind *
7 1.1.6.2 rmind * Redistribution and use in source and binary forms, with or without
8 1.1.6.2 rmind * modification, are permitted provided that the following conditions
9 1.1.6.2 rmind * are met:
10 1.1.6.2 rmind * 1. Redistributions of source code must retain the above copyright
11 1.1.6.2 rmind * notice, this list of conditions and the following disclaimer.
12 1.1.6.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.6.2 rmind * notice, this list of conditions and the following disclaimer in the
14 1.1.6.2 rmind * documentation and/or other materials provided with the distribution.
15 1.1.6.2 rmind *
16 1.1.6.2 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1.6.2 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.6.2 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.6.2 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1.6.2 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1.6.2 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1.6.2 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1.6.2 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1.6.2 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1.6.2 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1.6.2 rmind * POSSIBILITY OF SUCH DAMAGE.
27 1.1.6.2 rmind */
28 1.1.6.2 rmind
29 1.1.6.2 rmind /*
30 1.1.6.2 rmind * Auvitek AU0828 USB controller - I2C access ops
31 1.1.6.2 rmind */
32 1.1.6.2 rmind
33 1.1.6.2 rmind #include <sys/cdefs.h>
34 1.1.6.2 rmind __KERNEL_RCSID(0, "$NetBSD: auvitek_i2c.c,v 1.1.6.2 2011/03/05 20:54:10 rmind Exp $");
35 1.1.6.2 rmind
36 1.1.6.2 rmind #include <sys/param.h>
37 1.1.6.2 rmind #include <sys/systm.h>
38 1.1.6.2 rmind #include <sys/device.h>
39 1.1.6.2 rmind #include <sys/conf.h>
40 1.1.6.2 rmind #include <sys/bus.h>
41 1.1.6.2 rmind #include <sys/module.h>
42 1.1.6.2 rmind
43 1.1.6.2 rmind #include <dev/usb/usb.h>
44 1.1.6.2 rmind #include <dev/usb/usbdi.h>
45 1.1.6.2 rmind #include <dev/usb/usbdi_util.h>
46 1.1.6.2 rmind #include <dev/usb/usbdevs.h>
47 1.1.6.2 rmind
48 1.1.6.2 rmind #include <dev/i2c/i2cvar.h>
49 1.1.6.2 rmind
50 1.1.6.2 rmind #include <dev/usb/auvitekreg.h>
51 1.1.6.2 rmind #include <dev/usb/auvitekvar.h>
52 1.1.6.2 rmind
53 1.1.6.2 rmind static int auvitek_i2c_acquire_bus(void *, int);
54 1.1.6.2 rmind static void auvitek_i2c_release_bus(void *, int);
55 1.1.6.2 rmind static int auvitek_i2c_exec(void *, i2c_op_t, i2c_addr_t,
56 1.1.6.2 rmind const void *, size_t, void *, size_t, int);
57 1.1.6.2 rmind
58 1.1.6.2 rmind static int auvitek_i2c_read(struct auvitek_softc *, i2c_addr_t,
59 1.1.6.2 rmind uint8_t *, size_t);
60 1.1.6.2 rmind static int auvitek_i2c_write(struct auvitek_softc *, i2c_addr_t,
61 1.1.6.2 rmind const uint8_t *, size_t);
62 1.1.6.2 rmind static bool auvitek_i2c_wait(struct auvitek_softc *);
63 1.1.6.2 rmind static bool auvitek_i2c_wait_rdack(struct auvitek_softc *);
64 1.1.6.2 rmind static bool auvitek_i2c_wait_rddone(struct auvitek_softc *);
65 1.1.6.2 rmind static bool auvitek_i2c_wait_wrdone(struct auvitek_softc *);
66 1.1.6.2 rmind
67 1.1.6.2 rmind int
68 1.1.6.2 rmind auvitek_i2c_attach(struct auvitek_softc *sc)
69 1.1.6.2 rmind {
70 1.1.6.2 rmind mutex_init(&sc->sc_i2c_lock, MUTEX_DEFAULT, IPL_VM);
71 1.1.6.2 rmind sc->sc_i2c.ic_cookie = sc;
72 1.1.6.2 rmind sc->sc_i2c.ic_acquire_bus = auvitek_i2c_acquire_bus;
73 1.1.6.2 rmind sc->sc_i2c.ic_release_bus = auvitek_i2c_release_bus;
74 1.1.6.2 rmind sc->sc_i2c.ic_exec = auvitek_i2c_exec;
75 1.1.6.2 rmind
76 1.1.6.2 rmind return 0;
77 1.1.6.2 rmind }
78 1.1.6.2 rmind
79 1.1.6.2 rmind int
80 1.1.6.2 rmind auvitek_i2c_detach(struct auvitek_softc *sc, int flags)
81 1.1.6.2 rmind {
82 1.1.6.2 rmind mutex_destroy(&sc->sc_i2c_lock);
83 1.1.6.2 rmind
84 1.1.6.2 rmind return 0;
85 1.1.6.2 rmind }
86 1.1.6.2 rmind
87 1.1.6.2 rmind static int
88 1.1.6.2 rmind auvitek_i2c_acquire_bus(void *opaque, int flags)
89 1.1.6.2 rmind {
90 1.1.6.2 rmind struct auvitek_softc *sc = opaque;
91 1.1.6.2 rmind
92 1.1.6.2 rmind mutex_enter(&sc->sc_i2c_lock);
93 1.1.6.2 rmind
94 1.1.6.2 rmind return 0;
95 1.1.6.2 rmind }
96 1.1.6.2 rmind
97 1.1.6.2 rmind static void
98 1.1.6.2 rmind auvitek_i2c_release_bus(void *opaque, int flags)
99 1.1.6.2 rmind {
100 1.1.6.2 rmind struct auvitek_softc *sc = opaque;
101 1.1.6.2 rmind
102 1.1.6.2 rmind mutex_exit(&sc->sc_i2c_lock);
103 1.1.6.2 rmind }
104 1.1.6.2 rmind
105 1.1.6.2 rmind static int
106 1.1.6.2 rmind auvitek_i2c_exec(void *opaque, i2c_op_t op, i2c_addr_t addr,
107 1.1.6.2 rmind const void *cmd, size_t cmdlen, void *vbuf, size_t buflen, int flags)
108 1.1.6.2 rmind {
109 1.1.6.2 rmind struct auvitek_softc *sc = opaque;
110 1.1.6.2 rmind
111 1.1.6.2 rmind if (I2C_OP_READ_P(op))
112 1.1.6.2 rmind return auvitek_i2c_read(sc, addr, vbuf, buflen);
113 1.1.6.2 rmind else
114 1.1.6.2 rmind return auvitek_i2c_write(sc, addr, cmd, cmdlen);
115 1.1.6.2 rmind }
116 1.1.6.2 rmind
117 1.1.6.2 rmind static int
118 1.1.6.2 rmind auvitek_i2c_read(struct auvitek_softc *sc, i2c_addr_t addr,
119 1.1.6.2 rmind uint8_t *buf, size_t buflen)
120 1.1.6.2 rmind {
121 1.1.6.2 rmind uint8_t v;
122 1.1.6.2 rmind unsigned int i;
123 1.1.6.2 rmind
124 1.1.6.2 rmind KASSERT(mutex_owned(&sc->sc_i2c_lock));
125 1.1.6.2 rmind
126 1.1.6.2 rmind auvitek_write_1(sc, AU0828_REG_I2C_MBMODE, 1);
127 1.1.6.2 rmind auvitek_write_1(sc, AU0828_REG_I2C_CLKDIV, sc->sc_i2c_clkdiv);
128 1.1.6.2 rmind auvitek_write_1(sc, AU0828_REG_I2C_DSTADDR, addr << 1);
129 1.1.6.2 rmind
130 1.1.6.2 rmind if (buflen == 0) {
131 1.1.6.2 rmind auvitek_write_1(sc, AU0828_REG_I2C_TRIGGER,
132 1.1.6.2 rmind AU0828_I2C_TRIGGER_RD);
133 1.1.6.2 rmind if (auvitek_i2c_wait_rdack(sc) == false)
134 1.1.6.2 rmind return EBUSY;
135 1.1.6.2 rmind return 0;
136 1.1.6.2 rmind }
137 1.1.6.2 rmind
138 1.1.6.2 rmind for (i = 0; i < buflen; i++) {
139 1.1.6.2 rmind v = AU0828_I2C_TRIGGER_RD;
140 1.1.6.2 rmind if (i < (buflen - 1))
141 1.1.6.2 rmind v |= AU0828_I2C_TRIGGER_HOLD;
142 1.1.6.2 rmind auvitek_write_1(sc, AU0828_REG_I2C_TRIGGER, v);
143 1.1.6.2 rmind
144 1.1.6.2 rmind if (auvitek_i2c_wait_rddone(sc) == false)
145 1.1.6.2 rmind return EBUSY;
146 1.1.6.2 rmind
147 1.1.6.2 rmind buf[i] = auvitek_read_1(sc, AU0828_REG_I2C_FIFORD);
148 1.1.6.2 rmind }
149 1.1.6.2 rmind
150 1.1.6.2 rmind if (auvitek_i2c_wait(sc) == false)
151 1.1.6.2 rmind return EBUSY;
152 1.1.6.2 rmind
153 1.1.6.2 rmind return 0;
154 1.1.6.2 rmind }
155 1.1.6.2 rmind
156 1.1.6.2 rmind static int
157 1.1.6.2 rmind auvitek_i2c_write(struct auvitek_softc *sc, i2c_addr_t addr,
158 1.1.6.2 rmind const uint8_t *buf, size_t buflen)
159 1.1.6.2 rmind {
160 1.1.6.2 rmind uint8_t v;
161 1.1.6.2 rmind unsigned int i, fifolen;
162 1.1.6.2 rmind
163 1.1.6.2 rmind KASSERT(mutex_owned(&sc->sc_i2c_lock));
164 1.1.6.2 rmind
165 1.1.6.2 rmind auvitek_write_1(sc, AU0828_REG_I2C_MBMODE, 1);
166 1.1.6.2 rmind auvitek_write_1(sc, AU0828_REG_I2C_CLKDIV, sc->sc_i2c_clkdiv);
167 1.1.6.2 rmind auvitek_write_1(sc, AU0828_REG_I2C_DSTADDR, addr << 1);
168 1.1.6.2 rmind
169 1.1.6.2 rmind if (buflen == 0) {
170 1.1.6.2 rmind auvitek_write_1(sc, AU0828_REG_I2C_TRIGGER,
171 1.1.6.2 rmind AU0828_I2C_TRIGGER_RD);
172 1.1.6.2 rmind if (auvitek_i2c_wait(sc) == false)
173 1.1.6.2 rmind return EBUSY;
174 1.1.6.2 rmind if (auvitek_i2c_wait_rdack(sc) == false)
175 1.1.6.2 rmind return EBUSY;
176 1.1.6.2 rmind return 0;
177 1.1.6.2 rmind }
178 1.1.6.2 rmind
179 1.1.6.2 rmind fifolen = 0;
180 1.1.6.2 rmind for (i = 0; i < buflen; i++) {
181 1.1.6.2 rmind v = AU0828_I2C_TRIGGER_WR;
182 1.1.6.2 rmind if (i < (buflen - 1))
183 1.1.6.2 rmind v |= AU0828_I2C_TRIGGER_HOLD;
184 1.1.6.2 rmind
185 1.1.6.2 rmind auvitek_write_1(sc, AU0828_REG_I2C_FIFOWR, buf[i]);
186 1.1.6.2 rmind ++fifolen;
187 1.1.6.2 rmind
188 1.1.6.2 rmind if (fifolen == 4 || i == (buflen - 1)) {
189 1.1.6.2 rmind auvitek_write_1(sc, AU0828_REG_I2C_TRIGGER, v);
190 1.1.6.2 rmind fifolen = 0;
191 1.1.6.2 rmind
192 1.1.6.2 rmind if (auvitek_i2c_wait_wrdone(sc) == false)
193 1.1.6.2 rmind return EBUSY;
194 1.1.6.2 rmind }
195 1.1.6.2 rmind }
196 1.1.6.2 rmind
197 1.1.6.2 rmind if (auvitek_i2c_wait(sc) == false)
198 1.1.6.2 rmind return EBUSY;
199 1.1.6.2 rmind
200 1.1.6.2 rmind return 0;
201 1.1.6.2 rmind }
202 1.1.6.2 rmind
203 1.1.6.2 rmind static bool
204 1.1.6.2 rmind auvitek_i2c_wait(struct auvitek_softc *sc)
205 1.1.6.2 rmind {
206 1.1.6.2 rmind uint8_t status;
207 1.1.6.2 rmind int retry = 1000;
208 1.1.6.2 rmind
209 1.1.6.2 rmind while (--retry > 0) {
210 1.1.6.2 rmind status = auvitek_read_1(sc, AU0828_REG_I2C_STATUS);
211 1.1.6.2 rmind if (!(status & AU0828_I2C_STATUS_BUSY))
212 1.1.6.2 rmind break;
213 1.1.6.2 rmind delay(10);
214 1.1.6.2 rmind }
215 1.1.6.2 rmind if (retry == 0)
216 1.1.6.2 rmind return false;
217 1.1.6.2 rmind
218 1.1.6.2 rmind return true;
219 1.1.6.2 rmind }
220 1.1.6.2 rmind
221 1.1.6.2 rmind static bool
222 1.1.6.2 rmind auvitek_i2c_wait_rdack(struct auvitek_softc *sc)
223 1.1.6.2 rmind {
224 1.1.6.2 rmind uint8_t status;
225 1.1.6.2 rmind int retry = 1000;
226 1.1.6.2 rmind
227 1.1.6.2 rmind while (--retry > 0) {
228 1.1.6.2 rmind status = auvitek_read_1(sc, AU0828_REG_I2C_STATUS);
229 1.1.6.2 rmind if (!(status & AU0828_I2C_STATUS_NO_RD_ACK))
230 1.1.6.2 rmind break;
231 1.1.6.2 rmind delay(10);
232 1.1.6.2 rmind }
233 1.1.6.2 rmind if (retry == 0)
234 1.1.6.2 rmind return false;
235 1.1.6.2 rmind
236 1.1.6.2 rmind return true;
237 1.1.6.2 rmind }
238 1.1.6.2 rmind
239 1.1.6.2 rmind static bool
240 1.1.6.2 rmind auvitek_i2c_wait_rddone(struct auvitek_softc *sc)
241 1.1.6.2 rmind {
242 1.1.6.2 rmind uint8_t status;
243 1.1.6.2 rmind int retry = 1000;
244 1.1.6.2 rmind
245 1.1.6.2 rmind while (--retry > 0) {
246 1.1.6.2 rmind status = auvitek_read_1(sc, AU0828_REG_I2C_STATUS);
247 1.1.6.2 rmind if (status & AU0828_I2C_STATUS_RD_DONE)
248 1.1.6.2 rmind break;
249 1.1.6.2 rmind delay(10);
250 1.1.6.2 rmind }
251 1.1.6.2 rmind if (retry == 0)
252 1.1.6.2 rmind return false;
253 1.1.6.2 rmind
254 1.1.6.2 rmind return true;
255 1.1.6.2 rmind }
256 1.1.6.2 rmind
257 1.1.6.2 rmind static bool
258 1.1.6.2 rmind auvitek_i2c_wait_wrdone(struct auvitek_softc *sc)
259 1.1.6.2 rmind {
260 1.1.6.2 rmind uint8_t status;
261 1.1.6.2 rmind int retry = 1000;
262 1.1.6.2 rmind
263 1.1.6.2 rmind while (--retry > 0) {
264 1.1.6.2 rmind status = auvitek_read_1(sc, AU0828_REG_I2C_STATUS);
265 1.1.6.2 rmind if (status & AU0828_I2C_STATUS_WR_DONE)
266 1.1.6.2 rmind break;
267 1.1.6.2 rmind delay(10);
268 1.1.6.2 rmind }
269 1.1.6.2 rmind if (retry == 0)
270 1.1.6.2 rmind return false;
271 1.1.6.2 rmind
272 1.1.6.2 rmind return true;
273 1.1.6.2 rmind }
274