i2c.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/acpi.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/delay.h>
  27. #include <linux/nfc.h>
  28. #include <linux/platform_data/st-nci.h>
  29. #include "st-nci.h"
  30. #define DRIVER_DESC "NCI NFC driver for ST_NCI"
  31. /* ndlc header */
  32. #define ST_NCI_FRAME_HEADROOM 1
  33. #define ST_NCI_FRAME_TAILROOM 0
  34. #define ST_NCI_I2C_MIN_SIZE 4 /* PCB(1) + NCI Packet header(3) */
  35. #define ST_NCI_I2C_MAX_SIZE 250 /* req 4.2.1 */
  36. #define ST_NCI_I2C_DRIVER_NAME "st_nci_i2c"
  37. #define ST_NCI_GPIO_NAME_RESET "reset"
  38. struct st_nci_i2c_phy {
  39. struct i2c_client *i2c_dev;
  40. struct llt_ndlc *ndlc;
  41. bool irq_active;
  42. unsigned int gpio_reset;
  43. unsigned int irq_polarity;
  44. struct st_nci_se_status se_status;
  45. };
  46. static int st_nci_i2c_enable(void *phy_id)
  47. {
  48. struct st_nci_i2c_phy *phy = phy_id;
  49. gpio_set_value(phy->gpio_reset, 0);
  50. usleep_range(10000, 15000);
  51. gpio_set_value(phy->gpio_reset, 1);
  52. usleep_range(80000, 85000);
  53. if (phy->ndlc->powered == 0 && phy->irq_active == 0) {
  54. enable_irq(phy->i2c_dev->irq);
  55. phy->irq_active = true;
  56. }
  57. return 0;
  58. }
  59. static void st_nci_i2c_disable(void *phy_id)
  60. {
  61. struct st_nci_i2c_phy *phy = phy_id;
  62. disable_irq_nosync(phy->i2c_dev->irq);
  63. phy->irq_active = false;
  64. }
  65. /*
  66. * Writing a frame must not return the number of written bytes.
  67. * It must return either zero for success, or <0 for error.
  68. * In addition, it must not alter the skb
  69. */
  70. static int st_nci_i2c_write(void *phy_id, struct sk_buff *skb)
  71. {
  72. int r = -1;
  73. struct st_nci_i2c_phy *phy = phy_id;
  74. struct i2c_client *client = phy->i2c_dev;
  75. if (phy->ndlc->hard_fault != 0)
  76. return phy->ndlc->hard_fault;
  77. r = i2c_master_send(client, skb->data, skb->len);
  78. if (r < 0) { /* Retry, chip was in standby */
  79. usleep_range(1000, 4000);
  80. r = i2c_master_send(client, skb->data, skb->len);
  81. }
  82. if (r >= 0) {
  83. if (r != skb->len)
  84. r = -EREMOTEIO;
  85. else
  86. r = 0;
  87. }
  88. return r;
  89. }
  90. /*
  91. * Reads an ndlc frame and returns it in a newly allocated sk_buff.
  92. * returns:
  93. * 0 : if received frame is complete
  94. * -EREMOTEIO : i2c read error (fatal)
  95. * -EBADMSG : frame was incorrect and discarded
  96. * -ENOMEM : cannot allocate skb, frame dropped
  97. */
  98. static int st_nci_i2c_read(struct st_nci_i2c_phy *phy,
  99. struct sk_buff **skb)
  100. {
  101. int r;
  102. u8 len;
  103. u8 buf[ST_NCI_I2C_MAX_SIZE];
  104. struct i2c_client *client = phy->i2c_dev;
  105. r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
  106. if (r < 0) { /* Retry, chip was in standby */
  107. usleep_range(1000, 4000);
  108. r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
  109. }
  110. if (r != ST_NCI_I2C_MIN_SIZE)
  111. return -EREMOTEIO;
  112. len = be16_to_cpu(*(__be16 *) (buf + 2));
  113. if (len > ST_NCI_I2C_MAX_SIZE) {
  114. nfc_err(&client->dev, "invalid frame len\n");
  115. return -EBADMSG;
  116. }
  117. *skb = alloc_skb(ST_NCI_I2C_MIN_SIZE + len, GFP_KERNEL);
  118. if (*skb == NULL)
  119. return -ENOMEM;
  120. skb_reserve(*skb, ST_NCI_I2C_MIN_SIZE);
  121. skb_put(*skb, ST_NCI_I2C_MIN_SIZE);
  122. memcpy((*skb)->data, buf, ST_NCI_I2C_MIN_SIZE);
  123. if (!len)
  124. return 0;
  125. r = i2c_master_recv(client, buf, len);
  126. if (r != len) {
  127. kfree_skb(*skb);
  128. return -EREMOTEIO;
  129. }
  130. skb_put(*skb, len);
  131. memcpy((*skb)->data + ST_NCI_I2C_MIN_SIZE, buf, len);
  132. return 0;
  133. }
  134. /*
  135. * Reads an ndlc frame from the chip.
  136. *
  137. * On ST_NCI, IRQ goes in idle state when read starts.
  138. */
  139. static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
  140. {
  141. struct st_nci_i2c_phy *phy = phy_id;
  142. struct i2c_client *client;
  143. struct sk_buff *skb = NULL;
  144. int r;
  145. if (!phy || !phy->ndlc || irq != phy->i2c_dev->irq) {
  146. WARN_ON_ONCE(1);
  147. return IRQ_NONE;
  148. }
  149. client = phy->i2c_dev;
  150. dev_dbg(&client->dev, "IRQ\n");
  151. if (phy->ndlc->hard_fault)
  152. return IRQ_HANDLED;
  153. if (!phy->ndlc->powered) {
  154. st_nci_i2c_disable(phy);
  155. return IRQ_HANDLED;
  156. }
  157. r = st_nci_i2c_read(phy, &skb);
  158. if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
  159. return IRQ_HANDLED;
  160. ndlc_recv(phy->ndlc, skb);
  161. return IRQ_HANDLED;
  162. }
  163. static struct nfc_phy_ops i2c_phy_ops = {
  164. .write = st_nci_i2c_write,
  165. .enable = st_nci_i2c_enable,
  166. .disable = st_nci_i2c_disable,
  167. };
  168. static int st_nci_i2c_acpi_request_resources(struct i2c_client *client)
  169. {
  170. struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
  171. struct gpio_desc *gpiod_reset;
  172. struct device *dev = &client->dev;
  173. u8 tmp;
  174. /* Get RESET GPIO from ACPI */
  175. gpiod_reset = devm_gpiod_get_index(dev, ST_NCI_GPIO_NAME_RESET, 1,
  176. GPIOD_OUT_HIGH);
  177. if (IS_ERR(gpiod_reset)) {
  178. nfc_err(dev, "Unable to get RESET GPIO\n");
  179. return -ENODEV;
  180. }
  181. phy->gpio_reset = desc_to_gpio(gpiod_reset);
  182. phy->irq_polarity = irq_get_trigger_type(client->irq);
  183. phy->se_status.is_ese_present = false;
  184. phy->se_status.is_uicc_present = false;
  185. if (device_property_present(dev, "ese-present")) {
  186. device_property_read_u8(dev, "ese-present", &tmp);
  187. phy->se_status.is_ese_present = tmp;
  188. }
  189. if (device_property_present(dev, "uicc-present")) {
  190. device_property_read_u8(dev, "uicc-present", &tmp);
  191. phy->se_status.is_uicc_present = tmp;
  192. }
  193. return 0;
  194. }
  195. static int st_nci_i2c_of_request_resources(struct i2c_client *client)
  196. {
  197. struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
  198. struct device_node *pp;
  199. int gpio;
  200. int r;
  201. pp = client->dev.of_node;
  202. if (!pp)
  203. return -ENODEV;
  204. /* Get GPIO from device tree */
  205. gpio = of_get_named_gpio(pp, "reset-gpios", 0);
  206. if (gpio < 0) {
  207. nfc_err(&client->dev,
  208. "Failed to retrieve reset-gpios from device tree\n");
  209. return gpio;
  210. }
  211. /* GPIO request and configuration */
  212. r = devm_gpio_request_one(&client->dev, gpio,
  213. GPIOF_OUT_INIT_HIGH, ST_NCI_GPIO_NAME_RESET);
  214. if (r) {
  215. nfc_err(&client->dev, "Failed to request reset pin\n");
  216. return r;
  217. }
  218. phy->gpio_reset = gpio;
  219. phy->irq_polarity = irq_get_trigger_type(client->irq);
  220. phy->se_status.is_ese_present =
  221. of_property_read_bool(pp, "ese-present");
  222. phy->se_status.is_uicc_present =
  223. of_property_read_bool(pp, "uicc-present");
  224. return 0;
  225. }
  226. static int st_nci_i2c_request_resources(struct i2c_client *client)
  227. {
  228. struct st_nci_nfc_platform_data *pdata;
  229. struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
  230. int r;
  231. pdata = client->dev.platform_data;
  232. if (pdata == NULL) {
  233. nfc_err(&client->dev, "No platform data\n");
  234. return -EINVAL;
  235. }
  236. /* store for later use */
  237. phy->gpio_reset = pdata->gpio_reset;
  238. phy->irq_polarity = pdata->irq_polarity;
  239. r = devm_gpio_request_one(&client->dev,
  240. phy->gpio_reset, GPIOF_OUT_INIT_HIGH,
  241. ST_NCI_GPIO_NAME_RESET);
  242. if (r) {
  243. pr_err("%s : reset gpio_request failed\n", __FILE__);
  244. return r;
  245. }
  246. phy->se_status.is_ese_present = pdata->is_ese_present;
  247. phy->se_status.is_uicc_present = pdata->is_uicc_present;
  248. return 0;
  249. }
  250. static int st_nci_i2c_probe(struct i2c_client *client,
  251. const struct i2c_device_id *id)
  252. {
  253. struct st_nci_i2c_phy *phy;
  254. struct st_nci_nfc_platform_data *pdata;
  255. int r;
  256. dev_dbg(&client->dev, "%s\n", __func__);
  257. dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
  258. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  259. nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
  260. return -ENODEV;
  261. }
  262. phy = devm_kzalloc(&client->dev, sizeof(struct st_nci_i2c_phy),
  263. GFP_KERNEL);
  264. if (!phy)
  265. return -ENOMEM;
  266. phy->i2c_dev = client;
  267. i2c_set_clientdata(client, phy);
  268. pdata = client->dev.platform_data;
  269. if (!pdata && client->dev.of_node) {
  270. r = st_nci_i2c_of_request_resources(client);
  271. if (r) {
  272. nfc_err(&client->dev, "No platform data\n");
  273. return r;
  274. }
  275. } else if (pdata) {
  276. r = st_nci_i2c_request_resources(client);
  277. if (r) {
  278. nfc_err(&client->dev,
  279. "Cannot get platform resources\n");
  280. return r;
  281. }
  282. } else if (ACPI_HANDLE(&client->dev)) {
  283. r = st_nci_i2c_acpi_request_resources(client);
  284. if (r) {
  285. nfc_err(&client->dev, "Cannot get ACPI data\n");
  286. return r;
  287. }
  288. } else {
  289. nfc_err(&client->dev,
  290. "st_nci platform resources not available\n");
  291. return -ENODEV;
  292. }
  293. r = ndlc_probe(phy, &i2c_phy_ops, &client->dev,
  294. ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
  295. &phy->ndlc, &phy->se_status);
  296. if (r < 0) {
  297. nfc_err(&client->dev, "Unable to register ndlc layer\n");
  298. return r;
  299. }
  300. phy->irq_active = true;
  301. r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
  302. st_nci_irq_thread_fn,
  303. phy->irq_polarity | IRQF_ONESHOT,
  304. ST_NCI_DRIVER_NAME, phy);
  305. if (r < 0)
  306. nfc_err(&client->dev, "Unable to register IRQ handler\n");
  307. return r;
  308. }
  309. static int st_nci_i2c_remove(struct i2c_client *client)
  310. {
  311. struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
  312. dev_dbg(&client->dev, "%s\n", __func__);
  313. ndlc_remove(phy->ndlc);
  314. return 0;
  315. }
  316. static struct i2c_device_id st_nci_i2c_id_table[] = {
  317. {ST_NCI_DRIVER_NAME, 0},
  318. {}
  319. };
  320. MODULE_DEVICE_TABLE(i2c, st_nci_i2c_id_table);
  321. static const struct acpi_device_id st_nci_i2c_acpi_match[] = {
  322. {"SMO2101"},
  323. {"SMO2102"},
  324. {}
  325. };
  326. MODULE_DEVICE_TABLE(acpi, st_nci_i2c_acpi_match);
  327. static const struct of_device_id of_st_nci_i2c_match[] = {
  328. { .compatible = "st,st21nfcb-i2c", },
  329. { .compatible = "st,st21nfcb_i2c", },
  330. { .compatible = "st,st21nfcc-i2c", },
  331. {}
  332. };
  333. MODULE_DEVICE_TABLE(of, of_st_nci_i2c_match);
  334. static struct i2c_driver st_nci_i2c_driver = {
  335. .driver = {
  336. .name = ST_NCI_I2C_DRIVER_NAME,
  337. .of_match_table = of_match_ptr(of_st_nci_i2c_match),
  338. .acpi_match_table = ACPI_PTR(st_nci_i2c_acpi_match),
  339. },
  340. .probe = st_nci_i2c_probe,
  341. .id_table = st_nci_i2c_id_table,
  342. .remove = st_nci_i2c_remove,
  343. };
  344. module_i2c_driver(st_nci_i2c_driver);
  345. MODULE_LICENSE("GPL");
  346. MODULE_DESCRIPTION(DRIVER_DESC);