1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2012 Red Hat
4 * based in parts on udlfb.c:
5 * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
6 * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
7 * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
8 */
9
10 #include <drm/drm_atomic_state_helper.h>
11 #include <drm/drm_edid.h>
12 #include <drm/drm_crtc_helper.h>
13 #include <drm/drm_probe_helper.h>
14
15 #include "udl_connector.h"
16 #include "udl_drv.h"
17
udl_get_edid_block(void * data,u8 * buf,unsigned int block,size_t len)18 static int udl_get_edid_block(void *data, u8 *buf, unsigned int block,
19 size_t len)
20 {
21 int ret, i;
22 u8 *read_buff;
23 struct udl_device *udl = data;
24 struct usb_device *udev = udl_to_usb_device(udl);
25
26 read_buff = kmalloc(2, GFP_KERNEL);
27 if (!read_buff)
28 return -1;
29
30 for (i = 0; i < len; i++) {
31 int bval = (i + block * EDID_LENGTH) << 8;
32 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
33 0x02, (0x80 | (0x02 << 5)), bval,
34 0xA1, read_buff, 2, 1000);
35 if (ret < 1) {
36 DRM_ERROR("Read EDID byte %d failed err %x\n", i, ret);
37 kfree(read_buff);
38 return -1;
39 }
40 buf[i] = read_buff[1];
41 }
42
43 kfree(read_buff);
44 return 0;
45 }
46
udl_get_modes(struct drm_connector * connector)47 static int udl_get_modes(struct drm_connector *connector)
48 {
49 struct udl_drm_connector *udl_connector =
50 container_of(connector,
51 struct udl_drm_connector,
52 connector);
53
54 drm_connector_update_edid_property(connector, udl_connector->edid);
55 if (udl_connector->edid)
56 return drm_add_edid_modes(connector, udl_connector->edid);
57 return 0;
58 }
59
udl_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)60 static enum drm_mode_status udl_mode_valid(struct drm_connector *connector,
61 struct drm_display_mode *mode)
62 {
63 struct udl_device *udl = to_udl(connector->dev);
64 if (!udl->sku_pixel_limit)
65 return 0;
66
67 if (mode->vdisplay * mode->hdisplay > udl->sku_pixel_limit)
68 return MODE_VIRTUAL_Y;
69
70 return 0;
71 }
72
73 static enum drm_connector_status
udl_detect(struct drm_connector * connector,bool force)74 udl_detect(struct drm_connector *connector, bool force)
75 {
76 struct udl_device *udl = to_udl(connector->dev);
77 struct udl_drm_connector *udl_connector =
78 container_of(connector,
79 struct udl_drm_connector,
80 connector);
81
82 /* cleanup previous edid */
83 if (udl_connector->edid != NULL) {
84 kfree(udl_connector->edid);
85 udl_connector->edid = NULL;
86 }
87
88 udl_connector->edid = drm_do_get_edid(connector, udl_get_edid_block, udl);
89 if (!udl_connector->edid)
90 return connector_status_disconnected;
91
92 return connector_status_connected;
93 }
94
udl_connector_destroy(struct drm_connector * connector)95 static void udl_connector_destroy(struct drm_connector *connector)
96 {
97 struct udl_drm_connector *udl_connector =
98 container_of(connector,
99 struct udl_drm_connector,
100 connector);
101
102 drm_connector_cleanup(connector);
103 kfree(udl_connector->edid);
104 kfree(connector);
105 }
106
107 static const struct drm_connector_helper_funcs udl_connector_helper_funcs = {
108 .get_modes = udl_get_modes,
109 .mode_valid = udl_mode_valid,
110 };
111
112 static const struct drm_connector_funcs udl_connector_funcs = {
113 .reset = drm_atomic_helper_connector_reset,
114 .detect = udl_detect,
115 .fill_modes = drm_helper_probe_single_connector_modes,
116 .destroy = udl_connector_destroy,
117 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
118 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
119 };
120
udl_connector_init(struct drm_device * dev)121 struct drm_connector *udl_connector_init(struct drm_device *dev)
122 {
123 struct udl_drm_connector *udl_connector;
124 struct drm_connector *connector;
125
126 udl_connector = kzalloc(sizeof(struct udl_drm_connector), GFP_KERNEL);
127 if (!udl_connector)
128 return ERR_PTR(-ENOMEM);
129
130 connector = &udl_connector->connector;
131 drm_connector_init(dev, connector, &udl_connector_funcs,
132 DRM_MODE_CONNECTOR_VGA);
133 drm_connector_helper_add(connector, &udl_connector_helper_funcs);
134
135 connector->polled = DRM_CONNECTOR_POLL_HPD |
136 DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
137
138 return connector;
139 }
140