i2c.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /*
  2. * I2C Link Layer for ST21NFCA HCI based Driver
  3. * Copyright (C) 2014 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/crc-ccitt.h>
  19. #include <linux/module.h>
  20. #include <linux/i2c.h>
  21. #include <linux/gpio.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/delay.h>
  27. #include <linux/nfc.h>
  28. #include <linux/firmware.h>
  29. #include <linux/platform_data/st21nfca.h>
  30. #include <asm/unaligned.h>
  31. #include <net/nfc/hci.h>
  32. #include <net/nfc/llc.h>
  33. #include <net/nfc/nfc.h>
  34. #include "st21nfca.h"
  35. /*
  36. * Every frame starts with ST21NFCA_SOF_EOF and ends with ST21NFCA_SOF_EOF.
  37. * Because ST21NFCA_SOF_EOF is a possible data value, there is a mecanism
  38. * called byte stuffing has been introduced.
  39. *
  40. * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
  41. * - insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
  42. * - xor byte with ST21NFCA_BYTE_STUFFING_MASK
  43. */
  44. #define ST21NFCA_SOF_EOF 0x7e
  45. #define ST21NFCA_BYTE_STUFFING_MASK 0x20
  46. #define ST21NFCA_ESCAPE_BYTE_STUFFING 0x7d
  47. /* SOF + 00 */
  48. #define ST21NFCA_FRAME_HEADROOM 2
  49. /* 2 bytes crc + EOF */
  50. #define ST21NFCA_FRAME_TAILROOM 3
  51. #define IS_START_OF_FRAME(buf) (buf[0] == ST21NFCA_SOF_EOF && \
  52. buf[1] == 0)
  53. #define ST21NFCA_HCI_I2C_DRIVER_NAME "st21nfca_hci_i2c"
  54. static struct i2c_device_id st21nfca_hci_i2c_id_table[] = {
  55. {ST21NFCA_HCI_DRIVER_NAME, 0},
  56. {}
  57. };
  58. MODULE_DEVICE_TABLE(i2c, st21nfca_hci_i2c_id_table);
  59. struct st21nfca_i2c_phy {
  60. struct i2c_client *i2c_dev;
  61. struct nfc_hci_dev *hdev;
  62. unsigned int gpio_ena;
  63. unsigned int irq_polarity;
  64. struct st21nfca_se_status se_status;
  65. struct sk_buff *pending_skb;
  66. int current_read_len;
  67. /*
  68. * crc might have fail because i2c macro
  69. * is disable due to other interface activity
  70. */
  71. int crc_trials;
  72. int powered;
  73. int run_mode;
  74. /*
  75. * < 0 if hardware error occured (e.g. i2c err)
  76. * and prevents normal operation.
  77. */
  78. int hard_fault;
  79. struct mutex phy_lock;
  80. };
  81. static u8 len_seq[] = { 16, 24, 12, 29 };
  82. static u16 wait_tab[] = { 2, 3, 5, 15, 20, 40};
  83. #define I2C_DUMP_SKB(info, skb) \
  84. do { \
  85. pr_debug("%s:\n", info); \
  86. print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
  87. 16, 1, (skb)->data, (skb)->len, 0); \
  88. } while (0)
  89. /*
  90. * In order to get the CLF in a known state we generate an internal reboot
  91. * using a proprietary command.
  92. * Once the reboot is completed, we expect to receive a ST21NFCA_SOF_EOF
  93. * fill buffer.
  94. */
  95. static int st21nfca_hci_platform_init(struct st21nfca_i2c_phy *phy)
  96. {
  97. u16 wait_reboot[] = { 50, 300, 1000 };
  98. char reboot_cmd[] = { 0x7E, 0x66, 0x48, 0xF6, 0x7E };
  99. u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE];
  100. int i, r = -1;
  101. for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
  102. r = i2c_master_send(phy->i2c_dev, reboot_cmd,
  103. sizeof(reboot_cmd));
  104. if (r < 0)
  105. msleep(wait_reboot[i]);
  106. }
  107. if (r < 0)
  108. return r;
  109. /* CLF is spending about 20ms to do an internal reboot */
  110. msleep(20);
  111. r = -1;
  112. for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
  113. r = i2c_master_recv(phy->i2c_dev, tmp,
  114. ST21NFCA_HCI_LLC_MAX_SIZE);
  115. if (r < 0)
  116. msleep(wait_reboot[i]);
  117. }
  118. if (r < 0)
  119. return r;
  120. for (i = 0; i < ST21NFCA_HCI_LLC_MAX_SIZE &&
  121. tmp[i] == ST21NFCA_SOF_EOF; i++)
  122. ;
  123. if (r != ST21NFCA_HCI_LLC_MAX_SIZE)
  124. return -ENODEV;
  125. usleep_range(1000, 1500);
  126. return 0;
  127. }
  128. static int st21nfca_hci_i2c_enable(void *phy_id)
  129. {
  130. struct st21nfca_i2c_phy *phy = phy_id;
  131. gpio_set_value(phy->gpio_ena, 1);
  132. phy->powered = 1;
  133. phy->run_mode = ST21NFCA_HCI_MODE;
  134. usleep_range(10000, 15000);
  135. return 0;
  136. }
  137. static void st21nfca_hci_i2c_disable(void *phy_id)
  138. {
  139. struct st21nfca_i2c_phy *phy = phy_id;
  140. pr_info("\n");
  141. gpio_set_value(phy->gpio_ena, 0);
  142. phy->powered = 0;
  143. }
  144. static void st21nfca_hci_add_len_crc(struct sk_buff *skb)
  145. {
  146. u16 crc;
  147. u8 tmp;
  148. *skb_push(skb, 1) = 0;
  149. crc = crc_ccitt(0xffff, skb->data, skb->len);
  150. crc = ~crc;
  151. tmp = crc & 0x00ff;
  152. *skb_put(skb, 1) = tmp;
  153. tmp = (crc >> 8) & 0x00ff;
  154. *skb_put(skb, 1) = tmp;
  155. }
  156. static void st21nfca_hci_remove_len_crc(struct sk_buff *skb)
  157. {
  158. skb_pull(skb, ST21NFCA_FRAME_HEADROOM);
  159. skb_trim(skb, skb->len - ST21NFCA_FRAME_TAILROOM);
  160. }
  161. /*
  162. * Writing a frame must not return the number of written bytes.
  163. * It must return either zero for success, or <0 for error.
  164. * In addition, it must not alter the skb
  165. */
  166. static int st21nfca_hci_i2c_write(void *phy_id, struct sk_buff *skb)
  167. {
  168. int r = -1, i, j;
  169. struct st21nfca_i2c_phy *phy = phy_id;
  170. struct i2c_client *client = phy->i2c_dev;
  171. u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE * 2];
  172. I2C_DUMP_SKB("st21nfca_hci_i2c_write", skb);
  173. if (phy->hard_fault != 0)
  174. return phy->hard_fault;
  175. /*
  176. * Compute CRC before byte stuffing computation on frame
  177. * Note st21nfca_hci_add_len_crc is doing a byte stuffing
  178. * on its own value
  179. */
  180. st21nfca_hci_add_len_crc(skb);
  181. /* add ST21NFCA_SOF_EOF on tail */
  182. *skb_put(skb, 1) = ST21NFCA_SOF_EOF;
  183. /* add ST21NFCA_SOF_EOF on head */
  184. *skb_push(skb, 1) = ST21NFCA_SOF_EOF;
  185. /*
  186. * Compute byte stuffing
  187. * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
  188. * insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
  189. * xor byte with ST21NFCA_BYTE_STUFFING_MASK
  190. */
  191. tmp[0] = skb->data[0];
  192. for (i = 1, j = 1; i < skb->len - 1; i++, j++) {
  193. if (skb->data[i] == ST21NFCA_SOF_EOF
  194. || skb->data[i] == ST21NFCA_ESCAPE_BYTE_STUFFING) {
  195. tmp[j] = ST21NFCA_ESCAPE_BYTE_STUFFING;
  196. j++;
  197. tmp[j] = skb->data[i] ^ ST21NFCA_BYTE_STUFFING_MASK;
  198. } else {
  199. tmp[j] = skb->data[i];
  200. }
  201. }
  202. tmp[j] = skb->data[i];
  203. j++;
  204. /*
  205. * Manage sleep mode
  206. * Try 3 times to send data with delay between each
  207. */
  208. mutex_lock(&phy->phy_lock);
  209. for (i = 0; i < ARRAY_SIZE(wait_tab) && r < 0; i++) {
  210. r = i2c_master_send(client, tmp, j);
  211. if (r < 0)
  212. msleep(wait_tab[i]);
  213. }
  214. mutex_unlock(&phy->phy_lock);
  215. if (r >= 0) {
  216. if (r != j)
  217. r = -EREMOTEIO;
  218. else
  219. r = 0;
  220. }
  221. st21nfca_hci_remove_len_crc(skb);
  222. return r;
  223. }
  224. static int get_frame_size(u8 *buf, int buflen)
  225. {
  226. int len = 0;
  227. if (buf[len + 1] == ST21NFCA_SOF_EOF)
  228. return 0;
  229. for (len = 1; len < buflen && buf[len] != ST21NFCA_SOF_EOF; len++)
  230. ;
  231. return len;
  232. }
  233. static int check_crc(u8 *buf, int buflen)
  234. {
  235. u16 crc;
  236. crc = crc_ccitt(0xffff, buf, buflen - 2);
  237. crc = ~crc;
  238. if (buf[buflen - 2] != (crc & 0xff) || buf[buflen - 1] != (crc >> 8)) {
  239. pr_err(ST21NFCA_HCI_DRIVER_NAME
  240. ": CRC error 0x%x != 0x%x 0x%x\n", crc, buf[buflen - 1],
  241. buf[buflen - 2]);
  242. pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__);
  243. print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
  244. 16, 2, buf, buflen, false);
  245. return -EPERM;
  246. }
  247. return 0;
  248. }
  249. /*
  250. * Prepare received data for upper layer.
  251. * Received data include byte stuffing, crc and sof/eof
  252. * which is not usable by hci part.
  253. * returns:
  254. * frame size without sof/eof, header and byte stuffing
  255. * -EBADMSG : frame was incorrect and discarded
  256. */
  257. static int st21nfca_hci_i2c_repack(struct sk_buff *skb)
  258. {
  259. int i, j, r, size;
  260. if (skb->len < 1 || (skb->len > 1 && skb->data[1] != 0))
  261. return -EBADMSG;
  262. size = get_frame_size(skb->data, skb->len);
  263. if (size > 0) {
  264. skb_trim(skb, size);
  265. /* remove ST21NFCA byte stuffing for upper layer */
  266. for (i = 1, j = 0; i < skb->len; i++) {
  267. if (skb->data[i + j] ==
  268. (u8) ST21NFCA_ESCAPE_BYTE_STUFFING) {
  269. skb->data[i] = skb->data[i + j + 1]
  270. | ST21NFCA_BYTE_STUFFING_MASK;
  271. i++;
  272. j++;
  273. }
  274. skb->data[i] = skb->data[i + j];
  275. }
  276. /* remove byte stuffing useless byte */
  277. skb_trim(skb, i - j);
  278. /* remove ST21NFCA_SOF_EOF from head */
  279. skb_pull(skb, 1);
  280. r = check_crc(skb->data, skb->len);
  281. if (r != 0) {
  282. i = 0;
  283. return -EBADMSG;
  284. }
  285. /* remove headbyte */
  286. skb_pull(skb, 1);
  287. /* remove crc. Byte Stuffing is already removed here */
  288. skb_trim(skb, skb->len - 2);
  289. return skb->len;
  290. }
  291. return 0;
  292. }
  293. /*
  294. * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
  295. * that i2c bus will be flushed and that next read will start on a new frame.
  296. * returned skb contains only LLC header and payload.
  297. * returns:
  298. * frame size : if received frame is complete (find ST21NFCA_SOF_EOF at
  299. * end of read)
  300. * -EAGAIN : if received frame is incomplete (not find ST21NFCA_SOF_EOF
  301. * at end of read)
  302. * -EREMOTEIO : i2c read error (fatal)
  303. * -EBADMSG : frame was incorrect and discarded
  304. * (value returned from st21nfca_hci_i2c_repack)
  305. * -EIO : if no ST21NFCA_SOF_EOF is found after reaching
  306. * the read length end sequence
  307. */
  308. static int st21nfca_hci_i2c_read(struct st21nfca_i2c_phy *phy,
  309. struct sk_buff *skb)
  310. {
  311. int r, i;
  312. u8 len;
  313. u8 buf[ST21NFCA_HCI_LLC_MAX_PAYLOAD];
  314. struct i2c_client *client = phy->i2c_dev;
  315. if (phy->current_read_len < ARRAY_SIZE(len_seq)) {
  316. len = len_seq[phy->current_read_len];
  317. /*
  318. * Add retry mecanism
  319. * Operation on I2C interface may fail in case of operation on
  320. * RF or SWP interface
  321. */
  322. r = 0;
  323. mutex_lock(&phy->phy_lock);
  324. for (i = 0; i < ARRAY_SIZE(wait_tab) && r <= 0; i++) {
  325. r = i2c_master_recv(client, buf, len);
  326. if (r < 0)
  327. msleep(wait_tab[i]);
  328. }
  329. mutex_unlock(&phy->phy_lock);
  330. if (r != len) {
  331. phy->current_read_len = 0;
  332. return -EREMOTEIO;
  333. }
  334. /*
  335. * The first read sequence does not start with SOF.
  336. * Data is corrupeted so we drop it.
  337. */
  338. if (!phy->current_read_len && !IS_START_OF_FRAME(buf)) {
  339. skb_trim(skb, 0);
  340. phy->current_read_len = 0;
  341. return -EIO;
  342. } else if (phy->current_read_len && IS_START_OF_FRAME(buf)) {
  343. /*
  344. * Previous frame transmission was interrupted and
  345. * the frame got repeated.
  346. * Received frame start with ST21NFCA_SOF_EOF + 00.
  347. */
  348. skb_trim(skb, 0);
  349. phy->current_read_len = 0;
  350. }
  351. memcpy(skb_put(skb, len), buf, len);
  352. if (skb->data[skb->len - 1] == ST21NFCA_SOF_EOF) {
  353. phy->current_read_len = 0;
  354. return st21nfca_hci_i2c_repack(skb);
  355. }
  356. phy->current_read_len++;
  357. return -EAGAIN;
  358. }
  359. return -EIO;
  360. }
  361. /*
  362. * Reads an shdlc frame from the chip. This is not as straightforward as it
  363. * seems. The frame format is data-crc, and corruption can occur anywhere
  364. * while transiting on i2c bus, such that we could read an invalid data.
  365. * The tricky case is when we read a corrupted data or crc. We must detect
  366. * this here in order to determine that data can be transmitted to the hci
  367. * core. This is the reason why we check the crc here.
  368. * The CLF will repeat a frame until we send a RR on that frame.
  369. *
  370. * On ST21NFCA, IRQ goes in idle when read starts. As no size information are
  371. * available in the incoming data, other IRQ might come. Every IRQ will trigger
  372. * a read sequence with different length and will fill the current frame.
  373. * The reception is complete once we reach a ST21NFCA_SOF_EOF.
  374. */
  375. static irqreturn_t st21nfca_hci_irq_thread_fn(int irq, void *phy_id)
  376. {
  377. struct st21nfca_i2c_phy *phy = phy_id;
  378. struct i2c_client *client;
  379. int r;
  380. if (!phy || irq != phy->i2c_dev->irq) {
  381. WARN_ON_ONCE(1);
  382. return IRQ_NONE;
  383. }
  384. client = phy->i2c_dev;
  385. dev_dbg(&client->dev, "IRQ\n");
  386. if (phy->hard_fault != 0)
  387. return IRQ_HANDLED;
  388. r = st21nfca_hci_i2c_read(phy, phy->pending_skb);
  389. if (r == -EREMOTEIO) {
  390. phy->hard_fault = r;
  391. nfc_hci_recv_frame(phy->hdev, NULL);
  392. return IRQ_HANDLED;
  393. } else if (r == -EAGAIN || r == -EIO) {
  394. return IRQ_HANDLED;
  395. } else if (r == -EBADMSG && phy->crc_trials < ARRAY_SIZE(wait_tab)) {
  396. /*
  397. * With ST21NFCA, only one interface (I2C, RF or SWP)
  398. * may be active at a time.
  399. * Having incorrect crc is usually due to i2c macrocell
  400. * deactivation in the middle of a transmission.
  401. * It may generate corrupted data on i2c.
  402. * We give sometime to get i2c back.
  403. * The complete frame will be repeated.
  404. */
  405. msleep(wait_tab[phy->crc_trials]);
  406. phy->crc_trials++;
  407. phy->current_read_len = 0;
  408. kfree_skb(phy->pending_skb);
  409. } else if (r > 0) {
  410. /*
  411. * We succeeded to read data from the CLF and
  412. * data is valid.
  413. * Reset counter.
  414. */
  415. nfc_hci_recv_frame(phy->hdev, phy->pending_skb);
  416. phy->crc_trials = 0;
  417. } else {
  418. kfree_skb(phy->pending_skb);
  419. }
  420. phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
  421. if (phy->pending_skb == NULL) {
  422. phy->hard_fault = -ENOMEM;
  423. nfc_hci_recv_frame(phy->hdev, NULL);
  424. }
  425. return IRQ_HANDLED;
  426. }
  427. static struct nfc_phy_ops i2c_phy_ops = {
  428. .write = st21nfca_hci_i2c_write,
  429. .enable = st21nfca_hci_i2c_enable,
  430. .disable = st21nfca_hci_i2c_disable,
  431. };
  432. #ifdef CONFIG_OF
  433. static int st21nfca_hci_i2c_of_request_resources(struct i2c_client *client)
  434. {
  435. struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
  436. struct device_node *pp;
  437. int gpio;
  438. int r;
  439. pp = client->dev.of_node;
  440. if (!pp)
  441. return -ENODEV;
  442. /* Get GPIO from device tree */
  443. gpio = of_get_named_gpio(pp, "enable-gpios", 0);
  444. if (gpio < 0) {
  445. nfc_err(&client->dev, "Failed to retrieve enable-gpios from device tree\n");
  446. return gpio;
  447. }
  448. /* GPIO request and configuration */
  449. r = devm_gpio_request_one(&client->dev, gpio, GPIOF_OUT_INIT_HIGH,
  450. "clf_enable");
  451. if (r) {
  452. nfc_err(&client->dev, "Failed to request enable pin\n");
  453. return r;
  454. }
  455. phy->gpio_ena = gpio;
  456. phy->irq_polarity = irq_get_trigger_type(client->irq);
  457. phy->se_status.is_ese_present =
  458. of_property_read_bool(pp, "ese-present");
  459. phy->se_status.is_uicc_present =
  460. of_property_read_bool(pp, "uicc-present");
  461. return 0;
  462. }
  463. #else
  464. static int st21nfca_hci_i2c_of_request_resources(struct i2c_client *client)
  465. {
  466. return -ENODEV;
  467. }
  468. #endif
  469. static int st21nfca_hci_i2c_request_resources(struct i2c_client *client)
  470. {
  471. struct st21nfca_nfc_platform_data *pdata;
  472. struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
  473. int r;
  474. pdata = client->dev.platform_data;
  475. if (pdata == NULL) {
  476. nfc_err(&client->dev, "No platform data\n");
  477. return -EINVAL;
  478. }
  479. /* store for later use */
  480. phy->gpio_ena = pdata->gpio_ena;
  481. phy->irq_polarity = pdata->irq_polarity;
  482. if (phy->gpio_ena > 0) {
  483. r = devm_gpio_request_one(&client->dev, phy->gpio_ena,
  484. GPIOF_OUT_INIT_HIGH, "clf_enable");
  485. if (r) {
  486. pr_err("%s : ena gpio_request failed\n", __FILE__);
  487. return r;
  488. }
  489. }
  490. phy->se_status.is_ese_present = pdata->is_ese_present;
  491. phy->se_status.is_uicc_present = pdata->is_uicc_present;
  492. return 0;
  493. }
  494. static int st21nfca_hci_i2c_probe(struct i2c_client *client,
  495. const struct i2c_device_id *id)
  496. {
  497. struct st21nfca_i2c_phy *phy;
  498. struct st21nfca_nfc_platform_data *pdata;
  499. int r;
  500. dev_dbg(&client->dev, "%s\n", __func__);
  501. dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
  502. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  503. nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
  504. return -ENODEV;
  505. }
  506. phy = devm_kzalloc(&client->dev, sizeof(struct st21nfca_i2c_phy),
  507. GFP_KERNEL);
  508. if (!phy)
  509. return -ENOMEM;
  510. phy->i2c_dev = client;
  511. phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
  512. if (phy->pending_skb == NULL)
  513. return -ENOMEM;
  514. phy->current_read_len = 0;
  515. phy->crc_trials = 0;
  516. mutex_init(&phy->phy_lock);
  517. i2c_set_clientdata(client, phy);
  518. pdata = client->dev.platform_data;
  519. if (!pdata && client->dev.of_node) {
  520. r = st21nfca_hci_i2c_of_request_resources(client);
  521. if (r) {
  522. nfc_err(&client->dev, "No platform data\n");
  523. return r;
  524. }
  525. } else if (pdata) {
  526. r = st21nfca_hci_i2c_request_resources(client);
  527. if (r) {
  528. nfc_err(&client->dev, "Cannot get platform resources\n");
  529. return r;
  530. }
  531. } else {
  532. nfc_err(&client->dev, "st21nfca platform resources not available\n");
  533. return -ENODEV;
  534. }
  535. r = st21nfca_hci_platform_init(phy);
  536. if (r < 0) {
  537. nfc_err(&client->dev, "Unable to reboot st21nfca\n");
  538. return r;
  539. }
  540. r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
  541. st21nfca_hci_irq_thread_fn,
  542. phy->irq_polarity | IRQF_ONESHOT,
  543. ST21NFCA_HCI_DRIVER_NAME, phy);
  544. if (r < 0) {
  545. nfc_err(&client->dev, "Unable to register IRQ handler\n");
  546. return r;
  547. }
  548. return st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
  549. ST21NFCA_FRAME_HEADROOM,
  550. ST21NFCA_FRAME_TAILROOM,
  551. ST21NFCA_HCI_LLC_MAX_PAYLOAD,
  552. &phy->hdev,
  553. &phy->se_status);
  554. }
  555. static int st21nfca_hci_i2c_remove(struct i2c_client *client)
  556. {
  557. struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
  558. dev_dbg(&client->dev, "%s\n", __func__);
  559. st21nfca_hci_remove(phy->hdev);
  560. if (phy->powered)
  561. st21nfca_hci_i2c_disable(phy);
  562. return 0;
  563. }
  564. #ifdef CONFIG_OF
  565. static const struct of_device_id of_st21nfca_i2c_match[] = {
  566. { .compatible = "st,st21nfca-i2c", },
  567. { .compatible = "st,st21nfca_i2c", },
  568. {}
  569. };
  570. MODULE_DEVICE_TABLE(of, of_st21nfca_i2c_match);
  571. #endif
  572. static struct i2c_driver st21nfca_hci_i2c_driver = {
  573. .driver = {
  574. .owner = THIS_MODULE,
  575. .name = ST21NFCA_HCI_I2C_DRIVER_NAME,
  576. .of_match_table = of_match_ptr(of_st21nfca_i2c_match),
  577. },
  578. .probe = st21nfca_hci_i2c_probe,
  579. .id_table = st21nfca_hci_i2c_id_table,
  580. .remove = st21nfca_hci_i2c_remove,
  581. };
  582. module_i2c_driver(st21nfca_hci_i2c_driver);
  583. MODULE_LICENSE("GPL");
  584. MODULE_DESCRIPTION(DRIVER_DESC);