2 * DRM driver for Multi-Inno MI0283QT panels
4 * Copyright 2016 Noralf Trønnes
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/backlight.h>
13 #include <linux/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/module.h>
16 #include <linux/property.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/spi/spi.h>
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_drv.h>
22 #include <drm/drm_fb_helper.h>
23 #include <drm/drm_gem_cma_helper.h>
24 #include <drm/drm_gem_framebuffer_helper.h>
25 #include <drm/drm_modeset_helper.h>
26 #include <drm/tinydrm/mipi-dbi.h>
27 #include <drm/tinydrm/tinydrm-helpers.h>
28 #include <video/mipi_display.h>
30 #define ILI9341_FRMCTR1 0xb1
31 #define ILI9341_DISCTRL 0xb6
32 #define ILI9341_ETMOD 0xb7
34 #define ILI9341_PWCTRL1 0xc0
35 #define ILI9341_PWCTRL2 0xc1
36 #define ILI9341_VMCTRL1 0xc5
37 #define ILI9341_VMCTRL2 0xc7
38 #define ILI9341_PWCTRLA 0xcb
39 #define ILI9341_PWCTRLB 0xcf
41 #define ILI9341_PGAMCTRL 0xe0
42 #define ILI9341_NGAMCTRL 0xe1
43 #define ILI9341_DTCTRLA 0xe8
44 #define ILI9341_DTCTRLB 0xea
45 #define ILI9341_PWRSEQ 0xed
47 #define ILI9341_EN3GAM 0xf2
48 #define ILI9341_PUMPCTRL 0xf7
50 #define ILI9341_MADCTL_BGR BIT(3)
51 #define ILI9341_MADCTL_MV BIT(5)
52 #define ILI9341_MADCTL_MX BIT(6)
53 #define ILI9341_MADCTL_MY BIT(7)
55 static void mi0283qt_enable(struct drm_simple_display_pipe *pipe,
56 struct drm_crtc_state *crtc_state,
57 struct drm_plane_state *plane_state)
59 struct mipi_dbi *mipi = drm_to_mipi_dbi(pipe->crtc.dev);
63 if (!drm_dev_enter(pipe->crtc.dev, &idx))
68 ret = mipi_dbi_poweron_conditional_reset(mipi);
74 mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_OFF);
76 mipi_dbi_command(mipi, ILI9341_PWCTRLB, 0x00, 0x83, 0x30);
77 mipi_dbi_command(mipi, ILI9341_PWRSEQ, 0x64, 0x03, 0x12, 0x81);
78 mipi_dbi_command(mipi, ILI9341_DTCTRLA, 0x85, 0x01, 0x79);
79 mipi_dbi_command(mipi, ILI9341_PWCTRLA, 0x39, 0x2c, 0x00, 0x34, 0x02);
80 mipi_dbi_command(mipi, ILI9341_PUMPCTRL, 0x20);
81 mipi_dbi_command(mipi, ILI9341_DTCTRLB, 0x00, 0x00);
84 mipi_dbi_command(mipi, ILI9341_PWCTRL1, 0x26);
85 mipi_dbi_command(mipi, ILI9341_PWCTRL2, 0x11);
87 mipi_dbi_command(mipi, ILI9341_VMCTRL1, 0x35, 0x3e);
88 mipi_dbi_command(mipi, ILI9341_VMCTRL2, 0xbe);
90 /* Memory Access Control */
91 mipi_dbi_command(mipi, MIPI_DCS_SET_PIXEL_FORMAT, MIPI_DCS_PIXEL_FMT_16BIT);
94 mipi_dbi_command(mipi, ILI9341_FRMCTR1, 0x00, 0x1b);
97 mipi_dbi_command(mipi, ILI9341_EN3GAM, 0x08);
98 mipi_dbi_command(mipi, MIPI_DCS_SET_GAMMA_CURVE, 0x01);
99 mipi_dbi_command(mipi, ILI9341_PGAMCTRL,
100 0x1f, 0x1a, 0x18, 0x0a, 0x0f, 0x06, 0x45, 0x87,
101 0x32, 0x0a, 0x07, 0x02, 0x07, 0x05, 0x00);
102 mipi_dbi_command(mipi, ILI9341_NGAMCTRL,
103 0x00, 0x25, 0x27, 0x05, 0x10, 0x09, 0x3a, 0x78,
104 0x4d, 0x05, 0x18, 0x0d, 0x38, 0x3a, 0x1f);
107 mipi_dbi_command(mipi, ILI9341_ETMOD, 0x07);
110 mipi_dbi_command(mipi, ILI9341_DISCTRL, 0x0a, 0x82, 0x27, 0x00);
111 mipi_dbi_command(mipi, MIPI_DCS_EXIT_SLEEP_MODE);
114 mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_ON);
118 /* The PiTFT (ili9340) has a hardware reset circuit that
119 * resets only on power-on and not on each reboot through
120 * a gpio like the rpi-display does.
121 * As a result, we need to always apply the rotation value
122 * regardless of the display "on/off" state.
124 switch (mipi->rotation) {
126 addr_mode = ILI9341_MADCTL_MV | ILI9341_MADCTL_MY |
130 addr_mode = ILI9341_MADCTL_MY;
133 addr_mode = ILI9341_MADCTL_MV;
136 addr_mode = ILI9341_MADCTL_MX;
139 addr_mode |= ILI9341_MADCTL_BGR;
140 mipi_dbi_command(mipi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
141 mipi_dbi_enable_flush(mipi, crtc_state, plane_state);
146 static const struct drm_simple_display_pipe_funcs mi0283qt_pipe_funcs = {
147 .enable = mi0283qt_enable,
148 .disable = mipi_dbi_pipe_disable,
149 .update = mipi_dbi_pipe_update,
150 .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
153 static const struct drm_display_mode mi0283qt_mode = {
154 DRM_SIMPLE_MODE(320, 240, 58, 43),
157 DEFINE_DRM_GEM_CMA_FOPS(mi0283qt_fops);
159 static struct drm_driver mi0283qt_driver = {
160 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
162 .fops = &mi0283qt_fops,
163 .release = mipi_dbi_release,
164 DRM_GEM_CMA_VMAP_DRIVER_OPS,
165 .debugfs_init = mipi_dbi_debugfs_init,
167 .desc = "Multi-Inno MI0283QT",
173 static const struct of_device_id mi0283qt_of_match[] = {
174 { .compatible = "multi-inno,mi0283qt" },
177 MODULE_DEVICE_TABLE(of, mi0283qt_of_match);
179 static const struct spi_device_id mi0283qt_id[] = {
183 MODULE_DEVICE_TABLE(spi, mi0283qt_id);
185 static int mi0283qt_probe(struct spi_device *spi)
187 struct device *dev = &spi->dev;
188 struct drm_device *drm;
189 struct mipi_dbi *mipi;
190 struct gpio_desc *dc;
194 mipi = kzalloc(sizeof(*mipi), GFP_KERNEL);
199 ret = devm_drm_dev_init(dev, drm, &mi0283qt_driver);
205 drm_mode_config_init(drm);
207 mipi->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
208 if (IS_ERR(mipi->reset)) {
209 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
210 return PTR_ERR(mipi->reset);
213 dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW);
215 DRM_DEV_ERROR(dev, "Failed to get gpio 'dc'\n");
219 mipi->regulator = devm_regulator_get(dev, "power");
220 if (IS_ERR(mipi->regulator))
221 return PTR_ERR(mipi->regulator);
223 mipi->backlight = devm_of_find_backlight(dev);
224 if (IS_ERR(mipi->backlight))
225 return PTR_ERR(mipi->backlight);
227 device_property_read_u32(dev, "rotation", &rotation);
229 ret = mipi_dbi_spi_init(spi, mipi, dc);
233 ret = mipi_dbi_init(mipi, &mi0283qt_pipe_funcs, &mi0283qt_mode, rotation);
237 drm_mode_config_reset(drm);
239 ret = drm_dev_register(drm, 0);
243 spi_set_drvdata(spi, drm);
245 drm_fbdev_generic_setup(drm, 32);
250 static int mi0283qt_remove(struct spi_device *spi)
252 struct drm_device *drm = spi_get_drvdata(spi);
255 drm_atomic_helper_shutdown(drm);
260 static void mi0283qt_shutdown(struct spi_device *spi)
262 drm_atomic_helper_shutdown(spi_get_drvdata(spi));
265 static int __maybe_unused mi0283qt_pm_suspend(struct device *dev)
267 return drm_mode_config_helper_suspend(dev_get_drvdata(dev));
270 static int __maybe_unused mi0283qt_pm_resume(struct device *dev)
272 drm_mode_config_helper_resume(dev_get_drvdata(dev));
277 static const struct dev_pm_ops mi0283qt_pm_ops = {
278 SET_SYSTEM_SLEEP_PM_OPS(mi0283qt_pm_suspend, mi0283qt_pm_resume)
281 static struct spi_driver mi0283qt_spi_driver = {
284 .owner = THIS_MODULE,
285 .of_match_table = mi0283qt_of_match,
286 .pm = &mi0283qt_pm_ops,
288 .id_table = mi0283qt_id,
289 .probe = mi0283qt_probe,
290 .remove = mi0283qt_remove,
291 .shutdown = mi0283qt_shutdown,
293 module_spi_driver(mi0283qt_spi_driver);
295 MODULE_DESCRIPTION("Multi-Inno MI0283QT DRM driver");
296 MODULE_AUTHOR("Noralf Trønnes");
297 MODULE_LICENSE("GPL");