1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright (c) 2019 Radxa Limited 4 * Copyright (c) 2022 Edgeble AI Technologies Pvt. Ltd. 5 * 6 * Author: 7 * - Jagan Teki <jagan@amarulasolutions.com> 8 * - Stephen Chen <stephen@radxa.com> 9 */ 10 11 #include <drm/drm_mipi_dsi.h> 12 #include <drm/drm_modes.h> 13 #include <drm/drm_panel.h> 14 #include <drm/drm_print.h> 15 16 #include <linux/gpio/consumer.h> 17 #include <linux/delay.h> 18 #include <linux/module.h> 19 #include <linux/of_device.h> 20 #include <linux/regulator/consumer.h> 21 22 #define JD9365DA_INIT_CMD_LEN 2 23 24 struct jadard_init_cmd { 25 u8 data[JD9365DA_INIT_CMD_LEN]; 26 }; 27 28 struct jadard_panel_desc { 29 const struct drm_display_mode mode; 30 unsigned int lanes; 31 enum mipi_dsi_pixel_format format; 32 const struct jadard_init_cmd *init_cmds; 33 u32 num_init_cmds; 34 }; 35 36 struct jadard { 37 struct drm_panel panel; 38 struct mipi_dsi_device *dsi; 39 const struct jadard_panel_desc *desc; 40 41 struct regulator *vdd; 42 struct regulator *vccio; 43 struct gpio_desc *reset; 44 }; 45 46 static inline struct jadard *panel_to_jadard(struct drm_panel *panel) 47 { 48 return container_of(panel, struct jadard, panel); 49 } 50 51 static int jadard_enable(struct drm_panel *panel) 52 { 53 struct device *dev = panel->dev; 54 struct jadard *jadard = panel_to_jadard(panel); 55 const struct jadard_panel_desc *desc = jadard->desc; 56 struct mipi_dsi_device *dsi = jadard->dsi; 57 unsigned int i; 58 int err; 59 60 msleep(10); 61 62 for (i = 0; i < desc->num_init_cmds; i++) { 63 const struct jadard_init_cmd *cmd = &desc->init_cmds[i]; 64 65 err = mipi_dsi_dcs_write_buffer(dsi, cmd->data, JD9365DA_INIT_CMD_LEN); 66 if (err < 0) 67 return err; 68 } 69 70 msleep(120); 71 72 err = mipi_dsi_dcs_exit_sleep_mode(dsi); 73 if (err < 0) 74 DRM_DEV_ERROR(dev, "failed to exit sleep mode ret = %d\n", err); 75 76 err = mipi_dsi_dcs_set_display_on(dsi); 77 if (err < 0) 78 DRM_DEV_ERROR(dev, "failed to set display on ret = %d\n", err); 79 80 return 0; 81 } 82 83 static int jadard_disable(struct drm_panel *panel) 84 { 85 struct device *dev = panel->dev; 86 struct jadard *jadard = panel_to_jadard(panel); 87 int ret; 88 89 ret = mipi_dsi_dcs_set_display_off(jadard->dsi); 90 if (ret < 0) 91 DRM_DEV_ERROR(dev, "failed to set display off: %d\n", ret); 92 93 ret = mipi_dsi_dcs_enter_sleep_mode(jadard->dsi); 94 if (ret < 0) 95 DRM_DEV_ERROR(dev, "failed to enter sleep mode: %d\n", ret); 96 97 return 0; 98 } 99 100 static int jadard_prepare(struct drm_panel *panel) 101 { 102 struct jadard *jadard = panel_to_jadard(panel); 103 int ret; 104 105 ret = regulator_enable(jadard->vccio); 106 if (ret) 107 return ret; 108 109 ret = regulator_enable(jadard->vdd); 110 if (ret) 111 return ret; 112 113 gpiod_set_value(jadard->reset, 1); 114 msleep(5); 115 116 gpiod_set_value(jadard->reset, 0); 117 msleep(10); 118 119 gpiod_set_value(jadard->reset, 1); 120 msleep(120); 121 122 return 0; 123 } 124 125 static int jadard_unprepare(struct drm_panel *panel) 126 { 127 struct jadard *jadard = panel_to_jadard(panel); 128 129 gpiod_set_value(jadard->reset, 1); 130 msleep(120); 131 132 regulator_disable(jadard->vdd); 133 regulator_disable(jadard->vccio); 134 135 return 0; 136 } 137 138 static int jadard_get_modes(struct drm_panel *panel, 139 struct drm_connector *connector) 140 { 141 struct jadard *jadard = panel_to_jadard(panel); 142 const struct drm_display_mode *desc_mode = &jadard->desc->mode; 143 struct drm_display_mode *mode; 144 145 mode = drm_mode_duplicate(connector->dev, desc_mode); 146 if (!mode) { 147 DRM_DEV_ERROR(&jadard->dsi->dev, "failed to add mode %ux%ux@%u\n", 148 desc_mode->hdisplay, desc_mode->vdisplay, 149 drm_mode_vrefresh(desc_mode)); 150 return -ENOMEM; 151 } 152 153 drm_mode_set_name(mode); 154 drm_mode_probed_add(connector, mode); 155 156 connector->display_info.width_mm = mode->width_mm; 157 connector->display_info.height_mm = mode->height_mm; 158 159 return 1; 160 } 161 162 static const struct drm_panel_funcs jadard_funcs = { 163 .disable = jadard_disable, 164 .unprepare = jadard_unprepare, 165 .prepare = jadard_prepare, 166 .enable = jadard_enable, 167 .get_modes = jadard_get_modes, 168 }; 169 170 static const struct jadard_init_cmd cz101b4001_init_cmds[] = { 171 { .data = { 0xE0, 0x00 } }, 172 { .data = { 0xE1, 0x93 } }, 173 { .data = { 0xE2, 0x65 } }, 174 { .data = { 0xE3, 0xF8 } }, 175 { .data = { 0x80, 0x03 } }, 176 { .data = { 0xE0, 0x01 } }, 177 { .data = { 0x00, 0x00 } }, 178 { .data = { 0x01, 0x3B } }, 179 { .data = { 0x0C, 0x74 } }, 180 { .data = { 0x17, 0x00 } }, 181 { .data = { 0x18, 0xAF } }, 182 { .data = { 0x19, 0x00 } }, 183 { .data = { 0x1A, 0x00 } }, 184 { .data = { 0x1B, 0xAF } }, 185 { .data = { 0x1C, 0x00 } }, 186 { .data = { 0x35, 0x26 } }, 187 { .data = { 0x37, 0x09 } }, 188 { .data = { 0x38, 0x04 } }, 189 { .data = { 0x39, 0x00 } }, 190 { .data = { 0x3A, 0x01 } }, 191 { .data = { 0x3C, 0x78 } }, 192 { .data = { 0x3D, 0xFF } }, 193 { .data = { 0x3E, 0xFF } }, 194 { .data = { 0x3F, 0x7F } }, 195 { .data = { 0x40, 0x06 } }, 196 { .data = { 0x41, 0xA0 } }, 197 { .data = { 0x42, 0x81 } }, 198 { .data = { 0x43, 0x14 } }, 199 { .data = { 0x44, 0x23 } }, 200 { .data = { 0x45, 0x28 } }, 201 { .data = { 0x55, 0x02 } }, 202 { .data = { 0x57, 0x69 } }, 203 { .data = { 0x59, 0x0A } }, 204 { .data = { 0x5A, 0x2A } }, 205 { .data = { 0x5B, 0x17 } }, 206 { .data = { 0x5D, 0x7F } }, 207 { .data = { 0x5E, 0x6B } }, 208 { .data = { 0x5F, 0x5C } }, 209 { .data = { 0x60, 0x4F } }, 210 { .data = { 0x61, 0x4D } }, 211 { .data = { 0x62, 0x3F } }, 212 { .data = { 0x63, 0x42 } }, 213 { .data = { 0x64, 0x2B } }, 214 { .data = { 0x65, 0x44 } }, 215 { .data = { 0x66, 0x43 } }, 216 { .data = { 0x67, 0x43 } }, 217 { .data = { 0x68, 0x63 } }, 218 { .data = { 0x69, 0x52 } }, 219 { .data = { 0x6A, 0x5A } }, 220 { .data = { 0x6B, 0x4F } }, 221 { .data = { 0x6C, 0x4E } }, 222 { .data = { 0x6D, 0x20 } }, 223 { .data = { 0x6E, 0x0F } }, 224 { .data = { 0x6F, 0x00 } }, 225 { .data = { 0x70, 0x7F } }, 226 { .data = { 0x71, 0x6B } }, 227 { .data = { 0x72, 0x5C } }, 228 { .data = { 0x73, 0x4F } }, 229 { .data = { 0x74, 0x4D } }, 230 { .data = { 0x75, 0x3F } }, 231 { .data = { 0x76, 0x42 } }, 232 { .data = { 0x77, 0x2B } }, 233 { .data = { 0x78, 0x44 } }, 234 { .data = { 0x79, 0x43 } }, 235 { .data = { 0x7A, 0x43 } }, 236 { .data = { 0x7B, 0x63 } }, 237 { .data = { 0x7C, 0x52 } }, 238 { .data = { 0x7D, 0x5A } }, 239 { .data = { 0x7E, 0x4F } }, 240 { .data = { 0x7F, 0x4E } }, 241 { .data = { 0x80, 0x20 } }, 242 { .data = { 0x81, 0x0F } }, 243 { .data = { 0x82, 0x00 } }, 244 { .data = { 0xE0, 0x02 } }, 245 { .data = { 0x00, 0x02 } }, 246 { .data = { 0x01, 0x02 } }, 247 { .data = { 0x02, 0x00 } }, 248 { .data = { 0x03, 0x00 } }, 249 { .data = { 0x04, 0x1E } }, 250 { .data = { 0x05, 0x1E } }, 251 { .data = { 0x06, 0x1F } }, 252 { .data = { 0x07, 0x1F } }, 253 { .data = { 0x08, 0x1F } }, 254 { .data = { 0x09, 0x17 } }, 255 { .data = { 0x0A, 0x17 } }, 256 { .data = { 0x0B, 0x37 } }, 257 { .data = { 0x0C, 0x37 } }, 258 { .data = { 0x0D, 0x47 } }, 259 { .data = { 0x0E, 0x47 } }, 260 { .data = { 0x0F, 0x45 } }, 261 { .data = { 0x10, 0x45 } }, 262 { .data = { 0x11, 0x4B } }, 263 { .data = { 0x12, 0x4B } }, 264 { .data = { 0x13, 0x49 } }, 265 { .data = { 0x14, 0x49 } }, 266 { .data = { 0x15, 0x1F } }, 267 { .data = { 0x16, 0x01 } }, 268 { .data = { 0x17, 0x01 } }, 269 { .data = { 0x18, 0x00 } }, 270 { .data = { 0x19, 0x00 } }, 271 { .data = { 0x1A, 0x1E } }, 272 { .data = { 0x1B, 0x1E } }, 273 { .data = { 0x1C, 0x1F } }, 274 { .data = { 0x1D, 0x1F } }, 275 { .data = { 0x1E, 0x1F } }, 276 { .data = { 0x1F, 0x17 } }, 277 { .data = { 0x20, 0x17 } }, 278 { .data = { 0x21, 0x37 } }, 279 { .data = { 0x22, 0x37 } }, 280 { .data = { 0x23, 0x46 } }, 281 { .data = { 0x24, 0x46 } }, 282 { .data = { 0x25, 0x44 } }, 283 { .data = { 0x26, 0x44 } }, 284 { .data = { 0x27, 0x4A } }, 285 { .data = { 0x28, 0x4A } }, 286 { .data = { 0x29, 0x48 } }, 287 { .data = { 0x2A, 0x48 } }, 288 { .data = { 0x2B, 0x1F } }, 289 { .data = { 0x2C, 0x01 } }, 290 { .data = { 0x2D, 0x01 } }, 291 { .data = { 0x2E, 0x00 } }, 292 { .data = { 0x2F, 0x00 } }, 293 { .data = { 0x30, 0x1F } }, 294 { .data = { 0x31, 0x1F } }, 295 { .data = { 0x32, 0x1E } }, 296 { .data = { 0x33, 0x1E } }, 297 { .data = { 0x34, 0x1F } }, 298 { .data = { 0x35, 0x17 } }, 299 { .data = { 0x36, 0x17 } }, 300 { .data = { 0x37, 0x37 } }, 301 { .data = { 0x38, 0x37 } }, 302 { .data = { 0x39, 0x08 } }, 303 { .data = { 0x3A, 0x08 } }, 304 { .data = { 0x3B, 0x0A } }, 305 { .data = { 0x3C, 0x0A } }, 306 { .data = { 0x3D, 0x04 } }, 307 { .data = { 0x3E, 0x04 } }, 308 { .data = { 0x3F, 0x06 } }, 309 { .data = { 0x40, 0x06 } }, 310 { .data = { 0x41, 0x1F } }, 311 { .data = { 0x42, 0x02 } }, 312 { .data = { 0x43, 0x02 } }, 313 { .data = { 0x44, 0x00 } }, 314 { .data = { 0x45, 0x00 } }, 315 { .data = { 0x46, 0x1F } }, 316 { .data = { 0x47, 0x1F } }, 317 { .data = { 0x48, 0x1E } }, 318 { .data = { 0x49, 0x1E } }, 319 { .data = { 0x4A, 0x1F } }, 320 { .data = { 0x4B, 0x17 } }, 321 { .data = { 0x4C, 0x17 } }, 322 { .data = { 0x4D, 0x37 } }, 323 { .data = { 0x4E, 0x37 } }, 324 { .data = { 0x4F, 0x09 } }, 325 { .data = { 0x50, 0x09 } }, 326 { .data = { 0x51, 0x0B } }, 327 { .data = { 0x52, 0x0B } }, 328 { .data = { 0x53, 0x05 } }, 329 { .data = { 0x54, 0x05 } }, 330 { .data = { 0x55, 0x07 } }, 331 { .data = { 0x56, 0x07 } }, 332 { .data = { 0x57, 0x1F } }, 333 { .data = { 0x58, 0x40 } }, 334 { .data = { 0x5B, 0x30 } }, 335 { .data = { 0x5C, 0x16 } }, 336 { .data = { 0x5D, 0x34 } }, 337 { .data = { 0x5E, 0x05 } }, 338 { .data = { 0x5F, 0x02 } }, 339 { .data = { 0x63, 0x00 } }, 340 { .data = { 0x64, 0x6A } }, 341 { .data = { 0x67, 0x73 } }, 342 { .data = { 0x68, 0x1D } }, 343 { .data = { 0x69, 0x08 } }, 344 { .data = { 0x6A, 0x6A } }, 345 { .data = { 0x6B, 0x08 } }, 346 { .data = { 0x6C, 0x00 } }, 347 { .data = { 0x6D, 0x00 } }, 348 { .data = { 0x6E, 0x00 } }, 349 { .data = { 0x6F, 0x88 } }, 350 { .data = { 0x75, 0xFF } }, 351 { .data = { 0x77, 0xDD } }, 352 { .data = { 0x78, 0x3F } }, 353 { .data = { 0x79, 0x15 } }, 354 { .data = { 0x7A, 0x17 } }, 355 { .data = { 0x7D, 0x14 } }, 356 { .data = { 0x7E, 0x82 } }, 357 { .data = { 0xE0, 0x04 } }, 358 { .data = { 0x00, 0x0E } }, 359 { .data = { 0x02, 0xB3 } }, 360 { .data = { 0x09, 0x61 } }, 361 { .data = { 0x0E, 0x48 } }, 362 { .data = { 0xE0, 0x00 } }, 363 { .data = { 0xE6, 0x02 } }, 364 { .data = { 0xE7, 0x0C } }, 365 }; 366 367 static const struct jadard_panel_desc cz101b4001_desc = { 368 .mode = { 369 .clock = 70000, 370 371 .hdisplay = 800, 372 .hsync_start = 800 + 40, 373 .hsync_end = 800 + 40 + 18, 374 .htotal = 800 + 40 + 18 + 20, 375 376 .vdisplay = 1280, 377 .vsync_start = 1280 + 20, 378 .vsync_end = 1280 + 20 + 4, 379 .vtotal = 1280 + 20 + 4 + 20, 380 381 .width_mm = 62, 382 .height_mm = 110, 383 .type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED, 384 }, 385 .lanes = 4, 386 .format = MIPI_DSI_FMT_RGB888, 387 .init_cmds = cz101b4001_init_cmds, 388 .num_init_cmds = ARRAY_SIZE(cz101b4001_init_cmds), 389 }; 390 391 static int jadard_dsi_probe(struct mipi_dsi_device *dsi) 392 { 393 struct device *dev = &dsi->dev; 394 const struct jadard_panel_desc *desc; 395 struct jadard *jadard; 396 int ret; 397 398 jadard = devm_kzalloc(&dsi->dev, sizeof(*jadard), GFP_KERNEL); 399 if (!jadard) 400 return -ENOMEM; 401 402 desc = of_device_get_match_data(dev); 403 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST | 404 MIPI_DSI_MODE_NO_EOT_PACKET; 405 dsi->format = desc->format; 406 dsi->lanes = desc->lanes; 407 408 jadard->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW); 409 if (IS_ERR(jadard->reset)) { 410 DRM_DEV_ERROR(&dsi->dev, "failed to get our reset GPIO\n"); 411 return PTR_ERR(jadard->reset); 412 } 413 414 jadard->vdd = devm_regulator_get(dev, "vdd"); 415 if (IS_ERR(jadard->vdd)) { 416 DRM_DEV_ERROR(&dsi->dev, "failed to get vdd regulator\n"); 417 return PTR_ERR(jadard->vdd); 418 } 419 420 jadard->vccio = devm_regulator_get(dev, "vccio"); 421 if (IS_ERR(jadard->vccio)) { 422 DRM_DEV_ERROR(&dsi->dev, "failed to get vccio regulator\n"); 423 return PTR_ERR(jadard->vccio); 424 } 425 426 drm_panel_init(&jadard->panel, dev, &jadard_funcs, 427 DRM_MODE_CONNECTOR_DSI); 428 429 ret = drm_panel_of_backlight(&jadard->panel); 430 if (ret) 431 return ret; 432 433 drm_panel_add(&jadard->panel); 434 435 mipi_dsi_set_drvdata(dsi, jadard); 436 jadard->dsi = dsi; 437 jadard->desc = desc; 438 439 ret = mipi_dsi_attach(dsi); 440 if (ret < 0) 441 drm_panel_remove(&jadard->panel); 442 443 return ret; 444 } 445 446 static void jadard_dsi_remove(struct mipi_dsi_device *dsi) 447 { 448 struct jadard *jadard = mipi_dsi_get_drvdata(dsi); 449 450 mipi_dsi_detach(dsi); 451 drm_panel_remove(&jadard->panel); 452 } 453 454 static const struct of_device_id jadard_of_match[] = { 455 { .compatible = "chongzhou,cz101b4001", .data = &cz101b4001_desc }, 456 { /* sentinel */ } 457 }; 458 MODULE_DEVICE_TABLE(of, jadard_of_match); 459 460 static struct mipi_dsi_driver jadard_driver = { 461 .probe = jadard_dsi_probe, 462 .remove = jadard_dsi_remove, 463 .driver = { 464 .name = "jadard-jd9365da", 465 .of_match_table = jadard_of_match, 466 }, 467 }; 468 module_mipi_dsi_driver(jadard_driver); 469 470 MODULE_AUTHOR("Jagan Teki <jagan@edgeble.ai>"); 471 MODULE_AUTHOR("Stephen Chen <stephen@radxa.com>"); 472 MODULE_DESCRIPTION("Jadard JD9365DA-H3 WXGA DSI panel"); 473 MODULE_LICENSE("GPL"); 474