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_atomic_helper.h>
20 #include <drm/drm_fb_helper.h>
21 #include <drm/drm_crtc_helper.h>
22 #include <drm/drm_of.h>
23 #include <drm/drm_panel.h>
24 #include <linux/videodev2.h>
25 #include <video/of_display_timing.h>
29 struct imx_parallel_display
{
30 struct drm_connector connector
;
31 struct drm_encoder encoder
;
37 struct drm_display_mode mode
;
38 struct drm_panel
*panel
;
39 struct drm_bridge
*bridge
;
42 static inline struct imx_parallel_display
*con_to_imxpd(struct drm_connector
*c
)
44 return container_of(c
, struct imx_parallel_display
, connector
);
47 static inline struct imx_parallel_display
*enc_to_imxpd(struct drm_encoder
*e
)
49 return container_of(e
, struct imx_parallel_display
, encoder
);
52 static int imx_pd_connector_get_modes(struct drm_connector
*connector
)
54 struct imx_parallel_display
*imxpd
= con_to_imxpd(connector
);
55 struct device_node
*np
= imxpd
->dev
->of_node
;
58 if (imxpd
->panel
&& imxpd
->panel
->funcs
&&
59 imxpd
->panel
->funcs
->get_modes
) {
60 num_modes
= imxpd
->panel
->funcs
->get_modes(imxpd
->panel
);
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
);
77 ret
= of_get_drm_display_mode(np
, &imxpd
->mode
,
83 drm_mode_copy(mode
, &imxpd
->mode
);
84 mode
->type
|= DRM_MODE_TYPE_DRIVER
| DRM_MODE_TYPE_PREFERRED
,
85 drm_mode_probed_add(connector
, mode
);
92 static struct drm_encoder
*imx_pd_connector_best_encoder(
93 struct drm_connector
*connector
)
95 struct imx_parallel_display
*imxpd
= con_to_imxpd(connector
);
97 return &imxpd
->encoder
;
100 static void imx_pd_encoder_enable(struct drm_encoder
*encoder
)
102 struct imx_parallel_display
*imxpd
= enc_to_imxpd(encoder
);
104 drm_panel_prepare(imxpd
->panel
);
105 drm_panel_enable(imxpd
->panel
);
108 static void imx_pd_encoder_disable(struct drm_encoder
*encoder
)
110 struct imx_parallel_display
*imxpd
= enc_to_imxpd(encoder
);
112 drm_panel_disable(imxpd
->panel
);
113 drm_panel_unprepare(imxpd
->panel
);
116 static int imx_pd_encoder_atomic_check(struct drm_encoder
*encoder
,
117 struct drm_crtc_state
*crtc_state
,
118 struct drm_connector_state
*conn_state
)
120 struct imx_crtc_state
*imx_crtc_state
= to_imx_crtc_state(crtc_state
);
121 struct drm_display_info
*di
= &conn_state
->connector
->display_info
;
122 struct imx_parallel_display
*imxpd
= enc_to_imxpd(encoder
);
124 if (!imxpd
->bus_format
&& di
->num_bus_formats
) {
125 imx_crtc_state
->bus_flags
= di
->bus_flags
;
126 imx_crtc_state
->bus_format
= di
->bus_formats
[0];
128 imx_crtc_state
->bus_flags
= imxpd
->bus_flags
;
129 imx_crtc_state
->bus_format
= imxpd
->bus_format
;
131 imx_crtc_state
->di_hsync_pin
= 2;
132 imx_crtc_state
->di_vsync_pin
= 3;
137 static const struct drm_connector_funcs imx_pd_connector_funcs
= {
138 .dpms
= drm_atomic_helper_connector_dpms
,
139 .fill_modes
= drm_helper_probe_single_connector_modes
,
140 .destroy
= imx_drm_connector_destroy
,
141 .reset
= drm_atomic_helper_connector_reset
,
142 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
143 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
146 static const struct drm_connector_helper_funcs imx_pd_connector_helper_funcs
= {
147 .get_modes
= imx_pd_connector_get_modes
,
148 .best_encoder
= imx_pd_connector_best_encoder
,
151 static const struct drm_encoder_funcs imx_pd_encoder_funcs
= {
152 .destroy
= imx_drm_encoder_destroy
,
155 static const struct drm_encoder_helper_funcs imx_pd_encoder_helper_funcs
= {
156 .enable
= imx_pd_encoder_enable
,
157 .disable
= imx_pd_encoder_disable
,
158 .atomic_check
= imx_pd_encoder_atomic_check
,
161 static int imx_pd_register(struct drm_device
*drm
,
162 struct imx_parallel_display
*imxpd
)
164 struct drm_encoder
*encoder
= &imxpd
->encoder
;
167 ret
= imx_drm_encoder_parse_of(drm
, encoder
, imxpd
->dev
->of_node
);
171 /* set the connector's dpms to OFF so that
172 * drm_helper_connector_dpms() won't return
173 * immediately since the current state is ON
176 imxpd
->connector
.dpms
= DRM_MODE_DPMS_OFF
;
178 drm_encoder_helper_add(encoder
, &imx_pd_encoder_helper_funcs
);
179 drm_encoder_init(drm
, encoder
, &imx_pd_encoder_funcs
,
180 DRM_MODE_ENCODER_NONE
, NULL
);
182 if (!imxpd
->bridge
) {
183 drm_connector_helper_add(&imxpd
->connector
,
184 &imx_pd_connector_helper_funcs
);
185 drm_connector_init(drm
, &imxpd
->connector
,
186 &imx_pd_connector_funcs
,
187 DRM_MODE_CONNECTOR_VGA
);
191 drm_panel_attach(imxpd
->panel
, &imxpd
->connector
);
194 ret
= drm_bridge_attach(encoder
, imxpd
->bridge
, NULL
);
196 dev_err(imxpd
->dev
, "failed to attach bridge: %d\n",
201 drm_mode_connector_attach_encoder(&imxpd
->connector
, encoder
);
207 static int imx_pd_bind(struct device
*dev
, struct device
*master
, void *data
)
209 struct drm_device
*drm
= data
;
210 struct device_node
*np
= dev
->of_node
;
212 struct imx_parallel_display
*imxpd
;
217 imxpd
= devm_kzalloc(dev
, sizeof(*imxpd
), GFP_KERNEL
);
221 edidp
= of_get_property(np
, "edid", &imxpd
->edid_len
);
223 imxpd
->edid
= kmemdup(edidp
, imxpd
->edid_len
, GFP_KERNEL
);
225 ret
= of_property_read_string(np
, "interface-pix-fmt", &fmt
);
227 if (!strcmp(fmt
, "rgb24"))
228 bus_format
= MEDIA_BUS_FMT_RGB888_1X24
;
229 else if (!strcmp(fmt
, "rgb565"))
230 bus_format
= MEDIA_BUS_FMT_RGB565_1X16
;
231 else if (!strcmp(fmt
, "bgr666"))
232 bus_format
= MEDIA_BUS_FMT_RGB666_1X18
;
233 else if (!strcmp(fmt
, "lvds666"))
234 bus_format
= MEDIA_BUS_FMT_RGB666_1X24_CPADHI
;
236 imxpd
->bus_format
= bus_format
;
238 /* port@1 is the output port */
239 ret
= drm_of_find_panel_or_bridge(np
, 1, 0, &imxpd
->panel
, &imxpd
->bridge
);
240 if (ret
&& ret
!= -ENODEV
)
245 ret
= imx_pd_register(drm
, imxpd
);
249 dev_set_drvdata(dev
, imxpd
);
254 static void imx_pd_unbind(struct device
*dev
, struct device
*master
,
257 struct imx_parallel_display
*imxpd
= dev_get_drvdata(dev
);
260 drm_panel_detach(imxpd
->panel
);
265 static const struct component_ops imx_pd_ops
= {
267 .unbind
= imx_pd_unbind
,
270 static int imx_pd_probe(struct platform_device
*pdev
)
272 return component_add(&pdev
->dev
, &imx_pd_ops
);
275 static int imx_pd_remove(struct platform_device
*pdev
)
277 component_del(&pdev
->dev
, &imx_pd_ops
);
282 static const struct of_device_id imx_pd_dt_ids
[] = {
283 { .compatible
= "fsl,imx-parallel-display", },
286 MODULE_DEVICE_TABLE(of
, imx_pd_dt_ids
);
288 static struct platform_driver imx_pd_driver
= {
289 .probe
= imx_pd_probe
,
290 .remove
= imx_pd_remove
,
292 .of_match_table
= imx_pd_dt_ids
,
293 .name
= "imx-parallel-display",
297 module_platform_driver(imx_pd_driver
);
299 MODULE_DESCRIPTION("i.MX parallel display driver");
300 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
301 MODULE_LICENSE("GPL");
302 MODULE_ALIAS("platform:imx-parallel-display");