blob: 7b7bb31008319562bcb8d960e8e954ca6c3ceecb [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);
Quentin Lambert63bc5162014-08-04 21:07:07 +020076
Fabio Estevam39dcf4f2014-02-26 20:53:42 -030077 if (!mode)
78 return -EINVAL;
Lothar Waßmann075c4572014-01-13 11:24:33 +010079 of_get_drm_display_mode(np, &imxpd->mode, OF_USE_NATIVE_MODE);
Marek Vasutafb12ed2013-04-03 12:36:28 -030080 drm_mode_copy(mode, &imxpd->mode);
81 mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
82 drm_mode_probed_add(connector, mode);
83 num_modes++;
84 }
85
Sascha Hauer19022aa2012-09-21 10:07:48 +020086 return num_modes;
87}
88
Sascha Hauer19022aa2012-09-21 10:07:48 +020089static struct drm_encoder *imx_pd_connector_best_encoder(
90 struct drm_connector *connector)
91{
92 struct imx_parallel_display *imxpd = con_to_imxpd(connector);
93
Philipp Zabel49f98bc2016-07-06 14:49:24 +020094 return &imxpd->encoder;
Sascha Hauer19022aa2012-09-21 10:07:48 +020095}
96
Liu Yingf6e396e2016-07-08 17:41:01 +080097static void imx_pd_encoder_enable(struct drm_encoder *encoder)
Sascha Hauer19022aa2012-09-21 10:07:48 +020098{
Philipp Zabel49f98bc2016-07-06 14:49:24 +020099 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
Philipp Zabelad92c8b2014-11-03 15:54:47 +0100100
101 drm_panel_prepare(imxpd->panel);
102 drm_panel_enable(imxpd->panel);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200103}
104
Sascha Hauer19022aa2012-09-21 10:07:48 +0200105static void imx_pd_encoder_disable(struct drm_encoder *encoder)
106{
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200107 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
Philipp Zabelad92c8b2014-11-03 15:54:47 +0100108
109 drm_panel_disable(imxpd->panel);
110 drm_panel_unprepare(imxpd->panel);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200111}
112
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200113static int imx_pd_encoder_atomic_check(struct drm_encoder *encoder,
114 struct drm_crtc_state *crtc_state,
115 struct drm_connector_state *conn_state)
116{
117 struct imx_crtc_state *imx_crtc_state = to_imx_crtc_state(crtc_state);
118 struct drm_display_info *di = &conn_state->connector->display_info;
119 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
120
121 imx_crtc_state->bus_flags = di->bus_flags;
122 if (!imxpd->bus_format && di->num_bus_formats)
123 imx_crtc_state->bus_format = di->bus_formats[0];
124 else
125 imx_crtc_state->bus_format = imxpd->bus_format;
126 imx_crtc_state->di_hsync_pin = 2;
127 imx_crtc_state->di_vsync_pin = 3;
128
129 return 0;
130}
131
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100132static const struct drm_connector_funcs imx_pd_connector_funcs = {
Liu Yingf6e396e2016-07-08 17:41:01 +0800133 .dpms = drm_atomic_helper_connector_dpms,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200134 .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,
Liu Ying255c35f2016-07-08 17:40:56 +0800137 .reset = drm_atomic_helper_connector_reset,
138 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
139 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200140};
141
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100142static const struct drm_connector_helper_funcs imx_pd_connector_helper_funcs = {
Sascha Hauer19022aa2012-09-21 10:07:48 +0200143 .get_modes = imx_pd_connector_get_modes,
144 .best_encoder = imx_pd_connector_best_encoder,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200145};
146
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100147static const struct drm_encoder_funcs imx_pd_encoder_funcs = {
Russell King1b3f7672013-11-03 13:30:48 +0000148 .destroy = imx_drm_encoder_destroy,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200149};
150
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100151static const struct drm_encoder_helper_funcs imx_pd_encoder_helper_funcs = {
Liu Yingf6e396e2016-07-08 17:41:01 +0800152 .enable = imx_pd_encoder_enable,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200153 .disable = imx_pd_encoder_disable,
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200154 .atomic_check = imx_pd_encoder_atomic_check,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200155};
156
Russell King1b3f7672013-11-03 13:30:48 +0000157static int imx_pd_register(struct drm_device *drm,
158 struct imx_parallel_display *imxpd)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200159{
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200160 struct drm_encoder *encoder = &imxpd->encoder;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200161 int ret;
162
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200163 ret = imx_drm_encoder_parse_of(drm, encoder, imxpd->dev->of_node);
Russell King1b3f7672013-11-03 13:30:48 +0000164 if (ret)
165 return ret;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200166
Denis Cariklic026a3f2014-06-17 15:34:51 +0200167 /* set the connector's dpms to OFF so that
168 * drm_helper_connector_dpms() won't return
169 * immediately since the current state is ON
170 * at this point.
171 */
172 imxpd->connector.dpms = DRM_MODE_DPMS_OFF;
173
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200174 drm_encoder_helper_add(encoder, &imx_pd_encoder_helper_funcs);
175 drm_encoder_init(drm, encoder, &imx_pd_encoder_funcs,
176 DRM_MODE_ENCODER_NONE, NULL);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200177
178 drm_connector_helper_add(&imxpd->connector,
179 &imx_pd_connector_helper_funcs);
Russell King1b3f7672013-11-03 13:30:48 +0000180 drm_connector_init(drm, &imxpd->connector, &imx_pd_connector_funcs,
181 DRM_MODE_CONNECTOR_VGA);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200182
Philipp Zabel628f435b2014-02-25 11:55:04 +0100183 if (imxpd->panel)
184 drm_panel_attach(imxpd->panel, &imxpd->connector);
185
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200186 drm_mode_connector_attach_encoder(&imxpd->connector, encoder);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200187
Sascha Hauer19022aa2012-09-21 10:07:48 +0200188 return 0;
189}
190
Russell King17b50012013-11-03 11:23:34 +0000191static int imx_pd_bind(struct device *dev, struct device *master, void *data)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200192{
Russell King1b3f7672013-11-03 13:30:48 +0000193 struct drm_device *drm = data;
Russell King17b50012013-11-03 11:23:34 +0000194 struct device_node *np = dev->of_node;
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200195 struct device_node *ep;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200196 const u8 *edidp;
197 struct imx_parallel_display *imxpd;
198 int ret;
Liu Ying032003c2016-07-08 17:40:58 +0800199 u32 bus_format = 0;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200200 const char *fmt;
201
Russell King17b50012013-11-03 11:23:34 +0000202 imxpd = devm_kzalloc(dev, sizeof(*imxpd), GFP_KERNEL);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200203 if (!imxpd)
204 return -ENOMEM;
205
Sascha Hauer19022aa2012-09-21 10:07:48 +0200206 edidp = of_get_property(np, "edid", &imxpd->edid_len);
207 if (edidp)
208 imxpd->edid = kmemdup(edidp, imxpd->edid_len, GFP_KERNEL);
209
210 ret = of_property_read_string(np, "interface-pix-fmt", &fmt);
211 if (!ret) {
212 if (!strcmp(fmt, "rgb24"))
Liu Ying032003c2016-07-08 17:40:58 +0800213 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200214 else if (!strcmp(fmt, "rgb565"))
Liu Ying032003c2016-07-08 17:40:58 +0800215 bus_format = MEDIA_BUS_FMT_RGB565_1X16;
Marek Vasut7d0a66c2013-04-22 23:19:31 +0200216 else if (!strcmp(fmt, "bgr666"))
Liu Ying032003c2016-07-08 17:40:58 +0800217 bus_format = MEDIA_BUS_FMT_RGB666_1X18;
Emil Renner Berthing9e74d292014-03-29 13:57:44 +0100218 else if (!strcmp(fmt, "lvds666"))
Liu Ying032003c2016-07-08 17:40:58 +0800219 bus_format = MEDIA_BUS_FMT_RGB666_1X24_CPADHI;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200220 }
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200221 imxpd->bus_format = bus_format;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200222
Philipp Zabel553a59f2015-05-19 16:28:12 +0200223 /* port@1 is the output port */
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200224 ep = of_graph_get_endpoint_by_regs(np, 1, -1);
225 if (ep) {
226 struct device_node *remote;
Philipp Zabel553a59f2015-05-19 16:28:12 +0200227
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200228 remote = of_graph_get_remote_port_parent(ep);
229 of_node_put(ep);
230 if (remote) {
231 imxpd->panel = of_drm_find_panel(remote);
232 of_node_put(remote);
Philipp Zabel553a59f2015-05-19 16:28:12 +0200233 }
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200234 if (!imxpd->panel)
235 return -EPROBE_DEFER;
Liu Yingd70e96a2015-02-23 11:09:51 +0800236 }
Philipp Zabel628f435b2014-02-25 11:55:04 +0100237
Russell King17b50012013-11-03 11:23:34 +0000238 imxpd->dev = dev;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200239
Russell King1b3f7672013-11-03 13:30:48 +0000240 ret = imx_pd_register(drm, imxpd);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200241 if (ret)
242 return ret;
243
Russell King17b50012013-11-03 11:23:34 +0000244 dev_set_drvdata(dev, imxpd);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200245
246 return 0;
247}
248
Russell King17b50012013-11-03 11:23:34 +0000249static void imx_pd_unbind(struct device *dev, struct device *master,
250 void *data)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200251{
Russell King17b50012013-11-03 11:23:34 +0000252 struct imx_parallel_display *imxpd = dev_get_drvdata(dev);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200253
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200254 imxpd->encoder.funcs->destroy(&imxpd->encoder);
Russell King1b3f7672013-11-03 13:30:48 +0000255 imxpd->connector.funcs->destroy(&imxpd->connector);
Peter Seidererdcbc9eb2013-12-08 22:03:58 +0100256
257 kfree(imxpd->edid);
Russell King17b50012013-11-03 11:23:34 +0000258}
Sascha Hauer19022aa2012-09-21 10:07:48 +0200259
Russell King17b50012013-11-03 11:23:34 +0000260static const struct component_ops imx_pd_ops = {
261 .bind = imx_pd_bind,
262 .unbind = imx_pd_unbind,
263};
264
265static int imx_pd_probe(struct platform_device *pdev)
266{
267 return component_add(&pdev->dev, &imx_pd_ops);
268}
269
270static int imx_pd_remove(struct platform_device *pdev)
271{
272 component_del(&pdev->dev, &imx_pd_ops);
Peter Seidererdcbc9eb2013-12-08 22:03:58 +0100273
Sascha Hauer19022aa2012-09-21 10:07:48 +0200274 return 0;
275}
276
277static const struct of_device_id imx_pd_dt_ids[] = {
278 { .compatible = "fsl,imx-parallel-display", },
279 { /* sentinel */ }
280};
Jiada Wangfa45f2c2013-06-07 18:36:46 +0900281MODULE_DEVICE_TABLE(of, imx_pd_dt_ids);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200282
283static struct platform_driver imx_pd_driver = {
284 .probe = imx_pd_probe,
Bill Pemberton99c28f12012-11-19 13:20:51 -0500285 .remove = imx_pd_remove,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200286 .driver = {
287 .of_match_table = imx_pd_dt_ids,
288 .name = "imx-parallel-display",
Sascha Hauer19022aa2012-09-21 10:07:48 +0200289 },
290};
291
292module_platform_driver(imx_pd_driver);
293
294MODULE_DESCRIPTION("i.MX parallel display driver");
295MODULE_AUTHOR("Sascha Hauer, Pengutronix");
296MODULE_LICENSE("GPL");
Fabio Estevamb2da05f2013-08-18 21:40:05 -0300297MODULE_ALIAS("platform:imx-parallel-display");