1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Generated with linux-mdss-dsi-panel-driver-generator from vendor device tree. 4 * Copyright (c) 2023 Linaro Limited 5 */ 6 7 #include <linux/backlight.h> 8 #include <linux/delay.h> 9 #include <linux/gpio/consumer.h> 10 #include <linux/module.h> 11 #include <linux/of.h> 12 #include <linux/regulator/consumer.h> 13 14 #include <drm/display/drm_dsc.h> 15 #include <drm/display/drm_dsc_helper.h> 16 #include <drm/drm_mipi_dsi.h> 17 #include <drm/drm_modes.h> 18 #include <drm/drm_panel.h> 19 20 struct rm692e5_panel { 21 struct drm_panel panel; 22 struct mipi_dsi_device *dsi; 23 struct drm_dsc_config dsc; 24 struct regulator_bulk_data supplies[3]; 25 struct gpio_desc *reset_gpio; 26 }; 27 28 static inline struct rm692e5_panel *to_rm692e5_panel(struct drm_panel *panel) 29 { 30 return container_of(panel, struct rm692e5_panel, panel); 31 } 32 33 static void rm692e5_reset(struct rm692e5_panel *ctx) 34 { 35 gpiod_set_value_cansleep(ctx->reset_gpio, 0); 36 usleep_range(10000, 11000); 37 gpiod_set_value_cansleep(ctx->reset_gpio, 1); 38 usleep_range(5000, 6000); 39 gpiod_set_value_cansleep(ctx->reset_gpio, 0); 40 usleep_range(10000, 11000); 41 } 42 43 static int rm692e5_on(struct rm692e5_panel *ctx) 44 { 45 struct mipi_dsi_device *dsi = ctx->dsi; 46 struct device *dev = &dsi->dev; 47 int ret; 48 49 dsi->mode_flags |= MIPI_DSI_MODE_LPM; 50 51 mipi_dsi_generic_write_seq(dsi, 0xfe, 0x41); 52 mipi_dsi_generic_write_seq(dsi, 0xd6, 0x00); 53 mipi_dsi_generic_write_seq(dsi, 0xfe, 0x16); 54 mipi_dsi_generic_write_seq(dsi, 0x8a, 0x87); 55 mipi_dsi_generic_write_seq(dsi, 0xfe, 0x71); 56 mipi_dsi_generic_write_seq(dsi, 0x82, 0x01); 57 mipi_dsi_generic_write_seq(dsi, 0xc6, 0x00); 58 mipi_dsi_generic_write_seq(dsi, 0xc7, 0x2c); 59 mipi_dsi_generic_write_seq(dsi, 0xc8, 0x64); 60 mipi_dsi_generic_write_seq(dsi, 0xc9, 0x3c); 61 mipi_dsi_generic_write_seq(dsi, 0xca, 0x80); 62 mipi_dsi_generic_write_seq(dsi, 0xcb, 0x02); 63 mipi_dsi_generic_write_seq(dsi, 0xcc, 0x02); 64 mipi_dsi_generic_write_seq(dsi, 0xfe, 0x38); 65 mipi_dsi_generic_write_seq(dsi, 0x18, 0x13); 66 mipi_dsi_generic_write_seq(dsi, 0xfe, 0xf4); 67 mipi_dsi_generic_write_seq(dsi, 0x00, 0xff); 68 mipi_dsi_generic_write_seq(dsi, 0x01, 0xff); 69 mipi_dsi_generic_write_seq(dsi, 0x02, 0xcf); 70 mipi_dsi_generic_write_seq(dsi, 0x03, 0xbc); 71 mipi_dsi_generic_write_seq(dsi, 0x04, 0xb9); 72 mipi_dsi_generic_write_seq(dsi, 0x05, 0x99); 73 mipi_dsi_generic_write_seq(dsi, 0x06, 0x02); 74 mipi_dsi_generic_write_seq(dsi, 0x07, 0x0a); 75 mipi_dsi_generic_write_seq(dsi, 0x08, 0xe0); 76 mipi_dsi_generic_write_seq(dsi, 0x09, 0x4c); 77 mipi_dsi_generic_write_seq(dsi, 0x0a, 0xeb); 78 mipi_dsi_generic_write_seq(dsi, 0x0b, 0xe8); 79 mipi_dsi_generic_write_seq(dsi, 0x0c, 0x32); 80 mipi_dsi_generic_write_seq(dsi, 0x0d, 0x07); 81 mipi_dsi_generic_write_seq(dsi, 0xfe, 0xf4); 82 mipi_dsi_generic_write_seq(dsi, 0x0d, 0xc0); 83 mipi_dsi_generic_write_seq(dsi, 0x0e, 0xff); 84 mipi_dsi_generic_write_seq(dsi, 0x0f, 0xff); 85 mipi_dsi_generic_write_seq(dsi, 0x10, 0x33); 86 mipi_dsi_generic_write_seq(dsi, 0x11, 0x6f); 87 mipi_dsi_generic_write_seq(dsi, 0x12, 0x6e); 88 mipi_dsi_generic_write_seq(dsi, 0x13, 0xa6); 89 mipi_dsi_generic_write_seq(dsi, 0x14, 0x80); 90 mipi_dsi_generic_write_seq(dsi, 0x15, 0x02); 91 mipi_dsi_generic_write_seq(dsi, 0x16, 0x38); 92 mipi_dsi_generic_write_seq(dsi, 0x17, 0xd3); 93 mipi_dsi_generic_write_seq(dsi, 0x18, 0x3a); 94 mipi_dsi_generic_write_seq(dsi, 0x19, 0xba); 95 mipi_dsi_generic_write_seq(dsi, 0x1a, 0xcc); 96 mipi_dsi_generic_write_seq(dsi, 0x1b, 0x01); 97 98 ret = mipi_dsi_dcs_nop(dsi); 99 if (ret < 0) { 100 dev_err(dev, "Failed to nop: %d\n", ret); 101 return ret; 102 } 103 msleep(32); 104 105 mipi_dsi_generic_write_seq(dsi, 0xfe, 0x38); 106 mipi_dsi_generic_write_seq(dsi, 0x18, 0x13); 107 mipi_dsi_generic_write_seq(dsi, 0xfe, 0xd1); 108 mipi_dsi_generic_write_seq(dsi, 0xd3, 0x00); 109 mipi_dsi_generic_write_seq(dsi, 0xd0, 0x00); 110 mipi_dsi_generic_write_seq(dsi, 0xd2, 0x00); 111 mipi_dsi_generic_write_seq(dsi, 0xd4, 0x00); 112 mipi_dsi_generic_write_seq(dsi, 0xb4, 0x01); 113 mipi_dsi_generic_write_seq(dsi, 0xfe, 0xf9); 114 mipi_dsi_generic_write_seq(dsi, 0x00, 0xaf); 115 mipi_dsi_generic_write_seq(dsi, 0x1d, 0x37); 116 mipi_dsi_generic_write_seq(dsi, 0x44, 0x0a, 0x7b); 117 mipi_dsi_generic_write_seq(dsi, 0xfe, 0x00); 118 mipi_dsi_generic_write_seq(dsi, 0xfa, 0x01); 119 mipi_dsi_generic_write_seq(dsi, 0xc2, 0x08); 120 mipi_dsi_generic_write_seq(dsi, 0x35, 0x00); 121 mipi_dsi_generic_write_seq(dsi, 0x51, 0x05, 0x42); 122 123 ret = mipi_dsi_dcs_exit_sleep_mode(dsi); 124 if (ret < 0) { 125 dev_err(dev, "Failed to exit sleep mode: %d\n", ret); 126 return ret; 127 } 128 msleep(100); 129 130 ret = mipi_dsi_dcs_set_display_on(dsi); 131 if (ret < 0) { 132 dev_err(dev, "Failed to set display on: %d\n", ret); 133 return ret; 134 } 135 136 return 0; 137 } 138 139 static int rm692e5_disable(struct drm_panel *panel) 140 { 141 struct rm692e5_panel *ctx = to_rm692e5_panel(panel); 142 struct mipi_dsi_device *dsi = ctx->dsi; 143 struct device *dev = &dsi->dev; 144 int ret; 145 146 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM; 147 148 mipi_dsi_generic_write_seq(dsi, 0xfe, 0x00); 149 150 ret = mipi_dsi_dcs_set_display_off(dsi); 151 if (ret < 0) { 152 dev_err(dev, "Failed to set display off: %d\n", ret); 153 return ret; 154 } 155 156 ret = mipi_dsi_dcs_enter_sleep_mode(dsi); 157 if (ret < 0) { 158 dev_err(dev, "Failed to enter sleep mode: %d\n", ret); 159 return ret; 160 } 161 msleep(100); 162 163 return 0; 164 } 165 166 static int rm692e5_prepare(struct drm_panel *panel) 167 { 168 struct rm692e5_panel *ctx = to_rm692e5_panel(panel); 169 struct drm_dsc_picture_parameter_set pps; 170 struct device *dev = &ctx->dsi->dev; 171 int ret; 172 173 ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies); 174 if (ret < 0) { 175 dev_err(dev, "Failed to enable regulators: %d\n", ret); 176 return ret; 177 } 178 179 rm692e5_reset(ctx); 180 181 ret = rm692e5_on(ctx); 182 if (ret < 0) { 183 dev_err(dev, "Failed to initialize panel: %d\n", ret); 184 gpiod_set_value_cansleep(ctx->reset_gpio, 1); 185 regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies); 186 return ret; 187 } 188 189 drm_dsc_pps_payload_pack(&pps, &ctx->dsc); 190 191 ret = mipi_dsi_picture_parameter_set(ctx->dsi, &pps); 192 if (ret < 0) { 193 dev_err(panel->dev, "failed to transmit PPS: %d\n", ret); 194 return ret; 195 } 196 197 ret = mipi_dsi_compression_mode(ctx->dsi, true); 198 if (ret < 0) { 199 dev_err(dev, "failed to enable compression mode: %d\n", ret); 200 return ret; 201 } 202 203 msleep(28); 204 205 mipi_dsi_generic_write_seq(ctx->dsi, 0xfe, 0x40); 206 207 /* 0x05 -> 90Hz, 0x00 -> 60Hz */ 208 mipi_dsi_generic_write_seq(ctx->dsi, 0xbd, 0x05); 209 210 mipi_dsi_generic_write_seq(ctx->dsi, 0xfe, 0x00); 211 212 return 0; 213 } 214 215 static int rm692e5_unprepare(struct drm_panel *panel) 216 { 217 struct rm692e5_panel *ctx = to_rm692e5_panel(panel); 218 219 gpiod_set_value_cansleep(ctx->reset_gpio, 1); 220 regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies); 221 222 return 0; 223 } 224 225 static const struct drm_display_mode rm692e5_mode = { 226 .clock = (1224 + 32 + 8 + 8) * (2700 + 8 + 2 + 8) * 90 / 1000, 227 .hdisplay = 1224, 228 .hsync_start = 1224 + 32, 229 .hsync_end = 1224 + 32 + 8, 230 .htotal = 1224 + 32 + 8 + 8, 231 .vdisplay = 2700, 232 .vsync_start = 2700 + 8, 233 .vsync_end = 2700 + 8 + 2, 234 .vtotal = 2700 + 8 + 2 + 8, 235 .width_mm = 68, 236 .height_mm = 150, 237 }; 238 239 static int rm692e5_get_modes(struct drm_panel *panel, 240 struct drm_connector *connector) 241 { 242 struct drm_display_mode *mode; 243 244 mode = drm_mode_duplicate(connector->dev, &rm692e5_mode); 245 if (!mode) 246 return -ENOMEM; 247 248 drm_mode_set_name(mode); 249 250 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED; 251 connector->display_info.width_mm = mode->width_mm; 252 connector->display_info.height_mm = mode->height_mm; 253 drm_mode_probed_add(connector, mode); 254 255 return 1; 256 } 257 258 static const struct drm_panel_funcs rm692e5_panel_funcs = { 259 .prepare = rm692e5_prepare, 260 .unprepare = rm692e5_unprepare, 261 .disable = rm692e5_disable, 262 .get_modes = rm692e5_get_modes, 263 }; 264 265 static int rm692e5_bl_update_status(struct backlight_device *bl) 266 { 267 struct mipi_dsi_device *dsi = bl_get_data(bl); 268 u16 brightness = backlight_get_brightness(bl); 269 int ret; 270 271 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM; 272 273 ret = mipi_dsi_dcs_set_display_brightness_large(dsi, brightness); 274 if (ret < 0) 275 return ret; 276 277 dsi->mode_flags |= MIPI_DSI_MODE_LPM; 278 279 return 0; 280 } 281 282 static int rm692e5_bl_get_brightness(struct backlight_device *bl) 283 { 284 struct mipi_dsi_device *dsi = bl_get_data(bl); 285 u16 brightness; 286 int ret; 287 288 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM; 289 290 ret = mipi_dsi_dcs_get_display_brightness_large(dsi, &brightness); 291 if (ret < 0) 292 return ret; 293 294 dsi->mode_flags |= MIPI_DSI_MODE_LPM; 295 296 return brightness; 297 } 298 299 static const struct backlight_ops rm692e5_bl_ops = { 300 .update_status = rm692e5_bl_update_status, 301 .get_brightness = rm692e5_bl_get_brightness, 302 }; 303 304 static struct backlight_device * 305 rm692e5_create_backlight(struct mipi_dsi_device *dsi) 306 { 307 struct device *dev = &dsi->dev; 308 const struct backlight_properties props = { 309 .type = BACKLIGHT_RAW, 310 .brightness = 4095, 311 .max_brightness = 4095, 312 }; 313 314 return devm_backlight_device_register(dev, dev_name(dev), dev, dsi, 315 &rm692e5_bl_ops, &props); 316 } 317 318 static int rm692e5_probe(struct mipi_dsi_device *dsi) 319 { 320 struct device *dev = &dsi->dev; 321 struct rm692e5_panel *ctx; 322 int ret; 323 324 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); 325 if (!ctx) 326 return -ENOMEM; 327 328 ctx->supplies[0].supply = "vddio"; 329 ctx->supplies[1].supply = "dvdd"; 330 ctx->supplies[2].supply = "vci"; 331 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies), 332 ctx->supplies); 333 if (ret < 0) 334 return dev_err_probe(dev, ret, "Failed to get regulators\n"); 335 336 ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 337 if (IS_ERR(ctx->reset_gpio)) 338 return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio), 339 "Failed to get reset-gpios\n"); 340 341 ctx->dsi = dsi; 342 mipi_dsi_set_drvdata(dsi, ctx); 343 344 dsi->lanes = 4; 345 dsi->format = MIPI_DSI_FMT_RGB888; 346 dsi->mode_flags = MIPI_DSI_MODE_NO_EOT_PACKET | 347 MIPI_DSI_CLOCK_NON_CONTINUOUS; 348 349 drm_panel_init(&ctx->panel, dev, &rm692e5_panel_funcs, 350 DRM_MODE_CONNECTOR_DSI); 351 ctx->panel.prepare_prev_first = true; 352 353 ctx->panel.backlight = rm692e5_create_backlight(dsi); 354 if (IS_ERR(ctx->panel.backlight)) 355 return dev_err_probe(dev, PTR_ERR(ctx->panel.backlight), 356 "Failed to create backlight\n"); 357 358 drm_panel_add(&ctx->panel); 359 360 /* This panel only supports DSC; unconditionally enable it */ 361 dsi->dsc = &ctx->dsc; 362 363 /* TODO: Pass slice_per_pkt = 2 */ 364 ctx->dsc.dsc_version_major = 1; 365 ctx->dsc.dsc_version_minor = 1; 366 ctx->dsc.slice_height = 60; 367 ctx->dsc.slice_width = 1224; 368 369 ctx->dsc.slice_count = 1224 / ctx->dsc.slice_width; 370 ctx->dsc.bits_per_component = 8; 371 ctx->dsc.bits_per_pixel = 8 << 4; /* 4 fractional bits */ 372 ctx->dsc.block_pred_enable = true; 373 374 ret = mipi_dsi_attach(dsi); 375 if (ret < 0) { 376 dev_err(dev, "Failed to attach to DSI host: %d\n", ret); 377 drm_panel_remove(&ctx->panel); 378 return ret; 379 } 380 381 return 0; 382 } 383 384 static void rm692e5_remove(struct mipi_dsi_device *dsi) 385 { 386 struct rm692e5_panel *ctx = mipi_dsi_get_drvdata(dsi); 387 int ret; 388 389 ret = mipi_dsi_detach(dsi); 390 if (ret < 0) 391 dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret); 392 393 drm_panel_remove(&ctx->panel); 394 } 395 396 static const struct of_device_id rm692e5_of_match[] = { 397 { .compatible = "fairphone,fp5-rm692e5-boe" }, 398 { } 399 }; 400 MODULE_DEVICE_TABLE(of, rm692e5_of_match); 401 402 static struct mipi_dsi_driver rm692e5_driver = { 403 .probe = rm692e5_probe, 404 .remove = rm692e5_remove, 405 .driver = { 406 .name = "panel-rm692e5-boe-amoled", 407 .of_match_table = rm692e5_of_match, 408 }, 409 }; 410 module_mipi_dsi_driver(rm692e5_driver); 411 412 MODULE_DESCRIPTION("DRM driver for rm692e5-equipped DSI panels"); 413 MODULE_LICENSE("GPL"); 414