[PATCH linux v1 3/4] drivers: misc: Platform driver for seven segment display support

2016-12-13 Thread Jaghathiswari Rankappagounder Natarajan
Platform device driver which provides an API for displaying on two
7-segment displays, and implements the required bit-banging.
The hardware assumed is 74HC164 wired to two 7-segment displays.

Signed-off-by: Jaghathiswari Rankappagounder Natarajan 
---
 drivers/misc/Kconfig  |   8 ++
 drivers/misc/Makefile |   1 +
 drivers/misc/seven_seg_gpio.c | 206 ++
 3 files changed, 215 insertions(+)
 create mode 100644 drivers/misc/seven_seg_gpio.c

diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
index a21aec1..6508108 100644
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -812,6 +812,14 @@ config PANEL_BOOT_MESSAGE
  An empty message will only clear the display at driver init time. Any 
other
  printf()-formatted message is valid with newline and escape codes.

+config SEVEN_SEGMENT_GPIO
+   tristate "Platform driver to update seven segment display"
+   depends on SEVEN_SEGMENT_DISPLAY
+   help
+ Platform device driver which provides an API for displaying on two
+ 7-segment displays, and implements the required bit-banging.
+ The hardware assumed is 74HC164 wired to two 7-segment displays.
+
 source "drivers/misc/c2port/Kconfig"
 source "drivers/misc/eeprom/Kconfig"
 source "drivers/misc/cb710/Kconfig"
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index c2defbd..d9c0d20 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -58,3 +58,4 @@ obj-$(CONFIG_VEXPRESS_SYSCFG) += vexpress-syscfg.o
 obj-$(CONFIG_CXL_BASE) += cxl/
 obj-$(CONFIG_SEVEN_SEGMENT_DISPLAY)+= seven_seg_disp.o
 obj-$(CONFIG_PANEL) += panel.o
+obj-$(CONFIG_SEVEN_SEGMENT_GPIO)   += seven_seg_gpio.o
diff --git a/drivers/misc/seven_seg_gpio.c b/drivers/misc/seven_seg_gpio.c
new file mode 100644
index 000..3dcb903
--- /dev/null
+++ b/drivers/misc/seven_seg_gpio.c
@@ -0,0 +1,206 @@
+/*
+ * Copyright (C) 2016 Google, Inc
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 or later as
+ * published by the Free Software Foundation.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "seven_seg_disp.h"
+
+#define DELAY_INTVL_US 1
+
+#define CLOCK_GPIO_NAME "clock"
+#define DATA_GPIO_NAME "data"
+#define CLEAR_GPIO_NAME "clear"
+
+struct seven_seg_gpio_info {
+   u16 curr_disp_value;
+   u16 refresh_interval;
+   struct timer_list update_timer;
+   struct gpio_desc *clock_gpio;
+   struct gpio_desc *data_gpio;
+   struct gpio_desc *clear_gpio;
+};
+
+static void update_seven_seg_gpio_data(struct device *dev, u16 data)
+{
+   struct platform_device *pdev;
+   struct seven_seg_gpio_info *gpio_info;
+
+   pdev = container_of(dev, struct platform_device, dev);
+   if (pdev == NULL) {
+   pr_err("invalid NULL platform_device\n");
+   return;
+   }
+
+   gpio_info = platform_get_drvdata(pdev);
+   if (gpio_info == NULL) {
+   pr_err("invalid NULL gpio_info\n");
+   return;
+   }
+
+   gpio_info->curr_disp_value = data;
+}
+
+static void clear_seven_seg_gpio_data(struct device *dev, u16 data)
+{
+   struct platform_device *pdev;
+   struct seven_seg_gpio_info *gpio_info;
+
+   pdev = container_of(dev, struct platform_device, dev);
+   if (pdev == NULL) {
+   pr_err("invalid NULL platform_device\n");
+   return;
+   }
+
+   gpio_info = platform_get_drvdata(pdev);
+   if (gpio_info == NULL) {
+   pr_err("invalid NULL gpio_info\n");
+   return;
+   }
+
+   gpio_info->curr_disp_value = 0;
+}
+
+static void send_seven_seg_gpio_data(u16 disp_data,
+   struct seven_seg_gpio_info *gpio_info)
+{
+   int i;
+
+   gpiod_set_value(gpio_info->clear_gpio, 0);
+   udelay(DELAY_INTVL_US);
+   gpiod_set_value(gpio_info->clear_gpio, 1);
+   udelay(DELAY_INTVL_US);
+
+   for (i = 0; i < 16; i++) {
+   if (disp_data & 0x01)
+   gpiod_set_value(gpio_info->data_gpio, 1);
+   else
+   gpiod_set_value(gpio_info->data_gpio, 0);
+
+   udelay(DELAY_INTVL_US);
+
+   gpiod_set_value(gpio_info->clock_gpio, 0);
+   udelay(DELAY_INTVL_US);
+   gpiod_set_value(gpio_info->clock_gpio, 1);
+   udelay(DELAY_INTVL_US);
+
+   disp_data >>= 1;
+   }
+}
+
+static void disp_refresh_timer_handler(unsigned long data)
+{
+   u16 disp_data;
+   struct seven_seg_gpio_info *gpio_info =
+   (struct seven_seg_gpio_info *)data;
+   disp_data = gpio_info->curr_disp_value;
+
+   send_seven_seg_gpio_data(disp_data, gpio_info);
+   

[PATCH linux v1 3/4] drivers: misc: Platform driver for seven segment display support

2016-12-13 Thread Jaghathiswari Rankappagounder Natarajan
Platform device driver which provides an API for displaying on two
7-segment displays, and implements the required bit-banging.
The hardware assumed is 74HC164 wired to two 7-segment displays.

Signed-off-by: Jaghathiswari Rankappagounder Natarajan 
---
 drivers/misc/Kconfig  |   8 ++
 drivers/misc/Makefile |   1 +
 drivers/misc/seven_seg_gpio.c | 206 ++
 3 files changed, 215 insertions(+)
 create mode 100644 drivers/misc/seven_seg_gpio.c

diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
index a21aec1..6508108 100644
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -812,6 +812,14 @@ config PANEL_BOOT_MESSAGE
  An empty message will only clear the display at driver init time. Any 
other
  printf()-formatted message is valid with newline and escape codes.

+config SEVEN_SEGMENT_GPIO
+   tristate "Platform driver to update seven segment display"
+   depends on SEVEN_SEGMENT_DISPLAY
+   help
+ Platform device driver which provides an API for displaying on two
+ 7-segment displays, and implements the required bit-banging.
+ The hardware assumed is 74HC164 wired to two 7-segment displays.
+
 source "drivers/misc/c2port/Kconfig"
 source "drivers/misc/eeprom/Kconfig"
 source "drivers/misc/cb710/Kconfig"
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index c2defbd..d9c0d20 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -58,3 +58,4 @@ obj-$(CONFIG_VEXPRESS_SYSCFG) += vexpress-syscfg.o
 obj-$(CONFIG_CXL_BASE) += cxl/
 obj-$(CONFIG_SEVEN_SEGMENT_DISPLAY)+= seven_seg_disp.o
 obj-$(CONFIG_PANEL) += panel.o
+obj-$(CONFIG_SEVEN_SEGMENT_GPIO)   += seven_seg_gpio.o
diff --git a/drivers/misc/seven_seg_gpio.c b/drivers/misc/seven_seg_gpio.c
new file mode 100644
index 000..3dcb903
--- /dev/null
+++ b/drivers/misc/seven_seg_gpio.c
@@ -0,0 +1,206 @@
+/*
+ * Copyright (C) 2016 Google, Inc
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 or later as
+ * published by the Free Software Foundation.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "seven_seg_disp.h"
+
+#define DELAY_INTVL_US 1
+
+#define CLOCK_GPIO_NAME "clock"
+#define DATA_GPIO_NAME "data"
+#define CLEAR_GPIO_NAME "clear"
+
+struct seven_seg_gpio_info {
+   u16 curr_disp_value;
+   u16 refresh_interval;
+   struct timer_list update_timer;
+   struct gpio_desc *clock_gpio;
+   struct gpio_desc *data_gpio;
+   struct gpio_desc *clear_gpio;
+};
+
+static void update_seven_seg_gpio_data(struct device *dev, u16 data)
+{
+   struct platform_device *pdev;
+   struct seven_seg_gpio_info *gpio_info;
+
+   pdev = container_of(dev, struct platform_device, dev);
+   if (pdev == NULL) {
+   pr_err("invalid NULL platform_device\n");
+   return;
+   }
+
+   gpio_info = platform_get_drvdata(pdev);
+   if (gpio_info == NULL) {
+   pr_err("invalid NULL gpio_info\n");
+   return;
+   }
+
+   gpio_info->curr_disp_value = data;
+}
+
+static void clear_seven_seg_gpio_data(struct device *dev, u16 data)
+{
+   struct platform_device *pdev;
+   struct seven_seg_gpio_info *gpio_info;
+
+   pdev = container_of(dev, struct platform_device, dev);
+   if (pdev == NULL) {
+   pr_err("invalid NULL platform_device\n");
+   return;
+   }
+
+   gpio_info = platform_get_drvdata(pdev);
+   if (gpio_info == NULL) {
+   pr_err("invalid NULL gpio_info\n");
+   return;
+   }
+
+   gpio_info->curr_disp_value = 0;
+}
+
+static void send_seven_seg_gpio_data(u16 disp_data,
+   struct seven_seg_gpio_info *gpio_info)
+{
+   int i;
+
+   gpiod_set_value(gpio_info->clear_gpio, 0);
+   udelay(DELAY_INTVL_US);
+   gpiod_set_value(gpio_info->clear_gpio, 1);
+   udelay(DELAY_INTVL_US);
+
+   for (i = 0; i < 16; i++) {
+   if (disp_data & 0x01)
+   gpiod_set_value(gpio_info->data_gpio, 1);
+   else
+   gpiod_set_value(gpio_info->data_gpio, 0);
+
+   udelay(DELAY_INTVL_US);
+
+   gpiod_set_value(gpio_info->clock_gpio, 0);
+   udelay(DELAY_INTVL_US);
+   gpiod_set_value(gpio_info->clock_gpio, 1);
+   udelay(DELAY_INTVL_US);
+
+   disp_data >>= 1;
+   }
+}
+
+static void disp_refresh_timer_handler(unsigned long data)
+{
+   u16 disp_data;
+   struct seven_seg_gpio_info *gpio_info =
+   (struct seven_seg_gpio_info *)data;
+   disp_data = gpio_info->curr_disp_value;
+
+   send_seven_seg_gpio_data(disp_data, gpio_info);
+   mod_timer(_info->update_timer,
+