blob: ab6ed49f37abc27838524205efa50f5553359a80 [file] [log] [blame]
Rob Clarkcd5351f2011-11-12 12:09:40 -06001/*
Rob Clark8bb0daf2013-02-11 12:43:09 -05002 * drivers/gpu/drm/omapdrm/omap_encoder.c
Rob Clarkcd5351f2011-11-12 12:09:40 -06003 *
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
Laurent Pinchart2d278f52015-03-05 21:31:37 +020020#include <linux/list.h>
21
22#include <drm/drm_crtc.h>
23#include <drm/drm_crtc_helper.h>
Tomi Valkeinen4f930c02014-06-18 14:19:48 +030024#include <drm/drm_edid.h>
25
Rob Clarkcd5351f2011-11-12 12:09:40 -060026#include "omap_drv.h"
27
Rob Clarkcd5351f2011-11-12 12:09:40 -060028/*
29 * encoder funcs
30 */
31
32#define to_omap_encoder(x) container_of(x, struct omap_encoder, base)
33
Rob Clarkf5f94542012-12-04 13:59:12 -060034/* 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 */
Rob Clarkcd5351f2011-11-12 12:09:40 -060039struct omap_encoder {
40 struct drm_encoder base;
Rob Clarkf5f94542012-12-04 13:59:12 -060041 struct omap_dss_device *dssdev;
Rob Clarkcd5351f2011-11-12 12:09:40 -060042};
43
Archit Taneja0d8f3712013-03-26 19:15:19 +053044struct 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
Rob Clarkcd5351f2011-11-12 12:09:40 -060051static void omap_encoder_destroy(struct drm_encoder *encoder)
52{
53 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Archit Tanejaec72a812014-01-02 14:49:53 +053054
55 omap_encoder_set_enabled(encoder, false);
56
Rob Clarkcd5351f2011-11-12 12:09:40 -060057 drm_encoder_cleanup(encoder);
58 kfree(omap_encoder);
59}
60
Rob Clarkf5f94542012-12-04 13:59:12 -060061static 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
Rob Clarkcd5351f2011-11-12 12:09:40 -060077static void omap_encoder_dpms(struct drm_encoder *encoder, int mode)
78{
Rob Clarkcd5351f2011-11-12 12:09:40 -060079}
80
81static bool omap_encoder_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +020082 const struct drm_display_mode *mode,
Rob Clarkcd5351f2011-11-12 12:09:40 -060083 struct drm_display_mode *adjusted_mode)
84{
Rob Clarkcd5351f2011-11-12 12:09:40 -060085 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{
Tomi Valkeinen4f930c02014-06-18 14:19:48 +030092 struct drm_device *dev = encoder->dev;
93 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
94 struct omap_dss_device *dssdev = omap_encoder->dssdev;
95 struct drm_connector *connector;
96 bool hdmi_mode;
97 int r;
98
99 hdmi_mode = false;
100 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
101 if (connector->encoder == encoder) {
102 hdmi_mode = omap_connector_get_hdmi_mode(connector);
103 break;
104 }
105 }
106
107 if (dssdev->driver->set_hdmi_mode)
108 dssdev->driver->set_hdmi_mode(dssdev, hdmi_mode);
109
110 if (hdmi_mode && dssdev->driver->set_hdmi_infoframe) {
111 struct hdmi_avi_infoframe avi;
112
113 r = drm_hdmi_avi_infoframe_from_display_mode(&avi, adjusted_mode);
114 if (r == 0)
115 dssdev->driver->set_hdmi_infoframe(dssdev, &avi);
116 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600117}
118
119static void omap_encoder_prepare(struct drm_encoder *encoder)
120{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600121}
122
123static void omap_encoder_commit(struct drm_encoder *encoder)
124{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600125}
126
Rob Clarkcd5351f2011-11-12 12:09:40 -0600127static const struct drm_encoder_helper_funcs omap_encoder_helper_funcs = {
128 .dpms = omap_encoder_dpms,
129 .mode_fixup = omap_encoder_mode_fixup,
130 .mode_set = omap_encoder_mode_set,
131 .prepare = omap_encoder_prepare,
132 .commit = omap_encoder_commit,
133};
134
Rob Clarkf5f94542012-12-04 13:59:12 -0600135/*
136 * Instead of relying on the helpers for modeset, the omap_crtc code
137 * calls these functions in the proper sequence.
138 */
139
140int omap_encoder_set_enabled(struct drm_encoder *encoder, bool enabled)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600141{
142 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Rob Clarkf5f94542012-12-04 13:59:12 -0600143 struct omap_dss_device *dssdev = omap_encoder->dssdev;
144 struct omap_dss_driver *dssdrv = dssdev->driver;
145
146 if (enabled) {
147 return dssdrv->enable(dssdev);
148 } else {
149 dssdrv->disable(dssdev);
150 return 0;
151 }
152}
153
154int omap_encoder_update(struct drm_encoder *encoder,
155 struct omap_overlay_manager *mgr,
156 struct omap_video_timings *timings)
157{
158 struct drm_device *dev = encoder->dev;
159 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
160 struct omap_dss_device *dssdev = omap_encoder->dssdev;
161 struct omap_dss_driver *dssdrv = dssdev->driver;
162 int ret;
163
Tomi Valkeinena73fdc62013-07-24 13:01:34 +0300164 dssdev->src->manager = mgr;
Rob Clarkf5f94542012-12-04 13:59:12 -0600165
Archit Tanejabddabbe2013-03-26 19:15:20 +0530166 if (dssdrv->check_timings) {
167 ret = dssdrv->check_timings(dssdev, timings);
168 } else {
169 struct omap_video_timings t = {0};
170
171 dssdrv->get_timings(dssdev, &t);
172
173 if (memcmp(timings, &t, sizeof(struct omap_video_timings)))
174 ret = -EINVAL;
175 else
176 ret = 0;
177 }
178
Rob Clarkf5f94542012-12-04 13:59:12 -0600179 if (ret) {
180 dev_err(dev->dev, "could not set timings: %d\n", ret);
181 return ret;
182 }
183
Archit Tanejabddabbe2013-03-26 19:15:20 +0530184 if (dssdrv->set_timings)
185 dssdrv->set_timings(dssdev, timings);
Rob Clarkf5f94542012-12-04 13:59:12 -0600186
187 return 0;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600188}
189
190/* initialize encoder */
191struct drm_encoder *omap_encoder_init(struct drm_device *dev,
Rob Clarkf5f94542012-12-04 13:59:12 -0600192 struct omap_dss_device *dssdev)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600193{
194 struct drm_encoder *encoder = NULL;
195 struct omap_encoder *omap_encoder;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600196
197 omap_encoder = kzalloc(sizeof(*omap_encoder), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800198 if (!omap_encoder)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600199 goto fail;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600200
Rob Clarkf5f94542012-12-04 13:59:12 -0600201 omap_encoder->dssdev = dssdev;
202
Rob Clarkcd5351f2011-11-12 12:09:40 -0600203 encoder = &omap_encoder->base;
204
205 drm_encoder_init(dev, encoder, &omap_encoder_funcs,
206 DRM_MODE_ENCODER_TMDS);
207 drm_encoder_helper_add(encoder, &omap_encoder_helper_funcs);
208
Rob Clarkcd5351f2011-11-12 12:09:40 -0600209 return encoder;
210
211fail:
YAMANE Toshiaki582bc282012-11-14 19:31:27 +0900212 if (encoder)
Rob Clark65b0bd02011-12-09 23:26:07 -0600213 omap_encoder_destroy(encoder);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600214
215 return NULL;
216}