-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdc_kms.c
431 lines (348 loc) · 10.1 KB
/
cdc_kms.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
/*
* cdc_kms.c -- CDC Display Controller Mode Setting
*
* Copyright (C) 2017 TES Electronic Solutions GmbH
* Author: Christian Thaler <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <linux/clk.h>
#include <linux/mutex.h>
#include <drm/drmP.h>
#include <drm/drm_crtc.h>
#include <drm/drm_crtc_helper.h>
#include <drm/drm_atomic.h>
#include <drm/drm_atomic_helper.h>
#include <drm/drm_fb_cma_helper.h>
#include <drm/drm_gem_cma_helper.h>
#include <video/display_timing.h>
#include <video/of_display_timing.h>
#include <video/videomode.h>
#include <linux/of_graph.h>
#include <linux/wait.h>
#include "cdc_regs.h"
#include "cdc_drv.h"
#include "cdc_kms.h"
#include "cdc_crtc.h"
#include "cdc_plane.h"
#include "cdc_encoder.h"
/*******************************************************************************
* Format helper
*
* Note that the format id is configuration dependent!
*/
static const struct cdc_format cdc_formats[] = {
{ 0, DRM_FORMAT_ARGB8888, 32 },
{ 0, DRM_FORMAT_XRGB8888, 32 },
{ 1, DRM_FORMAT_RGB888, 24 },
{ 2, DRM_FORMAT_RGB565, 16 },
{ 3, DRM_FORMAT_ARGB4444, 16 },
{ 4, DRM_FORMAT_ARGB1555, 16 },
};
const struct cdc_format *
cdc_format_info(__u32 drm_fourcc)
{
int i;
for (i = 0; i < ARRAY_SIZE(cdc_formats); ++i) {
if (cdc_formats[i].fourcc == drm_fourcc)
return &cdc_formats[i];
}
return NULL;
}
static struct drm_framebuffer *cdc_fb_create(struct drm_device *dev,
struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd)
{
struct drm_framebuffer *fb;
struct drm_gem_cma_object *gem;
const struct cdc_format *format;
dev_dbg(dev->dev, "creating frame buffer %dx%d (%08x)\n",
mode_cmd->width, mode_cmd->height, mode_cmd->pixel_format);
format = cdc_format_info(mode_cmd->pixel_format);
if (format == NULL) {
dev_err(dev->dev, "requested unsupported pixel format %08x\n",
mode_cmd->pixel_format);
return ERR_PTR(-EINVAL);
}
if (mode_cmd->pitches[0] >= CDC_MAX_PITCH) {
dev_err(dev->dev, "requested too large pitch of %u\n",
mode_cmd->pitches[0]);
return ERR_PTR(-EINVAL);
}
fb = drm_fb_cma_create(dev, file_priv, mode_cmd);
gem = drm_fb_cma_get_gem_obj(fb, 0);
dev_dbg(dev->dev, "FB addr is 0x%08x\n", gem->paddr);
return fb;
}
static void cdc_output_poll_changed(struct drm_device *dev)
{
struct cdc_device *cdc = dev->dev_private;
dev_dbg(dev->dev, "%s\n", __func__);
if (!cdc->fbdev)
cdc->early_poll = true;
else
drm_fbdev_cma_hotplug_event(cdc->fbdev);
}
static int cdc_atomic_check(struct drm_device *dev,
struct drm_atomic_state *state)
{
int ret;
dev_dbg(dev->dev, "%s\n", __func__);
ret = drm_atomic_helper_check(dev, state);
if (ret < 0)
return ret;
return 0;
}
struct cdc_commit {
struct work_struct work;
struct drm_device *dev;
struct drm_atomic_state *state;
u32 crtcs;
};
static void cdc_atomic_complete(struct cdc_commit *commit)
{
struct drm_device *dev = commit->dev;
struct cdc_device *cdc = dev->dev_private;
struct drm_atomic_state *old_state = commit->state;
dev_dbg(dev->dev, "%s\n", __func__);
/* Apply the atomic update. */
drm_atomic_helper_commit_modeset_disables(dev, old_state);
drm_atomic_helper_commit_modeset_enables(dev, old_state);
/* todo: maybe set DRM_PLANE_COMMIT_ACTIVE_ONLY flag here
* This will stop the KMS core from sending plane update notifications
* for a disabled CRTC. Check if we manually ignore plane updates in
* this case.
*/
drm_atomic_helper_commit_planes(dev, old_state, 0);
drm_atomic_helper_wait_for_vblanks(dev, old_state);
drm_atomic_helper_cleanup_planes(dev, old_state);
/* todo: check if we can make use of the reference counting anywhere
* Maybe use worker threads for the commit? See:
* https://lists.freedesktop.org/archives/intel-gfx-trybot/2016-September/008592.html
*/
drm_atomic_state_put(old_state);
/* Complete the commit, wake up any waiter. */
spin_lock(&cdc->commit.wait.lock);
cdc->commit.pending = 0;
wake_up_all_locked(&cdc->commit.wait);
spin_unlock(&cdc->commit.wait.lock);
kfree(commit);
}
static void cdc_atomic_work(struct work_struct *work)
{
struct cdc_commit
*commit = container_of(work, struct cdc_commit, work);
cdc_atomic_complete(commit);
}
static int cdc_atomic_commit(struct drm_device *dev,
struct drm_atomic_state *state, bool async)
{
struct cdc_device *cdc = dev->dev_private;
struct cdc_commit *commit;
int ret;
dev_dbg(dev->dev, "%s\n", __func__);
ret = drm_atomic_helper_prepare_planes(dev, state);
if (ret)
return ret;
/* Allocate the commit object. */
commit = kzalloc(sizeof(*commit), GFP_KERNEL);
if (commit == NULL)
return -ENOMEM;
INIT_WORK(&commit->work, cdc_atomic_work);
commit->dev = dev;
commit->state = state;
/* Wait until all affected CRTCs have completed previous commits and
* mark them as pending.
*/
if (state->crtcs[0].ptr)
commit->crtcs = 1;
spin_lock(&cdc->commit.wait.lock);
ret = wait_event_interruptible_locked(cdc->commit.wait,
!(cdc->commit.pending));
if (ret == 0)
cdc->commit.pending = 1;
spin_unlock(&cdc->commit.wait.lock);
if (ret) {
kfree(commit);
return ret;
}
/* Swap the state, this is the point of no return. */
drm_atomic_helper_swap_state(state, true);
drm_atomic_state_get(state);
if (async)
schedule_work(&commit->work);
else
cdc_atomic_complete(commit);
return 0;
}
static const struct drm_mode_config_funcs cdc_mode_config_funcs = {
.fb_create = cdc_fb_create,
.output_poll_changed = cdc_output_poll_changed,
.atomic_check = cdc_atomic_check,
.atomic_commit = cdc_atomic_commit,
};
static int cdc_encoders_find_and_init(struct cdc_device *cdc,
struct of_endpoint *ep)
{
static const struct {
const char *compatible;
__u32 type;
} encoders[] = {
{ "adi,adv7513", DRM_MODE_ENCODER_TMDS },
{ "lvds-encoder", DRM_MODE_ENCODER_LVDS },
};
__u32 enc_type = DRM_MODE_ENCODER_NONE;
struct device_node *connector = NULL;
struct device_node *encoder = NULL;
struct device_node *ep_node = NULL;
struct device_node *entity_ep_node;
struct device_node *entity;
int ret;
/*
* Locate the connected entity and infer its type from the number of
* endpoints.
*/
entity = of_graph_get_remote_port_parent(ep->local_node);
if (!entity) {
dev_err(cdc->dev, "unconnected endpoint %s, skipping\n",
ep->local_node->full_name);
return -ENODEV;
}
dev_dbg(cdc->dev, "endpoint is connected to %s\n", entity->full_name);
entity_ep_node = of_parse_phandle(ep->local_node, "remote-endpoint", 0);
for_each_endpoint_of_node(entity, ep_node)
{
if (ep_node == entity_ep_node)
continue;
/*
* We've found one endpoint other than the input, this must
* be an encoder. Locate the connector.
*/
encoder = entity;
connector = of_graph_get_remote_port_parent(ep_node);
of_node_put(ep_node);
if (!connector) {
dev_warn(cdc->dev,
"no connector for encoder %s, skipping\n",
encoder->full_name);
of_node_put(entity_ep_node);
of_node_put(encoder);
return -ENODEV;
}
break;
}
of_node_put(entity_ep_node);
if (encoder) {
unsigned int i;
dev_dbg(cdc->dev, "found encoder %s\n", encoder->full_name);
/*
* If an encoder has been found, get its type based on its
* compatible string.
*/
for (i = 0; i < ARRAY_SIZE(encoders); ++i) {
if (of_device_is_compatible(encoder,
encoders[i].compatible)) {
enc_type = encoders[i].type;
break;
}
}
if (i == ARRAY_SIZE(encoders)) {
dev_warn(cdc->dev,
"unknown encoder type for %s, skipping\n",
encoder->full_name);
of_node_put(encoder);
of_node_put(connector);
return -EINVAL;
}
} else {
/*
* If no encoder has been found the entity must be the
* connector.
*/
connector = entity;
}
ret = cdc_encoder_init(cdc, enc_type, encoder, connector);
if (ret && ret != -EPROBE_DEFER)
dev_warn(cdc->dev,
"failed to initialize encoder %s (%d), skipping\n",
encoder->full_name, ret);
of_node_put(encoder);
of_node_put(connector);
return ret;
}
static int cdc_encoders_init(struct cdc_device *cdc)
{
struct device_node *np = cdc->dev->of_node;
struct device_node *ep_node;
dev_dbg(cdc->dev, "initializing encoder for %s\n", np->full_name);
/*
* CDC only has one endpoint. Now create the encoder for it.
*/
for_each_endpoint_of_node(np, ep_node) {
struct of_endpoint ep;
int ret;
ret = of_graph_parse_endpoint(ep_node, &ep);
if (ret < 0) {
of_node_put(ep_node);
return ret;
}
/* Process output pipeline. */
ret = cdc_encoders_find_and_init(cdc, &ep);
if (ret < 0) {
if (ret == -EPROBE_DEFER) {
of_node_put(ep_node);
return ret;
}
continue;
}
}
return 0;
}
int cdc_modeset_init(struct cdc_device *cdc)
{
struct drm_device *dev = cdc->ddev;
struct drm_fbdev_cma *fbdev;
int ret;
dev_dbg(cdc->dev, "%s\n", __func__);
drm_mode_config_init(dev);
dev->mode_config.min_width = 0;
dev->mode_config.min_height = 0;
dev->mode_config.max_width = CDC_MAX_WIDTH;
dev->mode_config.max_height = CDC_MAX_HEIGHT;
dev->mode_config.funcs = &cdc_mode_config_funcs;
/* Initialize vertical blanking interrupts handling. Start with vblank
* disabled for all CRTCs.
*/
ret = drm_vblank_init(dev, 1 /* num_crtcs */);
if (ret < 0) {
dev_err(cdc->dev, "failed to initialize vblank\n");
return ret;
}
cdc_planes_init(cdc);
cdc_crtc_create(cdc);
cdc_encoders_init(cdc);
drm_mode_config_reset(dev);
drm_kms_helper_poll_init(dev);
if (dev->mode_config.num_connector) {
dev_dbg(cdc->dev, "Initializing FBDEV CMA...\n");
fbdev = drm_fbdev_cma_init(dev, 32, 1, 1);
dev_dbg(cdc->dev, "Finished FBDEV CMA init call\n");
if (IS_ERR(fbdev)) {
dev_err(cdc->dev,
"could not initialize fbdev cma...\n");
return PTR_ERR(fbdev);
}
cdc->fbdev = fbdev;
// handle a poll event that occured before FBDEV was ready
if (cdc->early_poll)
drm_fbdev_cma_hotplug_event(fbdev);
} else {
dev_err(cdc->dev,
"no connector found, disabling fbdev emulation\n");
}
dev_dbg(cdc->dev, "Added FB at 0x%08x\n",
cdc->ddev->mode_config.fb_base);
return 0;
}