Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * TI TRF7970a RFID/NFC Transceiver Driver |
| 3 | * |
| 4 | * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com |
| 5 | * |
| 6 | * Author: Erick Macias <emacias@ti.com> |
| 7 | * Author: Felipe Balbi <balbi@ti.com> |
| 8 | * Author: Mark A. Greer <mgreer@animalcreek.com> |
| 9 | * |
| 10 | * This program is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 of |
| 12 | * the License as published by the Free Software Foundation. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/netdevice.h> |
| 18 | #include <linux/interrupt.h> |
Mark A. Greer | e6403b7 | 2014-03-25 08:54:38 -0700 | [diff] [blame] | 19 | #include <linux/pm_runtime.h> |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 20 | #include <linux/nfc.h> |
| 21 | #include <linux/skbuff.h> |
| 22 | #include <linux/delay.h> |
Mark Greer | d34e48d | 2017-04-25 15:43:55 -0700 | [diff] [blame] | 23 | #include <linux/gpio/consumer.h> |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 24 | #include <linux/of.h> |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 25 | #include <linux/spi/spi.h> |
| 26 | #include <linux/regulator/consumer.h> |
| 27 | |
| 28 | #include <net/nfc/nfc.h> |
| 29 | #include <net/nfc/digital.h> |
| 30 | |
| 31 | /* There are 3 ways the host can communicate with the trf7970a: |
| 32 | * parallel mode, SPI with Slave Select (SS) mode, and SPI without |
| 33 | * SS mode. The driver only supports the two SPI modes. |
| 34 | * |
| 35 | * The trf7970a is very timing sensitive and the VIN, EN2, and EN |
| 36 | * pins must asserted in that order and with specific delays in between. |
| 37 | * The delays used in the driver were provided by TI and have been |
Mark A. Greer | 95064bd | 2014-08-07 17:41:45 -0700 | [diff] [blame] | 38 | * confirmed to work with this driver. There is a bug with the current |
| 39 | * version of the trf7970a that requires that EN2 remain low no matter |
| 40 | * what. If it goes high, it will generate an RF field even when in |
| 41 | * passive target mode. TI has indicated that the chip will work okay |
| 42 | * when EN2 is left low. The 'en2-rf-quirk' device tree property |
| 43 | * indicates that trf7970a currently being used has the erratum and |
| 44 | * that EN2 must be kept low. |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 45 | * |
| 46 | * Timeouts are implemented using the delayed workqueue kernel facility. |
| 47 | * Timeouts are required so things don't hang when there is no response |
| 48 | * from the trf7970a (or tag). Using this mechanism creates a race with |
| 49 | * interrupts, however. That is, an interrupt and a timeout could occur |
| 50 | * closely enough together that one is blocked by the mutex while the other |
| 51 | * executes. When the timeout handler executes first and blocks the |
| 52 | * interrupt handler, it will eventually set the state to IDLE so the |
| 53 | * interrupt handler will check the state and exit with no harm done. |
| 54 | * When the interrupt handler executes first and blocks the timeout handler, |
| 55 | * the cancel_delayed_work() call will know that it didn't cancel the |
| 56 | * work item (i.e., timeout) and will return zero. That return code is |
| 57 | * used by the timer handler to indicate that it should ignore the timeout |
| 58 | * once its unblocked. |
| 59 | * |
| 60 | * Aborting an active command isn't as simple as it seems because the only |
| 61 | * way to abort a command that's already been sent to the tag is so turn |
| 62 | * off power to the tag. If we do that, though, we'd have to go through |
| 63 | * the entire anticollision procedure again but the digital layer doesn't |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 64 | * support that. So, if an abort is received before trf7970a_send_cmd() |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 65 | * has sent the command to the tag, it simply returns -ECANCELED. If the |
| 66 | * command has already been sent to the tag, then the driver continues |
| 67 | * normally and recieves the response data (or error) but just before |
| 68 | * sending the data upstream, it frees the rx_skb and sends -ECANCELED |
| 69 | * upstream instead. If the command failed, that error will be sent |
| 70 | * upstream. |
| 71 | * |
| 72 | * When recieving data from a tag and the interrupt status register has |
| 73 | * only the SRX bit set, it means that all of the data has been received |
| 74 | * (once what's in the fifo has been read). However, depending on timing |
| 75 | * an interrupt status with only the SRX bit set may not be recived. In |
Mark A. Greer | 5fa3af3 | 2014-03-25 08:54:29 -0700 | [diff] [blame] | 76 | * those cases, the timeout mechanism is used to wait 20 ms in case more |
| 77 | * data arrives. After 20 ms, it is assumed that all of the data has been |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 78 | * received and the accumulated rx data is sent upstream. The |
| 79 | * 'TRF7970A_ST_WAIT_FOR_RX_DATA_CONT' state is used for this purpose |
| 80 | * (i.e., it indicates that some data has been received but we're not sure |
| 81 | * if there is more coming so a timeout in this state means all data has |
Mark A. Greer | 5fa3af3 | 2014-03-25 08:54:29 -0700 | [diff] [blame] | 82 | * been received and there isn't an error). The delay is 20 ms since delays |
| 83 | * of ~16 ms have been observed during testing. |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 84 | * |
Mark A. Greer | 38b4eb1 | 2014-09-02 15:12:25 -0700 | [diff] [blame] | 85 | * When transmitting a frame larger than the FIFO size (127 bytes), the |
| 86 | * driver will wait 20 ms for the FIFO to drain past the low-watermark |
| 87 | * and generate an interrupt. The low-watermark set to 32 bytes so the |
| 88 | * interrupt should fire after 127 - 32 = 95 bytes have been sent. At |
| 89 | * the lowest possible bit rate (6.62 kbps for 15693), it will take up |
| 90 | * to ~14.35 ms so 20 ms is used for the timeout. |
| 91 | * |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 92 | * Type 2 write and sector select commands respond with a 4-bit ACK or NACK. |
| 93 | * Having only 4 bits in the FIFO won't normally generate an interrupt so |
| 94 | * driver enables the '4_bit_RX' bit of the Special Functions register 1 |
| 95 | * to cause an interrupt in that case. Leaving that bit for a read command |
| 96 | * messes up the data returned so it is only enabled when the framing is |
| 97 | * 'NFC_DIGITAL_FRAMING_NFCA_T2T' and the command is not a read command. |
| 98 | * Unfortunately, that means that the driver has to peek into tx frames |
| 99 | * when the framing is 'NFC_DIGITAL_FRAMING_NFCA_T2T'. This is done by |
| 100 | * the trf7970a_per_cmd_config() routine. |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 101 | * |
| 102 | * ISO/IEC 15693 frames specify whether to use single or double sub-carrier |
| 103 | * frequencies and whether to use low or high data rates in the flags byte |
| 104 | * of the frame. This means that the driver has to peek at all 15693 frames |
| 105 | * to determine what speed to set the communication to. In addition, write |
| 106 | * and lock commands use the OPTION flag to indicate that an EOF must be |
| 107 | * sent to the tag before it will send its response. So the driver has to |
| 108 | * examine all frames for that reason too. |
| 109 | * |
| 110 | * It is unclear how long to wait before sending the EOF. According to the |
| 111 | * Note under Table 1-1 in section 1.6 of |
| 112 | * http://www.ti.com/lit/ug/scbu011/scbu011.pdf, that wait should be at least |
| 113 | * 10 ms for TI Tag-it HF-I tags; however testing has shown that is not long |
Mark A. Greer | 38b4eb1 | 2014-09-02 15:12:25 -0700 | [diff] [blame] | 114 | * enough so 20 ms is used. So the timer is set to 40 ms - 20 ms to drain |
| 115 | * up to 127 bytes in the FIFO at the lowest bit rate plus another 20 ms to |
| 116 | * ensure the wait is long enough before sending the EOF. This seems to work |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 117 | * reliably. |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 118 | */ |
| 119 | |
Mark A. Greer | 8006289 | 2014-03-10 11:56:23 -0700 | [diff] [blame] | 120 | #define TRF7970A_SUPPORTED_PROTOCOLS \ |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 121 | (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \ |
Mark A. Greer | 6857bb9 | 2014-04-14 10:04:10 -0700 | [diff] [blame] | 122 | NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_FELICA_MASK | \ |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 123 | NFC_PROTO_ISO15693_MASK | NFC_PROTO_NFC_DEP_MASK) |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 124 | |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 125 | #define TRF7970A_AUTOSUSPEND_DELAY 30000 /* 30 seconds */ |
Geoff Lansberry | 837eb4d | 2016-12-21 23:18:32 -0500 | [diff] [blame] | 126 | #define TRF7970A_13MHZ_CLOCK_FREQUENCY 13560000 |
| 127 | #define TRF7970A_27MHZ_CLOCK_FREQUENCY 27120000 |
| 128 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 129 | #define TRF7970A_RX_SKB_ALLOC_SIZE 256 |
| 130 | |
Mark A. Greer | 1568bfe | 2014-09-02 15:12:20 -0700 | [diff] [blame] | 131 | #define TRF7970A_FIFO_SIZE 127 |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 132 | |
| 133 | /* TX length is 3 nibbles long ==> 4KB - 1 bytes max */ |
| 134 | #define TRF7970A_TX_MAX (4096 - 1) |
| 135 | |
Mark A. Greer | 1961843 | 2014-09-02 15:12:44 -0700 | [diff] [blame] | 136 | #define TRF7970A_WAIT_FOR_TX_IRQ 20 |
Mark A. Greer | 5fa3af3 | 2014-03-25 08:54:29 -0700 | [diff] [blame] | 137 | #define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 20 |
Mark A. Greer | 38b4eb1 | 2014-09-02 15:12:25 -0700 | [diff] [blame] | 138 | #define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 20 |
| 139 | #define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 40 |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 140 | |
Mark A. Greer | 4e64eff | 2014-09-02 15:12:24 -0700 | [diff] [blame] | 141 | /* Guard times for various RF technologies (in us) */ |
| 142 | #define TRF7970A_GUARD_TIME_NFCA 5000 |
| 143 | #define TRF7970A_GUARD_TIME_NFCB 5000 |
| 144 | #define TRF7970A_GUARD_TIME_NFCF 20000 |
| 145 | #define TRF7970A_GUARD_TIME_15693 1000 |
| 146 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 147 | /* Quirks */ |
| 148 | /* Erratum: When reading IRQ Status register on trf7970a, we must issue a |
| 149 | * read continuous command for IRQ Status and Collision Position registers. |
| 150 | */ |
Mark A. Greer | 772079e | 2014-08-07 17:41:43 -0700 | [diff] [blame] | 151 | #define TRF7970A_QUIRK_IRQ_STATUS_READ BIT(0) |
Mark A. Greer | 95064bd | 2014-08-07 17:41:45 -0700 | [diff] [blame] | 152 | #define TRF7970A_QUIRK_EN2_MUST_STAY_LOW BIT(1) |
Mark A. Greer | ab71481 | 2015-04-24 10:37:22 -0700 | [diff] [blame] | 153 | #define TRF7970A_QUIRK_T5T_RMB_EXTRA_BYTE BIT(2) |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 154 | |
| 155 | /* Direct commands */ |
| 156 | #define TRF7970A_CMD_IDLE 0x00 |
| 157 | #define TRF7970A_CMD_SOFT_INIT 0x03 |
| 158 | #define TRF7970A_CMD_RF_COLLISION 0x04 |
| 159 | #define TRF7970A_CMD_RF_COLLISION_RESPONSE_N 0x05 |
| 160 | #define TRF7970A_CMD_RF_COLLISION_RESPONSE_0 0x06 |
| 161 | #define TRF7970A_CMD_FIFO_RESET 0x0f |
| 162 | #define TRF7970A_CMD_TRANSMIT_NO_CRC 0x10 |
| 163 | #define TRF7970A_CMD_TRANSMIT 0x11 |
| 164 | #define TRF7970A_CMD_DELAY_TRANSMIT_NO_CRC 0x12 |
| 165 | #define TRF7970A_CMD_DELAY_TRANSMIT 0x13 |
| 166 | #define TRF7970A_CMD_EOF 0x14 |
| 167 | #define TRF7970A_CMD_CLOSE_SLOT 0x15 |
| 168 | #define TRF7970A_CMD_BLOCK_RX 0x16 |
| 169 | #define TRF7970A_CMD_ENABLE_RX 0x17 |
Mark A. Greer | 851ee3c | 2014-09-02 15:12:42 -0700 | [diff] [blame] | 170 | #define TRF7970A_CMD_TEST_INT_RF 0x18 |
| 171 | #define TRF7970A_CMD_TEST_EXT_RF 0x19 |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 172 | #define TRF7970A_CMD_RX_GAIN_ADJUST 0x1a |
| 173 | |
| 174 | /* Bits determining whether its a direct command or register R/W, |
| 175 | * whether to use a continuous SPI transaction or not, and the actual |
| 176 | * direct cmd opcode or regster address. |
| 177 | */ |
| 178 | #define TRF7970A_CMD_BIT_CTRL BIT(7) |
| 179 | #define TRF7970A_CMD_BIT_RW BIT(6) |
| 180 | #define TRF7970A_CMD_BIT_CONTINUOUS BIT(5) |
| 181 | #define TRF7970A_CMD_BIT_OPCODE(opcode) ((opcode) & 0x1f) |
| 182 | |
| 183 | /* Registers addresses */ |
| 184 | #define TRF7970A_CHIP_STATUS_CTRL 0x00 |
| 185 | #define TRF7970A_ISO_CTRL 0x01 |
| 186 | #define TRF7970A_ISO14443B_TX_OPTIONS 0x02 |
| 187 | #define TRF7970A_ISO14443A_HIGH_BITRATE_OPTIONS 0x03 |
| 188 | #define TRF7970A_TX_TIMER_SETTING_H_BYTE 0x04 |
| 189 | #define TRF7970A_TX_TIMER_SETTING_L_BYTE 0x05 |
| 190 | #define TRF7970A_TX_PULSE_LENGTH_CTRL 0x06 |
| 191 | #define TRF7970A_RX_NO_RESPONSE_WAIT 0x07 |
| 192 | #define TRF7970A_RX_WAIT_TIME 0x08 |
| 193 | #define TRF7970A_MODULATOR_SYS_CLK_CTRL 0x09 |
| 194 | #define TRF7970A_RX_SPECIAL_SETTINGS 0x0a |
| 195 | #define TRF7970A_REG_IO_CTRL 0x0b |
| 196 | #define TRF7970A_IRQ_STATUS 0x0c |
| 197 | #define TRF7970A_COLLISION_IRQ_MASK 0x0d |
| 198 | #define TRF7970A_COLLISION_POSITION 0x0e |
| 199 | #define TRF7970A_RSSI_OSC_STATUS 0x0f |
| 200 | #define TRF7970A_SPECIAL_FCN_REG1 0x10 |
| 201 | #define TRF7970A_SPECIAL_FCN_REG2 0x11 |
| 202 | #define TRF7970A_RAM1 0x12 |
| 203 | #define TRF7970A_RAM2 0x13 |
| 204 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS 0x14 |
| 205 | #define TRF7970A_NFC_LOW_FIELD_LEVEL 0x16 |
| 206 | #define TRF7970A_NFCID1 0x17 |
| 207 | #define TRF7970A_NFC_TARGET_LEVEL 0x18 |
| 208 | #define TRF79070A_NFC_TARGET_PROTOCOL 0x19 |
| 209 | #define TRF7970A_TEST_REGISTER1 0x1a |
| 210 | #define TRF7970A_TEST_REGISTER2 0x1b |
| 211 | #define TRF7970A_FIFO_STATUS 0x1c |
| 212 | #define TRF7970A_TX_LENGTH_BYTE1 0x1d |
| 213 | #define TRF7970A_TX_LENGTH_BYTE2 0x1e |
| 214 | #define TRF7970A_FIFO_IO_REGISTER 0x1f |
| 215 | |
| 216 | /* Chip Status Control Register Bits */ |
| 217 | #define TRF7970A_CHIP_STATUS_VRS5_3 BIT(0) |
| 218 | #define TRF7970A_CHIP_STATUS_REC_ON BIT(1) |
| 219 | #define TRF7970A_CHIP_STATUS_AGC_ON BIT(2) |
| 220 | #define TRF7970A_CHIP_STATUS_PM_ON BIT(3) |
| 221 | #define TRF7970A_CHIP_STATUS_RF_PWR BIT(4) |
| 222 | #define TRF7970A_CHIP_STATUS_RF_ON BIT(5) |
| 223 | #define TRF7970A_CHIP_STATUS_DIRECT BIT(6) |
| 224 | #define TRF7970A_CHIP_STATUS_STBY BIT(7) |
| 225 | |
| 226 | /* ISO Control Register Bits */ |
| 227 | #define TRF7970A_ISO_CTRL_15693_SGL_1OF4_662 0x00 |
| 228 | #define TRF7970A_ISO_CTRL_15693_SGL_1OF256_662 0x01 |
| 229 | #define TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648 0x02 |
| 230 | #define TRF7970A_ISO_CTRL_15693_SGL_1OF256_2648 0x03 |
| 231 | #define TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a 0x04 |
| 232 | #define TRF7970A_ISO_CTRL_15693_DBL_1OF256_667 0x05 |
| 233 | #define TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669 0x06 |
| 234 | #define TRF7970A_ISO_CTRL_15693_DBL_1OF256_2669 0x07 |
| 235 | #define TRF7970A_ISO_CTRL_14443A_106 0x08 |
| 236 | #define TRF7970A_ISO_CTRL_14443A_212 0x09 |
| 237 | #define TRF7970A_ISO_CTRL_14443A_424 0x0a |
| 238 | #define TRF7970A_ISO_CTRL_14443A_848 0x0b |
| 239 | #define TRF7970A_ISO_CTRL_14443B_106 0x0c |
| 240 | #define TRF7970A_ISO_CTRL_14443B_212 0x0d |
| 241 | #define TRF7970A_ISO_CTRL_14443B_424 0x0e |
| 242 | #define TRF7970A_ISO_CTRL_14443B_848 0x0f |
| 243 | #define TRF7970A_ISO_CTRL_FELICA_212 0x1a |
| 244 | #define TRF7970A_ISO_CTRL_FELICA_424 0x1b |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 245 | #define TRF7970A_ISO_CTRL_NFC_NFCA_106 0x01 |
| 246 | #define TRF7970A_ISO_CTRL_NFC_NFCF_212 0x02 |
| 247 | #define TRF7970A_ISO_CTRL_NFC_NFCF_424 0x03 |
| 248 | #define TRF7970A_ISO_CTRL_NFC_CE_14443A 0x00 |
| 249 | #define TRF7970A_ISO_CTRL_NFC_CE_14443B 0x01 |
| 250 | #define TRF7970A_ISO_CTRL_NFC_CE BIT(2) |
| 251 | #define TRF7970A_ISO_CTRL_NFC_ACTIVE BIT(3) |
| 252 | #define TRF7970A_ISO_CTRL_NFC_INITIATOR BIT(4) |
| 253 | #define TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE BIT(5) |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 254 | #define TRF7970A_ISO_CTRL_RFID BIT(5) |
| 255 | #define TRF7970A_ISO_CTRL_DIR_MODE BIT(6) |
| 256 | #define TRF7970A_ISO_CTRL_RX_CRC_N BIT(7) /* true == No CRC */ |
| 257 | |
| 258 | #define TRF7970A_ISO_CTRL_RFID_SPEED_MASK 0x1f |
| 259 | |
| 260 | /* Modulator and SYS_CLK Control Register Bits */ |
| 261 | #define TRF7970A_MODULATOR_DEPTH(n) ((n) & 0x7) |
| 262 | #define TRF7970A_MODULATOR_DEPTH_ASK10 (TRF7970A_MODULATOR_DEPTH(0)) |
| 263 | #define TRF7970A_MODULATOR_DEPTH_OOK (TRF7970A_MODULATOR_DEPTH(1)) |
| 264 | #define TRF7970A_MODULATOR_DEPTH_ASK7 (TRF7970A_MODULATOR_DEPTH(2)) |
| 265 | #define TRF7970A_MODULATOR_DEPTH_ASK8_5 (TRF7970A_MODULATOR_DEPTH(3)) |
| 266 | #define TRF7970A_MODULATOR_DEPTH_ASK13 (TRF7970A_MODULATOR_DEPTH(4)) |
| 267 | #define TRF7970A_MODULATOR_DEPTH_ASK16 (TRF7970A_MODULATOR_DEPTH(5)) |
| 268 | #define TRF7970A_MODULATOR_DEPTH_ASK22 (TRF7970A_MODULATOR_DEPTH(6)) |
| 269 | #define TRF7970A_MODULATOR_DEPTH_ASK30 (TRF7970A_MODULATOR_DEPTH(7)) |
| 270 | #define TRF7970A_MODULATOR_EN_ANA BIT(3) |
| 271 | #define TRF7970A_MODULATOR_CLK(n) (((n) & 0x3) << 4) |
| 272 | #define TRF7970A_MODULATOR_CLK_DISABLED (TRF7970A_MODULATOR_CLK(0)) |
| 273 | #define TRF7970A_MODULATOR_CLK_3_6 (TRF7970A_MODULATOR_CLK(1)) |
| 274 | #define TRF7970A_MODULATOR_CLK_6_13 (TRF7970A_MODULATOR_CLK(2)) |
| 275 | #define TRF7970A_MODULATOR_CLK_13_27 (TRF7970A_MODULATOR_CLK(3)) |
| 276 | #define TRF7970A_MODULATOR_EN_OOK BIT(6) |
| 277 | #define TRF7970A_MODULATOR_27MHZ BIT(7) |
| 278 | |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 279 | #define TRF7970A_RX_SPECIAL_SETTINGS_NO_LIM BIT(0) |
| 280 | #define TRF7970A_RX_SPECIAL_SETTINGS_AGCR BIT(1) |
| 281 | #define TRF7970A_RX_SPECIAL_SETTINGS_GD_0DB (0x0 << 2) |
| 282 | #define TRF7970A_RX_SPECIAL_SETTINGS_GD_5DB (0x1 << 2) |
| 283 | #define TRF7970A_RX_SPECIAL_SETTINGS_GD_10DB (0x2 << 2) |
| 284 | #define TRF7970A_RX_SPECIAL_SETTINGS_GD_15DB (0x3 << 2) |
| 285 | #define TRF7970A_RX_SPECIAL_SETTINGS_HBT BIT(4) |
| 286 | #define TRF7970A_RX_SPECIAL_SETTINGS_M848 BIT(5) |
| 287 | #define TRF7970A_RX_SPECIAL_SETTINGS_C424 BIT(6) |
| 288 | #define TRF7970A_RX_SPECIAL_SETTINGS_C212 BIT(7) |
| 289 | |
| 290 | #define TRF7970A_REG_IO_CTRL_VRS(v) ((v) & 0x07) |
| 291 | #define TRF7970A_REG_IO_CTRL_IO_LOW BIT(5) |
| 292 | #define TRF7970A_REG_IO_CTRL_EN_EXT_PA BIT(6) |
| 293 | #define TRF7970A_REG_IO_CTRL_AUTO_REG BIT(7) |
| 294 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 295 | /* IRQ Status Register Bits */ |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 296 | #define TRF7970A_IRQ_STATUS_NORESP BIT(0) /* ISO15693 only */ |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 297 | #define TRF7970A_IRQ_STATUS_NFC_COL_ERROR BIT(0) |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 298 | #define TRF7970A_IRQ_STATUS_COL BIT(1) |
| 299 | #define TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR BIT(2) |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 300 | #define TRF7970A_IRQ_STATUS_NFC_RF BIT(2) |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 301 | #define TRF7970A_IRQ_STATUS_PARITY_ERROR BIT(3) |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 302 | #define TRF7970A_IRQ_STATUS_NFC_SDD BIT(3) |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 303 | #define TRF7970A_IRQ_STATUS_CRC_ERROR BIT(4) |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 304 | #define TRF7970A_IRQ_STATUS_NFC_PROTO_ERROR BIT(4) |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 305 | #define TRF7970A_IRQ_STATUS_FIFO BIT(5) |
| 306 | #define TRF7970A_IRQ_STATUS_SRX BIT(6) |
| 307 | #define TRF7970A_IRQ_STATUS_TX BIT(7) |
| 308 | |
| 309 | #define TRF7970A_IRQ_STATUS_ERROR \ |
| 310 | (TRF7970A_IRQ_STATUS_COL | \ |
| 311 | TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR | \ |
| 312 | TRF7970A_IRQ_STATUS_PARITY_ERROR | \ |
| 313 | TRF7970A_IRQ_STATUS_CRC_ERROR) |
| 314 | |
Mark A. Greer | 851ee3c | 2014-09-02 15:12:42 -0700 | [diff] [blame] | 315 | #define TRF7970A_RSSI_OSC_STATUS_RSSI_MASK (BIT(2) | BIT(1) | BIT(0)) |
| 316 | #define TRF7970A_RSSI_OSC_STATUS_RSSI_X_MASK (BIT(5) | BIT(4) | BIT(3)) |
| 317 | #define TRF7970A_RSSI_OSC_STATUS_RSSI_OSC_OK BIT(6) |
| 318 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 319 | #define TRF7970A_SPECIAL_FCN_REG1_COL_7_6 BIT(0) |
| 320 | #define TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL BIT(1) |
| 321 | #define TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX BIT(2) |
| 322 | #define TRF7970A_SPECIAL_FCN_REG1_SP_DIR_MODE BIT(3) |
| 323 | #define TRF7970A_SPECIAL_FCN_REG1_NEXT_SLOT_37US BIT(4) |
| 324 | #define TRF7970A_SPECIAL_FCN_REG1_PAR43 BIT(5) |
| 325 | |
| 326 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_124 (0x0 << 2) |
| 327 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_120 (0x1 << 2) |
| 328 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_112 (0x2 << 2) |
| 329 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 (0x3 << 2) |
| 330 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_4 0x0 |
| 331 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_8 0x1 |
| 332 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_16 0x2 |
| 333 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32 0x3 |
| 334 | |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 335 | #define TRF7970A_NFC_LOW_FIELD_LEVEL_RFDET(v) ((v) & 0x07) |
| 336 | #define TRF7970A_NFC_LOW_FIELD_LEVEL_CLEX_DIS BIT(7) |
| 337 | |
| 338 | #define TRF7970A_NFC_TARGET_LEVEL_RFDET(v) ((v) & 0x07) |
| 339 | #define TRF7970A_NFC_TARGET_LEVEL_HI_RF BIT(3) |
Mark Greer | ae291f7 | 2015-08-19 08:57:58 -0700 | [diff] [blame] | 340 | #define TRF7970A_NFC_TARGET_LEVEL_SDD_EN BIT(5) |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 341 | #define TRF7970A_NFC_TARGET_LEVEL_LD_S_4BYTES (0x0 << 6) |
| 342 | #define TRF7970A_NFC_TARGET_LEVEL_LD_S_7BYTES (0x1 << 6) |
| 343 | #define TRF7970A_NFC_TARGET_LEVEL_LD_S_10BYTES (0x2 << 6) |
| 344 | |
Mark A. Greer | cb174ab | 2014-09-02 15:12:46 -0700 | [diff] [blame] | 345 | #define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106 BIT(0) |
| 346 | #define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_212 BIT(1) |
| 347 | #define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_424 (BIT(0) | BIT(1)) |
| 348 | #define TRF79070A_NFC_TARGET_PROTOCOL_PAS_14443B BIT(2) |
| 349 | #define TRF79070A_NFC_TARGET_PROTOCOL_PAS_106 BIT(3) |
| 350 | #define TRF79070A_NFC_TARGET_PROTOCOL_FELICA BIT(4) |
| 351 | #define TRF79070A_NFC_TARGET_PROTOCOL_RF_L BIT(6) |
| 352 | #define TRF79070A_NFC_TARGET_PROTOCOL_RF_H BIT(7) |
| 353 | |
| 354 | #define TRF79070A_NFC_TARGET_PROTOCOL_106A \ |
| 355 | (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \ |
| 356 | TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \ |
| 357 | TRF79070A_NFC_TARGET_PROTOCOL_PAS_106 | \ |
| 358 | TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106) |
| 359 | |
| 360 | #define TRF79070A_NFC_TARGET_PROTOCOL_106B \ |
| 361 | (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \ |
| 362 | TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \ |
| 363 | TRF79070A_NFC_TARGET_PROTOCOL_PAS_14443B | \ |
| 364 | TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106) |
| 365 | |
| 366 | #define TRF79070A_NFC_TARGET_PROTOCOL_212F \ |
| 367 | (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \ |
| 368 | TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \ |
| 369 | TRF79070A_NFC_TARGET_PROTOCOL_FELICA | \ |
| 370 | TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_212) |
| 371 | |
| 372 | #define TRF79070A_NFC_TARGET_PROTOCOL_424F \ |
| 373 | (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \ |
| 374 | TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \ |
| 375 | TRF79070A_NFC_TARGET_PROTOCOL_FELICA | \ |
| 376 | TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_424) |
| 377 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 378 | #define TRF7970A_FIFO_STATUS_OVERFLOW BIT(7) |
| 379 | |
| 380 | /* NFC (ISO/IEC 14443A) Type 2 Tag commands */ |
| 381 | #define NFC_T2T_CMD_READ 0x30 |
| 382 | |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 383 | /* ISO 15693 commands codes */ |
| 384 | #define ISO15693_CMD_INVENTORY 0x01 |
| 385 | #define ISO15693_CMD_READ_SINGLE_BLOCK 0x20 |
| 386 | #define ISO15693_CMD_WRITE_SINGLE_BLOCK 0x21 |
| 387 | #define ISO15693_CMD_LOCK_BLOCK 0x22 |
| 388 | #define ISO15693_CMD_READ_MULTIPLE_BLOCK 0x23 |
| 389 | #define ISO15693_CMD_WRITE_MULTIPLE_BLOCK 0x24 |
| 390 | #define ISO15693_CMD_SELECT 0x25 |
| 391 | #define ISO15693_CMD_RESET_TO_READY 0x26 |
| 392 | #define ISO15693_CMD_WRITE_AFI 0x27 |
| 393 | #define ISO15693_CMD_LOCK_AFI 0x28 |
| 394 | #define ISO15693_CMD_WRITE_DSFID 0x29 |
| 395 | #define ISO15693_CMD_LOCK_DSFID 0x2a |
| 396 | #define ISO15693_CMD_GET_SYSTEM_INFO 0x2b |
| 397 | #define ISO15693_CMD_GET_MULTIPLE_BLOCK_SECURITY_STATUS 0x2c |
| 398 | |
| 399 | /* ISO 15693 request and response flags */ |
| 400 | #define ISO15693_REQ_FLAG_SUB_CARRIER BIT(0) |
| 401 | #define ISO15693_REQ_FLAG_DATA_RATE BIT(1) |
| 402 | #define ISO15693_REQ_FLAG_INVENTORY BIT(2) |
| 403 | #define ISO15693_REQ_FLAG_PROTOCOL_EXT BIT(3) |
| 404 | #define ISO15693_REQ_FLAG_SELECT BIT(4) |
| 405 | #define ISO15693_REQ_FLAG_AFI BIT(4) |
| 406 | #define ISO15693_REQ_FLAG_ADDRESS BIT(5) |
| 407 | #define ISO15693_REQ_FLAG_NB_SLOTS BIT(5) |
| 408 | #define ISO15693_REQ_FLAG_OPTION BIT(6) |
| 409 | |
| 410 | #define ISO15693_REQ_FLAG_SPEED_MASK \ |
| 411 | (ISO15693_REQ_FLAG_SUB_CARRIER | ISO15693_REQ_FLAG_DATA_RATE) |
| 412 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 413 | enum trf7970a_state { |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 414 | TRF7970A_ST_PWR_OFF, |
Mark A. Greer | b5e17d9 | 2014-09-02 15:12:34 -0700 | [diff] [blame] | 415 | TRF7970A_ST_RF_OFF, |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 416 | TRF7970A_ST_IDLE, |
| 417 | TRF7970A_ST_IDLE_RX_BLOCKED, |
| 418 | TRF7970A_ST_WAIT_FOR_TX_FIFO, |
| 419 | TRF7970A_ST_WAIT_FOR_RX_DATA, |
| 420 | TRF7970A_ST_WAIT_FOR_RX_DATA_CONT, |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 421 | TRF7970A_ST_WAIT_TO_ISSUE_EOF, |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 422 | TRF7970A_ST_LISTENING, |
Mark A. Greer | cb174ab | 2014-09-02 15:12:46 -0700 | [diff] [blame] | 423 | TRF7970A_ST_LISTENING_MD, |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 424 | TRF7970A_ST_MAX |
| 425 | }; |
| 426 | |
| 427 | struct trf7970a { |
| 428 | enum trf7970a_state state; |
| 429 | struct device *dev; |
| 430 | struct spi_device *spi; |
| 431 | struct regulator *regulator; |
| 432 | struct nfc_digital_dev *ddev; |
| 433 | u32 quirks; |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 434 | bool is_initiator; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 435 | bool aborting; |
| 436 | struct sk_buff *tx_skb; |
| 437 | struct sk_buff *rx_skb; |
| 438 | nfc_digital_cmd_complete_t cb; |
| 439 | void *cb_arg; |
Mark A. Greer | ebcc5a0 | 2014-03-25 08:54:36 -0700 | [diff] [blame] | 440 | u8 chip_status_ctrl; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 441 | u8 iso_ctrl; |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 442 | u8 iso_ctrl_tech; |
Mark A. Greer | 12e9ade | 2014-03-25 08:54:35 -0700 | [diff] [blame] | 443 | u8 modulator_sys_clk_ctrl; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 444 | u8 special_fcn_reg1; |
Geoff Lansberry | 49d22c7 | 2016-12-21 23:18:33 -0500 | [diff] [blame] | 445 | u8 io_ctrl; |
Mark A. Greer | 4e64eff | 2014-09-02 15:12:24 -0700 | [diff] [blame] | 446 | unsigned int guard_time; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 447 | int technology; |
| 448 | int framing; |
Mark A. Greer | cb174ab | 2014-09-02 15:12:46 -0700 | [diff] [blame] | 449 | u8 md_rf_tech; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 450 | u8 tx_cmd; |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 451 | bool issue_eof; |
Mark A. Greer | ab71481 | 2015-04-24 10:37:22 -0700 | [diff] [blame] | 452 | bool adjust_resp_len; |
Mark Greer | d34e48d | 2017-04-25 15:43:55 -0700 | [diff] [blame] | 453 | struct gpio_desc *en_gpiod; |
| 454 | struct gpio_desc *en2_gpiod; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 455 | struct mutex lock; |
| 456 | unsigned int timeout; |
| 457 | bool ignore_timeout; |
| 458 | struct delayed_work timeout_work; |
| 459 | }; |
| 460 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 461 | static int trf7970a_cmd(struct trf7970a *trf, u8 opcode) |
| 462 | { |
| 463 | u8 cmd = TRF7970A_CMD_BIT_CTRL | TRF7970A_CMD_BIT_OPCODE(opcode); |
| 464 | int ret; |
| 465 | |
| 466 | dev_dbg(trf->dev, "cmd: 0x%x\n", cmd); |
| 467 | |
| 468 | ret = spi_write(trf->spi, &cmd, 1); |
| 469 | if (ret) |
| 470 | dev_err(trf->dev, "%s - cmd: 0x%x, ret: %d\n", __func__, cmd, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 471 | ret); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 472 | return ret; |
| 473 | } |
| 474 | |
| 475 | static int trf7970a_read(struct trf7970a *trf, u8 reg, u8 *val) |
| 476 | { |
| 477 | u8 addr = TRF7970A_CMD_BIT_RW | reg; |
| 478 | int ret; |
| 479 | |
| 480 | ret = spi_write_then_read(trf->spi, &addr, 1, val, 1); |
| 481 | if (ret) |
| 482 | dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 483 | ret); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 484 | |
| 485 | dev_dbg(trf->dev, "read(0x%x): 0x%x\n", addr, *val); |
| 486 | |
| 487 | return ret; |
| 488 | } |
| 489 | |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 490 | static int trf7970a_read_cont(struct trf7970a *trf, u8 reg, u8 *buf, |
| 491 | size_t len) |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 492 | { |
| 493 | u8 addr = reg | TRF7970A_CMD_BIT_RW | TRF7970A_CMD_BIT_CONTINUOUS; |
Mark A. Greer | 3e7f335 | 2014-09-02 15:12:18 -0700 | [diff] [blame] | 494 | struct spi_transfer t[2]; |
| 495 | struct spi_message m; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 496 | int ret; |
| 497 | |
| 498 | dev_dbg(trf->dev, "read_cont(0x%x, %zd)\n", addr, len); |
| 499 | |
Mark A. Greer | 3e7f335 | 2014-09-02 15:12:18 -0700 | [diff] [blame] | 500 | spi_message_init(&m); |
| 501 | |
| 502 | memset(&t, 0, sizeof(t)); |
| 503 | |
| 504 | t[0].tx_buf = &addr; |
| 505 | t[0].len = sizeof(addr); |
| 506 | spi_message_add_tail(&t[0], &m); |
| 507 | |
| 508 | t[1].rx_buf = buf; |
| 509 | t[1].len = len; |
| 510 | spi_message_add_tail(&t[1], &m); |
| 511 | |
| 512 | ret = spi_sync(trf->spi, &m); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 513 | if (ret) |
| 514 | dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 515 | ret); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 516 | return ret; |
| 517 | } |
| 518 | |
| 519 | static int trf7970a_write(struct trf7970a *trf, u8 reg, u8 val) |
| 520 | { |
| 521 | u8 buf[2] = { reg, val }; |
| 522 | int ret; |
| 523 | |
| 524 | dev_dbg(trf->dev, "write(0x%x): 0x%x\n", reg, val); |
| 525 | |
| 526 | ret = spi_write(trf->spi, buf, 2); |
| 527 | if (ret) |
| 528 | dev_err(trf->dev, "%s - write: 0x%x 0x%x, ret: %d\n", __func__, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 529 | buf[0], buf[1], ret); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 530 | |
| 531 | return ret; |
| 532 | } |
| 533 | |
| 534 | static int trf7970a_read_irqstatus(struct trf7970a *trf, u8 *status) |
| 535 | { |
| 536 | int ret; |
| 537 | u8 buf[2]; |
| 538 | u8 addr; |
| 539 | |
| 540 | addr = TRF7970A_IRQ_STATUS | TRF7970A_CMD_BIT_RW; |
| 541 | |
Mark A. Greer | 772079e | 2014-08-07 17:41:43 -0700 | [diff] [blame] | 542 | if (trf->quirks & TRF7970A_QUIRK_IRQ_STATUS_READ) { |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 543 | addr |= TRF7970A_CMD_BIT_CONTINUOUS; |
| 544 | ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2); |
| 545 | } else { |
| 546 | ret = spi_write_then_read(trf->spi, &addr, 1, buf, 1); |
| 547 | } |
| 548 | |
| 549 | if (ret) |
| 550 | dev_err(trf->dev, "%s - irqstatus: Status read failed: %d\n", |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 551 | __func__, ret); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 552 | else |
| 553 | *status = buf[0]; |
| 554 | |
| 555 | return ret; |
| 556 | } |
| 557 | |
Mark A. Greer | cb174ab | 2014-09-02 15:12:46 -0700 | [diff] [blame] | 558 | static int trf7970a_read_target_proto(struct trf7970a *trf, u8 *target_proto) |
| 559 | { |
| 560 | int ret; |
| 561 | u8 buf[2]; |
| 562 | u8 addr; |
| 563 | |
| 564 | addr = TRF79070A_NFC_TARGET_PROTOCOL | TRF7970A_CMD_BIT_RW | |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 565 | TRF7970A_CMD_BIT_CONTINUOUS; |
Mark A. Greer | cb174ab | 2014-09-02 15:12:46 -0700 | [diff] [blame] | 566 | |
| 567 | ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2); |
| 568 | if (ret) |
| 569 | dev_err(trf->dev, "%s - target_proto: Read failed: %d\n", |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 570 | __func__, ret); |
Mark A. Greer | cb174ab | 2014-09-02 15:12:46 -0700 | [diff] [blame] | 571 | else |
| 572 | *target_proto = buf[0]; |
| 573 | |
| 574 | return ret; |
| 575 | } |
| 576 | |
| 577 | static int trf7970a_mode_detect(struct trf7970a *trf, u8 *rf_tech) |
| 578 | { |
| 579 | int ret; |
| 580 | u8 target_proto, tech; |
| 581 | |
| 582 | ret = trf7970a_read_target_proto(trf, &target_proto); |
| 583 | if (ret) |
| 584 | return ret; |
| 585 | |
| 586 | switch (target_proto) { |
| 587 | case TRF79070A_NFC_TARGET_PROTOCOL_106A: |
| 588 | tech = NFC_DIGITAL_RF_TECH_106A; |
| 589 | break; |
| 590 | case TRF79070A_NFC_TARGET_PROTOCOL_106B: |
| 591 | tech = NFC_DIGITAL_RF_TECH_106B; |
| 592 | break; |
| 593 | case TRF79070A_NFC_TARGET_PROTOCOL_212F: |
| 594 | tech = NFC_DIGITAL_RF_TECH_212F; |
| 595 | break; |
| 596 | case TRF79070A_NFC_TARGET_PROTOCOL_424F: |
| 597 | tech = NFC_DIGITAL_RF_TECH_424F; |
| 598 | break; |
| 599 | default: |
| 600 | dev_dbg(trf->dev, "%s - mode_detect: target_proto: 0x%x\n", |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 601 | __func__, target_proto); |
Mark A. Greer | cb174ab | 2014-09-02 15:12:46 -0700 | [diff] [blame] | 602 | return -EIO; |
| 603 | } |
| 604 | |
| 605 | *rf_tech = tech; |
| 606 | |
| 607 | return ret; |
| 608 | } |
| 609 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 610 | static void trf7970a_send_upstream(struct trf7970a *trf) |
| 611 | { |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 612 | dev_kfree_skb_any(trf->tx_skb); |
| 613 | trf->tx_skb = NULL; |
| 614 | |
| 615 | if (trf->rx_skb && !IS_ERR(trf->rx_skb) && !trf->aborting) |
| 616 | print_hex_dump_debug("trf7970a rx data: ", DUMP_PREFIX_NONE, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 617 | 16, 1, trf->rx_skb->data, trf->rx_skb->len, |
| 618 | false); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 619 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 620 | trf->state = TRF7970A_ST_IDLE; |
| 621 | |
| 622 | if (trf->aborting) { |
| 623 | dev_dbg(trf->dev, "Abort process complete\n"); |
| 624 | |
| 625 | if (!IS_ERR(trf->rx_skb)) { |
| 626 | kfree_skb(trf->rx_skb); |
| 627 | trf->rx_skb = ERR_PTR(-ECANCELED); |
| 628 | } |
| 629 | |
| 630 | trf->aborting = false; |
| 631 | } |
| 632 | |
Mark A. Greer | ab71481 | 2015-04-24 10:37:22 -0700 | [diff] [blame] | 633 | if (trf->adjust_resp_len) { |
Mark Greer | aaee24a | 2015-08-19 08:58:52 -0700 | [diff] [blame] | 634 | if (trf->rx_skb) |
| 635 | skb_trim(trf->rx_skb, trf->rx_skb->len - 1); |
| 636 | |
Mark A. Greer | ab71481 | 2015-04-24 10:37:22 -0700 | [diff] [blame] | 637 | trf->adjust_resp_len = false; |
| 638 | } |
| 639 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 640 | trf->cb(trf->ddev, trf->cb_arg, trf->rx_skb); |
| 641 | |
| 642 | trf->rx_skb = NULL; |
| 643 | } |
| 644 | |
| 645 | static void trf7970a_send_err_upstream(struct trf7970a *trf, int errno) |
| 646 | { |
| 647 | dev_dbg(trf->dev, "Error - state: %d, errno: %d\n", trf->state, errno); |
| 648 | |
Mark A. Greer | 6fb9edc | 2014-09-02 15:12:43 -0700 | [diff] [blame] | 649 | cancel_delayed_work(&trf->timeout_work); |
| 650 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 651 | kfree_skb(trf->rx_skb); |
| 652 | trf->rx_skb = ERR_PTR(errno); |
| 653 | |
| 654 | trf7970a_send_upstream(trf); |
| 655 | } |
| 656 | |
| 657 | static int trf7970a_transmit(struct trf7970a *trf, struct sk_buff *skb, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 658 | unsigned int len, u8 *prefix, |
| 659 | unsigned int prefix_len) |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 660 | { |
Mark A. Greer | 7a1e555 | 2014-09-02 15:12:28 -0700 | [diff] [blame] | 661 | struct spi_transfer t[2]; |
| 662 | struct spi_message m; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 663 | unsigned int timeout; |
| 664 | int ret; |
| 665 | |
| 666 | print_hex_dump_debug("trf7970a tx data: ", DUMP_PREFIX_NONE, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 667 | 16, 1, skb->data, len, false); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 668 | |
Mark A. Greer | 7a1e555 | 2014-09-02 15:12:28 -0700 | [diff] [blame] | 669 | spi_message_init(&m); |
| 670 | |
| 671 | memset(&t, 0, sizeof(t)); |
| 672 | |
| 673 | t[0].tx_buf = prefix; |
| 674 | t[0].len = prefix_len; |
| 675 | spi_message_add_tail(&t[0], &m); |
| 676 | |
| 677 | t[1].tx_buf = skb->data; |
| 678 | t[1].len = len; |
| 679 | spi_message_add_tail(&t[1], &m); |
| 680 | |
| 681 | ret = spi_sync(trf->spi, &m); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 682 | if (ret) { |
| 683 | dev_err(trf->dev, "%s - Can't send tx data: %d\n", __func__, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 684 | ret); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 685 | return ret; |
| 686 | } |
| 687 | |
| 688 | skb_pull(skb, len); |
| 689 | |
| 690 | if (skb->len > 0) { |
| 691 | trf->state = TRF7970A_ST_WAIT_FOR_TX_FIFO; |
| 692 | timeout = TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT; |
| 693 | } else { |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 694 | if (trf->issue_eof) { |
| 695 | trf->state = TRF7970A_ST_WAIT_TO_ISSUE_EOF; |
| 696 | timeout = TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF; |
| 697 | } else { |
| 698 | trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA; |
Mark A. Greer | 1961843 | 2014-09-02 15:12:44 -0700 | [diff] [blame] | 699 | |
| 700 | if (!trf->timeout) |
| 701 | timeout = TRF7970A_WAIT_FOR_TX_IRQ; |
| 702 | else |
| 703 | timeout = trf->timeout; |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 704 | } |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", timeout, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 708 | trf->state); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 709 | |
| 710 | schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout)); |
| 711 | |
| 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | static void trf7970a_fill_fifo(struct trf7970a *trf) |
| 716 | { |
| 717 | struct sk_buff *skb = trf->tx_skb; |
| 718 | unsigned int len; |
| 719 | int ret; |
| 720 | u8 fifo_bytes; |
Mark A. Greer | 7a1e555 | 2014-09-02 15:12:28 -0700 | [diff] [blame] | 721 | u8 prefix; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 722 | |
| 723 | ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes); |
| 724 | if (ret) { |
| 725 | trf7970a_send_err_upstream(trf, ret); |
| 726 | return; |
| 727 | } |
| 728 | |
| 729 | dev_dbg(trf->dev, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes); |
| 730 | |
Mark A. Greer | 4542e83 | 2014-09-02 15:12:19 -0700 | [diff] [blame] | 731 | fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 732 | |
| 733 | /* Calculate how much more data can be written to the fifo */ |
| 734 | len = TRF7970A_FIFO_SIZE - fifo_bytes; |
Mark A. Greer | 0e840ed | 2014-09-02 15:12:29 -0700 | [diff] [blame] | 735 | if (!len) { |
| 736 | schedule_delayed_work(&trf->timeout_work, |
| 737 | msecs_to_jiffies(TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT)); |
| 738 | return; |
| 739 | } |
| 740 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 741 | len = min(skb->len, len); |
| 742 | |
Mark A. Greer | 7a1e555 | 2014-09-02 15:12:28 -0700 | [diff] [blame] | 743 | prefix = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_FIFO_IO_REGISTER; |
| 744 | |
| 745 | ret = trf7970a_transmit(trf, skb, len, &prefix, sizeof(prefix)); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 746 | if (ret) |
| 747 | trf7970a_send_err_upstream(trf, ret); |
| 748 | } |
| 749 | |
| 750 | static void trf7970a_drain_fifo(struct trf7970a *trf, u8 status) |
| 751 | { |
| 752 | struct sk_buff *skb = trf->rx_skb; |
| 753 | int ret; |
| 754 | u8 fifo_bytes; |
| 755 | |
| 756 | if (status & TRF7970A_IRQ_STATUS_ERROR) { |
| 757 | trf7970a_send_err_upstream(trf, -EIO); |
| 758 | return; |
| 759 | } |
| 760 | |
| 761 | ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes); |
| 762 | if (ret) { |
| 763 | trf7970a_send_err_upstream(trf, ret); |
| 764 | return; |
| 765 | } |
| 766 | |
| 767 | dev_dbg(trf->dev, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes); |
| 768 | |
Mark A. Greer | 4542e83 | 2014-09-02 15:12:19 -0700 | [diff] [blame] | 769 | fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW; |
| 770 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 771 | if (!fifo_bytes) |
| 772 | goto no_rx_data; |
| 773 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 774 | if (fifo_bytes > skb_tailroom(skb)) { |
| 775 | skb = skb_copy_expand(skb, skb_headroom(skb), |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 776 | max_t(int, fifo_bytes, |
| 777 | TRF7970A_RX_SKB_ALLOC_SIZE), |
| 778 | GFP_KERNEL); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 779 | if (!skb) { |
| 780 | trf7970a_send_err_upstream(trf, -ENOMEM); |
| 781 | return; |
| 782 | } |
| 783 | |
| 784 | kfree_skb(trf->rx_skb); |
| 785 | trf->rx_skb = skb; |
| 786 | } |
| 787 | |
| 788 | ret = trf7970a_read_cont(trf, TRF7970A_FIFO_IO_REGISTER, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 789 | skb_put(skb, fifo_bytes), fifo_bytes); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 790 | if (ret) { |
| 791 | trf7970a_send_err_upstream(trf, ret); |
| 792 | return; |
| 793 | } |
| 794 | |
| 795 | /* If received Type 2 ACK/NACK, shift right 4 bits and pass up */ |
| 796 | if ((trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T) && (skb->len == 1) && |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 797 | (trf->special_fcn_reg1 == TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX)) { |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 798 | skb->data[0] >>= 4; |
| 799 | status = TRF7970A_IRQ_STATUS_SRX; |
| 800 | } else { |
| 801 | trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA_CONT; |
Mark A. Greer | 5d8f759 | 2014-09-02 15:12:31 -0700 | [diff] [blame] | 802 | |
| 803 | ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes); |
| 804 | if (ret) { |
| 805 | trf7970a_send_err_upstream(trf, ret); |
| 806 | return; |
| 807 | } |
| 808 | |
| 809 | fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW; |
| 810 | |
| 811 | /* If there are bytes in the FIFO, set status to '0' so |
| 812 | * the if stmt below doesn't fire and the driver will wait |
| 813 | * for the trf7970a to generate another RX interrupt. |
| 814 | */ |
| 815 | if (fifo_bytes) |
| 816 | status = 0; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | no_rx_data: |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 820 | if (status == TRF7970A_IRQ_STATUS_SRX) { /* Receive complete */ |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 821 | trf7970a_send_upstream(trf); |
| 822 | return; |
| 823 | } |
| 824 | |
| 825 | dev_dbg(trf->dev, "Setting timeout for %d ms\n", |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 826 | TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 827 | |
| 828 | schedule_delayed_work(&trf->timeout_work, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 829 | msecs_to_jiffies(TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT)); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | static irqreturn_t trf7970a_irq(int irq, void *dev_id) |
| 833 | { |
| 834 | struct trf7970a *trf = dev_id; |
| 835 | int ret; |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 836 | u8 status, fifo_bytes, iso_ctrl; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 837 | |
| 838 | mutex_lock(&trf->lock); |
| 839 | |
Mark A. Greer | b5e17d9 | 2014-09-02 15:12:34 -0700 | [diff] [blame] | 840 | if (trf->state == TRF7970A_ST_RF_OFF) { |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 841 | mutex_unlock(&trf->lock); |
| 842 | return IRQ_NONE; |
| 843 | } |
| 844 | |
| 845 | ret = trf7970a_read_irqstatus(trf, &status); |
| 846 | if (ret) { |
| 847 | mutex_unlock(&trf->lock); |
| 848 | return IRQ_NONE; |
| 849 | } |
| 850 | |
| 851 | dev_dbg(trf->dev, "IRQ - state: %d, status: 0x%x\n", trf->state, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 852 | status); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 853 | |
| 854 | if (!status) { |
| 855 | mutex_unlock(&trf->lock); |
| 856 | return IRQ_NONE; |
| 857 | } |
| 858 | |
| 859 | switch (trf->state) { |
| 860 | case TRF7970A_ST_IDLE: |
| 861 | case TRF7970A_ST_IDLE_RX_BLOCKED: |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 862 | /* If initiator and getting interrupts caused by RF noise, |
| 863 | * turn off the receiver to avoid unnecessary interrupts. |
| 864 | * It will be turned back on in trf7970a_send_cmd() when |
| 865 | * the next command is issued. |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 866 | */ |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 867 | if (trf->is_initiator && (status & TRF7970A_IRQ_STATUS_ERROR)) { |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 868 | trf7970a_cmd(trf, TRF7970A_CMD_BLOCK_RX); |
| 869 | trf->state = TRF7970A_ST_IDLE_RX_BLOCKED; |
| 870 | } |
| 871 | |
| 872 | trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); |
| 873 | break; |
| 874 | case TRF7970A_ST_WAIT_FOR_TX_FIFO: |
| 875 | if (status & TRF7970A_IRQ_STATUS_TX) { |
| 876 | trf->ignore_timeout = |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 877 | !cancel_delayed_work(&trf->timeout_work); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 878 | trf7970a_fill_fifo(trf); |
| 879 | } else { |
| 880 | trf7970a_send_err_upstream(trf, -EIO); |
| 881 | } |
| 882 | break; |
| 883 | case TRF7970A_ST_WAIT_FOR_RX_DATA: |
| 884 | case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: |
| 885 | if (status & TRF7970A_IRQ_STATUS_SRX) { |
| 886 | trf->ignore_timeout = |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 887 | !cancel_delayed_work(&trf->timeout_work); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 888 | trf7970a_drain_fifo(trf, status); |
Mark A. Greer | bece3c5 | 2014-09-02 15:12:30 -0700 | [diff] [blame] | 889 | } else if (status & TRF7970A_IRQ_STATUS_FIFO) { |
| 890 | ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 891 | &fifo_bytes); |
Mark A. Greer | bece3c5 | 2014-09-02 15:12:30 -0700 | [diff] [blame] | 892 | |
| 893 | fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW; |
| 894 | |
| 895 | if (ret) |
| 896 | trf7970a_send_err_upstream(trf, ret); |
| 897 | else if (!fifo_bytes) |
| 898 | trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 899 | } else if ((status == TRF7970A_IRQ_STATUS_TX) || |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 900 | (!trf->is_initiator && |
| 901 | (status == (TRF7970A_IRQ_STATUS_TX | |
| 902 | TRF7970A_IRQ_STATUS_NFC_RF)))) { |
Mark A. Greer | 4dd836e | 2014-03-25 08:54:32 -0700 | [diff] [blame] | 903 | trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); |
Mark A. Greer | 1961843 | 2014-09-02 15:12:44 -0700 | [diff] [blame] | 904 | |
| 905 | if (!trf->timeout) { |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 906 | trf->ignore_timeout = |
| 907 | !cancel_delayed_work(&trf->timeout_work); |
Mark A. Greer | 1961843 | 2014-09-02 15:12:44 -0700 | [diff] [blame] | 908 | trf->rx_skb = ERR_PTR(0); |
| 909 | trf7970a_send_upstream(trf); |
| 910 | break; |
| 911 | } |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 912 | |
| 913 | if (trf->is_initiator) |
| 914 | break; |
| 915 | |
| 916 | iso_ctrl = trf->iso_ctrl; |
| 917 | |
| 918 | switch (trf->framing) { |
| 919 | case NFC_DIGITAL_FRAMING_NFCA_STANDARD: |
| 920 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC; |
| 921 | iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N; |
| 922 | trf->iso_ctrl = 0xff; /* Force ISO_CTRL write */ |
| 923 | break; |
| 924 | case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A: |
| 925 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT; |
| 926 | iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N; |
| 927 | trf->iso_ctrl = 0xff; /* Force ISO_CTRL write */ |
| 928 | break; |
| 929 | case NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE: |
| 930 | ret = trf7970a_write(trf, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 931 | TRF7970A_SPECIAL_FCN_REG1, |
| 932 | TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL); |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 933 | if (ret) |
Mark A. Greer | b9e3016 | 2014-09-23 11:25:47 -0700 | [diff] [blame] | 934 | goto err_unlock_exit; |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 935 | |
| 936 | trf->special_fcn_reg1 = |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 937 | TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL; |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 938 | break; |
| 939 | default: |
| 940 | break; |
| 941 | } |
| 942 | |
| 943 | if (iso_ctrl != trf->iso_ctrl) { |
| 944 | ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 945 | iso_ctrl); |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 946 | if (ret) |
Mark A. Greer | b9e3016 | 2014-09-23 11:25:47 -0700 | [diff] [blame] | 947 | goto err_unlock_exit; |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 948 | |
| 949 | trf->iso_ctrl = iso_ctrl; |
| 950 | } |
Mark A. Greer | 4dd836e | 2014-03-25 08:54:32 -0700 | [diff] [blame] | 951 | } else { |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 952 | trf7970a_send_err_upstream(trf, -EIO); |
| 953 | } |
| 954 | break; |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 955 | case TRF7970A_ST_WAIT_TO_ISSUE_EOF: |
| 956 | if (status != TRF7970A_IRQ_STATUS_TX) |
| 957 | trf7970a_send_err_upstream(trf, -EIO); |
| 958 | break; |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 959 | case TRF7970A_ST_LISTENING: |
| 960 | if (status & TRF7970A_IRQ_STATUS_SRX) { |
| 961 | trf->ignore_timeout = |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 962 | !cancel_delayed_work(&trf->timeout_work); |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 963 | trf7970a_drain_fifo(trf, status); |
| 964 | } else if (!(status & TRF7970A_IRQ_STATUS_NFC_RF)) { |
| 965 | trf7970a_send_err_upstream(trf, -EIO); |
| 966 | } |
| 967 | break; |
Mark A. Greer | cb174ab | 2014-09-02 15:12:46 -0700 | [diff] [blame] | 968 | case TRF7970A_ST_LISTENING_MD: |
| 969 | if (status & TRF7970A_IRQ_STATUS_SRX) { |
| 970 | trf->ignore_timeout = |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 971 | !cancel_delayed_work(&trf->timeout_work); |
Mark A. Greer | cb174ab | 2014-09-02 15:12:46 -0700 | [diff] [blame] | 972 | |
| 973 | ret = trf7970a_mode_detect(trf, &trf->md_rf_tech); |
| 974 | if (ret) { |
| 975 | trf7970a_send_err_upstream(trf, ret); |
| 976 | } else { |
| 977 | trf->state = TRF7970A_ST_LISTENING; |
| 978 | trf7970a_drain_fifo(trf, status); |
| 979 | } |
| 980 | } else if (!(status & TRF7970A_IRQ_STATUS_NFC_RF)) { |
| 981 | trf7970a_send_err_upstream(trf, -EIO); |
| 982 | } |
| 983 | break; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 984 | default: |
| 985 | dev_err(trf->dev, "%s - Driver in invalid state: %d\n", |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 986 | __func__, trf->state); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 987 | } |
| 988 | |
Mark A. Greer | b9e3016 | 2014-09-23 11:25:47 -0700 | [diff] [blame] | 989 | err_unlock_exit: |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 990 | mutex_unlock(&trf->lock); |
| 991 | return IRQ_HANDLED; |
| 992 | } |
| 993 | |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 994 | static void trf7970a_issue_eof(struct trf7970a *trf) |
| 995 | { |
| 996 | int ret; |
| 997 | |
| 998 | dev_dbg(trf->dev, "Issuing EOF\n"); |
| 999 | |
| 1000 | ret = trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); |
| 1001 | if (ret) |
| 1002 | trf7970a_send_err_upstream(trf, ret); |
| 1003 | |
| 1004 | ret = trf7970a_cmd(trf, TRF7970A_CMD_EOF); |
| 1005 | if (ret) |
| 1006 | trf7970a_send_err_upstream(trf, ret); |
| 1007 | |
| 1008 | trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA; |
| 1009 | |
| 1010 | dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1011 | trf->timeout, trf->state); |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1012 | |
| 1013 | schedule_delayed_work(&trf->timeout_work, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1014 | msecs_to_jiffies(trf->timeout)); |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1017 | static void trf7970a_timeout_work_handler(struct work_struct *work) |
| 1018 | { |
| 1019 | struct trf7970a *trf = container_of(work, struct trf7970a, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1020 | timeout_work.work); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1021 | |
| 1022 | dev_dbg(trf->dev, "Timeout - state: %d, ignore_timeout: %d\n", |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1023 | trf->state, trf->ignore_timeout); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1024 | |
| 1025 | mutex_lock(&trf->lock); |
| 1026 | |
| 1027 | if (trf->ignore_timeout) |
| 1028 | trf->ignore_timeout = false; |
| 1029 | else if (trf->state == TRF7970A_ST_WAIT_FOR_RX_DATA_CONT) |
Mark A. Greer | afa5b5f | 2014-09-02 15:12:32 -0700 | [diff] [blame] | 1030 | trf7970a_drain_fifo(trf, TRF7970A_IRQ_STATUS_SRX); |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1031 | else if (trf->state == TRF7970A_ST_WAIT_TO_ISSUE_EOF) |
| 1032 | trf7970a_issue_eof(trf); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1033 | else |
| 1034 | trf7970a_send_err_upstream(trf, -ETIMEDOUT); |
| 1035 | |
| 1036 | mutex_unlock(&trf->lock); |
| 1037 | } |
| 1038 | |
| 1039 | static int trf7970a_init(struct trf7970a *trf) |
| 1040 | { |
| 1041 | int ret; |
| 1042 | |
| 1043 | dev_dbg(trf->dev, "Initializing device - state: %d\n", trf->state); |
| 1044 | |
| 1045 | ret = trf7970a_cmd(trf, TRF7970A_CMD_SOFT_INIT); |
| 1046 | if (ret) |
| 1047 | goto err_out; |
| 1048 | |
| 1049 | ret = trf7970a_cmd(trf, TRF7970A_CMD_IDLE); |
| 1050 | if (ret) |
| 1051 | goto err_out; |
| 1052 | |
Geoff Lansberry | 49d22c7 | 2016-12-21 23:18:33 -0500 | [diff] [blame] | 1053 | ret = trf7970a_write(trf, TRF7970A_REG_IO_CTRL, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1054 | trf->io_ctrl | TRF7970A_REG_IO_CTRL_VRS(0x1)); |
Geoff Lansberry | 49d22c7 | 2016-12-21 23:18:33 -0500 | [diff] [blame] | 1055 | if (ret) |
| 1056 | goto err_out; |
| 1057 | |
Geoff Lansberry | 58d46f5 | 2016-04-18 15:48:39 -0400 | [diff] [blame] | 1058 | ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0); |
| 1059 | if (ret) |
| 1060 | goto err_out; |
| 1061 | |
Mark A. Greer | 4e007f8 | 2014-09-02 15:12:38 -0700 | [diff] [blame] | 1062 | usleep_range(1000, 2000); |
| 1063 | |
Mark A. Greer | 7149d6b | 2014-09-02 15:12:41 -0700 | [diff] [blame] | 1064 | trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON; |
| 1065 | |
Geoff Lansberry | 837eb4d | 2016-12-21 23:18:32 -0500 | [diff] [blame] | 1066 | ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1067 | trf->modulator_sys_clk_ctrl); |
Mark A. Greer | 6c08df4 | 2014-09-02 15:12:22 -0700 | [diff] [blame] | 1068 | if (ret) |
| 1069 | goto err_out; |
| 1070 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1071 | ret = trf7970a_write(trf, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1072 | TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 | |
| 1073 | TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1074 | if (ret) |
| 1075 | goto err_out; |
| 1076 | |
| 1077 | ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, 0); |
| 1078 | if (ret) |
| 1079 | goto err_out; |
| 1080 | |
| 1081 | trf->special_fcn_reg1 = 0; |
| 1082 | |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 1083 | trf->iso_ctrl = 0xff; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1084 | return 0; |
| 1085 | |
| 1086 | err_out: |
| 1087 | dev_dbg(trf->dev, "Couldn't init device: %d\n", ret); |
| 1088 | return ret; |
| 1089 | } |
| 1090 | |
| 1091 | static void trf7970a_switch_rf_off(struct trf7970a *trf) |
| 1092 | { |
Mark A. Greer | cfc708d | 2014-09-02 15:12:40 -0700 | [diff] [blame] | 1093 | if ((trf->state == TRF7970A_ST_PWR_OFF) || |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1094 | (trf->state == TRF7970A_ST_RF_OFF)) |
Mark A. Greer | cfc708d | 2014-09-02 15:12:40 -0700 | [diff] [blame] | 1095 | return; |
| 1096 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1097 | dev_dbg(trf->dev, "Switching rf off\n"); |
| 1098 | |
Mark A. Greer | a1d2dc5 | 2014-03-25 08:54:37 -0700 | [diff] [blame] | 1099 | trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON; |
| 1100 | |
| 1101 | trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, trf->chip_status_ctrl); |
| 1102 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1103 | trf->aborting = false; |
Mark A. Greer | b5e17d9 | 2014-09-02 15:12:34 -0700 | [diff] [blame] | 1104 | trf->state = TRF7970A_ST_RF_OFF; |
Mark A. Greer | e6403b7 | 2014-03-25 08:54:38 -0700 | [diff] [blame] | 1105 | |
| 1106 | pm_runtime_mark_last_busy(trf->dev); |
| 1107 | pm_runtime_put_autosuspend(trf->dev); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
Mark A. Greer | 0a1de84 | 2014-09-02 15:12:33 -0700 | [diff] [blame] | 1110 | static int trf7970a_switch_rf_on(struct trf7970a *trf) |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1111 | { |
Mark A. Greer | a08e545 | 2014-09-02 15:12:23 -0700 | [diff] [blame] | 1112 | int ret; |
| 1113 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1114 | dev_dbg(trf->dev, "Switching rf on\n"); |
| 1115 | |
Mark A. Greer | e6403b7 | 2014-03-25 08:54:38 -0700 | [diff] [blame] | 1116 | pm_runtime_get_sync(trf->dev); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1117 | |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1118 | if (trf->state != TRF7970A_ST_RF_OFF) { /* Power on, RF off */ |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 1119 | dev_err(trf->dev, "%s - Incorrect state: %d\n", __func__, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1120 | trf->state); |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 1121 | return -EINVAL; |
| 1122 | } |
| 1123 | |
Mark A. Greer | a08e545 | 2014-09-02 15:12:23 -0700 | [diff] [blame] | 1124 | ret = trf7970a_init(trf); |
| 1125 | if (ret) { |
| 1126 | dev_err(trf->dev, "%s - Can't initialize: %d\n", __func__, ret); |
Mark A. Greer | 0a1de84 | 2014-09-02 15:12:33 -0700 | [diff] [blame] | 1127 | return ret; |
Mark A. Greer | a08e545 | 2014-09-02 15:12:23 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
Mark A. Greer | e6403b7 | 2014-03-25 08:54:38 -0700 | [diff] [blame] | 1130 | trf->state = TRF7970A_ST_IDLE; |
Mark A. Greer | 0a1de84 | 2014-09-02 15:12:33 -0700 | [diff] [blame] | 1131 | |
| 1132 | return 0; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on) |
| 1136 | { |
| 1137 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
Mark A. Greer | 0a1de84 | 2014-09-02 15:12:33 -0700 | [diff] [blame] | 1138 | int ret = 0; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1139 | |
| 1140 | dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on); |
| 1141 | |
| 1142 | mutex_lock(&trf->lock); |
| 1143 | |
| 1144 | if (on) { |
| 1145 | switch (trf->state) { |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 1146 | case TRF7970A_ST_PWR_OFF: |
Mark A. Greer | b5e17d9 | 2014-09-02 15:12:34 -0700 | [diff] [blame] | 1147 | case TRF7970A_ST_RF_OFF: |
Mark A. Greer | 0a1de84 | 2014-09-02 15:12:33 -0700 | [diff] [blame] | 1148 | ret = trf7970a_switch_rf_on(trf); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1149 | break; |
| 1150 | case TRF7970A_ST_IDLE: |
| 1151 | case TRF7970A_ST_IDLE_RX_BLOCKED: |
| 1152 | break; |
| 1153 | default: |
| 1154 | dev_err(trf->dev, "%s - Invalid request: %d %d\n", |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1155 | __func__, trf->state, on); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1156 | trf7970a_switch_rf_off(trf); |
Mark A. Greer | 0a1de84 | 2014-09-02 15:12:33 -0700 | [diff] [blame] | 1157 | ret = -EINVAL; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1158 | } |
| 1159 | } else { |
| 1160 | switch (trf->state) { |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 1161 | case TRF7970A_ST_PWR_OFF: |
Mark A. Greer | b5e17d9 | 2014-09-02 15:12:34 -0700 | [diff] [blame] | 1162 | case TRF7970A_ST_RF_OFF: |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1163 | break; |
| 1164 | default: |
| 1165 | dev_err(trf->dev, "%s - Invalid request: %d %d\n", |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1166 | __func__, trf->state, on); |
Mark A. Greer | 0a1de84 | 2014-09-02 15:12:33 -0700 | [diff] [blame] | 1167 | ret = -EINVAL; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1168 | /* FALLTHROUGH */ |
| 1169 | case TRF7970A_ST_IDLE: |
| 1170 | case TRF7970A_ST_IDLE_RX_BLOCKED: |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1171 | case TRF7970A_ST_WAIT_FOR_RX_DATA: |
| 1172 | case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1173 | trf7970a_switch_rf_off(trf); |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | mutex_unlock(&trf->lock); |
Mark A. Greer | 0a1de84 | 2014-09-02 15:12:33 -0700 | [diff] [blame] | 1178 | return ret; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
Mark A. Greer | 307e5ca | 2014-09-02 15:12:39 -0700 | [diff] [blame] | 1181 | static int trf7970a_in_config_rf_tech(struct trf7970a *trf, int tech) |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1182 | { |
| 1183 | int ret = 0; |
| 1184 | |
| 1185 | dev_dbg(trf->dev, "rf technology: %d\n", tech); |
| 1186 | |
| 1187 | switch (tech) { |
| 1188 | case NFC_DIGITAL_RF_TECH_106A: |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 1189 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443A_106; |
Geoff Lansberry | 837eb4d | 2016-12-21 23:18:32 -0500 | [diff] [blame] | 1190 | trf->modulator_sys_clk_ctrl = |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1191 | (trf->modulator_sys_clk_ctrl & 0xf8) | |
| 1192 | TRF7970A_MODULATOR_DEPTH_OOK; |
Mark A. Greer | 4e64eff | 2014-09-02 15:12:24 -0700 | [diff] [blame] | 1193 | trf->guard_time = TRF7970A_GUARD_TIME_NFCA; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1194 | break; |
Mark A. Greer | 742b1f9 | 2014-03-31 17:52:33 -0700 | [diff] [blame] | 1195 | case NFC_DIGITAL_RF_TECH_106B: |
| 1196 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443B_106; |
Geoff Lansberry | 837eb4d | 2016-12-21 23:18:32 -0500 | [diff] [blame] | 1197 | trf->modulator_sys_clk_ctrl = |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1198 | (trf->modulator_sys_clk_ctrl & 0xf8) | |
| 1199 | TRF7970A_MODULATOR_DEPTH_ASK10; |
Mark A. Greer | 4e64eff | 2014-09-02 15:12:24 -0700 | [diff] [blame] | 1200 | trf->guard_time = TRF7970A_GUARD_TIME_NFCB; |
Mark A. Greer | 742b1f9 | 2014-03-31 17:52:33 -0700 | [diff] [blame] | 1201 | break; |
Mark A. Greer | 6857bb9 | 2014-04-14 10:04:10 -0700 | [diff] [blame] | 1202 | case NFC_DIGITAL_RF_TECH_212F: |
| 1203 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_212; |
Geoff Lansberry | 837eb4d | 2016-12-21 23:18:32 -0500 | [diff] [blame] | 1204 | trf->modulator_sys_clk_ctrl = |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1205 | (trf->modulator_sys_clk_ctrl & 0xf8) | |
| 1206 | TRF7970A_MODULATOR_DEPTH_ASK10; |
Mark A. Greer | 4e64eff | 2014-09-02 15:12:24 -0700 | [diff] [blame] | 1207 | trf->guard_time = TRF7970A_GUARD_TIME_NFCF; |
Mark A. Greer | 6857bb9 | 2014-04-14 10:04:10 -0700 | [diff] [blame] | 1208 | break; |
| 1209 | case NFC_DIGITAL_RF_TECH_424F: |
| 1210 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_424; |
Geoff Lansberry | 837eb4d | 2016-12-21 23:18:32 -0500 | [diff] [blame] | 1211 | trf->modulator_sys_clk_ctrl = |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1212 | (trf->modulator_sys_clk_ctrl & 0xf8) | |
| 1213 | TRF7970A_MODULATOR_DEPTH_ASK10; |
Mark A. Greer | 4e64eff | 2014-09-02 15:12:24 -0700 | [diff] [blame] | 1214 | trf->guard_time = TRF7970A_GUARD_TIME_NFCF; |
Mark A. Greer | 6857bb9 | 2014-04-14 10:04:10 -0700 | [diff] [blame] | 1215 | break; |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1216 | case NFC_DIGITAL_RF_TECH_ISO15693: |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 1217 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648; |
Geoff Lansberry | 837eb4d | 2016-12-21 23:18:32 -0500 | [diff] [blame] | 1218 | trf->modulator_sys_clk_ctrl = |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1219 | (trf->modulator_sys_clk_ctrl & 0xf8) | |
| 1220 | TRF7970A_MODULATOR_DEPTH_OOK; |
Mark A. Greer | 4e64eff | 2014-09-02 15:12:24 -0700 | [diff] [blame] | 1221 | trf->guard_time = TRF7970A_GUARD_TIME_15693; |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1222 | break; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1223 | default: |
| 1224 | dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech); |
| 1225 | return -EINVAL; |
| 1226 | } |
| 1227 | |
| 1228 | trf->technology = tech; |
| 1229 | |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1230 | /* If in initiator mode and not changing the RF tech due to a |
| 1231 | * PSL sequence (indicated by 'trf->iso_ctrl == 0xff' from |
| 1232 | * trf7970a_init()), clear the NFC Target Detection Level register |
| 1233 | * due to erratum. |
| 1234 | */ |
| 1235 | if (trf->iso_ctrl == 0xff) |
| 1236 | ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0); |
| 1237 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1238 | return ret; |
| 1239 | } |
| 1240 | |
Mark A. Greer | 851ee3c | 2014-09-02 15:12:42 -0700 | [diff] [blame] | 1241 | static int trf7970a_is_rf_field(struct trf7970a *trf, bool *is_rf_field) |
| 1242 | { |
| 1243 | int ret; |
| 1244 | u8 rssi; |
| 1245 | |
| 1246 | ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1247 | trf->chip_status_ctrl | |
| 1248 | TRF7970A_CHIP_STATUS_REC_ON); |
Mark A. Greer | 851ee3c | 2014-09-02 15:12:42 -0700 | [diff] [blame] | 1249 | if (ret) |
| 1250 | return ret; |
| 1251 | |
| 1252 | ret = trf7970a_cmd(trf, TRF7970A_CMD_TEST_EXT_RF); |
| 1253 | if (ret) |
| 1254 | return ret; |
| 1255 | |
| 1256 | usleep_range(50, 60); |
| 1257 | |
| 1258 | ret = trf7970a_read(trf, TRF7970A_RSSI_OSC_STATUS, &rssi); |
| 1259 | if (ret) |
| 1260 | return ret; |
| 1261 | |
| 1262 | ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1263 | trf->chip_status_ctrl); |
Mark A. Greer | 851ee3c | 2014-09-02 15:12:42 -0700 | [diff] [blame] | 1264 | if (ret) |
| 1265 | return ret; |
| 1266 | |
| 1267 | if (rssi & TRF7970A_RSSI_OSC_STATUS_RSSI_MASK) |
| 1268 | *is_rf_field = true; |
| 1269 | else |
| 1270 | *is_rf_field = false; |
| 1271 | |
| 1272 | return 0; |
| 1273 | } |
| 1274 | |
Mark A. Greer | 307e5ca | 2014-09-02 15:12:39 -0700 | [diff] [blame] | 1275 | static int trf7970a_in_config_framing(struct trf7970a *trf, int framing) |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1276 | { |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 1277 | u8 iso_ctrl = trf->iso_ctrl_tech; |
Mark A. Greer | 851ee3c | 2014-09-02 15:12:42 -0700 | [diff] [blame] | 1278 | bool is_rf_field = false; |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 1279 | int ret; |
| 1280 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1281 | dev_dbg(trf->dev, "framing: %d\n", framing); |
| 1282 | |
| 1283 | switch (framing) { |
| 1284 | case NFC_DIGITAL_FRAMING_NFCA_SHORT: |
| 1285 | case NFC_DIGITAL_FRAMING_NFCA_STANDARD: |
| 1286 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC; |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 1287 | iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1288 | break; |
| 1289 | case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A: |
Mark A. Greer | 8006289 | 2014-03-10 11:56:23 -0700 | [diff] [blame] | 1290 | case NFC_DIGITAL_FRAMING_NFCA_T4T: |
Mark A. Greer | 742b1f9 | 2014-03-31 17:52:33 -0700 | [diff] [blame] | 1291 | case NFC_DIGITAL_FRAMING_NFCB: |
| 1292 | case NFC_DIGITAL_FRAMING_NFCB_T4T: |
Mark A. Greer | 6857bb9 | 2014-04-14 10:04:10 -0700 | [diff] [blame] | 1293 | case NFC_DIGITAL_FRAMING_NFCF: |
| 1294 | case NFC_DIGITAL_FRAMING_NFCF_T3T: |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1295 | case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY: |
| 1296 | case NFC_DIGITAL_FRAMING_ISO15693_T5T: |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1297 | case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP: |
| 1298 | case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP: |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1299 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT; |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 1300 | iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1301 | break; |
| 1302 | case NFC_DIGITAL_FRAMING_NFCA_T2T: |
| 1303 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT; |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 1304 | iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1305 | break; |
| 1306 | default: |
| 1307 | dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing); |
| 1308 | return -EINVAL; |
| 1309 | } |
| 1310 | |
| 1311 | trf->framing = framing; |
| 1312 | |
Mark A. Greer | 851ee3c | 2014-09-02 15:12:42 -0700 | [diff] [blame] | 1313 | if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) { |
| 1314 | ret = trf7970a_is_rf_field(trf, &is_rf_field); |
| 1315 | if (ret) |
| 1316 | return ret; |
| 1317 | |
| 1318 | if (is_rf_field) |
| 1319 | return -EBUSY; |
| 1320 | } |
| 1321 | |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 1322 | if (iso_ctrl != trf->iso_ctrl) { |
| 1323 | ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl); |
| 1324 | if (ret) |
| 1325 | return ret; |
| 1326 | |
| 1327 | trf->iso_ctrl = iso_ctrl; |
Mark A. Greer | a0822a7 | 2014-03-25 08:54:34 -0700 | [diff] [blame] | 1328 | |
| 1329 | ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1330 | trf->modulator_sys_clk_ctrl); |
Mark A. Greer | a0822a7 | 2014-03-25 08:54:34 -0700 | [diff] [blame] | 1331 | if (ret) |
| 1332 | return ret; |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 1333 | } |
| 1334 | |
Mark A. Greer | a1d2dc5 | 2014-03-25 08:54:37 -0700 | [diff] [blame] | 1335 | if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) { |
| 1336 | ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1337 | trf->chip_status_ctrl | |
| 1338 | TRF7970A_CHIP_STATUS_RF_ON); |
Mark A. Greer | a1d2dc5 | 2014-03-25 08:54:37 -0700 | [diff] [blame] | 1339 | if (ret) |
| 1340 | return ret; |
| 1341 | |
| 1342 | trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON; |
| 1343 | |
Mark A. Greer | 4e64eff | 2014-09-02 15:12:24 -0700 | [diff] [blame] | 1344 | usleep_range(trf->guard_time, trf->guard_time + 1000); |
Mark A. Greer | a1d2dc5 | 2014-03-25 08:54:37 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 1347 | return 0; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1351 | int param) |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1352 | { |
| 1353 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
Mark A. Greer | 18422e6 | 2014-03-25 08:54:39 -0700 | [diff] [blame] | 1354 | int ret; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1355 | |
| 1356 | dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param); |
| 1357 | |
| 1358 | mutex_lock(&trf->lock); |
| 1359 | |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1360 | trf->is_initiator = true; |
| 1361 | |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 1362 | if ((trf->state == TRF7970A_ST_PWR_OFF) || |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1363 | (trf->state == TRF7970A_ST_RF_OFF)) { |
Mark A. Greer | 0a1de84 | 2014-09-02 15:12:33 -0700 | [diff] [blame] | 1364 | ret = trf7970a_switch_rf_on(trf); |
| 1365 | if (ret) |
| 1366 | goto err_unlock; |
| 1367 | } |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1368 | |
| 1369 | switch (type) { |
| 1370 | case NFC_DIGITAL_CONFIG_RF_TECH: |
Mark A. Greer | 307e5ca | 2014-09-02 15:12:39 -0700 | [diff] [blame] | 1371 | ret = trf7970a_in_config_rf_tech(trf, param); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1372 | break; |
| 1373 | case NFC_DIGITAL_CONFIG_FRAMING: |
Mark A. Greer | 307e5ca | 2014-09-02 15:12:39 -0700 | [diff] [blame] | 1374 | ret = trf7970a_in_config_framing(trf, param); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1375 | break; |
| 1376 | default: |
| 1377 | dev_dbg(trf->dev, "Unknown type: %d\n", type); |
| 1378 | ret = -EINVAL; |
| 1379 | } |
| 1380 | |
Mark A. Greer | 0a1de84 | 2014-09-02 15:12:33 -0700 | [diff] [blame] | 1381 | err_unlock: |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1382 | mutex_unlock(&trf->lock); |
| 1383 | return ret; |
| 1384 | } |
| 1385 | |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1386 | static int trf7970a_is_iso15693_write_or_lock(u8 cmd) |
| 1387 | { |
| 1388 | switch (cmd) { |
| 1389 | case ISO15693_CMD_WRITE_SINGLE_BLOCK: |
| 1390 | case ISO15693_CMD_LOCK_BLOCK: |
| 1391 | case ISO15693_CMD_WRITE_MULTIPLE_BLOCK: |
| 1392 | case ISO15693_CMD_WRITE_AFI: |
| 1393 | case ISO15693_CMD_LOCK_AFI: |
| 1394 | case ISO15693_CMD_WRITE_DSFID: |
| 1395 | case ISO15693_CMD_LOCK_DSFID: |
| 1396 | return 1; |
| 1397 | break; |
| 1398 | default: |
| 1399 | return 0; |
| 1400 | } |
| 1401 | } |
| 1402 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1403 | static int trf7970a_per_cmd_config(struct trf7970a *trf, struct sk_buff *skb) |
| 1404 | { |
| 1405 | u8 *req = skb->data; |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1406 | u8 special_fcn_reg1, iso_ctrl; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1407 | int ret; |
| 1408 | |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1409 | trf->issue_eof = false; |
| 1410 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1411 | /* When issuing Type 2 read command, make sure the '4_bit_RX' bit in |
| 1412 | * special functions register 1 is cleared; otherwise, its a write or |
| 1413 | * sector select command and '4_bit_RX' must be set. |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1414 | * |
| 1415 | * When issuing an ISO 15693 command, inspect the flags byte to see |
| 1416 | * what speed to use. Also, remember if the OPTION flag is set on |
| 1417 | * a Type 5 write or lock command so the driver will know that it |
| 1418 | * has to send an EOF in order to get a response. |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1419 | */ |
| 1420 | if ((trf->technology == NFC_DIGITAL_RF_TECH_106A) && |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1421 | (trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T)) { |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1422 | if (req[0] == NFC_T2T_CMD_READ) |
| 1423 | special_fcn_reg1 = 0; |
| 1424 | else |
| 1425 | special_fcn_reg1 = TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX; |
| 1426 | |
| 1427 | if (special_fcn_reg1 != trf->special_fcn_reg1) { |
| 1428 | ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1429 | special_fcn_reg1); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1430 | if (ret) |
| 1431 | return ret; |
| 1432 | |
| 1433 | trf->special_fcn_reg1 = special_fcn_reg1; |
| 1434 | } |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1435 | } else if (trf->technology == NFC_DIGITAL_RF_TECH_ISO15693) { |
| 1436 | iso_ctrl = trf->iso_ctrl & ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK; |
| 1437 | |
| 1438 | switch (req[0] & ISO15693_REQ_FLAG_SPEED_MASK) { |
| 1439 | case 0x00: |
| 1440 | iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_662; |
| 1441 | break; |
| 1442 | case ISO15693_REQ_FLAG_SUB_CARRIER: |
| 1443 | iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a; |
| 1444 | break; |
| 1445 | case ISO15693_REQ_FLAG_DATA_RATE: |
| 1446 | iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648; |
| 1447 | break; |
| 1448 | case (ISO15693_REQ_FLAG_SUB_CARRIER | |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1449 | ISO15693_REQ_FLAG_DATA_RATE): |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1450 | iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669; |
| 1451 | break; |
| 1452 | } |
| 1453 | |
| 1454 | if (iso_ctrl != trf->iso_ctrl) { |
| 1455 | ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl); |
| 1456 | if (ret) |
| 1457 | return ret; |
| 1458 | |
| 1459 | trf->iso_ctrl = iso_ctrl; |
| 1460 | } |
| 1461 | |
Mark A. Greer | ab71481 | 2015-04-24 10:37:22 -0700 | [diff] [blame] | 1462 | if (trf->framing == NFC_DIGITAL_FRAMING_ISO15693_T5T) { |
| 1463 | if (trf7970a_is_iso15693_write_or_lock(req[1]) && |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1464 | (req[0] & ISO15693_REQ_FLAG_OPTION)) |
Mark A. Greer | ab71481 | 2015-04-24 10:37:22 -0700 | [diff] [blame] | 1465 | trf->issue_eof = true; |
| 1466 | else if ((trf->quirks & |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1467 | TRF7970A_QUIRK_T5T_RMB_EXTRA_BYTE) && |
Mark A. Greer | ab71481 | 2015-04-24 10:37:22 -0700 | [diff] [blame] | 1468 | (req[1] == ISO15693_CMD_READ_MULTIPLE_BLOCK)) |
| 1469 | trf->adjust_resp_len = true; |
| 1470 | } |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | return 0; |
| 1474 | } |
| 1475 | |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1476 | static int trf7970a_send_cmd(struct nfc_digital_dev *ddev, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1477 | struct sk_buff *skb, u16 timeout, |
| 1478 | nfc_digital_cmd_complete_t cb, void *arg) |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1479 | { |
| 1480 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
Mark A. Greer | 7a1e555 | 2014-09-02 15:12:28 -0700 | [diff] [blame] | 1481 | u8 prefix[5]; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1482 | unsigned int len; |
| 1483 | int ret; |
Mark A. Greer | aff0564 | 2014-09-02 15:12:26 -0700 | [diff] [blame] | 1484 | u8 status; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1485 | |
| 1486 | dev_dbg(trf->dev, "New request - state: %d, timeout: %d ms, len: %d\n", |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1487 | trf->state, timeout, skb->len); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1488 | |
| 1489 | if (skb->len > TRF7970A_TX_MAX) |
| 1490 | return -EINVAL; |
| 1491 | |
| 1492 | mutex_lock(&trf->lock); |
| 1493 | |
| 1494 | if ((trf->state != TRF7970A_ST_IDLE) && |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1495 | (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) { |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1496 | dev_err(trf->dev, "%s - Bogus state: %d\n", __func__, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1497 | trf->state); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1498 | ret = -EIO; |
| 1499 | goto out_err; |
| 1500 | } |
| 1501 | |
| 1502 | if (trf->aborting) { |
| 1503 | dev_dbg(trf->dev, "Abort process complete\n"); |
| 1504 | trf->aborting = false; |
| 1505 | ret = -ECANCELED; |
| 1506 | goto out_err; |
| 1507 | } |
| 1508 | |
Mark A. Greer | 1961843 | 2014-09-02 15:12:44 -0700 | [diff] [blame] | 1509 | if (timeout) { |
| 1510 | trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1511 | GFP_KERNEL); |
Mark A. Greer | 1961843 | 2014-09-02 15:12:44 -0700 | [diff] [blame] | 1512 | if (!trf->rx_skb) { |
| 1513 | dev_dbg(trf->dev, "Can't alloc rx_skb\n"); |
| 1514 | ret = -ENOMEM; |
| 1515 | goto out_err; |
| 1516 | } |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | if (trf->state == TRF7970A_ST_IDLE_RX_BLOCKED) { |
| 1520 | ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX); |
| 1521 | if (ret) |
| 1522 | goto out_err; |
| 1523 | |
| 1524 | trf->state = TRF7970A_ST_IDLE; |
| 1525 | } |
| 1526 | |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1527 | if (trf->is_initiator) { |
| 1528 | ret = trf7970a_per_cmd_config(trf, skb); |
| 1529 | if (ret) |
| 1530 | goto out_err; |
| 1531 | } |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1532 | |
| 1533 | trf->ddev = ddev; |
| 1534 | trf->tx_skb = skb; |
| 1535 | trf->cb = cb; |
| 1536 | trf->cb_arg = arg; |
| 1537 | trf->timeout = timeout; |
| 1538 | trf->ignore_timeout = false; |
| 1539 | |
| 1540 | len = skb->len; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1541 | |
| 1542 | /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends |
| 1543 | * on what the current framing is, the address of the TX length byte 1 |
| 1544 | * register (0x1d), and the 2 byte length of the data to be transmitted. |
Mark A. Greer | 7a1e555 | 2014-09-02 15:12:28 -0700 | [diff] [blame] | 1545 | * That totals 5 bytes. |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1546 | */ |
| 1547 | prefix[0] = TRF7970A_CMD_BIT_CTRL | |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1548 | TRF7970A_CMD_BIT_OPCODE(TRF7970A_CMD_FIFO_RESET); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1549 | prefix[1] = TRF7970A_CMD_BIT_CTRL | |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1550 | TRF7970A_CMD_BIT_OPCODE(trf->tx_cmd); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1551 | prefix[2] = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_TX_LENGTH_BYTE1; |
| 1552 | |
| 1553 | if (trf->framing == NFC_DIGITAL_FRAMING_NFCA_SHORT) { |
| 1554 | prefix[3] = 0x00; |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1555 | prefix[4] = 0x0f; /* 7 bits */ |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1556 | } else { |
| 1557 | prefix[3] = (len & 0xf00) >> 4; |
| 1558 | prefix[3] |= ((len & 0xf0) >> 4); |
| 1559 | prefix[4] = ((len & 0x0f) << 4); |
| 1560 | } |
| 1561 | |
| 1562 | len = min_t(int, skb->len, TRF7970A_FIFO_SIZE); |
| 1563 | |
Mark A. Greer | aff0564 | 2014-09-02 15:12:26 -0700 | [diff] [blame] | 1564 | /* Clear possible spurious interrupt */ |
| 1565 | ret = trf7970a_read_irqstatus(trf, &status); |
| 1566 | if (ret) |
| 1567 | goto out_err; |
| 1568 | |
Mark A. Greer | 7a1e555 | 2014-09-02 15:12:28 -0700 | [diff] [blame] | 1569 | ret = trf7970a_transmit(trf, skb, len, prefix, sizeof(prefix)); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1570 | if (ret) { |
| 1571 | kfree_skb(trf->rx_skb); |
| 1572 | trf->rx_skb = NULL; |
| 1573 | } |
| 1574 | |
| 1575 | out_err: |
| 1576 | mutex_unlock(&trf->lock); |
| 1577 | return ret; |
| 1578 | } |
| 1579 | |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1580 | static int trf7970a_tg_config_rf_tech(struct trf7970a *trf, int tech) |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1581 | { |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1582 | int ret = 0; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1583 | |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1584 | dev_dbg(trf->dev, "rf technology: %d\n", tech); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1585 | |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1586 | switch (tech) { |
| 1587 | case NFC_DIGITAL_RF_TECH_106A: |
| 1588 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE | |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1589 | TRF7970A_ISO_CTRL_NFC_CE | TRF7970A_ISO_CTRL_NFC_CE_14443A; |
Geoff Lansberry | 837eb4d | 2016-12-21 23:18:32 -0500 | [diff] [blame] | 1590 | trf->modulator_sys_clk_ctrl = |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1591 | (trf->modulator_sys_clk_ctrl & 0xf8) | |
| 1592 | TRF7970A_MODULATOR_DEPTH_OOK; |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1593 | break; |
| 1594 | case NFC_DIGITAL_RF_TECH_212F: |
| 1595 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE | |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1596 | TRF7970A_ISO_CTRL_NFC_NFCF_212; |
Geoff Lansberry | 837eb4d | 2016-12-21 23:18:32 -0500 | [diff] [blame] | 1597 | trf->modulator_sys_clk_ctrl = |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1598 | (trf->modulator_sys_clk_ctrl & 0xf8) | |
| 1599 | TRF7970A_MODULATOR_DEPTH_ASK10; |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1600 | break; |
| 1601 | case NFC_DIGITAL_RF_TECH_424F: |
| 1602 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE | |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1603 | TRF7970A_ISO_CTRL_NFC_NFCF_424; |
Geoff Lansberry | 837eb4d | 2016-12-21 23:18:32 -0500 | [diff] [blame] | 1604 | trf->modulator_sys_clk_ctrl = |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1605 | (trf->modulator_sys_clk_ctrl & 0xf8) | |
| 1606 | TRF7970A_MODULATOR_DEPTH_ASK10; |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1607 | break; |
| 1608 | default: |
| 1609 | dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech); |
| 1610 | return -EINVAL; |
| 1611 | } |
| 1612 | |
| 1613 | trf->technology = tech; |
| 1614 | |
| 1615 | /* Normally we write the ISO_CTRL register in |
| 1616 | * trf7970a_tg_config_framing() because the framing can change |
| 1617 | * the value written. However, when sending a PSL RES, |
| 1618 | * digital_tg_send_psl_res_complete() doesn't call |
| 1619 | * trf7970a_tg_config_framing() so we must write the register |
| 1620 | * here. |
| 1621 | */ |
| 1622 | if ((trf->framing == NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED) && |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1623 | (trf->iso_ctrl_tech != trf->iso_ctrl)) { |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1624 | ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1625 | trf->iso_ctrl_tech); |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1626 | |
| 1627 | trf->iso_ctrl = trf->iso_ctrl_tech; |
| 1628 | } |
| 1629 | |
| 1630 | return ret; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1631 | } |
| 1632 | |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1633 | /* Since this is a target routine, several of the framing calls are |
| 1634 | * made between receiving the request and sending the response so they |
| 1635 | * should take effect until after the response is sent. This is accomplished |
| 1636 | * by skipping the ISO_CTRL register write here and doing it in the interrupt |
| 1637 | * handler. |
| 1638 | */ |
| 1639 | static int trf7970a_tg_config_framing(struct trf7970a *trf, int framing) |
| 1640 | { |
| 1641 | u8 iso_ctrl = trf->iso_ctrl_tech; |
| 1642 | int ret; |
| 1643 | |
| 1644 | dev_dbg(trf->dev, "framing: %d\n", framing); |
| 1645 | |
| 1646 | switch (framing) { |
| 1647 | case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP: |
| 1648 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC; |
| 1649 | iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N; |
| 1650 | break; |
| 1651 | case NFC_DIGITAL_FRAMING_NFCA_STANDARD: |
| 1652 | case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A: |
| 1653 | case NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE: |
| 1654 | /* These ones are applied in the interrupt handler */ |
| 1655 | iso_ctrl = trf->iso_ctrl; /* Don't write to ISO_CTRL yet */ |
| 1656 | break; |
| 1657 | case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP: |
| 1658 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT; |
| 1659 | iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N; |
| 1660 | break; |
| 1661 | case NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED: |
| 1662 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT; |
| 1663 | iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N; |
| 1664 | break; |
| 1665 | default: |
| 1666 | dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing); |
| 1667 | return -EINVAL; |
| 1668 | } |
| 1669 | |
| 1670 | trf->framing = framing; |
| 1671 | |
| 1672 | if (iso_ctrl != trf->iso_ctrl) { |
| 1673 | ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl); |
| 1674 | if (ret) |
| 1675 | return ret; |
| 1676 | |
| 1677 | trf->iso_ctrl = iso_ctrl; |
| 1678 | |
| 1679 | ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1680 | trf->modulator_sys_clk_ctrl); |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1681 | if (ret) |
| 1682 | return ret; |
| 1683 | } |
| 1684 | |
| 1685 | if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) { |
| 1686 | ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1687 | trf->chip_status_ctrl | |
| 1688 | TRF7970A_CHIP_STATUS_RF_ON); |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1689 | if (ret) |
| 1690 | return ret; |
| 1691 | |
| 1692 | trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON; |
| 1693 | } |
| 1694 | |
| 1695 | return 0; |
| 1696 | } |
| 1697 | |
| 1698 | static int trf7970a_tg_configure_hw(struct nfc_digital_dev *ddev, int type, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1699 | int param) |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1700 | { |
| 1701 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 1702 | int ret; |
| 1703 | |
| 1704 | dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param); |
| 1705 | |
| 1706 | mutex_lock(&trf->lock); |
| 1707 | |
| 1708 | trf->is_initiator = false; |
| 1709 | |
| 1710 | if ((trf->state == TRF7970A_ST_PWR_OFF) || |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1711 | (trf->state == TRF7970A_ST_RF_OFF)) { |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1712 | ret = trf7970a_switch_rf_on(trf); |
| 1713 | if (ret) |
| 1714 | goto err_unlock; |
| 1715 | } |
| 1716 | |
| 1717 | switch (type) { |
| 1718 | case NFC_DIGITAL_CONFIG_RF_TECH: |
| 1719 | ret = trf7970a_tg_config_rf_tech(trf, param); |
| 1720 | break; |
| 1721 | case NFC_DIGITAL_CONFIG_FRAMING: |
| 1722 | ret = trf7970a_tg_config_framing(trf, param); |
| 1723 | break; |
| 1724 | default: |
| 1725 | dev_dbg(trf->dev, "Unknown type: %d\n", type); |
| 1726 | ret = -EINVAL; |
| 1727 | } |
| 1728 | |
| 1729 | err_unlock: |
| 1730 | mutex_unlock(&trf->lock); |
| 1731 | return ret; |
| 1732 | } |
| 1733 | |
Mark A. Greer | cb174ab | 2014-09-02 15:12:46 -0700 | [diff] [blame] | 1734 | static int _trf7970a_tg_listen(struct nfc_digital_dev *ddev, u16 timeout, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1735 | nfc_digital_cmd_complete_t cb, void *arg, |
| 1736 | bool mode_detect) |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1737 | { |
| 1738 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1739 | int ret; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1740 | |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1741 | mutex_lock(&trf->lock); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1742 | |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1743 | if ((trf->state != TRF7970A_ST_IDLE) && |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1744 | (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) { |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1745 | dev_err(trf->dev, "%s - Bogus state: %d\n", __func__, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1746 | trf->state); |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1747 | ret = -EIO; |
| 1748 | goto out_err; |
| 1749 | } |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1750 | |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1751 | if (trf->aborting) { |
| 1752 | dev_dbg(trf->dev, "Abort process complete\n"); |
| 1753 | trf->aborting = false; |
| 1754 | ret = -ECANCELED; |
| 1755 | goto out_err; |
| 1756 | } |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1757 | |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1758 | trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1759 | GFP_KERNEL); |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1760 | if (!trf->rx_skb) { |
| 1761 | dev_dbg(trf->dev, "Can't alloc rx_skb\n"); |
| 1762 | ret = -ENOMEM; |
| 1763 | goto out_err; |
| 1764 | } |
| 1765 | |
| 1766 | ret = trf7970a_write(trf, TRF7970A_RX_SPECIAL_SETTINGS, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1767 | TRF7970A_RX_SPECIAL_SETTINGS_HBT | |
| 1768 | TRF7970A_RX_SPECIAL_SETTINGS_M848 | |
| 1769 | TRF7970A_RX_SPECIAL_SETTINGS_C424 | |
| 1770 | TRF7970A_RX_SPECIAL_SETTINGS_C212); |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1771 | if (ret) |
Mark A. Greer | fc0ae24 | 2014-09-23 11:25:48 -0700 | [diff] [blame] | 1772 | goto out_err; |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1773 | |
| 1774 | ret = trf7970a_write(trf, TRF7970A_REG_IO_CTRL, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1775 | trf->io_ctrl | TRF7970A_REG_IO_CTRL_VRS(0x1)); |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1776 | if (ret) |
Mark A. Greer | fc0ae24 | 2014-09-23 11:25:48 -0700 | [diff] [blame] | 1777 | goto out_err; |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1778 | |
| 1779 | ret = trf7970a_write(trf, TRF7970A_NFC_LOW_FIELD_LEVEL, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1780 | TRF7970A_NFC_LOW_FIELD_LEVEL_RFDET(0x3)); |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1781 | if (ret) |
Mark A. Greer | fc0ae24 | 2014-09-23 11:25:48 -0700 | [diff] [blame] | 1782 | goto out_err; |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1783 | |
| 1784 | ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1785 | TRF7970A_NFC_TARGET_LEVEL_RFDET(0x7)); |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1786 | if (ret) |
Mark A. Greer | fc0ae24 | 2014-09-23 11:25:48 -0700 | [diff] [blame] | 1787 | goto out_err; |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1788 | |
| 1789 | trf->ddev = ddev; |
| 1790 | trf->cb = cb; |
| 1791 | trf->cb_arg = arg; |
| 1792 | trf->timeout = timeout; |
| 1793 | trf->ignore_timeout = false; |
| 1794 | |
| 1795 | ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX); |
| 1796 | if (ret) |
| 1797 | goto out_err; |
| 1798 | |
Mark A. Greer | cb174ab | 2014-09-02 15:12:46 -0700 | [diff] [blame] | 1799 | trf->state = mode_detect ? TRF7970A_ST_LISTENING_MD : |
| 1800 | TRF7970A_ST_LISTENING; |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1801 | |
| 1802 | schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout)); |
| 1803 | |
| 1804 | out_err: |
| 1805 | mutex_unlock(&trf->lock); |
| 1806 | return ret; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1807 | } |
| 1808 | |
Mark A. Greer | cb174ab | 2014-09-02 15:12:46 -0700 | [diff] [blame] | 1809 | static int trf7970a_tg_listen(struct nfc_digital_dev *ddev, u16 timeout, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1810 | nfc_digital_cmd_complete_t cb, void *arg) |
Mark A. Greer | cb174ab | 2014-09-02 15:12:46 -0700 | [diff] [blame] | 1811 | { |
| 1812 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 1813 | |
| 1814 | dev_dbg(trf->dev, "Listen - state: %d, timeout: %d ms\n", |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1815 | trf->state, timeout); |
Mark A. Greer | cb174ab | 2014-09-02 15:12:46 -0700 | [diff] [blame] | 1816 | |
| 1817 | return _trf7970a_tg_listen(ddev, timeout, cb, arg, false); |
| 1818 | } |
| 1819 | |
| 1820 | static int trf7970a_tg_listen_md(struct nfc_digital_dev *ddev, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1821 | u16 timeout, nfc_digital_cmd_complete_t cb, |
| 1822 | void *arg) |
Mark A. Greer | cb174ab | 2014-09-02 15:12:46 -0700 | [diff] [blame] | 1823 | { |
| 1824 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 1825 | int ret; |
| 1826 | |
| 1827 | dev_dbg(trf->dev, "Listen MD - state: %d, timeout: %d ms\n", |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1828 | trf->state, timeout); |
Mark A. Greer | cb174ab | 2014-09-02 15:12:46 -0700 | [diff] [blame] | 1829 | |
| 1830 | ret = trf7970a_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1831 | NFC_DIGITAL_RF_TECH_106A); |
Mark A. Greer | cb174ab | 2014-09-02 15:12:46 -0700 | [diff] [blame] | 1832 | if (ret) |
| 1833 | return ret; |
| 1834 | |
| 1835 | ret = trf7970a_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1836 | NFC_DIGITAL_FRAMING_NFCA_NFC_DEP); |
Mark A. Greer | cb174ab | 2014-09-02 15:12:46 -0700 | [diff] [blame] | 1837 | if (ret) |
| 1838 | return ret; |
| 1839 | |
| 1840 | return _trf7970a_tg_listen(ddev, timeout, cb, arg, true); |
| 1841 | } |
| 1842 | |
| 1843 | static int trf7970a_tg_get_rf_tech(struct nfc_digital_dev *ddev, u8 *rf_tech) |
| 1844 | { |
| 1845 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 1846 | |
| 1847 | dev_dbg(trf->dev, "Get RF Tech - state: %d, rf_tech: %d\n", |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1848 | trf->state, trf->md_rf_tech); |
Mark A. Greer | cb174ab | 2014-09-02 15:12:46 -0700 | [diff] [blame] | 1849 | |
| 1850 | *rf_tech = trf->md_rf_tech; |
| 1851 | |
| 1852 | return 0; |
| 1853 | } |
| 1854 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1855 | static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev) |
| 1856 | { |
| 1857 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 1858 | |
| 1859 | dev_dbg(trf->dev, "Abort process initiated\n"); |
| 1860 | |
| 1861 | mutex_lock(&trf->lock); |
Mark A. Greer | 5876bc7 | 2014-03-25 08:54:30 -0700 | [diff] [blame] | 1862 | |
| 1863 | switch (trf->state) { |
| 1864 | case TRF7970A_ST_WAIT_FOR_TX_FIFO: |
| 1865 | case TRF7970A_ST_WAIT_FOR_RX_DATA: |
| 1866 | case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: |
| 1867 | case TRF7970A_ST_WAIT_TO_ISSUE_EOF: |
| 1868 | trf->aborting = true; |
| 1869 | break; |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1870 | case TRF7970A_ST_LISTENING: |
| 1871 | trf->ignore_timeout = !cancel_delayed_work(&trf->timeout_work); |
| 1872 | trf7970a_send_err_upstream(trf, -ECANCELED); |
| 1873 | dev_dbg(trf->dev, "Abort process complete\n"); |
| 1874 | break; |
Mark A. Greer | 5876bc7 | 2014-03-25 08:54:30 -0700 | [diff] [blame] | 1875 | default: |
| 1876 | break; |
| 1877 | } |
| 1878 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1879 | mutex_unlock(&trf->lock); |
| 1880 | } |
| 1881 | |
| 1882 | static struct nfc_digital_ops trf7970a_nfc_ops = { |
| 1883 | .in_configure_hw = trf7970a_in_configure_hw, |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1884 | .in_send_cmd = trf7970a_send_cmd, |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1885 | .tg_configure_hw = trf7970a_tg_configure_hw, |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1886 | .tg_send_cmd = trf7970a_send_cmd, |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1887 | .tg_listen = trf7970a_tg_listen, |
Mark A. Greer | cb174ab | 2014-09-02 15:12:46 -0700 | [diff] [blame] | 1888 | .tg_listen_md = trf7970a_tg_listen_md, |
| 1889 | .tg_get_rf_tech = trf7970a_tg_get_rf_tech, |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1890 | .switch_rf = trf7970a_switch_rf, |
| 1891 | .abort_cmd = trf7970a_abort_cmd, |
| 1892 | }; |
| 1893 | |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 1894 | static int trf7970a_power_up(struct trf7970a *trf) |
| 1895 | { |
| 1896 | int ret; |
| 1897 | |
| 1898 | dev_dbg(trf->dev, "Powering up - state: %d\n", trf->state); |
| 1899 | |
| 1900 | if (trf->state != TRF7970A_ST_PWR_OFF) |
| 1901 | return 0; |
| 1902 | |
| 1903 | ret = regulator_enable(trf->regulator); |
| 1904 | if (ret) { |
| 1905 | dev_err(trf->dev, "%s - Can't enable VIN: %d\n", __func__, ret); |
| 1906 | return ret; |
| 1907 | } |
| 1908 | |
| 1909 | usleep_range(5000, 6000); |
| 1910 | |
Mark Greer | d34e48d | 2017-04-25 15:43:55 -0700 | [diff] [blame] | 1911 | if (trf->en2_gpiod && |
| 1912 | !(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW)) { |
| 1913 | gpiod_set_value_cansleep(trf->en2_gpiod, 1); |
| 1914 | usleep_range(1000, 2000); |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 1915 | } |
| 1916 | |
Mark Greer | d34e48d | 2017-04-25 15:43:55 -0700 | [diff] [blame] | 1917 | gpiod_set_value_cansleep(trf->en_gpiod, 1); |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 1918 | |
| 1919 | usleep_range(20000, 21000); |
| 1920 | |
| 1921 | trf->state = TRF7970A_ST_RF_OFF; |
| 1922 | |
| 1923 | return 0; |
| 1924 | } |
| 1925 | |
| 1926 | static int trf7970a_power_down(struct trf7970a *trf) |
| 1927 | { |
| 1928 | int ret; |
| 1929 | |
| 1930 | dev_dbg(trf->dev, "Powering down - state: %d\n", trf->state); |
| 1931 | |
| 1932 | if (trf->state == TRF7970A_ST_PWR_OFF) |
| 1933 | return 0; |
| 1934 | |
| 1935 | if (trf->state != TRF7970A_ST_RF_OFF) { |
| 1936 | dev_dbg(trf->dev, "Can't power down - not RF_OFF state (%d)\n", |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1937 | trf->state); |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 1938 | return -EBUSY; |
| 1939 | } |
| 1940 | |
Mark Greer | d34e48d | 2017-04-25 15:43:55 -0700 | [diff] [blame] | 1941 | gpiod_set_value_cansleep(trf->en_gpiod, 0); |
Mark Greer | 67dec19 | 2017-04-25 15:43:49 -0700 | [diff] [blame] | 1942 | |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1943 | if (trf->en2_gpiod && !(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW)) |
Mark Greer | d34e48d | 2017-04-25 15:43:55 -0700 | [diff] [blame] | 1944 | gpiod_set_value_cansleep(trf->en2_gpiod, 0); |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 1945 | |
| 1946 | ret = regulator_disable(trf->regulator); |
| 1947 | if (ret) |
| 1948 | dev_err(trf->dev, "%s - Can't disable VIN: %d\n", __func__, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 1949 | ret); |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 1950 | |
| 1951 | trf->state = TRF7970A_ST_PWR_OFF; |
| 1952 | |
| 1953 | return ret; |
| 1954 | } |
| 1955 | |
Mark A. Greer | b528281 | 2014-09-02 15:12:36 -0700 | [diff] [blame] | 1956 | static int trf7970a_startup(struct trf7970a *trf) |
| 1957 | { |
| 1958 | int ret; |
| 1959 | |
| 1960 | ret = trf7970a_power_up(trf); |
| 1961 | if (ret) |
| 1962 | return ret; |
| 1963 | |
| 1964 | pm_runtime_set_active(trf->dev); |
| 1965 | pm_runtime_enable(trf->dev); |
| 1966 | pm_runtime_mark_last_busy(trf->dev); |
| 1967 | |
| 1968 | return 0; |
| 1969 | } |
| 1970 | |
| 1971 | static void trf7970a_shutdown(struct trf7970a *trf) |
| 1972 | { |
| 1973 | switch (trf->state) { |
| 1974 | case TRF7970A_ST_WAIT_FOR_TX_FIFO: |
| 1975 | case TRF7970A_ST_WAIT_FOR_RX_DATA: |
| 1976 | case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: |
| 1977 | case TRF7970A_ST_WAIT_TO_ISSUE_EOF: |
Mark A. Greer | 13b4272 | 2014-09-02 15:12:45 -0700 | [diff] [blame] | 1978 | case TRF7970A_ST_LISTENING: |
Mark A. Greer | b528281 | 2014-09-02 15:12:36 -0700 | [diff] [blame] | 1979 | trf7970a_send_err_upstream(trf, -ECANCELED); |
| 1980 | /* FALLTHROUGH */ |
| 1981 | case TRF7970A_ST_IDLE: |
| 1982 | case TRF7970A_ST_IDLE_RX_BLOCKED: |
| 1983 | trf7970a_switch_rf_off(trf); |
| 1984 | break; |
| 1985 | default: |
| 1986 | break; |
| 1987 | } |
| 1988 | |
| 1989 | pm_runtime_disable(trf->dev); |
| 1990 | pm_runtime_set_suspended(trf->dev); |
| 1991 | |
| 1992 | trf7970a_power_down(trf); |
| 1993 | } |
| 1994 | |
Mark A. Greer | fd0c828 | 2014-03-25 08:54:40 -0700 | [diff] [blame] | 1995 | static int trf7970a_get_autosuspend_delay(struct device_node *np) |
| 1996 | { |
| 1997 | int autosuspend_delay, ret; |
| 1998 | |
| 1999 | ret = of_property_read_u32(np, "autosuspend-delay", &autosuspend_delay); |
| 2000 | if (ret) |
| 2001 | autosuspend_delay = TRF7970A_AUTOSUSPEND_DELAY; |
| 2002 | |
Mark A. Greer | fd0c828 | 2014-03-25 08:54:40 -0700 | [diff] [blame] | 2003 | return autosuspend_delay; |
| 2004 | } |
| 2005 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 2006 | static int trf7970a_probe(struct spi_device *spi) |
| 2007 | { |
| 2008 | struct device_node *np = spi->dev.of_node; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 2009 | struct trf7970a *trf; |
Mark A. Greer | fd0c828 | 2014-03-25 08:54:40 -0700 | [diff] [blame] | 2010 | int uvolts, autosuspend_delay, ret; |
Geoff Lansberry | 837eb4d | 2016-12-21 23:18:32 -0500 | [diff] [blame] | 2011 | u32 clk_freq = TRF7970A_13MHZ_CLOCK_FREQUENCY; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 2012 | |
| 2013 | if (!np) { |
| 2014 | dev_err(&spi->dev, "No Device Tree entry\n"); |
| 2015 | return -EINVAL; |
| 2016 | } |
| 2017 | |
| 2018 | trf = devm_kzalloc(&spi->dev, sizeof(*trf), GFP_KERNEL); |
| 2019 | if (!trf) |
| 2020 | return -ENOMEM; |
| 2021 | |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 2022 | trf->state = TRF7970A_ST_PWR_OFF; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 2023 | trf->dev = &spi->dev; |
| 2024 | trf->spi = spi; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 2025 | |
| 2026 | spi->mode = SPI_MODE_1; |
| 2027 | spi->bits_per_word = 8; |
| 2028 | |
Mark A. Greer | 2470729 | 2014-09-02 15:12:17 -0700 | [diff] [blame] | 2029 | ret = spi_setup(spi); |
| 2030 | if (ret < 0) { |
| 2031 | dev_err(trf->dev, "Can't set up SPI Communication\n"); |
| 2032 | return ret; |
| 2033 | } |
| 2034 | |
Mark A. Greer | ab71481 | 2015-04-24 10:37:22 -0700 | [diff] [blame] | 2035 | if (of_property_read_bool(np, "t5t-rmb-extra-byte-quirk")) |
| 2036 | trf->quirks |= TRF7970A_QUIRK_T5T_RMB_EXTRA_BYTE; |
| 2037 | |
Mark A. Greer | 772079e | 2014-08-07 17:41:43 -0700 | [diff] [blame] | 2038 | if (of_property_read_bool(np, "irq-status-read-quirk")) |
| 2039 | trf->quirks |= TRF7970A_QUIRK_IRQ_STATUS_READ; |
| 2040 | |
Mark Greer | 69f984f | 2017-04-25 15:43:50 -0700 | [diff] [blame] | 2041 | /* There are two enable pins - only EN must be present in the DT */ |
Mark Greer | d34e48d | 2017-04-25 15:43:55 -0700 | [diff] [blame] | 2042 | trf->en_gpiod = devm_gpiod_get_index(trf->dev, "ti,enable", 0, |
| 2043 | GPIOD_OUT_LOW); |
| 2044 | if (IS_ERR(trf->en_gpiod)) { |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 2045 | dev_err(trf->dev, "No EN GPIO property\n"); |
Mark Greer | d34e48d | 2017-04-25 15:43:55 -0700 | [diff] [blame] | 2046 | return PTR_ERR(trf->en_gpiod); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 2047 | } |
| 2048 | |
Mark Greer | d34e48d | 2017-04-25 15:43:55 -0700 | [diff] [blame] | 2049 | trf->en2_gpiod = devm_gpiod_get_index_optional(trf->dev, "ti,enable", 1, |
| 2050 | GPIOD_OUT_LOW); |
| 2051 | if (!trf->en2_gpiod) { |
Guan Ben | ce69b95 | 2017-02-07 06:22:04 +0100 | [diff] [blame] | 2052 | dev_info(trf->dev, "No EN2 GPIO property\n"); |
Mark Greer | d34e48d | 2017-04-25 15:43:55 -0700 | [diff] [blame] | 2053 | } else if (IS_ERR(trf->en2_gpiod)) { |
| 2054 | dev_err(trf->dev, "Error getting EN2 GPIO property: %ld\n", |
| 2055 | PTR_ERR(trf->en2_gpiod)); |
| 2056 | return PTR_ERR(trf->en2_gpiod); |
| 2057 | } else if (of_property_read_bool(np, "en2-rf-quirk")) { |
| 2058 | trf->quirks |= TRF7970A_QUIRK_EN2_MUST_STAY_LOW; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 2059 | } |
| 2060 | |
Geoff Lansberry | 837eb4d | 2016-12-21 23:18:32 -0500 | [diff] [blame] | 2061 | of_property_read_u32(np, "clock-frequency", &clk_freq); |
Colin Ian King | e103853 | 2017-04-24 14:36:02 +0100 | [diff] [blame] | 2062 | if ((clk_freq != TRF7970A_27MHZ_CLOCK_FREQUENCY) && |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 2063 | (clk_freq != TRF7970A_13MHZ_CLOCK_FREQUENCY)) { |
Geoff Lansberry | 837eb4d | 2016-12-21 23:18:32 -0500 | [diff] [blame] | 2064 | dev_err(trf->dev, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 2065 | "clock-frequency (%u Hz) unsupported\n", clk_freq); |
Geoff Lansberry | 837eb4d | 2016-12-21 23:18:32 -0500 | [diff] [blame] | 2066 | return -EINVAL; |
| 2067 | } |
| 2068 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 2069 | ret = devm_request_threaded_irq(trf->dev, spi->irq, NULL, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 2070 | trf7970a_irq, |
| 2071 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
| 2072 | "trf7970a", trf); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 2073 | if (ret) { |
| 2074 | dev_err(trf->dev, "Can't request IRQ#%d: %d\n", spi->irq, ret); |
| 2075 | return ret; |
| 2076 | } |
| 2077 | |
| 2078 | mutex_init(&trf->lock); |
| 2079 | INIT_DELAYED_WORK(&trf->timeout_work, trf7970a_timeout_work_handler); |
| 2080 | |
| 2081 | trf->regulator = devm_regulator_get(&spi->dev, "vin"); |
| 2082 | if (IS_ERR(trf->regulator)) { |
| 2083 | ret = PTR_ERR(trf->regulator); |
| 2084 | dev_err(trf->dev, "Can't get VIN regulator: %d\n", ret); |
| 2085 | goto err_destroy_lock; |
| 2086 | } |
| 2087 | |
| 2088 | ret = regulator_enable(trf->regulator); |
| 2089 | if (ret) { |
| 2090 | dev_err(trf->dev, "Can't enable VIN: %d\n", ret); |
| 2091 | goto err_destroy_lock; |
| 2092 | } |
| 2093 | |
Mark Greer | a34631c | 2017-04-25 15:43:53 -0700 | [diff] [blame] | 2094 | uvolts = regulator_get_voltage(trf->regulator); |
Mark A. Greer | ebcc5a0 | 2014-03-25 08:54:36 -0700 | [diff] [blame] | 2095 | if (uvolts > 4000000) |
| 2096 | trf->chip_status_ctrl = TRF7970A_CHIP_STATUS_VRS5_3; |
| 2097 | |
Geoff Lansberry | 49d22c7 | 2016-12-21 23:18:33 -0500 | [diff] [blame] | 2098 | trf->regulator = devm_regulator_get(&spi->dev, "vdd-io"); |
| 2099 | if (IS_ERR(trf->regulator)) { |
| 2100 | ret = PTR_ERR(trf->regulator); |
| 2101 | dev_err(trf->dev, "Can't get VDD_IO regulator: %d\n", ret); |
| 2102 | goto err_destroy_lock; |
| 2103 | } |
| 2104 | |
| 2105 | ret = regulator_enable(trf->regulator); |
| 2106 | if (ret) { |
| 2107 | dev_err(trf->dev, "Can't enable VDD_IO: %d\n", ret); |
| 2108 | goto err_destroy_lock; |
| 2109 | } |
| 2110 | |
| 2111 | if (regulator_get_voltage(trf->regulator) == 1800000) { |
| 2112 | trf->io_ctrl = TRF7970A_REG_IO_CTRL_IO_LOW; |
| 2113 | dev_dbg(trf->dev, "trf7970a config vdd_io to 1.8V\n"); |
| 2114 | } |
| 2115 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 2116 | trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 2117 | TRF7970A_SUPPORTED_PROTOCOLS, |
| 2118 | NFC_DIGITAL_DRV_CAPS_IN_CRC | |
| 2119 | NFC_DIGITAL_DRV_CAPS_TG_CRC, 0, |
| 2120 | 0); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 2121 | if (!trf->ddev) { |
| 2122 | dev_err(trf->dev, "Can't allocate NFC digital device\n"); |
| 2123 | ret = -ENOMEM; |
| 2124 | goto err_disable_regulator; |
| 2125 | } |
| 2126 | |
| 2127 | nfc_digital_set_parent_dev(trf->ddev, trf->dev); |
| 2128 | nfc_digital_set_drvdata(trf->ddev, trf); |
| 2129 | spi_set_drvdata(spi, trf); |
| 2130 | |
Mark A. Greer | fd0c828 | 2014-03-25 08:54:40 -0700 | [diff] [blame] | 2131 | autosuspend_delay = trf7970a_get_autosuspend_delay(np); |
| 2132 | |
| 2133 | pm_runtime_set_autosuspend_delay(trf->dev, autosuspend_delay); |
Mark A. Greer | e6403b7 | 2014-03-25 08:54:38 -0700 | [diff] [blame] | 2134 | pm_runtime_use_autosuspend(trf->dev); |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 2135 | |
Mark A. Greer | b528281 | 2014-09-02 15:12:36 -0700 | [diff] [blame] | 2136 | ret = trf7970a_startup(trf); |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 2137 | if (ret) |
| 2138 | goto err_free_ddev; |
| 2139 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 2140 | ret = nfc_digital_register_device(trf->ddev); |
| 2141 | if (ret) { |
| 2142 | dev_err(trf->dev, "Can't register NFC digital device: %d\n", |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 2143 | ret); |
Mark A. Greer | b528281 | 2014-09-02 15:12:36 -0700 | [diff] [blame] | 2144 | goto err_shutdown; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | return 0; |
| 2148 | |
Mark A. Greer | b528281 | 2014-09-02 15:12:36 -0700 | [diff] [blame] | 2149 | err_shutdown: |
| 2150 | trf7970a_shutdown(trf); |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 2151 | err_free_ddev: |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 2152 | nfc_digital_free_device(trf->ddev); |
| 2153 | err_disable_regulator: |
| 2154 | regulator_disable(trf->regulator); |
| 2155 | err_destroy_lock: |
| 2156 | mutex_destroy(&trf->lock); |
| 2157 | return ret; |
| 2158 | } |
| 2159 | |
| 2160 | static int trf7970a_remove(struct spi_device *spi) |
| 2161 | { |
| 2162 | struct trf7970a *trf = spi_get_drvdata(spi); |
| 2163 | |
| 2164 | mutex_lock(&trf->lock); |
| 2165 | |
Mark A. Greer | b528281 | 2014-09-02 15:12:36 -0700 | [diff] [blame] | 2166 | trf7970a_shutdown(trf); |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 2167 | |
| 2168 | mutex_unlock(&trf->lock); |
Mark A. Greer | e6403b7 | 2014-03-25 08:54:38 -0700 | [diff] [blame] | 2169 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 2170 | nfc_digital_unregister_device(trf->ddev); |
| 2171 | nfc_digital_free_device(trf->ddev); |
| 2172 | |
| 2173 | regulator_disable(trf->regulator); |
| 2174 | |
| 2175 | mutex_destroy(&trf->lock); |
| 2176 | |
| 2177 | return 0; |
| 2178 | } |
| 2179 | |
Mark A. Greer | 77c9539 | 2014-09-02 15:12:37 -0700 | [diff] [blame] | 2180 | #ifdef CONFIG_PM_SLEEP |
| 2181 | static int trf7970a_suspend(struct device *dev) |
| 2182 | { |
Geliang Tang | 88e2ce0 | 2015-12-23 00:18:42 +0800 | [diff] [blame] | 2183 | struct spi_device *spi = to_spi_device(dev); |
Mark A. Greer | 77c9539 | 2014-09-02 15:12:37 -0700 | [diff] [blame] | 2184 | struct trf7970a *trf = spi_get_drvdata(spi); |
Mark A. Greer | 77c9539 | 2014-09-02 15:12:37 -0700 | [diff] [blame] | 2185 | |
| 2186 | dev_dbg(dev, "Suspend\n"); |
| 2187 | |
| 2188 | mutex_lock(&trf->lock); |
| 2189 | |
| 2190 | trf7970a_shutdown(trf); |
| 2191 | |
| 2192 | mutex_unlock(&trf->lock); |
| 2193 | |
Mark A. Greer | 671970f | 2014-09-23 11:25:45 -0700 | [diff] [blame] | 2194 | return 0; |
Mark A. Greer | 77c9539 | 2014-09-02 15:12:37 -0700 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | static int trf7970a_resume(struct device *dev) |
| 2198 | { |
Geliang Tang | 88e2ce0 | 2015-12-23 00:18:42 +0800 | [diff] [blame] | 2199 | struct spi_device *spi = to_spi_device(dev); |
Mark A. Greer | 77c9539 | 2014-09-02 15:12:37 -0700 | [diff] [blame] | 2200 | struct trf7970a *trf = spi_get_drvdata(spi); |
Mark A. Greer | 55ef2e7 | 2014-09-23 11:25:46 -0700 | [diff] [blame] | 2201 | int ret; |
Mark A. Greer | 77c9539 | 2014-09-02 15:12:37 -0700 | [diff] [blame] | 2202 | |
| 2203 | dev_dbg(dev, "Resume\n"); |
| 2204 | |
| 2205 | mutex_lock(&trf->lock); |
| 2206 | |
| 2207 | ret = trf7970a_startup(trf); |
| 2208 | |
| 2209 | mutex_unlock(&trf->lock); |
| 2210 | |
| 2211 | return ret; |
| 2212 | } |
| 2213 | #endif |
| 2214 | |
Rafael J. Wysocki | e8a9235 | 2014-12-14 23:14:36 +0100 | [diff] [blame] | 2215 | #ifdef CONFIG_PM |
Mark A. Greer | e6403b7 | 2014-03-25 08:54:38 -0700 | [diff] [blame] | 2216 | static int trf7970a_pm_runtime_suspend(struct device *dev) |
| 2217 | { |
Geliang Tang | 88e2ce0 | 2015-12-23 00:18:42 +0800 | [diff] [blame] | 2218 | struct spi_device *spi = to_spi_device(dev); |
Mark A. Greer | e6403b7 | 2014-03-25 08:54:38 -0700 | [diff] [blame] | 2219 | struct trf7970a *trf = spi_get_drvdata(spi); |
| 2220 | int ret; |
| 2221 | |
| 2222 | dev_dbg(dev, "Runtime suspend\n"); |
| 2223 | |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 2224 | mutex_lock(&trf->lock); |
Mark A. Greer | e6403b7 | 2014-03-25 08:54:38 -0700 | [diff] [blame] | 2225 | |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 2226 | ret = trf7970a_power_down(trf); |
Mark A. Greer | e6403b7 | 2014-03-25 08:54:38 -0700 | [diff] [blame] | 2227 | |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 2228 | mutex_unlock(&trf->lock); |
Mark A. Greer | e6403b7 | 2014-03-25 08:54:38 -0700 | [diff] [blame] | 2229 | |
| 2230 | return ret; |
| 2231 | } |
| 2232 | |
| 2233 | static int trf7970a_pm_runtime_resume(struct device *dev) |
| 2234 | { |
Geliang Tang | 88e2ce0 | 2015-12-23 00:18:42 +0800 | [diff] [blame] | 2235 | struct spi_device *spi = to_spi_device(dev); |
Mark A. Greer | e6403b7 | 2014-03-25 08:54:38 -0700 | [diff] [blame] | 2236 | struct trf7970a *trf = spi_get_drvdata(spi); |
| 2237 | int ret; |
| 2238 | |
| 2239 | dev_dbg(dev, "Runtime resume\n"); |
| 2240 | |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 2241 | ret = trf7970a_power_up(trf); |
| 2242 | if (!ret) |
| 2243 | pm_runtime_mark_last_busy(dev); |
Mark A. Greer | e6403b7 | 2014-03-25 08:54:38 -0700 | [diff] [blame] | 2244 | |
Mark A. Greer | ceccd6a | 2014-09-02 15:12:35 -0700 | [diff] [blame] | 2245 | return ret; |
Mark A. Greer | e6403b7 | 2014-03-25 08:54:38 -0700 | [diff] [blame] | 2246 | } |
| 2247 | #endif |
| 2248 | |
| 2249 | static const struct dev_pm_ops trf7970a_pm_ops = { |
Mark A. Greer | 77c9539 | 2014-09-02 15:12:37 -0700 | [diff] [blame] | 2250 | SET_SYSTEM_SLEEP_PM_OPS(trf7970a_suspend, trf7970a_resume) |
Mark A. Greer | e6403b7 | 2014-03-25 08:54:38 -0700 | [diff] [blame] | 2251 | SET_RUNTIME_PM_OPS(trf7970a_pm_runtime_suspend, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 2252 | trf7970a_pm_runtime_resume, NULL) |
Mark A. Greer | e6403b7 | 2014-03-25 08:54:38 -0700 | [diff] [blame] | 2253 | }; |
| 2254 | |
Javier Martinez Canillas | 3c39c1a | 2015-09-16 11:08:42 +0200 | [diff] [blame] | 2255 | static const struct of_device_id trf7970a_of_match[] = { |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 2256 | {.compatible = "ti,trf7970a",}, |
Mark Greer | fcc652f | 2017-04-25 15:43:52 -0700 | [diff] [blame] | 2257 | {}, |
Javier Martinez Canillas | 3c39c1a | 2015-09-16 11:08:42 +0200 | [diff] [blame] | 2258 | }; |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 2259 | |
Javier Martinez Canillas | 3c39c1a | 2015-09-16 11:08:42 +0200 | [diff] [blame] | 2260 | MODULE_DEVICE_TABLE(of, trf7970a_of_match); |
| 2261 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 2262 | static const struct spi_device_id trf7970a_id_table[] = { |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 2263 | {"trf7970a", 0}, |
| 2264 | {} |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 2265 | }; |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 2266 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 2267 | MODULE_DEVICE_TABLE(spi, trf7970a_id_table); |
| 2268 | |
| 2269 | static struct spi_driver trf7970a_spi_driver = { |
| 2270 | .probe = trf7970a_probe, |
| 2271 | .remove = trf7970a_remove, |
| 2272 | .id_table = trf7970a_id_table, |
Mark Greer | e2f0f67 | 2017-04-25 15:43:56 -0700 | [diff] [blame] | 2273 | .driver = { |
| 2274 | .name = "trf7970a", |
| 2275 | .of_match_table = of_match_ptr(trf7970a_of_match), |
| 2276 | .pm = &trf7970a_pm_ops, |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 2277 | }, |
| 2278 | }; |
| 2279 | |
| 2280 | module_spi_driver(trf7970a_spi_driver); |
| 2281 | |
| 2282 | MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>"); |
| 2283 | MODULE_LICENSE("GPL v2"); |
| 2284 | MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver"); |