xref: /linux/drivers/nfc/nfcmrvl/uart.c (revision e3cdf6cf5fc6db0643723083e2c70fffe098e249)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Marvell NFC-over-UART driver
4  *
5  * Copyright (C) 2015, Marvell International Ltd.
6  */
7 
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/printk.h>
15 
16 #include <net/nfc/nci.h>
17 #include <net/nfc/nci_core.h>
18 
19 #include "nfcmrvl.h"
20 
21 static unsigned int hci_muxed;
22 static unsigned int flow_control;
23 static unsigned int break_control;
24 
25 /*
26  * NFCMRVL NCI OPS
27  */
28 
29 static int nfcmrvl_uart_nci_open(struct nfcmrvl_private *priv)
30 {
31 	return 0;
32 }
33 
34 static int nfcmrvl_uart_nci_close(struct nfcmrvl_private *priv)
35 {
36 	return 0;
37 }
38 
39 static int nfcmrvl_uart_nci_send(struct nfcmrvl_private *priv,
40 				 struct sk_buff *skb)
41 {
42 	struct nci_uart *nu = priv->drv_data;
43 
44 	return nu->ops.send(nu, skb);
45 }
46 
47 static void nfcmrvl_uart_nci_update_config(struct nfcmrvl_private *priv,
48 					   const void *param)
49 {
50 	struct nci_uart *nu = priv->drv_data;
51 	const struct nfcmrvl_fw_uart_config *config = param;
52 
53 	nci_uart_set_config(nu, le32_to_cpu(config->baudrate),
54 			    config->flow_control);
55 }
56 
57 static const struct nfcmrvl_if_ops uart_ops = {
58 	.nci_open = nfcmrvl_uart_nci_open,
59 	.nci_close = nfcmrvl_uart_nci_close,
60 	.nci_send = nfcmrvl_uart_nci_send,
61 	.nci_update_config = nfcmrvl_uart_nci_update_config
62 };
63 
64 static int nfcmrvl_uart_parse_dt(struct device_node *node,
65 				 struct nfcmrvl_platform_data *pdata,
66 				 struct device *dev)
67 {
68 	struct device_node *matched_node;
69 	struct gpio_desc *reset_gpio;
70 	int ret;
71 
72 	matched_node = of_get_compatible_child(node, "marvell,nfc-uart");
73 	if (!matched_node) {
74 		matched_node = of_get_compatible_child(node, "mrvl,nfc-uart");
75 		if (!matched_node)
76 			return -ENODEV;
77 	}
78 
79 	ret = nfcmrvl_parse_dt(matched_node, pdata);
80 	if (ret < 0) {
81 		pr_err("Failed to get generic entries\n");
82 		of_node_put(matched_node);
83 		return ret;
84 	}
85 
86 	pdata->flow_control = of_property_read_bool(matched_node, "flow-control");
87 	pdata->break_control = of_property_read_bool(matched_node, "break-control");
88 
89 	reset_gpio = devm_fwnode_gpiod_get_optional(dev,
90 						    of_fwnode_handle(matched_node),
91 						    "reset", GPIOD_OUT_HIGH,
92 						    "nfcmrvl_reset_n");
93 	if (IS_ERR(reset_gpio)) {
94 		of_node_put(matched_node);
95 		return PTR_ERR(reset_gpio);
96 	}
97 	pdata->reset_gpio = reset_gpio;
98 
99 	of_node_put(matched_node);
100 
101 	return 0;
102 }
103 
104 /*
105  * NCI UART OPS
106  */
107 
108 static int nfcmrvl_nci_uart_open(struct nci_uart *nu)
109 {
110 	struct nfcmrvl_private *priv;
111 	struct nfcmrvl_platform_data config;
112 	const struct nfcmrvl_platform_data *pdata = NULL;
113 	struct device *dev = nu->tty->dev;
114 
115 	/*
116 	 * Platform data cannot be used here since usually it is already used
117 	 * by low level serial driver. We can try to retrieve serial device
118 	 * and check if DT entries were added.
119 	 */
120 
121 	if (dev && dev->parent && dev->parent->of_node)
122 		if (nfcmrvl_uart_parse_dt(dev->parent->of_node, &config, dev) == 0)
123 			pdata = &config;
124 
125 	if (!pdata) {
126 		pr_info("No platform data / DT -> fallback to module params\n");
127 		config.hci_muxed = hci_muxed;
128 		config.reset_gpio = NULL;
129 		config.flow_control = flow_control;
130 		config.break_control = break_control;
131 		pdata = &config;
132 	}
133 
134 	priv = nfcmrvl_nci_register_dev(NFCMRVL_PHY_UART, nu, &uart_ops,
135 					dev, pdata);
136 	if (IS_ERR(priv))
137 		return PTR_ERR(priv);
138 
139 	priv->support_fw_dnld = true;
140 
141 	nu->drv_data = priv;
142 	nu->ndev = priv->ndev;
143 
144 	return 0;
145 }
146 
147 static void nfcmrvl_nci_uart_close(struct nci_uart *nu)
148 {
149 	nfcmrvl_nci_unregister_dev((struct nfcmrvl_private *)nu->drv_data);
150 }
151 
152 static int nfcmrvl_nci_uart_recv(struct nci_uart *nu, struct sk_buff *skb)
153 {
154 	return nfcmrvl_nci_recv_frame((struct nfcmrvl_private *)nu->drv_data,
155 				      skb);
156 }
157 
158 static void nfcmrvl_nci_uart_tx_start(struct nci_uart *nu)
159 {
160 	struct nfcmrvl_private *priv = (struct nfcmrvl_private *)nu->drv_data;
161 
162 	if (priv->ndev->nfc_dev->fw_download_in_progress)
163 		return;
164 
165 	/* Remove BREAK to wake up the NFCC */
166 	if (priv->config.break_control && nu->tty->ops->break_ctl) {
167 		nu->tty->ops->break_ctl(nu->tty, 0);
168 		usleep_range(3000, 5000);
169 	}
170 }
171 
172 static void nfcmrvl_nci_uart_tx_done(struct nci_uart *nu)
173 {
174 	struct nfcmrvl_private *priv = (struct nfcmrvl_private *)nu->drv_data;
175 
176 	if (priv->ndev->nfc_dev->fw_download_in_progress)
177 		return;
178 
179 	/*
180 	 * To ensure that if the NFCC goes in DEEP SLEEP sate we can wake him
181 	 * up. we set BREAK. Once we will be ready to send again we will remove
182 	 * it.
183 	 */
184 	if (priv->config.break_control && nu->tty->ops->break_ctl) {
185 		nu->tty->ops->break_ctl(nu->tty, -1);
186 		usleep_range(1000, 3000);
187 	}
188 }
189 
190 static struct nci_uart nfcmrvl_nci_uart = {
191 	.owner  = THIS_MODULE,
192 	.name   = "nfcmrvl_uart",
193 	.driver = NCI_UART_DRIVER_MARVELL,
194 	.ops	= {
195 		.open		= nfcmrvl_nci_uart_open,
196 		.close		= nfcmrvl_nci_uart_close,
197 		.recv		= nfcmrvl_nci_uart_recv,
198 		.tx_start	= nfcmrvl_nci_uart_tx_start,
199 		.tx_done	= nfcmrvl_nci_uart_tx_done,
200 	}
201 };
202 module_driver(nfcmrvl_nci_uart, nci_uart_register, nci_uart_unregister);
203 
204 MODULE_AUTHOR("Marvell International Ltd.");
205 MODULE_DESCRIPTION("Marvell NFC-over-UART");
206 MODULE_LICENSE("GPL v2");
207 
208 module_param(flow_control, uint, 0);
209 MODULE_PARM_DESC(flow_control, "Tell if UART needs flow control at init.");
210 
211 module_param(break_control, uint, 0);
212 MODULE_PARM_DESC(break_control, "Tell if UART driver must drive break signal.");
213 
214 module_param(hci_muxed, uint, 0);
215 MODULE_PARM_DESC(hci_muxed, "Tell if transport is muxed in HCI one.");
216