1 From f12e336d29983c2f56da38810600b6f57cc62371 Mon Sep 17 00:00:00 2001
2 From: Eric Anholt <eric@anholt.net>
3 Date: Wed, 10 Feb 2016 11:42:32 -0800
4 Subject: [PATCH 335/381] drm/vc4: Add DPI driver
6 The DPI interface involves taking a ton of our GPIOs to be used as
7 outputs, and routing display signals over them in parallel.
9 v2: Use display_info.bus_formats[] to replace our custom DT
11 v3: Rebase on V3D documentation changes.
12 v4: Fix rebase detritus from V3D documentation changes.
14 Signed-off-by: Eric Anholt <eric@anholt.net>
15 Acked-by: Rob Herring <robh@kernel.org>
16 (cherry picked from commit 08302c35b59d306ff37b996e56fb2a488c1d2c2e)
18 .../devicetree/bindings/display/brcm,bcm-vc4.txt | 36 ++
19 drivers/gpu/drm/vc4/Kconfig | 1 +
20 drivers/gpu/drm/vc4/Makefile | 1 +
21 drivers/gpu/drm/vc4/vc4_debugfs.c | 1 +
22 drivers/gpu/drm/vc4/vc4_dpi.c | 520 +++++++++++++++++++++
23 drivers/gpu/drm/vc4/vc4_drv.c | 1 +
24 drivers/gpu/drm/vc4/vc4_drv.h | 5 +
25 7 files changed, 565 insertions(+)
26 create mode 100644 drivers/gpu/drm/vc4/vc4_dpi.c
28 --- a/Documentation/devicetree/bindings/display/brcm,bcm-vc4.txt
29 +++ b/Documentation/devicetree/bindings/display/brcm,bcm-vc4.txt
30 @@ -35,12 +35,22 @@ Optional properties for HDMI:
31 as an interrupt/status bit in the HDMI controller
32 itself). See bindings/pinctrl/brcm,bcm2835-gpio.txt
34 +Required properties for DPI:
35 +- compatible: Should be "brcm,bcm2835-dpi"
36 +- reg: Physical base address and length of the registers
37 +- clocks: a) core: The core clock the unit runs on
38 + b) pixel: The pixel clock that feeds the pixelvalve
39 +- port: Port node with a single endpoint connecting to the panel
40 + device, as defined in [1]
42 Required properties for V3D:
43 - compatible: Should be "brcm,bcm2835-v3d"
44 - reg: Physical base address and length of the V3D's registers
45 - interrupts: The interrupt number
46 See bindings/interrupt-controller/brcm,bcm2835-armctrl-ic.txt
48 +[1] Documentation/devicetree/bindings/media/video-interfaces.txt
52 compatible = "brcm,bcm2835-pixelvalve2";
53 @@ -66,6 +76,22 @@ hdmi: hdmi@7e902000 {
54 clock-names = "pixel", "hdmi";
58 + compatible = "brcm,bcm2835-dpi";
59 + reg = <0x7e208000 0x8c>;
60 + clocks = <&clocks BCM2835_CLOCK_VPU>,
61 + <&clocks BCM2835_CLOCK_DPI>;
62 + clock-names = "core", "pixel";
63 + #address-cells = <1>;
67 + dpi_out: endpoint@0 {
68 + remote-endpoint = <&panel_in>;
74 compatible = "brcm,bcm2835-v3d";
75 reg = <0x7ec00000 0x1000>;
76 @@ -75,3 +101,13 @@ v3d: v3d@7ec00000 {
78 compatible = "brcm,bcm2835-vc4";
82 + compatible = "ontat,yx700wv03", "simple-panel";
85 + panel_in: endpoint {
86 + remote-endpoint = <&dpi_out>;
90 --- a/drivers/gpu/drm/vc4/Kconfig
91 +++ b/drivers/gpu/drm/vc4/Kconfig
92 @@ -5,6 +5,7 @@ config DRM_VC4
94 select DRM_KMS_CMA_HELPER
95 select DRM_GEM_CMA_HELPER
98 Choose this option if you have a system that has a Broadcom
99 VC4 GPU, such as the Raspberry Pi or other BCM2708/BCM2835.
100 --- a/drivers/gpu/drm/vc4/Makefile
101 +++ b/drivers/gpu/drm/vc4/Makefile
102 @@ -7,6 +7,7 @@ vc4-y := \
110 --- a/drivers/gpu/drm/vc4/vc4_debugfs.c
111 +++ b/drivers/gpu/drm/vc4/vc4_debugfs.c
114 static const struct drm_info_list vc4_debugfs_list[] = {
115 {"bo_stats", vc4_bo_stats_debugfs, 0},
116 + {"dpi_regs", vc4_dpi_debugfs_regs, 0},
117 {"gem_exec", vc4_gem_exec_debugfs, 0},
118 {"hdmi_regs", vc4_hdmi_debugfs_regs, 0},
119 {"hvs_regs", vc4_hvs_debugfs_regs, 0},
121 +++ b/drivers/gpu/drm/vc4/vc4_dpi.c
124 + * Copyright (C) 2016 Broadcom Limited
126 + * This program is free software; you can redistribute it and/or modify it
127 + * under the terms of the GNU General Public License version 2 as published by
128 + * the Free Software Foundation.
130 + * This program is distributed in the hope that it will be useful, but WITHOUT
131 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
132 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
135 + * You should have received a copy of the GNU General Public License along with
136 + * this program. If not, see <http://www.gnu.org/licenses/>.
140 + * DOC: VC4 DPI module
142 + * The VC4 DPI hardware supports MIPI DPI type 4 and Nokia ViSSI
143 + * signals, which are routed out to GPIO0-27 with the ALT2 function.
146 +#include "drm_atomic_helper.h"
147 +#include "drm_crtc_helper.h"
148 +#include "drm_edid.h"
149 +#include "drm_panel.h"
150 +#include "linux/clk.h"
151 +#include "linux/component.h"
152 +#include "linux/of_graph.h"
153 +#include "linux/of_platform.h"
154 +#include "vc4_drv.h"
155 +#include "vc4_regs.h"
158 +# define DPI_OUTPUT_ENABLE_MODE BIT(16)
160 +/* The order field takes the incoming 24 bit RGB from the pixel valve
161 + * and shuffles the 3 channels.
163 +# define DPI_ORDER_MASK VC4_MASK(15, 14)
164 +# define DPI_ORDER_SHIFT 14
165 +# define DPI_ORDER_RGB 0
166 +# define DPI_ORDER_BGR 1
167 +# define DPI_ORDER_GRB 2
168 +# define DPI_ORDER_BRG 3
170 +/* The format field takes the ORDER-shuffled pixel valve data and
171 + * formats it onto the output lines.
173 +# define DPI_FORMAT_MASK VC4_MASK(13, 11)
174 +# define DPI_FORMAT_SHIFT 11
175 +/* This define is named in the hardware, but actually just outputs 0. */
176 +# define DPI_FORMAT_9BIT_666_RGB 0
177 +/* Outputs 00000000rrrrrggggggbbbbb */
178 +# define DPI_FORMAT_16BIT_565_RGB_1 1
179 +/* Outputs 000rrrrr00gggggg000bbbbb */
180 +# define DPI_FORMAT_16BIT_565_RGB_2 2
181 +/* Outputs 00rrrrr000gggggg00bbbbb0 */
182 +# define DPI_FORMAT_16BIT_565_RGB_3 3
183 +/* Outputs 000000rrrrrrggggggbbbbbb */
184 +# define DPI_FORMAT_18BIT_666_RGB_1 4
185 +/* Outputs 00rrrrrr00gggggg00bbbbbb */
186 +# define DPI_FORMAT_18BIT_666_RGB_2 5
187 +/* Outputs rrrrrrrrggggggggbbbbbbbb */
188 +# define DPI_FORMAT_24BIT_888_RGB 6
190 +/* Reverses the polarity of the corresponding signal */
191 +# define DPI_PIXEL_CLK_INVERT BIT(10)
192 +# define DPI_HSYNC_INVERT BIT(9)
193 +# define DPI_VSYNC_INVERT BIT(8)
194 +# define DPI_OUTPUT_ENABLE_INVERT BIT(7)
196 +/* Outputs the signal the falling clock edge instead of rising. */
197 +# define DPI_HSYNC_NEGATE BIT(6)
198 +# define DPI_VSYNC_NEGATE BIT(5)
199 +# define DPI_OUTPUT_ENABLE_NEGATE BIT(4)
201 +/* Disables the signal */
202 +# define DPI_HSYNC_DISABLE BIT(3)
203 +# define DPI_VSYNC_DISABLE BIT(2)
204 +# define DPI_OUTPUT_ENABLE_DISABLE BIT(1)
206 +/* Power gate to the device, full reset at 0 -> 1 transition */
207 +# define DPI_ENABLE BIT(0)
209 +/* All other registers besides DPI_C return the ID */
211 +# define DPI_ID_VALUE 0x00647069
213 +/* General DPI hardware state. */
215 + struct platform_device *pdev;
217 + struct drm_encoder *encoder;
218 + struct drm_connector *connector;
219 + struct drm_panel *panel;
221 + void __iomem *regs;
223 + struct clk *pixel_clock;
224 + struct clk *core_clock;
227 +#define DPI_READ(offset) readl(dpi->regs + (offset))
228 +#define DPI_WRITE(offset, val) writel(val, dpi->regs + (offset))
230 +/* VC4 DPI encoder KMS struct */
231 +struct vc4_dpi_encoder {
232 + struct vc4_encoder base;
233 + struct vc4_dpi *dpi;
236 +static inline struct vc4_dpi_encoder *
237 +to_vc4_dpi_encoder(struct drm_encoder *encoder)
239 + return container_of(encoder, struct vc4_dpi_encoder, base.base);
242 +/* VC4 DPI connector KMS struct */
243 +struct vc4_dpi_connector {
244 + struct drm_connector base;
245 + struct vc4_dpi *dpi;
247 + /* Since the connector is attached to just the one encoder,
248 + * this is the reference to it so we can do the best_encoder()
251 + struct drm_encoder *encoder;
254 +static inline struct vc4_dpi_connector *
255 +to_vc4_dpi_connector(struct drm_connector *connector)
257 + return container_of(connector, struct vc4_dpi_connector, base);
260 +#define DPI_REG(reg) { reg, #reg }
261 +static const struct {
269 +static void vc4_dpi_dump_regs(struct vc4_dpi *dpi)
273 + for (i = 0; i < ARRAY_SIZE(dpi_regs); i++) {
274 + DRM_INFO("0x%04x (%s): 0x%08x\n",
275 + dpi_regs[i].reg, dpi_regs[i].name,
276 + DPI_READ(dpi_regs[i].reg));
280 +#ifdef CONFIG_DEBUG_FS
281 +int vc4_dpi_debugfs_regs(struct seq_file *m, void *unused)
283 + struct drm_info_node *node = (struct drm_info_node *)m->private;
284 + struct drm_device *dev = node->minor->dev;
285 + struct vc4_dev *vc4 = to_vc4_dev(dev);
286 + struct vc4_dpi *dpi = vc4->dpi;
292 + for (i = 0; i < ARRAY_SIZE(dpi_regs); i++) {
293 + seq_printf(m, "%s (0x%04x): 0x%08x\n",
294 + dpi_regs[i].name, dpi_regs[i].reg,
295 + DPI_READ(dpi_regs[i].reg));
302 +static enum drm_connector_status
303 +vc4_dpi_connector_detect(struct drm_connector *connector, bool force)
305 + struct vc4_dpi_connector *vc4_connector =
306 + to_vc4_dpi_connector(connector);
307 + struct vc4_dpi *dpi = vc4_connector->dpi;
310 + return connector_status_connected;
312 + return connector_status_disconnected;
315 +static void vc4_dpi_connector_destroy(struct drm_connector *connector)
317 + drm_connector_unregister(connector);
318 + drm_connector_cleanup(connector);
321 +static int vc4_dpi_connector_get_modes(struct drm_connector *connector)
323 + struct vc4_dpi_connector *vc4_connector =
324 + to_vc4_dpi_connector(connector);
325 + struct vc4_dpi *dpi = vc4_connector->dpi;
328 + return drm_panel_get_modes(dpi->panel);
333 +static struct drm_encoder *
334 +vc4_dpi_connector_best_encoder(struct drm_connector *connector)
336 + struct vc4_dpi_connector *dpi_connector =
337 + to_vc4_dpi_connector(connector);
338 + return dpi_connector->encoder;
341 +static const struct drm_connector_funcs vc4_dpi_connector_funcs = {
342 + .dpms = drm_atomic_helper_connector_dpms,
343 + .detect = vc4_dpi_connector_detect,
344 + .fill_modes = drm_helper_probe_single_connector_modes,
345 + .destroy = vc4_dpi_connector_destroy,
346 + .reset = drm_atomic_helper_connector_reset,
347 + .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
348 + .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
351 +static const struct drm_connector_helper_funcs vc4_dpi_connector_helper_funcs = {
352 + .get_modes = vc4_dpi_connector_get_modes,
353 + .best_encoder = vc4_dpi_connector_best_encoder,
356 +static struct drm_connector *vc4_dpi_connector_init(struct drm_device *dev,
357 + struct vc4_dpi *dpi)
359 + struct drm_connector *connector = NULL;
360 + struct vc4_dpi_connector *dpi_connector;
363 + dpi_connector = devm_kzalloc(dev->dev, sizeof(*dpi_connector),
365 + if (!dpi_connector) {
369 + connector = &dpi_connector->base;
371 + dpi_connector->encoder = dpi->encoder;
372 + dpi_connector->dpi = dpi;
374 + drm_connector_init(dev, connector, &vc4_dpi_connector_funcs,
375 + DRM_MODE_CONNECTOR_DPI);
376 + drm_connector_helper_add(connector, &vc4_dpi_connector_helper_funcs);
378 + connector->polled = 0;
379 + connector->interlace_allowed = 0;
380 + connector->doublescan_allowed = 0;
382 + drm_mode_connector_attach_encoder(connector, dpi->encoder);
388 + vc4_dpi_connector_destroy(connector);
390 + return ERR_PTR(ret);
393 +static const struct drm_encoder_funcs vc4_dpi_encoder_funcs = {
394 + .destroy = drm_encoder_cleanup,
397 +static void vc4_dpi_encoder_disable(struct drm_encoder *encoder)
399 + struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder);
400 + struct vc4_dpi *dpi = vc4_encoder->dpi;
402 + drm_panel_disable(dpi->panel);
404 + clk_disable_unprepare(dpi->pixel_clock);
406 + drm_panel_unprepare(dpi->panel);
409 +static void vc4_dpi_encoder_enable(struct drm_encoder *encoder)
411 + struct drm_display_mode *mode = &encoder->crtc->mode;
412 + struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder);
413 + struct vc4_dpi *dpi = vc4_encoder->dpi;
414 + u32 dpi_c = DPI_ENABLE | DPI_OUTPUT_ENABLE_MODE;
417 + ret = drm_panel_prepare(dpi->panel);
419 + DRM_ERROR("Panel failed to prepare\n");
423 + if (dpi->connector->display_info.num_bus_formats) {
424 + u32 bus_format = dpi->connector->display_info.bus_formats[0];
426 + switch (bus_format) {
427 + case MEDIA_BUS_FMT_RGB888_1X24:
428 + dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
431 + case MEDIA_BUS_FMT_BGR888_1X24:
432 + dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
434 + dpi_c |= VC4_SET_FIELD(DPI_ORDER_BGR, DPI_ORDER);
436 + case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
437 + dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_2,
440 + case MEDIA_BUS_FMT_RGB666_1X18:
441 + dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_1,
444 + case MEDIA_BUS_FMT_RGB565_1X16:
445 + dpi_c |= VC4_SET_FIELD(DPI_FORMAT_16BIT_565_RGB_3,
449 + DRM_ERROR("Unknown media bus format %d\n", bus_format);
454 + if (mode->flags & DRM_MODE_FLAG_NHSYNC)
455 + dpi_c |= DPI_HSYNC_INVERT;
456 + else if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
457 + dpi_c |= DPI_HSYNC_DISABLE;
459 + if (mode->flags & DRM_MODE_FLAG_NVSYNC)
460 + dpi_c |= DPI_VSYNC_INVERT;
461 + else if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
462 + dpi_c |= DPI_VSYNC_DISABLE;
464 + DPI_WRITE(DPI_C, dpi_c);
466 + ret = clk_set_rate(dpi->pixel_clock, mode->clock * 1000);
468 + DRM_ERROR("Failed to set clock rate: %d\n", ret);
470 + ret = clk_prepare_enable(dpi->pixel_clock);
472 + DRM_ERROR("Failed to set clock rate: %d\n", ret);
474 + ret = drm_panel_enable(dpi->panel);
476 + DRM_ERROR("Panel failed to enable\n");
477 + drm_panel_unprepare(dpi->panel);
482 +static const struct drm_encoder_helper_funcs vc4_dpi_encoder_helper_funcs = {
483 + .disable = vc4_dpi_encoder_disable,
484 + .enable = vc4_dpi_encoder_enable,
487 +static const struct of_device_id vc4_dpi_dt_match[] = {
488 + { .compatible = "brcm,bcm2835-dpi", .data = NULL },
492 +/* Walks the OF graph to find the panel node and then asks DRM to look
495 +static struct drm_panel *vc4_dpi_get_panel(struct device *dev)
497 + struct device_node *endpoint, *panel_node;
498 + struct device_node *np = dev->of_node;
499 + struct drm_panel *panel;
501 + endpoint = of_graph_get_next_endpoint(np, NULL);
503 + dev_err(dev, "no endpoint to fetch DPI panel\n");
507 + /* don't proceed if we have an endpoint but no panel_node tied to it */
508 + panel_node = of_graph_get_remote_port_parent(endpoint);
509 + of_node_put(endpoint);
511 + dev_err(dev, "no valid panel node\n");
515 + panel = of_drm_find_panel(panel_node);
516 + of_node_put(panel_node);
521 +static int vc4_dpi_bind(struct device *dev, struct device *master, void *data)
523 + struct platform_device *pdev = to_platform_device(dev);
524 + struct drm_device *drm = dev_get_drvdata(master);
525 + struct vc4_dev *vc4 = to_vc4_dev(drm);
526 + struct vc4_dpi *dpi;
527 + struct vc4_dpi_encoder *vc4_dpi_encoder;
530 + dpi = devm_kzalloc(dev, sizeof(*dpi), GFP_KERNEL);
534 + vc4_dpi_encoder = devm_kzalloc(dev, sizeof(*vc4_dpi_encoder),
536 + if (!vc4_dpi_encoder)
538 + vc4_dpi_encoder->base.type = VC4_ENCODER_TYPE_DPI;
539 + vc4_dpi_encoder->dpi = dpi;
540 + dpi->encoder = &vc4_dpi_encoder->base.base;
543 + dpi->regs = vc4_ioremap_regs(pdev, 0);
544 + if (IS_ERR(dpi->regs))
545 + return PTR_ERR(dpi->regs);
547 + vc4_dpi_dump_regs(dpi);
549 + if (DPI_READ(DPI_ID) != DPI_ID_VALUE) {
550 + dev_err(dev, "Port returned 0x%08x for ID instead of 0x%08x\n",
551 + DPI_READ(DPI_ID), DPI_ID_VALUE);
555 + dpi->core_clock = devm_clk_get(dev, "core");
556 + if (IS_ERR(dpi->core_clock)) {
557 + ret = PTR_ERR(dpi->core_clock);
558 + if (ret != -EPROBE_DEFER)
559 + DRM_ERROR("Failed to get core clock: %d\n", ret);
562 + dpi->pixel_clock = devm_clk_get(dev, "pixel");
563 + if (IS_ERR(dpi->pixel_clock)) {
564 + ret = PTR_ERR(dpi->pixel_clock);
565 + if (ret != -EPROBE_DEFER)
566 + DRM_ERROR("Failed to get pixel clock: %d\n", ret);
570 + ret = clk_prepare_enable(dpi->core_clock);
572 + DRM_ERROR("Failed to turn on core clock: %d\n", ret);
574 + dpi->panel = vc4_dpi_get_panel(dev);
576 + drm_encoder_init(drm, dpi->encoder, &vc4_dpi_encoder_funcs,
577 + DRM_MODE_ENCODER_DPI);
578 + drm_encoder_helper_add(dpi->encoder, &vc4_dpi_encoder_helper_funcs);
580 + dpi->connector = vc4_dpi_connector_init(drm, dpi);
581 + if (IS_ERR(dpi->connector)) {
582 + ret = PTR_ERR(dpi->connector);
583 + goto err_destroy_encoder;
587 + drm_panel_attach(dpi->panel, dpi->connector);
589 + dev_set_drvdata(dev, dpi);
595 +err_destroy_encoder:
596 + drm_encoder_cleanup(dpi->encoder);
597 + clk_disable_unprepare(dpi->core_clock);
601 +static void vc4_dpi_unbind(struct device *dev, struct device *master,
604 + struct drm_device *drm = dev_get_drvdata(master);
605 + struct vc4_dev *vc4 = to_vc4_dev(drm);
606 + struct vc4_dpi *dpi = dev_get_drvdata(dev);
609 + drm_panel_detach(dpi->panel);
611 + vc4_dpi_connector_destroy(dpi->connector);
612 + drm_encoder_cleanup(dpi->encoder);
614 + clk_disable_unprepare(dpi->core_clock);
619 +static const struct component_ops vc4_dpi_ops = {
620 + .bind = vc4_dpi_bind,
621 + .unbind = vc4_dpi_unbind,
624 +static int vc4_dpi_dev_probe(struct platform_device *pdev)
626 + return component_add(&pdev->dev, &vc4_dpi_ops);
629 +static int vc4_dpi_dev_remove(struct platform_device *pdev)
631 + component_del(&pdev->dev, &vc4_dpi_ops);
635 +struct platform_driver vc4_dpi_driver = {
636 + .probe = vc4_dpi_dev_probe,
637 + .remove = vc4_dpi_dev_remove,
640 + .of_match_table = vc4_dpi_dt_match,
643 --- a/drivers/gpu/drm/vc4/vc4_drv.c
644 +++ b/drivers/gpu/drm/vc4/vc4_drv.c
645 @@ -259,6 +259,7 @@ static const struct component_master_ops
647 static struct platform_driver *const component_drivers[] = {
653 --- a/drivers/gpu/drm/vc4/vc4_drv.h
654 +++ b/drivers/gpu/drm/vc4/vc4_drv.h
655 @@ -16,6 +16,7 @@ struct vc4_dev {
657 struct vc4_crtc *crtc[3];
659 + struct vc4_dpi *dpi;
661 struct drm_fbdev_cma *fbdev;
662 struct rpi_firmware *firmware;
663 @@ -418,6 +419,10 @@ void vc4_debugfs_cleanup(struct drm_mino
665 void __iomem *vc4_ioremap_regs(struct platform_device *dev, int index);
668 +extern struct platform_driver vc4_dpi_driver;
669 +int vc4_dpi_debugfs_regs(struct seq_file *m, void *unused);
672 void vc4_gem_init(struct drm_device *dev);
673 void vc4_gem_destroy(struct drm_device *dev);