1 // SPDX-License-Identifier: GPL-2.0-only
2
3 #include <drm/drm_atomic_helper.h>
4 #include <drm/drm_edid.h>
5 #include <drm/drm_modeset_helper_vtables.h>
6 #include <drm/drm_probe_helper.h>
7
8 #include "mgag200_ddc.h"
9 #include "mgag200_drv.h"
10
mgag200_vga_bmc_encoder_atomic_disable(struct drm_encoder * encoder,struct drm_atomic_state * state)11 static void mgag200_vga_bmc_encoder_atomic_disable(struct drm_encoder *encoder,
12 struct drm_atomic_state *state)
13 {
14 struct mga_device *mdev = to_mga_device(encoder->dev);
15
16 if (mdev->info->sync_bmc)
17 mgag200_bmc_stop_scanout(mdev);
18 }
19
mgag200_vga_bmc_encoder_atomic_enable(struct drm_encoder * encoder,struct drm_atomic_state * state)20 static void mgag200_vga_bmc_encoder_atomic_enable(struct drm_encoder *encoder,
21 struct drm_atomic_state *state)
22 {
23 struct mga_device *mdev = to_mga_device(encoder->dev);
24
25 if (mdev->info->sync_bmc)
26 mgag200_bmc_start_scanout(mdev);
27 }
28
mgag200_vga_bmc_encoder_atomic_check(struct drm_encoder * encoder,struct drm_crtc_state * new_crtc_state,struct drm_connector_state * new_connector_state)29 static int mgag200_vga_bmc_encoder_atomic_check(struct drm_encoder *encoder,
30 struct drm_crtc_state *new_crtc_state,
31 struct drm_connector_state *new_connector_state)
32 {
33 struct mga_device *mdev = to_mga_device(encoder->dev);
34 struct mgag200_crtc_state *new_mgag200_crtc_state = to_mgag200_crtc_state(new_crtc_state);
35
36 new_mgag200_crtc_state->set_vidrst = mdev->info->sync_bmc;
37
38 return 0;
39 }
40
41 static const struct drm_encoder_helper_funcs mgag200_dac_encoder_helper_funcs = {
42 .atomic_disable = mgag200_vga_bmc_encoder_atomic_disable,
43 .atomic_enable = mgag200_vga_bmc_encoder_atomic_enable,
44 .atomic_check = mgag200_vga_bmc_encoder_atomic_check,
45 };
46
47 static const struct drm_encoder_funcs mgag200_dac_encoder_funcs = {
48 .destroy = drm_encoder_cleanup
49 };
50
mgag200_vga_bmc_connector_helper_get_modes(struct drm_connector * connector)51 static int mgag200_vga_bmc_connector_helper_get_modes(struct drm_connector *connector)
52 {
53 struct mga_device *mdev = to_mga_device(connector->dev);
54 const struct mgag200_device_info *minfo = mdev->info;
55 int count;
56
57 count = drm_connector_helper_get_modes(connector);
58
59 if (!count) {
60 /*
61 * There's no EDID data without a connected monitor. Set BMC-
62 * compatible modes in this case. The XGA default resolution
63 * should work well for all BMCs.
64 */
65 count = drm_add_modes_noedid(connector, minfo->max_hdisplay, minfo->max_vdisplay);
66 if (count)
67 drm_set_preferred_mode(connector, 1024, 768);
68 }
69
70 return count;
71 }
72
73 /*
74 * There's no monitor connected if the DDC did not return an EDID. Still
75 * return 'connected' as there's always a BMC. Incrementing the connector's
76 * epoch counter triggers an update of the related properties.
77 */
mgag200_vga_bmc_connector_helper_detect_ctx(struct drm_connector * connector,struct drm_modeset_acquire_ctx * ctx,bool force)78 static int mgag200_vga_bmc_connector_helper_detect_ctx(struct drm_connector *connector,
79 struct drm_modeset_acquire_ctx *ctx,
80 bool force)
81 {
82 enum drm_connector_status old_status, status;
83
84 if (connector->edid_blob_ptr)
85 old_status = connector_status_connected;
86 else
87 old_status = connector_status_disconnected;
88
89 status = drm_connector_helper_detect_from_ddc(connector, ctx, force);
90
91 if (status != old_status)
92 ++connector->epoch_counter;
93 return connector_status_connected;
94 }
95
96 static const struct drm_connector_helper_funcs mgag200_vga_connector_helper_funcs = {
97 .get_modes = mgag200_vga_bmc_connector_helper_get_modes,
98 .detect_ctx = mgag200_vga_bmc_connector_helper_detect_ctx,
99 };
100
101 static const struct drm_connector_funcs mgag200_vga_connector_funcs = {
102 .reset = drm_atomic_helper_connector_reset,
103 .fill_modes = drm_helper_probe_single_connector_modes,
104 .destroy = drm_connector_cleanup,
105 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
106 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state
107 };
108
mgag200_vga_bmc_output_init(struct mga_device * mdev)109 int mgag200_vga_bmc_output_init(struct mga_device *mdev)
110 {
111 struct drm_device *dev = &mdev->base;
112 struct drm_crtc *crtc = &mdev->crtc;
113 struct drm_encoder *encoder;
114 struct drm_connector *connector;
115 struct i2c_adapter *ddc;
116 int ret;
117
118 encoder = &mdev->output.vga.encoder;
119 ret = drm_encoder_init(dev, encoder, &mgag200_dac_encoder_funcs,
120 DRM_MODE_ENCODER_DAC, NULL);
121 if (ret) {
122 drm_err(dev, "drm_encoder_init() failed: %d\n", ret);
123 return ret;
124 }
125 drm_encoder_helper_add(encoder, &mgag200_dac_encoder_helper_funcs);
126
127 encoder->possible_crtcs = drm_crtc_mask(crtc);
128
129 ddc = mgag200_ddc_create(mdev);
130 if (IS_ERR(ddc)) {
131 ret = PTR_ERR(ddc);
132 drm_err(dev, "failed to add DDC bus: %d\n", ret);
133 return ret;
134 }
135
136 connector = &mdev->output.vga.connector;
137 ret = drm_connector_init_with_ddc(dev, connector,
138 &mgag200_vga_connector_funcs,
139 DRM_MODE_CONNECTOR_VGA, ddc);
140 if (ret) {
141 drm_err(dev, "drm_connector_init_with_ddc() failed: %d\n", ret);
142 return ret;
143 }
144 drm_connector_helper_add(connector, &mgag200_vga_connector_helper_funcs);
145
146 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
147 DRM_CONNECTOR_POLL_DISCONNECT;
148
149 ret = drm_connector_attach_encoder(connector, encoder);
150 if (ret) {
151 drm_err(dev, "drm_connector_attach_encoder() failed: %d\n", ret);
152 return ret;
153 }
154
155 return 0;
156 }
157