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