1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2010 Trusted Logic S.A.
4 * modifications copyright (C) 2015 NXP B.V.
5 */
6#include <linux/kernel.h>
7#include <linux/module.h>
8#include <linux/fs.h>
9#include <linux/slab.h>
10#include <linux/init.h>
11#include <linux/list.h>
12#include <linux/i2c.h>
13#include <linux/irq.h>
14#include <linux/jiffies.h>
15#include <linux/uaccess.h>
16#include <linux/delay.h>
17#include <linux/interrupt.h>
18#include <linux/io.h>
19#include <linux/platform_device.h>
20#include <linux/gpio.h>
21#include <linux/miscdevice.h>
22#include <linux/spinlock.h>
23#include "pn5xx_i2c.h"
24#include <linux/of_gpio.h>
25#include <linux/regulator/consumer.h>
26#include <linux/of.h>
27
28#define MAX_BUFFER_SIZE	512
29
30#define MODE_OFF    0
31#define MODE_RUN    1
32#define MODE_FW     2
33
34/* Only pn548, pn547 and pn544 are supported */
35#define CHIP "pn544"
36#define DRIVER_CARD "PN54x NFC"
37#define DRIVER_DESC "NFC driver for PN54x Family"
38
39#ifndef CONFIG_OF
40#define CONFIG_OF
41#endif
42
43struct pn54x_dev	{
44	wait_queue_head_t read_wq;
45	struct mutex read_mutex;
46	struct i2c_client *client;
47	struct miscdevice pn54x_device;
48	int ven_gpio;
49	int firm_gpio;
50	int irq_gpio;
51	int clkreq_gpio;
52	struct regulator *pvdd_reg;
53	struct regulator *vbat_reg;
54	struct regulator *pmuvcc_reg;
55	struct regulator *sevdd_reg;
56	bool irq_enabled;
57	spinlock_t irq_enabled_lock;
58};
59
60/**********************************************************
61 * Interrupt control and handler
62 **********************************************************/
63static void pn54x_disable_irq(struct pn54x_dev *pn54x_dev)
64{
65	unsigned long flags;
66
67	spin_lock_irqsave(&pn54x_dev->irq_enabled_lock, flags);
68	if (pn54x_dev->irq_enabled) {
69		disable_irq_nosync(pn54x_dev->client->irq);
70		pn54x_dev->irq_enabled = false;
71	}
72	spin_unlock_irqrestore(&pn54x_dev->irq_enabled_lock, flags);
73}
74
75static irqreturn_t pn54x_dev_irq_handler(int irq, void *dev_id)
76{
77	struct pn54x_dev *pn54x_dev = dev_id;
78
79	pn54x_disable_irq(pn54x_dev);
80
81	/* Wake up waiting readers */
82	wake_up(&pn54x_dev->read_wq);
83
84	return IRQ_HANDLED;
85}
86
87/**********************************************************
88 * private functions
89 **********************************************************/
90static int pn544_enable(struct pn54x_dev *dev, int mode)
91{
92	int r;
93
94	/* turn on the regulators */
95	/* -- if the regulators were specified, they're required */
96	if (dev->pvdd_reg) {
97		r = regulator_enable(dev->pvdd_reg);
98		if (r < 0) {
99			pr_err("%s: not able to enable pvdd\n", __func__);
100			return r;
101		}
102	}
103	if (dev->vbat_reg != NULL) {
104		r = regulator_enable(dev->vbat_reg);
105		if (r < 0) {
106			pr_err("%s: not able to enable vbat\n", __func__);
107			goto enable_exit0;
108		}
109	}
110	if (dev->pmuvcc_reg != NULL) {
111		r = regulator_enable(dev->pmuvcc_reg);
112		if (r < 0) {
113			pr_err("%s: not able to enable pmuvcc\n", __func__);
114			goto enable_exit1;
115		}
116	}
117	if (dev->sevdd_reg != NULL) {
118		r = regulator_enable(dev->sevdd_reg);
119		if (r < 0) {
120			pr_err("%s: not able to enable sevdd\n", __func__);
121			goto enable_exit2;
122		}
123	}
124
125	if (mode == MODE_RUN) {
126		if (gpio_is_valid(dev->firm_gpio))
127			gpio_set_value_cansleep(dev->firm_gpio, 0);
128		gpio_set_value_cansleep(dev->ven_gpio, 1);
129		msleep(100);
130	} else if (mode == MODE_FW) {
131		/* power on with firmware download (requires hw reset)
132		 */
133		gpio_set_value(dev->ven_gpio, 1);
134		msleep(20);
135		if (gpio_is_valid(dev->firm_gpio)) {
136			gpio_set_value(dev->firm_gpio, 1);
137		} else {
138			pr_err("%s Unused Firm GPIO %d\n", __func__, mode);
139			return GPIO_UNUSED;
140		}
141		msleep(20);
142		gpio_set_value(dev->ven_gpio, 0);
143		msleep(100);
144		gpio_set_value(dev->ven_gpio, 1);
145		msleep(20);
146	} else {
147		pr_err("%s bad arg %d\n", __func__, mode);
148		return -EINVAL;
149	}
150
151	return 0;
152
153enable_exit2:
154	if (dev->pmuvcc_reg)
155		regulator_disable(dev->pmuvcc_reg);
156enable_exit1:
157	if (dev->vbat_reg)
158		regulator_disable(dev->vbat_reg);
159enable_exit0:
160	if (dev->pvdd_reg)
161		regulator_disable(dev->pvdd_reg);
162
163	return r;
164}
165
166static void pn544_disable(struct pn54x_dev *dev)
167{
168	/* power off */
169	if (gpio_is_valid(dev->firm_gpio))
170		gpio_set_value_cansleep(dev->firm_gpio, 0);
171	gpio_set_value_cansleep(dev->ven_gpio, 0);
172	msleep(100);
173
174	if (dev->sevdd_reg)
175		regulator_disable(dev->sevdd_reg);
176	if (dev->pmuvcc_reg)
177		regulator_disable(dev->pmuvcc_reg);
178	if (dev->vbat_reg)
179		regulator_disable(dev->vbat_reg);
180	if (dev->pvdd_reg)
181		regulator_disable(dev->pvdd_reg);
182
183}
184
185/**********************************************************
186 * driver functions
187 **********************************************************/
188static ssize_t pn54x_dev_read(struct file *filp, char __user *buf,
189		size_t count, loff_t *offset)
190{
191	struct pn54x_dev *pn54x_dev = filp->private_data;
192	char tmp[MAX_BUFFER_SIZE];
193	int ret;
194
195	if (count > MAX_BUFFER_SIZE)
196		count = MAX_BUFFER_SIZE;
197
198	pr_debug("%s : reading %zu bytes.\n", __func__, count);
199
200	mutex_lock(&pn54x_dev->read_mutex);
201
202	if (!gpio_get_value(pn54x_dev->irq_gpio)) {
203		if (filp->f_flags & O_NONBLOCK) {
204			ret = -EAGAIN;
205			goto fail;
206		}
207
208		while (1) {
209			pn54x_dev->irq_enabled = true;
210			enable_irq(pn54x_dev->client->irq);
211			ret = wait_event_interruptible(
212					pn54x_dev->read_wq,
213					!pn54x_dev->irq_enabled);
214
215			pn54x_disable_irq(pn54x_dev);
216
217			if (ret)
218				goto fail;
219
220			if (gpio_get_value(pn54x_dev->irq_gpio))
221				break;
222
223			pr_warning("%s: spurious interrupt detected\n", __func__);
224		}
225	}
226
227	/* Read data */
228	ret = i2c_master_recv(pn54x_dev->client, tmp, count);
229
230	mutex_unlock(&pn54x_dev->read_mutex);
231	udelay(1000);
232
233	if (ret < 0) {
234		pr_err("%s: i2c_master_recv returned %d\n", __func__, ret);
235		return ret;
236	}
237	if (ret > count) {
238		pr_err("%s: received too many bytes from i2c (%d)\n",
239			__func__, ret);
240		return -EIO;
241	}
242	if (copy_to_user(buf, tmp, ret)) {
243		pr_warning("%s : failed to copy to user space\n", __func__);
244		return -EFAULT;
245	}
246	return ret;
247
248fail:
249	mutex_unlock(&pn54x_dev->read_mutex);
250	return ret;
251}
252
253static ssize_t pn54x_dev_write(struct file *filp, const char __user *buf,
254		size_t count, loff_t *offset)
255{
256	struct pn54x_dev  *pn54x_dev;
257	char tmp[MAX_BUFFER_SIZE];
258	int ret;
259
260	pn54x_dev = filp->private_data;
261
262	if (count > MAX_BUFFER_SIZE)
263		count = MAX_BUFFER_SIZE;
264
265	if (copy_from_user(tmp, buf, count)) {
266		pr_err("%s : failed to copy from user space\n", __func__);
267		return -EFAULT;
268	}
269
270	pr_debug("%s : writing %zu bytes.\n", __func__, count);
271	/* Write data */
272	ret = i2c_master_send(pn54x_dev->client, tmp, count);
273	if (ret != count) {
274		pr_err("%s : i2c_master_send returned %d\n", __func__, ret);
275		ret = -EIO;
276	}
277
278	udelay(1000);
279
280	return ret;
281}
282
283static int pn54x_dev_open(struct inode *inode, struct file *filp)
284{
285	struct pn54x_dev *pn54x_dev = container_of(filp->private_data,
286		struct pn54x_dev, pn54x_device);
287
288	filp->private_data = pn54x_dev;
289	pn544_enable(pn54x_dev, MODE_RUN);
290
291	return 0;
292}
293
294static int pn54x_dev_release(struct inode *inode, struct file *filp)
295{
296	struct pn54x_dev *pn54x_dev = filp->private_data;
297
298	pn544_disable(pn54x_dev);
299
300	return 0;
301}
302
303static long  pn54x_dev_ioctl(struct file *filp, unsigned int cmd,
304				unsigned long arg)
305{
306	struct pn54x_dev *pn54x_dev = filp->private_data;
307
308	switch (cmd) {
309	case PN544_SET_PWR:
310		if (arg == 2) {
311			/* power on w/FW */
312			if (pn544_enable(pn54x_dev, arg) == GPIO_UNUSED)
313				return GPIO_UNUSED;
314		} else if (arg == 1) {
315			/* power on */
316			pn544_enable(pn54x_dev, arg);
317		} else  if (arg == 0) {
318			/* power off */
319			pn544_disable(pn54x_dev);
320		} else {
321			pr_err("%s bad SET_PWR arg %lu\n", __func__, arg);
322			return -EINVAL;
323		}
324		break;
325	case PN54X_CLK_REQ:
326		if (arg == 1) {
327			if (gpio_is_valid(pn54x_dev->clkreq_gpio)) {
328				gpio_set_value(pn54x_dev->clkreq_gpio, 1);
329			} else {
330				pr_err("%s Unused Clkreq GPIO %lu\n", __func__, arg);
331				return GPIO_UNUSED;
332			}
333		} else if (arg == 0) {
334			if (gpio_is_valid(pn54x_dev->clkreq_gpio)) {
335				gpio_set_value(pn54x_dev->clkreq_gpio, 0);
336			} else {
337				pr_err("%s Unused Clkreq GPIO %lu\n", __func__, arg);
338				return GPIO_UNUSED;
339			}
340		} else {
341			pr_err("%s bad CLK_REQ arg %lu\n", __func__, arg);
342			return -EINVAL;
343		}
344		break;
345	default:
346		pr_err("%s bad ioctl %u\n", __func__, cmd);
347		return -EINVAL;
348	}
349
350	return 0;
351}
352
353static const struct file_operations pn54x_dev_fops = {
354	.owner	= THIS_MODULE,
355	.llseek	= no_llseek,
356	.read	= pn54x_dev_read,
357	.write	= pn54x_dev_write,
358	.open	= pn54x_dev_open,
359	.release  = pn54x_dev_release,
360	.unlocked_ioctl  = pn54x_dev_ioctl,
361};
362
363
364/*
365 * Handlers for alternative sources of platform_data
366 */
367#ifdef CONFIG_OF
368/*
369 * Translate OpenFirmware node properties into platform_data
370 */
371static int pn54x_get_pdata(struct device *dev, struct pn544_i2c_platform_data *pdata)
372{
373	struct device_node *node;
374	u32 flags;
375	int val;
376
377	/* make sure there is actually a device tree node */
378	node = dev->of_node;
379	if (!node)
380		return -ENODEV;
381
382	memset(pdata, 0, sizeof(*pdata));
383
384	/* read the dev tree data */
385
386	/* ven pin - enable's power to the chip - REQUIRED */
387	val = of_get_named_gpio_flags(node, "enable-gpios", 0, &flags);
388	if (val < 0) {
389		dev_err(dev, "VEN GPIO error getting from OF node\n");
390		return val;
391	}
392	pdata->ven_gpio = val;
393
394	/* firm pin - controls firmware download - OPTIONAL */
395	val = of_get_named_gpio_flags(node, "firmware-gpios", 0, &flags);
396	if (val < 0) {
397		dev_err(dev, "VEN GPIO error getting from OF node\n");
398		return val;
399	}
400	pdata->firm_gpio = val;
401
402	/* irq pin - data available irq - REQUIRED */
403	val = of_get_named_gpio_flags(node, "interrupt-gpios", 0, &flags);
404	if (val < 0) {
405		dev_err(dev, "VEN GPIO error getting from OF node\n");
406		return val;
407	}
408	pdata->irq_gpio = val;
409
410	/* clkreq pin - controls the clock to the PN547 - OPTIONAL */
411	val = of_get_named_gpio_flags(node, "nxp,pn54x-clkreq", 0, &flags);
412	if (val < 0) {
413		dev_err(dev, "VEN GPIO error getting from OF node\n");
414		return val;
415	}
416	pdata->clkreq_gpio = val;
417
418	/* handle the regulator lines - these are optional
419	 * PVdd - pad Vdd (544, 547)
420	 * Vbat - Battery (544, 547)
421	 * PMUVcc - UICC Power (544, 547)
422	 * SEVdd - SE Power (544)
423	 *
424	 * Will attempt to load a matching Regulator Resource for each
425	 * If no resource is provided, then the input will not be controlled
426	 * Example: if only PVdd is provided, it is the only one that will be
427	 *  turned on/off.
428	 */
429	pdata->pvdd_reg = regulator_get(dev, "nxp,pn54x-pvdd");
430	if (IS_ERR(pdata->pvdd_reg)) {
431		pr_err("%s: could not get nxp,pn54x-pvdd, rc=%ld\n", __func__, PTR_ERR(pdata->pvdd_reg));
432		pdata->pvdd_reg = NULL;
433	}
434
435	pdata->vbat_reg = regulator_get(dev, "nxp,pn54x-vbat");
436	if (IS_ERR(pdata->vbat_reg)) {
437		pr_err("%s: could not get nxp,pn54x-vbat, rc=%ld\n", __func__, PTR_ERR(pdata->vbat_reg));
438		pdata->vbat_reg = NULL;
439	}
440
441	pdata->pmuvcc_reg = regulator_get(dev, "nxp,pn54x-pmuvcc");
442	if (IS_ERR(pdata->pmuvcc_reg)) {
443		pr_err("%s: could not get nxp,pn54x-pmuvcc, rc=%ld\n", __func__, PTR_ERR(pdata->pmuvcc_reg));
444		pdata->pmuvcc_reg = NULL;
445	}
446
447	pdata->sevdd_reg = regulator_get(dev, "nxp,pn54x-sevdd");
448	if (IS_ERR(pdata->sevdd_reg)) {
449		pr_err("%s: could not get nxp,pn54x-sevdd, rc=%ld\n", __func__, PTR_ERR(pdata->sevdd_reg));
450		pdata->sevdd_reg = NULL;
451	}
452
453	return 0;
454}
455#else
456static int pn54x_get_pdata(struct device *dev, struct pn544_i2c_platform_data *pdata)
457{
458	pdata = dev->platform_data;
459	return 0;
460}
461#endif
462
463
464/*
465 * pn54x_probe
466 */
467static int pn54x_probe(struct i2c_client *client,
468		const struct i2c_device_id *id)
469{
470	int ret;
471	struct pn544_i2c_platform_data *pdata; // gpio values, from board file or DT
472	struct pn544_i2c_platform_data tmp_pdata;
473	struct pn54x_dev *pn54x_dev; // internal device specific data
474
475	/* ---- retrieve the platform data ---- */
476	/* If the dev.platform_data is NULL, then */
477	/* attempt to read from the device tree */
478	if (!client->dev.platform_data) {
479		ret = pn54x_get_pdata(&(client->dev), &tmp_pdata);
480		if (ret)
481			return ret;
482		pdata = &tmp_pdata;
483	} else {
484		pdata = client->dev.platform_data;
485	}
486
487	if (pdata == NULL) {
488		pr_err("%s : nfc probe fail\n", __func__);
489		return  -ENODEV;
490	}
491
492	/* validate the adapter has basic I2C functionality */
493	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
494		pr_err("%s : need I2C_FUNC_I2C\n", __func__);
495		return  -ENODEV;
496	}
497
498	/* reserve the GPIO pins */
499	ret = gpio_request(pdata->irq_gpio, "nfc_int");
500	if (ret) {
501		pr_err("%s :not able to get GPIO irq_gpio\n", __func__);
502		return  -ENODEV;
503	}
504	ret = gpio_to_irq(pdata->irq_gpio);
505	if (ret < 0) {
506		pr_err("%s :not able to map GPIO irq_gpio to an IRQ\n", __func__);
507		goto err_ven;
508	} else {
509		client->irq = ret;
510	}
511
512	ret = gpio_request(pdata->ven_gpio, "nfc_ven");
513	if (ret) {
514		pr_err("%s :not able to get GPIO ven_gpio\n", __func__);
515		goto err_ven;
516	}
517
518	if (gpio_is_valid(pdata->firm_gpio)) {
519		ret = gpio_request(pdata->firm_gpio, "nfc_firm");
520		if (ret) {
521			pr_err("%s :not able to get GPIO firm_gpio\n", __func__);
522			goto err_firm;
523		}
524	}
525
526	if (gpio_is_valid(pdata->clkreq_gpio)) {
527		ret = gpio_request(pdata->clkreq_gpio, "nfc_clkreq");
528		if (ret) {
529			pr_err("%s :not able to get GPIO clkreq_gpio\n", __func__);
530			goto err_clkreq;
531		}
532	}
533
534	/* allocate the pn54x driver information structure */
535	pn54x_dev = kzalloc(sizeof(*pn54x_dev), GFP_KERNEL);
536	if (pn54x_dev == NULL) {
537		dev_err(&client->dev, "failed to allocate memory for module data\n");
538		ret = -ENOMEM;
539		goto err_exit;
540	}
541
542	/* store the platform data in the driver info struct */
543	pn54x_dev->irq_gpio = pdata->irq_gpio;
544	pn54x_dev->ven_gpio = pdata->ven_gpio;
545	pn54x_dev->firm_gpio = pdata->firm_gpio;
546	pn54x_dev->clkreq_gpio = pdata->clkreq_gpio;
547	pn54x_dev->pvdd_reg = pdata->pvdd_reg;
548	pn54x_dev->vbat_reg = pdata->vbat_reg;
549	pn54x_dev->pmuvcc_reg = pdata->pmuvcc_reg;
550	pn54x_dev->sevdd_reg = pdata->sevdd_reg;
551
552	pn54x_dev->client = client;
553
554	/* finish configuring the I/O */
555	ret = gpio_direction_input(pn54x_dev->irq_gpio);
556	if (ret < 0) {
557		pr_err("%s :not able to set irq_gpio as input\n", __func__);
558		goto err_free;
559	}
560
561	ret = gpio_direction_output(pn54x_dev->ven_gpio, 0);
562	if (ret < 0) {
563		pr_err("%s : not able to set ven_gpio as output\n", __func__);
564		goto err_exit;
565	}
566
567	if (gpio_is_valid(pn54x_dev->firm_gpio)) {
568		ret = gpio_direction_output(pn54x_dev->firm_gpio, 0);
569		if (ret < 0) {
570			pr_err("%s : not able to set firm_gpio as output\n",
571				 __func__);
572			goto err_exit;
573		}
574	}
575
576	if (gpio_is_valid(pn54x_dev->clkreq_gpio)) {
577		ret = gpio_direction_output(pn54x_dev->clkreq_gpio, 0);
578		if (ret < 0) {
579			pr_err("%s : not able to set clkreq_gpio as output\n",
580				   __func__);
581			goto err_exit;
582		}
583	}
584
585	/* init mutex and queues */
586	init_waitqueue_head(&pn54x_dev->read_wq);
587	mutex_init(&pn54x_dev->read_mutex);
588	spin_lock_init(&pn54x_dev->irq_enabled_lock);
589
590	/* register as a misc device - character based with one entry point */
591	pn54x_dev->pn54x_device.minor = MISC_DYNAMIC_MINOR;
592	pn54x_dev->pn54x_device.name = CHIP;
593	pn54x_dev->pn54x_device.fops = &pn54x_dev_fops;
594	ret = misc_register(&pn54x_dev->pn54x_device);
595	if (ret) {
596		pr_err("%s : misc_register failed\n", __FILE__);
597		goto err_misc_register;
598	}
599
600	/* request irq.  the irq is set whenever the chip has data available
601	 * for reading.  it is cleared when all data has been read.
602	 */
603	pn54x_dev->irq_enabled = true;
604	ret = request_irq(client->irq, pn54x_dev_irq_handler,
605				IRQF_TRIGGER_HIGH, client->name, pn54x_dev);
606	if (ret) {
607		dev_err(&client->dev, "request_irq failed\n");
608		goto err_request_irq_failed;
609	}
610	pn54x_disable_irq(pn54x_dev);
611
612	i2c_set_clientdata(client, pn54x_dev);
613
614	return 0;
615
616err_request_irq_failed:
617	misc_deregister(&pn54x_dev->pn54x_device);
618err_misc_register:
619err_free:
620        kfree(pn54x_dev);
621err_exit:
622	if (gpio_is_valid(pdata->clkreq_gpio))
623		gpio_free(pdata->clkreq_gpio);
624err_clkreq:
625	if (gpio_is_valid(pdata->firm_gpio))
626		gpio_free(pdata->firm_gpio);
627err_firm:
628	gpio_free(pdata->ven_gpio);
629err_ven:
630	gpio_free(pdata->irq_gpio);
631	return ret;
632}
633
634static int pn54x_remove(struct i2c_client *client)
635{
636	struct pn54x_dev *pn54x_dev;
637
638	pn54x_dev = i2c_get_clientdata(client);
639	free_irq(client->irq, pn54x_dev);
640	misc_deregister(&pn54x_dev->pn54x_device);
641	mutex_destroy(&pn54x_dev->read_mutex);
642	gpio_free(pn54x_dev->irq_gpio);
643	gpio_free(pn54x_dev->ven_gpio);
644	if (gpio_is_valid(pn54x_dev->firm_gpio))
645		gpio_free(pn54x_dev->firm_gpio);
646	if (gpio_is_valid(pn54x_dev->clkreq_gpio))
647		gpio_free(pn54x_dev->clkreq_gpio);
648	regulator_put(pn54x_dev->pvdd_reg);
649	regulator_put(pn54x_dev->vbat_reg);
650	regulator_put(pn54x_dev->pmuvcc_reg);
651	regulator_put(pn54x_dev->sevdd_reg);
652
653	kfree(pn54x_dev);
654
655	return 0;
656}
657
658/*
659 *
660 */
661#ifdef CONFIG_OF
662static struct of_device_id pn54x_dt_match[] = {
663	{ .compatible = "nxp,pn547", },
664	{ .compatible = "nxp,pn544", },
665	{ .compatible = "nxp,pn7150", },
666	{},
667};
668MODULE_DEVICE_TABLE(of, pn54x_dt_match);
669#endif
670
671static const struct i2c_device_id pn54x_id[] = {
672	{ "pn547", 0 },
673	{ },
674};
675MODULE_DEVICE_TABLE(i2c, pn54x_id);
676
677static struct i2c_driver pn54x_driver = {
678	.id_table	= pn54x_id,
679	.probe		= pn54x_probe,
680	.remove		= pn54x_remove,
681	.driver		= {
682		.owner	= THIS_MODULE,
683		.name	= "pn544",
684		.of_match_table = pn54x_dt_match,
685	},
686};
687
688/*
689 * module load/unload record keeping
690 */
691module_i2c_driver(pn54x_driver);
692
693MODULE_AUTHOR("Sylvain Fonteneau");
694MODULE_DESCRIPTION(DRIVER_DESC);
695MODULE_LICENSE("GPL");
696