1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include <linux/array_size.h> 4 #include <linux/delay.h> 5 #include <linux/err.h> 6 #include <linux/gpio/consumer.h> 7 #include <linux/mod_devicetable.h> 8 #include <linux/module.h> 9 #include <linux/property.h> 10 #include <linux/regulator/consumer.h> 11 12 #include <video/mipi_display.h> 13 14 #include <drm/drm_mipi_dsi.h> 15 #include <drm/drm_modes.h> 16 #include <drm/drm_panel.h> 17 #include <drm/drm_probe_helper.h> 18 19 static const struct regulator_bulk_data lg_ld070wx3_supplies[] = { 20 { .supply = "vdd" }, { .supply = "vcc" }, 21 }; 22 23 struct lg_ld070wx3 { 24 struct drm_panel panel; 25 struct mipi_dsi_device *dsi; 26 27 struct regulator_bulk_data *supplies; 28 }; 29 30 static inline struct lg_ld070wx3 *to_lg_ld070wx3(struct drm_panel *panel) 31 { 32 return container_of(panel, struct lg_ld070wx3, panel); 33 } 34 35 static int lg_ld070wx3_prepare(struct drm_panel *panel) 36 { 37 struct lg_ld070wx3 *priv = to_lg_ld070wx3(panel); 38 struct mipi_dsi_multi_context ctx = { .dsi = priv->dsi }; 39 struct device *dev = panel->dev; 40 int ret; 41 42 ret = regulator_bulk_enable(ARRAY_SIZE(lg_ld070wx3_supplies), priv->supplies); 43 if (ret < 0) { 44 dev_err(dev, "failed to enable power supplies: %d\n", ret); 45 return ret; 46 } 47 48 /* 49 * According to spec delay between enabling supply is 0, 50 * for regulators to reach required voltage ~5ms needed. 51 * MIPI interface signal for setup requires additional 52 * 110ms which in total results in 115ms. 53 */ 54 mdelay(115); 55 56 mipi_dsi_dcs_soft_reset_multi(&ctx); 57 mipi_dsi_msleep(&ctx, 20); 58 59 /* Differential input impedance selection */ 60 mipi_dsi_dcs_write_seq_multi(&ctx, 0xae, 0x0b); 61 62 /* Enter test mode 1 and 2*/ 63 mipi_dsi_dcs_write_seq_multi(&ctx, 0xee, 0xea); 64 mipi_dsi_dcs_write_seq_multi(&ctx, 0xef, 0x5f); 65 66 /* Increased MIPI CLK driving ability */ 67 mipi_dsi_dcs_write_seq_multi(&ctx, 0xf2, 0x68); 68 69 /* Exit test mode 1 and 2 */ 70 mipi_dsi_dcs_write_seq_multi(&ctx, 0xee, 0x00); 71 mipi_dsi_dcs_write_seq_multi(&ctx, 0xef, 0x00); 72 73 return ctx.accum_err; 74 } 75 76 static int lg_ld070wx3_unprepare(struct drm_panel *panel) 77 { 78 struct lg_ld070wx3 *priv = to_lg_ld070wx3(panel); 79 struct mipi_dsi_multi_context ctx = { .dsi = priv->dsi }; 80 81 mipi_dsi_dcs_enter_sleep_mode_multi(&ctx); 82 83 msleep(50); 84 85 regulator_bulk_disable(ARRAY_SIZE(lg_ld070wx3_supplies), priv->supplies); 86 87 /* power supply must be off for at least 1s after panel disable */ 88 msleep(1000); 89 90 return 0; 91 } 92 93 static const struct drm_display_mode lg_ld070wx3_mode = { 94 .clock = (800 + 32 + 48 + 8) * (1280 + 5 + 3 + 1) * 60 / 1000, 95 .hdisplay = 800, 96 .hsync_start = 800 + 32, 97 .hsync_end = 800 + 32 + 48, 98 .htotal = 800 + 32 + 48 + 8, 99 .vdisplay = 1280, 100 .vsync_start = 1280 + 5, 101 .vsync_end = 1280 + 5 + 3, 102 .vtotal = 1280 + 5 + 3 + 1, 103 .width_mm = 94, 104 .height_mm = 151, 105 .type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED, 106 }; 107 108 static int lg_ld070wx3_get_modes(struct drm_panel *panel, 109 struct drm_connector *connector) 110 { 111 return drm_connector_helper_get_modes_fixed(connector, &lg_ld070wx3_mode); 112 } 113 114 static const struct drm_panel_funcs lg_ld070wx3_panel_funcs = { 115 .prepare = lg_ld070wx3_prepare, 116 .unprepare = lg_ld070wx3_unprepare, 117 .get_modes = lg_ld070wx3_get_modes, 118 }; 119 120 static int lg_ld070wx3_probe(struct mipi_dsi_device *dsi) 121 { 122 struct device *dev = &dsi->dev; 123 struct lg_ld070wx3 *priv; 124 int ret; 125 126 priv = devm_drm_panel_alloc(dev, struct lg_ld070wx3, panel, 127 &lg_ld070wx3_panel_funcs, 128 DRM_MODE_CONNECTOR_DSI); 129 if (IS_ERR(priv)) 130 return PTR_ERR(priv); 131 132 ret = devm_regulator_bulk_get_const(dev, ARRAY_SIZE(lg_ld070wx3_supplies), 133 lg_ld070wx3_supplies, &priv->supplies); 134 if (ret < 0) 135 return dev_err_probe(dev, ret, "failed to get supplies\n"); 136 137 priv->dsi = dsi; 138 mipi_dsi_set_drvdata(dsi, priv); 139 140 dsi->lanes = 4; 141 dsi->format = MIPI_DSI_FMT_RGB888; 142 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_LPM; 143 144 ret = drm_panel_of_backlight(&priv->panel); 145 if (ret < 0) 146 return dev_err_probe(dev, ret, "failed to get backlight\n"); 147 148 drm_panel_add(&priv->panel); 149 150 ret = devm_mipi_dsi_attach(dev, dsi); 151 if (ret < 0) { 152 drm_panel_remove(&priv->panel); 153 return dev_err_probe(dev, ret, "failed to attach to DSI host\n"); 154 } 155 156 return 0; 157 } 158 159 static void lg_ld070wx3_remove(struct mipi_dsi_device *dsi) 160 { 161 struct lg_ld070wx3 *priv = mipi_dsi_get_drvdata(dsi); 162 163 drm_panel_remove(&priv->panel); 164 } 165 166 static const struct of_device_id lg_ld070wx3_of_match[] = { 167 { .compatible = "lg,ld070wx3-sl01" }, 168 { /* sentinel */ } 169 }; 170 MODULE_DEVICE_TABLE(of, lg_ld070wx3_of_match); 171 172 static struct mipi_dsi_driver lg_ld070wx3_driver = { 173 .driver = { 174 .name = "panel-lg-ld070wx3", 175 .of_match_table = lg_ld070wx3_of_match, 176 }, 177 .probe = lg_ld070wx3_probe, 178 .remove = lg_ld070wx3_remove, 179 }; 180 module_mipi_dsi_driver(lg_ld070wx3_driver); 181 182 MODULE_AUTHOR("Svyatoslav Ryhel <clamor95@gmail.com>"); 183 MODULE_DESCRIPTION("LG LD070WX3-SL01 DSI panel driver"); 184 MODULE_LICENSE("GPL"); 185