xref: /openbmc/linux/drivers/gpu/drm/tiny/st7735r.c (revision 63aa5ec6)
1043386a0SNoralf Trønnes // SPDX-License-Identifier: GPL-2.0+
2043386a0SNoralf Trønnes /*
3d1d511d5SGeert Uytterhoeven  * DRM driver for display panels connected to a Sitronix ST7715R or ST7735R
4d1d511d5SGeert Uytterhoeven  * display controller in SPI mode.
5043386a0SNoralf Trønnes  *
6043386a0SNoralf Trønnes  * Copyright 2017 David Lechner <david@lechnology.com>
798823f37SGeert Uytterhoeven  * Copyright (C) 2019 Glider bvba
8043386a0SNoralf Trønnes  */
9043386a0SNoralf Trønnes 
10043386a0SNoralf Trønnes #include <linux/backlight.h>
11043386a0SNoralf Trønnes #include <linux/delay.h>
12043386a0SNoralf Trønnes #include <linux/dma-buf.h>
13043386a0SNoralf Trønnes #include <linux/gpio/consumer.h>
14043386a0SNoralf Trønnes #include <linux/module.h>
15043386a0SNoralf Trønnes #include <linux/property.h>
16043386a0SNoralf Trønnes #include <linux/spi/spi.h>
17043386a0SNoralf Trønnes #include <video/mipi_display.h>
18043386a0SNoralf Trønnes 
19043386a0SNoralf Trønnes #include <drm/drm_atomic_helper.h>
20043386a0SNoralf Trønnes #include <drm/drm_drv.h>
218ab59da2SThomas Zimmermann #include <drm/drm_fbdev_generic.h>
22820c1707SThomas Zimmermann #include <drm/drm_gem_atomic_helper.h>
234a83c26aSDanilo Krummrich #include <drm/drm_gem_dma_helper.h>
24f5ad671bSDaniel Vetter #include <drm/drm_managed.h>
25043386a0SNoralf Trønnes #include <drm/drm_mipi_dbi.h>
26043386a0SNoralf Trønnes 
27043386a0SNoralf Trønnes #define ST7735R_FRMCTR1		0xb1
28043386a0SNoralf Trønnes #define ST7735R_FRMCTR2		0xb2
29043386a0SNoralf Trønnes #define ST7735R_FRMCTR3		0xb3
30043386a0SNoralf Trønnes #define ST7735R_INVCTR		0xb4
31043386a0SNoralf Trønnes #define ST7735R_PWCTR1		0xc0
32043386a0SNoralf Trønnes #define ST7735R_PWCTR2		0xc1
33043386a0SNoralf Trønnes #define ST7735R_PWCTR3		0xc2
34043386a0SNoralf Trønnes #define ST7735R_PWCTR4		0xc3
35043386a0SNoralf Trønnes #define ST7735R_PWCTR5		0xc4
36043386a0SNoralf Trønnes #define ST7735R_VMCTR1		0xc5
37043386a0SNoralf Trønnes #define ST7735R_GAMCTRP1	0xe0
38043386a0SNoralf Trønnes #define ST7735R_GAMCTRN1	0xe1
39043386a0SNoralf Trønnes 
40043386a0SNoralf Trønnes #define ST7735R_MY	BIT(7)
41043386a0SNoralf Trønnes #define ST7735R_MX	BIT(6)
42043386a0SNoralf Trønnes #define ST7735R_MV	BIT(5)
4398823f37SGeert Uytterhoeven #define ST7735R_RGB	BIT(3)
44043386a0SNoralf Trønnes 
4598823f37SGeert Uytterhoeven struct st7735r_cfg {
4698823f37SGeert Uytterhoeven 	const struct drm_display_mode mode;
4798823f37SGeert Uytterhoeven 	unsigned int left_offset;
4898823f37SGeert Uytterhoeven 	unsigned int top_offset;
4998823f37SGeert Uytterhoeven 	unsigned int write_only:1;
5098823f37SGeert Uytterhoeven 	unsigned int rgb:1;		/* RGB (vs. BGR) */
5198823f37SGeert Uytterhoeven };
5298823f37SGeert Uytterhoeven 
5398823f37SGeert Uytterhoeven struct st7735r_priv {
5498823f37SGeert Uytterhoeven 	struct mipi_dbi_dev dbidev;	/* Must be first for .release() */
5598823f37SGeert Uytterhoeven 	const struct st7735r_cfg *cfg;
5698823f37SGeert Uytterhoeven };
5798823f37SGeert Uytterhoeven 
st7735r_pipe_enable(struct drm_simple_display_pipe * pipe,struct drm_crtc_state * crtc_state,struct drm_plane_state * plane_state)5898823f37SGeert Uytterhoeven static void st7735r_pipe_enable(struct drm_simple_display_pipe *pipe,
59043386a0SNoralf Trønnes 				struct drm_crtc_state *crtc_state,
60043386a0SNoralf Trønnes 				struct drm_plane_state *plane_state)
61043386a0SNoralf Trønnes {
62043386a0SNoralf Trønnes 	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
6398823f37SGeert Uytterhoeven 	struct st7735r_priv *priv = container_of(dbidev, struct st7735r_priv,
6498823f37SGeert Uytterhoeven 						 dbidev);
65043386a0SNoralf Trønnes 	struct mipi_dbi *dbi = &dbidev->dbi;
66043386a0SNoralf Trønnes 	int ret, idx;
67043386a0SNoralf Trønnes 	u8 addr_mode;
68043386a0SNoralf Trønnes 
69043386a0SNoralf Trønnes 	if (!drm_dev_enter(pipe->crtc.dev, &idx))
70043386a0SNoralf Trønnes 		return;
71043386a0SNoralf Trønnes 
72043386a0SNoralf Trønnes 	DRM_DEBUG_KMS("\n");
73043386a0SNoralf Trønnes 
74043386a0SNoralf Trønnes 	ret = mipi_dbi_poweron_reset(dbidev);
75043386a0SNoralf Trønnes 	if (ret)
76043386a0SNoralf Trønnes 		goto out_exit;
77043386a0SNoralf Trønnes 
78043386a0SNoralf Trønnes 	msleep(150);
79043386a0SNoralf Trønnes 
80043386a0SNoralf Trønnes 	mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
81043386a0SNoralf Trønnes 	msleep(500);
82043386a0SNoralf Trønnes 
83043386a0SNoralf Trønnes 	mipi_dbi_command(dbi, ST7735R_FRMCTR1, 0x01, 0x2c, 0x2d);
84043386a0SNoralf Trønnes 	mipi_dbi_command(dbi, ST7735R_FRMCTR2, 0x01, 0x2c, 0x2d);
85043386a0SNoralf Trønnes 	mipi_dbi_command(dbi, ST7735R_FRMCTR3, 0x01, 0x2c, 0x2d, 0x01, 0x2c,
86043386a0SNoralf Trønnes 			 0x2d);
87043386a0SNoralf Trønnes 	mipi_dbi_command(dbi, ST7735R_INVCTR, 0x07);
88043386a0SNoralf Trønnes 	mipi_dbi_command(dbi, ST7735R_PWCTR1, 0xa2, 0x02, 0x84);
89043386a0SNoralf Trønnes 	mipi_dbi_command(dbi, ST7735R_PWCTR2, 0xc5);
90043386a0SNoralf Trønnes 	mipi_dbi_command(dbi, ST7735R_PWCTR3, 0x0a, 0x00);
91043386a0SNoralf Trønnes 	mipi_dbi_command(dbi, ST7735R_PWCTR4, 0x8a, 0x2a);
92043386a0SNoralf Trønnes 	mipi_dbi_command(dbi, ST7735R_PWCTR5, 0x8a, 0xee);
93043386a0SNoralf Trønnes 	mipi_dbi_command(dbi, ST7735R_VMCTR1, 0x0e);
94043386a0SNoralf Trønnes 	mipi_dbi_command(dbi, MIPI_DCS_EXIT_INVERT_MODE);
95043386a0SNoralf Trønnes 	switch (dbidev->rotation) {
96043386a0SNoralf Trønnes 	default:
97043386a0SNoralf Trønnes 		addr_mode = ST7735R_MX | ST7735R_MY;
98043386a0SNoralf Trønnes 		break;
99043386a0SNoralf Trønnes 	case 90:
100043386a0SNoralf Trønnes 		addr_mode = ST7735R_MX | ST7735R_MV;
101043386a0SNoralf Trønnes 		break;
102043386a0SNoralf Trønnes 	case 180:
103043386a0SNoralf Trønnes 		addr_mode = 0;
104043386a0SNoralf Trønnes 		break;
105043386a0SNoralf Trønnes 	case 270:
106043386a0SNoralf Trønnes 		addr_mode = ST7735R_MY | ST7735R_MV;
107043386a0SNoralf Trønnes 		break;
108043386a0SNoralf Trønnes 	}
10998823f37SGeert Uytterhoeven 
11098823f37SGeert Uytterhoeven 	if (priv->cfg->rgb)
11198823f37SGeert Uytterhoeven 		addr_mode |= ST7735R_RGB;
11298823f37SGeert Uytterhoeven 
113043386a0SNoralf Trønnes 	mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
114043386a0SNoralf Trønnes 	mipi_dbi_command(dbi, MIPI_DCS_SET_PIXEL_FORMAT,
115043386a0SNoralf Trønnes 			 MIPI_DCS_PIXEL_FMT_16BIT);
116043386a0SNoralf Trønnes 	mipi_dbi_command(dbi, ST7735R_GAMCTRP1, 0x02, 0x1c, 0x07, 0x12, 0x37,
117043386a0SNoralf Trønnes 			 0x32, 0x29, 0x2d, 0x29, 0x25, 0x2b, 0x39, 0x00, 0x01,
118043386a0SNoralf Trønnes 			 0x03, 0x10);
119043386a0SNoralf Trønnes 	mipi_dbi_command(dbi, ST7735R_GAMCTRN1, 0x03, 0x1d, 0x07, 0x06, 0x2e,
120043386a0SNoralf Trønnes 			 0x2c, 0x29, 0x2d, 0x2e, 0x2e, 0x37, 0x3f, 0x00, 0x00,
121043386a0SNoralf Trønnes 			 0x02, 0x10);
122043386a0SNoralf Trønnes 	mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
123043386a0SNoralf Trønnes 
124043386a0SNoralf Trønnes 	msleep(100);
125043386a0SNoralf Trønnes 
126043386a0SNoralf Trønnes 	mipi_dbi_command(dbi, MIPI_DCS_ENTER_NORMAL_MODE);
127043386a0SNoralf Trønnes 
128043386a0SNoralf Trønnes 	msleep(20);
129043386a0SNoralf Trønnes 
130043386a0SNoralf Trønnes 	mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
131043386a0SNoralf Trønnes out_exit:
132043386a0SNoralf Trønnes 	drm_dev_exit(idx);
133043386a0SNoralf Trønnes }
134043386a0SNoralf Trønnes 
13598823f37SGeert Uytterhoeven static const struct drm_simple_display_pipe_funcs st7735r_pipe_funcs = {
136*63aa5ec6SThomas Zimmermann 	DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS(st7735r_pipe_enable),
137043386a0SNoralf Trønnes };
138043386a0SNoralf Trønnes 
13998823f37SGeert Uytterhoeven static const struct st7735r_cfg jd_t18003_t01_cfg = {
14098823f37SGeert Uytterhoeven 	.mode		= { DRM_SIMPLE_MODE(128, 160, 28, 35) },
14198823f37SGeert Uytterhoeven 	/* Cannot read from Adafruit 1.8" display via SPI */
14298823f37SGeert Uytterhoeven 	.write_only	= true,
14398823f37SGeert Uytterhoeven };
14498823f37SGeert Uytterhoeven 
145d1d511d5SGeert Uytterhoeven static const struct st7735r_cfg rh128128t_cfg = {
146d1d511d5SGeert Uytterhoeven 	.mode		= { DRM_SIMPLE_MODE(128, 128, 25, 26) },
147d1d511d5SGeert Uytterhoeven 	.left_offset	= 2,
148d1d511d5SGeert Uytterhoeven 	.top_offset	= 3,
149d1d511d5SGeert Uytterhoeven 	.rgb		= true,
150043386a0SNoralf Trønnes };
151043386a0SNoralf Trønnes 
1524a83c26aSDanilo Krummrich DEFINE_DRM_GEM_DMA_FOPS(st7735r_fops);
153043386a0SNoralf Trønnes 
15470a59dd8SDaniel Vetter static const struct drm_driver st7735r_driver = {
155043386a0SNoralf Trønnes 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
156043386a0SNoralf Trønnes 	.fops			= &st7735r_fops,
1574a83c26aSDanilo Krummrich 	DRM_GEM_DMA_DRIVER_OPS_VMAP,
158043386a0SNoralf Trønnes 	.debugfs_init		= mipi_dbi_debugfs_init,
159043386a0SNoralf Trønnes 	.name			= "st7735r",
160043386a0SNoralf Trønnes 	.desc			= "Sitronix ST7735R",
161043386a0SNoralf Trønnes 	.date			= "20171128",
162043386a0SNoralf Trønnes 	.major			= 1,
163043386a0SNoralf Trønnes 	.minor			= 0,
164043386a0SNoralf Trønnes };
165043386a0SNoralf Trønnes 
166043386a0SNoralf Trønnes static const struct of_device_id st7735r_of_match[] = {
16798823f37SGeert Uytterhoeven 	{ .compatible = "jianda,jd-t18003-t01", .data = &jd_t18003_t01_cfg },
168d1d511d5SGeert Uytterhoeven 	{ .compatible = "okaya,rh128128t", .data = &rh128128t_cfg },
169043386a0SNoralf Trønnes 	{ },
170043386a0SNoralf Trønnes };
171043386a0SNoralf Trønnes MODULE_DEVICE_TABLE(of, st7735r_of_match);
172043386a0SNoralf Trønnes 
173043386a0SNoralf Trønnes static const struct spi_device_id st7735r_id[] = {
17498823f37SGeert Uytterhoeven 	{ "jd-t18003-t01", (uintptr_t)&jd_t18003_t01_cfg },
1759ad6f181SJavier Martinez Canillas 	{ "rh128128t", (uintptr_t)&rh128128t_cfg },
176043386a0SNoralf Trønnes 	{ },
177043386a0SNoralf Trønnes };
178043386a0SNoralf Trønnes MODULE_DEVICE_TABLE(spi, st7735r_id);
179043386a0SNoralf Trønnes 
st7735r_probe(struct spi_device * spi)180043386a0SNoralf Trønnes static int st7735r_probe(struct spi_device *spi)
181043386a0SNoralf Trønnes {
182043386a0SNoralf Trønnes 	struct device *dev = &spi->dev;
18398823f37SGeert Uytterhoeven 	const struct st7735r_cfg *cfg;
184043386a0SNoralf Trønnes 	struct mipi_dbi_dev *dbidev;
18598823f37SGeert Uytterhoeven 	struct st7735r_priv *priv;
186043386a0SNoralf Trønnes 	struct drm_device *drm;
187043386a0SNoralf Trønnes 	struct mipi_dbi *dbi;
188043386a0SNoralf Trønnes 	struct gpio_desc *dc;
189043386a0SNoralf Trønnes 	u32 rotation = 0;
190043386a0SNoralf Trønnes 	int ret;
191043386a0SNoralf Trønnes 
1925703d6aeSAndy Shevchenko 	cfg = device_get_match_data(&spi->dev);
19398823f37SGeert Uytterhoeven 	if (!cfg)
19498823f37SGeert Uytterhoeven 		cfg = (void *)spi_get_device_id(spi)->driver_data;
19598823f37SGeert Uytterhoeven 
19614877bc7SDaniel Vetter 	priv = devm_drm_dev_alloc(dev, &st7735r_driver,
19714877bc7SDaniel Vetter 				  struct st7735r_priv, dbidev.drm);
19814877bc7SDaniel Vetter 	if (IS_ERR(priv))
19914877bc7SDaniel Vetter 		return PTR_ERR(priv);
200043386a0SNoralf Trønnes 
20198823f37SGeert Uytterhoeven 	dbidev = &priv->dbidev;
20298823f37SGeert Uytterhoeven 	priv->cfg = cfg;
20398823f37SGeert Uytterhoeven 
204043386a0SNoralf Trønnes 	dbi = &dbidev->dbi;
205043386a0SNoralf Trønnes 	drm = &dbidev->drm;
206043386a0SNoralf Trønnes 
207043386a0SNoralf Trønnes 	dbi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
208a3204e2eSAndy Shevchenko 	if (IS_ERR(dbi->reset))
209a3204e2eSAndy Shevchenko 		return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n");
210043386a0SNoralf Trønnes 
211043386a0SNoralf Trønnes 	dc = devm_gpiod_get(dev, "dc", GPIOD_OUT_LOW);
212a3204e2eSAndy Shevchenko 	if (IS_ERR(dc))
213a3204e2eSAndy Shevchenko 		return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n");
214043386a0SNoralf Trønnes 
215043386a0SNoralf Trønnes 	dbidev->backlight = devm_of_find_backlight(dev);
216043386a0SNoralf Trønnes 	if (IS_ERR(dbidev->backlight))
217043386a0SNoralf Trønnes 		return PTR_ERR(dbidev->backlight);
218043386a0SNoralf Trønnes 
219043386a0SNoralf Trønnes 	device_property_read_u32(dev, "rotation", &rotation);
220043386a0SNoralf Trønnes 
221043386a0SNoralf Trønnes 	ret = mipi_dbi_spi_init(spi, dbi, dc);
222043386a0SNoralf Trønnes 	if (ret)
223043386a0SNoralf Trønnes 		return ret;
224043386a0SNoralf Trønnes 
22598823f37SGeert Uytterhoeven 	if (cfg->write_only)
226043386a0SNoralf Trønnes 		dbi->read_commands = NULL;
227043386a0SNoralf Trønnes 
22898823f37SGeert Uytterhoeven 	dbidev->left_offset = cfg->left_offset;
22998823f37SGeert Uytterhoeven 	dbidev->top_offset = cfg->top_offset;
23098823f37SGeert Uytterhoeven 
23198823f37SGeert Uytterhoeven 	ret = mipi_dbi_dev_init(dbidev, &st7735r_pipe_funcs, &cfg->mode,
23298823f37SGeert Uytterhoeven 				rotation);
233043386a0SNoralf Trønnes 	if (ret)
234043386a0SNoralf Trønnes 		return ret;
235043386a0SNoralf Trønnes 
236043386a0SNoralf Trønnes 	drm_mode_config_reset(drm);
237043386a0SNoralf Trønnes 
238043386a0SNoralf Trønnes 	ret = drm_dev_register(drm, 0);
239043386a0SNoralf Trønnes 	if (ret)
240043386a0SNoralf Trønnes 		return ret;
241043386a0SNoralf Trønnes 
242043386a0SNoralf Trønnes 	spi_set_drvdata(spi, drm);
243043386a0SNoralf Trønnes 
244043386a0SNoralf Trønnes 	drm_fbdev_generic_setup(drm, 0);
245043386a0SNoralf Trønnes 
246043386a0SNoralf Trønnes 	return 0;
247043386a0SNoralf Trønnes }
248043386a0SNoralf Trønnes 
st7735r_remove(struct spi_device * spi)249a0386bbaSUwe Kleine-König static void st7735r_remove(struct spi_device *spi)
250043386a0SNoralf Trønnes {
251043386a0SNoralf Trønnes 	struct drm_device *drm = spi_get_drvdata(spi);
252043386a0SNoralf Trønnes 
253043386a0SNoralf Trønnes 	drm_dev_unplug(drm);
254043386a0SNoralf Trønnes 	drm_atomic_helper_shutdown(drm);
255043386a0SNoralf Trønnes }
256043386a0SNoralf Trønnes 
st7735r_shutdown(struct spi_device * spi)257043386a0SNoralf Trønnes static void st7735r_shutdown(struct spi_device *spi)
258043386a0SNoralf Trønnes {
259043386a0SNoralf Trønnes 	drm_atomic_helper_shutdown(spi_get_drvdata(spi));
260043386a0SNoralf Trønnes }
261043386a0SNoralf Trønnes 
262043386a0SNoralf Trønnes static struct spi_driver st7735r_spi_driver = {
263043386a0SNoralf Trønnes 	.driver = {
264043386a0SNoralf Trønnes 		.name = "st7735r",
265043386a0SNoralf Trønnes 		.of_match_table = st7735r_of_match,
266043386a0SNoralf Trønnes 	},
267043386a0SNoralf Trønnes 	.id_table = st7735r_id,
268043386a0SNoralf Trønnes 	.probe = st7735r_probe,
269043386a0SNoralf Trønnes 	.remove = st7735r_remove,
270043386a0SNoralf Trønnes 	.shutdown = st7735r_shutdown,
271043386a0SNoralf Trønnes };
272043386a0SNoralf Trønnes module_spi_driver(st7735r_spi_driver);
273043386a0SNoralf Trønnes 
274043386a0SNoralf Trønnes MODULE_DESCRIPTION("Sitronix ST7735R DRM driver");
275043386a0SNoralf Trønnes MODULE_AUTHOR("David Lechner <david@lechnology.com>");
276043386a0SNoralf Trønnes MODULE_LICENSE("GPL");
277