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.
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
30 #include <media/v4l2-common.h>
33 module_param(i2c_scan
, int, 0444);
34 MODULE_PARM_DESC(i2c_scan
, "scan i2c bus at insmod time");
36 #define I2C_WAIT_DELAY 512
37 #define I2C_WAIT_RETRY 64
39 static inline int i2c_slave_did_write_ack(struct i2c_adapter
*i2c_adap
)
41 struct au0828_dev
*dev
= i2c_adap
->algo_data
;
42 return au0828_read(dev
, AU0828_I2C_STATUS_201
) &
43 AU0828_I2C_STATUS_NO_WRITE_ACK
? 0 : 1;
46 static inline int i2c_slave_did_read_ack(struct i2c_adapter
*i2c_adap
)
48 struct au0828_dev
*dev
= i2c_adap
->algo_data
;
49 return au0828_read(dev
, AU0828_I2C_STATUS_201
) &
50 AU0828_I2C_STATUS_NO_READ_ACK
? 0 : 1;
53 static int i2c_wait_read_ack(struct i2c_adapter
*i2c_adap
)
57 for (count
= 0; count
< I2C_WAIT_RETRY
; count
++) {
58 if (!i2c_slave_did_read_ack(i2c_adap
))
60 udelay(I2C_WAIT_DELAY
);
63 if (I2C_WAIT_RETRY
== count
)
69 static inline int i2c_is_read_busy(struct i2c_adapter
*i2c_adap
)
71 struct au0828_dev
*dev
= i2c_adap
->algo_data
;
72 return au0828_read(dev
, AU0828_I2C_STATUS_201
) &
73 AU0828_I2C_STATUS_READ_DONE
? 0 : 1;
76 static int i2c_wait_read_done(struct i2c_adapter
*i2c_adap
)
80 for (count
= 0; count
< I2C_WAIT_RETRY
; count
++) {
81 if (!i2c_is_read_busy(i2c_adap
))
83 udelay(I2C_WAIT_DELAY
);
86 if (I2C_WAIT_RETRY
== count
)
92 static inline int i2c_is_write_done(struct i2c_adapter
*i2c_adap
)
94 struct au0828_dev
*dev
= i2c_adap
->algo_data
;
95 return au0828_read(dev
, AU0828_I2C_STATUS_201
) &
96 AU0828_I2C_STATUS_WRITE_DONE
? 1 : 0;
99 static int i2c_wait_write_done(struct i2c_adapter
*i2c_adap
)
103 for (count
= 0; count
< I2C_WAIT_RETRY
; count
++) {
104 if (i2c_is_write_done(i2c_adap
))
106 udelay(I2C_WAIT_DELAY
);
109 if (I2C_WAIT_RETRY
== count
)
115 static inline int i2c_is_busy(struct i2c_adapter
*i2c_adap
)
117 struct au0828_dev
*dev
= i2c_adap
->algo_data
;
118 return au0828_read(dev
, AU0828_I2C_STATUS_201
) &
119 AU0828_I2C_STATUS_BUSY
? 1 : 0;
122 static int i2c_wait_done(struct i2c_adapter
*i2c_adap
)
126 for (count
= 0; count
< I2C_WAIT_RETRY
; count
++) {
127 if (!i2c_is_busy(i2c_adap
))
129 udelay(I2C_WAIT_DELAY
);
132 if (I2C_WAIT_RETRY
== count
)
138 /* FIXME: Implement join handling correctly */
139 static int i2c_sendbytes(struct i2c_adapter
*i2c_adap
,
140 const struct i2c_msg
*msg
, int joined_rlen
)
143 struct au0828_dev
*dev
= i2c_adap
->algo_data
;
145 dprintk(4, "%s()\n", __func__
);
147 au0828_write(dev
, AU0828_I2C_MULTIBYTE_MODE_2FF
, 0x01);
149 /* Set the I2C clock */
150 au0828_write(dev
, AU0828_I2C_CLK_DIVIDER_202
,
151 dev
->board
.i2c_clk_divider
);
153 /* Hardware needs 8 bit addresses */
154 au0828_write(dev
, AU0828_I2C_DEST_ADDR_203
, msg
->addr
<< 1);
156 dprintk(4, "SEND: %02x\n", msg
->addr
);
158 /* Deal with i2c_scan */
160 /* The analog tuner detection code makes use of the SMBUS_QUICK
161 message (which involves a zero length i2c write). To avoid
162 checking the status register when we didn't strobe out any
163 actual bytes to the bus, just do a read check. This is
164 consistent with how I saw i2c device checking done in the
165 USB trace of the Windows driver */
166 au0828_write(dev
, AU0828_I2C_TRIGGER_200
,
167 AU0828_I2C_TRIGGER_READ
);
169 if (!i2c_wait_done(i2c_adap
))
172 if (i2c_wait_read_ack(i2c_adap
))
178 for (i
= 0; i
< msg
->len
;) {
180 dprintk(4, " %02x\n", msg
->buf
[i
]);
182 au0828_write(dev
, AU0828_I2C_WRITE_FIFO_205
, msg
->buf
[i
]);
187 if ((strobe
>= 4) || (i
>= msg
->len
)) {
189 /* Strobe the byte into the bus */
191 au0828_write(dev
, AU0828_I2C_TRIGGER_200
,
192 AU0828_I2C_TRIGGER_WRITE
|
193 AU0828_I2C_TRIGGER_HOLD
);
195 au0828_write(dev
, AU0828_I2C_TRIGGER_200
,
196 AU0828_I2C_TRIGGER_WRITE
);
198 /* Reset strobe trigger */
201 if (!i2c_wait_write_done(i2c_adap
))
207 if (!i2c_wait_done(i2c_adap
))
215 /* FIXME: Implement join handling correctly */
216 static int i2c_readbytes(struct i2c_adapter
*i2c_adap
,
217 const struct i2c_msg
*msg
, int joined
)
219 struct au0828_dev
*dev
= i2c_adap
->algo_data
;
222 dprintk(4, "%s()\n", __func__
);
224 au0828_write(dev
, AU0828_I2C_MULTIBYTE_MODE_2FF
, 0x01);
226 /* Set the I2C clock */
227 au0828_write(dev
, AU0828_I2C_CLK_DIVIDER_202
,
228 dev
->board
.i2c_clk_divider
);
230 /* Hardware needs 8 bit addresses */
231 au0828_write(dev
, AU0828_I2C_DEST_ADDR_203
, msg
->addr
<< 1);
233 dprintk(4, " RECV:\n");
235 /* Deal with i2c_scan */
237 au0828_write(dev
, AU0828_I2C_TRIGGER_200
,
238 AU0828_I2C_TRIGGER_READ
);
240 if (i2c_wait_read_ack(i2c_adap
))
245 for (i
= 0; i
< msg
->len
;) {
250 au0828_write(dev
, AU0828_I2C_TRIGGER_200
,
251 AU0828_I2C_TRIGGER_READ
|
252 AU0828_I2C_TRIGGER_HOLD
);
254 au0828_write(dev
, AU0828_I2C_TRIGGER_200
,
255 AU0828_I2C_TRIGGER_READ
);
257 if (!i2c_wait_read_done(i2c_adap
))
260 msg
->buf
[i
-1] = au0828_read(dev
, AU0828_I2C_READ_FIFO_209
) &
263 dprintk(4, " %02x\n", msg
->buf
[i
-1]);
265 if (!i2c_wait_done(i2c_adap
))
273 static int i2c_xfer(struct i2c_adapter
*i2c_adap
,
274 struct i2c_msg
*msgs
, int num
)
278 dprintk(4, "%s(num = %d)\n", __func__
, num
);
280 for (i
= 0; i
< num
; i
++) {
281 dprintk(4, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
282 __func__
, num
, msgs
[i
].addr
, msgs
[i
].len
);
283 if (msgs
[i
].flags
& I2C_M_RD
) {
285 retval
= i2c_readbytes(i2c_adap
, &msgs
[i
], 0);
286 } else if (i
+ 1 < num
&& (msgs
[i
+ 1].flags
& I2C_M_RD
) &&
287 msgs
[i
].addr
== msgs
[i
+ 1].addr
) {
288 /* write then read from same address */
289 retval
= i2c_sendbytes(i2c_adap
, &msgs
[i
],
294 retval
= i2c_readbytes(i2c_adap
, &msgs
[i
], 1);
297 retval
= i2c_sendbytes(i2c_adap
, &msgs
[i
], 0);
308 static u32
au0828_functionality(struct i2c_adapter
*adap
)
310 return I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_I2C
;
313 static struct i2c_algorithm au0828_i2c_algo_template
= {
314 .master_xfer
= i2c_xfer
,
315 .functionality
= au0828_functionality
,
318 /* ----------------------------------------------------------------------- */
320 static struct i2c_adapter au0828_i2c_adap_template
= {
322 .owner
= THIS_MODULE
,
323 .algo
= &au0828_i2c_algo_template
,
326 static struct i2c_client au0828_i2c_client_template
= {
327 .name
= "au0828 internal",
330 static char *i2c_devs
[128] = {
331 [0x8e >> 1] = "au8522",
332 [0xa0 >> 1] = "eeprom",
333 [0xc2 >> 1] = "tuner/xc5000",
336 static void do_i2c_scan(char *name
, struct i2c_client
*c
)
341 for (i
= 0; i
< 128; i
++) {
343 rc
= i2c_master_recv(c
, &buf
, 0);
346 printk(KERN_INFO
"%s: i2c scan: found device @ 0x%x [%s]\n",
347 name
, i
<< 1, i2c_devs
[i
] ? i2c_devs
[i
] : "???");
351 /* init + register i2c algo-bit adapter */
352 int au0828_i2c_register(struct au0828_dev
*dev
)
354 dprintk(1, "%s()\n", __func__
);
356 memcpy(&dev
->i2c_adap
, &au0828_i2c_adap_template
,
357 sizeof(dev
->i2c_adap
));
358 memcpy(&dev
->i2c_algo
, &au0828_i2c_algo_template
,
359 sizeof(dev
->i2c_algo
));
360 memcpy(&dev
->i2c_client
, &au0828_i2c_client_template
,
361 sizeof(dev
->i2c_client
));
363 dev
->i2c_adap
.dev
.parent
= &dev
->usbdev
->dev
;
365 strlcpy(dev
->i2c_adap
.name
, DRIVER_NAME
,
366 sizeof(dev
->i2c_adap
.name
));
368 dev
->i2c_adap
.algo
= &dev
->i2c_algo
;
369 dev
->i2c_adap
.algo_data
= dev
;
370 i2c_set_adapdata(&dev
->i2c_adap
, &dev
->v4l2_dev
);
371 i2c_add_adapter(&dev
->i2c_adap
);
373 dev
->i2c_client
.adapter
= &dev
->i2c_adap
;
375 if (0 == dev
->i2c_rc
) {
376 printk(KERN_INFO
"%s: i2c bus registered\n", DRIVER_NAME
);
378 do_i2c_scan(DRIVER_NAME
, &dev
->i2c_client
);
380 printk(KERN_INFO
"%s: i2c bus register FAILED\n", DRIVER_NAME
);
385 int au0828_i2c_unregister(struct au0828_dev
*dev
)
387 i2c_del_adapter(&dev
->i2c_adap
);