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