• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <asm/unaligned.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 
nxp_nci_i2c_set_mode(void * phy_id,enum nxp_nci_mode mode)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 
nxp_nci_i2c_write(void * phy_id,struct sk_buff * skb)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 const struct nxp_nci_phy_ops i2c_phy_ops = {
110 	.set_mode = nxp_nci_i2c_set_mode,
111 	.write = nxp_nci_i2c_write,
112 };
113 
nxp_nci_i2c_fw_read(struct nxp_nci_i2c_phy * phy,struct sk_buff ** skb)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 
nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy * phy,struct sk_buff ** skb)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 
nxp_nci_i2c_irq_thread_fn(int irq,void * phy_id)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 		if (info->mode == NXP_NCI_MODE_FW)
243 			nxp_nci_fw_recv_frame(phy->ndev, NULL);
244 	}
245 	if (r < 0) {
246 		nfc_err(&client->dev, "Read failed with error %d\n", r);
247 		goto exit_irq_handled;
248 	}
249 
250 	switch (info->mode) {
251 	case NXP_NCI_MODE_NCI:
252 		nci_recv_frame(phy->ndev, skb);
253 		break;
254 	case NXP_NCI_MODE_FW:
255 		nxp_nci_fw_recv_frame(phy->ndev, skb);
256 		break;
257 	case NXP_NCI_MODE_COLD:
258 		break;
259 	}
260 
261 exit_irq_handled:
262 	mutex_unlock(&info->info_lock);
263 	return IRQ_HANDLED;
264 exit_irq_none:
265 	WARN_ON_ONCE(1);
266 	return IRQ_NONE;
267 }
268 
269 #ifdef CONFIG_OF
270 
nxp_nci_i2c_parse_devtree(struct i2c_client * client)271 static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
272 {
273 	struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
274 	struct device_node *pp;
275 	int r;
276 
277 	pp = client->dev.of_node;
278 	if (!pp)
279 		return -ENODEV;
280 
281 	r = of_get_named_gpio(pp, "enable-gpios", 0);
282 	if (r == -EPROBE_DEFER)
283 		r = of_get_named_gpio(pp, "enable-gpios", 0);
284 	if (r < 0) {
285 		nfc_err(&client->dev, "Failed to get EN gpio, error: %d\n", r);
286 		return r;
287 	}
288 	phy->gpio_en = r;
289 
290 	r = of_get_named_gpio(pp, "firmware-gpios", 0);
291 	if (r == -EPROBE_DEFER)
292 		r = of_get_named_gpio(pp, "firmware-gpios", 0);
293 	if (r < 0) {
294 		nfc_err(&client->dev, "Failed to get FW gpio, error: %d\n", r);
295 		return r;
296 	}
297 	phy->gpio_fw = r;
298 
299 	r = irq_of_parse_and_map(pp, 0);
300 	if (r < 0) {
301 		nfc_err(&client->dev, "Unable to get irq, error: %d\n", r);
302 		return r;
303 	}
304 	client->irq = r;
305 
306 	return 0;
307 }
308 
309 #else
310 
nxp_nci_i2c_parse_devtree(struct i2c_client * client)311 static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
312 {
313 	return -ENODEV;
314 }
315 
316 #endif
317 
nxp_nci_i2c_acpi_config(struct nxp_nci_i2c_phy * phy)318 static int nxp_nci_i2c_acpi_config(struct nxp_nci_i2c_phy *phy)
319 {
320 	struct i2c_client *client = phy->i2c_dev;
321 	struct gpio_desc *gpiod_en, *gpiod_fw, *gpiod_irq;
322 
323 	gpiod_en = devm_gpiod_get_index(&client->dev, NULL, 2, GPIOD_OUT_LOW);
324 	gpiod_fw = devm_gpiod_get_index(&client->dev, NULL, 1, GPIOD_OUT_LOW);
325 	gpiod_irq = devm_gpiod_get_index(&client->dev, NULL, 0, GPIOD_IN);
326 
327 	if (IS_ERR(gpiod_en) || IS_ERR(gpiod_fw) || IS_ERR(gpiod_irq)) {
328 		nfc_err(&client->dev, "No GPIOs\n");
329 		return -EINVAL;
330 	}
331 
332 	client->irq = gpiod_to_irq(gpiod_irq);
333 	if (client->irq < 0) {
334 		nfc_err(&client->dev, "No IRQ\n");
335 		return -EINVAL;
336 	}
337 
338 	phy->gpio_en = desc_to_gpio(gpiod_en);
339 	phy->gpio_fw = desc_to_gpio(gpiod_fw);
340 	phy->gpio_irq = desc_to_gpio(gpiod_irq);
341 
342 	return 0;
343 }
344 
nxp_nci_i2c_probe(struct i2c_client * client,const struct i2c_device_id * id)345 static int nxp_nci_i2c_probe(struct i2c_client *client,
346 			    const struct i2c_device_id *id)
347 {
348 	struct nxp_nci_i2c_phy *phy;
349 	struct nxp_nci_nfc_platform_data *pdata;
350 	int r;
351 
352 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
353 		nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
354 		r = -ENODEV;
355 		goto probe_exit;
356 	}
357 
358 	phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy),
359 			   GFP_KERNEL);
360 	if (!phy) {
361 		r = -ENOMEM;
362 		goto probe_exit;
363 	}
364 
365 	phy->i2c_dev = client;
366 	i2c_set_clientdata(client, phy);
367 
368 	pdata = client->dev.platform_data;
369 
370 	if (!pdata && client->dev.of_node) {
371 		r = nxp_nci_i2c_parse_devtree(client);
372 		if (r < 0) {
373 			nfc_err(&client->dev, "Failed to get DT data\n");
374 			goto probe_exit;
375 		}
376 	} else if (pdata) {
377 		phy->gpio_en = pdata->gpio_en;
378 		phy->gpio_fw = pdata->gpio_fw;
379 		client->irq = pdata->irq;
380 	} else if (ACPI_HANDLE(&client->dev)) {
381 		r = nxp_nci_i2c_acpi_config(phy);
382 		if (r < 0)
383 			goto probe_exit;
384 		goto nci_probe;
385 	} else {
386 		nfc_err(&client->dev, "No platform data\n");
387 		r = -EINVAL;
388 		goto probe_exit;
389 	}
390 
391 	r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_en,
392 				  GPIOF_OUT_INIT_LOW, "nxp_nci_en");
393 	if (r < 0)
394 		goto probe_exit;
395 
396 	r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_fw,
397 				  GPIOF_OUT_INIT_LOW, "nxp_nci_fw");
398 	if (r < 0)
399 		goto probe_exit;
400 
401 nci_probe:
402 	r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
403 			  NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
404 	if (r < 0)
405 		goto probe_exit;
406 
407 	r = request_threaded_irq(client->irq, NULL,
408 				 nxp_nci_i2c_irq_thread_fn,
409 				 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
410 				 NXP_NCI_I2C_DRIVER_NAME, phy);
411 	if (r < 0)
412 		nfc_err(&client->dev, "Unable to register IRQ handler\n");
413 
414 probe_exit:
415 	return r;
416 }
417 
nxp_nci_i2c_remove(struct i2c_client * client)418 static int nxp_nci_i2c_remove(struct i2c_client *client)
419 {
420 	struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
421 
422 	nxp_nci_remove(phy->ndev);
423 	free_irq(client->irq, phy);
424 
425 	return 0;
426 }
427 
428 static struct i2c_device_id nxp_nci_i2c_id_table[] = {
429 	{"nxp-nci_i2c", 0},
430 	{}
431 };
432 MODULE_DEVICE_TABLE(i2c, nxp_nci_i2c_id_table);
433 
434 static const struct of_device_id of_nxp_nci_i2c_match[] = {
435 	{ .compatible = "nxp,nxp-nci-i2c", },
436 	{},
437 };
438 MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match);
439 
440 #ifdef CONFIG_ACPI
441 static struct acpi_device_id acpi_id[] = {
442 	{ "NXP7471" },
443 	{ },
444 };
445 MODULE_DEVICE_TABLE(acpi, acpi_id);
446 #endif
447 
448 static struct i2c_driver nxp_nci_i2c_driver = {
449 	.driver = {
450 		   .name = NXP_NCI_I2C_DRIVER_NAME,
451 		   .owner  = THIS_MODULE,
452 		   .acpi_match_table = ACPI_PTR(acpi_id),
453 		   .of_match_table = of_match_ptr(of_nxp_nci_i2c_match),
454 		  },
455 	.probe = nxp_nci_i2c_probe,
456 	.id_table = nxp_nci_i2c_id_table,
457 	.remove = nxp_nci_i2c_remove,
458 };
459 
460 module_i2c_driver(nxp_nci_i2c_driver);
461 
462 MODULE_LICENSE("GPL");
463 MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");
464 MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");
465 MODULE_AUTHOR("Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>");
466