summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/tidss/tidss_encoder.c
blob: 17a86bed805481c6cf054f6cb3176d60faa0351c (plain)
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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
 */

#include <linux/export.h>

#include <drm/drm_atomic_helper.h>
#include <drm/drm_bridge.h>
#include <drm/drm_bridge_connector.h>
#include <drm/drm_crtc.h>
#include <drm/drm_modeset_helper_vtables.h>
#include <drm/drm_panel.h>
#include <drm/drm_of.h>
#include <drm/drm_simple_kms_helper.h>

#include "tidss_crtc.h"
#include "tidss_drv.h"
#include "tidss_encoder.h"

struct tidss_encoder {
	struct drm_bridge bridge;
	struct drm_encoder encoder;
	struct drm_connector *connector;
	struct drm_bridge *next_bridge;
	struct tidss_device *tidss;
};

static inline struct tidss_encoder
*bridge_to_tidss_encoder(struct drm_bridge *b)
{
	return container_of(b, struct tidss_encoder, bridge);
}

static int tidss_bridge_attach(struct drm_bridge *bridge,
			       enum drm_bridge_attach_flags flags)
{
	struct tidss_encoder *t_enc = bridge_to_tidss_encoder(bridge);

	return drm_bridge_attach(bridge->encoder, t_enc->next_bridge,
				 bridge, flags);
}

static int tidss_bridge_atomic_check(struct drm_bridge *bridge,
				     struct drm_bridge_state *bridge_state,
				     struct drm_crtc_state *crtc_state,
				     struct drm_connector_state *conn_state)
{
	struct tidss_encoder *t_enc = bridge_to_tidss_encoder(bridge);
	struct tidss_device *tidss = t_enc->tidss;
	struct tidss_crtc_state *tcrtc_state = to_tidss_crtc_state(crtc_state);
	struct drm_display_info *di = &conn_state->connector->display_info;
	struct drm_bridge_state *next_bridge_state = NULL;

	if (t_enc->next_bridge)
		next_bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
								    t_enc->next_bridge);

	if (next_bridge_state) {
		tcrtc_state->bus_flags = next_bridge_state->input_bus_cfg.flags;
		tcrtc_state->bus_format = next_bridge_state->input_bus_cfg.format;
	} else if (di->num_bus_formats) {
		tcrtc_state->bus_format = di->bus_formats[0];
		tcrtc_state->bus_flags = di->bus_flags;
	} else {
		dev_err(tidss->dev, "%s: No bus_formats in connected display\n",
			__func__);
		return -EINVAL;
	}

	return 0;
}

static const struct drm_bridge_funcs tidss_bridge_funcs = {
	.attach				= tidss_bridge_attach,
	.atomic_check			= tidss_bridge_atomic_check,
	.atomic_reset			= drm_atomic_helper_bridge_reset,
	.atomic_duplicate_state		= drm_atomic_helper_bridge_duplicate_state,
	.atomic_destroy_state		= drm_atomic_helper_bridge_destroy_state,
};

int tidss_encoder_create(struct tidss_device *tidss,
			 struct drm_bridge *next_bridge,
			 u32 encoder_type, u32 possible_crtcs)
{
	struct tidss_encoder *t_enc;
	struct drm_encoder *enc;
	struct drm_connector *connector;
	int ret;

	t_enc = drmm_simple_encoder_alloc(&tidss->ddev, struct tidss_encoder,
					  encoder, encoder_type);
	if (IS_ERR(t_enc))
		return PTR_ERR(t_enc);

	t_enc->tidss = tidss;
	t_enc->next_bridge = next_bridge;
	t_enc->bridge.funcs = &tidss_bridge_funcs;

	enc = &t_enc->encoder;
	enc->possible_crtcs = possible_crtcs;

	/* Attaching first bridge to the encoder */
	ret = drm_bridge_attach(enc, &t_enc->bridge, NULL,
				DRM_BRIDGE_ATTACH_NO_CONNECTOR);
	if (ret) {
		dev_err(tidss->dev, "bridge attach failed: %d\n", ret);
		return ret;
	}

	/* Initializing the connector at the end of bridge-chain */
	connector = drm_bridge_connector_init(&tidss->ddev, enc);
	if (IS_ERR(connector)) {
		dev_err(tidss->dev, "bridge_connector create failed\n");
		return PTR_ERR(connector);
	}

	ret = drm_connector_attach_encoder(connector, enc);
	if (ret) {
		dev_err(tidss->dev, "attaching encoder to connector failed\n");
		return ret;
	}

	t_enc->connector = connector;

	dev_dbg(tidss->dev, "Encoder create done\n");

	return ret;
}