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