i2c.c 18 KB

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