Commit | Line | Data |
---|---|---|
14b6873a AH |
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> | |
4ea9526b | 16 | #include <drm/drm_atomic_helper.h> |
14b6873a AH |
17 | |
18 | #include <linux/regulator/consumer.h> | |
19 | ||
20 | #include <video/of_videomode.h> | |
21 | #include <video/videomode.h> | |
22 | ||
a2986e80 | 23 | #include "exynos_drm_crtc.h" |
14b6873a AH |
24 | |
25 | struct exynos_dpi { | |
2b8376c8 | 26 | struct drm_encoder encoder; |
14b6873a AH |
27 | struct device *dev; |
28 | struct device_node *panel_node; | |
29 | ||
30 | struct drm_panel *panel; | |
31 | struct drm_connector connector; | |
14b6873a AH |
32 | |
33 | struct videomode *vm; | |
14b6873a AH |
34 | }; |
35 | ||
36 | #define connector_to_dpi(c) container_of(c, struct exynos_dpi, connector) | |
37 | ||
2b8376c8 | 38 | static inline struct exynos_dpi *encoder_to_dpi(struct drm_encoder *e) |
4cfde1f2 | 39 | { |
cf67cc9a | 40 | return container_of(e, struct exynos_dpi, encoder); |
4cfde1f2 AH |
41 | } |
42 | ||
14b6873a AH |
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 | ||
aaa51b13 | 48 | if (ctx->panel && !ctx->panel->connector) |
000cc920 | 49 | drm_panel_attach(ctx->panel, &ctx->connector); |
14b6873a | 50 | |
000cc920 | 51 | return connector_status_connected; |
14b6873a AH |
52 | } |
53 | ||
54 | static void exynos_dpi_connector_destroy(struct drm_connector *connector) | |
55 | { | |
34ea3d38 | 56 | drm_connector_unregister(connector); |
14b6873a AH |
57 | drm_connector_cleanup(connector); |
58 | } | |
59 | ||
60 | static struct drm_connector_funcs exynos_dpi_connector_funcs = { | |
63498e30 | 61 | .dpms = drm_atomic_helper_connector_dpms, |
14b6873a AH |
62 | .detect = exynos_dpi_detect, |
63 | .fill_modes = drm_helper_probe_single_connector_modes, | |
64 | .destroy = exynos_dpi_connector_destroy, | |
4ea9526b GP |
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, | |
14b6873a AH |
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 | ||
14b6873a AH |
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 | ||
2b8376c8 | 100 | return &ctx->encoder; |
14b6873a AH |
101 | } |
102 | ||
103 | static struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs = { | |
104 | .get_modes = exynos_dpi_get_modes, | |
14b6873a AH |
105 | .best_encoder = exynos_dpi_best_encoder, |
106 | }; | |
107 | ||
2b8376c8 | 108 | static int exynos_dpi_create_connector(struct drm_encoder *encoder) |
14b6873a | 109 | { |
2b8376c8 | 110 | struct exynos_dpi *ctx = encoder_to_dpi(encoder); |
14b6873a AH |
111 | struct drm_connector *connector = &ctx->connector; |
112 | int ret; | |
113 | ||
ae08fe6c | 114 | connector->polled = DRM_CONNECTOR_POLL_HPD; |
14b6873a AH |
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); | |
34ea3d38 | 125 | drm_connector_register(connector); |
14b6873a AH |
126 | drm_mode_connector_attach_encoder(connector, encoder); |
127 | ||
128 | return 0; | |
129 | } | |
130 | ||
2b8376c8 GP |
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) | |
14b6873a | 145 | { |
cf67cc9a | 146 | struct exynos_dpi *ctx = encoder_to_dpi(encoder); |
b6595dc7 | 147 | |
39bbde9c AK |
148 | if (ctx->panel) { |
149 | drm_panel_prepare(ctx->panel); | |
14b6873a | 150 | drm_panel_enable(ctx->panel); |
39bbde9c | 151 | } |
14b6873a AH |
152 | } |
153 | ||
2b8376c8 | 154 | static void exynos_dpi_disable(struct drm_encoder *encoder) |
14b6873a | 155 | { |
cf67cc9a | 156 | struct exynos_dpi *ctx = encoder_to_dpi(encoder); |
b6595dc7 | 157 | |
39bbde9c | 158 | if (ctx->panel) { |
14b6873a | 159 | drm_panel_disable(ctx->panel); |
39bbde9c AK |
160 | drm_panel_unprepare(ctx->panel); |
161 | } | |
14b6873a AH |
162 | } |
163 | ||
2b8376c8 GP |
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, | |
b6595dc7 GP |
167 | .enable = exynos_dpi_enable, |
168 | .disable = exynos_dpi_disable, | |
14b6873a AH |
169 | }; |
170 | ||
2b8376c8 GP |
171 | static struct drm_encoder_funcs exynos_dpi_encoder_funcs = { |
172 | .destroy = drm_encoder_cleanup, | |
173 | }; | |
174 | ||
14b6873a AH |
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 | ||
f46ed1ab | 244 | static struct device_node *exynos_dpi_of_find_panel_node(struct device *dev) |
14b6873a AH |
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); | |
000cc920 AH |
283 | if (ret < 0) { |
284 | devm_kfree(dev, vm); | |
14b6873a | 285 | return ret; |
000cc920 | 286 | } |
14b6873a AH |
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 | ||
2b8376c8 | 299 | int exynos_dpi_bind(struct drm_device *dev, struct drm_encoder *encoder) |
a2986e80 GP |
300 | { |
301 | int ret; | |
302 | ||
2b8376c8 GP |
303 | ret = exynos_drm_crtc_get_pipe_from_type(dev, EXYNOS_DISPLAY_TYPE_LCD); |
304 | if (ret < 0) | |
a2986e80 | 305 | return ret; |
a2986e80 | 306 | |
2b8376c8 GP |
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, | |
13a3d91f | 312 | DRM_MODE_ENCODER_TMDS, NULL); |
2b8376c8 GP |
313 | |
314 | drm_encoder_helper_add(encoder, &exynos_dpi_encoder_helper_funcs); | |
315 | ||
316 | ret = exynos_dpi_create_connector(encoder); | |
a2986e80 GP |
317 | if (ret) { |
318 | DRM_ERROR("failed to create connector ret = %d\n", ret); | |
2b8376c8 | 319 | drm_encoder_cleanup(encoder); |
a2986e80 GP |
320 | return ret; |
321 | } | |
322 | ||
323 | return 0; | |
324 | } | |
325 | ||
2b8376c8 | 326 | struct drm_encoder *exynos_dpi_probe(struct device *dev) |
14b6873a AH |
327 | { |
328 | struct exynos_dpi *ctx; | |
329 | int ret; | |
330 | ||
331 | ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); | |
332 | if (!ctx) | |
4cfde1f2 | 333 | return ERR_PTR(-ENOMEM); |
14b6873a AH |
334 | |
335 | ctx->dev = dev; | |
14b6873a AH |
336 | |
337 | ret = exynos_dpi_parse_dt(ctx); | |
000cc920 AH |
338 | if (ret < 0) { |
339 | devm_kfree(dev, ctx); | |
86650408 | 340 | return NULL; |
000cc920 AH |
341 | } |
342 | ||
343 | if (ctx->panel_node) { | |
344 | ctx->panel = of_drm_find_panel(ctx->panel_node); | |
86650408 | 345 | if (!ctx->panel) |
000cc920 AH |
346 | return ERR_PTR(-EPROBE_DEFER); |
347 | } | |
14b6873a | 348 | |
cf67cc9a | 349 | return &ctx->encoder; |
14b6873a AH |
350 | } |
351 | ||
2b8376c8 | 352 | int exynos_dpi_remove(struct drm_encoder *encoder) |
14b6873a | 353 | { |
cf67cc9a | 354 | struct exynos_dpi *ctx = encoder_to_dpi(encoder); |
f37cd5e8 | 355 | |
cf67cc9a | 356 | exynos_dpi_disable(&ctx->encoder); |
90eac897 | 357 | |
90eac897 AH |
358 | if (ctx->panel) |
359 | drm_panel_detach(ctx->panel); | |
14b6873a AH |
360 | |
361 | return 0; | |
362 | } |