Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * DRM driver for MIPI DBI compatible display panels
0004  *
0005  * Copyright 2022 Noralf Trønnes
0006  */
0007 
0008 #include <linux/backlight.h>
0009 #include <linux/delay.h>
0010 #include <linux/firmware.h>
0011 #include <linux/gpio/consumer.h>
0012 #include <linux/module.h>
0013 #include <linux/property.h>
0014 #include <linux/regulator/consumer.h>
0015 #include <linux/spi/spi.h>
0016 
0017 #include <drm/drm_atomic_helper.h>
0018 #include <drm/drm_drv.h>
0019 #include <drm/drm_fb_helper.h>
0020 #include <drm/drm_gem_atomic_helper.h>
0021 #include <drm/drm_gem_cma_helper.h>
0022 #include <drm/drm_managed.h>
0023 #include <drm/drm_mipi_dbi.h>
0024 #include <drm/drm_modes.h>
0025 #include <drm/drm_modeset_helper.h>
0026 
0027 #include <video/mipi_display.h>
0028 
0029 static const u8 panel_mipi_dbi_magic[15] = { 'M', 'I', 'P', 'I', ' ', 'D', 'B', 'I',
0030                          0, 0, 0, 0, 0, 0, 0 };
0031 
0032 /*
0033  * The display controller configuration is stored in a firmware file.
0034  * The Device Tree 'compatible' property value with a '.bin' suffix is passed
0035  * to request_firmware() to fetch this file.
0036  */
0037 struct panel_mipi_dbi_config {
0038     /* Magic string: panel_mipi_dbi_magic */
0039     u8 magic[15];
0040 
0041     /* Config file format version */
0042     u8 file_format_version;
0043 
0044     /*
0045      * MIPI commands to execute when the display pipeline is enabled.
0046      * This is used to configure the display controller.
0047      *
0048      * The commands are stored in a byte array with the format:
0049      *     command, num_parameters, [ parameter, ...], command, ...
0050      *
0051      * Some commands require a pause before the next command can be received.
0052      * Inserting a delay in the command sequence is done by using the NOP command with one
0053      * parameter: delay in miliseconds (the No Operation command is part of the MIPI Display
0054      * Command Set where it has no parameters).
0055      *
0056      * Example:
0057      *     command 0x11
0058      *     sleep 120ms
0059      *     command 0xb1 parameters 0x01, 0x2c, 0x2d
0060      *     command 0x29
0061      *
0062      * Byte sequence:
0063      *     0x11 0x00
0064      *     0x00 0x01 0x78
0065      *     0xb1 0x03 0x01 0x2c 0x2d
0066      *     0x29 0x00
0067      */
0068     u8 commands[];
0069 };
0070 
0071 struct panel_mipi_dbi_commands {
0072     const u8 *buf;
0073     size_t len;
0074 };
0075 
0076 static struct panel_mipi_dbi_commands *
0077 panel_mipi_dbi_check_commands(struct device *dev, const struct firmware *fw)
0078 {
0079     const struct panel_mipi_dbi_config *config = (struct panel_mipi_dbi_config *)fw->data;
0080     struct panel_mipi_dbi_commands *commands;
0081     size_t size = fw->size, commands_len;
0082     unsigned int i = 0;
0083 
0084     if (size < sizeof(*config) + 2) { /* At least 1 command */
0085         dev_err(dev, "config: file size=%zu is too small\n", size);
0086         return ERR_PTR(-EINVAL);
0087     }
0088 
0089     if (memcmp(config->magic, panel_mipi_dbi_magic, sizeof(config->magic))) {
0090         dev_err(dev, "config: Bad magic: %15ph\n", config->magic);
0091         return ERR_PTR(-EINVAL);
0092     }
0093 
0094     if (config->file_format_version != 1) {
0095         dev_err(dev, "config: version=%u is not supported\n", config->file_format_version);
0096         return ERR_PTR(-EINVAL);
0097     }
0098 
0099     drm_dev_dbg(dev, DRM_UT_DRIVER, "size=%zu version=%u\n", size, config->file_format_version);
0100 
0101     commands_len = size - sizeof(*config);
0102 
0103     while ((i + 1) < commands_len) {
0104         u8 command = config->commands[i++];
0105         u8 num_parameters = config->commands[i++];
0106         const u8 *parameters = &config->commands[i];
0107 
0108         i += num_parameters;
0109         if (i > commands_len) {
0110             dev_err(dev, "config: command=0x%02x num_parameters=%u overflows\n",
0111                 command, num_parameters);
0112             return ERR_PTR(-EINVAL);
0113         }
0114 
0115         if (command == 0x00 && num_parameters == 1)
0116             drm_dev_dbg(dev, DRM_UT_DRIVER, "sleep %ums\n", parameters[0]);
0117         else
0118             drm_dev_dbg(dev, DRM_UT_DRIVER, "command %02x %*ph\n",
0119                     command, num_parameters, parameters);
0120     }
0121 
0122     if (i != commands_len) {
0123         dev_err(dev, "config: malformed command array\n");
0124         return ERR_PTR(-EINVAL);
0125     }
0126 
0127     commands = devm_kzalloc(dev, sizeof(*commands), GFP_KERNEL);
0128     if (!commands)
0129         return ERR_PTR(-ENOMEM);
0130 
0131     commands->len = commands_len;
0132     commands->buf = devm_kmemdup(dev, config->commands, commands->len, GFP_KERNEL);
0133     if (!commands->buf)
0134         return ERR_PTR(-ENOMEM);
0135 
0136     return commands;
0137 }
0138 
0139 static struct panel_mipi_dbi_commands *panel_mipi_dbi_commands_from_fw(struct device *dev)
0140 {
0141     struct panel_mipi_dbi_commands *commands;
0142     const struct firmware *fw;
0143     const char *compatible;
0144     char fw_name[40];
0145     int ret;
0146 
0147     ret = of_property_read_string_index(dev->of_node, "compatible", 0, &compatible);
0148     if (ret)
0149         return ERR_PTR(ret);
0150 
0151     snprintf(fw_name, sizeof(fw_name), "%s.bin", compatible);
0152     ret = request_firmware(&fw, fw_name, dev);
0153     if (ret) {
0154         dev_err(dev, "No config file found for compatible '%s' (error=%d)\n",
0155             compatible, ret);
0156 
0157         return ERR_PTR(ret);
0158     }
0159 
0160     commands = panel_mipi_dbi_check_commands(dev, fw);
0161     release_firmware(fw);
0162 
0163     return commands;
0164 }
0165 
0166 static void panel_mipi_dbi_commands_execute(struct mipi_dbi *dbi,
0167                         struct panel_mipi_dbi_commands *commands)
0168 {
0169     unsigned int i = 0;
0170 
0171     if (!commands)
0172         return;
0173 
0174     while (i < commands->len) {
0175         u8 command = commands->buf[i++];
0176         u8 num_parameters = commands->buf[i++];
0177         const u8 *parameters = &commands->buf[i];
0178 
0179         if (command == 0x00 && num_parameters == 1)
0180             msleep(parameters[0]);
0181         else if (num_parameters)
0182             mipi_dbi_command_stackbuf(dbi, command, parameters, num_parameters);
0183         else
0184             mipi_dbi_command(dbi, command);
0185 
0186         i += num_parameters;
0187     }
0188 }
0189 
0190 static void panel_mipi_dbi_enable(struct drm_simple_display_pipe *pipe,
0191                   struct drm_crtc_state *crtc_state,
0192                   struct drm_plane_state *plane_state)
0193 {
0194     struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
0195     struct mipi_dbi *dbi = &dbidev->dbi;
0196     int ret, idx;
0197 
0198     if (!drm_dev_enter(pipe->crtc.dev, &idx))
0199         return;
0200 
0201     drm_dbg(pipe->crtc.dev, "\n");
0202 
0203     ret = mipi_dbi_poweron_conditional_reset(dbidev);
0204     if (ret < 0)
0205         goto out_exit;
0206     if (!ret)
0207         panel_mipi_dbi_commands_execute(dbi, dbidev->driver_private);
0208 
0209     mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
0210 out_exit:
0211     drm_dev_exit(idx);
0212 }
0213 
0214 static const struct drm_simple_display_pipe_funcs panel_mipi_dbi_pipe_funcs = {
0215     .enable = panel_mipi_dbi_enable,
0216     .disable = mipi_dbi_pipe_disable,
0217     .update = mipi_dbi_pipe_update,
0218 };
0219 
0220 DEFINE_DRM_GEM_CMA_FOPS(panel_mipi_dbi_fops);
0221 
0222 static const struct drm_driver panel_mipi_dbi_driver = {
0223     .driver_features    = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
0224     .fops           = &panel_mipi_dbi_fops,
0225     DRM_GEM_CMA_DRIVER_OPS_VMAP,
0226     .debugfs_init       = mipi_dbi_debugfs_init,
0227     .name           = "panel-mipi-dbi",
0228     .desc           = "MIPI DBI compatible display panel",
0229     .date           = "20220103",
0230     .major          = 1,
0231     .minor          = 0,
0232 };
0233 
0234 static int panel_mipi_dbi_get_mode(struct mipi_dbi_dev *dbidev, struct drm_display_mode *mode)
0235 {
0236     struct device *dev = dbidev->drm.dev;
0237     u16 hback_porch, vback_porch;
0238     int ret;
0239 
0240     ret = of_get_drm_panel_display_mode(dev->of_node, mode, NULL);
0241     if (ret) {
0242         dev_err(dev, "%pOF: failed to get panel-timing (error=%d)\n", dev->of_node, ret);
0243         return ret;
0244     }
0245 
0246     mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
0247 
0248     hback_porch = mode->htotal - mode->hsync_end;
0249     vback_porch = mode->vtotal - mode->vsync_end;
0250 
0251     /*
0252      * Make sure width and height are set and that only back porch and
0253      * pixelclock are set in the other timing values. Also check that
0254      * width and height don't exceed the 16-bit value specified by MIPI DCS.
0255      */
0256     if (!mode->hdisplay || !mode->vdisplay || mode->flags ||
0257         mode->hsync_end > mode->hdisplay || (hback_porch + mode->hdisplay) > 0xffff ||
0258         mode->vsync_end > mode->vdisplay || (vback_porch + mode->vdisplay) > 0xffff) {
0259         dev_err(dev, "%pOF: panel-timing out of bounds\n", dev->of_node);
0260         return -EINVAL;
0261     }
0262 
0263     /* The driver doesn't use the pixel clock but it is mandatory so fake one if not set */
0264     if (!mode->clock)
0265         mode->clock = mode->htotal * mode->vtotal * 60 / 1000;
0266 
0267     dbidev->top_offset = vback_porch;
0268     dbidev->left_offset = hback_porch;
0269 
0270     return 0;
0271 }
0272 
0273 static int panel_mipi_dbi_spi_probe(struct spi_device *spi)
0274 {
0275     struct device *dev = &spi->dev;
0276     struct drm_display_mode mode;
0277     struct mipi_dbi_dev *dbidev;
0278     struct drm_device *drm;
0279     struct mipi_dbi *dbi;
0280     struct gpio_desc *dc;
0281     int ret;
0282 
0283     dbidev = devm_drm_dev_alloc(dev, &panel_mipi_dbi_driver, struct mipi_dbi_dev, drm);
0284     if (IS_ERR(dbidev))
0285         return PTR_ERR(dbidev);
0286 
0287     dbi = &dbidev->dbi;
0288     drm = &dbidev->drm;
0289 
0290     ret = panel_mipi_dbi_get_mode(dbidev, &mode);
0291     if (ret)
0292         return ret;
0293 
0294     dbidev->regulator = devm_regulator_get(dev, "power");
0295     if (IS_ERR(dbidev->regulator))
0296         return dev_err_probe(dev, PTR_ERR(dbidev->regulator),
0297                      "Failed to get regulator 'power'\n");
0298 
0299     dbidev->backlight = devm_of_find_backlight(dev);
0300     if (IS_ERR(dbidev->backlight))
0301         return dev_err_probe(dev, PTR_ERR(dbidev->backlight), "Failed to get backlight\n");
0302 
0303     dbi->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
0304     if (IS_ERR(dbi->reset))
0305         return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n");
0306 
0307     dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW);
0308     if (IS_ERR(dc))
0309         return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n");
0310 
0311     ret = mipi_dbi_spi_init(spi, dbi, dc);
0312     if (ret)
0313         return ret;
0314 
0315     if (device_property_present(dev, "write-only"))
0316         dbi->read_commands = NULL;
0317 
0318     dbidev->driver_private = panel_mipi_dbi_commands_from_fw(dev);
0319     if (IS_ERR(dbidev->driver_private))
0320         return PTR_ERR(dbidev->driver_private);
0321 
0322     ret = mipi_dbi_dev_init(dbidev, &panel_mipi_dbi_pipe_funcs, &mode, 0);
0323     if (ret)
0324         return ret;
0325 
0326     drm_mode_config_reset(drm);
0327 
0328     ret = drm_dev_register(drm, 0);
0329     if (ret)
0330         return ret;
0331 
0332     spi_set_drvdata(spi, drm);
0333 
0334     drm_fbdev_generic_setup(drm, 0);
0335 
0336     return 0;
0337 }
0338 
0339 static void panel_mipi_dbi_spi_remove(struct spi_device *spi)
0340 {
0341     struct drm_device *drm = spi_get_drvdata(spi);
0342 
0343     drm_dev_unplug(drm);
0344     drm_atomic_helper_shutdown(drm);
0345 }
0346 
0347 static void panel_mipi_dbi_spi_shutdown(struct spi_device *spi)
0348 {
0349     drm_atomic_helper_shutdown(spi_get_drvdata(spi));
0350 }
0351 
0352 static int __maybe_unused panel_mipi_dbi_pm_suspend(struct device *dev)
0353 {
0354     return drm_mode_config_helper_suspend(dev_get_drvdata(dev));
0355 }
0356 
0357 static int __maybe_unused panel_mipi_dbi_pm_resume(struct device *dev)
0358 {
0359     drm_mode_config_helper_resume(dev_get_drvdata(dev));
0360 
0361     return 0;
0362 }
0363 
0364 static const struct dev_pm_ops panel_mipi_dbi_pm_ops = {
0365     SET_SYSTEM_SLEEP_PM_OPS(panel_mipi_dbi_pm_suspend, panel_mipi_dbi_pm_resume)
0366 };
0367 
0368 static const struct of_device_id panel_mipi_dbi_spi_of_match[] = {
0369     { .compatible = "panel-mipi-dbi-spi" },
0370     {},
0371 };
0372 MODULE_DEVICE_TABLE(of, panel_mipi_dbi_spi_of_match);
0373 
0374 static const struct spi_device_id panel_mipi_dbi_spi_id[] = {
0375     { "panel-mipi-dbi-spi", 0 },
0376     { },
0377 };
0378 MODULE_DEVICE_TABLE(spi, panel_mipi_dbi_spi_id);
0379 
0380 static struct spi_driver panel_mipi_dbi_spi_driver = {
0381     .driver = {
0382         .name = "panel-mipi-dbi-spi",
0383         .owner = THIS_MODULE,
0384         .of_match_table = panel_mipi_dbi_spi_of_match,
0385         .pm = &panel_mipi_dbi_pm_ops,
0386     },
0387     .id_table = panel_mipi_dbi_spi_id,
0388     .probe = panel_mipi_dbi_spi_probe,
0389     .remove = panel_mipi_dbi_spi_remove,
0390     .shutdown = panel_mipi_dbi_spi_shutdown,
0391 };
0392 module_spi_driver(panel_mipi_dbi_spi_driver);
0393 
0394 MODULE_DESCRIPTION("MIPI DBI compatible display panel driver");
0395 MODULE_AUTHOR("Noralf Trønnes");
0396 MODULE_LICENSE("GPL");