]>
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 | ||
1568bfef | 128 | #define TRF7970A_FIFO_SIZE 127 |
165063f1 MG |
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 | ||
4542e834 | 564 | fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW; |
165063f1 MG |
565 | |
566 | /* Calculate how much more data can be written to the fifo */ | |
567 | len = TRF7970A_FIFO_SIZE - fifo_bytes; | |
568 | len = min(skb->len, len); | |
569 | ||
570 | ret = trf7970a_transmit(trf, skb, len); | |
571 | if (ret) | |
572 | trf7970a_send_err_upstream(trf, ret); | |
573 | } | |
574 | ||
575 | static void trf7970a_drain_fifo(struct trf7970a *trf, u8 status) | |
576 | { | |
577 | struct sk_buff *skb = trf->rx_skb; | |
578 | int ret; | |
579 | u8 fifo_bytes; | |
580 | ||
581 | if (status & TRF7970A_IRQ_STATUS_ERROR) { | |
582 | trf7970a_send_err_upstream(trf, -EIO); | |
583 | return; | |
584 | } | |
585 | ||
586 | ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes); | |
587 | if (ret) { | |
588 | trf7970a_send_err_upstream(trf, ret); | |
589 | return; | |
590 | } | |
591 | ||
592 | dev_dbg(trf->dev, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes); | |
593 | ||
4542e834 MG |
594 | fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW; |
595 | ||
165063f1 MG |
596 | if (!fifo_bytes) |
597 | goto no_rx_data; | |
598 | ||
165063f1 MG |
599 | if (fifo_bytes > skb_tailroom(skb)) { |
600 | skb = skb_copy_expand(skb, skb_headroom(skb), | |
601 | max_t(int, fifo_bytes, | |
602 | TRF7970A_RX_SKB_ALLOC_SIZE), | |
603 | GFP_KERNEL); | |
604 | if (!skb) { | |
605 | trf7970a_send_err_upstream(trf, -ENOMEM); | |
606 | return; | |
607 | } | |
608 | ||
609 | kfree_skb(trf->rx_skb); | |
610 | trf->rx_skb = skb; | |
611 | } | |
612 | ||
613 | ret = trf7970a_read_cont(trf, TRF7970A_FIFO_IO_REGISTER, | |
614 | skb_put(skb, fifo_bytes), fifo_bytes); | |
615 | if (ret) { | |
616 | trf7970a_send_err_upstream(trf, ret); | |
617 | return; | |
618 | } | |
619 | ||
620 | /* If received Type 2 ACK/NACK, shift right 4 bits and pass up */ | |
621 | if ((trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T) && (skb->len == 1) && | |
622 | (trf->special_fcn_reg1 == | |
623 | TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX)) { | |
624 | skb->data[0] >>= 4; | |
625 | status = TRF7970A_IRQ_STATUS_SRX; | |
626 | } else { | |
627 | trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA_CONT; | |
628 | } | |
629 | ||
630 | no_rx_data: | |
631 | if (status == TRF7970A_IRQ_STATUS_SRX) { /* Receive complete */ | |
632 | trf7970a_send_upstream(trf); | |
633 | return; | |
634 | } | |
635 | ||
636 | dev_dbg(trf->dev, "Setting timeout for %d ms\n", | |
637 | TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT); | |
638 | ||
639 | schedule_delayed_work(&trf->timeout_work, | |
640 | msecs_to_jiffies(TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT)); | |
641 | } | |
642 | ||
643 | static irqreturn_t trf7970a_irq(int irq, void *dev_id) | |
644 | { | |
645 | struct trf7970a *trf = dev_id; | |
646 | int ret; | |
647 | u8 status; | |
648 | ||
649 | mutex_lock(&trf->lock); | |
650 | ||
651 | if (trf->state == TRF7970A_ST_OFF) { | |
652 | mutex_unlock(&trf->lock); | |
653 | return IRQ_NONE; | |
654 | } | |
655 | ||
656 | ret = trf7970a_read_irqstatus(trf, &status); | |
657 | if (ret) { | |
658 | mutex_unlock(&trf->lock); | |
659 | return IRQ_NONE; | |
660 | } | |
661 | ||
662 | dev_dbg(trf->dev, "IRQ - state: %d, status: 0x%x\n", trf->state, | |
663 | status); | |
664 | ||
665 | if (!status) { | |
666 | mutex_unlock(&trf->lock); | |
667 | return IRQ_NONE; | |
668 | } | |
669 | ||
670 | switch (trf->state) { | |
671 | case TRF7970A_ST_IDLE: | |
672 | case TRF7970A_ST_IDLE_RX_BLOCKED: | |
673 | /* If getting interrupts caused by RF noise, turn off the | |
674 | * receiver to avoid unnecessary interrupts. It will be | |
675 | * turned back on in trf7970a_in_send_cmd() when the next | |
676 | * command is issued. | |
677 | */ | |
678 | if (status & TRF7970A_IRQ_STATUS_ERROR) { | |
679 | trf7970a_cmd(trf, TRF7970A_CMD_BLOCK_RX); | |
680 | trf->state = TRF7970A_ST_IDLE_RX_BLOCKED; | |
681 | } | |
682 | ||
683 | trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); | |
684 | break; | |
685 | case TRF7970A_ST_WAIT_FOR_TX_FIFO: | |
686 | if (status & TRF7970A_IRQ_STATUS_TX) { | |
687 | trf->ignore_timeout = | |
688 | !cancel_delayed_work(&trf->timeout_work); | |
689 | trf7970a_fill_fifo(trf); | |
690 | } else { | |
691 | trf7970a_send_err_upstream(trf, -EIO); | |
692 | } | |
693 | break; | |
694 | case TRF7970A_ST_WAIT_FOR_RX_DATA: | |
695 | case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: | |
696 | if (status & TRF7970A_IRQ_STATUS_SRX) { | |
697 | trf->ignore_timeout = | |
698 | !cancel_delayed_work(&trf->timeout_work); | |
699 | trf7970a_drain_fifo(trf, status); | |
4dd836e4 MG |
700 | } else if (status == TRF7970A_IRQ_STATUS_TX) { |
701 | trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); | |
702 | } else { | |
165063f1 MG |
703 | trf7970a_send_err_upstream(trf, -EIO); |
704 | } | |
705 | break; | |
9d9304b3 MG |
706 | case TRF7970A_ST_WAIT_TO_ISSUE_EOF: |
707 | if (status != TRF7970A_IRQ_STATUS_TX) | |
708 | trf7970a_send_err_upstream(trf, -EIO); | |
709 | break; | |
165063f1 MG |
710 | default: |
711 | dev_err(trf->dev, "%s - Driver in invalid state: %d\n", | |
712 | __func__, trf->state); | |
713 | } | |
714 | ||
715 | mutex_unlock(&trf->lock); | |
716 | return IRQ_HANDLED; | |
717 | } | |
718 | ||
9d9304b3 MG |
719 | static void trf7970a_issue_eof(struct trf7970a *trf) |
720 | { | |
721 | int ret; | |
722 | ||
723 | dev_dbg(trf->dev, "Issuing EOF\n"); | |
724 | ||
725 | ret = trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET); | |
726 | if (ret) | |
727 | trf7970a_send_err_upstream(trf, ret); | |
728 | ||
729 | ret = trf7970a_cmd(trf, TRF7970A_CMD_EOF); | |
730 | if (ret) | |
731 | trf7970a_send_err_upstream(trf, ret); | |
732 | ||
733 | trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA; | |
734 | ||
735 | dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", | |
736 | trf->timeout, trf->state); | |
737 | ||
738 | schedule_delayed_work(&trf->timeout_work, | |
739 | msecs_to_jiffies(trf->timeout)); | |
740 | } | |
741 | ||
165063f1 MG |
742 | static void trf7970a_timeout_work_handler(struct work_struct *work) |
743 | { | |
744 | struct trf7970a *trf = container_of(work, struct trf7970a, | |
745 | timeout_work.work); | |
746 | ||
747 | dev_dbg(trf->dev, "Timeout - state: %d, ignore_timeout: %d\n", | |
748 | trf->state, trf->ignore_timeout); | |
749 | ||
750 | mutex_lock(&trf->lock); | |
751 | ||
752 | if (trf->ignore_timeout) | |
753 | trf->ignore_timeout = false; | |
754 | else if (trf->state == TRF7970A_ST_WAIT_FOR_RX_DATA_CONT) | |
755 | trf7970a_send_upstream(trf); /* No more rx data so send up */ | |
9d9304b3 MG |
756 | else if (trf->state == TRF7970A_ST_WAIT_TO_ISSUE_EOF) |
757 | trf7970a_issue_eof(trf); | |
165063f1 MG |
758 | else |
759 | trf7970a_send_err_upstream(trf, -ETIMEDOUT); | |
760 | ||
761 | mutex_unlock(&trf->lock); | |
762 | } | |
763 | ||
764 | static int trf7970a_init(struct trf7970a *trf) | |
765 | { | |
766 | int ret; | |
767 | ||
768 | dev_dbg(trf->dev, "Initializing device - state: %d\n", trf->state); | |
769 | ||
770 | ret = trf7970a_cmd(trf, TRF7970A_CMD_SOFT_INIT); | |
771 | if (ret) | |
772 | goto err_out; | |
773 | ||
774 | ret = trf7970a_cmd(trf, TRF7970A_CMD_IDLE); | |
775 | if (ret) | |
776 | goto err_out; | |
777 | ||
6c08df42 MG |
778 | ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL, 0); |
779 | if (ret) | |
780 | goto err_out; | |
781 | ||
782 | trf->modulator_sys_clk_ctrl = 0; | |
783 | ||
b887eb09 MG |
784 | /* Must clear NFC Target Detection Level reg due to erratum */ |
785 | ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0); | |
786 | if (ret) | |
787 | goto err_out; | |
788 | ||
165063f1 MG |
789 | ret = trf7970a_write(trf, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS, |
790 | TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 | | |
791 | TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32); | |
792 | if (ret) | |
793 | goto err_out; | |
794 | ||
795 | ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, 0); | |
796 | if (ret) | |
797 | goto err_out; | |
798 | ||
799 | trf->special_fcn_reg1 = 0; | |
800 | ||
49d19cc7 | 801 | trf->iso_ctrl = 0xff; |
165063f1 MG |
802 | return 0; |
803 | ||
804 | err_out: | |
805 | dev_dbg(trf->dev, "Couldn't init device: %d\n", ret); | |
806 | return ret; | |
807 | } | |
808 | ||
809 | static void trf7970a_switch_rf_off(struct trf7970a *trf) | |
810 | { | |
811 | dev_dbg(trf->dev, "Switching rf off\n"); | |
812 | ||
a1d2dc5b MG |
813 | trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON; |
814 | ||
815 | trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, trf->chip_status_ctrl); | |
816 | ||
165063f1 MG |
817 | trf->aborting = false; |
818 | trf->state = TRF7970A_ST_OFF; | |
e6403b7c MG |
819 | |
820 | pm_runtime_mark_last_busy(trf->dev); | |
821 | pm_runtime_put_autosuspend(trf->dev); | |
165063f1 MG |
822 | } |
823 | ||
18422e68 | 824 | static void trf7970a_switch_rf_on(struct trf7970a *trf) |
165063f1 | 825 | { |
165063f1 MG |
826 | dev_dbg(trf->dev, "Switching rf on\n"); |
827 | ||
e6403b7c | 828 | pm_runtime_get_sync(trf->dev); |
165063f1 | 829 | |
e6403b7c | 830 | trf->state = TRF7970A_ST_IDLE; |
165063f1 MG |
831 | } |
832 | ||
833 | static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on) | |
834 | { | |
835 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
165063f1 MG |
836 | |
837 | dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on); | |
838 | ||
839 | mutex_lock(&trf->lock); | |
840 | ||
841 | if (on) { | |
842 | switch (trf->state) { | |
843 | case TRF7970A_ST_OFF: | |
18422e68 | 844 | trf7970a_switch_rf_on(trf); |
165063f1 MG |
845 | break; |
846 | case TRF7970A_ST_IDLE: | |
847 | case TRF7970A_ST_IDLE_RX_BLOCKED: | |
848 | break; | |
849 | default: | |
850 | dev_err(trf->dev, "%s - Invalid request: %d %d\n", | |
851 | __func__, trf->state, on); | |
852 | trf7970a_switch_rf_off(trf); | |
853 | } | |
854 | } else { | |
855 | switch (trf->state) { | |
856 | case TRF7970A_ST_OFF: | |
857 | break; | |
858 | default: | |
859 | dev_err(trf->dev, "%s - Invalid request: %d %d\n", | |
860 | __func__, trf->state, on); | |
861 | /* FALLTHROUGH */ | |
862 | case TRF7970A_ST_IDLE: | |
863 | case TRF7970A_ST_IDLE_RX_BLOCKED: | |
864 | trf7970a_switch_rf_off(trf); | |
865 | } | |
866 | } | |
867 | ||
868 | mutex_unlock(&trf->lock); | |
18422e68 | 869 | return 0; |
165063f1 MG |
870 | } |
871 | ||
872 | static int trf7970a_config_rf_tech(struct trf7970a *trf, int tech) | |
873 | { | |
874 | int ret = 0; | |
875 | ||
876 | dev_dbg(trf->dev, "rf technology: %d\n", tech); | |
877 | ||
878 | switch (tech) { | |
879 | case NFC_DIGITAL_RF_TECH_106A: | |
49d19cc7 | 880 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443A_106; |
12e9ade3 | 881 | trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK; |
165063f1 | 882 | break; |
742b1f9f MG |
883 | case NFC_DIGITAL_RF_TECH_106B: |
884 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443B_106; | |
885 | trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10; | |
886 | break; | |
6857bb96 MG |
887 | case NFC_DIGITAL_RF_TECH_212F: |
888 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_212; | |
889 | trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10; | |
890 | break; | |
891 | case NFC_DIGITAL_RF_TECH_424F: | |
892 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_424; | |
893 | trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10; | |
894 | break; | |
9d9304b3 | 895 | case NFC_DIGITAL_RF_TECH_ISO15693: |
49d19cc7 | 896 | trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648; |
12e9ade3 | 897 | trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK; |
9d9304b3 | 898 | break; |
165063f1 MG |
899 | default: |
900 | dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech); | |
901 | return -EINVAL; | |
902 | } | |
903 | ||
904 | trf->technology = tech; | |
905 | ||
906 | return ret; | |
907 | } | |
908 | ||
909 | static int trf7970a_config_framing(struct trf7970a *trf, int framing) | |
910 | { | |
49d19cc7 MG |
911 | u8 iso_ctrl = trf->iso_ctrl_tech; |
912 | int ret; | |
913 | ||
165063f1 MG |
914 | dev_dbg(trf->dev, "framing: %d\n", framing); |
915 | ||
916 | switch (framing) { | |
917 | case NFC_DIGITAL_FRAMING_NFCA_SHORT: | |
918 | case NFC_DIGITAL_FRAMING_NFCA_STANDARD: | |
919 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC; | |
49d19cc7 | 920 | iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N; |
165063f1 MG |
921 | break; |
922 | case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A: | |
80062891 | 923 | case NFC_DIGITAL_FRAMING_NFCA_T4T: |
742b1f9f MG |
924 | case NFC_DIGITAL_FRAMING_NFCB: |
925 | case NFC_DIGITAL_FRAMING_NFCB_T4T: | |
6857bb96 MG |
926 | case NFC_DIGITAL_FRAMING_NFCF: |
927 | case NFC_DIGITAL_FRAMING_NFCF_T3T: | |
9d9304b3 MG |
928 | case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY: |
929 | case NFC_DIGITAL_FRAMING_ISO15693_T5T: | |
165063f1 | 930 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT; |
49d19cc7 | 931 | iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N; |
165063f1 MG |
932 | break; |
933 | case NFC_DIGITAL_FRAMING_NFCA_T2T: | |
934 | trf->tx_cmd = TRF7970A_CMD_TRANSMIT; | |
49d19cc7 | 935 | iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N; |
165063f1 MG |
936 | break; |
937 | default: | |
938 | dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing); | |
939 | return -EINVAL; | |
940 | } | |
941 | ||
942 | trf->framing = framing; | |
943 | ||
49d19cc7 MG |
944 | if (iso_ctrl != trf->iso_ctrl) { |
945 | ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl); | |
946 | if (ret) | |
947 | return ret; | |
948 | ||
949 | trf->iso_ctrl = iso_ctrl; | |
a0822a7e MG |
950 | |
951 | ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL, | |
12e9ade3 | 952 | trf->modulator_sys_clk_ctrl); |
a0822a7e MG |
953 | if (ret) |
954 | return ret; | |
49d19cc7 MG |
955 | } |
956 | ||
a1d2dc5b MG |
957 | if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) { |
958 | ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, | |
959 | trf->chip_status_ctrl | | |
960 | TRF7970A_CHIP_STATUS_RF_ON); | |
961 | if (ret) | |
962 | return ret; | |
963 | ||
964 | trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON; | |
965 | ||
966 | usleep_range(5000, 6000); | |
967 | } | |
968 | ||
49d19cc7 | 969 | return 0; |
165063f1 MG |
970 | } |
971 | ||
972 | static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type, | |
973 | int param) | |
974 | { | |
975 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
18422e68 | 976 | int ret; |
165063f1 MG |
977 | |
978 | dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param); | |
979 | ||
980 | mutex_lock(&trf->lock); | |
981 | ||
18422e68 MG |
982 | if (trf->state == TRF7970A_ST_OFF) |
983 | trf7970a_switch_rf_on(trf); | |
165063f1 MG |
984 | |
985 | switch (type) { | |
986 | case NFC_DIGITAL_CONFIG_RF_TECH: | |
987 | ret = trf7970a_config_rf_tech(trf, param); | |
988 | break; | |
989 | case NFC_DIGITAL_CONFIG_FRAMING: | |
990 | ret = trf7970a_config_framing(trf, param); | |
991 | break; | |
992 | default: | |
993 | dev_dbg(trf->dev, "Unknown type: %d\n", type); | |
994 | ret = -EINVAL; | |
995 | } | |
996 | ||
165063f1 MG |
997 | mutex_unlock(&trf->lock); |
998 | return ret; | |
999 | } | |
1000 | ||
9d9304b3 MG |
1001 | static int trf7970a_is_iso15693_write_or_lock(u8 cmd) |
1002 | { | |
1003 | switch (cmd) { | |
1004 | case ISO15693_CMD_WRITE_SINGLE_BLOCK: | |
1005 | case ISO15693_CMD_LOCK_BLOCK: | |
1006 | case ISO15693_CMD_WRITE_MULTIPLE_BLOCK: | |
1007 | case ISO15693_CMD_WRITE_AFI: | |
1008 | case ISO15693_CMD_LOCK_AFI: | |
1009 | case ISO15693_CMD_WRITE_DSFID: | |
1010 | case ISO15693_CMD_LOCK_DSFID: | |
1011 | return 1; | |
1012 | break; | |
1013 | default: | |
1014 | return 0; | |
1015 | } | |
1016 | } | |
1017 | ||
165063f1 MG |
1018 | static int trf7970a_per_cmd_config(struct trf7970a *trf, struct sk_buff *skb) |
1019 | { | |
1020 | u8 *req = skb->data; | |
9d9304b3 | 1021 | u8 special_fcn_reg1, iso_ctrl; |
165063f1 MG |
1022 | int ret; |
1023 | ||
9d9304b3 MG |
1024 | trf->issue_eof = false; |
1025 | ||
165063f1 MG |
1026 | /* When issuing Type 2 read command, make sure the '4_bit_RX' bit in |
1027 | * special functions register 1 is cleared; otherwise, its a write or | |
1028 | * sector select command and '4_bit_RX' must be set. | |
9d9304b3 MG |
1029 | * |
1030 | * When issuing an ISO 15693 command, inspect the flags byte to see | |
1031 | * what speed to use. Also, remember if the OPTION flag is set on | |
1032 | * a Type 5 write or lock command so the driver will know that it | |
1033 | * has to send an EOF in order to get a response. | |
165063f1 MG |
1034 | */ |
1035 | if ((trf->technology == NFC_DIGITAL_RF_TECH_106A) && | |
1036 | (trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T)) { | |
1037 | if (req[0] == NFC_T2T_CMD_READ) | |
1038 | special_fcn_reg1 = 0; | |
1039 | else | |
1040 | special_fcn_reg1 = TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX; | |
1041 | ||
1042 | if (special_fcn_reg1 != trf->special_fcn_reg1) { | |
1043 | ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, | |
1044 | special_fcn_reg1); | |
1045 | if (ret) | |
1046 | return ret; | |
1047 | ||
1048 | trf->special_fcn_reg1 = special_fcn_reg1; | |
1049 | } | |
9d9304b3 MG |
1050 | } else if (trf->technology == NFC_DIGITAL_RF_TECH_ISO15693) { |
1051 | iso_ctrl = trf->iso_ctrl & ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK; | |
1052 | ||
1053 | switch (req[0] & ISO15693_REQ_FLAG_SPEED_MASK) { | |
1054 | case 0x00: | |
1055 | iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_662; | |
1056 | break; | |
1057 | case ISO15693_REQ_FLAG_SUB_CARRIER: | |
1058 | iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a; | |
1059 | break; | |
1060 | case ISO15693_REQ_FLAG_DATA_RATE: | |
1061 | iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648; | |
1062 | break; | |
1063 | case (ISO15693_REQ_FLAG_SUB_CARRIER | | |
1064 | ISO15693_REQ_FLAG_DATA_RATE): | |
1065 | iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669; | |
1066 | break; | |
1067 | } | |
1068 | ||
1069 | if (iso_ctrl != trf->iso_ctrl) { | |
1070 | ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl); | |
1071 | if (ret) | |
1072 | return ret; | |
1073 | ||
1074 | trf->iso_ctrl = iso_ctrl; | |
1075 | } | |
1076 | ||
1077 | if ((trf->framing == NFC_DIGITAL_FRAMING_ISO15693_T5T) && | |
1078 | trf7970a_is_iso15693_write_or_lock(req[1]) && | |
1079 | (req[0] & ISO15693_REQ_FLAG_OPTION)) | |
1080 | trf->issue_eof = true; | |
165063f1 MG |
1081 | } |
1082 | ||
1083 | return 0; | |
1084 | } | |
1085 | ||
1086 | static int trf7970a_in_send_cmd(struct nfc_digital_dev *ddev, | |
1087 | struct sk_buff *skb, u16 timeout, | |
1088 | nfc_digital_cmd_complete_t cb, void *arg) | |
1089 | { | |
1090 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
1091 | char *prefix; | |
1092 | unsigned int len; | |
1093 | int ret; | |
1094 | ||
1095 | dev_dbg(trf->dev, "New request - state: %d, timeout: %d ms, len: %d\n", | |
1096 | trf->state, timeout, skb->len); | |
1097 | ||
1098 | if (skb->len > TRF7970A_TX_MAX) | |
1099 | return -EINVAL; | |
1100 | ||
1101 | mutex_lock(&trf->lock); | |
1102 | ||
1103 | if ((trf->state != TRF7970A_ST_IDLE) && | |
1104 | (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) { | |
1105 | dev_err(trf->dev, "%s - Bogus state: %d\n", __func__, | |
1106 | trf->state); | |
1107 | ret = -EIO; | |
1108 | goto out_err; | |
1109 | } | |
1110 | ||
1111 | if (trf->aborting) { | |
1112 | dev_dbg(trf->dev, "Abort process complete\n"); | |
1113 | trf->aborting = false; | |
1114 | ret = -ECANCELED; | |
1115 | goto out_err; | |
1116 | } | |
1117 | ||
1118 | trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE, | |
1119 | GFP_KERNEL); | |
1120 | if (!trf->rx_skb) { | |
1121 | dev_dbg(trf->dev, "Can't alloc rx_skb\n"); | |
1122 | ret = -ENOMEM; | |
1123 | goto out_err; | |
1124 | } | |
1125 | ||
1126 | if (trf->state == TRF7970A_ST_IDLE_RX_BLOCKED) { | |
1127 | ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX); | |
1128 | if (ret) | |
1129 | goto out_err; | |
1130 | ||
1131 | trf->state = TRF7970A_ST_IDLE; | |
1132 | } | |
1133 | ||
1134 | ret = trf7970a_per_cmd_config(trf, skb); | |
1135 | if (ret) | |
1136 | goto out_err; | |
1137 | ||
1138 | trf->ddev = ddev; | |
1139 | trf->tx_skb = skb; | |
1140 | trf->cb = cb; | |
1141 | trf->cb_arg = arg; | |
1142 | trf->timeout = timeout; | |
1143 | trf->ignore_timeout = false; | |
1144 | ||
1145 | len = skb->len; | |
1146 | prefix = skb_push(skb, TRF7970A_TX_SKB_HEADROOM); | |
1147 | ||
1148 | /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends | |
1149 | * on what the current framing is, the address of the TX length byte 1 | |
1150 | * register (0x1d), and the 2 byte length of the data to be transmitted. | |
1151 | */ | |
1152 | prefix[0] = TRF7970A_CMD_BIT_CTRL | | |
1153 | TRF7970A_CMD_BIT_OPCODE(TRF7970A_CMD_FIFO_RESET); | |
1154 | prefix[1] = TRF7970A_CMD_BIT_CTRL | | |
1155 | TRF7970A_CMD_BIT_OPCODE(trf->tx_cmd); | |
1156 | prefix[2] = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_TX_LENGTH_BYTE1; | |
1157 | ||
1158 | if (trf->framing == NFC_DIGITAL_FRAMING_NFCA_SHORT) { | |
1159 | prefix[3] = 0x00; | |
1160 | prefix[4] = 0x0f; /* 7 bits */ | |
1161 | } else { | |
1162 | prefix[3] = (len & 0xf00) >> 4; | |
1163 | prefix[3] |= ((len & 0xf0) >> 4); | |
1164 | prefix[4] = ((len & 0x0f) << 4); | |
1165 | } | |
1166 | ||
1167 | len = min_t(int, skb->len, TRF7970A_FIFO_SIZE); | |
1168 | ||
165063f1 MG |
1169 | ret = trf7970a_transmit(trf, skb, len); |
1170 | if (ret) { | |
1171 | kfree_skb(trf->rx_skb); | |
1172 | trf->rx_skb = NULL; | |
1173 | } | |
1174 | ||
1175 | out_err: | |
1176 | mutex_unlock(&trf->lock); | |
1177 | return ret; | |
1178 | } | |
1179 | ||
1180 | static int trf7970a_tg_configure_hw(struct nfc_digital_dev *ddev, | |
1181 | int type, int param) | |
1182 | { | |
1183 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
1184 | ||
1185 | dev_dbg(trf->dev, "Unsupported interface\n"); | |
1186 | ||
1187 | return -EINVAL; | |
1188 | } | |
1189 | ||
1190 | static int trf7970a_tg_send_cmd(struct nfc_digital_dev *ddev, | |
1191 | struct sk_buff *skb, u16 timeout, | |
1192 | nfc_digital_cmd_complete_t cb, void *arg) | |
1193 | { | |
1194 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
1195 | ||
1196 | dev_dbg(trf->dev, "Unsupported interface\n"); | |
1197 | ||
1198 | return -EINVAL; | |
1199 | } | |
1200 | ||
1201 | static int trf7970a_tg_listen(struct nfc_digital_dev *ddev, | |
1202 | u16 timeout, nfc_digital_cmd_complete_t cb, void *arg) | |
1203 | { | |
1204 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
1205 | ||
1206 | dev_dbg(trf->dev, "Unsupported interface\n"); | |
1207 | ||
1208 | return -EINVAL; | |
1209 | } | |
1210 | ||
165063f1 MG |
1211 | static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev) |
1212 | { | |
1213 | struct trf7970a *trf = nfc_digital_get_drvdata(ddev); | |
1214 | ||
1215 | dev_dbg(trf->dev, "Abort process initiated\n"); | |
1216 | ||
1217 | mutex_lock(&trf->lock); | |
5876bc75 MG |
1218 | |
1219 | switch (trf->state) { | |
1220 | case TRF7970A_ST_WAIT_FOR_TX_FIFO: | |
1221 | case TRF7970A_ST_WAIT_FOR_RX_DATA: | |
1222 | case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: | |
1223 | case TRF7970A_ST_WAIT_TO_ISSUE_EOF: | |
1224 | trf->aborting = true; | |
1225 | break; | |
1226 | default: | |
1227 | break; | |
1228 | } | |
1229 | ||
165063f1 MG |
1230 | mutex_unlock(&trf->lock); |
1231 | } | |
1232 | ||
1233 | static struct nfc_digital_ops trf7970a_nfc_ops = { | |
1234 | .in_configure_hw = trf7970a_in_configure_hw, | |
1235 | .in_send_cmd = trf7970a_in_send_cmd, | |
1236 | .tg_configure_hw = trf7970a_tg_configure_hw, | |
1237 | .tg_send_cmd = trf7970a_tg_send_cmd, | |
1238 | .tg_listen = trf7970a_tg_listen, | |
165063f1 MG |
1239 | .switch_rf = trf7970a_switch_rf, |
1240 | .abort_cmd = trf7970a_abort_cmd, | |
1241 | }; | |
1242 | ||
fd0c8280 MG |
1243 | static int trf7970a_get_autosuspend_delay(struct device_node *np) |
1244 | { | |
1245 | int autosuspend_delay, ret; | |
1246 | ||
1247 | ret = of_property_read_u32(np, "autosuspend-delay", &autosuspend_delay); | |
1248 | if (ret) | |
1249 | autosuspend_delay = TRF7970A_AUTOSUSPEND_DELAY; | |
1250 | ||
fd0c8280 MG |
1251 | return autosuspend_delay; |
1252 | } | |
1253 | ||
3bd14233 MG |
1254 | static int trf7970a_get_vin_voltage_override(struct device_node *np, |
1255 | u32 *vin_uvolts) | |
1256 | { | |
1257 | return of_property_read_u32(np, "vin-voltage-override", vin_uvolts); | |
1258 | } | |
1259 | ||
165063f1 MG |
1260 | static int trf7970a_probe(struct spi_device *spi) |
1261 | { | |
1262 | struct device_node *np = spi->dev.of_node; | |
165063f1 | 1263 | struct trf7970a *trf; |
fd0c8280 | 1264 | int uvolts, autosuspend_delay, ret; |
165063f1 MG |
1265 | |
1266 | if (!np) { | |
1267 | dev_err(&spi->dev, "No Device Tree entry\n"); | |
1268 | return -EINVAL; | |
1269 | } | |
1270 | ||
1271 | trf = devm_kzalloc(&spi->dev, sizeof(*trf), GFP_KERNEL); | |
1272 | if (!trf) | |
1273 | return -ENOMEM; | |
1274 | ||
1275 | trf->state = TRF7970A_ST_OFF; | |
1276 | trf->dev = &spi->dev; | |
1277 | trf->spi = spi; | |
165063f1 MG |
1278 | |
1279 | spi->mode = SPI_MODE_1; | |
1280 | spi->bits_per_word = 8; | |
1281 | ||
24707296 MG |
1282 | ret = spi_setup(spi); |
1283 | if (ret < 0) { | |
1284 | dev_err(trf->dev, "Can't set up SPI Communication\n"); | |
1285 | return ret; | |
1286 | } | |
1287 | ||
772079eb MG |
1288 | if (of_property_read_bool(np, "irq-status-read-quirk")) |
1289 | trf->quirks |= TRF7970A_QUIRK_IRQ_STATUS_READ; | |
1290 | ||
165063f1 MG |
1291 | /* There are two enable pins - both must be present */ |
1292 | trf->en_gpio = of_get_named_gpio(np, "ti,enable-gpios", 0); | |
1293 | if (!gpio_is_valid(trf->en_gpio)) { | |
1294 | dev_err(trf->dev, "No EN GPIO property\n"); | |
1295 | return trf->en_gpio; | |
1296 | } | |
1297 | ||
1298 | ret = devm_gpio_request_one(trf->dev, trf->en_gpio, | |
f23b7352 | 1299 | GPIOF_DIR_OUT | GPIOF_INIT_LOW, "trf7970a EN"); |
165063f1 MG |
1300 | if (ret) { |
1301 | dev_err(trf->dev, "Can't request EN GPIO: %d\n", ret); | |
1302 | return ret; | |
1303 | } | |
1304 | ||
1305 | trf->en2_gpio = of_get_named_gpio(np, "ti,enable-gpios", 1); | |
1306 | if (!gpio_is_valid(trf->en2_gpio)) { | |
1307 | dev_err(trf->dev, "No EN2 GPIO property\n"); | |
1308 | return trf->en2_gpio; | |
1309 | } | |
1310 | ||
1311 | ret = devm_gpio_request_one(trf->dev, trf->en2_gpio, | |
f23b7352 | 1312 | GPIOF_DIR_OUT | GPIOF_INIT_LOW, "trf7970a EN2"); |
165063f1 MG |
1313 | if (ret) { |
1314 | dev_err(trf->dev, "Can't request EN2 GPIO: %d\n", ret); | |
1315 | return ret; | |
1316 | } | |
1317 | ||
95064bd9 MG |
1318 | if (of_property_read_bool(np, "en2-rf-quirk")) |
1319 | trf->quirks |= TRF7970A_QUIRK_EN2_MUST_STAY_LOW; | |
1320 | ||
165063f1 MG |
1321 | ret = devm_request_threaded_irq(trf->dev, spi->irq, NULL, |
1322 | trf7970a_irq, IRQF_TRIGGER_RISING | IRQF_ONESHOT, | |
1323 | "trf7970a", trf); | |
1324 | if (ret) { | |
1325 | dev_err(trf->dev, "Can't request IRQ#%d: %d\n", spi->irq, ret); | |
1326 | return ret; | |
1327 | } | |
1328 | ||
1329 | mutex_init(&trf->lock); | |
1330 | INIT_DELAYED_WORK(&trf->timeout_work, trf7970a_timeout_work_handler); | |
1331 | ||
1332 | trf->regulator = devm_regulator_get(&spi->dev, "vin"); | |
1333 | if (IS_ERR(trf->regulator)) { | |
1334 | ret = PTR_ERR(trf->regulator); | |
1335 | dev_err(trf->dev, "Can't get VIN regulator: %d\n", ret); | |
1336 | goto err_destroy_lock; | |
1337 | } | |
1338 | ||
1339 | ret = regulator_enable(trf->regulator); | |
1340 | if (ret) { | |
1341 | dev_err(trf->dev, "Can't enable VIN: %d\n", ret); | |
1342 | goto err_destroy_lock; | |
1343 | } | |
1344 | ||
3bd14233 MG |
1345 | ret = trf7970a_get_vin_voltage_override(np, &uvolts); |
1346 | if (ret) | |
1347 | uvolts = regulator_get_voltage(trf->regulator); | |
ebcc5a0d MG |
1348 | |
1349 | if (uvolts > 4000000) | |
1350 | trf->chip_status_ctrl = TRF7970A_CHIP_STATUS_VRS5_3; | |
1351 | ||
165063f1 MG |
1352 | trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops, |
1353 | TRF7970A_SUPPORTED_PROTOCOLS, | |
1354 | NFC_DIGITAL_DRV_CAPS_IN_CRC, TRF7970A_TX_SKB_HEADROOM, | |
1355 | 0); | |
1356 | if (!trf->ddev) { | |
1357 | dev_err(trf->dev, "Can't allocate NFC digital device\n"); | |
1358 | ret = -ENOMEM; | |
1359 | goto err_disable_regulator; | |
1360 | } | |
1361 | ||
1362 | nfc_digital_set_parent_dev(trf->ddev, trf->dev); | |
1363 | nfc_digital_set_drvdata(trf->ddev, trf); | |
1364 | spi_set_drvdata(spi, trf); | |
1365 | ||
fd0c8280 MG |
1366 | autosuspend_delay = trf7970a_get_autosuspend_delay(np); |
1367 | ||
1368 | pm_runtime_set_autosuspend_delay(trf->dev, autosuspend_delay); | |
e6403b7c MG |
1369 | pm_runtime_use_autosuspend(trf->dev); |
1370 | pm_runtime_enable(trf->dev); | |
1371 | ||
165063f1 MG |
1372 | ret = nfc_digital_register_device(trf->ddev); |
1373 | if (ret) { | |
1374 | dev_err(trf->dev, "Can't register NFC digital device: %d\n", | |
1375 | ret); | |
1376 | goto err_free_ddev; | |
1377 | } | |
1378 | ||
1379 | return 0; | |
1380 | ||
1381 | err_free_ddev: | |
e6403b7c | 1382 | pm_runtime_disable(trf->dev); |
165063f1 MG |
1383 | nfc_digital_free_device(trf->ddev); |
1384 | err_disable_regulator: | |
1385 | regulator_disable(trf->regulator); | |
1386 | err_destroy_lock: | |
1387 | mutex_destroy(&trf->lock); | |
1388 | return ret; | |
1389 | } | |
1390 | ||
1391 | static int trf7970a_remove(struct spi_device *spi) | |
1392 | { | |
1393 | struct trf7970a *trf = spi_get_drvdata(spi); | |
1394 | ||
1395 | mutex_lock(&trf->lock); | |
1396 | ||
165063f1 MG |
1397 | switch (trf->state) { |
1398 | case TRF7970A_ST_WAIT_FOR_TX_FIFO: | |
1399 | case TRF7970A_ST_WAIT_FOR_RX_DATA: | |
1400 | case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: | |
9d9304b3 | 1401 | case TRF7970A_ST_WAIT_TO_ISSUE_EOF: |
165063f1 | 1402 | trf7970a_send_err_upstream(trf, -ECANCELED); |
e6403b7c MG |
1403 | /* FALLTHROUGH */ |
1404 | case TRF7970A_ST_IDLE: | |
1405 | case TRF7970A_ST_IDLE_RX_BLOCKED: | |
1406 | pm_runtime_put_sync(trf->dev); | |
165063f1 MG |
1407 | break; |
1408 | default: | |
1409 | break; | |
1410 | } | |
1411 | ||
1412 | mutex_unlock(&trf->lock); | |
1413 | ||
e6403b7c MG |
1414 | pm_runtime_disable(trf->dev); |
1415 | ||
165063f1 MG |
1416 | nfc_digital_unregister_device(trf->ddev); |
1417 | nfc_digital_free_device(trf->ddev); | |
1418 | ||
1419 | regulator_disable(trf->regulator); | |
1420 | ||
1421 | mutex_destroy(&trf->lock); | |
1422 | ||
1423 | return 0; | |
1424 | } | |
1425 | ||
e6403b7c MG |
1426 | #ifdef CONFIG_PM_RUNTIME |
1427 | static int trf7970a_pm_runtime_suspend(struct device *dev) | |
1428 | { | |
1429 | struct spi_device *spi = container_of(dev, struct spi_device, dev); | |
1430 | struct trf7970a *trf = spi_get_drvdata(spi); | |
1431 | int ret; | |
1432 | ||
1433 | dev_dbg(dev, "Runtime suspend\n"); | |
1434 | ||
1435 | if (trf->state != TRF7970A_ST_OFF) { | |
1436 | dev_dbg(dev, "Can't suspend - not in OFF state (%d)\n", | |
1437 | trf->state); | |
1438 | return -EBUSY; | |
1439 | } | |
1440 | ||
1441 | gpio_set_value(trf->en_gpio, 0); | |
1442 | gpio_set_value(trf->en2_gpio, 0); | |
1443 | ||
1444 | ret = regulator_disable(trf->regulator); | |
1445 | if (ret) | |
1446 | dev_err(dev, "%s - Can't disable VIN: %d\n", __func__, ret); | |
1447 | ||
1448 | return ret; | |
1449 | } | |
1450 | ||
1451 | static int trf7970a_pm_runtime_resume(struct device *dev) | |
1452 | { | |
1453 | struct spi_device *spi = container_of(dev, struct spi_device, dev); | |
1454 | struct trf7970a *trf = spi_get_drvdata(spi); | |
1455 | int ret; | |
1456 | ||
1457 | dev_dbg(dev, "Runtime resume\n"); | |
1458 | ||
1459 | ret = regulator_enable(trf->regulator); | |
1460 | if (ret) { | |
1461 | dev_err(dev, "%s - Can't enable VIN: %d\n", __func__, ret); | |
1462 | return ret; | |
1463 | } | |
1464 | ||
1465 | usleep_range(5000, 6000); | |
1466 | ||
95064bd9 MG |
1467 | if (!(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW)) { |
1468 | gpio_set_value(trf->en2_gpio, 1); | |
1469 | usleep_range(1000, 2000); | |
1470 | } | |
1471 | ||
e6403b7c MG |
1472 | gpio_set_value(trf->en_gpio, 1); |
1473 | ||
1474 | usleep_range(20000, 21000); | |
1475 | ||
1476 | ret = trf7970a_init(trf); | |
1477 | if (ret) { | |
1478 | dev_err(dev, "%s - Can't initialize: %d\n", __func__, ret); | |
1479 | return ret; | |
1480 | } | |
1481 | ||
1482 | pm_runtime_mark_last_busy(dev); | |
1483 | ||
1484 | return 0; | |
1485 | } | |
1486 | #endif | |
1487 | ||
1488 | static const struct dev_pm_ops trf7970a_pm_ops = { | |
1489 | SET_RUNTIME_PM_OPS(trf7970a_pm_runtime_suspend, | |
1490 | trf7970a_pm_runtime_resume, NULL) | |
1491 | }; | |
1492 | ||
165063f1 | 1493 | static const struct spi_device_id trf7970a_id_table[] = { |
772079eb | 1494 | { "trf7970a", 0 }, |
165063f1 MG |
1495 | { } |
1496 | }; | |
1497 | MODULE_DEVICE_TABLE(spi, trf7970a_id_table); | |
1498 | ||
1499 | static struct spi_driver trf7970a_spi_driver = { | |
1500 | .probe = trf7970a_probe, | |
1501 | .remove = trf7970a_remove, | |
1502 | .id_table = trf7970a_id_table, | |
1503 | .driver = { | |
1504 | .name = "trf7970a", | |
1505 | .owner = THIS_MODULE, | |
e6403b7c | 1506 | .pm = &trf7970a_pm_ops, |
165063f1 MG |
1507 | }, |
1508 | }; | |
1509 | ||
1510 | module_spi_driver(trf7970a_spi_driver); | |
1511 | ||
1512 | MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>"); | |
1513 | MODULE_LICENSE("GPL v2"); | |
1514 | MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver"); |