Commit | Line | Data |
---|---|---|
19022aaa SH |
1 | /* |
2 | * i.MX drm driver - parallel display implementation | |
3 | * | |
4 | * Copyright (C) 2012 Sascha Hauer, Pengutronix | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version 2 | |
9 | * of the License, or (at your option) any later version. | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
19022aaa SH |
14 | */ |
15 | ||
17b5001b | 16 | #include <linux/component.h> |
19022aaa SH |
17 | #include <linux/module.h> |
18 | #include <drm/drmP.h> | |
19 | #include <drm/drm_fb_helper.h> | |
20 | #include <drm/drm_crtc_helper.h> | |
628f435b | 21 | #include <drm/drm_panel.h> |
19022aaa | 22 | #include <linux/videodev2.h> |
075c457b | 23 | #include <video/of_display_timing.h> |
553a59fc | 24 | #include <linux/of_graph.h> |
19022aaa SH |
25 | |
26 | #include "imx-drm.h" | |
27 | ||
28 | #define con_to_imxpd(x) container_of(x, struct imx_parallel_display, connector) | |
29 | #define enc_to_imxpd(x) container_of(x, struct imx_parallel_display, encoder) | |
30 | ||
31 | struct imx_parallel_display { | |
32 | struct drm_connector connector; | |
19022aaa | 33 | struct drm_encoder encoder; |
19022aaa SH |
34 | struct device *dev; |
35 | void *edid; | |
36 | int edid_len; | |
2872c807 | 37 | u32 bus_format; |
19022aaa SH |
38 | int mode_valid; |
39 | struct drm_display_mode mode; | |
628f435b | 40 | struct drm_panel *panel; |
19022aaa SH |
41 | }; |
42 | ||
43 | static enum drm_connector_status imx_pd_connector_detect( | |
44 | struct drm_connector *connector, bool force) | |
45 | { | |
46 | return connector_status_connected; | |
47 | } | |
48 | ||
19022aaa SH |
49 | static int imx_pd_connector_get_modes(struct drm_connector *connector) |
50 | { | |
51 | struct imx_parallel_display *imxpd = con_to_imxpd(connector); | |
afb12edf | 52 | struct device_node *np = imxpd->dev->of_node; |
19022aaa SH |
53 | int num_modes = 0; |
54 | ||
628f435b PZ |
55 | if (imxpd->panel && imxpd->panel->funcs && |
56 | imxpd->panel->funcs->get_modes) { | |
9832e811 PZ |
57 | struct drm_display_info *di = &connector->display_info; |
58 | ||
628f435b | 59 | num_modes = imxpd->panel->funcs->get_modes(imxpd->panel); |
9832e811 PZ |
60 | if (!imxpd->bus_format && di->num_bus_formats) |
61 | imxpd->bus_format = di->bus_formats[0]; | |
628f435b PZ |
62 | if (num_modes > 0) |
63 | return num_modes; | |
64 | } | |
65 | ||
19022aaa SH |
66 | if (imxpd->edid) { |
67 | drm_mode_connector_update_edid_property(connector, imxpd->edid); | |
68 | num_modes = drm_add_edid_modes(connector, imxpd->edid); | |
69 | } | |
70 | ||
71 | if (imxpd->mode_valid) { | |
72 | struct drm_display_mode *mode = drm_mode_create(connector->dev); | |
63bc5164 | 73 | |
39dcf4f7 FE |
74 | if (!mode) |
75 | return -EINVAL; | |
19022aaa SH |
76 | drm_mode_copy(mode, &imxpd->mode); |
77 | mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED, | |
afb12edf MV |
78 | drm_mode_probed_add(connector, mode); |
79 | num_modes++; | |
80 | } | |
81 | ||
82 | if (np) { | |
83 | struct drm_display_mode *mode = drm_mode_create(connector->dev); | |
63bc5164 | 84 | |
39dcf4f7 FE |
85 | if (!mode) |
86 | return -EINVAL; | |
075c457b | 87 | of_get_drm_display_mode(np, &imxpd->mode, OF_USE_NATIVE_MODE); |
afb12edf MV |
88 | drm_mode_copy(mode, &imxpd->mode); |
89 | mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED, | |
19022aaa SH |
90 | drm_mode_probed_add(connector, mode); |
91 | num_modes++; | |
92 | } | |
93 | ||
94 | return num_modes; | |
95 | } | |
96 | ||
19022aaa SH |
97 | static struct drm_encoder *imx_pd_connector_best_encoder( |
98 | struct drm_connector *connector) | |
99 | { | |
100 | struct imx_parallel_display *imxpd = con_to_imxpd(connector); | |
101 | ||
102 | return &imxpd->encoder; | |
103 | } | |
104 | ||
105 | static void imx_pd_encoder_dpms(struct drm_encoder *encoder, int mode) | |
106 | { | |
628f435b PZ |
107 | struct imx_parallel_display *imxpd = enc_to_imxpd(encoder); |
108 | ||
109 | if (mode != DRM_MODE_DPMS_ON) | |
110 | drm_panel_disable(imxpd->panel); | |
111 | else | |
112 | drm_panel_enable(imxpd->panel); | |
19022aaa SH |
113 | } |
114 | ||
19022aaa SH |
115 | static void imx_pd_encoder_prepare(struct drm_encoder *encoder) |
116 | { | |
117 | struct imx_parallel_display *imxpd = enc_to_imxpd(encoder); | |
118 | ||
2872c807 | 119 | imx_drm_set_bus_format(encoder, imxpd->bus_format); |
19022aaa SH |
120 | } |
121 | ||
122 | static void imx_pd_encoder_commit(struct drm_encoder *encoder) | |
123 | { | |
ad92c8bf PZ |
124 | struct imx_parallel_display *imxpd = enc_to_imxpd(encoder); |
125 | ||
126 | drm_panel_prepare(imxpd->panel); | |
127 | drm_panel_enable(imxpd->panel); | |
19022aaa SH |
128 | } |
129 | ||
130 | static void imx_pd_encoder_mode_set(struct drm_encoder *encoder, | |
eb10d635 SL |
131 | struct drm_display_mode *orig_mode, |
132 | struct drm_display_mode *mode) | |
19022aaa SH |
133 | { |
134 | } | |
135 | ||
136 | static void imx_pd_encoder_disable(struct drm_encoder *encoder) | |
137 | { | |
ad92c8bf PZ |
138 | struct imx_parallel_display *imxpd = enc_to_imxpd(encoder); |
139 | ||
140 | drm_panel_disable(imxpd->panel); | |
141 | drm_panel_unprepare(imxpd->panel); | |
19022aaa SH |
142 | } |
143 | ||
7ae847dd | 144 | static const struct drm_connector_funcs imx_pd_connector_funcs = { |
19022aaa SH |
145 | .dpms = drm_helper_connector_dpms, |
146 | .fill_modes = drm_helper_probe_single_connector_modes, | |
147 | .detect = imx_pd_connector_detect, | |
1b3f7675 | 148 | .destroy = imx_drm_connector_destroy, |
19022aaa SH |
149 | }; |
150 | ||
7ae847dd | 151 | static const struct drm_connector_helper_funcs imx_pd_connector_helper_funcs = { |
19022aaa SH |
152 | .get_modes = imx_pd_connector_get_modes, |
153 | .best_encoder = imx_pd_connector_best_encoder, | |
19022aaa SH |
154 | }; |
155 | ||
7ae847dd | 156 | static const struct drm_encoder_funcs imx_pd_encoder_funcs = { |
1b3f7675 | 157 | .destroy = imx_drm_encoder_destroy, |
19022aaa SH |
158 | }; |
159 | ||
7ae847dd | 160 | static const struct drm_encoder_helper_funcs imx_pd_encoder_helper_funcs = { |
19022aaa | 161 | .dpms = imx_pd_encoder_dpms, |
19022aaa SH |
162 | .prepare = imx_pd_encoder_prepare, |
163 | .commit = imx_pd_encoder_commit, | |
164 | .mode_set = imx_pd_encoder_mode_set, | |
165 | .disable = imx_pd_encoder_disable, | |
166 | }; | |
167 | ||
1b3f7675 RK |
168 | static int imx_pd_register(struct drm_device *drm, |
169 | struct imx_parallel_display *imxpd) | |
19022aaa SH |
170 | { |
171 | int ret; | |
172 | ||
1b3f7675 RK |
173 | ret = imx_drm_encoder_parse_of(drm, &imxpd->encoder, |
174 | imxpd->dev->of_node); | |
175 | if (ret) | |
176 | return ret; | |
19022aaa | 177 | |
c026a3f3 DC |
178 | /* set the connector's dpms to OFF so that |
179 | * drm_helper_connector_dpms() won't return | |
180 | * immediately since the current state is ON | |
181 | * at this point. | |
182 | */ | |
183 | imxpd->connector.dpms = DRM_MODE_DPMS_OFF; | |
184 | ||
19022aaa | 185 | drm_encoder_helper_add(&imxpd->encoder, &imx_pd_encoder_helper_funcs); |
1b3f7675 | 186 | drm_encoder_init(drm, &imxpd->encoder, &imx_pd_encoder_funcs, |
13a3d91f | 187 | DRM_MODE_ENCODER_NONE, NULL); |
19022aaa SH |
188 | |
189 | drm_connector_helper_add(&imxpd->connector, | |
190 | &imx_pd_connector_helper_funcs); | |
1b3f7675 RK |
191 | drm_connector_init(drm, &imxpd->connector, &imx_pd_connector_funcs, |
192 | DRM_MODE_CONNECTOR_VGA); | |
19022aaa | 193 | |
628f435b PZ |
194 | if (imxpd->panel) |
195 | drm_panel_attach(imxpd->panel, &imxpd->connector); | |
196 | ||
1b3f7675 | 197 | drm_mode_connector_attach_encoder(&imxpd->connector, &imxpd->encoder); |
19022aaa | 198 | |
19022aaa SH |
199 | return 0; |
200 | } | |
201 | ||
17b5001b | 202 | static int imx_pd_bind(struct device *dev, struct device *master, void *data) |
19022aaa | 203 | { |
1b3f7675 | 204 | struct drm_device *drm = data; |
17b5001b | 205 | struct device_node *np = dev->of_node; |
553a59fc | 206 | struct device_node *port; |
19022aaa SH |
207 | const u8 *edidp; |
208 | struct imx_parallel_display *imxpd; | |
209 | int ret; | |
210 | const char *fmt; | |
211 | ||
17b5001b | 212 | imxpd = devm_kzalloc(dev, sizeof(*imxpd), GFP_KERNEL); |
19022aaa SH |
213 | if (!imxpd) |
214 | return -ENOMEM; | |
215 | ||
216 | edidp = of_get_property(np, "edid", &imxpd->edid_len); | |
217 | if (edidp) | |
218 | imxpd->edid = kmemdup(edidp, imxpd->edid_len, GFP_KERNEL); | |
219 | ||
220 | ret = of_property_read_string(np, "interface-pix-fmt", &fmt); | |
221 | if (!ret) { | |
222 | if (!strcmp(fmt, "rgb24")) | |
2872c807 | 223 | imxpd->bus_format = MEDIA_BUS_FMT_RGB888_1X24; |
19022aaa | 224 | else if (!strcmp(fmt, "rgb565")) |
2872c807 | 225 | imxpd->bus_format = MEDIA_BUS_FMT_RGB565_1X16; |
7d0a66c0 | 226 | else if (!strcmp(fmt, "bgr666")) |
2872c807 | 227 | imxpd->bus_format = MEDIA_BUS_FMT_RGB666_1X18; |
9e74d292 | 228 | else if (!strcmp(fmt, "lvds666")) |
2872c807 | 229 | imxpd->bus_format = MEDIA_BUS_FMT_RGB666_1X24_CPADHI; |
19022aaa SH |
230 | } |
231 | ||
553a59fc PZ |
232 | /* port@1 is the output port */ |
233 | port = of_graph_get_port_by_id(np, 1); | |
234 | if (port) { | |
235 | struct device_node *endpoint, *remote; | |
236 | ||
237 | endpoint = of_get_child_by_name(port, "endpoint"); | |
238 | if (endpoint) { | |
239 | remote = of_graph_get_remote_port_parent(endpoint); | |
240 | if (remote) | |
241 | imxpd->panel = of_drm_find_panel(remote); | |
242 | if (!imxpd->panel) | |
243 | return -EPROBE_DEFER; | |
244 | } | |
d70e96ae | 245 | } |
628f435b | 246 | |
17b5001b | 247 | imxpd->dev = dev; |
19022aaa | 248 | |
1b3f7675 | 249 | ret = imx_pd_register(drm, imxpd); |
19022aaa SH |
250 | if (ret) |
251 | return ret; | |
252 | ||
17b5001b | 253 | dev_set_drvdata(dev, imxpd); |
19022aaa SH |
254 | |
255 | return 0; | |
256 | } | |
257 | ||
17b5001b RK |
258 | static void imx_pd_unbind(struct device *dev, struct device *master, |
259 | void *data) | |
19022aaa | 260 | { |
17b5001b | 261 | struct imx_parallel_display *imxpd = dev_get_drvdata(dev); |
19022aaa | 262 | |
1b3f7675 RK |
263 | imxpd->encoder.funcs->destroy(&imxpd->encoder); |
264 | imxpd->connector.funcs->destroy(&imxpd->connector); | |
dcbc9eb1 PS |
265 | |
266 | kfree(imxpd->edid); | |
17b5001b | 267 | } |
19022aaa | 268 | |
17b5001b RK |
269 | static const struct component_ops imx_pd_ops = { |
270 | .bind = imx_pd_bind, | |
271 | .unbind = imx_pd_unbind, | |
272 | }; | |
273 | ||
274 | static int imx_pd_probe(struct platform_device *pdev) | |
275 | { | |
276 | return component_add(&pdev->dev, &imx_pd_ops); | |
277 | } | |
278 | ||
279 | static int imx_pd_remove(struct platform_device *pdev) | |
280 | { | |
281 | component_del(&pdev->dev, &imx_pd_ops); | |
dcbc9eb1 | 282 | |
19022aaa SH |
283 | return 0; |
284 | } | |
285 | ||
286 | static const struct of_device_id imx_pd_dt_ids[] = { | |
287 | { .compatible = "fsl,imx-parallel-display", }, | |
288 | { /* sentinel */ } | |
289 | }; | |
fa45f2c7 | 290 | MODULE_DEVICE_TABLE(of, imx_pd_dt_ids); |
19022aaa SH |
291 | |
292 | static struct platform_driver imx_pd_driver = { | |
293 | .probe = imx_pd_probe, | |
99c28f10 | 294 | .remove = imx_pd_remove, |
19022aaa SH |
295 | .driver = { |
296 | .of_match_table = imx_pd_dt_ids, | |
297 | .name = "imx-parallel-display", | |
19022aaa SH |
298 | }, |
299 | }; | |
300 | ||
301 | module_platform_driver(imx_pd_driver); | |
302 | ||
303 | MODULE_DESCRIPTION("i.MX parallel display driver"); | |
304 | MODULE_AUTHOR("Sascha Hauer, Pengutronix"); | |
305 | MODULE_LICENSE("GPL"); | |
b2da05ff | 306 | MODULE_ALIAS("platform:imx-parallel-display"); |