Lines Matching +full:nfc +full:- +full:spi
1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI TRF7970a RFID/NFC Transceiver Driver
5 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
17 #include <linux/nfc.h>
22 #include <linux/spi/spi.h>
25 #include <net/nfc/nfc.h>
26 #include <net/nfc/digital.h>
29 * parallel mode, SPI with Slave Select (SS) mode, and SPI without
30 * SS mode. The driver only supports the two SPI modes.
39 * when EN2 is left low. The 'en2-rf-quirk' device tree property
62 * has sent the command to the tag, it simply returns -ECANCELED. If the
65 * sending the data upstream, it frees the rx_skb and sends -ECANCELED
83 * driver will wait 20 ms for the FIFO to drain past the low-watermark
84 * and generate an interrupt. The low-watermark set to 32 bytes so the
85 * interrupt should fire after 127 - 32 = 95 bytes have been sent. At
89 * Type 2 write and sector select commands respond with a 4-bit ACK or NACK.
99 * ISO/IEC 15693 frames specify whether to use single or double sub-carrier
108 * Note under Table 1-1 in section 1.6 of
110 * 10 ms for TI Tag-it HF-I tags; however testing has shown that is not long
111 * enough so 20 ms is used. So the timer is set to 40 ms - 20 ms to drain
130 /* TX length is 3 nibbles long ==> 4KB - 1 bytes max */
131 #define TRF7970A_TX_MAX (4096 - 1)
171 * whether to use a continuous SPI transaction or not, and the actual
382 /* NFC (ISO/IEC 14443A) Type 2 Tag commands */
432 struct spi_device *spi; member
470 dev_dbg(trf->dev, "cmd: 0x%x\n", cmd); in trf7970a_cmd()
472 ret = spi_write(trf->spi, &cmd, 1); in trf7970a_cmd()
474 dev_err(trf->dev, "%s - cmd: 0x%x, ret: %d\n", __func__, cmd, in trf7970a_cmd()
484 ret = spi_write_then_read(trf->spi, &addr, 1, val, 1); in trf7970a_read()
486 dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr, in trf7970a_read()
489 dev_dbg(trf->dev, "read(0x%x): 0x%x\n", addr, *val); in trf7970a_read()
502 dev_dbg(trf->dev, "read_cont(0x%x, %zd)\n", addr, len); in trf7970a_read_cont()
516 ret = spi_sync(trf->spi, &m); in trf7970a_read_cont()
518 dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr, in trf7970a_read_cont()
528 dev_dbg(trf->dev, "write(0x%x): 0x%x\n", reg, val); in trf7970a_write()
530 ret = spi_write(trf->spi, buf, 2); in trf7970a_write()
532 dev_err(trf->dev, "%s - write: 0x%x 0x%x, ret: %d\n", __func__, in trf7970a_write()
546 if (trf->quirks & TRF7970A_QUIRK_IRQ_STATUS_READ) { in trf7970a_read_irqstatus()
548 ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2); in trf7970a_read_irqstatus()
550 ret = spi_write_then_read(trf->spi, &addr, 1, buf, 1); in trf7970a_read_irqstatus()
554 dev_err(trf->dev, "%s - irqstatus: Status read failed: %d\n", in trf7970a_read_irqstatus()
567 if (!trf->custom_rx_gain_reduction) in trf7970a_update_rx_gain_reduction()
574 reg |= trf->rx_gain_reduction; in trf7970a_update_rx_gain_reduction()
606 ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2); in trf7970a_read_target_proto()
608 dev_err(trf->dev, "%s - target_proto: Read failed: %d\n", in trf7970a_read_target_proto()
639 dev_dbg(trf->dev, "%s - mode_detect: target_proto: 0x%x\n", in trf7970a_mode_detect()
641 return -EIO; in trf7970a_mode_detect()
651 dev_kfree_skb_any(trf->tx_skb); in trf7970a_send_upstream()
652 trf->tx_skb = NULL; in trf7970a_send_upstream()
654 if (trf->rx_skb && !IS_ERR(trf->rx_skb) && !trf->aborting) in trf7970a_send_upstream()
656 16, 1, trf->rx_skb->data, trf->rx_skb->len, in trf7970a_send_upstream()
659 trf->state = TRF7970A_ST_IDLE; in trf7970a_send_upstream()
661 if (trf->aborting) { in trf7970a_send_upstream()
662 dev_dbg(trf->dev, "Abort process complete\n"); in trf7970a_send_upstream()
664 if (!IS_ERR(trf->rx_skb)) { in trf7970a_send_upstream()
665 kfree_skb(trf->rx_skb); in trf7970a_send_upstream()
666 trf->rx_skb = ERR_PTR(-ECANCELED); in trf7970a_send_upstream()
669 trf->aborting = false; in trf7970a_send_upstream()
672 trf->cb(trf->ddev, trf->cb_arg, trf->rx_skb); in trf7970a_send_upstream()
674 trf->rx_skb = NULL; in trf7970a_send_upstream()
679 dev_dbg(trf->dev, "Error - state: %d, errno: %d\n", trf->state, errno); in trf7970a_send_err_upstream()
681 cancel_delayed_work(&trf->timeout_work); in trf7970a_send_err_upstream()
683 kfree_skb(trf->rx_skb); in trf7970a_send_err_upstream()
684 trf->rx_skb = ERR_PTR(errno); in trf7970a_send_err_upstream()
699 16, 1, skb->data, len, false); in trf7970a_transmit()
709 t[1].tx_buf = skb->data; in trf7970a_transmit()
713 ret = spi_sync(trf->spi, &m); in trf7970a_transmit()
715 dev_err(trf->dev, "%s - Can't send tx data: %d\n", __func__, in trf7970a_transmit()
722 if (skb->len > 0) { in trf7970a_transmit()
723 trf->state = TRF7970A_ST_WAIT_FOR_TX_FIFO; in trf7970a_transmit()
726 if (trf->issue_eof) { in trf7970a_transmit()
727 trf->state = TRF7970A_ST_WAIT_TO_ISSUE_EOF; in trf7970a_transmit()
730 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA; in trf7970a_transmit()
732 if (!trf->timeout) in trf7970a_transmit()
735 timeout = trf->timeout; in trf7970a_transmit()
739 dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", timeout, in trf7970a_transmit()
740 trf->state); in trf7970a_transmit()
742 schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout)); in trf7970a_transmit()
749 struct sk_buff *skb = trf->tx_skb; in trf7970a_fill_fifo()
761 dev_dbg(trf->dev, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes); in trf7970a_fill_fifo()
766 len = TRF7970A_FIFO_SIZE - fifo_bytes; in trf7970a_fill_fifo()
768 schedule_delayed_work(&trf->timeout_work, in trf7970a_fill_fifo()
773 len = min(skb->len, len); in trf7970a_fill_fifo()
784 struct sk_buff *skb = trf->rx_skb; in trf7970a_drain_fifo()
789 trf7970a_send_err_upstream(trf, -EIO); in trf7970a_drain_fifo()
799 dev_dbg(trf->dev, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes); in trf7970a_drain_fifo()
812 trf7970a_send_err_upstream(trf, -ENOMEM); in trf7970a_drain_fifo()
816 kfree_skb(trf->rx_skb); in trf7970a_drain_fifo()
817 trf->rx_skb = skb; in trf7970a_drain_fifo()
828 if ((trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T) && (skb->len == 1) && in trf7970a_drain_fifo()
829 (trf->special_fcn_reg1 == TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX)) { in trf7970a_drain_fifo()
830 skb->data[0] >>= 4; in trf7970a_drain_fifo()
833 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA_CONT; in trf7970a_drain_fifo()
857 dev_dbg(trf->dev, "Setting timeout for %d ms\n", in trf7970a_drain_fifo()
860 schedule_delayed_work(&trf->timeout_work, in trf7970a_drain_fifo()
870 mutex_lock(&trf->lock); in trf7970a_irq()
872 if (trf->state == TRF7970A_ST_RF_OFF) { in trf7970a_irq()
873 mutex_unlock(&trf->lock); in trf7970a_irq()
879 mutex_unlock(&trf->lock); in trf7970a_irq()
883 dev_dbg(trf->dev, "IRQ - state: %d, status: 0x%x\n", trf->state, in trf7970a_irq()
887 mutex_unlock(&trf->lock); in trf7970a_irq()
891 switch (trf->state) { in trf7970a_irq()
899 if (trf->is_initiator && (status & TRF7970A_IRQ_STATUS_ERROR)) { in trf7970a_irq()
901 trf->state = TRF7970A_ST_IDLE_RX_BLOCKED; in trf7970a_irq()
908 trf->ignore_timeout = in trf7970a_irq()
909 !cancel_delayed_work(&trf->timeout_work); in trf7970a_irq()
912 trf7970a_send_err_upstream(trf, -EIO); in trf7970a_irq()
918 trf->ignore_timeout = in trf7970a_irq()
919 !cancel_delayed_work(&trf->timeout_work); in trf7970a_irq()
932 (!trf->is_initiator && in trf7970a_irq()
937 if (!trf->timeout) { in trf7970a_irq()
938 trf->ignore_timeout = in trf7970a_irq()
939 !cancel_delayed_work(&trf->timeout_work); in trf7970a_irq()
940 trf->rx_skb = ERR_PTR(0); in trf7970a_irq()
945 if (trf->is_initiator) in trf7970a_irq()
948 iso_ctrl = trf->iso_ctrl; in trf7970a_irq()
950 switch (trf->framing) { in trf7970a_irq()
952 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC; in trf7970a_irq()
954 trf->iso_ctrl = 0xff; /* Force ISO_CTRL write */ in trf7970a_irq()
957 trf->tx_cmd = TRF7970A_CMD_TRANSMIT; in trf7970a_irq()
959 trf->iso_ctrl = 0xff; /* Force ISO_CTRL write */ in trf7970a_irq()
968 trf->special_fcn_reg1 = in trf7970a_irq()
975 if (iso_ctrl != trf->iso_ctrl) { in trf7970a_irq()
980 trf->iso_ctrl = iso_ctrl; in trf7970a_irq()
983 trf7970a_send_err_upstream(trf, -EIO); in trf7970a_irq()
988 trf7970a_send_err_upstream(trf, -EIO); in trf7970a_irq()
992 trf->ignore_timeout = in trf7970a_irq()
993 !cancel_delayed_work(&trf->timeout_work); in trf7970a_irq()
996 trf7970a_send_err_upstream(trf, -EIO); in trf7970a_irq()
1001 trf->ignore_timeout = in trf7970a_irq()
1002 !cancel_delayed_work(&trf->timeout_work); in trf7970a_irq()
1004 ret = trf7970a_mode_detect(trf, &trf->md_rf_tech); in trf7970a_irq()
1008 trf->state = TRF7970A_ST_LISTENING; in trf7970a_irq()
1012 trf7970a_send_err_upstream(trf, -EIO); in trf7970a_irq()
1016 dev_err(trf->dev, "%s - Driver in invalid state: %d\n", in trf7970a_irq()
1017 __func__, trf->state); in trf7970a_irq()
1021 mutex_unlock(&trf->lock); in trf7970a_irq()
1029 dev_dbg(trf->dev, "Issuing EOF\n"); in trf7970a_issue_eof()
1039 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA; in trf7970a_issue_eof()
1041 dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", in trf7970a_issue_eof()
1042 trf->timeout, trf->state); in trf7970a_issue_eof()
1044 schedule_delayed_work(&trf->timeout_work, in trf7970a_issue_eof()
1045 msecs_to_jiffies(trf->timeout)); in trf7970a_issue_eof()
1053 dev_dbg(trf->dev, "Timeout - state: %d, ignore_timeout: %d\n", in trf7970a_timeout_work_handler()
1054 trf->state, trf->ignore_timeout); in trf7970a_timeout_work_handler()
1056 mutex_lock(&trf->lock); in trf7970a_timeout_work_handler()
1058 if (trf->ignore_timeout) in trf7970a_timeout_work_handler()
1059 trf->ignore_timeout = false; in trf7970a_timeout_work_handler()
1060 else if (trf->state == TRF7970A_ST_WAIT_FOR_RX_DATA_CONT) in trf7970a_timeout_work_handler()
1062 else if (trf->state == TRF7970A_ST_WAIT_TO_ISSUE_EOF) in trf7970a_timeout_work_handler()
1065 trf7970a_send_err_upstream(trf, -ETIMEDOUT); in trf7970a_timeout_work_handler()
1067 mutex_unlock(&trf->lock); in trf7970a_timeout_work_handler()
1074 dev_dbg(trf->dev, "Initializing device - state: %d\n", trf->state); in trf7970a_init()
1090 trf->io_ctrl | TRF7970A_REG_IO_CTRL_VRS(0x1)); in trf7970a_init()
1100 trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON; in trf7970a_init()
1103 trf->modulator_sys_clk_ctrl); in trf7970a_init()
1117 trf->special_fcn_reg1 = 0; in trf7970a_init()
1119 trf->iso_ctrl = 0xff; in trf7970a_init()
1123 dev_dbg(trf->dev, "Couldn't init device: %d\n", ret); in trf7970a_init()
1129 if ((trf->state == TRF7970A_ST_PWR_OFF) || in trf7970a_switch_rf_off()
1130 (trf->state == TRF7970A_ST_RF_OFF)) in trf7970a_switch_rf_off()
1133 dev_dbg(trf->dev, "Switching rf off\n"); in trf7970a_switch_rf_off()
1135 trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON; in trf7970a_switch_rf_off()
1137 trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, trf->chip_status_ctrl); in trf7970a_switch_rf_off()
1139 trf->aborting = false; in trf7970a_switch_rf_off()
1140 trf->state = TRF7970A_ST_RF_OFF; in trf7970a_switch_rf_off()
1142 pm_runtime_mark_last_busy(trf->dev); in trf7970a_switch_rf_off()
1143 pm_runtime_put_autosuspend(trf->dev); in trf7970a_switch_rf_off()
1150 dev_dbg(trf->dev, "Switching rf on\n"); in trf7970a_switch_rf_on()
1152 pm_runtime_get_sync(trf->dev); in trf7970a_switch_rf_on()
1154 if (trf->state != TRF7970A_ST_RF_OFF) { /* Power on, RF off */ in trf7970a_switch_rf_on()
1155 dev_err(trf->dev, "%s - Incorrect state: %d\n", __func__, in trf7970a_switch_rf_on()
1156 trf->state); in trf7970a_switch_rf_on()
1157 return -EINVAL; in trf7970a_switch_rf_on()
1162 dev_err(trf->dev, "%s - Can't initialize: %d\n", __func__, ret); in trf7970a_switch_rf_on()
1166 trf->state = TRF7970A_ST_IDLE; in trf7970a_switch_rf_on()
1176 dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on); in trf7970a_switch_rf()
1178 mutex_lock(&trf->lock); in trf7970a_switch_rf()
1181 switch (trf->state) { in trf7970a_switch_rf()
1190 dev_err(trf->dev, "%s - Invalid request: %d %d\n", in trf7970a_switch_rf()
1191 __func__, trf->state, on); in trf7970a_switch_rf()
1193 ret = -EINVAL; in trf7970a_switch_rf()
1196 switch (trf->state) { in trf7970a_switch_rf()
1201 dev_err(trf->dev, "%s - Invalid request: %d %d\n", in trf7970a_switch_rf()
1202 __func__, trf->state, on); in trf7970a_switch_rf()
1203 ret = -EINVAL; in trf7970a_switch_rf()
1213 mutex_unlock(&trf->lock); in trf7970a_switch_rf()
1221 dev_dbg(trf->dev, "rf technology: %d\n", tech); in trf7970a_in_config_rf_tech()
1225 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443A_106; in trf7970a_in_config_rf_tech()
1226 trf->modulator_sys_clk_ctrl = in trf7970a_in_config_rf_tech()
1227 (trf->modulator_sys_clk_ctrl & 0xf8) | in trf7970a_in_config_rf_tech()
1229 trf->guard_time = TRF7970A_GUARD_TIME_NFCA; in trf7970a_in_config_rf_tech()
1232 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443B_106; in trf7970a_in_config_rf_tech()
1233 trf->modulator_sys_clk_ctrl = in trf7970a_in_config_rf_tech()
1234 (trf->modulator_sys_clk_ctrl & 0xf8) | in trf7970a_in_config_rf_tech()
1236 trf->guard_time = TRF7970A_GUARD_TIME_NFCB; in trf7970a_in_config_rf_tech()
1239 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_212; in trf7970a_in_config_rf_tech()
1240 trf->modulator_sys_clk_ctrl = in trf7970a_in_config_rf_tech()
1241 (trf->modulator_sys_clk_ctrl & 0xf8) | in trf7970a_in_config_rf_tech()
1243 trf->guard_time = TRF7970A_GUARD_TIME_NFCF; in trf7970a_in_config_rf_tech()
1246 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_424; in trf7970a_in_config_rf_tech()
1247 trf->modulator_sys_clk_ctrl = in trf7970a_in_config_rf_tech()
1248 (trf->modulator_sys_clk_ctrl & 0xf8) | in trf7970a_in_config_rf_tech()
1250 trf->guard_time = TRF7970A_GUARD_TIME_NFCF; in trf7970a_in_config_rf_tech()
1253 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648; in trf7970a_in_config_rf_tech()
1254 trf->modulator_sys_clk_ctrl = in trf7970a_in_config_rf_tech()
1255 (trf->modulator_sys_clk_ctrl & 0xf8) | in trf7970a_in_config_rf_tech()
1257 trf->guard_time = TRF7970A_GUARD_TIME_15693; in trf7970a_in_config_rf_tech()
1260 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech); in trf7970a_in_config_rf_tech()
1261 return -EINVAL; in trf7970a_in_config_rf_tech()
1264 trf->technology = tech; in trf7970a_in_config_rf_tech()
1267 * PSL sequence (indicated by 'trf->iso_ctrl == 0xff' from in trf7970a_in_config_rf_tech()
1268 * trf7970a_init()), clear the NFC Target Detection Level register in trf7970a_in_config_rf_tech()
1271 if (trf->iso_ctrl == 0xff) in trf7970a_in_config_rf_tech()
1283 trf->chip_status_ctrl | in trf7970a_is_rf_field()
1299 trf->chip_status_ctrl); in trf7970a_is_rf_field()
1313 u8 iso_ctrl = trf->iso_ctrl_tech; in trf7970a_in_config_framing()
1317 dev_dbg(trf->dev, "framing: %d\n", framing); in trf7970a_in_config_framing()
1322 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC; in trf7970a_in_config_framing()
1335 trf->tx_cmd = TRF7970A_CMD_TRANSMIT; in trf7970a_in_config_framing()
1339 trf->tx_cmd = TRF7970A_CMD_TRANSMIT; in trf7970a_in_config_framing()
1343 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing); in trf7970a_in_config_framing()
1344 return -EINVAL; in trf7970a_in_config_framing()
1347 trf->framing = framing; in trf7970a_in_config_framing()
1349 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) { in trf7970a_in_config_framing()
1355 return -EBUSY; in trf7970a_in_config_framing()
1358 if (iso_ctrl != trf->iso_ctrl) { in trf7970a_in_config_framing()
1363 trf->iso_ctrl = iso_ctrl; in trf7970a_in_config_framing()
1366 trf->modulator_sys_clk_ctrl); in trf7970a_in_config_framing()
1371 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) { in trf7970a_in_config_framing()
1373 trf->chip_status_ctrl | in trf7970a_in_config_framing()
1378 trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON; in trf7970a_in_config_framing()
1380 usleep_range(trf->guard_time, trf->guard_time + 1000); in trf7970a_in_config_framing()
1392 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param); in trf7970a_in_configure_hw()
1394 mutex_lock(&trf->lock); in trf7970a_in_configure_hw()
1396 trf->is_initiator = true; in trf7970a_in_configure_hw()
1398 if ((trf->state == TRF7970A_ST_PWR_OFF) || in trf7970a_in_configure_hw()
1399 (trf->state == TRF7970A_ST_RF_OFF)) { in trf7970a_in_configure_hw()
1413 dev_dbg(trf->dev, "Unknown type: %d\n", type); in trf7970a_in_configure_hw()
1414 ret = -EINVAL; in trf7970a_in_configure_hw()
1418 mutex_unlock(&trf->lock); in trf7970a_in_configure_hw()
1441 const u8 *req = skb->data; in trf7970a_per_cmd_config()
1445 trf->issue_eof = false; in trf7970a_per_cmd_config()
1456 if ((trf->technology == NFC_DIGITAL_RF_TECH_106A) && in trf7970a_per_cmd_config()
1457 (trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T)) { in trf7970a_per_cmd_config()
1463 if (special_fcn_reg1 != trf->special_fcn_reg1) { in trf7970a_per_cmd_config()
1469 trf->special_fcn_reg1 = special_fcn_reg1; in trf7970a_per_cmd_config()
1471 } else if (trf->technology == NFC_DIGITAL_RF_TECH_ISO15693) { in trf7970a_per_cmd_config()
1472 iso_ctrl = trf->iso_ctrl & ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK; in trf7970a_per_cmd_config()
1490 if (iso_ctrl != trf->iso_ctrl) { in trf7970a_per_cmd_config()
1495 trf->iso_ctrl = iso_ctrl; in trf7970a_per_cmd_config()
1498 if ((trf->framing == NFC_DIGITAL_FRAMING_ISO15693_T5T) && in trf7970a_per_cmd_config()
1501 trf->issue_eof = true; in trf7970a_per_cmd_config()
1517 dev_dbg(trf->dev, "New request - state: %d, timeout: %d ms, len: %d\n", in trf7970a_send_cmd()
1518 trf->state, timeout, skb->len); in trf7970a_send_cmd()
1520 if (skb->len > TRF7970A_TX_MAX) in trf7970a_send_cmd()
1521 return -EINVAL; in trf7970a_send_cmd()
1523 mutex_lock(&trf->lock); in trf7970a_send_cmd()
1525 if ((trf->state != TRF7970A_ST_IDLE) && in trf7970a_send_cmd()
1526 (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) { in trf7970a_send_cmd()
1527 dev_err(trf->dev, "%s - Bogus state: %d\n", __func__, in trf7970a_send_cmd()
1528 trf->state); in trf7970a_send_cmd()
1529 ret = -EIO; in trf7970a_send_cmd()
1533 if (trf->aborting) { in trf7970a_send_cmd()
1534 dev_dbg(trf->dev, "Abort process complete\n"); in trf7970a_send_cmd()
1535 trf->aborting = false; in trf7970a_send_cmd()
1536 ret = -ECANCELED; in trf7970a_send_cmd()
1541 trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE, in trf7970a_send_cmd()
1543 if (!trf->rx_skb) { in trf7970a_send_cmd()
1544 dev_dbg(trf->dev, "Can't alloc rx_skb\n"); in trf7970a_send_cmd()
1545 ret = -ENOMEM; in trf7970a_send_cmd()
1550 if (trf->state == TRF7970A_ST_IDLE_RX_BLOCKED) { in trf7970a_send_cmd()
1555 trf->state = TRF7970A_ST_IDLE; in trf7970a_send_cmd()
1558 if (trf->is_initiator) { in trf7970a_send_cmd()
1564 trf->ddev = ddev; in trf7970a_send_cmd()
1565 trf->tx_skb = skb; in trf7970a_send_cmd()
1566 trf->cb = cb; in trf7970a_send_cmd()
1567 trf->cb_arg = arg; in trf7970a_send_cmd()
1568 trf->timeout = timeout; in trf7970a_send_cmd()
1569 trf->ignore_timeout = false; in trf7970a_send_cmd()
1571 len = skb->len; in trf7970a_send_cmd()
1581 TRF7970A_CMD_BIT_OPCODE(trf->tx_cmd); in trf7970a_send_cmd()
1584 if (trf->framing == NFC_DIGITAL_FRAMING_NFCA_SHORT) { in trf7970a_send_cmd()
1593 len = min_t(int, skb->len, TRF7970A_FIFO_SIZE); in trf7970a_send_cmd()
1602 kfree_skb(trf->rx_skb); in trf7970a_send_cmd()
1603 trf->rx_skb = NULL; in trf7970a_send_cmd()
1607 mutex_unlock(&trf->lock); in trf7970a_send_cmd()
1615 dev_dbg(trf->dev, "rf technology: %d\n", tech); in trf7970a_tg_config_rf_tech()
1619 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE | in trf7970a_tg_config_rf_tech()
1621 trf->modulator_sys_clk_ctrl = in trf7970a_tg_config_rf_tech()
1622 (trf->modulator_sys_clk_ctrl & 0xf8) | in trf7970a_tg_config_rf_tech()
1626 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE | in trf7970a_tg_config_rf_tech()
1628 trf->modulator_sys_clk_ctrl = in trf7970a_tg_config_rf_tech()
1629 (trf->modulator_sys_clk_ctrl & 0xf8) | in trf7970a_tg_config_rf_tech()
1633 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE | in trf7970a_tg_config_rf_tech()
1635 trf->modulator_sys_clk_ctrl = in trf7970a_tg_config_rf_tech()
1636 (trf->modulator_sys_clk_ctrl & 0xf8) | in trf7970a_tg_config_rf_tech()
1640 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech); in trf7970a_tg_config_rf_tech()
1641 return -EINVAL; in trf7970a_tg_config_rf_tech()
1644 trf->technology = tech; in trf7970a_tg_config_rf_tech()
1653 if ((trf->framing == NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED) && in trf7970a_tg_config_rf_tech()
1654 (trf->iso_ctrl_tech != trf->iso_ctrl)) { in trf7970a_tg_config_rf_tech()
1655 ret = trf7970a_update_iso_ctrl_register(trf, trf->iso_ctrl_tech); in trf7970a_tg_config_rf_tech()
1657 trf->iso_ctrl = trf->iso_ctrl_tech; in trf7970a_tg_config_rf_tech()
1671 u8 iso_ctrl = trf->iso_ctrl_tech; in trf7970a_tg_config_framing()
1674 dev_dbg(trf->dev, "framing: %d\n", framing); in trf7970a_tg_config_framing()
1678 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC; in trf7970a_tg_config_framing()
1685 iso_ctrl = trf->iso_ctrl; /* Don't write to ISO_CTRL yet */ in trf7970a_tg_config_framing()
1688 trf->tx_cmd = TRF7970A_CMD_TRANSMIT; in trf7970a_tg_config_framing()
1692 trf->tx_cmd = TRF7970A_CMD_TRANSMIT; in trf7970a_tg_config_framing()
1696 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing); in trf7970a_tg_config_framing()
1697 return -EINVAL; in trf7970a_tg_config_framing()
1700 trf->framing = framing; in trf7970a_tg_config_framing()
1702 if (iso_ctrl != trf->iso_ctrl) { in trf7970a_tg_config_framing()
1707 trf->iso_ctrl = iso_ctrl; in trf7970a_tg_config_framing()
1710 trf->modulator_sys_clk_ctrl); in trf7970a_tg_config_framing()
1715 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) { in trf7970a_tg_config_framing()
1717 trf->chip_status_ctrl | in trf7970a_tg_config_framing()
1722 trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON; in trf7970a_tg_config_framing()
1734 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param); in trf7970a_tg_configure_hw()
1736 mutex_lock(&trf->lock); in trf7970a_tg_configure_hw()
1738 trf->is_initiator = false; in trf7970a_tg_configure_hw()
1740 if ((trf->state == TRF7970A_ST_PWR_OFF) || in trf7970a_tg_configure_hw()
1741 (trf->state == TRF7970A_ST_RF_OFF)) { in trf7970a_tg_configure_hw()
1755 dev_dbg(trf->dev, "Unknown type: %d\n", type); in trf7970a_tg_configure_hw()
1756 ret = -EINVAL; in trf7970a_tg_configure_hw()
1760 mutex_unlock(&trf->lock); in trf7970a_tg_configure_hw()
1771 mutex_lock(&trf->lock); in _trf7970a_tg_listen()
1773 if ((trf->state != TRF7970A_ST_IDLE) && in _trf7970a_tg_listen()
1774 (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) { in _trf7970a_tg_listen()
1775 dev_err(trf->dev, "%s - Bogus state: %d\n", __func__, in _trf7970a_tg_listen()
1776 trf->state); in _trf7970a_tg_listen()
1777 ret = -EIO; in _trf7970a_tg_listen()
1781 if (trf->aborting) { in _trf7970a_tg_listen()
1782 dev_dbg(trf->dev, "Abort process complete\n"); in _trf7970a_tg_listen()
1783 trf->aborting = false; in _trf7970a_tg_listen()
1784 ret = -ECANCELED; in _trf7970a_tg_listen()
1788 trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE, in _trf7970a_tg_listen()
1790 if (!trf->rx_skb) { in _trf7970a_tg_listen()
1791 dev_dbg(trf->dev, "Can't alloc rx_skb\n"); in _trf7970a_tg_listen()
1792 ret = -ENOMEM; in _trf7970a_tg_listen()
1809 trf->io_ctrl | TRF7970A_REG_IO_CTRL_VRS(0x1)); in _trf7970a_tg_listen()
1823 trf->ddev = ddev; in _trf7970a_tg_listen()
1824 trf->cb = cb; in _trf7970a_tg_listen()
1825 trf->cb_arg = arg; in _trf7970a_tg_listen()
1826 trf->timeout = timeout; in _trf7970a_tg_listen()
1827 trf->ignore_timeout = false; in _trf7970a_tg_listen()
1833 trf->state = mode_detect ? TRF7970A_ST_LISTENING_MD : in _trf7970a_tg_listen()
1836 schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout)); in _trf7970a_tg_listen()
1839 mutex_unlock(&trf->lock); in _trf7970a_tg_listen()
1848 dev_dbg(trf->dev, "Listen - state: %d, timeout: %d ms\n", in trf7970a_tg_listen()
1849 trf->state, timeout); in trf7970a_tg_listen()
1861 dev_dbg(trf->dev, "Listen MD - state: %d, timeout: %d ms\n", in trf7970a_tg_listen_md()
1862 trf->state, timeout); in trf7970a_tg_listen_md()
1881 dev_dbg(trf->dev, "Get RF Tech - state: %d, rf_tech: %d\n", in trf7970a_tg_get_rf_tech()
1882 trf->state, trf->md_rf_tech); in trf7970a_tg_get_rf_tech()
1884 *rf_tech = trf->md_rf_tech; in trf7970a_tg_get_rf_tech()
1893 dev_dbg(trf->dev, "Abort process initiated\n"); in trf7970a_abort_cmd()
1895 mutex_lock(&trf->lock); in trf7970a_abort_cmd()
1897 switch (trf->state) { in trf7970a_abort_cmd()
1902 trf->aborting = true; in trf7970a_abort_cmd()
1905 trf->ignore_timeout = !cancel_delayed_work(&trf->timeout_work); in trf7970a_abort_cmd()
1906 trf7970a_send_err_upstream(trf, -ECANCELED); in trf7970a_abort_cmd()
1907 dev_dbg(trf->dev, "Abort process complete\n"); in trf7970a_abort_cmd()
1913 mutex_unlock(&trf->lock); in trf7970a_abort_cmd()
1932 dev_dbg(trf->dev, "Powering up - state: %d\n", trf->state); in trf7970a_power_up()
1934 if (trf->state != TRF7970A_ST_PWR_OFF) in trf7970a_power_up()
1937 ret = regulator_enable(trf->vin_regulator); in trf7970a_power_up()
1939 dev_err(trf->dev, "%s - Can't enable VIN: %d\n", __func__, ret); in trf7970a_power_up()
1945 if (trf->en2_gpiod && in trf7970a_power_up()
1946 !(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW)) { in trf7970a_power_up()
1947 gpiod_set_value_cansleep(trf->en2_gpiod, 1); in trf7970a_power_up()
1951 gpiod_set_value_cansleep(trf->en_gpiod, 1); in trf7970a_power_up()
1955 trf->state = TRF7970A_ST_RF_OFF; in trf7970a_power_up()
1964 dev_dbg(trf->dev, "Powering down - state: %d\n", trf->state); in trf7970a_power_down()
1966 if (trf->state == TRF7970A_ST_PWR_OFF) in trf7970a_power_down()
1969 if (trf->state != TRF7970A_ST_RF_OFF) { in trf7970a_power_down()
1970 dev_dbg(trf->dev, "Can't power down - not RF_OFF state (%d)\n", in trf7970a_power_down()
1971 trf->state); in trf7970a_power_down()
1972 return -EBUSY; in trf7970a_power_down()
1975 gpiod_set_value_cansleep(trf->en_gpiod, 0); in trf7970a_power_down()
1977 if (trf->en2_gpiod && !(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW)) in trf7970a_power_down()
1978 gpiod_set_value_cansleep(trf->en2_gpiod, 0); in trf7970a_power_down()
1980 ret = regulator_disable(trf->vin_regulator); in trf7970a_power_down()
1982 dev_err(trf->dev, "%s - Can't disable VIN: %d\n", __func__, in trf7970a_power_down()
1985 trf->state = TRF7970A_ST_PWR_OFF; in trf7970a_power_down()
2002 pm_runtime_set_active(trf->dev); in trf7970a_startup()
2003 pm_runtime_enable(trf->dev); in trf7970a_startup()
2004 pm_runtime_mark_last_busy(trf->dev); in trf7970a_startup()
2011 switch (trf->state) { in trf7970a_shutdown()
2017 trf7970a_send_err_upstream(trf, -ECANCELED); in trf7970a_shutdown()
2027 pm_runtime_disable(trf->dev); in trf7970a_shutdown()
2028 pm_runtime_set_suspended(trf->dev); in trf7970a_shutdown()
2037 ret = of_property_read_u32(np, "autosuspend-delay", &autosuspend_delay); in trf7970a_get_autosuspend_delay()
2044 static int trf7970a_probe(struct spi_device *spi) in trf7970a_probe() argument
2046 const struct device_node *np = spi->dev.of_node; in trf7970a_probe()
2053 dev_err(&spi->dev, "No Device Tree entry\n"); in trf7970a_probe()
2054 return -EINVAL; in trf7970a_probe()
2057 trf = devm_kzalloc(&spi->dev, sizeof(*trf), GFP_KERNEL); in trf7970a_probe()
2059 return -ENOMEM; in trf7970a_probe()
2061 trf->state = TRF7970A_ST_PWR_OFF; in trf7970a_probe()
2062 trf->dev = &spi->dev; in trf7970a_probe()
2063 trf->spi = spi; in trf7970a_probe()
2065 spi->mode = SPI_MODE_1; in trf7970a_probe()
2066 spi->bits_per_word = 8; in trf7970a_probe()
2068 ret = spi_setup(spi); in trf7970a_probe()
2070 dev_err(trf->dev, "Can't set up SPI Communication\n"); in trf7970a_probe()
2074 if (of_property_read_bool(np, "irq-status-read-quirk")) in trf7970a_probe()
2075 trf->quirks |= TRF7970A_QUIRK_IRQ_STATUS_READ; in trf7970a_probe()
2077 /* There are two enable pins - only EN must be present in the DT */ in trf7970a_probe()
2078 trf->en_gpiod = devm_gpiod_get_index(trf->dev, "ti,enable", 0, in trf7970a_probe()
2080 if (IS_ERR(trf->en_gpiod)) { in trf7970a_probe()
2081 dev_err(trf->dev, "No EN GPIO property\n"); in trf7970a_probe()
2082 return PTR_ERR(trf->en_gpiod); in trf7970a_probe()
2085 trf->en2_gpiod = devm_gpiod_get_index_optional(trf->dev, "ti,enable", 1, in trf7970a_probe()
2087 if (!trf->en2_gpiod) { in trf7970a_probe()
2088 dev_info(trf->dev, "No EN2 GPIO property\n"); in trf7970a_probe()
2089 } else if (IS_ERR(trf->en2_gpiod)) { in trf7970a_probe()
2090 dev_err(trf->dev, "Error getting EN2 GPIO property: %ld\n", in trf7970a_probe()
2091 PTR_ERR(trf->en2_gpiod)); in trf7970a_probe()
2092 return PTR_ERR(trf->en2_gpiod); in trf7970a_probe()
2093 } else if (of_property_read_bool(np, "en2-rf-quirk")) { in trf7970a_probe()
2094 trf->quirks |= TRF7970A_QUIRK_EN2_MUST_STAY_LOW; in trf7970a_probe()
2097 of_property_read_u32(np, "clock-frequency", &clk_freq); in trf7970a_probe()
2100 dev_err(trf->dev, in trf7970a_probe()
2101 "clock-frequency (%u Hz) unsupported\n", clk_freq); in trf7970a_probe()
2102 return -EINVAL; in trf7970a_probe()
2106 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_27MHZ; in trf7970a_probe()
2107 dev_dbg(trf->dev, "trf7970a configured for 27MHz crystal\n"); in trf7970a_probe()
2109 trf->modulator_sys_clk_ctrl = 0; in trf7970a_probe()
2112 if (of_property_read_u32(np, "ti,rx-gain-reduction-db", &rx_gain_reduction_db) == 0) { in trf7970a_probe()
2114 dev_warn(trf->dev, "RX Gain reduction too high. Ignored\n"); in trf7970a_probe()
2116 dev_warn(trf->dev, "RX Gain must be set in 5 dB increments. Ignored\n"); in trf7970a_probe()
2118 dev_dbg(trf->dev, "RX gain set to -%udB\n", rx_gain_reduction_db); in trf7970a_probe()
2119 trf->rx_gain_reduction = ((rx_gain_reduction_db / in trf7970a_probe()
2122 trf->custom_rx_gain_reduction = true; in trf7970a_probe()
2126 ret = devm_request_threaded_irq(trf->dev, spi->irq, NULL, in trf7970a_probe()
2131 dev_err(trf->dev, "Can't request IRQ#%d: %d\n", spi->irq, ret); in trf7970a_probe()
2135 mutex_init(&trf->lock); in trf7970a_probe()
2136 INIT_DELAYED_WORK(&trf->timeout_work, trf7970a_timeout_work_handler); in trf7970a_probe()
2138 trf->vin_regulator = devm_regulator_get(&spi->dev, "vin"); in trf7970a_probe()
2139 if (IS_ERR(trf->vin_regulator)) { in trf7970a_probe()
2140 ret = PTR_ERR(trf->vin_regulator); in trf7970a_probe()
2141 dev_err(trf->dev, "Can't get VIN regulator: %d\n", ret); in trf7970a_probe()
2145 ret = regulator_enable(trf->vin_regulator); in trf7970a_probe()
2147 dev_err(trf->dev, "Can't enable VIN: %d\n", ret); in trf7970a_probe()
2151 uvolts = regulator_get_voltage(trf->vin_regulator); in trf7970a_probe()
2153 trf->chip_status_ctrl = TRF7970A_CHIP_STATUS_VRS5_3; in trf7970a_probe()
2155 trf->vddio_regulator = devm_regulator_get(&spi->dev, "vdd-io"); in trf7970a_probe()
2156 if (IS_ERR(trf->vddio_regulator)) { in trf7970a_probe()
2157 ret = PTR_ERR(trf->vddio_regulator); in trf7970a_probe()
2158 dev_err(trf->dev, "Can't get VDD_IO regulator: %d\n", ret); in trf7970a_probe()
2162 ret = regulator_enable(trf->vddio_regulator); in trf7970a_probe()
2164 dev_err(trf->dev, "Can't enable VDD_IO: %d\n", ret); in trf7970a_probe()
2168 if (regulator_get_voltage(trf->vddio_regulator) == 1800000) { in trf7970a_probe()
2169 trf->io_ctrl = TRF7970A_REG_IO_CTRL_IO_LOW; in trf7970a_probe()
2170 dev_dbg(trf->dev, "trf7970a config vdd_io to 1.8V\n"); in trf7970a_probe()
2173 trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops, in trf7970a_probe()
2178 if (!trf->ddev) { in trf7970a_probe()
2179 dev_err(trf->dev, "Can't allocate NFC digital device\n"); in trf7970a_probe()
2180 ret = -ENOMEM; in trf7970a_probe()
2184 nfc_digital_set_parent_dev(trf->ddev, trf->dev); in trf7970a_probe()
2185 nfc_digital_set_drvdata(trf->ddev, trf); in trf7970a_probe()
2186 spi_set_drvdata(spi, trf); in trf7970a_probe()
2190 pm_runtime_set_autosuspend_delay(trf->dev, autosuspend_delay); in trf7970a_probe()
2191 pm_runtime_use_autosuspend(trf->dev); in trf7970a_probe()
2197 ret = nfc_digital_register_device(trf->ddev); in trf7970a_probe()
2199 dev_err(trf->dev, "Can't register NFC digital device: %d\n", in trf7970a_probe()
2209 nfc_digital_free_device(trf->ddev); in trf7970a_probe()
2211 regulator_disable(trf->vddio_regulator); in trf7970a_probe()
2213 regulator_disable(trf->vin_regulator); in trf7970a_probe()
2215 mutex_destroy(&trf->lock); in trf7970a_probe()
2219 static void trf7970a_remove(struct spi_device *spi) in trf7970a_remove() argument
2221 struct trf7970a *trf = spi_get_drvdata(spi); in trf7970a_remove()
2223 mutex_lock(&trf->lock); in trf7970a_remove()
2227 mutex_unlock(&trf->lock); in trf7970a_remove()
2229 nfc_digital_unregister_device(trf->ddev); in trf7970a_remove()
2230 nfc_digital_free_device(trf->ddev); in trf7970a_remove()
2232 regulator_disable(trf->vddio_regulator); in trf7970a_remove()
2233 regulator_disable(trf->vin_regulator); in trf7970a_remove()
2235 mutex_destroy(&trf->lock); in trf7970a_remove()
2241 struct spi_device *spi = to_spi_device(dev); in trf7970a_suspend() local
2242 struct trf7970a *trf = spi_get_drvdata(spi); in trf7970a_suspend()
2244 mutex_lock(&trf->lock); in trf7970a_suspend()
2248 mutex_unlock(&trf->lock); in trf7970a_suspend()
2255 struct spi_device *spi = to_spi_device(dev); in trf7970a_resume() local
2256 struct trf7970a *trf = spi_get_drvdata(spi); in trf7970a_resume()
2259 mutex_lock(&trf->lock); in trf7970a_resume()
2263 mutex_unlock(&trf->lock); in trf7970a_resume()
2272 struct spi_device *spi = to_spi_device(dev); in trf7970a_pm_runtime_suspend() local
2273 struct trf7970a *trf = spi_get_drvdata(spi); in trf7970a_pm_runtime_suspend()
2276 mutex_lock(&trf->lock); in trf7970a_pm_runtime_suspend()
2280 mutex_unlock(&trf->lock); in trf7970a_pm_runtime_suspend()
2287 struct spi_device *spi = to_spi_device(dev); in trf7970a_pm_runtime_resume() local
2288 struct trf7970a *trf = spi_get_drvdata(spi); in trf7970a_pm_runtime_resume()
2317 MODULE_DEVICE_TABLE(spi, trf7970a_id_table);
2334 MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver");