1be3d162aSKrzysztof Kozlowski // SPDX-License-Identifier: GPL-2.0-only 24dd649d1SAditya Srivastava /* 3caf6e49bSVincent Cuissard * Marvell NFC-over-SPI driver: SPI interface related functions 4caf6e49bSVincent Cuissard * 5caf6e49bSVincent Cuissard * Copyright (C) 2015, Marvell International Ltd. 6be3d162aSKrzysztof Kozlowski */ 7caf6e49bSVincent Cuissard 8caf6e49bSVincent Cuissard #include <linux/module.h> 9caf6e49bSVincent Cuissard #include <linux/interrupt.h> 10caf6e49bSVincent Cuissard #include <linux/nfc.h> 11caf6e49bSVincent Cuissard #include <linux/of_irq.h> 12caf6e49bSVincent Cuissard #include <net/nfc/nci.h> 13caf6e49bSVincent Cuissard #include <net/nfc/nci_core.h> 14caf6e49bSVincent Cuissard #include <linux/spi/spi.h> 15caf6e49bSVincent Cuissard #include "nfcmrvl.h" 16caf6e49bSVincent Cuissard 17caf6e49bSVincent Cuissard #define SPI_WAIT_HANDSHAKE 1 18caf6e49bSVincent Cuissard 19caf6e49bSVincent Cuissard struct nfcmrvl_spi_drv_data { 20caf6e49bSVincent Cuissard unsigned long flags; 21caf6e49bSVincent Cuissard struct spi_device *spi; 22caf6e49bSVincent Cuissard struct nci_spi *nci_spi; 23caf6e49bSVincent Cuissard struct completion handshake_completion; 24caf6e49bSVincent Cuissard struct nfcmrvl_private *priv; 25caf6e49bSVincent Cuissard }; 26caf6e49bSVincent Cuissard 27caf6e49bSVincent Cuissard static irqreturn_t nfcmrvl_spi_int_irq_thread_fn(int irq, void *drv_data_ptr) 28caf6e49bSVincent Cuissard { 29caf6e49bSVincent Cuissard struct nfcmrvl_spi_drv_data *drv_data = drv_data_ptr; 30caf6e49bSVincent Cuissard struct sk_buff *skb; 31caf6e49bSVincent Cuissard 32caf6e49bSVincent Cuissard /* 33caf6e49bSVincent Cuissard * Special case where we are waiting for SPI_INT deassertion to start a 34caf6e49bSVincent Cuissard * transfer. 35caf6e49bSVincent Cuissard */ 36caf6e49bSVincent Cuissard if (test_and_clear_bit(SPI_WAIT_HANDSHAKE, &drv_data->flags)) { 37caf6e49bSVincent Cuissard complete(&drv_data->handshake_completion); 38caf6e49bSVincent Cuissard return IRQ_HANDLED; 39caf6e49bSVincent Cuissard } 40caf6e49bSVincent Cuissard 41caf6e49bSVincent Cuissard /* Normal case, SPI_INT deasserted by slave to trigger a master read */ 42caf6e49bSVincent Cuissard 43caf6e49bSVincent Cuissard skb = nci_spi_read(drv_data->nci_spi); 44caf6e49bSVincent Cuissard if (!skb) { 45caf6e49bSVincent Cuissard nfc_err(&drv_data->spi->dev, "failed to read spi packet"); 46caf6e49bSVincent Cuissard return IRQ_HANDLED; 47caf6e49bSVincent Cuissard } 48caf6e49bSVincent Cuissard 49caf6e49bSVincent Cuissard if (nfcmrvl_nci_recv_frame(drv_data->priv, skb) < 0) 50caf6e49bSVincent Cuissard nfc_err(&drv_data->spi->dev, "corrupted RX packet"); 51caf6e49bSVincent Cuissard 52caf6e49bSVincent Cuissard return IRQ_HANDLED; 53caf6e49bSVincent Cuissard } 54caf6e49bSVincent Cuissard 55caf6e49bSVincent Cuissard static int nfcmrvl_spi_nci_open(struct nfcmrvl_private *priv) 56caf6e49bSVincent Cuissard { 57caf6e49bSVincent Cuissard return 0; 58caf6e49bSVincent Cuissard } 59caf6e49bSVincent Cuissard 60caf6e49bSVincent Cuissard static int nfcmrvl_spi_nci_close(struct nfcmrvl_private *priv) 61caf6e49bSVincent Cuissard { 62caf6e49bSVincent Cuissard return 0; 63caf6e49bSVincent Cuissard } 64caf6e49bSVincent Cuissard 65caf6e49bSVincent Cuissard static int nfcmrvl_spi_nci_send(struct nfcmrvl_private *priv, 66caf6e49bSVincent Cuissard struct sk_buff *skb) 67caf6e49bSVincent Cuissard { 68caf6e49bSVincent Cuissard struct nfcmrvl_spi_drv_data *drv_data = priv->drv_data; 69caf6e49bSVincent Cuissard int err; 70caf6e49bSVincent Cuissard 71caf6e49bSVincent Cuissard /* Reinit completion for slave handshake */ 72caf6e49bSVincent Cuissard reinit_completion(&drv_data->handshake_completion); 73caf6e49bSVincent Cuissard set_bit(SPI_WAIT_HANDSHAKE, &drv_data->flags); 74caf6e49bSVincent Cuissard 75caf6e49bSVincent Cuissard /* 76caf6e49bSVincent Cuissard * Append a dummy byte at the end of SPI frame. This is due to a 77caf6e49bSVincent Cuissard * specific DMA implementation in the controller 78caf6e49bSVincent Cuissard */ 79caf6e49bSVincent Cuissard skb_put(skb, 1); 80caf6e49bSVincent Cuissard 81caf6e49bSVincent Cuissard /* Send the SPI packet */ 82caf6e49bSVincent Cuissard err = nci_spi_send(drv_data->nci_spi, &drv_data->handshake_completion, 83caf6e49bSVincent Cuissard skb); 84ca42fb9eSDan Carpenter if (err) 85caf6e49bSVincent Cuissard nfc_err(priv->dev, "spi_send failed %d", err); 86ca42fb9eSDan Carpenter 87caf6e49bSVincent Cuissard return err; 88caf6e49bSVincent Cuissard } 89caf6e49bSVincent Cuissard 90caf6e49bSVincent Cuissard static void nfcmrvl_spi_nci_update_config(struct nfcmrvl_private *priv, 91caf6e49bSVincent Cuissard const void *param) 92caf6e49bSVincent Cuissard { 93caf6e49bSVincent Cuissard struct nfcmrvl_spi_drv_data *drv_data = priv->drv_data; 94caf6e49bSVincent Cuissard const struct nfcmrvl_fw_spi_config *config = param; 95caf6e49bSVincent Cuissard 96caf6e49bSVincent Cuissard drv_data->nci_spi->xfer_speed_hz = config->clk; 97caf6e49bSVincent Cuissard } 98caf6e49bSVincent Cuissard 9926955037SKrzysztof Kozlowski static const struct nfcmrvl_if_ops spi_ops = { 100caf6e49bSVincent Cuissard .nci_open = nfcmrvl_spi_nci_open, 101caf6e49bSVincent Cuissard .nci_close = nfcmrvl_spi_nci_close, 102caf6e49bSVincent Cuissard .nci_send = nfcmrvl_spi_nci_send, 103caf6e49bSVincent Cuissard .nci_update_config = nfcmrvl_spi_nci_update_config, 104caf6e49bSVincent Cuissard }; 105caf6e49bSVincent Cuissard 1063833b874SKrzysztof Kozlowski static int nfcmrvl_spi_parse_dt(struct device_node *node, 107caf6e49bSVincent Cuissard struct nfcmrvl_platform_data *pdata) 108caf6e49bSVincent Cuissard { 109caf6e49bSVincent Cuissard int ret; 110caf6e49bSVincent Cuissard 111caf6e49bSVincent Cuissard ret = nfcmrvl_parse_dt(node, pdata); 112caf6e49bSVincent Cuissard if (ret < 0) { 113caf6e49bSVincent Cuissard pr_err("Failed to get generic entries\n"); 114caf6e49bSVincent Cuissard return ret; 115caf6e49bSVincent Cuissard } 116caf6e49bSVincent Cuissard 117caf6e49bSVincent Cuissard ret = irq_of_parse_and_map(node, 0); 118caf6e49bSVincent Cuissard if (ret < 0) { 119caf6e49bSVincent Cuissard pr_err("Unable to get irq, error: %d\n", ret); 120caf6e49bSVincent Cuissard return ret; 121caf6e49bSVincent Cuissard } 122caf6e49bSVincent Cuissard pdata->irq = ret; 123caf6e49bSVincent Cuissard 124caf6e49bSVincent Cuissard return 0; 125caf6e49bSVincent Cuissard } 126caf6e49bSVincent Cuissard 127caf6e49bSVincent Cuissard static int nfcmrvl_spi_probe(struct spi_device *spi) 128caf6e49bSVincent Cuissard { 129fe53159fSKrzysztof Kozlowski const struct nfcmrvl_platform_data *pdata; 130caf6e49bSVincent Cuissard struct nfcmrvl_platform_data config; 131caf6e49bSVincent Cuissard struct nfcmrvl_spi_drv_data *drv_data; 132caf6e49bSVincent Cuissard int ret = 0; 133caf6e49bSVincent Cuissard 134caf6e49bSVincent Cuissard drv_data = devm_kzalloc(&spi->dev, sizeof(*drv_data), GFP_KERNEL); 135caf6e49bSVincent Cuissard if (!drv_data) 136caf6e49bSVincent Cuissard return -ENOMEM; 137caf6e49bSVincent Cuissard 138caf6e49bSVincent Cuissard drv_data->spi = spi; 139caf6e49bSVincent Cuissard drv_data->priv = NULL; 140caf6e49bSVincent Cuissard spi_set_drvdata(spi, drv_data); 141caf6e49bSVincent Cuissard 142caf6e49bSVincent Cuissard pdata = spi->dev.platform_data; 143caf6e49bSVincent Cuissard 144caf6e49bSVincent Cuissard if (!pdata && spi->dev.of_node) 145caf6e49bSVincent Cuissard if (nfcmrvl_spi_parse_dt(spi->dev.of_node, &config) == 0) 146caf6e49bSVincent Cuissard pdata = &config; 147caf6e49bSVincent Cuissard 148caf6e49bSVincent Cuissard if (!pdata) 149caf6e49bSVincent Cuissard return -EINVAL; 150caf6e49bSVincent Cuissard 151caf6e49bSVincent Cuissard ret = devm_request_threaded_irq(&drv_data->spi->dev, pdata->irq, 152caf6e49bSVincent Cuissard NULL, nfcmrvl_spi_int_irq_thread_fn, 153caf6e49bSVincent Cuissard IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 154caf6e49bSVincent Cuissard "nfcmrvl_spi_int", drv_data); 155caf6e49bSVincent Cuissard if (ret < 0) { 156caf6e49bSVincent Cuissard nfc_err(&drv_data->spi->dev, "Unable to register IRQ handler"); 157caf6e49bSVincent Cuissard return -ENODEV; 158caf6e49bSVincent Cuissard } 159caf6e49bSVincent Cuissard 160caf6e49bSVincent Cuissard drv_data->priv = nfcmrvl_nci_register_dev(NFCMRVL_PHY_SPI, 161caf6e49bSVincent Cuissard drv_data, &spi_ops, 162caf6e49bSVincent Cuissard &drv_data->spi->dev, 163caf6e49bSVincent Cuissard pdata); 164caf6e49bSVincent Cuissard if (IS_ERR(drv_data->priv)) 165caf6e49bSVincent Cuissard return PTR_ERR(drv_data->priv); 166caf6e49bSVincent Cuissard 167caf6e49bSVincent Cuissard drv_data->priv->support_fw_dnld = true; 168caf6e49bSVincent Cuissard 169caf6e49bSVincent Cuissard drv_data->nci_spi = nci_spi_allocate_spi(drv_data->spi, 0, 10, 170caf6e49bSVincent Cuissard drv_data->priv->ndev); 171caf6e49bSVincent Cuissard 172caf6e49bSVincent Cuissard /* Init completion for slave handshake */ 173caf6e49bSVincent Cuissard init_completion(&drv_data->handshake_completion); 174caf6e49bSVincent Cuissard return 0; 175caf6e49bSVincent Cuissard } 176caf6e49bSVincent Cuissard 177*a0386bbaSUwe Kleine-König static void nfcmrvl_spi_remove(struct spi_device *spi) 178caf6e49bSVincent Cuissard { 179caf6e49bSVincent Cuissard struct nfcmrvl_spi_drv_data *drv_data = spi_get_drvdata(spi); 180caf6e49bSVincent Cuissard 181caf6e49bSVincent Cuissard nfcmrvl_nci_unregister_dev(drv_data->priv); 182caf6e49bSVincent Cuissard } 183caf6e49bSVincent Cuissard 18426f20ff5SKrzysztof Kozlowski static const struct of_device_id of_nfcmrvl_spi_match[] __maybe_unused = { 185d8e018c0SVincent Cuissard { .compatible = "marvell,nfc-spi", }, 186caf6e49bSVincent Cuissard {}, 187caf6e49bSVincent Cuissard }; 188caf6e49bSVincent Cuissard MODULE_DEVICE_TABLE(of, of_nfcmrvl_spi_match); 189caf6e49bSVincent Cuissard 190caf6e49bSVincent Cuissard static const struct spi_device_id nfcmrvl_spi_id_table[] = { 191caf6e49bSVincent Cuissard { "nfcmrvl_spi", 0 }, 192caf6e49bSVincent Cuissard { } 193caf6e49bSVincent Cuissard }; 194caf6e49bSVincent Cuissard MODULE_DEVICE_TABLE(spi, nfcmrvl_spi_id_table); 195caf6e49bSVincent Cuissard 196caf6e49bSVincent Cuissard static struct spi_driver nfcmrvl_spi_driver = { 197caf6e49bSVincent Cuissard .probe = nfcmrvl_spi_probe, 198caf6e49bSVincent Cuissard .remove = nfcmrvl_spi_remove, 199caf6e49bSVincent Cuissard .id_table = nfcmrvl_spi_id_table, 200caf6e49bSVincent Cuissard .driver = { 201caf6e49bSVincent Cuissard .name = "nfcmrvl_spi", 202caf6e49bSVincent Cuissard .owner = THIS_MODULE, 203caf6e49bSVincent Cuissard .of_match_table = of_match_ptr(of_nfcmrvl_spi_match), 204caf6e49bSVincent Cuissard }, 205caf6e49bSVincent Cuissard }; 206caf6e49bSVincent Cuissard 207caf6e49bSVincent Cuissard module_spi_driver(nfcmrvl_spi_driver); 208caf6e49bSVincent Cuissard 209caf6e49bSVincent Cuissard MODULE_AUTHOR("Marvell International Ltd."); 210caf6e49bSVincent Cuissard MODULE_DESCRIPTION("Marvell NFC-over-SPI driver"); 211caf6e49bSVincent Cuissard MODULE_LICENSE("GPL v2"); 212