1 // SPDX-License-Identifier: GPL-2.0+
3 * DRM driver for Ilitek ILI9486 panels
8 #include <linux/backlight.h>
9 #include <linux/delay.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/module.h>
12 #include <linux/property.h>
13 #include <linux/spi/spi.h>
15 #include <video/mipi_display.h>
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_drv.h>
19 #include <drm/drm_fbdev_dma.h>
20 #include <drm/drm_gem_atomic_helper.h>
21 #include <drm/drm_gem_dma_helper.h>
22 #include <drm/drm_managed.h>
23 #include <drm/drm_mipi_dbi.h>
24 #include <drm/drm_modeset_helper.h>
26 #define ILI9486_ITFCTR1 0xb0
27 #define ILI9486_PWCTRL1 0xc2
28 #define ILI9486_VMCTRL1 0xc5
29 #define ILI9486_PGAMCTRL 0xe0
30 #define ILI9486_NGAMCTRL 0xe1
31 #define ILI9486_DGAMCTRL 0xe2
32 #define ILI9486_MADCTL_BGR BIT(3)
33 #define ILI9486_MADCTL_MV BIT(5)
34 #define ILI9486_MADCTL_MX BIT(6)
35 #define ILI9486_MADCTL_MY BIT(7)
38 * The PiScreen/waveshare rpi-lcd-35 has a SPI to 16-bit parallel bus converter
39 * in front of the display controller. This means that 8-bit values have to be
40 * transferred as 16-bit.
42 static int waveshare_command(struct mipi_dbi *mipi, u8 *cmd, u8 *par,
45 struct spi_device *spi = mipi->spi;
52 buf = kmalloc(32 * sizeof(u16), GFP_KERNEL);
57 * The displays are Raspberry Pi HATs and connected to the 8-bit only
58 * SPI controller, so 16-bit command and parameters need byte swapping
59 * before being transferred as 8-bit on the big endian SPI bus.
61 buf[0] = cpu_to_be16(*cmd);
62 spi_bus_lock(spi->controller);
63 gpiod_set_value_cansleep(mipi->dc, 0);
64 speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 2);
65 ret = mipi_dbi_spi_transfer(spi, speed_hz, 8, buf, 2);
66 spi_bus_unlock(spi->controller);
70 /* 8-bit configuration data, not 16-bit pixel data */
72 for (i = 0; i < num; i++)
73 buf[i] = cpu_to_be16(par[i]);
79 * Check whether pixel data bytes needs to be swapped or not
81 if (*cmd == MIPI_DCS_WRITE_MEMORY_START && !mipi->swap_bytes)
84 spi_bus_lock(spi->controller);
85 gpiod_set_value_cansleep(mipi->dc, 1);
86 speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
87 ret = mipi_dbi_spi_transfer(spi, speed_hz, bpw, data, num);
88 spi_bus_unlock(spi->controller);
95 static void waveshare_enable(struct drm_simple_display_pipe *pipe,
96 struct drm_crtc_state *crtc_state,
97 struct drm_plane_state *plane_state)
99 struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
100 struct mipi_dbi *dbi = &dbidev->dbi;
104 if (!drm_dev_enter(pipe->crtc.dev, &idx))
109 ret = mipi_dbi_poweron_conditional_reset(dbidev);
115 mipi_dbi_command(dbi, ILI9486_ITFCTR1);
116 mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
119 mipi_dbi_command(dbi, MIPI_DCS_SET_PIXEL_FORMAT, 0x55);
121 mipi_dbi_command(dbi, ILI9486_PWCTRL1, 0x44);
123 mipi_dbi_command(dbi, ILI9486_VMCTRL1, 0x00, 0x00, 0x00, 0x00);
125 mipi_dbi_command(dbi, ILI9486_PGAMCTRL,
126 0x0F, 0x1F, 0x1C, 0x0C, 0x0F, 0x08, 0x48, 0x98,
127 0x37, 0x0A, 0x13, 0x04, 0x11, 0x0D, 0x0);
128 mipi_dbi_command(dbi, ILI9486_NGAMCTRL,
129 0x0F, 0x32, 0x2E, 0x0B, 0x0D, 0x05, 0x47, 0x75,
130 0x37, 0x06, 0x10, 0x03, 0x24, 0x20, 0x00);
131 mipi_dbi_command(dbi, ILI9486_DGAMCTRL,
132 0x0F, 0x32, 0x2E, 0x0B, 0x0D, 0x05, 0x47, 0x75,
133 0x37, 0x06, 0x10, 0x03, 0x24, 0x20, 0x00);
135 mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
139 switch (dbidev->rotation) {
141 addr_mode = ILI9486_MADCTL_MY;
144 addr_mode = ILI9486_MADCTL_MV;
147 addr_mode = ILI9486_MADCTL_MX;
150 addr_mode = ILI9486_MADCTL_MV | ILI9486_MADCTL_MY |
154 addr_mode |= ILI9486_MADCTL_BGR;
155 mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
156 mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
161 static const struct drm_simple_display_pipe_funcs waveshare_pipe_funcs = {
162 DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS(waveshare_enable),
165 static const struct drm_display_mode waveshare_mode = {
166 DRM_SIMPLE_MODE(480, 320, 73, 49),
169 DEFINE_DRM_GEM_DMA_FOPS(ili9486_fops);
171 static const struct drm_driver ili9486_driver = {
172 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
173 .fops = &ili9486_fops,
174 DRM_GEM_DMA_DRIVER_OPS_VMAP,
175 .debugfs_init = mipi_dbi_debugfs_init,
177 .desc = "Ilitek ILI9486",
183 static const struct of_device_id ili9486_of_match[] = {
184 { .compatible = "waveshare,rpi-lcd-35" },
185 { .compatible = "ozzmaker,piscreen" },
188 MODULE_DEVICE_TABLE(of, ili9486_of_match);
190 static const struct spi_device_id ili9486_id[] = {
196 MODULE_DEVICE_TABLE(spi, ili9486_id);
198 static int ili9486_probe(struct spi_device *spi)
200 struct device *dev = &spi->dev;
201 struct mipi_dbi_dev *dbidev;
202 struct drm_device *drm;
203 struct mipi_dbi *dbi;
204 struct gpio_desc *dc;
208 dbidev = devm_drm_dev_alloc(dev, &ili9486_driver,
209 struct mipi_dbi_dev, drm);
211 return PTR_ERR(dbidev);
216 dbi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
217 if (IS_ERR(dbi->reset))
218 return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n");
220 dc = devm_gpiod_get(dev, "dc", GPIOD_OUT_LOW);
222 return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n");
224 dbidev->backlight = devm_of_find_backlight(dev);
225 if (IS_ERR(dbidev->backlight))
226 return PTR_ERR(dbidev->backlight);
228 device_property_read_u32(dev, "rotation", &rotation);
230 ret = mipi_dbi_spi_init(spi, dbi, dc);
234 dbi->command = waveshare_command;
235 dbi->read_commands = NULL;
237 ret = mipi_dbi_dev_init(dbidev, &waveshare_pipe_funcs,
238 &waveshare_mode, rotation);
242 drm_mode_config_reset(drm);
244 ret = drm_dev_register(drm, 0);
248 spi_set_drvdata(spi, drm);
250 drm_fbdev_dma_setup(drm, 0);
255 static void ili9486_remove(struct spi_device *spi)
257 struct drm_device *drm = spi_get_drvdata(spi);
260 drm_atomic_helper_shutdown(drm);
263 static void ili9486_shutdown(struct spi_device *spi)
265 drm_atomic_helper_shutdown(spi_get_drvdata(spi));
268 static struct spi_driver ili9486_spi_driver = {
271 .of_match_table = ili9486_of_match,
273 .id_table = ili9486_id,
274 .probe = ili9486_probe,
275 .remove = ili9486_remove,
276 .shutdown = ili9486_shutdown,
278 module_spi_driver(ili9486_spi_driver);
280 MODULE_DESCRIPTION("Ilitek ILI9486 DRM driver");
282 MODULE_LICENSE("GPL");