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