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/regulator/consumer.h> 19 20 #include <video/of_videomode.h> 21 #include <video/videomode.h> 22 23 #include "exynos_drm_crtc.h" 24 25 struct exynos_dpi { 26 struct drm_encoder encoder; 27 struct device *dev; 28 struct device_node *panel_node; 29 30 struct drm_panel *panel; 31 struct drm_connector connector; 32 33 struct videomode *vm; 34 }; 35 36 #define connector_to_dpi(c) container_of(c, struct exynos_dpi, connector) 37 38 static inline struct exynos_dpi *encoder_to_dpi(struct drm_encoder *e) 39 { 40 return container_of(e, struct exynos_dpi, encoder); 41 } 42 43 static enum drm_connector_status 44 exynos_dpi_detect(struct drm_connector *connector, bool force) 45 { 46 struct exynos_dpi *ctx = connector_to_dpi(connector); 47 48 if (ctx->panel && !ctx->panel->connector) 49 drm_panel_attach(ctx->panel, &ctx->connector); 50 51 return connector_status_connected; 52 } 53 54 static void exynos_dpi_connector_destroy(struct drm_connector *connector) 55 { 56 drm_connector_unregister(connector); 57 drm_connector_cleanup(connector); 58 } 59 60 static struct drm_connector_funcs exynos_dpi_connector_funcs = { 61 .dpms = drm_atomic_helper_connector_dpms, 62 .detect = exynos_dpi_detect, 63 .fill_modes = drm_helper_probe_single_connector_modes, 64 .destroy = exynos_dpi_connector_destroy, 65 .reset = drm_atomic_helper_connector_reset, 66 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 67 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 68 }; 69 70 static int exynos_dpi_get_modes(struct drm_connector *connector) 71 { 72 struct exynos_dpi *ctx = connector_to_dpi(connector); 73 74 /* fimd timings gets precedence over panel modes */ 75 if (ctx->vm) { 76 struct drm_display_mode *mode; 77 78 mode = drm_mode_create(connector->dev); 79 if (!mode) { 80 DRM_ERROR("failed to create a new display mode\n"); 81 return 0; 82 } 83 drm_display_mode_from_videomode(ctx->vm, mode); 84 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED; 85 drm_mode_probed_add(connector, mode); 86 return 1; 87 } 88 89 if (ctx->panel) 90 return ctx->panel->funcs->get_modes(ctx->panel); 91 92 return 0; 93 } 94 95 static struct drm_encoder * 96 exynos_dpi_best_encoder(struct drm_connector *connector) 97 { 98 struct exynos_dpi *ctx = connector_to_dpi(connector); 99 100 return &ctx->encoder; 101 } 102 103 static struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs = { 104 .get_modes = exynos_dpi_get_modes, 105 .best_encoder = exynos_dpi_best_encoder, 106 }; 107 108 static int exynos_dpi_create_connector(struct drm_encoder *encoder) 109 { 110 struct exynos_dpi *ctx = encoder_to_dpi(encoder); 111 struct drm_connector *connector = &ctx->connector; 112 int ret; 113 114 connector->polled = DRM_CONNECTOR_POLL_HPD; 115 116 ret = drm_connector_init(encoder->dev, connector, 117 &exynos_dpi_connector_funcs, 118 DRM_MODE_CONNECTOR_VGA); 119 if (ret) { 120 DRM_ERROR("failed to initialize connector with drm\n"); 121 return ret; 122 } 123 124 drm_connector_helper_add(connector, &exynos_dpi_connector_helper_funcs); 125 drm_connector_register(connector); 126 drm_mode_connector_attach_encoder(connector, encoder); 127 128 return 0; 129 } 130 131 static bool exynos_dpi_mode_fixup(struct drm_encoder *encoder, 132 const struct drm_display_mode *mode, 133 struct drm_display_mode *adjusted_mode) 134 { 135 return true; 136 } 137 138 static void exynos_dpi_mode_set(struct drm_encoder *encoder, 139 struct drm_display_mode *mode, 140 struct drm_display_mode *adjusted_mode) 141 { 142 } 143 144 static void exynos_dpi_enable(struct drm_encoder *encoder) 145 { 146 struct exynos_dpi *ctx = encoder_to_dpi(encoder); 147 148 if (ctx->panel) { 149 drm_panel_prepare(ctx->panel); 150 drm_panel_enable(ctx->panel); 151 } 152 } 153 154 static void exynos_dpi_disable(struct drm_encoder *encoder) 155 { 156 struct exynos_dpi *ctx = encoder_to_dpi(encoder); 157 158 if (ctx->panel) { 159 drm_panel_disable(ctx->panel); 160 drm_panel_unprepare(ctx->panel); 161 } 162 } 163 164 static struct drm_encoder_helper_funcs exynos_dpi_encoder_helper_funcs = { 165 .mode_fixup = exynos_dpi_mode_fixup, 166 .mode_set = exynos_dpi_mode_set, 167 .enable = exynos_dpi_enable, 168 .disable = exynos_dpi_disable, 169 }; 170 171 static struct drm_encoder_funcs exynos_dpi_encoder_funcs = { 172 .destroy = drm_encoder_cleanup, 173 }; 174 175 /* of_* functions will be removed after merge of of_graph patches */ 176 static struct device_node * 177 of_get_child_by_name_reg(struct device_node *parent, const char *name, u32 reg) 178 { 179 struct device_node *np; 180 181 for_each_child_of_node(parent, np) { 182 u32 r; 183 184 if (!np->name || of_node_cmp(np->name, name)) 185 continue; 186 187 if (of_property_read_u32(np, "reg", &r) < 0) 188 r = 0; 189 190 if (reg == r) 191 break; 192 } 193 194 return np; 195 } 196 197 static struct device_node *of_graph_get_port_by_reg(struct device_node *parent, 198 u32 reg) 199 { 200 struct device_node *ports, *port; 201 202 ports = of_get_child_by_name(parent, "ports"); 203 if (ports) 204 parent = ports; 205 206 port = of_get_child_by_name_reg(parent, "port", reg); 207 208 of_node_put(ports); 209 210 return port; 211 } 212 213 static struct device_node * 214 of_graph_get_endpoint_by_reg(struct device_node *port, u32 reg) 215 { 216 return of_get_child_by_name_reg(port, "endpoint", reg); 217 } 218 219 static struct device_node * 220 of_graph_get_remote_port_parent(const struct device_node *node) 221 { 222 struct device_node *np; 223 unsigned int depth; 224 225 np = of_parse_phandle(node, "remote-endpoint", 0); 226 227 /* Walk 3 levels up only if there is 'ports' node. */ 228 for (depth = 3; depth && np; depth--) { 229 np = of_get_next_parent(np); 230 if (depth == 2 && of_node_cmp(np->name, "ports")) 231 break; 232 } 233 return np; 234 } 235 236 enum { 237 FIMD_PORT_IN0, 238 FIMD_PORT_IN1, 239 FIMD_PORT_IN2, 240 FIMD_PORT_RGB, 241 FIMD_PORT_WRB, 242 }; 243 244 static struct device_node *exynos_dpi_of_find_panel_node(struct device *dev) 245 { 246 struct device_node *np, *ep; 247 248 np = of_graph_get_port_by_reg(dev->of_node, FIMD_PORT_RGB); 249 if (!np) 250 return NULL; 251 252 ep = of_graph_get_endpoint_by_reg(np, 0); 253 of_node_put(np); 254 if (!ep) 255 return NULL; 256 257 np = of_graph_get_remote_port_parent(ep); 258 of_node_put(ep); 259 260 return np; 261 } 262 263 static int exynos_dpi_parse_dt(struct exynos_dpi *ctx) 264 { 265 struct device *dev = ctx->dev; 266 struct device_node *dn = dev->of_node; 267 struct device_node *np; 268 269 ctx->panel_node = exynos_dpi_of_find_panel_node(dev); 270 271 np = of_get_child_by_name(dn, "display-timings"); 272 if (np) { 273 struct videomode *vm; 274 int ret; 275 276 of_node_put(np); 277 278 vm = devm_kzalloc(dev, sizeof(*ctx->vm), GFP_KERNEL); 279 if (!vm) 280 return -ENOMEM; 281 282 ret = of_get_videomode(dn, vm, 0); 283 if (ret < 0) { 284 devm_kfree(dev, vm); 285 return ret; 286 } 287 288 ctx->vm = vm; 289 290 return 0; 291 } 292 293 if (!ctx->panel_node) 294 return -EINVAL; 295 296 return 0; 297 } 298 299 int exynos_dpi_bind(struct drm_device *dev, struct drm_encoder *encoder) 300 { 301 int ret; 302 303 ret = exynos_drm_crtc_get_pipe_from_type(dev, EXYNOS_DISPLAY_TYPE_LCD); 304 if (ret < 0) 305 return ret; 306 307 encoder->possible_crtcs = 1 << ret; 308 309 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs); 310 311 drm_encoder_init(dev, encoder, &exynos_dpi_encoder_funcs, 312 DRM_MODE_ENCODER_TMDS); 313 314 drm_encoder_helper_add(encoder, &exynos_dpi_encoder_helper_funcs); 315 316 ret = exynos_dpi_create_connector(encoder); 317 if (ret) { 318 DRM_ERROR("failed to create connector ret = %d\n", ret); 319 drm_encoder_cleanup(encoder); 320 return ret; 321 } 322 323 return 0; 324 } 325 326 struct drm_encoder *exynos_dpi_probe(struct device *dev) 327 { 328 struct exynos_dpi *ctx; 329 int ret; 330 331 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); 332 if (!ctx) 333 return ERR_PTR(-ENOMEM); 334 335 ctx->dev = dev; 336 337 ret = exynos_dpi_parse_dt(ctx); 338 if (ret < 0) { 339 devm_kfree(dev, ctx); 340 return NULL; 341 } 342 343 if (ctx->panel_node) { 344 ctx->panel = of_drm_find_panel(ctx->panel_node); 345 if (!ctx->panel) 346 return ERR_PTR(-EPROBE_DEFER); 347 } 348 349 return &ctx->encoder; 350 } 351 352 int exynos_dpi_remove(struct drm_encoder *encoder) 353 { 354 struct exynos_dpi *ctx = encoder_to_dpi(encoder); 355 356 exynos_dpi_disable(&ctx->encoder); 357 358 if (ctx->panel) 359 drm_panel_detach(ctx->panel); 360 361 return 0; 362 } 363