Loading...
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 */
27struct omap_encoder {
28 struct drm_encoder base;
29 struct omap_dss_device *output;
30};
31
32static 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
40static const struct drm_encoder_funcs omap_encoder_funcs = {
41 .destroy = omap_encoder_destroy,
42};
43
44static 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
72static 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 drm_bridge *bridge;
81 struct videomode vm = { 0 };
82 u32 bus_flags;
83
84 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
85 if (connector->encoder == encoder)
86 break;
87 }
88
89 drm_display_mode_to_videomode(adjusted_mode, &vm);
90
91 /*
92 * HACK: This fixes the vm flags.
93 * struct drm_display_mode does not contain the VSYNC/HSYNC/DE flags and
94 * they get lost when converting back and forth between struct
95 * drm_display_mode and struct videomode. The hack below goes and
96 * fetches the missing flags.
97 *
98 * A better solution is to use DRM's bus-flags through the whole driver.
99 */
100 for (bridge = output->bridge; bridge;
101 bridge = drm_bridge_get_next_bridge(bridge)) {
102 if (!bridge->timings)
103 continue;
104
105 bus_flags = bridge->timings->input_bus_flags;
106 omap_encoder_update_videomode_flags(&vm, bus_flags);
107 }
108
109 bus_flags = connector->display_info.bus_flags;
110 omap_encoder_update_videomode_flags(&vm, bus_flags);
111
112 /* Set timings for all devices in the display pipeline. */
113 dss_mgr_set_timings(output, &vm);
114}
115
116static const struct drm_encoder_helper_funcs omap_encoder_helper_funcs = {
117 .mode_set = omap_encoder_mode_set,
118};
119
120/* initialize encoder */
121struct drm_encoder *omap_encoder_init(struct drm_device *dev,
122 struct omap_dss_device *output)
123{
124 struct drm_encoder *encoder = NULL;
125 struct omap_encoder *omap_encoder;
126
127 omap_encoder = kzalloc(sizeof(*omap_encoder), GFP_KERNEL);
128 if (!omap_encoder)
129 goto fail;
130
131 omap_encoder->output = output;
132
133 encoder = &omap_encoder->base;
134
135 drm_encoder_init(dev, encoder, &omap_encoder_funcs,
136 DRM_MODE_ENCODER_TMDS, NULL);
137 drm_encoder_helper_add(encoder, &omap_encoder_helper_funcs);
138
139 return encoder;
140
141fail:
142 if (encoder)
143 omap_encoder_destroy(encoder);
144
145 return NULL;
146}
1/*
2 * drivers/gpu/drm/omapdrm/omap_encoder.c
3 *
4 * Copyright (C) 2011 Texas Instruments
5 * Author: Rob Clark <rob@ti.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "omap_drv.h"
21
22#include "drm_crtc.h"
23#include "drm_crtc_helper.h"
24
25#include <linux/list.h>
26
27
28/*
29 * encoder funcs
30 */
31
32#define to_omap_encoder(x) container_of(x, struct omap_encoder, base)
33
34/* The encoder and connector both map to same dssdev.. the encoder
35 * handles the 'active' parts, ie. anything the modifies the state
36 * of the hw, and the connector handles the 'read-only' parts, like
37 * detecting connection and reading edid.
38 */
39struct omap_encoder {
40 struct drm_encoder base;
41 struct omap_dss_device *dssdev;
42};
43
44struct omap_dss_device *omap_encoder_get_dssdev(struct drm_encoder *encoder)
45{
46 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
47
48 return omap_encoder->dssdev;
49}
50
51static void omap_encoder_destroy(struct drm_encoder *encoder)
52{
53 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
54
55 omap_encoder_set_enabled(encoder, false);
56
57 drm_encoder_cleanup(encoder);
58 kfree(omap_encoder);
59}
60
61static const struct drm_encoder_funcs omap_encoder_funcs = {
62 .destroy = omap_encoder_destroy,
63};
64
65/*
66 * The CRTC drm_crtc_helper_set_mode() doesn't really give us the right
67 * order.. the easiest way to work around this for now is to make all
68 * the encoder-helper's no-op's and have the omap_crtc code take care
69 * of the sequencing and call us in the right points.
70 *
71 * Eventually to handle connecting CRTCs to different encoders properly,
72 * either the CRTC helpers need to change or we need to replace
73 * drm_crtc_helper_set_mode(), but lets wait until atomic-modeset for
74 * that.
75 */
76
77static void omap_encoder_dpms(struct drm_encoder *encoder, int mode)
78{
79}
80
81static bool omap_encoder_mode_fixup(struct drm_encoder *encoder,
82 const struct drm_display_mode *mode,
83 struct drm_display_mode *adjusted_mode)
84{
85 return true;
86}
87
88static void omap_encoder_mode_set(struct drm_encoder *encoder,
89 struct drm_display_mode *mode,
90 struct drm_display_mode *adjusted_mode)
91{
92}
93
94static void omap_encoder_prepare(struct drm_encoder *encoder)
95{
96}
97
98static void omap_encoder_commit(struct drm_encoder *encoder)
99{
100}
101
102static const struct drm_encoder_helper_funcs omap_encoder_helper_funcs = {
103 .dpms = omap_encoder_dpms,
104 .mode_fixup = omap_encoder_mode_fixup,
105 .mode_set = omap_encoder_mode_set,
106 .prepare = omap_encoder_prepare,
107 .commit = omap_encoder_commit,
108};
109
110/*
111 * Instead of relying on the helpers for modeset, the omap_crtc code
112 * calls these functions in the proper sequence.
113 */
114
115int omap_encoder_set_enabled(struct drm_encoder *encoder, bool enabled)
116{
117 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
118 struct omap_dss_device *dssdev = omap_encoder->dssdev;
119 struct omap_dss_driver *dssdrv = dssdev->driver;
120
121 if (enabled) {
122 return dssdrv->enable(dssdev);
123 } else {
124 dssdrv->disable(dssdev);
125 return 0;
126 }
127}
128
129int omap_encoder_update(struct drm_encoder *encoder,
130 struct omap_overlay_manager *mgr,
131 struct omap_video_timings *timings)
132{
133 struct drm_device *dev = encoder->dev;
134 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
135 struct omap_dss_device *dssdev = omap_encoder->dssdev;
136 struct omap_dss_driver *dssdrv = dssdev->driver;
137 int ret;
138
139 dssdev->src->manager = mgr;
140
141 if (dssdrv->check_timings) {
142 ret = dssdrv->check_timings(dssdev, timings);
143 } else {
144 struct omap_video_timings t = {0};
145
146 dssdrv->get_timings(dssdev, &t);
147
148 if (memcmp(timings, &t, sizeof(struct omap_video_timings)))
149 ret = -EINVAL;
150 else
151 ret = 0;
152 }
153
154 if (ret) {
155 dev_err(dev->dev, "could not set timings: %d\n", ret);
156 return ret;
157 }
158
159 if (dssdrv->set_timings)
160 dssdrv->set_timings(dssdev, timings);
161
162 return 0;
163}
164
165/* initialize encoder */
166struct drm_encoder *omap_encoder_init(struct drm_device *dev,
167 struct omap_dss_device *dssdev)
168{
169 struct drm_encoder *encoder = NULL;
170 struct omap_encoder *omap_encoder;
171
172 omap_encoder = kzalloc(sizeof(*omap_encoder), GFP_KERNEL);
173 if (!omap_encoder)
174 goto fail;
175
176 omap_encoder->dssdev = dssdev;
177
178 encoder = &omap_encoder->base;
179
180 drm_encoder_init(dev, encoder, &omap_encoder_funcs,
181 DRM_MODE_ENCODER_TMDS);
182 drm_encoder_helper_add(encoder, &omap_encoder_helper_funcs);
183
184 return encoder;
185
186fail:
187 if (encoder)
188 omap_encoder_destroy(encoder);
189
190 return NULL;
191}