xref: /linux/drivers/i2c/busses/i2c-tiny-usb.c (revision 6a7ce82f02e8ce7391e40018749d93762abf6ee0)
1e8c76eedSTill Harbaum /*
2e8c76eedSTill Harbaum  * driver for the i2c-tiny-usb adapter - 1.0
3e8c76eedSTill Harbaum  * http://www.harbaum.org/till/i2c_tiny_usb
4e8c76eedSTill Harbaum  *
5e8c76eedSTill Harbaum  * Copyright (C) 2006-2007 Till Harbaum (Till@Harbaum.org)
6e8c76eedSTill Harbaum  *
7e8c76eedSTill Harbaum  * This program is free software; you can redistribute it and/or
8e8c76eedSTill Harbaum  * modify it under the terms of the GNU General Public License as
9e8c76eedSTill Harbaum  * published by the Free Software Foundation, version 2.
10e8c76eedSTill Harbaum  *
11e8c76eedSTill Harbaum  */
12e8c76eedSTill Harbaum 
13e8c76eedSTill Harbaum #include <linux/kernel.h>
14e8c76eedSTill Harbaum #include <linux/errno.h>
15e8c76eedSTill Harbaum #include <linux/module.h>
16e8c76eedSTill Harbaum 
17e8c76eedSTill Harbaum /* include interfaces to usb layer */
18e8c76eedSTill Harbaum #include <linux/usb.h>
19e8c76eedSTill Harbaum 
20e8c76eedSTill Harbaum /* include interface to i2c layer */
21e8c76eedSTill Harbaum #include <linux/i2c.h>
22e8c76eedSTill Harbaum 
23e8c76eedSTill Harbaum /* commands via USB, must match command ids in the firmware */
24e8c76eedSTill Harbaum #define CMD_ECHO		0
25e8c76eedSTill Harbaum #define CMD_GET_FUNC		1
26e8c76eedSTill Harbaum #define CMD_SET_DELAY		2
27e8c76eedSTill Harbaum #define CMD_GET_STATUS		3
28e8c76eedSTill Harbaum 
29e8c76eedSTill Harbaum #define CMD_I2C_IO		4
30e8c76eedSTill Harbaum #define CMD_I2C_IO_BEGIN	(1<<0)
31e8c76eedSTill Harbaum #define CMD_I2C_IO_END		(1<<1)
32e8c76eedSTill Harbaum 
33e8c76eedSTill Harbaum /* i2c bit delay, default is 10us -> 100kHz */
34e8c76eedSTill Harbaum static int delay = 10;
35e8c76eedSTill Harbaum module_param(delay, int, 0);
36e8c76eedSTill Harbaum MODULE_PARM_DESC(delay, "bit delay in microseconds, "
37e8c76eedSTill Harbaum 		 "e.g. 10 for 100kHz (default is 100kHz)");
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 {
53e8c76eedSTill Harbaum 	unsigned char status;
54e8c76eedSTill Harbaum 	struct i2c_msg *pmsg;
55e8c76eedSTill Harbaum 	int i;
56e8c76eedSTill Harbaum 
57e8c76eedSTill Harbaum 	dev_dbg(&adapter->dev, "master xfer %d messages:\n", num);
58e8c76eedSTill Harbaum 
59e8c76eedSTill Harbaum 	for (i = 0 ; i < num ; i++) {
60e8c76eedSTill Harbaum 		int cmd = CMD_I2C_IO;
61e8c76eedSTill Harbaum 
62e8c76eedSTill Harbaum 		if (i == 0)
63e8c76eedSTill Harbaum 			cmd |= CMD_I2C_IO_BEGIN;
64e8c76eedSTill Harbaum 
65e8c76eedSTill Harbaum 		if (i == num-1)
66e8c76eedSTill Harbaum 			cmd |= CMD_I2C_IO_END;
67e8c76eedSTill Harbaum 
68e8c76eedSTill Harbaum 		pmsg = &msgs[i];
69e8c76eedSTill Harbaum 
70e8c76eedSTill Harbaum 		dev_dbg(&adapter->dev,
71e8c76eedSTill Harbaum 			"  %d: %s (flags %d) %d bytes to 0x%02x\n",
72e8c76eedSTill Harbaum 			i, pmsg->flags & I2C_M_RD ? "read" : "write",
73e8c76eedSTill Harbaum 			pmsg->flags, pmsg->len, pmsg->addr);
74e8c76eedSTill Harbaum 
75e8c76eedSTill Harbaum 		/* and directly send the message */
76e8c76eedSTill Harbaum 		if (pmsg->flags & I2C_M_RD) {
77e8c76eedSTill Harbaum 			/* read data */
78e8c76eedSTill Harbaum 			if (usb_read(adapter, cmd,
79e8c76eedSTill Harbaum 				     pmsg->flags, pmsg->addr,
80e8c76eedSTill Harbaum 				     pmsg->buf, pmsg->len) != pmsg->len) {
81e8c76eedSTill Harbaum 				dev_err(&adapter->dev,
82e8c76eedSTill Harbaum 					"failure reading data\n");
83e8c76eedSTill Harbaum 				return -EREMOTEIO;
84e8c76eedSTill Harbaum 			}
85e8c76eedSTill Harbaum 		} else {
86e8c76eedSTill Harbaum 			/* write data */
87e8c76eedSTill Harbaum 			if (usb_write(adapter, cmd,
88e8c76eedSTill Harbaum 				      pmsg->flags, pmsg->addr,
89e8c76eedSTill Harbaum 				      pmsg->buf, pmsg->len) != pmsg->len) {
90e8c76eedSTill Harbaum 				dev_err(&adapter->dev,
91e8c76eedSTill Harbaum 					"failure writing data\n");
92e8c76eedSTill Harbaum 				return -EREMOTEIO;
93e8c76eedSTill Harbaum 			}
94e8c76eedSTill Harbaum 		}
95e8c76eedSTill Harbaum 
96e8c76eedSTill Harbaum 		/* read status */
97e8c76eedSTill Harbaum 		if (usb_read(adapter, CMD_GET_STATUS, 0, 0, &status, 1) != 1) {
98e8c76eedSTill Harbaum 			dev_err(&adapter->dev, "failure reading status\n");
99e8c76eedSTill Harbaum 			return -EREMOTEIO;
100e8c76eedSTill Harbaum 		}
101e8c76eedSTill Harbaum 
102e8c76eedSTill Harbaum 		dev_dbg(&adapter->dev, "  status = %d\n", status);
103e8c76eedSTill Harbaum 		if (status == STATUS_ADDRESS_NAK)
104e8c76eedSTill Harbaum 			return -EREMOTEIO;
105e8c76eedSTill Harbaum 	}
106e8c76eedSTill Harbaum 
107e8c76eedSTill Harbaum 	return i;
108e8c76eedSTill Harbaum }
109e8c76eedSTill Harbaum 
110e8c76eedSTill Harbaum static u32 usb_func(struct i2c_adapter *adapter)
111e8c76eedSTill Harbaum {
112e8c76eedSTill Harbaum 	u32 func;
113e8c76eedSTill Harbaum 
114e8c76eedSTill Harbaum 	/* get functionality from adapter */
115e8c76eedSTill Harbaum 	if (usb_read(adapter, CMD_GET_FUNC, 0, 0, &func, sizeof(func)) !=
116e8c76eedSTill Harbaum 	    sizeof(func)) {
117e8c76eedSTill Harbaum 		dev_err(&adapter->dev, "failure reading functionality\n");
118e8c76eedSTill Harbaum 		return 0;
119e8c76eedSTill Harbaum 	}
120e8c76eedSTill Harbaum 
121e8c76eedSTill Harbaum 	return func;
122e8c76eedSTill Harbaum }
123e8c76eedSTill Harbaum 
124e8c76eedSTill Harbaum /* This is the actual algorithm we define */
125e8c76eedSTill Harbaum static const struct i2c_algorithm usb_algorithm = {
126e8c76eedSTill Harbaum 	.master_xfer	= usb_xfer,
127e8c76eedSTill Harbaum 	.functionality	= usb_func,
128e8c76eedSTill Harbaum };
129e8c76eedSTill Harbaum 
130e8c76eedSTill Harbaum /* ----- end of i2c layer ------------------------------------------------ */
131e8c76eedSTill Harbaum 
132e8c76eedSTill Harbaum /* ----- begin of usb layer ---------------------------------------------- */
133e8c76eedSTill Harbaum 
134e8c76eedSTill Harbaum /* The usb i2c interface uses a vid/pid pair donated by */
135e8c76eedSTill Harbaum /* Future Technology Devices International Ltd. */
136e8c76eedSTill Harbaum static struct usb_device_id i2c_tiny_usb_table [] = {
137e8c76eedSTill Harbaum 	{ USB_DEVICE(0x0403, 0xc631) },
138e8c76eedSTill Harbaum 	{ }			/* Terminating entry */
139e8c76eedSTill Harbaum };
140e8c76eedSTill Harbaum 
141e8c76eedSTill Harbaum MODULE_DEVICE_TABLE(usb, i2c_tiny_usb_table);
142e8c76eedSTill Harbaum 
143e8c76eedSTill Harbaum /* Structure to hold all of our device specific stuff */
144e8c76eedSTill Harbaum struct i2c_tiny_usb {
145e8c76eedSTill Harbaum 	struct usb_device *usb_dev; /* the usb device for this device */
146e8c76eedSTill Harbaum 	struct usb_interface *interface; /* the interface for this device */
147e8c76eedSTill Harbaum 	struct i2c_adapter adapter; /* i2c related things */
148e8c76eedSTill Harbaum };
149e8c76eedSTill Harbaum 
150e8c76eedSTill Harbaum static int usb_read(struct i2c_adapter *adapter, int cmd,
151e8c76eedSTill Harbaum 		    int value, int index, void *data, int len)
152e8c76eedSTill Harbaum {
153e8c76eedSTill Harbaum 	struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
154e8c76eedSTill Harbaum 
155e8c76eedSTill Harbaum 	/* do control transfer */
156e8c76eedSTill Harbaum 	return usb_control_msg(dev->usb_dev, usb_rcvctrlpipe(dev->usb_dev, 0),
157e8c76eedSTill Harbaum 			       cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE |
158e8c76eedSTill Harbaum 			       USB_DIR_IN, value, index, data, len, 2000);
159e8c76eedSTill Harbaum }
160e8c76eedSTill Harbaum 
161e8c76eedSTill Harbaum static int usb_write(struct i2c_adapter *adapter, int cmd,
162e8c76eedSTill Harbaum 		     int value, int index, void *data, int len)
163e8c76eedSTill Harbaum {
164e8c76eedSTill Harbaum 	struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
165e8c76eedSTill Harbaum 
166e8c76eedSTill Harbaum 	/* do control transfer */
167e8c76eedSTill Harbaum 	return usb_control_msg(dev->usb_dev, usb_sndctrlpipe(dev->usb_dev, 0),
168e8c76eedSTill Harbaum 			       cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
169e8c76eedSTill Harbaum 			       value, index, data, len, 2000);
170e8c76eedSTill Harbaum }
171e8c76eedSTill Harbaum 
172e8c76eedSTill Harbaum static void i2c_tiny_usb_free(struct i2c_tiny_usb *dev)
173e8c76eedSTill Harbaum {
174e8c76eedSTill Harbaum 	usb_put_dev(dev->usb_dev);
175e8c76eedSTill Harbaum 	kfree(dev);
176e8c76eedSTill Harbaum }
177e8c76eedSTill Harbaum 
178e8c76eedSTill Harbaum static int i2c_tiny_usb_probe(struct usb_interface *interface,
179e8c76eedSTill Harbaum 			      const struct usb_device_id *id)
180e8c76eedSTill Harbaum {
181e8c76eedSTill Harbaum 	struct i2c_tiny_usb *dev;
182e8c76eedSTill Harbaum 	int retval = -ENOMEM;
183e8c76eedSTill Harbaum 	u16 version;
184e8c76eedSTill Harbaum 
185e8c76eedSTill Harbaum 	dev_dbg(&interface->dev, "probing usb device\n");
186e8c76eedSTill Harbaum 
187e8c76eedSTill Harbaum 	/* allocate memory for our device state and initialize it */
188e8c76eedSTill Harbaum 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
189e8c76eedSTill Harbaum 	if (dev == NULL) {
190e8c76eedSTill Harbaum 		dev_err(&interface->dev, "Out of memory\n");
191e8c76eedSTill Harbaum 		goto error;
192e8c76eedSTill Harbaum 	}
193e8c76eedSTill Harbaum 
194e8c76eedSTill Harbaum 	dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));
195e8c76eedSTill Harbaum 	dev->interface = interface;
196e8c76eedSTill Harbaum 
197e8c76eedSTill Harbaum 	/* save our data pointer in this interface device */
198e8c76eedSTill Harbaum 	usb_set_intfdata(interface, dev);
199e8c76eedSTill Harbaum 
200e8c76eedSTill Harbaum 	version = le16_to_cpu(dev->usb_dev->descriptor.bcdDevice);
201e8c76eedSTill Harbaum 	dev_info(&interface->dev,
202e8c76eedSTill Harbaum 		 "version %x.%02x found at bus %03d address %03d\n",
203e8c76eedSTill Harbaum 		 version >> 8, version & 0xff,
204e8c76eedSTill Harbaum 		 dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
205e8c76eedSTill Harbaum 
206e8c76eedSTill Harbaum 	/* setup i2c adapter description */
207e8c76eedSTill Harbaum 	dev->adapter.owner = THIS_MODULE;
208e8c76eedSTill Harbaum 	dev->adapter.class = I2C_CLASS_HWMON;
209e8c76eedSTill Harbaum 	dev->adapter.algo = &usb_algorithm;
210e8c76eedSTill Harbaum 	dev->adapter.algo_data = dev;
211*6a7ce82fSJean Delvare 	snprintf(dev->adapter.name, sizeof(dev->adapter.name),
212e8c76eedSTill Harbaum 		 "i2c-tiny-usb at bus %03d device %03d",
213e8c76eedSTill Harbaum 		 dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
214e8c76eedSTill Harbaum 
215e8c76eedSTill Harbaum 	if (usb_write(&dev->adapter, CMD_SET_DELAY,
216e8c76eedSTill Harbaum 		      cpu_to_le16(delay), 0, NULL, 0) != 0) {
217e8c76eedSTill Harbaum 		dev_err(&dev->adapter.dev,
218e8c76eedSTill Harbaum 			"failure setting delay to %dus\n", delay);
219e8c76eedSTill Harbaum 		retval = -EIO;
220e8c76eedSTill Harbaum 		goto error;
221e8c76eedSTill Harbaum 	}
222e8c76eedSTill Harbaum 
223e8c76eedSTill Harbaum 	dev->adapter.dev.parent = &dev->interface->dev;
224e8c76eedSTill Harbaum 
225e8c76eedSTill Harbaum 	/* and finally attach to i2c layer */
226e8c76eedSTill Harbaum 	i2c_add_adapter(&dev->adapter);
227e8c76eedSTill Harbaum 
228e8c76eedSTill Harbaum 	/* inform user about successful attachment to i2c layer */
229e8c76eedSTill Harbaum 	dev_info(&dev->adapter.dev, "connected i2c-tiny-usb device\n");
230e8c76eedSTill Harbaum 
231e8c76eedSTill Harbaum 	return 0;
232e8c76eedSTill Harbaum 
233e8c76eedSTill Harbaum  error:
234e8c76eedSTill Harbaum 	if (dev)
235e8c76eedSTill Harbaum 		i2c_tiny_usb_free(dev);
236e8c76eedSTill Harbaum 
237e8c76eedSTill Harbaum 	return retval;
238e8c76eedSTill Harbaum }
239e8c76eedSTill Harbaum 
240e8c76eedSTill Harbaum static void i2c_tiny_usb_disconnect(struct usb_interface *interface)
241e8c76eedSTill Harbaum {
242e8c76eedSTill Harbaum 	struct i2c_tiny_usb *dev = usb_get_intfdata(interface);
243e8c76eedSTill Harbaum 
244e8c76eedSTill Harbaum 	i2c_del_adapter(&dev->adapter);
245e8c76eedSTill Harbaum 	usb_set_intfdata(interface, NULL);
246e8c76eedSTill Harbaum 	i2c_tiny_usb_free(dev);
247e8c76eedSTill Harbaum 
248e8c76eedSTill Harbaum 	dev_dbg(&interface->dev, "disconnected\n");
249e8c76eedSTill Harbaum }
250e8c76eedSTill Harbaum 
251e8c76eedSTill Harbaum static struct usb_driver i2c_tiny_usb_driver = {
252e8c76eedSTill Harbaum 	.name		= "i2c-tiny-usb",
253e8c76eedSTill Harbaum 	.probe		= i2c_tiny_usb_probe,
254e8c76eedSTill Harbaum 	.disconnect	= i2c_tiny_usb_disconnect,
255e8c76eedSTill Harbaum 	.id_table	= i2c_tiny_usb_table,
256e8c76eedSTill Harbaum };
257e8c76eedSTill Harbaum 
258e8c76eedSTill Harbaum static int __init usb_i2c_tiny_usb_init(void)
259e8c76eedSTill Harbaum {
260e8c76eedSTill Harbaum 	/* register this driver with the USB subsystem */
261e8c76eedSTill Harbaum 	return usb_register(&i2c_tiny_usb_driver);
262e8c76eedSTill Harbaum }
263e8c76eedSTill Harbaum 
264e8c76eedSTill Harbaum static void __exit usb_i2c_tiny_usb_exit(void)
265e8c76eedSTill Harbaum {
266e8c76eedSTill Harbaum 	/* deregister this driver with the USB subsystem */
267e8c76eedSTill Harbaum 	usb_deregister(&i2c_tiny_usb_driver);
268e8c76eedSTill Harbaum }
269e8c76eedSTill Harbaum 
270e8c76eedSTill Harbaum module_init(usb_i2c_tiny_usb_init);
271e8c76eedSTill Harbaum module_exit(usb_i2c_tiny_usb_exit);
272e8c76eedSTill Harbaum 
273e8c76eedSTill Harbaum /* ----- end of usb layer ------------------------------------------------ */
274e8c76eedSTill Harbaum 
275e8c76eedSTill Harbaum MODULE_AUTHOR("Till Harbaum <Till@Harbaum.org>");
276e8c76eedSTill Harbaum MODULE_DESCRIPTION("i2c-tiny-usb driver v1.0");
277e8c76eedSTill Harbaum MODULE_LICENSE("GPL");
278