Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * SPI Link Layer for ST NCI based Driver
0004  * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
0005  */
0006 
0007 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0008 
0009 #include <linux/module.h>
0010 #include <linux/spi/spi.h>
0011 #include <linux/gpio/consumer.h>
0012 #include <linux/acpi.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/delay.h>
0015 #include <linux/nfc.h>
0016 #include <linux/of.h>
0017 #include <net/nfc/nci.h>
0018 
0019 #include "st-nci.h"
0020 
0021 #define DRIVER_DESC "NCI NFC driver for ST_NCI"
0022 
0023 /* ndlc header */
0024 #define ST_NCI_FRAME_HEADROOM   1
0025 #define ST_NCI_FRAME_TAILROOM   0
0026 
0027 #define ST_NCI_SPI_MIN_SIZE 4   /* PCB(1) + NCI Packet header(3) */
0028 #define ST_NCI_SPI_MAX_SIZE 250 /* req 4.2.1 */
0029 
0030 #define ST_NCI_DRIVER_NAME "st_nci"
0031 #define ST_NCI_SPI_DRIVER_NAME "st_nci_spi"
0032 
0033 struct st_nci_spi_phy {
0034     struct spi_device *spi_dev;
0035     struct llt_ndlc *ndlc;
0036 
0037     bool irq_active;
0038 
0039     struct gpio_desc *gpiod_reset;
0040 
0041     struct st_nci_se_status se_status;
0042 };
0043 
0044 static int st_nci_spi_enable(void *phy_id)
0045 {
0046     struct st_nci_spi_phy *phy = phy_id;
0047 
0048     gpiod_set_value(phy->gpiod_reset, 0);
0049     usleep_range(10000, 15000);
0050     gpiod_set_value(phy->gpiod_reset, 1);
0051     usleep_range(80000, 85000);
0052 
0053     if (phy->ndlc->powered == 0 && phy->irq_active == 0) {
0054         enable_irq(phy->spi_dev->irq);
0055         phy->irq_active = true;
0056     }
0057 
0058     return 0;
0059 }
0060 
0061 static void st_nci_spi_disable(void *phy_id)
0062 {
0063     struct st_nci_spi_phy *phy = phy_id;
0064 
0065     disable_irq_nosync(phy->spi_dev->irq);
0066     phy->irq_active = false;
0067 }
0068 
0069 /*
0070  * Writing a frame must not return the number of written bytes.
0071  * It must return either zero for success, or <0 for error.
0072  * In addition, it must not alter the skb
0073  */
0074 static int st_nci_spi_write(void *phy_id, struct sk_buff *skb)
0075 {
0076     int r;
0077     struct st_nci_spi_phy *phy = phy_id;
0078     struct spi_device *dev = phy->spi_dev;
0079     struct sk_buff *skb_rx;
0080     u8 buf[ST_NCI_SPI_MAX_SIZE + NCI_DATA_HDR_SIZE +
0081            ST_NCI_FRAME_HEADROOM + ST_NCI_FRAME_TAILROOM];
0082     struct spi_transfer spi_xfer = {
0083         .tx_buf = skb->data,
0084         .rx_buf = buf,
0085         .len = skb->len,
0086     };
0087 
0088     if (phy->ndlc->hard_fault != 0)
0089         return phy->ndlc->hard_fault;
0090 
0091     r = spi_sync_transfer(dev, &spi_xfer, 1);
0092     /*
0093      * We may have received some valuable data on miso line.
0094      * Send them back in the ndlc state machine.
0095      */
0096     if (!r) {
0097         skb_rx = alloc_skb(skb->len, GFP_KERNEL);
0098         if (!skb_rx)
0099             return -ENOMEM;
0100 
0101         skb_put(skb_rx, skb->len);
0102         memcpy(skb_rx->data, buf, skb->len);
0103         ndlc_recv(phy->ndlc, skb_rx);
0104     }
0105 
0106     return r;
0107 }
0108 
0109 /*
0110  * Reads an ndlc frame and returns it in a newly allocated sk_buff.
0111  * returns:
0112  * 0 : if received frame is complete
0113  * -EREMOTEIO : i2c read error (fatal)
0114  * -EBADMSG : frame was incorrect and discarded
0115  * -ENOMEM : cannot allocate skb, frame dropped
0116  */
0117 static int st_nci_spi_read(struct st_nci_spi_phy *phy,
0118             struct sk_buff **skb)
0119 {
0120     int r;
0121     u8 len;
0122     u8 buf[ST_NCI_SPI_MAX_SIZE];
0123     struct spi_device *dev = phy->spi_dev;
0124     struct spi_transfer spi_xfer = {
0125         .rx_buf = buf,
0126         .len = ST_NCI_SPI_MIN_SIZE,
0127     };
0128 
0129     r = spi_sync_transfer(dev, &spi_xfer, 1);
0130     if (r < 0)
0131         return -EREMOTEIO;
0132 
0133     len = be16_to_cpu(*(__be16 *) (buf + 2));
0134     if (len > ST_NCI_SPI_MAX_SIZE) {
0135         nfc_err(&dev->dev, "invalid frame len\n");
0136         phy->ndlc->hard_fault = 1;
0137         return -EBADMSG;
0138     }
0139 
0140     *skb = alloc_skb(ST_NCI_SPI_MIN_SIZE + len, GFP_KERNEL);
0141     if (*skb == NULL)
0142         return -ENOMEM;
0143 
0144     skb_reserve(*skb, ST_NCI_SPI_MIN_SIZE);
0145     skb_put(*skb, ST_NCI_SPI_MIN_SIZE);
0146     memcpy((*skb)->data, buf, ST_NCI_SPI_MIN_SIZE);
0147 
0148     if (!len)
0149         return 0;
0150 
0151     spi_xfer.len = len;
0152     r = spi_sync_transfer(dev, &spi_xfer, 1);
0153     if (r < 0) {
0154         kfree_skb(*skb);
0155         return -EREMOTEIO;
0156     }
0157 
0158     skb_put(*skb, len);
0159     memcpy((*skb)->data + ST_NCI_SPI_MIN_SIZE, buf, len);
0160 
0161     return 0;
0162 }
0163 
0164 /*
0165  * Reads an ndlc frame from the chip.
0166  *
0167  * On ST21NFCB, IRQ goes in idle state when read starts.
0168  */
0169 static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
0170 {
0171     struct st_nci_spi_phy *phy = phy_id;
0172     struct sk_buff *skb = NULL;
0173     int r;
0174 
0175     if (!phy || !phy->ndlc || irq != phy->spi_dev->irq) {
0176         WARN_ON_ONCE(1);
0177         return IRQ_NONE;
0178     }
0179 
0180     if (phy->ndlc->hard_fault)
0181         return IRQ_HANDLED;
0182 
0183     if (!phy->ndlc->powered) {
0184         st_nci_spi_disable(phy);
0185         return IRQ_HANDLED;
0186     }
0187 
0188     r = st_nci_spi_read(phy, &skb);
0189     if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
0190         return IRQ_HANDLED;
0191 
0192     ndlc_recv(phy->ndlc, skb);
0193 
0194     return IRQ_HANDLED;
0195 }
0196 
0197 static const struct nfc_phy_ops spi_phy_ops = {
0198     .write = st_nci_spi_write,
0199     .enable = st_nci_spi_enable,
0200     .disable = st_nci_spi_disable,
0201 };
0202 
0203 static const struct acpi_gpio_params reset_gpios = { 1, 0, false };
0204 
0205 static const struct acpi_gpio_mapping acpi_st_nci_gpios[] = {
0206     { "reset-gpios", &reset_gpios, 1 },
0207     {},
0208 };
0209 
0210 static int st_nci_spi_probe(struct spi_device *dev)
0211 {
0212     struct st_nci_spi_phy *phy;
0213     int r;
0214 
0215     /* Check SPI platform functionnalities */
0216     if (!dev) {
0217         pr_debug("%s: dev is NULL. Device is not accessible.\n",
0218             __func__);
0219         return -ENODEV;
0220     }
0221 
0222     phy = devm_kzalloc(&dev->dev, sizeof(struct st_nci_spi_phy),
0223                GFP_KERNEL);
0224     if (!phy)
0225         return -ENOMEM;
0226 
0227     phy->spi_dev = dev;
0228 
0229     spi_set_drvdata(dev, phy);
0230 
0231     r = devm_acpi_dev_add_driver_gpios(&dev->dev, acpi_st_nci_gpios);
0232     if (r)
0233         dev_dbg(&dev->dev, "Unable to add GPIO mapping table\n");
0234 
0235     /* Get RESET GPIO */
0236     phy->gpiod_reset = devm_gpiod_get(&dev->dev, "reset", GPIOD_OUT_HIGH);
0237     if (IS_ERR(phy->gpiod_reset)) {
0238         nfc_err(&dev->dev, "Unable to get RESET GPIO\n");
0239         return PTR_ERR(phy->gpiod_reset);
0240     }
0241 
0242     phy->se_status.is_ese_present =
0243             device_property_read_bool(&dev->dev, "ese-present");
0244     phy->se_status.is_uicc_present =
0245             device_property_read_bool(&dev->dev, "uicc-present");
0246 
0247     r = ndlc_probe(phy, &spi_phy_ops, &dev->dev,
0248             ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
0249             &phy->ndlc, &phy->se_status);
0250     if (r < 0) {
0251         nfc_err(&dev->dev, "Unable to register ndlc layer\n");
0252         return r;
0253     }
0254 
0255     phy->irq_active = true;
0256     r = devm_request_threaded_irq(&dev->dev, dev->irq, NULL,
0257                 st_nci_irq_thread_fn,
0258                 IRQF_ONESHOT,
0259                 ST_NCI_SPI_DRIVER_NAME, phy);
0260     if (r < 0)
0261         nfc_err(&dev->dev, "Unable to register IRQ handler\n");
0262 
0263     return r;
0264 }
0265 
0266 static void st_nci_spi_remove(struct spi_device *dev)
0267 {
0268     struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
0269 
0270     ndlc_remove(phy->ndlc);
0271 }
0272 
0273 static struct spi_device_id st_nci_spi_id_table[] = {
0274     {ST_NCI_SPI_DRIVER_NAME, 0},
0275     {"st21nfcb-spi", 0},
0276     {}
0277 };
0278 MODULE_DEVICE_TABLE(spi, st_nci_spi_id_table);
0279 
0280 static const struct acpi_device_id st_nci_spi_acpi_match[] __maybe_unused = {
0281     {"SMO2101", 0},
0282     {}
0283 };
0284 MODULE_DEVICE_TABLE(acpi, st_nci_spi_acpi_match);
0285 
0286 static const struct of_device_id of_st_nci_spi_match[] __maybe_unused = {
0287     { .compatible = "st,st21nfcb-spi", },
0288     {}
0289 };
0290 MODULE_DEVICE_TABLE(of, of_st_nci_spi_match);
0291 
0292 static struct spi_driver st_nci_spi_driver = {
0293     .driver = {
0294         .name = ST_NCI_SPI_DRIVER_NAME,
0295         .of_match_table = of_match_ptr(of_st_nci_spi_match),
0296         .acpi_match_table = ACPI_PTR(st_nci_spi_acpi_match),
0297     },
0298     .probe = st_nci_spi_probe,
0299     .id_table = st_nci_spi_id_table,
0300     .remove = st_nci_spi_remove,
0301 };
0302 module_spi_driver(st_nci_spi_driver);
0303 
0304 MODULE_LICENSE("GPL");
0305 MODULE_DESCRIPTION(DRIVER_DESC);