Commit | Line | Data |
---|---|---|
2bc4d4f8 CR |
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> | |
a1269dd1 | 28 | #include <net/nfc/nci.h> |
2bc4d4f8 CR |
29 | #include <linux/platform_data/st-nci.h> |
30 | ||
e67e7e59 | 31 | #include "st-nci.h" |
2bc4d4f8 CR |
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 | ||
58 | #define SPI_DUMP_SKB(info, skb) \ | |
59 | do { \ | |
60 | pr_debug("%s:\n", info); \ | |
61 | print_hex_dump(KERN_DEBUG, "spi: ", DUMP_PREFIX_OFFSET, \ | |
62 | 16, 1, (skb)->data, (skb)->len, 0); \ | |
63 | } while (0) | |
64 | ||
65 | static int st_nci_spi_enable(void *phy_id) | |
66 | { | |
67 | struct st_nci_spi_phy *phy = phy_id; | |
68 | ||
69 | gpio_set_value(phy->gpio_reset, 0); | |
70 | usleep_range(10000, 15000); | |
71 | gpio_set_value(phy->gpio_reset, 1); | |
72 | usleep_range(80000, 85000); | |
73 | ||
74 | if (phy->ndlc->powered == 0) | |
75 | enable_irq(phy->spi_dev->irq); | |
76 | ||
77 | return 0; | |
78 | } | |
79 | ||
80 | static void st_nci_spi_disable(void *phy_id) | |
81 | { | |
82 | struct st_nci_spi_phy *phy = phy_id; | |
83 | ||
84 | disable_irq_nosync(phy->spi_dev->irq); | |
85 | } | |
86 | ||
87 | /* | |
88 | * Writing a frame must not return the number of written bytes. | |
89 | * It must return either zero for success, or <0 for error. | |
90 | * In addition, it must not alter the skb | |
91 | */ | |
92 | static int st_nci_spi_write(void *phy_id, struct sk_buff *skb) | |
93 | { | |
94 | int r; | |
95 | struct st_nci_spi_phy *phy = phy_id; | |
96 | struct spi_device *dev = phy->spi_dev; | |
97 | struct sk_buff *skb_rx; | |
a1269dd1 CR |
98 | u8 buf[ST_NCI_SPI_MAX_SIZE + NCI_DATA_HDR_SIZE + |
99 | ST_NCI_FRAME_HEADROOM + ST_NCI_FRAME_TAILROOM]; | |
2bc4d4f8 CR |
100 | struct spi_transfer spi_xfer = { |
101 | .tx_buf = skb->data, | |
102 | .rx_buf = buf, | |
103 | .len = skb->len, | |
104 | }; | |
105 | ||
106 | SPI_DUMP_SKB("st_nci_spi_write", skb); | |
107 | ||
108 | if (phy->ndlc->hard_fault != 0) | |
109 | return phy->ndlc->hard_fault; | |
110 | ||
111 | r = spi_sync_transfer(dev, &spi_xfer, 1); | |
112 | /* | |
113 | * We may have received some valuable data on miso line. | |
114 | * Send them back in the ndlc state machine. | |
115 | */ | |
116 | if (!r) { | |
117 | skb_rx = alloc_skb(skb->len, GFP_KERNEL); | |
118 | if (!skb_rx) { | |
119 | r = -ENOMEM; | |
120 | goto exit; | |
121 | } | |
122 | ||
123 | skb_put(skb_rx, skb->len); | |
124 | memcpy(skb_rx->data, buf, skb->len); | |
125 | ndlc_recv(phy->ndlc, skb_rx); | |
126 | } | |
127 | ||
128 | exit: | |
129 | return r; | |
130 | } | |
131 | ||
132 | /* | |
133 | * Reads an ndlc frame and returns it in a newly allocated sk_buff. | |
134 | * returns: | |
135 | * 0 : if received frame is complete | |
136 | * -EREMOTEIO : i2c read error (fatal) | |
137 | * -EBADMSG : frame was incorrect and discarded | |
138 | * -ENOMEM : cannot allocate skb, frame dropped | |
139 | */ | |
140 | static int st_nci_spi_read(struct st_nci_spi_phy *phy, | |
141 | struct sk_buff **skb) | |
142 | { | |
143 | int r; | |
144 | u8 len; | |
145 | u8 buf[ST_NCI_SPI_MAX_SIZE]; | |
146 | struct spi_device *dev = phy->spi_dev; | |
147 | struct spi_transfer spi_xfer = { | |
148 | .rx_buf = buf, | |
149 | .len = ST_NCI_SPI_MIN_SIZE, | |
150 | }; | |
151 | ||
152 | r = spi_sync_transfer(dev, &spi_xfer, 1); | |
153 | if (r < 0) | |
154 | return -EREMOTEIO; | |
155 | ||
156 | len = be16_to_cpu(*(__be16 *) (buf + 2)); | |
157 | if (len > ST_NCI_SPI_MAX_SIZE) { | |
158 | nfc_err(&dev->dev, "invalid frame len\n"); | |
159 | phy->ndlc->hard_fault = 1; | |
160 | return -EBADMSG; | |
161 | } | |
162 | ||
163 | *skb = alloc_skb(ST_NCI_SPI_MIN_SIZE + len, GFP_KERNEL); | |
164 | if (*skb == NULL) | |
165 | return -ENOMEM; | |
166 | ||
167 | skb_reserve(*skb, ST_NCI_SPI_MIN_SIZE); | |
168 | skb_put(*skb, ST_NCI_SPI_MIN_SIZE); | |
169 | memcpy((*skb)->data, buf, ST_NCI_SPI_MIN_SIZE); | |
170 | ||
171 | if (!len) | |
172 | return 0; | |
173 | ||
174 | spi_xfer.len = len; | |
175 | r = spi_sync_transfer(dev, &spi_xfer, 1); | |
176 | if (r < 0) { | |
177 | kfree_skb(*skb); | |
178 | return -EREMOTEIO; | |
179 | } | |
180 | ||
181 | skb_put(*skb, len); | |
182 | memcpy((*skb)->data + ST_NCI_SPI_MIN_SIZE, buf, len); | |
183 | ||
184 | SPI_DUMP_SKB("spi frame read", *skb); | |
185 | ||
186 | return 0; | |
187 | } | |
188 | ||
189 | /* | |
190 | * Reads an ndlc frame from the chip. | |
191 | * | |
192 | * On ST21NFCB, IRQ goes in idle state when read starts. | |
193 | */ | |
194 | static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id) | |
195 | { | |
196 | struct st_nci_spi_phy *phy = phy_id; | |
197 | struct spi_device *dev; | |
198 | struct sk_buff *skb = NULL; | |
199 | int r; | |
200 | ||
201 | if (!phy || !phy->ndlc || irq != phy->spi_dev->irq) { | |
202 | WARN_ON_ONCE(1); | |
203 | return IRQ_NONE; | |
204 | } | |
205 | ||
206 | dev = phy->spi_dev; | |
207 | dev_dbg(&dev->dev, "IRQ\n"); | |
208 | ||
209 | if (phy->ndlc->hard_fault) | |
210 | return IRQ_HANDLED; | |
211 | ||
212 | if (!phy->ndlc->powered) { | |
213 | st_nci_spi_disable(phy); | |
214 | return IRQ_HANDLED; | |
215 | } | |
216 | ||
217 | r = st_nci_spi_read(phy, &skb); | |
218 | if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG) | |
219 | return IRQ_HANDLED; | |
220 | ||
221 | ndlc_recv(phy->ndlc, skb); | |
222 | ||
223 | return IRQ_HANDLED; | |
224 | } | |
225 | ||
226 | static struct nfc_phy_ops spi_phy_ops = { | |
227 | .write = st_nci_spi_write, | |
228 | .enable = st_nci_spi_enable, | |
229 | .disable = st_nci_spi_disable, | |
230 | }; | |
231 | ||
232 | #ifdef CONFIG_OF | |
233 | static int st_nci_spi_of_request_resources(struct spi_device *dev) | |
234 | { | |
235 | struct st_nci_spi_phy *phy = spi_get_drvdata(dev); | |
236 | struct device_node *pp; | |
237 | int gpio; | |
238 | int r; | |
239 | ||
240 | pp = dev->dev.of_node; | |
241 | if (!pp) | |
242 | return -ENODEV; | |
243 | ||
244 | /* Get GPIO from device tree */ | |
245 | gpio = of_get_named_gpio(pp, "reset-gpios", 0); | |
246 | if (gpio < 0) { | |
247 | nfc_err(&dev->dev, | |
248 | "Failed to retrieve reset-gpios from device tree\n"); | |
249 | return gpio; | |
250 | } | |
251 | ||
252 | /* GPIO request and configuration */ | |
253 | r = devm_gpio_request_one(&dev->dev, gpio, | |
254 | GPIOF_OUT_INIT_HIGH, "clf_reset"); | |
255 | if (r) { | |
256 | nfc_err(&dev->dev, "Failed to request reset pin\n"); | |
257 | return r; | |
258 | } | |
259 | phy->gpio_reset = gpio; | |
260 | ||
261 | phy->irq_polarity = irq_get_trigger_type(dev->irq); | |
262 | ||
263 | return 0; | |
264 | } | |
265 | #else | |
266 | static int st_nci_spi_of_request_resources(struct spi_device *dev) | |
267 | { | |
268 | return -ENODEV; | |
269 | } | |
270 | #endif | |
271 | ||
272 | static int st_nci_spi_request_resources(struct spi_device *dev) | |
273 | { | |
274 | struct st_nci_nfc_platform_data *pdata; | |
275 | struct st_nci_spi_phy *phy = spi_get_drvdata(dev); | |
276 | int r; | |
277 | ||
278 | pdata = dev->dev.platform_data; | |
279 | if (pdata == NULL) { | |
280 | nfc_err(&dev->dev, "No platform data\n"); | |
281 | return -EINVAL; | |
282 | } | |
283 | ||
284 | /* store for later use */ | |
285 | phy->gpio_reset = pdata->gpio_reset; | |
286 | phy->irq_polarity = pdata->irq_polarity; | |
287 | ||
288 | r = devm_gpio_request_one(&dev->dev, | |
289 | phy->gpio_reset, GPIOF_OUT_INIT_HIGH, "clf_reset"); | |
290 | if (r) { | |
291 | pr_err("%s : reset gpio_request failed\n", __FILE__); | |
292 | return r; | |
293 | } | |
294 | ||
295 | return 0; | |
296 | } | |
297 | ||
298 | static int st_nci_spi_probe(struct spi_device *dev) | |
299 | { | |
300 | struct st_nci_spi_phy *phy; | |
301 | struct st_nci_nfc_platform_data *pdata; | |
302 | int r; | |
303 | ||
304 | dev_dbg(&dev->dev, "%s\n", __func__); | |
305 | dev_dbg(&dev->dev, "IRQ: %d\n", dev->irq); | |
306 | ||
307 | /* Check SPI platform functionnalities */ | |
308 | if (!dev) { | |
309 | pr_debug("%s: dev is NULL. Device is not accessible.\n", | |
310 | __func__); | |
311 | return -ENODEV; | |
312 | } | |
313 | ||
314 | phy = devm_kzalloc(&dev->dev, sizeof(struct st_nci_spi_phy), | |
315 | GFP_KERNEL); | |
316 | if (!phy) | |
317 | return -ENOMEM; | |
318 | ||
319 | phy->spi_dev = dev; | |
320 | ||
321 | spi_set_drvdata(dev, phy); | |
322 | ||
323 | pdata = dev->dev.platform_data; | |
324 | if (!pdata && dev->dev.of_node) { | |
325 | r = st_nci_spi_of_request_resources(dev); | |
326 | if (r) { | |
327 | nfc_err(&dev->dev, "No platform data\n"); | |
328 | return r; | |
329 | } | |
330 | } else if (pdata) { | |
331 | r = st_nci_spi_request_resources(dev); | |
332 | if (r) { | |
333 | nfc_err(&dev->dev, | |
334 | "Cannot get platform resources\n"); | |
335 | return r; | |
336 | } | |
337 | } else { | |
338 | nfc_err(&dev->dev, | |
339 | "st_nci platform resources not available\n"); | |
340 | return -ENODEV; | |
341 | } | |
342 | ||
343 | r = ndlc_probe(phy, &spi_phy_ops, &dev->dev, | |
344 | ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM, | |
345 | &phy->ndlc); | |
346 | if (r < 0) { | |
347 | nfc_err(&dev->dev, "Unable to register ndlc layer\n"); | |
348 | return r; | |
349 | } | |
350 | ||
351 | r = devm_request_threaded_irq(&dev->dev, dev->irq, NULL, | |
352 | st_nci_irq_thread_fn, | |
353 | phy->irq_polarity | IRQF_ONESHOT, | |
354 | ST_NCI_SPI_DRIVER_NAME, phy); | |
355 | if (r < 0) | |
356 | nfc_err(&dev->dev, "Unable to register IRQ handler\n"); | |
357 | ||
358 | return r; | |
359 | } | |
360 | ||
361 | static int st_nci_spi_remove(struct spi_device *dev) | |
362 | { | |
363 | struct st_nci_spi_phy *phy = spi_get_drvdata(dev); | |
364 | ||
365 | dev_dbg(&dev->dev, "%s\n", __func__); | |
366 | ||
367 | ndlc_remove(phy->ndlc); | |
368 | ||
369 | return 0; | |
370 | } | |
371 | ||
372 | #ifdef CONFIG_OF | |
373 | static const struct of_device_id of_st_nci_spi_match[] = { | |
374 | { .compatible = "st,st21nfcb-spi", }, | |
375 | {} | |
376 | }; | |
377 | MODULE_DEVICE_TABLE(of, of_st_nci_spi_match); | |
378 | #endif | |
379 | ||
380 | static struct spi_driver st_nci_spi_driver = { | |
381 | .driver = { | |
382 | .owner = THIS_MODULE, | |
383 | .name = ST_NCI_SPI_DRIVER_NAME, | |
384 | .of_match_table = of_match_ptr(of_st_nci_spi_match), | |
385 | }, | |
386 | .probe = st_nci_spi_probe, | |
387 | .id_table = st_nci_spi_id_table, | |
388 | .remove = st_nci_spi_remove, | |
389 | }; | |
390 | ||
391 | module_spi_driver(st_nci_spi_driver); | |
392 | ||
393 | MODULE_LICENSE("GPL"); | |
394 | MODULE_DESCRIPTION(DRIVER_DESC); |