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.
8 * Authors: Clément Perrochaud <clement.perrochaud@nxp.com>
9 * Authors: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>
11 * Derived from PN544 device driver:
12 * Copyright (C) 2012 Intel Corporation. All rights reserved.
15 #include <linux/acpi.h>
16 #include <linux/delay.h>
17 #include <linux/i2c.h>
18 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/nfc.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/unaligned.h>
24 #include <net/nfc/nfc.h>
28 #define NXP_NCI_I2C_DRIVER_NAME "nxp-nci_i2c"
30 #define NXP_NCI_I2C_MAX_PAYLOAD 32
32 struct nxp_nci_i2c_phy
{
33 struct i2c_client
*i2c_dev
;
36 struct gpio_desc
*gpiod_en
;
37 struct gpio_desc
*gpiod_fw
;
40 * < 0 if hardware error occurred (e.g. i2c err)
41 * and prevents normal operation.
45 static int nxp_nci_i2c_set_mode(void *phy_id
,
46 enum nxp_nci_mode mode
)
48 struct nxp_nci_i2c_phy
*phy
= (struct nxp_nci_i2c_phy
*) phy_id
;
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);
52 usleep_range(10000, 15000);
54 if (mode
== NXP_NCI_MODE_COLD
)
60 static int nxp_nci_i2c_write(void *phy_id
, struct sk_buff
*skb
)
63 struct nxp_nci_i2c_phy
*phy
= phy_id
;
64 struct i2c_client
*client
= phy
->i2c_dev
;
66 if (phy
->hard_fault
!= 0)
67 return phy
->hard_fault
;
69 r
= i2c_master_send(client
, skb
->data
, skb
->len
);
71 /* Retry, chip was in standby */
73 r
= i2c_master_send(client
, skb
->data
, skb
->len
);
77 nfc_err(&client
->dev
, "Error %d on I2C send\n", r
);
78 } else if (r
!= skb
->len
) {
80 "Invalid length sent: %u (expected %u)\n",
84 /* Success but return 0 and not number of bytes */
91 static const struct nxp_nci_phy_ops i2c_phy_ops
= {
92 .set_mode
= nxp_nci_i2c_set_mode
,
93 .write
= nxp_nci_i2c_write
,
96 static int nxp_nci_i2c_fw_read(struct nxp_nci_i2c_phy
*phy
,
99 struct i2c_client
*client
= phy
->i2c_dev
;
104 r
= i2c_master_recv(client
, (u8
*) &header
, NXP_NCI_FW_HDR_LEN
);
107 } else if (r
!= NXP_NCI_FW_HDR_LEN
) {
108 nfc_err(&client
->dev
, "Incorrect header length: %u\n", r
);
113 frame_len
= (be16_to_cpu(header
) & NXP_NCI_FW_FRAME_LEN_MASK
) +
116 *skb
= alloc_skb(NXP_NCI_FW_HDR_LEN
+ frame_len
, GFP_KERNEL
);
122 skb_put_data(*skb
, &header
, NXP_NCI_FW_HDR_LEN
);
124 r
= i2c_master_recv(client
, skb_put(*skb
, frame_len
), frame_len
);
126 goto fw_read_exit_free_skb
;
127 } else if (r
!= frame_len
) {
128 nfc_err(&client
->dev
,
129 "Invalid frame length: %u (expected %zu)\n",
132 goto fw_read_exit_free_skb
;
137 fw_read_exit_free_skb
:
143 static int nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy
*phy
,
144 struct sk_buff
**skb
)
146 struct nci_ctrl_hdr header
; /* May actually be a data header */
147 struct i2c_client
*client
= phy
->i2c_dev
;
150 r
= i2c_master_recv(client
, (u8
*) &header
, NCI_CTRL_HDR_SIZE
);
153 } else if (r
!= NCI_CTRL_HDR_SIZE
) {
154 nfc_err(&client
->dev
, "Incorrect header length: %u\n", r
);
159 *skb
= alloc_skb(NCI_CTRL_HDR_SIZE
+ header
.plen
, GFP_KERNEL
);
165 skb_put_data(*skb
, (void *)&header
, NCI_CTRL_HDR_SIZE
);
170 r
= i2c_master_recv(client
, skb_put(*skb
, header
.plen
), header
.plen
);
172 goto nci_read_exit_free_skb
;
173 } else if (r
!= header
.plen
) {
174 nfc_err(&client
->dev
,
175 "Invalid frame payload length: %u (expected %u)\n",
178 goto nci_read_exit_free_skb
;
183 nci_read_exit_free_skb
:
189 static irqreturn_t
nxp_nci_i2c_irq_thread_fn(int irq
, void *phy_id
)
191 struct nxp_nci_i2c_phy
*phy
= phy_id
;
192 struct i2c_client
*client
;
193 struct nxp_nci_info
*info
;
195 struct sk_buff
*skb
= NULL
;
198 if (!phy
|| !phy
->ndev
)
201 client
= phy
->i2c_dev
;
203 if (!client
|| irq
!= client
->irq
)
206 info
= nci_get_drvdata(phy
->ndev
);
211 mutex_lock(&info
->info_lock
);
213 if (phy
->hard_fault
!= 0)
214 goto exit_irq_handled
;
216 switch (info
->mode
) {
217 case NXP_NCI_MODE_NCI
:
218 r
= nxp_nci_i2c_nci_read(phy
, &skb
);
220 case NXP_NCI_MODE_FW
:
221 r
= nxp_nci_i2c_fw_read(phy
, &skb
);
223 case NXP_NCI_MODE_COLD
:
228 if (r
== -EREMOTEIO
) {
230 if (info
->mode
== NXP_NCI_MODE_FW
)
231 nxp_nci_fw_recv_frame(phy
->ndev
, NULL
);
234 nfc_err(&client
->dev
, "Read failed with error %d\n", r
);
235 goto exit_irq_handled
;
238 switch (info
->mode
) {
239 case NXP_NCI_MODE_NCI
:
240 nci_recv_frame(phy
->ndev
, skb
);
242 case NXP_NCI_MODE_FW
:
243 nxp_nci_fw_recv_frame(phy
->ndev
, skb
);
245 case NXP_NCI_MODE_COLD
:
250 mutex_unlock(&info
->info_lock
);
257 static const struct acpi_gpio_params firmware_gpios
= { 1, 0, false };
258 static const struct acpi_gpio_params enable_gpios
= { 2, 0, false };
260 static const struct acpi_gpio_mapping acpi_nxp_nci_gpios
[] = {
261 { "enable-gpios", &enable_gpios
, 1 },
262 { "firmware-gpios", &firmware_gpios
, 1 },
266 static int nxp_nci_i2c_probe(struct i2c_client
*client
)
268 struct device
*dev
= &client
->dev
;
269 struct nxp_nci_i2c_phy
*phy
;
272 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
273 nfc_err(&client
->dev
, "Need I2C_FUNC_I2C\n");
277 phy
= devm_kzalloc(&client
->dev
, sizeof(struct nxp_nci_i2c_phy
),
282 phy
->i2c_dev
= client
;
283 i2c_set_clientdata(client
, phy
);
285 r
= devm_acpi_dev_add_driver_gpios(dev
, acpi_nxp_nci_gpios
);
287 dev_dbg(dev
, "Unable to add GPIO mapping table\n");
289 phy
->gpiod_en
= devm_gpiod_get(dev
, "enable", GPIOD_OUT_LOW
);
290 if (IS_ERR(phy
->gpiod_en
)) {
291 nfc_err(dev
, "Failed to get EN gpio\n");
292 return PTR_ERR(phy
->gpiod_en
);
295 phy
->gpiod_fw
= devm_gpiod_get_optional(dev
, "firmware", GPIOD_OUT_LOW
);
296 if (IS_ERR(phy
->gpiod_fw
)) {
297 nfc_err(dev
, "Failed to get FW gpio\n");
298 return PTR_ERR(phy
->gpiod_fw
);
301 r
= nxp_nci_probe(phy
, &client
->dev
, &i2c_phy_ops
,
302 NXP_NCI_I2C_MAX_PAYLOAD
, &phy
->ndev
);
306 r
= request_threaded_irq(client
->irq
, NULL
,
307 nxp_nci_i2c_irq_thread_fn
,
308 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
309 NXP_NCI_I2C_DRIVER_NAME
, phy
);
311 nfc_err(&client
->dev
, "Unable to register IRQ handler\n");
316 static void nxp_nci_i2c_remove(struct i2c_client
*client
)
318 struct nxp_nci_i2c_phy
*phy
= i2c_get_clientdata(client
);
320 nxp_nci_remove(phy
->ndev
);
321 free_irq(client
->irq
, phy
);
324 static const struct i2c_device_id nxp_nci_i2c_id_table
[] = {
328 MODULE_DEVICE_TABLE(i2c
, nxp_nci_i2c_id_table
);
330 static const struct of_device_id of_nxp_nci_i2c_match
[] = {
331 { .compatible
= "nxp,nxp-nci-i2c", },
334 MODULE_DEVICE_TABLE(of
, of_nxp_nci_i2c_match
);
337 static const struct acpi_device_id acpi_id
[] = {
343 MODULE_DEVICE_TABLE(acpi
, acpi_id
);
346 static struct i2c_driver nxp_nci_i2c_driver
= {
348 .name
= NXP_NCI_I2C_DRIVER_NAME
,
349 .acpi_match_table
= ACPI_PTR(acpi_id
),
350 .of_match_table
= of_nxp_nci_i2c_match
,
352 .probe
= nxp_nci_i2c_probe
,
353 .id_table
= nxp_nci_i2c_id_table
,
354 .remove
= nxp_nci_i2c_remove
,
357 module_i2c_driver(nxp_nci_i2c_driver
);
359 MODULE_LICENSE("GPL");
360 MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");
361 MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");
362 MODULE_AUTHOR("Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>");