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 | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 341 | u8 special_fcn_reg1; |
| 342 | int technology; |
| 343 | int framing; |
| 344 | u8 tx_cmd; |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 345 | bool issue_eof; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 346 | int en2_gpio; |
| 347 | int en_gpio; |
| 348 | struct mutex lock; |
| 349 | unsigned int timeout; |
| 350 | bool ignore_timeout; |
| 351 | struct delayed_work timeout_work; |
| 352 | }; |
| 353 | |
| 354 | |
| 355 | static int trf7970a_cmd(struct trf7970a *trf, u8 opcode) |
| 356 | { |
| 357 | u8 cmd = TRF7970A_CMD_BIT_CTRL | TRF7970A_CMD_BIT_OPCODE(opcode); |
| 358 | int ret; |
| 359 | |
| 360 | dev_dbg(trf->dev, "cmd: 0x%x\n", cmd); |
| 361 | |
| 362 | ret = spi_write(trf->spi, &cmd, 1); |
| 363 | if (ret) |
| 364 | dev_err(trf->dev, "%s - cmd: 0x%x, ret: %d\n", __func__, cmd, |
| 365 | ret); |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | static int trf7970a_read(struct trf7970a *trf, u8 reg, u8 *val) |
| 370 | { |
| 371 | u8 addr = TRF7970A_CMD_BIT_RW | reg; |
| 372 | int ret; |
| 373 | |
| 374 | ret = spi_write_then_read(trf->spi, &addr, 1, val, 1); |
| 375 | if (ret) |
| 376 | dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr, |
| 377 | ret); |
| 378 | |
| 379 | dev_dbg(trf->dev, "read(0x%x): 0x%x\n", addr, *val); |
| 380 | |
| 381 | return ret; |
| 382 | } |
| 383 | |
| 384 | static int trf7970a_read_cont(struct trf7970a *trf, u8 reg, |
| 385 | u8 *buf, size_t len) |
| 386 | { |
| 387 | u8 addr = reg | TRF7970A_CMD_BIT_RW | TRF7970A_CMD_BIT_CONTINUOUS; |
| 388 | int ret; |
| 389 | |
| 390 | dev_dbg(trf->dev, "read_cont(0x%x, %zd)\n", addr, len); |
| 391 | |
| 392 | ret = spi_write_then_read(trf->spi, &addr, 1, buf, len); |
| 393 | if (ret) |
| 394 | dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr, |
| 395 | ret); |
| 396 | return ret; |
| 397 | } |
| 398 | |
| 399 | static int trf7970a_write(struct trf7970a *trf, u8 reg, u8 val) |
| 400 | { |
| 401 | u8 buf[2] = { reg, val }; |
| 402 | int ret; |
| 403 | |
| 404 | dev_dbg(trf->dev, "write(0x%x): 0x%x\n", reg, val); |
| 405 | |
| 406 | ret = spi_write(trf->spi, buf, 2); |
| 407 | if (ret) |
| 408 | dev_err(trf->dev, "%s - write: 0x%x 0x%x, ret: %d\n", __func__, |
| 409 | buf[0], buf[1], ret); |
| 410 | |
| 411 | return ret; |
| 412 | } |
| 413 | |
| 414 | static int trf7970a_read_irqstatus(struct trf7970a *trf, u8 *status) |
| 415 | { |
| 416 | int ret; |
| 417 | u8 buf[2]; |
| 418 | u8 addr; |
| 419 | |
| 420 | addr = TRF7970A_IRQ_STATUS | TRF7970A_CMD_BIT_RW; |
| 421 | |
| 422 | if (trf->quirks & TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA) { |
| 423 | addr |= TRF7970A_CMD_BIT_CONTINUOUS; |
| 424 | ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2); |
| 425 | } else { |
| 426 | ret = spi_write_then_read(trf->spi, &addr, 1, buf, 1); |
| 427 | } |
| 428 | |
| 429 | if (ret) |
| 430 | dev_err(trf->dev, "%s - irqstatus: Status read failed: %d\n", |
| 431 | __func__, ret); |
| 432 | else |
| 433 | *status = buf[0]; |
| 434 | |
| 435 | return ret; |
| 436 | } |
| 437 | |
| 438 | static void trf7970a_send_upstream(struct trf7970a *trf) |
| 439 | { |
| 440 | u8 rssi; |
| 441 | |
| 442 | dev_kfree_skb_any(trf->tx_skb); |
| 443 | trf->tx_skb = NULL; |
| 444 | |
| 445 | if (trf->rx_skb && !IS_ERR(trf->rx_skb) && !trf->aborting) |
| 446 | print_hex_dump_debug("trf7970a rx data: ", DUMP_PREFIX_NONE, |
| 447 | 16, 1, trf->rx_skb->data, trf->rx_skb->len, |
| 448 | false); |
| 449 | |
| 450 | /* According to the manual it is "good form" to reset the fifo and |
| 451 | * read the RSSI levels & oscillator status register here. It doesn't |
| 452 | * explain why. |
| 453 | */ |
| 454 | trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); |
| 455 | trf7970a_read(trf, TRF7970A_RSSI_OSC_STATUS, &rssi); |
| 456 | |
| 457 | trf->state = TRF7970A_ST_IDLE; |
| 458 | |
| 459 | if (trf->aborting) { |
| 460 | dev_dbg(trf->dev, "Abort process complete\n"); |
| 461 | |
| 462 | if (!IS_ERR(trf->rx_skb)) { |
| 463 | kfree_skb(trf->rx_skb); |
| 464 | trf->rx_skb = ERR_PTR(-ECANCELED); |
| 465 | } |
| 466 | |
| 467 | trf->aborting = false; |
| 468 | } |
| 469 | |
| 470 | trf->cb(trf->ddev, trf->cb_arg, trf->rx_skb); |
| 471 | |
| 472 | trf->rx_skb = NULL; |
| 473 | } |
| 474 | |
| 475 | static void trf7970a_send_err_upstream(struct trf7970a *trf, int errno) |
| 476 | { |
| 477 | dev_dbg(trf->dev, "Error - state: %d, errno: %d\n", trf->state, errno); |
| 478 | |
| 479 | kfree_skb(trf->rx_skb); |
| 480 | trf->rx_skb = ERR_PTR(errno); |
| 481 | |
| 482 | trf7970a_send_upstream(trf); |
| 483 | } |
| 484 | |
| 485 | static int trf7970a_transmit(struct trf7970a *trf, struct sk_buff *skb, |
| 486 | unsigned int len) |
| 487 | { |
| 488 | unsigned int timeout; |
| 489 | int ret; |
| 490 | |
| 491 | print_hex_dump_debug("trf7970a tx data: ", DUMP_PREFIX_NONE, |
| 492 | 16, 1, skb->data, len, false); |
| 493 | |
| 494 | ret = spi_write(trf->spi, skb->data, len); |
| 495 | if (ret) { |
| 496 | dev_err(trf->dev, "%s - Can't send tx data: %d\n", __func__, |
| 497 | ret); |
| 498 | return ret; |
| 499 | } |
| 500 | |
| 501 | skb_pull(skb, len); |
| 502 | |
| 503 | if (skb->len > 0) { |
| 504 | trf->state = TRF7970A_ST_WAIT_FOR_TX_FIFO; |
| 505 | timeout = TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT; |
| 506 | } else { |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 507 | if (trf->issue_eof) { |
| 508 | trf->state = TRF7970A_ST_WAIT_TO_ISSUE_EOF; |
| 509 | timeout = TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF; |
| 510 | } else { |
| 511 | trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA; |
| 512 | timeout = trf->timeout; |
| 513 | } |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", timeout, |
| 517 | trf->state); |
| 518 | |
| 519 | schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout)); |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | static void trf7970a_fill_fifo(struct trf7970a *trf) |
| 525 | { |
| 526 | struct sk_buff *skb = trf->tx_skb; |
| 527 | unsigned int len; |
| 528 | int ret; |
| 529 | u8 fifo_bytes; |
| 530 | |
| 531 | ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes); |
| 532 | if (ret) { |
| 533 | trf7970a_send_err_upstream(trf, ret); |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | dev_dbg(trf->dev, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes); |
| 538 | |
| 539 | if (fifo_bytes & TRF7970A_FIFO_STATUS_OVERFLOW) { |
| 540 | dev_err(trf->dev, "%s - fifo overflow: 0x%x\n", __func__, |
| 541 | fifo_bytes); |
| 542 | trf7970a_send_err_upstream(trf, -EIO); |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | /* Calculate how much more data can be written to the fifo */ |
| 547 | len = TRF7970A_FIFO_SIZE - fifo_bytes; |
| 548 | len = min(skb->len, len); |
| 549 | |
| 550 | ret = trf7970a_transmit(trf, skb, len); |
| 551 | if (ret) |
| 552 | trf7970a_send_err_upstream(trf, ret); |
| 553 | } |
| 554 | |
| 555 | static void trf7970a_drain_fifo(struct trf7970a *trf, u8 status) |
| 556 | { |
| 557 | struct sk_buff *skb = trf->rx_skb; |
| 558 | int ret; |
| 559 | u8 fifo_bytes; |
| 560 | |
| 561 | if (status & TRF7970A_IRQ_STATUS_ERROR) { |
| 562 | trf7970a_send_err_upstream(trf, -EIO); |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes); |
| 567 | if (ret) { |
| 568 | trf7970a_send_err_upstream(trf, ret); |
| 569 | return; |
| 570 | } |
| 571 | |
| 572 | dev_dbg(trf->dev, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes); |
| 573 | |
| 574 | if (!fifo_bytes) |
| 575 | goto no_rx_data; |
| 576 | |
| 577 | if (fifo_bytes & TRF7970A_FIFO_STATUS_OVERFLOW) { |
| 578 | dev_err(trf->dev, "%s - fifo overflow: 0x%x\n", __func__, |
| 579 | fifo_bytes); |
| 580 | trf7970a_send_err_upstream(trf, -EIO); |
| 581 | return; |
| 582 | } |
| 583 | |
| 584 | if (fifo_bytes > skb_tailroom(skb)) { |
| 585 | skb = skb_copy_expand(skb, skb_headroom(skb), |
| 586 | max_t(int, fifo_bytes, |
| 587 | TRF7970A_RX_SKB_ALLOC_SIZE), |
| 588 | GFP_KERNEL); |
| 589 | if (!skb) { |
| 590 | trf7970a_send_err_upstream(trf, -ENOMEM); |
| 591 | return; |
| 592 | } |
| 593 | |
| 594 | kfree_skb(trf->rx_skb); |
| 595 | trf->rx_skb = skb; |
| 596 | } |
| 597 | |
| 598 | ret = trf7970a_read_cont(trf, TRF7970A_FIFO_IO_REGISTER, |
| 599 | skb_put(skb, fifo_bytes), fifo_bytes); |
| 600 | if (ret) { |
| 601 | trf7970a_send_err_upstream(trf, ret); |
| 602 | return; |
| 603 | } |
| 604 | |
| 605 | /* If received Type 2 ACK/NACK, shift right 4 bits and pass up */ |
| 606 | if ((trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T) && (skb->len == 1) && |
| 607 | (trf->special_fcn_reg1 == |
| 608 | TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX)) { |
| 609 | skb->data[0] >>= 4; |
| 610 | status = TRF7970A_IRQ_STATUS_SRX; |
| 611 | } else { |
| 612 | trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA_CONT; |
| 613 | } |
| 614 | |
| 615 | no_rx_data: |
| 616 | if (status == TRF7970A_IRQ_STATUS_SRX) { /* Receive complete */ |
| 617 | trf7970a_send_upstream(trf); |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | dev_dbg(trf->dev, "Setting timeout for %d ms\n", |
| 622 | TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT); |
| 623 | |
| 624 | schedule_delayed_work(&trf->timeout_work, |
| 625 | msecs_to_jiffies(TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT)); |
| 626 | } |
| 627 | |
| 628 | static irqreturn_t trf7970a_irq(int irq, void *dev_id) |
| 629 | { |
| 630 | struct trf7970a *trf = dev_id; |
| 631 | int ret; |
| 632 | u8 status; |
| 633 | |
| 634 | mutex_lock(&trf->lock); |
| 635 | |
| 636 | if (trf->state == TRF7970A_ST_OFF) { |
| 637 | mutex_unlock(&trf->lock); |
| 638 | return IRQ_NONE; |
| 639 | } |
| 640 | |
| 641 | ret = trf7970a_read_irqstatus(trf, &status); |
| 642 | if (ret) { |
| 643 | mutex_unlock(&trf->lock); |
| 644 | return IRQ_NONE; |
| 645 | } |
| 646 | |
| 647 | dev_dbg(trf->dev, "IRQ - state: %d, status: 0x%x\n", trf->state, |
| 648 | status); |
| 649 | |
| 650 | if (!status) { |
| 651 | mutex_unlock(&trf->lock); |
| 652 | return IRQ_NONE; |
| 653 | } |
| 654 | |
| 655 | switch (trf->state) { |
| 656 | case TRF7970A_ST_IDLE: |
| 657 | case TRF7970A_ST_IDLE_RX_BLOCKED: |
| 658 | /* If getting interrupts caused by RF noise, turn off the |
| 659 | * receiver to avoid unnecessary interrupts. It will be |
| 660 | * turned back on in trf7970a_in_send_cmd() when the next |
| 661 | * command is issued. |
| 662 | */ |
| 663 | if (status & TRF7970A_IRQ_STATUS_ERROR) { |
| 664 | trf7970a_cmd(trf, TRF7970A_CMD_BLOCK_RX); |
| 665 | trf->state = TRF7970A_ST_IDLE_RX_BLOCKED; |
| 666 | } |
| 667 | |
| 668 | trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); |
| 669 | break; |
| 670 | case TRF7970A_ST_WAIT_FOR_TX_FIFO: |
| 671 | if (status & TRF7970A_IRQ_STATUS_TX) { |
| 672 | trf->ignore_timeout = |
| 673 | !cancel_delayed_work(&trf->timeout_work); |
| 674 | trf7970a_fill_fifo(trf); |
| 675 | } else { |
| 676 | trf7970a_send_err_upstream(trf, -EIO); |
| 677 | } |
| 678 | break; |
| 679 | case TRF7970A_ST_WAIT_FOR_RX_DATA: |
| 680 | case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: |
| 681 | if (status & TRF7970A_IRQ_STATUS_SRX) { |
| 682 | trf->ignore_timeout = |
| 683 | !cancel_delayed_work(&trf->timeout_work); |
| 684 | trf7970a_drain_fifo(trf, status); |
Mark A. Greer | 4dd836e | 2014-03-25 08:54:32 -0700 | [diff] [blame] | 685 | } else if (status == TRF7970A_IRQ_STATUS_TX) { |
| 686 | trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); |
| 687 | } else { |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 688 | trf7970a_send_err_upstream(trf, -EIO); |
| 689 | } |
| 690 | break; |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 691 | case TRF7970A_ST_WAIT_TO_ISSUE_EOF: |
| 692 | if (status != TRF7970A_IRQ_STATUS_TX) |
| 693 | trf7970a_send_err_upstream(trf, -EIO); |
| 694 | break; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 695 | default: |
| 696 | dev_err(trf->dev, "%s - Driver in invalid state: %d\n", |
| 697 | __func__, trf->state); |
| 698 | } |
| 699 | |
| 700 | mutex_unlock(&trf->lock); |
| 701 | return IRQ_HANDLED; |
| 702 | } |
| 703 | |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 704 | static void trf7970a_issue_eof(struct trf7970a *trf) |
| 705 | { |
| 706 | int ret; |
| 707 | |
| 708 | dev_dbg(trf->dev, "Issuing EOF\n"); |
| 709 | |
| 710 | ret = trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); |
| 711 | if (ret) |
| 712 | trf7970a_send_err_upstream(trf, ret); |
| 713 | |
| 714 | ret = trf7970a_cmd(trf, TRF7970A_CMD_EOF); |
| 715 | if (ret) |
| 716 | trf7970a_send_err_upstream(trf, ret); |
| 717 | |
| 718 | trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA; |
| 719 | |
| 720 | dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", |
| 721 | trf->timeout, trf->state); |
| 722 | |
| 723 | schedule_delayed_work(&trf->timeout_work, |
| 724 | msecs_to_jiffies(trf->timeout)); |
| 725 | } |
| 726 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 727 | static void trf7970a_timeout_work_handler(struct work_struct *work) |
| 728 | { |
| 729 | struct trf7970a *trf = container_of(work, struct trf7970a, |
| 730 | timeout_work.work); |
| 731 | |
| 732 | dev_dbg(trf->dev, "Timeout - state: %d, ignore_timeout: %d\n", |
| 733 | trf->state, trf->ignore_timeout); |
| 734 | |
| 735 | mutex_lock(&trf->lock); |
| 736 | |
| 737 | if (trf->ignore_timeout) |
| 738 | trf->ignore_timeout = false; |
| 739 | else if (trf->state == TRF7970A_ST_WAIT_FOR_RX_DATA_CONT) |
| 740 | trf7970a_send_upstream(trf); /* No more rx data so send up */ |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 741 | else if (trf->state == TRF7970A_ST_WAIT_TO_ISSUE_EOF) |
| 742 | trf7970a_issue_eof(trf); |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 743 | else |
| 744 | trf7970a_send_err_upstream(trf, -ETIMEDOUT); |
| 745 | |
| 746 | mutex_unlock(&trf->lock); |
| 747 | } |
| 748 | |
| 749 | static int trf7970a_init(struct trf7970a *trf) |
| 750 | { |
| 751 | int ret; |
| 752 | |
| 753 | dev_dbg(trf->dev, "Initializing device - state: %d\n", trf->state); |
| 754 | |
| 755 | ret = trf7970a_cmd(trf, TRF7970A_CMD_SOFT_INIT); |
| 756 | if (ret) |
| 757 | goto err_out; |
| 758 | |
| 759 | ret = trf7970a_cmd(trf, TRF7970A_CMD_IDLE); |
| 760 | if (ret) |
| 761 | goto err_out; |
| 762 | |
Mark A. Greer | b887eb0 | 2014-03-25 08:54:31 -0700 | [diff] [blame] | 763 | /* Must clear NFC Target Detection Level reg due to erratum */ |
| 764 | ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0); |
| 765 | if (ret) |
| 766 | goto err_out; |
| 767 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 768 | ret = trf7970a_write(trf, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS, |
| 769 | TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 | |
| 770 | TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32); |
| 771 | if (ret) |
| 772 | goto err_out; |
| 773 | |
| 774 | ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, 0); |
| 775 | if (ret) |
| 776 | goto err_out; |
| 777 | |
| 778 | trf->special_fcn_reg1 = 0; |
| 779 | |
| 780 | ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, |
| 781 | TRF7970A_CHIP_STATUS_RF_ON | |
| 782 | TRF7970A_CHIP_STATUS_VRS5_3); |
| 783 | if (ret) |
| 784 | goto err_out; |
| 785 | |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 786 | trf->iso_ctrl = 0xff; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 787 | return 0; |
| 788 | |
| 789 | err_out: |
| 790 | dev_dbg(trf->dev, "Couldn't init device: %d\n", ret); |
| 791 | return ret; |
| 792 | } |
| 793 | |
| 794 | static void trf7970a_switch_rf_off(struct trf7970a *trf) |
| 795 | { |
| 796 | dev_dbg(trf->dev, "Switching rf off\n"); |
| 797 | |
| 798 | gpio_set_value(trf->en_gpio, 0); |
| 799 | gpio_set_value(trf->en2_gpio, 0); |
| 800 | |
| 801 | trf->aborting = false; |
| 802 | trf->state = TRF7970A_ST_OFF; |
| 803 | } |
| 804 | |
| 805 | static int trf7970a_switch_rf_on(struct trf7970a *trf) |
| 806 | { |
| 807 | unsigned long delay; |
| 808 | int ret; |
| 809 | |
| 810 | dev_dbg(trf->dev, "Switching rf on\n"); |
| 811 | |
| 812 | if (trf->powering_up) |
| 813 | usleep_range(5000, 6000); |
| 814 | |
| 815 | gpio_set_value(trf->en2_gpio, 1); |
| 816 | usleep_range(1000, 2000); |
| 817 | gpio_set_value(trf->en_gpio, 1); |
| 818 | |
| 819 | /* The delay between enabling the trf7970a and issuing the first |
| 820 | * command is significantly longer the very first time after powering |
| 821 | * up. Make sure the longer delay is only done the first time. |
| 822 | */ |
| 823 | if (trf->powering_up) { |
| 824 | delay = 20000; |
| 825 | trf->powering_up = false; |
| 826 | } else { |
| 827 | delay = 5000; |
| 828 | } |
| 829 | |
| 830 | usleep_range(delay, delay + 1000); |
| 831 | |
| 832 | ret = trf7970a_init(trf); |
| 833 | if (ret) |
| 834 | trf7970a_switch_rf_off(trf); |
| 835 | else |
| 836 | trf->state = TRF7970A_ST_IDLE; |
| 837 | |
| 838 | return ret; |
| 839 | } |
| 840 | |
| 841 | static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on) |
| 842 | { |
| 843 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 844 | int ret = 0; |
| 845 | |
| 846 | dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on); |
| 847 | |
| 848 | mutex_lock(&trf->lock); |
| 849 | |
| 850 | if (on) { |
| 851 | switch (trf->state) { |
| 852 | case TRF7970A_ST_OFF: |
| 853 | ret = trf7970a_switch_rf_on(trf); |
| 854 | break; |
| 855 | case TRF7970A_ST_IDLE: |
| 856 | case TRF7970A_ST_IDLE_RX_BLOCKED: |
| 857 | break; |
| 858 | default: |
| 859 | dev_err(trf->dev, "%s - Invalid request: %d %d\n", |
| 860 | __func__, trf->state, on); |
| 861 | trf7970a_switch_rf_off(trf); |
| 862 | } |
| 863 | } else { |
| 864 | switch (trf->state) { |
| 865 | case TRF7970A_ST_OFF: |
| 866 | break; |
| 867 | default: |
| 868 | dev_err(trf->dev, "%s - Invalid request: %d %d\n", |
| 869 | __func__, trf->state, on); |
| 870 | /* FALLTHROUGH */ |
| 871 | case TRF7970A_ST_IDLE: |
| 872 | case TRF7970A_ST_IDLE_RX_BLOCKED: |
| 873 | trf7970a_switch_rf_off(trf); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | mutex_unlock(&trf->lock); |
| 878 | return ret; |
| 879 | } |
| 880 | |
| 881 | static int trf7970a_config_rf_tech(struct trf7970a *trf, int tech) |
| 882 | { |
| 883 | int ret = 0; |
| 884 | |
| 885 | dev_dbg(trf->dev, "rf technology: %d\n", tech); |
| 886 | |
| 887 | switch (tech) { |
| 888 | case NFC_DIGITAL_RF_TECH_106A: |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 889 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443A_106; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 890 | break; |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 891 | case NFC_DIGITAL_RF_TECH_ISO15693: |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 892 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648; |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 893 | break; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 894 | default: |
| 895 | dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech); |
| 896 | return -EINVAL; |
| 897 | } |
| 898 | |
| 899 | trf->technology = tech; |
| 900 | |
| 901 | return ret; |
| 902 | } |
| 903 | |
| 904 | static int trf7970a_config_framing(struct trf7970a *trf, int framing) |
| 905 | { |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 906 | u8 iso_ctrl = trf->iso_ctrl_tech; |
| 907 | int ret; |
| 908 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 909 | dev_dbg(trf->dev, "framing: %d\n", framing); |
| 910 | |
| 911 | switch (framing) { |
| 912 | case NFC_DIGITAL_FRAMING_NFCA_SHORT: |
| 913 | case NFC_DIGITAL_FRAMING_NFCA_STANDARD: |
| 914 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC; |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 915 | iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 916 | break; |
| 917 | case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A: |
Mark A. Greer | 8006289 | 2014-03-10 11:56:23 -0700 | [diff] [blame] | 918 | case NFC_DIGITAL_FRAMING_NFCA_T4T: |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 919 | case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY: |
| 920 | case NFC_DIGITAL_FRAMING_ISO15693_T5T: |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 921 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT; |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 922 | iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 923 | break; |
| 924 | case NFC_DIGITAL_FRAMING_NFCA_T2T: |
| 925 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT; |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 926 | iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 927 | break; |
| 928 | default: |
| 929 | dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing); |
| 930 | return -EINVAL; |
| 931 | } |
| 932 | |
| 933 | trf->framing = framing; |
| 934 | |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 935 | if (iso_ctrl != trf->iso_ctrl) { |
| 936 | ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl); |
| 937 | if (ret) |
| 938 | return ret; |
| 939 | |
| 940 | trf->iso_ctrl = iso_ctrl; |
Mark A. Greer | a0822a7 | 2014-03-25 08:54:34 -0700 | [diff] [blame^] | 941 | |
| 942 | ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL, |
| 943 | TRF7970A_MODULATOR_DEPTH_OOK); |
| 944 | if (ret) |
| 945 | return ret; |
Mark A. Greer | 49d19cc | 2014-03-25 08:54:33 -0700 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | return 0; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type, |
| 952 | int param) |
| 953 | { |
| 954 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 955 | int ret = 0; |
| 956 | |
| 957 | dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param); |
| 958 | |
| 959 | mutex_lock(&trf->lock); |
| 960 | |
| 961 | if (trf->state == TRF7970A_ST_OFF) { |
| 962 | ret = trf7970a_switch_rf_on(trf); |
| 963 | if (ret) |
| 964 | goto err_out; |
| 965 | } |
| 966 | |
| 967 | switch (type) { |
| 968 | case NFC_DIGITAL_CONFIG_RF_TECH: |
| 969 | ret = trf7970a_config_rf_tech(trf, param); |
| 970 | break; |
| 971 | case NFC_DIGITAL_CONFIG_FRAMING: |
| 972 | ret = trf7970a_config_framing(trf, param); |
| 973 | break; |
| 974 | default: |
| 975 | dev_dbg(trf->dev, "Unknown type: %d\n", type); |
| 976 | ret = -EINVAL; |
| 977 | } |
| 978 | |
| 979 | err_out: |
| 980 | mutex_unlock(&trf->lock); |
| 981 | return ret; |
| 982 | } |
| 983 | |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 984 | static int trf7970a_is_iso15693_write_or_lock(u8 cmd) |
| 985 | { |
| 986 | switch (cmd) { |
| 987 | case ISO15693_CMD_WRITE_SINGLE_BLOCK: |
| 988 | case ISO15693_CMD_LOCK_BLOCK: |
| 989 | case ISO15693_CMD_WRITE_MULTIPLE_BLOCK: |
| 990 | case ISO15693_CMD_WRITE_AFI: |
| 991 | case ISO15693_CMD_LOCK_AFI: |
| 992 | case ISO15693_CMD_WRITE_DSFID: |
| 993 | case ISO15693_CMD_LOCK_DSFID: |
| 994 | return 1; |
| 995 | break; |
| 996 | default: |
| 997 | return 0; |
| 998 | } |
| 999 | } |
| 1000 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1001 | static int trf7970a_per_cmd_config(struct trf7970a *trf, struct sk_buff *skb) |
| 1002 | { |
| 1003 | u8 *req = skb->data; |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1004 | u8 special_fcn_reg1, iso_ctrl; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1005 | int ret; |
| 1006 | |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1007 | trf->issue_eof = false; |
| 1008 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1009 | /* When issuing Type 2 read command, make sure the '4_bit_RX' bit in |
| 1010 | * special functions register 1 is cleared; otherwise, its a write or |
| 1011 | * sector select command and '4_bit_RX' must be set. |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1012 | * |
| 1013 | * When issuing an ISO 15693 command, inspect the flags byte to see |
| 1014 | * what speed to use. Also, remember if the OPTION flag is set on |
| 1015 | * a Type 5 write or lock command so the driver will know that it |
| 1016 | * has to send an EOF in order to get a response. |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1017 | */ |
| 1018 | if ((trf->technology == NFC_DIGITAL_RF_TECH_106A) && |
| 1019 | (trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T)) { |
| 1020 | if (req[0] == NFC_T2T_CMD_READ) |
| 1021 | special_fcn_reg1 = 0; |
| 1022 | else |
| 1023 | special_fcn_reg1 = TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX; |
| 1024 | |
| 1025 | if (special_fcn_reg1 != trf->special_fcn_reg1) { |
| 1026 | ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, |
| 1027 | special_fcn_reg1); |
| 1028 | if (ret) |
| 1029 | return ret; |
| 1030 | |
| 1031 | trf->special_fcn_reg1 = special_fcn_reg1; |
| 1032 | } |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1033 | } else if (trf->technology == NFC_DIGITAL_RF_TECH_ISO15693) { |
| 1034 | iso_ctrl = trf->iso_ctrl & ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK; |
| 1035 | |
| 1036 | switch (req[0] & ISO15693_REQ_FLAG_SPEED_MASK) { |
| 1037 | case 0x00: |
| 1038 | iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_662; |
| 1039 | break; |
| 1040 | case ISO15693_REQ_FLAG_SUB_CARRIER: |
| 1041 | iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a; |
| 1042 | break; |
| 1043 | case ISO15693_REQ_FLAG_DATA_RATE: |
| 1044 | iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648; |
| 1045 | break; |
| 1046 | case (ISO15693_REQ_FLAG_SUB_CARRIER | |
| 1047 | ISO15693_REQ_FLAG_DATA_RATE): |
| 1048 | iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669; |
| 1049 | break; |
| 1050 | } |
| 1051 | |
| 1052 | if (iso_ctrl != trf->iso_ctrl) { |
| 1053 | ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl); |
| 1054 | if (ret) |
| 1055 | return ret; |
| 1056 | |
| 1057 | trf->iso_ctrl = iso_ctrl; |
| 1058 | } |
| 1059 | |
| 1060 | if ((trf->framing == NFC_DIGITAL_FRAMING_ISO15693_T5T) && |
| 1061 | trf7970a_is_iso15693_write_or_lock(req[1]) && |
| 1062 | (req[0] & ISO15693_REQ_FLAG_OPTION)) |
| 1063 | trf->issue_eof = true; |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | return 0; |
| 1067 | } |
| 1068 | |
| 1069 | static int trf7970a_in_send_cmd(struct nfc_digital_dev *ddev, |
| 1070 | struct sk_buff *skb, u16 timeout, |
| 1071 | nfc_digital_cmd_complete_t cb, void *arg) |
| 1072 | { |
| 1073 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 1074 | char *prefix; |
| 1075 | unsigned int len; |
| 1076 | int ret; |
| 1077 | |
| 1078 | dev_dbg(trf->dev, "New request - state: %d, timeout: %d ms, len: %d\n", |
| 1079 | trf->state, timeout, skb->len); |
| 1080 | |
| 1081 | if (skb->len > TRF7970A_TX_MAX) |
| 1082 | return -EINVAL; |
| 1083 | |
| 1084 | mutex_lock(&trf->lock); |
| 1085 | |
| 1086 | if ((trf->state != TRF7970A_ST_IDLE) && |
| 1087 | (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) { |
| 1088 | dev_err(trf->dev, "%s - Bogus state: %d\n", __func__, |
| 1089 | trf->state); |
| 1090 | ret = -EIO; |
| 1091 | goto out_err; |
| 1092 | } |
| 1093 | |
| 1094 | if (trf->aborting) { |
| 1095 | dev_dbg(trf->dev, "Abort process complete\n"); |
| 1096 | trf->aborting = false; |
| 1097 | ret = -ECANCELED; |
| 1098 | goto out_err; |
| 1099 | } |
| 1100 | |
| 1101 | trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE, |
| 1102 | GFP_KERNEL); |
| 1103 | if (!trf->rx_skb) { |
| 1104 | dev_dbg(trf->dev, "Can't alloc rx_skb\n"); |
| 1105 | ret = -ENOMEM; |
| 1106 | goto out_err; |
| 1107 | } |
| 1108 | |
| 1109 | if (trf->state == TRF7970A_ST_IDLE_RX_BLOCKED) { |
| 1110 | ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX); |
| 1111 | if (ret) |
| 1112 | goto out_err; |
| 1113 | |
| 1114 | trf->state = TRF7970A_ST_IDLE; |
| 1115 | } |
| 1116 | |
| 1117 | ret = trf7970a_per_cmd_config(trf, skb); |
| 1118 | if (ret) |
| 1119 | goto out_err; |
| 1120 | |
| 1121 | trf->ddev = ddev; |
| 1122 | trf->tx_skb = skb; |
| 1123 | trf->cb = cb; |
| 1124 | trf->cb_arg = arg; |
| 1125 | trf->timeout = timeout; |
| 1126 | trf->ignore_timeout = false; |
| 1127 | |
| 1128 | len = skb->len; |
| 1129 | prefix = skb_push(skb, TRF7970A_TX_SKB_HEADROOM); |
| 1130 | |
| 1131 | /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends |
| 1132 | * on what the current framing is, the address of the TX length byte 1 |
| 1133 | * register (0x1d), and the 2 byte length of the data to be transmitted. |
| 1134 | */ |
| 1135 | prefix[0] = TRF7970A_CMD_BIT_CTRL | |
| 1136 | TRF7970A_CMD_BIT_OPCODE(TRF7970A_CMD_FIFO_RESET); |
| 1137 | prefix[1] = TRF7970A_CMD_BIT_CTRL | |
| 1138 | TRF7970A_CMD_BIT_OPCODE(trf->tx_cmd); |
| 1139 | prefix[2] = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_TX_LENGTH_BYTE1; |
| 1140 | |
| 1141 | if (trf->framing == NFC_DIGITAL_FRAMING_NFCA_SHORT) { |
| 1142 | prefix[3] = 0x00; |
| 1143 | prefix[4] = 0x0f; /* 7 bits */ |
| 1144 | } else { |
| 1145 | prefix[3] = (len & 0xf00) >> 4; |
| 1146 | prefix[3] |= ((len & 0xf0) >> 4); |
| 1147 | prefix[4] = ((len & 0x0f) << 4); |
| 1148 | } |
| 1149 | |
| 1150 | len = min_t(int, skb->len, TRF7970A_FIFO_SIZE); |
| 1151 | |
| 1152 | usleep_range(1000, 2000); |
| 1153 | |
| 1154 | ret = trf7970a_transmit(trf, skb, len); |
| 1155 | if (ret) { |
| 1156 | kfree_skb(trf->rx_skb); |
| 1157 | trf->rx_skb = NULL; |
| 1158 | } |
| 1159 | |
| 1160 | out_err: |
| 1161 | mutex_unlock(&trf->lock); |
| 1162 | return ret; |
| 1163 | } |
| 1164 | |
| 1165 | static int trf7970a_tg_configure_hw(struct nfc_digital_dev *ddev, |
| 1166 | int type, int param) |
| 1167 | { |
| 1168 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 1169 | |
| 1170 | dev_dbg(trf->dev, "Unsupported interface\n"); |
| 1171 | |
| 1172 | return -EINVAL; |
| 1173 | } |
| 1174 | |
| 1175 | static int trf7970a_tg_send_cmd(struct nfc_digital_dev *ddev, |
| 1176 | struct sk_buff *skb, u16 timeout, |
| 1177 | nfc_digital_cmd_complete_t cb, void *arg) |
| 1178 | { |
| 1179 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 1180 | |
| 1181 | dev_dbg(trf->dev, "Unsupported interface\n"); |
| 1182 | |
| 1183 | return -EINVAL; |
| 1184 | } |
| 1185 | |
| 1186 | static int trf7970a_tg_listen(struct nfc_digital_dev *ddev, |
| 1187 | u16 timeout, nfc_digital_cmd_complete_t cb, void *arg) |
| 1188 | { |
| 1189 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 1190 | |
| 1191 | dev_dbg(trf->dev, "Unsupported interface\n"); |
| 1192 | |
| 1193 | return -EINVAL; |
| 1194 | } |
| 1195 | |
| 1196 | static int trf7970a_tg_listen_mdaa(struct nfc_digital_dev *ddev, |
| 1197 | struct digital_tg_mdaa_params *mdaa_params, |
| 1198 | u16 timeout, nfc_digital_cmd_complete_t cb, void *arg) |
| 1199 | { |
| 1200 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 1201 | |
| 1202 | dev_dbg(trf->dev, "Unsupported interface\n"); |
| 1203 | |
| 1204 | return -EINVAL; |
| 1205 | } |
| 1206 | |
| 1207 | static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev) |
| 1208 | { |
| 1209 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); |
| 1210 | |
| 1211 | dev_dbg(trf->dev, "Abort process initiated\n"); |
| 1212 | |
| 1213 | mutex_lock(&trf->lock); |
Mark A. Greer | 5876bc7 | 2014-03-25 08:54:30 -0700 | [diff] [blame] | 1214 | |
| 1215 | switch (trf->state) { |
| 1216 | case TRF7970A_ST_WAIT_FOR_TX_FIFO: |
| 1217 | case TRF7970A_ST_WAIT_FOR_RX_DATA: |
| 1218 | case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: |
| 1219 | case TRF7970A_ST_WAIT_TO_ISSUE_EOF: |
| 1220 | trf->aborting = true; |
| 1221 | break; |
| 1222 | default: |
| 1223 | break; |
| 1224 | } |
| 1225 | |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1226 | mutex_unlock(&trf->lock); |
| 1227 | } |
| 1228 | |
| 1229 | static struct nfc_digital_ops trf7970a_nfc_ops = { |
| 1230 | .in_configure_hw = trf7970a_in_configure_hw, |
| 1231 | .in_send_cmd = trf7970a_in_send_cmd, |
| 1232 | .tg_configure_hw = trf7970a_tg_configure_hw, |
| 1233 | .tg_send_cmd = trf7970a_tg_send_cmd, |
| 1234 | .tg_listen = trf7970a_tg_listen, |
| 1235 | .tg_listen_mdaa = trf7970a_tg_listen_mdaa, |
| 1236 | .switch_rf = trf7970a_switch_rf, |
| 1237 | .abort_cmd = trf7970a_abort_cmd, |
| 1238 | }; |
| 1239 | |
| 1240 | static int trf7970a_probe(struct spi_device *spi) |
| 1241 | { |
| 1242 | struct device_node *np = spi->dev.of_node; |
| 1243 | const struct spi_device_id *id = spi_get_device_id(spi); |
| 1244 | struct trf7970a *trf; |
| 1245 | int ret; |
| 1246 | |
| 1247 | if (!np) { |
| 1248 | dev_err(&spi->dev, "No Device Tree entry\n"); |
| 1249 | return -EINVAL; |
| 1250 | } |
| 1251 | |
| 1252 | trf = devm_kzalloc(&spi->dev, sizeof(*trf), GFP_KERNEL); |
| 1253 | if (!trf) |
| 1254 | return -ENOMEM; |
| 1255 | |
| 1256 | trf->state = TRF7970A_ST_OFF; |
| 1257 | trf->dev = &spi->dev; |
| 1258 | trf->spi = spi; |
| 1259 | trf->quirks = id->driver_data; |
| 1260 | |
| 1261 | spi->mode = SPI_MODE_1; |
| 1262 | spi->bits_per_word = 8; |
| 1263 | |
| 1264 | /* There are two enable pins - both must be present */ |
| 1265 | trf->en_gpio = of_get_named_gpio(np, "ti,enable-gpios", 0); |
| 1266 | if (!gpio_is_valid(trf->en_gpio)) { |
| 1267 | dev_err(trf->dev, "No EN GPIO property\n"); |
| 1268 | return trf->en_gpio; |
| 1269 | } |
| 1270 | |
| 1271 | ret = devm_gpio_request_one(trf->dev, trf->en_gpio, |
| 1272 | GPIOF_DIR_OUT | GPIOF_INIT_LOW, "EN"); |
| 1273 | if (ret) { |
| 1274 | dev_err(trf->dev, "Can't request EN GPIO: %d\n", ret); |
| 1275 | return ret; |
| 1276 | } |
| 1277 | |
| 1278 | trf->en2_gpio = of_get_named_gpio(np, "ti,enable-gpios", 1); |
| 1279 | if (!gpio_is_valid(trf->en2_gpio)) { |
| 1280 | dev_err(trf->dev, "No EN2 GPIO property\n"); |
| 1281 | return trf->en2_gpio; |
| 1282 | } |
| 1283 | |
| 1284 | ret = devm_gpio_request_one(trf->dev, trf->en2_gpio, |
| 1285 | GPIOF_DIR_OUT | GPIOF_INIT_LOW, "EN2"); |
| 1286 | if (ret) { |
| 1287 | dev_err(trf->dev, "Can't request EN2 GPIO: %d\n", ret); |
| 1288 | return ret; |
| 1289 | } |
| 1290 | |
| 1291 | ret = devm_request_threaded_irq(trf->dev, spi->irq, NULL, |
| 1292 | trf7970a_irq, IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
| 1293 | "trf7970a", trf); |
| 1294 | if (ret) { |
| 1295 | dev_err(trf->dev, "Can't request IRQ#%d: %d\n", spi->irq, ret); |
| 1296 | return ret; |
| 1297 | } |
| 1298 | |
| 1299 | mutex_init(&trf->lock); |
| 1300 | INIT_DELAYED_WORK(&trf->timeout_work, trf7970a_timeout_work_handler); |
| 1301 | |
| 1302 | trf->regulator = devm_regulator_get(&spi->dev, "vin"); |
| 1303 | if (IS_ERR(trf->regulator)) { |
| 1304 | ret = PTR_ERR(trf->regulator); |
| 1305 | dev_err(trf->dev, "Can't get VIN regulator: %d\n", ret); |
| 1306 | goto err_destroy_lock; |
| 1307 | } |
| 1308 | |
| 1309 | ret = regulator_enable(trf->regulator); |
| 1310 | if (ret) { |
| 1311 | dev_err(trf->dev, "Can't enable VIN: %d\n", ret); |
| 1312 | goto err_destroy_lock; |
| 1313 | } |
| 1314 | |
| 1315 | trf->powering_up = true; |
| 1316 | |
| 1317 | trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops, |
| 1318 | TRF7970A_SUPPORTED_PROTOCOLS, |
| 1319 | NFC_DIGITAL_DRV_CAPS_IN_CRC, TRF7970A_TX_SKB_HEADROOM, |
| 1320 | 0); |
| 1321 | if (!trf->ddev) { |
| 1322 | dev_err(trf->dev, "Can't allocate NFC digital device\n"); |
| 1323 | ret = -ENOMEM; |
| 1324 | goto err_disable_regulator; |
| 1325 | } |
| 1326 | |
| 1327 | nfc_digital_set_parent_dev(trf->ddev, trf->dev); |
| 1328 | nfc_digital_set_drvdata(trf->ddev, trf); |
| 1329 | spi_set_drvdata(spi, trf); |
| 1330 | |
| 1331 | ret = nfc_digital_register_device(trf->ddev); |
| 1332 | if (ret) { |
| 1333 | dev_err(trf->dev, "Can't register NFC digital device: %d\n", |
| 1334 | ret); |
| 1335 | goto err_free_ddev; |
| 1336 | } |
| 1337 | |
| 1338 | return 0; |
| 1339 | |
| 1340 | err_free_ddev: |
| 1341 | nfc_digital_free_device(trf->ddev); |
| 1342 | err_disable_regulator: |
| 1343 | regulator_disable(trf->regulator); |
| 1344 | err_destroy_lock: |
| 1345 | mutex_destroy(&trf->lock); |
| 1346 | return ret; |
| 1347 | } |
| 1348 | |
| 1349 | static int trf7970a_remove(struct spi_device *spi) |
| 1350 | { |
| 1351 | struct trf7970a *trf = spi_get_drvdata(spi); |
| 1352 | |
| 1353 | mutex_lock(&trf->lock); |
| 1354 | |
| 1355 | trf7970a_switch_rf_off(trf); |
| 1356 | trf7970a_init(trf); |
| 1357 | |
| 1358 | switch (trf->state) { |
| 1359 | case TRF7970A_ST_WAIT_FOR_TX_FIFO: |
| 1360 | case TRF7970A_ST_WAIT_FOR_RX_DATA: |
| 1361 | case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: |
Mark A. Greer | 9d9304b | 2014-03-10 11:56:24 -0700 | [diff] [blame] | 1362 | case TRF7970A_ST_WAIT_TO_ISSUE_EOF: |
Mark A. Greer | 165063f | 2014-03-10 11:56:22 -0700 | [diff] [blame] | 1363 | trf7970a_send_err_upstream(trf, -ECANCELED); |
| 1364 | break; |
| 1365 | default: |
| 1366 | break; |
| 1367 | } |
| 1368 | |
| 1369 | mutex_unlock(&trf->lock); |
| 1370 | |
| 1371 | nfc_digital_unregister_device(trf->ddev); |
| 1372 | nfc_digital_free_device(trf->ddev); |
| 1373 | |
| 1374 | regulator_disable(trf->regulator); |
| 1375 | |
| 1376 | mutex_destroy(&trf->lock); |
| 1377 | |
| 1378 | return 0; |
| 1379 | } |
| 1380 | |
| 1381 | static const struct spi_device_id trf7970a_id_table[] = { |
| 1382 | { "trf7970a", TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA }, |
| 1383 | { } |
| 1384 | }; |
| 1385 | MODULE_DEVICE_TABLE(spi, trf7970a_id_table); |
| 1386 | |
| 1387 | static struct spi_driver trf7970a_spi_driver = { |
| 1388 | .probe = trf7970a_probe, |
| 1389 | .remove = trf7970a_remove, |
| 1390 | .id_table = trf7970a_id_table, |
| 1391 | .driver = { |
| 1392 | .name = "trf7970a", |
| 1393 | .owner = THIS_MODULE, |
| 1394 | }, |
| 1395 | }; |
| 1396 | |
| 1397 | module_spi_driver(trf7970a_spi_driver); |
| 1398 | |
| 1399 | MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>"); |
| 1400 | MODULE_LICENSE("GPL v2"); |
| 1401 | MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver"); |