i2c.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * I2C Link Layer for ST NCI NFC controller familly based Driver
  3. * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/module.h>
  19. #include <linux/i2c.h>
  20. #include <linux/gpio/consumer.h>
  21. #include <linux/acpi.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <linux/nfc.h>
  25. #include <linux/of.h>
  26. #include "st-nci.h"
  27. #define DRIVER_DESC "NCI NFC driver for ST_NCI"
  28. /* ndlc header */
  29. #define ST_NCI_FRAME_HEADROOM 1
  30. #define ST_NCI_FRAME_TAILROOM 0
  31. #define ST_NCI_I2C_MIN_SIZE 4 /* PCB(1) + NCI Packet header(3) */
  32. #define ST_NCI_I2C_MAX_SIZE 250 /* req 4.2.1 */
  33. #define ST_NCI_DRIVER_NAME "st_nci"
  34. #define ST_NCI_I2C_DRIVER_NAME "st_nci_i2c"
  35. struct st_nci_i2c_phy {
  36. struct i2c_client *i2c_dev;
  37. struct llt_ndlc *ndlc;
  38. bool irq_active;
  39. struct gpio_desc *gpiod_reset;
  40. struct st_nci_se_status se_status;
  41. };
  42. static int st_nci_i2c_enable(void *phy_id)
  43. {
  44. struct st_nci_i2c_phy *phy = phy_id;
  45. gpiod_set_value(phy->gpiod_reset, 0);
  46. usleep_range(10000, 15000);
  47. gpiod_set_value(phy->gpiod_reset, 1);
  48. usleep_range(80000, 85000);
  49. if (phy->ndlc->powered == 0 && phy->irq_active == 0) {
  50. enable_irq(phy->i2c_dev->irq);
  51. phy->irq_active = true;
  52. }
  53. return 0;
  54. }
  55. static void st_nci_i2c_disable(void *phy_id)
  56. {
  57. struct st_nci_i2c_phy *phy = phy_id;
  58. disable_irq_nosync(phy->i2c_dev->irq);
  59. phy->irq_active = false;
  60. }
  61. /*
  62. * Writing a frame must not return the number of written bytes.
  63. * It must return either zero for success, or <0 for error.
  64. * In addition, it must not alter the skb
  65. */
  66. static int st_nci_i2c_write(void *phy_id, struct sk_buff *skb)
  67. {
  68. int r = -1;
  69. struct st_nci_i2c_phy *phy = phy_id;
  70. struct i2c_client *client = phy->i2c_dev;
  71. if (phy->ndlc->hard_fault != 0)
  72. return phy->ndlc->hard_fault;
  73. r = i2c_master_send(client, skb->data, skb->len);
  74. if (r < 0) { /* Retry, chip was in standby */
  75. usleep_range(1000, 4000);
  76. r = i2c_master_send(client, skb->data, skb->len);
  77. }
  78. if (r >= 0) {
  79. if (r != skb->len)
  80. r = -EREMOTEIO;
  81. else
  82. r = 0;
  83. }
  84. return r;
  85. }
  86. /*
  87. * Reads an ndlc frame and returns it in a newly allocated sk_buff.
  88. * returns:
  89. * 0 : if received frame is complete
  90. * -EREMOTEIO : i2c read error (fatal)
  91. * -EBADMSG : frame was incorrect and discarded
  92. * -ENOMEM : cannot allocate skb, frame dropped
  93. */
  94. static int st_nci_i2c_read(struct st_nci_i2c_phy *phy,
  95. struct sk_buff **skb)
  96. {
  97. int r;
  98. u8 len;
  99. u8 buf[ST_NCI_I2C_MAX_SIZE];
  100. struct i2c_client *client = phy->i2c_dev;
  101. r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
  102. if (r < 0) { /* Retry, chip was in standby */
  103. usleep_range(1000, 4000);
  104. r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
  105. }
  106. if (r != ST_NCI_I2C_MIN_SIZE)
  107. return -EREMOTEIO;
  108. len = be16_to_cpu(*(__be16 *) (buf + 2));
  109. if (len > ST_NCI_I2C_MAX_SIZE) {
  110. nfc_err(&client->dev, "invalid frame len\n");
  111. return -EBADMSG;
  112. }
  113. *skb = alloc_skb(ST_NCI_I2C_MIN_SIZE + len, GFP_KERNEL);
  114. if (*skb == NULL)
  115. return -ENOMEM;
  116. skb_reserve(*skb, ST_NCI_I2C_MIN_SIZE);
  117. skb_put(*skb, ST_NCI_I2C_MIN_SIZE);
  118. memcpy((*skb)->data, buf, ST_NCI_I2C_MIN_SIZE);
  119. if (!len)
  120. return 0;
  121. r = i2c_master_recv(client, buf, len);
  122. if (r != len) {
  123. kfree_skb(*skb);
  124. return -EREMOTEIO;
  125. }
  126. skb_put(*skb, len);
  127. memcpy((*skb)->data + ST_NCI_I2C_MIN_SIZE, buf, len);
  128. return 0;
  129. }
  130. /*
  131. * Reads an ndlc frame from the chip.
  132. *
  133. * On ST_NCI, IRQ goes in idle state when read starts.
  134. */
  135. static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
  136. {
  137. struct st_nci_i2c_phy *phy = phy_id;
  138. struct i2c_client *client;
  139. struct sk_buff *skb = NULL;
  140. int r;
  141. if (!phy || !phy->ndlc || irq != phy->i2c_dev->irq) {
  142. WARN_ON_ONCE(1);
  143. return IRQ_NONE;
  144. }
  145. client = phy->i2c_dev;
  146. dev_dbg(&client->dev, "IRQ\n");
  147. if (phy->ndlc->hard_fault)
  148. return IRQ_HANDLED;
  149. if (!phy->ndlc->powered) {
  150. st_nci_i2c_disable(phy);
  151. return IRQ_HANDLED;
  152. }
  153. r = st_nci_i2c_read(phy, &skb);
  154. if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
  155. return IRQ_HANDLED;
  156. ndlc_recv(phy->ndlc, skb);
  157. return IRQ_HANDLED;
  158. }
  159. static struct nfc_phy_ops i2c_phy_ops = {
  160. .write = st_nci_i2c_write,
  161. .enable = st_nci_i2c_enable,
  162. .disable = st_nci_i2c_disable,
  163. };
  164. static const struct acpi_gpio_params reset_gpios = { 1, 0, false };
  165. static const struct acpi_gpio_mapping acpi_st_nci_gpios[] = {
  166. { "reset-gpios", &reset_gpios, 1 },
  167. {},
  168. };
  169. static int st_nci_i2c_probe(struct i2c_client *client,
  170. const struct i2c_device_id *id)
  171. {
  172. struct device *dev = &client->dev;
  173. struct st_nci_i2c_phy *phy;
  174. int r;
  175. dev_dbg(&client->dev, "%s\n", __func__);
  176. dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
  177. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  178. nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
  179. return -ENODEV;
  180. }
  181. phy = devm_kzalloc(dev, sizeof(struct st_nci_i2c_phy), GFP_KERNEL);
  182. if (!phy)
  183. return -ENOMEM;
  184. phy->i2c_dev = client;
  185. i2c_set_clientdata(client, phy);
  186. r = devm_acpi_dev_add_driver_gpios(dev, acpi_st_nci_gpios);
  187. if (r)
  188. dev_dbg(dev, "Unable to add GPIO mapping table\n");
  189. /* Get RESET GPIO */
  190. phy->gpiod_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  191. if (IS_ERR(phy->gpiod_reset)) {
  192. nfc_err(dev, "Unable to get RESET GPIO\n");
  193. return -ENODEV;
  194. }
  195. phy->se_status.is_ese_present =
  196. device_property_read_bool(dev, "ese-present");
  197. phy->se_status.is_uicc_present =
  198. device_property_read_bool(dev, "uicc-present");
  199. r = ndlc_probe(phy, &i2c_phy_ops, &client->dev,
  200. ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
  201. &phy->ndlc, &phy->se_status);
  202. if (r < 0) {
  203. nfc_err(&client->dev, "Unable to register ndlc layer\n");
  204. return r;
  205. }
  206. phy->irq_active = true;
  207. r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
  208. st_nci_irq_thread_fn,
  209. IRQF_ONESHOT,
  210. ST_NCI_DRIVER_NAME, phy);
  211. if (r < 0)
  212. nfc_err(&client->dev, "Unable to register IRQ handler\n");
  213. return r;
  214. }
  215. static int st_nci_i2c_remove(struct i2c_client *client)
  216. {
  217. struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
  218. dev_dbg(&client->dev, "%s\n", __func__);
  219. ndlc_remove(phy->ndlc);
  220. return 0;
  221. }
  222. static struct i2c_device_id st_nci_i2c_id_table[] = {
  223. {ST_NCI_DRIVER_NAME, 0},
  224. {}
  225. };
  226. MODULE_DEVICE_TABLE(i2c, st_nci_i2c_id_table);
  227. static const struct acpi_device_id st_nci_i2c_acpi_match[] = {
  228. {"SMO2101"},
  229. {"SMO2102"},
  230. {}
  231. };
  232. MODULE_DEVICE_TABLE(acpi, st_nci_i2c_acpi_match);
  233. static const struct of_device_id of_st_nci_i2c_match[] = {
  234. { .compatible = "st,st21nfcb-i2c", },
  235. { .compatible = "st,st21nfcb_i2c", },
  236. { .compatible = "st,st21nfcc-i2c", },
  237. {}
  238. };
  239. MODULE_DEVICE_TABLE(of, of_st_nci_i2c_match);
  240. static struct i2c_driver st_nci_i2c_driver = {
  241. .driver = {
  242. .name = ST_NCI_I2C_DRIVER_NAME,
  243. .of_match_table = of_match_ptr(of_st_nci_i2c_match),
  244. .acpi_match_table = ACPI_PTR(st_nci_i2c_acpi_match),
  245. },
  246. .probe = st_nci_i2c_probe,
  247. .id_table = st_nci_i2c_id_table,
  248. .remove = st_nci_i2c_remove,
  249. };
  250. module_i2c_driver(st_nci_i2c_driver);
  251. MODULE_LICENSE("GPL");
  252. MODULE_DESCRIPTION(DRIVER_DESC);