Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * --------------------------------------------------------------------
0004  * Driver for ST NFC Transceiver ST95HF
0005  * --------------------------------------------------------------------
0006  * Copyright (C) 2015 STMicroelectronics Pvt. Ltd. All rights reserved.
0007  */
0008 
0009 #include <linux/err.h>
0010 #include <linux/gpio.h>
0011 #include <linux/init.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/irq.h>
0014 #include <linux/module.h>
0015 #include <linux/netdevice.h>
0016 #include <linux/nfc.h>
0017 #include <linux/of_gpio.h>
0018 #include <linux/of.h>
0019 #include <linux/property.h>
0020 #include <linux/regulator/consumer.h>
0021 #include <linux/wait.h>
0022 #include <net/nfc/digital.h>
0023 #include <net/nfc/nfc.h>
0024 
0025 #include "spi.h"
0026 
0027 /* supported protocols */
0028 #define ST95HF_SUPPORTED_PROT       (NFC_PROTO_ISO14443_MASK | \
0029                     NFC_PROTO_ISO14443_B_MASK | \
0030                     NFC_PROTO_ISO15693_MASK)
0031 /* driver capabilities */
0032 #define ST95HF_CAPABILITIES     NFC_DIGITAL_DRV_CAPS_IN_CRC
0033 
0034 /* Command Send Interface */
0035 /* ST95HF_COMMAND_SEND CMD Ids */
0036 #define ECHO_CMD            0x55
0037 #define WRITE_REGISTER_CMD      0x9
0038 #define PROTOCOL_SELECT_CMD     0x2
0039 #define SEND_RECEIVE_CMD        0x4
0040 
0041 /* Select protocol codes */
0042 #define ISO15693_PROTOCOL_CODE      0x1
0043 #define ISO14443A_PROTOCOL_CODE     0x2
0044 #define ISO14443B_PROTOCOL_CODE     0x3
0045 
0046 /*
0047  * head room len is 3
0048  * 1 byte for control byte
0049  * 1 byte for cmd
0050  * 1 byte for size
0051  */
0052 #define ST95HF_HEADROOM_LEN     3
0053 
0054 /*
0055  * tailroom is 1 for ISO14443A
0056  * and 0 for ISO14443B/ISO15693,
0057  * hence the max value 1 should be
0058  * taken.
0059  */
0060 #define ST95HF_TAILROOM_LEN     1
0061 
0062 /* Command Response interface */
0063 #define MAX_RESPONSE_BUFFER_SIZE    280
0064 #define ECHORESPONSE            0x55
0065 #define ST95HF_ERR_MASK         0xF
0066 #define ST95HF_TIMEOUT_ERROR        0x87
0067 #define ST95HF_NFCA_CRC_ERR_MASK    0x20
0068 #define ST95HF_NFCB_CRC_ERR_MASK    0x01
0069 
0070 /* ST95HF transmission flag values */
0071 #define TRFLAG_NFCA_SHORT_FRAME     0x07
0072 #define TRFLAG_NFCA_STD_FRAME       0x08
0073 #define TRFLAG_NFCA_STD_FRAME_CRC   0x28
0074 
0075 /* Misc defs */
0076 #define HIGH                1
0077 #define LOW             0
0078 #define ISO14443A_RATS_REQ      0xE0
0079 #define RATS_TB1_PRESENT_MASK       0x20
0080 #define RATS_TA1_PRESENT_MASK       0x10
0081 #define TB1_FWI_MASK            0xF0
0082 #define WTX_REQ_FROM_TAG        0xF2
0083 
0084 #define MAX_CMD_LEN         0x7
0085 
0086 #define MAX_CMD_PARAMS          4
0087 struct cmd {
0088     int cmd_len;
0089     unsigned char cmd_id;
0090     unsigned char no_cmd_params;
0091     unsigned char cmd_params[MAX_CMD_PARAMS];
0092     enum req_type req;
0093 };
0094 
0095 struct param_list {
0096     int param_offset;
0097     int new_param_val;
0098 };
0099 
0100 /*
0101  * List of top-level cmds to be used internally by the driver.
0102  * All these commands are build on top of ST95HF basic commands
0103  * such as SEND_RECEIVE_CMD, PROTOCOL_SELECT_CMD, etc.
0104  * These top level cmds are used internally while implementing various ops of
0105  * digital layer/driver probe or extending the digital framework layer for
0106  * features that are not yet implemented there, for example, WTX cmd handling.
0107  */
0108 enum st95hf_cmd_list {
0109     CMD_ECHO,
0110     CMD_ISO14443A_CONFIG,
0111     CMD_ISO14443A_DEMOGAIN,
0112     CMD_ISO14443B_DEMOGAIN,
0113     CMD_ISO14443A_PROTOCOL_SELECT,
0114     CMD_ISO14443B_PROTOCOL_SELECT,
0115     CMD_WTX_RESPONSE,
0116     CMD_FIELD_OFF,
0117     CMD_ISO15693_PROTOCOL_SELECT,
0118 };
0119 
0120 static const struct cmd cmd_array[] = {
0121     [CMD_ECHO] = {
0122         .cmd_len = 0x2,
0123         .cmd_id = ECHO_CMD,
0124         .no_cmd_params = 0,
0125         .req = SYNC,
0126     },
0127     [CMD_ISO14443A_CONFIG] = {
0128         .cmd_len = 0x7,
0129         .cmd_id = WRITE_REGISTER_CMD,
0130         .no_cmd_params = 0x4,
0131         .cmd_params = {0x3A, 0x00, 0x5A, 0x04},
0132         .req = SYNC,
0133     },
0134     [CMD_ISO14443A_DEMOGAIN] = {
0135         .cmd_len = 0x7,
0136         .cmd_id = WRITE_REGISTER_CMD,
0137         .no_cmd_params = 0x4,
0138         .cmd_params = {0x68, 0x01, 0x01, 0xDF},
0139         .req = SYNC,
0140     },
0141     [CMD_ISO14443B_DEMOGAIN] = {
0142         .cmd_len = 0x7,
0143         .cmd_id = WRITE_REGISTER_CMD,
0144         .no_cmd_params = 0x4,
0145         .cmd_params = {0x68, 0x01, 0x01, 0x51},
0146         .req = SYNC,
0147     },
0148     [CMD_ISO14443A_PROTOCOL_SELECT] = {
0149         .cmd_len = 0x7,
0150         .cmd_id = PROTOCOL_SELECT_CMD,
0151         .no_cmd_params = 0x4,
0152         .cmd_params = {ISO14443A_PROTOCOL_CODE, 0x00, 0x01, 0xA0},
0153         .req = SYNC,
0154     },
0155     [CMD_ISO14443B_PROTOCOL_SELECT] = {
0156         .cmd_len = 0x7,
0157         .cmd_id = PROTOCOL_SELECT_CMD,
0158         .no_cmd_params = 0x4,
0159         .cmd_params = {ISO14443B_PROTOCOL_CODE, 0x01, 0x03, 0xFF},
0160         .req = SYNC,
0161     },
0162     [CMD_WTX_RESPONSE] = {
0163         .cmd_len = 0x6,
0164         .cmd_id = SEND_RECEIVE_CMD,
0165         .no_cmd_params = 0x3,
0166         .cmd_params = {0xF2, 0x00, TRFLAG_NFCA_STD_FRAME_CRC},
0167         .req = ASYNC,
0168     },
0169     [CMD_FIELD_OFF] = {
0170         .cmd_len = 0x5,
0171         .cmd_id = PROTOCOL_SELECT_CMD,
0172         .no_cmd_params = 0x2,
0173         .cmd_params = {0x0, 0x0},
0174         .req = SYNC,
0175     },
0176     [CMD_ISO15693_PROTOCOL_SELECT] = {
0177         .cmd_len = 0x5,
0178         .cmd_id = PROTOCOL_SELECT_CMD,
0179         .no_cmd_params = 0x2,
0180         .cmd_params = {ISO15693_PROTOCOL_CODE, 0x0D},
0181         .req = SYNC,
0182     },
0183 };
0184 
0185 /* st95_digital_cmd_complete_arg stores client context */
0186 struct st95_digital_cmd_complete_arg {
0187     struct sk_buff *skb_resp;
0188     nfc_digital_cmd_complete_t complete_cb;
0189     void *cb_usrarg;
0190     bool rats;
0191 };
0192 
0193 /*
0194  * structure containing ST95HF driver specific data.
0195  * @spicontext: structure containing information required
0196  *  for spi communication between st95hf and host.
0197  * @ddev: nfc digital device object.
0198  * @nfcdev: nfc device object.
0199  * @enable_gpio: gpio used to enable st95hf transceiver.
0200  * @complete_cb_arg: structure to store various context information
0201  *  that is passed from nfc requesting thread to the threaded ISR.
0202  * @st95hf_supply: regulator "consumer" for NFC device.
0203  * @sendrcv_trflag: last byte of frame send by sendrecv command
0204  *  of st95hf. This byte contains transmission flag info.
0205  * @exchange_lock: semaphore used for signaling the st95hf_remove
0206  *  function that the last outstanding async nfc request is finished.
0207  * @rm_lock: mutex for ensuring safe access of nfc digital object
0208  *  from threaded ISR. Usage of this mutex avoids any race between
0209  *  deletion of the object from st95hf_remove() and its access from
0210  *  the threaded ISR.
0211  * @nfcdev_free: flag to have the state of nfc device object.
0212  *  [alive | died]
0213  * @current_protocol: current nfc protocol.
0214  * @current_rf_tech: current rf technology.
0215  * @fwi: frame waiting index, received in reply of RATS according to
0216  *  digital protocol.
0217  */
0218 struct st95hf_context {
0219     struct st95hf_spi_context spicontext;
0220     struct nfc_digital_dev *ddev;
0221     struct nfc_dev *nfcdev;
0222     unsigned int enable_gpio;
0223     struct st95_digital_cmd_complete_arg complete_cb_arg;
0224     struct regulator *st95hf_supply;
0225     unsigned char sendrcv_trflag;
0226     struct semaphore exchange_lock;
0227     struct mutex rm_lock;
0228     bool nfcdev_free;
0229     u8 current_protocol;
0230     u8 current_rf_tech;
0231     int fwi;
0232 };
0233 
0234 /*
0235  * st95hf_send_recv_cmd() is for sending commands to ST95HF
0236  * that are described in the cmd_array[]. It can optionally
0237  * receive the response if the cmd request is of type
0238  * SYNC. For that to happen caller must pass true to recv_res.
0239  * For ASYNC request, recv_res is ignored and the
0240  * function will never try to receive the response on behalf
0241  * of the caller.
0242  */
0243 static int st95hf_send_recv_cmd(struct st95hf_context *st95context,
0244                 enum st95hf_cmd_list cmd,
0245                 int no_modif,
0246                 struct param_list *list_array,
0247                 bool recv_res)
0248 {
0249     unsigned char spi_cmd_buffer[MAX_CMD_LEN];
0250     int i, ret;
0251     struct device *dev = &st95context->spicontext.spidev->dev;
0252 
0253     if (cmd_array[cmd].cmd_len > MAX_CMD_LEN)
0254         return -EINVAL;
0255     if (cmd_array[cmd].no_cmd_params < no_modif)
0256         return -EINVAL;
0257     if (no_modif && !list_array)
0258         return -EINVAL;
0259 
0260     spi_cmd_buffer[0] = ST95HF_COMMAND_SEND;
0261     spi_cmd_buffer[1] = cmd_array[cmd].cmd_id;
0262     spi_cmd_buffer[2] = cmd_array[cmd].no_cmd_params;
0263 
0264     memcpy(&spi_cmd_buffer[3], cmd_array[cmd].cmd_params,
0265            spi_cmd_buffer[2]);
0266 
0267     for (i = 0; i < no_modif; i++) {
0268         if (list_array[i].param_offset >= cmd_array[cmd].no_cmd_params)
0269             return -EINVAL;
0270         spi_cmd_buffer[3 + list_array[i].param_offset] =
0271                         list_array[i].new_param_val;
0272     }
0273 
0274     ret = st95hf_spi_send(&st95context->spicontext,
0275                   spi_cmd_buffer,
0276                   cmd_array[cmd].cmd_len,
0277                   cmd_array[cmd].req);
0278     if (ret) {
0279         dev_err(dev, "st95hf_spi_send failed with error %d\n", ret);
0280         return ret;
0281     }
0282 
0283     if (cmd_array[cmd].req == SYNC && recv_res) {
0284         unsigned char st95hf_response_arr[2];
0285 
0286         ret = st95hf_spi_recv_response(&st95context->spicontext,
0287                            st95hf_response_arr);
0288         if (ret < 0) {
0289             dev_err(dev, "spi error from st95hf_spi_recv_response(), err = 0x%x\n",
0290                 ret);
0291             return ret;
0292         }
0293 
0294         if (st95hf_response_arr[0]) {
0295             dev_err(dev, "st95hf error from st95hf_spi_recv_response(), err = 0x%x\n",
0296                 st95hf_response_arr[0]);
0297             return -EIO;
0298         }
0299     }
0300 
0301     return 0;
0302 }
0303 
0304 static int st95hf_echo_command(struct st95hf_context *st95context)
0305 {
0306     int result = 0;
0307     unsigned char echo_response;
0308 
0309     result = st95hf_send_recv_cmd(st95context, CMD_ECHO, 0, NULL, false);
0310     if (result)
0311         return result;
0312 
0313     /* If control reached here, response can be taken */
0314     result = st95hf_spi_recv_echo_res(&st95context->spicontext,
0315                       &echo_response);
0316     if (result) {
0317         dev_err(&st95context->spicontext.spidev->dev,
0318             "err: echo response receive error = 0x%x\n", result);
0319         return result;
0320     }
0321 
0322     if (echo_response == ECHORESPONSE)
0323         return 0;
0324 
0325     dev_err(&st95context->spicontext.spidev->dev, "err: echo res is 0x%x\n",
0326         echo_response);
0327 
0328     return -EIO;
0329 }
0330 
0331 static int secondary_configuration_type4a(struct st95hf_context *stcontext)
0332 {
0333     int result = 0;
0334     struct device *dev = &stcontext->nfcdev->dev;
0335 
0336     /* 14443A config setting after select protocol */
0337     result = st95hf_send_recv_cmd(stcontext,
0338                       CMD_ISO14443A_CONFIG,
0339                       0,
0340                       NULL,
0341                       true);
0342     if (result) {
0343         dev_err(dev, "type a config cmd, err = 0x%x\n", result);
0344         return result;
0345     }
0346 
0347     /* 14443A demo gain setting */
0348     result = st95hf_send_recv_cmd(stcontext,
0349                       CMD_ISO14443A_DEMOGAIN,
0350                       0,
0351                       NULL,
0352                       true);
0353     if (result)
0354         dev_err(dev, "type a demogain cmd, err = 0x%x\n", result);
0355 
0356     return result;
0357 }
0358 
0359 static int secondary_configuration_type4b(struct st95hf_context *stcontext)
0360 {
0361     int result = 0;
0362     struct device *dev = &stcontext->nfcdev->dev;
0363 
0364     result = st95hf_send_recv_cmd(stcontext,
0365                       CMD_ISO14443B_DEMOGAIN,
0366                       0,
0367                       NULL,
0368                       true);
0369     if (result)
0370         dev_err(dev, "type b demogain cmd, err = 0x%x\n", result);
0371 
0372     return result;
0373 }
0374 
0375 static int st95hf_select_protocol(struct st95hf_context *stcontext, int type)
0376 {
0377     int result = 0;
0378     struct device *dev;
0379 
0380     dev = &stcontext->nfcdev->dev;
0381 
0382     switch (type) {
0383     case NFC_DIGITAL_RF_TECH_106A:
0384         stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_106A;
0385         result = st95hf_send_recv_cmd(stcontext,
0386                           CMD_ISO14443A_PROTOCOL_SELECT,
0387                           0,
0388                           NULL,
0389                           true);
0390         if (result) {
0391             dev_err(dev, "protocol sel, err = 0x%x\n",
0392                 result);
0393             return result;
0394         }
0395 
0396         /* secondary config. for 14443Type 4A after protocol select */
0397         result = secondary_configuration_type4a(stcontext);
0398         if (result) {
0399             dev_err(dev, "type a secondary config, err = 0x%x\n",
0400                 result);
0401             return result;
0402         }
0403         break;
0404     case NFC_DIGITAL_RF_TECH_106B:
0405         stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_106B;
0406         result = st95hf_send_recv_cmd(stcontext,
0407                           CMD_ISO14443B_PROTOCOL_SELECT,
0408                           0,
0409                           NULL,
0410                           true);
0411         if (result) {
0412             dev_err(dev, "protocol sel send, err = 0x%x\n",
0413                 result);
0414             return result;
0415         }
0416 
0417         /*
0418          * delay of 5-6 ms is required after select protocol
0419          * command in case of ISO14443 Type B
0420          */
0421         usleep_range(50000, 60000);
0422 
0423         /* secondary config. for 14443Type 4B after protocol select */
0424         result = secondary_configuration_type4b(stcontext);
0425         if (result) {
0426             dev_err(dev, "type b secondary config, err = 0x%x\n",
0427                 result);
0428             return result;
0429         }
0430         break;
0431     case NFC_DIGITAL_RF_TECH_ISO15693:
0432         stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_ISO15693;
0433         result = st95hf_send_recv_cmd(stcontext,
0434                           CMD_ISO15693_PROTOCOL_SELECT,
0435                           0,
0436                           NULL,
0437                           true);
0438         if (result) {
0439             dev_err(dev, "protocol sel send, err = 0x%x\n",
0440                 result);
0441             return result;
0442         }
0443         break;
0444     default:
0445         return -EINVAL;
0446     }
0447 
0448     return 0;
0449 }
0450 
0451 static void st95hf_send_st95enable_negativepulse(struct st95hf_context *st95con)
0452 {
0453     /* First make irq_in pin high */
0454     gpio_set_value(st95con->enable_gpio, HIGH);
0455 
0456     /* wait for 1 milisecond */
0457     usleep_range(1000, 2000);
0458 
0459     /* Make irq_in pin low */
0460     gpio_set_value(st95con->enable_gpio, LOW);
0461 
0462     /* wait for minimum interrupt pulse to make st95 active */
0463     usleep_range(1000, 2000);
0464 
0465     /* At end make it high */
0466     gpio_set_value(st95con->enable_gpio, HIGH);
0467 }
0468 
0469 /*
0470  * Send a reset sequence over SPI bus (Reset command + wait 3ms +
0471  * negative pulse on st95hf enable gpio
0472  */
0473 static int st95hf_send_spi_reset_sequence(struct st95hf_context *st95context)
0474 {
0475     int result = 0;
0476     unsigned char reset_cmd = ST95HF_COMMAND_RESET;
0477 
0478     result = st95hf_spi_send(&st95context->spicontext,
0479                  &reset_cmd,
0480                  ST95HF_RESET_CMD_LEN,
0481                  ASYNC);
0482     if (result) {
0483         dev_err(&st95context->spicontext.spidev->dev,
0484             "spi reset sequence cmd error = %d", result);
0485         return result;
0486     }
0487 
0488     /* wait for 3 milisecond to complete the controller reset process */
0489     usleep_range(3000, 4000);
0490 
0491     /* send negative pulse to make st95hf active */
0492     st95hf_send_st95enable_negativepulse(st95context);
0493 
0494     /* wait for 10 milisecond : HFO setup time */
0495     usleep_range(10000, 20000);
0496 
0497     return result;
0498 }
0499 
0500 static int st95hf_por_sequence(struct st95hf_context *st95context)
0501 {
0502     int nth_attempt = 1;
0503     int result;
0504 
0505     st95hf_send_st95enable_negativepulse(st95context);
0506 
0507     usleep_range(5000, 6000);
0508     do {
0509         /* send an ECHO command and checks ST95HF response */
0510         result = st95hf_echo_command(st95context);
0511 
0512         dev_dbg(&st95context->spicontext.spidev->dev,
0513             "response from echo function = 0x%x, attempt = %d\n",
0514             result, nth_attempt);
0515 
0516         if (!result)
0517             return 0;
0518 
0519         /* send an pulse on IRQ in case of the chip is on sleep state */
0520         if (nth_attempt == 2)
0521             st95hf_send_st95enable_negativepulse(st95context);
0522         else
0523             st95hf_send_spi_reset_sequence(st95context);
0524 
0525         /* delay of 50 milisecond */
0526         usleep_range(50000, 51000);
0527     } while (nth_attempt++ < 3);
0528 
0529     return -ETIMEDOUT;
0530 }
0531 
0532 static int iso14443_config_fdt(struct st95hf_context *st95context, int wtxm)
0533 {
0534     int result = 0;
0535     struct device *dev = &st95context->spicontext.spidev->dev;
0536     struct nfc_digital_dev *nfcddev = st95context->ddev;
0537     unsigned char pp_typeb;
0538     struct param_list new_params[2];
0539 
0540     pp_typeb = cmd_array[CMD_ISO14443B_PROTOCOL_SELECT].cmd_params[2];
0541 
0542     if (nfcddev->curr_protocol == NFC_PROTO_ISO14443 &&
0543         st95context->fwi < 4)
0544         st95context->fwi = 4;
0545 
0546     new_params[0].param_offset = 2;
0547     if (nfcddev->curr_protocol == NFC_PROTO_ISO14443)
0548         new_params[0].new_param_val = st95context->fwi;
0549     else if (nfcddev->curr_protocol == NFC_PROTO_ISO14443_B)
0550         new_params[0].new_param_val = pp_typeb;
0551 
0552     new_params[1].param_offset = 3;
0553     new_params[1].new_param_val = wtxm;
0554 
0555     switch (nfcddev->curr_protocol) {
0556     case NFC_PROTO_ISO14443:
0557         result = st95hf_send_recv_cmd(st95context,
0558                           CMD_ISO14443A_PROTOCOL_SELECT,
0559                           2,
0560                           new_params,
0561                           true);
0562         if (result) {
0563             dev_err(dev, "WTX type a sel proto, err = 0x%x\n",
0564                 result);
0565             return result;
0566         }
0567 
0568         /* secondary config. for 14443Type 4A after protocol select */
0569         result = secondary_configuration_type4a(st95context);
0570         if (result) {
0571             dev_err(dev, "WTX type a second. config, err = 0x%x\n",
0572                 result);
0573             return result;
0574         }
0575         break;
0576     case NFC_PROTO_ISO14443_B:
0577         result = st95hf_send_recv_cmd(st95context,
0578                           CMD_ISO14443B_PROTOCOL_SELECT,
0579                           2,
0580                           new_params,
0581                           true);
0582         if (result) {
0583             dev_err(dev, "WTX type b sel proto, err = 0x%x\n",
0584                 result);
0585             return result;
0586         }
0587 
0588         /* secondary config. for 14443Type 4B after protocol select */
0589         result = secondary_configuration_type4b(st95context);
0590         if (result) {
0591             dev_err(dev, "WTX type b second. config, err = 0x%x\n",
0592                 result);
0593             return result;
0594         }
0595         break;
0596     default:
0597         return -EINVAL;
0598     }
0599 
0600     return 0;
0601 }
0602 
0603 static int st95hf_handle_wtx(struct st95hf_context *stcontext,
0604                  bool new_wtx,
0605                  int wtx_val)
0606 {
0607     int result = 0;
0608     unsigned char val_mm = 0;
0609     struct param_list new_params[1];
0610     struct nfc_digital_dev *nfcddev = stcontext->ddev;
0611     struct device *dev = &stcontext->nfcdev->dev;
0612 
0613     if (new_wtx) {
0614         result = iso14443_config_fdt(stcontext, wtx_val & 0x3f);
0615         if (result) {
0616             dev_err(dev, "Config. setting error on WTX req, err = 0x%x\n",
0617                 result);
0618             return result;
0619         }
0620 
0621         /* Send response of wtx with ASYNC as no response expected */
0622         new_params[0].param_offset = 1;
0623         new_params[0].new_param_val = wtx_val;
0624 
0625         result = st95hf_send_recv_cmd(stcontext,
0626                           CMD_WTX_RESPONSE,
0627                           1,
0628                           new_params,
0629                           false);
0630         if (result)
0631             dev_err(dev, "WTX response send, err = 0x%x\n", result);
0632         return result;
0633     }
0634 
0635     /* if no new wtx, cofigure with default values */
0636     if (nfcddev->curr_protocol == NFC_PROTO_ISO14443)
0637         val_mm = cmd_array[CMD_ISO14443A_PROTOCOL_SELECT].cmd_params[3];
0638     else if (nfcddev->curr_protocol == NFC_PROTO_ISO14443_B)
0639         val_mm = cmd_array[CMD_ISO14443B_PROTOCOL_SELECT].cmd_params[3];
0640 
0641     result = iso14443_config_fdt(stcontext, val_mm);
0642     if (result)
0643         dev_err(dev, "Default config. setting error after WTX processing, err = 0x%x\n",
0644             result);
0645 
0646     return result;
0647 }
0648 
0649 static int st95hf_error_handling(struct st95hf_context *stcontext,
0650                  struct sk_buff *skb_resp,
0651                  int res_len)
0652 {
0653     int result = 0;
0654     unsigned char error_byte;
0655     struct device *dev = &stcontext->nfcdev->dev;
0656 
0657     /* First check ST95HF specific error */
0658     if (skb_resp->data[0] & ST95HF_ERR_MASK) {
0659         if (skb_resp->data[0] == ST95HF_TIMEOUT_ERROR)
0660             result = -ETIMEDOUT;
0661         else
0662             result = -EIO;
0663         return result;
0664     }
0665 
0666     /* Check for CRC err only if CRC is present in the tag response */
0667     switch (stcontext->current_rf_tech) {
0668     case NFC_DIGITAL_RF_TECH_106A:
0669         if (stcontext->sendrcv_trflag == TRFLAG_NFCA_STD_FRAME_CRC) {
0670             error_byte = skb_resp->data[res_len - 3];
0671             if (error_byte & ST95HF_NFCA_CRC_ERR_MASK) {
0672                 /* CRC error occurred */
0673                 dev_err(dev, "CRC error, byte received = 0x%x\n",
0674                     error_byte);
0675                 result = -EIO;
0676             }
0677         }
0678         break;
0679     case NFC_DIGITAL_RF_TECH_106B:
0680     case NFC_DIGITAL_RF_TECH_ISO15693:
0681         error_byte = skb_resp->data[res_len - 1];
0682         if (error_byte & ST95HF_NFCB_CRC_ERR_MASK) {
0683             /* CRC error occurred */
0684             dev_err(dev, "CRC error, byte received = 0x%x\n",
0685                 error_byte);
0686             result = -EIO;
0687         }
0688         break;
0689     }
0690 
0691     return result;
0692 }
0693 
0694 static int st95hf_response_handler(struct st95hf_context *stcontext,
0695                    struct sk_buff *skb_resp,
0696                    int res_len)
0697 {
0698     int result = 0;
0699     int skb_len;
0700     unsigned char val_mm;
0701     struct nfc_digital_dev *nfcddev = stcontext->ddev;
0702     struct device *dev = &stcontext->nfcdev->dev;
0703     struct st95_digital_cmd_complete_arg *cb_arg;
0704 
0705     cb_arg = &stcontext->complete_cb_arg;
0706 
0707     /* Process the response */
0708     skb_put(skb_resp, res_len);
0709 
0710     /* Remove st95 header */
0711     skb_pull(skb_resp, 2);
0712 
0713     skb_len = skb_resp->len;
0714 
0715     /* check if it is case of RATS request reply & FWI is present */
0716     if (nfcddev->curr_protocol == NFC_PROTO_ISO14443 && cb_arg->rats &&
0717         (skb_resp->data[1] & RATS_TB1_PRESENT_MASK)) {
0718         if (skb_resp->data[1] & RATS_TA1_PRESENT_MASK)
0719             stcontext->fwi =
0720                 (skb_resp->data[3] & TB1_FWI_MASK) >> 4;
0721         else
0722             stcontext->fwi =
0723                 (skb_resp->data[2] & TB1_FWI_MASK) >> 4;
0724 
0725         val_mm = cmd_array[CMD_ISO14443A_PROTOCOL_SELECT].cmd_params[3];
0726 
0727         result = iso14443_config_fdt(stcontext, val_mm);
0728         if (result) {
0729             dev_err(dev, "error in config_fdt to handle fwi of ATS, error=%d\n",
0730                 result);
0731             return result;
0732         }
0733     }
0734     cb_arg->rats = false;
0735 
0736     /* Remove CRC bytes only if received frames data has an eod (CRC) */
0737     switch (stcontext->current_rf_tech) {
0738     case NFC_DIGITAL_RF_TECH_106A:
0739         if (stcontext->sendrcv_trflag == TRFLAG_NFCA_STD_FRAME_CRC)
0740             skb_trim(skb_resp, (skb_len - 5));
0741         else
0742             skb_trim(skb_resp, (skb_len - 3));
0743         break;
0744     case NFC_DIGITAL_RF_TECH_106B:
0745     case NFC_DIGITAL_RF_TECH_ISO15693:
0746         skb_trim(skb_resp, (skb_len - 3));
0747         break;
0748     }
0749 
0750     return result;
0751 }
0752 
0753 static irqreturn_t st95hf_irq_handler(int irq, void  *st95hfcontext)
0754 {
0755     struct st95hf_context *stcontext  =
0756         (struct st95hf_context *)st95hfcontext;
0757 
0758     if (stcontext->spicontext.req_issync) {
0759         complete(&stcontext->spicontext.done);
0760         stcontext->spicontext.req_issync = false;
0761         return IRQ_HANDLED;
0762     }
0763 
0764     return IRQ_WAKE_THREAD;
0765 }
0766 
0767 static irqreturn_t st95hf_irq_thread_handler(int irq, void  *st95hfcontext)
0768 {
0769     int result = 0;
0770     int res_len;
0771     static bool wtx;
0772     struct device *spidevice;
0773     struct sk_buff *skb_resp;
0774     struct st95hf_context *stcontext  =
0775         (struct st95hf_context *)st95hfcontext;
0776     struct st95_digital_cmd_complete_arg *cb_arg;
0777 
0778     spidevice = &stcontext->spicontext.spidev->dev;
0779 
0780     /*
0781      * check semaphore, if not down() already, then we don't
0782      * know in which context the ISR is called and surely it
0783      * will be a bug. Note that down() of the semaphore is done
0784      * in the corresponding st95hf_in_send_cmd() and then
0785      * only this ISR should be called. ISR will up() the
0786      * semaphore before leaving. Hence when the ISR is called
0787      * the correct behaviour is down_trylock() should always
0788      * return 1 (indicating semaphore cant be taken and hence no
0789      * change in semaphore count).
0790      * If not, then we up() the semaphore and crash on
0791      * a BUG() !
0792      */
0793     if (!down_trylock(&stcontext->exchange_lock)) {
0794         up(&stcontext->exchange_lock);
0795         WARN(1, "unknown context in ST95HF ISR");
0796         return IRQ_NONE;
0797     }
0798 
0799     cb_arg = &stcontext->complete_cb_arg;
0800     skb_resp = cb_arg->skb_resp;
0801 
0802     mutex_lock(&stcontext->rm_lock);
0803     res_len = st95hf_spi_recv_response(&stcontext->spicontext,
0804                        skb_resp->data);
0805     if (res_len < 0) {
0806         dev_err(spidevice, "TISR spi response err = 0x%x\n", res_len);
0807         result = res_len;
0808         goto end;
0809     }
0810 
0811     /* if stcontext->nfcdev_free is true, it means remove already ran */
0812     if (stcontext->nfcdev_free) {
0813         result = -ENODEV;
0814         goto end;
0815     }
0816 
0817     if (skb_resp->data[2] == WTX_REQ_FROM_TAG) {
0818         /* Request for new FWT from tag */
0819         result = st95hf_handle_wtx(stcontext, true, skb_resp->data[3]);
0820         if (result)
0821             goto end;
0822 
0823         wtx = true;
0824         mutex_unlock(&stcontext->rm_lock);
0825         return IRQ_HANDLED;
0826     }
0827 
0828     result = st95hf_error_handling(stcontext, skb_resp, res_len);
0829     if (result)
0830         goto end;
0831 
0832     result = st95hf_response_handler(stcontext, skb_resp, res_len);
0833     if (result)
0834         goto end;
0835 
0836     /*
0837      * If select protocol is done on wtx req. do select protocol
0838      * again with default values
0839      */
0840     if (wtx) {
0841         wtx = false;
0842         result = st95hf_handle_wtx(stcontext, false, 0);
0843         if (result)
0844             goto end;
0845     }
0846 
0847     /* call digital layer callback */
0848     cb_arg->complete_cb(stcontext->ddev, cb_arg->cb_usrarg, skb_resp);
0849 
0850     /* up the semaphore before returning */
0851     up(&stcontext->exchange_lock);
0852     mutex_unlock(&stcontext->rm_lock);
0853 
0854     return IRQ_HANDLED;
0855 
0856 end:
0857     kfree_skb(skb_resp);
0858     wtx = false;
0859     cb_arg->rats = false;
0860     skb_resp = ERR_PTR(result);
0861     /* call of callback with error */
0862     cb_arg->complete_cb(stcontext->ddev, cb_arg->cb_usrarg, skb_resp);
0863     /* up the semaphore before returning */
0864     up(&stcontext->exchange_lock);
0865     mutex_unlock(&stcontext->rm_lock);
0866     return IRQ_HANDLED;
0867 }
0868 
0869 /* NFC ops functions definition */
0870 static int st95hf_in_configure_hw(struct nfc_digital_dev *ddev,
0871                   int type,
0872                   int param)
0873 {
0874     struct st95hf_context *stcontext = nfc_digital_get_drvdata(ddev);
0875 
0876     if (type == NFC_DIGITAL_CONFIG_RF_TECH)
0877         return st95hf_select_protocol(stcontext, param);
0878 
0879     if (type == NFC_DIGITAL_CONFIG_FRAMING) {
0880         switch (param) {
0881         case NFC_DIGITAL_FRAMING_NFCA_SHORT:
0882             stcontext->sendrcv_trflag = TRFLAG_NFCA_SHORT_FRAME;
0883             break;
0884         case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
0885             stcontext->sendrcv_trflag = TRFLAG_NFCA_STD_FRAME;
0886             break;
0887         case NFC_DIGITAL_FRAMING_NFCA_T4T:
0888         case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
0889         case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
0890             stcontext->sendrcv_trflag = TRFLAG_NFCA_STD_FRAME_CRC;
0891             break;
0892         case NFC_DIGITAL_FRAMING_NFCB:
0893         case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY:
0894         case NFC_DIGITAL_FRAMING_ISO15693_T5T:
0895             break;
0896         }
0897     }
0898 
0899     return 0;
0900 }
0901 
0902 static int rf_off(struct st95hf_context *stcontext)
0903 {
0904     int rc;
0905     struct device *dev;
0906 
0907     dev = &stcontext->nfcdev->dev;
0908 
0909     rc = st95hf_send_recv_cmd(stcontext, CMD_FIELD_OFF, 0, NULL, true);
0910     if (rc)
0911         dev_err(dev, "protocol sel send field off, err = 0x%x\n", rc);
0912 
0913     return rc;
0914 }
0915 
0916 static int st95hf_in_send_cmd(struct nfc_digital_dev *ddev,
0917                   struct sk_buff *skb,
0918                   u16 timeout,
0919                   nfc_digital_cmd_complete_t cb,
0920                   void *arg)
0921 {
0922     struct st95hf_context *stcontext = nfc_digital_get_drvdata(ddev);
0923     int rc;
0924     struct sk_buff *skb_resp;
0925     int len_data_to_tag = 0;
0926 
0927     skb_resp = nfc_alloc_recv_skb(MAX_RESPONSE_BUFFER_SIZE, GFP_KERNEL);
0928     if (!skb_resp)
0929         return -ENOMEM;
0930 
0931     switch (stcontext->current_rf_tech) {
0932     case NFC_DIGITAL_RF_TECH_106A:
0933         len_data_to_tag = skb->len + 1;
0934         skb_put_u8(skb, stcontext->sendrcv_trflag);
0935         break;
0936     case NFC_DIGITAL_RF_TECH_106B:
0937     case NFC_DIGITAL_RF_TECH_ISO15693:
0938         len_data_to_tag = skb->len;
0939         break;
0940     default:
0941         rc = -EINVAL;
0942         goto free_skb_resp;
0943     }
0944 
0945     skb_push(skb, 3);
0946     skb->data[0] = ST95HF_COMMAND_SEND;
0947     skb->data[1] = SEND_RECEIVE_CMD;
0948     skb->data[2] = len_data_to_tag;
0949 
0950     stcontext->complete_cb_arg.skb_resp = skb_resp;
0951     stcontext->complete_cb_arg.cb_usrarg = arg;
0952     stcontext->complete_cb_arg.complete_cb = cb;
0953 
0954     if ((skb->data[3] == ISO14443A_RATS_REQ) &&
0955         ddev->curr_protocol == NFC_PROTO_ISO14443)
0956         stcontext->complete_cb_arg.rats = true;
0957 
0958     /*
0959      * down the semaphore to indicate to remove func that an
0960      * ISR is pending, note that it will not block here in any case.
0961      * If found blocked, it is a BUG!
0962      */
0963     rc = down_killable(&stcontext->exchange_lock);
0964     if (rc) {
0965         WARN(1, "Semaphore is not found up in st95hf_in_send_cmd\n");
0966         goto free_skb_resp;
0967     }
0968 
0969     rc = st95hf_spi_send(&stcontext->spicontext, skb->data,
0970                  skb->len,
0971                  ASYNC);
0972     if (rc) {
0973         dev_err(&stcontext->nfcdev->dev,
0974             "Error %d trying to perform data_exchange", rc);
0975         /* up the semaphore since ISR will never come in this case */
0976         up(&stcontext->exchange_lock);
0977         goto free_skb_resp;
0978     }
0979 
0980     kfree_skb(skb);
0981 
0982     return rc;
0983 
0984 free_skb_resp:
0985     kfree_skb(skb_resp);
0986     return rc;
0987 }
0988 
0989 /* p2p will be supported in a later release ! */
0990 static int st95hf_tg_configure_hw(struct nfc_digital_dev *ddev,
0991                   int type,
0992                   int param)
0993 {
0994     return 0;
0995 }
0996 
0997 static int st95hf_tg_send_cmd(struct nfc_digital_dev *ddev,
0998                   struct sk_buff *skb,
0999                   u16 timeout,
1000                   nfc_digital_cmd_complete_t cb,
1001                   void *arg)
1002 {
1003     return 0;
1004 }
1005 
1006 static int st95hf_tg_listen(struct nfc_digital_dev *ddev,
1007                 u16 timeout,
1008                 nfc_digital_cmd_complete_t cb,
1009                 void *arg)
1010 {
1011     return 0;
1012 }
1013 
1014 static int st95hf_tg_get_rf_tech(struct nfc_digital_dev *ddev, u8 *rf_tech)
1015 {
1016     return 0;
1017 }
1018 
1019 static int st95hf_switch_rf(struct nfc_digital_dev *ddev, bool on)
1020 {
1021     u8 rf_tech;
1022     struct st95hf_context *stcontext = nfc_digital_get_drvdata(ddev);
1023 
1024     rf_tech = ddev->curr_rf_tech;
1025 
1026     if (on)
1027         /* switch on RF field */
1028         return st95hf_select_protocol(stcontext, rf_tech);
1029 
1030     /* switch OFF RF field */
1031     return rf_off(stcontext);
1032 }
1033 
1034 /* TODO st95hf_abort_cmd */
1035 static void st95hf_abort_cmd(struct nfc_digital_dev *ddev)
1036 {
1037 }
1038 
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,
1042 
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,
1047 
1048     .switch_rf = st95hf_switch_rf,
1049     .abort_cmd = st95hf_abort_cmd,
1050 };
1051 
1052 static const struct spi_device_id st95hf_id[] = {
1053     { "st95hf", 0 },
1054     {}
1055 };
1056 MODULE_DEVICE_TABLE(spi, st95hf_id);
1057 
1058 static const struct of_device_id st95hf_spi_of_match[] __maybe_unused = {
1059     { .compatible = "st,st95hf" },
1060     {},
1061 };
1062 MODULE_DEVICE_TABLE(of, st95hf_spi_of_match);
1063 
1064 static int st95hf_probe(struct spi_device *nfc_spi_dev)
1065 {
1066     int ret;
1067 
1068     struct st95hf_context *st95context;
1069     struct st95hf_spi_context *spicontext;
1070 
1071     nfc_info(&nfc_spi_dev->dev, "ST95HF driver probe called.\n");
1072 
1073     st95context = devm_kzalloc(&nfc_spi_dev->dev,
1074                    sizeof(struct st95hf_context),
1075                    GFP_KERNEL);
1076     if (!st95context)
1077         return -ENOMEM;
1078 
1079     spicontext = &st95context->spicontext;
1080 
1081     spicontext->spidev = nfc_spi_dev;
1082 
1083     st95context->fwi =
1084         cmd_array[CMD_ISO14443A_PROTOCOL_SELECT].cmd_params[2];
1085 
1086     if (device_property_present(&nfc_spi_dev->dev, "st95hfvin")) {
1087         st95context->st95hf_supply =
1088             devm_regulator_get(&nfc_spi_dev->dev,
1089                        "st95hfvin");
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);
1093         }
1094 
1095         ret = regulator_enable(st95context->st95hf_supply);
1096         if (ret) {
1097             dev_err(&nfc_spi_dev->dev, "failed to enable regulator\n");
1098             return ret;
1099         }
1100     }
1101 
1102     init_completion(&spicontext->done);
1103     mutex_init(&spicontext->spi_lock);
1104 
1105     /*
1106      * Store spicontext in spi device object for using it in
1107      * remove function
1108      */
1109     dev_set_drvdata(&nfc_spi_dev->dev, spicontext);
1110 
1111     st95context->enable_gpio =
1112         of_get_named_gpio(nfc_spi_dev->dev.of_node,
1113                   "enable-gpio",
1114                   0);
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;
1119     }
1120 
1121     ret = devm_gpio_request_one(&nfc_spi_dev->dev, st95context->enable_gpio,
1122                     GPIOF_DIR_OUT | GPIOF_INIT_HIGH,
1123                     "enable_gpio");
1124     if (ret)
1125         goto err_disable_regulator;
1126 
1127     if (nfc_spi_dev->irq > 0) {
1128         if (devm_request_threaded_irq(&nfc_spi_dev->dev,
1129                           nfc_spi_dev->irq,
1130                           st95hf_irq_handler,
1131                           st95hf_irq_thread_handler,
1132                           IRQF_TRIGGER_FALLING,
1133                           "st95hf",
1134                           (void *)st95context) < 0) {
1135             dev_err(&nfc_spi_dev->dev, "err: irq request for st95hf is failed\n");
1136             ret =  -EINVAL;
1137             goto err_disable_regulator;
1138         }
1139     } else {
1140         dev_err(&nfc_spi_dev->dev, "not a valid IRQ associated with ST95HF\n");
1141         ret = -EINVAL;
1142         goto err_disable_regulator;
1143     }
1144 
1145     /*
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.
1150      */
1151     ret = st95hf_send_spi_reset_sequence(st95context);
1152     if (ret) {
1153         dev_err(&nfc_spi_dev->dev, "err: spi_reset_sequence failed\n");
1154         goto err_disable_regulator;
1155     }
1156 
1157     /* call PowerOnReset sequence of ST95hf to activate it */
1158     ret = st95hf_por_sequence(st95context);
1159     if (ret) {
1160         dev_err(&nfc_spi_dev->dev, "err: por seq failed for st95hf\n");
1161         goto err_disable_regulator;
1162     }
1163 
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) {
1171         ret = -ENOMEM;
1172         goto err_disable_regulator;
1173     }
1174 
1175     st95context->nfcdev = st95context->ddev->nfc_dev;
1176     nfc_digital_set_parent_dev(st95context->ddev, &nfc_spi_dev->dev);
1177 
1178     ret =  nfc_digital_register_device(st95context->ddev);
1179     if (ret) {
1180         dev_err(&st95context->nfcdev->dev, "st95hf registration failed\n");
1181         goto err_free_digital_device;
1182     }
1183 
1184     /* store st95context in nfc device object */
1185     nfc_digital_set_drvdata(st95context->ddev, st95context);
1186 
1187     sema_init(&st95context->exchange_lock, 1);
1188     mutex_init(&st95context->rm_lock);
1189 
1190     return ret;
1191 
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);
1197 
1198     return ret;
1199 }
1200 
1201 static void st95hf_remove(struct spi_device *nfc_spi_dev)
1202 {
1203     int result = 0;
1204     unsigned char reset_cmd = ST95HF_COMMAND_RESET;
1205     struct st95hf_spi_context *spictx = dev_get_drvdata(&nfc_spi_dev->dev);
1206 
1207     struct st95hf_context *stcontext = container_of(spictx,
1208                             struct st95hf_context,
1209                             spicontext);
1210 
1211     mutex_lock(&stcontext->rm_lock);
1212 
1213     nfc_digital_unregister_device(stcontext->ddev);
1214     nfc_digital_free_device(stcontext->ddev);
1215     stcontext->nfcdev_free = true;
1216 
1217     mutex_unlock(&stcontext->rm_lock);
1218 
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");
1223 
1224     /* next reset the ST95HF controller */
1225     result = st95hf_spi_send(&stcontext->spicontext,
1226                  &reset_cmd,
1227                  ST95HF_RESET_CMD_LEN,
1228                  ASYNC);
1229     if (result)
1230         dev_err(&spictx->spidev->dev,
1231             "ST95HF reset failed in remove() err = %d\n", result);
1232 
1233     /* wait for 3 ms to complete the controller reset process */
1234     usleep_range(3000, 4000);
1235 
1236     /* disable regulator */
1237     if (stcontext->st95hf_supply)
1238         regulator_disable(stcontext->st95hf_supply);
1239 }
1240 
1241 /* Register as SPI protocol driver */
1242 static struct spi_driver st95hf_driver = {
1243     .driver = {
1244         .name = "st95hf",
1245         .owner = THIS_MODULE,
1246         .of_match_table = of_match_ptr(st95hf_spi_of_match),
1247     },
1248     .id_table = st95hf_id,
1249     .probe = st95hf_probe,
1250     .remove = st95hf_remove,
1251 };
1252 
1253 module_spi_driver(st95hf_driver);
1254 
1255 MODULE_AUTHOR("Shikha Singh <shikha.singh@st.com>");
1256 MODULE_DESCRIPTION("ST NFC Transceiver ST95HF driver");
1257 MODULE_LICENSE("GPL v2");