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