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