1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2024, The Linux Foundation. All rights reserved. 4 * Generated with linux-mdss-dsi-panel-driver-generator from vendor device tree: 5 * Copyright (c) 2013, The Linux Foundation. All rights reserved. 6 */ 7 8 #include <linux/backlight.h> 9 #include <linux/delay.h> 10 #include <linux/gpio/consumer.h> 11 #include <linux/module.h> 12 #include <linux/regulator/consumer.h> 13 #include <linux/of.h> 14 15 #include <video/mipi_display.h> 16 17 #include <drm/drm_mipi_dsi.h> 18 #include <drm/drm_modes.h> 19 #include <drm/drm_panel.h> 20 #include <drm/drm_probe_helper.h> 21 22 struct nt37700f_tianma { 23 struct drm_panel panel; 24 struct mipi_dsi_device *dsi; 25 struct regulator *supply; 26 struct gpio_desc *reset_gpio; 27 }; 28 29 static inline 30 struct nt37700f_tianma *to_nt37700f_tianma(struct drm_panel *panel) 31 { 32 return container_of(panel, struct nt37700f_tianma, panel); 33 } 34 35 static void nt37700f_tianma_reset(struct nt37700f_tianma *ctx) 36 { 37 gpiod_set_value_cansleep(ctx->reset_gpio, 1); 38 usleep_range(1000, 2000); 39 gpiod_set_value_cansleep(ctx->reset_gpio, 0); 40 usleep_range(10000, 11000); 41 } 42 43 #define nt37700f_tianma_panel_switch_page(ctx, page) \ 44 mipi_dsi_dcs_write_seq_multi((ctx), 0xf0, 0x55, 0xaa, 0x52, 0x08, (page)) 45 46 static int nt37700f_tianma_on(struct nt37700f_tianma *ctx) 47 { 48 struct mipi_dsi_device *dsi = ctx->dsi; 49 struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi }; 50 51 nt37700f_tianma_panel_switch_page(&dsi_ctx, 0x00); 52 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xc0, 0x56); 53 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xca, 0x52); 54 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0x6f, 0x06); 55 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xb5, 0x2b, 0x1a); 56 nt37700f_tianma_panel_switch_page(&dsi_ctx, 0x01); 57 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xcd, 0x04, 0x82); 58 nt37700f_tianma_panel_switch_page(&dsi_ctx, 0x02); 59 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xcc, 0x00); 60 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xff, 0xaa, 0x55, 0xa5, 0x80); 61 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0x6f, 0x55); 62 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xf6, 0x00); 63 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0x6f, 0x56); 64 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xf6, 0x00); 65 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xff, 0xaa, 0x55, 0xa5, 0x81); 66 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0x6f, 0x07); 67 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xf3, 0x07); 68 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0x6f, 0x05); 69 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xf3, 0x25); 70 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0x90, 0x01); 71 72 mipi_dsi_dcs_set_column_address_multi(&dsi_ctx, 0x0000, 1080 - 1); 73 mipi_dsi_dcs_set_page_address_multi(&dsi_ctx, 0x0000, 2160 - 1); 74 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x20); 75 mipi_dsi_dcs_set_tear_on_multi(&dsi_ctx, MIPI_DSI_DCS_TEAR_MODE_VBLANK); 76 77 nt37700f_tianma_panel_switch_page(&dsi_ctx, 0x00); 78 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xc0, 0x56); 79 nt37700f_tianma_panel_switch_page(&dsi_ctx, 0x02); 80 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xcd, 0x00); 81 nt37700f_tianma_panel_switch_page(&dsi_ctx, 0x04); 82 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xd0, 0x11, 0x64); 83 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0x6f, 0x09); 84 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xb1, 0x20); 85 86 mipi_dsi_dcs_exit_sleep_mode_multi(&dsi_ctx); 87 mipi_dsi_msleep(&dsi_ctx, 120); 88 mipi_dsi_dcs_set_display_on_multi(&dsi_ctx); 89 90 return dsi_ctx.accum_err; 91 } 92 93 static int nt37700f_tianma_disable(struct drm_panel *panel) 94 { 95 struct nt37700f_tianma *ctx = to_nt37700f_tianma(panel); 96 struct mipi_dsi_device *dsi = ctx->dsi; 97 struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi }; 98 99 mipi_dsi_dcs_set_display_off_multi(&dsi_ctx); 100 mipi_dsi_msleep(&dsi_ctx, 50); 101 mipi_dsi_dcs_enter_sleep_mode_multi(&dsi_ctx); 102 mipi_dsi_msleep(&dsi_ctx, 100); 103 104 return dsi_ctx.accum_err; 105 } 106 107 static int nt37700f_tianma_prepare(struct drm_panel *panel) 108 { 109 struct nt37700f_tianma *ctx = to_nt37700f_tianma(panel); 110 struct device *dev = &ctx->dsi->dev; 111 int ret; 112 113 ret = regulator_enable(ctx->supply); 114 if (ret < 0) { 115 dev_err(dev, "Failed to enable power supply: %d\n", ret); 116 return ret; 117 } 118 119 nt37700f_tianma_reset(ctx); 120 121 ret = nt37700f_tianma_on(ctx); 122 if (ret < 0) { 123 dev_err(dev, "Failed to initialize panel: %d\n", ret); 124 gpiod_set_value_cansleep(ctx->reset_gpio, 1); 125 return ret; 126 } 127 128 return 0; 129 } 130 131 static int nt37700f_tianma_unprepare(struct drm_panel *panel) 132 { 133 struct nt37700f_tianma *ctx = to_nt37700f_tianma(panel); 134 135 gpiod_set_value_cansleep(ctx->reset_gpio, 1); 136 regulator_disable(ctx->supply); 137 138 return 0; 139 } 140 141 static const struct drm_display_mode nt37700f_tianma_mode = { 142 .clock = (1080 + 32 + 32 + 98) * (2160 + 32 + 4 + 98) * 60 / 1000, 143 .hdisplay = 1080, 144 .hsync_start = 1080 + 32, 145 .hsync_end = 1080 + 32 + 32, 146 .htotal = 1080 + 32 + 32 + 98, 147 .vdisplay = 2160, 148 .vsync_start = 2160 + 32, 149 .vsync_end = 2160 + 32 + 4, 150 .vtotal = 2160 + 32 + 4 + 98, 151 .width_mm = 69, 152 .height_mm = 137, 153 .type = DRM_MODE_TYPE_DRIVER, 154 }; 155 156 static int nt37700f_tianma_get_modes(struct drm_panel *panel, 157 struct drm_connector *connector) 158 { 159 return drm_connector_helper_get_modes_fixed(connector, &nt37700f_tianma_mode); 160 } 161 162 static const struct drm_panel_funcs nt37700f_tianma_panel_funcs = { 163 .prepare = nt37700f_tianma_prepare, 164 .unprepare = nt37700f_tianma_unprepare, 165 .disable = nt37700f_tianma_disable, 166 .get_modes = nt37700f_tianma_get_modes, 167 }; 168 169 static int nt37700f_tianma_bl_update_status(struct backlight_device *bl) 170 { 171 struct mipi_dsi_device *dsi = bl_get_data(bl); 172 u16 brightness = backlight_get_brightness(bl); 173 int ret; 174 175 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM; 176 177 ret = mipi_dsi_dcs_set_display_brightness_large(dsi, brightness); 178 if (ret < 0) 179 return ret; 180 181 dsi->mode_flags |= MIPI_DSI_MODE_LPM; 182 183 return 0; 184 } 185 186 static int nt37700f_tianma_bl_get_brightness(struct backlight_device *bl) 187 { 188 struct mipi_dsi_device *dsi = bl_get_data(bl); 189 u16 brightness; 190 int ret; 191 192 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM; 193 194 ret = mipi_dsi_dcs_get_display_brightness_large(dsi, &brightness); 195 if (ret < 0) 196 return ret; 197 198 dsi->mode_flags |= MIPI_DSI_MODE_LPM; 199 200 return brightness; 201 } 202 203 static const struct backlight_ops nt37700f_tianma_bl_ops = { 204 .update_status = nt37700f_tianma_bl_update_status, 205 .get_brightness = nt37700f_tianma_bl_get_brightness, 206 }; 207 208 static struct backlight_device * 209 nt37700f_tianma_create_backlight(struct mipi_dsi_device *dsi) 210 { 211 struct device *dev = &dsi->dev; 212 const struct backlight_properties props = { 213 .type = BACKLIGHT_RAW, 214 .brightness = 2047, 215 .max_brightness = 2047, 216 }; 217 218 return devm_backlight_device_register(dev, dev_name(dev), dev, dsi, 219 &nt37700f_tianma_bl_ops, &props); 220 } 221 222 static int nt37700f_tianma_probe(struct mipi_dsi_device *dsi) 223 { 224 struct device *dev = &dsi->dev; 225 struct nt37700f_tianma *ctx; 226 int ret; 227 228 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); 229 if (!ctx) 230 return -ENOMEM; 231 232 ctx->supply = devm_regulator_get(dev, "power"); 233 if (IS_ERR(ctx->supply)) 234 return dev_err_probe(dev, PTR_ERR(ctx->supply), 235 "Failed to get power-supply\n"); 236 237 ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 238 if (IS_ERR(ctx->reset_gpio)) 239 return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio), 240 "Failed to get reset-gpios\n"); 241 242 ctx->dsi = dsi; 243 mipi_dsi_set_drvdata(dsi, ctx); 244 245 dsi->lanes = 4; 246 dsi->format = MIPI_DSI_FMT_RGB888; 247 dsi->mode_flags = MIPI_DSI_MODE_VIDEO_BURST | 248 MIPI_DSI_CLOCK_NON_CONTINUOUS | MIPI_DSI_MODE_LPM; 249 250 drm_panel_init(&ctx->panel, dev, &nt37700f_tianma_panel_funcs, 251 DRM_MODE_CONNECTOR_DSI); 252 ctx->panel.prepare_prev_first = true; 253 254 ctx->panel.backlight = nt37700f_tianma_create_backlight(dsi); 255 if (IS_ERR(ctx->panel.backlight)) 256 return dev_err_probe(dev, PTR_ERR(ctx->panel.backlight), 257 "Failed to create backlight\n"); 258 259 drm_panel_add(&ctx->panel); 260 261 ret = mipi_dsi_attach(dsi); 262 if (ret < 0) { 263 drm_panel_remove(&ctx->panel); 264 return dev_err_probe(dev, ret, "Failed to attach to DSI host\n"); 265 } 266 267 return 0; 268 } 269 270 static void nt37700f_tianma_remove(struct mipi_dsi_device *dsi) 271 { 272 struct nt37700f_tianma *ctx = mipi_dsi_get_drvdata(dsi); 273 int ret; 274 275 ret = mipi_dsi_detach(dsi); 276 if (ret < 0) 277 dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret); 278 279 drm_panel_remove(&ctx->panel); 280 } 281 282 static const struct of_device_id nt37700f_tianma_of_match[] = { 283 { .compatible = "novatek,nt37700f" }, 284 { /* sentinel */ } 285 }; 286 MODULE_DEVICE_TABLE(of, nt37700f_tianma_of_match); 287 288 static struct mipi_dsi_driver nt37700f_tianma_driver = { 289 .probe = nt37700f_tianma_probe, 290 .remove = nt37700f_tianma_remove, 291 .driver = { 292 .name = "panel-novatek-nt37700f", 293 .of_match_table = nt37700f_tianma_of_match, 294 }, 295 }; 296 module_mipi_dsi_driver(nt37700f_tianma_driver); 297 298 MODULE_DESCRIPTION("DRM driver for nt37700f cmd mode dsi tianma panel"); 299 MODULE_LICENSE("GPL"); 300