i2c-viperboard.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Nano River Technologies viperboard i2c master driver
  3. *
  4. * (C) 2012 by Lemonage GmbH
  5. * Author: Lars Poeschel <poeschel@lemonage.de>
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/types.h>
  19. #include <linux/mutex.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/usb.h>
  22. #include <linux/i2c.h>
  23. #include <linux/mfd/viperboard.h>
  24. struct vprbrd_i2c {
  25. struct i2c_adapter i2c;
  26. u8 bus_freq_param;
  27. };
  28. /* i2c bus frequency module parameter */
  29. static u8 i2c_bus_param;
  30. static unsigned int i2c_bus_freq = 100;
  31. module_param(i2c_bus_freq, int, 0);
  32. MODULE_PARM_DESC(i2c_bus_freq,
  33. "i2c bus frequency in khz (default is 100) valid values: 10, 100, 200, 400, 1000, 3000, 6000");
  34. static int vprbrd_i2c_status(struct i2c_adapter *i2c,
  35. struct vprbrd_i2c_status *status, bool prev_error)
  36. {
  37. u16 bytes_xfer;
  38. int ret;
  39. struct vprbrd *vb = (struct vprbrd *)i2c->algo_data;
  40. /* check for protocol error */
  41. bytes_xfer = sizeof(struct vprbrd_i2c_status);
  42. ret = usb_control_msg(vb->usb_dev, usb_rcvctrlpipe(vb->usb_dev, 0),
  43. VPRBRD_USB_REQUEST_I2C, VPRBRD_USB_TYPE_IN, 0x0000, 0x0000,
  44. status, bytes_xfer, VPRBRD_USB_TIMEOUT_MS);
  45. if (ret != bytes_xfer)
  46. prev_error = true;
  47. if (prev_error) {
  48. dev_err(&i2c->dev, "failure in usb communication\n");
  49. return -EREMOTEIO;
  50. }
  51. dev_dbg(&i2c->dev, " status = %d\n", status->status);
  52. if (status->status != 0x00) {
  53. dev_err(&i2c->dev, "failure: i2c protocol error\n");
  54. return -EPROTO;
  55. }
  56. return 0;
  57. }
  58. static int vprbrd_i2c_receive(struct usb_device *usb_dev,
  59. struct vprbrd_i2c_read_msg *rmsg, int bytes_xfer)
  60. {
  61. int ret, bytes_actual;
  62. int error = 0;
  63. /* send the read request */
  64. ret = usb_bulk_msg(usb_dev,
  65. usb_sndbulkpipe(usb_dev, VPRBRD_EP_OUT), rmsg,
  66. sizeof(struct vprbrd_i2c_read_hdr), &bytes_actual,
  67. VPRBRD_USB_TIMEOUT_MS);
  68. if ((ret < 0)
  69. || (bytes_actual != sizeof(struct vprbrd_i2c_read_hdr))) {
  70. dev_err(&usb_dev->dev, "failure transmitting usb\n");
  71. error = -EREMOTEIO;
  72. }
  73. /* read the actual data */
  74. ret = usb_bulk_msg(usb_dev,
  75. usb_rcvbulkpipe(usb_dev, VPRBRD_EP_IN), rmsg,
  76. bytes_xfer, &bytes_actual, VPRBRD_USB_TIMEOUT_MS);
  77. if ((ret < 0) || (bytes_xfer != bytes_actual)) {
  78. dev_err(&usb_dev->dev, "failure receiving usb\n");
  79. error = -EREMOTEIO;
  80. }
  81. return error;
  82. }
  83. static int vprbrd_i2c_addr(struct usb_device *usb_dev,
  84. struct vprbrd_i2c_addr_msg *amsg)
  85. {
  86. int ret, bytes_actual;
  87. ret = usb_bulk_msg(usb_dev,
  88. usb_sndbulkpipe(usb_dev, VPRBRD_EP_OUT), amsg,
  89. sizeof(struct vprbrd_i2c_addr_msg), &bytes_actual,
  90. VPRBRD_USB_TIMEOUT_MS);
  91. if ((ret < 0) ||
  92. (sizeof(struct vprbrd_i2c_addr_msg) != bytes_actual)) {
  93. dev_err(&usb_dev->dev, "failure transmitting usb\n");
  94. return -EREMOTEIO;
  95. }
  96. return 0;
  97. }
  98. static int vprbrd_i2c_read(struct vprbrd *vb, struct i2c_msg *msg)
  99. {
  100. int ret;
  101. u16 remain_len, len1, len2, start = 0x0000;
  102. struct vprbrd_i2c_read_msg *rmsg =
  103. (struct vprbrd_i2c_read_msg *)vb->buf;
  104. remain_len = msg->len;
  105. rmsg->header.cmd = VPRBRD_I2C_CMD_READ;
  106. while (remain_len > 0) {
  107. rmsg->header.addr = cpu_to_le16(start + 0x4000);
  108. if (remain_len <= 255) {
  109. len1 = remain_len;
  110. len2 = 0x00;
  111. rmsg->header.len0 = remain_len;
  112. rmsg->header.len1 = 0x00;
  113. rmsg->header.len2 = 0x00;
  114. rmsg->header.len3 = 0x00;
  115. rmsg->header.len4 = 0x00;
  116. rmsg->header.len5 = 0x00;
  117. remain_len = 0;
  118. } else if (remain_len <= 510) {
  119. len1 = remain_len;
  120. len2 = 0x00;
  121. rmsg->header.len0 = remain_len - 255;
  122. rmsg->header.len1 = 0xff;
  123. rmsg->header.len2 = 0x00;
  124. rmsg->header.len3 = 0x00;
  125. rmsg->header.len4 = 0x00;
  126. rmsg->header.len5 = 0x00;
  127. remain_len = 0;
  128. } else if (remain_len <= 512) {
  129. len1 = remain_len;
  130. len2 = 0x00;
  131. rmsg->header.len0 = remain_len - 510;
  132. rmsg->header.len1 = 0xff;
  133. rmsg->header.len2 = 0xff;
  134. rmsg->header.len3 = 0x00;
  135. rmsg->header.len4 = 0x00;
  136. rmsg->header.len5 = 0x00;
  137. remain_len = 0;
  138. } else if (remain_len <= 767) {
  139. len1 = 512;
  140. len2 = remain_len - 512;
  141. rmsg->header.len0 = 0x02;
  142. rmsg->header.len1 = 0xff;
  143. rmsg->header.len2 = 0xff;
  144. rmsg->header.len3 = remain_len - 512;
  145. rmsg->header.len4 = 0x00;
  146. rmsg->header.len5 = 0x00;
  147. remain_len = 0;
  148. } else if (remain_len <= 1022) {
  149. len1 = 512;
  150. len2 = remain_len - 512;
  151. rmsg->header.len0 = 0x02;
  152. rmsg->header.len1 = 0xff;
  153. rmsg->header.len2 = 0xff;
  154. rmsg->header.len3 = remain_len - 767;
  155. rmsg->header.len4 = 0xff;
  156. rmsg->header.len5 = 0x00;
  157. remain_len = 0;
  158. } else if (remain_len <= 1024) {
  159. len1 = 512;
  160. len2 = remain_len - 512;
  161. rmsg->header.len0 = 0x02;
  162. rmsg->header.len1 = 0xff;
  163. rmsg->header.len2 = 0xff;
  164. rmsg->header.len3 = remain_len - 1022;
  165. rmsg->header.len4 = 0xff;
  166. rmsg->header.len5 = 0xff;
  167. remain_len = 0;
  168. } else {
  169. len1 = 512;
  170. len2 = 512;
  171. rmsg->header.len0 = 0x02;
  172. rmsg->header.len1 = 0xff;
  173. rmsg->header.len2 = 0xff;
  174. rmsg->header.len3 = 0x02;
  175. rmsg->header.len4 = 0xff;
  176. rmsg->header.len5 = 0xff;
  177. remain_len -= 1024;
  178. start += 1024;
  179. }
  180. rmsg->header.tf1 = cpu_to_le16(len1);
  181. rmsg->header.tf2 = cpu_to_le16(len2);
  182. /* first read transfer */
  183. ret = vprbrd_i2c_receive(vb->usb_dev, rmsg, len1);
  184. if (ret < 0)
  185. return ret;
  186. /* copy the received data */
  187. memcpy(msg->buf + start, rmsg, len1);
  188. /* second read transfer if neccessary */
  189. if (len2 > 0) {
  190. ret = vprbrd_i2c_receive(vb->usb_dev, rmsg, len2);
  191. if (ret < 0)
  192. return ret;
  193. /* copy the received data */
  194. memcpy(msg->buf + start + 512, rmsg, len2);
  195. }
  196. }
  197. return 0;
  198. }
  199. static int vprbrd_i2c_write(struct vprbrd *vb, struct i2c_msg *msg)
  200. {
  201. int ret, bytes_actual;
  202. u16 remain_len, bytes_xfer,
  203. start = 0x0000;
  204. struct vprbrd_i2c_write_msg *wmsg =
  205. (struct vprbrd_i2c_write_msg *)vb->buf;
  206. remain_len = msg->len;
  207. wmsg->header.cmd = VPRBRD_I2C_CMD_WRITE;
  208. wmsg->header.last = 0x00;
  209. wmsg->header.chan = 0x00;
  210. wmsg->header.spi = 0x0000;
  211. while (remain_len > 0) {
  212. wmsg->header.addr = cpu_to_le16(start + 0x4000);
  213. if (remain_len > 503) {
  214. wmsg->header.len1 = 0xff;
  215. wmsg->header.len2 = 0xf8;
  216. remain_len -= 503;
  217. bytes_xfer = 503 + sizeof(struct vprbrd_i2c_write_hdr);
  218. start += 503;
  219. } else if (remain_len > 255) {
  220. wmsg->header.len1 = 0xff;
  221. wmsg->header.len2 = (remain_len - 255);
  222. bytes_xfer = remain_len +
  223. sizeof(struct vprbrd_i2c_write_hdr);
  224. remain_len = 0;
  225. } else {
  226. wmsg->header.len1 = remain_len;
  227. wmsg->header.len2 = 0x00;
  228. bytes_xfer = remain_len +
  229. sizeof(struct vprbrd_i2c_write_hdr);
  230. remain_len = 0;
  231. }
  232. memcpy(wmsg->data, msg->buf + start,
  233. bytes_xfer - sizeof(struct vprbrd_i2c_write_hdr));
  234. ret = usb_bulk_msg(vb->usb_dev,
  235. usb_sndbulkpipe(vb->usb_dev,
  236. VPRBRD_EP_OUT), wmsg,
  237. bytes_xfer, &bytes_actual, VPRBRD_USB_TIMEOUT_MS);
  238. if ((ret < 0) || (bytes_xfer != bytes_actual))
  239. return -EREMOTEIO;
  240. }
  241. return 0;
  242. }
  243. static int vprbrd_i2c_xfer(struct i2c_adapter *i2c, struct i2c_msg *msgs,
  244. int num)
  245. {
  246. struct i2c_msg *pmsg;
  247. int i, ret,
  248. error = 0;
  249. struct vprbrd *vb = (struct vprbrd *)i2c->algo_data;
  250. struct vprbrd_i2c_addr_msg *amsg =
  251. (struct vprbrd_i2c_addr_msg *)vb->buf;
  252. struct vprbrd_i2c_status *smsg = (struct vprbrd_i2c_status *)vb->buf;
  253. dev_dbg(&i2c->dev, "master xfer %d messages:\n", num);
  254. for (i = 0 ; i < num ; i++) {
  255. pmsg = &msgs[i];
  256. dev_dbg(&i2c->dev,
  257. " %d: %s (flags %d) %d bytes to 0x%02x\n",
  258. i, pmsg->flags & I2C_M_RD ? "read" : "write",
  259. pmsg->flags, pmsg->len, pmsg->addr);
  260. mutex_lock(&vb->lock);
  261. /* directly send the message */
  262. if (pmsg->flags & I2C_M_RD) {
  263. /* read data */
  264. amsg->cmd = VPRBRD_I2C_CMD_ADDR;
  265. amsg->unknown2 = 0x00;
  266. amsg->unknown3 = 0x00;
  267. amsg->addr = pmsg->addr;
  268. amsg->unknown1 = 0x01;
  269. amsg->len = cpu_to_le16(pmsg->len);
  270. /* send the addr and len, we're interested to board */
  271. ret = vprbrd_i2c_addr(vb->usb_dev, amsg);
  272. if (ret < 0)
  273. error = ret;
  274. ret = vprbrd_i2c_read(vb, pmsg);
  275. if (ret < 0)
  276. error = ret;
  277. ret = vprbrd_i2c_status(i2c, smsg, error);
  278. if (ret < 0)
  279. error = ret;
  280. /* in case of protocol error, return the error */
  281. if (error < 0)
  282. goto error;
  283. } else {
  284. /* write data */
  285. ret = vprbrd_i2c_write(vb, pmsg);
  286. amsg->cmd = VPRBRD_I2C_CMD_ADDR;
  287. amsg->unknown2 = 0x00;
  288. amsg->unknown3 = 0x00;
  289. amsg->addr = pmsg->addr;
  290. amsg->unknown1 = 0x00;
  291. amsg->len = cpu_to_le16(pmsg->len);
  292. /* send the addr, the data goes to to board */
  293. ret = vprbrd_i2c_addr(vb->usb_dev, amsg);
  294. if (ret < 0)
  295. error = ret;
  296. ret = vprbrd_i2c_status(i2c, smsg, error);
  297. if (ret < 0)
  298. error = ret;
  299. if (error < 0)
  300. goto error;
  301. }
  302. mutex_unlock(&vb->lock);
  303. }
  304. return 0;
  305. error:
  306. mutex_unlock(&vb->lock);
  307. return error;
  308. }
  309. static u32 vprbrd_i2c_func(struct i2c_adapter *i2c)
  310. {
  311. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  312. }
  313. /* This is the actual algorithm we define */
  314. static const struct i2c_algorithm vprbrd_algorithm = {
  315. .master_xfer = vprbrd_i2c_xfer,
  316. .functionality = vprbrd_i2c_func,
  317. };
  318. static struct i2c_adapter_quirks vprbrd_quirks = {
  319. .max_read_len = 2048,
  320. .max_write_len = 2048,
  321. };
  322. static int vprbrd_i2c_probe(struct platform_device *pdev)
  323. {
  324. struct vprbrd *vb = dev_get_drvdata(pdev->dev.parent);
  325. struct vprbrd_i2c *vb_i2c;
  326. int ret;
  327. int pipe;
  328. vb_i2c = devm_kzalloc(&pdev->dev, sizeof(*vb_i2c), GFP_KERNEL);
  329. if (vb_i2c == NULL)
  330. return -ENOMEM;
  331. /* setup i2c adapter description */
  332. vb_i2c->i2c.owner = THIS_MODULE;
  333. vb_i2c->i2c.class = I2C_CLASS_HWMON;
  334. vb_i2c->i2c.algo = &vprbrd_algorithm;
  335. vb_i2c->i2c.quirks = &vprbrd_quirks;
  336. vb_i2c->i2c.algo_data = vb;
  337. /* save the param in usb capabable memory */
  338. vb_i2c->bus_freq_param = i2c_bus_param;
  339. snprintf(vb_i2c->i2c.name, sizeof(vb_i2c->i2c.name),
  340. "viperboard at bus %03d device %03d",
  341. vb->usb_dev->bus->busnum, vb->usb_dev->devnum);
  342. /* setting the bus frequency */
  343. if ((i2c_bus_param <= VPRBRD_I2C_FREQ_10KHZ)
  344. && (i2c_bus_param >= VPRBRD_I2C_FREQ_6MHZ)) {
  345. pipe = usb_sndctrlpipe(vb->usb_dev, 0);
  346. ret = usb_control_msg(vb->usb_dev, pipe,
  347. VPRBRD_USB_REQUEST_I2C_FREQ, VPRBRD_USB_TYPE_OUT,
  348. 0x0000, 0x0000, &vb_i2c->bus_freq_param, 1,
  349. VPRBRD_USB_TIMEOUT_MS);
  350. if (ret != 1) {
  351. dev_err(&pdev->dev, "failure setting i2c_bus_freq to %d\n",
  352. i2c_bus_freq);
  353. return -EIO;
  354. }
  355. } else {
  356. dev_err(&pdev->dev,
  357. "invalid i2c_bus_freq setting:%d\n", i2c_bus_freq);
  358. return -EIO;
  359. }
  360. vb_i2c->i2c.dev.parent = &pdev->dev;
  361. /* attach to i2c layer */
  362. i2c_add_adapter(&vb_i2c->i2c);
  363. platform_set_drvdata(pdev, vb_i2c);
  364. return 0;
  365. }
  366. static int vprbrd_i2c_remove(struct platform_device *pdev)
  367. {
  368. struct vprbrd_i2c *vb_i2c = platform_get_drvdata(pdev);
  369. i2c_del_adapter(&vb_i2c->i2c);
  370. return 0;
  371. }
  372. static struct platform_driver vprbrd_i2c_driver = {
  373. .driver.name = "viperboard-i2c",
  374. .driver.owner = THIS_MODULE,
  375. .probe = vprbrd_i2c_probe,
  376. .remove = vprbrd_i2c_remove,
  377. };
  378. static int __init vprbrd_i2c_init(void)
  379. {
  380. switch (i2c_bus_freq) {
  381. case 6000:
  382. i2c_bus_param = VPRBRD_I2C_FREQ_6MHZ;
  383. break;
  384. case 3000:
  385. i2c_bus_param = VPRBRD_I2C_FREQ_3MHZ;
  386. break;
  387. case 1000:
  388. i2c_bus_param = VPRBRD_I2C_FREQ_1MHZ;
  389. break;
  390. case 400:
  391. i2c_bus_param = VPRBRD_I2C_FREQ_400KHZ;
  392. break;
  393. case 200:
  394. i2c_bus_param = VPRBRD_I2C_FREQ_200KHZ;
  395. break;
  396. case 100:
  397. i2c_bus_param = VPRBRD_I2C_FREQ_100KHZ;
  398. break;
  399. case 10:
  400. i2c_bus_param = VPRBRD_I2C_FREQ_10KHZ;
  401. break;
  402. default:
  403. pr_warn("invalid i2c_bus_freq (%d)\n", i2c_bus_freq);
  404. i2c_bus_param = VPRBRD_I2C_FREQ_100KHZ;
  405. }
  406. return platform_driver_register(&vprbrd_i2c_driver);
  407. }
  408. subsys_initcall(vprbrd_i2c_init);
  409. static void __exit vprbrd_i2c_exit(void)
  410. {
  411. platform_driver_unregister(&vprbrd_i2c_driver);
  412. }
  413. module_exit(vprbrd_i2c_exit);
  414. MODULE_AUTHOR("Lars Poeschel <poeschel@lemonage.de>");
  415. MODULE_DESCRIPTION("I2C master driver for Nano River Techs Viperboard");
  416. MODULE_LICENSE("GPL");
  417. MODULE_ALIAS("platform:viperboard-i2c");