2 * I2C link layer for the NXP NCI driver
4 * Copyright (C) 2014 NXP Semiconductors All rights reserved.
6 * Authors: Clément Perrochaud <clement.perrochaud@nxp.com>
8 * Derived from PN544 device driver:
9 * Copyright (C) 2012 Intel Corporation. All rights reserved.
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms and conditions of the GNU General Public License,
13 * version 2, as published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/delay.h>
27 #include <linux/i2c.h>
28 #include <linux/interrupt.h>
29 #include <linux/miscdevice.h>
30 #include <linux/module.h>
31 #include <linux/nfc.h>
32 #include <linux/of_gpio.h>
33 #include <linux/of_irq.h>
34 #include <linux/platform_data/nxp-nci.h>
35 #include <linux/unaligned/access_ok.h>
37 #include <net/nfc/nfc.h>
41 #define NXP_NCI_I2C_DRIVER_NAME "nxp-nci_i2c"
43 #define NXP_NCI_I2C_MAX_PAYLOAD 32
45 struct nxp_nci_i2c_phy {
46 struct i2c_client *i2c_dev;
53 * < 0 if hardware error occurred (e.g. i2c err)
54 * and prevents normal operation.
58 static int nxp_nci_i2c_set_mode(void *phy_id,
59 enum nxp_nci_mode mode)
61 struct nxp_nci_i2c_phy *phy = (struct nxp_nci_i2c_phy *) phy_id;
63 gpio_set_value(phy->gpio_fw, (mode == NXP_NCI_MODE_FW) ? 1 : 0);
64 gpio_set_value(phy->gpio_en, (mode != NXP_NCI_MODE_COLD) ? 1 : 0);
65 usleep_range(10000, 15000);
67 if (mode == NXP_NCI_MODE_COLD)
73 static int nxp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
76 struct nxp_nci_i2c_phy *phy = phy_id;
77 struct i2c_client *client = phy->i2c_dev;
79 if (phy->hard_fault != 0)
80 return phy->hard_fault;
82 r = i2c_master_send(client, skb->data, skb->len);
83 if (r == -EREMOTEIO) {
84 /* Retry, chip was in standby */
85 usleep_range(110000, 120000);
86 r = i2c_master_send(client, skb->data, skb->len);
90 nfc_err(&client->dev, "Error %d on I2C send\n", r);
91 } else if (r != skb->len) {
93 "Invalid length sent: %u (expected %u)\n",
97 /* Success but return 0 and not number of bytes */
104 static struct nxp_nci_phy_ops i2c_phy_ops = {
105 .set_mode = nxp_nci_i2c_set_mode,
106 .write = nxp_nci_i2c_write,
109 static int nxp_nci_i2c_fw_read(struct nxp_nci_i2c_phy *phy,
110 struct sk_buff **skb)
112 struct i2c_client *client = phy->i2c_dev;
117 r = i2c_master_recv(client, (u8 *) &header, NXP_NCI_FW_HDR_LEN);
120 } else if (r != NXP_NCI_FW_HDR_LEN) {
121 nfc_err(&client->dev, "Incorrect header length: %u\n", r);
126 frame_len = (get_unaligned_be16(&header) & NXP_NCI_FW_FRAME_LEN_MASK) +
129 *skb = alloc_skb(NXP_NCI_FW_HDR_LEN + frame_len, GFP_KERNEL);
135 memcpy(skb_put(*skb, NXP_NCI_FW_HDR_LEN), &header, NXP_NCI_FW_HDR_LEN);
137 r = i2c_master_recv(client, skb_put(*skb, frame_len), frame_len);
138 if (r != frame_len) {
139 nfc_err(&client->dev,
140 "Invalid frame length: %u (expected %zu)\n",
143 goto fw_read_exit_free_skb;
148 fw_read_exit_free_skb:
154 static int nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy *phy,
155 struct sk_buff **skb)
157 struct nci_ctrl_hdr header; /* May actually be a data header */
158 struct i2c_client *client = phy->i2c_dev;
161 r = i2c_master_recv(client, (u8 *) &header, NCI_CTRL_HDR_SIZE);
164 } else if (r != NCI_CTRL_HDR_SIZE) {
165 nfc_err(&client->dev, "Incorrect header length: %u\n", r);
170 *skb = alloc_skb(NCI_CTRL_HDR_SIZE + header.plen, GFP_KERNEL);
176 memcpy(skb_put(*skb, NCI_CTRL_HDR_SIZE), (void *) &header,
179 r = i2c_master_recv(client, skb_put(*skb, header.plen), header.plen);
180 if (r != header.plen) {
181 nfc_err(&client->dev,
182 "Invalid frame payload length: %u (expected %u)\n",
185 goto nci_read_exit_free_skb;
190 nci_read_exit_free_skb:
196 static irqreturn_t nxp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
198 struct nxp_nci_i2c_phy *phy = phy_id;
199 struct i2c_client *client;
200 struct nxp_nci_info *info;
202 struct sk_buff *skb = NULL;
205 if (!phy || !phy->ndev)
208 client = phy->i2c_dev;
210 if (!client || irq != client->irq)
213 info = nci_get_drvdata(phy->ndev);
218 mutex_lock(&info->info_lock);
220 if (phy->hard_fault != 0)
221 goto exit_irq_handled;
223 switch (info->mode) {
224 case NXP_NCI_MODE_NCI:
225 r = nxp_nci_i2c_nci_read(phy, &skb);
227 case NXP_NCI_MODE_FW:
228 r = nxp_nci_i2c_fw_read(phy, &skb);
230 case NXP_NCI_MODE_COLD:
235 if (r == -EREMOTEIO) {
239 nfc_err(&client->dev, "Read failed with error %d\n", r);
240 goto exit_irq_handled;
243 switch (info->mode) {
244 case NXP_NCI_MODE_NCI:
245 nci_recv_frame(phy->ndev, skb);
247 case NXP_NCI_MODE_FW:
248 nxp_nci_fw_recv_frame(phy->ndev, skb);
250 case NXP_NCI_MODE_COLD:
255 mutex_unlock(&info->info_lock);
264 static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
266 struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
267 struct device_node *pp;
270 pp = client->dev.of_node;
274 r = of_get_named_gpio(pp, "enable-gpios", 0);
275 if (r == -EPROBE_DEFER)
276 r = of_get_named_gpio(pp, "enable-gpios", 0);
278 nfc_err(&client->dev, "Failed to get EN gpio, error: %d\n", r);
283 r = of_get_named_gpio(pp, "firmware-gpios", 0);
284 if (r == -EPROBE_DEFER)
285 r = of_get_named_gpio(pp, "firmware-gpios", 0);
287 nfc_err(&client->dev, "Failed to get FW gpio, error: %d\n", r);
292 r = irq_of_parse_and_map(pp, 0);
294 nfc_err(&client->dev, "Unable to get irq, error: %d\n", r);
304 static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
311 static int nxp_nci_i2c_probe(struct i2c_client *client,
312 const struct i2c_device_id *id)
314 struct nxp_nci_i2c_phy *phy;
315 struct nxp_nci_nfc_platform_data *pdata;
318 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
319 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
324 phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy),
331 phy->i2c_dev = client;
332 i2c_set_clientdata(client, phy);
334 pdata = client->dev.platform_data;
336 if (!pdata && client->dev.of_node) {
337 r = nxp_nci_i2c_parse_devtree(client);
339 nfc_err(&client->dev, "Failed to get DT data\n");
343 phy->gpio_en = pdata->gpio_en;
344 phy->gpio_fw = pdata->gpio_fw;
345 client->irq = pdata->irq;
347 nfc_err(&client->dev, "No platform data\n");
352 r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_en,
353 GPIOF_OUT_INIT_LOW, "nxp_nci_en");
357 r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_fw,
358 GPIOF_OUT_INIT_LOW, "nxp_nci_fw");
362 r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
363 NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
367 r = request_threaded_irq(client->irq, NULL,
368 nxp_nci_i2c_irq_thread_fn,
369 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
370 NXP_NCI_I2C_DRIVER_NAME, phy);
372 nfc_err(&client->dev, "Unable to register IRQ handler\n");
378 static int nxp_nci_i2c_remove(struct i2c_client *client)
380 struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
382 nxp_nci_remove(phy->ndev);
383 free_irq(client->irq, phy);
388 static struct i2c_device_id nxp_nci_i2c_id_table[] = {
392 MODULE_DEVICE_TABLE(i2c, nxp_nci_i2c_id_table);
394 static const struct of_device_id of_nxp_nci_i2c_match[] = {
395 { .compatible = "nxp,nxp-nci-i2c", },
398 MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match);
400 static struct i2c_driver nxp_nci_i2c_driver = {
402 .name = NXP_NCI_I2C_DRIVER_NAME,
403 .owner = THIS_MODULE,
404 .of_match_table = of_match_ptr(of_nxp_nci_i2c_match),
406 .probe = nxp_nci_i2c_probe,
407 .id_table = nxp_nci_i2c_id_table,
408 .remove = nxp_nci_i2c_remove,
411 module_i2c_driver(nxp_nci_i2c_driver);
413 MODULE_LICENSE("GPL");
414 MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");
415 MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");