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