1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Driver for NXP PN533 NFC Chip - I2C transport layer
4 *
5 * Copyright (C) 2011 Instituto Nokia de Tecnologia
6 * Copyright (C) 2012-2013 Tieto Poland
7 * Copyright (C) 2016 HALE electronic
8 */
9
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>
19 #include "pn533.h"
20
21 #define VERSION "0.1"
22
23 #define PN533_I2C_DRIVER_NAME "pn533_i2c"
24
25 struct pn533_i2c_phy {
26 struct i2c_client *i2c_dev;
27 struct pn533 *priv;
28
29 bool aborted;
30
31 int hard_fault; /*
32 * < 0 if hardware error occurred (e.g. i2c err)
33 * and prevents normal operation.
34 */
35 };
36
pn533_i2c_send_ack(struct pn533 * dev,gfp_t flags)37 static int pn533_i2c_send_ack(struct pn533 *dev, gfp_t flags)
38 {
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 */
43 int rc;
44
45 rc = i2c_master_send(client, ack, 6);
46
47 return rc;
48 }
49
pn533_i2c_send_frame(struct pn533 * dev,struct sk_buff * out)50 static int pn533_i2c_send_frame(struct pn533 *dev,
51 struct sk_buff *out)
52 {
53 struct pn533_i2c_phy *phy = dev->phy;
54 struct i2c_client *client = phy->i2c_dev;
55 int rc;
56
57 if (phy->hard_fault != 0)
58 return phy->hard_fault;
59
60 if (phy->priv == NULL)
61 phy->priv = dev;
62
63 phy->aborted = false;
64
65 print_hex_dump_debug("PN533_i2c TX: ", DUMP_PREFIX_NONE, 16, 1,
66 out->data, out->len, false);
67
68 rc = i2c_master_send(client, out->data, out->len);
69
70 if (rc == -EREMOTEIO) { /* Retry, chip was in power down */
71 usleep_range(6000, 10000);
72 rc = i2c_master_send(client, out->data, out->len);
73 }
74
75 if (rc >= 0) {
76 if (rc != out->len)
77 rc = -EREMOTEIO;
78 else
79 rc = 0;
80 }
81
82 return rc;
83 }
84
pn533_i2c_abort_cmd(struct pn533 * dev,gfp_t flags)85 static void pn533_i2c_abort_cmd(struct pn533 *dev, gfp_t flags)
86 {
87 struct pn533_i2c_phy *phy = dev->phy;
88
89 phy->aborted = true;
90
91 /* An ack will cancel the last issued command */
92 pn533_i2c_send_ack(dev, flags);
93
94 /* schedule cmd_complete_work to finish current command execution */
95 pn533_recv_frame(phy->priv, NULL, -ENOENT);
96 }
97
pn533_i2c_read(struct pn533_i2c_phy * phy,struct sk_buff ** skb)98 static int pn533_i2c_read(struct pn533_i2c_phy *phy, struct sk_buff **skb)
99 {
100 struct i2c_client *client = phy->i2c_dev;
101 int len = PN533_EXT_FRAME_HEADER_LEN +
102 PN533_STD_FRAME_MAX_PAYLOAD_LEN +
103 PN533_STD_FRAME_TAIL_LEN + 1;
104 int r;
105
106 *skb = alloc_skb(len, GFP_KERNEL);
107 if (*skb == NULL)
108 return -ENOMEM;
109
110 r = i2c_master_recv(client, skb_put(*skb, len), len);
111 if (r != len) {
112 nfc_err(&client->dev, "cannot read. r=%d len=%d\n", r, len);
113 kfree_skb(*skb);
114 return -EREMOTEIO;
115 }
116
117 if (!((*skb)->data[0] & 0x01)) {
118 nfc_err(&client->dev, "READY flag not set");
119 kfree_skb(*skb);
120 return -EBUSY;
121 }
122
123 /* remove READY byte */
124 skb_pull(*skb, 1);
125 /* trim to frame size */
126 skb_trim(*skb, phy->priv->ops->rx_frame_size((*skb)->data));
127
128 return 0;
129 }
130
pn533_i2c_irq_thread_fn(int irq,void * data)131 static irqreturn_t pn533_i2c_irq_thread_fn(int irq, void *data)
132 {
133 struct pn533_i2c_phy *phy = data;
134 struct i2c_client *client;
135 struct sk_buff *skb = NULL;
136 int r;
137
138 if (!phy || irq != phy->i2c_dev->irq) {
139 WARN_ON_ONCE(1);
140 return IRQ_NONE;
141 }
142
143 client = phy->i2c_dev;
144 dev_dbg(&client->dev, "IRQ\n");
145
146 if (phy->hard_fault != 0)
147 return IRQ_HANDLED;
148
149 r = pn533_i2c_read(phy, &skb);
150 if (r == -EREMOTEIO) {
151 phy->hard_fault = r;
152
153 pn533_recv_frame(phy->priv, NULL, -EREMOTEIO);
154
155 return IRQ_HANDLED;
156 } else if ((r == -ENOMEM) || (r == -EBADMSG) || (r == -EBUSY)) {
157 return IRQ_HANDLED;
158 }
159
160 if (!phy->aborted)
161 pn533_recv_frame(phy->priv, skb, 0);
162
163 return IRQ_HANDLED;
164 }
165
166 static struct pn533_phy_ops i2c_phy_ops = {
167 .send_frame = pn533_i2c_send_frame,
168 .send_ack = pn533_i2c_send_ack,
169 .abort_cmd = pn533_i2c_abort_cmd,
170 };
171
172
pn533_i2c_probe(struct i2c_client * client,const struct i2c_device_id * id)173 static int pn533_i2c_probe(struct i2c_client *client,
174 const struct i2c_device_id *id)
175 {
176 struct pn533_i2c_phy *phy;
177 struct pn533 *priv;
178 int r = 0;
179
180 dev_dbg(&client->dev, "%s\n", __func__);
181 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
182
183 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
184 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
185 return -ENODEV;
186 }
187
188 phy = devm_kzalloc(&client->dev, sizeof(struct pn533_i2c_phy),
189 GFP_KERNEL);
190 if (!phy)
191 return -ENOMEM;
192
193 phy->i2c_dev = client;
194 i2c_set_clientdata(client, phy);
195
196 priv = pn53x_common_init(PN533_DEVICE_PN532,
197 PN533_PROTO_REQ_ACK_RESP,
198 phy, &i2c_phy_ops, NULL,
199 &phy->i2c_dev->dev);
200
201 if (IS_ERR(priv)) {
202 r = PTR_ERR(priv);
203 return r;
204 }
205
206 phy->priv = priv;
207 r = pn532_i2c_nfc_alloc(priv, PN533_NO_TYPE_B_PROTOCOLS, &client->dev);
208 if (r)
209 goto nfc_alloc_err;
210
211 r = request_threaded_irq(client->irq, NULL, pn533_i2c_irq_thread_fn,
212 IRQF_TRIGGER_FALLING |
213 IRQF_SHARED | IRQF_ONESHOT,
214 PN533_I2C_DRIVER_NAME, phy);
215 if (r < 0) {
216 nfc_err(&client->dev, "Unable to register IRQ handler\n");
217 goto irq_rqst_err;
218 }
219
220 r = pn533_finalize_setup(priv);
221 if (r)
222 goto fn_setup_err;
223
224 r = nfc_register_device(priv->nfc_dev);
225 if (r)
226 goto fn_setup_err;
227
228 return r;
229
230 fn_setup_err:
231 free_irq(client->irq, phy);
232
233 irq_rqst_err:
234 nfc_free_device(priv->nfc_dev);
235
236 nfc_alloc_err:
237 pn53x_common_clean(phy->priv);
238
239 return r;
240 }
241
pn533_i2c_remove(struct i2c_client * client)242 static int pn533_i2c_remove(struct i2c_client *client)
243 {
244 struct pn533_i2c_phy *phy = i2c_get_clientdata(client);
245
246 dev_dbg(&client->dev, "%s\n", __func__);
247
248 free_irq(client->irq, phy);
249
250 pn53x_unregister_nfc(phy->priv);
251 pn53x_common_clean(phy->priv);
252
253 return 0;
254 }
255
256 static const struct of_device_id of_pn533_i2c_match[] = {
257 { .compatible = "nxp,pn532", },
258 /*
259 * NOTE: The use of the compatibles with the trailing "...-i2c" is
260 * deprecated and will be removed.
261 */
262 { .compatible = "nxp,pn533-i2c", },
263 { .compatible = "nxp,pn532-i2c", },
264 {},
265 };
266 MODULE_DEVICE_TABLE(of, of_pn533_i2c_match);
267
268 static const struct i2c_device_id pn533_i2c_id_table[] = {
269 { PN533_I2C_DRIVER_NAME, 0 },
270 {}
271 };
272 MODULE_DEVICE_TABLE(i2c, pn533_i2c_id_table);
273
274 static struct i2c_driver pn533_i2c_driver = {
275 .driver = {
276 .name = PN533_I2C_DRIVER_NAME,
277 .of_match_table = of_match_ptr(of_pn533_i2c_match),
278 },
279 .probe = pn533_i2c_probe,
280 .id_table = pn533_i2c_id_table,
281 .remove = pn533_i2c_remove,
282 };
283
284 module_i2c_driver(pn533_i2c_driver);
285
286 MODULE_AUTHOR("Michael Thalmeier <michael.thalmeier@hale.at>");
287 MODULE_DESCRIPTION("PN533 I2C driver ver " VERSION);
288 MODULE_VERSION(VERSION);
289 MODULE_LICENSE("GPL");
290