]>
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> | |
60cd6d89 | 22 | #include <linux/gpio/consumer.h> |
60cd6d89 | 23 | #include <linux/acpi.h> |
2bc4d4f8 CR |
24 | #include <linux/interrupt.h> |
25 | #include <linux/delay.h> | |
26 | #include <linux/nfc.h> | |
a89e68f1 | 27 | #include <linux/of.h> |
a1269dd1 | 28 | #include <net/nfc/nci.h> |
2bc4d4f8 | 29 | |
e67e7e59 | 30 | #include "st-nci.h" |
2bc4d4f8 CR |
31 | |
32 | #define DRIVER_DESC "NCI NFC driver for ST_NCI" | |
33 | ||
34 | /* ndlc header */ | |
35 | #define ST_NCI_FRAME_HEADROOM 1 | |
ba2c231c | 36 | #define ST_NCI_FRAME_TAILROOM 0 |
2bc4d4f8 CR |
37 | |
38 | #define ST_NCI_SPI_MIN_SIZE 4 /* PCB(1) + NCI Packet header(3) */ | |
39 | #define ST_NCI_SPI_MAX_SIZE 250 /* req 4.2.1 */ | |
40 | ||
61a04101 | 41 | #define ST_NCI_DRIVER_NAME "st_nci" |
2bc4d4f8 CR |
42 | #define ST_NCI_SPI_DRIVER_NAME "st_nci_spi" |
43 | ||
de72dbc5 | 44 | #define ST_NCI_GPIO_NAME_RESET "reset" |
4c62c208 | 45 | |
2bc4d4f8 CR |
46 | struct st_nci_spi_phy { |
47 | struct spi_device *spi_dev; | |
48 | struct llt_ndlc *ndlc; | |
49 | ||
bb2496c3 CR |
50 | bool irq_active; |
51 | ||
a89e68f1 | 52 | struct gpio_desc *gpiod_reset; |
3648dc6d CR |
53 | |
54 | struct st_nci_se_status se_status; | |
2bc4d4f8 CR |
55 | }; |
56 | ||
2bc4d4f8 CR |
57 | static int st_nci_spi_enable(void *phy_id) |
58 | { | |
59 | struct st_nci_spi_phy *phy = phy_id; | |
60 | ||
a89e68f1 | 61 | gpiod_set_value(phy->gpiod_reset, 0); |
2bc4d4f8 | 62 | usleep_range(10000, 15000); |
a89e68f1 | 63 | gpiod_set_value(phy->gpiod_reset, 1); |
2bc4d4f8 CR |
64 | usleep_range(80000, 85000); |
65 | ||
bb2496c3 | 66 | if (phy->ndlc->powered == 0 && phy->irq_active == 0) { |
2bc4d4f8 | 67 | enable_irq(phy->spi_dev->irq); |
bb2496c3 CR |
68 | phy->irq_active = true; |
69 | } | |
2bc4d4f8 CR |
70 | |
71 | return 0; | |
72 | } | |
73 | ||
74 | static void st_nci_spi_disable(void *phy_id) | |
75 | { | |
76 | struct st_nci_spi_phy *phy = phy_id; | |
77 | ||
78 | disable_irq_nosync(phy->spi_dev->irq); | |
bb2496c3 | 79 | phy->irq_active = false; |
2bc4d4f8 CR |
80 | } |
81 | ||
82 | /* | |
83 | * Writing a frame must not return the number of written bytes. | |
84 | * It must return either zero for success, or <0 for error. | |
85 | * In addition, it must not alter the skb | |
86 | */ | |
87 | static int st_nci_spi_write(void *phy_id, struct sk_buff *skb) | |
88 | { | |
89 | int r; | |
90 | struct st_nci_spi_phy *phy = phy_id; | |
91 | struct spi_device *dev = phy->spi_dev; | |
92 | struct sk_buff *skb_rx; | |
a1269dd1 CR |
93 | u8 buf[ST_NCI_SPI_MAX_SIZE + NCI_DATA_HDR_SIZE + |
94 | ST_NCI_FRAME_HEADROOM + ST_NCI_FRAME_TAILROOM]; | |
2bc4d4f8 CR |
95 | struct spi_transfer spi_xfer = { |
96 | .tx_buf = skb->data, | |
97 | .rx_buf = buf, | |
98 | .len = skb->len, | |
99 | }; | |
100 | ||
2bc4d4f8 CR |
101 | if (phy->ndlc->hard_fault != 0) |
102 | return phy->ndlc->hard_fault; | |
103 | ||
104 | r = spi_sync_transfer(dev, &spi_xfer, 1); | |
105 | /* | |
106 | * We may have received some valuable data on miso line. | |
107 | * Send them back in the ndlc state machine. | |
108 | */ | |
109 | if (!r) { | |
110 | skb_rx = alloc_skb(skb->len, GFP_KERNEL); | |
111 | if (!skb_rx) { | |
112 | r = -ENOMEM; | |
113 | goto exit; | |
114 | } | |
115 | ||
116 | skb_put(skb_rx, skb->len); | |
117 | memcpy(skb_rx->data, buf, skb->len); | |
118 | ndlc_recv(phy->ndlc, skb_rx); | |
119 | } | |
120 | ||
121 | exit: | |
122 | return r; | |
123 | } | |
124 | ||
125 | /* | |
126 | * Reads an ndlc frame and returns it in a newly allocated sk_buff. | |
127 | * returns: | |
128 | * 0 : if received frame is complete | |
129 | * -EREMOTEIO : i2c read error (fatal) | |
130 | * -EBADMSG : frame was incorrect and discarded | |
131 | * -ENOMEM : cannot allocate skb, frame dropped | |
132 | */ | |
133 | static int st_nci_spi_read(struct st_nci_spi_phy *phy, | |
134 | struct sk_buff **skb) | |
135 | { | |
136 | int r; | |
137 | u8 len; | |
138 | u8 buf[ST_NCI_SPI_MAX_SIZE]; | |
139 | struct spi_device *dev = phy->spi_dev; | |
140 | struct spi_transfer spi_xfer = { | |
141 | .rx_buf = buf, | |
142 | .len = ST_NCI_SPI_MIN_SIZE, | |
143 | }; | |
144 | ||
145 | r = spi_sync_transfer(dev, &spi_xfer, 1); | |
146 | if (r < 0) | |
147 | return -EREMOTEIO; | |
148 | ||
149 | len = be16_to_cpu(*(__be16 *) (buf + 2)); | |
150 | if (len > ST_NCI_SPI_MAX_SIZE) { | |
151 | nfc_err(&dev->dev, "invalid frame len\n"); | |
152 | phy->ndlc->hard_fault = 1; | |
153 | return -EBADMSG; | |
154 | } | |
155 | ||
156 | *skb = alloc_skb(ST_NCI_SPI_MIN_SIZE + len, GFP_KERNEL); | |
157 | if (*skb == NULL) | |
158 | return -ENOMEM; | |
159 | ||
160 | skb_reserve(*skb, ST_NCI_SPI_MIN_SIZE); | |
161 | skb_put(*skb, ST_NCI_SPI_MIN_SIZE); | |
162 | memcpy((*skb)->data, buf, ST_NCI_SPI_MIN_SIZE); | |
163 | ||
164 | if (!len) | |
165 | return 0; | |
166 | ||
167 | spi_xfer.len = len; | |
168 | r = spi_sync_transfer(dev, &spi_xfer, 1); | |
169 | if (r < 0) { | |
170 | kfree_skb(*skb); | |
171 | return -EREMOTEIO; | |
172 | } | |
173 | ||
174 | skb_put(*skb, len); | |
175 | memcpy((*skb)->data + ST_NCI_SPI_MIN_SIZE, buf, len); | |
176 | ||
2bc4d4f8 CR |
177 | return 0; |
178 | } | |
179 | ||
180 | /* | |
181 | * Reads an ndlc frame from the chip. | |
182 | * | |
183 | * On ST21NFCB, IRQ goes in idle state when read starts. | |
184 | */ | |
185 | static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id) | |
186 | { | |
187 | struct st_nci_spi_phy *phy = phy_id; | |
188 | struct spi_device *dev; | |
189 | struct sk_buff *skb = NULL; | |
190 | int r; | |
191 | ||
192 | if (!phy || !phy->ndlc || irq != phy->spi_dev->irq) { | |
193 | WARN_ON_ONCE(1); | |
194 | return IRQ_NONE; | |
195 | } | |
196 | ||
197 | dev = phy->spi_dev; | |
198 | dev_dbg(&dev->dev, "IRQ\n"); | |
199 | ||
200 | if (phy->ndlc->hard_fault) | |
201 | return IRQ_HANDLED; | |
202 | ||
203 | if (!phy->ndlc->powered) { | |
204 | st_nci_spi_disable(phy); | |
205 | return IRQ_HANDLED; | |
206 | } | |
207 | ||
208 | r = st_nci_spi_read(phy, &skb); | |
209 | if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG) | |
210 | return IRQ_HANDLED; | |
211 | ||
212 | ndlc_recv(phy->ndlc, skb); | |
213 | ||
214 | return IRQ_HANDLED; | |
215 | } | |
216 | ||
217 | static struct nfc_phy_ops spi_phy_ops = { | |
218 | .write = st_nci_spi_write, | |
219 | .enable = st_nci_spi_enable, | |
220 | .disable = st_nci_spi_disable, | |
221 | }; | |
222 | ||
60cd6d89 CR |
223 | static int st_nci_spi_acpi_request_resources(struct spi_device *spi_dev) |
224 | { | |
225 | struct st_nci_spi_phy *phy = spi_get_drvdata(spi_dev); | |
1f34b204 | 226 | struct device *dev = &spi_dev->dev; |
60cd6d89 | 227 | |
60cd6d89 | 228 | /* Get RESET GPIO from ACPI */ |
a89e68f1 AS |
229 | phy->gpiod_reset = devm_gpiod_get_index(dev, ST_NCI_GPIO_NAME_RESET, |
230 | 1, GPIOD_OUT_HIGH); | |
231 | if (IS_ERR(phy->gpiod_reset)) { | |
60cd6d89 | 232 | nfc_err(dev, "Unable to get RESET GPIO\n"); |
a89e68f1 | 233 | return PTR_ERR(phy->gpiod_reset); |
60cd6d89 CR |
234 | } |
235 | ||
60cd6d89 CR |
236 | return 0; |
237 | } | |
238 | ||
a89e68f1 | 239 | static int st_nci_spi_of_request_resources(struct spi_device *spi) |
2bc4d4f8 | 240 | { |
a89e68f1 AS |
241 | struct st_nci_spi_phy *phy = spi_get_drvdata(spi); |
242 | struct device *dev = &spi->dev; | |
2bc4d4f8 CR |
243 | |
244 | /* Get GPIO from device tree */ | |
a89e68f1 AS |
245 | phy->gpiod_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); |
246 | if (IS_ERR(phy->gpiod_reset)) { | |
247 | nfc_err(dev, "Unable to get RESET GPIO\n"); | |
248 | return PTR_ERR(phy->gpiod_reset); | |
2bc4d4f8 | 249 | } |
2bc4d4f8 | 250 | |
2bc4d4f8 CR |
251 | return 0; |
252 | } | |
2bc4d4f8 | 253 | |
2bc4d4f8 CR |
254 | static int st_nci_spi_probe(struct spi_device *dev) |
255 | { | |
256 | struct st_nci_spi_phy *phy; | |
2bc4d4f8 CR |
257 | int r; |
258 | ||
259 | dev_dbg(&dev->dev, "%s\n", __func__); | |
260 | dev_dbg(&dev->dev, "IRQ: %d\n", dev->irq); | |
261 | ||
262 | /* Check SPI platform functionnalities */ | |
263 | if (!dev) { | |
264 | pr_debug("%s: dev is NULL. Device is not accessible.\n", | |
265 | __func__); | |
266 | return -ENODEV; | |
267 | } | |
268 | ||
269 | phy = devm_kzalloc(&dev->dev, sizeof(struct st_nci_spi_phy), | |
270 | GFP_KERNEL); | |
271 | if (!phy) | |
272 | return -ENOMEM; | |
273 | ||
274 | phy->spi_dev = dev; | |
275 | ||
276 | spi_set_drvdata(dev, phy); | |
277 | ||
61a04101 | 278 | if (dev->dev.of_node) { |
2bc4d4f8 CR |
279 | r = st_nci_spi_of_request_resources(dev); |
280 | if (r) { | |
281 | nfc_err(&dev->dev, "No platform data\n"); | |
282 | return r; | |
283 | } | |
60cd6d89 CR |
284 | } else if (ACPI_HANDLE(&dev->dev)) { |
285 | r = st_nci_spi_acpi_request_resources(dev); | |
286 | if (r) { | |
287 | nfc_err(&dev->dev, "Cannot get ACPI data\n"); | |
288 | return r; | |
289 | } | |
2bc4d4f8 CR |
290 | } else { |
291 | nfc_err(&dev->dev, | |
292 | "st_nci platform resources not available\n"); | |
293 | return -ENODEV; | |
294 | } | |
295 | ||
75719b2b AS |
296 | phy->se_status.is_ese_present = |
297 | device_property_read_bool(&dev->dev, "ese-present"); | |
298 | phy->se_status.is_uicc_present = | |
299 | device_property_read_bool(&dev->dev, "uicc-present"); | |
300 | ||
2bc4d4f8 CR |
301 | r = ndlc_probe(phy, &spi_phy_ops, &dev->dev, |
302 | ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM, | |
3648dc6d | 303 | &phy->ndlc, &phy->se_status); |
2bc4d4f8 CR |
304 | if (r < 0) { |
305 | nfc_err(&dev->dev, "Unable to register ndlc layer\n"); | |
306 | return r; | |
307 | } | |
308 | ||
bb2496c3 | 309 | phy->irq_active = true; |
2bc4d4f8 CR |
310 | r = devm_request_threaded_irq(&dev->dev, dev->irq, NULL, |
311 | st_nci_irq_thread_fn, | |
1af9fea6 | 312 | IRQF_ONESHOT, |
2bc4d4f8 CR |
313 | ST_NCI_SPI_DRIVER_NAME, phy); |
314 | if (r < 0) | |
315 | nfc_err(&dev->dev, "Unable to register IRQ handler\n"); | |
316 | ||
317 | return r; | |
318 | } | |
319 | ||
320 | static int st_nci_spi_remove(struct spi_device *dev) | |
321 | { | |
322 | struct st_nci_spi_phy *phy = spi_get_drvdata(dev); | |
323 | ||
324 | dev_dbg(&dev->dev, "%s\n", __func__); | |
325 | ||
326 | ndlc_remove(phy->ndlc); | |
327 | ||
328 | return 0; | |
329 | } | |
330 | ||
3252897f CR |
331 | static struct spi_device_id st_nci_spi_id_table[] = { |
332 | {ST_NCI_SPI_DRIVER_NAME, 0}, | |
333 | {} | |
334 | }; | |
335 | MODULE_DEVICE_TABLE(spi, st_nci_spi_id_table); | |
336 | ||
60cd6d89 CR |
337 | static const struct acpi_device_id st_nci_spi_acpi_match[] = { |
338 | {"SMO2101", 0}, | |
339 | {} | |
340 | }; | |
341 | MODULE_DEVICE_TABLE(acpi, st_nci_spi_acpi_match); | |
342 | ||
2bc4d4f8 CR |
343 | static const struct of_device_id of_st_nci_spi_match[] = { |
344 | { .compatible = "st,st21nfcb-spi", }, | |
345 | {} | |
346 | }; | |
347 | MODULE_DEVICE_TABLE(of, of_st_nci_spi_match); | |
2bc4d4f8 CR |
348 | |
349 | static struct spi_driver st_nci_spi_driver = { | |
350 | .driver = { | |
2bc4d4f8 CR |
351 | .name = ST_NCI_SPI_DRIVER_NAME, |
352 | .of_match_table = of_match_ptr(of_st_nci_spi_match), | |
60cd6d89 | 353 | .acpi_match_table = ACPI_PTR(st_nci_spi_acpi_match), |
2bc4d4f8 CR |
354 | }, |
355 | .probe = st_nci_spi_probe, | |
356 | .id_table = st_nci_spi_id_table, | |
357 | .remove = st_nci_spi_remove, | |
358 | }; | |
2bc4d4f8 CR |
359 | module_spi_driver(st_nci_spi_driver); |
360 | ||
361 | MODULE_LICENSE("GPL"); | |
362 | MODULE_DESCRIPTION(DRIVER_DESC); |