blob: 4abac21c5c9bbc0751df61f22bc8f1eaca020b37 [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
Sascha Hauer19022aa2012-09-21 10:07:48 +020029struct imx_parallel_display {
30 struct drm_connector connector;
Philipp Zabel49f98bc2016-07-06 14:49:24 +020031 struct drm_encoder encoder;
Sascha Hauer19022aa2012-09-21 10:07:48 +020032 struct device *dev;
33 void *edid;
34 int edid_len;
Philipp Zabel49f98bc2016-07-06 14:49:24 +020035 u32 bus_format;
Sascha Hauer19022aa2012-09-21 10:07:48 +020036 struct drm_display_mode mode;
Philipp Zabel628f435b2014-02-25 11:55:04 +010037 struct drm_panel *panel;
Sascha Hauer19022aa2012-09-21 10:07:48 +020038};
39
Philipp Zabel3df07392016-07-06 15:47:11 +020040static inline struct imx_parallel_display *con_to_imxpd(struct drm_connector *c)
41{
42 return container_of(c, struct imx_parallel_display, connector);
43}
44
Philipp Zabel49f98bc2016-07-06 14:49:24 +020045static inline struct imx_parallel_display *enc_to_imxpd(struct drm_encoder *e)
46{
47 return container_of(e, struct imx_parallel_display, encoder);
48}
49
Sascha Hauer19022aa2012-09-21 10:07:48 +020050static enum drm_connector_status imx_pd_connector_detect(
51 struct drm_connector *connector, bool force)
52{
53 return connector_status_connected;
54}
55
Sascha Hauer19022aa2012-09-21 10:07:48 +020056static int imx_pd_connector_get_modes(struct drm_connector *connector)
57{
58 struct imx_parallel_display *imxpd = con_to_imxpd(connector);
Marek Vasutafb12ed2013-04-03 12:36:28 -030059 struct device_node *np = imxpd->dev->of_node;
Sascha Hauer19022aa2012-09-21 10:07:48 +020060 int num_modes = 0;
61
Philipp Zabel628f435b2014-02-25 11:55:04 +010062 if (imxpd->panel && imxpd->panel->funcs &&
63 imxpd->panel->funcs->get_modes) {
64 num_modes = imxpd->panel->funcs->get_modes(imxpd->panel);
65 if (num_modes > 0)
66 return num_modes;
67 }
68
Sascha Hauer19022aa2012-09-21 10:07:48 +020069 if (imxpd->edid) {
70 drm_mode_connector_update_edid_property(connector, imxpd->edid);
71 num_modes = drm_add_edid_modes(connector, imxpd->edid);
72 }
73
Marek Vasutafb12ed2013-04-03 12:36:28 -030074 if (np) {
75 struct drm_display_mode *mode = drm_mode_create(connector->dev);
Lothar Waßmann76ecd9c2016-07-12 15:30:01 +020076 int ret;
Quentin Lambert63bc5162014-08-04 21:07:07 +020077
Fabio Estevam39dcf4f2014-02-26 20:53:42 -030078 if (!mode)
79 return -EINVAL;
Lothar Waßmann76ecd9c2016-07-12 15:30:01 +020080
81 ret = of_get_drm_display_mode(np, &imxpd->mode,
82 OF_USE_NATIVE_MODE);
83 if (ret)
84 return ret;
85
Marek Vasutafb12ed2013-04-03 12:36:28 -030086 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++;
90 }
91
Sascha Hauer19022aa2012-09-21 10:07:48 +020092 return num_modes;
93}
94
Sascha Hauer19022aa2012-09-21 10:07:48 +020095static struct drm_encoder *imx_pd_connector_best_encoder(
96 struct drm_connector *connector)
97{
98 struct imx_parallel_display *imxpd = con_to_imxpd(connector);
99
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200100 return &imxpd->encoder;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200101}
102
Liu Yingf6e396e2016-07-08 17:41:01 +0800103static void imx_pd_encoder_enable(struct drm_encoder *encoder)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200104{
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200105 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
Philipp Zabelad92c8b2014-11-03 15:54:47 +0100106
107 drm_panel_prepare(imxpd->panel);
108 drm_panel_enable(imxpd->panel);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200109}
110
Sascha Hauer19022aa2012-09-21 10:07:48 +0200111static void imx_pd_encoder_disable(struct drm_encoder *encoder)
112{
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200113 struct imx_parallel_display *imxpd = enc_to_imxpd(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
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200119static int imx_pd_encoder_atomic_check(struct drm_encoder *encoder,
120 struct drm_crtc_state *crtc_state,
121 struct drm_connector_state *conn_state)
122{
123 struct imx_crtc_state *imx_crtc_state = to_imx_crtc_state(crtc_state);
124 struct drm_display_info *di = &conn_state->connector->display_info;
125 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
126
127 imx_crtc_state->bus_flags = di->bus_flags;
128 if (!imxpd->bus_format && di->num_bus_formats)
129 imx_crtc_state->bus_format = di->bus_formats[0];
130 else
131 imx_crtc_state->bus_format = imxpd->bus_format;
132 imx_crtc_state->di_hsync_pin = 2;
133 imx_crtc_state->di_vsync_pin = 3;
134
135 return 0;
136}
137
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100138static const struct drm_connector_funcs imx_pd_connector_funcs = {
Liu Yingf6e396e2016-07-08 17:41:01 +0800139 .dpms = drm_atomic_helper_connector_dpms,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200140 .fill_modes = drm_helper_probe_single_connector_modes,
141 .detect = imx_pd_connector_detect,
Russell King1b3f7672013-11-03 13:30:48 +0000142 .destroy = imx_drm_connector_destroy,
Liu Ying255c35f2016-07-08 17:40:56 +0800143 .reset = drm_atomic_helper_connector_reset,
144 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
145 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200146};
147
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100148static const struct drm_connector_helper_funcs imx_pd_connector_helper_funcs = {
Sascha Hauer19022aa2012-09-21 10:07:48 +0200149 .get_modes = imx_pd_connector_get_modes,
150 .best_encoder = imx_pd_connector_best_encoder,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200151};
152
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100153static const struct drm_encoder_funcs imx_pd_encoder_funcs = {
Russell King1b3f7672013-11-03 13:30:48 +0000154 .destroy = imx_drm_encoder_destroy,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200155};
156
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100157static const struct drm_encoder_helper_funcs imx_pd_encoder_helper_funcs = {
Liu Yingf6e396e2016-07-08 17:41:01 +0800158 .enable = imx_pd_encoder_enable,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200159 .disable = imx_pd_encoder_disable,
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200160 .atomic_check = imx_pd_encoder_atomic_check,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200161};
162
Russell King1b3f7672013-11-03 13:30:48 +0000163static int imx_pd_register(struct drm_device *drm,
164 struct imx_parallel_display *imxpd)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200165{
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200166 struct drm_encoder *encoder = &imxpd->encoder;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200167 int ret;
168
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200169 ret = imx_drm_encoder_parse_of(drm, encoder, imxpd->dev->of_node);
Russell King1b3f7672013-11-03 13:30:48 +0000170 if (ret)
171 return ret;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200172
Denis Cariklic026a3f2014-06-17 15:34:51 +0200173 /* set the connector's dpms to OFF so that
174 * drm_helper_connector_dpms() won't return
175 * immediately since the current state is ON
176 * at this point.
177 */
178 imxpd->connector.dpms = DRM_MODE_DPMS_OFF;
179
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200180 drm_encoder_helper_add(encoder, &imx_pd_encoder_helper_funcs);
181 drm_encoder_init(drm, encoder, &imx_pd_encoder_funcs,
182 DRM_MODE_ENCODER_NONE, NULL);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200183
184 drm_connector_helper_add(&imxpd->connector,
185 &imx_pd_connector_helper_funcs);
Russell King1b3f7672013-11-03 13:30:48 +0000186 drm_connector_init(drm, &imxpd->connector, &imx_pd_connector_funcs,
187 DRM_MODE_CONNECTOR_VGA);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200188
Philipp Zabel628f435b2014-02-25 11:55:04 +0100189 if (imxpd->panel)
190 drm_panel_attach(imxpd->panel, &imxpd->connector);
191
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200192 drm_mode_connector_attach_encoder(&imxpd->connector, encoder);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200193
Sascha Hauer19022aa2012-09-21 10:07:48 +0200194 return 0;
195}
196
Russell King17b50012013-11-03 11:23:34 +0000197static int imx_pd_bind(struct device *dev, struct device *master, void *data)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200198{
Russell King1b3f7672013-11-03 13:30:48 +0000199 struct drm_device *drm = data;
Russell King17b50012013-11-03 11:23:34 +0000200 struct device_node *np = dev->of_node;
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200201 struct device_node *ep;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200202 const u8 *edidp;
203 struct imx_parallel_display *imxpd;
204 int ret;
Liu Ying032003c2016-07-08 17:40:58 +0800205 u32 bus_format = 0;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200206 const char *fmt;
207
Russell King17b50012013-11-03 11:23:34 +0000208 imxpd = devm_kzalloc(dev, sizeof(*imxpd), GFP_KERNEL);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200209 if (!imxpd)
210 return -ENOMEM;
211
Sascha Hauer19022aa2012-09-21 10:07:48 +0200212 edidp = of_get_property(np, "edid", &imxpd->edid_len);
213 if (edidp)
214 imxpd->edid = kmemdup(edidp, imxpd->edid_len, GFP_KERNEL);
215
216 ret = of_property_read_string(np, "interface-pix-fmt", &fmt);
217 if (!ret) {
218 if (!strcmp(fmt, "rgb24"))
Liu Ying032003c2016-07-08 17:40:58 +0800219 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200220 else if (!strcmp(fmt, "rgb565"))
Liu Ying032003c2016-07-08 17:40:58 +0800221 bus_format = MEDIA_BUS_FMT_RGB565_1X16;
Marek Vasut7d0a66c2013-04-22 23:19:31 +0200222 else if (!strcmp(fmt, "bgr666"))
Liu Ying032003c2016-07-08 17:40:58 +0800223 bus_format = MEDIA_BUS_FMT_RGB666_1X18;
Emil Renner Berthing9e74d292014-03-29 13:57:44 +0100224 else if (!strcmp(fmt, "lvds666"))
Liu Ying032003c2016-07-08 17:40:58 +0800225 bus_format = MEDIA_BUS_FMT_RGB666_1X24_CPADHI;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200226 }
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200227 imxpd->bus_format = bus_format;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200228
Philipp Zabel553a59f2015-05-19 16:28:12 +0200229 /* port@1 is the output port */
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200230 ep = of_graph_get_endpoint_by_regs(np, 1, -1);
231 if (ep) {
232 struct device_node *remote;
Philipp Zabel553a59f2015-05-19 16:28:12 +0200233
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200234 remote = of_graph_get_remote_port_parent(ep);
235 of_node_put(ep);
236 if (remote) {
237 imxpd->panel = of_drm_find_panel(remote);
238 of_node_put(remote);
Philipp Zabel553a59f2015-05-19 16:28:12 +0200239 }
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200240 if (!imxpd->panel)
241 return -EPROBE_DEFER;
Liu Yingd70e96a2015-02-23 11:09:51 +0800242 }
Philipp Zabel628f435b2014-02-25 11:55:04 +0100243
Russell King17b50012013-11-03 11:23:34 +0000244 imxpd->dev = dev;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200245
Russell King1b3f7672013-11-03 13:30:48 +0000246 ret = imx_pd_register(drm, imxpd);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200247 if (ret)
248 return ret;
249
Russell King17b50012013-11-03 11:23:34 +0000250 dev_set_drvdata(dev, imxpd);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200251
252 return 0;
253}
254
Russell King17b50012013-11-03 11:23:34 +0000255static void imx_pd_unbind(struct device *dev, struct device *master,
256 void *data)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200257{
Russell King17b50012013-11-03 11:23:34 +0000258 struct imx_parallel_display *imxpd = dev_get_drvdata(dev);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200259
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200260 imxpd->encoder.funcs->destroy(&imxpd->encoder);
Russell King1b3f7672013-11-03 13:30:48 +0000261 imxpd->connector.funcs->destroy(&imxpd->connector);
Peter Seidererdcbc9eb2013-12-08 22:03:58 +0100262
263 kfree(imxpd->edid);
Russell King17b50012013-11-03 11:23:34 +0000264}
Sascha Hauer19022aa2012-09-21 10:07:48 +0200265
Russell King17b50012013-11-03 11:23:34 +0000266static const struct component_ops imx_pd_ops = {
267 .bind = imx_pd_bind,
268 .unbind = imx_pd_unbind,
269};
270
271static int imx_pd_probe(struct platform_device *pdev)
272{
273 return component_add(&pdev->dev, &imx_pd_ops);
274}
275
276static int imx_pd_remove(struct platform_device *pdev)
277{
278 component_del(&pdev->dev, &imx_pd_ops);
Peter Seidererdcbc9eb2013-12-08 22:03:58 +0100279
Sascha Hauer19022aa2012-09-21 10:07:48 +0200280 return 0;
281}
282
283static const struct of_device_id imx_pd_dt_ids[] = {
284 { .compatible = "fsl,imx-parallel-display", },
285 { /* sentinel */ }
286};
Jiada Wangfa45f2c2013-06-07 18:36:46 +0900287MODULE_DEVICE_TABLE(of, imx_pd_dt_ids);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200288
289static struct platform_driver imx_pd_driver = {
290 .probe = imx_pd_probe,
Bill Pemberton99c28f12012-11-19 13:20:51 -0500291 .remove = imx_pd_remove,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200292 .driver = {
293 .of_match_table = imx_pd_dt_ids,
294 .name = "imx-parallel-display",
Sascha Hauer19022aa2012-09-21 10:07:48 +0200295 },
296};
297
298module_platform_driver(imx_pd_driver);
299
300MODULE_DESCRIPTION("i.MX parallel display driver");
301MODULE_AUTHOR("Sascha Hauer, Pengutronix");
302MODULE_LICENSE("GPL");
Fabio Estevamb2da05f2013-08-18 21:40:05 -0300303MODULE_ALIAS("platform:imx-parallel-display");