xref: /linux/drivers/gpu/drm/panel/panel-mantix-mlaf057we51.c (revision de848da12f752170c2ebe114804a985314fd5a6a)
172967d56SGuido Günther // SPDX-License-Identifier: GPL-2.0
272967d56SGuido Günther /*
372967d56SGuido Günther  * Mantix MLAF057WE51 5.7" MIPI-DSI panel driver
472967d56SGuido Günther  *
572967d56SGuido Günther  * Copyright (C) Purism SPC 2020
672967d56SGuido Günther  */
772967d56SGuido Günther 
872967d56SGuido Günther #include <linux/backlight.h>
972967d56SGuido Günther #include <linux/delay.h>
1072967d56SGuido Günther #include <linux/gpio/consumer.h>
111311f3dfSGuido Günther #include <linux/media-bus-format.h>
1272967d56SGuido Günther #include <linux/module.h>
13722d4f06SRob Herring #include <linux/of.h>
1472967d56SGuido Günther #include <linux/regulator/consumer.h>
1572967d56SGuido Günther 
1672967d56SGuido Günther #include <video/mipi_display.h>
1772967d56SGuido Günther 
1872967d56SGuido Günther #include <drm/drm_mipi_dsi.h>
1972967d56SGuido Günther #include <drm/drm_modes.h>
2072967d56SGuido Günther #include <drm/drm_panel.h>
2172967d56SGuido Günther 
2272967d56SGuido Günther #define DRV_NAME "panel-mantix-mlaf057we51"
2372967d56SGuido Günther 
2472967d56SGuido Günther /* Manufacturer specific Commands send via DSI */
2572967d56SGuido Günther #define MANTIX_CMD_OTP_STOP_RELOAD_MIPI 0x41
26*b61c4bc6STejas Vipin #define MANTIX_CMD_INT_CANCEL           0x4c
27dd396dbcSGuido Günther #define MANTIX_CMD_SPI_FINISH           0x90
2872967d56SGuido Günther 
2972967d56SGuido Günther struct mantix {
3072967d56SGuido Günther 	struct device *dev;
3172967d56SGuido Günther 	struct drm_panel panel;
32787099f8SGuido Günther 
3372967d56SGuido Günther 	struct gpio_desc *reset_gpio;
34787099f8SGuido Günther 	struct gpio_desc *tp_rstn_gpio;
3572967d56SGuido Günther 
3672967d56SGuido Günther 	struct regulator *avdd;
3772967d56SGuido Günther 	struct regulator *avee;
3872967d56SGuido Günther 	struct regulator *vddi;
396ae5837cSGuido Günther 
406ae5837cSGuido Günther 	const struct drm_display_mode *default_mode;
4172967d56SGuido Günther };
4272967d56SGuido Günther 
4372967d56SGuido Günther static inline struct mantix *panel_to_mantix(struct drm_panel *panel)
4472967d56SGuido Günther {
4572967d56SGuido Günther 	return container_of(panel, struct mantix, panel);
4672967d56SGuido Günther }
4772967d56SGuido Günther 
48e139c0ebSTejas Vipin static void mantix_init_sequence(struct mipi_dsi_multi_context *dsi_ctx)
4972967d56SGuido Günther {
5072967d56SGuido Günther 	/*
5172967d56SGuido Günther 	 * Init sequence was supplied by the panel vendor.
5272967d56SGuido Günther 	 */
53*b61c4bc6STejas Vipin 	mipi_dsi_generic_write_seq_multi(dsi_ctx, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x5a);
5472967d56SGuido Günther 
55e139c0ebSTejas Vipin 	mipi_dsi_generic_write_seq_multi(dsi_ctx, MANTIX_CMD_INT_CANCEL, 0x03);
56*b61c4bc6STejas Vipin 	mipi_dsi_generic_write_seq_multi(dsi_ctx, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x5a, 0x03);
57*b61c4bc6STejas Vipin 	mipi_dsi_generic_write_seq_multi(dsi_ctx, 0x80, 0xa9, 0x00);
5872967d56SGuido Günther 
59*b61c4bc6STejas Vipin 	mipi_dsi_generic_write_seq_multi(dsi_ctx, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x5a, 0x09);
60e139c0ebSTejas Vipin 	mipi_dsi_generic_write_seq_multi(dsi_ctx, 0x80, 0x64, 0x00, 0x64, 0x00, 0x00);
61e139c0ebSTejas Vipin 	mipi_dsi_msleep(dsi_ctx, 20);
6272967d56SGuido Günther 
63*b61c4bc6STejas Vipin 	mipi_dsi_generic_write_seq_multi(dsi_ctx, MANTIX_CMD_SPI_FINISH, 0xa5);
64*b61c4bc6STejas Vipin 	mipi_dsi_generic_write_seq_multi(dsi_ctx, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x00, 0x2f);
65e139c0ebSTejas Vipin 	mipi_dsi_msleep(dsi_ctx, 20);
6672967d56SGuido Günther }
6772967d56SGuido Günther 
6872967d56SGuido Günther static int mantix_enable(struct drm_panel *panel)
6972967d56SGuido Günther {
7072967d56SGuido Günther 	struct mantix *ctx = panel_to_mantix(panel);
71e139c0ebSTejas Vipin 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
72e139c0ebSTejas Vipin 	struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi };
7372967d56SGuido Günther 
74e139c0ebSTejas Vipin 	mantix_init_sequence(&dsi_ctx);
75e139c0ebSTejas Vipin 	if (!dsi_ctx.accum_err)
76e139c0ebSTejas Vipin 		dev_dbg(ctx->dev, "Panel init sequence done\n");
7772967d56SGuido Günther 
78e139c0ebSTejas Vipin 	mipi_dsi_dcs_exit_sleep_mode_multi(&dsi_ctx);
79e139c0ebSTejas Vipin 	mipi_dsi_msleep(&dsi_ctx, 20);
8072967d56SGuido Günther 
81e139c0ebSTejas Vipin 	mipi_dsi_dcs_set_display_on_multi(&dsi_ctx);
82e139c0ebSTejas Vipin 	mipi_dsi_usleep_range(&dsi_ctx, 10000, 12000);
8372967d56SGuido Günther 
84e139c0ebSTejas Vipin 	mipi_dsi_turn_on_peripheral_multi(&dsi_ctx);
8572967d56SGuido Günther 
86e139c0ebSTejas Vipin 	return dsi_ctx.accum_err;
8772967d56SGuido Günther }
8872967d56SGuido Günther 
8972967d56SGuido Günther static int mantix_disable(struct drm_panel *panel)
9072967d56SGuido Günther {
9172967d56SGuido Günther 	struct mantix *ctx = panel_to_mantix(panel);
9272967d56SGuido Günther 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
93e139c0ebSTejas Vipin 	struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi };
9472967d56SGuido Günther 
95e139c0ebSTejas Vipin 	mipi_dsi_dcs_set_display_off_multi(&dsi_ctx);
96e139c0ebSTejas Vipin 	mipi_dsi_dcs_enter_sleep_mode_multi(&dsi_ctx);
9772967d56SGuido Günther 
98e139c0ebSTejas Vipin 	return dsi_ctx.accum_err;
9972967d56SGuido Günther }
10072967d56SGuido Günther 
10172967d56SGuido Günther static int mantix_unprepare(struct drm_panel *panel)
10272967d56SGuido Günther {
10372967d56SGuido Günther 	struct mantix *ctx = panel_to_mantix(panel);
10472967d56SGuido Günther 
105787099f8SGuido Günther 	gpiod_set_value_cansleep(ctx->tp_rstn_gpio, 1);
106787099f8SGuido Günther 	usleep_range(5000, 6000);
107787099f8SGuido Günther 	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
108787099f8SGuido Günther 
10972967d56SGuido Günther 	regulator_disable(ctx->avee);
11072967d56SGuido Günther 	regulator_disable(ctx->avdd);
11172967d56SGuido Günther 	/* T11 */
11272967d56SGuido Günther 	usleep_range(5000, 6000);
11372967d56SGuido Günther 	regulator_disable(ctx->vddi);
11472967d56SGuido Günther 	/* T14 */
11572967d56SGuido Günther 	msleep(50);
11672967d56SGuido Günther 
11772967d56SGuido Günther 	return 0;
11872967d56SGuido Günther }
11972967d56SGuido Günther 
12072967d56SGuido Günther static int mantix_prepare(struct drm_panel *panel)
12172967d56SGuido Günther {
12272967d56SGuido Günther 	struct mantix *ctx = panel_to_mantix(panel);
12372967d56SGuido Günther 	int ret;
12472967d56SGuido Günther 
12572967d56SGuido Günther 	/* Focaltech FT8006P, section 7.3.1 and 7.3.4 */
12672967d56SGuido Günther 	dev_dbg(ctx->dev, "Resetting the panel\n");
12772967d56SGuido Günther 	ret = regulator_enable(ctx->vddi);
12872967d56SGuido Günther 	if (ret < 0) {
12972967d56SGuido Günther 		dev_err(ctx->dev, "Failed to enable vddi supply: %d\n", ret);
13072967d56SGuido Günther 		return ret;
13172967d56SGuido Günther 	}
13272967d56SGuido Günther 
13372967d56SGuido Günther 	/* T1 + T2 */
13472967d56SGuido Günther 	usleep_range(8000, 10000);
13572967d56SGuido Günther 
13672967d56SGuido Günther 	ret = regulator_enable(ctx->avdd);
13772967d56SGuido Günther 	if (ret < 0) {
13872967d56SGuido Günther 		dev_err(ctx->dev, "Failed to enable avdd supply: %d\n", ret);
13972967d56SGuido Günther 		return ret;
14072967d56SGuido Günther 	}
14172967d56SGuido Günther 
14272967d56SGuido Günther 	/* T2d */
14372967d56SGuido Günther 	usleep_range(3500, 4000);
14472967d56SGuido Günther 	ret = regulator_enable(ctx->avee);
14572967d56SGuido Günther 	if (ret < 0) {
14672967d56SGuido Günther 		dev_err(ctx->dev, "Failed to enable avee supply: %d\n", ret);
14772967d56SGuido Günther 		return ret;
14872967d56SGuido Günther 	}
14972967d56SGuido Günther 
150787099f8SGuido Günther 	/* T3 + T4 + time for voltage to become stable: */
151787099f8SGuido Günther 	usleep_range(6000, 7000);
15272967d56SGuido Günther 	gpiod_set_value_cansleep(ctx->reset_gpio, 0);
153787099f8SGuido Günther 	gpiod_set_value_cansleep(ctx->tp_rstn_gpio, 0);
15472967d56SGuido Günther 
15572967d56SGuido Günther 	/* T6 */
15672967d56SGuido Günther 	msleep(50);
15772967d56SGuido Günther 
15872967d56SGuido Günther 	return 0;
15972967d56SGuido Günther }
16072967d56SGuido Günther 
1616ae5837cSGuido Günther static const struct drm_display_mode default_mode_mantix = {
16272967d56SGuido Günther 	.hdisplay    = 720,
16372967d56SGuido Günther 	.hsync_start = 720 + 45,
16472967d56SGuido Günther 	.hsync_end   = 720 + 45 + 14,
16572967d56SGuido Günther 	.htotal	     = 720 + 45 + 14 + 25,
16672967d56SGuido Günther 	.vdisplay    = 1440,
16772967d56SGuido Günther 	.vsync_start = 1440 + 130,
16872967d56SGuido Günther 	.vsync_end   = 1440 + 130 + 8,
16972967d56SGuido Günther 	.vtotal	     = 1440 + 130 + 8 + 106,
17072967d56SGuido Günther 	.clock	     = 85298,
17172967d56SGuido Günther 	.flags	     = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
17272967d56SGuido Günther 	.width_mm    = 65,
17372967d56SGuido Günther 	.height_mm   = 130,
17472967d56SGuido Günther };
17572967d56SGuido Günther 
1764b2b869eSGuido Günther static const struct drm_display_mode default_mode_ys = {
1774b2b869eSGuido Günther 	.hdisplay    = 720,
1784b2b869eSGuido Günther 	.hsync_start = 720 + 45,
1794b2b869eSGuido Günther 	.hsync_end   = 720 + 45 + 14,
1804b2b869eSGuido Günther 	.htotal	     = 720 + 45 + 14 + 25,
1814b2b869eSGuido Günther 	.vdisplay    = 1440,
1824b2b869eSGuido Günther 	.vsync_start = 1440 + 175,
1834b2b869eSGuido Günther 	.vsync_end   = 1440 + 175 + 8,
1844b2b869eSGuido Günther 	.vtotal	     = 1440 + 175 + 8 + 50,
1854b2b869eSGuido Günther 	.clock	     = 85298,
1864b2b869eSGuido Günther 	.flags	     = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
1874b2b869eSGuido Günther 	.width_mm    = 65,
1884b2b869eSGuido Günther 	.height_mm   = 130,
1894b2b869eSGuido Günther };
1904b2b869eSGuido Günther 
1911311f3dfSGuido Günther static const u32 mantix_bus_formats[] = {
1921311f3dfSGuido Günther 	MEDIA_BUS_FMT_RGB888_1X24,
1931311f3dfSGuido Günther };
1941311f3dfSGuido Günther 
19572967d56SGuido Günther static int mantix_get_modes(struct drm_panel *panel,
19672967d56SGuido Günther 			    struct drm_connector *connector)
19772967d56SGuido Günther {
19872967d56SGuido Günther 	struct mantix *ctx = panel_to_mantix(panel);
19972967d56SGuido Günther 	struct drm_display_mode *mode;
20072967d56SGuido Günther 
2016ae5837cSGuido Günther 	mode = drm_mode_duplicate(connector->dev, ctx->default_mode);
20272967d56SGuido Günther 	if (!mode) {
20372967d56SGuido Günther 		dev_err(ctx->dev, "Failed to add mode %ux%u@%u\n",
2046ae5837cSGuido Günther 			ctx->default_mode->hdisplay, ctx->default_mode->vdisplay,
2056ae5837cSGuido Günther 			drm_mode_vrefresh(ctx->default_mode));
20672967d56SGuido Günther 		return -ENOMEM;
20772967d56SGuido Günther 	}
20872967d56SGuido Günther 
20972967d56SGuido Günther 	drm_mode_set_name(mode);
21072967d56SGuido Günther 
21172967d56SGuido Günther 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
21272967d56SGuido Günther 	connector->display_info.width_mm = mode->width_mm;
21372967d56SGuido Günther 	connector->display_info.height_mm = mode->height_mm;
21472967d56SGuido Günther 	drm_mode_probed_add(connector, mode);
21572967d56SGuido Günther 
2161311f3dfSGuido Günther 	drm_display_info_set_bus_formats(&connector->display_info,
2171311f3dfSGuido Günther 					 mantix_bus_formats,
2181311f3dfSGuido Günther 					 ARRAY_SIZE(mantix_bus_formats));
2191311f3dfSGuido Günther 
22072967d56SGuido Günther 	return 1;
22172967d56SGuido Günther }
22272967d56SGuido Günther 
22372967d56SGuido Günther static const struct drm_panel_funcs mantix_drm_funcs = {
22472967d56SGuido Günther 	.disable   = mantix_disable,
22572967d56SGuido Günther 	.unprepare = mantix_unprepare,
22672967d56SGuido Günther 	.prepare   = mantix_prepare,
22772967d56SGuido Günther 	.enable	   = mantix_enable,
22872967d56SGuido Günther 	.get_modes = mantix_get_modes,
22972967d56SGuido Günther };
23072967d56SGuido Günther 
23172967d56SGuido Günther static int mantix_probe(struct mipi_dsi_device *dsi)
23272967d56SGuido Günther {
23372967d56SGuido Günther 	struct device *dev = &dsi->dev;
23472967d56SGuido Günther 	struct mantix *ctx;
23572967d56SGuido Günther 	int ret;
23672967d56SGuido Günther 
23772967d56SGuido Günther 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
23872967d56SGuido Günther 	if (!ctx)
23972967d56SGuido Günther 		return -ENOMEM;
2406ae5837cSGuido Günther 	ctx->default_mode = of_device_get_match_data(dev);
24172967d56SGuido Günther 
242787099f8SGuido Günther 	ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
24372967d56SGuido Günther 	if (IS_ERR(ctx->reset_gpio)) {
24472967d56SGuido Günther 		dev_err(dev, "cannot get reset gpio\n");
24572967d56SGuido Günther 		return PTR_ERR(ctx->reset_gpio);
24672967d56SGuido Günther 	}
24772967d56SGuido Günther 
248787099f8SGuido Günther 	ctx->tp_rstn_gpio = devm_gpiod_get(dev, "mantix,tp-rstn", GPIOD_OUT_HIGH);
249787099f8SGuido Günther 	if (IS_ERR(ctx->tp_rstn_gpio)) {
250787099f8SGuido Günther 		dev_err(dev, "cannot get tp-rstn gpio\n");
251787099f8SGuido Günther 		return PTR_ERR(ctx->tp_rstn_gpio);
252787099f8SGuido Günther 	}
253787099f8SGuido Günther 
25472967d56SGuido Günther 	mipi_dsi_set_drvdata(dsi, ctx);
25572967d56SGuido Günther 	ctx->dev = dev;
25672967d56SGuido Günther 
25772967d56SGuido Günther 	dsi->lanes = 4;
25872967d56SGuido Günther 	dsi->format = MIPI_DSI_FMT_RGB888;
25972967d56SGuido Günther 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
26072967d56SGuido Günther 		MIPI_DSI_MODE_VIDEO_BURST | MIPI_DSI_MODE_VIDEO_SYNC_PULSE;
26172967d56SGuido Günther 
26272967d56SGuido Günther 	ctx->avdd = devm_regulator_get(dev, "avdd");
26372967d56SGuido Günther 	if (IS_ERR(ctx->avdd))
26472967d56SGuido Günther 		return dev_err_probe(dev, PTR_ERR(ctx->avdd), "Failed to request avdd regulator\n");
26572967d56SGuido Günther 
26672967d56SGuido Günther 	ctx->avee = devm_regulator_get(dev, "avee");
26772967d56SGuido Günther 	if (IS_ERR(ctx->avee))
26872967d56SGuido Günther 		return dev_err_probe(dev, PTR_ERR(ctx->avee), "Failed to request avee regulator\n");
26972967d56SGuido Günther 
27072967d56SGuido Günther 	ctx->vddi = devm_regulator_get(dev, "vddi");
27172967d56SGuido Günther 	if (IS_ERR(ctx->vddi))
27272967d56SGuido Günther 		return dev_err_probe(dev, PTR_ERR(ctx->vddi), "Failed to request vddi regulator\n");
27372967d56SGuido Günther 
27472967d56SGuido Günther 	drm_panel_init(&ctx->panel, dev, &mantix_drm_funcs,
27572967d56SGuido Günther 		       DRM_MODE_CONNECTOR_DSI);
27672967d56SGuido Günther 
27772967d56SGuido Günther 	ret = drm_panel_of_backlight(&ctx->panel);
27872967d56SGuido Günther 	if (ret)
27972967d56SGuido Günther 		return ret;
28072967d56SGuido Günther 
28172967d56SGuido Günther 	drm_panel_add(&ctx->panel);
28272967d56SGuido Günther 
28372967d56SGuido Günther 	ret = mipi_dsi_attach(dsi);
28472967d56SGuido Günther 	if (ret < 0) {
28572967d56SGuido Günther 		dev_err(dev, "mipi_dsi_attach failed (%d). Is host ready?\n", ret);
28672967d56SGuido Günther 		drm_panel_remove(&ctx->panel);
28772967d56SGuido Günther 		return ret;
28872967d56SGuido Günther 	}
28972967d56SGuido Günther 
29072967d56SGuido Günther 	dev_info(dev, "%ux%u@%u %ubpp dsi %udl - ready\n",
2916ae5837cSGuido Günther 		 ctx->default_mode->hdisplay, ctx->default_mode->vdisplay,
2926ae5837cSGuido Günther 		 drm_mode_vrefresh(ctx->default_mode),
29372967d56SGuido Günther 		 mipi_dsi_pixel_format_to_bpp(dsi->format), dsi->lanes);
29472967d56SGuido Günther 
29572967d56SGuido Günther 	return 0;
29672967d56SGuido Günther }
29772967d56SGuido Günther 
29872967d56SGuido Günther static void mantix_shutdown(struct mipi_dsi_device *dsi)
29972967d56SGuido Günther {
30072967d56SGuido Günther 	struct mantix *ctx = mipi_dsi_get_drvdata(dsi);
30172967d56SGuido Günther 
30272967d56SGuido Günther 	drm_panel_unprepare(&ctx->panel);
30372967d56SGuido Günther 	drm_panel_disable(&ctx->panel);
30472967d56SGuido Günther }
30572967d56SGuido Günther 
30679abca2bSUwe Kleine-König static void mantix_remove(struct mipi_dsi_device *dsi)
30772967d56SGuido Günther {
30872967d56SGuido Günther 	struct mantix *ctx = mipi_dsi_get_drvdata(dsi);
30972967d56SGuido Günther 
31072967d56SGuido Günther 	mantix_shutdown(dsi);
31172967d56SGuido Günther 
31272967d56SGuido Günther 	mipi_dsi_detach(dsi);
31372967d56SGuido Günther 	drm_panel_remove(&ctx->panel);
31472967d56SGuido Günther }
31572967d56SGuido Günther 
31672967d56SGuido Günther static const struct of_device_id mantix_of_match[] = {
3176ae5837cSGuido Günther 	{ .compatible = "mantix,mlaf057we51-x", .data = &default_mode_mantix },
3184b2b869eSGuido Günther 	{ .compatible = "ys,ys57pss36bh5gq", .data = &default_mode_ys },
31972967d56SGuido Günther 	{ /* sentinel */ }
32072967d56SGuido Günther };
32172967d56SGuido Günther MODULE_DEVICE_TABLE(of, mantix_of_match);
32272967d56SGuido Günther 
32372967d56SGuido Günther static struct mipi_dsi_driver mantix_driver = {
32472967d56SGuido Günther 	.probe	= mantix_probe,
32572967d56SGuido Günther 	.remove = mantix_remove,
32672967d56SGuido Günther 	.shutdown = mantix_shutdown,
32772967d56SGuido Günther 	.driver = {
32872967d56SGuido Günther 		.name = DRV_NAME,
32972967d56SGuido Günther 		.of_match_table = mantix_of_match,
33072967d56SGuido Günther 	},
33172967d56SGuido Günther };
33272967d56SGuido Günther module_mipi_dsi_driver(mantix_driver);
33372967d56SGuido Günther 
33472967d56SGuido Günther MODULE_AUTHOR("Guido Günther <agx@sigxcpu.org>");
33572967d56SGuido Günther MODULE_DESCRIPTION("DRM driver for Mantix MLAF057WE51-X MIPI DSI panel");
33672967d56SGuido Günther MODULE_LICENSE("GPL v2");
337