Re: [PATCH v3 1/2] gpio - Add EXAR XRA1403 SPI GPIO expander driver

2017-05-22 Thread Linus Walleij
On Mon, May 15, 2017 at 7:58 AM, Nandor Han  wrote:

> This driver support basic XRA1403 functionalities:
> - set gpio direction
> - get gpio direction
> - set gpio high/low
> - get gpio status
>
> Signed-off-by: Nandor Han 
> Signed-off-by: Semi Malinen 

Patch applied for kernel v4.13.

Yours,
Linus Walleij


Re: [PATCH v3 1/2] gpio - Add EXAR XRA1403 SPI GPIO expander driver

2017-05-22 Thread Linus Walleij
On Mon, May 15, 2017 at 7:58 AM, Nandor Han  wrote:

> This driver support basic XRA1403 functionalities:
> - set gpio direction
> - get gpio direction
> - set gpio high/low
> - get gpio status
>
> Signed-off-by: Nandor Han 
> Signed-off-by: Semi Malinen 

Patch applied for kernel v4.13.

Yours,
Linus Walleij


[PATCH v3 1/2] gpio - Add EXAR XRA1403 SPI GPIO expander driver

2017-05-14 Thread Nandor Han
This driver support basic XRA1403 functionalities:
- set gpio direction
- get gpio direction
- set gpio high/low
- get gpio status

Signed-off-by: Nandor Han 
Signed-off-by: Semi Malinen 
---
 drivers/gpio/Kconfig|   5 +
 drivers/gpio/Makefile   |   1 +
 drivers/gpio/gpio-xra1403.c | 237 
 3 files changed, 243 insertions(+)
 create mode 100644 drivers/gpio/gpio-xra1403.c

diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index 23ca51e..dbde3ae 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -1225,6 +1225,11 @@ config GPIO_PISOSR
  GPIO driver for SPI compatible parallel-in/serial-out shift
  registers. These are input only devices.
 
+config GPIO_XRA1403
+   tristate "EXAR XRA1403 16-bit GPIO expander"
+   help
+ GPIO driver for EXAR XRA1403 16-bit SPI-based GPIO expander.
+
 endmenu
 
 menu "SPI or I2C GPIO expanders"
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
index 68b9627..4d9adc6 100644
--- a/drivers/gpio/Makefile
+++ b/drivers/gpio/Makefile
@@ -141,6 +141,7 @@ obj-$(CONFIG_GPIO_XGENE)+= gpio-xgene.o
 obj-$(CONFIG_GPIO_XGENE_SB)+= gpio-xgene-sb.o
 obj-$(CONFIG_GPIO_XILINX)  += gpio-xilinx.o
 obj-$(CONFIG_GPIO_XLP) += gpio-xlp.o
+obj-$(CONFIG_GPIO_XRA1403) += gpio-xra1403.o
 obj-$(CONFIG_GPIO_XTENSA)  += gpio-xtensa.o
 obj-$(CONFIG_GPIO_ZEVIO)   += gpio-zevio.o
 obj-$(CONFIG_GPIO_ZYNQ)+= gpio-zynq.o
diff --git a/drivers/gpio/gpio-xra1403.c b/drivers/gpio/gpio-xra1403.c
new file mode 100644
index 000..0230e4b
--- /dev/null
+++ b/drivers/gpio/gpio-xra1403.c
@@ -0,0 +1,237 @@
+/*
+ * GPIO driver for EXAR XRA1403 16-bit GPIO expander
+ *
+ * Copyright (c) 2017, General Electric Company
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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 
+
+/* XRA1403 registers */
+#define XRA_GSR   0x00 /* GPIO State */
+#define XRA_OCR   0x02 /* Output Control */
+#define XRA_PIR   0x04 /* Input Polarity Inversion */
+#define XRA_GCR   0x06 /* GPIO Configuration */
+#define XRA_PUR   0x08 /* Input Internal Pull-up Resistor Enable/Disable */
+#define XRA_IER   0x0A /* Input Interrupt Enable */
+#define XRA_TSCR  0x0C /* Output Three-State Control */
+#define XRA_ISR   0x0E /* Input Interrupt Status */
+#define XRA_REIR  0x10 /* Input Rising Edge Interrupt Enable */
+#define XRA_FEIR  0x12 /* Input Falling Edge Interrupt Enable */
+#define XRA_IFR   0x14 /* Input Filter Enable/Disable */
+
+struct xra1403 {
+   struct gpio_chip  chip;
+   struct regmap *regmap;
+};
+
+static const struct regmap_config xra1403_regmap_cfg = {
+   .reg_bits = 7,
+   .pad_bits = 1,
+   .val_bits = 8,
+
+   .max_register = XRA_IFR | 0x01,
+};
+
+static unsigned int to_reg(unsigned int reg, unsigned int offset)
+{
+   return reg + (offset > 7);
+}
+
+static int xra1403_direction_input(struct gpio_chip *chip, unsigned int offset)
+{
+   struct xra1403 *xra = gpiochip_get_data(chip);
+
+   return regmap_update_bits(xra->regmap, to_reg(XRA_GCR, offset),
+   BIT(offset % 8), BIT(offset % 8));
+}
+
+static int xra1403_direction_output(struct gpio_chip *chip, unsigned int 
offset,
+   int value)
+{
+   int ret;
+   struct xra1403 *xra = gpiochip_get_data(chip);
+
+   ret = regmap_update_bits(xra->regmap, to_reg(XRA_GCR, offset),
+   BIT(offset % 8), 0);
+   if (ret)
+   return ret;
+
+   ret = regmap_update_bits(xra->regmap, to_reg(XRA_OCR, offset),
+   BIT(offset % 8), value ? BIT(offset % 8) : 0);
+
+   return ret;
+}
+
+static int xra1403_get_direction(struct gpio_chip *chip, unsigned int offset)
+{
+   int ret;
+   unsigned int val;
+   struct xra1403 *xra = gpiochip_get_data(chip);
+
+   ret = regmap_read(xra->regmap, to_reg(XRA_GCR, offset), );
+   if (ret)
+   return ret;
+
+   return !!(val & BIT(offset % 8));
+}
+
+static int xra1403_get(struct gpio_chip *chip, unsigned int offset)
+{
+   int ret;
+   unsigned int val;
+   struct xra1403 *xra = gpiochip_get_data(chip);
+
+   ret = regmap_read(xra->regmap, 

[PATCH v3 1/2] gpio - Add EXAR XRA1403 SPI GPIO expander driver

2017-05-14 Thread Nandor Han
This driver support basic XRA1403 functionalities:
- set gpio direction
- get gpio direction
- set gpio high/low
- get gpio status

Signed-off-by: Nandor Han 
Signed-off-by: Semi Malinen 
---
 drivers/gpio/Kconfig|   5 +
 drivers/gpio/Makefile   |   1 +
 drivers/gpio/gpio-xra1403.c | 237 
 3 files changed, 243 insertions(+)
 create mode 100644 drivers/gpio/gpio-xra1403.c

diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index 23ca51e..dbde3ae 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -1225,6 +1225,11 @@ config GPIO_PISOSR
  GPIO driver for SPI compatible parallel-in/serial-out shift
  registers. These are input only devices.
 
+config GPIO_XRA1403
+   tristate "EXAR XRA1403 16-bit GPIO expander"
+   help
+ GPIO driver for EXAR XRA1403 16-bit SPI-based GPIO expander.
+
 endmenu
 
 menu "SPI or I2C GPIO expanders"
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
index 68b9627..4d9adc6 100644
--- a/drivers/gpio/Makefile
+++ b/drivers/gpio/Makefile
@@ -141,6 +141,7 @@ obj-$(CONFIG_GPIO_XGENE)+= gpio-xgene.o
 obj-$(CONFIG_GPIO_XGENE_SB)+= gpio-xgene-sb.o
 obj-$(CONFIG_GPIO_XILINX)  += gpio-xilinx.o
 obj-$(CONFIG_GPIO_XLP) += gpio-xlp.o
+obj-$(CONFIG_GPIO_XRA1403) += gpio-xra1403.o
 obj-$(CONFIG_GPIO_XTENSA)  += gpio-xtensa.o
 obj-$(CONFIG_GPIO_ZEVIO)   += gpio-zevio.o
 obj-$(CONFIG_GPIO_ZYNQ)+= gpio-zynq.o
diff --git a/drivers/gpio/gpio-xra1403.c b/drivers/gpio/gpio-xra1403.c
new file mode 100644
index 000..0230e4b
--- /dev/null
+++ b/drivers/gpio/gpio-xra1403.c
@@ -0,0 +1,237 @@
+/*
+ * GPIO driver for EXAR XRA1403 16-bit GPIO expander
+ *
+ * Copyright (c) 2017, General Electric Company
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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 
+
+/* XRA1403 registers */
+#define XRA_GSR   0x00 /* GPIO State */
+#define XRA_OCR   0x02 /* Output Control */
+#define XRA_PIR   0x04 /* Input Polarity Inversion */
+#define XRA_GCR   0x06 /* GPIO Configuration */
+#define XRA_PUR   0x08 /* Input Internal Pull-up Resistor Enable/Disable */
+#define XRA_IER   0x0A /* Input Interrupt Enable */
+#define XRA_TSCR  0x0C /* Output Three-State Control */
+#define XRA_ISR   0x0E /* Input Interrupt Status */
+#define XRA_REIR  0x10 /* Input Rising Edge Interrupt Enable */
+#define XRA_FEIR  0x12 /* Input Falling Edge Interrupt Enable */
+#define XRA_IFR   0x14 /* Input Filter Enable/Disable */
+
+struct xra1403 {
+   struct gpio_chip  chip;
+   struct regmap *regmap;
+};
+
+static const struct regmap_config xra1403_regmap_cfg = {
+   .reg_bits = 7,
+   .pad_bits = 1,
+   .val_bits = 8,
+
+   .max_register = XRA_IFR | 0x01,
+};
+
+static unsigned int to_reg(unsigned int reg, unsigned int offset)
+{
+   return reg + (offset > 7);
+}
+
+static int xra1403_direction_input(struct gpio_chip *chip, unsigned int offset)
+{
+   struct xra1403 *xra = gpiochip_get_data(chip);
+
+   return regmap_update_bits(xra->regmap, to_reg(XRA_GCR, offset),
+   BIT(offset % 8), BIT(offset % 8));
+}
+
+static int xra1403_direction_output(struct gpio_chip *chip, unsigned int 
offset,
+   int value)
+{
+   int ret;
+   struct xra1403 *xra = gpiochip_get_data(chip);
+
+   ret = regmap_update_bits(xra->regmap, to_reg(XRA_GCR, offset),
+   BIT(offset % 8), 0);
+   if (ret)
+   return ret;
+
+   ret = regmap_update_bits(xra->regmap, to_reg(XRA_OCR, offset),
+   BIT(offset % 8), value ? BIT(offset % 8) : 0);
+
+   return ret;
+}
+
+static int xra1403_get_direction(struct gpio_chip *chip, unsigned int offset)
+{
+   int ret;
+   unsigned int val;
+   struct xra1403 *xra = gpiochip_get_data(chip);
+
+   ret = regmap_read(xra->regmap, to_reg(XRA_GCR, offset), );
+   if (ret)
+   return ret;
+
+   return !!(val & BIT(offset % 8));
+}
+
+static int xra1403_get(struct gpio_chip *chip, unsigned int offset)
+{
+   int ret;
+   unsigned int val;
+   struct xra1403 *xra = gpiochip_get_data(chip);
+
+   ret = regmap_read(xra->regmap, to_reg(XRA_GSR, offset), );
+   if