1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for NXP PN533 NFC Chip - I2C transport layer
5 * Copyright (C) 2011 Instituto Nokia de Tecnologia
6 * Copyright (C) 2012-2013 Tieto Poland
7 * Copyright (C) 2016 HALE electronic
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/i2c.h>
15 #include <linux/nfc.h>
16 #include <linux/netdevice.h>
17 #include <linux/interrupt.h>
18 #include <net/nfc/nfc.h>
23 #define PN533_I2C_DRIVER_NAME "pn533_i2c"
25 struct pn533_i2c_phy {
26 struct i2c_client *i2c_dev;
32 * < 0 if hardware error occurred (e.g. i2c err)
33 * and prevents normal operation.
37 static int pn533_i2c_send_ack(struct pn533 *dev, gfp_t flags)
39 struct pn533_i2c_phy *phy = dev->phy;
40 struct i2c_client *client = phy->i2c_dev;
41 static const u8 ack[6] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
42 /* spec 6.2.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
44 return i2c_master_send(client, ack, 6);
47 static int pn533_i2c_send_frame(struct pn533 *dev,
50 struct pn533_i2c_phy *phy = dev->phy;
51 struct i2c_client *client = phy->i2c_dev;
54 if (phy->hard_fault != 0)
55 return phy->hard_fault;
57 if (phy->priv == NULL)
62 print_hex_dump_debug("PN533_i2c TX: ", DUMP_PREFIX_NONE, 16, 1,
63 out->data, out->len, false);
65 rc = i2c_master_send(client, out->data, out->len);
67 if (rc == -EREMOTEIO) { /* Retry, chip was in power down */
68 usleep_range(6000, 10000);
69 rc = i2c_master_send(client, out->data, out->len);
82 static void pn533_i2c_abort_cmd(struct pn533 *dev, gfp_t flags)
84 struct pn533_i2c_phy *phy = dev->phy;
88 /* An ack will cancel the last issued command */
89 pn533_i2c_send_ack(dev, flags);
91 /* schedule cmd_complete_work to finish current command execution */
92 pn533_recv_frame(phy->priv, NULL, -ENOENT);
95 static int pn533_i2c_read(struct pn533_i2c_phy *phy, struct sk_buff **skb)
97 struct i2c_client *client = phy->i2c_dev;
98 int len = PN533_EXT_FRAME_HEADER_LEN +
99 PN533_STD_FRAME_MAX_PAYLOAD_LEN +
100 PN533_STD_FRAME_TAIL_LEN + 1;
103 *skb = alloc_skb(len, GFP_KERNEL);
107 r = i2c_master_recv(client, skb_put(*skb, len), len);
109 nfc_err(&client->dev, "cannot read. r=%d len=%d\n", r, len);
114 if (!((*skb)->data[0] & 0x01)) {
115 nfc_err(&client->dev, "READY flag not set");
120 /* remove READY byte */
122 /* trim to frame size */
123 skb_trim(*skb, phy->priv->ops->rx_frame_size((*skb)->data));
128 static irqreturn_t pn533_i2c_irq_thread_fn(int irq, void *data)
130 struct pn533_i2c_phy *phy = data;
131 struct sk_buff *skb = NULL;
134 if (!phy || irq != phy->i2c_dev->irq) {
139 if (phy->hard_fault != 0)
142 r = pn533_i2c_read(phy, &skb);
143 if (r == -EREMOTEIO) {
146 pn533_recv_frame(phy->priv, NULL, -EREMOTEIO);
149 } else if ((r == -ENOMEM) || (r == -EBADMSG) || (r == -EBUSY)) {
154 pn533_recv_frame(phy->priv, skb, 0);
159 static const struct pn533_phy_ops i2c_phy_ops = {
160 .send_frame = pn533_i2c_send_frame,
161 .send_ack = pn533_i2c_send_ack,
162 .abort_cmd = pn533_i2c_abort_cmd,
166 static int pn533_i2c_probe(struct i2c_client *client,
167 const struct i2c_device_id *id)
169 struct pn533_i2c_phy *phy;
173 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
174 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
178 phy = devm_kzalloc(&client->dev, sizeof(struct pn533_i2c_phy),
183 phy->i2c_dev = client;
184 i2c_set_clientdata(client, phy);
186 priv = pn53x_common_init(PN533_DEVICE_PN532,
187 PN533_PROTO_REQ_ACK_RESP,
188 phy, &i2c_phy_ops, NULL,
192 return PTR_ERR(priv);
195 r = pn532_i2c_nfc_alloc(priv, PN533_NO_TYPE_B_PROTOCOLS, &client->dev);
199 r = request_threaded_irq(client->irq, NULL, pn533_i2c_irq_thread_fn,
200 IRQF_TRIGGER_FALLING |
201 IRQF_SHARED | IRQF_ONESHOT,
202 PN533_I2C_DRIVER_NAME, phy);
204 nfc_err(&client->dev, "Unable to register IRQ handler\n");
208 r = pn533_finalize_setup(priv);
212 r = nfc_register_device(priv->nfc_dev);
219 free_irq(client->irq, phy);
222 nfc_free_device(priv->nfc_dev);
225 pn53x_common_clean(phy->priv);
230 static void pn533_i2c_remove(struct i2c_client *client)
232 struct pn533_i2c_phy *phy = i2c_get_clientdata(client);
234 free_irq(client->irq, phy);
236 pn53x_unregister_nfc(phy->priv);
237 pn53x_common_clean(phy->priv);
240 static const struct of_device_id of_pn533_i2c_match[] __maybe_unused = {
241 { .compatible = "nxp,pn532", },
243 * NOTE: The use of the compatibles with the trailing "...-i2c" is
244 * deprecated and will be removed.
246 { .compatible = "nxp,pn533-i2c", },
247 { .compatible = "nxp,pn532-i2c", },
250 MODULE_DEVICE_TABLE(of, of_pn533_i2c_match);
252 static const struct i2c_device_id pn533_i2c_id_table[] = {
253 { PN533_I2C_DRIVER_NAME, 0 },
256 MODULE_DEVICE_TABLE(i2c, pn533_i2c_id_table);
258 static struct i2c_driver pn533_i2c_driver = {
260 .name = PN533_I2C_DRIVER_NAME,
261 .of_match_table = of_match_ptr(of_pn533_i2c_match),
263 .probe = pn533_i2c_probe,
264 .id_table = pn533_i2c_id_table,
265 .remove = pn533_i2c_remove,
268 module_i2c_driver(pn533_i2c_driver);
270 MODULE_AUTHOR("Michael Thalmeier <michael.thalmeier@hale.at>");
271 MODULE_DESCRIPTION("PN533 I2C driver ver " VERSION);
272 MODULE_VERSION(VERSION);
273 MODULE_LICENSE("GPL");