blob: 1dad297b01fd5a5723b5cba7dd92b352057c7d7a [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;
Philipp Zabelf140b0c2016-06-27 15:26:31 +020038 struct drm_bridge *bridge;
Sascha Hauer19022aa2012-09-21 10:07:48 +020039};
40
Philipp Zabel3df07392016-07-06 15:47:11 +020041static inline struct imx_parallel_display *con_to_imxpd(struct drm_connector *c)
42{
43 return container_of(c, struct imx_parallel_display, connector);
44}
45
Philipp Zabel49f98bc2016-07-06 14:49:24 +020046static inline struct imx_parallel_display *enc_to_imxpd(struct drm_encoder *e)
47{
48 return container_of(e, struct imx_parallel_display, encoder);
49}
50
Sascha Hauer19022aa2012-09-21 10:07:48 +020051static enum drm_connector_status imx_pd_connector_detect(
52 struct drm_connector *connector, bool force)
53{
54 return connector_status_connected;
55}
56
Sascha Hauer19022aa2012-09-21 10:07:48 +020057static int imx_pd_connector_get_modes(struct drm_connector *connector)
58{
59 struct imx_parallel_display *imxpd = con_to_imxpd(connector);
Marek Vasutafb12ed2013-04-03 12:36:28 -030060 struct device_node *np = imxpd->dev->of_node;
Sascha Hauer19022aa2012-09-21 10:07:48 +020061 int num_modes = 0;
62
Philipp Zabel628f435b2014-02-25 11:55:04 +010063 if (imxpd->panel && imxpd->panel->funcs &&
64 imxpd->panel->funcs->get_modes) {
65 num_modes = imxpd->panel->funcs->get_modes(imxpd->panel);
66 if (num_modes > 0)
67 return num_modes;
68 }
69
Sascha Hauer19022aa2012-09-21 10:07:48 +020070 if (imxpd->edid) {
71 drm_mode_connector_update_edid_property(connector, imxpd->edid);
72 num_modes = drm_add_edid_modes(connector, imxpd->edid);
73 }
74
Marek Vasutafb12ed2013-04-03 12:36:28 -030075 if (np) {
76 struct drm_display_mode *mode = drm_mode_create(connector->dev);
Lothar Waßmann76ecd9c2016-07-12 15:30:01 +020077 int ret;
Quentin Lambert63bc5162014-08-04 21:07:07 +020078
Fabio Estevam39dcf4f2014-02-26 20:53:42 -030079 if (!mode)
80 return -EINVAL;
Lothar Waßmann76ecd9c2016-07-12 15:30:01 +020081
82 ret = of_get_drm_display_mode(np, &imxpd->mode,
83 OF_USE_NATIVE_MODE);
84 if (ret)
85 return ret;
86
Marek Vasutafb12ed2013-04-03 12:36:28 -030087 drm_mode_copy(mode, &imxpd->mode);
88 mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
89 drm_mode_probed_add(connector, mode);
90 num_modes++;
91 }
92
Sascha Hauer19022aa2012-09-21 10:07:48 +020093 return num_modes;
94}
95
Sascha Hauer19022aa2012-09-21 10:07:48 +020096static struct drm_encoder *imx_pd_connector_best_encoder(
97 struct drm_connector *connector)
98{
99 struct imx_parallel_display *imxpd = con_to_imxpd(connector);
100
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200101 return &imxpd->encoder;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200102}
103
Liu Yingf6e396e2016-07-08 17:41:01 +0800104static void imx_pd_encoder_enable(struct drm_encoder *encoder)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200105{
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200106 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
Philipp Zabelad92c8b2014-11-03 15:54:47 +0100107
108 drm_panel_prepare(imxpd->panel);
109 drm_panel_enable(imxpd->panel);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200110}
111
Sascha Hauer19022aa2012-09-21 10:07:48 +0200112static void imx_pd_encoder_disable(struct drm_encoder *encoder)
113{
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200114 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
Philipp Zabelad92c8b2014-11-03 15:54:47 +0100115
116 drm_panel_disable(imxpd->panel);
117 drm_panel_unprepare(imxpd->panel);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200118}
119
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200120static int imx_pd_encoder_atomic_check(struct drm_encoder *encoder,
121 struct drm_crtc_state *crtc_state,
122 struct drm_connector_state *conn_state)
123{
124 struct imx_crtc_state *imx_crtc_state = to_imx_crtc_state(crtc_state);
125 struct drm_display_info *di = &conn_state->connector->display_info;
126 struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
127
128 imx_crtc_state->bus_flags = di->bus_flags;
129 if (!imxpd->bus_format && di->num_bus_formats)
130 imx_crtc_state->bus_format = di->bus_formats[0];
131 else
132 imx_crtc_state->bus_format = imxpd->bus_format;
133 imx_crtc_state->di_hsync_pin = 2;
134 imx_crtc_state->di_vsync_pin = 3;
135
136 return 0;
137}
138
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100139static const struct drm_connector_funcs imx_pd_connector_funcs = {
Liu Yingf6e396e2016-07-08 17:41:01 +0800140 .dpms = drm_atomic_helper_connector_dpms,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200141 .fill_modes = drm_helper_probe_single_connector_modes,
142 .detect = imx_pd_connector_detect,
Russell King1b3f7672013-11-03 13:30:48 +0000143 .destroy = imx_drm_connector_destroy,
Liu Ying255c35f2016-07-08 17:40:56 +0800144 .reset = drm_atomic_helper_connector_reset,
145 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
146 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200147};
148
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100149static const struct drm_connector_helper_funcs imx_pd_connector_helper_funcs = {
Sascha Hauer19022aa2012-09-21 10:07:48 +0200150 .get_modes = imx_pd_connector_get_modes,
151 .best_encoder = imx_pd_connector_best_encoder,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200152};
153
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100154static const struct drm_encoder_funcs imx_pd_encoder_funcs = {
Russell King1b3f7672013-11-03 13:30:48 +0000155 .destroy = imx_drm_encoder_destroy,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200156};
157
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100158static const struct drm_encoder_helper_funcs imx_pd_encoder_helper_funcs = {
Liu Yingf6e396e2016-07-08 17:41:01 +0800159 .enable = imx_pd_encoder_enable,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200160 .disable = imx_pd_encoder_disable,
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200161 .atomic_check = imx_pd_encoder_atomic_check,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200162};
163
Russell King1b3f7672013-11-03 13:30:48 +0000164static int imx_pd_register(struct drm_device *drm,
165 struct imx_parallel_display *imxpd)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200166{
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200167 struct drm_encoder *encoder = &imxpd->encoder;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200168 int ret;
169
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200170 ret = imx_drm_encoder_parse_of(drm, encoder, imxpd->dev->of_node);
Russell King1b3f7672013-11-03 13:30:48 +0000171 if (ret)
172 return ret;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200173
Denis Cariklic026a3f2014-06-17 15:34:51 +0200174 /* set the connector's dpms to OFF so that
175 * drm_helper_connector_dpms() won't return
176 * immediately since the current state is ON
177 * at this point.
178 */
179 imxpd->connector.dpms = DRM_MODE_DPMS_OFF;
180
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200181 drm_encoder_helper_add(encoder, &imx_pd_encoder_helper_funcs);
182 drm_encoder_init(drm, encoder, &imx_pd_encoder_funcs,
183 DRM_MODE_ENCODER_NONE, NULL);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200184
Philipp Zabelf140b0c2016-06-27 15:26:31 +0200185 if (!imxpd->bridge) {
186 drm_connector_helper_add(&imxpd->connector,
187 &imx_pd_connector_helper_funcs);
188 drm_connector_init(drm, &imxpd->connector,
189 &imx_pd_connector_funcs,
190 DRM_MODE_CONNECTOR_VGA);
191 }
Sascha Hauer19022aa2012-09-21 10:07:48 +0200192
Philipp Zabel628f435b2014-02-25 11:55:04 +0100193 if (imxpd->panel)
194 drm_panel_attach(imxpd->panel, &imxpd->connector);
195
Philipp Zabelf140b0c2016-06-27 15:26:31 +0200196 if (imxpd->bridge) {
197 imxpd->bridge->encoder = encoder;
198 encoder->bridge = imxpd->bridge;
199 ret = drm_bridge_attach(drm, imxpd->bridge);
200 if (ret < 0) {
201 dev_err(imxpd->dev, "failed to attach bridge: %d\n",
202 ret);
203 return ret;
204 }
205 } else {
206 drm_mode_connector_attach_encoder(&imxpd->connector, encoder);
207 }
Sascha Hauer19022aa2012-09-21 10:07:48 +0200208
Sascha Hauer19022aa2012-09-21 10:07:48 +0200209 return 0;
210}
211
Russell King17b50012013-11-03 11:23:34 +0000212static int imx_pd_bind(struct device *dev, struct device *master, void *data)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200213{
Russell King1b3f7672013-11-03 13:30:48 +0000214 struct drm_device *drm = data;
Russell King17b50012013-11-03 11:23:34 +0000215 struct device_node *np = dev->of_node;
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200216 struct device_node *ep;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200217 const u8 *edidp;
218 struct imx_parallel_display *imxpd;
219 int ret;
Liu Ying032003c2016-07-08 17:40:58 +0800220 u32 bus_format = 0;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200221 const char *fmt;
222
Russell King17b50012013-11-03 11:23:34 +0000223 imxpd = devm_kzalloc(dev, sizeof(*imxpd), GFP_KERNEL);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200224 if (!imxpd)
225 return -ENOMEM;
226
Sascha Hauer19022aa2012-09-21 10:07:48 +0200227 edidp = of_get_property(np, "edid", &imxpd->edid_len);
228 if (edidp)
229 imxpd->edid = kmemdup(edidp, imxpd->edid_len, GFP_KERNEL);
230
231 ret = of_property_read_string(np, "interface-pix-fmt", &fmt);
232 if (!ret) {
233 if (!strcmp(fmt, "rgb24"))
Liu Ying032003c2016-07-08 17:40:58 +0800234 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200235 else if (!strcmp(fmt, "rgb565"))
Liu Ying032003c2016-07-08 17:40:58 +0800236 bus_format = MEDIA_BUS_FMT_RGB565_1X16;
Marek Vasut7d0a66c2013-04-22 23:19:31 +0200237 else if (!strcmp(fmt, "bgr666"))
Liu Ying032003c2016-07-08 17:40:58 +0800238 bus_format = MEDIA_BUS_FMT_RGB666_1X18;
Emil Renner Berthing9e74d292014-03-29 13:57:44 +0100239 else if (!strcmp(fmt, "lvds666"))
Liu Ying032003c2016-07-08 17:40:58 +0800240 bus_format = MEDIA_BUS_FMT_RGB666_1X24_CPADHI;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200241 }
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200242 imxpd->bus_format = bus_format;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200243
Philipp Zabel553a59f2015-05-19 16:28:12 +0200244 /* port@1 is the output port */
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200245 ep = of_graph_get_endpoint_by_regs(np, 1, -1);
246 if (ep) {
247 struct device_node *remote;
Philipp Zabel553a59f2015-05-19 16:28:12 +0200248
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200249 remote = of_graph_get_remote_port_parent(ep);
Philipp Zabelf140b0c2016-06-27 15:26:31 +0200250 if (!remote) {
251 dev_warn(dev, "endpoint %s not connected\n",
252 ep->full_name);
253 of_node_put(ep);
254 return -ENODEV;
Philipp Zabel553a59f2015-05-19 16:28:12 +0200255 }
Philipp Zabelf140b0c2016-06-27 15:26:31 +0200256 of_node_put(ep);
257
258 imxpd->panel = of_drm_find_panel(remote);
259 if (imxpd->panel) {
260 dev_dbg(dev, "found panel %s\n", remote->full_name);
261 } else {
262 imxpd->bridge = of_drm_find_bridge(remote);
263 if (imxpd->bridge)
264 dev_dbg(dev, "found bridge %s\n",
265 remote->full_name);
266 }
267 if (!imxpd->panel && !imxpd->bridge) {
268 dev_dbg(dev, "waiting for panel or bridge %s\n",
269 remote->full_name);
270 of_node_put(remote);
Philipp Zabel3a1c1172016-05-03 14:39:29 +0200271 return -EPROBE_DEFER;
Philipp Zabelf140b0c2016-06-27 15:26:31 +0200272 }
273 of_node_put(remote);
Liu Yingd70e96a2015-02-23 11:09:51 +0800274 }
Philipp Zabel628f435b2014-02-25 11:55:04 +0100275
Russell King17b50012013-11-03 11:23:34 +0000276 imxpd->dev = dev;
Sascha Hauer19022aa2012-09-21 10:07:48 +0200277
Russell King1b3f7672013-11-03 13:30:48 +0000278 ret = imx_pd_register(drm, imxpd);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200279 if (ret)
280 return ret;
281
Russell King17b50012013-11-03 11:23:34 +0000282 dev_set_drvdata(dev, imxpd);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200283
284 return 0;
285}
286
Russell King17b50012013-11-03 11:23:34 +0000287static void imx_pd_unbind(struct device *dev, struct device *master,
288 void *data)
Sascha Hauer19022aa2012-09-21 10:07:48 +0200289{
Russell King17b50012013-11-03 11:23:34 +0000290 struct imx_parallel_display *imxpd = dev_get_drvdata(dev);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200291
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200292 imxpd->encoder.funcs->destroy(&imxpd->encoder);
Russell King1b3f7672013-11-03 13:30:48 +0000293 imxpd->connector.funcs->destroy(&imxpd->connector);
Peter Seidererdcbc9eb2013-12-08 22:03:58 +0100294
295 kfree(imxpd->edid);
Russell King17b50012013-11-03 11:23:34 +0000296}
Sascha Hauer19022aa2012-09-21 10:07:48 +0200297
Russell King17b50012013-11-03 11:23:34 +0000298static const struct component_ops imx_pd_ops = {
299 .bind = imx_pd_bind,
300 .unbind = imx_pd_unbind,
301};
302
303static int imx_pd_probe(struct platform_device *pdev)
304{
305 return component_add(&pdev->dev, &imx_pd_ops);
306}
307
308static int imx_pd_remove(struct platform_device *pdev)
309{
310 component_del(&pdev->dev, &imx_pd_ops);
Peter Seidererdcbc9eb2013-12-08 22:03:58 +0100311
Sascha Hauer19022aa2012-09-21 10:07:48 +0200312 return 0;
313}
314
315static const struct of_device_id imx_pd_dt_ids[] = {
316 { .compatible = "fsl,imx-parallel-display", },
317 { /* sentinel */ }
318};
Jiada Wangfa45f2c2013-06-07 18:36:46 +0900319MODULE_DEVICE_TABLE(of, imx_pd_dt_ids);
Sascha Hauer19022aa2012-09-21 10:07:48 +0200320
321static struct platform_driver imx_pd_driver = {
322 .probe = imx_pd_probe,
Bill Pemberton99c28f12012-11-19 13:20:51 -0500323 .remove = imx_pd_remove,
Sascha Hauer19022aa2012-09-21 10:07:48 +0200324 .driver = {
325 .of_match_table = imx_pd_dt_ids,
326 .name = "imx-parallel-display",
Sascha Hauer19022aa2012-09-21 10:07:48 +0200327 },
328};
329
330module_platform_driver(imx_pd_driver);
331
332MODULE_DESCRIPTION("i.MX parallel display driver");
333MODULE_AUTHOR("Sascha Hauer, Pengutronix");
334MODULE_LICENSE("GPL");
Fabio Estevamb2da05f2013-08-18 21:40:05 -0300335MODULE_ALIAS("platform:imx-parallel-display");