i2c.c 19 KB

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