i2c.c 6.9 KB

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