2 * i.MX drm driver - LVDS display bridge
4 * Copyright (C) 2012 Sascha Hauer, Pengutronix
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.
16 #include <linux/module.h>
17 #include <linux/clk.h>
18 #include <linux/component.h>
20 #include <drm/drm_fb_helper.h>
21 #include <drm/drm_crtc_helper.h>
22 #include <linux/mfd/syscon.h>
23 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
24 #include <linux/of_address.h>
25 #include <linux/of_device.h>
26 #include <video/of_videomode.h>
27 #include <linux/regmap.h>
28 #include <linux/videodev2.h>
32 #define DRIVER_NAME "imx-ldb"
34 #define LDB_CH0_MODE_EN_TO_DI0 (1 << 0)
35 #define LDB_CH0_MODE_EN_TO_DI1 (3 << 0)
36 #define LDB_CH0_MODE_EN_MASK (3 << 0)
37 #define LDB_CH1_MODE_EN_TO_DI0 (1 << 2)
38 #define LDB_CH1_MODE_EN_TO_DI1 (3 << 2)
39 #define LDB_CH1_MODE_EN_MASK (3 << 2)
40 #define LDB_SPLIT_MODE_EN (1 << 4)
41 #define LDB_DATA_WIDTH_CH0_24 (1 << 5)
42 #define LDB_BIT_MAP_CH0_JEIDA (1 << 6)
43 #define LDB_DATA_WIDTH_CH1_24 (1 << 7)
44 #define LDB_BIT_MAP_CH1_JEIDA (1 << 8)
45 #define LDB_DI0_VS_POL_ACT_LOW (1 << 9)
46 #define LDB_DI1_VS_POL_ACT_LOW (1 << 10)
47 #define LDB_BGREF_RMODE_INT (1 << 15)
49 #define con_to_imx_ldb_ch(x) container_of(x, struct imx_ldb_channel, connector)
50 #define enc_to_imx_ldb_ch(x) container_of(x, struct imx_ldb_channel, encoder)
54 struct imx_ldb_channel
{
56 struct drm_connector connector
;
57 struct drm_encoder encoder
;
58 struct device_node
*child
;
62 struct drm_display_mode mode
;
73 struct regmap
*regmap
;
75 struct imx_ldb_channel channel
[2];
76 struct clk
*clk
[2]; /* our own clock */
77 struct clk
*clk_sel
[4]; /* parent of display clock */
78 struct clk
*clk_pll
[2]; /* upstream clock we can adjust */
80 const struct bus_mux
*lvds_mux
;
83 static enum drm_connector_status
imx_ldb_connector_detect(
84 struct drm_connector
*connector
, bool force
)
86 return connector_status_connected
;
89 static int imx_ldb_connector_get_modes(struct drm_connector
*connector
)
91 struct imx_ldb_channel
*imx_ldb_ch
= con_to_imx_ldb_ch(connector
);
94 if (imx_ldb_ch
->edid
) {
95 drm_mode_connector_update_edid_property(connector
,
97 num_modes
= drm_add_edid_modes(connector
, imx_ldb_ch
->edid
);
100 if (imx_ldb_ch
->mode_valid
) {
101 struct drm_display_mode
*mode
;
103 mode
= drm_mode_create(connector
->dev
);
106 drm_mode_copy(mode
, &imx_ldb_ch
->mode
);
107 mode
->type
|= DRM_MODE_TYPE_DRIVER
| DRM_MODE_TYPE_PREFERRED
;
108 drm_mode_probed_add(connector
, mode
);
115 static struct drm_encoder
*imx_ldb_connector_best_encoder(
116 struct drm_connector
*connector
)
118 struct imx_ldb_channel
*imx_ldb_ch
= con_to_imx_ldb_ch(connector
);
120 return &imx_ldb_ch
->encoder
;
123 static void imx_ldb_encoder_dpms(struct drm_encoder
*encoder
, int mode
)
127 static bool imx_ldb_encoder_mode_fixup(struct drm_encoder
*encoder
,
128 const struct drm_display_mode
*mode
,
129 struct drm_display_mode
*adjusted_mode
)
134 static void imx_ldb_set_clock(struct imx_ldb
*ldb
, int mux
, int chno
,
135 unsigned long serial_clk
, unsigned long di_clk
)
139 dev_dbg(ldb
->dev
, "%s: now: %ld want: %ld\n", __func__
,
140 clk_get_rate(ldb
->clk_pll
[chno
]), serial_clk
);
141 clk_set_rate(ldb
->clk_pll
[chno
], serial_clk
);
143 dev_dbg(ldb
->dev
, "%s after: %ld\n", __func__
,
144 clk_get_rate(ldb
->clk_pll
[chno
]));
146 dev_dbg(ldb
->dev
, "%s: now: %ld want: %ld\n", __func__
,
147 clk_get_rate(ldb
->clk
[chno
]),
149 clk_set_rate(ldb
->clk
[chno
], di_clk
);
151 dev_dbg(ldb
->dev
, "%s after: %ld\n", __func__
,
152 clk_get_rate(ldb
->clk
[chno
]));
154 /* set display clock mux to LDB input clock */
155 ret
= clk_set_parent(ldb
->clk_sel
[mux
], ldb
->clk
[chno
]);
158 "unable to set di%d parent clock to ldb_di%d\n", mux
,
162 static void imx_ldb_encoder_prepare(struct drm_encoder
*encoder
)
164 struct imx_ldb_channel
*imx_ldb_ch
= enc_to_imx_ldb_ch(encoder
);
165 struct imx_ldb
*ldb
= imx_ldb_ch
->ldb
;
168 switch (imx_ldb_ch
->chno
) {
170 pixel_fmt
= (ldb
->ldb_ctrl
& LDB_DATA_WIDTH_CH0_24
) ?
171 V4L2_PIX_FMT_RGB24
: V4L2_PIX_FMT_BGR666
;
174 pixel_fmt
= (ldb
->ldb_ctrl
& LDB_DATA_WIDTH_CH1_24
) ?
175 V4L2_PIX_FMT_RGB24
: V4L2_PIX_FMT_BGR666
;
178 dev_err(ldb
->dev
, "unable to config di%d panel format\n",
180 pixel_fmt
= V4L2_PIX_FMT_RGB24
;
183 imx_drm_panel_format(encoder
, pixel_fmt
);
186 static void imx_ldb_encoder_commit(struct drm_encoder
*encoder
)
188 struct imx_ldb_channel
*imx_ldb_ch
= enc_to_imx_ldb_ch(encoder
);
189 struct imx_ldb
*ldb
= imx_ldb_ch
->ldb
;
190 int dual
= ldb
->ldb_ctrl
& LDB_SPLIT_MODE_EN
;
191 int mux
= imx_drm_encoder_get_mux_id(imx_ldb_ch
->child
, encoder
);
194 clk_prepare_enable(ldb
->clk
[0]);
195 clk_prepare_enable(ldb
->clk
[1]);
198 if (imx_ldb_ch
== &ldb
->channel
[0] || dual
) {
199 ldb
->ldb_ctrl
&= ~LDB_CH0_MODE_EN_MASK
;
200 if (mux
== 0 || ldb
->lvds_mux
)
201 ldb
->ldb_ctrl
|= LDB_CH0_MODE_EN_TO_DI0
;
203 ldb
->ldb_ctrl
|= LDB_CH0_MODE_EN_TO_DI1
;
205 if (imx_ldb_ch
== &ldb
->channel
[1] || dual
) {
206 ldb
->ldb_ctrl
&= ~LDB_CH1_MODE_EN_MASK
;
207 if (mux
== 1 || ldb
->lvds_mux
)
208 ldb
->ldb_ctrl
|= LDB_CH1_MODE_EN_TO_DI1
;
210 ldb
->ldb_ctrl
|= LDB_CH1_MODE_EN_TO_DI0
;
214 const struct bus_mux
*lvds_mux
= NULL
;
216 if (imx_ldb_ch
== &ldb
->channel
[0])
217 lvds_mux
= &ldb
->lvds_mux
[0];
218 else if (imx_ldb_ch
== &ldb
->channel
[1])
219 lvds_mux
= &ldb
->lvds_mux
[1];
221 regmap_update_bits(ldb
->regmap
, lvds_mux
->reg
, lvds_mux
->mask
,
222 mux
<< lvds_mux
->shift
);
225 regmap_write(ldb
->regmap
, IOMUXC_GPR2
, ldb
->ldb_ctrl
);
228 static void imx_ldb_encoder_mode_set(struct drm_encoder
*encoder
,
229 struct drm_display_mode
*orig_mode
,
230 struct drm_display_mode
*mode
)
232 struct imx_ldb_channel
*imx_ldb_ch
= enc_to_imx_ldb_ch(encoder
);
233 struct imx_ldb
*ldb
= imx_ldb_ch
->ldb
;
234 int dual
= ldb
->ldb_ctrl
& LDB_SPLIT_MODE_EN
;
235 unsigned long serial_clk
;
236 unsigned long di_clk
= mode
->clock
* 1000;
237 int mux
= imx_drm_encoder_get_mux_id(imx_ldb_ch
->child
, encoder
);
239 if (mode
->clock
> 170000) {
241 "%s: mode exceeds 170 MHz pixel clock\n", __func__
);
243 if (mode
->clock
> 85000 && !dual
) {
245 "%s: mode exceeds 85 MHz pixel clock\n", __func__
);
249 serial_clk
= 3500UL * mode
->clock
;
250 imx_ldb_set_clock(ldb
, mux
, 0, serial_clk
, di_clk
);
251 imx_ldb_set_clock(ldb
, mux
, 1, serial_clk
, di_clk
);
253 serial_clk
= 7000UL * mode
->clock
;
254 imx_ldb_set_clock(ldb
, mux
, imx_ldb_ch
->chno
, serial_clk
,
258 /* FIXME - assumes straight connections DI0 --> CH0, DI1 --> CH1 */
259 if (imx_ldb_ch
== &ldb
->channel
[0]) {
260 if (mode
->flags
& DRM_MODE_FLAG_NVSYNC
)
261 ldb
->ldb_ctrl
|= LDB_DI0_VS_POL_ACT_LOW
;
262 else if (mode
->flags
& DRM_MODE_FLAG_PVSYNC
)
263 ldb
->ldb_ctrl
&= ~LDB_DI0_VS_POL_ACT_LOW
;
265 if (imx_ldb_ch
== &ldb
->channel
[1]) {
266 if (mode
->flags
& DRM_MODE_FLAG_NVSYNC
)
267 ldb
->ldb_ctrl
|= LDB_DI1_VS_POL_ACT_LOW
;
268 else if (mode
->flags
& DRM_MODE_FLAG_PVSYNC
)
269 ldb
->ldb_ctrl
&= ~LDB_DI1_VS_POL_ACT_LOW
;
273 static void imx_ldb_encoder_disable(struct drm_encoder
*encoder
)
275 struct imx_ldb_channel
*imx_ldb_ch
= enc_to_imx_ldb_ch(encoder
);
276 struct imx_ldb
*ldb
= imx_ldb_ch
->ldb
;
279 * imx_ldb_encoder_disable is called by
280 * drm_helper_disable_unused_functions without
281 * the encoder being enabled before.
283 if (imx_ldb_ch
== &ldb
->channel
[0] &&
284 (ldb
->ldb_ctrl
& LDB_CH0_MODE_EN_MASK
) == 0)
286 else if (imx_ldb_ch
== &ldb
->channel
[1] &&
287 (ldb
->ldb_ctrl
& LDB_CH1_MODE_EN_MASK
) == 0)
290 if (imx_ldb_ch
== &ldb
->channel
[0])
291 ldb
->ldb_ctrl
&= ~LDB_CH0_MODE_EN_MASK
;
292 else if (imx_ldb_ch
== &ldb
->channel
[1])
293 ldb
->ldb_ctrl
&= ~LDB_CH1_MODE_EN_MASK
;
295 regmap_write(ldb
->regmap
, IOMUXC_GPR2
, ldb
->ldb_ctrl
);
297 if (ldb
->ldb_ctrl
& LDB_SPLIT_MODE_EN
) {
298 clk_disable_unprepare(ldb
->clk
[0]);
299 clk_disable_unprepare(ldb
->clk
[1]);
303 static struct drm_connector_funcs imx_ldb_connector_funcs
= {
304 .dpms
= drm_helper_connector_dpms
,
305 .fill_modes
= drm_helper_probe_single_connector_modes
,
306 .detect
= imx_ldb_connector_detect
,
307 .destroy
= imx_drm_connector_destroy
,
310 static struct drm_connector_helper_funcs imx_ldb_connector_helper_funcs
= {
311 .get_modes
= imx_ldb_connector_get_modes
,
312 .best_encoder
= imx_ldb_connector_best_encoder
,
315 static struct drm_encoder_funcs imx_ldb_encoder_funcs
= {
316 .destroy
= imx_drm_encoder_destroy
,
319 static struct drm_encoder_helper_funcs imx_ldb_encoder_helper_funcs
= {
320 .dpms
= imx_ldb_encoder_dpms
,
321 .mode_fixup
= imx_ldb_encoder_mode_fixup
,
322 .prepare
= imx_ldb_encoder_prepare
,
323 .commit
= imx_ldb_encoder_commit
,
324 .mode_set
= imx_ldb_encoder_mode_set
,
325 .disable
= imx_ldb_encoder_disable
,
328 static int imx_ldb_get_clk(struct imx_ldb
*ldb
, int chno
)
332 snprintf(clkname
, sizeof(clkname
), "di%d", chno
);
333 ldb
->clk
[chno
] = devm_clk_get(ldb
->dev
, clkname
);
334 if (IS_ERR(ldb
->clk
[chno
]))
335 return PTR_ERR(ldb
->clk
[chno
]);
337 snprintf(clkname
, sizeof(clkname
), "di%d_pll", chno
);
338 ldb
->clk_pll
[chno
] = devm_clk_get(ldb
->dev
, clkname
);
340 return PTR_ERR_OR_ZERO(ldb
->clk_pll
[chno
]);
343 static int imx_ldb_register(struct drm_device
*drm
,
344 struct imx_ldb_channel
*imx_ldb_ch
)
346 struct imx_ldb
*ldb
= imx_ldb_ch
->ldb
;
349 ret
= imx_drm_encoder_parse_of(drm
, &imx_ldb_ch
->encoder
,
354 ret
= imx_ldb_get_clk(ldb
, imx_ldb_ch
->chno
);
358 if (ldb
->ldb_ctrl
& LDB_SPLIT_MODE_EN
) {
359 ret
= imx_ldb_get_clk(ldb
, 1);
364 drm_encoder_helper_add(&imx_ldb_ch
->encoder
,
365 &imx_ldb_encoder_helper_funcs
);
366 drm_encoder_init(drm
, &imx_ldb_ch
->encoder
, &imx_ldb_encoder_funcs
,
367 DRM_MODE_ENCODER_LVDS
);
369 drm_connector_helper_add(&imx_ldb_ch
->connector
,
370 &imx_ldb_connector_helper_funcs
);
371 drm_connector_init(drm
, &imx_ldb_ch
->connector
,
372 &imx_ldb_connector_funcs
, DRM_MODE_CONNECTOR_LVDS
);
374 drm_mode_connector_attach_encoder(&imx_ldb_ch
->connector
,
375 &imx_ldb_ch
->encoder
);
385 static const char * const imx_ldb_bit_mappings
[] = {
386 [LVDS_BIT_MAP_SPWG
] = "spwg",
387 [LVDS_BIT_MAP_JEIDA
] = "jeida",
390 static const int of_get_data_mapping(struct device_node
*np
)
395 ret
= of_property_read_string(np
, "fsl,data-mapping", &bm
);
399 for (i
= 0; i
< ARRAY_SIZE(imx_ldb_bit_mappings
); i
++)
400 if (!strcasecmp(bm
, imx_ldb_bit_mappings
[i
]))
406 static struct bus_mux imx6q_lvds_mux
[2] = {
410 .mask
= IMX6Q_GPR3_LVDS0_MUX_CTL_MASK
,
414 .mask
= IMX6Q_GPR3_LVDS1_MUX_CTL_MASK
,
419 * For a device declaring compatible = "fsl,imx6q-ldb", "fsl,imx53-ldb",
420 * of_match_device will walk through this list and take the first entry
421 * matching any of its compatible values. Therefore, the more generic
422 * entries (in this case fsl,imx53-ldb) need to be ordered last.
424 static const struct of_device_id imx_ldb_dt_ids
[] = {
425 { .compatible
= "fsl,imx6q-ldb", .data
= imx6q_lvds_mux
, },
426 { .compatible
= "fsl,imx53-ldb", .data
= NULL
, },
429 MODULE_DEVICE_TABLE(of
, imx_ldb_dt_ids
);
431 static int imx_ldb_bind(struct device
*dev
, struct device
*master
, void *data
)
433 struct drm_device
*drm
= data
;
434 struct device_node
*np
= dev
->of_node
;
435 const struct of_device_id
*of_id
=
436 of_match_device(imx_ldb_dt_ids
, dev
);
437 struct device_node
*child
;
439 struct imx_ldb
*imx_ldb
;
446 imx_ldb
= devm_kzalloc(dev
, sizeof(*imx_ldb
), GFP_KERNEL
);
450 imx_ldb
->regmap
= syscon_regmap_lookup_by_phandle(np
, "gpr");
451 if (IS_ERR(imx_ldb
->regmap
)) {
452 dev_err(dev
, "failed to get parent regmap\n");
453 return PTR_ERR(imx_ldb
->regmap
);
459 imx_ldb
->lvds_mux
= of_id
->data
;
461 dual
= of_property_read_bool(np
, "fsl,dual-channel");
463 imx_ldb
->ldb_ctrl
|= LDB_SPLIT_MODE_EN
;
466 * There are three different possible clock mux configurations:
467 * i.MX53: ipu1_di0_sel, ipu1_di1_sel
468 * i.MX6q: ipu1_di0_sel, ipu1_di1_sel, ipu2_di0_sel, ipu2_di1_sel
469 * i.MX6dl: ipu1_di0_sel, ipu1_di1_sel, lcdif_sel
470 * Map them all to di0_sel...di3_sel.
472 for (i
= 0; i
< 4; i
++) {
475 sprintf(clkname
, "di%d_sel", i
);
476 imx_ldb
->clk_sel
[i
] = devm_clk_get(imx_ldb
->dev
, clkname
);
477 if (IS_ERR(imx_ldb
->clk_sel
[i
])) {
478 ret
= PTR_ERR(imx_ldb
->clk_sel
[i
]);
479 imx_ldb
->clk_sel
[i
] = NULL
;
486 for_each_child_of_node(np
, child
) {
487 struct imx_ldb_channel
*channel
;
489 ret
= of_property_read_u32(child
, "reg", &i
);
490 if (ret
|| i
< 0 || i
> 1)
494 dev_warn(dev
, "dual-channel mode, ignoring second output\n");
498 if (!of_device_is_available(child
))
501 channel
= &imx_ldb
->channel
[i
];
502 channel
->ldb
= imx_ldb
;
504 channel
->child
= child
;
506 edidp
= of_get_property(child
, "edid", &channel
->edid_len
);
508 channel
->edid
= kmemdup(edidp
, channel
->edid_len
,
511 ret
= of_get_drm_display_mode(child
, &channel
->mode
, 0);
513 channel
->mode_valid
= 1;
516 ret
= of_property_read_u32(child
, "fsl,data-width", &datawidth
);
519 else if (datawidth
!= 18 && datawidth
!= 24)
522 mapping
= of_get_data_mapping(child
);
524 case LVDS_BIT_MAP_SPWG
:
525 if (datawidth
== 24) {
528 LDB_DATA_WIDTH_CH0_24
;
531 LDB_DATA_WIDTH_CH1_24
;
534 case LVDS_BIT_MAP_JEIDA
:
535 if (datawidth
== 18) {
536 dev_err(dev
, "JEIDA standard only supported in 24 bit\n");
540 imx_ldb
->ldb_ctrl
|= LDB_DATA_WIDTH_CH0_24
|
541 LDB_BIT_MAP_CH0_JEIDA
;
543 imx_ldb
->ldb_ctrl
|= LDB_DATA_WIDTH_CH1_24
|
544 LDB_BIT_MAP_CH1_JEIDA
;
547 dev_err(dev
, "data mapping not specified or invalid\n");
551 ret
= imx_ldb_register(drm
, channel
);
556 dev_set_drvdata(dev
, imx_ldb
);
561 static void imx_ldb_unbind(struct device
*dev
, struct device
*master
,
564 struct imx_ldb
*imx_ldb
= dev_get_drvdata(dev
);
567 for (i
= 0; i
< 2; i
++) {
568 struct imx_ldb_channel
*channel
= &imx_ldb
->channel
[i
];
570 if (!channel
->connector
.funcs
)
573 channel
->connector
.funcs
->destroy(&channel
->connector
);
574 channel
->encoder
.funcs
->destroy(&channel
->encoder
);
576 kfree(channel
->edid
);
580 static const struct component_ops imx_ldb_ops
= {
581 .bind
= imx_ldb_bind
,
582 .unbind
= imx_ldb_unbind
,
585 static int imx_ldb_probe(struct platform_device
*pdev
)
587 return component_add(&pdev
->dev
, &imx_ldb_ops
);
590 static int imx_ldb_remove(struct platform_device
*pdev
)
592 component_del(&pdev
->dev
, &imx_ldb_ops
);
596 static struct platform_driver imx_ldb_driver
= {
597 .probe
= imx_ldb_probe
,
598 .remove
= imx_ldb_remove
,
600 .of_match_table
= imx_ldb_dt_ids
,
605 module_platform_driver(imx_ldb_driver
);
607 MODULE_DESCRIPTION("i.MX LVDS driver");
608 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
609 MODULE_LICENSE("GPL");
610 MODULE_ALIAS("platform:" DRIVER_NAME
);