fbdev/broadsheetfb: fix memory leak
[deliverable/linux.git] / drivers / nfc / trf7970a.c
CommitLineData
165063f1
MG
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>
e6403b7c 19#include <linux/pm_runtime.h>
165063f1
MG
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
95064bd9
MG
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.
165063f1
MG
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
13b4272a 65 * support that. So, if an abort is received before trf7970a_send_cmd()
165063f1
MG
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
5fa3af35
MG
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
165063f1
MG
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
5fa3af35
MG
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.
165063f1 85 *
38b4eb1f
MG
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 *
165063f1
MG
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.
9d9304b3
MG
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
38b4eb1f
MG
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
9d9304b3 118 * reliably.
165063f1
MG
119 */
120
80062891 121#define TRF7970A_SUPPORTED_PROTOCOLS \
9d9304b3 122 (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \
6857bb96 123 NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_FELICA_MASK | \
13b4272a 124 NFC_PROTO_ISO15693_MASK | NFC_PROTO_NFC_DEP_MASK)
165063f1 125
e6403b7c
MG
126#define TRF7970A_AUTOSUSPEND_DELAY 30000 /* 30 seconds */
127
165063f1
MG
128#define TRF7970A_RX_SKB_ALLOC_SIZE 256
129
1568bfef 130#define TRF7970A_FIFO_SIZE 127
165063f1
MG
131
132/* TX length is 3 nibbles long ==> 4KB - 1 bytes max */
133#define TRF7970A_TX_MAX (4096 - 1)
134
1961843c 135#define TRF7970A_WAIT_FOR_TX_IRQ 20
5fa3af35 136#define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 20
38b4eb1f
MG
137#define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 20
138#define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 40
165063f1 139
4e64eff8
MG
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
165063f1
MG
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 */
772079eb 150#define TRF7970A_QUIRK_IRQ_STATUS_READ BIT(0)
95064bd9 151#define TRF7970A_QUIRK_EN2_MUST_STAY_LOW BIT(1)
165063f1
MG
152
153/* Direct commands */
154#define TRF7970A_CMD_IDLE 0x00
155#define TRF7970A_CMD_SOFT_INIT 0x03
156#define TRF7970A_CMD_RF_COLLISION 0x04
157#define TRF7970A_CMD_RF_COLLISION_RESPONSE_N 0x05
158#define TRF7970A_CMD_RF_COLLISION_RESPONSE_0 0x06
159#define TRF7970A_CMD_FIFO_RESET 0x0f
160#define TRF7970A_CMD_TRANSMIT_NO_CRC 0x10
161#define TRF7970A_CMD_TRANSMIT 0x11
162#define TRF7970A_CMD_DELAY_TRANSMIT_NO_CRC 0x12
163#define TRF7970A_CMD_DELAY_TRANSMIT 0x13
164#define TRF7970A_CMD_EOF 0x14
165#define TRF7970A_CMD_CLOSE_SLOT 0x15
166#define TRF7970A_CMD_BLOCK_RX 0x16
167#define TRF7970A_CMD_ENABLE_RX 0x17
851ee3cb
MG
168#define TRF7970A_CMD_TEST_INT_RF 0x18
169#define TRF7970A_CMD_TEST_EXT_RF 0x19
165063f1
MG
170#define TRF7970A_CMD_RX_GAIN_ADJUST 0x1a
171
172/* Bits determining whether its a direct command or register R/W,
173 * whether to use a continuous SPI transaction or not, and the actual
174 * direct cmd opcode or regster address.
175 */
176#define TRF7970A_CMD_BIT_CTRL BIT(7)
177#define TRF7970A_CMD_BIT_RW BIT(6)
178#define TRF7970A_CMD_BIT_CONTINUOUS BIT(5)
179#define TRF7970A_CMD_BIT_OPCODE(opcode) ((opcode) & 0x1f)
180
181/* Registers addresses */
182#define TRF7970A_CHIP_STATUS_CTRL 0x00
183#define TRF7970A_ISO_CTRL 0x01
184#define TRF7970A_ISO14443B_TX_OPTIONS 0x02
185#define TRF7970A_ISO14443A_HIGH_BITRATE_OPTIONS 0x03
186#define TRF7970A_TX_TIMER_SETTING_H_BYTE 0x04
187#define TRF7970A_TX_TIMER_SETTING_L_BYTE 0x05
188#define TRF7970A_TX_PULSE_LENGTH_CTRL 0x06
189#define TRF7970A_RX_NO_RESPONSE_WAIT 0x07
190#define TRF7970A_RX_WAIT_TIME 0x08
191#define TRF7970A_MODULATOR_SYS_CLK_CTRL 0x09
192#define TRF7970A_RX_SPECIAL_SETTINGS 0x0a
193#define TRF7970A_REG_IO_CTRL 0x0b
194#define TRF7970A_IRQ_STATUS 0x0c
195#define TRF7970A_COLLISION_IRQ_MASK 0x0d
196#define TRF7970A_COLLISION_POSITION 0x0e
197#define TRF7970A_RSSI_OSC_STATUS 0x0f
198#define TRF7970A_SPECIAL_FCN_REG1 0x10
199#define TRF7970A_SPECIAL_FCN_REG2 0x11
200#define TRF7970A_RAM1 0x12
201#define TRF7970A_RAM2 0x13
202#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS 0x14
203#define TRF7970A_NFC_LOW_FIELD_LEVEL 0x16
204#define TRF7970A_NFCID1 0x17
205#define TRF7970A_NFC_TARGET_LEVEL 0x18
206#define TRF79070A_NFC_TARGET_PROTOCOL 0x19
207#define TRF7970A_TEST_REGISTER1 0x1a
208#define TRF7970A_TEST_REGISTER2 0x1b
209#define TRF7970A_FIFO_STATUS 0x1c
210#define TRF7970A_TX_LENGTH_BYTE1 0x1d
211#define TRF7970A_TX_LENGTH_BYTE2 0x1e
212#define TRF7970A_FIFO_IO_REGISTER 0x1f
213
214/* Chip Status Control Register Bits */
215#define TRF7970A_CHIP_STATUS_VRS5_3 BIT(0)
216#define TRF7970A_CHIP_STATUS_REC_ON BIT(1)
217#define TRF7970A_CHIP_STATUS_AGC_ON BIT(2)
218#define TRF7970A_CHIP_STATUS_PM_ON BIT(3)
219#define TRF7970A_CHIP_STATUS_RF_PWR BIT(4)
220#define TRF7970A_CHIP_STATUS_RF_ON BIT(5)
221#define TRF7970A_CHIP_STATUS_DIRECT BIT(6)
222#define TRF7970A_CHIP_STATUS_STBY BIT(7)
223
224/* ISO Control Register Bits */
225#define TRF7970A_ISO_CTRL_15693_SGL_1OF4_662 0x00
226#define TRF7970A_ISO_CTRL_15693_SGL_1OF256_662 0x01
227#define TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648 0x02
228#define TRF7970A_ISO_CTRL_15693_SGL_1OF256_2648 0x03
229#define TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a 0x04
230#define TRF7970A_ISO_CTRL_15693_DBL_1OF256_667 0x05
231#define TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669 0x06
232#define TRF7970A_ISO_CTRL_15693_DBL_1OF256_2669 0x07
233#define TRF7970A_ISO_CTRL_14443A_106 0x08
234#define TRF7970A_ISO_CTRL_14443A_212 0x09
235#define TRF7970A_ISO_CTRL_14443A_424 0x0a
236#define TRF7970A_ISO_CTRL_14443A_848 0x0b
237#define TRF7970A_ISO_CTRL_14443B_106 0x0c
238#define TRF7970A_ISO_CTRL_14443B_212 0x0d
239#define TRF7970A_ISO_CTRL_14443B_424 0x0e
240#define TRF7970A_ISO_CTRL_14443B_848 0x0f
241#define TRF7970A_ISO_CTRL_FELICA_212 0x1a
242#define TRF7970A_ISO_CTRL_FELICA_424 0x1b
13b4272a
MG
243#define TRF7970A_ISO_CTRL_NFC_NFCA_106 0x01
244#define TRF7970A_ISO_CTRL_NFC_NFCF_212 0x02
245#define TRF7970A_ISO_CTRL_NFC_NFCF_424 0x03
246#define TRF7970A_ISO_CTRL_NFC_CE_14443A 0x00
247#define TRF7970A_ISO_CTRL_NFC_CE_14443B 0x01
248#define TRF7970A_ISO_CTRL_NFC_CE BIT(2)
249#define TRF7970A_ISO_CTRL_NFC_ACTIVE BIT(3)
250#define TRF7970A_ISO_CTRL_NFC_INITIATOR BIT(4)
251#define TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE BIT(5)
165063f1
MG
252#define TRF7970A_ISO_CTRL_RFID BIT(5)
253#define TRF7970A_ISO_CTRL_DIR_MODE BIT(6)
254#define TRF7970A_ISO_CTRL_RX_CRC_N BIT(7) /* true == No CRC */
255
256#define TRF7970A_ISO_CTRL_RFID_SPEED_MASK 0x1f
257
258/* Modulator and SYS_CLK Control Register Bits */
259#define TRF7970A_MODULATOR_DEPTH(n) ((n) & 0x7)
260#define TRF7970A_MODULATOR_DEPTH_ASK10 (TRF7970A_MODULATOR_DEPTH(0))
261#define TRF7970A_MODULATOR_DEPTH_OOK (TRF7970A_MODULATOR_DEPTH(1))
262#define TRF7970A_MODULATOR_DEPTH_ASK7 (TRF7970A_MODULATOR_DEPTH(2))
263#define TRF7970A_MODULATOR_DEPTH_ASK8_5 (TRF7970A_MODULATOR_DEPTH(3))
264#define TRF7970A_MODULATOR_DEPTH_ASK13 (TRF7970A_MODULATOR_DEPTH(4))
265#define TRF7970A_MODULATOR_DEPTH_ASK16 (TRF7970A_MODULATOR_DEPTH(5))
266#define TRF7970A_MODULATOR_DEPTH_ASK22 (TRF7970A_MODULATOR_DEPTH(6))
267#define TRF7970A_MODULATOR_DEPTH_ASK30 (TRF7970A_MODULATOR_DEPTH(7))
268#define TRF7970A_MODULATOR_EN_ANA BIT(3)
269#define TRF7970A_MODULATOR_CLK(n) (((n) & 0x3) << 4)
270#define TRF7970A_MODULATOR_CLK_DISABLED (TRF7970A_MODULATOR_CLK(0))
271#define TRF7970A_MODULATOR_CLK_3_6 (TRF7970A_MODULATOR_CLK(1))
272#define TRF7970A_MODULATOR_CLK_6_13 (TRF7970A_MODULATOR_CLK(2))
273#define TRF7970A_MODULATOR_CLK_13_27 (TRF7970A_MODULATOR_CLK(3))
274#define TRF7970A_MODULATOR_EN_OOK BIT(6)
275#define TRF7970A_MODULATOR_27MHZ BIT(7)
276
13b4272a
MG
277#define TRF7970A_RX_SPECIAL_SETTINGS_NO_LIM BIT(0)
278#define TRF7970A_RX_SPECIAL_SETTINGS_AGCR BIT(1)
279#define TRF7970A_RX_SPECIAL_SETTINGS_GD_0DB (0x0 << 2)
280#define TRF7970A_RX_SPECIAL_SETTINGS_GD_5DB (0x1 << 2)
281#define TRF7970A_RX_SPECIAL_SETTINGS_GD_10DB (0x2 << 2)
282#define TRF7970A_RX_SPECIAL_SETTINGS_GD_15DB (0x3 << 2)
283#define TRF7970A_RX_SPECIAL_SETTINGS_HBT BIT(4)
284#define TRF7970A_RX_SPECIAL_SETTINGS_M848 BIT(5)
285#define TRF7970A_RX_SPECIAL_SETTINGS_C424 BIT(6)
286#define TRF7970A_RX_SPECIAL_SETTINGS_C212 BIT(7)
287
288#define TRF7970A_REG_IO_CTRL_VRS(v) ((v) & 0x07)
289#define TRF7970A_REG_IO_CTRL_IO_LOW BIT(5)
290#define TRF7970A_REG_IO_CTRL_EN_EXT_PA BIT(6)
291#define TRF7970A_REG_IO_CTRL_AUTO_REG BIT(7)
292
165063f1
MG
293/* IRQ Status Register Bits */
294#define TRF7970A_IRQ_STATUS_NORESP BIT(0) /* ISO15693 only */
13b4272a 295#define TRF7970A_IRQ_STATUS_NFC_COL_ERROR BIT(0)
165063f1
MG
296#define TRF7970A_IRQ_STATUS_COL BIT(1)
297#define TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR BIT(2)
13b4272a 298#define TRF7970A_IRQ_STATUS_NFC_RF BIT(2)
165063f1 299#define TRF7970A_IRQ_STATUS_PARITY_ERROR BIT(3)
13b4272a 300#define TRF7970A_IRQ_STATUS_NFC_SDD BIT(3)
165063f1 301#define TRF7970A_IRQ_STATUS_CRC_ERROR BIT(4)
13b4272a 302#define TRF7970A_IRQ_STATUS_NFC_PROTO_ERROR BIT(4)
165063f1
MG
303#define TRF7970A_IRQ_STATUS_FIFO BIT(5)
304#define TRF7970A_IRQ_STATUS_SRX BIT(6)
305#define TRF7970A_IRQ_STATUS_TX BIT(7)
306
307#define TRF7970A_IRQ_STATUS_ERROR \
308 (TRF7970A_IRQ_STATUS_COL | \
309 TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR | \
310 TRF7970A_IRQ_STATUS_PARITY_ERROR | \
311 TRF7970A_IRQ_STATUS_CRC_ERROR)
312
851ee3cb
MG
313#define TRF7970A_RSSI_OSC_STATUS_RSSI_MASK (BIT(2) | BIT(1) | BIT(0))
314#define TRF7970A_RSSI_OSC_STATUS_RSSI_X_MASK (BIT(5) | BIT(4) | BIT(3))
315#define TRF7970A_RSSI_OSC_STATUS_RSSI_OSC_OK BIT(6)
316
165063f1
MG
317#define TRF7970A_SPECIAL_FCN_REG1_COL_7_6 BIT(0)
318#define TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL BIT(1)
319#define TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX BIT(2)
320#define TRF7970A_SPECIAL_FCN_REG1_SP_DIR_MODE BIT(3)
321#define TRF7970A_SPECIAL_FCN_REG1_NEXT_SLOT_37US BIT(4)
322#define TRF7970A_SPECIAL_FCN_REG1_PAR43 BIT(5)
323
324#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_124 (0x0 << 2)
325#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_120 (0x1 << 2)
326#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_112 (0x2 << 2)
327#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 (0x3 << 2)
328#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_4 0x0
329#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_8 0x1
330#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_16 0x2
331#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32 0x3
332
13b4272a
MG
333#define TRF7970A_NFC_LOW_FIELD_LEVEL_RFDET(v) ((v) & 0x07)
334#define TRF7970A_NFC_LOW_FIELD_LEVEL_CLEX_DIS BIT(7)
335
336#define TRF7970A_NFC_TARGET_LEVEL_RFDET(v) ((v) & 0x07)
337#define TRF7970A_NFC_TARGET_LEVEL_HI_RF BIT(3)
338#define TRF7970A_NFC_TARGET_LEVEL_SDD_EN BIT(3)
339#define TRF7970A_NFC_TARGET_LEVEL_LD_S_4BYTES (0x0 << 6)
340#define TRF7970A_NFC_TARGET_LEVEL_LD_S_7BYTES (0x1 << 6)
341#define TRF7970A_NFC_TARGET_LEVEL_LD_S_10BYTES (0x2 << 6)
342
cb174aba
MG
343#define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106 BIT(0)
344#define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_212 BIT(1)
345#define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_424 (BIT(0) | BIT(1))
346#define TRF79070A_NFC_TARGET_PROTOCOL_PAS_14443B BIT(2)
347#define TRF79070A_NFC_TARGET_PROTOCOL_PAS_106 BIT(3)
348#define TRF79070A_NFC_TARGET_PROTOCOL_FELICA BIT(4)
349#define TRF79070A_NFC_TARGET_PROTOCOL_RF_L BIT(6)
350#define TRF79070A_NFC_TARGET_PROTOCOL_RF_H BIT(7)
351
352#define TRF79070A_NFC_TARGET_PROTOCOL_106A \
353 (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \
354 TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \
355 TRF79070A_NFC_TARGET_PROTOCOL_PAS_106 | \
356 TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106)
357
358#define TRF79070A_NFC_TARGET_PROTOCOL_106B \
359 (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \
360 TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \
361 TRF79070A_NFC_TARGET_PROTOCOL_PAS_14443B | \
362 TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106)
363
364#define TRF79070A_NFC_TARGET_PROTOCOL_212F \
365 (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \
366 TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \
367 TRF79070A_NFC_TARGET_PROTOCOL_FELICA | \
368 TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_212)
369
370#define TRF79070A_NFC_TARGET_PROTOCOL_424F \
371 (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \
372 TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \
373 TRF79070A_NFC_TARGET_PROTOCOL_FELICA | \
374 TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_424)
375
165063f1
MG
376#define TRF7970A_FIFO_STATUS_OVERFLOW BIT(7)
377
378/* NFC (ISO/IEC 14443A) Type 2 Tag commands */
379#define NFC_T2T_CMD_READ 0x30
380
9d9304b3
MG
381/* ISO 15693 commands codes */
382#define ISO15693_CMD_INVENTORY 0x01
383#define ISO15693_CMD_READ_SINGLE_BLOCK 0x20
384#define ISO15693_CMD_WRITE_SINGLE_BLOCK 0x21
385#define ISO15693_CMD_LOCK_BLOCK 0x22
386#define ISO15693_CMD_READ_MULTIPLE_BLOCK 0x23
387#define ISO15693_CMD_WRITE_MULTIPLE_BLOCK 0x24
388#define ISO15693_CMD_SELECT 0x25
389#define ISO15693_CMD_RESET_TO_READY 0x26
390#define ISO15693_CMD_WRITE_AFI 0x27
391#define ISO15693_CMD_LOCK_AFI 0x28
392#define ISO15693_CMD_WRITE_DSFID 0x29
393#define ISO15693_CMD_LOCK_DSFID 0x2a
394#define ISO15693_CMD_GET_SYSTEM_INFO 0x2b
395#define ISO15693_CMD_GET_MULTIPLE_BLOCK_SECURITY_STATUS 0x2c
396
397/* ISO 15693 request and response flags */
398#define ISO15693_REQ_FLAG_SUB_CARRIER BIT(0)
399#define ISO15693_REQ_FLAG_DATA_RATE BIT(1)
400#define ISO15693_REQ_FLAG_INVENTORY BIT(2)
401#define ISO15693_REQ_FLAG_PROTOCOL_EXT BIT(3)
402#define ISO15693_REQ_FLAG_SELECT BIT(4)
403#define ISO15693_REQ_FLAG_AFI BIT(4)
404#define ISO15693_REQ_FLAG_ADDRESS BIT(5)
405#define ISO15693_REQ_FLAG_NB_SLOTS BIT(5)
406#define ISO15693_REQ_FLAG_OPTION BIT(6)
407
408#define ISO15693_REQ_FLAG_SPEED_MASK \
409 (ISO15693_REQ_FLAG_SUB_CARRIER | ISO15693_REQ_FLAG_DATA_RATE)
410
165063f1 411enum trf7970a_state {
ceccd6aa 412 TRF7970A_ST_PWR_OFF,
b5e17d9b 413 TRF7970A_ST_RF_OFF,
165063f1
MG
414 TRF7970A_ST_IDLE,
415 TRF7970A_ST_IDLE_RX_BLOCKED,
416 TRF7970A_ST_WAIT_FOR_TX_FIFO,
417 TRF7970A_ST_WAIT_FOR_RX_DATA,
418 TRF7970A_ST_WAIT_FOR_RX_DATA_CONT,
9d9304b3 419 TRF7970A_ST_WAIT_TO_ISSUE_EOF,
13b4272a 420 TRF7970A_ST_LISTENING,
cb174aba 421 TRF7970A_ST_LISTENING_MD,
165063f1
MG
422 TRF7970A_ST_MAX
423};
424
425struct trf7970a {
426 enum trf7970a_state state;
427 struct device *dev;
428 struct spi_device *spi;
429 struct regulator *regulator;
430 struct nfc_digital_dev *ddev;
431 u32 quirks;
13b4272a 432 bool is_initiator;
165063f1
MG
433 bool aborting;
434 struct sk_buff *tx_skb;
435 struct sk_buff *rx_skb;
436 nfc_digital_cmd_complete_t cb;
437 void *cb_arg;
ebcc5a0d 438 u8 chip_status_ctrl;
165063f1 439 u8 iso_ctrl;
49d19cc7 440 u8 iso_ctrl_tech;
12e9ade3 441 u8 modulator_sys_clk_ctrl;
165063f1 442 u8 special_fcn_reg1;
4e64eff8 443 unsigned int guard_time;
165063f1
MG
444 int technology;
445 int framing;
cb174aba 446 u8 md_rf_tech;
165063f1 447 u8 tx_cmd;
9d9304b3 448 bool issue_eof;
165063f1
MG
449 int en2_gpio;
450 int en_gpio;
451 struct mutex lock;
452 unsigned int timeout;
453 bool ignore_timeout;
454 struct delayed_work timeout_work;
455};
456
457
458static int trf7970a_cmd(struct trf7970a *trf, u8 opcode)
459{
460 u8 cmd = TRF7970A_CMD_BIT_CTRL | TRF7970A_CMD_BIT_OPCODE(opcode);
461 int ret;
462
463 dev_dbg(trf->dev, "cmd: 0x%x\n", cmd);
464
465 ret = spi_write(trf->spi, &cmd, 1);
466 if (ret)
467 dev_err(trf->dev, "%s - cmd: 0x%x, ret: %d\n", __func__, cmd,
468 ret);
469 return ret;
470}
471
472static int trf7970a_read(struct trf7970a *trf, u8 reg, u8 *val)
473{
474 u8 addr = TRF7970A_CMD_BIT_RW | reg;
475 int ret;
476
477 ret = spi_write_then_read(trf->spi, &addr, 1, val, 1);
478 if (ret)
479 dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr,
480 ret);
481
482 dev_dbg(trf->dev, "read(0x%x): 0x%x\n", addr, *val);
483
484 return ret;
485}
486
3e7f3356 487static int trf7970a_read_cont(struct trf7970a *trf, u8 reg, u8 *buf, size_t len)
165063f1
MG
488{
489 u8 addr = reg | TRF7970A_CMD_BIT_RW | TRF7970A_CMD_BIT_CONTINUOUS;
3e7f3356
MG
490 struct spi_transfer t[2];
491 struct spi_message m;
165063f1
MG
492 int ret;
493
494 dev_dbg(trf->dev, "read_cont(0x%x, %zd)\n", addr, len);
495
3e7f3356
MG
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);
165063f1
MG
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
772079eb 538 if (trf->quirks & TRF7970A_QUIRK_IRQ_STATUS_READ) {
165063f1
MG
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
cb174aba
MG
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
165063f1
MG
606static void trf7970a_send_upstream(struct trf7970a *trf)
607{
165063f1
MG
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
165063f1
MG
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
6fb9edcb
MG
638 cancel_delayed_work(&trf->timeout_work);
639
165063f1
MG
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,
7a1e5552 647 unsigned int len, u8 *prefix, unsigned int prefix_len)
165063f1 648{
7a1e5552
MG
649 struct spi_transfer t[2];
650 struct spi_message m;
165063f1
MG
651 unsigned int timeout;
652 int ret;
653
654 print_hex_dump_debug("trf7970a tx data: ", DUMP_PREFIX_NONE,
655 16, 1, skb->data, len, false);
656
7a1e5552
MG
657 spi_message_init(&m);
658
659 memset(&t, 0, sizeof(t));
660
661 t[0].tx_buf = prefix;
662 t[0].len = prefix_len;
663 spi_message_add_tail(&t[0], &m);
664
665 t[1].tx_buf = skb->data;
666 t[1].len = len;
667 spi_message_add_tail(&t[1], &m);
668
669 ret = spi_sync(trf->spi, &m);
165063f1
MG
670 if (ret) {
671 dev_err(trf->dev, "%s - Can't send tx data: %d\n", __func__,
672 ret);
673 return ret;
674 }
675
676 skb_pull(skb, len);
677
678 if (skb->len > 0) {
679 trf->state = TRF7970A_ST_WAIT_FOR_TX_FIFO;
680 timeout = TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT;
681 } else {
9d9304b3
MG
682 if (trf->issue_eof) {
683 trf->state = TRF7970A_ST_WAIT_TO_ISSUE_EOF;
684 timeout = TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF;
685 } else {
686 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA;
1961843c
MG
687
688 if (!trf->timeout)
689 timeout = TRF7970A_WAIT_FOR_TX_IRQ;
690 else
691 timeout = trf->timeout;
9d9304b3 692 }
165063f1
MG
693 }
694
695 dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", timeout,
696 trf->state);
697
698 schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout));
699
700 return 0;
701}
702
703static void trf7970a_fill_fifo(struct trf7970a *trf)
704{
705 struct sk_buff *skb = trf->tx_skb;
706 unsigned int len;
707 int ret;
708 u8 fifo_bytes;
7a1e5552 709 u8 prefix;
165063f1
MG
710
711 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
712 if (ret) {
713 trf7970a_send_err_upstream(trf, ret);
714 return;
715 }
716
717 dev_dbg(trf->dev, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes);
718
4542e834 719 fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
165063f1
MG
720
721 /* Calculate how much more data can be written to the fifo */
722 len = TRF7970A_FIFO_SIZE - fifo_bytes;
0e840ed5
MG
723 if (!len) {
724 schedule_delayed_work(&trf->timeout_work,
725 msecs_to_jiffies(TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT));
726 return;
727 }
728
165063f1
MG
729 len = min(skb->len, len);
730
7a1e5552
MG
731 prefix = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_FIFO_IO_REGISTER;
732
733 ret = trf7970a_transmit(trf, skb, len, &prefix, sizeof(prefix));
165063f1
MG
734 if (ret)
735 trf7970a_send_err_upstream(trf, ret);
736}
737
738static void trf7970a_drain_fifo(struct trf7970a *trf, u8 status)
739{
740 struct sk_buff *skb = trf->rx_skb;
741 int ret;
742 u8 fifo_bytes;
743
744 if (status & TRF7970A_IRQ_STATUS_ERROR) {
745 trf7970a_send_err_upstream(trf, -EIO);
746 return;
747 }
748
749 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
750 if (ret) {
751 trf7970a_send_err_upstream(trf, ret);
752 return;
753 }
754
755 dev_dbg(trf->dev, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes);
756
4542e834
MG
757 fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
758
165063f1
MG
759 if (!fifo_bytes)
760 goto no_rx_data;
761
165063f1
MG
762 if (fifo_bytes > skb_tailroom(skb)) {
763 skb = skb_copy_expand(skb, skb_headroom(skb),
764 max_t(int, fifo_bytes,
765 TRF7970A_RX_SKB_ALLOC_SIZE),
766 GFP_KERNEL);
767 if (!skb) {
768 trf7970a_send_err_upstream(trf, -ENOMEM);
769 return;
770 }
771
772 kfree_skb(trf->rx_skb);
773 trf->rx_skb = skb;
774 }
775
776 ret = trf7970a_read_cont(trf, TRF7970A_FIFO_IO_REGISTER,
777 skb_put(skb, fifo_bytes), fifo_bytes);
778 if (ret) {
779 trf7970a_send_err_upstream(trf, ret);
780 return;
781 }
782
783 /* If received Type 2 ACK/NACK, shift right 4 bits and pass up */
784 if ((trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T) && (skb->len == 1) &&
785 (trf->special_fcn_reg1 ==
786 TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX)) {
787 skb->data[0] >>= 4;
788 status = TRF7970A_IRQ_STATUS_SRX;
789 } else {
790 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA_CONT;
5d8f7594
MG
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;
165063f1
MG
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;
13b4272a 825 u8 status, fifo_bytes, iso_ctrl;
165063f1
MG
826
827 mutex_lock(&trf->lock);
828
b5e17d9b 829 if (trf->state == TRF7970A_ST_RF_OFF) {
165063f1
MG
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:
13b4272a
MG
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.
165063f1 855 */
13b4272a 856 if (trf->is_initiator && (status & TRF7970A_IRQ_STATUS_ERROR)) {
165063f1
MG
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);
bece3c54
MG
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);
13b4272a
MG
888 } else if ((status == TRF7970A_IRQ_STATUS_TX) ||
889 (!trf->is_initiator &&
890 (status == (TRF7970A_IRQ_STATUS_TX |
891 TRF7970A_IRQ_STATUS_NFC_RF)))) {
4dd836e4 892 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
1961843c
MG
893
894 if (!trf->timeout) {
895 trf->ignore_timeout = !cancel_delayed_work(
896 &trf->timeout_work);
897 trf->rx_skb = ERR_PTR(0);
898 trf7970a_send_upstream(trf);
899 break;
900 }
13b4272a
MG
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)
b9e3016a 923 goto err_unlock_exit;
13b4272a
MG
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)
b9e3016a 936 goto err_unlock_exit;
13b4272a
MG
937
938 trf->iso_ctrl = iso_ctrl;
939 }
4dd836e4 940 } else {
165063f1
MG
941 trf7970a_send_err_upstream(trf, -EIO);
942 }
943 break;
9d9304b3
MG
944 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
945 if (status != TRF7970A_IRQ_STATUS_TX)
946 trf7970a_send_err_upstream(trf, -EIO);
947 break;
13b4272a
MG
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;
cb174aba
MG
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;
165063f1
MG
973 default:
974 dev_err(trf->dev, "%s - Driver in invalid state: %d\n",
975 __func__, trf->state);
976 }
977
b9e3016a 978err_unlock_exit:
165063f1
MG
979 mutex_unlock(&trf->lock);
980 return IRQ_HANDLED;
981}
982
9d9304b3
MG
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
165063f1
MG
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)
afa5b5f1 1019 trf7970a_drain_fifo(trf, TRF7970A_IRQ_STATUS_SRX);
9d9304b3
MG
1020 else if (trf->state == TRF7970A_ST_WAIT_TO_ISSUE_EOF)
1021 trf7970a_issue_eof(trf);
165063f1
MG
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
4e007f81
MG
1042 usleep_range(1000, 2000);
1043
7149d6bf
MG
1044 trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
1045
6c08df42
MG
1046 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL, 0);
1047 if (ret)
1048 goto err_out;
1049
1050 trf->modulator_sys_clk_ctrl = 0;
1051
165063f1
MG
1052 ret = trf7970a_write(trf, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS,
1053 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 |
1054 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32);
1055 if (ret)
1056 goto err_out;
1057
1058 ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, 0);
1059 if (ret)
1060 goto err_out;
1061
1062 trf->special_fcn_reg1 = 0;
1063
49d19cc7 1064 trf->iso_ctrl = 0xff;
165063f1
MG
1065 return 0;
1066
1067err_out:
1068 dev_dbg(trf->dev, "Couldn't init device: %d\n", ret);
1069 return ret;
1070}
1071
1072static void trf7970a_switch_rf_off(struct trf7970a *trf)
1073{
cfc708db
MG
1074 if ((trf->state == TRF7970A_ST_PWR_OFF) ||
1075 (trf->state == TRF7970A_ST_RF_OFF))
1076 return;
1077
165063f1
MG
1078 dev_dbg(trf->dev, "Switching rf off\n");
1079
a1d2dc5b
MG
1080 trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
1081
1082 trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, trf->chip_status_ctrl);
1083
165063f1 1084 trf->aborting = false;
b5e17d9b 1085 trf->state = TRF7970A_ST_RF_OFF;
e6403b7c
MG
1086
1087 pm_runtime_mark_last_busy(trf->dev);
1088 pm_runtime_put_autosuspend(trf->dev);
165063f1
MG
1089}
1090
0a1de842 1091static int trf7970a_switch_rf_on(struct trf7970a *trf)
165063f1 1092{
a08e5454
MG
1093 int ret;
1094
165063f1
MG
1095 dev_dbg(trf->dev, "Switching rf on\n");
1096
e6403b7c 1097 pm_runtime_get_sync(trf->dev);
165063f1 1098
ceccd6aa
MG
1099 if (trf->state != TRF7970A_ST_RF_OFF) { /* Power on, RF off */
1100 dev_err(trf->dev, "%s - Incorrect state: %d\n", __func__,
1101 trf->state);
1102 return -EINVAL;
1103 }
1104
a08e5454
MG
1105 ret = trf7970a_init(trf);
1106 if (ret) {
1107 dev_err(trf->dev, "%s - Can't initialize: %d\n", __func__, ret);
0a1de842 1108 return ret;
a08e5454
MG
1109 }
1110
e6403b7c 1111 trf->state = TRF7970A_ST_IDLE;
0a1de842
MG
1112
1113 return 0;
165063f1
MG
1114}
1115
1116static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
1117{
1118 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
0a1de842 1119 int ret = 0;
165063f1
MG
1120
1121 dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on);
1122
1123 mutex_lock(&trf->lock);
1124
1125 if (on) {
1126 switch (trf->state) {
ceccd6aa 1127 case TRF7970A_ST_PWR_OFF:
b5e17d9b 1128 case TRF7970A_ST_RF_OFF:
0a1de842 1129 ret = trf7970a_switch_rf_on(trf);
165063f1
MG
1130 break;
1131 case TRF7970A_ST_IDLE:
1132 case TRF7970A_ST_IDLE_RX_BLOCKED:
1133 break;
1134 default:
1135 dev_err(trf->dev, "%s - Invalid request: %d %d\n",
1136 __func__, trf->state, on);
1137 trf7970a_switch_rf_off(trf);
0a1de842 1138 ret = -EINVAL;
165063f1
MG
1139 }
1140 } else {
1141 switch (trf->state) {
ceccd6aa 1142 case TRF7970A_ST_PWR_OFF:
b5e17d9b 1143 case TRF7970A_ST_RF_OFF:
165063f1
MG
1144 break;
1145 default:
1146 dev_err(trf->dev, "%s - Invalid request: %d %d\n",
1147 __func__, trf->state, on);
0a1de842 1148 ret = -EINVAL;
165063f1
MG
1149 /* FALLTHROUGH */
1150 case TRF7970A_ST_IDLE:
1151 case TRF7970A_ST_IDLE_RX_BLOCKED:
13b4272a
MG
1152 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1153 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
165063f1
MG
1154 trf7970a_switch_rf_off(trf);
1155 }
1156 }
1157
1158 mutex_unlock(&trf->lock);
0a1de842 1159 return ret;
165063f1
MG
1160}
1161
307e5caf 1162static int trf7970a_in_config_rf_tech(struct trf7970a *trf, int tech)
165063f1
MG
1163{
1164 int ret = 0;
1165
1166 dev_dbg(trf->dev, "rf technology: %d\n", tech);
1167
1168 switch (tech) {
1169 case NFC_DIGITAL_RF_TECH_106A:
49d19cc7 1170 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443A_106;
12e9ade3 1171 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
4e64eff8 1172 trf->guard_time = TRF7970A_GUARD_TIME_NFCA;
165063f1 1173 break;
742b1f9f
MG
1174 case NFC_DIGITAL_RF_TECH_106B:
1175 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443B_106;
1176 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
4e64eff8 1177 trf->guard_time = TRF7970A_GUARD_TIME_NFCB;
742b1f9f 1178 break;
6857bb96
MG
1179 case NFC_DIGITAL_RF_TECH_212F:
1180 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_212;
1181 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
4e64eff8 1182 trf->guard_time = TRF7970A_GUARD_TIME_NFCF;
6857bb96
MG
1183 break;
1184 case NFC_DIGITAL_RF_TECH_424F:
1185 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_424;
1186 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
4e64eff8 1187 trf->guard_time = TRF7970A_GUARD_TIME_NFCF;
6857bb96 1188 break;
9d9304b3 1189 case NFC_DIGITAL_RF_TECH_ISO15693:
49d19cc7 1190 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
12e9ade3 1191 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
4e64eff8 1192 trf->guard_time = TRF7970A_GUARD_TIME_15693;
9d9304b3 1193 break;
165063f1
MG
1194 default:
1195 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
1196 return -EINVAL;
1197 }
1198
1199 trf->technology = tech;
1200
13b4272a
MG
1201 /* If in initiator mode and not changing the RF tech due to a
1202 * PSL sequence (indicated by 'trf->iso_ctrl == 0xff' from
1203 * trf7970a_init()), clear the NFC Target Detection Level register
1204 * due to erratum.
1205 */
1206 if (trf->iso_ctrl == 0xff)
1207 ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
1208
165063f1
MG
1209 return ret;
1210}
1211
851ee3cb
MG
1212static int trf7970a_is_rf_field(struct trf7970a *trf, bool *is_rf_field)
1213{
1214 int ret;
1215 u8 rssi;
1216
1217 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1218 trf->chip_status_ctrl | TRF7970A_CHIP_STATUS_REC_ON);
1219 if (ret)
1220 return ret;
1221
1222 ret = trf7970a_cmd(trf, TRF7970A_CMD_TEST_EXT_RF);
1223 if (ret)
1224 return ret;
1225
1226 usleep_range(50, 60);
1227
1228 ret = trf7970a_read(trf, TRF7970A_RSSI_OSC_STATUS, &rssi);
1229 if (ret)
1230 return ret;
1231
1232 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1233 trf->chip_status_ctrl);
1234 if (ret)
1235 return ret;
1236
1237 if (rssi & TRF7970A_RSSI_OSC_STATUS_RSSI_MASK)
1238 *is_rf_field = true;
1239 else
1240 *is_rf_field = false;
1241
1242 return 0;
1243}
1244
307e5caf 1245static int trf7970a_in_config_framing(struct trf7970a *trf, int framing)
165063f1 1246{
49d19cc7 1247 u8 iso_ctrl = trf->iso_ctrl_tech;
851ee3cb 1248 bool is_rf_field = false;
49d19cc7
MG
1249 int ret;
1250
165063f1
MG
1251 dev_dbg(trf->dev, "framing: %d\n", framing);
1252
1253 switch (framing) {
1254 case NFC_DIGITAL_FRAMING_NFCA_SHORT:
1255 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
1256 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
49d19cc7 1257 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
165063f1
MG
1258 break;
1259 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
80062891 1260 case NFC_DIGITAL_FRAMING_NFCA_T4T:
742b1f9f
MG
1261 case NFC_DIGITAL_FRAMING_NFCB:
1262 case NFC_DIGITAL_FRAMING_NFCB_T4T:
6857bb96
MG
1263 case NFC_DIGITAL_FRAMING_NFCF:
1264 case NFC_DIGITAL_FRAMING_NFCF_T3T:
9d9304b3
MG
1265 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY:
1266 case NFC_DIGITAL_FRAMING_ISO15693_T5T:
13b4272a
MG
1267 case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
1268 case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP:
165063f1 1269 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
49d19cc7 1270 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
165063f1
MG
1271 break;
1272 case NFC_DIGITAL_FRAMING_NFCA_T2T:
1273 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
49d19cc7 1274 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
165063f1
MG
1275 break;
1276 default:
1277 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
1278 return -EINVAL;
1279 }
1280
1281 trf->framing = framing;
1282
851ee3cb
MG
1283 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
1284 ret = trf7970a_is_rf_field(trf, &is_rf_field);
1285 if (ret)
1286 return ret;
1287
1288 if (is_rf_field)
1289 return -EBUSY;
1290 }
1291
49d19cc7
MG
1292 if (iso_ctrl != trf->iso_ctrl) {
1293 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1294 if (ret)
1295 return ret;
1296
1297 trf->iso_ctrl = iso_ctrl;
a0822a7e
MG
1298
1299 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
12e9ade3 1300 trf->modulator_sys_clk_ctrl);
a0822a7e
MG
1301 if (ret)
1302 return ret;
49d19cc7
MG
1303 }
1304
a1d2dc5b
MG
1305 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
1306 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1307 trf->chip_status_ctrl |
1308 TRF7970A_CHIP_STATUS_RF_ON);
1309 if (ret)
1310 return ret;
1311
1312 trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
1313
4e64eff8 1314 usleep_range(trf->guard_time, trf->guard_time + 1000);
a1d2dc5b
MG
1315 }
1316
49d19cc7 1317 return 0;
165063f1
MG
1318}
1319
1320static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type,
1321 int param)
1322{
1323 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
18422e68 1324 int ret;
165063f1
MG
1325
1326 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
1327
1328 mutex_lock(&trf->lock);
1329
13b4272a
MG
1330 trf->is_initiator = true;
1331
ceccd6aa
MG
1332 if ((trf->state == TRF7970A_ST_PWR_OFF) ||
1333 (trf->state == TRF7970A_ST_RF_OFF)) {
0a1de842
MG
1334 ret = trf7970a_switch_rf_on(trf);
1335 if (ret)
1336 goto err_unlock;
1337 }
165063f1
MG
1338
1339 switch (type) {
1340 case NFC_DIGITAL_CONFIG_RF_TECH:
307e5caf 1341 ret = trf7970a_in_config_rf_tech(trf, param);
165063f1
MG
1342 break;
1343 case NFC_DIGITAL_CONFIG_FRAMING:
307e5caf 1344 ret = trf7970a_in_config_framing(trf, param);
165063f1
MG
1345 break;
1346 default:
1347 dev_dbg(trf->dev, "Unknown type: %d\n", type);
1348 ret = -EINVAL;
1349 }
1350
0a1de842 1351err_unlock:
165063f1
MG
1352 mutex_unlock(&trf->lock);
1353 return ret;
1354}
1355
9d9304b3
MG
1356static int trf7970a_is_iso15693_write_or_lock(u8 cmd)
1357{
1358 switch (cmd) {
1359 case ISO15693_CMD_WRITE_SINGLE_BLOCK:
1360 case ISO15693_CMD_LOCK_BLOCK:
1361 case ISO15693_CMD_WRITE_MULTIPLE_BLOCK:
1362 case ISO15693_CMD_WRITE_AFI:
1363 case ISO15693_CMD_LOCK_AFI:
1364 case ISO15693_CMD_WRITE_DSFID:
1365 case ISO15693_CMD_LOCK_DSFID:
1366 return 1;
1367 break;
1368 default:
1369 return 0;
1370 }
1371}
1372
165063f1
MG
1373static int trf7970a_per_cmd_config(struct trf7970a *trf, struct sk_buff *skb)
1374{
1375 u8 *req = skb->data;
9d9304b3 1376 u8 special_fcn_reg1, iso_ctrl;
165063f1
MG
1377 int ret;
1378
9d9304b3
MG
1379 trf->issue_eof = false;
1380
165063f1
MG
1381 /* When issuing Type 2 read command, make sure the '4_bit_RX' bit in
1382 * special functions register 1 is cleared; otherwise, its a write or
1383 * sector select command and '4_bit_RX' must be set.
9d9304b3
MG
1384 *
1385 * When issuing an ISO 15693 command, inspect the flags byte to see
1386 * what speed to use. Also, remember if the OPTION flag is set on
1387 * a Type 5 write or lock command so the driver will know that it
1388 * has to send an EOF in order to get a response.
165063f1
MG
1389 */
1390 if ((trf->technology == NFC_DIGITAL_RF_TECH_106A) &&
1391 (trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T)) {
1392 if (req[0] == NFC_T2T_CMD_READ)
1393 special_fcn_reg1 = 0;
1394 else
1395 special_fcn_reg1 = TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX;
1396
1397 if (special_fcn_reg1 != trf->special_fcn_reg1) {
1398 ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1,
1399 special_fcn_reg1);
1400 if (ret)
1401 return ret;
1402
1403 trf->special_fcn_reg1 = special_fcn_reg1;
1404 }
9d9304b3
MG
1405 } else if (trf->technology == NFC_DIGITAL_RF_TECH_ISO15693) {
1406 iso_ctrl = trf->iso_ctrl & ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK;
1407
1408 switch (req[0] & ISO15693_REQ_FLAG_SPEED_MASK) {
1409 case 0x00:
1410 iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_662;
1411 break;
1412 case ISO15693_REQ_FLAG_SUB_CARRIER:
1413 iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a;
1414 break;
1415 case ISO15693_REQ_FLAG_DATA_RATE:
1416 iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
1417 break;
1418 case (ISO15693_REQ_FLAG_SUB_CARRIER |
1419 ISO15693_REQ_FLAG_DATA_RATE):
1420 iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669;
1421 break;
1422 }
1423
1424 if (iso_ctrl != trf->iso_ctrl) {
1425 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1426 if (ret)
1427 return ret;
1428
1429 trf->iso_ctrl = iso_ctrl;
1430 }
1431
1432 if ((trf->framing == NFC_DIGITAL_FRAMING_ISO15693_T5T) &&
1433 trf7970a_is_iso15693_write_or_lock(req[1]) &&
1434 (req[0] & ISO15693_REQ_FLAG_OPTION))
1435 trf->issue_eof = true;
165063f1
MG
1436 }
1437
1438 return 0;
1439}
1440
13b4272a 1441static int trf7970a_send_cmd(struct nfc_digital_dev *ddev,
165063f1
MG
1442 struct sk_buff *skb, u16 timeout,
1443 nfc_digital_cmd_complete_t cb, void *arg)
1444{
1445 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
7a1e5552 1446 u8 prefix[5];
165063f1
MG
1447 unsigned int len;
1448 int ret;
aff0564a 1449 u8 status;
165063f1
MG
1450
1451 dev_dbg(trf->dev, "New request - state: %d, timeout: %d ms, len: %d\n",
1452 trf->state, timeout, skb->len);
1453
1454 if (skb->len > TRF7970A_TX_MAX)
1455 return -EINVAL;
1456
1457 mutex_lock(&trf->lock);
1458
1459 if ((trf->state != TRF7970A_ST_IDLE) &&
1460 (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) {
1461 dev_err(trf->dev, "%s - Bogus state: %d\n", __func__,
1462 trf->state);
1463 ret = -EIO;
1464 goto out_err;
1465 }
1466
1467 if (trf->aborting) {
1468 dev_dbg(trf->dev, "Abort process complete\n");
1469 trf->aborting = false;
1470 ret = -ECANCELED;
1471 goto out_err;
1472 }
1473
1961843c
MG
1474 if (timeout) {
1475 trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE,
1476 GFP_KERNEL);
1477 if (!trf->rx_skb) {
1478 dev_dbg(trf->dev, "Can't alloc rx_skb\n");
1479 ret = -ENOMEM;
1480 goto out_err;
1481 }
165063f1
MG
1482 }
1483
1484 if (trf->state == TRF7970A_ST_IDLE_RX_BLOCKED) {
1485 ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX);
1486 if (ret)
1487 goto out_err;
1488
1489 trf->state = TRF7970A_ST_IDLE;
1490 }
1491
13b4272a
MG
1492 if (trf->is_initiator) {
1493 ret = trf7970a_per_cmd_config(trf, skb);
1494 if (ret)
1495 goto out_err;
1496 }
165063f1
MG
1497
1498 trf->ddev = ddev;
1499 trf->tx_skb = skb;
1500 trf->cb = cb;
1501 trf->cb_arg = arg;
1502 trf->timeout = timeout;
1503 trf->ignore_timeout = false;
1504
1505 len = skb->len;
165063f1
MG
1506
1507 /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
1508 * on what the current framing is, the address of the TX length byte 1
1509 * register (0x1d), and the 2 byte length of the data to be transmitted.
7a1e5552 1510 * That totals 5 bytes.
165063f1
MG
1511 */
1512 prefix[0] = TRF7970A_CMD_BIT_CTRL |
1513 TRF7970A_CMD_BIT_OPCODE(TRF7970A_CMD_FIFO_RESET);
1514 prefix[1] = TRF7970A_CMD_BIT_CTRL |
1515 TRF7970A_CMD_BIT_OPCODE(trf->tx_cmd);
1516 prefix[2] = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_TX_LENGTH_BYTE1;
1517
1518 if (trf->framing == NFC_DIGITAL_FRAMING_NFCA_SHORT) {
1519 prefix[3] = 0x00;
1520 prefix[4] = 0x0f; /* 7 bits */
1521 } else {
1522 prefix[3] = (len & 0xf00) >> 4;
1523 prefix[3] |= ((len & 0xf0) >> 4);
1524 prefix[4] = ((len & 0x0f) << 4);
1525 }
1526
1527 len = min_t(int, skb->len, TRF7970A_FIFO_SIZE);
1528
aff0564a
MG
1529 /* Clear possible spurious interrupt */
1530 ret = trf7970a_read_irqstatus(trf, &status);
1531 if (ret)
1532 goto out_err;
1533
7a1e5552 1534 ret = trf7970a_transmit(trf, skb, len, prefix, sizeof(prefix));
165063f1
MG
1535 if (ret) {
1536 kfree_skb(trf->rx_skb);
1537 trf->rx_skb = NULL;
1538 }
1539
1540out_err:
1541 mutex_unlock(&trf->lock);
1542 return ret;
1543}
1544
13b4272a 1545static int trf7970a_tg_config_rf_tech(struct trf7970a *trf, int tech)
165063f1 1546{
13b4272a
MG
1547 int ret = 0;
1548
1549 dev_dbg(trf->dev, "rf technology: %d\n", tech);
1550
1551 switch (tech) {
1552 case NFC_DIGITAL_RF_TECH_106A:
1553 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
1554 TRF7970A_ISO_CTRL_NFC_CE |
1555 TRF7970A_ISO_CTRL_NFC_CE_14443A;
1556 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
1557 break;
1558 case NFC_DIGITAL_RF_TECH_212F:
1559 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
1560 TRF7970A_ISO_CTRL_NFC_NFCF_212;
1561 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
1562 break;
1563 case NFC_DIGITAL_RF_TECH_424F:
1564 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
1565 TRF7970A_ISO_CTRL_NFC_NFCF_424;
1566 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
1567 break;
1568 default:
1569 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
1570 return -EINVAL;
1571 }
1572
1573 trf->technology = tech;
165063f1 1574
13b4272a
MG
1575 /* Normally we write the ISO_CTRL register in
1576 * trf7970a_tg_config_framing() because the framing can change
1577 * the value written. However, when sending a PSL RES,
1578 * digital_tg_send_psl_res_complete() doesn't call
1579 * trf7970a_tg_config_framing() so we must write the register
1580 * here.
1581 */
1582 if ((trf->framing == NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED) &&
1583 (trf->iso_ctrl_tech != trf->iso_ctrl)) {
1584 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL,
1585 trf->iso_ctrl_tech);
1586
1587 trf->iso_ctrl = trf->iso_ctrl_tech;
1588 }
165063f1 1589
13b4272a 1590 return ret;
165063f1
MG
1591}
1592
13b4272a
MG
1593/* Since this is a target routine, several of the framing calls are
1594 * made between receiving the request and sending the response so they
1595 * should take effect until after the response is sent. This is accomplished
1596 * by skipping the ISO_CTRL register write here and doing it in the interrupt
1597 * handler.
1598 */
1599static int trf7970a_tg_config_framing(struct trf7970a *trf, int framing)
1600{
1601 u8 iso_ctrl = trf->iso_ctrl_tech;
1602 int ret;
1603
1604 dev_dbg(trf->dev, "framing: %d\n", framing);
1605
1606 switch (framing) {
1607 case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
1608 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
1609 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
1610 break;
1611 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
1612 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
1613 case NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE:
1614 /* These ones are applied in the interrupt handler */
1615 iso_ctrl = trf->iso_ctrl; /* Don't write to ISO_CTRL yet */
1616 break;
1617 case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP:
1618 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1619 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
1620 break;
1621 case NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED:
1622 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1623 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
1624 break;
1625 default:
1626 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
1627 return -EINVAL;
1628 }
1629
1630 trf->framing = framing;
1631
1632 if (iso_ctrl != trf->iso_ctrl) {
1633 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1634 if (ret)
1635 return ret;
1636
1637 trf->iso_ctrl = iso_ctrl;
1638
1639 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
1640 trf->modulator_sys_clk_ctrl);
1641 if (ret)
1642 return ret;
1643 }
1644
1645 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
1646 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1647 trf->chip_status_ctrl |
1648 TRF7970A_CHIP_STATUS_RF_ON);
1649 if (ret)
1650 return ret;
1651
1652 trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
1653 }
1654
1655 return 0;
1656}
1657
1658static int trf7970a_tg_configure_hw(struct nfc_digital_dev *ddev, int type,
1659 int param)
165063f1
MG
1660{
1661 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
13b4272a
MG
1662 int ret;
1663
1664 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
1665
1666 mutex_lock(&trf->lock);
1667
1668 trf->is_initiator = false;
165063f1 1669
13b4272a
MG
1670 if ((trf->state == TRF7970A_ST_PWR_OFF) ||
1671 (trf->state == TRF7970A_ST_RF_OFF)) {
1672 ret = trf7970a_switch_rf_on(trf);
1673 if (ret)
1674 goto err_unlock;
1675 }
1676
1677 switch (type) {
1678 case NFC_DIGITAL_CONFIG_RF_TECH:
1679 ret = trf7970a_tg_config_rf_tech(trf, param);
1680 break;
1681 case NFC_DIGITAL_CONFIG_FRAMING:
1682 ret = trf7970a_tg_config_framing(trf, param);
1683 break;
1684 default:
1685 dev_dbg(trf->dev, "Unknown type: %d\n", type);
1686 ret = -EINVAL;
1687 }
165063f1 1688
13b4272a
MG
1689err_unlock:
1690 mutex_unlock(&trf->lock);
1691 return ret;
165063f1
MG
1692}
1693
cb174aba
MG
1694static int _trf7970a_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
1695 nfc_digital_cmd_complete_t cb, void *arg, bool mode_detect)
165063f1
MG
1696{
1697 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
13b4272a
MG
1698 int ret;
1699
13b4272a 1700 mutex_lock(&trf->lock);
165063f1 1701
13b4272a
MG
1702 if ((trf->state != TRF7970A_ST_IDLE) &&
1703 (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) {
1704 dev_err(trf->dev, "%s - Bogus state: %d\n", __func__,
1705 trf->state);
1706 ret = -EIO;
1707 goto out_err;
1708 }
1709
1710 if (trf->aborting) {
1711 dev_dbg(trf->dev, "Abort process complete\n");
1712 trf->aborting = false;
1713 ret = -ECANCELED;
1714 goto out_err;
1715 }
1716
1717 trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE,
1718 GFP_KERNEL);
1719 if (!trf->rx_skb) {
1720 dev_dbg(trf->dev, "Can't alloc rx_skb\n");
1721 ret = -ENOMEM;
1722 goto out_err;
1723 }
1724
1725 ret = trf7970a_write(trf, TRF7970A_RX_SPECIAL_SETTINGS,
1726 TRF7970A_RX_SPECIAL_SETTINGS_HBT |
1727 TRF7970A_RX_SPECIAL_SETTINGS_M848 |
1728 TRF7970A_RX_SPECIAL_SETTINGS_C424 |
1729 TRF7970A_RX_SPECIAL_SETTINGS_C212);
1730 if (ret)
fc0ae243 1731 goto out_err;
13b4272a
MG
1732
1733 ret = trf7970a_write(trf, TRF7970A_REG_IO_CTRL,
1734 TRF7970A_REG_IO_CTRL_VRS(0x1));
1735 if (ret)
fc0ae243 1736 goto out_err;
165063f1 1737
13b4272a
MG
1738 ret = trf7970a_write(trf, TRF7970A_NFC_LOW_FIELD_LEVEL,
1739 TRF7970A_NFC_LOW_FIELD_LEVEL_RFDET(0x3));
1740 if (ret)
fc0ae243 1741 goto out_err;
13b4272a
MG
1742
1743 ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL,
1744 TRF7970A_NFC_TARGET_LEVEL_RFDET(0x7));
1745 if (ret)
fc0ae243 1746 goto out_err;
13b4272a
MG
1747
1748 trf->ddev = ddev;
1749 trf->cb = cb;
1750 trf->cb_arg = arg;
1751 trf->timeout = timeout;
1752 trf->ignore_timeout = false;
1753
1754 ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX);
1755 if (ret)
1756 goto out_err;
1757
cb174aba
MG
1758 trf->state = mode_detect ? TRF7970A_ST_LISTENING_MD :
1759 TRF7970A_ST_LISTENING;
13b4272a
MG
1760
1761 schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout));
1762
1763out_err:
1764 mutex_unlock(&trf->lock);
1765 return ret;
165063f1
MG
1766}
1767
cb174aba
MG
1768static int trf7970a_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
1769 nfc_digital_cmd_complete_t cb, void *arg)
1770{
1771 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1772
1773 dev_dbg(trf->dev, "Listen - state: %d, timeout: %d ms\n",
1774 trf->state, timeout);
1775
1776 return _trf7970a_tg_listen(ddev, timeout, cb, arg, false);
1777}
1778
1779static int trf7970a_tg_listen_md(struct nfc_digital_dev *ddev,
1780 u16 timeout, nfc_digital_cmd_complete_t cb, void *arg)
1781{
1782 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1783 int ret;
1784
1785 dev_dbg(trf->dev, "Listen MD - state: %d, timeout: %d ms\n",
1786 trf->state, timeout);
1787
1788 ret = trf7970a_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
1789 NFC_DIGITAL_RF_TECH_106A);
1790 if (ret)
1791 return ret;
1792
1793 ret = trf7970a_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1794 NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
1795 if (ret)
1796 return ret;
1797
1798 return _trf7970a_tg_listen(ddev, timeout, cb, arg, true);
1799}
1800
1801static int trf7970a_tg_get_rf_tech(struct nfc_digital_dev *ddev, u8 *rf_tech)
1802{
1803 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1804
1805 dev_dbg(trf->dev, "Get RF Tech - state: %d, rf_tech: %d\n",
1806 trf->state, trf->md_rf_tech);
1807
1808 *rf_tech = trf->md_rf_tech;
1809
1810 return 0;
1811}
1812
165063f1
MG
1813static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev)
1814{
1815 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1816
1817 dev_dbg(trf->dev, "Abort process initiated\n");
1818
1819 mutex_lock(&trf->lock);
5876bc75
MG
1820
1821 switch (trf->state) {
1822 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
1823 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1824 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1825 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
1826 trf->aborting = true;
1827 break;
13b4272a
MG
1828 case TRF7970A_ST_LISTENING:
1829 trf->ignore_timeout = !cancel_delayed_work(&trf->timeout_work);
1830 trf7970a_send_err_upstream(trf, -ECANCELED);
1831 dev_dbg(trf->dev, "Abort process complete\n");
1832 break;
5876bc75
MG
1833 default:
1834 break;
1835 }
1836
165063f1
MG
1837 mutex_unlock(&trf->lock);
1838}
1839
1840static struct nfc_digital_ops trf7970a_nfc_ops = {
1841 .in_configure_hw = trf7970a_in_configure_hw,
13b4272a 1842 .in_send_cmd = trf7970a_send_cmd,
165063f1 1843 .tg_configure_hw = trf7970a_tg_configure_hw,
13b4272a 1844 .tg_send_cmd = trf7970a_send_cmd,
165063f1 1845 .tg_listen = trf7970a_tg_listen,
cb174aba
MG
1846 .tg_listen_md = trf7970a_tg_listen_md,
1847 .tg_get_rf_tech = trf7970a_tg_get_rf_tech,
165063f1
MG
1848 .switch_rf = trf7970a_switch_rf,
1849 .abort_cmd = trf7970a_abort_cmd,
1850};
1851
ceccd6aa
MG
1852static int trf7970a_power_up(struct trf7970a *trf)
1853{
1854 int ret;
1855
1856 dev_dbg(trf->dev, "Powering up - state: %d\n", trf->state);
1857
1858 if (trf->state != TRF7970A_ST_PWR_OFF)
1859 return 0;
1860
1861 ret = regulator_enable(trf->regulator);
1862 if (ret) {
1863 dev_err(trf->dev, "%s - Can't enable VIN: %d\n", __func__, ret);
1864 return ret;
1865 }
1866
1867 usleep_range(5000, 6000);
1868
1869 if (!(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW)) {
1870 gpio_set_value(trf->en2_gpio, 1);
1871 usleep_range(1000, 2000);
1872 }
1873
1874 gpio_set_value(trf->en_gpio, 1);
1875
1876 usleep_range(20000, 21000);
1877
1878 trf->state = TRF7970A_ST_RF_OFF;
1879
1880 return 0;
1881}
1882
1883static int trf7970a_power_down(struct trf7970a *trf)
1884{
1885 int ret;
1886
1887 dev_dbg(trf->dev, "Powering down - state: %d\n", trf->state);
1888
1889 if (trf->state == TRF7970A_ST_PWR_OFF)
1890 return 0;
1891
1892 if (trf->state != TRF7970A_ST_RF_OFF) {
1893 dev_dbg(trf->dev, "Can't power down - not RF_OFF state (%d)\n",
1894 trf->state);
1895 return -EBUSY;
1896 }
1897
1898 gpio_set_value(trf->en_gpio, 0);
1899 gpio_set_value(trf->en2_gpio, 0);
1900
1901 ret = regulator_disable(trf->regulator);
1902 if (ret)
1903 dev_err(trf->dev, "%s - Can't disable VIN: %d\n", __func__,
1904 ret);
1905
1906 trf->state = TRF7970A_ST_PWR_OFF;
1907
1908 return ret;
1909}
1910
b528281b
MG
1911static int trf7970a_startup(struct trf7970a *trf)
1912{
1913 int ret;
1914
1915 ret = trf7970a_power_up(trf);
1916 if (ret)
1917 return ret;
1918
1919 pm_runtime_set_active(trf->dev);
1920 pm_runtime_enable(trf->dev);
1921 pm_runtime_mark_last_busy(trf->dev);
1922
1923 return 0;
1924}
1925
1926static void trf7970a_shutdown(struct trf7970a *trf)
1927{
1928 switch (trf->state) {
1929 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
1930 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1931 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1932 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
13b4272a 1933 case TRF7970A_ST_LISTENING:
b528281b
MG
1934 trf7970a_send_err_upstream(trf, -ECANCELED);
1935 /* FALLTHROUGH */
1936 case TRF7970A_ST_IDLE:
1937 case TRF7970A_ST_IDLE_RX_BLOCKED:
1938 trf7970a_switch_rf_off(trf);
1939 break;
1940 default:
1941 break;
1942 }
1943
1944 pm_runtime_disable(trf->dev);
1945 pm_runtime_set_suspended(trf->dev);
1946
1947 trf7970a_power_down(trf);
1948}
1949
fd0c8280
MG
1950static int trf7970a_get_autosuspend_delay(struct device_node *np)
1951{
1952 int autosuspend_delay, ret;
1953
1954 ret = of_property_read_u32(np, "autosuspend-delay", &autosuspend_delay);
1955 if (ret)
1956 autosuspend_delay = TRF7970A_AUTOSUSPEND_DELAY;
1957
fd0c8280
MG
1958 return autosuspend_delay;
1959}
1960
3bd14233
MG
1961static int trf7970a_get_vin_voltage_override(struct device_node *np,
1962 u32 *vin_uvolts)
1963{
1964 return of_property_read_u32(np, "vin-voltage-override", vin_uvolts);
1965}
1966
165063f1
MG
1967static int trf7970a_probe(struct spi_device *spi)
1968{
1969 struct device_node *np = spi->dev.of_node;
165063f1 1970 struct trf7970a *trf;
fd0c8280 1971 int uvolts, autosuspend_delay, ret;
165063f1
MG
1972
1973 if (!np) {
1974 dev_err(&spi->dev, "No Device Tree entry\n");
1975 return -EINVAL;
1976 }
1977
1978 trf = devm_kzalloc(&spi->dev, sizeof(*trf), GFP_KERNEL);
1979 if (!trf)
1980 return -ENOMEM;
1981
ceccd6aa 1982 trf->state = TRF7970A_ST_PWR_OFF;
165063f1
MG
1983 trf->dev = &spi->dev;
1984 trf->spi = spi;
165063f1
MG
1985
1986 spi->mode = SPI_MODE_1;
1987 spi->bits_per_word = 8;
1988
24707296
MG
1989 ret = spi_setup(spi);
1990 if (ret < 0) {
1991 dev_err(trf->dev, "Can't set up SPI Communication\n");
1992 return ret;
1993 }
1994
772079eb
MG
1995 if (of_property_read_bool(np, "irq-status-read-quirk"))
1996 trf->quirks |= TRF7970A_QUIRK_IRQ_STATUS_READ;
1997
165063f1
MG
1998 /* There are two enable pins - both must be present */
1999 trf->en_gpio = of_get_named_gpio(np, "ti,enable-gpios", 0);
2000 if (!gpio_is_valid(trf->en_gpio)) {
2001 dev_err(trf->dev, "No EN GPIO property\n");
2002 return trf->en_gpio;
2003 }
2004
2005 ret = devm_gpio_request_one(trf->dev, trf->en_gpio,
f23b7352 2006 GPIOF_DIR_OUT | GPIOF_INIT_LOW, "trf7970a EN");
165063f1
MG
2007 if (ret) {
2008 dev_err(trf->dev, "Can't request EN GPIO: %d\n", ret);
2009 return ret;
2010 }
2011
2012 trf->en2_gpio = of_get_named_gpio(np, "ti,enable-gpios", 1);
2013 if (!gpio_is_valid(trf->en2_gpio)) {
2014 dev_err(trf->dev, "No EN2 GPIO property\n");
2015 return trf->en2_gpio;
2016 }
2017
2018 ret = devm_gpio_request_one(trf->dev, trf->en2_gpio,
f23b7352 2019 GPIOF_DIR_OUT | GPIOF_INIT_LOW, "trf7970a EN2");
165063f1
MG
2020 if (ret) {
2021 dev_err(trf->dev, "Can't request EN2 GPIO: %d\n", ret);
2022 return ret;
2023 }
2024
95064bd9
MG
2025 if (of_property_read_bool(np, "en2-rf-quirk"))
2026 trf->quirks |= TRF7970A_QUIRK_EN2_MUST_STAY_LOW;
2027
165063f1
MG
2028 ret = devm_request_threaded_irq(trf->dev, spi->irq, NULL,
2029 trf7970a_irq, IRQF_TRIGGER_RISING | IRQF_ONESHOT,
2030 "trf7970a", trf);
2031 if (ret) {
2032 dev_err(trf->dev, "Can't request IRQ#%d: %d\n", spi->irq, ret);
2033 return ret;
2034 }
2035
2036 mutex_init(&trf->lock);
2037 INIT_DELAYED_WORK(&trf->timeout_work, trf7970a_timeout_work_handler);
2038
2039 trf->regulator = devm_regulator_get(&spi->dev, "vin");
2040 if (IS_ERR(trf->regulator)) {
2041 ret = PTR_ERR(trf->regulator);
2042 dev_err(trf->dev, "Can't get VIN regulator: %d\n", ret);
2043 goto err_destroy_lock;
2044 }
2045
2046 ret = regulator_enable(trf->regulator);
2047 if (ret) {
2048 dev_err(trf->dev, "Can't enable VIN: %d\n", ret);
2049 goto err_destroy_lock;
2050 }
2051
3bd14233
MG
2052 ret = trf7970a_get_vin_voltage_override(np, &uvolts);
2053 if (ret)
2054 uvolts = regulator_get_voltage(trf->regulator);
ebcc5a0d
MG
2055
2056 if (uvolts > 4000000)
2057 trf->chip_status_ctrl = TRF7970A_CHIP_STATUS_VRS5_3;
2058
165063f1
MG
2059 trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops,
2060 TRF7970A_SUPPORTED_PROTOCOLS,
13b4272a
MG
2061 NFC_DIGITAL_DRV_CAPS_IN_CRC |
2062 NFC_DIGITAL_DRV_CAPS_TG_CRC, 0, 0);
165063f1
MG
2063 if (!trf->ddev) {
2064 dev_err(trf->dev, "Can't allocate NFC digital device\n");
2065 ret = -ENOMEM;
2066 goto err_disable_regulator;
2067 }
2068
2069 nfc_digital_set_parent_dev(trf->ddev, trf->dev);
2070 nfc_digital_set_drvdata(trf->ddev, trf);
2071 spi_set_drvdata(spi, trf);
2072
fd0c8280
MG
2073 autosuspend_delay = trf7970a_get_autosuspend_delay(np);
2074
2075 pm_runtime_set_autosuspend_delay(trf->dev, autosuspend_delay);
e6403b7c 2076 pm_runtime_use_autosuspend(trf->dev);
ceccd6aa 2077
b528281b 2078 ret = trf7970a_startup(trf);
ceccd6aa
MG
2079 if (ret)
2080 goto err_free_ddev;
2081
165063f1
MG
2082 ret = nfc_digital_register_device(trf->ddev);
2083 if (ret) {
2084 dev_err(trf->dev, "Can't register NFC digital device: %d\n",
2085 ret);
b528281b 2086 goto err_shutdown;
165063f1
MG
2087 }
2088
2089 return 0;
2090
b528281b
MG
2091err_shutdown:
2092 trf7970a_shutdown(trf);
ceccd6aa 2093err_free_ddev:
165063f1
MG
2094 nfc_digital_free_device(trf->ddev);
2095err_disable_regulator:
2096 regulator_disable(trf->regulator);
2097err_destroy_lock:
2098 mutex_destroy(&trf->lock);
2099 return ret;
2100}
2101
2102static int trf7970a_remove(struct spi_device *spi)
2103{
2104 struct trf7970a *trf = spi_get_drvdata(spi);
2105
2106 mutex_lock(&trf->lock);
2107
b528281b 2108 trf7970a_shutdown(trf);
ceccd6aa
MG
2109
2110 mutex_unlock(&trf->lock);
e6403b7c 2111
165063f1
MG
2112 nfc_digital_unregister_device(trf->ddev);
2113 nfc_digital_free_device(trf->ddev);
2114
2115 regulator_disable(trf->regulator);
2116
2117 mutex_destroy(&trf->lock);
2118
2119 return 0;
2120}
2121
77c9539d
MG
2122#ifdef CONFIG_PM_SLEEP
2123static int trf7970a_suspend(struct device *dev)
2124{
2125 struct spi_device *spi = container_of(dev, struct spi_device, dev);
2126 struct trf7970a *trf = spi_get_drvdata(spi);
77c9539d
MG
2127
2128 dev_dbg(dev, "Suspend\n");
2129
2130 mutex_lock(&trf->lock);
2131
2132 trf7970a_shutdown(trf);
2133
2134 mutex_unlock(&trf->lock);
2135
671970f5 2136 return 0;
77c9539d
MG
2137}
2138
2139static int trf7970a_resume(struct device *dev)
2140{
2141 struct spi_device *spi = container_of(dev, struct spi_device, dev);
2142 struct trf7970a *trf = spi_get_drvdata(spi);
55ef2e75 2143 int ret;
77c9539d
MG
2144
2145 dev_dbg(dev, "Resume\n");
2146
2147 mutex_lock(&trf->lock);
2148
2149 ret = trf7970a_startup(trf);
2150
2151 mutex_unlock(&trf->lock);
2152
2153 return ret;
2154}
2155#endif
2156
e8a9235c 2157#ifdef CONFIG_PM
e6403b7c
MG
2158static int trf7970a_pm_runtime_suspend(struct device *dev)
2159{
2160 struct spi_device *spi = container_of(dev, struct spi_device, dev);
2161 struct trf7970a *trf = spi_get_drvdata(spi);
2162 int ret;
2163
2164 dev_dbg(dev, "Runtime suspend\n");
2165
ceccd6aa 2166 mutex_lock(&trf->lock);
e6403b7c 2167
ceccd6aa 2168 ret = trf7970a_power_down(trf);
e6403b7c 2169
ceccd6aa 2170 mutex_unlock(&trf->lock);
e6403b7c
MG
2171
2172 return ret;
2173}
2174
2175static int trf7970a_pm_runtime_resume(struct device *dev)
2176{
2177 struct spi_device *spi = container_of(dev, struct spi_device, dev);
2178 struct trf7970a *trf = spi_get_drvdata(spi);
2179 int ret;
2180
2181 dev_dbg(dev, "Runtime resume\n");
2182
ceccd6aa
MG
2183 ret = trf7970a_power_up(trf);
2184 if (!ret)
2185 pm_runtime_mark_last_busy(dev);
e6403b7c 2186
ceccd6aa 2187 return ret;
e6403b7c
MG
2188}
2189#endif
2190
2191static const struct dev_pm_ops trf7970a_pm_ops = {
77c9539d 2192 SET_SYSTEM_SLEEP_PM_OPS(trf7970a_suspend, trf7970a_resume)
e6403b7c
MG
2193 SET_RUNTIME_PM_OPS(trf7970a_pm_runtime_suspend,
2194 trf7970a_pm_runtime_resume, NULL)
2195};
2196
165063f1 2197static const struct spi_device_id trf7970a_id_table[] = {
772079eb 2198 { "trf7970a", 0 },
165063f1
MG
2199 { }
2200};
2201MODULE_DEVICE_TABLE(spi, trf7970a_id_table);
2202
2203static struct spi_driver trf7970a_spi_driver = {
2204 .probe = trf7970a_probe,
2205 .remove = trf7970a_remove,
2206 .id_table = trf7970a_id_table,
2207 .driver = {
2208 .name = "trf7970a",
2209 .owner = THIS_MODULE,
e6403b7c 2210 .pm = &trf7970a_pm_ops,
165063f1
MG
2211 },
2212};
2213
2214module_spi_driver(trf7970a_spi_driver);
2215
2216MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>");
2217MODULE_LICENSE("GPL v2");
2218MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver");
This page took 0.263298 seconds and 5 git commands to generate.