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