i2c.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /*
  2. * I2C Link Layer for PN544 HCI based Driver
  3. *
  4. * Copyright (C) 2012 Intel Corporation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the
  17. * Free Software Foundation, Inc.,
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/crc-ccitt.h>
  22. #include <linux/module.h>
  23. #include <linux/i2c.h>
  24. #include <linux/gpio.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/delay.h>
  28. #include <linux/nfc.h>
  29. #include <linux/firmware.h>
  30. #include <linux/unaligned/access_ok.h>
  31. #include <linux/platform_data/pn544.h>
  32. #include <net/nfc/hci.h>
  33. #include <net/nfc/llc.h>
  34. #include <net/nfc/nfc.h>
  35. #include "pn544.h"
  36. #define PN544_I2C_FRAME_HEADROOM 1
  37. #define PN544_I2C_FRAME_TAILROOM 2
  38. /* framing in HCI mode */
  39. #define PN544_HCI_I2C_LLC_LEN 1
  40. #define PN544_HCI_I2C_LLC_CRC 2
  41. #define PN544_HCI_I2C_LLC_LEN_CRC (PN544_HCI_I2C_LLC_LEN + \
  42. PN544_HCI_I2C_LLC_CRC)
  43. #define PN544_HCI_I2C_LLC_MIN_SIZE (1 + PN544_HCI_I2C_LLC_LEN_CRC)
  44. #define PN544_HCI_I2C_LLC_MAX_PAYLOAD 29
  45. #define PN544_HCI_I2C_LLC_MAX_SIZE (PN544_HCI_I2C_LLC_LEN_CRC + 1 + \
  46. PN544_HCI_I2C_LLC_MAX_PAYLOAD)
  47. static struct i2c_device_id pn544_hci_i2c_id_table[] = {
  48. {"pn544", 0},
  49. {}
  50. };
  51. MODULE_DEVICE_TABLE(i2c, pn544_hci_i2c_id_table);
  52. #define PN544_HCI_I2C_DRIVER_NAME "pn544_hci_i2c"
  53. #define PN544_FW_CMD_WRITE 0x08
  54. #define PN544_FW_CMD_CHECK 0x06
  55. struct pn544_i2c_fw_frame_write {
  56. u8 cmd;
  57. u16 be_length;
  58. u8 be_dest_addr[3];
  59. u16 be_datalen;
  60. u8 data[];
  61. } __packed;
  62. struct pn544_i2c_fw_frame_check {
  63. u8 cmd;
  64. u16 be_length;
  65. u8 be_start_addr[3];
  66. u16 be_datalen;
  67. u16 be_crc;
  68. } __packed;
  69. struct pn544_i2c_fw_frame_response {
  70. u8 status;
  71. u16 be_length;
  72. } __packed;
  73. struct pn544_i2c_fw_blob {
  74. u32 be_size;
  75. u32 be_destaddr;
  76. u8 data[];
  77. };
  78. #define PN544_FW_CMD_RESULT_TIMEOUT 0x01
  79. #define PN544_FW_CMD_RESULT_BAD_CRC 0x02
  80. #define PN544_FW_CMD_RESULT_ACCESS_DENIED 0x08
  81. #define PN544_FW_CMD_RESULT_PROTOCOL_ERROR 0x0B
  82. #define PN544_FW_CMD_RESULT_INVALID_PARAMETER 0x11
  83. #define PN544_FW_CMD_RESULT_INVALID_LENGTH 0x18
  84. #define PN544_FW_CMD_RESULT_WRITE_FAILED 0x74
  85. #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
  86. #define PN544_FW_WRITE_BUFFER_MAX_LEN 0x9f7
  87. #define PN544_FW_I2C_MAX_PAYLOAD PN544_HCI_I2C_LLC_MAX_SIZE
  88. #define PN544_FW_I2C_WRITE_FRAME_HEADER_LEN 8
  89. #define PN544_FW_I2C_WRITE_DATA_MAX_LEN MIN((PN544_FW_I2C_MAX_PAYLOAD -\
  90. PN544_FW_I2C_WRITE_FRAME_HEADER_LEN),\
  91. PN544_FW_WRITE_BUFFER_MAX_LEN)
  92. #define FW_WORK_STATE_IDLE 1
  93. #define FW_WORK_STATE_START 2
  94. #define FW_WORK_STATE_WAIT_WRITE_ANSWER 3
  95. #define FW_WORK_STATE_WAIT_CHECK_ANSWER 4
  96. struct pn544_i2c_phy {
  97. struct i2c_client *i2c_dev;
  98. struct nfc_hci_dev *hdev;
  99. unsigned int gpio_en;
  100. unsigned int gpio_irq;
  101. unsigned int gpio_fw;
  102. unsigned int en_polarity;
  103. struct work_struct fw_work;
  104. int fw_work_state;
  105. char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
  106. const struct firmware *fw;
  107. u32 fw_blob_dest_addr;
  108. size_t fw_blob_size;
  109. const u8 *fw_blob_data;
  110. size_t fw_written;
  111. int fw_cmd_result;
  112. int powered;
  113. int run_mode;
  114. int hard_fault; /*
  115. * < 0 if hardware error occured (e.g. i2c err)
  116. * and prevents normal operation.
  117. */
  118. };
  119. #define I2C_DUMP_SKB(info, skb) \
  120. do { \
  121. pr_debug("%s:\n", info); \
  122. print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
  123. 16, 1, (skb)->data, (skb)->len, 0); \
  124. } while (0)
  125. static void pn544_hci_i2c_platform_init(struct pn544_i2c_phy *phy)
  126. {
  127. int polarity, retry, ret;
  128. char rset_cmd[] = { 0x05, 0xF9, 0x04, 0x00, 0xC3, 0xE5 };
  129. int count = sizeof(rset_cmd);
  130. nfc_info(&phy->i2c_dev->dev, "Detecting nfc_en polarity\n");
  131. /* Disable fw download */
  132. gpio_set_value(phy->gpio_fw, 0);
  133. for (polarity = 0; polarity < 2; polarity++) {
  134. phy->en_polarity = polarity;
  135. retry = 3;
  136. while (retry--) {
  137. /* power off */
  138. gpio_set_value(phy->gpio_en, !phy->en_polarity);
  139. usleep_range(10000, 15000);
  140. /* power on */
  141. gpio_set_value(phy->gpio_en, phy->en_polarity);
  142. usleep_range(10000, 15000);
  143. /* send reset */
  144. dev_dbg(&phy->i2c_dev->dev, "Sending reset cmd\n");
  145. ret = i2c_master_send(phy->i2c_dev, rset_cmd, count);
  146. if (ret == count) {
  147. nfc_info(&phy->i2c_dev->dev,
  148. "nfc_en polarity : active %s\n",
  149. (polarity == 0 ? "low" : "high"));
  150. goto out;
  151. }
  152. }
  153. }
  154. nfc_err(&phy->i2c_dev->dev,
  155. "Could not detect nfc_en polarity, fallback to active high\n");
  156. out:
  157. gpio_set_value(phy->gpio_en, !phy->en_polarity);
  158. }
  159. static void pn544_hci_i2c_enable_mode(struct pn544_i2c_phy *phy, int run_mode)
  160. {
  161. gpio_set_value(phy->gpio_fw, run_mode == PN544_FW_MODE ? 1 : 0);
  162. gpio_set_value(phy->gpio_en, phy->en_polarity);
  163. usleep_range(10000, 15000);
  164. phy->run_mode = run_mode;
  165. }
  166. static int pn544_hci_i2c_enable(void *phy_id)
  167. {
  168. struct pn544_i2c_phy *phy = phy_id;
  169. pr_info("%s\n", __func__);
  170. pn544_hci_i2c_enable_mode(phy, PN544_HCI_MODE);
  171. phy->powered = 1;
  172. return 0;
  173. }
  174. static void pn544_hci_i2c_disable(void *phy_id)
  175. {
  176. struct pn544_i2c_phy *phy = phy_id;
  177. gpio_set_value(phy->gpio_fw, 0);
  178. gpio_set_value(phy->gpio_en, !phy->en_polarity);
  179. usleep_range(10000, 15000);
  180. gpio_set_value(phy->gpio_en, phy->en_polarity);
  181. usleep_range(10000, 15000);
  182. gpio_set_value(phy->gpio_en, !phy->en_polarity);
  183. usleep_range(10000, 15000);
  184. phy->powered = 0;
  185. }
  186. static void pn544_hci_i2c_add_len_crc(struct sk_buff *skb)
  187. {
  188. u16 crc;
  189. int len;
  190. len = skb->len + 2;
  191. *skb_push(skb, 1) = len;
  192. crc = crc_ccitt(0xffff, skb->data, skb->len);
  193. crc = ~crc;
  194. *skb_put(skb, 1) = crc & 0xff;
  195. *skb_put(skb, 1) = crc >> 8;
  196. }
  197. static void pn544_hci_i2c_remove_len_crc(struct sk_buff *skb)
  198. {
  199. skb_pull(skb, PN544_I2C_FRAME_HEADROOM);
  200. skb_trim(skb, PN544_I2C_FRAME_TAILROOM);
  201. }
  202. /*
  203. * Writing a frame must not return the number of written bytes.
  204. * It must return either zero for success, or <0 for error.
  205. * In addition, it must not alter the skb
  206. */
  207. static int pn544_hci_i2c_write(void *phy_id, struct sk_buff *skb)
  208. {
  209. int r;
  210. struct pn544_i2c_phy *phy = phy_id;
  211. struct i2c_client *client = phy->i2c_dev;
  212. if (phy->hard_fault != 0)
  213. return phy->hard_fault;
  214. usleep_range(3000, 6000);
  215. pn544_hci_i2c_add_len_crc(skb);
  216. I2C_DUMP_SKB("i2c frame written", skb);
  217. r = i2c_master_send(client, skb->data, skb->len);
  218. if (r == -EREMOTEIO) { /* Retry, chip was in standby */
  219. usleep_range(6000, 10000);
  220. r = i2c_master_send(client, skb->data, skb->len);
  221. }
  222. if (r >= 0) {
  223. if (r != skb->len)
  224. r = -EREMOTEIO;
  225. else
  226. r = 0;
  227. }
  228. pn544_hci_i2c_remove_len_crc(skb);
  229. return r;
  230. }
  231. static int check_crc(u8 *buf, int buflen)
  232. {
  233. int len;
  234. u16 crc;
  235. len = buf[0] + 1;
  236. crc = crc_ccitt(0xffff, buf, len - 2);
  237. crc = ~crc;
  238. if (buf[len - 2] != (crc & 0xff) || buf[len - 1] != (crc >> 8)) {
  239. pr_err("CRC error 0x%x != 0x%x 0x%x\n",
  240. crc, buf[len - 1], buf[len - 2]);
  241. pr_info("%s: BAD CRC\n", __func__);
  242. print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
  243. 16, 2, buf, buflen, false);
  244. return -EPERM;
  245. }
  246. return 0;
  247. }
  248. /*
  249. * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
  250. * that i2c bus will be flushed and that next read will start on a new frame.
  251. * returned skb contains only LLC header and payload.
  252. * returns:
  253. * -EREMOTEIO : i2c read error (fatal)
  254. * -EBADMSG : frame was incorrect and discarded
  255. * -ENOMEM : cannot allocate skb, frame dropped
  256. */
  257. static int pn544_hci_i2c_read(struct pn544_i2c_phy *phy, struct sk_buff **skb)
  258. {
  259. int r;
  260. u8 len;
  261. u8 tmp[PN544_HCI_I2C_LLC_MAX_SIZE - 1];
  262. struct i2c_client *client = phy->i2c_dev;
  263. r = i2c_master_recv(client, &len, 1);
  264. if (r != 1) {
  265. nfc_err(&client->dev, "cannot read len byte\n");
  266. return -EREMOTEIO;
  267. }
  268. if ((len < (PN544_HCI_I2C_LLC_MIN_SIZE - 1)) ||
  269. (len > (PN544_HCI_I2C_LLC_MAX_SIZE - 1))) {
  270. nfc_err(&client->dev, "invalid len byte\n");
  271. r = -EBADMSG;
  272. goto flush;
  273. }
  274. *skb = alloc_skb(1 + len, GFP_KERNEL);
  275. if (*skb == NULL) {
  276. r = -ENOMEM;
  277. goto flush;
  278. }
  279. *skb_put(*skb, 1) = len;
  280. r = i2c_master_recv(client, skb_put(*skb, len), len);
  281. if (r != len) {
  282. kfree_skb(*skb);
  283. return -EREMOTEIO;
  284. }
  285. I2C_DUMP_SKB("i2c frame read", *skb);
  286. r = check_crc((*skb)->data, (*skb)->len);
  287. if (r != 0) {
  288. kfree_skb(*skb);
  289. r = -EBADMSG;
  290. goto flush;
  291. }
  292. skb_pull(*skb, 1);
  293. skb_trim(*skb, (*skb)->len - 2);
  294. usleep_range(3000, 6000);
  295. return 0;
  296. flush:
  297. if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
  298. r = -EREMOTEIO;
  299. usleep_range(3000, 6000);
  300. return r;
  301. }
  302. static int pn544_hci_i2c_fw_read_status(struct pn544_i2c_phy *phy)
  303. {
  304. int r;
  305. struct pn544_i2c_fw_frame_response response;
  306. struct i2c_client *client = phy->i2c_dev;
  307. r = i2c_master_recv(client, (char *) &response, sizeof(response));
  308. if (r != sizeof(response)) {
  309. nfc_err(&client->dev, "cannot read fw status\n");
  310. return -EIO;
  311. }
  312. usleep_range(3000, 6000);
  313. switch (response.status) {
  314. case 0:
  315. return 0;
  316. case PN544_FW_CMD_RESULT_TIMEOUT:
  317. return -ETIMEDOUT;
  318. case PN544_FW_CMD_RESULT_BAD_CRC:
  319. return -ENODATA;
  320. case PN544_FW_CMD_RESULT_ACCESS_DENIED:
  321. return -EACCES;
  322. case PN544_FW_CMD_RESULT_PROTOCOL_ERROR:
  323. return -EPROTO;
  324. case PN544_FW_CMD_RESULT_INVALID_PARAMETER:
  325. return -EINVAL;
  326. case PN544_FW_CMD_RESULT_INVALID_LENGTH:
  327. return -EBADMSG;
  328. case PN544_FW_CMD_RESULT_WRITE_FAILED:
  329. return -EIO;
  330. default:
  331. return -EIO;
  332. }
  333. }
  334. /*
  335. * Reads an shdlc frame from the chip. This is not as straightforward as it
  336. * seems. There are cases where we could loose the frame start synchronization.
  337. * The frame format is len-data-crc, and corruption can occur anywhere while
  338. * transiting on i2c bus, such that we could read an invalid len.
  339. * In order to recover synchronization with the next frame, we must be sure
  340. * to read the real amount of data without using the len byte. We do this by
  341. * assuming the following:
  342. * - the chip will always present only one single complete frame on the bus
  343. * before triggering the interrupt
  344. * - the chip will not present a new frame until we have completely read
  345. * the previous one (or until we have handled the interrupt).
  346. * The tricky case is when we read a corrupted len that is less than the real
  347. * len. We must detect this here in order to determine that we need to flush
  348. * the bus. This is the reason why we check the crc here.
  349. */
  350. static irqreturn_t pn544_hci_i2c_irq_thread_fn(int irq, void *phy_id)
  351. {
  352. struct pn544_i2c_phy *phy = phy_id;
  353. struct i2c_client *client;
  354. struct sk_buff *skb = NULL;
  355. int r;
  356. if (!phy || irq != phy->i2c_dev->irq) {
  357. WARN_ON_ONCE(1);
  358. return IRQ_NONE;
  359. }
  360. client = phy->i2c_dev;
  361. dev_dbg(&client->dev, "IRQ\n");
  362. if (phy->hard_fault != 0)
  363. return IRQ_HANDLED;
  364. if (phy->run_mode == PN544_FW_MODE) {
  365. phy->fw_cmd_result = pn544_hci_i2c_fw_read_status(phy);
  366. schedule_work(&phy->fw_work);
  367. } else {
  368. r = pn544_hci_i2c_read(phy, &skb);
  369. if (r == -EREMOTEIO) {
  370. phy->hard_fault = r;
  371. nfc_hci_recv_frame(phy->hdev, NULL);
  372. return IRQ_HANDLED;
  373. } else if ((r == -ENOMEM) || (r == -EBADMSG)) {
  374. return IRQ_HANDLED;
  375. }
  376. nfc_hci_recv_frame(phy->hdev, skb);
  377. }
  378. return IRQ_HANDLED;
  379. }
  380. static struct nfc_phy_ops i2c_phy_ops = {
  381. .write = pn544_hci_i2c_write,
  382. .enable = pn544_hci_i2c_enable,
  383. .disable = pn544_hci_i2c_disable,
  384. };
  385. static int pn544_hci_i2c_fw_download(void *phy_id, const char *firmware_name)
  386. {
  387. struct pn544_i2c_phy *phy = phy_id;
  388. pr_info("Starting Firmware Download (%s)\n", firmware_name);
  389. strcpy(phy->firmware_name, firmware_name);
  390. phy->fw_work_state = FW_WORK_STATE_START;
  391. schedule_work(&phy->fw_work);
  392. return 0;
  393. }
  394. static void pn544_hci_i2c_fw_work_complete(struct pn544_i2c_phy *phy,
  395. int result)
  396. {
  397. pr_info("Firmware Download Complete, result=%d\n", result);
  398. pn544_hci_i2c_disable(phy);
  399. phy->fw_work_state = FW_WORK_STATE_IDLE;
  400. if (phy->fw) {
  401. release_firmware(phy->fw);
  402. phy->fw = NULL;
  403. }
  404. nfc_fw_download_done(phy->hdev->ndev, phy->firmware_name, (u32) -result);
  405. }
  406. static int pn544_hci_i2c_fw_write_cmd(struct i2c_client *client, u32 dest_addr,
  407. const u8 *data, u16 datalen)
  408. {
  409. u8 frame[PN544_FW_I2C_MAX_PAYLOAD];
  410. struct pn544_i2c_fw_frame_write *framep;
  411. u16 params_len;
  412. int framelen;
  413. int r;
  414. if (datalen > PN544_FW_I2C_WRITE_DATA_MAX_LEN)
  415. datalen = PN544_FW_I2C_WRITE_DATA_MAX_LEN;
  416. framep = (struct pn544_i2c_fw_frame_write *) frame;
  417. params_len = sizeof(framep->be_dest_addr) +
  418. sizeof(framep->be_datalen) + datalen;
  419. framelen = params_len + sizeof(framep->cmd) +
  420. sizeof(framep->be_length);
  421. framep->cmd = PN544_FW_CMD_WRITE;
  422. put_unaligned_be16(params_len, &framep->be_length);
  423. framep->be_dest_addr[0] = (dest_addr & 0xff0000) >> 16;
  424. framep->be_dest_addr[1] = (dest_addr & 0xff00) >> 8;
  425. framep->be_dest_addr[2] = dest_addr & 0xff;
  426. put_unaligned_be16(datalen, &framep->be_datalen);
  427. memcpy(framep->data, data, datalen);
  428. r = i2c_master_send(client, frame, framelen);
  429. if (r == framelen)
  430. return datalen;
  431. else if (r < 0)
  432. return r;
  433. else
  434. return -EIO;
  435. }
  436. static int pn544_hci_i2c_fw_check_cmd(struct i2c_client *client, u32 start_addr,
  437. const u8 *data, u16 datalen)
  438. {
  439. struct pn544_i2c_fw_frame_check frame;
  440. int r;
  441. u16 crc;
  442. /* calculate local crc for the data we want to check */
  443. crc = crc_ccitt(0xffff, data, datalen);
  444. frame.cmd = PN544_FW_CMD_CHECK;
  445. put_unaligned_be16(sizeof(frame.be_start_addr) +
  446. sizeof(frame.be_datalen) + sizeof(frame.be_crc),
  447. &frame.be_length);
  448. /* tell the chip the memory region to which our crc applies */
  449. frame.be_start_addr[0] = (start_addr & 0xff0000) >> 16;
  450. frame.be_start_addr[1] = (start_addr & 0xff00) >> 8;
  451. frame.be_start_addr[2] = start_addr & 0xff;
  452. put_unaligned_be16(datalen, &frame.be_datalen);
  453. /*
  454. * and give our local crc. Chip will calculate its own crc for the
  455. * region and compare with ours.
  456. */
  457. put_unaligned_be16(crc, &frame.be_crc);
  458. r = i2c_master_send(client, (const char *) &frame, sizeof(frame));
  459. if (r == sizeof(frame))
  460. return 0;
  461. else if (r < 0)
  462. return r;
  463. else
  464. return -EIO;
  465. }
  466. static int pn544_hci_i2c_fw_write_chunk(struct pn544_i2c_phy *phy)
  467. {
  468. int r;
  469. r = pn544_hci_i2c_fw_write_cmd(phy->i2c_dev,
  470. phy->fw_blob_dest_addr + phy->fw_written,
  471. phy->fw_blob_data + phy->fw_written,
  472. phy->fw_blob_size - phy->fw_written);
  473. if (r < 0)
  474. return r;
  475. phy->fw_written += r;
  476. phy->fw_work_state = FW_WORK_STATE_WAIT_WRITE_ANSWER;
  477. return 0;
  478. }
  479. static void pn544_hci_i2c_fw_work(struct work_struct *work)
  480. {
  481. struct pn544_i2c_phy *phy = container_of(work, struct pn544_i2c_phy,
  482. fw_work);
  483. int r;
  484. struct pn544_i2c_fw_blob *blob;
  485. switch (phy->fw_work_state) {
  486. case FW_WORK_STATE_START:
  487. pn544_hci_i2c_enable_mode(phy, PN544_FW_MODE);
  488. r = request_firmware(&phy->fw, phy->firmware_name,
  489. &phy->i2c_dev->dev);
  490. if (r < 0)
  491. goto exit_state_start;
  492. blob = (struct pn544_i2c_fw_blob *) phy->fw->data;
  493. phy->fw_blob_size = get_unaligned_be32(&blob->be_size);
  494. phy->fw_blob_dest_addr = get_unaligned_be32(&blob->be_destaddr);
  495. phy->fw_blob_data = blob->data;
  496. phy->fw_written = 0;
  497. r = pn544_hci_i2c_fw_write_chunk(phy);
  498. exit_state_start:
  499. if (r < 0)
  500. pn544_hci_i2c_fw_work_complete(phy, r);
  501. break;
  502. case FW_WORK_STATE_WAIT_WRITE_ANSWER:
  503. r = phy->fw_cmd_result;
  504. if (r < 0)
  505. goto exit_state_wait_write_answer;
  506. if (phy->fw_written == phy->fw_blob_size) {
  507. r = pn544_hci_i2c_fw_check_cmd(phy->i2c_dev,
  508. phy->fw_blob_dest_addr,
  509. phy->fw_blob_data,
  510. phy->fw_blob_size);
  511. if (r < 0)
  512. goto exit_state_wait_write_answer;
  513. phy->fw_work_state = FW_WORK_STATE_WAIT_CHECK_ANSWER;
  514. break;
  515. }
  516. r = pn544_hci_i2c_fw_write_chunk(phy);
  517. exit_state_wait_write_answer:
  518. if (r < 0)
  519. pn544_hci_i2c_fw_work_complete(phy, r);
  520. break;
  521. case FW_WORK_STATE_WAIT_CHECK_ANSWER:
  522. r = phy->fw_cmd_result;
  523. if (r < 0)
  524. goto exit_state_wait_check_answer;
  525. blob = (struct pn544_i2c_fw_blob *) (phy->fw_blob_data +
  526. phy->fw_blob_size);
  527. phy->fw_blob_size = get_unaligned_be32(&blob->be_size);
  528. if (phy->fw_blob_size != 0) {
  529. phy->fw_blob_dest_addr =
  530. get_unaligned_be32(&blob->be_destaddr);
  531. phy->fw_blob_data = blob->data;
  532. phy->fw_written = 0;
  533. r = pn544_hci_i2c_fw_write_chunk(phy);
  534. }
  535. exit_state_wait_check_answer:
  536. if (r < 0 || phy->fw_blob_size == 0)
  537. pn544_hci_i2c_fw_work_complete(phy, r);
  538. break;
  539. default:
  540. break;
  541. }
  542. }
  543. static int pn544_hci_i2c_probe(struct i2c_client *client,
  544. const struct i2c_device_id *id)
  545. {
  546. struct pn544_i2c_phy *phy;
  547. struct pn544_nfc_platform_data *pdata;
  548. int r = 0;
  549. dev_dbg(&client->dev, "%s\n", __func__);
  550. dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
  551. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  552. nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
  553. return -ENODEV;
  554. }
  555. phy = devm_kzalloc(&client->dev, sizeof(struct pn544_i2c_phy),
  556. GFP_KERNEL);
  557. if (!phy) {
  558. nfc_err(&client->dev,
  559. "Cannot allocate memory for pn544 i2c phy.\n");
  560. return -ENOMEM;
  561. }
  562. INIT_WORK(&phy->fw_work, pn544_hci_i2c_fw_work);
  563. phy->fw_work_state = FW_WORK_STATE_IDLE;
  564. phy->i2c_dev = client;
  565. i2c_set_clientdata(client, phy);
  566. pdata = client->dev.platform_data;
  567. if (pdata == NULL) {
  568. nfc_err(&client->dev, "No platform data\n");
  569. return -EINVAL;
  570. }
  571. if (pdata->request_resources == NULL) {
  572. nfc_err(&client->dev, "request_resources() missing\n");
  573. return -EINVAL;
  574. }
  575. r = pdata->request_resources(client);
  576. if (r) {
  577. nfc_err(&client->dev, "Cannot get platform resources\n");
  578. return r;
  579. }
  580. phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE);
  581. phy->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET);
  582. phy->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ);
  583. pn544_hci_i2c_platform_init(phy);
  584. r = request_threaded_irq(client->irq, NULL, pn544_hci_i2c_irq_thread_fn,
  585. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  586. PN544_HCI_I2C_DRIVER_NAME, phy);
  587. if (r < 0) {
  588. nfc_err(&client->dev, "Unable to register IRQ handler\n");
  589. goto err_rti;
  590. }
  591. r = pn544_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
  592. PN544_I2C_FRAME_HEADROOM, PN544_I2C_FRAME_TAILROOM,
  593. PN544_HCI_I2C_LLC_MAX_PAYLOAD,
  594. pn544_hci_i2c_fw_download, &phy->hdev);
  595. if (r < 0)
  596. goto err_hci;
  597. return 0;
  598. err_hci:
  599. free_irq(client->irq, phy);
  600. err_rti:
  601. if (pdata->free_resources != NULL)
  602. pdata->free_resources();
  603. return r;
  604. }
  605. static int pn544_hci_i2c_remove(struct i2c_client *client)
  606. {
  607. struct pn544_i2c_phy *phy = i2c_get_clientdata(client);
  608. struct pn544_nfc_platform_data *pdata = client->dev.platform_data;
  609. dev_dbg(&client->dev, "%s\n", __func__);
  610. cancel_work_sync(&phy->fw_work);
  611. if (phy->fw_work_state != FW_WORK_STATE_IDLE)
  612. pn544_hci_i2c_fw_work_complete(phy, -ENODEV);
  613. pn544_hci_remove(phy->hdev);
  614. if (phy->powered)
  615. pn544_hci_i2c_disable(phy);
  616. free_irq(client->irq, phy);
  617. if (pdata->free_resources)
  618. pdata->free_resources();
  619. return 0;
  620. }
  621. static struct i2c_driver pn544_hci_i2c_driver = {
  622. .driver = {
  623. .name = PN544_HCI_I2C_DRIVER_NAME,
  624. },
  625. .probe = pn544_hci_i2c_probe,
  626. .id_table = pn544_hci_i2c_id_table,
  627. .remove = pn544_hci_i2c_remove,
  628. };
  629. module_i2c_driver(pn544_hci_i2c_driver);
  630. MODULE_LICENSE("GPL");
  631. MODULE_DESCRIPTION(DRIVER_DESC);