1 // SPDX-License-Identifier: GPL-2.0+
3 * DRM driver for Sitronix ST7735R panels
5 * Copyright 2017 David Lechner <david@lechnology.com>
8 #include <linux/delay.h>
9 #include <linux/dma-buf.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/module.h>
12 #include <linux/property.h>
13 #include <linux/spi/spi.h>
14 #include <video/mipi_display.h>
16 #include <drm/drm_fb_helper.h>
17 #include <drm/drm_gem_framebuffer_helper.h>
18 #include <drm/tinydrm/mipi-dbi.h>
19 #include <drm/tinydrm/tinydrm-helpers.h>
21 #define ST7735R_FRMCTR1 0xb1
22 #define ST7735R_FRMCTR2 0xb2
23 #define ST7735R_FRMCTR3 0xb3
24 #define ST7735R_INVCTR 0xb4
25 #define ST7735R_PWCTR1 0xc0
26 #define ST7735R_PWCTR2 0xc1
27 #define ST7735R_PWCTR3 0xc2
28 #define ST7735R_PWCTR4 0xc3
29 #define ST7735R_PWCTR5 0xc4
30 #define ST7735R_VMCTR1 0xc5
31 #define ST7735R_GAMCTRP1 0xe0
32 #define ST7735R_GAMCTRN1 0xe1
34 #define ST7735R_MY BIT(7)
35 #define ST7735R_MX BIT(6)
36 #define ST7735R_MV BIT(5)
38 static void jd_t18003_t01_pipe_enable(struct drm_simple_display_pipe
*pipe
,
39 struct drm_crtc_state
*crtc_state
)
41 struct tinydrm_device
*tdev
= pipe_to_tinydrm(pipe
);
42 struct mipi_dbi
*mipi
= mipi_dbi_from_tinydrm(tdev
);
43 struct device
*dev
= tdev
->drm
->dev
;
49 mipi_dbi_hw_reset(mipi
);
51 ret
= mipi_dbi_command(mipi
, MIPI_DCS_SOFT_RESET
);
53 DRM_DEV_ERROR(dev
, "Error sending command %d\n", ret
);
59 mipi_dbi_command(mipi
, MIPI_DCS_EXIT_SLEEP_MODE
);
62 mipi_dbi_command(mipi
, ST7735R_FRMCTR1
, 0x01, 0x2c, 0x2d);
63 mipi_dbi_command(mipi
, ST7735R_FRMCTR2
, 0x01, 0x2c, 0x2d);
64 mipi_dbi_command(mipi
, ST7735R_FRMCTR3
, 0x01, 0x2c, 0x2d, 0x01, 0x2c,
66 mipi_dbi_command(mipi
, ST7735R_INVCTR
, 0x07);
67 mipi_dbi_command(mipi
, ST7735R_PWCTR1
, 0xa2, 0x02, 0x84);
68 mipi_dbi_command(mipi
, ST7735R_PWCTR2
, 0xc5);
69 mipi_dbi_command(mipi
, ST7735R_PWCTR3
, 0x0a, 0x00);
70 mipi_dbi_command(mipi
, ST7735R_PWCTR4
, 0x8a, 0x2a);
71 mipi_dbi_command(mipi
, ST7735R_PWCTR5
, 0x8a, 0xee);
72 mipi_dbi_command(mipi
, ST7735R_VMCTR1
, 0x0e);
73 mipi_dbi_command(mipi
, MIPI_DCS_EXIT_INVERT_MODE
);
74 switch (mipi
->rotation
) {
76 addr_mode
= ST7735R_MX
| ST7735R_MY
;
79 addr_mode
= ST7735R_MX
| ST7735R_MV
;
85 addr_mode
= ST7735R_MY
| ST7735R_MV
;
88 mipi_dbi_command(mipi
, MIPI_DCS_SET_ADDRESS_MODE
, addr_mode
);
89 mipi_dbi_command(mipi
, MIPI_DCS_SET_PIXEL_FORMAT
,
90 MIPI_DCS_PIXEL_FMT_16BIT
);
91 mipi_dbi_command(mipi
, ST7735R_GAMCTRP1
, 0x02, 0x1c, 0x07, 0x12, 0x37,
92 0x32, 0x29, 0x2d, 0x29, 0x25, 0x2b, 0x39, 0x00, 0x01,
94 mipi_dbi_command(mipi
, ST7735R_GAMCTRN1
, 0x03, 0x1d, 0x07, 0x06, 0x2e,
95 0x2c, 0x29, 0x2d, 0x2e, 0x2e, 0x37, 0x3f, 0x00, 0x00,
97 mipi_dbi_command(mipi
, MIPI_DCS_SET_DISPLAY_ON
);
101 mipi_dbi_command(mipi
, MIPI_DCS_ENTER_NORMAL_MODE
);
105 mipi_dbi_pipe_enable(pipe
, crtc_state
);
108 static const struct drm_simple_display_pipe_funcs jd_t18003_t01_pipe_funcs
= {
109 .enable
= jd_t18003_t01_pipe_enable
,
110 .disable
= mipi_dbi_pipe_disable
,
111 .update
= tinydrm_display_pipe_update
,
112 .prepare_fb
= tinydrm_display_pipe_prepare_fb
,
115 static const struct drm_display_mode jd_t18003_t01_mode
= {
116 TINYDRM_MODE(128, 160, 28, 35),
119 DEFINE_DRM_GEM_CMA_FOPS(st7735r_fops
);
121 static struct drm_driver st7735r_driver
= {
122 .driver_features
= DRIVER_GEM
| DRIVER_MODESET
| DRIVER_PRIME
|
124 .fops
= &st7735r_fops
,
125 TINYDRM_GEM_DRIVER_OPS
,
126 .lastclose
= drm_fb_helper_lastclose
,
127 .debugfs_init
= mipi_dbi_debugfs_init
,
129 .desc
= "Sitronix ST7735R",
135 static const struct of_device_id st7735r_of_match
[] = {
136 { .compatible
= "jianda,jd-t18003-t01" },
139 MODULE_DEVICE_TABLE(of
, st7735r_of_match
);
141 static const struct spi_device_id st7735r_id
[] = {
142 { "jd-t18003-t01", 0 },
145 MODULE_DEVICE_TABLE(spi
, st7735r_id
);
147 static int st7735r_probe(struct spi_device
*spi
)
149 struct device
*dev
= &spi
->dev
;
150 struct mipi_dbi
*mipi
;
151 struct gpio_desc
*dc
;
155 mipi
= devm_kzalloc(dev
, sizeof(*mipi
), GFP_KERNEL
);
159 mipi
->reset
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_HIGH
);
160 if (IS_ERR(mipi
->reset
)) {
161 DRM_DEV_ERROR(dev
, "Failed to get gpio 'reset'\n");
162 return PTR_ERR(mipi
->reset
);
165 dc
= devm_gpiod_get(dev
, "dc", GPIOD_OUT_LOW
);
167 DRM_DEV_ERROR(dev
, "Failed to get gpio 'dc'\n");
171 mipi
->backlight
= tinydrm_of_find_backlight(dev
);
172 if (IS_ERR(mipi
->backlight
))
173 return PTR_ERR(mipi
->backlight
);
175 device_property_read_u32(dev
, "rotation", &rotation
);
177 ret
= mipi_dbi_spi_init(spi
, mipi
, dc
);
181 /* Cannot read from Adafruit 1.8" display via SPI */
182 mipi
->read_commands
= NULL
;
184 ret
= mipi_dbi_init(&spi
->dev
, mipi
, &jd_t18003_t01_pipe_funcs
,
185 &st7735r_driver
, &jd_t18003_t01_mode
, rotation
);
189 spi_set_drvdata(spi
, mipi
);
191 return devm_tinydrm_register(&mipi
->tinydrm
);
194 static void st7735r_shutdown(struct spi_device
*spi
)
196 struct mipi_dbi
*mipi
= spi_get_drvdata(spi
);
198 tinydrm_shutdown(&mipi
->tinydrm
);
201 static struct spi_driver st7735r_spi_driver
= {
204 .owner
= THIS_MODULE
,
205 .of_match_table
= st7735r_of_match
,
207 .id_table
= st7735r_id
,
208 .probe
= st7735r_probe
,
209 .shutdown
= st7735r_shutdown
,
211 module_spi_driver(st7735r_spi_driver
);
213 MODULE_DESCRIPTION("Sitronix ST7735R DRM driver");
214 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
215 MODULE_LICENSE("GPL");