i2c.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Marvell NFC-over-I2C driver: I2C interface related functions
  4. *
  5. * Copyright (C) 2015, Marvell International Ltd.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/i2c.h>
  10. #include <linux/nfc.h>
  11. #include <linux/delay.h>
  12. #include <linux/of_irq.h>
  13. #include <net/nfc/nci.h>
  14. #include <net/nfc/nci_core.h>
  15. #include "nfcmrvl.h"
  16. struct nfcmrvl_i2c_drv_data {
  17. unsigned long flags;
  18. struct device *dev;
  19. struct i2c_client *i2c;
  20. struct nfcmrvl_private *priv;
  21. };
  22. static int nfcmrvl_i2c_read(struct nfcmrvl_i2c_drv_data *drv_data,
  23. struct sk_buff **skb)
  24. {
  25. int ret;
  26. struct nci_ctrl_hdr nci_hdr;
  27. /* Read NCI header to know the payload size */
  28. ret = i2c_master_recv(drv_data->i2c, (u8 *)&nci_hdr, NCI_CTRL_HDR_SIZE);
  29. if (ret != NCI_CTRL_HDR_SIZE) {
  30. nfc_err(&drv_data->i2c->dev, "cannot read NCI header\n");
  31. return -EBADMSG;
  32. }
  33. *skb = nci_skb_alloc(drv_data->priv->ndev,
  34. nci_hdr.plen + NCI_CTRL_HDR_SIZE, GFP_KERNEL);
  35. if (!*skb)
  36. return -ENOMEM;
  37. /* Copy NCI header into the SKB */
  38. skb_put_data(*skb, &nci_hdr, NCI_CTRL_HDR_SIZE);
  39. if (nci_hdr.plen) {
  40. /* Read the NCI payload */
  41. ret = i2c_master_recv(drv_data->i2c,
  42. skb_put(*skb, nci_hdr.plen),
  43. nci_hdr.plen);
  44. if (ret != nci_hdr.plen) {
  45. nfc_err(&drv_data->i2c->dev,
  46. "Invalid frame payload length: %u (expected %u)\n",
  47. ret, nci_hdr.plen);
  48. kfree_skb(*skb);
  49. return -EBADMSG;
  50. }
  51. }
  52. return 0;
  53. }
  54. static irqreturn_t nfcmrvl_i2c_int_irq_thread_fn(int irq, void *drv_data_ptr)
  55. {
  56. struct nfcmrvl_i2c_drv_data *drv_data = drv_data_ptr;
  57. struct sk_buff *skb = NULL;
  58. int ret;
  59. if (!drv_data->priv)
  60. return IRQ_HANDLED;
  61. if (test_bit(NFCMRVL_PHY_ERROR, &drv_data->priv->flags))
  62. return IRQ_HANDLED;
  63. ret = nfcmrvl_i2c_read(drv_data, &skb);
  64. switch (ret) {
  65. case -EREMOTEIO:
  66. set_bit(NFCMRVL_PHY_ERROR, &drv_data->priv->flags);
  67. break;
  68. case -ENOMEM:
  69. case -EBADMSG:
  70. nfc_err(&drv_data->i2c->dev, "read failed %d\n", ret);
  71. break;
  72. default:
  73. if (nfcmrvl_nci_recv_frame(drv_data->priv, skb) < 0)
  74. nfc_err(&drv_data->i2c->dev, "corrupted RX packet\n");
  75. break;
  76. }
  77. return IRQ_HANDLED;
  78. }
  79. static int nfcmrvl_i2c_nci_open(struct nfcmrvl_private *priv)
  80. {
  81. struct nfcmrvl_i2c_drv_data *drv_data = priv->drv_data;
  82. if (!drv_data)
  83. return -ENODEV;
  84. return 0;
  85. }
  86. static int nfcmrvl_i2c_nci_close(struct nfcmrvl_private *priv)
  87. {
  88. return 0;
  89. }
  90. static int nfcmrvl_i2c_nci_send(struct nfcmrvl_private *priv,
  91. struct sk_buff *skb)
  92. {
  93. struct nfcmrvl_i2c_drv_data *drv_data = priv->drv_data;
  94. int ret;
  95. if (test_bit(NFCMRVL_PHY_ERROR, &priv->flags)) {
  96. kfree_skb(skb);
  97. return -EREMOTEIO;
  98. }
  99. ret = i2c_master_send(drv_data->i2c, skb->data, skb->len);
  100. /* Retry if chip was in standby */
  101. if (ret == -EREMOTEIO) {
  102. nfc_info(drv_data->dev, "chip may sleep, retry\n");
  103. usleep_range(6000, 10000);
  104. ret = i2c_master_send(drv_data->i2c, skb->data, skb->len);
  105. }
  106. if (ret >= 0) {
  107. if (ret != skb->len) {
  108. nfc_err(drv_data->dev,
  109. "Invalid length sent: %u (expected %u)\n",
  110. ret, skb->len);
  111. ret = -EREMOTEIO;
  112. } else
  113. ret = 0;
  114. }
  115. if (ret) {
  116. kfree_skb(skb);
  117. return ret;
  118. }
  119. consume_skb(skb);
  120. return 0;
  121. }
  122. static void nfcmrvl_i2c_nci_update_config(struct nfcmrvl_private *priv,
  123. const void *param)
  124. {
  125. }
  126. static const struct nfcmrvl_if_ops i2c_ops = {
  127. .nci_open = nfcmrvl_i2c_nci_open,
  128. .nci_close = nfcmrvl_i2c_nci_close,
  129. .nci_send = nfcmrvl_i2c_nci_send,
  130. .nci_update_config = nfcmrvl_i2c_nci_update_config,
  131. };
  132. static int nfcmrvl_i2c_parse_dt(struct device_node *node,
  133. struct nfcmrvl_platform_data *pdata)
  134. {
  135. int ret;
  136. ret = nfcmrvl_parse_dt(node, pdata);
  137. if (ret < 0) {
  138. pr_err("Failed to get generic entries\n");
  139. return ret;
  140. }
  141. if (of_find_property(node, "i2c-int-falling", NULL))
  142. pdata->irq_polarity = IRQF_TRIGGER_FALLING;
  143. else
  144. pdata->irq_polarity = IRQF_TRIGGER_RISING;
  145. ret = irq_of_parse_and_map(node, 0);
  146. if (!ret) {
  147. pr_err("Unable to get irq\n");
  148. return -EINVAL;
  149. }
  150. pdata->irq = ret;
  151. return 0;
  152. }
  153. static int nfcmrvl_i2c_probe(struct i2c_client *client,
  154. const struct i2c_device_id *id)
  155. {
  156. const struct nfcmrvl_platform_data *pdata;
  157. struct nfcmrvl_i2c_drv_data *drv_data;
  158. struct nfcmrvl_platform_data config;
  159. int ret;
  160. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  161. nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
  162. return -ENODEV;
  163. }
  164. drv_data = devm_kzalloc(&client->dev, sizeof(*drv_data), GFP_KERNEL);
  165. if (!drv_data)
  166. return -ENOMEM;
  167. drv_data->i2c = client;
  168. drv_data->dev = &client->dev;
  169. drv_data->priv = NULL;
  170. i2c_set_clientdata(client, drv_data);
  171. pdata = client->dev.platform_data;
  172. if (!pdata && client->dev.of_node)
  173. if (nfcmrvl_i2c_parse_dt(client->dev.of_node, &config) == 0)
  174. pdata = &config;
  175. if (!pdata)
  176. return -EINVAL;
  177. /* Request the read IRQ */
  178. ret = devm_request_threaded_irq(&drv_data->i2c->dev, pdata->irq,
  179. NULL, nfcmrvl_i2c_int_irq_thread_fn,
  180. pdata->irq_polarity | IRQF_ONESHOT,
  181. "nfcmrvl_i2c_int", drv_data);
  182. if (ret < 0) {
  183. nfc_err(&drv_data->i2c->dev,
  184. "Unable to register IRQ handler\n");
  185. return ret;
  186. }
  187. drv_data->priv = nfcmrvl_nci_register_dev(NFCMRVL_PHY_I2C,
  188. drv_data, &i2c_ops,
  189. &drv_data->i2c->dev, pdata);
  190. if (IS_ERR(drv_data->priv))
  191. return PTR_ERR(drv_data->priv);
  192. drv_data->priv->support_fw_dnld = true;
  193. return 0;
  194. }
  195. static void nfcmrvl_i2c_remove(struct i2c_client *client)
  196. {
  197. struct nfcmrvl_i2c_drv_data *drv_data = i2c_get_clientdata(client);
  198. nfcmrvl_nci_unregister_dev(drv_data->priv);
  199. }
  200. static const struct of_device_id of_nfcmrvl_i2c_match[] __maybe_unused = {
  201. { .compatible = "marvell,nfc-i2c", },
  202. {},
  203. };
  204. MODULE_DEVICE_TABLE(of, of_nfcmrvl_i2c_match);
  205. static const struct i2c_device_id nfcmrvl_i2c_id_table[] = {
  206. { "nfcmrvl_i2c", 0 },
  207. {}
  208. };
  209. MODULE_DEVICE_TABLE(i2c, nfcmrvl_i2c_id_table);
  210. static struct i2c_driver nfcmrvl_i2c_driver = {
  211. .probe = nfcmrvl_i2c_probe,
  212. .id_table = nfcmrvl_i2c_id_table,
  213. .remove = nfcmrvl_i2c_remove,
  214. .driver = {
  215. .name = "nfcmrvl_i2c",
  216. .of_match_table = of_match_ptr(of_nfcmrvl_i2c_match),
  217. },
  218. };
  219. module_i2c_driver(nfcmrvl_i2c_driver);
  220. MODULE_AUTHOR("Marvell International Ltd.");
  221. MODULE_DESCRIPTION("Marvell NFC-over-I2C driver");
  222. MODULE_LICENSE("GPL v2");