xref: /openbmc/linux/drivers/nfc/nfcmrvl/uart.c (revision 791d3ef2)
1 /**
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 
26 static unsigned int hci_muxed;
27 static unsigned int flow_control;
28 static unsigned int break_control;
29 static unsigned int reset_n_io;
30 
31 /*
32 ** NFCMRVL NCI OPS
33 */
34 
35 static int nfcmrvl_uart_nci_open(struct nfcmrvl_private *priv)
36 {
37 	return 0;
38 }
39 
40 static int nfcmrvl_uart_nci_close(struct nfcmrvl_private *priv)
41 {
42 	return 0;
43 }
44 
45 static 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 
53 static 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 
63 static 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,
67 	.nci_update_config = nfcmrvl_uart_nci_update_config
68 };
69 
70 static int nfcmrvl_uart_parse_dt(struct device_node *node,
71 				 struct nfcmrvl_platform_data *pdata)
72 {
73 	struct device_node *matched_node;
74 	int ret;
75 
76 	matched_node = of_find_compatible_node(node, NULL, "marvell,nfc-uart");
77 	if (!matched_node) {
78 		matched_node = of_find_compatible_node(node, NULL,
79 						       "mrvl,nfc-uart");
80 		if (!matched_node)
81 			return -ENODEV;
82 	}
83 
84 	ret = nfcmrvl_parse_dt(matched_node, pdata);
85 	if (ret < 0) {
86 		pr_err("Failed to get generic entries\n");
87 		of_node_put(matched_node);
88 		return ret;
89 	}
90 
91 	if (of_find_property(matched_node, "flow-control", NULL))
92 		pdata->flow_control = 1;
93 	else
94 		pdata->flow_control = 0;
95 
96 	if (of_find_property(matched_node, "break-control", NULL))
97 		pdata->break_control = 1;
98 	else
99 		pdata->break_control = 0;
100 
101 	of_node_put(matched_node);
102 
103 	return 0;
104 }
105 
106 /*
107 ** NCI UART OPS
108 */
109 
110 static int nfcmrvl_nci_uart_open(struct nci_uart *nu)
111 {
112 	struct nfcmrvl_private *priv;
113 	struct nfcmrvl_platform_data *pdata = NULL;
114 	struct nfcmrvl_platform_data config;
115 	struct device *dev = nu->tty->dev;
116 
117 	/*
118 	 * Platform data cannot be used here since usually it is already used
119 	 * by low level serial driver. We can try to retrieve serial device
120 	 * and check if DT entries were added.
121 	 */
122 
123 	if (dev && dev->parent && dev->parent->of_node)
124 		if (nfcmrvl_uart_parse_dt(dev->parent->of_node, &config) == 0)
125 			pdata = &config;
126 
127 	if (!pdata) {
128 		pr_info("No platform data / DT -> fallback to module params\n");
129 		config.hci_muxed = hci_muxed;
130 		config.reset_n_io = reset_n_io;
131 		config.flow_control = flow_control;
132 		config.break_control = break_control;
133 		pdata = &config;
134 	}
135 
136 	priv = nfcmrvl_nci_register_dev(NFCMRVL_PHY_UART, nu, &uart_ops,
137 					dev, pdata);
138 	if (IS_ERR(priv))
139 		return PTR_ERR(priv);
140 
141 	priv->support_fw_dnld = true;
142 
143 	nu->drv_data = priv;
144 	nu->ndev = priv->ndev;
145 
146 	return 0;
147 }
148 
149 static void nfcmrvl_nci_uart_close(struct nci_uart *nu)
150 {
151 	nfcmrvl_nci_unregister_dev((struct nfcmrvl_private *)nu->drv_data);
152 }
153 
154 static int nfcmrvl_nci_uart_recv(struct nci_uart *nu, struct sk_buff *skb)
155 {
156 	return nfcmrvl_nci_recv_frame((struct nfcmrvl_private *)nu->drv_data,
157 				      skb);
158 }
159 
160 static void nfcmrvl_nci_uart_tx_start(struct nci_uart *nu)
161 {
162 	struct nfcmrvl_private *priv = (struct nfcmrvl_private *)nu->drv_data;
163 
164 	if (priv->ndev->nfc_dev->fw_download_in_progress)
165 		return;
166 
167 	/* Remove BREAK to wake up the NFCC */
168 	if (priv->config.break_control && nu->tty->ops->break_ctl) {
169 		nu->tty->ops->break_ctl(nu->tty, 0);
170 		usleep_range(3000, 5000);
171 	}
172 }
173 
174 static void nfcmrvl_nci_uart_tx_done(struct nci_uart *nu)
175 {
176 	struct nfcmrvl_private *priv = (struct nfcmrvl_private *)nu->drv_data;
177 
178 	if (priv->ndev->nfc_dev->fw_download_in_progress)
179 		return;
180 
181 	/*
182 	** To ensure that if the NFCC goes in DEEP SLEEP sate we can wake him
183 	** up. we set BREAK. Once we will be ready to send again we will remove
184 	** it.
185 	*/
186 	if (priv->config.break_control && nu->tty->ops->break_ctl) {
187 		nu->tty->ops->break_ctl(nu->tty, -1);
188 		usleep_range(1000, 3000);
189 	}
190 }
191 
192 static struct nci_uart nfcmrvl_nci_uart = {
193 	.owner  = THIS_MODULE,
194 	.name   = "nfcmrvl_uart",
195 	.driver = NCI_UART_DRIVER_MARVELL,
196 	.ops	= {
197 		.open		= nfcmrvl_nci_uart_open,
198 		.close		= nfcmrvl_nci_uart_close,
199 		.recv		= nfcmrvl_nci_uart_recv,
200 		.tx_start	= nfcmrvl_nci_uart_tx_start,
201 		.tx_done	= nfcmrvl_nci_uart_tx_done,
202 	}
203 };
204 
205 /*
206 ** Module init
207 */
208 
209 static int nfcmrvl_uart_init_module(void)
210 {
211 	return nci_uart_register(&nfcmrvl_nci_uart);
212 }
213 
214 static void nfcmrvl_uart_exit_module(void)
215 {
216 	nci_uart_unregister(&nfcmrvl_nci_uart);
217 }
218 
219 module_init(nfcmrvl_uart_init_module);
220 module_exit(nfcmrvl_uart_exit_module);
221 
222 MODULE_AUTHOR("Marvell International Ltd.");
223 MODULE_DESCRIPTION("Marvell NFC-over-UART");
224 MODULE_LICENSE("GPL v2");
225 
226 module_param(flow_control, uint, 0);
227 MODULE_PARM_DESC(flow_control, "Tell if UART needs flow control at init.");
228 
229 module_param(break_control, uint, 0);
230 MODULE_PARM_DESC(break_control, "Tell if UART driver must drive break signal.");
231 
232 module_param(hci_muxed, uint, 0);
233 MODULE_PARM_DESC(hci_muxed, "Tell if transport is muxed in HCI one.");
234 
235 module_param(reset_n_io, uint, 0);
236 MODULE_PARM_DESC(reset_n_io, "GPIO that is wired to RESET_N signal.");
237