Sfoglia il codice sorgente

Merge branch 'caf/driver/br_android_ncihalx_comm_12' into nfc-driver.lnx.13.0

* lc_origin/caf/driver/br_android_ncihalx_comm_12: (44 commits)
  Updated corresponding to - NFC_AR_00_E800_12.10.00_OpnSrc
  Updated corresponding to - NFC_AR_00_E800_12.0D.00_OpnSrc
  Updated corresponding to - NFC_AR_00_E800_12.0A.00_OpnSrc
  Updated corresponding to - NFC_AR_00_E000_12.07.00_OpnSrc
  Clean up for 12.04.01
  Updated corresponding to - NFC_AR_00_E000_12.04.01_OpnSrc
  Updated corresponding to - NFC_AR_00_E000_12.02.01_OpnSrc
  Updated corresponding to - NFC_AR_00_02.00_6000_12.51.00_OpnSrc
  Updated corresponding to - NFC_AR_00_6000_11.06.00
  Updated corresponding to - NFC_AR_00_6000_11.04.00_OpnSrc
  Updated corresponding to - NFC_AR_00_6000_11.02.00_OpnSrc
  Updated corresponding to - NFC_AR_00_6000_11.01.00_OpnSrc
  Updated corresponding to - NFC_AR_00_6000_10.00.08_OpnSrc
  Updated corresponding to - NFC_AR_00_6000_10.00.06_OpnSrc
  NFC_NCIHALx_AR2000.09.00.13_OpnSrc
  NFC_NCIHALx_AR2000.09.00.11_OpnSrc
  NFC_NCIHALx_AR2000.09.00.0A
  NFC_NCIHALx_AR2000.09.00.08_RC1
  MW recovery for FW download teardown.
  NFC_NCIHALx_AR2000.09.00.02_OpnSrc
  ...

Change-Id: If31e12a04fac55b45b5eabfb6a6207c63e1926bd
Tapas Dey 3 anni fa
parent
commit
9f6bae5ac6
11 ha cambiato i file con 2090 aggiunte e 0 eliminazioni
  1. 340 0
      LICENSE
  2. 2 0
      README.md
  3. 6 0
      nfc/Kbuild
  4. 13 0
      nfc/Kconfig
  5. 10 0
      nfc/Makefile
  6. 447 0
      nfc/common.c
  7. 212 0
      nfc/common.h
  8. 361 0
      nfc/common_ese.c
  9. 99 0
      nfc/common_ese.h
  10. 554 0
      nfc/i2c_drv.c
  11. 46 0
      nfc/i2c_drv.h

+ 340 - 0
LICENSE

@@ -0,0 +1,340 @@
+                    GNU GENERAL PUBLIC LICENSE
+                       Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc., <http://fsf.org/>
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+                            Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users.  This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it.  (Some other Free Software Foundation software is covered by
+the GNU Lesser General Public License instead.)  You can apply it to
+your programs, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+  To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+  For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have.  You must make sure that they, too, receive or can get the
+source code.  And you must show them these terms so they know their
+rights.
+
+  We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+  Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software.  If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+  Finally, any free program is threatened constantly by software
+patents.  We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary.  To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.
+
+                    GNU GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License.  The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language.  (Hereinafter, translation is included without limitation in
+the term "modification".)  Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+  1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+  2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+    a) You must cause the modified files to carry prominent notices
+    stating that you changed the files and the date of any change.
+
+    b) You must cause any work that you distribute or publish, that in
+    whole or in part contains or is derived from the Program or any
+    part thereof, to be licensed as a whole at no charge to all third
+    parties under the terms of this License.
+
+    c) If the modified program normally reads commands interactively
+    when run, you must cause it, when started running for such
+    interactive use in the most ordinary way, to print or display an
+    announcement including an appropriate copyright notice and a
+    notice that there is no warranty (or else, saying that you provide
+    a warranty) and that users may redistribute the program under
+    these conditions, and telling the user how to view a copy of this
+    License.  (Exception: if the Program itself is interactive but
+    does not normally print such an announcement, your work based on
+    the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole.  If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works.  But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+  3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+    a) Accompany it with the complete corresponding machine-readable
+    source code, which must be distributed under the terms of Sections
+    1 and 2 above on a medium customarily used for software interchange; or,
+
+    b) Accompany it with a written offer, valid for at least three
+    years, to give any third party, for a charge no more than your
+    cost of physically performing source distribution, a complete
+    machine-readable copy of the corresponding source code, to be
+    distributed under the terms of Sections 1 and 2 above on a medium
+    customarily used for software interchange; or,
+
+    c) Accompany it with the information you received as to the offer
+    to distribute corresponding source code.  (This alternative is
+    allowed only for noncommercial distribution and only if you
+    received the program in object code or executable form with such
+    an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it.  For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable.  However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+  4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License.  Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+  5. You are not required to accept this License, since you have not
+signed it.  However, nothing else grants you permission to modify or
+distribute the Program or its derivative works.  These actions are
+prohibited by law if you do not accept this License.  Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+  6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions.  You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+  7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License.  If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all.  For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices.  Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+  8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded.  In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+  9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time.  Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number.  If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation.  If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+  10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission.  For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this.  Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+                            NO WARRANTY
+
+  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+                     END OF TERMS AND CONDITIONS
+
+            How to Apply These Terms to Your New Programs
+
+  If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+  To do so, attach the following notices to the program.  It is safest
+to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+    {description}
+    Copyright (C) {year}  {fullname}
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License along
+    with this program; if not, write to the Free Software Foundation, Inc.,
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+    Gnomovision version 69, Copyright (C) year name of author
+    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+    This is free software, and you are welcome to redistribute it
+    under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License.  Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary.  Here is a sample; alter the names:
+
+  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+  `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+  {signature of Ty Coon}, 1 April 1989
+  Ty Coon, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs.  If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library.  If this is what you want to do, use the GNU Lesser General
+Public License instead of this License.
+

+ 2 - 0
README.md

@@ -0,0 +1,2 @@
+# NXPNFC_I2CDriver
+NFC I2C Open Source driver

+ 6 - 0
nfc/Kbuild

@@ -0,0 +1,6 @@
+obj-$(CONFIG_NXP_NFC_I2C) := pn553_i2c.o
+
+pn553_i2c-y := common.o \
+               common_ese.o \
+               i2c_drv.o
+

+ 13 - 0
nfc/Kconfig

@@ -0,0 +1,13 @@
+#
+# near field communication driver configuration
+#
+
+config NXP_NFC_I2C
+	tristate "NXP NCI based NFC I2C Slave Driver for SNxxx"
+	depends on I2C
+	help
+	  This enables the NFC driver for SNxxx based devices.
+	  This is for I2C connected version. NCI protocol logic
+	  resides in the usermode and it has no other NFC dependencies.
+
+	  If unsure, say N.

+ 10 - 0
nfc/Makefile

@@ -0,0 +1,10 @@
+KERNEL_SRC ?= /lib/modules/$(shell uname -r)/build
+
+all:
+	$(MAKE) -C $(KERNEL_SRC) M=$(shell pwd) modules $(KBUILD_OPTIONS)
+
+modules_install:
+	$(MAKE) INSTALL_MOD_STRIP=1 -C $(KERNEL_SRC) M=$(shell pwd) modules_install
+
+clean:
+	$(MAKE) -C $(KERNEL_SRC) M=$(PWD) clean

+ 447 - 0
nfc/common.c

@@ -0,0 +1,447 @@
+/******************************************************************************
+ * Copyright (C) 2015, The Linux Foundation. All rights reserved.
+ * Copyright (C) 2019-2021 NXP
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ ******************************************************************************/
+#include <linux/gpio.h>
+#include <linux/of_gpio.h>
+#include <linux/delay.h>
+
+#include "common_ese.h"
+
+int nfc_parse_dt(struct device *dev, struct platform_configs *nfc_configs,
+		 uint8_t interface)
+{
+	struct device_node *np = dev->of_node;
+	struct platform_gpio *nfc_gpio = &nfc_configs->gpio;
+
+	if (!np) {
+		pr_err("%s: nfc of_node NULL\n", __func__);
+		return -EINVAL;
+	}
+
+	nfc_gpio->irq = -EINVAL;
+	nfc_gpio->dwl_req = -EINVAL;
+	nfc_gpio->ven = -EINVAL;
+
+	/* irq required for i2c based chips only */
+	if (interface == PLATFORM_IF_I2C) {
+		nfc_gpio->irq = of_get_named_gpio(np, DTS_IRQ_GPIO_STR, 0);
+		if ((!gpio_is_valid(nfc_gpio->irq))) {
+			pr_err("%s: irq gpio invalid %d\n", __func__,
+			       nfc_gpio->irq);
+			return -EINVAL;
+		}
+		pr_info("%s: irq %d\n", __func__, nfc_gpio->irq);
+	}
+	nfc_gpio->ven = of_get_named_gpio(np, DTS_VEN_GPIO_STR, 0);
+	if ((!gpio_is_valid(nfc_gpio->ven))) {
+		pr_err("%s: ven gpio invalid %d\n", __func__, nfc_gpio->ven);
+		return -EINVAL;
+	}
+	/* some products like sn220 does not required fw dwl pin */
+	nfc_gpio->dwl_req = of_get_named_gpio(np, DTS_FWDN_GPIO_STR, 0);
+	if ((!gpio_is_valid(nfc_gpio->dwl_req)))
+		pr_warn("%s: dwl_req gpio invalid %d\n", __func__,
+			nfc_gpio->dwl_req);
+
+	pr_info("%s: %d, %d, %d\n", __func__, nfc_gpio->irq, nfc_gpio->ven,
+		nfc_gpio->dwl_req);
+	return 0;
+}
+
+void set_valid_gpio(int gpio, int value)
+{
+	if (gpio_is_valid(gpio)) {
+		pr_debug("%s: gpio %d value %d\n", __func__, gpio, value);
+		gpio_set_value(gpio, value);
+		/* hardware dependent delay */
+		usleep_range(NFC_GPIO_SET_WAIT_TIME_US,
+			     NFC_GPIO_SET_WAIT_TIME_US + 100);
+	}
+}
+
+int get_valid_gpio(int gpio)
+{
+	int value = -EINVAL;
+
+	if (gpio_is_valid(gpio)) {
+		value = gpio_get_value(gpio);
+		pr_debug("%s: gpio %d value %d\n", __func__, gpio, value);
+	}
+	return value;
+}
+
+void gpio_set_ven(struct nfc_dev *nfc_dev, int value)
+{
+	struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
+
+	if (gpio_get_value(nfc_gpio->ven) != value) {
+		pr_debug("%s: value %d\n", __func__, value);
+		/* reset on change in level from high to low */
+		if (value)
+			ese_cold_reset_release(nfc_dev);
+
+		gpio_set_value(nfc_gpio->ven, value);
+		/* hardware dependent delay */
+		usleep_range(NFC_GPIO_SET_WAIT_TIME_US,
+			     NFC_GPIO_SET_WAIT_TIME_US + 100);
+	}
+}
+
+int configure_gpio(unsigned int gpio, int flag)
+{
+	int ret;
+
+	pr_debug("%s: nfc gpio [%d] flag [%01x]\n", __func__, gpio, flag);
+	if (gpio_is_valid(gpio)) {
+		ret = gpio_request(gpio, "nfc_gpio");
+		if (ret) {
+			pr_err("%s: unable to request nfc gpio [%d]\n",
+			       __func__, gpio);
+			return ret;
+		}
+		/* set direction and value for output pin */
+		if (flag & GPIO_OUTPUT) {
+			ret = gpio_direction_output(gpio, (GPIO_HIGH & flag));
+			pr_debug("%s: nfc o/p gpio %d level %d\n", __func__,
+				 gpio, gpio_get_value(gpio));
+		} else {
+			ret = gpio_direction_input(gpio);
+			pr_debug("%s: nfc i/p gpio %d\n", __func__, gpio);
+		}
+
+		if (ret) {
+			pr_err("%s: unable to set direction for nfc gpio [%d]\n",
+			       __func__, gpio);
+			gpio_free(gpio);
+			return ret;
+		}
+		/* Consider value as control for input IRQ pin */
+		if (flag & GPIO_IRQ) {
+			ret = gpio_to_irq(gpio);
+			if (ret < 0) {
+				pr_err("%s: unable to set irq [%d]\n", __func__,
+				       gpio);
+				gpio_free(gpio);
+				return ret;
+			}
+			pr_debug("%s: gpio_to_irq successful [%d]\n", __func__,
+				 gpio);
+			return ret;
+		}
+	} else {
+		pr_err("%s: invalid gpio\n", __func__);
+		ret = -EINVAL;
+	}
+	return ret;
+}
+
+void gpio_free_all(struct nfc_dev *nfc_dev)
+{
+	struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
+
+	if (gpio_is_valid(nfc_gpio->dwl_req))
+		gpio_free(nfc_gpio->dwl_req);
+
+	if (gpio_is_valid(nfc_gpio->irq))
+		gpio_free(nfc_gpio->irq);
+
+	if (gpio_is_valid(nfc_gpio->ven))
+		gpio_free(nfc_gpio->ven);
+}
+
+void nfc_misc_unregister(struct nfc_dev *nfc_dev, int count)
+{
+	pr_debug("%s: entry\n", __func__);
+	device_destroy(nfc_dev->nfc_class, nfc_dev->devno);
+	cdev_del(&nfc_dev->c_dev);
+	class_destroy(nfc_dev->nfc_class);
+	unregister_chrdev_region(nfc_dev->devno, count);
+}
+
+int nfc_misc_register(struct nfc_dev *nfc_dev,
+		      const struct file_operations *nfc_fops, int count,
+		      char *devname, char *classname)
+{
+	int ret = 0;
+
+	ret = alloc_chrdev_region(&nfc_dev->devno, 0, count, devname);
+	if (ret < 0) {
+		pr_err("%s: failed to alloc chrdev region ret %d\n", __func__,
+		       ret);
+		return ret;
+	}
+	nfc_dev->nfc_class = class_create(THIS_MODULE, classname);
+	if (IS_ERR(nfc_dev->nfc_class)) {
+		ret = PTR_ERR(nfc_dev->nfc_class);
+		pr_err("%s: failed to register device class ret %d\n", __func__,
+		       ret);
+		unregister_chrdev_region(nfc_dev->devno, count);
+		return ret;
+	}
+	cdev_init(&nfc_dev->c_dev, nfc_fops);
+	ret = cdev_add(&nfc_dev->c_dev, nfc_dev->devno, count);
+	if (ret < 0) {
+		pr_err("%s: failed to add cdev ret %d\n", __func__, ret);
+		class_destroy(nfc_dev->nfc_class);
+		unregister_chrdev_region(nfc_dev->devno, count);
+		return ret;
+	}
+	nfc_dev->nfc_device = device_create(nfc_dev->nfc_class, NULL,
+					    nfc_dev->devno, nfc_dev, devname);
+	if (IS_ERR(nfc_dev->nfc_device)) {
+		ret = PTR_ERR(nfc_dev->nfc_device);
+		pr_err("%s: failed to create the device ret %d\n", __func__,
+		       ret);
+		cdev_del(&nfc_dev->c_dev);
+		class_destroy(nfc_dev->nfc_class);
+		unregister_chrdev_region(nfc_dev->devno, count);
+		return ret;
+	}
+	return 0;
+}
+
+/**
+ * nfc_ioctl_power_states() - power control
+ * @nfc_dev:    nfc device data structure
+ * @arg:    mode that we want to move to
+ *
+ * Device power control. Depending on the arg value, device moves to
+ * different states, refer common.h for args
+ *
+ * Return: -ENOIOCTLCMD if arg is not supported, 0 if Success(or no issue)
+ * and error ret code otherwise
+ */
+static int nfc_ioctl_power_states(struct nfc_dev *nfc_dev, unsigned long arg)
+{
+	int ret = 0;
+	struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
+
+	if (arg == NFC_POWER_OFF) {
+		/*
+		 * We are attempting a hardware reset so let us disable
+		 * interrupts to avoid spurious notifications to upper
+		 * layers.
+		 */
+		nfc_dev->nfc_disable_intr(nfc_dev);
+		set_valid_gpio(nfc_gpio->dwl_req, 0);
+		gpio_set_ven(nfc_dev, 0);
+		nfc_dev->nfc_ven_enabled = false;
+	} else if (arg == NFC_POWER_ON) {
+		nfc_dev->nfc_enable_intr(nfc_dev);
+		set_valid_gpio(nfc_gpio->dwl_req, 0);
+
+		gpio_set_ven(nfc_dev, 1);
+		nfc_dev->nfc_ven_enabled = true;
+	} else if (arg == NFC_FW_DWL_VEN_TOGGLE) {
+		/*
+		 * We are switching to download Mode, toggle the enable pin
+		 * in order to set the NFCC in the new mode
+		 */
+		nfc_dev->nfc_disable_intr(nfc_dev);
+		set_valid_gpio(nfc_gpio->dwl_req, 1);
+		nfc_dev->nfc_state = NFC_STATE_FW_DWL;
+		gpio_set_ven(nfc_dev, 0);
+		gpio_set_ven(nfc_dev, 1);
+		nfc_dev->nfc_enable_intr(nfc_dev);
+	} else if (arg == NFC_FW_DWL_HIGH) {
+		/*
+		 * Setting firmware download gpio to HIGH
+		 * before FW download start
+		 */
+		set_valid_gpio(nfc_gpio->dwl_req, 1);
+		nfc_dev->nfc_state = NFC_STATE_FW_DWL;
+
+	} else if (arg == NFC_VEN_FORCED_HARD_RESET) {
+		nfc_dev->nfc_disable_intr(nfc_dev);
+		gpio_set_ven(nfc_dev, 0);
+		gpio_set_ven(nfc_dev, 1);
+		nfc_dev->nfc_enable_intr(nfc_dev);
+	} else if (arg == NFC_FW_DWL_LOW) {
+		/*
+		 * Setting firmware download gpio to LOW
+		 * FW download finished
+		 */
+		set_valid_gpio(nfc_gpio->dwl_req, 0);
+		nfc_dev->nfc_state = NFC_STATE_NCI;
+	} else {
+		pr_err("%s: bad arg %lu\n", __func__, arg);
+		ret = -ENOIOCTLCMD;
+	}
+	return ret;
+}
+
+#ifdef CONFIG_COMPAT
+/**
+ * nfc_dev_compat_ioctl - used to set or get data from upper layer.
+ * @pfile   file node for opened device.
+ * @cmd     ioctl type from upper layer.
+ * @arg     ioctl arg from upper layer.
+ *
+ * NFC and ESE Device power control, based on the argument value
+ *
+ * Return: -ENOIOCTLCMD if arg is not supported
+ * 0 if Success(or no issue)
+ * 0 or 1 in case of arg is ESE_GET_PWR/ESE_POWER_STATE
+ * and error ret code otherwise
+ */
+long nfc_dev_compat_ioctl(struct file *pfile, unsigned int cmd,
+		      unsigned long arg)
+{
+	int ret = 0;
+	arg = (compat_u64)arg;
+	pr_debug("%s: cmd = %x arg = %zx\n", __func__, cmd, arg);
+	ret = nfc_dev_ioctl(pfile, cmd, arg);
+	return ret;
+}
+#endif
+
+/**
+ * nfc_dev_ioctl - used to set or get data from upper layer.
+ * @pfile   file node for opened device.
+ * @cmd     ioctl type from upper layer.
+ * @arg     ioctl arg from upper layer.
+ *
+ * NFC and ESE Device power control, based on the argument value
+ *
+ * Return: -ENOIOCTLCMD if arg is not supported
+ * 0 if Success(or no issue)
+ * 0 or 1 in case of arg is ESE_GET_PWR/ESE_POWER_STATE
+ * and error ret code otherwise
+ */
+long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
+{
+	int ret = 0;
+	struct nfc_dev *nfc_dev = pfile->private_data;
+
+	if (!nfc_dev)
+		return -ENODEV;
+
+	pr_debug("%s: cmd = %x arg = %zx\n", __func__, cmd, arg);
+	switch (cmd) {
+	case NFC_SET_PWR:
+		ret = nfc_ioctl_power_states(nfc_dev, arg);
+		break;
+	case ESE_SET_PWR:
+		ret = nfc_ese_pwr(nfc_dev, arg);
+		break;
+	case ESE_GET_PWR:
+		ret = nfc_ese_pwr(nfc_dev, ESE_POWER_STATE);
+		break;
+	default:
+		pr_err("%s: bad cmd %lu\n", __func__, arg);
+		ret = -ENOIOCTLCMD;
+	};
+	return ret;
+}
+
+int nfc_dev_open(struct inode *inode, struct file *filp)
+{
+	struct nfc_dev *nfc_dev = NULL;
+	nfc_dev = container_of(inode->i_cdev, struct nfc_dev, c_dev);
+
+	if (!nfc_dev)
+		return -ENODEV;
+
+	pr_debug("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
+
+	mutex_lock(&nfc_dev->dev_ref_mutex);
+
+	filp->private_data = nfc_dev;
+
+	if (nfc_dev->dev_ref_count == 0) {
+		set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
+
+		nfc_dev->nfc_enable_intr(nfc_dev);
+	}
+	nfc_dev->dev_ref_count = nfc_dev->dev_ref_count + 1;
+	mutex_unlock(&nfc_dev->dev_ref_mutex);
+	return 0;
+}
+
+int nfc_dev_flush(struct file *pfile, fl_owner_t id)
+{
+	struct nfc_dev *nfc_dev = pfile->private_data;
+
+	if (!nfc_dev)
+		return -ENODEV;
+	/*
+	 * release blocked user thread waiting for pending read during close
+	 */
+	if (!mutex_trylock(&nfc_dev->read_mutex)) {
+		nfc_dev->release_read = true;
+		nfc_dev->nfc_disable_intr(nfc_dev);
+		wake_up(&nfc_dev->read_wq);
+		pr_debug("%s: waiting for release of blocked read\n", __func__);
+		mutex_lock(&nfc_dev->read_mutex);
+		nfc_dev->release_read = false;
+	} else {
+		pr_debug("%s: read thread already released\n", __func__);
+	}
+	mutex_unlock(&nfc_dev->read_mutex);
+	return 0;
+}
+
+int nfc_dev_close(struct inode *inode, struct file *filp)
+{
+	struct nfc_dev *nfc_dev = NULL;
+	nfc_dev = container_of(inode->i_cdev, struct nfc_dev, c_dev);
+
+	if (!nfc_dev)
+		return -ENODEV;
+
+	pr_debug("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
+	mutex_lock(&nfc_dev->dev_ref_mutex);
+	if (nfc_dev->dev_ref_count == 1) {
+		nfc_dev->nfc_disable_intr(nfc_dev);
+		set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
+	}
+	if (nfc_dev->dev_ref_count > 0)
+		nfc_dev->dev_ref_count = nfc_dev->dev_ref_count - 1;
+	else {
+		/*
+		 * Use "ESE_RST_PROT_DIS" as argument
+		 * if eSE calls flow is via NFC driver
+		 * i.e. direct calls from SPI HAL to NFC driver
+		 */
+		nfc_ese_pwr(nfc_dev, ESE_RST_PROT_DIS_NFC);
+	}
+	filp->private_data = NULL;
+
+	mutex_unlock(&nfc_dev->dev_ref_mutex);
+	return 0;
+}
+
+int validate_nfc_state_nci(struct nfc_dev *nfc_dev)
+{
+	struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
+
+	if (!gpio_get_value(nfc_gpio->ven)) {
+		pr_err("%s: ven low - nfcc powered off\n", __func__);
+		return -ENODEV;
+	}
+	if (get_valid_gpio(nfc_gpio->dwl_req) == 1) {
+		pr_err("%s: fw download in-progress\n", __func__);
+		return -EBUSY;
+	}
+	if (nfc_dev->nfc_state != NFC_STATE_NCI) {
+		pr_err("%s: fw download state\n", __func__);
+		return -EBUSY;
+	}
+	return 0;
+}

+ 212 - 0
nfc/common.h

@@ -0,0 +1,212 @@
+/******************************************************************************
+ * Copyright (C) 2015, The Linux Foundation. All rights reserved.
+ * Copyright (C) 2019-2021 NXP
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ ******************************************************************************/
+#ifndef _COMMON_H_
+#define _COMMON_H_
+
+#include <linux/cdev.h>
+
+#include "i2c_drv.h"
+
+/* Max device count for this driver */
+#define DEV_COUNT			1
+/* i2c device class */
+#define CLASS_NAME			"nfc"
+
+/* NFC character device name, this will be in /dev/ */
+#define NFC_CHAR_DEV_NAME		"pn553"
+
+/* NCI packet details */
+#define NCI_CMD				(0x20)
+#define NCI_RSP				(0x40)
+#define NCI_HDR_LEN			(3)
+#define NCI_HDR_IDX			(0)
+#define NCI_HDR_OID_IDX			(1)
+#define NCI_PAYLOAD_IDX			(3)
+#define NCI_PAYLOAD_LEN_IDX		(2)
+
+/* FW DNLD packet details */
+#define DL_HDR_LEN			(2)
+#define DL_CRC_LEN			(2)
+
+#define MAX_NCI_PAYLOAD_LEN		(255)
+#define MAX_NCI_BUFFER_SIZE		(NCI_HDR_LEN + MAX_NCI_PAYLOAD_LEN)
+#define MAX_DL_PAYLOAD_LEN		(550)
+#define MAX_DL_BUFFER_SIZE		(DL_HDR_LEN + DL_CRC_LEN + \
+					MAX_DL_PAYLOAD_LEN)
+
+
+/* Retry count for normal write */
+#define NO_RETRY			(1)
+/* Maximum retry count for standby writes */
+#define MAX_RETRY_COUNT			(3)
+#define MAX_WRITE_IRQ_COUNT		(5)
+#define MAX_IRQ_WAIT_TIME		(90)
+#define WAKEUP_SRC_TIMEOUT		(2000)
+
+/* command response timeout */
+#define NCI_CMD_RSP_TIMEOUT_MS		(2000)
+/* Time to wait for NFCC to be ready again after any change in the GPIO */
+#define NFC_GPIO_SET_WAIT_TIME_US	(10000)
+/* Time to wait for IRQ low during write 5*3ms */
+#define NFC_WRITE_IRQ_WAIT_TIME_US	(3000)
+/* Time to wait before retrying i2c/I3C writes */
+#define WRITE_RETRY_WAIT_TIME_US	(1000)
+/* Time to wait before retrying read for some specific usecases */
+#define READ_RETRY_WAIT_TIME_US		(3500)
+#define NFC_MAGIC			(0xE9)
+
+/* Ioctls */
+/* The type should be aligned with MW HAL definitions */
+#define NFC_SET_PWR			_IOW(NFC_MAGIC, 0x01, uint32_t)
+#define ESE_SET_PWR			_IOW(NFC_MAGIC, 0x02, uint32_t)
+#define ESE_GET_PWR			_IOR(NFC_MAGIC, 0x03, uint32_t)
+
+#define DTS_IRQ_GPIO_STR		"nxp,pn544-irq"
+#define DTS_VEN_GPIO_STR		"nxp,pn544-ven"
+#define DTS_FWDN_GPIO_STR		"nxp,pn544-fw-dwnld"
+
+enum nfcc_ioctl_request {
+	/* NFC disable request with VEN LOW */
+	NFC_POWER_OFF = 0,
+	/* NFC enable request with VEN Toggle */
+	NFC_POWER_ON,
+	/* firmware download request with VEN Toggle */
+	NFC_FW_DWL_VEN_TOGGLE,
+	/* ISO reset request */
+	NFC_ISO_RESET,
+	/* request for firmware download gpio HIGH */
+	NFC_FW_DWL_HIGH,
+	/* VEN hard reset request */
+	NFC_VEN_FORCED_HARD_RESET,
+	/* request for firmware download gpio LOW */
+	NFC_FW_DWL_LOW,
+};
+
+/* nfc platform interface type */
+enum interface_flags {
+	/* I2C physical IF for NFCC */
+	PLATFORM_IF_I2C = 0,
+};
+
+/* nfc state flags */
+enum nfc_state_flags {
+	/* nfc in unknown state */
+	NFC_STATE_UNKNOWN = 0,
+	/* nfc in download mode */
+	NFC_STATE_FW_DWL = 0x1,
+	/* nfc booted in NCI mode */
+	NFC_STATE_NCI = 0x2,
+	/* nfc booted in Fw teared mode */
+	NFC_STATE_FW_TEARED = 0x4,
+};
+/*
+ * Power state for IBI handing, mainly needed to defer the IBI handling
+ *  for the IBI received in suspend state to do it later in resume call
+ */
+enum pm_state_flags {
+	PM_STATE_NORMAL = 0,
+	PM_STATE_SUSPEND,
+	PM_STATE_IBI_BEFORE_RESUME,
+};
+
+/* Enum for GPIO values */
+enum gpio_values {
+	GPIO_INPUT = 0x0,
+	GPIO_OUTPUT = 0x1,
+	GPIO_HIGH = 0x2,
+	GPIO_OUTPUT_HIGH = 0x3,
+	GPIO_IRQ = 0x4,
+};
+
+/* NFC GPIO variables */
+struct platform_gpio {
+	unsigned int irq;
+	unsigned int ven;
+	unsigned int dwl_req;
+};
+
+/* NFC Struct to get all the required configs from DTS */
+struct platform_configs {
+	struct platform_gpio gpio;
+};
+
+/* cold reset Features specific Parameters */
+struct cold_reset {
+	bool rsp_pending;	/* cmd rsp pending status */
+	bool in_progress;	/* for cold reset when gurad timer in progress */
+	bool reset_protection;	/* reset protection enabled/disabled */
+	uint8_t status;		/* status from response buffer */
+	uint8_t rst_prot_src;	/* reset protection source (SPI, NFC) */
+	struct timer_list timer;
+	wait_queue_head_t read_wq;
+};
+
+/* Device specific structure */
+struct nfc_dev {
+	wait_queue_head_t read_wq;
+	struct mutex read_mutex;
+	struct mutex write_mutex;
+	uint8_t *read_kbuf;
+	uint8_t *write_kbuf;
+	struct mutex dev_ref_mutex;
+	unsigned int dev_ref_count;
+	struct class *nfc_class;
+	struct device *nfc_device;
+	struct cdev c_dev;
+	dev_t devno;
+	/* Interface flag */
+	uint8_t interface;
+	/* nfc state flags */
+	uint8_t nfc_state;
+	/* NFC VEN pin state */
+	bool nfc_ven_enabled;
+	bool release_read;
+	union {
+		struct i2c_dev i2c_dev;
+	};
+	struct platform_configs configs;
+	struct cold_reset cold_reset;
+
+	/* function pointers for the common i2c functionality */
+	int (*nfc_read)(struct nfc_dev *dev, char *buf, size_t count,
+			int timeout);
+	int (*nfc_write)(struct nfc_dev *dev, const char *buf,
+			 const size_t count, int max_retry_cnt);
+	int (*nfc_enable_intr)(struct nfc_dev *dev);
+	int (*nfc_disable_intr)(struct nfc_dev *dev);
+};
+
+int nfc_dev_open(struct inode *inode, struct file *filp);
+int nfc_dev_flush(struct file *pfile, fl_owner_t id);
+int nfc_dev_close(struct inode *inode, struct file *filp);
+long nfc_dev_compat_ioctl(struct file *pfile, unsigned int cmd,
+		      unsigned long arg);
+long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg);
+int nfc_parse_dt(struct device *dev, struct platform_configs *nfc_configs,
+		 uint8_t interface);
+int nfc_misc_register(struct nfc_dev *nfc_dev,
+		      const struct file_operations *nfc_fops, int count,
+		      char *devname, char *classname);
+void nfc_misc_unregister(struct nfc_dev *nfc_dev, int count);
+int configure_gpio(unsigned int gpio, int flag);
+void gpio_set_ven(struct nfc_dev *nfc_dev, int value);
+void gpio_free_all(struct nfc_dev *nfc_dev);
+int validate_nfc_state_nci(struct nfc_dev *nfc_dev);
+#endif /* _COMMON_H_ */

+ 361 - 0
nfc/common_ese.c

@@ -0,0 +1,361 @@
+/******************************************************************************
+ * Copyright (C) 2020-2021 NXP
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ ******************************************************************************/
+#include <linux/jiffies.h>
+#include <linux/delay.h>
+#include <linux/gpio.h>
+
+#include "common_ese.h"
+
+static void cold_reset_gaurd_timer_callback(struct timer_list *t)
+{
+	struct cold_reset *cold_reset = from_timer(cold_reset, t, timer);
+
+	pr_debug("%s: entry\n", __func__);
+	cold_reset->in_progress = false;
+}
+
+static long start_cold_reset_guard_timer(struct cold_reset *cold_reset)
+{
+	long ret = -EINVAL;
+
+	if (timer_pending(&cold_reset->timer) == 1) {
+		pr_debug("%s: delete pending timer\n", __func__);
+		/* delete timer if already pending */
+		del_timer(&cold_reset->timer);
+	}
+	cold_reset->in_progress = true;
+	timer_setup(&cold_reset->timer, cold_reset_gaurd_timer_callback, 0);
+	ret = mod_timer(&cold_reset->timer,
+			jiffies + msecs_to_jiffies(ESE_CLD_RST_GUARD_TIME_MS));
+	return ret;
+}
+
+static int send_cold_reset_protection_cmd(struct nfc_dev *nfc_dev,
+					  bool requestType)
+{
+	int ret = 0;
+	int cmd_length = 0;
+	uint8_t *cmd = nfc_dev->write_kbuf;
+	struct cold_reset *cold_reset = &nfc_dev->cold_reset;
+
+	*cmd++ = NCI_PROP_MSG_CMD;
+
+	if (requestType) { /* reset protection */
+		*cmd++ = RST_PROT_OID;
+		*cmd++ = RST_PROT_PAYLOAD_SIZE;
+		*cmd++ = (!cold_reset->reset_protection) ? 1 : 0;
+	} else { /* cold reset */
+		*cmd++ = CLD_RST_OID;
+		*cmd++ = CLD_RST_PAYLOAD_SIZE;
+	}
+	cmd_length = cmd - nfc_dev->write_kbuf;
+
+	ret = nfc_dev->nfc_write(nfc_dev, nfc_dev->write_kbuf, cmd_length,
+				 MAX_RETRY_COUNT);
+	if (ret != cmd_length) {
+		ret = -EIO;
+		pr_err("%s: nfc_write returned %d\n", __func__, ret);
+		goto exit;
+	}
+	cmd = nfc_dev->write_kbuf;
+	if (requestType)
+		pr_debug(" %s: NxpNciX: %d > 0x%02x%02x%02x%02x\n", __func__,
+			 ret, cmd[NCI_HDR_IDX], cmd[NCI_HDR_OID_IDX],
+			 cmd[NCI_PAYLOAD_LEN_IDX], cmd[NCI_PAYLOAD_IDX]);
+	else
+		pr_debug(" %s: NxpNciX: %d > 0x%02x%02x%02x\n", __func__, ret,
+			 cmd[NCI_HDR_IDX], cmd[NCI_HDR_OID_IDX],
+			 cmd[NCI_PAYLOAD_LEN_IDX]);
+exit:
+	return ret;
+}
+
+void wakeup_on_prop_rsp(struct nfc_dev *nfc_dev, uint8_t *buf)
+{
+	struct cold_reset *cold_reset = &nfc_dev->cold_reset;
+
+	cold_reset->status = -EIO;
+	if ((NCI_HDR_LEN + buf[NCI_PAYLOAD_LEN_IDX]) != NCI_PROP_MSG_RSP_LEN)
+		pr_err("%s: invalid response for cold_reset/protection\n",
+		       __func__);
+	else
+		cold_reset->status = buf[NCI_PAYLOAD_IDX];
+
+	pr_debug(" %s: NxpNciR 0x%02x%02x%02x%02x\n", __func__,
+		 buf[NCI_HDR_IDX], buf[NCI_HDR_OID_IDX],
+		 buf[NCI_PAYLOAD_LEN_IDX], buf[NCI_PAYLOAD_IDX]);
+
+	cold_reset->rsp_pending = false;
+	wake_up_interruptible(&cold_reset->read_wq);
+}
+
+static int validate_cold_reset_protection_request(struct cold_reset *cold_reset,
+						  unsigned long arg)
+{
+	int ret = 0;
+
+	if (!cold_reset->reset_protection) {
+		if (IS_RST_PROT_EN_REQ(arg) && IS_SRC_VALID_PROT(arg)) {
+			pr_debug("%s: reset protection enable\n", __func__);
+		} else if (IS_CLD_RST_REQ(arg) && IS_SRC_VALID(arg)) {
+			pr_debug("%s: cold reset\n", __func__);
+		} else if (IS_RST_PROT_DIS_REQ(arg) && IS_SRC_VALID_PROT(arg)) {
+			pr_debug("%s: reset protection already disable\n",
+				 __func__);
+			ret = -EINVAL;
+		} else {
+			pr_err("%s: operation not permitted\n", __func__);
+			ret = -EPERM;
+		}
+	} else {
+		if (IS_RST_PROT_DIS_REQ(arg) &&
+		    IS_SRC(arg, cold_reset->rst_prot_src)) {
+			pr_debug("%s: disable reset protection from same src\n",
+				 __func__);
+		} else if (IS_CLD_RST_REQ(arg) &&
+			   IS_SRC(arg, cold_reset->rst_prot_src)) {
+			pr_debug("%s: cold reset from same source\n", __func__);
+		} else if (IS_RST_PROT_EN_REQ(arg) &&
+			   IS_SRC(arg, cold_reset->rst_prot_src)) {
+			pr_debug("%s: enable reset protection from same src\n",
+				 __func__);
+		} else {
+			pr_err("%s: operation not permitted\n", __func__);
+			ret = -EPERM;
+		}
+	}
+	return ret;
+}
+
+static int perform_cold_reset_protection(struct nfc_dev *nfc_dev,
+					 unsigned long arg)
+{
+	int ret = 0;
+	int timeout = 0;
+	char *rsp = nfc_dev->read_kbuf;
+	struct cold_reset *cold_reset = &nfc_dev->cold_reset;
+
+	/* check if NFCC not in the FW download or hard reset state */
+	ret = validate_nfc_state_nci(nfc_dev);
+	if (ret < 0) {
+		pr_err("%s: invalid cmd\n", __func__);
+		return ret;
+	}
+
+	/* check if NFCC not in the FW download or hard reset state */
+	ret = validate_cold_reset_protection_request(cold_reset, arg);
+	if (ret < 0) {
+		pr_err("%s: invalid cmd\n", __func__);
+		goto err;
+	}
+
+	/* check if cold reset already in progress */
+	if (IS_CLD_RST_REQ(arg) && cold_reset->in_progress) {
+		pr_err("%s: cold reset already in progress\n", __func__);
+		ret = -EBUSY;
+		goto err;
+	}
+
+	/* enable interrupt if not enabled incase when devnode not opened by HAL */
+	nfc_dev->nfc_enable_intr(nfc_dev);
+
+	mutex_lock(&nfc_dev->write_mutex);
+	/* write api has 15ms maximum wait to clear any pending read before */
+	cold_reset->status = -EIO;
+	cold_reset->rsp_pending = true;
+	ret = send_cold_reset_protection_cmd(nfc_dev, IS_RST_PROT_REQ(arg));
+	if (ret < 0) {
+		mutex_unlock(&nfc_dev->write_mutex);
+		cold_reset->rsp_pending = false;
+		pr_err("%s: failed to send cold reset/protection cmd\n",
+		       __func__);
+		goto err;
+	}
+
+	ret = 0;
+	/* start the cold reset guard timer */
+	if (IS_CLD_RST_REQ(arg)) {
+		/* Guard timer not needed when OSU over NFC */
+		if (!(cold_reset->reset_protection && IS_SRC_NFC(arg))) {
+			ret = start_cold_reset_guard_timer(cold_reset);
+			if (ret) {
+				mutex_unlock(&nfc_dev->write_mutex);
+				pr_err("%s: error in mod_timer\n", __func__);
+				goto err;
+			}
+		}
+	}
+
+	timeout = NCI_CMD_RSP_TIMEOUT_MS;
+	do {
+		/* call read api directly if reader thread is not blocked */
+		if (mutex_trylock(&nfc_dev->read_mutex)) {
+			pr_debug("%s: reader thread not pending\n", __func__);
+			ret = nfc_dev->nfc_read(nfc_dev, rsp, 3,
+						timeout);
+			mutex_unlock(&nfc_dev->read_mutex);
+			if (!ret)
+				break;
+			usleep_range(READ_RETRY_WAIT_TIME_US,
+					 READ_RETRY_WAIT_TIME_US + 500);
+		/* Read pending response form the HAL service */
+		} else if (!wait_event_interruptible_timeout(
+					cold_reset->read_wq,
+					cold_reset->rsp_pending == false,
+					msecs_to_jiffies(timeout))) {
+			pr_err("%s: cold reset/prot response timeout\n", __func__);
+			ret = -EAGAIN;
+		}
+	} while (ret == -ERESTARTSYS || ret == -EFAULT);
+	mutex_unlock(&nfc_dev->write_mutex);
+
+	timeout = ESE_CLD_RST_REBOOT_GUARD_TIME_MS;
+	if (ret == 0) { /* success case */
+		ret = cold_reset->status;
+		if (IS_RST_PROT_REQ(arg)) {
+			cold_reset->reset_protection = IS_RST_PROT_EN_REQ(arg);
+			cold_reset->rst_prot_src = IS_RST_PROT_EN_REQ(arg) ?
+								 GET_SRC(arg) :
+								 SRC_NONE;
+			/* wait for reboot guard timer */
+		} else if (wait_event_interruptible_timeout(
+				   cold_reset->read_wq, true,
+				   msecs_to_jiffies(timeout)) == 0) {
+			pr_info("%s: reboot guard timer timeout\n", __func__);
+		}
+	}
+err:
+	mutex_unlock(&nfc_dev->dev_ref_mutex);
+	return ret;
+}
+
+/**
+ * nfc_ese_pwr() - power control for ese
+ * @nfc_dev:    nfc device data structure
+ * @arg:    mode that we want to move to
+ *
+ * Device power control. Depending on the arg value, device moves to
+ * different states, refer common_ese.h for args
+ *
+ * Return: -ENOIOCTLCMD if arg is not supported
+ * 0 if Success(or no issue)
+ * 0 or 1 in case of arg is ESE_POWER_STATE
+ * and error ret code otherwise
+ */
+int nfc_ese_pwr(struct nfc_dev *nfc_dev, unsigned long arg)
+{
+	int ret = 0;
+	struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
+
+	if (arg == ESE_POWER_ON) {
+		/*
+		 * Let's store the NFC VEN pin state
+		 * will check stored value in case of eSE power off request,
+		 * to find out if NFC MW also sent request to set VEN HIGH
+		 * VEN state will remain HIGH if NFC is enabled otherwise
+		 * it will be set as LOW
+		 */
+		nfc_dev->nfc_ven_enabled = gpio_get_value(nfc_gpio->ven);
+		if (!nfc_dev->nfc_ven_enabled) {
+			pr_debug("%s: ese hal service setting ven high\n",
+				 __func__);
+			gpio_set_ven(nfc_dev, 1);
+		} else {
+			pr_debug("%s: ven already high\n", __func__);
+		}
+	} else if (arg == ESE_POWER_OFF) {
+		if (!nfc_dev->nfc_ven_enabled) {
+			pr_debug("%s: nfc not enabled, disabling ven\n",
+				 __func__);
+			gpio_set_ven(nfc_dev, 0);
+		} else {
+			pr_debug("%s: keep ven high as nfc is enabled\n",
+				 __func__);
+		}
+	} else if (arg == ESE_POWER_STATE) {
+		/* eSE get power state */
+		ret = gpio_get_value(nfc_gpio->ven);
+	} else if (IS_CLD_RST_REQ(arg) || IS_RST_PROT_REQ(arg)) {
+		ret = perform_cold_reset_protection(nfc_dev, arg);
+	} else {
+		pr_err("%s: bad arg %lu\n", __func__, arg);
+		ret = -ENOIOCTLCMD;
+	}
+	return ret;
+}
+EXPORT_SYMBOL(nfc_ese_pwr);
+
+#define ESE_LEGACY_INTERFACE
+#ifdef ESE_LEGACY_INTERFACE
+static struct nfc_dev *nfc_dev_legacy;
+
+/******************************************************************************
+ * perform_ese_cold_reset() - It shall be called by others driver(not nfc/ese)
+ * to perform cold reset only
+ * @arg: request of cold reset from other drivers should be ESE_CLD_RST_OTHER
+ *
+ * Returns:- 0 in case of success and negative values in case of failure
+ *****************************************************************************/
+int perform_ese_cold_reset(unsigned long arg)
+{
+	int ret = 0;
+
+	if (nfc_dev_legacy) {
+		if (IS_CLD_RST_REQ(arg) && IS_SRC_OTHER(arg)) {
+			ret = nfc_ese_pwr(nfc_dev_legacy, arg);
+		} else {
+			pr_err("%s: operation not permitted\n", __func__);
+			return -EPERM;
+		}
+	}
+	pr_debug("%s: arg = %d ret = %lu\n", __func__, arg, ret);
+	return ret;
+}
+EXPORT_SYMBOL(perform_ese_cold_reset);
+#endif /* ESE_LEGACY_INTERFACE */
+
+void ese_cold_reset_release(struct nfc_dev *nfc_dev)
+{
+	struct cold_reset *cold_reset = &nfc_dev->cold_reset;
+
+	cold_reset->rsp_pending = false;
+	cold_reset->in_progress = false;
+	if (timer_pending(&cold_reset->timer) == 1)
+		del_timer(&cold_reset->timer);
+}
+
+void common_ese_init(struct nfc_dev *nfc_dev)
+{
+	struct cold_reset *cold_reset = &nfc_dev->cold_reset;
+
+	cold_reset->reset_protection = false;
+	cold_reset->rst_prot_src = SRC_NONE;
+	init_waitqueue_head(&cold_reset->read_wq);
+	ese_cold_reset_release(nfc_dev);
+#ifdef ESE_LEGACY_INTERFACE
+	nfc_dev_legacy = nfc_dev;
+#endif /* ESE_LEGACY_INTERFACE */
+}
+
+void common_ese_exit(struct nfc_dev *nfc_dev)
+{
+#ifdef ESE_LEGACY_INTERFACE
+	nfc_dev_legacy = NULL;
+#endif /* ESE_LEGACY_INTERFACE */
+}

+ 99 - 0
nfc/common_ese.h

@@ -0,0 +1,99 @@
+/******************************************************************************
+ * Copyright (C) 2020-2021 NXP
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ ******************************************************************************/
+#ifndef _COMMON_ESE_H_
+#define _COMMON_ESE_H_
+
+#include "common.h"
+
+/* nci prop msg 1st byte */
+#define NCI_PROP_MSG_GID			0x0F
+#define NCI_PROP_MSG_CMD			(NCI_CMD | NCI_PROP_MSG_GID)
+#define NCI_PROP_MSG_RSP			(NCI_RSP | NCI_PROP_MSG_GID)
+
+/* nci prop msg 2nd byte */
+#define CLD_RST_OID				0x1E
+#define RST_PROT_OID				0x1F
+
+/* nci prop msg 3rd byte */
+#define CLD_RST_PAYLOAD_SIZE			0x00
+#define RST_PROT_PAYLOAD_SIZE			0x01
+
+/* nci prop msg response length */
+#define NCI_PROP_MSG_RSP_LEN			0x04
+
+/* cold reset guard time to allow back to back cold reset after some time */
+#define ESE_CLD_RST_GUARD_TIME_MS		(3000)
+/* guard time to reboot after reset */
+#define ESE_CLD_RST_REBOOT_GUARD_TIME_MS	(50)
+/* sources of reset protection and cold reset */
+enum reset_source {
+	SRC_SPI = 0,
+	SRC_NFC = 0x10,
+	SRC_OTHER = 0x20,
+	SRC_NONE = 0x80,
+};
+
+enum ese_ioctl_request {
+	ESE_POWER_ON = 0,	/* eSE POWER ON */
+	ESE_POWER_OFF,		/* eSE POWER OFF */
+	ESE_POWER_STATE,	/* eSE GET POWER STATE */
+
+	/* ese reset requests from eSE service/hal/driver */
+	ESE_CLD_RST,		/* eSE COLD RESET */
+	ESE_RST_PROT_EN,	/* eSE RESET PROTECTION ENABLE */
+	ESE_RST_PROT_DIS,	/* eSE RESET PROTECTION DISABLE */
+
+	/* similar ese reset requests from nfc service/hal/driver */
+	ESE_CLD_RST_NFC = ESE_CLD_RST | SRC_NFC,
+	ESE_RST_PROT_EN_NFC = ESE_RST_PROT_EN | SRC_NFC,
+	ESE_RST_PROT_DIS_NFC = ESE_RST_PROT_DIS | SRC_NFC,
+
+	/* similar ese reset requests from other service/hal/driver */
+	ESE_CLD_RST_OTHER = ESE_CLD_RST | SRC_OTHER,
+};
+
+#define GET_SRC(arg)			(arg & 0xF0)
+#define IS_SRC(arg, src)		(GET_SRC(arg) == src)
+#define IS_SRC_SPI(arg)			IS_SRC(arg, SRC_SPI)
+#define IS_SRC_NFC(arg)			IS_SRC(arg, SRC_NFC)
+#define IS_SRC_OTHER(arg)		IS_SRC(arg, SRC_OTHER)
+#define IS_SRC_VALID(arg)		(IS_SRC_SPI(arg) || \
+					IS_SRC_NFC(arg) ||  \
+					IS_SRC_OTHER(arg))
+#define IS_SRC_VALID_PROT(arg)		(IS_SRC_SPI(arg) || \
+					IS_SRC_NFC(arg))
+
+#define IS_RST(arg, type)		((arg & 0xF) == type)
+#define IS_CLD_RST_REQ(arg)		IS_RST(arg, ESE_CLD_RST)
+#define IS_RST_PROT_EN_REQ(arg)		IS_RST(arg, ESE_RST_PROT_EN)
+#define IS_RST_PROT_DIS_REQ(arg)	IS_RST(arg, ESE_RST_PROT_DIS)
+#define IS_RST_PROT_REQ(arg)		(IS_RST_PROT_EN_REQ(arg) || \
+					IS_RST_PROT_DIS_REQ(arg))
+/* This macro evaluates to 1 if prop cmd response is received */
+#define IS_PROP_CMD_RSP(buf)		((buf[0] == NCI_PROP_MSG_RSP) && \
+					((buf[1] == CLD_RST_OID) || \
+					(buf[1] == RST_PROT_OID)))
+
+void wakeup_on_prop_rsp(struct nfc_dev *nfc_dev, uint8_t *buf);
+int nfc_ese_pwr(struct nfc_dev *nfc_dev, unsigned long arg);
+void ese_cold_reset_release(struct nfc_dev *nfc_dev);
+void common_ese_init(struct nfc_dev *nfc_dev);
+void common_ese_exit(struct nfc_dev *nfc_dev);
+
+#endif /* _COMMON_ESE_H_ */

+ 554 - 0
nfc/i2c_drv.c

@@ -0,0 +1,554 @@
+/******************************************************************************
+ * Copyright (C) 2015, The Linux Foundation. All rights reserved.
+ * Copyright (C) 2013-2021 NXP
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ ******************************************************************************/
+/*
+ * Copyright (C) 2010 Trusted Logic S.A.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <linux/module.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>
+#include <linux/uaccess.h>
+#include <linux/gpio.h>
+#ifdef CONFIG_COMPAT
+#include <linux/compat.h>
+#endif
+#include "common_ese.h"
+
+/**
+ * i2c_disable_irq()
+ *
+ * Check if interrupt is disabled or not
+ * and disable interrupt
+ *
+ * Return: int
+ */
+int i2c_disable_irq(struct nfc_dev *dev)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&dev->i2c_dev.irq_enabled_lock, flags);
+	if (dev->i2c_dev.irq_enabled) {
+		disable_irq_nosync(dev->i2c_dev.client->irq);
+		dev->i2c_dev.irq_enabled = false;
+	}
+	spin_unlock_irqrestore(&dev->i2c_dev.irq_enabled_lock, flags);
+
+	return 0;
+}
+
+/**
+ * i2c_enable_irq()
+ *
+ * Check if interrupt is enabled or not
+ * and enable interrupt
+ *
+ * Return: int
+ */
+int i2c_enable_irq(struct nfc_dev *dev)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&dev->i2c_dev.irq_enabled_lock, flags);
+	if (!dev->i2c_dev.irq_enabled) {
+		dev->i2c_dev.irq_enabled = true;
+		enable_irq(dev->i2c_dev.client->irq);
+	}
+	spin_unlock_irqrestore(&dev->i2c_dev.irq_enabled_lock, flags);
+
+	return 0;
+}
+
+static irqreturn_t i2c_irq_handler(int irq, void *dev_id)
+{
+	struct nfc_dev *nfc_dev = dev_id;
+	struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev;
+
+	if (device_may_wakeup(&i2c_dev->client->dev))
+		pm_wakeup_event(&i2c_dev->client->dev, WAKEUP_SRC_TIMEOUT);
+
+	i2c_disable_irq(nfc_dev);
+	wake_up(&nfc_dev->read_wq);
+
+	return IRQ_HANDLED;
+}
+
+int i2c_read(struct nfc_dev *nfc_dev, char *buf, size_t count, int timeout)
+{
+	int ret;
+	struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev;
+	struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
+
+	pr_debug("%s: reading %zu bytes.\n", __func__, count);
+
+	if (timeout > NCI_CMD_RSP_TIMEOUT_MS)
+		timeout = NCI_CMD_RSP_TIMEOUT_MS;
+
+	if (count > MAX_NCI_BUFFER_SIZE)
+		count = MAX_NCI_BUFFER_SIZE;
+
+	if (!gpio_get_value(nfc_gpio->irq)) {
+		while (1) {
+			ret = 0;
+			if (!i2c_dev->irq_enabled) {
+				i2c_dev->irq_enabled = true;
+				enable_irq(i2c_dev->client->irq);
+			}
+			if (!gpio_get_value(nfc_gpio->irq)) {
+				if (timeout) {
+					ret = wait_event_interruptible_timeout(
+						nfc_dev->read_wq,
+						!i2c_dev->irq_enabled,
+						msecs_to_jiffies(timeout));
+					if (ret <= 0) {
+						pr_err("%s: timeout error\n",
+						       __func__);
+						goto err;
+					}
+				} else {
+					ret = wait_event_interruptible(
+						nfc_dev->read_wq,
+						!i2c_dev->irq_enabled);
+					if (ret) {
+						pr_err("%s: err wakeup of wq\n",
+						       __func__);
+						goto err;
+					}
+				}
+			}
+			i2c_disable_irq(nfc_dev);
+
+			if (gpio_get_value(nfc_gpio->irq))
+				break;
+			if (!gpio_get_value(nfc_gpio->ven)) {
+				pr_info("%s: releasing read\n", __func__);
+				ret = -EIO;
+				goto err;
+			}
+			/*
+			 * NFC service wanted to close the driver so,
+			 * release the calling reader thread asap.
+			 *
+			 * This can happen in case of nfc node close call from
+			 * eSE HAL in that case the NFC HAL reader thread
+			 * will again call read system call
+			 */
+			if (nfc_dev->release_read) {
+				pr_debug("%s: releasing read\n", __func__);
+				return 0;
+			}
+			pr_warn("%s: spurious interrupt detected\n", __func__);
+		}
+	}
+
+	memset(buf, 0x00, count);
+	/* Read data */
+	ret = i2c_master_recv(nfc_dev->i2c_dev.client, buf, count);
+	if (ret <= 0) {
+		pr_err("%s: returned %d\n", __func__, ret);
+		goto err;
+	}
+	/* check if it's response of cold reset command
+	 * NFC HAL process shouldn't receive this data as
+	 * command was sent by driver
+	 */
+	if (nfc_dev->cold_reset.rsp_pending) {
+		if (IS_PROP_CMD_RSP(buf)) {
+			/* Read data */
+			ret = i2c_master_recv(nfc_dev->i2c_dev.client,
+					      &buf[NCI_PAYLOAD_IDX],
+					      buf[NCI_PAYLOAD_LEN_IDX]);
+			if (ret <= 0) {
+				pr_err("%s: error reading cold rst/prot rsp\n",
+				       __func__);
+				goto err;
+			}
+			wakeup_on_prop_rsp(nfc_dev, buf);
+			/*
+			 * NFC process doesn't know about cold reset command
+			 * being sent as it was initiated by eSE process
+			 * we shouldn't return any data to NFC process
+			 */
+			return 0;
+		}
+	}
+err:
+	return ret;
+}
+
+int i2c_write(struct nfc_dev *nfc_dev, const char *buf, size_t count,
+	      int max_retry_cnt)
+{
+	int ret = -EINVAL;
+	int retry_cnt;
+	struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
+
+	if (count > MAX_DL_BUFFER_SIZE)
+		count = MAX_DL_BUFFER_SIZE;
+
+	pr_debug("%s: writing %zu bytes.\n", __func__, count);
+	/*
+	 * Wait for any pending read for max 15ms before write
+	 * This is to avoid any packet corruption during read, when
+	 * the host cmds resets NFCC during any parallel read operation
+	 */
+	for (retry_cnt = 1; retry_cnt <= MAX_WRITE_IRQ_COUNT; retry_cnt++) {
+		if (gpio_get_value(nfc_gpio->irq)) {
+			pr_warn("%s: irq high during write, wait\n", __func__);
+			usleep_range(NFC_WRITE_IRQ_WAIT_TIME_US,
+				     NFC_WRITE_IRQ_WAIT_TIME_US + 100);
+		} else {
+			break;
+		}
+		if (retry_cnt == MAX_WRITE_IRQ_COUNT &&
+		    gpio_get_value(nfc_gpio->irq)) {
+			pr_warn("%s: allow after maximum wait\n", __func__);
+		}
+	}
+
+	for (retry_cnt = 1; retry_cnt <= max_retry_cnt; retry_cnt++) {
+		ret = i2c_master_send(nfc_dev->i2c_dev.client, buf, count);
+		if (ret <= 0) {
+			pr_warn("%s: write failed ret(%d), maybe in standby\n",
+				__func__, ret);
+			usleep_range(WRITE_RETRY_WAIT_TIME_US,
+				     WRITE_RETRY_WAIT_TIME_US + 100);
+		} else if (ret != count) {
+			pr_err("%s: failed to write %d\n", __func__, ret);
+			ret = -EIO;
+		} else if (ret == count)
+			break;
+	}
+	return ret;
+}
+
+ssize_t nfc_i2c_dev_read(struct file *filp, char __user *buf, size_t count,
+			 loff_t *offset)
+{
+	int ret;
+	struct nfc_dev *nfc_dev = (struct nfc_dev *)filp->private_data;
+
+	if (!nfc_dev) {
+		pr_err("%s: device doesn't exist anymore\n", __func__);
+		return -ENODEV;
+	}
+	mutex_lock(&nfc_dev->read_mutex);
+	if (filp->f_flags & O_NONBLOCK) {
+		ret = i2c_master_recv(nfc_dev->i2c_dev.client, nfc_dev->read_kbuf, count);
+		pr_debug("%s: NONBLOCK read ret = %d\n", __func__, ret);
+	} else {
+		ret = i2c_read(nfc_dev, nfc_dev->read_kbuf, count, 0);
+	}
+	if (ret > 0) {
+		if (copy_to_user(buf, nfc_dev->read_kbuf, ret)) {
+			pr_warn("%s: failed to copy to user space\n", __func__);
+			ret = -EFAULT;
+		}
+	}
+	mutex_unlock(&nfc_dev->read_mutex);
+	return ret;
+}
+
+ssize_t nfc_i2c_dev_write(struct file *filp, const char __user *buf,
+			  size_t count, loff_t *offset)
+{
+	int ret;
+	struct nfc_dev *nfc_dev = (struct nfc_dev *)filp->private_data;
+
+	if (count > MAX_DL_BUFFER_SIZE)
+		count = MAX_DL_BUFFER_SIZE;
+
+	if (!nfc_dev) {
+		pr_err("%s: device doesn't exist anymore\n", __func__);
+		return -ENODEV;
+	}
+
+	mutex_lock(&nfc_dev->write_mutex);
+	if (copy_from_user(nfc_dev->write_kbuf, buf, count)) {
+		pr_err("%s: failed to copy from user space\n", __func__);
+		mutex_unlock(&nfc_dev->write_mutex);
+		return -EFAULT;
+	}
+	ret = i2c_write(nfc_dev, nfc_dev->write_kbuf, count, NO_RETRY);
+	mutex_unlock(&nfc_dev->write_mutex);
+	return ret;
+}
+
+static const struct file_operations nfc_i2c_dev_fops = {
+	.owner = THIS_MODULE,
+	.llseek = no_llseek,
+	.read = nfc_i2c_dev_read,
+	.write = nfc_i2c_dev_write,
+	.open = nfc_dev_open,
+	.flush = nfc_dev_flush,
+	.release = nfc_dev_close,
+	.unlocked_ioctl = nfc_dev_ioctl,
+#ifdef CONFIG_COMPAT
+	.compat_ioctl = nfc_dev_compat_ioctl,
+#endif
+};
+
+int nfc_i2c_dev_probe(struct i2c_client *client, const struct i2c_device_id *id)
+{
+	int ret = 0;
+	struct nfc_dev *nfc_dev = NULL;
+	struct i2c_dev *i2c_dev = NULL;
+	struct platform_configs nfc_configs;
+	struct platform_gpio *nfc_gpio = &nfc_configs.gpio;
+
+	pr_debug("%s: enter\n", __func__);
+	/* retrieve details of gpios from dt */
+	ret = nfc_parse_dt(&client->dev, &nfc_configs, PLATFORM_IF_I2C);
+	if (ret) {
+		pr_err("%s: failed to parse dt\n", __func__);
+		goto err;
+	}
+
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
+		pr_err("%s: need I2C_FUNC_I2C\n", __func__);
+		ret = -ENODEV;
+		goto err;
+	}
+	nfc_dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
+	if (nfc_dev == NULL) {
+		ret = -ENOMEM;
+		goto err;
+	}
+	nfc_dev->read_kbuf = kzalloc(MAX_NCI_BUFFER_SIZE, GFP_DMA | GFP_KERNEL);
+	if (!nfc_dev->read_kbuf) {
+		ret = -ENOMEM;
+		goto err_free_nfc_dev;
+	}
+	nfc_dev->write_kbuf = kzalloc(MAX_DL_BUFFER_SIZE, GFP_DMA | GFP_KERNEL);
+	if (!nfc_dev->write_kbuf) {
+		ret = -ENOMEM;
+		goto err_free_read_kbuf;
+	}
+	nfc_dev->interface = PLATFORM_IF_I2C;
+	nfc_dev->nfc_state = NFC_STATE_NCI;
+	nfc_dev->i2c_dev.client = client;
+	i2c_dev = &nfc_dev->i2c_dev;
+	nfc_dev->nfc_read = i2c_read;
+	nfc_dev->nfc_write = i2c_write;
+	nfc_dev->nfc_enable_intr = i2c_enable_irq;
+	nfc_dev->nfc_disable_intr = i2c_disable_irq;
+	ret = configure_gpio(nfc_gpio->ven, GPIO_OUTPUT);
+	if (ret) {
+		pr_err("%s: unable to request nfc reset gpio [%d]\n", __func__,
+		       nfc_gpio->ven);
+		goto err_free_write_kbuf;
+	}
+	ret = configure_gpio(nfc_gpio->irq, GPIO_IRQ);
+	if (ret <= 0) {
+		pr_err("%s: unable to request nfc irq gpio [%d]\n", __func__,
+		       nfc_gpio->irq);
+		goto err_free_gpio;
+	}
+	client->irq = ret;
+	ret = configure_gpio(nfc_gpio->dwl_req, GPIO_OUTPUT);
+	if (ret) {
+		pr_err("%s: unable to request nfc firm downl gpio [%d]\n",
+		       __func__, nfc_gpio->dwl_req);
+	}
+
+	/* copy the retrieved gpio details from DT */
+	memcpy(&nfc_dev->configs, &nfc_configs,
+	       sizeof(struct platform_configs));
+
+	/* init mutex and queues */
+	init_waitqueue_head(&nfc_dev->read_wq);
+	mutex_init(&nfc_dev->read_mutex);
+	mutex_init(&nfc_dev->write_mutex);
+	mutex_init(&nfc_dev->dev_ref_mutex);
+	spin_lock_init(&i2c_dev->irq_enabled_lock);
+	common_ese_init(nfc_dev);
+	ret = nfc_misc_register(nfc_dev, &nfc_i2c_dev_fops, DEV_COUNT,
+				NFC_CHAR_DEV_NAME, CLASS_NAME);
+	if (ret) {
+		pr_err("%s: nfc_misc_register failed\n", __func__);
+		goto err_mutex_destroy;
+	}
+	/* interrupt initializations */
+	pr_info("%s: requesting IRQ %d\n", __func__, client->irq);
+	i2c_dev->irq_enabled = true;
+	ret = request_irq(client->irq, i2c_irq_handler, IRQF_TRIGGER_HIGH,
+			  client->name, nfc_dev);
+	if (ret) {
+		pr_err("%s: request_irq failed\n", __func__);
+		goto err_nfc_misc_unregister;
+	}
+	i2c_disable_irq(nfc_dev);
+	gpio_set_ven(nfc_dev, 1);
+	gpio_set_ven(nfc_dev, 0);
+	gpio_set_ven(nfc_dev, 1);
+	device_init_wakeup(&client->dev, true);
+	i2c_set_clientdata(client, nfc_dev);
+	i2c_dev->irq_wake_up = false;
+
+	pr_info("%s: probing nfc i2c successfully\n", __func__);
+	return 0;
+err_nfc_misc_unregister:
+	nfc_misc_unregister(nfc_dev, DEV_COUNT);
+err_mutex_destroy:
+	mutex_destroy(&nfc_dev->dev_ref_mutex);
+	mutex_destroy(&nfc_dev->read_mutex);
+	mutex_destroy(&nfc_dev->write_mutex);
+err_free_gpio:
+	gpio_free_all(nfc_dev);
+err_free_write_kbuf:
+	kfree(nfc_dev->write_kbuf);
+err_free_read_kbuf:
+	kfree(nfc_dev->read_kbuf);
+err_free_nfc_dev:
+	kfree(nfc_dev);
+err:
+	pr_err("%s: probing not successful, check hardware\n", __func__);
+	return ret;
+}
+
+int nfc_i2c_dev_remove(struct i2c_client *client)
+{
+	int ret = 0;
+	struct nfc_dev *nfc_dev = NULL;
+
+	pr_info("%s: remove device\n", __func__);
+	nfc_dev = i2c_get_clientdata(client);
+	if (!nfc_dev) {
+		pr_err("%s: device doesn't exist anymore\n", __func__);
+		ret = -ENODEV;
+		return ret;
+	}
+	if (nfc_dev->dev_ref_count > 0) {
+		pr_err("%s: device already in use\n", __func__);
+		return -EBUSY;
+	}
+	device_init_wakeup(&client->dev, false);
+	free_irq(client->irq, nfc_dev);
+	nfc_misc_unregister(nfc_dev, DEV_COUNT);
+	mutex_destroy(&nfc_dev->read_mutex);
+	mutex_destroy(&nfc_dev->write_mutex);
+	gpio_free_all(nfc_dev);
+	kfree(nfc_dev->read_kbuf);
+	kfree(nfc_dev->write_kbuf);
+	kfree(nfc_dev);
+	return ret;
+}
+
+int nfc_i2c_dev_suspend(struct device *device)
+{
+	struct i2c_client *client = to_i2c_client(device);
+	struct nfc_dev *nfc_dev = i2c_get_clientdata(client);
+	struct i2c_dev *i2c_dev = NULL;
+	if (!nfc_dev) {
+		pr_err("%s: device doesn't exist anymore\n", __func__);
+		return -ENODEV;
+	}
+	i2c_dev = &nfc_dev->i2c_dev;
+
+	if (device_may_wakeup(&client->dev) && i2c_dev->irq_enabled) {
+		if (!enable_irq_wake(client->irq))
+			i2c_dev->irq_wake_up = true;
+	}
+	pr_debug("%s: irq_wake_up = %d", __func__, i2c_dev->irq_wake_up);
+	return 0;
+}
+
+int nfc_i2c_dev_resume(struct device *device)
+{
+	struct i2c_client *client = to_i2c_client(device);
+	struct nfc_dev *nfc_dev = i2c_get_clientdata(client);
+	struct i2c_dev *i2c_dev = NULL;
+	if (!nfc_dev) {
+		pr_err("%s: device doesn't exist anymore\n", __func__);
+		return -ENODEV;
+	}
+	i2c_dev = &nfc_dev->i2c_dev;
+
+	if (device_may_wakeup(&client->dev) && i2c_dev->irq_wake_up) {
+		if (!disable_irq_wake(client->irq))
+			i2c_dev->irq_wake_up = false;
+	}
+	pr_debug("%s: irq_wake_up = %d", __func__, i2c_dev->irq_wake_up);
+	return 0;
+}
+
+static const struct i2c_device_id nfc_i2c_dev_id[] = { { NFC_I2C_DEV_ID, 0 },
+						       {} };
+
+static const struct of_device_id nfc_i2c_dev_match_table[] = {
+	{
+		.compatible = NFC_I2C_DRV_STR,
+	},
+	{}
+};
+
+static const struct dev_pm_ops nfc_i2c_dev_pm_ops = { SET_SYSTEM_SLEEP_PM_OPS(
+	nfc_i2c_dev_suspend, nfc_i2c_dev_resume) };
+
+static struct i2c_driver nfc_i2c_dev_driver = {
+	.id_table = nfc_i2c_dev_id,
+	.probe = nfc_i2c_dev_probe,
+	.remove = nfc_i2c_dev_remove,
+	.driver = {
+		.name = NFC_I2C_DRV_STR,
+		.pm = &nfc_i2c_dev_pm_ops,
+		.of_match_table = nfc_i2c_dev_match_table,
+		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
+	},
+};
+
+MODULE_DEVICE_TABLE(of, nfc_i2c_dev_match_table);
+
+static int __init nfc_i2c_dev_init(void)
+{
+	int ret = 0;
+
+	pr_info("%s: Loading NXP NFC I2C driver\n", __func__);
+	ret = i2c_add_driver(&nfc_i2c_dev_driver);
+	if (ret != 0)
+		pr_err("%s: NFC I2C add driver error ret %d\n", __func__, ret);
+	return ret;
+}
+
+module_init(nfc_i2c_dev_init);
+
+static void __exit nfc_i2c_dev_exit(void)
+{
+	pr_info("%s: Unloading NXP NFC I2C driver\n", __func__);
+	i2c_del_driver(&nfc_i2c_dev_driver);
+}
+
+module_exit(nfc_i2c_dev_exit);
+
+MODULE_DESCRIPTION("NXP NFC I2C driver");
+MODULE_LICENSE("GPL");

+ 46 - 0
nfc/i2c_drv.h

@@ -0,0 +1,46 @@
+/******************************************************************************
+ * Copyright (C) 2015, The Linux Foundation. All rights reserved.
+ * Copyright (C) 2019-2021 NXP
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ ******************************************************************************/
+#ifndef _I2C_DRV_H_
+#define _I2C_DRV_H_
+
+#include <linux/i2c.h>
+
+/* kept same as dts */
+#define NFC_I2C_DRV_STR			"nxp,pn544"
+#define NFC_I2C_DEV_ID			"pn553"
+
+/* Interface specific parameters */
+struct i2c_dev {
+	struct i2c_client *client;
+	/* IRQ parameters */
+	bool irq_enabled;
+	spinlock_t irq_enabled_lock;
+	/* NFC_IRQ wake-up state */
+	bool irq_wake_up;
+};
+
+long nfc_i2c_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg);
+int nfc_i2c_dev_probe(struct i2c_client *client,
+		      const struct i2c_device_id *id);
+int nfc_i2c_dev_remove(struct i2c_client *client);
+int nfc_i2c_dev_suspend(struct device *device);
+int nfc_i2c_dev_resume(struct device *device);
+
+#endif /* _I2C_DRV_H_ */