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