Commit | Line | Data |
---|---|---|
46fe7771 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
cab47333 SS |
2 | /* |
3 | * -------------------------------------------------------------------- | |
4 | * Driver for ST NFC Transceiver ST95HF | |
5 | * -------------------------------------------------------------------- | |
6 | * Copyright (C) 2015 STMicroelectronics Pvt. Ltd. All rights reserved. | |
cab47333 SS |
7 | */ |
8 | ||
9 | #include <linux/err.h> | |
10 | #include <linux/gpio.h> | |
11 | #include <linux/init.h> | |
12 | #include <linux/interrupt.h> | |
13 | #include <linux/irq.h> | |
14 | #include <linux/module.h> | |
15 | #include <linux/netdevice.h> | |
16 | #include <linux/nfc.h> | |
17 | #include <linux/of_gpio.h> | |
18 | #include <linux/of.h> | |
19 | #include <linux/of_irq.h> | |
20 | #include <linux/property.h> | |
21 | #include <linux/regulator/consumer.h> | |
22 | #include <linux/wait.h> | |
23 | #include <net/nfc/digital.h> | |
24 | #include <net/nfc/nfc.h> | |
25 | ||
26 | #include "spi.h" | |
27 | ||
28 | /* supported protocols */ | |
29 | #define ST95HF_SUPPORTED_PROT (NFC_PROTO_ISO14443_MASK | \ | |
30 | NFC_PROTO_ISO14443_B_MASK | \ | |
31 | NFC_PROTO_ISO15693_MASK) | |
32 | /* driver capabilities */ | |
33 | #define ST95HF_CAPABILITIES NFC_DIGITAL_DRV_CAPS_IN_CRC | |
34 | ||
35 | /* Command Send Interface */ | |
36 | /* ST95HF_COMMAND_SEND CMD Ids */ | |
37 | #define ECHO_CMD 0x55 | |
38 | #define WRITE_REGISTER_CMD 0x9 | |
39 | #define PROTOCOL_SELECT_CMD 0x2 | |
40 | #define SEND_RECEIVE_CMD 0x4 | |
41 | ||
42 | /* Select protocol codes */ | |
43 | #define ISO15693_PROTOCOL_CODE 0x1 | |
44 | #define ISO14443A_PROTOCOL_CODE 0x2 | |
45 | #define ISO14443B_PROTOCOL_CODE 0x3 | |
46 | ||
47 | /* | |
48 | * head room len is 3 | |
49 | * 1 byte for control byte | |
50 | * 1 byte for cmd | |
51 | * 1 byte for size | |
52 | */ | |
53 | #define ST95HF_HEADROOM_LEN 3 | |
54 | ||
55 | /* | |
56 | * tailroom is 1 for ISO14443A | |
57 | * and 0 for ISO14443B/ISO15693, | |
58 | * hence the max value 1 should be | |
59 | * taken. | |
60 | */ | |
61 | #define ST95HF_TAILROOM_LEN 1 | |
62 | ||
63 | /* Command Response interface */ | |
64 | #define MAX_RESPONSE_BUFFER_SIZE 280 | |
65 | #define ECHORESPONSE 0x55 | |
66 | #define ST95HF_ERR_MASK 0xF | |
67 | #define ST95HF_TIMEOUT_ERROR 0x87 | |
68 | #define ST95HF_NFCA_CRC_ERR_MASK 0x20 | |
69 | #define ST95HF_NFCB_CRC_ERR_MASK 0x01 | |
70 | ||
71 | /* ST95HF transmission flag values */ | |
72 | #define TRFLAG_NFCA_SHORT_FRAME 0x07 | |
73 | #define TRFLAG_NFCA_STD_FRAME 0x08 | |
74 | #define TRFLAG_NFCA_STD_FRAME_CRC 0x28 | |
75 | ||
76 | /* Misc defs */ | |
77 | #define HIGH 1 | |
78 | #define LOW 0 | |
79 | #define ISO14443A_RATS_REQ 0xE0 | |
80 | #define RATS_TB1_PRESENT_MASK 0x20 | |
81 | #define RATS_TA1_PRESENT_MASK 0x10 | |
82 | #define TB1_FWI_MASK 0xF0 | |
83 | #define WTX_REQ_FROM_TAG 0xF2 | |
84 | ||
85 | #define MAX_CMD_LEN 0x7 | |
86 | ||
87 | #define MAX_CMD_PARAMS 4 | |
88 | struct cmd { | |
89 | int cmd_len; | |
90 | unsigned char cmd_id; | |
91 | unsigned char no_cmd_params; | |
92 | unsigned char cmd_params[MAX_CMD_PARAMS]; | |
93 | enum req_type req; | |
94 | }; | |
95 | ||
96 | struct param_list { | |
97 | int param_offset; | |
98 | int new_param_val; | |
99 | }; | |
100 | ||
101 | /* | |
102 | * List of top-level cmds to be used internally by the driver. | |
103 | * All these commands are build on top of ST95HF basic commands | |
104 | * such as SEND_RECEIVE_CMD, PROTOCOL_SELECT_CMD, etc. | |
105 | * These top level cmds are used internally while implementing various ops of | |
106 | * digital layer/driver probe or extending the digital framework layer for | |
107 | * features that are not yet implemented there, for example, WTX cmd handling. | |
108 | */ | |
109 | enum st95hf_cmd_list { | |
110 | CMD_ECHO, | |
111 | CMD_ISO14443A_CONFIG, | |
112 | CMD_ISO14443A_DEMOGAIN, | |
113 | CMD_ISO14443B_DEMOGAIN, | |
114 | CMD_ISO14443A_PROTOCOL_SELECT, | |
115 | CMD_ISO14443B_PROTOCOL_SELECT, | |
116 | CMD_WTX_RESPONSE, | |
117 | CMD_FIELD_OFF, | |
118 | CMD_ISO15693_PROTOCOL_SELECT, | |
119 | }; | |
120 | ||
121 | static const struct cmd cmd_array[] = { | |
122 | [CMD_ECHO] = { | |
123 | .cmd_len = 0x2, | |
124 | .cmd_id = ECHO_CMD, | |
125 | .no_cmd_params = 0, | |
126 | .req = SYNC, | |
127 | }, | |
128 | [CMD_ISO14443A_CONFIG] = { | |
129 | .cmd_len = 0x7, | |
130 | .cmd_id = WRITE_REGISTER_CMD, | |
131 | .no_cmd_params = 0x4, | |
132 | .cmd_params = {0x3A, 0x00, 0x5A, 0x04}, | |
133 | .req = SYNC, | |
134 | }, | |
135 | [CMD_ISO14443A_DEMOGAIN] = { | |
136 | .cmd_len = 0x7, | |
137 | .cmd_id = WRITE_REGISTER_CMD, | |
138 | .no_cmd_params = 0x4, | |
139 | .cmd_params = {0x68, 0x01, 0x01, 0xDF}, | |
140 | .req = SYNC, | |
141 | }, | |
142 | [CMD_ISO14443B_DEMOGAIN] = { | |
143 | .cmd_len = 0x7, | |
144 | .cmd_id = WRITE_REGISTER_CMD, | |
145 | .no_cmd_params = 0x4, | |
146 | .cmd_params = {0x68, 0x01, 0x01, 0x51}, | |
147 | .req = SYNC, | |
148 | }, | |
149 | [CMD_ISO14443A_PROTOCOL_SELECT] = { | |
150 | .cmd_len = 0x7, | |
151 | .cmd_id = PROTOCOL_SELECT_CMD, | |
152 | .no_cmd_params = 0x4, | |
153 | .cmd_params = {ISO14443A_PROTOCOL_CODE, 0x00, 0x01, 0xA0}, | |
154 | .req = SYNC, | |
155 | }, | |
156 | [CMD_ISO14443B_PROTOCOL_SELECT] = { | |
157 | .cmd_len = 0x7, | |
158 | .cmd_id = PROTOCOL_SELECT_CMD, | |
159 | .no_cmd_params = 0x4, | |
160 | .cmd_params = {ISO14443B_PROTOCOL_CODE, 0x01, 0x03, 0xFF}, | |
161 | .req = SYNC, | |
162 | }, | |
163 | [CMD_WTX_RESPONSE] = { | |
164 | .cmd_len = 0x6, | |
165 | .cmd_id = SEND_RECEIVE_CMD, | |
166 | .no_cmd_params = 0x3, | |
167 | .cmd_params = {0xF2, 0x00, TRFLAG_NFCA_STD_FRAME_CRC}, | |
168 | .req = ASYNC, | |
169 | }, | |
170 | [CMD_FIELD_OFF] = { | |
171 | .cmd_len = 0x5, | |
172 | .cmd_id = PROTOCOL_SELECT_CMD, | |
173 | .no_cmd_params = 0x2, | |
174 | .cmd_params = {0x0, 0x0}, | |
175 | .req = SYNC, | |
176 | }, | |
177 | [CMD_ISO15693_PROTOCOL_SELECT] = { | |
178 | .cmd_len = 0x5, | |
179 | .cmd_id = PROTOCOL_SELECT_CMD, | |
180 | .no_cmd_params = 0x2, | |
181 | .cmd_params = {ISO15693_PROTOCOL_CODE, 0x0D}, | |
182 | .req = SYNC, | |
183 | }, | |
184 | }; | |
185 | ||
186 | /* st95_digital_cmd_complete_arg stores client context */ | |
187 | struct st95_digital_cmd_complete_arg { | |
188 | struct sk_buff *skb_resp; | |
189 | nfc_digital_cmd_complete_t complete_cb; | |
190 | void *cb_usrarg; | |
191 | bool rats; | |
192 | }; | |
193 | ||
194 | /* | |
195 | * structure containing ST95HF driver specific data. | |
196 | * @spicontext: structure containing information required | |
197 | * for spi communication between st95hf and host. | |
198 | * @ddev: nfc digital device object. | |
199 | * @nfcdev: nfc device object. | |
200 | * @enable_gpio: gpio used to enable st95hf transceiver. | |
201 | * @complete_cb_arg: structure to store various context information | |
202 | * that is passed from nfc requesting thread to the threaded ISR. | |
203 | * @st95hf_supply: regulator "consumer" for NFC device. | |
204 | * @sendrcv_trflag: last byte of frame send by sendrecv command | |
205 | * of st95hf. This byte contains transmission flag info. | |
206 | * @exchange_lock: semaphore used for signaling the st95hf_remove | |
207 | * function that the last outstanding async nfc request is finished. | |
208 | * @rm_lock: mutex for ensuring safe access of nfc digital object | |
209 | * from threaded ISR. Usage of this mutex avoids any race between | |
210 | * deletion of the object from st95hf_remove() and its access from | |
211 | * the threaded ISR. | |
212 | * @nfcdev_free: flag to have the state of nfc device object. | |
213 | * [alive | died] | |
214 | * @current_protocol: current nfc protocol. | |
215 | * @current_rf_tech: current rf technology. | |
216 | * @fwi: frame waiting index, received in reply of RATS according to | |
217 | * digital protocol. | |
218 | */ | |
219 | struct st95hf_context { | |
220 | struct st95hf_spi_context spicontext; | |
221 | struct nfc_digital_dev *ddev; | |
222 | struct nfc_dev *nfcdev; | |
223 | unsigned int enable_gpio; | |
224 | struct st95_digital_cmd_complete_arg complete_cb_arg; | |
225 | struct regulator *st95hf_supply; | |
226 | unsigned char sendrcv_trflag; | |
227 | struct semaphore exchange_lock; | |
228 | struct mutex rm_lock; | |
229 | bool nfcdev_free; | |
230 | u8 current_protocol; | |
231 | u8 current_rf_tech; | |
232 | int fwi; | |
233 | }; | |
234 | ||
235 | /* | |
236 | * st95hf_send_recv_cmd() is for sending commands to ST95HF | |
237 | * that are described in the cmd_array[]. It can optionally | |
238 | * receive the response if the cmd request is of type | |
239 | * SYNC. For that to happen caller must pass true to recv_res. | |
240 | * For ASYNC request, recv_res is ignored and the | |
241 | * function will never try to receive the response on behalf | |
242 | * of the caller. | |
243 | */ | |
244 | static int st95hf_send_recv_cmd(struct st95hf_context *st95context, | |
245 | enum st95hf_cmd_list cmd, | |
246 | int no_modif, | |
247 | struct param_list *list_array, | |
248 | bool recv_res) | |
249 | { | |
250 | unsigned char spi_cmd_buffer[MAX_CMD_LEN]; | |
251 | int i, ret; | |
252 | struct device *dev = &st95context->spicontext.spidev->dev; | |
253 | ||
254 | if (cmd_array[cmd].cmd_len > MAX_CMD_LEN) | |
255 | return -EINVAL; | |
256 | if (cmd_array[cmd].no_cmd_params < no_modif) | |
257 | return -EINVAL; | |
258 | if (no_modif && !list_array) | |
259 | return -EINVAL; | |
260 | ||
261 | spi_cmd_buffer[0] = ST95HF_COMMAND_SEND; | |
262 | spi_cmd_buffer[1] = cmd_array[cmd].cmd_id; | |
263 | spi_cmd_buffer[2] = cmd_array[cmd].no_cmd_params; | |
264 | ||
265 | memcpy(&spi_cmd_buffer[3], cmd_array[cmd].cmd_params, | |
266 | spi_cmd_buffer[2]); | |
267 | ||
268 | for (i = 0; i < no_modif; i++) { | |
269 | if (list_array[i].param_offset >= cmd_array[cmd].no_cmd_params) | |
270 | return -EINVAL; | |
271 | spi_cmd_buffer[3 + list_array[i].param_offset] = | |
272 | list_array[i].new_param_val; | |
273 | } | |
274 | ||
275 | ret = st95hf_spi_send(&st95context->spicontext, | |
276 | spi_cmd_buffer, | |
277 | cmd_array[cmd].cmd_len, | |
278 | cmd_array[cmd].req); | |
279 | if (ret) { | |
280 | dev_err(dev, "st95hf_spi_send failed with error %d\n", ret); | |
281 | return ret; | |
282 | } | |
283 | ||
284 | if (cmd_array[cmd].req == SYNC && recv_res) { | |
285 | unsigned char st95hf_response_arr[2]; | |
286 | ||
287 | ret = st95hf_spi_recv_response(&st95context->spicontext, | |
288 | st95hf_response_arr); | |
289 | if (ret < 0) { | |
290 | dev_err(dev, "spi error from st95hf_spi_recv_response(), err = 0x%x\n", | |
291 | ret); | |
292 | return ret; | |
293 | } | |
294 | ||
295 | if (st95hf_response_arr[0]) { | |
296 | dev_err(dev, "st95hf error from st95hf_spi_recv_response(), err = 0x%x\n", | |
297 | st95hf_response_arr[0]); | |
298 | return -EIO; | |
299 | } | |
300 | } | |
301 | ||
302 | return 0; | |
303 | } | |
304 | ||
305 | static int st95hf_echo_command(struct st95hf_context *st95context) | |
306 | { | |
307 | int result = 0; | |
308 | unsigned char echo_response; | |
309 | ||
310 | result = st95hf_send_recv_cmd(st95context, CMD_ECHO, 0, NULL, false); | |
311 | if (result) | |
312 | return result; | |
313 | ||
314 | /* If control reached here, response can be taken */ | |
315 | result = st95hf_spi_recv_echo_res(&st95context->spicontext, | |
316 | &echo_response); | |
317 | if (result) { | |
318 | dev_err(&st95context->spicontext.spidev->dev, | |
90aa11f1 | 319 | "err: echo response receive error = 0x%x\n", result); |
cab47333 SS |
320 | return result; |
321 | } | |
322 | ||
323 | if (echo_response == ECHORESPONSE) | |
324 | return 0; | |
325 | ||
326 | dev_err(&st95context->spicontext.spidev->dev, "err: echo res is 0x%x\n", | |
327 | echo_response); | |
328 | ||
329 | return -EIO; | |
330 | } | |
331 | ||
332 | static int secondary_configuration_type4a(struct st95hf_context *stcontext) | |
333 | { | |
334 | int result = 0; | |
335 | struct device *dev = &stcontext->nfcdev->dev; | |
336 | ||
337 | /* 14443A config setting after select protocol */ | |
338 | result = st95hf_send_recv_cmd(stcontext, | |
339 | CMD_ISO14443A_CONFIG, | |
340 | 0, | |
341 | NULL, | |
342 | true); | |
343 | if (result) { | |
344 | dev_err(dev, "type a config cmd, err = 0x%x\n", result); | |
345 | return result; | |
346 | } | |
347 | ||
348 | /* 14443A demo gain setting */ | |
349 | result = st95hf_send_recv_cmd(stcontext, | |
350 | CMD_ISO14443A_DEMOGAIN, | |
351 | 0, | |
352 | NULL, | |
353 | true); | |
354 | if (result) | |
355 | dev_err(dev, "type a demogain cmd, err = 0x%x\n", result); | |
356 | ||
357 | return result; | |
358 | } | |
359 | ||
360 | static int secondary_configuration_type4b(struct st95hf_context *stcontext) | |
361 | { | |
362 | int result = 0; | |
363 | struct device *dev = &stcontext->nfcdev->dev; | |
364 | ||
365 | result = st95hf_send_recv_cmd(stcontext, | |
366 | CMD_ISO14443B_DEMOGAIN, | |
367 | 0, | |
368 | NULL, | |
369 | true); | |
370 | if (result) | |
371 | dev_err(dev, "type b demogain cmd, err = 0x%x\n", result); | |
372 | ||
373 | return result; | |
374 | } | |
375 | ||
376 | static int st95hf_select_protocol(struct st95hf_context *stcontext, int type) | |
377 | { | |
378 | int result = 0; | |
379 | struct device *dev; | |
380 | ||
381 | dev = &stcontext->nfcdev->dev; | |
382 | ||
383 | switch (type) { | |
384 | case NFC_DIGITAL_RF_TECH_106A: | |
385 | stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_106A; | |
386 | result = st95hf_send_recv_cmd(stcontext, | |
387 | CMD_ISO14443A_PROTOCOL_SELECT, | |
388 | 0, | |
389 | NULL, | |
390 | true); | |
391 | if (result) { | |
392 | dev_err(dev, "protocol sel, err = 0x%x\n", | |
393 | result); | |
394 | return result; | |
395 | } | |
396 | ||
397 | /* secondary config. for 14443Type 4A after protocol select */ | |
398 | result = secondary_configuration_type4a(stcontext); | |
399 | if (result) { | |
400 | dev_err(dev, "type a secondary config, err = 0x%x\n", | |
401 | result); | |
402 | return result; | |
403 | } | |
404 | break; | |
405 | case NFC_DIGITAL_RF_TECH_106B: | |
406 | stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_106B; | |
407 | result = st95hf_send_recv_cmd(stcontext, | |
408 | CMD_ISO14443B_PROTOCOL_SELECT, | |
409 | 0, | |
410 | NULL, | |
411 | true); | |
412 | if (result) { | |
413 | dev_err(dev, "protocol sel send, err = 0x%x\n", | |
414 | result); | |
415 | return result; | |
416 | } | |
417 | ||
418 | /* | |
419 | * delay of 5-6 ms is required after select protocol | |
420 | * command in case of ISO14443 Type B | |
421 | */ | |
422 | usleep_range(50000, 60000); | |
423 | ||
424 | /* secondary config. for 14443Type 4B after protocol select */ | |
425 | result = secondary_configuration_type4b(stcontext); | |
426 | if (result) { | |
427 | dev_err(dev, "type b secondary config, err = 0x%x\n", | |
428 | result); | |
429 | return result; | |
430 | } | |
431 | break; | |
432 | case NFC_DIGITAL_RF_TECH_ISO15693: | |
433 | stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_ISO15693; | |
434 | result = st95hf_send_recv_cmd(stcontext, | |
435 | CMD_ISO15693_PROTOCOL_SELECT, | |
436 | 0, | |
437 | NULL, | |
438 | true); | |
439 | if (result) { | |
440 | dev_err(dev, "protocol sel send, err = 0x%x\n", | |
441 | result); | |
442 | return result; | |
443 | } | |
444 | break; | |
445 | default: | |
446 | return -EINVAL; | |
447 | } | |
448 | ||
449 | return 0; | |
450 | } | |
451 | ||
452 | static void st95hf_send_st95enable_negativepulse(struct st95hf_context *st95con) | |
453 | { | |
454 | /* First make irq_in pin high */ | |
455 | gpio_set_value(st95con->enable_gpio, HIGH); | |
456 | ||
457 | /* wait for 1 milisecond */ | |
458 | usleep_range(1000, 2000); | |
459 | ||
460 | /* Make irq_in pin low */ | |
461 | gpio_set_value(st95con->enable_gpio, LOW); | |
462 | ||
463 | /* wait for minimum interrupt pulse to make st95 active */ | |
464 | usleep_range(1000, 2000); | |
465 | ||
466 | /* At end make it high */ | |
467 | gpio_set_value(st95con->enable_gpio, HIGH); | |
468 | } | |
469 | ||
470 | /* | |
471 | * Send a reset sequence over SPI bus (Reset command + wait 3ms + | |
472 | * negative pulse on st95hf enable gpio | |
473 | */ | |
474 | static int st95hf_send_spi_reset_sequence(struct st95hf_context *st95context) | |
475 | { | |
476 | int result = 0; | |
477 | unsigned char reset_cmd = ST95HF_COMMAND_RESET; | |
478 | ||
479 | result = st95hf_spi_send(&st95context->spicontext, | |
480 | &reset_cmd, | |
481 | ST95HF_RESET_CMD_LEN, | |
482 | ASYNC); | |
483 | if (result) { | |
484 | dev_err(&st95context->spicontext.spidev->dev, | |
485 | "spi reset sequence cmd error = %d", result); | |
486 | return result; | |
487 | } | |
488 | ||
489 | /* wait for 3 milisecond to complete the controller reset process */ | |
490 | usleep_range(3000, 4000); | |
491 | ||
492 | /* send negative pulse to make st95hf active */ | |
493 | st95hf_send_st95enable_negativepulse(st95context); | |
494 | ||
495 | /* wait for 10 milisecond : HFO setup time */ | |
496 | usleep_range(10000, 20000); | |
497 | ||
498 | return result; | |
499 | } | |
500 | ||
501 | static int st95hf_por_sequence(struct st95hf_context *st95context) | |
502 | { | |
503 | int nth_attempt = 1; | |
504 | int result; | |
505 | ||
506 | st95hf_send_st95enable_negativepulse(st95context); | |
507 | ||
508 | usleep_range(5000, 6000); | |
509 | do { | |
510 | /* send an ECHO command and checks ST95HF response */ | |
511 | result = st95hf_echo_command(st95context); | |
512 | ||
513 | dev_dbg(&st95context->spicontext.spidev->dev, | |
514 | "response from echo function = 0x%x, attempt = %d\n", | |
515 | result, nth_attempt); | |
516 | ||
517 | if (!result) | |
518 | return 0; | |
519 | ||
520 | /* send an pulse on IRQ in case of the chip is on sleep state */ | |
521 | if (nth_attempt == 2) | |
522 | st95hf_send_st95enable_negativepulse(st95context); | |
523 | else | |
524 | st95hf_send_spi_reset_sequence(st95context); | |
525 | ||
526 | /* delay of 50 milisecond */ | |
527 | usleep_range(50000, 51000); | |
528 | } while (nth_attempt++ < 3); | |
529 | ||
530 | return -ETIMEDOUT; | |
531 | } | |
532 | ||
533 | static int iso14443_config_fdt(struct st95hf_context *st95context, int wtxm) | |
534 | { | |
535 | int result = 0; | |
536 | struct device *dev = &st95context->spicontext.spidev->dev; | |
537 | struct nfc_digital_dev *nfcddev = st95context->ddev; | |
538 | unsigned char pp_typeb; | |
539 | struct param_list new_params[2]; | |
540 | ||
541 | pp_typeb = cmd_array[CMD_ISO14443B_PROTOCOL_SELECT].cmd_params[2]; | |
542 | ||
543 | if (nfcddev->curr_protocol == NFC_PROTO_ISO14443 && | |
544 | st95context->fwi < 4) | |
545 | st95context->fwi = 4; | |
546 | ||
547 | new_params[0].param_offset = 2; | |
548 | if (nfcddev->curr_protocol == NFC_PROTO_ISO14443) | |
549 | new_params[0].new_param_val = st95context->fwi; | |
550 | else if (nfcddev->curr_protocol == NFC_PROTO_ISO14443_B) | |
551 | new_params[0].new_param_val = pp_typeb; | |
552 | ||
553 | new_params[1].param_offset = 3; | |
554 | new_params[1].new_param_val = wtxm; | |
555 | ||
556 | switch (nfcddev->curr_protocol) { | |
557 | case NFC_PROTO_ISO14443: | |
558 | result = st95hf_send_recv_cmd(st95context, | |
559 | CMD_ISO14443A_PROTOCOL_SELECT, | |
560 | 2, | |
561 | new_params, | |
562 | true); | |
563 | if (result) { | |
564 | dev_err(dev, "WTX type a sel proto, err = 0x%x\n", | |
565 | result); | |
566 | return result; | |
567 | } | |
568 | ||
569 | /* secondary config. for 14443Type 4A after protocol select */ | |
570 | result = secondary_configuration_type4a(st95context); | |
571 | if (result) { | |
572 | dev_err(dev, "WTX type a second. config, err = 0x%x\n", | |
573 | result); | |
574 | return result; | |
575 | } | |
576 | break; | |
577 | case NFC_PROTO_ISO14443_B: | |
578 | result = st95hf_send_recv_cmd(st95context, | |
579 | CMD_ISO14443B_PROTOCOL_SELECT, | |
580 | 2, | |
581 | new_params, | |
582 | true); | |
583 | if (result) { | |
584 | dev_err(dev, "WTX type b sel proto, err = 0x%x\n", | |
585 | result); | |
586 | return result; | |
587 | } | |
588 | ||
589 | /* secondary config. for 14443Type 4B after protocol select */ | |
590 | result = secondary_configuration_type4b(st95context); | |
591 | if (result) { | |
592 | dev_err(dev, "WTX type b second. config, err = 0x%x\n", | |
593 | result); | |
594 | return result; | |
595 | } | |
596 | break; | |
597 | default: | |
598 | return -EINVAL; | |
599 | } | |
600 | ||
601 | return 0; | |
602 | } | |
603 | ||
604 | static int st95hf_handle_wtx(struct st95hf_context *stcontext, | |
605 | bool new_wtx, | |
606 | int wtx_val) | |
607 | { | |
608 | int result = 0; | |
609 | unsigned char val_mm = 0; | |
610 | struct param_list new_params[1]; | |
611 | struct nfc_digital_dev *nfcddev = stcontext->ddev; | |
612 | struct device *dev = &stcontext->nfcdev->dev; | |
613 | ||
614 | if (new_wtx) { | |
615 | result = iso14443_config_fdt(stcontext, wtx_val & 0x3f); | |
616 | if (result) { | |
617 | dev_err(dev, "Config. setting error on WTX req, err = 0x%x\n", | |
618 | result); | |
619 | return result; | |
620 | } | |
621 | ||
622 | /* Send response of wtx with ASYNC as no response expected */ | |
623 | new_params[0].param_offset = 1; | |
624 | new_params[0].new_param_val = wtx_val; | |
625 | ||
626 | result = st95hf_send_recv_cmd(stcontext, | |
627 | CMD_WTX_RESPONSE, | |
628 | 1, | |
629 | new_params, | |
630 | false); | |
631 | if (result) | |
632 | dev_err(dev, "WTX response send, err = 0x%x\n", result); | |
633 | return result; | |
634 | } | |
635 | ||
636 | /* if no new wtx, cofigure with default values */ | |
637 | if (nfcddev->curr_protocol == NFC_PROTO_ISO14443) | |
638 | val_mm = cmd_array[CMD_ISO14443A_PROTOCOL_SELECT].cmd_params[3]; | |
639 | else if (nfcddev->curr_protocol == NFC_PROTO_ISO14443_B) | |
640 | val_mm = cmd_array[CMD_ISO14443B_PROTOCOL_SELECT].cmd_params[3]; | |
641 | ||
642 | result = iso14443_config_fdt(stcontext, val_mm); | |
643 | if (result) | |
644 | dev_err(dev, "Default config. setting error after WTX processing, err = 0x%x\n", | |
645 | result); | |
646 | ||
647 | return result; | |
648 | } | |
649 | ||
650 | static int st95hf_error_handling(struct st95hf_context *stcontext, | |
651 | struct sk_buff *skb_resp, | |
652 | int res_len) | |
653 | { | |
654 | int result = 0; | |
655 | unsigned char error_byte; | |
656 | struct device *dev = &stcontext->nfcdev->dev; | |
657 | ||
658 | /* First check ST95HF specific error */ | |
659 | if (skb_resp->data[0] & ST95HF_ERR_MASK) { | |
660 | if (skb_resp->data[0] == ST95HF_TIMEOUT_ERROR) | |
661 | result = -ETIMEDOUT; | |
662 | else | |
663 | result = -EIO; | |
664 | return result; | |
665 | } | |
666 | ||
667 | /* Check for CRC err only if CRC is present in the tag response */ | |
668 | switch (stcontext->current_rf_tech) { | |
669 | case NFC_DIGITAL_RF_TECH_106A: | |
670 | if (stcontext->sendrcv_trflag == TRFLAG_NFCA_STD_FRAME_CRC) { | |
671 | error_byte = skb_resp->data[res_len - 3]; | |
672 | if (error_byte & ST95HF_NFCA_CRC_ERR_MASK) { | |
673 | /* CRC error occurred */ | |
674 | dev_err(dev, "CRC error, byte received = 0x%x\n", | |
675 | error_byte); | |
676 | result = -EIO; | |
677 | } | |
678 | } | |
679 | break; | |
680 | case NFC_DIGITAL_RF_TECH_106B: | |
681 | case NFC_DIGITAL_RF_TECH_ISO15693: | |
682 | error_byte = skb_resp->data[res_len - 1]; | |
683 | if (error_byte & ST95HF_NFCB_CRC_ERR_MASK) { | |
684 | /* CRC error occurred */ | |
685 | dev_err(dev, "CRC error, byte received = 0x%x\n", | |
686 | error_byte); | |
687 | result = -EIO; | |
688 | } | |
689 | break; | |
690 | } | |
691 | ||
692 | return result; | |
693 | } | |
694 | ||
695 | static int st95hf_response_handler(struct st95hf_context *stcontext, | |
696 | struct sk_buff *skb_resp, | |
697 | int res_len) | |
698 | { | |
699 | int result = 0; | |
700 | int skb_len; | |
701 | unsigned char val_mm; | |
702 | struct nfc_digital_dev *nfcddev = stcontext->ddev; | |
703 | struct device *dev = &stcontext->nfcdev->dev; | |
704 | struct st95_digital_cmd_complete_arg *cb_arg; | |
705 | ||
706 | cb_arg = &stcontext->complete_cb_arg; | |
707 | ||
708 | /* Process the response */ | |
709 | skb_put(skb_resp, res_len); | |
710 | ||
711 | /* Remove st95 header */ | |
712 | skb_pull(skb_resp, 2); | |
713 | ||
714 | skb_len = skb_resp->len; | |
715 | ||
716 | /* check if it is case of RATS request reply & FWI is present */ | |
717 | if (nfcddev->curr_protocol == NFC_PROTO_ISO14443 && cb_arg->rats && | |
718 | (skb_resp->data[1] & RATS_TB1_PRESENT_MASK)) { | |
719 | if (skb_resp->data[1] & RATS_TA1_PRESENT_MASK) | |
720 | stcontext->fwi = | |
721 | (skb_resp->data[3] & TB1_FWI_MASK) >> 4; | |
722 | else | |
723 | stcontext->fwi = | |
724 | (skb_resp->data[2] & TB1_FWI_MASK) >> 4; | |
725 | ||
726 | val_mm = cmd_array[CMD_ISO14443A_PROTOCOL_SELECT].cmd_params[3]; | |
727 | ||
728 | result = iso14443_config_fdt(stcontext, val_mm); | |
729 | if (result) { | |
730 | dev_err(dev, "error in config_fdt to handle fwi of ATS, error=%d\n", | |
731 | result); | |
732 | return result; | |
733 | } | |
734 | } | |
735 | cb_arg->rats = false; | |
736 | ||
737 | /* Remove CRC bytes only if received frames data has an eod (CRC) */ | |
738 | switch (stcontext->current_rf_tech) { | |
739 | case NFC_DIGITAL_RF_TECH_106A: | |
740 | if (stcontext->sendrcv_trflag == TRFLAG_NFCA_STD_FRAME_CRC) | |
741 | skb_trim(skb_resp, (skb_len - 5)); | |
742 | else | |
743 | skb_trim(skb_resp, (skb_len - 3)); | |
744 | break; | |
745 | case NFC_DIGITAL_RF_TECH_106B: | |
746 | case NFC_DIGITAL_RF_TECH_ISO15693: | |
747 | skb_trim(skb_resp, (skb_len - 3)); | |
748 | break; | |
749 | } | |
750 | ||
751 | return result; | |
752 | } | |
753 | ||
6b52d994 | 754 | static irqreturn_t st95hf_irq_handler(int irq, void *st95hfcontext) |
cab47333 SS |
755 | { |
756 | struct st95hf_context *stcontext = | |
757 | (struct st95hf_context *)st95hfcontext; | |
758 | ||
759 | if (stcontext->spicontext.req_issync) { | |
760 | complete(&stcontext->spicontext.done); | |
761 | stcontext->spicontext.req_issync = false; | |
762 | return IRQ_HANDLED; | |
763 | } | |
764 | ||
765 | return IRQ_WAKE_THREAD; | |
766 | } | |
767 | ||
6b52d994 | 768 | static irqreturn_t st95hf_irq_thread_handler(int irq, void *st95hfcontext) |
cab47333 SS |
769 | { |
770 | int result = 0; | |
771 | int res_len; | |
772 | static bool wtx; | |
cab47333 | 773 | struct device *spidevice; |
cab47333 SS |
774 | struct sk_buff *skb_resp; |
775 | struct st95hf_context *stcontext = | |
776 | (struct st95hf_context *)st95hfcontext; | |
777 | struct st95_digital_cmd_complete_arg *cb_arg; | |
778 | ||
779 | spidevice = &stcontext->spicontext.spidev->dev; | |
780 | ||
781 | /* | |
782 | * check semaphore, if not down() already, then we don't | |
783 | * know in which context the ISR is called and surely it | |
784 | * will be a bug. Note that down() of the semaphore is done | |
785 | * in the corresponding st95hf_in_send_cmd() and then | |
786 | * only this ISR should be called. ISR will up() the | |
787 | * semaphore before leaving. Hence when the ISR is called | |
788 | * the correct behaviour is down_trylock() should always | |
789 | * return 1 (indicating semaphore cant be taken and hence no | |
790 | * change in semaphore count). | |
791 | * If not, then we up() the semaphore and crash on | |
792 | * a BUG() ! | |
793 | */ | |
794 | if (!down_trylock(&stcontext->exchange_lock)) { | |
795 | up(&stcontext->exchange_lock); | |
796 | WARN(1, "unknown context in ST95HF ISR"); | |
797 | return IRQ_NONE; | |
798 | } | |
799 | ||
800 | cb_arg = &stcontext->complete_cb_arg; | |
801 | skb_resp = cb_arg->skb_resp; | |
802 | ||
803 | mutex_lock(&stcontext->rm_lock); | |
804 | res_len = st95hf_spi_recv_response(&stcontext->spicontext, | |
805 | skb_resp->data); | |
806 | if (res_len < 0) { | |
807 | dev_err(spidevice, "TISR spi response err = 0x%x\n", res_len); | |
808 | result = res_len; | |
809 | goto end; | |
810 | } | |
811 | ||
812 | /* if stcontext->nfcdev_free is true, it means remove already ran */ | |
813 | if (stcontext->nfcdev_free) { | |
814 | result = -ENODEV; | |
815 | goto end; | |
816 | } | |
817 | ||
cab47333 SS |
818 | if (skb_resp->data[2] == WTX_REQ_FROM_TAG) { |
819 | /* Request for new FWT from tag */ | |
820 | result = st95hf_handle_wtx(stcontext, true, skb_resp->data[3]); | |
821 | if (result) | |
822 | goto end; | |
823 | ||
824 | wtx = true; | |
825 | mutex_unlock(&stcontext->rm_lock); | |
826 | return IRQ_HANDLED; | |
827 | } | |
828 | ||
829 | result = st95hf_error_handling(stcontext, skb_resp, res_len); | |
830 | if (result) | |
831 | goto end; | |
832 | ||
833 | result = st95hf_response_handler(stcontext, skb_resp, res_len); | |
834 | if (result) | |
835 | goto end; | |
836 | ||
837 | /* | |
838 | * If select protocol is done on wtx req. do select protocol | |
839 | * again with default values | |
840 | */ | |
841 | if (wtx) { | |
842 | wtx = false; | |
843 | result = st95hf_handle_wtx(stcontext, false, 0); | |
844 | if (result) | |
845 | goto end; | |
846 | } | |
847 | ||
848 | /* call digital layer callback */ | |
849 | cb_arg->complete_cb(stcontext->ddev, cb_arg->cb_usrarg, skb_resp); | |
850 | ||
851 | /* up the semaphore before returning */ | |
852 | up(&stcontext->exchange_lock); | |
853 | mutex_unlock(&stcontext->rm_lock); | |
854 | ||
855 | return IRQ_HANDLED; | |
856 | ||
857 | end: | |
858 | kfree_skb(skb_resp); | |
859 | wtx = false; | |
860 | cb_arg->rats = false; | |
861 | skb_resp = ERR_PTR(result); | |
862 | /* call of callback with error */ | |
863 | cb_arg->complete_cb(stcontext->ddev, cb_arg->cb_usrarg, skb_resp); | |
864 | /* up the semaphore before returning */ | |
865 | up(&stcontext->exchange_lock); | |
866 | mutex_unlock(&stcontext->rm_lock); | |
867 | return IRQ_HANDLED; | |
868 | } | |
869 | ||
870 | /* NFC ops functions definition */ | |
871 | static int st95hf_in_configure_hw(struct nfc_digital_dev *ddev, | |
872 | int type, | |
873 | int param) | |
874 | { | |
875 | struct st95hf_context *stcontext = nfc_digital_get_drvdata(ddev); | |
876 | ||
877 | if (type == NFC_DIGITAL_CONFIG_RF_TECH) | |
878 | return st95hf_select_protocol(stcontext, param); | |
879 | ||
880 | if (type == NFC_DIGITAL_CONFIG_FRAMING) { | |
881 | switch (param) { | |
882 | case NFC_DIGITAL_FRAMING_NFCA_SHORT: | |
883 | stcontext->sendrcv_trflag = TRFLAG_NFCA_SHORT_FRAME; | |
884 | break; | |
885 | case NFC_DIGITAL_FRAMING_NFCA_STANDARD: | |
886 | stcontext->sendrcv_trflag = TRFLAG_NFCA_STD_FRAME; | |
887 | break; | |
888 | case NFC_DIGITAL_FRAMING_NFCA_T4T: | |
889 | case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP: | |
890 | case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A: | |
891 | stcontext->sendrcv_trflag = TRFLAG_NFCA_STD_FRAME_CRC; | |
892 | break; | |
893 | case NFC_DIGITAL_FRAMING_NFCB: | |
894 | case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY: | |
895 | case NFC_DIGITAL_FRAMING_ISO15693_T5T: | |
896 | break; | |
897 | } | |
898 | } | |
899 | ||
900 | return 0; | |
901 | } | |
902 | ||
903 | static int rf_off(struct st95hf_context *stcontext) | |
904 | { | |
905 | int rc; | |
906 | struct device *dev; | |
907 | ||
908 | dev = &stcontext->nfcdev->dev; | |
909 | ||
910 | rc = st95hf_send_recv_cmd(stcontext, CMD_FIELD_OFF, 0, NULL, true); | |
911 | if (rc) | |
912 | dev_err(dev, "protocol sel send field off, err = 0x%x\n", rc); | |
913 | ||
914 | return rc; | |
915 | } | |
916 | ||
917 | static int st95hf_in_send_cmd(struct nfc_digital_dev *ddev, | |
918 | struct sk_buff *skb, | |
919 | u16 timeout, | |
920 | nfc_digital_cmd_complete_t cb, | |
921 | void *arg) | |
922 | { | |
923 | struct st95hf_context *stcontext = nfc_digital_get_drvdata(ddev); | |
924 | int rc; | |
925 | struct sk_buff *skb_resp; | |
926 | int len_data_to_tag = 0; | |
927 | ||
928 | skb_resp = nfc_alloc_recv_skb(MAX_RESPONSE_BUFFER_SIZE, GFP_KERNEL); | |
929 | if (!skb_resp) { | |
930 | rc = -ENOMEM; | |
931 | goto error; | |
932 | } | |
933 | ||
934 | switch (stcontext->current_rf_tech) { | |
935 | case NFC_DIGITAL_RF_TECH_106A: | |
936 | len_data_to_tag = skb->len + 1; | |
634fef61 | 937 | skb_put_u8(skb, stcontext->sendrcv_trflag); |
cab47333 SS |
938 | break; |
939 | case NFC_DIGITAL_RF_TECH_106B: | |
940 | case NFC_DIGITAL_RF_TECH_ISO15693: | |
941 | len_data_to_tag = skb->len; | |
942 | break; | |
943 | default: | |
944 | rc = -EINVAL; | |
945 | goto free_skb_resp; | |
946 | } | |
947 | ||
948 | skb_push(skb, 3); | |
949 | skb->data[0] = ST95HF_COMMAND_SEND; | |
950 | skb->data[1] = SEND_RECEIVE_CMD; | |
951 | skb->data[2] = len_data_to_tag; | |
952 | ||
953 | stcontext->complete_cb_arg.skb_resp = skb_resp; | |
954 | stcontext->complete_cb_arg.cb_usrarg = arg; | |
955 | stcontext->complete_cb_arg.complete_cb = cb; | |
956 | ||
957 | if ((skb->data[3] == ISO14443A_RATS_REQ) && | |
958 | ddev->curr_protocol == NFC_PROTO_ISO14443) | |
959 | stcontext->complete_cb_arg.rats = true; | |
960 | ||
961 | /* | |
962 | * down the semaphore to indicate to remove func that an | |
963 | * ISR is pending, note that it will not block here in any case. | |
964 | * If found blocked, it is a BUG! | |
965 | */ | |
966 | rc = down_killable(&stcontext->exchange_lock); | |
967 | if (rc) { | |
968 | WARN(1, "Semaphore is not found up in st95hf_in_send_cmd\n"); | |
969 | return rc; | |
970 | } | |
971 | ||
972 | rc = st95hf_spi_send(&stcontext->spicontext, skb->data, | |
973 | skb->len, | |
974 | ASYNC); | |
975 | if (rc) { | |
976 | dev_err(&stcontext->nfcdev->dev, | |
977 | "Error %d trying to perform data_exchange", rc); | |
978 | /* up the semaphore since ISR will never come in this case */ | |
979 | up(&stcontext->exchange_lock); | |
980 | goto free_skb_resp; | |
981 | } | |
982 | ||
983 | kfree_skb(skb); | |
984 | ||
985 | return rc; | |
986 | ||
987 | free_skb_resp: | |
988 | kfree_skb(skb_resp); | |
989 | error: | |
990 | return rc; | |
991 | } | |
992 | ||
993 | /* p2p will be supported in a later release ! */ | |
994 | static int st95hf_tg_configure_hw(struct nfc_digital_dev *ddev, | |
995 | int type, | |
996 | int param) | |
997 | { | |
998 | return 0; | |
999 | } | |
1000 | ||
1001 | static int st95hf_tg_send_cmd(struct nfc_digital_dev *ddev, | |
1002 | struct sk_buff *skb, | |
1003 | u16 timeout, | |
1004 | nfc_digital_cmd_complete_t cb, | |
1005 | void *arg) | |
1006 | { | |
1007 | return 0; | |
1008 | } | |
1009 | ||
1010 | static int st95hf_tg_listen(struct nfc_digital_dev *ddev, | |
1011 | u16 timeout, | |
1012 | nfc_digital_cmd_complete_t cb, | |
1013 | void *arg) | |
1014 | { | |
1015 | return 0; | |
1016 | } | |
1017 | ||
1018 | static int st95hf_tg_get_rf_tech(struct nfc_digital_dev *ddev, u8 *rf_tech) | |
1019 | { | |
1020 | return 0; | |
1021 | } | |
1022 | ||
1023 | static int st95hf_switch_rf(struct nfc_digital_dev *ddev, bool on) | |
1024 | { | |
1025 | u8 rf_tech; | |
1026 | struct st95hf_context *stcontext = nfc_digital_get_drvdata(ddev); | |
1027 | ||
1028 | rf_tech = ddev->curr_rf_tech; | |
1029 | ||
1030 | if (on) | |
1031 | /* switch on RF field */ | |
1032 | return st95hf_select_protocol(stcontext, rf_tech); | |
1033 | ||
1034 | /* switch OFF RF field */ | |
1035 | return rf_off(stcontext); | |
1036 | } | |
1037 | ||
1038 | /* TODO st95hf_abort_cmd */ | |
1039 | static void st95hf_abort_cmd(struct nfc_digital_dev *ddev) | |
1040 | { | |
1041 | } | |
1042 | ||
1043 | static struct nfc_digital_ops st95hf_nfc_digital_ops = { | |
1044 | .in_configure_hw = st95hf_in_configure_hw, | |
1045 | .in_send_cmd = st95hf_in_send_cmd, | |
1046 | ||
1047 | .tg_listen = st95hf_tg_listen, | |
1048 | .tg_configure_hw = st95hf_tg_configure_hw, | |
1049 | .tg_send_cmd = st95hf_tg_send_cmd, | |
1050 | .tg_get_rf_tech = st95hf_tg_get_rf_tech, | |
1051 | ||
1052 | .switch_rf = st95hf_switch_rf, | |
1053 | .abort_cmd = st95hf_abort_cmd, | |
1054 | }; | |
1055 | ||
1056 | static const struct spi_device_id st95hf_id[] = { | |
1057 | { "st95hf", 0 }, | |
1058 | {} | |
1059 | }; | |
1060 | MODULE_DEVICE_TABLE(spi, st95hf_id); | |
1061 | ||
d0483053 DG |
1062 | static const struct of_device_id st95hf_spi_of_match[] = { |
1063 | { .compatible = "st,st95hf" }, | |
1064 | { }, | |
1065 | }; | |
1066 | MODULE_DEVICE_TABLE(of, st95hf_spi_of_match); | |
1067 | ||
cab47333 SS |
1068 | static int st95hf_probe(struct spi_device *nfc_spi_dev) |
1069 | { | |
1070 | int ret; | |
1071 | ||
1072 | struct st95hf_context *st95context; | |
1073 | struct st95hf_spi_context *spicontext; | |
1074 | ||
1075 | nfc_info(&nfc_spi_dev->dev, "ST95HF driver probe called.\n"); | |
1076 | ||
1077 | st95context = devm_kzalloc(&nfc_spi_dev->dev, | |
1078 | sizeof(struct st95hf_context), | |
1079 | GFP_KERNEL); | |
1080 | if (!st95context) | |
1081 | return -ENOMEM; | |
1082 | ||
1083 | spicontext = &st95context->spicontext; | |
1084 | ||
1085 | spicontext->spidev = nfc_spi_dev; | |
1086 | ||
1087 | st95context->fwi = | |
1088 | cmd_array[CMD_ISO14443A_PROTOCOL_SELECT].cmd_params[2]; | |
1089 | ||
1090 | if (device_property_present(&nfc_spi_dev->dev, "st95hfvin")) { | |
1091 | st95context->st95hf_supply = | |
1092 | devm_regulator_get(&nfc_spi_dev->dev, | |
1093 | "st95hfvin"); | |
1094 | if (IS_ERR(st95context->st95hf_supply)) { | |
1095 | dev_err(&nfc_spi_dev->dev, "failed to acquire regulator\n"); | |
1096 | return PTR_ERR(st95context->st95hf_supply); | |
1097 | } | |
1098 | ||
1099 | ret = regulator_enable(st95context->st95hf_supply); | |
1100 | if (ret) { | |
1101 | dev_err(&nfc_spi_dev->dev, "failed to enable regulator\n"); | |
1102 | return ret; | |
1103 | } | |
1104 | } | |
1105 | ||
1106 | init_completion(&spicontext->done); | |
1107 | mutex_init(&spicontext->spi_lock); | |
1108 | ||
1109 | /* | |
1110 | * Store spicontext in spi device object for using it in | |
1111 | * remove function | |
1112 | */ | |
1113 | dev_set_drvdata(&nfc_spi_dev->dev, spicontext); | |
1114 | ||
1115 | st95context->enable_gpio = | |
1116 | of_get_named_gpio(nfc_spi_dev->dev.of_node, | |
1117 | "enable-gpio", | |
1118 | 0); | |
1119 | if (!gpio_is_valid(st95context->enable_gpio)) { | |
1120 | dev_err(&nfc_spi_dev->dev, "No valid enable gpio\n"); | |
1121 | ret = st95context->enable_gpio; | |
1122 | goto err_disable_regulator; | |
1123 | } | |
1124 | ||
1125 | ret = devm_gpio_request_one(&nfc_spi_dev->dev, st95context->enable_gpio, | |
1126 | GPIOF_DIR_OUT | GPIOF_INIT_HIGH, | |
1127 | "enable_gpio"); | |
1128 | if (ret) | |
1129 | goto err_disable_regulator; | |
1130 | ||
1131 | if (nfc_spi_dev->irq > 0) { | |
1132 | if (devm_request_threaded_irq(&nfc_spi_dev->dev, | |
1133 | nfc_spi_dev->irq, | |
6b52d994 SS |
1134 | st95hf_irq_handler, |
1135 | st95hf_irq_thread_handler, | |
cab47333 SS |
1136 | IRQF_TRIGGER_FALLING, |
1137 | "st95hf", | |
1138 | (void *)st95context) < 0) { | |
1139 | dev_err(&nfc_spi_dev->dev, "err: irq request for st95hf is failed\n"); | |
1140 | ret = -EINVAL; | |
1141 | goto err_disable_regulator; | |
1142 | } | |
1143 | } else { | |
1144 | dev_err(&nfc_spi_dev->dev, "not a valid IRQ associated with ST95HF\n"); | |
1145 | ret = -EINVAL; | |
1146 | goto err_disable_regulator; | |
1147 | } | |
1148 | ||
1149 | /* | |
1150 | * First reset SPI to handle warm reset of the system. | |
1151 | * It will put the ST95HF device in Power ON state | |
1152 | * which make the state of device identical to state | |
1153 | * at the time of cold reset of the system. | |
1154 | */ | |
1155 | ret = st95hf_send_spi_reset_sequence(st95context); | |
1156 | if (ret) { | |
1157 | dev_err(&nfc_spi_dev->dev, "err: spi_reset_sequence failed\n"); | |
1158 | goto err_disable_regulator; | |
1159 | } | |
1160 | ||
1161 | /* call PowerOnReset sequence of ST95hf to activate it */ | |
1162 | ret = st95hf_por_sequence(st95context); | |
1163 | if (ret) { | |
1164 | dev_err(&nfc_spi_dev->dev, "err: por seq failed for st95hf\n"); | |
1165 | goto err_disable_regulator; | |
1166 | } | |
1167 | ||
1168 | /* create NFC dev object and register with NFC Subsystem */ | |
1169 | st95context->ddev = nfc_digital_allocate_device(&st95hf_nfc_digital_ops, | |
1170 | ST95HF_SUPPORTED_PROT, | |
1171 | ST95HF_CAPABILITIES, | |
1172 | ST95HF_HEADROOM_LEN, | |
1173 | ST95HF_TAILROOM_LEN); | |
1174 | if (!st95context->ddev) { | |
1175 | ret = -ENOMEM; | |
1176 | goto err_disable_regulator; | |
1177 | } | |
1178 | ||
1179 | st95context->nfcdev = st95context->ddev->nfc_dev; | |
1180 | nfc_digital_set_parent_dev(st95context->ddev, &nfc_spi_dev->dev); | |
1181 | ||
1182 | ret = nfc_digital_register_device(st95context->ddev); | |
1183 | if (ret) { | |
1184 | dev_err(&st95context->nfcdev->dev, "st95hf registration failed\n"); | |
1185 | goto err_free_digital_device; | |
1186 | } | |
1187 | ||
1188 | /* store st95context in nfc device object */ | |
1189 | nfc_digital_set_drvdata(st95context->ddev, st95context); | |
1190 | ||
1191 | sema_init(&st95context->exchange_lock, 1); | |
1192 | mutex_init(&st95context->rm_lock); | |
1193 | ||
1194 | return ret; | |
1195 | ||
1196 | err_free_digital_device: | |
1197 | nfc_digital_free_device(st95context->ddev); | |
1198 | err_disable_regulator: | |
1199 | if (st95context->st95hf_supply) | |
1200 | regulator_disable(st95context->st95hf_supply); | |
1201 | ||
1202 | return ret; | |
1203 | } | |
1204 | ||
1205 | static int st95hf_remove(struct spi_device *nfc_spi_dev) | |
1206 | { | |
1207 | int result = 0; | |
1208 | unsigned char reset_cmd = ST95HF_COMMAND_RESET; | |
1209 | struct st95hf_spi_context *spictx = dev_get_drvdata(&nfc_spi_dev->dev); | |
1210 | ||
1211 | struct st95hf_context *stcontext = container_of(spictx, | |
1212 | struct st95hf_context, | |
1213 | spicontext); | |
1214 | ||
1215 | mutex_lock(&stcontext->rm_lock); | |
1216 | ||
1217 | nfc_digital_unregister_device(stcontext->ddev); | |
1218 | nfc_digital_free_device(stcontext->ddev); | |
1219 | stcontext->nfcdev_free = true; | |
1220 | ||
1221 | mutex_unlock(&stcontext->rm_lock); | |
1222 | ||
1223 | /* if last in_send_cmd's ISR is pending, wait for it to finish */ | |
1224 | result = down_killable(&stcontext->exchange_lock); | |
1225 | if (result == -EINTR) | |
1226 | dev_err(&spictx->spidev->dev, "sleep for semaphore interrupted by signal\n"); | |
1227 | ||
1228 | /* next reset the ST95HF controller */ | |
1229 | result = st95hf_spi_send(&stcontext->spicontext, | |
1230 | &reset_cmd, | |
1231 | ST95HF_RESET_CMD_LEN, | |
1232 | ASYNC); | |
1233 | if (result) { | |
1234 | dev_err(&spictx->spidev->dev, | |
1235 | "ST95HF reset failed in remove() err = %d\n", result); | |
1236 | return result; | |
1237 | } | |
1238 | ||
1239 | /* wait for 3 ms to complete the controller reset process */ | |
1240 | usleep_range(3000, 4000); | |
1241 | ||
1242 | /* disable regulator */ | |
1243 | if (stcontext->st95hf_supply) | |
1244 | regulator_disable(stcontext->st95hf_supply); | |
1245 | ||
1246 | return result; | |
1247 | } | |
1248 | ||
1249 | /* Register as SPI protocol driver */ | |
1250 | static struct spi_driver st95hf_driver = { | |
1251 | .driver = { | |
1252 | .name = "st95hf", | |
1253 | .owner = THIS_MODULE, | |
d0483053 | 1254 | .of_match_table = of_match_ptr(st95hf_spi_of_match), |
cab47333 SS |
1255 | }, |
1256 | .id_table = st95hf_id, | |
1257 | .probe = st95hf_probe, | |
1258 | .remove = st95hf_remove, | |
1259 | }; | |
1260 | ||
1261 | module_spi_driver(st95hf_driver); | |
1262 | ||
1263 | MODULE_AUTHOR("Shikha Singh <shikha.singh@st.com>"); | |
1264 | MODULE_DESCRIPTION("ST NFC Transceiver ST95HF driver"); | |
1265 | MODULE_LICENSE("GPL v2"); |