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