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.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>
19 #include <linux/property.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/wait.h>
22 #include <net/nfc/digital.h>
23 #include <net/nfc/nfc.h>
27 /* supported protocols */
28 #define ST95HF_SUPPORTED_PROT (NFC_PROTO_ISO14443_MASK | \
29 NFC_PROTO_ISO14443_B_MASK | \
30 NFC_PROTO_ISO15693_MASK)
31 /* driver capabilities */
32 #define ST95HF_CAPABILITIES NFC_DIGITAL_DRV_CAPS_IN_CRC
34 /* Command Send Interface */
35 /* ST95HF_COMMAND_SEND CMD Ids */
37 #define WRITE_REGISTER_CMD 0x9
38 #define PROTOCOL_SELECT_CMD 0x2
39 #define SEND_RECEIVE_CMD 0x4
41 /* Select protocol codes */
42 #define ISO15693_PROTOCOL_CODE 0x1
43 #define ISO14443A_PROTOCOL_CODE 0x2
44 #define ISO14443B_PROTOCOL_CODE 0x3
48 * 1 byte for control byte
52 #define ST95HF_HEADROOM_LEN 3
55 * tailroom is 1 for ISO14443A
56 * and 0 for ISO14443B/ISO15693,
57 * hence the max value 1 should be
60 #define ST95HF_TAILROOM_LEN 1
62 /* Command Response interface */
63 #define MAX_RESPONSE_BUFFER_SIZE 280
64 #define ECHORESPONSE 0x55
65 #define ST95HF_ERR_MASK 0xF
66 #define ST95HF_TIMEOUT_ERROR 0x87
67 #define ST95HF_NFCA_CRC_ERR_MASK 0x20
68 #define ST95HF_NFCB_CRC_ERR_MASK 0x01
70 /* ST95HF transmission flag values */
71 #define TRFLAG_NFCA_SHORT_FRAME 0x07
72 #define TRFLAG_NFCA_STD_FRAME 0x08
73 #define TRFLAG_NFCA_STD_FRAME_CRC 0x28
78 #define ISO14443A_RATS_REQ 0xE0
79 #define RATS_TB1_PRESENT_MASK 0x20
80 #define RATS_TA1_PRESENT_MASK 0x10
81 #define TB1_FWI_MASK 0xF0
82 #define WTX_REQ_FROM_TAG 0xF2
84 #define MAX_CMD_LEN 0x7
86 #define MAX_CMD_PARAMS 4
90 unsigned char no_cmd_params;
91 unsigned char cmd_params[MAX_CMD_PARAMS];
101 * List of top-level cmds to be used internally by the driver.
102 * All these commands are build on top of ST95HF basic commands
103 * such as SEND_RECEIVE_CMD, PROTOCOL_SELECT_CMD, etc.
104 * These top level cmds are used internally while implementing various ops of
105 * digital layer/driver probe or extending the digital framework layer for
106 * features that are not yet implemented there, for example, WTX cmd handling.
108 enum st95hf_cmd_list {
110 CMD_ISO14443A_CONFIG,
111 CMD_ISO14443A_DEMOGAIN,
112 CMD_ISO14443B_DEMOGAIN,
113 CMD_ISO14443A_PROTOCOL_SELECT,
114 CMD_ISO14443B_PROTOCOL_SELECT,
117 CMD_ISO15693_PROTOCOL_SELECT,
120 static const struct cmd cmd_array[] = {
127 [CMD_ISO14443A_CONFIG] = {
129 .cmd_id = WRITE_REGISTER_CMD,
130 .no_cmd_params = 0x4,
131 .cmd_params = {0x3A, 0x00, 0x5A, 0x04},
134 [CMD_ISO14443A_DEMOGAIN] = {
136 .cmd_id = WRITE_REGISTER_CMD,
137 .no_cmd_params = 0x4,
138 .cmd_params = {0x68, 0x01, 0x01, 0xDF},
141 [CMD_ISO14443B_DEMOGAIN] = {
143 .cmd_id = WRITE_REGISTER_CMD,
144 .no_cmd_params = 0x4,
145 .cmd_params = {0x68, 0x01, 0x01, 0x51},
148 [CMD_ISO14443A_PROTOCOL_SELECT] = {
150 .cmd_id = PROTOCOL_SELECT_CMD,
151 .no_cmd_params = 0x4,
152 .cmd_params = {ISO14443A_PROTOCOL_CODE, 0x00, 0x01, 0xA0},
155 [CMD_ISO14443B_PROTOCOL_SELECT] = {
157 .cmd_id = PROTOCOL_SELECT_CMD,
158 .no_cmd_params = 0x4,
159 .cmd_params = {ISO14443B_PROTOCOL_CODE, 0x01, 0x03, 0xFF},
162 [CMD_WTX_RESPONSE] = {
164 .cmd_id = SEND_RECEIVE_CMD,
165 .no_cmd_params = 0x3,
166 .cmd_params = {0xF2, 0x00, TRFLAG_NFCA_STD_FRAME_CRC},
171 .cmd_id = PROTOCOL_SELECT_CMD,
172 .no_cmd_params = 0x2,
173 .cmd_params = {0x0, 0x0},
176 [CMD_ISO15693_PROTOCOL_SELECT] = {
178 .cmd_id = PROTOCOL_SELECT_CMD,
179 .no_cmd_params = 0x2,
180 .cmd_params = {ISO15693_PROTOCOL_CODE, 0x0D},
185 /* st95_digital_cmd_complete_arg stores client context */
186 struct st95_digital_cmd_complete_arg {
187 struct sk_buff *skb_resp;
188 nfc_digital_cmd_complete_t complete_cb;
194 * structure containing ST95HF driver specific data.
195 * @spicontext: structure containing information required
196 * for spi communication between st95hf and host.
197 * @ddev: nfc digital device object.
198 * @nfcdev: nfc device object.
199 * @enable_gpio: gpio used to enable st95hf transceiver.
200 * @complete_cb_arg: structure to store various context information
201 * that is passed from nfc requesting thread to the threaded ISR.
202 * @st95hf_supply: regulator "consumer" for NFC device.
203 * @sendrcv_trflag: last byte of frame send by sendrecv command
204 * of st95hf. This byte contains transmission flag info.
205 * @exchange_lock: semaphore used for signaling the st95hf_remove
206 * function that the last outstanding async nfc request is finished.
207 * @rm_lock: mutex for ensuring safe access of nfc digital object
208 * from threaded ISR. Usage of this mutex avoids any race between
209 * deletion of the object from st95hf_remove() and its access from
211 * @nfcdev_free: flag to have the state of nfc device object.
213 * @current_protocol: current nfc protocol.
214 * @current_rf_tech: current rf technology.
215 * @fwi: frame waiting index, received in reply of RATS according to
218 struct st95hf_context {
219 struct st95hf_spi_context spicontext;
220 struct nfc_digital_dev *ddev;
221 struct nfc_dev *nfcdev;
222 unsigned int enable_gpio;
223 struct st95_digital_cmd_complete_arg complete_cb_arg;
224 struct regulator *st95hf_supply;
225 unsigned char sendrcv_trflag;
226 struct semaphore exchange_lock;
227 struct mutex rm_lock;
235 * st95hf_send_recv_cmd() is for sending commands to ST95HF
236 * that are described in the cmd_array[]. It can optionally
237 * receive the response if the cmd request is of type
238 * SYNC. For that to happen caller must pass true to recv_res.
239 * For ASYNC request, recv_res is ignored and the
240 * function will never try to receive the response on behalf
243 static int st95hf_send_recv_cmd(struct st95hf_context *st95context,
244 enum st95hf_cmd_list cmd,
246 struct param_list *list_array,
249 unsigned char spi_cmd_buffer[MAX_CMD_LEN];
251 struct device *dev = &st95context->spicontext.spidev->dev;
253 if (cmd_array[cmd].cmd_len > MAX_CMD_LEN)
255 if (cmd_array[cmd].no_cmd_params < no_modif)
257 if (no_modif && !list_array)
260 spi_cmd_buffer[0] = ST95HF_COMMAND_SEND;
261 spi_cmd_buffer[1] = cmd_array[cmd].cmd_id;
262 spi_cmd_buffer[2] = cmd_array[cmd].no_cmd_params;
264 memcpy(&spi_cmd_buffer[3], cmd_array[cmd].cmd_params,
267 for (i = 0; i < no_modif; i++) {
268 if (list_array[i].param_offset >= cmd_array[cmd].no_cmd_params)
270 spi_cmd_buffer[3 + list_array[i].param_offset] =
271 list_array[i].new_param_val;
274 ret = st95hf_spi_send(&st95context->spicontext,
276 cmd_array[cmd].cmd_len,
279 dev_err(dev, "st95hf_spi_send failed with error %d\n", ret);
283 if (cmd_array[cmd].req == SYNC && recv_res) {
284 unsigned char st95hf_response_arr[2];
286 ret = st95hf_spi_recv_response(&st95context->spicontext,
287 st95hf_response_arr);
289 dev_err(dev, "spi error from st95hf_spi_recv_response(), err = 0x%x\n",
294 if (st95hf_response_arr[0]) {
295 dev_err(dev, "st95hf error from st95hf_spi_recv_response(), err = 0x%x\n",
296 st95hf_response_arr[0]);
304 static int st95hf_echo_command(struct st95hf_context *st95context)
307 unsigned char echo_response;
309 result = st95hf_send_recv_cmd(st95context, CMD_ECHO, 0, NULL, false);
313 /* If control reached here, response can be taken */
314 result = st95hf_spi_recv_echo_res(&st95context->spicontext,
317 dev_err(&st95context->spicontext.spidev->dev,
318 "err: echo response receive error = 0x%x\n", result);
322 if (echo_response == ECHORESPONSE)
325 dev_err(&st95context->spicontext.spidev->dev, "err: echo res is 0x%x\n",
331 static int secondary_configuration_type4a(struct st95hf_context *stcontext)
334 struct device *dev = &stcontext->nfcdev->dev;
336 /* 14443A config setting after select protocol */
337 result = st95hf_send_recv_cmd(stcontext,
338 CMD_ISO14443A_CONFIG,
343 dev_err(dev, "type a config cmd, err = 0x%x\n", result);
347 /* 14443A demo gain setting */
348 result = st95hf_send_recv_cmd(stcontext,
349 CMD_ISO14443A_DEMOGAIN,
354 dev_err(dev, "type a demogain cmd, err = 0x%x\n", result);
359 static int secondary_configuration_type4b(struct st95hf_context *stcontext)
362 struct device *dev = &stcontext->nfcdev->dev;
364 result = st95hf_send_recv_cmd(stcontext,
365 CMD_ISO14443B_DEMOGAIN,
370 dev_err(dev, "type b demogain cmd, err = 0x%x\n", result);
375 static int st95hf_select_protocol(struct st95hf_context *stcontext, int type)
380 dev = &stcontext->nfcdev->dev;
383 case NFC_DIGITAL_RF_TECH_106A:
384 stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_106A;
385 result = st95hf_send_recv_cmd(stcontext,
386 CMD_ISO14443A_PROTOCOL_SELECT,
391 dev_err(dev, "protocol sel, err = 0x%x\n",
396 /* secondary config. for 14443Type 4A after protocol select */
397 result = secondary_configuration_type4a(stcontext);
399 dev_err(dev, "type a secondary config, err = 0x%x\n",
404 case NFC_DIGITAL_RF_TECH_106B:
405 stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_106B;
406 result = st95hf_send_recv_cmd(stcontext,
407 CMD_ISO14443B_PROTOCOL_SELECT,
412 dev_err(dev, "protocol sel send, err = 0x%x\n",
418 * delay of 5-6 ms is required after select protocol
419 * command in case of ISO14443 Type B
421 usleep_range(50000, 60000);
423 /* secondary config. for 14443Type 4B after protocol select */
424 result = secondary_configuration_type4b(stcontext);
426 dev_err(dev, "type b secondary config, err = 0x%x\n",
431 case NFC_DIGITAL_RF_TECH_ISO15693:
432 stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_ISO15693;
433 result = st95hf_send_recv_cmd(stcontext,
434 CMD_ISO15693_PROTOCOL_SELECT,
439 dev_err(dev, "protocol sel send, err = 0x%x\n",
451 static void st95hf_send_st95enable_negativepulse(struct st95hf_context *st95con)
453 /* First make irq_in pin high */
454 gpio_set_value(st95con->enable_gpio, HIGH);
456 /* wait for 1 milisecond */
457 usleep_range(1000, 2000);
459 /* Make irq_in pin low */
460 gpio_set_value(st95con->enable_gpio, LOW);
462 /* wait for minimum interrupt pulse to make st95 active */
463 usleep_range(1000, 2000);
465 /* At end make it high */
466 gpio_set_value(st95con->enable_gpio, HIGH);
470 * Send a reset sequence over SPI bus (Reset command + wait 3ms +
471 * negative pulse on st95hf enable gpio
473 static int st95hf_send_spi_reset_sequence(struct st95hf_context *st95context)
476 unsigned char reset_cmd = ST95HF_COMMAND_RESET;
478 result = st95hf_spi_send(&st95context->spicontext,
480 ST95HF_RESET_CMD_LEN,
483 dev_err(&st95context->spicontext.spidev->dev,
484 "spi reset sequence cmd error = %d", result);
488 /* wait for 3 milisecond to complete the controller reset process */
489 usleep_range(3000, 4000);
491 /* send negative pulse to make st95hf active */
492 st95hf_send_st95enable_negativepulse(st95context);
494 /* wait for 10 milisecond : HFO setup time */
495 usleep_range(10000, 20000);
500 static int st95hf_por_sequence(struct st95hf_context *st95context)
505 st95hf_send_st95enable_negativepulse(st95context);
507 usleep_range(5000, 6000);
509 /* send an ECHO command and checks ST95HF response */
510 result = st95hf_echo_command(st95context);
512 dev_dbg(&st95context->spicontext.spidev->dev,
513 "response from echo function = 0x%x, attempt = %d\n",
514 result, nth_attempt);
519 /* send an pulse on IRQ in case of the chip is on sleep state */
520 if (nth_attempt == 2)
521 st95hf_send_st95enable_negativepulse(st95context);
523 st95hf_send_spi_reset_sequence(st95context);
525 /* delay of 50 milisecond */
526 usleep_range(50000, 51000);
527 } while (nth_attempt++ < 3);
532 static int iso14443_config_fdt(struct st95hf_context *st95context, int wtxm)
535 struct device *dev = &st95context->spicontext.spidev->dev;
536 struct nfc_digital_dev *nfcddev = st95context->ddev;
537 unsigned char pp_typeb;
538 struct param_list new_params[2];
540 pp_typeb = cmd_array[CMD_ISO14443B_PROTOCOL_SELECT].cmd_params[2];
542 if (nfcddev->curr_protocol == NFC_PROTO_ISO14443 &&
543 st95context->fwi < 4)
544 st95context->fwi = 4;
546 new_params[0].param_offset = 2;
547 if (nfcddev->curr_protocol == NFC_PROTO_ISO14443)
548 new_params[0].new_param_val = st95context->fwi;
549 else if (nfcddev->curr_protocol == NFC_PROTO_ISO14443_B)
550 new_params[0].new_param_val = pp_typeb;
552 new_params[1].param_offset = 3;
553 new_params[1].new_param_val = wtxm;
555 switch (nfcddev->curr_protocol) {
556 case NFC_PROTO_ISO14443:
557 result = st95hf_send_recv_cmd(st95context,
558 CMD_ISO14443A_PROTOCOL_SELECT,
563 dev_err(dev, "WTX type a sel proto, err = 0x%x\n",
568 /* secondary config. for 14443Type 4A after protocol select */
569 result = secondary_configuration_type4a(st95context);
571 dev_err(dev, "WTX type a second. config, err = 0x%x\n",
576 case NFC_PROTO_ISO14443_B:
577 result = st95hf_send_recv_cmd(st95context,
578 CMD_ISO14443B_PROTOCOL_SELECT,
583 dev_err(dev, "WTX type b sel proto, err = 0x%x\n",
588 /* secondary config. for 14443Type 4B after protocol select */
589 result = secondary_configuration_type4b(st95context);
591 dev_err(dev, "WTX type b second. config, err = 0x%x\n",
603 static int st95hf_handle_wtx(struct st95hf_context *stcontext,
608 unsigned char val_mm = 0;
609 struct param_list new_params[1];
610 struct nfc_digital_dev *nfcddev = stcontext->ddev;
611 struct device *dev = &stcontext->nfcdev->dev;
614 result = iso14443_config_fdt(stcontext, wtx_val & 0x3f);
616 dev_err(dev, "Config. setting error on WTX req, err = 0x%x\n",
621 /* Send response of wtx with ASYNC as no response expected */
622 new_params[0].param_offset = 1;
623 new_params[0].new_param_val = wtx_val;
625 result = st95hf_send_recv_cmd(stcontext,
631 dev_err(dev, "WTX response send, err = 0x%x\n", result);
635 /* if no new wtx, cofigure with default values */
636 if (nfcddev->curr_protocol == NFC_PROTO_ISO14443)
637 val_mm = cmd_array[CMD_ISO14443A_PROTOCOL_SELECT].cmd_params[3];
638 else if (nfcddev->curr_protocol == NFC_PROTO_ISO14443_B)
639 val_mm = cmd_array[CMD_ISO14443B_PROTOCOL_SELECT].cmd_params[3];
641 result = iso14443_config_fdt(stcontext, val_mm);
643 dev_err(dev, "Default config. setting error after WTX processing, err = 0x%x\n",
649 static int st95hf_error_handling(struct st95hf_context *stcontext,
650 struct sk_buff *skb_resp,
654 unsigned char error_byte;
655 struct device *dev = &stcontext->nfcdev->dev;
657 /* First check ST95HF specific error */
658 if (skb_resp->data[0] & ST95HF_ERR_MASK) {
659 if (skb_resp->data[0] == ST95HF_TIMEOUT_ERROR)
666 /* Check for CRC err only if CRC is present in the tag response */
667 switch (stcontext->current_rf_tech) {
668 case NFC_DIGITAL_RF_TECH_106A:
669 if (stcontext->sendrcv_trflag == TRFLAG_NFCA_STD_FRAME_CRC) {
670 error_byte = skb_resp->data[res_len - 3];
671 if (error_byte & ST95HF_NFCA_CRC_ERR_MASK) {
672 /* CRC error occurred */
673 dev_err(dev, "CRC error, byte received = 0x%x\n",
679 case NFC_DIGITAL_RF_TECH_106B:
680 case NFC_DIGITAL_RF_TECH_ISO15693:
681 error_byte = skb_resp->data[res_len - 1];
682 if (error_byte & ST95HF_NFCB_CRC_ERR_MASK) {
683 /* CRC error occurred */
684 dev_err(dev, "CRC error, byte received = 0x%x\n",
694 static int st95hf_response_handler(struct st95hf_context *stcontext,
695 struct sk_buff *skb_resp,
700 unsigned char val_mm;
701 struct nfc_digital_dev *nfcddev = stcontext->ddev;
702 struct device *dev = &stcontext->nfcdev->dev;
703 struct st95_digital_cmd_complete_arg *cb_arg;
705 cb_arg = &stcontext->complete_cb_arg;
707 /* Process the response */
708 skb_put(skb_resp, res_len);
710 /* Remove st95 header */
711 skb_pull(skb_resp, 2);
713 skb_len = skb_resp->len;
715 /* check if it is case of RATS request reply & FWI is present */
716 if (nfcddev->curr_protocol == NFC_PROTO_ISO14443 && cb_arg->rats &&
717 (skb_resp->data[1] & RATS_TB1_PRESENT_MASK)) {
718 if (skb_resp->data[1] & RATS_TA1_PRESENT_MASK)
720 (skb_resp->data[3] & TB1_FWI_MASK) >> 4;
723 (skb_resp->data[2] & TB1_FWI_MASK) >> 4;
725 val_mm = cmd_array[CMD_ISO14443A_PROTOCOL_SELECT].cmd_params[3];
727 result = iso14443_config_fdt(stcontext, val_mm);
729 dev_err(dev, "error in config_fdt to handle fwi of ATS, error=%d\n",
734 cb_arg->rats = false;
736 /* Remove CRC bytes only if received frames data has an eod (CRC) */
737 switch (stcontext->current_rf_tech) {
738 case NFC_DIGITAL_RF_TECH_106A:
739 if (stcontext->sendrcv_trflag == TRFLAG_NFCA_STD_FRAME_CRC)
740 skb_trim(skb_resp, (skb_len - 5));
742 skb_trim(skb_resp, (skb_len - 3));
744 case NFC_DIGITAL_RF_TECH_106B:
745 case NFC_DIGITAL_RF_TECH_ISO15693:
746 skb_trim(skb_resp, (skb_len - 3));
753 static irqreturn_t st95hf_irq_handler(int irq, void *st95hfcontext)
755 struct st95hf_context *stcontext =
756 (struct st95hf_context *)st95hfcontext;
758 if (stcontext->spicontext.req_issync) {
759 complete(&stcontext->spicontext.done);
760 stcontext->spicontext.req_issync = false;
764 return IRQ_WAKE_THREAD;
767 static irqreturn_t st95hf_irq_thread_handler(int irq, void *st95hfcontext)
772 struct device *spidevice;
773 struct sk_buff *skb_resp;
774 struct st95hf_context *stcontext =
775 (struct st95hf_context *)st95hfcontext;
776 struct st95_digital_cmd_complete_arg *cb_arg;
778 spidevice = &stcontext->spicontext.spidev->dev;
781 * check semaphore, if not down() already, then we don't
782 * know in which context the ISR is called and surely it
783 * will be a bug. Note that down() of the semaphore is done
784 * in the corresponding st95hf_in_send_cmd() and then
785 * only this ISR should be called. ISR will up() the
786 * semaphore before leaving. Hence when the ISR is called
787 * the correct behaviour is down_trylock() should always
788 * return 1 (indicating semaphore cant be taken and hence no
789 * change in semaphore count).
790 * If not, then we up() the semaphore and crash on
793 if (!down_trylock(&stcontext->exchange_lock)) {
794 up(&stcontext->exchange_lock);
795 WARN(1, "unknown context in ST95HF ISR");
799 cb_arg = &stcontext->complete_cb_arg;
800 skb_resp = cb_arg->skb_resp;
802 mutex_lock(&stcontext->rm_lock);
803 res_len = st95hf_spi_recv_response(&stcontext->spicontext,
806 dev_err(spidevice, "TISR spi response err = 0x%x\n", res_len);
811 /* if stcontext->nfcdev_free is true, it means remove already ran */
812 if (stcontext->nfcdev_free) {
817 if (skb_resp->data[2] == WTX_REQ_FROM_TAG) {
818 /* Request for new FWT from tag */
819 result = st95hf_handle_wtx(stcontext, true, skb_resp->data[3]);
824 mutex_unlock(&stcontext->rm_lock);
828 result = st95hf_error_handling(stcontext, skb_resp, res_len);
832 result = st95hf_response_handler(stcontext, skb_resp, res_len);
837 * If select protocol is done on wtx req. do select protocol
838 * again with default values
842 result = st95hf_handle_wtx(stcontext, false, 0);
847 /* call digital layer callback */
848 cb_arg->complete_cb(stcontext->ddev, cb_arg->cb_usrarg, skb_resp);
850 /* up the semaphore before returning */
851 up(&stcontext->exchange_lock);
852 mutex_unlock(&stcontext->rm_lock);
859 cb_arg->rats = false;
860 skb_resp = ERR_PTR(result);
861 /* call of callback with error */
862 cb_arg->complete_cb(stcontext->ddev, cb_arg->cb_usrarg, skb_resp);
863 /* up the semaphore before returning */
864 up(&stcontext->exchange_lock);
865 mutex_unlock(&stcontext->rm_lock);
869 /* NFC ops functions definition */
870 static int st95hf_in_configure_hw(struct nfc_digital_dev *ddev,
874 struct st95hf_context *stcontext = nfc_digital_get_drvdata(ddev);
876 if (type == NFC_DIGITAL_CONFIG_RF_TECH)
877 return st95hf_select_protocol(stcontext, param);
879 if (type == NFC_DIGITAL_CONFIG_FRAMING) {
881 case NFC_DIGITAL_FRAMING_NFCA_SHORT:
882 stcontext->sendrcv_trflag = TRFLAG_NFCA_SHORT_FRAME;
884 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
885 stcontext->sendrcv_trflag = TRFLAG_NFCA_STD_FRAME;
887 case NFC_DIGITAL_FRAMING_NFCA_T4T:
888 case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
889 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
890 stcontext->sendrcv_trflag = TRFLAG_NFCA_STD_FRAME_CRC;
892 case NFC_DIGITAL_FRAMING_NFCB:
893 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY:
894 case NFC_DIGITAL_FRAMING_ISO15693_T5T:
902 static int rf_off(struct st95hf_context *stcontext)
907 dev = &stcontext->nfcdev->dev;
909 rc = st95hf_send_recv_cmd(stcontext, CMD_FIELD_OFF, 0, NULL, true);
911 dev_err(dev, "protocol sel send field off, err = 0x%x\n", rc);
916 static int st95hf_in_send_cmd(struct nfc_digital_dev *ddev,
919 nfc_digital_cmd_complete_t cb,
922 struct st95hf_context *stcontext = nfc_digital_get_drvdata(ddev);
924 struct sk_buff *skb_resp;
925 int len_data_to_tag = 0;
927 skb_resp = nfc_alloc_recv_skb(MAX_RESPONSE_BUFFER_SIZE, GFP_KERNEL);
931 switch (stcontext->current_rf_tech) {
932 case NFC_DIGITAL_RF_TECH_106A:
933 len_data_to_tag = skb->len + 1;
934 skb_put_u8(skb, stcontext->sendrcv_trflag);
936 case NFC_DIGITAL_RF_TECH_106B:
937 case NFC_DIGITAL_RF_TECH_ISO15693:
938 len_data_to_tag = skb->len;
946 skb->data[0] = ST95HF_COMMAND_SEND;
947 skb->data[1] = SEND_RECEIVE_CMD;
948 skb->data[2] = len_data_to_tag;
950 stcontext->complete_cb_arg.skb_resp = skb_resp;
951 stcontext->complete_cb_arg.cb_usrarg = arg;
952 stcontext->complete_cb_arg.complete_cb = cb;
954 if ((skb->data[3] == ISO14443A_RATS_REQ) &&
955 ddev->curr_protocol == NFC_PROTO_ISO14443)
956 stcontext->complete_cb_arg.rats = true;
959 * down the semaphore to indicate to remove func that an
960 * ISR is pending, note that it will not block here in any case.
961 * If found blocked, it is a BUG!
963 rc = down_killable(&stcontext->exchange_lock);
965 WARN(1, "Semaphore is not found up in st95hf_in_send_cmd\n");
969 rc = st95hf_spi_send(&stcontext->spicontext, skb->data,
973 dev_err(&stcontext->nfcdev->dev,
974 "Error %d trying to perform data_exchange", rc);
975 /* up the semaphore since ISR will never come in this case */
976 up(&stcontext->exchange_lock);
989 /* p2p will be supported in a later release ! */
990 static int st95hf_tg_configure_hw(struct nfc_digital_dev *ddev,
997 static int st95hf_tg_send_cmd(struct nfc_digital_dev *ddev,
1000 nfc_digital_cmd_complete_t cb,
1006 static int st95hf_tg_listen(struct nfc_digital_dev *ddev,
1008 nfc_digital_cmd_complete_t cb,
1014 static int st95hf_tg_get_rf_tech(struct nfc_digital_dev *ddev, u8 *rf_tech)
1019 static int st95hf_switch_rf(struct nfc_digital_dev *ddev, bool on)
1022 struct st95hf_context *stcontext = nfc_digital_get_drvdata(ddev);
1024 rf_tech = ddev->curr_rf_tech;
1027 /* switch on RF field */
1028 return st95hf_select_protocol(stcontext, rf_tech);
1030 /* switch OFF RF field */
1031 return rf_off(stcontext);
1034 /* TODO st95hf_abort_cmd */
1035 static void st95hf_abort_cmd(struct nfc_digital_dev *ddev)
1039 static const struct nfc_digital_ops st95hf_nfc_digital_ops = {
1040 .in_configure_hw = st95hf_in_configure_hw,
1041 .in_send_cmd = st95hf_in_send_cmd,
1043 .tg_listen = st95hf_tg_listen,
1044 .tg_configure_hw = st95hf_tg_configure_hw,
1045 .tg_send_cmd = st95hf_tg_send_cmd,
1046 .tg_get_rf_tech = st95hf_tg_get_rf_tech,
1048 .switch_rf = st95hf_switch_rf,
1049 .abort_cmd = st95hf_abort_cmd,
1052 static const struct spi_device_id st95hf_id[] = {
1056 MODULE_DEVICE_TABLE(spi, st95hf_id);
1058 static const struct of_device_id st95hf_spi_of_match[] __maybe_unused = {
1059 { .compatible = "st,st95hf" },
1062 MODULE_DEVICE_TABLE(of, st95hf_spi_of_match);
1064 static int st95hf_probe(struct spi_device *nfc_spi_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_gpio =
1112 of_get_named_gpio(nfc_spi_dev->dev.of_node,
1115 if (!gpio_is_valid(st95context->enable_gpio)) {
1116 dev_err(&nfc_spi_dev->dev, "No valid enable gpio\n");
1117 ret = st95context->enable_gpio;
1118 goto err_disable_regulator;
1121 ret = devm_gpio_request_one(&nfc_spi_dev->dev, st95context->enable_gpio,
1122 GPIOF_DIR_OUT | GPIOF_INIT_HIGH,
1125 goto err_disable_regulator;
1127 if (nfc_spi_dev->irq > 0) {
1128 if (devm_request_threaded_irq(&nfc_spi_dev->dev,
1131 st95hf_irq_thread_handler,
1132 IRQF_TRIGGER_FALLING,
1134 (void *)st95context) < 0) {
1135 dev_err(&nfc_spi_dev->dev, "err: irq request for st95hf is failed\n");
1137 goto err_disable_regulator;
1140 dev_err(&nfc_spi_dev->dev, "not a valid IRQ associated with ST95HF\n");
1142 goto err_disable_regulator;
1146 * First reset SPI to handle warm reset of the system.
1147 * It will put the ST95HF device in Power ON state
1148 * which make the state of device identical to state
1149 * at the time of cold reset of the system.
1151 ret = st95hf_send_spi_reset_sequence(st95context);
1153 dev_err(&nfc_spi_dev->dev, "err: spi_reset_sequence failed\n");
1154 goto err_disable_regulator;
1157 /* call PowerOnReset sequence of ST95hf to activate it */
1158 ret = st95hf_por_sequence(st95context);
1160 dev_err(&nfc_spi_dev->dev, "err: por seq failed for st95hf\n");
1161 goto err_disable_regulator;
1164 /* create NFC dev object and register with NFC Subsystem */
1165 st95context->ddev = nfc_digital_allocate_device(&st95hf_nfc_digital_ops,
1166 ST95HF_SUPPORTED_PROT,
1167 ST95HF_CAPABILITIES,
1168 ST95HF_HEADROOM_LEN,
1169 ST95HF_TAILROOM_LEN);
1170 if (!st95context->ddev) {
1172 goto err_disable_regulator;
1175 st95context->nfcdev = st95context->ddev->nfc_dev;
1176 nfc_digital_set_parent_dev(st95context->ddev, &nfc_spi_dev->dev);
1178 ret = nfc_digital_register_device(st95context->ddev);
1180 dev_err(&st95context->nfcdev->dev, "st95hf registration failed\n");
1181 goto err_free_digital_device;
1184 /* store st95context in nfc device object */
1185 nfc_digital_set_drvdata(st95context->ddev, st95context);
1187 sema_init(&st95context->exchange_lock, 1);
1188 mutex_init(&st95context->rm_lock);
1192 err_free_digital_device:
1193 nfc_digital_free_device(st95context->ddev);
1194 err_disable_regulator:
1195 if (st95context->st95hf_supply)
1196 regulator_disable(st95context->st95hf_supply);
1201 static void st95hf_remove(struct spi_device *nfc_spi_dev)
1204 unsigned char reset_cmd = ST95HF_COMMAND_RESET;
1205 struct st95hf_spi_context *spictx = dev_get_drvdata(&nfc_spi_dev->dev);
1207 struct st95hf_context *stcontext = container_of(spictx,
1208 struct st95hf_context,
1211 mutex_lock(&stcontext->rm_lock);
1213 nfc_digital_unregister_device(stcontext->ddev);
1214 nfc_digital_free_device(stcontext->ddev);
1215 stcontext->nfcdev_free = true;
1217 mutex_unlock(&stcontext->rm_lock);
1219 /* if last in_send_cmd's ISR is pending, wait for it to finish */
1220 result = down_killable(&stcontext->exchange_lock);
1221 if (result == -EINTR)
1222 dev_err(&spictx->spidev->dev, "sleep for semaphore interrupted by signal\n");
1224 /* next reset the ST95HF controller */
1225 result = st95hf_spi_send(&stcontext->spicontext,
1227 ST95HF_RESET_CMD_LEN,
1230 dev_err(&spictx->spidev->dev,
1231 "ST95HF reset failed in remove() err = %d\n", result);
1233 /* wait for 3 ms to complete the controller reset process */
1234 usleep_range(3000, 4000);
1236 /* disable regulator */
1237 if (stcontext->st95hf_supply)
1238 regulator_disable(stcontext->st95hf_supply);
1241 /* Register as SPI protocol driver */
1242 static struct spi_driver st95hf_driver = {
1245 .owner = THIS_MODULE,
1246 .of_match_table = of_match_ptr(st95hf_spi_of_match),
1248 .id_table = st95hf_id,
1249 .probe = st95hf_probe,
1250 .remove = st95hf_remove,
1253 module_spi_driver(st95hf_driver);
1256 MODULE_DESCRIPTION("ST NFC Transceiver ST95HF driver");
1257 MODULE_LICENSE("GPL v2");