1*a10e763bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2e8c76eedSTill Harbaum /* 3e8c76eedSTill Harbaum * driver for the i2c-tiny-usb adapter - 1.0 4e8c76eedSTill Harbaum * http://www.harbaum.org/till/i2c_tiny_usb 5e8c76eedSTill Harbaum * 6e8c76eedSTill Harbaum * Copyright (C) 2006-2007 Till Harbaum (Till@Harbaum.org) 7e8c76eedSTill Harbaum */ 8e8c76eedSTill Harbaum 9e8c76eedSTill Harbaum #include <linux/kernel.h> 10e8c76eedSTill Harbaum #include <linux/errno.h> 11e8c76eedSTill Harbaum #include <linux/module.h> 125a0e3ad6STejun Heo #include <linux/slab.h> 131c010ff8SJean Delvare #include <linux/types.h> 14e8c76eedSTill Harbaum 15e8c76eedSTill Harbaum /* include interfaces to usb layer */ 16e8c76eedSTill Harbaum #include <linux/usb.h> 17e8c76eedSTill Harbaum 18e8c76eedSTill Harbaum /* include interface to i2c layer */ 19e8c76eedSTill Harbaum #include <linux/i2c.h> 20e8c76eedSTill Harbaum 21e8c76eedSTill Harbaum /* commands via USB, must match command ids in the firmware */ 22e8c76eedSTill Harbaum #define CMD_ECHO 0 23e8c76eedSTill Harbaum #define CMD_GET_FUNC 1 24e8c76eedSTill Harbaum #define CMD_SET_DELAY 2 25e8c76eedSTill Harbaum #define CMD_GET_STATUS 3 26e8c76eedSTill Harbaum 27e8c76eedSTill Harbaum #define CMD_I2C_IO 4 28e8c76eedSTill Harbaum #define CMD_I2C_IO_BEGIN (1<<0) 29e8c76eedSTill Harbaum #define CMD_I2C_IO_END (1<<1) 30e8c76eedSTill Harbaum 31c05d4902SJean Delvare /* i2c bit delay, default is 10us -> 100kHz max 32c05d4902SJean Delvare (in practice, due to additional delays in the i2c bitbanging 33c05d4902SJean Delvare code this results in a i2c clock of about 50kHz) */ 341c010ff8SJean Delvare static unsigned short delay = 10; 351c010ff8SJean Delvare module_param(delay, ushort, 0); 36c05d4902SJean Delvare MODULE_PARM_DESC(delay, "bit delay in microseconds " 37c05d4902SJean Delvare "(default is 10us for 100kHz max)"); 38e8c76eedSTill Harbaum 39e8c76eedSTill Harbaum static int usb_read(struct i2c_adapter *adapter, int cmd, 40e8c76eedSTill Harbaum int value, int index, void *data, int len); 41e8c76eedSTill Harbaum 42e8c76eedSTill Harbaum static int usb_write(struct i2c_adapter *adapter, int cmd, 43e8c76eedSTill Harbaum int value, int index, void *data, int len); 44e8c76eedSTill Harbaum 45e8c76eedSTill Harbaum /* ----- begin of i2c layer ---------------------------------------------- */ 46e8c76eedSTill Harbaum 47e8c76eedSTill Harbaum #define STATUS_IDLE 0 48e8c76eedSTill Harbaum #define STATUS_ADDRESS_ACK 1 49e8c76eedSTill Harbaum #define STATUS_ADDRESS_NAK 2 50e8c76eedSTill Harbaum 51e8c76eedSTill Harbaum static int usb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num) 52e8c76eedSTill Harbaum { 53243ec090SJussi Kivilinna unsigned char *pstatus; 54e8c76eedSTill Harbaum struct i2c_msg *pmsg; 55243ec090SJussi Kivilinna int i, ret; 56e8c76eedSTill Harbaum 57e8c76eedSTill Harbaum dev_dbg(&adapter->dev, "master xfer %d messages:\n", num); 58e8c76eedSTill Harbaum 59243ec090SJussi Kivilinna pstatus = kmalloc(sizeof(*pstatus), GFP_KERNEL); 60243ec090SJussi Kivilinna if (!pstatus) 61243ec090SJussi Kivilinna return -ENOMEM; 62243ec090SJussi Kivilinna 63e8c76eedSTill Harbaum for (i = 0 ; i < num ; i++) { 64e8c76eedSTill Harbaum int cmd = CMD_I2C_IO; 65e8c76eedSTill Harbaum 66e8c76eedSTill Harbaum if (i == 0) 67e8c76eedSTill Harbaum cmd |= CMD_I2C_IO_BEGIN; 68e8c76eedSTill Harbaum 69e8c76eedSTill Harbaum if (i == num-1) 70e8c76eedSTill Harbaum cmd |= CMD_I2C_IO_END; 71e8c76eedSTill Harbaum 72e8c76eedSTill Harbaum pmsg = &msgs[i]; 73e8c76eedSTill Harbaum 74e8c76eedSTill Harbaum dev_dbg(&adapter->dev, 75e8c76eedSTill Harbaum " %d: %s (flags %d) %d bytes to 0x%02x\n", 76e8c76eedSTill Harbaum i, pmsg->flags & I2C_M_RD ? "read" : "write", 77e8c76eedSTill Harbaum pmsg->flags, pmsg->len, pmsg->addr); 78e8c76eedSTill Harbaum 79e8c76eedSTill Harbaum /* and directly send the message */ 80e8c76eedSTill Harbaum if (pmsg->flags & I2C_M_RD) { 81e8c76eedSTill Harbaum /* read data */ 82e8c76eedSTill Harbaum if (usb_read(adapter, cmd, 83e8c76eedSTill Harbaum pmsg->flags, pmsg->addr, 84e8c76eedSTill Harbaum pmsg->buf, pmsg->len) != pmsg->len) { 85e8c76eedSTill Harbaum dev_err(&adapter->dev, 86e8c76eedSTill Harbaum "failure reading data\n"); 87243ec090SJussi Kivilinna ret = -EREMOTEIO; 88243ec090SJussi Kivilinna goto out; 89e8c76eedSTill Harbaum } 90e8c76eedSTill Harbaum } else { 91e8c76eedSTill Harbaum /* write data */ 92e8c76eedSTill Harbaum if (usb_write(adapter, cmd, 93e8c76eedSTill Harbaum pmsg->flags, pmsg->addr, 94e8c76eedSTill Harbaum pmsg->buf, pmsg->len) != pmsg->len) { 95e8c76eedSTill Harbaum dev_err(&adapter->dev, 96e8c76eedSTill Harbaum "failure writing data\n"); 97243ec090SJussi Kivilinna ret = -EREMOTEIO; 98243ec090SJussi Kivilinna goto out; 99e8c76eedSTill Harbaum } 100e8c76eedSTill Harbaum } 101e8c76eedSTill Harbaum 102e8c76eedSTill Harbaum /* read status */ 103243ec090SJussi Kivilinna if (usb_read(adapter, CMD_GET_STATUS, 0, 0, pstatus, 1) != 1) { 104e8c76eedSTill Harbaum dev_err(&adapter->dev, "failure reading status\n"); 105243ec090SJussi Kivilinna ret = -EREMOTEIO; 106243ec090SJussi Kivilinna goto out; 107e8c76eedSTill Harbaum } 108e8c76eedSTill Harbaum 109243ec090SJussi Kivilinna dev_dbg(&adapter->dev, " status = %d\n", *pstatus); 110243ec090SJussi Kivilinna if (*pstatus == STATUS_ADDRESS_NAK) { 111243ec090SJussi Kivilinna ret = -EREMOTEIO; 112243ec090SJussi Kivilinna goto out; 113243ec090SJussi Kivilinna } 114e8c76eedSTill Harbaum } 115e8c76eedSTill Harbaum 116243ec090SJussi Kivilinna ret = i; 117243ec090SJussi Kivilinna out: 118243ec090SJussi Kivilinna kfree(pstatus); 119243ec090SJussi Kivilinna return ret; 120e8c76eedSTill Harbaum } 121e8c76eedSTill Harbaum 122e8c76eedSTill Harbaum static u32 usb_func(struct i2c_adapter *adapter) 123e8c76eedSTill Harbaum { 124243ec090SJussi Kivilinna __le32 *pfunc; 125243ec090SJussi Kivilinna u32 ret; 126243ec090SJussi Kivilinna 127243ec090SJussi Kivilinna pfunc = kmalloc(sizeof(*pfunc), GFP_KERNEL); 128e8c76eedSTill Harbaum 129e8c76eedSTill Harbaum /* get functionality from adapter */ 130243ec090SJussi Kivilinna if (!pfunc || usb_read(adapter, CMD_GET_FUNC, 0, 0, pfunc, 131243ec090SJussi Kivilinna sizeof(*pfunc)) != sizeof(*pfunc)) { 132e8c76eedSTill Harbaum dev_err(&adapter->dev, "failure reading functionality\n"); 133243ec090SJussi Kivilinna ret = 0; 134243ec090SJussi Kivilinna goto out; 135e8c76eedSTill Harbaum } 136e8c76eedSTill Harbaum 137243ec090SJussi Kivilinna ret = le32_to_cpup(pfunc); 138243ec090SJussi Kivilinna out: 139243ec090SJussi Kivilinna kfree(pfunc); 140243ec090SJussi Kivilinna return ret; 141e8c76eedSTill Harbaum } 142e8c76eedSTill Harbaum 143e8c76eedSTill Harbaum /* This is the actual algorithm we define */ 144e8c76eedSTill Harbaum static const struct i2c_algorithm usb_algorithm = { 145e8c76eedSTill Harbaum .master_xfer = usb_xfer, 146e8c76eedSTill Harbaum .functionality = usb_func, 147e8c76eedSTill Harbaum }; 148e8c76eedSTill Harbaum 149e8c76eedSTill Harbaum /* ----- end of i2c layer ------------------------------------------------ */ 150e8c76eedSTill Harbaum 151e8c76eedSTill Harbaum /* ----- begin of usb layer ---------------------------------------------- */ 152e8c76eedSTill Harbaum 153fa16eefdSTill Harbaum /* 154fa16eefdSTill Harbaum * Initially the usb i2c interface uses a vid/pid pair donated by 155fa16eefdSTill Harbaum * Future Technology Devices International Ltd., later a pair was 156fa16eefdSTill Harbaum * bought from EZPrototypes 157fa16eefdSTill Harbaum */ 1584111ecd2SMárton Németh static const struct usb_device_id i2c_tiny_usb_table[] = { 159fa16eefdSTill Harbaum { USB_DEVICE(0x0403, 0xc631) }, /* FTDI */ 160fa16eefdSTill Harbaum { USB_DEVICE(0x1c40, 0x0534) }, /* EZPrototypes */ 161e8c76eedSTill Harbaum { } /* Terminating entry */ 162e8c76eedSTill Harbaum }; 163e8c76eedSTill Harbaum 164e8c76eedSTill Harbaum MODULE_DEVICE_TABLE(usb, i2c_tiny_usb_table); 165e8c76eedSTill Harbaum 166e8c76eedSTill Harbaum /* Structure to hold all of our device specific stuff */ 167e8c76eedSTill Harbaum struct i2c_tiny_usb { 168e8c76eedSTill Harbaum struct usb_device *usb_dev; /* the usb device for this device */ 169e8c76eedSTill Harbaum struct usb_interface *interface; /* the interface for this device */ 170e8c76eedSTill Harbaum struct i2c_adapter adapter; /* i2c related things */ 171e8c76eedSTill Harbaum }; 172e8c76eedSTill Harbaum 173e8c76eedSTill Harbaum static int usb_read(struct i2c_adapter *adapter, int cmd, 174e8c76eedSTill Harbaum int value, int index, void *data, int len) 175e8c76eedSTill Harbaum { 176e8c76eedSTill Harbaum struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data; 1775165da59SSebastian Reichel void *dmadata = kmalloc(len, GFP_KERNEL); 1785165da59SSebastian Reichel int ret; 1795165da59SSebastian Reichel 1805165da59SSebastian Reichel if (!dmadata) 1815165da59SSebastian Reichel return -ENOMEM; 182e8c76eedSTill Harbaum 183e8c76eedSTill Harbaum /* do control transfer */ 1845165da59SSebastian Reichel ret = usb_control_msg(dev->usb_dev, usb_rcvctrlpipe(dev->usb_dev, 0), 185e8c76eedSTill Harbaum cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE | 1865165da59SSebastian Reichel USB_DIR_IN, value, index, dmadata, len, 2000); 1875165da59SSebastian Reichel 1885165da59SSebastian Reichel memcpy(data, dmadata, len); 1895165da59SSebastian Reichel kfree(dmadata); 1905165da59SSebastian Reichel return ret; 191e8c76eedSTill Harbaum } 192e8c76eedSTill Harbaum 193e8c76eedSTill Harbaum static int usb_write(struct i2c_adapter *adapter, int cmd, 194e8c76eedSTill Harbaum int value, int index, void *data, int len) 195e8c76eedSTill Harbaum { 196e8c76eedSTill Harbaum struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data; 1975165da59SSebastian Reichel void *dmadata = kmemdup(data, len, GFP_KERNEL); 1985165da59SSebastian Reichel int ret; 1995165da59SSebastian Reichel 2005165da59SSebastian Reichel if (!dmadata) 2015165da59SSebastian Reichel return -ENOMEM; 202e8c76eedSTill Harbaum 203e8c76eedSTill Harbaum /* do control transfer */ 2045165da59SSebastian Reichel ret = usb_control_msg(dev->usb_dev, usb_sndctrlpipe(dev->usb_dev, 0), 205e8c76eedSTill Harbaum cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 2065165da59SSebastian Reichel value, index, dmadata, len, 2000); 2075165da59SSebastian Reichel 2085165da59SSebastian Reichel kfree(dmadata); 2095165da59SSebastian Reichel return ret; 210e8c76eedSTill Harbaum } 211e8c76eedSTill Harbaum 212e8c76eedSTill Harbaum static void i2c_tiny_usb_free(struct i2c_tiny_usb *dev) 213e8c76eedSTill Harbaum { 214e8c76eedSTill Harbaum usb_put_dev(dev->usb_dev); 215e8c76eedSTill Harbaum kfree(dev); 216e8c76eedSTill Harbaum } 217e8c76eedSTill Harbaum 218e8c76eedSTill Harbaum static int i2c_tiny_usb_probe(struct usb_interface *interface, 219e8c76eedSTill Harbaum const struct usb_device_id *id) 220e8c76eedSTill Harbaum { 221e8c76eedSTill Harbaum struct i2c_tiny_usb *dev; 222e8c76eedSTill Harbaum int retval = -ENOMEM; 223e8c76eedSTill Harbaum u16 version; 224e8c76eedSTill Harbaum 225e8c76eedSTill Harbaum dev_dbg(&interface->dev, "probing usb device\n"); 226e8c76eedSTill Harbaum 227e8c76eedSTill Harbaum /* allocate memory for our device state and initialize it */ 228e8c76eedSTill Harbaum dev = kzalloc(sizeof(*dev), GFP_KERNEL); 229e8c76eedSTill Harbaum if (dev == NULL) { 230e8c76eedSTill Harbaum dev_err(&interface->dev, "Out of memory\n"); 231e8c76eedSTill Harbaum goto error; 232e8c76eedSTill Harbaum } 233e8c76eedSTill Harbaum 234e8c76eedSTill Harbaum dev->usb_dev = usb_get_dev(interface_to_usbdev(interface)); 235e8c76eedSTill Harbaum dev->interface = interface; 236e8c76eedSTill Harbaum 237e8c76eedSTill Harbaum /* save our data pointer in this interface device */ 238e8c76eedSTill Harbaum usb_set_intfdata(interface, dev); 239e8c76eedSTill Harbaum 240e8c76eedSTill Harbaum version = le16_to_cpu(dev->usb_dev->descriptor.bcdDevice); 241e8c76eedSTill Harbaum dev_info(&interface->dev, 242e8c76eedSTill Harbaum "version %x.%02x found at bus %03d address %03d\n", 243e8c76eedSTill Harbaum version >> 8, version & 0xff, 244e8c76eedSTill Harbaum dev->usb_dev->bus->busnum, dev->usb_dev->devnum); 245e8c76eedSTill Harbaum 246e8c76eedSTill Harbaum /* setup i2c adapter description */ 247e8c76eedSTill Harbaum dev->adapter.owner = THIS_MODULE; 248e8c76eedSTill Harbaum dev->adapter.class = I2C_CLASS_HWMON; 249e8c76eedSTill Harbaum dev->adapter.algo = &usb_algorithm; 250e8c76eedSTill Harbaum dev->adapter.algo_data = dev; 2516a7ce82fSJean Delvare snprintf(dev->adapter.name, sizeof(dev->adapter.name), 252e8c76eedSTill Harbaum "i2c-tiny-usb at bus %03d device %03d", 253e8c76eedSTill Harbaum dev->usb_dev->bus->busnum, dev->usb_dev->devnum); 254e8c76eedSTill Harbaum 2551c010ff8SJean Delvare if (usb_write(&dev->adapter, CMD_SET_DELAY, delay, 0, NULL, 0) != 0) { 256e8c76eedSTill Harbaum dev_err(&dev->adapter.dev, 257e8c76eedSTill Harbaum "failure setting delay to %dus\n", delay); 258e8c76eedSTill Harbaum retval = -EIO; 259e8c76eedSTill Harbaum goto error; 260e8c76eedSTill Harbaum } 261e8c76eedSTill Harbaum 262e8c76eedSTill Harbaum dev->adapter.dev.parent = &dev->interface->dev; 263e8c76eedSTill Harbaum 264e8c76eedSTill Harbaum /* and finally attach to i2c layer */ 265e8c76eedSTill Harbaum i2c_add_adapter(&dev->adapter); 266e8c76eedSTill Harbaum 267e8c76eedSTill Harbaum /* inform user about successful attachment to i2c layer */ 268e8c76eedSTill Harbaum dev_info(&dev->adapter.dev, "connected i2c-tiny-usb device\n"); 269e8c76eedSTill Harbaum 270e8c76eedSTill Harbaum return 0; 271e8c76eedSTill Harbaum 272e8c76eedSTill Harbaum error: 273e8c76eedSTill Harbaum if (dev) 274e8c76eedSTill Harbaum i2c_tiny_usb_free(dev); 275e8c76eedSTill Harbaum 276e8c76eedSTill Harbaum return retval; 277e8c76eedSTill Harbaum } 278e8c76eedSTill Harbaum 279e8c76eedSTill Harbaum static void i2c_tiny_usb_disconnect(struct usb_interface *interface) 280e8c76eedSTill Harbaum { 281e8c76eedSTill Harbaum struct i2c_tiny_usb *dev = usb_get_intfdata(interface); 282e8c76eedSTill Harbaum 283e8c76eedSTill Harbaum i2c_del_adapter(&dev->adapter); 284e8c76eedSTill Harbaum usb_set_intfdata(interface, NULL); 285e8c76eedSTill Harbaum i2c_tiny_usb_free(dev); 286e8c76eedSTill Harbaum 287e8c76eedSTill Harbaum dev_dbg(&interface->dev, "disconnected\n"); 288e8c76eedSTill Harbaum } 289e8c76eedSTill Harbaum 290e8c76eedSTill Harbaum static struct usb_driver i2c_tiny_usb_driver = { 291e8c76eedSTill Harbaum .name = "i2c-tiny-usb", 292e8c76eedSTill Harbaum .probe = i2c_tiny_usb_probe, 293e8c76eedSTill Harbaum .disconnect = i2c_tiny_usb_disconnect, 294e8c76eedSTill Harbaum .id_table = i2c_tiny_usb_table, 295e8c76eedSTill Harbaum }; 296e8c76eedSTill Harbaum 297fe748483SGreg Kroah-Hartman module_usb_driver(i2c_tiny_usb_driver); 298e8c76eedSTill Harbaum 299e8c76eedSTill Harbaum /* ----- end of usb layer ------------------------------------------------ */ 300e8c76eedSTill Harbaum 301e8c76eedSTill Harbaum MODULE_AUTHOR("Till Harbaum <Till@Harbaum.org>"); 302e8c76eedSTill Harbaum MODULE_DESCRIPTION("i2c-tiny-usb driver v1.0"); 303e8c76eedSTill Harbaum MODULE_LICENSE("GPL"); 304