init from v2.6.32.60
[mach-moxart.git] / drivers / i2c / busses / i2c-tiny-usb.c
blobe29b6d5ba8efb401d64e024329d36f8a053470bd
1 /*
2 * driver for the i2c-tiny-usb adapter - 1.0
3 * http://www.harbaum.org/till/i2c_tiny_usb
5 * Copyright (C) 2006-2007 Till Harbaum (Till@Harbaum.org)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, version 2.
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/types.h>
18 /* include interfaces to usb layer */
19 #include <linux/usb.h>
21 /* include interface to i2c layer */
22 #include <linux/i2c.h>
24 /* commands via USB, must match command ids in the firmware */
25 #define CMD_ECHO 0
26 #define CMD_GET_FUNC 1
27 #define CMD_SET_DELAY 2
28 #define CMD_GET_STATUS 3
30 #define CMD_I2C_IO 4
31 #define CMD_I2C_IO_BEGIN (1<<0)
32 #define CMD_I2C_IO_END (1<<1)
34 /* i2c bit delay, default is 10us -> 100kHz */
35 static unsigned short delay = 10;
36 module_param(delay, ushort, 0);
37 MODULE_PARM_DESC(delay, "bit delay in microseconds, "
38 "e.g. 10 for 100kHz (default is 100kHz)");
40 static int usb_read(struct i2c_adapter *adapter, int cmd,
41 int value, int index, void *data, int len);
43 static int usb_write(struct i2c_adapter *adapter, int cmd,
44 int value, int index, void *data, int len);
46 /* ----- begin of i2c layer ---------------------------------------------- */
48 #define STATUS_IDLE 0
49 #define STATUS_ADDRESS_ACK 1
50 #define STATUS_ADDRESS_NAK 2
52 static int usb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
54 unsigned char status;
55 struct i2c_msg *pmsg;
56 int i;
58 dev_dbg(&adapter->dev, "master xfer %d messages:\n", num);
60 for (i = 0 ; i < num ; i++) {
61 int cmd = CMD_I2C_IO;
63 if (i == 0)
64 cmd |= CMD_I2C_IO_BEGIN;
66 if (i == num-1)
67 cmd |= CMD_I2C_IO_END;
69 pmsg = &msgs[i];
71 dev_dbg(&adapter->dev,
72 " %d: %s (flags %d) %d bytes to 0x%02x\n",
73 i, pmsg->flags & I2C_M_RD ? "read" : "write",
74 pmsg->flags, pmsg->len, pmsg->addr);
76 /* and directly send the message */
77 if (pmsg->flags & I2C_M_RD) {
78 /* read data */
79 if (usb_read(adapter, cmd,
80 pmsg->flags, pmsg->addr,
81 pmsg->buf, pmsg->len) != pmsg->len) {
82 dev_err(&adapter->dev,
83 "failure reading data\n");
84 return -EREMOTEIO;
86 } else {
87 /* write data */
88 if (usb_write(adapter, cmd,
89 pmsg->flags, pmsg->addr,
90 pmsg->buf, pmsg->len) != pmsg->len) {
91 dev_err(&adapter->dev,
92 "failure writing data\n");
93 return -EREMOTEIO;
97 /* read status */
98 if (usb_read(adapter, CMD_GET_STATUS, 0, 0, &status, 1) != 1) {
99 dev_err(&adapter->dev, "failure reading status\n");
100 return -EREMOTEIO;
103 dev_dbg(&adapter->dev, " status = %d\n", status);
104 if (status == STATUS_ADDRESS_NAK)
105 return -EREMOTEIO;
108 return i;
111 static u32 usb_func(struct i2c_adapter *adapter)
113 __le32 func;
115 /* get functionality from adapter */
116 if (usb_read(adapter, CMD_GET_FUNC, 0, 0, &func, sizeof(func)) !=
117 sizeof(func)) {
118 dev_err(&adapter->dev, "failure reading functionality\n");
119 return 0;
122 return le32_to_cpu(func);
125 /* This is the actual algorithm we define */
126 static const struct i2c_algorithm usb_algorithm = {
127 .master_xfer = usb_xfer,
128 .functionality = usb_func,
131 /* ----- end of i2c layer ------------------------------------------------ */
133 /* ----- begin of usb layer ---------------------------------------------- */
136 * Initially the usb i2c interface uses a vid/pid pair donated by
137 * Future Technology Devices International Ltd., later a pair was
138 * bought from EZPrototypes
140 static struct usb_device_id i2c_tiny_usb_table [] = {
141 { USB_DEVICE(0x0403, 0xc631) }, /* FTDI */
142 { USB_DEVICE(0x1c40, 0x0534) }, /* EZPrototypes */
143 { } /* Terminating entry */
146 MODULE_DEVICE_TABLE(usb, i2c_tiny_usb_table);
148 /* Structure to hold all of our device specific stuff */
149 struct i2c_tiny_usb {
150 struct usb_device *usb_dev; /* the usb device for this device */
151 struct usb_interface *interface; /* the interface for this device */
152 struct i2c_adapter adapter; /* i2c related things */
155 static int usb_read(struct i2c_adapter *adapter, int cmd,
156 int value, int index, void *data, int len)
158 struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
160 /* do control transfer */
161 return usb_control_msg(dev->usb_dev, usb_rcvctrlpipe(dev->usb_dev, 0),
162 cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE |
163 USB_DIR_IN, value, index, data, len, 2000);
166 static int usb_write(struct i2c_adapter *adapter, int cmd,
167 int value, int index, void *data, int len)
169 struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
171 /* do control transfer */
172 return usb_control_msg(dev->usb_dev, usb_sndctrlpipe(dev->usb_dev, 0),
173 cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
174 value, index, data, len, 2000);
177 static void i2c_tiny_usb_free(struct i2c_tiny_usb *dev)
179 usb_put_dev(dev->usb_dev);
180 kfree(dev);
183 static int i2c_tiny_usb_probe(struct usb_interface *interface,
184 const struct usb_device_id *id)
186 struct i2c_tiny_usb *dev;
187 int retval = -ENOMEM;
188 u16 version;
190 dev_dbg(&interface->dev, "probing usb device\n");
192 /* allocate memory for our device state and initialize it */
193 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
194 if (dev == NULL) {
195 dev_err(&interface->dev, "Out of memory\n");
196 goto error;
199 dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));
200 dev->interface = interface;
202 /* save our data pointer in this interface device */
203 usb_set_intfdata(interface, dev);
205 version = le16_to_cpu(dev->usb_dev->descriptor.bcdDevice);
206 dev_info(&interface->dev,
207 "version %x.%02x found at bus %03d address %03d\n",
208 version >> 8, version & 0xff,
209 dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
211 /* setup i2c adapter description */
212 dev->adapter.owner = THIS_MODULE;
213 dev->adapter.class = I2C_CLASS_HWMON;
214 dev->adapter.algo = &usb_algorithm;
215 dev->adapter.algo_data = dev;
216 snprintf(dev->adapter.name, sizeof(dev->adapter.name),
217 "i2c-tiny-usb at bus %03d device %03d",
218 dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
220 if (usb_write(&dev->adapter, CMD_SET_DELAY, delay, 0, NULL, 0) != 0) {
221 dev_err(&dev->adapter.dev,
222 "failure setting delay to %dus\n", delay);
223 retval = -EIO;
224 goto error;
227 dev->adapter.dev.parent = &dev->interface->dev;
229 /* and finally attach to i2c layer */
230 i2c_add_adapter(&dev->adapter);
232 /* inform user about successful attachment to i2c layer */
233 dev_info(&dev->adapter.dev, "connected i2c-tiny-usb device\n");
235 return 0;
237 error:
238 if (dev)
239 i2c_tiny_usb_free(dev);
241 return retval;
244 static void i2c_tiny_usb_disconnect(struct usb_interface *interface)
246 struct i2c_tiny_usb *dev = usb_get_intfdata(interface);
248 i2c_del_adapter(&dev->adapter);
249 usb_set_intfdata(interface, NULL);
250 i2c_tiny_usb_free(dev);
252 dev_dbg(&interface->dev, "disconnected\n");
255 static struct usb_driver i2c_tiny_usb_driver = {
256 .name = "i2c-tiny-usb",
257 .probe = i2c_tiny_usb_probe,
258 .disconnect = i2c_tiny_usb_disconnect,
259 .id_table = i2c_tiny_usb_table,
262 static int __init usb_i2c_tiny_usb_init(void)
264 /* register this driver with the USB subsystem */
265 return usb_register(&i2c_tiny_usb_driver);
268 static void __exit usb_i2c_tiny_usb_exit(void)
270 /* deregister this driver with the USB subsystem */
271 usb_deregister(&i2c_tiny_usb_driver);
274 module_init(usb_i2c_tiny_usb_init);
275 module_exit(usb_i2c_tiny_usb_exit);
277 /* ----- end of usb layer ------------------------------------------------ */
279 MODULE_AUTHOR("Till Harbaum <Till@Harbaum.org>");
280 MODULE_DESCRIPTION("i2c-tiny-usb driver v1.0");
281 MODULE_LICENSE("GPL");