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