blob: 00149a9a50ef6a9affc7e959a782701fc5ac67e6 [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>
23#include <linux/gpio.h>
24#include <linux/of.h>
25#include <linux/of_gpio.h>
26#include <linux/spi/spi.h>
27#include <linux/regulator/consumer.h>
28
29#include <net/nfc/nfc.h>
30#include <net/nfc/digital.h>
31
32/* There are 3 ways the host can communicate with the trf7970a:
33 * parallel mode, SPI with Slave Select (SS) mode, and SPI without
34 * SS mode. The driver only supports the two SPI modes.
35 *
36 * The trf7970a is very timing sensitive and the VIN, EN2, and EN
37 * pins must asserted in that order and with specific delays in between.
38 * The delays used in the driver were provided by TI and have been
Mark A. Greer95064bd2014-08-07 17:41:45 -070039 * confirmed to work with this driver. There is a bug with the current
40 * version of the trf7970a that requires that EN2 remain low no matter
41 * what. If it goes high, it will generate an RF field even when in
42 * passive target mode. TI has indicated that the chip will work okay
43 * when EN2 is left low. The 'en2-rf-quirk' device tree property
44 * indicates that trf7970a currently being used has the erratum and
45 * that EN2 must be kept low.
Mark A. Greer165063f2014-03-10 11:56:22 -070046 *
47 * Timeouts are implemented using the delayed workqueue kernel facility.
48 * Timeouts are required so things don't hang when there is no response
49 * from the trf7970a (or tag). Using this mechanism creates a race with
50 * interrupts, however. That is, an interrupt and a timeout could occur
51 * closely enough together that one is blocked by the mutex while the other
52 * executes. When the timeout handler executes first and blocks the
53 * interrupt handler, it will eventually set the state to IDLE so the
54 * interrupt handler will check the state and exit with no harm done.
55 * When the interrupt handler executes first and blocks the timeout handler,
56 * the cancel_delayed_work() call will know that it didn't cancel the
57 * work item (i.e., timeout) and will return zero. That return code is
58 * used by the timer handler to indicate that it should ignore the timeout
59 * once its unblocked.
60 *
61 * Aborting an active command isn't as simple as it seems because the only
62 * way to abort a command that's already been sent to the tag is so turn
63 * off power to the tag. If we do that, though, we'd have to go through
64 * the entire anticollision procedure again but the digital layer doesn't
Mark A. Greer13b42722014-09-02 15:12:45 -070065 * support that. So, if an abort is received before trf7970a_send_cmd()
Mark A. Greer165063f2014-03-10 11:56:22 -070066 * has sent the command to the tag, it simply returns -ECANCELED. If the
67 * command has already been sent to the tag, then the driver continues
68 * normally and recieves the response data (or error) but just before
69 * sending the data upstream, it frees the rx_skb and sends -ECANCELED
70 * upstream instead. If the command failed, that error will be sent
71 * upstream.
72 *
73 * When recieving data from a tag and the interrupt status register has
74 * only the SRX bit set, it means that all of the data has been received
75 * (once what's in the fifo has been read). However, depending on timing
76 * an interrupt status with only the SRX bit set may not be recived. In
Mark A. Greer5fa3af32014-03-25 08:54:29 -070077 * those cases, the timeout mechanism is used to wait 20 ms in case more
78 * data arrives. After 20 ms, it is assumed that all of the data has been
Mark A. Greer165063f2014-03-10 11:56:22 -070079 * received and the accumulated rx data is sent upstream. The
80 * 'TRF7970A_ST_WAIT_FOR_RX_DATA_CONT' state is used for this purpose
81 * (i.e., it indicates that some data has been received but we're not sure
82 * if there is more coming so a timeout in this state means all data has
Mark A. Greer5fa3af32014-03-25 08:54:29 -070083 * been received and there isn't an error). The delay is 20 ms since delays
84 * of ~16 ms have been observed during testing.
Mark A. Greer165063f2014-03-10 11:56:22 -070085 *
Mark A. Greer38b4eb12014-09-02 15:12:25 -070086 * When transmitting a frame larger than the FIFO size (127 bytes), the
87 * driver will wait 20 ms for the FIFO to drain past the low-watermark
88 * and generate an interrupt. The low-watermark set to 32 bytes so the
89 * interrupt should fire after 127 - 32 = 95 bytes have been sent. At
90 * the lowest possible bit rate (6.62 kbps for 15693), it will take up
91 * to ~14.35 ms so 20 ms is used for the timeout.
92 *
Mark A. Greer165063f2014-03-10 11:56:22 -070093 * Type 2 write and sector select commands respond with a 4-bit ACK or NACK.
94 * Having only 4 bits in the FIFO won't normally generate an interrupt so
95 * driver enables the '4_bit_RX' bit of the Special Functions register 1
96 * to cause an interrupt in that case. Leaving that bit for a read command
97 * messes up the data returned so it is only enabled when the framing is
98 * 'NFC_DIGITAL_FRAMING_NFCA_T2T' and the command is not a read command.
99 * Unfortunately, that means that the driver has to peek into tx frames
100 * when the framing is 'NFC_DIGITAL_FRAMING_NFCA_T2T'. This is done by
101 * the trf7970a_per_cmd_config() routine.
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700102 *
103 * ISO/IEC 15693 frames specify whether to use single or double sub-carrier
104 * frequencies and whether to use low or high data rates in the flags byte
105 * of the frame. This means that the driver has to peek at all 15693 frames
106 * to determine what speed to set the communication to. In addition, write
107 * and lock commands use the OPTION flag to indicate that an EOF must be
108 * sent to the tag before it will send its response. So the driver has to
109 * examine all frames for that reason too.
110 *
111 * It is unclear how long to wait before sending the EOF. According to the
112 * Note under Table 1-1 in section 1.6 of
113 * http://www.ti.com/lit/ug/scbu011/scbu011.pdf, that wait should be at least
114 * 10 ms for TI Tag-it HF-I tags; however testing has shown that is not long
Mark A. Greer38b4eb12014-09-02 15:12:25 -0700115 * enough so 20 ms is used. So the timer is set to 40 ms - 20 ms to drain
116 * up to 127 bytes in the FIFO at the lowest bit rate plus another 20 ms to
117 * ensure the wait is long enough before sending the EOF. This seems to work
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700118 * reliably.
Mark A. Greer165063f2014-03-10 11:56:22 -0700119 */
120
Mark A. Greer80062892014-03-10 11:56:23 -0700121#define TRF7970A_SUPPORTED_PROTOCOLS \
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700122 (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \
Mark A. Greer6857bb92014-04-14 10:04:10 -0700123 NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_FELICA_MASK | \
Mark A. Greer13b42722014-09-02 15:12:45 -0700124 NFC_PROTO_ISO15693_MASK | NFC_PROTO_NFC_DEP_MASK)
Mark A. Greer165063f2014-03-10 11:56:22 -0700125
Mark A. Greere6403b72014-03-25 08:54:38 -0700126#define TRF7970A_AUTOSUSPEND_DELAY 30000 /* 30 seconds */
127
Mark A. Greer165063f2014-03-10 11:56:22 -0700128#define TRF7970A_RX_SKB_ALLOC_SIZE 256
129
Mark A. Greer1568bfe2014-09-02 15:12:20 -0700130#define TRF7970A_FIFO_SIZE 127
Mark A. Greer165063f2014-03-10 11:56:22 -0700131
132/* TX length is 3 nibbles long ==> 4KB - 1 bytes max */
133#define TRF7970A_TX_MAX (4096 - 1)
134
Mark A. Greer19618432014-09-02 15:12:44 -0700135#define TRF7970A_WAIT_FOR_TX_IRQ 20
Mark A. Greer5fa3af32014-03-25 08:54:29 -0700136#define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 20
Mark A. Greer38b4eb12014-09-02 15:12:25 -0700137#define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 20
138#define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 40
Mark A. Greer165063f2014-03-10 11:56:22 -0700139
Mark A. Greer4e64eff2014-09-02 15:12:24 -0700140/* Guard times for various RF technologies (in us) */
141#define TRF7970A_GUARD_TIME_NFCA 5000
142#define TRF7970A_GUARD_TIME_NFCB 5000
143#define TRF7970A_GUARD_TIME_NFCF 20000
144#define TRF7970A_GUARD_TIME_15693 1000
145
Mark A. Greer165063f2014-03-10 11:56:22 -0700146/* Quirks */
147/* Erratum: When reading IRQ Status register on trf7970a, we must issue a
148 * read continuous command for IRQ Status and Collision Position registers.
149 */
Mark A. Greer772079e2014-08-07 17:41:43 -0700150#define TRF7970A_QUIRK_IRQ_STATUS_READ BIT(0)
Mark A. Greer95064bd2014-08-07 17:41:45 -0700151#define TRF7970A_QUIRK_EN2_MUST_STAY_LOW BIT(1)
Mark A. Greer165063f2014-03-10 11:56:22 -0700152
153/* Direct commands */
154#define TRF7970A_CMD_IDLE 0x00
155#define TRF7970A_CMD_SOFT_INIT 0x03
156#define TRF7970A_CMD_RF_COLLISION 0x04
157#define TRF7970A_CMD_RF_COLLISION_RESPONSE_N 0x05
158#define TRF7970A_CMD_RF_COLLISION_RESPONSE_0 0x06
159#define TRF7970A_CMD_FIFO_RESET 0x0f
160#define TRF7970A_CMD_TRANSMIT_NO_CRC 0x10
161#define TRF7970A_CMD_TRANSMIT 0x11
162#define TRF7970A_CMD_DELAY_TRANSMIT_NO_CRC 0x12
163#define TRF7970A_CMD_DELAY_TRANSMIT 0x13
164#define TRF7970A_CMD_EOF 0x14
165#define TRF7970A_CMD_CLOSE_SLOT 0x15
166#define TRF7970A_CMD_BLOCK_RX 0x16
167#define TRF7970A_CMD_ENABLE_RX 0x17
Mark A. Greer851ee3cb2014-09-02 15:12:42 -0700168#define TRF7970A_CMD_TEST_INT_RF 0x18
169#define TRF7970A_CMD_TEST_EXT_RF 0x19
Mark A. Greer165063f2014-03-10 11:56:22 -0700170#define TRF7970A_CMD_RX_GAIN_ADJUST 0x1a
171
172/* Bits determining whether its a direct command or register R/W,
173 * whether to use a continuous SPI transaction or not, and the actual
174 * direct cmd opcode or regster address.
175 */
176#define TRF7970A_CMD_BIT_CTRL BIT(7)
177#define TRF7970A_CMD_BIT_RW BIT(6)
178#define TRF7970A_CMD_BIT_CONTINUOUS BIT(5)
179#define TRF7970A_CMD_BIT_OPCODE(opcode) ((opcode) & 0x1f)
180
181/* Registers addresses */
182#define TRF7970A_CHIP_STATUS_CTRL 0x00
183#define TRF7970A_ISO_CTRL 0x01
184#define TRF7970A_ISO14443B_TX_OPTIONS 0x02
185#define TRF7970A_ISO14443A_HIGH_BITRATE_OPTIONS 0x03
186#define TRF7970A_TX_TIMER_SETTING_H_BYTE 0x04
187#define TRF7970A_TX_TIMER_SETTING_L_BYTE 0x05
188#define TRF7970A_TX_PULSE_LENGTH_CTRL 0x06
189#define TRF7970A_RX_NO_RESPONSE_WAIT 0x07
190#define TRF7970A_RX_WAIT_TIME 0x08
191#define TRF7970A_MODULATOR_SYS_CLK_CTRL 0x09
192#define TRF7970A_RX_SPECIAL_SETTINGS 0x0a
193#define TRF7970A_REG_IO_CTRL 0x0b
194#define TRF7970A_IRQ_STATUS 0x0c
195#define TRF7970A_COLLISION_IRQ_MASK 0x0d
196#define TRF7970A_COLLISION_POSITION 0x0e
197#define TRF7970A_RSSI_OSC_STATUS 0x0f
198#define TRF7970A_SPECIAL_FCN_REG1 0x10
199#define TRF7970A_SPECIAL_FCN_REG2 0x11
200#define TRF7970A_RAM1 0x12
201#define TRF7970A_RAM2 0x13
202#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS 0x14
203#define TRF7970A_NFC_LOW_FIELD_LEVEL 0x16
204#define TRF7970A_NFCID1 0x17
205#define TRF7970A_NFC_TARGET_LEVEL 0x18
206#define TRF79070A_NFC_TARGET_PROTOCOL 0x19
207#define TRF7970A_TEST_REGISTER1 0x1a
208#define TRF7970A_TEST_REGISTER2 0x1b
209#define TRF7970A_FIFO_STATUS 0x1c
210#define TRF7970A_TX_LENGTH_BYTE1 0x1d
211#define TRF7970A_TX_LENGTH_BYTE2 0x1e
212#define TRF7970A_FIFO_IO_REGISTER 0x1f
213
214/* Chip Status Control Register Bits */
215#define TRF7970A_CHIP_STATUS_VRS5_3 BIT(0)
216#define TRF7970A_CHIP_STATUS_REC_ON BIT(1)
217#define TRF7970A_CHIP_STATUS_AGC_ON BIT(2)
218#define TRF7970A_CHIP_STATUS_PM_ON BIT(3)
219#define TRF7970A_CHIP_STATUS_RF_PWR BIT(4)
220#define TRF7970A_CHIP_STATUS_RF_ON BIT(5)
221#define TRF7970A_CHIP_STATUS_DIRECT BIT(6)
222#define TRF7970A_CHIP_STATUS_STBY BIT(7)
223
224/* ISO Control Register Bits */
225#define TRF7970A_ISO_CTRL_15693_SGL_1OF4_662 0x00
226#define TRF7970A_ISO_CTRL_15693_SGL_1OF256_662 0x01
227#define TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648 0x02
228#define TRF7970A_ISO_CTRL_15693_SGL_1OF256_2648 0x03
229#define TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a 0x04
230#define TRF7970A_ISO_CTRL_15693_DBL_1OF256_667 0x05
231#define TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669 0x06
232#define TRF7970A_ISO_CTRL_15693_DBL_1OF256_2669 0x07
233#define TRF7970A_ISO_CTRL_14443A_106 0x08
234#define TRF7970A_ISO_CTRL_14443A_212 0x09
235#define TRF7970A_ISO_CTRL_14443A_424 0x0a
236#define TRF7970A_ISO_CTRL_14443A_848 0x0b
237#define TRF7970A_ISO_CTRL_14443B_106 0x0c
238#define TRF7970A_ISO_CTRL_14443B_212 0x0d
239#define TRF7970A_ISO_CTRL_14443B_424 0x0e
240#define TRF7970A_ISO_CTRL_14443B_848 0x0f
241#define TRF7970A_ISO_CTRL_FELICA_212 0x1a
242#define TRF7970A_ISO_CTRL_FELICA_424 0x1b
Mark A. Greer13b42722014-09-02 15:12:45 -0700243#define TRF7970A_ISO_CTRL_NFC_NFCA_106 0x01
244#define TRF7970A_ISO_CTRL_NFC_NFCF_212 0x02
245#define TRF7970A_ISO_CTRL_NFC_NFCF_424 0x03
246#define TRF7970A_ISO_CTRL_NFC_CE_14443A 0x00
247#define TRF7970A_ISO_CTRL_NFC_CE_14443B 0x01
248#define TRF7970A_ISO_CTRL_NFC_CE BIT(2)
249#define TRF7970A_ISO_CTRL_NFC_ACTIVE BIT(3)
250#define TRF7970A_ISO_CTRL_NFC_INITIATOR BIT(4)
251#define TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE BIT(5)
Mark A. Greer165063f2014-03-10 11:56:22 -0700252#define TRF7970A_ISO_CTRL_RFID BIT(5)
253#define TRF7970A_ISO_CTRL_DIR_MODE BIT(6)
254#define TRF7970A_ISO_CTRL_RX_CRC_N BIT(7) /* true == No CRC */
255
256#define TRF7970A_ISO_CTRL_RFID_SPEED_MASK 0x1f
257
258/* Modulator and SYS_CLK Control Register Bits */
259#define TRF7970A_MODULATOR_DEPTH(n) ((n) & 0x7)
260#define TRF7970A_MODULATOR_DEPTH_ASK10 (TRF7970A_MODULATOR_DEPTH(0))
261#define TRF7970A_MODULATOR_DEPTH_OOK (TRF7970A_MODULATOR_DEPTH(1))
262#define TRF7970A_MODULATOR_DEPTH_ASK7 (TRF7970A_MODULATOR_DEPTH(2))
263#define TRF7970A_MODULATOR_DEPTH_ASK8_5 (TRF7970A_MODULATOR_DEPTH(3))
264#define TRF7970A_MODULATOR_DEPTH_ASK13 (TRF7970A_MODULATOR_DEPTH(4))
265#define TRF7970A_MODULATOR_DEPTH_ASK16 (TRF7970A_MODULATOR_DEPTH(5))
266#define TRF7970A_MODULATOR_DEPTH_ASK22 (TRF7970A_MODULATOR_DEPTH(6))
267#define TRF7970A_MODULATOR_DEPTH_ASK30 (TRF7970A_MODULATOR_DEPTH(7))
268#define TRF7970A_MODULATOR_EN_ANA BIT(3)
269#define TRF7970A_MODULATOR_CLK(n) (((n) & 0x3) << 4)
270#define TRF7970A_MODULATOR_CLK_DISABLED (TRF7970A_MODULATOR_CLK(0))
271#define TRF7970A_MODULATOR_CLK_3_6 (TRF7970A_MODULATOR_CLK(1))
272#define TRF7970A_MODULATOR_CLK_6_13 (TRF7970A_MODULATOR_CLK(2))
273#define TRF7970A_MODULATOR_CLK_13_27 (TRF7970A_MODULATOR_CLK(3))
274#define TRF7970A_MODULATOR_EN_OOK BIT(6)
275#define TRF7970A_MODULATOR_27MHZ BIT(7)
276
Mark A. Greer13b42722014-09-02 15:12:45 -0700277#define TRF7970A_RX_SPECIAL_SETTINGS_NO_LIM BIT(0)
278#define TRF7970A_RX_SPECIAL_SETTINGS_AGCR BIT(1)
279#define TRF7970A_RX_SPECIAL_SETTINGS_GD_0DB (0x0 << 2)
280#define TRF7970A_RX_SPECIAL_SETTINGS_GD_5DB (0x1 << 2)
281#define TRF7970A_RX_SPECIAL_SETTINGS_GD_10DB (0x2 << 2)
282#define TRF7970A_RX_SPECIAL_SETTINGS_GD_15DB (0x3 << 2)
283#define TRF7970A_RX_SPECIAL_SETTINGS_HBT BIT(4)
284#define TRF7970A_RX_SPECIAL_SETTINGS_M848 BIT(5)
285#define TRF7970A_RX_SPECIAL_SETTINGS_C424 BIT(6)
286#define TRF7970A_RX_SPECIAL_SETTINGS_C212 BIT(7)
287
288#define TRF7970A_REG_IO_CTRL_VRS(v) ((v) & 0x07)
289#define TRF7970A_REG_IO_CTRL_IO_LOW BIT(5)
290#define TRF7970A_REG_IO_CTRL_EN_EXT_PA BIT(6)
291#define TRF7970A_REG_IO_CTRL_AUTO_REG BIT(7)
292
Mark A. Greer165063f2014-03-10 11:56:22 -0700293/* IRQ Status Register Bits */
294#define TRF7970A_IRQ_STATUS_NORESP BIT(0) /* ISO15693 only */
Mark A. Greer13b42722014-09-02 15:12:45 -0700295#define TRF7970A_IRQ_STATUS_NFC_COL_ERROR BIT(0)
Mark A. Greer165063f2014-03-10 11:56:22 -0700296#define TRF7970A_IRQ_STATUS_COL BIT(1)
297#define TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR BIT(2)
Mark A. Greer13b42722014-09-02 15:12:45 -0700298#define TRF7970A_IRQ_STATUS_NFC_RF BIT(2)
Mark A. Greer165063f2014-03-10 11:56:22 -0700299#define TRF7970A_IRQ_STATUS_PARITY_ERROR BIT(3)
Mark A. Greer13b42722014-09-02 15:12:45 -0700300#define TRF7970A_IRQ_STATUS_NFC_SDD BIT(3)
Mark A. Greer165063f2014-03-10 11:56:22 -0700301#define TRF7970A_IRQ_STATUS_CRC_ERROR BIT(4)
Mark A. Greer13b42722014-09-02 15:12:45 -0700302#define TRF7970A_IRQ_STATUS_NFC_PROTO_ERROR BIT(4)
Mark A. Greer165063f2014-03-10 11:56:22 -0700303#define TRF7970A_IRQ_STATUS_FIFO BIT(5)
304#define TRF7970A_IRQ_STATUS_SRX BIT(6)
305#define TRF7970A_IRQ_STATUS_TX BIT(7)
306
307#define TRF7970A_IRQ_STATUS_ERROR \
308 (TRF7970A_IRQ_STATUS_COL | \
309 TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR | \
310 TRF7970A_IRQ_STATUS_PARITY_ERROR | \
311 TRF7970A_IRQ_STATUS_CRC_ERROR)
312
Mark A. Greer851ee3cb2014-09-02 15:12:42 -0700313#define TRF7970A_RSSI_OSC_STATUS_RSSI_MASK (BIT(2) | BIT(1) | BIT(0))
314#define TRF7970A_RSSI_OSC_STATUS_RSSI_X_MASK (BIT(5) | BIT(4) | BIT(3))
315#define TRF7970A_RSSI_OSC_STATUS_RSSI_OSC_OK BIT(6)
316
Mark A. Greer165063f2014-03-10 11:56:22 -0700317#define TRF7970A_SPECIAL_FCN_REG1_COL_7_6 BIT(0)
318#define TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL BIT(1)
319#define TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX BIT(2)
320#define TRF7970A_SPECIAL_FCN_REG1_SP_DIR_MODE BIT(3)
321#define TRF7970A_SPECIAL_FCN_REG1_NEXT_SLOT_37US BIT(4)
322#define TRF7970A_SPECIAL_FCN_REG1_PAR43 BIT(5)
323
324#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_124 (0x0 << 2)
325#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_120 (0x1 << 2)
326#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_112 (0x2 << 2)
327#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 (0x3 << 2)
328#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_4 0x0
329#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_8 0x1
330#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_16 0x2
331#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32 0x3
332
Mark A. Greer13b42722014-09-02 15:12:45 -0700333#define TRF7970A_NFC_LOW_FIELD_LEVEL_RFDET(v) ((v) & 0x07)
334#define TRF7970A_NFC_LOW_FIELD_LEVEL_CLEX_DIS BIT(7)
335
336#define TRF7970A_NFC_TARGET_LEVEL_RFDET(v) ((v) & 0x07)
337#define TRF7970A_NFC_TARGET_LEVEL_HI_RF BIT(3)
338#define TRF7970A_NFC_TARGET_LEVEL_SDD_EN BIT(3)
339#define TRF7970A_NFC_TARGET_LEVEL_LD_S_4BYTES (0x0 << 6)
340#define TRF7970A_NFC_TARGET_LEVEL_LD_S_7BYTES (0x1 << 6)
341#define TRF7970A_NFC_TARGET_LEVEL_LD_S_10BYTES (0x2 << 6)
342
Mark A. Greercb174ab2014-09-02 15:12:46 -0700343#define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106 BIT(0)
344#define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_212 BIT(1)
345#define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_424 (BIT(0) | BIT(1))
346#define TRF79070A_NFC_TARGET_PROTOCOL_PAS_14443B BIT(2)
347#define TRF79070A_NFC_TARGET_PROTOCOL_PAS_106 BIT(3)
348#define TRF79070A_NFC_TARGET_PROTOCOL_FELICA BIT(4)
349#define TRF79070A_NFC_TARGET_PROTOCOL_RF_L BIT(6)
350#define TRF79070A_NFC_TARGET_PROTOCOL_RF_H BIT(7)
351
352#define TRF79070A_NFC_TARGET_PROTOCOL_106A \
353 (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \
354 TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \
355 TRF79070A_NFC_TARGET_PROTOCOL_PAS_106 | \
356 TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106)
357
358#define TRF79070A_NFC_TARGET_PROTOCOL_106B \
359 (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \
360 TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \
361 TRF79070A_NFC_TARGET_PROTOCOL_PAS_14443B | \
362 TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106)
363
364#define TRF79070A_NFC_TARGET_PROTOCOL_212F \
365 (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \
366 TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \
367 TRF79070A_NFC_TARGET_PROTOCOL_FELICA | \
368 TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_212)
369
370#define TRF79070A_NFC_TARGET_PROTOCOL_424F \
371 (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \
372 TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \
373 TRF79070A_NFC_TARGET_PROTOCOL_FELICA | \
374 TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_424)
375
Mark A. Greer165063f2014-03-10 11:56:22 -0700376#define TRF7970A_FIFO_STATUS_OVERFLOW BIT(7)
377
378/* NFC (ISO/IEC 14443A) Type 2 Tag commands */
379#define NFC_T2T_CMD_READ 0x30
380
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700381/* ISO 15693 commands codes */
382#define ISO15693_CMD_INVENTORY 0x01
383#define ISO15693_CMD_READ_SINGLE_BLOCK 0x20
384#define ISO15693_CMD_WRITE_SINGLE_BLOCK 0x21
385#define ISO15693_CMD_LOCK_BLOCK 0x22
386#define ISO15693_CMD_READ_MULTIPLE_BLOCK 0x23
387#define ISO15693_CMD_WRITE_MULTIPLE_BLOCK 0x24
388#define ISO15693_CMD_SELECT 0x25
389#define ISO15693_CMD_RESET_TO_READY 0x26
390#define ISO15693_CMD_WRITE_AFI 0x27
391#define ISO15693_CMD_LOCK_AFI 0x28
392#define ISO15693_CMD_WRITE_DSFID 0x29
393#define ISO15693_CMD_LOCK_DSFID 0x2a
394#define ISO15693_CMD_GET_SYSTEM_INFO 0x2b
395#define ISO15693_CMD_GET_MULTIPLE_BLOCK_SECURITY_STATUS 0x2c
396
397/* ISO 15693 request and response flags */
398#define ISO15693_REQ_FLAG_SUB_CARRIER BIT(0)
399#define ISO15693_REQ_FLAG_DATA_RATE BIT(1)
400#define ISO15693_REQ_FLAG_INVENTORY BIT(2)
401#define ISO15693_REQ_FLAG_PROTOCOL_EXT BIT(3)
402#define ISO15693_REQ_FLAG_SELECT BIT(4)
403#define ISO15693_REQ_FLAG_AFI BIT(4)
404#define ISO15693_REQ_FLAG_ADDRESS BIT(5)
405#define ISO15693_REQ_FLAG_NB_SLOTS BIT(5)
406#define ISO15693_REQ_FLAG_OPTION BIT(6)
407
408#define ISO15693_REQ_FLAG_SPEED_MASK \
409 (ISO15693_REQ_FLAG_SUB_CARRIER | ISO15693_REQ_FLAG_DATA_RATE)
410
Mark A. Greer165063f2014-03-10 11:56:22 -0700411enum trf7970a_state {
Mark A. Greerceccd6a2014-09-02 15:12:35 -0700412 TRF7970A_ST_PWR_OFF,
Mark A. Greerb5e17d92014-09-02 15:12:34 -0700413 TRF7970A_ST_RF_OFF,
Mark A. Greer165063f2014-03-10 11:56:22 -0700414 TRF7970A_ST_IDLE,
415 TRF7970A_ST_IDLE_RX_BLOCKED,
416 TRF7970A_ST_WAIT_FOR_TX_FIFO,
417 TRF7970A_ST_WAIT_FOR_RX_DATA,
418 TRF7970A_ST_WAIT_FOR_RX_DATA_CONT,
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700419 TRF7970A_ST_WAIT_TO_ISSUE_EOF,
Mark A. Greer13b42722014-09-02 15:12:45 -0700420 TRF7970A_ST_LISTENING,
Mark A. Greercb174ab2014-09-02 15:12:46 -0700421 TRF7970A_ST_LISTENING_MD,
Mark A. Greer165063f2014-03-10 11:56:22 -0700422 TRF7970A_ST_MAX
423};
424
425struct trf7970a {
426 enum trf7970a_state state;
427 struct device *dev;
428 struct spi_device *spi;
429 struct regulator *regulator;
430 struct nfc_digital_dev *ddev;
431 u32 quirks;
Mark A. Greer13b42722014-09-02 15:12:45 -0700432 bool is_initiator;
Mark A. Greer165063f2014-03-10 11:56:22 -0700433 bool aborting;
434 struct sk_buff *tx_skb;
435 struct sk_buff *rx_skb;
436 nfc_digital_cmd_complete_t cb;
437 void *cb_arg;
Mark A. Greerebcc5a02014-03-25 08:54:36 -0700438 u8 chip_status_ctrl;
Mark A. Greer165063f2014-03-10 11:56:22 -0700439 u8 iso_ctrl;
Mark A. Greer49d19cc2014-03-25 08:54:33 -0700440 u8 iso_ctrl_tech;
Mark A. Greer12e9ade2014-03-25 08:54:35 -0700441 u8 modulator_sys_clk_ctrl;
Mark A. Greer165063f2014-03-10 11:56:22 -0700442 u8 special_fcn_reg1;
Mark A. Greer4e64eff2014-09-02 15:12:24 -0700443 unsigned int guard_time;
Mark A. Greer165063f2014-03-10 11:56:22 -0700444 int technology;
445 int framing;
Mark A. Greercb174ab2014-09-02 15:12:46 -0700446 u8 md_rf_tech;
Mark A. Greer165063f2014-03-10 11:56:22 -0700447 u8 tx_cmd;
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700448 bool issue_eof;
Mark A. Greer165063f2014-03-10 11:56:22 -0700449 int en2_gpio;
450 int en_gpio;
451 struct mutex lock;
452 unsigned int timeout;
453 bool ignore_timeout;
454 struct delayed_work timeout_work;
455};
456
457
458static int trf7970a_cmd(struct trf7970a *trf, u8 opcode)
459{
460 u8 cmd = TRF7970A_CMD_BIT_CTRL | TRF7970A_CMD_BIT_OPCODE(opcode);
461 int ret;
462
463 dev_dbg(trf->dev, "cmd: 0x%x\n", cmd);
464
465 ret = spi_write(trf->spi, &cmd, 1);
466 if (ret)
467 dev_err(trf->dev, "%s - cmd: 0x%x, ret: %d\n", __func__, cmd,
468 ret);
469 return ret;
470}
471
472static int trf7970a_read(struct trf7970a *trf, u8 reg, u8 *val)
473{
474 u8 addr = TRF7970A_CMD_BIT_RW | reg;
475 int ret;
476
477 ret = spi_write_then_read(trf->spi, &addr, 1, val, 1);
478 if (ret)
479 dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr,
480 ret);
481
482 dev_dbg(trf->dev, "read(0x%x): 0x%x\n", addr, *val);
483
484 return ret;
485}
486
Mark A. Greer3e7f3352014-09-02 15:12:18 -0700487static int trf7970a_read_cont(struct trf7970a *trf, u8 reg, u8 *buf, size_t len)
Mark A. Greer165063f2014-03-10 11:56:22 -0700488{
489 u8 addr = reg | TRF7970A_CMD_BIT_RW | TRF7970A_CMD_BIT_CONTINUOUS;
Mark A. Greer3e7f3352014-09-02 15:12:18 -0700490 struct spi_transfer t[2];
491 struct spi_message m;
Mark A. Greer165063f2014-03-10 11:56:22 -0700492 int ret;
493
494 dev_dbg(trf->dev, "read_cont(0x%x, %zd)\n", addr, len);
495
Mark A. Greer3e7f3352014-09-02 15:12:18 -0700496 spi_message_init(&m);
497
498 memset(&t, 0, sizeof(t));
499
500 t[0].tx_buf = &addr;
501 t[0].len = sizeof(addr);
502 spi_message_add_tail(&t[0], &m);
503
504 t[1].rx_buf = buf;
505 t[1].len = len;
506 spi_message_add_tail(&t[1], &m);
507
508 ret = spi_sync(trf->spi, &m);
Mark A. Greer165063f2014-03-10 11:56:22 -0700509 if (ret)
510 dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr,
511 ret);
512 return ret;
513}
514
515static int trf7970a_write(struct trf7970a *trf, u8 reg, u8 val)
516{
517 u8 buf[2] = { reg, val };
518 int ret;
519
520 dev_dbg(trf->dev, "write(0x%x): 0x%x\n", reg, val);
521
522 ret = spi_write(trf->spi, buf, 2);
523 if (ret)
524 dev_err(trf->dev, "%s - write: 0x%x 0x%x, ret: %d\n", __func__,
525 buf[0], buf[1], ret);
526
527 return ret;
528}
529
530static int trf7970a_read_irqstatus(struct trf7970a *trf, u8 *status)
531{
532 int ret;
533 u8 buf[2];
534 u8 addr;
535
536 addr = TRF7970A_IRQ_STATUS | TRF7970A_CMD_BIT_RW;
537
Mark A. Greer772079e2014-08-07 17:41:43 -0700538 if (trf->quirks & TRF7970A_QUIRK_IRQ_STATUS_READ) {
Mark A. Greer165063f2014-03-10 11:56:22 -0700539 addr |= TRF7970A_CMD_BIT_CONTINUOUS;
540 ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2);
541 } else {
542 ret = spi_write_then_read(trf->spi, &addr, 1, buf, 1);
543 }
544
545 if (ret)
546 dev_err(trf->dev, "%s - irqstatus: Status read failed: %d\n",
547 __func__, ret);
548 else
549 *status = buf[0];
550
551 return ret;
552}
553
Mark A. Greercb174ab2014-09-02 15:12:46 -0700554static int trf7970a_read_target_proto(struct trf7970a *trf, u8 *target_proto)
555{
556 int ret;
557 u8 buf[2];
558 u8 addr;
559
560 addr = TRF79070A_NFC_TARGET_PROTOCOL | TRF7970A_CMD_BIT_RW |
561 TRF7970A_CMD_BIT_CONTINUOUS;
562
563 ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2);
564 if (ret)
565 dev_err(trf->dev, "%s - target_proto: Read failed: %d\n",
566 __func__, ret);
567 else
568 *target_proto = buf[0];
569
570 return ret;
571}
572
573static int trf7970a_mode_detect(struct trf7970a *trf, u8 *rf_tech)
574{
575 int ret;
576 u8 target_proto, tech;
577
578 ret = trf7970a_read_target_proto(trf, &target_proto);
579 if (ret)
580 return ret;
581
582 switch (target_proto) {
583 case TRF79070A_NFC_TARGET_PROTOCOL_106A:
584 tech = NFC_DIGITAL_RF_TECH_106A;
585 break;
586 case TRF79070A_NFC_TARGET_PROTOCOL_106B:
587 tech = NFC_DIGITAL_RF_TECH_106B;
588 break;
589 case TRF79070A_NFC_TARGET_PROTOCOL_212F:
590 tech = NFC_DIGITAL_RF_TECH_212F;
591 break;
592 case TRF79070A_NFC_TARGET_PROTOCOL_424F:
593 tech = NFC_DIGITAL_RF_TECH_424F;
594 break;
595 default:
596 dev_dbg(trf->dev, "%s - mode_detect: target_proto: 0x%x\n",
597 __func__, target_proto);
598 return -EIO;
599 }
600
601 *rf_tech = tech;
602
603 return ret;
604}
605
Mark A. Greer165063f2014-03-10 11:56:22 -0700606static void trf7970a_send_upstream(struct trf7970a *trf)
607{
Mark A. Greer165063f2014-03-10 11:56:22 -0700608 dev_kfree_skb_any(trf->tx_skb);
609 trf->tx_skb = NULL;
610
611 if (trf->rx_skb && !IS_ERR(trf->rx_skb) && !trf->aborting)
612 print_hex_dump_debug("trf7970a rx data: ", DUMP_PREFIX_NONE,
613 16, 1, trf->rx_skb->data, trf->rx_skb->len,
614 false);
615
Mark A. Greer165063f2014-03-10 11:56:22 -0700616 trf->state = TRF7970A_ST_IDLE;
617
618 if (trf->aborting) {
619 dev_dbg(trf->dev, "Abort process complete\n");
620
621 if (!IS_ERR(trf->rx_skb)) {
622 kfree_skb(trf->rx_skb);
623 trf->rx_skb = ERR_PTR(-ECANCELED);
624 }
625
626 trf->aborting = false;
627 }
628
629 trf->cb(trf->ddev, trf->cb_arg, trf->rx_skb);
630
631 trf->rx_skb = NULL;
632}
633
634static void trf7970a_send_err_upstream(struct trf7970a *trf, int errno)
635{
636 dev_dbg(trf->dev, "Error - state: %d, errno: %d\n", trf->state, errno);
637
Mark A. Greer6fb9edc2014-09-02 15:12:43 -0700638 cancel_delayed_work(&trf->timeout_work);
639
Mark A. Greer165063f2014-03-10 11:56:22 -0700640 kfree_skb(trf->rx_skb);
641 trf->rx_skb = ERR_PTR(errno);
642
643 trf7970a_send_upstream(trf);
644}
645
646static int trf7970a_transmit(struct trf7970a *trf, struct sk_buff *skb,
Mark A. Greer7a1e5552014-09-02 15:12:28 -0700647 unsigned int len, u8 *prefix, unsigned int prefix_len)
Mark A. Greer165063f2014-03-10 11:56:22 -0700648{
Mark A. Greer7a1e5552014-09-02 15:12:28 -0700649 struct spi_transfer t[2];
650 struct spi_message m;
Mark A. Greer165063f2014-03-10 11:56:22 -0700651 unsigned int timeout;
652 int ret;
653
654 print_hex_dump_debug("trf7970a tx data: ", DUMP_PREFIX_NONE,
655 16, 1, skb->data, len, false);
656
Mark A. Greer7a1e5552014-09-02 15:12:28 -0700657 spi_message_init(&m);
658
659 memset(&t, 0, sizeof(t));
660
661 t[0].tx_buf = prefix;
662 t[0].len = prefix_len;
663 spi_message_add_tail(&t[0], &m);
664
665 t[1].tx_buf = skb->data;
666 t[1].len = len;
667 spi_message_add_tail(&t[1], &m);
668
669 ret = spi_sync(trf->spi, &m);
Mark A. Greer165063f2014-03-10 11:56:22 -0700670 if (ret) {
671 dev_err(trf->dev, "%s - Can't send tx data: %d\n", __func__,
672 ret);
673 return ret;
674 }
675
676 skb_pull(skb, len);
677
678 if (skb->len > 0) {
679 trf->state = TRF7970A_ST_WAIT_FOR_TX_FIFO;
680 timeout = TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT;
681 } else {
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700682 if (trf->issue_eof) {
683 trf->state = TRF7970A_ST_WAIT_TO_ISSUE_EOF;
684 timeout = TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF;
685 } else {
686 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA;
Mark A. Greer19618432014-09-02 15:12:44 -0700687
688 if (!trf->timeout)
689 timeout = TRF7970A_WAIT_FOR_TX_IRQ;
690 else
691 timeout = trf->timeout;
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700692 }
Mark A. Greer165063f2014-03-10 11:56:22 -0700693 }
694
695 dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", timeout,
696 trf->state);
697
698 schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout));
699
700 return 0;
701}
702
703static void trf7970a_fill_fifo(struct trf7970a *trf)
704{
705 struct sk_buff *skb = trf->tx_skb;
706 unsigned int len;
707 int ret;
708 u8 fifo_bytes;
Mark A. Greer7a1e5552014-09-02 15:12:28 -0700709 u8 prefix;
Mark A. Greer165063f2014-03-10 11:56:22 -0700710
711 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
712 if (ret) {
713 trf7970a_send_err_upstream(trf, ret);
714 return;
715 }
716
717 dev_dbg(trf->dev, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes);
718
Mark A. Greer4542e832014-09-02 15:12:19 -0700719 fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
Mark A. Greer165063f2014-03-10 11:56:22 -0700720
721 /* Calculate how much more data can be written to the fifo */
722 len = TRF7970A_FIFO_SIZE - fifo_bytes;
Mark A. Greer0e840ed2014-09-02 15:12:29 -0700723 if (!len) {
724 schedule_delayed_work(&trf->timeout_work,
725 msecs_to_jiffies(TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT));
726 return;
727 }
728
Mark A. Greer165063f2014-03-10 11:56:22 -0700729 len = min(skb->len, len);
730
Mark A. Greer7a1e5552014-09-02 15:12:28 -0700731 prefix = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_FIFO_IO_REGISTER;
732
733 ret = trf7970a_transmit(trf, skb, len, &prefix, sizeof(prefix));
Mark A. Greer165063f2014-03-10 11:56:22 -0700734 if (ret)
735 trf7970a_send_err_upstream(trf, ret);
736}
737
738static void trf7970a_drain_fifo(struct trf7970a *trf, u8 status)
739{
740 struct sk_buff *skb = trf->rx_skb;
741 int ret;
742 u8 fifo_bytes;
743
744 if (status & TRF7970A_IRQ_STATUS_ERROR) {
745 trf7970a_send_err_upstream(trf, -EIO);
746 return;
747 }
748
749 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
750 if (ret) {
751 trf7970a_send_err_upstream(trf, ret);
752 return;
753 }
754
755 dev_dbg(trf->dev, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes);
756
Mark A. Greer4542e832014-09-02 15:12:19 -0700757 fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
758
Mark A. Greer165063f2014-03-10 11:56:22 -0700759 if (!fifo_bytes)
760 goto no_rx_data;
761
Mark A. Greer165063f2014-03-10 11:56:22 -0700762 if (fifo_bytes > skb_tailroom(skb)) {
763 skb = skb_copy_expand(skb, skb_headroom(skb),
764 max_t(int, fifo_bytes,
765 TRF7970A_RX_SKB_ALLOC_SIZE),
766 GFP_KERNEL);
767 if (!skb) {
768 trf7970a_send_err_upstream(trf, -ENOMEM);
769 return;
770 }
771
772 kfree_skb(trf->rx_skb);
773 trf->rx_skb = skb;
774 }
775
776 ret = trf7970a_read_cont(trf, TRF7970A_FIFO_IO_REGISTER,
777 skb_put(skb, fifo_bytes), fifo_bytes);
778 if (ret) {
779 trf7970a_send_err_upstream(trf, ret);
780 return;
781 }
782
783 /* If received Type 2 ACK/NACK, shift right 4 bits and pass up */
784 if ((trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T) && (skb->len == 1) &&
785 (trf->special_fcn_reg1 ==
786 TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX)) {
787 skb->data[0] >>= 4;
788 status = TRF7970A_IRQ_STATUS_SRX;
789 } else {
790 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA_CONT;
Mark A. Greer5d8f7592014-09-02 15:12:31 -0700791
792 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
793 if (ret) {
794 trf7970a_send_err_upstream(trf, ret);
795 return;
796 }
797
798 fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
799
800 /* If there are bytes in the FIFO, set status to '0' so
801 * the if stmt below doesn't fire and the driver will wait
802 * for the trf7970a to generate another RX interrupt.
803 */
804 if (fifo_bytes)
805 status = 0;
Mark A. Greer165063f2014-03-10 11:56:22 -0700806 }
807
808no_rx_data:
809 if (status == TRF7970A_IRQ_STATUS_SRX) { /* Receive complete */
810 trf7970a_send_upstream(trf);
811 return;
812 }
813
814 dev_dbg(trf->dev, "Setting timeout for %d ms\n",
815 TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT);
816
817 schedule_delayed_work(&trf->timeout_work,
818 msecs_to_jiffies(TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT));
819}
820
821static irqreturn_t trf7970a_irq(int irq, void *dev_id)
822{
823 struct trf7970a *trf = dev_id;
824 int ret;
Mark A. Greer13b42722014-09-02 15:12:45 -0700825 u8 status, fifo_bytes, iso_ctrl;
Mark A. Greer165063f2014-03-10 11:56:22 -0700826
827 mutex_lock(&trf->lock);
828
Mark A. Greerb5e17d92014-09-02 15:12:34 -0700829 if (trf->state == TRF7970A_ST_RF_OFF) {
Mark A. Greer165063f2014-03-10 11:56:22 -0700830 mutex_unlock(&trf->lock);
831 return IRQ_NONE;
832 }
833
834 ret = trf7970a_read_irqstatus(trf, &status);
835 if (ret) {
836 mutex_unlock(&trf->lock);
837 return IRQ_NONE;
838 }
839
840 dev_dbg(trf->dev, "IRQ - state: %d, status: 0x%x\n", trf->state,
841 status);
842
843 if (!status) {
844 mutex_unlock(&trf->lock);
845 return IRQ_NONE;
846 }
847
848 switch (trf->state) {
849 case TRF7970A_ST_IDLE:
850 case TRF7970A_ST_IDLE_RX_BLOCKED:
Mark A. Greer13b42722014-09-02 15:12:45 -0700851 /* If initiator and getting interrupts caused by RF noise,
852 * turn off the receiver to avoid unnecessary interrupts.
853 * It will be turned back on in trf7970a_send_cmd() when
854 * the next command is issued.
Mark A. Greer165063f2014-03-10 11:56:22 -0700855 */
Mark A. Greer13b42722014-09-02 15:12:45 -0700856 if (trf->is_initiator && (status & TRF7970A_IRQ_STATUS_ERROR)) {
Mark A. Greer165063f2014-03-10 11:56:22 -0700857 trf7970a_cmd(trf, TRF7970A_CMD_BLOCK_RX);
858 trf->state = TRF7970A_ST_IDLE_RX_BLOCKED;
859 }
860
861 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
862 break;
863 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
864 if (status & TRF7970A_IRQ_STATUS_TX) {
865 trf->ignore_timeout =
866 !cancel_delayed_work(&trf->timeout_work);
867 trf7970a_fill_fifo(trf);
868 } else {
869 trf7970a_send_err_upstream(trf, -EIO);
870 }
871 break;
872 case TRF7970A_ST_WAIT_FOR_RX_DATA:
873 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
874 if (status & TRF7970A_IRQ_STATUS_SRX) {
875 trf->ignore_timeout =
876 !cancel_delayed_work(&trf->timeout_work);
877 trf7970a_drain_fifo(trf, status);
Mark A. Greerbece3c52014-09-02 15:12:30 -0700878 } else if (status & TRF7970A_IRQ_STATUS_FIFO) {
879 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS,
880 &fifo_bytes);
881
882 fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
883
884 if (ret)
885 trf7970a_send_err_upstream(trf, ret);
886 else if (!fifo_bytes)
887 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
Mark A. Greer13b42722014-09-02 15:12:45 -0700888 } else if ((status == TRF7970A_IRQ_STATUS_TX) ||
889 (!trf->is_initiator &&
890 (status == (TRF7970A_IRQ_STATUS_TX |
891 TRF7970A_IRQ_STATUS_NFC_RF)))) {
Mark A. Greer4dd836e2014-03-25 08:54:32 -0700892 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
Mark A. Greer19618432014-09-02 15:12:44 -0700893
894 if (!trf->timeout) {
895 trf->ignore_timeout = !cancel_delayed_work(
896 &trf->timeout_work);
897 trf->rx_skb = ERR_PTR(0);
898 trf7970a_send_upstream(trf);
899 break;
900 }
Mark A. Greer13b42722014-09-02 15:12:45 -0700901
902 if (trf->is_initiator)
903 break;
904
905 iso_ctrl = trf->iso_ctrl;
906
907 switch (trf->framing) {
908 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
909 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
910 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
911 trf->iso_ctrl = 0xff; /* Force ISO_CTRL write */
912 break;
913 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
914 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
915 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
916 trf->iso_ctrl = 0xff; /* Force ISO_CTRL write */
917 break;
918 case NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE:
919 ret = trf7970a_write(trf,
920 TRF7970A_SPECIAL_FCN_REG1,
921 TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL);
922 if (ret)
923 return ret;
924
925 trf->special_fcn_reg1 =
926 TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL;
927 break;
928 default:
929 break;
930 }
931
932 if (iso_ctrl != trf->iso_ctrl) {
933 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL,
934 iso_ctrl);
935 if (ret)
936 return ret;
937
938 trf->iso_ctrl = iso_ctrl;
939 }
Mark A. Greer4dd836e2014-03-25 08:54:32 -0700940 } else {
Mark A. Greer165063f2014-03-10 11:56:22 -0700941 trf7970a_send_err_upstream(trf, -EIO);
942 }
943 break;
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700944 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
945 if (status != TRF7970A_IRQ_STATUS_TX)
946 trf7970a_send_err_upstream(trf, -EIO);
947 break;
Mark A. Greer13b42722014-09-02 15:12:45 -0700948 case TRF7970A_ST_LISTENING:
949 if (status & TRF7970A_IRQ_STATUS_SRX) {
950 trf->ignore_timeout =
951 !cancel_delayed_work(&trf->timeout_work);
952 trf7970a_drain_fifo(trf, status);
953 } else if (!(status & TRF7970A_IRQ_STATUS_NFC_RF)) {
954 trf7970a_send_err_upstream(trf, -EIO);
955 }
956 break;
Mark A. Greercb174ab2014-09-02 15:12:46 -0700957 case TRF7970A_ST_LISTENING_MD:
958 if (status & TRF7970A_IRQ_STATUS_SRX) {
959 trf->ignore_timeout =
960 !cancel_delayed_work(&trf->timeout_work);
961
962 ret = trf7970a_mode_detect(trf, &trf->md_rf_tech);
963 if (ret) {
964 trf7970a_send_err_upstream(trf, ret);
965 } else {
966 trf->state = TRF7970A_ST_LISTENING;
967 trf7970a_drain_fifo(trf, status);
968 }
969 } else if (!(status & TRF7970A_IRQ_STATUS_NFC_RF)) {
970 trf7970a_send_err_upstream(trf, -EIO);
971 }
972 break;
Mark A. Greer165063f2014-03-10 11:56:22 -0700973 default:
974 dev_err(trf->dev, "%s - Driver in invalid state: %d\n",
975 __func__, trf->state);
976 }
977
978 mutex_unlock(&trf->lock);
979 return IRQ_HANDLED;
980}
981
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700982static void trf7970a_issue_eof(struct trf7970a *trf)
983{
984 int ret;
985
986 dev_dbg(trf->dev, "Issuing EOF\n");
987
988 ret = trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
989 if (ret)
990 trf7970a_send_err_upstream(trf, ret);
991
992 ret = trf7970a_cmd(trf, TRF7970A_CMD_EOF);
993 if (ret)
994 trf7970a_send_err_upstream(trf, ret);
995
996 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA;
997
998 dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n",
999 trf->timeout, trf->state);
1000
1001 schedule_delayed_work(&trf->timeout_work,
1002 msecs_to_jiffies(trf->timeout));
1003}
1004
Mark A. Greer165063f2014-03-10 11:56:22 -07001005static void trf7970a_timeout_work_handler(struct work_struct *work)
1006{
1007 struct trf7970a *trf = container_of(work, struct trf7970a,
1008 timeout_work.work);
1009
1010 dev_dbg(trf->dev, "Timeout - state: %d, ignore_timeout: %d\n",
1011 trf->state, trf->ignore_timeout);
1012
1013 mutex_lock(&trf->lock);
1014
1015 if (trf->ignore_timeout)
1016 trf->ignore_timeout = false;
1017 else if (trf->state == TRF7970A_ST_WAIT_FOR_RX_DATA_CONT)
Mark A. Greerafa5b5f2014-09-02 15:12:32 -07001018 trf7970a_drain_fifo(trf, TRF7970A_IRQ_STATUS_SRX);
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001019 else if (trf->state == TRF7970A_ST_WAIT_TO_ISSUE_EOF)
1020 trf7970a_issue_eof(trf);
Mark A. Greer165063f2014-03-10 11:56:22 -07001021 else
1022 trf7970a_send_err_upstream(trf, -ETIMEDOUT);
1023
1024 mutex_unlock(&trf->lock);
1025}
1026
1027static int trf7970a_init(struct trf7970a *trf)
1028{
1029 int ret;
1030
1031 dev_dbg(trf->dev, "Initializing device - state: %d\n", trf->state);
1032
1033 ret = trf7970a_cmd(trf, TRF7970A_CMD_SOFT_INIT);
1034 if (ret)
1035 goto err_out;
1036
1037 ret = trf7970a_cmd(trf, TRF7970A_CMD_IDLE);
1038 if (ret)
1039 goto err_out;
1040
Mark A. Greer4e007f82014-09-02 15:12:38 -07001041 usleep_range(1000, 2000);
1042
Mark A. Greer7149d6b2014-09-02 15:12:41 -07001043 trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
1044
Mark A. Greer6c08df42014-09-02 15:12:22 -07001045 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL, 0);
1046 if (ret)
1047 goto err_out;
1048
1049 trf->modulator_sys_clk_ctrl = 0;
1050
Mark A. Greer165063f2014-03-10 11:56:22 -07001051 ret = trf7970a_write(trf, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS,
1052 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 |
1053 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32);
1054 if (ret)
1055 goto err_out;
1056
1057 ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, 0);
1058 if (ret)
1059 goto err_out;
1060
1061 trf->special_fcn_reg1 = 0;
1062
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001063 trf->iso_ctrl = 0xff;
Mark A. Greer165063f2014-03-10 11:56:22 -07001064 return 0;
1065
1066err_out:
1067 dev_dbg(trf->dev, "Couldn't init device: %d\n", ret);
1068 return ret;
1069}
1070
1071static void trf7970a_switch_rf_off(struct trf7970a *trf)
1072{
Mark A. Greercfc708d2014-09-02 15:12:40 -07001073 if ((trf->state == TRF7970A_ST_PWR_OFF) ||
1074 (trf->state == TRF7970A_ST_RF_OFF))
1075 return;
1076
Mark A. Greer165063f2014-03-10 11:56:22 -07001077 dev_dbg(trf->dev, "Switching rf off\n");
1078
Mark A. Greera1d2dc52014-03-25 08:54:37 -07001079 trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
1080
1081 trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, trf->chip_status_ctrl);
1082
Mark A. Greer165063f2014-03-10 11:56:22 -07001083 trf->aborting = false;
Mark A. Greerb5e17d92014-09-02 15:12:34 -07001084 trf->state = TRF7970A_ST_RF_OFF;
Mark A. Greere6403b72014-03-25 08:54:38 -07001085
1086 pm_runtime_mark_last_busy(trf->dev);
1087 pm_runtime_put_autosuspend(trf->dev);
Mark A. Greer165063f2014-03-10 11:56:22 -07001088}
1089
Mark A. Greer0a1de842014-09-02 15:12:33 -07001090static int trf7970a_switch_rf_on(struct trf7970a *trf)
Mark A. Greer165063f2014-03-10 11:56:22 -07001091{
Mark A. Greera08e5452014-09-02 15:12:23 -07001092 int ret;
1093
Mark A. Greer165063f2014-03-10 11:56:22 -07001094 dev_dbg(trf->dev, "Switching rf on\n");
1095
Mark A. Greere6403b72014-03-25 08:54:38 -07001096 pm_runtime_get_sync(trf->dev);
Mark A. Greer165063f2014-03-10 11:56:22 -07001097
Mark A. Greerceccd6a2014-09-02 15:12:35 -07001098 if (trf->state != TRF7970A_ST_RF_OFF) { /* Power on, RF off */
1099 dev_err(trf->dev, "%s - Incorrect state: %d\n", __func__,
1100 trf->state);
1101 return -EINVAL;
1102 }
1103
Mark A. Greera08e5452014-09-02 15:12:23 -07001104 ret = trf7970a_init(trf);
1105 if (ret) {
1106 dev_err(trf->dev, "%s - Can't initialize: %d\n", __func__, ret);
Mark A. Greer0a1de842014-09-02 15:12:33 -07001107 return ret;
Mark A. Greera08e5452014-09-02 15:12:23 -07001108 }
1109
Mark A. Greere6403b72014-03-25 08:54:38 -07001110 trf->state = TRF7970A_ST_IDLE;
Mark A. Greer0a1de842014-09-02 15:12:33 -07001111
1112 return 0;
Mark A. Greer165063f2014-03-10 11:56:22 -07001113}
1114
1115static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
1116{
1117 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
Mark A. Greer0a1de842014-09-02 15:12:33 -07001118 int ret = 0;
Mark A. Greer165063f2014-03-10 11:56:22 -07001119
1120 dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on);
1121
1122 mutex_lock(&trf->lock);
1123
1124 if (on) {
1125 switch (trf->state) {
Mark A. Greerceccd6a2014-09-02 15:12:35 -07001126 case TRF7970A_ST_PWR_OFF:
Mark A. Greerb5e17d92014-09-02 15:12:34 -07001127 case TRF7970A_ST_RF_OFF:
Mark A. Greer0a1de842014-09-02 15:12:33 -07001128 ret = trf7970a_switch_rf_on(trf);
Mark A. Greer165063f2014-03-10 11:56:22 -07001129 break;
1130 case TRF7970A_ST_IDLE:
1131 case TRF7970A_ST_IDLE_RX_BLOCKED:
1132 break;
1133 default:
1134 dev_err(trf->dev, "%s - Invalid request: %d %d\n",
1135 __func__, trf->state, on);
1136 trf7970a_switch_rf_off(trf);
Mark A. Greer0a1de842014-09-02 15:12:33 -07001137 ret = -EINVAL;
Mark A. Greer165063f2014-03-10 11:56:22 -07001138 }
1139 } else {
1140 switch (trf->state) {
Mark A. Greerceccd6a2014-09-02 15:12:35 -07001141 case TRF7970A_ST_PWR_OFF:
Mark A. Greerb5e17d92014-09-02 15:12:34 -07001142 case TRF7970A_ST_RF_OFF:
Mark A. Greer165063f2014-03-10 11:56:22 -07001143 break;
1144 default:
1145 dev_err(trf->dev, "%s - Invalid request: %d %d\n",
1146 __func__, trf->state, on);
Mark A. Greer0a1de842014-09-02 15:12:33 -07001147 ret = -EINVAL;
Mark A. Greer165063f2014-03-10 11:56:22 -07001148 /* FALLTHROUGH */
1149 case TRF7970A_ST_IDLE:
1150 case TRF7970A_ST_IDLE_RX_BLOCKED:
Mark A. Greer13b42722014-09-02 15:12:45 -07001151 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1152 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
Mark A. Greer165063f2014-03-10 11:56:22 -07001153 trf7970a_switch_rf_off(trf);
1154 }
1155 }
1156
1157 mutex_unlock(&trf->lock);
Mark A. Greer0a1de842014-09-02 15:12:33 -07001158 return ret;
Mark A. Greer165063f2014-03-10 11:56:22 -07001159}
1160
Mark A. Greer307e5ca2014-09-02 15:12:39 -07001161static int trf7970a_in_config_rf_tech(struct trf7970a *trf, int tech)
Mark A. Greer165063f2014-03-10 11:56:22 -07001162{
1163 int ret = 0;
1164
1165 dev_dbg(trf->dev, "rf technology: %d\n", tech);
1166
1167 switch (tech) {
1168 case NFC_DIGITAL_RF_TECH_106A:
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001169 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443A_106;
Mark A. Greer12e9ade2014-03-25 08:54:35 -07001170 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
Mark A. Greer4e64eff2014-09-02 15:12:24 -07001171 trf->guard_time = TRF7970A_GUARD_TIME_NFCA;
Mark A. Greer165063f2014-03-10 11:56:22 -07001172 break;
Mark A. Greer742b1f92014-03-31 17:52:33 -07001173 case NFC_DIGITAL_RF_TECH_106B:
1174 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443B_106;
1175 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
Mark A. Greer4e64eff2014-09-02 15:12:24 -07001176 trf->guard_time = TRF7970A_GUARD_TIME_NFCB;
Mark A. Greer742b1f92014-03-31 17:52:33 -07001177 break;
Mark A. Greer6857bb92014-04-14 10:04:10 -07001178 case NFC_DIGITAL_RF_TECH_212F:
1179 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_212;
1180 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
Mark A. Greer4e64eff2014-09-02 15:12:24 -07001181 trf->guard_time = TRF7970A_GUARD_TIME_NFCF;
Mark A. Greer6857bb92014-04-14 10:04:10 -07001182 break;
1183 case NFC_DIGITAL_RF_TECH_424F:
1184 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_424;
1185 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
Mark A. Greer4e64eff2014-09-02 15:12:24 -07001186 trf->guard_time = TRF7970A_GUARD_TIME_NFCF;
Mark A. Greer6857bb92014-04-14 10:04:10 -07001187 break;
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001188 case NFC_DIGITAL_RF_TECH_ISO15693:
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001189 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
Mark A. Greer12e9ade2014-03-25 08:54:35 -07001190 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
Mark A. Greer4e64eff2014-09-02 15:12:24 -07001191 trf->guard_time = TRF7970A_GUARD_TIME_15693;
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001192 break;
Mark A. Greer165063f2014-03-10 11:56:22 -07001193 default:
1194 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
1195 return -EINVAL;
1196 }
1197
1198 trf->technology = tech;
1199
Mark A. Greer13b42722014-09-02 15:12:45 -07001200 /* If in initiator mode and not changing the RF tech due to a
1201 * PSL sequence (indicated by 'trf->iso_ctrl == 0xff' from
1202 * trf7970a_init()), clear the NFC Target Detection Level register
1203 * due to erratum.
1204 */
1205 if (trf->iso_ctrl == 0xff)
1206 ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
1207
Mark A. Greer165063f2014-03-10 11:56:22 -07001208 return ret;
1209}
1210
Mark A. Greer851ee3cb2014-09-02 15:12:42 -07001211static int trf7970a_is_rf_field(struct trf7970a *trf, bool *is_rf_field)
1212{
1213 int ret;
1214 u8 rssi;
1215
1216 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1217 trf->chip_status_ctrl | TRF7970A_CHIP_STATUS_REC_ON);
1218 if (ret)
1219 return ret;
1220
1221 ret = trf7970a_cmd(trf, TRF7970A_CMD_TEST_EXT_RF);
1222 if (ret)
1223 return ret;
1224
1225 usleep_range(50, 60);
1226
1227 ret = trf7970a_read(trf, TRF7970A_RSSI_OSC_STATUS, &rssi);
1228 if (ret)
1229 return ret;
1230
1231 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1232 trf->chip_status_ctrl);
1233 if (ret)
1234 return ret;
1235
1236 if (rssi & TRF7970A_RSSI_OSC_STATUS_RSSI_MASK)
1237 *is_rf_field = true;
1238 else
1239 *is_rf_field = false;
1240
1241 return 0;
1242}
1243
Mark A. Greer307e5ca2014-09-02 15:12:39 -07001244static int trf7970a_in_config_framing(struct trf7970a *trf, int framing)
Mark A. Greer165063f2014-03-10 11:56:22 -07001245{
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001246 u8 iso_ctrl = trf->iso_ctrl_tech;
Mark A. Greer851ee3cb2014-09-02 15:12:42 -07001247 bool is_rf_field = false;
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001248 int ret;
1249
Mark A. Greer165063f2014-03-10 11:56:22 -07001250 dev_dbg(trf->dev, "framing: %d\n", framing);
1251
1252 switch (framing) {
1253 case NFC_DIGITAL_FRAMING_NFCA_SHORT:
1254 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
1255 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001256 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
Mark A. Greer165063f2014-03-10 11:56:22 -07001257 break;
1258 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
Mark A. Greer80062892014-03-10 11:56:23 -07001259 case NFC_DIGITAL_FRAMING_NFCA_T4T:
Mark A. Greer742b1f92014-03-31 17:52:33 -07001260 case NFC_DIGITAL_FRAMING_NFCB:
1261 case NFC_DIGITAL_FRAMING_NFCB_T4T:
Mark A. Greer6857bb92014-04-14 10:04:10 -07001262 case NFC_DIGITAL_FRAMING_NFCF:
1263 case NFC_DIGITAL_FRAMING_NFCF_T3T:
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001264 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY:
1265 case NFC_DIGITAL_FRAMING_ISO15693_T5T:
Mark A. Greer13b42722014-09-02 15:12:45 -07001266 case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
1267 case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP:
Mark A. Greer165063f2014-03-10 11:56:22 -07001268 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001269 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
Mark A. Greer165063f2014-03-10 11:56:22 -07001270 break;
1271 case NFC_DIGITAL_FRAMING_NFCA_T2T:
1272 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001273 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
Mark A. Greer165063f2014-03-10 11:56:22 -07001274 break;
1275 default:
1276 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
1277 return -EINVAL;
1278 }
1279
1280 trf->framing = framing;
1281
Mark A. Greer851ee3cb2014-09-02 15:12:42 -07001282 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
1283 ret = trf7970a_is_rf_field(trf, &is_rf_field);
1284 if (ret)
1285 return ret;
1286
1287 if (is_rf_field)
1288 return -EBUSY;
1289 }
1290
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001291 if (iso_ctrl != trf->iso_ctrl) {
1292 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1293 if (ret)
1294 return ret;
1295
1296 trf->iso_ctrl = iso_ctrl;
Mark A. Greera0822a72014-03-25 08:54:34 -07001297
1298 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
Mark A. Greer12e9ade2014-03-25 08:54:35 -07001299 trf->modulator_sys_clk_ctrl);
Mark A. Greera0822a72014-03-25 08:54:34 -07001300 if (ret)
1301 return ret;
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001302 }
1303
Mark A. Greera1d2dc52014-03-25 08:54:37 -07001304 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
1305 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1306 trf->chip_status_ctrl |
1307 TRF7970A_CHIP_STATUS_RF_ON);
1308 if (ret)
1309 return ret;
1310
1311 trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
1312
Mark A. Greer4e64eff2014-09-02 15:12:24 -07001313 usleep_range(trf->guard_time, trf->guard_time + 1000);
Mark A. Greera1d2dc52014-03-25 08:54:37 -07001314 }
1315
Mark A. Greer49d19cc2014-03-25 08:54:33 -07001316 return 0;
Mark A. Greer165063f2014-03-10 11:56:22 -07001317}
1318
1319static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type,
1320 int param)
1321{
1322 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
Mark A. Greer18422e62014-03-25 08:54:39 -07001323 int ret;
Mark A. Greer165063f2014-03-10 11:56:22 -07001324
1325 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
1326
1327 mutex_lock(&trf->lock);
1328
Mark A. Greer13b42722014-09-02 15:12:45 -07001329 trf->is_initiator = true;
1330
Mark A. Greerceccd6a2014-09-02 15:12:35 -07001331 if ((trf->state == TRF7970A_ST_PWR_OFF) ||
1332 (trf->state == TRF7970A_ST_RF_OFF)) {
Mark A. Greer0a1de842014-09-02 15:12:33 -07001333 ret = trf7970a_switch_rf_on(trf);
1334 if (ret)
1335 goto err_unlock;
1336 }
Mark A. Greer165063f2014-03-10 11:56:22 -07001337
1338 switch (type) {
1339 case NFC_DIGITAL_CONFIG_RF_TECH:
Mark A. Greer307e5ca2014-09-02 15:12:39 -07001340 ret = trf7970a_in_config_rf_tech(trf, param);
Mark A. Greer165063f2014-03-10 11:56:22 -07001341 break;
1342 case NFC_DIGITAL_CONFIG_FRAMING:
Mark A. Greer307e5ca2014-09-02 15:12:39 -07001343 ret = trf7970a_in_config_framing(trf, param);
Mark A. Greer165063f2014-03-10 11:56:22 -07001344 break;
1345 default:
1346 dev_dbg(trf->dev, "Unknown type: %d\n", type);
1347 ret = -EINVAL;
1348 }
1349
Mark A. Greer0a1de842014-09-02 15:12:33 -07001350err_unlock:
Mark A. Greer165063f2014-03-10 11:56:22 -07001351 mutex_unlock(&trf->lock);
1352 return ret;
1353}
1354
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001355static int trf7970a_is_iso15693_write_or_lock(u8 cmd)
1356{
1357 switch (cmd) {
1358 case ISO15693_CMD_WRITE_SINGLE_BLOCK:
1359 case ISO15693_CMD_LOCK_BLOCK:
1360 case ISO15693_CMD_WRITE_MULTIPLE_BLOCK:
1361 case ISO15693_CMD_WRITE_AFI:
1362 case ISO15693_CMD_LOCK_AFI:
1363 case ISO15693_CMD_WRITE_DSFID:
1364 case ISO15693_CMD_LOCK_DSFID:
1365 return 1;
1366 break;
1367 default:
1368 return 0;
1369 }
1370}
1371
Mark A. Greer165063f2014-03-10 11:56:22 -07001372static int trf7970a_per_cmd_config(struct trf7970a *trf, struct sk_buff *skb)
1373{
1374 u8 *req = skb->data;
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001375 u8 special_fcn_reg1, iso_ctrl;
Mark A. Greer165063f2014-03-10 11:56:22 -07001376 int ret;
1377
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001378 trf->issue_eof = false;
1379
Mark A. Greer165063f2014-03-10 11:56:22 -07001380 /* When issuing Type 2 read command, make sure the '4_bit_RX' bit in
1381 * special functions register 1 is cleared; otherwise, its a write or
1382 * sector select command and '4_bit_RX' must be set.
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001383 *
1384 * When issuing an ISO 15693 command, inspect the flags byte to see
1385 * what speed to use. Also, remember if the OPTION flag is set on
1386 * a Type 5 write or lock command so the driver will know that it
1387 * has to send an EOF in order to get a response.
Mark A. Greer165063f2014-03-10 11:56:22 -07001388 */
1389 if ((trf->technology == NFC_DIGITAL_RF_TECH_106A) &&
1390 (trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T)) {
1391 if (req[0] == NFC_T2T_CMD_READ)
1392 special_fcn_reg1 = 0;
1393 else
1394 special_fcn_reg1 = TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX;
1395
1396 if (special_fcn_reg1 != trf->special_fcn_reg1) {
1397 ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1,
1398 special_fcn_reg1);
1399 if (ret)
1400 return ret;
1401
1402 trf->special_fcn_reg1 = special_fcn_reg1;
1403 }
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001404 } else if (trf->technology == NFC_DIGITAL_RF_TECH_ISO15693) {
1405 iso_ctrl = trf->iso_ctrl & ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK;
1406
1407 switch (req[0] & ISO15693_REQ_FLAG_SPEED_MASK) {
1408 case 0x00:
1409 iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_662;
1410 break;
1411 case ISO15693_REQ_FLAG_SUB_CARRIER:
1412 iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a;
1413 break;
1414 case ISO15693_REQ_FLAG_DATA_RATE:
1415 iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
1416 break;
1417 case (ISO15693_REQ_FLAG_SUB_CARRIER |
1418 ISO15693_REQ_FLAG_DATA_RATE):
1419 iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669;
1420 break;
1421 }
1422
1423 if (iso_ctrl != trf->iso_ctrl) {
1424 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1425 if (ret)
1426 return ret;
1427
1428 trf->iso_ctrl = iso_ctrl;
1429 }
1430
1431 if ((trf->framing == NFC_DIGITAL_FRAMING_ISO15693_T5T) &&
1432 trf7970a_is_iso15693_write_or_lock(req[1]) &&
1433 (req[0] & ISO15693_REQ_FLAG_OPTION))
1434 trf->issue_eof = true;
Mark A. Greer165063f2014-03-10 11:56:22 -07001435 }
1436
1437 return 0;
1438}
1439
Mark A. Greer13b42722014-09-02 15:12:45 -07001440static int trf7970a_send_cmd(struct nfc_digital_dev *ddev,
Mark A. Greer165063f2014-03-10 11:56:22 -07001441 struct sk_buff *skb, u16 timeout,
1442 nfc_digital_cmd_complete_t cb, void *arg)
1443{
1444 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
Mark A. Greer7a1e5552014-09-02 15:12:28 -07001445 u8 prefix[5];
Mark A. Greer165063f2014-03-10 11:56:22 -07001446 unsigned int len;
1447 int ret;
Mark A. Greeraff05642014-09-02 15:12:26 -07001448 u8 status;
Mark A. Greer165063f2014-03-10 11:56:22 -07001449
1450 dev_dbg(trf->dev, "New request - state: %d, timeout: %d ms, len: %d\n",
1451 trf->state, timeout, skb->len);
1452
1453 if (skb->len > TRF7970A_TX_MAX)
1454 return -EINVAL;
1455
1456 mutex_lock(&trf->lock);
1457
1458 if ((trf->state != TRF7970A_ST_IDLE) &&
1459 (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) {
1460 dev_err(trf->dev, "%s - Bogus state: %d\n", __func__,
1461 trf->state);
1462 ret = -EIO;
1463 goto out_err;
1464 }
1465
1466 if (trf->aborting) {
1467 dev_dbg(trf->dev, "Abort process complete\n");
1468 trf->aborting = false;
1469 ret = -ECANCELED;
1470 goto out_err;
1471 }
1472
Mark A. Greer19618432014-09-02 15:12:44 -07001473 if (timeout) {
1474 trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE,
1475 GFP_KERNEL);
1476 if (!trf->rx_skb) {
1477 dev_dbg(trf->dev, "Can't alloc rx_skb\n");
1478 ret = -ENOMEM;
1479 goto out_err;
1480 }
Mark A. Greer165063f2014-03-10 11:56:22 -07001481 }
1482
1483 if (trf->state == TRF7970A_ST_IDLE_RX_BLOCKED) {
1484 ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX);
1485 if (ret)
1486 goto out_err;
1487
1488 trf->state = TRF7970A_ST_IDLE;
1489 }
1490
Mark A. Greer13b42722014-09-02 15:12:45 -07001491 if (trf->is_initiator) {
1492 ret = trf7970a_per_cmd_config(trf, skb);
1493 if (ret)
1494 goto out_err;
1495 }
Mark A. Greer165063f2014-03-10 11:56:22 -07001496
1497 trf->ddev = ddev;
1498 trf->tx_skb = skb;
1499 trf->cb = cb;
1500 trf->cb_arg = arg;
1501 trf->timeout = timeout;
1502 trf->ignore_timeout = false;
1503
1504 len = skb->len;
Mark A. Greer165063f2014-03-10 11:56:22 -07001505
1506 /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
1507 * on what the current framing is, the address of the TX length byte 1
1508 * register (0x1d), and the 2 byte length of the data to be transmitted.
Mark A. Greer7a1e5552014-09-02 15:12:28 -07001509 * That totals 5 bytes.
Mark A. Greer165063f2014-03-10 11:56:22 -07001510 */
1511 prefix[0] = TRF7970A_CMD_BIT_CTRL |
1512 TRF7970A_CMD_BIT_OPCODE(TRF7970A_CMD_FIFO_RESET);
1513 prefix[1] = TRF7970A_CMD_BIT_CTRL |
1514 TRF7970A_CMD_BIT_OPCODE(trf->tx_cmd);
1515 prefix[2] = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_TX_LENGTH_BYTE1;
1516
1517 if (trf->framing == NFC_DIGITAL_FRAMING_NFCA_SHORT) {
1518 prefix[3] = 0x00;
1519 prefix[4] = 0x0f; /* 7 bits */
1520 } else {
1521 prefix[3] = (len & 0xf00) >> 4;
1522 prefix[3] |= ((len & 0xf0) >> 4);
1523 prefix[4] = ((len & 0x0f) << 4);
1524 }
1525
1526 len = min_t(int, skb->len, TRF7970A_FIFO_SIZE);
1527
Mark A. Greeraff05642014-09-02 15:12:26 -07001528 /* Clear possible spurious interrupt */
1529 ret = trf7970a_read_irqstatus(trf, &status);
1530 if (ret)
1531 goto out_err;
1532
Mark A. Greer7a1e5552014-09-02 15:12:28 -07001533 ret = trf7970a_transmit(trf, skb, len, prefix, sizeof(prefix));
Mark A. Greer165063f2014-03-10 11:56:22 -07001534 if (ret) {
1535 kfree_skb(trf->rx_skb);
1536 trf->rx_skb = NULL;
1537 }
1538
1539out_err:
1540 mutex_unlock(&trf->lock);
1541 return ret;
1542}
1543
Mark A. Greer13b42722014-09-02 15:12:45 -07001544static int trf7970a_tg_config_rf_tech(struct trf7970a *trf, int tech)
Mark A. Greer165063f2014-03-10 11:56:22 -07001545{
Mark A. Greer13b42722014-09-02 15:12:45 -07001546 int ret = 0;
Mark A. Greer165063f2014-03-10 11:56:22 -07001547
Mark A. Greer13b42722014-09-02 15:12:45 -07001548 dev_dbg(trf->dev, "rf technology: %d\n", tech);
Mark A. Greer165063f2014-03-10 11:56:22 -07001549
Mark A. Greer13b42722014-09-02 15:12:45 -07001550 switch (tech) {
1551 case NFC_DIGITAL_RF_TECH_106A:
1552 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
1553 TRF7970A_ISO_CTRL_NFC_CE |
1554 TRF7970A_ISO_CTRL_NFC_CE_14443A;
1555 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
1556 break;
1557 case NFC_DIGITAL_RF_TECH_212F:
1558 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
1559 TRF7970A_ISO_CTRL_NFC_NFCF_212;
1560 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
1561 break;
1562 case NFC_DIGITAL_RF_TECH_424F:
1563 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
1564 TRF7970A_ISO_CTRL_NFC_NFCF_424;
1565 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
1566 break;
1567 default:
1568 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
1569 return -EINVAL;
1570 }
1571
1572 trf->technology = tech;
1573
1574 /* Normally we write the ISO_CTRL register in
1575 * trf7970a_tg_config_framing() because the framing can change
1576 * the value written. However, when sending a PSL RES,
1577 * digital_tg_send_psl_res_complete() doesn't call
1578 * trf7970a_tg_config_framing() so we must write the register
1579 * here.
1580 */
1581 if ((trf->framing == NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED) &&
1582 (trf->iso_ctrl_tech != trf->iso_ctrl)) {
1583 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL,
1584 trf->iso_ctrl_tech);
1585
1586 trf->iso_ctrl = trf->iso_ctrl_tech;
1587 }
1588
1589 return ret;
Mark A. Greer165063f2014-03-10 11:56:22 -07001590}
1591
Mark A. Greer13b42722014-09-02 15:12:45 -07001592/* Since this is a target routine, several of the framing calls are
1593 * made between receiving the request and sending the response so they
1594 * should take effect until after the response is sent. This is accomplished
1595 * by skipping the ISO_CTRL register write here and doing it in the interrupt
1596 * handler.
1597 */
1598static int trf7970a_tg_config_framing(struct trf7970a *trf, int framing)
1599{
1600 u8 iso_ctrl = trf->iso_ctrl_tech;
1601 int ret;
1602
1603 dev_dbg(trf->dev, "framing: %d\n", framing);
1604
1605 switch (framing) {
1606 case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
1607 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
1608 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
1609 break;
1610 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
1611 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
1612 case NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE:
1613 /* These ones are applied in the interrupt handler */
1614 iso_ctrl = trf->iso_ctrl; /* Don't write to ISO_CTRL yet */
1615 break;
1616 case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP:
1617 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1618 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
1619 break;
1620 case NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED:
1621 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1622 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
1623 break;
1624 default:
1625 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
1626 return -EINVAL;
1627 }
1628
1629 trf->framing = framing;
1630
1631 if (iso_ctrl != trf->iso_ctrl) {
1632 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1633 if (ret)
1634 return ret;
1635
1636 trf->iso_ctrl = iso_ctrl;
1637
1638 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
1639 trf->modulator_sys_clk_ctrl);
1640 if (ret)
1641 return ret;
1642 }
1643
1644 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
1645 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1646 trf->chip_status_ctrl |
1647 TRF7970A_CHIP_STATUS_RF_ON);
1648 if (ret)
1649 return ret;
1650
1651 trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
1652 }
1653
1654 return 0;
1655}
1656
1657static int trf7970a_tg_configure_hw(struct nfc_digital_dev *ddev, int type,
1658 int param)
1659{
1660 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1661 int ret;
1662
1663 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
1664
1665 mutex_lock(&trf->lock);
1666
1667 trf->is_initiator = false;
1668
1669 if ((trf->state == TRF7970A_ST_PWR_OFF) ||
1670 (trf->state == TRF7970A_ST_RF_OFF)) {
1671 ret = trf7970a_switch_rf_on(trf);
1672 if (ret)
1673 goto err_unlock;
1674 }
1675
1676 switch (type) {
1677 case NFC_DIGITAL_CONFIG_RF_TECH:
1678 ret = trf7970a_tg_config_rf_tech(trf, param);
1679 break;
1680 case NFC_DIGITAL_CONFIG_FRAMING:
1681 ret = trf7970a_tg_config_framing(trf, param);
1682 break;
1683 default:
1684 dev_dbg(trf->dev, "Unknown type: %d\n", type);
1685 ret = -EINVAL;
1686 }
1687
1688err_unlock:
1689 mutex_unlock(&trf->lock);
1690 return ret;
1691}
1692
Mark A. Greercb174ab2014-09-02 15:12:46 -07001693static int _trf7970a_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
1694 nfc_digital_cmd_complete_t cb, void *arg, bool mode_detect)
Mark A. Greer165063f2014-03-10 11:56:22 -07001695{
1696 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
Mark A. Greer13b42722014-09-02 15:12:45 -07001697 int ret;
Mark A. Greer165063f2014-03-10 11:56:22 -07001698
Mark A. Greer13b42722014-09-02 15:12:45 -07001699 mutex_lock(&trf->lock);
Mark A. Greer165063f2014-03-10 11:56:22 -07001700
Mark A. Greer13b42722014-09-02 15:12:45 -07001701 if ((trf->state != TRF7970A_ST_IDLE) &&
1702 (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) {
1703 dev_err(trf->dev, "%s - Bogus state: %d\n", __func__,
1704 trf->state);
1705 ret = -EIO;
1706 goto out_err;
1707 }
Mark A. Greer165063f2014-03-10 11:56:22 -07001708
Mark A. Greer13b42722014-09-02 15:12:45 -07001709 if (trf->aborting) {
1710 dev_dbg(trf->dev, "Abort process complete\n");
1711 trf->aborting = false;
1712 ret = -ECANCELED;
1713 goto out_err;
1714 }
Mark A. Greer165063f2014-03-10 11:56:22 -07001715
Mark A. Greer13b42722014-09-02 15:12:45 -07001716 trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE,
1717 GFP_KERNEL);
1718 if (!trf->rx_skb) {
1719 dev_dbg(trf->dev, "Can't alloc rx_skb\n");
1720 ret = -ENOMEM;
1721 goto out_err;
1722 }
1723
1724 ret = trf7970a_write(trf, TRF7970A_RX_SPECIAL_SETTINGS,
1725 TRF7970A_RX_SPECIAL_SETTINGS_HBT |
1726 TRF7970A_RX_SPECIAL_SETTINGS_M848 |
1727 TRF7970A_RX_SPECIAL_SETTINGS_C424 |
1728 TRF7970A_RX_SPECIAL_SETTINGS_C212);
1729 if (ret)
1730 return ret;
1731
1732 ret = trf7970a_write(trf, TRF7970A_REG_IO_CTRL,
1733 TRF7970A_REG_IO_CTRL_VRS(0x1));
1734 if (ret)
1735 return ret;
1736
1737 ret = trf7970a_write(trf, TRF7970A_NFC_LOW_FIELD_LEVEL,
1738 TRF7970A_NFC_LOW_FIELD_LEVEL_RFDET(0x3));
1739 if (ret)
1740 return ret;
1741
1742 ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL,
1743 TRF7970A_NFC_TARGET_LEVEL_RFDET(0x7));
1744 if (ret)
1745 return ret;
1746
1747 trf->ddev = ddev;
1748 trf->cb = cb;
1749 trf->cb_arg = arg;
1750 trf->timeout = timeout;
1751 trf->ignore_timeout = false;
1752
1753 ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX);
1754 if (ret)
1755 goto out_err;
1756
Mark A. Greercb174ab2014-09-02 15:12:46 -07001757 trf->state = mode_detect ? TRF7970A_ST_LISTENING_MD :
1758 TRF7970A_ST_LISTENING;
Mark A. Greer13b42722014-09-02 15:12:45 -07001759
1760 schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout));
1761
1762out_err:
1763 mutex_unlock(&trf->lock);
1764 return ret;
Mark A. Greer165063f2014-03-10 11:56:22 -07001765}
1766
Mark A. Greercb174ab2014-09-02 15:12:46 -07001767static int trf7970a_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
1768 nfc_digital_cmd_complete_t cb, void *arg)
1769{
1770 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1771
1772 dev_dbg(trf->dev, "Listen - state: %d, timeout: %d ms\n",
1773 trf->state, timeout);
1774
1775 return _trf7970a_tg_listen(ddev, timeout, cb, arg, false);
1776}
1777
1778static int trf7970a_tg_listen_md(struct nfc_digital_dev *ddev,
1779 u16 timeout, nfc_digital_cmd_complete_t cb, void *arg)
1780{
1781 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1782 int ret;
1783
1784 dev_dbg(trf->dev, "Listen MD - state: %d, timeout: %d ms\n",
1785 trf->state, timeout);
1786
1787 ret = trf7970a_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
1788 NFC_DIGITAL_RF_TECH_106A);
1789 if (ret)
1790 return ret;
1791
1792 ret = trf7970a_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1793 NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
1794 if (ret)
1795 return ret;
1796
1797 return _trf7970a_tg_listen(ddev, timeout, cb, arg, true);
1798}
1799
1800static int trf7970a_tg_get_rf_tech(struct nfc_digital_dev *ddev, u8 *rf_tech)
1801{
1802 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1803
1804 dev_dbg(trf->dev, "Get RF Tech - state: %d, rf_tech: %d\n",
1805 trf->state, trf->md_rf_tech);
1806
1807 *rf_tech = trf->md_rf_tech;
1808
1809 return 0;
1810}
1811
Mark A. Greer165063f2014-03-10 11:56:22 -07001812static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev)
1813{
1814 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1815
1816 dev_dbg(trf->dev, "Abort process initiated\n");
1817
1818 mutex_lock(&trf->lock);
Mark A. Greer5876bc72014-03-25 08:54:30 -07001819
1820 switch (trf->state) {
1821 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
1822 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1823 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1824 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
1825 trf->aborting = true;
1826 break;
Mark A. Greer13b42722014-09-02 15:12:45 -07001827 case TRF7970A_ST_LISTENING:
1828 trf->ignore_timeout = !cancel_delayed_work(&trf->timeout_work);
1829 trf7970a_send_err_upstream(trf, -ECANCELED);
1830 dev_dbg(trf->dev, "Abort process complete\n");
1831 break;
Mark A. Greer5876bc72014-03-25 08:54:30 -07001832 default:
1833 break;
1834 }
1835
Mark A. Greer165063f2014-03-10 11:56:22 -07001836 mutex_unlock(&trf->lock);
1837}
1838
1839static struct nfc_digital_ops trf7970a_nfc_ops = {
1840 .in_configure_hw = trf7970a_in_configure_hw,
Mark A. Greer13b42722014-09-02 15:12:45 -07001841 .in_send_cmd = trf7970a_send_cmd,
Mark A. Greer165063f2014-03-10 11:56:22 -07001842 .tg_configure_hw = trf7970a_tg_configure_hw,
Mark A. Greer13b42722014-09-02 15:12:45 -07001843 .tg_send_cmd = trf7970a_send_cmd,
Mark A. Greer165063f2014-03-10 11:56:22 -07001844 .tg_listen = trf7970a_tg_listen,
Mark A. Greercb174ab2014-09-02 15:12:46 -07001845 .tg_listen_md = trf7970a_tg_listen_md,
1846 .tg_get_rf_tech = trf7970a_tg_get_rf_tech,
Mark A. Greer165063f2014-03-10 11:56:22 -07001847 .switch_rf = trf7970a_switch_rf,
1848 .abort_cmd = trf7970a_abort_cmd,
1849};
1850
Mark A. Greerceccd6a2014-09-02 15:12:35 -07001851static int trf7970a_power_up(struct trf7970a *trf)
1852{
1853 int ret;
1854
1855 dev_dbg(trf->dev, "Powering up - state: %d\n", trf->state);
1856
1857 if (trf->state != TRF7970A_ST_PWR_OFF)
1858 return 0;
1859
1860 ret = regulator_enable(trf->regulator);
1861 if (ret) {
1862 dev_err(trf->dev, "%s - Can't enable VIN: %d\n", __func__, ret);
1863 return ret;
1864 }
1865
1866 usleep_range(5000, 6000);
1867
1868 if (!(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW)) {
1869 gpio_set_value(trf->en2_gpio, 1);
1870 usleep_range(1000, 2000);
1871 }
1872
1873 gpio_set_value(trf->en_gpio, 1);
1874
1875 usleep_range(20000, 21000);
1876
1877 trf->state = TRF7970A_ST_RF_OFF;
1878
1879 return 0;
1880}
1881
1882static int trf7970a_power_down(struct trf7970a *trf)
1883{
1884 int ret;
1885
1886 dev_dbg(trf->dev, "Powering down - state: %d\n", trf->state);
1887
1888 if (trf->state == TRF7970A_ST_PWR_OFF)
1889 return 0;
1890
1891 if (trf->state != TRF7970A_ST_RF_OFF) {
1892 dev_dbg(trf->dev, "Can't power down - not RF_OFF state (%d)\n",
1893 trf->state);
1894 return -EBUSY;
1895 }
1896
1897 gpio_set_value(trf->en_gpio, 0);
1898 gpio_set_value(trf->en2_gpio, 0);
1899
1900 ret = regulator_disable(trf->regulator);
1901 if (ret)
1902 dev_err(trf->dev, "%s - Can't disable VIN: %d\n", __func__,
1903 ret);
1904
1905 trf->state = TRF7970A_ST_PWR_OFF;
1906
1907 return ret;
1908}
1909
Mark A. Greerb5282812014-09-02 15:12:36 -07001910static int trf7970a_startup(struct trf7970a *trf)
1911{
1912 int ret;
1913
1914 ret = trf7970a_power_up(trf);
1915 if (ret)
1916 return ret;
1917
1918 pm_runtime_set_active(trf->dev);
1919 pm_runtime_enable(trf->dev);
1920 pm_runtime_mark_last_busy(trf->dev);
1921
1922 return 0;
1923}
1924
1925static void trf7970a_shutdown(struct trf7970a *trf)
1926{
1927 switch (trf->state) {
1928 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
1929 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1930 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1931 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
Mark A. Greer13b42722014-09-02 15:12:45 -07001932 case TRF7970A_ST_LISTENING:
Mark A. Greerb5282812014-09-02 15:12:36 -07001933 trf7970a_send_err_upstream(trf, -ECANCELED);
1934 /* FALLTHROUGH */
1935 case TRF7970A_ST_IDLE:
1936 case TRF7970A_ST_IDLE_RX_BLOCKED:
1937 trf7970a_switch_rf_off(trf);
1938 break;
1939 default:
1940 break;
1941 }
1942
1943 pm_runtime_disable(trf->dev);
1944 pm_runtime_set_suspended(trf->dev);
1945
1946 trf7970a_power_down(trf);
1947}
1948
Mark A. Greerfd0c8282014-03-25 08:54:40 -07001949static int trf7970a_get_autosuspend_delay(struct device_node *np)
1950{
1951 int autosuspend_delay, ret;
1952
1953 ret = of_property_read_u32(np, "autosuspend-delay", &autosuspend_delay);
1954 if (ret)
1955 autosuspend_delay = TRF7970A_AUTOSUSPEND_DELAY;
1956
Mark A. Greerfd0c8282014-03-25 08:54:40 -07001957 return autosuspend_delay;
1958}
1959
Mark A. Greer3bd14232014-08-07 17:41:41 -07001960static int trf7970a_get_vin_voltage_override(struct device_node *np,
1961 u32 *vin_uvolts)
1962{
1963 return of_property_read_u32(np, "vin-voltage-override", vin_uvolts);
1964}
1965
Mark A. Greer165063f2014-03-10 11:56:22 -07001966static int trf7970a_probe(struct spi_device *spi)
1967{
1968 struct device_node *np = spi->dev.of_node;
Mark A. Greer165063f2014-03-10 11:56:22 -07001969 struct trf7970a *trf;
Mark A. Greerfd0c8282014-03-25 08:54:40 -07001970 int uvolts, autosuspend_delay, ret;
Mark A. Greer165063f2014-03-10 11:56:22 -07001971
1972 if (!np) {
1973 dev_err(&spi->dev, "No Device Tree entry\n");
1974 return -EINVAL;
1975 }
1976
1977 trf = devm_kzalloc(&spi->dev, sizeof(*trf), GFP_KERNEL);
1978 if (!trf)
1979 return -ENOMEM;
1980
Mark A. Greerceccd6a2014-09-02 15:12:35 -07001981 trf->state = TRF7970A_ST_PWR_OFF;
Mark A. Greer165063f2014-03-10 11:56:22 -07001982 trf->dev = &spi->dev;
1983 trf->spi = spi;
Mark A. Greer165063f2014-03-10 11:56:22 -07001984
1985 spi->mode = SPI_MODE_1;
1986 spi->bits_per_word = 8;
1987
Mark A. Greer24707292014-09-02 15:12:17 -07001988 ret = spi_setup(spi);
1989 if (ret < 0) {
1990 dev_err(trf->dev, "Can't set up SPI Communication\n");
1991 return ret;
1992 }
1993
Mark A. Greer772079e2014-08-07 17:41:43 -07001994 if (of_property_read_bool(np, "irq-status-read-quirk"))
1995 trf->quirks |= TRF7970A_QUIRK_IRQ_STATUS_READ;
1996
Mark A. Greer165063f2014-03-10 11:56:22 -07001997 /* There are two enable pins - both must be present */
1998 trf->en_gpio = of_get_named_gpio(np, "ti,enable-gpios", 0);
1999 if (!gpio_is_valid(trf->en_gpio)) {
2000 dev_err(trf->dev, "No EN GPIO property\n");
2001 return trf->en_gpio;
2002 }
2003
2004 ret = devm_gpio_request_one(trf->dev, trf->en_gpio,
Mark A. Greerf23b7352014-08-07 17:41:47 -07002005 GPIOF_DIR_OUT | GPIOF_INIT_LOW, "trf7970a EN");
Mark A. Greer165063f2014-03-10 11:56:22 -07002006 if (ret) {
2007 dev_err(trf->dev, "Can't request EN GPIO: %d\n", ret);
2008 return ret;
2009 }
2010
2011 trf->en2_gpio = of_get_named_gpio(np, "ti,enable-gpios", 1);
2012 if (!gpio_is_valid(trf->en2_gpio)) {
2013 dev_err(trf->dev, "No EN2 GPIO property\n");
2014 return trf->en2_gpio;
2015 }
2016
2017 ret = devm_gpio_request_one(trf->dev, trf->en2_gpio,
Mark A. Greerf23b7352014-08-07 17:41:47 -07002018 GPIOF_DIR_OUT | GPIOF_INIT_LOW, "trf7970a EN2");
Mark A. Greer165063f2014-03-10 11:56:22 -07002019 if (ret) {
2020 dev_err(trf->dev, "Can't request EN2 GPIO: %d\n", ret);
2021 return ret;
2022 }
2023
Mark A. Greer95064bd2014-08-07 17:41:45 -07002024 if (of_property_read_bool(np, "en2-rf-quirk"))
2025 trf->quirks |= TRF7970A_QUIRK_EN2_MUST_STAY_LOW;
2026
Mark A. Greer165063f2014-03-10 11:56:22 -07002027 ret = devm_request_threaded_irq(trf->dev, spi->irq, NULL,
2028 trf7970a_irq, IRQF_TRIGGER_RISING | IRQF_ONESHOT,
2029 "trf7970a", trf);
2030 if (ret) {
2031 dev_err(trf->dev, "Can't request IRQ#%d: %d\n", spi->irq, ret);
2032 return ret;
2033 }
2034
2035 mutex_init(&trf->lock);
2036 INIT_DELAYED_WORK(&trf->timeout_work, trf7970a_timeout_work_handler);
2037
2038 trf->regulator = devm_regulator_get(&spi->dev, "vin");
2039 if (IS_ERR(trf->regulator)) {
2040 ret = PTR_ERR(trf->regulator);
2041 dev_err(trf->dev, "Can't get VIN regulator: %d\n", ret);
2042 goto err_destroy_lock;
2043 }
2044
2045 ret = regulator_enable(trf->regulator);
2046 if (ret) {
2047 dev_err(trf->dev, "Can't enable VIN: %d\n", ret);
2048 goto err_destroy_lock;
2049 }
2050
Mark A. Greer3bd14232014-08-07 17:41:41 -07002051 ret = trf7970a_get_vin_voltage_override(np, &uvolts);
2052 if (ret)
2053 uvolts = regulator_get_voltage(trf->regulator);
Mark A. Greerebcc5a02014-03-25 08:54:36 -07002054
2055 if (uvolts > 4000000)
2056 trf->chip_status_ctrl = TRF7970A_CHIP_STATUS_VRS5_3;
2057
Mark A. Greer165063f2014-03-10 11:56:22 -07002058 trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops,
2059 TRF7970A_SUPPORTED_PROTOCOLS,
Mark A. Greer13b42722014-09-02 15:12:45 -07002060 NFC_DIGITAL_DRV_CAPS_IN_CRC |
2061 NFC_DIGITAL_DRV_CAPS_TG_CRC, 0, 0);
Mark A. Greer165063f2014-03-10 11:56:22 -07002062 if (!trf->ddev) {
2063 dev_err(trf->dev, "Can't allocate NFC digital device\n");
2064 ret = -ENOMEM;
2065 goto err_disable_regulator;
2066 }
2067
2068 nfc_digital_set_parent_dev(trf->ddev, trf->dev);
2069 nfc_digital_set_drvdata(trf->ddev, trf);
2070 spi_set_drvdata(spi, trf);
2071
Mark A. Greerfd0c8282014-03-25 08:54:40 -07002072 autosuspend_delay = trf7970a_get_autosuspend_delay(np);
2073
2074 pm_runtime_set_autosuspend_delay(trf->dev, autosuspend_delay);
Mark A. Greere6403b72014-03-25 08:54:38 -07002075 pm_runtime_use_autosuspend(trf->dev);
Mark A. Greerceccd6a2014-09-02 15:12:35 -07002076
Mark A. Greerb5282812014-09-02 15:12:36 -07002077 ret = trf7970a_startup(trf);
Mark A. Greerceccd6a2014-09-02 15:12:35 -07002078 if (ret)
2079 goto err_free_ddev;
2080
Mark A. Greer165063f2014-03-10 11:56:22 -07002081 ret = nfc_digital_register_device(trf->ddev);
2082 if (ret) {
2083 dev_err(trf->dev, "Can't register NFC digital device: %d\n",
2084 ret);
Mark A. Greerb5282812014-09-02 15:12:36 -07002085 goto err_shutdown;
Mark A. Greer165063f2014-03-10 11:56:22 -07002086 }
2087
2088 return 0;
2089
Mark A. Greerb5282812014-09-02 15:12:36 -07002090err_shutdown:
2091 trf7970a_shutdown(trf);
Mark A. Greerceccd6a2014-09-02 15:12:35 -07002092err_free_ddev:
Mark A. Greer165063f2014-03-10 11:56:22 -07002093 nfc_digital_free_device(trf->ddev);
2094err_disable_regulator:
2095 regulator_disable(trf->regulator);
2096err_destroy_lock:
2097 mutex_destroy(&trf->lock);
2098 return ret;
2099}
2100
2101static int trf7970a_remove(struct spi_device *spi)
2102{
2103 struct trf7970a *trf = spi_get_drvdata(spi);
2104
2105 mutex_lock(&trf->lock);
2106
Mark A. Greerb5282812014-09-02 15:12:36 -07002107 trf7970a_shutdown(trf);
Mark A. Greerceccd6a2014-09-02 15:12:35 -07002108
2109 mutex_unlock(&trf->lock);
Mark A. Greere6403b72014-03-25 08:54:38 -07002110
Mark A. Greer165063f2014-03-10 11:56:22 -07002111 nfc_digital_unregister_device(trf->ddev);
2112 nfc_digital_free_device(trf->ddev);
2113
2114 regulator_disable(trf->regulator);
2115
2116 mutex_destroy(&trf->lock);
2117
2118 return 0;
2119}
2120
Mark A. Greer77c95392014-09-02 15:12:37 -07002121#ifdef CONFIG_PM_SLEEP
2122static int trf7970a_suspend(struct device *dev)
2123{
2124 struct spi_device *spi = container_of(dev, struct spi_device, dev);
2125 struct trf7970a *trf = spi_get_drvdata(spi);
Mark A. Greer77c95392014-09-02 15:12:37 -07002126
2127 dev_dbg(dev, "Suspend\n");
2128
2129 mutex_lock(&trf->lock);
2130
2131 trf7970a_shutdown(trf);
2132
2133 mutex_unlock(&trf->lock);
2134
Mark A. Greer671970f2014-09-23 11:25:45 -07002135 return 0;
Mark A. Greer77c95392014-09-02 15:12:37 -07002136}
2137
2138static int trf7970a_resume(struct device *dev)
2139{
2140 struct spi_device *spi = container_of(dev, struct spi_device, dev);
2141 struct trf7970a *trf = spi_get_drvdata(spi);
2142 int ret = 0;
2143
2144 dev_dbg(dev, "Resume\n");
2145
2146 mutex_lock(&trf->lock);
2147
2148 ret = trf7970a_startup(trf);
2149
2150 mutex_unlock(&trf->lock);
2151
2152 return ret;
2153}
2154#endif
2155
Mark A. Greere6403b72014-03-25 08:54:38 -07002156#ifdef CONFIG_PM_RUNTIME
2157static int trf7970a_pm_runtime_suspend(struct device *dev)
2158{
2159 struct spi_device *spi = container_of(dev, struct spi_device, dev);
2160 struct trf7970a *trf = spi_get_drvdata(spi);
2161 int ret;
2162
2163 dev_dbg(dev, "Runtime suspend\n");
2164
Mark A. Greerceccd6a2014-09-02 15:12:35 -07002165 mutex_lock(&trf->lock);
Mark A. Greere6403b72014-03-25 08:54:38 -07002166
Mark A. Greerceccd6a2014-09-02 15:12:35 -07002167 ret = trf7970a_power_down(trf);
Mark A. Greere6403b72014-03-25 08:54:38 -07002168
Mark A. Greerceccd6a2014-09-02 15:12:35 -07002169 mutex_unlock(&trf->lock);
Mark A. Greere6403b72014-03-25 08:54:38 -07002170
2171 return ret;
2172}
2173
2174static int trf7970a_pm_runtime_resume(struct device *dev)
2175{
2176 struct spi_device *spi = container_of(dev, struct spi_device, dev);
2177 struct trf7970a *trf = spi_get_drvdata(spi);
2178 int ret;
2179
2180 dev_dbg(dev, "Runtime resume\n");
2181
Mark A. Greerceccd6a2014-09-02 15:12:35 -07002182 ret = trf7970a_power_up(trf);
2183 if (!ret)
2184 pm_runtime_mark_last_busy(dev);
Mark A. Greere6403b72014-03-25 08:54:38 -07002185
Mark A. Greerceccd6a2014-09-02 15:12:35 -07002186 return ret;
Mark A. Greere6403b72014-03-25 08:54:38 -07002187}
2188#endif
2189
2190static const struct dev_pm_ops trf7970a_pm_ops = {
Mark A. Greer77c95392014-09-02 15:12:37 -07002191 SET_SYSTEM_SLEEP_PM_OPS(trf7970a_suspend, trf7970a_resume)
Mark A. Greere6403b72014-03-25 08:54:38 -07002192 SET_RUNTIME_PM_OPS(trf7970a_pm_runtime_suspend,
2193 trf7970a_pm_runtime_resume, NULL)
2194};
2195
Mark A. Greer165063f2014-03-10 11:56:22 -07002196static const struct spi_device_id trf7970a_id_table[] = {
Mark A. Greer772079e2014-08-07 17:41:43 -07002197 { "trf7970a", 0 },
Mark A. Greer165063f2014-03-10 11:56:22 -07002198 { }
2199};
2200MODULE_DEVICE_TABLE(spi, trf7970a_id_table);
2201
2202static struct spi_driver trf7970a_spi_driver = {
2203 .probe = trf7970a_probe,
2204 .remove = trf7970a_remove,
2205 .id_table = trf7970a_id_table,
2206 .driver = {
2207 .name = "trf7970a",
2208 .owner = THIS_MODULE,
Mark A. Greere6403b72014-03-25 08:54:38 -07002209 .pm = &trf7970a_pm_ops,
Mark A. Greer165063f2014-03-10 11:56:22 -07002210 },
2211};
2212
2213module_spi_driver(trf7970a_spi_driver);
2214
2215MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>");
2216MODULE_LICENSE("GPL v2");
2217MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver");