udldrmfb: Fix EDID not working with monitors with EDID extension blocks
[linux-2.6.git] / drivers / gpu / drm / udl / udl_connector.c
1 /*
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
13 #include "drmP.h"
14 #include "drm_crtc.h"
15 #include "drm_edid.h"
16 #include "drm_crtc_helper.h"
17 #include "udl_drv.h"
18
19 /* dummy connector to just get EDID,
20    all UDL appear to have a DVI-D */
21
22 static u8 *udl_get_edid(struct udl_device *udl)
23 {
24         u8 *block;
25         char rbuf[3];
26         int ret, i;
27
28         block = kmalloc(EDID_LENGTH, GFP_KERNEL);
29         if (block == NULL)
30                 return NULL;
31
32         for (i = 0; i < EDID_LENGTH; i++) {
33                 ret = usb_control_msg(udl->ddev->usbdev,
34                                       usb_rcvctrlpipe(udl->ddev->usbdev, 0), (0x02),
35                                       (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
36                                       HZ);
37                 if (ret < 1) {
38                         DRM_ERROR("Read EDID byte %d failed err %x\n", i, ret);
39                         i--;
40                         goto error;
41                 }
42                 block[i] = rbuf[1];
43         }
44
45         return block;
46
47 error:
48         kfree(block);
49         return NULL;
50 }
51
52 static int udl_get_modes(struct drm_connector *connector)
53 {
54         struct udl_device *udl = connector->dev->dev_private;
55         struct edid *edid;
56         int ret;
57
58         edid = (struct edid *)udl_get_edid(udl);
59
60         connector->display_info.raw_edid = (char *)edid;
61
62         /*
63          * We only read the main block, but if the monitor reports extension
64          * blocks then the drm edid code expects them to be present, so patch
65          * the extension count to 0.
66          */
67         edid->checksum += edid->extensions;
68         edid->extensions = 0;
69
70         drm_mode_connector_update_edid_property(connector, edid);
71         ret = drm_add_edid_modes(connector, edid);
72         connector->display_info.raw_edid = NULL;
73         kfree(edid);
74         return ret;
75 }
76
77 static int udl_mode_valid(struct drm_connector *connector,
78                           struct drm_display_mode *mode)
79 {
80         struct udl_device *udl = connector->dev->dev_private;
81         if (!udl->sku_pixel_limit)
82                 return 0;
83
84         if (mode->vdisplay * mode->hdisplay > udl->sku_pixel_limit)
85                 return MODE_VIRTUAL_Y;
86
87         return 0;
88 }
89
90 static enum drm_connector_status
91 udl_detect(struct drm_connector *connector, bool force)
92 {
93         if (drm_device_is_unplugged(connector->dev))
94                 return connector_status_disconnected;
95         return connector_status_connected;
96 }
97
98 struct drm_encoder *udl_best_single_encoder(struct drm_connector *connector)
99 {
100         int enc_id = connector->encoder_ids[0];
101         struct drm_mode_object *obj;
102         struct drm_encoder *encoder;
103
104         obj = drm_mode_object_find(connector->dev, enc_id, DRM_MODE_OBJECT_ENCODER);
105         if (!obj)
106                 return NULL;
107         encoder = obj_to_encoder(obj);
108         return encoder;
109 }
110
111 int udl_connector_set_property(struct drm_connector *connector, struct drm_property *property,
112                                uint64_t val)
113 {
114         return 0;
115 }
116
117 static void udl_connector_destroy(struct drm_connector *connector)
118 {
119         drm_sysfs_connector_remove(connector);
120         drm_connector_cleanup(connector);
121         kfree(connector);
122 }
123
124 struct drm_connector_helper_funcs udl_connector_helper_funcs = {
125         .get_modes = udl_get_modes,
126         .mode_valid = udl_mode_valid,
127         .best_encoder = udl_best_single_encoder,
128 };
129
130 struct drm_connector_funcs udl_connector_funcs = {
131         .dpms = drm_helper_connector_dpms,
132         .detect = udl_detect,
133         .fill_modes = drm_helper_probe_single_connector_modes,
134         .destroy = udl_connector_destroy,
135         .set_property = udl_connector_set_property,
136 };
137
138 int udl_connector_init(struct drm_device *dev, struct drm_encoder *encoder)
139 {
140         struct drm_connector *connector;
141
142         connector = kzalloc(sizeof(struct drm_connector), GFP_KERNEL);
143         if (!connector)
144                 return -ENOMEM;
145
146         drm_connector_init(dev, connector, &udl_connector_funcs, DRM_MODE_CONNECTOR_DVII);
147         drm_connector_helper_add(connector, &udl_connector_helper_funcs);
148
149         drm_sysfs_connector_add(connector);
150         drm_mode_connector_attach_encoder(connector, encoder);
151
152         drm_connector_attach_property(connector,
153                                       dev->mode_config.dirty_info_property,
154                                       1);
155         return 0;
156 }