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 <linux/list.h>
21
22 #include <drm/drm_crtc.h>
23 #include <drm/drm_crtc_helper.h>
24 #include <drm/drm_edid.h>
25
26 #include "omap_drv.h"
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 */
39 struct omap_encoder {
40 struct drm_encoder base;
41 struct omap_dss_device *dssdev;
42 };
43
omap_encoder_get_dssdev(struct drm_encoder * encoder)44 struct 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
omap_encoder_destroy(struct drm_encoder * encoder)51 static void omap_encoder_destroy(struct drm_encoder *encoder)
52 {
53 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
54
55 drm_encoder_cleanup(encoder);
56 kfree(omap_encoder);
57 }
58
59 static const struct drm_encoder_funcs omap_encoder_funcs = {
60 .destroy = omap_encoder_destroy,
61 };
62
omap_encoder_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)63 static void omap_encoder_mode_set(struct drm_encoder *encoder,
64 struct drm_display_mode *mode,
65 struct drm_display_mode *adjusted_mode)
66 {
67 struct drm_device *dev = encoder->dev;
68 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
69 struct omap_dss_device *dssdev = omap_encoder->dssdev;
70 struct drm_connector *connector;
71 bool hdmi_mode;
72 int r;
73
74 hdmi_mode = false;
75 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
76 if (connector->encoder == encoder) {
77 hdmi_mode = omap_connector_get_hdmi_mode(connector);
78 break;
79 }
80 }
81
82 if (dssdev->driver->set_hdmi_mode)
83 dssdev->driver->set_hdmi_mode(dssdev, hdmi_mode);
84
85 if (hdmi_mode && dssdev->driver->set_hdmi_infoframe) {
86 struct hdmi_avi_infoframe avi;
87
88 r = drm_hdmi_avi_infoframe_from_display_mode(&avi, adjusted_mode);
89 if (r == 0)
90 dssdev->driver->set_hdmi_infoframe(dssdev, &avi);
91 }
92 }
93
omap_encoder_disable(struct drm_encoder * encoder)94 static void omap_encoder_disable(struct drm_encoder *encoder)
95 {
96 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
97 struct omap_dss_device *dssdev = omap_encoder->dssdev;
98 struct omap_dss_driver *dssdrv = dssdev->driver;
99
100 dssdrv->disable(dssdev);
101 }
102
omap_encoder_update(struct drm_encoder * encoder,enum omap_channel channel,struct omap_video_timings * timings)103 static int omap_encoder_update(struct drm_encoder *encoder,
104 enum omap_channel channel,
105 struct omap_video_timings *timings)
106 {
107 struct drm_device *dev = encoder->dev;
108 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
109 struct omap_dss_device *dssdev = omap_encoder->dssdev;
110 struct omap_dss_driver *dssdrv = dssdev->driver;
111 int ret;
112
113 if (dssdrv->check_timings) {
114 ret = dssdrv->check_timings(dssdev, timings);
115 } else {
116 struct omap_video_timings t = {0};
117
118 dssdrv->get_timings(dssdev, &t);
119
120 if (memcmp(timings, &t, sizeof(struct omap_video_timings)))
121 ret = -EINVAL;
122 else
123 ret = 0;
124 }
125
126 if (ret) {
127 dev_err(dev->dev, "could not set timings: %d\n", ret);
128 return ret;
129 }
130
131 if (dssdrv->set_timings)
132 dssdrv->set_timings(dssdev, timings);
133
134 return 0;
135 }
136
omap_encoder_enable(struct drm_encoder * encoder)137 static void omap_encoder_enable(struct drm_encoder *encoder)
138 {
139 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
140 struct omap_dss_device *dssdev = omap_encoder->dssdev;
141 struct omap_dss_driver *dssdrv = dssdev->driver;
142 int r;
143
144 omap_encoder_update(encoder, omap_crtc_channel(encoder->crtc),
145 omap_crtc_timings(encoder->crtc));
146
147 r = dssdrv->enable(dssdev);
148 if (r)
149 dev_err(encoder->dev->dev,
150 "Failed to enable display '%s': %d\n",
151 dssdev->name, r);
152 }
153
omap_encoder_atomic_check(struct drm_encoder * encoder,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)154 static int omap_encoder_atomic_check(struct drm_encoder *encoder,
155 struct drm_crtc_state *crtc_state,
156 struct drm_connector_state *conn_state)
157 {
158 return 0;
159 }
160
161 static const struct drm_encoder_helper_funcs omap_encoder_helper_funcs = {
162 .mode_set = omap_encoder_mode_set,
163 .disable = omap_encoder_disable,
164 .enable = omap_encoder_enable,
165 .atomic_check = omap_encoder_atomic_check,
166 };
167
168 /* initialize encoder */
omap_encoder_init(struct drm_device * dev,struct omap_dss_device * dssdev)169 struct drm_encoder *omap_encoder_init(struct drm_device *dev,
170 struct omap_dss_device *dssdev)
171 {
172 struct drm_encoder *encoder = NULL;
173 struct omap_encoder *omap_encoder;
174
175 omap_encoder = kzalloc(sizeof(*omap_encoder), GFP_KERNEL);
176 if (!omap_encoder)
177 goto fail;
178
179 omap_encoder->dssdev = dssdev;
180
181 encoder = &omap_encoder->base;
182
183 drm_encoder_init(dev, encoder, &omap_encoder_funcs,
184 DRM_MODE_ENCODER_TMDS, NULL);
185 drm_encoder_helper_add(encoder, &omap_encoder_helper_funcs);
186
187 return encoder;
188
189 fail:
190 if (encoder)
191 omap_encoder_destroy(encoder);
192
193 return NULL;
194 }
195