1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * DRM driver for Sitronix ST7735R panels
4  *
5  * Copyright 2017 David Lechner <david@lechnology.com>
6  */
7 
8 #include <linux/backlight.h>
9 #include <linux/delay.h>
10 #include <linux/dma-buf.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/module.h>
13 #include <linux/property.h>
14 #include <linux/spi/spi.h>
15 #include <video/mipi_display.h>
16 
17 #include <drm/drm_fb_helper.h>
18 #include <drm/drm_gem_framebuffer_helper.h>
19 #include <drm/tinydrm/mipi-dbi.h>
20 #include <drm/tinydrm/tinydrm-helpers.h>
21 
22 #define ST7735R_FRMCTR1		0xb1
23 #define ST7735R_FRMCTR2		0xb2
24 #define ST7735R_FRMCTR3		0xb3
25 #define ST7735R_INVCTR		0xb4
26 #define ST7735R_PWCTR1		0xc0
27 #define ST7735R_PWCTR2		0xc1
28 #define ST7735R_PWCTR3		0xc2
29 #define ST7735R_PWCTR4		0xc3
30 #define ST7735R_PWCTR5		0xc4
31 #define ST7735R_VMCTR1		0xc5
32 #define ST7735R_GAMCTRP1	0xe0
33 #define ST7735R_GAMCTRN1	0xe1
34 
35 #define ST7735R_MY	BIT(7)
36 #define ST7735R_MX	BIT(6)
37 #define ST7735R_MV	BIT(5)
38 
jd_t18003_t01_pipe_enable(struct drm_simple_display_pipe * pipe,struct drm_crtc_state * crtc_state,struct drm_plane_state * plane_state)39 static void jd_t18003_t01_pipe_enable(struct drm_simple_display_pipe *pipe,
40 				      struct drm_crtc_state *crtc_state,
41 				      struct drm_plane_state *plane_state)
42 {
43 	struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
44 	struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
45 	int ret;
46 	u8 addr_mode;
47 
48 	DRM_DEBUG_KMS("\n");
49 
50 	ret = mipi_dbi_poweron_reset(mipi);
51 	if (ret)
52 		return;
53 
54 	msleep(150);
55 
56 	mipi_dbi_command(mipi, MIPI_DCS_EXIT_SLEEP_MODE);
57 	msleep(500);
58 
59 	mipi_dbi_command(mipi, ST7735R_FRMCTR1, 0x01, 0x2c, 0x2d);
60 	mipi_dbi_command(mipi, ST7735R_FRMCTR2, 0x01, 0x2c, 0x2d);
61 	mipi_dbi_command(mipi, ST7735R_FRMCTR3, 0x01, 0x2c, 0x2d, 0x01, 0x2c,
62 			 0x2d);
63 	mipi_dbi_command(mipi, ST7735R_INVCTR, 0x07);
64 	mipi_dbi_command(mipi, ST7735R_PWCTR1, 0xa2, 0x02, 0x84);
65 	mipi_dbi_command(mipi, ST7735R_PWCTR2, 0xc5);
66 	mipi_dbi_command(mipi, ST7735R_PWCTR3, 0x0a, 0x00);
67 	mipi_dbi_command(mipi, ST7735R_PWCTR4, 0x8a, 0x2a);
68 	mipi_dbi_command(mipi, ST7735R_PWCTR5, 0x8a, 0xee);
69 	mipi_dbi_command(mipi, ST7735R_VMCTR1, 0x0e);
70 	mipi_dbi_command(mipi, MIPI_DCS_EXIT_INVERT_MODE);
71 	switch (mipi->rotation) {
72 	default:
73 		addr_mode = ST7735R_MX | ST7735R_MY;
74 		break;
75 	case 90:
76 		addr_mode = ST7735R_MX | ST7735R_MV;
77 		break;
78 	case 180:
79 		addr_mode = 0;
80 		break;
81 	case 270:
82 		addr_mode = ST7735R_MY | ST7735R_MV;
83 		break;
84 	}
85 	mipi_dbi_command(mipi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
86 	mipi_dbi_command(mipi, MIPI_DCS_SET_PIXEL_FORMAT,
87 			 MIPI_DCS_PIXEL_FMT_16BIT);
88 	mipi_dbi_command(mipi, ST7735R_GAMCTRP1, 0x02, 0x1c, 0x07, 0x12, 0x37,
89 			 0x32, 0x29, 0x2d, 0x29, 0x25, 0x2b, 0x39, 0x00, 0x01,
90 			 0x03, 0x10);
91 	mipi_dbi_command(mipi, ST7735R_GAMCTRN1, 0x03, 0x1d, 0x07, 0x06, 0x2e,
92 			 0x2c, 0x29, 0x2d, 0x2e, 0x2e, 0x37, 0x3f, 0x00, 0x00,
93 			 0x02, 0x10);
94 	mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_ON);
95 
96 	msleep(100);
97 
98 	mipi_dbi_command(mipi, MIPI_DCS_ENTER_NORMAL_MODE);
99 
100 	msleep(20);
101 
102 	mipi_dbi_enable_flush(mipi, crtc_state, plane_state);
103 }
104 
105 static const struct drm_simple_display_pipe_funcs jd_t18003_t01_pipe_funcs = {
106 	.enable		= jd_t18003_t01_pipe_enable,
107 	.disable	= mipi_dbi_pipe_disable,
108 	.update		= tinydrm_display_pipe_update,
109 	.prepare_fb	= drm_gem_fb_simple_display_pipe_prepare_fb,
110 };
111 
112 static const struct drm_display_mode jd_t18003_t01_mode = {
113 	TINYDRM_MODE(128, 160, 28, 35),
114 };
115 
116 DEFINE_DRM_GEM_CMA_FOPS(st7735r_fops);
117 
118 static struct drm_driver st7735r_driver = {
119 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
120 				  DRIVER_ATOMIC,
121 	.fops			= &st7735r_fops,
122 	TINYDRM_GEM_DRIVER_OPS,
123 	.debugfs_init		= mipi_dbi_debugfs_init,
124 	.name			= "st7735r",
125 	.desc			= "Sitronix ST7735R",
126 	.date			= "20171128",
127 	.major			= 1,
128 	.minor			= 0,
129 };
130 
131 static const struct of_device_id st7735r_of_match[] = {
132 	{ .compatible = "jianda,jd-t18003-t01" },
133 	{ },
134 };
135 MODULE_DEVICE_TABLE(of, st7735r_of_match);
136 
137 static const struct spi_device_id st7735r_id[] = {
138 	{ "jd-t18003-t01", 0 },
139 	{ },
140 };
141 MODULE_DEVICE_TABLE(spi, st7735r_id);
142 
st7735r_probe(struct spi_device * spi)143 static int st7735r_probe(struct spi_device *spi)
144 {
145 	struct device *dev = &spi->dev;
146 	struct mipi_dbi *mipi;
147 	struct gpio_desc *dc;
148 	u32 rotation = 0;
149 	int ret;
150 
151 	mipi = devm_kzalloc(dev, sizeof(*mipi), GFP_KERNEL);
152 	if (!mipi)
153 		return -ENOMEM;
154 
155 	mipi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
156 	if (IS_ERR(mipi->reset)) {
157 		DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
158 		return PTR_ERR(mipi->reset);
159 	}
160 
161 	dc = devm_gpiod_get(dev, "dc", GPIOD_OUT_LOW);
162 	if (IS_ERR(dc)) {
163 		DRM_DEV_ERROR(dev, "Failed to get gpio 'dc'\n");
164 		return PTR_ERR(dc);
165 	}
166 
167 	mipi->backlight = devm_of_find_backlight(dev);
168 	if (IS_ERR(mipi->backlight))
169 		return PTR_ERR(mipi->backlight);
170 
171 	device_property_read_u32(dev, "rotation", &rotation);
172 
173 	ret = mipi_dbi_spi_init(spi, mipi, dc);
174 	if (ret)
175 		return ret;
176 
177 	/* Cannot read from Adafruit 1.8" display via SPI */
178 	mipi->read_commands = NULL;
179 
180 	ret = mipi_dbi_init(&spi->dev, mipi, &jd_t18003_t01_pipe_funcs,
181 			    &st7735r_driver, &jd_t18003_t01_mode, rotation);
182 	if (ret)
183 		return ret;
184 
185 	spi_set_drvdata(spi, mipi);
186 
187 	return devm_tinydrm_register(&mipi->tinydrm);
188 }
189 
st7735r_shutdown(struct spi_device * spi)190 static void st7735r_shutdown(struct spi_device *spi)
191 {
192 	struct mipi_dbi *mipi = spi_get_drvdata(spi);
193 
194 	tinydrm_shutdown(&mipi->tinydrm);
195 }
196 
197 static struct spi_driver st7735r_spi_driver = {
198 	.driver = {
199 		.name = "st7735r",
200 		.owner = THIS_MODULE,
201 		.of_match_table = st7735r_of_match,
202 	},
203 	.id_table = st7735r_id,
204 	.probe = st7735r_probe,
205 	.shutdown = st7735r_shutdown,
206 };
207 module_spi_driver(st7735r_spi_driver);
208 
209 MODULE_DESCRIPTION("Sitronix ST7735R DRM driver");
210 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
211 MODULE_LICENSE("GPL");
212