Commit | Line | Data |
---|---|---|
165063f1 MG |
1 | /* |
2 | * TI TRF7970a RFID/NFC Transceiver Driver | |
3 | * | |
4 | * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com | |
5 | * | |
6 | * Author: Erick Macias <emacias@ti.com> | |
7 | * Author: Felipe Balbi <balbi@ti.com> | |
8 | * Author: Mark A. Greer <mgreer@animalcreek.com> | |
9 | * | |
10 | * This program is free software: you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License version 2 of | |
12 | * the License as published by the Free Software Foundation. | |
13 | */ | |
14 | ||
15 | #include <linux/module.h> | |
16 | #include <linux/device.h> | |
17 | #include <linux/netdevice.h> | |
18 | #include <linux/interrupt.h> | |
e6403b7c | 19 | #include <linux/pm_runtime.h> |
165063f1 MG |
20 | #include <linux/nfc.h> |
21 | #include <linux/skbuff.h> | |
22 | #include <linux/delay.h> | |
23 | #include <linux/gpio.h> | |
24 | #include <linux/of.h> | |
25 | #include <linux/of_gpio.h> | |
26 | #include <linux/spi/spi.h> | |
27 | #include <linux/regulator/consumer.h> | |
28 | ||
29 | #include <net/nfc/nfc.h> | |
30 | #include <net/nfc/digital.h> | |
31 | ||
32 | /* There are 3 ways the host can communicate with the trf7970a: | |
33 | * parallel mode, SPI with Slave Select (SS) mode, and SPI without | |
34 | * SS mode. The driver only supports the two SPI modes. | |
35 | * | |
36 | * The trf7970a is very timing sensitive and the VIN, EN2, and EN | |
37 | * pins must asserted in that order and with specific delays in between. | |
38 | * The delays used in the driver were provided by TI and have been | |
95064bd9 MG |
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. | |
165063f1 MG |
46 | * |
47 | * Timeouts are implemented using the delayed workqueue kernel facility. | |
48 | * Timeouts are required so things don't hang when there is no response | |
49 | * from the trf7970a (or tag). Using this mechanism creates a race with | |
50 | * interrupts, however. That is, an interrupt and a timeout could occur | |
51 | * closely enough together that one is blocked by the mutex while the other | |
52 | * executes. When the timeout handler executes first and blocks the | |
53 | * interrupt handler, it will eventually set the state to IDLE so the | |
54 | * interrupt handler will check the state and exit with no harm done. | |
55 | * When the interrupt handler executes first and blocks the timeout handler, | |
56 | * the cancel_delayed_work() call will know that it didn't cancel the | |
57 | * work item (i.e., timeout) and will return zero. That return code is | |
58 | * used by the timer handler to indicate that it should ignore the timeout | |
59 | * once its unblocked. | |
60 | * | |
61 | * Aborting an active command isn't as simple as it seems because the only | |
62 | * way to abort a command that's already been sent to the tag is so turn | |
63 | * off power to the tag. If we do that, though, we'd have to go through | |
64 | * the entire anticollision procedure again but the digital layer doesn't | |
65 | * support that. So, if an abort is received before trf7970a_in_send_cmd() | |
66 | * has sent the command to the tag, it simply returns -ECANCELED. If the | |
67 | * command has already been sent to the tag, then the driver continues | |
68 | * normally and recieves the response data (or error) but just before | |
69 | * sending the data upstream, it frees the rx_skb and sends -ECANCELED | |
70 | * upstream instead. If the command failed, that error will be sent | |
71 | * upstream. | |
72 | * | |
73 | * When recieving data from a tag and the interrupt status register has | |
74 | * only the SRX bit set, it means that all of the data has been received | |
75 | * (once what's in the fifo has been read). However, depending on timing | |
76 | * an interrupt status with only the SRX bit set may not be recived. In | |
5fa3af35 MG |
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 | |
165063f1 MG |
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 | |
5fa3af35 MG |
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. | |
165063f1 | 85 | * |
38b4eb1f MG |
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. | |
92 | * | |
165063f1 MG |
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. | |
9d9304b3 MG |
102 | * |
103 | * ISO/IEC 15693 frames specify whether to use single or double sub-carrier | |
104 | * frequencies and whether to use low or high data rates in the flags byte | |
105 | * of the frame. This means that the driver has to peek at all 15693 frames | |
106 | * to determine what speed to set the communication to. In addition, write | |
107 | * and lock commands use the OPTION flag to indicate that an EOF must be | |
108 | * sent to the tag before it will send its response. So the driver has to | |
109 | * examine all frames for that reason too. | |
110 | * | |
111 | * It is unclear how long to wait before sending the EOF. According to the | |
112 | * Note under Table 1-1 in section 1.6 of | |
113 | * http://www.ti.com/lit/ug/scbu011/scbu011.pdf, that wait should be at least | |
114 | * 10 ms for TI Tag-it HF-I tags; however testing has shown that is not long | |
38b4eb1f MG |
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 | |
9d9304b3 | 118 | * reliably. |
165063f1 MG |
119 | */ |
120 | ||
80062891 | 121 | #define TRF7970A_SUPPORTED_PROTOCOLS \ |
9d9304b3 | 122 | (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \ |
6857bb96 MG |
123 | NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_FELICA_MASK | \ |
124 | NFC_PROTO_ISO15693_MASK) | |
165063f1 | 125 | |
e6403b7c MG |
126 | #define TRF7970A_AUTOSUSPEND_DELAY 30000 /* 30 seconds */ |
127 | ||
165063f1 MG |
128 | #define TRF7970A_RX_SKB_ALLOC_SIZE 256 |
129 | ||
1568bfef | 130 | #define TRF7970A_FIFO_SIZE 127 |
165063f1 MG |
131 | |
132 | /* TX length is 3 nibbles long ==> 4KB - 1 bytes max */ | |
133 | #define TRF7970A_TX_MAX (4096 - 1) | |
134 | ||
1961843c | 135 | #define TRF7970A_WAIT_FOR_TX_IRQ 20 |
5fa3af35 | 136 | #define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 20 |
38b4eb1f MG |
137 | #define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 20 |
138 | #define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 40 | |
165063f1 | 139 | |
4e64eff8 MG |
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 | |
145 | ||
165063f1 MG |
146 | /* Quirks */ |
147 | /* Erratum: When reading IRQ Status register on trf7970a, we must issue a | |
148 | * read continuous command for IRQ Status and Collision Position registers. | |
149 | */ | |
772079eb | 150 | #define TRF7970A_QUIRK_IRQ_STATUS_READ BIT(0) |
95064bd9 | 151 | #define TRF7970A_QUIRK_EN2_MUST_STAY_LOW BIT(1) |
165063f1 MG |
152 | |
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 | |
851ee3cb MG |
168 | #define TRF7970A_CMD_TEST_INT_RF 0x18 |
169 | #define TRF7970A_CMD_TEST_EXT_RF 0x19 | |
165063f1 MG |
170 | #define TRF7970A_CMD_RX_GAIN_ADJUST 0x1a |
171 | ||
172 | /* Bits determining whether its a direct command or register R/W, | |
173 | * whether to use a continuous SPI transaction or not, and the actual | |
174 | * direct cmd opcode or regster address. | |
175 | */ | |
176 | #define TRF7970A_CMD_BIT_CTRL BIT(7) | |
177 | #define TRF7970A_CMD_BIT_RW BIT(6) | |
178 | #define TRF7970A_CMD_BIT_CONTINUOUS BIT(5) | |
179 | #define TRF7970A_CMD_BIT_OPCODE(opcode) ((opcode) & 0x1f) | |
180 | ||
181 | /* Registers addresses */ | |
182 | #define TRF7970A_CHIP_STATUS_CTRL 0x00 | |
183 | #define TRF7970A_ISO_CTRL 0x01 | |
184 | #define TRF7970A_ISO14443B_TX_OPTIONS 0x02 | |
185 | #define TRF7970A_ISO14443A_HIGH_BITRATE_OPTIONS 0x03 | |
186 | #define TRF7970A_TX_TIMER_SETTING_H_BYTE 0x04 | |
187 | #define TRF7970A_TX_TIMER_SETTING_L_BYTE 0x05 | |
188 | #define TRF7970A_TX_PULSE_LENGTH_CTRL 0x06 | |
189 | #define TRF7970A_RX_NO_RESPONSE_WAIT 0x07 | |
190 | #define TRF7970A_RX_WAIT_TIME 0x08 | |
191 | #define TRF7970A_MODULATOR_SYS_CLK_CTRL 0x09 | |
192 | #define TRF7970A_RX_SPECIAL_SETTINGS 0x0a | |
193 | #define TRF7970A_REG_IO_CTRL 0x0b | |
194 | #define TRF7970A_IRQ_STATUS 0x0c | |
195 | #define TRF7970A_COLLISION_IRQ_MASK 0x0d | |
196 | #define TRF7970A_COLLISION_POSITION 0x0e | |
197 | #define TRF7970A_RSSI_OSC_STATUS 0x0f | |
198 | #define TRF7970A_SPECIAL_FCN_REG1 0x10 | |
199 | #define TRF7970A_SPECIAL_FCN_REG2 0x11 | |
200 | #define TRF7970A_RAM1 0x12 | |
201 | #define TRF7970A_RAM2 0x13 | |
202 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS 0x14 | |
203 | #define TRF7970A_NFC_LOW_FIELD_LEVEL 0x16 | |
204 | #define TRF7970A_NFCID1 0x17 | |
205 | #define TRF7970A_NFC_TARGET_LEVEL 0x18 | |
206 | #define TRF79070A_NFC_TARGET_PROTOCOL 0x19 | |
207 | #define TRF7970A_TEST_REGISTER1 0x1a | |
208 | #define TRF7970A_TEST_REGISTER2 0x1b | |
209 | #define TRF7970A_FIFO_STATUS 0x1c | |
210 | #define TRF7970A_TX_LENGTH_BYTE1 0x1d | |
211 | #define TRF7970A_TX_LENGTH_BYTE2 0x1e | |
212 | #define TRF7970A_FIFO_IO_REGISTER 0x1f | |
213 | ||
214 | /* Chip Status Control Register Bits */ | |
215 | #define TRF7970A_CHIP_STATUS_VRS5_3 BIT(0) | |
216 | #define TRF7970A_CHIP_STATUS_REC_ON BIT(1) | |
217 | #define TRF7970A_CHIP_STATUS_AGC_ON BIT(2) | |
218 | #define TRF7970A_CHIP_STATUS_PM_ON BIT(3) | |
219 | #define TRF7970A_CHIP_STATUS_RF_PWR BIT(4) | |
220 | #define TRF7970A_CHIP_STATUS_RF_ON BIT(5) | |
221 | #define TRF7970A_CHIP_STATUS_DIRECT BIT(6) | |
222 | #define TRF7970A_CHIP_STATUS_STBY BIT(7) | |
223 | ||
224 | /* ISO Control Register Bits */ | |
225 | #define TRF7970A_ISO_CTRL_15693_SGL_1OF4_662 0x00 | |
226 | #define TRF7970A_ISO_CTRL_15693_SGL_1OF256_662 0x01 | |
227 | #define TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648 0x02 | |
228 | #define TRF7970A_ISO_CTRL_15693_SGL_1OF256_2648 0x03 | |
229 | #define TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a 0x04 | |
230 | #define TRF7970A_ISO_CTRL_15693_DBL_1OF256_667 0x05 | |
231 | #define TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669 0x06 | |
232 | #define TRF7970A_ISO_CTRL_15693_DBL_1OF256_2669 0x07 | |
233 | #define TRF7970A_ISO_CTRL_14443A_106 0x08 | |
234 | #define TRF7970A_ISO_CTRL_14443A_212 0x09 | |
235 | #define TRF7970A_ISO_CTRL_14443A_424 0x0a | |
236 | #define TRF7970A_ISO_CTRL_14443A_848 0x0b | |
237 | #define TRF7970A_ISO_CTRL_14443B_106 0x0c | |
238 | #define TRF7970A_ISO_CTRL_14443B_212 0x0d | |
239 | #define TRF7970A_ISO_CTRL_14443B_424 0x0e | |
240 | #define TRF7970A_ISO_CTRL_14443B_848 0x0f | |
241 | #define TRF7970A_ISO_CTRL_FELICA_212 0x1a | |
242 | #define TRF7970A_ISO_CTRL_FELICA_424 0x1b | |
243 | #define TRF7970A_ISO_CTRL_RFID BIT(5) | |
244 | #define TRF7970A_ISO_CTRL_DIR_MODE BIT(6) | |
245 | #define TRF7970A_ISO_CTRL_RX_CRC_N BIT(7) /* true == No CRC */ | |
246 | ||
247 | #define TRF7970A_ISO_CTRL_RFID_SPEED_MASK 0x1f | |
248 | ||
249 | /* Modulator and SYS_CLK Control Register Bits */ | |
250 | #define TRF7970A_MODULATOR_DEPTH(n) ((n) & 0x7) | |
251 | #define TRF7970A_MODULATOR_DEPTH_ASK10 (TRF7970A_MODULATOR_DEPTH(0)) | |
252 | #define TRF7970A_MODULATOR_DEPTH_OOK (TRF7970A_MODULATOR_DEPTH(1)) | |
253 | #define TRF7970A_MODULATOR_DEPTH_ASK7 (TRF7970A_MODULATOR_DEPTH(2)) | |
254 | #define TRF7970A_MODULATOR_DEPTH_ASK8_5 (TRF7970A_MODULATOR_DEPTH(3)) | |
255 | #define TRF7970A_MODULATOR_DEPTH_ASK13 (TRF7970A_MODULATOR_DEPTH(4)) | |
256 | #define TRF7970A_MODULATOR_DEPTH_ASK16 (TRF7970A_MODULATOR_DEPTH(5)) | |
257 | #define TRF7970A_MODULATOR_DEPTH_ASK22 (TRF7970A_MODULATOR_DEPTH(6)) | |
258 | #define TRF7970A_MODULATOR_DEPTH_ASK30 (TRF7970A_MODULATOR_DEPTH(7)) | |
259 | #define TRF7970A_MODULATOR_EN_ANA BIT(3) | |
260 | #define TRF7970A_MODULATOR_CLK(n) (((n) & 0x3) << 4) | |
261 | #define TRF7970A_MODULATOR_CLK_DISABLED (TRF7970A_MODULATOR_CLK(0)) | |
262 | #define TRF7970A_MODULATOR_CLK_3_6 (TRF7970A_MODULATOR_CLK(1)) | |
263 | #define TRF7970A_MODULATOR_CLK_6_13 (TRF7970A_MODULATOR_CLK(2)) | |
264 | #define TRF7970A_MODULATOR_CLK_13_27 (TRF7970A_MODULATOR_CLK(3)) | |
265 | #define TRF7970A_MODULATOR_EN_OOK BIT(6) | |
266 | #define TRF7970A_MODULATOR_27MHZ BIT(7) | |
267 | ||
268 | /* IRQ Status Register Bits */ | |
269 | #define TRF7970A_IRQ_STATUS_NORESP BIT(0) /* ISO15693 only */ | |
270 | #define TRF7970A_IRQ_STATUS_COL BIT(1) | |
271 | #define TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR BIT(2) | |
272 | #define TRF7970A_IRQ_STATUS_PARITY_ERROR BIT(3) | |
273 | #define TRF7970A_IRQ_STATUS_CRC_ERROR BIT(4) | |
274 | #define TRF7970A_IRQ_STATUS_FIFO BIT(5) | |
275 | #define TRF7970A_IRQ_STATUS_SRX BIT(6) | |
276 | #define TRF7970A_IRQ_STATUS_TX BIT(7) | |
277 | ||
278 | #define TRF7970A_IRQ_STATUS_ERROR \ | |
279 | (TRF7970A_IRQ_STATUS_COL | \ | |
280 | TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR | \ | |
281 | TRF7970A_IRQ_STATUS_PARITY_ERROR | \ | |
282 | TRF7970A_IRQ_STATUS_CRC_ERROR) | |
283 | ||
851ee3cb MG |
284 | #define TRF7970A_RSSI_OSC_STATUS_RSSI_MASK (BIT(2) | BIT(1) | BIT(0)) |
285 | #define TRF7970A_RSSI_OSC_STATUS_RSSI_X_MASK (BIT(5) | BIT(4) | BIT(3)) | |
286 | #define TRF7970A_RSSI_OSC_STATUS_RSSI_OSC_OK BIT(6) | |
287 | ||
165063f1 MG |
288 | #define TRF7970A_SPECIAL_FCN_REG1_COL_7_6 BIT(0) |
289 | #define TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL BIT(1) | |
290 | #define TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX BIT(2) | |
291 | #define TRF7970A_SPECIAL_FCN_REG1_SP_DIR_MODE BIT(3) | |
292 | #define TRF7970A_SPECIAL_FCN_REG1_NEXT_SLOT_37US BIT(4) | |
293 | #define TRF7970A_SPECIAL_FCN_REG1_PAR43 BIT(5) | |
294 | ||
295 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_124 (0x0 << 2) | |
296 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_120 (0x1 << 2) | |
297 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_112 (0x2 << 2) | |
298 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 (0x3 << 2) | |
299 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_4 0x0 | |
300 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_8 0x1 | |
301 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_16 0x2 | |
302 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32 0x3 | |
303 | ||
304 | #define TRF7970A_FIFO_STATUS_OVERFLOW BIT(7) | |
305 | ||
306 | /* NFC (ISO/IEC 14443A) Type 2 Tag commands */ | |
307 | #define NFC_T2T_CMD_READ 0x30 | |
308 | ||
9d9304b3 MG |
309 | /* ISO 15693 commands codes */ |
310 | #define ISO15693_CMD_INVENTORY 0x01 | |
311 | #define ISO15693_CMD_READ_SINGLE_BLOCK 0x20 | |
312 | #define ISO15693_CMD_WRITE_SINGLE_BLOCK 0x21 | |
313 | #define ISO15693_CMD_LOCK_BLOCK 0x22 | |
314 | #define ISO15693_CMD_READ_MULTIPLE_BLOCK 0x23 | |
315 | #define ISO15693_CMD_WRITE_MULTIPLE_BLOCK 0x24 | |
316 | #define ISO15693_CMD_SELECT 0x25 | |
317 | #define ISO15693_CMD_RESET_TO_READY 0x26 | |
318 | #define ISO15693_CMD_WRITE_AFI 0x27 | |
319 | #define ISO15693_CMD_LOCK_AFI 0x28 | |
320 | #define ISO15693_CMD_WRITE_DSFID 0x29 | |
321 | #define ISO15693_CMD_LOCK_DSFID 0x2a | |
322 | #define ISO15693_CMD_GET_SYSTEM_INFO 0x2b | |
323 | #define ISO15693_CMD_GET_MULTIPLE_BLOCK_SECURITY_STATUS 0x2c | |
324 | ||
325 | /* ISO 15693 request and response flags */ | |
326 | #define ISO15693_REQ_FLAG_SUB_CARRIER BIT(0) | |
327 | #define ISO15693_REQ_FLAG_DATA_RATE BIT(1) | |
328 | #define ISO15693_REQ_FLAG_INVENTORY BIT(2) | |
329 | #define ISO15693_REQ_FLAG_PROTOCOL_EXT BIT(3) | |
330 | #define ISO15693_REQ_FLAG_SELECT BIT(4) | |
331 | #define ISO15693_REQ_FLAG_AFI BIT(4) | |
332 | #define ISO15693_REQ_FLAG_ADDRESS BIT(5) | |
333 | #define ISO15693_REQ_FLAG_NB_SLOTS BIT(5) | |
334 | #define ISO15693_REQ_FLAG_OPTION BIT(6) | |
335 | ||
336 | #define ISO15693_REQ_FLAG_SPEED_MASK \ | |
337 | (ISO15693_REQ_FLAG_SUB_CARRIER | ISO15693_REQ_FLAG_DATA_RATE) | |
338 | ||
165063f1 | 339 | enum trf7970a_state { |
ceccd6aa | 340 | TRF7970A_ST_PWR_OFF, |
b5e17d9b | 341 | TRF7970A_ST_RF_OFF, |
165063f1 MG |
342 | TRF7970A_ST_IDLE, |
343 | TRF7970A_ST_IDLE_RX_BLOCKED, | |
344 | TRF7970A_ST_WAIT_FOR_TX_FIFO, | |
345 | TRF7970A_ST_WAIT_FOR_RX_DATA, | |
346 | TRF7970A_ST_WAIT_FOR_RX_DATA_CONT, | |
9d9304b3 | 347 | TRF7970A_ST_WAIT_TO_ISSUE_EOF, |
165063f1 MG |
348 | TRF7970A_ST_MAX |
349 | }; | |
350 | ||
351 | struct trf7970a { | |
352 | enum trf7970a_state state; | |
353 | struct device *dev; | |
354 | struct spi_device *spi; | |
355 | struct regulator *regulator; | |
356 | struct nfc_digital_dev *ddev; | |
357 | u32 quirks; | |
165063f1 MG |
358 | bool aborting; |
359 | struct sk_buff *tx_skb; | |
360 | struct sk_buff *rx_skb; | |
361 | nfc_digital_cmd_complete_t cb; | |
362 | void *cb_arg; | |
ebcc5a0d | 363 | u8 chip_status_ctrl; |
165063f1 | 364 | u8 iso_ctrl; |
49d19cc7 | 365 | u8 iso_ctrl_tech; |
12e9ade3 | 366 | u8 modulator_sys_clk_ctrl; |
165063f1 | 367 | u8 special_fcn_reg1; |
4e64eff8 | 368 | unsigned int guard_time; |
165063f1 MG |
369 | int technology; |
370 | int framing; | |
371 | u8 tx_cmd; | |
9d9304b3 | 372 | bool issue_eof; |
165063f1 MG |
373 | int en2_gpio; |
374 | int en_gpio; | |
375 | struct mutex lock; | |
376 | unsigned int timeout; | |
377 | bool ignore_timeout; | |
378 | struct delayed_work timeout_work; | |
379 | }; | |
380 | ||
381 | ||
382 | static int trf7970a_cmd(struct trf7970a *trf, u8 opcode) | |
383 | { | |
384 | u8 cmd = TRF7970A_CMD_BIT_CTRL | TRF7970A_CMD_BIT_OPCODE(opcode); | |
385 | int ret; | |
386 | ||
387 | dev_dbg(trf->dev, "cmd: 0x%x\n", cmd); | |
388 | ||
389 | ret = spi_write(trf->spi, &cmd, 1); | |
390 | if (ret) | |
391 | dev_err(trf->dev, "%s - cmd: 0x%x, ret: %d\n", __func__, cmd, | |
392 | ret); | |
393 | return ret; | |
394 | } | |
395 | ||
396 | static int trf7970a_read(struct trf7970a *trf, u8 reg, u8 *val) | |
397 | { | |
398 | u8 addr = TRF7970A_CMD_BIT_RW | reg; | |
399 | int ret; | |
400 | ||
401 | ret = spi_write_then_read(trf->spi, &addr, 1, val, 1); | |
402 | if (ret) | |
403 | dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr, | |
404 | ret); | |
405 | ||
406 | dev_dbg(trf->dev, "read(0x%x): 0x%x\n", addr, *val); | |
407 | ||
408 | return ret; | |
409 | } | |
410 | ||
3e7f3356 | 411 | static int trf7970a_read_cont(struct trf7970a *trf, u8 reg, u8 *buf, size_t len) |
165063f1 MG |
412 | { |
413 | u8 addr = reg | TRF7970A_CMD_BIT_RW | TRF7970A_CMD_BIT_CONTINUOUS; | |
3e7f3356 MG |
414 | struct spi_transfer t[2]; |
415 | struct spi_message m; | |
165063f1 MG |
416 | int ret; |
417 | ||
418 | dev_dbg(trf->dev, "read_cont(0x%x, %zd)\n", addr, len); | |
419 | ||
3e7f3356 MG |
420 | spi_message_init(&m); |
421 | ||
422 | memset(&t, 0, sizeof(t)); | |
423 | ||
424 | t[0].tx_buf = &addr; | |
425 | t[0].len = sizeof(addr); | |
426 | spi_message_add_tail(&t[0], &m); | |
427 | ||
428 | t[1].rx_buf = buf; | |
429 | t[1].len = len; | |
430 | spi_message_add_tail(&t[1], &m); | |
431 | ||
432 | ret = spi_sync(trf->spi, &m); | |
165063f1 MG |
433 | if (ret) |
434 | dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr, | |
435 | ret); | |
436 | return ret; | |
437 | } | |
438 | ||
439 | static int trf7970a_write(struct trf7970a *trf, u8 reg, u8 val) | |
440 | { | |
441 | u8 buf[2] = { reg, val }; | |
442 | int ret; | |
443 | ||
444 | dev_dbg(trf->dev, "write(0x%x): 0x%x\n", reg, val); | |
445 | ||
446 | ret = spi_write(trf->spi, buf, 2); | |
447 | if (ret) | |
448 | dev_err(trf->dev, "%s - write: 0x%x 0x%x, ret: %d\n", __func__, | |
449 | buf[0], buf[1], ret); | |
450 | ||
451 | return ret; | |
452 | } | |
453 | ||
454 | static int trf7970a_read_irqstatus(struct trf7970a *trf, u8 *status) | |
455 | { | |
456 | int ret; | |
457 | u8 buf[2]; | |
458 | u8 addr; | |
459 | ||
460 | addr = TRF7970A_IRQ_STATUS | TRF7970A_CMD_BIT_RW; | |
461 | ||
772079eb | 462 | if (trf->quirks & TRF7970A_QUIRK_IRQ_STATUS_READ) { |
165063f1 MG |
463 | addr |= TRF7970A_CMD_BIT_CONTINUOUS; |
464 | ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2); | |
465 | } else { | |
466 | ret = spi_write_then_read(trf->spi, &addr, 1, buf, 1); | |
467 | } | |
468 | ||
469 | if (ret) | |
470 | dev_err(trf->dev, "%s - irqstatus: Status read failed: %d\n", | |
471 | __func__, ret); | |
472 | else | |
473 | *status = buf[0]; | |
474 | ||
475 | return ret; | |
476 | } | |
477 | ||
478 | static void trf7970a_send_upstream(struct trf7970a *trf) | |
479 | { | |
165063f1 MG |
480 | dev_kfree_skb_any(trf->tx_skb); |
481 | trf->tx_skb = NULL; | |
482 | ||
483 | if (trf->rx_skb && !IS_ERR(trf->rx_skb) && !trf->aborting) | |
484 | print_hex_dump_debug("trf7970a rx data: ", DUMP_PREFIX_NONE, | |
485 | 16, 1, trf->rx_skb->data, trf->rx_skb->len, | |
486 | false); | |
487 | ||
165063f1 MG |
488 | trf->state = TRF7970A_ST_IDLE; |
489 | ||
490 | if (trf->aborting) { | |
491 | dev_dbg(trf->dev, "Abort process complete\n"); | |
492 | ||
493 | if (!IS_ERR(trf->rx_skb)) { | |
494 | kfree_skb(trf->rx_skb); | |
495 | trf->rx_skb = ERR_PTR(-ECANCELED); | |
496 | } | |
497 | ||
498 | trf->aborting = false; | |
499 | } | |
500 | ||
501 | trf->cb(trf->ddev, trf->cb_arg, trf->rx_skb); | |
502 | ||
503 | trf->rx_skb = NULL; | |
504 | } | |
505 | ||
506 | static void trf7970a_send_err_upstream(struct trf7970a *trf, int errno) | |
507 | { | |
508 | dev_dbg(trf->dev, "Error - state: %d, errno: %d\n", trf->state, errno); | |
509 | ||
6fb9edcb MG |
510 | cancel_delayed_work(&trf->timeout_work); |
511 | ||
165063f1 MG |
512 | kfree_skb(trf->rx_skb); |
513 | trf->rx_skb = ERR_PTR(errno); | |
514 | ||
515 | trf7970a_send_upstream(trf); | |
516 | } | |
517 | ||
518 | static int trf7970a_transmit(struct trf7970a *trf, struct sk_buff *skb, | |
7a1e5552 | 519 | unsigned int len, u8 *prefix, unsigned int prefix_len) |
165063f1 | 520 | { |
7a1e5552 MG |
521 | struct spi_transfer t[2]; |
522 | struct spi_message m; | |
165063f1 MG |
523 | unsigned int timeout; |
524 | int ret; | |
525 | ||
526 | print_hex_dump_debug("trf7970a tx data: ", DUMP_PREFIX_NONE, | |
527 | 16, 1, skb->data, len, false); | |
528 | ||
7a1e5552 MG |
529 | spi_message_init(&m); |
530 | ||
531 | memset(&t, 0, sizeof(t)); | |
532 | ||
533 | t[0].tx_buf = prefix; | |
534 | t[0].len = prefix_len; | |
535 | spi_message_add_tail(&t[0], &m); | |
536 | ||
537 | t[1].tx_buf = skb->data; | |
538 | t[1].len = len; | |
539 | spi_message_add_tail(&t[1], &m); | |
540 | ||
541 | ret = spi_sync(trf->spi, &m); | |
165063f1 MG |
542 | if (ret) { |
543 | dev_err(trf->dev, "%s - Can't send tx data: %d\n", __func__, | |
544 | ret); | |
545 | return ret; | |
546 | } | |
547 | ||
548 | skb_pull(skb, len); | |
549 | ||
550 | if (skb->len > 0) { | |
551 | trf->state = TRF7970A_ST_WAIT_FOR_TX_FIFO; | |
552 | timeout = TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT; | |
553 | } else { | |
9d9304b3 MG |
554 | if (trf->issue_eof) { |
555 | trf->state = TRF7970A_ST_WAIT_TO_ISSUE_EOF; | |
556 | timeout = TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF; | |
557 | } else { | |
558 | trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA; | |
1961843c MG |
559 | |
560 | if (!trf->timeout) | |
561 | timeout = TRF7970A_WAIT_FOR_TX_IRQ; | |
562 | else | |
563 | timeout = trf->timeout; | |
9d9304b3 | 564 | } |
165063f1 MG |
565 | } |
566 | ||
567 | dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", timeout, | |
568 | trf->state); | |
569 | ||
570 | schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout)); | |
571 | ||
572 | return 0; | |
573 | } | |
574 | ||
575 | static void trf7970a_fill_fifo(struct trf7970a *trf) | |
576 | { | |
577 | struct sk_buff *skb = trf->tx_skb; | |
578 | unsigned int len; | |
579 | int ret; | |
580 | u8 fifo_bytes; | |
7a1e5552 | 581 | u8 prefix; |
165063f1 MG |
582 | |
583 | ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes); | |
584 | if (ret) { | |
585 | trf7970a_send_err_upstream(trf, ret); | |
586 | return; | |
587 | } | |
588 | ||
589 | dev_dbg(trf->dev, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes); | |
590 | ||
4542e834 | 591 | fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW; |
165063f1 MG |
592 | |
593 | /* Calculate how much more data can be written to the fifo */ | |
594 | len = TRF7970A_FIFO_SIZE - fifo_bytes; | |
0e840ed5 MG |
595 | if (!len) { |
596 | schedule_delayed_work(&trf->timeout_work, | |
597 | msecs_to_jiffies(TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT)); | |
598 | return; | |
599 | } | |
600 | ||
165063f1 MG |
601 | len = min(skb->len, len); |
602 | ||
7a1e5552 MG |
603 | prefix = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_FIFO_IO_REGISTER; |
604 | ||
605 | ret = trf7970a_transmit(trf, skb, len, &prefix, sizeof(prefix)); | |
165063f1 MG |
606 | if (ret) |
607 | trf7970a_send_err_upstream(trf, ret); | |
608 | } | |
609 | ||
610 | static void trf7970a_drain_fifo(struct trf7970a *trf, u8 status) | |
611 | { | |
612 | struct sk_buff *skb = trf->rx_skb; | |
613 | int ret; | |
614 | u8 fifo_bytes; | |
615 | ||
616 | if (status & TRF7970A_IRQ_STATUS_ERROR) { | |
617 | trf7970a_send_err_upstream(trf, -EIO); | |
618 | return; | |
619 | } | |
620 | ||
621 | ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes); | |
622 | if (ret) { | |
623 | trf7970a_send_err_upstream(trf, ret); | |
624 | return; | |
625 | } | |
626 | ||
627 | dev_dbg(trf->dev, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes); | |
628 | ||
4542e834 MG |
629 | fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW; |
630 | ||
165063f1 MG |
631 | if (!fifo_bytes) |
632 | goto no_rx_data; | |
633 | ||
165063f1 MG |
634 | if (fifo_bytes > skb_tailroom(skb)) { |
635 | skb = skb_copy_expand(skb, skb_headroom(skb), | |
636 | max_t(int, fifo_bytes, | |
637 | TRF7970A_RX_SKB_ALLOC_SIZE), | |
638 | GFP_KERNEL); | |
639 | if (!skb) { | |
640 | trf7970a_send_err_upstream(trf, -ENOMEM); | |
641 | return; | |
642 | } | |
643 | ||
644 | kfree_skb(trf->rx_skb); | |
645 | trf->rx_skb = skb; | |
646 | } | |
647 | ||
648 | ret = trf7970a_read_cont(trf, TRF7970A_FIFO_IO_REGISTER, | |
649 | skb_put(skb, fifo_bytes), fifo_bytes); | |
650 | if (ret) { | |
651 | trf7970a_send_err_upstream(trf, ret); | |
652 | return; | |
653 | } | |
654 | ||
655 | /* If received Type 2 ACK/NACK, shift right 4 bits and pass up */ | |
656 | if ((trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T) && (skb->len == 1) && | |
657 | (trf->special_fcn_reg1 == | |
658 | TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX)) { | |
659 | skb->data[0] >>= 4; | |
660 | status = TRF7970A_IRQ_STATUS_SRX; | |
661 | } else { | |
662 | trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA_CONT; | |
5d8f7594 MG |
663 | |
664 | ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes); | |
665 | if (ret) { | |
666 | trf7970a_send_err_upstream(trf, ret); | |
667 | return; | |
668 | } | |
669 | ||
670 | fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW; | |
671 | ||
672 | /* If there are bytes in the FIFO, set status to '0' so | |
673 | * the if stmt below doesn't fire and the driver will wait | |
674 | * for the trf7970a to generate another RX interrupt. | |
675 | */ | |
676 | if (fifo_bytes) | |
677 | status = 0; | |
165063f1 MG |
678 | } |
679 | ||
680 | no_rx_data: | |
681 | if (status == TRF7970A_IRQ_STATUS_SRX) { /* Receive complete */ | |
682 | trf7970a_send_upstream(trf); | |
683 | return; | |
684 | } | |
685 | ||
686 | dev_dbg(trf->dev, "Setting timeout for %d ms\n", | |
687 | TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT); | |
688 | ||
689 | schedule_delayed_work(&trf->timeout_work, | |
690 | msecs_to_jiffies(TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT)); | |
691 | } | |
692 | ||
693 | static irqreturn_t trf7970a_irq(int irq, void *dev_id) | |
694 | { | |
695 | struct trf7970a *trf = dev_id; | |
696 | int ret; | |
bece3c54 | 697 | u8 status, fifo_bytes; |
165063f1 MG |
698 | |
699 | mutex_lock(&trf->lock); | |
700 | ||
b5e17d9b | 701 | if (trf->state == TRF7970A_ST_RF_OFF) { |
165063f1 MG |
702 | mutex_unlock(&trf->lock); |
703 | return IRQ_NONE; | |
704 | } | |
705 | ||
706 | ret = trf7970a_read_irqstatus(trf, &status); | |
707 | if (ret) { | |
708 | mutex_unlock(&trf->lock); | |
709 | return IRQ_NONE; | |
710 | } | |
711 | ||
712 | dev_dbg(trf->dev, "IRQ - state: %d, status: 0x%x\n", trf->state, | |
713 | status); | |
714 | ||
715 | if (!status) { | |
716 | mutex_unlock(&trf->lock); | |
717 | return IRQ_NONE; | |
718 | } | |
719 | ||
720 | switch (trf->state) { | |
721 | case TRF7970A_ST_IDLE: | |
722 | case TRF7970A_ST_IDLE_RX_BLOCKED: | |
723 | /* If getting interrupts caused by RF noise, turn off the | |
724 | * receiver to avoid unnecessary interrupts. It will be | |
725 | * turned back on in trf7970a_in_send_cmd() when the next | |
726 | * command is issued. | |
727 | */ | |
728 | if (status & TRF7970A_IRQ_STATUS_ERROR) { | |
729 | trf7970a_cmd(trf, TRF7970A_CMD_BLOCK_RX); | |
730 | trf->state = TRF7970A_ST_IDLE_RX_BLOCKED; | |
731 | } | |
732 | ||
733 | trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); | |
734 | break; | |
735 | case TRF7970A_ST_WAIT_FOR_TX_FIFO: | |
736 | if (status & TRF7970A_IRQ_STATUS_TX) { | |
737 | trf->ignore_timeout = | |
738 | !cancel_delayed_work(&trf->timeout_work); | |
739 | trf7970a_fill_fifo(trf); | |
740 | } else { | |
741 | trf7970a_send_err_upstream(trf, -EIO); | |
742 | } | |
743 | break; | |
744 | case TRF7970A_ST_WAIT_FOR_RX_DATA: | |
745 | case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: | |
746 | if (status & TRF7970A_IRQ_STATUS_SRX) { | |
747 | trf->ignore_timeout = | |
748 | !cancel_delayed_work(&trf->timeout_work); | |
749 | trf7970a_drain_fifo(trf, status); | |
bece3c54 MG |
750 | } else if (status & TRF7970A_IRQ_STATUS_FIFO) { |
751 | ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, | |
752 | &fifo_bytes); | |
753 | ||
754 | fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW; | |
755 | ||
756 | if (ret) | |
757 | trf7970a_send_err_upstream(trf, ret); | |
758 | else if (!fifo_bytes) | |
759 | trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); | |
4dd836e4 MG |
760 | } else if (status == TRF7970A_IRQ_STATUS_TX) { |
761 | trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); | |
1961843c MG |
762 | |
763 | if (!trf->timeout) { | |
764 | trf->ignore_timeout = !cancel_delayed_work( | |
765 | &trf->timeout_work); | |
766 | trf->rx_skb = ERR_PTR(0); | |
767 | trf7970a_send_upstream(trf); | |
768 | break; | |
769 | } | |
4dd836e4 | 770 | } else { |
165063f1 MG |
771 | trf7970a_send_err_upstream(trf, -EIO); |
772 | } | |
773 | break; | |
9d9304b3 MG |
774 | case TRF7970A_ST_WAIT_TO_ISSUE_EOF: |
775 | if (status != TRF7970A_IRQ_STATUS_TX) | |
776 | trf7970a_send_err_upstream(trf, -EIO); | |
777 | break; | |
165063f1 MG |
778 | default: |
779 | dev_err(trf->dev, "%s - Driver in invalid state: %d\n", | |
780 | __func__, trf->state); | |
781 | } | |
782 | ||
783 | mutex_unlock(&trf->lock); | |
784 | return IRQ_HANDLED; | |
785 | } | |
786 | ||
9d9304b3 MG |
787 | static void trf7970a_issue_eof(struct trf7970a *trf) |
788 | { | |
789 | int ret; | |
790 | ||
791 | dev_dbg(trf->dev, "Issuing EOF\n"); | |
792 | ||
793 | ret = trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); | |
794 | if (ret) | |
795 | trf7970a_send_err_upstream(trf, ret); | |
796 | ||
797 | ret = trf7970a_cmd(trf, TRF7970A_CMD_EOF); | |
798 | if (ret) | |
799 | trf7970a_send_err_upstream(trf, ret); | |
800 | ||
801 | trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA; | |
802 | ||
803 | dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", | |
804 | trf->timeout, trf->state); | |
805 | ||
806 | schedule_delayed_work(&trf->timeout_work, | |
807 | msecs_to_jiffies(trf->timeout)); | |
808 | } | |
809 | ||
165063f1 MG |
810 | static void trf7970a_timeout_work_handler(struct work_struct *work) |
811 | { | |
812 | struct trf7970a *trf = container_of(work, struct trf7970a, | |
813 | timeout_work.work); | |
814 | ||
815 | dev_dbg(trf->dev, "Timeout - state: %d, ignore_timeout: %d\n", | |
816 | trf->state, trf->ignore_timeout); | |
817 | ||
818 | mutex_lock(&trf->lock); | |
819 | ||
820 | if (trf->ignore_timeout) | |
821 | trf->ignore_timeout = false; | |
822 | else if (trf->state == TRF7970A_ST_WAIT_FOR_RX_DATA_CONT) | |
afa5b5f1 | 823 | trf7970a_drain_fifo(trf, TRF7970A_IRQ_STATUS_SRX); |
9d9304b3 MG |
824 | else if (trf->state == TRF7970A_ST_WAIT_TO_ISSUE_EOF) |
825 | trf7970a_issue_eof(trf); | |
165063f1 MG |
826 | else |
827 | trf7970a_send_err_upstream(trf, -ETIMEDOUT); | |
828 | ||
829 | mutex_unlock(&trf->lock); | |
830 | } | |
831 | ||
832 | static int trf7970a_init(struct trf7970a *trf) | |
833 | { | |
834 | int ret; | |
835 | ||
836 | dev_dbg(trf->dev, "Initializing device - state: %d\n", trf->state); | |
837 | ||
838 | ret = trf7970a_cmd(trf, TRF7970A_CMD_SOFT_INIT); | |
839 | if (ret) | |
840 | goto err_out; | |
841 | ||
842 | ret = trf7970a_cmd(trf, TRF7970A_CMD_IDLE); | |
843 | if (ret) | |
844 | goto err_out; | |
845 | ||
4e007f81 MG |
846 | usleep_range(1000, 2000); |
847 | ||
7149d6bf MG |
848 | trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON; |
849 | ||
6c08df42 MG |
850 | ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL, 0); |
851 | if (ret) | |
852 | goto err_out; | |
853 | ||
854 | trf->modulator_sys_clk_ctrl = 0; | |
855 | ||
b887eb09 MG |
856 | /* Must clear NFC Target Detection Level reg due to erratum */ |
857 | ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0); | |
858 | if (ret) | |
859 | goto err_out; | |
860 | ||
165063f1 MG |
861 | ret = trf7970a_write(trf, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS, |
862 | TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 | | |
863 | TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32); | |
864 | if (ret) | |
865 | goto err_out; | |
866 | ||
867 | ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, 0); | |
868 | if (ret) | |
869 | goto err_out; | |
870 | ||
871 | trf->special_fcn_reg1 = 0; | |
872 | ||
49d19cc7 | 873 | trf->iso_ctrl = 0xff; |
165063f1 MG |
874 | return 0; |
875 | ||
876 | err_out: | |
877 | dev_dbg(trf->dev, "Couldn't init device: %d\n", ret); | |
878 | return ret; | |
879 | } | |
880 | ||
881 | static void trf7970a_switch_rf_off(struct trf7970a *trf) | |
882 | { | |
cfc708db MG |
883 | if ((trf->state == TRF7970A_ST_PWR_OFF) || |
884 | (trf->state == TRF7970A_ST_RF_OFF)) | |
885 | return; | |
886 | ||
165063f1 MG |
887 | dev_dbg(trf->dev, "Switching rf off\n"); |
888 | ||
a1d2dc5b MG |
889 | trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON; |
890 | ||
891 | trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, trf->chip_status_ctrl); | |
892 | ||
165063f1 | 893 | trf->aborting = false; |
b5e17d9b | 894 | trf->state = TRF7970A_ST_RF_OFF; |
e6403b7c MG |
895 | |
896 | pm_runtime_mark_last_busy(trf->dev); | |
897 | pm_runtime_put_autosuspend(trf->dev); | |
165063f1 MG |
898 | } |
899 | ||
0a1de842 | 900 | static int trf7970a_switch_rf_on(struct trf7970a *trf) |
165063f1 | 901 | { |
a08e5454 MG |
902 | int ret; |
903 | ||
165063f1 MG |
904 | dev_dbg(trf->dev, "Switching rf on\n"); |
905 | ||
e6403b7c | 906 | pm_runtime_get_sync(trf->dev); |
165063f1 | 907 | |
ceccd6aa MG |
908 | if (trf->state != TRF7970A_ST_RF_OFF) { /* Power on, RF off */ |
909 | dev_err(trf->dev, "%s - Incorrect state: %d\n", __func__, | |
910 | trf->state); | |
911 | return -EINVAL; | |
912 | } | |
913 | ||
a08e5454 MG |
914 | ret = trf7970a_init(trf); |
915 | if (ret) { | |
916 | dev_err(trf->dev, "%s - Can't initialize: %d\n", __func__, ret); | |
0a1de842 | 917 | return ret; |
a08e5454 MG |
918 | } |
919 | ||
e6403b7c | 920 | trf->state = TRF7970A_ST_IDLE; |
0a1de842 MG |
921 | |
922 | return 0; | |
165063f1 MG |
923 | } |
924 | ||
925 | static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on) | |
926 | { | |
927 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
0a1de842 | 928 | int ret = 0; |
165063f1 MG |
929 | |
930 | dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on); | |
931 | ||
932 | mutex_lock(&trf->lock); | |
933 | ||
934 | if (on) { | |
935 | switch (trf->state) { | |
ceccd6aa | 936 | case TRF7970A_ST_PWR_OFF: |
b5e17d9b | 937 | case TRF7970A_ST_RF_OFF: |
0a1de842 | 938 | ret = trf7970a_switch_rf_on(trf); |
165063f1 MG |
939 | break; |
940 | case TRF7970A_ST_IDLE: | |
941 | case TRF7970A_ST_IDLE_RX_BLOCKED: | |
942 | break; | |
943 | default: | |
944 | dev_err(trf->dev, "%s - Invalid request: %d %d\n", | |
945 | __func__, trf->state, on); | |
946 | trf7970a_switch_rf_off(trf); | |
0a1de842 | 947 | ret = -EINVAL; |
165063f1 MG |
948 | } |
949 | } else { | |
950 | switch (trf->state) { | |
ceccd6aa | 951 | case TRF7970A_ST_PWR_OFF: |
b5e17d9b | 952 | case TRF7970A_ST_RF_OFF: |
165063f1 MG |
953 | break; |
954 | default: | |
955 | dev_err(trf->dev, "%s - Invalid request: %d %d\n", | |
956 | __func__, trf->state, on); | |
0a1de842 | 957 | ret = -EINVAL; |
165063f1 MG |
958 | /* FALLTHROUGH */ |
959 | case TRF7970A_ST_IDLE: | |
960 | case TRF7970A_ST_IDLE_RX_BLOCKED: | |
961 | trf7970a_switch_rf_off(trf); | |
962 | } | |
963 | } | |
964 | ||
965 | mutex_unlock(&trf->lock); | |
0a1de842 | 966 | return ret; |
165063f1 MG |
967 | } |
968 | ||
307e5caf | 969 | static int trf7970a_in_config_rf_tech(struct trf7970a *trf, int tech) |
165063f1 MG |
970 | { |
971 | int ret = 0; | |
972 | ||
973 | dev_dbg(trf->dev, "rf technology: %d\n", tech); | |
974 | ||
975 | switch (tech) { | |
976 | case NFC_DIGITAL_RF_TECH_106A: | |
49d19cc7 | 977 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443A_106; |
12e9ade3 | 978 | trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK; |
4e64eff8 | 979 | trf->guard_time = TRF7970A_GUARD_TIME_NFCA; |
165063f1 | 980 | break; |
742b1f9f MG |
981 | case NFC_DIGITAL_RF_TECH_106B: |
982 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443B_106; | |
983 | trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10; | |
4e64eff8 | 984 | trf->guard_time = TRF7970A_GUARD_TIME_NFCB; |
742b1f9f | 985 | break; |
6857bb96 MG |
986 | case NFC_DIGITAL_RF_TECH_212F: |
987 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_212; | |
988 | trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10; | |
4e64eff8 | 989 | trf->guard_time = TRF7970A_GUARD_TIME_NFCF; |
6857bb96 MG |
990 | break; |
991 | case NFC_DIGITAL_RF_TECH_424F: | |
992 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_424; | |
993 | trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10; | |
4e64eff8 | 994 | trf->guard_time = TRF7970A_GUARD_TIME_NFCF; |
6857bb96 | 995 | break; |
9d9304b3 | 996 | case NFC_DIGITAL_RF_TECH_ISO15693: |
49d19cc7 | 997 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648; |
12e9ade3 | 998 | trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK; |
4e64eff8 | 999 | trf->guard_time = TRF7970A_GUARD_TIME_15693; |
9d9304b3 | 1000 | break; |
165063f1 MG |
1001 | default: |
1002 | dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech); | |
1003 | return -EINVAL; | |
1004 | } | |
1005 | ||
1006 | trf->technology = tech; | |
1007 | ||
1008 | return ret; | |
1009 | } | |
1010 | ||
851ee3cb MG |
1011 | static int trf7970a_is_rf_field(struct trf7970a *trf, bool *is_rf_field) |
1012 | { | |
1013 | int ret; | |
1014 | u8 rssi; | |
1015 | ||
1016 | ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, | |
1017 | trf->chip_status_ctrl | TRF7970A_CHIP_STATUS_REC_ON); | |
1018 | if (ret) | |
1019 | return ret; | |
1020 | ||
1021 | ret = trf7970a_cmd(trf, TRF7970A_CMD_TEST_EXT_RF); | |
1022 | if (ret) | |
1023 | return ret; | |
1024 | ||
1025 | usleep_range(50, 60); | |
1026 | ||
1027 | ret = trf7970a_read(trf, TRF7970A_RSSI_OSC_STATUS, &rssi); | |
1028 | if (ret) | |
1029 | return ret; | |
1030 | ||
1031 | ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, | |
1032 | trf->chip_status_ctrl); | |
1033 | if (ret) | |
1034 | return ret; | |
1035 | ||
1036 | if (rssi & TRF7970A_RSSI_OSC_STATUS_RSSI_MASK) | |
1037 | *is_rf_field = true; | |
1038 | else | |
1039 | *is_rf_field = false; | |
1040 | ||
1041 | return 0; | |
1042 | } | |
1043 | ||
307e5caf | 1044 | static int trf7970a_in_config_framing(struct trf7970a *trf, int framing) |
165063f1 | 1045 | { |
49d19cc7 | 1046 | u8 iso_ctrl = trf->iso_ctrl_tech; |
851ee3cb | 1047 | bool is_rf_field = false; |
49d19cc7 MG |
1048 | int ret; |
1049 | ||
165063f1 MG |
1050 | dev_dbg(trf->dev, "framing: %d\n", framing); |
1051 | ||
1052 | switch (framing) { | |
1053 | case NFC_DIGITAL_FRAMING_NFCA_SHORT: | |
1054 | case NFC_DIGITAL_FRAMING_NFCA_STANDARD: | |
1055 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC; | |
49d19cc7 | 1056 | iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N; |
165063f1 MG |
1057 | break; |
1058 | case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A: | |
80062891 | 1059 | case NFC_DIGITAL_FRAMING_NFCA_T4T: |
742b1f9f MG |
1060 | case NFC_DIGITAL_FRAMING_NFCB: |
1061 | case NFC_DIGITAL_FRAMING_NFCB_T4T: | |
6857bb96 MG |
1062 | case NFC_DIGITAL_FRAMING_NFCF: |
1063 | case NFC_DIGITAL_FRAMING_NFCF_T3T: | |
9d9304b3 MG |
1064 | case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY: |
1065 | case NFC_DIGITAL_FRAMING_ISO15693_T5T: | |
165063f1 | 1066 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT; |
49d19cc7 | 1067 | iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N; |
165063f1 MG |
1068 | break; |
1069 | case NFC_DIGITAL_FRAMING_NFCA_T2T: | |
1070 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT; | |
49d19cc7 | 1071 | iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N; |
165063f1 MG |
1072 | break; |
1073 | default: | |
1074 | dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing); | |
1075 | return -EINVAL; | |
1076 | } | |
1077 | ||
1078 | trf->framing = framing; | |
1079 | ||
851ee3cb MG |
1080 | if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) { |
1081 | ret = trf7970a_is_rf_field(trf, &is_rf_field); | |
1082 | if (ret) | |
1083 | return ret; | |
1084 | ||
1085 | if (is_rf_field) | |
1086 | return -EBUSY; | |
1087 | } | |
1088 | ||
49d19cc7 MG |
1089 | if (iso_ctrl != trf->iso_ctrl) { |
1090 | ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl); | |
1091 | if (ret) | |
1092 | return ret; | |
1093 | ||
1094 | trf->iso_ctrl = iso_ctrl; | |
a0822a7e MG |
1095 | |
1096 | ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL, | |
12e9ade3 | 1097 | trf->modulator_sys_clk_ctrl); |
a0822a7e MG |
1098 | if (ret) |
1099 | return ret; | |
49d19cc7 MG |
1100 | } |
1101 | ||
a1d2dc5b MG |
1102 | if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) { |
1103 | ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, | |
1104 | trf->chip_status_ctrl | | |
1105 | TRF7970A_CHIP_STATUS_RF_ON); | |
1106 | if (ret) | |
1107 | return ret; | |
1108 | ||
1109 | trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON; | |
1110 | ||
4e64eff8 | 1111 | usleep_range(trf->guard_time, trf->guard_time + 1000); |
a1d2dc5b MG |
1112 | } |
1113 | ||
49d19cc7 | 1114 | return 0; |
165063f1 MG |
1115 | } |
1116 | ||
1117 | static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type, | |
1118 | int param) | |
1119 | { | |
1120 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
18422e68 | 1121 | int ret; |
165063f1 MG |
1122 | |
1123 | dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param); | |
1124 | ||
1125 | mutex_lock(&trf->lock); | |
1126 | ||
ceccd6aa MG |
1127 | if ((trf->state == TRF7970A_ST_PWR_OFF) || |
1128 | (trf->state == TRF7970A_ST_RF_OFF)) { | |
0a1de842 MG |
1129 | ret = trf7970a_switch_rf_on(trf); |
1130 | if (ret) | |
1131 | goto err_unlock; | |
1132 | } | |
165063f1 MG |
1133 | |
1134 | switch (type) { | |
1135 | case NFC_DIGITAL_CONFIG_RF_TECH: | |
307e5caf | 1136 | ret = trf7970a_in_config_rf_tech(trf, param); |
165063f1 MG |
1137 | break; |
1138 | case NFC_DIGITAL_CONFIG_FRAMING: | |
307e5caf | 1139 | ret = trf7970a_in_config_framing(trf, param); |
165063f1 MG |
1140 | break; |
1141 | default: | |
1142 | dev_dbg(trf->dev, "Unknown type: %d\n", type); | |
1143 | ret = -EINVAL; | |
1144 | } | |
1145 | ||
0a1de842 | 1146 | err_unlock: |
165063f1 MG |
1147 | mutex_unlock(&trf->lock); |
1148 | return ret; | |
1149 | } | |
1150 | ||
9d9304b3 MG |
1151 | static int trf7970a_is_iso15693_write_or_lock(u8 cmd) |
1152 | { | |
1153 | switch (cmd) { | |
1154 | case ISO15693_CMD_WRITE_SINGLE_BLOCK: | |
1155 | case ISO15693_CMD_LOCK_BLOCK: | |
1156 | case ISO15693_CMD_WRITE_MULTIPLE_BLOCK: | |
1157 | case ISO15693_CMD_WRITE_AFI: | |
1158 | case ISO15693_CMD_LOCK_AFI: | |
1159 | case ISO15693_CMD_WRITE_DSFID: | |
1160 | case ISO15693_CMD_LOCK_DSFID: | |
1161 | return 1; | |
1162 | break; | |
1163 | default: | |
1164 | return 0; | |
1165 | } | |
1166 | } | |
1167 | ||
165063f1 MG |
1168 | static int trf7970a_per_cmd_config(struct trf7970a *trf, struct sk_buff *skb) |
1169 | { | |
1170 | u8 *req = skb->data; | |
9d9304b3 | 1171 | u8 special_fcn_reg1, iso_ctrl; |
165063f1 MG |
1172 | int ret; |
1173 | ||
9d9304b3 MG |
1174 | trf->issue_eof = false; |
1175 | ||
165063f1 MG |
1176 | /* When issuing Type 2 read command, make sure the '4_bit_RX' bit in |
1177 | * special functions register 1 is cleared; otherwise, its a write or | |
1178 | * sector select command and '4_bit_RX' must be set. | |
9d9304b3 MG |
1179 | * |
1180 | * When issuing an ISO 15693 command, inspect the flags byte to see | |
1181 | * what speed to use. Also, remember if the OPTION flag is set on | |
1182 | * a Type 5 write or lock command so the driver will know that it | |
1183 | * has to send an EOF in order to get a response. | |
165063f1 MG |
1184 | */ |
1185 | if ((trf->technology == NFC_DIGITAL_RF_TECH_106A) && | |
1186 | (trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T)) { | |
1187 | if (req[0] == NFC_T2T_CMD_READ) | |
1188 | special_fcn_reg1 = 0; | |
1189 | else | |
1190 | special_fcn_reg1 = TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX; | |
1191 | ||
1192 | if (special_fcn_reg1 != trf->special_fcn_reg1) { | |
1193 | ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, | |
1194 | special_fcn_reg1); | |
1195 | if (ret) | |
1196 | return ret; | |
1197 | ||
1198 | trf->special_fcn_reg1 = special_fcn_reg1; | |
1199 | } | |
9d9304b3 MG |
1200 | } else if (trf->technology == NFC_DIGITAL_RF_TECH_ISO15693) { |
1201 | iso_ctrl = trf->iso_ctrl & ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK; | |
1202 | ||
1203 | switch (req[0] & ISO15693_REQ_FLAG_SPEED_MASK) { | |
1204 | case 0x00: | |
1205 | iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_662; | |
1206 | break; | |
1207 | case ISO15693_REQ_FLAG_SUB_CARRIER: | |
1208 | iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a; | |
1209 | break; | |
1210 | case ISO15693_REQ_FLAG_DATA_RATE: | |
1211 | iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648; | |
1212 | break; | |
1213 | case (ISO15693_REQ_FLAG_SUB_CARRIER | | |
1214 | ISO15693_REQ_FLAG_DATA_RATE): | |
1215 | iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669; | |
1216 | break; | |
1217 | } | |
1218 | ||
1219 | if (iso_ctrl != trf->iso_ctrl) { | |
1220 | ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl); | |
1221 | if (ret) | |
1222 | return ret; | |
1223 | ||
1224 | trf->iso_ctrl = iso_ctrl; | |
1225 | } | |
1226 | ||
1227 | if ((trf->framing == NFC_DIGITAL_FRAMING_ISO15693_T5T) && | |
1228 | trf7970a_is_iso15693_write_or_lock(req[1]) && | |
1229 | (req[0] & ISO15693_REQ_FLAG_OPTION)) | |
1230 | trf->issue_eof = true; | |
165063f1 MG |
1231 | } |
1232 | ||
1233 | return 0; | |
1234 | } | |
1235 | ||
1236 | static int trf7970a_in_send_cmd(struct nfc_digital_dev *ddev, | |
1237 | struct sk_buff *skb, u16 timeout, | |
1238 | nfc_digital_cmd_complete_t cb, void *arg) | |
1239 | { | |
1240 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
7a1e5552 | 1241 | u8 prefix[5]; |
165063f1 MG |
1242 | unsigned int len; |
1243 | int ret; | |
aff0564a | 1244 | u8 status; |
165063f1 MG |
1245 | |
1246 | dev_dbg(trf->dev, "New request - state: %d, timeout: %d ms, len: %d\n", | |
1247 | trf->state, timeout, skb->len); | |
1248 | ||
1249 | if (skb->len > TRF7970A_TX_MAX) | |
1250 | return -EINVAL; | |
1251 | ||
1252 | mutex_lock(&trf->lock); | |
1253 | ||
1254 | if ((trf->state != TRF7970A_ST_IDLE) && | |
1255 | (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) { | |
1256 | dev_err(trf->dev, "%s - Bogus state: %d\n", __func__, | |
1257 | trf->state); | |
1258 | ret = -EIO; | |
1259 | goto out_err; | |
1260 | } | |
1261 | ||
1262 | if (trf->aborting) { | |
1263 | dev_dbg(trf->dev, "Abort process complete\n"); | |
1264 | trf->aborting = false; | |
1265 | ret = -ECANCELED; | |
1266 | goto out_err; | |
1267 | } | |
1268 | ||
1961843c MG |
1269 | if (timeout) { |
1270 | trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE, | |
1271 | GFP_KERNEL); | |
1272 | if (!trf->rx_skb) { | |
1273 | dev_dbg(trf->dev, "Can't alloc rx_skb\n"); | |
1274 | ret = -ENOMEM; | |
1275 | goto out_err; | |
1276 | } | |
165063f1 MG |
1277 | } |
1278 | ||
1279 | if (trf->state == TRF7970A_ST_IDLE_RX_BLOCKED) { | |
1280 | ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX); | |
1281 | if (ret) | |
1282 | goto out_err; | |
1283 | ||
1284 | trf->state = TRF7970A_ST_IDLE; | |
1285 | } | |
1286 | ||
1287 | ret = trf7970a_per_cmd_config(trf, skb); | |
1288 | if (ret) | |
1289 | goto out_err; | |
1290 | ||
1291 | trf->ddev = ddev; | |
1292 | trf->tx_skb = skb; | |
1293 | trf->cb = cb; | |
1294 | trf->cb_arg = arg; | |
1295 | trf->timeout = timeout; | |
1296 | trf->ignore_timeout = false; | |
1297 | ||
1298 | len = skb->len; | |
165063f1 MG |
1299 | |
1300 | /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends | |
1301 | * on what the current framing is, the address of the TX length byte 1 | |
1302 | * register (0x1d), and the 2 byte length of the data to be transmitted. | |
7a1e5552 | 1303 | * That totals 5 bytes. |
165063f1 MG |
1304 | */ |
1305 | prefix[0] = TRF7970A_CMD_BIT_CTRL | | |
1306 | TRF7970A_CMD_BIT_OPCODE(TRF7970A_CMD_FIFO_RESET); | |
1307 | prefix[1] = TRF7970A_CMD_BIT_CTRL | | |
1308 | TRF7970A_CMD_BIT_OPCODE(trf->tx_cmd); | |
1309 | prefix[2] = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_TX_LENGTH_BYTE1; | |
1310 | ||
1311 | if (trf->framing == NFC_DIGITAL_FRAMING_NFCA_SHORT) { | |
1312 | prefix[3] = 0x00; | |
1313 | prefix[4] = 0x0f; /* 7 bits */ | |
1314 | } else { | |
1315 | prefix[3] = (len & 0xf00) >> 4; | |
1316 | prefix[3] |= ((len & 0xf0) >> 4); | |
1317 | prefix[4] = ((len & 0x0f) << 4); | |
1318 | } | |
1319 | ||
1320 | len = min_t(int, skb->len, TRF7970A_FIFO_SIZE); | |
1321 | ||
aff0564a MG |
1322 | /* Clear possible spurious interrupt */ |
1323 | ret = trf7970a_read_irqstatus(trf, &status); | |
1324 | if (ret) | |
1325 | goto out_err; | |
1326 | ||
7a1e5552 | 1327 | ret = trf7970a_transmit(trf, skb, len, prefix, sizeof(prefix)); |
165063f1 MG |
1328 | if (ret) { |
1329 | kfree_skb(trf->rx_skb); | |
1330 | trf->rx_skb = NULL; | |
1331 | } | |
1332 | ||
1333 | out_err: | |
1334 | mutex_unlock(&trf->lock); | |
1335 | return ret; | |
1336 | } | |
1337 | ||
1338 | static int trf7970a_tg_configure_hw(struct nfc_digital_dev *ddev, | |
1339 | int type, int param) | |
1340 | { | |
1341 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
1342 | ||
1343 | dev_dbg(trf->dev, "Unsupported interface\n"); | |
1344 | ||
1345 | return -EINVAL; | |
1346 | } | |
1347 | ||
1348 | static int trf7970a_tg_send_cmd(struct nfc_digital_dev *ddev, | |
1349 | struct sk_buff *skb, u16 timeout, | |
1350 | nfc_digital_cmd_complete_t cb, void *arg) | |
1351 | { | |
1352 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
1353 | ||
1354 | dev_dbg(trf->dev, "Unsupported interface\n"); | |
1355 | ||
1356 | return -EINVAL; | |
1357 | } | |
1358 | ||
1359 | static int trf7970a_tg_listen(struct nfc_digital_dev *ddev, | |
1360 | u16 timeout, nfc_digital_cmd_complete_t cb, void *arg) | |
1361 | { | |
1362 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
1363 | ||
1364 | dev_dbg(trf->dev, "Unsupported interface\n"); | |
1365 | ||
1366 | return -EINVAL; | |
1367 | } | |
1368 | ||
165063f1 MG |
1369 | static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev) |
1370 | { | |
1371 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
1372 | ||
1373 | dev_dbg(trf->dev, "Abort process initiated\n"); | |
1374 | ||
1375 | mutex_lock(&trf->lock); | |
5876bc75 MG |
1376 | |
1377 | switch (trf->state) { | |
1378 | case TRF7970A_ST_WAIT_FOR_TX_FIFO: | |
1379 | case TRF7970A_ST_WAIT_FOR_RX_DATA: | |
1380 | case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: | |
1381 | case TRF7970A_ST_WAIT_TO_ISSUE_EOF: | |
1382 | trf->aborting = true; | |
1383 | break; | |
1384 | default: | |
1385 | break; | |
1386 | } | |
1387 | ||
165063f1 MG |
1388 | mutex_unlock(&trf->lock); |
1389 | } | |
1390 | ||
1391 | static struct nfc_digital_ops trf7970a_nfc_ops = { | |
1392 | .in_configure_hw = trf7970a_in_configure_hw, | |
1393 | .in_send_cmd = trf7970a_in_send_cmd, | |
1394 | .tg_configure_hw = trf7970a_tg_configure_hw, | |
1395 | .tg_send_cmd = trf7970a_tg_send_cmd, | |
1396 | .tg_listen = trf7970a_tg_listen, | |
165063f1 MG |
1397 | .switch_rf = trf7970a_switch_rf, |
1398 | .abort_cmd = trf7970a_abort_cmd, | |
1399 | }; | |
1400 | ||
ceccd6aa MG |
1401 | static int trf7970a_power_up(struct trf7970a *trf) |
1402 | { | |
1403 | int ret; | |
1404 | ||
1405 | dev_dbg(trf->dev, "Powering up - state: %d\n", trf->state); | |
1406 | ||
1407 | if (trf->state != TRF7970A_ST_PWR_OFF) | |
1408 | return 0; | |
1409 | ||
1410 | ret = regulator_enable(trf->regulator); | |
1411 | if (ret) { | |
1412 | dev_err(trf->dev, "%s - Can't enable VIN: %d\n", __func__, ret); | |
1413 | return ret; | |
1414 | } | |
1415 | ||
1416 | usleep_range(5000, 6000); | |
1417 | ||
1418 | if (!(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW)) { | |
1419 | gpio_set_value(trf->en2_gpio, 1); | |
1420 | usleep_range(1000, 2000); | |
1421 | } | |
1422 | ||
1423 | gpio_set_value(trf->en_gpio, 1); | |
1424 | ||
1425 | usleep_range(20000, 21000); | |
1426 | ||
1427 | trf->state = TRF7970A_ST_RF_OFF; | |
1428 | ||
1429 | return 0; | |
1430 | } | |
1431 | ||
1432 | static int trf7970a_power_down(struct trf7970a *trf) | |
1433 | { | |
1434 | int ret; | |
1435 | ||
1436 | dev_dbg(trf->dev, "Powering down - state: %d\n", trf->state); | |
1437 | ||
1438 | if (trf->state == TRF7970A_ST_PWR_OFF) | |
1439 | return 0; | |
1440 | ||
1441 | if (trf->state != TRF7970A_ST_RF_OFF) { | |
1442 | dev_dbg(trf->dev, "Can't power down - not RF_OFF state (%d)\n", | |
1443 | trf->state); | |
1444 | return -EBUSY; | |
1445 | } | |
1446 | ||
1447 | gpio_set_value(trf->en_gpio, 0); | |
1448 | gpio_set_value(trf->en2_gpio, 0); | |
1449 | ||
1450 | ret = regulator_disable(trf->regulator); | |
1451 | if (ret) | |
1452 | dev_err(trf->dev, "%s - Can't disable VIN: %d\n", __func__, | |
1453 | ret); | |
1454 | ||
1455 | trf->state = TRF7970A_ST_PWR_OFF; | |
1456 | ||
1457 | return ret; | |
1458 | } | |
1459 | ||
b528281b MG |
1460 | static int trf7970a_startup(struct trf7970a *trf) |
1461 | { | |
1462 | int ret; | |
1463 | ||
1464 | ret = trf7970a_power_up(trf); | |
1465 | if (ret) | |
1466 | return ret; | |
1467 | ||
1468 | pm_runtime_set_active(trf->dev); | |
1469 | pm_runtime_enable(trf->dev); | |
1470 | pm_runtime_mark_last_busy(trf->dev); | |
1471 | ||
1472 | return 0; | |
1473 | } | |
1474 | ||
1475 | static void trf7970a_shutdown(struct trf7970a *trf) | |
1476 | { | |
1477 | switch (trf->state) { | |
1478 | case TRF7970A_ST_WAIT_FOR_TX_FIFO: | |
1479 | case TRF7970A_ST_WAIT_FOR_RX_DATA: | |
1480 | case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: | |
1481 | case TRF7970A_ST_WAIT_TO_ISSUE_EOF: | |
1482 | trf7970a_send_err_upstream(trf, -ECANCELED); | |
1483 | /* FALLTHROUGH */ | |
1484 | case TRF7970A_ST_IDLE: | |
1485 | case TRF7970A_ST_IDLE_RX_BLOCKED: | |
1486 | trf7970a_switch_rf_off(trf); | |
1487 | break; | |
1488 | default: | |
1489 | break; | |
1490 | } | |
1491 | ||
1492 | pm_runtime_disable(trf->dev); | |
1493 | pm_runtime_set_suspended(trf->dev); | |
1494 | ||
1495 | trf7970a_power_down(trf); | |
1496 | } | |
1497 | ||
fd0c8280 MG |
1498 | static int trf7970a_get_autosuspend_delay(struct device_node *np) |
1499 | { | |
1500 | int autosuspend_delay, ret; | |
1501 | ||
1502 | ret = of_property_read_u32(np, "autosuspend-delay", &autosuspend_delay); | |
1503 | if (ret) | |
1504 | autosuspend_delay = TRF7970A_AUTOSUSPEND_DELAY; | |
1505 | ||
fd0c8280 MG |
1506 | return autosuspend_delay; |
1507 | } | |
1508 | ||
3bd14233 MG |
1509 | static int trf7970a_get_vin_voltage_override(struct device_node *np, |
1510 | u32 *vin_uvolts) | |
1511 | { | |
1512 | return of_property_read_u32(np, "vin-voltage-override", vin_uvolts); | |
1513 | } | |
1514 | ||
165063f1 MG |
1515 | static int trf7970a_probe(struct spi_device *spi) |
1516 | { | |
1517 | struct device_node *np = spi->dev.of_node; | |
165063f1 | 1518 | struct trf7970a *trf; |
fd0c8280 | 1519 | int uvolts, autosuspend_delay, ret; |
165063f1 MG |
1520 | |
1521 | if (!np) { | |
1522 | dev_err(&spi->dev, "No Device Tree entry\n"); | |
1523 | return -EINVAL; | |
1524 | } | |
1525 | ||
1526 | trf = devm_kzalloc(&spi->dev, sizeof(*trf), GFP_KERNEL); | |
1527 | if (!trf) | |
1528 | return -ENOMEM; | |
1529 | ||
ceccd6aa | 1530 | trf->state = TRF7970A_ST_PWR_OFF; |
165063f1 MG |
1531 | trf->dev = &spi->dev; |
1532 | trf->spi = spi; | |
165063f1 MG |
1533 | |
1534 | spi->mode = SPI_MODE_1; | |
1535 | spi->bits_per_word = 8; | |
1536 | ||
24707296 MG |
1537 | ret = spi_setup(spi); |
1538 | if (ret < 0) { | |
1539 | dev_err(trf->dev, "Can't set up SPI Communication\n"); | |
1540 | return ret; | |
1541 | } | |
1542 | ||
772079eb MG |
1543 | if (of_property_read_bool(np, "irq-status-read-quirk")) |
1544 | trf->quirks |= TRF7970A_QUIRK_IRQ_STATUS_READ; | |
1545 | ||
165063f1 MG |
1546 | /* There are two enable pins - both must be present */ |
1547 | trf->en_gpio = of_get_named_gpio(np, "ti,enable-gpios", 0); | |
1548 | if (!gpio_is_valid(trf->en_gpio)) { | |
1549 | dev_err(trf->dev, "No EN GPIO property\n"); | |
1550 | return trf->en_gpio; | |
1551 | } | |
1552 | ||
1553 | ret = devm_gpio_request_one(trf->dev, trf->en_gpio, | |
f23b7352 | 1554 | GPIOF_DIR_OUT | GPIOF_INIT_LOW, "trf7970a EN"); |
165063f1 MG |
1555 | if (ret) { |
1556 | dev_err(trf->dev, "Can't request EN GPIO: %d\n", ret); | |
1557 | return ret; | |
1558 | } | |
1559 | ||
1560 | trf->en2_gpio = of_get_named_gpio(np, "ti,enable-gpios", 1); | |
1561 | if (!gpio_is_valid(trf->en2_gpio)) { | |
1562 | dev_err(trf->dev, "No EN2 GPIO property\n"); | |
1563 | return trf->en2_gpio; | |
1564 | } | |
1565 | ||
1566 | ret = devm_gpio_request_one(trf->dev, trf->en2_gpio, | |
f23b7352 | 1567 | GPIOF_DIR_OUT | GPIOF_INIT_LOW, "trf7970a EN2"); |
165063f1 MG |
1568 | if (ret) { |
1569 | dev_err(trf->dev, "Can't request EN2 GPIO: %d\n", ret); | |
1570 | return ret; | |
1571 | } | |
1572 | ||
95064bd9 MG |
1573 | if (of_property_read_bool(np, "en2-rf-quirk")) |
1574 | trf->quirks |= TRF7970A_QUIRK_EN2_MUST_STAY_LOW; | |
1575 | ||
165063f1 MG |
1576 | ret = devm_request_threaded_irq(trf->dev, spi->irq, NULL, |
1577 | trf7970a_irq, IRQF_TRIGGER_RISING | IRQF_ONESHOT, | |
1578 | "trf7970a", trf); | |
1579 | if (ret) { | |
1580 | dev_err(trf->dev, "Can't request IRQ#%d: %d\n", spi->irq, ret); | |
1581 | return ret; | |
1582 | } | |
1583 | ||
1584 | mutex_init(&trf->lock); | |
1585 | INIT_DELAYED_WORK(&trf->timeout_work, trf7970a_timeout_work_handler); | |
1586 | ||
1587 | trf->regulator = devm_regulator_get(&spi->dev, "vin"); | |
1588 | if (IS_ERR(trf->regulator)) { | |
1589 | ret = PTR_ERR(trf->regulator); | |
1590 | dev_err(trf->dev, "Can't get VIN regulator: %d\n", ret); | |
1591 | goto err_destroy_lock; | |
1592 | } | |
1593 | ||
1594 | ret = regulator_enable(trf->regulator); | |
1595 | if (ret) { | |
1596 | dev_err(trf->dev, "Can't enable VIN: %d\n", ret); | |
1597 | goto err_destroy_lock; | |
1598 | } | |
1599 | ||
3bd14233 MG |
1600 | ret = trf7970a_get_vin_voltage_override(np, &uvolts); |
1601 | if (ret) | |
1602 | uvolts = regulator_get_voltage(trf->regulator); | |
ebcc5a0d MG |
1603 | |
1604 | if (uvolts > 4000000) | |
1605 | trf->chip_status_ctrl = TRF7970A_CHIP_STATUS_VRS5_3; | |
1606 | ||
165063f1 MG |
1607 | trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops, |
1608 | TRF7970A_SUPPORTED_PROTOCOLS, | |
7a1e5552 | 1609 | NFC_DIGITAL_DRV_CAPS_IN_CRC, 0, 0); |
165063f1 MG |
1610 | if (!trf->ddev) { |
1611 | dev_err(trf->dev, "Can't allocate NFC digital device\n"); | |
1612 | ret = -ENOMEM; | |
1613 | goto err_disable_regulator; | |
1614 | } | |
1615 | ||
1616 | nfc_digital_set_parent_dev(trf->ddev, trf->dev); | |
1617 | nfc_digital_set_drvdata(trf->ddev, trf); | |
1618 | spi_set_drvdata(spi, trf); | |
1619 | ||
fd0c8280 MG |
1620 | autosuspend_delay = trf7970a_get_autosuspend_delay(np); |
1621 | ||
1622 | pm_runtime_set_autosuspend_delay(trf->dev, autosuspend_delay); | |
e6403b7c | 1623 | pm_runtime_use_autosuspend(trf->dev); |
ceccd6aa | 1624 | |
b528281b | 1625 | ret = trf7970a_startup(trf); |
ceccd6aa MG |
1626 | if (ret) |
1627 | goto err_free_ddev; | |
1628 | ||
165063f1 MG |
1629 | ret = nfc_digital_register_device(trf->ddev); |
1630 | if (ret) { | |
1631 | dev_err(trf->dev, "Can't register NFC digital device: %d\n", | |
1632 | ret); | |
b528281b | 1633 | goto err_shutdown; |
165063f1 MG |
1634 | } |
1635 | ||
1636 | return 0; | |
1637 | ||
b528281b MG |
1638 | err_shutdown: |
1639 | trf7970a_shutdown(trf); | |
ceccd6aa | 1640 | err_free_ddev: |
165063f1 MG |
1641 | nfc_digital_free_device(trf->ddev); |
1642 | err_disable_regulator: | |
1643 | regulator_disable(trf->regulator); | |
1644 | err_destroy_lock: | |
1645 | mutex_destroy(&trf->lock); | |
1646 | return ret; | |
1647 | } | |
1648 | ||
1649 | static int trf7970a_remove(struct spi_device *spi) | |
1650 | { | |
1651 | struct trf7970a *trf = spi_get_drvdata(spi); | |
1652 | ||
1653 | mutex_lock(&trf->lock); | |
1654 | ||
b528281b | 1655 | trf7970a_shutdown(trf); |
ceccd6aa MG |
1656 | |
1657 | mutex_unlock(&trf->lock); | |
e6403b7c | 1658 | |
165063f1 MG |
1659 | nfc_digital_unregister_device(trf->ddev); |
1660 | nfc_digital_free_device(trf->ddev); | |
1661 | ||
1662 | regulator_disable(trf->regulator); | |
1663 | ||
1664 | mutex_destroy(&trf->lock); | |
1665 | ||
1666 | return 0; | |
1667 | } | |
1668 | ||
77c9539d MG |
1669 | #ifdef CONFIG_PM_SLEEP |
1670 | static int trf7970a_suspend(struct device *dev) | |
1671 | { | |
1672 | struct spi_device *spi = container_of(dev, struct spi_device, dev); | |
1673 | struct trf7970a *trf = spi_get_drvdata(spi); | |
1674 | int ret = 0; | |
1675 | ||
1676 | dev_dbg(dev, "Suspend\n"); | |
1677 | ||
1678 | mutex_lock(&trf->lock); | |
1679 | ||
1680 | trf7970a_shutdown(trf); | |
1681 | ||
1682 | mutex_unlock(&trf->lock); | |
1683 | ||
1684 | return ret; | |
1685 | } | |
1686 | ||
1687 | static int trf7970a_resume(struct device *dev) | |
1688 | { | |
1689 | struct spi_device *spi = container_of(dev, struct spi_device, dev); | |
1690 | struct trf7970a *trf = spi_get_drvdata(spi); | |
1691 | int ret = 0; | |
1692 | ||
1693 | dev_dbg(dev, "Resume\n"); | |
1694 | ||
1695 | mutex_lock(&trf->lock); | |
1696 | ||
1697 | ret = trf7970a_startup(trf); | |
1698 | ||
1699 | mutex_unlock(&trf->lock); | |
1700 | ||
1701 | return ret; | |
1702 | } | |
1703 | #endif | |
1704 | ||
e6403b7c MG |
1705 | #ifdef CONFIG_PM_RUNTIME |
1706 | static int trf7970a_pm_runtime_suspend(struct device *dev) | |
1707 | { | |
1708 | struct spi_device *spi = container_of(dev, struct spi_device, dev); | |
1709 | struct trf7970a *trf = spi_get_drvdata(spi); | |
1710 | int ret; | |
1711 | ||
1712 | dev_dbg(dev, "Runtime suspend\n"); | |
1713 | ||
ceccd6aa | 1714 | mutex_lock(&trf->lock); |
e6403b7c | 1715 | |
ceccd6aa | 1716 | ret = trf7970a_power_down(trf); |
e6403b7c | 1717 | |
ceccd6aa | 1718 | mutex_unlock(&trf->lock); |
e6403b7c MG |
1719 | |
1720 | return ret; | |
1721 | } | |
1722 | ||
1723 | static int trf7970a_pm_runtime_resume(struct device *dev) | |
1724 | { | |
1725 | struct spi_device *spi = container_of(dev, struct spi_device, dev); | |
1726 | struct trf7970a *trf = spi_get_drvdata(spi); | |
1727 | int ret; | |
1728 | ||
1729 | dev_dbg(dev, "Runtime resume\n"); | |
1730 | ||
ceccd6aa MG |
1731 | ret = trf7970a_power_up(trf); |
1732 | if (!ret) | |
1733 | pm_runtime_mark_last_busy(dev); | |
e6403b7c | 1734 | |
ceccd6aa | 1735 | return ret; |
e6403b7c MG |
1736 | } |
1737 | #endif | |
1738 | ||
1739 | static const struct dev_pm_ops trf7970a_pm_ops = { | |
77c9539d | 1740 | SET_SYSTEM_SLEEP_PM_OPS(trf7970a_suspend, trf7970a_resume) |
e6403b7c MG |
1741 | SET_RUNTIME_PM_OPS(trf7970a_pm_runtime_suspend, |
1742 | trf7970a_pm_runtime_resume, NULL) | |
1743 | }; | |
1744 | ||
165063f1 | 1745 | static const struct spi_device_id trf7970a_id_table[] = { |
772079eb | 1746 | { "trf7970a", 0 }, |
165063f1 MG |
1747 | { } |
1748 | }; | |
1749 | MODULE_DEVICE_TABLE(spi, trf7970a_id_table); | |
1750 | ||
1751 | static struct spi_driver trf7970a_spi_driver = { | |
1752 | .probe = trf7970a_probe, | |
1753 | .remove = trf7970a_remove, | |
1754 | .id_table = trf7970a_id_table, | |
1755 | .driver = { | |
1756 | .name = "trf7970a", | |
1757 | .owner = THIS_MODULE, | |
e6403b7c | 1758 | .pm = &trf7970a_pm_ops, |
165063f1 MG |
1759 | }, |
1760 | }; | |
1761 | ||
1762 | module_spi_driver(trf7970a_spi_driver); | |
1763 | ||
1764 | MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>"); | |
1765 | MODULE_LICENSE("GPL v2"); | |
1766 | MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver"); |