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_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 * 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.
93 * Type 2 write and sector select commands respond with a 4-bit ACK or NACK.
94 * Having only 4 bits in the FIFO won't normally generate an interrupt so
95 * driver enables the '4_bit_RX' bit of the Special Functions register 1
96 * to cause an interrupt in that case. Leaving that bit for a read command
97 * messes up the data returned so it is only enabled when the framing is
98 * 'NFC_DIGITAL_FRAMING_NFCA_T2T' and the command is not a read command.
99 * Unfortunately, that means that the driver has to peek into tx frames
100 * when the framing is 'NFC_DIGITAL_FRAMING_NFCA_T2T'. This is done by
101 * the trf7970a_per_cmd_config() routine.
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.
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
115 * enough so 20 ms is used. So the timer is set to 40 ms - 20 ms to drain
116 * up to 127 bytes in the FIFO at the lowest bit rate plus another 20 ms to
117 * ensure the wait is long enough before sending the EOF. This seems to work
121 #define TRF7970A_SUPPORTED_PROTOCOLS \
122 (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \
123 NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_FELICA_MASK | \
124 NFC_PROTO_ISO15693_MASK | NFC_PROTO_NFC_DEP_MASK)
126 #define TRF7970A_AUTOSUSPEND_DELAY 30000 /* 30 seconds */
128 #define TRF7970A_RX_SKB_ALLOC_SIZE 256
130 #define TRF7970A_FIFO_SIZE 127
132 /* TX length is 3 nibbles long ==> 4KB - 1 bytes max */
133 #define TRF7970A_TX_MAX (4096 - 1)
135 #define TRF7970A_WAIT_FOR_TX_IRQ 20
136 #define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 20
137 #define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 20
138 #define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 40
140 /* Guard times for various RF technologies (in us) */
141 #define TRF7970A_GUARD_TIME_NFCA 5000
142 #define TRF7970A_GUARD_TIME_NFCB 5000
143 #define TRF7970A_GUARD_TIME_NFCF 20000
144 #define TRF7970A_GUARD_TIME_15693 1000
147 /* Erratum: When reading IRQ Status register on trf7970a, we must issue a
148 * read continuous command for IRQ Status and Collision Position registers.
150 #define TRF7970A_QUIRK_IRQ_STATUS_READ BIT(0)
151 #define TRF7970A_QUIRK_EN2_MUST_STAY_LOW BIT(1)
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
168 #define TRF7970A_CMD_TEST_INT_RF 0x18
169 #define TRF7970A_CMD_TEST_EXT_RF 0x19
170 #define TRF7970A_CMD_RX_GAIN_ADJUST 0x1a
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.
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)
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
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)
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
243 #define TRF7970A_ISO_CTRL_NFC_NFCA_106 0x01
244 #define TRF7970A_ISO_CTRL_NFC_NFCF_212 0x02
245 #define TRF7970A_ISO_CTRL_NFC_NFCF_424 0x03
246 #define TRF7970A_ISO_CTRL_NFC_CE_14443A 0x00
247 #define TRF7970A_ISO_CTRL_NFC_CE_14443B 0x01
248 #define TRF7970A_ISO_CTRL_NFC_CE BIT(2)
249 #define TRF7970A_ISO_CTRL_NFC_ACTIVE BIT(3)
250 #define TRF7970A_ISO_CTRL_NFC_INITIATOR BIT(4)
251 #define TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE BIT(5)
252 #define TRF7970A_ISO_CTRL_RFID BIT(5)
253 #define TRF7970A_ISO_CTRL_DIR_MODE BIT(6)
254 #define TRF7970A_ISO_CTRL_RX_CRC_N BIT(7) /* true == No CRC */
256 #define TRF7970A_ISO_CTRL_RFID_SPEED_MASK 0x1f
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)
277 #define TRF7970A_RX_SPECIAL_SETTINGS_NO_LIM BIT(0)
278 #define TRF7970A_RX_SPECIAL_SETTINGS_AGCR BIT(1)
279 #define TRF7970A_RX_SPECIAL_SETTINGS_GD_0DB (0x0 << 2)
280 #define TRF7970A_RX_SPECIAL_SETTINGS_GD_5DB (0x1 << 2)
281 #define TRF7970A_RX_SPECIAL_SETTINGS_GD_10DB (0x2 << 2)
282 #define TRF7970A_RX_SPECIAL_SETTINGS_GD_15DB (0x3 << 2)
283 #define TRF7970A_RX_SPECIAL_SETTINGS_HBT BIT(4)
284 #define TRF7970A_RX_SPECIAL_SETTINGS_M848 BIT(5)
285 #define TRF7970A_RX_SPECIAL_SETTINGS_C424 BIT(6)
286 #define TRF7970A_RX_SPECIAL_SETTINGS_C212 BIT(7)
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)
293 /* IRQ Status Register Bits */
294 #define TRF7970A_IRQ_STATUS_NORESP BIT(0) /* ISO15693 only */
295 #define TRF7970A_IRQ_STATUS_NFC_COL_ERROR BIT(0)
296 #define TRF7970A_IRQ_STATUS_COL BIT(1)
297 #define TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR BIT(2)
298 #define TRF7970A_IRQ_STATUS_NFC_RF BIT(2)
299 #define TRF7970A_IRQ_STATUS_PARITY_ERROR BIT(3)
300 #define TRF7970A_IRQ_STATUS_NFC_SDD BIT(3)
301 #define TRF7970A_IRQ_STATUS_CRC_ERROR BIT(4)
302 #define TRF7970A_IRQ_STATUS_NFC_PROTO_ERROR BIT(4)
303 #define TRF7970A_IRQ_STATUS_FIFO BIT(5)
304 #define TRF7970A_IRQ_STATUS_SRX BIT(6)
305 #define TRF7970A_IRQ_STATUS_TX BIT(7)
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)
313 #define TRF7970A_RSSI_OSC_STATUS_RSSI_MASK (BIT(2) | BIT(1) | BIT(0))
314 #define TRF7970A_RSSI_OSC_STATUS_RSSI_X_MASK (BIT(5) | BIT(4) | BIT(3))
315 #define TRF7970A_RSSI_OSC_STATUS_RSSI_OSC_OK BIT(6)
317 #define TRF7970A_SPECIAL_FCN_REG1_COL_7_6 BIT(0)
318 #define TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL BIT(1)
319 #define TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX BIT(2)
320 #define TRF7970A_SPECIAL_FCN_REG1_SP_DIR_MODE BIT(3)
321 #define TRF7970A_SPECIAL_FCN_REG1_NEXT_SLOT_37US BIT(4)
322 #define TRF7970A_SPECIAL_FCN_REG1_PAR43 BIT(5)
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
333 #define TRF7970A_NFC_LOW_FIELD_LEVEL_RFDET(v) ((v) & 0x07)
334 #define TRF7970A_NFC_LOW_FIELD_LEVEL_CLEX_DIS BIT(7)
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)
343 #define TRF7970A_FIFO_STATUS_OVERFLOW BIT(7)
345 /* NFC (ISO/IEC 14443A) Type 2 Tag commands */
346 #define NFC_T2T_CMD_READ 0x30
348 /* ISO 15693 commands codes */
349 #define ISO15693_CMD_INVENTORY 0x01
350 #define ISO15693_CMD_READ_SINGLE_BLOCK 0x20
351 #define ISO15693_CMD_WRITE_SINGLE_BLOCK 0x21
352 #define ISO15693_CMD_LOCK_BLOCK 0x22
353 #define ISO15693_CMD_READ_MULTIPLE_BLOCK 0x23
354 #define ISO15693_CMD_WRITE_MULTIPLE_BLOCK 0x24
355 #define ISO15693_CMD_SELECT 0x25
356 #define ISO15693_CMD_RESET_TO_READY 0x26
357 #define ISO15693_CMD_WRITE_AFI 0x27
358 #define ISO15693_CMD_LOCK_AFI 0x28
359 #define ISO15693_CMD_WRITE_DSFID 0x29
360 #define ISO15693_CMD_LOCK_DSFID 0x2a
361 #define ISO15693_CMD_GET_SYSTEM_INFO 0x2b
362 #define ISO15693_CMD_GET_MULTIPLE_BLOCK_SECURITY_STATUS 0x2c
364 /* ISO 15693 request and response flags */
365 #define ISO15693_REQ_FLAG_SUB_CARRIER BIT(0)
366 #define ISO15693_REQ_FLAG_DATA_RATE BIT(1)
367 #define ISO15693_REQ_FLAG_INVENTORY BIT(2)
368 #define ISO15693_REQ_FLAG_PROTOCOL_EXT BIT(3)
369 #define ISO15693_REQ_FLAG_SELECT BIT(4)
370 #define ISO15693_REQ_FLAG_AFI BIT(4)
371 #define ISO15693_REQ_FLAG_ADDRESS BIT(5)
372 #define ISO15693_REQ_FLAG_NB_SLOTS BIT(5)
373 #define ISO15693_REQ_FLAG_OPTION BIT(6)
375 #define ISO15693_REQ_FLAG_SPEED_MASK \
376 (ISO15693_REQ_FLAG_SUB_CARRIER | ISO15693_REQ_FLAG_DATA_RATE)
378 enum trf7970a_state {
382 TRF7970A_ST_IDLE_RX_BLOCKED,
383 TRF7970A_ST_WAIT_FOR_TX_FIFO,
384 TRF7970A_ST_WAIT_FOR_RX_DATA,
385 TRF7970A_ST_WAIT_FOR_RX_DATA_CONT,
386 TRF7970A_ST_WAIT_TO_ISSUE_EOF,
387 TRF7970A_ST_LISTENING,
392 enum trf7970a_state state;
394 struct spi_device *spi;
395 struct regulator *regulator;
396 struct nfc_digital_dev *ddev;
400 struct sk_buff *tx_skb;
401 struct sk_buff *rx_skb;
402 nfc_digital_cmd_complete_t cb;
407 u8 modulator_sys_clk_ctrl;
409 unsigned int guard_time;
417 unsigned int timeout;
419 struct delayed_work timeout_work;
423 static int trf7970a_cmd(struct trf7970a *trf, u8 opcode)
425 u8 cmd = TRF7970A_CMD_BIT_CTRL | TRF7970A_CMD_BIT_OPCODE(opcode);
428 dev_dbg(trf->dev, "cmd: 0x%x\n", cmd);
430 ret = spi_write(trf->spi, &cmd, 1);
432 dev_err(trf->dev, "%s - cmd: 0x%x, ret: %d\n", __func__, cmd,
437 static int trf7970a_read(struct trf7970a *trf, u8 reg, u8 *val)
439 u8 addr = TRF7970A_CMD_BIT_RW | reg;
442 ret = spi_write_then_read(trf->spi, &addr, 1, val, 1);
444 dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr,
447 dev_dbg(trf->dev, "read(0x%x): 0x%x\n", addr, *val);
452 static int trf7970a_read_cont(struct trf7970a *trf, u8 reg, u8 *buf, size_t len)
454 u8 addr = reg | TRF7970A_CMD_BIT_RW | TRF7970A_CMD_BIT_CONTINUOUS;
455 struct spi_transfer t[2];
456 struct spi_message m;
459 dev_dbg(trf->dev, "read_cont(0x%x, %zd)\n", addr, len);
461 spi_message_init(&m);
463 memset(&t, 0, sizeof(t));
466 t[0].len = sizeof(addr);
467 spi_message_add_tail(&t[0], &m);
471 spi_message_add_tail(&t[1], &m);
473 ret = spi_sync(trf->spi, &m);
475 dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr,
480 static int trf7970a_write(struct trf7970a *trf, u8 reg, u8 val)
482 u8 buf[2] = { reg, val };
485 dev_dbg(trf->dev, "write(0x%x): 0x%x\n", reg, val);
487 ret = spi_write(trf->spi, buf, 2);
489 dev_err(trf->dev, "%s - write: 0x%x 0x%x, ret: %d\n", __func__,
490 buf[0], buf[1], ret);
495 static int trf7970a_read_irqstatus(struct trf7970a *trf, u8 *status)
501 addr = TRF7970A_IRQ_STATUS | TRF7970A_CMD_BIT_RW;
503 if (trf->quirks & TRF7970A_QUIRK_IRQ_STATUS_READ) {
504 addr |= TRF7970A_CMD_BIT_CONTINUOUS;
505 ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2);
507 ret = spi_write_then_read(trf->spi, &addr, 1, buf, 1);
511 dev_err(trf->dev, "%s - irqstatus: Status read failed: %d\n",
519 static void trf7970a_send_upstream(struct trf7970a *trf)
521 dev_kfree_skb_any(trf->tx_skb);
524 if (trf->rx_skb && !IS_ERR(trf->rx_skb) && !trf->aborting)
525 print_hex_dump_debug("trf7970a rx data: ", DUMP_PREFIX_NONE,
526 16, 1, trf->rx_skb->data, trf->rx_skb->len,
529 trf->state = TRF7970A_ST_IDLE;
532 dev_dbg(trf->dev, "Abort process complete\n");
534 if (!IS_ERR(trf->rx_skb)) {
535 kfree_skb(trf->rx_skb);
536 trf->rx_skb = ERR_PTR(-ECANCELED);
539 trf->aborting = false;
542 trf->cb(trf->ddev, trf->cb_arg, trf->rx_skb);
547 static void trf7970a_send_err_upstream(struct trf7970a *trf, int errno)
549 dev_dbg(trf->dev, "Error - state: %d, errno: %d\n", trf->state, errno);
551 cancel_delayed_work(&trf->timeout_work);
553 kfree_skb(trf->rx_skb);
554 trf->rx_skb = ERR_PTR(errno);
556 trf7970a_send_upstream(trf);
559 static int trf7970a_transmit(struct trf7970a *trf, struct sk_buff *skb,
560 unsigned int len, u8 *prefix, unsigned int prefix_len)
562 struct spi_transfer t[2];
563 struct spi_message m;
564 unsigned int timeout;
567 print_hex_dump_debug("trf7970a tx data: ", DUMP_PREFIX_NONE,
568 16, 1, skb->data, len, false);
570 spi_message_init(&m);
572 memset(&t, 0, sizeof(t));
574 t[0].tx_buf = prefix;
575 t[0].len = prefix_len;
576 spi_message_add_tail(&t[0], &m);
578 t[1].tx_buf = skb->data;
580 spi_message_add_tail(&t[1], &m);
582 ret = spi_sync(trf->spi, &m);
584 dev_err(trf->dev, "%s - Can't send tx data: %d\n", __func__,
592 trf->state = TRF7970A_ST_WAIT_FOR_TX_FIFO;
593 timeout = TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT;
595 if (trf->issue_eof) {
596 trf->state = TRF7970A_ST_WAIT_TO_ISSUE_EOF;
597 timeout = TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF;
599 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA;
602 timeout = TRF7970A_WAIT_FOR_TX_IRQ;
604 timeout = trf->timeout;
608 dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", timeout,
611 schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout));
616 static void trf7970a_fill_fifo(struct trf7970a *trf)
618 struct sk_buff *skb = trf->tx_skb;
624 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
626 trf7970a_send_err_upstream(trf, ret);
630 dev_dbg(trf->dev, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes);
632 fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
634 /* Calculate how much more data can be written to the fifo */
635 len = TRF7970A_FIFO_SIZE - fifo_bytes;
637 schedule_delayed_work(&trf->timeout_work,
638 msecs_to_jiffies(TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT));
642 len = min(skb->len, len);
644 prefix = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_FIFO_IO_REGISTER;
646 ret = trf7970a_transmit(trf, skb, len, &prefix, sizeof(prefix));
648 trf7970a_send_err_upstream(trf, ret);
651 static void trf7970a_drain_fifo(struct trf7970a *trf, u8 status)
653 struct sk_buff *skb = trf->rx_skb;
657 if (status & TRF7970A_IRQ_STATUS_ERROR) {
658 trf7970a_send_err_upstream(trf, -EIO);
662 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
664 trf7970a_send_err_upstream(trf, ret);
668 dev_dbg(trf->dev, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes);
670 fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
675 if (fifo_bytes > skb_tailroom(skb)) {
676 skb = skb_copy_expand(skb, skb_headroom(skb),
677 max_t(int, fifo_bytes,
678 TRF7970A_RX_SKB_ALLOC_SIZE),
681 trf7970a_send_err_upstream(trf, -ENOMEM);
685 kfree_skb(trf->rx_skb);
689 ret = trf7970a_read_cont(trf, TRF7970A_FIFO_IO_REGISTER,
690 skb_put(skb, fifo_bytes), fifo_bytes);
692 trf7970a_send_err_upstream(trf, ret);
696 /* If received Type 2 ACK/NACK, shift right 4 bits and pass up */
697 if ((trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T) && (skb->len == 1) &&
698 (trf->special_fcn_reg1 ==
699 TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX)) {
701 status = TRF7970A_IRQ_STATUS_SRX;
703 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA_CONT;
705 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
707 trf7970a_send_err_upstream(trf, ret);
711 fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
713 /* If there are bytes in the FIFO, set status to '0' so
714 * the if stmt below doesn't fire and the driver will wait
715 * for the trf7970a to generate another RX interrupt.
722 if (status == TRF7970A_IRQ_STATUS_SRX) { /* Receive complete */
723 trf7970a_send_upstream(trf);
727 dev_dbg(trf->dev, "Setting timeout for %d ms\n",
728 TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT);
730 schedule_delayed_work(&trf->timeout_work,
731 msecs_to_jiffies(TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT));
734 static irqreturn_t trf7970a_irq(int irq, void *dev_id)
736 struct trf7970a *trf = dev_id;
738 u8 status, fifo_bytes, iso_ctrl;
740 mutex_lock(&trf->lock);
742 if (trf->state == TRF7970A_ST_RF_OFF) {
743 mutex_unlock(&trf->lock);
747 ret = trf7970a_read_irqstatus(trf, &status);
749 mutex_unlock(&trf->lock);
753 dev_dbg(trf->dev, "IRQ - state: %d, status: 0x%x\n", trf->state,
757 mutex_unlock(&trf->lock);
761 switch (trf->state) {
762 case TRF7970A_ST_IDLE:
763 case TRF7970A_ST_IDLE_RX_BLOCKED:
764 /* If initiator and getting interrupts caused by RF noise,
765 * turn off the receiver to avoid unnecessary interrupts.
766 * It will be turned back on in trf7970a_send_cmd() when
767 * the next command is issued.
769 if (trf->is_initiator && (status & TRF7970A_IRQ_STATUS_ERROR)) {
770 trf7970a_cmd(trf, TRF7970A_CMD_BLOCK_RX);
771 trf->state = TRF7970A_ST_IDLE_RX_BLOCKED;
774 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
776 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
777 if (status & TRF7970A_IRQ_STATUS_TX) {
778 trf->ignore_timeout =
779 !cancel_delayed_work(&trf->timeout_work);
780 trf7970a_fill_fifo(trf);
782 trf7970a_send_err_upstream(trf, -EIO);
785 case TRF7970A_ST_WAIT_FOR_RX_DATA:
786 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
787 if (status & TRF7970A_IRQ_STATUS_SRX) {
788 trf->ignore_timeout =
789 !cancel_delayed_work(&trf->timeout_work);
790 trf7970a_drain_fifo(trf, status);
791 } else if (status & TRF7970A_IRQ_STATUS_FIFO) {
792 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS,
795 fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
798 trf7970a_send_err_upstream(trf, ret);
799 else if (!fifo_bytes)
800 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
801 } else if ((status == TRF7970A_IRQ_STATUS_TX) ||
802 (!trf->is_initiator &&
803 (status == (TRF7970A_IRQ_STATUS_TX |
804 TRF7970A_IRQ_STATUS_NFC_RF)))) {
805 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
808 trf->ignore_timeout = !cancel_delayed_work(
810 trf->rx_skb = ERR_PTR(0);
811 trf7970a_send_upstream(trf);
815 if (trf->is_initiator)
818 iso_ctrl = trf->iso_ctrl;
820 switch (trf->framing) {
821 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
822 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
823 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
824 trf->iso_ctrl = 0xff; /* Force ISO_CTRL write */
826 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
827 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
828 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
829 trf->iso_ctrl = 0xff; /* Force ISO_CTRL write */
831 case NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE:
832 ret = trf7970a_write(trf,
833 TRF7970A_SPECIAL_FCN_REG1,
834 TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL);
838 trf->special_fcn_reg1 =
839 TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL;
845 if (iso_ctrl != trf->iso_ctrl) {
846 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL,
851 trf->iso_ctrl = iso_ctrl;
854 trf7970a_send_err_upstream(trf, -EIO);
857 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
858 if (status != TRF7970A_IRQ_STATUS_TX)
859 trf7970a_send_err_upstream(trf, -EIO);
861 case TRF7970A_ST_LISTENING:
862 if (status & TRF7970A_IRQ_STATUS_SRX) {
863 trf->ignore_timeout =
864 !cancel_delayed_work(&trf->timeout_work);
865 trf7970a_drain_fifo(trf, status);
866 } else if (!(status & TRF7970A_IRQ_STATUS_NFC_RF)) {
867 trf7970a_send_err_upstream(trf, -EIO);
871 dev_err(trf->dev, "%s - Driver in invalid state: %d\n",
872 __func__, trf->state);
875 mutex_unlock(&trf->lock);
879 static void trf7970a_issue_eof(struct trf7970a *trf)
883 dev_dbg(trf->dev, "Issuing EOF\n");
885 ret = trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
887 trf7970a_send_err_upstream(trf, ret);
889 ret = trf7970a_cmd(trf, TRF7970A_CMD_EOF);
891 trf7970a_send_err_upstream(trf, ret);
893 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA;
895 dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n",
896 trf->timeout, trf->state);
898 schedule_delayed_work(&trf->timeout_work,
899 msecs_to_jiffies(trf->timeout));
902 static void trf7970a_timeout_work_handler(struct work_struct *work)
904 struct trf7970a *trf = container_of(work, struct trf7970a,
907 dev_dbg(trf->dev, "Timeout - state: %d, ignore_timeout: %d\n",
908 trf->state, trf->ignore_timeout);
910 mutex_lock(&trf->lock);
912 if (trf->ignore_timeout)
913 trf->ignore_timeout = false;
914 else if (trf->state == TRF7970A_ST_WAIT_FOR_RX_DATA_CONT)
915 trf7970a_drain_fifo(trf, TRF7970A_IRQ_STATUS_SRX);
916 else if (trf->state == TRF7970A_ST_WAIT_TO_ISSUE_EOF)
917 trf7970a_issue_eof(trf);
919 trf7970a_send_err_upstream(trf, -ETIMEDOUT);
921 mutex_unlock(&trf->lock);
924 static int trf7970a_init(struct trf7970a *trf)
928 dev_dbg(trf->dev, "Initializing device - state: %d\n", trf->state);
930 ret = trf7970a_cmd(trf, TRF7970A_CMD_SOFT_INIT);
934 ret = trf7970a_cmd(trf, TRF7970A_CMD_IDLE);
938 usleep_range(1000, 2000);
940 trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
942 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL, 0);
946 trf->modulator_sys_clk_ctrl = 0;
948 ret = trf7970a_write(trf, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS,
949 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 |
950 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32);
954 ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, 0);
958 trf->special_fcn_reg1 = 0;
960 trf->iso_ctrl = 0xff;
964 dev_dbg(trf->dev, "Couldn't init device: %d\n", ret);
968 static void trf7970a_switch_rf_off(struct trf7970a *trf)
970 if ((trf->state == TRF7970A_ST_PWR_OFF) ||
971 (trf->state == TRF7970A_ST_RF_OFF))
974 dev_dbg(trf->dev, "Switching rf off\n");
976 trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
978 trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, trf->chip_status_ctrl);
980 trf->aborting = false;
981 trf->state = TRF7970A_ST_RF_OFF;
983 pm_runtime_mark_last_busy(trf->dev);
984 pm_runtime_put_autosuspend(trf->dev);
987 static int trf7970a_switch_rf_on(struct trf7970a *trf)
991 dev_dbg(trf->dev, "Switching rf on\n");
993 pm_runtime_get_sync(trf->dev);
995 if (trf->state != TRF7970A_ST_RF_OFF) { /* Power on, RF off */
996 dev_err(trf->dev, "%s - Incorrect state: %d\n", __func__,
1001 ret = trf7970a_init(trf);
1003 dev_err(trf->dev, "%s - Can't initialize: %d\n", __func__, ret);
1007 trf->state = TRF7970A_ST_IDLE;
1012 static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
1014 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1017 dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on);
1019 mutex_lock(&trf->lock);
1022 switch (trf->state) {
1023 case TRF7970A_ST_PWR_OFF:
1024 case TRF7970A_ST_RF_OFF:
1025 ret = trf7970a_switch_rf_on(trf);
1027 case TRF7970A_ST_IDLE:
1028 case TRF7970A_ST_IDLE_RX_BLOCKED:
1031 dev_err(trf->dev, "%s - Invalid request: %d %d\n",
1032 __func__, trf->state, on);
1033 trf7970a_switch_rf_off(trf);
1037 switch (trf->state) {
1038 case TRF7970A_ST_PWR_OFF:
1039 case TRF7970A_ST_RF_OFF:
1042 dev_err(trf->dev, "%s - Invalid request: %d %d\n",
1043 __func__, trf->state, on);
1046 case TRF7970A_ST_IDLE:
1047 case TRF7970A_ST_IDLE_RX_BLOCKED:
1048 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1049 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1050 trf7970a_switch_rf_off(trf);
1054 mutex_unlock(&trf->lock);
1058 static int trf7970a_in_config_rf_tech(struct trf7970a *trf, int tech)
1062 dev_dbg(trf->dev, "rf technology: %d\n", tech);
1065 case NFC_DIGITAL_RF_TECH_106A:
1066 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443A_106;
1067 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
1068 trf->guard_time = TRF7970A_GUARD_TIME_NFCA;
1070 case NFC_DIGITAL_RF_TECH_106B:
1071 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443B_106;
1072 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
1073 trf->guard_time = TRF7970A_GUARD_TIME_NFCB;
1075 case NFC_DIGITAL_RF_TECH_212F:
1076 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_212;
1077 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
1078 trf->guard_time = TRF7970A_GUARD_TIME_NFCF;
1080 case NFC_DIGITAL_RF_TECH_424F:
1081 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_424;
1082 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
1083 trf->guard_time = TRF7970A_GUARD_TIME_NFCF;
1085 case NFC_DIGITAL_RF_TECH_ISO15693:
1086 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
1087 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
1088 trf->guard_time = TRF7970A_GUARD_TIME_15693;
1091 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
1095 trf->technology = tech;
1097 /* If in initiator mode and not changing the RF tech due to a
1098 * PSL sequence (indicated by 'trf->iso_ctrl == 0xff' from
1099 * trf7970a_init()), clear the NFC Target Detection Level register
1102 if (trf->iso_ctrl == 0xff)
1103 ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
1108 static int trf7970a_is_rf_field(struct trf7970a *trf, bool *is_rf_field)
1113 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1114 trf->chip_status_ctrl | TRF7970A_CHIP_STATUS_REC_ON);
1118 ret = trf7970a_cmd(trf, TRF7970A_CMD_TEST_EXT_RF);
1122 usleep_range(50, 60);
1124 ret = trf7970a_read(trf, TRF7970A_RSSI_OSC_STATUS, &rssi);
1128 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1129 trf->chip_status_ctrl);
1133 if (rssi & TRF7970A_RSSI_OSC_STATUS_RSSI_MASK)
1134 *is_rf_field = true;
1136 *is_rf_field = false;
1141 static int trf7970a_in_config_framing(struct trf7970a *trf, int framing)
1143 u8 iso_ctrl = trf->iso_ctrl_tech;
1144 bool is_rf_field = false;
1147 dev_dbg(trf->dev, "framing: %d\n", framing);
1150 case NFC_DIGITAL_FRAMING_NFCA_SHORT:
1151 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
1152 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
1153 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
1155 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
1156 case NFC_DIGITAL_FRAMING_NFCA_T4T:
1157 case NFC_DIGITAL_FRAMING_NFCB:
1158 case NFC_DIGITAL_FRAMING_NFCB_T4T:
1159 case NFC_DIGITAL_FRAMING_NFCF:
1160 case NFC_DIGITAL_FRAMING_NFCF_T3T:
1161 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY:
1162 case NFC_DIGITAL_FRAMING_ISO15693_T5T:
1163 case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
1164 case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP:
1165 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1166 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
1168 case NFC_DIGITAL_FRAMING_NFCA_T2T:
1169 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1170 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
1173 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
1177 trf->framing = framing;
1179 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
1180 ret = trf7970a_is_rf_field(trf, &is_rf_field);
1188 if (iso_ctrl != trf->iso_ctrl) {
1189 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1193 trf->iso_ctrl = iso_ctrl;
1195 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
1196 trf->modulator_sys_clk_ctrl);
1201 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
1202 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1203 trf->chip_status_ctrl |
1204 TRF7970A_CHIP_STATUS_RF_ON);
1208 trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
1210 usleep_range(trf->guard_time, trf->guard_time + 1000);
1216 static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type,
1219 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1222 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
1224 mutex_lock(&trf->lock);
1226 trf->is_initiator = true;
1228 if ((trf->state == TRF7970A_ST_PWR_OFF) ||
1229 (trf->state == TRF7970A_ST_RF_OFF)) {
1230 ret = trf7970a_switch_rf_on(trf);
1236 case NFC_DIGITAL_CONFIG_RF_TECH:
1237 ret = trf7970a_in_config_rf_tech(trf, param);
1239 case NFC_DIGITAL_CONFIG_FRAMING:
1240 ret = trf7970a_in_config_framing(trf, param);
1243 dev_dbg(trf->dev, "Unknown type: %d\n", type);
1248 mutex_unlock(&trf->lock);
1252 static int trf7970a_is_iso15693_write_or_lock(u8 cmd)
1255 case ISO15693_CMD_WRITE_SINGLE_BLOCK:
1256 case ISO15693_CMD_LOCK_BLOCK:
1257 case ISO15693_CMD_WRITE_MULTIPLE_BLOCK:
1258 case ISO15693_CMD_WRITE_AFI:
1259 case ISO15693_CMD_LOCK_AFI:
1260 case ISO15693_CMD_WRITE_DSFID:
1261 case ISO15693_CMD_LOCK_DSFID:
1269 static int trf7970a_per_cmd_config(struct trf7970a *trf, struct sk_buff *skb)
1271 u8 *req = skb->data;
1272 u8 special_fcn_reg1, iso_ctrl;
1275 trf->issue_eof = false;
1277 /* When issuing Type 2 read command, make sure the '4_bit_RX' bit in
1278 * special functions register 1 is cleared; otherwise, its a write or
1279 * sector select command and '4_bit_RX' must be set.
1281 * When issuing an ISO 15693 command, inspect the flags byte to see
1282 * what speed to use. Also, remember if the OPTION flag is set on
1283 * a Type 5 write or lock command so the driver will know that it
1284 * has to send an EOF in order to get a response.
1286 if ((trf->technology == NFC_DIGITAL_RF_TECH_106A) &&
1287 (trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T)) {
1288 if (req[0] == NFC_T2T_CMD_READ)
1289 special_fcn_reg1 = 0;
1291 special_fcn_reg1 = TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX;
1293 if (special_fcn_reg1 != trf->special_fcn_reg1) {
1294 ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1,
1299 trf->special_fcn_reg1 = special_fcn_reg1;
1301 } else if (trf->technology == NFC_DIGITAL_RF_TECH_ISO15693) {
1302 iso_ctrl = trf->iso_ctrl & ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK;
1304 switch (req[0] & ISO15693_REQ_FLAG_SPEED_MASK) {
1306 iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_662;
1308 case ISO15693_REQ_FLAG_SUB_CARRIER:
1309 iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a;
1311 case ISO15693_REQ_FLAG_DATA_RATE:
1312 iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
1314 case (ISO15693_REQ_FLAG_SUB_CARRIER |
1315 ISO15693_REQ_FLAG_DATA_RATE):
1316 iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669;
1320 if (iso_ctrl != trf->iso_ctrl) {
1321 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1325 trf->iso_ctrl = iso_ctrl;
1328 if ((trf->framing == NFC_DIGITAL_FRAMING_ISO15693_T5T) &&
1329 trf7970a_is_iso15693_write_or_lock(req[1]) &&
1330 (req[0] & ISO15693_REQ_FLAG_OPTION))
1331 trf->issue_eof = true;
1337 static int trf7970a_send_cmd(struct nfc_digital_dev *ddev,
1338 struct sk_buff *skb, u16 timeout,
1339 nfc_digital_cmd_complete_t cb, void *arg)
1341 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1347 dev_dbg(trf->dev, "New request - state: %d, timeout: %d ms, len: %d\n",
1348 trf->state, timeout, skb->len);
1350 if (skb->len > TRF7970A_TX_MAX)
1353 mutex_lock(&trf->lock);
1355 if ((trf->state != TRF7970A_ST_IDLE) &&
1356 (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) {
1357 dev_err(trf->dev, "%s - Bogus state: %d\n", __func__,
1363 if (trf->aborting) {
1364 dev_dbg(trf->dev, "Abort process complete\n");
1365 trf->aborting = false;
1371 trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE,
1374 dev_dbg(trf->dev, "Can't alloc rx_skb\n");
1380 if (trf->state == TRF7970A_ST_IDLE_RX_BLOCKED) {
1381 ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX);
1385 trf->state = TRF7970A_ST_IDLE;
1388 if (trf->is_initiator) {
1389 ret = trf7970a_per_cmd_config(trf, skb);
1398 trf->timeout = timeout;
1399 trf->ignore_timeout = false;
1403 /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
1404 * on what the current framing is, the address of the TX length byte 1
1405 * register (0x1d), and the 2 byte length of the data to be transmitted.
1406 * That totals 5 bytes.
1408 prefix[0] = TRF7970A_CMD_BIT_CTRL |
1409 TRF7970A_CMD_BIT_OPCODE(TRF7970A_CMD_FIFO_RESET);
1410 prefix[1] = TRF7970A_CMD_BIT_CTRL |
1411 TRF7970A_CMD_BIT_OPCODE(trf->tx_cmd);
1412 prefix[2] = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_TX_LENGTH_BYTE1;
1414 if (trf->framing == NFC_DIGITAL_FRAMING_NFCA_SHORT) {
1416 prefix[4] = 0x0f; /* 7 bits */
1418 prefix[3] = (len & 0xf00) >> 4;
1419 prefix[3] |= ((len & 0xf0) >> 4);
1420 prefix[4] = ((len & 0x0f) << 4);
1423 len = min_t(int, skb->len, TRF7970A_FIFO_SIZE);
1425 /* Clear possible spurious interrupt */
1426 ret = trf7970a_read_irqstatus(trf, &status);
1430 ret = trf7970a_transmit(trf, skb, len, prefix, sizeof(prefix));
1432 kfree_skb(trf->rx_skb);
1437 mutex_unlock(&trf->lock);
1441 static int trf7970a_tg_config_rf_tech(struct trf7970a *trf, int tech)
1445 dev_dbg(trf->dev, "rf technology: %d\n", tech);
1448 case NFC_DIGITAL_RF_TECH_106A:
1449 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
1450 TRF7970A_ISO_CTRL_NFC_CE |
1451 TRF7970A_ISO_CTRL_NFC_CE_14443A;
1452 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
1454 case NFC_DIGITAL_RF_TECH_212F:
1455 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
1456 TRF7970A_ISO_CTRL_NFC_NFCF_212;
1457 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
1459 case NFC_DIGITAL_RF_TECH_424F:
1460 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
1461 TRF7970A_ISO_CTRL_NFC_NFCF_424;
1462 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
1465 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
1469 trf->technology = tech;
1471 /* Normally we write the ISO_CTRL register in
1472 * trf7970a_tg_config_framing() because the framing can change
1473 * the value written. However, when sending a PSL RES,
1474 * digital_tg_send_psl_res_complete() doesn't call
1475 * trf7970a_tg_config_framing() so we must write the register
1478 if ((trf->framing == NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED) &&
1479 (trf->iso_ctrl_tech != trf->iso_ctrl)) {
1480 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL,
1481 trf->iso_ctrl_tech);
1483 trf->iso_ctrl = trf->iso_ctrl_tech;
1489 /* Since this is a target routine, several of the framing calls are
1490 * made between receiving the request and sending the response so they
1491 * should take effect until after the response is sent. This is accomplished
1492 * by skipping the ISO_CTRL register write here and doing it in the interrupt
1495 static int trf7970a_tg_config_framing(struct trf7970a *trf, int framing)
1497 u8 iso_ctrl = trf->iso_ctrl_tech;
1500 dev_dbg(trf->dev, "framing: %d\n", framing);
1503 case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
1504 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
1505 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
1507 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
1508 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
1509 case NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE:
1510 /* These ones are applied in the interrupt handler */
1511 iso_ctrl = trf->iso_ctrl; /* Don't write to ISO_CTRL yet */
1513 case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP:
1514 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1515 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
1517 case NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED:
1518 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
1519 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
1522 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
1526 trf->framing = framing;
1528 if (iso_ctrl != trf->iso_ctrl) {
1529 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1533 trf->iso_ctrl = iso_ctrl;
1535 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
1536 trf->modulator_sys_clk_ctrl);
1541 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
1542 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
1543 trf->chip_status_ctrl |
1544 TRF7970A_CHIP_STATUS_RF_ON);
1548 trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
1554 static int trf7970a_tg_configure_hw(struct nfc_digital_dev *ddev, int type,
1557 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1560 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
1562 mutex_lock(&trf->lock);
1564 trf->is_initiator = false;
1566 if ((trf->state == TRF7970A_ST_PWR_OFF) ||
1567 (trf->state == TRF7970A_ST_RF_OFF)) {
1568 ret = trf7970a_switch_rf_on(trf);
1574 case NFC_DIGITAL_CONFIG_RF_TECH:
1575 ret = trf7970a_tg_config_rf_tech(trf, param);
1577 case NFC_DIGITAL_CONFIG_FRAMING:
1578 ret = trf7970a_tg_config_framing(trf, param);
1581 dev_dbg(trf->dev, "Unknown type: %d\n", type);
1586 mutex_unlock(&trf->lock);
1590 static int trf7970a_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
1591 nfc_digital_cmd_complete_t cb, void *arg)
1593 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1596 dev_dbg(trf->dev, "Listen - state: %d, timeout: %d ms\n",
1597 trf->state, timeout);
1599 mutex_lock(&trf->lock);
1601 if ((trf->state != TRF7970A_ST_IDLE) &&
1602 (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) {
1603 dev_err(trf->dev, "%s - Bogus state: %d\n", __func__,
1609 if (trf->aborting) {
1610 dev_dbg(trf->dev, "Abort process complete\n");
1611 trf->aborting = false;
1616 trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE,
1619 dev_dbg(trf->dev, "Can't alloc rx_skb\n");
1624 ret = trf7970a_write(trf, TRF7970A_RX_SPECIAL_SETTINGS,
1625 TRF7970A_RX_SPECIAL_SETTINGS_HBT |
1626 TRF7970A_RX_SPECIAL_SETTINGS_M848 |
1627 TRF7970A_RX_SPECIAL_SETTINGS_C424 |
1628 TRF7970A_RX_SPECIAL_SETTINGS_C212);
1632 ret = trf7970a_write(trf, TRF7970A_REG_IO_CTRL,
1633 TRF7970A_REG_IO_CTRL_VRS(0x1));
1637 ret = trf7970a_write(trf, TRF7970A_NFC_LOW_FIELD_LEVEL,
1638 TRF7970A_NFC_LOW_FIELD_LEVEL_RFDET(0x3));
1642 ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL,
1643 TRF7970A_NFC_TARGET_LEVEL_RFDET(0x7));
1650 trf->timeout = timeout;
1651 trf->ignore_timeout = false;
1653 ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX);
1657 trf->state = TRF7970A_ST_LISTENING;
1659 schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout));
1662 mutex_unlock(&trf->lock);
1666 static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev)
1668 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1670 dev_dbg(trf->dev, "Abort process initiated\n");
1672 mutex_lock(&trf->lock);
1674 switch (trf->state) {
1675 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
1676 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1677 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1678 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
1679 trf->aborting = true;
1681 case TRF7970A_ST_LISTENING:
1682 trf->ignore_timeout = !cancel_delayed_work(&trf->timeout_work);
1683 trf7970a_send_err_upstream(trf, -ECANCELED);
1684 dev_dbg(trf->dev, "Abort process complete\n");
1690 mutex_unlock(&trf->lock);
1693 static struct nfc_digital_ops trf7970a_nfc_ops = {
1694 .in_configure_hw = trf7970a_in_configure_hw,
1695 .in_send_cmd = trf7970a_send_cmd,
1696 .tg_configure_hw = trf7970a_tg_configure_hw,
1697 .tg_send_cmd = trf7970a_send_cmd,
1698 .tg_listen = trf7970a_tg_listen,
1699 .switch_rf = trf7970a_switch_rf,
1700 .abort_cmd = trf7970a_abort_cmd,
1703 static int trf7970a_power_up(struct trf7970a *trf)
1707 dev_dbg(trf->dev, "Powering up - state: %d\n", trf->state);
1709 if (trf->state != TRF7970A_ST_PWR_OFF)
1712 ret = regulator_enable(trf->regulator);
1714 dev_err(trf->dev, "%s - Can't enable VIN: %d\n", __func__, ret);
1718 usleep_range(5000, 6000);
1720 if (!(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW)) {
1721 gpio_set_value(trf->en2_gpio, 1);
1722 usleep_range(1000, 2000);
1725 gpio_set_value(trf->en_gpio, 1);
1727 usleep_range(20000, 21000);
1729 trf->state = TRF7970A_ST_RF_OFF;
1734 static int trf7970a_power_down(struct trf7970a *trf)
1738 dev_dbg(trf->dev, "Powering down - state: %d\n", trf->state);
1740 if (trf->state == TRF7970A_ST_PWR_OFF)
1743 if (trf->state != TRF7970A_ST_RF_OFF) {
1744 dev_dbg(trf->dev, "Can't power down - not RF_OFF state (%d)\n",
1749 gpio_set_value(trf->en_gpio, 0);
1750 gpio_set_value(trf->en2_gpio, 0);
1752 ret = regulator_disable(trf->regulator);
1754 dev_err(trf->dev, "%s - Can't disable VIN: %d\n", __func__,
1757 trf->state = TRF7970A_ST_PWR_OFF;
1762 static int trf7970a_startup(struct trf7970a *trf)
1766 ret = trf7970a_power_up(trf);
1770 pm_runtime_set_active(trf->dev);
1771 pm_runtime_enable(trf->dev);
1772 pm_runtime_mark_last_busy(trf->dev);
1777 static void trf7970a_shutdown(struct trf7970a *trf)
1779 switch (trf->state) {
1780 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
1781 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1782 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1783 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
1784 case TRF7970A_ST_LISTENING:
1785 trf7970a_send_err_upstream(trf, -ECANCELED);
1787 case TRF7970A_ST_IDLE:
1788 case TRF7970A_ST_IDLE_RX_BLOCKED:
1789 trf7970a_switch_rf_off(trf);
1795 pm_runtime_disable(trf->dev);
1796 pm_runtime_set_suspended(trf->dev);
1798 trf7970a_power_down(trf);
1801 static int trf7970a_get_autosuspend_delay(struct device_node *np)
1803 int autosuspend_delay, ret;
1805 ret = of_property_read_u32(np, "autosuspend-delay", &autosuspend_delay);
1807 autosuspend_delay = TRF7970A_AUTOSUSPEND_DELAY;
1809 return autosuspend_delay;
1812 static int trf7970a_get_vin_voltage_override(struct device_node *np,
1815 return of_property_read_u32(np, "vin-voltage-override", vin_uvolts);
1818 static int trf7970a_probe(struct spi_device *spi)
1820 struct device_node *np = spi->dev.of_node;
1821 struct trf7970a *trf;
1822 int uvolts, autosuspend_delay, ret;
1825 dev_err(&spi->dev, "No Device Tree entry\n");
1829 trf = devm_kzalloc(&spi->dev, sizeof(*trf), GFP_KERNEL);
1833 trf->state = TRF7970A_ST_PWR_OFF;
1834 trf->dev = &spi->dev;
1837 spi->mode = SPI_MODE_1;
1838 spi->bits_per_word = 8;
1840 ret = spi_setup(spi);
1842 dev_err(trf->dev, "Can't set up SPI Communication\n");
1846 if (of_property_read_bool(np, "irq-status-read-quirk"))
1847 trf->quirks |= TRF7970A_QUIRK_IRQ_STATUS_READ;
1849 /* There are two enable pins - both must be present */
1850 trf->en_gpio = of_get_named_gpio(np, "ti,enable-gpios", 0);
1851 if (!gpio_is_valid(trf->en_gpio)) {
1852 dev_err(trf->dev, "No EN GPIO property\n");
1853 return trf->en_gpio;
1856 ret = devm_gpio_request_one(trf->dev, trf->en_gpio,
1857 GPIOF_DIR_OUT | GPIOF_INIT_LOW, "trf7970a EN");
1859 dev_err(trf->dev, "Can't request EN GPIO: %d\n", ret);
1863 trf->en2_gpio = of_get_named_gpio(np, "ti,enable-gpios", 1);
1864 if (!gpio_is_valid(trf->en2_gpio)) {
1865 dev_err(trf->dev, "No EN2 GPIO property\n");
1866 return trf->en2_gpio;
1869 ret = devm_gpio_request_one(trf->dev, trf->en2_gpio,
1870 GPIOF_DIR_OUT | GPIOF_INIT_LOW, "trf7970a EN2");
1872 dev_err(trf->dev, "Can't request EN2 GPIO: %d\n", ret);
1876 if (of_property_read_bool(np, "en2-rf-quirk"))
1877 trf->quirks |= TRF7970A_QUIRK_EN2_MUST_STAY_LOW;
1879 ret = devm_request_threaded_irq(trf->dev, spi->irq, NULL,
1880 trf7970a_irq, IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1883 dev_err(trf->dev, "Can't request IRQ#%d: %d\n", spi->irq, ret);
1887 mutex_init(&trf->lock);
1888 INIT_DELAYED_WORK(&trf->timeout_work, trf7970a_timeout_work_handler);
1890 trf->regulator = devm_regulator_get(&spi->dev, "vin");
1891 if (IS_ERR(trf->regulator)) {
1892 ret = PTR_ERR(trf->regulator);
1893 dev_err(trf->dev, "Can't get VIN regulator: %d\n", ret);
1894 goto err_destroy_lock;
1897 ret = regulator_enable(trf->regulator);
1899 dev_err(trf->dev, "Can't enable VIN: %d\n", ret);
1900 goto err_destroy_lock;
1903 ret = trf7970a_get_vin_voltage_override(np, &uvolts);
1905 uvolts = regulator_get_voltage(trf->regulator);
1907 if (uvolts > 4000000)
1908 trf->chip_status_ctrl = TRF7970A_CHIP_STATUS_VRS5_3;
1910 trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops,
1911 TRF7970A_SUPPORTED_PROTOCOLS,
1912 NFC_DIGITAL_DRV_CAPS_IN_CRC |
1913 NFC_DIGITAL_DRV_CAPS_TG_CRC, 0, 0);
1915 dev_err(trf->dev, "Can't allocate NFC digital device\n");
1917 goto err_disable_regulator;
1920 nfc_digital_set_parent_dev(trf->ddev, trf->dev);
1921 nfc_digital_set_drvdata(trf->ddev, trf);
1922 spi_set_drvdata(spi, trf);
1924 autosuspend_delay = trf7970a_get_autosuspend_delay(np);
1926 pm_runtime_set_autosuspend_delay(trf->dev, autosuspend_delay);
1927 pm_runtime_use_autosuspend(trf->dev);
1929 ret = trf7970a_startup(trf);
1933 ret = nfc_digital_register_device(trf->ddev);
1935 dev_err(trf->dev, "Can't register NFC digital device: %d\n",
1943 trf7970a_shutdown(trf);
1945 nfc_digital_free_device(trf->ddev);
1946 err_disable_regulator:
1947 regulator_disable(trf->regulator);
1949 mutex_destroy(&trf->lock);
1953 static int trf7970a_remove(struct spi_device *spi)
1955 struct trf7970a *trf = spi_get_drvdata(spi);
1957 mutex_lock(&trf->lock);
1959 trf7970a_shutdown(trf);
1961 mutex_unlock(&trf->lock);
1963 nfc_digital_unregister_device(trf->ddev);
1964 nfc_digital_free_device(trf->ddev);
1966 regulator_disable(trf->regulator);
1968 mutex_destroy(&trf->lock);
1973 #ifdef CONFIG_PM_SLEEP
1974 static int trf7970a_suspend(struct device *dev)
1976 struct spi_device *spi = container_of(dev, struct spi_device, dev);
1977 struct trf7970a *trf = spi_get_drvdata(spi);
1980 dev_dbg(dev, "Suspend\n");
1982 mutex_lock(&trf->lock);
1984 trf7970a_shutdown(trf);
1986 mutex_unlock(&trf->lock);
1991 static int trf7970a_resume(struct device *dev)
1993 struct spi_device *spi = container_of(dev, struct spi_device, dev);
1994 struct trf7970a *trf = spi_get_drvdata(spi);
1997 dev_dbg(dev, "Resume\n");
1999 mutex_lock(&trf->lock);
2001 ret = trf7970a_startup(trf);
2003 mutex_unlock(&trf->lock);
2009 #ifdef CONFIG_PM_RUNTIME
2010 static int trf7970a_pm_runtime_suspend(struct device *dev)
2012 struct spi_device *spi = container_of(dev, struct spi_device, dev);
2013 struct trf7970a *trf = spi_get_drvdata(spi);
2016 dev_dbg(dev, "Runtime suspend\n");
2018 mutex_lock(&trf->lock);
2020 ret = trf7970a_power_down(trf);
2022 mutex_unlock(&trf->lock);
2027 static int trf7970a_pm_runtime_resume(struct device *dev)
2029 struct spi_device *spi = container_of(dev, struct spi_device, dev);
2030 struct trf7970a *trf = spi_get_drvdata(spi);
2033 dev_dbg(dev, "Runtime resume\n");
2035 ret = trf7970a_power_up(trf);
2037 pm_runtime_mark_last_busy(dev);
2043 static const struct dev_pm_ops trf7970a_pm_ops = {
2044 SET_SYSTEM_SLEEP_PM_OPS(trf7970a_suspend, trf7970a_resume)
2045 SET_RUNTIME_PM_OPS(trf7970a_pm_runtime_suspend,
2046 trf7970a_pm_runtime_resume, NULL)
2049 static const struct spi_device_id trf7970a_id_table[] = {
2053 MODULE_DEVICE_TABLE(spi, trf7970a_id_table);
2055 static struct spi_driver trf7970a_spi_driver = {
2056 .probe = trf7970a_probe,
2057 .remove = trf7970a_remove,
2058 .id_table = trf7970a_id_table,
2061 .owner = THIS_MODULE,
2062 .pm = &trf7970a_pm_ops,
2066 module_spi_driver(trf7970a_spi_driver);
2068 MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>");
2069 MODULE_LICENSE("GPL v2");
2070 MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver");