1
2
3
4
5
6
7
8
9
10
11
12#include <linux/dma-buf.h>
13
14#include <drm/drm_atomic_helper.h>
15#include <drm/drm_crtc_helper.h>
16#include <drm/drm_damage_helper.h>
17#include <drm/drm_fourcc.h>
18#include <drm/drm_gem_framebuffer_helper.h>
19#include <drm/drm_gem_shmem_helper.h>
20#include <drm/drm_modeset_helper_vtables.h>
21#include <drm/drm_vblank.h>
22
23#include "udl_drv.h"
24
25#define UDL_COLOR_DEPTH_16BPP 0
26
27
28
29
30
31static char *udl_set_register(char *buf, u8 reg, u8 val)
32{
33 *buf++ = 0xAF;
34 *buf++ = 0x20;
35 *buf++ = reg;
36 *buf++ = val;
37 return buf;
38}
39
40static char *udl_vidreg_lock(char *buf)
41{
42 return udl_set_register(buf, 0xFF, 0x00);
43}
44
45static char *udl_vidreg_unlock(char *buf)
46{
47 return udl_set_register(buf, 0xFF, 0xFF);
48}
49
50static char *udl_set_blank_mode(char *buf, u8 mode)
51{
52 return udl_set_register(buf, UDL_REG_BLANK_MODE, mode);
53}
54
55static char *udl_set_color_depth(char *buf, u8 selection)
56{
57 return udl_set_register(buf, 0x00, selection);
58}
59
60static char *udl_set_base16bpp(char *wrptr, u32 base)
61{
62
63 wrptr = udl_set_register(wrptr, 0x20, base >> 16);
64 wrptr = udl_set_register(wrptr, 0x21, base >> 8);
65 return udl_set_register(wrptr, 0x22, base);
66}
67
68
69
70
71
72static char *udl_set_base8bpp(char *wrptr, u32 base)
73{
74 wrptr = udl_set_register(wrptr, 0x26, base >> 16);
75 wrptr = udl_set_register(wrptr, 0x27, base >> 8);
76 return udl_set_register(wrptr, 0x28, base);
77}
78
79static char *udl_set_register_16(char *wrptr, u8 reg, u16 value)
80{
81 wrptr = udl_set_register(wrptr, reg, value >> 8);
82 return udl_set_register(wrptr, reg+1, value);
83}
84
85
86
87
88
89static char *udl_set_register_16be(char *wrptr, u8 reg, u16 value)
90{
91 wrptr = udl_set_register(wrptr, reg, value);
92 return udl_set_register(wrptr, reg+1, value >> 8);
93}
94
95
96
97
98
99
100
101
102
103
104static u16 udl_lfsr16(u16 actual_count)
105{
106 u32 lv = 0xFFFF;
107
108 while (actual_count--) {
109 lv = ((lv << 1) |
110 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
111 & 0xFFFF;
112 }
113
114 return (u16) lv;
115}
116
117
118
119
120
121static char *udl_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
122{
123 return udl_set_register_16(wrptr, reg, udl_lfsr16(value));
124}
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148static char *udl_set_vid_cmds(char *wrptr, struct drm_display_mode *mode)
149{
150 u16 xds, yds;
151 u16 xde, yde;
152 u16 yec;
153
154
155 xds = mode->crtc_htotal - mode->crtc_hsync_start;
156 wrptr = udl_set_register_lfsr16(wrptr, 0x01, xds);
157
158 xde = xds + mode->crtc_hdisplay;
159 wrptr = udl_set_register_lfsr16(wrptr, 0x03, xde);
160
161
162 yds = mode->crtc_vtotal - mode->crtc_vsync_start;
163 wrptr = udl_set_register_lfsr16(wrptr, 0x05, yds);
164
165 yde = yds + mode->crtc_vdisplay;
166 wrptr = udl_set_register_lfsr16(wrptr, 0x07, yde);
167
168
169 wrptr = udl_set_register_lfsr16(wrptr, 0x09,
170 mode->crtc_htotal - 1);
171
172
173 wrptr = udl_set_register_lfsr16(wrptr, 0x0B, 1);
174
175
176 wrptr = udl_set_register_lfsr16(wrptr, 0x0D,
177 mode->crtc_hsync_end - mode->crtc_hsync_start + 1);
178
179
180 wrptr = udl_set_register_16(wrptr, 0x0F, mode->hdisplay);
181
182
183 yec = mode->crtc_vtotal;
184 wrptr = udl_set_register_lfsr16(wrptr, 0x11, yec);
185
186
187 wrptr = udl_set_register_lfsr16(wrptr, 0x13, 0);
188
189
190 wrptr = udl_set_register_lfsr16(wrptr, 0x15, mode->crtc_vsync_end - mode->crtc_vsync_start);
191
192
193 wrptr = udl_set_register_16(wrptr, 0x17, mode->crtc_vdisplay);
194
195 wrptr = udl_set_register_16be(wrptr, 0x1B,
196 mode->clock / 5);
197
198 return wrptr;
199}
200
201static char *udl_dummy_render(char *wrptr)
202{
203 *wrptr++ = 0xAF;
204 *wrptr++ = 0x6A;
205 *wrptr++ = 0x00;
206 *wrptr++ = 0x00;
207 *wrptr++ = 0x00;
208 *wrptr++ = 0x01;
209 *wrptr++ = 0x00;
210 *wrptr++ = 0x00;
211 *wrptr++ = 0x00;
212 return wrptr;
213}
214
215static int udl_crtc_write_mode_to_hw(struct drm_crtc *crtc)
216{
217 struct drm_device *dev = crtc->dev;
218 struct udl_device *udl = to_udl(dev);
219 struct urb *urb;
220 char *buf;
221 int retval;
222
223 if (udl->mode_buf_len == 0) {
224 DRM_ERROR("No mode set\n");
225 return -EINVAL;
226 }
227
228 urb = udl_get_urb(dev);
229 if (!urb)
230 return -ENOMEM;
231
232 buf = (char *)urb->transfer_buffer;
233
234 memcpy(buf, udl->mode_buf, udl->mode_buf_len);
235 retval = udl_submit_urb(dev, urb, udl->mode_buf_len);
236 DRM_DEBUG("write mode info %d\n", udl->mode_buf_len);
237 return retval;
238}
239
240static long udl_log_cpp(unsigned int cpp)
241{
242 if (WARN_ON(!is_power_of_2(cpp)))
243 return -EINVAL;
244 return __ffs(cpp);
245}
246
247static int udl_aligned_damage_clip(struct drm_rect *clip, int x, int y,
248 int width, int height)
249{
250 int x1, x2;
251
252 if (WARN_ON_ONCE(x < 0) ||
253 WARN_ON_ONCE(y < 0) ||
254 WARN_ON_ONCE(width < 0) ||
255 WARN_ON_ONCE(height < 0))
256 return -EINVAL;
257
258 x1 = ALIGN_DOWN(x, sizeof(unsigned long));
259 x2 = ALIGN(width + (x - x1), sizeof(unsigned long)) + x1;
260
261 clip->x1 = x1;
262 clip->y1 = y;
263 clip->x2 = x2;
264 clip->y2 = y + height;
265
266 return 0;
267}
268
269static int udl_handle_damage(struct drm_framebuffer *fb, int x, int y,
270 int width, int height)
271{
272 struct drm_device *dev = fb->dev;
273 struct dma_buf_attachment *import_attach = fb->obj[0]->import_attach;
274 int i, ret, tmp_ret;
275 char *cmd;
276 struct urb *urb;
277 struct drm_rect clip;
278 int log_bpp;
279 struct dma_buf_map map;
280 void *vaddr;
281
282 ret = udl_log_cpp(fb->format->cpp[0]);
283 if (ret < 0)
284 return ret;
285 log_bpp = ret;
286
287 ret = udl_aligned_damage_clip(&clip, x, y, width, height);
288 if (ret)
289 return ret;
290 else if ((clip.x2 > fb->width) || (clip.y2 > fb->height))
291 return -EINVAL;
292
293 if (import_attach) {
294 ret = dma_buf_begin_cpu_access(import_attach->dmabuf,
295 DMA_FROM_DEVICE);
296 if (ret)
297 return ret;
298 }
299
300 ret = drm_gem_shmem_vmap(fb->obj[0], &map);
301 if (ret) {
302 DRM_ERROR("failed to vmap fb\n");
303 goto out_dma_buf_end_cpu_access;
304 }
305 vaddr = map.vaddr;
306
307 urb = udl_get_urb(dev);
308 if (!urb) {
309 ret = -ENOMEM;
310 goto out_drm_gem_shmem_vunmap;
311 }
312 cmd = urb->transfer_buffer;
313
314 for (i = clip.y1; i < clip.y2; i++) {
315 const int line_offset = fb->pitches[0] * i;
316 const int byte_offset = line_offset + (clip.x1 << log_bpp);
317 const int dev_byte_offset = (fb->width * i + clip.x1) << log_bpp;
318 const int byte_width = (clip.x2 - clip.x1) << log_bpp;
319 ret = udl_render_hline(dev, log_bpp, &urb, (char *)vaddr,
320 &cmd, byte_offset, dev_byte_offset,
321 byte_width);
322 if (ret)
323 goto out_drm_gem_shmem_vunmap;
324 }
325
326 if (cmd > (char *)urb->transfer_buffer) {
327
328 int len;
329 if (cmd < (char *)urb->transfer_buffer + urb->transfer_buffer_length)
330 *cmd++ = 0xAF;
331 len = cmd - (char *)urb->transfer_buffer;
332 ret = udl_submit_urb(dev, urb, len);
333 } else {
334 udl_urb_completion(urb);
335 }
336
337 ret = 0;
338
339out_drm_gem_shmem_vunmap:
340 drm_gem_shmem_vunmap(fb->obj[0], &map);
341out_dma_buf_end_cpu_access:
342 if (import_attach) {
343 tmp_ret = dma_buf_end_cpu_access(import_attach->dmabuf,
344 DMA_FROM_DEVICE);
345 if (tmp_ret && !ret)
346 ret = tmp_ret;
347 }
348
349 return ret;
350}
351
352
353
354
355
356static const uint32_t udl_simple_display_pipe_formats[] = {
357 DRM_FORMAT_RGB565,
358 DRM_FORMAT_XRGB8888,
359};
360
361static enum drm_mode_status
362udl_simple_display_pipe_mode_valid(struct drm_simple_display_pipe *pipe,
363 const struct drm_display_mode *mode)
364{
365 return MODE_OK;
366}
367
368static void
369udl_simple_display_pipe_enable(struct drm_simple_display_pipe *pipe,
370 struct drm_crtc_state *crtc_state,
371 struct drm_plane_state *plane_state)
372{
373 struct drm_crtc *crtc = &pipe->crtc;
374 struct drm_device *dev = crtc->dev;
375 struct drm_framebuffer *fb = plane_state->fb;
376 struct udl_device *udl = to_udl(dev);
377 struct drm_display_mode *mode = &crtc_state->mode;
378 char *buf;
379 char *wrptr;
380 int color_depth = UDL_COLOR_DEPTH_16BPP;
381
382 buf = (char *)udl->mode_buf;
383
384
385
386
387
388 wrptr = udl_vidreg_lock(buf);
389 wrptr = udl_set_color_depth(wrptr, color_depth);
390
391 wrptr = udl_set_base16bpp(wrptr, 0);
392
393 wrptr = udl_set_base8bpp(wrptr, 2 * mode->vdisplay * mode->hdisplay);
394
395 wrptr = udl_set_vid_cmds(wrptr, mode);
396 wrptr = udl_set_blank_mode(wrptr, UDL_BLANK_MODE_ON);
397 wrptr = udl_vidreg_unlock(wrptr);
398
399 wrptr = udl_dummy_render(wrptr);
400
401 udl->mode_buf_len = wrptr - buf;
402
403 udl_handle_damage(fb, 0, 0, fb->width, fb->height);
404
405 if (!crtc_state->mode_changed)
406 return;
407
408
409 udl_crtc_write_mode_to_hw(crtc);
410}
411
412static void
413udl_simple_display_pipe_disable(struct drm_simple_display_pipe *pipe)
414{
415 struct drm_crtc *crtc = &pipe->crtc;
416 struct drm_device *dev = crtc->dev;
417 struct urb *urb;
418 char *buf;
419
420 urb = udl_get_urb(dev);
421 if (!urb)
422 return;
423
424 buf = (char *)urb->transfer_buffer;
425 buf = udl_vidreg_lock(buf);
426 buf = udl_set_blank_mode(buf, UDL_BLANK_MODE_POWERDOWN);
427 buf = udl_vidreg_unlock(buf);
428 buf = udl_dummy_render(buf);
429
430 udl_submit_urb(dev, urb, buf - (char *)urb->transfer_buffer);
431}
432
433static void
434udl_simple_display_pipe_update(struct drm_simple_display_pipe *pipe,
435 struct drm_plane_state *old_plane_state)
436{
437 struct drm_plane_state *state = pipe->plane.state;
438 struct drm_framebuffer *fb = state->fb;
439 struct drm_rect rect;
440
441 if (!fb)
442 return;
443
444 if (drm_atomic_helper_damage_merged(old_plane_state, state, &rect))
445 udl_handle_damage(fb, rect.x1, rect.y1, rect.x2 - rect.x1,
446 rect.y2 - rect.y1);
447}
448
449static const
450struct drm_simple_display_pipe_funcs udl_simple_display_pipe_funcs = {
451 .mode_valid = udl_simple_display_pipe_mode_valid,
452 .enable = udl_simple_display_pipe_enable,
453 .disable = udl_simple_display_pipe_disable,
454 .update = udl_simple_display_pipe_update,
455 .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
456};
457
458
459
460
461
462static const struct drm_mode_config_funcs udl_mode_funcs = {
463 .fb_create = drm_gem_fb_create_with_dirty,
464 .atomic_check = drm_atomic_helper_check,
465 .atomic_commit = drm_atomic_helper_commit,
466};
467
468int udl_modeset_init(struct drm_device *dev)
469{
470 size_t format_count = ARRAY_SIZE(udl_simple_display_pipe_formats);
471 struct udl_device *udl = to_udl(dev);
472 struct drm_connector *connector;
473 int ret;
474
475 ret = drmm_mode_config_init(dev);
476 if (ret)
477 return ret;
478
479 dev->mode_config.min_width = 640;
480 dev->mode_config.min_height = 480;
481
482 dev->mode_config.max_width = 2048;
483 dev->mode_config.max_height = 2048;
484
485 dev->mode_config.prefer_shadow = 0;
486 dev->mode_config.preferred_depth = 16;
487
488 dev->mode_config.funcs = &udl_mode_funcs;
489
490 connector = udl_connector_init(dev);
491 if (IS_ERR(connector))
492 return PTR_ERR(connector);
493
494 format_count = ARRAY_SIZE(udl_simple_display_pipe_formats);
495
496 ret = drm_simple_display_pipe_init(dev, &udl->display_pipe,
497 &udl_simple_display_pipe_funcs,
498 udl_simple_display_pipe_formats,
499 format_count, NULL, connector);
500 if (ret)
501 return ret;
502
503 drm_mode_config_reset(dev);
504
505 return 0;
506}
507