// SPDX-License-Identifier: GPL-2.0-only
/*
 * LH24030C50 RGB panel driver based on the ST7789V controller.
 */

#include <linux/delay.h>
#include <linux/media-bus-format.h>
#include <linux/of.h>
#include <video/display_timing.h>
#include <video/videomode.h>
#include <video/of_videomode.h>
#include <linux/gpio/consumer.h>
#include <linux/module.h>
#include <linux/regulator/consumer.h>
#include <linux/spi/spi.h>

#include <video/mipi_display.h>

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

#define ST7789V_COLMOD_RGB_FMT_18BITS		(6 << 4)
#define ST7789V_COLMOD_CTRL_FMT_18BITS		(6 << 0)

#define ST7789V_RAMCTRL_CMD		0xb0
#define ST7789V_RAMCTRL_RM_RGB			BIT(4)
#define ST7789V_RAMCTRL_DM_RGB			BIT(0)
#define ST7789V_RAMCTRL_MAGIC			(3 << 6)
#define ST7789V_RAMCTRL_EPF(n)			(((n) & 3) << 4)

#define ST7789V_RGBCTRL_CMD		0xb1
#define ST7789V_RGBCTRL_WO			BIT(7)
#define ST7789V_RGBCTRL_RCM(n)			(((n) & 3) << 5)
#define ST7789V_RGBCTRL_VSYNC_HIGH		BIT(3)
#define ST7789V_RGBCTRL_HSYNC_HIGH		BIT(2)
#define ST7789V_RGBCTRL_PCLK_HIGH		BIT(1)
#define ST7789V_RGBCTRL_DE_LOW			BIT(0)
#define ST7789V_RGBCTRL_VBP(n)			((n) & 0x7f)
#define ST7789V_RGBCTRL_HBP(n)			((n) & 0x1f)

#define ST7789V_PORCTRL_CMD		0xb2
#define ST7789V_PORCTRL_IDLE_BP(n)		(((n) & 0xf) << 4)
#define ST7789V_PORCTRL_IDLE_FP(n)		((n) & 0xf)
#define ST7789V_PORCTRL_PARTIAL_BP(n)		(((n) & 0xf) << 4)
#define ST7789V_PORCTRL_PARTIAL_FP(n)		((n) & 0xf)

#define ST7789V_GCTRL_CMD		0xb7
#define ST7789V_GCTRL_VGHS(n)			(((n) & 7) << 4)
#define ST7789V_GCTRL_VGLS(n)			((n) & 7)

#define ST7789V_VCOMS_CMD		0xbb

#define ST7789V_LCMCTRL_CMD		0xc0
#define ST7789V_LCMCTRL_XBGR			BIT(5)
#define ST7789V_LCMCTRL_XMX			BIT(3)
#define ST7789V_LCMCTRL_XMH			BIT(2)

#define ST7789V_VDVVRHEN_CMD		0xc2
#define ST7789V_VDVVRHEN_CMDEN			BIT(0)

#define ST7789V_VRHS_CMD		0xc3

#define ST7789V_VDVS_CMD		0xc4

#define ST7789V_FRCTRL2_CMD		0xc6

#define ST7789V_PWCTRL1_CMD		0xd0
#define ST7789V_PWCTRL1_MAGIC			0xa4
#define ST7789V_PWCTRL1_AVDD(n)			(((n) & 3) << 6)
#define ST7789V_PWCTRL1_AVCL(n)			(((n) & 3) << 4)
#define ST7789V_PWCTRL1_VDS(n)			((n) & 3)

#define ST7789V_PVGAMCTRL_CMD		0xe0
#define ST7789V_PVGAMCTRL_JP0(n)		(((n) & 3) << 4)
#define ST7789V_PVGAMCTRL_JP1(n)		(((n) & 3) << 4)
#define ST7789V_PVGAMCTRL_VP0(n)		((n) & 0xf)
#define ST7789V_PVGAMCTRL_VP1(n)		((n) & 0x3f)
#define ST7789V_PVGAMCTRL_VP2(n)		((n) & 0x3f)
#define ST7789V_PVGAMCTRL_VP4(n)		((n) & 0x1f)
#define ST7789V_PVGAMCTRL_VP6(n)		((n) & 0x1f)
#define ST7789V_PVGAMCTRL_VP13(n)		((n) & 0xf)
#define ST7789V_PVGAMCTRL_VP20(n)		((n) & 0x7f)
#define ST7789V_PVGAMCTRL_VP27(n)		((n) & 7)
#define ST7789V_PVGAMCTRL_VP36(n)		(((n) & 7) << 4)
#define ST7789V_PVGAMCTRL_VP43(n)		((n) & 0x7f)
#define ST7789V_PVGAMCTRL_VP50(n)		((n) & 0xf)
#define ST7789V_PVGAMCTRL_VP57(n)		((n) & 0x1f)
#define ST7789V_PVGAMCTRL_VP59(n)		((n) & 0x1f)
#define ST7789V_PVGAMCTRL_VP61(n)		((n) & 0x3f)
#define ST7789V_PVGAMCTRL_VP62(n)		((n) & 0x3f)
#define ST7789V_PVGAMCTRL_VP63(n)		(((n) & 0xf) << 4)

#define ST7789V_NVGAMCTRL_CMD		0xe1
#define ST7789V_NVGAMCTRL_JN0(n)		(((n) & 3) << 4)
#define ST7789V_NVGAMCTRL_JN1(n)		(((n) & 3) << 4)
#define ST7789V_NVGAMCTRL_VN0(n)		((n) & 0xf)
#define ST7789V_NVGAMCTRL_VN1(n)		((n) & 0x3f)
#define ST7789V_NVGAMCTRL_VN2(n)		((n) & 0x3f)
#define ST7789V_NVGAMCTRL_VN4(n)		((n) & 0x1f)
#define ST7789V_NVGAMCTRL_VN6(n)		((n) & 0x1f)
#define ST7789V_NVGAMCTRL_VN13(n)		((n) & 0xf)
#define ST7789V_NVGAMCTRL_VN20(n)		((n) & 0x7f)
#define ST7789V_NVGAMCTRL_VN27(n)		((n) & 7)
#define ST7789V_NVGAMCTRL_VN36(n)		(((n) & 7) << 4)
#define ST7789V_NVGAMCTRL_VN43(n)		((n) & 0x7f)
#define ST7789V_NVGAMCTRL_VN50(n)		((n) & 0xf)
#define ST7789V_NVGAMCTRL_VN57(n)		((n) & 0x1f)
#define ST7789V_NVGAMCTRL_VN59(n)		((n) & 0x1f)
#define ST7789V_NVGAMCTRL_VN61(n)		((n) & 0x3f)
#define ST7789V_NVGAMCTRL_VN62(n)		((n) & 0x3f)
#define ST7789V_NVGAMCTRL_VN63(n)		(((n) & 0xf) << 4)

#define ST7789V_TEST(val, func)			\
	do {					\
		if ((val = (func)))		\
			return val;		\
	} while (0)

struct st7789v {
	struct drm_panel panel;
	struct spi_device *spi;
	struct gpio_desc *reset;
	struct gpio_desc *sclk;
	struct gpio_desc *mosi;
	struct gpio_desc *cs;
	struct regulator *power;
};

enum st7789v_prefix {
	ST7789V_COMMAND = 0,
	ST7789V_DATA = 1,
};

static inline struct st7789v *panel_to_st7789v(struct drm_panel *panel)
{
	return container_of(panel, struct st7789v, panel);
}

static void st7789v_get_rgbctrl_from_dt(struct device *dev, u8 *ctrl,
					u8 *vbp, u8 *hbp)
{
	struct videomode vm;

	*ctrl = ST7789V_RGBCTRL_RCM(2);
	*vbp = 4;
	*hbp = 10;

	if (of_get_videomode(dev->of_node, &vm, 0))
		return;

	if (vm.flags & DISPLAY_FLAGS_VSYNC_HIGH)
		*ctrl |= ST7789V_RGBCTRL_VSYNC_HIGH;
	if (vm.flags & DISPLAY_FLAGS_HSYNC_HIGH)
		*ctrl |= ST7789V_RGBCTRL_HSYNC_HIGH;
	if (vm.flags & DISPLAY_FLAGS_DE_LOW)
		*ctrl |= ST7789V_RGBCTRL_DE_LOW;
	if (vm.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
		*ctrl |= ST7789V_RGBCTRL_PCLK_HIGH;

	*vbp = vm.vback_porch;
	*hbp = vm.hback_porch;
}

static void st7789v_get_active_area_from_dt(struct device *dev, u16 *width,
					    u16 *height)
{
	struct videomode vm;

	*width = 240;
	*height = 320;

	if (of_get_videomode(dev->of_node, &vm, 0))
		return;

	*width = vm.hactive;
	*height = vm.vactive;
}

static u32 st7789v_get_bus_flags_from_dt(struct device *dev)
{
	struct videomode vm;
	u32 bus_flags = DRM_BUS_FLAG_DE_HIGH |
			DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE;

	if (of_get_videomode(dev->of_node, &vm, 0))
		return bus_flags;

	bus_flags = 0;

	if (vm.flags & DISPLAY_FLAGS_DE_LOW)
		bus_flags |= DRM_BUS_FLAG_DE_LOW;
	else
		bus_flags |= DRM_BUS_FLAG_DE_HIGH;

	if (vm.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
		bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE;
	else
		bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE;

	return bus_flags;
}

static void st7789v_spi_bitbang(struct st7789v *ctx, int dc, u8 data)
{
	int i;

	gpiod_set_value_cansleep(ctx->cs, 0);
	udelay(1);
	gpiod_set_value_cansleep(ctx->sclk, 0);
	gpiod_set_value_cansleep(ctx->mosi, dc ? 1 : 0);
	udelay(2);
	gpiod_set_value_cansleep(ctx->sclk, 1);
	udelay(2);
	for (i = 0; i < 8; i++) {
		gpiod_set_value_cansleep(ctx->sclk, 0);
		gpiod_set_value_cansleep(ctx->mosi, (data >> (7 - i)) & 1);
		udelay(2);
		gpiod_set_value_cansleep(ctx->sclk, 1);
		udelay(2);
	}
	gpiod_set_value_cansleep(ctx->cs, 1);
	udelay(1);
}

static int st7789v_write_command(struct st7789v *ctx, u8 cmd)
{
	st7789v_spi_bitbang(ctx, 0, cmd);
	return 0;
}

static int st7789v_write_data(struct st7789v *ctx, u8 data)
{
	st7789v_spi_bitbang(ctx, 1, data);
	return 0;
}

static const struct drm_display_mode default_mode = {
	.clock = 7000,
	.hdisplay = 240,
	.hsync_start = 240 + 10,
	.hsync_end = 240 + 10 + 10,
	.htotal = 240 + 10 + 10 + 38,
	.vdisplay = 320,
	.vsync_start = 320 + 4,
	.vsync_end = 320 + 4 + 4,
	.vtotal = 320 + 4 + 4 + 8,
	.flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
};

static int st7789v_get_modes(struct drm_panel *panel,
			     struct drm_connector *connector)
{
	struct drm_display_mode *mode;
	const u32 bus_format = MEDIA_BUS_FMT_RGB666_1X18;
	const struct drm_display_mode *src_mode = &default_mode;
	struct videomode vm;

	if (!of_get_videomode(panel->dev->of_node, &vm, 0)) {
		struct drm_display_mode *dt_mode;

		dev_info(panel->dev,
			 "dt mode hact=%u vact=%u hfp=%u hbp=%u hsync=%u vfp=%u vbp=%u vsync=%u flags=0x%x\n",
			 vm.hactive, vm.vactive,
			 vm.hfront_porch, vm.hback_porch, vm.hsync_len,
			 vm.vfront_porch, vm.vback_porch, vm.vsync_len,
			 vm.flags);

		dt_mode = drm_mode_create(connector->dev);
		if (dt_mode) {
			drm_display_mode_from_videomode(&vm, dt_mode);
			dt_mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
			src_mode = dt_mode;
		}
	} else {
		dev_info(panel->dev,
			 "using default mode hdisplay=%u vdisplay=%u hsync_start=%u hsync_end=%u htotal=%u vsync_start=%u vsync_end=%u vtotal=%u\n",
			 src_mode->hdisplay, src_mode->vdisplay,
			 src_mode->hsync_start, src_mode->hsync_end,
			 src_mode->htotal, src_mode->vsync_start,
			 src_mode->vsync_end, src_mode->vtotal);
	}

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

	drm_mode_set_name(mode);

	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
	drm_mode_probed_add(connector, mode);

	connector->display_info.width_mm = 43;
	connector->display_info.height_mm = 57;
	drm_display_info_set_bus_formats(&connector->display_info,
					  &bus_format, 1);
	connector->display_info.bus_flags =
		st7789v_get_bus_flags_from_dt(panel->dev);
	dev_info(panel->dev, "connector bus_flags=0x%x bus_format=0x%x\n",
		 connector->display_info.bus_flags, bus_format);

	return 1;
}

static int st7789v_prepare(struct drm_panel *panel)
{
	struct st7789v *ctx = panel_to_st7789v(panel);
	u16 width, height;
	u8 rgbctrl, vbp, hbp;
	int ret;

	ret = regulator_enable(ctx->power);
	if (ret)
		return ret;

	gpiod_set_value(ctx->reset, 1);
	msleep(10);
	gpiod_set_value(ctx->reset, 0);
	msleep(150);

	ST7789V_TEST(ret, st7789v_write_command(ctx, MIPI_DCS_EXIT_SLEEP_MODE));

	/* We need to wait 120ms after a sleep out command */
	msleep(120);

	ST7789V_TEST(ret, st7789v_write_command(ctx,
						MIPI_DCS_SET_ADDRESS_MODE));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x00));

	ST7789V_TEST(ret, st7789v_write_command(ctx,
						MIPI_DCS_SET_PIXEL_FORMAT));
	ST7789V_TEST(ret, st7789v_write_data(ctx, MIPI_DCS_PIXEL_FMT_18BIT));

	st7789v_get_active_area_from_dt(panel->dev, &width, &height);
	dev_info(panel->dev, "active area width=%u height=%u\n", width, height);

	ST7789V_TEST(ret, st7789v_write_command(ctx,
						MIPI_DCS_SET_COLUMN_ADDRESS));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x00));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x00));
	ST7789V_TEST(ret, st7789v_write_data(ctx, (width - 1) >> 8));
	ST7789V_TEST(ret, st7789v_write_data(ctx, (width - 1) & 0xff));

	ST7789V_TEST(ret, st7789v_write_command(ctx,
						MIPI_DCS_SET_PAGE_ADDRESS));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x00));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x00));
	ST7789V_TEST(ret, st7789v_write_data(ctx, (height - 1) >> 8));
	ST7789V_TEST(ret, st7789v_write_data(ctx, (height - 1) & 0xff));

	ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_PORCTRL_CMD));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0xc));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0xc));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x33));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x33));

	ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_GCTRL_CMD));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x35));

	ST7789V_TEST(ret, st7789v_write_command(ctx, 0xb6));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x20));

	ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_VCOMS_CMD));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x2b));

	ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_LCMCTRL_CMD));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x2c));

	ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_VDVVRHEN_CMD));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x01));

	ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_VRHS_CMD));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x11));

	ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_VDVS_CMD));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x20));

	ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_FRCTRL2_CMD));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0xf));

	ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_PWCTRL1_CMD));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0xa4));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0xa1));

	ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_PVGAMCTRL_CMD));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0xd0));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x00));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x06));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x09));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x0b));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x2a));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x3c));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x55));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x4b));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x08));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x16));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x14));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x19));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x20));

	ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_NVGAMCTRL_CMD));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0xd0));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x00));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x06));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x09));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x0b));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x29));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x36));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x54));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x4b));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x0d));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x16));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x14));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x21));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x20));

	ST7789V_TEST(ret, st7789v_write_command(ctx, MIPI_DCS_ENTER_INVERT_MODE));

	st7789v_get_rgbctrl_from_dt(panel->dev, &rgbctrl, &vbp, &hbp);
	dev_info(panel->dev,
		 "init regs madctl=0x%02x colmod=0x%02x ramctrl=0x%02x,0x%02x rgbctrl=0x%02x,0x%02x,0x%02x\n",
		 0x00, MIPI_DCS_PIXEL_FMT_18BIT, 0x11, 0x00,
		 rgbctrl, vbp, hbp);

	ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_RAMCTRL_CMD));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x11));
	ST7789V_TEST(ret, st7789v_write_data(ctx, 0x00));

	ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_RGBCTRL_CMD));
	ST7789V_TEST(ret, st7789v_write_data(ctx, rgbctrl));
	ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_RGBCTRL_VBP(vbp)));
	ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_RGBCTRL_HBP(hbp)));

	return 0;
}

static int st7789v_enable(struct drm_panel *panel)
{
	struct st7789v *ctx = panel_to_st7789v(panel);
	int ret;

	ST7789V_TEST(ret, st7789v_write_command(ctx, MIPI_DCS_SET_DISPLAY_ON));
	msleep(50);

	return 0;
}

static int st7789v_disable(struct drm_panel *panel)
{
	struct st7789v *ctx = panel_to_st7789v(panel);
	int ret;

	ST7789V_TEST(ret, st7789v_write_command(ctx, MIPI_DCS_SET_DISPLAY_OFF));

	return 0;
}

static int st7789v_unprepare(struct drm_panel *panel)
{
	struct st7789v *ctx = panel_to_st7789v(panel);
	int ret;

	ST7789V_TEST(ret, st7789v_write_command(ctx, MIPI_DCS_ENTER_SLEEP_MODE));

	regulator_disable(ctx->power);

	return 0;
}

static const struct drm_panel_funcs st7789v_drm_funcs = {
	.disable	= st7789v_disable,
	.enable		= st7789v_enable,
	.get_modes	= st7789v_get_modes,
	.prepare	= st7789v_prepare,
	.unprepare	= st7789v_unprepare,
};

static int st7789v_probe(struct spi_device *spi)
{
	struct st7789v *ctx;
	int ret;

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

	spi_set_drvdata(spi, ctx);
	ctx->spi = spi;

	drm_panel_init(&ctx->panel, &spi->dev, &st7789v_drm_funcs,
		       DRM_MODE_CONNECTOR_DPI);

	ctx->power = devm_regulator_get(&spi->dev, "power");
	if (IS_ERR(ctx->power))
		return PTR_ERR(ctx->power);

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

	ctx->sclk = devm_gpiod_get(&spi->dev, "spi-scl", GPIOD_OUT_LOW);
	if (IS_ERR(ctx->sclk))
		return PTR_ERR(ctx->sclk);
	ctx->mosi = devm_gpiod_get(&spi->dev, "spi-sdi", GPIOD_OUT_LOW);
	if (IS_ERR(ctx->mosi))
		return PTR_ERR(ctx->mosi);
	ctx->cs = devm_gpiod_get(&spi->dev, "spi-cs", GPIOD_OUT_HIGH);
	if (IS_ERR(ctx->cs))
		return PTR_ERR(ctx->cs);

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

	drm_panel_add(&ctx->panel);

	return 0;
}

static int st7789v_remove(struct spi_device *spi)
{
	struct st7789v *ctx = spi_get_drvdata(spi);

	drm_panel_remove(&ctx->panel);

	return 0;
}

static const struct of_device_id st7789v_of_match[] = {
	{ .compatible = "lh,lh24030c50" },
	{ }
};
MODULE_DEVICE_TABLE(of, st7789v_of_match);

static struct spi_driver st7789v_driver = {
	.probe = st7789v_probe,
	.remove = st7789v_remove,
	.driver = {
		.name = "lh24030c50",
		.of_match_table = st7789v_of_match,
	},
};
module_spi_driver(st7789v_driver);

MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
MODULE_DESCRIPTION("LH24030C50 RGB LCD panel driver");
MODULE_LICENSE("GPL v2");