]>
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> | |
19 | #include <linux/nfc.h> | |
20 | #include <linux/skbuff.h> | |
21 | #include <linux/delay.h> | |
22 | #include <linux/gpio.h> | |
23 | #include <linux/of.h> | |
24 | #include <linux/of_gpio.h> | |
25 | #include <linux/spi/spi.h> | |
26 | #include <linux/regulator/consumer.h> | |
27 | ||
28 | #include <net/nfc/nfc.h> | |
29 | #include <net/nfc/digital.h> | |
30 | ||
31 | /* There are 3 ways the host can communicate with the trf7970a: | |
32 | * parallel mode, SPI with Slave Select (SS) mode, and SPI without | |
33 | * SS mode. The driver only supports the two SPI modes. | |
34 | * | |
35 | * The trf7970a is very timing sensitive and the VIN, EN2, and EN | |
36 | * pins must asserted in that order and with specific delays in between. | |
37 | * The delays used in the driver were provided by TI and have been | |
38 | * confirmed to work with this driver. | |
39 | * | |
40 | * Timeouts are implemented using the delayed workqueue kernel facility. | |
41 | * Timeouts are required so things don't hang when there is no response | |
42 | * from the trf7970a (or tag). Using this mechanism creates a race with | |
43 | * interrupts, however. That is, an interrupt and a timeout could occur | |
44 | * closely enough together that one is blocked by the mutex while the other | |
45 | * executes. When the timeout handler executes first and blocks the | |
46 | * interrupt handler, it will eventually set the state to IDLE so the | |
47 | * interrupt handler will check the state and exit with no harm done. | |
48 | * When the interrupt handler executes first and blocks the timeout handler, | |
49 | * the cancel_delayed_work() call will know that it didn't cancel the | |
50 | * work item (i.e., timeout) and will return zero. That return code is | |
51 | * used by the timer handler to indicate that it should ignore the timeout | |
52 | * once its unblocked. | |
53 | * | |
54 | * Aborting an active command isn't as simple as it seems because the only | |
55 | * way to abort a command that's already been sent to the tag is so turn | |
56 | * off power to the tag. If we do that, though, we'd have to go through | |
57 | * the entire anticollision procedure again but the digital layer doesn't | |
58 | * support that. So, if an abort is received before trf7970a_in_send_cmd() | |
59 | * has sent the command to the tag, it simply returns -ECANCELED. If the | |
60 | * command has already been sent to the tag, then the driver continues | |
61 | * normally and recieves the response data (or error) but just before | |
62 | * sending the data upstream, it frees the rx_skb and sends -ECANCELED | |
63 | * upstream instead. If the command failed, that error will be sent | |
64 | * upstream. | |
65 | * | |
66 | * When recieving data from a tag and the interrupt status register has | |
67 | * only the SRX bit set, it means that all of the data has been received | |
68 | * (once what's in the fifo has been read). However, depending on timing | |
69 | * an interrupt status with only the SRX bit set may not be recived. In | |
70 | * those cases, the timeout mechanism is used to wait 5 ms in case more | |
71 | * data arrives. After 5 ms, it is assumed that all of the data has been | |
72 | * received and the accumulated rx data is sent upstream. The | |
73 | * 'TRF7970A_ST_WAIT_FOR_RX_DATA_CONT' state is used for this purpose | |
74 | * (i.e., it indicates that some data has been received but we're not sure | |
75 | * if there is more coming so a timeout in this state means all data has | |
76 | * been received and there isn't an error). The delay is 5 ms since delays | |
77 | * over 2 ms have been observed during testing (a little extra just in case). | |
78 | * | |
79 | * Type 2 write and sector select commands respond with a 4-bit ACK or NACK. | |
80 | * Having only 4 bits in the FIFO won't normally generate an interrupt so | |
81 | * driver enables the '4_bit_RX' bit of the Special Functions register 1 | |
82 | * to cause an interrupt in that case. Leaving that bit for a read command | |
83 | * messes up the data returned so it is only enabled when the framing is | |
84 | * 'NFC_DIGITAL_FRAMING_NFCA_T2T' and the command is not a read command. | |
85 | * Unfortunately, that means that the driver has to peek into tx frames | |
86 | * when the framing is 'NFC_DIGITAL_FRAMING_NFCA_T2T'. This is done by | |
87 | * the trf7970a_per_cmd_config() routine. | |
88 | */ | |
89 | ||
90 | #define TRF7970A_SUPPORTED_PROTOCOLS NFC_PROTO_MIFARE_MASK | |
91 | ||
92 | /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends | |
93 | * on what the current framing is, the address of the TX length byte 1 | |
94 | * register (0x1d), and the 2 byte length of the data to be transmitted. | |
95 | * That totals 5 bytes. | |
96 | */ | |
97 | #define TRF7970A_TX_SKB_HEADROOM 5 | |
98 | ||
99 | #define TRF7970A_RX_SKB_ALLOC_SIZE 256 | |
100 | ||
101 | #define TRF7970A_FIFO_SIZE 128 | |
102 | ||
103 | /* TX length is 3 nibbles long ==> 4KB - 1 bytes max */ | |
104 | #define TRF7970A_TX_MAX (4096 - 1) | |
105 | ||
106 | #define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 5 | |
107 | #define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 3 | |
108 | ||
109 | /* Quirks */ | |
110 | /* Erratum: When reading IRQ Status register on trf7970a, we must issue a | |
111 | * read continuous command for IRQ Status and Collision Position registers. | |
112 | */ | |
113 | #define TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA BIT(0) | |
114 | ||
115 | /* Direct commands */ | |
116 | #define TRF7970A_CMD_IDLE 0x00 | |
117 | #define TRF7970A_CMD_SOFT_INIT 0x03 | |
118 | #define TRF7970A_CMD_RF_COLLISION 0x04 | |
119 | #define TRF7970A_CMD_RF_COLLISION_RESPONSE_N 0x05 | |
120 | #define TRF7970A_CMD_RF_COLLISION_RESPONSE_0 0x06 | |
121 | #define TRF7970A_CMD_FIFO_RESET 0x0f | |
122 | #define TRF7970A_CMD_TRANSMIT_NO_CRC 0x10 | |
123 | #define TRF7970A_CMD_TRANSMIT 0x11 | |
124 | #define TRF7970A_CMD_DELAY_TRANSMIT_NO_CRC 0x12 | |
125 | #define TRF7970A_CMD_DELAY_TRANSMIT 0x13 | |
126 | #define TRF7970A_CMD_EOF 0x14 | |
127 | #define TRF7970A_CMD_CLOSE_SLOT 0x15 | |
128 | #define TRF7970A_CMD_BLOCK_RX 0x16 | |
129 | #define TRF7970A_CMD_ENABLE_RX 0x17 | |
130 | #define TRF7970A_CMD_TEST_EXT_RF 0x18 | |
131 | #define TRF7970A_CMD_TEST_INT_RF 0x19 | |
132 | #define TRF7970A_CMD_RX_GAIN_ADJUST 0x1a | |
133 | ||
134 | /* Bits determining whether its a direct command or register R/W, | |
135 | * whether to use a continuous SPI transaction or not, and the actual | |
136 | * direct cmd opcode or regster address. | |
137 | */ | |
138 | #define TRF7970A_CMD_BIT_CTRL BIT(7) | |
139 | #define TRF7970A_CMD_BIT_RW BIT(6) | |
140 | #define TRF7970A_CMD_BIT_CONTINUOUS BIT(5) | |
141 | #define TRF7970A_CMD_BIT_OPCODE(opcode) ((opcode) & 0x1f) | |
142 | ||
143 | /* Registers addresses */ | |
144 | #define TRF7970A_CHIP_STATUS_CTRL 0x00 | |
145 | #define TRF7970A_ISO_CTRL 0x01 | |
146 | #define TRF7970A_ISO14443B_TX_OPTIONS 0x02 | |
147 | #define TRF7970A_ISO14443A_HIGH_BITRATE_OPTIONS 0x03 | |
148 | #define TRF7970A_TX_TIMER_SETTING_H_BYTE 0x04 | |
149 | #define TRF7970A_TX_TIMER_SETTING_L_BYTE 0x05 | |
150 | #define TRF7970A_TX_PULSE_LENGTH_CTRL 0x06 | |
151 | #define TRF7970A_RX_NO_RESPONSE_WAIT 0x07 | |
152 | #define TRF7970A_RX_WAIT_TIME 0x08 | |
153 | #define TRF7970A_MODULATOR_SYS_CLK_CTRL 0x09 | |
154 | #define TRF7970A_RX_SPECIAL_SETTINGS 0x0a | |
155 | #define TRF7970A_REG_IO_CTRL 0x0b | |
156 | #define TRF7970A_IRQ_STATUS 0x0c | |
157 | #define TRF7970A_COLLISION_IRQ_MASK 0x0d | |
158 | #define TRF7970A_COLLISION_POSITION 0x0e | |
159 | #define TRF7970A_RSSI_OSC_STATUS 0x0f | |
160 | #define TRF7970A_SPECIAL_FCN_REG1 0x10 | |
161 | #define TRF7970A_SPECIAL_FCN_REG2 0x11 | |
162 | #define TRF7970A_RAM1 0x12 | |
163 | #define TRF7970A_RAM2 0x13 | |
164 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS 0x14 | |
165 | #define TRF7970A_NFC_LOW_FIELD_LEVEL 0x16 | |
166 | #define TRF7970A_NFCID1 0x17 | |
167 | #define TRF7970A_NFC_TARGET_LEVEL 0x18 | |
168 | #define TRF79070A_NFC_TARGET_PROTOCOL 0x19 | |
169 | #define TRF7970A_TEST_REGISTER1 0x1a | |
170 | #define TRF7970A_TEST_REGISTER2 0x1b | |
171 | #define TRF7970A_FIFO_STATUS 0x1c | |
172 | #define TRF7970A_TX_LENGTH_BYTE1 0x1d | |
173 | #define TRF7970A_TX_LENGTH_BYTE2 0x1e | |
174 | #define TRF7970A_FIFO_IO_REGISTER 0x1f | |
175 | ||
176 | /* Chip Status Control Register Bits */ | |
177 | #define TRF7970A_CHIP_STATUS_VRS5_3 BIT(0) | |
178 | #define TRF7970A_CHIP_STATUS_REC_ON BIT(1) | |
179 | #define TRF7970A_CHIP_STATUS_AGC_ON BIT(2) | |
180 | #define TRF7970A_CHIP_STATUS_PM_ON BIT(3) | |
181 | #define TRF7970A_CHIP_STATUS_RF_PWR BIT(4) | |
182 | #define TRF7970A_CHIP_STATUS_RF_ON BIT(5) | |
183 | #define TRF7970A_CHIP_STATUS_DIRECT BIT(6) | |
184 | #define TRF7970A_CHIP_STATUS_STBY BIT(7) | |
185 | ||
186 | /* ISO Control Register Bits */ | |
187 | #define TRF7970A_ISO_CTRL_15693_SGL_1OF4_662 0x00 | |
188 | #define TRF7970A_ISO_CTRL_15693_SGL_1OF256_662 0x01 | |
189 | #define TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648 0x02 | |
190 | #define TRF7970A_ISO_CTRL_15693_SGL_1OF256_2648 0x03 | |
191 | #define TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a 0x04 | |
192 | #define TRF7970A_ISO_CTRL_15693_DBL_1OF256_667 0x05 | |
193 | #define TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669 0x06 | |
194 | #define TRF7970A_ISO_CTRL_15693_DBL_1OF256_2669 0x07 | |
195 | #define TRF7970A_ISO_CTRL_14443A_106 0x08 | |
196 | #define TRF7970A_ISO_CTRL_14443A_212 0x09 | |
197 | #define TRF7970A_ISO_CTRL_14443A_424 0x0a | |
198 | #define TRF7970A_ISO_CTRL_14443A_848 0x0b | |
199 | #define TRF7970A_ISO_CTRL_14443B_106 0x0c | |
200 | #define TRF7970A_ISO_CTRL_14443B_212 0x0d | |
201 | #define TRF7970A_ISO_CTRL_14443B_424 0x0e | |
202 | #define TRF7970A_ISO_CTRL_14443B_848 0x0f | |
203 | #define TRF7970A_ISO_CTRL_FELICA_212 0x1a | |
204 | #define TRF7970A_ISO_CTRL_FELICA_424 0x1b | |
205 | #define TRF7970A_ISO_CTRL_RFID BIT(5) | |
206 | #define TRF7970A_ISO_CTRL_DIR_MODE BIT(6) | |
207 | #define TRF7970A_ISO_CTRL_RX_CRC_N BIT(7) /* true == No CRC */ | |
208 | ||
209 | #define TRF7970A_ISO_CTRL_RFID_SPEED_MASK 0x1f | |
210 | ||
211 | /* Modulator and SYS_CLK Control Register Bits */ | |
212 | #define TRF7970A_MODULATOR_DEPTH(n) ((n) & 0x7) | |
213 | #define TRF7970A_MODULATOR_DEPTH_ASK10 (TRF7970A_MODULATOR_DEPTH(0)) | |
214 | #define TRF7970A_MODULATOR_DEPTH_OOK (TRF7970A_MODULATOR_DEPTH(1)) | |
215 | #define TRF7970A_MODULATOR_DEPTH_ASK7 (TRF7970A_MODULATOR_DEPTH(2)) | |
216 | #define TRF7970A_MODULATOR_DEPTH_ASK8_5 (TRF7970A_MODULATOR_DEPTH(3)) | |
217 | #define TRF7970A_MODULATOR_DEPTH_ASK13 (TRF7970A_MODULATOR_DEPTH(4)) | |
218 | #define TRF7970A_MODULATOR_DEPTH_ASK16 (TRF7970A_MODULATOR_DEPTH(5)) | |
219 | #define TRF7970A_MODULATOR_DEPTH_ASK22 (TRF7970A_MODULATOR_DEPTH(6)) | |
220 | #define TRF7970A_MODULATOR_DEPTH_ASK30 (TRF7970A_MODULATOR_DEPTH(7)) | |
221 | #define TRF7970A_MODULATOR_EN_ANA BIT(3) | |
222 | #define TRF7970A_MODULATOR_CLK(n) (((n) & 0x3) << 4) | |
223 | #define TRF7970A_MODULATOR_CLK_DISABLED (TRF7970A_MODULATOR_CLK(0)) | |
224 | #define TRF7970A_MODULATOR_CLK_3_6 (TRF7970A_MODULATOR_CLK(1)) | |
225 | #define TRF7970A_MODULATOR_CLK_6_13 (TRF7970A_MODULATOR_CLK(2)) | |
226 | #define TRF7970A_MODULATOR_CLK_13_27 (TRF7970A_MODULATOR_CLK(3)) | |
227 | #define TRF7970A_MODULATOR_EN_OOK BIT(6) | |
228 | #define TRF7970A_MODULATOR_27MHZ BIT(7) | |
229 | ||
230 | /* IRQ Status Register Bits */ | |
231 | #define TRF7970A_IRQ_STATUS_NORESP BIT(0) /* ISO15693 only */ | |
232 | #define TRF7970A_IRQ_STATUS_COL BIT(1) | |
233 | #define TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR BIT(2) | |
234 | #define TRF7970A_IRQ_STATUS_PARITY_ERROR BIT(3) | |
235 | #define TRF7970A_IRQ_STATUS_CRC_ERROR BIT(4) | |
236 | #define TRF7970A_IRQ_STATUS_FIFO BIT(5) | |
237 | #define TRF7970A_IRQ_STATUS_SRX BIT(6) | |
238 | #define TRF7970A_IRQ_STATUS_TX BIT(7) | |
239 | ||
240 | #define TRF7970A_IRQ_STATUS_ERROR \ | |
241 | (TRF7970A_IRQ_STATUS_COL | \ | |
242 | TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR | \ | |
243 | TRF7970A_IRQ_STATUS_PARITY_ERROR | \ | |
244 | TRF7970A_IRQ_STATUS_CRC_ERROR) | |
245 | ||
246 | #define TRF7970A_SPECIAL_FCN_REG1_COL_7_6 BIT(0) | |
247 | #define TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL BIT(1) | |
248 | #define TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX BIT(2) | |
249 | #define TRF7970A_SPECIAL_FCN_REG1_SP_DIR_MODE BIT(3) | |
250 | #define TRF7970A_SPECIAL_FCN_REG1_NEXT_SLOT_37US BIT(4) | |
251 | #define TRF7970A_SPECIAL_FCN_REG1_PAR43 BIT(5) | |
252 | ||
253 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_124 (0x0 << 2) | |
254 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_120 (0x1 << 2) | |
255 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_112 (0x2 << 2) | |
256 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 (0x3 << 2) | |
257 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_4 0x0 | |
258 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_8 0x1 | |
259 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_16 0x2 | |
260 | #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32 0x3 | |
261 | ||
262 | #define TRF7970A_FIFO_STATUS_OVERFLOW BIT(7) | |
263 | ||
264 | /* NFC (ISO/IEC 14443A) Type 2 Tag commands */ | |
265 | #define NFC_T2T_CMD_READ 0x30 | |
266 | ||
267 | enum trf7970a_state { | |
268 | TRF7970A_ST_OFF, | |
269 | TRF7970A_ST_IDLE, | |
270 | TRF7970A_ST_IDLE_RX_BLOCKED, | |
271 | TRF7970A_ST_WAIT_FOR_TX_FIFO, | |
272 | TRF7970A_ST_WAIT_FOR_RX_DATA, | |
273 | TRF7970A_ST_WAIT_FOR_RX_DATA_CONT, | |
274 | TRF7970A_ST_MAX | |
275 | }; | |
276 | ||
277 | struct trf7970a { | |
278 | enum trf7970a_state state; | |
279 | struct device *dev; | |
280 | struct spi_device *spi; | |
281 | struct regulator *regulator; | |
282 | struct nfc_digital_dev *ddev; | |
283 | u32 quirks; | |
284 | bool powering_up; | |
285 | bool aborting; | |
286 | struct sk_buff *tx_skb; | |
287 | struct sk_buff *rx_skb; | |
288 | nfc_digital_cmd_complete_t cb; | |
289 | void *cb_arg; | |
290 | u8 iso_ctrl; | |
291 | u8 special_fcn_reg1; | |
292 | int technology; | |
293 | int framing; | |
294 | u8 tx_cmd; | |
295 | int en2_gpio; | |
296 | int en_gpio; | |
297 | struct mutex lock; | |
298 | unsigned int timeout; | |
299 | bool ignore_timeout; | |
300 | struct delayed_work timeout_work; | |
301 | }; | |
302 | ||
303 | ||
304 | static int trf7970a_cmd(struct trf7970a *trf, u8 opcode) | |
305 | { | |
306 | u8 cmd = TRF7970A_CMD_BIT_CTRL | TRF7970A_CMD_BIT_OPCODE(opcode); | |
307 | int ret; | |
308 | ||
309 | dev_dbg(trf->dev, "cmd: 0x%x\n", cmd); | |
310 | ||
311 | ret = spi_write(trf->spi, &cmd, 1); | |
312 | if (ret) | |
313 | dev_err(trf->dev, "%s - cmd: 0x%x, ret: %d\n", __func__, cmd, | |
314 | ret); | |
315 | return ret; | |
316 | } | |
317 | ||
318 | static int trf7970a_read(struct trf7970a *trf, u8 reg, u8 *val) | |
319 | { | |
320 | u8 addr = TRF7970A_CMD_BIT_RW | reg; | |
321 | int ret; | |
322 | ||
323 | ret = spi_write_then_read(trf->spi, &addr, 1, val, 1); | |
324 | if (ret) | |
325 | dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr, | |
326 | ret); | |
327 | ||
328 | dev_dbg(trf->dev, "read(0x%x): 0x%x\n", addr, *val); | |
329 | ||
330 | return ret; | |
331 | } | |
332 | ||
333 | static int trf7970a_read_cont(struct trf7970a *trf, u8 reg, | |
334 | u8 *buf, size_t len) | |
335 | { | |
336 | u8 addr = reg | TRF7970A_CMD_BIT_RW | TRF7970A_CMD_BIT_CONTINUOUS; | |
337 | int ret; | |
338 | ||
339 | dev_dbg(trf->dev, "read_cont(0x%x, %zd)\n", addr, len); | |
340 | ||
341 | ret = spi_write_then_read(trf->spi, &addr, 1, buf, len); | |
342 | if (ret) | |
343 | dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr, | |
344 | ret); | |
345 | return ret; | |
346 | } | |
347 | ||
348 | static int trf7970a_write(struct trf7970a *trf, u8 reg, u8 val) | |
349 | { | |
350 | u8 buf[2] = { reg, val }; | |
351 | int ret; | |
352 | ||
353 | dev_dbg(trf->dev, "write(0x%x): 0x%x\n", reg, val); | |
354 | ||
355 | ret = spi_write(trf->spi, buf, 2); | |
356 | if (ret) | |
357 | dev_err(trf->dev, "%s - write: 0x%x 0x%x, ret: %d\n", __func__, | |
358 | buf[0], buf[1], ret); | |
359 | ||
360 | return ret; | |
361 | } | |
362 | ||
363 | static int trf7970a_read_irqstatus(struct trf7970a *trf, u8 *status) | |
364 | { | |
365 | int ret; | |
366 | u8 buf[2]; | |
367 | u8 addr; | |
368 | ||
369 | addr = TRF7970A_IRQ_STATUS | TRF7970A_CMD_BIT_RW; | |
370 | ||
371 | if (trf->quirks & TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA) { | |
372 | addr |= TRF7970A_CMD_BIT_CONTINUOUS; | |
373 | ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2); | |
374 | } else { | |
375 | ret = spi_write_then_read(trf->spi, &addr, 1, buf, 1); | |
376 | } | |
377 | ||
378 | if (ret) | |
379 | dev_err(trf->dev, "%s - irqstatus: Status read failed: %d\n", | |
380 | __func__, ret); | |
381 | else | |
382 | *status = buf[0]; | |
383 | ||
384 | return ret; | |
385 | } | |
386 | ||
387 | static void trf7970a_send_upstream(struct trf7970a *trf) | |
388 | { | |
389 | u8 rssi; | |
390 | ||
391 | dev_kfree_skb_any(trf->tx_skb); | |
392 | trf->tx_skb = NULL; | |
393 | ||
394 | if (trf->rx_skb && !IS_ERR(trf->rx_skb) && !trf->aborting) | |
395 | print_hex_dump_debug("trf7970a rx data: ", DUMP_PREFIX_NONE, | |
396 | 16, 1, trf->rx_skb->data, trf->rx_skb->len, | |
397 | false); | |
398 | ||
399 | /* According to the manual it is "good form" to reset the fifo and | |
400 | * read the RSSI levels & oscillator status register here. It doesn't | |
401 | * explain why. | |
402 | */ | |
403 | trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); | |
404 | trf7970a_read(trf, TRF7970A_RSSI_OSC_STATUS, &rssi); | |
405 | ||
406 | trf->state = TRF7970A_ST_IDLE; | |
407 | ||
408 | if (trf->aborting) { | |
409 | dev_dbg(trf->dev, "Abort process complete\n"); | |
410 | ||
411 | if (!IS_ERR(trf->rx_skb)) { | |
412 | kfree_skb(trf->rx_skb); | |
413 | trf->rx_skb = ERR_PTR(-ECANCELED); | |
414 | } | |
415 | ||
416 | trf->aborting = false; | |
417 | } | |
418 | ||
419 | trf->cb(trf->ddev, trf->cb_arg, trf->rx_skb); | |
420 | ||
421 | trf->rx_skb = NULL; | |
422 | } | |
423 | ||
424 | static void trf7970a_send_err_upstream(struct trf7970a *trf, int errno) | |
425 | { | |
426 | dev_dbg(trf->dev, "Error - state: %d, errno: %d\n", trf->state, errno); | |
427 | ||
428 | kfree_skb(trf->rx_skb); | |
429 | trf->rx_skb = ERR_PTR(errno); | |
430 | ||
431 | trf7970a_send_upstream(trf); | |
432 | } | |
433 | ||
434 | static int trf7970a_transmit(struct trf7970a *trf, struct sk_buff *skb, | |
435 | unsigned int len) | |
436 | { | |
437 | unsigned int timeout; | |
438 | int ret; | |
439 | ||
440 | print_hex_dump_debug("trf7970a tx data: ", DUMP_PREFIX_NONE, | |
441 | 16, 1, skb->data, len, false); | |
442 | ||
443 | ret = spi_write(trf->spi, skb->data, len); | |
444 | if (ret) { | |
445 | dev_err(trf->dev, "%s - Can't send tx data: %d\n", __func__, | |
446 | ret); | |
447 | return ret; | |
448 | } | |
449 | ||
450 | skb_pull(skb, len); | |
451 | ||
452 | if (skb->len > 0) { | |
453 | trf->state = TRF7970A_ST_WAIT_FOR_TX_FIFO; | |
454 | timeout = TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT; | |
455 | } else { | |
456 | trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA; | |
457 | timeout = trf->timeout; | |
458 | } | |
459 | ||
460 | dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", timeout, | |
461 | trf->state); | |
462 | ||
463 | schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout)); | |
464 | ||
465 | return 0; | |
466 | } | |
467 | ||
468 | static void trf7970a_fill_fifo(struct trf7970a *trf) | |
469 | { | |
470 | struct sk_buff *skb = trf->tx_skb; | |
471 | unsigned int len; | |
472 | int ret; | |
473 | u8 fifo_bytes; | |
474 | ||
475 | ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes); | |
476 | if (ret) { | |
477 | trf7970a_send_err_upstream(trf, ret); | |
478 | return; | |
479 | } | |
480 | ||
481 | dev_dbg(trf->dev, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes); | |
482 | ||
483 | if (fifo_bytes & TRF7970A_FIFO_STATUS_OVERFLOW) { | |
484 | dev_err(trf->dev, "%s - fifo overflow: 0x%x\n", __func__, | |
485 | fifo_bytes); | |
486 | trf7970a_send_err_upstream(trf, -EIO); | |
487 | return; | |
488 | } | |
489 | ||
490 | /* Calculate how much more data can be written to the fifo */ | |
491 | len = TRF7970A_FIFO_SIZE - fifo_bytes; | |
492 | len = min(skb->len, len); | |
493 | ||
494 | ret = trf7970a_transmit(trf, skb, len); | |
495 | if (ret) | |
496 | trf7970a_send_err_upstream(trf, ret); | |
497 | } | |
498 | ||
499 | static void trf7970a_drain_fifo(struct trf7970a *trf, u8 status) | |
500 | { | |
501 | struct sk_buff *skb = trf->rx_skb; | |
502 | int ret; | |
503 | u8 fifo_bytes; | |
504 | ||
505 | if (status & TRF7970A_IRQ_STATUS_ERROR) { | |
506 | trf7970a_send_err_upstream(trf, -EIO); | |
507 | return; | |
508 | } | |
509 | ||
510 | ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes); | |
511 | if (ret) { | |
512 | trf7970a_send_err_upstream(trf, ret); | |
513 | return; | |
514 | } | |
515 | ||
516 | dev_dbg(trf->dev, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes); | |
517 | ||
518 | if (!fifo_bytes) | |
519 | goto no_rx_data; | |
520 | ||
521 | if (fifo_bytes & TRF7970A_FIFO_STATUS_OVERFLOW) { | |
522 | dev_err(trf->dev, "%s - fifo overflow: 0x%x\n", __func__, | |
523 | fifo_bytes); | |
524 | trf7970a_send_err_upstream(trf, -EIO); | |
525 | return; | |
526 | } | |
527 | ||
528 | if (fifo_bytes > skb_tailroom(skb)) { | |
529 | skb = skb_copy_expand(skb, skb_headroom(skb), | |
530 | max_t(int, fifo_bytes, | |
531 | TRF7970A_RX_SKB_ALLOC_SIZE), | |
532 | GFP_KERNEL); | |
533 | if (!skb) { | |
534 | trf7970a_send_err_upstream(trf, -ENOMEM); | |
535 | return; | |
536 | } | |
537 | ||
538 | kfree_skb(trf->rx_skb); | |
539 | trf->rx_skb = skb; | |
540 | } | |
541 | ||
542 | ret = trf7970a_read_cont(trf, TRF7970A_FIFO_IO_REGISTER, | |
543 | skb_put(skb, fifo_bytes), fifo_bytes); | |
544 | if (ret) { | |
545 | trf7970a_send_err_upstream(trf, ret); | |
546 | return; | |
547 | } | |
548 | ||
549 | /* If received Type 2 ACK/NACK, shift right 4 bits and pass up */ | |
550 | if ((trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T) && (skb->len == 1) && | |
551 | (trf->special_fcn_reg1 == | |
552 | TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX)) { | |
553 | skb->data[0] >>= 4; | |
554 | status = TRF7970A_IRQ_STATUS_SRX; | |
555 | } else { | |
556 | trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA_CONT; | |
557 | } | |
558 | ||
559 | no_rx_data: | |
560 | if (status == TRF7970A_IRQ_STATUS_SRX) { /* Receive complete */ | |
561 | trf7970a_send_upstream(trf); | |
562 | return; | |
563 | } | |
564 | ||
565 | dev_dbg(trf->dev, "Setting timeout for %d ms\n", | |
566 | TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT); | |
567 | ||
568 | schedule_delayed_work(&trf->timeout_work, | |
569 | msecs_to_jiffies(TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT)); | |
570 | } | |
571 | ||
572 | static irqreturn_t trf7970a_irq(int irq, void *dev_id) | |
573 | { | |
574 | struct trf7970a *trf = dev_id; | |
575 | int ret; | |
576 | u8 status; | |
577 | ||
578 | mutex_lock(&trf->lock); | |
579 | ||
580 | if (trf->state == TRF7970A_ST_OFF) { | |
581 | mutex_unlock(&trf->lock); | |
582 | return IRQ_NONE; | |
583 | } | |
584 | ||
585 | ret = trf7970a_read_irqstatus(trf, &status); | |
586 | if (ret) { | |
587 | mutex_unlock(&trf->lock); | |
588 | return IRQ_NONE; | |
589 | } | |
590 | ||
591 | dev_dbg(trf->dev, "IRQ - state: %d, status: 0x%x\n", trf->state, | |
592 | status); | |
593 | ||
594 | if (!status) { | |
595 | mutex_unlock(&trf->lock); | |
596 | return IRQ_NONE; | |
597 | } | |
598 | ||
599 | switch (trf->state) { | |
600 | case TRF7970A_ST_IDLE: | |
601 | case TRF7970A_ST_IDLE_RX_BLOCKED: | |
602 | /* If getting interrupts caused by RF noise, turn off the | |
603 | * receiver to avoid unnecessary interrupts. It will be | |
604 | * turned back on in trf7970a_in_send_cmd() when the next | |
605 | * command is issued. | |
606 | */ | |
607 | if (status & TRF7970A_IRQ_STATUS_ERROR) { | |
608 | trf7970a_cmd(trf, TRF7970A_CMD_BLOCK_RX); | |
609 | trf->state = TRF7970A_ST_IDLE_RX_BLOCKED; | |
610 | } | |
611 | ||
612 | trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); | |
613 | break; | |
614 | case TRF7970A_ST_WAIT_FOR_TX_FIFO: | |
615 | if (status & TRF7970A_IRQ_STATUS_TX) { | |
616 | trf->ignore_timeout = | |
617 | !cancel_delayed_work(&trf->timeout_work); | |
618 | trf7970a_fill_fifo(trf); | |
619 | } else { | |
620 | trf7970a_send_err_upstream(trf, -EIO); | |
621 | } | |
622 | break; | |
623 | case TRF7970A_ST_WAIT_FOR_RX_DATA: | |
624 | case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: | |
625 | if (status & TRF7970A_IRQ_STATUS_SRX) { | |
626 | trf->ignore_timeout = | |
627 | !cancel_delayed_work(&trf->timeout_work); | |
628 | trf7970a_drain_fifo(trf, status); | |
629 | } else if (!(status & TRF7970A_IRQ_STATUS_TX)) { | |
630 | trf7970a_send_err_upstream(trf, -EIO); | |
631 | } | |
632 | break; | |
633 | default: | |
634 | dev_err(trf->dev, "%s - Driver in invalid state: %d\n", | |
635 | __func__, trf->state); | |
636 | } | |
637 | ||
638 | mutex_unlock(&trf->lock); | |
639 | return IRQ_HANDLED; | |
640 | } | |
641 | ||
642 | static void trf7970a_timeout_work_handler(struct work_struct *work) | |
643 | { | |
644 | struct trf7970a *trf = container_of(work, struct trf7970a, | |
645 | timeout_work.work); | |
646 | ||
647 | dev_dbg(trf->dev, "Timeout - state: %d, ignore_timeout: %d\n", | |
648 | trf->state, trf->ignore_timeout); | |
649 | ||
650 | mutex_lock(&trf->lock); | |
651 | ||
652 | if (trf->ignore_timeout) | |
653 | trf->ignore_timeout = false; | |
654 | else if (trf->state == TRF7970A_ST_WAIT_FOR_RX_DATA_CONT) | |
655 | trf7970a_send_upstream(trf); /* No more rx data so send up */ | |
656 | else | |
657 | trf7970a_send_err_upstream(trf, -ETIMEDOUT); | |
658 | ||
659 | mutex_unlock(&trf->lock); | |
660 | } | |
661 | ||
662 | static int trf7970a_init(struct trf7970a *trf) | |
663 | { | |
664 | int ret; | |
665 | ||
666 | dev_dbg(trf->dev, "Initializing device - state: %d\n", trf->state); | |
667 | ||
668 | ret = trf7970a_cmd(trf, TRF7970A_CMD_SOFT_INIT); | |
669 | if (ret) | |
670 | goto err_out; | |
671 | ||
672 | ret = trf7970a_cmd(trf, TRF7970A_CMD_IDLE); | |
673 | if (ret) | |
674 | goto err_out; | |
675 | ||
676 | ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL, | |
677 | TRF7970A_MODULATOR_DEPTH_OOK); | |
678 | if (ret) | |
679 | goto err_out; | |
680 | ||
681 | ret = trf7970a_write(trf, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS, | |
682 | TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 | | |
683 | TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32); | |
684 | if (ret) | |
685 | goto err_out; | |
686 | ||
687 | ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, 0); | |
688 | if (ret) | |
689 | goto err_out; | |
690 | ||
691 | trf->special_fcn_reg1 = 0; | |
692 | ||
693 | ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, | |
694 | TRF7970A_CHIP_STATUS_RF_ON | | |
695 | TRF7970A_CHIP_STATUS_VRS5_3); | |
696 | if (ret) | |
697 | goto err_out; | |
698 | ||
699 | return 0; | |
700 | ||
701 | err_out: | |
702 | dev_dbg(trf->dev, "Couldn't init device: %d\n", ret); | |
703 | return ret; | |
704 | } | |
705 | ||
706 | static void trf7970a_switch_rf_off(struct trf7970a *trf) | |
707 | { | |
708 | dev_dbg(trf->dev, "Switching rf off\n"); | |
709 | ||
710 | gpio_set_value(trf->en_gpio, 0); | |
711 | gpio_set_value(trf->en2_gpio, 0); | |
712 | ||
713 | trf->aborting = false; | |
714 | trf->state = TRF7970A_ST_OFF; | |
715 | } | |
716 | ||
717 | static int trf7970a_switch_rf_on(struct trf7970a *trf) | |
718 | { | |
719 | unsigned long delay; | |
720 | int ret; | |
721 | ||
722 | dev_dbg(trf->dev, "Switching rf on\n"); | |
723 | ||
724 | if (trf->powering_up) | |
725 | usleep_range(5000, 6000); | |
726 | ||
727 | gpio_set_value(trf->en2_gpio, 1); | |
728 | usleep_range(1000, 2000); | |
729 | gpio_set_value(trf->en_gpio, 1); | |
730 | ||
731 | /* The delay between enabling the trf7970a and issuing the first | |
732 | * command is significantly longer the very first time after powering | |
733 | * up. Make sure the longer delay is only done the first time. | |
734 | */ | |
735 | if (trf->powering_up) { | |
736 | delay = 20000; | |
737 | trf->powering_up = false; | |
738 | } else { | |
739 | delay = 5000; | |
740 | } | |
741 | ||
742 | usleep_range(delay, delay + 1000); | |
743 | ||
744 | ret = trf7970a_init(trf); | |
745 | if (ret) | |
746 | trf7970a_switch_rf_off(trf); | |
747 | else | |
748 | trf->state = TRF7970A_ST_IDLE; | |
749 | ||
750 | return ret; | |
751 | } | |
752 | ||
753 | static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on) | |
754 | { | |
755 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
756 | int ret = 0; | |
757 | ||
758 | dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on); | |
759 | ||
760 | mutex_lock(&trf->lock); | |
761 | ||
762 | if (on) { | |
763 | switch (trf->state) { | |
764 | case TRF7970A_ST_OFF: | |
765 | ret = trf7970a_switch_rf_on(trf); | |
766 | break; | |
767 | case TRF7970A_ST_IDLE: | |
768 | case TRF7970A_ST_IDLE_RX_BLOCKED: | |
769 | break; | |
770 | default: | |
771 | dev_err(trf->dev, "%s - Invalid request: %d %d\n", | |
772 | __func__, trf->state, on); | |
773 | trf7970a_switch_rf_off(trf); | |
774 | } | |
775 | } else { | |
776 | switch (trf->state) { | |
777 | case TRF7970A_ST_OFF: | |
778 | break; | |
779 | default: | |
780 | dev_err(trf->dev, "%s - Invalid request: %d %d\n", | |
781 | __func__, trf->state, on); | |
782 | /* FALLTHROUGH */ | |
783 | case TRF7970A_ST_IDLE: | |
784 | case TRF7970A_ST_IDLE_RX_BLOCKED: | |
785 | trf7970a_switch_rf_off(trf); | |
786 | } | |
787 | } | |
788 | ||
789 | mutex_unlock(&trf->lock); | |
790 | return ret; | |
791 | } | |
792 | ||
793 | static int trf7970a_config_rf_tech(struct trf7970a *trf, int tech) | |
794 | { | |
795 | int ret = 0; | |
796 | ||
797 | dev_dbg(trf->dev, "rf technology: %d\n", tech); | |
798 | ||
799 | switch (tech) { | |
800 | case NFC_DIGITAL_RF_TECH_106A: | |
801 | trf->iso_ctrl = TRF7970A_ISO_CTRL_14443A_106; | |
802 | break; | |
803 | default: | |
804 | dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech); | |
805 | return -EINVAL; | |
806 | } | |
807 | ||
808 | trf->technology = tech; | |
809 | ||
810 | return ret; | |
811 | } | |
812 | ||
813 | static int trf7970a_config_framing(struct trf7970a *trf, int framing) | |
814 | { | |
815 | dev_dbg(trf->dev, "framing: %d\n", framing); | |
816 | ||
817 | switch (framing) { | |
818 | case NFC_DIGITAL_FRAMING_NFCA_SHORT: | |
819 | case NFC_DIGITAL_FRAMING_NFCA_STANDARD: | |
820 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC; | |
821 | trf->iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N; | |
822 | break; | |
823 | case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A: | |
824 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT; | |
825 | trf->iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N; | |
826 | break; | |
827 | case NFC_DIGITAL_FRAMING_NFCA_T2T: | |
828 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT; | |
829 | trf->iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N; | |
830 | break; | |
831 | default: | |
832 | dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing); | |
833 | return -EINVAL; | |
834 | } | |
835 | ||
836 | trf->framing = framing; | |
837 | ||
838 | return trf7970a_write(trf, TRF7970A_ISO_CTRL, trf->iso_ctrl); | |
839 | } | |
840 | ||
841 | static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type, | |
842 | int param) | |
843 | { | |
844 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
845 | int ret = 0; | |
846 | ||
847 | dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param); | |
848 | ||
849 | mutex_lock(&trf->lock); | |
850 | ||
851 | if (trf->state == TRF7970A_ST_OFF) { | |
852 | ret = trf7970a_switch_rf_on(trf); | |
853 | if (ret) | |
854 | goto err_out; | |
855 | } | |
856 | ||
857 | switch (type) { | |
858 | case NFC_DIGITAL_CONFIG_RF_TECH: | |
859 | ret = trf7970a_config_rf_tech(trf, param); | |
860 | break; | |
861 | case NFC_DIGITAL_CONFIG_FRAMING: | |
862 | ret = trf7970a_config_framing(trf, param); | |
863 | break; | |
864 | default: | |
865 | dev_dbg(trf->dev, "Unknown type: %d\n", type); | |
866 | ret = -EINVAL; | |
867 | } | |
868 | ||
869 | err_out: | |
870 | mutex_unlock(&trf->lock); | |
871 | return ret; | |
872 | } | |
873 | ||
874 | static int trf7970a_per_cmd_config(struct trf7970a *trf, struct sk_buff *skb) | |
875 | { | |
876 | u8 *req = skb->data; | |
877 | u8 special_fcn_reg1; | |
878 | int ret; | |
879 | ||
880 | /* When issuing Type 2 read command, make sure the '4_bit_RX' bit in | |
881 | * special functions register 1 is cleared; otherwise, its a write or | |
882 | * sector select command and '4_bit_RX' must be set. | |
883 | */ | |
884 | if ((trf->technology == NFC_DIGITAL_RF_TECH_106A) && | |
885 | (trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T)) { | |
886 | if (req[0] == NFC_T2T_CMD_READ) | |
887 | special_fcn_reg1 = 0; | |
888 | else | |
889 | special_fcn_reg1 = TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX; | |
890 | ||
891 | if (special_fcn_reg1 != trf->special_fcn_reg1) { | |
892 | ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, | |
893 | special_fcn_reg1); | |
894 | if (ret) | |
895 | return ret; | |
896 | ||
897 | trf->special_fcn_reg1 = special_fcn_reg1; | |
898 | } | |
899 | } | |
900 | ||
901 | return 0; | |
902 | } | |
903 | ||
904 | static int trf7970a_in_send_cmd(struct nfc_digital_dev *ddev, | |
905 | struct sk_buff *skb, u16 timeout, | |
906 | nfc_digital_cmd_complete_t cb, void *arg) | |
907 | { | |
908 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
909 | char *prefix; | |
910 | unsigned int len; | |
911 | int ret; | |
912 | ||
913 | dev_dbg(trf->dev, "New request - state: %d, timeout: %d ms, len: %d\n", | |
914 | trf->state, timeout, skb->len); | |
915 | ||
916 | if (skb->len > TRF7970A_TX_MAX) | |
917 | return -EINVAL; | |
918 | ||
919 | mutex_lock(&trf->lock); | |
920 | ||
921 | if ((trf->state != TRF7970A_ST_IDLE) && | |
922 | (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) { | |
923 | dev_err(trf->dev, "%s - Bogus state: %d\n", __func__, | |
924 | trf->state); | |
925 | ret = -EIO; | |
926 | goto out_err; | |
927 | } | |
928 | ||
929 | if (trf->aborting) { | |
930 | dev_dbg(trf->dev, "Abort process complete\n"); | |
931 | trf->aborting = false; | |
932 | ret = -ECANCELED; | |
933 | goto out_err; | |
934 | } | |
935 | ||
936 | trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE, | |
937 | GFP_KERNEL); | |
938 | if (!trf->rx_skb) { | |
939 | dev_dbg(trf->dev, "Can't alloc rx_skb\n"); | |
940 | ret = -ENOMEM; | |
941 | goto out_err; | |
942 | } | |
943 | ||
944 | if (trf->state == TRF7970A_ST_IDLE_RX_BLOCKED) { | |
945 | ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX); | |
946 | if (ret) | |
947 | goto out_err; | |
948 | ||
949 | trf->state = TRF7970A_ST_IDLE; | |
950 | } | |
951 | ||
952 | ret = trf7970a_per_cmd_config(trf, skb); | |
953 | if (ret) | |
954 | goto out_err; | |
955 | ||
956 | trf->ddev = ddev; | |
957 | trf->tx_skb = skb; | |
958 | trf->cb = cb; | |
959 | trf->cb_arg = arg; | |
960 | trf->timeout = timeout; | |
961 | trf->ignore_timeout = false; | |
962 | ||
963 | len = skb->len; | |
964 | prefix = skb_push(skb, TRF7970A_TX_SKB_HEADROOM); | |
965 | ||
966 | /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends | |
967 | * on what the current framing is, the address of the TX length byte 1 | |
968 | * register (0x1d), and the 2 byte length of the data to be transmitted. | |
969 | */ | |
970 | prefix[0] = TRF7970A_CMD_BIT_CTRL | | |
971 | TRF7970A_CMD_BIT_OPCODE(TRF7970A_CMD_FIFO_RESET); | |
972 | prefix[1] = TRF7970A_CMD_BIT_CTRL | | |
973 | TRF7970A_CMD_BIT_OPCODE(trf->tx_cmd); | |
974 | prefix[2] = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_TX_LENGTH_BYTE1; | |
975 | ||
976 | if (trf->framing == NFC_DIGITAL_FRAMING_NFCA_SHORT) { | |
977 | prefix[3] = 0x00; | |
978 | prefix[4] = 0x0f; /* 7 bits */ | |
979 | } else { | |
980 | prefix[3] = (len & 0xf00) >> 4; | |
981 | prefix[3] |= ((len & 0xf0) >> 4); | |
982 | prefix[4] = ((len & 0x0f) << 4); | |
983 | } | |
984 | ||
985 | len = min_t(int, skb->len, TRF7970A_FIFO_SIZE); | |
986 | ||
987 | usleep_range(1000, 2000); | |
988 | ||
989 | ret = trf7970a_transmit(trf, skb, len); | |
990 | if (ret) { | |
991 | kfree_skb(trf->rx_skb); | |
992 | trf->rx_skb = NULL; | |
993 | } | |
994 | ||
995 | out_err: | |
996 | mutex_unlock(&trf->lock); | |
997 | return ret; | |
998 | } | |
999 | ||
1000 | static int trf7970a_tg_configure_hw(struct nfc_digital_dev *ddev, | |
1001 | int type, int param) | |
1002 | { | |
1003 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
1004 | ||
1005 | dev_dbg(trf->dev, "Unsupported interface\n"); | |
1006 | ||
1007 | return -EINVAL; | |
1008 | } | |
1009 | ||
1010 | static int trf7970a_tg_send_cmd(struct nfc_digital_dev *ddev, | |
1011 | struct sk_buff *skb, u16 timeout, | |
1012 | nfc_digital_cmd_complete_t cb, void *arg) | |
1013 | { | |
1014 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
1015 | ||
1016 | dev_dbg(trf->dev, "Unsupported interface\n"); | |
1017 | ||
1018 | return -EINVAL; | |
1019 | } | |
1020 | ||
1021 | static int trf7970a_tg_listen(struct nfc_digital_dev *ddev, | |
1022 | u16 timeout, nfc_digital_cmd_complete_t cb, void *arg) | |
1023 | { | |
1024 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
1025 | ||
1026 | dev_dbg(trf->dev, "Unsupported interface\n"); | |
1027 | ||
1028 | return -EINVAL; | |
1029 | } | |
1030 | ||
1031 | static int trf7970a_tg_listen_mdaa(struct nfc_digital_dev *ddev, | |
1032 | struct digital_tg_mdaa_params *mdaa_params, | |
1033 | u16 timeout, nfc_digital_cmd_complete_t cb, void *arg) | |
1034 | { | |
1035 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
1036 | ||
1037 | dev_dbg(trf->dev, "Unsupported interface\n"); | |
1038 | ||
1039 | return -EINVAL; | |
1040 | } | |
1041 | ||
1042 | static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev) | |
1043 | { | |
1044 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
1045 | ||
1046 | dev_dbg(trf->dev, "Abort process initiated\n"); | |
1047 | ||
1048 | mutex_lock(&trf->lock); | |
1049 | trf->aborting = true; | |
1050 | mutex_unlock(&trf->lock); | |
1051 | } | |
1052 | ||
1053 | static struct nfc_digital_ops trf7970a_nfc_ops = { | |
1054 | .in_configure_hw = trf7970a_in_configure_hw, | |
1055 | .in_send_cmd = trf7970a_in_send_cmd, | |
1056 | .tg_configure_hw = trf7970a_tg_configure_hw, | |
1057 | .tg_send_cmd = trf7970a_tg_send_cmd, | |
1058 | .tg_listen = trf7970a_tg_listen, | |
1059 | .tg_listen_mdaa = trf7970a_tg_listen_mdaa, | |
1060 | .switch_rf = trf7970a_switch_rf, | |
1061 | .abort_cmd = trf7970a_abort_cmd, | |
1062 | }; | |
1063 | ||
1064 | static int trf7970a_probe(struct spi_device *spi) | |
1065 | { | |
1066 | struct device_node *np = spi->dev.of_node; | |
1067 | const struct spi_device_id *id = spi_get_device_id(spi); | |
1068 | struct trf7970a *trf; | |
1069 | int ret; | |
1070 | ||
1071 | if (!np) { | |
1072 | dev_err(&spi->dev, "No Device Tree entry\n"); | |
1073 | return -EINVAL; | |
1074 | } | |
1075 | ||
1076 | trf = devm_kzalloc(&spi->dev, sizeof(*trf), GFP_KERNEL); | |
1077 | if (!trf) | |
1078 | return -ENOMEM; | |
1079 | ||
1080 | trf->state = TRF7970A_ST_OFF; | |
1081 | trf->dev = &spi->dev; | |
1082 | trf->spi = spi; | |
1083 | trf->quirks = id->driver_data; | |
1084 | ||
1085 | spi->mode = SPI_MODE_1; | |
1086 | spi->bits_per_word = 8; | |
1087 | ||
1088 | /* There are two enable pins - both must be present */ | |
1089 | trf->en_gpio = of_get_named_gpio(np, "ti,enable-gpios", 0); | |
1090 | if (!gpio_is_valid(trf->en_gpio)) { | |
1091 | dev_err(trf->dev, "No EN GPIO property\n"); | |
1092 | return trf->en_gpio; | |
1093 | } | |
1094 | ||
1095 | ret = devm_gpio_request_one(trf->dev, trf->en_gpio, | |
1096 | GPIOF_DIR_OUT | GPIOF_INIT_LOW, "EN"); | |
1097 | if (ret) { | |
1098 | dev_err(trf->dev, "Can't request EN GPIO: %d\n", ret); | |
1099 | return ret; | |
1100 | } | |
1101 | ||
1102 | trf->en2_gpio = of_get_named_gpio(np, "ti,enable-gpios", 1); | |
1103 | if (!gpio_is_valid(trf->en2_gpio)) { | |
1104 | dev_err(trf->dev, "No EN2 GPIO property\n"); | |
1105 | return trf->en2_gpio; | |
1106 | } | |
1107 | ||
1108 | ret = devm_gpio_request_one(trf->dev, trf->en2_gpio, | |
1109 | GPIOF_DIR_OUT | GPIOF_INIT_LOW, "EN2"); | |
1110 | if (ret) { | |
1111 | dev_err(trf->dev, "Can't request EN2 GPIO: %d\n", ret); | |
1112 | return ret; | |
1113 | } | |
1114 | ||
1115 | ret = devm_request_threaded_irq(trf->dev, spi->irq, NULL, | |
1116 | trf7970a_irq, IRQF_TRIGGER_RISING | IRQF_ONESHOT, | |
1117 | "trf7970a", trf); | |
1118 | if (ret) { | |
1119 | dev_err(trf->dev, "Can't request IRQ#%d: %d\n", spi->irq, ret); | |
1120 | return ret; | |
1121 | } | |
1122 | ||
1123 | mutex_init(&trf->lock); | |
1124 | INIT_DELAYED_WORK(&trf->timeout_work, trf7970a_timeout_work_handler); | |
1125 | ||
1126 | trf->regulator = devm_regulator_get(&spi->dev, "vin"); | |
1127 | if (IS_ERR(trf->regulator)) { | |
1128 | ret = PTR_ERR(trf->regulator); | |
1129 | dev_err(trf->dev, "Can't get VIN regulator: %d\n", ret); | |
1130 | goto err_destroy_lock; | |
1131 | } | |
1132 | ||
1133 | ret = regulator_enable(trf->regulator); | |
1134 | if (ret) { | |
1135 | dev_err(trf->dev, "Can't enable VIN: %d\n", ret); | |
1136 | goto err_destroy_lock; | |
1137 | } | |
1138 | ||
1139 | trf->powering_up = true; | |
1140 | ||
1141 | trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops, | |
1142 | TRF7970A_SUPPORTED_PROTOCOLS, | |
1143 | NFC_DIGITAL_DRV_CAPS_IN_CRC, TRF7970A_TX_SKB_HEADROOM, | |
1144 | 0); | |
1145 | if (!trf->ddev) { | |
1146 | dev_err(trf->dev, "Can't allocate NFC digital device\n"); | |
1147 | ret = -ENOMEM; | |
1148 | goto err_disable_regulator; | |
1149 | } | |
1150 | ||
1151 | nfc_digital_set_parent_dev(trf->ddev, trf->dev); | |
1152 | nfc_digital_set_drvdata(trf->ddev, trf); | |
1153 | spi_set_drvdata(spi, trf); | |
1154 | ||
1155 | ret = nfc_digital_register_device(trf->ddev); | |
1156 | if (ret) { | |
1157 | dev_err(trf->dev, "Can't register NFC digital device: %d\n", | |
1158 | ret); | |
1159 | goto err_free_ddev; | |
1160 | } | |
1161 | ||
1162 | return 0; | |
1163 | ||
1164 | err_free_ddev: | |
1165 | nfc_digital_free_device(trf->ddev); | |
1166 | err_disable_regulator: | |
1167 | regulator_disable(trf->regulator); | |
1168 | err_destroy_lock: | |
1169 | mutex_destroy(&trf->lock); | |
1170 | return ret; | |
1171 | } | |
1172 | ||
1173 | static int trf7970a_remove(struct spi_device *spi) | |
1174 | { | |
1175 | struct trf7970a *trf = spi_get_drvdata(spi); | |
1176 | ||
1177 | mutex_lock(&trf->lock); | |
1178 | ||
1179 | trf7970a_switch_rf_off(trf); | |
1180 | trf7970a_init(trf); | |
1181 | ||
1182 | switch (trf->state) { | |
1183 | case TRF7970A_ST_WAIT_FOR_TX_FIFO: | |
1184 | case TRF7970A_ST_WAIT_FOR_RX_DATA: | |
1185 | case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: | |
1186 | trf7970a_send_err_upstream(trf, -ECANCELED); | |
1187 | break; | |
1188 | default: | |
1189 | break; | |
1190 | } | |
1191 | ||
1192 | mutex_unlock(&trf->lock); | |
1193 | ||
1194 | nfc_digital_unregister_device(trf->ddev); | |
1195 | nfc_digital_free_device(trf->ddev); | |
1196 | ||
1197 | regulator_disable(trf->regulator); | |
1198 | ||
1199 | mutex_destroy(&trf->lock); | |
1200 | ||
1201 | return 0; | |
1202 | } | |
1203 | ||
1204 | static const struct spi_device_id trf7970a_id_table[] = { | |
1205 | { "trf7970a", TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA }, | |
1206 | { } | |
1207 | }; | |
1208 | MODULE_DEVICE_TABLE(spi, trf7970a_id_table); | |
1209 | ||
1210 | static struct spi_driver trf7970a_spi_driver = { | |
1211 | .probe = trf7970a_probe, | |
1212 | .remove = trf7970a_remove, | |
1213 | .id_table = trf7970a_id_table, | |
1214 | .driver = { | |
1215 | .name = "trf7970a", | |
1216 | .owner = THIS_MODULE, | |
1217 | }, | |
1218 | }; | |
1219 | ||
1220 | module_spi_driver(trf7970a_spi_driver); | |
1221 | ||
1222 | MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>"); | |
1223 | MODULE_LICENSE("GPL v2"); | |
1224 | MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver"); |