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