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