Thomas Gleixner | 46fe777 | 2019-05-31 01:09:57 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 2 | /* |
| 3 | * I2C link layer for the NXP NCI driver |
| 4 | * |
| 5 | * Copyright (C) 2014 NXP Semiconductors All rights reserved. |
Oleg Zhurakivskyy | 551e306 | 2015-05-25 13:55:13 +0300 | [diff] [blame] | 6 | * Copyright (C) 2012-2015 Intel Corporation. All rights reserved. |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 7 | * |
| 8 | * Authors: Clément Perrochaud <clement.perrochaud@nxp.com> |
Oleg Zhurakivskyy | 551e306 | 2015-05-25 13:55:13 +0300 | [diff] [blame] | 9 | * Authors: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com> |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 10 | * |
| 11 | * Derived from PN544 device driver: |
| 12 | * Copyright (C) 2012 Intel Corporation. All rights reserved. |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 16 | |
Oleg Zhurakivskyy | 551e306 | 2015-05-25 13:55:13 +0300 | [diff] [blame] | 17 | #include <linux/acpi.h> |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 18 | #include <linux/delay.h> |
| 19 | #include <linux/i2c.h> |
| 20 | #include <linux/interrupt.h> |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/nfc.h> |
Samuel Ortiz | 262e719 | 2015-05-28 17:10:03 +0200 | [diff] [blame] | 23 | #include <linux/gpio/consumer.h> |
Guenter Roeck | 2eee74b | 2015-08-01 06:59:29 -0700 | [diff] [blame] | 24 | #include <asm/unaligned.h> |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 25 | |
| 26 | #include <net/nfc/nfc.h> |
| 27 | |
| 28 | #include "nxp-nci.h" |
| 29 | |
| 30 | #define NXP_NCI_I2C_DRIVER_NAME "nxp-nci_i2c" |
| 31 | |
| 32 | #define NXP_NCI_I2C_MAX_PAYLOAD 32 |
| 33 | |
| 34 | struct nxp_nci_i2c_phy { |
| 35 | struct i2c_client *i2c_dev; |
| 36 | struct nci_dev *ndev; |
| 37 | |
Andy Shevchenko | 4320176 | 2019-07-29 16:35:04 +0300 | [diff] [blame] | 38 | struct gpio_desc *gpiod_en; |
| 39 | struct gpio_desc *gpiod_fw; |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 40 | |
| 41 | int hard_fault; /* |
| 42 | * < 0 if hardware error occurred (e.g. i2c err) |
| 43 | * and prevents normal operation. |
| 44 | */ |
| 45 | }; |
| 46 | |
| 47 | static int nxp_nci_i2c_set_mode(void *phy_id, |
| 48 | enum nxp_nci_mode mode) |
| 49 | { |
| 50 | struct nxp_nci_i2c_phy *phy = (struct nxp_nci_i2c_phy *) phy_id; |
| 51 | |
Andy Shevchenko | 4320176 | 2019-07-29 16:35:04 +0300 | [diff] [blame] | 52 | gpiod_set_value(phy->gpiod_fw, (mode == NXP_NCI_MODE_FW) ? 1 : 0); |
| 53 | gpiod_set_value(phy->gpiod_en, (mode != NXP_NCI_MODE_COLD) ? 1 : 0); |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 54 | usleep_range(10000, 15000); |
| 55 | |
| 56 | if (mode == NXP_NCI_MODE_COLD) |
| 57 | phy->hard_fault = 0; |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static int nxp_nci_i2c_write(void *phy_id, struct sk_buff *skb) |
| 63 | { |
| 64 | int r; |
| 65 | struct nxp_nci_i2c_phy *phy = phy_id; |
| 66 | struct i2c_client *client = phy->i2c_dev; |
| 67 | |
| 68 | if (phy->hard_fault != 0) |
| 69 | return phy->hard_fault; |
| 70 | |
| 71 | r = i2c_master_send(client, skb->data, skb->len); |
Fabio Estevam | 59df9bb | 2015-11-04 08:13:00 -0200 | [diff] [blame] | 72 | if (r < 0) { |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 73 | /* Retry, chip was in standby */ |
Nicholas Mc Guire | 96bd0b5 | 2017-01-22 13:28:39 +0100 | [diff] [blame] | 74 | msleep(110); |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 75 | r = i2c_master_send(client, skb->data, skb->len); |
| 76 | } |
| 77 | |
| 78 | if (r < 0) { |
| 79 | nfc_err(&client->dev, "Error %d on I2C send\n", r); |
| 80 | } else if (r != skb->len) { |
| 81 | nfc_err(&client->dev, |
| 82 | "Invalid length sent: %u (expected %u)\n", |
| 83 | r, skb->len); |
| 84 | r = -EREMOTEIO; |
| 85 | } else { |
| 86 | /* Success but return 0 and not number of bytes */ |
| 87 | r = 0; |
| 88 | } |
| 89 | |
| 90 | return r; |
| 91 | } |
| 92 | |
Julia Lawall | 7cf6d08 | 2015-10-11 13:24:13 +0200 | [diff] [blame] | 93 | static const struct nxp_nci_phy_ops i2c_phy_ops = { |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 94 | .set_mode = nxp_nci_i2c_set_mode, |
| 95 | .write = nxp_nci_i2c_write, |
| 96 | }; |
| 97 | |
| 98 | static int nxp_nci_i2c_fw_read(struct nxp_nci_i2c_phy *phy, |
| 99 | struct sk_buff **skb) |
| 100 | { |
| 101 | struct i2c_client *client = phy->i2c_dev; |
| 102 | u16 header; |
| 103 | size_t frame_len; |
| 104 | int r; |
| 105 | |
| 106 | r = i2c_master_recv(client, (u8 *) &header, NXP_NCI_FW_HDR_LEN); |
| 107 | if (r < 0) { |
| 108 | goto fw_read_exit; |
| 109 | } else if (r != NXP_NCI_FW_HDR_LEN) { |
| 110 | nfc_err(&client->dev, "Incorrect header length: %u\n", r); |
| 111 | r = -EBADMSG; |
| 112 | goto fw_read_exit; |
| 113 | } |
| 114 | |
Al Viro | 4ea2063 | 2017-04-17 00:42:22 +0200 | [diff] [blame] | 115 | frame_len = (be16_to_cpu(header) & NXP_NCI_FW_FRAME_LEN_MASK) + |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 116 | NXP_NCI_FW_CRC_LEN; |
| 117 | |
| 118 | *skb = alloc_skb(NXP_NCI_FW_HDR_LEN + frame_len, GFP_KERNEL); |
| 119 | if (*skb == NULL) { |
| 120 | r = -ENOMEM; |
| 121 | goto fw_read_exit; |
| 122 | } |
| 123 | |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 124 | skb_put_data(*skb, &header, NXP_NCI_FW_HDR_LEN); |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 125 | |
| 126 | r = i2c_master_recv(client, skb_put(*skb, frame_len), frame_len); |
| 127 | if (r != frame_len) { |
| 128 | nfc_err(&client->dev, |
| 129 | "Invalid frame length: %u (expected %zu)\n", |
| 130 | r, frame_len); |
| 131 | r = -EBADMSG; |
| 132 | goto fw_read_exit_free_skb; |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | |
| 137 | fw_read_exit_free_skb: |
| 138 | kfree_skb(*skb); |
| 139 | fw_read_exit: |
| 140 | return r; |
| 141 | } |
| 142 | |
| 143 | static int nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy *phy, |
| 144 | struct sk_buff **skb) |
| 145 | { |
| 146 | struct nci_ctrl_hdr header; /* May actually be a data header */ |
| 147 | struct i2c_client *client = phy->i2c_dev; |
| 148 | int r; |
| 149 | |
| 150 | r = i2c_master_recv(client, (u8 *) &header, NCI_CTRL_HDR_SIZE); |
| 151 | if (r < 0) { |
| 152 | goto nci_read_exit; |
| 153 | } else if (r != NCI_CTRL_HDR_SIZE) { |
| 154 | nfc_err(&client->dev, "Incorrect header length: %u\n", r); |
| 155 | r = -EBADMSG; |
| 156 | goto nci_read_exit; |
| 157 | } |
| 158 | |
| 159 | *skb = alloc_skb(NCI_CTRL_HDR_SIZE + header.plen, GFP_KERNEL); |
| 160 | if (*skb == NULL) { |
| 161 | r = -ENOMEM; |
| 162 | goto nci_read_exit; |
| 163 | } |
| 164 | |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 165 | skb_put_data(*skb, (void *)&header, NCI_CTRL_HDR_SIZE); |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 166 | |
| 167 | r = i2c_master_recv(client, skb_put(*skb, header.plen), header.plen); |
| 168 | if (r != header.plen) { |
| 169 | nfc_err(&client->dev, |
| 170 | "Invalid frame payload length: %u (expected %u)\n", |
| 171 | r, header.plen); |
| 172 | r = -EBADMSG; |
| 173 | goto nci_read_exit_free_skb; |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | |
| 178 | nci_read_exit_free_skb: |
| 179 | kfree_skb(*skb); |
| 180 | nci_read_exit: |
| 181 | return r; |
| 182 | } |
| 183 | |
| 184 | static irqreturn_t nxp_nci_i2c_irq_thread_fn(int irq, void *phy_id) |
| 185 | { |
| 186 | struct nxp_nci_i2c_phy *phy = phy_id; |
| 187 | struct i2c_client *client; |
| 188 | struct nxp_nci_info *info; |
| 189 | |
| 190 | struct sk_buff *skb = NULL; |
| 191 | int r = 0; |
| 192 | |
| 193 | if (!phy || !phy->ndev) |
| 194 | goto exit_irq_none; |
| 195 | |
| 196 | client = phy->i2c_dev; |
| 197 | |
| 198 | if (!client || irq != client->irq) |
| 199 | goto exit_irq_none; |
| 200 | |
| 201 | info = nci_get_drvdata(phy->ndev); |
| 202 | |
| 203 | if (!info) |
| 204 | goto exit_irq_none; |
| 205 | |
| 206 | mutex_lock(&info->info_lock); |
| 207 | |
| 208 | if (phy->hard_fault != 0) |
| 209 | goto exit_irq_handled; |
| 210 | |
| 211 | switch (info->mode) { |
| 212 | case NXP_NCI_MODE_NCI: |
| 213 | r = nxp_nci_i2c_nci_read(phy, &skb); |
| 214 | break; |
| 215 | case NXP_NCI_MODE_FW: |
| 216 | r = nxp_nci_i2c_fw_read(phy, &skb); |
| 217 | break; |
| 218 | case NXP_NCI_MODE_COLD: |
| 219 | r = -EREMOTEIO; |
| 220 | break; |
| 221 | } |
| 222 | |
| 223 | if (r == -EREMOTEIO) { |
| 224 | phy->hard_fault = r; |
| 225 | skb = NULL; |
| 226 | } else if (r < 0) { |
| 227 | nfc_err(&client->dev, "Read failed with error %d\n", r); |
| 228 | goto exit_irq_handled; |
| 229 | } |
| 230 | |
| 231 | switch (info->mode) { |
| 232 | case NXP_NCI_MODE_NCI: |
| 233 | nci_recv_frame(phy->ndev, skb); |
| 234 | break; |
| 235 | case NXP_NCI_MODE_FW: |
| 236 | nxp_nci_fw_recv_frame(phy->ndev, skb); |
| 237 | break; |
| 238 | case NXP_NCI_MODE_COLD: |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | exit_irq_handled: |
| 243 | mutex_unlock(&info->info_lock); |
| 244 | return IRQ_HANDLED; |
| 245 | exit_irq_none: |
| 246 | WARN_ON_ONCE(1); |
| 247 | return IRQ_NONE; |
| 248 | } |
| 249 | |
Andy Shevchenko | 099d03f | 2019-07-29 16:35:05 +0300 | [diff] [blame] | 250 | static const struct acpi_gpio_params firmware_gpios = { 1, 0, false }; |
| 251 | static const struct acpi_gpio_params enable_gpios = { 2, 0, false }; |
| 252 | |
| 253 | static const struct acpi_gpio_mapping acpi_nxp_nci_gpios[] = { |
| 254 | { "enable-gpios", &enable_gpios, 1 }, |
| 255 | { "firmware-gpios", &firmware_gpios, 1 }, |
| 256 | { } |
| 257 | }; |
| 258 | |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 259 | static int nxp_nci_i2c_probe(struct i2c_client *client, |
| 260 | const struct i2c_device_id *id) |
| 261 | { |
Andy Shevchenko | ad0acfd | 2019-07-29 16:35:06 +0300 | [diff] [blame] | 262 | struct device *dev = &client->dev; |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 263 | struct nxp_nci_i2c_phy *phy; |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 264 | int r; |
| 265 | |
| 266 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 267 | nfc_err(&client->dev, "Need I2C_FUNC_I2C\n"); |
Andy Shevchenko | 4f1cbf2 | 2019-07-29 16:35:07 +0300 | [diff] [blame] | 268 | return -ENODEV; |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy), |
| 272 | GFP_KERNEL); |
Andy Shevchenko | 4f1cbf2 | 2019-07-29 16:35:07 +0300 | [diff] [blame] | 273 | if (!phy) |
| 274 | return -ENOMEM; |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 275 | |
| 276 | phy->i2c_dev = client; |
| 277 | i2c_set_clientdata(client, phy); |
| 278 | |
Andy Shevchenko | ad0acfd | 2019-07-29 16:35:06 +0300 | [diff] [blame] | 279 | r = devm_acpi_dev_add_driver_gpios(dev, acpi_nxp_nci_gpios); |
| 280 | if (r) |
| 281 | return r; |
| 282 | |
| 283 | phy->gpiod_en = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW); |
| 284 | if (IS_ERR(phy->gpiod_en)) { |
| 285 | nfc_err(dev, "Failed to get EN gpio\n"); |
| 286 | return PTR_ERR(phy->gpiod_en); |
| 287 | } |
| 288 | |
| 289 | phy->gpiod_fw = devm_gpiod_get(dev, "firmware", GPIOD_OUT_LOW); |
| 290 | if (IS_ERR(phy->gpiod_fw)) { |
| 291 | nfc_err(dev, "Failed to get FW gpio\n"); |
| 292 | return PTR_ERR(phy->gpiod_fw); |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 293 | } |
| 294 | |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 295 | r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops, |
| 296 | NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev); |
| 297 | if (r < 0) |
Andy Shevchenko | 4f1cbf2 | 2019-07-29 16:35:07 +0300 | [diff] [blame] | 298 | return r; |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 299 | |
| 300 | r = request_threaded_irq(client->irq, NULL, |
| 301 | nxp_nci_i2c_irq_thread_fn, |
| 302 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
| 303 | NXP_NCI_I2C_DRIVER_NAME, phy); |
| 304 | if (r < 0) |
| 305 | nfc_err(&client->dev, "Unable to register IRQ handler\n"); |
| 306 | |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 307 | return r; |
| 308 | } |
| 309 | |
| 310 | static int nxp_nci_i2c_remove(struct i2c_client *client) |
| 311 | { |
| 312 | struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client); |
| 313 | |
| 314 | nxp_nci_remove(phy->ndev); |
| 315 | free_irq(client->irq, phy); |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
Arvind Yadav | 01e682a | 2017-08-21 22:33:55 +0530 | [diff] [blame] | 320 | static const struct i2c_device_id nxp_nci_i2c_id_table[] = { |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 321 | {"nxp-nci_i2c", 0}, |
| 322 | {} |
| 323 | }; |
| 324 | MODULE_DEVICE_TABLE(i2c, nxp_nci_i2c_id_table); |
| 325 | |
| 326 | static const struct of_device_id of_nxp_nci_i2c_match[] = { |
| 327 | { .compatible = "nxp,nxp-nci-i2c", }, |
| 328 | {}, |
| 329 | }; |
| 330 | MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match); |
| 331 | |
Oleg Zhurakivskyy | 551e306 | 2015-05-25 13:55:13 +0300 | [diff] [blame] | 332 | #ifdef CONFIG_ACPI |
Andy Shevchenko | 52c2ea0 | 2019-07-29 16:35:08 +0300 | [diff] [blame] | 333 | static const struct acpi_device_id acpi_id[] = { |
Andy Shevchenko | 1b14a37 | 2019-07-29 16:35:02 +0300 | [diff] [blame] | 334 | { "NXP1001" }, |
Oleg Zhurakivskyy | 551e306 | 2015-05-25 13:55:13 +0300 | [diff] [blame] | 335 | { "NXP7471" }, |
| 336 | { }, |
| 337 | }; |
| 338 | MODULE_DEVICE_TABLE(acpi, acpi_id); |
| 339 | #endif |
| 340 | |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 341 | static struct i2c_driver nxp_nci_i2c_driver = { |
| 342 | .driver = { |
| 343 | .name = NXP_NCI_I2C_DRIVER_NAME, |
Oleg Zhurakivskyy | 551e306 | 2015-05-25 13:55:13 +0300 | [diff] [blame] | 344 | .acpi_match_table = ACPI_PTR(acpi_id), |
Andy Shevchenko | da05208 | 2019-07-29 16:35:09 +0300 | [diff] [blame^] | 345 | .of_match_table = of_nxp_nci_i2c_match, |
Clément Perrochaud | 6be8867 | 2015-03-09 11:12:05 +0100 | [diff] [blame] | 346 | }, |
| 347 | .probe = nxp_nci_i2c_probe, |
| 348 | .id_table = nxp_nci_i2c_id_table, |
| 349 | .remove = nxp_nci_i2c_remove, |
| 350 | }; |
| 351 | |
| 352 | module_i2c_driver(nxp_nci_i2c_driver); |
| 353 | |
| 354 | MODULE_LICENSE("GPL"); |
| 355 | MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers"); |
| 356 | MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>"); |
Oleg Zhurakivskyy | 551e306 | 2015-05-25 13:55:13 +0300 | [diff] [blame] | 357 | MODULE_AUTHOR("Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>"); |