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