blob: eee5cc1a9220453139eb0e16f8ff732555f64438 [file] [log] [blame]
Mark A. Greer165063f2014-03-10 11:56:22 -07001/*
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>
Mark A. Greere6403b72014-03-25 08:54:38 -070019#include <linux/pm_runtime.h>
Mark A. Greer165063f2014-03-10 11:56:22 -070020#include <linux/nfc.h>
21#include <linux/skbuff.h>
22#include <linux/delay.h>
Mark Greerd34e48d2017-04-25 15:43:55 -070023#include <linux/gpio/consumer.h>
Mark A. Greer165063f2014-03-10 11:56:22 -070024#include <linux/of.h>
Mark A. Greer165063f2014-03-10 11:56:22 -070025#include <linux/spi/spi.h>
26#include <linux/regulator/consumer.h>
27
28#include <net/nfc/nfc.h>
29#include <net/nfc/digital.h>
30
31/* There are 3 ways the host can communicate with the trf7970a:
32 * parallel mode, SPI with Slave Select (SS) mode, and SPI without
33 * SS mode. The driver only supports the two SPI modes.
34 *
35 * The trf7970a is very timing sensitive and the VIN, EN2, and EN
36 * pins must asserted in that order and with specific delays in between.
37 * The delays used in the driver were provided by TI and have been
Mark A. Greer95064bd2014-08-07 17:41:45 -070038 * confirmed to work with this driver. There is a bug with the current
39 * version of the trf7970a that requires that EN2 remain low no matter
40 * what. If it goes high, it will generate an RF field even when in
41 * passive target mode. TI has indicated that the chip will work okay
42 * when EN2 is left low. The 'en2-rf-quirk' device tree property
43 * indicates that trf7970a currently being used has the erratum and
44 * that EN2 must be kept low.
Mark A. Greer165063f2014-03-10 11:56:22 -070045 *
46 * Timeouts are implemented using the delayed workqueue kernel facility.
47 * Timeouts are required so things don't hang when there is no response
48 * from the trf7970a (or tag). Using this mechanism creates a race with
49 * interrupts, however. That is, an interrupt and a timeout could occur
50 * closely enough together that one is blocked by the mutex while the other
51 * executes. When the timeout handler executes first and blocks the
52 * interrupt handler, it will eventually set the state to IDLE so the
53 * interrupt handler will check the state and exit with no harm done.
54 * When the interrupt handler executes first and blocks the timeout handler,
55 * the cancel_delayed_work() call will know that it didn't cancel the
56 * work item (i.e., timeout) and will return zero. That return code is
57 * used by the timer handler to indicate that it should ignore the timeout
58 * once its unblocked.
59 *
60 * Aborting an active command isn't as simple as it seems because the only
61 * way to abort a command that's already been sent to the tag is so turn
62 * off power to the tag. If we do that, though, we'd have to go through
63 * the entire anticollision procedure again but the digital layer doesn't
Mark A. Greer13b42722014-09-02 15:12:45 -070064 * support that. So, if an abort is received before trf7970a_send_cmd()
Mark A. Greer165063f2014-03-10 11:56:22 -070065 * has sent the command to the tag, it simply returns -ECANCELED. If the
66 * command has already been sent to the tag, then the driver continues
67 * normally and recieves the response data (or error) but just before
68 * sending the data upstream, it frees the rx_skb and sends -ECANCELED
69 * upstream instead. If the command failed, that error will be sent
70 * upstream.
71 *
72 * When recieving data from a tag and the interrupt status register has
73 * only the SRX bit set, it means that all of the data has been received
74 * (once what's in the fifo has been read). However, depending on timing
75 * an interrupt status with only the SRX bit set may not be recived. In
Mark A. Greer5fa3af32014-03-25 08:54:29 -070076 * those cases, the timeout mechanism is used to wait 20 ms in case more
77 * data arrives. After 20 ms, it is assumed that all of the data has been
Mark A. Greer165063f2014-03-10 11:56:22 -070078 * received and the accumulated rx data is sent upstream. The
79 * 'TRF7970A_ST_WAIT_FOR_RX_DATA_CONT' state is used for this purpose
80 * (i.e., it indicates that some data has been received but we're not sure
81 * if there is more coming so a timeout in this state means all data has
Mark A. Greer5fa3af32014-03-25 08:54:29 -070082 * been received and there isn't an error). The delay is 20 ms since delays
83 * of ~16 ms have been observed during testing.
Mark A. Greer165063f2014-03-10 11:56:22 -070084 *
Mark A. Greer38b4eb12014-09-02 15:12:25 -070085 * When transmitting a frame larger than the FIFO size (127 bytes), the
86 * driver will wait 20 ms for the FIFO to drain past the low-watermark
87 * and generate an interrupt. The low-watermark set to 32 bytes so the
88 * interrupt should fire after 127 - 32 = 95 bytes have been sent. At
89 * the lowest possible bit rate (6.62 kbps for 15693), it will take up
90 * to ~14.35 ms so 20 ms is used for the timeout.
91 *
Mark A. Greer165063f2014-03-10 11:56:22 -070092 * Type 2 write and sector select commands respond with a 4-bit ACK or NACK.
93 * Having only 4 bits in the FIFO won't normally generate an interrupt so
94 * driver enables the '4_bit_RX' bit of the Special Functions register 1
95 * to cause an interrupt in that case. Leaving that bit for a read command
96 * messes up the data returned so it is only enabled when the framing is
97 * 'NFC_DIGITAL_FRAMING_NFCA_T2T' and the command is not a read command.
98 * Unfortunately, that means that the driver has to peek into tx frames
99 * when the framing is 'NFC_DIGITAL_FRAMING_NFCA_T2T'. This is done by
100 * the trf7970a_per_cmd_config() routine.
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700101 *
102 * ISO/IEC 15693 frames specify whether to use single or double sub-carrier
103 * frequencies and whether to use low or high data rates in the flags byte
104 * of the frame. This means that the driver has to peek at all 15693 frames
105 * to determine what speed to set the communication to. In addition, write
106 * and lock commands use the OPTION flag to indicate that an EOF must be
107 * sent to the tag before it will send its response. So the driver has to
108 * examine all frames for that reason too.
109 *
110 * It is unclear how long to wait before sending the EOF. According to the
111 * Note under Table 1-1 in section 1.6 of
112 * http://www.ti.com/lit/ug/scbu011/scbu011.pdf, that wait should be at least
113 * 10 ms for TI Tag-it HF-I tags; however testing has shown that is not long
Mark A. Greer38b4eb12014-09-02 15:12:25 -0700114 * enough so 20 ms is used. So the timer is set to 40 ms - 20 ms to drain
115 * up to 127 bytes in the FIFO at the lowest bit rate plus another 20 ms to
116 * ensure the wait is long enough before sending the EOF. This seems to work
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700117 * reliably.
Mark A. Greer165063f2014-03-10 11:56:22 -0700118 */
119
Mark A. Greer80062892014-03-10 11:56:23 -0700120#define TRF7970A_SUPPORTED_PROTOCOLS \
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700121 (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \
Mark A. Greer6857bb92014-04-14 10:04:10 -0700122 NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_FELICA_MASK | \
Mark A. Greer13b42722014-09-02 15:12:45 -0700123 NFC_PROTO_ISO15693_MASK | NFC_PROTO_NFC_DEP_MASK)
Mark A. Greer165063f2014-03-10 11:56:22 -0700124
Mark Greere2f0f672017-04-25 15:43:56 -0700125#define TRF7970A_AUTOSUSPEND_DELAY 30000 /* 30 seconds */
Geoff Lansberry837eb4d2016-12-21 23:18:32 -0500126#define TRF7970A_13MHZ_CLOCK_FREQUENCY 13560000
127#define TRF7970A_27MHZ_CLOCK_FREQUENCY 27120000
128
Mark A. Greer165063f2014-03-10 11:56:22 -0700129#define TRF7970A_RX_SKB_ALLOC_SIZE 256
130
Mark A. Greer1568bfe2014-09-02 15:12:20 -0700131#define TRF7970A_FIFO_SIZE 127
Mark A. Greer165063f2014-03-10 11:56:22 -0700132
133/* TX length is 3 nibbles long ==> 4KB - 1 bytes max */
134#define TRF7970A_TX_MAX (4096 - 1)
135
Mark A. Greer19618432014-09-02 15:12:44 -0700136#define TRF7970A_WAIT_FOR_TX_IRQ 20
Mark A. Greer5fa3af32014-03-25 08:54:29 -0700137#define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 20
Mark A. Greer38b4eb12014-09-02 15:12:25 -0700138#define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 20
139#define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 40
Mark A. Greer165063f2014-03-10 11:56:22 -0700140
Mark A. Greer4e64eff2014-09-02 15:12:24 -0700141/* Guard times for various RF technologies (in us) */
142#define TRF7970A_GUARD_TIME_NFCA 5000
143#define TRF7970A_GUARD_TIME_NFCB 5000
144#define TRF7970A_GUARD_TIME_NFCF 20000
145#define TRF7970A_GUARD_TIME_15693 1000
146
Mark A. Greer165063f2014-03-10 11:56:22 -0700147/* Quirks */
148/* Erratum: When reading IRQ Status register on trf7970a, we must issue a
149 * read continuous command for IRQ Status and Collision Position registers.
150 */
Mark A. Greer772079e2014-08-07 17:41:43 -0700151#define TRF7970A_QUIRK_IRQ_STATUS_READ BIT(0)
Mark A. Greer95064bd2014-08-07 17:41:45 -0700152#define TRF7970A_QUIRK_EN2_MUST_STAY_LOW BIT(1)
Mark A. Greer165063f2014-03-10 11:56:22 -0700153
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
Mark A. Greer851ee3c2014-09-02 15:12:42 -0700169#define TRF7970A_CMD_TEST_INT_RF 0x18
170#define TRF7970A_CMD_TEST_EXT_RF 0x19
Mark A. Greer165063f2014-03-10 11:56:22 -0700171#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
Mark A. Greer13b42722014-09-02 15:12:45 -0700244#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)
Mark A. Greer165063f2014-03-10 11:56:22 -0700253#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
Mark A. Greer13b42722014-09-02 15:12:45 -0700278#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
Mark A. Greer165063f2014-03-10 11:56:22 -0700294/* IRQ Status Register Bits */
Mark Greere2f0f672017-04-25 15:43:56 -0700295#define TRF7970A_IRQ_STATUS_NORESP BIT(0) /* ISO15693 only */
Mark A. Greer13b42722014-09-02 15:12:45 -0700296#define TRF7970A_IRQ_STATUS_NFC_COL_ERROR BIT(0)
Mark A. Greer165063f2014-03-10 11:56:22 -0700297#define TRF7970A_IRQ_STATUS_COL BIT(1)
298#define TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR BIT(2)
Mark A. Greer13b42722014-09-02 15:12:45 -0700299#define TRF7970A_IRQ_STATUS_NFC_RF BIT(2)
Mark A. Greer165063f2014-03-10 11:56:22 -0700300#define TRF7970A_IRQ_STATUS_PARITY_ERROR BIT(3)
Mark A. Greer13b42722014-09-02 15:12:45 -0700301#define TRF7970A_IRQ_STATUS_NFC_SDD BIT(3)
Mark A. Greer165063f2014-03-10 11:56:22 -0700302#define TRF7970A_IRQ_STATUS_CRC_ERROR BIT(4)
Mark A. Greer13b42722014-09-02 15:12:45 -0700303#define TRF7970A_IRQ_STATUS_NFC_PROTO_ERROR BIT(4)
Mark A. Greer165063f2014-03-10 11:56:22 -0700304#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
Mark A. Greer851ee3c2014-09-02 15:12:42 -0700314#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
Mark A. Greer165063f2014-03-10 11:56:22 -0700318#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
Mark A. Greer13b42722014-09-02 15:12:45 -0700334#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)
Mark Greerae291f72015-08-19 08:57:58 -0700339#define TRF7970A_NFC_TARGET_LEVEL_SDD_EN BIT(5)
Mark A. Greer13b42722014-09-02 15:12:45 -0700340#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
Mark A. Greercb174ab2014-09-02 15:12:46 -0700344#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
Mark A. Greer165063f2014-03-10 11:56:22 -0700377#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
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700382/* 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
Mark A. Greer165063f2014-03-10 11:56:22 -0700412enum trf7970a_state {
Mark A. Greerceccd6a2014-09-02 15:12:35 -0700413 TRF7970A_ST_PWR_OFF,
Mark A. Greerb5e17d92014-09-02 15:12:34 -0700414 TRF7970A_ST_RF_OFF,
Mark A. Greer165063f2014-03-10 11:56:22 -0700415 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,
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700420 TRF7970A_ST_WAIT_TO_ISSUE_EOF,
Mark A. Greer13b42722014-09-02 15:12:45 -0700421 TRF7970A_ST_LISTENING,
Mark A. Greercb174ab2014-09-02 15:12:46 -0700422 TRF7970A_ST_LISTENING_MD,
Mark A. Greer165063f2014-03-10 11:56:22 -0700423 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;
Mark A. Greer13b42722014-09-02 15:12:45 -0700433 bool is_initiator;
Mark A. Greer165063f2014-03-10 11:56:22 -0700434 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;
Mark A. Greerebcc5a02014-03-25 08:54:36 -0700439 u8 chip_status_ctrl;
Mark A. Greer165063f2014-03-10 11:56:22 -0700440 u8 iso_ctrl;
Mark A. Greer49d19cc2014-03-25 08:54:33 -0700441 u8 iso_ctrl_tech;
Mark A. Greer12e9ade2014-03-25 08:54:35 -0700442 u8 modulator_sys_clk_ctrl;
Mark A. Greer165063f2014-03-10 11:56:22 -0700443 u8 special_fcn_reg1;
Geoff Lansberry49d22c72016-12-21 23:18:33 -0500444 u8 io_ctrl;
Mark A. Greer4e64eff2014-09-02 15:12:24 -0700445 unsigned int guard_time;
Mark A. Greer165063f2014-03-10 11:56:22 -0700446 int technology;
447 int framing;
Mark A. Greercb174ab2014-09-02 15:12:46 -0700448 u8 md_rf_tech;
Mark A. Greer165063f2014-03-10 11:56:22 -0700449 u8 tx_cmd;
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700450 bool issue_eof;
Mark Greerd34e48d2017-04-25 15:43:55 -0700451 struct gpio_desc *en_gpiod;
452 struct gpio_desc *en2_gpiod;
Mark A. Greer165063f2014-03-10 11:56:22 -0700453 struct mutex lock;
454 unsigned int timeout;
455 bool ignore_timeout;
456 struct delayed_work timeout_work;
457};
458
Mark A. Greer165063f2014-03-10 11:56:22 -0700459static int trf7970a_cmd(struct trf7970a *trf, u8 opcode)
460{
461 u8 cmd = TRF7970A_CMD_BIT_CTRL | TRF7970A_CMD_BIT_OPCODE(opcode);
462 int ret;
463
464 dev_dbg(trf->dev, "cmd: 0x%x\n", cmd);
465
466 ret = spi_write(trf->spi, &cmd, 1);
467 if (ret)
468 dev_err(trf->dev, "%s - cmd: 0x%x, ret: %d\n", __func__, cmd,
Mark Greere2f0f672017-04-25 15:43:56 -0700469 ret);
Mark A. Greer165063f2014-03-10 11:56:22 -0700470 return ret;
471}
472
473static int trf7970a_read(struct trf7970a *trf, u8 reg, u8 *val)
474{
475 u8 addr = TRF7970A_CMD_BIT_RW | reg;
476 int ret;
477
478 ret = spi_write_then_read(trf->spi, &addr, 1, val, 1);
479 if (ret)
480 dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr,
Mark Greere2f0f672017-04-25 15:43:56 -0700481 ret);
Mark A. Greer165063f2014-03-10 11:56:22 -0700482
483 dev_dbg(trf->dev, "read(0x%x): 0x%x\n", addr, *val);
484
485 return ret;
486}
487
Mark Greere2f0f672017-04-25 15:43:56 -0700488static int trf7970a_read_cont(struct trf7970a *trf, u8 reg, u8 *buf,
489 size_t len)
Mark A. Greer165063f2014-03-10 11:56:22 -0700490{
491 u8 addr = reg | TRF7970A_CMD_BIT_RW | TRF7970A_CMD_BIT_CONTINUOUS;
Mark A. Greer3e7f3352014-09-02 15:12:18 -0700492 struct spi_transfer t[2];
493 struct spi_message m;
Mark A. Greer165063f2014-03-10 11:56:22 -0700494 int ret;
495
496 dev_dbg(trf->dev, "read_cont(0x%x, %zd)\n", addr, len);
497
Mark A. Greer3e7f3352014-09-02 15:12:18 -0700498 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);
Mark A. Greer165063f2014-03-10 11:56:22 -0700511 if (ret)
512 dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr,
Mark Greere2f0f672017-04-25 15:43:56 -0700513 ret);
Mark A. Greer165063f2014-03-10 11:56:22 -0700514 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__,
Mark Greere2f0f672017-04-25 15:43:56 -0700527 buf[0], buf[1], ret);
Mark A. Greer165063f2014-03-10 11:56:22 -0700528
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
Mark A. Greer772079e2014-08-07 17:41:43 -0700540 if (trf->quirks & TRF7970A_QUIRK_IRQ_STATUS_READ) {
Mark A. Greer165063f2014-03-10 11:56:22 -0700541 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",
Mark Greere2f0f672017-04-25 15:43:56 -0700549 __func__, ret);
Mark A. Greer165063f2014-03-10 11:56:22 -0700550 else
551 *status = buf[0];
552
553 return ret;
554}
555
Mark A. Greercb174ab2014-09-02 15:12:46 -0700556static 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 |
Mark Greere2f0f672017-04-25 15:43:56 -0700563 TRF7970A_CMD_BIT_CONTINUOUS;
Mark A. Greercb174ab2014-09-02 15:12:46 -0700564
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",
Mark Greere2f0f672017-04-25 15:43:56 -0700568 __func__, ret);
Mark A. Greercb174ab2014-09-02 15:12:46 -0700569 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",
Mark Greere2f0f672017-04-25 15:43:56 -0700599 __func__, target_proto);
Mark A. Greercb174ab2014-09-02 15:12:46 -0700600 return -EIO;
601 }
602
603 *rf_tech = tech;
604
605 return ret;
606}
607
Mark A. Greer165063f2014-03-10 11:56:22 -0700608static void trf7970a_send_upstream(struct trf7970a *trf)
609{
Mark A. Greer165063f2014-03-10 11:56:22 -0700610 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,
Mark Greere2f0f672017-04-25 15:43:56 -0700615 16, 1, trf->rx_skb->data, trf->rx_skb->len,
616 false);
Mark A. Greer165063f2014-03-10 11:56:22 -0700617
Mark A. Greer165063f2014-03-10 11:56:22 -0700618 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 trf->cb(trf->ddev, trf->cb_arg, trf->rx_skb);
632
633 trf->rx_skb = NULL;
634}
635
636static void trf7970a_send_err_upstream(struct trf7970a *trf, int errno)
637{
638 dev_dbg(trf->dev, "Error - state: %d, errno: %d\n", trf->state, errno);
639
Mark A. Greer6fb9edc2014-09-02 15:12:43 -0700640 cancel_delayed_work(&trf->timeout_work);
641
Mark A. Greer165063f2014-03-10 11:56:22 -0700642 kfree_skb(trf->rx_skb);
643 trf->rx_skb = ERR_PTR(errno);
644
645 trf7970a_send_upstream(trf);
646}
647
648static int trf7970a_transmit(struct trf7970a *trf, struct sk_buff *skb,
Mark Greere2f0f672017-04-25 15:43:56 -0700649 unsigned int len, u8 *prefix,
650 unsigned int prefix_len)
Mark A. Greer165063f2014-03-10 11:56:22 -0700651{
Mark A. Greer7a1e5552014-09-02 15:12:28 -0700652 struct spi_transfer t[2];
653 struct spi_message m;
Mark A. Greer165063f2014-03-10 11:56:22 -0700654 unsigned int timeout;
655 int ret;
656
657 print_hex_dump_debug("trf7970a tx data: ", DUMP_PREFIX_NONE,
Mark Greere2f0f672017-04-25 15:43:56 -0700658 16, 1, skb->data, len, false);
Mark A. Greer165063f2014-03-10 11:56:22 -0700659
Mark A. Greer7a1e5552014-09-02 15:12:28 -0700660 spi_message_init(&m);
661
662 memset(&t, 0, sizeof(t));
663
664 t[0].tx_buf = prefix;
665 t[0].len = prefix_len;
666 spi_message_add_tail(&t[0], &m);
667
668 t[1].tx_buf = skb->data;
669 t[1].len = len;
670 spi_message_add_tail(&t[1], &m);
671
672 ret = spi_sync(trf->spi, &m);
Mark A. Greer165063f2014-03-10 11:56:22 -0700673 if (ret) {
674 dev_err(trf->dev, "%s - Can't send tx data: %d\n", __func__,
Mark Greere2f0f672017-04-25 15:43:56 -0700675 ret);
Mark A. Greer165063f2014-03-10 11:56:22 -0700676 return ret;
677 }
678
679 skb_pull(skb, len);
680
681 if (skb->len > 0) {
682 trf->state = TRF7970A_ST_WAIT_FOR_TX_FIFO;
683 timeout = TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT;
684 } else {
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700685 if (trf->issue_eof) {
686 trf->state = TRF7970A_ST_WAIT_TO_ISSUE_EOF;
687 timeout = TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF;
688 } else {
689 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA;
Mark A. Greer19618432014-09-02 15:12:44 -0700690
691 if (!trf->timeout)
692 timeout = TRF7970A_WAIT_FOR_TX_IRQ;
693 else
694 timeout = trf->timeout;
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700695 }
Mark A. Greer165063f2014-03-10 11:56:22 -0700696 }
697
698 dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", timeout,
Mark Greere2f0f672017-04-25 15:43:56 -0700699 trf->state);
Mark A. Greer165063f2014-03-10 11:56:22 -0700700
701 schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout));
702
703 return 0;
704}
705
706static void trf7970a_fill_fifo(struct trf7970a *trf)
707{
708 struct sk_buff *skb = trf->tx_skb;
709 unsigned int len;
710 int ret;
711 u8 fifo_bytes;
Mark A. Greer7a1e5552014-09-02 15:12:28 -0700712 u8 prefix;
Mark A. Greer165063f2014-03-10 11:56:22 -0700713
714 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
715 if (ret) {
716 trf7970a_send_err_upstream(trf, ret);
717 return;
718 }
719
720 dev_dbg(trf->dev, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes);
721
Mark A. Greer4542e832014-09-02 15:12:19 -0700722 fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
Mark A. Greer165063f2014-03-10 11:56:22 -0700723
724 /* Calculate how much more data can be written to the fifo */
725 len = TRF7970A_FIFO_SIZE - fifo_bytes;
Mark A. Greer0e840ed2014-09-02 15:12:29 -0700726 if (!len) {
727 schedule_delayed_work(&trf->timeout_work,
728 msecs_to_jiffies(TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT));
729 return;
730 }
731
Mark A. Greer165063f2014-03-10 11:56:22 -0700732 len = min(skb->len, len);
733
Mark A. Greer7a1e5552014-09-02 15:12:28 -0700734 prefix = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_FIFO_IO_REGISTER;
735
736 ret = trf7970a_transmit(trf, skb, len, &prefix, sizeof(prefix));
Mark A. Greer165063f2014-03-10 11:56:22 -0700737 if (ret)
738 trf7970a_send_err_upstream(trf, ret);
739}
740
741static void trf7970a_drain_fifo(struct trf7970a *trf, u8 status)
742{
743 struct sk_buff *skb = trf->rx_skb;
744 int ret;
745 u8 fifo_bytes;
746
747 if (status & TRF7970A_IRQ_STATUS_ERROR) {
748 trf7970a_send_err_upstream(trf, -EIO);
749 return;
750 }
751
752 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
753 if (ret) {
754 trf7970a_send_err_upstream(trf, ret);
755 return;
756 }
757
758 dev_dbg(trf->dev, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes);
759
Mark A. Greer4542e832014-09-02 15:12:19 -0700760 fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
761
Mark A. Greer165063f2014-03-10 11:56:22 -0700762 if (!fifo_bytes)
763 goto no_rx_data;
764
Mark A. Greer165063f2014-03-10 11:56:22 -0700765 if (fifo_bytes > skb_tailroom(skb)) {
766 skb = skb_copy_expand(skb, skb_headroom(skb),
Mark Greere2f0f672017-04-25 15:43:56 -0700767 max_t(int, fifo_bytes,
768 TRF7970A_RX_SKB_ALLOC_SIZE),
769 GFP_KERNEL);
Mark A. Greer165063f2014-03-10 11:56:22 -0700770 if (!skb) {
771 trf7970a_send_err_upstream(trf, -ENOMEM);
772 return;
773 }
774
775 kfree_skb(trf->rx_skb);
776 trf->rx_skb = skb;
777 }
778
779 ret = trf7970a_read_cont(trf, TRF7970A_FIFO_IO_REGISTER,
Mark Greere2f0f672017-04-25 15:43:56 -0700780 skb_put(skb, fifo_bytes), fifo_bytes);
Mark A. Greer165063f2014-03-10 11:56:22 -0700781 if (ret) {
782 trf7970a_send_err_upstream(trf, ret);
783 return;
784 }
785
786 /* If received Type 2 ACK/NACK, shift right 4 bits and pass up */
787 if ((trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T) && (skb->len == 1) &&
Mark Greere2f0f672017-04-25 15:43:56 -0700788 (trf->special_fcn_reg1 == TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX)) {
Mark A. Greer165063f2014-03-10 11:56:22 -0700789 skb->data[0] >>= 4;
790 status = TRF7970A_IRQ_STATUS_SRX;
791 } else {
792 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA_CONT;
Mark A. Greer5d8f7592014-09-02 15:12:31 -0700793
794 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
795 if (ret) {
796 trf7970a_send_err_upstream(trf, ret);
797 return;
798 }
799
800 fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
801
802 /* If there are bytes in the FIFO, set status to '0' so
803 * the if stmt below doesn't fire and the driver will wait
804 * for the trf7970a to generate another RX interrupt.
805 */
806 if (fifo_bytes)
807 status = 0;
Mark A. Greer165063f2014-03-10 11:56:22 -0700808 }
809
810no_rx_data:
Mark Greere2f0f672017-04-25 15:43:56 -0700811 if (status == TRF7970A_IRQ_STATUS_SRX) { /* Receive complete */
Mark A. Greer165063f2014-03-10 11:56:22 -0700812 trf7970a_send_upstream(trf);
813 return;
814 }
815
816 dev_dbg(trf->dev, "Setting timeout for %d ms\n",
Mark Greere2f0f672017-04-25 15:43:56 -0700817 TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT);
Mark A. Greer165063f2014-03-10 11:56:22 -0700818
819 schedule_delayed_work(&trf->timeout_work,
Mark Greere2f0f672017-04-25 15:43:56 -0700820 msecs_to_jiffies(TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT));
Mark A. Greer165063f2014-03-10 11:56:22 -0700821}
822
823static irqreturn_t trf7970a_irq(int irq, void *dev_id)
824{
825 struct trf7970a *trf = dev_id;
826 int ret;
Mark A. Greer13b42722014-09-02 15:12:45 -0700827 u8 status, fifo_bytes, iso_ctrl;
Mark A. Greer165063f2014-03-10 11:56:22 -0700828
829 mutex_lock(&trf->lock);
830
Mark A. Greerb5e17d92014-09-02 15:12:34 -0700831 if (trf->state == TRF7970A_ST_RF_OFF) {
Mark A. Greer165063f2014-03-10 11:56:22 -0700832 mutex_unlock(&trf->lock);
833 return IRQ_NONE;
834 }
835
836 ret = trf7970a_read_irqstatus(trf, &status);
837 if (ret) {
838 mutex_unlock(&trf->lock);
839 return IRQ_NONE;
840 }
841
842 dev_dbg(trf->dev, "IRQ - state: %d, status: 0x%x\n", trf->state,
Mark Greere2f0f672017-04-25 15:43:56 -0700843 status);
Mark A. Greer165063f2014-03-10 11:56:22 -0700844
845 if (!status) {
846 mutex_unlock(&trf->lock);
847 return IRQ_NONE;
848 }
849
850 switch (trf->state) {
851 case TRF7970A_ST_IDLE:
852 case TRF7970A_ST_IDLE_RX_BLOCKED:
Mark A. Greer13b42722014-09-02 15:12:45 -0700853 /* If initiator and getting interrupts caused by RF noise,
854 * turn off the receiver to avoid unnecessary interrupts.
855 * It will be turned back on in trf7970a_send_cmd() when
856 * the next command is issued.
Mark A. Greer165063f2014-03-10 11:56:22 -0700857 */
Mark A. Greer13b42722014-09-02 15:12:45 -0700858 if (trf->is_initiator && (status & TRF7970A_IRQ_STATUS_ERROR)) {
Mark A. Greer165063f2014-03-10 11:56:22 -0700859 trf7970a_cmd(trf, TRF7970A_CMD_BLOCK_RX);
860 trf->state = TRF7970A_ST_IDLE_RX_BLOCKED;
861 }
862
863 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
864 break;
865 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
866 if (status & TRF7970A_IRQ_STATUS_TX) {
867 trf->ignore_timeout =
Mark Greere2f0f672017-04-25 15:43:56 -0700868 !cancel_delayed_work(&trf->timeout_work);
Mark A. Greer165063f2014-03-10 11:56:22 -0700869 trf7970a_fill_fifo(trf);
870 } else {
871 trf7970a_send_err_upstream(trf, -EIO);
872 }
873 break;
874 case TRF7970A_ST_WAIT_FOR_RX_DATA:
875 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
876 if (status & TRF7970A_IRQ_STATUS_SRX) {
877 trf->ignore_timeout =
Mark Greere2f0f672017-04-25 15:43:56 -0700878 !cancel_delayed_work(&trf->timeout_work);
Mark A. Greer165063f2014-03-10 11:56:22 -0700879 trf7970a_drain_fifo(trf, status);
Mark A. Greerbece3c52014-09-02 15:12:30 -0700880 } else if (status & TRF7970A_IRQ_STATUS_FIFO) {
881 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS,
Mark Greere2f0f672017-04-25 15:43:56 -0700882 &fifo_bytes);
Mark A. Greerbece3c52014-09-02 15:12:30 -0700883
884 fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
885
886 if (ret)
887 trf7970a_send_err_upstream(trf, ret);
888 else if (!fifo_bytes)
889 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
Mark A. Greer13b42722014-09-02 15:12:45 -0700890 } else if ((status == TRF7970A_IRQ_STATUS_TX) ||
Mark Greere2f0f672017-04-25 15:43:56 -0700891 (!trf->is_initiator &&
892 (status == (TRF7970A_IRQ_STATUS_TX |
893 TRF7970A_IRQ_STATUS_NFC_RF)))) {
Mark A. Greer4dd836e2014-03-25 08:54:32 -0700894 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
Mark A. Greer19618432014-09-02 15:12:44 -0700895
896 if (!trf->timeout) {
Mark Greere2f0f672017-04-25 15:43:56 -0700897 trf->ignore_timeout =
898 !cancel_delayed_work(&trf->timeout_work);
Mark A. Greer19618432014-09-02 15:12:44 -0700899 trf->rx_skb = ERR_PTR(0);
900 trf7970a_send_upstream(trf);
901 break;
902 }
Mark A. Greer13b42722014-09-02 15:12:45 -0700903
904 if (trf->is_initiator)
905 break;
906
907 iso_ctrl = trf->iso_ctrl;
908
909 switch (trf->framing) {
910 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
911 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
912 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
913 trf->iso_ctrl = 0xff; /* Force ISO_CTRL write */
914 break;
915 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
916 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
917 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
918 trf->iso_ctrl = 0xff; /* Force ISO_CTRL write */
919 break;
920 case NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE:
921 ret = trf7970a_write(trf,
Mark Greere2f0f672017-04-25 15:43:56 -0700922 TRF7970A_SPECIAL_FCN_REG1,
923 TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL);
Mark A. Greer13b42722014-09-02 15:12:45 -0700924 if (ret)
Mark A. Greerb9e30162014-09-23 11:25:47 -0700925 goto err_unlock_exit;
Mark A. Greer13b42722014-09-02 15:12:45 -0700926
927 trf->special_fcn_reg1 =
Mark Greere2f0f672017-04-25 15:43:56 -0700928 TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL;
Mark A. Greer13b42722014-09-02 15:12:45 -0700929 break;
930 default:
931 break;
932 }
933
934 if (iso_ctrl != trf->iso_ctrl) {
935 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL,
Mark Greere2f0f672017-04-25 15:43:56 -0700936 iso_ctrl);
Mark A. Greer13b42722014-09-02 15:12:45 -0700937 if (ret)
Mark A. Greerb9e30162014-09-23 11:25:47 -0700938 goto err_unlock_exit;
Mark A. Greer13b42722014-09-02 15:12:45 -0700939
940 trf->iso_ctrl = iso_ctrl;
941 }
Mark A. Greer4dd836e2014-03-25 08:54:32 -0700942 } else {
Mark A. Greer165063f2014-03-10 11:56:22 -0700943 trf7970a_send_err_upstream(trf, -EIO);
944 }
945 break;
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700946 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
947 if (status != TRF7970A_IRQ_STATUS_TX)
948 trf7970a_send_err_upstream(trf, -EIO);
949 break;
Mark A. Greer13b42722014-09-02 15:12:45 -0700950 case TRF7970A_ST_LISTENING:
951 if (status & TRF7970A_IRQ_STATUS_SRX) {
952 trf->ignore_timeout =
Mark Greere2f0f672017-04-25 15:43:56 -0700953 !cancel_delayed_work(&trf->timeout_work);
Mark A. Greer13b42722014-09-02 15:12:45 -0700954 trf7970a_drain_fifo(trf, status);
955 } else if (!(status & TRF7970A_IRQ_STATUS_NFC_RF)) {
956 trf7970a_send_err_upstream(trf, -EIO);
957 }
958 break;
Mark A. Greercb174ab2014-09-02 15:12:46 -0700959 case TRF7970A_ST_LISTENING_MD:
960 if (status & TRF7970A_IRQ_STATUS_SRX) {
961 trf->ignore_timeout =
Mark Greere2f0f672017-04-25 15:43:56 -0700962 !cancel_delayed_work(&trf->timeout_work);
Mark A. Greercb174ab2014-09-02 15:12:46 -0700963
964 ret = trf7970a_mode_detect(trf, &trf->md_rf_tech);
965 if (ret) {
966 trf7970a_send_err_upstream(trf, ret);
967 } else {
968 trf->state = TRF7970A_ST_LISTENING;
969 trf7970a_drain_fifo(trf, status);
970 }
971 } else if (!(status & TRF7970A_IRQ_STATUS_NFC_RF)) {
972 trf7970a_send_err_upstream(trf, -EIO);
973 }
974 break;
Mark A. Greer165063f2014-03-10 11:56:22 -0700975 default:
976 dev_err(trf->dev, "%s - Driver in invalid state: %d\n",
Mark Greere2f0f672017-04-25 15:43:56 -0700977 __func__, trf->state);
Mark A. Greer165063f2014-03-10 11:56:22 -0700978 }
979
Mark A. Greerb9e30162014-09-23 11:25:47 -0700980err_unlock_exit:
Mark A. Greer165063f2014-03-10 11:56:22 -0700981 mutex_unlock(&trf->lock);
982 return IRQ_HANDLED;
983}
984
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700985static void trf7970a_issue_eof(struct trf7970a *trf)
986{
987 int ret;
988
989 dev_dbg(trf->dev, "Issuing EOF\n");
990
991 ret = trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
992 if (ret)
993 trf7970a_send_err_upstream(trf, ret);
994
995 ret = trf7970a_cmd(trf, TRF7970A_CMD_EOF);
996 if (ret)
997 trf7970a_send_err_upstream(trf, ret);
998
999 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA;
1000
1001 dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n",
Mark Greere2f0f672017-04-25 15:43:56 -07001002 trf->timeout, trf->state);
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001003
1004 schedule_delayed_work(&trf->timeout_work,
Mark Greere2f0f672017-04-25 15:43:56 -07001005 msecs_to_jiffies(trf->timeout));
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001006}
1007
Mark A. Greer165063f2014-03-10 11:56:22 -07001008static void trf7970a_timeout_work_handler(struct work_struct *work)
1009{
1010 struct trf7970a *trf = container_of(work, struct trf7970a,
Mark Greere2f0f672017-04-25 15:43:56 -07001011 timeout_work.work);
Mark A. Greer165063f2014-03-10 11:56:22 -07001012
1013 dev_dbg(trf->dev, "Timeout - state: %d, ignore_timeout: %d\n",
Mark Greere2f0f672017-04-25 15:43:56 -07001014 trf->state, trf->ignore_timeout);
Mark A. Greer165063f2014-03-10 11:56:22 -07001015
1016 mutex_lock(&trf->lock);
1017
1018 if (trf->ignore_timeout)
1019 trf->ignore_timeout = false;
1020 else if (trf->state == TRF7970A_ST_WAIT_FOR_RX_DATA_CONT)
Mark A. Greerafa5b5f2014-09-02 15:12:32 -07001021 trf7970a_drain_fifo(trf, TRF7970A_IRQ_STATUS_SRX);
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001022 else if (trf->state == TRF7970A_ST_WAIT_TO_ISSUE_EOF)
1023 trf7970a_issue_eof(trf);
Mark A. Greer165063f2014-03-10 11:56:22 -07001024 else
1025 trf7970a_send_err_upstream(trf, -ETIMEDOUT);
1026
1027 mutex_unlock(&trf->lock);
1028}
1029
1030static int trf7970a_init(struct trf7970a *trf)
1031{
1032 int ret;
1033
1034 dev_dbg(trf->dev, "Initializing device - state: %d\n", trf->state);
1035
1036 ret = trf7970a_cmd(trf, TRF7970A_CMD_SOFT_INIT);
1037 if (ret)
1038 goto err_out;
1039
1040 ret = trf7970a_cmd(trf, TRF7970A_CMD_IDLE);
1041 if (ret)
1042 goto err_out;
1043
Geoff Lansberry49d22c72016-12-21 23:18:33 -05001044 ret = trf7970a_write(trf, TRF7970A_REG_IO_CTRL,
Mark Greere2f0f672017-04-25 15:43:56 -07001045 trf->io_ctrl | TRF7970A_REG_IO_CTRL_VRS(0x1));
Geoff Lansberry49d22c72016-12-21 23:18:33 -05001046 if (ret)
1047 goto err_out;
1048
Geoff Lansberry58d46f52016-04-18 15:48:39 -04001049 ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
1050 if (ret)
1051 goto err_out;
1052
Mark A. Greer4e007f82014-09-02 15:12:38 -07001053 usleep_range(1000, 2000);
1054
Mark A. Greer7149d6b2014-09-02 15:12:41 -07001055 trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
1056
Geoff Lansberry837eb4d2016-12-21 23:18:32 -05001057 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
Mark Greere2f0f672017-04-25 15:43:56 -07001058 trf->modulator_sys_clk_ctrl);
Mark A. Greer6c08df42014-09-02 15:12:22 -07001059 if (ret)
1060 goto err_out;
1061
Mark A. Greer165063f2014-03-10 11:56:22 -07001062 ret = trf7970a_write(trf, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS,
Mark Greere2f0f672017-04-25 15:43:56 -07001063 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 |
1064 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32);
Mark A. Greer165063f2014-03-10 11:56:22 -07001065 if (ret)
1066 goto err_out;
1067
1068 ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, 0);
1069 if (ret)
1070 goto err_out;
1071
1072 trf->special_fcn_reg1 = 0;
1073
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001074 trf->iso_ctrl = 0xff;
Mark A. Greer165063f2014-03-10 11:56:22 -07001075 return 0;
1076
1077err_out:
1078 dev_dbg(trf->dev, "Couldn't init device: %d\n", ret);
1079 return ret;
1080}
1081
1082static void trf7970a_switch_rf_off(struct trf7970a *trf)
1083{
Mark A. Greercfc708d2014-09-02 15:12:40 -07001084 if ((trf->state == TRF7970A_ST_PWR_OFF) ||
Mark Greere2f0f672017-04-25 15:43:56 -07001085 (trf->state == TRF7970A_ST_RF_OFF))
Mark A. Greercfc708d2014-09-02 15:12:40 -07001086 return;
1087
Mark A. Greer165063f2014-03-10 11:56:22 -07001088 dev_dbg(trf->dev, "Switching rf off\n");
1089
Mark A. Greera1d2dc52014-03-25 08:54:37 -07001090 trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
1091
1092 trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, trf->chip_status_ctrl);
1093
Mark A. Greer165063f2014-03-10 11:56:22 -07001094 trf->aborting = false;
Mark A. Greerb5e17d92014-09-02 15:12:34 -07001095 trf->state = TRF7970A_ST_RF_OFF;
Mark A. Greere6403b72014-03-25 08:54:38 -07001096
1097 pm_runtime_mark_last_busy(trf->dev);
1098 pm_runtime_put_autosuspend(trf->dev);
Mark A. Greer165063f2014-03-10 11:56:22 -07001099}
1100
Mark A. Greer0a1de842014-09-02 15:12:33 -07001101static int trf7970a_switch_rf_on(struct trf7970a *trf)
Mark A. Greer165063f2014-03-10 11:56:22 -07001102{
Mark A. Greera08e5452014-09-02 15:12:23 -07001103 int ret;
1104
Mark A. Greer165063f2014-03-10 11:56:22 -07001105 dev_dbg(trf->dev, "Switching rf on\n");
1106
Mark A. Greere6403b72014-03-25 08:54:38 -07001107 pm_runtime_get_sync(trf->dev);
Mark A. Greer165063f2014-03-10 11:56:22 -07001108
Mark Greere2f0f672017-04-25 15:43:56 -07001109 if (trf->state != TRF7970A_ST_RF_OFF) { /* Power on, RF off */
Mark A. Greerceccd6a2014-09-02 15:12:35 -07001110 dev_err(trf->dev, "%s - Incorrect state: %d\n", __func__,
Mark Greere2f0f672017-04-25 15:43:56 -07001111 trf->state);
Mark A. Greerceccd6a2014-09-02 15:12:35 -07001112 return -EINVAL;
1113 }
1114
Mark A. Greera08e5452014-09-02 15:12:23 -07001115 ret = trf7970a_init(trf);
1116 if (ret) {
1117 dev_err(trf->dev, "%s - Can't initialize: %d\n", __func__, ret);
Mark A. Greer0a1de842014-09-02 15:12:33 -07001118 return ret;
Mark A. Greera08e5452014-09-02 15:12:23 -07001119 }
1120
Mark A. Greere6403b72014-03-25 08:54:38 -07001121 trf->state = TRF7970A_ST_IDLE;
Mark A. Greer0a1de842014-09-02 15:12:33 -07001122
1123 return 0;
Mark A. Greer165063f2014-03-10 11:56:22 -07001124}
1125
1126static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
1127{
1128 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
Mark A. Greer0a1de842014-09-02 15:12:33 -07001129 int ret = 0;
Mark A. Greer165063f2014-03-10 11:56:22 -07001130
1131 dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on);
1132
1133 mutex_lock(&trf->lock);
1134
1135 if (on) {
1136 switch (trf->state) {
Mark A. Greerceccd6a2014-09-02 15:12:35 -07001137 case TRF7970A_ST_PWR_OFF:
Mark A. Greerb5e17d92014-09-02 15:12:34 -07001138 case TRF7970A_ST_RF_OFF:
Mark A. Greer0a1de842014-09-02 15:12:33 -07001139 ret = trf7970a_switch_rf_on(trf);
Mark A. Greer165063f2014-03-10 11:56:22 -07001140 break;
1141 case TRF7970A_ST_IDLE:
1142 case TRF7970A_ST_IDLE_RX_BLOCKED:
1143 break;
1144 default:
1145 dev_err(trf->dev, "%s - Invalid request: %d %d\n",
Mark Greere2f0f672017-04-25 15:43:56 -07001146 __func__, trf->state, on);
Mark A. Greer165063f2014-03-10 11:56:22 -07001147 trf7970a_switch_rf_off(trf);
Mark A. Greer0a1de842014-09-02 15:12:33 -07001148 ret = -EINVAL;
Mark A. Greer165063f2014-03-10 11:56:22 -07001149 }
1150 } else {
1151 switch (trf->state) {
Mark A. Greerceccd6a2014-09-02 15:12:35 -07001152 case TRF7970A_ST_PWR_OFF:
Mark A. Greerb5e17d92014-09-02 15:12:34 -07001153 case TRF7970A_ST_RF_OFF:
Mark A. Greer165063f2014-03-10 11:56:22 -07001154 break;
1155 default:
1156 dev_err(trf->dev, "%s - Invalid request: %d %d\n",
Mark Greere2f0f672017-04-25 15:43:56 -07001157 __func__, trf->state, on);
Mark A. Greer0a1de842014-09-02 15:12:33 -07001158 ret = -EINVAL;
Mark A. Greer165063f2014-03-10 11:56:22 -07001159 /* FALLTHROUGH */
1160 case TRF7970A_ST_IDLE:
1161 case TRF7970A_ST_IDLE_RX_BLOCKED:
Mark A. Greer13b42722014-09-02 15:12:45 -07001162 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1163 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
Mark A. Greer165063f2014-03-10 11:56:22 -07001164 trf7970a_switch_rf_off(trf);
1165 }
1166 }
1167
1168 mutex_unlock(&trf->lock);
Mark A. Greer0a1de842014-09-02 15:12:33 -07001169 return ret;
Mark A. Greer165063f2014-03-10 11:56:22 -07001170}
1171
Mark A. Greer307e5ca2014-09-02 15:12:39 -07001172static int trf7970a_in_config_rf_tech(struct trf7970a *trf, int tech)
Mark A. Greer165063f2014-03-10 11:56:22 -07001173{
1174 int ret = 0;
1175
1176 dev_dbg(trf->dev, "rf technology: %d\n", tech);
1177
1178 switch (tech) {
1179 case NFC_DIGITAL_RF_TECH_106A:
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001180 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443A_106;
Geoff Lansberry837eb4d2016-12-21 23:18:32 -05001181 trf->modulator_sys_clk_ctrl =
Mark Greere2f0f672017-04-25 15:43:56 -07001182 (trf->modulator_sys_clk_ctrl & 0xf8) |
1183 TRF7970A_MODULATOR_DEPTH_OOK;
Mark A. Greer4e64eff2014-09-02 15:12:24 -07001184 trf->guard_time = TRF7970A_GUARD_TIME_NFCA;
Mark A. Greer165063f2014-03-10 11:56:22 -07001185 break;
Mark A. Greer742b1f92014-03-31 17:52:33 -07001186 case NFC_DIGITAL_RF_TECH_106B:
1187 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443B_106;
Geoff Lansberry837eb4d2016-12-21 23:18:32 -05001188 trf->modulator_sys_clk_ctrl =
Mark Greere2f0f672017-04-25 15:43:56 -07001189 (trf->modulator_sys_clk_ctrl & 0xf8) |
1190 TRF7970A_MODULATOR_DEPTH_ASK10;
Mark A. Greer4e64eff2014-09-02 15:12:24 -07001191 trf->guard_time = TRF7970A_GUARD_TIME_NFCB;
Mark A. Greer742b1f92014-03-31 17:52:33 -07001192 break;
Mark A. Greer6857bb92014-04-14 10:04:10 -07001193 case NFC_DIGITAL_RF_TECH_212F:
1194 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_212;
Geoff Lansberry837eb4d2016-12-21 23:18:32 -05001195 trf->modulator_sys_clk_ctrl =
Mark Greere2f0f672017-04-25 15:43:56 -07001196 (trf->modulator_sys_clk_ctrl & 0xf8) |
1197 TRF7970A_MODULATOR_DEPTH_ASK10;
Mark A. Greer4e64eff2014-09-02 15:12:24 -07001198 trf->guard_time = TRF7970A_GUARD_TIME_NFCF;
Mark A. Greer6857bb92014-04-14 10:04:10 -07001199 break;
1200 case NFC_DIGITAL_RF_TECH_424F:
1201 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_424;
Geoff Lansberry837eb4d2016-12-21 23:18:32 -05001202 trf->modulator_sys_clk_ctrl =
Mark Greere2f0f672017-04-25 15:43:56 -07001203 (trf->modulator_sys_clk_ctrl & 0xf8) |
1204 TRF7970A_MODULATOR_DEPTH_ASK10;
Mark A. Greer4e64eff2014-09-02 15:12:24 -07001205 trf->guard_time = TRF7970A_GUARD_TIME_NFCF;
Mark A. Greer6857bb92014-04-14 10:04:10 -07001206 break;
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001207 case NFC_DIGITAL_RF_TECH_ISO15693:
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001208 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
Geoff Lansberry837eb4d2016-12-21 23:18:32 -05001209 trf->modulator_sys_clk_ctrl =
Mark Greere2f0f672017-04-25 15:43:56 -07001210 (trf->modulator_sys_clk_ctrl & 0xf8) |
1211 TRF7970A_MODULATOR_DEPTH_OOK;
Mark A. Greer4e64eff2014-09-02 15:12:24 -07001212 trf->guard_time = TRF7970A_GUARD_TIME_15693;
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001213 break;
Mark A. Greer165063f2014-03-10 11:56:22 -07001214 default:
1215 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
1216 return -EINVAL;
1217 }
1218
1219 trf->technology = tech;
1220
Mark A. Greer13b42722014-09-02 15:12:45 -07001221 /* If in initiator mode and not changing the RF tech due to a
1222 * PSL sequence (indicated by 'trf->iso_ctrl == 0xff' from
1223 * trf7970a_init()), clear the NFC Target Detection Level register
1224 * due to erratum.
1225 */
1226 if (trf->iso_ctrl == 0xff)
1227 ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
1228
Mark A. Greer165063f2014-03-10 11:56:22 -07001229 return ret;
1230}
1231
Mark A. Greer851ee3c2014-09-02 15:12:42 -07001232static int trf7970a_is_rf_field(struct trf7970a *trf, bool *is_rf_field)
1233{
1234 int ret;
1235 u8 rssi;
1236
1237 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
Mark Greere2f0f672017-04-25 15:43:56 -07001238 trf->chip_status_ctrl |
1239 TRF7970A_CHIP_STATUS_REC_ON);
Mark A. Greer851ee3c2014-09-02 15:12:42 -07001240 if (ret)
1241 return ret;
1242
1243 ret = trf7970a_cmd(trf, TRF7970A_CMD_TEST_EXT_RF);
1244 if (ret)
1245 return ret;
1246
1247 usleep_range(50, 60);
1248
1249 ret = trf7970a_read(trf, TRF7970A_RSSI_OSC_STATUS, &rssi);
1250 if (ret)
1251 return ret;
1252
1253 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
Mark Greere2f0f672017-04-25 15:43:56 -07001254 trf->chip_status_ctrl);
Mark A. Greer851ee3c2014-09-02 15:12:42 -07001255 if (ret)
1256 return ret;
1257
1258 if (rssi & TRF7970A_RSSI_OSC_STATUS_RSSI_MASK)
1259 *is_rf_field = true;
1260 else
1261 *is_rf_field = false;
1262
1263 return 0;
1264}
1265
Mark A. Greer307e5ca2014-09-02 15:12:39 -07001266static int trf7970a_in_config_framing(struct trf7970a *trf, int framing)
Mark A. Greer165063f2014-03-10 11:56:22 -07001267{
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001268 u8 iso_ctrl = trf->iso_ctrl_tech;
Mark A. Greer851ee3c2014-09-02 15:12:42 -07001269 bool is_rf_field = false;
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001270 int ret;
1271
Mark A. Greer165063f2014-03-10 11:56:22 -07001272 dev_dbg(trf->dev, "framing: %d\n", framing);
1273
1274 switch (framing) {
1275 case NFC_DIGITAL_FRAMING_NFCA_SHORT:
1276 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
1277 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001278 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
Mark A. Greer165063f2014-03-10 11:56:22 -07001279 break;
1280 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
Mark A. Greer80062892014-03-10 11:56:23 -07001281 case NFC_DIGITAL_FRAMING_NFCA_T4T:
Mark A. Greer742b1f92014-03-31 17:52:33 -07001282 case NFC_DIGITAL_FRAMING_NFCB:
1283 case NFC_DIGITAL_FRAMING_NFCB_T4T:
Mark A. Greer6857bb92014-04-14 10:04:10 -07001284 case NFC_DIGITAL_FRAMING_NFCF:
1285 case NFC_DIGITAL_FRAMING_NFCF_T3T:
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001286 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY:
1287 case NFC_DIGITAL_FRAMING_ISO15693_T5T:
Mark A. Greer13b42722014-09-02 15:12:45 -07001288 case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
1289 case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP:
Mark A. Greer165063f2014-03-10 11:56:22 -07001290 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001291 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
Mark A. Greer165063f2014-03-10 11:56:22 -07001292 break;
1293 case NFC_DIGITAL_FRAMING_NFCA_T2T:
1294 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001295 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
Mark A. Greer165063f2014-03-10 11:56:22 -07001296 break;
1297 default:
1298 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
1299 return -EINVAL;
1300 }
1301
1302 trf->framing = framing;
1303
Mark A. Greer851ee3c2014-09-02 15:12:42 -07001304 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
1305 ret = trf7970a_is_rf_field(trf, &is_rf_field);
1306 if (ret)
1307 return ret;
1308
1309 if (is_rf_field)
1310 return -EBUSY;
1311 }
1312
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001313 if (iso_ctrl != trf->iso_ctrl) {
1314 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1315 if (ret)
1316 return ret;
1317
1318 trf->iso_ctrl = iso_ctrl;
Mark A. Greera0822a72014-03-25 08:54:34 -07001319
1320 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
Mark Greere2f0f672017-04-25 15:43:56 -07001321 trf->modulator_sys_clk_ctrl);
Mark A. Greera0822a72014-03-25 08:54:34 -07001322 if (ret)
1323 return ret;
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001324 }
1325
Mark A. Greera1d2dc52014-03-25 08:54:37 -07001326 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
1327 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
Mark Greere2f0f672017-04-25 15:43:56 -07001328 trf->chip_status_ctrl |
1329 TRF7970A_CHIP_STATUS_RF_ON);
Mark A. Greera1d2dc52014-03-25 08:54:37 -07001330 if (ret)
1331 return ret;
1332
1333 trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
1334
Mark A. Greer4e64eff2014-09-02 15:12:24 -07001335 usleep_range(trf->guard_time, trf->guard_time + 1000);
Mark A. Greera1d2dc52014-03-25 08:54:37 -07001336 }
1337
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001338 return 0;
Mark A. Greer165063f2014-03-10 11:56:22 -07001339}
1340
1341static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type,
Mark Greere2f0f672017-04-25 15:43:56 -07001342 int param)
Mark A. Greer165063f2014-03-10 11:56:22 -07001343{
1344 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
Mark A. Greer18422e62014-03-25 08:54:39 -07001345 int ret;
Mark A. Greer165063f2014-03-10 11:56:22 -07001346
1347 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
1348
1349 mutex_lock(&trf->lock);
1350
Mark A. Greer13b42722014-09-02 15:12:45 -07001351 trf->is_initiator = true;
1352
Mark A. Greerceccd6a2014-09-02 15:12:35 -07001353 if ((trf->state == TRF7970A_ST_PWR_OFF) ||
Mark Greere2f0f672017-04-25 15:43:56 -07001354 (trf->state == TRF7970A_ST_RF_OFF)) {
Mark A. Greer0a1de842014-09-02 15:12:33 -07001355 ret = trf7970a_switch_rf_on(trf);
1356 if (ret)
1357 goto err_unlock;
1358 }
Mark A. Greer165063f2014-03-10 11:56:22 -07001359
1360 switch (type) {
1361 case NFC_DIGITAL_CONFIG_RF_TECH:
Mark A. Greer307e5ca2014-09-02 15:12:39 -07001362 ret = trf7970a_in_config_rf_tech(trf, param);
Mark A. Greer165063f2014-03-10 11:56:22 -07001363 break;
1364 case NFC_DIGITAL_CONFIG_FRAMING:
Mark A. Greer307e5ca2014-09-02 15:12:39 -07001365 ret = trf7970a_in_config_framing(trf, param);
Mark A. Greer165063f2014-03-10 11:56:22 -07001366 break;
1367 default:
1368 dev_dbg(trf->dev, "Unknown type: %d\n", type);
1369 ret = -EINVAL;
1370 }
1371
Mark A. Greer0a1de842014-09-02 15:12:33 -07001372err_unlock:
Mark A. Greer165063f2014-03-10 11:56:22 -07001373 mutex_unlock(&trf->lock);
1374 return ret;
1375}
1376
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001377static int trf7970a_is_iso15693_write_or_lock(u8 cmd)
1378{
1379 switch (cmd) {
1380 case ISO15693_CMD_WRITE_SINGLE_BLOCK:
1381 case ISO15693_CMD_LOCK_BLOCK:
1382 case ISO15693_CMD_WRITE_MULTIPLE_BLOCK:
1383 case ISO15693_CMD_WRITE_AFI:
1384 case ISO15693_CMD_LOCK_AFI:
1385 case ISO15693_CMD_WRITE_DSFID:
1386 case ISO15693_CMD_LOCK_DSFID:
1387 return 1;
1388 break;
1389 default:
1390 return 0;
1391 }
1392}
1393
Mark A. Greer165063f2014-03-10 11:56:22 -07001394static int trf7970a_per_cmd_config(struct trf7970a *trf, struct sk_buff *skb)
1395{
1396 u8 *req = skb->data;
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001397 u8 special_fcn_reg1, iso_ctrl;
Mark A. Greer165063f2014-03-10 11:56:22 -07001398 int ret;
1399
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001400 trf->issue_eof = false;
1401
Mark A. Greer165063f2014-03-10 11:56:22 -07001402 /* When issuing Type 2 read command, make sure the '4_bit_RX' bit in
1403 * special functions register 1 is cleared; otherwise, its a write or
1404 * sector select command and '4_bit_RX' must be set.
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001405 *
1406 * When issuing an ISO 15693 command, inspect the flags byte to see
1407 * what speed to use. Also, remember if the OPTION flag is set on
1408 * a Type 5 write or lock command so the driver will know that it
1409 * has to send an EOF in order to get a response.
Mark A. Greer165063f2014-03-10 11:56:22 -07001410 */
1411 if ((trf->technology == NFC_DIGITAL_RF_TECH_106A) &&
Mark Greere2f0f672017-04-25 15:43:56 -07001412 (trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T)) {
Mark A. Greer165063f2014-03-10 11:56:22 -07001413 if (req[0] == NFC_T2T_CMD_READ)
1414 special_fcn_reg1 = 0;
1415 else
1416 special_fcn_reg1 = TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX;
1417
1418 if (special_fcn_reg1 != trf->special_fcn_reg1) {
1419 ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1,
Mark Greere2f0f672017-04-25 15:43:56 -07001420 special_fcn_reg1);
Mark A. Greer165063f2014-03-10 11:56:22 -07001421 if (ret)
1422 return ret;
1423
1424 trf->special_fcn_reg1 = special_fcn_reg1;
1425 }
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001426 } else if (trf->technology == NFC_DIGITAL_RF_TECH_ISO15693) {
1427 iso_ctrl = trf->iso_ctrl & ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK;
1428
1429 switch (req[0] & ISO15693_REQ_FLAG_SPEED_MASK) {
1430 case 0x00:
1431 iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_662;
1432 break;
1433 case ISO15693_REQ_FLAG_SUB_CARRIER:
1434 iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a;
1435 break;
1436 case ISO15693_REQ_FLAG_DATA_RATE:
1437 iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
1438 break;
1439 case (ISO15693_REQ_FLAG_SUB_CARRIER |
Mark Greere2f0f672017-04-25 15:43:56 -07001440 ISO15693_REQ_FLAG_DATA_RATE):
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001441 iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669;
1442 break;
1443 }
1444
1445 if (iso_ctrl != trf->iso_ctrl) {
1446 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1447 if (ret)
1448 return ret;
1449
1450 trf->iso_ctrl = iso_ctrl;
1451 }
1452
Mark Greera81d1ab2017-06-15 10:46:17 -07001453 if ((trf->framing == NFC_DIGITAL_FRAMING_ISO15693_T5T) &&
1454 trf7970a_is_iso15693_write_or_lock(req[1]) &&
1455 (req[0] & ISO15693_REQ_FLAG_OPTION))
1456 trf->issue_eof = true;
Mark A. Greer165063f2014-03-10 11:56:22 -07001457 }
1458
1459 return 0;
1460}
1461
Mark A. Greer13b42722014-09-02 15:12:45 -07001462static int trf7970a_send_cmd(struct nfc_digital_dev *ddev,
Mark Greere2f0f672017-04-25 15:43:56 -07001463 struct sk_buff *skb, u16 timeout,
1464 nfc_digital_cmd_complete_t cb, void *arg)
Mark A. Greer165063f2014-03-10 11:56:22 -07001465{
1466 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
Mark A. Greer7a1e5552014-09-02 15:12:28 -07001467 u8 prefix[5];
Mark A. Greer165063f2014-03-10 11:56:22 -07001468 unsigned int len;
1469 int ret;
Mark A. Greeraff05642014-09-02 15:12:26 -07001470 u8 status;
Mark A. Greer165063f2014-03-10 11:56:22 -07001471
1472 dev_dbg(trf->dev, "New request - state: %d, timeout: %d ms, len: %d\n",
Mark Greere2f0f672017-04-25 15:43:56 -07001473 trf->state, timeout, skb->len);
Mark A. Greer165063f2014-03-10 11:56:22 -07001474
1475 if (skb->len > TRF7970A_TX_MAX)
1476 return -EINVAL;
1477
1478 mutex_lock(&trf->lock);
1479
1480 if ((trf->state != TRF7970A_ST_IDLE) &&
Mark Greere2f0f672017-04-25 15:43:56 -07001481 (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) {
Mark A. Greer165063f2014-03-10 11:56:22 -07001482 dev_err(trf->dev, "%s - Bogus state: %d\n", __func__,
Mark Greere2f0f672017-04-25 15:43:56 -07001483 trf->state);
Mark A. Greer165063f2014-03-10 11:56:22 -07001484 ret = -EIO;
1485 goto out_err;
1486 }
1487
1488 if (trf->aborting) {
1489 dev_dbg(trf->dev, "Abort process complete\n");
1490 trf->aborting = false;
1491 ret = -ECANCELED;
1492 goto out_err;
1493 }
1494
Mark A. Greer19618432014-09-02 15:12:44 -07001495 if (timeout) {
1496 trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE,
Mark Greere2f0f672017-04-25 15:43:56 -07001497 GFP_KERNEL);
Mark A. Greer19618432014-09-02 15:12:44 -07001498 if (!trf->rx_skb) {
1499 dev_dbg(trf->dev, "Can't alloc rx_skb\n");
1500 ret = -ENOMEM;
1501 goto out_err;
1502 }
Mark A. Greer165063f2014-03-10 11:56:22 -07001503 }
1504
1505 if (trf->state == TRF7970A_ST_IDLE_RX_BLOCKED) {
1506 ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX);
1507 if (ret)
1508 goto out_err;
1509
1510 trf->state = TRF7970A_ST_IDLE;
1511 }
1512
Mark A. Greer13b42722014-09-02 15:12:45 -07001513 if (trf->is_initiator) {
1514 ret = trf7970a_per_cmd_config(trf, skb);
1515 if (ret)
1516 goto out_err;
1517 }
Mark A. Greer165063f2014-03-10 11:56:22 -07001518
1519 trf->ddev = ddev;
1520 trf->tx_skb = skb;
1521 trf->cb = cb;
1522 trf->cb_arg = arg;
1523 trf->timeout = timeout;
1524 trf->ignore_timeout = false;
1525
1526 len = skb->len;
Mark A. Greer165063f2014-03-10 11:56:22 -07001527
1528 /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
1529 * on what the current framing is, the address of the TX length byte 1
1530 * register (0x1d), and the 2 byte length of the data to be transmitted.
Mark A. Greer7a1e5552014-09-02 15:12:28 -07001531 * That totals 5 bytes.
Mark A. Greer165063f2014-03-10 11:56:22 -07001532 */
1533 prefix[0] = TRF7970A_CMD_BIT_CTRL |
Mark Greere2f0f672017-04-25 15:43:56 -07001534 TRF7970A_CMD_BIT_OPCODE(TRF7970A_CMD_FIFO_RESET);
Mark A. Greer165063f2014-03-10 11:56:22 -07001535 prefix[1] = TRF7970A_CMD_BIT_CTRL |
Mark Greere2f0f672017-04-25 15:43:56 -07001536 TRF7970A_CMD_BIT_OPCODE(trf->tx_cmd);
Mark A. Greer165063f2014-03-10 11:56:22 -07001537 prefix[2] = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_TX_LENGTH_BYTE1;
1538
1539 if (trf->framing == NFC_DIGITAL_FRAMING_NFCA_SHORT) {
1540 prefix[3] = 0x00;
Mark Greere2f0f672017-04-25 15:43:56 -07001541 prefix[4] = 0x0f; /* 7 bits */
Mark A. Greer165063f2014-03-10 11:56:22 -07001542 } else {
1543 prefix[3] = (len & 0xf00) >> 4;
1544 prefix[3] |= ((len & 0xf0) >> 4);
1545 prefix[4] = ((len & 0x0f) << 4);
1546 }
1547
1548 len = min_t(int, skb->len, TRF7970A_FIFO_SIZE);
1549
Mark A. Greeraff05642014-09-02 15:12:26 -07001550 /* Clear possible spurious interrupt */
1551 ret = trf7970a_read_irqstatus(trf, &status);
1552 if (ret)
1553 goto out_err;
1554
Mark A. Greer7a1e5552014-09-02 15:12:28 -07001555 ret = trf7970a_transmit(trf, skb, len, prefix, sizeof(prefix));
Mark A. Greer165063f2014-03-10 11:56:22 -07001556 if (ret) {
1557 kfree_skb(trf->rx_skb);
1558 trf->rx_skb = NULL;
1559 }
1560
1561out_err:
1562 mutex_unlock(&trf->lock);
1563 return ret;
1564}
1565
Mark A. Greer13b42722014-09-02 15:12:45 -07001566static int trf7970a_tg_config_rf_tech(struct trf7970a *trf, int tech)
Mark A. Greer165063f2014-03-10 11:56:22 -07001567{
Mark A. Greer13b42722014-09-02 15:12:45 -07001568 int ret = 0;
Mark A. Greer165063f2014-03-10 11:56:22 -07001569
Mark A. Greer13b42722014-09-02 15:12:45 -07001570 dev_dbg(trf->dev, "rf technology: %d\n", tech);
Mark A. Greer165063f2014-03-10 11:56:22 -07001571
Mark A. Greer13b42722014-09-02 15:12:45 -07001572 switch (tech) {
1573 case NFC_DIGITAL_RF_TECH_106A:
1574 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
Mark Greere2f0f672017-04-25 15:43:56 -07001575 TRF7970A_ISO_CTRL_NFC_CE | TRF7970A_ISO_CTRL_NFC_CE_14443A;
Geoff Lansberry837eb4d2016-12-21 23:18:32 -05001576 trf->modulator_sys_clk_ctrl =
Mark Greere2f0f672017-04-25 15:43:56 -07001577 (trf->modulator_sys_clk_ctrl & 0xf8) |
1578 TRF7970A_MODULATOR_DEPTH_OOK;
Mark A. Greer13b42722014-09-02 15:12:45 -07001579 break;
1580 case NFC_DIGITAL_RF_TECH_212F:
1581 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
Mark Greere2f0f672017-04-25 15:43:56 -07001582 TRF7970A_ISO_CTRL_NFC_NFCF_212;
Geoff Lansberry837eb4d2016-12-21 23:18:32 -05001583 trf->modulator_sys_clk_ctrl =
Mark Greere2f0f672017-04-25 15:43:56 -07001584 (trf->modulator_sys_clk_ctrl & 0xf8) |
1585 TRF7970A_MODULATOR_DEPTH_ASK10;
Mark A. Greer13b42722014-09-02 15:12:45 -07001586 break;
1587 case NFC_DIGITAL_RF_TECH_424F:
1588 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
Mark Greere2f0f672017-04-25 15:43:56 -07001589 TRF7970A_ISO_CTRL_NFC_NFCF_424;
Geoff Lansberry837eb4d2016-12-21 23:18:32 -05001590 trf->modulator_sys_clk_ctrl =
Mark Greere2f0f672017-04-25 15:43:56 -07001591 (trf->modulator_sys_clk_ctrl & 0xf8) |
1592 TRF7970A_MODULATOR_DEPTH_ASK10;
Mark A. Greer13b42722014-09-02 15:12:45 -07001593 break;
1594 default:
1595 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
1596 return -EINVAL;
1597 }
1598
1599 trf->technology = tech;
1600
1601 /* Normally we write the ISO_CTRL register in
1602 * trf7970a_tg_config_framing() because the framing can change
1603 * the value written. However, when sending a PSL RES,
1604 * digital_tg_send_psl_res_complete() doesn't call
1605 * trf7970a_tg_config_framing() so we must write the register
1606 * here.
1607 */
1608 if ((trf->framing == NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED) &&
Mark Greere2f0f672017-04-25 15:43:56 -07001609 (trf->iso_ctrl_tech != trf->iso_ctrl)) {
Mark A. Greer13b42722014-09-02 15:12:45 -07001610 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL,
Mark Greere2f0f672017-04-25 15:43:56 -07001611 trf->iso_ctrl_tech);
Mark A. Greer13b42722014-09-02 15:12:45 -07001612
1613 trf->iso_ctrl = trf->iso_ctrl_tech;
1614 }
1615
1616 return ret;
Mark A. Greer165063f2014-03-10 11:56:22 -07001617}
1618
Mark A. Greer13b42722014-09-02 15:12:45 -07001619/* Since this is a target routine, several of the framing calls are
1620 * made between receiving the request and sending the response so they
1621 * should take effect until after the response is sent. This is accomplished
1622 * by skipping the ISO_CTRL register write here and doing it in the interrupt
1623 * handler.
1624 */
1625static int trf7970a_tg_config_framing(struct trf7970a *trf, int framing)
1626{
1627 u8 iso_ctrl = trf->iso_ctrl_tech;
1628 int ret;
1629
1630 dev_dbg(trf->dev, "framing: %d\n", framing);
1631
1632 switch (framing) {
1633 case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
1634 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
1635 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
1636 break;
1637 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
1638 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
1639 case NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE:
1640 /* These ones are applied in the interrupt handler */
1641 iso_ctrl = trf->iso_ctrl; /* Don't write to ISO_CTRL yet */
1642 break;
1643 case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP:
1644 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1645 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
1646 break;
1647 case NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED:
1648 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1649 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
1650 break;
1651 default:
1652 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
1653 return -EINVAL;
1654 }
1655
1656 trf->framing = framing;
1657
1658 if (iso_ctrl != trf->iso_ctrl) {
1659 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1660 if (ret)
1661 return ret;
1662
1663 trf->iso_ctrl = iso_ctrl;
1664
1665 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
Mark Greere2f0f672017-04-25 15:43:56 -07001666 trf->modulator_sys_clk_ctrl);
Mark A. Greer13b42722014-09-02 15:12:45 -07001667 if (ret)
1668 return ret;
1669 }
1670
1671 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
1672 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
Mark Greere2f0f672017-04-25 15:43:56 -07001673 trf->chip_status_ctrl |
1674 TRF7970A_CHIP_STATUS_RF_ON);
Mark A. Greer13b42722014-09-02 15:12:45 -07001675 if (ret)
1676 return ret;
1677
1678 trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
1679 }
1680
1681 return 0;
1682}
1683
1684static int trf7970a_tg_configure_hw(struct nfc_digital_dev *ddev, int type,
Mark Greere2f0f672017-04-25 15:43:56 -07001685 int param)
Mark A. Greer13b42722014-09-02 15:12:45 -07001686{
1687 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1688 int ret;
1689
1690 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
1691
1692 mutex_lock(&trf->lock);
1693
1694 trf->is_initiator = false;
1695
1696 if ((trf->state == TRF7970A_ST_PWR_OFF) ||
Mark Greere2f0f672017-04-25 15:43:56 -07001697 (trf->state == TRF7970A_ST_RF_OFF)) {
Mark A. Greer13b42722014-09-02 15:12:45 -07001698 ret = trf7970a_switch_rf_on(trf);
1699 if (ret)
1700 goto err_unlock;
1701 }
1702
1703 switch (type) {
1704 case NFC_DIGITAL_CONFIG_RF_TECH:
1705 ret = trf7970a_tg_config_rf_tech(trf, param);
1706 break;
1707 case NFC_DIGITAL_CONFIG_FRAMING:
1708 ret = trf7970a_tg_config_framing(trf, param);
1709 break;
1710 default:
1711 dev_dbg(trf->dev, "Unknown type: %d\n", type);
1712 ret = -EINVAL;
1713 }
1714
1715err_unlock:
1716 mutex_unlock(&trf->lock);
1717 return ret;
1718}
1719
Mark A. Greercb174ab2014-09-02 15:12:46 -07001720static int _trf7970a_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
Mark Greere2f0f672017-04-25 15:43:56 -07001721 nfc_digital_cmd_complete_t cb, void *arg,
1722 bool mode_detect)
Mark A. Greer165063f2014-03-10 11:56:22 -07001723{
1724 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
Mark A. Greer13b42722014-09-02 15:12:45 -07001725 int ret;
Mark A. Greer165063f2014-03-10 11:56:22 -07001726
Mark A. Greer13b42722014-09-02 15:12:45 -07001727 mutex_lock(&trf->lock);
Mark A. Greer165063f2014-03-10 11:56:22 -07001728
Mark A. Greer13b42722014-09-02 15:12:45 -07001729 if ((trf->state != TRF7970A_ST_IDLE) &&
Mark Greere2f0f672017-04-25 15:43:56 -07001730 (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) {
Mark A. Greer13b42722014-09-02 15:12:45 -07001731 dev_err(trf->dev, "%s - Bogus state: %d\n", __func__,
Mark Greere2f0f672017-04-25 15:43:56 -07001732 trf->state);
Mark A. Greer13b42722014-09-02 15:12:45 -07001733 ret = -EIO;
1734 goto out_err;
1735 }
Mark A. Greer165063f2014-03-10 11:56:22 -07001736
Mark A. Greer13b42722014-09-02 15:12:45 -07001737 if (trf->aborting) {
1738 dev_dbg(trf->dev, "Abort process complete\n");
1739 trf->aborting = false;
1740 ret = -ECANCELED;
1741 goto out_err;
1742 }
Mark A. Greer165063f2014-03-10 11:56:22 -07001743
Mark A. Greer13b42722014-09-02 15:12:45 -07001744 trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE,
Mark Greere2f0f672017-04-25 15:43:56 -07001745 GFP_KERNEL);
Mark A. Greer13b42722014-09-02 15:12:45 -07001746 if (!trf->rx_skb) {
1747 dev_dbg(trf->dev, "Can't alloc rx_skb\n");
1748 ret = -ENOMEM;
1749 goto out_err;
1750 }
1751
1752 ret = trf7970a_write(trf, TRF7970A_RX_SPECIAL_SETTINGS,
Mark Greere2f0f672017-04-25 15:43:56 -07001753 TRF7970A_RX_SPECIAL_SETTINGS_HBT |
1754 TRF7970A_RX_SPECIAL_SETTINGS_M848 |
1755 TRF7970A_RX_SPECIAL_SETTINGS_C424 |
1756 TRF7970A_RX_SPECIAL_SETTINGS_C212);
Mark A. Greer13b42722014-09-02 15:12:45 -07001757 if (ret)
Mark A. Greerfc0ae242014-09-23 11:25:48 -07001758 goto out_err;
Mark A. Greer13b42722014-09-02 15:12:45 -07001759
1760 ret = trf7970a_write(trf, TRF7970A_REG_IO_CTRL,
Mark Greere2f0f672017-04-25 15:43:56 -07001761 trf->io_ctrl | TRF7970A_REG_IO_CTRL_VRS(0x1));
Mark A. Greer13b42722014-09-02 15:12:45 -07001762 if (ret)
Mark A. Greerfc0ae242014-09-23 11:25:48 -07001763 goto out_err;
Mark A. Greer13b42722014-09-02 15:12:45 -07001764
1765 ret = trf7970a_write(trf, TRF7970A_NFC_LOW_FIELD_LEVEL,
Mark Greere2f0f672017-04-25 15:43:56 -07001766 TRF7970A_NFC_LOW_FIELD_LEVEL_RFDET(0x3));
Mark A. Greer13b42722014-09-02 15:12:45 -07001767 if (ret)
Mark A. Greerfc0ae242014-09-23 11:25:48 -07001768 goto out_err;
Mark A. Greer13b42722014-09-02 15:12:45 -07001769
1770 ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL,
Mark Greere2f0f672017-04-25 15:43:56 -07001771 TRF7970A_NFC_TARGET_LEVEL_RFDET(0x7));
Mark A. Greer13b42722014-09-02 15:12:45 -07001772 if (ret)
Mark A. Greerfc0ae242014-09-23 11:25:48 -07001773 goto out_err;
Mark A. Greer13b42722014-09-02 15:12:45 -07001774
1775 trf->ddev = ddev;
1776 trf->cb = cb;
1777 trf->cb_arg = arg;
1778 trf->timeout = timeout;
1779 trf->ignore_timeout = false;
1780
1781 ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX);
1782 if (ret)
1783 goto out_err;
1784
Mark A. Greercb174ab2014-09-02 15:12:46 -07001785 trf->state = mode_detect ? TRF7970A_ST_LISTENING_MD :
1786 TRF7970A_ST_LISTENING;
Mark A. Greer13b42722014-09-02 15:12:45 -07001787
1788 schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout));
1789
1790out_err:
1791 mutex_unlock(&trf->lock);
1792 return ret;
Mark A. Greer165063f2014-03-10 11:56:22 -07001793}
1794
Mark A. Greercb174ab2014-09-02 15:12:46 -07001795static int trf7970a_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
Mark Greere2f0f672017-04-25 15:43:56 -07001796 nfc_digital_cmd_complete_t cb, void *arg)
Mark A. Greercb174ab2014-09-02 15:12:46 -07001797{
1798 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1799
1800 dev_dbg(trf->dev, "Listen - state: %d, timeout: %d ms\n",
Mark Greere2f0f672017-04-25 15:43:56 -07001801 trf->state, timeout);
Mark A. Greercb174ab2014-09-02 15:12:46 -07001802
1803 return _trf7970a_tg_listen(ddev, timeout, cb, arg, false);
1804}
1805
1806static int trf7970a_tg_listen_md(struct nfc_digital_dev *ddev,
Mark Greere2f0f672017-04-25 15:43:56 -07001807 u16 timeout, nfc_digital_cmd_complete_t cb,
1808 void *arg)
Mark A. Greercb174ab2014-09-02 15:12:46 -07001809{
1810 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1811 int ret;
1812
1813 dev_dbg(trf->dev, "Listen MD - state: %d, timeout: %d ms\n",
Mark Greere2f0f672017-04-25 15:43:56 -07001814 trf->state, timeout);
Mark A. Greercb174ab2014-09-02 15:12:46 -07001815
1816 ret = trf7970a_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
Mark Greere2f0f672017-04-25 15:43:56 -07001817 NFC_DIGITAL_RF_TECH_106A);
Mark A. Greercb174ab2014-09-02 15:12:46 -07001818 if (ret)
1819 return ret;
1820
1821 ret = trf7970a_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
Mark Greere2f0f672017-04-25 15:43:56 -07001822 NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
Mark A. Greercb174ab2014-09-02 15:12:46 -07001823 if (ret)
1824 return ret;
1825
1826 return _trf7970a_tg_listen(ddev, timeout, cb, arg, true);
1827}
1828
1829static int trf7970a_tg_get_rf_tech(struct nfc_digital_dev *ddev, u8 *rf_tech)
1830{
1831 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1832
1833 dev_dbg(trf->dev, "Get RF Tech - state: %d, rf_tech: %d\n",
Mark Greere2f0f672017-04-25 15:43:56 -07001834 trf->state, trf->md_rf_tech);
Mark A. Greercb174ab2014-09-02 15:12:46 -07001835
1836 *rf_tech = trf->md_rf_tech;
1837
1838 return 0;
1839}
1840
Mark A. Greer165063f2014-03-10 11:56:22 -07001841static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev)
1842{
1843 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1844
1845 dev_dbg(trf->dev, "Abort process initiated\n");
1846
1847 mutex_lock(&trf->lock);
Mark A. Greer5876bc72014-03-25 08:54:30 -07001848
1849 switch (trf->state) {
1850 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
1851 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1852 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1853 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
1854 trf->aborting = true;
1855 break;
Mark A. Greer13b42722014-09-02 15:12:45 -07001856 case TRF7970A_ST_LISTENING:
1857 trf->ignore_timeout = !cancel_delayed_work(&trf->timeout_work);
1858 trf7970a_send_err_upstream(trf, -ECANCELED);
1859 dev_dbg(trf->dev, "Abort process complete\n");
1860 break;
Mark A. Greer5876bc72014-03-25 08:54:30 -07001861 default:
1862 break;
1863 }
1864
Mark A. Greer165063f2014-03-10 11:56:22 -07001865 mutex_unlock(&trf->lock);
1866}
1867
1868static struct nfc_digital_ops trf7970a_nfc_ops = {
1869 .in_configure_hw = trf7970a_in_configure_hw,
Mark A. Greer13b42722014-09-02 15:12:45 -07001870 .in_send_cmd = trf7970a_send_cmd,
Mark A. Greer165063f2014-03-10 11:56:22 -07001871 .tg_configure_hw = trf7970a_tg_configure_hw,
Mark A. Greer13b42722014-09-02 15:12:45 -07001872 .tg_send_cmd = trf7970a_send_cmd,
Mark A. Greer165063f2014-03-10 11:56:22 -07001873 .tg_listen = trf7970a_tg_listen,
Mark A. Greercb174ab2014-09-02 15:12:46 -07001874 .tg_listen_md = trf7970a_tg_listen_md,
1875 .tg_get_rf_tech = trf7970a_tg_get_rf_tech,
Mark A. Greer165063f2014-03-10 11:56:22 -07001876 .switch_rf = trf7970a_switch_rf,
1877 .abort_cmd = trf7970a_abort_cmd,
1878};
1879
Mark A. Greerceccd6a2014-09-02 15:12:35 -07001880static int trf7970a_power_up(struct trf7970a *trf)
1881{
1882 int ret;
1883
1884 dev_dbg(trf->dev, "Powering up - state: %d\n", trf->state);
1885
1886 if (trf->state != TRF7970A_ST_PWR_OFF)
1887 return 0;
1888
1889 ret = regulator_enable(trf->regulator);
1890 if (ret) {
1891 dev_err(trf->dev, "%s - Can't enable VIN: %d\n", __func__, ret);
1892 return ret;
1893 }
1894
1895 usleep_range(5000, 6000);
1896
Mark Greerd34e48d2017-04-25 15:43:55 -07001897 if (trf->en2_gpiod &&
1898 !(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW)) {
1899 gpiod_set_value_cansleep(trf->en2_gpiod, 1);
1900 usleep_range(1000, 2000);
Mark A. Greerceccd6a2014-09-02 15:12:35 -07001901 }
1902
Mark Greerd34e48d2017-04-25 15:43:55 -07001903 gpiod_set_value_cansleep(trf->en_gpiod, 1);
Mark A. Greerceccd6a2014-09-02 15:12:35 -07001904
1905 usleep_range(20000, 21000);
1906
1907 trf->state = TRF7970A_ST_RF_OFF;
1908
1909 return 0;
1910}
1911
1912static int trf7970a_power_down(struct trf7970a *trf)
1913{
1914 int ret;
1915
1916 dev_dbg(trf->dev, "Powering down - state: %d\n", trf->state);
1917
1918 if (trf->state == TRF7970A_ST_PWR_OFF)
1919 return 0;
1920
1921 if (trf->state != TRF7970A_ST_RF_OFF) {
1922 dev_dbg(trf->dev, "Can't power down - not RF_OFF state (%d)\n",
Mark Greere2f0f672017-04-25 15:43:56 -07001923 trf->state);
Mark A. Greerceccd6a2014-09-02 15:12:35 -07001924 return -EBUSY;
1925 }
1926
Mark Greerd34e48d2017-04-25 15:43:55 -07001927 gpiod_set_value_cansleep(trf->en_gpiod, 0);
Mark Greer67dec192017-04-25 15:43:49 -07001928
Mark Greere2f0f672017-04-25 15:43:56 -07001929 if (trf->en2_gpiod && !(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW))
Mark Greerd34e48d2017-04-25 15:43:55 -07001930 gpiod_set_value_cansleep(trf->en2_gpiod, 0);
Mark A. Greerceccd6a2014-09-02 15:12:35 -07001931
1932 ret = regulator_disable(trf->regulator);
1933 if (ret)
1934 dev_err(trf->dev, "%s - Can't disable VIN: %d\n", __func__,
Mark Greere2f0f672017-04-25 15:43:56 -07001935 ret);
Mark A. Greerceccd6a2014-09-02 15:12:35 -07001936
1937 trf->state = TRF7970A_ST_PWR_OFF;
1938
1939 return ret;
1940}
1941
Mark A. Greerb5282812014-09-02 15:12:36 -07001942static int trf7970a_startup(struct trf7970a *trf)
1943{
1944 int ret;
1945
1946 ret = trf7970a_power_up(trf);
1947 if (ret)
1948 return ret;
1949
1950 pm_runtime_set_active(trf->dev);
1951 pm_runtime_enable(trf->dev);
1952 pm_runtime_mark_last_busy(trf->dev);
1953
1954 return 0;
1955}
1956
1957static void trf7970a_shutdown(struct trf7970a *trf)
1958{
1959 switch (trf->state) {
1960 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
1961 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1962 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1963 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
Mark A. Greer13b42722014-09-02 15:12:45 -07001964 case TRF7970A_ST_LISTENING:
Mark A. Greerb5282812014-09-02 15:12:36 -07001965 trf7970a_send_err_upstream(trf, -ECANCELED);
1966 /* FALLTHROUGH */
1967 case TRF7970A_ST_IDLE:
1968 case TRF7970A_ST_IDLE_RX_BLOCKED:
1969 trf7970a_switch_rf_off(trf);
1970 break;
1971 default:
1972 break;
1973 }
1974
1975 pm_runtime_disable(trf->dev);
1976 pm_runtime_set_suspended(trf->dev);
1977
1978 trf7970a_power_down(trf);
1979}
1980
Mark A. Greerfd0c8282014-03-25 08:54:40 -07001981static int trf7970a_get_autosuspend_delay(struct device_node *np)
1982{
1983 int autosuspend_delay, ret;
1984
1985 ret = of_property_read_u32(np, "autosuspend-delay", &autosuspend_delay);
1986 if (ret)
1987 autosuspend_delay = TRF7970A_AUTOSUSPEND_DELAY;
1988
Mark A. Greerfd0c8282014-03-25 08:54:40 -07001989 return autosuspend_delay;
1990}
1991
Mark A. Greer165063f2014-03-10 11:56:22 -07001992static int trf7970a_probe(struct spi_device *spi)
1993{
1994 struct device_node *np = spi->dev.of_node;
Mark A. Greer165063f2014-03-10 11:56:22 -07001995 struct trf7970a *trf;
Mark A. Greerfd0c8282014-03-25 08:54:40 -07001996 int uvolts, autosuspend_delay, ret;
Geoff Lansberry837eb4d2016-12-21 23:18:32 -05001997 u32 clk_freq = TRF7970A_13MHZ_CLOCK_FREQUENCY;
Mark A. Greer165063f2014-03-10 11:56:22 -07001998
1999 if (!np) {
2000 dev_err(&spi->dev, "No Device Tree entry\n");
2001 return -EINVAL;
2002 }
2003
2004 trf = devm_kzalloc(&spi->dev, sizeof(*trf), GFP_KERNEL);
2005 if (!trf)
2006 return -ENOMEM;
2007
Mark A. Greerceccd6a2014-09-02 15:12:35 -07002008 trf->state = TRF7970A_ST_PWR_OFF;
Mark A. Greer165063f2014-03-10 11:56:22 -07002009 trf->dev = &spi->dev;
2010 trf->spi = spi;
Mark A. Greer165063f2014-03-10 11:56:22 -07002011
2012 spi->mode = SPI_MODE_1;
2013 spi->bits_per_word = 8;
2014
Mark A. Greer24707292014-09-02 15:12:17 -07002015 ret = spi_setup(spi);
2016 if (ret < 0) {
2017 dev_err(trf->dev, "Can't set up SPI Communication\n");
2018 return ret;
2019 }
2020
Mark A. Greer772079e2014-08-07 17:41:43 -07002021 if (of_property_read_bool(np, "irq-status-read-quirk"))
2022 trf->quirks |= TRF7970A_QUIRK_IRQ_STATUS_READ;
2023
Mark Greer69f984f2017-04-25 15:43:50 -07002024 /* There are two enable pins - only EN must be present in the DT */
Mark Greerd34e48d2017-04-25 15:43:55 -07002025 trf->en_gpiod = devm_gpiod_get_index(trf->dev, "ti,enable", 0,
2026 GPIOD_OUT_LOW);
2027 if (IS_ERR(trf->en_gpiod)) {
Mark A. Greer165063f2014-03-10 11:56:22 -07002028 dev_err(trf->dev, "No EN GPIO property\n");
Mark Greerd34e48d2017-04-25 15:43:55 -07002029 return PTR_ERR(trf->en_gpiod);
Mark A. Greer165063f2014-03-10 11:56:22 -07002030 }
2031
Mark Greerd34e48d2017-04-25 15:43:55 -07002032 trf->en2_gpiod = devm_gpiod_get_index_optional(trf->dev, "ti,enable", 1,
2033 GPIOD_OUT_LOW);
2034 if (!trf->en2_gpiod) {
Guan Bence69b952017-02-07 06:22:04 +01002035 dev_info(trf->dev, "No EN2 GPIO property\n");
Mark Greerd34e48d2017-04-25 15:43:55 -07002036 } else if (IS_ERR(trf->en2_gpiod)) {
2037 dev_err(trf->dev, "Error getting EN2 GPIO property: %ld\n",
2038 PTR_ERR(trf->en2_gpiod));
2039 return PTR_ERR(trf->en2_gpiod);
2040 } else if (of_property_read_bool(np, "en2-rf-quirk")) {
2041 trf->quirks |= TRF7970A_QUIRK_EN2_MUST_STAY_LOW;
Mark A. Greer165063f2014-03-10 11:56:22 -07002042 }
2043
Geoff Lansberry837eb4d2016-12-21 23:18:32 -05002044 of_property_read_u32(np, "clock-frequency", &clk_freq);
Colin Ian Kinge1038532017-04-24 14:36:02 +01002045 if ((clk_freq != TRF7970A_27MHZ_CLOCK_FREQUENCY) &&
Mark Greere2f0f672017-04-25 15:43:56 -07002046 (clk_freq != TRF7970A_13MHZ_CLOCK_FREQUENCY)) {
Geoff Lansberry837eb4d2016-12-21 23:18:32 -05002047 dev_err(trf->dev,
Mark Greere2f0f672017-04-25 15:43:56 -07002048 "clock-frequency (%u Hz) unsupported\n", clk_freq);
Geoff Lansberry837eb4d2016-12-21 23:18:32 -05002049 return -EINVAL;
2050 }
2051
Geoff Lansberrybd751802017-04-27 17:28:46 -04002052 if (clk_freq == TRF7970A_27MHZ_CLOCK_FREQUENCY) {
2053 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_27MHZ;
2054 dev_dbg(trf->dev, "trf7970a configured for 27MHz crystal\n");
2055 } else {
2056 trf->modulator_sys_clk_ctrl = 0;
2057 }
2058
Mark A. Greer165063f2014-03-10 11:56:22 -07002059 ret = devm_request_threaded_irq(trf->dev, spi->irq, NULL,
Mark Greere2f0f672017-04-25 15:43:56 -07002060 trf7970a_irq,
2061 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
2062 "trf7970a", trf);
Mark A. Greer165063f2014-03-10 11:56:22 -07002063 if (ret) {
2064 dev_err(trf->dev, "Can't request IRQ#%d: %d\n", spi->irq, ret);
2065 return ret;
2066 }
2067
2068 mutex_init(&trf->lock);
2069 INIT_DELAYED_WORK(&trf->timeout_work, trf7970a_timeout_work_handler);
2070
2071 trf->regulator = devm_regulator_get(&spi->dev, "vin");
2072 if (IS_ERR(trf->regulator)) {
2073 ret = PTR_ERR(trf->regulator);
2074 dev_err(trf->dev, "Can't get VIN regulator: %d\n", ret);
2075 goto err_destroy_lock;
2076 }
2077
2078 ret = regulator_enable(trf->regulator);
2079 if (ret) {
2080 dev_err(trf->dev, "Can't enable VIN: %d\n", ret);
2081 goto err_destroy_lock;
2082 }
2083
Mark Greera34631c2017-04-25 15:43:53 -07002084 uvolts = regulator_get_voltage(trf->regulator);
Mark A. Greerebcc5a02014-03-25 08:54:36 -07002085 if (uvolts > 4000000)
2086 trf->chip_status_ctrl = TRF7970A_CHIP_STATUS_VRS5_3;
2087
Geoff Lansberry49d22c72016-12-21 23:18:33 -05002088 trf->regulator = devm_regulator_get(&spi->dev, "vdd-io");
2089 if (IS_ERR(trf->regulator)) {
2090 ret = PTR_ERR(trf->regulator);
2091 dev_err(trf->dev, "Can't get VDD_IO regulator: %d\n", ret);
2092 goto err_destroy_lock;
2093 }
2094
2095 ret = regulator_enable(trf->regulator);
2096 if (ret) {
2097 dev_err(trf->dev, "Can't enable VDD_IO: %d\n", ret);
2098 goto err_destroy_lock;
2099 }
2100
2101 if (regulator_get_voltage(trf->regulator) == 1800000) {
2102 trf->io_ctrl = TRF7970A_REG_IO_CTRL_IO_LOW;
2103 dev_dbg(trf->dev, "trf7970a config vdd_io to 1.8V\n");
2104 }
2105
Mark A. Greer165063f2014-03-10 11:56:22 -07002106 trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops,
Mark Greere2f0f672017-04-25 15:43:56 -07002107 TRF7970A_SUPPORTED_PROTOCOLS,
2108 NFC_DIGITAL_DRV_CAPS_IN_CRC |
2109 NFC_DIGITAL_DRV_CAPS_TG_CRC, 0,
2110 0);
Mark A. Greer165063f2014-03-10 11:56:22 -07002111 if (!trf->ddev) {
2112 dev_err(trf->dev, "Can't allocate NFC digital device\n");
2113 ret = -ENOMEM;
2114 goto err_disable_regulator;
2115 }
2116
2117 nfc_digital_set_parent_dev(trf->ddev, trf->dev);
2118 nfc_digital_set_drvdata(trf->ddev, trf);
2119 spi_set_drvdata(spi, trf);
2120
Mark A. Greerfd0c8282014-03-25 08:54:40 -07002121 autosuspend_delay = trf7970a_get_autosuspend_delay(np);
2122
2123 pm_runtime_set_autosuspend_delay(trf->dev, autosuspend_delay);
Mark A. Greere6403b72014-03-25 08:54:38 -07002124 pm_runtime_use_autosuspend(trf->dev);
Mark A. Greerceccd6a2014-09-02 15:12:35 -07002125
Mark A. Greerb5282812014-09-02 15:12:36 -07002126 ret = trf7970a_startup(trf);
Mark A. Greerceccd6a2014-09-02 15:12:35 -07002127 if (ret)
2128 goto err_free_ddev;
2129
Mark A. Greer165063f2014-03-10 11:56:22 -07002130 ret = nfc_digital_register_device(trf->ddev);
2131 if (ret) {
2132 dev_err(trf->dev, "Can't register NFC digital device: %d\n",
Mark Greere2f0f672017-04-25 15:43:56 -07002133 ret);
Mark A. Greerb5282812014-09-02 15:12:36 -07002134 goto err_shutdown;
Mark A. Greer165063f2014-03-10 11:56:22 -07002135 }
2136
2137 return 0;
2138
Mark A. Greerb5282812014-09-02 15:12:36 -07002139err_shutdown:
2140 trf7970a_shutdown(trf);
Mark A. Greerceccd6a2014-09-02 15:12:35 -07002141err_free_ddev:
Mark A. Greer165063f2014-03-10 11:56:22 -07002142 nfc_digital_free_device(trf->ddev);
2143err_disable_regulator:
2144 regulator_disable(trf->regulator);
2145err_destroy_lock:
2146 mutex_destroy(&trf->lock);
2147 return ret;
2148}
2149
2150static int trf7970a_remove(struct spi_device *spi)
2151{
2152 struct trf7970a *trf = spi_get_drvdata(spi);
2153
2154 mutex_lock(&trf->lock);
2155
Mark A. Greerb5282812014-09-02 15:12:36 -07002156 trf7970a_shutdown(trf);
Mark A. Greerceccd6a2014-09-02 15:12:35 -07002157
2158 mutex_unlock(&trf->lock);
Mark A. Greere6403b72014-03-25 08:54:38 -07002159
Mark A. Greer165063f2014-03-10 11:56:22 -07002160 nfc_digital_unregister_device(trf->ddev);
2161 nfc_digital_free_device(trf->ddev);
2162
2163 regulator_disable(trf->regulator);
2164
2165 mutex_destroy(&trf->lock);
2166
2167 return 0;
2168}
2169
Mark A. Greer77c95392014-09-02 15:12:37 -07002170#ifdef CONFIG_PM_SLEEP
2171static int trf7970a_suspend(struct device *dev)
2172{
Geliang Tang88e2ce02015-12-23 00:18:42 +08002173 struct spi_device *spi = to_spi_device(dev);
Mark A. Greer77c95392014-09-02 15:12:37 -07002174 struct trf7970a *trf = spi_get_drvdata(spi);
Mark A. Greer77c95392014-09-02 15:12:37 -07002175
2176 dev_dbg(dev, "Suspend\n");
2177
2178 mutex_lock(&trf->lock);
2179
2180 trf7970a_shutdown(trf);
2181
2182 mutex_unlock(&trf->lock);
2183
Mark A. Greer671970f2014-09-23 11:25:45 -07002184 return 0;
Mark A. Greer77c95392014-09-02 15:12:37 -07002185}
2186
2187static int trf7970a_resume(struct device *dev)
2188{
Geliang Tang88e2ce02015-12-23 00:18:42 +08002189 struct spi_device *spi = to_spi_device(dev);
Mark A. Greer77c95392014-09-02 15:12:37 -07002190 struct trf7970a *trf = spi_get_drvdata(spi);
Mark A. Greer55ef2e72014-09-23 11:25:46 -07002191 int ret;
Mark A. Greer77c95392014-09-02 15:12:37 -07002192
2193 dev_dbg(dev, "Resume\n");
2194
2195 mutex_lock(&trf->lock);
2196
2197 ret = trf7970a_startup(trf);
2198
2199 mutex_unlock(&trf->lock);
2200
2201 return ret;
2202}
2203#endif
2204
Rafael J. Wysockie8a92352014-12-14 23:14:36 +01002205#ifdef CONFIG_PM
Mark A. Greere6403b72014-03-25 08:54:38 -07002206static int trf7970a_pm_runtime_suspend(struct device *dev)
2207{
Geliang Tang88e2ce02015-12-23 00:18:42 +08002208 struct spi_device *spi = to_spi_device(dev);
Mark A. Greere6403b72014-03-25 08:54:38 -07002209 struct trf7970a *trf = spi_get_drvdata(spi);
2210 int ret;
2211
2212 dev_dbg(dev, "Runtime suspend\n");
2213
Mark A. Greerceccd6a2014-09-02 15:12:35 -07002214 mutex_lock(&trf->lock);
Mark A. Greere6403b72014-03-25 08:54:38 -07002215
Mark A. Greerceccd6a2014-09-02 15:12:35 -07002216 ret = trf7970a_power_down(trf);
Mark A. Greere6403b72014-03-25 08:54:38 -07002217
Mark A. Greerceccd6a2014-09-02 15:12:35 -07002218 mutex_unlock(&trf->lock);
Mark A. Greere6403b72014-03-25 08:54:38 -07002219
2220 return ret;
2221}
2222
2223static int trf7970a_pm_runtime_resume(struct device *dev)
2224{
Geliang Tang88e2ce02015-12-23 00:18:42 +08002225 struct spi_device *spi = to_spi_device(dev);
Mark A. Greere6403b72014-03-25 08:54:38 -07002226 struct trf7970a *trf = spi_get_drvdata(spi);
2227 int ret;
2228
2229 dev_dbg(dev, "Runtime resume\n");
2230
Mark A. Greerceccd6a2014-09-02 15:12:35 -07002231 ret = trf7970a_power_up(trf);
2232 if (!ret)
2233 pm_runtime_mark_last_busy(dev);
Mark A. Greere6403b72014-03-25 08:54:38 -07002234
Mark A. Greerceccd6a2014-09-02 15:12:35 -07002235 return ret;
Mark A. Greere6403b72014-03-25 08:54:38 -07002236}
2237#endif
2238
2239static const struct dev_pm_ops trf7970a_pm_ops = {
Mark A. Greer77c95392014-09-02 15:12:37 -07002240 SET_SYSTEM_SLEEP_PM_OPS(trf7970a_suspend, trf7970a_resume)
Mark A. Greere6403b72014-03-25 08:54:38 -07002241 SET_RUNTIME_PM_OPS(trf7970a_pm_runtime_suspend,
Mark Greere2f0f672017-04-25 15:43:56 -07002242 trf7970a_pm_runtime_resume, NULL)
Mark A. Greere6403b72014-03-25 08:54:38 -07002243};
2244
Javier Martinez Canillas3c39c1a2015-09-16 11:08:42 +02002245static const struct of_device_id trf7970a_of_match[] = {
Mark Greere2f0f672017-04-25 15:43:56 -07002246 {.compatible = "ti,trf7970a",},
Mark Greerfcc652f2017-04-25 15:43:52 -07002247 {},
Javier Martinez Canillas3c39c1a2015-09-16 11:08:42 +02002248};
Mark Greere2f0f672017-04-25 15:43:56 -07002249
Javier Martinez Canillas3c39c1a2015-09-16 11:08:42 +02002250MODULE_DEVICE_TABLE(of, trf7970a_of_match);
2251
Mark A. Greer165063f2014-03-10 11:56:22 -07002252static const struct spi_device_id trf7970a_id_table[] = {
Mark Greere2f0f672017-04-25 15:43:56 -07002253 {"trf7970a", 0},
2254 {}
Mark A. Greer165063f2014-03-10 11:56:22 -07002255};
Mark Greere2f0f672017-04-25 15:43:56 -07002256
Mark A. Greer165063f2014-03-10 11:56:22 -07002257MODULE_DEVICE_TABLE(spi, trf7970a_id_table);
2258
2259static struct spi_driver trf7970a_spi_driver = {
2260 .probe = trf7970a_probe,
2261 .remove = trf7970a_remove,
2262 .id_table = trf7970a_id_table,
Mark Greere2f0f672017-04-25 15:43:56 -07002263 .driver = {
2264 .name = "trf7970a",
2265 .of_match_table = of_match_ptr(trf7970a_of_match),
2266 .pm = &trf7970a_pm_ops,
Mark A. Greer165063f2014-03-10 11:56:22 -07002267 },
2268};
2269
2270module_spi_driver(trf7970a_spi_driver);
2271
2272MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>");
2273MODULE_LICENSE("GPL v2");
2274MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver");