i2c-diolan-u2c.c 13 KB

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