xref: /linux/drivers/gpu/drm/omapdrm/omap_encoder.c (revision face6a3615a649456eb4549f6d474221d877d604)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2011 Texas Instruments Incorporated - https://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 drm_device *dev = encoder->dev;
79 	struct drm_connector *connector;
80 	struct videomode vm = { 0 };
81 	u32 bus_flags;
82 
83 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
84 		if (connector->encoder == encoder)
85 			break;
86 	}
87 
88 	drm_display_mode_to_videomode(adjusted_mode, &vm);
89 
90 	/*
91 	 * HACK: This fixes the vm flags.
92 	 * struct drm_display_mode does not contain the VSYNC/HSYNC/DE flags and
93 	 * they get lost when converting back and forth between struct
94 	 * drm_display_mode and struct videomode. The hack below goes and
95 	 * fetches the missing flags.
96 	 *
97 	 * A better solution is to use DRM's bus-flags through the whole driver.
98 	 */
99 	drm_for_each_bridge_in_chain_from(output->bridge, bridge) {
100 		if (!bridge->timings)
101 			continue;
102 
103 		bus_flags = bridge->timings->input_bus_flags;
104 		omap_encoder_update_videomode_flags(&vm, bus_flags);
105 	}
106 
107 	bus_flags = connector->display_info.bus_flags;
108 	omap_encoder_update_videomode_flags(&vm, bus_flags);
109 
110 	/* Set timings for all devices in the display pipeline. */
111 	dss_mgr_set_timings(output, &vm);
112 }
113 
114 static const struct drm_encoder_helper_funcs omap_encoder_helper_funcs = {
115 	.mode_set = omap_encoder_mode_set,
116 };
117 
118 /* initialize encoder */
119 struct drm_encoder *omap_encoder_init(struct drm_device *dev,
120 				      struct omap_dss_device *output)
121 {
122 	struct drm_encoder *encoder = NULL;
123 	struct omap_encoder *omap_encoder;
124 
125 	omap_encoder = kzalloc(sizeof(*omap_encoder), GFP_KERNEL);
126 	if (!omap_encoder)
127 		goto fail;
128 
129 	omap_encoder->output = output;
130 
131 	encoder = &omap_encoder->base;
132 
133 	drm_encoder_init(dev, encoder, &omap_encoder_funcs,
134 			 DRM_MODE_ENCODER_TMDS, NULL);
135 	drm_encoder_helper_add(encoder, &omap_encoder_helper_funcs);
136 
137 	return encoder;
138 
139 fail:
140 	if (encoder)
141 		omap_encoder_destroy(encoder);
142 
143 	return NULL;
144 }
145