]>
Commit | Line | Data |
---|---|---|
265a6510 ST |
1 | /* |
2 | * Driver for the Auvitek AU0828 USB bridge | |
3 | * | |
6d897616 | 4 | * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org> |
265a6510 ST |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * | |
15 | * GNU General Public License for more details. | |
265a6510 ST |
16 | */ |
17 | ||
83afb32a MCC |
18 | #include "au0828.h" |
19 | ||
265a6510 ST |
20 | #include <linux/module.h> |
21 | #include <linux/moduleparam.h> | |
22 | #include <linux/init.h> | |
23 | #include <linux/delay.h> | |
18d73c58 | 24 | #include <linux/io.h> |
265a6510 | 25 | |
faaf01b2 | 26 | #include "media/tuner.h" |
265a6510 ST |
27 | #include <media/v4l2-common.h> |
28 | ||
b33d24c4 | 29 | static int i2c_scan; |
265a6510 ST |
30 | module_param(i2c_scan, int, 0444); |
31 | MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time"); | |
32 | ||
baede40c DH |
33 | #define I2C_WAIT_DELAY 25 |
34 | #define I2C_WAIT_RETRY 1000 | |
265a6510 ST |
35 | |
36 | static inline int i2c_slave_did_write_ack(struct i2c_adapter *i2c_adap) | |
37 | { | |
38 | struct au0828_dev *dev = i2c_adap->algo_data; | |
9beb0de9 DH |
39 | return au0828_read(dev, AU0828_I2C_STATUS_201) & |
40 | AU0828_I2C_STATUS_NO_WRITE_ACK ? 0 : 1; | |
265a6510 ST |
41 | } |
42 | ||
43 | static inline int i2c_slave_did_read_ack(struct i2c_adapter *i2c_adap) | |
44 | { | |
45 | struct au0828_dev *dev = i2c_adap->algo_data; | |
9beb0de9 DH |
46 | return au0828_read(dev, AU0828_I2C_STATUS_201) & |
47 | AU0828_I2C_STATUS_NO_READ_ACK ? 0 : 1; | |
265a6510 ST |
48 | } |
49 | ||
50 | static int i2c_wait_read_ack(struct i2c_adapter *i2c_adap) | |
51 | { | |
52 | int count; | |
53 | ||
54 | for (count = 0; count < I2C_WAIT_RETRY; count++) { | |
55 | if (!i2c_slave_did_read_ack(i2c_adap)) | |
56 | break; | |
57 | udelay(I2C_WAIT_DELAY); | |
58 | } | |
59 | ||
60 | if (I2C_WAIT_RETRY == count) | |
61 | return 0; | |
62 | ||
63 | return 1; | |
64 | } | |
65 | ||
66 | static inline int i2c_is_read_busy(struct i2c_adapter *i2c_adap) | |
67 | { | |
68 | struct au0828_dev *dev = i2c_adap->algo_data; | |
9beb0de9 DH |
69 | return au0828_read(dev, AU0828_I2C_STATUS_201) & |
70 | AU0828_I2C_STATUS_READ_DONE ? 0 : 1; | |
265a6510 ST |
71 | } |
72 | ||
73 | static int i2c_wait_read_done(struct i2c_adapter *i2c_adap) | |
74 | { | |
75 | int count; | |
76 | ||
77 | for (count = 0; count < I2C_WAIT_RETRY; count++) { | |
78 | if (!i2c_is_read_busy(i2c_adap)) | |
79 | break; | |
80 | udelay(I2C_WAIT_DELAY); | |
81 | } | |
82 | ||
83 | if (I2C_WAIT_RETRY == count) | |
84 | return 0; | |
85 | ||
86 | return 1; | |
87 | } | |
88 | ||
89 | static inline int i2c_is_write_done(struct i2c_adapter *i2c_adap) | |
90 | { | |
91 | struct au0828_dev *dev = i2c_adap->algo_data; | |
9beb0de9 DH |
92 | return au0828_read(dev, AU0828_I2C_STATUS_201) & |
93 | AU0828_I2C_STATUS_WRITE_DONE ? 1 : 0; | |
265a6510 ST |
94 | } |
95 | ||
96 | static int i2c_wait_write_done(struct i2c_adapter *i2c_adap) | |
97 | { | |
98 | int count; | |
99 | ||
100 | for (count = 0; count < I2C_WAIT_RETRY; count++) { | |
101 | if (i2c_is_write_done(i2c_adap)) | |
102 | break; | |
103 | udelay(I2C_WAIT_DELAY); | |
104 | } | |
105 | ||
106 | if (I2C_WAIT_RETRY == count) | |
107 | return 0; | |
108 | ||
109 | return 1; | |
110 | } | |
111 | ||
112 | static inline int i2c_is_busy(struct i2c_adapter *i2c_adap) | |
113 | { | |
114 | struct au0828_dev *dev = i2c_adap->algo_data; | |
9beb0de9 DH |
115 | return au0828_read(dev, AU0828_I2C_STATUS_201) & |
116 | AU0828_I2C_STATUS_BUSY ? 1 : 0; | |
265a6510 ST |
117 | } |
118 | ||
119 | static int i2c_wait_done(struct i2c_adapter *i2c_adap) | |
120 | { | |
121 | int count; | |
122 | ||
123 | for (count = 0; count < I2C_WAIT_RETRY; count++) { | |
124 | if (!i2c_is_busy(i2c_adap)) | |
125 | break; | |
126 | udelay(I2C_WAIT_DELAY); | |
127 | } | |
128 | ||
129 | if (I2C_WAIT_RETRY == count) | |
130 | return 0; | |
131 | ||
132 | return 1; | |
133 | } | |
134 | ||
135 | /* FIXME: Implement join handling correctly */ | |
136 | static int i2c_sendbytes(struct i2c_adapter *i2c_adap, | |
137 | const struct i2c_msg *msg, int joined_rlen) | |
138 | { | |
139 | int i, strobe = 0; | |
140 | struct au0828_dev *dev = i2c_adap->algo_data; | |
cfd0c77d | 141 | u8 i2c_speed = dev->board.i2c_clk_divider; |
265a6510 | 142 | |
f07e8e4b | 143 | dprintk(4, "%s()\n", __func__); |
265a6510 | 144 | |
9beb0de9 | 145 | au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01); |
32c000ad | 146 | |
ad03d300 DH |
147 | if (((dev->board.tuner_type == TUNER_XC5000) || |
148 | (dev->board.tuner_type == TUNER_XC5000C)) && | |
cfd0c77d MCC |
149 | (dev->board.tuner_addr == msg->addr)) { |
150 | /* | |
151 | * Due to I2C clock stretch, we need to use a lower speed | |
152 | * on xc5000 for commands. However, firmware transfer can | |
153 | * speed up to 400 KHz. | |
154 | */ | |
155 | if (msg->len == 64) | |
156 | i2c_speed = AU0828_I2C_CLK_250KHZ; | |
157 | else | |
158 | i2c_speed = AU0828_I2C_CLK_20KHZ; | |
faaf01b2 | 159 | } |
cfd0c77d MCC |
160 | /* Set the I2C clock */ |
161 | au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202, i2c_speed); | |
265a6510 ST |
162 | |
163 | /* Hardware needs 8 bit addresses */ | |
9beb0de9 | 164 | au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1); |
265a6510 | 165 | |
bc3c613c | 166 | dprintk(4, "SEND: %02x\n", msg->addr); |
265a6510 | 167 | |
dc8685b5 DH |
168 | /* Deal with i2c_scan */ |
169 | if (msg->len == 0) { | |
170 | /* The analog tuner detection code makes use of the SMBUS_QUICK | |
171 | message (which involves a zero length i2c write). To avoid | |
172 | checking the status register when we didn't strobe out any | |
173 | actual bytes to the bus, just do a read check. This is | |
174 | consistent with how I saw i2c device checking done in the | |
175 | USB trace of the Windows driver */ | |
9beb0de9 DH |
176 | au0828_write(dev, AU0828_I2C_TRIGGER_200, |
177 | AU0828_I2C_TRIGGER_READ); | |
178 | ||
dc8685b5 DH |
179 | if (!i2c_wait_done(i2c_adap)) |
180 | return -EIO; | |
181 | ||
182 | if (i2c_wait_read_ack(i2c_adap)) | |
183 | return -EIO; | |
184 | ||
185 | return 0; | |
186 | } | |
187 | ||
a9c36aad | 188 | for (i = 0; i < msg->len;) { |
265a6510 | 189 | |
bc3c613c | 190 | dprintk(4, " %02x\n", msg->buf[i]); |
265a6510 | 191 | |
9beb0de9 | 192 | au0828_write(dev, AU0828_I2C_WRITE_FIFO_205, msg->buf[i]); |
265a6510 ST |
193 | |
194 | strobe++; | |
195 | i++; | |
196 | ||
197 | if ((strobe >= 4) || (i >= msg->len)) { | |
198 | ||
199 | /* Strobe the byte into the bus */ | |
200 | if (i < msg->len) | |
9beb0de9 DH |
201 | au0828_write(dev, AU0828_I2C_TRIGGER_200, |
202 | AU0828_I2C_TRIGGER_WRITE | | |
203 | AU0828_I2C_TRIGGER_HOLD); | |
265a6510 | 204 | else |
9beb0de9 DH |
205 | au0828_write(dev, AU0828_I2C_TRIGGER_200, |
206 | AU0828_I2C_TRIGGER_WRITE); | |
265a6510 ST |
207 | |
208 | /* Reset strobe trigger */ | |
209 | strobe = 0; | |
210 | ||
211 | if (!i2c_wait_write_done(i2c_adap)) | |
212 | return -EIO; | |
213 | ||
214 | } | |
215 | ||
216 | } | |
217 | if (!i2c_wait_done(i2c_adap)) | |
218 | return -EIO; | |
219 | ||
bc3c613c | 220 | dprintk(4, "\n"); |
265a6510 ST |
221 | |
222 | return msg->len; | |
223 | } | |
224 | ||
225 | /* FIXME: Implement join handling correctly */ | |
226 | static int i2c_readbytes(struct i2c_adapter *i2c_adap, | |
227 | const struct i2c_msg *msg, int joined) | |
228 | { | |
229 | struct au0828_dev *dev = i2c_adap->algo_data; | |
cfd0c77d | 230 | u8 i2c_speed = dev->board.i2c_clk_divider; |
265a6510 ST |
231 | int i; |
232 | ||
f07e8e4b | 233 | dprintk(4, "%s()\n", __func__); |
265a6510 | 234 | |
9beb0de9 | 235 | au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01); |
32c000ad | 236 | |
cfd0c77d MCC |
237 | /* |
238 | * Due to xc5000c clock stretch, we cannot use full speed at | |
239 | * readings from xc5000, as otherwise they'll fail. | |
240 | */ | |
241 | if (((dev->board.tuner_type == TUNER_XC5000) || | |
242 | (dev->board.tuner_type == TUNER_XC5000C)) && | |
243 | (dev->board.tuner_addr == msg->addr)) | |
244 | i2c_speed = AU0828_I2C_CLK_20KHZ; | |
245 | ||
16af6f5a | 246 | /* Set the I2C clock */ |
cfd0c77d | 247 | au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202, i2c_speed); |
265a6510 ST |
248 | |
249 | /* Hardware needs 8 bit addresses */ | |
9beb0de9 | 250 | au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1); |
265a6510 | 251 | |
bc3c613c | 252 | dprintk(4, " RECV:\n"); |
265a6510 ST |
253 | |
254 | /* Deal with i2c_scan */ | |
255 | if (msg->len == 0) { | |
9beb0de9 DH |
256 | au0828_write(dev, AU0828_I2C_TRIGGER_200, |
257 | AU0828_I2C_TRIGGER_READ); | |
258 | ||
265a6510 ST |
259 | if (i2c_wait_read_ack(i2c_adap)) |
260 | return -EIO; | |
261 | return 0; | |
262 | } | |
263 | ||
a9c36aad | 264 | for (i = 0; i < msg->len;) { |
265a6510 ST |
265 | |
266 | i++; | |
267 | ||
268 | if (i < msg->len) | |
9beb0de9 DH |
269 | au0828_write(dev, AU0828_I2C_TRIGGER_200, |
270 | AU0828_I2C_TRIGGER_READ | | |
271 | AU0828_I2C_TRIGGER_HOLD); | |
265a6510 | 272 | else |
9beb0de9 DH |
273 | au0828_write(dev, AU0828_I2C_TRIGGER_200, |
274 | AU0828_I2C_TRIGGER_READ); | |
265a6510 ST |
275 | |
276 | if (!i2c_wait_read_done(i2c_adap)) | |
277 | return -EIO; | |
278 | ||
9beb0de9 DH |
279 | msg->buf[i-1] = au0828_read(dev, AU0828_I2C_READ_FIFO_209) & |
280 | 0xff; | |
265a6510 | 281 | |
bc3c613c | 282 | dprintk(4, " %02x\n", msg->buf[i-1]); |
265a6510 ST |
283 | } |
284 | if (!i2c_wait_done(i2c_adap)) | |
285 | return -EIO; | |
286 | ||
bc3c613c | 287 | dprintk(4, "\n"); |
265a6510 ST |
288 | |
289 | return msg->len; | |
290 | } | |
291 | ||
292 | static int i2c_xfer(struct i2c_adapter *i2c_adap, | |
293 | struct i2c_msg *msgs, int num) | |
294 | { | |
295 | int i, retval = 0; | |
296 | ||
f07e8e4b | 297 | dprintk(4, "%s(num = %d)\n", __func__, num); |
265a6510 | 298 | |
a9c36aad | 299 | for (i = 0; i < num; i++) { |
bc3c613c | 300 | dprintk(4, "%s(num = %d) addr = 0x%02x len = 0x%x\n", |
f07e8e4b | 301 | __func__, num, msgs[i].addr, msgs[i].len); |
265a6510 ST |
302 | if (msgs[i].flags & I2C_M_RD) { |
303 | /* read */ | |
304 | retval = i2c_readbytes(i2c_adap, &msgs[i], 0); | |
305 | } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) && | |
306 | msgs[i].addr == msgs[i + 1].addr) { | |
307 | /* write then read from same address */ | |
308 | retval = i2c_sendbytes(i2c_adap, &msgs[i], | |
309 | msgs[i + 1].len); | |
310 | if (retval < 0) | |
311 | goto err; | |
312 | i++; | |
313 | retval = i2c_readbytes(i2c_adap, &msgs[i], 1); | |
314 | } else { | |
315 | /* write */ | |
316 | retval = i2c_sendbytes(i2c_adap, &msgs[i], 0); | |
317 | } | |
318 | if (retval < 0) | |
319 | goto err; | |
320 | } | |
321 | return num; | |
322 | ||
323 | err: | |
324 | return retval; | |
325 | } | |
326 | ||
265a6510 ST |
327 | static u32 au0828_functionality(struct i2c_adapter *adap) |
328 | { | |
329 | return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C; | |
330 | } | |
331 | ||
332 | static struct i2c_algorithm au0828_i2c_algo_template = { | |
333 | .master_xfer = i2c_xfer, | |
334 | .functionality = au0828_functionality, | |
335 | }; | |
336 | ||
337 | /* ----------------------------------------------------------------------- */ | |
338 | ||
339 | static struct i2c_adapter au0828_i2c_adap_template = { | |
83afb32a | 340 | .name = KBUILD_MODNAME, |
265a6510 | 341 | .owner = THIS_MODULE, |
265a6510 | 342 | .algo = &au0828_i2c_algo_template, |
265a6510 ST |
343 | }; |
344 | ||
345 | static struct i2c_client au0828_i2c_client_template = { | |
346 | .name = "au0828 internal", | |
347 | }; | |
348 | ||
349 | static char *i2c_devs[128] = { | |
18d73c58 MCC |
350 | [0x8e >> 1] = "au8522", |
351 | [0xa0 >> 1] = "eeprom", | |
352 | [0xc2 >> 1] = "tuner/xc5000", | |
265a6510 ST |
353 | }; |
354 | ||
355 | static void do_i2c_scan(char *name, struct i2c_client *c) | |
356 | { | |
357 | unsigned char buf; | |
358 | int i, rc; | |
359 | ||
360 | for (i = 0; i < 128; i++) { | |
361 | c->addr = i; | |
362 | rc = i2c_master_recv(c, &buf, 0); | |
363 | if (rc < 0) | |
364 | continue; | |
83afb32a | 365 | pr_info("%s: i2c scan: found device @ 0x%x [%s]\n", |
265a6510 ST |
366 | name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???"); |
367 | } | |
368 | } | |
369 | ||
a824f0f4 | 370 | /* init + register i2c adapter */ |
265a6510 ST |
371 | int au0828_i2c_register(struct au0828_dev *dev) |
372 | { | |
f07e8e4b | 373 | dprintk(1, "%s()\n", __func__); |
265a6510 | 374 | |
f01e0ffd EG |
375 | dev->i2c_adap = au0828_i2c_adap_template; |
376 | dev->i2c_algo = au0828_i2c_algo_template; | |
377 | dev->i2c_client = au0828_i2c_client_template; | |
265a6510 ST |
378 | |
379 | dev->i2c_adap.dev.parent = &dev->usbdev->dev; | |
380 | ||
83afb32a | 381 | strlcpy(dev->i2c_adap.name, KBUILD_MODNAME, |
265a6510 ST |
382 | sizeof(dev->i2c_adap.name)); |
383 | ||
b14667f3 | 384 | dev->i2c_adap.algo = &dev->i2c_algo; |
265a6510 | 385 | dev->i2c_adap.algo_data = dev; |
8a4e7866 | 386 | #ifdef CONFIG_VIDEO_AU0828_V4L2 |
b14667f3 | 387 | i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev); |
8a4e7866 MK |
388 | #else |
389 | i2c_set_adapdata(&dev->i2c_adap, dev); | |
390 | #endif | |
265a6510 ST |
391 | i2c_add_adapter(&dev->i2c_adap); |
392 | ||
393 | dev->i2c_client.adapter = &dev->i2c_adap; | |
394 | ||
395 | if (0 == dev->i2c_rc) { | |
83afb32a | 396 | pr_info("i2c bus registered\n"); |
265a6510 | 397 | if (i2c_scan) |
83afb32a | 398 | do_i2c_scan(KBUILD_MODNAME, &dev->i2c_client); |
265a6510 | 399 | } else |
83afb32a | 400 | pr_info("i2c bus register FAILED\n"); |
bc3c613c | 401 | |
265a6510 ST |
402 | return dev->i2c_rc; |
403 | } | |
404 | ||
405 | int au0828_i2c_unregister(struct au0828_dev *dev) | |
406 | { | |
407 | i2c_del_adapter(&dev->i2c_adap); | |
408 | return 0; | |
409 | } | |
410 |