-
Notifications
You must be signed in to change notification settings - Fork 30
/
fl2000_drm.c
537 lines (430 loc) · 15.2 KB
/
fl2000_drm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// SPDX-License-Identifier: GPL-2.0
/*
* (C) Copyright 2012, Red Hat
* (C) Copyright 2018-2020, Artem Mygaiev
*/
#include "fl2000.h"
#define DRM_DRIVER_NAME "fl2000_drm"
#define DRM_DRIVER_DESC "USB-HDMI"
#define DRM_DRIVER_DATE "20181001"
#define DRM_DRIVER_MAJOR 0
#define DRM_DRIVER_MINOR 0
#define DRM_DRIVER_PATCHLEVEL 1
/* Maximum supported resolution, out-of-the-blue numbers */
#define FL20000_MAX_WIDTH 4000
#define FL20000_MAX_HEIGHT 4000
/* Force using 32-bit XRGB8888 on input for simplicity */
#define FL2000_FB_BPP 32
static const u32 fl2000_pixel_formats[] = {
DRM_FORMAT_XRGB8888,
};
/* Maximum pixel clock set to 500MHz. It is hard to get more or less precise PLL configuration for
* higher clock
*/
#define FL2000_MAX_PIXCLOCK 500000000
/* PLL computing precision is 6 digits after comma */
#define FL2000_PLL_PRECISION 1000000
/* Input xtal clock, Hz */
#define FL2000_XTAL 10000000 /* 10 MHz */
/* Internal vco clock min/max, Hz */
#define FL2000_VCOCLOCK_MIN 62500000 /* 62.5 MHz */
#define FL2000_VCOCLOCK_MAX 1000000000 /* 1GHz */
/* Maximum acceptable ppm error */
#define FL2000_PPM_ERR_MAX 500
/* Assume bulk transfers can use only 80% of USB bandwidth */
#define FL2000_BULK_BW_PERCENT 80
#define FL2000_BULK_BW_HIGH_SPEED (480000000ull * 100 / FL2000_BULK_BW_PERCENT / 8)
#define FL2000_BULK_BW_SUPER_SPEED (5000000000ull * 100 / FL2000_BULK_BW_PERCENT / 8)
#define FL2000_BULK_BW_SUPER_SPEED_PLUS (10000000000ull * 100 / FL2000_BULK_BW_PERCENT / 8)
static unsigned int fl2000_get_bytes_pix(enum usb_device_speed speed, unsigned int pixclock)
{
unsigned int bytes_pix;
unsigned int max_bw;
/* Calculate maximum bandwidth, bytes per second */
switch (speed) {
case USB_SPEED_HIGH:
max_bw = FL2000_BULK_BW_HIGH_SPEED;
break;
case USB_SPEED_SUPER:
max_bw = FL2000_BULK_BW_SUPER_SPEED;
break;
case USB_SPEED_SUPER_PLUS:
max_bw = FL2000_BULK_BW_SUPER_SPEED_PLUS;
break;
default:
return 0;
}
/* Maximum bytes per pixel with maximum bandwidth */
bytes_pix = max_bw / pixclock;
switch (bytes_pix) {
case 0: /* Not enough */
case 1: /* RGB 332 not supported*/
return 0;
case 2: /* RGB 565 */
case 3: /* RGB 888 */
break;
default:
bytes_pix = 3;
break;
}
return bytes_pix;
}
struct fl2000_drm_if {
struct usb_device *usb_dev;
struct drm_device drm;
struct drm_simple_display_pipe pipe;
struct fl2000_stream *stream;
struct fl2000_intr *intr;
};
DEFINE_DRM_GEM_DMA_FOPS(fl2000_drm_driver_fops);
static void fl2000_drm_release(struct drm_device *drm)
{
drm_atomic_helper_shutdown(drm);
drm_mode_config_cleanup(drm);
}
static struct drm_driver fl2000_drm_driver = {
.driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
.lastclose = drm_fb_helper_lastclose,
.ioctls = NULL,
.fops = &fl2000_drm_driver_fops,
.release = fl2000_drm_release,
DRM_GEM_DMA_DRIVER_OPS_VMAP,
.name = DRM_DRIVER_NAME,
.desc = DRM_DRIVER_DESC,
.date = DRM_DRIVER_DATE,
.major = DRM_DRIVER_MAJOR,
.minor = DRM_DRIVER_MINOR,
.patchlevel = DRM_DRIVER_PATCHLEVEL,
};
static const struct drm_mode_config_funcs fl2000_mode_config_funcs = {
.fb_create = drm_gem_fb_create_with_dirty,
.atomic_check = drm_atomic_helper_check,
.atomic_commit = drm_atomic_helper_commit,
};
/* Integer division compute of ppm error */
static u64 fl2000_pll_ppm_err(u64 clock_mil, u32 vco_clk, u32 divisor)
{
u64 pll_clk_mil = (u64)vco_clk * FL2000_PLL_PRECISION / divisor;
u64 pll_clk_err;
/* Not using abs() here to avoid possible overflow */
if (pll_clk_mil > clock_mil)
pll_clk_err = pll_clk_mil - clock_mil;
else
pll_clk_err = clock_mil - pll_clk_mil;
return pll_clk_err / (clock_mil / FL2000_PLL_PRECISION);
}
static inline u32 fl2000_pll_get_divisor(u64 clock_mil, u32 vco_clk, u64 *min_ppm_err)
{
static const u32 divisor_arr[] = {
2, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128
};
u32 best_divisor = 0;
/* Iterate over array */
for (int i = 0; i < ARRAY_SIZE(divisor_arr); i++) {
u32 divisor = divisor_arr[i];
u64 ppm_err = fl2000_pll_ppm_err(clock_mil, vco_clk, divisor);
if (ppm_err < *min_ppm_err) {
*min_ppm_err = ppm_err;
best_divisor = divisor;
}
}
return best_divisor;
}
/* Try to match pixel clock - find parameters with minimal PLL error */
static u64 fl2000_pll_calc(u64 clock_mil, struct fl2000_pll *pll, u32 *clock_calculated)
{
static const u32 prescaler_max = 2;
static const u32 multiplier_max = 128;
u64 min_ppm_err = (u64)(-1);
for (u32 prescaler = 1; prescaler <= prescaler_max; prescaler++)
for (u32 multiplier = 1; multiplier <= multiplier_max; multiplier++) {
/* Do not need precision here yet, no 10^6 multiply */
u32 vco_clk = FL2000_XTAL / prescaler * multiplier;
u32 divisor;
if (vco_clk < FL2000_VCOCLOCK_MIN || vco_clk > FL2000_VCOCLOCK_MAX)
continue;
divisor = fl2000_pll_get_divisor(clock_mil, vco_clk, &min_ppm_err);
if (divisor == 0)
continue;
pll->prescaler = prescaler;
pll->multiplier = multiplier;
pll->divisor = divisor;
if (vco_clk < 125000000)
pll->function = 0;
else if (vco_clk < 250000000)
pll->function = 1;
else if (vco_clk < 500000000)
pll->function = 2;
else
pll->function = 3;
*clock_calculated = vco_clk / divisor;
}
return min_ppm_err;
}
static int fl2000_mode_calc(const struct drm_display_mode *mode,
struct drm_display_mode *adjusted_mode, struct fl2000_pll *pll)
{
u64 ppm_err;
u32 clock_calculated;
u64 clock_mil_adjusted;
const u64 clock_mil = (u64)mode->clock * 1000 * FL2000_PLL_PRECISION;
const int max_h_adjustment = 10;
if (mode->clock * 1000 > FL2000_MAX_PIXCLOCK)
return -1;
/* Try to match pixel clock slightly adjusting htotal value, sequence is:
* 0, -1, 1, -2, 2, -3, 3, -3, 4, -4, 5, -5, ...
* Here, 's' is used for sign, 'm' is used for modulo, and 'd' is the adjustment value
*/
for (int m = 0, s = 0, d = 0; m <= max_h_adjustment * 2; m++, s = -s, d += m * s) {
/* Maximum pixel clock 1GHz, or 10^9Hz. Multiply by 10^6 we get 10^15Hz. Assume
* maximum htotal is 10000 pix (no way) we get 10^19 max value and using u64 which
* is 1.8*10^19 no overflow can occur. Assume all this was checked before
*/
clock_mil_adjusted = clock_mil * (mode->htotal + d) / mode->htotal;
/* To keep precision use clock multiplied by 10^6 */
ppm_err = fl2000_pll_calc(clock_mil_adjusted, pll, &clock_calculated);
/* Stop searching as soon as the first valid option found */
if (ppm_err < FL2000_PPM_ERR_MAX) {
if (adjusted_mode) {
drm_mode_copy(adjusted_mode, mode);
adjusted_mode->htotal += d;
adjusted_mode->clock = clock_calculated / 1000;
}
return 0;
}
}
/* Cannot find PLL configuration that satisfy requirements */
return -1;
}
static enum drm_mode_status fl2000_display_mode_valid(struct drm_simple_display_pipe *pipe,
const struct drm_display_mode *mode)
{
struct drm_device *drm = pipe->crtc.dev;
struct drm_display_mode adjusted_mode;
struct fl2000_pll pll;
struct fl2000_drm_if *drm_if = drm->dev_private;
struct usb_device *usb_dev = drm_if->usb_dev;
/* Get PLL configuration and check if mode adjustments needed */
if (fl2000_mode_calc(mode, &adjusted_mode, &pll))
return MODE_BAD;
if (fl2000_get_bytes_pix(usb_dev->speed, adjusted_mode.clock) == 0)
return MODE_BAD;
return MODE_OK;
}
static void fl2000_display_enable(struct drm_simple_display_pipe *pipe,
struct drm_crtc_state *cstate,
struct drm_plane_state *plane_state)
{
struct drm_crtc *crtc = &pipe->crtc;
struct drm_device *drm = pipe->crtc.dev;
struct fl2000_drm_if *drm_if = drm->dev_private;
/* TODO: check cstate/pstate? */
UNUSED(cstate);
UNUSED(plane_state);
fl2000_stream_enable(drm_if->stream);
drm_crtc_vblank_on(crtc);
}
static void fl2000_display_disable(struct drm_simple_display_pipe *pipe)
{
struct drm_crtc *crtc = &pipe->crtc;
struct drm_device *drm = pipe->crtc.dev;
struct fl2000_drm_if *drm_if = drm->dev_private;
fl2000_stream_disable(drm_if->stream);
drm_crtc_vblank_off(crtc);
}
static void fb2000_dirty(struct drm_framebuffer *fb, struct drm_rect *rect)
{
int ret;
int idx;
struct drm_device *drm = fb->dev;
struct fl2000_drm_if *drm_if = drm->dev_private;
struct drm_gem_dma_object *dma_obj = drm_fb_dma_get_gem_obj(fb, 0);
UNUSED(rect);
if (!drm_dev_enter(fb->dev, &idx)) {
dev_err(drm->dev, "DRM enter failed!");
return;
}
ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
if (ret)
return;
fl2000_stream_compress(drm_if->stream, dma_obj->vaddr, fb->height, fb->width,
fb->pitches[0]);
drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
drm_dev_exit(idx);
}
static void fl2000_display_update(struct drm_simple_display_pipe *pipe,
struct drm_plane_state *old_state)
{
struct drm_crtc *crtc = &pipe->crtc;
struct drm_device *drm = crtc->dev;
struct drm_pending_vblank_event *event = crtc->state->event;
struct drm_plane_state *state = pipe->plane.state;
struct drm_rect rect;
if (drm_atomic_helper_damage_merged(old_state, state, &rect))
fb2000_dirty(state->fb, &rect);
if (event) {
crtc->state->event = NULL;
spin_lock_irq(&drm->event_lock);
if (crtc->state->active && drm_crtc_vblank_get(crtc) == 0)
drm_crtc_arm_vblank_event(crtc, event);
else
drm_crtc_send_vblank_event(crtc, event);
spin_unlock_irq(&drm->event_lock);
}
}
/* Logical pipe management (no HW configuration here) */
static const struct drm_simple_display_pipe_funcs fl2000_display_funcs = {
.mode_valid = fl2000_display_mode_valid,
.enable = fl2000_display_enable,
.disable = fl2000_display_disable,
.update = fl2000_display_update
};
static void fl2000_output_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
struct drm_display_mode *adjusted_mode)
{
struct drm_device *drm = encoder->dev;
struct fl2000_drm_if *drm_if = drm->dev_private;
struct usb_device *usb_dev = drm_if->usb_dev;
struct fl2000_timings timings;
struct fl2000_pll pll;
unsigned int bytes_pix;
/* Get PLL configuration and cehc if mode adjustments needed */
if (fl2000_mode_calc(mode, adjusted_mode, &pll))
return;
/* Check how many bytes per pixel shall be used with adjusted clock */
bytes_pix = fl2000_get_bytes_pix(usb_dev->speed, adjusted_mode->clock);
if (!bytes_pix)
return;
dev_info(drm->dev, "Mode requested: " DRM_MODE_FMT, DRM_MODE_ARG(mode));
dev_info(drm->dev, "Mode configured: " DRM_MODE_FMT, DRM_MODE_ARG(adjusted_mode));
/* Prepare timing configuration */
timings.hactive = adjusted_mode->hdisplay;
timings.htotal = adjusted_mode->htotal;
timings.hsync_width = adjusted_mode->hsync_end - adjusted_mode->hsync_start;
timings.hstart = adjusted_mode->htotal - adjusted_mode->hsync_start + 1;
timings.vactive = adjusted_mode->vdisplay;
timings.vtotal = adjusted_mode->vtotal;
timings.vsync_width = adjusted_mode->vsync_end - adjusted_mode->vsync_start;
timings.vstart = adjusted_mode->vtotal - adjusted_mode->vsync_start + 1;
/* Set PLL settings */
fl2000_set_pll(usb_dev, &pll);
/* Reset FL2000 & confirm PLL settings */
fl2000_reset(usb_dev);
/* Set timings settings */
fl2000_set_timings(usb_dev, &timings);
/* Pixel format according to number of bytes per pixel */
fl2000_set_pixfmt(usb_dev, bytes_pix);
/* Configure frame transfers */
fl2000_set_transfers(usb_dev);
/* Enable interrupts */
fl2000_enable_interrupts(usb_dev);
fl2000_afe_magic(usb_dev);
fl2000_stream_mode_set(drm_if->stream, mode->hdisplay * mode->vdisplay, bytes_pix);
}
/* FL2000 HW control functions: mode configuration, turn on/off */
static const struct drm_encoder_helper_funcs fl2000_encoder_funcs = {
.mode_set = fl2000_output_mode_set,
};
static void fl2000_drm_if_release(struct device *dev, void *res)
{
struct fl2000_drm_if *drm_if = res;
struct drm_device *drm = &drm_if->drm;
struct usb_device *usb_dev = drm_if->usb_dev;
dev_info(dev, "Unbinding FL2000 master");
/* Detach bridge */
component_unbind_all(dev, drm);
/* Start streaming interface */
fl2000_stream_destroy(usb_dev);
/* Start interrupts interface */
fl2000_intr_destroy(usb_dev);
/* Prepare to DRM device shutdown */
drm_kms_helper_poll_fini(drm);
drm_dev_unplug(drm);
drm_dev_put(drm);
}
/* TODO: release on errors! */
int fl2000_drm_bind(struct device *master)
{
int ret = 0;
struct usb_device *usb_dev = to_usb_device(master->parent);
struct fl2000_drm_if *drm_if;
struct drm_device *drm;
struct drm_mode_config *mode_config;
u64 dma_mask;
dev_info(master, "Binding FL2000 master");
drm_if = devm_drm_dev_alloc(master, &fl2000_drm_driver, struct fl2000_drm_if, drm);
if (IS_ERR(drm_if)) {
dev_err(master, "Cannot allocate DRM structure (%ld)", PTR_ERR(drm_if));
return (int)PTR_ERR(drm_if);
}
drm = &drm_if->drm;
drm_if->usb_dev = usb_dev;
drm->dev_private = drm_if;
ret = drmm_mode_config_init(drm);
if (ret) {
dev_err(master, "Cannot initialize DRM mode (%d)", ret);
return ret;
}
mode_config = &drm->mode_config;
mode_config->funcs = &fl2000_mode_config_funcs;
mode_config->min_width = 1;
mode_config->max_width = FL20000_MAX_WIDTH;
mode_config->min_height = 1;
mode_config->max_height = FL20000_MAX_HEIGHT;
/* Set DMA mask for DRM device from mask of the 'parent' USB device */
dma_mask = dma_get_mask(&usb_dev->dev);
ret = dma_set_coherent_mask(drm->dev, dma_mask);
if (ret) {
dev_err(drm->dev, "Cannot set DRM device DMA mask (%d)", ret);
return ret;
}
ret = drm_simple_display_pipe_init(drm, &drm_if->pipe, &fl2000_display_funcs,
fl2000_pixel_formats, ARRAY_SIZE(fl2000_pixel_formats),
NULL, NULL);
if (ret) {
dev_err(drm->dev, "Cannot configure simple display pipe (%d)", ret);
return ret;
}
/* Register 'mode_set' function to operate prior to bridge */
drm_encoder_helper_add(&drm_if->pipe.encoder, &fl2000_encoder_funcs);
/* Start streaming interface */
drm_if->stream = fl2000_stream_create(usb_dev, &drm_if->pipe.crtc);
/* Start interrupts interface */
drm_if->intr = fl2000_intr_create(usb_dev, drm);
/* Attach bridge */
ret = component_bind_all(master, &drm_if->pipe);
if (ret) {
dev_err(drm->dev, "Cannot attach bridge (%d)", ret);
return ret;
}
drm_mode_config_reset(drm);
ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
if (ret) {
dev_err(drm->dev, "Failed to initialize %d VBLANK(s) (%d)",
drm->mode_config.num_crtc, ret);
return ret;
}
drm_kms_helper_poll_init(drm);
drm_plane_enable_fb_damage_clips(&drm_if->pipe.plane);
ret = drm_dev_register(drm, 0);
if (ret) {
dev_err(drm->dev, "Cannot register DRM device (%d)", ret);
return ret;
}
fl2000_reset(usb_dev);
fl2000_usb_magic(usb_dev);
drm_fbdev_generic_setup(drm, FL2000_FB_BPP);
return 0;
}
void fl2000_drm_unbind(struct device *master)
{
struct usb_device *usb_dev = to_usb_device(master->parent);
devres_release(&usb_dev->dev, fl2000_drm_if_release, NULL, NULL);
}