2 * I2C link layer for the NXP NCI driver
4 * Copyright (C) 2014 NXP Semiconductors All rights reserved.
5 * Copyright (C) 2012-2015 Intel Corporation. All rights reserved.
7 * Authors: Clément Perrochaud <clement.perrochaud@nxp.com>
8 * Authors: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>
10 * Derived from PN544 device driver:
11 * Copyright (C) 2012 Intel Corporation. All rights reserved.
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.
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.
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/>.
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/acpi.h>
29 #include <linux/delay.h>
30 #include <linux/i2c.h>
31 #include <linux/interrupt.h>
32 #include <linux/miscdevice.h>
33 #include <linux/module.h>
34 #include <linux/nfc.h>
35 #include <linux/gpio/consumer.h>
36 #include <linux/of_gpio.h>
37 #include <linux/of_irq.h>
38 #include <linux/platform_data/nxp-nci.h>
39 #include <asm/unaligned.h>
41 #include <net/nfc/nfc.h>
45 #define NXP_NCI_I2C_DRIVER_NAME "nxp-nci_i2c"
47 #define NXP_NCI_I2C_MAX_PAYLOAD 32
49 struct nxp_nci_i2c_phy {
50 struct i2c_client *i2c_dev;
57 * < 0 if hardware error occurred (e.g. i2c err)
58 * and prevents normal operation.
62 static int nxp_nci_i2c_set_mode(void *phy_id,
63 enum nxp_nci_mode mode)
65 struct nxp_nci_i2c_phy *phy = (struct nxp_nci_i2c_phy *) phy_id;
67 gpio_set_value(phy->gpio_fw, (mode == NXP_NCI_MODE_FW) ? 1 : 0);
68 gpio_set_value(phy->gpio_en, (mode != NXP_NCI_MODE_COLD) ? 1 : 0);
69 usleep_range(10000, 15000);
71 if (mode == NXP_NCI_MODE_COLD)
77 static int nxp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
80 struct nxp_nci_i2c_phy *phy = phy_id;
81 struct i2c_client *client = phy->i2c_dev;
83 if (phy->hard_fault != 0)
84 return phy->hard_fault;
86 r = i2c_master_send(client, skb->data, skb->len);
88 /* Retry, chip was in standby */
89 usleep_range(110000, 120000);
90 r = i2c_master_send(client, skb->data, skb->len);
94 nfc_err(&client->dev, "Error %d on I2C send\n", r);
95 } else if (r != skb->len) {
97 "Invalid length sent: %u (expected %u)\n",
101 /* Success but return 0 and not number of bytes */
108 static const struct nxp_nci_phy_ops i2c_phy_ops = {
109 .set_mode = nxp_nci_i2c_set_mode,
110 .write = nxp_nci_i2c_write,
113 static int nxp_nci_i2c_fw_read(struct nxp_nci_i2c_phy *phy,
114 struct sk_buff **skb)
116 struct i2c_client *client = phy->i2c_dev;
121 r = i2c_master_recv(client, (u8 *) &header, NXP_NCI_FW_HDR_LEN);
124 } else if (r != NXP_NCI_FW_HDR_LEN) {
125 nfc_err(&client->dev, "Incorrect header length: %u\n", r);
130 frame_len = (get_unaligned_be16(&header) & NXP_NCI_FW_FRAME_LEN_MASK) +
133 *skb = alloc_skb(NXP_NCI_FW_HDR_LEN + frame_len, GFP_KERNEL);
139 memcpy(skb_put(*skb, NXP_NCI_FW_HDR_LEN), &header, NXP_NCI_FW_HDR_LEN);
141 r = i2c_master_recv(client, skb_put(*skb, frame_len), frame_len);
142 if (r != frame_len) {
143 nfc_err(&client->dev,
144 "Invalid frame length: %u (expected %zu)\n",
147 goto fw_read_exit_free_skb;
152 fw_read_exit_free_skb:
158 static int nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy *phy,
159 struct sk_buff **skb)
161 struct nci_ctrl_hdr header; /* May actually be a data header */
162 struct i2c_client *client = phy->i2c_dev;
165 r = i2c_master_recv(client, (u8 *) &header, NCI_CTRL_HDR_SIZE);
168 } else if (r != NCI_CTRL_HDR_SIZE) {
169 nfc_err(&client->dev, "Incorrect header length: %u\n", r);
174 *skb = alloc_skb(NCI_CTRL_HDR_SIZE + header.plen, GFP_KERNEL);
180 memcpy(skb_put(*skb, NCI_CTRL_HDR_SIZE), (void *) &header,
183 r = i2c_master_recv(client, skb_put(*skb, header.plen), header.plen);
184 if (r != header.plen) {
185 nfc_err(&client->dev,
186 "Invalid frame payload length: %u (expected %u)\n",
189 goto nci_read_exit_free_skb;
194 nci_read_exit_free_skb:
200 static irqreturn_t nxp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
202 struct nxp_nci_i2c_phy *phy = phy_id;
203 struct i2c_client *client;
204 struct nxp_nci_info *info;
206 struct sk_buff *skb = NULL;
209 if (!phy || !phy->ndev)
212 client = phy->i2c_dev;
214 if (!client || irq != client->irq)
217 info = nci_get_drvdata(phy->ndev);
222 mutex_lock(&info->info_lock);
224 if (phy->hard_fault != 0)
225 goto exit_irq_handled;
227 switch (info->mode) {
228 case NXP_NCI_MODE_NCI:
229 r = nxp_nci_i2c_nci_read(phy, &skb);
231 case NXP_NCI_MODE_FW:
232 r = nxp_nci_i2c_fw_read(phy, &skb);
234 case NXP_NCI_MODE_COLD:
239 if (r == -EREMOTEIO) {
241 if (info->mode == NXP_NCI_MODE_FW)
242 nxp_nci_fw_recv_frame(phy->ndev, NULL);
245 nfc_err(&client->dev, "Read failed with error %d\n", r);
246 goto exit_irq_handled;
249 switch (info->mode) {
250 case NXP_NCI_MODE_NCI:
251 nci_recv_frame(phy->ndev, skb);
253 case NXP_NCI_MODE_FW:
254 nxp_nci_fw_recv_frame(phy->ndev, skb);
256 case NXP_NCI_MODE_COLD:
261 mutex_unlock(&info->info_lock);
268 static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
270 struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
271 struct device_node *pp;
274 pp = client->dev.of_node;
278 r = of_get_named_gpio(pp, "enable-gpios", 0);
279 if (r == -EPROBE_DEFER)
280 r = of_get_named_gpio(pp, "enable-gpios", 0);
282 nfc_err(&client->dev, "Failed to get EN gpio, error: %d\n", r);
287 r = of_get_named_gpio(pp, "firmware-gpios", 0);
288 if (r == -EPROBE_DEFER)
289 r = of_get_named_gpio(pp, "firmware-gpios", 0);
291 nfc_err(&client->dev, "Failed to get FW gpio, error: %d\n", r);
299 static int nxp_nci_i2c_acpi_config(struct nxp_nci_i2c_phy *phy)
301 struct i2c_client *client = phy->i2c_dev;
302 struct gpio_desc *gpiod_en, *gpiod_fw;
304 gpiod_en = devm_gpiod_get_index(&client->dev, NULL, 2, GPIOD_OUT_LOW);
305 gpiod_fw = devm_gpiod_get_index(&client->dev, NULL, 1, GPIOD_OUT_LOW);
307 if (IS_ERR(gpiod_en) || IS_ERR(gpiod_fw)) {
308 nfc_err(&client->dev, "No GPIOs\n");
312 phy->gpio_en = desc_to_gpio(gpiod_en);
313 phy->gpio_fw = desc_to_gpio(gpiod_fw);
318 static int nxp_nci_i2c_probe(struct i2c_client *client,
319 const struct i2c_device_id *id)
321 struct nxp_nci_i2c_phy *phy;
322 struct nxp_nci_nfc_platform_data *pdata;
325 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
326 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
331 phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy),
338 phy->i2c_dev = client;
339 i2c_set_clientdata(client, phy);
341 pdata = client->dev.platform_data;
343 if (!pdata && client->dev.of_node) {
344 r = nxp_nci_i2c_parse_devtree(client);
346 nfc_err(&client->dev, "Failed to get DT data\n");
350 phy->gpio_en = pdata->gpio_en;
351 phy->gpio_fw = pdata->gpio_fw;
352 } else if (ACPI_HANDLE(&client->dev)) {
353 r = nxp_nci_i2c_acpi_config(phy);
358 nfc_err(&client->dev, "No platform data\n");
363 r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_en,
364 GPIOF_OUT_INIT_LOW, "nxp_nci_en");
368 r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_fw,
369 GPIOF_OUT_INIT_LOW, "nxp_nci_fw");
374 r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
375 NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
379 r = request_threaded_irq(client->irq, NULL,
380 nxp_nci_i2c_irq_thread_fn,
381 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
382 NXP_NCI_I2C_DRIVER_NAME, phy);
384 nfc_err(&client->dev, "Unable to register IRQ handler\n");
390 static int nxp_nci_i2c_remove(struct i2c_client *client)
392 struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
394 nxp_nci_remove(phy->ndev);
395 free_irq(client->irq, phy);
400 static struct i2c_device_id nxp_nci_i2c_id_table[] = {
404 MODULE_DEVICE_TABLE(i2c, nxp_nci_i2c_id_table);
406 static const struct of_device_id of_nxp_nci_i2c_match[] = {
407 { .compatible = "nxp,nxp-nci-i2c", },
410 MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match);
413 static struct acpi_device_id acpi_id[] = {
417 MODULE_DEVICE_TABLE(acpi, acpi_id);
420 static struct i2c_driver nxp_nci_i2c_driver = {
422 .name = NXP_NCI_I2C_DRIVER_NAME,
423 .acpi_match_table = ACPI_PTR(acpi_id),
424 .of_match_table = of_match_ptr(of_nxp_nci_i2c_match),
426 .probe = nxp_nci_i2c_probe,
427 .id_table = nxp_nci_i2c_id_table,
428 .remove = nxp_nci_i2c_remove,
431 module_i2c_driver(nxp_nci_i2c_driver);
433 MODULE_LICENSE("GPL");
434 MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");
435 MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");
436 MODULE_AUTHOR("Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>");