Merge branch 'jejb-fixes' into fixes
[deliverable/linux.git] / drivers / gpu / drm / imx / parallel-display.c
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.
14 */
15
16 #include <linux/component.h>
17 #include <linux/module.h>
18 #include <drm/drmP.h>
19 #include <drm/drm_fb_helper.h>
20 #include <drm/drm_crtc_helper.h>
21 #include <drm/drm_panel.h>
22 #include <linux/videodev2.h>
23 #include <video/of_display_timing.h>
24 #include <linux/of_graph.h>
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;
33 struct drm_encoder encoder;
34 struct device *dev;
35 void *edid;
36 int edid_len;
37 u32 bus_format;
38 struct drm_display_mode mode;
39 struct drm_panel *panel;
40 };
41
42 static enum drm_connector_status imx_pd_connector_detect(
43 struct drm_connector *connector, bool force)
44 {
45 return connector_status_connected;
46 }
47
48 static int imx_pd_connector_get_modes(struct drm_connector *connector)
49 {
50 struct imx_parallel_display *imxpd = con_to_imxpd(connector);
51 struct device_node *np = imxpd->dev->of_node;
52 int num_modes = 0;
53
54 if (imxpd->panel && imxpd->panel->funcs &&
55 imxpd->panel->funcs->get_modes) {
56 struct drm_display_info *di = &connector->display_info;
57
58 num_modes = imxpd->panel->funcs->get_modes(imxpd->panel);
59 if (!imxpd->bus_format && di->num_bus_formats)
60 imxpd->bus_format = di->bus_formats[0];
61 if (num_modes > 0)
62 return num_modes;
63 }
64
65 if (imxpd->edid) {
66 drm_mode_connector_update_edid_property(connector, imxpd->edid);
67 num_modes = drm_add_edid_modes(connector, imxpd->edid);
68 }
69
70 if (np) {
71 struct drm_display_mode *mode = drm_mode_create(connector->dev);
72
73 if (!mode)
74 return -EINVAL;
75 of_get_drm_display_mode(np, &imxpd->mode, OF_USE_NATIVE_MODE);
76 drm_mode_copy(mode, &imxpd->mode);
77 mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
78 drm_mode_probed_add(connector, mode);
79 num_modes++;
80 }
81
82 return num_modes;
83 }
84
85 static struct drm_encoder *imx_pd_connector_best_encoder(
86 struct drm_connector *connector)
87 {
88 struct imx_parallel_display *imxpd = con_to_imxpd(connector);
89
90 return &imxpd->encoder;
91 }
92
93 static void imx_pd_encoder_dpms(struct drm_encoder *encoder, int mode)
94 {
95 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
96
97 if (mode != DRM_MODE_DPMS_ON)
98 drm_panel_disable(imxpd->panel);
99 else
100 drm_panel_enable(imxpd->panel);
101 }
102
103 static void imx_pd_encoder_prepare(struct drm_encoder *encoder)
104 {
105 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
106 imx_drm_set_bus_config(encoder, imxpd->bus_format, 2, 3,
107 imxpd->connector.display_info.bus_flags);
108 }
109
110 static void imx_pd_encoder_commit(struct drm_encoder *encoder)
111 {
112 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
113
114 drm_panel_prepare(imxpd->panel);
115 drm_panel_enable(imxpd->panel);
116 }
117
118 static void imx_pd_encoder_mode_set(struct drm_encoder *encoder,
119 struct drm_display_mode *orig_mode,
120 struct drm_display_mode *mode)
121 {
122 }
123
124 static void imx_pd_encoder_disable(struct drm_encoder *encoder)
125 {
126 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
127
128 drm_panel_disable(imxpd->panel);
129 drm_panel_unprepare(imxpd->panel);
130 }
131
132 static const struct drm_connector_funcs imx_pd_connector_funcs = {
133 .dpms = drm_helper_connector_dpms,
134 .fill_modes = drm_helper_probe_single_connector_modes,
135 .detect = imx_pd_connector_detect,
136 .destroy = imx_drm_connector_destroy,
137 };
138
139 static const struct drm_connector_helper_funcs imx_pd_connector_helper_funcs = {
140 .get_modes = imx_pd_connector_get_modes,
141 .best_encoder = imx_pd_connector_best_encoder,
142 };
143
144 static const struct drm_encoder_funcs imx_pd_encoder_funcs = {
145 .destroy = imx_drm_encoder_destroy,
146 };
147
148 static const struct drm_encoder_helper_funcs imx_pd_encoder_helper_funcs = {
149 .dpms = imx_pd_encoder_dpms,
150 .prepare = imx_pd_encoder_prepare,
151 .commit = imx_pd_encoder_commit,
152 .mode_set = imx_pd_encoder_mode_set,
153 .disable = imx_pd_encoder_disable,
154 };
155
156 static int imx_pd_register(struct drm_device *drm,
157 struct imx_parallel_display *imxpd)
158 {
159 int ret;
160
161 ret = imx_drm_encoder_parse_of(drm, &imxpd->encoder,
162 imxpd->dev->of_node);
163 if (ret)
164 return ret;
165
166 /* set the connector's dpms to OFF so that
167 * drm_helper_connector_dpms() won't return
168 * immediately since the current state is ON
169 * at this point.
170 */
171 imxpd->connector.dpms = DRM_MODE_DPMS_OFF;
172
173 drm_encoder_helper_add(&imxpd->encoder, &imx_pd_encoder_helper_funcs);
174 drm_encoder_init(drm, &imxpd->encoder, &imx_pd_encoder_funcs,
175 DRM_MODE_ENCODER_NONE, NULL);
176
177 drm_connector_helper_add(&imxpd->connector,
178 &imx_pd_connector_helper_funcs);
179 drm_connector_init(drm, &imxpd->connector, &imx_pd_connector_funcs,
180 DRM_MODE_CONNECTOR_VGA);
181
182 if (imxpd->panel)
183 drm_panel_attach(imxpd->panel, &imxpd->connector);
184
185 drm_mode_connector_attach_encoder(&imxpd->connector, &imxpd->encoder);
186
187 return 0;
188 }
189
190 static int imx_pd_bind(struct device *dev, struct device *master, void *data)
191 {
192 struct drm_device *drm = data;
193 struct device_node *np = dev->of_node;
194 struct device_node *ep;
195 const u8 *edidp;
196 struct imx_parallel_display *imxpd;
197 int ret;
198 const char *fmt;
199
200 imxpd = devm_kzalloc(dev, sizeof(*imxpd), GFP_KERNEL);
201 if (!imxpd)
202 return -ENOMEM;
203
204 edidp = of_get_property(np, "edid", &imxpd->edid_len);
205 if (edidp)
206 imxpd->edid = kmemdup(edidp, imxpd->edid_len, GFP_KERNEL);
207
208 ret = of_property_read_string(np, "interface-pix-fmt", &fmt);
209 if (!ret) {
210 if (!strcmp(fmt, "rgb24"))
211 imxpd->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
212 else if (!strcmp(fmt, "rgb565"))
213 imxpd->bus_format = MEDIA_BUS_FMT_RGB565_1X16;
214 else if (!strcmp(fmt, "bgr666"))
215 imxpd->bus_format = MEDIA_BUS_FMT_RGB666_1X18;
216 else if (!strcmp(fmt, "lvds666"))
217 imxpd->bus_format = MEDIA_BUS_FMT_RGB666_1X24_CPADHI;
218 }
219
220 /* port@1 is the output port */
221 ep = of_graph_get_endpoint_by_regs(np, 1, -1);
222 if (ep) {
223 struct device_node *remote;
224
225 remote = of_graph_get_remote_port_parent(ep);
226 of_node_put(ep);
227 if (remote) {
228 imxpd->panel = of_drm_find_panel(remote);
229 of_node_put(remote);
230 }
231 if (!imxpd->panel)
232 return -EPROBE_DEFER;
233 }
234
235 imxpd->dev = dev;
236
237 ret = imx_pd_register(drm, imxpd);
238 if (ret)
239 return ret;
240
241 dev_set_drvdata(dev, imxpd);
242
243 return 0;
244 }
245
246 static void imx_pd_unbind(struct device *dev, struct device *master,
247 void *data)
248 {
249 struct imx_parallel_display *imxpd = dev_get_drvdata(dev);
250
251 imxpd->encoder.funcs->destroy(&imxpd->encoder);
252 imxpd->connector.funcs->destroy(&imxpd->connector);
253
254 kfree(imxpd->edid);
255 }
256
257 static const struct component_ops imx_pd_ops = {
258 .bind = imx_pd_bind,
259 .unbind = imx_pd_unbind,
260 };
261
262 static int imx_pd_probe(struct platform_device *pdev)
263 {
264 return component_add(&pdev->dev, &imx_pd_ops);
265 }
266
267 static int imx_pd_remove(struct platform_device *pdev)
268 {
269 component_del(&pdev->dev, &imx_pd_ops);
270
271 return 0;
272 }
273
274 static const struct of_device_id imx_pd_dt_ids[] = {
275 { .compatible = "fsl,imx-parallel-display", },
276 { /* sentinel */ }
277 };
278 MODULE_DEVICE_TABLE(of, imx_pd_dt_ids);
279
280 static struct platform_driver imx_pd_driver = {
281 .probe = imx_pd_probe,
282 .remove = imx_pd_remove,
283 .driver = {
284 .of_match_table = imx_pd_dt_ids,
285 .name = "imx-parallel-display",
286 },
287 };
288
289 module_platform_driver(imx_pd_driver);
290
291 MODULE_DESCRIPTION("i.MX parallel display driver");
292 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
293 MODULE_LICENSE("GPL");
294 MODULE_ALIAS("platform:imx-parallel-display");
This page took 0.039836 seconds and 6 git commands to generate.