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