Re: [RFC 4/4] NFC: pn533: add I2C phy driver

2016-05-04 Thread Olliver Schinagl

Hi Michael,

I know this is an old patch already applied, but I noticed the following:

On 25-03-16 15:46, Michael Thalmeier wrote:

This adds the I2C phy interface for the pn533 driver. This way the driver can
be used to interact with I2C connected pn532.

Signed-off-by: Michael Thalmeier 
---
  drivers/nfc/pn533/Kconfig  |  11 ++
  drivers/nfc/pn533/Makefile |   2 +
  drivers/nfc/pn533/i2c.c| 277 +
  drivers/nfc/pn533/pn533.c  |  29 +
  drivers/nfc/pn533/pn533.h  |   2 +
  5 files changed, 321 insertions(+)
  create mode 100644 drivers/nfc/pn533/i2c.c

diff --git a/drivers/nfc/pn533/Kconfig b/drivers/nfc/pn533/Kconfig
index b5a926e..d94122d 100644
--- a/drivers/nfc/pn533/Kconfig
+++ b/drivers/nfc/pn533/Kconfig
@@ -14,3 +14,14 @@ config NFC_PN533_USB

  If you choose to build a module, it'll be called pn533_usb.
  Say N if unsure.
+
+config NFC_PN533_I2C
+   tristate "NFC PN533 device support (I2C)"
+   depends on I2C
+   select NFC_PN533
+   ---help---
+ This module adds support for the NXP pn533 I2C interface.
+ Select this if your platform is using the I2C bus.
+
+ If you choose to build a module, it'll be called pn533_i2c.
+ Say N if unsure.
diff --git a/drivers/nfc/pn533/Makefile b/drivers/nfc/pn533/Makefile
index 12c6be4..51d24c6 100644
--- a/drivers/nfc/pn533/Makefile
+++ b/drivers/nfc/pn533/Makefile
@@ -2,6 +2,8 @@
  # Makefile for PN533 NFC driver
  #
  pn533_usb-objs  = usb.o
+pn533_i2c-objs  = i2c.o

  obj-$(CONFIG_NFC_PN533) += pn533.o
  obj-$(CONFIG_NFC_PN533_USB) += pn533_usb.o
+obj-$(CONFIG_NFC_PN533_I2C) += pn533_i2c.o
diff --git a/drivers/nfc/pn533/i2c.c b/drivers/nfc/pn533/i2c.c
new file mode 100644
index 000..35066b30
--- /dev/null
+++ b/drivers/nfc/pn533/i2c.c
@@ -0,0 +1,277 @@
+/*
+ * Driver for NXP PN533 NFC Chip - I2C transport layer
+ *
+ * Copyright (C) 2011 Instituto Nokia de Tecnologia
+ * Copyright (C) 2012-2013 Tieto Poland
+ * Copyright (C) 2016 HALE electronic
+ *
+ *
+ * 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, see .
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "pn533.h"
+
+#define VERSION "0.1"
+
+#define PN533_I2C_DRIVER_NAME "pn533_i2c"
+
+struct pn533_i2c_phy {
+   struct i2c_client *i2c_dev;
+   struct pn533 *priv;
+
+   int hard_fault; /*
+* < 0 if hardware error occured (e.g. i2c err)
+* and prevents normal operation.
+*/
+};
+
+static int pn533_i2c_send_ack(struct pn533 *dev, gfp_t flags)
+{
+   struct pn533_i2c_phy *phy = dev->phy;
+   struct i2c_client *client = phy->i2c_dev;
+   u8 ack[6] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
+   /* spec 6.2.1.3:  Preamble, SoPC (2), ACK Code (2), Postamble */
+   int rc;
+
+   rc = i2c_master_send(client, ack, 6);
+
+   return rc;
+}
+
+static int pn533_i2c_send_frame(struct pn533 *dev,
+   struct sk_buff *out)
+{
+   struct pn533_i2c_phy *phy = dev->phy;
+   struct i2c_client *client = phy->i2c_dev;
+   int rc;
+
+   if (phy->hard_fault != 0)
+   return phy->hard_fault;
+
+   if (phy->priv == NULL)
+   phy->priv = dev;
+
+   print_hex_dump_debug("PN533_i2c TX: ", DUMP_PREFIX_NONE, 16, 1,
+out->data, out->len, false);
+
+   rc = i2c_master_send(client, out->data, out->len);
+
+   if (rc == -EREMOTEIO) { /* Retry, chip was in power down */
+   usleep_range(6000, 1);
+   rc = i2c_master_send(client, out->data, out->len);
+   }
+
+   if (rc >= 0) {
+   if (rc != out->len)
+   rc = -EREMOTEIO;
+   else
+   rc = 0;
+   }
+
+   return rc;
+}
+
+static void pn533_i2c_abort_cmd(struct pn533 *dev, gfp_t flags)
+{
+   /* An ack will cancel the last issued command */
+   pn533_i2c_send_ack(dev, flags);
+
+   /* schedule cmd_complete_work to finish current command execution */
+   if (dev->cmd != NULL)
+   dev->cmd->status = -ENOENT;
+   queue_work(dev->wq, >cmd_complete_work);
+}
+
+static int pn533_i2c_read(struct pn533_i2c_phy *phy, 

Re: [RFC 4/4] NFC: pn533: add I2C phy driver

2016-05-04 Thread Olliver Schinagl

Hi Michael,

I know this is an old patch already applied, but I noticed the following:

On 25-03-16 15:46, Michael Thalmeier wrote:

This adds the I2C phy interface for the pn533 driver. This way the driver can
be used to interact with I2C connected pn532.

Signed-off-by: Michael Thalmeier 
---
  drivers/nfc/pn533/Kconfig  |  11 ++
  drivers/nfc/pn533/Makefile |   2 +
  drivers/nfc/pn533/i2c.c| 277 +
  drivers/nfc/pn533/pn533.c  |  29 +
  drivers/nfc/pn533/pn533.h  |   2 +
  5 files changed, 321 insertions(+)
  create mode 100644 drivers/nfc/pn533/i2c.c

diff --git a/drivers/nfc/pn533/Kconfig b/drivers/nfc/pn533/Kconfig
index b5a926e..d94122d 100644
--- a/drivers/nfc/pn533/Kconfig
+++ b/drivers/nfc/pn533/Kconfig
@@ -14,3 +14,14 @@ config NFC_PN533_USB

  If you choose to build a module, it'll be called pn533_usb.
  Say N if unsure.
+
+config NFC_PN533_I2C
+   tristate "NFC PN533 device support (I2C)"
+   depends on I2C
+   select NFC_PN533
+   ---help---
+ This module adds support for the NXP pn533 I2C interface.
+ Select this if your platform is using the I2C bus.
+
+ If you choose to build a module, it'll be called pn533_i2c.
+ Say N if unsure.
diff --git a/drivers/nfc/pn533/Makefile b/drivers/nfc/pn533/Makefile
index 12c6be4..51d24c6 100644
--- a/drivers/nfc/pn533/Makefile
+++ b/drivers/nfc/pn533/Makefile
@@ -2,6 +2,8 @@
  # Makefile for PN533 NFC driver
  #
  pn533_usb-objs  = usb.o
+pn533_i2c-objs  = i2c.o

  obj-$(CONFIG_NFC_PN533) += pn533.o
  obj-$(CONFIG_NFC_PN533_USB) += pn533_usb.o
+obj-$(CONFIG_NFC_PN533_I2C) += pn533_i2c.o
diff --git a/drivers/nfc/pn533/i2c.c b/drivers/nfc/pn533/i2c.c
new file mode 100644
index 000..35066b30
--- /dev/null
+++ b/drivers/nfc/pn533/i2c.c
@@ -0,0 +1,277 @@
+/*
+ * Driver for NXP PN533 NFC Chip - I2C transport layer
+ *
+ * Copyright (C) 2011 Instituto Nokia de Tecnologia
+ * Copyright (C) 2012-2013 Tieto Poland
+ * Copyright (C) 2016 HALE electronic
+ *
+ *
+ * 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, see .
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "pn533.h"
+
+#define VERSION "0.1"
+
+#define PN533_I2C_DRIVER_NAME "pn533_i2c"
+
+struct pn533_i2c_phy {
+   struct i2c_client *i2c_dev;
+   struct pn533 *priv;
+
+   int hard_fault; /*
+* < 0 if hardware error occured (e.g. i2c err)
+* and prevents normal operation.
+*/
+};
+
+static int pn533_i2c_send_ack(struct pn533 *dev, gfp_t flags)
+{
+   struct pn533_i2c_phy *phy = dev->phy;
+   struct i2c_client *client = phy->i2c_dev;
+   u8 ack[6] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
+   /* spec 6.2.1.3:  Preamble, SoPC (2), ACK Code (2), Postamble */
+   int rc;
+
+   rc = i2c_master_send(client, ack, 6);
+
+   return rc;
+}
+
+static int pn533_i2c_send_frame(struct pn533 *dev,
+   struct sk_buff *out)
+{
+   struct pn533_i2c_phy *phy = dev->phy;
+   struct i2c_client *client = phy->i2c_dev;
+   int rc;
+
+   if (phy->hard_fault != 0)
+   return phy->hard_fault;
+
+   if (phy->priv == NULL)
+   phy->priv = dev;
+
+   print_hex_dump_debug("PN533_i2c TX: ", DUMP_PREFIX_NONE, 16, 1,
+out->data, out->len, false);
+
+   rc = i2c_master_send(client, out->data, out->len);
+
+   if (rc == -EREMOTEIO) { /* Retry, chip was in power down */
+   usleep_range(6000, 1);
+   rc = i2c_master_send(client, out->data, out->len);
+   }
+
+   if (rc >= 0) {
+   if (rc != out->len)
+   rc = -EREMOTEIO;
+   else
+   rc = 0;
+   }
+
+   return rc;
+}
+
+static void pn533_i2c_abort_cmd(struct pn533 *dev, gfp_t flags)
+{
+   /* An ack will cancel the last issued command */
+   pn533_i2c_send_ack(dev, flags);
+
+   /* schedule cmd_complete_work to finish current command execution */
+   if (dev->cmd != NULL)
+   dev->cmd->status = -ENOENT;
+   queue_work(dev->wq, >cmd_complete_work);
+}
+
+static int pn533_i2c_read(struct pn533_i2c_phy *phy, struct sk_buff **skb)
+{
+   

Re: [RFC 4/4] NFC: pn533: add I2C phy driver

2016-04-09 Thread Samuel Ortiz
Hi Michael,

On Fri, Mar 25, 2016 at 03:46:54PM +0100, Michael Thalmeier wrote:
> This adds the I2C phy interface for the pn533 driver. This way the driver can
> be used to interact with I2C connected pn532.
> 
> Signed-off-by: Michael Thalmeier 
> ---
>  drivers/nfc/pn533/Kconfig  |  11 ++
>  drivers/nfc/pn533/Makefile |   2 +
>  drivers/nfc/pn533/i2c.c| 277 
> +
>  drivers/nfc/pn533/pn533.c  |  29 +
>  drivers/nfc/pn533/pn533.h  |   2 +
>  5 files changed, 321 insertions(+)
>  create mode 100644 drivers/nfc/pn533/i2c.c
> 
> diff --git a/drivers/nfc/pn533/Kconfig b/drivers/nfc/pn533/Kconfig
> index b5a926e..d94122d 100644
> --- a/drivers/nfc/pn533/Kconfig
> +++ b/drivers/nfc/pn533/Kconfig
> @@ -14,3 +14,14 @@ config NFC_PN533_USB
>  
> If you choose to build a module, it'll be called pn533_usb.
> Say N if unsure.
> +
> +config NFC_PN533_I2C
> + tristate "NFC PN533 device support (I2C)"
> + depends on I2C
> + select NFC_PN533
> + ---help---
> +   This module adds support for the NXP pn533 I2C interface.
> +   Select this if your platform is using the I2C bus.
> +
> +   If you choose to build a module, it'll be called pn533_i2c.
> +   Say N if unsure.
> diff --git a/drivers/nfc/pn533/Makefile b/drivers/nfc/pn533/Makefile
> index 12c6be4..51d24c6 100644
> --- a/drivers/nfc/pn533/Makefile
> +++ b/drivers/nfc/pn533/Makefile
> @@ -2,6 +2,8 @@
>  # Makefile for PN533 NFC driver
>  #
>  pn533_usb-objs  = usb.o
> +pn533_i2c-objs  = i2c.o
>  
>  obj-$(CONFIG_NFC_PN533) += pn533.o
>  obj-$(CONFIG_NFC_PN533_USB) += pn533_usb.o
> +obj-$(CONFIG_NFC_PN533_I2C) += pn533_i2c.o
> diff --git a/drivers/nfc/pn533/i2c.c b/drivers/nfc/pn533/i2c.c
> new file mode 100644
> index 000..35066b30
> --- /dev/null
> +++ b/drivers/nfc/pn533/i2c.c
> @@ -0,0 +1,277 @@
> +/*
> + * Driver for NXP PN533 NFC Chip - I2C transport layer
> + *
> + * Copyright (C) 2011 Instituto Nokia de Tecnologia
> + * Copyright (C) 2012-2013 Tieto Poland
> + * Copyright (C) 2016 HALE electronic
> + *
> + *
> + * 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, see .
> + */
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include "pn533.h"
> +
> +#define VERSION "0.1"
> +
> +#define PN533_I2C_DRIVER_NAME "pn533_i2c"
> +
> +struct pn533_i2c_phy {
> + struct i2c_client *i2c_dev;
> + struct pn533 *priv;
> +
> + int hard_fault; /*
> +  * < 0 if hardware error occured (e.g. i2c err)
> +  * and prevents normal operation.
> +  */
> +};
> +
> +static int pn533_i2c_send_ack(struct pn533 *dev, gfp_t flags)
> +{
> + struct pn533_i2c_phy *phy = dev->phy;
> + struct i2c_client *client = phy->i2c_dev;
> + u8 ack[6] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
> + /* spec 6.2.1.3:  Preamble, SoPC (2), ACK Code (2), Postamble */
> + int rc;
> +
> + rc = i2c_master_send(client, ack, 6);
> +
> + return rc;
> +}
> +
> +static int pn533_i2c_send_frame(struct pn533 *dev,
> + struct sk_buff *out)
> +{
> + struct pn533_i2c_phy *phy = dev->phy;
> + struct i2c_client *client = phy->i2c_dev;
> + int rc;
> +
> + if (phy->hard_fault != 0)
> + return phy->hard_fault;
> +
> + if (phy->priv == NULL)
> + phy->priv = dev;
> +
> + print_hex_dump_debug("PN533_i2c TX: ", DUMP_PREFIX_NONE, 16, 1,
> +  out->data, out->len, false);
> +
> + rc = i2c_master_send(client, out->data, out->len);
> +
> + if (rc == -EREMOTEIO) { /* Retry, chip was in power down */
> + usleep_range(6000, 1);
> + rc = i2c_master_send(client, out->data, out->len);
> + }
> +
> + if (rc >= 0) {
> + if (rc != out->len)
> + rc = -EREMOTEIO;
> + else
> + rc = 0;
> + }
> +
> + return rc;
> +}
> +
> +static void pn533_i2c_abort_cmd(struct pn533 *dev, gfp_t flags)
> +{
> + /* An ack will cancel the last issued command */
> + pn533_i2c_send_ack(dev, flags);
> +
> + /* schedule cmd_complete_work to finish current command execution */
> + if (dev->cmd != NULL)
> +

Re: [RFC 4/4] NFC: pn533: add I2C phy driver

2016-04-09 Thread Samuel Ortiz
Hi Michael,

On Fri, Mar 25, 2016 at 03:46:54PM +0100, Michael Thalmeier wrote:
> This adds the I2C phy interface for the pn533 driver. This way the driver can
> be used to interact with I2C connected pn532.
> 
> Signed-off-by: Michael Thalmeier 
> ---
>  drivers/nfc/pn533/Kconfig  |  11 ++
>  drivers/nfc/pn533/Makefile |   2 +
>  drivers/nfc/pn533/i2c.c| 277 
> +
>  drivers/nfc/pn533/pn533.c  |  29 +
>  drivers/nfc/pn533/pn533.h  |   2 +
>  5 files changed, 321 insertions(+)
>  create mode 100644 drivers/nfc/pn533/i2c.c
> 
> diff --git a/drivers/nfc/pn533/Kconfig b/drivers/nfc/pn533/Kconfig
> index b5a926e..d94122d 100644
> --- a/drivers/nfc/pn533/Kconfig
> +++ b/drivers/nfc/pn533/Kconfig
> @@ -14,3 +14,14 @@ config NFC_PN533_USB
>  
> If you choose to build a module, it'll be called pn533_usb.
> Say N if unsure.
> +
> +config NFC_PN533_I2C
> + tristate "NFC PN533 device support (I2C)"
> + depends on I2C
> + select NFC_PN533
> + ---help---
> +   This module adds support for the NXP pn533 I2C interface.
> +   Select this if your platform is using the I2C bus.
> +
> +   If you choose to build a module, it'll be called pn533_i2c.
> +   Say N if unsure.
> diff --git a/drivers/nfc/pn533/Makefile b/drivers/nfc/pn533/Makefile
> index 12c6be4..51d24c6 100644
> --- a/drivers/nfc/pn533/Makefile
> +++ b/drivers/nfc/pn533/Makefile
> @@ -2,6 +2,8 @@
>  # Makefile for PN533 NFC driver
>  #
>  pn533_usb-objs  = usb.o
> +pn533_i2c-objs  = i2c.o
>  
>  obj-$(CONFIG_NFC_PN533) += pn533.o
>  obj-$(CONFIG_NFC_PN533_USB) += pn533_usb.o
> +obj-$(CONFIG_NFC_PN533_I2C) += pn533_i2c.o
> diff --git a/drivers/nfc/pn533/i2c.c b/drivers/nfc/pn533/i2c.c
> new file mode 100644
> index 000..35066b30
> --- /dev/null
> +++ b/drivers/nfc/pn533/i2c.c
> @@ -0,0 +1,277 @@
> +/*
> + * Driver for NXP PN533 NFC Chip - I2C transport layer
> + *
> + * Copyright (C) 2011 Instituto Nokia de Tecnologia
> + * Copyright (C) 2012-2013 Tieto Poland
> + * Copyright (C) 2016 HALE electronic
> + *
> + *
> + * 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, see .
> + */
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include "pn533.h"
> +
> +#define VERSION "0.1"
> +
> +#define PN533_I2C_DRIVER_NAME "pn533_i2c"
> +
> +struct pn533_i2c_phy {
> + struct i2c_client *i2c_dev;
> + struct pn533 *priv;
> +
> + int hard_fault; /*
> +  * < 0 if hardware error occured (e.g. i2c err)
> +  * and prevents normal operation.
> +  */
> +};
> +
> +static int pn533_i2c_send_ack(struct pn533 *dev, gfp_t flags)
> +{
> + struct pn533_i2c_phy *phy = dev->phy;
> + struct i2c_client *client = phy->i2c_dev;
> + u8 ack[6] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
> + /* spec 6.2.1.3:  Preamble, SoPC (2), ACK Code (2), Postamble */
> + int rc;
> +
> + rc = i2c_master_send(client, ack, 6);
> +
> + return rc;
> +}
> +
> +static int pn533_i2c_send_frame(struct pn533 *dev,
> + struct sk_buff *out)
> +{
> + struct pn533_i2c_phy *phy = dev->phy;
> + struct i2c_client *client = phy->i2c_dev;
> + int rc;
> +
> + if (phy->hard_fault != 0)
> + return phy->hard_fault;
> +
> + if (phy->priv == NULL)
> + phy->priv = dev;
> +
> + print_hex_dump_debug("PN533_i2c TX: ", DUMP_PREFIX_NONE, 16, 1,
> +  out->data, out->len, false);
> +
> + rc = i2c_master_send(client, out->data, out->len);
> +
> + if (rc == -EREMOTEIO) { /* Retry, chip was in power down */
> + usleep_range(6000, 1);
> + rc = i2c_master_send(client, out->data, out->len);
> + }
> +
> + if (rc >= 0) {
> + if (rc != out->len)
> + rc = -EREMOTEIO;
> + else
> + rc = 0;
> + }
> +
> + return rc;
> +}
> +
> +static void pn533_i2c_abort_cmd(struct pn533 *dev, gfp_t flags)
> +{
> + /* An ack will cancel the last issued command */
> + pn533_i2c_send_ack(dev, flags);
> +
> + /* schedule cmd_complete_work to finish current command execution */
> + if (dev->cmd != NULL)
> + dev->cmd->status = 

[RFC 4/4] NFC: pn533: add I2C phy driver

2016-03-25 Thread Michael Thalmeier
This adds the I2C phy interface for the pn533 driver. This way the driver can
be used to interact with I2C connected pn532.

Signed-off-by: Michael Thalmeier 
---
 drivers/nfc/pn533/Kconfig  |  11 ++
 drivers/nfc/pn533/Makefile |   2 +
 drivers/nfc/pn533/i2c.c| 277 +
 drivers/nfc/pn533/pn533.c  |  29 +
 drivers/nfc/pn533/pn533.h  |   2 +
 5 files changed, 321 insertions(+)
 create mode 100644 drivers/nfc/pn533/i2c.c

diff --git a/drivers/nfc/pn533/Kconfig b/drivers/nfc/pn533/Kconfig
index b5a926e..d94122d 100644
--- a/drivers/nfc/pn533/Kconfig
+++ b/drivers/nfc/pn533/Kconfig
@@ -14,3 +14,14 @@ config NFC_PN533_USB
 
  If you choose to build a module, it'll be called pn533_usb.
  Say N if unsure.
+
+config NFC_PN533_I2C
+   tristate "NFC PN533 device support (I2C)"
+   depends on I2C
+   select NFC_PN533
+   ---help---
+ This module adds support for the NXP pn533 I2C interface.
+ Select this if your platform is using the I2C bus.
+
+ If you choose to build a module, it'll be called pn533_i2c.
+ Say N if unsure.
diff --git a/drivers/nfc/pn533/Makefile b/drivers/nfc/pn533/Makefile
index 12c6be4..51d24c6 100644
--- a/drivers/nfc/pn533/Makefile
+++ b/drivers/nfc/pn533/Makefile
@@ -2,6 +2,8 @@
 # Makefile for PN533 NFC driver
 #
 pn533_usb-objs  = usb.o
+pn533_i2c-objs  = i2c.o
 
 obj-$(CONFIG_NFC_PN533) += pn533.o
 obj-$(CONFIG_NFC_PN533_USB) += pn533_usb.o
+obj-$(CONFIG_NFC_PN533_I2C) += pn533_i2c.o
diff --git a/drivers/nfc/pn533/i2c.c b/drivers/nfc/pn533/i2c.c
new file mode 100644
index 000..35066b30
--- /dev/null
+++ b/drivers/nfc/pn533/i2c.c
@@ -0,0 +1,277 @@
+/*
+ * Driver for NXP PN533 NFC Chip - I2C transport layer
+ *
+ * Copyright (C) 2011 Instituto Nokia de Tecnologia
+ * Copyright (C) 2012-2013 Tieto Poland
+ * Copyright (C) 2016 HALE electronic
+ *
+ *
+ * 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, see .
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "pn533.h"
+
+#define VERSION "0.1"
+
+#define PN533_I2C_DRIVER_NAME "pn533_i2c"
+
+struct pn533_i2c_phy {
+   struct i2c_client *i2c_dev;
+   struct pn533 *priv;
+
+   int hard_fault; /*
+* < 0 if hardware error occured (e.g. i2c err)
+* and prevents normal operation.
+*/
+};
+
+static int pn533_i2c_send_ack(struct pn533 *dev, gfp_t flags)
+{
+   struct pn533_i2c_phy *phy = dev->phy;
+   struct i2c_client *client = phy->i2c_dev;
+   u8 ack[6] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
+   /* spec 6.2.1.3:  Preamble, SoPC (2), ACK Code (2), Postamble */
+   int rc;
+
+   rc = i2c_master_send(client, ack, 6);
+
+   return rc;
+}
+
+static int pn533_i2c_send_frame(struct pn533 *dev,
+   struct sk_buff *out)
+{
+   struct pn533_i2c_phy *phy = dev->phy;
+   struct i2c_client *client = phy->i2c_dev;
+   int rc;
+
+   if (phy->hard_fault != 0)
+   return phy->hard_fault;
+
+   if (phy->priv == NULL)
+   phy->priv = dev;
+
+   print_hex_dump_debug("PN533_i2c TX: ", DUMP_PREFIX_NONE, 16, 1,
+out->data, out->len, false);
+
+   rc = i2c_master_send(client, out->data, out->len);
+
+   if (rc == -EREMOTEIO) { /* Retry, chip was in power down */
+   usleep_range(6000, 1);
+   rc = i2c_master_send(client, out->data, out->len);
+   }
+
+   if (rc >= 0) {
+   if (rc != out->len)
+   rc = -EREMOTEIO;
+   else
+   rc = 0;
+   }
+
+   return rc;
+}
+
+static void pn533_i2c_abort_cmd(struct pn533 *dev, gfp_t flags)
+{
+   /* An ack will cancel the last issued command */
+   pn533_i2c_send_ack(dev, flags);
+
+   /* schedule cmd_complete_work to finish current command execution */
+   if (dev->cmd != NULL)
+   dev->cmd->status = -ENOENT;
+   queue_work(dev->wq, >cmd_complete_work);
+}
+
+static int pn533_i2c_read(struct pn533_i2c_phy *phy, struct sk_buff **skb)
+{
+   struct i2c_client *client = phy->i2c_dev;
+   int len = PN533_EXT_FRAME_HEADER_LEN +
+ 

[RFC 4/4] NFC: pn533: add I2C phy driver

2016-03-25 Thread Michael Thalmeier
This adds the I2C phy interface for the pn533 driver. This way the driver can
be used to interact with I2C connected pn532.

Signed-off-by: Michael Thalmeier 
---
 drivers/nfc/pn533/Kconfig  |  11 ++
 drivers/nfc/pn533/Makefile |   2 +
 drivers/nfc/pn533/i2c.c| 277 +
 drivers/nfc/pn533/pn533.c  |  29 +
 drivers/nfc/pn533/pn533.h  |   2 +
 5 files changed, 321 insertions(+)
 create mode 100644 drivers/nfc/pn533/i2c.c

diff --git a/drivers/nfc/pn533/Kconfig b/drivers/nfc/pn533/Kconfig
index b5a926e..d94122d 100644
--- a/drivers/nfc/pn533/Kconfig
+++ b/drivers/nfc/pn533/Kconfig
@@ -14,3 +14,14 @@ config NFC_PN533_USB
 
  If you choose to build a module, it'll be called pn533_usb.
  Say N if unsure.
+
+config NFC_PN533_I2C
+   tristate "NFC PN533 device support (I2C)"
+   depends on I2C
+   select NFC_PN533
+   ---help---
+ This module adds support for the NXP pn533 I2C interface.
+ Select this if your platform is using the I2C bus.
+
+ If you choose to build a module, it'll be called pn533_i2c.
+ Say N if unsure.
diff --git a/drivers/nfc/pn533/Makefile b/drivers/nfc/pn533/Makefile
index 12c6be4..51d24c6 100644
--- a/drivers/nfc/pn533/Makefile
+++ b/drivers/nfc/pn533/Makefile
@@ -2,6 +2,8 @@
 # Makefile for PN533 NFC driver
 #
 pn533_usb-objs  = usb.o
+pn533_i2c-objs  = i2c.o
 
 obj-$(CONFIG_NFC_PN533) += pn533.o
 obj-$(CONFIG_NFC_PN533_USB) += pn533_usb.o
+obj-$(CONFIG_NFC_PN533_I2C) += pn533_i2c.o
diff --git a/drivers/nfc/pn533/i2c.c b/drivers/nfc/pn533/i2c.c
new file mode 100644
index 000..35066b30
--- /dev/null
+++ b/drivers/nfc/pn533/i2c.c
@@ -0,0 +1,277 @@
+/*
+ * Driver for NXP PN533 NFC Chip - I2C transport layer
+ *
+ * Copyright (C) 2011 Instituto Nokia de Tecnologia
+ * Copyright (C) 2012-2013 Tieto Poland
+ * Copyright (C) 2016 HALE electronic
+ *
+ *
+ * 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, see .
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "pn533.h"
+
+#define VERSION "0.1"
+
+#define PN533_I2C_DRIVER_NAME "pn533_i2c"
+
+struct pn533_i2c_phy {
+   struct i2c_client *i2c_dev;
+   struct pn533 *priv;
+
+   int hard_fault; /*
+* < 0 if hardware error occured (e.g. i2c err)
+* and prevents normal operation.
+*/
+};
+
+static int pn533_i2c_send_ack(struct pn533 *dev, gfp_t flags)
+{
+   struct pn533_i2c_phy *phy = dev->phy;
+   struct i2c_client *client = phy->i2c_dev;
+   u8 ack[6] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
+   /* spec 6.2.1.3:  Preamble, SoPC (2), ACK Code (2), Postamble */
+   int rc;
+
+   rc = i2c_master_send(client, ack, 6);
+
+   return rc;
+}
+
+static int pn533_i2c_send_frame(struct pn533 *dev,
+   struct sk_buff *out)
+{
+   struct pn533_i2c_phy *phy = dev->phy;
+   struct i2c_client *client = phy->i2c_dev;
+   int rc;
+
+   if (phy->hard_fault != 0)
+   return phy->hard_fault;
+
+   if (phy->priv == NULL)
+   phy->priv = dev;
+
+   print_hex_dump_debug("PN533_i2c TX: ", DUMP_PREFIX_NONE, 16, 1,
+out->data, out->len, false);
+
+   rc = i2c_master_send(client, out->data, out->len);
+
+   if (rc == -EREMOTEIO) { /* Retry, chip was in power down */
+   usleep_range(6000, 1);
+   rc = i2c_master_send(client, out->data, out->len);
+   }
+
+   if (rc >= 0) {
+   if (rc != out->len)
+   rc = -EREMOTEIO;
+   else
+   rc = 0;
+   }
+
+   return rc;
+}
+
+static void pn533_i2c_abort_cmd(struct pn533 *dev, gfp_t flags)
+{
+   /* An ack will cancel the last issued command */
+   pn533_i2c_send_ack(dev, flags);
+
+   /* schedule cmd_complete_work to finish current command execution */
+   if (dev->cmd != NULL)
+   dev->cmd->status = -ENOENT;
+   queue_work(dev->wq, >cmd_complete_work);
+}
+
+static int pn533_i2c_read(struct pn533_i2c_phy *phy, struct sk_buff **skb)
+{
+   struct i2c_client *client = phy->i2c_dev;
+   int len = PN533_EXT_FRAME_HEADER_LEN +
+ PN533_STD_FRAME_MAX_PAYLOAD_LEN