1 // SPDX-License-Identifier: GPL-2.0-only
3 * driver for the i2c-tiny-usb adapter - 1.0
4 * http://www.harbaum.org/till/i2c_tiny_usb
6 * Copyright (C) 2006-2007 Till Harbaum (Till@Harbaum.org)
9 #include <linux/kernel.h>
10 #include <linux/errno.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/types.h>
15 /* include interfaces to usb layer */
16 #include <linux/usb.h>
18 /* include interface to i2c layer */
19 #include <linux/i2c.h>
21 /* commands via USB, must match command ids in the firmware */
23 #define CMD_GET_FUNC 1
24 #define CMD_SET_DELAY 2
25 #define CMD_GET_STATUS 3
28 #define CMD_I2C_IO_BEGIN (1<<0)
29 #define CMD_I2C_IO_END (1<<1)
31 /* i2c bit delay, default is 10us -> 100kHz max
32 (in practice, due to additional delays in the i2c bitbanging
33 code this results in a i2c clock of about 50kHz) */
34 static unsigned short delay
= 10;
35 module_param(delay
, ushort
, 0);
36 MODULE_PARM_DESC(delay
, "bit delay in microseconds "
37 "(default is 10us for 100kHz max)");
39 static int usb_read(struct i2c_adapter
*adapter
, int cmd
,
40 int value
, int index
, void *data
, int len
);
42 static int usb_write(struct i2c_adapter
*adapter
, int cmd
,
43 int value
, int index
, void *data
, int len
);
45 /* ----- begin of i2c layer ---------------------------------------------- */
48 #define STATUS_ADDRESS_ACK 1
49 #define STATUS_ADDRESS_NAK 2
51 static int usb_xfer(struct i2c_adapter
*adapter
, struct i2c_msg
*msgs
, int num
)
53 unsigned char *pstatus
;
57 pstatus
= kmalloc(sizeof(*pstatus
), GFP_KERNEL
);
61 for (i
= 0 ; i
< num
; i
++) {
65 cmd
|= CMD_I2C_IO_BEGIN
;
68 cmd
|= CMD_I2C_IO_END
;
72 dev_dbg(&adapter
->dev
,
73 " %d: %s (flags %d) %d bytes to 0x%02x\n",
74 i
, pmsg
->flags
& I2C_M_RD
? "read" : "write",
75 pmsg
->flags
, pmsg
->len
, pmsg
->addr
);
77 /* and directly send the message */
78 if (pmsg
->flags
& I2C_M_RD
) {
80 if (usb_read(adapter
, cmd
,
81 pmsg
->flags
, pmsg
->addr
,
82 pmsg
->buf
, pmsg
->len
) != pmsg
->len
) {
83 dev_err(&adapter
->dev
,
84 "failure reading data\n");
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");
101 if (usb_read(adapter
, CMD_GET_STATUS
, 0, 0, pstatus
, 1) != 1) {
102 dev_err(&adapter
->dev
, "failure reading status\n");
107 dev_dbg(&adapter
->dev
, " status = %d\n", *pstatus
);
108 if (*pstatus
== STATUS_ADDRESS_NAK
) {
120 static u32
usb_func(struct i2c_adapter
*adapter
)
125 pfunc
= kmalloc(sizeof(*pfunc
), GFP_KERNEL
);
127 /* get functionality from adapter */
128 if (!pfunc
|| usb_read(adapter
, CMD_GET_FUNC
, 0, 0, pfunc
,
129 sizeof(*pfunc
)) != sizeof(*pfunc
)) {
130 dev_err(&adapter
->dev
, "failure reading functionality\n");
135 ret
= le32_to_cpup(pfunc
);
141 /* This is the actual algorithm we define */
142 static const struct i2c_algorithm usb_algorithm
= {
144 .functionality
= usb_func
,
147 /* ----- end of i2c layer ------------------------------------------------ */
149 /* ----- begin of usb layer ---------------------------------------------- */
152 * Initially the usb i2c interface uses a vid/pid pair donated by
153 * Future Technology Devices International Ltd., later a pair was
154 * bought from EZPrototypes
156 static const struct usb_device_id i2c_tiny_usb_table
[] = {
157 { USB_DEVICE(0x0403, 0xc631) }, /* FTDI */
158 { USB_DEVICE(0x1c40, 0x0534) }, /* EZPrototypes */
159 { } /* Terminating entry */
162 MODULE_DEVICE_TABLE(usb
, i2c_tiny_usb_table
);
164 /* Structure to hold all of our device specific stuff */
165 struct i2c_tiny_usb
{
166 struct usb_device
*usb_dev
; /* the usb device for this device */
167 struct usb_interface
*interface
; /* the interface for this device */
168 struct i2c_adapter adapter
; /* i2c related things */
171 static int usb_read(struct i2c_adapter
*adapter
, int cmd
,
172 int value
, int index
, void *data
, int len
)
174 struct i2c_tiny_usb
*dev
= (struct i2c_tiny_usb
*)adapter
->algo_data
;
175 void *dmadata
= kmalloc(len
, GFP_KERNEL
);
181 /* do control transfer */
182 ret
= usb_control_msg(dev
->usb_dev
, usb_rcvctrlpipe(dev
->usb_dev
, 0),
183 cmd
, USB_TYPE_VENDOR
| USB_RECIP_INTERFACE
|
184 USB_DIR_IN
, value
, index
, dmadata
, len
, 2000);
186 memcpy(data
, dmadata
, len
);
191 static int usb_write(struct i2c_adapter
*adapter
, int cmd
,
192 int value
, int index
, void *data
, int len
)
194 struct i2c_tiny_usb
*dev
= (struct i2c_tiny_usb
*)adapter
->algo_data
;
195 void *dmadata
= kmemdup(data
, len
, GFP_KERNEL
);
201 /* do control transfer */
202 ret
= usb_control_msg(dev
->usb_dev
, usb_sndctrlpipe(dev
->usb_dev
, 0),
203 cmd
, USB_TYPE_VENDOR
| USB_RECIP_INTERFACE
,
204 value
, index
, dmadata
, len
, 2000);
210 static void i2c_tiny_usb_free(struct i2c_tiny_usb
*dev
)
212 usb_put_dev(dev
->usb_dev
);
216 static int i2c_tiny_usb_probe(struct usb_interface
*interface
,
217 const struct usb_device_id
*id
)
219 struct i2c_tiny_usb
*dev
;
220 int retval
= -ENOMEM
;
223 if (interface
->intf_assoc
&&
224 interface
->intf_assoc
->bFunctionClass
!= USB_CLASS_VENDOR_SPEC
)
227 dev_dbg(&interface
->dev
, "probing usb device\n");
229 /* allocate memory for our device state and initialize it */
230 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
234 dev
->usb_dev
= usb_get_dev(interface_to_usbdev(interface
));
235 dev
->interface
= interface
;
237 /* save our data pointer in this interface device */
238 usb_set_intfdata(interface
, dev
);
240 version
= le16_to_cpu(dev
->usb_dev
->descriptor
.bcdDevice
);
241 dev_info(&interface
->dev
,
242 "version %x.%02x found at bus %03d address %03d\n",
243 version
>> 8, version
& 0xff,
244 dev
->usb_dev
->bus
->busnum
, dev
->usb_dev
->devnum
);
246 /* setup i2c adapter description */
247 dev
->adapter
.owner
= THIS_MODULE
;
248 dev
->adapter
.class = I2C_CLASS_HWMON
;
249 dev
->adapter
.algo
= &usb_algorithm
;
250 dev
->adapter
.algo_data
= dev
;
251 snprintf(dev
->adapter
.name
, sizeof(dev
->adapter
.name
),
252 "i2c-tiny-usb at bus %03d device %03d",
253 dev
->usb_dev
->bus
->busnum
, dev
->usb_dev
->devnum
);
255 if (usb_write(&dev
->adapter
, CMD_SET_DELAY
, delay
, 0, NULL
, 0) != 0) {
256 dev_err(&dev
->adapter
.dev
,
257 "failure setting delay to %dus\n", delay
);
262 dev
->adapter
.dev
.parent
= &dev
->interface
->dev
;
264 /* and finally attach to i2c layer */
265 i2c_add_adapter(&dev
->adapter
);
267 /* inform user about successful attachment to i2c layer */
268 dev_info(&dev
->adapter
.dev
, "connected i2c-tiny-usb device\n");
274 i2c_tiny_usb_free(dev
);
279 static void i2c_tiny_usb_disconnect(struct usb_interface
*interface
)
281 struct i2c_tiny_usb
*dev
= usb_get_intfdata(interface
);
283 i2c_del_adapter(&dev
->adapter
);
284 usb_set_intfdata(interface
, NULL
);
285 i2c_tiny_usb_free(dev
);
287 dev_dbg(&interface
->dev
, "disconnected\n");
290 static struct usb_driver i2c_tiny_usb_driver
= {
291 .name
= "i2c-tiny-usb",
292 .probe
= i2c_tiny_usb_probe
,
293 .disconnect
= i2c_tiny_usb_disconnect
,
294 .id_table
= i2c_tiny_usb_table
,
297 module_usb_driver(i2c_tiny_usb_driver
);
299 /* ----- end of usb layer ------------------------------------------------ */
301 MODULE_AUTHOR("Till Harbaum <Till@Harbaum.org>");
302 MODULE_DESCRIPTION("i2c-tiny-usb driver v1.0");
303 MODULE_LICENSE("GPL");