/*
- * I2C_ALGO_USB.C
+ * usbvision_i2c.c
* i2c algorithm for USB-I2C Bridges
*
- * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
+ * Copyright (c) 1999-2007 Joerg Heckenbach <joerg@heckenbach-aw.de>
* Dwaine Garden <dwainegarden@rogers.com>
*
* This module is part of usbvision driver project.
#include "usbvision.h"
#define DBG_I2C 1<<0
-#define DBG_ALGO 1<<1
static int i2c_debug = 0;
#define PDEBUG(level, fmt, args...) \
if (i2c_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
-static int usbvision_i2c_write(void *data, unsigned char addr, char *buf,
+static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
short len);
-static int usbvision_i2c_read(void *data, unsigned char addr, char *buf,
+static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
short len);
static inline int try_write_address(struct i2c_adapter *i2c_adap,
unsigned char addr, int retries)
{
- void *data;
+ struct usb_usbvision *usbvision;
int i, ret = -1;
char buf[4];
- data = i2c_get_adapdata(i2c_adap);
+ usbvision = i2c_get_adapdata(i2c_adap);
buf[0] = 0x00;
for (i = 0; i <= retries; i++) {
- ret = (usbvision_i2c_write(data, addr, buf, 1));
+ ret = (usbvision_i2c_write(usbvision, addr, buf, 1));
if (ret == 1)
break; /* success! */
udelay(5);
udelay(10);
}
if (i) {
- PDEBUG(DBG_ALGO,"Needed %d retries for address %#2x", i, addr);
- PDEBUG(DBG_ALGO,"Maybe there's no device at this address");
+ PDEBUG(DBG_I2C,"Needed %d retries for address %#2x", i, addr);
+ PDEBUG(DBG_I2C,"Maybe there's no device at this address");
}
return ret;
}
static inline int try_read_address(struct i2c_adapter *i2c_adap,
unsigned char addr, int retries)
{
- void *data;
+ struct usb_usbvision *usbvision;
int i, ret = -1;
char buf[4];
- data = i2c_get_adapdata(i2c_adap);
+ usbvision = i2c_get_adapdata(i2c_adap);
for (i = 0; i <= retries; i++) {
- ret = (usbvision_i2c_read(data, addr, buf, 1));
+ ret = (usbvision_i2c_read(usbvision, addr, buf, 1));
if (ret == 1)
break; /* success! */
udelay(5);
udelay(10);
}
if (i) {
- PDEBUG(DBG_ALGO,"Needed %d retries for address %#2x", i, addr);
- PDEBUG(DBG_ALGO,"Maybe there's no device at this address");
+ PDEBUG(DBG_I2C,"Needed %d retries for address %#2x", i, addr);
+ PDEBUG(DBG_I2C,"Maybe there's no device at this address");
}
return ret;
}
}
static int
-usb_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
+usbvision_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
{
struct i2c_msg *pmsg;
- void *data;
+ struct usb_usbvision *usbvision;
int i, ret;
unsigned char addr;
- data = i2c_get_adapdata(i2c_adap);
-
+ usbvision = i2c_get_adapdata(i2c_adap);
for (i = 0; i < num; i++) {
pmsg = &msgs[i];
ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr);
if (ret != 0) {
- PDEBUG(DBG_ALGO,"got NAK from device, message #%d", i);
+ PDEBUG(DBG_I2C,"got NAK from device, message #%d", i);
return (ret < 0) ? ret : -EREMOTEIO;
}
if (pmsg->flags & I2C_M_RD) {
/* read bytes into buffer */
- ret = (usbvision_i2c_read(data, addr, pmsg->buf, pmsg->len));
+ ret = (usbvision_i2c_read(usbvision, addr, pmsg->buf, pmsg->len));
if (ret < pmsg->len) {
return (ret < 0) ? ret : -EREMOTEIO;
}
} else {
/* write bytes from buffer */
- ret = (usbvision_i2c_write(data, addr, pmsg->buf, pmsg->len));
+ ret = (usbvision_i2c_write(usbvision, addr, pmsg->buf, pmsg->len));
if (ret < pmsg->len) {
return (ret < 0) ? ret : -EREMOTEIO;
}
return 0;
}
-static u32 usb_func(struct i2c_adapter *adap)
+static u32 functionality(struct i2c_adapter *adap)
{
return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
}
/* -----exported algorithm data: ------------------------------------- */
-static struct i2c_algorithm i2c_usb_algo = {
- .master_xfer = usb_xfer,
+static struct i2c_algorithm usbvision_algo = {
+ .master_xfer = usbvision_i2c_xfer,
.smbus_xfer = NULL,
.algo_control = algo_control,
- .functionality = usb_func,
+ .functionality = functionality,
};
-/*
- * registering functions to load algorithms at runtime
- */
-static int usbvision_i2c_usb_add_bus(struct i2c_adapter *adap)
-{
- PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]");
- PDEBUG(DBG_ALGO, "ALGO debugging is enabled [i2c]");
-
- /* register new adapter to i2c module... */
-
- adap->algo = &i2c_usb_algo;
-
- adap->timeout = 100; /* default values, should */
- adap->retries = 3; /* be replaced by defines */
-
- i2c_add_adapter(adap);
-
- PDEBUG(DBG_ALGO,"i2c bus for %s registered", adap->name);
-
- return 0;
-}
-
-
-int usbvision_i2c_usb_del_bus(struct i2c_adapter *adap)
-{
-
- i2c_del_adapter(adap);
-
- PDEBUG(DBG_ALGO,"i2c bus for %s unregistered", adap->name);
-
- return 0;
-}
-
-
/* ----------------------------------------------------------------------- */
/* usbvision specific I2C functions */
/* ----------------------------------------------------------------------- */
static struct i2c_adapter i2c_adap_template;
static struct i2c_client i2c_client_template;
-int usbvision_init_i2c(struct usb_usbvision *usbvision)
+int usbvision_i2c_register(struct usb_usbvision *usbvision)
{
- memcpy(&usbvision->i2c_adap, &i2c_adap_template,
- sizeof(struct i2c_adapter));
- memcpy(&usbvision->i2c_client, &i2c_client_template,
- sizeof(struct i2c_client));
+ int ret;
- sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name),
- " #%d", usbvision->vdev->minor & 0x1f);
- PDEBUG(DBG_I2C,"Adaptername: %s", usbvision->i2c_adap.name);
+ usbvision->i2c_adap = i2c_adap_template;
usbvision->i2c_adap.dev.parent = &usbvision->dev->dev;
- i2c_set_adapdata(&usbvision->i2c_adap, usbvision);
- i2c_set_clientdata(&usbvision->i2c_client, usbvision);
+ PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]");
+ sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name),
+ " #%d", usbvision->vdev->minor & 0x1f);
+ PDEBUG(DBG_I2C,"I2C Registering adaptername: %s", usbvision->i2c_adap.name);
+ i2c_set_adapdata(&usbvision->i2c_adap,usbvision);
+ if ((ret = i2c_add_adapter(&usbvision->i2c_adap)) < 0) {
+ PDEBUG(DBG_I2C,"could not add I2C adapter %s", usbvision->i2c_adap.name);
+ return ret;
+ }
+
+ /* TODO: use i2c_client for eeprom detection as an example... */
+ usbvision->i2c_client = i2c_client_template;
usbvision->i2c_client.adapter = &usbvision->i2c_adap;
if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) {
- printk(KERN_ERR "usbvision_init_i2c: can't write reg\n");
+ printk(KERN_ERR "usbvision_i2c_register: can't write reg\n");
return -EBUSY;
}
}
#endif
- return usbvision_i2c_usb_add_bus(&usbvision->i2c_adap);
+ return 0;
+}
+
+int usbvision_i2c_unregister(struct usb_usbvision *usbvision)
+{
+
+ i2c_del_adapter(&(usbvision->i2c_adap));
+
+ PDEBUG(DBG_I2C,"i2c bus for %s unregistered", usbvision->i2c_adap.name);
+
+ return 0;
}
void call_i2c_clients(struct usb_usbvision *usbvision, unsigned int cmd,
{
struct usb_usbvision *usbvision;
- usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
-
+ usbvision = i2c_get_adapdata(client->adapter);
switch (client->addr << 1) {
case 0x43:
case 0x4b:
{
struct usb_usbvision *usbvision;
- usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
+ usbvision = i2c_get_adapdata(client->adapter);
PDEBUG(DBG_I2C,"usbvision[%d] detaches %s", usbvision->nr, client->name);
return 0;
return len;
}
-static int usbvision_i2c_write(void *data, unsigned char addr, char *buf,
+static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
short len)
{
char *bufPtr = buf;
int wrcount = 0;
int count;
int maxLen = 4;
- struct usb_usbvision *usbvision = (struct usb_usbvision *) data;
while (len > 0) {
count = (len > maxLen) ? maxLen : len;
return wrcount;
}
-static int usbvision_i2c_read(void *data, unsigned char addr, char *buf,
+static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
short len)
{
char temp[4];
int retval, i;
int rdcount = 0;
int count;
- struct usb_usbvision *usbvision = (struct usb_usbvision *) data;
while (len > 0) {
count = (len > 3) ? 4 : len;
.owner = THIS_MODULE,
.name = "usbvision",
.id = I2C_HW_B_BT848, /* FIXME */
+ .algo = &usbvision_algo,
+ .algo_data = NULL,
.client_register = attach_inform,
.client_unregister = detach_inform,
#ifdef I2C_ADAP_CLASS_TV_ANALOG