1 // SPDX-License-Identifier: GPL-2.0-only
3 * --------------------------------------------------------------------
4 * Driver for ST NFC Transceiver ST95HF
5 * --------------------------------------------------------------------
6 * Copyright (C) 2015 STMicroelectronics Pvt. Ltd. All rights reserved.
10 #include <linux/gpio/consumer.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>
18 #include <linux/property.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/wait.h>
21 #include <net/nfc/digital.h>
22 #include <net/nfc/nfc.h>
26 /* supported protocols */
27 #define ST95HF_SUPPORTED_PROT (NFC_PROTO_ISO14443_MASK | \
28 NFC_PROTO_ISO14443_B_MASK | \
29 NFC_PROTO_ISO15693_MASK)
30 /* driver capabilities */
31 #define ST95HF_CAPABILITIES NFC_DIGITAL_DRV_CAPS_IN_CRC
33 /* Command Send Interface */
34 /* ST95HF_COMMAND_SEND CMD Ids */
36 #define WRITE_REGISTER_CMD 0x9
37 #define PROTOCOL_SELECT_CMD 0x2
38 #define SEND_RECEIVE_CMD 0x4
40 /* Select protocol codes */
41 #define ISO15693_PROTOCOL_CODE 0x1
42 #define ISO14443A_PROTOCOL_CODE 0x2
43 #define ISO14443B_PROTOCOL_CODE 0x3
47 * 1 byte for control byte
51 #define ST95HF_HEADROOM_LEN 3
54 * tailroom is 1 for ISO14443A
55 * and 0 for ISO14443B/ISO15693,
56 * hence the max value 1 should be
59 #define ST95HF_TAILROOM_LEN 1
61 /* Command Response interface */
62 #define MAX_RESPONSE_BUFFER_SIZE 280
63 #define ECHORESPONSE 0x55
64 #define ST95HF_ERR_MASK 0xF
65 #define ST95HF_TIMEOUT_ERROR 0x87
66 #define ST95HF_NFCA_CRC_ERR_MASK 0x20
67 #define ST95HF_NFCB_CRC_ERR_MASK 0x01
69 /* ST95HF transmission flag values */
70 #define TRFLAG_NFCA_SHORT_FRAME 0x07
71 #define TRFLAG_NFCA_STD_FRAME 0x08
72 #define TRFLAG_NFCA_STD_FRAME_CRC 0x28
77 #define ISO14443A_RATS_REQ 0xE0
78 #define RATS_TB1_PRESENT_MASK 0x20
79 #define RATS_TA1_PRESENT_MASK 0x10
80 #define TB1_FWI_MASK 0xF0
81 #define WTX_REQ_FROM_TAG 0xF2
83 #define MAX_CMD_LEN 0x7
85 #define MAX_CMD_PARAMS 4
89 unsigned char no_cmd_params;
90 unsigned char cmd_params[MAX_CMD_PARAMS];
100 * List of top-level cmds to be used internally by the driver.
101 * All these commands are build on top of ST95HF basic commands
102 * such as SEND_RECEIVE_CMD, PROTOCOL_SELECT_CMD, etc.
103 * These top level cmds are used internally while implementing various ops of
104 * digital layer/driver probe or extending the digital framework layer for
105 * features that are not yet implemented there, for example, WTX cmd handling.
107 enum st95hf_cmd_list {
109 CMD_ISO14443A_CONFIG,
110 CMD_ISO14443A_DEMOGAIN,
111 CMD_ISO14443B_DEMOGAIN,
112 CMD_ISO14443A_PROTOCOL_SELECT,
113 CMD_ISO14443B_PROTOCOL_SELECT,
116 CMD_ISO15693_PROTOCOL_SELECT,
119 static const struct cmd cmd_array[] = {
126 [CMD_ISO14443A_CONFIG] = {
128 .cmd_id = WRITE_REGISTER_CMD,
129 .no_cmd_params = 0x4,
130 .cmd_params = {0x3A, 0x00, 0x5A, 0x04},
133 [CMD_ISO14443A_DEMOGAIN] = {
135 .cmd_id = WRITE_REGISTER_CMD,
136 .no_cmd_params = 0x4,
137 .cmd_params = {0x68, 0x01, 0x01, 0xDF},
140 [CMD_ISO14443B_DEMOGAIN] = {
142 .cmd_id = WRITE_REGISTER_CMD,
143 .no_cmd_params = 0x4,
144 .cmd_params = {0x68, 0x01, 0x01, 0x51},
147 [CMD_ISO14443A_PROTOCOL_SELECT] = {
149 .cmd_id = PROTOCOL_SELECT_CMD,
150 .no_cmd_params = 0x4,
151 .cmd_params = {ISO14443A_PROTOCOL_CODE, 0x00, 0x01, 0xA0},
154 [CMD_ISO14443B_PROTOCOL_SELECT] = {
156 .cmd_id = PROTOCOL_SELECT_CMD,
157 .no_cmd_params = 0x4,
158 .cmd_params = {ISO14443B_PROTOCOL_CODE, 0x01, 0x03, 0xFF},
161 [CMD_WTX_RESPONSE] = {
163 .cmd_id = SEND_RECEIVE_CMD,
164 .no_cmd_params = 0x3,
165 .cmd_params = {0xF2, 0x00, TRFLAG_NFCA_STD_FRAME_CRC},
170 .cmd_id = PROTOCOL_SELECT_CMD,
171 .no_cmd_params = 0x2,
172 .cmd_params = {0x0, 0x0},
175 [CMD_ISO15693_PROTOCOL_SELECT] = {
177 .cmd_id = PROTOCOL_SELECT_CMD,
178 .no_cmd_params = 0x2,
179 .cmd_params = {ISO15693_PROTOCOL_CODE, 0x0D},
184 /* st95_digital_cmd_complete_arg stores client context */
185 struct st95_digital_cmd_complete_arg {
186 struct sk_buff *skb_resp;
187 nfc_digital_cmd_complete_t complete_cb;
193 * structure containing ST95HF driver specific data.
194 * @spicontext: structure containing information required
195 * for spi communication between st95hf and host.
196 * @ddev: nfc digital device object.
197 * @nfcdev: nfc device object.
198 * @enable_gpiod: gpio used to enable st95hf transceiver.
199 * @complete_cb_arg: structure to store various context information
200 * that is passed from nfc requesting thread to the threaded ISR.
201 * @st95hf_supply: regulator "consumer" for NFC device.
202 * @sendrcv_trflag: last byte of frame send by sendrecv command
203 * of st95hf. This byte contains transmission flag info.
204 * @exchange_lock: semaphore used for signaling the st95hf_remove
205 * function that the last outstanding async nfc request is finished.
206 * @rm_lock: mutex for ensuring safe access of nfc digital object
207 * from threaded ISR. Usage of this mutex avoids any race between
208 * deletion of the object from st95hf_remove() and its access from
210 * @nfcdev_free: flag to have the state of nfc device object.
212 * @current_protocol: current nfc protocol.
213 * @current_rf_tech: current rf technology.
214 * @fwi: frame waiting index, received in reply of RATS according to
217 struct st95hf_context {
218 struct st95hf_spi_context spicontext;
219 struct nfc_digital_dev *ddev;
220 struct nfc_dev *nfcdev;
221 struct gpio_desc *enable_gpiod;
222 struct st95_digital_cmd_complete_arg complete_cb_arg;
223 struct regulator *st95hf_supply;
224 unsigned char sendrcv_trflag;
225 struct semaphore exchange_lock;
226 struct mutex rm_lock;
234 * st95hf_send_recv_cmd() is for sending commands to ST95HF
235 * that are described in the cmd_array[]. It can optionally
236 * receive the response if the cmd request is of type
237 * SYNC. For that to happen caller must pass true to recv_res.
238 * For ASYNC request, recv_res is ignored and the
239 * function will never try to receive the response on behalf
242 static int st95hf_send_recv_cmd(struct st95hf_context *st95context,
243 enum st95hf_cmd_list cmd,
245 struct param_list *list_array,
248 unsigned char spi_cmd_buffer[MAX_CMD_LEN];
250 struct device *dev = &st95context->spicontext.spidev->dev;
252 if (cmd_array[cmd].cmd_len > MAX_CMD_LEN)
254 if (cmd_array[cmd].no_cmd_params < no_modif)
256 if (no_modif && !list_array)
259 spi_cmd_buffer[0] = ST95HF_COMMAND_SEND;
260 spi_cmd_buffer[1] = cmd_array[cmd].cmd_id;
261 spi_cmd_buffer[2] = cmd_array[cmd].no_cmd_params;
263 memcpy(&spi_cmd_buffer[3], cmd_array[cmd].cmd_params,
266 for (i = 0; i < no_modif; i++) {
267 if (list_array[i].param_offset >= cmd_array[cmd].no_cmd_params)
269 spi_cmd_buffer[3 + list_array[i].param_offset] =
270 list_array[i].new_param_val;
273 ret = st95hf_spi_send(&st95context->spicontext,
275 cmd_array[cmd].cmd_len,
278 dev_err(dev, "st95hf_spi_send failed with error %d\n", ret);
282 if (cmd_array[cmd].req == SYNC && recv_res) {
283 unsigned char st95hf_response_arr[2];
285 ret = st95hf_spi_recv_response(&st95context->spicontext,
286 st95hf_response_arr);
288 dev_err(dev, "spi error from st95hf_spi_recv_response(), err = 0x%x\n",
293 if (st95hf_response_arr[0]) {
294 dev_err(dev, "st95hf error from st95hf_spi_recv_response(), err = 0x%x\n",
295 st95hf_response_arr[0]);
303 static int st95hf_echo_command(struct st95hf_context *st95context)
306 unsigned char echo_response;
308 result = st95hf_send_recv_cmd(st95context, CMD_ECHO, 0, NULL, false);
312 /* If control reached here, response can be taken */
313 result = st95hf_spi_recv_echo_res(&st95context->spicontext,
316 dev_err(&st95context->spicontext.spidev->dev,
317 "err: echo response receive error = 0x%x\n", result);
321 if (echo_response == ECHORESPONSE)
324 dev_err(&st95context->spicontext.spidev->dev, "err: echo res is 0x%x\n",
330 static int secondary_configuration_type4a(struct st95hf_context *stcontext)
333 struct device *dev = &stcontext->nfcdev->dev;
335 /* 14443A config setting after select protocol */
336 result = st95hf_send_recv_cmd(stcontext,
337 CMD_ISO14443A_CONFIG,
342 dev_err(dev, "type a config cmd, err = 0x%x\n", result);
346 /* 14443A demo gain setting */
347 result = st95hf_send_recv_cmd(stcontext,
348 CMD_ISO14443A_DEMOGAIN,
353 dev_err(dev, "type a demogain cmd, err = 0x%x\n", result);
358 static int secondary_configuration_type4b(struct st95hf_context *stcontext)
361 struct device *dev = &stcontext->nfcdev->dev;
363 result = st95hf_send_recv_cmd(stcontext,
364 CMD_ISO14443B_DEMOGAIN,
369 dev_err(dev, "type b demogain cmd, err = 0x%x\n", result);
374 static int st95hf_select_protocol(struct st95hf_context *stcontext, int type)
379 dev = &stcontext->nfcdev->dev;
382 case NFC_DIGITAL_RF_TECH_106A:
383 stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_106A;
384 result = st95hf_send_recv_cmd(stcontext,
385 CMD_ISO14443A_PROTOCOL_SELECT,
390 dev_err(dev, "protocol sel, err = 0x%x\n",
395 /* secondary config. for 14443Type 4A after protocol select */
396 result = secondary_configuration_type4a(stcontext);
398 dev_err(dev, "type a secondary config, err = 0x%x\n",
403 case NFC_DIGITAL_RF_TECH_106B:
404 stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_106B;
405 result = st95hf_send_recv_cmd(stcontext,
406 CMD_ISO14443B_PROTOCOL_SELECT,
411 dev_err(dev, "protocol sel send, err = 0x%x\n",
417 * delay of 5-6 ms is required after select protocol
418 * command in case of ISO14443 Type B
420 usleep_range(50000, 60000);
422 /* secondary config. for 14443Type 4B after protocol select */
423 result = secondary_configuration_type4b(stcontext);
425 dev_err(dev, "type b secondary config, err = 0x%x\n",
430 case NFC_DIGITAL_RF_TECH_ISO15693:
431 stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_ISO15693;
432 result = st95hf_send_recv_cmd(stcontext,
433 CMD_ISO15693_PROTOCOL_SELECT,
438 dev_err(dev, "protocol sel send, err = 0x%x\n",
450 static void st95hf_send_st95enable_negativepulse(struct st95hf_context *st95con)
452 /* First make irq_in pin high */
453 gpiod_set_value(st95con->enable_gpiod, HIGH);
455 /* wait for 1 milisecond */
456 usleep_range(1000, 2000);
458 /* Make irq_in pin low */
459 gpiod_set_value(st95con->enable_gpiod, LOW);
461 /* wait for minimum interrupt pulse to make st95 active */
462 usleep_range(1000, 2000);
464 /* At end make it high */
465 gpiod_set_value(st95con->enable_gpiod, HIGH);
469 * Send a reset sequence over SPI bus (Reset command + wait 3ms +
470 * negative pulse on st95hf enable gpio
472 static int st95hf_send_spi_reset_sequence(struct st95hf_context *st95context)
475 unsigned char reset_cmd = ST95HF_COMMAND_RESET;
477 result = st95hf_spi_send(&st95context->spicontext,
479 ST95HF_RESET_CMD_LEN,
482 dev_err(&st95context->spicontext.spidev->dev,
483 "spi reset sequence cmd error = %d", result);
487 /* wait for 3 milisecond to complete the controller reset process */
488 usleep_range(3000, 4000);
490 /* send negative pulse to make st95hf active */
491 st95hf_send_st95enable_negativepulse(st95context);
493 /* wait for 10 milisecond : HFO setup time */
494 usleep_range(10000, 20000);
499 static int st95hf_por_sequence(struct st95hf_context *st95context)
504 st95hf_send_st95enable_negativepulse(st95context);
506 usleep_range(5000, 6000);
508 /* send an ECHO command and checks ST95HF response */
509 result = st95hf_echo_command(st95context);
511 dev_dbg(&st95context->spicontext.spidev->dev,
512 "response from echo function = 0x%x, attempt = %d\n",
513 result, nth_attempt);
518 /* send an pulse on IRQ in case of the chip is on sleep state */
519 if (nth_attempt == 2)
520 st95hf_send_st95enable_negativepulse(st95context);
522 st95hf_send_spi_reset_sequence(st95context);
524 /* delay of 50 milisecond */
525 usleep_range(50000, 51000);
526 } while (nth_attempt++ < 3);
531 static int iso14443_config_fdt(struct st95hf_context *st95context, int wtxm)
534 struct device *dev = &st95context->spicontext.spidev->dev;
535 struct nfc_digital_dev *nfcddev = st95context->ddev;
536 unsigned char pp_typeb;
537 struct param_list new_params[2];
539 pp_typeb = cmd_array[CMD_ISO14443B_PROTOCOL_SELECT].cmd_params[2];
541 if (nfcddev->curr_protocol == NFC_PROTO_ISO14443 &&
542 st95context->fwi < 4)
543 st95context->fwi = 4;
545 new_params[0].param_offset = 2;
546 if (nfcddev->curr_protocol == NFC_PROTO_ISO14443)
547 new_params[0].new_param_val = st95context->fwi;
548 else if (nfcddev->curr_protocol == NFC_PROTO_ISO14443_B)
549 new_params[0].new_param_val = pp_typeb;
551 new_params[1].param_offset = 3;
552 new_params[1].new_param_val = wtxm;
554 switch (nfcddev->curr_protocol) {
555 case NFC_PROTO_ISO14443:
556 result = st95hf_send_recv_cmd(st95context,
557 CMD_ISO14443A_PROTOCOL_SELECT,
562 dev_err(dev, "WTX type a sel proto, err = 0x%x\n",
567 /* secondary config. for 14443Type 4A after protocol select */
568 result = secondary_configuration_type4a(st95context);
570 dev_err(dev, "WTX type a second. config, err = 0x%x\n",
575 case NFC_PROTO_ISO14443_B:
576 result = st95hf_send_recv_cmd(st95context,
577 CMD_ISO14443B_PROTOCOL_SELECT,
582 dev_err(dev, "WTX type b sel proto, err = 0x%x\n",
587 /* secondary config. for 14443Type 4B after protocol select */
588 result = secondary_configuration_type4b(st95context);
590 dev_err(dev, "WTX type b second. config, err = 0x%x\n",
602 static int st95hf_handle_wtx(struct st95hf_context *stcontext,
607 unsigned char val_mm = 0;
608 struct param_list new_params[1];
609 struct nfc_digital_dev *nfcddev = stcontext->ddev;
610 struct device *dev = &stcontext->nfcdev->dev;
613 result = iso14443_config_fdt(stcontext, wtx_val & 0x3f);
615 dev_err(dev, "Config. setting error on WTX req, err = 0x%x\n",
620 /* Send response of wtx with ASYNC as no response expected */
621 new_params[0].param_offset = 1;
622 new_params[0].new_param_val = wtx_val;
624 result = st95hf_send_recv_cmd(stcontext,
630 dev_err(dev, "WTX response send, err = 0x%x\n", result);
634 /* if no new wtx, cofigure with default values */
635 if (nfcddev->curr_protocol == NFC_PROTO_ISO14443)
636 val_mm = cmd_array[CMD_ISO14443A_PROTOCOL_SELECT].cmd_params[3];
637 else if (nfcddev->curr_protocol == NFC_PROTO_ISO14443_B)
638 val_mm = cmd_array[CMD_ISO14443B_PROTOCOL_SELECT].cmd_params[3];
640 result = iso14443_config_fdt(stcontext, val_mm);
642 dev_err(dev, "Default config. setting error after WTX processing, err = 0x%x\n",
648 static int st95hf_error_handling(struct st95hf_context *stcontext,
649 struct sk_buff *skb_resp,
653 unsigned char error_byte;
654 struct device *dev = &stcontext->nfcdev->dev;
656 /* First check ST95HF specific error */
657 if (skb_resp->data[0] & ST95HF_ERR_MASK) {
658 if (skb_resp->data[0] == ST95HF_TIMEOUT_ERROR)
665 /* Check for CRC err only if CRC is present in the tag response */
666 switch (stcontext->current_rf_tech) {
667 case NFC_DIGITAL_RF_TECH_106A:
668 if (stcontext->sendrcv_trflag == TRFLAG_NFCA_STD_FRAME_CRC) {
669 error_byte = skb_resp->data[res_len - 3];
670 if (error_byte & ST95HF_NFCA_CRC_ERR_MASK) {
671 /* CRC error occurred */
672 dev_err(dev, "CRC error, byte received = 0x%x\n",
678 case NFC_DIGITAL_RF_TECH_106B:
679 case NFC_DIGITAL_RF_TECH_ISO15693:
680 error_byte = skb_resp->data[res_len - 1];
681 if (error_byte & ST95HF_NFCB_CRC_ERR_MASK) {
682 /* CRC error occurred */
683 dev_err(dev, "CRC error, byte received = 0x%x\n",
693 static int st95hf_response_handler(struct st95hf_context *stcontext,
694 struct sk_buff *skb_resp,
699 unsigned char val_mm;
700 struct nfc_digital_dev *nfcddev = stcontext->ddev;
701 struct device *dev = &stcontext->nfcdev->dev;
702 struct st95_digital_cmd_complete_arg *cb_arg;
704 cb_arg = &stcontext->complete_cb_arg;
706 /* Process the response */
707 skb_put(skb_resp, res_len);
709 /* Remove st95 header */
710 skb_pull(skb_resp, 2);
712 skb_len = skb_resp->len;
714 /* check if it is case of RATS request reply & FWI is present */
715 if (nfcddev->curr_protocol == NFC_PROTO_ISO14443 && cb_arg->rats &&
716 (skb_resp->data[1] & RATS_TB1_PRESENT_MASK)) {
717 if (skb_resp->data[1] & RATS_TA1_PRESENT_MASK)
719 (skb_resp->data[3] & TB1_FWI_MASK) >> 4;
722 (skb_resp->data[2] & TB1_FWI_MASK) >> 4;
724 val_mm = cmd_array[CMD_ISO14443A_PROTOCOL_SELECT].cmd_params[3];
726 result = iso14443_config_fdt(stcontext, val_mm);
728 dev_err(dev, "error in config_fdt to handle fwi of ATS, error=%d\n",
733 cb_arg->rats = false;
735 /* Remove CRC bytes only if received frames data has an eod (CRC) */
736 switch (stcontext->current_rf_tech) {
737 case NFC_DIGITAL_RF_TECH_106A:
738 if (stcontext->sendrcv_trflag == TRFLAG_NFCA_STD_FRAME_CRC)
739 skb_trim(skb_resp, (skb_len - 5));
741 skb_trim(skb_resp, (skb_len - 3));
743 case NFC_DIGITAL_RF_TECH_106B:
744 case NFC_DIGITAL_RF_TECH_ISO15693:
745 skb_trim(skb_resp, (skb_len - 3));
752 static irqreturn_t st95hf_irq_handler(int irq, void *st95hfcontext)
754 struct st95hf_context *stcontext =
755 (struct st95hf_context *)st95hfcontext;
757 if (stcontext->spicontext.req_issync) {
758 complete(&stcontext->spicontext.done);
759 stcontext->spicontext.req_issync = false;
763 return IRQ_WAKE_THREAD;
766 static irqreturn_t st95hf_irq_thread_handler(int irq, void *st95hfcontext)
771 struct device *spidevice;
772 struct sk_buff *skb_resp;
773 struct st95hf_context *stcontext =
774 (struct st95hf_context *)st95hfcontext;
775 struct st95_digital_cmd_complete_arg *cb_arg;
777 spidevice = &stcontext->spicontext.spidev->dev;
780 * check semaphore, if not down() already, then we don't
781 * know in which context the ISR is called and surely it
782 * will be a bug. Note that down() of the semaphore is done
783 * in the corresponding st95hf_in_send_cmd() and then
784 * only this ISR should be called. ISR will up() the
785 * semaphore before leaving. Hence when the ISR is called
786 * the correct behaviour is down_trylock() should always
787 * return 1 (indicating semaphore cant be taken and hence no
788 * change in semaphore count).
789 * If not, then we up() the semaphore and crash on
792 if (!down_trylock(&stcontext->exchange_lock)) {
793 up(&stcontext->exchange_lock);
794 WARN(1, "unknown context in ST95HF ISR");
798 cb_arg = &stcontext->complete_cb_arg;
799 skb_resp = cb_arg->skb_resp;
801 mutex_lock(&stcontext->rm_lock);
802 res_len = st95hf_spi_recv_response(&stcontext->spicontext,
805 dev_err(spidevice, "TISR spi response err = 0x%x\n", res_len);
810 /* if stcontext->nfcdev_free is true, it means remove already ran */
811 if (stcontext->nfcdev_free) {
816 if (skb_resp->data[2] == WTX_REQ_FROM_TAG) {
817 /* Request for new FWT from tag */
818 result = st95hf_handle_wtx(stcontext, true, skb_resp->data[3]);
823 mutex_unlock(&stcontext->rm_lock);
827 result = st95hf_error_handling(stcontext, skb_resp, res_len);
831 result = st95hf_response_handler(stcontext, skb_resp, res_len);
836 * If select protocol is done on wtx req. do select protocol
837 * again with default values
841 result = st95hf_handle_wtx(stcontext, false, 0);
846 /* call digital layer callback */
847 cb_arg->complete_cb(stcontext->ddev, cb_arg->cb_usrarg, skb_resp);
849 /* up the semaphore before returning */
850 up(&stcontext->exchange_lock);
851 mutex_unlock(&stcontext->rm_lock);
858 cb_arg->rats = false;
859 skb_resp = ERR_PTR(result);
860 /* call of callback with error */
861 cb_arg->complete_cb(stcontext->ddev, cb_arg->cb_usrarg, skb_resp);
862 /* up the semaphore before returning */
863 up(&stcontext->exchange_lock);
864 mutex_unlock(&stcontext->rm_lock);
868 /* NFC ops functions definition */
869 static int st95hf_in_configure_hw(struct nfc_digital_dev *ddev,
873 struct st95hf_context *stcontext = nfc_digital_get_drvdata(ddev);
875 if (type == NFC_DIGITAL_CONFIG_RF_TECH)
876 return st95hf_select_protocol(stcontext, param);
878 if (type == NFC_DIGITAL_CONFIG_FRAMING) {
880 case NFC_DIGITAL_FRAMING_NFCA_SHORT:
881 stcontext->sendrcv_trflag = TRFLAG_NFCA_SHORT_FRAME;
883 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
884 stcontext->sendrcv_trflag = TRFLAG_NFCA_STD_FRAME;
886 case NFC_DIGITAL_FRAMING_NFCA_T4T:
887 case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
888 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
889 stcontext->sendrcv_trflag = TRFLAG_NFCA_STD_FRAME_CRC;
891 case NFC_DIGITAL_FRAMING_NFCB:
892 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY:
893 case NFC_DIGITAL_FRAMING_ISO15693_T5T:
901 static int rf_off(struct st95hf_context *stcontext)
906 dev = &stcontext->nfcdev->dev;
908 rc = st95hf_send_recv_cmd(stcontext, CMD_FIELD_OFF, 0, NULL, true);
910 dev_err(dev, "protocol sel send field off, err = 0x%x\n", rc);
915 static int st95hf_in_send_cmd(struct nfc_digital_dev *ddev,
918 nfc_digital_cmd_complete_t cb,
921 struct st95hf_context *stcontext = nfc_digital_get_drvdata(ddev);
923 struct sk_buff *skb_resp;
924 int len_data_to_tag = 0;
926 skb_resp = nfc_alloc_recv_skb(MAX_RESPONSE_BUFFER_SIZE, GFP_KERNEL);
930 switch (stcontext->current_rf_tech) {
931 case NFC_DIGITAL_RF_TECH_106A:
932 len_data_to_tag = skb->len + 1;
933 skb_put_u8(skb, stcontext->sendrcv_trflag);
935 case NFC_DIGITAL_RF_TECH_106B:
936 case NFC_DIGITAL_RF_TECH_ISO15693:
937 len_data_to_tag = skb->len;
945 skb->data[0] = ST95HF_COMMAND_SEND;
946 skb->data[1] = SEND_RECEIVE_CMD;
947 skb->data[2] = len_data_to_tag;
949 stcontext->complete_cb_arg.skb_resp = skb_resp;
950 stcontext->complete_cb_arg.cb_usrarg = arg;
951 stcontext->complete_cb_arg.complete_cb = cb;
953 if ((skb->data[3] == ISO14443A_RATS_REQ) &&
954 ddev->curr_protocol == NFC_PROTO_ISO14443)
955 stcontext->complete_cb_arg.rats = true;
958 * down the semaphore to indicate to remove func that an
959 * ISR is pending, note that it will not block here in any case.
960 * If found blocked, it is a BUG!
962 rc = down_killable(&stcontext->exchange_lock);
964 WARN(1, "Semaphore is not found up in st95hf_in_send_cmd\n");
968 rc = st95hf_spi_send(&stcontext->spicontext, skb->data,
972 dev_err(&stcontext->nfcdev->dev,
973 "Error %d trying to perform data_exchange", rc);
974 /* up the semaphore since ISR will never come in this case */
975 up(&stcontext->exchange_lock);
988 /* p2p will be supported in a later release ! */
989 static int st95hf_tg_configure_hw(struct nfc_digital_dev *ddev,
996 static int st95hf_tg_send_cmd(struct nfc_digital_dev *ddev,
999 nfc_digital_cmd_complete_t cb,
1005 static int st95hf_tg_listen(struct nfc_digital_dev *ddev,
1007 nfc_digital_cmd_complete_t cb,
1013 static int st95hf_tg_get_rf_tech(struct nfc_digital_dev *ddev, u8 *rf_tech)
1018 static int st95hf_switch_rf(struct nfc_digital_dev *ddev, bool on)
1021 struct st95hf_context *stcontext = nfc_digital_get_drvdata(ddev);
1023 rf_tech = ddev->curr_rf_tech;
1026 /* switch on RF field */
1027 return st95hf_select_protocol(stcontext, rf_tech);
1029 /* switch OFF RF field */
1030 return rf_off(stcontext);
1033 /* TODO st95hf_abort_cmd */
1034 static void st95hf_abort_cmd(struct nfc_digital_dev *ddev)
1038 static const struct nfc_digital_ops st95hf_nfc_digital_ops = {
1039 .in_configure_hw = st95hf_in_configure_hw,
1040 .in_send_cmd = st95hf_in_send_cmd,
1042 .tg_listen = st95hf_tg_listen,
1043 .tg_configure_hw = st95hf_tg_configure_hw,
1044 .tg_send_cmd = st95hf_tg_send_cmd,
1045 .tg_get_rf_tech = st95hf_tg_get_rf_tech,
1047 .switch_rf = st95hf_switch_rf,
1048 .abort_cmd = st95hf_abort_cmd,
1051 static const struct spi_device_id st95hf_id[] = {
1055 MODULE_DEVICE_TABLE(spi, st95hf_id);
1057 static const struct of_device_id st95hf_spi_of_match[] __maybe_unused = {
1058 { .compatible = "st,st95hf" },
1061 MODULE_DEVICE_TABLE(of, st95hf_spi_of_match);
1063 static int st95hf_probe(struct spi_device *nfc_spi_dev)
1065 struct device *dev = &nfc_spi_dev->dev;
1068 struct st95hf_context *st95context;
1069 struct st95hf_spi_context *spicontext;
1071 nfc_info(&nfc_spi_dev->dev, "ST95HF driver probe called.\n");
1073 st95context = devm_kzalloc(&nfc_spi_dev->dev,
1074 sizeof(struct st95hf_context),
1079 spicontext = &st95context->spicontext;
1081 spicontext->spidev = nfc_spi_dev;
1084 cmd_array[CMD_ISO14443A_PROTOCOL_SELECT].cmd_params[2];
1086 if (device_property_present(&nfc_spi_dev->dev, "st95hfvin")) {
1087 st95context->st95hf_supply =
1088 devm_regulator_get(&nfc_spi_dev->dev,
1090 if (IS_ERR(st95context->st95hf_supply)) {
1091 dev_err(&nfc_spi_dev->dev, "failed to acquire regulator\n");
1092 return PTR_ERR(st95context->st95hf_supply);
1095 ret = regulator_enable(st95context->st95hf_supply);
1097 dev_err(&nfc_spi_dev->dev, "failed to enable regulator\n");
1102 init_completion(&spicontext->done);
1103 mutex_init(&spicontext->spi_lock);
1106 * Store spicontext in spi device object for using it in
1109 dev_set_drvdata(&nfc_spi_dev->dev, spicontext);
1111 st95context->enable_gpiod = devm_gpiod_get(dev, "enable", GPIOD_OUT_HIGH);
1112 if (IS_ERR(st95context->enable_gpiod)) {
1113 ret = PTR_ERR(st95context->enable_gpiod);
1114 dev_err(&nfc_spi_dev->dev, "No valid enable gpio\n");
1115 goto err_disable_regulator;
1118 ret = gpiod_set_consumer_name(st95context->enable_gpiod, "enable_gpio");
1120 goto err_disable_regulator;
1122 if (nfc_spi_dev->irq > 0) {
1123 if (devm_request_threaded_irq(&nfc_spi_dev->dev,
1126 st95hf_irq_thread_handler,
1127 IRQF_TRIGGER_FALLING,
1129 (void *)st95context) < 0) {
1130 dev_err(&nfc_spi_dev->dev, "err: irq request for st95hf is failed\n");
1132 goto err_disable_regulator;
1135 dev_err(&nfc_spi_dev->dev, "not a valid IRQ associated with ST95HF\n");
1137 goto err_disable_regulator;
1141 * First reset SPI to handle warm reset of the system.
1142 * It will put the ST95HF device in Power ON state
1143 * which make the state of device identical to state
1144 * at the time of cold reset of the system.
1146 ret = st95hf_send_spi_reset_sequence(st95context);
1148 dev_err(&nfc_spi_dev->dev, "err: spi_reset_sequence failed\n");
1149 goto err_disable_regulator;
1152 /* call PowerOnReset sequence of ST95hf to activate it */
1153 ret = st95hf_por_sequence(st95context);
1155 dev_err(&nfc_spi_dev->dev, "err: por seq failed for st95hf\n");
1156 goto err_disable_regulator;
1159 /* create NFC dev object and register with NFC Subsystem */
1160 st95context->ddev = nfc_digital_allocate_device(&st95hf_nfc_digital_ops,
1161 ST95HF_SUPPORTED_PROT,
1162 ST95HF_CAPABILITIES,
1163 ST95HF_HEADROOM_LEN,
1164 ST95HF_TAILROOM_LEN);
1165 if (!st95context->ddev) {
1167 goto err_disable_regulator;
1170 st95context->nfcdev = st95context->ddev->nfc_dev;
1171 nfc_digital_set_parent_dev(st95context->ddev, &nfc_spi_dev->dev);
1173 ret = nfc_digital_register_device(st95context->ddev);
1175 dev_err(&st95context->nfcdev->dev, "st95hf registration failed\n");
1176 goto err_free_digital_device;
1179 /* store st95context in nfc device object */
1180 nfc_digital_set_drvdata(st95context->ddev, st95context);
1182 sema_init(&st95context->exchange_lock, 1);
1183 mutex_init(&st95context->rm_lock);
1187 err_free_digital_device:
1188 nfc_digital_free_device(st95context->ddev);
1189 err_disable_regulator:
1190 if (st95context->st95hf_supply)
1191 regulator_disable(st95context->st95hf_supply);
1196 static void st95hf_remove(struct spi_device *nfc_spi_dev)
1199 unsigned char reset_cmd = ST95HF_COMMAND_RESET;
1200 struct st95hf_spi_context *spictx = dev_get_drvdata(&nfc_spi_dev->dev);
1202 struct st95hf_context *stcontext = container_of(spictx,
1203 struct st95hf_context,
1206 mutex_lock(&stcontext->rm_lock);
1208 nfc_digital_unregister_device(stcontext->ddev);
1209 nfc_digital_free_device(stcontext->ddev);
1210 stcontext->nfcdev_free = true;
1212 mutex_unlock(&stcontext->rm_lock);
1214 /* if last in_send_cmd's ISR is pending, wait for it to finish */
1215 result = down_killable(&stcontext->exchange_lock);
1216 if (result == -EINTR)
1217 dev_err(&spictx->spidev->dev, "sleep for semaphore interrupted by signal\n");
1219 /* next reset the ST95HF controller */
1220 result = st95hf_spi_send(&stcontext->spicontext,
1222 ST95HF_RESET_CMD_LEN,
1225 dev_err(&spictx->spidev->dev,
1226 "ST95HF reset failed in remove() err = %d\n", result);
1228 /* wait for 3 ms to complete the controller reset process */
1229 usleep_range(3000, 4000);
1231 /* disable regulator */
1232 if (stcontext->st95hf_supply)
1233 regulator_disable(stcontext->st95hf_supply);
1236 /* Register as SPI protocol driver */
1237 static struct spi_driver st95hf_driver = {
1240 .of_match_table = of_match_ptr(st95hf_spi_of_match),
1242 .id_table = st95hf_id,
1243 .probe = st95hf_probe,
1244 .remove = st95hf_remove,
1247 module_spi_driver(st95hf_driver);
1249 MODULE_AUTHOR("Shikha Singh <shikha.singh@st.com>");
1250 MODULE_DESCRIPTION("ST NFC Transceiver ST95HF driver");
1251 MODULE_LICENSE("GPL v2");