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