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> |
| 19 | #include <linux/nfc.h> |
| 20 | #include <linux/skbuff.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/gpio.h> |
| 23 | #include <linux/of.h> |
| 24 | #include <linux/of_gpio.h> |
| 25 | #include <linux/spi/spi.h> |
| 26 | #include <linux/regulator/consumer.h> |
| 27 | |
| 28 | #include <net/nfc/nfc.h> |
| 29 | #include <net/nfc/digital.h> |
| 30 | |
| 31 | /* There are 3 ways the host can communicate with the trf7970a: |
| 32 | * parallel mode, SPI with Slave Select (SS) mode, and SPI without |
| 33 | * SS mode. The driver only supports the two SPI modes. |
| 34 | * |
| 35 | * The trf7970a is very timing sensitive and the VIN, EN2, and EN |
| 36 | * pins must asserted in that order and with specific delays in between. |
| 37 | * The delays used in the driver were provided by TI and have been |
| 38 | * confirmed to work with this driver. |
| 39 | * |
| 40 | * Timeouts are implemented using the delayed workqueue kernel facility. |
| 41 | * Timeouts are required so things don't hang when there is no response |
| 42 | * from the trf7970a (or tag). Using this mechanism creates a race with |
| 43 | * interrupts, however. That is, an interrupt and a timeout could occur |
| 44 | * closely enough together that one is blocked by the mutex while the other |
| 45 | * executes. When the timeout handler executes first and blocks the |
| 46 | * interrupt handler, it will eventually set the state to IDLE so the |
| 47 | * interrupt handler will check the state and exit with no harm done. |
| 48 | * When the interrupt handler executes first and blocks the timeout handler, |
| 49 | * the cancel_delayed_work() call will know that it didn't cancel the |
| 50 | * work item (i.e., timeout) and will return zero. That return code is |
| 51 | * used by the timer handler to indicate that it should ignore the timeout |
| 52 | * once its unblocked. |
| 53 | * |
| 54 | * Aborting an active command isn't as simple as it seems because the only |
| 55 | * way to abort a command that's already been sent to the tag is so turn |
| 56 | * off power to the tag. If we do that, though, we'd have to go through |
| 57 | * the entire anticollision procedure again but the digital layer doesn't |
| 58 | * support that. So, if an abort is received before trf7970a_in_send_cmd() |
| 59 | * has sent the command to the tag, it simply returns -ECANCELED. If the |
| 60 | * command has already been sent to the tag, then the driver continues |
| 61 | * normally and recieves the response data (or error) but just before |
| 62 | * sending the data upstream, it frees the rx_skb and sends -ECANCELED |
| 63 | * upstream instead. If the command failed, that error will be sent |
| 64 | * upstream. |
| 65 | * |
| 66 | * When recieving data from a tag and the interrupt status register has |
| 67 | * only the SRX bit set, it means that all of the data has been received |
| 68 | * (once what's in the fifo has been read). However, depending on timing |
| 69 | * 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] | 70 | * those cases, the timeout mechanism is used to wait 20 ms in case more |
| 71 | * 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] | 72 | * received and the accumulated rx data is sent upstream. The |
| 73 | * 'TRF7970A_ST_WAIT_FOR_RX_DATA_CONT' state is used for this purpose |
| 74 | * (i.e., it indicates that some data has been received but we're not sure |
| 75 | * 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] | 76 | * been received and there isn't an error). The delay is 20 ms since delays |
| 77 | * of ~16 ms have been observed during testing. |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 78 | * |
| 79 | * Type 2 write and sector select commands respond with a 4-bit ACK or NACK. |
| 80 | * Having only 4 bits in the FIFO won't normally generate an interrupt so |
| 81 | * driver enables the '4_bit_RX' bit of the Special Functions register 1 |
| 82 | * to cause an interrupt in that case. Leaving that bit for a read command |
| 83 | * messes up the data returned so it is only enabled when the framing is |
| 84 | * 'NFC_DIGITAL_FRAMING_NFCA_T2T' and the command is not a read command. |
| 85 | * Unfortunately, that means that the driver has to peek into tx frames |
| 86 | * when the framing is 'NFC_DIGITAL_FRAMING_NFCA_T2T'. This is done by |
| 87 | * the trf7970a_per_cmd_config() routine. |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 88 | * |
| 89 | * ISO/IEC 15693 frames specify whether to use single or double sub-carrier |
| 90 | * frequencies and whether to use low or high data rates in the flags byte |
| 91 | * of the frame. This means that the driver has to peek at all 15693 frames |
| 92 | * to determine what speed to set the communication to. In addition, write |
| 93 | * and lock commands use the OPTION flag to indicate that an EOF must be |
| 94 | * sent to the tag before it will send its response. So the driver has to |
| 95 | * examine all frames for that reason too. |
| 96 | * |
| 97 | * It is unclear how long to wait before sending the EOF. According to the |
| 98 | * Note under Table 1-1 in section 1.6 of |
| 99 | * http://www.ti.com/lit/ug/scbu011/scbu011.pdf, that wait should be at least |
| 100 | * 10 ms for TI Tag-it HF-I tags; however testing has shown that is not long |
| 101 | * enough. For this reason, the driver waits 20 ms which seems to work |
| 102 | * reliably. |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 103 | */ |
| 104 | |
Mark A. Greer | 8006289 | 2014-03-10 11:56:23 -0700 | [diff] [blame] | 105 | #define TRF7970A_SUPPORTED_PROTOCOLS \ |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 106 | (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \ |
| 107 | NFC_PROTO_ISO15693_MASK) |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 108 | |
| 109 | /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends |
| 110 | * on what the current framing is, the address of the TX length byte 1 |
| 111 | * register (0x1d), and the 2 byte length of the data to be transmitted. |
| 112 | * That totals 5 bytes. |
| 113 | */ |
| 114 | #define TRF7970A_TX_SKB_HEADROOM 5 |
| 115 | |
| 116 | #define TRF7970A_RX_SKB_ALLOC_SIZE 256 |
| 117 | |
| 118 | #define TRF7970A_FIFO_SIZE 128 |
| 119 | |
| 120 | /* TX length is 3 nibbles long ==> 4KB - 1 bytes max */ |
| 121 | #define TRF7970A_TX_MAX (4096 - 1) |
| 122 | |
Mark A. Greer | 5fa3af3 | 2014-03-25 08:54:29 -0700 | [diff] [blame] | 123 | #define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 20 |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 124 | #define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 3 |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 125 | #define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 20 |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 126 | |
| 127 | /* Quirks */ |
| 128 | /* Erratum: When reading IRQ Status register on trf7970a, we must issue a |
| 129 | * read continuous command for IRQ Status and Collision Position registers. |
| 130 | */ |
| 131 | #define TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA BIT(0) |
| 132 | |
| 133 | /* Direct commands */ |
| 134 | #define TRF7970A_CMD_IDLE 0x00 |
| 135 | #define TRF7970A_CMD_SOFT_INIT 0x03 |
| 136 | #define TRF7970A_CMD_RF_COLLISION 0x04 |
| 137 | #define TRF7970A_CMD_RF_COLLISION_RESPONSE_N 0x05 |
| 138 | #define TRF7970A_CMD_RF_COLLISION_RESPONSE_0 0x06 |
| 139 | #define TRF7970A_CMD_FIFO_RESET 0x0f |
| 140 | #define TRF7970A_CMD_TRANSMIT_NO_CRC 0x10 |
| 141 | #define TRF7970A_CMD_TRANSMIT 0x11 |
| 142 | #define TRF7970A_CMD_DELAY_TRANSMIT_NO_CRC 0x12 |
| 143 | #define TRF7970A_CMD_DELAY_TRANSMIT 0x13 |
| 144 | #define TRF7970A_CMD_EOF 0x14 |
| 145 | #define TRF7970A_CMD_CLOSE_SLOT 0x15 |
| 146 | #define TRF7970A_CMD_BLOCK_RX 0x16 |
| 147 | #define TRF7970A_CMD_ENABLE_RX 0x17 |
| 148 | #define TRF7970A_CMD_TEST_EXT_RF 0x18 |
| 149 | #define TRF7970A_CMD_TEST_INT_RF 0x19 |
| 150 | #define TRF7970A_CMD_RX_GAIN_ADJUST 0x1a |
| 151 | |
| 152 | /* Bits determining whether its a direct command or register R/W, |
| 153 | * whether to use a continuous SPI transaction or not, and the actual |
| 154 | * direct cmd opcode or regster address. |
| 155 | */ |
| 156 | #define TRF7970A_CMD_BIT_CTRL BIT(7) |
| 157 | #define TRF7970A_CMD_BIT_RW BIT(6) |
| 158 | #define TRF7970A_CMD_BIT_CONTINUOUS BIT(5) |
| 159 | #define TRF7970A_CMD_BIT_OPCODE(opcode) ((opcode) & 0x1f) |
| 160 | |
| 161 | /* Registers addresses */ |
| 162 | #define TRF7970A_CHIP_STATUS_CTRL 0x00 |
| 163 | #define TRF7970A_ISO_CTRL 0x01 |
| 164 | #define TRF7970A_ISO14443B_TX_OPTIONS 0x02 |
| 165 | #define TRF7970A_ISO14443A_HIGH_BITRATE_OPTIONS 0x03 |
| 166 | #define TRF7970A_TX_TIMER_SETTING_H_BYTE 0x04 |
| 167 | #define TRF7970A_TX_TIMER_SETTING_L_BYTE 0x05 |
| 168 | #define TRF7970A_TX_PULSE_LENGTH_CTRL 0x06 |
| 169 | #define TRF7970A_RX_NO_RESPONSE_WAIT 0x07 |
| 170 | #define TRF7970A_RX_WAIT_TIME 0x08 |
| 171 | #define TRF7970A_MODULATOR_SYS_CLK_CTRL 0x09 |
| 172 | #define TRF7970A_RX_SPECIAL_SETTINGS 0x0a |
| 173 | #define TRF7970A_REG_IO_CTRL 0x0b |
| 174 | #define TRF7970A_IRQ_STATUS 0x0c |
| 175 | #define TRF7970A_COLLISION_IRQ_MASK 0x0d |
| 176 | #define TRF7970A_COLLISION_POSITION 0x0e |
| 177 | #define TRF7970A_RSSI_OSC_STATUS 0x0f |
| 178 | #define TRF7970A_SPECIAL_FCN_REG1 0x10 |
| 179 | #define TRF7970A_SPECIAL_FCN_REG2 0x11 |
| 180 | #define TRF7970A_RAM1 0x12 |
| 181 | #define TRF7970A_RAM2 0x13 |
| 182 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS 0x14 |
| 183 | #define TRF7970A_NFC_LOW_FIELD_LEVEL 0x16 |
| 184 | #define TRF7970A_NFCID1 0x17 |
| 185 | #define TRF7970A_NFC_TARGET_LEVEL 0x18 |
| 186 | #define TRF79070A_NFC_TARGET_PROTOCOL 0x19 |
| 187 | #define TRF7970A_TEST_REGISTER1 0x1a |
| 188 | #define TRF7970A_TEST_REGISTER2 0x1b |
| 189 | #define TRF7970A_FIFO_STATUS 0x1c |
| 190 | #define TRF7970A_TX_LENGTH_BYTE1 0x1d |
| 191 | #define TRF7970A_TX_LENGTH_BYTE2 0x1e |
| 192 | #define TRF7970A_FIFO_IO_REGISTER 0x1f |
| 193 | |
| 194 | /* Chip Status Control Register Bits */ |
| 195 | #define TRF7970A_CHIP_STATUS_VRS5_3 BIT(0) |
| 196 | #define TRF7970A_CHIP_STATUS_REC_ON BIT(1) |
| 197 | #define TRF7970A_CHIP_STATUS_AGC_ON BIT(2) |
| 198 | #define TRF7970A_CHIP_STATUS_PM_ON BIT(3) |
| 199 | #define TRF7970A_CHIP_STATUS_RF_PWR BIT(4) |
| 200 | #define TRF7970A_CHIP_STATUS_RF_ON BIT(5) |
| 201 | #define TRF7970A_CHIP_STATUS_DIRECT BIT(6) |
| 202 | #define TRF7970A_CHIP_STATUS_STBY BIT(7) |
| 203 | |
| 204 | /* ISO Control Register Bits */ |
| 205 | #define TRF7970A_ISO_CTRL_15693_SGL_1OF4_662 0x00 |
| 206 | #define TRF7970A_ISO_CTRL_15693_SGL_1OF256_662 0x01 |
| 207 | #define TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648 0x02 |
| 208 | #define TRF7970A_ISO_CTRL_15693_SGL_1OF256_2648 0x03 |
| 209 | #define TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a 0x04 |
| 210 | #define TRF7970A_ISO_CTRL_15693_DBL_1OF256_667 0x05 |
| 211 | #define TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669 0x06 |
| 212 | #define TRF7970A_ISO_CTRL_15693_DBL_1OF256_2669 0x07 |
| 213 | #define TRF7970A_ISO_CTRL_14443A_106 0x08 |
| 214 | #define TRF7970A_ISO_CTRL_14443A_212 0x09 |
| 215 | #define TRF7970A_ISO_CTRL_14443A_424 0x0a |
| 216 | #define TRF7970A_ISO_CTRL_14443A_848 0x0b |
| 217 | #define TRF7970A_ISO_CTRL_14443B_106 0x0c |
| 218 | #define TRF7970A_ISO_CTRL_14443B_212 0x0d |
| 219 | #define TRF7970A_ISO_CTRL_14443B_424 0x0e |
| 220 | #define TRF7970A_ISO_CTRL_14443B_848 0x0f |
| 221 | #define TRF7970A_ISO_CTRL_FELICA_212 0x1a |
| 222 | #define TRF7970A_ISO_CTRL_FELICA_424 0x1b |
| 223 | #define TRF7970A_ISO_CTRL_RFID BIT(5) |
| 224 | #define TRF7970A_ISO_CTRL_DIR_MODE BIT(6) |
| 225 | #define TRF7970A_ISO_CTRL_RX_CRC_N BIT(7) /* true == No CRC */ |
| 226 | |
| 227 | #define TRF7970A_ISO_CTRL_RFID_SPEED_MASK 0x1f |
| 228 | |
| 229 | /* Modulator and SYS_CLK Control Register Bits */ |
| 230 | #define TRF7970A_MODULATOR_DEPTH(n) ((n) & 0x7) |
| 231 | #define TRF7970A_MODULATOR_DEPTH_ASK10 (TRF7970A_MODULATOR_DEPTH(0)) |
| 232 | #define TRF7970A_MODULATOR_DEPTH_OOK (TRF7970A_MODULATOR_DEPTH(1)) |
| 233 | #define TRF7970A_MODULATOR_DEPTH_ASK7 (TRF7970A_MODULATOR_DEPTH(2)) |
| 234 | #define TRF7970A_MODULATOR_DEPTH_ASK8_5 (TRF7970A_MODULATOR_DEPTH(3)) |
| 235 | #define TRF7970A_MODULATOR_DEPTH_ASK13 (TRF7970A_MODULATOR_DEPTH(4)) |
| 236 | #define TRF7970A_MODULATOR_DEPTH_ASK16 (TRF7970A_MODULATOR_DEPTH(5)) |
| 237 | #define TRF7970A_MODULATOR_DEPTH_ASK22 (TRF7970A_MODULATOR_DEPTH(6)) |
| 238 | #define TRF7970A_MODULATOR_DEPTH_ASK30 (TRF7970A_MODULATOR_DEPTH(7)) |
| 239 | #define TRF7970A_MODULATOR_EN_ANA BIT(3) |
| 240 | #define TRF7970A_MODULATOR_CLK(n) (((n) & 0x3) << 4) |
| 241 | #define TRF7970A_MODULATOR_CLK_DISABLED (TRF7970A_MODULATOR_CLK(0)) |
| 242 | #define TRF7970A_MODULATOR_CLK_3_6 (TRF7970A_MODULATOR_CLK(1)) |
| 243 | #define TRF7970A_MODULATOR_CLK_6_13 (TRF7970A_MODULATOR_CLK(2)) |
| 244 | #define TRF7970A_MODULATOR_CLK_13_27 (TRF7970A_MODULATOR_CLK(3)) |
| 245 | #define TRF7970A_MODULATOR_EN_OOK BIT(6) |
| 246 | #define TRF7970A_MODULATOR_27MHZ BIT(7) |
| 247 | |
| 248 | /* IRQ Status Register Bits */ |
| 249 | #define TRF7970A_IRQ_STATUS_NORESP BIT(0) /* ISO15693 only */ |
| 250 | #define TRF7970A_IRQ_STATUS_COL BIT(1) |
| 251 | #define TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR BIT(2) |
| 252 | #define TRF7970A_IRQ_STATUS_PARITY_ERROR BIT(3) |
| 253 | #define TRF7970A_IRQ_STATUS_CRC_ERROR BIT(4) |
| 254 | #define TRF7970A_IRQ_STATUS_FIFO BIT(5) |
| 255 | #define TRF7970A_IRQ_STATUS_SRX BIT(6) |
| 256 | #define TRF7970A_IRQ_STATUS_TX BIT(7) |
| 257 | |
| 258 | #define TRF7970A_IRQ_STATUS_ERROR \ |
| 259 | (TRF7970A_IRQ_STATUS_COL | \ |
| 260 | TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR | \ |
| 261 | TRF7970A_IRQ_STATUS_PARITY_ERROR | \ |
| 262 | TRF7970A_IRQ_STATUS_CRC_ERROR) |
| 263 | |
| 264 | #define TRF7970A_SPECIAL_FCN_REG1_COL_7_6 BIT(0) |
| 265 | #define TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL BIT(1) |
| 266 | #define TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX BIT(2) |
| 267 | #define TRF7970A_SPECIAL_FCN_REG1_SP_DIR_MODE BIT(3) |
| 268 | #define TRF7970A_SPECIAL_FCN_REG1_NEXT_SLOT_37US BIT(4) |
| 269 | #define TRF7970A_SPECIAL_FCN_REG1_PAR43 BIT(5) |
| 270 | |
| 271 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_124 (0x0 << 2) |
| 272 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_120 (0x1 << 2) |
| 273 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_112 (0x2 << 2) |
| 274 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 (0x3 << 2) |
| 275 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_4 0x0 |
| 276 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_8 0x1 |
| 277 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_16 0x2 |
| 278 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32 0x3 |
| 279 | |
| 280 | #define TRF7970A_FIFO_STATUS_OVERFLOW BIT(7) |
| 281 | |
| 282 | /* NFC (ISO/IEC 14443A) Type 2 Tag commands */ |
| 283 | #define NFC_T2T_CMD_READ 0x30 |
| 284 | |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 285 | /* ISO 15693 commands codes */ |
| 286 | #define ISO15693_CMD_INVENTORY 0x01 |
| 287 | #define ISO15693_CMD_READ_SINGLE_BLOCK 0x20 |
| 288 | #define ISO15693_CMD_WRITE_SINGLE_BLOCK 0x21 |
| 289 | #define ISO15693_CMD_LOCK_BLOCK 0x22 |
| 290 | #define ISO15693_CMD_READ_MULTIPLE_BLOCK 0x23 |
| 291 | #define ISO15693_CMD_WRITE_MULTIPLE_BLOCK 0x24 |
| 292 | #define ISO15693_CMD_SELECT 0x25 |
| 293 | #define ISO15693_CMD_RESET_TO_READY 0x26 |
| 294 | #define ISO15693_CMD_WRITE_AFI 0x27 |
| 295 | #define ISO15693_CMD_LOCK_AFI 0x28 |
| 296 | #define ISO15693_CMD_WRITE_DSFID 0x29 |
| 297 | #define ISO15693_CMD_LOCK_DSFID 0x2a |
| 298 | #define ISO15693_CMD_GET_SYSTEM_INFO 0x2b |
| 299 | #define ISO15693_CMD_GET_MULTIPLE_BLOCK_SECURITY_STATUS 0x2c |
| 300 | |
| 301 | /* ISO 15693 request and response flags */ |
| 302 | #define ISO15693_REQ_FLAG_SUB_CARRIER BIT(0) |
| 303 | #define ISO15693_REQ_FLAG_DATA_RATE BIT(1) |
| 304 | #define ISO15693_REQ_FLAG_INVENTORY BIT(2) |
| 305 | #define ISO15693_REQ_FLAG_PROTOCOL_EXT BIT(3) |
| 306 | #define ISO15693_REQ_FLAG_SELECT BIT(4) |
| 307 | #define ISO15693_REQ_FLAG_AFI BIT(4) |
| 308 | #define ISO15693_REQ_FLAG_ADDRESS BIT(5) |
| 309 | #define ISO15693_REQ_FLAG_NB_SLOTS BIT(5) |
| 310 | #define ISO15693_REQ_FLAG_OPTION BIT(6) |
| 311 | |
| 312 | #define ISO15693_REQ_FLAG_SPEED_MASK \ |
| 313 | (ISO15693_REQ_FLAG_SUB_CARRIER | ISO15693_REQ_FLAG_DATA_RATE) |
| 314 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 315 | enum trf7970a_state { |
| 316 | TRF7970A_ST_OFF, |
| 317 | TRF7970A_ST_IDLE, |
| 318 | TRF7970A_ST_IDLE_RX_BLOCKED, |
| 319 | TRF7970A_ST_WAIT_FOR_TX_FIFO, |
| 320 | TRF7970A_ST_WAIT_FOR_RX_DATA, |
| 321 | TRF7970A_ST_WAIT_FOR_RX_DATA_CONT, |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 322 | TRF7970A_ST_WAIT_TO_ISSUE_EOF, |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 323 | TRF7970A_ST_MAX |
| 324 | }; |
| 325 | |
| 326 | struct trf7970a { |
| 327 | enum trf7970a_state state; |
| 328 | struct device *dev; |
| 329 | struct spi_device *spi; |
| 330 | struct regulator *regulator; |
| 331 | struct nfc_digital_dev *ddev; |
| 332 | u32 quirks; |
| 333 | bool powering_up; |
| 334 | bool aborting; |
| 335 | struct sk_buff *tx_skb; |
| 336 | struct sk_buff *rx_skb; |
| 337 | nfc_digital_cmd_complete_t cb; |
| 338 | void *cb_arg; |
| 339 | u8 iso_ctrl; |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 340 | u8 iso_ctrl_tech; |
Mark A. Greer | 12e9ade | 2014-03-25 08:54:35 -0700 | [diff] [blame^] | 341 | u8 modulator_sys_clk_ctrl; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 342 | u8 special_fcn_reg1; |
| 343 | int technology; |
| 344 | int framing; |
| 345 | u8 tx_cmd; |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 346 | bool issue_eof; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 347 | int en2_gpio; |
| 348 | int en_gpio; |
| 349 | struct mutex lock; |
| 350 | unsigned int timeout; |
| 351 | bool ignore_timeout; |
| 352 | struct delayed_work timeout_work; |
| 353 | }; |
| 354 | |
| 355 | |
| 356 | static int trf7970a_cmd(struct trf7970a *trf, u8 opcode) |
| 357 | { |
| 358 | u8 cmd = TRF7970A_CMD_BIT_CTRL | TRF7970A_CMD_BIT_OPCODE(opcode); |
| 359 | int ret; |
| 360 | |
| 361 | dev_dbg(trf->dev, "cmd: 0x%x\n", cmd); |
| 362 | |
| 363 | ret = spi_write(trf->spi, &cmd, 1); |
| 364 | if (ret) |
| 365 | dev_err(trf->dev, "%s - cmd: 0x%x, ret: %d\n", __func__, cmd, |
| 366 | ret); |
| 367 | return ret; |
| 368 | } |
| 369 | |
| 370 | static int trf7970a_read(struct trf7970a *trf, u8 reg, u8 *val) |
| 371 | { |
| 372 | u8 addr = TRF7970A_CMD_BIT_RW | reg; |
| 373 | int ret; |
| 374 | |
| 375 | ret = spi_write_then_read(trf->spi, &addr, 1, val, 1); |
| 376 | if (ret) |
| 377 | dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr, |
| 378 | ret); |
| 379 | |
| 380 | dev_dbg(trf->dev, "read(0x%x): 0x%x\n", addr, *val); |
| 381 | |
| 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | static int trf7970a_read_cont(struct trf7970a *trf, u8 reg, |
| 386 | u8 *buf, size_t len) |
| 387 | { |
| 388 | u8 addr = reg | TRF7970A_CMD_BIT_RW | TRF7970A_CMD_BIT_CONTINUOUS; |
| 389 | int ret; |
| 390 | |
| 391 | dev_dbg(trf->dev, "read_cont(0x%x, %zd)\n", addr, len); |
| 392 | |
| 393 | ret = spi_write_then_read(trf->spi, &addr, 1, buf, len); |
| 394 | if (ret) |
| 395 | dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr, |
| 396 | ret); |
| 397 | return ret; |
| 398 | } |
| 399 | |
| 400 | static int trf7970a_write(struct trf7970a *trf, u8 reg, u8 val) |
| 401 | { |
| 402 | u8 buf[2] = { reg, val }; |
| 403 | int ret; |
| 404 | |
| 405 | dev_dbg(trf->dev, "write(0x%x): 0x%x\n", reg, val); |
| 406 | |
| 407 | ret = spi_write(trf->spi, buf, 2); |
| 408 | if (ret) |
| 409 | dev_err(trf->dev, "%s - write: 0x%x 0x%x, ret: %d\n", __func__, |
| 410 | buf[0], buf[1], ret); |
| 411 | |
| 412 | return ret; |
| 413 | } |
| 414 | |
| 415 | static int trf7970a_read_irqstatus(struct trf7970a *trf, u8 *status) |
| 416 | { |
| 417 | int ret; |
| 418 | u8 buf[2]; |
| 419 | u8 addr; |
| 420 | |
| 421 | addr = TRF7970A_IRQ_STATUS | TRF7970A_CMD_BIT_RW; |
| 422 | |
| 423 | if (trf->quirks & TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA) { |
| 424 | addr |= TRF7970A_CMD_BIT_CONTINUOUS; |
| 425 | ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2); |
| 426 | } else { |
| 427 | ret = spi_write_then_read(trf->spi, &addr, 1, buf, 1); |
| 428 | } |
| 429 | |
| 430 | if (ret) |
| 431 | dev_err(trf->dev, "%s - irqstatus: Status read failed: %d\n", |
| 432 | __func__, ret); |
| 433 | else |
| 434 | *status = buf[0]; |
| 435 | |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | static void trf7970a_send_upstream(struct trf7970a *trf) |
| 440 | { |
| 441 | u8 rssi; |
| 442 | |
| 443 | dev_kfree_skb_any(trf->tx_skb); |
| 444 | trf->tx_skb = NULL; |
| 445 | |
| 446 | if (trf->rx_skb && !IS_ERR(trf->rx_skb) && !trf->aborting) |
| 447 | print_hex_dump_debug("trf7970a rx data: ", DUMP_PREFIX_NONE, |
| 448 | 16, 1, trf->rx_skb->data, trf->rx_skb->len, |
| 449 | false); |
| 450 | |
| 451 | /* According to the manual it is "good form" to reset the fifo and |
| 452 | * read the RSSI levels & oscillator status register here. It doesn't |
| 453 | * explain why. |
| 454 | */ |
| 455 | trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); |
| 456 | trf7970a_read(trf, TRF7970A_RSSI_OSC_STATUS, &rssi); |
| 457 | |
| 458 | trf->state = TRF7970A_ST_IDLE; |
| 459 | |
| 460 | if (trf->aborting) { |
| 461 | dev_dbg(trf->dev, "Abort process complete\n"); |
| 462 | |
| 463 | if (!IS_ERR(trf->rx_skb)) { |
| 464 | kfree_skb(trf->rx_skb); |
| 465 | trf->rx_skb = ERR_PTR(-ECANCELED); |
| 466 | } |
| 467 | |
| 468 | trf->aborting = false; |
| 469 | } |
| 470 | |
| 471 | trf->cb(trf->ddev, trf->cb_arg, trf->rx_skb); |
| 472 | |
| 473 | trf->rx_skb = NULL; |
| 474 | } |
| 475 | |
| 476 | static void trf7970a_send_err_upstream(struct trf7970a *trf, int errno) |
| 477 | { |
| 478 | dev_dbg(trf->dev, "Error - state: %d, errno: %d\n", trf->state, errno); |
| 479 | |
| 480 | kfree_skb(trf->rx_skb); |
| 481 | trf->rx_skb = ERR_PTR(errno); |
| 482 | |
| 483 | trf7970a_send_upstream(trf); |
| 484 | } |
| 485 | |
| 486 | static int trf7970a_transmit(struct trf7970a *trf, struct sk_buff *skb, |
| 487 | unsigned int len) |
| 488 | { |
| 489 | unsigned int timeout; |
| 490 | int ret; |
| 491 | |
| 492 | print_hex_dump_debug("trf7970a tx data: ", DUMP_PREFIX_NONE, |
| 493 | 16, 1, skb->data, len, false); |
| 494 | |
| 495 | ret = spi_write(trf->spi, skb->data, len); |
| 496 | if (ret) { |
| 497 | dev_err(trf->dev, "%s - Can't send tx data: %d\n", __func__, |
| 498 | ret); |
| 499 | return ret; |
| 500 | } |
| 501 | |
| 502 | skb_pull(skb, len); |
| 503 | |
| 504 | if (skb->len > 0) { |
| 505 | trf->state = TRF7970A_ST_WAIT_FOR_TX_FIFO; |
| 506 | timeout = TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT; |
| 507 | } else { |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 508 | if (trf->issue_eof) { |
| 509 | trf->state = TRF7970A_ST_WAIT_TO_ISSUE_EOF; |
| 510 | timeout = TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF; |
| 511 | } else { |
| 512 | trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA; |
| 513 | timeout = trf->timeout; |
| 514 | } |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", timeout, |
| 518 | trf->state); |
| 519 | |
| 520 | schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout)); |
| 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | static void trf7970a_fill_fifo(struct trf7970a *trf) |
| 526 | { |
| 527 | struct sk_buff *skb = trf->tx_skb; |
| 528 | unsigned int len; |
| 529 | int ret; |
| 530 | u8 fifo_bytes; |
| 531 | |
| 532 | ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes); |
| 533 | if (ret) { |
| 534 | trf7970a_send_err_upstream(trf, ret); |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | dev_dbg(trf->dev, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes); |
| 539 | |
| 540 | if (fifo_bytes & TRF7970A_FIFO_STATUS_OVERFLOW) { |
| 541 | dev_err(trf->dev, "%s - fifo overflow: 0x%x\n", __func__, |
| 542 | fifo_bytes); |
| 543 | trf7970a_send_err_upstream(trf, -EIO); |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | /* Calculate how much more data can be written to the fifo */ |
| 548 | len = TRF7970A_FIFO_SIZE - fifo_bytes; |
| 549 | len = min(skb->len, len); |
| 550 | |
| 551 | ret = trf7970a_transmit(trf, skb, len); |
| 552 | if (ret) |
| 553 | trf7970a_send_err_upstream(trf, ret); |
| 554 | } |
| 555 | |
| 556 | static void trf7970a_drain_fifo(struct trf7970a *trf, u8 status) |
| 557 | { |
| 558 | struct sk_buff *skb = trf->rx_skb; |
| 559 | int ret; |
| 560 | u8 fifo_bytes; |
| 561 | |
| 562 | if (status & TRF7970A_IRQ_STATUS_ERROR) { |
| 563 | trf7970a_send_err_upstream(trf, -EIO); |
| 564 | return; |
| 565 | } |
| 566 | |
| 567 | ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes); |
| 568 | if (ret) { |
| 569 | trf7970a_send_err_upstream(trf, ret); |
| 570 | return; |
| 571 | } |
| 572 | |
| 573 | dev_dbg(trf->dev, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes); |
| 574 | |
| 575 | if (!fifo_bytes) |
| 576 | goto no_rx_data; |
| 577 | |
| 578 | if (fifo_bytes & TRF7970A_FIFO_STATUS_OVERFLOW) { |
| 579 | dev_err(trf->dev, "%s - fifo overflow: 0x%x\n", __func__, |
| 580 | fifo_bytes); |
| 581 | trf7970a_send_err_upstream(trf, -EIO); |
| 582 | return; |
| 583 | } |
| 584 | |
| 585 | if (fifo_bytes > skb_tailroom(skb)) { |
| 586 | skb = skb_copy_expand(skb, skb_headroom(skb), |
| 587 | max_t(int, fifo_bytes, |
| 588 | TRF7970A_RX_SKB_ALLOC_SIZE), |
| 589 | GFP_KERNEL); |
| 590 | if (!skb) { |
| 591 | trf7970a_send_err_upstream(trf, -ENOMEM); |
| 592 | return; |
| 593 | } |
| 594 | |
| 595 | kfree_skb(trf->rx_skb); |
| 596 | trf->rx_skb = skb; |
| 597 | } |
| 598 | |
| 599 | ret = trf7970a_read_cont(trf, TRF7970A_FIFO_IO_REGISTER, |
| 600 | skb_put(skb, fifo_bytes), fifo_bytes); |
| 601 | if (ret) { |
| 602 | trf7970a_send_err_upstream(trf, ret); |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | /* If received Type 2 ACK/NACK, shift right 4 bits and pass up */ |
| 607 | if ((trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T) && (skb->len == 1) && |
| 608 | (trf->special_fcn_reg1 == |
| 609 | TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX)) { |
| 610 | skb->data[0] >>= 4; |
| 611 | status = TRF7970A_IRQ_STATUS_SRX; |
| 612 | } else { |
| 613 | trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA_CONT; |
| 614 | } |
| 615 | |
| 616 | no_rx_data: |
| 617 | if (status == TRF7970A_IRQ_STATUS_SRX) { /* Receive complete */ |
| 618 | trf7970a_send_upstream(trf); |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | dev_dbg(trf->dev, "Setting timeout for %d ms\n", |
| 623 | TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT); |
| 624 | |
| 625 | schedule_delayed_work(&trf->timeout_work, |
| 626 | msecs_to_jiffies(TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT)); |
| 627 | } |
| 628 | |
| 629 | static irqreturn_t trf7970a_irq(int irq, void *dev_id) |
| 630 | { |
| 631 | struct trf7970a *trf = dev_id; |
| 632 | int ret; |
| 633 | u8 status; |
| 634 | |
| 635 | mutex_lock(&trf->lock); |
| 636 | |
| 637 | if (trf->state == TRF7970A_ST_OFF) { |
| 638 | mutex_unlock(&trf->lock); |
| 639 | return IRQ_NONE; |
| 640 | } |
| 641 | |
| 642 | ret = trf7970a_read_irqstatus(trf, &status); |
| 643 | if (ret) { |
| 644 | mutex_unlock(&trf->lock); |
| 645 | return IRQ_NONE; |
| 646 | } |
| 647 | |
| 648 | dev_dbg(trf->dev, "IRQ - state: %d, status: 0x%x\n", trf->state, |
| 649 | status); |
| 650 | |
| 651 | if (!status) { |
| 652 | mutex_unlock(&trf->lock); |
| 653 | return IRQ_NONE; |
| 654 | } |
| 655 | |
| 656 | switch (trf->state) { |
| 657 | case TRF7970A_ST_IDLE: |
| 658 | case TRF7970A_ST_IDLE_RX_BLOCKED: |
| 659 | /* If getting interrupts caused by RF noise, turn off the |
| 660 | * receiver to avoid unnecessary interrupts. It will be |
| 661 | * turned back on in trf7970a_in_send_cmd() when the next |
| 662 | * command is issued. |
| 663 | */ |
| 664 | if (status & TRF7970A_IRQ_STATUS_ERROR) { |
| 665 | trf7970a_cmd(trf, TRF7970A_CMD_BLOCK_RX); |
| 666 | trf->state = TRF7970A_ST_IDLE_RX_BLOCKED; |
| 667 | } |
| 668 | |
| 669 | trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); |
| 670 | break; |
| 671 | case TRF7970A_ST_WAIT_FOR_TX_FIFO: |
| 672 | if (status & TRF7970A_IRQ_STATUS_TX) { |
| 673 | trf->ignore_timeout = |
| 674 | !cancel_delayed_work(&trf->timeout_work); |
| 675 | trf7970a_fill_fifo(trf); |
| 676 | } else { |
| 677 | trf7970a_send_err_upstream(trf, -EIO); |
| 678 | } |
| 679 | break; |
| 680 | case TRF7970A_ST_WAIT_FOR_RX_DATA: |
| 681 | case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: |
| 682 | if (status & TRF7970A_IRQ_STATUS_SRX) { |
| 683 | trf->ignore_timeout = |
| 684 | !cancel_delayed_work(&trf->timeout_work); |
| 685 | trf7970a_drain_fifo(trf, status); |
Mark A. Greer | 4dd836e | 2014-03-25 08:54:32 -0700 | [diff] [blame] | 686 | } else if (status == TRF7970A_IRQ_STATUS_TX) { |
| 687 | trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); |
| 688 | } else { |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 689 | trf7970a_send_err_upstream(trf, -EIO); |
| 690 | } |
| 691 | break; |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 692 | case TRF7970A_ST_WAIT_TO_ISSUE_EOF: |
| 693 | if (status != TRF7970A_IRQ_STATUS_TX) |
| 694 | trf7970a_send_err_upstream(trf, -EIO); |
| 695 | break; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 696 | default: |
| 697 | dev_err(trf->dev, "%s - Driver in invalid state: %d\n", |
| 698 | __func__, trf->state); |
| 699 | } |
| 700 | |
| 701 | mutex_unlock(&trf->lock); |
| 702 | return IRQ_HANDLED; |
| 703 | } |
| 704 | |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 705 | static void trf7970a_issue_eof(struct trf7970a *trf) |
| 706 | { |
| 707 | int ret; |
| 708 | |
| 709 | dev_dbg(trf->dev, "Issuing EOF\n"); |
| 710 | |
| 711 | ret = trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); |
| 712 | if (ret) |
| 713 | trf7970a_send_err_upstream(trf, ret); |
| 714 | |
| 715 | ret = trf7970a_cmd(trf, TRF7970A_CMD_EOF); |
| 716 | if (ret) |
| 717 | trf7970a_send_err_upstream(trf, ret); |
| 718 | |
| 719 | trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA; |
| 720 | |
| 721 | dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", |
| 722 | trf->timeout, trf->state); |
| 723 | |
| 724 | schedule_delayed_work(&trf->timeout_work, |
| 725 | msecs_to_jiffies(trf->timeout)); |
| 726 | } |
| 727 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 728 | static void trf7970a_timeout_work_handler(struct work_struct *work) |
| 729 | { |
| 730 | struct trf7970a *trf = container_of(work, struct trf7970a, |
| 731 | timeout_work.work); |
| 732 | |
| 733 | dev_dbg(trf->dev, "Timeout - state: %d, ignore_timeout: %d\n", |
| 734 | trf->state, trf->ignore_timeout); |
| 735 | |
| 736 | mutex_lock(&trf->lock); |
| 737 | |
| 738 | if (trf->ignore_timeout) |
| 739 | trf->ignore_timeout = false; |
| 740 | else if (trf->state == TRF7970A_ST_WAIT_FOR_RX_DATA_CONT) |
| 741 | trf7970a_send_upstream(trf); /* No more rx data so send up */ |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 742 | else if (trf->state == TRF7970A_ST_WAIT_TO_ISSUE_EOF) |
| 743 | trf7970a_issue_eof(trf); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 744 | else |
| 745 | trf7970a_send_err_upstream(trf, -ETIMEDOUT); |
| 746 | |
| 747 | mutex_unlock(&trf->lock); |
| 748 | } |
| 749 | |
| 750 | static int trf7970a_init(struct trf7970a *trf) |
| 751 | { |
| 752 | int ret; |
| 753 | |
| 754 | dev_dbg(trf->dev, "Initializing device - state: %d\n", trf->state); |
| 755 | |
| 756 | ret = trf7970a_cmd(trf, TRF7970A_CMD_SOFT_INIT); |
| 757 | if (ret) |
| 758 | goto err_out; |
| 759 | |
| 760 | ret = trf7970a_cmd(trf, TRF7970A_CMD_IDLE); |
| 761 | if (ret) |
| 762 | goto err_out; |
| 763 | |
Mark A. Greer | b887eb0 | 2014-03-25 08:54:31 -0700 | [diff] [blame] | 764 | /* Must clear NFC Target Detection Level reg due to erratum */ |
| 765 | ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0); |
| 766 | if (ret) |
| 767 | goto err_out; |
| 768 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 769 | ret = trf7970a_write(trf, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS, |
| 770 | TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 | |
| 771 | TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32); |
| 772 | if (ret) |
| 773 | goto err_out; |
| 774 | |
| 775 | ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, 0); |
| 776 | if (ret) |
| 777 | goto err_out; |
| 778 | |
| 779 | trf->special_fcn_reg1 = 0; |
| 780 | |
| 781 | ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, |
| 782 | TRF7970A_CHIP_STATUS_RF_ON | |
| 783 | TRF7970A_CHIP_STATUS_VRS5_3); |
| 784 | if (ret) |
| 785 | goto err_out; |
| 786 | |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 787 | trf->iso_ctrl = 0xff; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 788 | return 0; |
| 789 | |
| 790 | err_out: |
| 791 | dev_dbg(trf->dev, "Couldn't init device: %d\n", ret); |
| 792 | return ret; |
| 793 | } |
| 794 | |
| 795 | static void trf7970a_switch_rf_off(struct trf7970a *trf) |
| 796 | { |
| 797 | dev_dbg(trf->dev, "Switching rf off\n"); |
| 798 | |
| 799 | gpio_set_value(trf->en_gpio, 0); |
| 800 | gpio_set_value(trf->en2_gpio, 0); |
| 801 | |
| 802 | trf->aborting = false; |
| 803 | trf->state = TRF7970A_ST_OFF; |
| 804 | } |
| 805 | |
| 806 | static int trf7970a_switch_rf_on(struct trf7970a *trf) |
| 807 | { |
| 808 | unsigned long delay; |
| 809 | int ret; |
| 810 | |
| 811 | dev_dbg(trf->dev, "Switching rf on\n"); |
| 812 | |
| 813 | if (trf->powering_up) |
| 814 | usleep_range(5000, 6000); |
| 815 | |
| 816 | gpio_set_value(trf->en2_gpio, 1); |
| 817 | usleep_range(1000, 2000); |
| 818 | gpio_set_value(trf->en_gpio, 1); |
| 819 | |
| 820 | /* The delay between enabling the trf7970a and issuing the first |
| 821 | * command is significantly longer the very first time after powering |
| 822 | * up. Make sure the longer delay is only done the first time. |
| 823 | */ |
| 824 | if (trf->powering_up) { |
| 825 | delay = 20000; |
| 826 | trf->powering_up = false; |
| 827 | } else { |
| 828 | delay = 5000; |
| 829 | } |
| 830 | |
| 831 | usleep_range(delay, delay + 1000); |
| 832 | |
| 833 | ret = trf7970a_init(trf); |
| 834 | if (ret) |
| 835 | trf7970a_switch_rf_off(trf); |
| 836 | else |
| 837 | trf->state = TRF7970A_ST_IDLE; |
| 838 | |
| 839 | return ret; |
| 840 | } |
| 841 | |
| 842 | static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on) |
| 843 | { |
| 844 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 845 | int ret = 0; |
| 846 | |
| 847 | dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on); |
| 848 | |
| 849 | mutex_lock(&trf->lock); |
| 850 | |
| 851 | if (on) { |
| 852 | switch (trf->state) { |
| 853 | case TRF7970A_ST_OFF: |
| 854 | ret = trf7970a_switch_rf_on(trf); |
| 855 | break; |
| 856 | case TRF7970A_ST_IDLE: |
| 857 | case TRF7970A_ST_IDLE_RX_BLOCKED: |
| 858 | break; |
| 859 | default: |
| 860 | dev_err(trf->dev, "%s - Invalid request: %d %d\n", |
| 861 | __func__, trf->state, on); |
| 862 | trf7970a_switch_rf_off(trf); |
| 863 | } |
| 864 | } else { |
| 865 | switch (trf->state) { |
| 866 | case TRF7970A_ST_OFF: |
| 867 | break; |
| 868 | default: |
| 869 | dev_err(trf->dev, "%s - Invalid request: %d %d\n", |
| 870 | __func__, trf->state, on); |
| 871 | /* FALLTHROUGH */ |
| 872 | case TRF7970A_ST_IDLE: |
| 873 | case TRF7970A_ST_IDLE_RX_BLOCKED: |
| 874 | trf7970a_switch_rf_off(trf); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | mutex_unlock(&trf->lock); |
| 879 | return ret; |
| 880 | } |
| 881 | |
| 882 | static int trf7970a_config_rf_tech(struct trf7970a *trf, int tech) |
| 883 | { |
| 884 | int ret = 0; |
| 885 | |
| 886 | dev_dbg(trf->dev, "rf technology: %d\n", tech); |
| 887 | |
| 888 | switch (tech) { |
| 889 | case NFC_DIGITAL_RF_TECH_106A: |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 890 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443A_106; |
Mark A. Greer | 12e9ade | 2014-03-25 08:54:35 -0700 | [diff] [blame^] | 891 | trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 892 | break; |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 893 | case NFC_DIGITAL_RF_TECH_ISO15693: |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 894 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648; |
Mark A. Greer | 12e9ade | 2014-03-25 08:54:35 -0700 | [diff] [blame^] | 895 | trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK; |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 896 | break; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 897 | default: |
| 898 | dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech); |
| 899 | return -EINVAL; |
| 900 | } |
| 901 | |
| 902 | trf->technology = tech; |
| 903 | |
| 904 | return ret; |
| 905 | } |
| 906 | |
| 907 | static int trf7970a_config_framing(struct trf7970a *trf, int framing) |
| 908 | { |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 909 | u8 iso_ctrl = trf->iso_ctrl_tech; |
| 910 | int ret; |
| 911 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 912 | dev_dbg(trf->dev, "framing: %d\n", framing); |
| 913 | |
| 914 | switch (framing) { |
| 915 | case NFC_DIGITAL_FRAMING_NFCA_SHORT: |
| 916 | case NFC_DIGITAL_FRAMING_NFCA_STANDARD: |
| 917 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC; |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 918 | iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 919 | break; |
| 920 | case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A: |
Mark A. Greer | 8006289 | 2014-03-10 11:56:23 -0700 | [diff] [blame] | 921 | case NFC_DIGITAL_FRAMING_NFCA_T4T: |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 922 | case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY: |
| 923 | case NFC_DIGITAL_FRAMING_ISO15693_T5T: |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 924 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT; |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 925 | iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 926 | break; |
| 927 | case NFC_DIGITAL_FRAMING_NFCA_T2T: |
| 928 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT; |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 929 | iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 930 | break; |
| 931 | default: |
| 932 | dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing); |
| 933 | return -EINVAL; |
| 934 | } |
| 935 | |
| 936 | trf->framing = framing; |
| 937 | |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 938 | if (iso_ctrl != trf->iso_ctrl) { |
| 939 | ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl); |
| 940 | if (ret) |
| 941 | return ret; |
| 942 | |
| 943 | trf->iso_ctrl = iso_ctrl; |
Mark A. Greer | a0822a7 | 2014-03-25 08:54:34 -0700 | [diff] [blame] | 944 | |
| 945 | ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL, |
Mark A. Greer | 12e9ade | 2014-03-25 08:54:35 -0700 | [diff] [blame^] | 946 | trf->modulator_sys_clk_ctrl); |
Mark A. Greer | a0822a7 | 2014-03-25 08:54:34 -0700 | [diff] [blame] | 947 | if (ret) |
| 948 | return ret; |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | return 0; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type, |
| 955 | int param) |
| 956 | { |
| 957 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 958 | int ret = 0; |
| 959 | |
| 960 | dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param); |
| 961 | |
| 962 | mutex_lock(&trf->lock); |
| 963 | |
| 964 | if (trf->state == TRF7970A_ST_OFF) { |
| 965 | ret = trf7970a_switch_rf_on(trf); |
| 966 | if (ret) |
| 967 | goto err_out; |
| 968 | } |
| 969 | |
| 970 | switch (type) { |
| 971 | case NFC_DIGITAL_CONFIG_RF_TECH: |
| 972 | ret = trf7970a_config_rf_tech(trf, param); |
| 973 | break; |
| 974 | case NFC_DIGITAL_CONFIG_FRAMING: |
| 975 | ret = trf7970a_config_framing(trf, param); |
| 976 | break; |
| 977 | default: |
| 978 | dev_dbg(trf->dev, "Unknown type: %d\n", type); |
| 979 | ret = -EINVAL; |
| 980 | } |
| 981 | |
| 982 | err_out: |
| 983 | mutex_unlock(&trf->lock); |
| 984 | return ret; |
| 985 | } |
| 986 | |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 987 | static int trf7970a_is_iso15693_write_or_lock(u8 cmd) |
| 988 | { |
| 989 | switch (cmd) { |
| 990 | case ISO15693_CMD_WRITE_SINGLE_BLOCK: |
| 991 | case ISO15693_CMD_LOCK_BLOCK: |
| 992 | case ISO15693_CMD_WRITE_MULTIPLE_BLOCK: |
| 993 | case ISO15693_CMD_WRITE_AFI: |
| 994 | case ISO15693_CMD_LOCK_AFI: |
| 995 | case ISO15693_CMD_WRITE_DSFID: |
| 996 | case ISO15693_CMD_LOCK_DSFID: |
| 997 | return 1; |
| 998 | break; |
| 999 | default: |
| 1000 | return 0; |
| 1001 | } |
| 1002 | } |
| 1003 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1004 | static int trf7970a_per_cmd_config(struct trf7970a *trf, struct sk_buff *skb) |
| 1005 | { |
| 1006 | u8 *req = skb->data; |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1007 | u8 special_fcn_reg1, iso_ctrl; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1008 | int ret; |
| 1009 | |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1010 | trf->issue_eof = false; |
| 1011 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1012 | /* When issuing Type 2 read command, make sure the '4_bit_RX' bit in |
| 1013 | * special functions register 1 is cleared; otherwise, its a write or |
| 1014 | * sector select command and '4_bit_RX' must be set. |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1015 | * |
| 1016 | * When issuing an ISO 15693 command, inspect the flags byte to see |
| 1017 | * what speed to use. Also, remember if the OPTION flag is set on |
| 1018 | * a Type 5 write or lock command so the driver will know that it |
| 1019 | * has to send an EOF in order to get a response. |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1020 | */ |
| 1021 | if ((trf->technology == NFC_DIGITAL_RF_TECH_106A) && |
| 1022 | (trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T)) { |
| 1023 | if (req[0] == NFC_T2T_CMD_READ) |
| 1024 | special_fcn_reg1 = 0; |
| 1025 | else |
| 1026 | special_fcn_reg1 = TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX; |
| 1027 | |
| 1028 | if (special_fcn_reg1 != trf->special_fcn_reg1) { |
| 1029 | ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, |
| 1030 | special_fcn_reg1); |
| 1031 | if (ret) |
| 1032 | return ret; |
| 1033 | |
| 1034 | trf->special_fcn_reg1 = special_fcn_reg1; |
| 1035 | } |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1036 | } else if (trf->technology == NFC_DIGITAL_RF_TECH_ISO15693) { |
| 1037 | iso_ctrl = trf->iso_ctrl & ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK; |
| 1038 | |
| 1039 | switch (req[0] & ISO15693_REQ_FLAG_SPEED_MASK) { |
| 1040 | case 0x00: |
| 1041 | iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_662; |
| 1042 | break; |
| 1043 | case ISO15693_REQ_FLAG_SUB_CARRIER: |
| 1044 | iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a; |
| 1045 | break; |
| 1046 | case ISO15693_REQ_FLAG_DATA_RATE: |
| 1047 | iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648; |
| 1048 | break; |
| 1049 | case (ISO15693_REQ_FLAG_SUB_CARRIER | |
| 1050 | ISO15693_REQ_FLAG_DATA_RATE): |
| 1051 | iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669; |
| 1052 | break; |
| 1053 | } |
| 1054 | |
| 1055 | if (iso_ctrl != trf->iso_ctrl) { |
| 1056 | ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl); |
| 1057 | if (ret) |
| 1058 | return ret; |
| 1059 | |
| 1060 | trf->iso_ctrl = iso_ctrl; |
| 1061 | } |
| 1062 | |
| 1063 | if ((trf->framing == NFC_DIGITAL_FRAMING_ISO15693_T5T) && |
| 1064 | trf7970a_is_iso15693_write_or_lock(req[1]) && |
| 1065 | (req[0] & ISO15693_REQ_FLAG_OPTION)) |
| 1066 | trf->issue_eof = true; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | return 0; |
| 1070 | } |
| 1071 | |
| 1072 | static int trf7970a_in_send_cmd(struct nfc_digital_dev *ddev, |
| 1073 | struct sk_buff *skb, u16 timeout, |
| 1074 | nfc_digital_cmd_complete_t cb, void *arg) |
| 1075 | { |
| 1076 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 1077 | char *prefix; |
| 1078 | unsigned int len; |
| 1079 | int ret; |
| 1080 | |
| 1081 | dev_dbg(trf->dev, "New request - state: %d, timeout: %d ms, len: %d\n", |
| 1082 | trf->state, timeout, skb->len); |
| 1083 | |
| 1084 | if (skb->len > TRF7970A_TX_MAX) |
| 1085 | return -EINVAL; |
| 1086 | |
| 1087 | mutex_lock(&trf->lock); |
| 1088 | |
| 1089 | if ((trf->state != TRF7970A_ST_IDLE) && |
| 1090 | (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) { |
| 1091 | dev_err(trf->dev, "%s - Bogus state: %d\n", __func__, |
| 1092 | trf->state); |
| 1093 | ret = -EIO; |
| 1094 | goto out_err; |
| 1095 | } |
| 1096 | |
| 1097 | if (trf->aborting) { |
| 1098 | dev_dbg(trf->dev, "Abort process complete\n"); |
| 1099 | trf->aborting = false; |
| 1100 | ret = -ECANCELED; |
| 1101 | goto out_err; |
| 1102 | } |
| 1103 | |
| 1104 | trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE, |
| 1105 | GFP_KERNEL); |
| 1106 | if (!trf->rx_skb) { |
| 1107 | dev_dbg(trf->dev, "Can't alloc rx_skb\n"); |
| 1108 | ret = -ENOMEM; |
| 1109 | goto out_err; |
| 1110 | } |
| 1111 | |
| 1112 | if (trf->state == TRF7970A_ST_IDLE_RX_BLOCKED) { |
| 1113 | ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX); |
| 1114 | if (ret) |
| 1115 | goto out_err; |
| 1116 | |
| 1117 | trf->state = TRF7970A_ST_IDLE; |
| 1118 | } |
| 1119 | |
| 1120 | ret = trf7970a_per_cmd_config(trf, skb); |
| 1121 | if (ret) |
| 1122 | goto out_err; |
| 1123 | |
| 1124 | trf->ddev = ddev; |
| 1125 | trf->tx_skb = skb; |
| 1126 | trf->cb = cb; |
| 1127 | trf->cb_arg = arg; |
| 1128 | trf->timeout = timeout; |
| 1129 | trf->ignore_timeout = false; |
| 1130 | |
| 1131 | len = skb->len; |
| 1132 | prefix = skb_push(skb, TRF7970A_TX_SKB_HEADROOM); |
| 1133 | |
| 1134 | /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends |
| 1135 | * on what the current framing is, the address of the TX length byte 1 |
| 1136 | * register (0x1d), and the 2 byte length of the data to be transmitted. |
| 1137 | */ |
| 1138 | prefix[0] = TRF7970A_CMD_BIT_CTRL | |
| 1139 | TRF7970A_CMD_BIT_OPCODE(TRF7970A_CMD_FIFO_RESET); |
| 1140 | prefix[1] = TRF7970A_CMD_BIT_CTRL | |
| 1141 | TRF7970A_CMD_BIT_OPCODE(trf->tx_cmd); |
| 1142 | prefix[2] = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_TX_LENGTH_BYTE1; |
| 1143 | |
| 1144 | if (trf->framing == NFC_DIGITAL_FRAMING_NFCA_SHORT) { |
| 1145 | prefix[3] = 0x00; |
| 1146 | prefix[4] = 0x0f; /* 7 bits */ |
| 1147 | } else { |
| 1148 | prefix[3] = (len & 0xf00) >> 4; |
| 1149 | prefix[3] |= ((len & 0xf0) >> 4); |
| 1150 | prefix[4] = ((len & 0x0f) << 4); |
| 1151 | } |
| 1152 | |
| 1153 | len = min_t(int, skb->len, TRF7970A_FIFO_SIZE); |
| 1154 | |
| 1155 | usleep_range(1000, 2000); |
| 1156 | |
| 1157 | ret = trf7970a_transmit(trf, skb, len); |
| 1158 | if (ret) { |
| 1159 | kfree_skb(trf->rx_skb); |
| 1160 | trf->rx_skb = NULL; |
| 1161 | } |
| 1162 | |
| 1163 | out_err: |
| 1164 | mutex_unlock(&trf->lock); |
| 1165 | return ret; |
| 1166 | } |
| 1167 | |
| 1168 | static int trf7970a_tg_configure_hw(struct nfc_digital_dev *ddev, |
| 1169 | int type, int param) |
| 1170 | { |
| 1171 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 1172 | |
| 1173 | dev_dbg(trf->dev, "Unsupported interface\n"); |
| 1174 | |
| 1175 | return -EINVAL; |
| 1176 | } |
| 1177 | |
| 1178 | static int trf7970a_tg_send_cmd(struct nfc_digital_dev *ddev, |
| 1179 | struct sk_buff *skb, u16 timeout, |
| 1180 | nfc_digital_cmd_complete_t cb, void *arg) |
| 1181 | { |
| 1182 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 1183 | |
| 1184 | dev_dbg(trf->dev, "Unsupported interface\n"); |
| 1185 | |
| 1186 | return -EINVAL; |
| 1187 | } |
| 1188 | |
| 1189 | static int trf7970a_tg_listen(struct nfc_digital_dev *ddev, |
| 1190 | u16 timeout, nfc_digital_cmd_complete_t cb, void *arg) |
| 1191 | { |
| 1192 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 1193 | |
| 1194 | dev_dbg(trf->dev, "Unsupported interface\n"); |
| 1195 | |
| 1196 | return -EINVAL; |
| 1197 | } |
| 1198 | |
| 1199 | static int trf7970a_tg_listen_mdaa(struct nfc_digital_dev *ddev, |
| 1200 | struct digital_tg_mdaa_params *mdaa_params, |
| 1201 | u16 timeout, nfc_digital_cmd_complete_t cb, void *arg) |
| 1202 | { |
| 1203 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 1204 | |
| 1205 | dev_dbg(trf->dev, "Unsupported interface\n"); |
| 1206 | |
| 1207 | return -EINVAL; |
| 1208 | } |
| 1209 | |
| 1210 | static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev) |
| 1211 | { |
| 1212 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 1213 | |
| 1214 | dev_dbg(trf->dev, "Abort process initiated\n"); |
| 1215 | |
| 1216 | mutex_lock(&trf->lock); |
Mark A. Greer | 5876bc7 | 2014-03-25 08:54:30 -0700 | [diff] [blame] | 1217 | |
| 1218 | switch (trf->state) { |
| 1219 | case TRF7970A_ST_WAIT_FOR_TX_FIFO: |
| 1220 | case TRF7970A_ST_WAIT_FOR_RX_DATA: |
| 1221 | case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: |
| 1222 | case TRF7970A_ST_WAIT_TO_ISSUE_EOF: |
| 1223 | trf->aborting = true; |
| 1224 | break; |
| 1225 | default: |
| 1226 | break; |
| 1227 | } |
| 1228 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1229 | mutex_unlock(&trf->lock); |
| 1230 | } |
| 1231 | |
| 1232 | static struct nfc_digital_ops trf7970a_nfc_ops = { |
| 1233 | .in_configure_hw = trf7970a_in_configure_hw, |
| 1234 | .in_send_cmd = trf7970a_in_send_cmd, |
| 1235 | .tg_configure_hw = trf7970a_tg_configure_hw, |
| 1236 | .tg_send_cmd = trf7970a_tg_send_cmd, |
| 1237 | .tg_listen = trf7970a_tg_listen, |
| 1238 | .tg_listen_mdaa = trf7970a_tg_listen_mdaa, |
| 1239 | .switch_rf = trf7970a_switch_rf, |
| 1240 | .abort_cmd = trf7970a_abort_cmd, |
| 1241 | }; |
| 1242 | |
| 1243 | static int trf7970a_probe(struct spi_device *spi) |
| 1244 | { |
| 1245 | struct device_node *np = spi->dev.of_node; |
| 1246 | const struct spi_device_id *id = spi_get_device_id(spi); |
| 1247 | struct trf7970a *trf; |
| 1248 | int ret; |
| 1249 | |
| 1250 | if (!np) { |
| 1251 | dev_err(&spi->dev, "No Device Tree entry\n"); |
| 1252 | return -EINVAL; |
| 1253 | } |
| 1254 | |
| 1255 | trf = devm_kzalloc(&spi->dev, sizeof(*trf), GFP_KERNEL); |
| 1256 | if (!trf) |
| 1257 | return -ENOMEM; |
| 1258 | |
| 1259 | trf->state = TRF7970A_ST_OFF; |
| 1260 | trf->dev = &spi->dev; |
| 1261 | trf->spi = spi; |
| 1262 | trf->quirks = id->driver_data; |
| 1263 | |
| 1264 | spi->mode = SPI_MODE_1; |
| 1265 | spi->bits_per_word = 8; |
| 1266 | |
| 1267 | /* There are two enable pins - both must be present */ |
| 1268 | trf->en_gpio = of_get_named_gpio(np, "ti,enable-gpios", 0); |
| 1269 | if (!gpio_is_valid(trf->en_gpio)) { |
| 1270 | dev_err(trf->dev, "No EN GPIO property\n"); |
| 1271 | return trf->en_gpio; |
| 1272 | } |
| 1273 | |
| 1274 | ret = devm_gpio_request_one(trf->dev, trf->en_gpio, |
| 1275 | GPIOF_DIR_OUT | GPIOF_INIT_LOW, "EN"); |
| 1276 | if (ret) { |
| 1277 | dev_err(trf->dev, "Can't request EN GPIO: %d\n", ret); |
| 1278 | return ret; |
| 1279 | } |
| 1280 | |
| 1281 | trf->en2_gpio = of_get_named_gpio(np, "ti,enable-gpios", 1); |
| 1282 | if (!gpio_is_valid(trf->en2_gpio)) { |
| 1283 | dev_err(trf->dev, "No EN2 GPIO property\n"); |
| 1284 | return trf->en2_gpio; |
| 1285 | } |
| 1286 | |
| 1287 | ret = devm_gpio_request_one(trf->dev, trf->en2_gpio, |
| 1288 | GPIOF_DIR_OUT | GPIOF_INIT_LOW, "EN2"); |
| 1289 | if (ret) { |
| 1290 | dev_err(trf->dev, "Can't request EN2 GPIO: %d\n", ret); |
| 1291 | return ret; |
| 1292 | } |
| 1293 | |
| 1294 | ret = devm_request_threaded_irq(trf->dev, spi->irq, NULL, |
| 1295 | trf7970a_irq, IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
| 1296 | "trf7970a", trf); |
| 1297 | if (ret) { |
| 1298 | dev_err(trf->dev, "Can't request IRQ#%d: %d\n", spi->irq, ret); |
| 1299 | return ret; |
| 1300 | } |
| 1301 | |
| 1302 | mutex_init(&trf->lock); |
| 1303 | INIT_DELAYED_WORK(&trf->timeout_work, trf7970a_timeout_work_handler); |
| 1304 | |
| 1305 | trf->regulator = devm_regulator_get(&spi->dev, "vin"); |
| 1306 | if (IS_ERR(trf->regulator)) { |
| 1307 | ret = PTR_ERR(trf->regulator); |
| 1308 | dev_err(trf->dev, "Can't get VIN regulator: %d\n", ret); |
| 1309 | goto err_destroy_lock; |
| 1310 | } |
| 1311 | |
| 1312 | ret = regulator_enable(trf->regulator); |
| 1313 | if (ret) { |
| 1314 | dev_err(trf->dev, "Can't enable VIN: %d\n", ret); |
| 1315 | goto err_destroy_lock; |
| 1316 | } |
| 1317 | |
| 1318 | trf->powering_up = true; |
| 1319 | |
| 1320 | trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops, |
| 1321 | TRF7970A_SUPPORTED_PROTOCOLS, |
| 1322 | NFC_DIGITAL_DRV_CAPS_IN_CRC, TRF7970A_TX_SKB_HEADROOM, |
| 1323 | 0); |
| 1324 | if (!trf->ddev) { |
| 1325 | dev_err(trf->dev, "Can't allocate NFC digital device\n"); |
| 1326 | ret = -ENOMEM; |
| 1327 | goto err_disable_regulator; |
| 1328 | } |
| 1329 | |
| 1330 | nfc_digital_set_parent_dev(trf->ddev, trf->dev); |
| 1331 | nfc_digital_set_drvdata(trf->ddev, trf); |
| 1332 | spi_set_drvdata(spi, trf); |
| 1333 | |
| 1334 | ret = nfc_digital_register_device(trf->ddev); |
| 1335 | if (ret) { |
| 1336 | dev_err(trf->dev, "Can't register NFC digital device: %d\n", |
| 1337 | ret); |
| 1338 | goto err_free_ddev; |
| 1339 | } |
| 1340 | |
| 1341 | return 0; |
| 1342 | |
| 1343 | err_free_ddev: |
| 1344 | nfc_digital_free_device(trf->ddev); |
| 1345 | err_disable_regulator: |
| 1346 | regulator_disable(trf->regulator); |
| 1347 | err_destroy_lock: |
| 1348 | mutex_destroy(&trf->lock); |
| 1349 | return ret; |
| 1350 | } |
| 1351 | |
| 1352 | static int trf7970a_remove(struct spi_device *spi) |
| 1353 | { |
| 1354 | struct trf7970a *trf = spi_get_drvdata(spi); |
| 1355 | |
| 1356 | mutex_lock(&trf->lock); |
| 1357 | |
| 1358 | trf7970a_switch_rf_off(trf); |
| 1359 | trf7970a_init(trf); |
| 1360 | |
| 1361 | switch (trf->state) { |
| 1362 | case TRF7970A_ST_WAIT_FOR_TX_FIFO: |
| 1363 | case TRF7970A_ST_WAIT_FOR_RX_DATA: |
| 1364 | case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1365 | case TRF7970A_ST_WAIT_TO_ISSUE_EOF: |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1366 | trf7970a_send_err_upstream(trf, -ECANCELED); |
| 1367 | break; |
| 1368 | default: |
| 1369 | break; |
| 1370 | } |
| 1371 | |
| 1372 | mutex_unlock(&trf->lock); |
| 1373 | |
| 1374 | nfc_digital_unregister_device(trf->ddev); |
| 1375 | nfc_digital_free_device(trf->ddev); |
| 1376 | |
| 1377 | regulator_disable(trf->regulator); |
| 1378 | |
| 1379 | mutex_destroy(&trf->lock); |
| 1380 | |
| 1381 | return 0; |
| 1382 | } |
| 1383 | |
| 1384 | static const struct spi_device_id trf7970a_id_table[] = { |
| 1385 | { "trf7970a", TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA }, |
| 1386 | { } |
| 1387 | }; |
| 1388 | MODULE_DEVICE_TABLE(spi, trf7970a_id_table); |
| 1389 | |
| 1390 | static struct spi_driver trf7970a_spi_driver = { |
| 1391 | .probe = trf7970a_probe, |
| 1392 | .remove = trf7970a_remove, |
| 1393 | .id_table = trf7970a_id_table, |
| 1394 | .driver = { |
| 1395 | .name = "trf7970a", |
| 1396 | .owner = THIS_MODULE, |
| 1397 | }, |
| 1398 | }; |
| 1399 | |
| 1400 | module_spi_driver(trf7970a_spi_driver); |
| 1401 | |
| 1402 | MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>"); |
| 1403 | MODULE_LICENSE("GPL v2"); |
| 1404 | MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver"); |