i2c-dln2.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Driver for the Diolan DLN-2 USB-I2C adapter
  3. *
  4. * Copyright (c) 2014 Intel Corporation
  5. *
  6. * Derived from:
  7. * i2c-diolan-u2c.c
  8. * Copyright (c) 2010-2011 Ericsson AB
  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/module.h>
  16. #include <linux/types.h>
  17. #include <linux/slab.h>
  18. #include <linux/i2c.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/mfd/dln2.h>
  21. #define DLN2_I2C_MODULE_ID 0x03
  22. #define DLN2_I2C_CMD(cmd) DLN2_CMD(cmd, DLN2_I2C_MODULE_ID)
  23. /* I2C commands */
  24. #define DLN2_I2C_GET_PORT_COUNT DLN2_I2C_CMD(0x00)
  25. #define DLN2_I2C_ENABLE DLN2_I2C_CMD(0x01)
  26. #define DLN2_I2C_DISABLE DLN2_I2C_CMD(0x02)
  27. #define DLN2_I2C_IS_ENABLED DLN2_I2C_CMD(0x03)
  28. #define DLN2_I2C_WRITE DLN2_I2C_CMD(0x06)
  29. #define DLN2_I2C_READ DLN2_I2C_CMD(0x07)
  30. #define DLN2_I2C_SCAN_DEVICES DLN2_I2C_CMD(0x08)
  31. #define DLN2_I2C_PULLUP_ENABLE DLN2_I2C_CMD(0x09)
  32. #define DLN2_I2C_PULLUP_DISABLE DLN2_I2C_CMD(0x0A)
  33. #define DLN2_I2C_PULLUP_IS_ENABLED DLN2_I2C_CMD(0x0B)
  34. #define DLN2_I2C_TRANSFER DLN2_I2C_CMD(0x0C)
  35. #define DLN2_I2C_SET_MAX_REPLY_COUNT DLN2_I2C_CMD(0x0D)
  36. #define DLN2_I2C_GET_MAX_REPLY_COUNT DLN2_I2C_CMD(0x0E)
  37. #define DLN2_I2C_MAX_XFER_SIZE 256
  38. #define DLN2_I2C_BUF_SIZE (DLN2_I2C_MAX_XFER_SIZE + 16)
  39. struct dln2_i2c {
  40. struct platform_device *pdev;
  41. struct i2c_adapter adapter;
  42. u8 port;
  43. /*
  44. * Buffer to hold the packet for read or write transfers. One is enough
  45. * since we can't have multiple transfers in parallel on the i2c bus.
  46. */
  47. void *buf;
  48. };
  49. static int dln2_i2c_enable(struct dln2_i2c *dln2, bool enable)
  50. {
  51. u16 cmd;
  52. struct {
  53. u8 port;
  54. } tx;
  55. tx.port = dln2->port;
  56. if (enable)
  57. cmd = DLN2_I2C_ENABLE;
  58. else
  59. cmd = DLN2_I2C_DISABLE;
  60. return dln2_transfer_tx(dln2->pdev, cmd, &tx, sizeof(tx));
  61. }
  62. static int dln2_i2c_write(struct dln2_i2c *dln2, u8 addr,
  63. u8 *data, u16 data_len)
  64. {
  65. int ret;
  66. struct {
  67. u8 port;
  68. u8 addr;
  69. u8 mem_addr_len;
  70. __le32 mem_addr;
  71. __le16 buf_len;
  72. u8 buf[DLN2_I2C_MAX_XFER_SIZE];
  73. } __packed *tx = dln2->buf;
  74. unsigned len;
  75. BUILD_BUG_ON(sizeof(*tx) > DLN2_I2C_BUF_SIZE);
  76. tx->port = dln2->port;
  77. tx->addr = addr;
  78. tx->mem_addr_len = 0;
  79. tx->mem_addr = 0;
  80. tx->buf_len = cpu_to_le16(data_len);
  81. memcpy(tx->buf, data, data_len);
  82. len = sizeof(*tx) + data_len - DLN2_I2C_MAX_XFER_SIZE;
  83. ret = dln2_transfer_tx(dln2->pdev, DLN2_I2C_WRITE, tx, len);
  84. if (ret < 0)
  85. return ret;
  86. return data_len;
  87. }
  88. static int dln2_i2c_read(struct dln2_i2c *dln2, u16 addr, u8 *data,
  89. u16 data_len)
  90. {
  91. int ret;
  92. struct {
  93. u8 port;
  94. u8 addr;
  95. u8 mem_addr_len;
  96. __le32 mem_addr;
  97. __le16 buf_len;
  98. } __packed tx;
  99. struct {
  100. __le16 buf_len;
  101. u8 buf[DLN2_I2C_MAX_XFER_SIZE];
  102. } __packed *rx = dln2->buf;
  103. unsigned rx_len = sizeof(*rx);
  104. BUILD_BUG_ON(sizeof(*rx) > DLN2_I2C_BUF_SIZE);
  105. tx.port = dln2->port;
  106. tx.addr = addr;
  107. tx.mem_addr_len = 0;
  108. tx.mem_addr = 0;
  109. tx.buf_len = cpu_to_le16(data_len);
  110. ret = dln2_transfer(dln2->pdev, DLN2_I2C_READ, &tx, sizeof(tx),
  111. rx, &rx_len);
  112. if (ret < 0)
  113. return ret;
  114. if (rx_len < sizeof(rx->buf_len) + data_len)
  115. return -EPROTO;
  116. if (le16_to_cpu(rx->buf_len) != data_len)
  117. return -EPROTO;
  118. memcpy(data, rx->buf, data_len);
  119. return data_len;
  120. }
  121. static int dln2_i2c_xfer(struct i2c_adapter *adapter,
  122. struct i2c_msg *msgs, int num)
  123. {
  124. struct dln2_i2c *dln2 = i2c_get_adapdata(adapter);
  125. struct i2c_msg *pmsg;
  126. int i;
  127. for (i = 0; i < num; i++) {
  128. int ret;
  129. pmsg = &msgs[i];
  130. if (pmsg->flags & I2C_M_RD) {
  131. ret = dln2_i2c_read(dln2, pmsg->addr, pmsg->buf,
  132. pmsg->len);
  133. if (ret < 0)
  134. return ret;
  135. pmsg->len = ret;
  136. } else {
  137. ret = dln2_i2c_write(dln2, pmsg->addr, pmsg->buf,
  138. pmsg->len);
  139. if (ret != pmsg->len)
  140. return -EPROTO;
  141. }
  142. }
  143. return num;
  144. }
  145. static u32 dln2_i2c_func(struct i2c_adapter *a)
  146. {
  147. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA |
  148. I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
  149. I2C_FUNC_SMBUS_I2C_BLOCK;
  150. }
  151. static const struct i2c_algorithm dln2_i2c_usb_algorithm = {
  152. .master_xfer = dln2_i2c_xfer,
  153. .functionality = dln2_i2c_func,
  154. };
  155. static struct i2c_adapter_quirks dln2_i2c_quirks = {
  156. .max_read_len = DLN2_I2C_MAX_XFER_SIZE,
  157. .max_write_len = DLN2_I2C_MAX_XFER_SIZE,
  158. };
  159. static int dln2_i2c_probe(struct platform_device *pdev)
  160. {
  161. int ret;
  162. struct dln2_i2c *dln2;
  163. struct device *dev = &pdev->dev;
  164. struct dln2_platform_data *pdata = dev_get_platdata(&pdev->dev);
  165. dln2 = devm_kzalloc(dev, sizeof(*dln2), GFP_KERNEL);
  166. if (!dln2)
  167. return -ENOMEM;
  168. dln2->buf = devm_kmalloc(dev, DLN2_I2C_BUF_SIZE, GFP_KERNEL);
  169. if (!dln2->buf)
  170. return -ENOMEM;
  171. dln2->pdev = pdev;
  172. dln2->port = pdata->port;
  173. /* setup i2c adapter description */
  174. dln2->adapter.owner = THIS_MODULE;
  175. dln2->adapter.class = I2C_CLASS_HWMON;
  176. dln2->adapter.algo = &dln2_i2c_usb_algorithm;
  177. dln2->adapter.quirks = &dln2_i2c_quirks;
  178. dln2->adapter.dev.parent = dev;
  179. dln2->adapter.dev.of_node = dev->of_node;
  180. i2c_set_adapdata(&dln2->adapter, dln2);
  181. snprintf(dln2->adapter.name, sizeof(dln2->adapter.name), "%s-%s-%d",
  182. "dln2-i2c", dev_name(pdev->dev.parent), dln2->port);
  183. platform_set_drvdata(pdev, dln2);
  184. /* initialize the i2c interface */
  185. ret = dln2_i2c_enable(dln2, true);
  186. if (ret < 0) {
  187. dev_err(dev, "failed to initialize adapter: %d\n", ret);
  188. return ret;
  189. }
  190. /* and finally attach to i2c layer */
  191. ret = i2c_add_adapter(&dln2->adapter);
  192. if (ret < 0) {
  193. dev_err(dev, "failed to add I2C adapter: %d\n", ret);
  194. goto out_disable;
  195. }
  196. return 0;
  197. out_disable:
  198. dln2_i2c_enable(dln2, false);
  199. return ret;
  200. }
  201. static int dln2_i2c_remove(struct platform_device *pdev)
  202. {
  203. struct dln2_i2c *dln2 = platform_get_drvdata(pdev);
  204. i2c_del_adapter(&dln2->adapter);
  205. dln2_i2c_enable(dln2, false);
  206. return 0;
  207. }
  208. static struct platform_driver dln2_i2c_driver = {
  209. .driver.name = "dln2-i2c",
  210. .probe = dln2_i2c_probe,
  211. .remove = dln2_i2c_remove,
  212. };
  213. module_platform_driver(dln2_i2c_driver);
  214. MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
  215. MODULE_DESCRIPTION("Driver for the Diolan DLN2 I2C master interface");
  216. MODULE_LICENSE("GPL v2");
  217. MODULE_ALIAS("platform:dln2-i2c");