1 // SPDX-License-Identifier: GPL-2.0-only
3 * I2C link layer for the NXP NCI driver
5 * Copyright (C) 2014 NXP Semiconductors All rights reserved.
6 * Copyright (C) 2012-2015 Intel Corporation. All rights reserved.
11 * Derived from PN544 device driver:
12 * Copyright (C) 2012 Intel Corporation. All rights reserved.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/acpi.h>
18 #include <linux/delay.h>
19 #include <linux/i2c.h>
20 #include <linux/interrupt.h>
21 #include <linux/module.h>
22 #include <linux/nfc.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/of_gpio.h>
25 #include <linux/of_irq.h>
26 #include <linux/platform_data/nxp-nci.h>
27 #include <asm/unaligned.h>
29 #include <net/nfc/nfc.h>
33 #define NXP_NCI_I2C_DRIVER_NAME "nxp-nci_i2c"
35 #define NXP_NCI_I2C_MAX_PAYLOAD 32
37 struct nxp_nci_i2c_phy {
38 struct i2c_client *i2c_dev;
45 * < 0 if hardware error occurred (e.g. i2c err)
46 * and prevents normal operation.
50 static int nxp_nci_i2c_set_mode(void *phy_id,
51 enum nxp_nci_mode mode)
53 struct nxp_nci_i2c_phy *phy = (struct nxp_nci_i2c_phy *) phy_id;
55 gpio_set_value(phy->gpio_fw, (mode == NXP_NCI_MODE_FW) ? 1 : 0);
56 gpio_set_value(phy->gpio_en, (mode != NXP_NCI_MODE_COLD) ? 1 : 0);
57 usleep_range(10000, 15000);
59 if (mode == NXP_NCI_MODE_COLD)
65 static int nxp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
68 struct nxp_nci_i2c_phy *phy = phy_id;
69 struct i2c_client *client = phy->i2c_dev;
71 if (phy->hard_fault != 0)
72 return phy->hard_fault;
74 r = i2c_master_send(client, skb->data, skb->len);
76 /* Retry, chip was in standby */
78 r = i2c_master_send(client, skb->data, skb->len);
82 nfc_err(&client->dev, "Error %d on I2C send\n", r);
83 } else if (r != skb->len) {
85 "Invalid length sent: %u (expected %u)\n",
89 /* Success but return 0 and not number of bytes */
96 static const struct nxp_nci_phy_ops i2c_phy_ops = {
97 .set_mode = nxp_nci_i2c_set_mode,
98 .write = nxp_nci_i2c_write,
101 static int nxp_nci_i2c_fw_read(struct nxp_nci_i2c_phy *phy,
102 struct sk_buff **skb)
104 struct i2c_client *client = phy->i2c_dev;
109 r = i2c_master_recv(client, (u8 *) &header, NXP_NCI_FW_HDR_LEN);
112 } else if (r != NXP_NCI_FW_HDR_LEN) {
113 nfc_err(&client->dev, "Incorrect header length: %u\n", r);
118 frame_len = (be16_to_cpu(header) & NXP_NCI_FW_FRAME_LEN_MASK) +
121 *skb = alloc_skb(NXP_NCI_FW_HDR_LEN + frame_len, GFP_KERNEL);
127 skb_put_data(*skb, &header, NXP_NCI_FW_HDR_LEN);
129 r = i2c_master_recv(client, skb_put(*skb, frame_len), frame_len);
130 if (r != frame_len) {
131 nfc_err(&client->dev,
132 "Invalid frame length: %u (expected %zu)\n",
135 goto fw_read_exit_free_skb;
140 fw_read_exit_free_skb:
146 static int nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy *phy,
147 struct sk_buff **skb)
149 struct nci_ctrl_hdr header; /* May actually be a data header */
150 struct i2c_client *client = phy->i2c_dev;
153 r = i2c_master_recv(client, (u8 *) &header, NCI_CTRL_HDR_SIZE);
156 } else if (r != NCI_CTRL_HDR_SIZE) {
157 nfc_err(&client->dev, "Incorrect header length: %u\n", r);
162 *skb = alloc_skb(NCI_CTRL_HDR_SIZE + header.plen, GFP_KERNEL);
168 skb_put_data(*skb, (void *)&header, NCI_CTRL_HDR_SIZE);
170 r = i2c_master_recv(client, skb_put(*skb, header.plen), header.plen);
171 if (r != header.plen) {
172 nfc_err(&client->dev,
173 "Invalid frame payload length: %u (expected %u)\n",
176 goto nci_read_exit_free_skb;
181 nci_read_exit_free_skb:
187 static irqreturn_t nxp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
189 struct nxp_nci_i2c_phy *phy = phy_id;
190 struct i2c_client *client;
191 struct nxp_nci_info *info;
193 struct sk_buff *skb = NULL;
196 if (!phy || !phy->ndev)
199 client = phy->i2c_dev;
201 if (!client || irq != client->irq)
204 info = nci_get_drvdata(phy->ndev);
209 mutex_lock(&info->info_lock);
211 if (phy->hard_fault != 0)
212 goto exit_irq_handled;
214 switch (info->mode) {
215 case NXP_NCI_MODE_NCI:
216 r = nxp_nci_i2c_nci_read(phy, &skb);
218 case NXP_NCI_MODE_FW:
219 r = nxp_nci_i2c_fw_read(phy, &skb);
221 case NXP_NCI_MODE_COLD:
226 if (r == -EREMOTEIO) {
230 nfc_err(&client->dev, "Read failed with error %d\n", r);
231 goto exit_irq_handled;
234 switch (info->mode) {
235 case NXP_NCI_MODE_NCI:
236 nci_recv_frame(phy->ndev, skb);
238 case NXP_NCI_MODE_FW:
239 nxp_nci_fw_recv_frame(phy->ndev, skb);
241 case NXP_NCI_MODE_COLD:
246 mutex_unlock(&info->info_lock);
253 static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
255 struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
256 struct device_node *pp;
259 pp = client->dev.of_node;
263 r = of_get_named_gpio(pp, "enable-gpios", 0);
264 if (r == -EPROBE_DEFER)
265 r = of_get_named_gpio(pp, "enable-gpios", 0);
267 nfc_err(&client->dev, "Failed to get EN gpio, error: %d\n", r);
272 r = of_get_named_gpio(pp, "firmware-gpios", 0);
273 if (r == -EPROBE_DEFER)
274 r = of_get_named_gpio(pp, "firmware-gpios", 0);
276 nfc_err(&client->dev, "Failed to get FW gpio, error: %d\n", r);
284 static int nxp_nci_i2c_acpi_config(struct nxp_nci_i2c_phy *phy)
286 struct i2c_client *client = phy->i2c_dev;
287 struct gpio_desc *gpiod_en, *gpiod_fw;
289 gpiod_en = devm_gpiod_get_index(&client->dev, NULL, 2, GPIOD_OUT_LOW);
290 gpiod_fw = devm_gpiod_get_index(&client->dev, NULL, 1, GPIOD_OUT_LOW);
292 if (IS_ERR(gpiod_en) || IS_ERR(gpiod_fw)) {
293 nfc_err(&client->dev, "No GPIOs\n");
297 phy->gpio_en = desc_to_gpio(gpiod_en);
298 phy->gpio_fw = desc_to_gpio(gpiod_fw);
303 static int nxp_nci_i2c_probe(struct i2c_client *client,
304 const struct i2c_device_id *id)
306 struct nxp_nci_i2c_phy *phy;
307 struct nxp_nci_nfc_platform_data *pdata;
310 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
311 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
316 phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy),
323 phy->i2c_dev = client;
324 i2c_set_clientdata(client, phy);
326 pdata = client->dev.platform_data;
328 if (!pdata && client->dev.of_node) {
329 r = nxp_nci_i2c_parse_devtree(client);
331 nfc_err(&client->dev, "Failed to get DT data\n");
335 phy->gpio_en = pdata->gpio_en;
336 phy->gpio_fw = pdata->gpio_fw;
337 } else if (ACPI_HANDLE(&client->dev)) {
338 r = nxp_nci_i2c_acpi_config(phy);
343 nfc_err(&client->dev, "No platform data\n");
348 r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_en,
349 GPIOF_OUT_INIT_LOW, "nxp_nci_en");
353 r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_fw,
354 GPIOF_OUT_INIT_LOW, "nxp_nci_fw");
359 r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
360 NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
364 r = request_threaded_irq(client->irq, NULL,
365 nxp_nci_i2c_irq_thread_fn,
366 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
367 NXP_NCI_I2C_DRIVER_NAME, phy);
369 nfc_err(&client->dev, "Unable to register IRQ handler\n");
375 static int nxp_nci_i2c_remove(struct i2c_client *client)
377 struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
379 nxp_nci_remove(phy->ndev);
380 free_irq(client->irq, phy);
385 static const struct i2c_device_id nxp_nci_i2c_id_table[] = {
389 MODULE_DEVICE_TABLE(i2c, nxp_nci_i2c_id_table);
391 static const struct of_device_id of_nxp_nci_i2c_match[] = {
392 { .compatible = "nxp,nxp-nci-i2c", },
395 MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match);
398 static struct acpi_device_id acpi_id[] = {
402 MODULE_DEVICE_TABLE(acpi, acpi_id);
405 static struct i2c_driver nxp_nci_i2c_driver = {
407 .name = NXP_NCI_I2C_DRIVER_NAME,
408 .acpi_match_table = ACPI_PTR(acpi_id),
409 .of_match_table = of_match_ptr(of_nxp_nci_i2c_match),
411 .probe = nxp_nci_i2c_probe,
412 .id_table = nxp_nci_i2c_id_table,
413 .remove = nxp_nci_i2c_remove,
416 module_i2c_driver(nxp_nci_i2c_driver);
418 MODULE_LICENSE("GPL");
419 MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");