From 83df88e9a3791abb0ed500a2678d29016dec73ef Mon Sep 17 00:00:00 2001
From: Jerry Han <hanxu5@huaqin.corp-partner.google.com>
Date: Thu, 1 Aug 2019 16:01:07 +0800
Subject: [PATCH 04/10] [v4,2/2] drm/panel: Add Boe Himax8279d MIPI-DSI LCD
 panel

Support Boe Himax8279d 8.0" 1200x1920 TFT LCD panel, it is a MIPI DSI
panel.

V4:
- Frefix all function maes with boe_ (Sam)
- Fsed "enable_gpio" replace "reset_gpio", Make it look clearer (Sam)
- Sort include lines alphabetically (Sam)
- Fixed entries in the makefile must be sorted alphabetically (Sam)
- Add send_mipi_cmds function to avoid duplicating the code (Sam)
- Add the necessary delay(reset_delay_t5) between reset and sending
    the initialization command (Rock wang)

V3:
- Remove unnecessary delays in sending initialization commands (Jitao Shi)

V2:
- Use SPDX identifier (Sam)
- Use necessary header files replace drmP.h (Sam)
- Delete unnecessary header files #include <linux/err.h> (Sam)
- Specifies a GPIOs array to control the reset timing,
    instead of reading "dsi-reset-sequence" data from DTS (Sam)
- Delete backlight_disable() function when already disabled (Sam)
- Use devm_of_find_backlight() replace of_find_backlight_by_node() (Sam)
- Move the necessary data in the DTS to the current file,
    like porch, display_mode and Init code etc. (Sam)
- Add compatible device "boe,himax8279d10p" (Sam)

V1:
- Support Boe Himax8279d 8.0" 1200x1920 TFT LCD panel, it is a MIPI DSI
    panel.

Signed-off-by: Jerry Han <hanxu5@huaqin.corp-partner.google.com>
Cc: Jitao Shi <jitao.shi@mediatek.com>
Cc: Derek Basehore <dbasehore@chromium.org>
Cc: Rock wang <rock_wang@himax.com.cn>
---
 MAINTAINERS                                  |  12 +-
 drivers/gpu/drm/panel/Kconfig                |  22 +-
 drivers/gpu/drm/panel/panel-boe-himax8279d.c | 201 +++++++++----------
 3 files changed, 114 insertions(+), 121 deletions(-)

diff --git a/MAINTAINERS b/MAINTAINERS
index 787575705ed4..942068a63ef4 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -5083,6 +5083,12 @@ T:	git git://anongit.freedesktop.org/drm/drm-misc
 S:	Maintained
 F:	drivers/gpu/drm/bochs/
 
+DRM DRIVER FOR BOE HIMAX8279D PANELS
+M:	Jerry Han <hanxu5@huaqin.corp-partner.google.com>
+S:	Maintained
+F:	drivers/gpu/drm/panel/panel-boe-himax8279d.c
+F:	Documentation/devicetree/bindings/display/panel/boe,himax8279d.txt
+
 DRM DRIVER FOR FARADAY TVE200 TV ENCODER
 M:	Linus Walleij <linus.walleij@linaro.org>
 T:	git git://anongit.freedesktop.org/drm/drm-misc
@@ -5108,12 +5114,6 @@ S:	Maintained
 F:	drivers/gpu/drm/tiny/ili9225.c
 F:	Documentation/devicetree/bindings/display/ilitek,ili9225.txt
 
-DRM DRIVER FOR BOE HIMAX8279D PANELS
-M:	Jerry Han <hanxu5@huaqin.corp-partner.google.com>
-S:	Maintained
-F:	drivers/gpu/drm/panel/panel-boe-himax8279d.c
-F:	Documentation/devicetree/bindings/display/panel/boe,himax8279d.txt
-
 DRM DRIVER FOR HX8357D PANELS
 M:	Eric Anholt <eric@anholt.net>
 T:	git git://anongit.freedesktop.org/drm/drm-misc
diff --git a/drivers/gpu/drm/panel/Kconfig b/drivers/gpu/drm/panel/Kconfig
index e8b060b1b59b..257094a16a7d 100644
--- a/drivers/gpu/drm/panel/Kconfig
+++ b/drivers/gpu/drm/panel/Kconfig
@@ -18,6 +18,17 @@ config DRM_PANEL_ARM_VERSATILE
 	  reference designs. The panel is detected using special registers
 	  in the Versatile family syscon registers.
 
+config DRM_PANEL_BOE_HIMAX8279D
+	tristate "Boe Himax8279d panel"
+	depends on OF
+	depends on DRM_MIPI_DSI
+	depends on BACKLIGHT_CLASS_DEVICE
+	help
+	  Say Y here if you want to enable support for Boe Himax8279d
+	  TFT-LCD modules. The panel has a 1200x1920 resolution and uses
+	  24 bit RGB per pixel. It provides a MIPI DSI interface to
+	  the host and has a built-in LED backlight.
+
 config DRM_PANEL_LVDS
 	tristate "Generic LVDS panel driver"
 	depends on OF
@@ -293,17 +304,6 @@ config DRM_PANEL_SITRONIX_ST7789V
 	  Say Y here if you want to enable support for the Sitronix
 	  ST7789V controller for 240x320 LCD panels
 
-config DRM_PANEL_BOE_HIMAX8279D
-	tristate "Boe Himax8279d panel"
-	depends on OF
-	depends on DRM_MIPI_DSI
-	depends on BACKLIGHT_CLASS_DEVICE
-	help
-	  Say Y here if you want to enable support for Same type
-	  TFT-LCD modules. The panel has a 1200x1920 resolution and uses
-	  24 bit RGB per pixel. It provides a MIPI DSI interface to
-	  the host and has a built-in LED backlight.
-
 config DRM_PANEL_TPO_TPG110
 	tristate "TPO TPG 800x400 panel"
 	depends on OF && SPI && GPIOLIB
diff --git a/drivers/gpu/drm/panel/panel-boe-himax8279d.c b/drivers/gpu/drm/panel/panel-boe-himax8279d.c
index 60f5f8bf2e14..aa7d91279819 100644
--- a/drivers/gpu/drm/panel/panel-boe-himax8279d.c
+++ b/drivers/gpu/drm/panel/panel-boe-himax8279d.c
@@ -17,10 +17,10 @@
 #include <linux/gpio/consumer.h>
 #include <linux/regulator/consumer.h>
 
-#include <drm/drm_modes.h>
+#include <drm/drm_device.h>
 #include <drm/drm_mipi_dsi.h>
+#include <drm/drm_modes.h>
 #include <drm/drm_panel.h>
-#include <drm/drm_device.h>
 #include <drm/drm_print.h>
 
 #include <video/mipi_display.h>
@@ -37,15 +37,16 @@ struct panel_cmd {
 struct panel_desc {
 	const struct drm_display_mode *display_mode;
 	unsigned int bpc;
-	unsigned int width;
-	unsigned int height;
+	unsigned int width_mm;
+	unsigned int height_mm;
 
 	unsigned int delay_t1;
 	unsigned int reset_delay_t2;
 	unsigned int reset_delay_t3;
 	unsigned int reset_delay_t4;
+	unsigned int reset_delay_t5;
 
-	unsigned long flags;
+	unsigned long mode_flags;
 	enum mipi_dsi_pixel_format format;
 	unsigned int lanes;
 	const struct panel_cmd *on_cmds;
@@ -58,7 +59,7 @@ struct panel_info {
 	const struct panel_desc *desc;
 
 	struct backlight_device *backlight;
-	struct gpio_desc *reset_gpio;
+	struct gpio_desc *enable_gpio;
 	struct gpio_desc *pp33_gpio;
 	struct gpio_desc *pp18_gpio;
 
@@ -66,19 +67,49 @@ struct panel_info {
 	bool enabled;
 };
 
+static inline struct panel_info *to_panel_info(struct drm_panel *panel)
+{
+	return container_of(panel, struct panel_info, base);
+}
+
 void set_gpios(struct panel_info *pinfo, int enable)
 {
-	gpiod_set_value(pinfo->reset_gpio, enable);
+	gpiod_set_value(pinfo->enable_gpio, enable);
 	gpiod_set_value(pinfo->pp33_gpio, enable);
 	gpiod_set_value(pinfo->pp18_gpio, enable);
 }
 
-static inline struct panel_info *to_panel_info(struct drm_panel *panel)
+static int send_mipi_cmds(struct drm_panel *panel, const struct panel_cmd *cmds)
 {
-	return container_of(panel, struct panel_info, base);
+	struct panel_info *pinfo = to_panel_info(panel);
+	unsigned int i = 0;
+	int err;
+
+	if (!cmds)
+		return -EFAULT;
+
+	for (i = 0; cmds[i].len != 0; i++) {
+		const struct panel_cmd *cmd = &cmds[i];
+
+		if (cmd->len == 2)
+			err = mipi_dsi_dcs_write(pinfo->link,
+						    cmd->data[1], NULL, 0);
+		else
+			err = mipi_dsi_dcs_write(pinfo->link,
+						    cmd->data[1], cmd->data + 2,
+						    cmd->len - 2);
+
+		if (err < 0)
+			return err;
+
+		usleep_range((cmd->data[0]) * 1000,
+			    (1 + cmd->data[0]) * 1000);
+	}
+
+	return 0;
 }
 
-static int panel_disable(struct drm_panel *panel)
+static int boe_panel_disable(struct drm_panel *panel)
 {
 	struct panel_info *pinfo = to_panel_info(panel);
 
@@ -89,7 +120,7 @@ static int panel_disable(struct drm_panel *panel)
 	return 0;
 }
 
-static int panel_unprepare(struct drm_panel *panel)
+static int boe_panel_unprepare(struct drm_panel *panel)
 {
 	struct panel_info *pinfo = to_panel_info(panel);
 	int err;
@@ -98,34 +129,14 @@ static int panel_unprepare(struct drm_panel *panel)
 		return 0;
 
 	/* send off code */
-	if (pinfo->desc->off_cmds) {
-		const struct panel_cmd *cmds = pinfo->desc->off_cmds;
-		unsigned int i;
-
-		for (i = 0; cmds[i].len != 0; i++) {
-			const struct panel_cmd *cmd = &cmds[i];
-
-			if (cmd->len == 2)
-				err = mipi_dsi_dcs_write(pinfo->link,
-					    cmd->data[1], NULL, 0);
-			else
-				err = mipi_dsi_dcs_write(pinfo->link,
-					    cmd->data[1], cmd->data + 2,
-					    cmd->len - 2);
-
-			if (err < 0) {
-				dev_err(panel->dev,
-					"failed to send DCS Off Code: %d\n",
-					err);
-				goto poweroff;
-			}
-			if (cmd->data[0])
-				usleep_range((cmd->data[0]) * 1000,
-					    (1 + cmd->data[0]) * 1000);
-		}
+	err = send_mipi_cmds(panel, pinfo->desc->off_cmds);
+	if (err < 0) {
+		dev_err(panel->dev,
+			"failed to send DCS Off Code: %d\n",
+			err);
+		goto poweroff;
 	}
 
-	usleep_range(1000, 1000);
 	set_gpios(pinfo, 0);
 
 	pinfo->prepared = false;
@@ -137,7 +148,7 @@ static int panel_unprepare(struct drm_panel *panel)
 	return err;
 }
 
-static int panel_prepare(struct drm_panel *panel)
+static int boe_panel_prepare(struct drm_panel *panel)
 {
 	struct panel_info *pinfo = to_panel_info(panel);
 	const struct panel_desc *desc = pinfo->desc;
@@ -152,47 +163,27 @@ static int panel_prepare(struct drm_panel *panel)
 	gpiod_set_value(pinfo->pp33_gpio, 1);
 
 	/* reset sequence */
-	// T2 (> 14ms)
 	usleep_range(desc->reset_delay_t2, 1000 + desc->reset_delay_t2);
 
-	// T3 (>= 0ms)
 	if (desc->reset_delay_t3) {
-		gpiod_set_value(pinfo->reset_gpio, 1);
+		gpiod_set_value(pinfo->enable_gpio, 1);
 		usleep_range(desc->reset_delay_t3, 1000 + desc->reset_delay_t3);
-		gpiod_set_value(pinfo->reset_gpio, 0);
+		gpiod_set_value(pinfo->enable_gpio, 0);
 	}
 
-	// T4 (> 20us)
 	usleep_range(desc->reset_delay_t4, 1000 + desc->reset_delay_t4);
-	gpiod_set_value(pinfo->reset_gpio, 1);
+	gpiod_set_value(pinfo->enable_gpio, 1);
+
+	if (desc->reset_delay_t5)
+		usleep_range(desc->reset_delay_t5, 1000 + desc->reset_delay_t5);
 
 	/* send init code */
-	if (pinfo->desc->on_cmds) {
-		const struct panel_cmd *cmds = pinfo->desc->on_cmds;
-		unsigned int i;
-
-		for (i = 0; cmds[i].len != 0; i++) {
-			const struct panel_cmd *cmd = &cmds[i];
-
-			if (cmd->len == 2)
-				err = mipi_dsi_dcs_write(pinfo->link,
-					    cmd->data[1], NULL, 0);
-			else
-				err = mipi_dsi_dcs_write(pinfo->link,
-					    cmd->data[1], cmd->data + 2,
-					    cmd->len - 2);
-
-			if (err < 0) {
-				dev_err(panel->dev,
-					"failed to send DCS Init Code: %d\n",
-					err);
-				goto poweroff;
-			}
-
-			if (cmd->data[0])
-				usleep_range(cmd->data[0] * 1000,
-					    (1 + cmd->data[0]) * 1000);
-		}
+	err = send_mipi_cmds(panel, pinfo->desc->on_cmds);
+	if (err < 0) {
+		dev_err(panel->dev,
+			"failed to send DCS Init Code: %d\n",
+			err);
+		goto poweroff;
 	}
 
 	pinfo->prepared = true;
@@ -204,7 +195,7 @@ static int panel_prepare(struct drm_panel *panel)
 	return err;
 }
 
-static int panel_enable(struct drm_panel *panel)
+static int boe_panel_enable(struct drm_panel *panel)
 {
 	struct panel_info *pinfo = to_panel_info(panel);
 	int ret;
@@ -224,7 +215,7 @@ static int panel_enable(struct drm_panel *panel)
 	return 0;
 }
 
-static int panel_get_modes(struct drm_panel *panel)
+static int boe_panel_get_modes(struct drm_panel *panel)
 {
 	struct panel_info *pinfo = to_panel_info(panel);
 	const struct drm_display_mode *m = pinfo->desc->display_mode;
@@ -232,7 +223,7 @@ static int panel_get_modes(struct drm_panel *panel)
 
 	mode = drm_mode_duplicate(panel->drm, m);
 	if (!mode) {
-		DRM_DEV_ERROR(panel->drm->dev, "failed to add mode %ux%ux@%u\n",
+		DRM_DEV_ERROR(panel->drm->dev, "failed to add mode %ux%u@%u\n",
 				m->hdisplay, m->vdisplay, m->vrefresh);
 		return -ENOMEM;
 	}
@@ -241,19 +232,19 @@ static int panel_get_modes(struct drm_panel *panel)
 
 	drm_mode_probed_add(panel->connector, mode);
 
-	panel->connector->display_info.width_mm = pinfo->desc->width;
-	panel->connector->display_info.height_mm = pinfo->desc->height;
+	panel->connector->display_info.width_mm = pinfo->desc->width_mm;
+	panel->connector->display_info.height_mm = pinfo->desc->height_mm;
 	panel->connector->display_info.bpc = pinfo->desc->bpc;
 
 	return 1;
 }
 
 static const struct drm_panel_funcs panel_funcs = {
-	.disable = panel_disable,
-	.unprepare = panel_unprepare,
-	.prepare = panel_prepare,
-	.enable = panel_enable,
-	.get_modes = panel_get_modes,
+	.disable = boe_panel_disable,
+	.unprepare = boe_panel_unprepare,
+	.prepare = boe_panel_prepare,
+	.enable = boe_panel_enable,
+	.get_modes = boe_panel_get_modes,
 };
 
 /* 8 inch */
@@ -579,7 +570,7 @@ static const struct panel_cmd boe_himax8279d8p_on_cmds[] = {
 
 static const struct panel_cmd boe_himax8279d8p_off_cmds[] = {
 	_INIT_CMD(0x00, 0x28),
-	_INIT_CMD(0x00, 0x10),
+	_INIT_CMD(0x01, 0x10),
 
 	{},
 };
@@ -587,14 +578,15 @@ static const struct panel_cmd boe_himax8279d8p_off_cmds[] = {
 static const struct panel_desc boe_himax8279d8p_panel_desc = {
 	.display_mode = &boe_himax8279d8p_display_mode,
 	.bpc = 8,
-	.width = 107,
-	.height = 172,
+	.width_mm = 107,
+	.height_mm = 172,
 	.delay_t1 = 5000,
 	.reset_delay_t2 = 14000,
 	.reset_delay_t3 = 1000,
 	.reset_delay_t4 = 1000,
-	.flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
-		    MIPI_DSI_CLOCK_NON_CONTINUOUS | MIPI_DSI_MODE_LPM,
+	.reset_delay_t5 = 5000,
+	.mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
+			MIPI_DSI_CLOCK_NON_CONTINUOUS | MIPI_DSI_MODE_LPM,
 	.format = MIPI_DSI_FMT_RGB888,
 	.lanes = 4,
 	.on_cmds = boe_himax8279d8p_on_cmds,
@@ -921,7 +913,7 @@ static const struct panel_cmd boe_himax8279d10p_on_cmds[] = {
 
 static const struct panel_cmd boe_himax8279d10p_off_cmds[] = {
 	_INIT_CMD(0x00, 0x28),
-	_INIT_CMD(0x00, 0x10),
+	_INIT_CMD(0x01, 0x10),
 
 	{},
 };
@@ -929,14 +921,15 @@ static const struct panel_cmd boe_himax8279d10p_off_cmds[] = {
 static const struct panel_desc boe_himax8279d10p_panel_desc = {
 	.display_mode = &boe_himax8279d10p_display_mode,
 	.bpc = 8,
-	.width = 135,
-	.height = 216,
+	.width_mm = 135,
+	.height_mm = 216,
 	.delay_t1 = 5000,
 	.reset_delay_t2 = 14000,
 	.reset_delay_t3 = 1000,
 	.reset_delay_t4 = 1000,
-	.flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
-		    MIPI_DSI_CLOCK_NON_CONTINUOUS | MIPI_DSI_MODE_LPM,
+	.reset_delay_t5 = 5000,
+	.mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
+			MIPI_DSI_CLOCK_NON_CONTINUOUS | MIPI_DSI_MODE_LPM,
 	.format = MIPI_DSI_FMT_RGB888,
 	.lanes = 4,
 	.on_cmds = boe_himax8279d10p_on_cmds,
@@ -946,11 +939,11 @@ static const struct panel_desc boe_himax8279d10p_panel_desc = {
 static const struct of_device_id panel_of_match[] = {
 	{ .compatible = "boe,himax8279d8p",
 	  .data = &boe_himax8279d8p_panel_desc
-	},
-	{ .compatible = "boe,himax8279d10p",
+	}, { .compatible = "boe,himax8279d10p",
 	  .data = &boe_himax8279d10p_panel_desc
-	},
-	{ }
+	}, {
+		/* sentinel */
+	}
 };
 MODULE_DEVICE_TABLE(of, panel_of_match);
 
@@ -973,12 +966,12 @@ static int panel_add(struct panel_info *pinfo)
 		pinfo->pp33_gpio = NULL;
 	}
 
-	pinfo->reset_gpio = devm_gpiod_get_optional(dev, "enable",
+	pinfo->enable_gpio = devm_gpiod_get_optional(dev, "enable",
 						    GPIOD_OUT_LOW);
-	if (IS_ERR(pinfo->reset_gpio)) {
-		err = PTR_ERR(pinfo->reset_gpio);
+	if (IS_ERR(pinfo->enable_gpio)) {
+		err = PTR_ERR(pinfo->enable_gpio);
 		dev_err(dev, "failed to get enable gpio: %d\n", err);
-		pinfo->reset_gpio = NULL;
+		pinfo->enable_gpio = NULL;
 	}
 
 	pinfo->backlight = devm_of_find_backlight(dev);
@@ -1005,15 +998,15 @@ static void panel_del(struct panel_info *pinfo)
 static int panel_probe(struct mipi_dsi_device *dsi)
 {
 	struct panel_info *pinfo;
-	int err;
 	const struct panel_desc *desc;
+	int err;
 
 	pinfo = devm_kzalloc(&dsi->dev, sizeof(*pinfo), GFP_KERNEL);
 	if (!pinfo)
 		return -ENOMEM;
 
 	desc = of_device_get_match_data(&dsi->dev);
-	dsi->mode_flags = desc->flags;
+	dsi->mode_flags = desc->mode_flags;
 	dsi->format = desc->format;
 	dsi->lanes = desc->lanes;
 	pinfo->desc = desc;
@@ -1033,12 +1026,12 @@ static int panel_remove(struct mipi_dsi_device *dsi)
 	struct panel_info *pinfo = mipi_dsi_get_drvdata(dsi);
 	int err;
 
-	err = panel_unprepare(&pinfo->base);
+	err = boe_panel_unprepare(&pinfo->base);
 	if (err < 0)
 		DRM_DEV_ERROR(&dsi->dev, "failed to unprepare panel: %d\n",
 						err);
 
-	err = panel_disable(&pinfo->base);
+	err = boe_panel_disable(&pinfo->base);
 	if (err < 0)
 		DRM_DEV_ERROR(&dsi->dev, "failed to disable panel: %d\n", err);
 
@@ -1057,8 +1050,8 @@ static void panel_shutdown(struct mipi_dsi_device *dsi)
 {
 	struct panel_info *pinfo = mipi_dsi_get_drvdata(dsi);
 
-	panel_disable(&pinfo->base);
-	panel_unprepare(&pinfo->base);
+	boe_panel_disable(&pinfo->base);
+	boe_panel_unprepare(&pinfo->base);
 }
 
 static struct mipi_dsi_driver panel_driver = {
-- 
2.17.1

