// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2019-2020 Kali Prasad <kprasadvnsi@protonmail.com>
 */

#include <linux/gpio/consumer.h>
#include <linux/delay.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/regulator/consumer.h>

#include <drm/drm_mipi_dsi.h>
#include <drm/drm_modes.h>
#include <drm/drm_panel.h>

#include <video/mipi_display.h>

#define _INIT_CMD(...) { \
	.len = sizeof((char[]){__VA_ARGS__}), \
	.data = (char[]){__VA_ARGS__} }

struct panel_init_cmd {
	u8 dtype;
	u8 wait;
	u8 dlen;
	const char *data;
};

static const char * const regulator_names[] = {
	"dvdd",
	"avdd",
	"cvdd"
};

struct afj101_ba2131 {
	struct drm_panel	panel;
	struct mipi_dsi_device	*dsi;

	struct regulator_bulk_data supplies[ARRAY_SIZE(regulator_names)];
	struct gpio_desc	*reset;
	struct gpio_desc	*enable;
};

static inline struct afj101_ba2131 *panel_to_sl101_pn27d1665(struct drm_panel *panel)
{
	return container_of(panel, struct afj101_ba2131, panel);
}

/*
 * Display manufacturer failed to provide init sequencing according to
 * https://chromium-review.googlesource.com/c/chromiumos/third_party/coreboot/+/892065/
 * so the init sequence stems from a register dump of a working panel.
 */
static const struct panel_init_cmd afj101_ba2131_init_cmds[] = {

};


static int afj101_ba2131_prepare(struct drm_panel *panel)
{
	struct afj101_ba2131 *ctx = panel_to_sl101_pn27d1665(panel);
	struct mipi_dsi_device *dsi = ctx->dsi;
	unsigned int i;
	int ret;

	gpiod_set_value(ctx->enable, 1);
	msleep(25);

	gpiod_set_value(ctx->reset, 1);
	msleep(25);

	gpiod_set_value(ctx->reset, 0);
	msleep(200);

	for (i = 0; i < ARRAY_SIZE(afj101_ba2131_init_cmds); i++) {
		const struct panel_init_cmd *cmd = &afj101_ba2131_init_cmds[i];

		switch (cmd->dtype) {
			case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
			case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
			case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
			case MIPI_DSI_GENERIC_LONG_WRITE:
				ret = mipi_dsi_generic_write(dsi, cmd->data,
								cmd->dlen);
				break;
			case MIPI_DSI_DCS_SHORT_WRITE:
			case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
			case MIPI_DSI_DCS_LONG_WRITE:
				ret = mipi_dsi_dcs_write_buffer(dsi, cmd->data,
								cmd->dlen);
				break;
			default:
				return -EINVAL;
		}

		if (ret < 0)
			goto powerdown;

		if (cmd->wait)
				msleep(cmd->wait);
	}

	return 0;

powerdown:
	gpiod_set_value(ctx->reset, 1);
	msleep(50);

	// return regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
	return 0;
}

static int afj101_ba2131_enable(struct drm_panel *panel)
{
	struct afj101_ba2131 *ctx = panel_to_sl101_pn27d1665(panel);
	int ret;

	msleep(150);

	ret = mipi_dsi_dcs_set_display_on(ctx->dsi);
	if (ret < 0)
		return ret;

	msleep(50);

	return 0;
}

static int afj101_ba2131_disable(struct drm_panel *panel)
{
	struct afj101_ba2131 *ctx = panel_to_sl101_pn27d1665(panel);

	return mipi_dsi_dcs_set_display_off(ctx->dsi);
}

static int afj101_ba2131_unprepare(struct drm_panel *panel)
{
	struct afj101_ba2131 *ctx = panel_to_sl101_pn27d1665(panel);
	int ret;

	ret = mipi_dsi_dcs_set_display_off(ctx->dsi);
	if (ret < 0)
		dev_err(panel->dev, "failed to set display off: %d\n", ret);

	ret = mipi_dsi_dcs_enter_sleep_mode(ctx->dsi);
	if (ret < 0)
		dev_err(panel->dev, "failed to enter sleep mode: %d\n", ret);

	msleep(200);

	gpiod_set_value(ctx->reset, 1);
	msleep(20);

	gpiod_set_value(ctx->enable, 0);
	msleep(20);

	// return regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
	return 0;
}

static const struct drm_display_mode afj101_ba2131_default_mode = {
	.clock = 52000,

	.hdisplay    = 1024,
	.hsync_start = 1024 + 150,
	.hsync_end   = 1024 + 150 + 150,
	.htotal      = 1024 + 150 + 150 + 30,

	.vdisplay    = 600,
	.vsync_start = 600 + 10,
	.vsync_end   = 600 + 10 + 10,
	.vtotal      = 600 + 10 + 10 + 20,

	.type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
	.width_mm	= 136,
	.height_mm	= 217,
};

static int afj101_ba2131_get_modes(struct drm_panel *panel,
				  struct drm_connector *connector)
{
	struct afj101_ba2131 *ctx = panel_to_sl101_pn27d1665(panel);
	struct drm_display_mode *mode;

	mode = drm_mode_duplicate(connector->dev, &afj101_ba2131_default_mode);
	if (!mode) {
		dev_err(&ctx->dsi->dev, "failed to add mode %ux%u@%u\n",
			afj101_ba2131_default_mode.hdisplay,
			afj101_ba2131_default_mode.vdisplay,
			drm_mode_vrefresh(&afj101_ba2131_default_mode));
		return -ENOMEM;
	}

	drm_mode_set_name(mode);

	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
	connector->display_info.width_mm = mode->width_mm;
	connector->display_info.height_mm = mode->height_mm;
	drm_mode_probed_add(connector, mode);

	return 1;
}

static const struct drm_panel_funcs afj101_ba2131_funcs = {
	.disable = afj101_ba2131_disable,
	.unprepare = afj101_ba2131_unprepare,
	.prepare = afj101_ba2131_prepare,
	.enable = afj101_ba2131_enable,
	.get_modes = afj101_ba2131_get_modes,
};

static int afj101_ba2131_dsi_probe(struct mipi_dsi_device *dsi)
{
	struct afj101_ba2131 *ctx;
	int ret;

	ctx = devm_kzalloc(&dsi->dev, sizeof(*ctx), GFP_KERNEL);
	if (!ctx)
		return -ENOMEM;

	mipi_dsi_set_drvdata(dsi, ctx);
	ctx->dsi = dsi;

	ctx->enable = devm_gpiod_get(&dsi->dev, "enable", GPIOD_OUT_HIGH);
	if (IS_ERR(ctx->enable)) {
		dev_err(&dsi->dev, "Couldn't get our enable GPIO\n");
		return PTR_ERR(ctx->enable);
	}

	ctx->reset = devm_gpiod_get(&dsi->dev, "reset", GPIOD_OUT_LOW);
	if (IS_ERR(ctx->reset)) {
		dev_err(&dsi->dev, "Couldn't get our reset GPIO\n");
		return PTR_ERR(ctx->reset);
	}

	drm_panel_init(&ctx->panel, &dsi->dev, &afj101_ba2131_funcs,
		       DRM_MODE_CONNECTOR_DSI);

	ret = drm_panel_of_backlight(&ctx->panel);
	if (ret){
		return ret;
	}

	drm_panel_add(&ctx->panel);

	dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST | MIPI_DSI_MODE_LPM;
	dsi->format = MIPI_DSI_FMT_RGB888;
	dsi->lanes = 4;

	ret = mipi_dsi_attach(dsi);
	if (ret < 0) {
		drm_panel_remove(&ctx->panel);
		return ret;
	}

	return 0;
}

static int afj101_ba2131_dsi_remove(struct mipi_dsi_device *dsi)
{
	struct afj101_ba2131 *ctx = mipi_dsi_get_drvdata(dsi);

	mipi_dsi_detach(dsi);
	drm_panel_remove(&ctx->panel);

	return 0;
}

static const struct of_device_id afj101_ba2131_of_match[] = {
	{ .compatible = "innolux,afj101-ba2131", },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, afj101_ba2131_of_match);

static struct mipi_dsi_driver afj101_ba2131_driver = {
	.probe = afj101_ba2131_dsi_probe,
	.remove = afj101_ba2131_dsi_remove,
	.driver = {
		.name = "innolux-afj101-ba2131",
		.of_match_table = afj101_ba2131_of_match,
	},
};
module_mipi_dsi_driver(afj101_ba2131_driver);

MODULE_AUTHOR("Kali Prasad <kprasadvnsi@protonmail.com>");
MODULE_DESCRIPTION("Innolux AFJ101 BA2131 MIPI-DSI LCD panel");
MODULE_LICENSE("GPL");
