xref: /linux/drivers/gpu/drm/omapdrm/omap_encoder.c (revision 4fcbfbae)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
4  * Author: Rob Clark <rob@ti.com>
5  */
6 
7 #include <linux/list.h>
8 
9 #include <drm/drm_bridge.h>
10 #include <drm/drm_crtc.h>
11 #include <drm/drm_modeset_helper_vtables.h>
12 #include <drm/drm_edid.h>
13 
14 #include "omap_drv.h"
15 
16 /*
17  * encoder funcs
18  */
19 
20 #define to_omap_encoder(x) container_of(x, struct omap_encoder, base)
21 
22 /* The encoder and connector both map to same dssdev.. the encoder
23  * handles the 'active' parts, ie. anything the modifies the state
24  * of the hw, and the connector handles the 'read-only' parts, like
25  * detecting connection and reading edid.
26  */
27 struct omap_encoder {
28 	struct drm_encoder base;
29 	struct omap_dss_device *output;
30 };
31 
32 static void omap_encoder_destroy(struct drm_encoder *encoder)
33 {
34 	struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
35 
36 	drm_encoder_cleanup(encoder);
37 	kfree(omap_encoder);
38 }
39 
40 static const struct drm_encoder_funcs omap_encoder_funcs = {
41 	.destroy = omap_encoder_destroy,
42 };
43 
44 static void omap_encoder_update_videomode_flags(struct videomode *vm,
45 						u32 bus_flags)
46 {
47 	if (!(vm->flags & (DISPLAY_FLAGS_DE_LOW |
48 			   DISPLAY_FLAGS_DE_HIGH))) {
49 		if (bus_flags & DRM_BUS_FLAG_DE_LOW)
50 			vm->flags |= DISPLAY_FLAGS_DE_LOW;
51 		else if (bus_flags & DRM_BUS_FLAG_DE_HIGH)
52 			vm->flags |= DISPLAY_FLAGS_DE_HIGH;
53 	}
54 
55 	if (!(vm->flags & (DISPLAY_FLAGS_PIXDATA_POSEDGE |
56 			   DISPLAY_FLAGS_PIXDATA_NEGEDGE))) {
57 		if (bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE)
58 			vm->flags |= DISPLAY_FLAGS_PIXDATA_POSEDGE;
59 		else if (bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE)
60 			vm->flags |= DISPLAY_FLAGS_PIXDATA_NEGEDGE;
61 	}
62 
63 	if (!(vm->flags & (DISPLAY_FLAGS_SYNC_POSEDGE |
64 			   DISPLAY_FLAGS_SYNC_NEGEDGE))) {
65 		if (bus_flags & DRM_BUS_FLAG_SYNC_DRIVE_POSEDGE)
66 			vm->flags |= DISPLAY_FLAGS_SYNC_POSEDGE;
67 		else if (bus_flags & DRM_BUS_FLAG_SYNC_DRIVE_NEGEDGE)
68 			vm->flags |= DISPLAY_FLAGS_SYNC_NEGEDGE;
69 	}
70 }
71 
72 static void omap_encoder_mode_set(struct drm_encoder *encoder,
73 				  struct drm_display_mode *mode,
74 				  struct drm_display_mode *adjusted_mode)
75 {
76 	struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
77 	struct omap_dss_device *output = omap_encoder->output;
78 	struct omap_dss_device *dssdev;
79 	struct drm_device *dev = encoder->dev;
80 	struct drm_connector *connector;
81 	struct drm_bridge *bridge;
82 	struct videomode vm = { 0 };
83 	u32 bus_flags;
84 
85 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
86 		if (connector->encoder == encoder)
87 			break;
88 	}
89 
90 	drm_display_mode_to_videomode(adjusted_mode, &vm);
91 
92 	/*
93 	 * HACK: This fixes the vm flags.
94 	 * struct drm_display_mode does not contain the VSYNC/HSYNC/DE flags and
95 	 * they get lost when converting back and forth between struct
96 	 * drm_display_mode and struct videomode. The hack below goes and
97 	 * fetches the missing flags.
98 	 *
99 	 * A better solution is to use DRM's bus-flags through the whole driver.
100 	 */
101 	for (dssdev = output; dssdev; dssdev = dssdev->next)
102 		omap_encoder_update_videomode_flags(&vm, dssdev->bus_flags);
103 
104 	for (bridge = output->bridge; bridge;
105 	     bridge = drm_bridge_get_next_bridge(bridge)) {
106 		if (!bridge->timings)
107 			continue;
108 
109 		bus_flags = bridge->timings->input_bus_flags;
110 		omap_encoder_update_videomode_flags(&vm, bus_flags);
111 	}
112 
113 	bus_flags = connector->display_info.bus_flags;
114 	omap_encoder_update_videomode_flags(&vm, bus_flags);
115 
116 	/* Set timings for all devices in the display pipeline. */
117 	dss_mgr_set_timings(output, &vm);
118 
119 	for (dssdev = output; dssdev; dssdev = dssdev->next) {
120 		if (dssdev->ops && dssdev->ops->set_timings)
121 			dssdev->ops->set_timings(dssdev, adjusted_mode);
122 	}
123 }
124 
125 static void omap_encoder_disable(struct drm_encoder *encoder)
126 {
127 	struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
128 	struct omap_dss_device *dssdev = omap_encoder->output;
129 	struct drm_device *dev = encoder->dev;
130 
131 	dev_dbg(dev->dev, "disable(%s)\n", dssdev->name);
132 
133 	/*
134 	 * Disable the chain of external devices, starting at the one at the
135 	 * internal encoder's output.
136 	 */
137 	omapdss_device_disable(dssdev->next);
138 
139 	/*
140 	 * Disable the internal encoder. This will disable the DSS output. The
141 	 * DSI is treated as an exception as DSI pipelines still use the legacy
142 	 * flow where the pipeline output controls the encoder.
143 	 */
144 	if (dssdev->type != OMAP_DISPLAY_TYPE_DSI) {
145 		if (dssdev->ops && dssdev->ops->disable)
146 			dssdev->ops->disable(dssdev);
147 		dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
148 	}
149 
150 	/*
151 	 * Perform the post-disable operations on the chain of external devices
152 	 * to complete the display pipeline disable.
153 	 */
154 	omapdss_device_post_disable(dssdev->next);
155 }
156 
157 static void omap_encoder_enable(struct drm_encoder *encoder)
158 {
159 	struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
160 	struct omap_dss_device *dssdev = omap_encoder->output;
161 	struct drm_device *dev = encoder->dev;
162 
163 	dev_dbg(dev->dev, "enable(%s)\n", dssdev->name);
164 
165 	/* Prepare the chain of external devices for pipeline enable. */
166 	omapdss_device_pre_enable(dssdev->next);
167 
168 	/*
169 	 * Enable the internal encoder. This will enable the DSS output. The
170 	 * DSI is treated as an exception as DSI pipelines still use the legacy
171 	 * flow where the pipeline output controls the encoder.
172 	 */
173 	if (dssdev->type != OMAP_DISPLAY_TYPE_DSI) {
174 		if (dssdev->ops && dssdev->ops->enable)
175 			dssdev->ops->enable(dssdev);
176 		dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
177 	}
178 
179 	/*
180 	 * Enable the chain of external devices, starting at the one at the
181 	 * internal encoder's output.
182 	 */
183 	omapdss_device_enable(dssdev->next);
184 }
185 
186 static int omap_encoder_atomic_check(struct drm_encoder *encoder,
187 				     struct drm_crtc_state *crtc_state,
188 				     struct drm_connector_state *conn_state)
189 {
190 	struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
191 	enum drm_mode_status status;
192 
193 	status = omap_connector_mode_fixup(omap_encoder->output,
194 					   &crtc_state->mode,
195 					   &crtc_state->adjusted_mode);
196 	if (status != MODE_OK) {
197 		dev_err(encoder->dev->dev, "invalid timings: %d\n", status);
198 		return -EINVAL;
199 	}
200 
201 	return 0;
202 }
203 
204 static const struct drm_encoder_helper_funcs omap_encoder_helper_funcs = {
205 	.mode_set = omap_encoder_mode_set,
206 	.disable = omap_encoder_disable,
207 	.enable = omap_encoder_enable,
208 	.atomic_check = omap_encoder_atomic_check,
209 };
210 
211 /* initialize encoder */
212 struct drm_encoder *omap_encoder_init(struct drm_device *dev,
213 				      struct omap_dss_device *output)
214 {
215 	struct drm_encoder *encoder = NULL;
216 	struct omap_encoder *omap_encoder;
217 
218 	omap_encoder = kzalloc(sizeof(*omap_encoder), GFP_KERNEL);
219 	if (!omap_encoder)
220 		goto fail;
221 
222 	omap_encoder->output = output;
223 
224 	encoder = &omap_encoder->base;
225 
226 	drm_encoder_init(dev, encoder, &omap_encoder_funcs,
227 			 DRM_MODE_ENCODER_TMDS, NULL);
228 	drm_encoder_helper_add(encoder, &omap_encoder_helper_funcs);
229 
230 	return encoder;
231 
232 fail:
233 	if (encoder)
234 		omap_encoder_destroy(encoder);
235 
236 	return NULL;
237 }
238