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