Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / nfc / st-nci / spi.c
blob821dfa950fa8f89f924eed83a9339b9b751f6dc8
1 /*
2 * SPI Link Layer for ST NCI based Driver
3 * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
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.
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.
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/>.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
33 #include "st-nci.h"
35 #define DRIVER_DESC "NCI NFC driver for ST_NCI"
37 /* ndlc header */
38 #define ST_NCI_FRAME_HEADROOM 1
39 #define ST_NCI_FRAME_TAILROOM 0
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 */
44 #define ST_NCI_SPI_DRIVER_NAME "st_nci_spi"
46 #define ST_NCI_GPIO_NAME_RESET "clf_reset"
48 struct st_nci_spi_phy {
49 struct spi_device *spi_dev;
50 struct llt_ndlc *ndlc;
52 bool irq_active;
54 unsigned int gpio_reset;
55 unsigned int irq_polarity;
57 struct st_nci_se_status se_status;
60 static int st_nci_spi_enable(void *phy_id)
62 struct st_nci_spi_phy *phy = phy_id;
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);
69 if (phy->ndlc->powered == 0 && phy->irq_active == 0) {
70 enable_irq(phy->spi_dev->irq);
71 phy->irq_active = true;
74 return 0;
77 static void st_nci_spi_disable(void *phy_id)
79 struct st_nci_spi_phy *phy = phy_id;
81 disable_irq_nosync(phy->spi_dev->irq);
82 phy->irq_active = false;
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
90 static int st_nci_spi_write(void *phy_id, struct sk_buff *skb)
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,
104 if (phy->ndlc->hard_fault != 0)
105 return phy->ndlc->hard_fault;
107 r = spi_sync_transfer(dev, &spi_xfer, 1);
109 * We may have received some valuable data on miso line.
110 * Send them back in the ndlc state machine.
112 if (!r) {
113 skb_rx = alloc_skb(skb->len, GFP_KERNEL);
114 if (!skb_rx) {
115 r = -ENOMEM;
116 goto exit;
119 skb_put(skb_rx, skb->len);
120 memcpy(skb_rx->data, buf, skb->len);
121 ndlc_recv(phy->ndlc, skb_rx);
124 exit:
125 return r;
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
136 static int st_nci_spi_read(struct st_nci_spi_phy *phy,
137 struct sk_buff **skb)
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,
148 r = spi_sync_transfer(dev, &spi_xfer, 1);
149 if (r < 0)
150 return -EREMOTEIO;
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;
159 *skb = alloc_skb(ST_NCI_SPI_MIN_SIZE + len, GFP_KERNEL);
160 if (*skb == NULL)
161 return -ENOMEM;
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);
167 if (!len)
168 return 0;
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;
177 skb_put(*skb, len);
178 memcpy((*skb)->data + ST_NCI_SPI_MIN_SIZE, buf, len);
180 return 0;
184 * Reads an ndlc frame from the chip.
186 * On ST21NFCB, IRQ goes in idle state when read starts.
188 static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
190 struct st_nci_spi_phy *phy = phy_id;
191 struct spi_device *dev;
192 struct sk_buff *skb = NULL;
193 int r;
195 if (!phy || !phy->ndlc || irq != phy->spi_dev->irq) {
196 WARN_ON_ONCE(1);
197 return IRQ_NONE;
200 dev = phy->spi_dev;
201 dev_dbg(&dev->dev, "IRQ\n");
203 if (phy->ndlc->hard_fault)
204 return IRQ_HANDLED;
206 if (!phy->ndlc->powered) {
207 st_nci_spi_disable(phy);
208 return IRQ_HANDLED;
211 r = st_nci_spi_read(phy, &skb);
212 if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
213 return IRQ_HANDLED;
215 ndlc_recv(phy->ndlc, skb);
217 return IRQ_HANDLED;
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,
226 static int st_nci_spi_acpi_request_resources(struct spi_device *spi_dev)
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;
233 if (!spi_dev)
234 return -EINVAL;
236 dev = &spi_dev->dev;
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;
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;
251 phy->gpio_reset = desc_to_gpio(gpiod_reset);
253 phy->irq_polarity = irq_get_trigger_type(spi_dev->irq);
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");
260 return 0;
263 static int st_nci_spi_of_request_resources(struct spi_device *dev)
265 struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
266 struct device_node *pp;
267 int gpio;
268 int r;
270 pp = dev->dev.of_node;
271 if (!pp)
272 return -ENODEV;
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;
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;
289 phy->gpio_reset = gpio;
291 phy->irq_polarity = irq_get_trigger_type(dev->irq);
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");
298 return 0;
301 static int st_nci_spi_request_resources(struct spi_device *dev)
303 struct st_nci_nfc_platform_data *pdata;
304 struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
305 int r;
307 pdata = dev->dev.platform_data;
308 if (pdata == NULL) {
309 nfc_err(&dev->dev, "No platform data\n");
310 return -EINVAL;
313 /* store for later use */
314 phy->gpio_reset = pdata->gpio_reset;
315 phy->irq_polarity = pdata->irq_polarity;
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;
325 phy->se_status.is_ese_present = pdata->is_ese_present;
326 phy->se_status.is_uicc_present = pdata->is_uicc_present;
328 return 0;
331 static int st_nci_spi_probe(struct spi_device *dev)
333 struct st_nci_spi_phy *phy;
334 struct st_nci_nfc_platform_data *pdata;
335 int r;
337 dev_dbg(&dev->dev, "%s\n", __func__);
338 dev_dbg(&dev->dev, "IRQ: %d\n", dev->irq);
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;
347 phy = devm_kzalloc(&dev->dev, sizeof(struct st_nci_spi_phy),
348 GFP_KERNEL);
349 if (!phy)
350 return -ENOMEM;
352 phy->spi_dev = dev;
354 spi_set_drvdata(dev, phy);
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;
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;
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;
376 } else {
377 nfc_err(&dev->dev,
378 "st_nci platform resources not available\n");
379 return -ENODEV;
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;
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");
398 return r;
401 static int st_nci_spi_remove(struct spi_device *dev)
403 struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
405 dev_dbg(&dev->dev, "%s\n", __func__);
407 ndlc_remove(phy->ndlc);
409 return 0;
412 static struct spi_device_id st_nci_spi_id_table[] = {
413 {ST_NCI_SPI_DRIVER_NAME, 0},
416 MODULE_DEVICE_TABLE(spi, st_nci_spi_id_table);
418 static const struct acpi_device_id st_nci_spi_acpi_match[] = {
419 {"SMO2101", 0},
422 MODULE_DEVICE_TABLE(acpi, st_nci_spi_acpi_match);
424 static const struct of_device_id of_st_nci_spi_match[] = {
425 { .compatible = "st,st21nfcb-spi", },
428 MODULE_DEVICE_TABLE(of, of_st_nci_spi_match);
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),
436 .probe = st_nci_spi_probe,
437 .id_table = st_nci_spi_id_table,
438 .remove = st_nci_spi_remove,
440 module_spi_driver(st_nci_spi_driver);
442 MODULE_LICENSE("GPL");
443 MODULE_DESCRIPTION(DRIVER_DESC);