xref: /openbmc/linux/drivers/nfc/st-nci/spi.c (revision cd4d09ec)
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/gpio/consumer.h>
24 #include <linux/of_irq.h>
25 #include <linux/of_gpio.h>
26 #include <linux/acpi.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <linux/nfc.h>
30 #include <net/nfc/nci.h>
31 #include <linux/platform_data/st-nci.h>
32 
33 #include "st-nci.h"
34 
35 #define DRIVER_DESC "NCI NFC driver for ST_NCI"
36 
37 /* ndlc header */
38 #define ST_NCI_FRAME_HEADROOM	1
39 #define ST_NCI_FRAME_TAILROOM	0
40 
41 #define ST_NCI_SPI_MIN_SIZE 4   /* PCB(1) + NCI Packet header(3) */
42 #define ST_NCI_SPI_MAX_SIZE 250 /* req 4.2.1 */
43 
44 #define ST_NCI_SPI_DRIVER_NAME "st_nci_spi"
45 
46 #define ST_NCI_GPIO_NAME_RESET "clf_reset"
47 
48 struct st_nci_spi_phy {
49 	struct spi_device *spi_dev;
50 	struct llt_ndlc *ndlc;
51 
52 	bool irq_active;
53 
54 	unsigned int gpio_reset;
55 	unsigned int irq_polarity;
56 
57 	struct st_nci_se_status se_status;
58 };
59 
60 static int st_nci_spi_enable(void *phy_id)
61 {
62 	struct st_nci_spi_phy *phy = phy_id;
63 
64 	gpio_set_value(phy->gpio_reset, 0);
65 	usleep_range(10000, 15000);
66 	gpio_set_value(phy->gpio_reset, 1);
67 	usleep_range(80000, 85000);
68 
69 	if (phy->ndlc->powered == 0 && phy->irq_active == 0) {
70 		enable_irq(phy->spi_dev->irq);
71 		phy->irq_active = true;
72 	}
73 
74 	return 0;
75 }
76 
77 static void st_nci_spi_disable(void *phy_id)
78 {
79 	struct st_nci_spi_phy *phy = phy_id;
80 
81 	disable_irq_nosync(phy->spi_dev->irq);
82 	phy->irq_active = false;
83 }
84 
85 /*
86  * Writing a frame must not return the number of written bytes.
87  * It must return either zero for success, or <0 for error.
88  * In addition, it must not alter the skb
89  */
90 static int st_nci_spi_write(void *phy_id, struct sk_buff *skb)
91 {
92 	int r;
93 	struct st_nci_spi_phy *phy = phy_id;
94 	struct spi_device *dev = phy->spi_dev;
95 	struct sk_buff *skb_rx;
96 	u8 buf[ST_NCI_SPI_MAX_SIZE + NCI_DATA_HDR_SIZE +
97 	       ST_NCI_FRAME_HEADROOM + ST_NCI_FRAME_TAILROOM];
98 	struct spi_transfer spi_xfer = {
99 		.tx_buf = skb->data,
100 		.rx_buf = buf,
101 		.len = skb->len,
102 	};
103 
104 	if (phy->ndlc->hard_fault != 0)
105 		return phy->ndlc->hard_fault;
106 
107 	r = spi_sync_transfer(dev, &spi_xfer, 1);
108 	/*
109 	 * We may have received some valuable data on miso line.
110 	 * Send them back in the ndlc state machine.
111 	 */
112 	if (!r) {
113 		skb_rx = alloc_skb(skb->len, GFP_KERNEL);
114 		if (!skb_rx) {
115 			r = -ENOMEM;
116 			goto exit;
117 		}
118 
119 		skb_put(skb_rx, skb->len);
120 		memcpy(skb_rx->data, buf, skb->len);
121 		ndlc_recv(phy->ndlc, skb_rx);
122 	}
123 
124 exit:
125 	return r;
126 }
127 
128 /*
129  * Reads an ndlc frame and returns it in a newly allocated sk_buff.
130  * returns:
131  * 0 : if received frame is complete
132  * -EREMOTEIO : i2c read error (fatal)
133  * -EBADMSG : frame was incorrect and discarded
134  * -ENOMEM : cannot allocate skb, frame dropped
135  */
136 static int st_nci_spi_read(struct st_nci_spi_phy *phy,
137 			struct sk_buff **skb)
138 {
139 	int r;
140 	u8 len;
141 	u8 buf[ST_NCI_SPI_MAX_SIZE];
142 	struct spi_device *dev = phy->spi_dev;
143 	struct spi_transfer spi_xfer = {
144 		.rx_buf = buf,
145 		.len = ST_NCI_SPI_MIN_SIZE,
146 	};
147 
148 	r = spi_sync_transfer(dev, &spi_xfer, 1);
149 	if (r < 0)
150 		return -EREMOTEIO;
151 
152 	len = be16_to_cpu(*(__be16 *) (buf + 2));
153 	if (len > ST_NCI_SPI_MAX_SIZE) {
154 		nfc_err(&dev->dev, "invalid frame len\n");
155 		phy->ndlc->hard_fault = 1;
156 		return -EBADMSG;
157 	}
158 
159 	*skb = alloc_skb(ST_NCI_SPI_MIN_SIZE + len, GFP_KERNEL);
160 	if (*skb == NULL)
161 		return -ENOMEM;
162 
163 	skb_reserve(*skb, ST_NCI_SPI_MIN_SIZE);
164 	skb_put(*skb, ST_NCI_SPI_MIN_SIZE);
165 	memcpy((*skb)->data, buf, ST_NCI_SPI_MIN_SIZE);
166 
167 	if (!len)
168 		return 0;
169 
170 	spi_xfer.len = len;
171 	r = spi_sync_transfer(dev, &spi_xfer, 1);
172 	if (r < 0) {
173 		kfree_skb(*skb);
174 		return -EREMOTEIO;
175 	}
176 
177 	skb_put(*skb, len);
178 	memcpy((*skb)->data + ST_NCI_SPI_MIN_SIZE, buf, len);
179 
180 	return 0;
181 }
182 
183 /*
184  * Reads an ndlc frame from the chip.
185  *
186  * On ST21NFCB, IRQ goes in idle state when read starts.
187  */
188 static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
189 {
190 	struct st_nci_spi_phy *phy = phy_id;
191 	struct spi_device *dev;
192 	struct sk_buff *skb = NULL;
193 	int r;
194 
195 	if (!phy || !phy->ndlc || irq != phy->spi_dev->irq) {
196 		WARN_ON_ONCE(1);
197 		return IRQ_NONE;
198 	}
199 
200 	dev = phy->spi_dev;
201 	dev_dbg(&dev->dev, "IRQ\n");
202 
203 	if (phy->ndlc->hard_fault)
204 		return IRQ_HANDLED;
205 
206 	if (!phy->ndlc->powered) {
207 		st_nci_spi_disable(phy);
208 		return IRQ_HANDLED;
209 	}
210 
211 	r = st_nci_spi_read(phy, &skb);
212 	if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
213 		return IRQ_HANDLED;
214 
215 	ndlc_recv(phy->ndlc, skb);
216 
217 	return IRQ_HANDLED;
218 }
219 
220 static struct nfc_phy_ops spi_phy_ops = {
221 	.write = st_nci_spi_write,
222 	.enable = st_nci_spi_enable,
223 	.disable = st_nci_spi_disable,
224 };
225 
226 static int st_nci_spi_acpi_request_resources(struct spi_device *spi_dev)
227 {
228 	struct st_nci_spi_phy *phy = spi_get_drvdata(spi_dev);
229 	const struct acpi_device_id *id;
230 	struct gpio_desc *gpiod_reset;
231 	struct device *dev;
232 
233 	if (!spi_dev)
234 		return -EINVAL;
235 
236 	dev = &spi_dev->dev;
237 
238 	/* Match the struct device against a given list of ACPI IDs */
239 	id = acpi_match_device(dev->driver->acpi_match_table, dev);
240 	if (!id)
241 		return -ENODEV;
242 
243 	/* Get RESET GPIO from ACPI */
244 	gpiod_reset = devm_gpiod_get_index(dev, ST_NCI_GPIO_NAME_RESET, 1,
245 					   GPIOD_OUT_HIGH);
246 	if (IS_ERR(gpiod_reset)) {
247 		nfc_err(dev, "Unable to get RESET GPIO\n");
248 		return -ENODEV;
249 	}
250 
251 	phy->gpio_reset = desc_to_gpio(gpiod_reset);
252 
253 	phy->irq_polarity = irq_get_trigger_type(spi_dev->irq);
254 
255 	phy->se_status.is_ese_present =
256 				device_property_present(dev, "ese-present");
257 	phy->se_status.is_uicc_present =
258 				device_property_present(dev, "uicc-present");
259 
260 	return 0;
261 }
262 
263 static int st_nci_spi_of_request_resources(struct spi_device *dev)
264 {
265 	struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
266 	struct device_node *pp;
267 	int gpio;
268 	int r;
269 
270 	pp = dev->dev.of_node;
271 	if (!pp)
272 		return -ENODEV;
273 
274 	/* Get GPIO from device tree */
275 	gpio = of_get_named_gpio(pp, "reset-gpios", 0);
276 	if (gpio < 0) {
277 		nfc_err(&dev->dev,
278 			"Failed to retrieve reset-gpios from device tree\n");
279 		return gpio;
280 	}
281 
282 	/* GPIO request and configuration */
283 	r = devm_gpio_request_one(&dev->dev, gpio,
284 				GPIOF_OUT_INIT_HIGH, ST_NCI_GPIO_NAME_RESET);
285 	if (r) {
286 		nfc_err(&dev->dev, "Failed to request reset pin\n");
287 		return r;
288 	}
289 	phy->gpio_reset = gpio;
290 
291 	phy->irq_polarity = irq_get_trigger_type(dev->irq);
292 
293 	phy->se_status.is_ese_present =
294 				of_property_read_bool(pp, "ese-present");
295 	phy->se_status.is_uicc_present =
296 				of_property_read_bool(pp, "uicc-present");
297 
298 	return 0;
299 }
300 
301 static int st_nci_spi_request_resources(struct spi_device *dev)
302 {
303 	struct st_nci_nfc_platform_data *pdata;
304 	struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
305 	int r;
306 
307 	pdata = dev->dev.platform_data;
308 	if (pdata == NULL) {
309 		nfc_err(&dev->dev, "No platform data\n");
310 		return -EINVAL;
311 	}
312 
313 	/* store for later use */
314 	phy->gpio_reset = pdata->gpio_reset;
315 	phy->irq_polarity = pdata->irq_polarity;
316 
317 	r = devm_gpio_request_one(&dev->dev,
318 			phy->gpio_reset, GPIOF_OUT_INIT_HIGH,
319 			ST_NCI_GPIO_NAME_RESET);
320 	if (r) {
321 		pr_err("%s : reset gpio_request failed\n", __FILE__);
322 		return r;
323 	}
324 
325 	phy->se_status.is_ese_present = pdata->is_ese_present;
326 	phy->se_status.is_uicc_present = pdata->is_uicc_present;
327 
328 	return 0;
329 }
330 
331 static int st_nci_spi_probe(struct spi_device *dev)
332 {
333 	struct st_nci_spi_phy *phy;
334 	struct st_nci_nfc_platform_data *pdata;
335 	int r;
336 
337 	dev_dbg(&dev->dev, "%s\n", __func__);
338 	dev_dbg(&dev->dev, "IRQ: %d\n", dev->irq);
339 
340 	/* Check SPI platform functionnalities */
341 	if (!dev) {
342 		pr_debug("%s: dev is NULL. Device is not accessible.\n",
343 			__func__);
344 		return -ENODEV;
345 	}
346 
347 	phy = devm_kzalloc(&dev->dev, sizeof(struct st_nci_spi_phy),
348 			   GFP_KERNEL);
349 	if (!phy)
350 		return -ENOMEM;
351 
352 	phy->spi_dev = dev;
353 
354 	spi_set_drvdata(dev, phy);
355 
356 	pdata = dev->dev.platform_data;
357 	if (!pdata && dev->dev.of_node) {
358 		r = st_nci_spi_of_request_resources(dev);
359 		if (r) {
360 			nfc_err(&dev->dev, "No platform data\n");
361 			return r;
362 		}
363 	} else if (pdata) {
364 		r = st_nci_spi_request_resources(dev);
365 		if (r) {
366 			nfc_err(&dev->dev,
367 				"Cannot get platform resources\n");
368 			return r;
369 		}
370 	} else if (ACPI_HANDLE(&dev->dev)) {
371 		r = st_nci_spi_acpi_request_resources(dev);
372 		if (r) {
373 			nfc_err(&dev->dev, "Cannot get ACPI data\n");
374 			return r;
375 		}
376 	} else {
377 		nfc_err(&dev->dev,
378 			"st_nci platform resources not available\n");
379 		return -ENODEV;
380 	}
381 
382 	r = ndlc_probe(phy, &spi_phy_ops, &dev->dev,
383 			ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
384 			&phy->ndlc, &phy->se_status);
385 	if (r < 0) {
386 		nfc_err(&dev->dev, "Unable to register ndlc layer\n");
387 		return r;
388 	}
389 
390 	phy->irq_active = true;
391 	r = devm_request_threaded_irq(&dev->dev, dev->irq, NULL,
392 				st_nci_irq_thread_fn,
393 				phy->irq_polarity | IRQF_ONESHOT,
394 				ST_NCI_SPI_DRIVER_NAME, phy);
395 	if (r < 0)
396 		nfc_err(&dev->dev, "Unable to register IRQ handler\n");
397 
398 	return r;
399 }
400 
401 static int st_nci_spi_remove(struct spi_device *dev)
402 {
403 	struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
404 
405 	dev_dbg(&dev->dev, "%s\n", __func__);
406 
407 	ndlc_remove(phy->ndlc);
408 
409 	return 0;
410 }
411 
412 static struct spi_device_id st_nci_spi_id_table[] = {
413 	{ST_NCI_SPI_DRIVER_NAME, 0},
414 	{}
415 };
416 MODULE_DEVICE_TABLE(spi, st_nci_spi_id_table);
417 
418 static const struct acpi_device_id st_nci_spi_acpi_match[] = {
419 	{"SMO2101", 0},
420 	{}
421 };
422 MODULE_DEVICE_TABLE(acpi, st_nci_spi_acpi_match);
423 
424 static const struct of_device_id of_st_nci_spi_match[] = {
425 	{ .compatible = "st,st21nfcb-spi", },
426 	{}
427 };
428 MODULE_DEVICE_TABLE(of, of_st_nci_spi_match);
429 
430 static struct spi_driver st_nci_spi_driver = {
431 	.driver = {
432 		.name = ST_NCI_SPI_DRIVER_NAME,
433 		.of_match_table = of_match_ptr(of_st_nci_spi_match),
434 		.acpi_match_table = ACPI_PTR(st_nci_spi_acpi_match),
435 	},
436 	.probe = st_nci_spi_probe,
437 	.id_table = st_nci_spi_id_table,
438 	.remove = st_nci_spi_remove,
439 };
440 module_spi_driver(st_nci_spi_driver);
441 
442 MODULE_LICENSE("GPL");
443 MODULE_DESCRIPTION(DRIVER_DESC);
444