cregit-Linux how code gets into the kernel

Release 4.14 drivers/nfc/trf7970a.c

Directory: drivers/nfc
/*
 * TI TRF7970a RFID/NFC Transceiver Driver
 *
 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
 *
 * Author: Erick Macias <emacias@ti.com>
 * Author: Felipe Balbi <balbi@ti.com>
 * Author: Mark A. Greer <mgreer@animalcreek.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2  of
 * the License as published by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/device.h>
#include <linux/netdevice.h>
#include <linux/interrupt.h>
#include <linux/pm_runtime.h>
#include <linux/nfc.h>
#include <linux/skbuff.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/of.h>
#include <linux/spi/spi.h>
#include <linux/regulator/consumer.h>

#include <net/nfc/nfc.h>
#include <net/nfc/digital.h>

/* There are 3 ways the host can communicate with the trf7970a:
 * parallel mode, SPI with Slave Select (SS) mode, and SPI without
 * SS mode.  The driver only supports the two SPI modes.
 *
 * The trf7970a is very timing sensitive and the VIN, EN2, and EN
 * pins must asserted in that order and with specific delays in between.
 * The delays used in the driver were provided by TI and have been
 * confirmed to work with this driver.  There is a bug with the current
 * version of the trf7970a that requires that EN2 remain low no matter
 * what.  If it goes high, it will generate an RF field even when in
 * passive target mode.  TI has indicated that the chip will work okay
 * when EN2 is left low.  The 'en2-rf-quirk' device tree property
 * indicates that trf7970a currently being used has the erratum and
 * that EN2 must be kept low.
 *
 * Timeouts are implemented using the delayed workqueue kernel facility.
 * Timeouts are required so things don't hang when there is no response
 * from the trf7970a (or tag).  Using this mechanism creates a race with
 * interrupts, however.  That is, an interrupt and a timeout could occur
 * closely enough together that one is blocked by the mutex while the other
 * executes.  When the timeout handler executes first and blocks the
 * interrupt handler, it will eventually set the state to IDLE so the
 * interrupt handler will check the state and exit with no harm done.
 * When the interrupt handler executes first and blocks the timeout handler,
 * the cancel_delayed_work() call will know that it didn't cancel the
 * work item (i.e., timeout) and will return zero.  That return code is
 * used by the timer handler to indicate that it should ignore the timeout
 * once its unblocked.
 *
 * Aborting an active command isn't as simple as it seems because the only
 * way to abort a command that's already been sent to the tag is so turn
 * off power to the tag.  If we do that, though, we'd have to go through
 * the entire anticollision procedure again but the digital layer doesn't
 * support that.  So, if an abort is received before trf7970a_send_cmd()
 * has sent the command to the tag, it simply returns -ECANCELED.  If the
 * command has already been sent to the tag, then the driver continues
 * normally and recieves the response data (or error) but just before
 * sending the data upstream, it frees the rx_skb and sends -ECANCELED
 * upstream instead.  If the command failed, that error will be sent
 * upstream.
 *
 * When recieving data from a tag and the interrupt status register has
 * only the SRX bit set, it means that all of the data has been received
 * (once what's in the fifo has been read).  However, depending on timing
 * an interrupt status with only the SRX bit set may not be recived.  In
 * those cases, the timeout mechanism is used to wait 20 ms in case more
 * data arrives.  After 20 ms, it is assumed that all of the data has been
 * received and the accumulated rx data is sent upstream.  The
 * 'TRF7970A_ST_WAIT_FOR_RX_DATA_CONT' state is used for this purpose
 * (i.e., it indicates that some data has been received but we're not sure
 * if there is more coming so a timeout in this state means all data has
 * been received and there isn't an error).  The delay is 20 ms since delays
 * of ~16 ms have been observed during testing.
 *
 * When transmitting a frame larger than the FIFO size (127 bytes), the
 * driver will wait 20 ms for the FIFO to drain past the low-watermark
 * and generate an interrupt.  The low-watermark set to 32 bytes so the
 * interrupt should fire after 127 - 32 = 95 bytes have been sent.  At
 * the lowest possible bit rate (6.62 kbps for 15693), it will take up
 * to ~14.35 ms so 20 ms is used for the timeout.
 *
 * Type 2 write and sector select commands respond with a 4-bit ACK or NACK.
 * Having only 4 bits in the FIFO won't normally generate an interrupt so
 * driver enables the '4_bit_RX' bit of the Special Functions register 1
 * to cause an interrupt in that case.  Leaving that bit for a read command
 * messes up the data returned so it is only enabled when the framing is
 * 'NFC_DIGITAL_FRAMING_NFCA_T2T' and the command is not a read command.
 * Unfortunately, that means that the driver has to peek into tx frames
 * when the framing is 'NFC_DIGITAL_FRAMING_NFCA_T2T'.  This is done by
 * the trf7970a_per_cmd_config() routine.
 *
 * ISO/IEC 15693 frames specify whether to use single or double sub-carrier
 * frequencies and whether to use low or high data rates in the flags byte
 * of the frame.  This means that the driver has to peek at all 15693 frames
 * to determine what speed to set the communication to.  In addition, write
 * and lock commands use the OPTION flag to indicate that an EOF must be
 * sent to the tag before it will send its response.  So the driver has to
 * examine all frames for that reason too.
 *
 * It is unclear how long to wait before sending the EOF.  According to the
 * Note under Table 1-1 in section 1.6 of
 * http://www.ti.com/lit/ug/scbu011/scbu011.pdf, that wait should be at least
 * 10 ms for TI Tag-it HF-I tags; however testing has shown that is not long
 * enough so 20 ms is used.  So the timer is set to 40 ms - 20 ms to drain
 * up to 127 bytes in the FIFO at the lowest bit rate plus another 20 ms to
 * ensure the wait is long enough before sending the EOF.  This seems to work
 * reliably.
 */


#define TRF7970A_SUPPORTED_PROTOCOLS \
		(NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK |      \
                 NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_FELICA_MASK | \
                 NFC_PROTO_ISO15693_MASK | NFC_PROTO_NFC_DEP_MASK)


#define TRF7970A_AUTOSUSPEND_DELAY		30000	
/* 30 seconds */

#define TRF7970A_13MHZ_CLOCK_FREQUENCY		13560000

#define TRF7970A_27MHZ_CLOCK_FREQUENCY		27120000


#define TRF7970A_RX_SKB_ALLOC_SIZE		256


#define TRF7970A_FIFO_SIZE			127

/* TX length is 3 nibbles long ==> 4KB - 1 bytes max */

#define TRF7970A_TX_MAX				(4096 - 1)


#define TRF7970A_WAIT_FOR_TX_IRQ		20

#define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT	20

#define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT	20

#define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF	40

/* Guard times for various RF technologies (in us) */

#define TRF7970A_GUARD_TIME_NFCA		5000

#define TRF7970A_GUARD_TIME_NFCB		5000

#define TRF7970A_GUARD_TIME_NFCF		20000

#define TRF7970A_GUARD_TIME_15693		1000

/* Quirks */
/* Erratum: When reading IRQ Status register on trf7970a, we must issue a
 * read continuous command for IRQ Status and Collision Position registers.
 */

#define TRF7970A_QUIRK_IRQ_STATUS_READ		BIT(0)

#define TRF7970A_QUIRK_EN2_MUST_STAY_LOW	BIT(1)

/* Direct commands */

#define TRF7970A_CMD_IDLE			0x00

#define TRF7970A_CMD_SOFT_INIT			0x03

#define TRF7970A_CMD_RF_COLLISION		0x04

#define TRF7970A_CMD_RF_COLLISION_RESPONSE_N	0x05

#define TRF7970A_CMD_RF_COLLISION_RESPONSE_0	0x06

#define TRF7970A_CMD_FIFO_RESET			0x0f

#define TRF7970A_CMD_TRANSMIT_NO_CRC		0x10

#define TRF7970A_CMD_TRANSMIT			0x11

#define TRF7970A_CMD_DELAY_TRANSMIT_NO_CRC	0x12

#define TRF7970A_CMD_DELAY_TRANSMIT		0x13

#define TRF7970A_CMD_EOF			0x14

#define TRF7970A_CMD_CLOSE_SLOT			0x15

#define TRF7970A_CMD_BLOCK_RX			0x16

#define TRF7970A_CMD_ENABLE_RX			0x17

#define TRF7970A_CMD_TEST_INT_RF		0x18

#define TRF7970A_CMD_TEST_EXT_RF		0x19

#define TRF7970A_CMD_RX_GAIN_ADJUST		0x1a

/* Bits determining whether its a direct command or register R/W,
 * whether to use a continuous SPI transaction or not, and the actual
 * direct cmd opcode or regster address.
 */

#define TRF7970A_CMD_BIT_CTRL			BIT(7)

#define TRF7970A_CMD_BIT_RW			BIT(6)

#define TRF7970A_CMD_BIT_CONTINUOUS		BIT(5)

#define TRF7970A_CMD_BIT_OPCODE(opcode)		((opcode) & 0x1f)

/* Registers addresses */

#define TRF7970A_CHIP_STATUS_CTRL		0x00

#define TRF7970A_ISO_CTRL			0x01

#define TRF7970A_ISO14443B_TX_OPTIONS		0x02

#define TRF7970A_ISO14443A_HIGH_BITRATE_OPTIONS	0x03

#define TRF7970A_TX_TIMER_SETTING_H_BYTE	0x04

#define TRF7970A_TX_TIMER_SETTING_L_BYTE	0x05

#define TRF7970A_TX_PULSE_LENGTH_CTRL		0x06

#define TRF7970A_RX_NO_RESPONSE_WAIT		0x07

#define TRF7970A_RX_WAIT_TIME			0x08

#define TRF7970A_MODULATOR_SYS_CLK_CTRL		0x09

#define TRF7970A_RX_SPECIAL_SETTINGS		0x0a

#define TRF7970A_REG_IO_CTRL			0x0b

#define TRF7970A_IRQ_STATUS			0x0c

#define TRF7970A_COLLISION_IRQ_MASK		0x0d

#define TRF7970A_COLLISION_POSITION		0x0e

#define TRF7970A_RSSI_OSC_STATUS		0x0f

#define TRF7970A_SPECIAL_FCN_REG1		0x10

#define TRF7970A_SPECIAL_FCN_REG2		0x11

#define TRF7970A_RAM1				0x12

#define TRF7970A_RAM2				0x13

#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS	0x14

#define TRF7970A_NFC_LOW_FIELD_LEVEL		0x16

#define TRF7970A_NFCID1				0x17

#define TRF7970A_NFC_TARGET_LEVEL		0x18

#define TRF79070A_NFC_TARGET_PROTOCOL		0x19

#define TRF7970A_TEST_REGISTER1			0x1a

#define TRF7970A_TEST_REGISTER2			0x1b

#define TRF7970A_FIFO_STATUS			0x1c

#define TRF7970A_TX_LENGTH_BYTE1		0x1d

#define TRF7970A_TX_LENGTH_BYTE2		0x1e

#define TRF7970A_FIFO_IO_REGISTER		0x1f

/* Chip Status Control Register Bits */

#define TRF7970A_CHIP_STATUS_VRS5_3		BIT(0)

#define TRF7970A_CHIP_STATUS_REC_ON		BIT(1)

#define TRF7970A_CHIP_STATUS_AGC_ON		BIT(2)

#define TRF7970A_CHIP_STATUS_PM_ON		BIT(3)

#define TRF7970A_CHIP_STATUS_RF_PWR		BIT(4)

#define TRF7970A_CHIP_STATUS_RF_ON		BIT(5)

#define TRF7970A_CHIP_STATUS_DIRECT		BIT(6)

#define TRF7970A_CHIP_STATUS_STBY		BIT(7)

/* ISO Control Register Bits */

#define TRF7970A_ISO_CTRL_15693_SGL_1OF4_662	0x00

#define TRF7970A_ISO_CTRL_15693_SGL_1OF256_662	0x01

#define TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648	0x02

#define TRF7970A_ISO_CTRL_15693_SGL_1OF256_2648	0x03

#define TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a	0x04

#define TRF7970A_ISO_CTRL_15693_DBL_1OF256_667	0x05

#define TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669	0x06

#define TRF7970A_ISO_CTRL_15693_DBL_1OF256_2669	0x07

#define TRF7970A_ISO_CTRL_14443A_106		0x08

#define TRF7970A_ISO_CTRL_14443A_212		0x09

#define TRF7970A_ISO_CTRL_14443A_424		0x0a

#define TRF7970A_ISO_CTRL_14443A_848		0x0b

#define TRF7970A_ISO_CTRL_14443B_106		0x0c

#define TRF7970A_ISO_CTRL_14443B_212		0x0d

#define TRF7970A_ISO_CTRL_14443B_424		0x0e

#define TRF7970A_ISO_CTRL_14443B_848		0x0f

#define TRF7970A_ISO_CTRL_FELICA_212		0x1a

#define TRF7970A_ISO_CTRL_FELICA_424		0x1b

#define TRF7970A_ISO_CTRL_NFC_NFCA_106		0x01

#define TRF7970A_ISO_CTRL_NFC_NFCF_212		0x02

#define TRF7970A_ISO_CTRL_NFC_NFCF_424		0x03

#define TRF7970A_ISO_CTRL_NFC_CE_14443A		0x00

#define TRF7970A_ISO_CTRL_NFC_CE_14443B		0x01

#define TRF7970A_ISO_CTRL_NFC_CE		BIT(2)

#define TRF7970A_ISO_CTRL_NFC_ACTIVE		BIT(3)

#define TRF7970A_ISO_CTRL_NFC_INITIATOR		BIT(4)

#define TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE	BIT(5)

#define TRF7970A_ISO_CTRL_RFID			BIT(5)

#define TRF7970A_ISO_CTRL_DIR_MODE		BIT(6)

#define TRF7970A_ISO_CTRL_RX_CRC_N		BIT(7)	
/* true == No CRC */


#define TRF7970A_ISO_CTRL_RFID_SPEED_MASK	0x1f

/* Modulator and SYS_CLK Control Register Bits */

#define TRF7970A_MODULATOR_DEPTH(n)		((n) & 0x7)

#define TRF7970A_MODULATOR_DEPTH_ASK10		(TRF7970A_MODULATOR_DEPTH(0))

#define TRF7970A_MODULATOR_DEPTH_OOK		(TRF7970A_MODULATOR_DEPTH(1))

#define TRF7970A_MODULATOR_DEPTH_ASK7		(TRF7970A_MODULATOR_DEPTH(2))

#define TRF7970A_MODULATOR_DEPTH_ASK8_5		(TRF7970A_MODULATOR_DEPTH(3))

#define TRF7970A_MODULATOR_DEPTH_ASK13		(TRF7970A_MODULATOR_DEPTH(4))

#define TRF7970A_MODULATOR_DEPTH_ASK16		(TRF7970A_MODULATOR_DEPTH(5))

#define TRF7970A_MODULATOR_DEPTH_ASK22		(TRF7970A_MODULATOR_DEPTH(6))

#define TRF7970A_MODULATOR_DEPTH_ASK30		(TRF7970A_MODULATOR_DEPTH(7))

#define TRF7970A_MODULATOR_EN_ANA		BIT(3)

#define TRF7970A_MODULATOR_CLK(n)		(((n) & 0x3) << 4)

#define TRF7970A_MODULATOR_CLK_DISABLED		(TRF7970A_MODULATOR_CLK(0))

#define TRF7970A_MODULATOR_CLK_3_6		(TRF7970A_MODULATOR_CLK(1))

#define TRF7970A_MODULATOR_CLK_6_13		(TRF7970A_MODULATOR_CLK(2))

#define TRF7970A_MODULATOR_CLK_13_27		(TRF7970A_MODULATOR_CLK(3))

#define TRF7970A_MODULATOR_EN_OOK		BIT(6)

#define TRF7970A_MODULATOR_27MHZ		BIT(7)


#define TRF7970A_RX_SPECIAL_SETTINGS_NO_LIM	BIT(0)

#define TRF7970A_RX_SPECIAL_SETTINGS_AGCR	BIT(1)

#define TRF7970A_RX_SPECIAL_SETTINGS_GD_0DB	(0x0 << 2)

#define TRF7970A_RX_SPECIAL_SETTINGS_GD_5DB	(0x1 << 2)

#define TRF7970A_RX_SPECIAL_SETTINGS_GD_10DB	(0x2 << 2)

#define TRF7970A_RX_SPECIAL_SETTINGS_GD_15DB	(0x3 << 2)

#define TRF7970A_RX_SPECIAL_SETTINGS_HBT	BIT(4)

#define TRF7970A_RX_SPECIAL_SETTINGS_M848	BIT(5)

#define TRF7970A_RX_SPECIAL_SETTINGS_C424	BIT(6)

#define TRF7970A_RX_SPECIAL_SETTINGS_C212	BIT(7)


#define TRF7970A_REG_IO_CTRL_VRS(v)		((v) & 0x07)

#define TRF7970A_REG_IO_CTRL_IO_LOW		BIT(5)

#define TRF7970A_REG_IO_CTRL_EN_EXT_PA		BIT(6)

#define TRF7970A_REG_IO_CTRL_AUTO_REG		BIT(7)

/* IRQ Status Register Bits */

#define TRF7970A_IRQ_STATUS_NORESP		BIT(0)	
/* ISO15693 only */

#define TRF7970A_IRQ_STATUS_NFC_COL_ERROR	BIT(0)

#define TRF7970A_IRQ_STATUS_COL			BIT(1)

#define TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR	BIT(2)

#define TRF7970A_IRQ_STATUS_NFC_RF		BIT(2)

#define TRF7970A_IRQ_STATUS_PARITY_ERROR	BIT(3)

#define TRF7970A_IRQ_STATUS_NFC_SDD		BIT(3)

#define TRF7970A_IRQ_STATUS_CRC_ERROR		BIT(4)

#define TRF7970A_IRQ_STATUS_NFC_PROTO_ERROR	BIT(4)

#define TRF7970A_IRQ_STATUS_FIFO		BIT(5)

#define TRF7970A_IRQ_STATUS_SRX			BIT(6)

#define TRF7970A_IRQ_STATUS_TX			BIT(7)


#define TRF7970A_IRQ_STATUS_ERROR				\
		(TRF7970A_IRQ_STATUS_COL |                      \
                 TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR |        \
                 TRF7970A_IRQ_STATUS_PARITY_ERROR |             \
                 TRF7970A_IRQ_STATUS_CRC_ERROR)


#define TRF7970A_RSSI_OSC_STATUS_RSSI_MASK	(BIT(2) | BIT(1) | BIT(0))

#define TRF7970A_RSSI_OSC_STATUS_RSSI_X_MASK	(BIT(5) | BIT(4) | BIT(3))

#define TRF7970A_RSSI_OSC_STATUS_RSSI_OSC_OK	BIT(6)


#define TRF7970A_SPECIAL_FCN_REG1_COL_7_6		BIT(0)

#define TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL		BIT(1)

#define TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX		BIT(2)

#define TRF7970A_SPECIAL_FCN_REG1_SP_DIR_MODE		BIT(3)

#define TRF7970A_SPECIAL_FCN_REG1_NEXT_SLOT_37US	BIT(4)

#define TRF7970A_SPECIAL_FCN_REG1_PAR43			BIT(5)


#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_124	(0x0 << 2)

#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_120	(0x1 << 2)

#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_112	(0x2 << 2)

#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96	(0x3 << 2)

#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_4	0x0

#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_8	0x1

#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_16	0x2

#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32	0x3


#define TRF7970A_NFC_LOW_FIELD_LEVEL_RFDET(v)	((v) & 0x07)

#define TRF7970A_NFC_LOW_FIELD_LEVEL_CLEX_DIS	BIT(7)


#define TRF7970A_NFC_TARGET_LEVEL_RFDET(v)	((v) & 0x07)

#define TRF7970A_NFC_TARGET_LEVEL_HI_RF		BIT(3)

#define TRF7970A_NFC_TARGET_LEVEL_SDD_EN	BIT(5)

#define TRF7970A_NFC_TARGET_LEVEL_LD_S_4BYTES	(0x0 << 6)

#define TRF7970A_NFC_TARGET_LEVEL_LD_S_7BYTES	(0x1 << 6)

#define TRF7970A_NFC_TARGET_LEVEL_LD_S_10BYTES	(0x2 << 6)


#define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106		BIT(0)

#define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_212		BIT(1)

#define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_424		(BIT(0) | BIT(1))

#define TRF79070A_NFC_TARGET_PROTOCOL_PAS_14443B	BIT(2)

#define TRF79070A_NFC_TARGET_PROTOCOL_PAS_106		BIT(3)

#define TRF79070A_NFC_TARGET_PROTOCOL_FELICA		BIT(4)

#define TRF79070A_NFC_TARGET_PROTOCOL_RF_L		BIT(6)

#define TRF79070A_NFC_TARGET_PROTOCOL_RF_H		BIT(7)


#define TRF79070A_NFC_TARGET_PROTOCOL_106A		\
	 (TRF79070A_NFC_TARGET_PROTOCOL_RF_H |           \
          TRF79070A_NFC_TARGET_PROTOCOL_RF_L |          \
          TRF79070A_NFC_TARGET_PROTOCOL_PAS_106 |       \
          TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106)


#define TRF79070A_NFC_TARGET_PROTOCOL_106B		\
	 (TRF79070A_NFC_TARGET_PROTOCOL_RF_H |           \
          TRF79070A_NFC_TARGET_PROTOCOL_RF_L |          \
          TRF79070A_NFC_TARGET_PROTOCOL_PAS_14443B |    \
          TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106)


#define TRF79070A_NFC_TARGET_PROTOCOL_212F		\
	 (TRF79070A_NFC_TARGET_PROTOCOL_RF_H |           \
          TRF79070A_NFC_TARGET_PROTOCOL_RF_L |          \
          TRF79070A_NFC_TARGET_PROTOCOL_FELICA |        \
          TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_212)


#define TRF79070A_NFC_TARGET_PROTOCOL_424F		\
	 (TRF79070A_NFC_TARGET_PROTOCOL_RF_H |           \
          TRF79070A_NFC_TARGET_PROTOCOL_RF_L |          \
          TRF79070A_NFC_TARGET_PROTOCOL_FELICA |        \
          TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_424)


#define TRF7970A_FIFO_STATUS_OVERFLOW		BIT(7)

/* NFC (ISO/IEC 14443A) Type 2 Tag commands */

#define NFC_T2T_CMD_READ			0x30

/* ISO 15693 commands codes */

#define ISO15693_CMD_INVENTORY			0x01

#define ISO15693_CMD_READ_SINGLE_BLOCK		0x20

#define ISO15693_CMD_WRITE_SINGLE_BLOCK		0x21

#define ISO15693_CMD_LOCK_BLOCK			0x22

#define ISO15693_CMD_READ_MULTIPLE_BLOCK	0x23

#define ISO15693_CMD_WRITE_MULTIPLE_BLOCK	0x24

#define ISO15693_CMD_SELECT			0x25

#define ISO15693_CMD_RESET_TO_READY		0x26

#define ISO15693_CMD_WRITE_AFI			0x27

#define ISO15693_CMD_LOCK_AFI			0x28

#define ISO15693_CMD_WRITE_DSFID		0x29

#define ISO15693_CMD_LOCK_DSFID			0x2a

#define ISO15693_CMD_GET_SYSTEM_INFO		0x2b

#define ISO15693_CMD_GET_MULTIPLE_BLOCK_SECURITY_STATUS	0x2c

/* ISO 15693 request and response flags */

#define ISO15693_REQ_FLAG_SUB_CARRIER		BIT(0)

#define ISO15693_REQ_FLAG_DATA_RATE		BIT(1)

#define ISO15693_REQ_FLAG_INVENTORY		BIT(2)

#define ISO15693_REQ_FLAG_PROTOCOL_EXT		BIT(3)

#define ISO15693_REQ_FLAG_SELECT		BIT(4)

#define ISO15693_REQ_FLAG_AFI			BIT(4)

#define ISO15693_REQ_FLAG_ADDRESS		BIT(5)

#define ISO15693_REQ_FLAG_NB_SLOTS		BIT(5)

#define ISO15693_REQ_FLAG_OPTION		BIT(6)


#define ISO15693_REQ_FLAG_SPEED_MASK \
		(ISO15693_REQ_FLAG_SUB_CARRIER | ISO15693_REQ_FLAG_DATA_RATE)


enum trf7970a_state {
	
TRF7970A_ST_PWR_OFF,
	
TRF7970A_ST_RF_OFF,
	
TRF7970A_ST_IDLE,
	
TRF7970A_ST_IDLE_RX_BLOCKED,
	
TRF7970A_ST_WAIT_FOR_TX_FIFO,
	
TRF7970A_ST_WAIT_FOR_RX_DATA,
	
TRF7970A_ST_WAIT_FOR_RX_DATA_CONT,
	
TRF7970A_ST_WAIT_TO_ISSUE_EOF,
	
TRF7970A_ST_LISTENING,
	
TRF7970A_ST_LISTENING_MD,
	
TRF7970A_ST_MAX
};


struct trf7970a {
	
enum trf7970a_state		state;
	
struct device			*dev;
	
struct spi_device		*spi;
	
struct regulator		*regulator;
	
struct nfc_digital_dev		*ddev;
	
u32				quirks;
	
bool				is_initiator;
	
bool				aborting;
	
struct sk_buff			*tx_skb;
	
struct sk_buff			*rx_skb;
	
nfc_digital_cmd_complete_t	cb;
	
void				*cb_arg;
	
u8				chip_status_ctrl;
	
u8				iso_ctrl;
	
u8				iso_ctrl_tech;
	
u8				modulator_sys_clk_ctrl;
	
u8				special_fcn_reg1;
	
u8				io_ctrl;
	
unsigned int			guard_time;
	
int				technology;
	
int				framing;
	
u8				md_rf_tech;
	
u8				tx_cmd;
	
bool				issue_eof;
	
struct gpio_desc		*en_gpiod;
	
struct gpio_desc		*en2_gpiod;
	
struct mutex			lock;
	
unsigned int			timeout;
	
bool				ignore_timeout;
	
struct delayed_work		timeout_work;
};


static int trf7970a_cmd(struct trf7970a *trf, u8 opcode) { u8 cmd = TRF7970A_CMD_BIT_CTRL | TRF7970A_CMD_BIT_OPCODE(opcode); int ret; dev_dbg(trf->dev, "cmd: 0x%x\n", cmd); ret = spi_write(trf->spi, &cmd, 1); if (ret) dev_err(trf->dev, "%s - cmd: 0x%x, ret: %d\n", __func__, cmd, ret); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Mark A. Greer74100.00%1100.00%
Total74100.00%1100.00%


static int trf7970a_read(struct trf7970a *trf, u8 reg, u8 *val) { u8 addr = TRF7970A_CMD_BIT_RW | reg; int ret; ret = spi_write_then_read(trf->spi, &addr, 1, val, 1); if (ret) dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr, ret); dev_dbg(trf->dev, "read(0x%x): 0x%x\n", addr, *val); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Mark A. Greer82100.00%1100.00%
Total82100.00%1100.00%


static int trf7970a_read_cont(struct trf7970a *trf, u8 reg, u8 *buf, size_t len) { u8 addr = reg | TRF7970A_CMD_BIT_RW | TRF7970A_CMD_BIT_CONTINUOUS; struct spi_transfer t[2]; struct spi_message m; int ret; dev_dbg(trf->dev, "read_cont(0x%x, %zd)\n", addr, len); spi_message_init(&m); memset(&t, 0, sizeof(t)); t[0].tx_buf = &addr; t[0].len = sizeof(addr); spi_message_add_tail(&t[0], &m); t[1].rx_buf = buf; t[1].len = len; spi_message_add_tail(&t[1], &m); ret = spi_sync(trf->spi, &m); if (ret) dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr, ret); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Mark A. Greer174100.00%2100.00%
Total174100.00%2100.00%


static int trf7970a_write(struct trf7970a *trf, u8 reg, u8 val) { u8 buf[2] = { reg, val }; int ret; dev_dbg(trf->dev, "write(0x%x): 0x%x\n", reg, val); ret = spi_write(trf->spi, buf, 2); if (ret) dev_err(trf->dev, "%s - write: 0x%x 0x%x, ret: %d\n", __func__, buf[0], buf[1], ret); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Mark A. Greer88100.00%1100.00%
Total88100.00%1100.00%


static int trf7970a_read_irqstatus(struct trf7970a *trf, u8 *status) { int ret; u8 buf[2]; u8 addr; addr = TRF7970A_IRQ_STATUS | TRF7970A_CMD_BIT_RW; if (trf->quirks & TRF7970A_QUIRK_IRQ_STATUS_READ) { addr |= TRF7970A_CMD_BIT_CONTINUOUS; ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2); } else { ret = spi_write_then_read(trf->spi, &addr, 1, buf, 1); } if (ret) dev_err(trf->dev, "%s - irqstatus: Status read failed: %d\n", __func__, ret); else *status = buf[0]; return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Mark A. Greer115100.00%2100.00%
Total115100.00%2100.00%


static int trf7970a_read_target_proto(struct trf7970a *trf, u8 *target_proto) { int ret; u8 buf[2]; u8 addr; addr = TRF79070A_NFC_TARGET_PROTOCOL | TRF7970A_CMD_BIT_RW | TRF7970A_CMD_BIT_CONTINUOUS; ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2); if (ret) dev_err(trf->dev, "%s - target_proto: Read failed: %d\n", __func__, ret); else *target_proto = buf[0]; return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Mark A. Greer82100.00%1100.00%
Total82100.00%1100.00%


static int trf7970a_mode_detect(struct trf7970a *trf, u8 *rf_tech) { int ret; u8 target_proto, tech; ret = trf7970a_read_target_proto(trf, &target_proto); if (ret) return ret; switch (target_proto) { case TRF79070A_NFC_TARGET_PROTOCOL_106A: tech = NFC_DIGITAL_RF_TECH_106A; break; case TRF79070A_NFC_TARGET_PROTOCOL_106B: tech = NFC_DIGITAL_RF_TECH_106B; break; case TRF79070A_NFC_TARGET_PROTOCOL_212F: tech = NFC_DIGITAL_RF_TECH_212F; break; case TRF79070A_NFC_TARGET_PROTOCOL_424F: tech = NFC_DIGITAL_RF_TECH_424F; break; default: dev_dbg(trf->dev, "%s - mode_detect: target_proto: 0x%x\n", __func__, target_proto); return -EIO; } *rf_tech = tech; return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Mark A. Greer104100.00%1100.00%
Total104100.00%1100.00%


static void trf7970a_send_upstream(struct trf7970a *trf) { dev_kfree_skb_any(trf->tx_skb); trf->tx_skb = NULL; if (trf->rx_skb && !IS_ERR(trf->rx_skb) && !trf->aborting) print_hex_dump_debug("trf7970a rx data: ", DUMP_PREFIX_NONE, 16, 1, trf->rx_skb->data, trf->rx_skb->len, false); trf->state = TRF7970A_ST_IDLE; if (trf->aborting) { dev_dbg(trf->dev, "Abort process complete\n"); if (!IS_ERR(trf->rx_skb)) { kfree_skb(trf->rx_skb); trf->rx_skb = ERR_PTR(-ECANCELED); } trf->aborting = false; } trf->cb(trf->ddev, trf->cb_arg, trf->rx_skb); trf->rx_skb = NULL; }

Contributors

PersonTokensPropCommitsCommitProp
Mark A. Greer149100.00%1100.00%
Total149100.00%1100.00%


static void trf7970a_send_err_upstream(struct trf7970a *trf, int errno) { dev_dbg(trf->dev, "Error - state: %d, errno: %d\n", trf->state, errno); cancel_delayed_work(&trf->timeout_work); kfree_skb(trf->rx_skb); trf->rx_skb = ERR_PTR(errno); trf7970a_send_upstream(trf); }

Contributors

PersonTokensPropCommitsCommitProp
Mark A. Greer58100.00%2100.00%
Total58100.00%2100.00%


static int trf7970a_transmit(struct trf7970a *trf, struct sk_buff *skb, unsigned int len, u8 *prefix, unsigned int prefix_len) { struct spi_transfer t[2]; struct spi_message m; unsigned int timeout; int ret; print_hex_dump_debug("trf7970a tx data: ", DUMP_PREFIX_NONE, 16, 1, skb->data, len, false); spi_message_init(&m); memset(&t, 0, sizeof(t)); t[0].tx_buf = prefix; t[0].len = prefix_len; spi_message_add_tail(&t[0], &m); t[1].tx_buf = skb->data; t[1].len = len; spi_message_add_tail(&t[1], &m); ret = spi_sync(trf->spi, &m); if (ret) { dev_err(trf->dev, "%s - Can't send tx data: %d\n", __func__, ret); return ret; } skb_pull(skb, len); if (skb->len > 0) { trf->state = TRF7970A_ST_WAIT_FOR_TX_FIFO; timeout = TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT; } else { if (trf->issue_eof) { trf->state = TRF7970A_ST_WAIT_TO_ISSUE_EOF; timeout = TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF; } else { trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA; if (!trf->timeout) timeout = TRF7970A_WAIT_FOR_TX_IRQ; else timeout = trf->timeout; } } dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", timeout, trf->state); schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout)); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Mark A. Greer286100.00%4100.00%
Total286100.00%4100.00%


static void trf7970a_fill_fifo(struct trf7970a *trf) { struct sk_buff *skb = trf->tx_skb; unsigned int len; int ret; u8 fifo_bytes; u8 prefix; ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes); if (ret) { trf7970a_send_err_upstream(trf, ret); return; } dev_dbg(trf->dev, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes); fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW; /* Calculate how much more data can be written to the fifo */ len = TRF7970A_FIFO_SIZE - fifo_bytes; if (!len) { schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT)); return; } len = min(skb->len, len); prefix = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_FIFO_IO_REGISTER; ret = trf7970a_transmit(trf, skb, len, &prefix, sizeof(prefix)); if (ret) trf7970a_send_err_upstream(trf, ret); }

Contributors

PersonTokensPropCommitsCommitProp
Mark A. Greer150100.00%4100.00%
Total150100.00%4100.00%


static void trf7970a_drain_fifo(struct trf7970a *trf, u8 status) { struct sk_buff *skb = trf->rx_skb; int ret; u8 fifo_bytes; if (status & TRF7970A_IRQ_STATUS_ERROR) { trf7970a_send_err_upstream(trf, -EIO); return; } ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes); if (ret) { trf7970a_send_err_upstream(trf, ret); return; } dev_dbg(trf->dev, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes); fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW; if (!fifo_bytes) goto no_rx_data; if (fifo_bytes > skb_tailroom(skb)) { skb = skb_copy_expand(skb, skb_headroom(skb), max_t(int, fifo_bytes, TRF7970A_RX_SKB_ALLOC_SIZE), GFP_KERNEL); if (!skb) { trf7970a_send_err_upstream(trf, -ENOMEM); return; } kfree_skb(trf->rx_skb); trf->rx_skb = skb; } ret = trf7970a_read_cont(trf, TRF7970A_FIFO_IO_REGISTER, skb_put(skb, fifo_bytes), fifo_bytes); if (ret) { trf7970a_send_err_upstream(trf, ret); return; } /* If received Type 2 ACK/NACK, shift right 4 bits and pass up */ if ((trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T) && (skb->len == 1) && (trf->special_fcn_reg1 == TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX)) { skb->data[0] >>= 4; status = TRF7970A_IRQ_STATUS_SRX; } else { trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA_CONT; ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes); if (ret) { trf7970a_send_err_upstream(trf, ret); return; } fifo_bytes &=