xref: /openbmc/linux/drivers/nfc/st-nci/spi.c (revision bc5aa3a0)
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 "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 	struct gpio_desc *gpiod_reset;
230 	struct device *dev = &spi_dev->dev;
231 	u8 tmp;
232 
233 	/* Get RESET GPIO from ACPI */
234 	gpiod_reset = devm_gpiod_get_index(dev, ST_NCI_GPIO_NAME_RESET, 1,
235 					   GPIOD_OUT_HIGH);
236 	if (IS_ERR(gpiod_reset)) {
237 		nfc_err(dev, "Unable to get RESET GPIO\n");
238 		return -ENODEV;
239 	}
240 
241 	phy->gpio_reset = desc_to_gpio(gpiod_reset);
242 
243 	phy->irq_polarity = irq_get_trigger_type(spi_dev->irq);
244 
245 	phy->se_status.is_ese_present = false;
246 	phy->se_status.is_uicc_present = false;
247 
248 	if (device_property_present(dev, "ese-present")) {
249 		device_property_read_u8(dev, "ese-present", &tmp);
250 		tmp = phy->se_status.is_ese_present;
251 	}
252 
253 	if (device_property_present(dev, "uicc-present")) {
254 		device_property_read_u8(dev, "uicc-present", &tmp);
255 		tmp = phy->se_status.is_uicc_present;
256 	}
257 
258 	return 0;
259 }
260 
261 static int st_nci_spi_of_request_resources(struct spi_device *dev)
262 {
263 	struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
264 	struct device_node *pp;
265 	int gpio;
266 	int r;
267 
268 	pp = dev->dev.of_node;
269 	if (!pp)
270 		return -ENODEV;
271 
272 	/* Get GPIO from device tree */
273 	gpio = of_get_named_gpio(pp, "reset-gpios", 0);
274 	if (gpio < 0) {
275 		nfc_err(&dev->dev,
276 			"Failed to retrieve reset-gpios from device tree\n");
277 		return gpio;
278 	}
279 
280 	/* GPIO request and configuration */
281 	r = devm_gpio_request_one(&dev->dev, gpio,
282 				GPIOF_OUT_INIT_HIGH, ST_NCI_GPIO_NAME_RESET);
283 	if (r) {
284 		nfc_err(&dev->dev, "Failed to request reset pin\n");
285 		return r;
286 	}
287 	phy->gpio_reset = gpio;
288 
289 	phy->irq_polarity = irq_get_trigger_type(dev->irq);
290 
291 	phy->se_status.is_ese_present =
292 				of_property_read_bool(pp, "ese-present");
293 	phy->se_status.is_uicc_present =
294 				of_property_read_bool(pp, "uicc-present");
295 
296 	return 0;
297 }
298 
299 static int st_nci_spi_request_resources(struct spi_device *dev)
300 {
301 	struct st_nci_nfc_platform_data *pdata;
302 	struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
303 	int r;
304 
305 	pdata = dev->dev.platform_data;
306 	if (pdata == NULL) {
307 		nfc_err(&dev->dev, "No platform data\n");
308 		return -EINVAL;
309 	}
310 
311 	/* store for later use */
312 	phy->gpio_reset = pdata->gpio_reset;
313 	phy->irq_polarity = pdata->irq_polarity;
314 
315 	r = devm_gpio_request_one(&dev->dev,
316 			phy->gpio_reset, GPIOF_OUT_INIT_HIGH,
317 			ST_NCI_GPIO_NAME_RESET);
318 	if (r) {
319 		pr_err("%s : reset gpio_request failed\n", __FILE__);
320 		return r;
321 	}
322 
323 	phy->se_status.is_ese_present = pdata->is_ese_present;
324 	phy->se_status.is_uicc_present = pdata->is_uicc_present;
325 
326 	return 0;
327 }
328 
329 static int st_nci_spi_probe(struct spi_device *dev)
330 {
331 	struct st_nci_spi_phy *phy;
332 	struct st_nci_nfc_platform_data *pdata;
333 	int r;
334 
335 	dev_dbg(&dev->dev, "%s\n", __func__);
336 	dev_dbg(&dev->dev, "IRQ: %d\n", dev->irq);
337 
338 	/* Check SPI platform functionnalities */
339 	if (!dev) {
340 		pr_debug("%s: dev is NULL. Device is not accessible.\n",
341 			__func__);
342 		return -ENODEV;
343 	}
344 
345 	phy = devm_kzalloc(&dev->dev, sizeof(struct st_nci_spi_phy),
346 			   GFP_KERNEL);
347 	if (!phy)
348 		return -ENOMEM;
349 
350 	phy->spi_dev = dev;
351 
352 	spi_set_drvdata(dev, phy);
353 
354 	pdata = dev->dev.platform_data;
355 	if (!pdata && dev->dev.of_node) {
356 		r = st_nci_spi_of_request_resources(dev);
357 		if (r) {
358 			nfc_err(&dev->dev, "No platform data\n");
359 			return r;
360 		}
361 	} else if (pdata) {
362 		r = st_nci_spi_request_resources(dev);
363 		if (r) {
364 			nfc_err(&dev->dev,
365 				"Cannot get platform resources\n");
366 			return r;
367 		}
368 	} else if (ACPI_HANDLE(&dev->dev)) {
369 		r = st_nci_spi_acpi_request_resources(dev);
370 		if (r) {
371 			nfc_err(&dev->dev, "Cannot get ACPI data\n");
372 			return r;
373 		}
374 	} else {
375 		nfc_err(&dev->dev,
376 			"st_nci platform resources not available\n");
377 		return -ENODEV;
378 	}
379 
380 	r = ndlc_probe(phy, &spi_phy_ops, &dev->dev,
381 			ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
382 			&phy->ndlc, &phy->se_status);
383 	if (r < 0) {
384 		nfc_err(&dev->dev, "Unable to register ndlc layer\n");
385 		return r;
386 	}
387 
388 	phy->irq_active = true;
389 	r = devm_request_threaded_irq(&dev->dev, dev->irq, NULL,
390 				st_nci_irq_thread_fn,
391 				phy->irq_polarity | IRQF_ONESHOT,
392 				ST_NCI_SPI_DRIVER_NAME, phy);
393 	if (r < 0)
394 		nfc_err(&dev->dev, "Unable to register IRQ handler\n");
395 
396 	return r;
397 }
398 
399 static int st_nci_spi_remove(struct spi_device *dev)
400 {
401 	struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
402 
403 	dev_dbg(&dev->dev, "%s\n", __func__);
404 
405 	ndlc_remove(phy->ndlc);
406 
407 	return 0;
408 }
409 
410 static struct spi_device_id st_nci_spi_id_table[] = {
411 	{ST_NCI_SPI_DRIVER_NAME, 0},
412 	{}
413 };
414 MODULE_DEVICE_TABLE(spi, st_nci_spi_id_table);
415 
416 static const struct acpi_device_id st_nci_spi_acpi_match[] = {
417 	{"SMO2101", 0},
418 	{}
419 };
420 MODULE_DEVICE_TABLE(acpi, st_nci_spi_acpi_match);
421 
422 static const struct of_device_id of_st_nci_spi_match[] = {
423 	{ .compatible = "st,st21nfcb-spi", },
424 	{}
425 };
426 MODULE_DEVICE_TABLE(of, of_st_nci_spi_match);
427 
428 static struct spi_driver st_nci_spi_driver = {
429 	.driver = {
430 		.name = ST_NCI_SPI_DRIVER_NAME,
431 		.of_match_table = of_match_ptr(of_st_nci_spi_match),
432 		.acpi_match_table = ACPI_PTR(st_nci_spi_acpi_match),
433 	},
434 	.probe = st_nci_spi_probe,
435 	.id_table = st_nci_spi_id_table,
436 	.remove = st_nci_spi_remove,
437 };
438 module_spi_driver(st_nci_spi_driver);
439 
440 MODULE_LICENSE("GPL");
441 MODULE_DESCRIPTION(DRIVER_DESC);
442