1a10e763bSThomas 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 57243ec090SJussi Kivilinna pstatus = kmalloc(sizeof(*pstatus), GFP_KERNEL); 58243ec090SJussi Kivilinna if (!pstatus) 59243ec090SJussi Kivilinna return -ENOMEM; 60243ec090SJussi Kivilinna 61e8c76eedSTill Harbaum for (i = 0 ; i < num ; i++) { 62e8c76eedSTill Harbaum int cmd = CMD_I2C_IO; 63e8c76eedSTill Harbaum 64e8c76eedSTill Harbaum if (i == 0) 65e8c76eedSTill Harbaum cmd |= CMD_I2C_IO_BEGIN; 66e8c76eedSTill Harbaum 67e8c76eedSTill Harbaum if (i == num-1) 68e8c76eedSTill Harbaum cmd |= CMD_I2C_IO_END; 69e8c76eedSTill Harbaum 70e8c76eedSTill Harbaum pmsg = &msgs[i]; 71e8c76eedSTill Harbaum 72e8c76eedSTill Harbaum dev_dbg(&adapter->dev, 73e8c76eedSTill Harbaum " %d: %s (flags %d) %d bytes to 0x%02x\n", 74e8c76eedSTill Harbaum i, pmsg->flags & I2C_M_RD ? "read" : "write", 75e8c76eedSTill Harbaum pmsg->flags, pmsg->len, pmsg->addr); 76e8c76eedSTill Harbaum 77e8c76eedSTill Harbaum /* and directly send the message */ 78e8c76eedSTill Harbaum if (pmsg->flags & I2C_M_RD) { 79e8c76eedSTill Harbaum /* read data */ 80e8c76eedSTill Harbaum if (usb_read(adapter, cmd, 81e8c76eedSTill Harbaum pmsg->flags, pmsg->addr, 82e8c76eedSTill Harbaum pmsg->buf, pmsg->len) != pmsg->len) { 83e8c76eedSTill Harbaum dev_err(&adapter->dev, 84e8c76eedSTill Harbaum "failure reading data\n"); 85bc0757a5SChristoph Müllner ret = -EIO; 86243ec090SJussi Kivilinna goto out; 87e8c76eedSTill Harbaum } 88e8c76eedSTill Harbaum } else { 89e8c76eedSTill Harbaum /* write data */ 90e8c76eedSTill Harbaum if (usb_write(adapter, cmd, 91e8c76eedSTill Harbaum pmsg->flags, pmsg->addr, 92e8c76eedSTill Harbaum pmsg->buf, pmsg->len) != pmsg->len) { 93e8c76eedSTill Harbaum dev_err(&adapter->dev, 94e8c76eedSTill Harbaum "failure writing data\n"); 95bc0757a5SChristoph Müllner ret = -EIO; 96243ec090SJussi Kivilinna goto out; 97e8c76eedSTill Harbaum } 98e8c76eedSTill Harbaum } 99e8c76eedSTill Harbaum 100e8c76eedSTill Harbaum /* read status */ 101243ec090SJussi Kivilinna if (usb_read(adapter, CMD_GET_STATUS, 0, 0, pstatus, 1) != 1) { 102e8c76eedSTill Harbaum dev_err(&adapter->dev, "failure reading status\n"); 103bc0757a5SChristoph Müllner ret = -EIO; 104243ec090SJussi Kivilinna goto out; 105e8c76eedSTill Harbaum } 106e8c76eedSTill Harbaum 107243ec090SJussi Kivilinna dev_dbg(&adapter->dev, " status = %d\n", *pstatus); 108243ec090SJussi Kivilinna if (*pstatus == STATUS_ADDRESS_NAK) { 109bc0757a5SChristoph Müllner ret = -ENXIO; 110243ec090SJussi Kivilinna goto out; 111243ec090SJussi Kivilinna } 112e8c76eedSTill Harbaum } 113e8c76eedSTill Harbaum 114243ec090SJussi Kivilinna ret = i; 115243ec090SJussi Kivilinna out: 116243ec090SJussi Kivilinna kfree(pstatus); 117243ec090SJussi Kivilinna return ret; 118e8c76eedSTill Harbaum } 119e8c76eedSTill Harbaum 120e8c76eedSTill Harbaum static u32 usb_func(struct i2c_adapter *adapter) 121e8c76eedSTill Harbaum { 122243ec090SJussi Kivilinna __le32 *pfunc; 123243ec090SJussi Kivilinna u32 ret; 124243ec090SJussi Kivilinna 125243ec090SJussi Kivilinna pfunc = kmalloc(sizeof(*pfunc), GFP_KERNEL); 126e8c76eedSTill Harbaum 127e8c76eedSTill Harbaum /* get functionality from adapter */ 128243ec090SJussi Kivilinna if (!pfunc || usb_read(adapter, CMD_GET_FUNC, 0, 0, pfunc, 129243ec090SJussi Kivilinna sizeof(*pfunc)) != sizeof(*pfunc)) { 130e8c76eedSTill Harbaum dev_err(&adapter->dev, "failure reading functionality\n"); 131243ec090SJussi Kivilinna ret = 0; 132243ec090SJussi Kivilinna goto out; 133e8c76eedSTill Harbaum } 134e8c76eedSTill Harbaum 135243ec090SJussi Kivilinna ret = le32_to_cpup(pfunc); 136243ec090SJussi Kivilinna out: 137243ec090SJussi Kivilinna kfree(pfunc); 138243ec090SJussi Kivilinna return ret; 139e8c76eedSTill Harbaum } 140e8c76eedSTill Harbaum 141e8c76eedSTill Harbaum /* This is the actual algorithm we define */ 142e8c76eedSTill Harbaum static const struct i2c_algorithm usb_algorithm = { 143*664e69d2SWolfram Sang .xfer = usb_xfer, 144e8c76eedSTill Harbaum .functionality = usb_func, 145e8c76eedSTill Harbaum }; 146e8c76eedSTill Harbaum 147e8c76eedSTill Harbaum /* ----- end of i2c layer ------------------------------------------------ */ 148e8c76eedSTill Harbaum 149e8c76eedSTill Harbaum /* ----- begin of usb layer ---------------------------------------------- */ 150e8c76eedSTill Harbaum 151fa16eefdSTill Harbaum /* 152fa16eefdSTill Harbaum * Initially the usb i2c interface uses a vid/pid pair donated by 153fa16eefdSTill Harbaum * Future Technology Devices International Ltd., later a pair was 154fa16eefdSTill Harbaum * bought from EZPrototypes 155fa16eefdSTill Harbaum */ 1564111ecd2SMárton Németh static const struct usb_device_id i2c_tiny_usb_table[] = { 157fa16eefdSTill Harbaum { USB_DEVICE(0x0403, 0xc631) }, /* FTDI */ 158fa16eefdSTill Harbaum { USB_DEVICE(0x1c40, 0x0534) }, /* EZPrototypes */ 159e8c76eedSTill Harbaum { } /* Terminating entry */ 160e8c76eedSTill Harbaum }; 161e8c76eedSTill Harbaum 162e8c76eedSTill Harbaum MODULE_DEVICE_TABLE(usb, i2c_tiny_usb_table); 163e8c76eedSTill Harbaum 164e8c76eedSTill Harbaum /* Structure to hold all of our device specific stuff */ 165e8c76eedSTill Harbaum struct i2c_tiny_usb { 166e8c76eedSTill Harbaum struct usb_device *usb_dev; /* the usb device for this device */ 167e8c76eedSTill Harbaum struct usb_interface *interface; /* the interface for this device */ 168e8c76eedSTill Harbaum struct i2c_adapter adapter; /* i2c related things */ 169e8c76eedSTill Harbaum }; 170e8c76eedSTill Harbaum 171e8c76eedSTill Harbaum static int usb_read(struct i2c_adapter *adapter, int cmd, 172e8c76eedSTill Harbaum int value, int index, void *data, int len) 173e8c76eedSTill Harbaum { 174e8c76eedSTill Harbaum struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data; 1755165da59SSebastian Reichel void *dmadata = kmalloc(len, GFP_KERNEL); 1765165da59SSebastian Reichel int ret; 1775165da59SSebastian Reichel 1785165da59SSebastian Reichel if (!dmadata) 1795165da59SSebastian Reichel return -ENOMEM; 180e8c76eedSTill Harbaum 181e8c76eedSTill Harbaum /* do control transfer */ 1825165da59SSebastian Reichel ret = usb_control_msg(dev->usb_dev, usb_rcvctrlpipe(dev->usb_dev, 0), 183e8c76eedSTill Harbaum cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE | 1845165da59SSebastian Reichel USB_DIR_IN, value, index, dmadata, len, 2000); 1855165da59SSebastian Reichel 1865165da59SSebastian Reichel memcpy(data, dmadata, len); 1875165da59SSebastian Reichel kfree(dmadata); 1885165da59SSebastian Reichel return ret; 189e8c76eedSTill Harbaum } 190e8c76eedSTill Harbaum 191e8c76eedSTill Harbaum static int usb_write(struct i2c_adapter *adapter, int cmd, 192e8c76eedSTill Harbaum int value, int index, void *data, int len) 193e8c76eedSTill Harbaum { 194e8c76eedSTill Harbaum struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data; 1955165da59SSebastian Reichel void *dmadata = kmemdup(data, len, GFP_KERNEL); 1965165da59SSebastian Reichel int ret; 1975165da59SSebastian Reichel 1985165da59SSebastian Reichel if (!dmadata) 1995165da59SSebastian Reichel return -ENOMEM; 200e8c76eedSTill Harbaum 201e8c76eedSTill Harbaum /* do control transfer */ 2025165da59SSebastian Reichel ret = usb_control_msg(dev->usb_dev, usb_sndctrlpipe(dev->usb_dev, 0), 203e8c76eedSTill Harbaum cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 2045165da59SSebastian Reichel value, index, dmadata, len, 2000); 2055165da59SSebastian Reichel 2065165da59SSebastian Reichel kfree(dmadata); 2075165da59SSebastian Reichel return ret; 208e8c76eedSTill Harbaum } 209e8c76eedSTill Harbaum 210e8c76eedSTill Harbaum static void i2c_tiny_usb_free(struct i2c_tiny_usb *dev) 211e8c76eedSTill Harbaum { 212e8c76eedSTill Harbaum usb_put_dev(dev->usb_dev); 213e8c76eedSTill Harbaum kfree(dev); 214e8c76eedSTill Harbaum } 215e8c76eedSTill Harbaum 216e8c76eedSTill Harbaum static int i2c_tiny_usb_probe(struct usb_interface *interface, 217e8c76eedSTill Harbaum const struct usb_device_id *id) 218e8c76eedSTill Harbaum { 219e8c76eedSTill Harbaum struct i2c_tiny_usb *dev; 220e8c76eedSTill Harbaum int retval = -ENOMEM; 221e8c76eedSTill Harbaum u16 version; 222e8c76eedSTill Harbaum 2233253f692SHarry Geyer if (interface->intf_assoc && 2243253f692SHarry Geyer interface->intf_assoc->bFunctionClass != USB_CLASS_VENDOR_SPEC) 2253253f692SHarry Geyer return -ENODEV; 2263253f692SHarry Geyer 227e8c76eedSTill Harbaum dev_dbg(&interface->dev, "probing usb device\n"); 228e8c76eedSTill Harbaum 229e8c76eedSTill Harbaum /* allocate memory for our device state and initialize it */ 230e8c76eedSTill Harbaum dev = kzalloc(sizeof(*dev), GFP_KERNEL); 2316b3b21a8SMarkus Elfring if (!dev) 232e8c76eedSTill Harbaum goto error; 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