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.h>
25 #include <linux/of_gpio.h>
26 #include <linux/spi/spi.h>
27 #include <linux/regulator/consumer.h>
29 #include <net/nfc/nfc.h>
30 #include <net/nfc/digital.h>
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.
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
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.
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
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_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
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
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
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
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.
86 * When transmitting a frame larger than the FIFO size (127 bytes), the
87 * driver will wait 20 ms for the FIFO to drain past the low-watermark
88 * and generate an interrupt. The low-watermark set to 32 bytes so the
89 * interrupt should fire after 127 - 32 = 95 bytes have been sent. At
90 * the lowest possible bit rate (6.62 kbps for 15693), it will take up
91 * to ~14.35 ms so 20 ms is used for the timeout.
93 * Type 2 write and sector select commands respond with a 4-bit ACK or NACK.
94 * Having only 4 bits in the FIFO won't normally generate an interrupt so
95 * driver enables the '4_bit_RX' bit of the Special Functions register 1
96 * to cause an interrupt in that case. Leaving that bit for a read command
97 * messes up the data returned so it is only enabled when the framing is
98 * 'NFC_DIGITAL_FRAMING_NFCA_T2T' and the command is not a read command.
99 * Unfortunately, that means that the driver has to peek into tx frames
100 * when the framing is 'NFC_DIGITAL_FRAMING_NFCA_T2T'. This is done by
101 * the trf7970a_per_cmd_config() routine.
103 * ISO/IEC 15693 frames specify whether to use single or double sub-carrier
104 * frequencies and whether to use low or high data rates in the flags byte
105 * of the frame. This means that the driver has to peek at all 15693 frames
106 * to determine what speed to set the communication to. In addition, write
107 * and lock commands use the OPTION flag to indicate that an EOF must be
108 * sent to the tag before it will send its response. So the driver has to
109 * examine all frames for that reason too.
111 * It is unclear how long to wait before sending the EOF. According to the
112 * Note under Table 1-1 in section 1.6 of
113 * http://www.ti.com/lit/ug/scbu011/scbu011.pdf, that wait should be at least
114 * 10 ms for TI Tag-it HF-I tags; however testing has shown that is not long
115 * enough so 20 ms is used. So the timer is set to 40 ms - 20 ms to drain
116 * up to 127 bytes in the FIFO at the lowest bit rate plus another 20 ms to
117 * ensure the wait is long enough before sending the EOF. This seems to work
121 #define TRF7970A_SUPPORTED_PROTOCOLS \
122 (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \
123 NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_FELICA_MASK | \
124 NFC_PROTO_ISO15693_MASK | NFC_PROTO_NFC_DEP_MASK)
126 #define TRF7970A_AUTOSUSPEND_DELAY 30000 /* 30 seconds */
128 #define TRF7970A_RX_SKB_ALLOC_SIZE 256
130 #define TRF7970A_FIFO_SIZE 127
132 /* TX length is 3 nibbles long ==> 4KB - 1 bytes max */
133 #define TRF7970A_TX_MAX (4096 - 1)
135 #define TRF7970A_WAIT_FOR_TX_IRQ 20
136 #define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 20
137 #define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 20
138 #define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 40
140 /* Guard times for various RF technologies (in us) */
141 #define TRF7970A_GUARD_TIME_NFCA 5000
142 #define TRF7970A_GUARD_TIME_NFCB 5000
143 #define TRF7970A_GUARD_TIME_NFCF 20000
144 #define TRF7970A_GUARD_TIME_15693 1000
147 /* Erratum: When reading IRQ Status register on trf7970a, we must issue a
148 * read continuous command for IRQ Status and Collision Position registers.
150 #define TRF7970A_QUIRK_IRQ_STATUS_READ BIT(0)
151 #define TRF7970A_QUIRK_EN2_MUST_STAY_LOW BIT(1)
153 /* Direct commands */
154 #define TRF7970A_CMD_IDLE 0x00
155 #define TRF7970A_CMD_SOFT_INIT 0x03
156 #define TRF7970A_CMD_RF_COLLISION 0x04
157 #define TRF7970A_CMD_RF_COLLISION_RESPONSE_N 0x05
158 #define TRF7970A_CMD_RF_COLLISION_RESPONSE_0 0x06
159 #define TRF7970A_CMD_FIFO_RESET 0x0f
160 #define TRF7970A_CMD_TRANSMIT_NO_CRC 0x10
161 #define TRF7970A_CMD_TRANSMIT 0x11
162 #define TRF7970A_CMD_DELAY_TRANSMIT_NO_CRC 0x12
163 #define TRF7970A_CMD_DELAY_TRANSMIT 0x13
164 #define TRF7970A_CMD_EOF 0x14
165 #define TRF7970A_CMD_CLOSE_SLOT 0x15
166 #define TRF7970A_CMD_BLOCK_RX 0x16
167 #define TRF7970A_CMD_ENABLE_RX 0x17
168 #define TRF7970A_CMD_TEST_INT_RF 0x18
169 #define TRF7970A_CMD_TEST_EXT_RF 0x19
170 #define TRF7970A_CMD_RX_GAIN_ADJUST 0x1a
172 /* Bits determining whether its a direct command or register R/W,
173 * whether to use a continuous SPI transaction or not, and the actual
174 * direct cmd opcode or regster address.
176 #define TRF7970A_CMD_BIT_CTRL BIT(7)
177 #define TRF7970A_CMD_BIT_RW BIT(6)
178 #define TRF7970A_CMD_BIT_CONTINUOUS BIT(5)
179 #define TRF7970A_CMD_BIT_OPCODE(opcode) ((opcode) & 0x1f)
181 /* Registers addresses */
182 #define TRF7970A_CHIP_STATUS_CTRL 0x00
183 #define TRF7970A_ISO_CTRL 0x01
184 #define TRF7970A_ISO14443B_TX_OPTIONS 0x02
185 #define TRF7970A_ISO14443A_HIGH_BITRATE_OPTIONS 0x03
186 #define TRF7970A_TX_TIMER_SETTING_H_BYTE 0x04
187 #define TRF7970A_TX_TIMER_SETTING_L_BYTE 0x05
188 #define TRF7970A_TX_PULSE_LENGTH_CTRL 0x06
189 #define TRF7970A_RX_NO_RESPONSE_WAIT 0x07
190 #define TRF7970A_RX_WAIT_TIME 0x08
191 #define TRF7970A_MODULATOR_SYS_CLK_CTRL 0x09
192 #define TRF7970A_RX_SPECIAL_SETTINGS 0x0a
193 #define TRF7970A_REG_IO_CTRL 0x0b
194 #define TRF7970A_IRQ_STATUS 0x0c
195 #define TRF7970A_COLLISION_IRQ_MASK 0x0d
196 #define TRF7970A_COLLISION_POSITION 0x0e
197 #define TRF7970A_RSSI_OSC_STATUS 0x0f
198 #define TRF7970A_SPECIAL_FCN_REG1 0x10
199 #define TRF7970A_SPECIAL_FCN_REG2 0x11
200 #define TRF7970A_RAM1 0x12
201 #define TRF7970A_RAM2 0x13
202 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS 0x14
203 #define TRF7970A_NFC_LOW_FIELD_LEVEL 0x16
204 #define TRF7970A_NFCID1 0x17
205 #define TRF7970A_NFC_TARGET_LEVEL 0x18
206 #define TRF79070A_NFC_TARGET_PROTOCOL 0x19
207 #define TRF7970A_TEST_REGISTER1 0x1a
208 #define TRF7970A_TEST_REGISTER2 0x1b
209 #define TRF7970A_FIFO_STATUS 0x1c
210 #define TRF7970A_TX_LENGTH_BYTE1 0x1d
211 #define TRF7970A_TX_LENGTH_BYTE2 0x1e
212 #define TRF7970A_FIFO_IO_REGISTER 0x1f
214 /* Chip Status Control Register Bits */
215 #define TRF7970A_CHIP_STATUS_VRS5_3 BIT(0)
216 #define TRF7970A_CHIP_STATUS_REC_ON BIT(1)
217 #define TRF7970A_CHIP_STATUS_AGC_ON BIT(2)
218 #define TRF7970A_CHIP_STATUS_PM_ON BIT(3)
219 #define TRF7970A_CHIP_STATUS_RF_PWR BIT(4)
220 #define TRF7970A_CHIP_STATUS_RF_ON BIT(5)
221 #define TRF7970A_CHIP_STATUS_DIRECT BIT(6)
222 #define TRF7970A_CHIP_STATUS_STBY BIT(7)
224 /* ISO Control Register Bits */
225 #define TRF7970A_ISO_CTRL_15693_SGL_1OF4_662 0x00
226 #define TRF7970A_ISO_CTRL_15693_SGL_1OF256_662 0x01
227 #define TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648 0x02
228 #define TRF7970A_ISO_CTRL_15693_SGL_1OF256_2648 0x03
229 #define TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a 0x04
230 #define TRF7970A_ISO_CTRL_15693_DBL_1OF256_667 0x05
231 #define TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669 0x06
232 #define TRF7970A_ISO_CTRL_15693_DBL_1OF256_2669 0x07
233 #define TRF7970A_ISO_CTRL_14443A_106 0x08
234 #define TRF7970A_ISO_CTRL_14443A_212 0x09
235 #define TRF7970A_ISO_CTRL_14443A_424 0x0a
236 #define TRF7970A_ISO_CTRL_14443A_848 0x0b
237 #define TRF7970A_ISO_CTRL_14443B_106 0x0c
238 #define TRF7970A_ISO_CTRL_14443B_212 0x0d
239 #define TRF7970A_ISO_CTRL_14443B_424 0x0e
240 #define TRF7970A_ISO_CTRL_14443B_848 0x0f
241 #define TRF7970A_ISO_CTRL_FELICA_212 0x1a
242 #define TRF7970A_ISO_CTRL_FELICA_424 0x1b
243 #define TRF7970A_ISO_CTRL_NFC_NFCA_106 0x01
244 #define TRF7970A_ISO_CTRL_NFC_NFCF_212 0x02
245 #define TRF7970A_ISO_CTRL_NFC_NFCF_424 0x03
246 #define TRF7970A_ISO_CTRL_NFC_CE_14443A 0x00
247 #define TRF7970A_ISO_CTRL_NFC_CE_14443B 0x01
248 #define TRF7970A_ISO_CTRL_NFC_CE BIT(2)
249 #define TRF7970A_ISO_CTRL_NFC_ACTIVE BIT(3)
250 #define TRF7970A_ISO_CTRL_NFC_INITIATOR BIT(4)
251 #define TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE BIT(5)
252 #define TRF7970A_ISO_CTRL_RFID BIT(5)
253 #define TRF7970A_ISO_CTRL_DIR_MODE BIT(6)
254 #define TRF7970A_ISO_CTRL_RX_CRC_N BIT(7) /* true == No CRC */
256 #define TRF7970A_ISO_CTRL_RFID_SPEED_MASK 0x1f
258 /* Modulator and SYS_CLK Control Register Bits */
259 #define TRF7970A_MODULATOR_DEPTH(n) ((n) & 0x7)
260 #define TRF7970A_MODULATOR_DEPTH_ASK10 (TRF7970A_MODULATOR_DEPTH(0))
261 #define TRF7970A_MODULATOR_DEPTH_OOK (TRF7970A_MODULATOR_DEPTH(1))
262 #define TRF7970A_MODULATOR_DEPTH_ASK7 (TRF7970A_MODULATOR_DEPTH(2))
263 #define TRF7970A_MODULATOR_DEPTH_ASK8_5 (TRF7970A_MODULATOR_DEPTH(3))
264 #define TRF7970A_MODULATOR_DEPTH_ASK13 (TRF7970A_MODULATOR_DEPTH(4))
265 #define TRF7970A_MODULATOR_DEPTH_ASK16 (TRF7970A_MODULATOR_DEPTH(5))
266 #define TRF7970A_MODULATOR_DEPTH_ASK22 (TRF7970A_MODULATOR_DEPTH(6))
267 #define TRF7970A_MODULATOR_DEPTH_ASK30 (TRF7970A_MODULATOR_DEPTH(7))
268 #define TRF7970A_MODULATOR_EN_ANA BIT(3)
269 #define TRF7970A_MODULATOR_CLK(n) (((n) & 0x3) << 4)
270 #define TRF7970A_MODULATOR_CLK_DISABLED (TRF7970A_MODULATOR_CLK(0))
271 #define TRF7970A_MODULATOR_CLK_3_6 (TRF7970A_MODULATOR_CLK(1))
272 #define TRF7970A_MODULATOR_CLK_6_13 (TRF7970A_MODULATOR_CLK(2))
273 #define TRF7970A_MODULATOR_CLK_13_27 (TRF7970A_MODULATOR_CLK(3))
274 #define TRF7970A_MODULATOR_EN_OOK BIT(6)
275 #define TRF7970A_MODULATOR_27MHZ BIT(7)
277 #define TRF7970A_RX_SPECIAL_SETTINGS_NO_LIM BIT(0)
278 #define TRF7970A_RX_SPECIAL_SETTINGS_AGCR BIT(1)
279 #define TRF7970A_RX_SPECIAL_SETTINGS_GD_0DB (0x0 << 2)
280 #define TRF7970A_RX_SPECIAL_SETTINGS_GD_5DB (0x1 << 2)
281 #define TRF7970A_RX_SPECIAL_SETTINGS_GD_10DB (0x2 << 2)
282 #define TRF7970A_RX_SPECIAL_SETTINGS_GD_15DB (0x3 << 2)
283 #define TRF7970A_RX_SPECIAL_SETTINGS_HBT BIT(4)
284 #define TRF7970A_RX_SPECIAL_SETTINGS_M848 BIT(5)
285 #define TRF7970A_RX_SPECIAL_SETTINGS_C424 BIT(6)
286 #define TRF7970A_RX_SPECIAL_SETTINGS_C212 BIT(7)
288 #define TRF7970A_REG_IO_CTRL_VRS(v) ((v) & 0x07)
289 #define TRF7970A_REG_IO_CTRL_IO_LOW BIT(5)
290 #define TRF7970A_REG_IO_CTRL_EN_EXT_PA BIT(6)
291 #define TRF7970A_REG_IO_CTRL_AUTO_REG BIT(7)
293 /* IRQ Status Register Bits */
294 #define TRF7970A_IRQ_STATUS_NORESP BIT(0) /* ISO15693 only */
295 #define TRF7970A_IRQ_STATUS_NFC_COL_ERROR BIT(0)
296 #define TRF7970A_IRQ_STATUS_COL BIT(1)
297 #define TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR BIT(2)
298 #define TRF7970A_IRQ_STATUS_NFC_RF BIT(2)
299 #define TRF7970A_IRQ_STATUS_PARITY_ERROR BIT(3)
300 #define TRF7970A_IRQ_STATUS_NFC_SDD BIT(3)
301 #define TRF7970A_IRQ_STATUS_CRC_ERROR BIT(4)
302 #define TRF7970A_IRQ_STATUS_NFC_PROTO_ERROR BIT(4)
303 #define TRF7970A_IRQ_STATUS_FIFO BIT(5)
304 #define TRF7970A_IRQ_STATUS_SRX BIT(6)
305 #define TRF7970A_IRQ_STATUS_TX BIT(7)
307 #define TRF7970A_IRQ_STATUS_ERROR \
308 (TRF7970A_IRQ_STATUS_COL | \
309 TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR | \
310 TRF7970A_IRQ_STATUS_PARITY_ERROR | \
311 TRF7970A_IRQ_STATUS_CRC_ERROR)
313 #define TRF7970A_RSSI_OSC_STATUS_RSSI_MASK (BIT(2) | BIT(1) | BIT(0))
314 #define TRF7970A_RSSI_OSC_STATUS_RSSI_X_MASK (BIT(5) | BIT(4) | BIT(3))
315 #define TRF7970A_RSSI_OSC_STATUS_RSSI_OSC_OK BIT(6)
317 #define TRF7970A_SPECIAL_FCN_REG1_COL_7_6 BIT(0)
318 #define TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL BIT(1)
319 #define TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX BIT(2)
320 #define TRF7970A_SPECIAL_FCN_REG1_SP_DIR_MODE BIT(3)
321 #define TRF7970A_SPECIAL_FCN_REG1_NEXT_SLOT_37US BIT(4)
322 #define TRF7970A_SPECIAL_FCN_REG1_PAR43 BIT(5)
324 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_124 (0x0 << 2)
325 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_120 (0x1 << 2)
326 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_112 (0x2 << 2)
327 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 (0x3 << 2)
328 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_4 0x0
329 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_8 0x1
330 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_16 0x2
331 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32 0x3
333 #define TRF7970A_NFC_LOW_FIELD_LEVEL_RFDET(v) ((v) & 0x07)
334 #define TRF7970A_NFC_LOW_FIELD_LEVEL_CLEX_DIS BIT(7)
336 #define TRF7970A_NFC_TARGET_LEVEL_RFDET(v) ((v) & 0x07)
337 #define TRF7970A_NFC_TARGET_LEVEL_HI_RF BIT(3)
338 #define TRF7970A_NFC_TARGET_LEVEL_SDD_EN BIT(3)
339 #define TRF7970A_NFC_TARGET_LEVEL_LD_S_4BYTES (0x0 << 6)
340 #define TRF7970A_NFC_TARGET_LEVEL_LD_S_7BYTES (0x1 << 6)
341 #define TRF7970A_NFC_TARGET_LEVEL_LD_S_10BYTES (0x2 << 6)
343 #define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106 BIT(0)
344 #define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_212 BIT(1)
345 #define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_424 (BIT(0) | BIT(1))
346 #define TRF79070A_NFC_TARGET_PROTOCOL_PAS_14443B BIT(2)
347 #define TRF79070A_NFC_TARGET_PROTOCOL_PAS_106 BIT(3)
348 #define TRF79070A_NFC_TARGET_PROTOCOL_FELICA BIT(4)
349 #define TRF79070A_NFC_TARGET_PROTOCOL_RF_L BIT(6)
350 #define TRF79070A_NFC_TARGET_PROTOCOL_RF_H BIT(7)
352 #define TRF79070A_NFC_TARGET_PROTOCOL_106A \
353 (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \
354 TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \
355 TRF79070A_NFC_TARGET_PROTOCOL_PAS_106 | \
356 TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106)
358 #define TRF79070A_NFC_TARGET_PROTOCOL_106B \
359 (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \
360 TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \
361 TRF79070A_NFC_TARGET_PROTOCOL_PAS_14443B | \
362 TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106)
364 #define TRF79070A_NFC_TARGET_PROTOCOL_212F \
365 (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \
366 TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \
367 TRF79070A_NFC_TARGET_PROTOCOL_FELICA | \
368 TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_212)
370 #define TRF79070A_NFC_TARGET_PROTOCOL_424F \
371 (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \
372 TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \
373 TRF79070A_NFC_TARGET_PROTOCOL_FELICA | \
374 TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_424)
376 #define TRF7970A_FIFO_STATUS_OVERFLOW BIT(7)
378 /* NFC (ISO/IEC 14443A) Type 2 Tag commands */
379 #define NFC_T2T_CMD_READ 0x30
381 /* ISO 15693 commands codes */
382 #define ISO15693_CMD_INVENTORY 0x01
383 #define ISO15693_CMD_READ_SINGLE_BLOCK 0x20
384 #define ISO15693_CMD_WRITE_SINGLE_BLOCK 0x21
385 #define ISO15693_CMD_LOCK_BLOCK 0x22
386 #define ISO15693_CMD_READ_MULTIPLE_BLOCK 0x23
387 #define ISO15693_CMD_WRITE_MULTIPLE_BLOCK 0x24
388 #define ISO15693_CMD_SELECT 0x25
389 #define ISO15693_CMD_RESET_TO_READY 0x26
390 #define ISO15693_CMD_WRITE_AFI 0x27
391 #define ISO15693_CMD_LOCK_AFI 0x28
392 #define ISO15693_CMD_WRITE_DSFID 0x29
393 #define ISO15693_CMD_LOCK_DSFID 0x2a
394 #define ISO15693_CMD_GET_SYSTEM_INFO 0x2b
395 #define ISO15693_CMD_GET_MULTIPLE_BLOCK_SECURITY_STATUS 0x2c
397 /* ISO 15693 request and response flags */
398 #define ISO15693_REQ_FLAG_SUB_CARRIER BIT(0)
399 #define ISO15693_REQ_FLAG_DATA_RATE BIT(1)
400 #define ISO15693_REQ_FLAG_INVENTORY BIT(2)
401 #define ISO15693_REQ_FLAG_PROTOCOL_EXT BIT(3)
402 #define ISO15693_REQ_FLAG_SELECT BIT(4)
403 #define ISO15693_REQ_FLAG_AFI BIT(4)
404 #define ISO15693_REQ_FLAG_ADDRESS BIT(5)
405 #define ISO15693_REQ_FLAG_NB_SLOTS BIT(5)
406 #define ISO15693_REQ_FLAG_OPTION BIT(6)
408 #define ISO15693_REQ_FLAG_SPEED_MASK \
409 (ISO15693_REQ_FLAG_SUB_CARRIER | ISO15693_REQ_FLAG_DATA_RATE)
411 enum trf7970a_state
{
415 TRF7970A_ST_IDLE_RX_BLOCKED
,
416 TRF7970A_ST_WAIT_FOR_TX_FIFO
,
417 TRF7970A_ST_WAIT_FOR_RX_DATA
,
418 TRF7970A_ST_WAIT_FOR_RX_DATA_CONT
,
419 TRF7970A_ST_WAIT_TO_ISSUE_EOF
,
420 TRF7970A_ST_LISTENING
,
421 TRF7970A_ST_LISTENING_MD
,
426 enum trf7970a_state state
;
428 struct spi_device
*spi
;
429 struct regulator
*regulator
;
430 struct nfc_digital_dev
*ddev
;
434 struct sk_buff
*tx_skb
;
435 struct sk_buff
*rx_skb
;
436 nfc_digital_cmd_complete_t cb
;
441 u8 modulator_sys_clk_ctrl
;
443 unsigned int guard_time
;
452 unsigned int timeout
;
454 struct delayed_work timeout_work
;
458 static int trf7970a_cmd(struct trf7970a
*trf
, u8 opcode
)
460 u8 cmd
= TRF7970A_CMD_BIT_CTRL
| TRF7970A_CMD_BIT_OPCODE(opcode
);
463 dev_dbg(trf
->dev
, "cmd: 0x%x\n", cmd
);
465 ret
= spi_write(trf
->spi
, &cmd
, 1);
467 dev_err(trf
->dev
, "%s - cmd: 0x%x, ret: %d\n", __func__
, cmd
,
472 static int trf7970a_read(struct trf7970a
*trf
, u8 reg
, u8
*val
)
474 u8 addr
= TRF7970A_CMD_BIT_RW
| reg
;
477 ret
= spi_write_then_read(trf
->spi
, &addr
, 1, val
, 1);
479 dev_err(trf
->dev
, "%s - addr: 0x%x, ret: %d\n", __func__
, addr
,
482 dev_dbg(trf
->dev
, "read(0x%x): 0x%x\n", addr
, *val
);
487 static int trf7970a_read_cont(struct trf7970a
*trf
, u8 reg
, u8
*buf
, size_t len
)
489 u8 addr
= reg
| TRF7970A_CMD_BIT_RW
| TRF7970A_CMD_BIT_CONTINUOUS
;
490 struct spi_transfer t
[2];
491 struct spi_message m
;
494 dev_dbg(trf
->dev
, "read_cont(0x%x, %zd)\n", addr
, len
);
496 spi_message_init(&m
);
498 memset(&t
, 0, sizeof(t
));
501 t
[0].len
= sizeof(addr
);
502 spi_message_add_tail(&t
[0], &m
);
506 spi_message_add_tail(&t
[1], &m
);
508 ret
= spi_sync(trf
->spi
, &m
);
510 dev_err(trf
->dev
, "%s - addr: 0x%x, ret: %d\n", __func__
, addr
,
515 static int trf7970a_write(struct trf7970a
*trf
, u8 reg
, u8 val
)
517 u8 buf
[2] = { reg
, val
};
520 dev_dbg(trf
->dev
, "write(0x%x): 0x%x\n", reg
, val
);
522 ret
= spi_write(trf
->spi
, buf
, 2);
524 dev_err(trf
->dev
, "%s - write: 0x%x 0x%x, ret: %d\n", __func__
,
525 buf
[0], buf
[1], ret
);
530 static int trf7970a_read_irqstatus(struct trf7970a
*trf
, u8
*status
)
536 addr
= TRF7970A_IRQ_STATUS
| TRF7970A_CMD_BIT_RW
;
538 if (trf
->quirks
& TRF7970A_QUIRK_IRQ_STATUS_READ
) {
539 addr
|= TRF7970A_CMD_BIT_CONTINUOUS
;
540 ret
= spi_write_then_read(trf
->spi
, &addr
, 1, buf
, 2);
542 ret
= spi_write_then_read(trf
->spi
, &addr
, 1, buf
, 1);
546 dev_err(trf
->dev
, "%s - irqstatus: Status read failed: %d\n",
554 static int trf7970a_read_target_proto(struct trf7970a
*trf
, u8
*target_proto
)
560 addr
= TRF79070A_NFC_TARGET_PROTOCOL
| TRF7970A_CMD_BIT_RW
|
561 TRF7970A_CMD_BIT_CONTINUOUS
;
563 ret
= spi_write_then_read(trf
->spi
, &addr
, 1, buf
, 2);
565 dev_err(trf
->dev
, "%s - target_proto: Read failed: %d\n",
568 *target_proto
= buf
[0];
573 static int trf7970a_mode_detect(struct trf7970a
*trf
, u8
*rf_tech
)
576 u8 target_proto
, tech
;
578 ret
= trf7970a_read_target_proto(trf
, &target_proto
);
582 switch (target_proto
) {
583 case TRF79070A_NFC_TARGET_PROTOCOL_106A
:
584 tech
= NFC_DIGITAL_RF_TECH_106A
;
586 case TRF79070A_NFC_TARGET_PROTOCOL_106B
:
587 tech
= NFC_DIGITAL_RF_TECH_106B
;
589 case TRF79070A_NFC_TARGET_PROTOCOL_212F
:
590 tech
= NFC_DIGITAL_RF_TECH_212F
;
592 case TRF79070A_NFC_TARGET_PROTOCOL_424F
:
593 tech
= NFC_DIGITAL_RF_TECH_424F
;
596 dev_dbg(trf
->dev
, "%s - mode_detect: target_proto: 0x%x\n",
597 __func__
, target_proto
);
606 static void trf7970a_send_upstream(struct trf7970a
*trf
)
608 dev_kfree_skb_any(trf
->tx_skb
);
611 if (trf
->rx_skb
&& !IS_ERR(trf
->rx_skb
) && !trf
->aborting
)
612 print_hex_dump_debug("trf7970a rx data: ", DUMP_PREFIX_NONE
,
613 16, 1, trf
->rx_skb
->data
, trf
->rx_skb
->len
,
616 trf
->state
= TRF7970A_ST_IDLE
;
619 dev_dbg(trf
->dev
, "Abort process complete\n");
621 if (!IS_ERR(trf
->rx_skb
)) {
622 kfree_skb(trf
->rx_skb
);
623 trf
->rx_skb
= ERR_PTR(-ECANCELED
);
626 trf
->aborting
= false;
629 trf
->cb(trf
->ddev
, trf
->cb_arg
, trf
->rx_skb
);
634 static void trf7970a_send_err_upstream(struct trf7970a
*trf
, int errno
)
636 dev_dbg(trf
->dev
, "Error - state: %d, errno: %d\n", trf
->state
, errno
);
638 cancel_delayed_work(&trf
->timeout_work
);
640 kfree_skb(trf
->rx_skb
);
641 trf
->rx_skb
= ERR_PTR(errno
);
643 trf7970a_send_upstream(trf
);
646 static int trf7970a_transmit(struct trf7970a
*trf
, struct sk_buff
*skb
,
647 unsigned int len
, u8
*prefix
, unsigned int prefix_len
)
649 struct spi_transfer t
[2];
650 struct spi_message m
;
651 unsigned int timeout
;
654 print_hex_dump_debug("trf7970a tx data: ", DUMP_PREFIX_NONE
,
655 16, 1, skb
->data
, len
, false);
657 spi_message_init(&m
);
659 memset(&t
, 0, sizeof(t
));
661 t
[0].tx_buf
= prefix
;
662 t
[0].len
= prefix_len
;
663 spi_message_add_tail(&t
[0], &m
);
665 t
[1].tx_buf
= skb
->data
;
667 spi_message_add_tail(&t
[1], &m
);
669 ret
= spi_sync(trf
->spi
, &m
);
671 dev_err(trf
->dev
, "%s - Can't send tx data: %d\n", __func__
,
679 trf
->state
= TRF7970A_ST_WAIT_FOR_TX_FIFO
;
680 timeout
= TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT
;
682 if (trf
->issue_eof
) {
683 trf
->state
= TRF7970A_ST_WAIT_TO_ISSUE_EOF
;
684 timeout
= TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF
;
686 trf
->state
= TRF7970A_ST_WAIT_FOR_RX_DATA
;
689 timeout
= TRF7970A_WAIT_FOR_TX_IRQ
;
691 timeout
= trf
->timeout
;
695 dev_dbg(trf
->dev
, "Setting timeout for %d ms, state: %d\n", timeout
,
698 schedule_delayed_work(&trf
->timeout_work
, msecs_to_jiffies(timeout
));
703 static void trf7970a_fill_fifo(struct trf7970a
*trf
)
705 struct sk_buff
*skb
= trf
->tx_skb
;
711 ret
= trf7970a_read(trf
, TRF7970A_FIFO_STATUS
, &fifo_bytes
);
713 trf7970a_send_err_upstream(trf
, ret
);
717 dev_dbg(trf
->dev
, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes
);
719 fifo_bytes
&= ~TRF7970A_FIFO_STATUS_OVERFLOW
;
721 /* Calculate how much more data can be written to the fifo */
722 len
= TRF7970A_FIFO_SIZE
- fifo_bytes
;
724 schedule_delayed_work(&trf
->timeout_work
,
725 msecs_to_jiffies(TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT
));
729 len
= min(skb
->len
, len
);
731 prefix
= TRF7970A_CMD_BIT_CONTINUOUS
| TRF7970A_FIFO_IO_REGISTER
;
733 ret
= trf7970a_transmit(trf
, skb
, len
, &prefix
, sizeof(prefix
));
735 trf7970a_send_err_upstream(trf
, ret
);
738 static void trf7970a_drain_fifo(struct trf7970a
*trf
, u8 status
)
740 struct sk_buff
*skb
= trf
->rx_skb
;
744 if (status
& TRF7970A_IRQ_STATUS_ERROR
) {
745 trf7970a_send_err_upstream(trf
, -EIO
);
749 ret
= trf7970a_read(trf
, TRF7970A_FIFO_STATUS
, &fifo_bytes
);
751 trf7970a_send_err_upstream(trf
, ret
);
755 dev_dbg(trf
->dev
, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes
);
757 fifo_bytes
&= ~TRF7970A_FIFO_STATUS_OVERFLOW
;
762 if (fifo_bytes
> skb_tailroom(skb
)) {
763 skb
= skb_copy_expand(skb
, skb_headroom(skb
),
764 max_t(int, fifo_bytes
,
765 TRF7970A_RX_SKB_ALLOC_SIZE
),
768 trf7970a_send_err_upstream(trf
, -ENOMEM
);
772 kfree_skb(trf
->rx_skb
);
776 ret
= trf7970a_read_cont(trf
, TRF7970A_FIFO_IO_REGISTER
,
777 skb_put(skb
, fifo_bytes
), fifo_bytes
);
779 trf7970a_send_err_upstream(trf
, ret
);
783 /* If received Type 2 ACK/NACK, shift right 4 bits and pass up */
784 if ((trf
->framing
== NFC_DIGITAL_FRAMING_NFCA_T2T
) && (skb
->len
== 1) &&
785 (trf
->special_fcn_reg1
==
786 TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX
)) {
788 status
= TRF7970A_IRQ_STATUS_SRX
;
790 trf
->state
= TRF7970A_ST_WAIT_FOR_RX_DATA_CONT
;
792 ret
= trf7970a_read(trf
, TRF7970A_FIFO_STATUS
, &fifo_bytes
);
794 trf7970a_send_err_upstream(trf
, ret
);
798 fifo_bytes
&= ~TRF7970A_FIFO_STATUS_OVERFLOW
;
800 /* If there are bytes in the FIFO, set status to '0' so
801 * the if stmt below doesn't fire and the driver will wait
802 * for the trf7970a to generate another RX interrupt.
809 if (status
== TRF7970A_IRQ_STATUS_SRX
) { /* Receive complete */
810 trf7970a_send_upstream(trf
);
814 dev_dbg(trf
->dev
, "Setting timeout for %d ms\n",
815 TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT
);
817 schedule_delayed_work(&trf
->timeout_work
,
818 msecs_to_jiffies(TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT
));
821 static irqreturn_t
trf7970a_irq(int irq
, void *dev_id
)
823 struct trf7970a
*trf
= dev_id
;
825 u8 status
, fifo_bytes
, iso_ctrl
;
827 mutex_lock(&trf
->lock
);
829 if (trf
->state
== TRF7970A_ST_RF_OFF
) {
830 mutex_unlock(&trf
->lock
);
834 ret
= trf7970a_read_irqstatus(trf
, &status
);
836 mutex_unlock(&trf
->lock
);
840 dev_dbg(trf
->dev
, "IRQ - state: %d, status: 0x%x\n", trf
->state
,
844 mutex_unlock(&trf
->lock
);
848 switch (trf
->state
) {
849 case TRF7970A_ST_IDLE
:
850 case TRF7970A_ST_IDLE_RX_BLOCKED
:
851 /* If initiator and getting interrupts caused by RF noise,
852 * turn off the receiver to avoid unnecessary interrupts.
853 * It will be turned back on in trf7970a_send_cmd() when
854 * the next command is issued.
856 if (trf
->is_initiator
&& (status
& TRF7970A_IRQ_STATUS_ERROR
)) {
857 trf7970a_cmd(trf
, TRF7970A_CMD_BLOCK_RX
);
858 trf
->state
= TRF7970A_ST_IDLE_RX_BLOCKED
;
861 trf7970a_cmd(trf
, TRF7970A_CMD_FIFO_RESET
);
863 case TRF7970A_ST_WAIT_FOR_TX_FIFO
:
864 if (status
& TRF7970A_IRQ_STATUS_TX
) {
865 trf
->ignore_timeout
=
866 !cancel_delayed_work(&trf
->timeout_work
);
867 trf7970a_fill_fifo(trf
);
869 trf7970a_send_err_upstream(trf
, -EIO
);
872 case TRF7970A_ST_WAIT_FOR_RX_DATA
:
873 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT
:
874 if (status
& TRF7970A_IRQ_STATUS_SRX
) {
875 trf
->ignore_timeout
=
876 !cancel_delayed_work(&trf
->timeout_work
);
877 trf7970a_drain_fifo(trf
, status
);
878 } else if (status
& TRF7970A_IRQ_STATUS_FIFO
) {
879 ret
= trf7970a_read(trf
, TRF7970A_FIFO_STATUS
,
882 fifo_bytes
&= ~TRF7970A_FIFO_STATUS_OVERFLOW
;
885 trf7970a_send_err_upstream(trf
, ret
);
886 else if (!fifo_bytes
)
887 trf7970a_cmd(trf
, TRF7970A_CMD_FIFO_RESET
);
888 } else if ((status
== TRF7970A_IRQ_STATUS_TX
) ||
889 (!trf
->is_initiator
&&
890 (status
== (TRF7970A_IRQ_STATUS_TX
|
891 TRF7970A_IRQ_STATUS_NFC_RF
)))) {
892 trf7970a_cmd(trf
, TRF7970A_CMD_FIFO_RESET
);
895 trf
->ignore_timeout
= !cancel_delayed_work(
897 trf
->rx_skb
= ERR_PTR(0);
898 trf7970a_send_upstream(trf
);
902 if (trf
->is_initiator
)
905 iso_ctrl
= trf
->iso_ctrl
;
907 switch (trf
->framing
) {
908 case NFC_DIGITAL_FRAMING_NFCA_STANDARD
:
909 trf
->tx_cmd
= TRF7970A_CMD_TRANSMIT_NO_CRC
;
910 iso_ctrl
|= TRF7970A_ISO_CTRL_RX_CRC_N
;
911 trf
->iso_ctrl
= 0xff; /* Force ISO_CTRL write */
913 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A
:
914 trf
->tx_cmd
= TRF7970A_CMD_TRANSMIT
;
915 iso_ctrl
&= ~TRF7970A_ISO_CTRL_RX_CRC_N
;
916 trf
->iso_ctrl
= 0xff; /* Force ISO_CTRL write */
918 case NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE
:
919 ret
= trf7970a_write(trf
,
920 TRF7970A_SPECIAL_FCN_REG1
,
921 TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL
);
923 goto err_unlock_exit
;
925 trf
->special_fcn_reg1
=
926 TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL
;
932 if (iso_ctrl
!= trf
->iso_ctrl
) {
933 ret
= trf7970a_write(trf
, TRF7970A_ISO_CTRL
,
936 goto err_unlock_exit
;
938 trf
->iso_ctrl
= iso_ctrl
;
941 trf7970a_send_err_upstream(trf
, -EIO
);
944 case TRF7970A_ST_WAIT_TO_ISSUE_EOF
:
945 if (status
!= TRF7970A_IRQ_STATUS_TX
)
946 trf7970a_send_err_upstream(trf
, -EIO
);
948 case TRF7970A_ST_LISTENING
:
949 if (status
& TRF7970A_IRQ_STATUS_SRX
) {
950 trf
->ignore_timeout
=
951 !cancel_delayed_work(&trf
->timeout_work
);
952 trf7970a_drain_fifo(trf
, status
);
953 } else if (!(status
& TRF7970A_IRQ_STATUS_NFC_RF
)) {
954 trf7970a_send_err_upstream(trf
, -EIO
);
957 case TRF7970A_ST_LISTENING_MD
:
958 if (status
& TRF7970A_IRQ_STATUS_SRX
) {
959 trf
->ignore_timeout
=
960 !cancel_delayed_work(&trf
->timeout_work
);
962 ret
= trf7970a_mode_detect(trf
, &trf
->md_rf_tech
);
964 trf7970a_send_err_upstream(trf
, ret
);
966 trf
->state
= TRF7970A_ST_LISTENING
;
967 trf7970a_drain_fifo(trf
, status
);
969 } else if (!(status
& TRF7970A_IRQ_STATUS_NFC_RF
)) {
970 trf7970a_send_err_upstream(trf
, -EIO
);
974 dev_err(trf
->dev
, "%s - Driver in invalid state: %d\n",
975 __func__
, trf
->state
);
979 mutex_unlock(&trf
->lock
);
983 static void trf7970a_issue_eof(struct trf7970a
*trf
)
987 dev_dbg(trf
->dev
, "Issuing EOF\n");
989 ret
= trf7970a_cmd(trf
, TRF7970A_CMD_FIFO_RESET
);
991 trf7970a_send_err_upstream(trf
, ret
);
993 ret
= trf7970a_cmd(trf
, TRF7970A_CMD_EOF
);
995 trf7970a_send_err_upstream(trf
, ret
);
997 trf
->state
= TRF7970A_ST_WAIT_FOR_RX_DATA
;
999 dev_dbg(trf
->dev
, "Setting timeout for %d ms, state: %d\n",
1000 trf
->timeout
, trf
->state
);
1002 schedule_delayed_work(&trf
->timeout_work
,
1003 msecs_to_jiffies(trf
->timeout
));
1006 static void trf7970a_timeout_work_handler(struct work_struct
*work
)
1008 struct trf7970a
*trf
= container_of(work
, struct trf7970a
,
1011 dev_dbg(trf
->dev
, "Timeout - state: %d, ignore_timeout: %d\n",
1012 trf
->state
, trf
->ignore_timeout
);
1014 mutex_lock(&trf
->lock
);
1016 if (trf
->ignore_timeout
)
1017 trf
->ignore_timeout
= false;
1018 else if (trf
->state
== TRF7970A_ST_WAIT_FOR_RX_DATA_CONT
)
1019 trf7970a_drain_fifo(trf
, TRF7970A_IRQ_STATUS_SRX
);
1020 else if (trf
->state
== TRF7970A_ST_WAIT_TO_ISSUE_EOF
)
1021 trf7970a_issue_eof(trf
);
1023 trf7970a_send_err_upstream(trf
, -ETIMEDOUT
);
1025 mutex_unlock(&trf
->lock
);
1028 static int trf7970a_init(struct trf7970a
*trf
)
1032 dev_dbg(trf
->dev
, "Initializing device - state: %d\n", trf
->state
);
1034 ret
= trf7970a_cmd(trf
, TRF7970A_CMD_SOFT_INIT
);
1038 ret
= trf7970a_cmd(trf
, TRF7970A_CMD_IDLE
);
1042 usleep_range(1000, 2000);
1044 trf
->chip_status_ctrl
&= ~TRF7970A_CHIP_STATUS_RF_ON
;
1046 ret
= trf7970a_write(trf
, TRF7970A_MODULATOR_SYS_CLK_CTRL
, 0);
1050 trf
->modulator_sys_clk_ctrl
= 0;
1052 ret
= trf7970a_write(trf
, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS
,
1053 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96
|
1054 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32
);
1058 ret
= trf7970a_write(trf
, TRF7970A_SPECIAL_FCN_REG1
, 0);
1062 trf
->special_fcn_reg1
= 0;
1064 trf
->iso_ctrl
= 0xff;
1068 dev_dbg(trf
->dev
, "Couldn't init device: %d\n", ret
);
1072 static void trf7970a_switch_rf_off(struct trf7970a
*trf
)
1074 if ((trf
->state
== TRF7970A_ST_PWR_OFF
) ||
1075 (trf
->state
== TRF7970A_ST_RF_OFF
))
1078 dev_dbg(trf
->dev
, "Switching rf off\n");
1080 trf
->chip_status_ctrl
&= ~TRF7970A_CHIP_STATUS_RF_ON
;
1082 trf7970a_write(trf
, TRF7970A_CHIP_STATUS_CTRL
, trf
->chip_status_ctrl
);
1084 trf
->aborting
= false;
1085 trf
->state
= TRF7970A_ST_RF_OFF
;
1087 pm_runtime_mark_last_busy(trf
->dev
);
1088 pm_runtime_put_autosuspend(trf
->dev
);
1091 static int trf7970a_switch_rf_on(struct trf7970a
*trf
)
1095 dev_dbg(trf
->dev
, "Switching rf on\n");
1097 pm_runtime_get_sync(trf
->dev
);
1099 if (trf
->state
!= TRF7970A_ST_RF_OFF
) { /* Power on, RF off */
1100 dev_err(trf
->dev
, "%s - Incorrect state: %d\n", __func__
,
1105 ret
= trf7970a_init(trf
);
1107 dev_err(trf
->dev
, "%s - Can't initialize: %d\n", __func__
, ret
);
1111 trf
->state
= TRF7970A_ST_IDLE
;
1116 static int trf7970a_switch_rf(struct nfc_digital_dev
*ddev
, bool on
)
1118 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
1121 dev_dbg(trf
->dev
, "Switching RF - state: %d, on: %d\n", trf
->state
, on
);
1123 mutex_lock(&trf
->lock
);
1126 switch (trf
->state
) {
1127 case TRF7970A_ST_PWR_OFF
:
1128 case TRF7970A_ST_RF_OFF
:
1129 ret
= trf7970a_switch_rf_on(trf
);
1131 case TRF7970A_ST_IDLE
:
1132 case TRF7970A_ST_IDLE_RX_BLOCKED
:
1135 dev_err(trf
->dev
, "%s - Invalid request: %d %d\n",
1136 __func__
, trf
->state
, on
);
1137 trf7970a_switch_rf_off(trf
);
1141 switch (trf
->state
) {
1142 case TRF7970A_ST_PWR_OFF
:
1143 case TRF7970A_ST_RF_OFF
:
1146 dev_err(trf
->dev
, "%s - Invalid request: %d %d\n",
1147 __func__
, trf
->state
, on
);
1150 case TRF7970A_ST_IDLE
:
1151 case TRF7970A_ST_IDLE_RX_BLOCKED
:
1152 case TRF7970A_ST_WAIT_FOR_RX_DATA
:
1153 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT
:
1154 trf7970a_switch_rf_off(trf
);
1158 mutex_unlock(&trf
->lock
);
1162 static int trf7970a_in_config_rf_tech(struct trf7970a
*trf
, int tech
)
1166 dev_dbg(trf
->dev
, "rf technology: %d\n", tech
);
1169 case NFC_DIGITAL_RF_TECH_106A
:
1170 trf
->iso_ctrl_tech
= TRF7970A_ISO_CTRL_14443A_106
;
1171 trf
->modulator_sys_clk_ctrl
= TRF7970A_MODULATOR_DEPTH_OOK
;
1172 trf
->guard_time
= TRF7970A_GUARD_TIME_NFCA
;
1174 case NFC_DIGITAL_RF_TECH_106B
:
1175 trf
->iso_ctrl_tech
= TRF7970A_ISO_CTRL_14443B_106
;
1176 trf
->modulator_sys_clk_ctrl
= TRF7970A_MODULATOR_DEPTH_ASK10
;
1177 trf
->guard_time
= TRF7970A_GUARD_TIME_NFCB
;
1179 case NFC_DIGITAL_RF_TECH_212F
:
1180 trf
->iso_ctrl_tech
= TRF7970A_ISO_CTRL_FELICA_212
;
1181 trf
->modulator_sys_clk_ctrl
= TRF7970A_MODULATOR_DEPTH_ASK10
;
1182 trf
->guard_time
= TRF7970A_GUARD_TIME_NFCF
;
1184 case NFC_DIGITAL_RF_TECH_424F
:
1185 trf
->iso_ctrl_tech
= TRF7970A_ISO_CTRL_FELICA_424
;
1186 trf
->modulator_sys_clk_ctrl
= TRF7970A_MODULATOR_DEPTH_ASK10
;
1187 trf
->guard_time
= TRF7970A_GUARD_TIME_NFCF
;
1189 case NFC_DIGITAL_RF_TECH_ISO15693
:
1190 trf
->iso_ctrl_tech
= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648
;
1191 trf
->modulator_sys_clk_ctrl
= TRF7970A_MODULATOR_DEPTH_OOK
;
1192 trf
->guard_time
= TRF7970A_GUARD_TIME_15693
;
1195 dev_dbg(trf
->dev
, "Unsupported rf technology: %d\n", tech
);
1199 trf
->technology
= tech
;
1201 /* If in initiator mode and not changing the RF tech due to a
1202 * PSL sequence (indicated by 'trf->iso_ctrl == 0xff' from
1203 * trf7970a_init()), clear the NFC Target Detection Level register
1206 if (trf
->iso_ctrl
== 0xff)
1207 ret
= trf7970a_write(trf
, TRF7970A_NFC_TARGET_LEVEL
, 0);
1212 static int trf7970a_is_rf_field(struct trf7970a
*trf
, bool *is_rf_field
)
1217 ret
= trf7970a_write(trf
, TRF7970A_CHIP_STATUS_CTRL
,
1218 trf
->chip_status_ctrl
| TRF7970A_CHIP_STATUS_REC_ON
);
1222 ret
= trf7970a_cmd(trf
, TRF7970A_CMD_TEST_EXT_RF
);
1226 usleep_range(50, 60);
1228 ret
= trf7970a_read(trf
, TRF7970A_RSSI_OSC_STATUS
, &rssi
);
1232 ret
= trf7970a_write(trf
, TRF7970A_CHIP_STATUS_CTRL
,
1233 trf
->chip_status_ctrl
);
1237 if (rssi
& TRF7970A_RSSI_OSC_STATUS_RSSI_MASK
)
1238 *is_rf_field
= true;
1240 *is_rf_field
= false;
1245 static int trf7970a_in_config_framing(struct trf7970a
*trf
, int framing
)
1247 u8 iso_ctrl
= trf
->iso_ctrl_tech
;
1248 bool is_rf_field
= false;
1251 dev_dbg(trf
->dev
, "framing: %d\n", framing
);
1254 case NFC_DIGITAL_FRAMING_NFCA_SHORT
:
1255 case NFC_DIGITAL_FRAMING_NFCA_STANDARD
:
1256 trf
->tx_cmd
= TRF7970A_CMD_TRANSMIT_NO_CRC
;
1257 iso_ctrl
|= TRF7970A_ISO_CTRL_RX_CRC_N
;
1259 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A
:
1260 case NFC_DIGITAL_FRAMING_NFCA_T4T
:
1261 case NFC_DIGITAL_FRAMING_NFCB
:
1262 case NFC_DIGITAL_FRAMING_NFCB_T4T
:
1263 case NFC_DIGITAL_FRAMING_NFCF
:
1264 case NFC_DIGITAL_FRAMING_NFCF_T3T
:
1265 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY
:
1266 case NFC_DIGITAL_FRAMING_ISO15693_T5T
:
1267 case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP
:
1268 case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP
:
1269 trf
->tx_cmd
= TRF7970A_CMD_TRANSMIT
;
1270 iso_ctrl
&= ~TRF7970A_ISO_CTRL_RX_CRC_N
;
1272 case NFC_DIGITAL_FRAMING_NFCA_T2T
:
1273 trf
->tx_cmd
= TRF7970A_CMD_TRANSMIT
;
1274 iso_ctrl
|= TRF7970A_ISO_CTRL_RX_CRC_N
;
1277 dev_dbg(trf
->dev
, "Unsupported Framing: %d\n", framing
);
1281 trf
->framing
= framing
;
1283 if (!(trf
->chip_status_ctrl
& TRF7970A_CHIP_STATUS_RF_ON
)) {
1284 ret
= trf7970a_is_rf_field(trf
, &is_rf_field
);
1292 if (iso_ctrl
!= trf
->iso_ctrl
) {
1293 ret
= trf7970a_write(trf
, TRF7970A_ISO_CTRL
, iso_ctrl
);
1297 trf
->iso_ctrl
= iso_ctrl
;
1299 ret
= trf7970a_write(trf
, TRF7970A_MODULATOR_SYS_CLK_CTRL
,
1300 trf
->modulator_sys_clk_ctrl
);
1305 if (!(trf
->chip_status_ctrl
& TRF7970A_CHIP_STATUS_RF_ON
)) {
1306 ret
= trf7970a_write(trf
, TRF7970A_CHIP_STATUS_CTRL
,
1307 trf
->chip_status_ctrl
|
1308 TRF7970A_CHIP_STATUS_RF_ON
);
1312 trf
->chip_status_ctrl
|= TRF7970A_CHIP_STATUS_RF_ON
;
1314 usleep_range(trf
->guard_time
, trf
->guard_time
+ 1000);
1320 static int trf7970a_in_configure_hw(struct nfc_digital_dev
*ddev
, int type
,
1323 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
1326 dev_dbg(trf
->dev
, "Configure hw - type: %d, param: %d\n", type
, param
);
1328 mutex_lock(&trf
->lock
);
1330 trf
->is_initiator
= true;
1332 if ((trf
->state
== TRF7970A_ST_PWR_OFF
) ||
1333 (trf
->state
== TRF7970A_ST_RF_OFF
)) {
1334 ret
= trf7970a_switch_rf_on(trf
);
1340 case NFC_DIGITAL_CONFIG_RF_TECH
:
1341 ret
= trf7970a_in_config_rf_tech(trf
, param
);
1343 case NFC_DIGITAL_CONFIG_FRAMING
:
1344 ret
= trf7970a_in_config_framing(trf
, param
);
1347 dev_dbg(trf
->dev
, "Unknown type: %d\n", type
);
1352 mutex_unlock(&trf
->lock
);
1356 static int trf7970a_is_iso15693_write_or_lock(u8 cmd
)
1359 case ISO15693_CMD_WRITE_SINGLE_BLOCK
:
1360 case ISO15693_CMD_LOCK_BLOCK
:
1361 case ISO15693_CMD_WRITE_MULTIPLE_BLOCK
:
1362 case ISO15693_CMD_WRITE_AFI
:
1363 case ISO15693_CMD_LOCK_AFI
:
1364 case ISO15693_CMD_WRITE_DSFID
:
1365 case ISO15693_CMD_LOCK_DSFID
:
1373 static int trf7970a_per_cmd_config(struct trf7970a
*trf
, struct sk_buff
*skb
)
1375 u8
*req
= skb
->data
;
1376 u8 special_fcn_reg1
, iso_ctrl
;
1379 trf
->issue_eof
= false;
1381 /* When issuing Type 2 read command, make sure the '4_bit_RX' bit in
1382 * special functions register 1 is cleared; otherwise, its a write or
1383 * sector select command and '4_bit_RX' must be set.
1385 * When issuing an ISO 15693 command, inspect the flags byte to see
1386 * what speed to use. Also, remember if the OPTION flag is set on
1387 * a Type 5 write or lock command so the driver will know that it
1388 * has to send an EOF in order to get a response.
1390 if ((trf
->technology
== NFC_DIGITAL_RF_TECH_106A
) &&
1391 (trf
->framing
== NFC_DIGITAL_FRAMING_NFCA_T2T
)) {
1392 if (req
[0] == NFC_T2T_CMD_READ
)
1393 special_fcn_reg1
= 0;
1395 special_fcn_reg1
= TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX
;
1397 if (special_fcn_reg1
!= trf
->special_fcn_reg1
) {
1398 ret
= trf7970a_write(trf
, TRF7970A_SPECIAL_FCN_REG1
,
1403 trf
->special_fcn_reg1
= special_fcn_reg1
;
1405 } else if (trf
->technology
== NFC_DIGITAL_RF_TECH_ISO15693
) {
1406 iso_ctrl
= trf
->iso_ctrl
& ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK
;
1408 switch (req
[0] & ISO15693_REQ_FLAG_SPEED_MASK
) {
1410 iso_ctrl
|= TRF7970A_ISO_CTRL_15693_SGL_1OF4_662
;
1412 case ISO15693_REQ_FLAG_SUB_CARRIER
:
1413 iso_ctrl
|= TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a
;
1415 case ISO15693_REQ_FLAG_DATA_RATE
:
1416 iso_ctrl
|= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648
;
1418 case (ISO15693_REQ_FLAG_SUB_CARRIER
|
1419 ISO15693_REQ_FLAG_DATA_RATE
):
1420 iso_ctrl
|= TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669
;
1424 if (iso_ctrl
!= trf
->iso_ctrl
) {
1425 ret
= trf7970a_write(trf
, TRF7970A_ISO_CTRL
, iso_ctrl
);
1429 trf
->iso_ctrl
= iso_ctrl
;
1432 if ((trf
->framing
== NFC_DIGITAL_FRAMING_ISO15693_T5T
) &&
1433 trf7970a_is_iso15693_write_or_lock(req
[1]) &&
1434 (req
[0] & ISO15693_REQ_FLAG_OPTION
))
1435 trf
->issue_eof
= true;
1441 static int trf7970a_send_cmd(struct nfc_digital_dev
*ddev
,
1442 struct sk_buff
*skb
, u16 timeout
,
1443 nfc_digital_cmd_complete_t cb
, void *arg
)
1445 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
1451 dev_dbg(trf
->dev
, "New request - state: %d, timeout: %d ms, len: %d\n",
1452 trf
->state
, timeout
, skb
->len
);
1454 if (skb
->len
> TRF7970A_TX_MAX
)
1457 mutex_lock(&trf
->lock
);
1459 if ((trf
->state
!= TRF7970A_ST_IDLE
) &&
1460 (trf
->state
!= TRF7970A_ST_IDLE_RX_BLOCKED
)) {
1461 dev_err(trf
->dev
, "%s - Bogus state: %d\n", __func__
,
1467 if (trf
->aborting
) {
1468 dev_dbg(trf
->dev
, "Abort process complete\n");
1469 trf
->aborting
= false;
1475 trf
->rx_skb
= nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE
,
1478 dev_dbg(trf
->dev
, "Can't alloc rx_skb\n");
1484 if (trf
->state
== TRF7970A_ST_IDLE_RX_BLOCKED
) {
1485 ret
= trf7970a_cmd(trf
, TRF7970A_CMD_ENABLE_RX
);
1489 trf
->state
= TRF7970A_ST_IDLE
;
1492 if (trf
->is_initiator
) {
1493 ret
= trf7970a_per_cmd_config(trf
, skb
);
1502 trf
->timeout
= timeout
;
1503 trf
->ignore_timeout
= false;
1507 /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
1508 * on what the current framing is, the address of the TX length byte 1
1509 * register (0x1d), and the 2 byte length of the data to be transmitted.
1510 * That totals 5 bytes.
1512 prefix
[0] = TRF7970A_CMD_BIT_CTRL
|
1513 TRF7970A_CMD_BIT_OPCODE(TRF7970A_CMD_FIFO_RESET
);
1514 prefix
[1] = TRF7970A_CMD_BIT_CTRL
|
1515 TRF7970A_CMD_BIT_OPCODE(trf
->tx_cmd
);
1516 prefix
[2] = TRF7970A_CMD_BIT_CONTINUOUS
| TRF7970A_TX_LENGTH_BYTE1
;
1518 if (trf
->framing
== NFC_DIGITAL_FRAMING_NFCA_SHORT
) {
1520 prefix
[4] = 0x0f; /* 7 bits */
1522 prefix
[3] = (len
& 0xf00) >> 4;
1523 prefix
[3] |= ((len
& 0xf0) >> 4);
1524 prefix
[4] = ((len
& 0x0f) << 4);
1527 len
= min_t(int, skb
->len
, TRF7970A_FIFO_SIZE
);
1529 /* Clear possible spurious interrupt */
1530 ret
= trf7970a_read_irqstatus(trf
, &status
);
1534 ret
= trf7970a_transmit(trf
, skb
, len
, prefix
, sizeof(prefix
));
1536 kfree_skb(trf
->rx_skb
);
1541 mutex_unlock(&trf
->lock
);
1545 static int trf7970a_tg_config_rf_tech(struct trf7970a
*trf
, int tech
)
1549 dev_dbg(trf
->dev
, "rf technology: %d\n", tech
);
1552 case NFC_DIGITAL_RF_TECH_106A
:
1553 trf
->iso_ctrl_tech
= TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE
|
1554 TRF7970A_ISO_CTRL_NFC_CE
|
1555 TRF7970A_ISO_CTRL_NFC_CE_14443A
;
1556 trf
->modulator_sys_clk_ctrl
= TRF7970A_MODULATOR_DEPTH_OOK
;
1558 case NFC_DIGITAL_RF_TECH_212F
:
1559 trf
->iso_ctrl_tech
= TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE
|
1560 TRF7970A_ISO_CTRL_NFC_NFCF_212
;
1561 trf
->modulator_sys_clk_ctrl
= TRF7970A_MODULATOR_DEPTH_ASK10
;
1563 case NFC_DIGITAL_RF_TECH_424F
:
1564 trf
->iso_ctrl_tech
= TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE
|
1565 TRF7970A_ISO_CTRL_NFC_NFCF_424
;
1566 trf
->modulator_sys_clk_ctrl
= TRF7970A_MODULATOR_DEPTH_ASK10
;
1569 dev_dbg(trf
->dev
, "Unsupported rf technology: %d\n", tech
);
1573 trf
->technology
= tech
;
1575 /* Normally we write the ISO_CTRL register in
1576 * trf7970a_tg_config_framing() because the framing can change
1577 * the value written. However, when sending a PSL RES,
1578 * digital_tg_send_psl_res_complete() doesn't call
1579 * trf7970a_tg_config_framing() so we must write the register
1582 if ((trf
->framing
== NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED
) &&
1583 (trf
->iso_ctrl_tech
!= trf
->iso_ctrl
)) {
1584 ret
= trf7970a_write(trf
, TRF7970A_ISO_CTRL
,
1585 trf
->iso_ctrl_tech
);
1587 trf
->iso_ctrl
= trf
->iso_ctrl_tech
;
1593 /* Since this is a target routine, several of the framing calls are
1594 * made between receiving the request and sending the response so they
1595 * should take effect until after the response is sent. This is accomplished
1596 * by skipping the ISO_CTRL register write here and doing it in the interrupt
1599 static int trf7970a_tg_config_framing(struct trf7970a
*trf
, int framing
)
1601 u8 iso_ctrl
= trf
->iso_ctrl_tech
;
1604 dev_dbg(trf
->dev
, "framing: %d\n", framing
);
1607 case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP
:
1608 trf
->tx_cmd
= TRF7970A_CMD_TRANSMIT_NO_CRC
;
1609 iso_ctrl
|= TRF7970A_ISO_CTRL_RX_CRC_N
;
1611 case NFC_DIGITAL_FRAMING_NFCA_STANDARD
:
1612 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A
:
1613 case NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE
:
1614 /* These ones are applied in the interrupt handler */
1615 iso_ctrl
= trf
->iso_ctrl
; /* Don't write to ISO_CTRL yet */
1617 case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP
:
1618 trf
->tx_cmd
= TRF7970A_CMD_TRANSMIT
;
1619 iso_ctrl
&= ~TRF7970A_ISO_CTRL_RX_CRC_N
;
1621 case NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED
:
1622 trf
->tx_cmd
= TRF7970A_CMD_TRANSMIT
;
1623 iso_ctrl
&= ~TRF7970A_ISO_CTRL_RX_CRC_N
;
1626 dev_dbg(trf
->dev
, "Unsupported Framing: %d\n", framing
);
1630 trf
->framing
= framing
;
1632 if (iso_ctrl
!= trf
->iso_ctrl
) {
1633 ret
= trf7970a_write(trf
, TRF7970A_ISO_CTRL
, iso_ctrl
);
1637 trf
->iso_ctrl
= iso_ctrl
;
1639 ret
= trf7970a_write(trf
, TRF7970A_MODULATOR_SYS_CLK_CTRL
,
1640 trf
->modulator_sys_clk_ctrl
);
1645 if (!(trf
->chip_status_ctrl
& TRF7970A_CHIP_STATUS_RF_ON
)) {
1646 ret
= trf7970a_write(trf
, TRF7970A_CHIP_STATUS_CTRL
,
1647 trf
->chip_status_ctrl
|
1648 TRF7970A_CHIP_STATUS_RF_ON
);
1652 trf
->chip_status_ctrl
|= TRF7970A_CHIP_STATUS_RF_ON
;
1658 static int trf7970a_tg_configure_hw(struct nfc_digital_dev
*ddev
, int type
,
1661 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
1664 dev_dbg(trf
->dev
, "Configure hw - type: %d, param: %d\n", type
, param
);
1666 mutex_lock(&trf
->lock
);
1668 trf
->is_initiator
= false;
1670 if ((trf
->state
== TRF7970A_ST_PWR_OFF
) ||
1671 (trf
->state
== TRF7970A_ST_RF_OFF
)) {
1672 ret
= trf7970a_switch_rf_on(trf
);
1678 case NFC_DIGITAL_CONFIG_RF_TECH
:
1679 ret
= trf7970a_tg_config_rf_tech(trf
, param
);
1681 case NFC_DIGITAL_CONFIG_FRAMING
:
1682 ret
= trf7970a_tg_config_framing(trf
, param
);
1685 dev_dbg(trf
->dev
, "Unknown type: %d\n", type
);
1690 mutex_unlock(&trf
->lock
);
1694 static int _trf7970a_tg_listen(struct nfc_digital_dev
*ddev
, u16 timeout
,
1695 nfc_digital_cmd_complete_t cb
, void *arg
, bool mode_detect
)
1697 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
1700 mutex_lock(&trf
->lock
);
1702 if ((trf
->state
!= TRF7970A_ST_IDLE
) &&
1703 (trf
->state
!= TRF7970A_ST_IDLE_RX_BLOCKED
)) {
1704 dev_err(trf
->dev
, "%s - Bogus state: %d\n", __func__
,
1710 if (trf
->aborting
) {
1711 dev_dbg(trf
->dev
, "Abort process complete\n");
1712 trf
->aborting
= false;
1717 trf
->rx_skb
= nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE
,
1720 dev_dbg(trf
->dev
, "Can't alloc rx_skb\n");
1725 ret
= trf7970a_write(trf
, TRF7970A_RX_SPECIAL_SETTINGS
,
1726 TRF7970A_RX_SPECIAL_SETTINGS_HBT
|
1727 TRF7970A_RX_SPECIAL_SETTINGS_M848
|
1728 TRF7970A_RX_SPECIAL_SETTINGS_C424
|
1729 TRF7970A_RX_SPECIAL_SETTINGS_C212
);
1733 ret
= trf7970a_write(trf
, TRF7970A_REG_IO_CTRL
,
1734 TRF7970A_REG_IO_CTRL_VRS(0x1));
1738 ret
= trf7970a_write(trf
, TRF7970A_NFC_LOW_FIELD_LEVEL
,
1739 TRF7970A_NFC_LOW_FIELD_LEVEL_RFDET(0x3));
1743 ret
= trf7970a_write(trf
, TRF7970A_NFC_TARGET_LEVEL
,
1744 TRF7970A_NFC_TARGET_LEVEL_RFDET(0x7));
1751 trf
->timeout
= timeout
;
1752 trf
->ignore_timeout
= false;
1754 ret
= trf7970a_cmd(trf
, TRF7970A_CMD_ENABLE_RX
);
1758 trf
->state
= mode_detect
? TRF7970A_ST_LISTENING_MD
:
1759 TRF7970A_ST_LISTENING
;
1761 schedule_delayed_work(&trf
->timeout_work
, msecs_to_jiffies(timeout
));
1764 mutex_unlock(&trf
->lock
);
1768 static int trf7970a_tg_listen(struct nfc_digital_dev
*ddev
, u16 timeout
,
1769 nfc_digital_cmd_complete_t cb
, void *arg
)
1771 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
1773 dev_dbg(trf
->dev
, "Listen - state: %d, timeout: %d ms\n",
1774 trf
->state
, timeout
);
1776 return _trf7970a_tg_listen(ddev
, timeout
, cb
, arg
, false);
1779 static int trf7970a_tg_listen_md(struct nfc_digital_dev
*ddev
,
1780 u16 timeout
, nfc_digital_cmd_complete_t cb
, void *arg
)
1782 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
1785 dev_dbg(trf
->dev
, "Listen MD - state: %d, timeout: %d ms\n",
1786 trf
->state
, timeout
);
1788 ret
= trf7970a_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_RF_TECH
,
1789 NFC_DIGITAL_RF_TECH_106A
);
1793 ret
= trf7970a_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
1794 NFC_DIGITAL_FRAMING_NFCA_NFC_DEP
);
1798 return _trf7970a_tg_listen(ddev
, timeout
, cb
, arg
, true);
1801 static int trf7970a_tg_get_rf_tech(struct nfc_digital_dev
*ddev
, u8
*rf_tech
)
1803 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
1805 dev_dbg(trf
->dev
, "Get RF Tech - state: %d, rf_tech: %d\n",
1806 trf
->state
, trf
->md_rf_tech
);
1808 *rf_tech
= trf
->md_rf_tech
;
1813 static void trf7970a_abort_cmd(struct nfc_digital_dev
*ddev
)
1815 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
1817 dev_dbg(trf
->dev
, "Abort process initiated\n");
1819 mutex_lock(&trf
->lock
);
1821 switch (trf
->state
) {
1822 case TRF7970A_ST_WAIT_FOR_TX_FIFO
:
1823 case TRF7970A_ST_WAIT_FOR_RX_DATA
:
1824 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT
:
1825 case TRF7970A_ST_WAIT_TO_ISSUE_EOF
:
1826 trf
->aborting
= true;
1828 case TRF7970A_ST_LISTENING
:
1829 trf
->ignore_timeout
= !cancel_delayed_work(&trf
->timeout_work
);
1830 trf7970a_send_err_upstream(trf
, -ECANCELED
);
1831 dev_dbg(trf
->dev
, "Abort process complete\n");
1837 mutex_unlock(&trf
->lock
);
1840 static struct nfc_digital_ops trf7970a_nfc_ops
= {
1841 .in_configure_hw
= trf7970a_in_configure_hw
,
1842 .in_send_cmd
= trf7970a_send_cmd
,
1843 .tg_configure_hw
= trf7970a_tg_configure_hw
,
1844 .tg_send_cmd
= trf7970a_send_cmd
,
1845 .tg_listen
= trf7970a_tg_listen
,
1846 .tg_listen_md
= trf7970a_tg_listen_md
,
1847 .tg_get_rf_tech
= trf7970a_tg_get_rf_tech
,
1848 .switch_rf
= trf7970a_switch_rf
,
1849 .abort_cmd
= trf7970a_abort_cmd
,
1852 static int trf7970a_power_up(struct trf7970a
*trf
)
1856 dev_dbg(trf
->dev
, "Powering up - state: %d\n", trf
->state
);
1858 if (trf
->state
!= TRF7970A_ST_PWR_OFF
)
1861 ret
= regulator_enable(trf
->regulator
);
1863 dev_err(trf
->dev
, "%s - Can't enable VIN: %d\n", __func__
, ret
);
1867 usleep_range(5000, 6000);
1869 if (!(trf
->quirks
& TRF7970A_QUIRK_EN2_MUST_STAY_LOW
)) {
1870 gpio_set_value(trf
->en2_gpio
, 1);
1871 usleep_range(1000, 2000);
1874 gpio_set_value(trf
->en_gpio
, 1);
1876 usleep_range(20000, 21000);
1878 trf
->state
= TRF7970A_ST_RF_OFF
;
1883 static int trf7970a_power_down(struct trf7970a
*trf
)
1887 dev_dbg(trf
->dev
, "Powering down - state: %d\n", trf
->state
);
1889 if (trf
->state
== TRF7970A_ST_PWR_OFF
)
1892 if (trf
->state
!= TRF7970A_ST_RF_OFF
) {
1893 dev_dbg(trf
->dev
, "Can't power down - not RF_OFF state (%d)\n",
1898 gpio_set_value(trf
->en_gpio
, 0);
1899 gpio_set_value(trf
->en2_gpio
, 0);
1901 ret
= regulator_disable(trf
->regulator
);
1903 dev_err(trf
->dev
, "%s - Can't disable VIN: %d\n", __func__
,
1906 trf
->state
= TRF7970A_ST_PWR_OFF
;
1911 static int trf7970a_startup(struct trf7970a
*trf
)
1915 ret
= trf7970a_power_up(trf
);
1919 pm_runtime_set_active(trf
->dev
);
1920 pm_runtime_enable(trf
->dev
);
1921 pm_runtime_mark_last_busy(trf
->dev
);
1926 static void trf7970a_shutdown(struct trf7970a
*trf
)
1928 switch (trf
->state
) {
1929 case TRF7970A_ST_WAIT_FOR_TX_FIFO
:
1930 case TRF7970A_ST_WAIT_FOR_RX_DATA
:
1931 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT
:
1932 case TRF7970A_ST_WAIT_TO_ISSUE_EOF
:
1933 case TRF7970A_ST_LISTENING
:
1934 trf7970a_send_err_upstream(trf
, -ECANCELED
);
1936 case TRF7970A_ST_IDLE
:
1937 case TRF7970A_ST_IDLE_RX_BLOCKED
:
1938 trf7970a_switch_rf_off(trf
);
1944 pm_runtime_disable(trf
->dev
);
1945 pm_runtime_set_suspended(trf
->dev
);
1947 trf7970a_power_down(trf
);
1950 static int trf7970a_get_autosuspend_delay(struct device_node
*np
)
1952 int autosuspend_delay
, ret
;
1954 ret
= of_property_read_u32(np
, "autosuspend-delay", &autosuspend_delay
);
1956 autosuspend_delay
= TRF7970A_AUTOSUSPEND_DELAY
;
1958 return autosuspend_delay
;
1961 static int trf7970a_get_vin_voltage_override(struct device_node
*np
,
1964 return of_property_read_u32(np
, "vin-voltage-override", vin_uvolts
);
1967 static int trf7970a_probe(struct spi_device
*spi
)
1969 struct device_node
*np
= spi
->dev
.of_node
;
1970 struct trf7970a
*trf
;
1971 int uvolts
, autosuspend_delay
, ret
;
1974 dev_err(&spi
->dev
, "No Device Tree entry\n");
1978 trf
= devm_kzalloc(&spi
->dev
, sizeof(*trf
), GFP_KERNEL
);
1982 trf
->state
= TRF7970A_ST_PWR_OFF
;
1983 trf
->dev
= &spi
->dev
;
1986 spi
->mode
= SPI_MODE_1
;
1987 spi
->bits_per_word
= 8;
1989 ret
= spi_setup(spi
);
1991 dev_err(trf
->dev
, "Can't set up SPI Communication\n");
1995 if (of_property_read_bool(np
, "irq-status-read-quirk"))
1996 trf
->quirks
|= TRF7970A_QUIRK_IRQ_STATUS_READ
;
1998 /* There are two enable pins - both must be present */
1999 trf
->en_gpio
= of_get_named_gpio(np
, "ti,enable-gpios", 0);
2000 if (!gpio_is_valid(trf
->en_gpio
)) {
2001 dev_err(trf
->dev
, "No EN GPIO property\n");
2002 return trf
->en_gpio
;
2005 ret
= devm_gpio_request_one(trf
->dev
, trf
->en_gpio
,
2006 GPIOF_DIR_OUT
| GPIOF_INIT_LOW
, "trf7970a EN");
2008 dev_err(trf
->dev
, "Can't request EN GPIO: %d\n", ret
);
2012 trf
->en2_gpio
= of_get_named_gpio(np
, "ti,enable-gpios", 1);
2013 if (!gpio_is_valid(trf
->en2_gpio
)) {
2014 dev_err(trf
->dev
, "No EN2 GPIO property\n");
2015 return trf
->en2_gpio
;
2018 ret
= devm_gpio_request_one(trf
->dev
, trf
->en2_gpio
,
2019 GPIOF_DIR_OUT
| GPIOF_INIT_LOW
, "trf7970a EN2");
2021 dev_err(trf
->dev
, "Can't request EN2 GPIO: %d\n", ret
);
2025 if (of_property_read_bool(np
, "en2-rf-quirk"))
2026 trf
->quirks
|= TRF7970A_QUIRK_EN2_MUST_STAY_LOW
;
2028 ret
= devm_request_threaded_irq(trf
->dev
, spi
->irq
, NULL
,
2029 trf7970a_irq
, IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
2032 dev_err(trf
->dev
, "Can't request IRQ#%d: %d\n", spi
->irq
, ret
);
2036 mutex_init(&trf
->lock
);
2037 INIT_DELAYED_WORK(&trf
->timeout_work
, trf7970a_timeout_work_handler
);
2039 trf
->regulator
= devm_regulator_get(&spi
->dev
, "vin");
2040 if (IS_ERR(trf
->regulator
)) {
2041 ret
= PTR_ERR(trf
->regulator
);
2042 dev_err(trf
->dev
, "Can't get VIN regulator: %d\n", ret
);
2043 goto err_destroy_lock
;
2046 ret
= regulator_enable(trf
->regulator
);
2048 dev_err(trf
->dev
, "Can't enable VIN: %d\n", ret
);
2049 goto err_destroy_lock
;
2052 ret
= trf7970a_get_vin_voltage_override(np
, &uvolts
);
2054 uvolts
= regulator_get_voltage(trf
->regulator
);
2056 if (uvolts
> 4000000)
2057 trf
->chip_status_ctrl
= TRF7970A_CHIP_STATUS_VRS5_3
;
2059 trf
->ddev
= nfc_digital_allocate_device(&trf7970a_nfc_ops
,
2060 TRF7970A_SUPPORTED_PROTOCOLS
,
2061 NFC_DIGITAL_DRV_CAPS_IN_CRC
|
2062 NFC_DIGITAL_DRV_CAPS_TG_CRC
, 0, 0);
2064 dev_err(trf
->dev
, "Can't allocate NFC digital device\n");
2066 goto err_disable_regulator
;
2069 nfc_digital_set_parent_dev(trf
->ddev
, trf
->dev
);
2070 nfc_digital_set_drvdata(trf
->ddev
, trf
);
2071 spi_set_drvdata(spi
, trf
);
2073 autosuspend_delay
= trf7970a_get_autosuspend_delay(np
);
2075 pm_runtime_set_autosuspend_delay(trf
->dev
, autosuspend_delay
);
2076 pm_runtime_use_autosuspend(trf
->dev
);
2078 ret
= trf7970a_startup(trf
);
2082 ret
= nfc_digital_register_device(trf
->ddev
);
2084 dev_err(trf
->dev
, "Can't register NFC digital device: %d\n",
2092 trf7970a_shutdown(trf
);
2094 nfc_digital_free_device(trf
->ddev
);
2095 err_disable_regulator
:
2096 regulator_disable(trf
->regulator
);
2098 mutex_destroy(&trf
->lock
);
2102 static int trf7970a_remove(struct spi_device
*spi
)
2104 struct trf7970a
*trf
= spi_get_drvdata(spi
);
2106 mutex_lock(&trf
->lock
);
2108 trf7970a_shutdown(trf
);
2110 mutex_unlock(&trf
->lock
);
2112 nfc_digital_unregister_device(trf
->ddev
);
2113 nfc_digital_free_device(trf
->ddev
);
2115 regulator_disable(trf
->regulator
);
2117 mutex_destroy(&trf
->lock
);
2122 #ifdef CONFIG_PM_SLEEP
2123 static int trf7970a_suspend(struct device
*dev
)
2125 struct spi_device
*spi
= container_of(dev
, struct spi_device
, dev
);
2126 struct trf7970a
*trf
= spi_get_drvdata(spi
);
2128 dev_dbg(dev
, "Suspend\n");
2130 mutex_lock(&trf
->lock
);
2132 trf7970a_shutdown(trf
);
2134 mutex_unlock(&trf
->lock
);
2139 static int trf7970a_resume(struct device
*dev
)
2141 struct spi_device
*spi
= container_of(dev
, struct spi_device
, dev
);
2142 struct trf7970a
*trf
= spi_get_drvdata(spi
);
2145 dev_dbg(dev
, "Resume\n");
2147 mutex_lock(&trf
->lock
);
2149 ret
= trf7970a_startup(trf
);
2151 mutex_unlock(&trf
->lock
);
2158 static int trf7970a_pm_runtime_suspend(struct device
*dev
)
2160 struct spi_device
*spi
= container_of(dev
, struct spi_device
, dev
);
2161 struct trf7970a
*trf
= spi_get_drvdata(spi
);
2164 dev_dbg(dev
, "Runtime suspend\n");
2166 mutex_lock(&trf
->lock
);
2168 ret
= trf7970a_power_down(trf
);
2170 mutex_unlock(&trf
->lock
);
2175 static int trf7970a_pm_runtime_resume(struct device
*dev
)
2177 struct spi_device
*spi
= container_of(dev
, struct spi_device
, dev
);
2178 struct trf7970a
*trf
= spi_get_drvdata(spi
);
2181 dev_dbg(dev
, "Runtime resume\n");
2183 ret
= trf7970a_power_up(trf
);
2185 pm_runtime_mark_last_busy(dev
);
2191 static const struct dev_pm_ops trf7970a_pm_ops
= {
2192 SET_SYSTEM_SLEEP_PM_OPS(trf7970a_suspend
, trf7970a_resume
)
2193 SET_RUNTIME_PM_OPS(trf7970a_pm_runtime_suspend
,
2194 trf7970a_pm_runtime_resume
, NULL
)
2197 static const struct spi_device_id trf7970a_id_table
[] = {
2201 MODULE_DEVICE_TABLE(spi
, trf7970a_id_table
);
2203 static struct spi_driver trf7970a_spi_driver
= {
2204 .probe
= trf7970a_probe
,
2205 .remove
= trf7970a_remove
,
2206 .id_table
= trf7970a_id_table
,
2209 .owner
= THIS_MODULE
,
2210 .pm
= &trf7970a_pm_ops
,
2214 module_spi_driver(trf7970a_spi_driver
);
2216 MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>");
2217 MODULE_LICENSE("GPL v2");
2218 MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver");