2 * Driver for the Auvitek AU0828 USB bridge
4 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
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.
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
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
30 #include "media/tuner.h"
31 #include <media/v4l2-common.h>
34 module_param(i2c_scan
, int, 0444);
35 MODULE_PARM_DESC(i2c_scan
, "scan i2c bus at insmod time");
37 #define I2C_WAIT_DELAY 25
38 #define I2C_WAIT_RETRY 1000
40 static inline int i2c_slave_did_write_ack(struct i2c_adapter
*i2c_adap
)
42 struct au0828_dev
*dev
= i2c_adap
->algo_data
;
43 return au0828_read(dev
, AU0828_I2C_STATUS_201
) &
44 AU0828_I2C_STATUS_NO_WRITE_ACK
? 0 : 1;
47 static inline int i2c_slave_did_read_ack(struct i2c_adapter
*i2c_adap
)
49 struct au0828_dev
*dev
= i2c_adap
->algo_data
;
50 return au0828_read(dev
, AU0828_I2C_STATUS_201
) &
51 AU0828_I2C_STATUS_NO_READ_ACK
? 0 : 1;
54 static int i2c_wait_read_ack(struct i2c_adapter
*i2c_adap
)
58 for (count
= 0; count
< I2C_WAIT_RETRY
; count
++) {
59 if (!i2c_slave_did_read_ack(i2c_adap
))
61 udelay(I2C_WAIT_DELAY
);
64 if (I2C_WAIT_RETRY
== count
)
70 static inline int i2c_is_read_busy(struct i2c_adapter
*i2c_adap
)
72 struct au0828_dev
*dev
= i2c_adap
->algo_data
;
73 return au0828_read(dev
, AU0828_I2C_STATUS_201
) &
74 AU0828_I2C_STATUS_READ_DONE
? 0 : 1;
77 static int i2c_wait_read_done(struct i2c_adapter
*i2c_adap
)
81 for (count
= 0; count
< I2C_WAIT_RETRY
; count
++) {
82 if (!i2c_is_read_busy(i2c_adap
))
84 udelay(I2C_WAIT_DELAY
);
87 if (I2C_WAIT_RETRY
== count
)
93 static inline int i2c_is_write_done(struct i2c_adapter
*i2c_adap
)
95 struct au0828_dev
*dev
= i2c_adap
->algo_data
;
96 return au0828_read(dev
, AU0828_I2C_STATUS_201
) &
97 AU0828_I2C_STATUS_WRITE_DONE
? 1 : 0;
100 static int i2c_wait_write_done(struct i2c_adapter
*i2c_adap
)
104 for (count
= 0; count
< I2C_WAIT_RETRY
; count
++) {
105 if (i2c_is_write_done(i2c_adap
))
107 udelay(I2C_WAIT_DELAY
);
110 if (I2C_WAIT_RETRY
== count
)
116 static inline int i2c_is_busy(struct i2c_adapter
*i2c_adap
)
118 struct au0828_dev
*dev
= i2c_adap
->algo_data
;
119 return au0828_read(dev
, AU0828_I2C_STATUS_201
) &
120 AU0828_I2C_STATUS_BUSY
? 1 : 0;
123 static int i2c_wait_done(struct i2c_adapter
*i2c_adap
)
127 for (count
= 0; count
< I2C_WAIT_RETRY
; count
++) {
128 if (!i2c_is_busy(i2c_adap
))
130 udelay(I2C_WAIT_DELAY
);
133 if (I2C_WAIT_RETRY
== count
)
139 /* FIXME: Implement join handling correctly */
140 static int i2c_sendbytes(struct i2c_adapter
*i2c_adap
,
141 const struct i2c_msg
*msg
, int joined_rlen
)
144 struct au0828_dev
*dev
= i2c_adap
->algo_data
;
145 u8 i2c_speed
= dev
->board
.i2c_clk_divider
;
147 dprintk(4, "%s()\n", __func__
);
149 au0828_write(dev
, AU0828_I2C_MULTIBYTE_MODE_2FF
, 0x01);
151 if (((dev
->board
.tuner_type
== TUNER_XC5000
) ||
152 (dev
->board
.tuner_type
== TUNER_XC5000C
)) &&
153 (dev
->board
.tuner_addr
== msg
->addr
)) {
155 * Due to I2C clock stretch, we need to use a lower speed
156 * on xc5000 for commands. However, firmware transfer can
157 * speed up to 400 KHz.
160 i2c_speed
= AU0828_I2C_CLK_250KHZ
;
162 i2c_speed
= AU0828_I2C_CLK_20KHZ
;
164 /* Set the I2C clock */
165 au0828_write(dev
, AU0828_I2C_CLK_DIVIDER_202
, i2c_speed
);
167 /* Hardware needs 8 bit addresses */
168 au0828_write(dev
, AU0828_I2C_DEST_ADDR_203
, msg
->addr
<< 1);
170 dprintk(4, "SEND: %02x\n", msg
->addr
);
172 /* Deal with i2c_scan */
174 /* The analog tuner detection code makes use of the SMBUS_QUICK
175 message (which involves a zero length i2c write). To avoid
176 checking the status register when we didn't strobe out any
177 actual bytes to the bus, just do a read check. This is
178 consistent with how I saw i2c device checking done in the
179 USB trace of the Windows driver */
180 au0828_write(dev
, AU0828_I2C_TRIGGER_200
,
181 AU0828_I2C_TRIGGER_READ
);
183 if (!i2c_wait_done(i2c_adap
))
186 if (i2c_wait_read_ack(i2c_adap
))
192 for (i
= 0; i
< msg
->len
;) {
194 dprintk(4, " %02x\n", msg
->buf
[i
]);
196 au0828_write(dev
, AU0828_I2C_WRITE_FIFO_205
, msg
->buf
[i
]);
201 if ((strobe
>= 4) || (i
>= msg
->len
)) {
203 /* Strobe the byte into the bus */
205 au0828_write(dev
, AU0828_I2C_TRIGGER_200
,
206 AU0828_I2C_TRIGGER_WRITE
|
207 AU0828_I2C_TRIGGER_HOLD
);
209 au0828_write(dev
, AU0828_I2C_TRIGGER_200
,
210 AU0828_I2C_TRIGGER_WRITE
);
212 /* Reset strobe trigger */
215 if (!i2c_wait_write_done(i2c_adap
))
221 if (!i2c_wait_done(i2c_adap
))
229 /* FIXME: Implement join handling correctly */
230 static int i2c_readbytes(struct i2c_adapter
*i2c_adap
,
231 const struct i2c_msg
*msg
, int joined
)
233 struct au0828_dev
*dev
= i2c_adap
->algo_data
;
234 u8 i2c_speed
= dev
->board
.i2c_clk_divider
;
237 dprintk(4, "%s()\n", __func__
);
239 au0828_write(dev
, AU0828_I2C_MULTIBYTE_MODE_2FF
, 0x01);
242 * Due to xc5000c clock stretch, we cannot use full speed at
243 * readings from xc5000, as otherwise they'll fail.
245 if (((dev
->board
.tuner_type
== TUNER_XC5000
) ||
246 (dev
->board
.tuner_type
== TUNER_XC5000C
)) &&
247 (dev
->board
.tuner_addr
== msg
->addr
))
248 i2c_speed
= AU0828_I2C_CLK_20KHZ
;
250 /* Set the I2C clock */
251 au0828_write(dev
, AU0828_I2C_CLK_DIVIDER_202
, i2c_speed
);
253 /* Hardware needs 8 bit addresses */
254 au0828_write(dev
, AU0828_I2C_DEST_ADDR_203
, msg
->addr
<< 1);
256 dprintk(4, " RECV:\n");
258 /* Deal with i2c_scan */
260 au0828_write(dev
, AU0828_I2C_TRIGGER_200
,
261 AU0828_I2C_TRIGGER_READ
);
263 if (i2c_wait_read_ack(i2c_adap
))
268 for (i
= 0; i
< msg
->len
;) {
273 au0828_write(dev
, AU0828_I2C_TRIGGER_200
,
274 AU0828_I2C_TRIGGER_READ
|
275 AU0828_I2C_TRIGGER_HOLD
);
277 au0828_write(dev
, AU0828_I2C_TRIGGER_200
,
278 AU0828_I2C_TRIGGER_READ
);
280 if (!i2c_wait_read_done(i2c_adap
))
283 msg
->buf
[i
-1] = au0828_read(dev
, AU0828_I2C_READ_FIFO_209
) &
286 dprintk(4, " %02x\n", msg
->buf
[i
-1]);
288 if (!i2c_wait_done(i2c_adap
))
296 static int i2c_xfer(struct i2c_adapter
*i2c_adap
,
297 struct i2c_msg
*msgs
, int num
)
301 dprintk(4, "%s(num = %d)\n", __func__
, num
);
303 for (i
= 0; i
< num
; i
++) {
304 dprintk(4, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
305 __func__
, num
, msgs
[i
].addr
, msgs
[i
].len
);
306 if (msgs
[i
].flags
& I2C_M_RD
) {
308 retval
= i2c_readbytes(i2c_adap
, &msgs
[i
], 0);
309 } else if (i
+ 1 < num
&& (msgs
[i
+ 1].flags
& I2C_M_RD
) &&
310 msgs
[i
].addr
== msgs
[i
+ 1].addr
) {
311 /* write then read from same address */
312 retval
= i2c_sendbytes(i2c_adap
, &msgs
[i
],
317 retval
= i2c_readbytes(i2c_adap
, &msgs
[i
], 1);
320 retval
= i2c_sendbytes(i2c_adap
, &msgs
[i
], 0);
331 static u32
au0828_functionality(struct i2c_adapter
*adap
)
333 return I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_I2C
;
336 static struct i2c_algorithm au0828_i2c_algo_template
= {
337 .master_xfer
= i2c_xfer
,
338 .functionality
= au0828_functionality
,
341 /* ----------------------------------------------------------------------- */
343 static struct i2c_adapter au0828_i2c_adap_template
= {
344 .name
= KBUILD_MODNAME
,
345 .owner
= THIS_MODULE
,
346 .algo
= &au0828_i2c_algo_template
,
349 static struct i2c_client au0828_i2c_client_template
= {
350 .name
= "au0828 internal",
353 static char *i2c_devs
[128] = {
354 [0x8e >> 1] = "au8522",
355 [0xa0 >> 1] = "eeprom",
356 [0xc2 >> 1] = "tuner/xc5000",
359 static void do_i2c_scan(char *name
, struct i2c_client
*c
)
364 for (i
= 0; i
< 128; i
++) {
366 rc
= i2c_master_recv(c
, &buf
, 0);
369 pr_info("%s: i2c scan: found device @ 0x%x [%s]\n",
370 name
, i
<< 1, i2c_devs
[i
] ? i2c_devs
[i
] : "???");
374 /* init + register i2c adapter */
375 int au0828_i2c_register(struct au0828_dev
*dev
)
377 dprintk(1, "%s()\n", __func__
);
379 dev
->i2c_adap
= au0828_i2c_adap_template
;
380 dev
->i2c_algo
= au0828_i2c_algo_template
;
381 dev
->i2c_client
= au0828_i2c_client_template
;
383 dev
->i2c_adap
.dev
.parent
= &dev
->usbdev
->dev
;
385 strlcpy(dev
->i2c_adap
.name
, KBUILD_MODNAME
,
386 sizeof(dev
->i2c_adap
.name
));
388 dev
->i2c_adap
.algo
= &dev
->i2c_algo
;
389 dev
->i2c_adap
.algo_data
= dev
;
390 #ifdef CONFIG_VIDEO_AU0828_V4L2
391 i2c_set_adapdata(&dev
->i2c_adap
, &dev
->v4l2_dev
);
393 i2c_set_adapdata(&dev
->i2c_adap
, dev
);
395 i2c_add_adapter(&dev
->i2c_adap
);
397 dev
->i2c_client
.adapter
= &dev
->i2c_adap
;
399 if (0 == dev
->i2c_rc
) {
400 pr_info("i2c bus registered\n");
402 do_i2c_scan(KBUILD_MODNAME
, &dev
->i2c_client
);
404 pr_info("i2c bus register FAILED\n");
409 int au0828_i2c_unregister(struct au0828_dev
*dev
)
411 i2c_del_adapter(&dev
->i2c_adap
);