1 // SPDX-License-Identifier: GPL-2.0+
3 * DRM driver for display panels connected to a Sitronix ST7715R or ST7735R
4 * display controller in SPI mode.
6 * Copyright 2017 David Lechner <david@lechnology.com>
7 * Copyright (C) 2019 Glider bvba
10 #include <linux/backlight.h>
11 #include <linux/delay.h>
12 #include <linux/dma-buf.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/module.h>
15 #include <linux/property.h>
16 #include <linux/spi/spi.h>
17 #include <video/mipi_display.h>
19 #include <drm/drm_atomic_helper.h>
20 #include <drm/drm_drv.h>
21 #include <drm/drm_fb_helper.h>
22 #include <drm/drm_gem_atomic_helper.h>
23 #include <drm/drm_gem_cma_helper.h>
24 #include <drm/drm_managed.h>
25 #include <drm/drm_mipi_dbi.h>
27 #define ST7735R_FRMCTR1 0xb1
28 #define ST7735R_FRMCTR2 0xb2
29 #define ST7735R_FRMCTR3 0xb3
30 #define ST7735R_INVCTR 0xb4
31 #define ST7735R_PWCTR1 0xc0
32 #define ST7735R_PWCTR2 0xc1
33 #define ST7735R_PWCTR3 0xc2
34 #define ST7735R_PWCTR4 0xc3
35 #define ST7735R_PWCTR5 0xc4
36 #define ST7735R_VMCTR1 0xc5
37 #define ST7735R_GAMCTRP1 0xe0
38 #define ST7735R_GAMCTRN1 0xe1
40 #define ST7735R_MY BIT(7)
41 #define ST7735R_MX BIT(6)
42 #define ST7735R_MV BIT(5)
43 #define ST7735R_RGB BIT(3)
46 const struct drm_display_mode mode;
47 unsigned int left_offset;
48 unsigned int top_offset;
49 unsigned int write_only:1;
50 unsigned int rgb:1; /* RGB (vs. BGR) */
54 struct mipi_dbi_dev dbidev; /* Must be first for .release() */
55 const struct st7735r_cfg *cfg;
58 static void st7735r_pipe_enable(struct drm_simple_display_pipe *pipe,
59 struct drm_crtc_state *crtc_state,
60 struct drm_plane_state *plane_state)
62 struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
63 struct st7735r_priv *priv = container_of(dbidev, struct st7735r_priv,
65 struct mipi_dbi *dbi = &dbidev->dbi;
69 if (!drm_dev_enter(pipe->crtc.dev, &idx))
74 ret = mipi_dbi_poweron_reset(dbidev);
80 mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
83 mipi_dbi_command(dbi, ST7735R_FRMCTR1, 0x01, 0x2c, 0x2d);
84 mipi_dbi_command(dbi, ST7735R_FRMCTR2, 0x01, 0x2c, 0x2d);
85 mipi_dbi_command(dbi, ST7735R_FRMCTR3, 0x01, 0x2c, 0x2d, 0x01, 0x2c,
87 mipi_dbi_command(dbi, ST7735R_INVCTR, 0x07);
88 mipi_dbi_command(dbi, ST7735R_PWCTR1, 0xa2, 0x02, 0x84);
89 mipi_dbi_command(dbi, ST7735R_PWCTR2, 0xc5);
90 mipi_dbi_command(dbi, ST7735R_PWCTR3, 0x0a, 0x00);
91 mipi_dbi_command(dbi, ST7735R_PWCTR4, 0x8a, 0x2a);
92 mipi_dbi_command(dbi, ST7735R_PWCTR5, 0x8a, 0xee);
93 mipi_dbi_command(dbi, ST7735R_VMCTR1, 0x0e);
94 mipi_dbi_command(dbi, MIPI_DCS_EXIT_INVERT_MODE);
95 switch (dbidev->rotation) {
97 addr_mode = ST7735R_MX | ST7735R_MY;
100 addr_mode = ST7735R_MX | ST7735R_MV;
106 addr_mode = ST7735R_MY | ST7735R_MV;
111 addr_mode |= ST7735R_RGB;
113 mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
114 mipi_dbi_command(dbi, MIPI_DCS_SET_PIXEL_FORMAT,
115 MIPI_DCS_PIXEL_FMT_16BIT);
116 mipi_dbi_command(dbi, ST7735R_GAMCTRP1, 0x02, 0x1c, 0x07, 0x12, 0x37,
117 0x32, 0x29, 0x2d, 0x29, 0x25, 0x2b, 0x39, 0x00, 0x01,
119 mipi_dbi_command(dbi, ST7735R_GAMCTRN1, 0x03, 0x1d, 0x07, 0x06, 0x2e,
120 0x2c, 0x29, 0x2d, 0x2e, 0x2e, 0x37, 0x3f, 0x00, 0x00,
122 mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
126 mipi_dbi_command(dbi, MIPI_DCS_ENTER_NORMAL_MODE);
130 mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
135 static const struct drm_simple_display_pipe_funcs st7735r_pipe_funcs = {
136 .enable = st7735r_pipe_enable,
137 .disable = mipi_dbi_pipe_disable,
138 .update = mipi_dbi_pipe_update,
139 .prepare_fb = drm_gem_simple_display_pipe_prepare_fb,
142 static const struct st7735r_cfg jd_t18003_t01_cfg = {
143 .mode = { DRM_SIMPLE_MODE(128, 160, 28, 35) },
144 /* Cannot read from Adafruit 1.8" display via SPI */
148 static const struct st7735r_cfg rh128128t_cfg = {
149 .mode = { DRM_SIMPLE_MODE(128, 128, 25, 26) },
155 DEFINE_DRM_GEM_CMA_FOPS(st7735r_fops);
157 static const struct drm_driver st7735r_driver = {
158 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
159 .fops = &st7735r_fops,
160 DRM_GEM_CMA_DRIVER_OPS_VMAP,
161 .debugfs_init = mipi_dbi_debugfs_init,
163 .desc = "Sitronix ST7735R",
169 static const struct of_device_id st7735r_of_match[] = {
170 { .compatible = "jianda,jd-t18003-t01", .data = &jd_t18003_t01_cfg },
171 { .compatible = "okaya,rh128128t", .data = &rh128128t_cfg },
174 MODULE_DEVICE_TABLE(of, st7735r_of_match);
176 static const struct spi_device_id st7735r_id[] = {
177 { "jd-t18003-t01", (uintptr_t)&jd_t18003_t01_cfg },
180 MODULE_DEVICE_TABLE(spi, st7735r_id);
182 static int st7735r_probe(struct spi_device *spi)
184 struct device *dev = &spi->dev;
185 const struct st7735r_cfg *cfg;
186 struct mipi_dbi_dev *dbidev;
187 struct st7735r_priv *priv;
188 struct drm_device *drm;
189 struct mipi_dbi *dbi;
190 struct gpio_desc *dc;
194 cfg = device_get_match_data(&spi->dev);
196 cfg = (void *)spi_get_device_id(spi)->driver_data;
198 priv = devm_drm_dev_alloc(dev, &st7735r_driver,
199 struct st7735r_priv, dbidev.drm);
201 return PTR_ERR(priv);
203 dbidev = &priv->dbidev;
209 dbi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
210 if (IS_ERR(dbi->reset)) {
211 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
212 return PTR_ERR(dbi->reset);
215 dc = devm_gpiod_get(dev, "dc", GPIOD_OUT_LOW);
217 DRM_DEV_ERROR(dev, "Failed to get gpio 'dc'\n");
221 dbidev->backlight = devm_of_find_backlight(dev);
222 if (IS_ERR(dbidev->backlight))
223 return PTR_ERR(dbidev->backlight);
225 device_property_read_u32(dev, "rotation", &rotation);
227 ret = mipi_dbi_spi_init(spi, dbi, dc);
232 dbi->read_commands = NULL;
234 dbidev->left_offset = cfg->left_offset;
235 dbidev->top_offset = cfg->top_offset;
237 ret = mipi_dbi_dev_init(dbidev, &st7735r_pipe_funcs, &cfg->mode,
242 drm_mode_config_reset(drm);
244 ret = drm_dev_register(drm, 0);
248 spi_set_drvdata(spi, drm);
250 drm_fbdev_generic_setup(drm, 0);
255 static int st7735r_remove(struct spi_device *spi)
257 struct drm_device *drm = spi_get_drvdata(spi);
260 drm_atomic_helper_shutdown(drm);
265 static void st7735r_shutdown(struct spi_device *spi)
267 drm_atomic_helper_shutdown(spi_get_drvdata(spi));
270 static struct spi_driver st7735r_spi_driver = {
273 .of_match_table = st7735r_of_match,
275 .id_table = st7735r_id,
276 .probe = st7735r_probe,
277 .remove = st7735r_remove,
278 .shutdown = st7735r_shutdown,
280 module_spi_driver(st7735r_spi_driver);
282 MODULE_DESCRIPTION("Sitronix ST7735R DRM driver");
283 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
284 MODULE_LICENSE("GPL");