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>
33 /* ----------------------------------------------------------- */
35 static unsigned int i2c_scan;
36 module_param(i2c_scan, int, 0444);
37 MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
39 static unsigned int i2c_debug;
40 module_param(i2c_debug, int, 0644);
41 MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
44 #define dprintk1(lvl, fmt, args...) \
46 if (i2c_debug >= lvl) { \
47 printk(fmt, ##args); \
51 #define dprintk2(lvl, fmt, args...) \
53 if (i2c_debug >= lvl) { \
54 printk(KERN_DEBUG "%s at %s: " fmt, \
55 dev->name, __func__ , ##args); \
61 * cx231xx_i2c_send_bytes()
63 int cx231xx_i2c_send_bytes(struct i2c_adapter *i2c_adap,
64 const struct i2c_msg *msg)
66 struct cx231xx_i2c *bus = i2c_adap->algo_data;
67 struct cx231xx *dev = bus->dev;
68 struct cx231xx_i2c_xfer_data req_data;
78 if( (bus->nr ==1) && (msg->addr == 0x61) && (dev->tuner_type == TUNER_XC5000) ) {
82 if( size == 2 ) { /* register write sub addr*/
84 /* Just writing sub address will cause problem to XC5000
85 So ignore the request */
88 } else if( size == 4 ) { /* register write with sub addr*/
91 saddr = msg->buf[0] << 8 | msg->buf[1];
92 else if ( msg->len == 1 )
96 case 0x0000: /* start tuner calibration mode */
98 dev->xc_fw_load_done = 1; /* FW Loading is done */
100 case 0x000D: /* Set signal source */
101 case 0x0001: /* Set TV standard - Video */
102 case 0x0002: /* Set TV standard - Audio */
103 case 0x0003: /* Set RF Frequency */
107 if(dev->xc_fw_load_done)
113 dprintk1(1, " GPIO W R I T E : addr 0x%x, len %d, saddr 0x%x\n",
114 msg->addr, msg->len, saddr);
116 return dev->cx231xx_gpio_i2c_write(dev, msg->addr, msg->buf, msg->len);
121 /* special case for Xc5000 tuner case */
124 /* adjust the length to correct length */
126 buf_ptr = (u8*) (msg->buf + 1 );
129 /* prepare xfer_data struct */
130 req_data.dev_addr = msg->addr;
131 req_data.direction = msg->flags;
132 req_data.saddr_len = saddr_len;
133 req_data.saddr_dat = msg->buf[0];
134 req_data.buf_size = size > 16 ? 16: size;
135 req_data.p_buffer = (u8*)(buf_ptr + loop * 16);
137 bus->i2c_nostop = (size > 16) ? 1: 0;
138 bus->i2c_reserve = (loop == 0) ? 0: 1;
140 /* usb send command */
141 status = dev->cx231xx_send_usb_command(bus, &req_data);
152 bus->i2c_reserve = 0;
154 } else { /* regular case */
156 /* prepare xfer_data struct */
157 req_data.dev_addr = msg->addr;
158 req_data.direction = msg->flags;
159 req_data.saddr_len = 0;
160 req_data.saddr_dat = 0;
161 req_data.buf_size = msg->len;
162 req_data.p_buffer = msg->buf;
164 /* usb send command */
165 status = dev->cx231xx_send_usb_command(bus, &req_data);
168 return status < 0 ? status: 0;
172 * cx231xx_i2c_recv_bytes()
173 * read a byte from the i2c device
175 static int cx231xx_i2c_recv_bytes(struct i2c_adapter *i2c_adap,
176 const struct i2c_msg *msg)
178 struct cx231xx_i2c *bus = i2c_adap->algo_data;
179 struct cx231xx *dev = bus->dev;
180 struct cx231xx_i2c_xfer_data req_data;
185 if((bus->nr ==1) && (msg->addr == 0x61) && dev->tuner_type == TUNER_XC5000) {
188 saddr = msg->buf[0] << 8 | msg->buf[1];
189 else if ( msg->len == 1 )
192 if( dev->xc_fw_load_done) {
195 case 0x0009: /* BUSY check */
196 dprintk1(1, " GPIO R E A D : Special case BUSY check \n");
197 /* Try to read BUSY register, just set it to zero */
202 case 0x0004: /* read Lock status */
209 /* this is a special case to handle Xceive tuner clock stretch issue
210 with gpio based I2C interface */
211 dprintk1(1, " GPIO R E A D : addr 0x%x, len %d, saddr 0x%x\n",
212 msg->addr, msg->len, msg->buf[0] << 8| msg->buf[1]);
213 status = dev->cx231xx_gpio_i2c_write(dev, msg->addr, msg->buf, msg->len);
214 status = dev->cx231xx_gpio_i2c_read(dev, msg->addr, msg->buf, msg->len);
219 /* prepare xfer_data struct */
220 req_data.dev_addr = msg->addr;
221 req_data.direction = msg->flags;
222 req_data.saddr_len = msg->len;
223 req_data.saddr_dat = msg->buf[0] << 8 | msg->buf[1];
224 req_data.buf_size = msg->len;
225 req_data.p_buffer = msg->buf;
227 /* usb send command */
228 status = dev->cx231xx_send_usb_command(bus, &req_data);
232 /* prepare xfer_data struct */
233 req_data.dev_addr = msg->addr;
234 req_data.direction = msg->flags;
235 req_data.saddr_len = 0;
236 req_data.saddr_dat = 0;
237 req_data.buf_size = msg->len;
238 req_data.p_buffer = msg->buf;
240 /* usb send command */
241 status = dev->cx231xx_send_usb_command(bus, &req_data);
244 return status < 0 ? status: 0;
248 * cx231xx_i2c_recv_bytes_with_saddr()
249 * read a byte from the i2c device
251 static int cx231xx_i2c_recv_bytes_with_saddr(struct i2c_adapter *i2c_adap,
252 const struct i2c_msg *msg1, const struct i2c_msg *msg2)
254 struct cx231xx_i2c *bus = i2c_adap->algo_data;
255 struct cx231xx *dev = bus->dev;
256 struct cx231xx_i2c_xfer_data req_data;
262 saddr = msg1->buf[0] << 8 | msg1->buf[1];
263 else if ( msg1->len == 1 )
264 saddr = msg1->buf[0];
266 if ( (bus->nr ==1) && (msg2->addr == 0x61) && dev->tuner_type == TUNER_XC5000) {
268 if( (msg2->len < 16) ) {
270 dprintk1(1, " i2c_read : addr 0x%x, len %d, subaddr 0x%x, leng %d\n",
271 msg2->addr, msg2->len, saddr, msg1->len);
274 case 0x0008: /* read FW load status */
277 case 0x0004: /* read Lock status */
283 status = dev->cx231xx_gpio_i2c_write(dev, msg1->addr, msg1->buf, msg1->len);
284 status = dev->cx231xx_gpio_i2c_read(dev, msg2->addr, msg2->buf, msg2->len);
290 /* prepare xfer_data struct */
291 req_data.dev_addr = msg2->addr;
292 req_data.direction = msg2->flags;
293 req_data.saddr_len = msg1->len;
294 req_data.saddr_dat = saddr;
295 req_data.buf_size = msg2->len;
296 req_data.p_buffer = msg2->buf;
298 /* usb send command */
299 status = dev->cx231xx_send_usb_command(bus, &req_data);
301 return status < 0 ? status: 0;
305 * cx231xx_i2c_check_for_device()
306 * check if there is a i2c_device at the supplied address
308 static int cx231xx_i2c_check_for_device(struct i2c_adapter *i2c_adap,
309 const struct i2c_msg *msg)
311 struct cx231xx_i2c *bus = i2c_adap->algo_data;
312 struct cx231xx *dev = bus->dev;
313 struct cx231xx_i2c_xfer_data req_data;
316 /* prepare xfer_data struct */
317 req_data.dev_addr = msg->addr;
318 req_data.direction = msg->flags;
319 req_data.saddr_len = 0;
320 req_data.saddr_dat = 0;
321 req_data.buf_size = 0;
322 req_data.p_buffer = NULL;
324 /* usb send command */
325 status = dev->cx231xx_send_usb_command(bus, &req_data);
327 return status < 0 ? status: 0;
332 * the main i2c transfer function
334 static int cx231xx_i2c_xfer(struct i2c_adapter *i2c_adap,
335 struct i2c_msg msgs[], int num)
337 struct cx231xx_i2c *bus = i2c_adap->algo_data;
338 struct cx231xx *dev = bus->dev;
339 int addr, rc, i, byte;
344 for (i = 0; i < num; i++) {
346 addr = msgs[i].addr >> 1;
348 dprintk2(2, "%s %s addr=%x len=%d:",
349 (msgs[i].flags & I2C_M_RD) ? "read" : "write",
350 i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
351 if (!msgs[i].len) { /* no len: check only for device presence */
352 rc = cx231xx_i2c_check_for_device(i2c_adap, &msgs[i]);
354 dprintk2(2, " no device\n");
358 } else if (msgs[i].flags & I2C_M_RD) {
360 rc = cx231xx_i2c_recv_bytes(i2c_adap, &msgs[i]);
361 if (i2c_debug >= 2) {
362 for (byte = 0; byte < msgs[i].len; byte++)
363 printk(" %02x", msgs[i].buf[byte]);
365 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
366 msgs[i].addr == msgs[i + 1].addr && (msgs[i].len <= 2) && (bus->nr < 2)) {
368 rc = cx231xx_i2c_recv_bytes_with_saddr(i2c_adap, &msgs[i], &msgs[i+1]);
369 if (i2c_debug >= 2) {
370 for (byte = 0; byte < msgs[i].len; byte++)
371 printk(" %02x", msgs[i].buf[byte]);
376 if (i2c_debug >= 2) {
377 for (byte = 0; byte < msgs[i].len; byte++)
378 printk(" %02x", msgs[i].buf[byte]);
380 rc = cx231xx_i2c_send_bytes(i2c_adap,&msgs[i]);
390 dprintk2(2, " ERROR: %i\n", rc);
394 /* ----------------------------------------------------------- */
399 static u32 functionality(struct i2c_adapter *adap)
401 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
406 * gets called when a device attaches to the i2c bus
407 * does some basic configuration
409 static int attach_inform(struct i2c_client *client)
411 struct cx231xx_i2c *bus = i2c_get_adapdata(client->adapter);
412 struct cx231xx *dev = bus->dev;
414 switch (client->addr << 1) {
416 dprintk1(1, "attach_inform: Geminit III detected.\n");
419 dprintk1(1, "attach_inform: Acquarius detected.\n");
422 dprintk1(1, "attach_inform: eeprom detected.\n");
425 dprintk1(1, "attach_inform: Colibri detected.\n");
429 struct IR_i2c *ir = i2c_get_clientdata(client);
430 dprintk1(1, "attach_inform: IR detected (%s).\n",
432 cx231xx_set_ir(dev, ir);
437 dprintk1(1, "attach_inform: Hammerhead detected.\n");
441 if (!dev->tuner_addr)
442 dev->tuner_addr = client->addr;
444 dprintk1(1, "attach inform: detected I2C address %x\n",
451 static int detach_inform(struct i2c_client *client)
453 dprintk1(1, "i2c detach [client=%s]\n", client->name);
458 static struct i2c_algorithm cx231xx_algo = {
459 .master_xfer = cx231xx_i2c_xfer,
460 .functionality = functionality,
463 static struct i2c_adapter cx231xx_adap_template = {
464 .owner = THIS_MODULE,
465 .class = I2C_CLASS_TV_ANALOG,
467 .id = I2C_HW_B_CX231XX,
468 .algo = &cx231xx_algo,
469 .client_register = attach_inform,
470 .client_unregister = detach_inform,
473 static struct i2c_client cx231xx_client_template = {
474 .name = "cx231xx internal",
477 /* ----------------------------------------------------------- */
481 * incomplete list of known devices
483 static char *i2c_devs[128] = {
484 [0x60 >> 1] = "colibri",
485 [0x88 >> 1] = "hammerhead",
487 [0x32 >> 1] = "GeminiIII",
488 [0x02 >> 1] = "Aquarius",
489 [0xa0 >> 1] = "eeprom",
490 [0xc0 >> 1] = "tuner/XC3028",
491 [0xc2 >> 1] = "tuner/XC5000",
495 * cx231xx_do_i2c_scan()
496 * check i2c address range for devices
498 void cx231xx_do_i2c_scan(struct cx231xx *dev, struct i2c_client *c)
503 cx231xx_info(": Checking for I2C devices ..\n");
504 for (i = 0; i < 128; i++) {
506 rc = i2c_master_recv(c, &buf, 0);
509 cx231xx_info("%s: i2c scan: found device @ 0x%x [%s]\n",
510 dev->name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
512 cx231xx_info(": Completed Checking for I2C devices.\n");
516 * cx231xx_i2c_call_clients()
517 * send commands to all attached i2c devices
519 void cx231xx_i2c_call_clients(struct cx231xx_i2c *bus, unsigned int cmd, void *arg)
521 /* struct cx231xx *dev = bus->dev; */
523 BUG_ON(NULL == bus->i2c_adap.algo_data);
524 i2c_clients_command(&bus->i2c_adap, cmd, arg);
528 * cx231xx_i2c_register()
531 int cx231xx_i2c_register(struct cx231xx_i2c *bus)
533 struct cx231xx *dev = bus->dev;
535 BUG_ON(!dev->cx231xx_send_usb_command);
537 cx231xx_info("%s(bus = %d)\n", __func__, bus->nr);
539 memcpy(&bus->i2c_adap, &cx231xx_adap_template,
540 sizeof(bus->i2c_adap));
541 memcpy(&bus->i2c_algo, &cx231xx_algo,
542 sizeof(bus->i2c_algo));
543 memcpy(&bus->i2c_client, &cx231xx_client_template,
544 sizeof(bus->i2c_client));
546 bus->i2c_adap.dev.parent = &dev->udev->dev;
548 strlcpy(bus->i2c_adap.name, bus->dev->name,
549 sizeof(bus->i2c_adap.name));
551 bus->i2c_algo.data = bus;
552 bus->i2c_adap.algo_data = bus;
553 i2c_set_adapdata(&bus->i2c_adap, bus);
554 i2c_add_adapter(&bus->i2c_adap);
556 bus->i2c_client.adapter = &bus->i2c_adap;
558 if (0 == bus->i2c_rc) {
559 cx231xx_info("%s: i2c bus %d registered\n", dev->name, bus->nr);
561 cx231xx_do_i2c_scan(dev, &bus->i2c_client);
563 cx231xx_warn("%s: i2c bus %d register FAILED\n",
570 * cx231xx_i2c_unregister()
573 int cx231xx_i2c_unregister(struct cx231xx_i2c *bus)
575 i2c_del_adapter(&bus->i2c_adap);