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