xref: /openbmc/linux/drivers/nfc/st-nci/spi.c (revision 3648dc6d27f648b8e3ce9b48874627a833d53c3a)
1 /*
2  * SPI Link Layer for ST NCI based Driver
3  * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 
20 #include <linux/module.h>
21 #include <linux/spi/spi.h>
22 #include <linux/gpio.h>
23 #include <linux/of_irq.h>
24 #include <linux/of_gpio.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
27 #include <linux/nfc.h>
28 #include <net/nfc/nci.h>
29 #include <linux/platform_data/st-nci.h>
30 
31 #include "st-nci.h"
32 
33 #define DRIVER_DESC "NCI NFC driver for ST_NCI"
34 
35 /* ndlc header */
36 #define ST_NCI_FRAME_HEADROOM	1
37 #define ST_NCI_FRAME_TAILROOM 0
38 
39 #define ST_NCI_SPI_MIN_SIZE 4   /* PCB(1) + NCI Packet header(3) */
40 #define ST_NCI_SPI_MAX_SIZE 250 /* req 4.2.1 */
41 
42 #define ST_NCI_SPI_DRIVER_NAME "st_nci_spi"
43 
44 static struct spi_device_id st_nci_spi_id_table[] = {
45 	{ST_NCI_SPI_DRIVER_NAME, 0},
46 	{}
47 };
48 MODULE_DEVICE_TABLE(spi, st_nci_spi_id_table);
49 
50 struct st_nci_spi_phy {
51 	struct spi_device *spi_dev;
52 	struct llt_ndlc *ndlc;
53 
54 	unsigned int gpio_reset;
55 	unsigned int irq_polarity;
56 
57 	struct st_nci_se_status se_status;
58 };
59 
60 #define SPI_DUMP_SKB(info, skb)					\
61 do {								\
62 	pr_debug("%s:\n", info);				\
63 	print_hex_dump(KERN_DEBUG, "spi: ", DUMP_PREFIX_OFFSET,	\
64 		       16, 1, (skb)->data, (skb)->len, 0);	\
65 } while (0)
66 
67 static int st_nci_spi_enable(void *phy_id)
68 {
69 	struct st_nci_spi_phy *phy = phy_id;
70 
71 	gpio_set_value(phy->gpio_reset, 0);
72 	usleep_range(10000, 15000);
73 	gpio_set_value(phy->gpio_reset, 1);
74 	usleep_range(80000, 85000);
75 
76 	if (phy->ndlc->powered == 0)
77 		enable_irq(phy->spi_dev->irq);
78 
79 	return 0;
80 }
81 
82 static void st_nci_spi_disable(void *phy_id)
83 {
84 	struct st_nci_spi_phy *phy = phy_id;
85 
86 	disable_irq_nosync(phy->spi_dev->irq);
87 }
88 
89 /*
90  * Writing a frame must not return the number of written bytes.
91  * It must return either zero for success, or <0 for error.
92  * In addition, it must not alter the skb
93  */
94 static int st_nci_spi_write(void *phy_id, struct sk_buff *skb)
95 {
96 	int r;
97 	struct st_nci_spi_phy *phy = phy_id;
98 	struct spi_device *dev = phy->spi_dev;
99 	struct sk_buff *skb_rx;
100 	u8 buf[ST_NCI_SPI_MAX_SIZE + NCI_DATA_HDR_SIZE +
101 	       ST_NCI_FRAME_HEADROOM + ST_NCI_FRAME_TAILROOM];
102 	struct spi_transfer spi_xfer = {
103 		.tx_buf = skb->data,
104 		.rx_buf = buf,
105 		.len = skb->len,
106 	};
107 
108 	SPI_DUMP_SKB("st_nci_spi_write", skb);
109 
110 	if (phy->ndlc->hard_fault != 0)
111 		return phy->ndlc->hard_fault;
112 
113 	r = spi_sync_transfer(dev, &spi_xfer, 1);
114 	/*
115 	 * We may have received some valuable data on miso line.
116 	 * Send them back in the ndlc state machine.
117 	 */
118 	if (!r) {
119 		skb_rx = alloc_skb(skb->len, GFP_KERNEL);
120 		if (!skb_rx) {
121 			r = -ENOMEM;
122 			goto exit;
123 		}
124 
125 		skb_put(skb_rx, skb->len);
126 		memcpy(skb_rx->data, buf, skb->len);
127 		ndlc_recv(phy->ndlc, skb_rx);
128 	}
129 
130 exit:
131 	return r;
132 }
133 
134 /*
135  * Reads an ndlc frame and returns it in a newly allocated sk_buff.
136  * returns:
137  * 0 : if received frame is complete
138  * -EREMOTEIO : i2c read error (fatal)
139  * -EBADMSG : frame was incorrect and discarded
140  * -ENOMEM : cannot allocate skb, frame dropped
141  */
142 static int st_nci_spi_read(struct st_nci_spi_phy *phy,
143 			struct sk_buff **skb)
144 {
145 	int r;
146 	u8 len;
147 	u8 buf[ST_NCI_SPI_MAX_SIZE];
148 	struct spi_device *dev = phy->spi_dev;
149 	struct spi_transfer spi_xfer = {
150 		.rx_buf = buf,
151 		.len = ST_NCI_SPI_MIN_SIZE,
152 	};
153 
154 	r = spi_sync_transfer(dev, &spi_xfer, 1);
155 	if (r < 0)
156 		return -EREMOTEIO;
157 
158 	len = be16_to_cpu(*(__be16 *) (buf + 2));
159 	if (len > ST_NCI_SPI_MAX_SIZE) {
160 		nfc_err(&dev->dev, "invalid frame len\n");
161 		phy->ndlc->hard_fault = 1;
162 		return -EBADMSG;
163 	}
164 
165 	*skb = alloc_skb(ST_NCI_SPI_MIN_SIZE + len, GFP_KERNEL);
166 	if (*skb == NULL)
167 		return -ENOMEM;
168 
169 	skb_reserve(*skb, ST_NCI_SPI_MIN_SIZE);
170 	skb_put(*skb, ST_NCI_SPI_MIN_SIZE);
171 	memcpy((*skb)->data, buf, ST_NCI_SPI_MIN_SIZE);
172 
173 	if (!len)
174 		return 0;
175 
176 	spi_xfer.len = len;
177 	r = spi_sync_transfer(dev, &spi_xfer, 1);
178 	if (r < 0) {
179 		kfree_skb(*skb);
180 		return -EREMOTEIO;
181 	}
182 
183 	skb_put(*skb, len);
184 	memcpy((*skb)->data + ST_NCI_SPI_MIN_SIZE, buf, len);
185 
186 	SPI_DUMP_SKB("spi frame read", *skb);
187 
188 	return 0;
189 }
190 
191 /*
192  * Reads an ndlc frame from the chip.
193  *
194  * On ST21NFCB, IRQ goes in idle state when read starts.
195  */
196 static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
197 {
198 	struct st_nci_spi_phy *phy = phy_id;
199 	struct spi_device *dev;
200 	struct sk_buff *skb = NULL;
201 	int r;
202 
203 	if (!phy || !phy->ndlc || irq != phy->spi_dev->irq) {
204 		WARN_ON_ONCE(1);
205 		return IRQ_NONE;
206 	}
207 
208 	dev = phy->spi_dev;
209 	dev_dbg(&dev->dev, "IRQ\n");
210 
211 	if (phy->ndlc->hard_fault)
212 		return IRQ_HANDLED;
213 
214 	if (!phy->ndlc->powered) {
215 		st_nci_spi_disable(phy);
216 		return IRQ_HANDLED;
217 	}
218 
219 	r = st_nci_spi_read(phy, &skb);
220 	if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
221 		return IRQ_HANDLED;
222 
223 	ndlc_recv(phy->ndlc, skb);
224 
225 	return IRQ_HANDLED;
226 }
227 
228 static struct nfc_phy_ops spi_phy_ops = {
229 	.write = st_nci_spi_write,
230 	.enable = st_nci_spi_enable,
231 	.disable = st_nci_spi_disable,
232 };
233 
234 #ifdef CONFIG_OF
235 static int st_nci_spi_of_request_resources(struct spi_device *dev)
236 {
237 	struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
238 	struct device_node *pp;
239 	int gpio;
240 	int r;
241 
242 	pp = dev->dev.of_node;
243 	if (!pp)
244 		return -ENODEV;
245 
246 	/* Get GPIO from device tree */
247 	gpio = of_get_named_gpio(pp, "reset-gpios", 0);
248 	if (gpio < 0) {
249 		nfc_err(&dev->dev,
250 			"Failed to retrieve reset-gpios from device tree\n");
251 		return gpio;
252 	}
253 
254 	/* GPIO request and configuration */
255 	r = devm_gpio_request_one(&dev->dev, gpio,
256 				GPIOF_OUT_INIT_HIGH, "clf_reset");
257 	if (r) {
258 		nfc_err(&dev->dev, "Failed to request reset pin\n");
259 		return r;
260 	}
261 	phy->gpio_reset = gpio;
262 
263 	phy->irq_polarity = irq_get_trigger_type(dev->irq);
264 
265 	phy->se_status.is_ese_present =
266 				of_property_read_bool(pp, "ese-present");
267 	phy->se_status.is_uicc_present =
268 				of_property_read_bool(pp, "uicc-present");
269 
270 	return 0;
271 }
272 #else
273 static int st_nci_spi_of_request_resources(struct spi_device *dev)
274 {
275 	return -ENODEV;
276 }
277 #endif
278 
279 static int st_nci_spi_request_resources(struct spi_device *dev)
280 {
281 	struct st_nci_nfc_platform_data *pdata;
282 	struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
283 	int r;
284 
285 	pdata = dev->dev.platform_data;
286 	if (pdata == NULL) {
287 		nfc_err(&dev->dev, "No platform data\n");
288 		return -EINVAL;
289 	}
290 
291 	/* store for later use */
292 	phy->gpio_reset = pdata->gpio_reset;
293 	phy->irq_polarity = pdata->irq_polarity;
294 
295 	r = devm_gpio_request_one(&dev->dev,
296 			phy->gpio_reset, GPIOF_OUT_INIT_HIGH, "clf_reset");
297 	if (r) {
298 		pr_err("%s : reset gpio_request failed\n", __FILE__);
299 		return r;
300 	}
301 
302 	phy->se_status.is_ese_present = pdata->is_ese_present;
303 	phy->se_status.is_uicc_present = pdata->is_uicc_present;
304 
305 	return 0;
306 }
307 
308 static int st_nci_spi_probe(struct spi_device *dev)
309 {
310 	struct st_nci_spi_phy *phy;
311 	struct st_nci_nfc_platform_data *pdata;
312 	int r;
313 
314 	dev_dbg(&dev->dev, "%s\n", __func__);
315 	dev_dbg(&dev->dev, "IRQ: %d\n", dev->irq);
316 
317 	/* Check SPI platform functionnalities */
318 	if (!dev) {
319 		pr_debug("%s: dev is NULL. Device is not accessible.\n",
320 			__func__);
321 		return -ENODEV;
322 	}
323 
324 	phy = devm_kzalloc(&dev->dev, sizeof(struct st_nci_spi_phy),
325 			   GFP_KERNEL);
326 	if (!phy)
327 		return -ENOMEM;
328 
329 	phy->spi_dev = dev;
330 
331 	spi_set_drvdata(dev, phy);
332 
333 	pdata = dev->dev.platform_data;
334 	if (!pdata && dev->dev.of_node) {
335 		r = st_nci_spi_of_request_resources(dev);
336 		if (r) {
337 			nfc_err(&dev->dev, "No platform data\n");
338 			return r;
339 		}
340 	} else if (pdata) {
341 		r = st_nci_spi_request_resources(dev);
342 		if (r) {
343 			nfc_err(&dev->dev,
344 				"Cannot get platform resources\n");
345 			return r;
346 		}
347 	} else {
348 		nfc_err(&dev->dev,
349 			"st_nci platform resources not available\n");
350 		return -ENODEV;
351 	}
352 
353 	r = ndlc_probe(phy, &spi_phy_ops, &dev->dev,
354 			ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
355 			&phy->ndlc, &phy->se_status);
356 	if (r < 0) {
357 		nfc_err(&dev->dev, "Unable to register ndlc layer\n");
358 		return r;
359 	}
360 
361 	r = devm_request_threaded_irq(&dev->dev, dev->irq, NULL,
362 				st_nci_irq_thread_fn,
363 				phy->irq_polarity | IRQF_ONESHOT,
364 				ST_NCI_SPI_DRIVER_NAME, phy);
365 	if (r < 0)
366 		nfc_err(&dev->dev, "Unable to register IRQ handler\n");
367 
368 	return r;
369 }
370 
371 static int st_nci_spi_remove(struct spi_device *dev)
372 {
373 	struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
374 
375 	dev_dbg(&dev->dev, "%s\n", __func__);
376 
377 	ndlc_remove(phy->ndlc);
378 
379 	return 0;
380 }
381 
382 #ifdef CONFIG_OF
383 static const struct of_device_id of_st_nci_spi_match[] = {
384 	{ .compatible = "st,st21nfcb-spi", },
385 	{}
386 };
387 MODULE_DEVICE_TABLE(of, of_st_nci_spi_match);
388 #endif
389 
390 static struct spi_driver st_nci_spi_driver = {
391 	.driver = {
392 		.owner = THIS_MODULE,
393 		.name = ST_NCI_SPI_DRIVER_NAME,
394 		.of_match_table = of_match_ptr(of_st_nci_spi_match),
395 	},
396 	.probe = st_nci_spi_probe,
397 	.id_table = st_nci_spi_id_table,
398 	.remove = st_nci_spi_remove,
399 };
400 
401 module_spi_driver(st_nci_spi_driver);
402 
403 MODULE_LICENSE("GPL");
404 MODULE_DESCRIPTION(DRIVER_DESC);
405