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