2 * i.MX drm driver - parallel display implementation
4 * Copyright (C) 2012 Sascha Hauer, Pengutronix
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.
16 #include <linux/component.h>
17 #include <linux/module.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>
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)
31 struct imx_parallel_display
{
32 struct drm_connector connector
;
33 struct drm_encoder encoder
;
38 struct drm_display_mode mode
;
39 struct drm_panel
*panel
;
42 static enum drm_connector_status
imx_pd_connector_detect(
43 struct drm_connector
*connector
, bool force
)
45 return connector_status_connected
;
48 static int imx_pd_connector_get_modes(struct drm_connector
*connector
)
50 struct imx_parallel_display
*imxpd
= con_to_imxpd(connector
);
51 struct device_node
*np
= imxpd
->dev
->of_node
;
54 if (imxpd
->panel
&& imxpd
->panel
->funcs
&&
55 imxpd
->panel
->funcs
->get_modes
) {
56 struct drm_display_info
*di
= &connector
->display_info
;
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];
66 drm_mode_connector_update_edid_property(connector
, imxpd
->edid
);
67 num_modes
= drm_add_edid_modes(connector
, imxpd
->edid
);
71 struct drm_display_mode
*mode
= drm_mode_create(connector
->dev
);
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
);
85 static struct drm_encoder
*imx_pd_connector_best_encoder(
86 struct drm_connector
*connector
)
88 struct imx_parallel_display
*imxpd
= con_to_imxpd(connector
);
90 return &imxpd
->encoder
;
93 static void imx_pd_encoder_dpms(struct drm_encoder
*encoder
, int mode
)
95 struct imx_parallel_display
*imxpd
= enc_to_imxpd(encoder
);
97 if (mode
!= DRM_MODE_DPMS_ON
)
98 drm_panel_disable(imxpd
->panel
);
100 drm_panel_enable(imxpd
->panel
);
103 static void imx_pd_encoder_prepare(struct drm_encoder
*encoder
)
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
);
110 static void imx_pd_encoder_commit(struct drm_encoder
*encoder
)
112 struct imx_parallel_display
*imxpd
= enc_to_imxpd(encoder
);
114 drm_panel_prepare(imxpd
->panel
);
115 drm_panel_enable(imxpd
->panel
);
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
)
124 static void imx_pd_encoder_disable(struct drm_encoder
*encoder
)
126 struct imx_parallel_display
*imxpd
= enc_to_imxpd(encoder
);
128 drm_panel_disable(imxpd
->panel
);
129 drm_panel_unprepare(imxpd
->panel
);
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
,
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
,
144 static const struct drm_encoder_funcs imx_pd_encoder_funcs
= {
145 .destroy
= imx_drm_encoder_destroy
,
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
,
156 static int imx_pd_register(struct drm_device
*drm
,
157 struct imx_parallel_display
*imxpd
)
161 ret
= imx_drm_encoder_parse_of(drm
, &imxpd
->encoder
,
162 imxpd
->dev
->of_node
);
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
171 imxpd
->connector
.dpms
= DRM_MODE_DPMS_OFF
;
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
);
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
);
183 drm_panel_attach(imxpd
->panel
, &imxpd
->connector
);
185 drm_mode_connector_attach_encoder(&imxpd
->connector
, &imxpd
->encoder
);
190 static int imx_pd_bind(struct device
*dev
, struct device
*master
, void *data
)
192 struct drm_device
*drm
= data
;
193 struct device_node
*np
= dev
->of_node
;
194 struct device_node
*ep
;
196 struct imx_parallel_display
*imxpd
;
200 imxpd
= devm_kzalloc(dev
, sizeof(*imxpd
), GFP_KERNEL
);
204 edidp
= of_get_property(np
, "edid", &imxpd
->edid_len
);
206 imxpd
->edid
= kmemdup(edidp
, imxpd
->edid_len
, GFP_KERNEL
);
208 ret
= of_property_read_string(np
, "interface-pix-fmt", &fmt
);
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
;
220 /* port@1 is the output port */
221 ep
= of_graph_get_endpoint_by_regs(np
, 1, -1);
223 struct device_node
*remote
;
225 remote
= of_graph_get_remote_port_parent(ep
);
228 imxpd
->panel
= of_drm_find_panel(remote
);
232 return -EPROBE_DEFER
;
237 ret
= imx_pd_register(drm
, imxpd
);
241 dev_set_drvdata(dev
, imxpd
);
246 static void imx_pd_unbind(struct device
*dev
, struct device
*master
,
249 struct imx_parallel_display
*imxpd
= dev_get_drvdata(dev
);
251 imxpd
->encoder
.funcs
->destroy(&imxpd
->encoder
);
252 imxpd
->connector
.funcs
->destroy(&imxpd
->connector
);
257 static const struct component_ops imx_pd_ops
= {
259 .unbind
= imx_pd_unbind
,
262 static int imx_pd_probe(struct platform_device
*pdev
)
264 return component_add(&pdev
->dev
, &imx_pd_ops
);
267 static int imx_pd_remove(struct platform_device
*pdev
)
269 component_del(&pdev
->dev
, &imx_pd_ops
);
274 static const struct of_device_id imx_pd_dt_ids
[] = {
275 { .compatible
= "fsl,imx-parallel-display", },
278 MODULE_DEVICE_TABLE(of
, imx_pd_dt_ids
);
280 static struct platform_driver imx_pd_driver
= {
281 .probe
= imx_pd_probe
,
282 .remove
= imx_pd_remove
,
284 .of_match_table
= imx_pd_dt_ids
,
285 .name
= "imx-parallel-display",
289 module_platform_driver(imx_pd_driver
);
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");