| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * I2C link layer for the NXP NCI driver |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 9 | 10 | * |
|---|
| 10 | 11 | * Derived from PN544 device driver: |
|---|
| 11 | 12 | * Copyright (C) 2012 Intel Corporation. All rights reserved. |
|---|
| 12 | | - * |
|---|
| 13 | | - * This program is free software; you can redistribute it and/or modify it |
|---|
| 14 | | - * under the terms and conditions of the GNU General Public License, |
|---|
| 15 | | - * version 2, as published by the Free Software Foundation. |
|---|
| 16 | | - * |
|---|
| 17 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 18 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 19 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 20 | | - * GNU General Public License for more details. |
|---|
| 21 | | - * |
|---|
| 22 | | - * You should have received a copy of the GNU General Public License |
|---|
| 23 | | - * along with this program; if not, see <http://www.gnu.org/licenses/>. |
|---|
| 24 | 13 | */ |
|---|
| 25 | | - |
|---|
| 26 | | -#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
|---|
| 27 | 14 | |
|---|
| 28 | 15 | #include <linux/acpi.h> |
|---|
| 29 | 16 | #include <linux/delay.h> |
|---|
| .. | .. |
|---|
| 32 | 19 | #include <linux/module.h> |
|---|
| 33 | 20 | #include <linux/nfc.h> |
|---|
| 34 | 21 | #include <linux/gpio/consumer.h> |
|---|
| 35 | | -#include <linux/of_gpio.h> |
|---|
| 36 | | -#include <linux/of_irq.h> |
|---|
| 37 | | -#include <linux/platform_data/nxp-nci.h> |
|---|
| 38 | 22 | #include <asm/unaligned.h> |
|---|
| 39 | 23 | |
|---|
| 40 | 24 | #include <net/nfc/nfc.h> |
|---|
| .. | .. |
|---|
| 49 | 33 | struct i2c_client *i2c_dev; |
|---|
| 50 | 34 | struct nci_dev *ndev; |
|---|
| 51 | 35 | |
|---|
| 52 | | - unsigned int gpio_en; |
|---|
| 53 | | - unsigned int gpio_fw; |
|---|
| 36 | + struct gpio_desc *gpiod_en; |
|---|
| 37 | + struct gpio_desc *gpiod_fw; |
|---|
| 54 | 38 | |
|---|
| 55 | 39 | int hard_fault; /* |
|---|
| 56 | 40 | * < 0 if hardware error occurred (e.g. i2c err) |
|---|
| .. | .. |
|---|
| 63 | 47 | { |
|---|
| 64 | 48 | struct nxp_nci_i2c_phy *phy = (struct nxp_nci_i2c_phy *) phy_id; |
|---|
| 65 | 49 | |
|---|
| 66 | | - gpio_set_value(phy->gpio_fw, (mode == NXP_NCI_MODE_FW) ? 1 : 0); |
|---|
| 67 | | - gpio_set_value(phy->gpio_en, (mode != NXP_NCI_MODE_COLD) ? 1 : 0); |
|---|
| 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); |
|---|
| 68 | 52 | usleep_range(10000, 15000); |
|---|
| 69 | 53 | |
|---|
| 70 | 54 | if (mode == NXP_NCI_MODE_COLD) |
|---|
| .. | .. |
|---|
| 138 | 122 | skb_put_data(*skb, &header, NXP_NCI_FW_HDR_LEN); |
|---|
| 139 | 123 | |
|---|
| 140 | 124 | r = i2c_master_recv(client, skb_put(*skb, frame_len), frame_len); |
|---|
| 141 | | - if (r != frame_len) { |
|---|
| 125 | + if (r < 0) { |
|---|
| 126 | + goto fw_read_exit_free_skb; |
|---|
| 127 | + } else if (r != frame_len) { |
|---|
| 142 | 128 | nfc_err(&client->dev, |
|---|
| 143 | 129 | "Invalid frame length: %u (expected %zu)\n", |
|---|
| 144 | 130 | r, frame_len); |
|---|
| .. | .. |
|---|
| 178 | 164 | |
|---|
| 179 | 165 | skb_put_data(*skb, (void *)&header, NCI_CTRL_HDR_SIZE); |
|---|
| 180 | 166 | |
|---|
| 167 | + if (!header.plen) |
|---|
| 168 | + return 0; |
|---|
| 169 | + |
|---|
| 181 | 170 | r = i2c_master_recv(client, skb_put(*skb, header.plen), header.plen); |
|---|
| 182 | | - if (r != header.plen) { |
|---|
| 171 | + if (r < 0) { |
|---|
| 172 | + goto nci_read_exit_free_skb; |
|---|
| 173 | + } else if (r != header.plen) { |
|---|
| 183 | 174 | nfc_err(&client->dev, |
|---|
| 184 | 175 | "Invalid frame payload length: %u (expected %u)\n", |
|---|
| 185 | 176 | r, header.plen); |
|---|
| .. | .. |
|---|
| 263 | 254 | return IRQ_NONE; |
|---|
| 264 | 255 | } |
|---|
| 265 | 256 | |
|---|
| 266 | | -static int nxp_nci_i2c_parse_devtree(struct i2c_client *client) |
|---|
| 267 | | -{ |
|---|
| 268 | | - struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client); |
|---|
| 269 | | - struct device_node *pp; |
|---|
| 270 | | - int r; |
|---|
| 257 | +static const struct acpi_gpio_params firmware_gpios = { 1, 0, false }; |
|---|
| 258 | +static const struct acpi_gpio_params enable_gpios = { 2, 0, false }; |
|---|
| 271 | 259 | |
|---|
| 272 | | - pp = client->dev.of_node; |
|---|
| 273 | | - if (!pp) |
|---|
| 274 | | - return -ENODEV; |
|---|
| 275 | | - |
|---|
| 276 | | - r = of_get_named_gpio(pp, "enable-gpios", 0); |
|---|
| 277 | | - if (r == -EPROBE_DEFER) |
|---|
| 278 | | - r = of_get_named_gpio(pp, "enable-gpios", 0); |
|---|
| 279 | | - if (r < 0) { |
|---|
| 280 | | - nfc_err(&client->dev, "Failed to get EN gpio, error: %d\n", r); |
|---|
| 281 | | - return r; |
|---|
| 282 | | - } |
|---|
| 283 | | - phy->gpio_en = r; |
|---|
| 284 | | - |
|---|
| 285 | | - r = of_get_named_gpio(pp, "firmware-gpios", 0); |
|---|
| 286 | | - if (r == -EPROBE_DEFER) |
|---|
| 287 | | - r = of_get_named_gpio(pp, "firmware-gpios", 0); |
|---|
| 288 | | - if (r < 0) { |
|---|
| 289 | | - nfc_err(&client->dev, "Failed to get FW gpio, error: %d\n", r); |
|---|
| 290 | | - return r; |
|---|
| 291 | | - } |
|---|
| 292 | | - phy->gpio_fw = r; |
|---|
| 293 | | - |
|---|
| 294 | | - return 0; |
|---|
| 295 | | -} |
|---|
| 296 | | - |
|---|
| 297 | | -static int nxp_nci_i2c_acpi_config(struct nxp_nci_i2c_phy *phy) |
|---|
| 298 | | -{ |
|---|
| 299 | | - struct i2c_client *client = phy->i2c_dev; |
|---|
| 300 | | - struct gpio_desc *gpiod_en, *gpiod_fw; |
|---|
| 301 | | - |
|---|
| 302 | | - gpiod_en = devm_gpiod_get_index(&client->dev, NULL, 2, GPIOD_OUT_LOW); |
|---|
| 303 | | - gpiod_fw = devm_gpiod_get_index(&client->dev, NULL, 1, GPIOD_OUT_LOW); |
|---|
| 304 | | - |
|---|
| 305 | | - if (IS_ERR(gpiod_en) || IS_ERR(gpiod_fw)) { |
|---|
| 306 | | - nfc_err(&client->dev, "No GPIOs\n"); |
|---|
| 307 | | - return -EINVAL; |
|---|
| 308 | | - } |
|---|
| 309 | | - |
|---|
| 310 | | - phy->gpio_en = desc_to_gpio(gpiod_en); |
|---|
| 311 | | - phy->gpio_fw = desc_to_gpio(gpiod_fw); |
|---|
| 312 | | - |
|---|
| 313 | | - return 0; |
|---|
| 314 | | -} |
|---|
| 260 | +static const struct acpi_gpio_mapping acpi_nxp_nci_gpios[] = { |
|---|
| 261 | + { "enable-gpios", &enable_gpios, 1 }, |
|---|
| 262 | + { "firmware-gpios", &firmware_gpios, 1 }, |
|---|
| 263 | + { } |
|---|
| 264 | +}; |
|---|
| 315 | 265 | |
|---|
| 316 | 266 | static int nxp_nci_i2c_probe(struct i2c_client *client, |
|---|
| 317 | 267 | const struct i2c_device_id *id) |
|---|
| 318 | 268 | { |
|---|
| 269 | + struct device *dev = &client->dev; |
|---|
| 319 | 270 | struct nxp_nci_i2c_phy *phy; |
|---|
| 320 | | - struct nxp_nci_nfc_platform_data *pdata; |
|---|
| 321 | 271 | int r; |
|---|
| 322 | 272 | |
|---|
| 323 | 273 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
|---|
| 324 | 274 | nfc_err(&client->dev, "Need I2C_FUNC_I2C\n"); |
|---|
| 325 | | - r = -ENODEV; |
|---|
| 326 | | - goto probe_exit; |
|---|
| 275 | + return -ENODEV; |
|---|
| 327 | 276 | } |
|---|
| 328 | 277 | |
|---|
| 329 | 278 | phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy), |
|---|
| 330 | 279 | GFP_KERNEL); |
|---|
| 331 | | - if (!phy) { |
|---|
| 332 | | - r = -ENOMEM; |
|---|
| 333 | | - goto probe_exit; |
|---|
| 334 | | - } |
|---|
| 280 | + if (!phy) |
|---|
| 281 | + return -ENOMEM; |
|---|
| 335 | 282 | |
|---|
| 336 | 283 | phy->i2c_dev = client; |
|---|
| 337 | 284 | i2c_set_clientdata(client, phy); |
|---|
| 338 | 285 | |
|---|
| 339 | | - pdata = client->dev.platform_data; |
|---|
| 286 | + r = devm_acpi_dev_add_driver_gpios(dev, acpi_nxp_nci_gpios); |
|---|
| 287 | + if (r) |
|---|
| 288 | + dev_dbg(dev, "Unable to add GPIO mapping table\n"); |
|---|
| 340 | 289 | |
|---|
| 341 | | - if (!pdata && client->dev.of_node) { |
|---|
| 342 | | - r = nxp_nci_i2c_parse_devtree(client); |
|---|
| 343 | | - if (r < 0) { |
|---|
| 344 | | - nfc_err(&client->dev, "Failed to get DT data\n"); |
|---|
| 345 | | - goto probe_exit; |
|---|
| 346 | | - } |
|---|
| 347 | | - } else if (pdata) { |
|---|
| 348 | | - phy->gpio_en = pdata->gpio_en; |
|---|
| 349 | | - phy->gpio_fw = pdata->gpio_fw; |
|---|
| 350 | | - } else if (ACPI_HANDLE(&client->dev)) { |
|---|
| 351 | | - r = nxp_nci_i2c_acpi_config(phy); |
|---|
| 352 | | - if (r < 0) |
|---|
| 353 | | - goto probe_exit; |
|---|
| 354 | | - goto nci_probe; |
|---|
| 355 | | - } else { |
|---|
| 356 | | - nfc_err(&client->dev, "No platform data\n"); |
|---|
| 357 | | - r = -EINVAL; |
|---|
| 358 | | - goto probe_exit; |
|---|
| 290 | + phy->gpiod_en = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW); |
|---|
| 291 | + if (IS_ERR(phy->gpiod_en)) { |
|---|
| 292 | + nfc_err(dev, "Failed to get EN gpio\n"); |
|---|
| 293 | + return PTR_ERR(phy->gpiod_en); |
|---|
| 359 | 294 | } |
|---|
| 360 | 295 | |
|---|
| 361 | | - r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_en, |
|---|
| 362 | | - GPIOF_OUT_INIT_LOW, "nxp_nci_en"); |
|---|
| 363 | | - if (r < 0) |
|---|
| 364 | | - goto probe_exit; |
|---|
| 296 | + phy->gpiod_fw = devm_gpiod_get(dev, "firmware", GPIOD_OUT_LOW); |
|---|
| 297 | + if (IS_ERR(phy->gpiod_fw)) { |
|---|
| 298 | + nfc_err(dev, "Failed to get FW gpio\n"); |
|---|
| 299 | + return PTR_ERR(phy->gpiod_fw); |
|---|
| 300 | + } |
|---|
| 365 | 301 | |
|---|
| 366 | | - r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_fw, |
|---|
| 367 | | - GPIOF_OUT_INIT_LOW, "nxp_nci_fw"); |
|---|
| 368 | | - if (r < 0) |
|---|
| 369 | | - goto probe_exit; |
|---|
| 370 | | - |
|---|
| 371 | | -nci_probe: |
|---|
| 372 | 302 | r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops, |
|---|
| 373 | 303 | NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev); |
|---|
| 374 | 304 | if (r < 0) |
|---|
| 375 | | - goto probe_exit; |
|---|
| 305 | + return r; |
|---|
| 376 | 306 | |
|---|
| 377 | 307 | r = request_threaded_irq(client->irq, NULL, |
|---|
| 378 | 308 | nxp_nci_i2c_irq_thread_fn, |
|---|
| .. | .. |
|---|
| 381 | 311 | if (r < 0) |
|---|
| 382 | 312 | nfc_err(&client->dev, "Unable to register IRQ handler\n"); |
|---|
| 383 | 313 | |
|---|
| 384 | | -probe_exit: |
|---|
| 385 | 314 | return r; |
|---|
| 386 | 315 | } |
|---|
| 387 | 316 | |
|---|
| .. | .. |
|---|
| 403 | 332 | |
|---|
| 404 | 333 | static const struct of_device_id of_nxp_nci_i2c_match[] = { |
|---|
| 405 | 334 | { .compatible = "nxp,nxp-nci-i2c", }, |
|---|
| 406 | | - {}, |
|---|
| 335 | + {} |
|---|
| 407 | 336 | }; |
|---|
| 408 | 337 | MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match); |
|---|
| 409 | 338 | |
|---|
| 410 | 339 | #ifdef CONFIG_ACPI |
|---|
| 411 | | -static struct acpi_device_id acpi_id[] = { |
|---|
| 340 | +static const struct acpi_device_id acpi_id[] = { |
|---|
| 341 | + { "NXP1001" }, |
|---|
| 412 | 342 | { "NXP7471" }, |
|---|
| 413 | | - { }, |
|---|
| 343 | + { } |
|---|
| 414 | 344 | }; |
|---|
| 415 | 345 | MODULE_DEVICE_TABLE(acpi, acpi_id); |
|---|
| 416 | 346 | #endif |
|---|
| .. | .. |
|---|
| 419 | 349 | .driver = { |
|---|
| 420 | 350 | .name = NXP_NCI_I2C_DRIVER_NAME, |
|---|
| 421 | 351 | .acpi_match_table = ACPI_PTR(acpi_id), |
|---|
| 422 | | - .of_match_table = of_match_ptr(of_nxp_nci_i2c_match), |
|---|
| 352 | + .of_match_table = of_nxp_nci_i2c_match, |
|---|
| 423 | 353 | }, |
|---|
| 424 | 354 | .probe = nxp_nci_i2c_probe, |
|---|
| 425 | 355 | .id_table = nxp_nci_i2c_id_table, |
|---|