blob: 51c8240a1672a1a329d40b4cb97dbf3582e48f83 [file] [log] [blame]
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -08001/*
2 * Marvell NFC driver: major functions
3 *
Vincent Cuissardfb101c02015-10-26 10:27:37 +01004 * Copyright (C) 2014-2015 Marvell International Ltd.
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -08005 *
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.
12 *
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 */
18
19#include <linux/module.h>
Vincent Cuissard4a2b9472015-06-11 11:25:46 +020020#include <linux/gpio.h>
21#include <linux/delay.h>
Vincent Cuissarddc14bde2015-06-11 14:00:19 +020022#include <linux/of_gpio.h>
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -080023#include <linux/nfc.h>
24#include <net/nfc/nci.h>
25#include <net/nfc/nci_core.h>
26#include "nfcmrvl.h"
27
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -080028static int nfcmrvl_nci_open(struct nci_dev *ndev)
29{
30 struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
31 int err;
32
33 if (test_and_set_bit(NFCMRVL_NCI_RUNNING, &priv->flags))
34 return 0;
35
Vincent Cuissardb5b3e232015-10-26 10:27:41 +010036 /* Reset possible fault of previous session */
37 clear_bit(NFCMRVL_PHY_ERROR, &priv->flags);
38
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -080039 err = priv->if_ops->nci_open(priv);
40
41 if (err)
42 clear_bit(NFCMRVL_NCI_RUNNING, &priv->flags);
43
44 return err;
45}
46
47static int nfcmrvl_nci_close(struct nci_dev *ndev)
48{
49 struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
50
51 if (!test_and_clear_bit(NFCMRVL_NCI_RUNNING, &priv->flags))
52 return 0;
53
54 priv->if_ops->nci_close(priv);
55
56 return 0;
57}
58
59static int nfcmrvl_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
60{
61 struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
62
63 nfc_info(priv->dev, "send entry, len %d\n", skb->len);
64
65 skb->dev = (void *)ndev;
66
Vincent Cuissarddc14bde2015-06-11 14:00:19 +020067 if (priv->config.hci_muxed) {
Vincent Cuissardf1f1a7d2015-06-11 11:25:43 +020068 unsigned char *hdr;
69 unsigned char len = skb->len;
70
71 hdr = (char *) skb_push(skb, NFCMRVL_HCI_EVENT_HEADER_SIZE);
72 hdr[0] = NFCMRVL_HCI_COMMAND_CODE;
73 hdr[1] = NFCMRVL_HCI_OGF;
74 hdr[2] = NFCMRVL_HCI_OCF;
75 hdr[3] = len;
76 }
77
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -080078 return priv->if_ops->nci_send(priv, skb);
79}
80
Amitkumar Karwar15203b42014-01-06 12:58:20 -080081static int nfcmrvl_nci_setup(struct nci_dev *ndev)
82{
Vincent Cuissardd0dcad82015-06-12 15:35:54 +020083 __u8 val = 1;
84
85 nci_set_config(ndev, NFCMRVL_PB_BAIL_OUT, 1, &val);
Amitkumar Karwar15203b42014-01-06 12:58:20 -080086 return 0;
87}
88
Vincent Cuissard3194c682015-10-26 10:27:39 +010089static int nfcmrvl_nci_fw_download(struct nci_dev *ndev,
90 const char *firmware_name)
91{
92 return nfcmrvl_fw_dnld_start(ndev, firmware_name);
93}
94
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -080095static struct nci_ops nfcmrvl_nci_ops = {
96 .open = nfcmrvl_nci_open,
97 .close = nfcmrvl_nci_close,
98 .send = nfcmrvl_nci_send,
Amitkumar Karwar15203b42014-01-06 12:58:20 -080099 .setup = nfcmrvl_nci_setup,
Vincent Cuissard3194c682015-10-26 10:27:39 +0100100 .fw_download = nfcmrvl_nci_fw_download,
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -0800101};
102
Vincent Cuissard58d34aa2015-10-26 10:27:40 +0100103struct nfcmrvl_private *nfcmrvl_nci_register_dev(enum nfcmrvl_phy phy,
104 void *drv_data,
Vincent Cuissarddc14bde2015-06-11 14:00:19 +0200105 struct nfcmrvl_if_ops *ops,
106 struct device *dev,
107 struct nfcmrvl_platform_data *pdata)
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -0800108{
109 struct nfcmrvl_private *priv;
110 int rc;
Vincent Cuissard58d34aa2015-10-26 10:27:40 +0100111 int headroom;
112 int tailroom;
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -0800113 u32 protocols;
114
115 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
116 if (!priv)
117 return ERR_PTR(-ENOMEM);
118
119 priv->drv_data = drv_data;
120 priv->if_ops = ops;
121 priv->dev = dev;
Vincent Cuissard58d34aa2015-10-26 10:27:40 +0100122 priv->phy = phy;
Vincent Cuissard4a2b9472015-06-11 11:25:46 +0200123
Vincent Cuissarddc14bde2015-06-11 14:00:19 +0200124 memcpy(&priv->config, pdata, sizeof(*pdata));
125
126 if (priv->config.reset_n_io) {
Vincent Cuissard4a2b9472015-06-11 11:25:46 +0200127 rc = devm_gpio_request_one(dev,
Vincent Cuissarddc14bde2015-06-11 14:00:19 +0200128 priv->config.reset_n_io,
Vincent Cuissard4a2b9472015-06-11 11:25:46 +0200129 GPIOF_OUT_INIT_LOW,
130 "nfcmrvl_reset_n");
131 if (rc < 0)
132 nfc_err(dev, "failed to request reset_n io\n");
133 }
Vincent Cuissardf1f1a7d2015-06-11 11:25:43 +0200134
Vincent Cuissardcaf6e492015-10-26 10:27:44 +0100135 if (phy == NFCMRVL_PHY_SPI) {
136 headroom = NCI_SPI_HDR_LEN;
137 tailroom = 1;
138 } else
139 headroom = tailroom = 0;
Vincent Cuissard58d34aa2015-10-26 10:27:40 +0100140
Vincent Cuissarddc14bde2015-06-11 14:00:19 +0200141 if (priv->config.hci_muxed)
Vincent Cuissard58d34aa2015-10-26 10:27:40 +0100142 headroom += NFCMRVL_HCI_EVENT_HEADER_SIZE;
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -0800143
144 protocols = NFC_PROTO_JEWEL_MASK
Vincent Cuissarddc14bde2015-06-11 14:00:19 +0200145 | NFC_PROTO_MIFARE_MASK
146 | NFC_PROTO_FELICA_MASK
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -0800147 | NFC_PROTO_ISO14443_MASK
148 | NFC_PROTO_ISO14443_B_MASK
Vincent Cuissard83d56722015-06-12 15:35:52 +0200149 | NFC_PROTO_ISO15693_MASK
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -0800150 | NFC_PROTO_NFC_DEP_MASK;
151
Vincent Cuissardf1f1a7d2015-06-11 11:25:43 +0200152 priv->ndev = nci_allocate_device(&nfcmrvl_nci_ops, protocols,
Vincent Cuissard58d34aa2015-10-26 10:27:40 +0100153 headroom, tailroom);
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -0800154 if (!priv->ndev) {
Joe Perches3590ebc2015-04-07 00:17:00 -0700155 nfc_err(dev, "nci_allocate_device failed\n");
Amitkumar Karwarbb55dc22014-01-08 10:52:27 -0800156 rc = -ENOMEM;
157 goto error;
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -0800158 }
159
160 nci_set_drvdata(priv->ndev, priv);
161
162 rc = nci_register_device(priv->ndev);
163 if (rc) {
Joe Perches3590ebc2015-04-07 00:17:00 -0700164 nfc_err(dev, "nci_register_device failed %d\n", rc);
Vincent Cuissard3194c682015-10-26 10:27:39 +0100165 goto error_free_dev;
166 }
167
168 /* Ensure that controller is powered off */
169 nfcmrvl_chip_halt(priv);
170
171 rc = nfcmrvl_fw_dnld_init(priv);
172 if (rc) {
173 nfc_err(dev, "failed to initialize FW download %d\n", rc);
174 goto error_free_dev;
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -0800175 }
176
177 nfc_info(dev, "registered with nci successfully\n");
178 return priv;
Amitkumar Karwarbb55dc22014-01-08 10:52:27 -0800179
Vincent Cuissard3194c682015-10-26 10:27:39 +0100180error_free_dev:
181 nci_free_device(priv->ndev);
Amitkumar Karwarbb55dc22014-01-08 10:52:27 -0800182error:
183 kfree(priv);
184 return ERR_PTR(rc);
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -0800185}
186EXPORT_SYMBOL_GPL(nfcmrvl_nci_register_dev);
187
188void nfcmrvl_nci_unregister_dev(struct nfcmrvl_private *priv)
189{
190 struct nci_dev *ndev = priv->ndev;
191
Vincent Cuissard3194c682015-10-26 10:27:39 +0100192 if (priv->ndev->nfc_dev->fw_download_in_progress)
193 nfcmrvl_fw_dnld_abort(priv);
194
195 nfcmrvl_fw_dnld_deinit(priv);
196
Vincent Cuissardb2fe2882015-11-03 19:19:34 +0100197 if (priv->config.reset_n_io)
198 devm_gpio_free(priv->dev, priv->config.reset_n_io);
199
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -0800200 nci_unregister_device(ndev);
201 nci_free_device(ndev);
202 kfree(priv);
203}
204EXPORT_SYMBOL_GPL(nfcmrvl_nci_unregister_dev);
205
Vincent Cuissarde1bf80c2015-06-11 11:25:44 +0200206int nfcmrvl_nci_recv_frame(struct nfcmrvl_private *priv, struct sk_buff *skb)
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -0800207{
Vincent Cuissarddc14bde2015-06-11 14:00:19 +0200208 if (priv->config.hci_muxed) {
Vincent Cuissardf1f1a7d2015-06-11 11:25:43 +0200209 if (skb->data[0] == NFCMRVL_HCI_EVENT_CODE &&
210 skb->data[1] == NFCMRVL_HCI_NFC_EVENT_CODE) {
211 /* Data packet, let's extract NCI payload */
212 skb_pull(skb, NFCMRVL_HCI_EVENT_HEADER_SIZE);
213 } else {
214 /* Skip this packet */
215 kfree_skb(skb);
216 return 0;
217 }
218 }
219
Vincent Cuissard3194c682015-10-26 10:27:39 +0100220 if (priv->ndev->nfc_dev->fw_download_in_progress) {
221 nfcmrvl_fw_dnld_recv_frame(priv, skb);
222 return 0;
223 }
224
Vincent Cuissarde1bf80c2015-06-11 11:25:44 +0200225 if (test_bit(NFCMRVL_NCI_RUNNING, &priv->flags))
226 nci_recv_frame(priv->ndev, skb);
227 else {
228 /* Drop this packet since nobody wants it */
229 kfree_skb(skb);
230 return 0;
231 }
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -0800232
Vincent Cuissarde1bf80c2015-06-11 11:25:44 +0200233 return 0;
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -0800234}
235EXPORT_SYMBOL_GPL(nfcmrvl_nci_recv_frame);
236
Vincent Cuissard4a2b9472015-06-11 11:25:46 +0200237void nfcmrvl_chip_reset(struct nfcmrvl_private *priv)
238{
Vincent Cuissardb5b3e232015-10-26 10:27:41 +0100239 /* Reset possible fault of previous session */
240 clear_bit(NFCMRVL_PHY_ERROR, &priv->flags);
Vincent Cuissard4a2b9472015-06-11 11:25:46 +0200241
Vincent Cuissarddc14bde2015-06-11 14:00:19 +0200242 if (priv->config.reset_n_io) {
Vincent Cuissard4a2b9472015-06-11 11:25:46 +0200243 nfc_info(priv->dev, "reset the chip\n");
Vincent Cuissarddc14bde2015-06-11 14:00:19 +0200244 gpio_set_value(priv->config.reset_n_io, 0);
Vincent Cuissard4a2b9472015-06-11 11:25:46 +0200245 usleep_range(5000, 10000);
Vincent Cuissarddc14bde2015-06-11 14:00:19 +0200246 gpio_set_value(priv->config.reset_n_io, 1);
Vincent Cuissard4a2b9472015-06-11 11:25:46 +0200247 } else
248 nfc_info(priv->dev, "no reset available on this interface\n");
249}
250
Vincent Cuissard3194c682015-10-26 10:27:39 +0100251void nfcmrvl_chip_halt(struct nfcmrvl_private *priv)
252{
253 if (priv->config.reset_n_io)
254 gpio_set_value(priv->config.reset_n_io, 0);
255}
256
Vincent Cuissarddc14bde2015-06-11 14:00:19 +0200257int nfcmrvl_parse_dt(struct device_node *node,
258 struct nfcmrvl_platform_data *pdata)
259{
260 int reset_n_io;
261
262 reset_n_io = of_get_named_gpio(node, "reset-n-io", 0);
263 if (reset_n_io < 0) {
264 pr_info("no reset-n-io config\n");
265 reset_n_io = 0;
266 } else if (!gpio_is_valid(reset_n_io)) {
267 pr_err("invalid reset-n-io GPIO\n");
268 return reset_n_io;
269 }
270 pdata->reset_n_io = reset_n_io;
271
272 if (of_find_property(node, "hci-muxed", NULL))
273 pdata->hci_muxed = 1;
274 else
275 pdata->hci_muxed = 0;
276
277 return 0;
278}
Vincent Cuissarddc14bde2015-06-11 14:00:19 +0200279EXPORT_SYMBOL_GPL(nfcmrvl_parse_dt);
280
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -0800281MODULE_AUTHOR("Marvell International Ltd.");
Vincent Cuissardfb101c02015-10-26 10:27:37 +0100282MODULE_DESCRIPTION("Marvell NFC driver");
Amitkumar Karwarf26e30c2014-01-06 12:58:19 -0800283MODULE_LICENSE("GPL v2");