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.
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/i2c.h>
27 #include <linux/i2c-mux.h>
28 #include <media/v4l2-common.h>
29 #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 int get_real_i2c_port(struct cx231xx
*dev
, int bus_nr
)
60 return dev
->port_3_switch_enabled
? I2C_1_MUX_3
: I2C_1_MUX_1
;
64 static inline bool is_tuner(struct cx231xx
*dev
, struct cx231xx_i2c
*bus
,
65 const struct i2c_msg
*msg
, int tuner_type
)
67 int i2c_port
= get_real_i2c_port(dev
, bus
->nr
);
69 if (i2c_port
!= dev
->board
.tuner_i2c_master
)
72 if (msg
->addr
!= dev
->board
.tuner_addr
)
75 if (dev
->tuner_type
!= tuner_type
)
82 * cx231xx_i2c_send_bytes()
84 static int cx231xx_i2c_send_bytes(struct i2c_adapter
*i2c_adap
,
85 const struct i2c_msg
*msg
)
87 struct cx231xx_i2c
*bus
= i2c_adap
->algo_data
;
88 struct cx231xx
*dev
= bus
->dev
;
89 struct cx231xx_i2c_xfer_data req_data
;
98 if (is_tuner(dev
, bus
, msg
, TUNER_XC5000
)) {
101 if (size
== 2) { /* register write sub addr */
102 /* Just writing sub address will cause problem
103 * to XC5000. So ignore the request */
105 } else if (size
== 4) { /* register write with sub addr */
107 saddr
= msg
->buf
[0] << 8 | msg
->buf
[1];
108 else if (msg
->len
== 1)
112 case 0x0000: /* start tuner calibration mode */
114 /* FW Loading is done */
115 dev
->xc_fw_load_done
= 1;
117 case 0x000D: /* Set signal source */
118 case 0x0001: /* Set TV standard - Video */
119 case 0x0002: /* Set TV standard - Audio */
120 case 0x0003: /* Set RF Frequency */
124 if (dev
->xc_fw_load_done
)
131 "GPIO WRITE: addr 0x%x, len %d, saddr 0x%x\n",
132 msg
->addr
, msg
->len
, saddr
);
134 return dev
->cx231xx_gpio_i2c_write(dev
,
141 /* special case for Xc5000 tuner case */
144 /* adjust the length to correct length */
146 buf_ptr
= (u8
*) (msg
->buf
+ 1);
149 /* prepare xfer_data struct */
150 req_data
.dev_addr
= msg
->addr
;
151 req_data
.direction
= msg
->flags
;
152 req_data
.saddr_len
= saddr_len
;
153 req_data
.saddr_dat
= msg
->buf
[0];
154 req_data
.buf_size
= size
> 16 ? 16 : size
;
155 req_data
.p_buffer
= (u8
*) (buf_ptr
+ loop
* 16);
157 bus
->i2c_nostop
= (size
> 16) ? 1 : 0;
158 bus
->i2c_reserve
= (loop
== 0) ? 0 : 1;
160 /* usb send command */
161 status
= dev
->cx231xx_send_usb_command(bus
, &req_data
);
172 bus
->i2c_reserve
= 0;
174 } else { /* regular case */
176 /* prepare xfer_data struct */
177 req_data
.dev_addr
= msg
->addr
;
178 req_data
.direction
= msg
->flags
;
179 req_data
.saddr_len
= 0;
180 req_data
.saddr_dat
= 0;
181 req_data
.buf_size
= msg
->len
;
182 req_data
.p_buffer
= msg
->buf
;
184 /* usb send command */
185 status
= dev
->cx231xx_send_usb_command(bus
, &req_data
);
188 return status
< 0 ? status
: 0;
192 * cx231xx_i2c_recv_bytes()
193 * read a byte from the i2c device
195 static int cx231xx_i2c_recv_bytes(struct i2c_adapter
*i2c_adap
,
196 const struct i2c_msg
*msg
)
198 struct cx231xx_i2c
*bus
= i2c_adap
->algo_data
;
199 struct cx231xx
*dev
= bus
->dev
;
200 struct cx231xx_i2c_xfer_data req_data
;
205 if (is_tuner(dev
, bus
, msg
, TUNER_XC5000
)) {
207 saddr
= msg
->buf
[0] << 8 | msg
->buf
[1];
208 else if (msg
->len
== 1)
211 if (dev
->xc_fw_load_done
) {
214 case 0x0009: /* BUSY check */
216 "GPIO R E A D: Special case BUSY check \n");
217 /*Try read BUSY register, just set it to zero*/
222 case 0x0004: /* read Lock status */
229 /* this is a special case to handle Xceive tuner
230 clock stretch issue with gpio based I2C */
233 "GPIO R E A D: addr 0x%x, len %d, saddr 0x%x\n",
235 msg
->buf
[0] << 8 | msg
->buf
[1]);
238 dev
->cx231xx_gpio_i2c_write(dev
, msg
->addr
,
242 dev
->cx231xx_gpio_i2c_read(dev
, msg
->addr
,
249 /* prepare xfer_data struct */
250 req_data
.dev_addr
= msg
->addr
;
251 req_data
.direction
= msg
->flags
;
252 req_data
.saddr_len
= msg
->len
;
253 req_data
.saddr_dat
= msg
->buf
[0] << 8 | msg
->buf
[1];
254 req_data
.buf_size
= msg
->len
;
255 req_data
.p_buffer
= msg
->buf
;
257 /* usb send command */
258 status
= dev
->cx231xx_send_usb_command(bus
, &req_data
);
262 /* prepare xfer_data struct */
263 req_data
.dev_addr
= msg
->addr
;
264 req_data
.direction
= msg
->flags
;
265 req_data
.saddr_len
= 0;
266 req_data
.saddr_dat
= 0;
267 req_data
.buf_size
= msg
->len
;
268 req_data
.p_buffer
= msg
->buf
;
270 /* usb send command */
271 status
= dev
->cx231xx_send_usb_command(bus
, &req_data
);
274 return status
< 0 ? status
: 0;
278 * cx231xx_i2c_recv_bytes_with_saddr()
279 * read a byte from the i2c device
281 static int cx231xx_i2c_recv_bytes_with_saddr(struct i2c_adapter
*i2c_adap
,
282 const struct i2c_msg
*msg1
,
283 const struct i2c_msg
*msg2
)
285 struct cx231xx_i2c
*bus
= i2c_adap
->algo_data
;
286 struct cx231xx
*dev
= bus
->dev
;
287 struct cx231xx_i2c_xfer_data req_data
;
293 saddr
= msg1
->buf
[0] << 8 | msg1
->buf
[1];
294 else if (msg1
->len
== 1)
295 saddr
= msg1
->buf
[0];
297 if (is_tuner(dev
, bus
, msg2
, TUNER_XC5000
)) {
298 if ((msg2
->len
< 16)) {
301 "i2c_read: addr 0x%x, len %d, saddr 0x%x, len %d\n",
302 msg2
->addr
, msg2
->len
, saddr
, msg1
->len
);
305 case 0x0008: /* read FW load status */
308 case 0x0004: /* read Lock status */
315 dev
->cx231xx_gpio_i2c_write(dev
, msg1
->addr
,
319 dev
->cx231xx_gpio_i2c_read(dev
, msg2
->addr
,
327 /* prepare xfer_data struct */
328 req_data
.dev_addr
= msg2
->addr
;
329 req_data
.direction
= msg2
->flags
;
330 req_data
.saddr_len
= msg1
->len
;
331 req_data
.saddr_dat
= saddr
;
332 req_data
.buf_size
= msg2
->len
;
333 req_data
.p_buffer
= msg2
->buf
;
335 /* usb send command */
336 status
= dev
->cx231xx_send_usb_command(bus
, &req_data
);
338 return status
< 0 ? status
: 0;
342 * cx231xx_i2c_check_for_device()
343 * check if there is a i2c_device at the supplied address
345 static int cx231xx_i2c_check_for_device(struct i2c_adapter
*i2c_adap
,
346 const struct i2c_msg
*msg
)
348 struct cx231xx_i2c
*bus
= i2c_adap
->algo_data
;
349 struct cx231xx
*dev
= bus
->dev
;
350 struct cx231xx_i2c_xfer_data req_data
;
354 /* prepare xfer_data struct */
355 req_data
.dev_addr
= msg
->addr
;
356 req_data
.direction
= I2C_M_RD
;
357 req_data
.saddr_len
= 0;
358 req_data
.saddr_dat
= 0;
359 req_data
.buf_size
= 1;
360 req_data
.p_buffer
= buf
;
362 /* usb send command */
363 status
= dev
->cx231xx_send_usb_command(bus
, &req_data
);
365 return status
< 0 ? status
: 0;
370 * the main i2c transfer function
372 static int cx231xx_i2c_xfer(struct i2c_adapter
*i2c_adap
,
373 struct i2c_msg msgs
[], int num
)
375 struct cx231xx_i2c
*bus
= i2c_adap
->algo_data
;
376 struct cx231xx
*dev
= bus
->dev
;
377 int addr
, rc
, i
, byte
;
379 mutex_lock(&dev
->i2c_lock
);
380 for (i
= 0; i
< num
; i
++) {
384 dprintk2(2, "%s %s addr=0x%x len=%d:",
385 (msgs
[i
].flags
& I2C_M_RD
) ? "read" : "write",
386 i
== num
- 1 ? "stop" : "nonstop", addr
, msgs
[i
].len
);
388 /* no len: check only for device presence */
389 rc
= cx231xx_i2c_check_for_device(i2c_adap
, &msgs
[i
]);
391 dprintk2(2, " no device\n");
392 mutex_unlock(&dev
->i2c_lock
);
396 } else if (msgs
[i
].flags
& I2C_M_RD
) {
398 rc
= cx231xx_i2c_recv_bytes(i2c_adap
, &msgs
[i
]);
399 if (i2c_debug
>= 2) {
400 for (byte
= 0; byte
< msgs
[i
].len
; byte
++)
401 printk(KERN_CONT
" %02x", msgs
[i
].buf
[byte
]);
403 } else if (i
+ 1 < num
&& (msgs
[i
+ 1].flags
& I2C_M_RD
) &&
404 msgs
[i
].addr
== msgs
[i
+ 1].addr
405 && (msgs
[i
].len
<= 2) && (bus
->nr
< 3)) {
407 if (i2c_debug
>= 2) {
408 for (byte
= 0; byte
< msgs
[i
].len
; byte
++)
409 printk(KERN_CONT
" %02x", msgs
[i
].buf
[byte
]);
410 printk(KERN_CONT
"\n");
413 dprintk2(2, "plus %s %s addr=0x%x len=%d:",
414 (msgs
[i
+1].flags
& I2C_M_RD
) ? "read" : "write",
415 i
+1 == num
- 1 ? "stop" : "nonstop", addr
, msgs
[i
+1].len
);
416 rc
= cx231xx_i2c_recv_bytes_with_saddr(i2c_adap
,
419 if (i2c_debug
>= 2) {
420 for (byte
= 0; byte
< msgs
[i
+1].len
; byte
++)
421 printk(KERN_CONT
" %02x", msgs
[i
+1].buf
[byte
]);
426 if (i2c_debug
>= 2) {
427 for (byte
= 0; byte
< msgs
[i
].len
; byte
++)
428 printk(KERN_CONT
" %02x", msgs
[i
].buf
[byte
]);
430 rc
= cx231xx_i2c_send_bytes(i2c_adap
, &msgs
[i
]);
435 printk(KERN_CONT
"\n");
437 mutex_unlock(&dev
->i2c_lock
);
440 dprintk2(2, " ERROR: %i\n", rc
);
441 mutex_unlock(&dev
->i2c_lock
);
445 /* ----------------------------------------------------------- */
450 static u32
functionality(struct i2c_adapter
*adap
)
452 return I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_I2C
;
455 static const struct i2c_algorithm cx231xx_algo
= {
456 .master_xfer
= cx231xx_i2c_xfer
,
457 .functionality
= functionality
,
460 static const struct i2c_adapter cx231xx_adap_template
= {
461 .owner
= THIS_MODULE
,
463 .algo
= &cx231xx_algo
,
466 /* ----------------------------------------------------------- */
470 * incomplete list of known devices
472 static const char *i2c_devs
[128] = {
473 [0x20 >> 1] = "demod",
474 [0x60 >> 1] = "colibri",
475 [0x88 >> 1] = "hammerhead",
477 [0x32 >> 1] = "GeminiIII",
478 [0x02 >> 1] = "Aquarius",
479 [0xa0 >> 1] = "eeprom",
480 [0xc0 >> 1] = "tuner",
481 [0xc2 >> 1] = "tuner",
485 * cx231xx_do_i2c_scan()
486 * check i2c address range for devices
488 void cx231xx_do_i2c_scan(struct cx231xx
*dev
, int i2c_port
)
492 struct i2c_adapter
*adap
;
493 struct i2c_msg msg
= {
502 /* Don't generate I2C errors during scan */
503 dev
->i2c_scan_running
= true;
504 adap
= cx231xx_get_i2c_adap(dev
, i2c_port
);
506 for (i
= 0; i
< 128; i
++) {
508 rc
= i2c_transfer(adap
, &msg
, 1);
513 "i2c scan: found device @ port %d addr 0x%x [%s]\n",
516 i2c_devs
[i
] ? i2c_devs
[i
] : "???");
519 dev
->i2c_scan_running
= false;
523 * cx231xx_i2c_register()
526 int cx231xx_i2c_register(struct cx231xx_i2c
*bus
)
528 struct cx231xx
*dev
= bus
->dev
;
530 BUG_ON(!dev
->cx231xx_send_usb_command
);
532 bus
->i2c_adap
= cx231xx_adap_template
;
533 bus
->i2c_adap
.dev
.parent
= dev
->dev
;
535 snprintf(bus
->i2c_adap
.name
, sizeof(bus
->i2c_adap
.name
), "%s-%d", bus
->dev
->name
, bus
->nr
);
537 bus
->i2c_adap
.algo_data
= bus
;
538 i2c_set_adapdata(&bus
->i2c_adap
, &dev
->v4l2_dev
);
539 bus
->i2c_rc
= i2c_add_adapter(&bus
->i2c_adap
);
541 if (0 != bus
->i2c_rc
)
543 "i2c bus %d register FAILED\n", bus
->nr
);
549 * cx231xx_i2c_unregister()
552 void cx231xx_i2c_unregister(struct cx231xx_i2c
*bus
)
555 i2c_del_adapter(&bus
->i2c_adap
);
559 * cx231xx_i2c_mux_select()
560 * switch i2c master number 1 between port1 and port3
562 static int cx231xx_i2c_mux_select(struct i2c_mux_core
*muxc
, u32 chan_id
)
564 struct cx231xx
*dev
= i2c_mux_priv(muxc
);
566 return cx231xx_enable_i2c_port_3(dev
, chan_id
);
569 int cx231xx_i2c_mux_create(struct cx231xx
*dev
)
571 dev
->muxc
= i2c_mux_alloc(&dev
->i2c_bus
[1].i2c_adap
, dev
->dev
, 2, 0, 0,
572 cx231xx_i2c_mux_select
, NULL
);
575 dev
->muxc
->priv
= dev
;
579 int cx231xx_i2c_mux_register(struct cx231xx
*dev
, int mux_no
)
581 return i2c_mux_add_adapter(dev
->muxc
,
583 mux_no
/* chan_id */,
587 void cx231xx_i2c_mux_unregister(struct cx231xx
*dev
)
589 i2c_mux_del_adapters(dev
->muxc
);
592 struct i2c_adapter
*cx231xx_get_i2c_adap(struct cx231xx
*dev
, int i2c_port
)
596 return &dev
->i2c_bus
[0].i2c_adap
;
598 return &dev
->i2c_bus
[1].i2c_adap
;
600 return &dev
->i2c_bus
[2].i2c_adap
;
602 return dev
->muxc
->adapter
[0];
604 return dev
->muxc
->adapter
[1];
609 EXPORT_SYMBOL_GPL(cx231xx_get_i2c_adap
);