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