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