GNU Linux-libre 4.19.314-gnu1
[releases.git] / drivers / nfc / trf7970a.c
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/consumer.h>
24 #include <linux/of.h>
25 #include <linux/spi/spi.h>
26 #include <linux/regulator/consumer.h>
27
28 #include <net/nfc/nfc.h>
29 #include <net/nfc/digital.h>
30
31 /* There are 3 ways the host can communicate with the trf7970a:
32  * parallel mode, SPI with Slave Select (SS) mode, and SPI without
33  * SS mode.  The driver only supports the two SPI modes.
34  *
35  * The trf7970a is very timing sensitive and the VIN, EN2, and EN
36  * pins must asserted in that order and with specific delays in between.
37  * The delays used in the driver were provided by TI and have been
38  * confirmed to work with this driver.  There is a bug with the current
39  * version of the trf7970a that requires that EN2 remain low no matter
40  * what.  If it goes high, it will generate an RF field even when in
41  * passive target mode.  TI has indicated that the chip will work okay
42  * when EN2 is left low.  The 'en2-rf-quirk' device tree property
43  * indicates that trf7970a currently being used has the erratum and
44  * that EN2 must be kept low.
45  *
46  * Timeouts are implemented using the delayed workqueue kernel facility.
47  * Timeouts are required so things don't hang when there is no response
48  * from the trf7970a (or tag).  Using this mechanism creates a race with
49  * interrupts, however.  That is, an interrupt and a timeout could occur
50  * closely enough together that one is blocked by the mutex while the other
51  * executes.  When the timeout handler executes first and blocks the
52  * interrupt handler, it will eventually set the state to IDLE so the
53  * interrupt handler will check the state and exit with no harm done.
54  * When the interrupt handler executes first and blocks the timeout handler,
55  * the cancel_delayed_work() call will know that it didn't cancel the
56  * work item (i.e., timeout) and will return zero.  That return code is
57  * used by the timer handler to indicate that it should ignore the timeout
58  * once its unblocked.
59  *
60  * Aborting an active command isn't as simple as it seems because the only
61  * way to abort a command that's already been sent to the tag is so turn
62  * off power to the tag.  If we do that, though, we'd have to go through
63  * the entire anticollision procedure again but the digital layer doesn't
64  * support that.  So, if an abort is received before trf7970a_send_cmd()
65  * has sent the command to the tag, it simply returns -ECANCELED.  If the
66  * command has already been sent to the tag, then the driver continues
67  * normally and recieves the response data (or error) but just before
68  * sending the data upstream, it frees the rx_skb and sends -ECANCELED
69  * upstream instead.  If the command failed, that error will be sent
70  * upstream.
71  *
72  * When recieving data from a tag and the interrupt status register has
73  * only the SRX bit set, it means that all of the data has been received
74  * (once what's in the fifo has been read).  However, depending on timing
75  * an interrupt status with only the SRX bit set may not be recived.  In
76  * those cases, the timeout mechanism is used to wait 20 ms in case more
77  * data arrives.  After 20 ms, it is assumed that all of the data has been
78  * received and the accumulated rx data is sent upstream.  The
79  * 'TRF7970A_ST_WAIT_FOR_RX_DATA_CONT' state is used for this purpose
80  * (i.e., it indicates that some data has been received but we're not sure
81  * if there is more coming so a timeout in this state means all data has
82  * been received and there isn't an error).  The delay is 20 ms since delays
83  * of ~16 ms have been observed during testing.
84  *
85  * When transmitting a frame larger than the FIFO size (127 bytes), the
86  * driver will wait 20 ms for the FIFO to drain past the low-watermark
87  * and generate an interrupt.  The low-watermark set to 32 bytes so the
88  * interrupt should fire after 127 - 32 = 95 bytes have been sent.  At
89  * the lowest possible bit rate (6.62 kbps for 15693), it will take up
90  * to ~14.35 ms so 20 ms is used for the timeout.
91  *
92  * Type 2 write and sector select commands respond with a 4-bit ACK or NACK.
93  * Having only 4 bits in the FIFO won't normally generate an interrupt so
94  * driver enables the '4_bit_RX' bit of the Special Functions register 1
95  * to cause an interrupt in that case.  Leaving that bit for a read command
96  * messes up the data returned so it is only enabled when the framing is
97  * 'NFC_DIGITAL_FRAMING_NFCA_T2T' and the command is not a read command.
98  * Unfortunately, that means that the driver has to peek into tx frames
99  * when the framing is 'NFC_DIGITAL_FRAMING_NFCA_T2T'.  This is done by
100  * the trf7970a_per_cmd_config() routine.
101  *
102  * ISO/IEC 15693 frames specify whether to use single or double sub-carrier
103  * frequencies and whether to use low or high data rates in the flags byte
104  * of the frame.  This means that the driver has to peek at all 15693 frames
105  * to determine what speed to set the communication to.  In addition, write
106  * and lock commands use the OPTION flag to indicate that an EOF must be
107  * sent to the tag before it will send its response.  So the driver has to
108  * examine all frames for that reason too.
109  *
110  * It is unclear how long to wait before sending the EOF.  According to the
111  * Note under Table 1-1 in section 1.6 of
112  * http://www.ti.com/lit/ug/scbu011/scbu011.pdf, that wait should be at least
113  * 10 ms for TI Tag-it HF-I tags; however testing has shown that is not long
114  * enough so 20 ms is used.  So the timer is set to 40 ms - 20 ms to drain
115  * up to 127 bytes in the FIFO at the lowest bit rate plus another 20 ms to
116  * ensure the wait is long enough before sending the EOF.  This seems to work
117  * reliably.
118  */
119
120 #define TRF7970A_SUPPORTED_PROTOCOLS \
121                 (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK |      \
122                  NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_FELICA_MASK | \
123                  NFC_PROTO_ISO15693_MASK | NFC_PROTO_NFC_DEP_MASK)
124
125 #define TRF7970A_AUTOSUSPEND_DELAY              30000   /* 30 seconds */
126 #define TRF7970A_13MHZ_CLOCK_FREQUENCY          13560000
127 #define TRF7970A_27MHZ_CLOCK_FREQUENCY          27120000
128
129 #define TRF7970A_RX_SKB_ALLOC_SIZE              256
130
131 #define TRF7970A_FIFO_SIZE                      127
132
133 /* TX length is 3 nibbles long ==> 4KB - 1 bytes max */
134 #define TRF7970A_TX_MAX                         (4096 - 1)
135
136 #define TRF7970A_WAIT_FOR_TX_IRQ                20
137 #define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT       20
138 #define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT    20
139 #define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF     40
140
141 /* Guard times for various RF technologies (in us) */
142 #define TRF7970A_GUARD_TIME_NFCA                5000
143 #define TRF7970A_GUARD_TIME_NFCB                5000
144 #define TRF7970A_GUARD_TIME_NFCF                20000
145 #define TRF7970A_GUARD_TIME_15693               1000
146
147 /* Quirks */
148 /* Erratum: When reading IRQ Status register on trf7970a, we must issue a
149  * read continuous command for IRQ Status and Collision Position registers.
150  */
151 #define TRF7970A_QUIRK_IRQ_STATUS_READ          BIT(0)
152 #define TRF7970A_QUIRK_EN2_MUST_STAY_LOW        BIT(1)
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(5)
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                *vin_regulator;
431         struct regulator                *vddio_regulator;
432         struct nfc_digital_dev          *ddev;
433         u32                             quirks;
434         bool                            is_initiator;
435         bool                            aborting;
436         struct sk_buff                  *tx_skb;
437         struct sk_buff                  *rx_skb;
438         nfc_digital_cmd_complete_t      cb;
439         void                            *cb_arg;
440         u8                              chip_status_ctrl;
441         u8                              iso_ctrl;
442         u8                              iso_ctrl_tech;
443         u8                              modulator_sys_clk_ctrl;
444         u8                              special_fcn_reg1;
445         u8                              io_ctrl;
446         unsigned int                    guard_time;
447         int                             technology;
448         int                             framing;
449         u8                              md_rf_tech;
450         u8                              tx_cmd;
451         bool                            issue_eof;
452         struct gpio_desc                *en_gpiod;
453         struct gpio_desc                *en2_gpiod;
454         struct mutex                    lock;
455         unsigned int                    timeout;
456         bool                            ignore_timeout;
457         struct delayed_work             timeout_work;
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,
490                               size_t len)
491 {
492         u8 addr = reg | TRF7970A_CMD_BIT_RW | TRF7970A_CMD_BIT_CONTINUOUS;
493         struct spi_transfer t[2];
494         struct spi_message m;
495         int ret;
496
497         dev_dbg(trf->dev, "read_cont(0x%x, %zd)\n", addr, len);
498
499         spi_message_init(&m);
500
501         memset(&t, 0, sizeof(t));
502
503         t[0].tx_buf = &addr;
504         t[0].len = sizeof(addr);
505         spi_message_add_tail(&t[0], &m);
506
507         t[1].rx_buf = buf;
508         t[1].len = len;
509         spi_message_add_tail(&t[1], &m);
510
511         ret = spi_sync(trf->spi, &m);
512         if (ret)
513                 dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr,
514                         ret);
515         return ret;
516 }
517
518 static int trf7970a_write(struct trf7970a *trf, u8 reg, u8 val)
519 {
520         u8 buf[2] = { reg, val };
521         int ret;
522
523         dev_dbg(trf->dev, "write(0x%x): 0x%x\n", reg, val);
524
525         ret = spi_write(trf->spi, buf, 2);
526         if (ret)
527                 dev_err(trf->dev, "%s - write: 0x%x 0x%x, ret: %d\n", __func__,
528                         buf[0], buf[1], ret);
529
530         return ret;
531 }
532
533 static int trf7970a_read_irqstatus(struct trf7970a *trf, u8 *status)
534 {
535         int ret;
536         u8 buf[2];
537         u8 addr;
538
539         addr = TRF7970A_IRQ_STATUS | TRF7970A_CMD_BIT_RW;
540
541         if (trf->quirks & TRF7970A_QUIRK_IRQ_STATUS_READ) {
542                 addr |= TRF7970A_CMD_BIT_CONTINUOUS;
543                 ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2);
544         } else {
545                 ret = spi_write_then_read(trf->spi, &addr, 1, buf, 1);
546         }
547
548         if (ret)
549                 dev_err(trf->dev, "%s - irqstatus: Status read failed: %d\n",
550                         __func__, ret);
551         else
552                 *status = buf[0];
553
554         return ret;
555 }
556
557 static int trf7970a_read_target_proto(struct trf7970a *trf, u8 *target_proto)
558 {
559         int ret;
560         u8 buf[2];
561         u8 addr;
562
563         addr = TRF79070A_NFC_TARGET_PROTOCOL | TRF7970A_CMD_BIT_RW |
564                TRF7970A_CMD_BIT_CONTINUOUS;
565
566         ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2);
567         if (ret)
568                 dev_err(trf->dev, "%s - target_proto: Read failed: %d\n",
569                         __func__, ret);
570         else
571                 *target_proto = buf[0];
572
573         return ret;
574 }
575
576 static int trf7970a_mode_detect(struct trf7970a *trf, u8 *rf_tech)
577 {
578         int ret;
579         u8 target_proto, tech;
580
581         ret = trf7970a_read_target_proto(trf, &target_proto);
582         if (ret)
583                 return ret;
584
585         switch (target_proto) {
586         case TRF79070A_NFC_TARGET_PROTOCOL_106A:
587                 tech = NFC_DIGITAL_RF_TECH_106A;
588                 break;
589         case TRF79070A_NFC_TARGET_PROTOCOL_106B:
590                 tech = NFC_DIGITAL_RF_TECH_106B;
591                 break;
592         case TRF79070A_NFC_TARGET_PROTOCOL_212F:
593                 tech = NFC_DIGITAL_RF_TECH_212F;
594                 break;
595         case TRF79070A_NFC_TARGET_PROTOCOL_424F:
596                 tech = NFC_DIGITAL_RF_TECH_424F;
597                 break;
598         default:
599                 dev_dbg(trf->dev, "%s - mode_detect: target_proto: 0x%x\n",
600                         __func__, target_proto);
601                 return -EIO;
602         }
603
604         *rf_tech = tech;
605
606         return ret;
607 }
608
609 static void trf7970a_send_upstream(struct trf7970a *trf)
610 {
611         dev_kfree_skb_any(trf->tx_skb);
612         trf->tx_skb = NULL;
613
614         if (trf->rx_skb && !IS_ERR(trf->rx_skb) && !trf->aborting)
615                 print_hex_dump_debug("trf7970a rx data: ", DUMP_PREFIX_NONE,
616                                      16, 1, trf->rx_skb->data, trf->rx_skb->len,
617                                      false);
618
619         trf->state = TRF7970A_ST_IDLE;
620
621         if (trf->aborting) {
622                 dev_dbg(trf->dev, "Abort process complete\n");
623
624                 if (!IS_ERR(trf->rx_skb)) {
625                         kfree_skb(trf->rx_skb);
626                         trf->rx_skb = ERR_PTR(-ECANCELED);
627                 }
628
629                 trf->aborting = false;
630         }
631
632         trf->cb(trf->ddev, trf->cb_arg, trf->rx_skb);
633
634         trf->rx_skb = NULL;
635 }
636
637 static void trf7970a_send_err_upstream(struct trf7970a *trf, int errno)
638 {
639         dev_dbg(trf->dev, "Error - state: %d, errno: %d\n", trf->state, errno);
640
641         cancel_delayed_work(&trf->timeout_work);
642
643         kfree_skb(trf->rx_skb);
644         trf->rx_skb = ERR_PTR(errno);
645
646         trf7970a_send_upstream(trf);
647 }
648
649 static int trf7970a_transmit(struct trf7970a *trf, struct sk_buff *skb,
650                              unsigned int len, u8 *prefix,
651                              unsigned int prefix_len)
652 {
653         struct spi_transfer t[2];
654         struct spi_message m;
655         unsigned int timeout;
656         int ret;
657
658         print_hex_dump_debug("trf7970a tx data: ", DUMP_PREFIX_NONE,
659                              16, 1, skb->data, len, false);
660
661         spi_message_init(&m);
662
663         memset(&t, 0, sizeof(t));
664
665         t[0].tx_buf = prefix;
666         t[0].len = prefix_len;
667         spi_message_add_tail(&t[0], &m);
668
669         t[1].tx_buf = skb->data;
670         t[1].len = len;
671         spi_message_add_tail(&t[1], &m);
672
673         ret = spi_sync(trf->spi, &m);
674         if (ret) {
675                 dev_err(trf->dev, "%s - Can't send tx data: %d\n", __func__,
676                         ret);
677                 return ret;
678         }
679
680         skb_pull(skb, len);
681
682         if (skb->len > 0) {
683                 trf->state = TRF7970A_ST_WAIT_FOR_TX_FIFO;
684                 timeout = TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT;
685         } else {
686                 if (trf->issue_eof) {
687                         trf->state = TRF7970A_ST_WAIT_TO_ISSUE_EOF;
688                         timeout = TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF;
689                 } else {
690                         trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA;
691
692                         if (!trf->timeout)
693                                 timeout = TRF7970A_WAIT_FOR_TX_IRQ;
694                         else
695                                 timeout = trf->timeout;
696                 }
697         }
698
699         dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", timeout,
700                 trf->state);
701
702         schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout));
703
704         return 0;
705 }
706
707 static void trf7970a_fill_fifo(struct trf7970a *trf)
708 {
709         struct sk_buff *skb = trf->tx_skb;
710         unsigned int len;
711         int ret;
712         u8 fifo_bytes;
713         u8 prefix;
714
715         ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
716         if (ret) {
717                 trf7970a_send_err_upstream(trf, ret);
718                 return;
719         }
720
721         dev_dbg(trf->dev, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes);
722
723         fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
724
725         /* Calculate how much more data can be written to the fifo */
726         len = TRF7970A_FIFO_SIZE - fifo_bytes;
727         if (!len) {
728                 schedule_delayed_work(&trf->timeout_work,
729                         msecs_to_jiffies(TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT));
730                 return;
731         }
732
733         len = min(skb->len, len);
734
735         prefix = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_FIFO_IO_REGISTER;
736
737         ret = trf7970a_transmit(trf, skb, len, &prefix, sizeof(prefix));
738         if (ret)
739                 trf7970a_send_err_upstream(trf, ret);
740 }
741
742 static void trf7970a_drain_fifo(struct trf7970a *trf, u8 status)
743 {
744         struct sk_buff *skb = trf->rx_skb;
745         int ret;
746         u8 fifo_bytes;
747
748         if (status & TRF7970A_IRQ_STATUS_ERROR) {
749                 trf7970a_send_err_upstream(trf, -EIO);
750                 return;
751         }
752
753         ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
754         if (ret) {
755                 trf7970a_send_err_upstream(trf, ret);
756                 return;
757         }
758
759         dev_dbg(trf->dev, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes);
760
761         fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
762
763         if (!fifo_bytes)
764                 goto no_rx_data;
765
766         if (fifo_bytes > skb_tailroom(skb)) {
767                 skb = skb_copy_expand(skb, skb_headroom(skb),
768                                       max_t(int, fifo_bytes,
769                                             TRF7970A_RX_SKB_ALLOC_SIZE),
770                                       GFP_KERNEL);
771                 if (!skb) {
772                         trf7970a_send_err_upstream(trf, -ENOMEM);
773                         return;
774                 }
775
776                 kfree_skb(trf->rx_skb);
777                 trf->rx_skb = skb;
778         }
779
780         ret = trf7970a_read_cont(trf, TRF7970A_FIFO_IO_REGISTER,
781                                  skb_put(skb, fifo_bytes), fifo_bytes);
782         if (ret) {
783                 trf7970a_send_err_upstream(trf, ret);
784                 return;
785         }
786
787         /* If received Type 2 ACK/NACK, shift right 4 bits and pass up */
788         if ((trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T) && (skb->len == 1) &&
789             (trf->special_fcn_reg1 == TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX)) {
790                 skb->data[0] >>= 4;
791                 status = TRF7970A_IRQ_STATUS_SRX;
792         } else {
793                 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA_CONT;
794
795                 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
796                 if (ret) {
797                         trf7970a_send_err_upstream(trf, ret);
798                         return;
799                 }
800
801                 fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
802
803                 /* If there are bytes in the FIFO, set status to '0' so
804                  * the if stmt below doesn't fire and the driver will wait
805                  * for the trf7970a to generate another RX interrupt.
806                  */
807                 if (fifo_bytes)
808                         status = 0;
809         }
810
811 no_rx_data:
812         if (status == TRF7970A_IRQ_STATUS_SRX) {        /* Receive complete */
813                 trf7970a_send_upstream(trf);
814                 return;
815         }
816
817         dev_dbg(trf->dev, "Setting timeout for %d ms\n",
818                 TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT);
819
820         schedule_delayed_work(&trf->timeout_work,
821                            msecs_to_jiffies(TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT));
822 }
823
824 static irqreturn_t trf7970a_irq(int irq, void *dev_id)
825 {
826         struct trf7970a *trf = dev_id;
827         int ret;
828         u8 status, fifo_bytes, iso_ctrl;
829
830         mutex_lock(&trf->lock);
831
832         if (trf->state == TRF7970A_ST_RF_OFF) {
833                 mutex_unlock(&trf->lock);
834                 return IRQ_NONE;
835         }
836
837         ret = trf7970a_read_irqstatus(trf, &status);
838         if (ret) {
839                 mutex_unlock(&trf->lock);
840                 return IRQ_NONE;
841         }
842
843         dev_dbg(trf->dev, "IRQ - state: %d, status: 0x%x\n", trf->state,
844                 status);
845
846         if (!status) {
847                 mutex_unlock(&trf->lock);
848                 return IRQ_NONE;
849         }
850
851         switch (trf->state) {
852         case TRF7970A_ST_IDLE:
853         case TRF7970A_ST_IDLE_RX_BLOCKED:
854                 /* If initiator and getting interrupts caused by RF noise,
855                  * turn off the receiver to avoid unnecessary interrupts.
856                  * It will be turned back on in trf7970a_send_cmd() when
857                  * the next command is issued.
858                  */
859                 if (trf->is_initiator && (status & TRF7970A_IRQ_STATUS_ERROR)) {
860                         trf7970a_cmd(trf, TRF7970A_CMD_BLOCK_RX);
861                         trf->state = TRF7970A_ST_IDLE_RX_BLOCKED;
862                 }
863
864                 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
865                 break;
866         case TRF7970A_ST_WAIT_FOR_TX_FIFO:
867                 if (status & TRF7970A_IRQ_STATUS_TX) {
868                         trf->ignore_timeout =
869                             !cancel_delayed_work(&trf->timeout_work);
870                         trf7970a_fill_fifo(trf);
871                 } else {
872                         trf7970a_send_err_upstream(trf, -EIO);
873                 }
874                 break;
875         case TRF7970A_ST_WAIT_FOR_RX_DATA:
876         case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
877                 if (status & TRF7970A_IRQ_STATUS_SRX) {
878                         trf->ignore_timeout =
879                             !cancel_delayed_work(&trf->timeout_work);
880                         trf7970a_drain_fifo(trf, status);
881                 } else if (status & TRF7970A_IRQ_STATUS_FIFO) {
882                         ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS,
883                                             &fifo_bytes);
884
885                         fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
886
887                         if (ret)
888                                 trf7970a_send_err_upstream(trf, ret);
889                         else if (!fifo_bytes)
890                                 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
891                 } else if ((status == TRF7970A_IRQ_STATUS_TX) ||
892                            (!trf->is_initiator &&
893                             (status == (TRF7970A_IRQ_STATUS_TX |
894                                         TRF7970A_IRQ_STATUS_NFC_RF)))) {
895                         trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
896
897                         if (!trf->timeout) {
898                                 trf->ignore_timeout =
899                                     !cancel_delayed_work(&trf->timeout_work);
900                                 trf->rx_skb = ERR_PTR(0);
901                                 trf7970a_send_upstream(trf);
902                                 break;
903                         }
904
905                         if (trf->is_initiator)
906                                 break;
907
908                         iso_ctrl = trf->iso_ctrl;
909
910                         switch (trf->framing) {
911                         case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
912                                 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
913                                 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
914                                 trf->iso_ctrl = 0xff; /* Force ISO_CTRL write */
915                                 break;
916                         case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
917                                 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
918                                 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
919                                 trf->iso_ctrl = 0xff; /* Force ISO_CTRL write */
920                                 break;
921                         case NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE:
922                                 ret = trf7970a_write(trf,
923                                          TRF7970A_SPECIAL_FCN_REG1,
924                                          TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL);
925                                 if (ret)
926                                         goto err_unlock_exit;
927
928                                 trf->special_fcn_reg1 =
929                                     TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL;
930                                 break;
931                         default:
932                                 break;
933                         }
934
935                         if (iso_ctrl != trf->iso_ctrl) {
936                                 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL,
937                                                      iso_ctrl);
938                                 if (ret)
939                                         goto err_unlock_exit;
940
941                                 trf->iso_ctrl = iso_ctrl;
942                         }
943                 } else {
944                         trf7970a_send_err_upstream(trf, -EIO);
945                 }
946                 break;
947         case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
948                 if (status != TRF7970A_IRQ_STATUS_TX)
949                         trf7970a_send_err_upstream(trf, -EIO);
950                 break;
951         case TRF7970A_ST_LISTENING:
952                 if (status & TRF7970A_IRQ_STATUS_SRX) {
953                         trf->ignore_timeout =
954                             !cancel_delayed_work(&trf->timeout_work);
955                         trf7970a_drain_fifo(trf, status);
956                 } else if (!(status & TRF7970A_IRQ_STATUS_NFC_RF)) {
957                         trf7970a_send_err_upstream(trf, -EIO);
958                 }
959                 break;
960         case TRF7970A_ST_LISTENING_MD:
961                 if (status & TRF7970A_IRQ_STATUS_SRX) {
962                         trf->ignore_timeout =
963                             !cancel_delayed_work(&trf->timeout_work);
964
965                         ret = trf7970a_mode_detect(trf, &trf->md_rf_tech);
966                         if (ret) {
967                                 trf7970a_send_err_upstream(trf, ret);
968                         } else {
969                                 trf->state = TRF7970A_ST_LISTENING;
970                                 trf7970a_drain_fifo(trf, status);
971                         }
972                 } else if (!(status & TRF7970A_IRQ_STATUS_NFC_RF)) {
973                         trf7970a_send_err_upstream(trf, -EIO);
974                 }
975                 break;
976         default:
977                 dev_err(trf->dev, "%s - Driver in invalid state: %d\n",
978                         __func__, trf->state);
979         }
980
981 err_unlock_exit:
982         mutex_unlock(&trf->lock);
983         return IRQ_HANDLED;
984 }
985
986 static void trf7970a_issue_eof(struct trf7970a *trf)
987 {
988         int ret;
989
990         dev_dbg(trf->dev, "Issuing EOF\n");
991
992         ret = trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
993         if (ret)
994                 trf7970a_send_err_upstream(trf, ret);
995
996         ret = trf7970a_cmd(trf, TRF7970A_CMD_EOF);
997         if (ret)
998                 trf7970a_send_err_upstream(trf, ret);
999
1000         trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA;
1001
1002         dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n",
1003                 trf->timeout, trf->state);
1004
1005         schedule_delayed_work(&trf->timeout_work,
1006                               msecs_to_jiffies(trf->timeout));
1007 }
1008
1009 static void trf7970a_timeout_work_handler(struct work_struct *work)
1010 {
1011         struct trf7970a *trf = container_of(work, struct trf7970a,
1012                                             timeout_work.work);
1013
1014         dev_dbg(trf->dev, "Timeout - state: %d, ignore_timeout: %d\n",
1015                 trf->state, trf->ignore_timeout);
1016
1017         mutex_lock(&trf->lock);
1018
1019         if (trf->ignore_timeout)
1020                 trf->ignore_timeout = false;
1021         else if (trf->state == TRF7970A_ST_WAIT_FOR_RX_DATA_CONT)
1022                 trf7970a_drain_fifo(trf, TRF7970A_IRQ_STATUS_SRX);
1023         else if (trf->state == TRF7970A_ST_WAIT_TO_ISSUE_EOF)
1024                 trf7970a_issue_eof(trf);
1025         else
1026                 trf7970a_send_err_upstream(trf, -ETIMEDOUT);
1027
1028         mutex_unlock(&trf->lock);
1029 }
1030
1031 static int trf7970a_init(struct trf7970a *trf)
1032 {
1033         int ret;
1034
1035         dev_dbg(trf->dev, "Initializing device - state: %d\n", trf->state);
1036
1037         ret = trf7970a_cmd(trf, TRF7970A_CMD_SOFT_INIT);
1038         if (ret)
1039                 goto err_out;
1040
1041         ret = trf7970a_cmd(trf, TRF7970A_CMD_IDLE);
1042         if (ret)
1043                 goto err_out;
1044
1045         ret = trf7970a_write(trf, TRF7970A_REG_IO_CTRL,
1046                              trf->io_ctrl | TRF7970A_REG_IO_CTRL_VRS(0x1));
1047         if (ret)
1048                 goto err_out;
1049
1050         ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
1051         if (ret)
1052                 goto err_out;
1053
1054         usleep_range(1000, 2000);
1055
1056         trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
1057
1058         ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
1059                              trf->modulator_sys_clk_ctrl);
1060         if (ret)
1061                 goto err_out;
1062
1063         ret = trf7970a_write(trf, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS,
1064                              TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 |
1065                              TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32);
1066         if (ret)
1067                 goto err_out;
1068
1069         ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, 0);
1070         if (ret)
1071                 goto err_out;
1072
1073         trf->special_fcn_reg1 = 0;
1074
1075         trf->iso_ctrl = 0xff;
1076         return 0;
1077
1078 err_out:
1079         dev_dbg(trf->dev, "Couldn't init device: %d\n", ret);
1080         return ret;
1081 }
1082
1083 static void trf7970a_switch_rf_off(struct trf7970a *trf)
1084 {
1085         if ((trf->state == TRF7970A_ST_PWR_OFF) ||
1086             (trf->state == TRF7970A_ST_RF_OFF))
1087                 return;
1088
1089         dev_dbg(trf->dev, "Switching rf off\n");
1090
1091         trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
1092
1093         trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, trf->chip_status_ctrl);
1094
1095         trf->aborting = false;
1096         trf->state = TRF7970A_ST_RF_OFF;
1097
1098         pm_runtime_mark_last_busy(trf->dev);
1099         pm_runtime_put_autosuspend(trf->dev);
1100 }
1101
1102 static int trf7970a_switch_rf_on(struct trf7970a *trf)
1103 {
1104         int ret;
1105
1106         dev_dbg(trf->dev, "Switching rf on\n");
1107
1108         pm_runtime_get_sync(trf->dev);
1109
1110         if (trf->state != TRF7970A_ST_RF_OFF) { /* Power on, RF off */
1111                 dev_err(trf->dev, "%s - Incorrect state: %d\n", __func__,
1112                         trf->state);
1113                 return -EINVAL;
1114         }
1115
1116         ret = trf7970a_init(trf);
1117         if (ret) {
1118                 dev_err(trf->dev, "%s - Can't initialize: %d\n", __func__, ret);
1119                 return ret;
1120         }
1121
1122         trf->state = TRF7970A_ST_IDLE;
1123
1124         return 0;
1125 }
1126
1127 static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
1128 {
1129         struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1130         int ret = 0;
1131
1132         dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on);
1133
1134         mutex_lock(&trf->lock);
1135
1136         if (on) {
1137                 switch (trf->state) {
1138                 case TRF7970A_ST_PWR_OFF:
1139                 case TRF7970A_ST_RF_OFF:
1140                         ret = trf7970a_switch_rf_on(trf);
1141                         break;
1142                 case TRF7970A_ST_IDLE:
1143                 case TRF7970A_ST_IDLE_RX_BLOCKED:
1144                         break;
1145                 default:
1146                         dev_err(trf->dev, "%s - Invalid request: %d %d\n",
1147                                 __func__, trf->state, on);
1148                         trf7970a_switch_rf_off(trf);
1149                         ret = -EINVAL;
1150                 }
1151         } else {
1152                 switch (trf->state) {
1153                 case TRF7970A_ST_PWR_OFF:
1154                 case TRF7970A_ST_RF_OFF:
1155                         break;
1156                 default:
1157                         dev_err(trf->dev, "%s - Invalid request: %d %d\n",
1158                                 __func__, trf->state, on);
1159                         ret = -EINVAL;
1160                         /* FALLTHROUGH */
1161                 case TRF7970A_ST_IDLE:
1162                 case TRF7970A_ST_IDLE_RX_BLOCKED:
1163                 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1164                 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1165                         trf7970a_switch_rf_off(trf);
1166                 }
1167         }
1168
1169         mutex_unlock(&trf->lock);
1170         return ret;
1171 }
1172
1173 static int trf7970a_in_config_rf_tech(struct trf7970a *trf, int tech)
1174 {
1175         int ret = 0;
1176
1177         dev_dbg(trf->dev, "rf technology: %d\n", tech);
1178
1179         switch (tech) {
1180         case NFC_DIGITAL_RF_TECH_106A:
1181                 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443A_106;
1182                 trf->modulator_sys_clk_ctrl =
1183                     (trf->modulator_sys_clk_ctrl & 0xf8) |
1184                     TRF7970A_MODULATOR_DEPTH_OOK;
1185                 trf->guard_time = TRF7970A_GUARD_TIME_NFCA;
1186                 break;
1187         case NFC_DIGITAL_RF_TECH_106B:
1188                 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443B_106;
1189                 trf->modulator_sys_clk_ctrl =
1190                     (trf->modulator_sys_clk_ctrl & 0xf8) |
1191                     TRF7970A_MODULATOR_DEPTH_ASK10;
1192                 trf->guard_time = TRF7970A_GUARD_TIME_NFCB;
1193                 break;
1194         case NFC_DIGITAL_RF_TECH_212F:
1195                 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_212;
1196                 trf->modulator_sys_clk_ctrl =
1197                     (trf->modulator_sys_clk_ctrl & 0xf8) |
1198                     TRF7970A_MODULATOR_DEPTH_ASK10;
1199                 trf->guard_time = TRF7970A_GUARD_TIME_NFCF;
1200                 break;
1201         case NFC_DIGITAL_RF_TECH_424F:
1202                 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_424;
1203                 trf->modulator_sys_clk_ctrl =
1204                     (trf->modulator_sys_clk_ctrl & 0xf8) |
1205                     TRF7970A_MODULATOR_DEPTH_ASK10;
1206                 trf->guard_time = TRF7970A_GUARD_TIME_NFCF;
1207                 break;
1208         case NFC_DIGITAL_RF_TECH_ISO15693:
1209                 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
1210                 trf->modulator_sys_clk_ctrl =
1211                     (trf->modulator_sys_clk_ctrl & 0xf8) |
1212                     TRF7970A_MODULATOR_DEPTH_OOK;
1213                 trf->guard_time = TRF7970A_GUARD_TIME_15693;
1214                 break;
1215         default:
1216                 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
1217                 return -EINVAL;
1218         }
1219
1220         trf->technology = tech;
1221
1222         /* If in initiator mode and not changing the RF tech due to a
1223          * PSL sequence (indicated by 'trf->iso_ctrl == 0xff' from
1224          * trf7970a_init()), clear the NFC Target Detection Level register
1225          * due to erratum.
1226          */
1227         if (trf->iso_ctrl == 0xff)
1228                 ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
1229
1230         return ret;
1231 }
1232
1233 static int trf7970a_is_rf_field(struct trf7970a *trf, bool *is_rf_field)
1234 {
1235         int ret;
1236         u8 rssi;
1237
1238         ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1239                              trf->chip_status_ctrl |
1240                              TRF7970A_CHIP_STATUS_REC_ON);
1241         if (ret)
1242                 return ret;
1243
1244         ret = trf7970a_cmd(trf, TRF7970A_CMD_TEST_EXT_RF);
1245         if (ret)
1246                 return ret;
1247
1248         usleep_range(50, 60);
1249
1250         ret = trf7970a_read(trf, TRF7970A_RSSI_OSC_STATUS, &rssi);
1251         if (ret)
1252                 return ret;
1253
1254         ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1255                              trf->chip_status_ctrl);
1256         if (ret)
1257                 return ret;
1258
1259         if (rssi & TRF7970A_RSSI_OSC_STATUS_RSSI_MASK)
1260                 *is_rf_field = true;
1261         else
1262                 *is_rf_field = false;
1263
1264         return 0;
1265 }
1266
1267 static int trf7970a_in_config_framing(struct trf7970a *trf, int framing)
1268 {
1269         u8 iso_ctrl = trf->iso_ctrl_tech;
1270         bool is_rf_field = false;
1271         int ret;
1272
1273         dev_dbg(trf->dev, "framing: %d\n", framing);
1274
1275         switch (framing) {
1276         case NFC_DIGITAL_FRAMING_NFCA_SHORT:
1277         case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
1278                 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
1279                 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
1280                 break;
1281         case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
1282         case NFC_DIGITAL_FRAMING_NFCA_T4T:
1283         case NFC_DIGITAL_FRAMING_NFCB:
1284         case NFC_DIGITAL_FRAMING_NFCB_T4T:
1285         case NFC_DIGITAL_FRAMING_NFCF:
1286         case NFC_DIGITAL_FRAMING_NFCF_T3T:
1287         case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY:
1288         case NFC_DIGITAL_FRAMING_ISO15693_T5T:
1289         case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
1290         case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP:
1291                 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1292                 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
1293                 break;
1294         case NFC_DIGITAL_FRAMING_NFCA_T2T:
1295                 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1296                 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
1297                 break;
1298         default:
1299                 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
1300                 return -EINVAL;
1301         }
1302
1303         trf->framing = framing;
1304
1305         if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
1306                 ret = trf7970a_is_rf_field(trf, &is_rf_field);
1307                 if (ret)
1308                         return ret;
1309
1310                 if (is_rf_field)
1311                         return -EBUSY;
1312         }
1313
1314         if (iso_ctrl != trf->iso_ctrl) {
1315                 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1316                 if (ret)
1317                         return ret;
1318
1319                 trf->iso_ctrl = iso_ctrl;
1320
1321                 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
1322                                      trf->modulator_sys_clk_ctrl);
1323                 if (ret)
1324                         return ret;
1325         }
1326
1327         if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
1328                 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1329                                      trf->chip_status_ctrl |
1330                                      TRF7970A_CHIP_STATUS_RF_ON);
1331                 if (ret)
1332                         return ret;
1333
1334                 trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
1335
1336                 usleep_range(trf->guard_time, trf->guard_time + 1000);
1337         }
1338
1339         return 0;
1340 }
1341
1342 static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type,
1343                                     int param)
1344 {
1345         struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1346         int ret;
1347
1348         dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
1349
1350         mutex_lock(&trf->lock);
1351
1352         trf->is_initiator = true;
1353
1354         if ((trf->state == TRF7970A_ST_PWR_OFF) ||
1355             (trf->state == TRF7970A_ST_RF_OFF)) {
1356                 ret = trf7970a_switch_rf_on(trf);
1357                 if (ret)
1358                         goto err_unlock;
1359         }
1360
1361         switch (type) {
1362         case NFC_DIGITAL_CONFIG_RF_TECH:
1363                 ret = trf7970a_in_config_rf_tech(trf, param);
1364                 break;
1365         case NFC_DIGITAL_CONFIG_FRAMING:
1366                 ret = trf7970a_in_config_framing(trf, param);
1367                 break;
1368         default:
1369                 dev_dbg(trf->dev, "Unknown type: %d\n", type);
1370                 ret = -EINVAL;
1371         }
1372
1373 err_unlock:
1374         mutex_unlock(&trf->lock);
1375         return ret;
1376 }
1377
1378 static int trf7970a_is_iso15693_write_or_lock(u8 cmd)
1379 {
1380         switch (cmd) {
1381         case ISO15693_CMD_WRITE_SINGLE_BLOCK:
1382         case ISO15693_CMD_LOCK_BLOCK:
1383         case ISO15693_CMD_WRITE_MULTIPLE_BLOCK:
1384         case ISO15693_CMD_WRITE_AFI:
1385         case ISO15693_CMD_LOCK_AFI:
1386         case ISO15693_CMD_WRITE_DSFID:
1387         case ISO15693_CMD_LOCK_DSFID:
1388                 return 1;
1389                 break;
1390         default:
1391                 return 0;
1392         }
1393 }
1394
1395 static int trf7970a_per_cmd_config(struct trf7970a *trf, struct sk_buff *skb)
1396 {
1397         u8 *req = skb->data;
1398         u8 special_fcn_reg1, iso_ctrl;
1399         int ret;
1400
1401         trf->issue_eof = false;
1402
1403         /* When issuing Type 2 read command, make sure the '4_bit_RX' bit in
1404          * special functions register 1 is cleared; otherwise, its a write or
1405          * sector select command and '4_bit_RX' must be set.
1406          *
1407          * When issuing an ISO 15693 command, inspect the flags byte to see
1408          * what speed to use.  Also, remember if the OPTION flag is set on
1409          * a Type 5 write or lock command so the driver will know that it
1410          * has to send an EOF in order to get a response.
1411          */
1412         if ((trf->technology == NFC_DIGITAL_RF_TECH_106A) &&
1413             (trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T)) {
1414                 if (req[0] == NFC_T2T_CMD_READ)
1415                         special_fcn_reg1 = 0;
1416                 else
1417                         special_fcn_reg1 = TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX;
1418
1419                 if (special_fcn_reg1 != trf->special_fcn_reg1) {
1420                         ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1,
1421                                              special_fcn_reg1);
1422                         if (ret)
1423                                 return ret;
1424
1425                         trf->special_fcn_reg1 = special_fcn_reg1;
1426                 }
1427         } else if (trf->technology == NFC_DIGITAL_RF_TECH_ISO15693) {
1428                 iso_ctrl = trf->iso_ctrl & ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK;
1429
1430                 switch (req[0] & ISO15693_REQ_FLAG_SPEED_MASK) {
1431                 case 0x00:
1432                         iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_662;
1433                         break;
1434                 case ISO15693_REQ_FLAG_SUB_CARRIER:
1435                         iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a;
1436                         break;
1437                 case ISO15693_REQ_FLAG_DATA_RATE:
1438                         iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
1439                         break;
1440                 case (ISO15693_REQ_FLAG_SUB_CARRIER |
1441                       ISO15693_REQ_FLAG_DATA_RATE):
1442                         iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669;
1443                         break;
1444                 }
1445
1446                 if (iso_ctrl != trf->iso_ctrl) {
1447                         ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1448                         if (ret)
1449                                 return ret;
1450
1451                         trf->iso_ctrl = iso_ctrl;
1452                 }
1453
1454                 if ((trf->framing == NFC_DIGITAL_FRAMING_ISO15693_T5T) &&
1455                     trf7970a_is_iso15693_write_or_lock(req[1]) &&
1456                     (req[0] & ISO15693_REQ_FLAG_OPTION))
1457                         trf->issue_eof = true;
1458         }
1459
1460         return 0;
1461 }
1462
1463 static int trf7970a_send_cmd(struct nfc_digital_dev *ddev,
1464                              struct sk_buff *skb, u16 timeout,
1465                              nfc_digital_cmd_complete_t cb, void *arg)
1466 {
1467         struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1468         u8 prefix[5];
1469         unsigned int len;
1470         int ret;
1471         u8 status;
1472
1473         dev_dbg(trf->dev, "New request - state: %d, timeout: %d ms, len: %d\n",
1474                 trf->state, timeout, skb->len);
1475
1476         if (skb->len > TRF7970A_TX_MAX)
1477                 return -EINVAL;
1478
1479         mutex_lock(&trf->lock);
1480
1481         if ((trf->state != TRF7970A_ST_IDLE) &&
1482             (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) {
1483                 dev_err(trf->dev, "%s - Bogus state: %d\n", __func__,
1484                         trf->state);
1485                 ret = -EIO;
1486                 goto out_err;
1487         }
1488
1489         if (trf->aborting) {
1490                 dev_dbg(trf->dev, "Abort process complete\n");
1491                 trf->aborting = false;
1492                 ret = -ECANCELED;
1493                 goto out_err;
1494         }
1495
1496         if (timeout) {
1497                 trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE,
1498                                                  GFP_KERNEL);
1499                 if (!trf->rx_skb) {
1500                         dev_dbg(trf->dev, "Can't alloc rx_skb\n");
1501                         ret = -ENOMEM;
1502                         goto out_err;
1503                 }
1504         }
1505
1506         if (trf->state == TRF7970A_ST_IDLE_RX_BLOCKED) {
1507                 ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX);
1508                 if (ret)
1509                         goto out_err;
1510
1511                 trf->state = TRF7970A_ST_IDLE;
1512         }
1513
1514         if (trf->is_initiator) {
1515                 ret = trf7970a_per_cmd_config(trf, skb);
1516                 if (ret)
1517                         goto out_err;
1518         }
1519
1520         trf->ddev = ddev;
1521         trf->tx_skb = skb;
1522         trf->cb = cb;
1523         trf->cb_arg = arg;
1524         trf->timeout = timeout;
1525         trf->ignore_timeout = false;
1526
1527         len = skb->len;
1528
1529         /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
1530          * on what the current framing is, the address of the TX length byte 1
1531          * register (0x1d), and the 2 byte length of the data to be transmitted.
1532          * That totals 5 bytes.
1533          */
1534         prefix[0] = TRF7970A_CMD_BIT_CTRL |
1535             TRF7970A_CMD_BIT_OPCODE(TRF7970A_CMD_FIFO_RESET);
1536         prefix[1] = TRF7970A_CMD_BIT_CTRL |
1537             TRF7970A_CMD_BIT_OPCODE(trf->tx_cmd);
1538         prefix[2] = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_TX_LENGTH_BYTE1;
1539
1540         if (trf->framing == NFC_DIGITAL_FRAMING_NFCA_SHORT) {
1541                 prefix[3] = 0x00;
1542                 prefix[4] = 0x0f;       /* 7 bits */
1543         } else {
1544                 prefix[3] = (len & 0xf00) >> 4;
1545                 prefix[3] |= ((len & 0xf0) >> 4);
1546                 prefix[4] = ((len & 0x0f) << 4);
1547         }
1548
1549         len = min_t(int, skb->len, TRF7970A_FIFO_SIZE);
1550
1551         /* Clear possible spurious interrupt */
1552         ret = trf7970a_read_irqstatus(trf, &status);
1553         if (ret)
1554                 goto out_err;
1555
1556         ret = trf7970a_transmit(trf, skb, len, prefix, sizeof(prefix));
1557         if (ret) {
1558                 kfree_skb(trf->rx_skb);
1559                 trf->rx_skb = NULL;
1560         }
1561
1562 out_err:
1563         mutex_unlock(&trf->lock);
1564         return ret;
1565 }
1566
1567 static int trf7970a_tg_config_rf_tech(struct trf7970a *trf, int tech)
1568 {
1569         int ret = 0;
1570
1571         dev_dbg(trf->dev, "rf technology: %d\n", tech);
1572
1573         switch (tech) {
1574         case NFC_DIGITAL_RF_TECH_106A:
1575                 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
1576                     TRF7970A_ISO_CTRL_NFC_CE | TRF7970A_ISO_CTRL_NFC_CE_14443A;
1577                 trf->modulator_sys_clk_ctrl =
1578                     (trf->modulator_sys_clk_ctrl & 0xf8) |
1579                     TRF7970A_MODULATOR_DEPTH_OOK;
1580                 break;
1581         case NFC_DIGITAL_RF_TECH_212F:
1582                 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
1583                     TRF7970A_ISO_CTRL_NFC_NFCF_212;
1584                 trf->modulator_sys_clk_ctrl =
1585                     (trf->modulator_sys_clk_ctrl & 0xf8) |
1586                     TRF7970A_MODULATOR_DEPTH_ASK10;
1587                 break;
1588         case NFC_DIGITAL_RF_TECH_424F:
1589                 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
1590                     TRF7970A_ISO_CTRL_NFC_NFCF_424;
1591                 trf->modulator_sys_clk_ctrl =
1592                     (trf->modulator_sys_clk_ctrl & 0xf8) |
1593                     TRF7970A_MODULATOR_DEPTH_ASK10;
1594                 break;
1595         default:
1596                 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
1597                 return -EINVAL;
1598         }
1599
1600         trf->technology = tech;
1601
1602         /* Normally we write the ISO_CTRL register in
1603          * trf7970a_tg_config_framing() because the framing can change
1604          * the value written.  However, when sending a PSL RES,
1605          * digital_tg_send_psl_res_complete() doesn't call
1606          * trf7970a_tg_config_framing() so we must write the register
1607          * here.
1608          */
1609         if ((trf->framing == NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED) &&
1610             (trf->iso_ctrl_tech != trf->iso_ctrl)) {
1611                 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL,
1612                                      trf->iso_ctrl_tech);
1613
1614                 trf->iso_ctrl = trf->iso_ctrl_tech;
1615         }
1616
1617         return ret;
1618 }
1619
1620 /* Since this is a target routine, several of the framing calls are
1621  * made between receiving the request and sending the response so they
1622  * should take effect until after the response is sent.  This is accomplished
1623  * by skipping the ISO_CTRL register write here and doing it in the interrupt
1624  * handler.
1625  */
1626 static int trf7970a_tg_config_framing(struct trf7970a *trf, int framing)
1627 {
1628         u8 iso_ctrl = trf->iso_ctrl_tech;
1629         int ret;
1630
1631         dev_dbg(trf->dev, "framing: %d\n", framing);
1632
1633         switch (framing) {
1634         case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
1635                 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
1636                 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
1637                 break;
1638         case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
1639         case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
1640         case NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE:
1641                 /* These ones are applied in the interrupt handler */
1642                 iso_ctrl = trf->iso_ctrl; /* Don't write to ISO_CTRL yet */
1643                 break;
1644         case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP:
1645                 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1646                 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
1647                 break;
1648         case NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED:
1649                 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1650                 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
1651                 break;
1652         default:
1653                 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
1654                 return -EINVAL;
1655         }
1656
1657         trf->framing = framing;
1658
1659         if (iso_ctrl != trf->iso_ctrl) {
1660                 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1661                 if (ret)
1662                         return ret;
1663
1664                 trf->iso_ctrl = iso_ctrl;
1665
1666                 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
1667                                      trf->modulator_sys_clk_ctrl);
1668                 if (ret)
1669                         return ret;
1670         }
1671
1672         if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
1673                 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1674                                      trf->chip_status_ctrl |
1675                                      TRF7970A_CHIP_STATUS_RF_ON);
1676                 if (ret)
1677                         return ret;
1678
1679                 trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
1680         }
1681
1682         return 0;
1683 }
1684
1685 static int trf7970a_tg_configure_hw(struct nfc_digital_dev *ddev, int type,
1686                                     int param)
1687 {
1688         struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1689         int ret;
1690
1691         dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
1692
1693         mutex_lock(&trf->lock);
1694
1695         trf->is_initiator = false;
1696
1697         if ((trf->state == TRF7970A_ST_PWR_OFF) ||
1698             (trf->state == TRF7970A_ST_RF_OFF)) {
1699                 ret = trf7970a_switch_rf_on(trf);
1700                 if (ret)
1701                         goto err_unlock;
1702         }
1703
1704         switch (type) {
1705         case NFC_DIGITAL_CONFIG_RF_TECH:
1706                 ret = trf7970a_tg_config_rf_tech(trf, param);
1707                 break;
1708         case NFC_DIGITAL_CONFIG_FRAMING:
1709                 ret = trf7970a_tg_config_framing(trf, param);
1710                 break;
1711         default:
1712                 dev_dbg(trf->dev, "Unknown type: %d\n", type);
1713                 ret = -EINVAL;
1714         }
1715
1716 err_unlock:
1717         mutex_unlock(&trf->lock);
1718         return ret;
1719 }
1720
1721 static int _trf7970a_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
1722                                nfc_digital_cmd_complete_t cb, void *arg,
1723                                bool mode_detect)
1724 {
1725         struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1726         int ret;
1727
1728         mutex_lock(&trf->lock);
1729
1730         if ((trf->state != TRF7970A_ST_IDLE) &&
1731             (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) {
1732                 dev_err(trf->dev, "%s - Bogus state: %d\n", __func__,
1733                         trf->state);
1734                 ret = -EIO;
1735                 goto out_err;
1736         }
1737
1738         if (trf->aborting) {
1739                 dev_dbg(trf->dev, "Abort process complete\n");
1740                 trf->aborting = false;
1741                 ret = -ECANCELED;
1742                 goto out_err;
1743         }
1744
1745         trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE,
1746                                          GFP_KERNEL);
1747         if (!trf->rx_skb) {
1748                 dev_dbg(trf->dev, "Can't alloc rx_skb\n");
1749                 ret = -ENOMEM;
1750                 goto out_err;
1751         }
1752
1753         ret = trf7970a_write(trf, TRF7970A_RX_SPECIAL_SETTINGS,
1754                              TRF7970A_RX_SPECIAL_SETTINGS_HBT |
1755                              TRF7970A_RX_SPECIAL_SETTINGS_M848 |
1756                              TRF7970A_RX_SPECIAL_SETTINGS_C424 |
1757                              TRF7970A_RX_SPECIAL_SETTINGS_C212);
1758         if (ret)
1759                 goto out_err;
1760
1761         ret = trf7970a_write(trf, TRF7970A_REG_IO_CTRL,
1762                              trf->io_ctrl | TRF7970A_REG_IO_CTRL_VRS(0x1));
1763         if (ret)
1764                 goto out_err;
1765
1766         ret = trf7970a_write(trf, TRF7970A_NFC_LOW_FIELD_LEVEL,
1767                              TRF7970A_NFC_LOW_FIELD_LEVEL_RFDET(0x3));
1768         if (ret)
1769                 goto out_err;
1770
1771         ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL,
1772                              TRF7970A_NFC_TARGET_LEVEL_RFDET(0x7));
1773         if (ret)
1774                 goto out_err;
1775
1776         trf->ddev = ddev;
1777         trf->cb = cb;
1778         trf->cb_arg = arg;
1779         trf->timeout = timeout;
1780         trf->ignore_timeout = false;
1781
1782         ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX);
1783         if (ret)
1784                 goto out_err;
1785
1786         trf->state = mode_detect ? TRF7970A_ST_LISTENING_MD :
1787                                    TRF7970A_ST_LISTENING;
1788
1789         schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout));
1790
1791 out_err:
1792         mutex_unlock(&trf->lock);
1793         return ret;
1794 }
1795
1796 static int trf7970a_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
1797                               nfc_digital_cmd_complete_t cb, void *arg)
1798 {
1799         struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1800
1801         dev_dbg(trf->dev, "Listen - state: %d, timeout: %d ms\n",
1802                 trf->state, timeout);
1803
1804         return _trf7970a_tg_listen(ddev, timeout, cb, arg, false);
1805 }
1806
1807 static int trf7970a_tg_listen_md(struct nfc_digital_dev *ddev,
1808                                  u16 timeout, nfc_digital_cmd_complete_t cb,
1809                                  void *arg)
1810 {
1811         struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1812         int ret;
1813
1814         dev_dbg(trf->dev, "Listen MD - state: %d, timeout: %d ms\n",
1815                 trf->state, timeout);
1816
1817         ret = trf7970a_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
1818                                        NFC_DIGITAL_RF_TECH_106A);
1819         if (ret)
1820                 return ret;
1821
1822         ret = trf7970a_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1823                                        NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
1824         if (ret)
1825                 return ret;
1826
1827         return _trf7970a_tg_listen(ddev, timeout, cb, arg, true);
1828 }
1829
1830 static int trf7970a_tg_get_rf_tech(struct nfc_digital_dev *ddev, u8 *rf_tech)
1831 {
1832         struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1833
1834         dev_dbg(trf->dev, "Get RF Tech - state: %d, rf_tech: %d\n",
1835                 trf->state, trf->md_rf_tech);
1836
1837         *rf_tech = trf->md_rf_tech;
1838
1839         return 0;
1840 }
1841
1842 static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev)
1843 {
1844         struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1845
1846         dev_dbg(trf->dev, "Abort process initiated\n");
1847
1848         mutex_lock(&trf->lock);
1849
1850         switch (trf->state) {
1851         case TRF7970A_ST_WAIT_FOR_TX_FIFO:
1852         case TRF7970A_ST_WAIT_FOR_RX_DATA:
1853         case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1854         case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
1855                 trf->aborting = true;
1856                 break;
1857         case TRF7970A_ST_LISTENING:
1858                 trf->ignore_timeout = !cancel_delayed_work(&trf->timeout_work);
1859                 trf7970a_send_err_upstream(trf, -ECANCELED);
1860                 dev_dbg(trf->dev, "Abort process complete\n");
1861                 break;
1862         default:
1863                 break;
1864         }
1865
1866         mutex_unlock(&trf->lock);
1867 }
1868
1869 static struct nfc_digital_ops trf7970a_nfc_ops = {
1870         .in_configure_hw        = trf7970a_in_configure_hw,
1871         .in_send_cmd            = trf7970a_send_cmd,
1872         .tg_configure_hw        = trf7970a_tg_configure_hw,
1873         .tg_send_cmd            = trf7970a_send_cmd,
1874         .tg_listen              = trf7970a_tg_listen,
1875         .tg_listen_md           = trf7970a_tg_listen_md,
1876         .tg_get_rf_tech         = trf7970a_tg_get_rf_tech,
1877         .switch_rf              = trf7970a_switch_rf,
1878         .abort_cmd              = trf7970a_abort_cmd,
1879 };
1880
1881 static int trf7970a_power_up(struct trf7970a *trf)
1882 {
1883         int ret;
1884
1885         dev_dbg(trf->dev, "Powering up - state: %d\n", trf->state);
1886
1887         if (trf->state != TRF7970A_ST_PWR_OFF)
1888                 return 0;
1889
1890         ret = regulator_enable(trf->vin_regulator);
1891         if (ret) {
1892                 dev_err(trf->dev, "%s - Can't enable VIN: %d\n", __func__, ret);
1893                 return ret;
1894         }
1895
1896         usleep_range(5000, 6000);
1897
1898         if (trf->en2_gpiod &&
1899             !(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW)) {
1900                 gpiod_set_value_cansleep(trf->en2_gpiod, 1);
1901                 usleep_range(1000, 2000);
1902         }
1903
1904         gpiod_set_value_cansleep(trf->en_gpiod, 1);
1905
1906         usleep_range(20000, 21000);
1907
1908         trf->state = TRF7970A_ST_RF_OFF;
1909
1910         return 0;
1911 }
1912
1913 static int trf7970a_power_down(struct trf7970a *trf)
1914 {
1915         int ret;
1916
1917         dev_dbg(trf->dev, "Powering down - state: %d\n", trf->state);
1918
1919         if (trf->state == TRF7970A_ST_PWR_OFF)
1920                 return 0;
1921
1922         if (trf->state != TRF7970A_ST_RF_OFF) {
1923                 dev_dbg(trf->dev, "Can't power down - not RF_OFF state (%d)\n",
1924                         trf->state);
1925                 return -EBUSY;
1926         }
1927
1928         gpiod_set_value_cansleep(trf->en_gpiod, 0);
1929
1930         if (trf->en2_gpiod && !(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW))
1931                 gpiod_set_value_cansleep(trf->en2_gpiod, 0);
1932
1933         ret = regulator_disable(trf->vin_regulator);
1934         if (ret)
1935                 dev_err(trf->dev, "%s - Can't disable VIN: %d\n", __func__,
1936                         ret);
1937
1938         trf->state = TRF7970A_ST_PWR_OFF;
1939
1940         return ret;
1941 }
1942
1943 static int trf7970a_startup(struct trf7970a *trf)
1944 {
1945         int ret;
1946
1947         ret = trf7970a_power_up(trf);
1948         if (ret)
1949                 return ret;
1950
1951         pm_runtime_set_active(trf->dev);
1952         pm_runtime_enable(trf->dev);
1953         pm_runtime_mark_last_busy(trf->dev);
1954
1955         return 0;
1956 }
1957
1958 static void trf7970a_shutdown(struct trf7970a *trf)
1959 {
1960         switch (trf->state) {
1961         case TRF7970A_ST_WAIT_FOR_TX_FIFO:
1962         case TRF7970A_ST_WAIT_FOR_RX_DATA:
1963         case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1964         case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
1965         case TRF7970A_ST_LISTENING:
1966                 trf7970a_send_err_upstream(trf, -ECANCELED);
1967                 /* FALLTHROUGH */
1968         case TRF7970A_ST_IDLE:
1969         case TRF7970A_ST_IDLE_RX_BLOCKED:
1970                 trf7970a_switch_rf_off(trf);
1971                 break;
1972         default:
1973                 break;
1974         }
1975
1976         pm_runtime_disable(trf->dev);
1977         pm_runtime_set_suspended(trf->dev);
1978
1979         trf7970a_power_down(trf);
1980 }
1981
1982 static int trf7970a_get_autosuspend_delay(struct device_node *np)
1983 {
1984         int autosuspend_delay, ret;
1985
1986         ret = of_property_read_u32(np, "autosuspend-delay", &autosuspend_delay);
1987         if (ret)
1988                 autosuspend_delay = TRF7970A_AUTOSUSPEND_DELAY;
1989
1990         return autosuspend_delay;
1991 }
1992
1993 static int trf7970a_probe(struct spi_device *spi)
1994 {
1995         struct device_node *np = spi->dev.of_node;
1996         struct trf7970a *trf;
1997         int uvolts, autosuspend_delay, ret;
1998         u32 clk_freq = TRF7970A_13MHZ_CLOCK_FREQUENCY;
1999
2000         if (!np) {
2001                 dev_err(&spi->dev, "No Device Tree entry\n");
2002                 return -EINVAL;
2003         }
2004
2005         trf = devm_kzalloc(&spi->dev, sizeof(*trf), GFP_KERNEL);
2006         if (!trf)
2007                 return -ENOMEM;
2008
2009         trf->state = TRF7970A_ST_PWR_OFF;
2010         trf->dev = &spi->dev;
2011         trf->spi = spi;
2012
2013         spi->mode = SPI_MODE_1;
2014         spi->bits_per_word = 8;
2015
2016         ret = spi_setup(spi);
2017         if (ret < 0) {
2018                 dev_err(trf->dev, "Can't set up SPI Communication\n");
2019                 return ret;
2020         }
2021
2022         if (of_property_read_bool(np, "irq-status-read-quirk"))
2023                 trf->quirks |= TRF7970A_QUIRK_IRQ_STATUS_READ;
2024
2025         /* There are two enable pins - only EN must be present in the DT */
2026         trf->en_gpiod = devm_gpiod_get_index(trf->dev, "ti,enable", 0,
2027                                              GPIOD_OUT_LOW);
2028         if (IS_ERR(trf->en_gpiod)) {
2029                 dev_err(trf->dev, "No EN GPIO property\n");
2030                 return PTR_ERR(trf->en_gpiod);
2031         }
2032
2033         trf->en2_gpiod = devm_gpiod_get_index_optional(trf->dev, "ti,enable", 1,
2034                                                        GPIOD_OUT_LOW);
2035         if (!trf->en2_gpiod) {
2036                 dev_info(trf->dev, "No EN2 GPIO property\n");
2037         } else if (IS_ERR(trf->en2_gpiod)) {
2038                 dev_err(trf->dev, "Error getting EN2 GPIO property: %ld\n",
2039                         PTR_ERR(trf->en2_gpiod));
2040                 return PTR_ERR(trf->en2_gpiod);
2041         } else if (of_property_read_bool(np, "en2-rf-quirk")) {
2042                 trf->quirks |= TRF7970A_QUIRK_EN2_MUST_STAY_LOW;
2043         }
2044
2045         of_property_read_u32(np, "clock-frequency", &clk_freq);
2046         if ((clk_freq != TRF7970A_27MHZ_CLOCK_FREQUENCY) &&
2047             (clk_freq != TRF7970A_13MHZ_CLOCK_FREQUENCY)) {
2048                 dev_err(trf->dev,
2049                         "clock-frequency (%u Hz) unsupported\n", clk_freq);
2050                 return -EINVAL;
2051         }
2052
2053         if (clk_freq == TRF7970A_27MHZ_CLOCK_FREQUENCY) {
2054                 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_27MHZ;
2055                 dev_dbg(trf->dev, "trf7970a configured for 27MHz crystal\n");
2056         } else {
2057                 trf->modulator_sys_clk_ctrl = 0;
2058         }
2059
2060         ret = devm_request_threaded_irq(trf->dev, spi->irq, NULL,
2061                                         trf7970a_irq,
2062                                         IRQF_TRIGGER_RISING | IRQF_ONESHOT,
2063                                         "trf7970a", trf);
2064         if (ret) {
2065                 dev_err(trf->dev, "Can't request IRQ#%d: %d\n", spi->irq, ret);
2066                 return ret;
2067         }
2068
2069         mutex_init(&trf->lock);
2070         INIT_DELAYED_WORK(&trf->timeout_work, trf7970a_timeout_work_handler);
2071
2072         trf->vin_regulator = devm_regulator_get(&spi->dev, "vin");
2073         if (IS_ERR(trf->vin_regulator)) {
2074                 ret = PTR_ERR(trf->vin_regulator);
2075                 dev_err(trf->dev, "Can't get VIN regulator: %d\n", ret);
2076                 goto err_destroy_lock;
2077         }
2078
2079         ret = regulator_enable(trf->vin_regulator);
2080         if (ret) {
2081                 dev_err(trf->dev, "Can't enable VIN: %d\n", ret);
2082                 goto err_destroy_lock;
2083         }
2084
2085         uvolts = regulator_get_voltage(trf->vin_regulator);
2086         if (uvolts > 4000000)
2087                 trf->chip_status_ctrl = TRF7970A_CHIP_STATUS_VRS5_3;
2088
2089         trf->vddio_regulator = devm_regulator_get(&spi->dev, "vdd-io");
2090         if (IS_ERR(trf->vddio_regulator)) {
2091                 ret = PTR_ERR(trf->vddio_regulator);
2092                 dev_err(trf->dev, "Can't get VDD_IO regulator: %d\n", ret);
2093                 goto err_disable_vin_regulator;
2094         }
2095
2096         ret = regulator_enable(trf->vddio_regulator);
2097         if (ret) {
2098                 dev_err(trf->dev, "Can't enable VDD_IO: %d\n", ret);
2099                 goto err_disable_vin_regulator;
2100         }
2101
2102         if (regulator_get_voltage(trf->vddio_regulator) == 1800000) {
2103                 trf->io_ctrl = TRF7970A_REG_IO_CTRL_IO_LOW;
2104                 dev_dbg(trf->dev, "trf7970a config vdd_io to 1.8V\n");
2105         }
2106
2107         trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops,
2108                                                 TRF7970A_SUPPORTED_PROTOCOLS,
2109                                                 NFC_DIGITAL_DRV_CAPS_IN_CRC |
2110                                                 NFC_DIGITAL_DRV_CAPS_TG_CRC, 0,
2111                                                 0);
2112         if (!trf->ddev) {
2113                 dev_err(trf->dev, "Can't allocate NFC digital device\n");
2114                 ret = -ENOMEM;
2115                 goto err_disable_vddio_regulator;
2116         }
2117
2118         nfc_digital_set_parent_dev(trf->ddev, trf->dev);
2119         nfc_digital_set_drvdata(trf->ddev, trf);
2120         spi_set_drvdata(spi, trf);
2121
2122         autosuspend_delay = trf7970a_get_autosuspend_delay(np);
2123
2124         pm_runtime_set_autosuspend_delay(trf->dev, autosuspend_delay);
2125         pm_runtime_use_autosuspend(trf->dev);
2126
2127         ret = trf7970a_startup(trf);
2128         if (ret)
2129                 goto err_free_ddev;
2130
2131         ret = nfc_digital_register_device(trf->ddev);
2132         if (ret) {
2133                 dev_err(trf->dev, "Can't register NFC digital device: %d\n",
2134                         ret);
2135                 goto err_shutdown;
2136         }
2137
2138         return 0;
2139
2140 err_shutdown:
2141         trf7970a_shutdown(trf);
2142 err_free_ddev:
2143         nfc_digital_free_device(trf->ddev);
2144 err_disable_vddio_regulator:
2145         regulator_disable(trf->vddio_regulator);
2146 err_disable_vin_regulator:
2147         regulator_disable(trf->vin_regulator);
2148 err_destroy_lock:
2149         mutex_destroy(&trf->lock);
2150         return ret;
2151 }
2152
2153 static int trf7970a_remove(struct spi_device *spi)
2154 {
2155         struct trf7970a *trf = spi_get_drvdata(spi);
2156
2157         mutex_lock(&trf->lock);
2158
2159         trf7970a_shutdown(trf);
2160
2161         mutex_unlock(&trf->lock);
2162
2163         nfc_digital_unregister_device(trf->ddev);
2164         nfc_digital_free_device(trf->ddev);
2165
2166         regulator_disable(trf->vddio_regulator);
2167         regulator_disable(trf->vin_regulator);
2168
2169         mutex_destroy(&trf->lock);
2170
2171         return 0;
2172 }
2173
2174 #ifdef CONFIG_PM_SLEEP
2175 static int trf7970a_suspend(struct device *dev)
2176 {
2177         struct spi_device *spi = to_spi_device(dev);
2178         struct trf7970a *trf = spi_get_drvdata(spi);
2179
2180         dev_dbg(dev, "Suspend\n");
2181
2182         mutex_lock(&trf->lock);
2183
2184         trf7970a_shutdown(trf);
2185
2186         mutex_unlock(&trf->lock);
2187
2188         return 0;
2189 }
2190
2191 static int trf7970a_resume(struct device *dev)
2192 {
2193         struct spi_device *spi = to_spi_device(dev);
2194         struct trf7970a *trf = spi_get_drvdata(spi);
2195         int ret;
2196
2197         dev_dbg(dev, "Resume\n");
2198
2199         mutex_lock(&trf->lock);
2200
2201         ret = trf7970a_startup(trf);
2202
2203         mutex_unlock(&trf->lock);
2204
2205         return ret;
2206 }
2207 #endif
2208
2209 #ifdef CONFIG_PM
2210 static int trf7970a_pm_runtime_suspend(struct device *dev)
2211 {
2212         struct spi_device *spi = to_spi_device(dev);
2213         struct trf7970a *trf = spi_get_drvdata(spi);
2214         int ret;
2215
2216         dev_dbg(dev, "Runtime suspend\n");
2217
2218         mutex_lock(&trf->lock);
2219
2220         ret = trf7970a_power_down(trf);
2221
2222         mutex_unlock(&trf->lock);
2223
2224         return ret;
2225 }
2226
2227 static int trf7970a_pm_runtime_resume(struct device *dev)
2228 {
2229         struct spi_device *spi = to_spi_device(dev);
2230         struct trf7970a *trf = spi_get_drvdata(spi);
2231         int ret;
2232
2233         dev_dbg(dev, "Runtime resume\n");
2234
2235         ret = trf7970a_power_up(trf);
2236         if (!ret)
2237                 pm_runtime_mark_last_busy(dev);
2238
2239         return ret;
2240 }
2241 #endif
2242
2243 static const struct dev_pm_ops trf7970a_pm_ops = {
2244         SET_SYSTEM_SLEEP_PM_OPS(trf7970a_suspend, trf7970a_resume)
2245         SET_RUNTIME_PM_OPS(trf7970a_pm_runtime_suspend,
2246                            trf7970a_pm_runtime_resume, NULL)
2247 };
2248
2249 static const struct of_device_id trf7970a_of_match[] = {
2250         {.compatible = "ti,trf7970a",},
2251         {},
2252 };
2253
2254 MODULE_DEVICE_TABLE(of, trf7970a_of_match);
2255
2256 static const struct spi_device_id trf7970a_id_table[] = {
2257         {"trf7970a", 0},
2258         {}
2259 };
2260
2261 MODULE_DEVICE_TABLE(spi, trf7970a_id_table);
2262
2263 static struct spi_driver trf7970a_spi_driver = {
2264         .probe          = trf7970a_probe,
2265         .remove         = trf7970a_remove,
2266         .id_table       = trf7970a_id_table,
2267         .driver = {
2268                 .name           = "trf7970a",
2269                 .of_match_table = of_match_ptr(trf7970a_of_match),
2270                 .pm             = &trf7970a_pm_ops,
2271         },
2272 };
2273
2274 module_spi_driver(trf7970a_spi_driver);
2275
2276 MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>");
2277 MODULE_LICENSE("GPL v2");
2278 MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver");