1 /* 2 * Exynos DRM Parallel output support. 3 * 4 * Copyright (c) 2014 Samsung Electronics Co., Ltd 5 * 6 * Contacts: Andrzej Hajda <a.hajda@samsung.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License version 2 as 10 * published by the Free Software Foundation. 11 */ 12 13 #include <drm/drmP.h> 14 #include <drm/drm_crtc_helper.h> 15 #include <drm/drm_panel.h> 16 #include <drm/drm_atomic_helper.h> 17 18 #include <linux/of_graph.h> 19 #include <linux/regulator/consumer.h> 20 21 #include <video/of_videomode.h> 22 #include <video/videomode.h> 23 24 #include "exynos_drm_crtc.h" 25 26 struct exynos_dpi { 27 struct drm_encoder encoder; 28 struct device *dev; 29 struct device_node *panel_node; 30 31 struct drm_panel *panel; 32 struct drm_connector connector; 33 34 struct videomode *vm; 35 }; 36 37 #define connector_to_dpi(c) container_of(c, struct exynos_dpi, connector) 38 39 static inline struct exynos_dpi *encoder_to_dpi(struct drm_encoder *e) 40 { 41 return container_of(e, struct exynos_dpi, encoder); 42 } 43 44 static enum drm_connector_status 45 exynos_dpi_detect(struct drm_connector *connector, bool force) 46 { 47 struct exynos_dpi *ctx = connector_to_dpi(connector); 48 49 if (ctx->panel && !ctx->panel->connector) 50 drm_panel_attach(ctx->panel, &ctx->connector); 51 52 return connector_status_connected; 53 } 54 55 static void exynos_dpi_connector_destroy(struct drm_connector *connector) 56 { 57 drm_connector_unregister(connector); 58 drm_connector_cleanup(connector); 59 } 60 61 static const struct drm_connector_funcs exynos_dpi_connector_funcs = { 62 .dpms = drm_atomic_helper_connector_dpms, 63 .detect = exynos_dpi_detect, 64 .fill_modes = drm_helper_probe_single_connector_modes, 65 .destroy = exynos_dpi_connector_destroy, 66 .reset = drm_atomic_helper_connector_reset, 67 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 68 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 69 }; 70 71 static int exynos_dpi_get_modes(struct drm_connector *connector) 72 { 73 struct exynos_dpi *ctx = connector_to_dpi(connector); 74 75 /* fimd timings gets precedence over panel modes */ 76 if (ctx->vm) { 77 struct drm_display_mode *mode; 78 79 mode = drm_mode_create(connector->dev); 80 if (!mode) { 81 DRM_ERROR("failed to create a new display mode\n"); 82 return 0; 83 } 84 drm_display_mode_from_videomode(ctx->vm, mode); 85 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED; 86 drm_mode_probed_add(connector, mode); 87 return 1; 88 } 89 90 if (ctx->panel) 91 return ctx->panel->funcs->get_modes(ctx->panel); 92 93 return 0; 94 } 95 96 static struct drm_encoder * 97 exynos_dpi_best_encoder(struct drm_connector *connector) 98 { 99 struct exynos_dpi *ctx = connector_to_dpi(connector); 100 101 return &ctx->encoder; 102 } 103 104 static const struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs = { 105 .get_modes = exynos_dpi_get_modes, 106 .best_encoder = exynos_dpi_best_encoder, 107 }; 108 109 static int exynos_dpi_create_connector(struct drm_encoder *encoder) 110 { 111 struct exynos_dpi *ctx = encoder_to_dpi(encoder); 112 struct drm_connector *connector = &ctx->connector; 113 int ret; 114 115 connector->polled = DRM_CONNECTOR_POLL_HPD; 116 117 ret = drm_connector_init(encoder->dev, connector, 118 &exynos_dpi_connector_funcs, 119 DRM_MODE_CONNECTOR_VGA); 120 if (ret) { 121 DRM_ERROR("failed to initialize connector with drm\n"); 122 return ret; 123 } 124 125 drm_connector_helper_add(connector, &exynos_dpi_connector_helper_funcs); 126 drm_connector_register(connector); 127 drm_mode_connector_attach_encoder(connector, encoder); 128 129 return 0; 130 } 131 132 static void exynos_dpi_mode_set(struct drm_encoder *encoder, 133 struct drm_display_mode *mode, 134 struct drm_display_mode *adjusted_mode) 135 { 136 } 137 138 static void exynos_dpi_enable(struct drm_encoder *encoder) 139 { 140 struct exynos_dpi *ctx = encoder_to_dpi(encoder); 141 142 if (ctx->panel) { 143 drm_panel_prepare(ctx->panel); 144 drm_panel_enable(ctx->panel); 145 } 146 } 147 148 static void exynos_dpi_disable(struct drm_encoder *encoder) 149 { 150 struct exynos_dpi *ctx = encoder_to_dpi(encoder); 151 152 if (ctx->panel) { 153 drm_panel_disable(ctx->panel); 154 drm_panel_unprepare(ctx->panel); 155 } 156 } 157 158 static const struct drm_encoder_helper_funcs exynos_dpi_encoder_helper_funcs = { 159 .mode_set = exynos_dpi_mode_set, 160 .enable = exynos_dpi_enable, 161 .disable = exynos_dpi_disable, 162 }; 163 164 static const struct drm_encoder_funcs exynos_dpi_encoder_funcs = { 165 .destroy = drm_encoder_cleanup, 166 }; 167 168 enum { 169 FIMD_PORT_IN0, 170 FIMD_PORT_IN1, 171 FIMD_PORT_IN2, 172 FIMD_PORT_RGB, 173 FIMD_PORT_WRB, 174 }; 175 176 static struct device_node *exynos_dpi_of_find_panel_node(struct device *dev) 177 { 178 struct device_node *np, *ep; 179 180 ep = of_graph_get_endpoint_by_regs(dev->of_node, FIMD_PORT_RGB, 0); 181 if (!ep) 182 return NULL; 183 184 np = of_graph_get_remote_port_parent(ep); 185 of_node_put(ep); 186 187 return np; 188 } 189 190 static int exynos_dpi_parse_dt(struct exynos_dpi *ctx) 191 { 192 struct device *dev = ctx->dev; 193 struct device_node *dn = dev->of_node; 194 struct device_node *np; 195 196 ctx->panel_node = exynos_dpi_of_find_panel_node(dev); 197 198 np = of_get_child_by_name(dn, "display-timings"); 199 if (np) { 200 struct videomode *vm; 201 int ret; 202 203 of_node_put(np); 204 205 vm = devm_kzalloc(dev, sizeof(*ctx->vm), GFP_KERNEL); 206 if (!vm) 207 return -ENOMEM; 208 209 ret = of_get_videomode(dn, vm, 0); 210 if (ret < 0) { 211 devm_kfree(dev, vm); 212 return ret; 213 } 214 215 ctx->vm = vm; 216 217 return 0; 218 } 219 220 if (!ctx->panel_node) 221 return -EINVAL; 222 223 return 0; 224 } 225 226 int exynos_dpi_bind(struct drm_device *dev, struct drm_encoder *encoder) 227 { 228 int ret; 229 230 ret = exynos_drm_crtc_get_pipe_from_type(dev, EXYNOS_DISPLAY_TYPE_LCD); 231 if (ret < 0) 232 return ret; 233 234 encoder->possible_crtcs = 1 << ret; 235 236 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs); 237 238 drm_encoder_init(dev, encoder, &exynos_dpi_encoder_funcs, 239 DRM_MODE_ENCODER_TMDS, NULL); 240 241 drm_encoder_helper_add(encoder, &exynos_dpi_encoder_helper_funcs); 242 243 ret = exynos_dpi_create_connector(encoder); 244 if (ret) { 245 DRM_ERROR("failed to create connector ret = %d\n", ret); 246 drm_encoder_cleanup(encoder); 247 return ret; 248 } 249 250 return 0; 251 } 252 253 struct drm_encoder *exynos_dpi_probe(struct device *dev) 254 { 255 struct exynos_dpi *ctx; 256 int ret; 257 258 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); 259 if (!ctx) 260 return ERR_PTR(-ENOMEM); 261 262 ctx->dev = dev; 263 264 ret = exynos_dpi_parse_dt(ctx); 265 if (ret < 0) { 266 devm_kfree(dev, ctx); 267 return NULL; 268 } 269 270 if (ctx->panel_node) { 271 ctx->panel = of_drm_find_panel(ctx->panel_node); 272 if (!ctx->panel) 273 return ERR_PTR(-EPROBE_DEFER); 274 } 275 276 return &ctx->encoder; 277 } 278 279 int exynos_dpi_remove(struct drm_encoder *encoder) 280 { 281 struct exynos_dpi *ctx = encoder_to_dpi(encoder); 282 283 exynos_dpi_disable(&ctx->encoder); 284 285 if (ctx->panel) 286 drm_panel_detach(ctx->panel); 287 288 return 0; 289 } 290