1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Elida kd35t133 5.5" MIPI-DSI panel driver 4 * Copyright (C) 2020 Theobroma Systems Design und Consulting GmbH 5 * 6 * based on 7 * 8 * Rockteck jh057n00900 5.5" MIPI-DSI panel driver 9 * Copyright (C) Purism SPC 2019 10 */ 11 12 #include <linux/delay.h> 13 #include <linux/gpio/consumer.h> 14 #include <linux/media-bus-format.h> 15 #include <linux/module.h> 16 #include <linux/of.h> 17 #include <linux/regulator/consumer.h> 18 19 #include <video/display_timing.h> 20 #include <video/mipi_display.h> 21 22 #include <drm/drm_mipi_dsi.h> 23 #include <drm/drm_modes.h> 24 #include <drm/drm_panel.h> 25 #include <drm/drm_print.h> 26 27 /* Manufacturer specific Commands send via DSI */ 28 #define KD35T133_CMD_INTERFACEMODECTRL 0xb0 29 #define KD35T133_CMD_FRAMERATECTRL 0xb1 30 #define KD35T133_CMD_DISPLAYINVERSIONCTRL 0xb4 31 #define KD35T133_CMD_DISPLAYFUNCTIONCTRL 0xb6 32 #define KD35T133_CMD_POWERCONTROL1 0xc0 33 #define KD35T133_CMD_POWERCONTROL2 0xc1 34 #define KD35T133_CMD_VCOMCONTROL 0xc5 35 #define KD35T133_CMD_POSITIVEGAMMA 0xe0 36 #define KD35T133_CMD_NEGATIVEGAMMA 0xe1 37 #define KD35T133_CMD_SETIMAGEFUNCTION 0xe9 38 #define KD35T133_CMD_ADJUSTCONTROL3 0xf7 39 40 struct kd35t133 { 41 struct device *dev; 42 struct drm_panel panel; 43 struct gpio_desc *reset_gpio; 44 struct regulator *vdd; 45 struct regulator *iovcc; 46 bool prepared; 47 }; 48 49 static inline struct kd35t133 *panel_to_kd35t133(struct drm_panel *panel) 50 { 51 return container_of(panel, struct kd35t133, panel); 52 } 53 54 #define dsi_dcs_write_seq(dsi, cmd, seq...) do { \ 55 static const u8 b[] = { cmd, seq }; \ 56 int ret; \ 57 ret = mipi_dsi_dcs_write_buffer(dsi, b, ARRAY_SIZE(b)); \ 58 if (ret < 0) \ 59 return ret; \ 60 } while (0) 61 62 static int kd35t133_init_sequence(struct kd35t133 *ctx) 63 { 64 struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev); 65 struct device *dev = ctx->dev; 66 67 /* 68 * Init sequence was supplied by the panel vendor with minimal 69 * documentation. 70 */ 71 dsi_dcs_write_seq(dsi, KD35T133_CMD_POSITIVEGAMMA, 72 0x00, 0x13, 0x18, 0x04, 0x0f, 0x06, 0x3a, 0x56, 73 0x4d, 0x03, 0x0a, 0x06, 0x30, 0x3e, 0x0f); 74 dsi_dcs_write_seq(dsi, KD35T133_CMD_NEGATIVEGAMMA, 75 0x00, 0x13, 0x18, 0x01, 0x11, 0x06, 0x38, 0x34, 76 0x4d, 0x06, 0x0d, 0x0b, 0x31, 0x37, 0x0f); 77 dsi_dcs_write_seq(dsi, KD35T133_CMD_POWERCONTROL1, 0x18, 0x17); 78 dsi_dcs_write_seq(dsi, KD35T133_CMD_POWERCONTROL2, 0x41); 79 dsi_dcs_write_seq(dsi, KD35T133_CMD_VCOMCONTROL, 0x00, 0x1a, 0x80); 80 dsi_dcs_write_seq(dsi, MIPI_DCS_SET_ADDRESS_MODE, 0x48); 81 dsi_dcs_write_seq(dsi, MIPI_DCS_SET_PIXEL_FORMAT, 0x55); 82 dsi_dcs_write_seq(dsi, KD35T133_CMD_INTERFACEMODECTRL, 0x00); 83 dsi_dcs_write_seq(dsi, KD35T133_CMD_FRAMERATECTRL, 0xa0); 84 dsi_dcs_write_seq(dsi, KD35T133_CMD_DISPLAYINVERSIONCTRL, 0x02); 85 dsi_dcs_write_seq(dsi, KD35T133_CMD_DISPLAYFUNCTIONCTRL, 86 0x20, 0x02); 87 dsi_dcs_write_seq(dsi, KD35T133_CMD_SETIMAGEFUNCTION, 0x00); 88 dsi_dcs_write_seq(dsi, KD35T133_CMD_ADJUSTCONTROL3, 89 0xa9, 0x51, 0x2c, 0x82); 90 mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_INVERT_MODE, NULL, 0); 91 92 DRM_DEV_DEBUG_DRIVER(dev, "Panel init sequence done\n"); 93 return 0; 94 } 95 96 static int kd35t133_unprepare(struct drm_panel *panel) 97 { 98 struct kd35t133 *ctx = panel_to_kd35t133(panel); 99 struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev); 100 int ret; 101 102 if (!ctx->prepared) 103 return 0; 104 105 ret = mipi_dsi_dcs_set_display_off(dsi); 106 if (ret < 0) 107 DRM_DEV_ERROR(ctx->dev, "failed to set display off: %d\n", 108 ret); 109 110 ret = mipi_dsi_dcs_enter_sleep_mode(dsi); 111 if (ret < 0) { 112 DRM_DEV_ERROR(ctx->dev, "failed to enter sleep mode: %d\n", 113 ret); 114 return ret; 115 } 116 117 regulator_disable(ctx->iovcc); 118 regulator_disable(ctx->vdd); 119 120 ctx->prepared = false; 121 122 return 0; 123 } 124 125 static int kd35t133_prepare(struct drm_panel *panel) 126 { 127 struct kd35t133 *ctx = panel_to_kd35t133(panel); 128 struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev); 129 int ret; 130 131 if (ctx->prepared) 132 return 0; 133 134 DRM_DEV_DEBUG_DRIVER(ctx->dev, "Resetting the panel\n"); 135 ret = regulator_enable(ctx->vdd); 136 if (ret < 0) { 137 DRM_DEV_ERROR(ctx->dev, 138 "Failed to enable vdd supply: %d\n", ret); 139 return ret; 140 } 141 142 ret = regulator_enable(ctx->iovcc); 143 if (ret < 0) { 144 DRM_DEV_ERROR(ctx->dev, 145 "Failed to enable iovcc supply: %d\n", ret); 146 goto disable_vdd; 147 } 148 149 msleep(20); 150 151 gpiod_set_value_cansleep(ctx->reset_gpio, 1); 152 usleep_range(10, 20); 153 gpiod_set_value_cansleep(ctx->reset_gpio, 0); 154 155 msleep(20); 156 157 ret = mipi_dsi_dcs_exit_sleep_mode(dsi); 158 if (ret < 0) { 159 DRM_DEV_ERROR(ctx->dev, "Failed to exit sleep mode: %d\n", ret); 160 goto disable_iovcc; 161 } 162 163 msleep(250); 164 165 ret = kd35t133_init_sequence(ctx); 166 if (ret < 0) { 167 DRM_DEV_ERROR(ctx->dev, "Panel init sequence failed: %d\n", 168 ret); 169 goto disable_iovcc; 170 } 171 172 ret = mipi_dsi_dcs_set_display_on(dsi); 173 if (ret < 0) { 174 DRM_DEV_ERROR(ctx->dev, "Failed to set display on: %d\n", ret); 175 goto disable_iovcc; 176 } 177 178 msleep(50); 179 180 ctx->prepared = true; 181 182 return 0; 183 184 disable_iovcc: 185 regulator_disable(ctx->iovcc); 186 disable_vdd: 187 regulator_disable(ctx->vdd); 188 return ret; 189 } 190 191 static const struct drm_display_mode default_mode = { 192 .hdisplay = 320, 193 .hsync_start = 320 + 130, 194 .hsync_end = 320 + 130 + 4, 195 .htotal = 320 + 130 + 4 + 130, 196 .vdisplay = 480, 197 .vsync_start = 480 + 2, 198 .vsync_end = 480 + 2 + 1, 199 .vtotal = 480 + 2 + 1 + 2, 200 .clock = 17000, 201 .width_mm = 42, 202 .height_mm = 82, 203 }; 204 205 static int kd35t133_get_modes(struct drm_panel *panel, 206 struct drm_connector *connector) 207 { 208 struct kd35t133 *ctx = panel_to_kd35t133(panel); 209 struct drm_display_mode *mode; 210 211 mode = drm_mode_duplicate(connector->dev, &default_mode); 212 if (!mode) { 213 DRM_DEV_ERROR(ctx->dev, "Failed to add mode %ux%u@%u\n", 214 default_mode.hdisplay, default_mode.vdisplay, 215 drm_mode_vrefresh(&default_mode)); 216 return -ENOMEM; 217 } 218 219 drm_mode_set_name(mode); 220 221 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED; 222 connector->display_info.width_mm = mode->width_mm; 223 connector->display_info.height_mm = mode->height_mm; 224 drm_mode_probed_add(connector, mode); 225 226 return 1; 227 } 228 229 static const struct drm_panel_funcs kd35t133_funcs = { 230 .unprepare = kd35t133_unprepare, 231 .prepare = kd35t133_prepare, 232 .get_modes = kd35t133_get_modes, 233 }; 234 235 static int kd35t133_probe(struct mipi_dsi_device *dsi) 236 { 237 struct device *dev = &dsi->dev; 238 struct kd35t133 *ctx; 239 int ret; 240 241 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); 242 if (!ctx) 243 return -ENOMEM; 244 245 ctx->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); 246 if (IS_ERR(ctx->reset_gpio)) { 247 DRM_DEV_ERROR(dev, "cannot get reset gpio\n"); 248 return PTR_ERR(ctx->reset_gpio); 249 } 250 251 ctx->vdd = devm_regulator_get(dev, "vdd"); 252 if (IS_ERR(ctx->vdd)) { 253 ret = PTR_ERR(ctx->vdd); 254 if (ret != -EPROBE_DEFER) 255 DRM_DEV_ERROR(dev, 256 "Failed to request vdd regulator: %d\n", 257 ret); 258 return ret; 259 } 260 261 ctx->iovcc = devm_regulator_get(dev, "iovcc"); 262 if (IS_ERR(ctx->iovcc)) { 263 ret = PTR_ERR(ctx->iovcc); 264 if (ret != -EPROBE_DEFER) 265 DRM_DEV_ERROR(dev, 266 "Failed to request iovcc regulator: %d\n", 267 ret); 268 return ret; 269 } 270 271 mipi_dsi_set_drvdata(dsi, ctx); 272 273 ctx->dev = dev; 274 275 dsi->lanes = 1; 276 dsi->format = MIPI_DSI_FMT_RGB888; 277 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST | 278 MIPI_DSI_MODE_LPM | MIPI_DSI_MODE_EOT_PACKET; 279 280 drm_panel_init(&ctx->panel, &dsi->dev, &kd35t133_funcs, 281 DRM_MODE_CONNECTOR_DSI); 282 283 ret = drm_panel_of_backlight(&ctx->panel); 284 if (ret) 285 return ret; 286 287 drm_panel_add(&ctx->panel); 288 289 ret = mipi_dsi_attach(dsi); 290 if (ret < 0) { 291 DRM_DEV_ERROR(dev, "mipi_dsi_attach failed: %d\n", ret); 292 drm_panel_remove(&ctx->panel); 293 return ret; 294 } 295 296 return 0; 297 } 298 299 static void kd35t133_shutdown(struct mipi_dsi_device *dsi) 300 { 301 struct kd35t133 *ctx = mipi_dsi_get_drvdata(dsi); 302 int ret; 303 304 ret = drm_panel_unprepare(&ctx->panel); 305 if (ret < 0) 306 DRM_DEV_ERROR(&dsi->dev, "Failed to unprepare panel: %d\n", 307 ret); 308 309 ret = drm_panel_disable(&ctx->panel); 310 if (ret < 0) 311 DRM_DEV_ERROR(&dsi->dev, "Failed to disable panel: %d\n", 312 ret); 313 } 314 315 static int kd35t133_remove(struct mipi_dsi_device *dsi) 316 { 317 struct kd35t133 *ctx = mipi_dsi_get_drvdata(dsi); 318 int ret; 319 320 kd35t133_shutdown(dsi); 321 322 ret = mipi_dsi_detach(dsi); 323 if (ret < 0) 324 DRM_DEV_ERROR(&dsi->dev, "Failed to detach from DSI host: %d\n", 325 ret); 326 327 drm_panel_remove(&ctx->panel); 328 329 return 0; 330 } 331 332 static const struct of_device_id kd35t133_of_match[] = { 333 { .compatible = "elida,kd35t133" }, 334 { /* sentinel */ } 335 }; 336 MODULE_DEVICE_TABLE(of, kd35t133_of_match); 337 338 static struct mipi_dsi_driver kd35t133_driver = { 339 .driver = { 340 .name = "panel-elida-kd35t133", 341 .of_match_table = kd35t133_of_match, 342 }, 343 .probe = kd35t133_probe, 344 .remove = kd35t133_remove, 345 .shutdown = kd35t133_shutdown, 346 }; 347 module_mipi_dsi_driver(kd35t133_driver); 348 349 MODULE_AUTHOR("Heiko Stuebner <heiko.stuebner@theobroma-systems.com>"); 350 MODULE_DESCRIPTION("DRM driver for Elida kd35t133 MIPI DSI panel"); 351 MODULE_LICENSE("GPL v2"); 352