Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Mantix MLAF057WE51 5.7" MIPI-DSI panel driver
0004  *
0005  * Copyright (C) Purism SPC 2020
0006  */
0007 
0008 #include <linux/backlight.h>
0009 #include <linux/delay.h>
0010 #include <linux/gpio/consumer.h>
0011 #include <linux/media-bus-format.h>
0012 #include <linux/module.h>
0013 #include <linux/of_device.h>
0014 #include <linux/regulator/consumer.h>
0015 
0016 #include <video/mipi_display.h>
0017 
0018 #include <drm/drm_mipi_dsi.h>
0019 #include <drm/drm_modes.h>
0020 #include <drm/drm_panel.h>
0021 
0022 #define DRV_NAME "panel-mantix-mlaf057we51"
0023 
0024 /* Manufacturer specific Commands send via DSI */
0025 #define MANTIX_CMD_OTP_STOP_RELOAD_MIPI 0x41
0026 #define MANTIX_CMD_INT_CANCEL           0x4C
0027 #define MANTIX_CMD_SPI_FINISH           0x90
0028 
0029 struct mantix {
0030     struct device *dev;
0031     struct drm_panel panel;
0032 
0033     struct gpio_desc *reset_gpio;
0034     struct gpio_desc *tp_rstn_gpio;
0035 
0036     struct regulator *avdd;
0037     struct regulator *avee;
0038     struct regulator *vddi;
0039 
0040     const struct drm_display_mode *default_mode;
0041 };
0042 
0043 static inline struct mantix *panel_to_mantix(struct drm_panel *panel)
0044 {
0045     return container_of(panel, struct mantix, panel);
0046 }
0047 
0048 #define dsi_generic_write_seq(dsi, seq...) do {             \
0049         static const u8 d[] = { seq };              \
0050         int ret;                        \
0051         ret = mipi_dsi_generic_write(dsi, d, ARRAY_SIZE(d));    \
0052         if (ret < 0)                        \
0053             return ret;                 \
0054     } while (0)
0055 
0056 static int mantix_init_sequence(struct mantix *ctx)
0057 {
0058     struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
0059     struct device *dev = ctx->dev;
0060 
0061     /*
0062      * Init sequence was supplied by the panel vendor.
0063      */
0064     dsi_generic_write_seq(dsi, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x5A);
0065 
0066     dsi_generic_write_seq(dsi, MANTIX_CMD_INT_CANCEL, 0x03);
0067     dsi_generic_write_seq(dsi, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x5A, 0x03);
0068     dsi_generic_write_seq(dsi, 0x80, 0xA9, 0x00);
0069 
0070     dsi_generic_write_seq(dsi, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x5A, 0x09);
0071     dsi_generic_write_seq(dsi, 0x80, 0x64, 0x00, 0x64, 0x00, 0x00);
0072     msleep(20);
0073 
0074     dsi_generic_write_seq(dsi, MANTIX_CMD_SPI_FINISH, 0xA5);
0075     dsi_generic_write_seq(dsi, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x00, 0x2F);
0076     msleep(20);
0077 
0078     dev_dbg(dev, "Panel init sequence done\n");
0079     return 0;
0080 }
0081 
0082 static int mantix_enable(struct drm_panel *panel)
0083 {
0084     struct mantix *ctx = panel_to_mantix(panel);
0085     struct device *dev = ctx->dev;
0086     struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
0087     int ret;
0088 
0089     ret = mantix_init_sequence(ctx);
0090     if (ret < 0) {
0091         dev_err(ctx->dev, "Panel init sequence failed: %d\n", ret);
0092         return ret;
0093     }
0094 
0095     ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
0096     if (ret < 0) {
0097         dev_err(dev, "Failed to exit sleep mode\n");
0098         return ret;
0099     }
0100     msleep(20);
0101 
0102     ret = mipi_dsi_dcs_set_display_on(dsi);
0103     if (ret)
0104         return ret;
0105     usleep_range(10000, 12000);
0106 
0107     ret = mipi_dsi_turn_on_peripheral(dsi);
0108     if (ret < 0) {
0109         dev_err(dev, "Failed to turn on peripheral\n");
0110         return ret;
0111     }
0112 
0113     return 0;
0114 }
0115 
0116 static int mantix_disable(struct drm_panel *panel)
0117 {
0118     struct mantix *ctx = panel_to_mantix(panel);
0119     struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
0120     int ret;
0121 
0122     ret = mipi_dsi_dcs_set_display_off(dsi);
0123     if (ret < 0)
0124         dev_err(ctx->dev, "Failed to turn off the display: %d\n", ret);
0125 
0126     ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
0127     if (ret < 0)
0128         dev_err(ctx->dev, "Failed to enter sleep mode: %d\n", ret);
0129 
0130 
0131     return 0;
0132 }
0133 
0134 static int mantix_unprepare(struct drm_panel *panel)
0135 {
0136     struct mantix *ctx = panel_to_mantix(panel);
0137 
0138     gpiod_set_value_cansleep(ctx->tp_rstn_gpio, 1);
0139     usleep_range(5000, 6000);
0140     gpiod_set_value_cansleep(ctx->reset_gpio, 1);
0141 
0142     regulator_disable(ctx->avee);
0143     regulator_disable(ctx->avdd);
0144     /* T11 */
0145     usleep_range(5000, 6000);
0146     regulator_disable(ctx->vddi);
0147     /* T14 */
0148     msleep(50);
0149 
0150     return 0;
0151 }
0152 
0153 static int mantix_prepare(struct drm_panel *panel)
0154 {
0155     struct mantix *ctx = panel_to_mantix(panel);
0156     int ret;
0157 
0158     /* Focaltech FT8006P, section 7.3.1 and 7.3.4 */
0159     dev_dbg(ctx->dev, "Resetting the panel\n");
0160     ret = regulator_enable(ctx->vddi);
0161     if (ret < 0) {
0162         dev_err(ctx->dev, "Failed to enable vddi supply: %d\n", ret);
0163         return ret;
0164     }
0165 
0166     /* T1 + T2 */
0167     usleep_range(8000, 10000);
0168 
0169     ret = regulator_enable(ctx->avdd);
0170     if (ret < 0) {
0171         dev_err(ctx->dev, "Failed to enable avdd supply: %d\n", ret);
0172         return ret;
0173     }
0174 
0175     /* T2d */
0176     usleep_range(3500, 4000);
0177     ret = regulator_enable(ctx->avee);
0178     if (ret < 0) {
0179         dev_err(ctx->dev, "Failed to enable avee supply: %d\n", ret);
0180         return ret;
0181     }
0182 
0183     /* T3 + T4 + time for voltage to become stable: */
0184     usleep_range(6000, 7000);
0185     gpiod_set_value_cansleep(ctx->reset_gpio, 0);
0186     gpiod_set_value_cansleep(ctx->tp_rstn_gpio, 0);
0187 
0188     /* T6 */
0189     msleep(50);
0190 
0191     return 0;
0192 }
0193 
0194 static const struct drm_display_mode default_mode_mantix = {
0195     .hdisplay    = 720,
0196     .hsync_start = 720 + 45,
0197     .hsync_end   = 720 + 45 + 14,
0198     .htotal      = 720 + 45 + 14 + 25,
0199     .vdisplay    = 1440,
0200     .vsync_start = 1440 + 130,
0201     .vsync_end   = 1440 + 130 + 8,
0202     .vtotal      = 1440 + 130 + 8 + 106,
0203     .clock       = 85298,
0204     .flags       = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
0205     .width_mm    = 65,
0206     .height_mm   = 130,
0207 };
0208 
0209 static const struct drm_display_mode default_mode_ys = {
0210     .hdisplay    = 720,
0211     .hsync_start = 720 + 45,
0212     .hsync_end   = 720 + 45 + 14,
0213     .htotal      = 720 + 45 + 14 + 25,
0214     .vdisplay    = 1440,
0215     .vsync_start = 1440 + 175,
0216     .vsync_end   = 1440 + 175 + 8,
0217     .vtotal      = 1440 + 175 + 8 + 50,
0218     .clock       = 85298,
0219     .flags       = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
0220     .width_mm    = 65,
0221     .height_mm   = 130,
0222 };
0223 
0224 static const u32 mantix_bus_formats[] = {
0225     MEDIA_BUS_FMT_RGB888_1X24,
0226 };
0227 
0228 static int mantix_get_modes(struct drm_panel *panel,
0229                 struct drm_connector *connector)
0230 {
0231     struct mantix *ctx = panel_to_mantix(panel);
0232     struct drm_display_mode *mode;
0233 
0234     mode = drm_mode_duplicate(connector->dev, ctx->default_mode);
0235     if (!mode) {
0236         dev_err(ctx->dev, "Failed to add mode %ux%u@%u\n",
0237             ctx->default_mode->hdisplay, ctx->default_mode->vdisplay,
0238             drm_mode_vrefresh(ctx->default_mode));
0239         return -ENOMEM;
0240     }
0241 
0242     drm_mode_set_name(mode);
0243 
0244     mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
0245     connector->display_info.width_mm = mode->width_mm;
0246     connector->display_info.height_mm = mode->height_mm;
0247     drm_mode_probed_add(connector, mode);
0248 
0249     drm_display_info_set_bus_formats(&connector->display_info,
0250                      mantix_bus_formats,
0251                      ARRAY_SIZE(mantix_bus_formats));
0252 
0253     return 1;
0254 }
0255 
0256 static const struct drm_panel_funcs mantix_drm_funcs = {
0257     .disable   = mantix_disable,
0258     .unprepare = mantix_unprepare,
0259     .prepare   = mantix_prepare,
0260     .enable    = mantix_enable,
0261     .get_modes = mantix_get_modes,
0262 };
0263 
0264 static int mantix_probe(struct mipi_dsi_device *dsi)
0265 {
0266     struct device *dev = &dsi->dev;
0267     struct mantix *ctx;
0268     int ret;
0269 
0270     ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
0271     if (!ctx)
0272         return -ENOMEM;
0273     ctx->default_mode = of_device_get_match_data(dev);
0274 
0275     ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
0276     if (IS_ERR(ctx->reset_gpio)) {
0277         dev_err(dev, "cannot get reset gpio\n");
0278         return PTR_ERR(ctx->reset_gpio);
0279     }
0280 
0281     ctx->tp_rstn_gpio = devm_gpiod_get(dev, "mantix,tp-rstn", GPIOD_OUT_HIGH);
0282     if (IS_ERR(ctx->tp_rstn_gpio)) {
0283         dev_err(dev, "cannot get tp-rstn gpio\n");
0284         return PTR_ERR(ctx->tp_rstn_gpio);
0285     }
0286 
0287     mipi_dsi_set_drvdata(dsi, ctx);
0288     ctx->dev = dev;
0289 
0290     dsi->lanes = 4;
0291     dsi->format = MIPI_DSI_FMT_RGB888;
0292     dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
0293         MIPI_DSI_MODE_VIDEO_BURST | MIPI_DSI_MODE_VIDEO_SYNC_PULSE;
0294 
0295     ctx->avdd = devm_regulator_get(dev, "avdd");
0296     if (IS_ERR(ctx->avdd))
0297         return dev_err_probe(dev, PTR_ERR(ctx->avdd), "Failed to request avdd regulator\n");
0298 
0299     ctx->avee = devm_regulator_get(dev, "avee");
0300     if (IS_ERR(ctx->avee))
0301         return dev_err_probe(dev, PTR_ERR(ctx->avee), "Failed to request avee regulator\n");
0302 
0303     ctx->vddi = devm_regulator_get(dev, "vddi");
0304     if (IS_ERR(ctx->vddi))
0305         return dev_err_probe(dev, PTR_ERR(ctx->vddi), "Failed to request vddi regulator\n");
0306 
0307     drm_panel_init(&ctx->panel, dev, &mantix_drm_funcs,
0308                DRM_MODE_CONNECTOR_DSI);
0309 
0310     ret = drm_panel_of_backlight(&ctx->panel);
0311     if (ret)
0312         return ret;
0313 
0314     drm_panel_add(&ctx->panel);
0315 
0316     ret = mipi_dsi_attach(dsi);
0317     if (ret < 0) {
0318         dev_err(dev, "mipi_dsi_attach failed (%d). Is host ready?\n", ret);
0319         drm_panel_remove(&ctx->panel);
0320         return ret;
0321     }
0322 
0323     dev_info(dev, "%ux%u@%u %ubpp dsi %udl - ready\n",
0324          ctx->default_mode->hdisplay, ctx->default_mode->vdisplay,
0325          drm_mode_vrefresh(ctx->default_mode),
0326          mipi_dsi_pixel_format_to_bpp(dsi->format), dsi->lanes);
0327 
0328     return 0;
0329 }
0330 
0331 static void mantix_shutdown(struct mipi_dsi_device *dsi)
0332 {
0333     struct mantix *ctx = mipi_dsi_get_drvdata(dsi);
0334 
0335     drm_panel_unprepare(&ctx->panel);
0336     drm_panel_disable(&ctx->panel);
0337 }
0338 
0339 static int mantix_remove(struct mipi_dsi_device *dsi)
0340 {
0341     struct mantix *ctx = mipi_dsi_get_drvdata(dsi);
0342 
0343     mantix_shutdown(dsi);
0344 
0345     mipi_dsi_detach(dsi);
0346     drm_panel_remove(&ctx->panel);
0347 
0348     return 0;
0349 }
0350 
0351 static const struct of_device_id mantix_of_match[] = {
0352     { .compatible = "mantix,mlaf057we51-x", .data = &default_mode_mantix },
0353     { .compatible = "ys,ys57pss36bh5gq", .data = &default_mode_ys },
0354     { /* sentinel */ }
0355 };
0356 MODULE_DEVICE_TABLE(of, mantix_of_match);
0357 
0358 static struct mipi_dsi_driver mantix_driver = {
0359     .probe  = mantix_probe,
0360     .remove = mantix_remove,
0361     .shutdown = mantix_shutdown,
0362     .driver = {
0363         .name = DRV_NAME,
0364         .of_match_table = mantix_of_match,
0365     },
0366 };
0367 module_mipi_dsi_driver(mantix_driver);
0368 
0369 MODULE_AUTHOR("Guido Günther <agx@sigxcpu.org>");
0370 MODULE_DESCRIPTION("DRM driver for Mantix MLAF057WE51-X MIPI DSI panel");
0371 MODULE_LICENSE("GPL v2");