blob: 2d1fd02cd3d663c672fffd8c5d4b6eb60e63e195 [file] [log] [blame]
Sascha Hauer19022aa2012-09-21 10:07:48 +02001/*
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.
Sascha Hauer19022aa2012-09-21 10:07:48 +020014 */
15
Russell King17b50012013-11-03 11:23:34 +000016#include <linux/component.h>
Sascha Hauer19022aa2012-09-21 10:07:48 +020017#include <linux/module.h>
18#include <drm/drmP.h>
19#include <drm/drm_fb_helper.h>
20#include <drm/drm_crtc_helper.h>
Philipp Zabel628f435b2014-02-25 11:55:04 +010021#include <drm/drm_panel.h>
Sascha Hauer19022aa2012-09-21 10:07:48 +020022#include <linux/videodev2.h>
Lothar Waßmann075c4572014-01-13 11:24:33 +010023#include <video/of_display_timing.h>
Philipp Zabel553a59f2015-05-19 16:28:12 +020024#include <linux/of_graph.h>
Sascha Hauer19022aa2012-09-21 10:07:48 +020025
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
31struct imx_parallel_display {
32 struct drm_connector connector;
Sascha Hauer19022aa2012-09-21 10:07:48 +020033 struct drm_encoder encoder;
Sascha Hauer19022aa2012-09-21 10:07:48 +020034 struct device *dev;
35 void *edid;
36 int edid_len;
Philipp Zabel2872c802015-02-02 17:25:59 +010037 u32 bus_format;
Sascha Hauer19022aa2012-09-21 10:07:48 +020038 struct drm_display_mode mode;
Philipp Zabel628f435b2014-02-25 11:55:04 +010039 struct drm_panel *panel;
Sascha Hauer19022aa2012-09-21 10:07:48 +020040};
41
42static enum drm_connector_status imx_pd_connector_detect(
43 struct drm_connector *connector, bool force)
44{
45 return connector_status_connected;
46}
47
Sascha Hauer19022aa2012-09-21 10:07:48 +020048static int imx_pd_connector_get_modes(struct drm_connector *connector)
49{
50 struct imx_parallel_display *imxpd = con_to_imxpd(connector);
Marek Vasutafb12ed2013-04-03 12:36:28 -030051 struct device_node *np = imxpd->dev->of_node;
Sascha Hauer19022aa2012-09-21 10:07:48 +020052 int num_modes = 0;
53
Philipp Zabel628f435b2014-02-25 11:55:04 +010054 if (imxpd->panel && imxpd->panel->funcs &&
55 imxpd->panel->funcs->get_modes) {
Philipp Zabel9832e812015-11-13 18:10:04 +010056 struct drm_display_info *di = &connector->display_info;
57
Philipp Zabel628f435b2014-02-25 11:55:04 +010058 num_modes = imxpd->panel->funcs->get_modes(imxpd->panel);
Philipp Zabel9832e812015-11-13 18:10:04 +010059 if (!imxpd->bus_format && di->num_bus_formats)
60 imxpd->bus_format = di->bus_formats[0];
Philipp Zabel628f435b2014-02-25 11:55:04 +010061 if (num_modes > 0)
62 return num_modes;
63 }
64
Sascha Hauer19022aa2012-09-21 10:07:48 +020065 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
Marek Vasutafb12ed2013-04-03 12:36:28 -030070 if (np) {
71 struct drm_display_mode *mode = drm_mode_create(connector->dev);
Quentin Lambert63bc5162014-08-04 21:07:07 +020072
Fabio Estevam39dcf4f2014-02-26 20:53:42 -030073 if (!mode)
74 return -EINVAL;
Lothar Waßmann075c4572014-01-13 11:24:33 +010075 of_get_drm_display_mode(np, &imxpd->mode, OF_USE_NATIVE_MODE);
Marek Vasutafb12ed2013-04-03 12:36:28 -030076 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
Sascha Hauer19022aa2012-09-21 10:07:48 +020082 return num_modes;
83}
84
Sascha Hauer19022aa2012-09-21 10:07:48 +020085static 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
93static void imx_pd_encoder_dpms(struct drm_encoder *encoder, int mode)
94{
Philipp Zabel628f435b2014-02-25 11:55:04 +010095 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);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200101}
102
Sascha Hauer19022aa2012-09-21 10:07:48 +0200103static void imx_pd_encoder_prepare(struct drm_encoder *encoder)
104{
105 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
Philipp Zabel4ed094f2016-05-09 17:02:13 +0200106 imx_drm_set_bus_config(encoder, imxpd->bus_format, 2, 3,
107 imxpd->connector.display_info.bus_flags);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200108}
109
110static void imx_pd_encoder_commit(struct drm_encoder *encoder)
111{
Philipp Zabelad92c8b2014-11-03 15:54:47 +0100112 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
113
114 drm_panel_prepare(imxpd->panel);
115 drm_panel_enable(imxpd->panel);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200116}
117
118static void imx_pd_encoder_mode_set(struct drm_encoder *encoder,
Steve Longerbeameb10d632014-12-18 18:00:24 -0800119 struct drm_display_mode *orig_mode,
120 struct drm_display_mode *mode)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200121{
122}
123
124static void imx_pd_encoder_disable(struct drm_encoder *encoder)
125{
Philipp Zabelad92c8b2014-11-03 15:54:47 +0100126 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
127
128 drm_panel_disable(imxpd->panel);
129 drm_panel_unprepare(imxpd->panel);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200130}
131
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100132static const struct drm_connector_funcs imx_pd_connector_funcs = {
Sascha Hauer19022aa2012-09-21 10:07:48 +0200133 .dpms = drm_helper_connector_dpms,
134 .fill_modes = drm_helper_probe_single_connector_modes,
135 .detect = imx_pd_connector_detect,
Russell King1b3f7672013-11-03 13:30:48 +0000136 .destroy = imx_drm_connector_destroy,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200137};
138
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100139static const struct drm_connector_helper_funcs imx_pd_connector_helper_funcs = {
Sascha Hauer19022aa2012-09-21 10:07:48 +0200140 .get_modes = imx_pd_connector_get_modes,
141 .best_encoder = imx_pd_connector_best_encoder,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200142};
143
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100144static const struct drm_encoder_funcs imx_pd_encoder_funcs = {
Russell King1b3f7672013-11-03 13:30:48 +0000145 .destroy = imx_drm_encoder_destroy,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200146};
147
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100148static const struct drm_encoder_helper_funcs imx_pd_encoder_helper_funcs = {
Sascha Hauer19022aa2012-09-21 10:07:48 +0200149 .dpms = imx_pd_encoder_dpms,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200150 .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
Russell King1b3f7672013-11-03 13:30:48 +0000156static int imx_pd_register(struct drm_device *drm,
157 struct imx_parallel_display *imxpd)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200158{
159 int ret;
160
Russell King1b3f7672013-11-03 13:30:48 +0000161 ret = imx_drm_encoder_parse_of(drm, &imxpd->encoder,
162 imxpd->dev->of_node);
163 if (ret)
164 return ret;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200165
Denis Cariklic026a3f2014-06-17 15:34:51 +0200166 /* 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
Sascha Hauer19022aa2012-09-21 10:07:48 +0200173 drm_encoder_helper_add(&imxpd->encoder, &imx_pd_encoder_helper_funcs);
Russell King1b3f7672013-11-03 13:30:48 +0000174 drm_encoder_init(drm, &imxpd->encoder, &imx_pd_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200175 DRM_MODE_ENCODER_NONE, NULL);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200176
177 drm_connector_helper_add(&imxpd->connector,
178 &imx_pd_connector_helper_funcs);
Russell King1b3f7672013-11-03 13:30:48 +0000179 drm_connector_init(drm, &imxpd->connector, &imx_pd_connector_funcs,
180 DRM_MODE_CONNECTOR_VGA);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200181
Philipp Zabel628f435b2014-02-25 11:55:04 +0100182 if (imxpd->panel)
183 drm_panel_attach(imxpd->panel, &imxpd->connector);
184
Russell King1b3f7672013-11-03 13:30:48 +0000185 drm_mode_connector_attach_encoder(&imxpd->connector, &imxpd->encoder);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200186
Sascha Hauer19022aa2012-09-21 10:07:48 +0200187 return 0;
188}
189
Russell King17b50012013-11-03 11:23:34 +0000190static int imx_pd_bind(struct device *dev, struct device *master, void *data)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200191{
Russell King1b3f7672013-11-03 13:30:48 +0000192 struct drm_device *drm = data;
Russell King17b50012013-11-03 11:23:34 +0000193 struct device_node *np = dev->of_node;
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200194 struct device_node *ep;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200195 const u8 *edidp;
196 struct imx_parallel_display *imxpd;
197 int ret;
198 const char *fmt;
199
Russell King17b50012013-11-03 11:23:34 +0000200 imxpd = devm_kzalloc(dev, sizeof(*imxpd), GFP_KERNEL);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200201 if (!imxpd)
202 return -ENOMEM;
203
Sascha Hauer19022aa2012-09-21 10:07:48 +0200204 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"))
Philipp Zabel2872c802015-02-02 17:25:59 +0100211 imxpd->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200212 else if (!strcmp(fmt, "rgb565"))
Philipp Zabel2872c802015-02-02 17:25:59 +0100213 imxpd->bus_format = MEDIA_BUS_FMT_RGB565_1X16;
Marek Vasut7d0a66c2013-04-22 23:19:31 +0200214 else if (!strcmp(fmt, "bgr666"))
Philipp Zabel2872c802015-02-02 17:25:59 +0100215 imxpd->bus_format = MEDIA_BUS_FMT_RGB666_1X18;
Emil Renner Berthing9e74d292014-03-29 13:57:44 +0100216 else if (!strcmp(fmt, "lvds666"))
Philipp Zabel2872c802015-02-02 17:25:59 +0100217 imxpd->bus_format = MEDIA_BUS_FMT_RGB666_1X24_CPADHI;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200218 }
219
Philipp Zabel553a59f2015-05-19 16:28:12 +0200220 /* port@1 is the output port */
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200221 ep = of_graph_get_endpoint_by_regs(np, 1, -1);
222 if (ep) {
223 struct device_node *remote;
Philipp Zabel553a59f2015-05-19 16:28:12 +0200224
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200225 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);
Philipp Zabel553a59f2015-05-19 16:28:12 +0200230 }
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200231 if (!imxpd->panel)
232 return -EPROBE_DEFER;
Liu Yingd70e96a2015-02-23 11:09:51 +0800233 }
Philipp Zabel628f435b2014-02-25 11:55:04 +0100234
Russell King17b50012013-11-03 11:23:34 +0000235 imxpd->dev = dev;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200236
Russell King1b3f7672013-11-03 13:30:48 +0000237 ret = imx_pd_register(drm, imxpd);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200238 if (ret)
239 return ret;
240
Russell King17b50012013-11-03 11:23:34 +0000241 dev_set_drvdata(dev, imxpd);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200242
243 return 0;
244}
245
Russell King17b50012013-11-03 11:23:34 +0000246static void imx_pd_unbind(struct device *dev, struct device *master,
247 void *data)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200248{
Russell King17b50012013-11-03 11:23:34 +0000249 struct imx_parallel_display *imxpd = dev_get_drvdata(dev);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200250
Russell King1b3f7672013-11-03 13:30:48 +0000251 imxpd->encoder.funcs->destroy(&imxpd->encoder);
252 imxpd->connector.funcs->destroy(&imxpd->connector);
Peter Seidererdcbc9eb2013-12-08 22:03:58 +0100253
254 kfree(imxpd->edid);
Russell King17b50012013-11-03 11:23:34 +0000255}
Sascha Hauer19022aa2012-09-21 10:07:48 +0200256
Russell King17b50012013-11-03 11:23:34 +0000257static const struct component_ops imx_pd_ops = {
258 .bind = imx_pd_bind,
259 .unbind = imx_pd_unbind,
260};
261
262static int imx_pd_probe(struct platform_device *pdev)
263{
264 return component_add(&pdev->dev, &imx_pd_ops);
265}
266
267static int imx_pd_remove(struct platform_device *pdev)
268{
269 component_del(&pdev->dev, &imx_pd_ops);
Peter Seidererdcbc9eb2013-12-08 22:03:58 +0100270
Sascha Hauer19022aa2012-09-21 10:07:48 +0200271 return 0;
272}
273
274static const struct of_device_id imx_pd_dt_ids[] = {
275 { .compatible = "fsl,imx-parallel-display", },
276 { /* sentinel */ }
277};
Jiada Wangfa45f2c2013-06-07 18:36:46 +0900278MODULE_DEVICE_TABLE(of, imx_pd_dt_ids);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200279
280static struct platform_driver imx_pd_driver = {
281 .probe = imx_pd_probe,
Bill Pemberton99c28f12012-11-19 13:20:51 -0500282 .remove = imx_pd_remove,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200283 .driver = {
284 .of_match_table = imx_pd_dt_ids,
285 .name = "imx-parallel-display",
Sascha Hauer19022aa2012-09-21 10:07:48 +0200286 },
287};
288
289module_platform_driver(imx_pd_driver);
290
291MODULE_DESCRIPTION("i.MX parallel display driver");
292MODULE_AUTHOR("Sascha Hauer, Pengutronix");
293MODULE_LICENSE("GPL");
Fabio Estevamb2da05f2013-08-18 21:40:05 -0300294MODULE_ALIAS("platform:imx-parallel-display");