blob: 3e950ddaaf77544010779f0694cdc7d01389268a [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
Mark A. Greer95064bd2014-08-07 17:41:45 -070039 * 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.
Mark A. Greer165063f2014-03-10 11:56:22 -070046 *
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
Mark A. Greer5fa3af32014-03-25 08:54:29 -070077 * 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
Mark A. Greer165063f2014-03-10 11:56:22 -070079 * 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
Mark A. Greer5fa3af32014-03-25 08:54:29 -070083 * been received and there isn't an error). The delay is 20 ms since delays
84 * of ~16 ms have been observed during testing.
Mark A. Greer165063f2014-03-10 11:56:22 -070085 *
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.
Mark A. Greer9d9304b2014-03-10 11:56:24 -070095 *
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.
Mark A. Greer165063f2014-03-10 11:56:22 -0700110 */
111
Mark A. Greer80062892014-03-10 11:56:23 -0700112#define TRF7970A_SUPPORTED_PROTOCOLS \
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700113 (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \
Mark A. Greer6857bb92014-04-14 10:04:10 -0700114 NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_FELICA_MASK | \
115 NFC_PROTO_ISO15693_MASK)
Mark A. Greer165063f2014-03-10 11:56:22 -0700116
Mark A. Greere6403b72014-03-25 08:54:38 -0700117#define TRF7970A_AUTOSUSPEND_DELAY 30000 /* 30 seconds */
118
Mark A. Greer165063f2014-03-10 11:56:22 -0700119/* 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 128
129
130/* TX length is 3 nibbles long ==> 4KB - 1 bytes max */
131#define TRF7970A_TX_MAX (4096 - 1)
132
Mark A. Greer5fa3af32014-03-25 08:54:29 -0700133#define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 20
Mark A. Greer165063f2014-03-10 11:56:22 -0700134#define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 3
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700135#define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 20
Mark A. Greer165063f2014-03-10 11:56:22 -0700136
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 */
Mark A. Greer772079e2014-08-07 17:41:43 -0700141#define TRF7970A_QUIRK_IRQ_STATUS_READ BIT(0)
Mark A. Greer95064bd2014-08-07 17:41:45 -0700142#define TRF7970A_QUIRK_EN2_MUST_STAY_LOW BIT(1)
Mark A. Greer165063f2014-03-10 11:56:22 -0700143
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
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700296/* 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
Mark A. Greer165063f2014-03-10 11:56:22 -0700326enum 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,
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700333 TRF7970A_ST_WAIT_TO_ISSUE_EOF,
Mark A. Greer165063f2014-03-10 11:56:22 -0700334 TRF7970A_ST_MAX
335};
336
337struct 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;
Mark A. Greer165063f2014-03-10 11:56:22 -0700344 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;
Mark A. Greerebcc5a02014-03-25 08:54:36 -0700349 u8 chip_status_ctrl;
Mark A. Greer165063f2014-03-10 11:56:22 -0700350 u8 iso_ctrl;
Mark A. Greer49d19cc2014-03-25 08:54:33 -0700351 u8 iso_ctrl_tech;
Mark A. Greer12e9ade2014-03-25 08:54:35 -0700352 u8 modulator_sys_clk_ctrl;
Mark A. Greer165063f2014-03-10 11:56:22 -0700353 u8 special_fcn_reg1;
354 int technology;
355 int framing;
356 u8 tx_cmd;
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700357 bool issue_eof;
Mark A. Greer165063f2014-03-10 11:56:22 -0700358 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
367static 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
381static 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
396static int trf7970a_read_cont(struct trf7970a *trf, u8 reg,
397 u8 *buf, size_t len)
398{
399 u8 addr = reg | TRF7970A_CMD_BIT_RW | TRF7970A_CMD_BIT_CONTINUOUS;
400 int ret;
401
402 dev_dbg(trf->dev, "read_cont(0x%x, %zd)\n", addr, len);
403
404 ret = spi_write_then_read(trf->spi, &addr, 1, buf, len);
405 if (ret)
406 dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr,
407 ret);
408 return ret;
409}
410
411static int trf7970a_write(struct trf7970a *trf, u8 reg, u8 val)
412{
413 u8 buf[2] = { reg, val };
414 int ret;
415
416 dev_dbg(trf->dev, "write(0x%x): 0x%x\n", reg, val);
417
418 ret = spi_write(trf->spi, buf, 2);
419 if (ret)
420 dev_err(trf->dev, "%s - write: 0x%x 0x%x, ret: %d\n", __func__,
421 buf[0], buf[1], ret);
422
423 return ret;
424}
425
426static int trf7970a_read_irqstatus(struct trf7970a *trf, u8 *status)
427{
428 int ret;
429 u8 buf[2];
430 u8 addr;
431
432 addr = TRF7970A_IRQ_STATUS | TRF7970A_CMD_BIT_RW;
433
Mark A. Greer772079e2014-08-07 17:41:43 -0700434 if (trf->quirks & TRF7970A_QUIRK_IRQ_STATUS_READ) {
Mark A. Greer165063f2014-03-10 11:56:22 -0700435 addr |= TRF7970A_CMD_BIT_CONTINUOUS;
436 ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2);
437 } else {
438 ret = spi_write_then_read(trf->spi, &addr, 1, buf, 1);
439 }
440
441 if (ret)
442 dev_err(trf->dev, "%s - irqstatus: Status read failed: %d\n",
443 __func__, ret);
444 else
445 *status = buf[0];
446
447 return ret;
448}
449
450static void trf7970a_send_upstream(struct trf7970a *trf)
451{
452 u8 rssi;
453
454 dev_kfree_skb_any(trf->tx_skb);
455 trf->tx_skb = NULL;
456
457 if (trf->rx_skb && !IS_ERR(trf->rx_skb) && !trf->aborting)
458 print_hex_dump_debug("trf7970a rx data: ", DUMP_PREFIX_NONE,
459 16, 1, trf->rx_skb->data, trf->rx_skb->len,
460 false);
461
462 /* According to the manual it is "good form" to reset the fifo and
463 * read the RSSI levels & oscillator status register here. It doesn't
464 * explain why.
465 */
466 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
467 trf7970a_read(trf, TRF7970A_RSSI_OSC_STATUS, &rssi);
468
469 trf->state = TRF7970A_ST_IDLE;
470
471 if (trf->aborting) {
472 dev_dbg(trf->dev, "Abort process complete\n");
473
474 if (!IS_ERR(trf->rx_skb)) {
475 kfree_skb(trf->rx_skb);
476 trf->rx_skb = ERR_PTR(-ECANCELED);
477 }
478
479 trf->aborting = false;
480 }
481
482 trf->cb(trf->ddev, trf->cb_arg, trf->rx_skb);
483
484 trf->rx_skb = NULL;
485}
486
487static void trf7970a_send_err_upstream(struct trf7970a *trf, int errno)
488{
489 dev_dbg(trf->dev, "Error - state: %d, errno: %d\n", trf->state, errno);
490
491 kfree_skb(trf->rx_skb);
492 trf->rx_skb = ERR_PTR(errno);
493
494 trf7970a_send_upstream(trf);
495}
496
497static int trf7970a_transmit(struct trf7970a *trf, struct sk_buff *skb,
498 unsigned int len)
499{
500 unsigned int timeout;
501 int ret;
502
503 print_hex_dump_debug("trf7970a tx data: ", DUMP_PREFIX_NONE,
504 16, 1, skb->data, len, false);
505
506 ret = spi_write(trf->spi, skb->data, len);
507 if (ret) {
508 dev_err(trf->dev, "%s - Can't send tx data: %d\n", __func__,
509 ret);
510 return ret;
511 }
512
513 skb_pull(skb, len);
514
515 if (skb->len > 0) {
516 trf->state = TRF7970A_ST_WAIT_FOR_TX_FIFO;
517 timeout = TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT;
518 } else {
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700519 if (trf->issue_eof) {
520 trf->state = TRF7970A_ST_WAIT_TO_ISSUE_EOF;
521 timeout = TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF;
522 } else {
523 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA;
524 timeout = trf->timeout;
525 }
Mark A. Greer165063f2014-03-10 11:56:22 -0700526 }
527
528 dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", timeout,
529 trf->state);
530
531 schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout));
532
533 return 0;
534}
535
536static void trf7970a_fill_fifo(struct trf7970a *trf)
537{
538 struct sk_buff *skb = trf->tx_skb;
539 unsigned int len;
540 int ret;
541 u8 fifo_bytes;
542
543 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
544 if (ret) {
545 trf7970a_send_err_upstream(trf, ret);
546 return;
547 }
548
549 dev_dbg(trf->dev, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes);
550
551 if (fifo_bytes & TRF7970A_FIFO_STATUS_OVERFLOW) {
552 dev_err(trf->dev, "%s - fifo overflow: 0x%x\n", __func__,
553 fifo_bytes);
554 trf7970a_send_err_upstream(trf, -EIO);
555 return;
556 }
557
558 /* Calculate how much more data can be written to the fifo */
559 len = TRF7970A_FIFO_SIZE - fifo_bytes;
560 len = min(skb->len, len);
561
562 ret = trf7970a_transmit(trf, skb, len);
563 if (ret)
564 trf7970a_send_err_upstream(trf, ret);
565}
566
567static void trf7970a_drain_fifo(struct trf7970a *trf, u8 status)
568{
569 struct sk_buff *skb = trf->rx_skb;
570 int ret;
571 u8 fifo_bytes;
572
573 if (status & TRF7970A_IRQ_STATUS_ERROR) {
574 trf7970a_send_err_upstream(trf, -EIO);
575 return;
576 }
577
578 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
579 if (ret) {
580 trf7970a_send_err_upstream(trf, ret);
581 return;
582 }
583
584 dev_dbg(trf->dev, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes);
585
586 if (!fifo_bytes)
587 goto no_rx_data;
588
589 if (fifo_bytes & TRF7970A_FIFO_STATUS_OVERFLOW) {
590 dev_err(trf->dev, "%s - fifo overflow: 0x%x\n", __func__,
591 fifo_bytes);
592 trf7970a_send_err_upstream(trf, -EIO);
593 return;
594 }
595
596 if (fifo_bytes > skb_tailroom(skb)) {
597 skb = skb_copy_expand(skb, skb_headroom(skb),
598 max_t(int, fifo_bytes,
599 TRF7970A_RX_SKB_ALLOC_SIZE),
600 GFP_KERNEL);
601 if (!skb) {
602 trf7970a_send_err_upstream(trf, -ENOMEM);
603 return;
604 }
605
606 kfree_skb(trf->rx_skb);
607 trf->rx_skb = skb;
608 }
609
610 ret = trf7970a_read_cont(trf, TRF7970A_FIFO_IO_REGISTER,
611 skb_put(skb, fifo_bytes), fifo_bytes);
612 if (ret) {
613 trf7970a_send_err_upstream(trf, ret);
614 return;
615 }
616
617 /* If received Type 2 ACK/NACK, shift right 4 bits and pass up */
618 if ((trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T) && (skb->len == 1) &&
619 (trf->special_fcn_reg1 ==
620 TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX)) {
621 skb->data[0] >>= 4;
622 status = TRF7970A_IRQ_STATUS_SRX;
623 } else {
624 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA_CONT;
625 }
626
627no_rx_data:
628 if (status == TRF7970A_IRQ_STATUS_SRX) { /* Receive complete */
629 trf7970a_send_upstream(trf);
630 return;
631 }
632
633 dev_dbg(trf->dev, "Setting timeout for %d ms\n",
634 TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT);
635
636 schedule_delayed_work(&trf->timeout_work,
637 msecs_to_jiffies(TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT));
638}
639
640static irqreturn_t trf7970a_irq(int irq, void *dev_id)
641{
642 struct trf7970a *trf = dev_id;
643 int ret;
644 u8 status;
645
646 mutex_lock(&trf->lock);
647
648 if (trf->state == TRF7970A_ST_OFF) {
649 mutex_unlock(&trf->lock);
650 return IRQ_NONE;
651 }
652
653 ret = trf7970a_read_irqstatus(trf, &status);
654 if (ret) {
655 mutex_unlock(&trf->lock);
656 return IRQ_NONE;
657 }
658
659 dev_dbg(trf->dev, "IRQ - state: %d, status: 0x%x\n", trf->state,
660 status);
661
662 if (!status) {
663 mutex_unlock(&trf->lock);
664 return IRQ_NONE;
665 }
666
667 switch (trf->state) {
668 case TRF7970A_ST_IDLE:
669 case TRF7970A_ST_IDLE_RX_BLOCKED:
670 /* If getting interrupts caused by RF noise, turn off the
671 * receiver to avoid unnecessary interrupts. It will be
672 * turned back on in trf7970a_in_send_cmd() when the next
673 * command is issued.
674 */
675 if (status & TRF7970A_IRQ_STATUS_ERROR) {
676 trf7970a_cmd(trf, TRF7970A_CMD_BLOCK_RX);
677 trf->state = TRF7970A_ST_IDLE_RX_BLOCKED;
678 }
679
680 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
681 break;
682 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
683 if (status & TRF7970A_IRQ_STATUS_TX) {
684 trf->ignore_timeout =
685 !cancel_delayed_work(&trf->timeout_work);
686 trf7970a_fill_fifo(trf);
687 } else {
688 trf7970a_send_err_upstream(trf, -EIO);
689 }
690 break;
691 case TRF7970A_ST_WAIT_FOR_RX_DATA:
692 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
693 if (status & TRF7970A_IRQ_STATUS_SRX) {
694 trf->ignore_timeout =
695 !cancel_delayed_work(&trf->timeout_work);
696 trf7970a_drain_fifo(trf, status);
Mark A. Greer4dd836e2014-03-25 08:54:32 -0700697 } else if (status == TRF7970A_IRQ_STATUS_TX) {
698 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
699 } else {
Mark A. Greer165063f2014-03-10 11:56:22 -0700700 trf7970a_send_err_upstream(trf, -EIO);
701 }
702 break;
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700703 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
704 if (status != TRF7970A_IRQ_STATUS_TX)
705 trf7970a_send_err_upstream(trf, -EIO);
706 break;
Mark A. Greer165063f2014-03-10 11:56:22 -0700707 default:
708 dev_err(trf->dev, "%s - Driver in invalid state: %d\n",
709 __func__, trf->state);
710 }
711
712 mutex_unlock(&trf->lock);
713 return IRQ_HANDLED;
714}
715
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700716static void trf7970a_issue_eof(struct trf7970a *trf)
717{
718 int ret;
719
720 dev_dbg(trf->dev, "Issuing EOF\n");
721
722 ret = trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
723 if (ret)
724 trf7970a_send_err_upstream(trf, ret);
725
726 ret = trf7970a_cmd(trf, TRF7970A_CMD_EOF);
727 if (ret)
728 trf7970a_send_err_upstream(trf, ret);
729
730 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA;
731
732 dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n",
733 trf->timeout, trf->state);
734
735 schedule_delayed_work(&trf->timeout_work,
736 msecs_to_jiffies(trf->timeout));
737}
738
Mark A. Greer165063f2014-03-10 11:56:22 -0700739static void trf7970a_timeout_work_handler(struct work_struct *work)
740{
741 struct trf7970a *trf = container_of(work, struct trf7970a,
742 timeout_work.work);
743
744 dev_dbg(trf->dev, "Timeout - state: %d, ignore_timeout: %d\n",
745 trf->state, trf->ignore_timeout);
746
747 mutex_lock(&trf->lock);
748
749 if (trf->ignore_timeout)
750 trf->ignore_timeout = false;
751 else if (trf->state == TRF7970A_ST_WAIT_FOR_RX_DATA_CONT)
752 trf7970a_send_upstream(trf); /* No more rx data so send up */
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700753 else if (trf->state == TRF7970A_ST_WAIT_TO_ISSUE_EOF)
754 trf7970a_issue_eof(trf);
Mark A. Greer165063f2014-03-10 11:56:22 -0700755 else
756 trf7970a_send_err_upstream(trf, -ETIMEDOUT);
757
758 mutex_unlock(&trf->lock);
759}
760
761static int trf7970a_init(struct trf7970a *trf)
762{
763 int ret;
764
765 dev_dbg(trf->dev, "Initializing device - state: %d\n", trf->state);
766
767 ret = trf7970a_cmd(trf, TRF7970A_CMD_SOFT_INIT);
768 if (ret)
769 goto err_out;
770
771 ret = trf7970a_cmd(trf, TRF7970A_CMD_IDLE);
772 if (ret)
773 goto err_out;
774
Mark A. Greerb887eb02014-03-25 08:54:31 -0700775 /* Must clear NFC Target Detection Level reg due to erratum */
776 ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
777 if (ret)
778 goto err_out;
779
Mark A. Greer165063f2014-03-10 11:56:22 -0700780 ret = trf7970a_write(trf, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS,
781 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 |
782 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32);
783 if (ret)
784 goto err_out;
785
786 ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, 0);
787 if (ret)
788 goto err_out;
789
790 trf->special_fcn_reg1 = 0;
791
Mark A. Greer49d19cc2014-03-25 08:54:33 -0700792 trf->iso_ctrl = 0xff;
Mark A. Greer165063f2014-03-10 11:56:22 -0700793 return 0;
794
795err_out:
796 dev_dbg(trf->dev, "Couldn't init device: %d\n", ret);
797 return ret;
798}
799
800static void trf7970a_switch_rf_off(struct trf7970a *trf)
801{
802 dev_dbg(trf->dev, "Switching rf off\n");
803
Mark A. Greera1d2dc52014-03-25 08:54:37 -0700804 trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
805
806 trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, trf->chip_status_ctrl);
807
Mark A. Greer165063f2014-03-10 11:56:22 -0700808 trf->aborting = false;
809 trf->state = TRF7970A_ST_OFF;
Mark A. Greere6403b72014-03-25 08:54:38 -0700810
811 pm_runtime_mark_last_busy(trf->dev);
812 pm_runtime_put_autosuspend(trf->dev);
Mark A. Greer165063f2014-03-10 11:56:22 -0700813}
814
Mark A. Greer18422e62014-03-25 08:54:39 -0700815static void trf7970a_switch_rf_on(struct trf7970a *trf)
Mark A. Greer165063f2014-03-10 11:56:22 -0700816{
Mark A. Greer165063f2014-03-10 11:56:22 -0700817 dev_dbg(trf->dev, "Switching rf on\n");
818
Mark A. Greere6403b72014-03-25 08:54:38 -0700819 pm_runtime_get_sync(trf->dev);
Mark A. Greer165063f2014-03-10 11:56:22 -0700820
Mark A. Greere6403b72014-03-25 08:54:38 -0700821 trf->state = TRF7970A_ST_IDLE;
Mark A. Greer165063f2014-03-10 11:56:22 -0700822}
823
824static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
825{
826 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
Mark A. Greer165063f2014-03-10 11:56:22 -0700827
828 dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on);
829
830 mutex_lock(&trf->lock);
831
832 if (on) {
833 switch (trf->state) {
834 case TRF7970A_ST_OFF:
Mark A. Greer18422e62014-03-25 08:54:39 -0700835 trf7970a_switch_rf_on(trf);
Mark A. Greer165063f2014-03-10 11:56:22 -0700836 break;
837 case TRF7970A_ST_IDLE:
838 case TRF7970A_ST_IDLE_RX_BLOCKED:
839 break;
840 default:
841 dev_err(trf->dev, "%s - Invalid request: %d %d\n",
842 __func__, trf->state, on);
843 trf7970a_switch_rf_off(trf);
844 }
845 } else {
846 switch (trf->state) {
847 case TRF7970A_ST_OFF:
848 break;
849 default:
850 dev_err(trf->dev, "%s - Invalid request: %d %d\n",
851 __func__, trf->state, on);
852 /* FALLTHROUGH */
853 case TRF7970A_ST_IDLE:
854 case TRF7970A_ST_IDLE_RX_BLOCKED:
855 trf7970a_switch_rf_off(trf);
856 }
857 }
858
859 mutex_unlock(&trf->lock);
Mark A. Greer18422e62014-03-25 08:54:39 -0700860 return 0;
Mark A. Greer165063f2014-03-10 11:56:22 -0700861}
862
863static int trf7970a_config_rf_tech(struct trf7970a *trf, int tech)
864{
865 int ret = 0;
866
867 dev_dbg(trf->dev, "rf technology: %d\n", tech);
868
869 switch (tech) {
870 case NFC_DIGITAL_RF_TECH_106A:
Mark A. Greer49d19cc2014-03-25 08:54:33 -0700871 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443A_106;
Mark A. Greer12e9ade2014-03-25 08:54:35 -0700872 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
Mark A. Greer165063f2014-03-10 11:56:22 -0700873 break;
Mark A. Greer742b1f92014-03-31 17:52:33 -0700874 case NFC_DIGITAL_RF_TECH_106B:
875 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443B_106;
876 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
877 break;
Mark A. Greer6857bb92014-04-14 10:04:10 -0700878 case NFC_DIGITAL_RF_TECH_212F:
879 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_212;
880 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
881 break;
882 case NFC_DIGITAL_RF_TECH_424F:
883 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_424;
884 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
885 break;
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700886 case NFC_DIGITAL_RF_TECH_ISO15693:
Mark A. Greer49d19cc2014-03-25 08:54:33 -0700887 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
Mark A. Greer12e9ade2014-03-25 08:54:35 -0700888 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700889 break;
Mark A. Greer165063f2014-03-10 11:56:22 -0700890 default:
891 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
892 return -EINVAL;
893 }
894
895 trf->technology = tech;
896
897 return ret;
898}
899
900static int trf7970a_config_framing(struct trf7970a *trf, int framing)
901{
Mark A. Greer49d19cc2014-03-25 08:54:33 -0700902 u8 iso_ctrl = trf->iso_ctrl_tech;
903 int ret;
904
Mark A. Greer165063f2014-03-10 11:56:22 -0700905 dev_dbg(trf->dev, "framing: %d\n", framing);
906
907 switch (framing) {
908 case NFC_DIGITAL_FRAMING_NFCA_SHORT:
909 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
910 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
Mark A. Greer49d19cc2014-03-25 08:54:33 -0700911 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
Mark A. Greer165063f2014-03-10 11:56:22 -0700912 break;
913 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
Mark A. Greer80062892014-03-10 11:56:23 -0700914 case NFC_DIGITAL_FRAMING_NFCA_T4T:
Mark A. Greer742b1f92014-03-31 17:52:33 -0700915 case NFC_DIGITAL_FRAMING_NFCB:
916 case NFC_DIGITAL_FRAMING_NFCB_T4T:
Mark A. Greer6857bb92014-04-14 10:04:10 -0700917 case NFC_DIGITAL_FRAMING_NFCF:
918 case NFC_DIGITAL_FRAMING_NFCF_T3T:
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700919 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY:
920 case NFC_DIGITAL_FRAMING_ISO15693_T5T:
Mark A. Greer165063f2014-03-10 11:56:22 -0700921 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
Mark A. Greer49d19cc2014-03-25 08:54:33 -0700922 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
Mark A. Greer165063f2014-03-10 11:56:22 -0700923 break;
924 case NFC_DIGITAL_FRAMING_NFCA_T2T:
925 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
Mark A. Greer49d19cc2014-03-25 08:54:33 -0700926 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
Mark A. Greer165063f2014-03-10 11:56:22 -0700927 break;
928 default:
929 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
930 return -EINVAL;
931 }
932
933 trf->framing = framing;
934
Mark A. Greer49d19cc2014-03-25 08:54:33 -0700935 if (iso_ctrl != trf->iso_ctrl) {
936 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
937 if (ret)
938 return ret;
939
940 trf->iso_ctrl = iso_ctrl;
Mark A. Greera0822a72014-03-25 08:54:34 -0700941
942 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
Mark A. Greer12e9ade2014-03-25 08:54:35 -0700943 trf->modulator_sys_clk_ctrl);
Mark A. Greera0822a72014-03-25 08:54:34 -0700944 if (ret)
945 return ret;
Mark A. Greer49d19cc2014-03-25 08:54:33 -0700946 }
947
Mark A. Greera1d2dc52014-03-25 08:54:37 -0700948 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
949 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
950 trf->chip_status_ctrl |
951 TRF7970A_CHIP_STATUS_RF_ON);
952 if (ret)
953 return ret;
954
955 trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
956
957 usleep_range(5000, 6000);
958 }
959
Mark A. Greer49d19cc2014-03-25 08:54:33 -0700960 return 0;
Mark A. Greer165063f2014-03-10 11:56:22 -0700961}
962
963static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type,
964 int param)
965{
966 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
Mark A. Greer18422e62014-03-25 08:54:39 -0700967 int ret;
Mark A. Greer165063f2014-03-10 11:56:22 -0700968
969 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
970
971 mutex_lock(&trf->lock);
972
Mark A. Greer18422e62014-03-25 08:54:39 -0700973 if (trf->state == TRF7970A_ST_OFF)
974 trf7970a_switch_rf_on(trf);
Mark A. Greer165063f2014-03-10 11:56:22 -0700975
976 switch (type) {
977 case NFC_DIGITAL_CONFIG_RF_TECH:
978 ret = trf7970a_config_rf_tech(trf, param);
979 break;
980 case NFC_DIGITAL_CONFIG_FRAMING:
981 ret = trf7970a_config_framing(trf, param);
982 break;
983 default:
984 dev_dbg(trf->dev, "Unknown type: %d\n", type);
985 ret = -EINVAL;
986 }
987
Mark A. Greer165063f2014-03-10 11:56:22 -0700988 mutex_unlock(&trf->lock);
989 return ret;
990}
991
Mark A. Greer9d9304b2014-03-10 11:56:24 -0700992static int trf7970a_is_iso15693_write_or_lock(u8 cmd)
993{
994 switch (cmd) {
995 case ISO15693_CMD_WRITE_SINGLE_BLOCK:
996 case ISO15693_CMD_LOCK_BLOCK:
997 case ISO15693_CMD_WRITE_MULTIPLE_BLOCK:
998 case ISO15693_CMD_WRITE_AFI:
999 case ISO15693_CMD_LOCK_AFI:
1000 case ISO15693_CMD_WRITE_DSFID:
1001 case ISO15693_CMD_LOCK_DSFID:
1002 return 1;
1003 break;
1004 default:
1005 return 0;
1006 }
1007}
1008
Mark A. Greer165063f2014-03-10 11:56:22 -07001009static int trf7970a_per_cmd_config(struct trf7970a *trf, struct sk_buff *skb)
1010{
1011 u8 *req = skb->data;
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001012 u8 special_fcn_reg1, iso_ctrl;
Mark A. Greer165063f2014-03-10 11:56:22 -07001013 int ret;
1014
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001015 trf->issue_eof = false;
1016
Mark A. Greer165063f2014-03-10 11:56:22 -07001017 /* When issuing Type 2 read command, make sure the '4_bit_RX' bit in
1018 * special functions register 1 is cleared; otherwise, its a write or
1019 * sector select command and '4_bit_RX' must be set.
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001020 *
1021 * When issuing an ISO 15693 command, inspect the flags byte to see
1022 * what speed to use. Also, remember if the OPTION flag is set on
1023 * a Type 5 write or lock command so the driver will know that it
1024 * has to send an EOF in order to get a response.
Mark A. Greer165063f2014-03-10 11:56:22 -07001025 */
1026 if ((trf->technology == NFC_DIGITAL_RF_TECH_106A) &&
1027 (trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T)) {
1028 if (req[0] == NFC_T2T_CMD_READ)
1029 special_fcn_reg1 = 0;
1030 else
1031 special_fcn_reg1 = TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX;
1032
1033 if (special_fcn_reg1 != trf->special_fcn_reg1) {
1034 ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1,
1035 special_fcn_reg1);
1036 if (ret)
1037 return ret;
1038
1039 trf->special_fcn_reg1 = special_fcn_reg1;
1040 }
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001041 } else if (trf->technology == NFC_DIGITAL_RF_TECH_ISO15693) {
1042 iso_ctrl = trf->iso_ctrl & ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK;
1043
1044 switch (req[0] & ISO15693_REQ_FLAG_SPEED_MASK) {
1045 case 0x00:
1046 iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_662;
1047 break;
1048 case ISO15693_REQ_FLAG_SUB_CARRIER:
1049 iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a;
1050 break;
1051 case ISO15693_REQ_FLAG_DATA_RATE:
1052 iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
1053 break;
1054 case (ISO15693_REQ_FLAG_SUB_CARRIER |
1055 ISO15693_REQ_FLAG_DATA_RATE):
1056 iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669;
1057 break;
1058 }
1059
1060 if (iso_ctrl != trf->iso_ctrl) {
1061 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1062 if (ret)
1063 return ret;
1064
1065 trf->iso_ctrl = iso_ctrl;
1066 }
1067
1068 if ((trf->framing == NFC_DIGITAL_FRAMING_ISO15693_T5T) &&
1069 trf7970a_is_iso15693_write_or_lock(req[1]) &&
1070 (req[0] & ISO15693_REQ_FLAG_OPTION))
1071 trf->issue_eof = true;
Mark A. Greer165063f2014-03-10 11:56:22 -07001072 }
1073
1074 return 0;
1075}
1076
1077static int trf7970a_in_send_cmd(struct nfc_digital_dev *ddev,
1078 struct sk_buff *skb, u16 timeout,
1079 nfc_digital_cmd_complete_t cb, void *arg)
1080{
1081 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1082 char *prefix;
1083 unsigned int len;
1084 int ret;
1085
1086 dev_dbg(trf->dev, "New request - state: %d, timeout: %d ms, len: %d\n",
1087 trf->state, timeout, skb->len);
1088
1089 if (skb->len > TRF7970A_TX_MAX)
1090 return -EINVAL;
1091
1092 mutex_lock(&trf->lock);
1093
1094 if ((trf->state != TRF7970A_ST_IDLE) &&
1095 (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) {
1096 dev_err(trf->dev, "%s - Bogus state: %d\n", __func__,
1097 trf->state);
1098 ret = -EIO;
1099 goto out_err;
1100 }
1101
1102 if (trf->aborting) {
1103 dev_dbg(trf->dev, "Abort process complete\n");
1104 trf->aborting = false;
1105 ret = -ECANCELED;
1106 goto out_err;
1107 }
1108
1109 trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE,
1110 GFP_KERNEL);
1111 if (!trf->rx_skb) {
1112 dev_dbg(trf->dev, "Can't alloc rx_skb\n");
1113 ret = -ENOMEM;
1114 goto out_err;
1115 }
1116
1117 if (trf->state == TRF7970A_ST_IDLE_RX_BLOCKED) {
1118 ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX);
1119 if (ret)
1120 goto out_err;
1121
1122 trf->state = TRF7970A_ST_IDLE;
1123 }
1124
1125 ret = trf7970a_per_cmd_config(trf, skb);
1126 if (ret)
1127 goto out_err;
1128
1129 trf->ddev = ddev;
1130 trf->tx_skb = skb;
1131 trf->cb = cb;
1132 trf->cb_arg = arg;
1133 trf->timeout = timeout;
1134 trf->ignore_timeout = false;
1135
1136 len = skb->len;
1137 prefix = skb_push(skb, TRF7970A_TX_SKB_HEADROOM);
1138
1139 /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
1140 * on what the current framing is, the address of the TX length byte 1
1141 * register (0x1d), and the 2 byte length of the data to be transmitted.
1142 */
1143 prefix[0] = TRF7970A_CMD_BIT_CTRL |
1144 TRF7970A_CMD_BIT_OPCODE(TRF7970A_CMD_FIFO_RESET);
1145 prefix[1] = TRF7970A_CMD_BIT_CTRL |
1146 TRF7970A_CMD_BIT_OPCODE(trf->tx_cmd);
1147 prefix[2] = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_TX_LENGTH_BYTE1;
1148
1149 if (trf->framing == NFC_DIGITAL_FRAMING_NFCA_SHORT) {
1150 prefix[3] = 0x00;
1151 prefix[4] = 0x0f; /* 7 bits */
1152 } else {
1153 prefix[3] = (len & 0xf00) >> 4;
1154 prefix[3] |= ((len & 0xf0) >> 4);
1155 prefix[4] = ((len & 0x0f) << 4);
1156 }
1157
1158 len = min_t(int, skb->len, TRF7970A_FIFO_SIZE);
1159
1160 usleep_range(1000, 2000);
1161
1162 ret = trf7970a_transmit(trf, skb, len);
1163 if (ret) {
1164 kfree_skb(trf->rx_skb);
1165 trf->rx_skb = NULL;
1166 }
1167
1168out_err:
1169 mutex_unlock(&trf->lock);
1170 return ret;
1171}
1172
1173static int trf7970a_tg_configure_hw(struct nfc_digital_dev *ddev,
1174 int type, int param)
1175{
1176 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1177
1178 dev_dbg(trf->dev, "Unsupported interface\n");
1179
1180 return -EINVAL;
1181}
1182
1183static int trf7970a_tg_send_cmd(struct nfc_digital_dev *ddev,
1184 struct sk_buff *skb, u16 timeout,
1185 nfc_digital_cmd_complete_t cb, void *arg)
1186{
1187 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1188
1189 dev_dbg(trf->dev, "Unsupported interface\n");
1190
1191 return -EINVAL;
1192}
1193
1194static int trf7970a_tg_listen(struct nfc_digital_dev *ddev,
1195 u16 timeout, nfc_digital_cmd_complete_t cb, void *arg)
1196{
1197 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1198
1199 dev_dbg(trf->dev, "Unsupported interface\n");
1200
1201 return -EINVAL;
1202}
1203
1204static int trf7970a_tg_listen_mdaa(struct nfc_digital_dev *ddev,
1205 struct digital_tg_mdaa_params *mdaa_params,
1206 u16 timeout, nfc_digital_cmd_complete_t cb, void *arg)
1207{
1208 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1209
1210 dev_dbg(trf->dev, "Unsupported interface\n");
1211
1212 return -EINVAL;
1213}
1214
1215static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev)
1216{
1217 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1218
1219 dev_dbg(trf->dev, "Abort process initiated\n");
1220
1221 mutex_lock(&trf->lock);
Mark A. Greer5876bc72014-03-25 08:54:30 -07001222
1223 switch (trf->state) {
1224 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
1225 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1226 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1227 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
1228 trf->aborting = true;
1229 break;
1230 default:
1231 break;
1232 }
1233
Mark A. Greer165063f2014-03-10 11:56:22 -07001234 mutex_unlock(&trf->lock);
1235}
1236
1237static struct nfc_digital_ops trf7970a_nfc_ops = {
1238 .in_configure_hw = trf7970a_in_configure_hw,
1239 .in_send_cmd = trf7970a_in_send_cmd,
1240 .tg_configure_hw = trf7970a_tg_configure_hw,
1241 .tg_send_cmd = trf7970a_tg_send_cmd,
1242 .tg_listen = trf7970a_tg_listen,
1243 .tg_listen_mdaa = trf7970a_tg_listen_mdaa,
1244 .switch_rf = trf7970a_switch_rf,
1245 .abort_cmd = trf7970a_abort_cmd,
1246};
1247
Mark A. Greerfd0c8282014-03-25 08:54:40 -07001248static int trf7970a_get_autosuspend_delay(struct device_node *np)
1249{
1250 int autosuspend_delay, ret;
1251
1252 ret = of_property_read_u32(np, "autosuspend-delay", &autosuspend_delay);
1253 if (ret)
1254 autosuspend_delay = TRF7970A_AUTOSUSPEND_DELAY;
1255
1256 of_node_put(np);
1257
1258 return autosuspend_delay;
1259}
1260
Mark A. Greer3bd14232014-08-07 17:41:41 -07001261static int trf7970a_get_vin_voltage_override(struct device_node *np,
1262 u32 *vin_uvolts)
1263{
1264 return of_property_read_u32(np, "vin-voltage-override", vin_uvolts);
1265}
1266
Mark A. Greer165063f2014-03-10 11:56:22 -07001267static int trf7970a_probe(struct spi_device *spi)
1268{
1269 struct device_node *np = spi->dev.of_node;
Mark A. Greer165063f2014-03-10 11:56:22 -07001270 struct trf7970a *trf;
Mark A. Greerfd0c8282014-03-25 08:54:40 -07001271 int uvolts, autosuspend_delay, ret;
Mark A. Greer165063f2014-03-10 11:56:22 -07001272
1273 if (!np) {
1274 dev_err(&spi->dev, "No Device Tree entry\n");
1275 return -EINVAL;
1276 }
1277
1278 trf = devm_kzalloc(&spi->dev, sizeof(*trf), GFP_KERNEL);
1279 if (!trf)
1280 return -ENOMEM;
1281
1282 trf->state = TRF7970A_ST_OFF;
1283 trf->dev = &spi->dev;
1284 trf->spi = spi;
Mark A. Greer165063f2014-03-10 11:56:22 -07001285
1286 spi->mode = SPI_MODE_1;
1287 spi->bits_per_word = 8;
1288
Mark A. Greer772079e2014-08-07 17:41:43 -07001289 if (of_property_read_bool(np, "irq-status-read-quirk"))
1290 trf->quirks |= TRF7970A_QUIRK_IRQ_STATUS_READ;
1291
Mark A. Greer165063f2014-03-10 11:56:22 -07001292 /* There are two enable pins - both must be present */
1293 trf->en_gpio = of_get_named_gpio(np, "ti,enable-gpios", 0);
1294 if (!gpio_is_valid(trf->en_gpio)) {
1295 dev_err(trf->dev, "No EN GPIO property\n");
1296 return trf->en_gpio;
1297 }
1298
1299 ret = devm_gpio_request_one(trf->dev, trf->en_gpio,
Mark A. Greerf23b7352014-08-07 17:41:47 -07001300 GPIOF_DIR_OUT | GPIOF_INIT_LOW, "trf7970a EN");
Mark A. Greer165063f2014-03-10 11:56:22 -07001301 if (ret) {
1302 dev_err(trf->dev, "Can't request EN GPIO: %d\n", ret);
1303 return ret;
1304 }
1305
1306 trf->en2_gpio = of_get_named_gpio(np, "ti,enable-gpios", 1);
1307 if (!gpio_is_valid(trf->en2_gpio)) {
1308 dev_err(trf->dev, "No EN2 GPIO property\n");
1309 return trf->en2_gpio;
1310 }
1311
1312 ret = devm_gpio_request_one(trf->dev, trf->en2_gpio,
Mark A. Greerf23b7352014-08-07 17:41:47 -07001313 GPIOF_DIR_OUT | GPIOF_INIT_LOW, "trf7970a EN2");
Mark A. Greer165063f2014-03-10 11:56:22 -07001314 if (ret) {
1315 dev_err(trf->dev, "Can't request EN2 GPIO: %d\n", ret);
1316 return ret;
1317 }
1318
Mark A. Greer95064bd2014-08-07 17:41:45 -07001319 if (of_property_read_bool(np, "en2-rf-quirk"))
1320 trf->quirks |= TRF7970A_QUIRK_EN2_MUST_STAY_LOW;
1321
Mark A. Greer165063f2014-03-10 11:56:22 -07001322 ret = devm_request_threaded_irq(trf->dev, spi->irq, NULL,
1323 trf7970a_irq, IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1324 "trf7970a", trf);
1325 if (ret) {
1326 dev_err(trf->dev, "Can't request IRQ#%d: %d\n", spi->irq, ret);
1327 return ret;
1328 }
1329
1330 mutex_init(&trf->lock);
1331 INIT_DELAYED_WORK(&trf->timeout_work, trf7970a_timeout_work_handler);
1332
1333 trf->regulator = devm_regulator_get(&spi->dev, "vin");
1334 if (IS_ERR(trf->regulator)) {
1335 ret = PTR_ERR(trf->regulator);
1336 dev_err(trf->dev, "Can't get VIN regulator: %d\n", ret);
1337 goto err_destroy_lock;
1338 }
1339
1340 ret = regulator_enable(trf->regulator);
1341 if (ret) {
1342 dev_err(trf->dev, "Can't enable VIN: %d\n", ret);
1343 goto err_destroy_lock;
1344 }
1345
Mark A. Greer3bd14232014-08-07 17:41:41 -07001346 ret = trf7970a_get_vin_voltage_override(np, &uvolts);
1347 if (ret)
1348 uvolts = regulator_get_voltage(trf->regulator);
Mark A. Greerebcc5a02014-03-25 08:54:36 -07001349
1350 if (uvolts > 4000000)
1351 trf->chip_status_ctrl = TRF7970A_CHIP_STATUS_VRS5_3;
1352
Mark A. Greer165063f2014-03-10 11:56:22 -07001353 trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops,
1354 TRF7970A_SUPPORTED_PROTOCOLS,
1355 NFC_DIGITAL_DRV_CAPS_IN_CRC, TRF7970A_TX_SKB_HEADROOM,
1356 0);
1357 if (!trf->ddev) {
1358 dev_err(trf->dev, "Can't allocate NFC digital device\n");
1359 ret = -ENOMEM;
1360 goto err_disable_regulator;
1361 }
1362
1363 nfc_digital_set_parent_dev(trf->ddev, trf->dev);
1364 nfc_digital_set_drvdata(trf->ddev, trf);
1365 spi_set_drvdata(spi, trf);
1366
Mark A. Greerfd0c8282014-03-25 08:54:40 -07001367 autosuspend_delay = trf7970a_get_autosuspend_delay(np);
1368
1369 pm_runtime_set_autosuspend_delay(trf->dev, autosuspend_delay);
Mark A. Greere6403b72014-03-25 08:54:38 -07001370 pm_runtime_use_autosuspend(trf->dev);
1371 pm_runtime_enable(trf->dev);
1372
Mark A. Greer165063f2014-03-10 11:56:22 -07001373 ret = nfc_digital_register_device(trf->ddev);
1374 if (ret) {
1375 dev_err(trf->dev, "Can't register NFC digital device: %d\n",
1376 ret);
1377 goto err_free_ddev;
1378 }
1379
1380 return 0;
1381
1382err_free_ddev:
Mark A. Greere6403b72014-03-25 08:54:38 -07001383 pm_runtime_disable(trf->dev);
Mark A. Greer165063f2014-03-10 11:56:22 -07001384 nfc_digital_free_device(trf->ddev);
1385err_disable_regulator:
1386 regulator_disable(trf->regulator);
1387err_destroy_lock:
1388 mutex_destroy(&trf->lock);
1389 return ret;
1390}
1391
1392static int trf7970a_remove(struct spi_device *spi)
1393{
1394 struct trf7970a *trf = spi_get_drvdata(spi);
1395
1396 mutex_lock(&trf->lock);
1397
Mark A. Greer165063f2014-03-10 11:56:22 -07001398 switch (trf->state) {
1399 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
1400 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1401 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
Mark A. Greer9d9304b2014-03-10 11:56:24 -07001402 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
Mark A. Greer165063f2014-03-10 11:56:22 -07001403 trf7970a_send_err_upstream(trf, -ECANCELED);
Mark A. Greere6403b72014-03-25 08:54:38 -07001404 /* FALLTHROUGH */
1405 case TRF7970A_ST_IDLE:
1406 case TRF7970A_ST_IDLE_RX_BLOCKED:
1407 pm_runtime_put_sync(trf->dev);
Mark A. Greer165063f2014-03-10 11:56:22 -07001408 break;
1409 default:
1410 break;
1411 }
1412
1413 mutex_unlock(&trf->lock);
1414
Mark A. Greere6403b72014-03-25 08:54:38 -07001415 pm_runtime_disable(trf->dev);
1416
Mark A. Greer165063f2014-03-10 11:56:22 -07001417 nfc_digital_unregister_device(trf->ddev);
1418 nfc_digital_free_device(trf->ddev);
1419
1420 regulator_disable(trf->regulator);
1421
1422 mutex_destroy(&trf->lock);
1423
1424 return 0;
1425}
1426
Mark A. Greere6403b72014-03-25 08:54:38 -07001427#ifdef CONFIG_PM_RUNTIME
1428static int trf7970a_pm_runtime_suspend(struct device *dev)
1429{
1430 struct spi_device *spi = container_of(dev, struct spi_device, dev);
1431 struct trf7970a *trf = spi_get_drvdata(spi);
1432 int ret;
1433
1434 dev_dbg(dev, "Runtime suspend\n");
1435
1436 if (trf->state != TRF7970A_ST_OFF) {
1437 dev_dbg(dev, "Can't suspend - not in OFF state (%d)\n",
1438 trf->state);
1439 return -EBUSY;
1440 }
1441
1442 gpio_set_value(trf->en_gpio, 0);
1443 gpio_set_value(trf->en2_gpio, 0);
1444
1445 ret = regulator_disable(trf->regulator);
1446 if (ret)
1447 dev_err(dev, "%s - Can't disable VIN: %d\n", __func__, ret);
1448
1449 return ret;
1450}
1451
1452static int trf7970a_pm_runtime_resume(struct device *dev)
1453{
1454 struct spi_device *spi = container_of(dev, struct spi_device, dev);
1455 struct trf7970a *trf = spi_get_drvdata(spi);
1456 int ret;
1457
1458 dev_dbg(dev, "Runtime resume\n");
1459
1460 ret = regulator_enable(trf->regulator);
1461 if (ret) {
1462 dev_err(dev, "%s - Can't enable VIN: %d\n", __func__, ret);
1463 return ret;
1464 }
1465
1466 usleep_range(5000, 6000);
1467
Mark A. Greer95064bd2014-08-07 17:41:45 -07001468 if (!(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW)) {
1469 gpio_set_value(trf->en2_gpio, 1);
1470 usleep_range(1000, 2000);
1471 }
1472
Mark A. Greere6403b72014-03-25 08:54:38 -07001473 gpio_set_value(trf->en_gpio, 1);
1474
1475 usleep_range(20000, 21000);
1476
1477 ret = trf7970a_init(trf);
1478 if (ret) {
1479 dev_err(dev, "%s - Can't initialize: %d\n", __func__, ret);
1480 return ret;
1481 }
1482
1483 pm_runtime_mark_last_busy(dev);
1484
1485 return 0;
1486}
1487#endif
1488
1489static const struct dev_pm_ops trf7970a_pm_ops = {
1490 SET_RUNTIME_PM_OPS(trf7970a_pm_runtime_suspend,
1491 trf7970a_pm_runtime_resume, NULL)
1492};
1493
Mark A. Greer165063f2014-03-10 11:56:22 -07001494static const struct spi_device_id trf7970a_id_table[] = {
Mark A. Greer772079e2014-08-07 17:41:43 -07001495 { "trf7970a", 0 },
Mark A. Greer165063f2014-03-10 11:56:22 -07001496 { }
1497};
1498MODULE_DEVICE_TABLE(spi, trf7970a_id_table);
1499
1500static struct spi_driver trf7970a_spi_driver = {
1501 .probe = trf7970a_probe,
1502 .remove = trf7970a_remove,
1503 .id_table = trf7970a_id_table,
1504 .driver = {
1505 .name = "trf7970a",
1506 .owner = THIS_MODULE,
Mark A. Greere6403b72014-03-25 08:54:38 -07001507 .pm = &trf7970a_pm_ops,
Mark A. Greer165063f2014-03-10 11:56:22 -07001508 },
1509};
1510
1511module_spi_driver(trf7970a_spi_driver);
1512
1513MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>");
1514MODULE_LICENSE("GPL v2");
1515MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver");