2 * TI TRF7970a RFID/NFC Transceiver Driver
4 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
6 * Author: Erick Macias <emacias@ti.com>
7 * Author: Felipe Balbi <balbi@ti.com>
8 * Author: Mark A. Greer <mgreer@animalcreek.com>
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.
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/netdevice.h>
18 #include <linux/interrupt.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/nfc.h>
21 #include <linux/skbuff.h>
22 #include <linux/delay.h>
23 #include <linux/gpio.h>
25 #include <linux/of_gpio.h>
26 #include <linux/spi/spi.h>
27 #include <linux/regulator/consumer.h>
29 #include <net/nfc/nfc.h>
30 #include <net/nfc/digital.h>
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.
36 * The trf7970a is very timing sensitive and the VIN, EN2, and EN
37 * pins must asserted in that order and with specific delays in between.
38 * The delays used in the driver were provided by TI and have been
39 * confirmed to work with this driver. There is a bug with the current
40 * version of the trf7970a that requires that EN2 remain low no matter
41 * what. If it goes high, it will generate an RF field even when in
42 * passive target mode. TI has indicated that the chip will work okay
43 * when EN2 is left low. The 'en2-rf-quirk' device tree property
44 * indicates that trf7970a currently being used has the erratum and
45 * that EN2 must be kept low.
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
61 * Aborting an active command isn't as simple as it seems because the only
62 * way to abort a command that's already been sent to the tag is so turn
63 * off power to the tag. If we do that, though, we'd have to go through
64 * the entire anticollision procedure again but the digital layer doesn't
65 * support that. So, if an abort is received before trf7970a_in_send_cmd()
66 * has sent the command to the tag, it simply returns -ECANCELED. If the
67 * command has already been sent to the tag, then the driver continues
68 * normally and recieves the response data (or error) but just before
69 * sending the data upstream, it frees the rx_skb and sends -ECANCELED
70 * upstream instead. If the command failed, that error will be sent
73 * When recieving data from a tag and the interrupt status register has
74 * only the SRX bit set, it means that all of the data has been received
75 * (once what's in the fifo has been read). However, depending on timing
76 * an interrupt status with only the SRX bit set may not be recived. In
77 * those cases, the timeout mechanism is used to wait 20 ms in case more
78 * data arrives. After 20 ms, it is assumed that all of the data has been
79 * received and the accumulated rx data is sent upstream. The
80 * 'TRF7970A_ST_WAIT_FOR_RX_DATA_CONT' state is used for this purpose
81 * (i.e., it indicates that some data has been received but we're not sure
82 * if there is more coming so a timeout in this state means all data has
83 * been received and there isn't an error). The delay is 20 ms since delays
84 * of ~16 ms have been observed during testing.
86 * Type 2 write and sector select commands respond with a 4-bit ACK or NACK.
87 * Having only 4 bits in the FIFO won't normally generate an interrupt so
88 * driver enables the '4_bit_RX' bit of the Special Functions register 1
89 * to cause an interrupt in that case. Leaving that bit for a read command
90 * messes up the data returned so it is only enabled when the framing is
91 * 'NFC_DIGITAL_FRAMING_NFCA_T2T' and the command is not a read command.
92 * Unfortunately, that means that the driver has to peek into tx frames
93 * when the framing is 'NFC_DIGITAL_FRAMING_NFCA_T2T'. This is done by
94 * the trf7970a_per_cmd_config() routine.
96 * ISO/IEC 15693 frames specify whether to use single or double sub-carrier
97 * frequencies and whether to use low or high data rates in the flags byte
98 * of the frame. This means that the driver has to peek at all 15693 frames
99 * to determine what speed to set the communication to. In addition, write
100 * and lock commands use the OPTION flag to indicate that an EOF must be
101 * sent to the tag before it will send its response. So the driver has to
102 * examine all frames for that reason too.
104 * It is unclear how long to wait before sending the EOF. According to the
105 * Note under Table 1-1 in section 1.6 of
106 * http://www.ti.com/lit/ug/scbu011/scbu011.pdf, that wait should be at least
107 * 10 ms for TI Tag-it HF-I tags; however testing has shown that is not long
108 * enough. For this reason, the driver waits 20 ms which seems to work
112 #define TRF7970A_SUPPORTED_PROTOCOLS \
113 (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \
114 NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_FELICA_MASK | \
115 NFC_PROTO_ISO15693_MASK)
117 #define TRF7970A_AUTOSUSPEND_DELAY 30000 /* 30 seconds */
119 /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
120 * on what the current framing is, the address of the TX length byte 1
121 * register (0x1d), and the 2 byte length of the data to be transmitted.
122 * That totals 5 bytes.
124 #define TRF7970A_TX_SKB_HEADROOM 5
126 #define TRF7970A_RX_SKB_ALLOC_SIZE 256
128 #define TRF7970A_FIFO_SIZE 127
130 /* TX length is 3 nibbles long ==> 4KB - 1 bytes max */
131 #define TRF7970A_TX_MAX (4096 - 1)
133 #define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 20
134 #define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 3
135 #define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 20
138 /* Erratum: When reading IRQ Status register on trf7970a, we must issue a
139 * read continuous command for IRQ Status and Collision Position registers.
141 #define TRF7970A_QUIRK_IRQ_STATUS_READ BIT(0)
142 #define TRF7970A_QUIRK_EN2_MUST_STAY_LOW BIT(1)
144 /* Direct commands */
145 #define TRF7970A_CMD_IDLE 0x00
146 #define TRF7970A_CMD_SOFT_INIT 0x03
147 #define TRF7970A_CMD_RF_COLLISION 0x04
148 #define TRF7970A_CMD_RF_COLLISION_RESPONSE_N 0x05
149 #define TRF7970A_CMD_RF_COLLISION_RESPONSE_0 0x06
150 #define TRF7970A_CMD_FIFO_RESET 0x0f
151 #define TRF7970A_CMD_TRANSMIT_NO_CRC 0x10
152 #define TRF7970A_CMD_TRANSMIT 0x11
153 #define TRF7970A_CMD_DELAY_TRANSMIT_NO_CRC 0x12
154 #define TRF7970A_CMD_DELAY_TRANSMIT 0x13
155 #define TRF7970A_CMD_EOF 0x14
156 #define TRF7970A_CMD_CLOSE_SLOT 0x15
157 #define TRF7970A_CMD_BLOCK_RX 0x16
158 #define TRF7970A_CMD_ENABLE_RX 0x17
159 #define TRF7970A_CMD_TEST_EXT_RF 0x18
160 #define TRF7970A_CMD_TEST_INT_RF 0x19
161 #define TRF7970A_CMD_RX_GAIN_ADJUST 0x1a
163 /* Bits determining whether its a direct command or register R/W,
164 * whether to use a continuous SPI transaction or not, and the actual
165 * direct cmd opcode or regster address.
167 #define TRF7970A_CMD_BIT_CTRL BIT(7)
168 #define TRF7970A_CMD_BIT_RW BIT(6)
169 #define TRF7970A_CMD_BIT_CONTINUOUS BIT(5)
170 #define TRF7970A_CMD_BIT_OPCODE(opcode) ((opcode) & 0x1f)
172 /* Registers addresses */
173 #define TRF7970A_CHIP_STATUS_CTRL 0x00
174 #define TRF7970A_ISO_CTRL 0x01
175 #define TRF7970A_ISO14443B_TX_OPTIONS 0x02
176 #define TRF7970A_ISO14443A_HIGH_BITRATE_OPTIONS 0x03
177 #define TRF7970A_TX_TIMER_SETTING_H_BYTE 0x04
178 #define TRF7970A_TX_TIMER_SETTING_L_BYTE 0x05
179 #define TRF7970A_TX_PULSE_LENGTH_CTRL 0x06
180 #define TRF7970A_RX_NO_RESPONSE_WAIT 0x07
181 #define TRF7970A_RX_WAIT_TIME 0x08
182 #define TRF7970A_MODULATOR_SYS_CLK_CTRL 0x09
183 #define TRF7970A_RX_SPECIAL_SETTINGS 0x0a
184 #define TRF7970A_REG_IO_CTRL 0x0b
185 #define TRF7970A_IRQ_STATUS 0x0c
186 #define TRF7970A_COLLISION_IRQ_MASK 0x0d
187 #define TRF7970A_COLLISION_POSITION 0x0e
188 #define TRF7970A_RSSI_OSC_STATUS 0x0f
189 #define TRF7970A_SPECIAL_FCN_REG1 0x10
190 #define TRF7970A_SPECIAL_FCN_REG2 0x11
191 #define TRF7970A_RAM1 0x12
192 #define TRF7970A_RAM2 0x13
193 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS 0x14
194 #define TRF7970A_NFC_LOW_FIELD_LEVEL 0x16
195 #define TRF7970A_NFCID1 0x17
196 #define TRF7970A_NFC_TARGET_LEVEL 0x18
197 #define TRF79070A_NFC_TARGET_PROTOCOL 0x19
198 #define TRF7970A_TEST_REGISTER1 0x1a
199 #define TRF7970A_TEST_REGISTER2 0x1b
200 #define TRF7970A_FIFO_STATUS 0x1c
201 #define TRF7970A_TX_LENGTH_BYTE1 0x1d
202 #define TRF7970A_TX_LENGTH_BYTE2 0x1e
203 #define TRF7970A_FIFO_IO_REGISTER 0x1f
205 /* Chip Status Control Register Bits */
206 #define TRF7970A_CHIP_STATUS_VRS5_3 BIT(0)
207 #define TRF7970A_CHIP_STATUS_REC_ON BIT(1)
208 #define TRF7970A_CHIP_STATUS_AGC_ON BIT(2)
209 #define TRF7970A_CHIP_STATUS_PM_ON BIT(3)
210 #define TRF7970A_CHIP_STATUS_RF_PWR BIT(4)
211 #define TRF7970A_CHIP_STATUS_RF_ON BIT(5)
212 #define TRF7970A_CHIP_STATUS_DIRECT BIT(6)
213 #define TRF7970A_CHIP_STATUS_STBY BIT(7)
215 /* ISO Control Register Bits */
216 #define TRF7970A_ISO_CTRL_15693_SGL_1OF4_662 0x00
217 #define TRF7970A_ISO_CTRL_15693_SGL_1OF256_662 0x01
218 #define TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648 0x02
219 #define TRF7970A_ISO_CTRL_15693_SGL_1OF256_2648 0x03
220 #define TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a 0x04
221 #define TRF7970A_ISO_CTRL_15693_DBL_1OF256_667 0x05
222 #define TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669 0x06
223 #define TRF7970A_ISO_CTRL_15693_DBL_1OF256_2669 0x07
224 #define TRF7970A_ISO_CTRL_14443A_106 0x08
225 #define TRF7970A_ISO_CTRL_14443A_212 0x09
226 #define TRF7970A_ISO_CTRL_14443A_424 0x0a
227 #define TRF7970A_ISO_CTRL_14443A_848 0x0b
228 #define TRF7970A_ISO_CTRL_14443B_106 0x0c
229 #define TRF7970A_ISO_CTRL_14443B_212 0x0d
230 #define TRF7970A_ISO_CTRL_14443B_424 0x0e
231 #define TRF7970A_ISO_CTRL_14443B_848 0x0f
232 #define TRF7970A_ISO_CTRL_FELICA_212 0x1a
233 #define TRF7970A_ISO_CTRL_FELICA_424 0x1b
234 #define TRF7970A_ISO_CTRL_RFID BIT(5)
235 #define TRF7970A_ISO_CTRL_DIR_MODE BIT(6)
236 #define TRF7970A_ISO_CTRL_RX_CRC_N BIT(7) /* true == No CRC */
238 #define TRF7970A_ISO_CTRL_RFID_SPEED_MASK 0x1f
240 /* Modulator and SYS_CLK Control Register Bits */
241 #define TRF7970A_MODULATOR_DEPTH(n) ((n) & 0x7)
242 #define TRF7970A_MODULATOR_DEPTH_ASK10 (TRF7970A_MODULATOR_DEPTH(0))
243 #define TRF7970A_MODULATOR_DEPTH_OOK (TRF7970A_MODULATOR_DEPTH(1))
244 #define TRF7970A_MODULATOR_DEPTH_ASK7 (TRF7970A_MODULATOR_DEPTH(2))
245 #define TRF7970A_MODULATOR_DEPTH_ASK8_5 (TRF7970A_MODULATOR_DEPTH(3))
246 #define TRF7970A_MODULATOR_DEPTH_ASK13 (TRF7970A_MODULATOR_DEPTH(4))
247 #define TRF7970A_MODULATOR_DEPTH_ASK16 (TRF7970A_MODULATOR_DEPTH(5))
248 #define TRF7970A_MODULATOR_DEPTH_ASK22 (TRF7970A_MODULATOR_DEPTH(6))
249 #define TRF7970A_MODULATOR_DEPTH_ASK30 (TRF7970A_MODULATOR_DEPTH(7))
250 #define TRF7970A_MODULATOR_EN_ANA BIT(3)
251 #define TRF7970A_MODULATOR_CLK(n) (((n) & 0x3) << 4)
252 #define TRF7970A_MODULATOR_CLK_DISABLED (TRF7970A_MODULATOR_CLK(0))
253 #define TRF7970A_MODULATOR_CLK_3_6 (TRF7970A_MODULATOR_CLK(1))
254 #define TRF7970A_MODULATOR_CLK_6_13 (TRF7970A_MODULATOR_CLK(2))
255 #define TRF7970A_MODULATOR_CLK_13_27 (TRF7970A_MODULATOR_CLK(3))
256 #define TRF7970A_MODULATOR_EN_OOK BIT(6)
257 #define TRF7970A_MODULATOR_27MHZ BIT(7)
259 /* IRQ Status Register Bits */
260 #define TRF7970A_IRQ_STATUS_NORESP BIT(0) /* ISO15693 only */
261 #define TRF7970A_IRQ_STATUS_COL BIT(1)
262 #define TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR BIT(2)
263 #define TRF7970A_IRQ_STATUS_PARITY_ERROR BIT(3)
264 #define TRF7970A_IRQ_STATUS_CRC_ERROR BIT(4)
265 #define TRF7970A_IRQ_STATUS_FIFO BIT(5)
266 #define TRF7970A_IRQ_STATUS_SRX BIT(6)
267 #define TRF7970A_IRQ_STATUS_TX BIT(7)
269 #define TRF7970A_IRQ_STATUS_ERROR \
270 (TRF7970A_IRQ_STATUS_COL | \
271 TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR | \
272 TRF7970A_IRQ_STATUS_PARITY_ERROR | \
273 TRF7970A_IRQ_STATUS_CRC_ERROR)
275 #define TRF7970A_SPECIAL_FCN_REG1_COL_7_6 BIT(0)
276 #define TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL BIT(1)
277 #define TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX BIT(2)
278 #define TRF7970A_SPECIAL_FCN_REG1_SP_DIR_MODE BIT(3)
279 #define TRF7970A_SPECIAL_FCN_REG1_NEXT_SLOT_37US BIT(4)
280 #define TRF7970A_SPECIAL_FCN_REG1_PAR43 BIT(5)
282 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_124 (0x0 << 2)
283 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_120 (0x1 << 2)
284 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_112 (0x2 << 2)
285 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 (0x3 << 2)
286 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_4 0x0
287 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_8 0x1
288 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_16 0x2
289 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32 0x3
291 #define TRF7970A_FIFO_STATUS_OVERFLOW BIT(7)
293 /* NFC (ISO/IEC 14443A) Type 2 Tag commands */
294 #define NFC_T2T_CMD_READ 0x30
296 /* ISO 15693 commands codes */
297 #define ISO15693_CMD_INVENTORY 0x01
298 #define ISO15693_CMD_READ_SINGLE_BLOCK 0x20
299 #define ISO15693_CMD_WRITE_SINGLE_BLOCK 0x21
300 #define ISO15693_CMD_LOCK_BLOCK 0x22
301 #define ISO15693_CMD_READ_MULTIPLE_BLOCK 0x23
302 #define ISO15693_CMD_WRITE_MULTIPLE_BLOCK 0x24
303 #define ISO15693_CMD_SELECT 0x25
304 #define ISO15693_CMD_RESET_TO_READY 0x26
305 #define ISO15693_CMD_WRITE_AFI 0x27
306 #define ISO15693_CMD_LOCK_AFI 0x28
307 #define ISO15693_CMD_WRITE_DSFID 0x29
308 #define ISO15693_CMD_LOCK_DSFID 0x2a
309 #define ISO15693_CMD_GET_SYSTEM_INFO 0x2b
310 #define ISO15693_CMD_GET_MULTIPLE_BLOCK_SECURITY_STATUS 0x2c
312 /* ISO 15693 request and response flags */
313 #define ISO15693_REQ_FLAG_SUB_CARRIER BIT(0)
314 #define ISO15693_REQ_FLAG_DATA_RATE BIT(1)
315 #define ISO15693_REQ_FLAG_INVENTORY BIT(2)
316 #define ISO15693_REQ_FLAG_PROTOCOL_EXT BIT(3)
317 #define ISO15693_REQ_FLAG_SELECT BIT(4)
318 #define ISO15693_REQ_FLAG_AFI BIT(4)
319 #define ISO15693_REQ_FLAG_ADDRESS BIT(5)
320 #define ISO15693_REQ_FLAG_NB_SLOTS BIT(5)
321 #define ISO15693_REQ_FLAG_OPTION BIT(6)
323 #define ISO15693_REQ_FLAG_SPEED_MASK \
324 (ISO15693_REQ_FLAG_SUB_CARRIER | ISO15693_REQ_FLAG_DATA_RATE)
326 enum trf7970a_state
{
329 TRF7970A_ST_IDLE_RX_BLOCKED
,
330 TRF7970A_ST_WAIT_FOR_TX_FIFO
,
331 TRF7970A_ST_WAIT_FOR_RX_DATA
,
332 TRF7970A_ST_WAIT_FOR_RX_DATA_CONT
,
333 TRF7970A_ST_WAIT_TO_ISSUE_EOF
,
338 enum trf7970a_state state
;
340 struct spi_device
*spi
;
341 struct regulator
*regulator
;
342 struct nfc_digital_dev
*ddev
;
345 struct sk_buff
*tx_skb
;
346 struct sk_buff
*rx_skb
;
347 nfc_digital_cmd_complete_t cb
;
352 u8 modulator_sys_clk_ctrl
;
361 unsigned int timeout
;
363 struct delayed_work timeout_work
;
367 static int trf7970a_cmd(struct trf7970a
*trf
, u8 opcode
)
369 u8 cmd
= TRF7970A_CMD_BIT_CTRL
| TRF7970A_CMD_BIT_OPCODE(opcode
);
372 dev_dbg(trf
->dev
, "cmd: 0x%x\n", cmd
);
374 ret
= spi_write(trf
->spi
, &cmd
, 1);
376 dev_err(trf
->dev
, "%s - cmd: 0x%x, ret: %d\n", __func__
, cmd
,
381 static int trf7970a_read(struct trf7970a
*trf
, u8 reg
, u8
*val
)
383 u8 addr
= TRF7970A_CMD_BIT_RW
| reg
;
386 ret
= spi_write_then_read(trf
->spi
, &addr
, 1, val
, 1);
388 dev_err(trf
->dev
, "%s - addr: 0x%x, ret: %d\n", __func__
, addr
,
391 dev_dbg(trf
->dev
, "read(0x%x): 0x%x\n", addr
, *val
);
396 static int trf7970a_read_cont(struct trf7970a
*trf
, u8 reg
, u8
*buf
, size_t len
)
398 u8 addr
= reg
| TRF7970A_CMD_BIT_RW
| TRF7970A_CMD_BIT_CONTINUOUS
;
399 struct spi_transfer t
[2];
400 struct spi_message m
;
403 dev_dbg(trf
->dev
, "read_cont(0x%x, %zd)\n", addr
, len
);
405 spi_message_init(&m
);
407 memset(&t
, 0, sizeof(t
));
410 t
[0].len
= sizeof(addr
);
411 spi_message_add_tail(&t
[0], &m
);
415 spi_message_add_tail(&t
[1], &m
);
417 ret
= spi_sync(trf
->spi
, &m
);
419 dev_err(trf
->dev
, "%s - addr: 0x%x, ret: %d\n", __func__
, addr
,
424 static int trf7970a_write(struct trf7970a
*trf
, u8 reg
, u8 val
)
426 u8 buf
[2] = { reg
, val
};
429 dev_dbg(trf
->dev
, "write(0x%x): 0x%x\n", reg
, val
);
431 ret
= spi_write(trf
->spi
, buf
, 2);
433 dev_err(trf
->dev
, "%s - write: 0x%x 0x%x, ret: %d\n", __func__
,
434 buf
[0], buf
[1], ret
);
439 static int trf7970a_read_irqstatus(struct trf7970a
*trf
, u8
*status
)
445 addr
= TRF7970A_IRQ_STATUS
| TRF7970A_CMD_BIT_RW
;
447 if (trf
->quirks
& TRF7970A_QUIRK_IRQ_STATUS_READ
) {
448 addr
|= TRF7970A_CMD_BIT_CONTINUOUS
;
449 ret
= spi_write_then_read(trf
->spi
, &addr
, 1, buf
, 2);
451 ret
= spi_write_then_read(trf
->spi
, &addr
, 1, buf
, 1);
455 dev_err(trf
->dev
, "%s - irqstatus: Status read failed: %d\n",
463 static void trf7970a_send_upstream(struct trf7970a
*trf
)
467 dev_kfree_skb_any(trf
->tx_skb
);
470 if (trf
->rx_skb
&& !IS_ERR(trf
->rx_skb
) && !trf
->aborting
)
471 print_hex_dump_debug("trf7970a rx data: ", DUMP_PREFIX_NONE
,
472 16, 1, trf
->rx_skb
->data
, trf
->rx_skb
->len
,
475 /* According to the manual it is "good form" to reset the fifo and
476 * read the RSSI levels & oscillator status register here. It doesn't
479 trf7970a_cmd(trf
, TRF7970A_CMD_FIFO_RESET
);
480 trf7970a_read(trf
, TRF7970A_RSSI_OSC_STATUS
, &rssi
);
482 trf
->state
= TRF7970A_ST_IDLE
;
485 dev_dbg(trf
->dev
, "Abort process complete\n");
487 if (!IS_ERR(trf
->rx_skb
)) {
488 kfree_skb(trf
->rx_skb
);
489 trf
->rx_skb
= ERR_PTR(-ECANCELED
);
492 trf
->aborting
= false;
495 trf
->cb(trf
->ddev
, trf
->cb_arg
, trf
->rx_skb
);
500 static void trf7970a_send_err_upstream(struct trf7970a
*trf
, int errno
)
502 dev_dbg(trf
->dev
, "Error - state: %d, errno: %d\n", trf
->state
, errno
);
504 kfree_skb(trf
->rx_skb
);
505 trf
->rx_skb
= ERR_PTR(errno
);
507 trf7970a_send_upstream(trf
);
510 static int trf7970a_transmit(struct trf7970a
*trf
, struct sk_buff
*skb
,
513 unsigned int timeout
;
516 print_hex_dump_debug("trf7970a tx data: ", DUMP_PREFIX_NONE
,
517 16, 1, skb
->data
, len
, false);
519 ret
= spi_write(trf
->spi
, skb
->data
, len
);
521 dev_err(trf
->dev
, "%s - Can't send tx data: %d\n", __func__
,
529 trf
->state
= TRF7970A_ST_WAIT_FOR_TX_FIFO
;
530 timeout
= TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT
;
532 if (trf
->issue_eof
) {
533 trf
->state
= TRF7970A_ST_WAIT_TO_ISSUE_EOF
;
534 timeout
= TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF
;
536 trf
->state
= TRF7970A_ST_WAIT_FOR_RX_DATA
;
537 timeout
= trf
->timeout
;
541 dev_dbg(trf
->dev
, "Setting timeout for %d ms, state: %d\n", timeout
,
544 schedule_delayed_work(&trf
->timeout_work
, msecs_to_jiffies(timeout
));
549 static void trf7970a_fill_fifo(struct trf7970a
*trf
)
551 struct sk_buff
*skb
= trf
->tx_skb
;
556 ret
= trf7970a_read(trf
, TRF7970A_FIFO_STATUS
, &fifo_bytes
);
558 trf7970a_send_err_upstream(trf
, ret
);
562 dev_dbg(trf
->dev
, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes
);
564 fifo_bytes
&= ~TRF7970A_FIFO_STATUS_OVERFLOW
;
566 /* Calculate how much more data can be written to the fifo */
567 len
= TRF7970A_FIFO_SIZE
- fifo_bytes
;
568 len
= min(skb
->len
, len
);
570 ret
= trf7970a_transmit(trf
, skb
, len
);
572 trf7970a_send_err_upstream(trf
, ret
);
575 static void trf7970a_drain_fifo(struct trf7970a
*trf
, u8 status
)
577 struct sk_buff
*skb
= trf
->rx_skb
;
581 if (status
& TRF7970A_IRQ_STATUS_ERROR
) {
582 trf7970a_send_err_upstream(trf
, -EIO
);
586 ret
= trf7970a_read(trf
, TRF7970A_FIFO_STATUS
, &fifo_bytes
);
588 trf7970a_send_err_upstream(trf
, ret
);
592 dev_dbg(trf
->dev
, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes
);
594 fifo_bytes
&= ~TRF7970A_FIFO_STATUS_OVERFLOW
;
599 if (fifo_bytes
> skb_tailroom(skb
)) {
600 skb
= skb_copy_expand(skb
, skb_headroom(skb
),
601 max_t(int, fifo_bytes
,
602 TRF7970A_RX_SKB_ALLOC_SIZE
),
605 trf7970a_send_err_upstream(trf
, -ENOMEM
);
609 kfree_skb(trf
->rx_skb
);
613 ret
= trf7970a_read_cont(trf
, TRF7970A_FIFO_IO_REGISTER
,
614 skb_put(skb
, fifo_bytes
), fifo_bytes
);
616 trf7970a_send_err_upstream(trf
, ret
);
620 /* If received Type 2 ACK/NACK, shift right 4 bits and pass up */
621 if ((trf
->framing
== NFC_DIGITAL_FRAMING_NFCA_T2T
) && (skb
->len
== 1) &&
622 (trf
->special_fcn_reg1
==
623 TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX
)) {
625 status
= TRF7970A_IRQ_STATUS_SRX
;
627 trf
->state
= TRF7970A_ST_WAIT_FOR_RX_DATA_CONT
;
631 if (status
== TRF7970A_IRQ_STATUS_SRX
) { /* Receive complete */
632 trf7970a_send_upstream(trf
);
636 dev_dbg(trf
->dev
, "Setting timeout for %d ms\n",
637 TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT
);
639 schedule_delayed_work(&trf
->timeout_work
,
640 msecs_to_jiffies(TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT
));
643 static irqreturn_t
trf7970a_irq(int irq
, void *dev_id
)
645 struct trf7970a
*trf
= dev_id
;
649 mutex_lock(&trf
->lock
);
651 if (trf
->state
== TRF7970A_ST_OFF
) {
652 mutex_unlock(&trf
->lock
);
656 ret
= trf7970a_read_irqstatus(trf
, &status
);
658 mutex_unlock(&trf
->lock
);
662 dev_dbg(trf
->dev
, "IRQ - state: %d, status: 0x%x\n", trf
->state
,
666 mutex_unlock(&trf
->lock
);
670 switch (trf
->state
) {
671 case TRF7970A_ST_IDLE
:
672 case TRF7970A_ST_IDLE_RX_BLOCKED
:
673 /* If getting interrupts caused by RF noise, turn off the
674 * receiver to avoid unnecessary interrupts. It will be
675 * turned back on in trf7970a_in_send_cmd() when the next
678 if (status
& TRF7970A_IRQ_STATUS_ERROR
) {
679 trf7970a_cmd(trf
, TRF7970A_CMD_BLOCK_RX
);
680 trf
->state
= TRF7970A_ST_IDLE_RX_BLOCKED
;
683 trf7970a_cmd(trf
, TRF7970A_CMD_FIFO_RESET
);
685 case TRF7970A_ST_WAIT_FOR_TX_FIFO
:
686 if (status
& TRF7970A_IRQ_STATUS_TX
) {
687 trf
->ignore_timeout
=
688 !cancel_delayed_work(&trf
->timeout_work
);
689 trf7970a_fill_fifo(trf
);
691 trf7970a_send_err_upstream(trf
, -EIO
);
694 case TRF7970A_ST_WAIT_FOR_RX_DATA
:
695 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT
:
696 if (status
& TRF7970A_IRQ_STATUS_SRX
) {
697 trf
->ignore_timeout
=
698 !cancel_delayed_work(&trf
->timeout_work
);
699 trf7970a_drain_fifo(trf
, status
);
700 } else if (status
== TRF7970A_IRQ_STATUS_TX
) {
701 trf7970a_cmd(trf
, TRF7970A_CMD_FIFO_RESET
);
703 trf7970a_send_err_upstream(trf
, -EIO
);
706 case TRF7970A_ST_WAIT_TO_ISSUE_EOF
:
707 if (status
!= TRF7970A_IRQ_STATUS_TX
)
708 trf7970a_send_err_upstream(trf
, -EIO
);
711 dev_err(trf
->dev
, "%s - Driver in invalid state: %d\n",
712 __func__
, trf
->state
);
715 mutex_unlock(&trf
->lock
);
719 static void trf7970a_issue_eof(struct trf7970a
*trf
)
723 dev_dbg(trf
->dev
, "Issuing EOF\n");
725 ret
= trf7970a_cmd(trf
, TRF7970A_CMD_FIFO_RESET
);
727 trf7970a_send_err_upstream(trf
, ret
);
729 ret
= trf7970a_cmd(trf
, TRF7970A_CMD_EOF
);
731 trf7970a_send_err_upstream(trf
, ret
);
733 trf
->state
= TRF7970A_ST_WAIT_FOR_RX_DATA
;
735 dev_dbg(trf
->dev
, "Setting timeout for %d ms, state: %d\n",
736 trf
->timeout
, trf
->state
);
738 schedule_delayed_work(&trf
->timeout_work
,
739 msecs_to_jiffies(trf
->timeout
));
742 static void trf7970a_timeout_work_handler(struct work_struct
*work
)
744 struct trf7970a
*trf
= container_of(work
, struct trf7970a
,
747 dev_dbg(trf
->dev
, "Timeout - state: %d, ignore_timeout: %d\n",
748 trf
->state
, trf
->ignore_timeout
);
750 mutex_lock(&trf
->lock
);
752 if (trf
->ignore_timeout
)
753 trf
->ignore_timeout
= false;
754 else if (trf
->state
== TRF7970A_ST_WAIT_FOR_RX_DATA_CONT
)
755 trf7970a_send_upstream(trf
); /* No more rx data so send up */
756 else if (trf
->state
== TRF7970A_ST_WAIT_TO_ISSUE_EOF
)
757 trf7970a_issue_eof(trf
);
759 trf7970a_send_err_upstream(trf
, -ETIMEDOUT
);
761 mutex_unlock(&trf
->lock
);
764 static int trf7970a_init(struct trf7970a
*trf
)
768 dev_dbg(trf
->dev
, "Initializing device - state: %d\n", trf
->state
);
770 ret
= trf7970a_cmd(trf
, TRF7970A_CMD_SOFT_INIT
);
774 ret
= trf7970a_cmd(trf
, TRF7970A_CMD_IDLE
);
778 ret
= trf7970a_write(trf
, TRF7970A_MODULATOR_SYS_CLK_CTRL
, 0);
782 trf
->modulator_sys_clk_ctrl
= 0;
784 /* Must clear NFC Target Detection Level reg due to erratum */
785 ret
= trf7970a_write(trf
, TRF7970A_NFC_TARGET_LEVEL
, 0);
789 ret
= trf7970a_write(trf
, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS
,
790 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96
|
791 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32
);
795 ret
= trf7970a_write(trf
, TRF7970A_SPECIAL_FCN_REG1
, 0);
799 trf
->special_fcn_reg1
= 0;
801 trf
->iso_ctrl
= 0xff;
805 dev_dbg(trf
->dev
, "Couldn't init device: %d\n", ret
);
809 static void trf7970a_switch_rf_off(struct trf7970a
*trf
)
811 dev_dbg(trf
->dev
, "Switching rf off\n");
813 trf
->chip_status_ctrl
&= ~TRF7970A_CHIP_STATUS_RF_ON
;
815 trf7970a_write(trf
, TRF7970A_CHIP_STATUS_CTRL
, trf
->chip_status_ctrl
);
817 trf
->aborting
= false;
818 trf
->state
= TRF7970A_ST_OFF
;
820 pm_runtime_mark_last_busy(trf
->dev
);
821 pm_runtime_put_autosuspend(trf
->dev
);
824 static void trf7970a_switch_rf_on(struct trf7970a
*trf
)
828 dev_dbg(trf
->dev
, "Switching rf on\n");
830 pm_runtime_get_sync(trf
->dev
);
832 ret
= trf7970a_init(trf
);
834 dev_err(trf
->dev
, "%s - Can't initialize: %d\n", __func__
, ret
);
838 trf
->state
= TRF7970A_ST_IDLE
;
841 static int trf7970a_switch_rf(struct nfc_digital_dev
*ddev
, bool on
)
843 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
845 dev_dbg(trf
->dev
, "Switching RF - state: %d, on: %d\n", trf
->state
, on
);
847 mutex_lock(&trf
->lock
);
850 switch (trf
->state
) {
851 case TRF7970A_ST_OFF
:
852 trf7970a_switch_rf_on(trf
);
854 case TRF7970A_ST_IDLE
:
855 case TRF7970A_ST_IDLE_RX_BLOCKED
:
858 dev_err(trf
->dev
, "%s - Invalid request: %d %d\n",
859 __func__
, trf
->state
, on
);
860 trf7970a_switch_rf_off(trf
);
863 switch (trf
->state
) {
864 case TRF7970A_ST_OFF
:
867 dev_err(trf
->dev
, "%s - Invalid request: %d %d\n",
868 __func__
, trf
->state
, on
);
870 case TRF7970A_ST_IDLE
:
871 case TRF7970A_ST_IDLE_RX_BLOCKED
:
872 trf7970a_switch_rf_off(trf
);
876 mutex_unlock(&trf
->lock
);
880 static int trf7970a_config_rf_tech(struct trf7970a
*trf
, int tech
)
884 dev_dbg(trf
->dev
, "rf technology: %d\n", tech
);
887 case NFC_DIGITAL_RF_TECH_106A
:
888 trf
->iso_ctrl_tech
= TRF7970A_ISO_CTRL_14443A_106
;
889 trf
->modulator_sys_clk_ctrl
= TRF7970A_MODULATOR_DEPTH_OOK
;
891 case NFC_DIGITAL_RF_TECH_106B
:
892 trf
->iso_ctrl_tech
= TRF7970A_ISO_CTRL_14443B_106
;
893 trf
->modulator_sys_clk_ctrl
= TRF7970A_MODULATOR_DEPTH_ASK10
;
895 case NFC_DIGITAL_RF_TECH_212F
:
896 trf
->iso_ctrl_tech
= TRF7970A_ISO_CTRL_FELICA_212
;
897 trf
->modulator_sys_clk_ctrl
= TRF7970A_MODULATOR_DEPTH_ASK10
;
899 case NFC_DIGITAL_RF_TECH_424F
:
900 trf
->iso_ctrl_tech
= TRF7970A_ISO_CTRL_FELICA_424
;
901 trf
->modulator_sys_clk_ctrl
= TRF7970A_MODULATOR_DEPTH_ASK10
;
903 case NFC_DIGITAL_RF_TECH_ISO15693
:
904 trf
->iso_ctrl_tech
= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648
;
905 trf
->modulator_sys_clk_ctrl
= TRF7970A_MODULATOR_DEPTH_OOK
;
908 dev_dbg(trf
->dev
, "Unsupported rf technology: %d\n", tech
);
912 trf
->technology
= tech
;
917 static int trf7970a_config_framing(struct trf7970a
*trf
, int framing
)
919 u8 iso_ctrl
= trf
->iso_ctrl_tech
;
922 dev_dbg(trf
->dev
, "framing: %d\n", framing
);
925 case NFC_DIGITAL_FRAMING_NFCA_SHORT
:
926 case NFC_DIGITAL_FRAMING_NFCA_STANDARD
:
927 trf
->tx_cmd
= TRF7970A_CMD_TRANSMIT_NO_CRC
;
928 iso_ctrl
|= TRF7970A_ISO_CTRL_RX_CRC_N
;
930 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A
:
931 case NFC_DIGITAL_FRAMING_NFCA_T4T
:
932 case NFC_DIGITAL_FRAMING_NFCB
:
933 case NFC_DIGITAL_FRAMING_NFCB_T4T
:
934 case NFC_DIGITAL_FRAMING_NFCF
:
935 case NFC_DIGITAL_FRAMING_NFCF_T3T
:
936 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY
:
937 case NFC_DIGITAL_FRAMING_ISO15693_T5T
:
938 trf
->tx_cmd
= TRF7970A_CMD_TRANSMIT
;
939 iso_ctrl
&= ~TRF7970A_ISO_CTRL_RX_CRC_N
;
941 case NFC_DIGITAL_FRAMING_NFCA_T2T
:
942 trf
->tx_cmd
= TRF7970A_CMD_TRANSMIT
;
943 iso_ctrl
|= TRF7970A_ISO_CTRL_RX_CRC_N
;
946 dev_dbg(trf
->dev
, "Unsupported Framing: %d\n", framing
);
950 trf
->framing
= framing
;
952 if (iso_ctrl
!= trf
->iso_ctrl
) {
953 ret
= trf7970a_write(trf
, TRF7970A_ISO_CTRL
, iso_ctrl
);
957 trf
->iso_ctrl
= iso_ctrl
;
959 ret
= trf7970a_write(trf
, TRF7970A_MODULATOR_SYS_CLK_CTRL
,
960 trf
->modulator_sys_clk_ctrl
);
965 if (!(trf
->chip_status_ctrl
& TRF7970A_CHIP_STATUS_RF_ON
)) {
966 ret
= trf7970a_write(trf
, TRF7970A_CHIP_STATUS_CTRL
,
967 trf
->chip_status_ctrl
|
968 TRF7970A_CHIP_STATUS_RF_ON
);
972 trf
->chip_status_ctrl
|= TRF7970A_CHIP_STATUS_RF_ON
;
974 usleep_range(5000, 6000);
980 static int trf7970a_in_configure_hw(struct nfc_digital_dev
*ddev
, int type
,
983 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
986 dev_dbg(trf
->dev
, "Configure hw - type: %d, param: %d\n", type
, param
);
988 mutex_lock(&trf
->lock
);
990 if (trf
->state
== TRF7970A_ST_OFF
)
991 trf7970a_switch_rf_on(trf
);
994 case NFC_DIGITAL_CONFIG_RF_TECH
:
995 ret
= trf7970a_config_rf_tech(trf
, param
);
997 case NFC_DIGITAL_CONFIG_FRAMING
:
998 ret
= trf7970a_config_framing(trf
, param
);
1001 dev_dbg(trf
->dev
, "Unknown type: %d\n", type
);
1005 mutex_unlock(&trf
->lock
);
1009 static int trf7970a_is_iso15693_write_or_lock(u8 cmd
)
1012 case ISO15693_CMD_WRITE_SINGLE_BLOCK
:
1013 case ISO15693_CMD_LOCK_BLOCK
:
1014 case ISO15693_CMD_WRITE_MULTIPLE_BLOCK
:
1015 case ISO15693_CMD_WRITE_AFI
:
1016 case ISO15693_CMD_LOCK_AFI
:
1017 case ISO15693_CMD_WRITE_DSFID
:
1018 case ISO15693_CMD_LOCK_DSFID
:
1026 static int trf7970a_per_cmd_config(struct trf7970a
*trf
, struct sk_buff
*skb
)
1028 u8
*req
= skb
->data
;
1029 u8 special_fcn_reg1
, iso_ctrl
;
1032 trf
->issue_eof
= false;
1034 /* When issuing Type 2 read command, make sure the '4_bit_RX' bit in
1035 * special functions register 1 is cleared; otherwise, its a write or
1036 * sector select command and '4_bit_RX' must be set.
1038 * When issuing an ISO 15693 command, inspect the flags byte to see
1039 * what speed to use. Also, remember if the OPTION flag is set on
1040 * a Type 5 write or lock command so the driver will know that it
1041 * has to send an EOF in order to get a response.
1043 if ((trf
->technology
== NFC_DIGITAL_RF_TECH_106A
) &&
1044 (trf
->framing
== NFC_DIGITAL_FRAMING_NFCA_T2T
)) {
1045 if (req
[0] == NFC_T2T_CMD_READ
)
1046 special_fcn_reg1
= 0;
1048 special_fcn_reg1
= TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX
;
1050 if (special_fcn_reg1
!= trf
->special_fcn_reg1
) {
1051 ret
= trf7970a_write(trf
, TRF7970A_SPECIAL_FCN_REG1
,
1056 trf
->special_fcn_reg1
= special_fcn_reg1
;
1058 } else if (trf
->technology
== NFC_DIGITAL_RF_TECH_ISO15693
) {
1059 iso_ctrl
= trf
->iso_ctrl
& ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK
;
1061 switch (req
[0] & ISO15693_REQ_FLAG_SPEED_MASK
) {
1063 iso_ctrl
|= TRF7970A_ISO_CTRL_15693_SGL_1OF4_662
;
1065 case ISO15693_REQ_FLAG_SUB_CARRIER
:
1066 iso_ctrl
|= TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a
;
1068 case ISO15693_REQ_FLAG_DATA_RATE
:
1069 iso_ctrl
|= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648
;
1071 case (ISO15693_REQ_FLAG_SUB_CARRIER
|
1072 ISO15693_REQ_FLAG_DATA_RATE
):
1073 iso_ctrl
|= TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669
;
1077 if (iso_ctrl
!= trf
->iso_ctrl
) {
1078 ret
= trf7970a_write(trf
, TRF7970A_ISO_CTRL
, iso_ctrl
);
1082 trf
->iso_ctrl
= iso_ctrl
;
1085 if ((trf
->framing
== NFC_DIGITAL_FRAMING_ISO15693_T5T
) &&
1086 trf7970a_is_iso15693_write_or_lock(req
[1]) &&
1087 (req
[0] & ISO15693_REQ_FLAG_OPTION
))
1088 trf
->issue_eof
= true;
1094 static int trf7970a_in_send_cmd(struct nfc_digital_dev
*ddev
,
1095 struct sk_buff
*skb
, u16 timeout
,
1096 nfc_digital_cmd_complete_t cb
, void *arg
)
1098 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
1103 dev_dbg(trf
->dev
, "New request - state: %d, timeout: %d ms, len: %d\n",
1104 trf
->state
, timeout
, skb
->len
);
1106 if (skb
->len
> TRF7970A_TX_MAX
)
1109 mutex_lock(&trf
->lock
);
1111 if ((trf
->state
!= TRF7970A_ST_IDLE
) &&
1112 (trf
->state
!= TRF7970A_ST_IDLE_RX_BLOCKED
)) {
1113 dev_err(trf
->dev
, "%s - Bogus state: %d\n", __func__
,
1119 if (trf
->aborting
) {
1120 dev_dbg(trf
->dev
, "Abort process complete\n");
1121 trf
->aborting
= false;
1126 trf
->rx_skb
= nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE
,
1129 dev_dbg(trf
->dev
, "Can't alloc rx_skb\n");
1134 if (trf
->state
== TRF7970A_ST_IDLE_RX_BLOCKED
) {
1135 ret
= trf7970a_cmd(trf
, TRF7970A_CMD_ENABLE_RX
);
1139 trf
->state
= TRF7970A_ST_IDLE
;
1142 ret
= trf7970a_per_cmd_config(trf
, skb
);
1150 trf
->timeout
= timeout
;
1151 trf
->ignore_timeout
= false;
1154 prefix
= skb_push(skb
, TRF7970A_TX_SKB_HEADROOM
);
1156 /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
1157 * on what the current framing is, the address of the TX length byte 1
1158 * register (0x1d), and the 2 byte length of the data to be transmitted.
1160 prefix
[0] = TRF7970A_CMD_BIT_CTRL
|
1161 TRF7970A_CMD_BIT_OPCODE(TRF7970A_CMD_FIFO_RESET
);
1162 prefix
[1] = TRF7970A_CMD_BIT_CTRL
|
1163 TRF7970A_CMD_BIT_OPCODE(trf
->tx_cmd
);
1164 prefix
[2] = TRF7970A_CMD_BIT_CONTINUOUS
| TRF7970A_TX_LENGTH_BYTE1
;
1166 if (trf
->framing
== NFC_DIGITAL_FRAMING_NFCA_SHORT
) {
1168 prefix
[4] = 0x0f; /* 7 bits */
1170 prefix
[3] = (len
& 0xf00) >> 4;
1171 prefix
[3] |= ((len
& 0xf0) >> 4);
1172 prefix
[4] = ((len
& 0x0f) << 4);
1175 len
= min_t(int, skb
->len
, TRF7970A_FIFO_SIZE
);
1177 ret
= trf7970a_transmit(trf
, skb
, len
);
1179 kfree_skb(trf
->rx_skb
);
1184 mutex_unlock(&trf
->lock
);
1188 static int trf7970a_tg_configure_hw(struct nfc_digital_dev
*ddev
,
1189 int type
, int param
)
1191 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
1193 dev_dbg(trf
->dev
, "Unsupported interface\n");
1198 static int trf7970a_tg_send_cmd(struct nfc_digital_dev
*ddev
,
1199 struct sk_buff
*skb
, u16 timeout
,
1200 nfc_digital_cmd_complete_t cb
, void *arg
)
1202 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
1204 dev_dbg(trf
->dev
, "Unsupported interface\n");
1209 static int trf7970a_tg_listen(struct nfc_digital_dev
*ddev
,
1210 u16 timeout
, nfc_digital_cmd_complete_t cb
, void *arg
)
1212 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
1214 dev_dbg(trf
->dev
, "Unsupported interface\n");
1219 static void trf7970a_abort_cmd(struct nfc_digital_dev
*ddev
)
1221 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
1223 dev_dbg(trf
->dev
, "Abort process initiated\n");
1225 mutex_lock(&trf
->lock
);
1227 switch (trf
->state
) {
1228 case TRF7970A_ST_WAIT_FOR_TX_FIFO
:
1229 case TRF7970A_ST_WAIT_FOR_RX_DATA
:
1230 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT
:
1231 case TRF7970A_ST_WAIT_TO_ISSUE_EOF
:
1232 trf
->aborting
= true;
1238 mutex_unlock(&trf
->lock
);
1241 static struct nfc_digital_ops trf7970a_nfc_ops
= {
1242 .in_configure_hw
= trf7970a_in_configure_hw
,
1243 .in_send_cmd
= trf7970a_in_send_cmd
,
1244 .tg_configure_hw
= trf7970a_tg_configure_hw
,
1245 .tg_send_cmd
= trf7970a_tg_send_cmd
,
1246 .tg_listen
= trf7970a_tg_listen
,
1247 .switch_rf
= trf7970a_switch_rf
,
1248 .abort_cmd
= trf7970a_abort_cmd
,
1251 static int trf7970a_get_autosuspend_delay(struct device_node
*np
)
1253 int autosuspend_delay
, ret
;
1255 ret
= of_property_read_u32(np
, "autosuspend-delay", &autosuspend_delay
);
1257 autosuspend_delay
= TRF7970A_AUTOSUSPEND_DELAY
;
1259 return autosuspend_delay
;
1262 static int trf7970a_get_vin_voltage_override(struct device_node
*np
,
1265 return of_property_read_u32(np
, "vin-voltage-override", vin_uvolts
);
1268 static int trf7970a_probe(struct spi_device
*spi
)
1270 struct device_node
*np
= spi
->dev
.of_node
;
1271 struct trf7970a
*trf
;
1272 int uvolts
, autosuspend_delay
, ret
;
1275 dev_err(&spi
->dev
, "No Device Tree entry\n");
1279 trf
= devm_kzalloc(&spi
->dev
, sizeof(*trf
), GFP_KERNEL
);
1283 trf
->state
= TRF7970A_ST_OFF
;
1284 trf
->dev
= &spi
->dev
;
1287 spi
->mode
= SPI_MODE_1
;
1288 spi
->bits_per_word
= 8;
1290 ret
= spi_setup(spi
);
1292 dev_err(trf
->dev
, "Can't set up SPI Communication\n");
1296 if (of_property_read_bool(np
, "irq-status-read-quirk"))
1297 trf
->quirks
|= TRF7970A_QUIRK_IRQ_STATUS_READ
;
1299 /* There are two enable pins - both must be present */
1300 trf
->en_gpio
= of_get_named_gpio(np
, "ti,enable-gpios", 0);
1301 if (!gpio_is_valid(trf
->en_gpio
)) {
1302 dev_err(trf
->dev
, "No EN GPIO property\n");
1303 return trf
->en_gpio
;
1306 ret
= devm_gpio_request_one(trf
->dev
, trf
->en_gpio
,
1307 GPIOF_DIR_OUT
| GPIOF_INIT_LOW
, "trf7970a EN");
1309 dev_err(trf
->dev
, "Can't request EN GPIO: %d\n", ret
);
1313 trf
->en2_gpio
= of_get_named_gpio(np
, "ti,enable-gpios", 1);
1314 if (!gpio_is_valid(trf
->en2_gpio
)) {
1315 dev_err(trf
->dev
, "No EN2 GPIO property\n");
1316 return trf
->en2_gpio
;
1319 ret
= devm_gpio_request_one(trf
->dev
, trf
->en2_gpio
,
1320 GPIOF_DIR_OUT
| GPIOF_INIT_LOW
, "trf7970a EN2");
1322 dev_err(trf
->dev
, "Can't request EN2 GPIO: %d\n", ret
);
1326 if (of_property_read_bool(np
, "en2-rf-quirk"))
1327 trf
->quirks
|= TRF7970A_QUIRK_EN2_MUST_STAY_LOW
;
1329 ret
= devm_request_threaded_irq(trf
->dev
, spi
->irq
, NULL
,
1330 trf7970a_irq
, IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
1333 dev_err(trf
->dev
, "Can't request IRQ#%d: %d\n", spi
->irq
, ret
);
1337 mutex_init(&trf
->lock
);
1338 INIT_DELAYED_WORK(&trf
->timeout_work
, trf7970a_timeout_work_handler
);
1340 trf
->regulator
= devm_regulator_get(&spi
->dev
, "vin");
1341 if (IS_ERR(trf
->regulator
)) {
1342 ret
= PTR_ERR(trf
->regulator
);
1343 dev_err(trf
->dev
, "Can't get VIN regulator: %d\n", ret
);
1344 goto err_destroy_lock
;
1347 ret
= regulator_enable(trf
->regulator
);
1349 dev_err(trf
->dev
, "Can't enable VIN: %d\n", ret
);
1350 goto err_destroy_lock
;
1353 ret
= trf7970a_get_vin_voltage_override(np
, &uvolts
);
1355 uvolts
= regulator_get_voltage(trf
->regulator
);
1357 if (uvolts
> 4000000)
1358 trf
->chip_status_ctrl
= TRF7970A_CHIP_STATUS_VRS5_3
;
1360 trf
->ddev
= nfc_digital_allocate_device(&trf7970a_nfc_ops
,
1361 TRF7970A_SUPPORTED_PROTOCOLS
,
1362 NFC_DIGITAL_DRV_CAPS_IN_CRC
, TRF7970A_TX_SKB_HEADROOM
,
1365 dev_err(trf
->dev
, "Can't allocate NFC digital device\n");
1367 goto err_disable_regulator
;
1370 nfc_digital_set_parent_dev(trf
->ddev
, trf
->dev
);
1371 nfc_digital_set_drvdata(trf
->ddev
, trf
);
1372 spi_set_drvdata(spi
, trf
);
1374 autosuspend_delay
= trf7970a_get_autosuspend_delay(np
);
1376 pm_runtime_set_autosuspend_delay(trf
->dev
, autosuspend_delay
);
1377 pm_runtime_use_autosuspend(trf
->dev
);
1378 pm_runtime_enable(trf
->dev
);
1380 ret
= nfc_digital_register_device(trf
->ddev
);
1382 dev_err(trf
->dev
, "Can't register NFC digital device: %d\n",
1390 pm_runtime_disable(trf
->dev
);
1391 nfc_digital_free_device(trf
->ddev
);
1392 err_disable_regulator
:
1393 regulator_disable(trf
->regulator
);
1395 mutex_destroy(&trf
->lock
);
1399 static int trf7970a_remove(struct spi_device
*spi
)
1401 struct trf7970a
*trf
= spi_get_drvdata(spi
);
1403 mutex_lock(&trf
->lock
);
1405 switch (trf
->state
) {
1406 case TRF7970A_ST_WAIT_FOR_TX_FIFO
:
1407 case TRF7970A_ST_WAIT_FOR_RX_DATA
:
1408 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT
:
1409 case TRF7970A_ST_WAIT_TO_ISSUE_EOF
:
1410 trf7970a_send_err_upstream(trf
, -ECANCELED
);
1412 case TRF7970A_ST_IDLE
:
1413 case TRF7970A_ST_IDLE_RX_BLOCKED
:
1414 pm_runtime_put_sync(trf
->dev
);
1420 mutex_unlock(&trf
->lock
);
1422 pm_runtime_disable(trf
->dev
);
1424 nfc_digital_unregister_device(trf
->ddev
);
1425 nfc_digital_free_device(trf
->ddev
);
1427 regulator_disable(trf
->regulator
);
1429 mutex_destroy(&trf
->lock
);
1434 #ifdef CONFIG_PM_RUNTIME
1435 static int trf7970a_pm_runtime_suspend(struct device
*dev
)
1437 struct spi_device
*spi
= container_of(dev
, struct spi_device
, dev
);
1438 struct trf7970a
*trf
= spi_get_drvdata(spi
);
1441 dev_dbg(dev
, "Runtime suspend\n");
1443 if (trf
->state
!= TRF7970A_ST_OFF
) {
1444 dev_dbg(dev
, "Can't suspend - not in OFF state (%d)\n",
1449 gpio_set_value(trf
->en_gpio
, 0);
1450 gpio_set_value(trf
->en2_gpio
, 0);
1452 ret
= regulator_disable(trf
->regulator
);
1454 dev_err(dev
, "%s - Can't disable VIN: %d\n", __func__
, ret
);
1459 static int trf7970a_pm_runtime_resume(struct device
*dev
)
1461 struct spi_device
*spi
= container_of(dev
, struct spi_device
, dev
);
1462 struct trf7970a
*trf
= spi_get_drvdata(spi
);
1465 dev_dbg(dev
, "Runtime resume\n");
1467 ret
= regulator_enable(trf
->regulator
);
1469 dev_err(dev
, "%s - Can't enable VIN: %d\n", __func__
, ret
);
1473 usleep_range(5000, 6000);
1475 if (!(trf
->quirks
& TRF7970A_QUIRK_EN2_MUST_STAY_LOW
)) {
1476 gpio_set_value(trf
->en2_gpio
, 1);
1477 usleep_range(1000, 2000);
1480 gpio_set_value(trf
->en_gpio
, 1);
1482 usleep_range(20000, 21000);
1484 pm_runtime_mark_last_busy(dev
);
1490 static const struct dev_pm_ops trf7970a_pm_ops
= {
1491 SET_RUNTIME_PM_OPS(trf7970a_pm_runtime_suspend
,
1492 trf7970a_pm_runtime_resume
, NULL
)
1495 static const struct spi_device_id trf7970a_id_table
[] = {
1499 MODULE_DEVICE_TABLE(spi
, trf7970a_id_table
);
1501 static struct spi_driver trf7970a_spi_driver
= {
1502 .probe
= trf7970a_probe
,
1503 .remove
= trf7970a_remove
,
1504 .id_table
= trf7970a_id_table
,
1507 .owner
= THIS_MODULE
,
1508 .pm
= &trf7970a_pm_ops
,
1512 module_spi_driver(trf7970a_spi_driver
);
1514 MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>");
1515 MODULE_LICENSE("GPL v2");
1516 MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver");