xref: /linux/drivers/gpu/drm/ast/ast_vga.c (revision 5ce42b5de461c3154f61a023b191dd6b77ee66c0)
1 // SPDX-License-Identifier: MIT
2 
3 #include <drm/drm_atomic_state_helper.h>
4 #include <drm/drm_edid.h>
5 #include <drm/drm_modeset_helper_vtables.h>
6 #include <drm/drm_print.h>
7 #include <drm/drm_probe_helper.h>
8 
9 #include "ast_ddc.h"
10 #include "ast_drv.h"
11 
12 /*
13  * Encoder
14  */
15 
16 static const struct drm_encoder_funcs ast_vga_encoder_funcs = {
17 	.destroy = drm_encoder_cleanup,
18 };
19 
20 /*
21  * Connector
22  */
23 
24 static int ast_vga_connector_helper_get_modes(struct drm_connector *connector)
25 {
26 	struct ast_connector *ast_connector = to_ast_connector(connector);
27 	int count;
28 
29 	if (ast_connector->physical_status == connector_status_connected) {
30 		count = drm_connector_helper_get_modes(connector);
31 	} else {
32 		/*
33 		 * There's no EDID data without a connected monitor. Set BMC-
34 		 * compatible modes in this case. The XGA default resolution
35 		 * should work well for all BMCs.
36 		 */
37 		count = drm_add_modes_noedid(connector, 4096, 4096);
38 		if (count)
39 			drm_set_preferred_mode(connector, 1024, 768);
40 	}
41 
42 	return count;
43 }
44 
45 static int ast_vga_connector_helper_detect_ctx(struct drm_connector *connector,
46 					       struct drm_modeset_acquire_ctx *ctx,
47 					       bool force)
48 {
49 	struct ast_connector *ast_connector = to_ast_connector(connector);
50 	enum drm_connector_status status;
51 
52 	status = drm_connector_helper_detect_from_ddc(connector, ctx, force);
53 
54 	if (status != ast_connector->physical_status)
55 		++connector->epoch_counter;
56 	ast_connector->physical_status = status;
57 
58 	return connector_status_connected;
59 }
60 
61 static const struct drm_connector_helper_funcs ast_vga_connector_helper_funcs = {
62 	.get_modes = ast_vga_connector_helper_get_modes,
63 	.detect_ctx = ast_vga_connector_helper_detect_ctx,
64 };
65 
66 static const struct drm_connector_funcs ast_vga_connector_funcs = {
67 	.reset = drm_atomic_helper_connector_reset,
68 	.fill_modes = drm_helper_probe_single_connector_modes,
69 	.destroy = drm_connector_cleanup,
70 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
71 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
72 };
73 
74 static int ast_vga_connector_init(struct drm_device *dev, struct drm_connector *connector)
75 {
76 	struct ast_device *ast = to_ast_device(dev);
77 	struct i2c_adapter *ddc;
78 	int ret;
79 
80 	ddc = ast_ddc_create(ast);
81 	if (IS_ERR(ddc)) {
82 		ret = PTR_ERR(ddc);
83 		drm_err(dev, "failed to add DDC bus for connector; ret=%d\n", ret);
84 		return ret;
85 	}
86 
87 	ret = drm_connector_init_with_ddc(dev, connector, &ast_vga_connector_funcs,
88 					  DRM_MODE_CONNECTOR_VGA, ddc);
89 	if (ret)
90 		return ret;
91 
92 	drm_connector_helper_add(connector, &ast_vga_connector_helper_funcs);
93 
94 	connector->interlace_allowed = 0;
95 	connector->doublescan_allowed = 0;
96 
97 	connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
98 
99 	return 0;
100 }
101 
102 int ast_vga_output_init(struct ast_device *ast)
103 {
104 	struct drm_device *dev = &ast->base;
105 	struct drm_crtc *crtc = &ast->crtc;
106 	struct drm_encoder *encoder = &ast->output.vga.encoder;
107 	struct ast_connector *ast_connector = &ast->output.vga.connector;
108 	struct drm_connector *connector = &ast_connector->base;
109 	int ret;
110 
111 	ret = drm_encoder_init(dev, encoder, &ast_vga_encoder_funcs,
112 			       DRM_MODE_ENCODER_DAC, NULL);
113 	if (ret)
114 		return ret;
115 	encoder->possible_crtcs = drm_crtc_mask(crtc);
116 
117 	ret = ast_vga_connector_init(dev, connector);
118 	if (ret)
119 		return ret;
120 	ast_connector->physical_status = connector->status;
121 
122 	ret = drm_connector_attach_encoder(connector, encoder);
123 	if (ret)
124 		return ret;
125 
126 	return 0;
127 }
128