i2c.c 19 KB

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