1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include <linux/backlight.h> 4 #include <linux/delay.h> 5 #include <linux/gpio/consumer.h> 6 #include <linux/module.h> 7 #include <linux/of.h> 8 #include <linux/regulator/consumer.h> 9 10 #include <drm/drm_mipi_dsi.h> 11 #include <drm/drm_modes.h> 12 #include <drm/drm_panel.h> 13 14 struct tm5p5_nt35596 { 15 struct drm_panel panel; 16 struct mipi_dsi_device *dsi; 17 struct regulator_bulk_data supplies[2]; 18 struct gpio_desc *reset_gpio; 19 bool prepared; 20 }; 21 22 static inline struct tm5p5_nt35596 *to_tm5p5_nt35596(struct drm_panel *panel) 23 { 24 return container_of(panel, struct tm5p5_nt35596, panel); 25 } 26 27 #define dsi_generic_write_seq(dsi, seq...) do { \ 28 static const u8 d[] = { seq }; \ 29 int ret; \ 30 ret = mipi_dsi_generic_write(dsi, d, ARRAY_SIZE(d)); \ 31 if (ret < 0) \ 32 return ret; \ 33 } while (0) 34 35 #define dsi_dcs_write_seq(dsi, seq...) do { \ 36 static const u8 d[] = { seq }; \ 37 int ret; \ 38 ret = mipi_dsi_dcs_write_buffer(dsi, d, ARRAY_SIZE(d)); \ 39 if (ret < 0) \ 40 return ret; \ 41 } while (0) 42 43 static void tm5p5_nt35596_reset(struct tm5p5_nt35596 *ctx) 44 { 45 gpiod_set_value_cansleep(ctx->reset_gpio, 1); 46 usleep_range(1000, 2000); 47 gpiod_set_value_cansleep(ctx->reset_gpio, 0); 48 usleep_range(1000, 2000); 49 gpiod_set_value_cansleep(ctx->reset_gpio, 1); 50 usleep_range(15000, 16000); 51 } 52 53 static int tm5p5_nt35596_on(struct tm5p5_nt35596 *ctx) 54 { 55 struct mipi_dsi_device *dsi = ctx->dsi; 56 57 dsi_generic_write_seq(dsi, 0xff, 0x05); 58 dsi_generic_write_seq(dsi, 0xfb, 0x01); 59 dsi_generic_write_seq(dsi, 0xc5, 0x31); 60 dsi_generic_write_seq(dsi, 0xff, 0x04); 61 dsi_generic_write_seq(dsi, 0x01, 0x84); 62 dsi_generic_write_seq(dsi, 0x05, 0x25); 63 dsi_generic_write_seq(dsi, 0x06, 0x01); 64 dsi_generic_write_seq(dsi, 0x07, 0x20); 65 dsi_generic_write_seq(dsi, 0x08, 0x06); 66 dsi_generic_write_seq(dsi, 0x09, 0x08); 67 dsi_generic_write_seq(dsi, 0x0a, 0x10); 68 dsi_generic_write_seq(dsi, 0x0b, 0x10); 69 dsi_generic_write_seq(dsi, 0x0c, 0x10); 70 dsi_generic_write_seq(dsi, 0x0d, 0x14); 71 dsi_generic_write_seq(dsi, 0x0e, 0x14); 72 dsi_generic_write_seq(dsi, 0x0f, 0x14); 73 dsi_generic_write_seq(dsi, 0x10, 0x14); 74 dsi_generic_write_seq(dsi, 0x11, 0x14); 75 dsi_generic_write_seq(dsi, 0x12, 0x14); 76 dsi_generic_write_seq(dsi, 0x17, 0xf3); 77 dsi_generic_write_seq(dsi, 0x18, 0xc0); 78 dsi_generic_write_seq(dsi, 0x19, 0xc0); 79 dsi_generic_write_seq(dsi, 0x1a, 0xc0); 80 dsi_generic_write_seq(dsi, 0x1b, 0xb3); 81 dsi_generic_write_seq(dsi, 0x1c, 0xb3); 82 dsi_generic_write_seq(dsi, 0x1d, 0xb3); 83 dsi_generic_write_seq(dsi, 0x1e, 0xb3); 84 dsi_generic_write_seq(dsi, 0x1f, 0xb3); 85 dsi_generic_write_seq(dsi, 0x20, 0xb3); 86 dsi_generic_write_seq(dsi, 0xfb, 0x01); 87 dsi_generic_write_seq(dsi, 0xff, 0x00); 88 dsi_generic_write_seq(dsi, 0xfb, 0x01); 89 dsi_generic_write_seq(dsi, 0x35, 0x01); 90 dsi_generic_write_seq(dsi, 0xd3, 0x06); 91 dsi_generic_write_seq(dsi, 0xd4, 0x04); 92 dsi_generic_write_seq(dsi, 0x5e, 0x0d); 93 dsi_generic_write_seq(dsi, 0x11, 0x00); 94 msleep(100); 95 dsi_generic_write_seq(dsi, 0x29, 0x00); 96 dsi_generic_write_seq(dsi, 0x53, 0x24); 97 98 return 0; 99 } 100 101 static int tm5p5_nt35596_off(struct tm5p5_nt35596 *ctx) 102 { 103 struct mipi_dsi_device *dsi = ctx->dsi; 104 struct device *dev = &dsi->dev; 105 int ret; 106 107 ret = mipi_dsi_dcs_set_display_off(dsi); 108 if (ret < 0) { 109 dev_err(dev, "Failed to set display off: %d\n", ret); 110 return ret; 111 } 112 msleep(60); 113 114 ret = mipi_dsi_dcs_enter_sleep_mode(dsi); 115 if (ret < 0) { 116 dev_err(dev, "Failed to enter sleep mode: %d\n", ret); 117 return ret; 118 } 119 120 dsi_dcs_write_seq(dsi, 0x4f, 0x01); 121 122 return 0; 123 } 124 125 static int tm5p5_nt35596_prepare(struct drm_panel *panel) 126 { 127 struct tm5p5_nt35596 *ctx = to_tm5p5_nt35596(panel); 128 struct device *dev = &ctx->dsi->dev; 129 int ret; 130 131 if (ctx->prepared) 132 return 0; 133 134 ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies); 135 if (ret < 0) { 136 dev_err(dev, "Failed to enable regulators: %d\n", ret); 137 return ret; 138 } 139 140 tm5p5_nt35596_reset(ctx); 141 142 ret = tm5p5_nt35596_on(ctx); 143 if (ret < 0) { 144 dev_err(dev, "Failed to initialize panel: %d\n", ret); 145 gpiod_set_value_cansleep(ctx->reset_gpio, 0); 146 regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), 147 ctx->supplies); 148 return ret; 149 } 150 151 ctx->prepared = true; 152 return 0; 153 } 154 155 static int tm5p5_nt35596_unprepare(struct drm_panel *panel) 156 { 157 struct tm5p5_nt35596 *ctx = to_tm5p5_nt35596(panel); 158 struct device *dev = &ctx->dsi->dev; 159 int ret; 160 161 if (!ctx->prepared) 162 return 0; 163 164 ret = tm5p5_nt35596_off(ctx); 165 if (ret < 0) 166 dev_err(dev, "Failed to un-initialize panel: %d\n", ret); 167 168 gpiod_set_value_cansleep(ctx->reset_gpio, 0); 169 regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), 170 ctx->supplies); 171 172 ctx->prepared = false; 173 return 0; 174 } 175 176 static const struct drm_display_mode tm5p5_nt35596_mode = { 177 .clock = (1080 + 100 + 8 + 16) * (1920 + 4 + 2 + 4) * 60 / 1000, 178 .hdisplay = 1080, 179 .hsync_start = 1080 + 100, 180 .hsync_end = 1080 + 100 + 8, 181 .htotal = 1080 + 100 + 8 + 16, 182 .vdisplay = 1920, 183 .vsync_start = 1920 + 4, 184 .vsync_end = 1920 + 4 + 2, 185 .vtotal = 1920 + 4 + 2 + 4, 186 .vrefresh = 60, 187 .width_mm = 68, 188 .height_mm = 121, 189 }; 190 191 static int tm5p5_nt35596_get_modes(struct drm_panel *panel, 192 struct drm_connector *connector) 193 { 194 struct drm_display_mode *mode; 195 196 mode = drm_mode_duplicate(connector->dev, &tm5p5_nt35596_mode); 197 if (!mode) 198 return -ENOMEM; 199 200 drm_mode_set_name(mode); 201 202 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED; 203 connector->display_info.width_mm = mode->width_mm; 204 connector->display_info.height_mm = mode->height_mm; 205 drm_mode_probed_add(connector, mode); 206 207 return 1; 208 } 209 210 static const struct drm_panel_funcs tm5p5_nt35596_panel_funcs = { 211 .prepare = tm5p5_nt35596_prepare, 212 .unprepare = tm5p5_nt35596_unprepare, 213 .get_modes = tm5p5_nt35596_get_modes, 214 }; 215 216 static int tm5p5_nt35596_bl_update_status(struct backlight_device *bl) 217 { 218 struct mipi_dsi_device *dsi = bl_get_data(bl); 219 u16 brightness = bl->props.brightness; 220 int ret; 221 222 if (bl->props.power != FB_BLANK_UNBLANK || 223 bl->props.fb_blank != FB_BLANK_UNBLANK || 224 bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK)) 225 brightness = 0; 226 227 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM; 228 229 ret = mipi_dsi_dcs_set_display_brightness(dsi, brightness); 230 if (ret < 0) 231 return ret; 232 233 dsi->mode_flags |= MIPI_DSI_MODE_LPM; 234 235 return 0; 236 } 237 238 static int tm5p5_nt35596_bl_get_brightness(struct backlight_device *bl) 239 { 240 struct mipi_dsi_device *dsi = bl_get_data(bl); 241 u16 brightness = bl->props.brightness; 242 int ret; 243 244 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM; 245 246 ret = mipi_dsi_dcs_get_display_brightness(dsi, &brightness); 247 if (ret < 0) 248 return ret; 249 250 dsi->mode_flags |= MIPI_DSI_MODE_LPM; 251 252 return brightness & 0xff; 253 } 254 255 static const struct backlight_ops tm5p5_nt35596_bl_ops = { 256 .update_status = tm5p5_nt35596_bl_update_status, 257 .get_brightness = tm5p5_nt35596_bl_get_brightness, 258 }; 259 260 static struct backlight_device * 261 tm5p5_nt35596_create_backlight(struct mipi_dsi_device *dsi) 262 { 263 struct device *dev = &dsi->dev; 264 const struct backlight_properties props = { 265 .type = BACKLIGHT_RAW, 266 .brightness = 255, 267 .max_brightness = 255, 268 }; 269 270 return devm_backlight_device_register(dev, dev_name(dev), dev, dsi, 271 &tm5p5_nt35596_bl_ops, &props); 272 } 273 274 static int tm5p5_nt35596_probe(struct mipi_dsi_device *dsi) 275 { 276 struct device *dev = &dsi->dev; 277 struct tm5p5_nt35596 *ctx; 278 int ret; 279 280 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); 281 if (!ctx) 282 return -ENOMEM; 283 284 ctx->supplies[0].supply = "vdd"; 285 ctx->supplies[1].supply = "vddio"; 286 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies), 287 ctx->supplies); 288 if (ret < 0) { 289 dev_err(dev, "Failed to get regulators: %d\n", ret); 290 return ret; 291 } 292 293 ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW); 294 if (IS_ERR(ctx->reset_gpio)) { 295 ret = PTR_ERR(ctx->reset_gpio); 296 dev_err(dev, "Failed to get reset-gpios: %d\n", ret); 297 return ret; 298 } 299 300 ctx->dsi = dsi; 301 mipi_dsi_set_drvdata(dsi, ctx); 302 303 dsi->lanes = 4; 304 dsi->format = MIPI_DSI_FMT_RGB888; 305 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST | 306 MIPI_DSI_MODE_VIDEO_HSE | MIPI_DSI_MODE_EOT_PACKET | 307 MIPI_DSI_CLOCK_NON_CONTINUOUS | MIPI_DSI_MODE_LPM; 308 309 drm_panel_init(&ctx->panel, dev, &tm5p5_nt35596_panel_funcs, 310 DRM_MODE_CONNECTOR_DSI); 311 312 ctx->panel.backlight = tm5p5_nt35596_create_backlight(dsi); 313 if (IS_ERR(ctx->panel.backlight)) { 314 ret = PTR_ERR(ctx->panel.backlight); 315 dev_err(dev, "Failed to create backlight: %d\n", ret); 316 return ret; 317 } 318 319 ret = drm_panel_add(&ctx->panel); 320 if (ret < 0) { 321 dev_err(dev, "Failed to add panel: %d\n", ret); 322 return ret; 323 } 324 325 ret = mipi_dsi_attach(dsi); 326 if (ret < 0) { 327 dev_err(dev, "Failed to attach to DSI host: %d\n", ret); 328 return ret; 329 } 330 331 return 0; 332 } 333 334 static int tm5p5_nt35596_remove(struct mipi_dsi_device *dsi) 335 { 336 struct tm5p5_nt35596 *ctx = mipi_dsi_get_drvdata(dsi); 337 int ret; 338 339 ret = mipi_dsi_detach(dsi); 340 if (ret < 0) 341 dev_err(&dsi->dev, 342 "Failed to detach from DSI host: %d\n", ret); 343 344 drm_panel_remove(&ctx->panel); 345 346 return 0; 347 } 348 349 static const struct of_device_id tm5p5_nt35596_of_match[] = { 350 { .compatible = "asus,z00t-tm5p5-n35596" }, 351 { /* sentinel */ } 352 }; 353 MODULE_DEVICE_TABLE(of, tm5p5_nt35596_of_match); 354 355 static struct mipi_dsi_driver tm5p5_nt35596_driver = { 356 .probe = tm5p5_nt35596_probe, 357 .remove = tm5p5_nt35596_remove, 358 .driver = { 359 .name = "panel-tm5p5-nt35596", 360 .of_match_table = tm5p5_nt35596_of_match, 361 }, 362 }; 363 module_mipi_dsi_driver(tm5p5_nt35596_driver); 364 365 MODULE_AUTHOR("Konrad Dybcio <konradybcio@gmail.com>"); 366 MODULE_DESCRIPTION("DRM driver for tm5p5 nt35596 1080p video mode dsi panel"); 367 MODULE_LICENSE("GPL v2"); 368