1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2015 Free Electrons 4 * Copyright (C) 2015 NextThing Co 5 * 6 * Maxime Ripard <maxime.ripard@free-electrons.com> 7 */ 8 9 #include <linux/clk.h> 10 11 #include <drm/drm_atomic_helper.h> 12 #include <drm/drm_bridge.h> 13 #include <drm/drm_of.h> 14 #include <drm/drm_panel.h> 15 #include <drm/drm_print.h> 16 #include <drm/drm_probe_helper.h> 17 18 #include "sun4i_crtc.h" 19 #include "sun4i_tcon.h" 20 #include "sun4i_rgb.h" 21 22 struct sun4i_rgb { 23 struct drm_connector connector; 24 struct drm_encoder encoder; 25 26 struct sun4i_tcon *tcon; 27 struct drm_panel *panel; 28 struct drm_bridge *bridge; 29 }; 30 31 static inline struct sun4i_rgb * 32 drm_connector_to_sun4i_rgb(struct drm_connector *connector) 33 { 34 return container_of(connector, struct sun4i_rgb, 35 connector); 36 } 37 38 static inline struct sun4i_rgb * 39 drm_encoder_to_sun4i_rgb(struct drm_encoder *encoder) 40 { 41 return container_of(encoder, struct sun4i_rgb, 42 encoder); 43 } 44 45 static int sun4i_rgb_get_modes(struct drm_connector *connector) 46 { 47 struct sun4i_rgb *rgb = 48 drm_connector_to_sun4i_rgb(connector); 49 50 return drm_panel_get_modes(rgb->panel, connector); 51 } 52 53 /* 54 * VESA DMT defines a tolerance of 0.5% on the pixel clock, while the 55 * CVT spec reuses that tolerance in its examples, so it looks to be a 56 * good default tolerance for the EDID-based modes. Define it to 5 per 57 * mille to avoid floating point operations. 58 */ 59 #define SUN4I_RGB_DOTCLOCK_TOLERANCE_PER_MILLE 5 60 61 static enum drm_mode_status sun4i_rgb_mode_valid(struct drm_encoder *crtc, 62 const struct drm_display_mode *mode) 63 { 64 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(crtc); 65 struct sun4i_tcon *tcon = rgb->tcon; 66 u32 hsync = mode->hsync_end - mode->hsync_start; 67 u32 vsync = mode->vsync_end - mode->vsync_start; 68 unsigned long long rate = mode->clock * 1000; 69 unsigned long long lowest, highest; 70 unsigned long long rounded_rate; 71 72 DRM_DEBUG_DRIVER("Validating modes...\n"); 73 74 if (hsync < 1) 75 return MODE_HSYNC_NARROW; 76 77 if (hsync > 0x3ff) 78 return MODE_HSYNC_WIDE; 79 80 if ((mode->hdisplay < 1) || (mode->htotal < 1)) 81 return MODE_H_ILLEGAL; 82 83 if ((mode->hdisplay > 0x7ff) || (mode->htotal > 0xfff)) 84 return MODE_BAD_HVALUE; 85 86 DRM_DEBUG_DRIVER("Horizontal parameters OK\n"); 87 88 if (vsync < 1) 89 return MODE_VSYNC_NARROW; 90 91 if (vsync > 0x3ff) 92 return MODE_VSYNC_WIDE; 93 94 if ((mode->vdisplay < 1) || (mode->vtotal < 1)) 95 return MODE_V_ILLEGAL; 96 97 if ((mode->vdisplay > 0x7ff) || (mode->vtotal > 0xfff)) 98 return MODE_BAD_VVALUE; 99 100 DRM_DEBUG_DRIVER("Vertical parameters OK\n"); 101 102 /* 103 * TODO: We should use the struct display_timing if available 104 * and / or trying to stretch the timings within that 105 * tolerancy to take care of panels that we wouldn't be able 106 * to have a exact match for. 107 */ 108 if (rgb->panel) { 109 DRM_DEBUG_DRIVER("RGB panel used, skipping clock rate checks"); 110 goto out; 111 } 112 113 /* 114 * That shouldn't ever happen unless something is really wrong, but it 115 * doesn't harm to check. 116 */ 117 if (!rgb->bridge) 118 goto out; 119 120 tcon->dclk_min_div = 6; 121 tcon->dclk_max_div = 127; 122 rounded_rate = clk_round_rate(tcon->dclk, rate); 123 124 lowest = rate * (1000 - SUN4I_RGB_DOTCLOCK_TOLERANCE_PER_MILLE); 125 do_div(lowest, 1000); 126 if (rounded_rate < lowest) 127 return MODE_CLOCK_LOW; 128 129 highest = rate * (1000 + SUN4I_RGB_DOTCLOCK_TOLERANCE_PER_MILLE); 130 do_div(highest, 1000); 131 if (rounded_rate > highest) 132 return MODE_CLOCK_HIGH; 133 134 out: 135 DRM_DEBUG_DRIVER("Clock rate OK\n"); 136 137 return MODE_OK; 138 } 139 140 static struct drm_connector_helper_funcs sun4i_rgb_con_helper_funcs = { 141 .get_modes = sun4i_rgb_get_modes, 142 }; 143 144 static void 145 sun4i_rgb_connector_destroy(struct drm_connector *connector) 146 { 147 struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector); 148 149 drm_panel_detach(rgb->panel); 150 drm_connector_cleanup(connector); 151 } 152 153 static const struct drm_connector_funcs sun4i_rgb_con_funcs = { 154 .fill_modes = drm_helper_probe_single_connector_modes, 155 .destroy = sun4i_rgb_connector_destroy, 156 .reset = drm_atomic_helper_connector_reset, 157 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 158 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 159 }; 160 161 static void sun4i_rgb_encoder_enable(struct drm_encoder *encoder) 162 { 163 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder); 164 165 DRM_DEBUG_DRIVER("Enabling RGB output\n"); 166 167 if (rgb->panel) { 168 drm_panel_prepare(rgb->panel); 169 drm_panel_enable(rgb->panel); 170 } 171 } 172 173 static void sun4i_rgb_encoder_disable(struct drm_encoder *encoder) 174 { 175 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder); 176 177 DRM_DEBUG_DRIVER("Disabling RGB output\n"); 178 179 if (rgb->panel) { 180 drm_panel_disable(rgb->panel); 181 drm_panel_unprepare(rgb->panel); 182 } 183 } 184 185 static struct drm_encoder_helper_funcs sun4i_rgb_enc_helper_funcs = { 186 .disable = sun4i_rgb_encoder_disable, 187 .enable = sun4i_rgb_encoder_enable, 188 .mode_valid = sun4i_rgb_mode_valid, 189 }; 190 191 static void sun4i_rgb_enc_destroy(struct drm_encoder *encoder) 192 { 193 drm_encoder_cleanup(encoder); 194 } 195 196 static struct drm_encoder_funcs sun4i_rgb_enc_funcs = { 197 .destroy = sun4i_rgb_enc_destroy, 198 }; 199 200 int sun4i_rgb_init(struct drm_device *drm, struct sun4i_tcon *tcon) 201 { 202 struct drm_encoder *encoder; 203 struct sun4i_rgb *rgb; 204 int ret; 205 206 rgb = devm_kzalloc(drm->dev, sizeof(*rgb), GFP_KERNEL); 207 if (!rgb) 208 return -ENOMEM; 209 rgb->tcon = tcon; 210 encoder = &rgb->encoder; 211 212 ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0, 213 &rgb->panel, &rgb->bridge); 214 if (ret) { 215 dev_info(drm->dev, "No panel or bridge found... RGB output disabled\n"); 216 return 0; 217 } 218 219 drm_encoder_helper_add(&rgb->encoder, 220 &sun4i_rgb_enc_helper_funcs); 221 ret = drm_encoder_init(drm, 222 &rgb->encoder, 223 &sun4i_rgb_enc_funcs, 224 DRM_MODE_ENCODER_NONE, 225 NULL); 226 if (ret) { 227 dev_err(drm->dev, "Couldn't initialise the rgb encoder\n"); 228 goto err_out; 229 } 230 231 /* The RGB encoder can only work with the TCON channel 0 */ 232 rgb->encoder.possible_crtcs = drm_crtc_mask(&tcon->crtc->crtc); 233 234 if (rgb->panel) { 235 drm_connector_helper_add(&rgb->connector, 236 &sun4i_rgb_con_helper_funcs); 237 ret = drm_connector_init(drm, &rgb->connector, 238 &sun4i_rgb_con_funcs, 239 DRM_MODE_CONNECTOR_Unknown); 240 if (ret) { 241 dev_err(drm->dev, "Couldn't initialise the rgb connector\n"); 242 goto err_cleanup_connector; 243 } 244 245 drm_connector_attach_encoder(&rgb->connector, 246 &rgb->encoder); 247 248 ret = drm_panel_attach(rgb->panel, &rgb->connector); 249 if (ret) { 250 dev_err(drm->dev, "Couldn't attach our panel\n"); 251 goto err_cleanup_connector; 252 } 253 } 254 255 if (rgb->bridge) { 256 ret = drm_bridge_attach(encoder, rgb->bridge, NULL, 0); 257 if (ret) { 258 dev_err(drm->dev, "Couldn't attach our bridge\n"); 259 goto err_cleanup_connector; 260 } 261 } 262 263 return 0; 264 265 err_cleanup_connector: 266 drm_encoder_cleanup(&rgb->encoder); 267 err_out: 268 return ret; 269 } 270 EXPORT_SYMBOL(sun4i_rgb_init); 271