dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / nfc / nfcmrvl / spi.c
blobfc8e78a29d77a83feacd0cda380f6cd736e96fff
1 /**
2 * Marvell NFC-over-SPI driver: SPI interface related functions
4 * Copyright (C) 2015, Marvell International Ltd.
6 * This software file (the "File") is distributed by Marvell International
7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8 * (the "License"). You may use, redistribute and/or modify this File in
9 * accordance with the terms and conditions of the License, a copy of which
10 * is available on the worldwide web at
11 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
14 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
15 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
16 * this warranty disclaimer.
17 **/
19 #include <linux/module.h>
20 #include <linux/interrupt.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/nfc.h>
23 #include <linux/gpio.h>
24 #include <linux/of_irq.h>
25 #include <linux/of_gpio.h>
26 #include <net/nfc/nci.h>
27 #include <net/nfc/nci_core.h>
28 #include <linux/spi/spi.h>
29 #include <linux/gpio.h>
30 #include "nfcmrvl.h"
32 #define SPI_WAIT_HANDSHAKE 1
34 struct nfcmrvl_spi_drv_data {
35 unsigned long flags;
36 struct spi_device *spi;
37 struct nci_spi *nci_spi;
38 struct completion handshake_completion;
39 struct nfcmrvl_private *priv;
42 static irqreturn_t nfcmrvl_spi_int_irq_thread_fn(int irq, void *drv_data_ptr)
44 struct nfcmrvl_spi_drv_data *drv_data = drv_data_ptr;
45 struct sk_buff *skb;
48 * Special case where we are waiting for SPI_INT deassertion to start a
49 * transfer.
51 if (test_and_clear_bit(SPI_WAIT_HANDSHAKE, &drv_data->flags)) {
52 complete(&drv_data->handshake_completion);
53 return IRQ_HANDLED;
56 /* Normal case, SPI_INT deasserted by slave to trigger a master read */
58 skb = nci_spi_read(drv_data->nci_spi);
59 if (!skb) {
60 nfc_err(&drv_data->spi->dev, "failed to read spi packet");
61 return IRQ_HANDLED;
64 if (nfcmrvl_nci_recv_frame(drv_data->priv, skb) < 0)
65 nfc_err(&drv_data->spi->dev, "corrupted RX packet");
67 return IRQ_HANDLED;
70 static int nfcmrvl_spi_nci_open(struct nfcmrvl_private *priv)
72 return 0;
75 static int nfcmrvl_spi_nci_close(struct nfcmrvl_private *priv)
77 return 0;
80 static int nfcmrvl_spi_nci_send(struct nfcmrvl_private *priv,
81 struct sk_buff *skb)
83 struct nfcmrvl_spi_drv_data *drv_data = priv->drv_data;
84 int err;
86 /* Reinit completion for slave handshake */
87 reinit_completion(&drv_data->handshake_completion);
88 set_bit(SPI_WAIT_HANDSHAKE, &drv_data->flags);
91 * Append a dummy byte at the end of SPI frame. This is due to a
92 * specific DMA implementation in the controller
94 skb_put(skb, 1);
96 /* Send the SPI packet */
97 err = nci_spi_send(drv_data->nci_spi, &drv_data->handshake_completion,
98 skb);
99 if (err)
100 nfc_err(priv->dev, "spi_send failed %d", err);
102 return err;
105 static void nfcmrvl_spi_nci_update_config(struct nfcmrvl_private *priv,
106 const void *param)
108 struct nfcmrvl_spi_drv_data *drv_data = priv->drv_data;
109 const struct nfcmrvl_fw_spi_config *config = param;
111 drv_data->nci_spi->xfer_speed_hz = config->clk;
114 static struct nfcmrvl_if_ops spi_ops = {
115 .nci_open = nfcmrvl_spi_nci_open,
116 .nci_close = nfcmrvl_spi_nci_close,
117 .nci_send = nfcmrvl_spi_nci_send,
118 .nci_update_config = nfcmrvl_spi_nci_update_config,
121 static int nfcmrvl_spi_parse_dt(struct device_node *node,
122 struct nfcmrvl_platform_data *pdata)
124 int ret;
126 ret = nfcmrvl_parse_dt(node, pdata);
127 if (ret < 0) {
128 pr_err("Failed to get generic entries\n");
129 return ret;
132 ret = irq_of_parse_and_map(node, 0);
133 if (ret < 0) {
134 pr_err("Unable to get irq, error: %d\n", ret);
135 return ret;
137 pdata->irq = ret;
139 return 0;
142 static int nfcmrvl_spi_probe(struct spi_device *spi)
144 struct nfcmrvl_platform_data *pdata;
145 struct nfcmrvl_platform_data config;
146 struct nfcmrvl_spi_drv_data *drv_data;
147 int ret = 0;
149 drv_data = devm_kzalloc(&spi->dev, sizeof(*drv_data), GFP_KERNEL);
150 if (!drv_data)
151 return -ENOMEM;
153 drv_data->spi = spi;
154 drv_data->priv = NULL;
155 spi_set_drvdata(spi, drv_data);
157 pdata = spi->dev.platform_data;
159 if (!pdata && spi->dev.of_node)
160 if (nfcmrvl_spi_parse_dt(spi->dev.of_node, &config) == 0)
161 pdata = &config;
163 if (!pdata)
164 return -EINVAL;
166 ret = devm_request_threaded_irq(&drv_data->spi->dev, pdata->irq,
167 NULL, nfcmrvl_spi_int_irq_thread_fn,
168 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
169 "nfcmrvl_spi_int", drv_data);
170 if (ret < 0) {
171 nfc_err(&drv_data->spi->dev, "Unable to register IRQ handler");
172 return -ENODEV;
175 drv_data->priv = nfcmrvl_nci_register_dev(NFCMRVL_PHY_SPI,
176 drv_data, &spi_ops,
177 &drv_data->spi->dev,
178 pdata);
179 if (IS_ERR(drv_data->priv))
180 return PTR_ERR(drv_data->priv);
182 drv_data->priv->support_fw_dnld = true;
184 drv_data->nci_spi = nci_spi_allocate_spi(drv_data->spi, 0, 10,
185 drv_data->priv->ndev);
187 /* Init completion for slave handshake */
188 init_completion(&drv_data->handshake_completion);
189 return 0;
192 static int nfcmrvl_spi_remove(struct spi_device *spi)
194 struct nfcmrvl_spi_drv_data *drv_data = spi_get_drvdata(spi);
196 nfcmrvl_nci_unregister_dev(drv_data->priv);
197 return 0;
200 static const struct of_device_id of_nfcmrvl_spi_match[] = {
201 { .compatible = "marvell,nfc-spi", },
204 MODULE_DEVICE_TABLE(of, of_nfcmrvl_spi_match);
206 static const struct spi_device_id nfcmrvl_spi_id_table[] = {
207 { "nfcmrvl_spi", 0 },
210 MODULE_DEVICE_TABLE(spi, nfcmrvl_spi_id_table);
212 static struct spi_driver nfcmrvl_spi_driver = {
213 .probe = nfcmrvl_spi_probe,
214 .remove = nfcmrvl_spi_remove,
215 .id_table = nfcmrvl_spi_id_table,
216 .driver = {
217 .name = "nfcmrvl_spi",
218 .owner = THIS_MODULE,
219 .of_match_table = of_match_ptr(of_nfcmrvl_spi_match),
223 module_spi_driver(nfcmrvl_spi_driver);
225 MODULE_AUTHOR("Marvell International Ltd.");
226 MODULE_DESCRIPTION("Marvell NFC-over-SPI driver");
227 MODULE_LICENSE("GPL v2");