blob: 0177a2c4b77a52cf267c5377a9ad9d9ab2fc6247 [file] [log] [blame]
Rob Clarkcd5351f2011-11-12 12:09:40 -06001/*
Andrew F. Davisbb5cdf82017-12-05 14:29:31 -06002 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
Rob Clarkcd5351f2011-11-12 12:09:40 -06003 * Author: Rob Clark <rob@ti.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
Laurent Pinchart2d278f52015-03-05 21:31:37 +020018#include <linux/list.h>
19
20#include <drm/drm_crtc.h>
21#include <drm/drm_crtc_helper.h>
Tomi Valkeinen4f930c02014-06-18 14:19:48 +030022#include <drm/drm_edid.h>
23
Rob Clarkcd5351f2011-11-12 12:09:40 -060024#include "omap_drv.h"
25
Rob Clarkcd5351f2011-11-12 12:09:40 -060026/*
27 * encoder funcs
28 */
29
30#define to_omap_encoder(x) container_of(x, struct omap_encoder, base)
31
Rob Clarkf5f94542012-12-04 13:59:12 -060032/* The encoder and connector both map to same dssdev.. the encoder
33 * handles the 'active' parts, ie. anything the modifies the state
34 * of the hw, and the connector handles the 'read-only' parts, like
35 * detecting connection and reading edid.
36 */
Rob Clarkcd5351f2011-11-12 12:09:40 -060037struct omap_encoder {
38 struct drm_encoder base;
Laurent Pinchartd96aaad2018-05-31 23:14:43 +030039 struct omap_dss_device *output;
40 struct omap_dss_device *display;
Rob Clarkcd5351f2011-11-12 12:09:40 -060041};
42
43static void omap_encoder_destroy(struct drm_encoder *encoder)
44{
45 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Archit Tanejaec72a812014-01-02 14:49:53 +053046
Rob Clarkcd5351f2011-11-12 12:09:40 -060047 drm_encoder_cleanup(encoder);
48 kfree(omap_encoder);
49}
50
Rob Clarkf5f94542012-12-04 13:59:12 -060051static const struct drm_encoder_funcs omap_encoder_funcs = {
52 .destroy = omap_encoder_destroy,
53};
54
Rob Clarkcd5351f2011-11-12 12:09:40 -060055static void omap_encoder_mode_set(struct drm_encoder *encoder,
Laurent Pinchart3fbda312018-06-07 17:58:57 +030056 struct drm_display_mode *mode,
57 struct drm_display_mode *adjusted_mode)
Rob Clarkcd5351f2011-11-12 12:09:40 -060058{
Tomi Valkeinen4f930c02014-06-18 14:19:48 +030059 struct drm_device *dev = encoder->dev;
60 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Laurent Pinchart3fbda312018-06-07 17:58:57 +030061 struct omap_dss_device *display = omap_encoder->display;
Tomi Valkeinen4f930c02014-06-18 14:19:48 +030062 struct drm_connector *connector;
Laurent Pinchart3fbda312018-06-07 17:58:57 +030063 struct omap_dss_device *dssdev;
64 struct videomode vm = { 0 };
Tomi Valkeinen4f930c02014-06-18 14:19:48 +030065 bool hdmi_mode;
66 int r;
67
Laurent Pinchart3fbda312018-06-07 17:58:57 +030068 drm_display_mode_to_videomode(adjusted_mode, &vm);
69
70 /*
71 * HACK: This fixes the vm flags.
72 * struct drm_display_mode does not contain the VSYNC/HSYNC/DE flags and
73 * they get lost when converting back and forth between struct
74 * drm_display_mode and struct videomode. The hack below goes and
75 * fetches the missing flags.
76 *
77 * A better solution is to use DRM's bus-flags through the whole driver.
78 */
79 for (dssdev = omap_encoder->output; dssdev; dssdev = dssdev->next) {
80 unsigned long bus_flags = dssdev->bus_flags;
81
82 if (!(vm.flags & (DISPLAY_FLAGS_DE_LOW |
83 DISPLAY_FLAGS_DE_HIGH))) {
84 if (bus_flags & DRM_BUS_FLAG_DE_LOW)
85 vm.flags |= DISPLAY_FLAGS_DE_LOW;
86 else if (bus_flags & DRM_BUS_FLAG_DE_HIGH)
87 vm.flags |= DISPLAY_FLAGS_DE_HIGH;
88 }
89
90 if (!(vm.flags & (DISPLAY_FLAGS_PIXDATA_POSEDGE |
91 DISPLAY_FLAGS_PIXDATA_NEGEDGE))) {
92 if (bus_flags & DRM_BUS_FLAG_PIXDATA_POSEDGE)
93 vm.flags |= DISPLAY_FLAGS_PIXDATA_POSEDGE;
94 else if (bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE)
95 vm.flags |= DISPLAY_FLAGS_PIXDATA_NEGEDGE;
96 }
97
98 if (!(vm.flags & (DISPLAY_FLAGS_SYNC_POSEDGE |
99 DISPLAY_FLAGS_SYNC_NEGEDGE))) {
100 if (bus_flags & DRM_BUS_FLAG_SYNC_POSEDGE)
101 vm.flags |= DISPLAY_FLAGS_SYNC_POSEDGE;
102 else if (bus_flags & DRM_BUS_FLAG_SYNC_NEGEDGE)
103 vm.flags |= DISPLAY_FLAGS_SYNC_NEGEDGE;
104 }
105 }
106
107 /*
108 * HACK: Call the .set_timings() operation if available, this will
109 * eventually store timings in the CRTC. Otherwise (for DSI outputs)
110 * store the timings directly.
111 *
112 * All outputs should be brought in sync to operate similarly.
113 */
114 if (display->ops->set_timings)
115 display->ops->set_timings(display, &vm);
116 else
117 *omap_crtc_timings(encoder->crtc) = vm;
118
Tomi Valkeinen4f930c02014-06-18 14:19:48 +0300119 hdmi_mode = false;
120 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
121 if (connector->encoder == encoder) {
122 hdmi_mode = omap_connector_get_hdmi_mode(connector);
123 break;
124 }
125 }
126
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300127 dssdev = omap_encoder->output;
128
Laurent Pinchart83910ad2018-06-01 19:45:01 +0300129 if (dssdev->ops->hdmi.set_hdmi_mode)
130 dssdev->ops->hdmi.set_hdmi_mode(dssdev, hdmi_mode);
Tomi Valkeinen4f930c02014-06-18 14:19:48 +0300131
Laurent Pinchart83910ad2018-06-01 19:45:01 +0300132 if (hdmi_mode && dssdev->ops->hdmi.set_infoframe) {
Tomi Valkeinen4f930c02014-06-18 14:19:48 +0300133 struct hdmi_avi_infoframe avi;
134
Shashank Sharma0c1f5282017-07-13 21:03:07 +0530135 r = drm_hdmi_avi_infoframe_from_display_mode(&avi, adjusted_mode,
136 false);
Tomi Valkeinen4f930c02014-06-18 14:19:48 +0300137 if (r == 0)
Laurent Pinchart83910ad2018-06-01 19:45:01 +0300138 dssdev->ops->hdmi.set_infoframe(dssdev, &avi);
Tomi Valkeinen4f930c02014-06-18 14:19:48 +0300139 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600140}
141
Laurent Pinchart68dc0392015-03-07 00:22:39 +0200142static void omap_encoder_disable(struct drm_encoder *encoder)
143{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600144 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Laurent Pinchartd96aaad2018-05-31 23:14:43 +0300145 struct omap_dss_device *dssdev = omap_encoder->display;
Rob Clarkf5f94542012-12-04 13:59:12 -0600146
Laurent Pinchart83910ad2018-06-01 19:45:01 +0300147 dssdev->ops->disable(dssdev);
Rob Clarkf5f94542012-12-04 13:59:12 -0600148}
149
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300150static void omap_encoder_enable(struct drm_encoder *encoder)
151{
152 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Laurent Pinchartd96aaad2018-05-31 23:14:43 +0300153 struct omap_dss_device *dssdev = omap_encoder->display;
Tomi Valkeinen8d83bbd2016-01-05 11:43:16 +0200154 int r;
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300155
Laurent Pinchart83910ad2018-06-01 19:45:01 +0300156 r = dssdev->ops->enable(dssdev);
Tomi Valkeinen8d83bbd2016-01-05 11:43:16 +0200157 if (r)
158 dev_err(encoder->dev->dev,
159 "Failed to enable display '%s': %d\n",
160 dssdev->name, r);
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300161}
162
163static int omap_encoder_atomic_check(struct drm_encoder *encoder,
164 struct drm_crtc_state *crtc_state,
165 struct drm_connector_state *conn_state)
166{
Laurent Pinchart3fbda312018-06-07 17:58:57 +0300167 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
168 struct drm_device *dev = encoder->dev;
169 struct omap_dss_device *dssdev;
170 struct videomode vm = { 0 };
171 int ret;
172
173 drm_display_mode_to_videomode(&crtc_state->mode, &vm);
174
175 for (dssdev = omap_encoder->output; dssdev; dssdev = dssdev->next) {
176 if (!dssdev->ops->check_timings)
177 continue;
178
179 ret = dssdev->ops->check_timings(dssdev, &vm);
180 if (ret) {
181 dev_err(dev->dev, "invalid timings: %d\n", ret);
182 return ret;
183 }
184 }
185
186 drm_display_mode_from_videomode(&vm, &crtc_state->adjusted_mode);
187
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300188 return 0;
189}
190
191static const struct drm_encoder_helper_funcs omap_encoder_helper_funcs = {
192 .mode_set = omap_encoder_mode_set,
193 .disable = omap_encoder_disable,
194 .enable = omap_encoder_enable,
195 .atomic_check = omap_encoder_atomic_check,
196};
197
Rob Clarkcd5351f2011-11-12 12:09:40 -0600198/* initialize encoder */
199struct drm_encoder *omap_encoder_init(struct drm_device *dev,
Laurent Pinchartd96aaad2018-05-31 23:14:43 +0300200 struct omap_dss_device *output,
201 struct omap_dss_device *display)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600202{
203 struct drm_encoder *encoder = NULL;
204 struct omap_encoder *omap_encoder;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600205
206 omap_encoder = kzalloc(sizeof(*omap_encoder), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800207 if (!omap_encoder)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600208 goto fail;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600209
Laurent Pinchartd96aaad2018-05-31 23:14:43 +0300210 omap_encoder->output = output;
211 omap_encoder->display = display;
Rob Clarkf5f94542012-12-04 13:59:12 -0600212
Rob Clarkcd5351f2011-11-12 12:09:40 -0600213 encoder = &omap_encoder->base;
214
215 drm_encoder_init(dev, encoder, &omap_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200216 DRM_MODE_ENCODER_TMDS, NULL);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600217 drm_encoder_helper_add(encoder, &omap_encoder_helper_funcs);
218
Rob Clarkcd5351f2011-11-12 12:09:40 -0600219 return encoder;
220
221fail:
YAMANE Toshiaki582bc282012-11-14 19:31:27 +0900222 if (encoder)
Rob Clark65b0bd02011-12-09 23:26:07 -0600223 omap_encoder_destroy(encoder);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600224
225 return NULL;
226}