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 | ||
800ba2b5 | 60 | static const 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 | ||
800ba2b5 | 103 | static const struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs = { |
14b6873a | 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 void exynos_dpi_mode_set(struct drm_encoder *encoder, |
132 | struct drm_display_mode *mode, | |
133 | struct drm_display_mode *adjusted_mode) | |
134 | { | |
135 | } | |
136 | ||
137 | static void exynos_dpi_enable(struct drm_encoder *encoder) | |
14b6873a | 138 | { |
cf67cc9a | 139 | struct exynos_dpi *ctx = encoder_to_dpi(encoder); |
b6595dc7 | 140 | |
39bbde9c AK |
141 | if (ctx->panel) { |
142 | drm_panel_prepare(ctx->panel); | |
14b6873a | 143 | drm_panel_enable(ctx->panel); |
39bbde9c | 144 | } |
14b6873a AH |
145 | } |
146 | ||
2b8376c8 | 147 | static void exynos_dpi_disable(struct drm_encoder *encoder) |
14b6873a | 148 | { |
cf67cc9a | 149 | struct exynos_dpi *ctx = encoder_to_dpi(encoder); |
b6595dc7 | 150 | |
39bbde9c | 151 | if (ctx->panel) { |
14b6873a | 152 | drm_panel_disable(ctx->panel); |
39bbde9c AK |
153 | drm_panel_unprepare(ctx->panel); |
154 | } | |
14b6873a AH |
155 | } |
156 | ||
800ba2b5 | 157 | static const struct drm_encoder_helper_funcs exynos_dpi_encoder_helper_funcs = { |
2b8376c8 | 158 | .mode_set = exynos_dpi_mode_set, |
b6595dc7 GP |
159 | .enable = exynos_dpi_enable, |
160 | .disable = exynos_dpi_disable, | |
14b6873a AH |
161 | }; |
162 | ||
800ba2b5 | 163 | static const struct drm_encoder_funcs exynos_dpi_encoder_funcs = { |
2b8376c8 GP |
164 | .destroy = drm_encoder_cleanup, |
165 | }; | |
166 | ||
14b6873a AH |
167 | /* of_* functions will be removed after merge of of_graph patches */ |
168 | static struct device_node * | |
169 | of_get_child_by_name_reg(struct device_node *parent, const char *name, u32 reg) | |
170 | { | |
171 | struct device_node *np; | |
172 | ||
173 | for_each_child_of_node(parent, np) { | |
174 | u32 r; | |
175 | ||
176 | if (!np->name || of_node_cmp(np->name, name)) | |
177 | continue; | |
178 | ||
179 | if (of_property_read_u32(np, "reg", &r) < 0) | |
180 | r = 0; | |
181 | ||
182 | if (reg == r) | |
183 | break; | |
184 | } | |
185 | ||
186 | return np; | |
187 | } | |
188 | ||
189 | static struct device_node *of_graph_get_port_by_reg(struct device_node *parent, | |
190 | u32 reg) | |
191 | { | |
192 | struct device_node *ports, *port; | |
193 | ||
194 | ports = of_get_child_by_name(parent, "ports"); | |
195 | if (ports) | |
196 | parent = ports; | |
197 | ||
198 | port = of_get_child_by_name_reg(parent, "port", reg); | |
199 | ||
200 | of_node_put(ports); | |
201 | ||
202 | return port; | |
203 | } | |
204 | ||
205 | static struct device_node * | |
206 | of_graph_get_endpoint_by_reg(struct device_node *port, u32 reg) | |
207 | { | |
208 | return of_get_child_by_name_reg(port, "endpoint", reg); | |
209 | } | |
210 | ||
211 | static struct device_node * | |
212 | of_graph_get_remote_port_parent(const struct device_node *node) | |
213 | { | |
214 | struct device_node *np; | |
215 | unsigned int depth; | |
216 | ||
217 | np = of_parse_phandle(node, "remote-endpoint", 0); | |
218 | ||
219 | /* Walk 3 levels up only if there is 'ports' node. */ | |
220 | for (depth = 3; depth && np; depth--) { | |
221 | np = of_get_next_parent(np); | |
222 | if (depth == 2 && of_node_cmp(np->name, "ports")) | |
223 | break; | |
224 | } | |
225 | return np; | |
226 | } | |
227 | ||
228 | enum { | |
229 | FIMD_PORT_IN0, | |
230 | FIMD_PORT_IN1, | |
231 | FIMD_PORT_IN2, | |
232 | FIMD_PORT_RGB, | |
233 | FIMD_PORT_WRB, | |
234 | }; | |
235 | ||
f46ed1ab | 236 | static struct device_node *exynos_dpi_of_find_panel_node(struct device *dev) |
14b6873a AH |
237 | { |
238 | struct device_node *np, *ep; | |
239 | ||
240 | np = of_graph_get_port_by_reg(dev->of_node, FIMD_PORT_RGB); | |
241 | if (!np) | |
242 | return NULL; | |
243 | ||
244 | ep = of_graph_get_endpoint_by_reg(np, 0); | |
245 | of_node_put(np); | |
246 | if (!ep) | |
247 | return NULL; | |
248 | ||
249 | np = of_graph_get_remote_port_parent(ep); | |
250 | of_node_put(ep); | |
251 | ||
252 | return np; | |
253 | } | |
254 | ||
255 | static int exynos_dpi_parse_dt(struct exynos_dpi *ctx) | |
256 | { | |
257 | struct device *dev = ctx->dev; | |
258 | struct device_node *dn = dev->of_node; | |
259 | struct device_node *np; | |
260 | ||
261 | ctx->panel_node = exynos_dpi_of_find_panel_node(dev); | |
262 | ||
263 | np = of_get_child_by_name(dn, "display-timings"); | |
264 | if (np) { | |
265 | struct videomode *vm; | |
266 | int ret; | |
267 | ||
268 | of_node_put(np); | |
269 | ||
270 | vm = devm_kzalloc(dev, sizeof(*ctx->vm), GFP_KERNEL); | |
271 | if (!vm) | |
272 | return -ENOMEM; | |
273 | ||
274 | ret = of_get_videomode(dn, vm, 0); | |
000cc920 AH |
275 | if (ret < 0) { |
276 | devm_kfree(dev, vm); | |
14b6873a | 277 | return ret; |
000cc920 | 278 | } |
14b6873a AH |
279 | |
280 | ctx->vm = vm; | |
281 | ||
282 | return 0; | |
283 | } | |
284 | ||
285 | if (!ctx->panel_node) | |
286 | return -EINVAL; | |
287 | ||
288 | return 0; | |
289 | } | |
290 | ||
2b8376c8 | 291 | int exynos_dpi_bind(struct drm_device *dev, struct drm_encoder *encoder) |
a2986e80 GP |
292 | { |
293 | int ret; | |
294 | ||
2b8376c8 GP |
295 | ret = exynos_drm_crtc_get_pipe_from_type(dev, EXYNOS_DISPLAY_TYPE_LCD); |
296 | if (ret < 0) | |
a2986e80 | 297 | return ret; |
a2986e80 | 298 | |
2b8376c8 GP |
299 | encoder->possible_crtcs = 1 << ret; |
300 | ||
301 | DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs); | |
302 | ||
303 | drm_encoder_init(dev, encoder, &exynos_dpi_encoder_funcs, | |
13a3d91f | 304 | DRM_MODE_ENCODER_TMDS, NULL); |
2b8376c8 GP |
305 | |
306 | drm_encoder_helper_add(encoder, &exynos_dpi_encoder_helper_funcs); | |
307 | ||
308 | ret = exynos_dpi_create_connector(encoder); | |
a2986e80 GP |
309 | if (ret) { |
310 | DRM_ERROR("failed to create connector ret = %d\n", ret); | |
2b8376c8 | 311 | drm_encoder_cleanup(encoder); |
a2986e80 GP |
312 | return ret; |
313 | } | |
314 | ||
315 | return 0; | |
316 | } | |
317 | ||
2b8376c8 | 318 | struct drm_encoder *exynos_dpi_probe(struct device *dev) |
14b6873a AH |
319 | { |
320 | struct exynos_dpi *ctx; | |
321 | int ret; | |
322 | ||
323 | ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); | |
324 | if (!ctx) | |
4cfde1f2 | 325 | return ERR_PTR(-ENOMEM); |
14b6873a AH |
326 | |
327 | ctx->dev = dev; | |
14b6873a AH |
328 | |
329 | ret = exynos_dpi_parse_dt(ctx); | |
000cc920 AH |
330 | if (ret < 0) { |
331 | devm_kfree(dev, ctx); | |
86650408 | 332 | return NULL; |
000cc920 AH |
333 | } |
334 | ||
335 | if (ctx->panel_node) { | |
336 | ctx->panel = of_drm_find_panel(ctx->panel_node); | |
86650408 | 337 | if (!ctx->panel) |
000cc920 AH |
338 | return ERR_PTR(-EPROBE_DEFER); |
339 | } | |
14b6873a | 340 | |
cf67cc9a | 341 | return &ctx->encoder; |
14b6873a AH |
342 | } |
343 | ||
2b8376c8 | 344 | int exynos_dpi_remove(struct drm_encoder *encoder) |
14b6873a | 345 | { |
cf67cc9a | 346 | struct exynos_dpi *ctx = encoder_to_dpi(encoder); |
f37cd5e8 | 347 | |
cf67cc9a | 348 | exynos_dpi_disable(&ctx->encoder); |
90eac897 | 349 | |
90eac897 AH |
350 | if (ctx->panel) |
351 | drm_panel_detach(ctx->panel); | |
14b6873a AH |
352 | |
353 | return 0; | |
354 | } |