Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 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 { |
Andrzej Hajda | 4cfde1f | 2014-11-17 09:54:26 +0100 | [diff] [blame] | 25 | struct exynos_drm_display display; |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 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 | |
Andrzej Hajda | 4cfde1f | 2014-11-17 09:54:26 +0100 | [diff] [blame] | 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 | |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 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 | |
Tomasz Figa | aaa51b1 | 2014-06-10 22:57:57 +0200 | [diff] [blame] | 49 | if (ctx->panel && !ctx->panel->connector) |
Andrzej Hajda | 000cc92 | 2014-04-03 16:26:00 +0200 | [diff] [blame] | 50 | drm_panel_attach(ctx->panel, &ctx->connector); |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 51 | |
Andrzej Hajda | 000cc92 | 2014-04-03 16:26:00 +0200 | [diff] [blame] | 52 | return connector_status_connected; |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static void exynos_dpi_connector_destroy(struct drm_connector *connector) |
| 56 | { |
Thomas Wood | 34ea3d3 | 2014-05-29 16:57:41 +0100 | [diff] [blame] | 57 | drm_connector_unregister(connector); |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 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 | |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 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, |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 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 | { |
Andrzej Hajda | 5af3d9b | 2014-11-17 09:54:27 +0100 | [diff] [blame] | 109 | struct exynos_dpi *ctx = display_to_dpi(display); |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 110 | struct drm_connector *connector = &ctx->connector; |
| 111 | int ret; |
| 112 | |
| 113 | ctx->encoder = encoder; |
| 114 | |
Inki Dae | ae08fe6 | 2014-04-01 19:45:28 +0900 | [diff] [blame] | 115 | connector->polled = DRM_CONNECTOR_POLL_HPD; |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 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); |
Thomas Wood | 34ea3d3 | 2014-05-29 16:57:41 +0100 | [diff] [blame] | 126 | drm_connector_register(connector); |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 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 | { |
Ajay Kumar | 39bbde9 | 2014-07-31 23:12:05 +0530 | [diff] [blame] | 134 | if (ctx->panel) { |
| 135 | drm_panel_prepare(ctx->panel); |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 136 | drm_panel_enable(ctx->panel); |
Ajay Kumar | 39bbde9 | 2014-07-31 23:12:05 +0530 | [diff] [blame] | 137 | } |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static void exynos_dpi_poweroff(struct exynos_dpi *ctx) |
| 141 | { |
Ajay Kumar | 39bbde9 | 2014-07-31 23:12:05 +0530 | [diff] [blame] | 142 | if (ctx->panel) { |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 143 | drm_panel_disable(ctx->panel); |
Ajay Kumar | 39bbde9 | 2014-07-31 23:12:05 +0530 | [diff] [blame] | 144 | drm_panel_unprepare(ctx->panel); |
| 145 | } |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static void exynos_dpi_dpms(struct exynos_drm_display *display, int mode) |
| 149 | { |
Andrzej Hajda | 5af3d9b | 2014-11-17 09:54:27 +0100 | [diff] [blame] | 150 | struct exynos_dpi *ctx = display_to_dpi(display); |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 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; |
Damien Lespiau | 10d9b4e | 2014-06-09 14:21:08 +0100 | [diff] [blame] | 165 | } |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 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 | |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 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 | |
Sachin Kamat | f46ed1a | 2014-05-22 10:32:54 +0530 | [diff] [blame] | 243 | static struct device_node *exynos_dpi_of_find_panel_node(struct device *dev) |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 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); |
Andrzej Hajda | 000cc92 | 2014-04-03 16:26:00 +0200 | [diff] [blame] | 282 | if (ret < 0) { |
| 283 | devm_kfree(dev, vm); |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 284 | return ret; |
Andrzej Hajda | 000cc92 | 2014-04-03 16:26:00 +0200 | [diff] [blame] | 285 | } |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 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 | |
Andrzej Hajda | 000cc92 | 2014-04-03 16:26:00 +0200 | [diff] [blame] | 298 | struct exynos_drm_display *exynos_dpi_probe(struct device *dev) |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 299 | { |
| 300 | struct exynos_dpi *ctx; |
| 301 | int ret; |
| 302 | |
| 303 | ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); |
| 304 | if (!ctx) |
Andrzej Hajda | 4cfde1f | 2014-11-17 09:54:26 +0100 | [diff] [blame] | 305 | return ERR_PTR(-ENOMEM); |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 306 | |
Andrzej Hajda | 4cfde1f | 2014-11-17 09:54:26 +0100 | [diff] [blame] | 307 | ctx->display.type = EXYNOS_DISPLAY_TYPE_LCD; |
| 308 | ctx->display.ops = &exynos_dpi_display_ops; |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 309 | ctx->dev = dev; |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 310 | ctx->dpms_mode = DRM_MODE_DPMS_OFF; |
| 311 | |
Andrzej Hajda | 4cfde1f | 2014-11-17 09:54:26 +0100 | [diff] [blame] | 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 | |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 318 | ret = exynos_dpi_parse_dt(ctx); |
Andrzej Hajda | 000cc92 | 2014-04-03 16:26:00 +0200 | [diff] [blame] | 319 | if (ret < 0) { |
| 320 | devm_kfree(dev, ctx); |
Inki Dae | df5225b | 2014-05-29 18:28:02 +0900 | [diff] [blame] | 321 | goto err_del_component; |
Andrzej Hajda | 000cc92 | 2014-04-03 16:26:00 +0200 | [diff] [blame] | 322 | } |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 323 | |
Andrzej Hajda | 000cc92 | 2014-04-03 16:26:00 +0200 | [diff] [blame] | 324 | if (ctx->panel_node) { |
| 325 | ctx->panel = of_drm_find_panel(ctx->panel_node); |
Inki Dae | df5225b | 2014-05-29 18:28:02 +0900 | [diff] [blame] | 326 | if (!ctx->panel) { |
| 327 | exynos_drm_component_del(dev, |
| 328 | EXYNOS_DEVICE_TYPE_CONNECTOR); |
Andrzej Hajda | 000cc92 | 2014-04-03 16:26:00 +0200 | [diff] [blame] | 329 | return ERR_PTR(-EPROBE_DEFER); |
Inki Dae | df5225b | 2014-05-29 18:28:02 +0900 | [diff] [blame] | 330 | } |
Andrzej Hajda | 000cc92 | 2014-04-03 16:26:00 +0200 | [diff] [blame] | 331 | } |
| 332 | |
Andrzej Hajda | 4cfde1f | 2014-11-17 09:54:26 +0100 | [diff] [blame] | 333 | return &ctx->display; |
Inki Dae | df5225b | 2014-05-29 18:28:02 +0900 | [diff] [blame] | 334 | |
| 335 | err_del_component: |
| 336 | exynos_drm_component_del(dev, EXYNOS_DEVICE_TYPE_CONNECTOR); |
| 337 | |
| 338 | return NULL; |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 339 | } |
| 340 | |
Andrzej Hajda | 4cfde1f | 2014-11-17 09:54:26 +0100 | [diff] [blame] | 341 | int exynos_dpi_remove(struct exynos_drm_display *display) |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 342 | { |
Andrzej Hajda | 4cfde1f | 2014-11-17 09:54:26 +0100 | [diff] [blame] | 343 | struct exynos_dpi *ctx = display_to_dpi(display); |
Inki Dae | f37cd5e | 2014-05-09 14:25:20 +0900 | [diff] [blame] | 344 | |
Andrzej Hajda | 4cfde1f | 2014-11-17 09:54:26 +0100 | [diff] [blame] | 345 | exynos_dpi_dpms(&ctx->display, DRM_MODE_DPMS_OFF); |
Andrzej Hajda | 90eac89 | 2014-09-09 15:16:11 +0200 | [diff] [blame] | 346 | |
Andrzej Hajda | 90eac89 | 2014-09-09 15:16:11 +0200 | [diff] [blame] | 347 | if (ctx->panel) |
| 348 | drm_panel_detach(ctx->panel); |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 349 | |
Andrzej Hajda | 4cfde1f | 2014-11-17 09:54:26 +0100 | [diff] [blame] | 350 | exynos_drm_component_del(ctx->dev, EXYNOS_DEVICE_TYPE_CONNECTOR); |
Inki Dae | df5225b | 2014-05-29 18:28:02 +0900 | [diff] [blame] | 351 | |
Andrzej Hajda | 14b6873 | 2014-03-17 13:03:56 +0100 | [diff] [blame] | 352 | return 0; |
| 353 | } |