blob: bb5dbd66fea714b41a2f0a74dcb709284e47df85 [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>
Liu Ying255c35f2016-07-08 17:40:56 +080019#include <drm/drm_atomic_helper.h>
Sascha Hauer19022aa2012-09-21 10:07:48 +020020#include <drm/drm_fb_helper.h>
21#include <drm/drm_crtc_helper.h>
Philipp Zabel628f435b2014-02-25 11:55:04 +010022#include <drm/drm_panel.h>
Sascha Hauer19022aa2012-09-21 10:07:48 +020023#include <linux/videodev2.h>
Lothar Waßmann075c4572014-01-13 11:24:33 +010024#include <video/of_display_timing.h>
Philipp Zabel553a59f2015-05-19 16:28:12 +020025#include <linux/of_graph.h>
Sascha Hauer19022aa2012-09-21 10:07:48 +020026
27#include "imx-drm.h"
28
29#define con_to_imxpd(x) container_of(x, struct imx_parallel_display, connector)
Liu Ying032003c2016-07-08 17:40:58 +080030#define imx_enc_to_imxpd(x) \
31 container_of(x, struct imx_parallel_display, imx_encoder)
Sascha Hauer19022aa2012-09-21 10:07:48 +020032
33struct imx_parallel_display {
34 struct drm_connector connector;
Liu Ying032003c2016-07-08 17:40:58 +080035 struct imx_drm_encoder imx_encoder;
Sascha Hauer19022aa2012-09-21 10:07:48 +020036 struct device *dev;
37 void *edid;
38 int edid_len;
Sascha Hauer19022aa2012-09-21 10:07:48 +020039 struct drm_display_mode mode;
Philipp Zabel628f435b2014-02-25 11:55:04 +010040 struct drm_panel *panel;
Sascha Hauer19022aa2012-09-21 10:07:48 +020041};
42
43static enum drm_connector_status imx_pd_connector_detect(
44 struct drm_connector *connector, bool force)
45{
46 return connector_status_connected;
47}
48
Sascha Hauer19022aa2012-09-21 10:07:48 +020049static int imx_pd_connector_get_modes(struct drm_connector *connector)
50{
51 struct imx_parallel_display *imxpd = con_to_imxpd(connector);
Marek Vasutafb12ed2013-04-03 12:36:28 -030052 struct device_node *np = imxpd->dev->of_node;
Sascha Hauer19022aa2012-09-21 10:07:48 +020053 int num_modes = 0;
54
Philipp Zabel628f435b2014-02-25 11:55:04 +010055 if (imxpd->panel && imxpd->panel->funcs &&
56 imxpd->panel->funcs->get_modes) {
Philipp Zabel9832e812015-11-13 18:10:04 +010057 struct drm_display_info *di = &connector->display_info;
58
Philipp Zabel628f435b2014-02-25 11:55:04 +010059 num_modes = imxpd->panel->funcs->get_modes(imxpd->panel);
Liu Ying032003c2016-07-08 17:40:58 +080060 if (!imxpd->imx_encoder.bus_format && di->num_bus_formats)
61 imxpd->imx_encoder.bus_format = di->bus_formats[0];
62 imxpd->imx_encoder.bus_flags = di->bus_flags;
Philipp Zabel628f435b2014-02-25 11:55:04 +010063 if (num_modes > 0)
64 return num_modes;
65 }
66
Sascha Hauer19022aa2012-09-21 10:07:48 +020067 if (imxpd->edid) {
68 drm_mode_connector_update_edid_property(connector, imxpd->edid);
69 num_modes = drm_add_edid_modes(connector, imxpd->edid);
70 }
71
Marek Vasutafb12ed2013-04-03 12:36:28 -030072 if (np) {
73 struct drm_display_mode *mode = drm_mode_create(connector->dev);
Quentin Lambert63bc51642014-08-04 21:07:07 +020074
Fabio Estevam39dcf4f2014-02-26 20:53:42 -030075 if (!mode)
76 return -EINVAL;
Lothar Waßmann075c4572014-01-13 11:24:33 +010077 of_get_drm_display_mode(np, &imxpd->mode, OF_USE_NATIVE_MODE);
Marek Vasutafb12ed2013-04-03 12:36:28 -030078 drm_mode_copy(mode, &imxpd->mode);
79 mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
80 drm_mode_probed_add(connector, mode);
81 num_modes++;
82 }
83
Sascha Hauer19022aa2012-09-21 10:07:48 +020084 return num_modes;
85}
86
Sascha Hauer19022aa2012-09-21 10:07:48 +020087static struct drm_encoder *imx_pd_connector_best_encoder(
88 struct drm_connector *connector)
89{
90 struct imx_parallel_display *imxpd = con_to_imxpd(connector);
91
Liu Ying032003c2016-07-08 17:40:58 +080092 return &imxpd->imx_encoder.encoder;
Sascha Hauer19022aa2012-09-21 10:07:48 +020093}
94
Liu Yingf6e396e2016-07-08 17:41:01 +080095static void imx_pd_encoder_enable(struct drm_encoder *encoder)
Sascha Hauer19022aa2012-09-21 10:07:48 +020096{
Liu Ying032003c2016-07-08 17:40:58 +080097 struct imx_drm_encoder *imx_encoder = enc_to_imx_enc(encoder);
98 struct imx_parallel_display *imxpd = imx_enc_to_imxpd(imx_encoder);
Philipp Zabelad92c8b2014-11-03 15:54:47 +010099
100 drm_panel_prepare(imxpd->panel);
101 drm_panel_enable(imxpd->panel);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200102}
103
104static void imx_pd_encoder_mode_set(struct drm_encoder *encoder,
Steve Longerbeameb10d632014-12-18 18:00:24 -0800105 struct drm_display_mode *orig_mode,
106 struct drm_display_mode *mode)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200107{
108}
109
110static void imx_pd_encoder_disable(struct drm_encoder *encoder)
111{
Liu Ying032003c2016-07-08 17:40:58 +0800112 struct imx_drm_encoder *imx_encoder = enc_to_imx_enc(encoder);
113 struct imx_parallel_display *imxpd = imx_enc_to_imxpd(imx_encoder);
Philipp Zabelad92c8b2014-11-03 15:54:47 +0100114
115 drm_panel_disable(imxpd->panel);
116 drm_panel_unprepare(imxpd->panel);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200117}
118
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100119static const struct drm_connector_funcs imx_pd_connector_funcs = {
Liu Yingf6e396e2016-07-08 17:41:01 +0800120 .dpms = drm_atomic_helper_connector_dpms,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200121 .fill_modes = drm_helper_probe_single_connector_modes,
122 .detect = imx_pd_connector_detect,
Russell King1b3f7672013-11-03 13:30:48 +0000123 .destroy = imx_drm_connector_destroy,
Liu Ying255c35f2016-07-08 17:40:56 +0800124 .reset = drm_atomic_helper_connector_reset,
125 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
126 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200127};
128
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100129static const struct drm_connector_helper_funcs imx_pd_connector_helper_funcs = {
Sascha Hauer19022aa2012-09-21 10:07:48 +0200130 .get_modes = imx_pd_connector_get_modes,
131 .best_encoder = imx_pd_connector_best_encoder,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200132};
133
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100134static const struct drm_encoder_funcs imx_pd_encoder_funcs = {
Russell King1b3f7672013-11-03 13:30:48 +0000135 .destroy = imx_drm_encoder_destroy,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200136};
137
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100138static const struct drm_encoder_helper_funcs imx_pd_encoder_helper_funcs = {
Sascha Hauer19022aa2012-09-21 10:07:48 +0200139 .mode_set = imx_pd_encoder_mode_set,
Liu Yingf6e396e2016-07-08 17:41:01 +0800140 .enable = imx_pd_encoder_enable,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200141 .disable = imx_pd_encoder_disable,
142};
143
Russell King1b3f7672013-11-03 13:30:48 +0000144static int imx_pd_register(struct drm_device *drm,
145 struct imx_parallel_display *imxpd)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200146{
147 int ret;
148
Liu Ying032003c2016-07-08 17:40:58 +0800149 ret = imx_drm_encoder_parse_of(drm, &imxpd->imx_encoder.encoder,
Russell King1b3f7672013-11-03 13:30:48 +0000150 imxpd->dev->of_node);
151 if (ret)
152 return ret;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200153
Denis Cariklic026a3f2014-06-17 15:34:51 +0200154 /* set the connector's dpms to OFF so that
155 * drm_helper_connector_dpms() won't return
156 * immediately since the current state is ON
157 * at this point.
158 */
159 imxpd->connector.dpms = DRM_MODE_DPMS_OFF;
160
Liu Ying032003c2016-07-08 17:40:58 +0800161 drm_encoder_helper_add(&imxpd->imx_encoder.encoder,
162 &imx_pd_encoder_helper_funcs);
163 drm_encoder_init(drm, &imxpd->imx_encoder.encoder,
164 &imx_pd_encoder_funcs, DRM_MODE_ENCODER_NONE, NULL);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200165
166 drm_connector_helper_add(&imxpd->connector,
167 &imx_pd_connector_helper_funcs);
Russell King1b3f7672013-11-03 13:30:48 +0000168 drm_connector_init(drm, &imxpd->connector, &imx_pd_connector_funcs,
169 DRM_MODE_CONNECTOR_VGA);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200170
Philipp Zabel628f435b2014-02-25 11:55:04 +0100171 if (imxpd->panel)
172 drm_panel_attach(imxpd->panel, &imxpd->connector);
173
Liu Ying032003c2016-07-08 17:40:58 +0800174 drm_mode_connector_attach_encoder(&imxpd->connector,
175 &imxpd->imx_encoder.encoder);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200176
Sascha Hauer19022aa2012-09-21 10:07:48 +0200177 return 0;
178}
179
Russell King17b50012013-11-03 11:23:34 +0000180static int imx_pd_bind(struct device *dev, struct device *master, void *data)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200181{
Russell King1b3f7672013-11-03 13:30:48 +0000182 struct drm_device *drm = data;
Russell King17b50012013-11-03 11:23:34 +0000183 struct device_node *np = dev->of_node;
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200184 struct device_node *ep;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200185 const u8 *edidp;
186 struct imx_parallel_display *imxpd;
187 int ret;
Liu Ying032003c2016-07-08 17:40:58 +0800188 u32 bus_format = 0;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200189 const char *fmt;
190
Russell King17b50012013-11-03 11:23:34 +0000191 imxpd = devm_kzalloc(dev, sizeof(*imxpd), GFP_KERNEL);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200192 if (!imxpd)
193 return -ENOMEM;
194
Sascha Hauer19022aa2012-09-21 10:07:48 +0200195 edidp = of_get_property(np, "edid", &imxpd->edid_len);
196 if (edidp)
197 imxpd->edid = kmemdup(edidp, imxpd->edid_len, GFP_KERNEL);
198
199 ret = of_property_read_string(np, "interface-pix-fmt", &fmt);
200 if (!ret) {
201 if (!strcmp(fmt, "rgb24"))
Liu Ying032003c2016-07-08 17:40:58 +0800202 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200203 else if (!strcmp(fmt, "rgb565"))
Liu Ying032003c2016-07-08 17:40:58 +0800204 bus_format = MEDIA_BUS_FMT_RGB565_1X16;
Marek Vasut7d0a66c2013-04-22 23:19:31 +0200205 else if (!strcmp(fmt, "bgr666"))
Liu Ying032003c2016-07-08 17:40:58 +0800206 bus_format = MEDIA_BUS_FMT_RGB666_1X18;
Emil Renner Berthing9e74d292014-03-29 13:57:44 +0100207 else if (!strcmp(fmt, "lvds666"))
Liu Ying032003c2016-07-08 17:40:58 +0800208 bus_format = MEDIA_BUS_FMT_RGB666_1X24_CPADHI;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200209 }
Liu Ying032003c2016-07-08 17:40:58 +0800210 imxpd->imx_encoder.bus_format = bus_format;
211 imxpd->imx_encoder.di_hsync_pin = 2;
212 imxpd->imx_encoder.di_vsync_pin = 3;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200213
Philipp Zabel553a59f2015-05-19 16:28:12 +0200214 /* port@1 is the output port */
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200215 ep = of_graph_get_endpoint_by_regs(np, 1, -1);
216 if (ep) {
217 struct device_node *remote;
Philipp Zabel553a59f2015-05-19 16:28:12 +0200218
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200219 remote = of_graph_get_remote_port_parent(ep);
220 of_node_put(ep);
221 if (remote) {
222 imxpd->panel = of_drm_find_panel(remote);
223 of_node_put(remote);
Philipp Zabel553a59f2015-05-19 16:28:12 +0200224 }
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200225 if (!imxpd->panel)
226 return -EPROBE_DEFER;
Liu Yingd70e96a2015-02-23 11:09:51 +0800227 }
Philipp Zabel628f435b2014-02-25 11:55:04 +0100228
Russell King17b50012013-11-03 11:23:34 +0000229 imxpd->dev = dev;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200230
Russell King1b3f7672013-11-03 13:30:48 +0000231 ret = imx_pd_register(drm, imxpd);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200232 if (ret)
233 return ret;
234
Russell King17b50012013-11-03 11:23:34 +0000235 dev_set_drvdata(dev, imxpd);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200236
237 return 0;
238}
239
Russell King17b50012013-11-03 11:23:34 +0000240static void imx_pd_unbind(struct device *dev, struct device *master,
241 void *data)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200242{
Russell King17b50012013-11-03 11:23:34 +0000243 struct imx_parallel_display *imxpd = dev_get_drvdata(dev);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200244
Liu Ying032003c2016-07-08 17:40:58 +0800245 imxpd->imx_encoder.encoder.funcs->destroy(&imxpd->imx_encoder.encoder);
Russell King1b3f7672013-11-03 13:30:48 +0000246 imxpd->connector.funcs->destroy(&imxpd->connector);
Peter Seidererdcbc9eb2013-12-08 22:03:58 +0100247
248 kfree(imxpd->edid);
Russell King17b50012013-11-03 11:23:34 +0000249}
Sascha Hauer19022aa2012-09-21 10:07:48 +0200250
Russell King17b50012013-11-03 11:23:34 +0000251static const struct component_ops imx_pd_ops = {
252 .bind = imx_pd_bind,
253 .unbind = imx_pd_unbind,
254};
255
256static int imx_pd_probe(struct platform_device *pdev)
257{
258 return component_add(&pdev->dev, &imx_pd_ops);
259}
260
261static int imx_pd_remove(struct platform_device *pdev)
262{
263 component_del(&pdev->dev, &imx_pd_ops);
Peter Seidererdcbc9eb2013-12-08 22:03:58 +0100264
Sascha Hauer19022aa2012-09-21 10:07:48 +0200265 return 0;
266}
267
268static const struct of_device_id imx_pd_dt_ids[] = {
269 { .compatible = "fsl,imx-parallel-display", },
270 { /* sentinel */ }
271};
Jiada Wangfa45f2c2013-06-07 18:36:46 +0900272MODULE_DEVICE_TABLE(of, imx_pd_dt_ids);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200273
274static struct platform_driver imx_pd_driver = {
275 .probe = imx_pd_probe,
Bill Pemberton99c28f12012-11-19 13:20:51 -0500276 .remove = imx_pd_remove,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200277 .driver = {
278 .of_match_table = imx_pd_dt_ids,
279 .name = "imx-parallel-display",
Sascha Hauer19022aa2012-09-21 10:07:48 +0200280 },
281};
282
283module_platform_driver(imx_pd_driver);
284
285MODULE_DESCRIPTION("i.MX parallel display driver");
286MODULE_AUTHOR("Sascha Hauer, Pengutronix");
287MODULE_LICENSE("GPL");
Fabio Estevamb2da05f2013-08-18 21:40:05 -0300288MODULE_ALIAS("platform:imx-parallel-display");