i2c.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Driver for NXP PN533 NFC Chip - I2C transport layer
  3. *
  4. * Copyright (C) 2011 Instituto Nokia de Tecnologia
  5. * Copyright (C) 2012-2013 Tieto Poland
  6. * Copyright (C) 2016 HALE electronic
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <linux/device.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/slab.h>
  26. #include <linux/i2c.h>
  27. #include <linux/nfc.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/interrupt.h>
  30. #include <net/nfc/nfc.h>
  31. #include "pn533.h"
  32. #define VERSION "0.1"
  33. #define PN533_I2C_DRIVER_NAME "pn533_i2c"
  34. struct pn533_i2c_phy {
  35. struct i2c_client *i2c_dev;
  36. struct pn533 *priv;
  37. bool aborted;
  38. int hard_fault; /*
  39. * < 0 if hardware error occurred (e.g. i2c err)
  40. * and prevents normal operation.
  41. */
  42. };
  43. static int pn533_i2c_send_ack(struct pn533 *dev, gfp_t flags)
  44. {
  45. struct pn533_i2c_phy *phy = dev->phy;
  46. struct i2c_client *client = phy->i2c_dev;
  47. u8 ack[6] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
  48. /* spec 6.2.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
  49. int rc;
  50. rc = i2c_master_send(client, ack, 6);
  51. return rc;
  52. }
  53. static int pn533_i2c_send_frame(struct pn533 *dev,
  54. struct sk_buff *out)
  55. {
  56. struct pn533_i2c_phy *phy = dev->phy;
  57. struct i2c_client *client = phy->i2c_dev;
  58. int rc;
  59. if (phy->hard_fault != 0)
  60. return phy->hard_fault;
  61. if (phy->priv == NULL)
  62. phy->priv = dev;
  63. phy->aborted = false;
  64. print_hex_dump_debug("PN533_i2c TX: ", DUMP_PREFIX_NONE, 16, 1,
  65. out->data, out->len, false);
  66. rc = i2c_master_send(client, out->data, out->len);
  67. if (rc == -EREMOTEIO) { /* Retry, chip was in power down */
  68. usleep_range(6000, 10000);
  69. rc = i2c_master_send(client, out->data, out->len);
  70. }
  71. if (rc >= 0) {
  72. if (rc != out->len)
  73. rc = -EREMOTEIO;
  74. else
  75. rc = 0;
  76. }
  77. return rc;
  78. }
  79. static void pn533_i2c_abort_cmd(struct pn533 *dev, gfp_t flags)
  80. {
  81. struct pn533_i2c_phy *phy = dev->phy;
  82. phy->aborted = true;
  83. /* An ack will cancel the last issued command */
  84. pn533_i2c_send_ack(dev, flags);
  85. /* schedule cmd_complete_work to finish current command execution */
  86. pn533_recv_frame(phy->priv, NULL, -ENOENT);
  87. }
  88. static int pn533_i2c_read(struct pn533_i2c_phy *phy, struct sk_buff **skb)
  89. {
  90. struct i2c_client *client = phy->i2c_dev;
  91. int len = PN533_EXT_FRAME_HEADER_LEN +
  92. PN533_STD_FRAME_MAX_PAYLOAD_LEN +
  93. PN533_STD_FRAME_TAIL_LEN + 1;
  94. int r;
  95. *skb = alloc_skb(len, GFP_KERNEL);
  96. if (*skb == NULL)
  97. return -ENOMEM;
  98. r = i2c_master_recv(client, skb_put(*skb, len), len);
  99. if (r != len) {
  100. nfc_err(&client->dev, "cannot read. r=%d len=%d\n", r, len);
  101. kfree_skb(*skb);
  102. return -EREMOTEIO;
  103. }
  104. if (!((*skb)->data[0] & 0x01)) {
  105. nfc_err(&client->dev, "READY flag not set");
  106. kfree_skb(*skb);
  107. return -EBUSY;
  108. }
  109. /* remove READY byte */
  110. skb_pull(*skb, 1);
  111. /* trim to frame size */
  112. skb_trim(*skb, phy->priv->ops->rx_frame_size((*skb)->data));
  113. return 0;
  114. }
  115. static irqreturn_t pn533_i2c_irq_thread_fn(int irq, void *data)
  116. {
  117. struct pn533_i2c_phy *phy = data;
  118. struct i2c_client *client;
  119. struct sk_buff *skb = NULL;
  120. int r;
  121. if (!phy || irq != phy->i2c_dev->irq) {
  122. WARN_ON_ONCE(1);
  123. return IRQ_NONE;
  124. }
  125. client = phy->i2c_dev;
  126. dev_dbg(&client->dev, "IRQ\n");
  127. if (phy->hard_fault != 0)
  128. return IRQ_HANDLED;
  129. r = pn533_i2c_read(phy, &skb);
  130. if (r == -EREMOTEIO) {
  131. phy->hard_fault = r;
  132. pn533_recv_frame(phy->priv, NULL, -EREMOTEIO);
  133. return IRQ_HANDLED;
  134. } else if ((r == -ENOMEM) || (r == -EBADMSG) || (r == -EBUSY)) {
  135. return IRQ_HANDLED;
  136. }
  137. if (!phy->aborted)
  138. pn533_recv_frame(phy->priv, skb, 0);
  139. return IRQ_HANDLED;
  140. }
  141. static struct pn533_phy_ops i2c_phy_ops = {
  142. .send_frame = pn533_i2c_send_frame,
  143. .send_ack = pn533_i2c_send_ack,
  144. .abort_cmd = pn533_i2c_abort_cmd,
  145. };
  146. static int pn533_i2c_probe(struct i2c_client *client,
  147. const struct i2c_device_id *id)
  148. {
  149. struct pn533_i2c_phy *phy;
  150. struct pn533 *priv;
  151. int r = 0;
  152. dev_dbg(&client->dev, "%s\n", __func__);
  153. dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
  154. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  155. nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
  156. return -ENODEV;
  157. }
  158. phy = devm_kzalloc(&client->dev, sizeof(struct pn533_i2c_phy),
  159. GFP_KERNEL);
  160. if (!phy)
  161. return -ENOMEM;
  162. phy->i2c_dev = client;
  163. i2c_set_clientdata(client, phy);
  164. r = request_threaded_irq(client->irq, NULL, pn533_i2c_irq_thread_fn,
  165. IRQF_TRIGGER_FALLING |
  166. IRQF_SHARED | IRQF_ONESHOT,
  167. PN533_I2C_DRIVER_NAME, phy);
  168. if (r < 0)
  169. nfc_err(&client->dev, "Unable to register IRQ handler\n");
  170. priv = pn533_register_device(PN533_DEVICE_PN532,
  171. PN533_NO_TYPE_B_PROTOCOLS,
  172. PN533_PROTO_REQ_ACK_RESP,
  173. phy, &i2c_phy_ops, NULL,
  174. &phy->i2c_dev->dev,
  175. &client->dev);
  176. if (IS_ERR(priv)) {
  177. r = PTR_ERR(priv);
  178. goto err_register;
  179. }
  180. phy->priv = priv;
  181. return 0;
  182. err_register:
  183. free_irq(client->irq, phy);
  184. return r;
  185. }
  186. static int pn533_i2c_remove(struct i2c_client *client)
  187. {
  188. struct pn533_i2c_phy *phy = i2c_get_clientdata(client);
  189. dev_dbg(&client->dev, "%s\n", __func__);
  190. free_irq(client->irq, phy);
  191. pn533_unregister_device(phy->priv);
  192. return 0;
  193. }
  194. static const struct of_device_id of_pn533_i2c_match[] = {
  195. { .compatible = "nxp,pn533-i2c", },
  196. { .compatible = "nxp,pn532-i2c", },
  197. {},
  198. };
  199. MODULE_DEVICE_TABLE(of, of_pn533_i2c_match);
  200. static struct i2c_device_id pn533_i2c_id_table[] = {
  201. { PN533_I2C_DRIVER_NAME, 0 },
  202. {}
  203. };
  204. MODULE_DEVICE_TABLE(i2c, pn533_i2c_id_table);
  205. static struct i2c_driver pn533_i2c_driver = {
  206. .driver = {
  207. .name = PN533_I2C_DRIVER_NAME,
  208. .owner = THIS_MODULE,
  209. .of_match_table = of_match_ptr(of_pn533_i2c_match),
  210. },
  211. .probe = pn533_i2c_probe,
  212. .id_table = pn533_i2c_id_table,
  213. .remove = pn533_i2c_remove,
  214. };
  215. module_i2c_driver(pn533_i2c_driver);
  216. MODULE_AUTHOR("Michael Thalmeier <michael.thalmeier@hale.at>");
  217. MODULE_DESCRIPTION("PN533 I2C driver ver " VERSION);
  218. MODULE_VERSION(VERSION);
  219. MODULE_LICENSE("GPL");