1 /* 2 * Copyright (C) 2014 NVIDIA Corporation 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 2 as 6 * published by the Free Software Foundation. 7 */ 8 9 #include <linux/backlight.h> 10 #include <linux/gpio/consumer.h> 11 #include <linux/module.h> 12 #include <linux/of.h> 13 #include <linux/regulator/consumer.h> 14 15 #include <drm/drmP.h> 16 #include <drm/drm_crtc.h> 17 #include <drm/drm_mipi_dsi.h> 18 #include <drm/drm_panel.h> 19 20 #include <video/mipi_display.h> 21 22 struct sharp_panel { 23 struct drm_panel base; 24 /* the datasheet refers to them as DSI-LINK1 and DSI-LINK2 */ 25 struct mipi_dsi_device *link1; 26 struct mipi_dsi_device *link2; 27 28 struct backlight_device *backlight; 29 struct regulator *supply; 30 31 bool prepared; 32 bool enabled; 33 34 const struct drm_display_mode *mode; 35 }; 36 37 static inline struct sharp_panel *to_sharp_panel(struct drm_panel *panel) 38 { 39 return container_of(panel, struct sharp_panel, base); 40 } 41 42 static void sharp_wait_frames(struct sharp_panel *sharp, unsigned int frames) 43 { 44 unsigned int refresh = drm_mode_vrefresh(sharp->mode); 45 46 if (WARN_ON(frames > refresh)) 47 return; 48 49 msleep(1000 / (refresh / frames)); 50 } 51 52 static int sharp_panel_write(struct sharp_panel *sharp, u16 offset, u8 value) 53 { 54 u8 payload[3] = { offset >> 8, offset & 0xff, value }; 55 struct mipi_dsi_device *dsi = sharp->link1; 56 ssize_t err; 57 58 err = mipi_dsi_generic_write(dsi, payload, sizeof(payload)); 59 if (err < 0) { 60 dev_err(&dsi->dev, "failed to write %02x to %04x: %zd\n", 61 value, offset, err); 62 return err; 63 } 64 65 err = mipi_dsi_dcs_nop(dsi); 66 if (err < 0) { 67 dev_err(&dsi->dev, "failed to send DCS nop: %zd\n", err); 68 return err; 69 } 70 71 usleep_range(10, 20); 72 73 return 0; 74 } 75 76 static __maybe_unused int sharp_panel_read(struct sharp_panel *sharp, 77 u16 offset, u8 *value) 78 { 79 ssize_t err; 80 81 cpu_to_be16s(&offset); 82 83 err = mipi_dsi_generic_read(sharp->link1, &offset, sizeof(offset), 84 value, sizeof(*value)); 85 if (err < 0) 86 dev_err(&sharp->link1->dev, "failed to read from %04x: %zd\n", 87 offset, err); 88 89 return err; 90 } 91 92 static int sharp_panel_disable(struct drm_panel *panel) 93 { 94 struct sharp_panel *sharp = to_sharp_panel(panel); 95 96 if (!sharp->enabled) 97 return 0; 98 99 if (sharp->backlight) { 100 sharp->backlight->props.power = FB_BLANK_POWERDOWN; 101 backlight_update_status(sharp->backlight); 102 } 103 104 sharp->enabled = false; 105 106 return 0; 107 } 108 109 static int sharp_panel_unprepare(struct drm_panel *panel) 110 { 111 struct sharp_panel *sharp = to_sharp_panel(panel); 112 int err; 113 114 if (!sharp->prepared) 115 return 0; 116 117 sharp_wait_frames(sharp, 4); 118 119 err = mipi_dsi_dcs_set_display_off(sharp->link1); 120 if (err < 0) 121 dev_err(panel->dev, "failed to set display off: %d\n", err); 122 123 err = mipi_dsi_dcs_enter_sleep_mode(sharp->link1); 124 if (err < 0) 125 dev_err(panel->dev, "failed to enter sleep mode: %d\n", err); 126 127 msleep(120); 128 129 regulator_disable(sharp->supply); 130 131 sharp->prepared = false; 132 133 return 0; 134 } 135 136 static int sharp_setup_symmetrical_split(struct mipi_dsi_device *left, 137 struct mipi_dsi_device *right, 138 const struct drm_display_mode *mode) 139 { 140 int err; 141 142 err = mipi_dsi_dcs_set_column_address(left, 0, mode->hdisplay / 2 - 1); 143 if (err < 0) { 144 dev_err(&left->dev, "failed to set column address: %d\n", err); 145 return err; 146 } 147 148 err = mipi_dsi_dcs_set_page_address(left, 0, mode->vdisplay - 1); 149 if (err < 0) { 150 dev_err(&left->dev, "failed to set page address: %d\n", err); 151 return err; 152 } 153 154 err = mipi_dsi_dcs_set_column_address(right, mode->hdisplay / 2, 155 mode->hdisplay - 1); 156 if (err < 0) { 157 dev_err(&right->dev, "failed to set column address: %d\n", err); 158 return err; 159 } 160 161 err = mipi_dsi_dcs_set_page_address(right, 0, mode->vdisplay - 1); 162 if (err < 0) { 163 dev_err(&right->dev, "failed to set page address: %d\n", err); 164 return err; 165 } 166 167 return 0; 168 } 169 170 static int sharp_panel_prepare(struct drm_panel *panel) 171 { 172 struct sharp_panel *sharp = to_sharp_panel(panel); 173 u8 format = MIPI_DCS_PIXEL_FMT_24BIT; 174 int err; 175 176 if (sharp->prepared) 177 return 0; 178 179 err = regulator_enable(sharp->supply); 180 if (err < 0) 181 return err; 182 183 /* 184 * According to the datasheet, the panel needs around 10 ms to fully 185 * power up. At least another 120 ms is required before exiting sleep 186 * mode to make sure the panel is ready. Throw in another 20 ms for 187 * good measure. 188 */ 189 msleep(150); 190 191 err = mipi_dsi_dcs_exit_sleep_mode(sharp->link1); 192 if (err < 0) { 193 dev_err(panel->dev, "failed to exit sleep mode: %d\n", err); 194 goto poweroff; 195 } 196 197 /* 198 * The MIPI DCS specification mandates this delay only between the 199 * exit_sleep_mode and enter_sleep_mode commands, so it isn't strictly 200 * necessary here. 201 */ 202 /* 203 msleep(120); 204 */ 205 206 /* set left-right mode */ 207 err = sharp_panel_write(sharp, 0x1000, 0x2a); 208 if (err < 0) { 209 dev_err(panel->dev, "failed to set left-right mode: %d\n", err); 210 goto poweroff; 211 } 212 213 /* enable command mode */ 214 err = sharp_panel_write(sharp, 0x1001, 0x01); 215 if (err < 0) { 216 dev_err(panel->dev, "failed to enable command mode: %d\n", err); 217 goto poweroff; 218 } 219 220 err = mipi_dsi_dcs_set_pixel_format(sharp->link1, format); 221 if (err < 0) { 222 dev_err(panel->dev, "failed to set pixel format: %d\n", err); 223 goto poweroff; 224 } 225 226 /* 227 * TODO: The device supports both left-right and even-odd split 228 * configurations, but this driver currently supports only the left- 229 * right split. To support a different mode a mechanism needs to be 230 * put in place to communicate the configuration back to the DSI host 231 * controller. 232 */ 233 err = sharp_setup_symmetrical_split(sharp->link1, sharp->link2, 234 sharp->mode); 235 if (err < 0) { 236 dev_err(panel->dev, "failed to set up symmetrical split: %d\n", 237 err); 238 goto poweroff; 239 } 240 241 err = mipi_dsi_dcs_set_display_on(sharp->link1); 242 if (err < 0) { 243 dev_err(panel->dev, "failed to set display on: %d\n", err); 244 goto poweroff; 245 } 246 247 sharp->prepared = true; 248 249 /* wait for 6 frames before continuing */ 250 sharp_wait_frames(sharp, 6); 251 252 return 0; 253 254 poweroff: 255 regulator_disable(sharp->supply); 256 return err; 257 } 258 259 static int sharp_panel_enable(struct drm_panel *panel) 260 { 261 struct sharp_panel *sharp = to_sharp_panel(panel); 262 263 if (sharp->enabled) 264 return 0; 265 266 if (sharp->backlight) { 267 sharp->backlight->props.power = FB_BLANK_UNBLANK; 268 backlight_update_status(sharp->backlight); 269 } 270 271 sharp->enabled = true; 272 273 return 0; 274 } 275 276 static const struct drm_display_mode default_mode = { 277 .clock = 278000, 278 .hdisplay = 2560, 279 .hsync_start = 2560 + 128, 280 .hsync_end = 2560 + 128 + 64, 281 .htotal = 2560 + 128 + 64 + 64, 282 .vdisplay = 1600, 283 .vsync_start = 1600 + 4, 284 .vsync_end = 1600 + 4 + 8, 285 .vtotal = 1600 + 4 + 8 + 32, 286 .vrefresh = 60, 287 }; 288 289 static int sharp_panel_get_modes(struct drm_panel *panel) 290 { 291 struct drm_display_mode *mode; 292 293 mode = drm_mode_duplicate(panel->drm, &default_mode); 294 if (!mode) { 295 dev_err(panel->drm->dev, "failed to add mode %ux%ux@%u\n", 296 default_mode.hdisplay, default_mode.vdisplay, 297 default_mode.vrefresh); 298 return -ENOMEM; 299 } 300 301 drm_mode_set_name(mode); 302 303 drm_mode_probed_add(panel->connector, mode); 304 305 panel->connector->display_info.width_mm = 217; 306 panel->connector->display_info.height_mm = 136; 307 308 return 1; 309 } 310 311 static const struct drm_panel_funcs sharp_panel_funcs = { 312 .disable = sharp_panel_disable, 313 .unprepare = sharp_panel_unprepare, 314 .prepare = sharp_panel_prepare, 315 .enable = sharp_panel_enable, 316 .get_modes = sharp_panel_get_modes, 317 }; 318 319 static const struct of_device_id sharp_of_match[] = { 320 { .compatible = "sharp,lq101r1sx01", }, 321 { } 322 }; 323 MODULE_DEVICE_TABLE(of, sharp_of_match); 324 325 static int sharp_panel_add(struct sharp_panel *sharp) 326 { 327 struct device_node *np; 328 int err; 329 330 sharp->mode = &default_mode; 331 332 sharp->supply = devm_regulator_get(&sharp->link1->dev, "power"); 333 if (IS_ERR(sharp->supply)) 334 return PTR_ERR(sharp->supply); 335 336 np = of_parse_phandle(sharp->link1->dev.of_node, "backlight", 0); 337 if (np) { 338 sharp->backlight = of_find_backlight_by_node(np); 339 of_node_put(np); 340 341 if (!sharp->backlight) 342 return -EPROBE_DEFER; 343 } 344 345 drm_panel_init(&sharp->base); 346 sharp->base.funcs = &sharp_panel_funcs; 347 sharp->base.dev = &sharp->link1->dev; 348 349 err = drm_panel_add(&sharp->base); 350 if (err < 0) 351 goto put_backlight; 352 353 return 0; 354 355 put_backlight: 356 if (sharp->backlight) 357 put_device(&sharp->backlight->dev); 358 359 return err; 360 } 361 362 static void sharp_panel_del(struct sharp_panel *sharp) 363 { 364 if (sharp->base.dev) 365 drm_panel_remove(&sharp->base); 366 367 if (sharp->backlight) 368 put_device(&sharp->backlight->dev); 369 370 if (sharp->link2) 371 put_device(&sharp->link2->dev); 372 } 373 374 static int sharp_panel_probe(struct mipi_dsi_device *dsi) 375 { 376 struct mipi_dsi_device *secondary = NULL; 377 struct sharp_panel *sharp; 378 struct device_node *np; 379 int err; 380 381 dsi->lanes = 4; 382 dsi->format = MIPI_DSI_FMT_RGB888; 383 dsi->mode_flags = MIPI_DSI_MODE_LPM; 384 385 /* Find DSI-LINK1 */ 386 np = of_parse_phandle(dsi->dev.of_node, "link2", 0); 387 if (np) { 388 secondary = of_find_mipi_dsi_device_by_node(np); 389 of_node_put(np); 390 391 if (!secondary) 392 return -EPROBE_DEFER; 393 } 394 395 /* register a panel for only the DSI-LINK1 interface */ 396 if (secondary) { 397 sharp = devm_kzalloc(&dsi->dev, sizeof(*sharp), GFP_KERNEL); 398 if (!sharp) { 399 put_device(&secondary->dev); 400 return -ENOMEM; 401 } 402 403 mipi_dsi_set_drvdata(dsi, sharp); 404 405 sharp->link2 = secondary; 406 sharp->link1 = dsi; 407 408 err = sharp_panel_add(sharp); 409 if (err < 0) { 410 put_device(&secondary->dev); 411 return err; 412 } 413 } 414 415 err = mipi_dsi_attach(dsi); 416 if (err < 0) { 417 if (secondary) 418 sharp_panel_del(sharp); 419 420 return err; 421 } 422 423 return 0; 424 } 425 426 static int sharp_panel_remove(struct mipi_dsi_device *dsi) 427 { 428 struct sharp_panel *sharp = mipi_dsi_get_drvdata(dsi); 429 int err; 430 431 /* only detach from host for the DSI-LINK2 interface */ 432 if (!sharp) { 433 mipi_dsi_detach(dsi); 434 return 0; 435 } 436 437 err = sharp_panel_disable(&sharp->base); 438 if (err < 0) 439 dev_err(&dsi->dev, "failed to disable panel: %d\n", err); 440 441 err = mipi_dsi_detach(dsi); 442 if (err < 0) 443 dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", err); 444 445 drm_panel_detach(&sharp->base); 446 sharp_panel_del(sharp); 447 448 return 0; 449 } 450 451 static void sharp_panel_shutdown(struct mipi_dsi_device *dsi) 452 { 453 struct sharp_panel *sharp = mipi_dsi_get_drvdata(dsi); 454 455 /* nothing to do for DSI-LINK2 */ 456 if (!sharp) 457 return; 458 459 sharp_panel_disable(&sharp->base); 460 } 461 462 static struct mipi_dsi_driver sharp_panel_driver = { 463 .driver = { 464 .name = "panel-sharp-lq101r1sx01", 465 .of_match_table = sharp_of_match, 466 }, 467 .probe = sharp_panel_probe, 468 .remove = sharp_panel_remove, 469 .shutdown = sharp_panel_shutdown, 470 }; 471 module_mipi_dsi_driver(sharp_panel_driver); 472 473 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>"); 474 MODULE_DESCRIPTION("Sharp LQ101R1SX01 panel driver"); 475 MODULE_LICENSE("GPL v2"); 476