1 /*
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
20 #include <drm/drm_fb_helper.h>
21 #include <drm/drm_gem_framebuffer_helper.h>
22 #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
59 static const u8 st7586_lookup[] = { 0x7, 0x4, 0x2, 0x0 };
60
st7586_xrgb8888_to_gray332(u8 * dst,void * vaddr,struct drm_framebuffer * fb,struct drm_clip_rect * clip)61 static 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
st7586_buf_copy(void * dst,struct drm_framebuffer * fb,struct drm_clip_rect * clip)88 static 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
st7586_fb_dirty(struct drm_framebuffer * fb,struct drm_file * file_priv,unsigned int flags,unsigned int color,struct drm_clip_rect * clips,unsigned int num_clips)112 static 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 if (!mipi->enabled)
124 return 0;
125
126 tinydrm_merge_clips(&clip, clips, num_clips, flags, fb->width,
127 fb->height);
128
129 /* 3 pixels per byte, so grow clip to nearest multiple of 3 */
130 clip.x1 = rounddown(clip.x1, 3);
131 clip.x2 = roundup(clip.x2, 3);
132
133 DRM_DEBUG("Flushing [FB:%d] x1=%u, x2=%u, y1=%u, y2=%u\n", fb->base.id,
134 clip.x1, clip.x2, clip.y1, clip.y2);
135
136 ret = st7586_buf_copy(mipi->tx_buf, fb, &clip);
137 if (ret)
138 return ret;
139
140 /* Pixels are packed 3 per byte */
141 start = clip.x1 / 3;
142 end = clip.x2 / 3;
143
144 mipi_dbi_command(mipi, MIPI_DCS_SET_COLUMN_ADDRESS,
145 (start >> 8) & 0xFF, start & 0xFF,
146 (end >> 8) & 0xFF, (end - 1) & 0xFF);
147 mipi_dbi_command(mipi, MIPI_DCS_SET_PAGE_ADDRESS,
148 (clip.y1 >> 8) & 0xFF, clip.y1 & 0xFF,
149 (clip.y2 >> 8) & 0xFF, (clip.y2 - 1) & 0xFF);
150
151 ret = mipi_dbi_command_buf(mipi, MIPI_DCS_WRITE_MEMORY_START,
152 (u8 *)mipi->tx_buf,
153 (end - start) * (clip.y2 - clip.y1));
154
155 return ret;
156 }
157
158 static const struct drm_framebuffer_funcs st7586_fb_funcs = {
159 .destroy = drm_gem_fb_destroy,
160 .create_handle = drm_gem_fb_create_handle,
161 .dirty = tinydrm_fb_dirty,
162 };
163
st7586_pipe_enable(struct drm_simple_display_pipe * pipe,struct drm_crtc_state * crtc_state,struct drm_plane_state * plane_state)164 static void st7586_pipe_enable(struct drm_simple_display_pipe *pipe,
165 struct drm_crtc_state *crtc_state,
166 struct drm_plane_state *plane_state)
167 {
168 struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
169 struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
170 int ret;
171 u8 addr_mode;
172
173 DRM_DEBUG_KMS("\n");
174
175 ret = mipi_dbi_poweron_reset(mipi);
176 if (ret)
177 return;
178
179 mipi_dbi_command(mipi, ST7586_AUTO_READ_CTRL, 0x9f);
180 mipi_dbi_command(mipi, ST7586_OTP_RW_CTRL, 0x00);
181
182 msleep(10);
183
184 mipi_dbi_command(mipi, ST7586_OTP_READ);
185
186 msleep(20);
187
188 mipi_dbi_command(mipi, ST7586_OTP_CTRL_OUT);
189 mipi_dbi_command(mipi, MIPI_DCS_EXIT_SLEEP_MODE);
190 mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_OFF);
191
192 msleep(50);
193
194 mipi_dbi_command(mipi, ST7586_SET_VOP_OFFSET, 0x00);
195 mipi_dbi_command(mipi, ST7586_SET_VOP, 0xe3, 0x00);
196 mipi_dbi_command(mipi, ST7586_SET_BIAS_SYSTEM, 0x02);
197 mipi_dbi_command(mipi, ST7586_SET_BOOST_LEVEL, 0x04);
198 mipi_dbi_command(mipi, ST7586_ENABLE_ANALOG, 0x1d);
199 mipi_dbi_command(mipi, ST7586_SET_NLINE_INV, 0x00);
200 mipi_dbi_command(mipi, ST7586_DISP_MODE_GRAY);
201 mipi_dbi_command(mipi, ST7586_ENABLE_DDRAM, 0x02);
202
203 switch (mipi->rotation) {
204 default:
205 addr_mode = 0x00;
206 break;
207 case 90:
208 addr_mode = ST7586_DISP_CTRL_MY;
209 break;
210 case 180:
211 addr_mode = ST7586_DISP_CTRL_MX | ST7586_DISP_CTRL_MY;
212 break;
213 case 270:
214 addr_mode = ST7586_DISP_CTRL_MX;
215 break;
216 }
217 mipi_dbi_command(mipi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
218
219 mipi_dbi_command(mipi, ST7586_SET_DISP_DUTY, 0x7f);
220 mipi_dbi_command(mipi, ST7586_SET_PART_DISP, 0xa0);
221 mipi_dbi_command(mipi, MIPI_DCS_SET_PARTIAL_AREA, 0x00, 0x00, 0x00, 0x77);
222 mipi_dbi_command(mipi, MIPI_DCS_EXIT_INVERT_MODE);
223
224 msleep(100);
225
226 mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_ON);
227
228 mipi_dbi_enable_flush(mipi, crtc_state, plane_state);
229 }
230
st7586_pipe_disable(struct drm_simple_display_pipe * pipe)231 static void st7586_pipe_disable(struct drm_simple_display_pipe *pipe)
232 {
233 struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
234 struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
235
236 DRM_DEBUG_KMS("\n");
237
238 if (!mipi->enabled)
239 return;
240
241 mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_OFF);
242 mipi->enabled = false;
243 }
244
245 static const u32 st7586_formats[] = {
246 DRM_FORMAT_XRGB8888,
247 };
248
st7586_init(struct device * dev,struct mipi_dbi * mipi,const struct drm_simple_display_pipe_funcs * pipe_funcs,struct drm_driver * driver,const struct drm_display_mode * mode,unsigned int rotation)249 static int st7586_init(struct device *dev, struct mipi_dbi *mipi,
250 const struct drm_simple_display_pipe_funcs *pipe_funcs,
251 struct drm_driver *driver, const struct drm_display_mode *mode,
252 unsigned int rotation)
253 {
254 size_t bufsize = (mode->vdisplay + 2) / 3 * mode->hdisplay;
255 struct tinydrm_device *tdev = &mipi->tinydrm;
256 int ret;
257
258 mutex_init(&mipi->cmdlock);
259
260 mipi->tx_buf = devm_kmalloc(dev, bufsize, GFP_KERNEL);
261 if (!mipi->tx_buf)
262 return -ENOMEM;
263
264 ret = devm_tinydrm_init(dev, tdev, &st7586_fb_funcs, driver);
265 if (ret)
266 return ret;
267
268 tdev->fb_dirty = st7586_fb_dirty;
269
270 ret = tinydrm_display_pipe_init(tdev, pipe_funcs,
271 DRM_MODE_CONNECTOR_VIRTUAL,
272 st7586_formats,
273 ARRAY_SIZE(st7586_formats),
274 mode, rotation);
275 if (ret)
276 return ret;
277
278 tdev->drm->mode_config.preferred_depth = 32;
279 mipi->rotation = rotation;
280
281 drm_mode_config_reset(tdev->drm);
282
283 DRM_DEBUG_KMS("preferred_depth=%u, rotation = %u\n",
284 tdev->drm->mode_config.preferred_depth, rotation);
285
286 return 0;
287 }
288
289 static const struct drm_simple_display_pipe_funcs st7586_pipe_funcs = {
290 .enable = st7586_pipe_enable,
291 .disable = st7586_pipe_disable,
292 .update = tinydrm_display_pipe_update,
293 .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
294 };
295
296 static const struct drm_display_mode st7586_mode = {
297 TINYDRM_MODE(178, 128, 37, 27),
298 };
299
300 DEFINE_DRM_GEM_CMA_FOPS(st7586_fops);
301
302 static struct drm_driver st7586_driver = {
303 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
304 DRIVER_ATOMIC,
305 .fops = &st7586_fops,
306 TINYDRM_GEM_DRIVER_OPS,
307 .debugfs_init = mipi_dbi_debugfs_init,
308 .name = "st7586",
309 .desc = "Sitronix ST7586",
310 .date = "20170801",
311 .major = 1,
312 .minor = 0,
313 };
314
315 static const struct of_device_id st7586_of_match[] = {
316 { .compatible = "lego,ev3-lcd" },
317 {},
318 };
319 MODULE_DEVICE_TABLE(of, st7586_of_match);
320
321 static const struct spi_device_id st7586_id[] = {
322 { "ev3-lcd", 0 },
323 { },
324 };
325 MODULE_DEVICE_TABLE(spi, st7586_id);
326
st7586_probe(struct spi_device * spi)327 static int st7586_probe(struct spi_device *spi)
328 {
329 struct device *dev = &spi->dev;
330 struct mipi_dbi *mipi;
331 struct gpio_desc *a0;
332 u32 rotation = 0;
333 int ret;
334
335 mipi = devm_kzalloc(dev, sizeof(*mipi), GFP_KERNEL);
336 if (!mipi)
337 return -ENOMEM;
338
339 mipi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
340 if (IS_ERR(mipi->reset)) {
341 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
342 return PTR_ERR(mipi->reset);
343 }
344
345 a0 = devm_gpiod_get(dev, "a0", GPIOD_OUT_LOW);
346 if (IS_ERR(a0)) {
347 DRM_DEV_ERROR(dev, "Failed to get gpio 'a0'\n");
348 return PTR_ERR(a0);
349 }
350
351 device_property_read_u32(dev, "rotation", &rotation);
352
353 ret = mipi_dbi_spi_init(spi, mipi, a0);
354 if (ret)
355 return ret;
356
357 /* Cannot read from this controller via SPI */
358 mipi->read_commands = NULL;
359
360 /*
361 * we are using 8-bit data, so we are not actually swapping anything,
362 * but setting mipi->swap_bytes makes mipi_dbi_typec3_command() do the
363 * right thing and not use 16-bit transfers (which results in swapped
364 * bytes on little-endian systems and causes out of order data to be
365 * sent to the display).
366 */
367 mipi->swap_bytes = true;
368
369 ret = st7586_init(&spi->dev, mipi, &st7586_pipe_funcs, &st7586_driver,
370 &st7586_mode, rotation);
371 if (ret)
372 return ret;
373
374 spi_set_drvdata(spi, mipi);
375
376 return devm_tinydrm_register(&mipi->tinydrm);
377 }
378
st7586_shutdown(struct spi_device * spi)379 static void st7586_shutdown(struct spi_device *spi)
380 {
381 struct mipi_dbi *mipi = spi_get_drvdata(spi);
382
383 tinydrm_shutdown(&mipi->tinydrm);
384 }
385
386 static struct spi_driver st7586_spi_driver = {
387 .driver = {
388 .name = "st7586",
389 .owner = THIS_MODULE,
390 .of_match_table = st7586_of_match,
391 },
392 .id_table = st7586_id,
393 .probe = st7586_probe,
394 .shutdown = st7586_shutdown,
395 };
396 module_spi_driver(st7586_spi_driver);
397
398 MODULE_DESCRIPTION("Sitronix ST7586 DRM driver");
399 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
400 MODULE_LICENSE("GPL");
401