blob: 624f5b50b75518dd3f094efbc2c943e9e7dc3658 [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
Rob Clarkcd5351f2011-11-12 12:09:40 -060055 drm_encoder_cleanup(encoder);
56 kfree(omap_encoder);
57}
58
Rob Clarkf5f94542012-12-04 13:59:12 -060059static const struct drm_encoder_funcs omap_encoder_funcs = {
60 .destroy = omap_encoder_destroy,
61};
62
Rob Clarkcd5351f2011-11-12 12:09:40 -060063static void omap_encoder_mode_set(struct drm_encoder *encoder,
64 struct drm_display_mode *mode,
65 struct drm_display_mode *adjusted_mode)
66{
Tomi Valkeinen4f930c02014-06-18 14:19:48 +030067 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
Shashank Sharma0c1f5282017-07-13 21:03:07 +053088 r = drm_hdmi_avi_infoframe_from_display_mode(&avi, adjusted_mode,
89 false);
Tomi Valkeinen4f930c02014-06-18 14:19:48 +030090 if (r == 0)
91 dssdev->driver->set_hdmi_infoframe(dssdev, &avi);
92 }
Rob Clarkcd5351f2011-11-12 12:09:40 -060093}
94
Laurent Pinchart68dc0392015-03-07 00:22:39 +020095static void omap_encoder_disable(struct drm_encoder *encoder)
96{
Rob Clarkcd5351f2011-11-12 12:09:40 -060097 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
Rob Clarkf5f94542012-12-04 13:59:12 -060098 struct omap_dss_device *dssdev = omap_encoder->dssdev;
99 struct omap_dss_driver *dssdrv = dssdev->driver;
100
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300101 dssdrv->disable(dssdev);
Rob Clarkf5f94542012-12-04 13:59:12 -0600102}
103
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300104static int omap_encoder_update(struct drm_encoder *encoder,
105 enum omap_channel channel,
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300106 struct videomode *vm)
Rob Clarkf5f94542012-12-04 13:59:12 -0600107{
108 struct drm_device *dev = encoder->dev;
109 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
110 struct omap_dss_device *dssdev = omap_encoder->dssdev;
111 struct omap_dss_driver *dssdrv = dssdev->driver;
112 int ret;
113
Archit Tanejabddabbe2013-03-26 19:15:20 +0530114 if (dssdrv->check_timings) {
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300115 ret = dssdrv->check_timings(dssdev, vm);
Archit Tanejabddabbe2013-03-26 19:15:20 +0530116 } else {
Peter Ujfalusi4520ff22016-09-22 14:07:03 +0300117 struct videomode t = {0};
Archit Tanejabddabbe2013-03-26 19:15:20 +0530118
119 dssdrv->get_timings(dssdev, &t);
120
Laurent Pinchartd501b122016-12-12 11:57:24 +0200121 if (memcmp(vm, &t, sizeof(*vm)))
Archit Tanejabddabbe2013-03-26 19:15:20 +0530122 ret = -EINVAL;
123 else
124 ret = 0;
125 }
126
Rob Clarkf5f94542012-12-04 13:59:12 -0600127 if (ret) {
128 dev_err(dev->dev, "could not set timings: %d\n", ret);
129 return ret;
130 }
131
Archit Tanejabddabbe2013-03-26 19:15:20 +0530132 if (dssdrv->set_timings)
Peter Ujfalusida11bbbb2016-09-22 14:07:04 +0300133 dssdrv->set_timings(dssdev, vm);
Rob Clarkf5f94542012-12-04 13:59:12 -0600134
135 return 0;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600136}
137
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300138static void omap_encoder_enable(struct drm_encoder *encoder)
139{
140 struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
141 struct omap_dss_device *dssdev = omap_encoder->dssdev;
142 struct omap_dss_driver *dssdrv = dssdev->driver;
Tomi Valkeinen8d83bbd2016-01-05 11:43:16 +0200143 int r;
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300144
145 omap_encoder_update(encoder, omap_crtc_channel(encoder->crtc),
146 omap_crtc_timings(encoder->crtc));
147
Tomi Valkeinen8d83bbd2016-01-05 11:43:16 +0200148 r = dssdrv->enable(dssdev);
149 if (r)
150 dev_err(encoder->dev->dev,
151 "Failed to enable display '%s': %d\n",
152 dssdev->name, r);
Laurent Pinchart4029755e2015-05-28 02:34:05 +0300153}
154
155static int omap_encoder_atomic_check(struct drm_encoder *encoder,
156 struct drm_crtc_state *crtc_state,
157 struct drm_connector_state *conn_state)
158{
159 return 0;
160}
161
162static const struct drm_encoder_helper_funcs omap_encoder_helper_funcs = {
163 .mode_set = omap_encoder_mode_set,
164 .disable = omap_encoder_disable,
165 .enable = omap_encoder_enable,
166 .atomic_check = omap_encoder_atomic_check,
167};
168
Rob Clarkcd5351f2011-11-12 12:09:40 -0600169/* initialize encoder */
170struct drm_encoder *omap_encoder_init(struct drm_device *dev,
Rob Clarkf5f94542012-12-04 13:59:12 -0600171 struct omap_dss_device *dssdev)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600172{
173 struct drm_encoder *encoder = NULL;
174 struct omap_encoder *omap_encoder;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600175
176 omap_encoder = kzalloc(sizeof(*omap_encoder), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800177 if (!omap_encoder)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600178 goto fail;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600179
Rob Clarkf5f94542012-12-04 13:59:12 -0600180 omap_encoder->dssdev = dssdev;
181
Rob Clarkcd5351f2011-11-12 12:09:40 -0600182 encoder = &omap_encoder->base;
183
184 drm_encoder_init(dev, encoder, &omap_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200185 DRM_MODE_ENCODER_TMDS, NULL);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600186 drm_encoder_helper_add(encoder, &omap_encoder_helper_funcs);
187
Rob Clarkcd5351f2011-11-12 12:09:40 -0600188 return encoder;
189
190fail:
YAMANE Toshiaki582bc282012-11-14 19:31:27 +0900191 if (encoder)
Rob Clark65b0bd02011-12-09 23:26:07 -0600192 omap_encoder_destroy(encoder);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600193
194 return NULL;
195}