Lines Matching +full:i2c +full:- +full:retry +full:- +full:count

1 // SPDX-License-Identifier: GPL-2.0-only
3 * I2C Link Layer for PN544 HCI based Driver
10 #include <linux/crc-ccitt.h>
12 #include <linux/i2c.h>
51 MODULE_DEVICE_TABLE(i2c, pn544_hci_i2c_id_table);
134 #define PN544_FW_I2C_WRITE_DATA_MAX_LEN MIN((PN544_FW_I2C_MAX_PAYLOAD -\
138 #define PN544_FW_SECURE_CHUNK_WRITE_DATA_MAX_LEN (PN544_FW_I2C_MAX_PAYLOAD -\
176 * < 0 if hardware error occured (e.g. i2c err)
184 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
185 16, 1, (skb)->data, (skb)->len, 0); \
190 int polarity, retry, ret; in pn544_hci_i2c_platform_init() local
192 int count = sizeof(rset_cmd); in pn544_hci_i2c_platform_init() local
194 nfc_info(&phy->i2c_dev->dev, "Detecting nfc_en polarity\n"); in pn544_hci_i2c_platform_init()
197 gpiod_set_value_cansleep(phy->gpiod_fw, 0); in pn544_hci_i2c_platform_init()
200 phy->en_polarity = polarity; in pn544_hci_i2c_platform_init()
201 retry = 3; in pn544_hci_i2c_platform_init()
202 while (retry--) { in pn544_hci_i2c_platform_init()
204 gpiod_set_value_cansleep(phy->gpiod_en, !phy->en_polarity); in pn544_hci_i2c_platform_init()
208 gpiod_set_value_cansleep(phy->gpiod_en, phy->en_polarity); in pn544_hci_i2c_platform_init()
212 dev_dbg(&phy->i2c_dev->dev, "Sending reset cmd\n"); in pn544_hci_i2c_platform_init()
213 ret = i2c_master_send(phy->i2c_dev, rset_cmd, count); in pn544_hci_i2c_platform_init()
214 if (ret == count) { in pn544_hci_i2c_platform_init()
215 nfc_info(&phy->i2c_dev->dev, in pn544_hci_i2c_platform_init()
223 nfc_err(&phy->i2c_dev->dev, in pn544_hci_i2c_platform_init()
227 gpiod_set_value_cansleep(phy->gpiod_en, !phy->en_polarity); in pn544_hci_i2c_platform_init()
233 gpiod_set_value_cansleep(phy->gpiod_fw, run_mode == PN544_FW_MODE ? 1 : 0); in pn544_hci_i2c_enable_mode()
234 gpiod_set_value_cansleep(phy->gpiod_en, phy->en_polarity); in pn544_hci_i2c_enable_mode()
237 phy->run_mode = run_mode; in pn544_hci_i2c_enable_mode()
248 phy->powered = 1; in pn544_hci_i2c_enable()
257 gpiod_set_value_cansleep(phy->gpiod_fw, 0); in pn544_hci_i2c_disable()
258 gpiod_set_value_cansleep(phy->gpiod_en, !phy->en_polarity); in pn544_hci_i2c_disable()
261 gpiod_set_value_cansleep(phy->gpiod_en, phy->en_polarity); in pn544_hci_i2c_disable()
264 gpiod_set_value_cansleep(phy->gpiod_en, !phy->en_polarity); in pn544_hci_i2c_disable()
267 phy->powered = 0; in pn544_hci_i2c_disable()
275 len = skb->len + 2; in pn544_hci_i2c_add_len_crc()
278 crc = crc_ccitt(0xffff, skb->data, skb->len); in pn544_hci_i2c_add_len_crc()
299 struct i2c_client *client = phy->i2c_dev; in pn544_hci_i2c_write()
301 if (phy->hard_fault != 0) in pn544_hci_i2c_write()
302 return phy->hard_fault; in pn544_hci_i2c_write()
308 I2C_DUMP_SKB("i2c frame written", skb); in pn544_hci_i2c_write()
310 r = i2c_master_send(client, skb->data, skb->len); in pn544_hci_i2c_write()
312 if (r == -EREMOTEIO) { /* Retry, chip was in standby */ in pn544_hci_i2c_write()
314 r = i2c_master_send(client, skb->data, skb->len); in pn544_hci_i2c_write()
318 if (r != skb->len) in pn544_hci_i2c_write()
319 r = -EREMOTEIO; in pn544_hci_i2c_write()
335 crc = crc_ccitt(0xffff, buf, len - 2); in check_crc()
338 if (buf[len - 2] != (crc & 0xff) || buf[len - 1] != (crc >> 8)) { in check_crc()
340 crc, buf[len - 1], buf[len - 2]); in check_crc()
344 return -EPERM; in check_crc()
351 * that i2c bus will be flushed and that next read will start on a new frame.
354 * -EREMOTEIO : i2c read error (fatal)
355 * -EBADMSG : frame was incorrect and discarded
356 * -ENOMEM : cannot allocate skb, frame dropped
362 u8 tmp[PN544_HCI_I2C_LLC_MAX_SIZE - 1]; in pn544_hci_i2c_read()
363 struct i2c_client *client = phy->i2c_dev; in pn544_hci_i2c_read()
367 nfc_err(&client->dev, "cannot read len byte\n"); in pn544_hci_i2c_read()
368 return -EREMOTEIO; in pn544_hci_i2c_read()
371 if ((len < (PN544_HCI_I2C_LLC_MIN_SIZE - 1)) || in pn544_hci_i2c_read()
372 (len > (PN544_HCI_I2C_LLC_MAX_SIZE - 1))) { in pn544_hci_i2c_read()
373 nfc_err(&client->dev, "invalid len byte\n"); in pn544_hci_i2c_read()
374 r = -EBADMSG; in pn544_hci_i2c_read()
380 r = -ENOMEM; in pn544_hci_i2c_read()
389 return -EREMOTEIO; in pn544_hci_i2c_read()
392 I2C_DUMP_SKB("i2c frame read", *skb); in pn544_hci_i2c_read()
394 r = check_crc((*skb)->data, (*skb)->len); in pn544_hci_i2c_read()
397 r = -EBADMSG; in pn544_hci_i2c_read()
402 skb_trim(*skb, (*skb)->len - 2); in pn544_hci_i2c_read()
410 r = -EREMOTEIO; in pn544_hci_i2c_read()
421 struct i2c_client *client = phy->i2c_dev; in pn544_hci_i2c_fw_read_status()
425 nfc_err(&client->dev, "cannot read fw status\n"); in pn544_hci_i2c_fw_read_status()
426 return -EIO; in pn544_hci_i2c_fw_read_status()
437 return -ETIMEDOUT; in pn544_hci_i2c_fw_read_status()
439 return -ENODATA; in pn544_hci_i2c_fw_read_status()
441 return -EACCES; in pn544_hci_i2c_fw_read_status()
443 return -EPROTO; in pn544_hci_i2c_fw_read_status()
445 return -EINVAL; in pn544_hci_i2c_fw_read_status()
447 return -ENOTSUPP; in pn544_hci_i2c_fw_read_status()
449 return -EBADMSG; in pn544_hci_i2c_fw_read_status()
451 return -ENOKEY; in pn544_hci_i2c_fw_read_status()
453 return -EINVAL; in pn544_hci_i2c_fw_read_status()
455 return -ENOMEM; in pn544_hci_i2c_fw_read_status()
457 return -EACCES; in pn544_hci_i2c_fw_read_status()
460 return -EIO; in pn544_hci_i2c_fw_read_status()
462 return -EIO; in pn544_hci_i2c_fw_read_status()
469 * The frame format is len-data-crc, and corruption can occur anywhere while
470 * transiting on i2c bus, such that we could read an invalid len.
474 * - the chip will always present only one single complete frame on the bus
476 * - the chip will not present a new frame until we have completely read
489 if (!phy || irq != phy->i2c_dev->irq) { in pn544_hci_i2c_irq_thread_fn()
494 client = phy->i2c_dev; in pn544_hci_i2c_irq_thread_fn()
495 dev_dbg(&client->dev, "IRQ\n"); in pn544_hci_i2c_irq_thread_fn()
497 if (phy->hard_fault != 0) in pn544_hci_i2c_irq_thread_fn()
500 if (phy->run_mode == PN544_FW_MODE) { in pn544_hci_i2c_irq_thread_fn()
501 phy->fw_cmd_result = pn544_hci_i2c_fw_read_status(phy); in pn544_hci_i2c_irq_thread_fn()
502 schedule_work(&phy->fw_work); in pn544_hci_i2c_irq_thread_fn()
505 if (r == -EREMOTEIO) { in pn544_hci_i2c_irq_thread_fn()
506 phy->hard_fault = r; in pn544_hci_i2c_irq_thread_fn()
508 nfc_hci_recv_frame(phy->hdev, NULL); in pn544_hci_i2c_irq_thread_fn()
511 } else if ((r == -ENOMEM) || (r == -EBADMSG)) { in pn544_hci_i2c_irq_thread_fn()
515 nfc_hci_recv_frame(phy->hdev, skb); in pn544_hci_i2c_irq_thread_fn()
533 strcpy(phy->firmware_name, firmware_name); in pn544_hci_i2c_fw_download()
535 phy->hw_variant = hw_variant; in pn544_hci_i2c_fw_download()
536 phy->fw_work_state = FW_WORK_STATE_START; in pn544_hci_i2c_fw_download()
538 schedule_work(&phy->fw_work); in pn544_hci_i2c_fw_download()
550 phy->fw_work_state = FW_WORK_STATE_IDLE; in pn544_hci_i2c_fw_work_complete()
552 if (phy->fw) { in pn544_hci_i2c_fw_work_complete()
553 release_firmware(phy->fw); in pn544_hci_i2c_fw_work_complete()
554 phy->fw = NULL; in pn544_hci_i2c_fw_work_complete()
557 nfc_fw_download_done(phy->hdev->ndev, phy->firmware_name, (u32) -result); in pn544_hci_i2c_fw_work_complete()
574 params_len = sizeof(framep->be_dest_addr) + in pn544_hci_i2c_fw_write_cmd()
575 sizeof(framep->be_datalen) + datalen; in pn544_hci_i2c_fw_write_cmd()
576 framelen = params_len + sizeof(framep->cmd) + in pn544_hci_i2c_fw_write_cmd()
577 sizeof(framep->be_length); in pn544_hci_i2c_fw_write_cmd()
579 framep->cmd = PN544_FW_CMD_WRITE; in pn544_hci_i2c_fw_write_cmd()
581 put_unaligned_be16(params_len, &framep->be_length); in pn544_hci_i2c_fw_write_cmd()
583 framep->be_dest_addr[0] = (dest_addr & 0xff0000) >> 16; in pn544_hci_i2c_fw_write_cmd()
584 framep->be_dest_addr[1] = (dest_addr & 0xff00) >> 8; in pn544_hci_i2c_fw_write_cmd()
585 framep->be_dest_addr[2] = dest_addr & 0xff; in pn544_hci_i2c_fw_write_cmd()
587 put_unaligned_be16(datalen, &framep->be_datalen); in pn544_hci_i2c_fw_write_cmd()
589 memcpy(framep->data, data, datalen); in pn544_hci_i2c_fw_write_cmd()
598 return -EIO; in pn544_hci_i2c_fw_write_cmd()
637 return -EIO; in pn544_hci_i2c_fw_check_cmd()
644 r = pn544_hci_i2c_fw_write_cmd(phy->i2c_dev, in pn544_hci_i2c_fw_write_chunk()
645 phy->fw_blob_dest_addr + phy->fw_written, in pn544_hci_i2c_fw_write_chunk()
646 phy->fw_blob_data + phy->fw_written, in pn544_hci_i2c_fw_write_chunk()
647 phy->fw_blob_size - phy->fw_written); in pn544_hci_i2c_fw_write_chunk()
651 phy->fw_written += r; in pn544_hci_i2c_fw_write_chunk()
652 phy->fw_work_state = FW_WORK_STATE_WAIT_WRITE_ANSWER; in pn544_hci_i2c_fw_write_chunk()
670 chunk->cmd = PN544_FW_CMD_SECURE_CHUNK_WRITE; in pn544_hci_i2c_fw_secure_write_frame_cmd()
672 put_unaligned_be16(datalen, &chunk->be_datalen); in pn544_hci_i2c_fw_secure_write_frame_cmd()
674 memcpy(chunk->data, data, datalen); in pn544_hci_i2c_fw_secure_write_frame_cmd()
676 chunklen = sizeof(chunk->cmd) + sizeof(chunk->be_datalen) + datalen; in pn544_hci_i2c_fw_secure_write_frame_cmd()
678 r = i2c_master_send(phy->i2c_dev, buf, chunklen); in pn544_hci_i2c_fw_secure_write_frame_cmd()
685 return -EIO; in pn544_hci_i2c_fw_secure_write_frame_cmd()
694 framep = (struct pn544_i2c_fw_secure_frame *) phy->fw_blob_data; in pn544_hci_i2c_fw_secure_write_frame()
695 if (phy->fw_written == 0) in pn544_hci_i2c_fw_secure_write_frame()
696 phy->fw_blob_size = get_unaligned_be16(&framep->be_datalen) in pn544_hci_i2c_fw_secure_write_frame()
700 if (phy->fw_blob_size > PN544_FW_I2C_MAX_PAYLOAD && in pn544_hci_i2c_fw_secure_write_frame()
701 framep->cmd != PN544_FW_CMD_SECURE_WRITE) in pn544_hci_i2c_fw_secure_write_frame()
702 return -EINVAL; in pn544_hci_i2c_fw_secure_write_frame()
705 if (phy->fw_blob_size < PN544_FW_I2C_MAX_PAYLOAD) { in pn544_hci_i2c_fw_secure_write_frame()
706 r = i2c_master_send(phy->i2c_dev, in pn544_hci_i2c_fw_secure_write_frame()
707 (const char *) phy->fw_blob_data, phy->fw_blob_size); in pn544_hci_i2c_fw_secure_write_frame()
709 if (r == phy->fw_blob_size) in pn544_hci_i2c_fw_secure_write_frame()
714 return -EIO; in pn544_hci_i2c_fw_secure_write_frame()
718 phy->fw_blob_data + phy->fw_written, in pn544_hci_i2c_fw_secure_write_frame()
719 phy->fw_blob_size - phy->fw_written); in pn544_hci_i2c_fw_secure_write_frame()
724 phy->fw_written += r; in pn544_hci_i2c_fw_secure_write_frame()
725 phy->fw_work_state = FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER; in pn544_hci_i2c_fw_secure_write_frame()
728 if (framep->cmd == PN544_FW_CMD_RESET) { in pn544_hci_i2c_fw_secure_write_frame()
730 phy->fw_cmd_result = 0; in pn544_hci_i2c_fw_secure_write_frame()
731 schedule_work(&phy->fw_work); in pn544_hci_i2c_fw_secure_write_frame()
745 switch (phy->fw_work_state) { in pn544_hci_i2c_fw_work()
749 r = request_firmware(&phy->fw, phy->firmware_name, in pn544_hci_i2c_fw_work()
750 &phy->i2c_dev->dev); in pn544_hci_i2c_fw_work()
754 phy->fw_written = 0; in pn544_hci_i2c_fw_work()
756 switch (phy->hw_variant) { in pn544_hci_i2c_fw_work()
758 blob = (struct pn544_i2c_fw_blob *) phy->fw->data; in pn544_hci_i2c_fw_work()
759 phy->fw_blob_size = get_unaligned_be32(&blob->be_size); in pn544_hci_i2c_fw_work()
760 phy->fw_blob_dest_addr = get_unaligned_be32( in pn544_hci_i2c_fw_work()
761 &blob->be_destaddr); in pn544_hci_i2c_fw_work()
762 phy->fw_blob_data = blob->data; in pn544_hci_i2c_fw_work()
768 phy->fw->data; in pn544_hci_i2c_fw_work()
769 phy->fw_blob_data = secure_blob->data; in pn544_hci_i2c_fw_work()
770 phy->fw_size = phy->fw->size; in pn544_hci_i2c_fw_work()
774 r = -ENOTSUPP; in pn544_hci_i2c_fw_work()
784 r = phy->fw_cmd_result; in pn544_hci_i2c_fw_work()
788 if (phy->fw_written == phy->fw_blob_size) { in pn544_hci_i2c_fw_work()
789 r = pn544_hci_i2c_fw_check_cmd(phy->i2c_dev, in pn544_hci_i2c_fw_work()
790 phy->fw_blob_dest_addr, in pn544_hci_i2c_fw_work()
791 phy->fw_blob_data, in pn544_hci_i2c_fw_work()
792 phy->fw_blob_size); in pn544_hci_i2c_fw_work()
795 phy->fw_work_state = FW_WORK_STATE_WAIT_CHECK_ANSWER; in pn544_hci_i2c_fw_work()
807 r = phy->fw_cmd_result; in pn544_hci_i2c_fw_work()
811 blob = (struct pn544_i2c_fw_blob *) (phy->fw_blob_data + in pn544_hci_i2c_fw_work()
812 phy->fw_blob_size); in pn544_hci_i2c_fw_work()
813 phy->fw_blob_size = get_unaligned_be32(&blob->be_size); in pn544_hci_i2c_fw_work()
814 if (phy->fw_blob_size != 0) { in pn544_hci_i2c_fw_work()
815 phy->fw_blob_dest_addr = in pn544_hci_i2c_fw_work()
816 get_unaligned_be32(&blob->be_destaddr); in pn544_hci_i2c_fw_work()
817 phy->fw_blob_data = blob->data; in pn544_hci_i2c_fw_work()
819 phy->fw_written = 0; in pn544_hci_i2c_fw_work()
824 if (r < 0 || phy->fw_blob_size == 0) in pn544_hci_i2c_fw_work()
829 r = phy->fw_cmd_result; in pn544_hci_i2c_fw_work()
838 if (phy->fw_written == phy->fw_blob_size) { in pn544_hci_i2c_fw_work()
840 (phy->fw_blob_data + phy->fw_blob_size); in pn544_hci_i2c_fw_work()
841 phy->fw_size -= phy->fw_blob_size + in pn544_hci_i2c_fw_work()
843 if (phy->fw_size >= PN544_FW_SECURE_BLOB_HEADER_LEN in pn544_hci_i2c_fw_work()
845 phy->fw_blob_data = secure_blob->data; in pn544_hci_i2c_fw_work()
847 phy->fw_written = 0; in pn544_hci_i2c_fw_work()
853 if (r < 0 || phy->fw_size == 0) in pn544_hci_i2c_fw_work()
866 { "enable-gpios", &enable_gpios, 1 },
867 { "firmware-gpios", &firmware_gpios, 1 },
874 struct device *dev = &client->dev; in pn544_hci_i2c_probe()
878 dev_dbg(&client->dev, "%s\n", __func__); in pn544_hci_i2c_probe()
879 dev_dbg(&client->dev, "IRQ: %d\n", client->irq); in pn544_hci_i2c_probe()
881 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { in pn544_hci_i2c_probe()
882 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n"); in pn544_hci_i2c_probe()
883 return -ENODEV; in pn544_hci_i2c_probe()
886 phy = devm_kzalloc(&client->dev, sizeof(struct pn544_i2c_phy), in pn544_hci_i2c_probe()
889 return -ENOMEM; in pn544_hci_i2c_probe()
891 INIT_WORK(&phy->fw_work, pn544_hci_i2c_fw_work); in pn544_hci_i2c_probe()
892 phy->fw_work_state = FW_WORK_STATE_IDLE; in pn544_hci_i2c_probe()
894 phy->i2c_dev = client; in pn544_hci_i2c_probe()
902 phy->gpiod_en = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW); in pn544_hci_i2c_probe()
903 if (IS_ERR(phy->gpiod_en)) { in pn544_hci_i2c_probe()
905 return PTR_ERR(phy->gpiod_en); in pn544_hci_i2c_probe()
909 phy->gpiod_fw = devm_gpiod_get(dev, "firmware", GPIOD_OUT_LOW); in pn544_hci_i2c_probe()
910 if (IS_ERR(phy->gpiod_fw)) { in pn544_hci_i2c_probe()
912 return PTR_ERR(phy->gpiod_fw); in pn544_hci_i2c_probe()
917 r = devm_request_threaded_irq(&client->dev, client->irq, NULL, in pn544_hci_i2c_probe()
922 nfc_err(&client->dev, "Unable to register IRQ handler\n"); in pn544_hci_i2c_probe()
929 pn544_hci_i2c_fw_download, &phy->hdev); in pn544_hci_i2c_probe()
940 dev_dbg(&client->dev, "%s\n", __func__); in pn544_hci_i2c_remove()
942 cancel_work_sync(&phy->fw_work); in pn544_hci_i2c_remove()
943 if (phy->fw_work_state != FW_WORK_STATE_IDLE) in pn544_hci_i2c_remove()
944 pn544_hci_i2c_fw_work_complete(phy, -ENODEV); in pn544_hci_i2c_remove()
946 pn544_hci_remove(phy->hdev); in pn544_hci_i2c_remove()
948 if (phy->powered) in pn544_hci_i2c_remove()
955 { .compatible = "nxp,pn544-i2c", },