blob: 9fe88c9b4b0d8f97d10d2b8defd2adbfbc395ece [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)
30#define enc_to_imxpd(x) container_of(x, struct imx_parallel_display, encoder)
31
32struct imx_parallel_display {
33 struct drm_connector connector;
Sascha Hauer19022aa2012-09-21 10:07:48 +020034 struct drm_encoder encoder;
Sascha Hauer19022aa2012-09-21 10:07:48 +020035 struct device *dev;
36 void *edid;
37 int edid_len;
Philipp Zabel2872c802015-02-02 17:25:59 +010038 u32 bus_format;
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);
Philipp Zabel9832e812015-11-13 18:10:04 +010060 if (!imxpd->bus_format && di->num_bus_formats)
61 imxpd->bus_format = di->bus_formats[0];
Philipp Zabel628f435b2014-02-25 11:55:04 +010062 if (num_modes > 0)
63 return num_modes;
64 }
65
Sascha Hauer19022aa2012-09-21 10:07:48 +020066 if (imxpd->edid) {
67 drm_mode_connector_update_edid_property(connector, imxpd->edid);
68 num_modes = drm_add_edid_modes(connector, imxpd->edid);
69 }
70
Marek Vasutafb12ed2013-04-03 12:36:28 -030071 if (np) {
72 struct drm_display_mode *mode = drm_mode_create(connector->dev);
Quentin Lambert63bc51642014-08-04 21:07:07 +020073
Fabio Estevam39dcf4f2014-02-26 20:53:42 -030074 if (!mode)
75 return -EINVAL;
Lothar Waßmann075c4572014-01-13 11:24:33 +010076 of_get_drm_display_mode(np, &imxpd->mode, OF_USE_NATIVE_MODE);
Marek Vasutafb12ed2013-04-03 12:36:28 -030077 drm_mode_copy(mode, &imxpd->mode);
78 mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
79 drm_mode_probed_add(connector, mode);
80 num_modes++;
81 }
82
Sascha Hauer19022aa2012-09-21 10:07:48 +020083 return num_modes;
84}
85
Sascha Hauer19022aa2012-09-21 10:07:48 +020086static struct drm_encoder *imx_pd_connector_best_encoder(
87 struct drm_connector *connector)
88{
89 struct imx_parallel_display *imxpd = con_to_imxpd(connector);
90
91 return &imxpd->encoder;
92}
93
94static void imx_pd_encoder_dpms(struct drm_encoder *encoder, int mode)
95{
Philipp Zabel628f435b2014-02-25 11:55:04 +010096 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
97
98 if (mode != DRM_MODE_DPMS_ON)
99 drm_panel_disable(imxpd->panel);
100 else
101 drm_panel_enable(imxpd->panel);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200102}
103
Sascha Hauer19022aa2012-09-21 10:07:48 +0200104static void imx_pd_encoder_prepare(struct drm_encoder *encoder)
105{
106 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
Philipp Zabel4ed094f2016-05-09 17:02:13 +0200107 imx_drm_set_bus_config(encoder, imxpd->bus_format, 2, 3,
108 imxpd->connector.display_info.bus_flags);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200109}
110
111static void imx_pd_encoder_commit(struct drm_encoder *encoder)
112{
Philipp Zabelad92c8b2014-11-03 15:54:47 +0100113 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
114
115 drm_panel_prepare(imxpd->panel);
116 drm_panel_enable(imxpd->panel);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200117}
118
119static void imx_pd_encoder_mode_set(struct drm_encoder *encoder,
Steve Longerbeameb10d632014-12-18 18:00:24 -0800120 struct drm_display_mode *orig_mode,
121 struct drm_display_mode *mode)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200122{
123}
124
125static void imx_pd_encoder_disable(struct drm_encoder *encoder)
126{
Philipp Zabelad92c8b2014-11-03 15:54:47 +0100127 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
128
129 drm_panel_disable(imxpd->panel);
130 drm_panel_unprepare(imxpd->panel);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200131}
132
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100133static const struct drm_connector_funcs imx_pd_connector_funcs = {
Sascha Hauer19022aa2012-09-21 10:07:48 +0200134 .dpms = drm_helper_connector_dpms,
135 .fill_modes = drm_helper_probe_single_connector_modes,
136 .detect = imx_pd_connector_detect,
Russell King1b3f7672013-11-03 13:30:48 +0000137 .destroy = imx_drm_connector_destroy,
Liu Ying255c35f2016-07-08 17:40:56 +0800138 .reset = drm_atomic_helper_connector_reset,
139 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
140 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200141};
142
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100143static const struct drm_connector_helper_funcs imx_pd_connector_helper_funcs = {
Sascha Hauer19022aa2012-09-21 10:07:48 +0200144 .get_modes = imx_pd_connector_get_modes,
145 .best_encoder = imx_pd_connector_best_encoder,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200146};
147
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100148static const struct drm_encoder_funcs imx_pd_encoder_funcs = {
Russell King1b3f7672013-11-03 13:30:48 +0000149 .destroy = imx_drm_encoder_destroy,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200150};
151
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100152static const struct drm_encoder_helper_funcs imx_pd_encoder_helper_funcs = {
Sascha Hauer19022aa2012-09-21 10:07:48 +0200153 .dpms = imx_pd_encoder_dpms,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200154 .prepare = imx_pd_encoder_prepare,
155 .commit = imx_pd_encoder_commit,
156 .mode_set = imx_pd_encoder_mode_set,
157 .disable = imx_pd_encoder_disable,
158};
159
Russell King1b3f7672013-11-03 13:30:48 +0000160static int imx_pd_register(struct drm_device *drm,
161 struct imx_parallel_display *imxpd)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200162{
163 int ret;
164
Russell King1b3f7672013-11-03 13:30:48 +0000165 ret = imx_drm_encoder_parse_of(drm, &imxpd->encoder,
166 imxpd->dev->of_node);
167 if (ret)
168 return ret;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200169
Denis Cariklic026a3f2014-06-17 15:34:51 +0200170 /* set the connector's dpms to OFF so that
171 * drm_helper_connector_dpms() won't return
172 * immediately since the current state is ON
173 * at this point.
174 */
175 imxpd->connector.dpms = DRM_MODE_DPMS_OFF;
176
Sascha Hauer19022aa2012-09-21 10:07:48 +0200177 drm_encoder_helper_add(&imxpd->encoder, &imx_pd_encoder_helper_funcs);
Russell King1b3f7672013-11-03 13:30:48 +0000178 drm_encoder_init(drm, &imxpd->encoder, &imx_pd_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200179 DRM_MODE_ENCODER_NONE, NULL);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200180
181 drm_connector_helper_add(&imxpd->connector,
182 &imx_pd_connector_helper_funcs);
Russell King1b3f7672013-11-03 13:30:48 +0000183 drm_connector_init(drm, &imxpd->connector, &imx_pd_connector_funcs,
184 DRM_MODE_CONNECTOR_VGA);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200185
Philipp Zabel628f435b2014-02-25 11:55:04 +0100186 if (imxpd->panel)
187 drm_panel_attach(imxpd->panel, &imxpd->connector);
188
Russell King1b3f7672013-11-03 13:30:48 +0000189 drm_mode_connector_attach_encoder(&imxpd->connector, &imxpd->encoder);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200190
Sascha Hauer19022aa2012-09-21 10:07:48 +0200191 return 0;
192}
193
Russell King17b50012013-11-03 11:23:34 +0000194static int imx_pd_bind(struct device *dev, struct device *master, void *data)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200195{
Russell King1b3f7672013-11-03 13:30:48 +0000196 struct drm_device *drm = data;
Russell King17b50012013-11-03 11:23:34 +0000197 struct device_node *np = dev->of_node;
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200198 struct device_node *ep;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200199 const u8 *edidp;
200 struct imx_parallel_display *imxpd;
201 int ret;
202 const char *fmt;
203
Russell King17b50012013-11-03 11:23:34 +0000204 imxpd = devm_kzalloc(dev, sizeof(*imxpd), GFP_KERNEL);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200205 if (!imxpd)
206 return -ENOMEM;
207
Sascha Hauer19022aa2012-09-21 10:07:48 +0200208 edidp = of_get_property(np, "edid", &imxpd->edid_len);
209 if (edidp)
210 imxpd->edid = kmemdup(edidp, imxpd->edid_len, GFP_KERNEL);
211
212 ret = of_property_read_string(np, "interface-pix-fmt", &fmt);
213 if (!ret) {
214 if (!strcmp(fmt, "rgb24"))
Philipp Zabel2872c802015-02-02 17:25:59 +0100215 imxpd->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200216 else if (!strcmp(fmt, "rgb565"))
Philipp Zabel2872c802015-02-02 17:25:59 +0100217 imxpd->bus_format = MEDIA_BUS_FMT_RGB565_1X16;
Marek Vasut7d0a66c2013-04-22 23:19:31 +0200218 else if (!strcmp(fmt, "bgr666"))
Philipp Zabel2872c802015-02-02 17:25:59 +0100219 imxpd->bus_format = MEDIA_BUS_FMT_RGB666_1X18;
Emil Renner Berthing9e74d292014-03-29 13:57:44 +0100220 else if (!strcmp(fmt, "lvds666"))
Philipp Zabel2872c802015-02-02 17:25:59 +0100221 imxpd->bus_format = MEDIA_BUS_FMT_RGB666_1X24_CPADHI;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200222 }
223
Philipp Zabel553a59f2015-05-19 16:28:12 +0200224 /* port@1 is the output port */
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200225 ep = of_graph_get_endpoint_by_regs(np, 1, -1);
226 if (ep) {
227 struct device_node *remote;
Philipp Zabel553a59f2015-05-19 16:28:12 +0200228
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200229 remote = of_graph_get_remote_port_parent(ep);
230 of_node_put(ep);
231 if (remote) {
232 imxpd->panel = of_drm_find_panel(remote);
233 of_node_put(remote);
Philipp Zabel553a59f2015-05-19 16:28:12 +0200234 }
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200235 if (!imxpd->panel)
236 return -EPROBE_DEFER;
Liu Yingd70e96a2015-02-23 11:09:51 +0800237 }
Philipp Zabel628f435b2014-02-25 11:55:04 +0100238
Russell King17b50012013-11-03 11:23:34 +0000239 imxpd->dev = dev;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200240
Russell King1b3f7672013-11-03 13:30:48 +0000241 ret = imx_pd_register(drm, imxpd);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200242 if (ret)
243 return ret;
244
Russell King17b50012013-11-03 11:23:34 +0000245 dev_set_drvdata(dev, imxpd);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200246
247 return 0;
248}
249
Russell King17b50012013-11-03 11:23:34 +0000250static void imx_pd_unbind(struct device *dev, struct device *master,
251 void *data)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200252{
Russell King17b50012013-11-03 11:23:34 +0000253 struct imx_parallel_display *imxpd = dev_get_drvdata(dev);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200254
Russell King1b3f7672013-11-03 13:30:48 +0000255 imxpd->encoder.funcs->destroy(&imxpd->encoder);
256 imxpd->connector.funcs->destroy(&imxpd->connector);
Peter Seidererdcbc9eb2013-12-08 22:03:58 +0100257
258 kfree(imxpd->edid);
Russell King17b50012013-11-03 11:23:34 +0000259}
Sascha Hauer19022aa2012-09-21 10:07:48 +0200260
Russell King17b50012013-11-03 11:23:34 +0000261static const struct component_ops imx_pd_ops = {
262 .bind = imx_pd_bind,
263 .unbind = imx_pd_unbind,
264};
265
266static int imx_pd_probe(struct platform_device *pdev)
267{
268 return component_add(&pdev->dev, &imx_pd_ops);
269}
270
271static int imx_pd_remove(struct platform_device *pdev)
272{
273 component_del(&pdev->dev, &imx_pd_ops);
Peter Seidererdcbc9eb2013-12-08 22:03:58 +0100274
Sascha Hauer19022aa2012-09-21 10:07:48 +0200275 return 0;
276}
277
278static const struct of_device_id imx_pd_dt_ids[] = {
279 { .compatible = "fsl,imx-parallel-display", },
280 { /* sentinel */ }
281};
Jiada Wangfa45f2c2013-06-07 18:36:46 +0900282MODULE_DEVICE_TABLE(of, imx_pd_dt_ids);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200283
284static struct platform_driver imx_pd_driver = {
285 .probe = imx_pd_probe,
Bill Pemberton99c28f12012-11-19 13:20:51 -0500286 .remove = imx_pd_remove,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200287 .driver = {
288 .of_match_table = imx_pd_dt_ids,
289 .name = "imx-parallel-display",
Sascha Hauer19022aa2012-09-21 10:07:48 +0200290 },
291};
292
293module_platform_driver(imx_pd_driver);
294
295MODULE_DESCRIPTION("i.MX parallel display driver");
296MODULE_AUTHOR("Sascha Hauer, Pengutronix");
297MODULE_LICENSE("GPL");
Fabio Estevamb2da05f2013-08-18 21:40:05 -0300298MODULE_ALIAS("platform:imx-parallel-display");