xref: /openbmc/linux/drivers/gpu/drm/msm/dp/dp_drm.c (revision 1f012283)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2017-2020, The Linux Foundation. All rights reserved.
4  */
5 
6 #include <drm/drm_atomic_helper.h>
7 #include <drm/drm_atomic.h>
8 #include <drm/drm_bridge.h>
9 #include <drm/drm_crtc.h>
10 
11 #include "msm_drv.h"
12 #include "msm_kms.h"
13 #include "dp_drm.h"
14 
15 struct dp_connector {
16 	struct drm_connector base;
17 	struct msm_dp *dp_display;
18 };
19 #define to_dp_connector(x) container_of(x, struct dp_connector, base)
20 
21 /**
22  * dp_connector_detect - callback to determine if connector is connected
23  * @conn: Pointer to drm connector structure
24  * @force: Force detect setting from drm framework
25  * Returns: Connector 'is connected' status
26  */
27 static enum drm_connector_status dp_connector_detect(struct drm_connector *conn,
28 		bool force)
29 {
30 	struct msm_dp *dp;
31 
32 	dp = to_dp_connector(conn)->dp_display;
33 
34 	DRM_DEBUG_DP("is_connected = %s\n",
35 		(dp->is_connected) ? "true" : "false");
36 
37 	return (dp->is_connected) ? connector_status_connected :
38 					connector_status_disconnected;
39 }
40 
41 /**
42  * dp_connector_get_modes - callback to add drm modes via drm_mode_probed_add()
43  * @connector: Pointer to drm connector structure
44  * Returns: Number of modes added
45  */
46 static int dp_connector_get_modes(struct drm_connector *connector)
47 {
48 	int rc = 0;
49 	struct msm_dp *dp;
50 	struct dp_display_mode *dp_mode = NULL;
51 	struct drm_display_mode *m, drm_mode;
52 
53 	if (!connector)
54 		return 0;
55 
56 	dp = to_dp_connector(connector)->dp_display;
57 
58 	dp_mode = kzalloc(sizeof(*dp_mode),  GFP_KERNEL);
59 	if (!dp_mode)
60 		return 0;
61 
62 	/* pluggable case assumes EDID is read when HPD */
63 	if (dp->is_connected) {
64 		/*
65 		 *The get_modes() function might return one mode that is stored
66 		 * in dp_mode when compliance test is in progress. If not, the
67 		 * return value is equal to the total number of modes supported
68 		 * by the sink
69 		 */
70 		rc = dp_display_get_modes(dp, dp_mode);
71 		if (rc <= 0) {
72 			DRM_ERROR("failed to get DP sink modes, rc=%d\n", rc);
73 			kfree(dp_mode);
74 			return rc;
75 		}
76 		if (dp_mode->drm_mode.clock) { /* valid DP mode */
77 			memset(&drm_mode, 0x0, sizeof(drm_mode));
78 			drm_mode_copy(&drm_mode, &dp_mode->drm_mode);
79 			m = drm_mode_duplicate(connector->dev, &drm_mode);
80 			if (!m) {
81 				DRM_ERROR("failed to add mode %ux%u\n",
82 				       drm_mode.hdisplay,
83 				       drm_mode.vdisplay);
84 				kfree(dp_mode);
85 				return 0;
86 			}
87 			drm_mode_probed_add(connector, m);
88 		}
89 	} else {
90 		DRM_DEBUG_DP("No sink connected\n");
91 	}
92 	kfree(dp_mode);
93 	return rc;
94 }
95 
96 /**
97  * dp_connector_mode_valid - callback to determine if specified mode is valid
98  * @connector: Pointer to drm connector structure
99  * @mode: Pointer to drm mode structure
100  * Returns: Validity status for specified mode
101  */
102 static enum drm_mode_status dp_connector_mode_valid(
103 		struct drm_connector *connector,
104 		struct drm_display_mode *mode)
105 {
106 	struct msm_dp *dp_disp;
107 
108 	dp_disp = to_dp_connector(connector)->dp_display;
109 
110 	if ((dp_disp->max_pclk_khz <= 0) ||
111 			(dp_disp->max_pclk_khz > DP_MAX_PIXEL_CLK_KHZ) ||
112 			(mode->clock > dp_disp->max_pclk_khz))
113 		return MODE_BAD;
114 
115 	return dp_display_validate_mode(dp_disp, mode->clock);
116 }
117 
118 static const struct drm_connector_funcs dp_connector_funcs = {
119 	.detect = dp_connector_detect,
120 	.fill_modes = drm_helper_probe_single_connector_modes,
121 	.destroy = drm_connector_cleanup,
122 	.reset = drm_atomic_helper_connector_reset,
123 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
124 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
125 };
126 
127 static const struct drm_connector_helper_funcs dp_connector_helper_funcs = {
128 	.get_modes = dp_connector_get_modes,
129 	.mode_valid = dp_connector_mode_valid,
130 };
131 
132 /* connector initialization */
133 struct drm_connector *dp_drm_connector_init(struct msm_dp *dp_display)
134 {
135 	struct drm_connector *connector = NULL;
136 	struct dp_connector *dp_connector;
137 	int ret;
138 
139 	dp_connector = devm_kzalloc(dp_display->drm_dev->dev,
140 					sizeof(*dp_connector),
141 					GFP_KERNEL);
142 	if (!dp_connector)
143 		return ERR_PTR(-ENOMEM);
144 
145 	dp_connector->dp_display = dp_display;
146 
147 	connector = &dp_connector->base;
148 
149 	ret = drm_connector_init(dp_display->drm_dev, connector,
150 			&dp_connector_funcs,
151 			dp_display->connector_type);
152 	if (ret)
153 		return ERR_PTR(ret);
154 
155 	drm_connector_helper_add(connector, &dp_connector_helper_funcs);
156 
157 	/*
158 	 * Enable HPD to let hpd event is handled when cable is connected.
159 	 */
160 	connector->polled = DRM_CONNECTOR_POLL_HPD;
161 
162 	drm_connector_attach_encoder(connector, dp_display->encoder);
163 
164 	if (dp_display->panel_bridge) {
165 		ret = drm_bridge_attach(dp_display->encoder,
166 					dp_display->panel_bridge, NULL,
167 					DRM_BRIDGE_ATTACH_NO_CONNECTOR);
168 		if (ret < 0) {
169 			DRM_ERROR("failed to attach panel bridge: %d\n", ret);
170 			return ERR_PTR(ret);
171 		}
172 	}
173 
174 	return connector;
175 }
176