Path: blob/master/drivers/i2c/busses/i2c-diolan-u2c.c
15111 views
/*1* Driver for the Diolan u2c-12 USB-I2C adapter2*3* Copyright (c) 2010-2011 Ericsson AB4*5* Derived from:6* i2c-tiny-usb.c7* Copyright (C) 2006-2007 Till Harbaum ([email protected])8*9* This program is free software; you can redistribute it and/or10* modify it under the terms of the GNU General Public License as11* published by the Free Software Foundation, version 2.12*/1314#include <linux/kernel.h>15#include <linux/errno.h>16#include <linux/module.h>17#include <linux/types.h>18#include <linux/slab.h>19#include <linux/usb.h>20#include <linux/i2c.h>2122#define DRIVER_NAME "i2c-diolan-u2c"2324#define USB_VENDOR_ID_DIOLAN 0x0abf25#define USB_DEVICE_ID_DIOLAN_U2C 0x33702627#define DIOLAN_OUT_EP 0x0228#define DIOLAN_IN_EP 0x842930/* commands via USB, must match command ids in the firmware */31#define CMD_I2C_READ 0x0132#define CMD_I2C_WRITE 0x0233#define CMD_I2C_SCAN 0x03 /* Returns list of detected devices */34#define CMD_I2C_RELEASE_SDA 0x0435#define CMD_I2C_RELEASE_SCL 0x0536#define CMD_I2C_DROP_SDA 0x0637#define CMD_I2C_DROP_SCL 0x0738#define CMD_I2C_READ_SDA 0x0839#define CMD_I2C_READ_SCL 0x0940#define CMD_GET_FW_VERSION 0x0a41#define CMD_GET_SERIAL 0x0b42#define CMD_I2C_START 0x0c43#define CMD_I2C_STOP 0x0d44#define CMD_I2C_REPEATED_START 0x0e45#define CMD_I2C_PUT_BYTE 0x0f46#define CMD_I2C_GET_BYTE 0x1047#define CMD_I2C_PUT_ACK 0x1148#define CMD_I2C_GET_ACK 0x1249#define CMD_I2C_PUT_BYTE_ACK 0x1350#define CMD_I2C_GET_BYTE_ACK 0x1451#define CMD_I2C_SET_SPEED 0x1b52#define CMD_I2C_GET_SPEED 0x1c53#define CMD_I2C_SET_CLK_SYNC 0x2454#define CMD_I2C_GET_CLK_SYNC 0x2555#define CMD_I2C_SET_CLK_SYNC_TO 0x2656#define CMD_I2C_GET_CLK_SYNC_TO 0x275758#define RESP_OK 0x0059#define RESP_FAILED 0x0160#define RESP_BAD_MEMADDR 0x0461#define RESP_DATA_ERR 0x0562#define RESP_NOT_IMPLEMENTED 0x0663#define RESP_NACK 0x0764#define RESP_TIMEOUT 0x096566#define U2C_I2C_SPEED_FAST 0 /* 400 kHz */67#define U2C_I2C_SPEED_STD 1 /* 100 kHz */68#define U2C_I2C_SPEED_2KHZ 242 /* 2 kHz, minimum speed */69#define U2C_I2C_SPEED(f) ((DIV_ROUND_UP(1000000, (f)) - 10) / 2 + 1)7071#define U2C_I2C_FREQ_FAST 40000072#define U2C_I2C_FREQ_STD 10000073#define U2C_I2C_FREQ(s) (1000000 / (2 * (s - 1) + 10))7475#define DIOLAN_USB_TIMEOUT 100 /* in ms */76#define DIOLAN_SYNC_TIMEOUT 20 /* in ms */7778#define DIOLAN_OUTBUF_LEN 12879#define DIOLAN_FLUSH_LEN (DIOLAN_OUTBUF_LEN - 4)80#define DIOLAN_INBUF_LEN 256 /* Maximum supported receive length */8182/* Structure to hold all of our device specific stuff */83struct i2c_diolan_u2c {84u8 obuffer[DIOLAN_OUTBUF_LEN]; /* output buffer */85u8 ibuffer[DIOLAN_INBUF_LEN]; /* input buffer */86struct usb_device *usb_dev; /* the usb device for this device */87struct usb_interface *interface;/* the interface for this device */88struct i2c_adapter adapter; /* i2c related things */89int olen; /* Output buffer length */90int ocount; /* Number of enqueued messages */91};9293static uint frequency = U2C_I2C_FREQ_STD; /* I2C clock frequency in Hz */9495module_param(frequency, uint, S_IRUGO | S_IWUSR);96MODULE_PARM_DESC(frequency, "I2C clock frequency in hertz");9798/* usb layer */99100/* Send command to device, and get response. */101static int diolan_usb_transfer(struct i2c_diolan_u2c *dev)102{103int ret = 0;104int actual;105int i;106107if (!dev->olen || !dev->ocount)108return -EINVAL;109110ret = usb_bulk_msg(dev->usb_dev,111usb_sndbulkpipe(dev->usb_dev, DIOLAN_OUT_EP),112dev->obuffer, dev->olen, &actual,113DIOLAN_USB_TIMEOUT);114if (!ret) {115for (i = 0; i < dev->ocount; i++) {116int tmpret;117118tmpret = usb_bulk_msg(dev->usb_dev,119usb_rcvbulkpipe(dev->usb_dev,120DIOLAN_IN_EP),121dev->ibuffer,122sizeof(dev->ibuffer), &actual,123DIOLAN_USB_TIMEOUT);124/*125* Stop command processing if a previous command126* returned an error.127* Note that we still need to retrieve all messages.128*/129if (ret < 0)130continue;131ret = tmpret;132if (ret == 0 && actual > 0) {133switch (dev->ibuffer[actual - 1]) {134case RESP_NACK:135/*136* Return ENXIO if NACK was received as137* response to the address phase,138* EIO otherwise139*/140ret = i == 1 ? -ENXIO : -EIO;141break;142case RESP_TIMEOUT:143ret = -ETIMEDOUT;144break;145case RESP_OK:146/* strip off return code */147ret = actual - 1;148break;149default:150ret = -EIO;151break;152}153}154}155}156dev->olen = 0;157dev->ocount = 0;158return ret;159}160161static int diolan_write_cmd(struct i2c_diolan_u2c *dev, bool flush)162{163if (flush || dev->olen >= DIOLAN_FLUSH_LEN)164return diolan_usb_transfer(dev);165return 0;166}167168/* Send command (no data) */169static int diolan_usb_cmd(struct i2c_diolan_u2c *dev, u8 command, bool flush)170{171dev->obuffer[dev->olen++] = command;172dev->ocount++;173return diolan_write_cmd(dev, flush);174}175176/* Send command with one byte of data */177static int diolan_usb_cmd_data(struct i2c_diolan_u2c *dev, u8 command, u8 data,178bool flush)179{180dev->obuffer[dev->olen++] = command;181dev->obuffer[dev->olen++] = data;182dev->ocount++;183return diolan_write_cmd(dev, flush);184}185186/* Send command with two bytes of data */187static int diolan_usb_cmd_data2(struct i2c_diolan_u2c *dev, u8 command, u8 d1,188u8 d2, bool flush)189{190dev->obuffer[dev->olen++] = command;191dev->obuffer[dev->olen++] = d1;192dev->obuffer[dev->olen++] = d2;193dev->ocount++;194return diolan_write_cmd(dev, flush);195}196197/*198* Flush input queue.199* If we don't do this at startup and the controller has queued up200* messages which were not retrieved, it will stop responding201* at some point.202*/203static void diolan_flush_input(struct i2c_diolan_u2c *dev)204{205int i;206207for (i = 0; i < 10; i++) {208int actual = 0;209int ret;210211ret = usb_bulk_msg(dev->usb_dev,212usb_rcvbulkpipe(dev->usb_dev, DIOLAN_IN_EP),213dev->ibuffer, sizeof(dev->ibuffer), &actual,214DIOLAN_USB_TIMEOUT);215if (ret < 0 || actual == 0)216break;217}218if (i == 10)219dev_err(&dev->interface->dev, "Failed to flush input buffer\n");220}221222static int diolan_i2c_start(struct i2c_diolan_u2c *dev)223{224return diolan_usb_cmd(dev, CMD_I2C_START, false);225}226227static int diolan_i2c_repeated_start(struct i2c_diolan_u2c *dev)228{229return diolan_usb_cmd(dev, CMD_I2C_REPEATED_START, false);230}231232static int diolan_i2c_stop(struct i2c_diolan_u2c *dev)233{234return diolan_usb_cmd(dev, CMD_I2C_STOP, true);235}236237static int diolan_i2c_get_byte_ack(struct i2c_diolan_u2c *dev, bool ack,238u8 *byte)239{240int ret;241242ret = diolan_usb_cmd_data(dev, CMD_I2C_GET_BYTE_ACK, ack, true);243if (ret > 0)244*byte = dev->ibuffer[0];245else if (ret == 0)246ret = -EIO;247248return ret;249}250251static int diolan_i2c_put_byte_ack(struct i2c_diolan_u2c *dev, u8 byte)252{253return diolan_usb_cmd_data(dev, CMD_I2C_PUT_BYTE_ACK, byte, false);254}255256static int diolan_set_speed(struct i2c_diolan_u2c *dev, u8 speed)257{258return diolan_usb_cmd_data(dev, CMD_I2C_SET_SPEED, speed, true);259}260261/* Enable or disable clock synchronization (stretching) */262static int diolan_set_clock_synch(struct i2c_diolan_u2c *dev, bool enable)263{264return diolan_usb_cmd_data(dev, CMD_I2C_SET_CLK_SYNC, enable, true);265}266267/* Set clock synchronization timeout in ms */268static int diolan_set_clock_synch_timeout(struct i2c_diolan_u2c *dev, int ms)269{270int to_val = ms * 10;271272return diolan_usb_cmd_data2(dev, CMD_I2C_SET_CLK_SYNC_TO,273to_val & 0xff, (to_val >> 8) & 0xff, true);274}275276static void diolan_fw_version(struct i2c_diolan_u2c *dev)277{278int ret;279280ret = diolan_usb_cmd(dev, CMD_GET_FW_VERSION, true);281if (ret >= 2)282dev_info(&dev->interface->dev,283"Diolan U2C firmware version %u.%u\n",284(unsigned int)dev->ibuffer[0],285(unsigned int)dev->ibuffer[1]);286}287288static void diolan_get_serial(struct i2c_diolan_u2c *dev)289{290int ret;291u32 serial;292293ret = diolan_usb_cmd(dev, CMD_GET_SERIAL, true);294if (ret >= 4) {295serial = le32_to_cpu(*(u32 *)dev->ibuffer);296dev_info(&dev->interface->dev,297"Diolan U2C serial number %u\n", serial);298}299}300301static int diolan_init(struct i2c_diolan_u2c *dev)302{303int speed, ret;304305if (frequency >= 200000) {306speed = U2C_I2C_SPEED_FAST;307frequency = U2C_I2C_FREQ_FAST;308} else if (frequency >= 100000 || frequency == 0) {309speed = U2C_I2C_SPEED_STD;310frequency = U2C_I2C_FREQ_STD;311} else {312speed = U2C_I2C_SPEED(frequency);313if (speed > U2C_I2C_SPEED_2KHZ)314speed = U2C_I2C_SPEED_2KHZ;315frequency = U2C_I2C_FREQ(speed);316}317318dev_info(&dev->interface->dev,319"Diolan U2C at USB bus %03d address %03d speed %d Hz\n",320dev->usb_dev->bus->busnum, dev->usb_dev->devnum, frequency);321322diolan_flush_input(dev);323diolan_fw_version(dev);324diolan_get_serial(dev);325326/* Set I2C speed */327ret = diolan_set_speed(dev, speed);328if (ret < 0)329return ret;330331/* Configure I2C clock synchronization */332ret = diolan_set_clock_synch(dev, speed != U2C_I2C_SPEED_FAST);333if (ret < 0)334return ret;335336if (speed != U2C_I2C_SPEED_FAST)337ret = diolan_set_clock_synch_timeout(dev, DIOLAN_SYNC_TIMEOUT);338339return ret;340}341342/* i2c layer */343344static int diolan_usb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,345int num)346{347struct i2c_diolan_u2c *dev = i2c_get_adapdata(adapter);348struct i2c_msg *pmsg;349int i, j;350int ret, sret;351352ret = diolan_i2c_start(dev);353if (ret < 0)354return ret;355356for (i = 0; i < num; i++) {357pmsg = &msgs[i];358if (i) {359ret = diolan_i2c_repeated_start(dev);360if (ret < 0)361goto abort;362}363if (pmsg->flags & I2C_M_RD) {364ret =365diolan_i2c_put_byte_ack(dev, (pmsg->addr << 1) | 1);366if (ret < 0)367goto abort;368for (j = 0; j < pmsg->len; j++) {369u8 byte;370bool ack = j < pmsg->len - 1;371372/*373* Don't send NACK if this is the first byte374* of a SMBUS_BLOCK message.375*/376if (j == 0 && (pmsg->flags & I2C_M_RECV_LEN))377ack = true;378379ret = diolan_i2c_get_byte_ack(dev, ack, &byte);380if (ret < 0)381goto abort;382/*383* Adjust count if first received byte is length384*/385if (j == 0 && (pmsg->flags & I2C_M_RECV_LEN)) {386if (byte == 0387|| byte > I2C_SMBUS_BLOCK_MAX) {388ret = -EPROTO;389goto abort;390}391pmsg->len += byte;392}393pmsg->buf[j] = byte;394}395} else {396ret = diolan_i2c_put_byte_ack(dev, pmsg->addr << 1);397if (ret < 0)398goto abort;399for (j = 0; j < pmsg->len; j++) {400ret = diolan_i2c_put_byte_ack(dev,401pmsg->buf[j]);402if (ret < 0)403goto abort;404}405}406}407abort:408sret = diolan_i2c_stop(dev);409if (sret < 0 && ret >= 0)410ret = sret;411return ret;412}413414/*415* Return list of supported functionality.416*/417static u32 diolan_usb_func(struct i2c_adapter *a)418{419return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |420I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_FUNC_SMBUS_BLOCK_PROC_CALL;421}422423static const struct i2c_algorithm diolan_usb_algorithm = {424.master_xfer = diolan_usb_xfer,425.functionality = diolan_usb_func,426};427428/* device layer */429430static const struct usb_device_id diolan_u2c_table[] = {431{ USB_DEVICE(USB_VENDOR_ID_DIOLAN, USB_DEVICE_ID_DIOLAN_U2C) },432{ }433};434435MODULE_DEVICE_TABLE(usb, diolan_u2c_table);436437static void diolan_u2c_free(struct i2c_diolan_u2c *dev)438{439usb_put_dev(dev->usb_dev);440kfree(dev);441}442443static int diolan_u2c_probe(struct usb_interface *interface,444const struct usb_device_id *id)445{446struct i2c_diolan_u2c *dev;447int ret;448449/* allocate memory for our device state and initialize it */450dev = kzalloc(sizeof(*dev), GFP_KERNEL);451if (dev == NULL) {452dev_err(&interface->dev, "no memory for device state\n");453ret = -ENOMEM;454goto error;455}456457dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));458dev->interface = interface;459460/* save our data pointer in this interface device */461usb_set_intfdata(interface, dev);462463/* setup i2c adapter description */464dev->adapter.owner = THIS_MODULE;465dev->adapter.class = I2C_CLASS_HWMON;466dev->adapter.algo = &diolan_usb_algorithm;467i2c_set_adapdata(&dev->adapter, dev);468snprintf(dev->adapter.name, sizeof(dev->adapter.name),469DRIVER_NAME " at bus %03d device %03d",470dev->usb_dev->bus->busnum, dev->usb_dev->devnum);471472dev->adapter.dev.parent = &dev->interface->dev;473474/* initialize diolan i2c interface */475ret = diolan_init(dev);476if (ret < 0) {477dev_err(&interface->dev, "failed to initialize adapter\n");478goto error_free;479}480481/* and finally attach to i2c layer */482ret = i2c_add_adapter(&dev->adapter);483if (ret < 0) {484dev_err(&interface->dev, "failed to add I2C adapter\n");485goto error_free;486}487488dev_dbg(&interface->dev, "connected " DRIVER_NAME "\n");489490return 0;491492error_free:493usb_set_intfdata(interface, NULL);494diolan_u2c_free(dev);495error:496return ret;497}498499static void diolan_u2c_disconnect(struct usb_interface *interface)500{501struct i2c_diolan_u2c *dev = usb_get_intfdata(interface);502503i2c_del_adapter(&dev->adapter);504usb_set_intfdata(interface, NULL);505diolan_u2c_free(dev);506507dev_dbg(&interface->dev, "disconnected\n");508}509510static struct usb_driver diolan_u2c_driver = {511.name = DRIVER_NAME,512.probe = diolan_u2c_probe,513.disconnect = diolan_u2c_disconnect,514.id_table = diolan_u2c_table,515};516517static int __init diolan_u2c_init(void)518{519/* register this driver with the USB subsystem */520return usb_register(&diolan_u2c_driver);521}522523static void __exit diolan_u2c_exit(void)524{525/* deregister this driver with the USB subsystem */526usb_deregister(&diolan_u2c_driver);527}528529module_init(diolan_u2c_init);530module_exit(diolan_u2c_exit);531532MODULE_AUTHOR("Guenter Roeck <[email protected]>");533MODULE_DESCRIPTION(DRIVER_NAME " driver");534MODULE_LICENSE("GPL");535536537