Btrfs: device_list_add() should not update list when mounted
[linux/fpc-iii.git] / drivers / staging / imx-drm / parallel-display.c
blob4ca61afdf6222a3d425e3efdc0728cda42373dcb
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/component.h>
22 #include <linux/module.h>
23 #include <drm/drmP.h>
24 #include <drm/drm_fb_helper.h>
25 #include <drm/drm_crtc_helper.h>
26 #include <drm/drm_panel.h>
27 #include <linux/videodev2.h>
28 #include <video/of_display_timing.h>
30 #include "imx-drm.h"
32 #define con_to_imxpd(x) container_of(x, struct imx_parallel_display, connector)
33 #define enc_to_imxpd(x) container_of(x, struct imx_parallel_display, encoder)
35 struct imx_parallel_display {
36 struct drm_connector connector;
37 struct drm_encoder 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;
44 struct drm_panel *panel;
47 static enum drm_connector_status imx_pd_connector_detect(
48 struct drm_connector *connector, bool force)
50 return connector_status_connected;
53 static int imx_pd_connector_get_modes(struct drm_connector *connector)
55 struct imx_parallel_display *imxpd = con_to_imxpd(connector);
56 struct device_node *np = imxpd->dev->of_node;
57 int num_modes = 0;
59 if (imxpd->panel && imxpd->panel->funcs &&
60 imxpd->panel->funcs->get_modes) {
61 num_modes = imxpd->panel->funcs->get_modes(imxpd->panel);
62 if (num_modes > 0)
63 return num_modes;
66 if (imxpd->edid) {
67 drm_mode_connector_update_edid_property(connector, imxpd->edid);
68 num_modes = drm_add_edid_modes(connector, imxpd->edid);
71 if (imxpd->mode_valid) {
72 struct drm_display_mode *mode = drm_mode_create(connector->dev);
73 if (!mode)
74 return -EINVAL;
75 drm_mode_copy(mode, &imxpd->mode);
76 mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
77 drm_mode_probed_add(connector, mode);
78 num_modes++;
81 if (np) {
82 struct drm_display_mode *mode = drm_mode_create(connector->dev);
83 if (!mode)
84 return -EINVAL;
85 of_get_drm_display_mode(np, &imxpd->mode, OF_USE_NATIVE_MODE);
86 drm_mode_copy(mode, &imxpd->mode);
87 mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
88 drm_mode_probed_add(connector, mode);
89 num_modes++;
92 return num_modes;
95 static struct drm_encoder *imx_pd_connector_best_encoder(
96 struct drm_connector *connector)
98 struct imx_parallel_display *imxpd = con_to_imxpd(connector);
100 return &imxpd->encoder;
103 static void imx_pd_encoder_dpms(struct drm_encoder *encoder, int mode)
105 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
107 if (mode != DRM_MODE_DPMS_ON)
108 drm_panel_disable(imxpd->panel);
109 else
110 drm_panel_enable(imxpd->panel);
113 static bool imx_pd_encoder_mode_fixup(struct drm_encoder *encoder,
114 const struct drm_display_mode *mode,
115 struct drm_display_mode *adjusted_mode)
117 return true;
120 static void imx_pd_encoder_prepare(struct drm_encoder *encoder)
122 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
124 imx_drm_panel_format(encoder, imxpd->interface_pix_fmt);
127 static void imx_pd_encoder_commit(struct drm_encoder *encoder)
131 static void imx_pd_encoder_mode_set(struct drm_encoder *encoder,
132 struct drm_display_mode *mode,
133 struct drm_display_mode *adjusted_mode)
137 static void imx_pd_encoder_disable(struct drm_encoder *encoder)
141 static struct drm_connector_funcs imx_pd_connector_funcs = {
142 .dpms = drm_helper_connector_dpms,
143 .fill_modes = drm_helper_probe_single_connector_modes,
144 .detect = imx_pd_connector_detect,
145 .destroy = imx_drm_connector_destroy,
148 static struct drm_connector_helper_funcs imx_pd_connector_helper_funcs = {
149 .get_modes = imx_pd_connector_get_modes,
150 .best_encoder = imx_pd_connector_best_encoder,
153 static struct drm_encoder_funcs imx_pd_encoder_funcs = {
154 .destroy = imx_drm_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 drm_device *drm,
167 struct imx_parallel_display *imxpd)
169 int ret;
171 ret = imx_drm_encoder_parse_of(drm, &imxpd->encoder,
172 imxpd->dev->of_node);
173 if (ret)
174 return ret;
176 /* set the connector's dpms to OFF so that
177 * drm_helper_connector_dpms() won't return
178 * immediately since the current state is ON
179 * at this point.
181 imxpd->connector.dpms = DRM_MODE_DPMS_OFF;
183 drm_encoder_helper_add(&imxpd->encoder, &imx_pd_encoder_helper_funcs);
184 drm_encoder_init(drm, &imxpd->encoder, &imx_pd_encoder_funcs,
185 DRM_MODE_ENCODER_NONE);
187 drm_connector_helper_add(&imxpd->connector,
188 &imx_pd_connector_helper_funcs);
189 drm_connector_init(drm, &imxpd->connector, &imx_pd_connector_funcs,
190 DRM_MODE_CONNECTOR_VGA);
192 if (imxpd->panel)
193 drm_panel_attach(imxpd->panel, &imxpd->connector);
195 drm_mode_connector_attach_encoder(&imxpd->connector, &imxpd->encoder);
197 imxpd->connector.encoder = &imxpd->encoder;
199 return 0;
202 static int imx_pd_bind(struct device *dev, struct device *master, void *data)
204 struct drm_device *drm = data;
205 struct device_node *np = dev->of_node;
206 struct device_node *panel_node;
207 const u8 *edidp;
208 struct imx_parallel_display *imxpd;
209 int ret;
210 const char *fmt;
212 imxpd = devm_kzalloc(dev, sizeof(*imxpd), GFP_KERNEL);
213 if (!imxpd)
214 return -ENOMEM;
216 edidp = of_get_property(np, "edid", &imxpd->edid_len);
217 if (edidp)
218 imxpd->edid = kmemdup(edidp, imxpd->edid_len, GFP_KERNEL);
220 ret = of_property_read_string(np, "interface-pix-fmt", &fmt);
221 if (!ret) {
222 if (!strcmp(fmt, "rgb24"))
223 imxpd->interface_pix_fmt = V4L2_PIX_FMT_RGB24;
224 else if (!strcmp(fmt, "rgb565"))
225 imxpd->interface_pix_fmt = V4L2_PIX_FMT_RGB565;
226 else if (!strcmp(fmt, "bgr666"))
227 imxpd->interface_pix_fmt = V4L2_PIX_FMT_BGR666;
228 else if (!strcmp(fmt, "lvds666"))
229 imxpd->interface_pix_fmt = v4l2_fourcc('L', 'V', 'D', '6');
232 panel_node = of_parse_phandle(np, "fsl,panel", 0);
233 if (panel_node)
234 imxpd->panel = of_drm_find_panel(panel_node);
236 imxpd->dev = dev;
238 ret = imx_pd_register(drm, imxpd);
239 if (ret)
240 return ret;
242 dev_set_drvdata(dev, imxpd);
244 return 0;
247 static void imx_pd_unbind(struct device *dev, struct device *master,
248 void *data)
250 struct imx_parallel_display *imxpd = dev_get_drvdata(dev);
252 imxpd->encoder.funcs->destroy(&imxpd->encoder);
253 imxpd->connector.funcs->destroy(&imxpd->connector);
256 static const struct component_ops imx_pd_ops = {
257 .bind = imx_pd_bind,
258 .unbind = imx_pd_unbind,
261 static int imx_pd_probe(struct platform_device *pdev)
263 return component_add(&pdev->dev, &imx_pd_ops);
266 static int imx_pd_remove(struct platform_device *pdev)
268 component_del(&pdev->dev, &imx_pd_ops);
269 return 0;
272 static const struct of_device_id imx_pd_dt_ids[] = {
273 { .compatible = "fsl,imx-parallel-display", },
274 { /* sentinel */ }
276 MODULE_DEVICE_TABLE(of, imx_pd_dt_ids);
278 static struct platform_driver imx_pd_driver = {
279 .probe = imx_pd_probe,
280 .remove = imx_pd_remove,
281 .driver = {
282 .of_match_table = imx_pd_dt_ids,
283 .name = "imx-parallel-display",
284 .owner = THIS_MODULE,
288 module_platform_driver(imx_pd_driver);
290 MODULE_DESCRIPTION("i.MX parallel display driver");
291 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
292 MODULE_LICENSE("GPL");
293 MODULE_ALIAS("platform:imx-parallel-display");