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> | |
255c35f8 | 19 | #include <drm/drm_atomic_helper.h> |
19022aaa SH |
20 | #include <drm/drm_fb_helper.h> |
21 | #include <drm/drm_crtc_helper.h> | |
628f435b | 22 | #include <drm/drm_panel.h> |
19022aaa | 23 | #include <linux/videodev2.h> |
075c457b | 24 | #include <video/of_display_timing.h> |
553a59fc | 25 | #include <linux/of_graph.h> |
19022aaa SH |
26 | |
27 | #include "imx-drm.h" | |
28 | ||
29 | #define con_to_imxpd(x) container_of(x, struct imx_parallel_display, connector) | |
30 | #define enc_to_imxpd(x) container_of(x, struct imx_parallel_display, encoder) | |
31 | ||
32 | struct imx_parallel_display { | |
33 | struct drm_connector connector; | |
19022aaa | 34 | struct drm_encoder encoder; |
19022aaa SH |
35 | struct device *dev; |
36 | void *edid; | |
37 | int edid_len; | |
2872c807 | 38 | u32 bus_format; |
19022aaa | 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 | ||
afb12edf MV |
71 | if (np) { |
72 | struct drm_display_mode *mode = drm_mode_create(connector->dev); | |
63bc5164 | 73 | |
39dcf4f7 FE |
74 | if (!mode) |
75 | return -EINVAL; | |
075c457b | 76 | of_get_drm_display_mode(np, &imxpd->mode, OF_USE_NATIVE_MODE); |
afb12edf MV |
77 | drm_mode_copy(mode, &imxpd->mode); |
78 | mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED, | |
19022aaa SH |
79 | drm_mode_probed_add(connector, mode); |
80 | num_modes++; | |
81 | } | |
82 | ||
83 | return num_modes; | |
84 | } | |
85 | ||
19022aaa SH |
86 | static struct drm_encoder *imx_pd_connector_best_encoder( |
87 | struct drm_connector *connector) | |
88 | { | |
89 | struct imx_parallel_display *imxpd = con_to_imxpd(connector); | |
90 | ||
91 | return &imxpd->encoder; | |
92 | } | |
93 | ||
94 | static void imx_pd_encoder_dpms(struct drm_encoder *encoder, int mode) | |
95 | { | |
628f435b PZ |
96 | struct imx_parallel_display *imxpd = enc_to_imxpd(encoder); |
97 | ||
98 | if (mode != DRM_MODE_DPMS_ON) | |
99 | drm_panel_disable(imxpd->panel); | |
100 | else | |
101 | drm_panel_enable(imxpd->panel); | |
19022aaa SH |
102 | } |
103 | ||
19022aaa SH |
104 | static void imx_pd_encoder_prepare(struct drm_encoder *encoder) |
105 | { | |
106 | struct imx_parallel_display *imxpd = enc_to_imxpd(encoder); | |
4ed094fd PZ |
107 | imx_drm_set_bus_config(encoder, imxpd->bus_format, 2, 3, |
108 | imxpd->connector.display_info.bus_flags); | |
19022aaa SH |
109 | } |
110 | ||
111 | static void imx_pd_encoder_commit(struct drm_encoder *encoder) | |
112 | { | |
ad92c8bf PZ |
113 | struct imx_parallel_display *imxpd = enc_to_imxpd(encoder); |
114 | ||
115 | drm_panel_prepare(imxpd->panel); | |
116 | drm_panel_enable(imxpd->panel); | |
19022aaa SH |
117 | } |
118 | ||
119 | static void imx_pd_encoder_mode_set(struct drm_encoder *encoder, | |
eb10d635 SL |
120 | struct drm_display_mode *orig_mode, |
121 | struct drm_display_mode *mode) | |
19022aaa SH |
122 | { |
123 | } | |
124 | ||
125 | static void imx_pd_encoder_disable(struct drm_encoder *encoder) | |
126 | { | |
ad92c8bf PZ |
127 | struct imx_parallel_display *imxpd = enc_to_imxpd(encoder); |
128 | ||
129 | drm_panel_disable(imxpd->panel); | |
130 | drm_panel_unprepare(imxpd->panel); | |
19022aaa SH |
131 | } |
132 | ||
7ae847dd | 133 | static const struct drm_connector_funcs imx_pd_connector_funcs = { |
19022aaa SH |
134 | .dpms = drm_helper_connector_dpms, |
135 | .fill_modes = drm_helper_probe_single_connector_modes, | |
136 | .detect = imx_pd_connector_detect, | |
1b3f7675 | 137 | .destroy = imx_drm_connector_destroy, |
255c35f8 LY |
138 | .reset = drm_atomic_helper_connector_reset, |
139 | .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, | |
140 | .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, | |
19022aaa SH |
141 | }; |
142 | ||
7ae847dd | 143 | static const struct drm_connector_helper_funcs imx_pd_connector_helper_funcs = { |
19022aaa SH |
144 | .get_modes = imx_pd_connector_get_modes, |
145 | .best_encoder = imx_pd_connector_best_encoder, | |
19022aaa SH |
146 | }; |
147 | ||
7ae847dd | 148 | static const struct drm_encoder_funcs imx_pd_encoder_funcs = { |
1b3f7675 | 149 | .destroy = imx_drm_encoder_destroy, |
19022aaa SH |
150 | }; |
151 | ||
7ae847dd | 152 | static const struct drm_encoder_helper_funcs imx_pd_encoder_helper_funcs = { |
19022aaa | 153 | .dpms = imx_pd_encoder_dpms, |
19022aaa SH |
154 | .prepare = imx_pd_encoder_prepare, |
155 | .commit = imx_pd_encoder_commit, | |
156 | .mode_set = imx_pd_encoder_mode_set, | |
157 | .disable = imx_pd_encoder_disable, | |
158 | }; | |
159 | ||
1b3f7675 RK |
160 | static int imx_pd_register(struct drm_device *drm, |
161 | struct imx_parallel_display *imxpd) | |
19022aaa SH |
162 | { |
163 | int ret; | |
164 | ||
1b3f7675 RK |
165 | ret = imx_drm_encoder_parse_of(drm, &imxpd->encoder, |
166 | imxpd->dev->of_node); | |
167 | if (ret) | |
168 | return ret; | |
19022aaa | 169 | |
c026a3f3 DC |
170 | /* set the connector's dpms to OFF so that |
171 | * drm_helper_connector_dpms() won't return | |
172 | * immediately since the current state is ON | |
173 | * at this point. | |
174 | */ | |
175 | imxpd->connector.dpms = DRM_MODE_DPMS_OFF; | |
176 | ||
19022aaa | 177 | drm_encoder_helper_add(&imxpd->encoder, &imx_pd_encoder_helper_funcs); |
1b3f7675 | 178 | drm_encoder_init(drm, &imxpd->encoder, &imx_pd_encoder_funcs, |
13a3d91f | 179 | DRM_MODE_ENCODER_NONE, NULL); |
19022aaa SH |
180 | |
181 | drm_connector_helper_add(&imxpd->connector, | |
182 | &imx_pd_connector_helper_funcs); | |
1b3f7675 RK |
183 | drm_connector_init(drm, &imxpd->connector, &imx_pd_connector_funcs, |
184 | DRM_MODE_CONNECTOR_VGA); | |
19022aaa | 185 | |
628f435b PZ |
186 | if (imxpd->panel) |
187 | drm_panel_attach(imxpd->panel, &imxpd->connector); | |
188 | ||
1b3f7675 | 189 | drm_mode_connector_attach_encoder(&imxpd->connector, &imxpd->encoder); |
19022aaa | 190 | |
19022aaa SH |
191 | return 0; |
192 | } | |
193 | ||
17b5001b | 194 | static int imx_pd_bind(struct device *dev, struct device *master, void *data) |
19022aaa | 195 | { |
1b3f7675 | 196 | struct drm_device *drm = data; |
17b5001b | 197 | struct device_node *np = dev->of_node; |
3a1c117f | 198 | struct device_node *ep; |
19022aaa SH |
199 | const u8 *edidp; |
200 | struct imx_parallel_display *imxpd; | |
201 | int ret; | |
202 | const char *fmt; | |
203 | ||
17b5001b | 204 | imxpd = devm_kzalloc(dev, sizeof(*imxpd), GFP_KERNEL); |
19022aaa SH |
205 | if (!imxpd) |
206 | return -ENOMEM; | |
207 | ||
208 | edidp = of_get_property(np, "edid", &imxpd->edid_len); | |
209 | if (edidp) | |
210 | imxpd->edid = kmemdup(edidp, imxpd->edid_len, GFP_KERNEL); | |
211 | ||
212 | ret = of_property_read_string(np, "interface-pix-fmt", &fmt); | |
213 | if (!ret) { | |
214 | if (!strcmp(fmt, "rgb24")) | |
2872c807 | 215 | imxpd->bus_format = MEDIA_BUS_FMT_RGB888_1X24; |
19022aaa | 216 | else if (!strcmp(fmt, "rgb565")) |
2872c807 | 217 | imxpd->bus_format = MEDIA_BUS_FMT_RGB565_1X16; |
7d0a66c0 | 218 | else if (!strcmp(fmt, "bgr666")) |
2872c807 | 219 | imxpd->bus_format = MEDIA_BUS_FMT_RGB666_1X18; |
9e74d292 | 220 | else if (!strcmp(fmt, "lvds666")) |
2872c807 | 221 | imxpd->bus_format = MEDIA_BUS_FMT_RGB666_1X24_CPADHI; |
19022aaa SH |
222 | } |
223 | ||
553a59fc | 224 | /* port@1 is the output port */ |
3a1c117f PZ |
225 | ep = of_graph_get_endpoint_by_regs(np, 1, -1); |
226 | if (ep) { | |
227 | struct device_node *remote; | |
228 | ||
229 | remote = of_graph_get_remote_port_parent(ep); | |
230 | of_node_put(ep); | |
231 | if (remote) { | |
232 | imxpd->panel = of_drm_find_panel(remote); | |
233 | of_node_put(remote); | |
553a59fc | 234 | } |
3a1c117f PZ |
235 | if (!imxpd->panel) |
236 | return -EPROBE_DEFER; | |
d70e96ae | 237 | } |
628f435b | 238 | |
17b5001b | 239 | imxpd->dev = dev; |
19022aaa | 240 | |
1b3f7675 | 241 | ret = imx_pd_register(drm, imxpd); |
19022aaa SH |
242 | if (ret) |
243 | return ret; | |
244 | ||
17b5001b | 245 | dev_set_drvdata(dev, imxpd); |
19022aaa SH |
246 | |
247 | return 0; | |
248 | } | |
249 | ||
17b5001b RK |
250 | static void imx_pd_unbind(struct device *dev, struct device *master, |
251 | void *data) | |
19022aaa | 252 | { |
17b5001b | 253 | struct imx_parallel_display *imxpd = dev_get_drvdata(dev); |
19022aaa | 254 | |
1b3f7675 RK |
255 | imxpd->encoder.funcs->destroy(&imxpd->encoder); |
256 | imxpd->connector.funcs->destroy(&imxpd->connector); | |
dcbc9eb1 PS |
257 | |
258 | kfree(imxpd->edid); | |
17b5001b | 259 | } |
19022aaa | 260 | |
17b5001b RK |
261 | static const struct component_ops imx_pd_ops = { |
262 | .bind = imx_pd_bind, | |
263 | .unbind = imx_pd_unbind, | |
264 | }; | |
265 | ||
266 | static int imx_pd_probe(struct platform_device *pdev) | |
267 | { | |
268 | return component_add(&pdev->dev, &imx_pd_ops); | |
269 | } | |
270 | ||
271 | static int imx_pd_remove(struct platform_device *pdev) | |
272 | { | |
273 | component_del(&pdev->dev, &imx_pd_ops); | |
dcbc9eb1 | 274 | |
19022aaa SH |
275 | return 0; |
276 | } | |
277 | ||
278 | static const struct of_device_id imx_pd_dt_ids[] = { | |
279 | { .compatible = "fsl,imx-parallel-display", }, | |
280 | { /* sentinel */ } | |
281 | }; | |
fa45f2c7 | 282 | MODULE_DEVICE_TABLE(of, imx_pd_dt_ids); |
19022aaa SH |
283 | |
284 | static struct platform_driver imx_pd_driver = { | |
285 | .probe = imx_pd_probe, | |
99c28f10 | 286 | .remove = imx_pd_remove, |
19022aaa SH |
287 | .driver = { |
288 | .of_match_table = imx_pd_dt_ids, | |
289 | .name = "imx-parallel-display", | |
19022aaa SH |
290 | }, |
291 | }; | |
292 | ||
293 | module_platform_driver(imx_pd_driver); | |
294 | ||
295 | MODULE_DESCRIPTION("i.MX parallel display driver"); | |
296 | MODULE_AUTHOR("Sascha Hauer, Pengutronix"); | |
297 | MODULE_LICENSE("GPL"); | |
b2da05ff | 298 | MODULE_ALIAS("platform:imx-parallel-display"); |