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