2 * --------------------------------------------------------------------
3 * Driver for ST NFC Transceiver ST95HF
4 * --------------------------------------------------------------------
5 * Copyright (C) 2015 STMicroelectronics Pvt. Ltd. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <linux/err.h>
21 #include <linux/gpio.h>
22 #include <linux/init.h>
23 #include <linux/interrupt.h>
24 #include <linux/irq.h>
25 #include <linux/module.h>
26 #include <linux/netdevice.h>
27 #include <linux/nfc.h>
28 #include <linux/of_gpio.h>
30 #include <linux/of_irq.h>
31 #include <linux/property.h>
32 #include <linux/regulator/consumer.h>
33 #include <linux/wait.h>
34 #include <net/nfc/digital.h>
35 #include <net/nfc/nfc.h>
39 /* supported protocols */
40 #define ST95HF_SUPPORTED_PROT (NFC_PROTO_ISO14443_MASK | \
41 NFC_PROTO_ISO14443_B_MASK | \
42 NFC_PROTO_ISO15693_MASK)
43 /* driver capabilities */
44 #define ST95HF_CAPABILITIES NFC_DIGITAL_DRV_CAPS_IN_CRC
46 /* Command Send Interface */
47 /* ST95HF_COMMAND_SEND CMD Ids */
49 #define WRITE_REGISTER_CMD 0x9
50 #define PROTOCOL_SELECT_CMD 0x2
51 #define SEND_RECEIVE_CMD 0x4
53 /* Select protocol codes */
54 #define ISO15693_PROTOCOL_CODE 0x1
55 #define ISO14443A_PROTOCOL_CODE 0x2
56 #define ISO14443B_PROTOCOL_CODE 0x3
60 * 1 byte for control byte
64 #define ST95HF_HEADROOM_LEN 3
67 * tailroom is 1 for ISO14443A
68 * and 0 for ISO14443B/ISO15693,
69 * hence the max value 1 should be
72 #define ST95HF_TAILROOM_LEN 1
74 /* Command Response interface */
75 #define MAX_RESPONSE_BUFFER_SIZE 280
76 #define ECHORESPONSE 0x55
77 #define ST95HF_ERR_MASK 0xF
78 #define ST95HF_TIMEOUT_ERROR 0x87
79 #define ST95HF_NFCA_CRC_ERR_MASK 0x20
80 #define ST95HF_NFCB_CRC_ERR_MASK 0x01
82 /* ST95HF transmission flag values */
83 #define TRFLAG_NFCA_SHORT_FRAME 0x07
84 #define TRFLAG_NFCA_STD_FRAME 0x08
85 #define TRFLAG_NFCA_STD_FRAME_CRC 0x28
90 #define ISO14443A_RATS_REQ 0xE0
91 #define RATS_TB1_PRESENT_MASK 0x20
92 #define RATS_TA1_PRESENT_MASK 0x10
93 #define TB1_FWI_MASK 0xF0
94 #define WTX_REQ_FROM_TAG 0xF2
96 #define MAX_CMD_LEN 0x7
98 #define MAX_CMD_PARAMS 4
101 unsigned char cmd_id
;
102 unsigned char no_cmd_params
;
103 unsigned char cmd_params
[MAX_CMD_PARAMS
];
113 * List of top-level cmds to be used internally by the driver.
114 * All these commands are build on top of ST95HF basic commands
115 * such as SEND_RECEIVE_CMD, PROTOCOL_SELECT_CMD, etc.
116 * These top level cmds are used internally while implementing various ops of
117 * digital layer/driver probe or extending the digital framework layer for
118 * features that are not yet implemented there, for example, WTX cmd handling.
120 enum st95hf_cmd_list
{
122 CMD_ISO14443A_CONFIG
,
123 CMD_ISO14443A_DEMOGAIN
,
124 CMD_ISO14443B_DEMOGAIN
,
125 CMD_ISO14443A_PROTOCOL_SELECT
,
126 CMD_ISO14443B_PROTOCOL_SELECT
,
129 CMD_ISO15693_PROTOCOL_SELECT
,
132 static const struct cmd cmd_array
[] = {
139 [CMD_ISO14443A_CONFIG
] = {
141 .cmd_id
= WRITE_REGISTER_CMD
,
142 .no_cmd_params
= 0x4,
143 .cmd_params
= {0x3A, 0x00, 0x5A, 0x04},
146 [CMD_ISO14443A_DEMOGAIN
] = {
148 .cmd_id
= WRITE_REGISTER_CMD
,
149 .no_cmd_params
= 0x4,
150 .cmd_params
= {0x68, 0x01, 0x01, 0xDF},
153 [CMD_ISO14443B_DEMOGAIN
] = {
155 .cmd_id
= WRITE_REGISTER_CMD
,
156 .no_cmd_params
= 0x4,
157 .cmd_params
= {0x68, 0x01, 0x01, 0x51},
160 [CMD_ISO14443A_PROTOCOL_SELECT
] = {
162 .cmd_id
= PROTOCOL_SELECT_CMD
,
163 .no_cmd_params
= 0x4,
164 .cmd_params
= {ISO14443A_PROTOCOL_CODE
, 0x00, 0x01, 0xA0},
167 [CMD_ISO14443B_PROTOCOL_SELECT
] = {
169 .cmd_id
= PROTOCOL_SELECT_CMD
,
170 .no_cmd_params
= 0x4,
171 .cmd_params
= {ISO14443B_PROTOCOL_CODE
, 0x01, 0x03, 0xFF},
174 [CMD_WTX_RESPONSE
] = {
176 .cmd_id
= SEND_RECEIVE_CMD
,
177 .no_cmd_params
= 0x3,
178 .cmd_params
= {0xF2, 0x00, TRFLAG_NFCA_STD_FRAME_CRC
},
183 .cmd_id
= PROTOCOL_SELECT_CMD
,
184 .no_cmd_params
= 0x2,
185 .cmd_params
= {0x0, 0x0},
188 [CMD_ISO15693_PROTOCOL_SELECT
] = {
190 .cmd_id
= PROTOCOL_SELECT_CMD
,
191 .no_cmd_params
= 0x2,
192 .cmd_params
= {ISO15693_PROTOCOL_CODE
, 0x0D},
197 /* st95_digital_cmd_complete_arg stores client context */
198 struct st95_digital_cmd_complete_arg
{
199 struct sk_buff
*skb_resp
;
200 nfc_digital_cmd_complete_t complete_cb
;
206 * structure containing ST95HF driver specific data.
207 * @spicontext: structure containing information required
208 * for spi communication between st95hf and host.
209 * @ddev: nfc digital device object.
210 * @nfcdev: nfc device object.
211 * @enable_gpio: gpio used to enable st95hf transceiver.
212 * @complete_cb_arg: structure to store various context information
213 * that is passed from nfc requesting thread to the threaded ISR.
214 * @st95hf_supply: regulator "consumer" for NFC device.
215 * @sendrcv_trflag: last byte of frame send by sendrecv command
216 * of st95hf. This byte contains transmission flag info.
217 * @exchange_lock: semaphore used for signaling the st95hf_remove
218 * function that the last outstanding async nfc request is finished.
219 * @rm_lock: mutex for ensuring safe access of nfc digital object
220 * from threaded ISR. Usage of this mutex avoids any race between
221 * deletion of the object from st95hf_remove() and its access from
223 * @nfcdev_free: flag to have the state of nfc device object.
225 * @current_protocol: current nfc protocol.
226 * @current_rf_tech: current rf technology.
227 * @fwi: frame waiting index, received in reply of RATS according to
230 struct st95hf_context
{
231 struct st95hf_spi_context spicontext
;
232 struct nfc_digital_dev
*ddev
;
233 struct nfc_dev
*nfcdev
;
234 unsigned int enable_gpio
;
235 struct st95_digital_cmd_complete_arg complete_cb_arg
;
236 struct regulator
*st95hf_supply
;
237 unsigned char sendrcv_trflag
;
238 struct semaphore exchange_lock
;
239 struct mutex rm_lock
;
247 * st95hf_send_recv_cmd() is for sending commands to ST95HF
248 * that are described in the cmd_array[]. It can optionally
249 * receive the response if the cmd request is of type
250 * SYNC. For that to happen caller must pass true to recv_res.
251 * For ASYNC request, recv_res is ignored and the
252 * function will never try to receive the response on behalf
255 static int st95hf_send_recv_cmd(struct st95hf_context
*st95context
,
256 enum st95hf_cmd_list cmd
,
258 struct param_list
*list_array
,
261 unsigned char spi_cmd_buffer
[MAX_CMD_LEN
];
263 struct device
*dev
= &st95context
->spicontext
.spidev
->dev
;
265 if (cmd_array
[cmd
].cmd_len
> MAX_CMD_LEN
)
267 if (cmd_array
[cmd
].no_cmd_params
< no_modif
)
269 if (no_modif
&& !list_array
)
272 spi_cmd_buffer
[0] = ST95HF_COMMAND_SEND
;
273 spi_cmd_buffer
[1] = cmd_array
[cmd
].cmd_id
;
274 spi_cmd_buffer
[2] = cmd_array
[cmd
].no_cmd_params
;
276 memcpy(&spi_cmd_buffer
[3], cmd_array
[cmd
].cmd_params
,
279 for (i
= 0; i
< no_modif
; i
++) {
280 if (list_array
[i
].param_offset
>= cmd_array
[cmd
].no_cmd_params
)
282 spi_cmd_buffer
[3 + list_array
[i
].param_offset
] =
283 list_array
[i
].new_param_val
;
286 ret
= st95hf_spi_send(&st95context
->spicontext
,
288 cmd_array
[cmd
].cmd_len
,
291 dev_err(dev
, "st95hf_spi_send failed with error %d\n", ret
);
295 if (cmd_array
[cmd
].req
== SYNC
&& recv_res
) {
296 unsigned char st95hf_response_arr
[2];
298 ret
= st95hf_spi_recv_response(&st95context
->spicontext
,
299 st95hf_response_arr
);
301 dev_err(dev
, "spi error from st95hf_spi_recv_response(), err = 0x%x\n",
306 if (st95hf_response_arr
[0]) {
307 dev_err(dev
, "st95hf error from st95hf_spi_recv_response(), err = 0x%x\n",
308 st95hf_response_arr
[0]);
316 static int st95hf_echo_command(struct st95hf_context
*st95context
)
319 unsigned char echo_response
;
321 result
= st95hf_send_recv_cmd(st95context
, CMD_ECHO
, 0, NULL
, false);
325 /* If control reached here, response can be taken */
326 result
= st95hf_spi_recv_echo_res(&st95context
->spicontext
,
329 dev_err(&st95context
->spicontext
.spidev
->dev
,
330 "err: echo response receieve error = 0x%x\n", result
);
334 if (echo_response
== ECHORESPONSE
)
337 dev_err(&st95context
->spicontext
.spidev
->dev
, "err: echo res is 0x%x\n",
343 static int secondary_configuration_type4a(struct st95hf_context
*stcontext
)
346 struct device
*dev
= &stcontext
->nfcdev
->dev
;
348 /* 14443A config setting after select protocol */
349 result
= st95hf_send_recv_cmd(stcontext
,
350 CMD_ISO14443A_CONFIG
,
355 dev_err(dev
, "type a config cmd, err = 0x%x\n", result
);
359 /* 14443A demo gain setting */
360 result
= st95hf_send_recv_cmd(stcontext
,
361 CMD_ISO14443A_DEMOGAIN
,
366 dev_err(dev
, "type a demogain cmd, err = 0x%x\n", result
);
371 static int secondary_configuration_type4b(struct st95hf_context
*stcontext
)
374 struct device
*dev
= &stcontext
->nfcdev
->dev
;
376 result
= st95hf_send_recv_cmd(stcontext
,
377 CMD_ISO14443B_DEMOGAIN
,
382 dev_err(dev
, "type b demogain cmd, err = 0x%x\n", result
);
387 static int st95hf_select_protocol(struct st95hf_context
*stcontext
, int type
)
392 dev
= &stcontext
->nfcdev
->dev
;
395 case NFC_DIGITAL_RF_TECH_106A
:
396 stcontext
->current_rf_tech
= NFC_DIGITAL_RF_TECH_106A
;
397 result
= st95hf_send_recv_cmd(stcontext
,
398 CMD_ISO14443A_PROTOCOL_SELECT
,
403 dev_err(dev
, "protocol sel, err = 0x%x\n",
408 /* secondary config. for 14443Type 4A after protocol select */
409 result
= secondary_configuration_type4a(stcontext
);
411 dev_err(dev
, "type a secondary config, err = 0x%x\n",
416 case NFC_DIGITAL_RF_TECH_106B
:
417 stcontext
->current_rf_tech
= NFC_DIGITAL_RF_TECH_106B
;
418 result
= st95hf_send_recv_cmd(stcontext
,
419 CMD_ISO14443B_PROTOCOL_SELECT
,
424 dev_err(dev
, "protocol sel send, err = 0x%x\n",
430 * delay of 5-6 ms is required after select protocol
431 * command in case of ISO14443 Type B
433 usleep_range(50000, 60000);
435 /* secondary config. for 14443Type 4B after protocol select */
436 result
= secondary_configuration_type4b(stcontext
);
438 dev_err(dev
, "type b secondary config, err = 0x%x\n",
443 case NFC_DIGITAL_RF_TECH_ISO15693
:
444 stcontext
->current_rf_tech
= NFC_DIGITAL_RF_TECH_ISO15693
;
445 result
= st95hf_send_recv_cmd(stcontext
,
446 CMD_ISO15693_PROTOCOL_SELECT
,
451 dev_err(dev
, "protocol sel send, err = 0x%x\n",
463 static void st95hf_send_st95enable_negativepulse(struct st95hf_context
*st95con
)
465 /* First make irq_in pin high */
466 gpio_set_value(st95con
->enable_gpio
, HIGH
);
468 /* wait for 1 milisecond */
469 usleep_range(1000, 2000);
471 /* Make irq_in pin low */
472 gpio_set_value(st95con
->enable_gpio
, LOW
);
474 /* wait for minimum interrupt pulse to make st95 active */
475 usleep_range(1000, 2000);
477 /* At end make it high */
478 gpio_set_value(st95con
->enable_gpio
, HIGH
);
482 * Send a reset sequence over SPI bus (Reset command + wait 3ms +
483 * negative pulse on st95hf enable gpio
485 static int st95hf_send_spi_reset_sequence(struct st95hf_context
*st95context
)
488 unsigned char reset_cmd
= ST95HF_COMMAND_RESET
;
490 result
= st95hf_spi_send(&st95context
->spicontext
,
492 ST95HF_RESET_CMD_LEN
,
495 dev_err(&st95context
->spicontext
.spidev
->dev
,
496 "spi reset sequence cmd error = %d", result
);
500 /* wait for 3 milisecond to complete the controller reset process */
501 usleep_range(3000, 4000);
503 /* send negative pulse to make st95hf active */
504 st95hf_send_st95enable_negativepulse(st95context
);
506 /* wait for 10 milisecond : HFO setup time */
507 usleep_range(10000, 20000);
512 static int st95hf_por_sequence(struct st95hf_context
*st95context
)
517 st95hf_send_st95enable_negativepulse(st95context
);
519 usleep_range(5000, 6000);
521 /* send an ECHO command and checks ST95HF response */
522 result
= st95hf_echo_command(st95context
);
524 dev_dbg(&st95context
->spicontext
.spidev
->dev
,
525 "response from echo function = 0x%x, attempt = %d\n",
526 result
, nth_attempt
);
531 /* send an pulse on IRQ in case of the chip is on sleep state */
532 if (nth_attempt
== 2)
533 st95hf_send_st95enable_negativepulse(st95context
);
535 st95hf_send_spi_reset_sequence(st95context
);
537 /* delay of 50 milisecond */
538 usleep_range(50000, 51000);
539 } while (nth_attempt
++ < 3);
544 static int iso14443_config_fdt(struct st95hf_context
*st95context
, int wtxm
)
547 struct device
*dev
= &st95context
->spicontext
.spidev
->dev
;
548 struct nfc_digital_dev
*nfcddev
= st95context
->ddev
;
549 unsigned char pp_typeb
;
550 struct param_list new_params
[2];
552 pp_typeb
= cmd_array
[CMD_ISO14443B_PROTOCOL_SELECT
].cmd_params
[2];
554 if (nfcddev
->curr_protocol
== NFC_PROTO_ISO14443
&&
555 st95context
->fwi
< 4)
556 st95context
->fwi
= 4;
558 new_params
[0].param_offset
= 2;
559 if (nfcddev
->curr_protocol
== NFC_PROTO_ISO14443
)
560 new_params
[0].new_param_val
= st95context
->fwi
;
561 else if (nfcddev
->curr_protocol
== NFC_PROTO_ISO14443_B
)
562 new_params
[0].new_param_val
= pp_typeb
;
564 new_params
[1].param_offset
= 3;
565 new_params
[1].new_param_val
= wtxm
;
567 switch (nfcddev
->curr_protocol
) {
568 case NFC_PROTO_ISO14443
:
569 result
= st95hf_send_recv_cmd(st95context
,
570 CMD_ISO14443A_PROTOCOL_SELECT
,
575 dev_err(dev
, "WTX type a sel proto, err = 0x%x\n",
580 /* secondary config. for 14443Type 4A after protocol select */
581 result
= secondary_configuration_type4a(st95context
);
583 dev_err(dev
, "WTX type a second. config, err = 0x%x\n",
588 case NFC_PROTO_ISO14443_B
:
589 result
= st95hf_send_recv_cmd(st95context
,
590 CMD_ISO14443B_PROTOCOL_SELECT
,
595 dev_err(dev
, "WTX type b sel proto, err = 0x%x\n",
600 /* secondary config. for 14443Type 4B after protocol select */
601 result
= secondary_configuration_type4b(st95context
);
603 dev_err(dev
, "WTX type b second. config, err = 0x%x\n",
615 static int st95hf_handle_wtx(struct st95hf_context
*stcontext
,
620 unsigned char val_mm
= 0;
621 struct param_list new_params
[1];
622 struct nfc_digital_dev
*nfcddev
= stcontext
->ddev
;
623 struct device
*dev
= &stcontext
->nfcdev
->dev
;
626 result
= iso14443_config_fdt(stcontext
, wtx_val
& 0x3f);
628 dev_err(dev
, "Config. setting error on WTX req, err = 0x%x\n",
633 /* Send response of wtx with ASYNC as no response expected */
634 new_params
[0].param_offset
= 1;
635 new_params
[0].new_param_val
= wtx_val
;
637 result
= st95hf_send_recv_cmd(stcontext
,
643 dev_err(dev
, "WTX response send, err = 0x%x\n", result
);
647 /* if no new wtx, cofigure with default values */
648 if (nfcddev
->curr_protocol
== NFC_PROTO_ISO14443
)
649 val_mm
= cmd_array
[CMD_ISO14443A_PROTOCOL_SELECT
].cmd_params
[3];
650 else if (nfcddev
->curr_protocol
== NFC_PROTO_ISO14443_B
)
651 val_mm
= cmd_array
[CMD_ISO14443B_PROTOCOL_SELECT
].cmd_params
[3];
653 result
= iso14443_config_fdt(stcontext
, val_mm
);
655 dev_err(dev
, "Default config. setting error after WTX processing, err = 0x%x\n",
661 static int st95hf_error_handling(struct st95hf_context
*stcontext
,
662 struct sk_buff
*skb_resp
,
666 unsigned char error_byte
;
667 struct device
*dev
= &stcontext
->nfcdev
->dev
;
669 /* First check ST95HF specific error */
670 if (skb_resp
->data
[0] & ST95HF_ERR_MASK
) {
671 if (skb_resp
->data
[0] == ST95HF_TIMEOUT_ERROR
)
678 /* Check for CRC err only if CRC is present in the tag response */
679 switch (stcontext
->current_rf_tech
) {
680 case NFC_DIGITAL_RF_TECH_106A
:
681 if (stcontext
->sendrcv_trflag
== TRFLAG_NFCA_STD_FRAME_CRC
) {
682 error_byte
= skb_resp
->data
[res_len
- 3];
683 if (error_byte
& ST95HF_NFCA_CRC_ERR_MASK
) {
684 /* CRC error occurred */
685 dev_err(dev
, "CRC error, byte received = 0x%x\n",
691 case NFC_DIGITAL_RF_TECH_106B
:
692 case NFC_DIGITAL_RF_TECH_ISO15693
:
693 error_byte
= skb_resp
->data
[res_len
- 1];
694 if (error_byte
& ST95HF_NFCB_CRC_ERR_MASK
) {
695 /* CRC error occurred */
696 dev_err(dev
, "CRC error, byte received = 0x%x\n",
706 static int st95hf_response_handler(struct st95hf_context
*stcontext
,
707 struct sk_buff
*skb_resp
,
712 unsigned char val_mm
;
713 struct nfc_digital_dev
*nfcddev
= stcontext
->ddev
;
714 struct device
*dev
= &stcontext
->nfcdev
->dev
;
715 struct st95_digital_cmd_complete_arg
*cb_arg
;
717 cb_arg
= &stcontext
->complete_cb_arg
;
719 /* Process the response */
720 skb_put(skb_resp
, res_len
);
722 /* Remove st95 header */
723 skb_pull(skb_resp
, 2);
725 skb_len
= skb_resp
->len
;
727 /* check if it is case of RATS request reply & FWI is present */
728 if (nfcddev
->curr_protocol
== NFC_PROTO_ISO14443
&& cb_arg
->rats
&&
729 (skb_resp
->data
[1] & RATS_TB1_PRESENT_MASK
)) {
730 if (skb_resp
->data
[1] & RATS_TA1_PRESENT_MASK
)
732 (skb_resp
->data
[3] & TB1_FWI_MASK
) >> 4;
735 (skb_resp
->data
[2] & TB1_FWI_MASK
) >> 4;
737 val_mm
= cmd_array
[CMD_ISO14443A_PROTOCOL_SELECT
].cmd_params
[3];
739 result
= iso14443_config_fdt(stcontext
, val_mm
);
741 dev_err(dev
, "error in config_fdt to handle fwi of ATS, error=%d\n",
746 cb_arg
->rats
= false;
748 /* Remove CRC bytes only if received frames data has an eod (CRC) */
749 switch (stcontext
->current_rf_tech
) {
750 case NFC_DIGITAL_RF_TECH_106A
:
751 if (stcontext
->sendrcv_trflag
== TRFLAG_NFCA_STD_FRAME_CRC
)
752 skb_trim(skb_resp
, (skb_len
- 5));
754 skb_trim(skb_resp
, (skb_len
- 3));
756 case NFC_DIGITAL_RF_TECH_106B
:
757 case NFC_DIGITAL_RF_TECH_ISO15693
:
758 skb_trim(skb_resp
, (skb_len
- 3));
765 static irqreturn_t
st95hf_irq_handler(int irq
, void *st95hfcontext
)
767 struct st95hf_context
*stcontext
=
768 (struct st95hf_context
*)st95hfcontext
;
770 if (stcontext
->spicontext
.req_issync
) {
771 complete(&stcontext
->spicontext
.done
);
772 stcontext
->spicontext
.req_issync
= false;
776 return IRQ_WAKE_THREAD
;
779 static irqreturn_t
st95hf_irq_thread_handler(int irq
, void *st95hfcontext
)
785 struct device
*spidevice
;
786 struct nfc_digital_dev
*nfcddev
;
787 struct sk_buff
*skb_resp
;
788 struct st95hf_context
*stcontext
=
789 (struct st95hf_context
*)st95hfcontext
;
790 struct st95_digital_cmd_complete_arg
*cb_arg
;
792 spidevice
= &stcontext
->spicontext
.spidev
->dev
;
795 * check semaphore, if not down() already, then we don't
796 * know in which context the ISR is called and surely it
797 * will be a bug. Note that down() of the semaphore is done
798 * in the corresponding st95hf_in_send_cmd() and then
799 * only this ISR should be called. ISR will up() the
800 * semaphore before leaving. Hence when the ISR is called
801 * the correct behaviour is down_trylock() should always
802 * return 1 (indicating semaphore cant be taken and hence no
803 * change in semaphore count).
804 * If not, then we up() the semaphore and crash on
807 if (!down_trylock(&stcontext
->exchange_lock
)) {
808 up(&stcontext
->exchange_lock
);
809 WARN(1, "unknown context in ST95HF ISR");
813 cb_arg
= &stcontext
->complete_cb_arg
;
814 skb_resp
= cb_arg
->skb_resp
;
816 mutex_lock(&stcontext
->rm_lock
);
817 res_len
= st95hf_spi_recv_response(&stcontext
->spicontext
,
820 dev_err(spidevice
, "TISR spi response err = 0x%x\n", res_len
);
825 /* if stcontext->nfcdev_free is true, it means remove already ran */
826 if (stcontext
->nfcdev_free
) {
831 dev
= &stcontext
->nfcdev
->dev
;
832 nfcddev
= stcontext
->ddev
;
833 if (skb_resp
->data
[2] == WTX_REQ_FROM_TAG
) {
834 /* Request for new FWT from tag */
835 result
= st95hf_handle_wtx(stcontext
, true, skb_resp
->data
[3]);
840 mutex_unlock(&stcontext
->rm_lock
);
844 result
= st95hf_error_handling(stcontext
, skb_resp
, res_len
);
848 result
= st95hf_response_handler(stcontext
, skb_resp
, res_len
);
853 * If select protocol is done on wtx req. do select protocol
854 * again with default values
858 result
= st95hf_handle_wtx(stcontext
, false, 0);
863 /* call digital layer callback */
864 cb_arg
->complete_cb(stcontext
->ddev
, cb_arg
->cb_usrarg
, skb_resp
);
866 /* up the semaphore before returning */
867 up(&stcontext
->exchange_lock
);
868 mutex_unlock(&stcontext
->rm_lock
);
875 cb_arg
->rats
= false;
876 skb_resp
= ERR_PTR(result
);
877 /* call of callback with error */
878 cb_arg
->complete_cb(stcontext
->ddev
, cb_arg
->cb_usrarg
, skb_resp
);
879 /* up the semaphore before returning */
880 up(&stcontext
->exchange_lock
);
881 mutex_unlock(&stcontext
->rm_lock
);
885 /* NFC ops functions definition */
886 static int st95hf_in_configure_hw(struct nfc_digital_dev
*ddev
,
890 struct st95hf_context
*stcontext
= nfc_digital_get_drvdata(ddev
);
892 if (type
== NFC_DIGITAL_CONFIG_RF_TECH
)
893 return st95hf_select_protocol(stcontext
, param
);
895 if (type
== NFC_DIGITAL_CONFIG_FRAMING
) {
897 case NFC_DIGITAL_FRAMING_NFCA_SHORT
:
898 stcontext
->sendrcv_trflag
= TRFLAG_NFCA_SHORT_FRAME
;
900 case NFC_DIGITAL_FRAMING_NFCA_STANDARD
:
901 stcontext
->sendrcv_trflag
= TRFLAG_NFCA_STD_FRAME
;
903 case NFC_DIGITAL_FRAMING_NFCA_T4T
:
904 case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP
:
905 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A
:
906 stcontext
->sendrcv_trflag
= TRFLAG_NFCA_STD_FRAME_CRC
;
908 case NFC_DIGITAL_FRAMING_NFCB
:
909 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY
:
910 case NFC_DIGITAL_FRAMING_ISO15693_T5T
:
918 static int rf_off(struct st95hf_context
*stcontext
)
923 dev
= &stcontext
->nfcdev
->dev
;
925 rc
= st95hf_send_recv_cmd(stcontext
, CMD_FIELD_OFF
, 0, NULL
, true);
927 dev_err(dev
, "protocol sel send field off, err = 0x%x\n", rc
);
932 static int st95hf_in_send_cmd(struct nfc_digital_dev
*ddev
,
935 nfc_digital_cmd_complete_t cb
,
938 struct st95hf_context
*stcontext
= nfc_digital_get_drvdata(ddev
);
940 struct sk_buff
*skb_resp
;
941 int len_data_to_tag
= 0;
943 skb_resp
= nfc_alloc_recv_skb(MAX_RESPONSE_BUFFER_SIZE
, GFP_KERNEL
);
949 switch (stcontext
->current_rf_tech
) {
950 case NFC_DIGITAL_RF_TECH_106A
:
951 len_data_to_tag
= skb
->len
+ 1;
952 skb_put_u8(skb
, stcontext
->sendrcv_trflag
);
954 case NFC_DIGITAL_RF_TECH_106B
:
955 case NFC_DIGITAL_RF_TECH_ISO15693
:
956 len_data_to_tag
= skb
->len
;
964 skb
->data
[0] = ST95HF_COMMAND_SEND
;
965 skb
->data
[1] = SEND_RECEIVE_CMD
;
966 skb
->data
[2] = len_data_to_tag
;
968 stcontext
->complete_cb_arg
.skb_resp
= skb_resp
;
969 stcontext
->complete_cb_arg
.cb_usrarg
= arg
;
970 stcontext
->complete_cb_arg
.complete_cb
= cb
;
972 if ((skb
->data
[3] == ISO14443A_RATS_REQ
) &&
973 ddev
->curr_protocol
== NFC_PROTO_ISO14443
)
974 stcontext
->complete_cb_arg
.rats
= true;
977 * down the semaphore to indicate to remove func that an
978 * ISR is pending, note that it will not block here in any case.
979 * If found blocked, it is a BUG!
981 rc
= down_killable(&stcontext
->exchange_lock
);
983 WARN(1, "Semaphore is not found up in st95hf_in_send_cmd\n");
987 rc
= st95hf_spi_send(&stcontext
->spicontext
, skb
->data
,
991 dev_err(&stcontext
->nfcdev
->dev
,
992 "Error %d trying to perform data_exchange", rc
);
993 /* up the semaphore since ISR will never come in this case */
994 up(&stcontext
->exchange_lock
);
1003 kfree_skb(skb_resp
);
1008 /* p2p will be supported in a later release ! */
1009 static int st95hf_tg_configure_hw(struct nfc_digital_dev
*ddev
,
1016 static int st95hf_tg_send_cmd(struct nfc_digital_dev
*ddev
,
1017 struct sk_buff
*skb
,
1019 nfc_digital_cmd_complete_t cb
,
1025 static int st95hf_tg_listen(struct nfc_digital_dev
*ddev
,
1027 nfc_digital_cmd_complete_t cb
,
1033 static int st95hf_tg_get_rf_tech(struct nfc_digital_dev
*ddev
, u8
*rf_tech
)
1038 static int st95hf_switch_rf(struct nfc_digital_dev
*ddev
, bool on
)
1041 struct st95hf_context
*stcontext
= nfc_digital_get_drvdata(ddev
);
1043 rf_tech
= ddev
->curr_rf_tech
;
1046 /* switch on RF field */
1047 return st95hf_select_protocol(stcontext
, rf_tech
);
1049 /* switch OFF RF field */
1050 return rf_off(stcontext
);
1053 /* TODO st95hf_abort_cmd */
1054 static void st95hf_abort_cmd(struct nfc_digital_dev
*ddev
)
1058 static struct nfc_digital_ops st95hf_nfc_digital_ops
= {
1059 .in_configure_hw
= st95hf_in_configure_hw
,
1060 .in_send_cmd
= st95hf_in_send_cmd
,
1062 .tg_listen
= st95hf_tg_listen
,
1063 .tg_configure_hw
= st95hf_tg_configure_hw
,
1064 .tg_send_cmd
= st95hf_tg_send_cmd
,
1065 .tg_get_rf_tech
= st95hf_tg_get_rf_tech
,
1067 .switch_rf
= st95hf_switch_rf
,
1068 .abort_cmd
= st95hf_abort_cmd
,
1071 static const struct spi_device_id st95hf_id
[] = {
1075 MODULE_DEVICE_TABLE(spi
, st95hf_id
);
1077 static int st95hf_probe(struct spi_device
*nfc_spi_dev
)
1081 struct st95hf_context
*st95context
;
1082 struct st95hf_spi_context
*spicontext
;
1084 nfc_info(&nfc_spi_dev
->dev
, "ST95HF driver probe called.\n");
1086 st95context
= devm_kzalloc(&nfc_spi_dev
->dev
,
1087 sizeof(struct st95hf_context
),
1092 spicontext
= &st95context
->spicontext
;
1094 spicontext
->spidev
= nfc_spi_dev
;
1097 cmd_array
[CMD_ISO14443A_PROTOCOL_SELECT
].cmd_params
[2];
1099 if (device_property_present(&nfc_spi_dev
->dev
, "st95hfvin")) {
1100 st95context
->st95hf_supply
=
1101 devm_regulator_get(&nfc_spi_dev
->dev
,
1103 if (IS_ERR(st95context
->st95hf_supply
)) {
1104 dev_err(&nfc_spi_dev
->dev
, "failed to acquire regulator\n");
1105 return PTR_ERR(st95context
->st95hf_supply
);
1108 ret
= regulator_enable(st95context
->st95hf_supply
);
1110 dev_err(&nfc_spi_dev
->dev
, "failed to enable regulator\n");
1115 init_completion(&spicontext
->done
);
1116 mutex_init(&spicontext
->spi_lock
);
1119 * Store spicontext in spi device object for using it in
1122 dev_set_drvdata(&nfc_spi_dev
->dev
, spicontext
);
1124 st95context
->enable_gpio
=
1125 of_get_named_gpio(nfc_spi_dev
->dev
.of_node
,
1128 if (!gpio_is_valid(st95context
->enable_gpio
)) {
1129 dev_err(&nfc_spi_dev
->dev
, "No valid enable gpio\n");
1130 ret
= st95context
->enable_gpio
;
1131 goto err_disable_regulator
;
1134 ret
= devm_gpio_request_one(&nfc_spi_dev
->dev
, st95context
->enable_gpio
,
1135 GPIOF_DIR_OUT
| GPIOF_INIT_HIGH
,
1138 goto err_disable_regulator
;
1140 if (nfc_spi_dev
->irq
> 0) {
1141 if (devm_request_threaded_irq(&nfc_spi_dev
->dev
,
1144 st95hf_irq_thread_handler
,
1145 IRQF_TRIGGER_FALLING
,
1147 (void *)st95context
) < 0) {
1148 dev_err(&nfc_spi_dev
->dev
, "err: irq request for st95hf is failed\n");
1150 goto err_disable_regulator
;
1153 dev_err(&nfc_spi_dev
->dev
, "not a valid IRQ associated with ST95HF\n");
1155 goto err_disable_regulator
;
1159 * First reset SPI to handle warm reset of the system.
1160 * It will put the ST95HF device in Power ON state
1161 * which make the state of device identical to state
1162 * at the time of cold reset of the system.
1164 ret
= st95hf_send_spi_reset_sequence(st95context
);
1166 dev_err(&nfc_spi_dev
->dev
, "err: spi_reset_sequence failed\n");
1167 goto err_disable_regulator
;
1170 /* call PowerOnReset sequence of ST95hf to activate it */
1171 ret
= st95hf_por_sequence(st95context
);
1173 dev_err(&nfc_spi_dev
->dev
, "err: por seq failed for st95hf\n");
1174 goto err_disable_regulator
;
1177 /* create NFC dev object and register with NFC Subsystem */
1178 st95context
->ddev
= nfc_digital_allocate_device(&st95hf_nfc_digital_ops
,
1179 ST95HF_SUPPORTED_PROT
,
1180 ST95HF_CAPABILITIES
,
1181 ST95HF_HEADROOM_LEN
,
1182 ST95HF_TAILROOM_LEN
);
1183 if (!st95context
->ddev
) {
1185 goto err_disable_regulator
;
1188 st95context
->nfcdev
= st95context
->ddev
->nfc_dev
;
1189 nfc_digital_set_parent_dev(st95context
->ddev
, &nfc_spi_dev
->dev
);
1191 ret
= nfc_digital_register_device(st95context
->ddev
);
1193 dev_err(&st95context
->nfcdev
->dev
, "st95hf registration failed\n");
1194 goto err_free_digital_device
;
1197 /* store st95context in nfc device object */
1198 nfc_digital_set_drvdata(st95context
->ddev
, st95context
);
1200 sema_init(&st95context
->exchange_lock
, 1);
1201 mutex_init(&st95context
->rm_lock
);
1205 err_free_digital_device
:
1206 nfc_digital_free_device(st95context
->ddev
);
1207 err_disable_regulator
:
1208 if (st95context
->st95hf_supply
)
1209 regulator_disable(st95context
->st95hf_supply
);
1214 static int st95hf_remove(struct spi_device
*nfc_spi_dev
)
1217 unsigned char reset_cmd
= ST95HF_COMMAND_RESET
;
1218 struct st95hf_spi_context
*spictx
= dev_get_drvdata(&nfc_spi_dev
->dev
);
1220 struct st95hf_context
*stcontext
= container_of(spictx
,
1221 struct st95hf_context
,
1224 mutex_lock(&stcontext
->rm_lock
);
1226 nfc_digital_unregister_device(stcontext
->ddev
);
1227 nfc_digital_free_device(stcontext
->ddev
);
1228 stcontext
->nfcdev_free
= true;
1230 mutex_unlock(&stcontext
->rm_lock
);
1232 /* if last in_send_cmd's ISR is pending, wait for it to finish */
1233 result
= down_killable(&stcontext
->exchange_lock
);
1234 if (result
== -EINTR
)
1235 dev_err(&spictx
->spidev
->dev
, "sleep for semaphore interrupted by signal\n");
1237 /* next reset the ST95HF controller */
1238 result
= st95hf_spi_send(&stcontext
->spicontext
,
1240 ST95HF_RESET_CMD_LEN
,
1243 dev_err(&spictx
->spidev
->dev
,
1244 "ST95HF reset failed in remove() err = %d\n", result
);
1248 /* wait for 3 ms to complete the controller reset process */
1249 usleep_range(3000, 4000);
1251 /* disable regulator */
1252 if (stcontext
->st95hf_supply
)
1253 regulator_disable(stcontext
->st95hf_supply
);
1258 /* Register as SPI protocol driver */
1259 static struct spi_driver st95hf_driver
= {
1262 .owner
= THIS_MODULE
,
1264 .id_table
= st95hf_id
,
1265 .probe
= st95hf_probe
,
1266 .remove
= st95hf_remove
,
1269 module_spi_driver(st95hf_driver
);
1271 MODULE_AUTHOR("Shikha Singh <shikha.singh@st.com>");
1272 MODULE_DESCRIPTION("ST NFC Transceiver ST95HF driver");
1273 MODULE_LICENSE("GPL v2");