1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic driver for NXP NCI NFC chips
5 * Copyright (C) 2014 NXP Semiconductors All rights reserved.
7 * Authors: Clément Perrochaud <clement.perrochaud@nxp.com>
9 * Derived from PN544 device driver:
10 * Copyright (C) 2012 Intel Corporation. All rights reserved.
13 #include <linux/delay.h>
14 #include <linux/module.h>
15 #include <linux/nfc.h>
17 #include <net/nfc/nci_core.h>
21 #define NXP_NCI_HDR_LEN 4
23 #define NXP_NCI_NFC_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
24 NFC_PROTO_MIFARE_MASK | \
25 NFC_PROTO_FELICA_MASK | \
26 NFC_PROTO_ISO14443_MASK | \
27 NFC_PROTO_ISO14443_B_MASK | \
28 NFC_PROTO_NFC_DEP_MASK)
30 static int nxp_nci_open(struct nci_dev
*ndev
)
32 struct nxp_nci_info
*info
= nci_get_drvdata(ndev
);
35 mutex_lock(&info
->info_lock
);
37 if (info
->mode
!= NXP_NCI_MODE_COLD
) {
42 if (info
->phy_ops
->set_mode
)
43 r
= info
->phy_ops
->set_mode(info
->phy_id
, NXP_NCI_MODE_NCI
);
45 info
->mode
= NXP_NCI_MODE_NCI
;
48 mutex_unlock(&info
->info_lock
);
52 static int nxp_nci_close(struct nci_dev
*ndev
)
54 struct nxp_nci_info
*info
= nci_get_drvdata(ndev
);
57 mutex_lock(&info
->info_lock
);
59 if (info
->phy_ops
->set_mode
)
60 r
= info
->phy_ops
->set_mode(info
->phy_id
, NXP_NCI_MODE_COLD
);
62 info
->mode
= NXP_NCI_MODE_COLD
;
64 mutex_unlock(&info
->info_lock
);
68 static int nxp_nci_send(struct nci_dev
*ndev
, struct sk_buff
*skb
)
70 struct nxp_nci_info
*info
= nci_get_drvdata(ndev
);
73 if (!info
->phy_ops
->write
) {
78 if (info
->mode
!= NXP_NCI_MODE_NCI
) {
83 r
= info
->phy_ops
->write(info
->phy_id
, skb
);
91 static struct nci_ops nxp_nci_ops
= {
93 .close
= nxp_nci_close
,
95 .fw_download
= nxp_nci_fw_download
,
98 int nxp_nci_probe(void *phy_id
, struct device
*pdev
,
99 const struct nxp_nci_phy_ops
*phy_ops
,
100 unsigned int max_payload
,
101 struct nci_dev
**ndev
)
103 struct nxp_nci_info
*info
;
106 info
= devm_kzalloc(pdev
, sizeof(struct nxp_nci_info
), GFP_KERNEL
);
112 info
->phy_id
= phy_id
;
114 info
->phy_ops
= phy_ops
;
115 info
->max_payload
= max_payload
;
116 INIT_WORK(&info
->fw_info
.work
, nxp_nci_fw_work
);
117 init_completion(&info
->fw_info
.cmd_completion
);
118 mutex_init(&info
->info_lock
);
120 if (info
->phy_ops
->set_mode
) {
121 r
= info
->phy_ops
->set_mode(info
->phy_id
, NXP_NCI_MODE_COLD
);
126 info
->mode
= NXP_NCI_MODE_COLD
;
128 info
->ndev
= nci_allocate_device(&nxp_nci_ops
, NXP_NCI_NFC_PROTOCOLS
,
135 nci_set_parent_dev(info
->ndev
, pdev
);
136 nci_set_drvdata(info
->ndev
, info
);
137 r
= nci_register_device(info
->ndev
);
139 goto probe_exit_free_nci
;
146 nci_free_device(info
->ndev
);
150 EXPORT_SYMBOL(nxp_nci_probe
);
152 void nxp_nci_remove(struct nci_dev
*ndev
)
154 struct nxp_nci_info
*info
= nci_get_drvdata(ndev
);
156 if (info
->mode
== NXP_NCI_MODE_FW
)
157 nxp_nci_fw_work_complete(info
, -ESHUTDOWN
);
158 cancel_work_sync(&info
->fw_info
.work
);
160 mutex_lock(&info
->info_lock
);
162 if (info
->phy_ops
->set_mode
)
163 info
->phy_ops
->set_mode(info
->phy_id
, NXP_NCI_MODE_COLD
);
165 nci_unregister_device(ndev
);
166 nci_free_device(ndev
);
168 mutex_unlock(&info
->info_lock
);
170 EXPORT_SYMBOL(nxp_nci_remove
);
172 MODULE_LICENSE("GPL");
173 MODULE_DESCRIPTION("NXP NCI NFC driver");
174 MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");