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