1 // SPDX-License-Identifier: GPL-2.0-or-later
3 cx231xx-i2c.c - driver for Conexant Cx23100/101/102 USB video capture devices
5 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
7 Based on Cx23885 driver
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/i2c.h>
15 #include <linux/i2c-mux.h>
16 #include <media/v4l2-common.h>
17 #include <media/tuner.h>
20 /* ----------------------------------------------------------- */
22 static unsigned int i2c_scan
;
23 module_param(i2c_scan
, int, 0444);
24 MODULE_PARM_DESC(i2c_scan
, "scan i2c bus at insmod time");
26 static unsigned int i2c_debug
;
27 module_param(i2c_debug
, int, 0644);
28 MODULE_PARM_DESC(i2c_debug
, "enable debug messages [i2c]");
30 #define dprintk1(lvl, fmt, args...) \
32 if (i2c_debug >= lvl) { \
33 printk(fmt, ##args); \
37 #define dprintk2(lvl, fmt, args...) \
39 if (i2c_debug >= lvl) { \
40 printk(KERN_DEBUG "%s at %s: " fmt, \
41 dev->name, __func__ , ##args); \
45 static inline int get_real_i2c_port(struct cx231xx
*dev
, int bus_nr
)
48 return dev
->port_3_switch_enabled
? I2C_1_MUX_3
: I2C_1_MUX_1
;
52 static inline bool is_tuner(struct cx231xx
*dev
, struct cx231xx_i2c
*bus
,
53 const struct i2c_msg
*msg
, int tuner_type
)
55 int i2c_port
= get_real_i2c_port(dev
, bus
->nr
);
57 if (i2c_port
!= dev
->board
.tuner_i2c_master
)
60 if (msg
->addr
!= dev
->board
.tuner_addr
)
63 if (dev
->tuner_type
!= tuner_type
)
70 * cx231xx_i2c_send_bytes()
72 static int cx231xx_i2c_send_bytes(struct i2c_adapter
*i2c_adap
,
73 const struct i2c_msg
*msg
)
75 struct cx231xx_i2c
*bus
= i2c_adap
->algo_data
;
76 struct cx231xx
*dev
= bus
->dev
;
77 struct cx231xx_i2c_xfer_data req_data
;
86 if (is_tuner(dev
, bus
, msg
, TUNER_XC5000
)) {
89 if (size
== 2) { /* register write sub addr */
90 /* Just writing sub address will cause problem
91 * to XC5000. So ignore the request */
93 } else if (size
== 4) { /* register write with sub addr */
95 saddr
= msg
->buf
[0] << 8 | msg
->buf
[1];
96 else if (msg
->len
== 1)
100 case 0x0000: /* start tuner calibration mode */
102 /* FW Loading is done */
103 dev
->xc_fw_load_done
= 1;
105 case 0x000D: /* Set signal source */
106 case 0x0001: /* Set TV standard - Video */
107 case 0x0002: /* Set TV standard - Audio */
108 case 0x0003: /* Set RF Frequency */
112 if (dev
->xc_fw_load_done
)
119 "GPIO WRITE: addr 0x%x, len %d, saddr 0x%x\n",
120 msg
->addr
, msg
->len
, saddr
);
122 return dev
->cx231xx_gpio_i2c_write(dev
,
129 /* special case for Xc5000 tuner case */
132 /* adjust the length to correct length */
134 buf_ptr
= (u8
*) (msg
->buf
+ 1);
137 /* prepare xfer_data struct */
138 req_data
.dev_addr
= msg
->addr
;
139 req_data
.direction
= msg
->flags
;
140 req_data
.saddr_len
= saddr_len
;
141 req_data
.saddr_dat
= msg
->buf
[0];
142 req_data
.buf_size
= size
> 16 ? 16 : size
;
143 req_data
.p_buffer
= (u8
*) (buf_ptr
+ loop
* 16);
145 bus
->i2c_nostop
= (size
> 16) ? 1 : 0;
146 bus
->i2c_reserve
= (loop
== 0) ? 0 : 1;
148 /* usb send command */
149 status
= dev
->cx231xx_send_usb_command(bus
, &req_data
);
160 bus
->i2c_reserve
= 0;
162 } else { /* regular case */
164 /* prepare xfer_data struct */
165 req_data
.dev_addr
= msg
->addr
;
166 req_data
.direction
= msg
->flags
;
167 req_data
.saddr_len
= 0;
168 req_data
.saddr_dat
= 0;
169 req_data
.buf_size
= msg
->len
;
170 req_data
.p_buffer
= msg
->buf
;
172 /* usb send command */
173 status
= dev
->cx231xx_send_usb_command(bus
, &req_data
);
176 return status
< 0 ? status
: 0;
180 * cx231xx_i2c_recv_bytes()
181 * read a byte from the i2c device
183 static int cx231xx_i2c_recv_bytes(struct i2c_adapter
*i2c_adap
,
184 const struct i2c_msg
*msg
)
186 struct cx231xx_i2c
*bus
= i2c_adap
->algo_data
;
187 struct cx231xx
*dev
= bus
->dev
;
188 struct cx231xx_i2c_xfer_data req_data
;
193 if (is_tuner(dev
, bus
, msg
, TUNER_XC5000
)) {
195 saddr
= msg
->buf
[0] << 8 | msg
->buf
[1];
196 else if (msg
->len
== 1)
199 if (dev
->xc_fw_load_done
) {
202 case 0x0009: /* BUSY check */
204 "GPIO R E A D: Special case BUSY check \n");
205 /*Try read BUSY register, just set it to zero*/
210 case 0x0004: /* read Lock status */
217 /* this is a special case to handle Xceive tuner
218 clock stretch issue with gpio based I2C */
221 "GPIO R E A D: addr 0x%x, len %d, saddr 0x%x\n",
223 msg
->buf
[0] << 8 | msg
->buf
[1]);
226 dev
->cx231xx_gpio_i2c_write(dev
, msg
->addr
,
230 dev
->cx231xx_gpio_i2c_read(dev
, msg
->addr
,
237 /* prepare xfer_data struct */
238 req_data
.dev_addr
= msg
->addr
;
239 req_data
.direction
= msg
->flags
;
240 req_data
.saddr_len
= msg
->len
;
241 req_data
.saddr_dat
= msg
->buf
[0] << 8 | msg
->buf
[1];
242 req_data
.buf_size
= msg
->len
;
243 req_data
.p_buffer
= msg
->buf
;
245 /* usb send command */
246 status
= dev
->cx231xx_send_usb_command(bus
, &req_data
);
250 /* prepare xfer_data struct */
251 req_data
.dev_addr
= msg
->addr
;
252 req_data
.direction
= msg
->flags
;
253 req_data
.saddr_len
= 0;
254 req_data
.saddr_dat
= 0;
255 req_data
.buf_size
= msg
->len
;
256 req_data
.p_buffer
= msg
->buf
;
258 /* usb send command */
259 status
= dev
->cx231xx_send_usb_command(bus
, &req_data
);
262 return status
< 0 ? status
: 0;
266 * cx231xx_i2c_recv_bytes_with_saddr()
267 * read a byte from the i2c device
269 static int cx231xx_i2c_recv_bytes_with_saddr(struct i2c_adapter
*i2c_adap
,
270 const struct i2c_msg
*msg1
,
271 const struct i2c_msg
*msg2
)
273 struct cx231xx_i2c
*bus
= i2c_adap
->algo_data
;
274 struct cx231xx
*dev
= bus
->dev
;
275 struct cx231xx_i2c_xfer_data req_data
;
281 saddr
= msg1
->buf
[0] << 8 | msg1
->buf
[1];
282 else if (msg1
->len
== 1)
283 saddr
= msg1
->buf
[0];
285 if (is_tuner(dev
, bus
, msg2
, TUNER_XC5000
)) {
286 if ((msg2
->len
< 16)) {
289 "i2c_read: addr 0x%x, len %d, saddr 0x%x, len %d\n",
290 msg2
->addr
, msg2
->len
, saddr
, msg1
->len
);
293 case 0x0008: /* read FW load status */
296 case 0x0004: /* read Lock status */
303 dev
->cx231xx_gpio_i2c_write(dev
, msg1
->addr
,
307 dev
->cx231xx_gpio_i2c_read(dev
, msg2
->addr
,
315 /* prepare xfer_data struct */
316 req_data
.dev_addr
= msg2
->addr
;
317 req_data
.direction
= msg2
->flags
;
318 req_data
.saddr_len
= msg1
->len
;
319 req_data
.saddr_dat
= saddr
;
320 req_data
.buf_size
= msg2
->len
;
321 req_data
.p_buffer
= msg2
->buf
;
323 /* usb send command */
324 status
= dev
->cx231xx_send_usb_command(bus
, &req_data
);
326 return status
< 0 ? status
: 0;
330 * cx231xx_i2c_check_for_device()
331 * check if there is a i2c_device at the supplied address
333 static int cx231xx_i2c_check_for_device(struct i2c_adapter
*i2c_adap
,
334 const struct i2c_msg
*msg
)
336 struct cx231xx_i2c
*bus
= i2c_adap
->algo_data
;
337 struct cx231xx
*dev
= bus
->dev
;
338 struct cx231xx_i2c_xfer_data req_data
;
342 /* prepare xfer_data struct */
343 req_data
.dev_addr
= msg
->addr
;
344 req_data
.direction
= I2C_M_RD
;
345 req_data
.saddr_len
= 0;
346 req_data
.saddr_dat
= 0;
347 req_data
.buf_size
= 1;
348 req_data
.p_buffer
= buf
;
350 /* usb send command */
351 status
= dev
->cx231xx_send_usb_command(bus
, &req_data
);
353 return status
< 0 ? status
: 0;
358 * the main i2c transfer function
360 static int cx231xx_i2c_xfer(struct i2c_adapter
*i2c_adap
,
361 struct i2c_msg msgs
[], int num
)
363 struct cx231xx_i2c
*bus
= i2c_adap
->algo_data
;
364 struct cx231xx
*dev
= bus
->dev
;
365 int addr
, rc
, i
, byte
;
367 mutex_lock(&dev
->i2c_lock
);
368 for (i
= 0; i
< num
; i
++) {
372 dprintk2(2, "%s %s addr=0x%x len=%d:",
373 (msgs
[i
].flags
& I2C_M_RD
) ? "read" : "write",
374 i
== num
- 1 ? "stop" : "nonstop", addr
, msgs
[i
].len
);
376 /* no len: check only for device presence */
377 rc
= cx231xx_i2c_check_for_device(i2c_adap
, &msgs
[i
]);
379 dprintk2(2, " no device\n");
380 mutex_unlock(&dev
->i2c_lock
);
384 } else if (msgs
[i
].flags
& I2C_M_RD
) {
386 rc
= cx231xx_i2c_recv_bytes(i2c_adap
, &msgs
[i
]);
387 if (i2c_debug
>= 2) {
388 for (byte
= 0; byte
< msgs
[i
].len
; byte
++)
389 printk(KERN_CONT
" %02x", msgs
[i
].buf
[byte
]);
391 } else if (i
+ 1 < num
&& (msgs
[i
+ 1].flags
& I2C_M_RD
) &&
392 msgs
[i
].addr
== msgs
[i
+ 1].addr
393 && (msgs
[i
].len
<= 2) && (bus
->nr
< 3)) {
395 if (i2c_debug
>= 2) {
396 for (byte
= 0; byte
< msgs
[i
].len
; byte
++)
397 printk(KERN_CONT
" %02x", msgs
[i
].buf
[byte
]);
398 printk(KERN_CONT
"\n");
401 dprintk2(2, "plus %s %s addr=0x%x len=%d:",
402 (msgs
[i
+1].flags
& I2C_M_RD
) ? "read" : "write",
403 i
+1 == num
- 1 ? "stop" : "nonstop", addr
, msgs
[i
+1].len
);
404 rc
= cx231xx_i2c_recv_bytes_with_saddr(i2c_adap
,
407 if (i2c_debug
>= 2) {
408 for (byte
= 0; byte
< msgs
[i
+1].len
; byte
++)
409 printk(KERN_CONT
" %02x", msgs
[i
+1].buf
[byte
]);
414 if (i2c_debug
>= 2) {
415 for (byte
= 0; byte
< msgs
[i
].len
; byte
++)
416 printk(KERN_CONT
" %02x", msgs
[i
].buf
[byte
]);
418 rc
= cx231xx_i2c_send_bytes(i2c_adap
, &msgs
[i
]);
423 printk(KERN_CONT
"\n");
425 mutex_unlock(&dev
->i2c_lock
);
428 dprintk2(2, " ERROR: %i\n", rc
);
429 mutex_unlock(&dev
->i2c_lock
);
433 /* ----------------------------------------------------------- */
438 static u32
functionality(struct i2c_adapter
*adap
)
440 return I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_I2C
;
443 static const struct i2c_algorithm cx231xx_algo
= {
444 .master_xfer
= cx231xx_i2c_xfer
,
445 .functionality
= functionality
,
448 static const struct i2c_adapter cx231xx_adap_template
= {
449 .owner
= THIS_MODULE
,
451 .algo
= &cx231xx_algo
,
454 /* ----------------------------------------------------------- */
458 * incomplete list of known devices
460 static const char *i2c_devs
[128] = {
461 [0x20 >> 1] = "demod",
462 [0x60 >> 1] = "colibri",
463 [0x88 >> 1] = "hammerhead",
465 [0x32 >> 1] = "GeminiIII",
466 [0x02 >> 1] = "Aquarius",
467 [0xa0 >> 1] = "eeprom",
468 [0xc0 >> 1] = "tuner",
469 [0xc2 >> 1] = "tuner",
473 * cx231xx_do_i2c_scan()
474 * check i2c address range for devices
476 void cx231xx_do_i2c_scan(struct cx231xx
*dev
, int i2c_port
)
480 struct i2c_adapter
*adap
;
481 struct i2c_msg msg
= {
490 /* Don't generate I2C errors during scan */
491 dev
->i2c_scan_running
= true;
492 adap
= cx231xx_get_i2c_adap(dev
, i2c_port
);
494 for (i
= 0; i
< 128; i
++) {
496 rc
= i2c_transfer(adap
, &msg
, 1);
501 "i2c scan: found device @ port %d addr 0x%x [%s]\n",
504 i2c_devs
[i
] ? i2c_devs
[i
] : "???");
507 dev
->i2c_scan_running
= false;
511 * cx231xx_i2c_register()
514 int cx231xx_i2c_register(struct cx231xx_i2c
*bus
)
516 struct cx231xx
*dev
= bus
->dev
;
518 if (!dev
->cx231xx_send_usb_command
)
521 bus
->i2c_adap
= cx231xx_adap_template
;
522 bus
->i2c_adap
.dev
.parent
= dev
->dev
;
524 snprintf(bus
->i2c_adap
.name
, sizeof(bus
->i2c_adap
.name
), "%s-%d", bus
->dev
->name
, bus
->nr
);
526 bus
->i2c_adap
.algo_data
= bus
;
527 i2c_set_adapdata(&bus
->i2c_adap
, &dev
->v4l2_dev
);
528 bus
->i2c_rc
= i2c_add_adapter(&bus
->i2c_adap
);
530 if (0 != bus
->i2c_rc
)
532 "i2c bus %d register FAILED\n", bus
->nr
);
538 * cx231xx_i2c_unregister()
541 void cx231xx_i2c_unregister(struct cx231xx_i2c
*bus
)
544 i2c_del_adapter(&bus
->i2c_adap
);
548 * cx231xx_i2c_mux_select()
549 * switch i2c master number 1 between port1 and port3
551 static int cx231xx_i2c_mux_select(struct i2c_mux_core
*muxc
, u32 chan_id
)
553 struct cx231xx
*dev
= i2c_mux_priv(muxc
);
555 return cx231xx_enable_i2c_port_3(dev
, chan_id
);
558 int cx231xx_i2c_mux_create(struct cx231xx
*dev
)
560 dev
->muxc
= i2c_mux_alloc(&dev
->i2c_bus
[1].i2c_adap
, dev
->dev
, 2, 0, 0,
561 cx231xx_i2c_mux_select
, NULL
);
564 dev
->muxc
->priv
= dev
;
568 int cx231xx_i2c_mux_register(struct cx231xx
*dev
, int mux_no
)
570 return i2c_mux_add_adapter(dev
->muxc
,
572 mux_no
/* chan_id */,
576 void cx231xx_i2c_mux_unregister(struct cx231xx
*dev
)
578 i2c_mux_del_adapters(dev
->muxc
);
581 struct i2c_adapter
*cx231xx_get_i2c_adap(struct cx231xx
*dev
, int i2c_port
)
585 return &dev
->i2c_bus
[0].i2c_adap
;
587 return &dev
->i2c_bus
[1].i2c_adap
;
589 return &dev
->i2c_bus
[2].i2c_adap
;
591 return dev
->muxc
->adapter
[0];
593 return dev
->muxc
->adapter
[1];
598 EXPORT_SYMBOL_GPL(cx231xx_get_i2c_adap
);