2 * TI TRF7970a RFID/NFC Transceiver Driver
4 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
6 * Author: Erick Macias <emacias@ti.com>
7 * Author: Felipe Balbi <balbi@ti.com>
8 * Author: Mark A. Greer <mgreer@animalcreek.com>
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.
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/netdevice.h>
18 #include <linux/interrupt.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/nfc.h>
21 #include <linux/skbuff.h>
22 #include <linux/delay.h>
23 #include <linux/gpio/consumer.h>
25 #include <linux/spi/spi.h>
26 #include <linux/regulator/consumer.h>
28 #include <net/nfc/nfc.h>
29 #include <net/nfc/digital.h>
31 /* There are 3 ways the host can communicate with the trf7970a:
32 * parallel mode, SPI with Slave Select (SS) mode, and SPI without
33 * SS mode. The driver only supports the two SPI modes.
35 * The trf7970a is very timing sensitive and the VIN, EN2, and EN
36 * pins must asserted in that order and with specific delays in between.
37 * The delays used in the driver were provided by TI and have been
38 * confirmed to work with this driver. There is a bug with the current
39 * version of the trf7970a that requires that EN2 remain low no matter
40 * what. If it goes high, it will generate an RF field even when in
41 * passive target mode. TI has indicated that the chip will work okay
42 * when EN2 is left low. The 'en2-rf-quirk' device tree property
43 * indicates that trf7970a currently being used has the erratum and
44 * that EN2 must be kept low.
46 * Timeouts are implemented using the delayed workqueue kernel facility.
47 * Timeouts are required so things don't hang when there is no response
48 * from the trf7970a (or tag). Using this mechanism creates a race with
49 * interrupts, however. That is, an interrupt and a timeout could occur
50 * closely enough together that one is blocked by the mutex while the other
51 * executes. When the timeout handler executes first and blocks the
52 * interrupt handler, it will eventually set the state to IDLE so the
53 * interrupt handler will check the state and exit with no harm done.
54 * When the interrupt handler executes first and blocks the timeout handler,
55 * the cancel_delayed_work() call will know that it didn't cancel the
56 * work item (i.e., timeout) and will return zero. That return code is
57 * used by the timer handler to indicate that it should ignore the timeout
60 * Aborting an active command isn't as simple as it seems because the only
61 * way to abort a command that's already been sent to the tag is so turn
62 * off power to the tag. If we do that, though, we'd have to go through
63 * the entire anticollision procedure again but the digital layer doesn't
64 * support that. So, if an abort is received before trf7970a_send_cmd()
65 * has sent the command to the tag, it simply returns -ECANCELED. If the
66 * command has already been sent to the tag, then the driver continues
67 * normally and recieves the response data (or error) but just before
68 * sending the data upstream, it frees the rx_skb and sends -ECANCELED
69 * upstream instead. If the command failed, that error will be sent
72 * When recieving data from a tag and the interrupt status register has
73 * only the SRX bit set, it means that all of the data has been received
74 * (once what's in the fifo has been read). However, depending on timing
75 * an interrupt status with only the SRX bit set may not be recived. In
76 * those cases, the timeout mechanism is used to wait 20 ms in case more
77 * data arrives. After 20 ms, it is assumed that all of the data has been
78 * received and the accumulated rx data is sent upstream. The
79 * 'TRF7970A_ST_WAIT_FOR_RX_DATA_CONT' state is used for this purpose
80 * (i.e., it indicates that some data has been received but we're not sure
81 * if there is more coming so a timeout in this state means all data has
82 * been received and there isn't an error). The delay is 20 ms since delays
83 * of ~16 ms have been observed during testing.
85 * When transmitting a frame larger than the FIFO size (127 bytes), the
86 * driver will wait 20 ms for the FIFO to drain past the low-watermark
87 * and generate an interrupt. The low-watermark set to 32 bytes so the
88 * interrupt should fire after 127 - 32 = 95 bytes have been sent. At
89 * the lowest possible bit rate (6.62 kbps for 15693), it will take up
90 * to ~14.35 ms so 20 ms is used for the timeout.
92 * Type 2 write and sector select commands respond with a 4-bit ACK or NACK.
93 * Having only 4 bits in the FIFO won't normally generate an interrupt so
94 * driver enables the '4_bit_RX' bit of the Special Functions register 1
95 * to cause an interrupt in that case. Leaving that bit for a read command
96 * messes up the data returned so it is only enabled when the framing is
97 * 'NFC_DIGITAL_FRAMING_NFCA_T2T' and the command is not a read command.
98 * Unfortunately, that means that the driver has to peek into tx frames
99 * when the framing is 'NFC_DIGITAL_FRAMING_NFCA_T2T'. This is done by
100 * the trf7970a_per_cmd_config() routine.
102 * ISO/IEC 15693 frames specify whether to use single or double sub-carrier
103 * frequencies and whether to use low or high data rates in the flags byte
104 * of the frame. This means that the driver has to peek at all 15693 frames
105 * to determine what speed to set the communication to. In addition, write
106 * and lock commands use the OPTION flag to indicate that an EOF must be
107 * sent to the tag before it will send its response. So the driver has to
108 * examine all frames for that reason too.
110 * It is unclear how long to wait before sending the EOF. According to the
111 * Note under Table 1-1 in section 1.6 of
112 * http://www.ti.com/lit/ug/scbu011/scbu011.pdf, that wait should be at least
113 * 10 ms for TI Tag-it HF-I tags; however testing has shown that is not long
114 * enough so 20 ms is used. So the timer is set to 40 ms - 20 ms to drain
115 * up to 127 bytes in the FIFO at the lowest bit rate plus another 20 ms to
116 * ensure the wait is long enough before sending the EOF. This seems to work
120 #define TRF7970A_SUPPORTED_PROTOCOLS \
121 (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \
122 NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_FELICA_MASK | \
123 NFC_PROTO_ISO15693_MASK | NFC_PROTO_NFC_DEP_MASK)
125 #define TRF7970A_AUTOSUSPEND_DELAY 30000 /* 30 seconds */
126 #define TRF7970A_13MHZ_CLOCK_FREQUENCY 13560000
127 #define TRF7970A_27MHZ_CLOCK_FREQUENCY 27120000
130 #define TRF7970A_RX_SKB_ALLOC_SIZE 256
132 #define TRF7970A_FIFO_SIZE 127
134 /* TX length is 3 nibbles long ==> 4KB - 1 bytes max */
135 #define TRF7970A_TX_MAX (4096 - 1)
137 #define TRF7970A_WAIT_FOR_TX_IRQ 20
138 #define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 20
139 #define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 20
140 #define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 40
142 /* Guard times for various RF technologies (in us) */
143 #define TRF7970A_GUARD_TIME_NFCA 5000
144 #define TRF7970A_GUARD_TIME_NFCB 5000
145 #define TRF7970A_GUARD_TIME_NFCF 20000
146 #define TRF7970A_GUARD_TIME_15693 1000
149 /* Erratum: When reading IRQ Status register on trf7970a, we must issue a
150 * read continuous command for IRQ Status and Collision Position registers.
152 #define TRF7970A_QUIRK_IRQ_STATUS_READ BIT(0)
153 #define TRF7970A_QUIRK_EN2_MUST_STAY_LOW BIT(1)
154 #define TRF7970A_QUIRK_T5T_RMB_EXTRA_BYTE BIT(2)
156 /* Direct commands */
157 #define TRF7970A_CMD_IDLE 0x00
158 #define TRF7970A_CMD_SOFT_INIT 0x03
159 #define TRF7970A_CMD_RF_COLLISION 0x04
160 #define TRF7970A_CMD_RF_COLLISION_RESPONSE_N 0x05
161 #define TRF7970A_CMD_RF_COLLISION_RESPONSE_0 0x06
162 #define TRF7970A_CMD_FIFO_RESET 0x0f
163 #define TRF7970A_CMD_TRANSMIT_NO_CRC 0x10
164 #define TRF7970A_CMD_TRANSMIT 0x11
165 #define TRF7970A_CMD_DELAY_TRANSMIT_NO_CRC 0x12
166 #define TRF7970A_CMD_DELAY_TRANSMIT 0x13
167 #define TRF7970A_CMD_EOF 0x14
168 #define TRF7970A_CMD_CLOSE_SLOT 0x15
169 #define TRF7970A_CMD_BLOCK_RX 0x16
170 #define TRF7970A_CMD_ENABLE_RX 0x17
171 #define TRF7970A_CMD_TEST_INT_RF 0x18
172 #define TRF7970A_CMD_TEST_EXT_RF 0x19
173 #define TRF7970A_CMD_RX_GAIN_ADJUST 0x1a
175 /* Bits determining whether its a direct command or register R/W,
176 * whether to use a continuous SPI transaction or not, and the actual
177 * direct cmd opcode or regster address.
179 #define TRF7970A_CMD_BIT_CTRL BIT(7)
180 #define TRF7970A_CMD_BIT_RW BIT(6)
181 #define TRF7970A_CMD_BIT_CONTINUOUS BIT(5)
182 #define TRF7970A_CMD_BIT_OPCODE(opcode) ((opcode) & 0x1f)
184 /* Registers addresses */
185 #define TRF7970A_CHIP_STATUS_CTRL 0x00
186 #define TRF7970A_ISO_CTRL 0x01
187 #define TRF7970A_ISO14443B_TX_OPTIONS 0x02
188 #define TRF7970A_ISO14443A_HIGH_BITRATE_OPTIONS 0x03
189 #define TRF7970A_TX_TIMER_SETTING_H_BYTE 0x04
190 #define TRF7970A_TX_TIMER_SETTING_L_BYTE 0x05
191 #define TRF7970A_TX_PULSE_LENGTH_CTRL 0x06
192 #define TRF7970A_RX_NO_RESPONSE_WAIT 0x07
193 #define TRF7970A_RX_WAIT_TIME 0x08
194 #define TRF7970A_MODULATOR_SYS_CLK_CTRL 0x09
195 #define TRF7970A_RX_SPECIAL_SETTINGS 0x0a
196 #define TRF7970A_REG_IO_CTRL 0x0b
197 #define TRF7970A_IRQ_STATUS 0x0c
198 #define TRF7970A_COLLISION_IRQ_MASK 0x0d
199 #define TRF7970A_COLLISION_POSITION 0x0e
200 #define TRF7970A_RSSI_OSC_STATUS 0x0f
201 #define TRF7970A_SPECIAL_FCN_REG1 0x10
202 #define TRF7970A_SPECIAL_FCN_REG2 0x11
203 #define TRF7970A_RAM1 0x12
204 #define TRF7970A_RAM2 0x13
205 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS 0x14
206 #define TRF7970A_NFC_LOW_FIELD_LEVEL 0x16
207 #define TRF7970A_NFCID1 0x17
208 #define TRF7970A_NFC_TARGET_LEVEL 0x18
209 #define TRF79070A_NFC_TARGET_PROTOCOL 0x19
210 #define TRF7970A_TEST_REGISTER1 0x1a
211 #define TRF7970A_TEST_REGISTER2 0x1b
212 #define TRF7970A_FIFO_STATUS 0x1c
213 #define TRF7970A_TX_LENGTH_BYTE1 0x1d
214 #define TRF7970A_TX_LENGTH_BYTE2 0x1e
215 #define TRF7970A_FIFO_IO_REGISTER 0x1f
217 /* Chip Status Control Register Bits */
218 #define TRF7970A_CHIP_STATUS_VRS5_3 BIT(0)
219 #define TRF7970A_CHIP_STATUS_REC_ON BIT(1)
220 #define TRF7970A_CHIP_STATUS_AGC_ON BIT(2)
221 #define TRF7970A_CHIP_STATUS_PM_ON BIT(3)
222 #define TRF7970A_CHIP_STATUS_RF_PWR BIT(4)
223 #define TRF7970A_CHIP_STATUS_RF_ON BIT(5)
224 #define TRF7970A_CHIP_STATUS_DIRECT BIT(6)
225 #define TRF7970A_CHIP_STATUS_STBY BIT(7)
227 /* ISO Control Register Bits */
228 #define TRF7970A_ISO_CTRL_15693_SGL_1OF4_662 0x00
229 #define TRF7970A_ISO_CTRL_15693_SGL_1OF256_662 0x01
230 #define TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648 0x02
231 #define TRF7970A_ISO_CTRL_15693_SGL_1OF256_2648 0x03
232 #define TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a 0x04
233 #define TRF7970A_ISO_CTRL_15693_DBL_1OF256_667 0x05
234 #define TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669 0x06
235 #define TRF7970A_ISO_CTRL_15693_DBL_1OF256_2669 0x07
236 #define TRF7970A_ISO_CTRL_14443A_106 0x08
237 #define TRF7970A_ISO_CTRL_14443A_212 0x09
238 #define TRF7970A_ISO_CTRL_14443A_424 0x0a
239 #define TRF7970A_ISO_CTRL_14443A_848 0x0b
240 #define TRF7970A_ISO_CTRL_14443B_106 0x0c
241 #define TRF7970A_ISO_CTRL_14443B_212 0x0d
242 #define TRF7970A_ISO_CTRL_14443B_424 0x0e
243 #define TRF7970A_ISO_CTRL_14443B_848 0x0f
244 #define TRF7970A_ISO_CTRL_FELICA_212 0x1a
245 #define TRF7970A_ISO_CTRL_FELICA_424 0x1b
246 #define TRF7970A_ISO_CTRL_NFC_NFCA_106 0x01
247 #define TRF7970A_ISO_CTRL_NFC_NFCF_212 0x02
248 #define TRF7970A_ISO_CTRL_NFC_NFCF_424 0x03
249 #define TRF7970A_ISO_CTRL_NFC_CE_14443A 0x00
250 #define TRF7970A_ISO_CTRL_NFC_CE_14443B 0x01
251 #define TRF7970A_ISO_CTRL_NFC_CE BIT(2)
252 #define TRF7970A_ISO_CTRL_NFC_ACTIVE BIT(3)
253 #define TRF7970A_ISO_CTRL_NFC_INITIATOR BIT(4)
254 #define TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE BIT(5)
255 #define TRF7970A_ISO_CTRL_RFID BIT(5)
256 #define TRF7970A_ISO_CTRL_DIR_MODE BIT(6)
257 #define TRF7970A_ISO_CTRL_RX_CRC_N BIT(7) /* true == No CRC */
259 #define TRF7970A_ISO_CTRL_RFID_SPEED_MASK 0x1f
261 /* Modulator and SYS_CLK Control Register Bits */
262 #define TRF7970A_MODULATOR_DEPTH(n) ((n) & 0x7)
263 #define TRF7970A_MODULATOR_DEPTH_ASK10 (TRF7970A_MODULATOR_DEPTH(0))
264 #define TRF7970A_MODULATOR_DEPTH_OOK (TRF7970A_MODULATOR_DEPTH(1))
265 #define TRF7970A_MODULATOR_DEPTH_ASK7 (TRF7970A_MODULATOR_DEPTH(2))
266 #define TRF7970A_MODULATOR_DEPTH_ASK8_5 (TRF7970A_MODULATOR_DEPTH(3))
267 #define TRF7970A_MODULATOR_DEPTH_ASK13 (TRF7970A_MODULATOR_DEPTH(4))
268 #define TRF7970A_MODULATOR_DEPTH_ASK16 (TRF7970A_MODULATOR_DEPTH(5))
269 #define TRF7970A_MODULATOR_DEPTH_ASK22 (TRF7970A_MODULATOR_DEPTH(6))
270 #define TRF7970A_MODULATOR_DEPTH_ASK30 (TRF7970A_MODULATOR_DEPTH(7))
271 #define TRF7970A_MODULATOR_EN_ANA BIT(3)
272 #define TRF7970A_MODULATOR_CLK(n) (((n) & 0x3) << 4)
273 #define TRF7970A_MODULATOR_CLK_DISABLED (TRF7970A_MODULATOR_CLK(0))
274 #define TRF7970A_MODULATOR_CLK_3_6 (TRF7970A_MODULATOR_CLK(1))
275 #define TRF7970A_MODULATOR_CLK_6_13 (TRF7970A_MODULATOR_CLK(2))
276 #define TRF7970A_MODULATOR_CLK_13_27 (TRF7970A_MODULATOR_CLK(3))
277 #define TRF7970A_MODULATOR_EN_OOK BIT(6)
278 #define TRF7970A_MODULATOR_27MHZ BIT(7)
280 #define TRF7970A_RX_SPECIAL_SETTINGS_NO_LIM BIT(0)
281 #define TRF7970A_RX_SPECIAL_SETTINGS_AGCR BIT(1)
282 #define TRF7970A_RX_SPECIAL_SETTINGS_GD_0DB (0x0 << 2)
283 #define TRF7970A_RX_SPECIAL_SETTINGS_GD_5DB (0x1 << 2)
284 #define TRF7970A_RX_SPECIAL_SETTINGS_GD_10DB (0x2 << 2)
285 #define TRF7970A_RX_SPECIAL_SETTINGS_GD_15DB (0x3 << 2)
286 #define TRF7970A_RX_SPECIAL_SETTINGS_HBT BIT(4)
287 #define TRF7970A_RX_SPECIAL_SETTINGS_M848 BIT(5)
288 #define TRF7970A_RX_SPECIAL_SETTINGS_C424 BIT(6)
289 #define TRF7970A_RX_SPECIAL_SETTINGS_C212 BIT(7)
291 #define TRF7970A_REG_IO_CTRL_VRS(v) ((v) & 0x07)
292 #define TRF7970A_REG_IO_CTRL_IO_LOW BIT(5)
293 #define TRF7970A_REG_IO_CTRL_EN_EXT_PA BIT(6)
294 #define TRF7970A_REG_IO_CTRL_AUTO_REG BIT(7)
296 /* IRQ Status Register Bits */
297 #define TRF7970A_IRQ_STATUS_NORESP BIT(0) /* ISO15693 only */
298 #define TRF7970A_IRQ_STATUS_NFC_COL_ERROR BIT(0)
299 #define TRF7970A_IRQ_STATUS_COL BIT(1)
300 #define TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR BIT(2)
301 #define TRF7970A_IRQ_STATUS_NFC_RF BIT(2)
302 #define TRF7970A_IRQ_STATUS_PARITY_ERROR BIT(3)
303 #define TRF7970A_IRQ_STATUS_NFC_SDD BIT(3)
304 #define TRF7970A_IRQ_STATUS_CRC_ERROR BIT(4)
305 #define TRF7970A_IRQ_STATUS_NFC_PROTO_ERROR BIT(4)
306 #define TRF7970A_IRQ_STATUS_FIFO BIT(5)
307 #define TRF7970A_IRQ_STATUS_SRX BIT(6)
308 #define TRF7970A_IRQ_STATUS_TX BIT(7)
310 #define TRF7970A_IRQ_STATUS_ERROR \
311 (TRF7970A_IRQ_STATUS_COL | \
312 TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR | \
313 TRF7970A_IRQ_STATUS_PARITY_ERROR | \
314 TRF7970A_IRQ_STATUS_CRC_ERROR)
316 #define TRF7970A_RSSI_OSC_STATUS_RSSI_MASK (BIT(2) | BIT(1) | BIT(0))
317 #define TRF7970A_RSSI_OSC_STATUS_RSSI_X_MASK (BIT(5) | BIT(4) | BIT(3))
318 #define TRF7970A_RSSI_OSC_STATUS_RSSI_OSC_OK BIT(6)
320 #define TRF7970A_SPECIAL_FCN_REG1_COL_7_6 BIT(0)
321 #define TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL BIT(1)
322 #define TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX BIT(2)
323 #define TRF7970A_SPECIAL_FCN_REG1_SP_DIR_MODE BIT(3)
324 #define TRF7970A_SPECIAL_FCN_REG1_NEXT_SLOT_37US BIT(4)
325 #define TRF7970A_SPECIAL_FCN_REG1_PAR43 BIT(5)
327 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_124 (0x0 << 2)
328 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_120 (0x1 << 2)
329 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_112 (0x2 << 2)
330 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 (0x3 << 2)
331 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_4 0x0
332 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_8 0x1
333 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_16 0x2
334 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32 0x3
336 #define TRF7970A_NFC_LOW_FIELD_LEVEL_RFDET(v) ((v) & 0x07)
337 #define TRF7970A_NFC_LOW_FIELD_LEVEL_CLEX_DIS BIT(7)
339 #define TRF7970A_NFC_TARGET_LEVEL_RFDET(v) ((v) & 0x07)
340 #define TRF7970A_NFC_TARGET_LEVEL_HI_RF BIT(3)
341 #define TRF7970A_NFC_TARGET_LEVEL_SDD_EN BIT(5)
342 #define TRF7970A_NFC_TARGET_LEVEL_LD_S_4BYTES (0x0 << 6)
343 #define TRF7970A_NFC_TARGET_LEVEL_LD_S_7BYTES (0x1 << 6)
344 #define TRF7970A_NFC_TARGET_LEVEL_LD_S_10BYTES (0x2 << 6)
346 #define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106 BIT(0)
347 #define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_212 BIT(1)
348 #define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_424 (BIT(0) | BIT(1))
349 #define TRF79070A_NFC_TARGET_PROTOCOL_PAS_14443B BIT(2)
350 #define TRF79070A_NFC_TARGET_PROTOCOL_PAS_106 BIT(3)
351 #define TRF79070A_NFC_TARGET_PROTOCOL_FELICA BIT(4)
352 #define TRF79070A_NFC_TARGET_PROTOCOL_RF_L BIT(6)
353 #define TRF79070A_NFC_TARGET_PROTOCOL_RF_H BIT(7)
355 #define TRF79070A_NFC_TARGET_PROTOCOL_106A \
356 (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \
357 TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \
358 TRF79070A_NFC_TARGET_PROTOCOL_PAS_106 | \
359 TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106)
361 #define TRF79070A_NFC_TARGET_PROTOCOL_106B \
362 (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \
363 TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \
364 TRF79070A_NFC_TARGET_PROTOCOL_PAS_14443B | \
365 TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106)
367 #define TRF79070A_NFC_TARGET_PROTOCOL_212F \
368 (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \
369 TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \
370 TRF79070A_NFC_TARGET_PROTOCOL_FELICA | \
371 TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_212)
373 #define TRF79070A_NFC_TARGET_PROTOCOL_424F \
374 (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \
375 TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \
376 TRF79070A_NFC_TARGET_PROTOCOL_FELICA | \
377 TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_424)
379 #define TRF7970A_FIFO_STATUS_OVERFLOW BIT(7)
381 /* NFC (ISO/IEC 14443A) Type 2 Tag commands */
382 #define NFC_T2T_CMD_READ 0x30
384 /* ISO 15693 commands codes */
385 #define ISO15693_CMD_INVENTORY 0x01
386 #define ISO15693_CMD_READ_SINGLE_BLOCK 0x20
387 #define ISO15693_CMD_WRITE_SINGLE_BLOCK 0x21
388 #define ISO15693_CMD_LOCK_BLOCK 0x22
389 #define ISO15693_CMD_READ_MULTIPLE_BLOCK 0x23
390 #define ISO15693_CMD_WRITE_MULTIPLE_BLOCK 0x24
391 #define ISO15693_CMD_SELECT 0x25
392 #define ISO15693_CMD_RESET_TO_READY 0x26
393 #define ISO15693_CMD_WRITE_AFI 0x27
394 #define ISO15693_CMD_LOCK_AFI 0x28
395 #define ISO15693_CMD_WRITE_DSFID 0x29
396 #define ISO15693_CMD_LOCK_DSFID 0x2a
397 #define ISO15693_CMD_GET_SYSTEM_INFO 0x2b
398 #define ISO15693_CMD_GET_MULTIPLE_BLOCK_SECURITY_STATUS 0x2c
400 /* ISO 15693 request and response flags */
401 #define ISO15693_REQ_FLAG_SUB_CARRIER BIT(0)
402 #define ISO15693_REQ_FLAG_DATA_RATE BIT(1)
403 #define ISO15693_REQ_FLAG_INVENTORY BIT(2)
404 #define ISO15693_REQ_FLAG_PROTOCOL_EXT BIT(3)
405 #define ISO15693_REQ_FLAG_SELECT BIT(4)
406 #define ISO15693_REQ_FLAG_AFI BIT(4)
407 #define ISO15693_REQ_FLAG_ADDRESS BIT(5)
408 #define ISO15693_REQ_FLAG_NB_SLOTS BIT(5)
409 #define ISO15693_REQ_FLAG_OPTION BIT(6)
411 #define ISO15693_REQ_FLAG_SPEED_MASK \
412 (ISO15693_REQ_FLAG_SUB_CARRIER | ISO15693_REQ_FLAG_DATA_RATE)
414 enum trf7970a_state {
418 TRF7970A_ST_IDLE_RX_BLOCKED,
419 TRF7970A_ST_WAIT_FOR_TX_FIFO,
420 TRF7970A_ST_WAIT_FOR_RX_DATA,
421 TRF7970A_ST_WAIT_FOR_RX_DATA_CONT,
422 TRF7970A_ST_WAIT_TO_ISSUE_EOF,
423 TRF7970A_ST_LISTENING,
424 TRF7970A_ST_LISTENING_MD,
429 enum trf7970a_state state;
431 struct spi_device *spi;
432 struct regulator *regulator;
433 struct nfc_digital_dev *ddev;
437 struct sk_buff *tx_skb;
438 struct sk_buff *rx_skb;
439 nfc_digital_cmd_complete_t cb;
444 u8 modulator_sys_clk_ctrl;
447 unsigned int guard_time;
453 bool adjust_resp_len;
454 struct gpio_desc *en_gpiod;
455 struct gpio_desc *en2_gpiod;
457 unsigned int timeout;
459 struct delayed_work timeout_work;
463 static int trf7970a_cmd(struct trf7970a *trf, u8 opcode)
465 u8 cmd = TRF7970A_CMD_BIT_CTRL | TRF7970A_CMD_BIT_OPCODE(opcode);
468 dev_dbg(trf->dev, "cmd: 0x%x\n", cmd);
470 ret = spi_write(trf->spi, &cmd, 1);
472 dev_err(trf->dev, "%s - cmd: 0x%x, ret: %d\n", __func__, cmd,
477 static int trf7970a_read(struct trf7970a *trf, u8 reg, u8 *val)
479 u8 addr = TRF7970A_CMD_BIT_RW | reg;
482 ret = spi_write_then_read(trf->spi, &addr, 1, val, 1);
484 dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr,
487 dev_dbg(trf->dev, "read(0x%x): 0x%x\n", addr, *val);
492 static int trf7970a_read_cont(struct trf7970a *trf, u8 reg, u8 *buf, size_t len)
494 u8 addr = reg | TRF7970A_CMD_BIT_RW | TRF7970A_CMD_BIT_CONTINUOUS;
495 struct spi_transfer t[2];
496 struct spi_message m;
499 dev_dbg(trf->dev, "read_cont(0x%x, %zd)\n", addr, len);
501 spi_message_init(&m);
503 memset(&t, 0, sizeof(t));
506 t[0].len = sizeof(addr);
507 spi_message_add_tail(&t[0], &m);
511 spi_message_add_tail(&t[1], &m);
513 ret = spi_sync(trf->spi, &m);
515 dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr,
520 static int trf7970a_write(struct trf7970a *trf, u8 reg, u8 val)
522 u8 buf[2] = { reg, val };
525 dev_dbg(trf->dev, "write(0x%x): 0x%x\n", reg, val);
527 ret = spi_write(trf->spi, buf, 2);
529 dev_err(trf->dev, "%s - write: 0x%x 0x%x, ret: %d\n", __func__,
530 buf[0], buf[1], ret);
535 static int trf7970a_read_irqstatus(struct trf7970a *trf, u8 *status)
541 addr = TRF7970A_IRQ_STATUS | TRF7970A_CMD_BIT_RW;
543 if (trf->quirks & TRF7970A_QUIRK_IRQ_STATUS_READ) {
544 addr |= TRF7970A_CMD_BIT_CONTINUOUS;
545 ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2);
547 ret = spi_write_then_read(trf->spi, &addr, 1, buf, 1);
551 dev_err(trf->dev, "%s - irqstatus: Status read failed: %d\n",
559 static int trf7970a_read_target_proto(struct trf7970a *trf, u8 *target_proto)
565 addr = TRF79070A_NFC_TARGET_PROTOCOL | TRF7970A_CMD_BIT_RW |
566 TRF7970A_CMD_BIT_CONTINUOUS;
568 ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2);
570 dev_err(trf->dev, "%s - target_proto: Read failed: %d\n",
573 *target_proto = buf[0];
578 static int trf7970a_mode_detect(struct trf7970a *trf, u8 *rf_tech)
581 u8 target_proto, tech;
583 ret = trf7970a_read_target_proto(trf, &target_proto);
587 switch (target_proto) {
588 case TRF79070A_NFC_TARGET_PROTOCOL_106A:
589 tech = NFC_DIGITAL_RF_TECH_106A;
591 case TRF79070A_NFC_TARGET_PROTOCOL_106B:
592 tech = NFC_DIGITAL_RF_TECH_106B;
594 case TRF79070A_NFC_TARGET_PROTOCOL_212F:
595 tech = NFC_DIGITAL_RF_TECH_212F;
597 case TRF79070A_NFC_TARGET_PROTOCOL_424F:
598 tech = NFC_DIGITAL_RF_TECH_424F;
601 dev_dbg(trf->dev, "%s - mode_detect: target_proto: 0x%x\n",
602 __func__, target_proto);
611 static void trf7970a_send_upstream(struct trf7970a *trf)
613 dev_kfree_skb_any(trf->tx_skb);
616 if (trf->rx_skb && !IS_ERR(trf->rx_skb) && !trf->aborting)
617 print_hex_dump_debug("trf7970a rx data: ", DUMP_PREFIX_NONE,
618 16, 1, trf->rx_skb->data, trf->rx_skb->len,
621 trf->state = TRF7970A_ST_IDLE;
624 dev_dbg(trf->dev, "Abort process complete\n");
626 if (!IS_ERR(trf->rx_skb)) {
627 kfree_skb(trf->rx_skb);
628 trf->rx_skb = ERR_PTR(-ECANCELED);
631 trf->aborting = false;
634 if (trf->adjust_resp_len) {
636 skb_trim(trf->rx_skb, trf->rx_skb->len - 1);
638 trf->adjust_resp_len = false;
641 trf->cb(trf->ddev, trf->cb_arg, trf->rx_skb);
646 static void trf7970a_send_err_upstream(struct trf7970a *trf, int errno)
648 dev_dbg(trf->dev, "Error - state: %d, errno: %d\n", trf->state, errno);
650 cancel_delayed_work(&trf->timeout_work);
652 kfree_skb(trf->rx_skb);
653 trf->rx_skb = ERR_PTR(errno);
655 trf7970a_send_upstream(trf);
658 static int trf7970a_transmit(struct trf7970a *trf, struct sk_buff *skb,
659 unsigned int len, u8 *prefix, unsigned int prefix_len)
661 struct spi_transfer t[2];
662 struct spi_message m;
663 unsigned int timeout;
666 print_hex_dump_debug("trf7970a tx data: ", DUMP_PREFIX_NONE,
667 16, 1, skb->data, len, false);
669 spi_message_init(&m);
671 memset(&t, 0, sizeof(t));
673 t[0].tx_buf = prefix;
674 t[0].len = prefix_len;
675 spi_message_add_tail(&t[0], &m);
677 t[1].tx_buf = skb->data;
679 spi_message_add_tail(&t[1], &m);
681 ret = spi_sync(trf->spi, &m);
683 dev_err(trf->dev, "%s - Can't send tx data: %d\n", __func__,
691 trf->state = TRF7970A_ST_WAIT_FOR_TX_FIFO;
692 timeout = TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT;
694 if (trf->issue_eof) {
695 trf->state = TRF7970A_ST_WAIT_TO_ISSUE_EOF;
696 timeout = TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF;
698 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA;
701 timeout = TRF7970A_WAIT_FOR_TX_IRQ;
703 timeout = trf->timeout;
707 dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", timeout,
710 schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout));
715 static void trf7970a_fill_fifo(struct trf7970a *trf)
717 struct sk_buff *skb = trf->tx_skb;
723 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
725 trf7970a_send_err_upstream(trf, ret);
729 dev_dbg(trf->dev, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes);
731 fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
733 /* Calculate how much more data can be written to the fifo */
734 len = TRF7970A_FIFO_SIZE - fifo_bytes;
736 schedule_delayed_work(&trf->timeout_work,
737 msecs_to_jiffies(TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT));
741 len = min(skb->len, len);
743 prefix = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_FIFO_IO_REGISTER;
745 ret = trf7970a_transmit(trf, skb, len, &prefix, sizeof(prefix));
747 trf7970a_send_err_upstream(trf, ret);
750 static void trf7970a_drain_fifo(struct trf7970a *trf, u8 status)
752 struct sk_buff *skb = trf->rx_skb;
756 if (status & TRF7970A_IRQ_STATUS_ERROR) {
757 trf7970a_send_err_upstream(trf, -EIO);
761 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
763 trf7970a_send_err_upstream(trf, ret);
767 dev_dbg(trf->dev, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes);
769 fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
774 if (fifo_bytes > skb_tailroom(skb)) {
775 skb = skb_copy_expand(skb, skb_headroom(skb),
776 max_t(int, fifo_bytes,
777 TRF7970A_RX_SKB_ALLOC_SIZE),
780 trf7970a_send_err_upstream(trf, -ENOMEM);
784 kfree_skb(trf->rx_skb);
788 ret = trf7970a_read_cont(trf, TRF7970A_FIFO_IO_REGISTER,
789 skb_put(skb, fifo_bytes), fifo_bytes);
791 trf7970a_send_err_upstream(trf, ret);
795 /* If received Type 2 ACK/NACK, shift right 4 bits and pass up */
796 if ((trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T) && (skb->len == 1) &&
797 (trf->special_fcn_reg1 ==
798 TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX)) {
800 status = TRF7970A_IRQ_STATUS_SRX;
802 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA_CONT;
804 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
806 trf7970a_send_err_upstream(trf, ret);
810 fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
812 /* If there are bytes in the FIFO, set status to '0' so
813 * the if stmt below doesn't fire and the driver will wait
814 * for the trf7970a to generate another RX interrupt.
821 if (status == TRF7970A_IRQ_STATUS_SRX) { /* Receive complete */
822 trf7970a_send_upstream(trf);
826 dev_dbg(trf->dev, "Setting timeout for %d ms\n",
827 TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT);
829 schedule_delayed_work(&trf->timeout_work,
830 msecs_to_jiffies(TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT));
833 static irqreturn_t trf7970a_irq(int irq, void *dev_id)
835 struct trf7970a *trf = dev_id;
837 u8 status, fifo_bytes, iso_ctrl;
839 mutex_lock(&trf->lock);
841 if (trf->state == TRF7970A_ST_RF_OFF) {
842 mutex_unlock(&trf->lock);
846 ret = trf7970a_read_irqstatus(trf, &status);
848 mutex_unlock(&trf->lock);
852 dev_dbg(trf->dev, "IRQ - state: %d, status: 0x%x\n", trf->state,
856 mutex_unlock(&trf->lock);
860 switch (trf->state) {
861 case TRF7970A_ST_IDLE:
862 case TRF7970A_ST_IDLE_RX_BLOCKED:
863 /* If initiator and getting interrupts caused by RF noise,
864 * turn off the receiver to avoid unnecessary interrupts.
865 * It will be turned back on in trf7970a_send_cmd() when
866 * the next command is issued.
868 if (trf->is_initiator && (status & TRF7970A_IRQ_STATUS_ERROR)) {
869 trf7970a_cmd(trf, TRF7970A_CMD_BLOCK_RX);
870 trf->state = TRF7970A_ST_IDLE_RX_BLOCKED;
873 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
875 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
876 if (status & TRF7970A_IRQ_STATUS_TX) {
877 trf->ignore_timeout =
878 !cancel_delayed_work(&trf->timeout_work);
879 trf7970a_fill_fifo(trf);
881 trf7970a_send_err_upstream(trf, -EIO);
884 case TRF7970A_ST_WAIT_FOR_RX_DATA:
885 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
886 if (status & TRF7970A_IRQ_STATUS_SRX) {
887 trf->ignore_timeout =
888 !cancel_delayed_work(&trf->timeout_work);
889 trf7970a_drain_fifo(trf, status);
890 } else if (status & TRF7970A_IRQ_STATUS_FIFO) {
891 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS,
894 fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
897 trf7970a_send_err_upstream(trf, ret);
898 else if (!fifo_bytes)
899 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
900 } else if ((status == TRF7970A_IRQ_STATUS_TX) ||
901 (!trf->is_initiator &&
902 (status == (TRF7970A_IRQ_STATUS_TX |
903 TRF7970A_IRQ_STATUS_NFC_RF)))) {
904 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
907 trf->ignore_timeout = !cancel_delayed_work(
909 trf->rx_skb = ERR_PTR(0);
910 trf7970a_send_upstream(trf);
914 if (trf->is_initiator)
917 iso_ctrl = trf->iso_ctrl;
919 switch (trf->framing) {
920 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
921 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
922 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
923 trf->iso_ctrl = 0xff; /* Force ISO_CTRL write */
925 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
926 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
927 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
928 trf->iso_ctrl = 0xff; /* Force ISO_CTRL write */
930 case NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE:
931 ret = trf7970a_write(trf,
932 TRF7970A_SPECIAL_FCN_REG1,
933 TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL);
935 goto err_unlock_exit;
937 trf->special_fcn_reg1 =
938 TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL;
944 if (iso_ctrl != trf->iso_ctrl) {
945 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL,
948 goto err_unlock_exit;
950 trf->iso_ctrl = iso_ctrl;
953 trf7970a_send_err_upstream(trf, -EIO);
956 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
957 if (status != TRF7970A_IRQ_STATUS_TX)
958 trf7970a_send_err_upstream(trf, -EIO);
960 case TRF7970A_ST_LISTENING:
961 if (status & TRF7970A_IRQ_STATUS_SRX) {
962 trf->ignore_timeout =
963 !cancel_delayed_work(&trf->timeout_work);
964 trf7970a_drain_fifo(trf, status);
965 } else if (!(status & TRF7970A_IRQ_STATUS_NFC_RF)) {
966 trf7970a_send_err_upstream(trf, -EIO);
969 case TRF7970A_ST_LISTENING_MD:
970 if (status & TRF7970A_IRQ_STATUS_SRX) {
971 trf->ignore_timeout =
972 !cancel_delayed_work(&trf->timeout_work);
974 ret = trf7970a_mode_detect(trf, &trf->md_rf_tech);
976 trf7970a_send_err_upstream(trf, ret);
978 trf->state = TRF7970A_ST_LISTENING;
979 trf7970a_drain_fifo(trf, status);
981 } else if (!(status & TRF7970A_IRQ_STATUS_NFC_RF)) {
982 trf7970a_send_err_upstream(trf, -EIO);
986 dev_err(trf->dev, "%s - Driver in invalid state: %d\n",
987 __func__, trf->state);
991 mutex_unlock(&trf->lock);
995 static void trf7970a_issue_eof(struct trf7970a *trf)
999 dev_dbg(trf->dev, "Issuing EOF\n");
1001 ret = trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
1003 trf7970a_send_err_upstream(trf, ret);
1005 ret = trf7970a_cmd(trf, TRF7970A_CMD_EOF);
1007 trf7970a_send_err_upstream(trf, ret);
1009 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA;
1011 dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n",
1012 trf->timeout, trf->state);
1014 schedule_delayed_work(&trf->timeout_work,
1015 msecs_to_jiffies(trf->timeout));
1018 static void trf7970a_timeout_work_handler(struct work_struct *work)
1020 struct trf7970a *trf = container_of(work, struct trf7970a,
1023 dev_dbg(trf->dev, "Timeout - state: %d, ignore_timeout: %d\n",
1024 trf->state, trf->ignore_timeout);
1026 mutex_lock(&trf->lock);
1028 if (trf->ignore_timeout)
1029 trf->ignore_timeout = false;
1030 else if (trf->state == TRF7970A_ST_WAIT_FOR_RX_DATA_CONT)
1031 trf7970a_drain_fifo(trf, TRF7970A_IRQ_STATUS_SRX);
1032 else if (trf->state == TRF7970A_ST_WAIT_TO_ISSUE_EOF)
1033 trf7970a_issue_eof(trf);
1035 trf7970a_send_err_upstream(trf, -ETIMEDOUT);
1037 mutex_unlock(&trf->lock);
1040 static int trf7970a_init(struct trf7970a *trf)
1044 dev_dbg(trf->dev, "Initializing device - state: %d\n", trf->state);
1046 ret = trf7970a_cmd(trf, TRF7970A_CMD_SOFT_INIT);
1050 ret = trf7970a_cmd(trf, TRF7970A_CMD_IDLE);
1054 ret = trf7970a_write(trf, TRF7970A_REG_IO_CTRL,
1055 trf->io_ctrl | TRF7970A_REG_IO_CTRL_VRS(0x1));
1059 ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
1063 usleep_range(1000, 2000);
1065 trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
1067 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
1068 trf->modulator_sys_clk_ctrl);
1072 ret = trf7970a_write(trf, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS,
1073 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 |
1074 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32);
1078 ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, 0);
1082 trf->special_fcn_reg1 = 0;
1084 trf->iso_ctrl = 0xff;
1088 dev_dbg(trf->dev, "Couldn't init device: %d\n", ret);
1092 static void trf7970a_switch_rf_off(struct trf7970a *trf)
1094 if ((trf->state == TRF7970A_ST_PWR_OFF) ||
1095 (trf->state == TRF7970A_ST_RF_OFF))
1098 dev_dbg(trf->dev, "Switching rf off\n");
1100 trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
1102 trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, trf->chip_status_ctrl);
1104 trf->aborting = false;
1105 trf->state = TRF7970A_ST_RF_OFF;
1107 pm_runtime_mark_last_busy(trf->dev);
1108 pm_runtime_put_autosuspend(trf->dev);
1111 static int trf7970a_switch_rf_on(struct trf7970a *trf)
1115 dev_dbg(trf->dev, "Switching rf on\n");
1117 pm_runtime_get_sync(trf->dev);
1119 if (trf->state != TRF7970A_ST_RF_OFF) { /* Power on, RF off */
1120 dev_err(trf->dev, "%s - Incorrect state: %d\n", __func__,
1125 ret = trf7970a_init(trf);
1127 dev_err(trf->dev, "%s - Can't initialize: %d\n", __func__, ret);
1131 trf->state = TRF7970A_ST_IDLE;
1136 static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
1138 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1141 dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on);
1143 mutex_lock(&trf->lock);
1146 switch (trf->state) {
1147 case TRF7970A_ST_PWR_OFF:
1148 case TRF7970A_ST_RF_OFF:
1149 ret = trf7970a_switch_rf_on(trf);
1151 case TRF7970A_ST_IDLE:
1152 case TRF7970A_ST_IDLE_RX_BLOCKED:
1155 dev_err(trf->dev, "%s - Invalid request: %d %d\n",
1156 __func__, trf->state, on);
1157 trf7970a_switch_rf_off(trf);
1161 switch (trf->state) {
1162 case TRF7970A_ST_PWR_OFF:
1163 case TRF7970A_ST_RF_OFF:
1166 dev_err(trf->dev, "%s - Invalid request: %d %d\n",
1167 __func__, trf->state, on);
1170 case TRF7970A_ST_IDLE:
1171 case TRF7970A_ST_IDLE_RX_BLOCKED:
1172 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1173 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1174 trf7970a_switch_rf_off(trf);
1178 mutex_unlock(&trf->lock);
1182 static int trf7970a_in_config_rf_tech(struct trf7970a *trf, int tech)
1186 dev_dbg(trf->dev, "rf technology: %d\n", tech);
1189 case NFC_DIGITAL_RF_TECH_106A:
1190 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443A_106;
1191 trf->modulator_sys_clk_ctrl =
1192 (trf->modulator_sys_clk_ctrl & 0xf8) |
1193 TRF7970A_MODULATOR_DEPTH_OOK;
1194 trf->guard_time = TRF7970A_GUARD_TIME_NFCA;
1196 case NFC_DIGITAL_RF_TECH_106B:
1197 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443B_106;
1198 trf->modulator_sys_clk_ctrl =
1199 (trf->modulator_sys_clk_ctrl & 0xf8) |
1200 TRF7970A_MODULATOR_DEPTH_ASK10;
1201 trf->guard_time = TRF7970A_GUARD_TIME_NFCB;
1203 case NFC_DIGITAL_RF_TECH_212F:
1204 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_212;
1205 trf->modulator_sys_clk_ctrl =
1206 (trf->modulator_sys_clk_ctrl & 0xf8) |
1207 TRF7970A_MODULATOR_DEPTH_ASK10;
1208 trf->guard_time = TRF7970A_GUARD_TIME_NFCF;
1210 case NFC_DIGITAL_RF_TECH_424F:
1211 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_424;
1212 trf->modulator_sys_clk_ctrl =
1213 (trf->modulator_sys_clk_ctrl & 0xf8) |
1214 TRF7970A_MODULATOR_DEPTH_ASK10;
1215 trf->guard_time = TRF7970A_GUARD_TIME_NFCF;
1217 case NFC_DIGITAL_RF_TECH_ISO15693:
1218 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
1219 trf->modulator_sys_clk_ctrl =
1220 (trf->modulator_sys_clk_ctrl & 0xf8) |
1221 TRF7970A_MODULATOR_DEPTH_OOK;
1222 trf->guard_time = TRF7970A_GUARD_TIME_15693;
1225 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
1229 trf->technology = tech;
1231 /* If in initiator mode and not changing the RF tech due to a
1232 * PSL sequence (indicated by 'trf->iso_ctrl == 0xff' from
1233 * trf7970a_init()), clear the NFC Target Detection Level register
1236 if (trf->iso_ctrl == 0xff)
1237 ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
1242 static int trf7970a_is_rf_field(struct trf7970a *trf, bool *is_rf_field)
1247 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1248 trf->chip_status_ctrl | TRF7970A_CHIP_STATUS_REC_ON);
1252 ret = trf7970a_cmd(trf, TRF7970A_CMD_TEST_EXT_RF);
1256 usleep_range(50, 60);
1258 ret = trf7970a_read(trf, TRF7970A_RSSI_OSC_STATUS, &rssi);
1262 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1263 trf->chip_status_ctrl);
1267 if (rssi & TRF7970A_RSSI_OSC_STATUS_RSSI_MASK)
1268 *is_rf_field = true;
1270 *is_rf_field = false;
1275 static int trf7970a_in_config_framing(struct trf7970a *trf, int framing)
1277 u8 iso_ctrl = trf->iso_ctrl_tech;
1278 bool is_rf_field = false;
1281 dev_dbg(trf->dev, "framing: %d\n", framing);
1284 case NFC_DIGITAL_FRAMING_NFCA_SHORT:
1285 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
1286 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
1287 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
1289 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
1290 case NFC_DIGITAL_FRAMING_NFCA_T4T:
1291 case NFC_DIGITAL_FRAMING_NFCB:
1292 case NFC_DIGITAL_FRAMING_NFCB_T4T:
1293 case NFC_DIGITAL_FRAMING_NFCF:
1294 case NFC_DIGITAL_FRAMING_NFCF_T3T:
1295 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY:
1296 case NFC_DIGITAL_FRAMING_ISO15693_T5T:
1297 case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
1298 case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP:
1299 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1300 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
1302 case NFC_DIGITAL_FRAMING_NFCA_T2T:
1303 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1304 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
1307 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
1311 trf->framing = framing;
1313 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
1314 ret = trf7970a_is_rf_field(trf, &is_rf_field);
1322 if (iso_ctrl != trf->iso_ctrl) {
1323 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1327 trf->iso_ctrl = iso_ctrl;
1329 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
1330 trf->modulator_sys_clk_ctrl);
1335 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
1336 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1337 trf->chip_status_ctrl |
1338 TRF7970A_CHIP_STATUS_RF_ON);
1342 trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
1344 usleep_range(trf->guard_time, trf->guard_time + 1000);
1350 static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type,
1353 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1356 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
1358 mutex_lock(&trf->lock);
1360 trf->is_initiator = true;
1362 if ((trf->state == TRF7970A_ST_PWR_OFF) ||
1363 (trf->state == TRF7970A_ST_RF_OFF)) {
1364 ret = trf7970a_switch_rf_on(trf);
1370 case NFC_DIGITAL_CONFIG_RF_TECH:
1371 ret = trf7970a_in_config_rf_tech(trf, param);
1373 case NFC_DIGITAL_CONFIG_FRAMING:
1374 ret = trf7970a_in_config_framing(trf, param);
1377 dev_dbg(trf->dev, "Unknown type: %d\n", type);
1382 mutex_unlock(&trf->lock);
1386 static int trf7970a_is_iso15693_write_or_lock(u8 cmd)
1389 case ISO15693_CMD_WRITE_SINGLE_BLOCK:
1390 case ISO15693_CMD_LOCK_BLOCK:
1391 case ISO15693_CMD_WRITE_MULTIPLE_BLOCK:
1392 case ISO15693_CMD_WRITE_AFI:
1393 case ISO15693_CMD_LOCK_AFI:
1394 case ISO15693_CMD_WRITE_DSFID:
1395 case ISO15693_CMD_LOCK_DSFID:
1403 static int trf7970a_per_cmd_config(struct trf7970a *trf, struct sk_buff *skb)
1405 u8 *req = skb->data;
1406 u8 special_fcn_reg1, iso_ctrl;
1409 trf->issue_eof = false;
1411 /* When issuing Type 2 read command, make sure the '4_bit_RX' bit in
1412 * special functions register 1 is cleared; otherwise, its a write or
1413 * sector select command and '4_bit_RX' must be set.
1415 * When issuing an ISO 15693 command, inspect the flags byte to see
1416 * what speed to use. Also, remember if the OPTION flag is set on
1417 * a Type 5 write or lock command so the driver will know that it
1418 * has to send an EOF in order to get a response.
1420 if ((trf->technology == NFC_DIGITAL_RF_TECH_106A) &&
1421 (trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T)) {
1422 if (req[0] == NFC_T2T_CMD_READ)
1423 special_fcn_reg1 = 0;
1425 special_fcn_reg1 = TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX;
1427 if (special_fcn_reg1 != trf->special_fcn_reg1) {
1428 ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1,
1433 trf->special_fcn_reg1 = special_fcn_reg1;
1435 } else if (trf->technology == NFC_DIGITAL_RF_TECH_ISO15693) {
1436 iso_ctrl = trf->iso_ctrl & ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK;
1438 switch (req[0] & ISO15693_REQ_FLAG_SPEED_MASK) {
1440 iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_662;
1442 case ISO15693_REQ_FLAG_SUB_CARRIER:
1443 iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a;
1445 case ISO15693_REQ_FLAG_DATA_RATE:
1446 iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
1448 case (ISO15693_REQ_FLAG_SUB_CARRIER |
1449 ISO15693_REQ_FLAG_DATA_RATE):
1450 iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669;
1454 if (iso_ctrl != trf->iso_ctrl) {
1455 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1459 trf->iso_ctrl = iso_ctrl;
1462 if (trf->framing == NFC_DIGITAL_FRAMING_ISO15693_T5T) {
1463 if (trf7970a_is_iso15693_write_or_lock(req[1]) &&
1464 (req[0] & ISO15693_REQ_FLAG_OPTION))
1465 trf->issue_eof = true;
1466 else if ((trf->quirks &
1467 TRF7970A_QUIRK_T5T_RMB_EXTRA_BYTE) &&
1468 (req[1] == ISO15693_CMD_READ_MULTIPLE_BLOCK))
1469 trf->adjust_resp_len = true;
1476 static int trf7970a_send_cmd(struct nfc_digital_dev *ddev,
1477 struct sk_buff *skb, u16 timeout,
1478 nfc_digital_cmd_complete_t cb, void *arg)
1480 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1486 dev_dbg(trf->dev, "New request - state: %d, timeout: %d ms, len: %d\n",
1487 trf->state, timeout, skb->len);
1489 if (skb->len > TRF7970A_TX_MAX)
1492 mutex_lock(&trf->lock);
1494 if ((trf->state != TRF7970A_ST_IDLE) &&
1495 (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) {
1496 dev_err(trf->dev, "%s - Bogus state: %d\n", __func__,
1502 if (trf->aborting) {
1503 dev_dbg(trf->dev, "Abort process complete\n");
1504 trf->aborting = false;
1510 trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE,
1513 dev_dbg(trf->dev, "Can't alloc rx_skb\n");
1519 if (trf->state == TRF7970A_ST_IDLE_RX_BLOCKED) {
1520 ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX);
1524 trf->state = TRF7970A_ST_IDLE;
1527 if (trf->is_initiator) {
1528 ret = trf7970a_per_cmd_config(trf, skb);
1537 trf->timeout = timeout;
1538 trf->ignore_timeout = false;
1542 /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
1543 * on what the current framing is, the address of the TX length byte 1
1544 * register (0x1d), and the 2 byte length of the data to be transmitted.
1545 * That totals 5 bytes.
1547 prefix[0] = TRF7970A_CMD_BIT_CTRL |
1548 TRF7970A_CMD_BIT_OPCODE(TRF7970A_CMD_FIFO_RESET);
1549 prefix[1] = TRF7970A_CMD_BIT_CTRL |
1550 TRF7970A_CMD_BIT_OPCODE(trf->tx_cmd);
1551 prefix[2] = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_TX_LENGTH_BYTE1;
1553 if (trf->framing == NFC_DIGITAL_FRAMING_NFCA_SHORT) {
1555 prefix[4] = 0x0f; /* 7 bits */
1557 prefix[3] = (len & 0xf00) >> 4;
1558 prefix[3] |= ((len & 0xf0) >> 4);
1559 prefix[4] = ((len & 0x0f) << 4);
1562 len = min_t(int, skb->len, TRF7970A_FIFO_SIZE);
1564 /* Clear possible spurious interrupt */
1565 ret = trf7970a_read_irqstatus(trf, &status);
1569 ret = trf7970a_transmit(trf, skb, len, prefix, sizeof(prefix));
1571 kfree_skb(trf->rx_skb);
1576 mutex_unlock(&trf->lock);
1580 static int trf7970a_tg_config_rf_tech(struct trf7970a *trf, int tech)
1584 dev_dbg(trf->dev, "rf technology: %d\n", tech);
1587 case NFC_DIGITAL_RF_TECH_106A:
1588 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
1589 TRF7970A_ISO_CTRL_NFC_CE |
1590 TRF7970A_ISO_CTRL_NFC_CE_14443A;
1591 trf->modulator_sys_clk_ctrl =
1592 (trf->modulator_sys_clk_ctrl & 0xf8) |
1593 TRF7970A_MODULATOR_DEPTH_OOK;
1595 case NFC_DIGITAL_RF_TECH_212F:
1596 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
1597 TRF7970A_ISO_CTRL_NFC_NFCF_212;
1598 trf->modulator_sys_clk_ctrl =
1599 (trf->modulator_sys_clk_ctrl & 0xf8) |
1600 TRF7970A_MODULATOR_DEPTH_ASK10;
1602 case NFC_DIGITAL_RF_TECH_424F:
1603 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
1604 TRF7970A_ISO_CTRL_NFC_NFCF_424;
1605 trf->modulator_sys_clk_ctrl =
1606 (trf->modulator_sys_clk_ctrl & 0xf8) |
1607 TRF7970A_MODULATOR_DEPTH_ASK10;
1610 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
1614 trf->technology = tech;
1616 /* Normally we write the ISO_CTRL register in
1617 * trf7970a_tg_config_framing() because the framing can change
1618 * the value written. However, when sending a PSL RES,
1619 * digital_tg_send_psl_res_complete() doesn't call
1620 * trf7970a_tg_config_framing() so we must write the register
1623 if ((trf->framing == NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED) &&
1624 (trf->iso_ctrl_tech != trf->iso_ctrl)) {
1625 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL,
1626 trf->iso_ctrl_tech);
1628 trf->iso_ctrl = trf->iso_ctrl_tech;
1634 /* Since this is a target routine, several of the framing calls are
1635 * made between receiving the request and sending the response so they
1636 * should take effect until after the response is sent. This is accomplished
1637 * by skipping the ISO_CTRL register write here and doing it in the interrupt
1640 static int trf7970a_tg_config_framing(struct trf7970a *trf, int framing)
1642 u8 iso_ctrl = trf->iso_ctrl_tech;
1645 dev_dbg(trf->dev, "framing: %d\n", framing);
1648 case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
1649 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
1650 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
1652 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
1653 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
1654 case NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE:
1655 /* These ones are applied in the interrupt handler */
1656 iso_ctrl = trf->iso_ctrl; /* Don't write to ISO_CTRL yet */
1658 case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP:
1659 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1660 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
1662 case NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED:
1663 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1664 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
1667 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
1671 trf->framing = framing;
1673 if (iso_ctrl != trf->iso_ctrl) {
1674 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1678 trf->iso_ctrl = iso_ctrl;
1680 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
1681 trf->modulator_sys_clk_ctrl);
1686 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
1687 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1688 trf->chip_status_ctrl |
1689 TRF7970A_CHIP_STATUS_RF_ON);
1693 trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
1699 static int trf7970a_tg_configure_hw(struct nfc_digital_dev *ddev, int type,
1702 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1705 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
1707 mutex_lock(&trf->lock);
1709 trf->is_initiator = false;
1711 if ((trf->state == TRF7970A_ST_PWR_OFF) ||
1712 (trf->state == TRF7970A_ST_RF_OFF)) {
1713 ret = trf7970a_switch_rf_on(trf);
1719 case NFC_DIGITAL_CONFIG_RF_TECH:
1720 ret = trf7970a_tg_config_rf_tech(trf, param);
1722 case NFC_DIGITAL_CONFIG_FRAMING:
1723 ret = trf7970a_tg_config_framing(trf, param);
1726 dev_dbg(trf->dev, "Unknown type: %d\n", type);
1731 mutex_unlock(&trf->lock);
1735 static int _trf7970a_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
1736 nfc_digital_cmd_complete_t cb, void *arg, bool mode_detect)
1738 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1741 mutex_lock(&trf->lock);
1743 if ((trf->state != TRF7970A_ST_IDLE) &&
1744 (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) {
1745 dev_err(trf->dev, "%s - Bogus state: %d\n", __func__,
1751 if (trf->aborting) {
1752 dev_dbg(trf->dev, "Abort process complete\n");
1753 trf->aborting = false;
1758 trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE,
1761 dev_dbg(trf->dev, "Can't alloc rx_skb\n");
1766 ret = trf7970a_write(trf, TRF7970A_RX_SPECIAL_SETTINGS,
1767 TRF7970A_RX_SPECIAL_SETTINGS_HBT |
1768 TRF7970A_RX_SPECIAL_SETTINGS_M848 |
1769 TRF7970A_RX_SPECIAL_SETTINGS_C424 |
1770 TRF7970A_RX_SPECIAL_SETTINGS_C212);
1774 ret = trf7970a_write(trf, TRF7970A_REG_IO_CTRL,
1775 trf->io_ctrl | TRF7970A_REG_IO_CTRL_VRS(0x1));
1779 ret = trf7970a_write(trf, TRF7970A_NFC_LOW_FIELD_LEVEL,
1780 TRF7970A_NFC_LOW_FIELD_LEVEL_RFDET(0x3));
1784 ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL,
1785 TRF7970A_NFC_TARGET_LEVEL_RFDET(0x7));
1792 trf->timeout = timeout;
1793 trf->ignore_timeout = false;
1795 ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX);
1799 trf->state = mode_detect ? TRF7970A_ST_LISTENING_MD :
1800 TRF7970A_ST_LISTENING;
1802 schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout));
1805 mutex_unlock(&trf->lock);
1809 static int trf7970a_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
1810 nfc_digital_cmd_complete_t cb, void *arg)
1812 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1814 dev_dbg(trf->dev, "Listen - state: %d, timeout: %d ms\n",
1815 trf->state, timeout);
1817 return _trf7970a_tg_listen(ddev, timeout, cb, arg, false);
1820 static int trf7970a_tg_listen_md(struct nfc_digital_dev *ddev,
1821 u16 timeout, nfc_digital_cmd_complete_t cb, void *arg)
1823 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1826 dev_dbg(trf->dev, "Listen MD - state: %d, timeout: %d ms\n",
1827 trf->state, timeout);
1829 ret = trf7970a_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
1830 NFC_DIGITAL_RF_TECH_106A);
1834 ret = trf7970a_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1835 NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
1839 return _trf7970a_tg_listen(ddev, timeout, cb, arg, true);
1842 static int trf7970a_tg_get_rf_tech(struct nfc_digital_dev *ddev, u8 *rf_tech)
1844 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1846 dev_dbg(trf->dev, "Get RF Tech - state: %d, rf_tech: %d\n",
1847 trf->state, trf->md_rf_tech);
1849 *rf_tech = trf->md_rf_tech;
1854 static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev)
1856 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1858 dev_dbg(trf->dev, "Abort process initiated\n");
1860 mutex_lock(&trf->lock);
1862 switch (trf->state) {
1863 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
1864 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1865 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1866 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
1867 trf->aborting = true;
1869 case TRF7970A_ST_LISTENING:
1870 trf->ignore_timeout = !cancel_delayed_work(&trf->timeout_work);
1871 trf7970a_send_err_upstream(trf, -ECANCELED);
1872 dev_dbg(trf->dev, "Abort process complete\n");
1878 mutex_unlock(&trf->lock);
1881 static struct nfc_digital_ops trf7970a_nfc_ops = {
1882 .in_configure_hw = trf7970a_in_configure_hw,
1883 .in_send_cmd = trf7970a_send_cmd,
1884 .tg_configure_hw = trf7970a_tg_configure_hw,
1885 .tg_send_cmd = trf7970a_send_cmd,
1886 .tg_listen = trf7970a_tg_listen,
1887 .tg_listen_md = trf7970a_tg_listen_md,
1888 .tg_get_rf_tech = trf7970a_tg_get_rf_tech,
1889 .switch_rf = trf7970a_switch_rf,
1890 .abort_cmd = trf7970a_abort_cmd,
1893 static int trf7970a_power_up(struct trf7970a *trf)
1897 dev_dbg(trf->dev, "Powering up - state: %d\n", trf->state);
1899 if (trf->state != TRF7970A_ST_PWR_OFF)
1902 ret = regulator_enable(trf->regulator);
1904 dev_err(trf->dev, "%s - Can't enable VIN: %d\n", __func__, ret);
1908 usleep_range(5000, 6000);
1910 if (trf->en2_gpiod &&
1911 !(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW)) {
1912 gpiod_set_value_cansleep(trf->en2_gpiod, 1);
1913 usleep_range(1000, 2000);
1916 gpiod_set_value_cansleep(trf->en_gpiod, 1);
1918 usleep_range(20000, 21000);
1920 trf->state = TRF7970A_ST_RF_OFF;
1925 static int trf7970a_power_down(struct trf7970a *trf)
1929 dev_dbg(trf->dev, "Powering down - state: %d\n", trf->state);
1931 if (trf->state == TRF7970A_ST_PWR_OFF)
1934 if (trf->state != TRF7970A_ST_RF_OFF) {
1935 dev_dbg(trf->dev, "Can't power down - not RF_OFF state (%d)\n",
1940 gpiod_set_value_cansleep(trf->en_gpiod, 0);
1942 if (trf->en2_gpiod &&
1943 !(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW))
1944 gpiod_set_value_cansleep(trf->en2_gpiod, 0);
1946 ret = regulator_disable(trf->regulator);
1948 dev_err(trf->dev, "%s - Can't disable VIN: %d\n", __func__,
1951 trf->state = TRF7970A_ST_PWR_OFF;
1956 static int trf7970a_startup(struct trf7970a *trf)
1960 ret = trf7970a_power_up(trf);
1964 pm_runtime_set_active(trf->dev);
1965 pm_runtime_enable(trf->dev);
1966 pm_runtime_mark_last_busy(trf->dev);
1971 static void trf7970a_shutdown(struct trf7970a *trf)
1973 switch (trf->state) {
1974 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
1975 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1976 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1977 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
1978 case TRF7970A_ST_LISTENING:
1979 trf7970a_send_err_upstream(trf, -ECANCELED);
1981 case TRF7970A_ST_IDLE:
1982 case TRF7970A_ST_IDLE_RX_BLOCKED:
1983 trf7970a_switch_rf_off(trf);
1989 pm_runtime_disable(trf->dev);
1990 pm_runtime_set_suspended(trf->dev);
1992 trf7970a_power_down(trf);
1995 static int trf7970a_get_autosuspend_delay(struct device_node *np)
1997 int autosuspend_delay, ret;
1999 ret = of_property_read_u32(np, "autosuspend-delay", &autosuspend_delay);
2001 autosuspend_delay = TRF7970A_AUTOSUSPEND_DELAY;
2003 return autosuspend_delay;
2006 static int trf7970a_probe(struct spi_device *spi)
2008 struct device_node *np = spi->dev.of_node;
2009 struct trf7970a *trf;
2010 int uvolts, autosuspend_delay, ret;
2011 u32 clk_freq = TRF7970A_13MHZ_CLOCK_FREQUENCY;
2014 dev_err(&spi->dev, "No Device Tree entry\n");
2018 trf = devm_kzalloc(&spi->dev, sizeof(*trf), GFP_KERNEL);
2022 trf->state = TRF7970A_ST_PWR_OFF;
2023 trf->dev = &spi->dev;
2026 spi->mode = SPI_MODE_1;
2027 spi->bits_per_word = 8;
2029 ret = spi_setup(spi);
2031 dev_err(trf->dev, "Can't set up SPI Communication\n");
2035 if (of_property_read_bool(np, "t5t-rmb-extra-byte-quirk"))
2036 trf->quirks |= TRF7970A_QUIRK_T5T_RMB_EXTRA_BYTE;
2038 if (of_property_read_bool(np, "irq-status-read-quirk"))
2039 trf->quirks |= TRF7970A_QUIRK_IRQ_STATUS_READ;
2041 /* There are two enable pins - only EN must be present in the DT */
2042 trf->en_gpiod = devm_gpiod_get_index(trf->dev, "ti,enable", 0,
2044 if (IS_ERR(trf->en_gpiod)) {
2045 dev_err(trf->dev, "No EN GPIO property\n");
2046 return PTR_ERR(trf->en_gpiod);
2049 trf->en2_gpiod = devm_gpiod_get_index_optional(trf->dev, "ti,enable", 1,
2051 if (!trf->en2_gpiod) {
2052 dev_info(trf->dev, "No EN2 GPIO property\n");
2053 } else if (IS_ERR(trf->en2_gpiod)) {
2054 dev_err(trf->dev, "Error getting EN2 GPIO property: %ld\n",
2055 PTR_ERR(trf->en2_gpiod));
2056 return PTR_ERR(trf->en2_gpiod);
2057 } else if (of_property_read_bool(np, "en2-rf-quirk")) {
2058 trf->quirks |= TRF7970A_QUIRK_EN2_MUST_STAY_LOW;
2061 of_property_read_u32(np, "clock-frequency", &clk_freq);
2062 if ((clk_freq != TRF7970A_27MHZ_CLOCK_FREQUENCY) ||
2063 (clk_freq != TRF7970A_13MHZ_CLOCK_FREQUENCY)) {
2065 "clock-frequency (%u Hz) unsupported\n",
2070 ret = devm_request_threaded_irq(trf->dev, spi->irq, NULL,
2071 trf7970a_irq, IRQF_TRIGGER_RISING | IRQF_ONESHOT,
2074 dev_err(trf->dev, "Can't request IRQ#%d: %d\n", spi->irq, ret);
2078 mutex_init(&trf->lock);
2079 INIT_DELAYED_WORK(&trf->timeout_work, trf7970a_timeout_work_handler);
2081 trf->regulator = devm_regulator_get(&spi->dev, "vin");
2082 if (IS_ERR(trf->regulator)) {
2083 ret = PTR_ERR(trf->regulator);
2084 dev_err(trf->dev, "Can't get VIN regulator: %d\n", ret);
2085 goto err_destroy_lock;
2088 ret = regulator_enable(trf->regulator);
2090 dev_err(trf->dev, "Can't enable VIN: %d\n", ret);
2091 goto err_destroy_lock;
2094 uvolts = regulator_get_voltage(trf->regulator);
2095 if (uvolts > 4000000)
2096 trf->chip_status_ctrl = TRF7970A_CHIP_STATUS_VRS5_3;
2098 trf->regulator = devm_regulator_get(&spi->dev, "vdd-io");
2099 if (IS_ERR(trf->regulator)) {
2100 ret = PTR_ERR(trf->regulator);
2101 dev_err(trf->dev, "Can't get VDD_IO regulator: %d\n", ret);
2102 goto err_destroy_lock;
2105 ret = regulator_enable(trf->regulator);
2107 dev_err(trf->dev, "Can't enable VDD_IO: %d\n", ret);
2108 goto err_destroy_lock;
2111 if (regulator_get_voltage(trf->regulator) == 1800000) {
2112 trf->io_ctrl = TRF7970A_REG_IO_CTRL_IO_LOW;
2113 dev_dbg(trf->dev, "trf7970a config vdd_io to 1.8V\n");
2116 trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops,
2117 TRF7970A_SUPPORTED_PROTOCOLS,
2118 NFC_DIGITAL_DRV_CAPS_IN_CRC |
2119 NFC_DIGITAL_DRV_CAPS_TG_CRC, 0, 0);
2121 dev_err(trf->dev, "Can't allocate NFC digital device\n");
2123 goto err_disable_regulator;
2126 nfc_digital_set_parent_dev(trf->ddev, trf->dev);
2127 nfc_digital_set_drvdata(trf->ddev, trf);
2128 spi_set_drvdata(spi, trf);
2130 autosuspend_delay = trf7970a_get_autosuspend_delay(np);
2132 pm_runtime_set_autosuspend_delay(trf->dev, autosuspend_delay);
2133 pm_runtime_use_autosuspend(trf->dev);
2135 ret = trf7970a_startup(trf);
2139 ret = nfc_digital_register_device(trf->ddev);
2141 dev_err(trf->dev, "Can't register NFC digital device: %d\n",
2149 trf7970a_shutdown(trf);
2151 nfc_digital_free_device(trf->ddev);
2152 err_disable_regulator:
2153 regulator_disable(trf->regulator);
2155 mutex_destroy(&trf->lock);
2159 static int trf7970a_remove(struct spi_device *spi)
2161 struct trf7970a *trf = spi_get_drvdata(spi);
2163 mutex_lock(&trf->lock);
2165 trf7970a_shutdown(trf);
2167 mutex_unlock(&trf->lock);
2169 nfc_digital_unregister_device(trf->ddev);
2170 nfc_digital_free_device(trf->ddev);
2172 regulator_disable(trf->regulator);
2174 mutex_destroy(&trf->lock);
2179 #ifdef CONFIG_PM_SLEEP
2180 static int trf7970a_suspend(struct device *dev)
2182 struct spi_device *spi = to_spi_device(dev);
2183 struct trf7970a *trf = spi_get_drvdata(spi);
2185 dev_dbg(dev, "Suspend\n");
2187 mutex_lock(&trf->lock);
2189 trf7970a_shutdown(trf);
2191 mutex_unlock(&trf->lock);
2196 static int trf7970a_resume(struct device *dev)
2198 struct spi_device *spi = to_spi_device(dev);
2199 struct trf7970a *trf = spi_get_drvdata(spi);
2202 dev_dbg(dev, "Resume\n");
2204 mutex_lock(&trf->lock);
2206 ret = trf7970a_startup(trf);
2208 mutex_unlock(&trf->lock);
2215 static int trf7970a_pm_runtime_suspend(struct device *dev)
2217 struct spi_device *spi = to_spi_device(dev);
2218 struct trf7970a *trf = spi_get_drvdata(spi);
2221 dev_dbg(dev, "Runtime suspend\n");
2223 mutex_lock(&trf->lock);
2225 ret = trf7970a_power_down(trf);
2227 mutex_unlock(&trf->lock);
2232 static int trf7970a_pm_runtime_resume(struct device *dev)
2234 struct spi_device *spi = to_spi_device(dev);
2235 struct trf7970a *trf = spi_get_drvdata(spi);
2238 dev_dbg(dev, "Runtime resume\n");
2240 ret = trf7970a_power_up(trf);
2242 pm_runtime_mark_last_busy(dev);
2248 static const struct dev_pm_ops trf7970a_pm_ops = {
2249 SET_SYSTEM_SLEEP_PM_OPS(trf7970a_suspend, trf7970a_resume)
2250 SET_RUNTIME_PM_OPS(trf7970a_pm_runtime_suspend,
2251 trf7970a_pm_runtime_resume, NULL)
2254 static const struct of_device_id trf7970a_of_match[] = {
2255 { .compatible = "ti,trf7970a", },
2258 MODULE_DEVICE_TABLE(of, trf7970a_of_match);
2260 static const struct spi_device_id trf7970a_id_table[] = {
2264 MODULE_DEVICE_TABLE(spi, trf7970a_id_table);
2266 static struct spi_driver trf7970a_spi_driver = {
2267 .probe = trf7970a_probe,
2268 .remove = trf7970a_remove,
2269 .id_table = trf7970a_id_table,
2272 .of_match_table = of_match_ptr(trf7970a_of_match),
2273 .pm = &trf7970a_pm_ops,
2277 module_spi_driver(trf7970a_spi_driver);
2279 MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>");
2280 MODULE_LICENSE("GPL v2");
2281 MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver");