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_drv.h>
17 #include <drm/drm_edid.h>
18 #include <drm/drm_of.h>
19 #include <drm/drm_panel.h>
20 #include <drm/drm_probe_helper.h>
21 #include <drm/drm_simple_kms_helper.h>
22 #include <linux/clk.h>
23 #include <linux/component.h>
24 #include <linux/of_graph.h>
25 #include <linux/of_platform.h>
30 # define DPI_OUTPUT_ENABLE_MODE BIT(16)
32 /* The order field takes the incoming 24 bit RGB from the pixel valve
33 * and shuffles the 3 channels.
35 # define DPI_ORDER_MASK VC4_MASK(15, 14)
36 # define DPI_ORDER_SHIFT 14
37 # define DPI_ORDER_RGB 0
38 # define DPI_ORDER_BGR 1
39 # define DPI_ORDER_GRB 2
40 # define DPI_ORDER_BRG 3
42 /* The format field takes the ORDER-shuffled pixel valve data and
43 * formats it onto the output lines.
45 # define DPI_FORMAT_MASK VC4_MASK(13, 11)
46 # define DPI_FORMAT_SHIFT 11
47 /* This define is named in the hardware, but actually just outputs 0. */
48 # define DPI_FORMAT_9BIT_666_RGB 0
49 /* Outputs 00000000rrrrrggggggbbbbb */
50 # define DPI_FORMAT_16BIT_565_RGB_1 1
51 /* Outputs 000rrrrr00gggggg000bbbbb */
52 # define DPI_FORMAT_16BIT_565_RGB_2 2
53 /* Outputs 00rrrrr000gggggg00bbbbb0 */
54 # define DPI_FORMAT_16BIT_565_RGB_3 3
55 /* Outputs 000000rrrrrrggggggbbbbbb */
56 # define DPI_FORMAT_18BIT_666_RGB_1 4
57 /* Outputs 00rrrrrr00gggggg00bbbbbb */
58 # define DPI_FORMAT_18BIT_666_RGB_2 5
59 /* Outputs rrrrrrrrggggggggbbbbbbbb */
60 # define DPI_FORMAT_24BIT_888_RGB 6
62 /* Reverses the polarity of the corresponding signal */
63 # define DPI_PIXEL_CLK_INVERT BIT(10)
64 # define DPI_HSYNC_INVERT BIT(9)
65 # define DPI_VSYNC_INVERT BIT(8)
66 # define DPI_OUTPUT_ENABLE_INVERT BIT(7)
68 /* Outputs the signal the falling clock edge instead of rising. */
69 # define DPI_HSYNC_NEGATE BIT(6)
70 # define DPI_VSYNC_NEGATE BIT(5)
71 # define DPI_OUTPUT_ENABLE_NEGATE BIT(4)
73 /* Disables the signal */
74 # define DPI_HSYNC_DISABLE BIT(3)
75 # define DPI_VSYNC_DISABLE BIT(2)
76 # define DPI_OUTPUT_ENABLE_DISABLE BIT(1)
78 /* Power gate to the device, full reset at 0 -> 1 transition */
79 # define DPI_ENABLE BIT(0)
81 /* All other registers besides DPI_C return the ID */
83 # define DPI_ID_VALUE 0x00647069
85 /* General DPI hardware state. */
87 struct vc4_encoder encoder;
89 struct platform_device *pdev;
93 struct clk *pixel_clock;
94 struct clk *core_clock;
96 struct debugfs_regset32 regset;
99 static inline struct vc4_dpi *
100 to_vc4_dpi(struct drm_encoder *encoder)
102 return container_of(encoder, struct vc4_dpi, encoder.base);
105 #define DPI_READ(offset) readl(dpi->regs + (offset))
106 #define DPI_WRITE(offset, val) writel(val, dpi->regs + (offset))
108 static const struct debugfs_reg32 dpi_regs[] = {
113 static void vc4_dpi_encoder_disable(struct drm_encoder *encoder)
115 struct drm_device *dev = encoder->dev;
116 struct vc4_dpi *dpi = to_vc4_dpi(encoder);
119 if (!drm_dev_enter(dev, &idx))
122 clk_disable_unprepare(dpi->pixel_clock);
127 static void vc4_dpi_encoder_enable(struct drm_encoder *encoder)
129 struct drm_device *dev = encoder->dev;
130 struct drm_display_mode *mode = &encoder->crtc->mode;
131 struct vc4_dpi *dpi = to_vc4_dpi(encoder);
132 struct drm_connector_list_iter conn_iter;
133 struct drm_connector *connector = NULL, *connector_scan;
134 u32 dpi_c = DPI_ENABLE;
138 /* Look up the connector attached to DPI so we can get the
139 * bus_format. Ideally the bridge would tell us the
140 * bus_format we want, but it doesn't yet, so assume that it's
141 * uniform throughout the bridge chain.
143 drm_connector_list_iter_begin(dev, &conn_iter);
144 drm_for_each_connector_iter(connector_scan, &conn_iter) {
145 if (connector_scan->encoder == encoder) {
146 connector = connector_scan;
150 drm_connector_list_iter_end(&conn_iter);
153 if (connector->display_info.num_bus_formats) {
154 u32 bus_format = connector->display_info.bus_formats[0];
156 switch (bus_format) {
157 case MEDIA_BUS_FMT_RGB888_1X24:
158 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
161 case MEDIA_BUS_FMT_BGR888_1X24:
162 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
164 dpi_c |= VC4_SET_FIELD(DPI_ORDER_BGR,
167 case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
168 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_2,
171 case MEDIA_BUS_FMT_BGR666_1X24_CPADHI:
172 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_2,
174 dpi_c |= VC4_SET_FIELD(DPI_ORDER_BGR,
178 DRM_ERROR("Unknown media bus format %d\n",
181 case MEDIA_BUS_FMT_RGB666_1X18:
182 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_1,
185 case MEDIA_BUS_FMT_BGR666_1X18:
186 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_1,
188 dpi_c |= VC4_SET_FIELD(DPI_ORDER_BGR,
191 case MEDIA_BUS_FMT_RGB565_1X16:
192 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_16BIT_565_RGB_1,
195 case MEDIA_BUS_FMT_RGB565_1X24_CPADHI:
196 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_16BIT_565_RGB_2,
201 /* Default to 18bit if no connector found. */
202 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_1,
207 if (connector->display_info.bus_flags &
208 DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE)
209 dpi_c |= DPI_PIXEL_CLK_INVERT;
211 if (connector->display_info.bus_flags & DRM_BUS_FLAG_DE_LOW)
212 dpi_c |= DPI_OUTPUT_ENABLE_INVERT;
214 /* Default to 18bit if no connector found. */
215 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_1, DPI_FORMAT);
218 if (mode->flags & DRM_MODE_FLAG_CSYNC) {
219 if (mode->flags & DRM_MODE_FLAG_NCSYNC)
220 dpi_c |= DPI_OUTPUT_ENABLE_INVERT;
222 dpi_c |= DPI_OUTPUT_ENABLE_MODE;
224 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
225 dpi_c |= DPI_HSYNC_INVERT;
226 else if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
227 dpi_c |= DPI_HSYNC_DISABLE;
229 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
230 dpi_c |= DPI_VSYNC_INVERT;
231 else if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
232 dpi_c |= DPI_VSYNC_DISABLE;
235 if (!drm_dev_enter(dev, &idx))
238 DPI_WRITE(DPI_C, dpi_c);
240 ret = clk_set_rate(dpi->pixel_clock, mode->clock * 1000);
242 DRM_ERROR("Failed to set clock rate: %d\n", ret);
244 ret = clk_prepare_enable(dpi->pixel_clock);
246 DRM_ERROR("Failed to set clock rate: %d\n", ret);
251 static enum drm_mode_status vc4_dpi_encoder_mode_valid(struct drm_encoder *encoder,
252 const struct drm_display_mode *mode)
254 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
255 return MODE_NO_INTERLACE;
260 static const struct drm_encoder_helper_funcs vc4_dpi_encoder_helper_funcs = {
261 .disable = vc4_dpi_encoder_disable,
262 .enable = vc4_dpi_encoder_enable,
263 .mode_valid = vc4_dpi_encoder_mode_valid,
266 static int vc4_dpi_late_register(struct drm_encoder *encoder)
268 struct drm_device *drm = encoder->dev;
269 struct vc4_dpi *dpi = to_vc4_dpi(encoder);
272 ret = vc4_debugfs_add_regset32(drm->primary, "dpi_regs", &dpi->regset);
279 static const struct drm_encoder_funcs vc4_dpi_encoder_funcs = {
280 .late_register = vc4_dpi_late_register,
283 static const struct of_device_id vc4_dpi_dt_match[] = {
284 { .compatible = "brcm,bcm2835-dpi", .data = NULL },
288 /* Sets up the next link in the display chain, whether it's a panel or
291 static int vc4_dpi_init_bridge(struct vc4_dpi *dpi)
293 struct drm_device *drm = dpi->encoder.base.dev;
294 struct device *dev = &dpi->pdev->dev;
295 struct drm_bridge *bridge;
297 bridge = drmm_of_get_bridge(drm, dev->of_node, 0, 0);
298 if (IS_ERR(bridge)) {
299 /* If nothing was connected in the DT, that's not an
302 if (PTR_ERR(bridge) == -ENODEV)
305 return PTR_ERR(bridge);
308 return drm_bridge_attach(&dpi->encoder.base, bridge, NULL, 0);
311 static void vc4_dpi_disable_clock(void *ptr)
313 struct vc4_dpi *dpi = ptr;
315 clk_disable_unprepare(dpi->core_clock);
318 static int vc4_dpi_bind(struct device *dev, struct device *master, void *data)
320 struct platform_device *pdev = to_platform_device(dev);
321 struct drm_device *drm = dev_get_drvdata(master);
325 dpi = drmm_kzalloc(drm, sizeof(*dpi), GFP_KERNEL);
329 dpi->encoder.type = VC4_ENCODER_TYPE_DPI;
331 dpi->regs = vc4_ioremap_regs(pdev, 0);
332 if (IS_ERR(dpi->regs))
333 return PTR_ERR(dpi->regs);
334 dpi->regset.base = dpi->regs;
335 dpi->regset.regs = dpi_regs;
336 dpi->regset.nregs = ARRAY_SIZE(dpi_regs);
338 if (DPI_READ(DPI_ID) != DPI_ID_VALUE) {
339 dev_err(dev, "Port returned 0x%08x for ID instead of 0x%08x\n",
340 DPI_READ(DPI_ID), DPI_ID_VALUE);
344 dpi->core_clock = devm_clk_get(dev, "core");
345 if (IS_ERR(dpi->core_clock)) {
346 ret = PTR_ERR(dpi->core_clock);
347 if (ret != -EPROBE_DEFER)
348 DRM_ERROR("Failed to get core clock: %d\n", ret);
352 dpi->pixel_clock = devm_clk_get(dev, "pixel");
353 if (IS_ERR(dpi->pixel_clock)) {
354 ret = PTR_ERR(dpi->pixel_clock);
355 if (ret != -EPROBE_DEFER)
356 DRM_ERROR("Failed to get pixel clock: %d\n", ret);
360 ret = clk_prepare_enable(dpi->core_clock);
362 DRM_ERROR("Failed to turn on core clock: %d\n", ret);
366 ret = devm_add_action_or_reset(dev, vc4_dpi_disable_clock, dpi);
370 ret = drmm_encoder_init(drm, &dpi->encoder.base,
371 &vc4_dpi_encoder_funcs,
372 DRM_MODE_ENCODER_DPI,
377 drm_encoder_helper_add(&dpi->encoder.base, &vc4_dpi_encoder_helper_funcs);
379 ret = vc4_dpi_init_bridge(dpi);
383 dev_set_drvdata(dev, dpi);
388 static const struct component_ops vc4_dpi_ops = {
389 .bind = vc4_dpi_bind,
392 static int vc4_dpi_dev_probe(struct platform_device *pdev)
394 return component_add(&pdev->dev, &vc4_dpi_ops);
397 static int vc4_dpi_dev_remove(struct platform_device *pdev)
399 component_del(&pdev->dev, &vc4_dpi_ops);
403 struct platform_driver vc4_dpi_driver = {
404 .probe = vc4_dpi_dev_probe,
405 .remove = vc4_dpi_dev_remove,
408 .of_match_table = vc4_dpi_dt_match,