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.
41 * Timeouts are implemented using the delayed workqueue kernel facility.
42 * Timeouts are required so things don't hang when there is no response
43 * from the trf7970a (or tag). Using this mechanism creates a race with
44 * interrupts, however. That is, an interrupt and a timeout could occur
45 * closely enough together that one is blocked by the mutex while the other
46 * executes. When the timeout handler executes first and blocks the
47 * interrupt handler, it will eventually set the state to IDLE so the
48 * interrupt handler will check the state and exit with no harm done.
49 * When the interrupt handler executes first and blocks the timeout handler,
50 * the cancel_delayed_work() call will know that it didn't cancel the
51 * work item (i.e., timeout) and will return zero. That return code is
52 * used by the timer handler to indicate that it should ignore the timeout
55 * Aborting an active command isn't as simple as it seems because the only
56 * way to abort a command that's already been sent to the tag is so turn
57 * off power to the tag. If we do that, though, we'd have to go through
58 * the entire anticollision procedure again but the digital layer doesn't
59 * support that. So, if an abort is received before trf7970a_in_send_cmd()
60 * has sent the command to the tag, it simply returns -ECANCELED. If the
61 * command has already been sent to the tag, then the driver continues
62 * normally and recieves the response data (or error) but just before
63 * sending the data upstream, it frees the rx_skb and sends -ECANCELED
64 * upstream instead. If the command failed, that error will be sent
67 * When recieving data from a tag and the interrupt status register has
68 * only the SRX bit set, it means that all of the data has been received
69 * (once what's in the fifo has been read). However, depending on timing
70 * an interrupt status with only the SRX bit set may not be recived. In
71 * those cases, the timeout mechanism is used to wait 20 ms in case more
72 * data arrives. After 20 ms, it is assumed that all of the data has been
73 * received and the accumulated rx data is sent upstream. The
74 * 'TRF7970A_ST_WAIT_FOR_RX_DATA_CONT' state is used for this purpose
75 * (i.e., it indicates that some data has been received but we're not sure
76 * if there is more coming so a timeout in this state means all data has
77 * been received and there isn't an error). The delay is 20 ms since delays
78 * of ~16 ms have been observed during testing.
80 * Type 2 write and sector select commands respond with a 4-bit ACK or NACK.
81 * Having only 4 bits in the FIFO won't normally generate an interrupt so
82 * driver enables the '4_bit_RX' bit of the Special Functions register 1
83 * to cause an interrupt in that case. Leaving that bit for a read command
84 * messes up the data returned so it is only enabled when the framing is
85 * 'NFC_DIGITAL_FRAMING_NFCA_T2T' and the command is not a read command.
86 * Unfortunately, that means that the driver has to peek into tx frames
87 * when the framing is 'NFC_DIGITAL_FRAMING_NFCA_T2T'. This is done by
88 * the trf7970a_per_cmd_config() routine.
90 * ISO/IEC 15693 frames specify whether to use single or double sub-carrier
91 * frequencies and whether to use low or high data rates in the flags byte
92 * of the frame. This means that the driver has to peek at all 15693 frames
93 * to determine what speed to set the communication to. In addition, write
94 * and lock commands use the OPTION flag to indicate that an EOF must be
95 * sent to the tag before it will send its response. So the driver has to
96 * examine all frames for that reason too.
98 * It is unclear how long to wait before sending the EOF. According to the
99 * Note under Table 1-1 in section 1.6 of
100 * http://www.ti.com/lit/ug/scbu011/scbu011.pdf, that wait should be at least
101 * 10 ms for TI Tag-it HF-I tags; however testing has shown that is not long
102 * enough. For this reason, the driver waits 20 ms which seems to work
106 #define TRF7970A_SUPPORTED_PROTOCOLS \
107 (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \
108 NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_FELICA_MASK | \
109 NFC_PROTO_ISO15693_MASK)
111 #define TRF7970A_AUTOSUSPEND_DELAY 30000 /* 30 seconds */
113 /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
114 * on what the current framing is, the address of the TX length byte 1
115 * register (0x1d), and the 2 byte length of the data to be transmitted.
116 * That totals 5 bytes.
118 #define TRF7970A_TX_SKB_HEADROOM 5
120 #define TRF7970A_RX_SKB_ALLOC_SIZE 256
122 #define TRF7970A_FIFO_SIZE 128
124 /* TX length is 3 nibbles long ==> 4KB - 1 bytes max */
125 #define TRF7970A_TX_MAX (4096 - 1)
127 #define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 20
128 #define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 3
129 #define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 20
132 /* Erratum: When reading IRQ Status register on trf7970a, we must issue a
133 * read continuous command for IRQ Status and Collision Position registers.
135 #define TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA BIT(0)
137 /* Direct commands */
138 #define TRF7970A_CMD_IDLE 0x00
139 #define TRF7970A_CMD_SOFT_INIT 0x03
140 #define TRF7970A_CMD_RF_COLLISION 0x04
141 #define TRF7970A_CMD_RF_COLLISION_RESPONSE_N 0x05
142 #define TRF7970A_CMD_RF_COLLISION_RESPONSE_0 0x06
143 #define TRF7970A_CMD_FIFO_RESET 0x0f
144 #define TRF7970A_CMD_TRANSMIT_NO_CRC 0x10
145 #define TRF7970A_CMD_TRANSMIT 0x11
146 #define TRF7970A_CMD_DELAY_TRANSMIT_NO_CRC 0x12
147 #define TRF7970A_CMD_DELAY_TRANSMIT 0x13
148 #define TRF7970A_CMD_EOF 0x14
149 #define TRF7970A_CMD_CLOSE_SLOT 0x15
150 #define TRF7970A_CMD_BLOCK_RX 0x16
151 #define TRF7970A_CMD_ENABLE_RX 0x17
152 #define TRF7970A_CMD_TEST_EXT_RF 0x18
153 #define TRF7970A_CMD_TEST_INT_RF 0x19
154 #define TRF7970A_CMD_RX_GAIN_ADJUST 0x1a
156 /* Bits determining whether its a direct command or register R/W,
157 * whether to use a continuous SPI transaction or not, and the actual
158 * direct cmd opcode or regster address.
160 #define TRF7970A_CMD_BIT_CTRL BIT(7)
161 #define TRF7970A_CMD_BIT_RW BIT(6)
162 #define TRF7970A_CMD_BIT_CONTINUOUS BIT(5)
163 #define TRF7970A_CMD_BIT_OPCODE(opcode) ((opcode) & 0x1f)
165 /* Registers addresses */
166 #define TRF7970A_CHIP_STATUS_CTRL 0x00
167 #define TRF7970A_ISO_CTRL 0x01
168 #define TRF7970A_ISO14443B_TX_OPTIONS 0x02
169 #define TRF7970A_ISO14443A_HIGH_BITRATE_OPTIONS 0x03
170 #define TRF7970A_TX_TIMER_SETTING_H_BYTE 0x04
171 #define TRF7970A_TX_TIMER_SETTING_L_BYTE 0x05
172 #define TRF7970A_TX_PULSE_LENGTH_CTRL 0x06
173 #define TRF7970A_RX_NO_RESPONSE_WAIT 0x07
174 #define TRF7970A_RX_WAIT_TIME 0x08
175 #define TRF7970A_MODULATOR_SYS_CLK_CTRL 0x09
176 #define TRF7970A_RX_SPECIAL_SETTINGS 0x0a
177 #define TRF7970A_REG_IO_CTRL 0x0b
178 #define TRF7970A_IRQ_STATUS 0x0c
179 #define TRF7970A_COLLISION_IRQ_MASK 0x0d
180 #define TRF7970A_COLLISION_POSITION 0x0e
181 #define TRF7970A_RSSI_OSC_STATUS 0x0f
182 #define TRF7970A_SPECIAL_FCN_REG1 0x10
183 #define TRF7970A_SPECIAL_FCN_REG2 0x11
184 #define TRF7970A_RAM1 0x12
185 #define TRF7970A_RAM2 0x13
186 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS 0x14
187 #define TRF7970A_NFC_LOW_FIELD_LEVEL 0x16
188 #define TRF7970A_NFCID1 0x17
189 #define TRF7970A_NFC_TARGET_LEVEL 0x18
190 #define TRF79070A_NFC_TARGET_PROTOCOL 0x19
191 #define TRF7970A_TEST_REGISTER1 0x1a
192 #define TRF7970A_TEST_REGISTER2 0x1b
193 #define TRF7970A_FIFO_STATUS 0x1c
194 #define TRF7970A_TX_LENGTH_BYTE1 0x1d
195 #define TRF7970A_TX_LENGTH_BYTE2 0x1e
196 #define TRF7970A_FIFO_IO_REGISTER 0x1f
198 /* Chip Status Control Register Bits */
199 #define TRF7970A_CHIP_STATUS_VRS5_3 BIT(0)
200 #define TRF7970A_CHIP_STATUS_REC_ON BIT(1)
201 #define TRF7970A_CHIP_STATUS_AGC_ON BIT(2)
202 #define TRF7970A_CHIP_STATUS_PM_ON BIT(3)
203 #define TRF7970A_CHIP_STATUS_RF_PWR BIT(4)
204 #define TRF7970A_CHIP_STATUS_RF_ON BIT(5)
205 #define TRF7970A_CHIP_STATUS_DIRECT BIT(6)
206 #define TRF7970A_CHIP_STATUS_STBY BIT(7)
208 /* ISO Control Register Bits */
209 #define TRF7970A_ISO_CTRL_15693_SGL_1OF4_662 0x00
210 #define TRF7970A_ISO_CTRL_15693_SGL_1OF256_662 0x01
211 #define TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648 0x02
212 #define TRF7970A_ISO_CTRL_15693_SGL_1OF256_2648 0x03
213 #define TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a 0x04
214 #define TRF7970A_ISO_CTRL_15693_DBL_1OF256_667 0x05
215 #define TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669 0x06
216 #define TRF7970A_ISO_CTRL_15693_DBL_1OF256_2669 0x07
217 #define TRF7970A_ISO_CTRL_14443A_106 0x08
218 #define TRF7970A_ISO_CTRL_14443A_212 0x09
219 #define TRF7970A_ISO_CTRL_14443A_424 0x0a
220 #define TRF7970A_ISO_CTRL_14443A_848 0x0b
221 #define TRF7970A_ISO_CTRL_14443B_106 0x0c
222 #define TRF7970A_ISO_CTRL_14443B_212 0x0d
223 #define TRF7970A_ISO_CTRL_14443B_424 0x0e
224 #define TRF7970A_ISO_CTRL_14443B_848 0x0f
225 #define TRF7970A_ISO_CTRL_FELICA_212 0x1a
226 #define TRF7970A_ISO_CTRL_FELICA_424 0x1b
227 #define TRF7970A_ISO_CTRL_RFID BIT(5)
228 #define TRF7970A_ISO_CTRL_DIR_MODE BIT(6)
229 #define TRF7970A_ISO_CTRL_RX_CRC_N BIT(7) /* true == No CRC */
231 #define TRF7970A_ISO_CTRL_RFID_SPEED_MASK 0x1f
233 /* Modulator and SYS_CLK Control Register Bits */
234 #define TRF7970A_MODULATOR_DEPTH(n) ((n) & 0x7)
235 #define TRF7970A_MODULATOR_DEPTH_ASK10 (TRF7970A_MODULATOR_DEPTH(0))
236 #define TRF7970A_MODULATOR_DEPTH_OOK (TRF7970A_MODULATOR_DEPTH(1))
237 #define TRF7970A_MODULATOR_DEPTH_ASK7 (TRF7970A_MODULATOR_DEPTH(2))
238 #define TRF7970A_MODULATOR_DEPTH_ASK8_5 (TRF7970A_MODULATOR_DEPTH(3))
239 #define TRF7970A_MODULATOR_DEPTH_ASK13 (TRF7970A_MODULATOR_DEPTH(4))
240 #define TRF7970A_MODULATOR_DEPTH_ASK16 (TRF7970A_MODULATOR_DEPTH(5))
241 #define TRF7970A_MODULATOR_DEPTH_ASK22 (TRF7970A_MODULATOR_DEPTH(6))
242 #define TRF7970A_MODULATOR_DEPTH_ASK30 (TRF7970A_MODULATOR_DEPTH(7))
243 #define TRF7970A_MODULATOR_EN_ANA BIT(3)
244 #define TRF7970A_MODULATOR_CLK(n) (((n) & 0x3) << 4)
245 #define TRF7970A_MODULATOR_CLK_DISABLED (TRF7970A_MODULATOR_CLK(0))
246 #define TRF7970A_MODULATOR_CLK_3_6 (TRF7970A_MODULATOR_CLK(1))
247 #define TRF7970A_MODULATOR_CLK_6_13 (TRF7970A_MODULATOR_CLK(2))
248 #define TRF7970A_MODULATOR_CLK_13_27 (TRF7970A_MODULATOR_CLK(3))
249 #define TRF7970A_MODULATOR_EN_OOK BIT(6)
250 #define TRF7970A_MODULATOR_27MHZ BIT(7)
252 /* IRQ Status Register Bits */
253 #define TRF7970A_IRQ_STATUS_NORESP BIT(0) /* ISO15693 only */
254 #define TRF7970A_IRQ_STATUS_COL BIT(1)
255 #define TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR BIT(2)
256 #define TRF7970A_IRQ_STATUS_PARITY_ERROR BIT(3)
257 #define TRF7970A_IRQ_STATUS_CRC_ERROR BIT(4)
258 #define TRF7970A_IRQ_STATUS_FIFO BIT(5)
259 #define TRF7970A_IRQ_STATUS_SRX BIT(6)
260 #define TRF7970A_IRQ_STATUS_TX BIT(7)
262 #define TRF7970A_IRQ_STATUS_ERROR \
263 (TRF7970A_IRQ_STATUS_COL | \
264 TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR | \
265 TRF7970A_IRQ_STATUS_PARITY_ERROR | \
266 TRF7970A_IRQ_STATUS_CRC_ERROR)
268 #define TRF7970A_SPECIAL_FCN_REG1_COL_7_6 BIT(0)
269 #define TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL BIT(1)
270 #define TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX BIT(2)
271 #define TRF7970A_SPECIAL_FCN_REG1_SP_DIR_MODE BIT(3)
272 #define TRF7970A_SPECIAL_FCN_REG1_NEXT_SLOT_37US BIT(4)
273 #define TRF7970A_SPECIAL_FCN_REG1_PAR43 BIT(5)
275 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_124 (0x0 << 2)
276 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_120 (0x1 << 2)
277 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_112 (0x2 << 2)
278 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 (0x3 << 2)
279 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_4 0x0
280 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_8 0x1
281 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_16 0x2
282 #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32 0x3
284 #define TRF7970A_FIFO_STATUS_OVERFLOW BIT(7)
286 /* NFC (ISO/IEC 14443A) Type 2 Tag commands */
287 #define NFC_T2T_CMD_READ 0x30
289 /* ISO 15693 commands codes */
290 #define ISO15693_CMD_INVENTORY 0x01
291 #define ISO15693_CMD_READ_SINGLE_BLOCK 0x20
292 #define ISO15693_CMD_WRITE_SINGLE_BLOCK 0x21
293 #define ISO15693_CMD_LOCK_BLOCK 0x22
294 #define ISO15693_CMD_READ_MULTIPLE_BLOCK 0x23
295 #define ISO15693_CMD_WRITE_MULTIPLE_BLOCK 0x24
296 #define ISO15693_CMD_SELECT 0x25
297 #define ISO15693_CMD_RESET_TO_READY 0x26
298 #define ISO15693_CMD_WRITE_AFI 0x27
299 #define ISO15693_CMD_LOCK_AFI 0x28
300 #define ISO15693_CMD_WRITE_DSFID 0x29
301 #define ISO15693_CMD_LOCK_DSFID 0x2a
302 #define ISO15693_CMD_GET_SYSTEM_INFO 0x2b
303 #define ISO15693_CMD_GET_MULTIPLE_BLOCK_SECURITY_STATUS 0x2c
305 /* ISO 15693 request and response flags */
306 #define ISO15693_REQ_FLAG_SUB_CARRIER BIT(0)
307 #define ISO15693_REQ_FLAG_DATA_RATE BIT(1)
308 #define ISO15693_REQ_FLAG_INVENTORY BIT(2)
309 #define ISO15693_REQ_FLAG_PROTOCOL_EXT BIT(3)
310 #define ISO15693_REQ_FLAG_SELECT BIT(4)
311 #define ISO15693_REQ_FLAG_AFI BIT(4)
312 #define ISO15693_REQ_FLAG_ADDRESS BIT(5)
313 #define ISO15693_REQ_FLAG_NB_SLOTS BIT(5)
314 #define ISO15693_REQ_FLAG_OPTION BIT(6)
316 #define ISO15693_REQ_FLAG_SPEED_MASK \
317 (ISO15693_REQ_FLAG_SUB_CARRIER | ISO15693_REQ_FLAG_DATA_RATE)
319 enum trf7970a_state
{
322 TRF7970A_ST_IDLE_RX_BLOCKED
,
323 TRF7970A_ST_WAIT_FOR_TX_FIFO
,
324 TRF7970A_ST_WAIT_FOR_RX_DATA
,
325 TRF7970A_ST_WAIT_FOR_RX_DATA_CONT
,
326 TRF7970A_ST_WAIT_TO_ISSUE_EOF
,
331 enum trf7970a_state state
;
333 struct spi_device
*spi
;
334 struct regulator
*regulator
;
335 struct nfc_digital_dev
*ddev
;
338 struct sk_buff
*tx_skb
;
339 struct sk_buff
*rx_skb
;
340 nfc_digital_cmd_complete_t cb
;
345 u8 modulator_sys_clk_ctrl
;
354 unsigned int timeout
;
356 struct delayed_work timeout_work
;
360 static int trf7970a_cmd(struct trf7970a
*trf
, u8 opcode
)
362 u8 cmd
= TRF7970A_CMD_BIT_CTRL
| TRF7970A_CMD_BIT_OPCODE(opcode
);
365 dev_dbg(trf
->dev
, "cmd: 0x%x\n", cmd
);
367 ret
= spi_write(trf
->spi
, &cmd
, 1);
369 dev_err(trf
->dev
, "%s - cmd: 0x%x, ret: %d\n", __func__
, cmd
,
374 static int trf7970a_read(struct trf7970a
*trf
, u8 reg
, u8
*val
)
376 u8 addr
= TRF7970A_CMD_BIT_RW
| reg
;
379 ret
= spi_write_then_read(trf
->spi
, &addr
, 1, val
, 1);
381 dev_err(trf
->dev
, "%s - addr: 0x%x, ret: %d\n", __func__
, addr
,
384 dev_dbg(trf
->dev
, "read(0x%x): 0x%x\n", addr
, *val
);
389 static int trf7970a_read_cont(struct trf7970a
*trf
, u8 reg
,
392 u8 addr
= reg
| TRF7970A_CMD_BIT_RW
| TRF7970A_CMD_BIT_CONTINUOUS
;
395 dev_dbg(trf
->dev
, "read_cont(0x%x, %zd)\n", addr
, len
);
397 ret
= spi_write_then_read(trf
->spi
, &addr
, 1, buf
, len
);
399 dev_err(trf
->dev
, "%s - addr: 0x%x, ret: %d\n", __func__
, addr
,
404 static int trf7970a_write(struct trf7970a
*trf
, u8 reg
, u8 val
)
406 u8 buf
[2] = { reg
, val
};
409 dev_dbg(trf
->dev
, "write(0x%x): 0x%x\n", reg
, val
);
411 ret
= spi_write(trf
->spi
, buf
, 2);
413 dev_err(trf
->dev
, "%s - write: 0x%x 0x%x, ret: %d\n", __func__
,
414 buf
[0], buf
[1], ret
);
419 static int trf7970a_read_irqstatus(struct trf7970a
*trf
, u8
*status
)
425 addr
= TRF7970A_IRQ_STATUS
| TRF7970A_CMD_BIT_RW
;
427 if (trf
->quirks
& TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA
) {
428 addr
|= TRF7970A_CMD_BIT_CONTINUOUS
;
429 ret
= spi_write_then_read(trf
->spi
, &addr
, 1, buf
, 2);
431 ret
= spi_write_then_read(trf
->spi
, &addr
, 1, buf
, 1);
435 dev_err(trf
->dev
, "%s - irqstatus: Status read failed: %d\n",
443 static void trf7970a_send_upstream(struct trf7970a
*trf
)
447 dev_kfree_skb_any(trf
->tx_skb
);
450 if (trf
->rx_skb
&& !IS_ERR(trf
->rx_skb
) && !trf
->aborting
)
451 print_hex_dump_debug("trf7970a rx data: ", DUMP_PREFIX_NONE
,
452 16, 1, trf
->rx_skb
->data
, trf
->rx_skb
->len
,
455 /* According to the manual it is "good form" to reset the fifo and
456 * read the RSSI levels & oscillator status register here. It doesn't
459 trf7970a_cmd(trf
, TRF7970A_CMD_FIFO_RESET
);
460 trf7970a_read(trf
, TRF7970A_RSSI_OSC_STATUS
, &rssi
);
462 trf
->state
= TRF7970A_ST_IDLE
;
465 dev_dbg(trf
->dev
, "Abort process complete\n");
467 if (!IS_ERR(trf
->rx_skb
)) {
468 kfree_skb(trf
->rx_skb
);
469 trf
->rx_skb
= ERR_PTR(-ECANCELED
);
472 trf
->aborting
= false;
475 trf
->cb(trf
->ddev
, trf
->cb_arg
, trf
->rx_skb
);
480 static void trf7970a_send_err_upstream(struct trf7970a
*trf
, int errno
)
482 dev_dbg(trf
->dev
, "Error - state: %d, errno: %d\n", trf
->state
, errno
);
484 kfree_skb(trf
->rx_skb
);
485 trf
->rx_skb
= ERR_PTR(errno
);
487 trf7970a_send_upstream(trf
);
490 static int trf7970a_transmit(struct trf7970a
*trf
, struct sk_buff
*skb
,
493 unsigned int timeout
;
496 print_hex_dump_debug("trf7970a tx data: ", DUMP_PREFIX_NONE
,
497 16, 1, skb
->data
, len
, false);
499 ret
= spi_write(trf
->spi
, skb
->data
, len
);
501 dev_err(trf
->dev
, "%s - Can't send tx data: %d\n", __func__
,
509 trf
->state
= TRF7970A_ST_WAIT_FOR_TX_FIFO
;
510 timeout
= TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT
;
512 if (trf
->issue_eof
) {
513 trf
->state
= TRF7970A_ST_WAIT_TO_ISSUE_EOF
;
514 timeout
= TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF
;
516 trf
->state
= TRF7970A_ST_WAIT_FOR_RX_DATA
;
517 timeout
= trf
->timeout
;
521 dev_dbg(trf
->dev
, "Setting timeout for %d ms, state: %d\n", timeout
,
524 schedule_delayed_work(&trf
->timeout_work
, msecs_to_jiffies(timeout
));
529 static void trf7970a_fill_fifo(struct trf7970a
*trf
)
531 struct sk_buff
*skb
= trf
->tx_skb
;
536 ret
= trf7970a_read(trf
, TRF7970A_FIFO_STATUS
, &fifo_bytes
);
538 trf7970a_send_err_upstream(trf
, ret
);
542 dev_dbg(trf
->dev
, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes
);
544 if (fifo_bytes
& TRF7970A_FIFO_STATUS_OVERFLOW
) {
545 dev_err(trf
->dev
, "%s - fifo overflow: 0x%x\n", __func__
,
547 trf7970a_send_err_upstream(trf
, -EIO
);
551 /* Calculate how much more data can be written to the fifo */
552 len
= TRF7970A_FIFO_SIZE
- fifo_bytes
;
553 len
= min(skb
->len
, len
);
555 ret
= trf7970a_transmit(trf
, skb
, len
);
557 trf7970a_send_err_upstream(trf
, ret
);
560 static void trf7970a_drain_fifo(struct trf7970a
*trf
, u8 status
)
562 struct sk_buff
*skb
= trf
->rx_skb
;
566 if (status
& TRF7970A_IRQ_STATUS_ERROR
) {
567 trf7970a_send_err_upstream(trf
, -EIO
);
571 ret
= trf7970a_read(trf
, TRF7970A_FIFO_STATUS
, &fifo_bytes
);
573 trf7970a_send_err_upstream(trf
, ret
);
577 dev_dbg(trf
->dev
, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes
);
582 if (fifo_bytes
& TRF7970A_FIFO_STATUS_OVERFLOW
) {
583 dev_err(trf
->dev
, "%s - fifo overflow: 0x%x\n", __func__
,
585 trf7970a_send_err_upstream(trf
, -EIO
);
589 if (fifo_bytes
> skb_tailroom(skb
)) {
590 skb
= skb_copy_expand(skb
, skb_headroom(skb
),
591 max_t(int, fifo_bytes
,
592 TRF7970A_RX_SKB_ALLOC_SIZE
),
595 trf7970a_send_err_upstream(trf
, -ENOMEM
);
599 kfree_skb(trf
->rx_skb
);
603 ret
= trf7970a_read_cont(trf
, TRF7970A_FIFO_IO_REGISTER
,
604 skb_put(skb
, fifo_bytes
), fifo_bytes
);
606 trf7970a_send_err_upstream(trf
, ret
);
610 /* If received Type 2 ACK/NACK, shift right 4 bits and pass up */
611 if ((trf
->framing
== NFC_DIGITAL_FRAMING_NFCA_T2T
) && (skb
->len
== 1) &&
612 (trf
->special_fcn_reg1
==
613 TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX
)) {
615 status
= TRF7970A_IRQ_STATUS_SRX
;
617 trf
->state
= TRF7970A_ST_WAIT_FOR_RX_DATA_CONT
;
621 if (status
== TRF7970A_IRQ_STATUS_SRX
) { /* Receive complete */
622 trf7970a_send_upstream(trf
);
626 dev_dbg(trf
->dev
, "Setting timeout for %d ms\n",
627 TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT
);
629 schedule_delayed_work(&trf
->timeout_work
,
630 msecs_to_jiffies(TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT
));
633 static irqreturn_t
trf7970a_irq(int irq
, void *dev_id
)
635 struct trf7970a
*trf
= dev_id
;
639 mutex_lock(&trf
->lock
);
641 if (trf
->state
== TRF7970A_ST_OFF
) {
642 mutex_unlock(&trf
->lock
);
646 ret
= trf7970a_read_irqstatus(trf
, &status
);
648 mutex_unlock(&trf
->lock
);
652 dev_dbg(trf
->dev
, "IRQ - state: %d, status: 0x%x\n", trf
->state
,
656 mutex_unlock(&trf
->lock
);
660 switch (trf
->state
) {
661 case TRF7970A_ST_IDLE
:
662 case TRF7970A_ST_IDLE_RX_BLOCKED
:
663 /* If getting interrupts caused by RF noise, turn off the
664 * receiver to avoid unnecessary interrupts. It will be
665 * turned back on in trf7970a_in_send_cmd() when the next
668 if (status
& TRF7970A_IRQ_STATUS_ERROR
) {
669 trf7970a_cmd(trf
, TRF7970A_CMD_BLOCK_RX
);
670 trf
->state
= TRF7970A_ST_IDLE_RX_BLOCKED
;
673 trf7970a_cmd(trf
, TRF7970A_CMD_FIFO_RESET
);
675 case TRF7970A_ST_WAIT_FOR_TX_FIFO
:
676 if (status
& TRF7970A_IRQ_STATUS_TX
) {
677 trf
->ignore_timeout
=
678 !cancel_delayed_work(&trf
->timeout_work
);
679 trf7970a_fill_fifo(trf
);
681 trf7970a_send_err_upstream(trf
, -EIO
);
684 case TRF7970A_ST_WAIT_FOR_RX_DATA
:
685 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT
:
686 if (status
& TRF7970A_IRQ_STATUS_SRX
) {
687 trf
->ignore_timeout
=
688 !cancel_delayed_work(&trf
->timeout_work
);
689 trf7970a_drain_fifo(trf
, status
);
690 } else if (status
== TRF7970A_IRQ_STATUS_TX
) {
691 trf7970a_cmd(trf
, TRF7970A_CMD_FIFO_RESET
);
693 trf7970a_send_err_upstream(trf
, -EIO
);
696 case TRF7970A_ST_WAIT_TO_ISSUE_EOF
:
697 if (status
!= TRF7970A_IRQ_STATUS_TX
)
698 trf7970a_send_err_upstream(trf
, -EIO
);
701 dev_err(trf
->dev
, "%s - Driver in invalid state: %d\n",
702 __func__
, trf
->state
);
705 mutex_unlock(&trf
->lock
);
709 static void trf7970a_issue_eof(struct trf7970a
*trf
)
713 dev_dbg(trf
->dev
, "Issuing EOF\n");
715 ret
= trf7970a_cmd(trf
, TRF7970A_CMD_FIFO_RESET
);
717 trf7970a_send_err_upstream(trf
, ret
);
719 ret
= trf7970a_cmd(trf
, TRF7970A_CMD_EOF
);
721 trf7970a_send_err_upstream(trf
, ret
);
723 trf
->state
= TRF7970A_ST_WAIT_FOR_RX_DATA
;
725 dev_dbg(trf
->dev
, "Setting timeout for %d ms, state: %d\n",
726 trf
->timeout
, trf
->state
);
728 schedule_delayed_work(&trf
->timeout_work
,
729 msecs_to_jiffies(trf
->timeout
));
732 static void trf7970a_timeout_work_handler(struct work_struct
*work
)
734 struct trf7970a
*trf
= container_of(work
, struct trf7970a
,
737 dev_dbg(trf
->dev
, "Timeout - state: %d, ignore_timeout: %d\n",
738 trf
->state
, trf
->ignore_timeout
);
740 mutex_lock(&trf
->lock
);
742 if (trf
->ignore_timeout
)
743 trf
->ignore_timeout
= false;
744 else if (trf
->state
== TRF7970A_ST_WAIT_FOR_RX_DATA_CONT
)
745 trf7970a_send_upstream(trf
); /* No more rx data so send up */
746 else if (trf
->state
== TRF7970A_ST_WAIT_TO_ISSUE_EOF
)
747 trf7970a_issue_eof(trf
);
749 trf7970a_send_err_upstream(trf
, -ETIMEDOUT
);
751 mutex_unlock(&trf
->lock
);
754 static int trf7970a_init(struct trf7970a
*trf
)
758 dev_dbg(trf
->dev
, "Initializing device - state: %d\n", trf
->state
);
760 ret
= trf7970a_cmd(trf
, TRF7970A_CMD_SOFT_INIT
);
764 ret
= trf7970a_cmd(trf
, TRF7970A_CMD_IDLE
);
768 /* Must clear NFC Target Detection Level reg due to erratum */
769 ret
= trf7970a_write(trf
, TRF7970A_NFC_TARGET_LEVEL
, 0);
773 ret
= trf7970a_write(trf
, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS
,
774 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96
|
775 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32
);
779 ret
= trf7970a_write(trf
, TRF7970A_SPECIAL_FCN_REG1
, 0);
783 trf
->special_fcn_reg1
= 0;
785 trf
->iso_ctrl
= 0xff;
789 dev_dbg(trf
->dev
, "Couldn't init device: %d\n", ret
);
793 static void trf7970a_switch_rf_off(struct trf7970a
*trf
)
795 dev_dbg(trf
->dev
, "Switching rf off\n");
797 trf
->chip_status_ctrl
&= ~TRF7970A_CHIP_STATUS_RF_ON
;
799 trf7970a_write(trf
, TRF7970A_CHIP_STATUS_CTRL
, trf
->chip_status_ctrl
);
801 trf
->aborting
= false;
802 trf
->state
= TRF7970A_ST_OFF
;
804 pm_runtime_mark_last_busy(trf
->dev
);
805 pm_runtime_put_autosuspend(trf
->dev
);
808 static void trf7970a_switch_rf_on(struct trf7970a
*trf
)
810 dev_dbg(trf
->dev
, "Switching rf on\n");
812 pm_runtime_get_sync(trf
->dev
);
814 trf
->state
= TRF7970A_ST_IDLE
;
817 static int trf7970a_switch_rf(struct nfc_digital_dev
*ddev
, bool on
)
819 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
821 dev_dbg(trf
->dev
, "Switching RF - state: %d, on: %d\n", trf
->state
, on
);
823 mutex_lock(&trf
->lock
);
826 switch (trf
->state
) {
827 case TRF7970A_ST_OFF
:
828 trf7970a_switch_rf_on(trf
);
830 case TRF7970A_ST_IDLE
:
831 case TRF7970A_ST_IDLE_RX_BLOCKED
:
834 dev_err(trf
->dev
, "%s - Invalid request: %d %d\n",
835 __func__
, trf
->state
, on
);
836 trf7970a_switch_rf_off(trf
);
839 switch (trf
->state
) {
840 case TRF7970A_ST_OFF
:
843 dev_err(trf
->dev
, "%s - Invalid request: %d %d\n",
844 __func__
, trf
->state
, on
);
846 case TRF7970A_ST_IDLE
:
847 case TRF7970A_ST_IDLE_RX_BLOCKED
:
848 trf7970a_switch_rf_off(trf
);
852 mutex_unlock(&trf
->lock
);
856 static int trf7970a_config_rf_tech(struct trf7970a
*trf
, int tech
)
860 dev_dbg(trf
->dev
, "rf technology: %d\n", tech
);
863 case NFC_DIGITAL_RF_TECH_106A
:
864 trf
->iso_ctrl_tech
= TRF7970A_ISO_CTRL_14443A_106
;
865 trf
->modulator_sys_clk_ctrl
= TRF7970A_MODULATOR_DEPTH_OOK
;
867 case NFC_DIGITAL_RF_TECH_106B
:
868 trf
->iso_ctrl_tech
= TRF7970A_ISO_CTRL_14443B_106
;
869 trf
->modulator_sys_clk_ctrl
= TRF7970A_MODULATOR_DEPTH_ASK10
;
871 case NFC_DIGITAL_RF_TECH_212F
:
872 trf
->iso_ctrl_tech
= TRF7970A_ISO_CTRL_FELICA_212
;
873 trf
->modulator_sys_clk_ctrl
= TRF7970A_MODULATOR_DEPTH_ASK10
;
875 case NFC_DIGITAL_RF_TECH_424F
:
876 trf
->iso_ctrl_tech
= TRF7970A_ISO_CTRL_FELICA_424
;
877 trf
->modulator_sys_clk_ctrl
= TRF7970A_MODULATOR_DEPTH_ASK10
;
879 case NFC_DIGITAL_RF_TECH_ISO15693
:
880 trf
->iso_ctrl_tech
= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648
;
881 trf
->modulator_sys_clk_ctrl
= TRF7970A_MODULATOR_DEPTH_OOK
;
884 dev_dbg(trf
->dev
, "Unsupported rf technology: %d\n", tech
);
888 trf
->technology
= tech
;
893 static int trf7970a_config_framing(struct trf7970a
*trf
, int framing
)
895 u8 iso_ctrl
= trf
->iso_ctrl_tech
;
898 dev_dbg(trf
->dev
, "framing: %d\n", framing
);
901 case NFC_DIGITAL_FRAMING_NFCA_SHORT
:
902 case NFC_DIGITAL_FRAMING_NFCA_STANDARD
:
903 trf
->tx_cmd
= TRF7970A_CMD_TRANSMIT_NO_CRC
;
904 iso_ctrl
|= TRF7970A_ISO_CTRL_RX_CRC_N
;
906 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A
:
907 case NFC_DIGITAL_FRAMING_NFCA_T4T
:
908 case NFC_DIGITAL_FRAMING_NFCB
:
909 case NFC_DIGITAL_FRAMING_NFCB_T4T
:
910 case NFC_DIGITAL_FRAMING_NFCF
:
911 case NFC_DIGITAL_FRAMING_NFCF_T3T
:
912 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY
:
913 case NFC_DIGITAL_FRAMING_ISO15693_T5T
:
914 trf
->tx_cmd
= TRF7970A_CMD_TRANSMIT
;
915 iso_ctrl
&= ~TRF7970A_ISO_CTRL_RX_CRC_N
;
917 case NFC_DIGITAL_FRAMING_NFCA_T2T
:
918 trf
->tx_cmd
= TRF7970A_CMD_TRANSMIT
;
919 iso_ctrl
|= TRF7970A_ISO_CTRL_RX_CRC_N
;
922 dev_dbg(trf
->dev
, "Unsupported Framing: %d\n", framing
);
926 trf
->framing
= framing
;
928 if (iso_ctrl
!= trf
->iso_ctrl
) {
929 ret
= trf7970a_write(trf
, TRF7970A_ISO_CTRL
, iso_ctrl
);
933 trf
->iso_ctrl
= iso_ctrl
;
935 ret
= trf7970a_write(trf
, TRF7970A_MODULATOR_SYS_CLK_CTRL
,
936 trf
->modulator_sys_clk_ctrl
);
941 if (!(trf
->chip_status_ctrl
& TRF7970A_CHIP_STATUS_RF_ON
)) {
942 ret
= trf7970a_write(trf
, TRF7970A_CHIP_STATUS_CTRL
,
943 trf
->chip_status_ctrl
|
944 TRF7970A_CHIP_STATUS_RF_ON
);
948 trf
->chip_status_ctrl
|= TRF7970A_CHIP_STATUS_RF_ON
;
950 usleep_range(5000, 6000);
956 static int trf7970a_in_configure_hw(struct nfc_digital_dev
*ddev
, int type
,
959 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
962 dev_dbg(trf
->dev
, "Configure hw - type: %d, param: %d\n", type
, param
);
964 mutex_lock(&trf
->lock
);
966 if (trf
->state
== TRF7970A_ST_OFF
)
967 trf7970a_switch_rf_on(trf
);
970 case NFC_DIGITAL_CONFIG_RF_TECH
:
971 ret
= trf7970a_config_rf_tech(trf
, param
);
973 case NFC_DIGITAL_CONFIG_FRAMING
:
974 ret
= trf7970a_config_framing(trf
, param
);
977 dev_dbg(trf
->dev
, "Unknown type: %d\n", type
);
981 mutex_unlock(&trf
->lock
);
985 static int trf7970a_is_iso15693_write_or_lock(u8 cmd
)
988 case ISO15693_CMD_WRITE_SINGLE_BLOCK
:
989 case ISO15693_CMD_LOCK_BLOCK
:
990 case ISO15693_CMD_WRITE_MULTIPLE_BLOCK
:
991 case ISO15693_CMD_WRITE_AFI
:
992 case ISO15693_CMD_LOCK_AFI
:
993 case ISO15693_CMD_WRITE_DSFID
:
994 case ISO15693_CMD_LOCK_DSFID
:
1002 static int trf7970a_per_cmd_config(struct trf7970a
*trf
, struct sk_buff
*skb
)
1004 u8
*req
= skb
->data
;
1005 u8 special_fcn_reg1
, iso_ctrl
;
1008 trf
->issue_eof
= false;
1010 /* When issuing Type 2 read command, make sure the '4_bit_RX' bit in
1011 * special functions register 1 is cleared; otherwise, its a write or
1012 * sector select command and '4_bit_RX' must be set.
1014 * When issuing an ISO 15693 command, inspect the flags byte to see
1015 * what speed to use. Also, remember if the OPTION flag is set on
1016 * a Type 5 write or lock command so the driver will know that it
1017 * has to send an EOF in order to get a response.
1019 if ((trf
->technology
== NFC_DIGITAL_RF_TECH_106A
) &&
1020 (trf
->framing
== NFC_DIGITAL_FRAMING_NFCA_T2T
)) {
1021 if (req
[0] == NFC_T2T_CMD_READ
)
1022 special_fcn_reg1
= 0;
1024 special_fcn_reg1
= TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX
;
1026 if (special_fcn_reg1
!= trf
->special_fcn_reg1
) {
1027 ret
= trf7970a_write(trf
, TRF7970A_SPECIAL_FCN_REG1
,
1032 trf
->special_fcn_reg1
= special_fcn_reg1
;
1034 } else if (trf
->technology
== NFC_DIGITAL_RF_TECH_ISO15693
) {
1035 iso_ctrl
= trf
->iso_ctrl
& ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK
;
1037 switch (req
[0] & ISO15693_REQ_FLAG_SPEED_MASK
) {
1039 iso_ctrl
|= TRF7970A_ISO_CTRL_15693_SGL_1OF4_662
;
1041 case ISO15693_REQ_FLAG_SUB_CARRIER
:
1042 iso_ctrl
|= TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a
;
1044 case ISO15693_REQ_FLAG_DATA_RATE
:
1045 iso_ctrl
|= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648
;
1047 case (ISO15693_REQ_FLAG_SUB_CARRIER
|
1048 ISO15693_REQ_FLAG_DATA_RATE
):
1049 iso_ctrl
|= TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669
;
1053 if (iso_ctrl
!= trf
->iso_ctrl
) {
1054 ret
= trf7970a_write(trf
, TRF7970A_ISO_CTRL
, iso_ctrl
);
1058 trf
->iso_ctrl
= iso_ctrl
;
1061 if ((trf
->framing
== NFC_DIGITAL_FRAMING_ISO15693_T5T
) &&
1062 trf7970a_is_iso15693_write_or_lock(req
[1]) &&
1063 (req
[0] & ISO15693_REQ_FLAG_OPTION
))
1064 trf
->issue_eof
= true;
1070 static int trf7970a_in_send_cmd(struct nfc_digital_dev
*ddev
,
1071 struct sk_buff
*skb
, u16 timeout
,
1072 nfc_digital_cmd_complete_t cb
, void *arg
)
1074 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
1079 dev_dbg(trf
->dev
, "New request - state: %d, timeout: %d ms, len: %d\n",
1080 trf
->state
, timeout
, skb
->len
);
1082 if (skb
->len
> TRF7970A_TX_MAX
)
1085 mutex_lock(&trf
->lock
);
1087 if ((trf
->state
!= TRF7970A_ST_IDLE
) &&
1088 (trf
->state
!= TRF7970A_ST_IDLE_RX_BLOCKED
)) {
1089 dev_err(trf
->dev
, "%s - Bogus state: %d\n", __func__
,
1095 if (trf
->aborting
) {
1096 dev_dbg(trf
->dev
, "Abort process complete\n");
1097 trf
->aborting
= false;
1102 trf
->rx_skb
= nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE
,
1105 dev_dbg(trf
->dev
, "Can't alloc rx_skb\n");
1110 if (trf
->state
== TRF7970A_ST_IDLE_RX_BLOCKED
) {
1111 ret
= trf7970a_cmd(trf
, TRF7970A_CMD_ENABLE_RX
);
1115 trf
->state
= TRF7970A_ST_IDLE
;
1118 ret
= trf7970a_per_cmd_config(trf
, skb
);
1126 trf
->timeout
= timeout
;
1127 trf
->ignore_timeout
= false;
1130 prefix
= skb_push(skb
, TRF7970A_TX_SKB_HEADROOM
);
1132 /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
1133 * on what the current framing is, the address of the TX length byte 1
1134 * register (0x1d), and the 2 byte length of the data to be transmitted.
1136 prefix
[0] = TRF7970A_CMD_BIT_CTRL
|
1137 TRF7970A_CMD_BIT_OPCODE(TRF7970A_CMD_FIFO_RESET
);
1138 prefix
[1] = TRF7970A_CMD_BIT_CTRL
|
1139 TRF7970A_CMD_BIT_OPCODE(trf
->tx_cmd
);
1140 prefix
[2] = TRF7970A_CMD_BIT_CONTINUOUS
| TRF7970A_TX_LENGTH_BYTE1
;
1142 if (trf
->framing
== NFC_DIGITAL_FRAMING_NFCA_SHORT
) {
1144 prefix
[4] = 0x0f; /* 7 bits */
1146 prefix
[3] = (len
& 0xf00) >> 4;
1147 prefix
[3] |= ((len
& 0xf0) >> 4);
1148 prefix
[4] = ((len
& 0x0f) << 4);
1151 len
= min_t(int, skb
->len
, TRF7970A_FIFO_SIZE
);
1153 usleep_range(1000, 2000);
1155 ret
= trf7970a_transmit(trf
, skb
, len
);
1157 kfree_skb(trf
->rx_skb
);
1162 mutex_unlock(&trf
->lock
);
1166 static int trf7970a_tg_configure_hw(struct nfc_digital_dev
*ddev
,
1167 int type
, int param
)
1169 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
1171 dev_dbg(trf
->dev
, "Unsupported interface\n");
1176 static int trf7970a_tg_send_cmd(struct nfc_digital_dev
*ddev
,
1177 struct sk_buff
*skb
, u16 timeout
,
1178 nfc_digital_cmd_complete_t cb
, void *arg
)
1180 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
1182 dev_dbg(trf
->dev
, "Unsupported interface\n");
1187 static int trf7970a_tg_listen(struct nfc_digital_dev
*ddev
,
1188 u16 timeout
, nfc_digital_cmd_complete_t cb
, void *arg
)
1190 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
1192 dev_dbg(trf
->dev
, "Unsupported interface\n");
1197 static int trf7970a_tg_listen_mdaa(struct nfc_digital_dev
*ddev
,
1198 struct digital_tg_mdaa_params
*mdaa_params
,
1199 u16 timeout
, nfc_digital_cmd_complete_t cb
, void *arg
)
1201 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
1203 dev_dbg(trf
->dev
, "Unsupported interface\n");
1208 static void trf7970a_abort_cmd(struct nfc_digital_dev
*ddev
)
1210 struct trf7970a
*trf
= nfc_digital_get_drvdata(ddev
);
1212 dev_dbg(trf
->dev
, "Abort process initiated\n");
1214 mutex_lock(&trf
->lock
);
1216 switch (trf
->state
) {
1217 case TRF7970A_ST_WAIT_FOR_TX_FIFO
:
1218 case TRF7970A_ST_WAIT_FOR_RX_DATA
:
1219 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT
:
1220 case TRF7970A_ST_WAIT_TO_ISSUE_EOF
:
1221 trf
->aborting
= true;
1227 mutex_unlock(&trf
->lock
);
1230 static struct nfc_digital_ops trf7970a_nfc_ops
= {
1231 .in_configure_hw
= trf7970a_in_configure_hw
,
1232 .in_send_cmd
= trf7970a_in_send_cmd
,
1233 .tg_configure_hw
= trf7970a_tg_configure_hw
,
1234 .tg_send_cmd
= trf7970a_tg_send_cmd
,
1235 .tg_listen
= trf7970a_tg_listen
,
1236 .tg_listen_mdaa
= trf7970a_tg_listen_mdaa
,
1237 .switch_rf
= trf7970a_switch_rf
,
1238 .abort_cmd
= trf7970a_abort_cmd
,
1241 static int trf7970a_get_autosuspend_delay(struct device_node
*np
)
1243 int autosuspend_delay
, ret
;
1245 ret
= of_property_read_u32(np
, "autosuspend-delay", &autosuspend_delay
);
1247 autosuspend_delay
= TRF7970A_AUTOSUSPEND_DELAY
;
1251 return autosuspend_delay
;
1254 static int trf7970a_probe(struct spi_device
*spi
)
1256 struct device_node
*np
= spi
->dev
.of_node
;
1257 const struct spi_device_id
*id
= spi_get_device_id(spi
);
1258 struct trf7970a
*trf
;
1259 int uvolts
, autosuspend_delay
, ret
;
1262 dev_err(&spi
->dev
, "No Device Tree entry\n");
1266 trf
= devm_kzalloc(&spi
->dev
, sizeof(*trf
), GFP_KERNEL
);
1270 trf
->state
= TRF7970A_ST_OFF
;
1271 trf
->dev
= &spi
->dev
;
1273 trf
->quirks
= id
->driver_data
;
1275 spi
->mode
= SPI_MODE_1
;
1276 spi
->bits_per_word
= 8;
1278 /* There are two enable pins - both must be present */
1279 trf
->en_gpio
= of_get_named_gpio(np
, "ti,enable-gpios", 0);
1280 if (!gpio_is_valid(trf
->en_gpio
)) {
1281 dev_err(trf
->dev
, "No EN GPIO property\n");
1282 return trf
->en_gpio
;
1285 ret
= devm_gpio_request_one(trf
->dev
, trf
->en_gpio
,
1286 GPIOF_DIR_OUT
| GPIOF_INIT_LOW
, "EN");
1288 dev_err(trf
->dev
, "Can't request EN GPIO: %d\n", ret
);
1292 trf
->en2_gpio
= of_get_named_gpio(np
, "ti,enable-gpios", 1);
1293 if (!gpio_is_valid(trf
->en2_gpio
)) {
1294 dev_err(trf
->dev
, "No EN2 GPIO property\n");
1295 return trf
->en2_gpio
;
1298 ret
= devm_gpio_request_one(trf
->dev
, trf
->en2_gpio
,
1299 GPIOF_DIR_OUT
| GPIOF_INIT_LOW
, "EN2");
1301 dev_err(trf
->dev
, "Can't request EN2 GPIO: %d\n", ret
);
1305 ret
= devm_request_threaded_irq(trf
->dev
, spi
->irq
, NULL
,
1306 trf7970a_irq
, IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
1309 dev_err(trf
->dev
, "Can't request IRQ#%d: %d\n", spi
->irq
, ret
);
1313 mutex_init(&trf
->lock
);
1314 INIT_DELAYED_WORK(&trf
->timeout_work
, trf7970a_timeout_work_handler
);
1316 trf
->regulator
= devm_regulator_get(&spi
->dev
, "vin");
1317 if (IS_ERR(trf
->regulator
)) {
1318 ret
= PTR_ERR(trf
->regulator
);
1319 dev_err(trf
->dev
, "Can't get VIN regulator: %d\n", ret
);
1320 goto err_destroy_lock
;
1323 ret
= regulator_enable(trf
->regulator
);
1325 dev_err(trf
->dev
, "Can't enable VIN: %d\n", ret
);
1326 goto err_destroy_lock
;
1329 uvolts
= regulator_get_voltage(trf
->regulator
);
1331 if (uvolts
> 4000000)
1332 trf
->chip_status_ctrl
= TRF7970A_CHIP_STATUS_VRS5_3
;
1334 trf
->ddev
= nfc_digital_allocate_device(&trf7970a_nfc_ops
,
1335 TRF7970A_SUPPORTED_PROTOCOLS
,
1336 NFC_DIGITAL_DRV_CAPS_IN_CRC
, TRF7970A_TX_SKB_HEADROOM
,
1339 dev_err(trf
->dev
, "Can't allocate NFC digital device\n");
1341 goto err_disable_regulator
;
1344 nfc_digital_set_parent_dev(trf
->ddev
, trf
->dev
);
1345 nfc_digital_set_drvdata(trf
->ddev
, trf
);
1346 spi_set_drvdata(spi
, trf
);
1348 autosuspend_delay
= trf7970a_get_autosuspend_delay(np
);
1350 pm_runtime_set_autosuspend_delay(trf
->dev
, autosuspend_delay
);
1351 pm_runtime_use_autosuspend(trf
->dev
);
1352 pm_runtime_enable(trf
->dev
);
1354 ret
= nfc_digital_register_device(trf
->ddev
);
1356 dev_err(trf
->dev
, "Can't register NFC digital device: %d\n",
1364 pm_runtime_disable(trf
->dev
);
1365 nfc_digital_free_device(trf
->ddev
);
1366 err_disable_regulator
:
1367 regulator_disable(trf
->regulator
);
1369 mutex_destroy(&trf
->lock
);
1373 static int trf7970a_remove(struct spi_device
*spi
)
1375 struct trf7970a
*trf
= spi_get_drvdata(spi
);
1377 mutex_lock(&trf
->lock
);
1379 switch (trf
->state
) {
1380 case TRF7970A_ST_WAIT_FOR_TX_FIFO
:
1381 case TRF7970A_ST_WAIT_FOR_RX_DATA
:
1382 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT
:
1383 case TRF7970A_ST_WAIT_TO_ISSUE_EOF
:
1384 trf7970a_send_err_upstream(trf
, -ECANCELED
);
1386 case TRF7970A_ST_IDLE
:
1387 case TRF7970A_ST_IDLE_RX_BLOCKED
:
1388 pm_runtime_put_sync(trf
->dev
);
1394 mutex_unlock(&trf
->lock
);
1396 pm_runtime_disable(trf
->dev
);
1398 nfc_digital_unregister_device(trf
->ddev
);
1399 nfc_digital_free_device(trf
->ddev
);
1401 regulator_disable(trf
->regulator
);
1403 mutex_destroy(&trf
->lock
);
1408 #ifdef CONFIG_PM_RUNTIME
1409 static int trf7970a_pm_runtime_suspend(struct device
*dev
)
1411 struct spi_device
*spi
= container_of(dev
, struct spi_device
, dev
);
1412 struct trf7970a
*trf
= spi_get_drvdata(spi
);
1415 dev_dbg(dev
, "Runtime suspend\n");
1417 if (trf
->state
!= TRF7970A_ST_OFF
) {
1418 dev_dbg(dev
, "Can't suspend - not in OFF state (%d)\n",
1423 gpio_set_value(trf
->en_gpio
, 0);
1424 gpio_set_value(trf
->en2_gpio
, 0);
1426 ret
= regulator_disable(trf
->regulator
);
1428 dev_err(dev
, "%s - Can't disable VIN: %d\n", __func__
, ret
);
1433 static int trf7970a_pm_runtime_resume(struct device
*dev
)
1435 struct spi_device
*spi
= container_of(dev
, struct spi_device
, dev
);
1436 struct trf7970a
*trf
= spi_get_drvdata(spi
);
1439 dev_dbg(dev
, "Runtime resume\n");
1441 ret
= regulator_enable(trf
->regulator
);
1443 dev_err(dev
, "%s - Can't enable VIN: %d\n", __func__
, ret
);
1447 usleep_range(5000, 6000);
1449 gpio_set_value(trf
->en2_gpio
, 1);
1450 usleep_range(1000, 2000);
1451 gpio_set_value(trf
->en_gpio
, 1);
1453 usleep_range(20000, 21000);
1455 ret
= trf7970a_init(trf
);
1457 dev_err(dev
, "%s - Can't initialize: %d\n", __func__
, ret
);
1461 pm_runtime_mark_last_busy(dev
);
1467 static const struct dev_pm_ops trf7970a_pm_ops
= {
1468 SET_RUNTIME_PM_OPS(trf7970a_pm_runtime_suspend
,
1469 trf7970a_pm_runtime_resume
, NULL
)
1472 static const struct spi_device_id trf7970a_id_table
[] = {
1473 { "trf7970a", TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA
},
1476 MODULE_DEVICE_TABLE(spi
, trf7970a_id_table
);
1478 static struct spi_driver trf7970a_spi_driver
= {
1479 .probe
= trf7970a_probe
,
1480 .remove
= trf7970a_remove
,
1481 .id_table
= trf7970a_id_table
,
1484 .owner
= THIS_MODULE
,
1485 .pm
= &trf7970a_pm_ops
,
1489 module_spi_driver(trf7970a_spi_driver
);
1491 MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>");
1492 MODULE_LICENSE("GPL v2");
1493 MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver");