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 <linux/unaligned/access_ok.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
;
55 unsigned int gpio_irq
;
58 * < 0 if hardware error occurred (e.g. i2c err)
59 * and prevents normal operation.
63 static int nxp_nci_i2c_set_mode(void *phy_id
,
64 enum nxp_nci_mode mode
)
66 struct nxp_nci_i2c_phy
*phy
= (struct nxp_nci_i2c_phy
*) phy_id
;
68 gpio_set_value(phy
->gpio_fw
, (mode
== NXP_NCI_MODE_FW
) ? 1 : 0);
69 gpio_set_value(phy
->gpio_en
, (mode
!= NXP_NCI_MODE_COLD
) ? 1 : 0);
70 usleep_range(10000, 15000);
72 if (mode
== NXP_NCI_MODE_COLD
)
78 static int nxp_nci_i2c_write(void *phy_id
, struct sk_buff
*skb
)
81 struct nxp_nci_i2c_phy
*phy
= phy_id
;
82 struct i2c_client
*client
= phy
->i2c_dev
;
84 if (phy
->hard_fault
!= 0)
85 return phy
->hard_fault
;
87 r
= i2c_master_send(client
, skb
->data
, skb
->len
);
88 if (r
== -EREMOTEIO
) {
89 /* Retry, chip was in standby */
90 usleep_range(110000, 120000);
91 r
= i2c_master_send(client
, skb
->data
, skb
->len
);
95 nfc_err(&client
->dev
, "Error %d on I2C send\n", r
);
96 } else if (r
!= skb
->len
) {
98 "Invalid length sent: %u (expected %u)\n",
102 /* Success but return 0 and not number of bytes */
109 static const struct nxp_nci_phy_ops i2c_phy_ops
= {
110 .set_mode
= nxp_nci_i2c_set_mode
,
111 .write
= nxp_nci_i2c_write
,
114 static int nxp_nci_i2c_fw_read(struct nxp_nci_i2c_phy
*phy
,
115 struct sk_buff
**skb
)
117 struct i2c_client
*client
= phy
->i2c_dev
;
122 r
= i2c_master_recv(client
, (u8
*) &header
, NXP_NCI_FW_HDR_LEN
);
125 } else if (r
!= NXP_NCI_FW_HDR_LEN
) {
126 nfc_err(&client
->dev
, "Incorrect header length: %u\n", r
);
131 frame_len
= (get_unaligned_be16(&header
) & NXP_NCI_FW_FRAME_LEN_MASK
) +
134 *skb
= alloc_skb(NXP_NCI_FW_HDR_LEN
+ frame_len
, GFP_KERNEL
);
140 memcpy(skb_put(*skb
, NXP_NCI_FW_HDR_LEN
), &header
, NXP_NCI_FW_HDR_LEN
);
142 r
= i2c_master_recv(client
, skb_put(*skb
, frame_len
), frame_len
);
143 if (r
!= frame_len
) {
144 nfc_err(&client
->dev
,
145 "Invalid frame length: %u (expected %zu)\n",
148 goto fw_read_exit_free_skb
;
153 fw_read_exit_free_skb
:
159 static int nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy
*phy
,
160 struct sk_buff
**skb
)
162 struct nci_ctrl_hdr header
; /* May actually be a data header */
163 struct i2c_client
*client
= phy
->i2c_dev
;
166 r
= i2c_master_recv(client
, (u8
*) &header
, NCI_CTRL_HDR_SIZE
);
169 } else if (r
!= NCI_CTRL_HDR_SIZE
) {
170 nfc_err(&client
->dev
, "Incorrect header length: %u\n", r
);
175 *skb
= alloc_skb(NCI_CTRL_HDR_SIZE
+ header
.plen
, GFP_KERNEL
);
181 memcpy(skb_put(*skb
, NCI_CTRL_HDR_SIZE
), (void *) &header
,
184 r
= i2c_master_recv(client
, skb_put(*skb
, header
.plen
), header
.plen
);
185 if (r
!= header
.plen
) {
186 nfc_err(&client
->dev
,
187 "Invalid frame payload length: %u (expected %u)\n",
190 goto nci_read_exit_free_skb
;
195 nci_read_exit_free_skb
:
201 static irqreturn_t
nxp_nci_i2c_irq_thread_fn(int irq
, void *phy_id
)
203 struct nxp_nci_i2c_phy
*phy
= phy_id
;
204 struct i2c_client
*client
;
205 struct nxp_nci_info
*info
;
207 struct sk_buff
*skb
= NULL
;
210 if (!phy
|| !phy
->ndev
)
213 client
= phy
->i2c_dev
;
215 if (!client
|| irq
!= client
->irq
)
218 info
= nci_get_drvdata(phy
->ndev
);
223 mutex_lock(&info
->info_lock
);
225 if (phy
->hard_fault
!= 0)
226 goto exit_irq_handled
;
228 switch (info
->mode
) {
229 case NXP_NCI_MODE_NCI
:
230 r
= nxp_nci_i2c_nci_read(phy
, &skb
);
232 case NXP_NCI_MODE_FW
:
233 r
= nxp_nci_i2c_fw_read(phy
, &skb
);
235 case NXP_NCI_MODE_COLD
:
240 if (r
== -EREMOTEIO
) {
244 nfc_err(&client
->dev
, "Read failed with error %d\n", r
);
245 goto exit_irq_handled
;
248 switch (info
->mode
) {
249 case NXP_NCI_MODE_NCI
:
250 nci_recv_frame(phy
->ndev
, skb
);
252 case NXP_NCI_MODE_FW
:
253 nxp_nci_fw_recv_frame(phy
->ndev
, skb
);
255 case NXP_NCI_MODE_COLD
:
260 mutex_unlock(&info
->info_lock
);
269 static int nxp_nci_i2c_parse_devtree(struct i2c_client
*client
)
271 struct nxp_nci_i2c_phy
*phy
= i2c_get_clientdata(client
);
272 struct device_node
*pp
;
275 pp
= client
->dev
.of_node
;
279 r
= of_get_named_gpio(pp
, "enable-gpios", 0);
280 if (r
== -EPROBE_DEFER
)
281 r
= of_get_named_gpio(pp
, "enable-gpios", 0);
283 nfc_err(&client
->dev
, "Failed to get EN gpio, error: %d\n", r
);
288 r
= of_get_named_gpio(pp
, "firmware-gpios", 0);
289 if (r
== -EPROBE_DEFER
)
290 r
= of_get_named_gpio(pp
, "firmware-gpios", 0);
292 nfc_err(&client
->dev
, "Failed to get FW gpio, error: %d\n", r
);
297 r
= irq_of_parse_and_map(pp
, 0);
299 nfc_err(&client
->dev
, "Unable to get irq, error: %d\n", r
);
309 static int nxp_nci_i2c_parse_devtree(struct i2c_client
*client
)
316 static int nxp_nci_i2c_acpi_config(struct nxp_nci_i2c_phy
*phy
)
318 struct i2c_client
*client
= phy
->i2c_dev
;
319 struct gpio_desc
*gpiod_en
, *gpiod_fw
, *gpiod_irq
;
321 gpiod_en
= devm_gpiod_get_index(&client
->dev
, NULL
, 2, GPIOD_OUT_LOW
);
322 gpiod_fw
= devm_gpiod_get_index(&client
->dev
, NULL
, 1, GPIOD_OUT_LOW
);
323 gpiod_irq
= devm_gpiod_get_index(&client
->dev
, NULL
, 0, GPIOD_IN
);
325 if (IS_ERR(gpiod_en
) || IS_ERR(gpiod_fw
) || IS_ERR(gpiod_irq
)) {
326 nfc_err(&client
->dev
, "No GPIOs\n");
330 client
->irq
= gpiod_to_irq(gpiod_irq
);
331 if (client
->irq
< 0) {
332 nfc_err(&client
->dev
, "No IRQ\n");
336 phy
->gpio_en
= desc_to_gpio(gpiod_en
);
337 phy
->gpio_fw
= desc_to_gpio(gpiod_fw
);
338 phy
->gpio_irq
= desc_to_gpio(gpiod_irq
);
343 static int nxp_nci_i2c_probe(struct i2c_client
*client
,
344 const struct i2c_device_id
*id
)
346 struct nxp_nci_i2c_phy
*phy
;
347 struct nxp_nci_nfc_platform_data
*pdata
;
350 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
351 nfc_err(&client
->dev
, "Need I2C_FUNC_I2C\n");
356 phy
= devm_kzalloc(&client
->dev
, sizeof(struct nxp_nci_i2c_phy
),
363 phy
->i2c_dev
= client
;
364 i2c_set_clientdata(client
, phy
);
366 pdata
= client
->dev
.platform_data
;
368 if (!pdata
&& client
->dev
.of_node
) {
369 r
= nxp_nci_i2c_parse_devtree(client
);
371 nfc_err(&client
->dev
, "Failed to get DT data\n");
375 phy
->gpio_en
= pdata
->gpio_en
;
376 phy
->gpio_fw
= pdata
->gpio_fw
;
377 client
->irq
= pdata
->irq
;
378 } else if (ACPI_HANDLE(&client
->dev
)) {
379 r
= nxp_nci_i2c_acpi_config(phy
);
384 nfc_err(&client
->dev
, "No platform data\n");
389 r
= devm_gpio_request_one(&phy
->i2c_dev
->dev
, phy
->gpio_en
,
390 GPIOF_OUT_INIT_LOW
, "nxp_nci_en");
394 r
= devm_gpio_request_one(&phy
->i2c_dev
->dev
, phy
->gpio_fw
,
395 GPIOF_OUT_INIT_LOW
, "nxp_nci_fw");
400 r
= nxp_nci_probe(phy
, &client
->dev
, &i2c_phy_ops
,
401 NXP_NCI_I2C_MAX_PAYLOAD
, &phy
->ndev
);
405 r
= request_threaded_irq(client
->irq
, NULL
,
406 nxp_nci_i2c_irq_thread_fn
,
407 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
408 NXP_NCI_I2C_DRIVER_NAME
, phy
);
410 nfc_err(&client
->dev
, "Unable to register IRQ handler\n");
416 static int nxp_nci_i2c_remove(struct i2c_client
*client
)
418 struct nxp_nci_i2c_phy
*phy
= i2c_get_clientdata(client
);
420 nxp_nci_remove(phy
->ndev
);
421 free_irq(client
->irq
, phy
);
426 static struct i2c_device_id nxp_nci_i2c_id_table
[] = {
430 MODULE_DEVICE_TABLE(i2c
, nxp_nci_i2c_id_table
);
432 static const struct of_device_id of_nxp_nci_i2c_match
[] = {
433 { .compatible
= "nxp,nxp-nci-i2c", },
436 MODULE_DEVICE_TABLE(of
, of_nxp_nci_i2c_match
);
439 static struct acpi_device_id acpi_id
[] = {
443 MODULE_DEVICE_TABLE(acpi
, acpi_id
);
446 static struct i2c_driver nxp_nci_i2c_driver
= {
448 .name
= NXP_NCI_I2C_DRIVER_NAME
,
449 .owner
= THIS_MODULE
,
450 .acpi_match_table
= ACPI_PTR(acpi_id
),
451 .of_match_table
= of_match_ptr(of_nxp_nci_i2c_match
),
453 .probe
= nxp_nci_i2c_probe
,
454 .id_table
= nxp_nci_i2c_id_table
,
455 .remove
= nxp_nci_i2c_remove
,
458 module_i2c_driver(nxp_nci_i2c_driver
);
460 MODULE_LICENSE("GPL");
461 MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");
462 MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");
463 MODULE_AUTHOR("Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>");