blob: 9fd4423c8e7047a2498a08bcc154c1249ec7003f [file] [log] [blame]
David Lechnereac99d42017-08-07 12:39:39 -05001/*
2 * DRM driver for Sitronix ST7586 panels
3 *
4 * Copyright 2017 David Lechner <david@lechnology.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/delay.h>
13#include <linux/dma-buf.h>
14#include <linux/gpio/consumer.h>
15#include <linux/module.h>
16#include <linux/property.h>
17#include <linux/spi/spi.h>
18#include <video/mipi_display.h>
19
Noralf Trønnesd3820952017-12-08 20:37:42 +010020#include <drm/drm_fb_helper.h>
Noralf Trønnescce1a872017-09-24 14:26:16 +020021#include <drm/drm_gem_framebuffer_helper.h>
David Lechnereac99d42017-08-07 12:39:39 -050022#include <drm/tinydrm/mipi-dbi.h>
23#include <drm/tinydrm/tinydrm-helpers.h>
24
25/* controller-specific commands */
26#define ST7586_DISP_MODE_GRAY 0x38
27#define ST7586_DISP_MODE_MONO 0x39
28#define ST7586_ENABLE_DDRAM 0x3a
29#define ST7586_SET_DISP_DUTY 0xb0
30#define ST7586_SET_PART_DISP 0xb4
31#define ST7586_SET_NLINE_INV 0xb5
32#define ST7586_SET_VOP 0xc0
33#define ST7586_SET_BIAS_SYSTEM 0xc3
34#define ST7586_SET_BOOST_LEVEL 0xc4
35#define ST7586_SET_VOP_OFFSET 0xc7
36#define ST7586_ENABLE_ANALOG 0xd0
37#define ST7586_AUTO_READ_CTRL 0xd7
38#define ST7586_OTP_RW_CTRL 0xe0
39#define ST7586_OTP_CTRL_OUT 0xe1
40#define ST7586_OTP_READ 0xe3
41
42#define ST7586_DISP_CTRL_MX BIT(6)
43#define ST7586_DISP_CTRL_MY BIT(7)
44
45/*
46 * The ST7586 controller has an unusual pixel format where 2bpp grayscale is
47 * packed 3 pixels per byte with the first two pixels using 3 bits and the 3rd
48 * pixel using only 2 bits.
49 *
50 * | D7 | D6 | D5 || | || 2bpp |
51 * | (D4) | (D3) | (D2) || D1 | D0 || GRAY |
52 * +------+------+------++------+------++------+
53 * | 1 | 1 | 1 || 1 | 1 || 0 0 | black
54 * | 1 | 0 | 0 || 1 | 0 || 0 1 | dark gray
55 * | 0 | 1 | 0 || 0 | 1 || 1 0 | light gray
56 * | 0 | 0 | 0 || 0 | 0 || 1 1 | white
57 */
58
59static const u8 st7586_lookup[] = { 0x7, 0x4, 0x2, 0x0 };
60
61static void st7586_xrgb8888_to_gray332(u8 *dst, void *vaddr,
62 struct drm_framebuffer *fb,
63 struct drm_clip_rect *clip)
64{
65 size_t len = (clip->x2 - clip->x1) * (clip->y2 - clip->y1);
66 unsigned int x, y;
67 u8 *src, *buf, val;
68
69 buf = kmalloc(len, GFP_KERNEL);
70 if (!buf)
71 return;
72
73 tinydrm_xrgb8888_to_gray8(buf, vaddr, fb, clip);
74 src = buf;
75
76 for (y = clip->y1; y < clip->y2; y++) {
77 for (x = clip->x1; x < clip->x2; x += 3) {
78 val = st7586_lookup[*src++ >> 6] << 5;
79 val |= st7586_lookup[*src++ >> 6] << 2;
80 val |= st7586_lookup[*src++ >> 6] >> 1;
81 *dst++ = val;
82 }
83 }
84
85 kfree(buf);
86}
87
88static int st7586_buf_copy(void *dst, struct drm_framebuffer *fb,
89 struct drm_clip_rect *clip)
90{
91 struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
92 struct dma_buf_attachment *import_attach = cma_obj->base.import_attach;
93 void *src = cma_obj->vaddr;
94 int ret = 0;
95
96 if (import_attach) {
97 ret = dma_buf_begin_cpu_access(import_attach->dmabuf,
98 DMA_FROM_DEVICE);
99 if (ret)
100 return ret;
101 }
102
103 st7586_xrgb8888_to_gray332(dst, src, fb, clip);
104
105 if (import_attach)
106 ret = dma_buf_end_cpu_access(import_attach->dmabuf,
107 DMA_FROM_DEVICE);
108
109 return ret;
110}
111
112static int st7586_fb_dirty(struct drm_framebuffer *fb,
113 struct drm_file *file_priv, unsigned int flags,
114 unsigned int color, struct drm_clip_rect *clips,
115 unsigned int num_clips)
116{
117 struct tinydrm_device *tdev = fb->dev->dev_private;
118 struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
119 struct drm_clip_rect clip;
120 int start, end;
121 int ret = 0;
122
123 mutex_lock(&tdev->dirty_lock);
124
125 if (!mipi->enabled)
126 goto out_unlock;
127
128 /* fbdev can flush even when we're not interested */
129 if (tdev->pipe.plane.fb != fb)
130 goto out_unlock;
131
132 tinydrm_merge_clips(&clip, clips, num_clips, flags, fb->width,
133 fb->height);
134
135 /* 3 pixels per byte, so grow clip to nearest multiple of 3 */
136 clip.x1 = rounddown(clip.x1, 3);
137 clip.x2 = roundup(clip.x2, 3);
138
139 DRM_DEBUG("Flushing [FB:%d] x1=%u, x2=%u, y1=%u, y2=%u\n", fb->base.id,
140 clip.x1, clip.x2, clip.y1, clip.y2);
141
142 ret = st7586_buf_copy(mipi->tx_buf, fb, &clip);
143 if (ret)
144 goto out_unlock;
145
146 /* Pixels are packed 3 per byte */
147 start = clip.x1 / 3;
148 end = clip.x2 / 3;
149
150 mipi_dbi_command(mipi, MIPI_DCS_SET_COLUMN_ADDRESS,
151 (start >> 8) & 0xFF, start & 0xFF,
152 (end >> 8) & 0xFF, (end - 1) & 0xFF);
153 mipi_dbi_command(mipi, MIPI_DCS_SET_PAGE_ADDRESS,
154 (clip.y1 >> 8) & 0xFF, clip.y1 & 0xFF,
155 (clip.y2 >> 8) & 0xFF, (clip.y2 - 1) & 0xFF);
156
157 ret = mipi_dbi_command_buf(mipi, MIPI_DCS_WRITE_MEMORY_START,
158 (u8 *)mipi->tx_buf,
159 (end - start) * (clip.y2 - clip.y1));
160
161out_unlock:
162 mutex_unlock(&tdev->dirty_lock);
163
164 if (ret)
165 dev_err_once(fb->dev->dev, "Failed to update display %d\n",
166 ret);
167
168 return ret;
169}
170
171static const struct drm_framebuffer_funcs st7586_fb_funcs = {
Noralf Trønnescce1a872017-09-24 14:26:16 +0200172 .destroy = drm_gem_fb_destroy,
173 .create_handle = drm_gem_fb_create_handle,
David Lechnereac99d42017-08-07 12:39:39 -0500174 .dirty = st7586_fb_dirty,
175};
176
Colin Ian Kingf3bbc902017-08-16 10:23:06 +0100177static void st7586_pipe_enable(struct drm_simple_display_pipe *pipe,
178 struct drm_crtc_state *crtc_state)
David Lechnereac99d42017-08-07 12:39:39 -0500179{
180 struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
181 struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
David Lechnereac99d42017-08-07 12:39:39 -0500182 struct device *dev = tdev->drm->dev;
183 int ret;
184 u8 addr_mode;
185
186 DRM_DEBUG_KMS("\n");
187
188 mipi_dbi_hw_reset(mipi);
189 ret = mipi_dbi_command(mipi, ST7586_AUTO_READ_CTRL, 0x9f);
190 if (ret) {
Harsha Sharmae43e8182017-10-07 03:47:38 +0530191 DRM_DEV_ERROR(dev, "Error sending command %d\n", ret);
David Lechnereac99d42017-08-07 12:39:39 -0500192 return;
193 }
194
195 mipi_dbi_command(mipi, ST7586_OTP_RW_CTRL, 0x00);
196
197 msleep(10);
198
199 mipi_dbi_command(mipi, ST7586_OTP_READ);
200
201 msleep(20);
202
203 mipi_dbi_command(mipi, ST7586_OTP_CTRL_OUT);
204 mipi_dbi_command(mipi, MIPI_DCS_EXIT_SLEEP_MODE);
205 mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_OFF);
206
207 msleep(50);
208
209 mipi_dbi_command(mipi, ST7586_SET_VOP_OFFSET, 0x00);
210 mipi_dbi_command(mipi, ST7586_SET_VOP, 0xe3, 0x00);
211 mipi_dbi_command(mipi, ST7586_SET_BIAS_SYSTEM, 0x02);
212 mipi_dbi_command(mipi, ST7586_SET_BOOST_LEVEL, 0x04);
213 mipi_dbi_command(mipi, ST7586_ENABLE_ANALOG, 0x1d);
214 mipi_dbi_command(mipi, ST7586_SET_NLINE_INV, 0x00);
215 mipi_dbi_command(mipi, ST7586_DISP_MODE_GRAY);
216 mipi_dbi_command(mipi, ST7586_ENABLE_DDRAM, 0x02);
217
218 switch (mipi->rotation) {
219 default:
220 addr_mode = 0x00;
221 break;
222 case 90:
223 addr_mode = ST7586_DISP_CTRL_MY;
224 break;
225 case 180:
226 addr_mode = ST7586_DISP_CTRL_MX | ST7586_DISP_CTRL_MY;
227 break;
228 case 270:
229 addr_mode = ST7586_DISP_CTRL_MX;
230 break;
231 }
232 mipi_dbi_command(mipi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
233
234 mipi_dbi_command(mipi, ST7586_SET_DISP_DUTY, 0x7f);
235 mipi_dbi_command(mipi, ST7586_SET_PART_DISP, 0xa0);
236 mipi_dbi_command(mipi, MIPI_DCS_SET_PARTIAL_AREA, 0x00, 0x00, 0x00, 0x77);
237 mipi_dbi_command(mipi, MIPI_DCS_EXIT_INVERT_MODE);
238
239 msleep(100);
240
241 mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_ON);
242
Noralf Trønnes22edc8d2018-01-10 19:59:36 +0100243 mipi_dbi_enable_flush(mipi);
David Lechnereac99d42017-08-07 12:39:39 -0500244}
245
246static void st7586_pipe_disable(struct drm_simple_display_pipe *pipe)
247{
248 struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
249 struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
250
251 DRM_DEBUG_KMS("\n");
252
253 if (!mipi->enabled)
254 return;
255
256 mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_OFF);
257 mipi->enabled = false;
258}
259
260static const u32 st7586_formats[] = {
261 DRM_FORMAT_XRGB8888,
262};
263
264static int st7586_init(struct device *dev, struct mipi_dbi *mipi,
265 const struct drm_simple_display_pipe_funcs *pipe_funcs,
266 struct drm_driver *driver, const struct drm_display_mode *mode,
267 unsigned int rotation)
268{
269 size_t bufsize = (mode->vdisplay + 2) / 3 * mode->hdisplay;
270 struct tinydrm_device *tdev = &mipi->tinydrm;
271 int ret;
272
273 mutex_init(&mipi->cmdlock);
274
275 mipi->tx_buf = devm_kmalloc(dev, bufsize, GFP_KERNEL);
276 if (!mipi->tx_buf)
277 return -ENOMEM;
278
279 ret = devm_tinydrm_init(dev, tdev, &st7586_fb_funcs, driver);
280 if (ret)
281 return ret;
282
283 ret = tinydrm_display_pipe_init(tdev, pipe_funcs,
284 DRM_MODE_CONNECTOR_VIRTUAL,
285 st7586_formats,
286 ARRAY_SIZE(st7586_formats),
287 mode, rotation);
288 if (ret)
289 return ret;
290
291 tdev->drm->mode_config.preferred_depth = 32;
292 mipi->rotation = rotation;
293
294 drm_mode_config_reset(tdev->drm);
295
296 DRM_DEBUG_KMS("preferred_depth=%u, rotation = %u\n",
297 tdev->drm->mode_config.preferred_depth, rotation);
298
299 return 0;
300}
301
302static const struct drm_simple_display_pipe_funcs st7586_pipe_funcs = {
303 .enable = st7586_pipe_enable,
304 .disable = st7586_pipe_disable,
305 .update = tinydrm_display_pipe_update,
306 .prepare_fb = tinydrm_display_pipe_prepare_fb,
307};
308
309static const struct drm_display_mode st7586_mode = {
310 TINYDRM_MODE(178, 128, 37, 27),
311};
312
313DEFINE_DRM_GEM_CMA_FOPS(st7586_fops);
314
315static struct drm_driver st7586_driver = {
316 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
317 DRIVER_ATOMIC,
318 .fops = &st7586_fops,
319 TINYDRM_GEM_DRIVER_OPS,
Noralf Trønnesd3820952017-12-08 20:37:42 +0100320 .lastclose = drm_fb_helper_lastclose,
David Lechnereac99d42017-08-07 12:39:39 -0500321 .debugfs_init = mipi_dbi_debugfs_init,
322 .name = "st7586",
323 .desc = "Sitronix ST7586",
324 .date = "20170801",
325 .major = 1,
326 .minor = 0,
327};
328
329static const struct of_device_id st7586_of_match[] = {
330 { .compatible = "lego,ev3-lcd" },
331 {},
332};
333MODULE_DEVICE_TABLE(of, st7586_of_match);
334
335static const struct spi_device_id st7586_id[] = {
336 { "ev3-lcd", 0 },
337 { },
338};
339MODULE_DEVICE_TABLE(spi, st7586_id);
340
341static int st7586_probe(struct spi_device *spi)
342{
343 struct device *dev = &spi->dev;
David Lechnereac99d42017-08-07 12:39:39 -0500344 struct mipi_dbi *mipi;
345 struct gpio_desc *a0;
346 u32 rotation = 0;
347 int ret;
348
349 mipi = devm_kzalloc(dev, sizeof(*mipi), GFP_KERNEL);
350 if (!mipi)
351 return -ENOMEM;
352
353 mipi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
354 if (IS_ERR(mipi->reset)) {
Harsha Sharmae43e8182017-10-07 03:47:38 +0530355 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
David Lechnereac99d42017-08-07 12:39:39 -0500356 return PTR_ERR(mipi->reset);
357 }
358
359 a0 = devm_gpiod_get(dev, "a0", GPIOD_OUT_LOW);
360 if (IS_ERR(a0)) {
Harsha Sharmae43e8182017-10-07 03:47:38 +0530361 DRM_DEV_ERROR(dev, "Failed to get gpio 'a0'\n");
David Lechnereac99d42017-08-07 12:39:39 -0500362 return PTR_ERR(a0);
363 }
364
365 device_property_read_u32(dev, "rotation", &rotation);
366
367 ret = mipi_dbi_spi_init(spi, mipi, a0);
368 if (ret)
369 return ret;
370
371 /* Cannot read from this controller via SPI */
372 mipi->read_commands = NULL;
373
374 /*
375 * we are using 8-bit data, so we are not actually swapping anything,
376 * but setting mipi->swap_bytes makes mipi_dbi_typec3_command() do the
377 * right thing and not use 16-bit transfers (which results in swapped
378 * bytes on little-endian systems and causes out of order data to be
379 * sent to the display).
380 */
381 mipi->swap_bytes = true;
382
383 ret = st7586_init(&spi->dev, mipi, &st7586_pipe_funcs, &st7586_driver,
384 &st7586_mode, rotation);
385 if (ret)
386 return ret;
387
David Lechnereac99d42017-08-07 12:39:39 -0500388 spi_set_drvdata(spi, mipi);
389
Noralf Trønnes95a0cfe2017-09-08 17:07:26 +0200390 return devm_tinydrm_register(&mipi->tinydrm);
David Lechnereac99d42017-08-07 12:39:39 -0500391}
392
393static void st7586_shutdown(struct spi_device *spi)
394{
395 struct mipi_dbi *mipi = spi_get_drvdata(spi);
396
397 tinydrm_shutdown(&mipi->tinydrm);
398}
399
400static 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 .shutdown = st7586_shutdown,
409};
410module_spi_driver(st7586_spi_driver);
411
412MODULE_DESCRIPTION("Sitronix ST7586 DRM driver");
413MODULE_AUTHOR("David Lechner <david@lechnology.com>");
414MODULE_LICENSE("GPL");