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