i2c-cp2615.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * i2c support for Silicon Labs' CP2615 Digital Audio Bridge
  4. *
  5. * (c) 2021, Bence Csókás <[email protected]>
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/i2c.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/string.h>
  12. #include <linux/usb.h>
  13. /** CP2615 I/O Protocol implementation */
  14. #define CP2615_VID 0x10c4
  15. #define CP2615_PID 0xeac1
  16. #define IOP_EP_IN 0x82
  17. #define IOP_EP_OUT 0x02
  18. #define IOP_IFN 1
  19. #define IOP_ALTSETTING 2
  20. #define MAX_IOP_SIZE 64
  21. #define MAX_IOP_PAYLOAD_SIZE (MAX_IOP_SIZE - 6)
  22. #define MAX_I2C_SIZE (MAX_IOP_PAYLOAD_SIZE - 4)
  23. enum cp2615_iop_msg_type {
  24. iop_GetAccessoryInfo = 0xD100,
  25. iop_AccessoryInfo = 0xA100,
  26. iop_GetPortConfiguration = 0xD203,
  27. iop_PortConfiguration = 0xA203,
  28. iop_DoI2cTransfer = 0xD400,
  29. iop_I2cTransferResult = 0xA400,
  30. iop_GetSerialState = 0xD501,
  31. iop_SerialState = 0xA501
  32. };
  33. struct __packed cp2615_iop_msg {
  34. __be16 preamble, length, msg;
  35. u8 data[MAX_IOP_PAYLOAD_SIZE];
  36. };
  37. #define PART_ID_A01 0x1400
  38. #define PART_ID_A02 0x1500
  39. struct __packed cp2615_iop_accessory_info {
  40. __be16 part_id, option_id, proto_ver;
  41. };
  42. struct __packed cp2615_i2c_transfer {
  43. u8 tag, i2caddr, read_len, write_len;
  44. u8 data[MAX_I2C_SIZE];
  45. };
  46. /* Possible values for struct cp2615_i2c_transfer_result.status */
  47. enum cp2615_i2c_status {
  48. /* Writing to the internal EEPROM failed, because it is locked */
  49. CP2615_CFG_LOCKED = -6,
  50. /* read_len or write_len out of range */
  51. CP2615_INVALID_PARAM = -4,
  52. /* I2C slave did not ACK in time */
  53. CP2615_TIMEOUT,
  54. /* I2C bus busy */
  55. CP2615_BUS_BUSY,
  56. /* I2C bus error (ie. device NAK'd the request) */
  57. CP2615_BUS_ERROR,
  58. CP2615_SUCCESS
  59. };
  60. struct __packed cp2615_i2c_transfer_result {
  61. u8 tag, i2caddr;
  62. s8 status;
  63. u8 read_len;
  64. u8 data[MAX_I2C_SIZE];
  65. };
  66. static int cp2615_init_iop_msg(struct cp2615_iop_msg *ret, enum cp2615_iop_msg_type msg,
  67. const void *data, size_t data_len)
  68. {
  69. if (data_len > MAX_IOP_PAYLOAD_SIZE)
  70. return -EFBIG;
  71. if (!ret)
  72. return -EINVAL;
  73. ret->preamble = 0x2A2A;
  74. ret->length = htons(data_len + 6);
  75. ret->msg = htons(msg);
  76. if (data && data_len)
  77. memcpy(&ret->data, data, data_len);
  78. return 0;
  79. }
  80. static int cp2615_init_i2c_msg(struct cp2615_iop_msg *ret, const struct cp2615_i2c_transfer *data)
  81. {
  82. return cp2615_init_iop_msg(ret, iop_DoI2cTransfer, data, 4 + data->write_len);
  83. }
  84. /* Translates status codes to Linux errno's */
  85. static int cp2615_check_status(enum cp2615_i2c_status status)
  86. {
  87. switch (status) {
  88. case CP2615_SUCCESS:
  89. return 0;
  90. case CP2615_BUS_ERROR:
  91. return -ENXIO;
  92. case CP2615_BUS_BUSY:
  93. return -EAGAIN;
  94. case CP2615_TIMEOUT:
  95. return -ETIMEDOUT;
  96. case CP2615_INVALID_PARAM:
  97. return -EINVAL;
  98. case CP2615_CFG_LOCKED:
  99. return -EPERM;
  100. }
  101. /* Unknown error code */
  102. return -EPROTO;
  103. }
  104. /** Driver code */
  105. static int
  106. cp2615_i2c_send(struct usb_interface *usbif, struct cp2615_i2c_transfer *i2c_w)
  107. {
  108. struct cp2615_iop_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  109. struct usb_device *usbdev = interface_to_usbdev(usbif);
  110. int res = cp2615_init_i2c_msg(msg, i2c_w);
  111. if (!res)
  112. res = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, IOP_EP_OUT),
  113. msg, ntohs(msg->length), NULL, 0);
  114. kfree(msg);
  115. return res;
  116. }
  117. static int
  118. cp2615_i2c_recv(struct usb_interface *usbif, unsigned char tag, void *buf)
  119. {
  120. struct usb_device *usbdev = interface_to_usbdev(usbif);
  121. struct cp2615_iop_msg *msg;
  122. struct cp2615_i2c_transfer_result *i2c_r;
  123. int res;
  124. msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  125. if (!msg)
  126. return -ENOMEM;
  127. res = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, IOP_EP_IN), msg,
  128. sizeof(struct cp2615_iop_msg), NULL, 0);
  129. if (res < 0) {
  130. kfree(msg);
  131. return res;
  132. }
  133. i2c_r = (struct cp2615_i2c_transfer_result *)&msg->data;
  134. if (msg->msg != htons(iop_I2cTransferResult) || i2c_r->tag != tag) {
  135. kfree(msg);
  136. return -EIO;
  137. }
  138. res = cp2615_check_status(i2c_r->status);
  139. if (!res)
  140. memcpy(buf, &i2c_r->data, i2c_r->read_len);
  141. kfree(msg);
  142. return res;
  143. }
  144. /* Checks if the IOP is functional by querying the part's ID */
  145. static int cp2615_check_iop(struct usb_interface *usbif)
  146. {
  147. struct cp2615_iop_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  148. struct cp2615_iop_accessory_info *info = (struct cp2615_iop_accessory_info *)&msg->data;
  149. struct usb_device *usbdev = interface_to_usbdev(usbif);
  150. int res = cp2615_init_iop_msg(msg, iop_GetAccessoryInfo, NULL, 0);
  151. if (res)
  152. goto out;
  153. res = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, IOP_EP_OUT),
  154. msg, ntohs(msg->length), NULL, 0);
  155. if (res)
  156. goto out;
  157. res = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, IOP_EP_IN),
  158. msg, sizeof(struct cp2615_iop_msg), NULL, 0);
  159. if (res)
  160. goto out;
  161. if (msg->msg != htons(iop_AccessoryInfo)) {
  162. res = -EIO;
  163. goto out;
  164. }
  165. switch (ntohs(info->part_id)) {
  166. case PART_ID_A01:
  167. dev_dbg(&usbif->dev, "Found A01 part. (WARNING: errata exists!)\n");
  168. break;
  169. case PART_ID_A02:
  170. dev_dbg(&usbif->dev, "Found good A02 part.\n");
  171. break;
  172. default:
  173. dev_warn(&usbif->dev, "Unknown part ID %04X\n", ntohs(info->part_id));
  174. }
  175. out:
  176. kfree(msg);
  177. return res;
  178. }
  179. static int
  180. cp2615_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  181. {
  182. struct usb_interface *usbif = adap->algo_data;
  183. int i = 0, ret = 0;
  184. struct i2c_msg *msg;
  185. struct cp2615_i2c_transfer i2c_w = {0};
  186. dev_dbg(&usbif->dev, "Doing %d I2C transactions\n", num);
  187. for (; !ret && i < num; i++) {
  188. msg = &msgs[i];
  189. i2c_w.tag = 0xdd;
  190. i2c_w.i2caddr = i2c_8bit_addr_from_msg(msg);
  191. if (msg->flags & I2C_M_RD) {
  192. i2c_w.read_len = msg->len;
  193. i2c_w.write_len = 0;
  194. } else {
  195. i2c_w.read_len = 0;
  196. i2c_w.write_len = msg->len;
  197. memcpy(&i2c_w.data, msg->buf, i2c_w.write_len);
  198. }
  199. ret = cp2615_i2c_send(usbif, &i2c_w);
  200. if (ret)
  201. break;
  202. ret = cp2615_i2c_recv(usbif, i2c_w.tag, msg->buf);
  203. }
  204. if (ret < 0)
  205. return ret;
  206. return i;
  207. }
  208. static u32
  209. cp2615_i2c_func(struct i2c_adapter *adap)
  210. {
  211. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  212. }
  213. static const struct i2c_algorithm cp2615_i2c_algo = {
  214. .master_xfer = cp2615_i2c_master_xfer,
  215. .functionality = cp2615_i2c_func,
  216. };
  217. /*
  218. * This chip has some limitations: one is that the USB endpoint
  219. * can only receive 64 bytes/transfer, that leaves 54 bytes for
  220. * the I2C transfer. On top of that, EITHER read_len OR write_len
  221. * may be zero, but not both. If both are non-zero, the adapter
  222. * issues a write followed by a read. And the chip does not
  223. * support repeated START between the write and read phases.
  224. */
  225. static struct i2c_adapter_quirks cp2615_i2c_quirks = {
  226. .max_write_len = MAX_I2C_SIZE,
  227. .max_read_len = MAX_I2C_SIZE,
  228. .flags = I2C_AQ_COMB_WRITE_THEN_READ | I2C_AQ_NO_ZERO_LEN | I2C_AQ_NO_REP_START,
  229. .max_comb_1st_msg_len = MAX_I2C_SIZE,
  230. .max_comb_2nd_msg_len = MAX_I2C_SIZE
  231. };
  232. static void
  233. cp2615_i2c_remove(struct usb_interface *usbif)
  234. {
  235. struct i2c_adapter *adap = usb_get_intfdata(usbif);
  236. usb_set_intfdata(usbif, NULL);
  237. i2c_del_adapter(adap);
  238. }
  239. static int
  240. cp2615_i2c_probe(struct usb_interface *usbif, const struct usb_device_id *id)
  241. {
  242. int ret = 0;
  243. struct i2c_adapter *adap;
  244. struct usb_device *usbdev = interface_to_usbdev(usbif);
  245. ret = usb_set_interface(usbdev, IOP_IFN, IOP_ALTSETTING);
  246. if (ret)
  247. return ret;
  248. ret = cp2615_check_iop(usbif);
  249. if (ret)
  250. return ret;
  251. adap = devm_kzalloc(&usbif->dev, sizeof(struct i2c_adapter), GFP_KERNEL);
  252. if (!adap)
  253. return -ENOMEM;
  254. strncpy(adap->name, usbdev->serial, sizeof(adap->name) - 1);
  255. adap->owner = THIS_MODULE;
  256. adap->dev.parent = &usbif->dev;
  257. adap->dev.of_node = usbif->dev.of_node;
  258. adap->timeout = HZ;
  259. adap->algo = &cp2615_i2c_algo;
  260. adap->quirks = &cp2615_i2c_quirks;
  261. adap->algo_data = usbif;
  262. ret = i2c_add_adapter(adap);
  263. if (ret)
  264. return ret;
  265. usb_set_intfdata(usbif, adap);
  266. return 0;
  267. }
  268. static const struct usb_device_id id_table[] = {
  269. { USB_DEVICE_INTERFACE_NUMBER(CP2615_VID, CP2615_PID, IOP_IFN) },
  270. { }
  271. };
  272. MODULE_DEVICE_TABLE(usb, id_table);
  273. static struct usb_driver cp2615_i2c_driver = {
  274. .name = "i2c-cp2615",
  275. .probe = cp2615_i2c_probe,
  276. .disconnect = cp2615_i2c_remove,
  277. .id_table = id_table,
  278. };
  279. module_usb_driver(cp2615_i2c_driver);
  280. MODULE_AUTHOR("Bence Csókás <[email protected]>");
  281. MODULE_DESCRIPTION("CP2615 I2C bus driver");
  282. MODULE_LICENSE("GPL");