1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Generic DSI Command Mode panel driver 4 * 5 * Copyright (C) 2013 Texas Instruments 6 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com> 7 */ 8 9 /* #define DEBUG */ 10 11 #include <linux/backlight.h> 12 #include <linux/delay.h> 13 #include <linux/err.h> 14 #include <linux/fb.h> 15 #include <linux/gpio/consumer.h> 16 #include <linux/interrupt.h> 17 #include <linux/jiffies.h> 18 #include <linux/module.h> 19 #include <linux/platform_device.h> 20 #include <linux/sched/signal.h> 21 #include <linux/slab.h> 22 #include <linux/workqueue.h> 23 24 #include <video/omapfb_dss.h> 25 #include <video/mipi_display.h> 26 27 /* DSI Virtual channel. Hardcoded for now. */ 28 #define TCH 0 29 30 #define DCS_READ_NUM_ERRORS 0x05 31 #define DCS_BRIGHTNESS 0x51 32 #define DCS_CTRL_DISPLAY 0x53 33 #define DCS_GET_ID1 0xda 34 #define DCS_GET_ID2 0xdb 35 #define DCS_GET_ID3 0xdc 36 37 struct panel_drv_data { 38 struct omap_dss_device dssdev; 39 struct omap_dss_device *in; 40 41 struct omap_video_timings timings; 42 43 struct platform_device *pdev; 44 45 struct mutex lock; 46 47 struct backlight_device *bldev; 48 49 unsigned long hw_guard_end; /* next value of jiffies when we can 50 * issue the next sleep in/out command 51 */ 52 unsigned long hw_guard_wait; /* max guard time in jiffies */ 53 54 /* panel HW configuration from DT or platform data */ 55 struct gpio_desc *reset_gpio; 56 struct gpio_desc *ext_te_gpio; 57 58 bool use_dsi_backlight; 59 60 struct omap_dsi_pin_config pin_config; 61 62 /* runtime variables */ 63 bool enabled; 64 65 bool te_enabled; 66 67 atomic_t do_update; 68 int channel; 69 70 struct delayed_work te_timeout_work; 71 72 bool intro_printed; 73 74 bool ulps_enabled; 75 unsigned ulps_timeout; 76 struct delayed_work ulps_work; 77 }; 78 79 #define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev) 80 81 static irqreturn_t dsicm_te_isr(int irq, void *data); 82 static void dsicm_te_timeout_work_callback(struct work_struct *work); 83 static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable); 84 85 static int dsicm_panel_reset(struct panel_drv_data *ddata); 86 87 static void dsicm_ulps_work(struct work_struct *work); 88 89 static void hw_guard_start(struct panel_drv_data *ddata, int guard_msec) 90 { 91 ddata->hw_guard_wait = msecs_to_jiffies(guard_msec); 92 ddata->hw_guard_end = jiffies + ddata->hw_guard_wait; 93 } 94 95 static void hw_guard_wait(struct panel_drv_data *ddata) 96 { 97 unsigned long wait = ddata->hw_guard_end - jiffies; 98 99 if ((long)wait > 0 && time_before_eq(wait, ddata->hw_guard_wait)) { 100 set_current_state(TASK_UNINTERRUPTIBLE); 101 schedule_timeout(wait); 102 } 103 } 104 105 static int dsicm_dcs_read_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 *data) 106 { 107 struct omap_dss_device *in = ddata->in; 108 int r; 109 u8 buf[1]; 110 111 r = in->ops.dsi->dcs_read(in, ddata->channel, dcs_cmd, buf, 1); 112 113 if (r < 0) 114 return r; 115 116 *data = buf[0]; 117 118 return 0; 119 } 120 121 static int dsicm_dcs_write_0(struct panel_drv_data *ddata, u8 dcs_cmd) 122 { 123 struct omap_dss_device *in = ddata->in; 124 return in->ops.dsi->dcs_write(in, ddata->channel, &dcs_cmd, 1); 125 } 126 127 static int dsicm_dcs_write_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 param) 128 { 129 struct omap_dss_device *in = ddata->in; 130 u8 buf[2] = { dcs_cmd, param }; 131 132 return in->ops.dsi->dcs_write(in, ddata->channel, buf, 2); 133 } 134 135 static int dsicm_sleep_in(struct panel_drv_data *ddata) 136 137 { 138 struct omap_dss_device *in = ddata->in; 139 u8 cmd; 140 int r; 141 142 hw_guard_wait(ddata); 143 144 cmd = MIPI_DCS_ENTER_SLEEP_MODE; 145 r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, &cmd, 1); 146 if (r) 147 return r; 148 149 hw_guard_start(ddata, 120); 150 151 usleep_range(5000, 10000); 152 153 return 0; 154 } 155 156 static int dsicm_sleep_out(struct panel_drv_data *ddata) 157 { 158 int r; 159 160 hw_guard_wait(ddata); 161 162 r = dsicm_dcs_write_0(ddata, MIPI_DCS_EXIT_SLEEP_MODE); 163 if (r) 164 return r; 165 166 hw_guard_start(ddata, 120); 167 168 usleep_range(5000, 10000); 169 170 return 0; 171 } 172 173 static int dsicm_get_id(struct panel_drv_data *ddata, u8 *id1, u8 *id2, u8 *id3) 174 { 175 int r; 176 177 r = dsicm_dcs_read_1(ddata, DCS_GET_ID1, id1); 178 if (r) 179 return r; 180 r = dsicm_dcs_read_1(ddata, DCS_GET_ID2, id2); 181 if (r) 182 return r; 183 r = dsicm_dcs_read_1(ddata, DCS_GET_ID3, id3); 184 if (r) 185 return r; 186 187 return 0; 188 } 189 190 static int dsicm_set_update_window(struct panel_drv_data *ddata, 191 u16 x, u16 y, u16 w, u16 h) 192 { 193 struct omap_dss_device *in = ddata->in; 194 int r; 195 u16 x1 = x; 196 u16 x2 = x + w - 1; 197 u16 y1 = y; 198 u16 y2 = y + h - 1; 199 200 u8 buf[5]; 201 buf[0] = MIPI_DCS_SET_COLUMN_ADDRESS; 202 buf[1] = (x1 >> 8) & 0xff; 203 buf[2] = (x1 >> 0) & 0xff; 204 buf[3] = (x2 >> 8) & 0xff; 205 buf[4] = (x2 >> 0) & 0xff; 206 207 r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, buf, sizeof(buf)); 208 if (r) 209 return r; 210 211 buf[0] = MIPI_DCS_SET_PAGE_ADDRESS; 212 buf[1] = (y1 >> 8) & 0xff; 213 buf[2] = (y1 >> 0) & 0xff; 214 buf[3] = (y2 >> 8) & 0xff; 215 buf[4] = (y2 >> 0) & 0xff; 216 217 r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, buf, sizeof(buf)); 218 if (r) 219 return r; 220 221 in->ops.dsi->bta_sync(in, ddata->channel); 222 223 return r; 224 } 225 226 static void dsicm_queue_ulps_work(struct panel_drv_data *ddata) 227 { 228 if (ddata->ulps_timeout > 0) 229 schedule_delayed_work(&ddata->ulps_work, 230 msecs_to_jiffies(ddata->ulps_timeout)); 231 } 232 233 static void dsicm_cancel_ulps_work(struct panel_drv_data *ddata) 234 { 235 cancel_delayed_work(&ddata->ulps_work); 236 } 237 238 static int dsicm_enter_ulps(struct panel_drv_data *ddata) 239 { 240 struct omap_dss_device *in = ddata->in; 241 int r; 242 243 if (ddata->ulps_enabled) 244 return 0; 245 246 dsicm_cancel_ulps_work(ddata); 247 248 r = _dsicm_enable_te(ddata, false); 249 if (r) 250 goto err; 251 252 if (ddata->ext_te_gpio) 253 disable_irq(gpiod_to_irq(ddata->ext_te_gpio)); 254 255 in->ops.dsi->disable(in, false, true); 256 257 ddata->ulps_enabled = true; 258 259 return 0; 260 261 err: 262 dev_err(&ddata->pdev->dev, "enter ULPS failed"); 263 dsicm_panel_reset(ddata); 264 265 ddata->ulps_enabled = false; 266 267 dsicm_queue_ulps_work(ddata); 268 269 return r; 270 } 271 272 static int dsicm_exit_ulps(struct panel_drv_data *ddata) 273 { 274 struct omap_dss_device *in = ddata->in; 275 int r; 276 277 if (!ddata->ulps_enabled) 278 return 0; 279 280 r = in->ops.dsi->enable(in); 281 if (r) { 282 dev_err(&ddata->pdev->dev, "failed to enable DSI\n"); 283 goto err1; 284 } 285 286 in->ops.dsi->enable_hs(in, ddata->channel, true); 287 288 r = _dsicm_enable_te(ddata, true); 289 if (r) { 290 dev_err(&ddata->pdev->dev, "failed to re-enable TE"); 291 goto err2; 292 } 293 294 if (ddata->ext_te_gpio) 295 enable_irq(gpiod_to_irq(ddata->ext_te_gpio)); 296 297 dsicm_queue_ulps_work(ddata); 298 299 ddata->ulps_enabled = false; 300 301 return 0; 302 303 err2: 304 dev_err(&ddata->pdev->dev, "failed to exit ULPS"); 305 306 r = dsicm_panel_reset(ddata); 307 if (!r) { 308 if (ddata->ext_te_gpio) 309 enable_irq(gpiod_to_irq(ddata->ext_te_gpio)); 310 ddata->ulps_enabled = false; 311 } 312 err1: 313 dsicm_queue_ulps_work(ddata); 314 315 return r; 316 } 317 318 static int dsicm_wake_up(struct panel_drv_data *ddata) 319 { 320 if (ddata->ulps_enabled) 321 return dsicm_exit_ulps(ddata); 322 323 dsicm_cancel_ulps_work(ddata); 324 dsicm_queue_ulps_work(ddata); 325 return 0; 326 } 327 328 static int dsicm_bl_update_status(struct backlight_device *dev) 329 { 330 struct panel_drv_data *ddata = dev_get_drvdata(&dev->dev); 331 struct omap_dss_device *in = ddata->in; 332 int r; 333 int level = backlight_get_brightness(dev); 334 335 dev_dbg(&ddata->pdev->dev, "update brightness to %d\n", level); 336 337 mutex_lock(&ddata->lock); 338 339 if (ddata->enabled) { 340 in->ops.dsi->bus_lock(in); 341 342 r = dsicm_wake_up(ddata); 343 if (!r) 344 r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, level); 345 346 in->ops.dsi->bus_unlock(in); 347 } else { 348 r = 0; 349 } 350 351 mutex_unlock(&ddata->lock); 352 353 return r; 354 } 355 356 static int dsicm_bl_get_intensity(struct backlight_device *dev) 357 { 358 return backlight_get_brightness(dev); 359 } 360 361 static const struct backlight_ops dsicm_bl_ops = { 362 .get_brightness = dsicm_bl_get_intensity, 363 .update_status = dsicm_bl_update_status, 364 }; 365 366 static void dsicm_get_resolution(struct omap_dss_device *dssdev, 367 u16 *xres, u16 *yres) 368 { 369 *xres = dssdev->panel.timings.x_res; 370 *yres = dssdev->panel.timings.y_res; 371 } 372 373 static ssize_t dsicm_num_errors_show(struct device *dev, 374 struct device_attribute *attr, char *buf) 375 { 376 struct panel_drv_data *ddata = dev_get_drvdata(dev); 377 struct omap_dss_device *in = ddata->in; 378 u8 errors = 0; 379 int r; 380 381 mutex_lock(&ddata->lock); 382 383 if (ddata->enabled) { 384 in->ops.dsi->bus_lock(in); 385 386 r = dsicm_wake_up(ddata); 387 if (!r) 388 r = dsicm_dcs_read_1(ddata, DCS_READ_NUM_ERRORS, 389 &errors); 390 391 in->ops.dsi->bus_unlock(in); 392 } else { 393 r = -ENODEV; 394 } 395 396 mutex_unlock(&ddata->lock); 397 398 if (r) 399 return r; 400 401 return sysfs_emit(buf, "%d\n", errors); 402 } 403 404 static ssize_t dsicm_hw_revision_show(struct device *dev, 405 struct device_attribute *attr, char *buf) 406 { 407 struct panel_drv_data *ddata = dev_get_drvdata(dev); 408 struct omap_dss_device *in = ddata->in; 409 u8 id1, id2, id3; 410 int r; 411 412 mutex_lock(&ddata->lock); 413 414 if (ddata->enabled) { 415 in->ops.dsi->bus_lock(in); 416 417 r = dsicm_wake_up(ddata); 418 if (!r) 419 r = dsicm_get_id(ddata, &id1, &id2, &id3); 420 421 in->ops.dsi->bus_unlock(in); 422 } else { 423 r = -ENODEV; 424 } 425 426 mutex_unlock(&ddata->lock); 427 428 if (r) 429 return r; 430 431 return sysfs_emit(buf, "%02x.%02x.%02x\n", id1, id2, id3); 432 } 433 434 static ssize_t dsicm_store_ulps(struct device *dev, 435 struct device_attribute *attr, 436 const char *buf, size_t count) 437 { 438 struct panel_drv_data *ddata = dev_get_drvdata(dev); 439 struct omap_dss_device *in = ddata->in; 440 unsigned long t; 441 int r; 442 443 r = kstrtoul(buf, 0, &t); 444 if (r) 445 return r; 446 447 mutex_lock(&ddata->lock); 448 449 if (ddata->enabled) { 450 in->ops.dsi->bus_lock(in); 451 452 if (t) 453 r = dsicm_enter_ulps(ddata); 454 else 455 r = dsicm_wake_up(ddata); 456 457 in->ops.dsi->bus_unlock(in); 458 } 459 460 mutex_unlock(&ddata->lock); 461 462 if (r) 463 return r; 464 465 return count; 466 } 467 468 static ssize_t dsicm_show_ulps(struct device *dev, 469 struct device_attribute *attr, 470 char *buf) 471 { 472 struct panel_drv_data *ddata = dev_get_drvdata(dev); 473 unsigned t; 474 475 mutex_lock(&ddata->lock); 476 t = ddata->ulps_enabled; 477 mutex_unlock(&ddata->lock); 478 479 return sysfs_emit(buf, "%u\n", t); 480 } 481 482 static ssize_t dsicm_store_ulps_timeout(struct device *dev, 483 struct device_attribute *attr, 484 const char *buf, size_t count) 485 { 486 struct panel_drv_data *ddata = dev_get_drvdata(dev); 487 struct omap_dss_device *in = ddata->in; 488 unsigned long t; 489 int r; 490 491 r = kstrtoul(buf, 0, &t); 492 if (r) 493 return r; 494 495 mutex_lock(&ddata->lock); 496 ddata->ulps_timeout = t; 497 498 if (ddata->enabled) { 499 /* dsicm_wake_up will restart the timer */ 500 in->ops.dsi->bus_lock(in); 501 r = dsicm_wake_up(ddata); 502 in->ops.dsi->bus_unlock(in); 503 } 504 505 mutex_unlock(&ddata->lock); 506 507 if (r) 508 return r; 509 510 return count; 511 } 512 513 static ssize_t dsicm_show_ulps_timeout(struct device *dev, 514 struct device_attribute *attr, 515 char *buf) 516 { 517 struct panel_drv_data *ddata = dev_get_drvdata(dev); 518 unsigned t; 519 520 mutex_lock(&ddata->lock); 521 t = ddata->ulps_timeout; 522 mutex_unlock(&ddata->lock); 523 524 return sysfs_emit(buf, "%u\n", t); 525 } 526 527 static DEVICE_ATTR(num_dsi_errors, S_IRUGO, dsicm_num_errors_show, NULL); 528 static DEVICE_ATTR(hw_revision, S_IRUGO, dsicm_hw_revision_show, NULL); 529 static DEVICE_ATTR(ulps, S_IRUGO | S_IWUSR, 530 dsicm_show_ulps, dsicm_store_ulps); 531 static DEVICE_ATTR(ulps_timeout, S_IRUGO | S_IWUSR, 532 dsicm_show_ulps_timeout, dsicm_store_ulps_timeout); 533 534 static struct attribute *dsicm_attrs[] = { 535 &dev_attr_num_dsi_errors.attr, 536 &dev_attr_hw_revision.attr, 537 &dev_attr_ulps.attr, 538 &dev_attr_ulps_timeout.attr, 539 NULL, 540 }; 541 542 static const struct attribute_group dsicm_attr_group = { 543 .attrs = dsicm_attrs, 544 }; 545 546 static void dsicm_hw_reset(struct panel_drv_data *ddata) 547 { 548 /* 549 * Note that we appear to activate the reset line here. However 550 * existing DTSes specified incorrect polarity for it (active high), 551 * so in fact this deasserts the reset line. 552 */ 553 gpiod_set_value_cansleep(ddata->reset_gpio, 1); 554 udelay(10); 555 /* reset the panel */ 556 gpiod_set_value_cansleep(ddata->reset_gpio, 0); 557 /* keep reset asserted */ 558 udelay(10); 559 /* release reset line */ 560 gpiod_set_value_cansleep(ddata->reset_gpio, 1); 561 /* wait after releasing reset */ 562 usleep_range(5000, 10000); 563 } 564 565 static int dsicm_power_on(struct panel_drv_data *ddata) 566 { 567 struct omap_dss_device *in = ddata->in; 568 u8 id1, id2, id3; 569 int r; 570 struct omap_dss_dsi_config dsi_config = { 571 .mode = OMAP_DSS_DSI_CMD_MODE, 572 .pixel_format = OMAP_DSS_DSI_FMT_RGB888, 573 .timings = &ddata->timings, 574 .hs_clk_min = 150000000, 575 .hs_clk_max = 300000000, 576 .lp_clk_min = 7000000, 577 .lp_clk_max = 10000000, 578 }; 579 580 if (ddata->pin_config.num_pins > 0) { 581 r = in->ops.dsi->configure_pins(in, &ddata->pin_config); 582 if (r) { 583 dev_err(&ddata->pdev->dev, 584 "failed to configure DSI pins\n"); 585 goto err0; 586 } 587 } 588 589 r = in->ops.dsi->set_config(in, &dsi_config); 590 if (r) { 591 dev_err(&ddata->pdev->dev, "failed to configure DSI\n"); 592 goto err0; 593 } 594 595 r = in->ops.dsi->enable(in); 596 if (r) { 597 dev_err(&ddata->pdev->dev, "failed to enable DSI\n"); 598 goto err0; 599 } 600 601 dsicm_hw_reset(ddata); 602 603 in->ops.dsi->enable_hs(in, ddata->channel, false); 604 605 r = dsicm_sleep_out(ddata); 606 if (r) 607 goto err; 608 609 r = dsicm_get_id(ddata, &id1, &id2, &id3); 610 if (r) 611 goto err; 612 613 r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, 0xff); 614 if (r) 615 goto err; 616 617 r = dsicm_dcs_write_1(ddata, DCS_CTRL_DISPLAY, 618 (1<<2) | (1<<5)); /* BL | BCTRL */ 619 if (r) 620 goto err; 621 622 r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_PIXEL_FORMAT, 623 MIPI_DCS_PIXEL_FMT_24BIT); 624 if (r) 625 goto err; 626 627 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_ON); 628 if (r) 629 goto err; 630 631 r = _dsicm_enable_te(ddata, ddata->te_enabled); 632 if (r) 633 goto err; 634 635 r = in->ops.dsi->enable_video_output(in, ddata->channel); 636 if (r) 637 goto err; 638 639 ddata->enabled = 1; 640 641 if (!ddata->intro_printed) { 642 dev_info(&ddata->pdev->dev, "panel revision %02x.%02x.%02x\n", 643 id1, id2, id3); 644 ddata->intro_printed = true; 645 } 646 647 in->ops.dsi->enable_hs(in, ddata->channel, true); 648 649 return 0; 650 err: 651 dev_err(&ddata->pdev->dev, "error while enabling panel, issuing HW reset\n"); 652 653 dsicm_hw_reset(ddata); 654 655 in->ops.dsi->disable(in, true, false); 656 err0: 657 return r; 658 } 659 660 static void dsicm_power_off(struct panel_drv_data *ddata) 661 { 662 struct omap_dss_device *in = ddata->in; 663 int r; 664 665 in->ops.dsi->disable_video_output(in, ddata->channel); 666 667 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_OFF); 668 if (!r) 669 r = dsicm_sleep_in(ddata); 670 671 if (r) { 672 dev_err(&ddata->pdev->dev, 673 "error disabling panel, issuing HW reset\n"); 674 dsicm_hw_reset(ddata); 675 } 676 677 in->ops.dsi->disable(in, true, false); 678 679 ddata->enabled = 0; 680 } 681 682 static int dsicm_panel_reset(struct panel_drv_data *ddata) 683 { 684 dev_err(&ddata->pdev->dev, "performing LCD reset\n"); 685 686 dsicm_power_off(ddata); 687 dsicm_hw_reset(ddata); 688 return dsicm_power_on(ddata); 689 } 690 691 static int dsicm_connect(struct omap_dss_device *dssdev) 692 { 693 struct panel_drv_data *ddata = to_panel_data(dssdev); 694 struct omap_dss_device *in = ddata->in; 695 struct device *dev = &ddata->pdev->dev; 696 int r; 697 698 if (omapdss_device_is_connected(dssdev)) 699 return 0; 700 701 r = in->ops.dsi->connect(in, dssdev); 702 if (r) { 703 dev_err(dev, "Failed to connect to video source\n"); 704 return r; 705 } 706 707 r = in->ops.dsi->request_vc(ddata->in, &ddata->channel); 708 if (r) { 709 dev_err(dev, "failed to get virtual channel\n"); 710 goto err_req_vc; 711 } 712 713 r = in->ops.dsi->set_vc_id(ddata->in, ddata->channel, TCH); 714 if (r) { 715 dev_err(dev, "failed to set VC_ID\n"); 716 goto err_vc_id; 717 } 718 719 return 0; 720 721 err_vc_id: 722 in->ops.dsi->release_vc(ddata->in, ddata->channel); 723 err_req_vc: 724 in->ops.dsi->disconnect(in, dssdev); 725 return r; 726 } 727 728 static void dsicm_disconnect(struct omap_dss_device *dssdev) 729 { 730 struct panel_drv_data *ddata = to_panel_data(dssdev); 731 struct omap_dss_device *in = ddata->in; 732 733 if (!omapdss_device_is_connected(dssdev)) 734 return; 735 736 in->ops.dsi->release_vc(in, ddata->channel); 737 in->ops.dsi->disconnect(in, dssdev); 738 } 739 740 static int dsicm_enable(struct omap_dss_device *dssdev) 741 { 742 struct panel_drv_data *ddata = to_panel_data(dssdev); 743 struct omap_dss_device *in = ddata->in; 744 int r; 745 746 dev_dbg(&ddata->pdev->dev, "enable\n"); 747 748 mutex_lock(&ddata->lock); 749 750 if (!omapdss_device_is_connected(dssdev)) { 751 r = -ENODEV; 752 goto err; 753 } 754 755 if (omapdss_device_is_enabled(dssdev)) { 756 r = 0; 757 goto err; 758 } 759 760 in->ops.dsi->bus_lock(in); 761 762 r = dsicm_power_on(ddata); 763 764 in->ops.dsi->bus_unlock(in); 765 766 if (r) 767 goto err; 768 769 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE; 770 771 mutex_unlock(&ddata->lock); 772 773 return 0; 774 err: 775 dev_dbg(&ddata->pdev->dev, "enable failed\n"); 776 mutex_unlock(&ddata->lock); 777 return r; 778 } 779 780 static void dsicm_disable(struct omap_dss_device *dssdev) 781 { 782 struct panel_drv_data *ddata = to_panel_data(dssdev); 783 struct omap_dss_device *in = ddata->in; 784 int r; 785 786 dev_dbg(&ddata->pdev->dev, "disable\n"); 787 788 mutex_lock(&ddata->lock); 789 790 dsicm_cancel_ulps_work(ddata); 791 792 in->ops.dsi->bus_lock(in); 793 794 if (omapdss_device_is_enabled(dssdev)) { 795 r = dsicm_wake_up(ddata); 796 if (!r) 797 dsicm_power_off(ddata); 798 } 799 800 in->ops.dsi->bus_unlock(in); 801 802 dssdev->state = OMAP_DSS_DISPLAY_DISABLED; 803 804 mutex_unlock(&ddata->lock); 805 } 806 807 static void dsicm_framedone_cb(int err, void *data) 808 { 809 struct panel_drv_data *ddata = data; 810 struct omap_dss_device *in = ddata->in; 811 812 dev_dbg(&ddata->pdev->dev, "framedone, err %d\n", err); 813 in->ops.dsi->bus_unlock(ddata->in); 814 } 815 816 static irqreturn_t dsicm_te_isr(int irq, void *data) 817 { 818 struct panel_drv_data *ddata = data; 819 struct omap_dss_device *in = ddata->in; 820 int old; 821 int r; 822 823 old = atomic_cmpxchg(&ddata->do_update, 1, 0); 824 825 if (old) { 826 cancel_delayed_work(&ddata->te_timeout_work); 827 828 r = in->ops.dsi->update(in, ddata->channel, dsicm_framedone_cb, 829 ddata); 830 if (r) 831 goto err; 832 } 833 834 return IRQ_HANDLED; 835 err: 836 dev_err(&ddata->pdev->dev, "start update failed\n"); 837 in->ops.dsi->bus_unlock(in); 838 return IRQ_HANDLED; 839 } 840 841 static void dsicm_te_timeout_work_callback(struct work_struct *work) 842 { 843 struct panel_drv_data *ddata = container_of(work, struct panel_drv_data, 844 te_timeout_work.work); 845 struct omap_dss_device *in = ddata->in; 846 847 dev_err(&ddata->pdev->dev, "TE not received for 250ms!\n"); 848 849 atomic_set(&ddata->do_update, 0); 850 in->ops.dsi->bus_unlock(in); 851 } 852 853 static int dsicm_update(struct omap_dss_device *dssdev, 854 u16 x, u16 y, u16 w, u16 h) 855 { 856 struct panel_drv_data *ddata = to_panel_data(dssdev); 857 struct omap_dss_device *in = ddata->in; 858 int r; 859 860 dev_dbg(&ddata->pdev->dev, "update %d, %d, %d x %d\n", x, y, w, h); 861 862 mutex_lock(&ddata->lock); 863 in->ops.dsi->bus_lock(in); 864 865 r = dsicm_wake_up(ddata); 866 if (r) 867 goto err; 868 869 if (!ddata->enabled) { 870 r = 0; 871 goto err; 872 } 873 874 /* XXX no need to send this every frame, but dsi break if not done */ 875 r = dsicm_set_update_window(ddata, 0, 0, 876 dssdev->panel.timings.x_res, 877 dssdev->panel.timings.y_res); 878 if (r) 879 goto err; 880 881 if (ddata->te_enabled && ddata->ext_te_gpio) { 882 schedule_delayed_work(&ddata->te_timeout_work, 883 msecs_to_jiffies(250)); 884 atomic_set(&ddata->do_update, 1); 885 } else { 886 r = in->ops.dsi->update(in, ddata->channel, dsicm_framedone_cb, 887 ddata); 888 if (r) 889 goto err; 890 } 891 892 /* note: no bus_unlock here. unlock is in framedone_cb */ 893 mutex_unlock(&ddata->lock); 894 return 0; 895 err: 896 in->ops.dsi->bus_unlock(in); 897 mutex_unlock(&ddata->lock); 898 return r; 899 } 900 901 static int dsicm_sync(struct omap_dss_device *dssdev) 902 { 903 struct panel_drv_data *ddata = to_panel_data(dssdev); 904 struct omap_dss_device *in = ddata->in; 905 906 dev_dbg(&ddata->pdev->dev, "sync\n"); 907 908 mutex_lock(&ddata->lock); 909 in->ops.dsi->bus_lock(in); 910 in->ops.dsi->bus_unlock(in); 911 mutex_unlock(&ddata->lock); 912 913 dev_dbg(&ddata->pdev->dev, "sync done\n"); 914 915 return 0; 916 } 917 918 static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable) 919 { 920 struct omap_dss_device *in = ddata->in; 921 int r; 922 923 if (enable) 924 r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_TEAR_ON, 0); 925 else 926 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_TEAR_OFF); 927 928 if (!ddata->ext_te_gpio) 929 in->ops.dsi->enable_te(in, enable); 930 931 /* possible panel bug */ 932 msleep(100); 933 934 return r; 935 } 936 937 static int dsicm_enable_te(struct omap_dss_device *dssdev, bool enable) 938 { 939 struct panel_drv_data *ddata = to_panel_data(dssdev); 940 struct omap_dss_device *in = ddata->in; 941 int r; 942 943 mutex_lock(&ddata->lock); 944 945 if (ddata->te_enabled == enable) 946 goto end; 947 948 in->ops.dsi->bus_lock(in); 949 950 if (ddata->enabled) { 951 r = dsicm_wake_up(ddata); 952 if (r) 953 goto err; 954 955 r = _dsicm_enable_te(ddata, enable); 956 if (r) 957 goto err; 958 } 959 960 ddata->te_enabled = enable; 961 962 in->ops.dsi->bus_unlock(in); 963 end: 964 mutex_unlock(&ddata->lock); 965 966 return 0; 967 err: 968 in->ops.dsi->bus_unlock(in); 969 mutex_unlock(&ddata->lock); 970 971 return r; 972 } 973 974 static int dsicm_get_te(struct omap_dss_device *dssdev) 975 { 976 struct panel_drv_data *ddata = to_panel_data(dssdev); 977 int r; 978 979 mutex_lock(&ddata->lock); 980 r = ddata->te_enabled; 981 mutex_unlock(&ddata->lock); 982 983 return r; 984 } 985 986 static int dsicm_memory_read(struct omap_dss_device *dssdev, 987 void *buf, size_t size, 988 u16 x, u16 y, u16 w, u16 h) 989 { 990 struct panel_drv_data *ddata = to_panel_data(dssdev); 991 struct omap_dss_device *in = ddata->in; 992 int r; 993 int first = 1; 994 int plen; 995 unsigned buf_used = 0; 996 997 if (size < w * h * 3) 998 return -ENOMEM; 999 1000 mutex_lock(&ddata->lock); 1001 1002 if (!ddata->enabled) { 1003 r = -ENODEV; 1004 goto err1; 1005 } 1006 1007 size = min(w * h * 3, 1008 dssdev->panel.timings.x_res * 1009 dssdev->panel.timings.y_res * 3); 1010 1011 in->ops.dsi->bus_lock(in); 1012 1013 r = dsicm_wake_up(ddata); 1014 if (r) 1015 goto err2; 1016 1017 /* plen 1 or 2 goes into short packet. until checksum error is fixed, 1018 * use short packets. plen 32 works, but bigger packets seem to cause 1019 * an error. */ 1020 if (size % 2) 1021 plen = 1; 1022 else 1023 plen = 2; 1024 1025 dsicm_set_update_window(ddata, x, y, w, h); 1026 1027 r = in->ops.dsi->set_max_rx_packet_size(in, ddata->channel, plen); 1028 if (r) 1029 goto err2; 1030 1031 while (buf_used < size) { 1032 u8 dcs_cmd = first ? 0x2e : 0x3e; 1033 first = 0; 1034 1035 r = in->ops.dsi->dcs_read(in, ddata->channel, dcs_cmd, 1036 buf + buf_used, size - buf_used); 1037 1038 if (r < 0) { 1039 dev_err(dssdev->dev, "read error\n"); 1040 goto err3; 1041 } 1042 1043 buf_used += r; 1044 1045 if (r < plen) { 1046 dev_err(&ddata->pdev->dev, "short read\n"); 1047 break; 1048 } 1049 1050 if (signal_pending(current)) { 1051 dev_err(&ddata->pdev->dev, "signal pending, " 1052 "aborting memory read\n"); 1053 r = -ERESTARTSYS; 1054 goto err3; 1055 } 1056 } 1057 1058 r = buf_used; 1059 1060 err3: 1061 in->ops.dsi->set_max_rx_packet_size(in, ddata->channel, 1); 1062 err2: 1063 in->ops.dsi->bus_unlock(in); 1064 err1: 1065 mutex_unlock(&ddata->lock); 1066 return r; 1067 } 1068 1069 static void dsicm_ulps_work(struct work_struct *work) 1070 { 1071 struct panel_drv_data *ddata = container_of(work, struct panel_drv_data, 1072 ulps_work.work); 1073 struct omap_dss_device *dssdev = &ddata->dssdev; 1074 struct omap_dss_device *in = ddata->in; 1075 1076 mutex_lock(&ddata->lock); 1077 1078 if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE || !ddata->enabled) { 1079 mutex_unlock(&ddata->lock); 1080 return; 1081 } 1082 1083 in->ops.dsi->bus_lock(in); 1084 1085 dsicm_enter_ulps(ddata); 1086 1087 in->ops.dsi->bus_unlock(in); 1088 mutex_unlock(&ddata->lock); 1089 } 1090 1091 static struct omap_dss_driver dsicm_ops = { 1092 .connect = dsicm_connect, 1093 .disconnect = dsicm_disconnect, 1094 1095 .enable = dsicm_enable, 1096 .disable = dsicm_disable, 1097 1098 .update = dsicm_update, 1099 .sync = dsicm_sync, 1100 1101 .get_resolution = dsicm_get_resolution, 1102 .get_recommended_bpp = omapdss_default_get_recommended_bpp, 1103 1104 .enable_te = dsicm_enable_te, 1105 .get_te = dsicm_get_te, 1106 1107 .memory_read = dsicm_memory_read, 1108 }; 1109 1110 static int dsicm_probe(struct platform_device *pdev) 1111 { 1112 struct backlight_properties props; 1113 struct panel_drv_data *ddata; 1114 struct backlight_device *bldev = NULL; 1115 struct device *dev = &pdev->dev; 1116 struct omap_dss_device *dssdev; 1117 int r; 1118 1119 dev_dbg(dev, "probe\n"); 1120 1121 if (!pdev->dev.of_node) 1122 return -ENODEV; 1123 1124 ddata = devm_kzalloc_obj(dev, *ddata); 1125 if (!ddata) 1126 return -ENOMEM; 1127 1128 platform_set_drvdata(pdev, ddata); 1129 ddata->pdev = pdev; 1130 1131 ddata->in = omapdss_of_find_source_for_first_ep(pdev->dev.of_node); 1132 r = PTR_ERR_OR_ZERO(ddata->in); 1133 if (r) { 1134 dev_err(&pdev->dev, "failed to find video source: %d\n", r); 1135 return r; 1136 } 1137 1138 ddata->timings.x_res = 864; 1139 ddata->timings.y_res = 480; 1140 ddata->timings.pixelclock = 864 * 480 * 60; 1141 1142 dssdev = &ddata->dssdev; 1143 dssdev->dev = dev; 1144 dssdev->driver = &dsicm_ops; 1145 dssdev->panel.timings = ddata->timings; 1146 dssdev->type = OMAP_DISPLAY_TYPE_DSI; 1147 dssdev->owner = THIS_MODULE; 1148 1149 dssdev->panel.dsi_pix_fmt = OMAP_DSS_DSI_FMT_RGB888; 1150 dssdev->caps = OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE | 1151 OMAP_DSS_DISPLAY_CAP_TEAR_ELIM; 1152 1153 mutex_init(&ddata->lock); 1154 1155 r = omapdss_register_display(dssdev); 1156 if (r) { 1157 dev_err(dev, "Failed to register panel\n"); 1158 goto err_reg; 1159 } 1160 1161 atomic_set(&ddata->do_update, 0); 1162 1163 ddata->reset_gpio = devm_gpiod_get(&pdev->dev, "reset", GPIOD_OUT_LOW); 1164 r = PTR_ERR_OR_ZERO(ddata->reset_gpio); 1165 if (r) { 1166 dev_err(&pdev->dev, "Failed to request reset gpio: %d\n", r); 1167 return r; 1168 } 1169 1170 gpiod_set_consumer_name(ddata->reset_gpio, "taal rst"); 1171 1172 ddata->ext_te_gpio = devm_gpiod_get_optional(&pdev->dev, "te", 1173 GPIOD_IN); 1174 r = PTR_ERR_OR_ZERO(ddata->ext_te_gpio); 1175 if (r) { 1176 dev_err(&pdev->dev, "Failed to request TE gpio: %d\n", r); 1177 return r; 1178 } 1179 1180 if (ddata->ext_te_gpio) { 1181 gpiod_set_consumer_name(ddata->ext_te_gpio, "taal irq"); 1182 1183 r = devm_request_irq(dev, gpiod_to_irq(ddata->ext_te_gpio), 1184 dsicm_te_isr, 1185 IRQF_TRIGGER_RISING, 1186 "taal vsync", ddata); 1187 1188 if (r) 1189 return r; 1190 1191 INIT_DEFERRABLE_WORK(&ddata->te_timeout_work, 1192 dsicm_te_timeout_work_callback); 1193 1194 dev_dbg(dev, "Using GPIO TE\n"); 1195 } 1196 1197 INIT_DELAYED_WORK(&ddata->ulps_work, dsicm_ulps_work); 1198 1199 dsicm_hw_reset(ddata); 1200 1201 if (ddata->use_dsi_backlight) { 1202 memset(&props, 0, sizeof(struct backlight_properties)); 1203 props.max_brightness = 255; 1204 1205 props.type = BACKLIGHT_RAW; 1206 bldev = backlight_device_register(dev_name(dev), 1207 dev, ddata, &dsicm_bl_ops, &props); 1208 if (IS_ERR(bldev)) { 1209 r = PTR_ERR(bldev); 1210 goto err_reg; 1211 } 1212 1213 ddata->bldev = bldev; 1214 1215 bldev->props.power = BACKLIGHT_POWER_ON; 1216 bldev->props.brightness = 255; 1217 1218 dsicm_bl_update_status(bldev); 1219 } 1220 1221 r = sysfs_create_group(&dev->kobj, &dsicm_attr_group); 1222 if (r) { 1223 dev_err(dev, "failed to create sysfs files\n"); 1224 goto err_sysfs_create; 1225 } 1226 1227 return 0; 1228 1229 err_sysfs_create: 1230 if (bldev != NULL) 1231 backlight_device_unregister(bldev); 1232 err_reg: 1233 return r; 1234 } 1235 1236 static void dsicm_remove(struct platform_device *pdev) 1237 { 1238 struct panel_drv_data *ddata = platform_get_drvdata(pdev); 1239 struct omap_dss_device *dssdev = &ddata->dssdev; 1240 struct backlight_device *bldev; 1241 1242 dev_dbg(&pdev->dev, "remove\n"); 1243 1244 omapdss_unregister_display(dssdev); 1245 1246 dsicm_disable(dssdev); 1247 dsicm_disconnect(dssdev); 1248 1249 sysfs_remove_group(&pdev->dev.kobj, &dsicm_attr_group); 1250 1251 bldev = ddata->bldev; 1252 if (bldev != NULL) { 1253 bldev->props.power = BACKLIGHT_POWER_OFF; 1254 dsicm_bl_update_status(bldev); 1255 backlight_device_unregister(bldev); 1256 } 1257 1258 omap_dss_put_device(ddata->in); 1259 1260 dsicm_cancel_ulps_work(ddata); 1261 1262 /* reset, to be sure that the panel is in a valid state */ 1263 dsicm_hw_reset(ddata); 1264 } 1265 1266 static const struct of_device_id dsicm_of_match[] = { 1267 { .compatible = "omapdss,panel-dsi-cm", }, 1268 {}, 1269 }; 1270 1271 MODULE_DEVICE_TABLE(of, dsicm_of_match); 1272 1273 static struct platform_driver dsicm_driver = { 1274 .probe = dsicm_probe, 1275 .remove = dsicm_remove, 1276 .driver = { 1277 .name = "panel-dsi-cm", 1278 .of_match_table = dsicm_of_match, 1279 }, 1280 }; 1281 1282 module_platform_driver(dsicm_driver); 1283 1284 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>"); 1285 MODULE_DESCRIPTION("Generic DSI Command Mode Panel Driver"); 1286 MODULE_LICENSE("GPL"); 1287