xref: /linux/drivers/gpu/drm/tiny/hx8357d.c (revision 2c1ed907520c50326b8f604907a8478b27881a2e)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * DRM driver for the HX8357D LCD controller
4  *
5  * Copyright 2018 Broadcom
6  * Copyright 2018 David Lechner <david@lechnology.com>
7  * Copyright 2016 Noralf Trønnes
8  * Copyright (C) 2015 Adafruit Industries
9  * Copyright (C) 2013 Christian Vogelgsang
10  */
11 
12 #include <linux/backlight.h>
13 #include <linux/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/module.h>
16 #include <linux/property.h>
17 #include <linux/spi/spi.h>
18 
19 #include <drm/clients/drm_client_setup.h>
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_drv.h>
22 #include <drm/drm_fbdev_dma.h>
23 #include <drm/drm_gem_atomic_helper.h>
24 #include <drm/drm_gem_dma_helper.h>
25 #include <drm/drm_managed.h>
26 #include <drm/drm_mipi_dbi.h>
27 #include <drm/drm_modeset_helper.h>
28 #include <video/mipi_display.h>
29 
30 #define HX8357D_SETOSC 0xb0
31 #define HX8357D_SETPOWER 0xb1
32 #define HX8357D_SETRGB 0xb3
33 #define HX8357D_SETCYC 0xb3
34 #define HX8357D_SETCOM 0xb6
35 #define HX8357D_SETEXTC 0xb9
36 #define HX8357D_SETSTBA 0xc0
37 #define HX8357D_SETPANEL 0xcc
38 #define HX8357D_SETGAMMA 0xe0
39 
40 #define HX8357D_MADCTL_MY  0x80
41 #define HX8357D_MADCTL_MX  0x40
42 #define HX8357D_MADCTL_MV  0x20
43 #define HX8357D_MADCTL_ML  0x10
44 #define HX8357D_MADCTL_RGB 0x00
45 #define HX8357D_MADCTL_BGR 0x08
46 #define HX8357D_MADCTL_MH  0x04
47 
yx240qv29_enable(struct drm_simple_display_pipe * pipe,struct drm_crtc_state * crtc_state,struct drm_plane_state * plane_state)48 static void yx240qv29_enable(struct drm_simple_display_pipe *pipe,
49 			     struct drm_crtc_state *crtc_state,
50 			     struct drm_plane_state *plane_state)
51 {
52 	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
53 	struct mipi_dbi *dbi = &dbidev->dbi;
54 	u8 addr_mode;
55 	int ret, idx;
56 
57 	if (!drm_dev_enter(pipe->crtc.dev, &idx))
58 		return;
59 
60 	DRM_DEBUG_KMS("\n");
61 
62 	ret = mipi_dbi_poweron_conditional_reset(dbidev);
63 	if (ret < 0)
64 		goto out_exit;
65 	if (ret == 1)
66 		goto out_enable;
67 
68 	/* setextc */
69 	mipi_dbi_command(dbi, HX8357D_SETEXTC, 0xFF, 0x83, 0x57);
70 	msleep(150);
71 
72 	/* setRGB which also enables SDO */
73 	mipi_dbi_command(dbi, HX8357D_SETRGB, 0x00, 0x00, 0x06, 0x06);
74 
75 	/* -1.52V */
76 	mipi_dbi_command(dbi, HX8357D_SETCOM, 0x25);
77 
78 	/* Normal mode 70Hz, Idle mode 55 Hz */
79 	mipi_dbi_command(dbi, HX8357D_SETOSC, 0x68);
80 
81 	/* Set Panel - BGR, Gate direction swapped */
82 	mipi_dbi_command(dbi, HX8357D_SETPANEL, 0x05);
83 
84 	mipi_dbi_command(dbi, HX8357D_SETPOWER,
85 			 0x00,  /* Not deep standby */
86 			 0x15,  /* BT */
87 			 0x1C,  /* VSPR */
88 			 0x1C,  /* VSNR */
89 			 0x83,  /* AP */
90 			 0xAA);  /* FS */
91 
92 	mipi_dbi_command(dbi, HX8357D_SETSTBA,
93 			 0x50,  /* OPON normal */
94 			 0x50,  /* OPON idle */
95 			 0x01,  /* STBA */
96 			 0x3C,  /* STBA */
97 			 0x1E,  /* STBA */
98 			 0x08);  /* GEN */
99 
100 	mipi_dbi_command(dbi, HX8357D_SETCYC,
101 			 0x02,  /* NW 0x02 */
102 			 0x40,  /* RTN */
103 			 0x00,  /* DIV */
104 			 0x2A,  /* DUM */
105 			 0x2A,  /* DUM */
106 			 0x0D,  /* GDON */
107 			 0x78);  /* GDOFF */
108 
109 	mipi_dbi_command(dbi, HX8357D_SETGAMMA,
110 			 0x02,
111 			 0x0A,
112 			 0x11,
113 			 0x1d,
114 			 0x23,
115 			 0x35,
116 			 0x41,
117 			 0x4b,
118 			 0x4b,
119 			 0x42,
120 			 0x3A,
121 			 0x27,
122 			 0x1B,
123 			 0x08,
124 			 0x09,
125 			 0x03,
126 			 0x02,
127 			 0x0A,
128 			 0x11,
129 			 0x1d,
130 			 0x23,
131 			 0x35,
132 			 0x41,
133 			 0x4b,
134 			 0x4b,
135 			 0x42,
136 			 0x3A,
137 			 0x27,
138 			 0x1B,
139 			 0x08,
140 			 0x09,
141 			 0x03,
142 			 0x00,
143 			 0x01);
144 
145 	/* 16 bit */
146 	mipi_dbi_command(dbi, MIPI_DCS_SET_PIXEL_FORMAT,
147 			 MIPI_DCS_PIXEL_FMT_16BIT);
148 
149 	/* TE off */
150 	mipi_dbi_command(dbi, MIPI_DCS_SET_TEAR_ON, 0x00);
151 
152 	/* tear line */
153 	mipi_dbi_command(dbi, MIPI_DCS_SET_TEAR_SCANLINE, 0x00, 0x02);
154 
155 	/* Exit Sleep */
156 	mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
157 	msleep(150);
158 
159 	/* display on */
160 	mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
161 	usleep_range(5000, 7000);
162 
163 out_enable:
164 	switch (dbidev->rotation) {
165 	default:
166 		addr_mode = HX8357D_MADCTL_MX | HX8357D_MADCTL_MY;
167 		break;
168 	case 90:
169 		addr_mode = HX8357D_MADCTL_MV | HX8357D_MADCTL_MY;
170 		break;
171 	case 180:
172 		addr_mode = 0;
173 		break;
174 	case 270:
175 		addr_mode = HX8357D_MADCTL_MV | HX8357D_MADCTL_MX;
176 		break;
177 	}
178 	mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
179 	mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
180 out_exit:
181 	drm_dev_exit(idx);
182 }
183 
184 static const struct drm_simple_display_pipe_funcs hx8357d_pipe_funcs = {
185 	DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS(yx240qv29_enable),
186 };
187 
188 static const struct drm_display_mode yx350hv15_mode = {
189 	DRM_SIMPLE_MODE(320, 480, 60, 75),
190 };
191 
192 DEFINE_DRM_GEM_DMA_FOPS(hx8357d_fops);
193 
194 static const struct drm_driver hx8357d_driver = {
195 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
196 	.fops			= &hx8357d_fops,
197 	DRM_GEM_DMA_DRIVER_OPS_VMAP,
198 	DRM_FBDEV_DMA_DRIVER_OPS,
199 	.debugfs_init		= mipi_dbi_debugfs_init,
200 	.name			= "hx8357d",
201 	.desc			= "HX8357D",
202 	.major			= 1,
203 	.minor			= 0,
204 };
205 
206 static const struct of_device_id hx8357d_of_match[] = {
207 	{ .compatible = "adafruit,yx350hv15" },
208 	{ }
209 };
210 MODULE_DEVICE_TABLE(of, hx8357d_of_match);
211 
212 static const struct spi_device_id hx8357d_id[] = {
213 	{ "yx350hv15", 0 },
214 	{ }
215 };
216 MODULE_DEVICE_TABLE(spi, hx8357d_id);
217 
hx8357d_probe(struct spi_device * spi)218 static int hx8357d_probe(struct spi_device *spi)
219 {
220 	struct device *dev = &spi->dev;
221 	struct mipi_dbi_dev *dbidev;
222 	struct drm_device *drm;
223 	struct gpio_desc *dc;
224 	u32 rotation = 0;
225 	int ret;
226 
227 	dbidev = devm_drm_dev_alloc(dev, &hx8357d_driver,
228 				    struct mipi_dbi_dev, drm);
229 	if (IS_ERR(dbidev))
230 		return PTR_ERR(dbidev);
231 
232 	drm = &dbidev->drm;
233 
234 	dc = devm_gpiod_get(dev, "dc", GPIOD_OUT_LOW);
235 	if (IS_ERR(dc))
236 		return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n");
237 
238 	dbidev->backlight = devm_of_find_backlight(dev);
239 	if (IS_ERR(dbidev->backlight))
240 		return PTR_ERR(dbidev->backlight);
241 
242 	device_property_read_u32(dev, "rotation", &rotation);
243 
244 	ret = mipi_dbi_spi_init(spi, &dbidev->dbi, dc);
245 	if (ret)
246 		return ret;
247 
248 	ret = mipi_dbi_dev_init(dbidev, &hx8357d_pipe_funcs, &yx350hv15_mode, rotation);
249 	if (ret)
250 		return ret;
251 
252 	drm_mode_config_reset(drm);
253 
254 	ret = drm_dev_register(drm, 0);
255 	if (ret)
256 		return ret;
257 
258 	spi_set_drvdata(spi, drm);
259 
260 	drm_client_setup(drm, NULL);
261 
262 	return 0;
263 }
264 
hx8357d_remove(struct spi_device * spi)265 static void hx8357d_remove(struct spi_device *spi)
266 {
267 	struct drm_device *drm = spi_get_drvdata(spi);
268 
269 	drm_dev_unplug(drm);
270 	drm_atomic_helper_shutdown(drm);
271 }
272 
hx8357d_shutdown(struct spi_device * spi)273 static void hx8357d_shutdown(struct spi_device *spi)
274 {
275 	drm_atomic_helper_shutdown(spi_get_drvdata(spi));
276 }
277 
278 static struct spi_driver hx8357d_spi_driver = {
279 	.driver = {
280 		.name = "hx8357d",
281 		.of_match_table = hx8357d_of_match,
282 	},
283 	.id_table = hx8357d_id,
284 	.probe = hx8357d_probe,
285 	.remove = hx8357d_remove,
286 	.shutdown = hx8357d_shutdown,
287 };
288 module_spi_driver(hx8357d_spi_driver);
289 
290 MODULE_DESCRIPTION("HX8357D DRM driver");
291 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
292 MODULE_LICENSE("GPL");
293