usbvision-i2c.c revision 12a917f69d1468c91d646dbad8408dd0d39d6207
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 int usbvision_i2c_write(void *data, unsigned char addr, char *buf, 54 short len); 55static int usbvision_i2c_read(void *data, unsigned char addr, char *buf, 56 short len); 57 58static inline int try_write_address(struct i2c_adapter *i2c_adap, 59 unsigned char addr, int retries) 60{ 61 void *data; 62 int i, ret = -1; 63 char buf[4]; 64 65 data = i2c_get_adapdata(i2c_adap); 66 buf[0] = 0x00; 67 for (i = 0; i <= retries; i++) { 68 ret = (usbvision_i2c_write(data, addr, buf, 1)); 69 if (ret == 1) 70 break; /* success! */ 71 udelay(5); 72 if (i == retries) /* no success */ 73 break; 74 udelay(10); 75 } 76 if (i) { 77 PDEBUG(DBG_ALGO,"Needed %d retries for address %#2x", i, addr); 78 PDEBUG(DBG_ALGO,"Maybe there's no device at this address"); 79 } 80 return ret; 81} 82 83static inline int try_read_address(struct i2c_adapter *i2c_adap, 84 unsigned char addr, int retries) 85{ 86 void *data; 87 int i, ret = -1; 88 char buf[4]; 89 90 data = i2c_get_adapdata(i2c_adap); 91 for (i = 0; i <= retries; i++) { 92 ret = (usbvision_i2c_read(data, addr, buf, 1)); 93 if (ret == 1) 94 break; /* success! */ 95 udelay(5); 96 if (i == retries) /* no success */ 97 break; 98 udelay(10); 99 } 100 if (i) { 101 PDEBUG(DBG_ALGO,"Needed %d retries for address %#2x", i, addr); 102 PDEBUG(DBG_ALGO,"Maybe there's no device at this address"); 103 } 104 return ret; 105} 106 107static inline int usb_find_address(struct i2c_adapter *i2c_adap, 108 struct i2c_msg *msg, int retries, 109 unsigned char *add) 110{ 111 unsigned short flags = msg->flags; 112 113 unsigned char addr; 114 int ret; 115 if ((flags & I2C_M_TEN)) { 116 /* a ten bit address */ 117 addr = 0xf0 | ((msg->addr >> 7) & 0x03); 118 /* try extended address code... */ 119 ret = try_write_address(i2c_adap, addr, retries); 120 if (ret != 1) { 121 err("died at extended address code, while writing"); 122 return -EREMOTEIO; 123 } 124 add[0] = addr; 125 if (flags & I2C_M_RD) { 126 /* okay, now switch into reading mode */ 127 addr |= 0x01; 128 ret = try_read_address(i2c_adap, addr, retries); 129 if (ret != 1) { 130 err("died at extended address code, while reading"); 131 return -EREMOTEIO; 132 } 133 } 134 135 } else { /* normal 7bit address */ 136 addr = (msg->addr << 1); 137 if (flags & I2C_M_RD) 138 addr |= 1; 139 if (flags & I2C_M_REV_DIR_ADDR) 140 addr ^= 1; 141 142 add[0] = addr; 143 if (flags & I2C_M_RD) 144 ret = try_read_address(i2c_adap, addr, retries); 145 else 146 ret = try_write_address(i2c_adap, addr, retries); 147 148 if (ret != 1) { 149 return -EREMOTEIO; 150 } 151 } 152 return 0; 153} 154 155static int 156usb_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num) 157{ 158 struct i2c_msg *pmsg; 159 void *data; 160 int i, ret; 161 unsigned char addr; 162 163 data = i2c_get_adapdata(i2c_adap); 164 165 for (i = 0; i < num; i++) { 166 pmsg = &msgs[i]; 167 ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr); 168 if (ret != 0) { 169 PDEBUG(DBG_ALGO,"got NAK from device, message #%d", i); 170 return (ret < 0) ? ret : -EREMOTEIO; 171 } 172 173 if (pmsg->flags & I2C_M_RD) { 174 /* read bytes into buffer */ 175 ret = (usbvision_i2c_read(data, addr, pmsg->buf, pmsg->len)); 176 if (ret < pmsg->len) { 177 return (ret < 0) ? ret : -EREMOTEIO; 178 } 179 } else { 180 /* write bytes from buffer */ 181 ret = (usbvision_i2c_write(data, addr, pmsg->buf, pmsg->len)); 182 if (ret < pmsg->len) { 183 return (ret < 0) ? ret : -EREMOTEIO; 184 } 185 } 186 } 187 return num; 188} 189 190static int algo_control(struct i2c_adapter *adapter, unsigned int cmd, unsigned long arg) 191{ 192 return 0; 193} 194 195static u32 usb_func(struct i2c_adapter *adap) 196{ 197 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING; 198} 199 200 201/* -----exported algorithm data: ------------------------------------- */ 202 203static struct i2c_algorithm i2c_usb_algo = { 204 .master_xfer = usb_xfer, 205 .smbus_xfer = NULL, 206 .algo_control = algo_control, 207 .functionality = usb_func, 208}; 209 210 211/* 212 * registering functions to load algorithms at runtime 213 */ 214static int usbvision_i2c_usb_add_bus(struct i2c_adapter *adap) 215{ 216 PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]"); 217 PDEBUG(DBG_ALGO, "ALGO debugging is enabled [i2c]"); 218 219 /* register new adapter to i2c module... */ 220 221 adap->algo = &i2c_usb_algo; 222 223 adap->timeout = 100; /* default values, should */ 224 adap->retries = 3; /* be replaced by defines */ 225 226 i2c_add_adapter(adap); 227 228 PDEBUG(DBG_ALGO,"i2c bus for %s registered", adap->name); 229 230 return 0; 231} 232 233 234int usbvision_i2c_usb_del_bus(struct i2c_adapter *adap) 235{ 236 237 i2c_del_adapter(adap); 238 239 PDEBUG(DBG_ALGO,"i2c bus for %s unregistered", adap->name); 240 241 return 0; 242} 243 244 245/* ----------------------------------------------------------------------- */ 246/* usbvision specific I2C functions */ 247/* ----------------------------------------------------------------------- */ 248static struct i2c_adapter i2c_adap_template; 249static struct i2c_client i2c_client_template; 250 251int usbvision_init_i2c(struct usb_usbvision *usbvision) 252{ 253 memcpy(&usbvision->i2c_adap, &i2c_adap_template, 254 sizeof(struct i2c_adapter)); 255 memcpy(&usbvision->i2c_client, &i2c_client_template, 256 sizeof(struct i2c_client)); 257 258 sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name), 259 " #%d", usbvision->vdev->minor & 0x1f); 260 PDEBUG(DBG_I2C,"Adaptername: %s", usbvision->i2c_adap.name); 261 usbvision->i2c_adap.dev.parent = &usbvision->dev->dev; 262 263 i2c_set_adapdata(&usbvision->i2c_adap, usbvision); 264 i2c_set_clientdata(&usbvision->i2c_client, usbvision); 265 266 usbvision->i2c_client.adapter = &usbvision->i2c_adap; 267 268 if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) { 269 printk(KERN_ERR "usbvision_init_i2c: can't write reg\n"); 270 return -EBUSY; 271 } 272 273#ifdef CONFIG_MODULES 274 /* Request the load of the i2c modules we need */ 275 switch (usbvision_device_data[usbvision->DevModel].Codec) { 276 case CODEC_SAA7113: 277 request_module("saa7115"); 278 break; 279 case CODEC_SAA7111: 280 request_module("saa7115"); 281 break; 282 } 283 if (usbvision_device_data[usbvision->DevModel].Tuner == 1) { 284 request_module("tuner"); 285 } 286#endif 287 288 return usbvision_i2c_usb_add_bus(&usbvision->i2c_adap); 289} 290 291void call_i2c_clients(struct usb_usbvision *usbvision, unsigned int cmd, 292 void *arg) 293{ 294 i2c_clients_command(&usbvision->i2c_adap, cmd, arg); 295} 296 297static int attach_inform(struct i2c_client *client) 298{ 299 struct usb_usbvision *usbvision; 300 301 usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter); 302 303 switch (client->addr << 1) { 304 case 0x43: 305 case 0x4b: 306 { 307 struct tuner_setup tun_setup; 308 309 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO; 310 tun_setup.type = TUNER_TDA9887; 311 tun_setup.addr = client->addr; 312 313 call_i2c_clients(usbvision, TUNER_SET_TYPE_ADDR, &tun_setup); 314 315 break; 316 } 317 case 0x42: 318 PDEBUG(DBG_I2C,"attach_inform: saa7114 detected."); 319 break; 320 case 0x4a: 321 PDEBUG(DBG_I2C,"attach_inform: saa7113 detected."); 322 break; 323 case 0x48: 324 PDEBUG(DBG_I2C,"attach_inform: saa7111 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 usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter); 354 355 PDEBUG(DBG_I2C,"usbvision[%d] detaches %s", usbvision->nr, client->name); 356 return 0; 357} 358 359static int 360usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr, 361 char *buf, short len) 362{ 363 int rc, retries; 364 365 for (retries = 5;;) { 366 rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr); 367 if (rc < 0) 368 return rc; 369 370 /* Initiate byte read cycle */ 371 /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */ 372 /* d3 0=Wr 1=Rd */ 373 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 374 (len & 0x07) | 0x18); 375 if (rc < 0) 376 return rc; 377 378 /* Test for Busy and ACK */ 379 do { 380 /* USBVISION_SER_CONT -> d4 == 0 busy */ 381 rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT); 382 } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */ 383 if (rc < 0) 384 return rc; 385 386 /* USBVISION_SER_CONT -> d5 == 1 Not ack */ 387 if ((rc & 0x20) == 0) /* Ack? */ 388 break; 389 390 /* I2C abort */ 391 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00); 392 if (rc < 0) 393 return rc; 394 395 if (--retries < 0) 396 return -1; 397 } 398 399 switch (len) { 400 case 4: 401 buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4); 402 case 3: 403 buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3); 404 case 2: 405 buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2); 406 case 1: 407 buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1); 408 break; 409 default: 410 printk(KERN_ERR 411 "usbvision_i2c_read_max4: buffer length > 4\n"); 412 } 413 414 if (i2c_debug & DBG_I2C) { 415 int idx; 416 for (idx = 0; idx < len; idx++) { 417 PDEBUG(DBG_I2C,"read %x from address %x", (unsigned char)buf[idx], addr); 418 } 419 } 420 return len; 421} 422 423 424static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision, 425 unsigned char addr, const char *buf, 426 short len) 427{ 428 int rc, retries; 429 int i; 430 unsigned char value[6]; 431 unsigned char ser_cont; 432 433 ser_cont = (len & 0x07) | 0x10; 434 435 value[0] = addr; 436 value[1] = ser_cont; 437 for (i = 0; i < len; i++) 438 value[i + 2] = buf[i]; 439 440 for (retries = 5;;) { 441 rc = usb_control_msg(usbvision->dev, 442 usb_sndctrlpipe(usbvision->dev, 1), 443 USBVISION_OP_CODE, 444 USB_DIR_OUT | USB_TYPE_VENDOR | 445 USB_RECIP_ENDPOINT, 0, 446 (__u16) USBVISION_SER_ADRS, value, 447 len + 2, HZ); 448 449 if (rc < 0) 450 return rc; 451 452 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 453 (len & 0x07) | 0x10); 454 if (rc < 0) 455 return rc; 456 457 /* Test for Busy and ACK */ 458 do { 459 rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT); 460 } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */ 461 if (rc < 0) 462 return rc; 463 464 if ((rc & 0x20) == 0) /* Ack? */ 465 break; 466 467 /* I2C abort */ 468 usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00); 469 470 if (--retries < 0) 471 return -1; 472 473 } 474 475 if (i2c_debug & DBG_I2C) { 476 int idx; 477 for (idx = 0; idx < len; idx++) { 478 PDEBUG(DBG_I2C,"wrote %x at address %x", (unsigned char)buf[idx], addr); 479 } 480 } 481 return len; 482} 483 484static int usbvision_i2c_write(void *data, unsigned char addr, char *buf, 485 short len) 486{ 487 char *bufPtr = buf; 488 int retval; 489 int wrcount = 0; 490 int count; 491 int maxLen = 4; 492 struct usb_usbvision *usbvision = (struct usb_usbvision *) data; 493 494 while (len > 0) { 495 count = (len > maxLen) ? maxLen : len; 496 retval = usbvision_i2c_write_max4(usbvision, addr, bufPtr, count); 497 if (retval > 0) { 498 len -= count; 499 bufPtr += count; 500 wrcount += count; 501 } else 502 return (retval < 0) ? retval : -EFAULT; 503 } 504 return wrcount; 505} 506 507static int usbvision_i2c_read(void *data, unsigned char addr, char *buf, 508 short len) 509{ 510 char temp[4]; 511 int retval, i; 512 int rdcount = 0; 513 int count; 514 struct usb_usbvision *usbvision = (struct usb_usbvision *) data; 515 516 while (len > 0) { 517 count = (len > 3) ? 4 : len; 518 retval = usbvision_i2c_read_max4(usbvision, addr, temp, count); 519 if (retval > 0) { 520 for (i = 0; i < len; i++) 521 buf[rdcount + i] = temp[i]; 522 len -= count; 523 rdcount += count; 524 } else 525 return (retval < 0) ? retval : -EFAULT; 526 } 527 return rdcount; 528} 529 530static struct i2c_adapter i2c_adap_template = { 531 .owner = THIS_MODULE, 532 .name = "usbvision", 533 .id = I2C_HW_B_BT848, /* FIXME */ 534 .client_register = attach_inform, 535 .client_unregister = detach_inform, 536#ifdef I2C_ADAP_CLASS_TV_ANALOG 537 .class = I2C_ADAP_CLASS_TV_ANALOG, 538#else 539 .class = I2C_CLASS_TV_ANALOG, 540#endif 541}; 542 543static struct i2c_client i2c_client_template = { 544 .name = "usbvision internal", 545}; 546 547/* 548 * Overrides for Emacs so that we follow Linus's tabbing style. 549 * --------------------------------------------------------------------------- 550 * Local variables: 551 * c-basic-offset: 8 552 * End: 553 */ 554