Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Marvell NFC driver: major functions |
| 3 | * |
Vincent Cuissard | fb101c0 | 2015-10-26 10:27:37 +0100 | [diff] [blame] | 4 | * Copyright (C) 2014-2015 Marvell International Ltd. |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 5 | * |
| 6 | * This software file (the "File") is distributed by Marvell International |
| 7 | * Ltd. under the terms of the GNU General Public License Version 2, June 1991 |
| 8 | * (the "License"). You may use, redistribute and/or modify this File in |
| 9 | * accordance with the terms and conditions of the License, a copy of which |
| 10 | * is available on the worldwide web at |
| 11 | * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. |
| 12 | * |
| 13 | * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE |
| 14 | * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE |
| 15 | * ARE EXPRESSLY DISCLAIMED. The License provides additional details about |
| 16 | * this warranty disclaimer. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
Vincent Cuissard | 4a2b947f | 2015-06-11 11:25:46 +0200 | [diff] [blame] | 20 | #include <linux/gpio.h> |
| 21 | #include <linux/delay.h> |
Vincent Cuissard | dc14bdef | 2015-06-11 14:00:19 +0200 | [diff] [blame] | 22 | #include <linux/of_gpio.h> |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 23 | #include <linux/nfc.h> |
| 24 | #include <net/nfc/nci.h> |
| 25 | #include <net/nfc/nci_core.h> |
| 26 | #include "nfcmrvl.h" |
| 27 | |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 28 | static int nfcmrvl_nci_open(struct nci_dev *ndev) |
| 29 | { |
| 30 | struct nfcmrvl_private *priv = nci_get_drvdata(ndev); |
| 31 | int err; |
| 32 | |
| 33 | if (test_and_set_bit(NFCMRVL_NCI_RUNNING, &priv->flags)) |
| 34 | return 0; |
| 35 | |
Vincent Cuissard | b5b3e23 | 2015-10-26 10:27:41 +0100 | [diff] [blame] | 36 | /* Reset possible fault of previous session */ |
| 37 | clear_bit(NFCMRVL_PHY_ERROR, &priv->flags); |
| 38 | |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 39 | err = priv->if_ops->nci_open(priv); |
| 40 | |
| 41 | if (err) |
| 42 | clear_bit(NFCMRVL_NCI_RUNNING, &priv->flags); |
| 43 | |
| 44 | return err; |
| 45 | } |
| 46 | |
| 47 | static int nfcmrvl_nci_close(struct nci_dev *ndev) |
| 48 | { |
| 49 | struct nfcmrvl_private *priv = nci_get_drvdata(ndev); |
| 50 | |
| 51 | if (!test_and_clear_bit(NFCMRVL_NCI_RUNNING, &priv->flags)) |
| 52 | return 0; |
| 53 | |
| 54 | priv->if_ops->nci_close(priv); |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static int nfcmrvl_nci_send(struct nci_dev *ndev, struct sk_buff *skb) |
| 60 | { |
| 61 | struct nfcmrvl_private *priv = nci_get_drvdata(ndev); |
| 62 | |
| 63 | nfc_info(priv->dev, "send entry, len %d\n", skb->len); |
| 64 | |
| 65 | skb->dev = (void *)ndev; |
| 66 | |
Vincent Cuissard | dc14bdef | 2015-06-11 14:00:19 +0200 | [diff] [blame] | 67 | if (priv->config.hci_muxed) { |
Vincent Cuissard | f1f1a7d | 2015-06-11 11:25:43 +0200 | [diff] [blame] | 68 | unsigned char *hdr; |
| 69 | unsigned char len = skb->len; |
| 70 | |
Johannes Berg | d58ff35 | 2017-06-16 14:29:23 +0200 | [diff] [blame] | 71 | hdr = skb_push(skb, NFCMRVL_HCI_EVENT_HEADER_SIZE); |
Vincent Cuissard | f1f1a7d | 2015-06-11 11:25:43 +0200 | [diff] [blame] | 72 | hdr[0] = NFCMRVL_HCI_COMMAND_CODE; |
| 73 | hdr[1] = NFCMRVL_HCI_OGF; |
| 74 | hdr[2] = NFCMRVL_HCI_OCF; |
| 75 | hdr[3] = len; |
| 76 | } |
| 77 | |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 78 | return priv->if_ops->nci_send(priv, skb); |
| 79 | } |
| 80 | |
Amitkumar Karwar | 15203b4 | 2014-01-06 12:58:20 -0800 | [diff] [blame] | 81 | static int nfcmrvl_nci_setup(struct nci_dev *ndev) |
| 82 | { |
Vincent Cuissard | d0dcad8 | 2015-06-12 15:35:54 +0200 | [diff] [blame] | 83 | __u8 val = 1; |
| 84 | |
| 85 | nci_set_config(ndev, NFCMRVL_PB_BAIL_OUT, 1, &val); |
Amitkumar Karwar | 15203b4 | 2014-01-06 12:58:20 -0800 | [diff] [blame] | 86 | return 0; |
| 87 | } |
| 88 | |
Vincent Cuissard | 3194c68 | 2015-10-26 10:27:39 +0100 | [diff] [blame] | 89 | static int nfcmrvl_nci_fw_download(struct nci_dev *ndev, |
| 90 | const char *firmware_name) |
| 91 | { |
| 92 | return nfcmrvl_fw_dnld_start(ndev, firmware_name); |
| 93 | } |
| 94 | |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 95 | static struct nci_ops nfcmrvl_nci_ops = { |
| 96 | .open = nfcmrvl_nci_open, |
| 97 | .close = nfcmrvl_nci_close, |
| 98 | .send = nfcmrvl_nci_send, |
Amitkumar Karwar | 15203b4 | 2014-01-06 12:58:20 -0800 | [diff] [blame] | 99 | .setup = nfcmrvl_nci_setup, |
Vincent Cuissard | 3194c68 | 2015-10-26 10:27:39 +0100 | [diff] [blame] | 100 | .fw_download = nfcmrvl_nci_fw_download, |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 101 | }; |
| 102 | |
Vincent Cuissard | 58d34aa | 2015-10-26 10:27:40 +0100 | [diff] [blame] | 103 | struct nfcmrvl_private *nfcmrvl_nci_register_dev(enum nfcmrvl_phy phy, |
| 104 | void *drv_data, |
Vincent Cuissard | dc14bdef | 2015-06-11 14:00:19 +0200 | [diff] [blame] | 105 | struct nfcmrvl_if_ops *ops, |
| 106 | struct device *dev, |
| 107 | struct nfcmrvl_platform_data *pdata) |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 108 | { |
| 109 | struct nfcmrvl_private *priv; |
| 110 | int rc; |
Vincent Cuissard | 58d34aa | 2015-10-26 10:27:40 +0100 | [diff] [blame] | 111 | int headroom; |
| 112 | int tailroom; |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 113 | u32 protocols; |
| 114 | |
| 115 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 116 | if (!priv) |
| 117 | return ERR_PTR(-ENOMEM); |
| 118 | |
| 119 | priv->drv_data = drv_data; |
| 120 | priv->if_ops = ops; |
| 121 | priv->dev = dev; |
Vincent Cuissard | 58d34aa | 2015-10-26 10:27:40 +0100 | [diff] [blame] | 122 | priv->phy = phy; |
Vincent Cuissard | 4a2b947f | 2015-06-11 11:25:46 +0200 | [diff] [blame] | 123 | |
Vincent Cuissard | dc14bdef | 2015-06-11 14:00:19 +0200 | [diff] [blame] | 124 | memcpy(&priv->config, pdata, sizeof(*pdata)); |
| 125 | |
Johan Hovold | e33a3f8 | 2017-03-30 12:15:42 +0200 | [diff] [blame] | 126 | if (gpio_is_valid(priv->config.reset_n_io)) { |
Johan Hovold | 0cbe401 | 2017-03-30 12:15:37 +0200 | [diff] [blame] | 127 | rc = gpio_request_one(priv->config.reset_n_io, |
| 128 | GPIOF_OUT_INIT_LOW, |
| 129 | "nfcmrvl_reset_n"); |
| 130 | if (rc < 0) { |
Johan Hovold | e33a3f8 | 2017-03-30 12:15:42 +0200 | [diff] [blame] | 131 | priv->config.reset_n_io = -EINVAL; |
Vincent Cuissard | 4a2b947f | 2015-06-11 11:25:46 +0200 | [diff] [blame] | 132 | nfc_err(dev, "failed to request reset_n io\n"); |
Johan Hovold | 0cbe401 | 2017-03-30 12:15:37 +0200 | [diff] [blame] | 133 | } |
Vincent Cuissard | 4a2b947f | 2015-06-11 11:25:46 +0200 | [diff] [blame] | 134 | } |
Vincent Cuissard | f1f1a7d | 2015-06-11 11:25:43 +0200 | [diff] [blame] | 135 | |
Vincent Cuissard | caf6e49 | 2015-10-26 10:27:44 +0100 | [diff] [blame] | 136 | if (phy == NFCMRVL_PHY_SPI) { |
| 137 | headroom = NCI_SPI_HDR_LEN; |
| 138 | tailroom = 1; |
| 139 | } else |
| 140 | headroom = tailroom = 0; |
Vincent Cuissard | 58d34aa | 2015-10-26 10:27:40 +0100 | [diff] [blame] | 141 | |
Vincent Cuissard | dc14bdef | 2015-06-11 14:00:19 +0200 | [diff] [blame] | 142 | if (priv->config.hci_muxed) |
Vincent Cuissard | 58d34aa | 2015-10-26 10:27:40 +0100 | [diff] [blame] | 143 | headroom += NFCMRVL_HCI_EVENT_HEADER_SIZE; |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 144 | |
| 145 | protocols = NFC_PROTO_JEWEL_MASK |
Vincent Cuissard | dc14bdef | 2015-06-11 14:00:19 +0200 | [diff] [blame] | 146 | | NFC_PROTO_MIFARE_MASK |
| 147 | | NFC_PROTO_FELICA_MASK |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 148 | | NFC_PROTO_ISO14443_MASK |
| 149 | | NFC_PROTO_ISO14443_B_MASK |
Vincent Cuissard | 83d5672 | 2015-06-12 15:35:52 +0200 | [diff] [blame] | 150 | | NFC_PROTO_ISO15693_MASK |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 151 | | NFC_PROTO_NFC_DEP_MASK; |
| 152 | |
Vincent Cuissard | f1f1a7d | 2015-06-11 11:25:43 +0200 | [diff] [blame] | 153 | priv->ndev = nci_allocate_device(&nfcmrvl_nci_ops, protocols, |
Vincent Cuissard | 58d34aa | 2015-10-26 10:27:40 +0100 | [diff] [blame] | 154 | headroom, tailroom); |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 155 | if (!priv->ndev) { |
Joe Perches | 3590ebc | 2015-04-07 00:17:00 -0700 | [diff] [blame] | 156 | nfc_err(dev, "nci_allocate_device failed\n"); |
Amitkumar Karwar | bb55dc2 | 2014-01-08 10:52:27 -0800 | [diff] [blame] | 157 | rc = -ENOMEM; |
Johan Hovold | 0cbe401 | 2017-03-30 12:15:37 +0200 | [diff] [blame] | 158 | goto error_free_gpio; |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Vincent Cuissard | 3194c68 | 2015-10-26 10:27:39 +0100 | [diff] [blame] | 161 | rc = nfcmrvl_fw_dnld_init(priv); |
| 162 | if (rc) { |
| 163 | nfc_err(dev, "failed to initialize FW download %d\n", rc); |
| 164 | goto error_free_dev; |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Johan Hovold | 45dd39b | 2017-03-30 12:15:39 +0200 | [diff] [blame] | 167 | nci_set_drvdata(priv->ndev, priv); |
| 168 | |
| 169 | rc = nci_register_device(priv->ndev); |
| 170 | if (rc) { |
| 171 | nfc_err(dev, "nci_register_device failed %d\n", rc); |
| 172 | goto error_fw_dnld_deinit; |
| 173 | } |
| 174 | |
| 175 | /* Ensure that controller is powered off */ |
| 176 | nfcmrvl_chip_halt(priv); |
| 177 | |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 178 | nfc_info(dev, "registered with nci successfully\n"); |
| 179 | return priv; |
Amitkumar Karwar | bb55dc2 | 2014-01-08 10:52:27 -0800 | [diff] [blame] | 180 | |
Johan Hovold | 45dd39b | 2017-03-30 12:15:39 +0200 | [diff] [blame] | 181 | error_fw_dnld_deinit: |
| 182 | nfcmrvl_fw_dnld_deinit(priv); |
Vincent Cuissard | 3194c68 | 2015-10-26 10:27:39 +0100 | [diff] [blame] | 183 | error_free_dev: |
| 184 | nci_free_device(priv->ndev); |
Johan Hovold | 0cbe401 | 2017-03-30 12:15:37 +0200 | [diff] [blame] | 185 | error_free_gpio: |
Johan Hovold | e33a3f8 | 2017-03-30 12:15:42 +0200 | [diff] [blame] | 186 | if (gpio_is_valid(priv->config.reset_n_io)) |
Johan Hovold | 0cbe401 | 2017-03-30 12:15:37 +0200 | [diff] [blame] | 187 | gpio_free(priv->config.reset_n_io); |
Amitkumar Karwar | bb55dc2 | 2014-01-08 10:52:27 -0800 | [diff] [blame] | 188 | kfree(priv); |
| 189 | return ERR_PTR(rc); |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 190 | } |
| 191 | EXPORT_SYMBOL_GPL(nfcmrvl_nci_register_dev); |
| 192 | |
| 193 | void nfcmrvl_nci_unregister_dev(struct nfcmrvl_private *priv) |
| 194 | { |
| 195 | struct nci_dev *ndev = priv->ndev; |
| 196 | |
Vincent Cuissard | 3194c68 | 2015-10-26 10:27:39 +0100 | [diff] [blame] | 197 | if (priv->ndev->nfc_dev->fw_download_in_progress) |
| 198 | nfcmrvl_fw_dnld_abort(priv); |
| 199 | |
| 200 | nfcmrvl_fw_dnld_deinit(priv); |
| 201 | |
Johan Hovold | e33a3f8 | 2017-03-30 12:15:42 +0200 | [diff] [blame] | 202 | if (gpio_is_valid(priv->config.reset_n_io)) |
Johan Hovold | 0cbe401 | 2017-03-30 12:15:37 +0200 | [diff] [blame] | 203 | gpio_free(priv->config.reset_n_io); |
Vincent Cuissard | b2fe288 | 2015-11-03 19:19:34 +0100 | [diff] [blame] | 204 | |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 205 | nci_unregister_device(ndev); |
| 206 | nci_free_device(ndev); |
| 207 | kfree(priv); |
| 208 | } |
| 209 | EXPORT_SYMBOL_GPL(nfcmrvl_nci_unregister_dev); |
| 210 | |
Vincent Cuissard | e1bf80c | 2015-06-11 11:25:44 +0200 | [diff] [blame] | 211 | int nfcmrvl_nci_recv_frame(struct nfcmrvl_private *priv, struct sk_buff *skb) |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 212 | { |
Vincent Cuissard | dc14bdef | 2015-06-11 14:00:19 +0200 | [diff] [blame] | 213 | if (priv->config.hci_muxed) { |
Vincent Cuissard | f1f1a7d | 2015-06-11 11:25:43 +0200 | [diff] [blame] | 214 | if (skb->data[0] == NFCMRVL_HCI_EVENT_CODE && |
| 215 | skb->data[1] == NFCMRVL_HCI_NFC_EVENT_CODE) { |
| 216 | /* Data packet, let's extract NCI payload */ |
| 217 | skb_pull(skb, NFCMRVL_HCI_EVENT_HEADER_SIZE); |
| 218 | } else { |
| 219 | /* Skip this packet */ |
| 220 | kfree_skb(skb); |
| 221 | return 0; |
| 222 | } |
| 223 | } |
| 224 | |
Vincent Cuissard | 3194c68 | 2015-10-26 10:27:39 +0100 | [diff] [blame] | 225 | if (priv->ndev->nfc_dev->fw_download_in_progress) { |
| 226 | nfcmrvl_fw_dnld_recv_frame(priv, skb); |
| 227 | return 0; |
| 228 | } |
| 229 | |
Vincent Cuissard | e1bf80c | 2015-06-11 11:25:44 +0200 | [diff] [blame] | 230 | if (test_bit(NFCMRVL_NCI_RUNNING, &priv->flags)) |
| 231 | nci_recv_frame(priv->ndev, skb); |
| 232 | else { |
| 233 | /* Drop this packet since nobody wants it */ |
| 234 | kfree_skb(skb); |
| 235 | return 0; |
| 236 | } |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 237 | |
Vincent Cuissard | e1bf80c | 2015-06-11 11:25:44 +0200 | [diff] [blame] | 238 | return 0; |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 239 | } |
| 240 | EXPORT_SYMBOL_GPL(nfcmrvl_nci_recv_frame); |
| 241 | |
Vincent Cuissard | 4a2b947f | 2015-06-11 11:25:46 +0200 | [diff] [blame] | 242 | void nfcmrvl_chip_reset(struct nfcmrvl_private *priv) |
| 243 | { |
Vincent Cuissard | b5b3e23 | 2015-10-26 10:27:41 +0100 | [diff] [blame] | 244 | /* Reset possible fault of previous session */ |
| 245 | clear_bit(NFCMRVL_PHY_ERROR, &priv->flags); |
Vincent Cuissard | 4a2b947f | 2015-06-11 11:25:46 +0200 | [diff] [blame] | 246 | |
Vincent Cuissard | dc14bdef | 2015-06-11 14:00:19 +0200 | [diff] [blame] | 247 | if (priv->config.reset_n_io) { |
Vincent Cuissard | 4a2b947f | 2015-06-11 11:25:46 +0200 | [diff] [blame] | 248 | nfc_info(priv->dev, "reset the chip\n"); |
Vincent Cuissard | dc14bdef | 2015-06-11 14:00:19 +0200 | [diff] [blame] | 249 | gpio_set_value(priv->config.reset_n_io, 0); |
Vincent Cuissard | 4a2b947f | 2015-06-11 11:25:46 +0200 | [diff] [blame] | 250 | usleep_range(5000, 10000); |
Vincent Cuissard | dc14bdef | 2015-06-11 14:00:19 +0200 | [diff] [blame] | 251 | gpio_set_value(priv->config.reset_n_io, 1); |
Vincent Cuissard | 4a2b947f | 2015-06-11 11:25:46 +0200 | [diff] [blame] | 252 | } else |
| 253 | nfc_info(priv->dev, "no reset available on this interface\n"); |
| 254 | } |
| 255 | |
Vincent Cuissard | 3194c68 | 2015-10-26 10:27:39 +0100 | [diff] [blame] | 256 | void nfcmrvl_chip_halt(struct nfcmrvl_private *priv) |
| 257 | { |
| 258 | if (priv->config.reset_n_io) |
| 259 | gpio_set_value(priv->config.reset_n_io, 0); |
| 260 | } |
| 261 | |
Vincent Cuissard | dc14bdef | 2015-06-11 14:00:19 +0200 | [diff] [blame] | 262 | int nfcmrvl_parse_dt(struct device_node *node, |
| 263 | struct nfcmrvl_platform_data *pdata) |
| 264 | { |
| 265 | int reset_n_io; |
| 266 | |
| 267 | reset_n_io = of_get_named_gpio(node, "reset-n-io", 0); |
| 268 | if (reset_n_io < 0) { |
| 269 | pr_info("no reset-n-io config\n"); |
Vincent Cuissard | dc14bdef | 2015-06-11 14:00:19 +0200 | [diff] [blame] | 270 | } else if (!gpio_is_valid(reset_n_io)) { |
| 271 | pr_err("invalid reset-n-io GPIO\n"); |
| 272 | return reset_n_io; |
| 273 | } |
| 274 | pdata->reset_n_io = reset_n_io; |
| 275 | |
| 276 | if (of_find_property(node, "hci-muxed", NULL)) |
| 277 | pdata->hci_muxed = 1; |
| 278 | else |
| 279 | pdata->hci_muxed = 0; |
| 280 | |
| 281 | return 0; |
| 282 | } |
Vincent Cuissard | dc14bdef | 2015-06-11 14:00:19 +0200 | [diff] [blame] | 283 | EXPORT_SYMBOL_GPL(nfcmrvl_parse_dt); |
| 284 | |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 285 | MODULE_AUTHOR("Marvell International Ltd."); |
Vincent Cuissard | fb101c0 | 2015-10-26 10:27:37 +0100 | [diff] [blame] | 286 | MODULE_DESCRIPTION("Marvell NFC driver"); |
Amitkumar Karwar | f26e30c | 2014-01-06 12:58:19 -0800 | [diff] [blame] | 287 | MODULE_LICENSE("GPL v2"); |