usbvision-i2c.c revision 0fda5d4420fe1d6a19189386b6bc6532c97a7e0e
1/* 2 * usbvision_i2c.c 3 * i2c algorithm for USB-I2C Bridges 4 * 5 * Copyright (c) 1999-2007 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/init.h> 32#include <asm/uaccess.h> 33#include <linux/ioport.h> 34#include <linux/errno.h> 35#include <linux/usb.h> 36#include <linux/i2c.h> 37#include "usbvision.h" 38 39#define DBG_I2C 1<<0 40 41static int i2c_debug; 42 43module_param (i2c_debug, int, 0644); // debug_i2c_usb mode of the device driver 44MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]"); 45 46#define PDEBUG(level, fmt, args...) { \ 47 if (i2c_debug & (level)) \ 48 printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \ 49 __func__, __LINE__ , ## args); \ 50 } 51 52static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf, 53 short len); 54static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf, 55 short len); 56 57static inline int try_write_address(struct i2c_adapter *i2c_adap, 58 unsigned char addr, int retries) 59{ 60 struct usb_usbvision *usbvision; 61 int i, ret = -1; 62 char buf[4]; 63 64 usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap); 65 buf[0] = 0x00; 66 for (i = 0; i <= retries; i++) { 67 ret = (usbvision_i2c_write(usbvision, addr, buf, 1)); 68 if (ret == 1) 69 break; /* success! */ 70 udelay(5); 71 if (i == retries) /* no success */ 72 break; 73 udelay(10); 74 } 75 if (i) { 76 PDEBUG(DBG_I2C,"Needed %d retries for address %#2x", i, addr); 77 PDEBUG(DBG_I2C,"Maybe there's no device at this address"); 78 } 79 return ret; 80} 81 82static inline int try_read_address(struct i2c_adapter *i2c_adap, 83 unsigned char addr, int retries) 84{ 85 struct usb_usbvision *usbvision; 86 int i, ret = -1; 87 char buf[4]; 88 89 usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap); 90 for (i = 0; i <= retries; i++) { 91 ret = (usbvision_i2c_read(usbvision, addr, buf, 1)); 92 if (ret == 1) 93 break; /* success! */ 94 udelay(5); 95 if (i == retries) /* no success */ 96 break; 97 udelay(10); 98 } 99 if (i) { 100 PDEBUG(DBG_I2C,"Needed %d retries for address %#2x", i, addr); 101 PDEBUG(DBG_I2C,"Maybe there's no device at this address"); 102 } 103 return ret; 104} 105 106static inline int usb_find_address(struct i2c_adapter *i2c_adap, 107 struct i2c_msg *msg, int retries, 108 unsigned char *add) 109{ 110 unsigned short flags = msg->flags; 111 112 unsigned char addr; 113 int ret; 114 if ((flags & I2C_M_TEN)) { 115 /* a ten bit address */ 116 addr = 0xf0 | ((msg->addr >> 7) & 0x03); 117 /* try extended address code... */ 118 ret = try_write_address(i2c_adap, addr, retries); 119 if (ret != 1) { 120 dev_err(&i2c_adap->dev, 121 "died at extended address code, while writing\n"); 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 dev_err(&i2c_adap->dev, 131 "died at extended address code, while reading\n"); 132 return -EREMOTEIO; 133 } 134 } 135 136 } else { /* normal 7bit address */ 137 addr = (msg->addr << 1); 138 if (flags & I2C_M_RD) 139 addr |= 1; 140 141 add[0] = addr; 142 if (flags & I2C_M_RD) 143 ret = try_read_address(i2c_adap, addr, retries); 144 else 145 ret = try_write_address(i2c_adap, addr, retries); 146 147 if (ret != 1) { 148 return -EREMOTEIO; 149 } 150 } 151 return 0; 152} 153 154static int 155usbvision_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num) 156{ 157 struct i2c_msg *pmsg; 158 struct usb_usbvision *usbvision; 159 int i, ret; 160 unsigned char addr = 0; 161 162 usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap); 163 164 for (i = 0; i < num; i++) { 165 pmsg = &msgs[i]; 166 ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr); 167 if (ret != 0) { 168 PDEBUG(DBG_I2C,"got NAK from device, message #%d", i); 169 return (ret < 0) ? ret : -EREMOTEIO; 170 } 171 172 if (pmsg->flags & I2C_M_RD) { 173 /* read bytes into buffer */ 174 ret = (usbvision_i2c_read(usbvision, addr, pmsg->buf, pmsg->len)); 175 if (ret < pmsg->len) { 176 return (ret < 0) ? ret : -EREMOTEIO; 177 } 178 } else { 179 /* write bytes from buffer */ 180 ret = (usbvision_i2c_write(usbvision, addr, pmsg->buf, pmsg->len)); 181 if (ret < pmsg->len) { 182 return (ret < 0) ? ret : -EREMOTEIO; 183 } 184 } 185 } 186 return num; 187} 188 189static u32 functionality(struct i2c_adapter *adap) 190{ 191 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR; 192} 193 194/* -----exported algorithm data: ------------------------------------- */ 195 196static struct i2c_algorithm usbvision_algo = { 197 .master_xfer = usbvision_i2c_xfer, 198 .smbus_xfer = NULL, 199 .functionality = functionality, 200}; 201 202 203/* ----------------------------------------------------------------------- */ 204/* usbvision specific I2C functions */ 205/* ----------------------------------------------------------------------- */ 206static struct i2c_adapter i2c_adap_template; 207 208int usbvision_i2c_register(struct usb_usbvision *usbvision) 209{ 210 static unsigned short saa711x_addrs[] = { 211 0x4a >> 1, 0x48 >> 1, /* SAA7111, SAA7111A and SAA7113 */ 212 0x42 >> 1, 0x40 >> 1, /* SAA7114, SAA7115 and SAA7118 */ 213 I2C_CLIENT_END }; 214 215 memcpy(&usbvision->i2c_adap, &i2c_adap_template, 216 sizeof(struct i2c_adapter)); 217 218 sprintf(usbvision->i2c_adap.name, "%s-%d-%s", i2c_adap_template.name, 219 usbvision->dev->bus->busnum, usbvision->dev->devpath); 220 PDEBUG(DBG_I2C,"Adaptername: %s", usbvision->i2c_adap.name); 221 usbvision->i2c_adap.dev.parent = &usbvision->dev->dev; 222 223 i2c_set_adapdata(&usbvision->i2c_adap, &usbvision->v4l2_dev); 224 225 if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) { 226 printk(KERN_ERR "usbvision_register: can't write reg\n"); 227 return -EBUSY; 228 } 229 230 PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]"); 231 PDEBUG(DBG_I2C, "ALGO debugging is enabled [i2c]"); 232 233 /* register new adapter to i2c module... */ 234 235 usbvision->i2c_adap.algo = &usbvision_algo; 236 237 usbvision->i2c_adap.timeout = 100; /* default values, should */ 238 usbvision->i2c_adap.retries = 3; /* be replaced by defines */ 239 240 i2c_add_adapter(&usbvision->i2c_adap); 241 242 PDEBUG(DBG_I2C, "i2c bus for %s registered", usbvision->i2c_adap.name); 243 244 /* Request the load of the i2c modules we need */ 245 switch (usbvision_device_data[usbvision->DevModel].Codec) { 246 case CODEC_SAA7113: 247 case CODEC_SAA7111: 248 v4l2_i2c_new_subdev(&usbvision->v4l2_dev, 249 &usbvision->i2c_adap, "saa7115", 250 "saa7115_auto", 0, saa711x_addrs); 251 break; 252 } 253 if (usbvision_device_data[usbvision->DevModel].Tuner == 1) { 254 struct v4l2_subdev *sd; 255 enum v4l2_i2c_tuner_type type; 256 struct tuner_setup tun_setup; 257 258 sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev, 259 &usbvision->i2c_adap, "tuner", 260 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_DEMOD)); 261 /* depending on whether we found a demod or not, select 262 the tuner type. */ 263 type = sd ? ADDRS_TV_WITH_DEMOD : ADDRS_TV; 264 265 sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev, 266 &usbvision->i2c_adap, "tuner", 267 "tuner", 0, v4l2_i2c_tuner_addrs(type)); 268 269 if (usbvision->tuner_type != -1) { 270 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO; 271 tun_setup.type = usbvision->tuner_type; 272 tun_setup.addr = v4l2_i2c_subdev_addr(sd); 273 call_all(usbvision, tuner, s_type_addr, &tun_setup); 274 } 275 } 276 277 return 0; 278} 279 280int usbvision_i2c_unregister(struct usb_usbvision *usbvision) 281{ 282 283 i2c_del_adapter(&(usbvision->i2c_adap)); 284 285 PDEBUG(DBG_I2C,"i2c bus for %s unregistered", usbvision->i2c_adap.name); 286 287 return 0; 288} 289 290static int 291usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr, 292 char *buf, short len) 293{ 294 int rc, retries; 295 296 for (retries = 5;;) { 297 rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr); 298 if (rc < 0) 299 return rc; 300 301 /* Initiate byte read cycle */ 302 /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */ 303 /* d3 0=Wr 1=Rd */ 304 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 305 (len & 0x07) | 0x18); 306 if (rc < 0) 307 return rc; 308 309 /* Test for Busy and ACK */ 310 do { 311 /* USBVISION_SER_CONT -> d4 == 0 busy */ 312 rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT); 313 } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */ 314 if (rc < 0) 315 return rc; 316 317 /* USBVISION_SER_CONT -> d5 == 1 Not ack */ 318 if ((rc & 0x20) == 0) /* Ack? */ 319 break; 320 321 /* I2C abort */ 322 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00); 323 if (rc < 0) 324 return rc; 325 326 if (--retries < 0) 327 return -1; 328 } 329 330 switch (len) { 331 case 4: 332 buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4); 333 case 3: 334 buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3); 335 case 2: 336 buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2); 337 case 1: 338 buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1); 339 break; 340 default: 341 printk(KERN_ERR 342 "usbvision_i2c_read_max4: buffer length > 4\n"); 343 } 344 345 if (i2c_debug & DBG_I2C) { 346 int idx; 347 for (idx = 0; idx < len; idx++) { 348 PDEBUG(DBG_I2C,"read %x from address %x", (unsigned char)buf[idx], addr); 349 } 350 } 351 return len; 352} 353 354 355static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision, 356 unsigned char addr, const char *buf, 357 short len) 358{ 359 int rc, retries; 360 int i; 361 unsigned char value[6]; 362 unsigned char ser_cont; 363 364 ser_cont = (len & 0x07) | 0x10; 365 366 value[0] = addr; 367 value[1] = ser_cont; 368 for (i = 0; i < len; i++) 369 value[i + 2] = buf[i]; 370 371 for (retries = 5;;) { 372 rc = usb_control_msg(usbvision->dev, 373 usb_sndctrlpipe(usbvision->dev, 1), 374 USBVISION_OP_CODE, 375 USB_DIR_OUT | USB_TYPE_VENDOR | 376 USB_RECIP_ENDPOINT, 0, 377 (__u16) USBVISION_SER_ADRS, value, 378 len + 2, HZ); 379 380 if (rc < 0) 381 return rc; 382 383 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 384 (len & 0x07) | 0x10); 385 if (rc < 0) 386 return rc; 387 388 /* Test for Busy and ACK */ 389 do { 390 rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT); 391 } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */ 392 if (rc < 0) 393 return rc; 394 395 if ((rc & 0x20) == 0) /* Ack? */ 396 break; 397 398 /* I2C abort */ 399 usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00); 400 401 if (--retries < 0) 402 return -1; 403 404 } 405 406 if (i2c_debug & DBG_I2C) { 407 int idx; 408 for (idx = 0; idx < len; idx++) { 409 PDEBUG(DBG_I2C,"wrote %x at address %x", (unsigned char)buf[idx], addr); 410 } 411 } 412 return len; 413} 414 415static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf, 416 short len) 417{ 418 char *bufPtr = buf; 419 int retval; 420 int wrcount = 0; 421 int count; 422 int maxLen = 4; 423 424 while (len > 0) { 425 count = (len > maxLen) ? maxLen : len; 426 retval = usbvision_i2c_write_max4(usbvision, addr, bufPtr, count); 427 if (retval > 0) { 428 len -= count; 429 bufPtr += count; 430 wrcount += count; 431 } else 432 return (retval < 0) ? retval : -EFAULT; 433 } 434 return wrcount; 435} 436 437static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf, 438 short len) 439{ 440 char temp[4]; 441 int retval, i; 442 int rdcount = 0; 443 int count; 444 445 while (len > 0) { 446 count = (len > 3) ? 4 : len; 447 retval = usbvision_i2c_read_max4(usbvision, addr, temp, count); 448 if (retval > 0) { 449 for (i = 0; i < len; i++) 450 buf[rdcount + i] = temp[i]; 451 len -= count; 452 rdcount += count; 453 } else 454 return (retval < 0) ? retval : -EFAULT; 455 } 456 return rdcount; 457} 458 459static struct i2c_adapter i2c_adap_template = { 460 .owner = THIS_MODULE, 461 .name = "usbvision", 462}; 463 464/* 465 * Overrides for Emacs so that we follow Linus's tabbing style. 466 * --------------------------------------------------------------------------- 467 * Local variables: 468 * c-basic-offset: 8 469 * End: 470 */ 471