Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / gpu / drm / omapdrm / omap_connector.c
bloba0d7b1d905e85500cc011d76fefb19453384a7fe
1 /*
2 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
3 * Author: Rob Clark <rob@ti.com>
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.
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.
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/>.
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/drm_crtc.h>
20 #include <drm/drm_crtc_helper.h>
22 #include "omap_drv.h"
25 * connector funcs
28 #define to_omap_connector(x) container_of(x, struct omap_connector, base)
30 struct omap_connector {
31 struct drm_connector base;
32 struct omap_dss_device *dssdev;
33 bool hdmi_mode;
36 static void omap_connector_hpd_cb(void *cb_data,
37 enum drm_connector_status status)
39 struct omap_connector *omap_connector = cb_data;
40 struct drm_connector *connector = &omap_connector->base;
41 struct drm_device *dev = connector->dev;
42 enum drm_connector_status old_status;
44 mutex_lock(&dev->mode_config.mutex);
45 old_status = connector->status;
46 connector->status = status;
47 mutex_unlock(&dev->mode_config.mutex);
49 if (old_status != status)
50 drm_kms_helper_hotplug_event(dev);
53 bool omap_connector_get_hdmi_mode(struct drm_connector *connector)
55 struct omap_connector *omap_connector = to_omap_connector(connector);
57 return omap_connector->hdmi_mode;
60 static enum drm_connector_status omap_connector_detect(
61 struct drm_connector *connector, bool force)
63 struct omap_connector *omap_connector = to_omap_connector(connector);
64 struct omap_dss_device *dssdev = omap_connector->dssdev;
65 struct omap_dss_driver *dssdrv = dssdev->driver;
66 enum drm_connector_status ret;
68 if (dssdrv->detect) {
69 if (dssdrv->detect(dssdev))
70 ret = connector_status_connected;
71 else
72 ret = connector_status_disconnected;
73 } else if (dssdev->type == OMAP_DISPLAY_TYPE_DPI ||
74 dssdev->type == OMAP_DISPLAY_TYPE_DBI ||
75 dssdev->type == OMAP_DISPLAY_TYPE_SDI ||
76 dssdev->type == OMAP_DISPLAY_TYPE_DSI) {
77 ret = connector_status_connected;
78 } else {
79 ret = connector_status_unknown;
82 VERB("%s: %d (force=%d)", omap_connector->dssdev->name, ret, force);
84 return ret;
87 static void omap_connector_destroy(struct drm_connector *connector)
89 struct omap_connector *omap_connector = to_omap_connector(connector);
90 struct omap_dss_device *dssdev = omap_connector->dssdev;
92 DBG("%s", omap_connector->dssdev->name);
93 if (connector->polled == DRM_CONNECTOR_POLL_HPD &&
94 dssdev->driver->unregister_hpd_cb) {
95 dssdev->driver->unregister_hpd_cb(dssdev);
97 drm_connector_unregister(connector);
98 drm_connector_cleanup(connector);
99 kfree(omap_connector);
101 omap_dss_put_device(dssdev);
104 #define MAX_EDID 512
106 static int omap_connector_get_modes(struct drm_connector *connector)
108 struct omap_connector *omap_connector = to_omap_connector(connector);
109 struct omap_dss_device *dssdev = omap_connector->dssdev;
110 struct omap_dss_driver *dssdrv = dssdev->driver;
111 struct drm_device *dev = connector->dev;
112 int n = 0;
114 DBG("%s", omap_connector->dssdev->name);
116 /* if display exposes EDID, then we parse that in the normal way to
117 * build table of supported modes.. otherwise (ie. fixed resolution
118 * LCD panels) we just return a single mode corresponding to the
119 * currently configured timings:
121 if (dssdrv->read_edid) {
122 void *edid = kzalloc(MAX_EDID, GFP_KERNEL);
124 if ((dssdrv->read_edid(dssdev, edid, MAX_EDID) > 0) &&
125 drm_edid_is_valid(edid)) {
126 drm_mode_connector_update_edid_property(
127 connector, edid);
128 n = drm_add_edid_modes(connector, edid);
130 omap_connector->hdmi_mode =
131 drm_detect_hdmi_monitor(edid);
132 } else {
133 drm_mode_connector_update_edid_property(
134 connector, NULL);
137 kfree(edid);
138 } else {
139 struct drm_display_mode *mode = drm_mode_create(dev);
140 struct videomode vm = {0};
142 dssdrv->get_timings(dssdev, &vm);
144 drm_display_mode_from_videomode(&vm, mode);
146 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
147 drm_mode_set_name(mode);
148 drm_mode_probed_add(connector, mode);
150 if (dssdrv->get_size) {
151 dssdrv->get_size(dssdev,
152 &connector->display_info.width_mm,
153 &connector->display_info.height_mm);
156 n = 1;
159 return n;
162 static int omap_connector_mode_valid(struct drm_connector *connector,
163 struct drm_display_mode *mode)
165 struct omap_connector *omap_connector = to_omap_connector(connector);
166 struct omap_dss_device *dssdev = omap_connector->dssdev;
167 struct omap_dss_driver *dssdrv = dssdev->driver;
168 struct videomode vm = {0};
169 struct drm_device *dev = connector->dev;
170 struct drm_display_mode *new_mode;
171 int r, ret = MODE_BAD;
173 drm_display_mode_to_videomode(mode, &vm);
174 mode->vrefresh = drm_mode_vrefresh(mode);
177 * if the panel driver doesn't have a check_timings, it's most likely
178 * a fixed resolution panel, check if the timings match with the
179 * panel's timings
181 if (dssdrv->check_timings) {
182 r = dssdrv->check_timings(dssdev, &vm);
183 } else {
184 struct videomode t = {0};
186 dssdrv->get_timings(dssdev, &t);
189 * Ignore the flags, as we don't get them from
190 * drm_display_mode_to_videomode.
192 t.flags = 0;
194 if (memcmp(&vm, &t, sizeof(vm)))
195 r = -EINVAL;
196 else
197 r = 0;
200 if (!r) {
201 /* check if vrefresh is still valid */
202 new_mode = drm_mode_duplicate(dev, mode);
203 new_mode->clock = vm.pixelclock / 1000;
204 new_mode->vrefresh = 0;
205 if (mode->vrefresh == drm_mode_vrefresh(new_mode))
206 ret = MODE_OK;
207 drm_mode_destroy(dev, new_mode);
210 DBG("connector: mode %s: "
211 "%d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
212 (ret == MODE_OK) ? "valid" : "invalid",
213 mode->base.id, mode->name, mode->vrefresh, mode->clock,
214 mode->hdisplay, mode->hsync_start,
215 mode->hsync_end, mode->htotal,
216 mode->vdisplay, mode->vsync_start,
217 mode->vsync_end, mode->vtotal, mode->type, mode->flags);
219 return ret;
222 static const struct drm_connector_funcs omap_connector_funcs = {
223 .reset = drm_atomic_helper_connector_reset,
224 .detect = omap_connector_detect,
225 .fill_modes = drm_helper_probe_single_connector_modes,
226 .destroy = omap_connector_destroy,
227 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
228 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
231 static const struct drm_connector_helper_funcs omap_connector_helper_funcs = {
232 .get_modes = omap_connector_get_modes,
233 .mode_valid = omap_connector_mode_valid,
236 /* initialize connector */
237 struct drm_connector *omap_connector_init(struct drm_device *dev,
238 int connector_type, struct omap_dss_device *dssdev,
239 struct drm_encoder *encoder)
241 struct drm_connector *connector = NULL;
242 struct omap_connector *omap_connector;
243 bool hpd_supported = false;
245 DBG("%s", dssdev->name);
247 omap_dss_get_device(dssdev);
249 omap_connector = kzalloc(sizeof(*omap_connector), GFP_KERNEL);
250 if (!omap_connector)
251 goto fail;
253 omap_connector->dssdev = dssdev;
255 connector = &omap_connector->base;
257 drm_connector_init(dev, connector, &omap_connector_funcs,
258 connector_type);
259 drm_connector_helper_add(connector, &omap_connector_helper_funcs);
261 if (dssdev->driver->register_hpd_cb) {
262 int ret = dssdev->driver->register_hpd_cb(dssdev,
263 omap_connector_hpd_cb,
264 omap_connector);
265 if (!ret)
266 hpd_supported = true;
267 else if (ret != -ENOTSUPP)
268 DBG("%s: Failed to register HPD callback (%d).",
269 dssdev->name, ret);
272 if (hpd_supported)
273 connector->polled = DRM_CONNECTOR_POLL_HPD;
274 else if (dssdev->driver->detect)
275 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
276 DRM_CONNECTOR_POLL_DISCONNECT;
277 else
278 connector->polled = 0;
280 connector->interlace_allowed = 1;
281 connector->doublescan_allowed = 0;
283 return connector;
285 fail:
286 if (connector)
287 omap_connector_destroy(connector);
289 return NULL;