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