Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux/fpc-iii.git] / drivers / staging / imx-drm / parallel-display.c
blob351d61dede0024003f39c7ace312e552081c79ad
1 /*
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 * MA 02110-1301, USA.
21 #include <linux/module.h>
22 #include <drm/drmP.h>
23 #include <drm/drm_fb_helper.h>
24 #include <drm/drm_crtc_helper.h>
25 #include <linux/videodev2.h>
26 #include <video/of_display_timing.h>
28 #include "imx-drm.h"
30 #define con_to_imxpd(x) container_of(x, struct imx_parallel_display, connector)
31 #define enc_to_imxpd(x) container_of(x, struct imx_parallel_display, encoder)
33 struct imx_parallel_display {
34 struct drm_connector connector;
35 struct imx_drm_connector *imx_drm_connector;
36 struct drm_encoder encoder;
37 struct imx_drm_encoder *imx_drm_encoder;
38 struct device *dev;
39 void *edid;
40 int edid_len;
41 u32 interface_pix_fmt;
42 int mode_valid;
43 struct drm_display_mode mode;
46 static enum drm_connector_status imx_pd_connector_detect(
47 struct drm_connector *connector, bool force)
49 return connector_status_connected;
52 static void imx_pd_connector_destroy(struct drm_connector *connector)
54 /* do not free here */
57 static int imx_pd_connector_get_modes(struct drm_connector *connector)
59 struct imx_parallel_display *imxpd = con_to_imxpd(connector);
60 struct device_node *np = imxpd->dev->of_node;
61 int num_modes = 0;
63 if (imxpd->edid) {
64 drm_mode_connector_update_edid_property(connector, imxpd->edid);
65 num_modes = drm_add_edid_modes(connector, imxpd->edid);
68 if (imxpd->mode_valid) {
69 struct drm_display_mode *mode = drm_mode_create(connector->dev);
70 drm_mode_copy(mode, &imxpd->mode);
71 mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
72 drm_mode_probed_add(connector, mode);
73 num_modes++;
76 if (np) {
77 struct drm_display_mode *mode = drm_mode_create(connector->dev);
78 of_get_drm_display_mode(np, &imxpd->mode, OF_USE_NATIVE_MODE);
79 drm_mode_copy(mode, &imxpd->mode);
80 mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
81 drm_mode_probed_add(connector, mode);
82 num_modes++;
85 return num_modes;
88 static int imx_pd_connector_mode_valid(struct drm_connector *connector,
89 struct drm_display_mode *mode)
91 return 0;
94 static struct drm_encoder *imx_pd_connector_best_encoder(
95 struct drm_connector *connector)
97 struct imx_parallel_display *imxpd = con_to_imxpd(connector);
99 return &imxpd->encoder;
102 static void imx_pd_encoder_dpms(struct drm_encoder *encoder, int mode)
106 static bool imx_pd_encoder_mode_fixup(struct drm_encoder *encoder,
107 const struct drm_display_mode *mode,
108 struct drm_display_mode *adjusted_mode)
110 return true;
113 static void imx_pd_encoder_prepare(struct drm_encoder *encoder)
115 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
117 imx_drm_crtc_panel_format(encoder->crtc, DRM_MODE_ENCODER_NONE,
118 imxpd->interface_pix_fmt);
121 static void imx_pd_encoder_commit(struct drm_encoder *encoder)
125 static void imx_pd_encoder_mode_set(struct drm_encoder *encoder,
126 struct drm_display_mode *mode,
127 struct drm_display_mode *adjusted_mode)
131 static void imx_pd_encoder_disable(struct drm_encoder *encoder)
135 static void imx_pd_encoder_destroy(struct drm_encoder *encoder)
137 /* do not free here */
140 static struct drm_connector_funcs imx_pd_connector_funcs = {
141 .dpms = drm_helper_connector_dpms,
142 .fill_modes = drm_helper_probe_single_connector_modes,
143 .detect = imx_pd_connector_detect,
144 .destroy = imx_pd_connector_destroy,
147 static struct drm_connector_helper_funcs imx_pd_connector_helper_funcs = {
148 .get_modes = imx_pd_connector_get_modes,
149 .best_encoder = imx_pd_connector_best_encoder,
150 .mode_valid = imx_pd_connector_mode_valid,
153 static struct drm_encoder_funcs imx_pd_encoder_funcs = {
154 .destroy = imx_pd_encoder_destroy,
157 static struct drm_encoder_helper_funcs imx_pd_encoder_helper_funcs = {
158 .dpms = imx_pd_encoder_dpms,
159 .mode_fixup = imx_pd_encoder_mode_fixup,
160 .prepare = imx_pd_encoder_prepare,
161 .commit = imx_pd_encoder_commit,
162 .mode_set = imx_pd_encoder_mode_set,
163 .disable = imx_pd_encoder_disable,
166 static int imx_pd_register(struct imx_parallel_display *imxpd)
168 int ret;
170 drm_mode_connector_attach_encoder(&imxpd->connector, &imxpd->encoder);
172 imxpd->connector.funcs = &imx_pd_connector_funcs;
173 imxpd->encoder.funcs = &imx_pd_encoder_funcs;
175 imxpd->encoder.encoder_type = DRM_MODE_ENCODER_NONE;
176 imxpd->connector.connector_type = DRM_MODE_CONNECTOR_VGA;
178 drm_encoder_helper_add(&imxpd->encoder, &imx_pd_encoder_helper_funcs);
179 ret = imx_drm_add_encoder(&imxpd->encoder, &imxpd->imx_drm_encoder,
180 THIS_MODULE);
181 if (ret) {
182 dev_err(imxpd->dev, "adding encoder failed with %d\n", ret);
183 return ret;
186 drm_connector_helper_add(&imxpd->connector,
187 &imx_pd_connector_helper_funcs);
189 ret = imx_drm_add_connector(&imxpd->connector,
190 &imxpd->imx_drm_connector, THIS_MODULE);
191 if (ret) {
192 imx_drm_remove_encoder(imxpd->imx_drm_encoder);
193 dev_err(imxpd->dev, "adding connector failed with %d\n", ret);
194 return ret;
197 imxpd->connector.encoder = &imxpd->encoder;
199 return 0;
202 static int imx_pd_probe(struct platform_device *pdev)
204 struct device_node *np = pdev->dev.of_node;
205 const u8 *edidp;
206 struct imx_parallel_display *imxpd;
207 int ret;
208 const char *fmt;
210 imxpd = devm_kzalloc(&pdev->dev, sizeof(*imxpd), GFP_KERNEL);
211 if (!imxpd)
212 return -ENOMEM;
214 edidp = of_get_property(np, "edid", &imxpd->edid_len);
215 if (edidp)
216 imxpd->edid = kmemdup(edidp, imxpd->edid_len, GFP_KERNEL);
218 ret = of_property_read_string(np, "interface-pix-fmt", &fmt);
219 if (!ret) {
220 if (!strcmp(fmt, "rgb24"))
221 imxpd->interface_pix_fmt = V4L2_PIX_FMT_RGB24;
222 else if (!strcmp(fmt, "rgb565"))
223 imxpd->interface_pix_fmt = V4L2_PIX_FMT_RGB565;
224 else if (!strcmp(fmt, "bgr666"))
225 imxpd->interface_pix_fmt = V4L2_PIX_FMT_BGR666;
228 imxpd->dev = &pdev->dev;
230 ret = imx_pd_register(imxpd);
231 if (ret)
232 return ret;
234 ret = imx_drm_encoder_add_possible_crtcs(imxpd->imx_drm_encoder, np);
236 platform_set_drvdata(pdev, imxpd);
238 return 0;
241 static int imx_pd_remove(struct platform_device *pdev)
243 struct imx_parallel_display *imxpd = platform_get_drvdata(pdev);
244 struct drm_connector *connector = &imxpd->connector;
245 struct drm_encoder *encoder = &imxpd->encoder;
247 drm_mode_connector_detach_encoder(connector, encoder);
249 imx_drm_remove_connector(imxpd->imx_drm_connector);
250 imx_drm_remove_encoder(imxpd->imx_drm_encoder);
252 return 0;
255 static const struct of_device_id imx_pd_dt_ids[] = {
256 { .compatible = "fsl,imx-parallel-display", },
257 { /* sentinel */ }
259 MODULE_DEVICE_TABLE(of, imx_pd_dt_ids);
261 static struct platform_driver imx_pd_driver = {
262 .probe = imx_pd_probe,
263 .remove = imx_pd_remove,
264 .driver = {
265 .of_match_table = imx_pd_dt_ids,
266 .name = "imx-parallel-display",
267 .owner = THIS_MODULE,
271 module_platform_driver(imx_pd_driver);
273 MODULE_DESCRIPTION("i.MX parallel display driver");
274 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
275 MODULE_LICENSE("GPL");
276 MODULE_ALIAS("platform:imx-parallel-display");