Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * LG.Philips LB035Q02 LCD Panel Driver
0004  *
0005  * Copyright (C) 2019 Texas Instruments Incorporated
0006  *
0007  * Based on the omapdrm-specific panel-lgphilips-lb035q02 driver
0008  *
0009  * Copyright (C) 2013 Texas Instruments Incorporated
0010  * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
0011  *
0012  * Based on a driver by: Steve Sakoman <steve@sakoman.com>
0013  */
0014 
0015 #include <linux/gpio/consumer.h>
0016 #include <linux/module.h>
0017 #include <linux/spi/spi.h>
0018 
0019 #include <drm/drm_connector.h>
0020 #include <drm/drm_modes.h>
0021 #include <drm/drm_panel.h>
0022 
0023 struct lb035q02_device {
0024     struct drm_panel panel;
0025 
0026     struct spi_device *spi;
0027     struct gpio_desc *enable_gpio;
0028 };
0029 
0030 #define to_lb035q02_device(p) container_of(p, struct lb035q02_device, panel)
0031 
0032 static int lb035q02_write(struct lb035q02_device *lcd, u16 reg, u16 val)
0033 {
0034     struct spi_message msg;
0035     struct spi_transfer index_xfer = {
0036         .len        = 3,
0037         .cs_change  = 1,
0038     };
0039     struct spi_transfer value_xfer = {
0040         .len        = 3,
0041     };
0042     u8  buffer[16];
0043 
0044     spi_message_init(&msg);
0045 
0046     /* register index */
0047     buffer[0] = 0x70;
0048     buffer[1] = 0x00;
0049     buffer[2] = reg & 0x7f;
0050     index_xfer.tx_buf = buffer;
0051     spi_message_add_tail(&index_xfer, &msg);
0052 
0053     /* register value */
0054     buffer[4] = 0x72;
0055     buffer[5] = val >> 8;
0056     buffer[6] = val;
0057     value_xfer.tx_buf = buffer + 4;
0058     spi_message_add_tail(&value_xfer, &msg);
0059 
0060     return spi_sync(lcd->spi, &msg);
0061 }
0062 
0063 static int lb035q02_init(struct lb035q02_device *lcd)
0064 {
0065     /* Init sequence from page 28 of the lb035q02 spec. */
0066     static const struct {
0067         u16 index;
0068         u16 value;
0069     } init_data[] = {
0070         { 0x01, 0x6300 },
0071         { 0x02, 0x0200 },
0072         { 0x03, 0x0177 },
0073         { 0x04, 0x04c7 },
0074         { 0x05, 0xffc0 },
0075         { 0x06, 0xe806 },
0076         { 0x0a, 0x4008 },
0077         { 0x0b, 0x0000 },
0078         { 0x0d, 0x0030 },
0079         { 0x0e, 0x2800 },
0080         { 0x0f, 0x0000 },
0081         { 0x16, 0x9f80 },
0082         { 0x17, 0x0a0f },
0083         { 0x1e, 0x00c1 },
0084         { 0x30, 0x0300 },
0085         { 0x31, 0x0007 },
0086         { 0x32, 0x0000 },
0087         { 0x33, 0x0000 },
0088         { 0x34, 0x0707 },
0089         { 0x35, 0x0004 },
0090         { 0x36, 0x0302 },
0091         { 0x37, 0x0202 },
0092         { 0x3a, 0x0a0d },
0093         { 0x3b, 0x0806 },
0094     };
0095 
0096     unsigned int i;
0097     int ret;
0098 
0099     for (i = 0; i < ARRAY_SIZE(init_data); ++i) {
0100         ret = lb035q02_write(lcd, init_data[i].index,
0101                      init_data[i].value);
0102         if (ret < 0)
0103             return ret;
0104     }
0105 
0106     return 0;
0107 }
0108 
0109 static int lb035q02_disable(struct drm_panel *panel)
0110 {
0111     struct lb035q02_device *lcd = to_lb035q02_device(panel);
0112 
0113     gpiod_set_value_cansleep(lcd->enable_gpio, 0);
0114 
0115     return 0;
0116 }
0117 
0118 static int lb035q02_enable(struct drm_panel *panel)
0119 {
0120     struct lb035q02_device *lcd = to_lb035q02_device(panel);
0121 
0122     gpiod_set_value_cansleep(lcd->enable_gpio, 1);
0123 
0124     return 0;
0125 }
0126 
0127 static const struct drm_display_mode lb035q02_mode = {
0128     .clock = 6500,
0129     .hdisplay = 320,
0130     .hsync_start = 320 + 20,
0131     .hsync_end = 320 + 20 + 2,
0132     .htotal = 320 + 20 + 2 + 68,
0133     .vdisplay = 240,
0134     .vsync_start = 240 + 4,
0135     .vsync_end = 240 + 4 + 2,
0136     .vtotal = 240 + 4 + 2 + 18,
0137     .type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
0138     .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
0139     .width_mm = 70,
0140     .height_mm = 53,
0141 };
0142 
0143 static int lb035q02_get_modes(struct drm_panel *panel,
0144                   struct drm_connector *connector)
0145 {
0146     struct drm_display_mode *mode;
0147 
0148     mode = drm_mode_duplicate(connector->dev, &lb035q02_mode);
0149     if (!mode)
0150         return -ENOMEM;
0151 
0152     drm_mode_set_name(mode);
0153     drm_mode_probed_add(connector, mode);
0154 
0155     connector->display_info.width_mm = lb035q02_mode.width_mm;
0156     connector->display_info.height_mm = lb035q02_mode.height_mm;
0157     /*
0158      * FIXME: According to the datasheet pixel data is sampled on the
0159      * rising edge of the clock, but the code running on the Gumstix Overo
0160      * Palo35 indicates sampling on the negative edge. This should be
0161      * tested on a real device.
0162      */
0163     connector->display_info.bus_flags = DRM_BUS_FLAG_DE_HIGH
0164                       | DRM_BUS_FLAG_SYNC_SAMPLE_POSEDGE
0165                       | DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE;
0166 
0167     return 1;
0168 }
0169 
0170 static const struct drm_panel_funcs lb035q02_funcs = {
0171     .disable = lb035q02_disable,
0172     .enable = lb035q02_enable,
0173     .get_modes = lb035q02_get_modes,
0174 };
0175 
0176 static int lb035q02_probe(struct spi_device *spi)
0177 {
0178     struct lb035q02_device *lcd;
0179     int ret;
0180 
0181     lcd = devm_kzalloc(&spi->dev, sizeof(*lcd), GFP_KERNEL);
0182     if (!lcd)
0183         return -ENOMEM;
0184 
0185     spi_set_drvdata(spi, lcd);
0186     lcd->spi = spi;
0187 
0188     lcd->enable_gpio = devm_gpiod_get(&spi->dev, "enable", GPIOD_OUT_LOW);
0189     if (IS_ERR(lcd->enable_gpio)) {
0190         dev_err(&spi->dev, "failed to parse enable gpio\n");
0191         return PTR_ERR(lcd->enable_gpio);
0192     }
0193 
0194     ret = lb035q02_init(lcd);
0195     if (ret < 0)
0196         return ret;
0197 
0198     drm_panel_init(&lcd->panel, &lcd->spi->dev, &lb035q02_funcs,
0199                DRM_MODE_CONNECTOR_DPI);
0200 
0201     drm_panel_add(&lcd->panel);
0202 
0203     return 0;
0204 }
0205 
0206 static void lb035q02_remove(struct spi_device *spi)
0207 {
0208     struct lb035q02_device *lcd = spi_get_drvdata(spi);
0209 
0210     drm_panel_remove(&lcd->panel);
0211     drm_panel_disable(&lcd->panel);
0212 }
0213 
0214 static const struct of_device_id lb035q02_of_match[] = {
0215     { .compatible = "lgphilips,lb035q02", },
0216     { /* sentinel */ },
0217 };
0218 
0219 MODULE_DEVICE_TABLE(of, lb035q02_of_match);
0220 
0221 static const struct spi_device_id lb035q02_ids[] = {
0222     { "lb035q02", 0 },
0223     { /* sentinel */ }
0224 };
0225 
0226 MODULE_DEVICE_TABLE(spi, lb035q02_ids);
0227 
0228 static struct spi_driver lb035q02_driver = {
0229     .probe      = lb035q02_probe,
0230     .remove     = lb035q02_remove,
0231     .id_table   = lb035q02_ids,
0232     .driver     = {
0233         .name   = "panel-lg-lb035q02",
0234         .of_match_table = lb035q02_of_match,
0235     },
0236 };
0237 
0238 module_spi_driver(lb035q02_driver);
0239 
0240 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
0241 MODULE_DESCRIPTION("LG.Philips LB035Q02 LCD Panel driver");
0242 MODULE_LICENSE("GPL");