Lines Matching +full:spi +full:- +full:crc

1 // SPDX-License-Identifier: GPL-2.0-only
3 * --------------------------------------------------------------------
5 * --------------------------------------------------------------------
25 #include "spi.h"
101 * List of top-level cmds to be used internally by the driver.
196 * for spi communication between st95hf and host.
251 struct device *dev = &st95context->spicontext.spidev->dev; in st95hf_send_recv_cmd()
254 return -EINVAL; in st95hf_send_recv_cmd()
256 return -EINVAL; in st95hf_send_recv_cmd()
258 return -EINVAL; in st95hf_send_recv_cmd()
269 return -EINVAL; in st95hf_send_recv_cmd()
274 ret = st95hf_spi_send(&st95context->spicontext, in st95hf_send_recv_cmd()
286 ret = st95hf_spi_recv_response(&st95context->spicontext, in st95hf_send_recv_cmd()
289 dev_err(dev, "spi error from st95hf_spi_recv_response(), err = 0x%x\n", in st95hf_send_recv_cmd()
297 return -EIO; in st95hf_send_recv_cmd()
314 result = st95hf_spi_recv_echo_res(&st95context->spicontext, in st95hf_echo_command()
317 dev_err(&st95context->spicontext.spidev->dev, in st95hf_echo_command()
325 dev_err(&st95context->spicontext.spidev->dev, "err: echo res is 0x%x\n", in st95hf_echo_command()
328 return -EIO; in st95hf_echo_command()
334 struct device *dev = &stcontext->nfcdev->dev; in secondary_configuration_type4a()
362 struct device *dev = &stcontext->nfcdev->dev; in secondary_configuration_type4b()
380 dev = &stcontext->nfcdev->dev; in st95hf_select_protocol()
384 stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_106A; in st95hf_select_protocol()
405 stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_106B; in st95hf_select_protocol()
418 * delay of 5-6 ms is required after select protocol in st95hf_select_protocol()
432 stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_ISO15693; in st95hf_select_protocol()
445 return -EINVAL; in st95hf_select_protocol()
454 gpio_set_value(st95con->enable_gpio, HIGH); in st95hf_send_st95enable_negativepulse()
460 gpio_set_value(st95con->enable_gpio, LOW); in st95hf_send_st95enable_negativepulse()
466 gpio_set_value(st95con->enable_gpio, HIGH); in st95hf_send_st95enable_negativepulse()
470 * Send a reset sequence over SPI bus (Reset command + wait 3ms +
478 result = st95hf_spi_send(&st95context->spicontext, in st95hf_send_spi_reset_sequence()
483 dev_err(&st95context->spicontext.spidev->dev, in st95hf_send_spi_reset_sequence()
484 "spi reset sequence cmd error = %d", result); in st95hf_send_spi_reset_sequence()
512 dev_dbg(&st95context->spicontext.spidev->dev, in st95hf_por_sequence()
529 return -ETIMEDOUT; in st95hf_por_sequence()
535 struct device *dev = &st95context->spicontext.spidev->dev; in iso14443_config_fdt()
536 struct nfc_digital_dev *nfcddev = st95context->ddev; in iso14443_config_fdt()
542 if (nfcddev->curr_protocol == NFC_PROTO_ISO14443 && in iso14443_config_fdt()
543 st95context->fwi < 4) in iso14443_config_fdt()
544 st95context->fwi = 4; in iso14443_config_fdt()
547 if (nfcddev->curr_protocol == NFC_PROTO_ISO14443) in iso14443_config_fdt()
548 new_params[0].new_param_val = st95context->fwi; in iso14443_config_fdt()
549 else if (nfcddev->curr_protocol == NFC_PROTO_ISO14443_B) in iso14443_config_fdt()
555 switch (nfcddev->curr_protocol) { in iso14443_config_fdt()
597 return -EINVAL; in iso14443_config_fdt()
610 struct nfc_digital_dev *nfcddev = stcontext->ddev; in st95hf_handle_wtx()
611 struct device *dev = &stcontext->nfcdev->dev; in st95hf_handle_wtx()
636 if (nfcddev->curr_protocol == NFC_PROTO_ISO14443) in st95hf_handle_wtx()
638 else if (nfcddev->curr_protocol == NFC_PROTO_ISO14443_B) in st95hf_handle_wtx()
655 struct device *dev = &stcontext->nfcdev->dev; in st95hf_error_handling()
658 if (skb_resp->data[0] & ST95HF_ERR_MASK) { in st95hf_error_handling()
659 if (skb_resp->data[0] == ST95HF_TIMEOUT_ERROR) in st95hf_error_handling()
660 result = -ETIMEDOUT; in st95hf_error_handling()
662 result = -EIO; in st95hf_error_handling()
666 /* Check for CRC err only if CRC is present in the tag response */ in st95hf_error_handling()
667 switch (stcontext->current_rf_tech) { in st95hf_error_handling()
669 if (stcontext->sendrcv_trflag == TRFLAG_NFCA_STD_FRAME_CRC) { in st95hf_error_handling()
670 error_byte = skb_resp->data[res_len - 3]; in st95hf_error_handling()
672 /* CRC error occurred */ in st95hf_error_handling()
673 dev_err(dev, "CRC error, byte received = 0x%x\n", in st95hf_error_handling()
675 result = -EIO; in st95hf_error_handling()
681 error_byte = skb_resp->data[res_len - 1]; in st95hf_error_handling()
683 /* CRC error occurred */ in st95hf_error_handling()
684 dev_err(dev, "CRC error, byte received = 0x%x\n", in st95hf_error_handling()
686 result = -EIO; in st95hf_error_handling()
701 struct nfc_digital_dev *nfcddev = stcontext->ddev; in st95hf_response_handler()
702 struct device *dev = &stcontext->nfcdev->dev; in st95hf_response_handler()
705 cb_arg = &stcontext->complete_cb_arg; in st95hf_response_handler()
713 skb_len = skb_resp->len; in st95hf_response_handler()
716 if (nfcddev->curr_protocol == NFC_PROTO_ISO14443 && cb_arg->rats && in st95hf_response_handler()
717 (skb_resp->data[1] & RATS_TB1_PRESENT_MASK)) { in st95hf_response_handler()
718 if (skb_resp->data[1] & RATS_TA1_PRESENT_MASK) in st95hf_response_handler()
719 stcontext->fwi = in st95hf_response_handler()
720 (skb_resp->data[3] & TB1_FWI_MASK) >> 4; in st95hf_response_handler()
722 stcontext->fwi = in st95hf_response_handler()
723 (skb_resp->data[2] & TB1_FWI_MASK) >> 4; in st95hf_response_handler()
734 cb_arg->rats = false; in st95hf_response_handler()
736 /* Remove CRC bytes only if received frames data has an eod (CRC) */ in st95hf_response_handler()
737 switch (stcontext->current_rf_tech) { in st95hf_response_handler()
739 if (stcontext->sendrcv_trflag == TRFLAG_NFCA_STD_FRAME_CRC) in st95hf_response_handler()
740 skb_trim(skb_resp, (skb_len - 5)); in st95hf_response_handler()
742 skb_trim(skb_resp, (skb_len - 3)); in st95hf_response_handler()
746 skb_trim(skb_resp, (skb_len - 3)); in st95hf_response_handler()
758 if (stcontext->spicontext.req_issync) { in st95hf_irq_handler()
759 complete(&stcontext->spicontext.done); in st95hf_irq_handler()
760 stcontext->spicontext.req_issync = false; in st95hf_irq_handler()
778 spidevice = &stcontext->spicontext.spidev->dev; in st95hf_irq_thread_handler()
793 if (!down_trylock(&stcontext->exchange_lock)) { in st95hf_irq_thread_handler()
794 up(&stcontext->exchange_lock); in st95hf_irq_thread_handler()
799 cb_arg = &stcontext->complete_cb_arg; in st95hf_irq_thread_handler()
800 skb_resp = cb_arg->skb_resp; in st95hf_irq_thread_handler()
802 mutex_lock(&stcontext->rm_lock); in st95hf_irq_thread_handler()
803 res_len = st95hf_spi_recv_response(&stcontext->spicontext, in st95hf_irq_thread_handler()
804 skb_resp->data); in st95hf_irq_thread_handler()
806 dev_err(spidevice, "TISR spi response err = 0x%x\n", res_len); in st95hf_irq_thread_handler()
811 /* if stcontext->nfcdev_free is true, it means remove already ran */ in st95hf_irq_thread_handler()
812 if (stcontext->nfcdev_free) { in st95hf_irq_thread_handler()
813 result = -ENODEV; in st95hf_irq_thread_handler()
817 if (skb_resp->data[2] == WTX_REQ_FROM_TAG) { in st95hf_irq_thread_handler()
819 result = st95hf_handle_wtx(stcontext, true, skb_resp->data[3]); in st95hf_irq_thread_handler()
824 mutex_unlock(&stcontext->rm_lock); in st95hf_irq_thread_handler()
848 cb_arg->complete_cb(stcontext->ddev, cb_arg->cb_usrarg, skb_resp); in st95hf_irq_thread_handler()
851 up(&stcontext->exchange_lock); in st95hf_irq_thread_handler()
852 mutex_unlock(&stcontext->rm_lock); in st95hf_irq_thread_handler()
859 cb_arg->rats = false; in st95hf_irq_thread_handler()
862 cb_arg->complete_cb(stcontext->ddev, cb_arg->cb_usrarg, skb_resp); in st95hf_irq_thread_handler()
864 up(&stcontext->exchange_lock); in st95hf_irq_thread_handler()
865 mutex_unlock(&stcontext->rm_lock); in st95hf_irq_thread_handler()
882 stcontext->sendrcv_trflag = TRFLAG_NFCA_SHORT_FRAME; in st95hf_in_configure_hw()
885 stcontext->sendrcv_trflag = TRFLAG_NFCA_STD_FRAME; in st95hf_in_configure_hw()
890 stcontext->sendrcv_trflag = TRFLAG_NFCA_STD_FRAME_CRC; in st95hf_in_configure_hw()
907 dev = &stcontext->nfcdev->dev; in rf_off()
929 return -ENOMEM; in st95hf_in_send_cmd()
931 switch (stcontext->current_rf_tech) { in st95hf_in_send_cmd()
933 len_data_to_tag = skb->len + 1; in st95hf_in_send_cmd()
934 skb_put_u8(skb, stcontext->sendrcv_trflag); in st95hf_in_send_cmd()
938 len_data_to_tag = skb->len; in st95hf_in_send_cmd()
941 rc = -EINVAL; in st95hf_in_send_cmd()
946 skb->data[0] = ST95HF_COMMAND_SEND; in st95hf_in_send_cmd()
947 skb->data[1] = SEND_RECEIVE_CMD; in st95hf_in_send_cmd()
948 skb->data[2] = len_data_to_tag; in st95hf_in_send_cmd()
950 stcontext->complete_cb_arg.skb_resp = skb_resp; in st95hf_in_send_cmd()
951 stcontext->complete_cb_arg.cb_usrarg = arg; in st95hf_in_send_cmd()
952 stcontext->complete_cb_arg.complete_cb = cb; in st95hf_in_send_cmd()
954 if ((skb->data[3] == ISO14443A_RATS_REQ) && in st95hf_in_send_cmd()
955 ddev->curr_protocol == NFC_PROTO_ISO14443) in st95hf_in_send_cmd()
956 stcontext->complete_cb_arg.rats = true; in st95hf_in_send_cmd()
963 rc = down_killable(&stcontext->exchange_lock); in st95hf_in_send_cmd()
969 rc = st95hf_spi_send(&stcontext->spicontext, skb->data, in st95hf_in_send_cmd()
970 skb->len, in st95hf_in_send_cmd()
973 dev_err(&stcontext->nfcdev->dev, in st95hf_in_send_cmd()
976 up(&stcontext->exchange_lock); in st95hf_in_send_cmd()
1024 rf_tech = ddev->curr_rf_tech; in st95hf_switch_rf()
1056 MODULE_DEVICE_TABLE(spi, st95hf_id);
1071 nfc_info(&nfc_spi_dev->dev, "ST95HF driver probe called.\n"); in st95hf_probe()
1073 st95context = devm_kzalloc(&nfc_spi_dev->dev, in st95hf_probe()
1077 return -ENOMEM; in st95hf_probe()
1079 spicontext = &st95context->spicontext; in st95hf_probe()
1081 spicontext->spidev = nfc_spi_dev; in st95hf_probe()
1083 st95context->fwi = in st95hf_probe()
1086 if (device_property_present(&nfc_spi_dev->dev, "st95hfvin")) { in st95hf_probe()
1087 st95context->st95hf_supply = in st95hf_probe()
1088 devm_regulator_get(&nfc_spi_dev->dev, in st95hf_probe()
1090 if (IS_ERR(st95context->st95hf_supply)) { in st95hf_probe()
1091 dev_err(&nfc_spi_dev->dev, "failed to acquire regulator\n"); in st95hf_probe()
1092 return PTR_ERR(st95context->st95hf_supply); in st95hf_probe()
1095 ret = regulator_enable(st95context->st95hf_supply); in st95hf_probe()
1097 dev_err(&nfc_spi_dev->dev, "failed to enable regulator\n"); in st95hf_probe()
1102 init_completion(&spicontext->done); in st95hf_probe()
1103 mutex_init(&spicontext->spi_lock); in st95hf_probe()
1106 * Store spicontext in spi device object for using it in in st95hf_probe()
1109 dev_set_drvdata(&nfc_spi_dev->dev, spicontext); in st95hf_probe()
1111 st95context->enable_gpio = in st95hf_probe()
1112 of_get_named_gpio(nfc_spi_dev->dev.of_node, in st95hf_probe()
1113 "enable-gpio", in st95hf_probe()
1115 if (!gpio_is_valid(st95context->enable_gpio)) { in st95hf_probe()
1116 dev_err(&nfc_spi_dev->dev, "No valid enable gpio\n"); in st95hf_probe()
1117 ret = st95context->enable_gpio; in st95hf_probe()
1121 ret = devm_gpio_request_one(&nfc_spi_dev->dev, st95context->enable_gpio, in st95hf_probe()
1127 if (nfc_spi_dev->irq > 0) { in st95hf_probe()
1128 if (devm_request_threaded_irq(&nfc_spi_dev->dev, in st95hf_probe()
1129 nfc_spi_dev->irq, in st95hf_probe()
1135 dev_err(&nfc_spi_dev->dev, "err: irq request for st95hf is failed\n"); in st95hf_probe()
1136 ret = -EINVAL; in st95hf_probe()
1140 dev_err(&nfc_spi_dev->dev, "not a valid IRQ associated with ST95HF\n"); in st95hf_probe()
1141 ret = -EINVAL; in st95hf_probe()
1146 * First reset SPI to handle warm reset of the system. in st95hf_probe()
1153 dev_err(&nfc_spi_dev->dev, "err: spi_reset_sequence failed\n"); in st95hf_probe()
1160 dev_err(&nfc_spi_dev->dev, "err: por seq failed for st95hf\n"); in st95hf_probe()
1165 st95context->ddev = nfc_digital_allocate_device(&st95hf_nfc_digital_ops, in st95hf_probe()
1170 if (!st95context->ddev) { in st95hf_probe()
1171 ret = -ENOMEM; in st95hf_probe()
1175 st95context->nfcdev = st95context->ddev->nfc_dev; in st95hf_probe()
1176 nfc_digital_set_parent_dev(st95context->ddev, &nfc_spi_dev->dev); in st95hf_probe()
1178 ret = nfc_digital_register_device(st95context->ddev); in st95hf_probe()
1180 dev_err(&st95context->nfcdev->dev, "st95hf registration failed\n"); in st95hf_probe()
1185 nfc_digital_set_drvdata(st95context->ddev, st95context); in st95hf_probe()
1187 sema_init(&st95context->exchange_lock, 1); in st95hf_probe()
1188 mutex_init(&st95context->rm_lock); in st95hf_probe()
1193 nfc_digital_free_device(st95context->ddev); in st95hf_probe()
1195 if (st95context->st95hf_supply) in st95hf_probe()
1196 regulator_disable(st95context->st95hf_supply); in st95hf_probe()
1205 struct st95hf_spi_context *spictx = dev_get_drvdata(&nfc_spi_dev->dev); in st95hf_remove()
1211 mutex_lock(&stcontext->rm_lock); in st95hf_remove()
1213 nfc_digital_unregister_device(stcontext->ddev); in st95hf_remove()
1214 nfc_digital_free_device(stcontext->ddev); in st95hf_remove()
1215 stcontext->nfcdev_free = true; in st95hf_remove()
1217 mutex_unlock(&stcontext->rm_lock); in st95hf_remove()
1220 result = down_killable(&stcontext->exchange_lock); in st95hf_remove()
1221 if (result == -EINTR) in st95hf_remove()
1222 dev_err(&spictx->spidev->dev, "sleep for semaphore interrupted by signal\n"); in st95hf_remove()
1225 result = st95hf_spi_send(&stcontext->spicontext, in st95hf_remove()
1230 dev_err(&spictx->spidev->dev, in st95hf_remove()
1237 if (stcontext->st95hf_supply) in st95hf_remove()
1238 regulator_disable(stcontext->st95hf_supply); in st95hf_remove()
1241 /* Register as SPI protocol driver */