2 * Copyright (C) 2016 Broadcom Limited
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * The VC4 DPI hardware supports MIPI DPI type 4 and Nokia ViSSI
21 * signals, which are routed out to GPIO0-27 with the ALT2 function.
24 #include "drm_atomic_helper.h"
25 #include "drm_crtc_helper.h"
27 #include "drm_panel.h"
28 #include "linux/clk.h"
29 #include "linux/component.h"
30 #include "linux/of_graph.h"
31 #include "linux/of_platform.h"
36 # define DPI_OUTPUT_ENABLE_MODE BIT(16)
38 /* The order field takes the incoming 24 bit RGB from the pixel valve
39 * and shuffles the 3 channels.
41 # define DPI_ORDER_MASK VC4_MASK(15, 14)
42 # define DPI_ORDER_SHIFT 14
43 # define DPI_ORDER_RGB 0
44 # define DPI_ORDER_BGR 1
45 # define DPI_ORDER_GRB 2
46 # define DPI_ORDER_BRG 3
48 /* The format field takes the ORDER-shuffled pixel valve data and
49 * formats it onto the output lines.
51 # define DPI_FORMAT_MASK VC4_MASK(13, 11)
52 # define DPI_FORMAT_SHIFT 11
53 /* This define is named in the hardware, but actually just outputs 0. */
54 # define DPI_FORMAT_9BIT_666_RGB 0
55 /* Outputs 00000000rrrrrggggggbbbbb */
56 # define DPI_FORMAT_16BIT_565_RGB_1 1
57 /* Outputs 000rrrrr00gggggg000bbbbb */
58 # define DPI_FORMAT_16BIT_565_RGB_2 2
59 /* Outputs 00rrrrr000gggggg00bbbbb0 */
60 # define DPI_FORMAT_16BIT_565_RGB_3 3
61 /* Outputs 000000rrrrrrggggggbbbbbb */
62 # define DPI_FORMAT_18BIT_666_RGB_1 4
63 /* Outputs 00rrrrrr00gggggg00bbbbbb */
64 # define DPI_FORMAT_18BIT_666_RGB_2 5
65 /* Outputs rrrrrrrrggggggggbbbbbbbb */
66 # define DPI_FORMAT_24BIT_888_RGB 6
68 /* Reverses the polarity of the corresponding signal */
69 # define DPI_PIXEL_CLK_INVERT BIT(10)
70 # define DPI_HSYNC_INVERT BIT(9)
71 # define DPI_VSYNC_INVERT BIT(8)
72 # define DPI_OUTPUT_ENABLE_INVERT BIT(7)
74 /* Outputs the signal the falling clock edge instead of rising. */
75 # define DPI_HSYNC_NEGATE BIT(6)
76 # define DPI_VSYNC_NEGATE BIT(5)
77 # define DPI_OUTPUT_ENABLE_NEGATE BIT(4)
79 /* Disables the signal */
80 # define DPI_HSYNC_DISABLE BIT(3)
81 # define DPI_VSYNC_DISABLE BIT(2)
82 # define DPI_OUTPUT_ENABLE_DISABLE BIT(1)
84 /* Power gate to the device, full reset at 0 -> 1 transition */
85 # define DPI_ENABLE BIT(0)
87 /* All other registers besides DPI_C return the ID */
89 # define DPI_ID_VALUE 0x00647069
91 /* General DPI hardware state. */
93 struct platform_device *pdev;
95 struct drm_encoder *encoder;
96 struct drm_connector *connector;
97 struct drm_panel *panel;
101 struct clk *pixel_clock;
102 struct clk *core_clock;
105 #define DPI_READ(offset) readl(dpi->regs + (offset))
106 #define DPI_WRITE(offset, val) writel(val, dpi->regs + (offset))
108 /* VC4 DPI encoder KMS struct */
109 struct vc4_dpi_encoder {
110 struct vc4_encoder base;
114 static inline struct vc4_dpi_encoder *
115 to_vc4_dpi_encoder(struct drm_encoder *encoder)
117 return container_of(encoder, struct vc4_dpi_encoder, base.base);
120 /* VC4 DPI connector KMS struct */
121 struct vc4_dpi_connector {
122 struct drm_connector base;
125 /* Since the connector is attached to just the one encoder,
126 * this is the reference to it so we can do the best_encoder()
129 struct drm_encoder *encoder;
132 static inline struct vc4_dpi_connector *
133 to_vc4_dpi_connector(struct drm_connector *connector)
135 return container_of(connector, struct vc4_dpi_connector, base);
138 #define DPI_REG(reg) { reg, #reg }
139 static const struct {
147 #ifdef CONFIG_DEBUG_FS
148 int vc4_dpi_debugfs_regs(struct seq_file *m, void *unused)
150 struct drm_info_node *node = (struct drm_info_node *)m->private;
151 struct drm_device *dev = node->minor->dev;
152 struct vc4_dev *vc4 = to_vc4_dev(dev);
153 struct vc4_dpi *dpi = vc4->dpi;
159 for (i = 0; i < ARRAY_SIZE(dpi_regs); i++) {
160 seq_printf(m, "%s (0x%04x): 0x%08x\n",
161 dpi_regs[i].name, dpi_regs[i].reg,
162 DPI_READ(dpi_regs[i].reg));
169 static enum drm_connector_status
170 vc4_dpi_connector_detect(struct drm_connector *connector, bool force)
172 struct vc4_dpi_connector *vc4_connector =
173 to_vc4_dpi_connector(connector);
174 struct vc4_dpi *dpi = vc4_connector->dpi;
177 return connector_status_connected;
179 return connector_status_disconnected;
182 static void vc4_dpi_connector_destroy(struct drm_connector *connector)
184 drm_connector_unregister(connector);
185 drm_connector_cleanup(connector);
188 static int vc4_dpi_connector_get_modes(struct drm_connector *connector)
190 struct vc4_dpi_connector *vc4_connector =
191 to_vc4_dpi_connector(connector);
192 struct vc4_dpi *dpi = vc4_connector->dpi;
195 return drm_panel_get_modes(dpi->panel);
200 static const struct drm_connector_funcs vc4_dpi_connector_funcs = {
201 .dpms = drm_atomic_helper_connector_dpms,
202 .detect = vc4_dpi_connector_detect,
203 .fill_modes = drm_helper_probe_single_connector_modes,
204 .destroy = vc4_dpi_connector_destroy,
205 .reset = drm_atomic_helper_connector_reset,
206 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
207 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
210 static const struct drm_connector_helper_funcs vc4_dpi_connector_helper_funcs = {
211 .get_modes = vc4_dpi_connector_get_modes,
214 static struct drm_connector *vc4_dpi_connector_init(struct drm_device *dev,
217 struct drm_connector *connector = NULL;
218 struct vc4_dpi_connector *dpi_connector;
220 dpi_connector = devm_kzalloc(dev->dev, sizeof(*dpi_connector),
223 return ERR_PTR(-ENOMEM);
225 connector = &dpi_connector->base;
227 dpi_connector->encoder = dpi->encoder;
228 dpi_connector->dpi = dpi;
230 drm_connector_init(dev, connector, &vc4_dpi_connector_funcs,
231 DRM_MODE_CONNECTOR_DPI);
232 drm_connector_helper_add(connector, &vc4_dpi_connector_helper_funcs);
234 connector->polled = 0;
235 connector->interlace_allowed = 0;
236 connector->doublescan_allowed = 0;
238 drm_mode_connector_attach_encoder(connector, dpi->encoder);
243 static const struct drm_encoder_funcs vc4_dpi_encoder_funcs = {
244 .destroy = drm_encoder_cleanup,
247 static void vc4_dpi_encoder_disable(struct drm_encoder *encoder)
249 struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder);
250 struct vc4_dpi *dpi = vc4_encoder->dpi;
252 drm_panel_disable(dpi->panel);
254 clk_disable_unprepare(dpi->pixel_clock);
256 drm_panel_unprepare(dpi->panel);
259 static void vc4_dpi_encoder_enable(struct drm_encoder *encoder)
261 struct drm_display_mode *mode = &encoder->crtc->mode;
262 struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder);
263 struct vc4_dpi *dpi = vc4_encoder->dpi;
264 u32 dpi_c = DPI_ENABLE | DPI_OUTPUT_ENABLE_MODE;
267 ret = drm_panel_prepare(dpi->panel);
269 DRM_ERROR("Panel failed to prepare\n");
273 if (dpi->connector->display_info.num_bus_formats) {
274 u32 bus_format = dpi->connector->display_info.bus_formats[0];
276 switch (bus_format) {
277 case MEDIA_BUS_FMT_RGB888_1X24:
278 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
281 case MEDIA_BUS_FMT_BGR888_1X24:
282 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
284 dpi_c |= VC4_SET_FIELD(DPI_ORDER_BGR, DPI_ORDER);
286 case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
287 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_2,
290 case MEDIA_BUS_FMT_RGB666_1X18:
291 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_1,
294 case MEDIA_BUS_FMT_RGB565_1X16:
295 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_16BIT_565_RGB_3,
299 DRM_ERROR("Unknown media bus format %d\n", bus_format);
304 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
305 dpi_c |= DPI_HSYNC_INVERT;
306 else if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
307 dpi_c |= DPI_HSYNC_DISABLE;
309 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
310 dpi_c |= DPI_VSYNC_INVERT;
311 else if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
312 dpi_c |= DPI_VSYNC_DISABLE;
314 DPI_WRITE(DPI_C, dpi_c);
316 ret = clk_set_rate(dpi->pixel_clock, mode->clock * 1000);
318 DRM_ERROR("Failed to set clock rate: %d\n", ret);
320 ret = clk_prepare_enable(dpi->pixel_clock);
322 DRM_ERROR("Failed to set clock rate: %d\n", ret);
324 ret = drm_panel_enable(dpi->panel);
326 DRM_ERROR("Panel failed to enable\n");
327 drm_panel_unprepare(dpi->panel);
332 static bool vc4_dpi_encoder_mode_fixup(struct drm_encoder *encoder,
333 const struct drm_display_mode *mode,
334 struct drm_display_mode *adjusted_mode)
336 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
342 static const struct drm_encoder_helper_funcs vc4_dpi_encoder_helper_funcs = {
343 .disable = vc4_dpi_encoder_disable,
344 .enable = vc4_dpi_encoder_enable,
345 .mode_fixup = vc4_dpi_encoder_mode_fixup,
348 static const struct of_device_id vc4_dpi_dt_match[] = {
349 { .compatible = "brcm,bcm2835-dpi", .data = NULL },
353 /* Walks the OF graph to find the panel node and then asks DRM to look
356 static struct drm_panel *vc4_dpi_get_panel(struct device *dev)
358 struct device_node *endpoint, *panel_node;
359 struct device_node *np = dev->of_node;
360 struct drm_panel *panel;
362 endpoint = of_graph_get_next_endpoint(np, NULL);
364 dev_err(dev, "no endpoint to fetch DPI panel\n");
368 /* don't proceed if we have an endpoint but no panel_node tied to it */
369 panel_node = of_graph_get_remote_port_parent(endpoint);
370 of_node_put(endpoint);
372 dev_err(dev, "no valid panel node\n");
376 panel = of_drm_find_panel(panel_node);
377 of_node_put(panel_node);
382 static int vc4_dpi_bind(struct device *dev, struct device *master, void *data)
384 struct platform_device *pdev = to_platform_device(dev);
385 struct drm_device *drm = dev_get_drvdata(master);
386 struct vc4_dev *vc4 = to_vc4_dev(drm);
388 struct vc4_dpi_encoder *vc4_dpi_encoder;
391 dpi = devm_kzalloc(dev, sizeof(*dpi), GFP_KERNEL);
395 vc4_dpi_encoder = devm_kzalloc(dev, sizeof(*vc4_dpi_encoder),
397 if (!vc4_dpi_encoder)
399 vc4_dpi_encoder->base.type = VC4_ENCODER_TYPE_DPI;
400 vc4_dpi_encoder->dpi = dpi;
401 dpi->encoder = &vc4_dpi_encoder->base.base;
404 dpi->regs = vc4_ioremap_regs(pdev, 0);
405 if (IS_ERR(dpi->regs))
406 return PTR_ERR(dpi->regs);
408 if (DPI_READ(DPI_ID) != DPI_ID_VALUE) {
409 dev_err(dev, "Port returned 0x%08x for ID instead of 0x%08x\n",
410 DPI_READ(DPI_ID), DPI_ID_VALUE);
414 dpi->core_clock = devm_clk_get(dev, "core");
415 if (IS_ERR(dpi->core_clock)) {
416 ret = PTR_ERR(dpi->core_clock);
417 if (ret != -EPROBE_DEFER)
418 DRM_ERROR("Failed to get core clock: %d\n", ret);
421 dpi->pixel_clock = devm_clk_get(dev, "pixel");
422 if (IS_ERR(dpi->pixel_clock)) {
423 ret = PTR_ERR(dpi->pixel_clock);
424 if (ret != -EPROBE_DEFER)
425 DRM_ERROR("Failed to get pixel clock: %d\n", ret);
429 ret = clk_prepare_enable(dpi->core_clock);
431 DRM_ERROR("Failed to turn on core clock: %d\n", ret);
433 dpi->panel = vc4_dpi_get_panel(dev);
435 drm_encoder_init(drm, dpi->encoder, &vc4_dpi_encoder_funcs,
436 DRM_MODE_ENCODER_DPI, NULL);
437 drm_encoder_helper_add(dpi->encoder, &vc4_dpi_encoder_helper_funcs);
439 dpi->connector = vc4_dpi_connector_init(drm, dpi);
440 if (IS_ERR(dpi->connector)) {
441 ret = PTR_ERR(dpi->connector);
442 goto err_destroy_encoder;
446 drm_panel_attach(dpi->panel, dpi->connector);
448 dev_set_drvdata(dev, dpi);
455 drm_encoder_cleanup(dpi->encoder);
456 clk_disable_unprepare(dpi->core_clock);
460 static void vc4_dpi_unbind(struct device *dev, struct device *master,
463 struct drm_device *drm = dev_get_drvdata(master);
464 struct vc4_dev *vc4 = to_vc4_dev(drm);
465 struct vc4_dpi *dpi = dev_get_drvdata(dev);
468 drm_panel_detach(dpi->panel);
470 vc4_dpi_connector_destroy(dpi->connector);
471 drm_encoder_cleanup(dpi->encoder);
473 clk_disable_unprepare(dpi->core_clock);
478 static const struct component_ops vc4_dpi_ops = {
479 .bind = vc4_dpi_bind,
480 .unbind = vc4_dpi_unbind,
483 static int vc4_dpi_dev_probe(struct platform_device *pdev)
485 return component_add(&pdev->dev, &vc4_dpi_ops);
488 static int vc4_dpi_dev_remove(struct platform_device *pdev)
490 component_del(&pdev->dev, &vc4_dpi_ops);
494 struct platform_driver vc4_dpi_driver = {
495 .probe = vc4_dpi_dev_probe,
496 .remove = vc4_dpi_dev_remove,
499 .of_match_table = vc4_dpi_dt_match,