i2c-diolan-u2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * Driver for the Diolan u2c-12 USB-I2C adapter
  3. *
  4. * Copyright (c) 2010-2011 Ericsson AB
  5. *
  6. * Derived from:
  7. * i2c-tiny-usb.c
  8. * Copyright (C) 2006-2007 Till Harbaum (Till@Harbaum.org)
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation, version 2.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/module.h>
  17. #include <linux/types.h>
  18. #include <linux/slab.h>
  19. #include <linux/usb.h>
  20. #include <linux/i2c.h>
  21. #define DRIVER_NAME "i2c-diolan-u2c"
  22. #define USB_VENDOR_ID_DIOLAN 0x0abf
  23. #define USB_DEVICE_ID_DIOLAN_U2C 0x3370
  24. /* commands via USB, must match command ids in the firmware */
  25. #define CMD_I2C_READ 0x01
  26. #define CMD_I2C_WRITE 0x02
  27. #define CMD_I2C_SCAN 0x03 /* Returns list of detected devices */
  28. #define CMD_I2C_RELEASE_SDA 0x04
  29. #define CMD_I2C_RELEASE_SCL 0x05
  30. #define CMD_I2C_DROP_SDA 0x06
  31. #define CMD_I2C_DROP_SCL 0x07
  32. #define CMD_I2C_READ_SDA 0x08
  33. #define CMD_I2C_READ_SCL 0x09
  34. #define CMD_GET_FW_VERSION 0x0a
  35. #define CMD_GET_SERIAL 0x0b
  36. #define CMD_I2C_START 0x0c
  37. #define CMD_I2C_STOP 0x0d
  38. #define CMD_I2C_REPEATED_START 0x0e
  39. #define CMD_I2C_PUT_BYTE 0x0f
  40. #define CMD_I2C_GET_BYTE 0x10
  41. #define CMD_I2C_PUT_ACK 0x11
  42. #define CMD_I2C_GET_ACK 0x12
  43. #define CMD_I2C_PUT_BYTE_ACK 0x13
  44. #define CMD_I2C_GET_BYTE_ACK 0x14
  45. #define CMD_I2C_SET_SPEED 0x1b
  46. #define CMD_I2C_GET_SPEED 0x1c
  47. #define CMD_I2C_SET_CLK_SYNC 0x24
  48. #define CMD_I2C_GET_CLK_SYNC 0x25
  49. #define CMD_I2C_SET_CLK_SYNC_TO 0x26
  50. #define CMD_I2C_GET_CLK_SYNC_TO 0x27
  51. #define RESP_OK 0x00
  52. #define RESP_FAILED 0x01
  53. #define RESP_BAD_MEMADDR 0x04
  54. #define RESP_DATA_ERR 0x05
  55. #define RESP_NOT_IMPLEMENTED 0x06
  56. #define RESP_NACK 0x07
  57. #define RESP_TIMEOUT 0x09
  58. #define U2C_I2C_SPEED_FAST 0 /* 400 kHz */
  59. #define U2C_I2C_SPEED_STD 1 /* 100 kHz */
  60. #define U2C_I2C_SPEED_2KHZ 242 /* 2 kHz, minimum speed */
  61. #define U2C_I2C_SPEED(f) ((DIV_ROUND_UP(1000000, (f)) - 10) / 2 + 1)
  62. #define U2C_I2C_FREQ_FAST 400000
  63. #define U2C_I2C_FREQ_STD 100000
  64. #define U2C_I2C_FREQ(s) (1000000 / (2 * (s - 1) + 10))
  65. #define DIOLAN_USB_TIMEOUT 100 /* in ms */
  66. #define DIOLAN_SYNC_TIMEOUT 20 /* in ms */
  67. #define DIOLAN_OUTBUF_LEN 128
  68. #define DIOLAN_FLUSH_LEN (DIOLAN_OUTBUF_LEN - 4)
  69. #define DIOLAN_INBUF_LEN 256 /* Maximum supported receive length */
  70. /* Structure to hold all of our device specific stuff */
  71. struct i2c_diolan_u2c {
  72. u8 obuffer[DIOLAN_OUTBUF_LEN]; /* output buffer */
  73. u8 ibuffer[DIOLAN_INBUF_LEN]; /* input buffer */
  74. int ep_in, ep_out; /* Endpoints */
  75. struct usb_device *usb_dev; /* the usb device for this device */
  76. struct usb_interface *interface;/* the interface for this device */
  77. struct i2c_adapter adapter; /* i2c related things */
  78. int olen; /* Output buffer length */
  79. int ocount; /* Number of enqueued messages */
  80. };
  81. static uint frequency = U2C_I2C_FREQ_STD; /* I2C clock frequency in Hz */
  82. module_param(frequency, uint, S_IRUGO | S_IWUSR);
  83. MODULE_PARM_DESC(frequency, "I2C clock frequency in hertz");
  84. /* usb layer */
  85. /* Send command to device, and get response. */
  86. static int diolan_usb_transfer(struct i2c_diolan_u2c *dev)
  87. {
  88. int ret = 0;
  89. int actual;
  90. int i;
  91. if (!dev->olen || !dev->ocount)
  92. return -EINVAL;
  93. ret = usb_bulk_msg(dev->usb_dev,
  94. usb_sndbulkpipe(dev->usb_dev, dev->ep_out),
  95. dev->obuffer, dev->olen, &actual,
  96. DIOLAN_USB_TIMEOUT);
  97. if (!ret) {
  98. for (i = 0; i < dev->ocount; i++) {
  99. int tmpret;
  100. tmpret = usb_bulk_msg(dev->usb_dev,
  101. usb_rcvbulkpipe(dev->usb_dev,
  102. dev->ep_in),
  103. dev->ibuffer,
  104. sizeof(dev->ibuffer), &actual,
  105. DIOLAN_USB_TIMEOUT);
  106. /*
  107. * Stop command processing if a previous command
  108. * returned an error.
  109. * Note that we still need to retrieve all messages.
  110. */
  111. if (ret < 0)
  112. continue;
  113. ret = tmpret;
  114. if (ret == 0 && actual > 0) {
  115. switch (dev->ibuffer[actual - 1]) {
  116. case RESP_NACK:
  117. /*
  118. * Return ENXIO if NACK was received as
  119. * response to the address phase,
  120. * EIO otherwise
  121. */
  122. ret = i == 1 ? -ENXIO : -EIO;
  123. break;
  124. case RESP_TIMEOUT:
  125. ret = -ETIMEDOUT;
  126. break;
  127. case RESP_OK:
  128. /* strip off return code */
  129. ret = actual - 1;
  130. break;
  131. default:
  132. ret = -EIO;
  133. break;
  134. }
  135. }
  136. }
  137. }
  138. dev->olen = 0;
  139. dev->ocount = 0;
  140. return ret;
  141. }
  142. static int diolan_write_cmd(struct i2c_diolan_u2c *dev, bool flush)
  143. {
  144. if (flush || dev->olen >= DIOLAN_FLUSH_LEN)
  145. return diolan_usb_transfer(dev);
  146. return 0;
  147. }
  148. /* Send command (no data) */
  149. static int diolan_usb_cmd(struct i2c_diolan_u2c *dev, u8 command, bool flush)
  150. {
  151. dev->obuffer[dev->olen++] = command;
  152. dev->ocount++;
  153. return diolan_write_cmd(dev, flush);
  154. }
  155. /* Send command with one byte of data */
  156. static int diolan_usb_cmd_data(struct i2c_diolan_u2c *dev, u8 command, u8 data,
  157. bool flush)
  158. {
  159. dev->obuffer[dev->olen++] = command;
  160. dev->obuffer[dev->olen++] = data;
  161. dev->ocount++;
  162. return diolan_write_cmd(dev, flush);
  163. }
  164. /* Send command with two bytes of data */
  165. static int diolan_usb_cmd_data2(struct i2c_diolan_u2c *dev, u8 command, u8 d1,
  166. u8 d2, bool flush)
  167. {
  168. dev->obuffer[dev->olen++] = command;
  169. dev->obuffer[dev->olen++] = d1;
  170. dev->obuffer[dev->olen++] = d2;
  171. dev->ocount++;
  172. return diolan_write_cmd(dev, flush);
  173. }
  174. /*
  175. * Flush input queue.
  176. * If we don't do this at startup and the controller has queued up
  177. * messages which were not retrieved, it will stop responding
  178. * at some point.
  179. */
  180. static void diolan_flush_input(struct i2c_diolan_u2c *dev)
  181. {
  182. int i;
  183. for (i = 0; i < 10; i++) {
  184. int actual = 0;
  185. int ret;
  186. ret = usb_bulk_msg(dev->usb_dev,
  187. usb_rcvbulkpipe(dev->usb_dev, dev->ep_in),
  188. dev->ibuffer, sizeof(dev->ibuffer), &actual,
  189. DIOLAN_USB_TIMEOUT);
  190. if (ret < 0 || actual == 0)
  191. break;
  192. }
  193. if (i == 10)
  194. dev_err(&dev->interface->dev, "Failed to flush input buffer\n");
  195. }
  196. static int diolan_i2c_start(struct i2c_diolan_u2c *dev)
  197. {
  198. return diolan_usb_cmd(dev, CMD_I2C_START, false);
  199. }
  200. static int diolan_i2c_repeated_start(struct i2c_diolan_u2c *dev)
  201. {
  202. return diolan_usb_cmd(dev, CMD_I2C_REPEATED_START, false);
  203. }
  204. static int diolan_i2c_stop(struct i2c_diolan_u2c *dev)
  205. {
  206. return diolan_usb_cmd(dev, CMD_I2C_STOP, true);
  207. }
  208. static int diolan_i2c_get_byte_ack(struct i2c_diolan_u2c *dev, bool ack,
  209. u8 *byte)
  210. {
  211. int ret;
  212. ret = diolan_usb_cmd_data(dev, CMD_I2C_GET_BYTE_ACK, ack, true);
  213. if (ret > 0)
  214. *byte = dev->ibuffer[0];
  215. else if (ret == 0)
  216. ret = -EIO;
  217. return ret;
  218. }
  219. static int diolan_i2c_put_byte_ack(struct i2c_diolan_u2c *dev, u8 byte)
  220. {
  221. return diolan_usb_cmd_data(dev, CMD_I2C_PUT_BYTE_ACK, byte, false);
  222. }
  223. static int diolan_set_speed(struct i2c_diolan_u2c *dev, u8 speed)
  224. {
  225. return diolan_usb_cmd_data(dev, CMD_I2C_SET_SPEED, speed, true);
  226. }
  227. /* Enable or disable clock synchronization (stretching) */
  228. static int diolan_set_clock_synch(struct i2c_diolan_u2c *dev, bool enable)
  229. {
  230. return diolan_usb_cmd_data(dev, CMD_I2C_SET_CLK_SYNC, enable, true);
  231. }
  232. /* Set clock synchronization timeout in ms */
  233. static int diolan_set_clock_synch_timeout(struct i2c_diolan_u2c *dev, int ms)
  234. {
  235. int to_val = ms * 10;
  236. return diolan_usb_cmd_data2(dev, CMD_I2C_SET_CLK_SYNC_TO,
  237. to_val & 0xff, (to_val >> 8) & 0xff, true);
  238. }
  239. static void diolan_fw_version(struct i2c_diolan_u2c *dev)
  240. {
  241. int ret;
  242. ret = diolan_usb_cmd(dev, CMD_GET_FW_VERSION, true);
  243. if (ret >= 2)
  244. dev_info(&dev->interface->dev,
  245. "Diolan U2C firmware version %u.%u\n",
  246. (unsigned int)dev->ibuffer[0],
  247. (unsigned int)dev->ibuffer[1]);
  248. }
  249. static void diolan_get_serial(struct i2c_diolan_u2c *dev)
  250. {
  251. int ret;
  252. u32 serial;
  253. ret = diolan_usb_cmd(dev, CMD_GET_SERIAL, true);
  254. if (ret >= 4) {
  255. serial = le32_to_cpu(*(u32 *)dev->ibuffer);
  256. dev_info(&dev->interface->dev,
  257. "Diolan U2C serial number %u\n", serial);
  258. }
  259. }
  260. static int diolan_init(struct i2c_diolan_u2c *dev)
  261. {
  262. int speed, ret;
  263. if (frequency >= 200000) {
  264. speed = U2C_I2C_SPEED_FAST;
  265. frequency = U2C_I2C_FREQ_FAST;
  266. } else if (frequency >= 100000 || frequency == 0) {
  267. speed = U2C_I2C_SPEED_STD;
  268. frequency = U2C_I2C_FREQ_STD;
  269. } else {
  270. speed = U2C_I2C_SPEED(frequency);
  271. if (speed > U2C_I2C_SPEED_2KHZ)
  272. speed = U2C_I2C_SPEED_2KHZ;
  273. frequency = U2C_I2C_FREQ(speed);
  274. }
  275. dev_info(&dev->interface->dev,
  276. "Diolan U2C at USB bus %03d address %03d speed %d Hz\n",
  277. dev->usb_dev->bus->busnum, dev->usb_dev->devnum, frequency);
  278. diolan_flush_input(dev);
  279. diolan_fw_version(dev);
  280. diolan_get_serial(dev);
  281. /* Set I2C speed */
  282. ret = diolan_set_speed(dev, speed);
  283. if (ret < 0)
  284. return ret;
  285. /* Configure I2C clock synchronization */
  286. ret = diolan_set_clock_synch(dev, speed != U2C_I2C_SPEED_FAST);
  287. if (ret < 0)
  288. return ret;
  289. if (speed != U2C_I2C_SPEED_FAST)
  290. ret = diolan_set_clock_synch_timeout(dev, DIOLAN_SYNC_TIMEOUT);
  291. return ret;
  292. }
  293. /* i2c layer */
  294. static int diolan_usb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
  295. int num)
  296. {
  297. struct i2c_diolan_u2c *dev = i2c_get_adapdata(adapter);
  298. struct i2c_msg *pmsg;
  299. int i, j;
  300. int ret, sret;
  301. ret = diolan_i2c_start(dev);
  302. if (ret < 0)
  303. return ret;
  304. for (i = 0; i < num; i++) {
  305. pmsg = &msgs[i];
  306. if (i) {
  307. ret = diolan_i2c_repeated_start(dev);
  308. if (ret < 0)
  309. goto abort;
  310. }
  311. if (pmsg->flags & I2C_M_RD) {
  312. ret =
  313. diolan_i2c_put_byte_ack(dev, (pmsg->addr << 1) | 1);
  314. if (ret < 0)
  315. goto abort;
  316. for (j = 0; j < pmsg->len; j++) {
  317. u8 byte;
  318. bool ack = j < pmsg->len - 1;
  319. /*
  320. * Don't send NACK if this is the first byte
  321. * of a SMBUS_BLOCK message.
  322. */
  323. if (j == 0 && (pmsg->flags & I2C_M_RECV_LEN))
  324. ack = true;
  325. ret = diolan_i2c_get_byte_ack(dev, ack, &byte);
  326. if (ret < 0)
  327. goto abort;
  328. /*
  329. * Adjust count if first received byte is length
  330. */
  331. if (j == 0 && (pmsg->flags & I2C_M_RECV_LEN)) {
  332. if (byte == 0
  333. || byte > I2C_SMBUS_BLOCK_MAX) {
  334. ret = -EPROTO;
  335. goto abort;
  336. }
  337. pmsg->len += byte;
  338. }
  339. pmsg->buf[j] = byte;
  340. }
  341. } else {
  342. ret = diolan_i2c_put_byte_ack(dev, pmsg->addr << 1);
  343. if (ret < 0)
  344. goto abort;
  345. for (j = 0; j < pmsg->len; j++) {
  346. ret = diolan_i2c_put_byte_ack(dev,
  347. pmsg->buf[j]);
  348. if (ret < 0)
  349. goto abort;
  350. }
  351. }
  352. }
  353. ret = num;
  354. abort:
  355. sret = diolan_i2c_stop(dev);
  356. if (sret < 0 && ret >= 0)
  357. ret = sret;
  358. return ret;
  359. }
  360. /*
  361. * Return list of supported functionality.
  362. */
  363. static u32 diolan_usb_func(struct i2c_adapter *a)
  364. {
  365. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  366. I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_FUNC_SMBUS_BLOCK_PROC_CALL;
  367. }
  368. static const struct i2c_algorithm diolan_usb_algorithm = {
  369. .master_xfer = diolan_usb_xfer,
  370. .functionality = diolan_usb_func,
  371. };
  372. /* device layer */
  373. static const struct usb_device_id diolan_u2c_table[] = {
  374. { USB_DEVICE(USB_VENDOR_ID_DIOLAN, USB_DEVICE_ID_DIOLAN_U2C) },
  375. { }
  376. };
  377. MODULE_DEVICE_TABLE(usb, diolan_u2c_table);
  378. static void diolan_u2c_free(struct i2c_diolan_u2c *dev)
  379. {
  380. usb_put_dev(dev->usb_dev);
  381. kfree(dev);
  382. }
  383. static int diolan_u2c_probe(struct usb_interface *interface,
  384. const struct usb_device_id *id)
  385. {
  386. struct usb_host_interface *hostif = interface->cur_altsetting;
  387. struct i2c_diolan_u2c *dev;
  388. int ret;
  389. if (hostif->desc.bInterfaceNumber != 0
  390. || hostif->desc.bNumEndpoints < 2)
  391. return -ENODEV;
  392. /* allocate memory for our device state and initialize it */
  393. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  394. if (dev == NULL) {
  395. ret = -ENOMEM;
  396. goto error;
  397. }
  398. dev->ep_out = hostif->endpoint[0].desc.bEndpointAddress;
  399. dev->ep_in = hostif->endpoint[1].desc.bEndpointAddress;
  400. dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));
  401. dev->interface = interface;
  402. /* save our data pointer in this interface device */
  403. usb_set_intfdata(interface, dev);
  404. /* setup i2c adapter description */
  405. dev->adapter.owner = THIS_MODULE;
  406. dev->adapter.class = I2C_CLASS_HWMON;
  407. dev->adapter.algo = &diolan_usb_algorithm;
  408. i2c_set_adapdata(&dev->adapter, dev);
  409. snprintf(dev->adapter.name, sizeof(dev->adapter.name),
  410. DRIVER_NAME " at bus %03d device %03d",
  411. dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
  412. dev->adapter.dev.parent = &dev->interface->dev;
  413. /* initialize diolan i2c interface */
  414. ret = diolan_init(dev);
  415. if (ret < 0) {
  416. dev_err(&interface->dev, "failed to initialize adapter\n");
  417. goto error_free;
  418. }
  419. /* and finally attach to i2c layer */
  420. ret = i2c_add_adapter(&dev->adapter);
  421. if (ret < 0) {
  422. dev_err(&interface->dev, "failed to add I2C adapter\n");
  423. goto error_free;
  424. }
  425. dev_dbg(&interface->dev, "connected " DRIVER_NAME "\n");
  426. return 0;
  427. error_free:
  428. usb_set_intfdata(interface, NULL);
  429. diolan_u2c_free(dev);
  430. error:
  431. return ret;
  432. }
  433. static void diolan_u2c_disconnect(struct usb_interface *interface)
  434. {
  435. struct i2c_diolan_u2c *dev = usb_get_intfdata(interface);
  436. i2c_del_adapter(&dev->adapter);
  437. usb_set_intfdata(interface, NULL);
  438. diolan_u2c_free(dev);
  439. dev_dbg(&interface->dev, "disconnected\n");
  440. }
  441. static struct usb_driver diolan_u2c_driver = {
  442. .name = DRIVER_NAME,
  443. .probe = diolan_u2c_probe,
  444. .disconnect = diolan_u2c_disconnect,
  445. .id_table = diolan_u2c_table,
  446. };
  447. module_usb_driver(diolan_u2c_driver);
  448. MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
  449. MODULE_DESCRIPTION(DRIVER_NAME " driver");
  450. MODULE_LICENSE("GPL");