blob: b5b1bbf37d3c5bafc2979860c8ce33d7a4aa69bf [file] [log] [blame]
Till Harbaume8c76ee2007-05-01 23:26:35 +02001/*
2 * driver for the i2c-tiny-usb adapter - 1.0
3 * http://www.harbaum.org/till/i2c_tiny_usb
4 *
5 * Copyright (C) 2006-2007 Till Harbaum (Till@Harbaum.org)
6 *
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.
10 *
11 */
12
13#include <linux/kernel.h>
14#include <linux/errno.h>
15#include <linux/module.h>
Jean Delvare1c010ff2010-02-05 17:48:13 +010016#include <linux/types.h>
Till Harbaume8c76ee2007-05-01 23:26:35 +020017
18/* include interfaces to usb layer */
19#include <linux/usb.h>
20
21/* include interface to i2c layer */
22#include <linux/i2c.h>
23
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
29
30#define CMD_I2C_IO 4
31#define CMD_I2C_IO_BEGIN (1<<0)
32#define CMD_I2C_IO_END (1<<1)
33
Jean Delvarec05d4902010-03-02 12:23:40 +010034/* i2c bit delay, default is 10us -> 100kHz max
35 (in practice, due to additional delays in the i2c bitbanging
36 code this results in a i2c clock of about 50kHz) */
Jean Delvare1c010ff2010-02-05 17:48:13 +010037static unsigned short delay = 10;
38module_param(delay, ushort, 0);
Jean Delvarec05d4902010-03-02 12:23:40 +010039MODULE_PARM_DESC(delay, "bit delay in microseconds "
40 "(default is 10us for 100kHz max)");
Till Harbaume8c76ee2007-05-01 23:26:35 +020041
42static int usb_read(struct i2c_adapter *adapter, int cmd,
43 int value, int index, void *data, int len);
44
45static int usb_write(struct i2c_adapter *adapter, int cmd,
46 int value, int index, void *data, int len);
47
48/* ----- begin of i2c layer ---------------------------------------------- */
49
50#define STATUS_IDLE 0
51#define STATUS_ADDRESS_ACK 1
52#define STATUS_ADDRESS_NAK 2
53
54static int usb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
55{
56 unsigned char status;
57 struct i2c_msg *pmsg;
58 int i;
59
60 dev_dbg(&adapter->dev, "master xfer %d messages:\n", num);
61
62 for (i = 0 ; i < num ; i++) {
63 int cmd = CMD_I2C_IO;
64
65 if (i == 0)
66 cmd |= CMD_I2C_IO_BEGIN;
67
68 if (i == num-1)
69 cmd |= CMD_I2C_IO_END;
70
71 pmsg = &msgs[i];
72
73 dev_dbg(&adapter->dev,
74 " %d: %s (flags %d) %d bytes to 0x%02x\n",
75 i, pmsg->flags & I2C_M_RD ? "read" : "write",
76 pmsg->flags, pmsg->len, pmsg->addr);
77
78 /* and directly send the message */
79 if (pmsg->flags & I2C_M_RD) {
80 /* read data */
81 if (usb_read(adapter, cmd,
82 pmsg->flags, pmsg->addr,
83 pmsg->buf, pmsg->len) != pmsg->len) {
84 dev_err(&adapter->dev,
85 "failure reading data\n");
86 return -EREMOTEIO;
87 }
88 } else {
89 /* write data */
90 if (usb_write(adapter, cmd,
91 pmsg->flags, pmsg->addr,
92 pmsg->buf, pmsg->len) != pmsg->len) {
93 dev_err(&adapter->dev,
94 "failure writing data\n");
95 return -EREMOTEIO;
96 }
97 }
98
99 /* read status */
100 if (usb_read(adapter, CMD_GET_STATUS, 0, 0, &status, 1) != 1) {
101 dev_err(&adapter->dev, "failure reading status\n");
102 return -EREMOTEIO;
103 }
104
105 dev_dbg(&adapter->dev, " status = %d\n", status);
106 if (status == STATUS_ADDRESS_NAK)
107 return -EREMOTEIO;
108 }
109
110 return i;
111}
112
113static u32 usb_func(struct i2c_adapter *adapter)
114{
Jean Delvare1c010ff2010-02-05 17:48:13 +0100115 __le32 func;
Till Harbaume8c76ee2007-05-01 23:26:35 +0200116
117 /* get functionality from adapter */
118 if (usb_read(adapter, CMD_GET_FUNC, 0, 0, &func, sizeof(func)) !=
119 sizeof(func)) {
120 dev_err(&adapter->dev, "failure reading functionality\n");
121 return 0;
122 }
123
Jean Delvare1c010ff2010-02-05 17:48:13 +0100124 return le32_to_cpu(func);
Till Harbaume8c76ee2007-05-01 23:26:35 +0200125}
126
127/* This is the actual algorithm we define */
128static const struct i2c_algorithm usb_algorithm = {
129 .master_xfer = usb_xfer,
130 .functionality = usb_func,
131};
132
133/* ----- end of i2c layer ------------------------------------------------ */
134
135/* ----- begin of usb layer ---------------------------------------------- */
136
Till Harbaumfa16eef2008-04-11 12:07:05 +0200137/*
138 * Initially the usb i2c interface uses a vid/pid pair donated by
139 * Future Technology Devices International Ltd., later a pair was
140 * bought from EZPrototypes
141 */
Márton Németh4111ecd2010-03-02 12:23:37 +0100142static const struct usb_device_id i2c_tiny_usb_table[] = {
Till Harbaumfa16eef2008-04-11 12:07:05 +0200143 { USB_DEVICE(0x0403, 0xc631) }, /* FTDI */
144 { USB_DEVICE(0x1c40, 0x0534) }, /* EZPrototypes */
145 { } /* Terminating entry */
Till Harbaume8c76ee2007-05-01 23:26:35 +0200146};
147
148MODULE_DEVICE_TABLE(usb, i2c_tiny_usb_table);
149
150/* Structure to hold all of our device specific stuff */
151struct i2c_tiny_usb {
152 struct usb_device *usb_dev; /* the usb device for this device */
153 struct usb_interface *interface; /* the interface for this device */
154 struct i2c_adapter adapter; /* i2c related things */
155};
156
157static int usb_read(struct i2c_adapter *adapter, int cmd,
158 int value, int index, void *data, int len)
159{
160 struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
161
162 /* do control transfer */
163 return usb_control_msg(dev->usb_dev, usb_rcvctrlpipe(dev->usb_dev, 0),
164 cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE |
165 USB_DIR_IN, value, index, data, len, 2000);
166}
167
168static int usb_write(struct i2c_adapter *adapter, int cmd,
169 int value, int index, void *data, int len)
170{
171 struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
172
173 /* do control transfer */
174 return usb_control_msg(dev->usb_dev, usb_sndctrlpipe(dev->usb_dev, 0),
175 cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
176 value, index, data, len, 2000);
177}
178
179static void i2c_tiny_usb_free(struct i2c_tiny_usb *dev)
180{
181 usb_put_dev(dev->usb_dev);
182 kfree(dev);
183}
184
185static int i2c_tiny_usb_probe(struct usb_interface *interface,
186 const struct usb_device_id *id)
187{
188 struct i2c_tiny_usb *dev;
189 int retval = -ENOMEM;
190 u16 version;
191
192 dev_dbg(&interface->dev, "probing usb device\n");
193
194 /* allocate memory for our device state and initialize it */
195 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
196 if (dev == NULL) {
197 dev_err(&interface->dev, "Out of memory\n");
198 goto error;
199 }
200
201 dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));
202 dev->interface = interface;
203
204 /* save our data pointer in this interface device */
205 usb_set_intfdata(interface, dev);
206
207 version = le16_to_cpu(dev->usb_dev->descriptor.bcdDevice);
208 dev_info(&interface->dev,
209 "version %x.%02x found at bus %03d address %03d\n",
210 version >> 8, version & 0xff,
211 dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
212
213 /* setup i2c adapter description */
214 dev->adapter.owner = THIS_MODULE;
215 dev->adapter.class = I2C_CLASS_HWMON;
216 dev->adapter.algo = &usb_algorithm;
217 dev->adapter.algo_data = dev;
Jean Delvare6a7ce822007-05-22 19:49:16 +0200218 snprintf(dev->adapter.name, sizeof(dev->adapter.name),
Till Harbaume8c76ee2007-05-01 23:26:35 +0200219 "i2c-tiny-usb at bus %03d device %03d",
220 dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
221
Jean Delvare1c010ff2010-02-05 17:48:13 +0100222 if (usb_write(&dev->adapter, CMD_SET_DELAY, delay, 0, NULL, 0) != 0) {
Till Harbaume8c76ee2007-05-01 23:26:35 +0200223 dev_err(&dev->adapter.dev,
224 "failure setting delay to %dus\n", delay);
225 retval = -EIO;
226 goto error;
227 }
228
229 dev->adapter.dev.parent = &dev->interface->dev;
230
231 /* and finally attach to i2c layer */
232 i2c_add_adapter(&dev->adapter);
233
234 /* inform user about successful attachment to i2c layer */
235 dev_info(&dev->adapter.dev, "connected i2c-tiny-usb device\n");
236
237 return 0;
238
239 error:
240 if (dev)
241 i2c_tiny_usb_free(dev);
242
243 return retval;
244}
245
246static void i2c_tiny_usb_disconnect(struct usb_interface *interface)
247{
248 struct i2c_tiny_usb *dev = usb_get_intfdata(interface);
249
250 i2c_del_adapter(&dev->adapter);
251 usb_set_intfdata(interface, NULL);
252 i2c_tiny_usb_free(dev);
253
254 dev_dbg(&interface->dev, "disconnected\n");
255}
256
257static struct usb_driver i2c_tiny_usb_driver = {
258 .name = "i2c-tiny-usb",
259 .probe = i2c_tiny_usb_probe,
260 .disconnect = i2c_tiny_usb_disconnect,
261 .id_table = i2c_tiny_usb_table,
262};
263
264static int __init usb_i2c_tiny_usb_init(void)
265{
266 /* register this driver with the USB subsystem */
267 return usb_register(&i2c_tiny_usb_driver);
268}
269
270static void __exit usb_i2c_tiny_usb_exit(void)
271{
272 /* deregister this driver with the USB subsystem */
273 usb_deregister(&i2c_tiny_usb_driver);
274}
275
276module_init(usb_i2c_tiny_usb_init);
277module_exit(usb_i2c_tiny_usb_exit);
278
279/* ----- end of usb layer ------------------------------------------------ */
280
281MODULE_AUTHOR("Till Harbaum <Till@Harbaum.org>");
282MODULE_DESCRIPTION("i2c-tiny-usb driver v1.0");
283MODULE_LICENSE("GPL");