blob: 353bc1ea6f68540e5e14d538b2c6f76d6625d437 [file] [log] [blame]
Dave Airlie53209182010-12-15 07:14:24 +10001/*
2 * Copyright (C) 2012 Red Hat
3 * based in parts on udlfb.c:
4 * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
5 * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
6 * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
7 *
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License v2. See the file COPYING in the main directory of this archive for
10 * more details.
11 */
12
David Howells760285e2012-10-02 18:01:07 +010013#include <drm/drmP.h>
14#include <drm/drm_crtc.h>
15#include <drm/drm_edid.h>
16#include <drm/drm_crtc_helper.h>
Dave Airlie53209182010-12-15 07:14:24 +100017#include "udl_drv.h"
18
19/* dummy connector to just get EDID,
20 all UDL appear to have a DVI-D */
21
22static u8 *udl_get_edid(struct udl_device *udl)
23{
24 u8 *block;
Hans de Goede242187b2013-01-11 12:08:57 +010025 char *rbuf;
Dave Airlie53209182010-12-15 07:14:24 +100026 int ret, i;
27
28 block = kmalloc(EDID_LENGTH, GFP_KERNEL);
29 if (block == NULL)
30 return NULL;
31
Hans de Goede242187b2013-01-11 12:08:57 +010032 rbuf = kmalloc(2, GFP_KERNEL);
33 if (rbuf == NULL)
34 goto error;
35
Dave Airlie53209182010-12-15 07:14:24 +100036 for (i = 0; i < EDID_LENGTH; i++) {
37 ret = usb_control_msg(udl->ddev->usbdev,
38 usb_rcvctrlpipe(udl->ddev->usbdev, 0), (0x02),
39 (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
40 HZ);
41 if (ret < 1) {
42 DRM_ERROR("Read EDID byte %d failed err %x\n", i, ret);
43 i--;
44 goto error;
45 }
46 block[i] = rbuf[1];
47 }
48
Hans de Goede242187b2013-01-11 12:08:57 +010049 kfree(rbuf);
Dave Airlie53209182010-12-15 07:14:24 +100050 return block;
51
52error:
53 kfree(block);
Hans de Goede242187b2013-01-11 12:08:57 +010054 kfree(rbuf);
Dave Airlie53209182010-12-15 07:14:24 +100055 return NULL;
56}
57
58static int udl_get_modes(struct drm_connector *connector)
59{
60 struct udl_device *udl = connector->dev->dev_private;
61 struct edid *edid;
62 int ret;
63
64 edid = (struct edid *)udl_get_edid(udl);
65
Hans de Goedec9308122013-01-11 12:08:56 +010066 /*
67 * We only read the main block, but if the monitor reports extension
68 * blocks then the drm edid code expects them to be present, so patch
69 * the extension count to 0.
70 */
71 edid->checksum += edid->extensions;
72 edid->extensions = 0;
73
Dave Airlie53209182010-12-15 07:14:24 +100074 drm_mode_connector_update_edid_property(connector, edid);
75 ret = drm_add_edid_modes(connector, edid);
Dave Airlie53209182010-12-15 07:14:24 +100076 kfree(edid);
77 return ret;
78}
79
80static int udl_mode_valid(struct drm_connector *connector,
81 struct drm_display_mode *mode)
82{
Dave Airlie3a758852012-09-25 16:17:43 +100083 struct udl_device *udl = connector->dev->dev_private;
84 if (!udl->sku_pixel_limit)
85 return 0;
86
87 if (mode->vdisplay * mode->hdisplay > udl->sku_pixel_limit)
88 return MODE_VIRTUAL_Y;
89
Dave Airlie53209182010-12-15 07:14:24 +100090 return 0;
91}
92
93static enum drm_connector_status
94udl_detect(struct drm_connector *connector, bool force)
95{
96 if (drm_device_is_unplugged(connector->dev))
97 return connector_status_disconnected;
98 return connector_status_connected;
99}
100
Sachin Kamatb27b6d32012-11-19 05:27:43 +0000101static struct drm_encoder*
102udl_best_single_encoder(struct drm_connector *connector)
Dave Airlie53209182010-12-15 07:14:24 +1000103{
104 int enc_id = connector->encoder_ids[0];
105 struct drm_mode_object *obj;
106 struct drm_encoder *encoder;
107
108 obj = drm_mode_object_find(connector->dev, enc_id, DRM_MODE_OBJECT_ENCODER);
109 if (!obj)
110 return NULL;
111 encoder = obj_to_encoder(obj);
112 return encoder;
113}
114
Sachin Kamatb27b6d32012-11-19 05:27:43 +0000115static int udl_connector_set_property(struct drm_connector *connector,
116 struct drm_property *property,
117 uint64_t val)
Dave Airlie53209182010-12-15 07:14:24 +1000118{
119 return 0;
120}
121
122static void udl_connector_destroy(struct drm_connector *connector)
123{
124 drm_sysfs_connector_remove(connector);
125 drm_connector_cleanup(connector);
126 kfree(connector);
127}
128
Sachin Kamatb27b6d32012-11-19 05:27:43 +0000129static struct drm_connector_helper_funcs udl_connector_helper_funcs = {
Dave Airlie53209182010-12-15 07:14:24 +1000130 .get_modes = udl_get_modes,
131 .mode_valid = udl_mode_valid,
132 .best_encoder = udl_best_single_encoder,
133};
134
Sachin Kamatb27b6d32012-11-19 05:27:43 +0000135static struct drm_connector_funcs udl_connector_funcs = {
Dave Airlie53209182010-12-15 07:14:24 +1000136 .dpms = drm_helper_connector_dpms,
137 .detect = udl_detect,
138 .fill_modes = drm_helper_probe_single_connector_modes,
139 .destroy = udl_connector_destroy,
140 .set_property = udl_connector_set_property,
141};
142
143int udl_connector_init(struct drm_device *dev, struct drm_encoder *encoder)
144{
145 struct drm_connector *connector;
146
147 connector = kzalloc(sizeof(struct drm_connector), GFP_KERNEL);
148 if (!connector)
149 return -ENOMEM;
150
151 drm_connector_init(dev, connector, &udl_connector_funcs, DRM_MODE_CONNECTOR_DVII);
152 drm_connector_helper_add(connector, &udl_connector_helper_funcs);
153
154 drm_sysfs_connector_add(connector);
155 drm_mode_connector_attach_encoder(connector, encoder);
156
Rob Clark99d1dba2012-10-11 20:46:48 -0500157 drm_object_attach_property(&connector->base,
Dave Airlie53209182010-12-15 07:14:24 +1000158 dev->mode_config.dirty_info_property,
159 1);
160 return 0;
161}