xref: /openbmc/linux/drivers/gpu/drm/tiny/st7586.c (revision add48ba4)
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/dma-buf.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/module.h>
12 #include <linux/property.h>
13 #include <linux/spi/spi.h>
14 #include <video/mipi_display.h>
15 
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_damage_helper.h>
18 #include <drm/drm_drv.h>
19 #include <drm/drm_fb_cma_helper.h>
20 #include <drm/drm_fb_helper.h>
21 #include <drm/drm_format_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, 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 	struct dma_buf_attachment *import_attach = cma_obj->base.import_attach;
96 	void *src = cma_obj->vaddr;
97 	int ret = 0;
98 
99 	if (import_attach) {
100 		ret = dma_buf_begin_cpu_access(import_attach->dmabuf,
101 					       DMA_FROM_DEVICE);
102 		if (ret)
103 			return ret;
104 	}
105 
106 	st7586_xrgb8888_to_gray332(dst, src, fb, clip);
107 
108 	if (import_attach)
109 		ret = dma_buf_end_cpu_access(import_attach->dmabuf,
110 					     DMA_FROM_DEVICE);
111 
112 	return ret;
113 }
114 
115 static void st7586_fb_dirty(struct drm_framebuffer *fb, struct drm_rect *rect)
116 {
117 	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(fb->dev);
118 	struct mipi_dbi *dbi = &dbidev->dbi;
119 	int start, end, idx, ret = 0;
120 
121 	if (!dbidev->enabled)
122 		return;
123 
124 	if (!drm_dev_enter(fb->dev, &idx))
125 		return;
126 
127 	/* 3 pixels per byte, so grow clip to nearest multiple of 3 */
128 	rect->x1 = rounddown(rect->x1, 3);
129 	rect->x2 = roundup(rect->x2, 3);
130 
131 	DRM_DEBUG_KMS("Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect));
132 
133 	ret = st7586_buf_copy(dbidev->tx_buf, fb, rect);
134 	if (ret)
135 		goto err_msg;
136 
137 	/* Pixels are packed 3 per byte */
138 	start = rect->x1 / 3;
139 	end = rect->x2 / 3;
140 
141 	mipi_dbi_command(dbi, MIPI_DCS_SET_COLUMN_ADDRESS,
142 			 (start >> 8) & 0xFF, start & 0xFF,
143 			 (end >> 8) & 0xFF, (end - 1) & 0xFF);
144 	mipi_dbi_command(dbi, MIPI_DCS_SET_PAGE_ADDRESS,
145 			 (rect->y1 >> 8) & 0xFF, rect->y1 & 0xFF,
146 			 (rect->y2 >> 8) & 0xFF, (rect->y2 - 1) & 0xFF);
147 
148 	ret = mipi_dbi_command_buf(dbi, MIPI_DCS_WRITE_MEMORY_START,
149 				   (u8 *)dbidev->tx_buf,
150 				   (end - start) * (rect->y2 - rect->y1));
151 err_msg:
152 	if (ret)
153 		dev_err_once(fb->dev->dev, "Failed to update display %d\n", ret);
154 
155 	drm_dev_exit(idx);
156 }
157 
158 static void st7586_pipe_update(struct drm_simple_display_pipe *pipe,
159 			       struct drm_plane_state *old_state)
160 {
161 	struct drm_plane_state *state = pipe->plane.state;
162 	struct drm_rect rect;
163 
164 	if (drm_atomic_helper_damage_merged(old_state, state, &rect))
165 		st7586_fb_dirty(state->fb, &rect);
166 }
167 
168 static void st7586_pipe_enable(struct drm_simple_display_pipe *pipe,
169 			       struct drm_crtc_state *crtc_state,
170 			       struct drm_plane_state *plane_state)
171 {
172 	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
173 	struct drm_framebuffer *fb = plane_state->fb;
174 	struct mipi_dbi *dbi = &dbidev->dbi;
175 	struct drm_rect rect = {
176 		.x1 = 0,
177 		.x2 = fb->width,
178 		.y1 = 0,
179 		.y2 = fb->height,
180 	};
181 	int idx, ret;
182 	u8 addr_mode;
183 
184 	if (!drm_dev_enter(pipe->crtc.dev, &idx))
185 		return;
186 
187 	DRM_DEBUG_KMS("\n");
188 
189 	ret = mipi_dbi_poweron_reset(dbidev);
190 	if (ret)
191 		goto out_exit;
192 
193 	mipi_dbi_command(dbi, ST7586_AUTO_READ_CTRL, 0x9f);
194 	mipi_dbi_command(dbi, ST7586_OTP_RW_CTRL, 0x00);
195 
196 	msleep(10);
197 
198 	mipi_dbi_command(dbi, ST7586_OTP_READ);
199 
200 	msleep(20);
201 
202 	mipi_dbi_command(dbi, ST7586_OTP_CTRL_OUT);
203 	mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
204 	mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_OFF);
205 
206 	msleep(50);
207 
208 	mipi_dbi_command(dbi, ST7586_SET_VOP_OFFSET, 0x00);
209 	mipi_dbi_command(dbi, ST7586_SET_VOP, 0xe3, 0x00);
210 	mipi_dbi_command(dbi, ST7586_SET_BIAS_SYSTEM, 0x02);
211 	mipi_dbi_command(dbi, ST7586_SET_BOOST_LEVEL, 0x04);
212 	mipi_dbi_command(dbi, ST7586_ENABLE_ANALOG, 0x1d);
213 	mipi_dbi_command(dbi, ST7586_SET_NLINE_INV, 0x00);
214 	mipi_dbi_command(dbi, ST7586_DISP_MODE_GRAY);
215 	mipi_dbi_command(dbi, ST7586_ENABLE_DDRAM, 0x02);
216 
217 	switch (dbidev->rotation) {
218 	default:
219 		addr_mode = 0x00;
220 		break;
221 	case 90:
222 		addr_mode = ST7586_DISP_CTRL_MY;
223 		break;
224 	case 180:
225 		addr_mode = ST7586_DISP_CTRL_MX | ST7586_DISP_CTRL_MY;
226 		break;
227 	case 270:
228 		addr_mode = ST7586_DISP_CTRL_MX;
229 		break;
230 	}
231 	mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
232 
233 	mipi_dbi_command(dbi, ST7586_SET_DISP_DUTY, 0x7f);
234 	mipi_dbi_command(dbi, ST7586_SET_PART_DISP, 0xa0);
235 	mipi_dbi_command(dbi, MIPI_DCS_SET_PARTIAL_ROWS, 0x00, 0x00, 0x00, 0x77);
236 	mipi_dbi_command(dbi, MIPI_DCS_EXIT_INVERT_MODE);
237 
238 	msleep(100);
239 
240 	dbidev->enabled = true;
241 	st7586_fb_dirty(fb, &rect);
242 
243 	mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
244 out_exit:
245 	drm_dev_exit(idx);
246 }
247 
248 static void st7586_pipe_disable(struct drm_simple_display_pipe *pipe)
249 {
250 	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
251 
252 	/*
253 	 * This callback is not protected by drm_dev_enter/exit since we want to
254 	 * turn off the display on regular driver unload. It's highly unlikely
255 	 * that the underlying SPI controller is gone should this be called after
256 	 * unplug.
257 	 */
258 
259 	DRM_DEBUG_KMS("\n");
260 
261 	if (!dbidev->enabled)
262 		return;
263 
264 	mipi_dbi_command(&dbidev->dbi, MIPI_DCS_SET_DISPLAY_OFF);
265 	dbidev->enabled = false;
266 }
267 
268 static const u32 st7586_formats[] = {
269 	DRM_FORMAT_XRGB8888,
270 };
271 
272 static const struct drm_simple_display_pipe_funcs st7586_pipe_funcs = {
273 	.enable		= st7586_pipe_enable,
274 	.disable	= st7586_pipe_disable,
275 	.update		= st7586_pipe_update,
276 	.prepare_fb	= drm_gem_fb_simple_display_pipe_prepare_fb,
277 };
278 
279 static const struct drm_display_mode st7586_mode = {
280 	DRM_SIMPLE_MODE(178, 128, 37, 27),
281 };
282 
283 DEFINE_DRM_GEM_CMA_FOPS(st7586_fops);
284 
285 static struct drm_driver st7586_driver = {
286 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
287 	.fops			= &st7586_fops,
288 	DRM_GEM_CMA_VMAP_DRIVER_OPS,
289 	.debugfs_init		= mipi_dbi_debugfs_init,
290 	.name			= "st7586",
291 	.desc			= "Sitronix ST7586",
292 	.date			= "20170801",
293 	.major			= 1,
294 	.minor			= 0,
295 };
296 
297 static const struct of_device_id st7586_of_match[] = {
298 	{ .compatible = "lego,ev3-lcd" },
299 	{},
300 };
301 MODULE_DEVICE_TABLE(of, st7586_of_match);
302 
303 static const struct spi_device_id st7586_id[] = {
304 	{ "ev3-lcd", 0 },
305 	{ },
306 };
307 MODULE_DEVICE_TABLE(spi, st7586_id);
308 
309 static int st7586_probe(struct spi_device *spi)
310 {
311 	struct device *dev = &spi->dev;
312 	struct mipi_dbi_dev *dbidev;
313 	struct drm_device *drm;
314 	struct mipi_dbi *dbi;
315 	struct gpio_desc *a0;
316 	u32 rotation = 0;
317 	size_t bufsize;
318 	int ret;
319 
320 	dbidev = devm_drm_dev_alloc(dev, &st7586_driver,
321 				    struct mipi_dbi_dev, drm);
322 	if (IS_ERR(dbidev))
323 		return PTR_ERR(dbidev);
324 
325 	dbi = &dbidev->dbi;
326 	drm = &dbidev->drm;
327 
328 	bufsize = (st7586_mode.vdisplay + 2) / 3 * st7586_mode.hdisplay;
329 
330 	dbi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
331 	if (IS_ERR(dbi->reset)) {
332 		DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
333 		return PTR_ERR(dbi->reset);
334 	}
335 
336 	a0 = devm_gpiod_get(dev, "a0", GPIOD_OUT_LOW);
337 	if (IS_ERR(a0)) {
338 		DRM_DEV_ERROR(dev, "Failed to get gpio 'a0'\n");
339 		return PTR_ERR(a0);
340 	}
341 
342 	device_property_read_u32(dev, "rotation", &rotation);
343 
344 	ret = mipi_dbi_spi_init(spi, dbi, a0);
345 	if (ret)
346 		return ret;
347 
348 	/* Cannot read from this controller via SPI */
349 	dbi->read_commands = NULL;
350 
351 	ret = mipi_dbi_dev_init_with_formats(dbidev, &st7586_pipe_funcs,
352 					     st7586_formats, ARRAY_SIZE(st7586_formats),
353 					     &st7586_mode, rotation, bufsize);
354 	if (ret)
355 		return ret;
356 
357 	/*
358 	 * we are using 8-bit data, so we are not actually swapping anything,
359 	 * but setting mipi->swap_bytes makes mipi_dbi_typec3_command() do the
360 	 * right thing and not use 16-bit transfers (which results in swapped
361 	 * bytes on little-endian systems and causes out of order data to be
362 	 * sent to the display).
363 	 */
364 	dbi->swap_bytes = true;
365 
366 	drm_mode_config_reset(drm);
367 
368 	ret = drm_dev_register(drm, 0);
369 	if (ret)
370 		return ret;
371 
372 	spi_set_drvdata(spi, drm);
373 
374 	drm_fbdev_generic_setup(drm, 0);
375 
376 	return 0;
377 }
378 
379 static int st7586_remove(struct spi_device *spi)
380 {
381 	struct drm_device *drm = spi_get_drvdata(spi);
382 
383 	drm_dev_unplug(drm);
384 	drm_atomic_helper_shutdown(drm);
385 
386 	return 0;
387 }
388 
389 static void st7586_shutdown(struct spi_device *spi)
390 {
391 	drm_atomic_helper_shutdown(spi_get_drvdata(spi));
392 }
393 
394 static struct spi_driver st7586_spi_driver = {
395 	.driver = {
396 		.name = "st7586",
397 		.owner = THIS_MODULE,
398 		.of_match_table = st7586_of_match,
399 	},
400 	.id_table = st7586_id,
401 	.probe = st7586_probe,
402 	.remove = st7586_remove,
403 	.shutdown = st7586_shutdown,
404 };
405 module_spi_driver(st7586_spi_driver);
406 
407 MODULE_DESCRIPTION("Sitronix ST7586 DRM driver");
408 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
409 MODULE_LICENSE("GPL");
410