xref: /openbmc/linux/drivers/gpu/drm/tiny/st7586.c (revision 8dd3cdea)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * DRM driver for Sitronix ST7586 panels
4  *
5  * Copyright 2017 David Lechner <david@lechnology.com>
6  */
7 
8 #include <linux/delay.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/module.h>
11 #include <linux/property.h>
12 #include <linux/spi/spi.h>
13 #include <video/mipi_display.h>
14 
15 #include <drm/drm_atomic_helper.h>
16 #include <drm/drm_damage_helper.h>
17 #include <drm/drm_drv.h>
18 #include <drm/drm_fb_cma_helper.h>
19 #include <drm/drm_fb_helper.h>
20 #include <drm/drm_format_helper.h>
21 #include <drm/drm_gem_atomic_helper.h>
22 #include <drm/drm_gem_cma_helper.h>
23 #include <drm/drm_gem_framebuffer_helper.h>
24 #include <drm/drm_managed.h>
25 #include <drm/drm_mipi_dbi.h>
26 #include <drm/drm_rect.h>
27 
28 /* controller-specific commands */
29 #define ST7586_DISP_MODE_GRAY	0x38
30 #define ST7586_DISP_MODE_MONO	0x39
31 #define ST7586_ENABLE_DDRAM	0x3a
32 #define ST7586_SET_DISP_DUTY	0xb0
33 #define ST7586_SET_PART_DISP	0xb4
34 #define ST7586_SET_NLINE_INV	0xb5
35 #define ST7586_SET_VOP		0xc0
36 #define ST7586_SET_BIAS_SYSTEM	0xc3
37 #define ST7586_SET_BOOST_LEVEL	0xc4
38 #define ST7586_SET_VOP_OFFSET	0xc7
39 #define ST7586_ENABLE_ANALOG	0xd0
40 #define ST7586_AUTO_READ_CTRL	0xd7
41 #define ST7586_OTP_RW_CTRL	0xe0
42 #define ST7586_OTP_CTRL_OUT	0xe1
43 #define ST7586_OTP_READ		0xe3
44 
45 #define ST7586_DISP_CTRL_MX	BIT(6)
46 #define ST7586_DISP_CTRL_MY	BIT(7)
47 
48 /*
49  * The ST7586 controller has an unusual pixel format where 2bpp grayscale is
50  * packed 3 pixels per byte with the first two pixels using 3 bits and the 3rd
51  * pixel using only 2 bits.
52  *
53  * |  D7  |  D6  |  D5  ||      |      || 2bpp |
54  * | (D4) | (D3) | (D2) ||  D1  |  D0  || GRAY |
55  * +------+------+------++------+------++------+
56  * |  1   |  1   |  1   ||  1   |  1   || 0  0 | black
57  * |  1   |  0   |  0   ||  1   |  0   || 0  1 | dark gray
58  * |  0   |  1   |  0   ||  0   |  1   || 1  0 | light gray
59  * |  0   |  0   |  0   ||  0   |  0   || 1  1 | white
60  */
61 
62 static const u8 st7586_lookup[] = { 0x7, 0x4, 0x2, 0x0 };
63 
64 static void st7586_xrgb8888_to_gray332(u8 *dst, void *vaddr,
65 				       struct drm_framebuffer *fb,
66 				       struct drm_rect *clip)
67 {
68 	size_t len = (clip->x2 - clip->x1) * (clip->y2 - clip->y1);
69 	unsigned int x, y;
70 	u8 *src, *buf, val;
71 
72 	buf = kmalloc(len, GFP_KERNEL);
73 	if (!buf)
74 		return;
75 
76 	drm_fb_xrgb8888_to_gray8(buf, 0, vaddr, fb, clip);
77 	src = buf;
78 
79 	for (y = clip->y1; y < clip->y2; y++) {
80 		for (x = clip->x1; x < clip->x2; x += 3) {
81 			val = st7586_lookup[*src++ >> 6] << 5;
82 			val |= st7586_lookup[*src++ >> 6] << 2;
83 			val |= st7586_lookup[*src++ >> 6] >> 1;
84 			*dst++ = val;
85 		}
86 	}
87 
88 	kfree(buf);
89 }
90 
91 static int st7586_buf_copy(void *dst, struct drm_framebuffer *fb,
92 			   struct drm_rect *clip)
93 {
94 	struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
95 	void *src = cma_obj->vaddr;
96 	int ret = 0;
97 
98 	ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
99 	if (ret)
100 		return ret;
101 
102 	st7586_xrgb8888_to_gray332(dst, src, fb, clip);
103 
104 	drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
105 
106 	return 0;
107 }
108 
109 static void st7586_fb_dirty(struct drm_framebuffer *fb, struct drm_rect *rect)
110 {
111 	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(fb->dev);
112 	struct mipi_dbi *dbi = &dbidev->dbi;
113 	int start, end, idx, ret = 0;
114 
115 	if (!drm_dev_enter(fb->dev, &idx))
116 		return;
117 
118 	/* 3 pixels per byte, so grow clip to nearest multiple of 3 */
119 	rect->x1 = rounddown(rect->x1, 3);
120 	rect->x2 = roundup(rect->x2, 3);
121 
122 	DRM_DEBUG_KMS("Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect));
123 
124 	ret = st7586_buf_copy(dbidev->tx_buf, fb, rect);
125 	if (ret)
126 		goto err_msg;
127 
128 	/* Pixels are packed 3 per byte */
129 	start = rect->x1 / 3;
130 	end = rect->x2 / 3;
131 
132 	mipi_dbi_command(dbi, MIPI_DCS_SET_COLUMN_ADDRESS,
133 			 (start >> 8) & 0xFF, start & 0xFF,
134 			 (end >> 8) & 0xFF, (end - 1) & 0xFF);
135 	mipi_dbi_command(dbi, MIPI_DCS_SET_PAGE_ADDRESS,
136 			 (rect->y1 >> 8) & 0xFF, rect->y1 & 0xFF,
137 			 (rect->y2 >> 8) & 0xFF, (rect->y2 - 1) & 0xFF);
138 
139 	ret = mipi_dbi_command_buf(dbi, MIPI_DCS_WRITE_MEMORY_START,
140 				   (u8 *)dbidev->tx_buf,
141 				   (end - start) * (rect->y2 - rect->y1));
142 err_msg:
143 	if (ret)
144 		dev_err_once(fb->dev->dev, "Failed to update display %d\n", ret);
145 
146 	drm_dev_exit(idx);
147 }
148 
149 static void st7586_pipe_update(struct drm_simple_display_pipe *pipe,
150 			       struct drm_plane_state *old_state)
151 {
152 	struct drm_plane_state *state = pipe->plane.state;
153 	struct drm_rect rect;
154 
155 	if (!pipe->crtc.state->active)
156 		return;
157 
158 	if (drm_atomic_helper_damage_merged(old_state, state, &rect))
159 		st7586_fb_dirty(state->fb, &rect);
160 }
161 
162 static void st7586_pipe_enable(struct drm_simple_display_pipe *pipe,
163 			       struct drm_crtc_state *crtc_state,
164 			       struct drm_plane_state *plane_state)
165 {
166 	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
167 	struct drm_framebuffer *fb = plane_state->fb;
168 	struct mipi_dbi *dbi = &dbidev->dbi;
169 	struct drm_rect rect = {
170 		.x1 = 0,
171 		.x2 = fb->width,
172 		.y1 = 0,
173 		.y2 = fb->height,
174 	};
175 	int idx, ret;
176 	u8 addr_mode;
177 
178 	if (!drm_dev_enter(pipe->crtc.dev, &idx))
179 		return;
180 
181 	DRM_DEBUG_KMS("\n");
182 
183 	ret = mipi_dbi_poweron_reset(dbidev);
184 	if (ret)
185 		goto out_exit;
186 
187 	mipi_dbi_command(dbi, ST7586_AUTO_READ_CTRL, 0x9f);
188 	mipi_dbi_command(dbi, ST7586_OTP_RW_CTRL, 0x00);
189 
190 	msleep(10);
191 
192 	mipi_dbi_command(dbi, ST7586_OTP_READ);
193 
194 	msleep(20);
195 
196 	mipi_dbi_command(dbi, ST7586_OTP_CTRL_OUT);
197 	mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
198 	mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_OFF);
199 
200 	msleep(50);
201 
202 	mipi_dbi_command(dbi, ST7586_SET_VOP_OFFSET, 0x00);
203 	mipi_dbi_command(dbi, ST7586_SET_VOP, 0xe3, 0x00);
204 	mipi_dbi_command(dbi, ST7586_SET_BIAS_SYSTEM, 0x02);
205 	mipi_dbi_command(dbi, ST7586_SET_BOOST_LEVEL, 0x04);
206 	mipi_dbi_command(dbi, ST7586_ENABLE_ANALOG, 0x1d);
207 	mipi_dbi_command(dbi, ST7586_SET_NLINE_INV, 0x00);
208 	mipi_dbi_command(dbi, ST7586_DISP_MODE_GRAY);
209 	mipi_dbi_command(dbi, ST7586_ENABLE_DDRAM, 0x02);
210 
211 	switch (dbidev->rotation) {
212 	default:
213 		addr_mode = 0x00;
214 		break;
215 	case 90:
216 		addr_mode = ST7586_DISP_CTRL_MY;
217 		break;
218 	case 180:
219 		addr_mode = ST7586_DISP_CTRL_MX | ST7586_DISP_CTRL_MY;
220 		break;
221 	case 270:
222 		addr_mode = ST7586_DISP_CTRL_MX;
223 		break;
224 	}
225 	mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
226 
227 	mipi_dbi_command(dbi, ST7586_SET_DISP_DUTY, 0x7f);
228 	mipi_dbi_command(dbi, ST7586_SET_PART_DISP, 0xa0);
229 	mipi_dbi_command(dbi, MIPI_DCS_SET_PARTIAL_ROWS, 0x00, 0x00, 0x00, 0x77);
230 	mipi_dbi_command(dbi, MIPI_DCS_EXIT_INVERT_MODE);
231 
232 	msleep(100);
233 
234 	st7586_fb_dirty(fb, &rect);
235 
236 	mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
237 out_exit:
238 	drm_dev_exit(idx);
239 }
240 
241 static void st7586_pipe_disable(struct drm_simple_display_pipe *pipe)
242 {
243 	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
244 
245 	/*
246 	 * This callback is not protected by drm_dev_enter/exit since we want to
247 	 * turn off the display on regular driver unload. It's highly unlikely
248 	 * that the underlying SPI controller is gone should this be called after
249 	 * unplug.
250 	 */
251 
252 	DRM_DEBUG_KMS("\n");
253 
254 	mipi_dbi_command(&dbidev->dbi, MIPI_DCS_SET_DISPLAY_OFF);
255 }
256 
257 static const u32 st7586_formats[] = {
258 	DRM_FORMAT_XRGB8888,
259 };
260 
261 static const struct drm_simple_display_pipe_funcs st7586_pipe_funcs = {
262 	.enable		= st7586_pipe_enable,
263 	.disable	= st7586_pipe_disable,
264 	.update		= st7586_pipe_update,
265 };
266 
267 static const struct drm_display_mode st7586_mode = {
268 	DRM_SIMPLE_MODE(178, 128, 37, 27),
269 };
270 
271 DEFINE_DRM_GEM_CMA_FOPS(st7586_fops);
272 
273 static const struct drm_driver st7586_driver = {
274 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
275 	.fops			= &st7586_fops,
276 	DRM_GEM_CMA_DRIVER_OPS_VMAP,
277 	.debugfs_init		= mipi_dbi_debugfs_init,
278 	.name			= "st7586",
279 	.desc			= "Sitronix ST7586",
280 	.date			= "20170801",
281 	.major			= 1,
282 	.minor			= 0,
283 };
284 
285 static const struct of_device_id st7586_of_match[] = {
286 	{ .compatible = "lego,ev3-lcd" },
287 	{},
288 };
289 MODULE_DEVICE_TABLE(of, st7586_of_match);
290 
291 static const struct spi_device_id st7586_id[] = {
292 	{ "ev3-lcd", 0 },
293 	{ },
294 };
295 MODULE_DEVICE_TABLE(spi, st7586_id);
296 
297 static int st7586_probe(struct spi_device *spi)
298 {
299 	struct device *dev = &spi->dev;
300 	struct mipi_dbi_dev *dbidev;
301 	struct drm_device *drm;
302 	struct mipi_dbi *dbi;
303 	struct gpio_desc *a0;
304 	u32 rotation = 0;
305 	size_t bufsize;
306 	int ret;
307 
308 	dbidev = devm_drm_dev_alloc(dev, &st7586_driver,
309 				    struct mipi_dbi_dev, drm);
310 	if (IS_ERR(dbidev))
311 		return PTR_ERR(dbidev);
312 
313 	dbi = &dbidev->dbi;
314 	drm = &dbidev->drm;
315 
316 	bufsize = (st7586_mode.vdisplay + 2) / 3 * st7586_mode.hdisplay;
317 
318 	dbi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
319 	if (IS_ERR(dbi->reset))
320 		return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n");
321 
322 	a0 = devm_gpiod_get(dev, "a0", GPIOD_OUT_LOW);
323 	if (IS_ERR(a0))
324 		return dev_err_probe(dev, PTR_ERR(a0), "Failed to get GPIO 'a0'\n");
325 
326 	device_property_read_u32(dev, "rotation", &rotation);
327 
328 	ret = mipi_dbi_spi_init(spi, dbi, a0);
329 	if (ret)
330 		return ret;
331 
332 	/* Cannot read from this controller via SPI */
333 	dbi->read_commands = NULL;
334 
335 	ret = mipi_dbi_dev_init_with_formats(dbidev, &st7586_pipe_funcs,
336 					     st7586_formats, ARRAY_SIZE(st7586_formats),
337 					     &st7586_mode, rotation, bufsize);
338 	if (ret)
339 		return ret;
340 
341 	/*
342 	 * we are using 8-bit data, so we are not actually swapping anything,
343 	 * but setting mipi->swap_bytes makes mipi_dbi_typec3_command() do the
344 	 * right thing and not use 16-bit transfers (which results in swapped
345 	 * bytes on little-endian systems and causes out of order data to be
346 	 * sent to the display).
347 	 */
348 	dbi->swap_bytes = true;
349 
350 	drm_mode_config_reset(drm);
351 
352 	ret = drm_dev_register(drm, 0);
353 	if (ret)
354 		return ret;
355 
356 	spi_set_drvdata(spi, drm);
357 
358 	drm_fbdev_generic_setup(drm, 0);
359 
360 	return 0;
361 }
362 
363 static void st7586_remove(struct spi_device *spi)
364 {
365 	struct drm_device *drm = spi_get_drvdata(spi);
366 
367 	drm_dev_unplug(drm);
368 	drm_atomic_helper_shutdown(drm);
369 }
370 
371 static void st7586_shutdown(struct spi_device *spi)
372 {
373 	drm_atomic_helper_shutdown(spi_get_drvdata(spi));
374 }
375 
376 static struct spi_driver st7586_spi_driver = {
377 	.driver = {
378 		.name = "st7586",
379 		.owner = THIS_MODULE,
380 		.of_match_table = st7586_of_match,
381 	},
382 	.id_table = st7586_id,
383 	.probe = st7586_probe,
384 	.remove = st7586_remove,
385 	.shutdown = st7586_shutdown,
386 };
387 module_spi_driver(st7586_spi_driver);
388 
389 MODULE_DESCRIPTION("Sitronix ST7586 DRM driver");
390 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
391 MODULE_LICENSE("GPL");
392