Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * TPO TD043MTEA1 Panel driver
0004  *
0005  * Author: Gražvydas Ignotas <notasas@gmail.com>
0006  * Converted to new DSS device model: Tomi Valkeinen <tomi.valkeinen@ti.com>
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <linux/delay.h>
0011 #include <linux/spi/spi.h>
0012 #include <linux/regulator/consumer.h>
0013 #include <linux/gpio.h>
0014 #include <linux/err.h>
0015 #include <linux/slab.h>
0016 #include <linux/of_gpio.h>
0017 
0018 #include <video/omapfb_dss.h>
0019 
0020 #define TPO_R02_MODE(x)     ((x) & 7)
0021 #define TPO_R02_MODE_800x480    7
0022 #define TPO_R02_NCLK_RISING BIT(3)
0023 #define TPO_R02_HSYNC_HIGH  BIT(4)
0024 #define TPO_R02_VSYNC_HIGH  BIT(5)
0025 
0026 #define TPO_R03_NSTANDBY    BIT(0)
0027 #define TPO_R03_EN_CP_CLK   BIT(1)
0028 #define TPO_R03_EN_VGL_PUMP BIT(2)
0029 #define TPO_R03_EN_PWM      BIT(3)
0030 #define TPO_R03_DRIVING_CAP_100 BIT(4)
0031 #define TPO_R03_EN_PRE_CHARGE   BIT(6)
0032 #define TPO_R03_SOFTWARE_CTL    BIT(7)
0033 
0034 #define TPO_R04_NFLIP_H     BIT(0)
0035 #define TPO_R04_NFLIP_V     BIT(1)
0036 #define TPO_R04_CP_CLK_FREQ_1H  BIT(2)
0037 #define TPO_R04_VGL_FREQ_1H BIT(4)
0038 
0039 #define TPO_R03_VAL_NORMAL (TPO_R03_NSTANDBY | TPO_R03_EN_CP_CLK | \
0040             TPO_R03_EN_VGL_PUMP |  TPO_R03_EN_PWM | \
0041             TPO_R03_DRIVING_CAP_100 | TPO_R03_EN_PRE_CHARGE | \
0042             TPO_R03_SOFTWARE_CTL)
0043 
0044 #define TPO_R03_VAL_STANDBY (TPO_R03_DRIVING_CAP_100 | \
0045             TPO_R03_EN_PRE_CHARGE | TPO_R03_SOFTWARE_CTL)
0046 
0047 static const u16 tpo_td043_def_gamma[12] = {
0048     105, 315, 381, 431, 490, 537, 579, 686, 780, 837, 880, 1023
0049 };
0050 
0051 struct panel_drv_data {
0052     struct omap_dss_device  dssdev;
0053     struct omap_dss_device *in;
0054 
0055     struct omap_video_timings videomode;
0056 
0057     int data_lines;
0058 
0059     struct spi_device *spi;
0060     struct regulator *vcc_reg;
0061     int nreset_gpio;
0062     u16 gamma[12];
0063     u32 mode;
0064     u32 hmirror:1;
0065     u32 vmirror:1;
0066     u32 powered_on:1;
0067     u32 spi_suspended:1;
0068     u32 power_on_resume:1;
0069 };
0070 
0071 static const struct omap_video_timings tpo_td043_timings = {
0072     .x_res      = 800,
0073     .y_res      = 480,
0074 
0075     .pixelclock = 36000000,
0076 
0077     .hsw        = 1,
0078     .hfp        = 68,
0079     .hbp        = 214,
0080 
0081     .vsw        = 1,
0082     .vfp        = 39,
0083     .vbp        = 34,
0084 
0085     .vsync_level    = OMAPDSS_SIG_ACTIVE_LOW,
0086     .hsync_level    = OMAPDSS_SIG_ACTIVE_LOW,
0087     .data_pclk_edge = OMAPDSS_DRIVE_SIG_FALLING_EDGE,
0088     .de_level   = OMAPDSS_SIG_ACTIVE_HIGH,
0089     .sync_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE,
0090 };
0091 
0092 #define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
0093 
0094 static int tpo_td043_write(struct spi_device *spi, u8 addr, u8 data)
0095 {
0096     struct spi_message  m;
0097     struct spi_transfer xfer;
0098     u16         w;
0099     int         r;
0100 
0101     spi_message_init(&m);
0102 
0103     memset(&xfer, 0, sizeof(xfer));
0104 
0105     w = ((u16)addr << 10) | (1 << 8) | data;
0106     xfer.tx_buf = &w;
0107     xfer.bits_per_word = 16;
0108     xfer.len = 2;
0109     spi_message_add_tail(&xfer, &m);
0110 
0111     r = spi_sync(spi, &m);
0112     if (r < 0)
0113         dev_warn(&spi->dev, "failed to write to LCD reg (%d)\n", r);
0114     return r;
0115 }
0116 
0117 static void tpo_td043_write_gamma(struct spi_device *spi, u16 gamma[12])
0118 {
0119     u8 i, val;
0120 
0121     /* gamma bits [9:8] */
0122     for (val = i = 0; i < 4; i++)
0123         val |= (gamma[i] & 0x300) >> ((i + 1) * 2);
0124     tpo_td043_write(spi, 0x11, val);
0125 
0126     for (val = i = 0; i < 4; i++)
0127         val |= (gamma[i+4] & 0x300) >> ((i + 1) * 2);
0128     tpo_td043_write(spi, 0x12, val);
0129 
0130     for (val = i = 0; i < 4; i++)
0131         val |= (gamma[i+8] & 0x300) >> ((i + 1) * 2);
0132     tpo_td043_write(spi, 0x13, val);
0133 
0134     /* gamma bits [7:0] */
0135     for (val = i = 0; i < 12; i++)
0136         tpo_td043_write(spi, 0x14 + i, gamma[i] & 0xff);
0137 }
0138 
0139 static int tpo_td043_write_mirror(struct spi_device *spi, bool h, bool v)
0140 {
0141     u8 reg4 = TPO_R04_NFLIP_H | TPO_R04_NFLIP_V |
0142         TPO_R04_CP_CLK_FREQ_1H | TPO_R04_VGL_FREQ_1H;
0143     if (h)
0144         reg4 &= ~TPO_R04_NFLIP_H;
0145     if (v)
0146         reg4 &= ~TPO_R04_NFLIP_V;
0147 
0148     return tpo_td043_write(spi, 4, reg4);
0149 }
0150 
0151 static int tpo_td043_set_hmirror(struct omap_dss_device *dssdev, bool enable)
0152 {
0153     struct panel_drv_data *ddata = dev_get_drvdata(dssdev->dev);
0154 
0155     ddata->hmirror = enable;
0156     return tpo_td043_write_mirror(ddata->spi, ddata->hmirror,
0157             ddata->vmirror);
0158 }
0159 
0160 static bool tpo_td043_get_hmirror(struct omap_dss_device *dssdev)
0161 {
0162     struct panel_drv_data *ddata = dev_get_drvdata(dssdev->dev);
0163 
0164     return ddata->hmirror;
0165 }
0166 
0167 static ssize_t tpo_td043_vmirror_show(struct device *dev,
0168     struct device_attribute *attr, char *buf)
0169 {
0170     struct panel_drv_data *ddata = dev_get_drvdata(dev);
0171 
0172     return sysfs_emit(buf, "%d\n", ddata->vmirror);
0173 }
0174 
0175 static ssize_t tpo_td043_vmirror_store(struct device *dev,
0176     struct device_attribute *attr, const char *buf, size_t count)
0177 {
0178     struct panel_drv_data *ddata = dev_get_drvdata(dev);
0179     int val;
0180     int ret;
0181 
0182     ret = kstrtoint(buf, 0, &val);
0183     if (ret < 0)
0184         return ret;
0185 
0186     val = !!val;
0187 
0188     ret = tpo_td043_write_mirror(ddata->spi, ddata->hmirror, val);
0189     if (ret < 0)
0190         return ret;
0191 
0192     ddata->vmirror = val;
0193 
0194     return count;
0195 }
0196 
0197 static ssize_t tpo_td043_mode_show(struct device *dev,
0198     struct device_attribute *attr, char *buf)
0199 {
0200     struct panel_drv_data *ddata = dev_get_drvdata(dev);
0201 
0202     return sysfs_emit(buf, "%d\n", ddata->mode);
0203 }
0204 
0205 static ssize_t tpo_td043_mode_store(struct device *dev,
0206     struct device_attribute *attr, const char *buf, size_t count)
0207 {
0208     struct panel_drv_data *ddata = dev_get_drvdata(dev);
0209     long val;
0210     int ret;
0211 
0212     ret = kstrtol(buf, 0, &val);
0213     if (ret != 0 || val & ~7)
0214         return -EINVAL;
0215 
0216     ddata->mode = val;
0217 
0218     val |= TPO_R02_NCLK_RISING;
0219     tpo_td043_write(ddata->spi, 2, val);
0220 
0221     return count;
0222 }
0223 
0224 static ssize_t tpo_td043_gamma_show(struct device *dev,
0225     struct device_attribute *attr, char *buf)
0226 {
0227     struct panel_drv_data *ddata = dev_get_drvdata(dev);
0228     ssize_t len = 0;
0229     int ret;
0230     int i;
0231 
0232     for (i = 0; i < ARRAY_SIZE(ddata->gamma); i++) {
0233         ret = snprintf(buf + len, PAGE_SIZE - len, "%u ",
0234                 ddata->gamma[i]);
0235         if (ret < 0)
0236             return ret;
0237         len += ret;
0238     }
0239     buf[len - 1] = '\n';
0240 
0241     return len;
0242 }
0243 
0244 static ssize_t tpo_td043_gamma_store(struct device *dev,
0245     struct device_attribute *attr, const char *buf, size_t count)
0246 {
0247     struct panel_drv_data *ddata = dev_get_drvdata(dev);
0248     unsigned int g[12];
0249     int ret;
0250     int i;
0251 
0252     ret = sscanf(buf, "%u %u %u %u %u %u %u %u %u %u %u %u",
0253             &g[0], &g[1], &g[2], &g[3], &g[4], &g[5],
0254             &g[6], &g[7], &g[8], &g[9], &g[10], &g[11]);
0255 
0256     if (ret != 12)
0257         return -EINVAL;
0258 
0259     for (i = 0; i < 12; i++)
0260         ddata->gamma[i] = g[i];
0261 
0262     tpo_td043_write_gamma(ddata->spi, ddata->gamma);
0263 
0264     return count;
0265 }
0266 
0267 static DEVICE_ATTR(vmirror, S_IRUGO | S_IWUSR,
0268         tpo_td043_vmirror_show, tpo_td043_vmirror_store);
0269 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
0270         tpo_td043_mode_show, tpo_td043_mode_store);
0271 static DEVICE_ATTR(gamma, S_IRUGO | S_IWUSR,
0272         tpo_td043_gamma_show, tpo_td043_gamma_store);
0273 
0274 static struct attribute *tpo_td043_attrs[] = {
0275     &dev_attr_vmirror.attr,
0276     &dev_attr_mode.attr,
0277     &dev_attr_gamma.attr,
0278     NULL,
0279 };
0280 
0281 static const struct attribute_group tpo_td043_attr_group = {
0282     .attrs = tpo_td043_attrs,
0283 };
0284 
0285 static int tpo_td043_power_on(struct panel_drv_data *ddata)
0286 {
0287     int r;
0288 
0289     if (ddata->powered_on)
0290         return 0;
0291 
0292     r = regulator_enable(ddata->vcc_reg);
0293     if (r != 0)
0294         return r;
0295 
0296     /* wait for panel to stabilize */
0297     msleep(160);
0298 
0299     if (gpio_is_valid(ddata->nreset_gpio))
0300         gpio_set_value(ddata->nreset_gpio, 1);
0301 
0302     tpo_td043_write(ddata->spi, 2,
0303             TPO_R02_MODE(ddata->mode) | TPO_R02_NCLK_RISING);
0304     tpo_td043_write(ddata->spi, 3, TPO_R03_VAL_NORMAL);
0305     tpo_td043_write(ddata->spi, 0x20, 0xf0);
0306     tpo_td043_write(ddata->spi, 0x21, 0xf0);
0307     tpo_td043_write_mirror(ddata->spi, ddata->hmirror,
0308             ddata->vmirror);
0309     tpo_td043_write_gamma(ddata->spi, ddata->gamma);
0310 
0311     ddata->powered_on = 1;
0312     return 0;
0313 }
0314 
0315 static void tpo_td043_power_off(struct panel_drv_data *ddata)
0316 {
0317     if (!ddata->powered_on)
0318         return;
0319 
0320     tpo_td043_write(ddata->spi, 3,
0321             TPO_R03_VAL_STANDBY | TPO_R03_EN_PWM);
0322 
0323     if (gpio_is_valid(ddata->nreset_gpio))
0324         gpio_set_value(ddata->nreset_gpio, 0);
0325 
0326     /* wait for at least 2 vsyncs before cutting off power */
0327     msleep(50);
0328 
0329     tpo_td043_write(ddata->spi, 3, TPO_R03_VAL_STANDBY);
0330 
0331     regulator_disable(ddata->vcc_reg);
0332 
0333     ddata->powered_on = 0;
0334 }
0335 
0336 static int tpo_td043_connect(struct omap_dss_device *dssdev)
0337 {
0338     struct panel_drv_data *ddata = to_panel_data(dssdev);
0339     struct omap_dss_device *in = ddata->in;
0340 
0341     if (omapdss_device_is_connected(dssdev))
0342         return 0;
0343 
0344     return in->ops.dpi->connect(in, dssdev);
0345 }
0346 
0347 static void tpo_td043_disconnect(struct omap_dss_device *dssdev)
0348 {
0349     struct panel_drv_data *ddata = to_panel_data(dssdev);
0350     struct omap_dss_device *in = ddata->in;
0351 
0352     if (!omapdss_device_is_connected(dssdev))
0353         return;
0354 
0355     in->ops.dpi->disconnect(in, dssdev);
0356 }
0357 
0358 static int tpo_td043_enable(struct omap_dss_device *dssdev)
0359 {
0360     struct panel_drv_data *ddata = to_panel_data(dssdev);
0361     struct omap_dss_device *in = ddata->in;
0362     int r;
0363 
0364     if (!omapdss_device_is_connected(dssdev))
0365         return -ENODEV;
0366 
0367     if (omapdss_device_is_enabled(dssdev))
0368         return 0;
0369 
0370     if (ddata->data_lines)
0371         in->ops.dpi->set_data_lines(in, ddata->data_lines);
0372     in->ops.dpi->set_timings(in, &ddata->videomode);
0373 
0374     r = in->ops.dpi->enable(in);
0375     if (r)
0376         return r;
0377 
0378     /*
0379      * If we are resuming from system suspend, SPI clocks might not be
0380      * enabled yet, so we'll program the LCD from SPI PM resume callback.
0381      */
0382     if (!ddata->spi_suspended) {
0383         r = tpo_td043_power_on(ddata);
0384         if (r) {
0385             in->ops.dpi->disable(in);
0386             return r;
0387         }
0388     }
0389 
0390     dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
0391 
0392     return 0;
0393 }
0394 
0395 static void tpo_td043_disable(struct omap_dss_device *dssdev)
0396 {
0397     struct panel_drv_data *ddata = to_panel_data(dssdev);
0398     struct omap_dss_device *in = ddata->in;
0399 
0400     if (!omapdss_device_is_enabled(dssdev))
0401         return;
0402 
0403     in->ops.dpi->disable(in);
0404 
0405     if (!ddata->spi_suspended)
0406         tpo_td043_power_off(ddata);
0407 
0408     dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
0409 }
0410 
0411 static void tpo_td043_set_timings(struct omap_dss_device *dssdev,
0412         struct omap_video_timings *timings)
0413 {
0414     struct panel_drv_data *ddata = to_panel_data(dssdev);
0415     struct omap_dss_device *in = ddata->in;
0416 
0417     ddata->videomode = *timings;
0418     dssdev->panel.timings = *timings;
0419 
0420     in->ops.dpi->set_timings(in, timings);
0421 }
0422 
0423 static void tpo_td043_get_timings(struct omap_dss_device *dssdev,
0424         struct omap_video_timings *timings)
0425 {
0426     struct panel_drv_data *ddata = to_panel_data(dssdev);
0427 
0428     *timings = ddata->videomode;
0429 }
0430 
0431 static int tpo_td043_check_timings(struct omap_dss_device *dssdev,
0432         struct omap_video_timings *timings)
0433 {
0434     struct panel_drv_data *ddata = to_panel_data(dssdev);
0435     struct omap_dss_device *in = ddata->in;
0436 
0437     return in->ops.dpi->check_timings(in, timings);
0438 }
0439 
0440 static struct omap_dss_driver tpo_td043_ops = {
0441     .connect    = tpo_td043_connect,
0442     .disconnect = tpo_td043_disconnect,
0443 
0444     .enable     = tpo_td043_enable,
0445     .disable    = tpo_td043_disable,
0446 
0447     .set_timings    = tpo_td043_set_timings,
0448     .get_timings    = tpo_td043_get_timings,
0449     .check_timings  = tpo_td043_check_timings,
0450 
0451     .set_mirror = tpo_td043_set_hmirror,
0452     .get_mirror = tpo_td043_get_hmirror,
0453 
0454     .get_resolution = omapdss_default_get_resolution,
0455 };
0456 
0457 
0458 static int tpo_td043_probe_of(struct spi_device *spi)
0459 {
0460     struct device_node *node = spi->dev.of_node;
0461     struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
0462     struct omap_dss_device *in;
0463     int gpio;
0464 
0465     gpio = of_get_named_gpio(node, "reset-gpios", 0);
0466     if (!gpio_is_valid(gpio)) {
0467         dev_err(&spi->dev, "failed to parse enable gpio\n");
0468         return gpio;
0469     }
0470     ddata->nreset_gpio = gpio;
0471 
0472     in = omapdss_of_find_source_for_first_ep(node);
0473     if (IS_ERR(in)) {
0474         dev_err(&spi->dev, "failed to find video source\n");
0475         return PTR_ERR(in);
0476     }
0477 
0478     ddata->in = in;
0479 
0480     return 0;
0481 }
0482 
0483 static int tpo_td043_probe(struct spi_device *spi)
0484 {
0485     struct panel_drv_data *ddata;
0486     struct omap_dss_device *dssdev;
0487     int r;
0488 
0489     dev_dbg(&spi->dev, "%s\n", __func__);
0490 
0491     if (!spi->dev.of_node)
0492         return -ENODEV;
0493 
0494     spi->bits_per_word = 16;
0495     spi->mode = SPI_MODE_0;
0496 
0497     r = spi_setup(spi);
0498     if (r < 0) {
0499         dev_err(&spi->dev, "spi_setup failed: %d\n", r);
0500         return r;
0501     }
0502 
0503     ddata = devm_kzalloc(&spi->dev, sizeof(*ddata), GFP_KERNEL);
0504     if (ddata == NULL)
0505         return -ENOMEM;
0506 
0507     dev_set_drvdata(&spi->dev, ddata);
0508 
0509     ddata->spi = spi;
0510 
0511     r = tpo_td043_probe_of(spi);
0512     if (r)
0513         return r;
0514 
0515     ddata->mode = TPO_R02_MODE_800x480;
0516     memcpy(ddata->gamma, tpo_td043_def_gamma, sizeof(ddata->gamma));
0517 
0518     ddata->vcc_reg = devm_regulator_get(&spi->dev, "vcc");
0519     if (IS_ERR(ddata->vcc_reg)) {
0520         r = dev_err_probe(&spi->dev, r, "failed to get LCD VCC regulator\n");
0521         goto err_regulator;
0522     }
0523 
0524     if (gpio_is_valid(ddata->nreset_gpio)) {
0525         r = devm_gpio_request_one(&spi->dev,
0526                 ddata->nreset_gpio, GPIOF_OUT_INIT_LOW,
0527                 "lcd reset");
0528         if (r < 0) {
0529             dev_err(&spi->dev, "couldn't request reset GPIO\n");
0530             goto err_gpio_req;
0531         }
0532     }
0533 
0534     r = sysfs_create_group(&spi->dev.kobj, &tpo_td043_attr_group);
0535     if (r) {
0536         dev_err(&spi->dev, "failed to create sysfs files\n");
0537         goto err_sysfs;
0538     }
0539 
0540     ddata->videomode = tpo_td043_timings;
0541 
0542     dssdev = &ddata->dssdev;
0543     dssdev->dev = &spi->dev;
0544     dssdev->driver = &tpo_td043_ops;
0545     dssdev->type = OMAP_DISPLAY_TYPE_DPI;
0546     dssdev->owner = THIS_MODULE;
0547     dssdev->panel.timings = ddata->videomode;
0548 
0549     r = omapdss_register_display(dssdev);
0550     if (r) {
0551         dev_err(&spi->dev, "Failed to register panel\n");
0552         goto err_reg;
0553     }
0554 
0555     return 0;
0556 
0557 err_reg:
0558     sysfs_remove_group(&spi->dev.kobj, &tpo_td043_attr_group);
0559 err_sysfs:
0560 err_gpio_req:
0561 err_regulator:
0562     omap_dss_put_device(ddata->in);
0563     return r;
0564 }
0565 
0566 static void tpo_td043_remove(struct spi_device *spi)
0567 {
0568     struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
0569     struct omap_dss_device *dssdev = &ddata->dssdev;
0570     struct omap_dss_device *in = ddata->in;
0571 
0572     dev_dbg(&ddata->spi->dev, "%s\n", __func__);
0573 
0574     omapdss_unregister_display(dssdev);
0575 
0576     tpo_td043_disable(dssdev);
0577     tpo_td043_disconnect(dssdev);
0578 
0579     omap_dss_put_device(in);
0580 
0581     sysfs_remove_group(&spi->dev.kobj, &tpo_td043_attr_group);
0582 }
0583 
0584 #ifdef CONFIG_PM_SLEEP
0585 static int tpo_td043_spi_suspend(struct device *dev)
0586 {
0587     struct panel_drv_data *ddata = dev_get_drvdata(dev);
0588 
0589     dev_dbg(dev, "tpo_td043_spi_suspend, tpo %p\n", ddata);
0590 
0591     ddata->power_on_resume = ddata->powered_on;
0592     tpo_td043_power_off(ddata);
0593     ddata->spi_suspended = 1;
0594 
0595     return 0;
0596 }
0597 
0598 static int tpo_td043_spi_resume(struct device *dev)
0599 {
0600     struct panel_drv_data *ddata = dev_get_drvdata(dev);
0601     int ret;
0602 
0603     dev_dbg(dev, "tpo_td043_spi_resume\n");
0604 
0605     if (ddata->power_on_resume) {
0606         ret = tpo_td043_power_on(ddata);
0607         if (ret)
0608             return ret;
0609     }
0610     ddata->spi_suspended = 0;
0611 
0612     return 0;
0613 }
0614 #endif
0615 
0616 static SIMPLE_DEV_PM_OPS(tpo_td043_spi_pm,
0617     tpo_td043_spi_suspend, tpo_td043_spi_resume);
0618 
0619 static const struct of_device_id tpo_td043_of_match[] = {
0620     { .compatible = "omapdss,tpo,td043mtea1", },
0621     {},
0622 };
0623 
0624 MODULE_DEVICE_TABLE(of, tpo_td043_of_match);
0625 
0626 static struct spi_driver tpo_td043_spi_driver = {
0627     .driver = {
0628         .name   = "panel-tpo-td043mtea1",
0629         .pm = &tpo_td043_spi_pm,
0630         .of_match_table = tpo_td043_of_match,
0631         .suppress_bind_attrs = true,
0632     },
0633     .probe  = tpo_td043_probe,
0634     .remove = tpo_td043_remove,
0635 };
0636 
0637 module_spi_driver(tpo_td043_spi_driver);
0638 
0639 MODULE_ALIAS("spi:tpo,td043mtea1");
0640 MODULE_AUTHOR("Gražvydas Ignotas <notasas@gmail.com>");
0641 MODULE_DESCRIPTION("TPO TD043MTEA1 LCD Driver");
0642 MODULE_LICENSE("GPL");