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