15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/slab.h>
21 #include <linux/i2c.h>
23 #define DRIVER_NAME "i2c-diolan-u2c"
25 #define USB_VENDOR_ID_DIOLAN 0x0abf
26 #define USB_DEVICE_ID_DIOLAN_U2C 0x3370
28 #define DIOLAN_OUT_EP 0x02
29 #define DIOLAN_IN_EP 0x84
32 #define CMD_I2C_READ 0x01
33 #define CMD_I2C_WRITE 0x02
34 #define CMD_I2C_SCAN 0x03
35 #define CMD_I2C_RELEASE_SDA 0x04
36 #define CMD_I2C_RELEASE_SCL 0x05
37 #define CMD_I2C_DROP_SDA 0x06
38 #define CMD_I2C_DROP_SCL 0x07
39 #define CMD_I2C_READ_SDA 0x08
40 #define CMD_I2C_READ_SCL 0x09
41 #define CMD_GET_FW_VERSION 0x0a
42 #define CMD_GET_SERIAL 0x0b
43 #define CMD_I2C_START 0x0c
44 #define CMD_I2C_STOP 0x0d
45 #define CMD_I2C_REPEATED_START 0x0e
46 #define CMD_I2C_PUT_BYTE 0x0f
47 #define CMD_I2C_GET_BYTE 0x10
48 #define CMD_I2C_PUT_ACK 0x11
49 #define CMD_I2C_GET_ACK 0x12
50 #define CMD_I2C_PUT_BYTE_ACK 0x13
51 #define CMD_I2C_GET_BYTE_ACK 0x14
52 #define CMD_I2C_SET_SPEED 0x1b
53 #define CMD_I2C_GET_SPEED 0x1c
54 #define CMD_I2C_SET_CLK_SYNC 0x24
55 #define CMD_I2C_GET_CLK_SYNC 0x25
56 #define CMD_I2C_SET_CLK_SYNC_TO 0x26
57 #define CMD_I2C_GET_CLK_SYNC_TO 0x27
60 #define RESP_FAILED 0x01
61 #define RESP_BAD_MEMADDR 0x04
62 #define RESP_DATA_ERR 0x05
63 #define RESP_NOT_IMPLEMENTED 0x06
64 #define RESP_NACK 0x07
65 #define RESP_TIMEOUT 0x09
67 #define U2C_I2C_SPEED_FAST 0
68 #define U2C_I2C_SPEED_STD 1
69 #define U2C_I2C_SPEED_2KHZ 242
70 #define U2C_I2C_SPEED(f) ((DIV_ROUND_UP(1000000, (f)) - 10) / 2 + 1)
72 #define U2C_I2C_FREQ_FAST 400000
73 #define U2C_I2C_FREQ_STD 100000
74 #define U2C_I2C_FREQ(s) (1000000 / (2 * (s - 1) + 10))
76 #define DIOLAN_USB_TIMEOUT 100
77 #define DIOLAN_SYNC_TIMEOUT 20
79 #define DIOLAN_OUTBUF_LEN 128
80 #define DIOLAN_FLUSH_LEN (DIOLAN_OUTBUF_LEN - 4)
81 #define DIOLAN_INBUF_LEN 256
116 for (i = 0; i < dev->
ocount; i++) {
133 if (ret == 0 && actual > 0) {
134 switch (dev->
ibuffer[actual - 1]) {
162 static int diolan_write_cmd(
struct i2c_diolan_u2c *dev,
bool flush)
165 return diolan_usb_transfer(dev);
174 return diolan_write_cmd(dev, flush);
184 return diolan_write_cmd(dev, flush);
195 return diolan_write_cmd(dev, flush);
208 for (i = 0; i < 10; i++) {
216 if (ret < 0 || actual == 0)
269 static int diolan_set_clock_synch_timeout(
struct i2c_diolan_u2c *dev,
int ms)
271 int to_val = ms * 10;
274 to_val & 0xff, (to_val >> 8) & 0xff,
true);
284 "Diolan U2C firmware version %u.%u\n",
286 (
unsigned int)dev->
ibuffer[1]);
298 "Diolan U2C serial number %u\n", serial);
320 "Diolan U2C at USB bus %03d address %03d speed %d Hz\n",
323 diolan_flush_input(dev);
324 diolan_fw_version(dev);
325 diolan_get_serial(dev);
328 ret = diolan_set_speed(dev, speed);
353 ret = diolan_i2c_start(dev);
357 for (i = 0; i < num; i++) {
360 ret = diolan_i2c_repeated_start(dev);
366 diolan_i2c_put_byte_ack(dev, (pmsg->
addr << 1) | 1);
369 for (j = 0; j < pmsg->
len; j++) {
371 bool ack = j < pmsg->
len - 1;
380 ret = diolan_i2c_get_byte_ack(dev, ack, &byte);
397 ret = diolan_i2c_put_byte_ack(dev, pmsg->
addr << 1);
400 for (j = 0; j < pmsg->
len; j++) {
401 ret = diolan_i2c_put_byte_ack(dev,
410 sret = diolan_i2c_stop(dev);
411 if (sret < 0 && ret >= 0)
426 .master_xfer = diolan_usb_xfer,
427 .functionality = diolan_usb_func,
454 dev_err(&interface->dev,
"no memory for device state\n");
463 usb_set_intfdata(interface, dev);
468 dev->
adapter.algo = &diolan_usb_algorithm;
469 i2c_set_adapdata(&dev->
adapter, dev);
477 ret = diolan_init(dev);
479 dev_err(&interface->dev,
"failed to initialize adapter\n");
486 dev_err(&interface->dev,
"failed to add I2C adapter\n");
495 usb_set_intfdata(interface,
NULL);
496 diolan_u2c_free(dev);
501 static void diolan_u2c_disconnect(
struct usb_interface *interface)
506 usb_set_intfdata(interface,
NULL);
507 diolan_u2c_free(dev);
509 dev_dbg(&interface->dev,
"disconnected\n");
512 static struct usb_driver diolan_u2c_driver = {
514 .probe = diolan_u2c_probe,
515 .disconnect = diolan_u2c_disconnect,
516 .id_table = diolan_u2c_table,