Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * ld9040 AMOLED LCD drm_panel driver.
0004  *
0005  * Copyright (c) 2014 Samsung Electronics Co., Ltd
0006  * Derived from drivers/video/backlight/ld9040.c
0007  *
0008  * Andrzej Hajda <a.hajda@samsung.com>
0009 */
0010 
0011 #include <linux/delay.h>
0012 #include <linux/gpio/consumer.h>
0013 #include <linux/module.h>
0014 #include <linux/of.h>
0015 #include <linux/regulator/consumer.h>
0016 #include <linux/spi/spi.h>
0017 
0018 #include <video/mipi_display.h>
0019 #include <video/of_videomode.h>
0020 #include <video/videomode.h>
0021 
0022 #include <drm/drm_modes.h>
0023 #include <drm/drm_panel.h>
0024 
0025 /* Manufacturer Command Set */
0026 #define MCS_MANPWR      0xb0
0027 #define MCS_ELVSS_ON        0xb1
0028 #define MCS_USER_SETTING    0xf0
0029 #define MCS_DISPCTL     0xf2
0030 #define MCS_POWER_CTRL      0xf4
0031 #define MCS_GTCON       0xf7
0032 #define MCS_PANEL_CONDITION 0xf8
0033 #define MCS_GAMMA_SET1      0xf9
0034 #define MCS_GAMMA_CTRL      0xfb
0035 
0036 /* array of gamma tables for gamma value 2.2 */
0037 static u8 const ld9040_gammas[25][22] = {
0038     { 0xf9, 0x00, 0x13, 0xb2, 0xba, 0xd2, 0x00, 0x30, 0x00, 0xaf, 0xc0,
0039       0xb8, 0xcd, 0x00, 0x3d, 0x00, 0xa8, 0xb8, 0xb7, 0xcd, 0x00, 0x44 },
0040     { 0xf9, 0x00, 0x13, 0xb9, 0xb9, 0xd0, 0x00, 0x3c, 0x00, 0xaf, 0xbf,
0041       0xb6, 0xcb, 0x00, 0x4b, 0x00, 0xa8, 0xb9, 0xb5, 0xcc, 0x00, 0x52 },
0042     { 0xf9, 0x00, 0x13, 0xba, 0xb9, 0xcd, 0x00, 0x41, 0x00, 0xb0, 0xbe,
0043       0xb5, 0xc9, 0x00, 0x51, 0x00, 0xa9, 0xb9, 0xb5, 0xca, 0x00, 0x57 },
0044     { 0xf9, 0x00, 0x13, 0xb9, 0xb8, 0xcd, 0x00, 0x46, 0x00, 0xb1, 0xbc,
0045       0xb5, 0xc8, 0x00, 0x56, 0x00, 0xaa, 0xb8, 0xb4, 0xc9, 0x00, 0x5d },
0046     { 0xf9, 0x00, 0x13, 0xba, 0xb8, 0xcb, 0x00, 0x4b, 0x00, 0xb3, 0xbc,
0047       0xb4, 0xc7, 0x00, 0x5c, 0x00, 0xac, 0xb8, 0xb4, 0xc8, 0x00, 0x62 },
0048     { 0xf9, 0x00, 0x13, 0xbb, 0xb7, 0xca, 0x00, 0x4f, 0x00, 0xb4, 0xbb,
0049       0xb3, 0xc7, 0x00, 0x60, 0x00, 0xad, 0xb8, 0xb4, 0xc7, 0x00, 0x67 },
0050     { 0xf9, 0x00, 0x47, 0xba, 0xb6, 0xca, 0x00, 0x53, 0x00, 0xb5, 0xbb,
0051       0xb3, 0xc6, 0x00, 0x65, 0x00, 0xae, 0xb8, 0xb3, 0xc7, 0x00, 0x6c },
0052     { 0xf9, 0x00, 0x71, 0xbb, 0xb5, 0xc8, 0x00, 0x57, 0x00, 0xb5, 0xbb,
0053       0xb0, 0xc5, 0x00, 0x6a, 0x00, 0xae, 0xb9, 0xb1, 0xc6, 0x00, 0x70 },
0054     { 0xf9, 0x00, 0x7b, 0xbb, 0xb4, 0xc8, 0x00, 0x5b, 0x00, 0xb5, 0xba,
0055       0xb1, 0xc4, 0x00, 0x6e, 0x00, 0xae, 0xb9, 0xb0, 0xc5, 0x00, 0x75 },
0056     { 0xf9, 0x00, 0x82, 0xba, 0xb4, 0xc7, 0x00, 0x5f, 0x00, 0xb5, 0xba,
0057       0xb0, 0xc3, 0x00, 0x72, 0x00, 0xae, 0xb8, 0xb0, 0xc3, 0x00, 0x7a },
0058     { 0xf9, 0x00, 0x89, 0xba, 0xb3, 0xc8, 0x00, 0x62, 0x00, 0xb6, 0xba,
0059       0xaf, 0xc3, 0x00, 0x76, 0x00, 0xaf, 0xb7, 0xae, 0xc4, 0x00, 0x7e },
0060     { 0xf9, 0x00, 0x8b, 0xb9, 0xb3, 0xc7, 0x00, 0x65, 0x00, 0xb7, 0xb8,
0061       0xaf, 0xc3, 0x00, 0x7a, 0x00, 0x80, 0xb6, 0xae, 0xc4, 0x00, 0x81 },
0062     { 0xf9, 0x00, 0x93, 0xba, 0xb3, 0xc5, 0x00, 0x69, 0x00, 0xb8, 0xb9,
0063       0xae, 0xc1, 0x00, 0x7f, 0x00, 0xb0, 0xb6, 0xae, 0xc3, 0x00, 0x85 },
0064     { 0xf9, 0x00, 0x97, 0xba, 0xb2, 0xc5, 0x00, 0x6c, 0x00, 0xb8, 0xb8,
0065       0xae, 0xc1, 0x00, 0x82, 0x00, 0xb0, 0xb6, 0xae, 0xc2, 0x00, 0x89 },
0066     { 0xf9, 0x00, 0x9a, 0xba, 0xb1, 0xc4, 0x00, 0x6f, 0x00, 0xb8, 0xb8,
0067       0xad, 0xc0, 0x00, 0x86, 0x00, 0xb0, 0xb7, 0xad, 0xc0, 0x00, 0x8d },
0068     { 0xf9, 0x00, 0x9c, 0xb9, 0xb0, 0xc4, 0x00, 0x72, 0x00, 0xb8, 0xb8,
0069       0xac, 0xbf, 0x00, 0x8a, 0x00, 0xb0, 0xb6, 0xac, 0xc0, 0x00, 0x91 },
0070     { 0xf9, 0x00, 0x9e, 0xba, 0xb0, 0xc2, 0x00, 0x75, 0x00, 0xb9, 0xb8,
0071       0xab, 0xbe, 0x00, 0x8e, 0x00, 0xb0, 0xb6, 0xac, 0xbf, 0x00, 0x94 },
0072     { 0xf9, 0x00, 0xa0, 0xb9, 0xaf, 0xc3, 0x00, 0x77, 0x00, 0xb9, 0xb7,
0073       0xab, 0xbe, 0x00, 0x90, 0x00, 0xb0, 0xb6, 0xab, 0xbf, 0x00, 0x97 },
0074     { 0xf9, 0x00, 0xa2, 0xb9, 0xaf, 0xc2, 0x00, 0x7a, 0x00, 0xb9, 0xb7,
0075       0xaa, 0xbd, 0x00, 0x94, 0x00, 0xb0, 0xb5, 0xab, 0xbf, 0x00, 0x9a },
0076     { 0xf9, 0x00, 0xa4, 0xb9, 0xaf, 0xc1, 0x00, 0x7d, 0x00, 0xb9, 0xb6,
0077       0xaa, 0xbb, 0x00, 0x97, 0x00, 0xb1, 0xb5, 0xaa, 0xbf, 0x00, 0x9d },
0078     { 0xf9, 0x00, 0xa4, 0xb8, 0xb0, 0xbf, 0x00, 0x80, 0x00, 0xb8, 0xb6,
0079       0xaa, 0xbc, 0x00, 0x9a, 0x00, 0xb0, 0xb5, 0xab, 0xbd, 0x00, 0xa0 },
0080     { 0xf9, 0x00, 0xa8, 0xb8, 0xae, 0xbe, 0x00, 0x84, 0x00, 0xb9, 0xb7,
0081       0xa8, 0xbc, 0x00, 0x9d, 0x00, 0xb2, 0xb5, 0xaa, 0xbc, 0x00, 0xa4 },
0082     { 0xf9, 0x00, 0xa9, 0xb6, 0xad, 0xbf, 0x00, 0x86, 0x00, 0xb8, 0xb5,
0083       0xa8, 0xbc, 0x00, 0xa0, 0x00, 0xb3, 0xb3, 0xa9, 0xbc, 0x00, 0xa7 },
0084     { 0xf9, 0x00, 0xa9, 0xb7, 0xae, 0xbd, 0x00, 0x89, 0x00, 0xb7, 0xb6,
0085       0xa8, 0xba, 0x00, 0xa4, 0x00, 0xb1, 0xb4, 0xaa, 0xbb, 0x00, 0xaa },
0086     { 0xf9, 0x00, 0xa7, 0xb4, 0xae, 0xbf, 0x00, 0x91, 0x00, 0xb2, 0xb4,
0087       0xaa, 0xbb, 0x00, 0xac, 0x00, 0xb3, 0xb1, 0xaa, 0xbc, 0x00, 0xb3 },
0088 };
0089 
0090 struct ld9040 {
0091     struct device *dev;
0092     struct drm_panel panel;
0093 
0094     struct regulator_bulk_data supplies[2];
0095     struct gpio_desc *reset_gpio;
0096     u32 power_on_delay;
0097     u32 reset_delay;
0098     struct videomode vm;
0099     u32 width_mm;
0100     u32 height_mm;
0101 
0102     int brightness;
0103 
0104     /* This field is tested by functions directly accessing bus before
0105      * transfer, transfer is skipped if it is set. In case of transfer
0106      * failure or unexpected response the field is set to error value.
0107      * Such construct allows to eliminate many checks in higher level
0108      * functions.
0109      */
0110     int error;
0111 };
0112 
0113 static inline struct ld9040 *panel_to_ld9040(struct drm_panel *panel)
0114 {
0115     return container_of(panel, struct ld9040, panel);
0116 }
0117 
0118 static int ld9040_clear_error(struct ld9040 *ctx)
0119 {
0120     int ret = ctx->error;
0121 
0122     ctx->error = 0;
0123     return ret;
0124 }
0125 
0126 static int ld9040_spi_write_word(struct ld9040 *ctx, u16 data)
0127 {
0128     struct spi_device *spi = to_spi_device(ctx->dev);
0129     struct spi_transfer xfer = {
0130         .len        = 2,
0131         .tx_buf     = &data,
0132     };
0133     struct spi_message msg;
0134 
0135     spi_message_init(&msg);
0136     spi_message_add_tail(&xfer, &msg);
0137 
0138     return spi_sync(spi, &msg);
0139 }
0140 
0141 static void ld9040_dcs_write(struct ld9040 *ctx, const u8 *data, size_t len)
0142 {
0143     int ret = 0;
0144 
0145     if (ctx->error < 0 || len == 0)
0146         return;
0147 
0148     dev_dbg(ctx->dev, "writing dcs seq: %*ph\n", (int)len, data);
0149     ret = ld9040_spi_write_word(ctx, *data);
0150 
0151     while (!ret && --len) {
0152         ++data;
0153         ret = ld9040_spi_write_word(ctx, *data | 0x100);
0154     }
0155 
0156     if (ret) {
0157         dev_err(ctx->dev, "error %d writing dcs seq: %*ph\n", ret,
0158             (int)len, data);
0159         ctx->error = ret;
0160     }
0161 
0162     usleep_range(300, 310);
0163 }
0164 
0165 #define ld9040_dcs_write_seq_static(ctx, seq...) \
0166 ({\
0167     static const u8 d[] = { seq };\
0168     ld9040_dcs_write(ctx, d, ARRAY_SIZE(d));\
0169 })
0170 
0171 static void ld9040_brightness_set(struct ld9040 *ctx)
0172 {
0173     ld9040_dcs_write(ctx, ld9040_gammas[ctx->brightness],
0174              ARRAY_SIZE(ld9040_gammas[ctx->brightness]));
0175 
0176     ld9040_dcs_write_seq_static(ctx, MCS_GAMMA_CTRL, 0x02, 0x5a);
0177 }
0178 
0179 static void ld9040_init(struct ld9040 *ctx)
0180 {
0181     ld9040_dcs_write_seq_static(ctx, MCS_USER_SETTING, 0x5a, 0x5a);
0182     ld9040_dcs_write_seq_static(ctx, MCS_PANEL_CONDITION,
0183         0x05, 0x65, 0x96, 0x71, 0x7d, 0x19, 0x3b, 0x0d,
0184         0x19, 0x7e, 0x0d, 0xe2, 0x00, 0x00, 0x7e, 0x7d,
0185         0x07, 0x07, 0x20, 0x20, 0x20, 0x02, 0x02);
0186     ld9040_dcs_write_seq_static(ctx, MCS_DISPCTL,
0187         0x02, 0x08, 0x08, 0x10, 0x10);
0188     ld9040_dcs_write_seq_static(ctx, MCS_MANPWR, 0x04);
0189     ld9040_dcs_write_seq_static(ctx, MCS_POWER_CTRL,
0190         0x0a, 0x87, 0x25, 0x6a, 0x44, 0x02, 0x88);
0191     ld9040_dcs_write_seq_static(ctx, MCS_ELVSS_ON, 0x0d, 0x00, 0x16);
0192     ld9040_dcs_write_seq_static(ctx, MCS_GTCON, 0x09, 0x00, 0x00);
0193     ld9040_brightness_set(ctx);
0194     ld9040_dcs_write_seq_static(ctx, MIPI_DCS_EXIT_SLEEP_MODE);
0195     ld9040_dcs_write_seq_static(ctx, MIPI_DCS_SET_DISPLAY_ON);
0196 }
0197 
0198 static int ld9040_power_on(struct ld9040 *ctx)
0199 {
0200     int ret;
0201 
0202     ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
0203     if (ret < 0)
0204         return ret;
0205 
0206     msleep(ctx->power_on_delay);
0207     gpiod_set_value(ctx->reset_gpio, 0);
0208     msleep(ctx->reset_delay);
0209     gpiod_set_value(ctx->reset_gpio, 1);
0210     msleep(ctx->reset_delay);
0211 
0212     return 0;
0213 }
0214 
0215 static int ld9040_power_off(struct ld9040 *ctx)
0216 {
0217     return regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
0218 }
0219 
0220 static int ld9040_disable(struct drm_panel *panel)
0221 {
0222     return 0;
0223 }
0224 
0225 static int ld9040_unprepare(struct drm_panel *panel)
0226 {
0227     struct ld9040 *ctx = panel_to_ld9040(panel);
0228 
0229     msleep(120);
0230     ld9040_dcs_write_seq_static(ctx, MIPI_DCS_SET_DISPLAY_OFF);
0231     ld9040_dcs_write_seq_static(ctx, MIPI_DCS_ENTER_SLEEP_MODE);
0232     msleep(40);
0233 
0234     ld9040_clear_error(ctx);
0235 
0236     return ld9040_power_off(ctx);
0237 }
0238 
0239 static int ld9040_prepare(struct drm_panel *panel)
0240 {
0241     struct ld9040 *ctx = panel_to_ld9040(panel);
0242     int ret;
0243 
0244     ret = ld9040_power_on(ctx);
0245     if (ret < 0)
0246         return ret;
0247 
0248     ld9040_init(ctx);
0249 
0250     ret = ld9040_clear_error(ctx);
0251 
0252     if (ret < 0)
0253         ld9040_unprepare(panel);
0254 
0255     return ret;
0256 }
0257 
0258 static int ld9040_enable(struct drm_panel *panel)
0259 {
0260     return 0;
0261 }
0262 
0263 static int ld9040_get_modes(struct drm_panel *panel,
0264                 struct drm_connector *connector)
0265 {
0266     struct ld9040 *ctx = panel_to_ld9040(panel);
0267     struct drm_display_mode *mode;
0268 
0269     mode = drm_mode_create(connector->dev);
0270     if (!mode) {
0271         dev_err(panel->dev, "failed to create a new display mode\n");
0272         return 0;
0273     }
0274 
0275     drm_display_mode_from_videomode(&ctx->vm, mode);
0276     mode->width_mm = ctx->width_mm;
0277     mode->height_mm = ctx->height_mm;
0278     connector->display_info.width_mm = mode->width_mm;
0279     connector->display_info.height_mm = mode->height_mm;
0280 
0281     mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
0282     drm_mode_probed_add(connector, mode);
0283 
0284     return 1;
0285 }
0286 
0287 static const struct drm_panel_funcs ld9040_drm_funcs = {
0288     .disable = ld9040_disable,
0289     .unprepare = ld9040_unprepare,
0290     .prepare = ld9040_prepare,
0291     .enable = ld9040_enable,
0292     .get_modes = ld9040_get_modes,
0293 };
0294 
0295 static int ld9040_parse_dt(struct ld9040 *ctx)
0296 {
0297     struct device *dev = ctx->dev;
0298     struct device_node *np = dev->of_node;
0299     int ret;
0300 
0301     ret = of_get_videomode(np, &ctx->vm, 0);
0302     if (ret < 0)
0303         return ret;
0304 
0305     of_property_read_u32(np, "power-on-delay", &ctx->power_on_delay);
0306     of_property_read_u32(np, "reset-delay", &ctx->reset_delay);
0307     of_property_read_u32(np, "panel-width-mm", &ctx->width_mm);
0308     of_property_read_u32(np, "panel-height-mm", &ctx->height_mm);
0309 
0310     return 0;
0311 }
0312 
0313 static int ld9040_probe(struct spi_device *spi)
0314 {
0315     struct device *dev = &spi->dev;
0316     struct ld9040 *ctx;
0317     int ret;
0318 
0319     ctx = devm_kzalloc(dev, sizeof(struct ld9040), GFP_KERNEL);
0320     if (!ctx)
0321         return -ENOMEM;
0322 
0323     spi_set_drvdata(spi, ctx);
0324 
0325     ctx->dev = dev;
0326     ctx->brightness = ARRAY_SIZE(ld9040_gammas) - 1;
0327 
0328     ret = ld9040_parse_dt(ctx);
0329     if (ret < 0)
0330         return ret;
0331 
0332     ctx->supplies[0].supply = "vdd3";
0333     ctx->supplies[1].supply = "vci";
0334     ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
0335                       ctx->supplies);
0336     if (ret < 0)
0337         return ret;
0338 
0339     ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
0340     if (IS_ERR(ctx->reset_gpio)) {
0341         dev_err(dev, "cannot get reset-gpios %ld\n",
0342             PTR_ERR(ctx->reset_gpio));
0343         return PTR_ERR(ctx->reset_gpio);
0344     }
0345 
0346     spi->bits_per_word = 9;
0347     ret = spi_setup(spi);
0348     if (ret < 0) {
0349         dev_err(dev, "spi setup failed.\n");
0350         return ret;
0351     }
0352 
0353     drm_panel_init(&ctx->panel, dev, &ld9040_drm_funcs,
0354                DRM_MODE_CONNECTOR_DPI);
0355 
0356     drm_panel_add(&ctx->panel);
0357 
0358     return 0;
0359 }
0360 
0361 static void ld9040_remove(struct spi_device *spi)
0362 {
0363     struct ld9040 *ctx = spi_get_drvdata(spi);
0364 
0365     ld9040_power_off(ctx);
0366     drm_panel_remove(&ctx->panel);
0367 }
0368 
0369 static const struct of_device_id ld9040_of_match[] = {
0370     { .compatible = "samsung,ld9040" },
0371     { }
0372 };
0373 MODULE_DEVICE_TABLE(of, ld9040_of_match);
0374 
0375 static const struct spi_device_id ld9040_ids[] = {
0376     { "ld9040", },
0377     { /* sentinel */ }
0378 };
0379 MODULE_DEVICE_TABLE(spi, ld9040_ids);
0380 
0381 static struct spi_driver ld9040_driver = {
0382     .probe = ld9040_probe,
0383     .remove = ld9040_remove,
0384     .id_table = ld9040_ids,
0385     .driver = {
0386         .name = "panel-samsung-ld9040",
0387         .of_match_table = ld9040_of_match,
0388     },
0389 };
0390 module_spi_driver(ld9040_driver);
0391 
0392 MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
0393 MODULE_DESCRIPTION("ld9040 LCD Driver");
0394 MODULE_LICENSE("GPL v2");