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