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  */
39 struct omap_encoder {
40 	struct drm_encoder base;
41 	struct omap_dss_device *dssdev;
42 };
43 
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 
51 static void omap_encoder_destroy(struct drm_encoder *encoder)
52 {
53 	struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
54 	drm_encoder_cleanup(encoder);
55 	kfree(omap_encoder);
56 }
57 
58 static const struct drm_encoder_funcs omap_encoder_funcs = {
59 	.destroy = omap_encoder_destroy,
60 };
61 
62 /*
63  * The CRTC drm_crtc_helper_set_mode() doesn't really give us the right
64  * order.. the easiest way to work around this for now is to make all
65  * the encoder-helper's no-op's and have the omap_crtc code take care
66  * of the sequencing and call us in the right points.
67  *
68  * Eventually to handle connecting CRTCs to different encoders properly,
69  * either the CRTC helpers need to change or we need to replace
70  * drm_crtc_helper_set_mode(), but lets wait until atomic-modeset for
71  * that.
72  */
73 
74 static void omap_encoder_dpms(struct drm_encoder *encoder, int mode)
75 {
76 }
77 
78 static bool omap_encoder_mode_fixup(struct drm_encoder *encoder,
79 				  const struct drm_display_mode *mode,
80 				  struct drm_display_mode *adjusted_mode)
81 {
82 	return true;
83 }
84 
85 static void omap_encoder_mode_set(struct drm_encoder *encoder,
86 				struct drm_display_mode *mode,
87 				struct drm_display_mode *adjusted_mode)
88 {
89 }
90 
91 static void omap_encoder_prepare(struct drm_encoder *encoder)
92 {
93 }
94 
95 static void omap_encoder_commit(struct drm_encoder *encoder)
96 {
97 }
98 
99 static const struct drm_encoder_helper_funcs omap_encoder_helper_funcs = {
100 	.dpms = omap_encoder_dpms,
101 	.mode_fixup = omap_encoder_mode_fixup,
102 	.mode_set = omap_encoder_mode_set,
103 	.prepare = omap_encoder_prepare,
104 	.commit = omap_encoder_commit,
105 };
106 
107 /*
108  * Instead of relying on the helpers for modeset, the omap_crtc code
109  * calls these functions in the proper sequence.
110  */
111 
112 int omap_encoder_set_enabled(struct drm_encoder *encoder, bool enabled)
113 {
114 	struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
115 	struct omap_dss_device *dssdev = omap_encoder->dssdev;
116 	struct omap_dss_driver *dssdrv = dssdev->driver;
117 
118 	if (enabled) {
119 		return dssdrv->enable(dssdev);
120 	} else {
121 		dssdrv->disable(dssdev);
122 		return 0;
123 	}
124 }
125 
126 int omap_encoder_update(struct drm_encoder *encoder,
127 		struct omap_overlay_manager *mgr,
128 		struct omap_video_timings *timings)
129 {
130 	struct drm_device *dev = encoder->dev;
131 	struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
132 	struct omap_dss_device *dssdev = omap_encoder->dssdev;
133 	struct omap_dss_driver *dssdrv = dssdev->driver;
134 	int ret;
135 
136 	dssdev->src->manager = mgr;
137 
138 	if (dssdrv->check_timings) {
139 		ret = dssdrv->check_timings(dssdev, timings);
140 	} else {
141 		struct omap_video_timings t = {0};
142 
143 		dssdrv->get_timings(dssdev, &t);
144 
145 		if (memcmp(timings, &t, sizeof(struct omap_video_timings)))
146 			ret = -EINVAL;
147 		else
148 			ret = 0;
149 	}
150 
151 	if (ret) {
152 		dev_err(dev->dev, "could not set timings: %d\n", ret);
153 		return ret;
154 	}
155 
156 	if (dssdrv->set_timings)
157 		dssdrv->set_timings(dssdev, timings);
158 
159 	return 0;
160 }
161 
162 /* initialize encoder */
163 struct drm_encoder *omap_encoder_init(struct drm_device *dev,
164 		struct omap_dss_device *dssdev)
165 {
166 	struct drm_encoder *encoder = NULL;
167 	struct omap_encoder *omap_encoder;
168 
169 	omap_encoder = kzalloc(sizeof(*omap_encoder), GFP_KERNEL);
170 	if (!omap_encoder)
171 		goto fail;
172 
173 	omap_encoder->dssdev = dssdev;
174 
175 	encoder = &omap_encoder->base;
176 
177 	drm_encoder_init(dev, encoder, &omap_encoder_funcs,
178 			 DRM_MODE_ENCODER_TMDS);
179 	drm_encoder_helper_add(encoder, &omap_encoder_helper_funcs);
180 
181 	return encoder;
182 
183 fail:
184 	if (encoder)
185 		omap_encoder_destroy(encoder);
186 
187 	return NULL;
188 }
189