blob: b4c4796dd23e26115dcb330421e9311ec99d7ba3 [file] [log] [blame]
Vincent Cuissarde097dc622015-06-11 14:00:20 +02001/**
2 * Marvell NFC-over-UART driver
3 *
4 * Copyright (C) 2015, Marvell International Ltd.
5 *
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>
20#include <linux/delay.h>
21#include <linux/of_gpio.h>
22#include <net/nfc/nci.h>
23#include <net/nfc/nci_core.h>
24#include "nfcmrvl.h"
25
26static unsigned int hci_muxed;
27static unsigned int flow_control;
28static unsigned int break_control;
29static unsigned int reset_n_io;
30
31/*
32** NFCMRVL NCI OPS
33*/
34
35static int nfcmrvl_uart_nci_open(struct nfcmrvl_private *priv)
36{
37 return 0;
38}
39
40static int nfcmrvl_uart_nci_close(struct nfcmrvl_private *priv)
41{
42 return 0;
43}
44
45static int nfcmrvl_uart_nci_send(struct nfcmrvl_private *priv,
46 struct sk_buff *skb)
47{
48 struct nci_uart *nu = priv->drv_data;
49
50 return nu->ops.send(nu, skb);
51}
52
Vincent Cuissard3194c682015-10-26 10:27:39 +010053static void nfcmrvl_uart_nci_update_config(struct nfcmrvl_private *priv,
54 const void *param)
55{
56 struct nci_uart *nu = priv->drv_data;
57 const struct nfcmrvl_fw_uart_config *config = param;
58
59 nci_uart_set_config(nu, le32_to_cpu(config->baudrate),
60 config->flow_control);
61}
62
Vincent Cuissarde097dc622015-06-11 14:00:20 +020063static struct nfcmrvl_if_ops uart_ops = {
64 .nci_open = nfcmrvl_uart_nci_open,
65 .nci_close = nfcmrvl_uart_nci_close,
66 .nci_send = nfcmrvl_uart_nci_send,
Vincent Cuissard3194c682015-10-26 10:27:39 +010067 .nci_update_config = nfcmrvl_uart_nci_update_config
Vincent Cuissarde097dc622015-06-11 14:00:20 +020068};
69
70#ifdef CONFIG_OF
71
72static int nfcmrvl_uart_parse_dt(struct device_node *node,
73 struct nfcmrvl_platform_data *pdata)
74{
75 struct device_node *matched_node;
76 int ret;
77
Vincent Cuissardd8e018c2015-10-26 10:27:45 +010078 matched_node = of_find_compatible_node(node, NULL, "marvell,nfc-uart");
79 if (!matched_node) {
80 matched_node = of_find_compatible_node(node, NULL,
81 "mrvl,nfc-uart");
82 if (!matched_node)
83 return -ENODEV;
84 }
Vincent Cuissarde097dc622015-06-11 14:00:20 +020085
86 ret = nfcmrvl_parse_dt(matched_node, pdata);
87 if (ret < 0) {
88 pr_err("Failed to get generic entries\n");
89 return ret;
90 }
91
92 if (of_find_property(matched_node, "flow-control", NULL))
93 pdata->flow_control = 1;
94 else
95 pdata->flow_control = 0;
96
97 if (of_find_property(matched_node, "break-control", NULL))
98 pdata->break_control = 1;
99 else
100 pdata->break_control = 0;
101
102 return 0;
103}
104
105#else
106
107static int nfcmrvl_uart_parse_dt(struct device_node *node,
108 struct nfcmrvl_platform_data *pdata)
109{
110 return -ENODEV;
111}
112
113#endif
114
115/*
116** NCI UART OPS
117*/
118
119static int nfcmrvl_nci_uart_open(struct nci_uart *nu)
120{
121 struct nfcmrvl_private *priv;
122 struct nfcmrvl_platform_data *pdata = NULL;
123 struct nfcmrvl_platform_data config;
124
125 /*
126 * Platform data cannot be used here since usually it is already used
127 * by low level serial driver. We can try to retrieve serial device
128 * and check if DT entries were added.
129 */
130
131 if (nu->tty->dev->parent && nu->tty->dev->parent->of_node)
132 if (nfcmrvl_uart_parse_dt(nu->tty->dev->parent->of_node,
133 &config) == 0)
134 pdata = &config;
135
136 if (!pdata) {
137 pr_info("No platform data / DT -> fallback to module params\n");
138 config.hci_muxed = hci_muxed;
139 config.reset_n_io = reset_n_io;
140 config.flow_control = flow_control;
141 config.break_control = break_control;
142 pdata = &config;
143 }
144
Vincent Cuissard58d34aa2015-10-26 10:27:40 +0100145 priv = nfcmrvl_nci_register_dev(NFCMRVL_PHY_UART, nu, &uart_ops,
146 nu->tty->dev, pdata);
Vincent Cuissarde097dc622015-06-11 14:00:20 +0200147 if (IS_ERR(priv))
148 return PTR_ERR(priv);
149
Vincent Cuissard3194c682015-10-26 10:27:39 +0100150 priv->support_fw_dnld = true;
Vincent Cuissarde097dc622015-06-11 14:00:20 +0200151
152 nu->drv_data = priv;
153 nu->ndev = priv->ndev;
154
Vincent Cuissarde097dc622015-06-11 14:00:20 +0200155 return 0;
156}
157
158static void nfcmrvl_nci_uart_close(struct nci_uart *nu)
159{
160 nfcmrvl_nci_unregister_dev((struct nfcmrvl_private *)nu->drv_data);
161}
162
163static int nfcmrvl_nci_uart_recv(struct nci_uart *nu, struct sk_buff *skb)
164{
165 return nfcmrvl_nci_recv_frame((struct nfcmrvl_private *)nu->drv_data,
166 skb);
167}
168
169static void nfcmrvl_nci_uart_tx_start(struct nci_uart *nu)
170{
171 struct nfcmrvl_private *priv = (struct nfcmrvl_private *)nu->drv_data;
172
Vincent Cuissardfeacf002015-11-03 19:19:32 +0100173 if (priv->ndev->nfc_dev->fw_download_in_progress)
174 return;
175
Vincent Cuissarde097dc622015-06-11 14:00:20 +0200176 /* Remove BREAK to wake up the NFCC */
177 if (priv->config.break_control && nu->tty->ops->break_ctl) {
178 nu->tty->ops->break_ctl(nu->tty, 0);
179 usleep_range(3000, 5000);
180 }
181}
182
183static void nfcmrvl_nci_uart_tx_done(struct nci_uart *nu)
184{
185 struct nfcmrvl_private *priv = (struct nfcmrvl_private *)nu->drv_data;
186
Vincent Cuissardfeacf002015-11-03 19:19:32 +0100187 if (priv->ndev->nfc_dev->fw_download_in_progress)
188 return;
189
Vincent Cuissarde097dc622015-06-11 14:00:20 +0200190 /*
191 ** To ensure that if the NFCC goes in DEEP SLEEP sate we can wake him
192 ** up. we set BREAK. Once we will be ready to send again we will remove
193 ** it.
194 */
Vincent Cuissardd2d2e642015-11-03 19:19:33 +0100195 if (priv->config.break_control && nu->tty->ops->break_ctl) {
Vincent Cuissarde097dc622015-06-11 14:00:20 +0200196 nu->tty->ops->break_ctl(nu->tty, -1);
Vincent Cuissardd2d2e642015-11-03 19:19:33 +0100197 usleep_range(1000, 3000);
198 }
Vincent Cuissarde097dc622015-06-11 14:00:20 +0200199}
200
201static struct nci_uart nfcmrvl_nci_uart = {
202 .owner = THIS_MODULE,
203 .name = "nfcmrvl_uart",
204 .driver = NCI_UART_DRIVER_MARVELL,
205 .ops = {
206 .open = nfcmrvl_nci_uart_open,
207 .close = nfcmrvl_nci_uart_close,
208 .recv = nfcmrvl_nci_uart_recv,
209 .tx_start = nfcmrvl_nci_uart_tx_start,
210 .tx_done = nfcmrvl_nci_uart_tx_done,
211 }
212};
213
214/*
215** Module init
216*/
217
218static int nfcmrvl_uart_init_module(void)
219{
220 return nci_uart_register(&nfcmrvl_nci_uart);
221}
222
223static void nfcmrvl_uart_exit_module(void)
224{
225 nci_uart_unregister(&nfcmrvl_nci_uart);
226}
227
228module_init(nfcmrvl_uart_init_module);
229module_exit(nfcmrvl_uart_exit_module);
230
231MODULE_AUTHOR("Marvell International Ltd.");
232MODULE_DESCRIPTION("Marvell NFC-over-UART");
233MODULE_LICENSE("GPL v2");
234
235module_param(flow_control, uint, 0);
236MODULE_PARM_DESC(flow_control, "Tell if UART needs flow control at init.");
237
238module_param(break_control, uint, 0);
239MODULE_PARM_DESC(break_control, "Tell if UART driver must drive break signal.");
240
241module_param(hci_muxed, uint, 0);
242MODULE_PARM_DESC(hci_muxed, "Tell if transport is muxed in HCI one.");
243
244module_param(reset_n_io, uint, 0);
245MODULE_PARM_DESC(reset_n_io, "GPIO that is wired to RESET_N signal.");