Lines Matching +full:autosuspend +full:- +full:delay +full:- +full:ms

1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
19 #include <linux/delay.h>
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
73 * those cases, the timeout mechanism is used to wait 20 ms in case more
74 * data arrives. After 20 ms, it is assumed that all of the data has been
79 * been received and there isn't an error). The delay is 20 ms since delays
80 * of ~16 ms have been observed during testing.
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
87 * to ~14.35 ms so 20 ms is used for the timeout.
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
112 * up to 127 bytes in the FIFO at the lowest bit rate plus another 20 ms to
130 /* TX length is 3 nibbles long ==> 4KB - 1 bytes max */
131 #define TRF7970A_TX_MAX (4096 - 1)
461 dev_dbg(trf->dev, "cmd: 0x%x\n", cmd); in trf7970a_cmd()
463 ret = spi_write(trf->spi, &cmd, 1); in trf7970a_cmd()
465 dev_err(trf->dev, "%s - cmd: 0x%x, ret: %d\n", __func__, cmd, in trf7970a_cmd()
475 ret = spi_write_then_read(trf->spi, &addr, 1, val, 1); in trf7970a_read()
477 dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr, in trf7970a_read()
480 dev_dbg(trf->dev, "read(0x%x): 0x%x\n", addr, *val); in trf7970a_read()
493 dev_dbg(trf->dev, "read_cont(0x%x, %zd)\n", addr, len); in trf7970a_read_cont()
507 ret = spi_sync(trf->spi, &m); in trf7970a_read_cont()
509 dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr, in trf7970a_read_cont()
519 dev_dbg(trf->dev, "write(0x%x): 0x%x\n", reg, val); in trf7970a_write()
521 ret = spi_write(trf->spi, buf, 2); in trf7970a_write()
523 dev_err(trf->dev, "%s - write: 0x%x 0x%x, ret: %d\n", __func__, in trf7970a_write()
537 if (trf->quirks & TRF7970A_QUIRK_IRQ_STATUS_READ) { in trf7970a_read_irqstatus()
539 ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2); in trf7970a_read_irqstatus()
541 ret = spi_write_then_read(trf->spi, &addr, 1, buf, 1); in trf7970a_read_irqstatus()
545 dev_err(trf->dev, "%s - irqstatus: Status read failed: %d\n", in trf7970a_read_irqstatus()
562 ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2); in trf7970a_read_target_proto()
564 dev_err(trf->dev, "%s - target_proto: Read failed: %d\n", in trf7970a_read_target_proto()
595 dev_dbg(trf->dev, "%s - mode_detect: target_proto: 0x%x\n", in trf7970a_mode_detect()
597 return -EIO; in trf7970a_mode_detect()
607 dev_kfree_skb_any(trf->tx_skb); in trf7970a_send_upstream()
608 trf->tx_skb = NULL; in trf7970a_send_upstream()
610 if (trf->rx_skb && !IS_ERR(trf->rx_skb) && !trf->aborting) in trf7970a_send_upstream()
612 16, 1, trf->rx_skb->data, trf->rx_skb->len, in trf7970a_send_upstream()
615 trf->state = TRF7970A_ST_IDLE; in trf7970a_send_upstream()
617 if (trf->aborting) { in trf7970a_send_upstream()
618 dev_dbg(trf->dev, "Abort process complete\n"); in trf7970a_send_upstream()
620 if (!IS_ERR(trf->rx_skb)) { in trf7970a_send_upstream()
621 kfree_skb(trf->rx_skb); in trf7970a_send_upstream()
622 trf->rx_skb = ERR_PTR(-ECANCELED); in trf7970a_send_upstream()
625 trf->aborting = false; in trf7970a_send_upstream()
628 trf->cb(trf->ddev, trf->cb_arg, trf->rx_skb); in trf7970a_send_upstream()
630 trf->rx_skb = NULL; in trf7970a_send_upstream()
635 dev_dbg(trf->dev, "Error - state: %d, errno: %d\n", trf->state, errno); in trf7970a_send_err_upstream()
637 cancel_delayed_work(&trf->timeout_work); in trf7970a_send_err_upstream()
639 kfree_skb(trf->rx_skb); in trf7970a_send_err_upstream()
640 trf->rx_skb = ERR_PTR(errno); in trf7970a_send_err_upstream()
655 16, 1, skb->data, len, false); in trf7970a_transmit()
665 t[1].tx_buf = skb->data; in trf7970a_transmit()
669 ret = spi_sync(trf->spi, &m); in trf7970a_transmit()
671 dev_err(trf->dev, "%s - Can't send tx data: %d\n", __func__, in trf7970a_transmit()
678 if (skb->len > 0) { in trf7970a_transmit()
679 trf->state = TRF7970A_ST_WAIT_FOR_TX_FIFO; in trf7970a_transmit()
682 if (trf->issue_eof) { in trf7970a_transmit()
683 trf->state = TRF7970A_ST_WAIT_TO_ISSUE_EOF; in trf7970a_transmit()
686 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA; in trf7970a_transmit()
688 if (!trf->timeout) in trf7970a_transmit()
691 timeout = trf->timeout; in trf7970a_transmit()
695 dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", timeout, in trf7970a_transmit()
696 trf->state); in trf7970a_transmit()
698 schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout)); in trf7970a_transmit()
705 struct sk_buff *skb = trf->tx_skb; in trf7970a_fill_fifo()
717 dev_dbg(trf->dev, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes); in trf7970a_fill_fifo()
722 len = TRF7970A_FIFO_SIZE - fifo_bytes; in trf7970a_fill_fifo()
724 schedule_delayed_work(&trf->timeout_work, in trf7970a_fill_fifo()
729 len = min(skb->len, len); in trf7970a_fill_fifo()
740 struct sk_buff *skb = trf->rx_skb; in trf7970a_drain_fifo()
745 trf7970a_send_err_upstream(trf, -EIO); in trf7970a_drain_fifo()
755 dev_dbg(trf->dev, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes); in trf7970a_drain_fifo()
768 trf7970a_send_err_upstream(trf, -ENOMEM); in trf7970a_drain_fifo()
772 kfree_skb(trf->rx_skb); in trf7970a_drain_fifo()
773 trf->rx_skb = skb; in trf7970a_drain_fifo()
784 if ((trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T) && (skb->len == 1) && in trf7970a_drain_fifo()
785 (trf->special_fcn_reg1 == TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX)) { in trf7970a_drain_fifo()
786 skb->data[0] >>= 4; in trf7970a_drain_fifo()
789 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA_CONT; in trf7970a_drain_fifo()
813 dev_dbg(trf->dev, "Setting timeout for %d ms\n", in trf7970a_drain_fifo()
816 schedule_delayed_work(&trf->timeout_work, in trf7970a_drain_fifo()
826 mutex_lock(&trf->lock); in trf7970a_irq()
828 if (trf->state == TRF7970A_ST_RF_OFF) { in trf7970a_irq()
829 mutex_unlock(&trf->lock); in trf7970a_irq()
835 mutex_unlock(&trf->lock); in trf7970a_irq()
839 dev_dbg(trf->dev, "IRQ - state: %d, status: 0x%x\n", trf->state, in trf7970a_irq()
843 mutex_unlock(&trf->lock); in trf7970a_irq()
847 switch (trf->state) { in trf7970a_irq()
855 if (trf->is_initiator && (status & TRF7970A_IRQ_STATUS_ERROR)) { in trf7970a_irq()
857 trf->state = TRF7970A_ST_IDLE_RX_BLOCKED; in trf7970a_irq()
864 trf->ignore_timeout = in trf7970a_irq()
865 !cancel_delayed_work(&trf->timeout_work); in trf7970a_irq()
868 trf7970a_send_err_upstream(trf, -EIO); in trf7970a_irq()
874 trf->ignore_timeout = in trf7970a_irq()
875 !cancel_delayed_work(&trf->timeout_work); in trf7970a_irq()
888 (!trf->is_initiator && in trf7970a_irq()
893 if (!trf->timeout) { in trf7970a_irq()
894 trf->ignore_timeout = in trf7970a_irq()
895 !cancel_delayed_work(&trf->timeout_work); in trf7970a_irq()
896 trf->rx_skb = ERR_PTR(0); in trf7970a_irq()
901 if (trf->is_initiator) in trf7970a_irq()
904 iso_ctrl = trf->iso_ctrl; in trf7970a_irq()
906 switch (trf->framing) { in trf7970a_irq()
908 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC; in trf7970a_irq()
910 trf->iso_ctrl = 0xff; /* Force ISO_CTRL write */ in trf7970a_irq()
913 trf->tx_cmd = TRF7970A_CMD_TRANSMIT; in trf7970a_irq()
915 trf->iso_ctrl = 0xff; /* Force ISO_CTRL write */ in trf7970a_irq()
924 trf->special_fcn_reg1 = in trf7970a_irq()
931 if (iso_ctrl != trf->iso_ctrl) { in trf7970a_irq()
937 trf->iso_ctrl = iso_ctrl; in trf7970a_irq()
940 trf7970a_send_err_upstream(trf, -EIO); in trf7970a_irq()
945 trf7970a_send_err_upstream(trf, -EIO); in trf7970a_irq()
949 trf->ignore_timeout = in trf7970a_irq()
950 !cancel_delayed_work(&trf->timeout_work); in trf7970a_irq()
953 trf7970a_send_err_upstream(trf, -EIO); in trf7970a_irq()
958 trf->ignore_timeout = in trf7970a_irq()
959 !cancel_delayed_work(&trf->timeout_work); in trf7970a_irq()
961 ret = trf7970a_mode_detect(trf, &trf->md_rf_tech); in trf7970a_irq()
965 trf->state = TRF7970A_ST_LISTENING; in trf7970a_irq()
969 trf7970a_send_err_upstream(trf, -EIO); in trf7970a_irq()
973 dev_err(trf->dev, "%s - Driver in invalid state: %d\n", in trf7970a_irq()
974 __func__, trf->state); in trf7970a_irq()
978 mutex_unlock(&trf->lock); in trf7970a_irq()
986 dev_dbg(trf->dev, "Issuing EOF\n"); in trf7970a_issue_eof()
996 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA; in trf7970a_issue_eof()
998 dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", in trf7970a_issue_eof()
999 trf->timeout, trf->state); in trf7970a_issue_eof()
1001 schedule_delayed_work(&trf->timeout_work, in trf7970a_issue_eof()
1002 msecs_to_jiffies(trf->timeout)); in trf7970a_issue_eof()
1010 dev_dbg(trf->dev, "Timeout - state: %d, ignore_timeout: %d\n", in trf7970a_timeout_work_handler()
1011 trf->state, trf->ignore_timeout); in trf7970a_timeout_work_handler()
1013 mutex_lock(&trf->lock); in trf7970a_timeout_work_handler()
1015 if (trf->ignore_timeout) in trf7970a_timeout_work_handler()
1016 trf->ignore_timeout = false; in trf7970a_timeout_work_handler()
1017 else if (trf->state == TRF7970A_ST_WAIT_FOR_RX_DATA_CONT) in trf7970a_timeout_work_handler()
1019 else if (trf->state == TRF7970A_ST_WAIT_TO_ISSUE_EOF) in trf7970a_timeout_work_handler()
1022 trf7970a_send_err_upstream(trf, -ETIMEDOUT); in trf7970a_timeout_work_handler()
1024 mutex_unlock(&trf->lock); in trf7970a_timeout_work_handler()
1031 dev_dbg(trf->dev, "Initializing device - state: %d\n", trf->state); in trf7970a_init()
1042 trf->io_ctrl | TRF7970A_REG_IO_CTRL_VRS(0x1)); in trf7970a_init()
1052 trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON; in trf7970a_init()
1055 trf->modulator_sys_clk_ctrl); in trf7970a_init()
1069 trf->special_fcn_reg1 = 0; in trf7970a_init()
1071 trf->iso_ctrl = 0xff; in trf7970a_init()
1075 dev_dbg(trf->dev, "Couldn't init device: %d\n", ret); in trf7970a_init()
1081 if ((trf->state == TRF7970A_ST_PWR_OFF) || in trf7970a_switch_rf_off()
1082 (trf->state == TRF7970A_ST_RF_OFF)) in trf7970a_switch_rf_off()
1085 dev_dbg(trf->dev, "Switching rf off\n"); in trf7970a_switch_rf_off()
1087 trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON; in trf7970a_switch_rf_off()
1089 trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, trf->chip_status_ctrl); in trf7970a_switch_rf_off()
1091 trf->aborting = false; in trf7970a_switch_rf_off()
1092 trf->state = TRF7970A_ST_RF_OFF; in trf7970a_switch_rf_off()
1094 pm_runtime_mark_last_busy(trf->dev); in trf7970a_switch_rf_off()
1095 pm_runtime_put_autosuspend(trf->dev); in trf7970a_switch_rf_off()
1102 dev_dbg(trf->dev, "Switching rf on\n"); in trf7970a_switch_rf_on()
1104 pm_runtime_get_sync(trf->dev); in trf7970a_switch_rf_on()
1106 if (trf->state != TRF7970A_ST_RF_OFF) { /* Power on, RF off */ in trf7970a_switch_rf_on()
1107 dev_err(trf->dev, "%s - Incorrect state: %d\n", __func__, in trf7970a_switch_rf_on()
1108 trf->state); in trf7970a_switch_rf_on()
1109 return -EINVAL; in trf7970a_switch_rf_on()
1114 dev_err(trf->dev, "%s - Can't initialize: %d\n", __func__, ret); in trf7970a_switch_rf_on()
1118 trf->state = TRF7970A_ST_IDLE; in trf7970a_switch_rf_on()
1128 dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on); in trf7970a_switch_rf()
1130 mutex_lock(&trf->lock); in trf7970a_switch_rf()
1133 switch (trf->state) { in trf7970a_switch_rf()
1142 dev_err(trf->dev, "%s - Invalid request: %d %d\n", in trf7970a_switch_rf()
1143 __func__, trf->state, on); in trf7970a_switch_rf()
1145 ret = -EINVAL; in trf7970a_switch_rf()
1148 switch (trf->state) { in trf7970a_switch_rf()
1153 dev_err(trf->dev, "%s - Invalid request: %d %d\n", in trf7970a_switch_rf()
1154 __func__, trf->state, on); in trf7970a_switch_rf()
1155 ret = -EINVAL; in trf7970a_switch_rf()
1165 mutex_unlock(&trf->lock); in trf7970a_switch_rf()
1173 dev_dbg(trf->dev, "rf technology: %d\n", tech); in trf7970a_in_config_rf_tech()
1177 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443A_106; in trf7970a_in_config_rf_tech()
1178 trf->modulator_sys_clk_ctrl = in trf7970a_in_config_rf_tech()
1179 (trf->modulator_sys_clk_ctrl & 0xf8) | in trf7970a_in_config_rf_tech()
1181 trf->guard_time = TRF7970A_GUARD_TIME_NFCA; in trf7970a_in_config_rf_tech()
1184 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443B_106; in trf7970a_in_config_rf_tech()
1185 trf->modulator_sys_clk_ctrl = in trf7970a_in_config_rf_tech()
1186 (trf->modulator_sys_clk_ctrl & 0xf8) | in trf7970a_in_config_rf_tech()
1188 trf->guard_time = TRF7970A_GUARD_TIME_NFCB; in trf7970a_in_config_rf_tech()
1191 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_212; in trf7970a_in_config_rf_tech()
1192 trf->modulator_sys_clk_ctrl = in trf7970a_in_config_rf_tech()
1193 (trf->modulator_sys_clk_ctrl & 0xf8) | in trf7970a_in_config_rf_tech()
1195 trf->guard_time = TRF7970A_GUARD_TIME_NFCF; in trf7970a_in_config_rf_tech()
1198 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_424; in trf7970a_in_config_rf_tech()
1199 trf->modulator_sys_clk_ctrl = in trf7970a_in_config_rf_tech()
1200 (trf->modulator_sys_clk_ctrl & 0xf8) | in trf7970a_in_config_rf_tech()
1202 trf->guard_time = TRF7970A_GUARD_TIME_NFCF; in trf7970a_in_config_rf_tech()
1205 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648; in trf7970a_in_config_rf_tech()
1206 trf->modulator_sys_clk_ctrl = in trf7970a_in_config_rf_tech()
1207 (trf->modulator_sys_clk_ctrl & 0xf8) | in trf7970a_in_config_rf_tech()
1209 trf->guard_time = TRF7970A_GUARD_TIME_15693; in trf7970a_in_config_rf_tech()
1212 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech); in trf7970a_in_config_rf_tech()
1213 return -EINVAL; in trf7970a_in_config_rf_tech()
1216 trf->technology = tech; in trf7970a_in_config_rf_tech()
1219 * PSL sequence (indicated by 'trf->iso_ctrl == 0xff' from in trf7970a_in_config_rf_tech()
1223 if (trf->iso_ctrl == 0xff) in trf7970a_in_config_rf_tech()
1235 trf->chip_status_ctrl | in trf7970a_is_rf_field()
1251 trf->chip_status_ctrl); in trf7970a_is_rf_field()
1265 u8 iso_ctrl = trf->iso_ctrl_tech; in trf7970a_in_config_framing()
1269 dev_dbg(trf->dev, "framing: %d\n", framing); in trf7970a_in_config_framing()
1274 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC; in trf7970a_in_config_framing()
1287 trf->tx_cmd = TRF7970A_CMD_TRANSMIT; in trf7970a_in_config_framing()
1291 trf->tx_cmd = TRF7970A_CMD_TRANSMIT; in trf7970a_in_config_framing()
1295 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing); in trf7970a_in_config_framing()
1296 return -EINVAL; in trf7970a_in_config_framing()
1299 trf->framing = framing; in trf7970a_in_config_framing()
1301 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) { in trf7970a_in_config_framing()
1307 return -EBUSY; in trf7970a_in_config_framing()
1310 if (iso_ctrl != trf->iso_ctrl) { in trf7970a_in_config_framing()
1315 trf->iso_ctrl = iso_ctrl; in trf7970a_in_config_framing()
1318 trf->modulator_sys_clk_ctrl); in trf7970a_in_config_framing()
1323 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) { in trf7970a_in_config_framing()
1325 trf->chip_status_ctrl | in trf7970a_in_config_framing()
1330 trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON; in trf7970a_in_config_framing()
1332 usleep_range(trf->guard_time, trf->guard_time + 1000); in trf7970a_in_config_framing()
1344 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param); in trf7970a_in_configure_hw()
1346 mutex_lock(&trf->lock); in trf7970a_in_configure_hw()
1348 trf->is_initiator = true; in trf7970a_in_configure_hw()
1350 if ((trf->state == TRF7970A_ST_PWR_OFF) || in trf7970a_in_configure_hw()
1351 (trf->state == TRF7970A_ST_RF_OFF)) { in trf7970a_in_configure_hw()
1365 dev_dbg(trf->dev, "Unknown type: %d\n", type); in trf7970a_in_configure_hw()
1366 ret = -EINVAL; in trf7970a_in_configure_hw()
1370 mutex_unlock(&trf->lock); in trf7970a_in_configure_hw()
1393 const u8 *req = skb->data; in trf7970a_per_cmd_config()
1397 trf->issue_eof = false; in trf7970a_per_cmd_config()
1408 if ((trf->technology == NFC_DIGITAL_RF_TECH_106A) && in trf7970a_per_cmd_config()
1409 (trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T)) { in trf7970a_per_cmd_config()
1415 if (special_fcn_reg1 != trf->special_fcn_reg1) { in trf7970a_per_cmd_config()
1421 trf->special_fcn_reg1 = special_fcn_reg1; in trf7970a_per_cmd_config()
1423 } else if (trf->technology == NFC_DIGITAL_RF_TECH_ISO15693) { in trf7970a_per_cmd_config()
1424 iso_ctrl = trf->iso_ctrl & ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK; in trf7970a_per_cmd_config()
1442 if (iso_ctrl != trf->iso_ctrl) { in trf7970a_per_cmd_config()
1447 trf->iso_ctrl = iso_ctrl; in trf7970a_per_cmd_config()
1450 if ((trf->framing == NFC_DIGITAL_FRAMING_ISO15693_T5T) && in trf7970a_per_cmd_config()
1453 trf->issue_eof = true; in trf7970a_per_cmd_config()
1469 dev_dbg(trf->dev, "New request - state: %d, timeout: %d ms, len: %d\n", in trf7970a_send_cmd()
1470 trf->state, timeout, skb->len); in trf7970a_send_cmd()
1472 if (skb->len > TRF7970A_TX_MAX) in trf7970a_send_cmd()
1473 return -EINVAL; in trf7970a_send_cmd()
1475 mutex_lock(&trf->lock); in trf7970a_send_cmd()
1477 if ((trf->state != TRF7970A_ST_IDLE) && in trf7970a_send_cmd()
1478 (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) { in trf7970a_send_cmd()
1479 dev_err(trf->dev, "%s - Bogus state: %d\n", __func__, in trf7970a_send_cmd()
1480 trf->state); in trf7970a_send_cmd()
1481 ret = -EIO; in trf7970a_send_cmd()
1485 if (trf->aborting) { in trf7970a_send_cmd()
1486 dev_dbg(trf->dev, "Abort process complete\n"); in trf7970a_send_cmd()
1487 trf->aborting = false; in trf7970a_send_cmd()
1488 ret = -ECANCELED; in trf7970a_send_cmd()
1493 trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE, in trf7970a_send_cmd()
1495 if (!trf->rx_skb) { in trf7970a_send_cmd()
1496 dev_dbg(trf->dev, "Can't alloc rx_skb\n"); in trf7970a_send_cmd()
1497 ret = -ENOMEM; in trf7970a_send_cmd()
1502 if (trf->state == TRF7970A_ST_IDLE_RX_BLOCKED) { in trf7970a_send_cmd()
1507 trf->state = TRF7970A_ST_IDLE; in trf7970a_send_cmd()
1510 if (trf->is_initiator) { in trf7970a_send_cmd()
1516 trf->ddev = ddev; in trf7970a_send_cmd()
1517 trf->tx_skb = skb; in trf7970a_send_cmd()
1518 trf->cb = cb; in trf7970a_send_cmd()
1519 trf->cb_arg = arg; in trf7970a_send_cmd()
1520 trf->timeout = timeout; in trf7970a_send_cmd()
1521 trf->ignore_timeout = false; in trf7970a_send_cmd()
1523 len = skb->len; in trf7970a_send_cmd()
1533 TRF7970A_CMD_BIT_OPCODE(trf->tx_cmd); in trf7970a_send_cmd()
1536 if (trf->framing == NFC_DIGITAL_FRAMING_NFCA_SHORT) { in trf7970a_send_cmd()
1545 len = min_t(int, skb->len, TRF7970A_FIFO_SIZE); in trf7970a_send_cmd()
1554 kfree_skb(trf->rx_skb); in trf7970a_send_cmd()
1555 trf->rx_skb = NULL; in trf7970a_send_cmd()
1559 mutex_unlock(&trf->lock); in trf7970a_send_cmd()
1567 dev_dbg(trf->dev, "rf technology: %d\n", tech); in trf7970a_tg_config_rf_tech()
1571 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE | in trf7970a_tg_config_rf_tech()
1573 trf->modulator_sys_clk_ctrl = in trf7970a_tg_config_rf_tech()
1574 (trf->modulator_sys_clk_ctrl & 0xf8) | in trf7970a_tg_config_rf_tech()
1578 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE | in trf7970a_tg_config_rf_tech()
1580 trf->modulator_sys_clk_ctrl = in trf7970a_tg_config_rf_tech()
1581 (trf->modulator_sys_clk_ctrl & 0xf8) | in trf7970a_tg_config_rf_tech()
1585 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE | in trf7970a_tg_config_rf_tech()
1587 trf->modulator_sys_clk_ctrl = in trf7970a_tg_config_rf_tech()
1588 (trf->modulator_sys_clk_ctrl & 0xf8) | in trf7970a_tg_config_rf_tech()
1592 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech); in trf7970a_tg_config_rf_tech()
1593 return -EINVAL; in trf7970a_tg_config_rf_tech()
1596 trf->technology = tech; in trf7970a_tg_config_rf_tech()
1605 if ((trf->framing == NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED) && in trf7970a_tg_config_rf_tech()
1606 (trf->iso_ctrl_tech != trf->iso_ctrl)) { in trf7970a_tg_config_rf_tech()
1608 trf->iso_ctrl_tech); in trf7970a_tg_config_rf_tech()
1610 trf->iso_ctrl = trf->iso_ctrl_tech; in trf7970a_tg_config_rf_tech()
1624 u8 iso_ctrl = trf->iso_ctrl_tech; in trf7970a_tg_config_framing()
1627 dev_dbg(trf->dev, "framing: %d\n", framing); in trf7970a_tg_config_framing()
1631 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC; in trf7970a_tg_config_framing()
1638 iso_ctrl = trf->iso_ctrl; /* Don't write to ISO_CTRL yet */ in trf7970a_tg_config_framing()
1641 trf->tx_cmd = TRF7970A_CMD_TRANSMIT; in trf7970a_tg_config_framing()
1645 trf->tx_cmd = TRF7970A_CMD_TRANSMIT; in trf7970a_tg_config_framing()
1649 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing); in trf7970a_tg_config_framing()
1650 return -EINVAL; in trf7970a_tg_config_framing()
1653 trf->framing = framing; in trf7970a_tg_config_framing()
1655 if (iso_ctrl != trf->iso_ctrl) { in trf7970a_tg_config_framing()
1660 trf->iso_ctrl = iso_ctrl; in trf7970a_tg_config_framing()
1663 trf->modulator_sys_clk_ctrl); in trf7970a_tg_config_framing()
1668 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) { in trf7970a_tg_config_framing()
1670 trf->chip_status_ctrl | in trf7970a_tg_config_framing()
1675 trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON; in trf7970a_tg_config_framing()
1687 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param); in trf7970a_tg_configure_hw()
1689 mutex_lock(&trf->lock); in trf7970a_tg_configure_hw()
1691 trf->is_initiator = false; in trf7970a_tg_configure_hw()
1693 if ((trf->state == TRF7970A_ST_PWR_OFF) || in trf7970a_tg_configure_hw()
1694 (trf->state == TRF7970A_ST_RF_OFF)) { in trf7970a_tg_configure_hw()
1708 dev_dbg(trf->dev, "Unknown type: %d\n", type); in trf7970a_tg_configure_hw()
1709 ret = -EINVAL; in trf7970a_tg_configure_hw()
1713 mutex_unlock(&trf->lock); in trf7970a_tg_configure_hw()
1724 mutex_lock(&trf->lock); in _trf7970a_tg_listen()
1726 if ((trf->state != TRF7970A_ST_IDLE) && in _trf7970a_tg_listen()
1727 (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) { in _trf7970a_tg_listen()
1728 dev_err(trf->dev, "%s - Bogus state: %d\n", __func__, in _trf7970a_tg_listen()
1729 trf->state); in _trf7970a_tg_listen()
1730 ret = -EIO; in _trf7970a_tg_listen()
1734 if (trf->aborting) { in _trf7970a_tg_listen()
1735 dev_dbg(trf->dev, "Abort process complete\n"); in _trf7970a_tg_listen()
1736 trf->aborting = false; in _trf7970a_tg_listen()
1737 ret = -ECANCELED; in _trf7970a_tg_listen()
1741 trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE, in _trf7970a_tg_listen()
1743 if (!trf->rx_skb) { in _trf7970a_tg_listen()
1744 dev_dbg(trf->dev, "Can't alloc rx_skb\n"); in _trf7970a_tg_listen()
1745 ret = -ENOMEM; in _trf7970a_tg_listen()
1758 trf->io_ctrl | TRF7970A_REG_IO_CTRL_VRS(0x1)); in _trf7970a_tg_listen()
1772 trf->ddev = ddev; in _trf7970a_tg_listen()
1773 trf->cb = cb; in _trf7970a_tg_listen()
1774 trf->cb_arg = arg; in _trf7970a_tg_listen()
1775 trf->timeout = timeout; in _trf7970a_tg_listen()
1776 trf->ignore_timeout = false; in _trf7970a_tg_listen()
1782 trf->state = mode_detect ? TRF7970A_ST_LISTENING_MD : in _trf7970a_tg_listen()
1785 schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout)); in _trf7970a_tg_listen()
1788 mutex_unlock(&trf->lock); in _trf7970a_tg_listen()
1797 dev_dbg(trf->dev, "Listen - state: %d, timeout: %d ms\n", in trf7970a_tg_listen()
1798 trf->state, timeout); in trf7970a_tg_listen()
1810 dev_dbg(trf->dev, "Listen MD - state: %d, timeout: %d ms\n", in trf7970a_tg_listen_md()
1811 trf->state, timeout); in trf7970a_tg_listen_md()
1830 dev_dbg(trf->dev, "Get RF Tech - state: %d, rf_tech: %d\n", in trf7970a_tg_get_rf_tech()
1831 trf->state, trf->md_rf_tech); in trf7970a_tg_get_rf_tech()
1833 *rf_tech = trf->md_rf_tech; in trf7970a_tg_get_rf_tech()
1842 dev_dbg(trf->dev, "Abort process initiated\n"); in trf7970a_abort_cmd()
1844 mutex_lock(&trf->lock); in trf7970a_abort_cmd()
1846 switch (trf->state) { in trf7970a_abort_cmd()
1851 trf->aborting = true; in trf7970a_abort_cmd()
1854 trf->ignore_timeout = !cancel_delayed_work(&trf->timeout_work); in trf7970a_abort_cmd()
1855 trf7970a_send_err_upstream(trf, -ECANCELED); in trf7970a_abort_cmd()
1856 dev_dbg(trf->dev, "Abort process complete\n"); in trf7970a_abort_cmd()
1862 mutex_unlock(&trf->lock); in trf7970a_abort_cmd()
1881 dev_dbg(trf->dev, "Powering up - state: %d\n", trf->state); in trf7970a_power_up()
1883 if (trf->state != TRF7970A_ST_PWR_OFF) in trf7970a_power_up()
1886 ret = regulator_enable(trf->regulator); in trf7970a_power_up()
1888 dev_err(trf->dev, "%s - Can't enable VIN: %d\n", __func__, ret); in trf7970a_power_up()
1894 if (trf->en2_gpiod && in trf7970a_power_up()
1895 !(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW)) { in trf7970a_power_up()
1896 gpiod_set_value_cansleep(trf->en2_gpiod, 1); in trf7970a_power_up()
1900 gpiod_set_value_cansleep(trf->en_gpiod, 1); in trf7970a_power_up()
1904 trf->state = TRF7970A_ST_RF_OFF; in trf7970a_power_up()
1913 dev_dbg(trf->dev, "Powering down - state: %d\n", trf->state); in trf7970a_power_down()
1915 if (trf->state == TRF7970A_ST_PWR_OFF) in trf7970a_power_down()
1918 if (trf->state != TRF7970A_ST_RF_OFF) { in trf7970a_power_down()
1919 dev_dbg(trf->dev, "Can't power down - not RF_OFF state (%d)\n", in trf7970a_power_down()
1920 trf->state); in trf7970a_power_down()
1921 return -EBUSY; in trf7970a_power_down()
1924 gpiod_set_value_cansleep(trf->en_gpiod, 0); in trf7970a_power_down()
1926 if (trf->en2_gpiod && !(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW)) in trf7970a_power_down()
1927 gpiod_set_value_cansleep(trf->en2_gpiod, 0); in trf7970a_power_down()
1929 ret = regulator_disable(trf->regulator); in trf7970a_power_down()
1931 dev_err(trf->dev, "%s - Can't disable VIN: %d\n", __func__, in trf7970a_power_down()
1934 trf->state = TRF7970A_ST_PWR_OFF; in trf7970a_power_down()
1947 pm_runtime_set_active(trf->dev); in trf7970a_startup()
1948 pm_runtime_enable(trf->dev); in trf7970a_startup()
1949 pm_runtime_mark_last_busy(trf->dev); in trf7970a_startup()
1956 switch (trf->state) { in trf7970a_shutdown()
1962 trf7970a_send_err_upstream(trf, -ECANCELED); in trf7970a_shutdown()
1972 pm_runtime_disable(trf->dev); in trf7970a_shutdown()
1973 pm_runtime_set_suspended(trf->dev); in trf7970a_shutdown()
1982 ret = of_property_read_u32(np, "autosuspend-delay", &autosuspend_delay); in trf7970a_get_autosuspend_delay()
1991 const struct device_node *np = spi->dev.of_node; in trf7970a_probe()
1997 dev_err(&spi->dev, "No Device Tree entry\n"); in trf7970a_probe()
1998 return -EINVAL; in trf7970a_probe()
2001 trf = devm_kzalloc(&spi->dev, sizeof(*trf), GFP_KERNEL); in trf7970a_probe()
2003 return -ENOMEM; in trf7970a_probe()
2005 trf->state = TRF7970A_ST_PWR_OFF; in trf7970a_probe()
2006 trf->dev = &spi->dev; in trf7970a_probe()
2007 trf->spi = spi; in trf7970a_probe()
2009 spi->mode = SPI_MODE_1; in trf7970a_probe()
2010 spi->bits_per_word = 8; in trf7970a_probe()
2014 dev_err(trf->dev, "Can't set up SPI Communication\n"); in trf7970a_probe()
2018 if (of_property_read_bool(np, "irq-status-read-quirk")) in trf7970a_probe()
2019 trf->quirks |= TRF7970A_QUIRK_IRQ_STATUS_READ; in trf7970a_probe()
2021 /* There are two enable pins - only EN must be present in the DT */ in trf7970a_probe()
2022 trf->en_gpiod = devm_gpiod_get_index(trf->dev, "ti,enable", 0, in trf7970a_probe()
2024 if (IS_ERR(trf->en_gpiod)) { in trf7970a_probe()
2025 dev_err(trf->dev, "No EN GPIO property\n"); in trf7970a_probe()
2026 return PTR_ERR(trf->en_gpiod); in trf7970a_probe()
2029 trf->en2_gpiod = devm_gpiod_get_index_optional(trf->dev, "ti,enable", 1, in trf7970a_probe()
2031 if (!trf->en2_gpiod) { in trf7970a_probe()
2032 dev_info(trf->dev, "No EN2 GPIO property\n"); in trf7970a_probe()
2033 } else if (IS_ERR(trf->en2_gpiod)) { in trf7970a_probe()
2034 dev_err(trf->dev, "Error getting EN2 GPIO property: %ld\n", in trf7970a_probe()
2035 PTR_ERR(trf->en2_gpiod)); in trf7970a_probe()
2036 return PTR_ERR(trf->en2_gpiod); in trf7970a_probe()
2037 } else if (of_property_read_bool(np, "en2-rf-quirk")) { in trf7970a_probe()
2038 trf->quirks |= TRF7970A_QUIRK_EN2_MUST_STAY_LOW; in trf7970a_probe()
2041 of_property_read_u32(np, "clock-frequency", &clk_freq); in trf7970a_probe()
2044 dev_err(trf->dev, in trf7970a_probe()
2045 "clock-frequency (%u Hz) unsupported\n", clk_freq); in trf7970a_probe()
2046 return -EINVAL; in trf7970a_probe()
2050 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_27MHZ; in trf7970a_probe()
2051 dev_dbg(trf->dev, "trf7970a configured for 27MHz crystal\n"); in trf7970a_probe()
2053 trf->modulator_sys_clk_ctrl = 0; in trf7970a_probe()
2056 ret = devm_request_threaded_irq(trf->dev, spi->irq, NULL, in trf7970a_probe()
2061 dev_err(trf->dev, "Can't request IRQ#%d: %d\n", spi->irq, ret); in trf7970a_probe()
2065 mutex_init(&trf->lock); in trf7970a_probe()
2066 INIT_DELAYED_WORK(&trf->timeout_work, trf7970a_timeout_work_handler); in trf7970a_probe()
2068 trf->regulator = devm_regulator_get(&spi->dev, "vin"); in trf7970a_probe()
2069 if (IS_ERR(trf->regulator)) { in trf7970a_probe()
2070 ret = PTR_ERR(trf->regulator); in trf7970a_probe()
2071 dev_err(trf->dev, "Can't get VIN regulator: %d\n", ret); in trf7970a_probe()
2075 ret = regulator_enable(trf->regulator); in trf7970a_probe()
2077 dev_err(trf->dev, "Can't enable VIN: %d\n", ret); in trf7970a_probe()
2081 uvolts = regulator_get_voltage(trf->regulator); in trf7970a_probe()
2083 trf->chip_status_ctrl = TRF7970A_CHIP_STATUS_VRS5_3; in trf7970a_probe()
2085 trf->regulator = devm_regulator_get(&spi->dev, "vdd-io"); in trf7970a_probe()
2086 if (IS_ERR(trf->regulator)) { in trf7970a_probe()
2087 ret = PTR_ERR(trf->regulator); in trf7970a_probe()
2088 dev_err(trf->dev, "Can't get VDD_IO regulator: %d\n", ret); in trf7970a_probe()
2092 ret = regulator_enable(trf->regulator); in trf7970a_probe()
2094 dev_err(trf->dev, "Can't enable VDD_IO: %d\n", ret); in trf7970a_probe()
2098 if (regulator_get_voltage(trf->regulator) == 1800000) { in trf7970a_probe()
2099 trf->io_ctrl = TRF7970A_REG_IO_CTRL_IO_LOW; in trf7970a_probe()
2100 dev_dbg(trf->dev, "trf7970a config vdd_io to 1.8V\n"); in trf7970a_probe()
2103 trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops, in trf7970a_probe()
2108 if (!trf->ddev) { in trf7970a_probe()
2109 dev_err(trf->dev, "Can't allocate NFC digital device\n"); in trf7970a_probe()
2110 ret = -ENOMEM; in trf7970a_probe()
2114 nfc_digital_set_parent_dev(trf->ddev, trf->dev); in trf7970a_probe()
2115 nfc_digital_set_drvdata(trf->ddev, trf); in trf7970a_probe()
2120 pm_runtime_set_autosuspend_delay(trf->dev, autosuspend_delay); in trf7970a_probe()
2121 pm_runtime_use_autosuspend(trf->dev); in trf7970a_probe()
2127 ret = nfc_digital_register_device(trf->ddev); in trf7970a_probe()
2129 dev_err(trf->dev, "Can't register NFC digital device: %d\n", in trf7970a_probe()
2139 nfc_digital_free_device(trf->ddev); in trf7970a_probe()
2141 regulator_disable(trf->regulator); in trf7970a_probe()
2143 mutex_destroy(&trf->lock); in trf7970a_probe()
2151 mutex_lock(&trf->lock); in trf7970a_remove()
2155 mutex_unlock(&trf->lock); in trf7970a_remove()
2157 nfc_digital_unregister_device(trf->ddev); in trf7970a_remove()
2158 nfc_digital_free_device(trf->ddev); in trf7970a_remove()
2160 regulator_disable(trf->regulator); in trf7970a_remove()
2162 mutex_destroy(&trf->lock); in trf7970a_remove()
2171 mutex_lock(&trf->lock); in trf7970a_suspend()
2175 mutex_unlock(&trf->lock); in trf7970a_suspend()
2186 mutex_lock(&trf->lock); in trf7970a_resume()
2190 mutex_unlock(&trf->lock); in trf7970a_resume()
2203 mutex_lock(&trf->lock); in trf7970a_pm_runtime_suspend()
2207 mutex_unlock(&trf->lock); in trf7970a_pm_runtime_suspend()