Lines Matching +full:nfc +full:- +full:spi
1 // SPDX-License-Identifier: GPL-2.0-only
3 * --------------------------------------------------------------------
4 * Driver for ST NFC Transceiver ST95HF
5 * --------------------------------------------------------------------
16 #include <linux/nfc.h>
21 #include <net/nfc/digital.h>
22 #include <net/nfc/nfc.h>
24 #include "spi.h"
100 * List of top-level cmds to be used internally by the driver.
195 * for spi communication between st95hf and host.
196 * @ddev: nfc digital device object.
197 * @nfcdev: nfc device object.
200 * that is passed from nfc requesting thread to the threaded ISR.
201 * @st95hf_supply: regulator "consumer" for NFC device.
205 * function that the last outstanding async nfc request is finished.
206 * @rm_lock: mutex for ensuring safe access of nfc digital object
210 * @nfcdev_free: flag to have the state of nfc device object.
212 * @current_protocol: current nfc protocol.
250 struct device *dev = &st95context->spicontext.spidev->dev; in st95hf_send_recv_cmd()
253 return -EINVAL; in st95hf_send_recv_cmd()
255 return -EINVAL; in st95hf_send_recv_cmd()
257 return -EINVAL; in st95hf_send_recv_cmd()
268 return -EINVAL; in st95hf_send_recv_cmd()
273 ret = st95hf_spi_send(&st95context->spicontext, in st95hf_send_recv_cmd()
285 ret = st95hf_spi_recv_response(&st95context->spicontext, in st95hf_send_recv_cmd()
288 dev_err(dev, "spi error from st95hf_spi_recv_response(), err = 0x%x\n", in st95hf_send_recv_cmd()
296 return -EIO; in st95hf_send_recv_cmd()
313 result = st95hf_spi_recv_echo_res(&st95context->spicontext, in st95hf_echo_command()
316 dev_err(&st95context->spicontext.spidev->dev, in st95hf_echo_command()
324 dev_err(&st95context->spicontext.spidev->dev, "err: echo res is 0x%x\n", in st95hf_echo_command()
327 return -EIO; in st95hf_echo_command()
333 struct device *dev = &stcontext->nfcdev->dev; in secondary_configuration_type4a()
361 struct device *dev = &stcontext->nfcdev->dev; in secondary_configuration_type4b()
379 dev = &stcontext->nfcdev->dev; in st95hf_select_protocol()
383 stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_106A; in st95hf_select_protocol()
404 stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_106B; in st95hf_select_protocol()
417 * delay of 5-6 ms is required after select protocol in st95hf_select_protocol()
431 stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_ISO15693; in st95hf_select_protocol()
444 return -EINVAL; in st95hf_select_protocol()
453 gpiod_set_value(st95con->enable_gpiod, HIGH); in st95hf_send_st95enable_negativepulse()
459 gpiod_set_value(st95con->enable_gpiod, LOW); in st95hf_send_st95enable_negativepulse()
465 gpiod_set_value(st95con->enable_gpiod, HIGH); in st95hf_send_st95enable_negativepulse()
469 * Send a reset sequence over SPI bus (Reset command + wait 3ms +
477 result = st95hf_spi_send(&st95context->spicontext, in st95hf_send_spi_reset_sequence()
482 dev_err(&st95context->spicontext.spidev->dev, in st95hf_send_spi_reset_sequence()
483 "spi reset sequence cmd error = %d", result); in st95hf_send_spi_reset_sequence()
511 dev_dbg(&st95context->spicontext.spidev->dev, in st95hf_por_sequence()
528 return -ETIMEDOUT; in st95hf_por_sequence()
534 struct device *dev = &st95context->spicontext.spidev->dev; in iso14443_config_fdt()
535 struct nfc_digital_dev *nfcddev = st95context->ddev; in iso14443_config_fdt()
541 if (nfcddev->curr_protocol == NFC_PROTO_ISO14443 && in iso14443_config_fdt()
542 st95context->fwi < 4) in iso14443_config_fdt()
543 st95context->fwi = 4; in iso14443_config_fdt()
546 if (nfcddev->curr_protocol == NFC_PROTO_ISO14443) in iso14443_config_fdt()
547 new_params[0].new_param_val = st95context->fwi; in iso14443_config_fdt()
548 else if (nfcddev->curr_protocol == NFC_PROTO_ISO14443_B) in iso14443_config_fdt()
554 switch (nfcddev->curr_protocol) { in iso14443_config_fdt()
596 return -EINVAL; in iso14443_config_fdt()
609 struct nfc_digital_dev *nfcddev = stcontext->ddev; in st95hf_handle_wtx()
610 struct device *dev = &stcontext->nfcdev->dev; in st95hf_handle_wtx()
635 if (nfcddev->curr_protocol == NFC_PROTO_ISO14443) in st95hf_handle_wtx()
637 else if (nfcddev->curr_protocol == NFC_PROTO_ISO14443_B) in st95hf_handle_wtx()
654 struct device *dev = &stcontext->nfcdev->dev; in st95hf_error_handling()
657 if (skb_resp->data[0] & ST95HF_ERR_MASK) { in st95hf_error_handling()
658 if (skb_resp->data[0] == ST95HF_TIMEOUT_ERROR) in st95hf_error_handling()
659 result = -ETIMEDOUT; in st95hf_error_handling()
661 result = -EIO; in st95hf_error_handling()
666 switch (stcontext->current_rf_tech) { in st95hf_error_handling()
668 if (stcontext->sendrcv_trflag == TRFLAG_NFCA_STD_FRAME_CRC) { in st95hf_error_handling()
669 error_byte = skb_resp->data[res_len - 3]; in st95hf_error_handling()
674 result = -EIO; in st95hf_error_handling()
680 error_byte = skb_resp->data[res_len - 1]; in st95hf_error_handling()
685 result = -EIO; in st95hf_error_handling()
700 struct nfc_digital_dev *nfcddev = stcontext->ddev; in st95hf_response_handler()
701 struct device *dev = &stcontext->nfcdev->dev; in st95hf_response_handler()
704 cb_arg = &stcontext->complete_cb_arg; in st95hf_response_handler()
712 skb_len = skb_resp->len; in st95hf_response_handler()
715 if (nfcddev->curr_protocol == NFC_PROTO_ISO14443 && cb_arg->rats && in st95hf_response_handler()
716 (skb_resp->data[1] & RATS_TB1_PRESENT_MASK)) { in st95hf_response_handler()
717 if (skb_resp->data[1] & RATS_TA1_PRESENT_MASK) in st95hf_response_handler()
718 stcontext->fwi = in st95hf_response_handler()
719 (skb_resp->data[3] & TB1_FWI_MASK) >> 4; in st95hf_response_handler()
721 stcontext->fwi = in st95hf_response_handler()
722 (skb_resp->data[2] & TB1_FWI_MASK) >> 4; in st95hf_response_handler()
733 cb_arg->rats = false; in st95hf_response_handler()
736 switch (stcontext->current_rf_tech) { in st95hf_response_handler()
738 if (stcontext->sendrcv_trflag == TRFLAG_NFCA_STD_FRAME_CRC) in st95hf_response_handler()
739 skb_trim(skb_resp, (skb_len - 5)); in st95hf_response_handler()
741 skb_trim(skb_resp, (skb_len - 3)); in st95hf_response_handler()
745 skb_trim(skb_resp, (skb_len - 3)); in st95hf_response_handler()
757 if (stcontext->spicontext.req_issync) { in st95hf_irq_handler()
758 complete(&stcontext->spicontext.done); in st95hf_irq_handler()
759 stcontext->spicontext.req_issync = false; in st95hf_irq_handler()
777 spidevice = &stcontext->spicontext.spidev->dev; in st95hf_irq_thread_handler()
792 if (!down_trylock(&stcontext->exchange_lock)) { in st95hf_irq_thread_handler()
793 up(&stcontext->exchange_lock); in st95hf_irq_thread_handler()
798 cb_arg = &stcontext->complete_cb_arg; in st95hf_irq_thread_handler()
799 skb_resp = cb_arg->skb_resp; in st95hf_irq_thread_handler()
801 mutex_lock(&stcontext->rm_lock); in st95hf_irq_thread_handler()
802 res_len = st95hf_spi_recv_response(&stcontext->spicontext, in st95hf_irq_thread_handler()
803 skb_resp->data); in st95hf_irq_thread_handler()
805 dev_err(spidevice, "TISR spi response err = 0x%x\n", res_len); in st95hf_irq_thread_handler()
810 /* if stcontext->nfcdev_free is true, it means remove already ran */ in st95hf_irq_thread_handler()
811 if (stcontext->nfcdev_free) { in st95hf_irq_thread_handler()
812 result = -ENODEV; in st95hf_irq_thread_handler()
816 if (skb_resp->data[2] == WTX_REQ_FROM_TAG) { in st95hf_irq_thread_handler()
818 result = st95hf_handle_wtx(stcontext, true, skb_resp->data[3]); in st95hf_irq_thread_handler()
823 mutex_unlock(&stcontext->rm_lock); in st95hf_irq_thread_handler()
847 cb_arg->complete_cb(stcontext->ddev, cb_arg->cb_usrarg, skb_resp); in st95hf_irq_thread_handler()
850 up(&stcontext->exchange_lock); in st95hf_irq_thread_handler()
851 mutex_unlock(&stcontext->rm_lock); in st95hf_irq_thread_handler()
858 cb_arg->rats = false; in st95hf_irq_thread_handler()
861 cb_arg->complete_cb(stcontext->ddev, cb_arg->cb_usrarg, skb_resp); in st95hf_irq_thread_handler()
863 up(&stcontext->exchange_lock); in st95hf_irq_thread_handler()
864 mutex_unlock(&stcontext->rm_lock); in st95hf_irq_thread_handler()
868 /* NFC ops functions definition */
881 stcontext->sendrcv_trflag = TRFLAG_NFCA_SHORT_FRAME; in st95hf_in_configure_hw()
884 stcontext->sendrcv_trflag = TRFLAG_NFCA_STD_FRAME; in st95hf_in_configure_hw()
889 stcontext->sendrcv_trflag = TRFLAG_NFCA_STD_FRAME_CRC; in st95hf_in_configure_hw()
906 dev = &stcontext->nfcdev->dev; in rf_off()
928 return -ENOMEM; in st95hf_in_send_cmd()
930 switch (stcontext->current_rf_tech) { in st95hf_in_send_cmd()
932 len_data_to_tag = skb->len + 1; in st95hf_in_send_cmd()
933 skb_put_u8(skb, stcontext->sendrcv_trflag); in st95hf_in_send_cmd()
937 len_data_to_tag = skb->len; in st95hf_in_send_cmd()
940 rc = -EINVAL; in st95hf_in_send_cmd()
945 skb->data[0] = ST95HF_COMMAND_SEND; in st95hf_in_send_cmd()
946 skb->data[1] = SEND_RECEIVE_CMD; in st95hf_in_send_cmd()
947 skb->data[2] = len_data_to_tag; in st95hf_in_send_cmd()
949 stcontext->complete_cb_arg.skb_resp = skb_resp; in st95hf_in_send_cmd()
950 stcontext->complete_cb_arg.cb_usrarg = arg; in st95hf_in_send_cmd()
951 stcontext->complete_cb_arg.complete_cb = cb; in st95hf_in_send_cmd()
953 if ((skb->data[3] == ISO14443A_RATS_REQ) && in st95hf_in_send_cmd()
954 ddev->curr_protocol == NFC_PROTO_ISO14443) in st95hf_in_send_cmd()
955 stcontext->complete_cb_arg.rats = true; in st95hf_in_send_cmd()
962 rc = down_killable(&stcontext->exchange_lock); in st95hf_in_send_cmd()
968 rc = st95hf_spi_send(&stcontext->spicontext, skb->data, in st95hf_in_send_cmd()
969 skb->len, in st95hf_in_send_cmd()
972 dev_err(&stcontext->nfcdev->dev, in st95hf_in_send_cmd()
975 up(&stcontext->exchange_lock); in st95hf_in_send_cmd()
1023 rf_tech = ddev->curr_rf_tech; in st95hf_switch_rf()
1055 MODULE_DEVICE_TABLE(spi, st95hf_id);
1065 struct device *dev = &nfc_spi_dev->dev; in st95hf_probe()
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_gpiod = devm_gpiod_get(dev, "enable", GPIOD_OUT_HIGH); in st95hf_probe()
1112 if (IS_ERR(st95context->enable_gpiod)) { in st95hf_probe()
1113 ret = PTR_ERR(st95context->enable_gpiod); in st95hf_probe()
1114 dev_err(&nfc_spi_dev->dev, "No valid enable gpio\n"); in st95hf_probe()
1118 ret = gpiod_set_consumer_name(st95context->enable_gpiod, "enable_gpio"); in st95hf_probe()
1122 if (nfc_spi_dev->irq > 0) { in st95hf_probe()
1123 if (devm_request_threaded_irq(&nfc_spi_dev->dev, in st95hf_probe()
1124 nfc_spi_dev->irq, in st95hf_probe()
1130 dev_err(&nfc_spi_dev->dev, "err: irq request for st95hf is failed\n"); in st95hf_probe()
1131 ret = -EINVAL; in st95hf_probe()
1135 dev_err(&nfc_spi_dev->dev, "not a valid IRQ associated with ST95HF\n"); in st95hf_probe()
1136 ret = -EINVAL; in st95hf_probe()
1141 * First reset SPI to handle warm reset of the system. in st95hf_probe()
1148 dev_err(&nfc_spi_dev->dev, "err: spi_reset_sequence failed\n"); in st95hf_probe()
1155 dev_err(&nfc_spi_dev->dev, "err: por seq failed for st95hf\n"); in st95hf_probe()
1159 /* create NFC dev object and register with NFC Subsystem */ in st95hf_probe()
1160 st95context->ddev = nfc_digital_allocate_device(&st95hf_nfc_digital_ops, in st95hf_probe()
1165 if (!st95context->ddev) { in st95hf_probe()
1166 ret = -ENOMEM; in st95hf_probe()
1170 st95context->nfcdev = st95context->ddev->nfc_dev; in st95hf_probe()
1171 nfc_digital_set_parent_dev(st95context->ddev, &nfc_spi_dev->dev); in st95hf_probe()
1173 ret = nfc_digital_register_device(st95context->ddev); in st95hf_probe()
1175 dev_err(&st95context->nfcdev->dev, "st95hf registration failed\n"); in st95hf_probe()
1179 /* store st95context in nfc device object */ in st95hf_probe()
1180 nfc_digital_set_drvdata(st95context->ddev, st95context); in st95hf_probe()
1182 sema_init(&st95context->exchange_lock, 1); in st95hf_probe()
1183 mutex_init(&st95context->rm_lock); in st95hf_probe()
1188 nfc_digital_free_device(st95context->ddev); in st95hf_probe()
1190 if (st95context->st95hf_supply) in st95hf_probe()
1191 regulator_disable(st95context->st95hf_supply); in st95hf_probe()
1200 struct st95hf_spi_context *spictx = dev_get_drvdata(&nfc_spi_dev->dev); in st95hf_remove()
1206 mutex_lock(&stcontext->rm_lock); in st95hf_remove()
1208 nfc_digital_unregister_device(stcontext->ddev); in st95hf_remove()
1209 nfc_digital_free_device(stcontext->ddev); in st95hf_remove()
1210 stcontext->nfcdev_free = true; in st95hf_remove()
1212 mutex_unlock(&stcontext->rm_lock); in st95hf_remove()
1215 result = down_killable(&stcontext->exchange_lock); in st95hf_remove()
1216 if (result == -EINTR) in st95hf_remove()
1217 dev_err(&spictx->spidev->dev, "sleep for semaphore interrupted by signal\n"); in st95hf_remove()
1220 result = st95hf_spi_send(&stcontext->spicontext, in st95hf_remove()
1225 dev_err(&spictx->spidev->dev, in st95hf_remove()
1232 if (stcontext->st95hf_supply) in st95hf_remove()
1233 regulator_disable(stcontext->st95hf_supply); in st95hf_remove()
1236 /* Register as SPI protocol driver */
1250 MODULE_DESCRIPTION("ST NFC Transceiver ST95HF driver");