aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/nfc
diff options
context:
space:
mode:
authorMark A. Greer <mgreer@animalcreek.com>2014-09-02 18:12:45 -0400
committerSamuel Ortiz <sameo@linux.intel.com>2014-09-07 17:13:45 -0400
commit13b4272a8264220ec043a922fd1fa05da72d57ae (patch)
tree16020e2f0755ed64aa02b74bf9b8b330b4647513 /drivers/nfc
parent1961843ceeca0d3e55744bba7ae8d9e23d04cf6a (diff)
NFC: trf7970a: Add Target Mode Support
Add target mode support to the trf7970a NFC driver. This effectively enables full Peer-to-Peer support. To handle changing framing between sending a response frame and receiving the subsequent request frame, most of the framing changes take effect in the interrupt handler and not in trf7970a_tg_config_framing(). Signed-off-by: Mark A. Greer <mgreer@animalcreek.com> Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
Diffstat (limited to 'drivers/nfc')
-rw-r--r--drivers/nfc/trf7970a.c374
1 files changed, 339 insertions, 35 deletions
diff --git a/drivers/nfc/trf7970a.c b/drivers/nfc/trf7970a.c
index 8b109e15687f..b33cc0211f53 100644
--- a/drivers/nfc/trf7970a.c
+++ b/drivers/nfc/trf7970a.c
@@ -62,7 +62,7 @@
62 * way to abort a command that's already been sent to the tag is so turn 62 * way to abort a command that's already been sent to the tag is so turn
63 * off power to the tag. If we do that, though, we'd have to go through 63 * off power to the tag. If we do that, though, we'd have to go through
64 * the entire anticollision procedure again but the digital layer doesn't 64 * the entire anticollision procedure again but the digital layer doesn't
65 * support that. So, if an abort is received before trf7970a_in_send_cmd() 65 * support that. So, if an abort is received before trf7970a_send_cmd()
66 * has sent the command to the tag, it simply returns -ECANCELED. If the 66 * has sent the command to the tag, it simply returns -ECANCELED. If the
67 * command has already been sent to the tag, then the driver continues 67 * command has already been sent to the tag, then the driver continues
68 * normally and recieves the response data (or error) but just before 68 * normally and recieves the response data (or error) but just before
@@ -121,7 +121,7 @@
121#define TRF7970A_SUPPORTED_PROTOCOLS \ 121#define TRF7970A_SUPPORTED_PROTOCOLS \
122 (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \ 122 (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \
123 NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_FELICA_MASK | \ 123 NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_FELICA_MASK | \
124 NFC_PROTO_ISO15693_MASK) 124 NFC_PROTO_ISO15693_MASK | NFC_PROTO_NFC_DEP_MASK)
125 125
126#define TRF7970A_AUTOSUSPEND_DELAY 30000 /* 30 seconds */ 126#define TRF7970A_AUTOSUSPEND_DELAY 30000 /* 30 seconds */
127 127
@@ -240,6 +240,15 @@
240#define TRF7970A_ISO_CTRL_14443B_848 0x0f 240#define TRF7970A_ISO_CTRL_14443B_848 0x0f
241#define TRF7970A_ISO_CTRL_FELICA_212 0x1a 241#define TRF7970A_ISO_CTRL_FELICA_212 0x1a
242#define TRF7970A_ISO_CTRL_FELICA_424 0x1b 242#define TRF7970A_ISO_CTRL_FELICA_424 0x1b
243#define TRF7970A_ISO_CTRL_NFC_NFCA_106 0x01
244#define TRF7970A_ISO_CTRL_NFC_NFCF_212 0x02
245#define TRF7970A_ISO_CTRL_NFC_NFCF_424 0x03
246#define TRF7970A_ISO_CTRL_NFC_CE_14443A 0x00
247#define TRF7970A_ISO_CTRL_NFC_CE_14443B 0x01
248#define TRF7970A_ISO_CTRL_NFC_CE BIT(2)
249#define TRF7970A_ISO_CTRL_NFC_ACTIVE BIT(3)
250#define TRF7970A_ISO_CTRL_NFC_INITIATOR BIT(4)
251#define TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE BIT(5)
243#define TRF7970A_ISO_CTRL_RFID BIT(5) 252#define TRF7970A_ISO_CTRL_RFID BIT(5)
244#define TRF7970A_ISO_CTRL_DIR_MODE BIT(6) 253#define TRF7970A_ISO_CTRL_DIR_MODE BIT(6)
245#define TRF7970A_ISO_CTRL_RX_CRC_N BIT(7) /* true == No CRC */ 254#define TRF7970A_ISO_CTRL_RX_CRC_N BIT(7) /* true == No CRC */
@@ -265,12 +274,32 @@
265#define TRF7970A_MODULATOR_EN_OOK BIT(6) 274#define TRF7970A_MODULATOR_EN_OOK BIT(6)
266#define TRF7970A_MODULATOR_27MHZ BIT(7) 275#define TRF7970A_MODULATOR_27MHZ BIT(7)
267 276
277#define TRF7970A_RX_SPECIAL_SETTINGS_NO_LIM BIT(0)
278#define TRF7970A_RX_SPECIAL_SETTINGS_AGCR BIT(1)
279#define TRF7970A_RX_SPECIAL_SETTINGS_GD_0DB (0x0 << 2)
280#define TRF7970A_RX_SPECIAL_SETTINGS_GD_5DB (0x1 << 2)
281#define TRF7970A_RX_SPECIAL_SETTINGS_GD_10DB (0x2 << 2)
282#define TRF7970A_RX_SPECIAL_SETTINGS_GD_15DB (0x3 << 2)
283#define TRF7970A_RX_SPECIAL_SETTINGS_HBT BIT(4)
284#define TRF7970A_RX_SPECIAL_SETTINGS_M848 BIT(5)
285#define TRF7970A_RX_SPECIAL_SETTINGS_C424 BIT(6)
286#define TRF7970A_RX_SPECIAL_SETTINGS_C212 BIT(7)
287
288#define TRF7970A_REG_IO_CTRL_VRS(v) ((v) & 0x07)
289#define TRF7970A_REG_IO_CTRL_IO_LOW BIT(5)
290#define TRF7970A_REG_IO_CTRL_EN_EXT_PA BIT(6)
291#define TRF7970A_REG_IO_CTRL_AUTO_REG BIT(7)
292
268/* IRQ Status Register Bits */ 293/* IRQ Status Register Bits */
269#define TRF7970A_IRQ_STATUS_NORESP BIT(0) /* ISO15693 only */ 294#define TRF7970A_IRQ_STATUS_NORESP BIT(0) /* ISO15693 only */
295#define TRF7970A_IRQ_STATUS_NFC_COL_ERROR BIT(0)
270#define TRF7970A_IRQ_STATUS_COL BIT(1) 296#define TRF7970A_IRQ_STATUS_COL BIT(1)
271#define TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR BIT(2) 297#define TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR BIT(2)
298#define TRF7970A_IRQ_STATUS_NFC_RF BIT(2)
272#define TRF7970A_IRQ_STATUS_PARITY_ERROR BIT(3) 299#define TRF7970A_IRQ_STATUS_PARITY_ERROR BIT(3)
300#define TRF7970A_IRQ_STATUS_NFC_SDD BIT(3)
273#define TRF7970A_IRQ_STATUS_CRC_ERROR BIT(4) 301#define TRF7970A_IRQ_STATUS_CRC_ERROR BIT(4)
302#define TRF7970A_IRQ_STATUS_NFC_PROTO_ERROR BIT(4)
274#define TRF7970A_IRQ_STATUS_FIFO BIT(5) 303#define TRF7970A_IRQ_STATUS_FIFO BIT(5)
275#define TRF7970A_IRQ_STATUS_SRX BIT(6) 304#define TRF7970A_IRQ_STATUS_SRX BIT(6)
276#define TRF7970A_IRQ_STATUS_TX BIT(7) 305#define TRF7970A_IRQ_STATUS_TX BIT(7)
@@ -301,6 +330,16 @@
301#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_16 0x2 330#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_16 0x2
302#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32 0x3 331#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32 0x3
303 332
333#define TRF7970A_NFC_LOW_FIELD_LEVEL_RFDET(v) ((v) & 0x07)
334#define TRF7970A_NFC_LOW_FIELD_LEVEL_CLEX_DIS BIT(7)
335
336#define TRF7970A_NFC_TARGET_LEVEL_RFDET(v) ((v) & 0x07)
337#define TRF7970A_NFC_TARGET_LEVEL_HI_RF BIT(3)
338#define TRF7970A_NFC_TARGET_LEVEL_SDD_EN BIT(3)
339#define TRF7970A_NFC_TARGET_LEVEL_LD_S_4BYTES (0x0 << 6)
340#define TRF7970A_NFC_TARGET_LEVEL_LD_S_7BYTES (0x1 << 6)
341#define TRF7970A_NFC_TARGET_LEVEL_LD_S_10BYTES (0x2 << 6)
342
304#define TRF7970A_FIFO_STATUS_OVERFLOW BIT(7) 343#define TRF7970A_FIFO_STATUS_OVERFLOW BIT(7)
305 344
306/* NFC (ISO/IEC 14443A) Type 2 Tag commands */ 345/* NFC (ISO/IEC 14443A) Type 2 Tag commands */
@@ -345,6 +384,7 @@ enum trf7970a_state {
345 TRF7970A_ST_WAIT_FOR_RX_DATA, 384 TRF7970A_ST_WAIT_FOR_RX_DATA,
346 TRF7970A_ST_WAIT_FOR_RX_DATA_CONT, 385 TRF7970A_ST_WAIT_FOR_RX_DATA_CONT,
347 TRF7970A_ST_WAIT_TO_ISSUE_EOF, 386 TRF7970A_ST_WAIT_TO_ISSUE_EOF,
387 TRF7970A_ST_LISTENING,
348 TRF7970A_ST_MAX 388 TRF7970A_ST_MAX
349}; 389};
350 390
@@ -355,6 +395,7 @@ struct trf7970a {
355 struct regulator *regulator; 395 struct regulator *regulator;
356 struct nfc_digital_dev *ddev; 396 struct nfc_digital_dev *ddev;
357 u32 quirks; 397 u32 quirks;
398 bool is_initiator;
358 bool aborting; 399 bool aborting;
359 struct sk_buff *tx_skb; 400 struct sk_buff *tx_skb;
360 struct sk_buff *rx_skb; 401 struct sk_buff *rx_skb;
@@ -694,7 +735,7 @@ static irqreturn_t trf7970a_irq(int irq, void *dev_id)
694{ 735{
695 struct trf7970a *trf = dev_id; 736 struct trf7970a *trf = dev_id;
696 int ret; 737 int ret;
697 u8 status, fifo_bytes; 738 u8 status, fifo_bytes, iso_ctrl;
698 739
699 mutex_lock(&trf->lock); 740 mutex_lock(&trf->lock);
700 741
@@ -720,12 +761,12 @@ static irqreturn_t trf7970a_irq(int irq, void *dev_id)
720 switch (trf->state) { 761 switch (trf->state) {
721 case TRF7970A_ST_IDLE: 762 case TRF7970A_ST_IDLE:
722 case TRF7970A_ST_IDLE_RX_BLOCKED: 763 case TRF7970A_ST_IDLE_RX_BLOCKED:
723 /* If getting interrupts caused by RF noise, turn off the 764 /* If initiator and getting interrupts caused by RF noise,
724 * receiver to avoid unnecessary interrupts. It will be 765 * turn off the receiver to avoid unnecessary interrupts.
725 * turned back on in trf7970a_in_send_cmd() when the next 766 * It will be turned back on in trf7970a_send_cmd() when
726 * command is issued. 767 * the next command is issued.
727 */ 768 */
728 if (status & TRF7970A_IRQ_STATUS_ERROR) { 769 if (trf->is_initiator && (status & TRF7970A_IRQ_STATUS_ERROR)) {
729 trf7970a_cmd(trf, TRF7970A_CMD_BLOCK_RX); 770 trf7970a_cmd(trf, TRF7970A_CMD_BLOCK_RX);
730 trf->state = TRF7970A_ST_IDLE_RX_BLOCKED; 771 trf->state = TRF7970A_ST_IDLE_RX_BLOCKED;
731 } 772 }
@@ -757,7 +798,10 @@ static irqreturn_t trf7970a_irq(int irq, void *dev_id)
757 trf7970a_send_err_upstream(trf, ret); 798 trf7970a_send_err_upstream(trf, ret);
758 else if (!fifo_bytes) 799 else if (!fifo_bytes)
759 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); 800 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
760 } else if (status == TRF7970A_IRQ_STATUS_TX) { 801 } else if ((status == TRF7970A_IRQ_STATUS_TX) ||
802 (!trf->is_initiator &&
803 (status == (TRF7970A_IRQ_STATUS_TX |
804 TRF7970A_IRQ_STATUS_NFC_RF)))) {
761 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); 805 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
762 806
763 if (!trf->timeout) { 807 if (!trf->timeout) {
@@ -767,6 +811,45 @@ static irqreturn_t trf7970a_irq(int irq, void *dev_id)
767 trf7970a_send_upstream(trf); 811 trf7970a_send_upstream(trf);
768 break; 812 break;
769 } 813 }
814
815 if (trf->is_initiator)
816 break;
817
818 iso_ctrl = trf->iso_ctrl;
819
820 switch (trf->framing) {
821 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
822 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
823 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
824 trf->iso_ctrl = 0xff; /* Force ISO_CTRL write */
825 break;
826 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
827 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
828 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
829 trf->iso_ctrl = 0xff; /* Force ISO_CTRL write */
830 break;
831 case NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE:
832 ret = trf7970a_write(trf,
833 TRF7970A_SPECIAL_FCN_REG1,
834 TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL);
835 if (ret)
836 return ret;
837
838 trf->special_fcn_reg1 =
839 TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL;
840 break;
841 default:
842 break;
843 }
844
845 if (iso_ctrl != trf->iso_ctrl) {
846 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL,
847 iso_ctrl);
848 if (ret)
849 return ret;
850
851 trf->iso_ctrl = iso_ctrl;
852 }
770 } else { 853 } else {
771 trf7970a_send_err_upstream(trf, -EIO); 854 trf7970a_send_err_upstream(trf, -EIO);
772 } 855 }
@@ -775,6 +858,15 @@ static irqreturn_t trf7970a_irq(int irq, void *dev_id)
775 if (status != TRF7970A_IRQ_STATUS_TX) 858 if (status != TRF7970A_IRQ_STATUS_TX)
776 trf7970a_send_err_upstream(trf, -EIO); 859 trf7970a_send_err_upstream(trf, -EIO);
777 break; 860 break;
861 case TRF7970A_ST_LISTENING:
862 if (status & TRF7970A_IRQ_STATUS_SRX) {
863 trf->ignore_timeout =
864 !cancel_delayed_work(&trf->timeout_work);
865 trf7970a_drain_fifo(trf, status);
866 } else if (!(status & TRF7970A_IRQ_STATUS_NFC_RF)) {
867 trf7970a_send_err_upstream(trf, -EIO);
868 }
869 break;
778 default: 870 default:
779 dev_err(trf->dev, "%s - Driver in invalid state: %d\n", 871 dev_err(trf->dev, "%s - Driver in invalid state: %d\n",
780 __func__, trf->state); 872 __func__, trf->state);
@@ -853,11 +945,6 @@ static int trf7970a_init(struct trf7970a *trf)
853 945
854 trf->modulator_sys_clk_ctrl = 0; 946 trf->modulator_sys_clk_ctrl = 0;
855 947
856 /* Must clear NFC Target Detection Level reg due to erratum */
857 ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
858 if (ret)
859 goto err_out;
860
861 ret = trf7970a_write(trf, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS, 948 ret = trf7970a_write(trf, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS,
862 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 | 949 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 |
863 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32); 950 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32);
@@ -958,6 +1045,8 @@ static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
958 /* FALLTHROUGH */ 1045 /* FALLTHROUGH */
959 case TRF7970A_ST_IDLE: 1046 case TRF7970A_ST_IDLE:
960 case TRF7970A_ST_IDLE_RX_BLOCKED: 1047 case TRF7970A_ST_IDLE_RX_BLOCKED:
1048 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1049 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
961 trf7970a_switch_rf_off(trf); 1050 trf7970a_switch_rf_off(trf);
962 } 1051 }
963 } 1052 }
@@ -1005,6 +1094,14 @@ static int trf7970a_in_config_rf_tech(struct trf7970a *trf, int tech)
1005 1094
1006 trf->technology = tech; 1095 trf->technology = tech;
1007 1096
1097 /* If in initiator mode and not changing the RF tech due to a
1098 * PSL sequence (indicated by 'trf->iso_ctrl == 0xff' from
1099 * trf7970a_init()), clear the NFC Target Detection Level register
1100 * due to erratum.
1101 */
1102 if (trf->iso_ctrl == 0xff)
1103 ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
1104
1008 return ret; 1105 return ret;
1009} 1106}
1010 1107
@@ -1063,6 +1160,8 @@ static int trf7970a_in_config_framing(struct trf7970a *trf, int framing)
1063 case NFC_DIGITAL_FRAMING_NFCF_T3T: 1160 case NFC_DIGITAL_FRAMING_NFCF_T3T:
1064 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY: 1161 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY:
1065 case NFC_DIGITAL_FRAMING_ISO15693_T5T: 1162 case NFC_DIGITAL_FRAMING_ISO15693_T5T:
1163 case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
1164 case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP:
1066 trf->tx_cmd = TRF7970A_CMD_TRANSMIT; 1165 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1067 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N; 1166 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
1068 break; 1167 break;
@@ -1124,6 +1223,8 @@ static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type,
1124 1223
1125 mutex_lock(&trf->lock); 1224 mutex_lock(&trf->lock);
1126 1225
1226 trf->is_initiator = true;
1227
1127 if ((trf->state == TRF7970A_ST_PWR_OFF) || 1228 if ((trf->state == TRF7970A_ST_PWR_OFF) ||
1128 (trf->state == TRF7970A_ST_RF_OFF)) { 1229 (trf->state == TRF7970A_ST_RF_OFF)) {
1129 ret = trf7970a_switch_rf_on(trf); 1230 ret = trf7970a_switch_rf_on(trf);
@@ -1233,7 +1334,7 @@ static int trf7970a_per_cmd_config(struct trf7970a *trf, struct sk_buff *skb)
1233 return 0; 1334 return 0;
1234} 1335}
1235 1336
1236static int trf7970a_in_send_cmd(struct nfc_digital_dev *ddev, 1337static int trf7970a_send_cmd(struct nfc_digital_dev *ddev,
1237 struct sk_buff *skb, u16 timeout, 1338 struct sk_buff *skb, u16 timeout,
1238 nfc_digital_cmd_complete_t cb, void *arg) 1339 nfc_digital_cmd_complete_t cb, void *arg)
1239{ 1340{
@@ -1284,9 +1385,11 @@ static int trf7970a_in_send_cmd(struct nfc_digital_dev *ddev,
1284 trf->state = TRF7970A_ST_IDLE; 1385 trf->state = TRF7970A_ST_IDLE;
1285 } 1386 }
1286 1387
1287 ret = trf7970a_per_cmd_config(trf, skb); 1388 if (trf->is_initiator) {
1288 if (ret) 1389 ret = trf7970a_per_cmd_config(trf, skb);
1289 goto out_err; 1390 if (ret)
1391 goto out_err;
1392 }
1290 1393
1291 trf->ddev = ddev; 1394 trf->ddev = ddev;
1292 trf->tx_skb = skb; 1395 trf->tx_skb = skb;
@@ -1335,35 +1438,229 @@ out_err:
1335 return ret; 1438 return ret;
1336} 1439}
1337 1440
1338static int trf7970a_tg_configure_hw(struct nfc_digital_dev *ddev, 1441static int trf7970a_tg_config_rf_tech(struct trf7970a *trf, int tech)
1339 int type, int param)
1340{ 1442{
1341 struct trf7970a *trf = nfc_digital_get_drvdata(ddev); 1443 int ret = 0;
1444
1445 dev_dbg(trf->dev, "rf technology: %d\n", tech);
1446
1447 switch (tech) {
1448 case NFC_DIGITAL_RF_TECH_106A:
1449 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
1450 TRF7970A_ISO_CTRL_NFC_CE |
1451 TRF7970A_ISO_CTRL_NFC_CE_14443A;
1452 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
1453 break;
1454 case NFC_DIGITAL_RF_TECH_212F:
1455 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
1456 TRF7970A_ISO_CTRL_NFC_NFCF_212;
1457 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
1458 break;
1459 case NFC_DIGITAL_RF_TECH_424F:
1460 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
1461 TRF7970A_ISO_CTRL_NFC_NFCF_424;
1462 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
1463 break;
1464 default:
1465 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
1466 return -EINVAL;
1467 }
1468
1469 trf->technology = tech;
1342 1470
1343 dev_dbg(trf->dev, "Unsupported interface\n"); 1471 /* Normally we write the ISO_CTRL register in
1472 * trf7970a_tg_config_framing() because the framing can change
1473 * the value written. However, when sending a PSL RES,
1474 * digital_tg_send_psl_res_complete() doesn't call
1475 * trf7970a_tg_config_framing() so we must write the register
1476 * here.
1477 */
1478 if ((trf->framing == NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED) &&
1479 (trf->iso_ctrl_tech != trf->iso_ctrl)) {
1480 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL,
1481 trf->iso_ctrl_tech);
1482
1483 trf->iso_ctrl = trf->iso_ctrl_tech;
1484 }
1344 1485
1345 return -EINVAL; 1486 return ret;
1346} 1487}
1347 1488
1348static int trf7970a_tg_send_cmd(struct nfc_digital_dev *ddev, 1489/* Since this is a target routine, several of the framing calls are
1349 struct sk_buff *skb, u16 timeout, 1490 * made between receiving the request and sending the response so they
1350 nfc_digital_cmd_complete_t cb, void *arg) 1491 * should take effect until after the response is sent. This is accomplished
1492 * by skipping the ISO_CTRL register write here and doing it in the interrupt
1493 * handler.
1494 */
1495static int trf7970a_tg_config_framing(struct trf7970a *trf, int framing)
1496{
1497 u8 iso_ctrl = trf->iso_ctrl_tech;
1498 int ret;
1499
1500 dev_dbg(trf->dev, "framing: %d\n", framing);
1501
1502 switch (framing) {
1503 case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
1504 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
1505 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
1506 break;
1507 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
1508 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
1509 case NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE:
1510 /* These ones are applied in the interrupt handler */
1511 iso_ctrl = trf->iso_ctrl; /* Don't write to ISO_CTRL yet */
1512 break;
1513 case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP:
1514 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1515 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
1516 break;
1517 case NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED:
1518 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1519 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
1520 break;
1521 default:
1522 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
1523 return -EINVAL;
1524 }
1525
1526 trf->framing = framing;
1527
1528 if (iso_ctrl != trf->iso_ctrl) {
1529 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1530 if (ret)
1531 return ret;
1532
1533 trf->iso_ctrl = iso_ctrl;
1534
1535 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
1536 trf->modulator_sys_clk_ctrl);
1537 if (ret)
1538 return ret;
1539 }
1540
1541 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
1542 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1543 trf->chip_status_ctrl |
1544 TRF7970A_CHIP_STATUS_RF_ON);
1545 if (ret)
1546 return ret;
1547
1548 trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
1549 }
1550
1551 return 0;
1552}
1553
1554static int trf7970a_tg_configure_hw(struct nfc_digital_dev *ddev, int type,
1555 int param)
1351{ 1556{
1352 struct trf7970a *trf = nfc_digital_get_drvdata(ddev); 1557 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1558 int ret;
1559
1560 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
1561
1562 mutex_lock(&trf->lock);
1563
1564 trf->is_initiator = false;
1353 1565
1354 dev_dbg(trf->dev, "Unsupported interface\n"); 1566 if ((trf->state == TRF7970A_ST_PWR_OFF) ||
1567 (trf->state == TRF7970A_ST_RF_OFF)) {
1568 ret = trf7970a_switch_rf_on(trf);
1569 if (ret)
1570 goto err_unlock;
1571 }
1572
1573 switch (type) {
1574 case NFC_DIGITAL_CONFIG_RF_TECH:
1575 ret = trf7970a_tg_config_rf_tech(trf, param);
1576 break;
1577 case NFC_DIGITAL_CONFIG_FRAMING:
1578 ret = trf7970a_tg_config_framing(trf, param);
1579 break;
1580 default:
1581 dev_dbg(trf->dev, "Unknown type: %d\n", type);
1582 ret = -EINVAL;
1583 }
1355 1584
1356 return -EINVAL; 1585err_unlock:
1586 mutex_unlock(&trf->lock);
1587 return ret;
1357} 1588}
1358 1589
1359static int trf7970a_tg_listen(struct nfc_digital_dev *ddev, 1590static int trf7970a_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
1360 u16 timeout, nfc_digital_cmd_complete_t cb, void *arg) 1591 nfc_digital_cmd_complete_t cb, void *arg)
1361{ 1592{
1362 struct trf7970a *trf = nfc_digital_get_drvdata(ddev); 1593 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1594 int ret;
1595
1596 dev_dbg(trf->dev, "Listen - state: %d, timeout: %d ms\n",
1597 trf->state, timeout);
1598
1599 mutex_lock(&trf->lock);
1363 1600
1364 dev_dbg(trf->dev, "Unsupported interface\n"); 1601 if ((trf->state != TRF7970A_ST_IDLE) &&
1602 (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) {
1603 dev_err(trf->dev, "%s - Bogus state: %d\n", __func__,
1604 trf->state);
1605 ret = -EIO;
1606 goto out_err;
1607 }
1608
1609 if (trf->aborting) {
1610 dev_dbg(trf->dev, "Abort process complete\n");
1611 trf->aborting = false;
1612 ret = -ECANCELED;
1613 goto out_err;
1614 }
1615
1616 trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE,
1617 GFP_KERNEL);
1618 if (!trf->rx_skb) {
1619 dev_dbg(trf->dev, "Can't alloc rx_skb\n");
1620 ret = -ENOMEM;
1621 goto out_err;
1622 }
1623
1624 ret = trf7970a_write(trf, TRF7970A_RX_SPECIAL_SETTINGS,
1625 TRF7970A_RX_SPECIAL_SETTINGS_HBT |
1626 TRF7970A_RX_SPECIAL_SETTINGS_M848 |
1627 TRF7970A_RX_SPECIAL_SETTINGS_C424 |
1628 TRF7970A_RX_SPECIAL_SETTINGS_C212);
1629 if (ret)
1630 return ret;
1631
1632 ret = trf7970a_write(trf, TRF7970A_REG_IO_CTRL,
1633 TRF7970A_REG_IO_CTRL_VRS(0x1));
1634 if (ret)
1635 return ret;
1365 1636
1366 return -EINVAL; 1637 ret = trf7970a_write(trf, TRF7970A_NFC_LOW_FIELD_LEVEL,
1638 TRF7970A_NFC_LOW_FIELD_LEVEL_RFDET(0x3));
1639 if (ret)
1640 return ret;
1641
1642 ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL,
1643 TRF7970A_NFC_TARGET_LEVEL_RFDET(0x7));
1644 if (ret)
1645 return ret;
1646
1647 trf->ddev = ddev;
1648 trf->cb = cb;
1649 trf->cb_arg = arg;
1650 trf->timeout = timeout;
1651 trf->ignore_timeout = false;
1652
1653 ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX);
1654 if (ret)
1655 goto out_err;
1656
1657 trf->state = TRF7970A_ST_LISTENING;
1658
1659 schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout));
1660
1661out_err:
1662 mutex_unlock(&trf->lock);
1663 return ret;
1367} 1664}
1368 1665
1369static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev) 1666static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev)
@@ -1381,6 +1678,11 @@ static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev)
1381 case TRF7970A_ST_WAIT_TO_ISSUE_EOF: 1678 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
1382 trf->aborting = true; 1679 trf->aborting = true;
1383 break; 1680 break;
1681 case TRF7970A_ST_LISTENING:
1682 trf->ignore_timeout = !cancel_delayed_work(&trf->timeout_work);
1683 trf7970a_send_err_upstream(trf, -ECANCELED);
1684 dev_dbg(trf->dev, "Abort process complete\n");
1685 break;
1384 default: 1686 default:
1385 break; 1687 break;
1386 } 1688 }
@@ -1390,9 +1692,9 @@ static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev)
1390 1692
1391static struct nfc_digital_ops trf7970a_nfc_ops = { 1693static struct nfc_digital_ops trf7970a_nfc_ops = {
1392 .in_configure_hw = trf7970a_in_configure_hw, 1694 .in_configure_hw = trf7970a_in_configure_hw,
1393 .in_send_cmd = trf7970a_in_send_cmd, 1695 .in_send_cmd = trf7970a_send_cmd,
1394 .tg_configure_hw = trf7970a_tg_configure_hw, 1696 .tg_configure_hw = trf7970a_tg_configure_hw,
1395 .tg_send_cmd = trf7970a_tg_send_cmd, 1697 .tg_send_cmd = trf7970a_send_cmd,
1396 .tg_listen = trf7970a_tg_listen, 1698 .tg_listen = trf7970a_tg_listen,
1397 .switch_rf = trf7970a_switch_rf, 1699 .switch_rf = trf7970a_switch_rf,
1398 .abort_cmd = trf7970a_abort_cmd, 1700 .abort_cmd = trf7970a_abort_cmd,
@@ -1479,6 +1781,7 @@ static void trf7970a_shutdown(struct trf7970a *trf)
1479 case TRF7970A_ST_WAIT_FOR_RX_DATA: 1781 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1480 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: 1782 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1481 case TRF7970A_ST_WAIT_TO_ISSUE_EOF: 1783 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
1784 case TRF7970A_ST_LISTENING:
1482 trf7970a_send_err_upstream(trf, -ECANCELED); 1785 trf7970a_send_err_upstream(trf, -ECANCELED);
1483 /* FALLTHROUGH */ 1786 /* FALLTHROUGH */
1484 case TRF7970A_ST_IDLE: 1787 case TRF7970A_ST_IDLE:
@@ -1606,7 +1909,8 @@ static int trf7970a_probe(struct spi_device *spi)
1606 1909
1607 trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops, 1910 trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops,
1608 TRF7970A_SUPPORTED_PROTOCOLS, 1911 TRF7970A_SUPPORTED_PROTOCOLS,
1609 NFC_DIGITAL_DRV_CAPS_IN_CRC, 0, 0); 1912 NFC_DIGITAL_DRV_CAPS_IN_CRC |
1913 NFC_DIGITAL_DRV_CAPS_TG_CRC, 0, 0);
1610 if (!trf->ddev) { 1914 if (!trf->ddev) {
1611 dev_err(trf->dev, "Can't allocate NFC digital device\n"); 1915 dev_err(trf->dev, "Can't allocate NFC digital device\n");
1612 ret = -ENOMEM; 1916 ret = -ENOMEM;