linux/drivers/video/fbdev/omap2/omapfb/displays/panel-tpo-td043mtea1.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * TPO TD043MTEA1 Panel driver
   4 *
   5 * Author: Gražvydas Ignotas <notasas@gmail.com>
   6 * Converted to new DSS device model: Tomi Valkeinen <tomi.valkeinen@ti.com>
   7 */
   8
   9#include <linux/module.h>
  10#include <linux/delay.h>
  11#include <linux/spi/spi.h>
  12#include <linux/regulator/consumer.h>
  13#include <linux/gpio.h>
  14#include <linux/err.h>
  15#include <linux/slab.h>
  16#include <linux/of_gpio.h>
  17
  18#include <video/omapfb_dss.h>
  19
  20#define TPO_R02_MODE(x)         ((x) & 7)
  21#define TPO_R02_MODE_800x480    7
  22#define TPO_R02_NCLK_RISING     BIT(3)
  23#define TPO_R02_HSYNC_HIGH      BIT(4)
  24#define TPO_R02_VSYNC_HIGH      BIT(5)
  25
  26#define TPO_R03_NSTANDBY        BIT(0)
  27#define TPO_R03_EN_CP_CLK       BIT(1)
  28#define TPO_R03_EN_VGL_PUMP     BIT(2)
  29#define TPO_R03_EN_PWM          BIT(3)
  30#define TPO_R03_DRIVING_CAP_100 BIT(4)
  31#define TPO_R03_EN_PRE_CHARGE   BIT(6)
  32#define TPO_R03_SOFTWARE_CTL    BIT(7)
  33
  34#define TPO_R04_NFLIP_H         BIT(0)
  35#define TPO_R04_NFLIP_V         BIT(1)
  36#define TPO_R04_CP_CLK_FREQ_1H  BIT(2)
  37#define TPO_R04_VGL_FREQ_1H     BIT(4)
  38
  39#define TPO_R03_VAL_NORMAL (TPO_R03_NSTANDBY | TPO_R03_EN_CP_CLK | \
  40                        TPO_R03_EN_VGL_PUMP |  TPO_R03_EN_PWM | \
  41                        TPO_R03_DRIVING_CAP_100 | TPO_R03_EN_PRE_CHARGE | \
  42                        TPO_R03_SOFTWARE_CTL)
  43
  44#define TPO_R03_VAL_STANDBY (TPO_R03_DRIVING_CAP_100 | \
  45                        TPO_R03_EN_PRE_CHARGE | TPO_R03_SOFTWARE_CTL)
  46
  47static const u16 tpo_td043_def_gamma[12] = {
  48        105, 315, 381, 431, 490, 537, 579, 686, 780, 837, 880, 1023
  49};
  50
  51struct panel_drv_data {
  52        struct omap_dss_device  dssdev;
  53        struct omap_dss_device *in;
  54
  55        struct omap_video_timings videomode;
  56
  57        int data_lines;
  58
  59        struct spi_device *spi;
  60        struct regulator *vcc_reg;
  61        int nreset_gpio;
  62        u16 gamma[12];
  63        u32 mode;
  64        u32 hmirror:1;
  65        u32 vmirror:1;
  66        u32 powered_on:1;
  67        u32 spi_suspended:1;
  68        u32 power_on_resume:1;
  69};
  70
  71static const struct omap_video_timings tpo_td043_timings = {
  72        .x_res          = 800,
  73        .y_res          = 480,
  74
  75        .pixelclock     = 36000000,
  76
  77        .hsw            = 1,
  78        .hfp            = 68,
  79        .hbp            = 214,
  80
  81        .vsw            = 1,
  82        .vfp            = 39,
  83        .vbp            = 34,
  84
  85        .vsync_level    = OMAPDSS_SIG_ACTIVE_LOW,
  86        .hsync_level    = OMAPDSS_SIG_ACTIVE_LOW,
  87        .data_pclk_edge = OMAPDSS_DRIVE_SIG_FALLING_EDGE,
  88        .de_level       = OMAPDSS_SIG_ACTIVE_HIGH,
  89        .sync_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE,
  90};
  91
  92#define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
  93
  94static int tpo_td043_write(struct spi_device *spi, u8 addr, u8 data)
  95{
  96        struct spi_message      m;
  97        struct spi_transfer     xfer;
  98        u16                     w;
  99        int                     r;
 100
 101        spi_message_init(&m);
 102
 103        memset(&xfer, 0, sizeof(xfer));
 104
 105        w = ((u16)addr << 10) | (1 << 8) | data;
 106        xfer.tx_buf = &w;
 107        xfer.bits_per_word = 16;
 108        xfer.len = 2;
 109        spi_message_add_tail(&xfer, &m);
 110
 111        r = spi_sync(spi, &m);
 112        if (r < 0)
 113                dev_warn(&spi->dev, "failed to write to LCD reg (%d)\n", r);
 114        return r;
 115}
 116
 117static void tpo_td043_write_gamma(struct spi_device *spi, u16 gamma[12])
 118{
 119        u8 i, val;
 120
 121        /* gamma bits [9:8] */
 122        for (val = i = 0; i < 4; i++)
 123                val |= (gamma[i] & 0x300) >> ((i + 1) * 2);
 124        tpo_td043_write(spi, 0x11, val);
 125
 126        for (val = i = 0; i < 4; i++)
 127                val |= (gamma[i+4] & 0x300) >> ((i + 1) * 2);
 128        tpo_td043_write(spi, 0x12, val);
 129
 130        for (val = i = 0; i < 4; i++)
 131                val |= (gamma[i+8] & 0x300) >> ((i + 1) * 2);
 132        tpo_td043_write(spi, 0x13, val);
 133
 134        /* gamma bits [7:0] */
 135        for (val = i = 0; i < 12; i++)
 136                tpo_td043_write(spi, 0x14 + i, gamma[i] & 0xff);
 137}
 138
 139static int tpo_td043_write_mirror(struct spi_device *spi, bool h, bool v)
 140{
 141        u8 reg4 = TPO_R04_NFLIP_H | TPO_R04_NFLIP_V |
 142                TPO_R04_CP_CLK_FREQ_1H | TPO_R04_VGL_FREQ_1H;
 143        if (h)
 144                reg4 &= ~TPO_R04_NFLIP_H;
 145        if (v)
 146                reg4 &= ~TPO_R04_NFLIP_V;
 147
 148        return tpo_td043_write(spi, 4, reg4);
 149}
 150
 151static int tpo_td043_set_hmirror(struct omap_dss_device *dssdev, bool enable)
 152{
 153        struct panel_drv_data *ddata = dev_get_drvdata(dssdev->dev);
 154
 155        ddata->hmirror = enable;
 156        return tpo_td043_write_mirror(ddata->spi, ddata->hmirror,
 157                        ddata->vmirror);
 158}
 159
 160static bool tpo_td043_get_hmirror(struct omap_dss_device *dssdev)
 161{
 162        struct panel_drv_data *ddata = dev_get_drvdata(dssdev->dev);
 163
 164        return ddata->hmirror;
 165}
 166
 167static ssize_t tpo_td043_vmirror_show(struct device *dev,
 168        struct device_attribute *attr, char *buf)
 169{
 170        struct panel_drv_data *ddata = dev_get_drvdata(dev);
 171
 172        return snprintf(buf, PAGE_SIZE, "%d\n", ddata->vmirror);
 173}
 174
 175static ssize_t tpo_td043_vmirror_store(struct device *dev,
 176        struct device_attribute *attr, const char *buf, size_t count)
 177{
 178        struct panel_drv_data *ddata = dev_get_drvdata(dev);
 179        int val;
 180        int ret;
 181
 182        ret = kstrtoint(buf, 0, &val);
 183        if (ret < 0)
 184                return ret;
 185
 186        val = !!val;
 187
 188        ret = tpo_td043_write_mirror(ddata->spi, ddata->hmirror, val);
 189        if (ret < 0)
 190                return ret;
 191
 192        ddata->vmirror = val;
 193
 194        return count;
 195}
 196
 197static ssize_t tpo_td043_mode_show(struct device *dev,
 198        struct device_attribute *attr, char *buf)
 199{
 200        struct panel_drv_data *ddata = dev_get_drvdata(dev);
 201
 202        return snprintf(buf, PAGE_SIZE, "%d\n", ddata->mode);
 203}
 204
 205static ssize_t tpo_td043_mode_store(struct device *dev,
 206        struct device_attribute *attr, const char *buf, size_t count)
 207{
 208        struct panel_drv_data *ddata = dev_get_drvdata(dev);
 209        long val;
 210        int ret;
 211
 212        ret = kstrtol(buf, 0, &val);
 213        if (ret != 0 || val & ~7)
 214                return -EINVAL;
 215
 216        ddata->mode = val;
 217
 218        val |= TPO_R02_NCLK_RISING;
 219        tpo_td043_write(ddata->spi, 2, val);
 220
 221        return count;
 222}
 223
 224static ssize_t tpo_td043_gamma_show(struct device *dev,
 225        struct device_attribute *attr, char *buf)
 226{
 227        struct panel_drv_data *ddata = dev_get_drvdata(dev);
 228        ssize_t len = 0;
 229        int ret;
 230        int i;
 231
 232        for (i = 0; i < ARRAY_SIZE(ddata->gamma); i++) {
 233                ret = snprintf(buf + len, PAGE_SIZE - len, "%u ",
 234                                ddata->gamma[i]);
 235                if (ret < 0)
 236                        return ret;
 237                len += ret;
 238        }
 239        buf[len - 1] = '\n';
 240
 241        return len;
 242}
 243
 244static ssize_t tpo_td043_gamma_store(struct device *dev,
 245        struct device_attribute *attr, const char *buf, size_t count)
 246{
 247        struct panel_drv_data *ddata = dev_get_drvdata(dev);
 248        unsigned int g[12];
 249        int ret;
 250        int i;
 251
 252        ret = sscanf(buf, "%u %u %u %u %u %u %u %u %u %u %u %u",
 253                        &g[0], &g[1], &g[2], &g[3], &g[4], &g[5],
 254                        &g[6], &g[7], &g[8], &g[9], &g[10], &g[11]);
 255
 256        if (ret != 12)
 257                return -EINVAL;
 258
 259        for (i = 0; i < 12; i++)
 260                ddata->gamma[i] = g[i];
 261
 262        tpo_td043_write_gamma(ddata->spi, ddata->gamma);
 263
 264        return count;
 265}
 266
 267static DEVICE_ATTR(vmirror, S_IRUGO | S_IWUSR,
 268                tpo_td043_vmirror_show, tpo_td043_vmirror_store);
 269static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
 270                tpo_td043_mode_show, tpo_td043_mode_store);
 271static DEVICE_ATTR(gamma, S_IRUGO | S_IWUSR,
 272                tpo_td043_gamma_show, tpo_td043_gamma_store);
 273
 274static struct attribute *tpo_td043_attrs[] = {
 275        &dev_attr_vmirror.attr,
 276        &dev_attr_mode.attr,
 277        &dev_attr_gamma.attr,
 278        NULL,
 279};
 280
 281static const struct attribute_group tpo_td043_attr_group = {
 282        .attrs = tpo_td043_attrs,
 283};
 284
 285static int tpo_td043_power_on(struct panel_drv_data *ddata)
 286{
 287        int r;
 288
 289        if (ddata->powered_on)
 290                return 0;
 291
 292        r = regulator_enable(ddata->vcc_reg);
 293        if (r != 0)
 294                return r;
 295
 296        /* wait for panel to stabilize */
 297        msleep(160);
 298
 299        if (gpio_is_valid(ddata->nreset_gpio))
 300                gpio_set_value(ddata->nreset_gpio, 1);
 301
 302        tpo_td043_write(ddata->spi, 2,
 303                        TPO_R02_MODE(ddata->mode) | TPO_R02_NCLK_RISING);
 304        tpo_td043_write(ddata->spi, 3, TPO_R03_VAL_NORMAL);
 305        tpo_td043_write(ddata->spi, 0x20, 0xf0);
 306        tpo_td043_write(ddata->spi, 0x21, 0xf0);
 307        tpo_td043_write_mirror(ddata->spi, ddata->hmirror,
 308                        ddata->vmirror);
 309        tpo_td043_write_gamma(ddata->spi, ddata->gamma);
 310
 311        ddata->powered_on = 1;
 312        return 0;
 313}
 314
 315static void tpo_td043_power_off(struct panel_drv_data *ddata)
 316{
 317        if (!ddata->powered_on)
 318                return;
 319
 320        tpo_td043_write(ddata->spi, 3,
 321                        TPO_R03_VAL_STANDBY | TPO_R03_EN_PWM);
 322
 323        if (gpio_is_valid(ddata->nreset_gpio))
 324                gpio_set_value(ddata->nreset_gpio, 0);
 325
 326        /* wait for at least 2 vsyncs before cutting off power */
 327        msleep(50);
 328
 329        tpo_td043_write(ddata->spi, 3, TPO_R03_VAL_STANDBY);
 330
 331        regulator_disable(ddata->vcc_reg);
 332
 333        ddata->powered_on = 0;
 334}
 335
 336static int tpo_td043_connect(struct omap_dss_device *dssdev)
 337{
 338        struct panel_drv_data *ddata = to_panel_data(dssdev);
 339        struct omap_dss_device *in = ddata->in;
 340        int r;
 341
 342        if (omapdss_device_is_connected(dssdev))
 343                return 0;
 344
 345        r = in->ops.dpi->connect(in, dssdev);
 346        if (r)
 347                return r;
 348
 349        return 0;
 350}
 351
 352static void tpo_td043_disconnect(struct omap_dss_device *dssdev)
 353{
 354        struct panel_drv_data *ddata = to_panel_data(dssdev);
 355        struct omap_dss_device *in = ddata->in;
 356
 357        if (!omapdss_device_is_connected(dssdev))
 358                return;
 359
 360        in->ops.dpi->disconnect(in, dssdev);
 361}
 362
 363static int tpo_td043_enable(struct omap_dss_device *dssdev)
 364{
 365        struct panel_drv_data *ddata = to_panel_data(dssdev);
 366        struct omap_dss_device *in = ddata->in;
 367        int r;
 368
 369        if (!omapdss_device_is_connected(dssdev))
 370                return -ENODEV;
 371
 372        if (omapdss_device_is_enabled(dssdev))
 373                return 0;
 374
 375        if (ddata->data_lines)
 376                in->ops.dpi->set_data_lines(in, ddata->data_lines);
 377        in->ops.dpi->set_timings(in, &ddata->videomode);
 378
 379        r = in->ops.dpi->enable(in);
 380        if (r)
 381                return r;
 382
 383        /*
 384         * If we are resuming from system suspend, SPI clocks might not be
 385         * enabled yet, so we'll program the LCD from SPI PM resume callback.
 386         */
 387        if (!ddata->spi_suspended) {
 388                r = tpo_td043_power_on(ddata);
 389                if (r) {
 390                        in->ops.dpi->disable(in);
 391                        return r;
 392                }
 393        }
 394
 395        dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
 396
 397        return 0;
 398}
 399
 400static void tpo_td043_disable(struct omap_dss_device *dssdev)
 401{
 402        struct panel_drv_data *ddata = to_panel_data(dssdev);
 403        struct omap_dss_device *in = ddata->in;
 404
 405        if (!omapdss_device_is_enabled(dssdev))
 406                return;
 407
 408        in->ops.dpi->disable(in);
 409
 410        if (!ddata->spi_suspended)
 411                tpo_td043_power_off(ddata);
 412
 413        dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
 414}
 415
 416static void tpo_td043_set_timings(struct omap_dss_device *dssdev,
 417                struct omap_video_timings *timings)
 418{
 419        struct panel_drv_data *ddata = to_panel_data(dssdev);
 420        struct omap_dss_device *in = ddata->in;
 421
 422        ddata->videomode = *timings;
 423        dssdev->panel.timings = *timings;
 424
 425        in->ops.dpi->set_timings(in, timings);
 426}
 427
 428static void tpo_td043_get_timings(struct omap_dss_device *dssdev,
 429                struct omap_video_timings *timings)
 430{
 431        struct panel_drv_data *ddata = to_panel_data(dssdev);
 432
 433        *timings = ddata->videomode;
 434}
 435
 436static int tpo_td043_check_timings(struct omap_dss_device *dssdev,
 437                struct omap_video_timings *timings)
 438{
 439        struct panel_drv_data *ddata = to_panel_data(dssdev);
 440        struct omap_dss_device *in = ddata->in;
 441
 442        return in->ops.dpi->check_timings(in, timings);
 443}
 444
 445static struct omap_dss_driver tpo_td043_ops = {
 446        .connect        = tpo_td043_connect,
 447        .disconnect     = tpo_td043_disconnect,
 448
 449        .enable         = tpo_td043_enable,
 450        .disable        = tpo_td043_disable,
 451
 452        .set_timings    = tpo_td043_set_timings,
 453        .get_timings    = tpo_td043_get_timings,
 454        .check_timings  = tpo_td043_check_timings,
 455
 456        .set_mirror     = tpo_td043_set_hmirror,
 457        .get_mirror     = tpo_td043_get_hmirror,
 458
 459        .get_resolution = omapdss_default_get_resolution,
 460};
 461
 462
 463static int tpo_td043_probe_of(struct spi_device *spi)
 464{
 465        struct device_node *node = spi->dev.of_node;
 466        struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
 467        struct omap_dss_device *in;
 468        int gpio;
 469
 470        gpio = of_get_named_gpio(node, "reset-gpios", 0);
 471        if (!gpio_is_valid(gpio)) {
 472                dev_err(&spi->dev, "failed to parse enable gpio\n");
 473                return gpio;
 474        }
 475        ddata->nreset_gpio = gpio;
 476
 477        in = omapdss_of_find_source_for_first_ep(node);
 478        if (IS_ERR(in)) {
 479                dev_err(&spi->dev, "failed to find video source\n");
 480                return PTR_ERR(in);
 481        }
 482
 483        ddata->in = in;
 484
 485        return 0;
 486}
 487
 488static int tpo_td043_probe(struct spi_device *spi)
 489{
 490        struct panel_drv_data *ddata;
 491        struct omap_dss_device *dssdev;
 492        int r;
 493
 494        dev_dbg(&spi->dev, "%s\n", __func__);
 495
 496        if (!spi->dev.of_node)
 497                return -ENODEV;
 498
 499        spi->bits_per_word = 16;
 500        spi->mode = SPI_MODE_0;
 501
 502        r = spi_setup(spi);
 503        if (r < 0) {
 504                dev_err(&spi->dev, "spi_setup failed: %d\n", r);
 505                return r;
 506        }
 507
 508        ddata = devm_kzalloc(&spi->dev, sizeof(*ddata), GFP_KERNEL);
 509        if (ddata == NULL)
 510                return -ENOMEM;
 511
 512        dev_set_drvdata(&spi->dev, ddata);
 513
 514        ddata->spi = spi;
 515
 516        r = tpo_td043_probe_of(spi);
 517        if (r)
 518                return r;
 519
 520        ddata->mode = TPO_R02_MODE_800x480;
 521        memcpy(ddata->gamma, tpo_td043_def_gamma, sizeof(ddata->gamma));
 522
 523        ddata->vcc_reg = devm_regulator_get(&spi->dev, "vcc");
 524        if (IS_ERR(ddata->vcc_reg)) {
 525                dev_err(&spi->dev, "failed to get LCD VCC regulator\n");
 526                r = PTR_ERR(ddata->vcc_reg);
 527                goto err_regulator;
 528        }
 529
 530        if (gpio_is_valid(ddata->nreset_gpio)) {
 531                r = devm_gpio_request_one(&spi->dev,
 532                                ddata->nreset_gpio, GPIOF_OUT_INIT_LOW,
 533                                "lcd reset");
 534                if (r < 0) {
 535                        dev_err(&spi->dev, "couldn't request reset GPIO\n");
 536                        goto err_gpio_req;
 537                }
 538        }
 539
 540        r = sysfs_create_group(&spi->dev.kobj, &tpo_td043_attr_group);
 541        if (r) {
 542                dev_err(&spi->dev, "failed to create sysfs files\n");
 543                goto err_sysfs;
 544        }
 545
 546        ddata->videomode = tpo_td043_timings;
 547
 548        dssdev = &ddata->dssdev;
 549        dssdev->dev = &spi->dev;
 550        dssdev->driver = &tpo_td043_ops;
 551        dssdev->type = OMAP_DISPLAY_TYPE_DPI;
 552        dssdev->owner = THIS_MODULE;
 553        dssdev->panel.timings = ddata->videomode;
 554
 555        r = omapdss_register_display(dssdev);
 556        if (r) {
 557                dev_err(&spi->dev, "Failed to register panel\n");
 558                goto err_reg;
 559        }
 560
 561        return 0;
 562
 563err_reg:
 564        sysfs_remove_group(&spi->dev.kobj, &tpo_td043_attr_group);
 565err_sysfs:
 566err_gpio_req:
 567err_regulator:
 568        omap_dss_put_device(ddata->in);
 569        return r;
 570}
 571
 572static int tpo_td043_remove(struct spi_device *spi)
 573{
 574        struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
 575        struct omap_dss_device *dssdev = &ddata->dssdev;
 576        struct omap_dss_device *in = ddata->in;
 577
 578        dev_dbg(&ddata->spi->dev, "%s\n", __func__);
 579
 580        omapdss_unregister_display(dssdev);
 581
 582        tpo_td043_disable(dssdev);
 583        tpo_td043_disconnect(dssdev);
 584
 585        omap_dss_put_device(in);
 586
 587        sysfs_remove_group(&spi->dev.kobj, &tpo_td043_attr_group);
 588
 589        return 0;
 590}
 591
 592#ifdef CONFIG_PM_SLEEP
 593static int tpo_td043_spi_suspend(struct device *dev)
 594{
 595        struct panel_drv_data *ddata = dev_get_drvdata(dev);
 596
 597        dev_dbg(dev, "tpo_td043_spi_suspend, tpo %p\n", ddata);
 598
 599        ddata->power_on_resume = ddata->powered_on;
 600        tpo_td043_power_off(ddata);
 601        ddata->spi_suspended = 1;
 602
 603        return 0;
 604}
 605
 606static int tpo_td043_spi_resume(struct device *dev)
 607{
 608        struct panel_drv_data *ddata = dev_get_drvdata(dev);
 609        int ret;
 610
 611        dev_dbg(dev, "tpo_td043_spi_resume\n");
 612
 613        if (ddata->power_on_resume) {
 614                ret = tpo_td043_power_on(ddata);
 615                if (ret)
 616                        return ret;
 617        }
 618        ddata->spi_suspended = 0;
 619
 620        return 0;
 621}
 622#endif
 623
 624static SIMPLE_DEV_PM_OPS(tpo_td043_spi_pm,
 625        tpo_td043_spi_suspend, tpo_td043_spi_resume);
 626
 627static const struct of_device_id tpo_td043_of_match[] = {
 628        { .compatible = "omapdss,tpo,td043mtea1", },
 629        {},
 630};
 631
 632MODULE_DEVICE_TABLE(of, tpo_td043_of_match);
 633
 634static struct spi_driver tpo_td043_spi_driver = {
 635        .driver = {
 636                .name   = "panel-tpo-td043mtea1",
 637                .pm     = &tpo_td043_spi_pm,
 638                .of_match_table = tpo_td043_of_match,
 639                .suppress_bind_attrs = true,
 640        },
 641        .probe  = tpo_td043_probe,
 642        .remove = tpo_td043_remove,
 643};
 644
 645module_spi_driver(tpo_td043_spi_driver);
 646
 647MODULE_ALIAS("spi:tpo,td043mtea1");
 648MODULE_AUTHOR("Gražvydas Ignotas <notasas@gmail.com>");
 649MODULE_DESCRIPTION("TPO TD043MTEA1 LCD Driver");
 650MODULE_LICENSE("GPL");
 651