usbvision-i2c.c revision 483dfdb64fd4a9f240c84e0e225a90c044d65402
1/* 2 * I2C_ALGO_USB.C 3 * i2c algorithm for USB-I2C Bridges 4 * 5 * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de> 6 * Dwaine Garden <dwainegarden@rogers.com> 7 * 8 * This module is part of usbvision driver project. 9 * Updates to driver completed by Dwaine P. Garden 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation; either version 2 of the License, or 14 * (at your option) any later version. 15 * 16 * This program is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 * 21 * You should have received a copy of the GNU General Public License 22 * along with this program; if not, write to the Free Software 23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 24 */ 25 26 27#include <linux/kernel.h> 28#include <linux/module.h> 29#include <linux/delay.h> 30#include <linux/slab.h> 31#include <linux/version.h> 32 #include <linux/utsname.h> 33#include <linux/init.h> 34#include <asm/uaccess.h> 35#include <linux/ioport.h> 36#include <linux/errno.h> 37#include <linux/sched.h> 38#include <linux/usb.h> 39#include <linux/i2c.h> 40#include "usbvision.h" 41 42#define DBG_I2C 1<<0 43#define DBG_ALGO 1<<1 44 45static int i2c_debug = 0; 46 47module_param (i2c_debug, int, 0644); // debug_i2c_usb mode of the device driver 48MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]"); 49 50#define PDEBUG(level, fmt, args...) \ 51 if (i2c_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args) 52 53static inline int try_write_address(struct i2c_adapter *i2c_adap, 54 unsigned char addr, int retries) 55{ 56 struct i2c_algo_usb_data *adap = i2c_adap->algo_data; 57 void *data; 58 int i, ret = -1; 59 char buf[4]; 60 61 data = i2c_get_adapdata(i2c_adap); 62 buf[0] = 0x00; 63 for (i = 0; i <= retries; i++) { 64 ret = (adap->outb(data, addr, buf, 1)); 65 if (ret == 1) 66 break; /* success! */ 67 udelay(5 /*adap->udelay */ ); 68 if (i == retries) /* no success */ 69 break; 70 udelay(adap->udelay); 71 } 72 if (i) { 73 PDEBUG(DBG_ALGO,"Needed %d retries for address %#2x", i, addr); 74 PDEBUG(DBG_ALGO,"Maybe there's no device at this address"); 75 } 76 return ret; 77} 78 79static inline int try_read_address(struct i2c_adapter *i2c_adap, 80 unsigned char addr, int retries) 81{ 82 struct i2c_algo_usb_data *adap = i2c_adap->algo_data; 83 void *data; 84 int i, ret = -1; 85 char buf[4]; 86 87 data = i2c_get_adapdata(i2c_adap); 88 for (i = 0; i <= retries; i++) { 89 ret = (adap->inb(data, addr, buf, 1)); 90 if (ret == 1) 91 break; /* success! */ 92 udelay(5 /*adap->udelay */ ); 93 if (i == retries) /* no success */ 94 break; 95 udelay(adap->udelay); 96 } 97 if (i) { 98 PDEBUG(DBG_ALGO,"Needed %d retries for address %#2x", i, addr); 99 PDEBUG(DBG_ALGO,"Maybe there's no device at this address"); 100 } 101 return ret; 102} 103 104static inline int usb_find_address(struct i2c_adapter *i2c_adap, 105 struct i2c_msg *msg, int retries, 106 unsigned char *add) 107{ 108 unsigned short flags = msg->flags; 109 110 unsigned char addr; 111 int ret; 112 if ((flags & I2C_M_TEN)) { 113 /* a ten bit address */ 114 addr = 0xf0 | ((msg->addr >> 7) & 0x03); 115 /* try extended address code... */ 116 ret = try_write_address(i2c_adap, addr, retries); 117 if (ret != 1) { 118 err("died at extended address code, while writing"); 119 return -EREMOTEIO; 120 } 121 add[0] = addr; 122 if (flags & I2C_M_RD) { 123 /* okay, now switch into reading mode */ 124 addr |= 0x01; 125 ret = try_read_address(i2c_adap, addr, retries); 126 if (ret != 1) { 127 err("died at extended address code, while reading"); 128 return -EREMOTEIO; 129 } 130 } 131 132 } else { /* normal 7bit address */ 133 addr = (msg->addr << 1); 134 if (flags & I2C_M_RD) 135 addr |= 1; 136 if (flags & I2C_M_REV_DIR_ADDR) 137 addr ^= 1; 138 139 add[0] = addr; 140 if (flags & I2C_M_RD) 141 ret = try_read_address(i2c_adap, addr, retries); 142 else 143 ret = try_write_address(i2c_adap, addr, retries); 144 145 if (ret != 1) { 146 return -EREMOTEIO; 147 } 148 } 149 return 0; 150} 151 152static int 153usb_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num) 154{ 155 struct i2c_msg *pmsg; 156 struct i2c_algo_usb_data *adap = i2c_adap->algo_data; 157 void *data; 158 int i, ret; 159 unsigned char addr; 160 161 data = i2c_get_adapdata(i2c_adap); 162 163 for (i = 0; i < num; i++) { 164 pmsg = &msgs[i]; 165 ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr); 166 if (ret != 0) { 167 PDEBUG(DBG_ALGO,"got NAK from device, message #%d", i); 168 return (ret < 0) ? ret : -EREMOTEIO; 169 } 170 171 if (pmsg->flags & I2C_M_RD) { 172 /* read bytes into buffer */ 173 ret = (adap->inb(data, addr, pmsg->buf, pmsg->len)); 174 if (ret < pmsg->len) { 175 return (ret < 0) ? ret : -EREMOTEIO; 176 } 177 } else { 178 /* write bytes from buffer */ 179 ret = (adap->outb(data, addr, pmsg->buf, pmsg->len)); 180 if (ret < pmsg->len) { 181 return (ret < 0) ? ret : -EREMOTEIO; 182 } 183 } 184 } 185 return num; 186} 187 188static int algo_control(struct i2c_adapter *adapter, unsigned int cmd, unsigned long arg) 189{ 190 return 0; 191} 192 193static u32 usb_func(struct i2c_adapter *adap) 194{ 195 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING; 196} 197 198 199/* -----exported algorithm data: ------------------------------------- */ 200 201static struct i2c_algorithm i2c_usb_algo = { 202 .master_xfer = usb_xfer, 203 .smbus_xfer = NULL, 204 .algo_control = algo_control, 205 .functionality = usb_func, 206}; 207 208 209/* 210 * registering functions to load algorithms at runtime 211 */ 212int usbvision_i2c_usb_add_bus(struct i2c_adapter *adap) 213{ 214 PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]"); 215 PDEBUG(DBG_ALGO, "ALGO debugging is enabled [i2c]"); 216 217 /* register new adapter to i2c module... */ 218 219 adap->algo = &i2c_usb_algo; 220 221 adap->timeout = 100; /* default values, should */ 222 adap->retries = 3; /* be replaced by defines */ 223 224 i2c_add_adapter(adap); 225 226 PDEBUG(DBG_ALGO,"i2c bus for %s registered", adap->name); 227 228 return 0; 229} 230 231 232int usbvision_i2c_usb_del_bus(struct i2c_adapter *adap) 233{ 234 235 i2c_del_adapter(adap); 236 237 PDEBUG(DBG_ALGO,"i2c bus for %s unregistered", adap->name); 238 239 return 0; 240} 241 242 243/* ----------------------------------------------------------------------- */ 244/* usbvision specific I2C functions */ 245/* ----------------------------------------------------------------------- */ 246static struct i2c_adapter i2c_adap_template; 247static struct i2c_algo_usb_data i2c_algo_template; 248static struct i2c_client i2c_client_template; 249 250int usbvision_init_i2c(struct usb_usbvision *usbvision) 251{ 252 memcpy(&usbvision->i2c_adap, &i2c_adap_template, 253 sizeof(struct i2c_adapter)); 254 memcpy(&usbvision->i2c_algo, &i2c_algo_template, 255 sizeof(struct i2c_algo_usb_data)); 256 memcpy(&usbvision->i2c_client, &i2c_client_template, 257 sizeof(struct i2c_client)); 258 259 sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name), 260 " #%d", usbvision->vdev->minor & 0x1f); 261 PDEBUG(DBG_I2C,"Adaptername: %s", usbvision->i2c_adap.name); 262 263 i2c_set_adapdata(&usbvision->i2c_adap, usbvision); 264 i2c_set_clientdata(&usbvision->i2c_client, usbvision); 265 i2c_set_algo_usb_data(&usbvision->i2c_algo, usbvision); 266 267 usbvision->i2c_adap.algo_data = &usbvision->i2c_algo; 268 usbvision->i2c_client.adapter = &usbvision->i2c_adap; 269 270 if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) { 271 printk(KERN_ERR "usbvision_init_i2c: can't write reg\n"); 272 return -EBUSY; 273 } 274 275#ifdef CONFIG_MODULES 276 /* Request the load of the i2c modules we need */ 277 switch (usbvision_device_data[usbvision->DevModel].Codec) { 278 case CODEC_SAA7113: 279 request_module("saa7115"); 280 break; 281 case CODEC_SAA7111: 282 request_module("saa7115"); 283 break; 284 } 285 if (usbvision_device_data[usbvision->DevModel].Tuner == 1) { 286 request_module("tuner"); 287 } 288#endif 289 290 return usbvision_i2c_usb_add_bus(&usbvision->i2c_adap); 291} 292 293void call_i2c_clients(struct usb_usbvision *usbvision, unsigned int cmd, 294 void *arg) 295{ 296 BUG_ON(NULL == usbvision->i2c_adap.algo_data); 297 i2c_clients_command(&usbvision->i2c_adap, cmd, arg); 298} 299 300static int attach_inform(struct i2c_client *client) 301{ 302 struct usb_usbvision *usbvision; 303 304 usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter); 305 306 switch (client->addr << 1) { 307 case 0x43: 308 case 0x4b: 309 { 310 struct tuner_setup tun_setup; 311 312 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO; 313 tun_setup.type = TUNER_TDA9887; 314 tun_setup.addr = client->addr; 315 316 call_i2c_clients(usbvision, TUNER_SET_TYPE_ADDR, &tun_setup); 317 318 break; 319 } 320 case 0x42: 321 PDEBUG(DBG_I2C,"attach_inform: saa7114 detected."); 322 break; 323 case 0x4a: 324 PDEBUG(DBG_I2C,"attach_inform: saa7113 detected."); 325 break; 326 case 0xa0: 327 PDEBUG(DBG_I2C,"attach_inform: eeprom detected."); 328 break; 329 330 default: 331 { 332 struct tuner_setup tun_setup; 333 334 PDEBUG(DBG_I2C,"attach inform: detected I2C address %x", client->addr << 1); 335 usbvision->tuner_addr = client->addr; 336 337 if ((usbvision->have_tuner) && (usbvision->tuner_type != -1)) { 338 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO; 339 tun_setup.type = usbvision->tuner_type; 340 tun_setup.addr = usbvision->tuner_addr; 341 call_i2c_clients(usbvision, TUNER_SET_TYPE_ADDR, &tun_setup); 342 } 343 } 344 break; 345 } 346 return 0; 347} 348 349static int detach_inform(struct i2c_client *client) 350{ 351 struct usb_usbvision *usbvision; 352 353 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) 354 usbvision = (struct usb_usbvision *)client->adapter->data; 355 #else 356 usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter); 357 #endif 358 359 PDEBUG(DBG_I2C,"usbvision[%d] detaches %s", usbvision->nr, client->name); 360 return 0; 361} 362 363static int 364usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr, 365 char *buf, short len) 366{ 367 int rc, retries; 368 369 for (retries = 5;;) { 370 rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr); 371 if (rc < 0) 372 return rc; 373 374 /* Initiate byte read cycle */ 375 /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */ 376 /* d3 0=Wr 1=Rd */ 377 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 378 (len & 0x07) | 0x18); 379 if (rc < 0) 380 return rc; 381 382 /* Test for Busy and ACK */ 383 do { 384 /* USBVISION_SER_CONT -> d4 == 0 busy */ 385 rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT); 386 } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */ 387 if (rc < 0) 388 return rc; 389 390 /* USBVISION_SER_CONT -> d5 == 1 Not ack */ 391 if ((rc & 0x20) == 0) /* Ack? */ 392 break; 393 394 /* I2C abort */ 395 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00); 396 if (rc < 0) 397 return rc; 398 399 if (--retries < 0) 400 return -1; 401 } 402 403 switch (len) { 404 case 4: 405 buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4); 406 case 3: 407 buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3); 408 case 2: 409 buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2); 410 case 1: 411 buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1); 412 break; 413 default: 414 printk(KERN_ERR 415 "usbvision_i2c_read_max4: buffer length > 4\n"); 416 } 417 418 if (i2c_debug & DBG_I2C) { 419 int idx; 420 for (idx = 0; idx < len; idx++) { 421 PDEBUG(DBG_I2C,"read %x from address %x", (unsigned char)buf[idx], addr); 422 } 423 } 424 return len; 425} 426 427 428static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision, 429 unsigned char addr, const char *buf, 430 short len) 431{ 432 int rc, retries; 433 int i; 434 unsigned char value[6]; 435 unsigned char ser_cont; 436 437 ser_cont = (len & 0x07) | 0x10; 438 439 value[0] = addr; 440 value[1] = ser_cont; 441 for (i = 0; i < len; i++) 442 value[i + 2] = buf[i]; 443 444 for (retries = 5;;) { 445 rc = usb_control_msg(usbvision->dev, 446 usb_sndctrlpipe(usbvision->dev, 1), 447 USBVISION_OP_CODE, 448 USB_DIR_OUT | USB_TYPE_VENDOR | 449 USB_RECIP_ENDPOINT, 0, 450 (__u16) USBVISION_SER_ADRS, value, 451 len + 2, HZ); 452 453 if (rc < 0) 454 return rc; 455 456 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 457 (len & 0x07) | 0x10); 458 if (rc < 0) 459 return rc; 460 461 /* Test for Busy and ACK */ 462 do { 463 rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT); 464 } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */ 465 if (rc < 0) 466 return rc; 467 468 if ((rc & 0x20) == 0) /* Ack? */ 469 break; 470 471 /* I2C abort */ 472 usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00); 473 474 if (--retries < 0) 475 return -1; 476 477 } 478 479 if (i2c_debug & DBG_I2C) { 480 int idx; 481 for (idx = 0; idx < len; idx++) { 482 PDEBUG(DBG_I2C,"wrote %x at address %x", (unsigned char)buf[idx], addr); 483 } 484 } 485 return len; 486} 487 488static int usbvision_i2c_write(void *data, unsigned char addr, char *buf, 489 short len) 490{ 491 char *bufPtr = buf; 492 int retval; 493 int wrcount = 0; 494 int count; 495 int maxLen = 4; 496 struct usb_usbvision *usbvision = (struct usb_usbvision *) data; 497 498 while (len > 0) { 499 count = (len > maxLen) ? maxLen : len; 500 retval = usbvision_i2c_write_max4(usbvision, addr, bufPtr, count); 501 if (retval > 0) { 502 len -= count; 503 bufPtr += count; 504 wrcount += count; 505 } else 506 return (retval < 0) ? retval : -EFAULT; 507 } 508 return wrcount; 509} 510 511static int usbvision_i2c_read(void *data, unsigned char addr, char *buf, 512 short len) 513{ 514 char temp[4]; 515 int retval, i; 516 int rdcount = 0; 517 int count; 518 struct usb_usbvision *usbvision = (struct usb_usbvision *) data; 519 520 while (len > 0) { 521 count = (len > 3) ? 4 : len; 522 retval = usbvision_i2c_read_max4(usbvision, addr, temp, count); 523 if (retval > 0) { 524 for (i = 0; i < len; i++) 525 buf[rdcount + i] = temp[i]; 526 len -= count; 527 rdcount += count; 528 } else 529 return (retval < 0) ? retval : -EFAULT; 530 } 531 return rdcount; 532} 533 534static struct i2c_algo_usb_data i2c_algo_template = { 535 .data = NULL, 536 .inb = usbvision_i2c_read, 537 .outb = usbvision_i2c_write, 538 .udelay = 10, 539 .mdelay = 10, 540 .timeout = 100, 541}; 542 543static struct i2c_adapter i2c_adap_template = { 544 .owner = THIS_MODULE, 545 .name = "usbvision", 546 .id = I2C_HW_B_BT848, /* FIXME */ 547 .algo = NULL, 548 .algo_data = NULL, 549 .client_register = attach_inform, 550 .client_unregister = detach_inform, 551#if defined (I2C_ADAP_CLASS_TV_ANALOG) 552 .class = I2C_ADAP_CLASS_TV_ANALOG, 553#elif defined (I2C_CLASS_TV_ANALOG) 554 .class = I2C_CLASS_TV_ANALOG, 555#endif 556}; 557 558static struct i2c_client i2c_client_template = { 559 .name = "usbvision internal", 560}; 561 562EXPORT_SYMBOL(usbvision_i2c_usb_add_bus); 563EXPORT_SYMBOL(usbvision_i2c_usb_del_bus); 564 565/* 566 * Overrides for Emacs so that we follow Linus's tabbing style. 567 * --------------------------------------------------------------------------- 568 * Local variables: 569 * c-basic-offset: 8 570 * End: 571 */ 572