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>
13*722d4f06SRob 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
2672967d56SGuido Günther #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
panel_to_mantix(struct drm_panel * panel)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
mantix_init_sequence(struct mantix * ctx)4872967d56SGuido Günther static int mantix_init_sequence(struct mantix *ctx)
4972967d56SGuido Günther {
5072967d56SGuido Günther struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
5172967d56SGuido Günther struct device *dev = ctx->dev;
5272967d56SGuido Günther
5372967d56SGuido Günther /*
5472967d56SGuido Günther * Init sequence was supplied by the panel vendor.
5572967d56SGuido Günther */
562ba1f9b7SJavier Martinez Canillas mipi_dsi_generic_write_seq(dsi, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x5A);
5772967d56SGuido Günther
582ba1f9b7SJavier Martinez Canillas mipi_dsi_generic_write_seq(dsi, MANTIX_CMD_INT_CANCEL, 0x03);
592ba1f9b7SJavier Martinez Canillas mipi_dsi_generic_write_seq(dsi, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x5A, 0x03);
602ba1f9b7SJavier Martinez Canillas mipi_dsi_generic_write_seq(dsi, 0x80, 0xA9, 0x00);
6172967d56SGuido Günther
622ba1f9b7SJavier Martinez Canillas mipi_dsi_generic_write_seq(dsi, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x5A, 0x09);
632ba1f9b7SJavier Martinez Canillas mipi_dsi_generic_write_seq(dsi, 0x80, 0x64, 0x00, 0x64, 0x00, 0x00);
6472967d56SGuido Günther msleep(20);
6572967d56SGuido Günther
662ba1f9b7SJavier Martinez Canillas mipi_dsi_generic_write_seq(dsi, MANTIX_CMD_SPI_FINISH, 0xA5);
672ba1f9b7SJavier Martinez Canillas mipi_dsi_generic_write_seq(dsi, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x00, 0x2F);
68dd396dbcSGuido Günther msleep(20);
69dd396dbcSGuido Günther
7072967d56SGuido Günther dev_dbg(dev, "Panel init sequence done\n");
7172967d56SGuido Günther return 0;
7272967d56SGuido Günther }
7372967d56SGuido Günther
mantix_enable(struct drm_panel * panel)7472967d56SGuido Günther static int mantix_enable(struct drm_panel *panel)
7572967d56SGuido Günther {
7672967d56SGuido Günther struct mantix *ctx = panel_to_mantix(panel);
7772967d56SGuido Günther struct device *dev = ctx->dev;
7872967d56SGuido Günther struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
7972967d56SGuido Günther int ret;
8072967d56SGuido Günther
8172967d56SGuido Günther ret = mantix_init_sequence(ctx);
8272967d56SGuido Günther if (ret < 0) {
8372967d56SGuido Günther dev_err(ctx->dev, "Panel init sequence failed: %d\n", ret);
8472967d56SGuido Günther return ret;
8572967d56SGuido Günther }
8672967d56SGuido Günther
8772967d56SGuido Günther ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
8872967d56SGuido Günther if (ret < 0) {
8972967d56SGuido Günther dev_err(dev, "Failed to exit sleep mode\n");
9072967d56SGuido Günther return ret;
9172967d56SGuido Günther }
9272967d56SGuido Günther msleep(20);
9372967d56SGuido Günther
9472967d56SGuido Günther ret = mipi_dsi_dcs_set_display_on(dsi);
9572967d56SGuido Günther if (ret)
9672967d56SGuido Günther return ret;
9772967d56SGuido Günther usleep_range(10000, 12000);
9872967d56SGuido Günther
9972967d56SGuido Günther ret = mipi_dsi_turn_on_peripheral(dsi);
10072967d56SGuido Günther if (ret < 0) {
10172967d56SGuido Günther dev_err(dev, "Failed to turn on peripheral\n");
10272967d56SGuido Günther return ret;
10372967d56SGuido Günther }
10472967d56SGuido Günther
10572967d56SGuido Günther return 0;
10672967d56SGuido Günther }
10772967d56SGuido Günther
mantix_disable(struct drm_panel * panel)10872967d56SGuido Günther static int mantix_disable(struct drm_panel *panel)
10972967d56SGuido Günther {
11072967d56SGuido Günther struct mantix *ctx = panel_to_mantix(panel);
11172967d56SGuido Günther struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
11272967d56SGuido Günther int ret;
11372967d56SGuido Günther
11472967d56SGuido Günther ret = mipi_dsi_dcs_set_display_off(dsi);
11572967d56SGuido Günther if (ret < 0)
11672967d56SGuido Günther dev_err(ctx->dev, "Failed to turn off the display: %d\n", ret);
11772967d56SGuido Günther
11872967d56SGuido Günther ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
11972967d56SGuido Günther if (ret < 0)
12072967d56SGuido Günther dev_err(ctx->dev, "Failed to enter sleep mode: %d\n", ret);
12172967d56SGuido Günther
12272967d56SGuido Günther
12372967d56SGuido Günther return 0;
12472967d56SGuido Günther }
12572967d56SGuido Günther
mantix_unprepare(struct drm_panel * panel)12672967d56SGuido Günther static int mantix_unprepare(struct drm_panel *panel)
12772967d56SGuido Günther {
12872967d56SGuido Günther struct mantix *ctx = panel_to_mantix(panel);
12972967d56SGuido Günther
130787099f8SGuido Günther gpiod_set_value_cansleep(ctx->tp_rstn_gpio, 1);
131787099f8SGuido Günther usleep_range(5000, 6000);
132787099f8SGuido Günther gpiod_set_value_cansleep(ctx->reset_gpio, 1);
133787099f8SGuido Günther
13472967d56SGuido Günther regulator_disable(ctx->avee);
13572967d56SGuido Günther regulator_disable(ctx->avdd);
13672967d56SGuido Günther /* T11 */
13772967d56SGuido Günther usleep_range(5000, 6000);
13872967d56SGuido Günther regulator_disable(ctx->vddi);
13972967d56SGuido Günther /* T14 */
14072967d56SGuido Günther msleep(50);
14172967d56SGuido Günther
14272967d56SGuido Günther return 0;
14372967d56SGuido Günther }
14472967d56SGuido Günther
mantix_prepare(struct drm_panel * panel)14572967d56SGuido Günther static int mantix_prepare(struct drm_panel *panel)
14672967d56SGuido Günther {
14772967d56SGuido Günther struct mantix *ctx = panel_to_mantix(panel);
14872967d56SGuido Günther int ret;
14972967d56SGuido Günther
15072967d56SGuido Günther /* Focaltech FT8006P, section 7.3.1 and 7.3.4 */
15172967d56SGuido Günther dev_dbg(ctx->dev, "Resetting the panel\n");
15272967d56SGuido Günther ret = regulator_enable(ctx->vddi);
15372967d56SGuido Günther if (ret < 0) {
15472967d56SGuido Günther dev_err(ctx->dev, "Failed to enable vddi supply: %d\n", ret);
15572967d56SGuido Günther return ret;
15672967d56SGuido Günther }
15772967d56SGuido Günther
15872967d56SGuido Günther /* T1 + T2 */
15972967d56SGuido Günther usleep_range(8000, 10000);
16072967d56SGuido Günther
16172967d56SGuido Günther ret = regulator_enable(ctx->avdd);
16272967d56SGuido Günther if (ret < 0) {
16372967d56SGuido Günther dev_err(ctx->dev, "Failed to enable avdd supply: %d\n", ret);
16472967d56SGuido Günther return ret;
16572967d56SGuido Günther }
16672967d56SGuido Günther
16772967d56SGuido Günther /* T2d */
16872967d56SGuido Günther usleep_range(3500, 4000);
16972967d56SGuido Günther ret = regulator_enable(ctx->avee);
17072967d56SGuido Günther if (ret < 0) {
17172967d56SGuido Günther dev_err(ctx->dev, "Failed to enable avee supply: %d\n", ret);
17272967d56SGuido Günther return ret;
17372967d56SGuido Günther }
17472967d56SGuido Günther
175787099f8SGuido Günther /* T3 + T4 + time for voltage to become stable: */
176787099f8SGuido Günther usleep_range(6000, 7000);
17772967d56SGuido Günther gpiod_set_value_cansleep(ctx->reset_gpio, 0);
178787099f8SGuido Günther gpiod_set_value_cansleep(ctx->tp_rstn_gpio, 0);
17972967d56SGuido Günther
18072967d56SGuido Günther /* T6 */
18172967d56SGuido Günther msleep(50);
18272967d56SGuido Günther
18372967d56SGuido Günther return 0;
18472967d56SGuido Günther }
18572967d56SGuido Günther
1866ae5837cSGuido Günther static const struct drm_display_mode default_mode_mantix = {
18772967d56SGuido Günther .hdisplay = 720,
18872967d56SGuido Günther .hsync_start = 720 + 45,
18972967d56SGuido Günther .hsync_end = 720 + 45 + 14,
19072967d56SGuido Günther .htotal = 720 + 45 + 14 + 25,
19172967d56SGuido Günther .vdisplay = 1440,
19272967d56SGuido Günther .vsync_start = 1440 + 130,
19372967d56SGuido Günther .vsync_end = 1440 + 130 + 8,
19472967d56SGuido Günther .vtotal = 1440 + 130 + 8 + 106,
19572967d56SGuido Günther .clock = 85298,
19672967d56SGuido Günther .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
19772967d56SGuido Günther .width_mm = 65,
19872967d56SGuido Günther .height_mm = 130,
19972967d56SGuido Günther };
20072967d56SGuido Günther
2014b2b869eSGuido Günther static const struct drm_display_mode default_mode_ys = {
2024b2b869eSGuido Günther .hdisplay = 720,
2034b2b869eSGuido Günther .hsync_start = 720 + 45,
2044b2b869eSGuido Günther .hsync_end = 720 + 45 + 14,
2054b2b869eSGuido Günther .htotal = 720 + 45 + 14 + 25,
2064b2b869eSGuido Günther .vdisplay = 1440,
2074b2b869eSGuido Günther .vsync_start = 1440 + 175,
2084b2b869eSGuido Günther .vsync_end = 1440 + 175 + 8,
2094b2b869eSGuido Günther .vtotal = 1440 + 175 + 8 + 50,
2104b2b869eSGuido Günther .clock = 85298,
2114b2b869eSGuido Günther .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
2124b2b869eSGuido Günther .width_mm = 65,
2134b2b869eSGuido Günther .height_mm = 130,
2144b2b869eSGuido Günther };
2154b2b869eSGuido Günther
2161311f3dfSGuido Günther static const u32 mantix_bus_formats[] = {
2171311f3dfSGuido Günther MEDIA_BUS_FMT_RGB888_1X24,
2181311f3dfSGuido Günther };
2191311f3dfSGuido Günther
mantix_get_modes(struct drm_panel * panel,struct drm_connector * connector)22072967d56SGuido Günther static int mantix_get_modes(struct drm_panel *panel,
22172967d56SGuido Günther struct drm_connector *connector)
22272967d56SGuido Günther {
22372967d56SGuido Günther struct mantix *ctx = panel_to_mantix(panel);
22472967d56SGuido Günther struct drm_display_mode *mode;
22572967d56SGuido Günther
2266ae5837cSGuido Günther mode = drm_mode_duplicate(connector->dev, ctx->default_mode);
22772967d56SGuido Günther if (!mode) {
22872967d56SGuido Günther dev_err(ctx->dev, "Failed to add mode %ux%u@%u\n",
2296ae5837cSGuido Günther ctx->default_mode->hdisplay, ctx->default_mode->vdisplay,
2306ae5837cSGuido Günther drm_mode_vrefresh(ctx->default_mode));
23172967d56SGuido Günther return -ENOMEM;
23272967d56SGuido Günther }
23372967d56SGuido Günther
23472967d56SGuido Günther drm_mode_set_name(mode);
23572967d56SGuido Günther
23672967d56SGuido Günther mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
23772967d56SGuido Günther connector->display_info.width_mm = mode->width_mm;
23872967d56SGuido Günther connector->display_info.height_mm = mode->height_mm;
23972967d56SGuido Günther drm_mode_probed_add(connector, mode);
24072967d56SGuido Günther
2411311f3dfSGuido Günther drm_display_info_set_bus_formats(&connector->display_info,
2421311f3dfSGuido Günther mantix_bus_formats,
2431311f3dfSGuido Günther ARRAY_SIZE(mantix_bus_formats));
2441311f3dfSGuido Günther
24572967d56SGuido Günther return 1;
24672967d56SGuido Günther }
24772967d56SGuido Günther
24872967d56SGuido Günther static const struct drm_panel_funcs mantix_drm_funcs = {
24972967d56SGuido Günther .disable = mantix_disable,
25072967d56SGuido Günther .unprepare = mantix_unprepare,
25172967d56SGuido Günther .prepare = mantix_prepare,
25272967d56SGuido Günther .enable = mantix_enable,
25372967d56SGuido Günther .get_modes = mantix_get_modes,
25472967d56SGuido Günther };
25572967d56SGuido Günther
mantix_probe(struct mipi_dsi_device * dsi)25672967d56SGuido Günther static int mantix_probe(struct mipi_dsi_device *dsi)
25772967d56SGuido Günther {
25872967d56SGuido Günther struct device *dev = &dsi->dev;
25972967d56SGuido Günther struct mantix *ctx;
26072967d56SGuido Günther int ret;
26172967d56SGuido Günther
26272967d56SGuido Günther ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
26372967d56SGuido Günther if (!ctx)
26472967d56SGuido Günther return -ENOMEM;
2656ae5837cSGuido Günther ctx->default_mode = of_device_get_match_data(dev);
26672967d56SGuido Günther
267787099f8SGuido Günther ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
26872967d56SGuido Günther if (IS_ERR(ctx->reset_gpio)) {
26972967d56SGuido Günther dev_err(dev, "cannot get reset gpio\n");
27072967d56SGuido Günther return PTR_ERR(ctx->reset_gpio);
27172967d56SGuido Günther }
27272967d56SGuido Günther
273787099f8SGuido Günther ctx->tp_rstn_gpio = devm_gpiod_get(dev, "mantix,tp-rstn", GPIOD_OUT_HIGH);
274787099f8SGuido Günther if (IS_ERR(ctx->tp_rstn_gpio)) {
275787099f8SGuido Günther dev_err(dev, "cannot get tp-rstn gpio\n");
276787099f8SGuido Günther return PTR_ERR(ctx->tp_rstn_gpio);
277787099f8SGuido Günther }
278787099f8SGuido Günther
27972967d56SGuido Günther mipi_dsi_set_drvdata(dsi, ctx);
28072967d56SGuido Günther ctx->dev = dev;
28172967d56SGuido Günther
28272967d56SGuido Günther dsi->lanes = 4;
28372967d56SGuido Günther dsi->format = MIPI_DSI_FMT_RGB888;
28472967d56SGuido Günther dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
28572967d56SGuido Günther MIPI_DSI_MODE_VIDEO_BURST | MIPI_DSI_MODE_VIDEO_SYNC_PULSE;
28672967d56SGuido Günther
28772967d56SGuido Günther ctx->avdd = devm_regulator_get(dev, "avdd");
28872967d56SGuido Günther if (IS_ERR(ctx->avdd))
28972967d56SGuido Günther return dev_err_probe(dev, PTR_ERR(ctx->avdd), "Failed to request avdd regulator\n");
29072967d56SGuido Günther
29172967d56SGuido Günther ctx->avee = devm_regulator_get(dev, "avee");
29272967d56SGuido Günther if (IS_ERR(ctx->avee))
29372967d56SGuido Günther return dev_err_probe(dev, PTR_ERR(ctx->avee), "Failed to request avee regulator\n");
29472967d56SGuido Günther
29572967d56SGuido Günther ctx->vddi = devm_regulator_get(dev, "vddi");
29672967d56SGuido Günther if (IS_ERR(ctx->vddi))
29772967d56SGuido Günther return dev_err_probe(dev, PTR_ERR(ctx->vddi), "Failed to request vddi regulator\n");
29872967d56SGuido Günther
29972967d56SGuido Günther drm_panel_init(&ctx->panel, dev, &mantix_drm_funcs,
30072967d56SGuido Günther DRM_MODE_CONNECTOR_DSI);
30172967d56SGuido Günther
30272967d56SGuido Günther ret = drm_panel_of_backlight(&ctx->panel);
30372967d56SGuido Günther if (ret)
30472967d56SGuido Günther return ret;
30572967d56SGuido Günther
30672967d56SGuido Günther drm_panel_add(&ctx->panel);
30772967d56SGuido Günther
30872967d56SGuido Günther ret = mipi_dsi_attach(dsi);
30972967d56SGuido Günther if (ret < 0) {
31072967d56SGuido Günther dev_err(dev, "mipi_dsi_attach failed (%d). Is host ready?\n", ret);
31172967d56SGuido Günther drm_panel_remove(&ctx->panel);
31272967d56SGuido Günther return ret;
31372967d56SGuido Günther }
31472967d56SGuido Günther
31572967d56SGuido Günther dev_info(dev, "%ux%u@%u %ubpp dsi %udl - ready\n",
3166ae5837cSGuido Günther ctx->default_mode->hdisplay, ctx->default_mode->vdisplay,
3176ae5837cSGuido Günther drm_mode_vrefresh(ctx->default_mode),
31872967d56SGuido Günther mipi_dsi_pixel_format_to_bpp(dsi->format), dsi->lanes);
31972967d56SGuido Günther
32072967d56SGuido Günther return 0;
32172967d56SGuido Günther }
32272967d56SGuido Günther
mantix_shutdown(struct mipi_dsi_device * dsi)32372967d56SGuido Günther static void mantix_shutdown(struct mipi_dsi_device *dsi)
32472967d56SGuido Günther {
32572967d56SGuido Günther struct mantix *ctx = mipi_dsi_get_drvdata(dsi);
32672967d56SGuido Günther
32772967d56SGuido Günther drm_panel_unprepare(&ctx->panel);
32872967d56SGuido Günther drm_panel_disable(&ctx->panel);
32972967d56SGuido Günther }
33072967d56SGuido Günther
mantix_remove(struct mipi_dsi_device * dsi)33179abca2bSUwe Kleine-König static void mantix_remove(struct mipi_dsi_device *dsi)
33272967d56SGuido Günther {
33372967d56SGuido Günther struct mantix *ctx = mipi_dsi_get_drvdata(dsi);
33472967d56SGuido Günther
33572967d56SGuido Günther mantix_shutdown(dsi);
33672967d56SGuido Günther
33772967d56SGuido Günther mipi_dsi_detach(dsi);
33872967d56SGuido Günther drm_panel_remove(&ctx->panel);
33972967d56SGuido Günther }
34072967d56SGuido Günther
34172967d56SGuido Günther static const struct of_device_id mantix_of_match[] = {
3426ae5837cSGuido Günther { .compatible = "mantix,mlaf057we51-x", .data = &default_mode_mantix },
3434b2b869eSGuido Günther { .compatible = "ys,ys57pss36bh5gq", .data = &default_mode_ys },
34472967d56SGuido Günther { /* sentinel */ }
34572967d56SGuido Günther };
34672967d56SGuido Günther MODULE_DEVICE_TABLE(of, mantix_of_match);
34772967d56SGuido Günther
34872967d56SGuido Günther static struct mipi_dsi_driver mantix_driver = {
34972967d56SGuido Günther .probe = mantix_probe,
35072967d56SGuido Günther .remove = mantix_remove,
35172967d56SGuido Günther .shutdown = mantix_shutdown,
35272967d56SGuido Günther .driver = {
35372967d56SGuido Günther .name = DRV_NAME,
35472967d56SGuido Günther .of_match_table = mantix_of_match,
35572967d56SGuido Günther },
35672967d56SGuido Günther };
35772967d56SGuido Günther module_mipi_dsi_driver(mantix_driver);
35872967d56SGuido Günther
35972967d56SGuido Günther MODULE_AUTHOR("Guido Günther <agx@sigxcpu.org>");
36072967d56SGuido Günther MODULE_DESCRIPTION("DRM driver for Mantix MLAF057WE51-X MIPI DSI panel");
36172967d56SGuido Günther MODULE_LICENSE("GPL v2");
362