Merge branch 'next-tpm' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[sfrench/cifs-2.6.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_fb_cma_helper.h>
21 #include <drm/drm_gem_cma_helper.h>
22 #include <drm/drm_gem_framebuffer_helper.h>
23 #include <drm/tinydrm/mipi-dbi.h>
24 #include <drm/tinydrm/tinydrm-helpers.h>
25
26 /* controller-specific commands */
27 #define ST7586_DISP_MODE_GRAY   0x38
28 #define ST7586_DISP_MODE_MONO   0x39
29 #define ST7586_ENABLE_DDRAM     0x3a
30 #define ST7586_SET_DISP_DUTY    0xb0
31 #define ST7586_SET_PART_DISP    0xb4
32 #define ST7586_SET_NLINE_INV    0xb5
33 #define ST7586_SET_VOP          0xc0
34 #define ST7586_SET_BIAS_SYSTEM  0xc3
35 #define ST7586_SET_BOOST_LEVEL  0xc4
36 #define ST7586_SET_VOP_OFFSET   0xc7
37 #define ST7586_ENABLE_ANALOG    0xd0
38 #define ST7586_AUTO_READ_CTRL   0xd7
39 #define ST7586_OTP_RW_CTRL      0xe0
40 #define ST7586_OTP_CTRL_OUT     0xe1
41 #define ST7586_OTP_READ         0xe3
42
43 #define ST7586_DISP_CTRL_MX     BIT(6)
44 #define ST7586_DISP_CTRL_MY     BIT(7)
45
46 /*
47  * The ST7586 controller has an unusual pixel format where 2bpp grayscale is
48  * packed 3 pixels per byte with the first two pixels using 3 bits and the 3rd
49  * pixel using only 2 bits.
50  *
51  * |  D7  |  D6  |  D5  ||      |      || 2bpp |
52  * | (D4) | (D3) | (D2) ||  D1  |  D0  || GRAY |
53  * +------+------+------++------+------++------+
54  * |  1   |  1   |  1   ||  1   |  1   || 0  0 | black
55  * |  1   |  0   |  0   ||  1   |  0   || 0  1 | dark gray
56  * |  0   |  1   |  0   ||  0   |  1   || 1  0 | light gray
57  * |  0   |  0   |  0   ||  0   |  0   || 1  1 | white
58  */
59
60 static const u8 st7586_lookup[] = { 0x7, 0x4, 0x2, 0x0 };
61
62 static void st7586_xrgb8888_to_gray332(u8 *dst, void *vaddr,
63                                        struct drm_framebuffer *fb,
64                                        struct drm_clip_rect *clip)
65 {
66         size_t len = (clip->x2 - clip->x1) * (clip->y2 - clip->y1);
67         unsigned int x, y;
68         u8 *src, *buf, val;
69
70         buf = kmalloc(len, GFP_KERNEL);
71         if (!buf)
72                 return;
73
74         tinydrm_xrgb8888_to_gray8(buf, vaddr, fb, clip);
75         src = buf;
76
77         for (y = clip->y1; y < clip->y2; y++) {
78                 for (x = clip->x1; x < clip->x2; x += 3) {
79                         val = st7586_lookup[*src++ >> 6] << 5;
80                         val |= st7586_lookup[*src++ >> 6] << 2;
81                         val |= st7586_lookup[*src++ >> 6] >> 1;
82                         *dst++ = val;
83                 }
84         }
85
86         kfree(buf);
87 }
88
89 static int st7586_buf_copy(void *dst, struct drm_framebuffer *fb,
90                            struct drm_clip_rect *clip)
91 {
92         struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
93         struct dma_buf_attachment *import_attach = cma_obj->base.import_attach;
94         void *src = cma_obj->vaddr;
95         int ret = 0;
96
97         if (import_attach) {
98                 ret = dma_buf_begin_cpu_access(import_attach->dmabuf,
99                                                DMA_FROM_DEVICE);
100                 if (ret)
101                         return ret;
102         }
103
104         st7586_xrgb8888_to_gray332(dst, src, fb, clip);
105
106         if (import_attach)
107                 ret = dma_buf_end_cpu_access(import_attach->dmabuf,
108                                              DMA_FROM_DEVICE);
109
110         return ret;
111 }
112
113 static int st7586_fb_dirty(struct drm_framebuffer *fb,
114                            struct drm_file *file_priv, unsigned int flags,
115                            unsigned int color, struct drm_clip_rect *clips,
116                            unsigned int num_clips)
117 {
118         struct tinydrm_device *tdev = fb->dev->dev_private;
119         struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
120         struct drm_clip_rect clip;
121         int start, end;
122         int ret = 0;
123
124         if (!mipi->enabled)
125                 return 0;
126
127         tinydrm_merge_clips(&clip, clips, num_clips, flags, fb->width,
128                             fb->height);
129
130         /* 3 pixels per byte, so grow clip to nearest multiple of 3 */
131         clip.x1 = rounddown(clip.x1, 3);
132         clip.x2 = roundup(clip.x2, 3);
133
134         DRM_DEBUG("Flushing [FB:%d] x1=%u, x2=%u, y1=%u, y2=%u\n", fb->base.id,
135                   clip.x1, clip.x2, clip.y1, clip.y2);
136
137         ret = st7586_buf_copy(mipi->tx_buf, fb, &clip);
138         if (ret)
139                 return ret;
140
141         /* Pixels are packed 3 per byte */
142         start = clip.x1 / 3;
143         end = clip.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                          (clip.y1 >> 8) & 0xFF, clip.y1 & 0xFF,
150                          (clip.y2 >> 8) & 0xFF, (clip.y2 - 1) & 0xFF);
151
152         ret = mipi_dbi_command_buf(mipi, MIPI_DCS_WRITE_MEMORY_START,
153                                    (u8 *)mipi->tx_buf,
154                                    (end - start) * (clip.y2 - clip.y1));
155
156         return ret;
157 }
158
159 static const struct drm_framebuffer_funcs st7586_fb_funcs = {
160         .destroy        = drm_gem_fb_destroy,
161         .create_handle  = drm_gem_fb_create_handle,
162         .dirty          = tinydrm_fb_dirty,
163 };
164
165 static void st7586_pipe_enable(struct drm_simple_display_pipe *pipe,
166                                struct drm_crtc_state *crtc_state,
167                                struct drm_plane_state *plane_state)
168 {
169         struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
170         struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
171         int ret;
172         u8 addr_mode;
173
174         DRM_DEBUG_KMS("\n");
175
176         ret = mipi_dbi_poweron_reset(mipi);
177         if (ret)
178                 return;
179
180         mipi_dbi_command(mipi, ST7586_AUTO_READ_CTRL, 0x9f);
181         mipi_dbi_command(mipi, ST7586_OTP_RW_CTRL, 0x00);
182
183         msleep(10);
184
185         mipi_dbi_command(mipi, ST7586_OTP_READ);
186
187         msleep(20);
188
189         mipi_dbi_command(mipi, ST7586_OTP_CTRL_OUT);
190         mipi_dbi_command(mipi, MIPI_DCS_EXIT_SLEEP_MODE);
191         mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_OFF);
192
193         msleep(50);
194
195         mipi_dbi_command(mipi, ST7586_SET_VOP_OFFSET, 0x00);
196         mipi_dbi_command(mipi, ST7586_SET_VOP, 0xe3, 0x00);
197         mipi_dbi_command(mipi, ST7586_SET_BIAS_SYSTEM, 0x02);
198         mipi_dbi_command(mipi, ST7586_SET_BOOST_LEVEL, 0x04);
199         mipi_dbi_command(mipi, ST7586_ENABLE_ANALOG, 0x1d);
200         mipi_dbi_command(mipi, ST7586_SET_NLINE_INV, 0x00);
201         mipi_dbi_command(mipi, ST7586_DISP_MODE_GRAY);
202         mipi_dbi_command(mipi, ST7586_ENABLE_DDRAM, 0x02);
203
204         switch (mipi->rotation) {
205         default:
206                 addr_mode = 0x00;
207                 break;
208         case 90:
209                 addr_mode = ST7586_DISP_CTRL_MY;
210                 break;
211         case 180:
212                 addr_mode = ST7586_DISP_CTRL_MX | ST7586_DISP_CTRL_MY;
213                 break;
214         case 270:
215                 addr_mode = ST7586_DISP_CTRL_MX;
216                 break;
217         }
218         mipi_dbi_command(mipi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
219
220         mipi_dbi_command(mipi, ST7586_SET_DISP_DUTY, 0x7f);
221         mipi_dbi_command(mipi, ST7586_SET_PART_DISP, 0xa0);
222         mipi_dbi_command(mipi, MIPI_DCS_SET_PARTIAL_AREA, 0x00, 0x00, 0x00, 0x77);
223         mipi_dbi_command(mipi, MIPI_DCS_EXIT_INVERT_MODE);
224
225         msleep(100);
226
227         mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_ON);
228
229         mipi_dbi_enable_flush(mipi, crtc_state, plane_state);
230 }
231
232 static void st7586_pipe_disable(struct drm_simple_display_pipe *pipe)
233 {
234         struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
235         struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
236
237         DRM_DEBUG_KMS("\n");
238
239         if (!mipi->enabled)
240                 return;
241
242         mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_OFF);
243         mipi->enabled = false;
244 }
245
246 static const u32 st7586_formats[] = {
247         DRM_FORMAT_XRGB8888,
248 };
249
250 static int st7586_init(struct device *dev, struct mipi_dbi *mipi,
251                 const struct drm_simple_display_pipe_funcs *pipe_funcs,
252                 struct drm_driver *driver, const struct drm_display_mode *mode,
253                 unsigned int rotation)
254 {
255         size_t bufsize = (mode->vdisplay + 2) / 3 * mode->hdisplay;
256         struct tinydrm_device *tdev = &mipi->tinydrm;
257         int ret;
258
259         mutex_init(&mipi->cmdlock);
260
261         mipi->tx_buf = devm_kmalloc(dev, bufsize, GFP_KERNEL);
262         if (!mipi->tx_buf)
263                 return -ENOMEM;
264
265         ret = devm_tinydrm_init(dev, tdev, &st7586_fb_funcs, driver);
266         if (ret)
267                 return ret;
268
269         tdev->fb_dirty = st7586_fb_dirty;
270
271         ret = tinydrm_display_pipe_init(tdev, pipe_funcs,
272                                         DRM_MODE_CONNECTOR_VIRTUAL,
273                                         st7586_formats,
274                                         ARRAY_SIZE(st7586_formats),
275                                         mode, rotation);
276         if (ret)
277                 return ret;
278
279         tdev->drm->mode_config.preferred_depth = 32;
280         mipi->rotation = rotation;
281
282         drm_mode_config_reset(tdev->drm);
283
284         DRM_DEBUG_KMS("preferred_depth=%u, rotation = %u\n",
285                       tdev->drm->mode_config.preferred_depth, rotation);
286
287         return 0;
288 }
289
290 static const struct drm_simple_display_pipe_funcs st7586_pipe_funcs = {
291         .enable         = st7586_pipe_enable,
292         .disable        = st7586_pipe_disable,
293         .update         = tinydrm_display_pipe_update,
294         .prepare_fb     = drm_gem_fb_simple_display_pipe_prepare_fb,
295 };
296
297 static const struct drm_display_mode st7586_mode = {
298         TINYDRM_MODE(178, 128, 37, 27),
299 };
300
301 DEFINE_DRM_GEM_CMA_FOPS(st7586_fops);
302
303 static struct drm_driver st7586_driver = {
304         .driver_features        = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
305                                   DRIVER_ATOMIC,
306         .fops                   = &st7586_fops,
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 mipi_dbi *mipi;
332         struct gpio_desc *a0;
333         u32 rotation = 0;
334         int ret;
335
336         mipi = devm_kzalloc(dev, sizeof(*mipi), GFP_KERNEL);
337         if (!mipi)
338                 return -ENOMEM;
339
340         mipi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
341         if (IS_ERR(mipi->reset)) {
342                 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
343                 return PTR_ERR(mipi->reset);
344         }
345
346         a0 = devm_gpiod_get(dev, "a0", GPIOD_OUT_LOW);
347         if (IS_ERR(a0)) {
348                 DRM_DEV_ERROR(dev, "Failed to get gpio 'a0'\n");
349                 return PTR_ERR(a0);
350         }
351
352         device_property_read_u32(dev, "rotation", &rotation);
353
354         ret = mipi_dbi_spi_init(spi, mipi, a0);
355         if (ret)
356                 return ret;
357
358         /* Cannot read from this controller via SPI */
359         mipi->read_commands = NULL;
360
361         /*
362          * we are using 8-bit data, so we are not actually swapping anything,
363          * but setting mipi->swap_bytes makes mipi_dbi_typec3_command() do the
364          * right thing and not use 16-bit transfers (which results in swapped
365          * bytes on little-endian systems and causes out of order data to be
366          * sent to the display).
367          */
368         mipi->swap_bytes = true;
369
370         ret = st7586_init(&spi->dev, mipi, &st7586_pipe_funcs, &st7586_driver,
371                           &st7586_mode, rotation);
372         if (ret)
373                 return ret;
374
375         spi_set_drvdata(spi, mipi);
376
377         return devm_tinydrm_register(&mipi->tinydrm);
378 }
379
380 static void st7586_shutdown(struct spi_device *spi)
381 {
382         struct mipi_dbi *mipi = spi_get_drvdata(spi);
383
384         tinydrm_shutdown(&mipi->tinydrm);
385 }
386
387 static struct spi_driver st7586_spi_driver = {
388         .driver = {
389                 .name = "st7586",
390                 .owner = THIS_MODULE,
391                 .of_match_table = st7586_of_match,
392         },
393         .id_table = st7586_id,
394         .probe = st7586_probe,
395         .shutdown = st7586_shutdown,
396 };
397 module_spi_driver(st7586_spi_driver);
398
399 MODULE_DESCRIPTION("Sitronix ST7586 DRM driver");
400 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
401 MODULE_LICENSE("GPL");