xref: /openbmc/linux/drivers/nfc/nxp-nci/i2c.c (revision d3597236)
1 /*
2  * I2C link layer for the NXP NCI driver
3  *
4  * Copyright (C) 2014  NXP Semiconductors  All rights reserved.
5  * Copyright (C) 2012-2015  Intel Corporation. All rights reserved.
6  *
7  * Authors: Clément Perrochaud <clement.perrochaud@nxp.com>
8  * Authors: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>
9  *
10  * Derived from PN544 device driver:
11  * Copyright (C) 2012  Intel Corporation. All rights reserved.
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms and conditions of the GNU General Public License,
15  * version 2, as published by the Free Software Foundation.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 
28 #include <linux/acpi.h>
29 #include <linux/delay.h>
30 #include <linux/i2c.h>
31 #include <linux/interrupt.h>
32 #include <linux/miscdevice.h>
33 #include <linux/module.h>
34 #include <linux/nfc.h>
35 #include <linux/gpio/consumer.h>
36 #include <linux/of_gpio.h>
37 #include <linux/of_irq.h>
38 #include <linux/platform_data/nxp-nci.h>
39 #include <linux/unaligned/access_ok.h>
40 
41 #include <net/nfc/nfc.h>
42 
43 #include "nxp-nci.h"
44 
45 #define NXP_NCI_I2C_DRIVER_NAME	"nxp-nci_i2c"
46 
47 #define NXP_NCI_I2C_MAX_PAYLOAD	32
48 
49 struct nxp_nci_i2c_phy {
50 	struct i2c_client *i2c_dev;
51 	struct nci_dev *ndev;
52 
53 	unsigned int gpio_en;
54 	unsigned int gpio_fw;
55 	unsigned int gpio_irq;
56 
57 	int hard_fault; /*
58 			 * < 0 if hardware error occurred (e.g. i2c err)
59 			 * and prevents normal operation.
60 			 */
61 };
62 
63 static int nxp_nci_i2c_set_mode(void *phy_id,
64 				    enum nxp_nci_mode mode)
65 {
66 	struct nxp_nci_i2c_phy *phy = (struct nxp_nci_i2c_phy *) phy_id;
67 
68 	gpio_set_value(phy->gpio_fw, (mode == NXP_NCI_MODE_FW) ? 1 : 0);
69 	gpio_set_value(phy->gpio_en, (mode != NXP_NCI_MODE_COLD) ? 1 : 0);
70 	usleep_range(10000, 15000);
71 
72 	if (mode == NXP_NCI_MODE_COLD)
73 		phy->hard_fault = 0;
74 
75 	return 0;
76 }
77 
78 static int nxp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
79 {
80 	int r;
81 	struct nxp_nci_i2c_phy *phy = phy_id;
82 	struct i2c_client *client = phy->i2c_dev;
83 
84 	if (phy->hard_fault != 0)
85 		return phy->hard_fault;
86 
87 	r = i2c_master_send(client, skb->data, skb->len);
88 	if (r == -EREMOTEIO) {
89 		/* Retry, chip was in standby */
90 		usleep_range(110000, 120000);
91 		r = i2c_master_send(client, skb->data, skb->len);
92 	}
93 
94 	if (r < 0) {
95 		nfc_err(&client->dev, "Error %d on I2C send\n", r);
96 	} else if (r != skb->len) {
97 		nfc_err(&client->dev,
98 			"Invalid length sent: %u (expected %u)\n",
99 			r, skb->len);
100 		r = -EREMOTEIO;
101 	} else {
102 		/* Success but return 0 and not number of bytes */
103 		r = 0;
104 	}
105 
106 	return r;
107 }
108 
109 static struct nxp_nci_phy_ops i2c_phy_ops = {
110 	.set_mode = nxp_nci_i2c_set_mode,
111 	.write = nxp_nci_i2c_write,
112 };
113 
114 static int nxp_nci_i2c_fw_read(struct nxp_nci_i2c_phy *phy,
115 			       struct sk_buff **skb)
116 {
117 	struct i2c_client *client = phy->i2c_dev;
118 	u16 header;
119 	size_t frame_len;
120 	int r;
121 
122 	r = i2c_master_recv(client, (u8 *) &header, NXP_NCI_FW_HDR_LEN);
123 	if (r < 0) {
124 		goto fw_read_exit;
125 	} else if (r != NXP_NCI_FW_HDR_LEN) {
126 		nfc_err(&client->dev, "Incorrect header length: %u\n", r);
127 		r = -EBADMSG;
128 		goto fw_read_exit;
129 	}
130 
131 	frame_len = (get_unaligned_be16(&header) & NXP_NCI_FW_FRAME_LEN_MASK) +
132 		    NXP_NCI_FW_CRC_LEN;
133 
134 	*skb = alloc_skb(NXP_NCI_FW_HDR_LEN + frame_len, GFP_KERNEL);
135 	if (*skb == NULL) {
136 		r = -ENOMEM;
137 		goto fw_read_exit;
138 	}
139 
140 	memcpy(skb_put(*skb, NXP_NCI_FW_HDR_LEN), &header, NXP_NCI_FW_HDR_LEN);
141 
142 	r = i2c_master_recv(client, skb_put(*skb, frame_len), frame_len);
143 	if (r != frame_len) {
144 		nfc_err(&client->dev,
145 			"Invalid frame length: %u (expected %zu)\n",
146 			r, frame_len);
147 		r = -EBADMSG;
148 		goto fw_read_exit_free_skb;
149 	}
150 
151 	return 0;
152 
153 fw_read_exit_free_skb:
154 	kfree_skb(*skb);
155 fw_read_exit:
156 	return r;
157 }
158 
159 static int nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy *phy,
160 				struct sk_buff **skb)
161 {
162 	struct nci_ctrl_hdr header; /* May actually be a data header */
163 	struct i2c_client *client = phy->i2c_dev;
164 	int r;
165 
166 	r = i2c_master_recv(client, (u8 *) &header, NCI_CTRL_HDR_SIZE);
167 	if (r < 0) {
168 		goto nci_read_exit;
169 	} else if (r != NCI_CTRL_HDR_SIZE) {
170 		nfc_err(&client->dev, "Incorrect header length: %u\n", r);
171 		r = -EBADMSG;
172 		goto nci_read_exit;
173 	}
174 
175 	*skb = alloc_skb(NCI_CTRL_HDR_SIZE + header.plen, GFP_KERNEL);
176 	if (*skb == NULL) {
177 		r = -ENOMEM;
178 		goto nci_read_exit;
179 	}
180 
181 	memcpy(skb_put(*skb, NCI_CTRL_HDR_SIZE), (void *) &header,
182 	       NCI_CTRL_HDR_SIZE);
183 
184 	r = i2c_master_recv(client, skb_put(*skb, header.plen), header.plen);
185 	if (r != header.plen) {
186 		nfc_err(&client->dev,
187 			"Invalid frame payload length: %u (expected %u)\n",
188 			r, header.plen);
189 		r = -EBADMSG;
190 		goto nci_read_exit_free_skb;
191 	}
192 
193 	return 0;
194 
195 nci_read_exit_free_skb:
196 	kfree_skb(*skb);
197 nci_read_exit:
198 	return r;
199 }
200 
201 static irqreturn_t nxp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
202 {
203 	struct nxp_nci_i2c_phy *phy = phy_id;
204 	struct i2c_client *client;
205 	struct nxp_nci_info *info;
206 
207 	struct sk_buff *skb = NULL;
208 	int r = 0;
209 
210 	if (!phy || !phy->ndev)
211 		goto exit_irq_none;
212 
213 	client = phy->i2c_dev;
214 
215 	if (!client || irq != client->irq)
216 		goto exit_irq_none;
217 
218 	info = nci_get_drvdata(phy->ndev);
219 
220 	if (!info)
221 		goto exit_irq_none;
222 
223 	mutex_lock(&info->info_lock);
224 
225 	if (phy->hard_fault != 0)
226 		goto exit_irq_handled;
227 
228 	switch (info->mode) {
229 	case NXP_NCI_MODE_NCI:
230 		r = nxp_nci_i2c_nci_read(phy, &skb);
231 		break;
232 	case NXP_NCI_MODE_FW:
233 		r = nxp_nci_i2c_fw_read(phy, &skb);
234 		break;
235 	case NXP_NCI_MODE_COLD:
236 		r = -EREMOTEIO;
237 		break;
238 	}
239 
240 	if (r == -EREMOTEIO) {
241 		phy->hard_fault = r;
242 		skb = NULL;
243 	} else if (r < 0) {
244 		nfc_err(&client->dev, "Read failed with error %d\n", r);
245 		goto exit_irq_handled;
246 	}
247 
248 	switch (info->mode) {
249 	case NXP_NCI_MODE_NCI:
250 		nci_recv_frame(phy->ndev, skb);
251 		break;
252 	case NXP_NCI_MODE_FW:
253 		nxp_nci_fw_recv_frame(phy->ndev, skb);
254 		break;
255 	case NXP_NCI_MODE_COLD:
256 		break;
257 	}
258 
259 exit_irq_handled:
260 	mutex_unlock(&info->info_lock);
261 	return IRQ_HANDLED;
262 exit_irq_none:
263 	WARN_ON_ONCE(1);
264 	return IRQ_NONE;
265 }
266 
267 #ifdef CONFIG_OF
268 
269 static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
270 {
271 	struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
272 	struct device_node *pp;
273 	int r;
274 
275 	pp = client->dev.of_node;
276 	if (!pp)
277 		return -ENODEV;
278 
279 	r = of_get_named_gpio(pp, "enable-gpios", 0);
280 	if (r == -EPROBE_DEFER)
281 		r = of_get_named_gpio(pp, "enable-gpios", 0);
282 	if (r < 0) {
283 		nfc_err(&client->dev, "Failed to get EN gpio, error: %d\n", r);
284 		return r;
285 	}
286 	phy->gpio_en = r;
287 
288 	r = of_get_named_gpio(pp, "firmware-gpios", 0);
289 	if (r == -EPROBE_DEFER)
290 		r = of_get_named_gpio(pp, "firmware-gpios", 0);
291 	if (r < 0) {
292 		nfc_err(&client->dev, "Failed to get FW gpio, error: %d\n", r);
293 		return r;
294 	}
295 	phy->gpio_fw = r;
296 
297 	r = irq_of_parse_and_map(pp, 0);
298 	if (r < 0) {
299 		nfc_err(&client->dev, "Unable to get irq, error: %d\n", r);
300 		return r;
301 	}
302 	client->irq = r;
303 
304 	return 0;
305 }
306 
307 #else
308 
309 static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
310 {
311 	return -ENODEV;
312 }
313 
314 #endif
315 
316 static int nxp_nci_i2c_acpi_config(struct nxp_nci_i2c_phy *phy)
317 {
318 	struct i2c_client *client = phy->i2c_dev;
319 	struct gpio_desc *gpiod_en, *gpiod_fw, *gpiod_irq;
320 
321 	gpiod_en = devm_gpiod_get_index(&client->dev, NULL, 2);
322 	gpiod_fw = devm_gpiod_get_index(&client->dev, NULL, 1);
323 	gpiod_irq = devm_gpiod_get_index(&client->dev, NULL, 0);
324 
325 	if (IS_ERR(gpiod_en) || IS_ERR(gpiod_fw) || IS_ERR(gpiod_irq)) {
326 		nfc_err(&client->dev, "No GPIOs\n");
327 		return -EINVAL;
328 	}
329 
330 	gpiod_direction_output(gpiod_en, 0);
331 	gpiod_direction_output(gpiod_fw, 0);
332 	gpiod_direction_input(gpiod_irq);
333 
334 	client->irq = gpiod_to_irq(gpiod_irq);
335 	if (client->irq < 0) {
336 		nfc_err(&client->dev, "No IRQ\n");
337 		return -EINVAL;
338 	}
339 
340 	phy->gpio_en = desc_to_gpio(gpiod_en);
341 	phy->gpio_fw = desc_to_gpio(gpiod_fw);
342 	phy->gpio_irq = desc_to_gpio(gpiod_irq);
343 
344 	return 0;
345 }
346 
347 static int nxp_nci_i2c_probe(struct i2c_client *client,
348 			    const struct i2c_device_id *id)
349 {
350 	struct nxp_nci_i2c_phy *phy;
351 	struct nxp_nci_nfc_platform_data *pdata;
352 	int r;
353 
354 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
355 		nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
356 		r = -ENODEV;
357 		goto probe_exit;
358 	}
359 
360 	phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy),
361 			   GFP_KERNEL);
362 	if (!phy) {
363 		r = -ENOMEM;
364 		goto probe_exit;
365 	}
366 
367 	phy->i2c_dev = client;
368 	i2c_set_clientdata(client, phy);
369 
370 	pdata = client->dev.platform_data;
371 
372 	if (!pdata && client->dev.of_node) {
373 		r = nxp_nci_i2c_parse_devtree(client);
374 		if (r < 0) {
375 			nfc_err(&client->dev, "Failed to get DT data\n");
376 			goto probe_exit;
377 		}
378 	} else if (pdata) {
379 		phy->gpio_en = pdata->gpio_en;
380 		phy->gpio_fw = pdata->gpio_fw;
381 		client->irq = pdata->irq;
382 	} else if (ACPI_HANDLE(&client->dev)) {
383 		r = nxp_nci_i2c_acpi_config(phy);
384 		if (r < 0)
385 			goto probe_exit;
386 		goto nci_probe;
387 	} else {
388 		nfc_err(&client->dev, "No platform data\n");
389 		r = -EINVAL;
390 		goto probe_exit;
391 	}
392 
393 	r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_en,
394 				  GPIOF_OUT_INIT_LOW, "nxp_nci_en");
395 	if (r < 0)
396 		goto probe_exit;
397 
398 	r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_fw,
399 				  GPIOF_OUT_INIT_LOW, "nxp_nci_fw");
400 	if (r < 0)
401 		goto probe_exit;
402 
403 nci_probe:
404 	r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
405 			  NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
406 	if (r < 0)
407 		goto probe_exit;
408 
409 	r = request_threaded_irq(client->irq, NULL,
410 				 nxp_nci_i2c_irq_thread_fn,
411 				 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
412 				 NXP_NCI_I2C_DRIVER_NAME, phy);
413 	if (r < 0)
414 		nfc_err(&client->dev, "Unable to register IRQ handler\n");
415 
416 probe_exit:
417 	return r;
418 }
419 
420 static int nxp_nci_i2c_remove(struct i2c_client *client)
421 {
422 	struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
423 
424 	nxp_nci_remove(phy->ndev);
425 	free_irq(client->irq, phy);
426 
427 	return 0;
428 }
429 
430 static struct i2c_device_id nxp_nci_i2c_id_table[] = {
431 	{"nxp-nci_i2c", 0},
432 	{}
433 };
434 MODULE_DEVICE_TABLE(i2c, nxp_nci_i2c_id_table);
435 
436 static const struct of_device_id of_nxp_nci_i2c_match[] = {
437 	{ .compatible = "nxp,nxp-nci-i2c", },
438 	{},
439 };
440 MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match);
441 
442 #ifdef CONFIG_ACPI
443 static struct acpi_device_id acpi_id[] = {
444 	{ "NXP7471" },
445 	{ },
446 };
447 MODULE_DEVICE_TABLE(acpi, acpi_id);
448 #endif
449 
450 static struct i2c_driver nxp_nci_i2c_driver = {
451 	.driver = {
452 		   .name = NXP_NCI_I2C_DRIVER_NAME,
453 		   .owner  = THIS_MODULE,
454 		   .acpi_match_table = ACPI_PTR(acpi_id),
455 		   .of_match_table = of_match_ptr(of_nxp_nci_i2c_match),
456 		  },
457 	.probe = nxp_nci_i2c_probe,
458 	.id_table = nxp_nci_i2c_id_table,
459 	.remove = nxp_nci_i2c_remove,
460 };
461 
462 module_i2c_driver(nxp_nci_i2c_driver);
463 
464 MODULE_LICENSE("GPL");
465 MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");
466 MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");
467 MODULE_AUTHOR("Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>");
468