2 cx231xx-i2c.c - driver for Conexant Cx23100/101/102 USB video capture devices
4 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
6 Based on Cx23885 driver
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/usb.h>
26 #include <linux/i2c.h>
27 #include <media/v4l2-common.h>
28 #include <media/tuner.h>
32 /* ----------------------------------------------------------- */
34 static unsigned int i2c_scan
;
35 module_param(i2c_scan
, int, 0444);
36 MODULE_PARM_DESC(i2c_scan
, "scan i2c bus at insmod time");
38 static unsigned int i2c_debug
;
39 module_param(i2c_debug
, int, 0644);
40 MODULE_PARM_DESC(i2c_debug
, "enable debug messages [i2c]");
42 #define dprintk1(lvl, fmt, args...) \
44 if (i2c_debug >= lvl) { \
45 printk(fmt, ##args); \
49 #define dprintk2(lvl, fmt, args...) \
51 if (i2c_debug >= lvl) { \
52 printk(KERN_DEBUG "%s at %s: " fmt, \
53 dev->name, __func__ , ##args); \
57 static inline bool is_tuner(struct cx231xx
*dev
, struct cx231xx_i2c
*bus
,
58 const struct i2c_msg
*msg
, int tuner_type
)
60 if (bus
->nr
!= dev
->board
.tuner_i2c_master
)
63 if (msg
->addr
!= dev
->board
.tuner_addr
)
66 if (dev
->tuner_type
!= tuner_type
)
73 * cx231xx_i2c_send_bytes()
75 static int cx231xx_i2c_send_bytes(struct i2c_adapter
*i2c_adap
,
76 const struct i2c_msg
*msg
)
78 struct cx231xx_i2c
*bus
= i2c_adap
->algo_data
;
79 struct cx231xx
*dev
= bus
->dev
;
80 struct cx231xx_i2c_xfer_data req_data
;
89 if (is_tuner(dev
, bus
, msg
, TUNER_XC5000
)) {
92 if (size
== 2) { /* register write sub addr */
93 /* Just writing sub address will cause problem
94 * to XC5000. So ignore the request */
96 } else if (size
== 4) { /* register write with sub addr */
98 saddr
= msg
->buf
[0] << 8 | msg
->buf
[1];
99 else if (msg
->len
== 1)
103 case 0x0000: /* start tuner calibration mode */
105 /* FW Loading is done */
106 dev
->xc_fw_load_done
= 1;
108 case 0x000D: /* Set signal source */
109 case 0x0001: /* Set TV standard - Video */
110 case 0x0002: /* Set TV standard - Audio */
111 case 0x0003: /* Set RF Frequency */
115 if (dev
->xc_fw_load_done
)
122 "GPIO WRITE: addr 0x%x, len %d, saddr 0x%x\n",
123 msg
->addr
, msg
->len
, saddr
);
125 return dev
->cx231xx_gpio_i2c_write(dev
,
132 /* special case for Xc5000 tuner case */
135 /* adjust the length to correct length */
137 buf_ptr
= (u8
*) (msg
->buf
+ 1);
140 /* prepare xfer_data struct */
141 req_data
.dev_addr
= msg
->addr
;
142 req_data
.direction
= msg
->flags
;
143 req_data
.saddr_len
= saddr_len
;
144 req_data
.saddr_dat
= msg
->buf
[0];
145 req_data
.buf_size
= size
> 16 ? 16 : size
;
146 req_data
.p_buffer
= (u8
*) (buf_ptr
+ loop
* 16);
148 bus
->i2c_nostop
= (size
> 16) ? 1 : 0;
149 bus
->i2c_reserve
= (loop
== 0) ? 0 : 1;
151 /* usb send command */
152 status
= dev
->cx231xx_send_usb_command(bus
, &req_data
);
163 bus
->i2c_reserve
= 0;
165 } else { /* regular case */
167 /* prepare xfer_data struct */
168 req_data
.dev_addr
= msg
->addr
;
169 req_data
.direction
= msg
->flags
;
170 req_data
.saddr_len
= 0;
171 req_data
.saddr_dat
= 0;
172 req_data
.buf_size
= msg
->len
;
173 req_data
.p_buffer
= msg
->buf
;
175 /* usb send command */
176 status
= dev
->cx231xx_send_usb_command(bus
, &req_data
);
179 return status
< 0 ? status
: 0;
183 * cx231xx_i2c_recv_bytes()
184 * read a byte from the i2c device
186 static int cx231xx_i2c_recv_bytes(struct i2c_adapter
*i2c_adap
,
187 const struct i2c_msg
*msg
)
189 struct cx231xx_i2c
*bus
= i2c_adap
->algo_data
;
190 struct cx231xx
*dev
= bus
->dev
;
191 struct cx231xx_i2c_xfer_data req_data
;
196 if (is_tuner(dev
, bus
, msg
, TUNER_XC5000
)) {
198 saddr
= msg
->buf
[0] << 8 | msg
->buf
[1];
199 else if (msg
->len
== 1)
202 if (dev
->xc_fw_load_done
) {
205 case 0x0009: /* BUSY check */
207 "GPIO R E A D: Special case BUSY check \n");
208 /*Try read BUSY register, just set it to zero*/
213 case 0x0004: /* read Lock status */
220 /* this is a special case to handle Xceive tuner
221 clock stretch issue with gpio based I2C */
224 "GPIO R E A D: addr 0x%x, len %d, saddr 0x%x\n",
226 msg
->buf
[0] << 8 | msg
->buf
[1]);
229 dev
->cx231xx_gpio_i2c_write(dev
, msg
->addr
,
233 dev
->cx231xx_gpio_i2c_read(dev
, msg
->addr
,
240 /* prepare xfer_data struct */
241 req_data
.dev_addr
= msg
->addr
;
242 req_data
.direction
= msg
->flags
;
243 req_data
.saddr_len
= msg
->len
;
244 req_data
.saddr_dat
= msg
->buf
[0] << 8 | msg
->buf
[1];
245 req_data
.buf_size
= msg
->len
;
246 req_data
.p_buffer
= msg
->buf
;
248 /* usb send command */
249 status
= dev
->cx231xx_send_usb_command(bus
, &req_data
);
253 /* prepare xfer_data struct */
254 req_data
.dev_addr
= msg
->addr
;
255 req_data
.direction
= msg
->flags
;
256 req_data
.saddr_len
= 0;
257 req_data
.saddr_dat
= 0;
258 req_data
.buf_size
= msg
->len
;
259 req_data
.p_buffer
= msg
->buf
;
261 /* usb send command */
262 status
= dev
->cx231xx_send_usb_command(bus
, &req_data
);
265 return status
< 0 ? status
: 0;
269 * cx231xx_i2c_recv_bytes_with_saddr()
270 * read a byte from the i2c device
272 static int cx231xx_i2c_recv_bytes_with_saddr(struct i2c_adapter
*i2c_adap
,
273 const struct i2c_msg
*msg1
,
274 const struct i2c_msg
*msg2
)
276 struct cx231xx_i2c
*bus
= i2c_adap
->algo_data
;
277 struct cx231xx
*dev
= bus
->dev
;
278 struct cx231xx_i2c_xfer_data req_data
;
284 saddr
= msg1
->buf
[0] << 8 | msg1
->buf
[1];
285 else if (msg1
->len
== 1)
286 saddr
= msg1
->buf
[0];
288 if (is_tuner(dev
, bus
, msg2
, TUNER_XC5000
)) {
289 if ((msg2
->len
< 16)) {
292 "i2c_read: addr 0x%x, len %d, saddr 0x%x, len %d\n",
293 msg2
->addr
, msg2
->len
, saddr
, msg1
->len
);
296 case 0x0008: /* read FW load status */
299 case 0x0004: /* read Lock status */
306 dev
->cx231xx_gpio_i2c_write(dev
, msg1
->addr
,
310 dev
->cx231xx_gpio_i2c_read(dev
, msg2
->addr
,
318 /* prepare xfer_data struct */
319 req_data
.dev_addr
= msg2
->addr
;
320 req_data
.direction
= msg2
->flags
;
321 req_data
.saddr_len
= msg1
->len
;
322 req_data
.saddr_dat
= saddr
;
323 req_data
.buf_size
= msg2
->len
;
324 req_data
.p_buffer
= msg2
->buf
;
326 /* usb send command */
327 status
= dev
->cx231xx_send_usb_command(bus
, &req_data
);
329 return status
< 0 ? status
: 0;
333 * cx231xx_i2c_check_for_device()
334 * check if there is a i2c_device at the supplied address
336 static int cx231xx_i2c_check_for_device(struct i2c_adapter
*i2c_adap
,
337 const struct i2c_msg
*msg
)
339 struct cx231xx_i2c
*bus
= i2c_adap
->algo_data
;
340 struct cx231xx
*dev
= bus
->dev
;
341 struct cx231xx_i2c_xfer_data req_data
;
344 /* prepare xfer_data struct */
345 req_data
.dev_addr
= msg
->addr
;
346 req_data
.direction
= msg
->flags
;
347 req_data
.saddr_len
= 0;
348 req_data
.saddr_dat
= 0;
349 req_data
.buf_size
= 0;
350 req_data
.p_buffer
= NULL
;
352 /* usb send command */
353 status
= dev
->cx231xx_send_usb_command(bus
, &req_data
);
355 return status
< 0 ? status
: 0;
360 * the main i2c transfer function
362 static int cx231xx_i2c_xfer(struct i2c_adapter
*i2c_adap
,
363 struct i2c_msg msgs
[], int num
)
365 struct cx231xx_i2c
*bus
= i2c_adap
->algo_data
;
366 struct cx231xx
*dev
= bus
->dev
;
367 int addr
, rc
, i
, byte
;
371 mutex_lock(&dev
->i2c_lock
);
372 for (i
= 0; i
< num
; i
++) {
376 dprintk2(2, "%s %s addr=0x%x len=%d:",
377 (msgs
[i
].flags
& I2C_M_RD
) ? "read" : "write",
378 i
== num
- 1 ? "stop" : "nonstop", addr
, msgs
[i
].len
);
380 /* no len: check only for device presence */
381 rc
= cx231xx_i2c_check_for_device(i2c_adap
, &msgs
[i
]);
383 dprintk2(2, " no device\n");
384 mutex_unlock(&dev
->i2c_lock
);
388 } else if (msgs
[i
].flags
& I2C_M_RD
) {
390 rc
= cx231xx_i2c_recv_bytes(i2c_adap
, &msgs
[i
]);
391 if (i2c_debug
>= 2) {
392 for (byte
= 0; byte
< msgs
[i
].len
; byte
++)
393 printk(KERN_CONT
" %02x", msgs
[i
].buf
[byte
]);
395 } else if (i
+ 1 < num
&& (msgs
[i
+ 1].flags
& I2C_M_RD
) &&
396 msgs
[i
].addr
== msgs
[i
+ 1].addr
397 && (msgs
[i
].len
<= 2) && (bus
->nr
< 3)) {
399 if (i2c_debug
>= 2) {
400 for (byte
= 0; byte
< msgs
[i
].len
; byte
++)
401 printk(KERN_CONT
" %02x", msgs
[i
].buf
[byte
]);
402 printk(KERN_CONT
"\n");
405 dprintk2(2, "plus %s %s addr=0x%x len=%d:",
406 (msgs
[i
+1].flags
& I2C_M_RD
) ? "read" : "write",
407 i
+1 == num
- 1 ? "stop" : "nonstop", addr
, msgs
[i
+1].len
);
408 rc
= cx231xx_i2c_recv_bytes_with_saddr(i2c_adap
,
411 if (i2c_debug
>= 2) {
412 for (byte
= 0; byte
< msgs
[i
+1].len
; byte
++)
413 printk(KERN_CONT
" %02x", msgs
[i
+1].buf
[byte
]);
418 if (i2c_debug
>= 2) {
419 for (byte
= 0; byte
< msgs
[i
].len
; byte
++)
420 printk(KERN_CONT
" %02x", msgs
[i
].buf
[byte
]);
422 rc
= cx231xx_i2c_send_bytes(i2c_adap
, &msgs
[i
]);
427 printk(KERN_CONT
"\n");
429 mutex_unlock(&dev
->i2c_lock
);
432 dprintk2(2, " ERROR: %i\n", rc
);
433 mutex_unlock(&dev
->i2c_lock
);
437 /* ----------------------------------------------------------- */
442 static u32
functionality(struct i2c_adapter
*adap
)
444 return I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_I2C
;
447 static struct i2c_algorithm cx231xx_algo
= {
448 .master_xfer
= cx231xx_i2c_xfer
,
449 .functionality
= functionality
,
452 static struct i2c_adapter cx231xx_adap_template
= {
453 .owner
= THIS_MODULE
,
455 .algo
= &cx231xx_algo
,
458 static struct i2c_client cx231xx_client_template
= {
459 .name
= "cx231xx internal",
462 /* ----------------------------------------------------------- */
466 * incomplete list of known devices
468 static char *i2c_devs
[128] = {
469 [0x60 >> 1] = "colibri",
470 [0x88 >> 1] = "hammerhead",
472 [0x32 >> 1] = "GeminiIII",
473 [0x02 >> 1] = "Aquarius",
474 [0xa0 >> 1] = "eeprom",
475 [0xc0 >> 1] = "tuner",
476 [0xc2 >> 1] = "tuner",
480 * cx231xx_do_i2c_scan()
481 * check i2c address range for devices
483 void cx231xx_do_i2c_scan(struct cx231xx
*dev
, struct i2c_client
*c
)
488 cx231xx_info(": Checking for I2C devices ..\n");
489 for (i
= 0; i
< 128; i
++) {
491 rc
= i2c_master_recv(c
, &buf
, 0);
494 cx231xx_info("%s: i2c scan: found device @ 0x%x [%s]\n",
496 i2c_devs
[i
] ? i2c_devs
[i
] : "???");
498 cx231xx_info(": Completed Checking for I2C devices.\n");
502 * cx231xx_i2c_register()
505 int cx231xx_i2c_register(struct cx231xx_i2c
*bus
)
507 struct cx231xx
*dev
= bus
->dev
;
509 BUG_ON(!dev
->cx231xx_send_usb_command
);
511 bus
->i2c_adap
= cx231xx_adap_template
;
512 bus
->i2c_client
= cx231xx_client_template
;
513 bus
->i2c_adap
.dev
.parent
= &dev
->udev
->dev
;
515 strlcpy(bus
->i2c_adap
.name
, bus
->dev
->name
, sizeof(bus
->i2c_adap
.name
));
517 bus
->i2c_adap
.algo_data
= bus
;
518 i2c_set_adapdata(&bus
->i2c_adap
, &dev
->v4l2_dev
);
519 i2c_add_adapter(&bus
->i2c_adap
);
521 bus
->i2c_client
.adapter
= &bus
->i2c_adap
;
523 if (0 == bus
->i2c_rc
) {
525 cx231xx_do_i2c_scan(dev
, &bus
->i2c_client
);
527 cx231xx_warn("%s: i2c bus %d register FAILED\n",
534 * cx231xx_i2c_unregister()
537 int cx231xx_i2c_unregister(struct cx231xx_i2c
*bus
)
539 i2c_del_adapter(&bus
->i2c_adap
);