Lines Matching +full:trig +full:- +full:gpios
1 // SPDX-License-Identifier: GPL-2.0-only
10 #include <linux/crc-ccitt.h>
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 -\
185 16, 1, (skb)->data, (skb)->len, 0); \
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()
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()
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()
246 phy->powered = 1; in pn544_hci_i2c_enable()
255 gpiod_set_value_cansleep(phy->gpiod_fw, 0); in pn544_hci_i2c_disable()
256 gpiod_set_value_cansleep(phy->gpiod_en, !phy->en_polarity); in pn544_hci_i2c_disable()
259 gpiod_set_value_cansleep(phy->gpiod_en, phy->en_polarity); in pn544_hci_i2c_disable()
262 gpiod_set_value_cansleep(phy->gpiod_en, !phy->en_polarity); in pn544_hci_i2c_disable()
265 phy->powered = 0; in pn544_hci_i2c_disable()
273 len = skb->len + 2; in pn544_hci_i2c_add_len_crc()
276 crc = crc_ccitt(0xffff, skb->data, skb->len); in pn544_hci_i2c_add_len_crc()
297 struct i2c_client *client = phy->i2c_dev; in pn544_hci_i2c_write()
299 if (phy->hard_fault != 0) in pn544_hci_i2c_write()
300 return phy->hard_fault; in pn544_hci_i2c_write()
308 r = i2c_master_send(client, skb->data, skb->len); in pn544_hci_i2c_write()
310 if (r == -EREMOTEIO) { /* Retry, chip was in standby */ in pn544_hci_i2c_write()
312 r = i2c_master_send(client, skb->data, skb->len); in pn544_hci_i2c_write()
316 if (r != skb->len) in pn544_hci_i2c_write()
317 r = -EREMOTEIO; in pn544_hci_i2c_write()
333 crc = crc_ccitt(0xffff, buf, len - 2); in check_crc()
336 if (buf[len - 2] != (crc & 0xff) || buf[len - 1] != (crc >> 8)) { in check_crc()
338 crc, buf[len - 1], buf[len - 2]); in check_crc()
342 return -EPERM; in check_crc()
352 * -EREMOTEIO : i2c read error (fatal)
353 * -EBADMSG : frame was incorrect and discarded
354 * -ENOMEM : cannot allocate skb, frame dropped
360 u8 tmp[PN544_HCI_I2C_LLC_MAX_SIZE - 1]; in pn544_hci_i2c_read()
361 struct i2c_client *client = phy->i2c_dev; in pn544_hci_i2c_read()
365 nfc_err(&client->dev, "cannot read len byte\n"); in pn544_hci_i2c_read()
366 return -EREMOTEIO; in pn544_hci_i2c_read()
369 if ((len < (PN544_HCI_I2C_LLC_MIN_SIZE - 1)) || in pn544_hci_i2c_read()
370 (len > (PN544_HCI_I2C_LLC_MAX_SIZE - 1))) { in pn544_hci_i2c_read()
371 nfc_err(&client->dev, "invalid len byte\n"); in pn544_hci_i2c_read()
372 r = -EBADMSG; in pn544_hci_i2c_read()
378 r = -ENOMEM; in pn544_hci_i2c_read()
387 return -EREMOTEIO; in pn544_hci_i2c_read()
392 r = check_crc((*skb)->data, (*skb)->len); in pn544_hci_i2c_read()
395 r = -EBADMSG; in pn544_hci_i2c_read()
400 skb_trim(*skb, (*skb)->len - 2); in pn544_hci_i2c_read()
408 r = -EREMOTEIO; in pn544_hci_i2c_read()
419 struct i2c_client *client = phy->i2c_dev; in pn544_hci_i2c_fw_read_status()
423 nfc_err(&client->dev, "cannot read fw status\n"); in pn544_hci_i2c_fw_read_status()
424 return -EIO; in pn544_hci_i2c_fw_read_status()
435 return -ETIMEDOUT; in pn544_hci_i2c_fw_read_status()
437 return -ENODATA; in pn544_hci_i2c_fw_read_status()
439 return -EACCES; in pn544_hci_i2c_fw_read_status()
441 return -EPROTO; in pn544_hci_i2c_fw_read_status()
443 return -EINVAL; in pn544_hci_i2c_fw_read_status()
445 return -ENOTSUPP; in pn544_hci_i2c_fw_read_status()
447 return -EBADMSG; in pn544_hci_i2c_fw_read_status()
449 return -ENOKEY; in pn544_hci_i2c_fw_read_status()
451 return -EINVAL; in pn544_hci_i2c_fw_read_status()
453 return -ENOMEM; in pn544_hci_i2c_fw_read_status()
455 return -EACCES; in pn544_hci_i2c_fw_read_status()
458 return -EIO; in pn544_hci_i2c_fw_read_status()
460 return -EIO; in pn544_hci_i2c_fw_read_status()
467 * The frame format is len-data-crc, and corruption can occur anywhere while
472 * - the chip will always present only one single complete frame on the bus
474 * - the chip will not present a new frame until we have completely read
487 if (!phy || irq != phy->i2c_dev->irq) { in pn544_hci_i2c_irq_thread_fn()
492 client = phy->i2c_dev; in pn544_hci_i2c_irq_thread_fn()
493 dev_dbg(&client->dev, "IRQ\n"); in pn544_hci_i2c_irq_thread_fn()
495 if (phy->hard_fault != 0) in pn544_hci_i2c_irq_thread_fn()
498 if (phy->run_mode == PN544_FW_MODE) { in pn544_hci_i2c_irq_thread_fn()
499 phy->fw_cmd_result = pn544_hci_i2c_fw_read_status(phy); in pn544_hci_i2c_irq_thread_fn()
500 schedule_work(&phy->fw_work); in pn544_hci_i2c_irq_thread_fn()
503 if (r == -EREMOTEIO) { in pn544_hci_i2c_irq_thread_fn()
504 phy->hard_fault = r; in pn544_hci_i2c_irq_thread_fn()
506 nfc_hci_recv_frame(phy->hdev, NULL); in pn544_hci_i2c_irq_thread_fn()
509 } else if ((r == -ENOMEM) || (r == -EBADMSG)) { in pn544_hci_i2c_irq_thread_fn()
513 nfc_hci_recv_frame(phy->hdev, skb); in pn544_hci_i2c_irq_thread_fn()
531 strcpy(phy->firmware_name, firmware_name); in pn544_hci_i2c_fw_download()
533 phy->hw_variant = hw_variant; in pn544_hci_i2c_fw_download()
534 phy->fw_work_state = FW_WORK_STATE_START; in pn544_hci_i2c_fw_download()
536 schedule_work(&phy->fw_work); in pn544_hci_i2c_fw_download()
548 phy->fw_work_state = FW_WORK_STATE_IDLE; in pn544_hci_i2c_fw_work_complete()
550 if (phy->fw) { in pn544_hci_i2c_fw_work_complete()
551 release_firmware(phy->fw); in pn544_hci_i2c_fw_work_complete()
552 phy->fw = NULL; in pn544_hci_i2c_fw_work_complete()
555 nfc_fw_download_done(phy->hdev->ndev, phy->firmware_name, (u32) -result); in pn544_hci_i2c_fw_work_complete()
572 params_len = sizeof(framep->be_dest_addr) + in pn544_hci_i2c_fw_write_cmd()
573 sizeof(framep->be_datalen) + datalen; in pn544_hci_i2c_fw_write_cmd()
574 framelen = params_len + sizeof(framep->cmd) + in pn544_hci_i2c_fw_write_cmd()
575 sizeof(framep->be_length); in pn544_hci_i2c_fw_write_cmd()
577 framep->cmd = PN544_FW_CMD_WRITE; in pn544_hci_i2c_fw_write_cmd()
579 put_unaligned_be16(params_len, &framep->be_length); in pn544_hci_i2c_fw_write_cmd()
581 framep->be_dest_addr[0] = (dest_addr & 0xff0000) >> 16; in pn544_hci_i2c_fw_write_cmd()
582 framep->be_dest_addr[1] = (dest_addr & 0xff00) >> 8; in pn544_hci_i2c_fw_write_cmd()
583 framep->be_dest_addr[2] = dest_addr & 0xff; in pn544_hci_i2c_fw_write_cmd()
585 put_unaligned_be16(datalen, &framep->be_datalen); in pn544_hci_i2c_fw_write_cmd()
587 memcpy(framep->data, data, datalen); in pn544_hci_i2c_fw_write_cmd()
596 return -EIO; in pn544_hci_i2c_fw_write_cmd()
635 return -EIO; in pn544_hci_i2c_fw_check_cmd()
642 r = pn544_hci_i2c_fw_write_cmd(phy->i2c_dev, in pn544_hci_i2c_fw_write_chunk()
643 phy->fw_blob_dest_addr + phy->fw_written, in pn544_hci_i2c_fw_write_chunk()
644 phy->fw_blob_data + phy->fw_written, in pn544_hci_i2c_fw_write_chunk()
645 phy->fw_blob_size - phy->fw_written); in pn544_hci_i2c_fw_write_chunk()
649 phy->fw_written += r; in pn544_hci_i2c_fw_write_chunk()
650 phy->fw_work_state = FW_WORK_STATE_WAIT_WRITE_ANSWER; in pn544_hci_i2c_fw_write_chunk()
668 chunk->cmd = PN544_FW_CMD_SECURE_CHUNK_WRITE; in pn544_hci_i2c_fw_secure_write_frame_cmd()
670 put_unaligned_be16(datalen, &chunk->be_datalen); in pn544_hci_i2c_fw_secure_write_frame_cmd()
672 memcpy(chunk->data, data, datalen); in pn544_hci_i2c_fw_secure_write_frame_cmd()
674 chunklen = sizeof(chunk->cmd) + sizeof(chunk->be_datalen) + datalen; in pn544_hci_i2c_fw_secure_write_frame_cmd()
676 r = i2c_master_send(phy->i2c_dev, buf, chunklen); in pn544_hci_i2c_fw_secure_write_frame_cmd()
683 return -EIO; in pn544_hci_i2c_fw_secure_write_frame_cmd()
692 framep = (struct pn544_i2c_fw_secure_frame *) phy->fw_blob_data; in pn544_hci_i2c_fw_secure_write_frame()
693 if (phy->fw_written == 0) in pn544_hci_i2c_fw_secure_write_frame()
694 phy->fw_blob_size = get_unaligned_be16(&framep->be_datalen) in pn544_hci_i2c_fw_secure_write_frame()
698 if (phy->fw_blob_size > PN544_FW_I2C_MAX_PAYLOAD && in pn544_hci_i2c_fw_secure_write_frame()
699 framep->cmd != PN544_FW_CMD_SECURE_WRITE) in pn544_hci_i2c_fw_secure_write_frame()
700 return -EINVAL; in pn544_hci_i2c_fw_secure_write_frame()
703 if (phy->fw_blob_size < PN544_FW_I2C_MAX_PAYLOAD) { in pn544_hci_i2c_fw_secure_write_frame()
704 r = i2c_master_send(phy->i2c_dev, in pn544_hci_i2c_fw_secure_write_frame()
705 (const char *) phy->fw_blob_data, phy->fw_blob_size); in pn544_hci_i2c_fw_secure_write_frame()
707 if (r == phy->fw_blob_size) in pn544_hci_i2c_fw_secure_write_frame()
712 return -EIO; in pn544_hci_i2c_fw_secure_write_frame()
716 phy->fw_blob_data + phy->fw_written, in pn544_hci_i2c_fw_secure_write_frame()
717 phy->fw_blob_size - phy->fw_written); in pn544_hci_i2c_fw_secure_write_frame()
722 phy->fw_written += r; in pn544_hci_i2c_fw_secure_write_frame()
723 phy->fw_work_state = FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER; in pn544_hci_i2c_fw_secure_write_frame()
725 /* SW reset command will not trig any response from PN544 */ in pn544_hci_i2c_fw_secure_write_frame()
726 if (framep->cmd == PN544_FW_CMD_RESET) { in pn544_hci_i2c_fw_secure_write_frame()
728 phy->fw_cmd_result = 0; in pn544_hci_i2c_fw_secure_write_frame()
729 schedule_work(&phy->fw_work); in pn544_hci_i2c_fw_secure_write_frame()
743 switch (phy->fw_work_state) { in pn544_hci_i2c_fw_work()
747 r = request_firmware(&phy->fw, phy->firmware_name, in pn544_hci_i2c_fw_work()
748 &phy->i2c_dev->dev); in pn544_hci_i2c_fw_work()
752 phy->fw_written = 0; in pn544_hci_i2c_fw_work()
754 switch (phy->hw_variant) { in pn544_hci_i2c_fw_work()
756 blob = (struct pn544_i2c_fw_blob *) phy->fw->data; in pn544_hci_i2c_fw_work()
757 phy->fw_blob_size = get_unaligned_be32(&blob->be_size); in pn544_hci_i2c_fw_work()
758 phy->fw_blob_dest_addr = get_unaligned_be32( in pn544_hci_i2c_fw_work()
759 &blob->be_destaddr); in pn544_hci_i2c_fw_work()
760 phy->fw_blob_data = blob->data; in pn544_hci_i2c_fw_work()
766 phy->fw->data; in pn544_hci_i2c_fw_work()
767 phy->fw_blob_data = secure_blob->data; in pn544_hci_i2c_fw_work()
768 phy->fw_size = phy->fw->size; in pn544_hci_i2c_fw_work()
772 r = -ENOTSUPP; in pn544_hci_i2c_fw_work()
782 r = phy->fw_cmd_result; in pn544_hci_i2c_fw_work()
786 if (phy->fw_written == phy->fw_blob_size) { in pn544_hci_i2c_fw_work()
787 r = pn544_hci_i2c_fw_check_cmd(phy->i2c_dev, in pn544_hci_i2c_fw_work()
788 phy->fw_blob_dest_addr, in pn544_hci_i2c_fw_work()
789 phy->fw_blob_data, in pn544_hci_i2c_fw_work()
790 phy->fw_blob_size); in pn544_hci_i2c_fw_work()
793 phy->fw_work_state = FW_WORK_STATE_WAIT_CHECK_ANSWER; in pn544_hci_i2c_fw_work()
805 r = phy->fw_cmd_result; in pn544_hci_i2c_fw_work()
809 blob = (struct pn544_i2c_fw_blob *) (phy->fw_blob_data + in pn544_hci_i2c_fw_work()
810 phy->fw_blob_size); in pn544_hci_i2c_fw_work()
811 phy->fw_blob_size = get_unaligned_be32(&blob->be_size); in pn544_hci_i2c_fw_work()
812 if (phy->fw_blob_size != 0) { in pn544_hci_i2c_fw_work()
813 phy->fw_blob_dest_addr = in pn544_hci_i2c_fw_work()
814 get_unaligned_be32(&blob->be_destaddr); in pn544_hci_i2c_fw_work()
815 phy->fw_blob_data = blob->data; in pn544_hci_i2c_fw_work()
817 phy->fw_written = 0; in pn544_hci_i2c_fw_work()
822 if (r < 0 || phy->fw_blob_size == 0) in pn544_hci_i2c_fw_work()
827 r = phy->fw_cmd_result; in pn544_hci_i2c_fw_work()
836 if (phy->fw_written == phy->fw_blob_size) { in pn544_hci_i2c_fw_work()
838 (phy->fw_blob_data + phy->fw_blob_size); in pn544_hci_i2c_fw_work()
839 phy->fw_size -= phy->fw_blob_size + in pn544_hci_i2c_fw_work()
841 if (phy->fw_size >= PN544_FW_SECURE_BLOB_HEADER_LEN in pn544_hci_i2c_fw_work()
843 phy->fw_blob_data = secure_blob->data; in pn544_hci_i2c_fw_work()
845 phy->fw_written = 0; in pn544_hci_i2c_fw_work()
851 if (r < 0 || phy->fw_size == 0) in pn544_hci_i2c_fw_work()
864 { "enable-gpios", &enable_gpios, 1 },
865 { "firmware-gpios", &firmware_gpios, 1 },
871 struct device *dev = &client->dev; in pn544_hci_i2c_probe()
875 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { in pn544_hci_i2c_probe()
876 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n"); in pn544_hci_i2c_probe()
877 return -ENODEV; in pn544_hci_i2c_probe()
880 phy = devm_kzalloc(&client->dev, sizeof(struct pn544_i2c_phy), in pn544_hci_i2c_probe()
883 return -ENOMEM; in pn544_hci_i2c_probe()
885 INIT_WORK(&phy->fw_work, pn544_hci_i2c_fw_work); in pn544_hci_i2c_probe()
886 phy->fw_work_state = FW_WORK_STATE_IDLE; in pn544_hci_i2c_probe()
888 phy->i2c_dev = client; in pn544_hci_i2c_probe()
896 phy->gpiod_en = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW); in pn544_hci_i2c_probe()
897 if (IS_ERR(phy->gpiod_en)) { in pn544_hci_i2c_probe()
899 return PTR_ERR(phy->gpiod_en); in pn544_hci_i2c_probe()
903 phy->gpiod_fw = devm_gpiod_get(dev, "firmware", GPIOD_OUT_LOW); in pn544_hci_i2c_probe()
904 if (IS_ERR(phy->gpiod_fw)) { in pn544_hci_i2c_probe()
906 return PTR_ERR(phy->gpiod_fw); in pn544_hci_i2c_probe()
911 r = devm_request_threaded_irq(&client->dev, client->irq, NULL, in pn544_hci_i2c_probe()
916 nfc_err(&client->dev, "Unable to register IRQ handler\n"); in pn544_hci_i2c_probe()
923 pn544_hci_i2c_fw_download, &phy->hdev); in pn544_hci_i2c_probe()
934 cancel_work_sync(&phy->fw_work); in pn544_hci_i2c_remove()
935 if (phy->fw_work_state != FW_WORK_STATE_IDLE) in pn544_hci_i2c_remove()
936 pn544_hci_i2c_fw_work_complete(phy, -ENODEV); in pn544_hci_i2c_remove()
938 pn544_hci_remove(phy->hdev); in pn544_hci_i2c_remove()
940 if (phy->powered) in pn544_hci_i2c_remove()
945 { .compatible = "nxp,pn544-i2c", },