1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2016 Broadcom Limited
9 * The VC4 DPI hardware supports MIPI DPI type 4 and Nokia ViSSI
10 * signals. On BCM2835, these can be routed out to GPIO0-27 with the
14 #include <drm/drm_atomic_helper.h>
15 #include <drm/drm_bridge.h>
16 #include <drm/drm_edid.h>
17 #include <drm/drm_of.h>
18 #include <drm/drm_panel.h>
19 #include <drm/drm_probe_helper.h>
20 #include <drm/drm_simple_kms_helper.h>
21 #include <linux/clk.h>
22 #include <linux/component.h>
23 #include <linux/of_graph.h>
24 #include <linux/of_platform.h>
29 # define DPI_OUTPUT_ENABLE_MODE BIT(16)
31 /* The order field takes the incoming 24 bit RGB from the pixel valve
32 * and shuffles the 3 channels.
34 # define DPI_ORDER_MASK VC4_MASK(15, 14)
35 # define DPI_ORDER_SHIFT 14
36 # define DPI_ORDER_RGB 0
37 # define DPI_ORDER_BGR 1
38 # define DPI_ORDER_GRB 2
39 # define DPI_ORDER_BRG 3
41 /* The format field takes the ORDER-shuffled pixel valve data and
42 * formats it onto the output lines.
44 # define DPI_FORMAT_MASK VC4_MASK(13, 11)
45 # define DPI_FORMAT_SHIFT 11
46 /* This define is named in the hardware, but actually just outputs 0. */
47 # define DPI_FORMAT_9BIT_666_RGB 0
48 /* Outputs 00000000rrrrrggggggbbbbb */
49 # define DPI_FORMAT_16BIT_565_RGB_1 1
50 /* Outputs 000rrrrr00gggggg000bbbbb */
51 # define DPI_FORMAT_16BIT_565_RGB_2 2
52 /* Outputs 00rrrrr000gggggg00bbbbb0 */
53 # define DPI_FORMAT_16BIT_565_RGB_3 3
54 /* Outputs 000000rrrrrrggggggbbbbbb */
55 # define DPI_FORMAT_18BIT_666_RGB_1 4
56 /* Outputs 00rrrrrr00gggggg00bbbbbb */
57 # define DPI_FORMAT_18BIT_666_RGB_2 5
58 /* Outputs rrrrrrrrggggggggbbbbbbbb */
59 # define DPI_FORMAT_24BIT_888_RGB 6
61 /* Reverses the polarity of the corresponding signal */
62 # define DPI_PIXEL_CLK_INVERT BIT(10)
63 # define DPI_HSYNC_INVERT BIT(9)
64 # define DPI_VSYNC_INVERT BIT(8)
65 # define DPI_OUTPUT_ENABLE_INVERT BIT(7)
67 /* Outputs the signal the falling clock edge instead of rising. */
68 # define DPI_HSYNC_NEGATE BIT(6)
69 # define DPI_VSYNC_NEGATE BIT(5)
70 # define DPI_OUTPUT_ENABLE_NEGATE BIT(4)
72 /* Disables the signal */
73 # define DPI_HSYNC_DISABLE BIT(3)
74 # define DPI_VSYNC_DISABLE BIT(2)
75 # define DPI_OUTPUT_ENABLE_DISABLE BIT(1)
77 /* Power gate to the device, full reset at 0 -> 1 transition */
78 # define DPI_ENABLE BIT(0)
80 /* All other registers besides DPI_C return the ID */
82 # define DPI_ID_VALUE 0x00647069
84 /* General DPI hardware state. */
86 struct platform_device *pdev;
88 struct drm_encoder *encoder;
92 struct clk *pixel_clock;
93 struct clk *core_clock;
95 struct debugfs_regset32 regset;
98 #define DPI_READ(offset) readl(dpi->regs + (offset))
99 #define DPI_WRITE(offset, val) writel(val, dpi->regs + (offset))
101 /* VC4 DPI encoder KMS struct */
102 struct vc4_dpi_encoder {
103 struct vc4_encoder base;
107 static inline struct vc4_dpi_encoder *
108 to_vc4_dpi_encoder(struct drm_encoder *encoder)
110 return container_of(encoder, struct vc4_dpi_encoder, base.base);
113 static const struct debugfs_reg32 dpi_regs[] = {
118 static void vc4_dpi_encoder_disable(struct drm_encoder *encoder)
120 struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder);
121 struct vc4_dpi *dpi = vc4_encoder->dpi;
123 clk_disable_unprepare(dpi->pixel_clock);
126 static void vc4_dpi_encoder_enable(struct drm_encoder *encoder)
128 struct drm_device *dev = encoder->dev;
129 struct drm_display_mode *mode = &encoder->crtc->mode;
130 struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder);
131 struct vc4_dpi *dpi = vc4_encoder->dpi;
132 struct drm_connector_list_iter conn_iter;
133 struct drm_connector *connector = NULL, *connector_scan;
134 u32 dpi_c = DPI_ENABLE | DPI_OUTPUT_ENABLE_MODE;
137 /* Look up the connector attached to DPI so we can get the
138 * bus_format. Ideally the bridge would tell us the
139 * bus_format we want, but it doesn't yet, so assume that it's
140 * uniform throughout the bridge chain.
142 drm_connector_list_iter_begin(dev, &conn_iter);
143 drm_for_each_connector_iter(connector_scan, &conn_iter) {
144 if (connector_scan->encoder == encoder) {
145 connector = connector_scan;
149 drm_connector_list_iter_end(&conn_iter);
151 if (connector && connector->display_info.num_bus_formats) {
152 u32 bus_format = connector->display_info.bus_formats[0];
154 switch (bus_format) {
155 case MEDIA_BUS_FMT_RGB888_1X24:
156 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
159 case MEDIA_BUS_FMT_BGR888_1X24:
160 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
162 dpi_c |= VC4_SET_FIELD(DPI_ORDER_BGR, DPI_ORDER);
164 case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
165 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_2,
168 case MEDIA_BUS_FMT_RGB666_1X18:
169 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_1,
172 case MEDIA_BUS_FMT_RGB565_1X16:
173 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_16BIT_565_RGB_3,
177 DRM_ERROR("Unknown media bus format %d\n", bus_format);
181 /* Default to 24bit if no connector found. */
182 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB, DPI_FORMAT);
185 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
186 dpi_c |= DPI_HSYNC_INVERT;
187 else if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
188 dpi_c |= DPI_HSYNC_DISABLE;
190 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
191 dpi_c |= DPI_VSYNC_INVERT;
192 else if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
193 dpi_c |= DPI_VSYNC_DISABLE;
195 DPI_WRITE(DPI_C, dpi_c);
197 ret = clk_set_rate(dpi->pixel_clock, mode->clock * 1000);
199 DRM_ERROR("Failed to set clock rate: %d\n", ret);
201 ret = clk_prepare_enable(dpi->pixel_clock);
203 DRM_ERROR("Failed to set clock rate: %d\n", ret);
206 static enum drm_mode_status vc4_dpi_encoder_mode_valid(struct drm_encoder *encoder,
207 const struct drm_display_mode *mode)
209 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
210 return MODE_NO_INTERLACE;
215 static const struct drm_encoder_helper_funcs vc4_dpi_encoder_helper_funcs = {
216 .disable = vc4_dpi_encoder_disable,
217 .enable = vc4_dpi_encoder_enable,
218 .mode_valid = vc4_dpi_encoder_mode_valid,
221 static const struct of_device_id vc4_dpi_dt_match[] = {
222 { .compatible = "brcm,bcm2835-dpi", .data = NULL },
226 /* Sets up the next link in the display chain, whether it's a panel or
229 static int vc4_dpi_init_bridge(struct vc4_dpi *dpi)
231 struct device *dev = &dpi->pdev->dev;
232 struct drm_bridge *bridge;
234 bridge = devm_drm_of_get_bridge(dev, dev->of_node, 0, 0);
235 if (IS_ERR(bridge)) {
236 /* If nothing was connected in the DT, that's not an
239 if (PTR_ERR(bridge) == -ENODEV)
242 return PTR_ERR(bridge);
245 return drm_bridge_attach(dpi->encoder, bridge, NULL, 0);
248 static int vc4_dpi_bind(struct device *dev, struct device *master, void *data)
250 struct platform_device *pdev = to_platform_device(dev);
251 struct drm_device *drm = dev_get_drvdata(master);
252 struct vc4_dev *vc4 = to_vc4_dev(drm);
254 struct vc4_dpi_encoder *vc4_dpi_encoder;
257 dpi = devm_kzalloc(dev, sizeof(*dpi), GFP_KERNEL);
261 vc4_dpi_encoder = devm_kzalloc(dev, sizeof(*vc4_dpi_encoder),
263 if (!vc4_dpi_encoder)
265 vc4_dpi_encoder->base.type = VC4_ENCODER_TYPE_DPI;
266 vc4_dpi_encoder->dpi = dpi;
267 dpi->encoder = &vc4_dpi_encoder->base.base;
270 dpi->regs = vc4_ioremap_regs(pdev, 0);
271 if (IS_ERR(dpi->regs))
272 return PTR_ERR(dpi->regs);
273 dpi->regset.base = dpi->regs;
274 dpi->regset.regs = dpi_regs;
275 dpi->regset.nregs = ARRAY_SIZE(dpi_regs);
277 if (DPI_READ(DPI_ID) != DPI_ID_VALUE) {
278 dev_err(dev, "Port returned 0x%08x for ID instead of 0x%08x\n",
279 DPI_READ(DPI_ID), DPI_ID_VALUE);
283 dpi->core_clock = devm_clk_get(dev, "core");
284 if (IS_ERR(dpi->core_clock)) {
285 ret = PTR_ERR(dpi->core_clock);
286 if (ret != -EPROBE_DEFER)
287 DRM_ERROR("Failed to get core clock: %d\n", ret);
290 dpi->pixel_clock = devm_clk_get(dev, "pixel");
291 if (IS_ERR(dpi->pixel_clock)) {
292 ret = PTR_ERR(dpi->pixel_clock);
293 if (ret != -EPROBE_DEFER)
294 DRM_ERROR("Failed to get pixel clock: %d\n", ret);
298 ret = clk_prepare_enable(dpi->core_clock);
300 DRM_ERROR("Failed to turn on core clock: %d\n", ret);
302 drm_simple_encoder_init(drm, dpi->encoder, DRM_MODE_ENCODER_DPI);
303 drm_encoder_helper_add(dpi->encoder, &vc4_dpi_encoder_helper_funcs);
305 ret = vc4_dpi_init_bridge(dpi);
307 goto err_destroy_encoder;
309 dev_set_drvdata(dev, dpi);
313 vc4_debugfs_add_regset32(drm, "dpi_regs", &dpi->regset);
318 drm_encoder_cleanup(dpi->encoder);
319 clk_disable_unprepare(dpi->core_clock);
323 static void vc4_dpi_unbind(struct device *dev, struct device *master,
326 struct drm_device *drm = dev_get_drvdata(master);
327 struct vc4_dev *vc4 = to_vc4_dev(drm);
328 struct vc4_dpi *dpi = dev_get_drvdata(dev);
330 drm_of_panel_bridge_remove(dev->of_node, 0, 0);
332 drm_encoder_cleanup(dpi->encoder);
334 clk_disable_unprepare(dpi->core_clock);
339 static const struct component_ops vc4_dpi_ops = {
340 .bind = vc4_dpi_bind,
341 .unbind = vc4_dpi_unbind,
344 static int vc4_dpi_dev_probe(struct platform_device *pdev)
346 return component_add(&pdev->dev, &vc4_dpi_ops);
349 static int vc4_dpi_dev_remove(struct platform_device *pdev)
351 component_del(&pdev->dev, &vc4_dpi_ops);
355 struct platform_driver vc4_dpi_driver = {
356 .probe = vc4_dpi_dev_probe,
357 .remove = vc4_dpi_dev_remove,
360 .of_match_table = vc4_dpi_dt_match,