1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2016 Broadcom Limited 4 */ 5 6 /** 7 * DOC: VC4 DPI module 8 * 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 11 * ALT2 function. 12 */ 13 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/media-bus-format.h> 24 #include <linux/of_graph.h> 25 #include <linux/of_platform.h> 26 #include "vc4_drv.h" 27 #include "vc4_regs.h" 28 29 #define DPI_C 0x00 30 # define DPI_OUTPUT_ENABLE_MODE BIT(16) 31 32 /* The order field takes the incoming 24 bit RGB from the pixel valve 33 * and shuffles the 3 channels. 34 */ 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 41 42 /* The format field takes the ORDER-shuffled pixel valve data and 43 * formats it onto the output lines. 44 */ 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 61 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) 67 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) 72 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) 77 78 /* Power gate to the device, full reset at 0 -> 1 transition */ 79 # define DPI_ENABLE BIT(0) 80 81 /* All other registers besides DPI_C return the ID */ 82 #define DPI_ID 0x04 83 # define DPI_ID_VALUE 0x00647069 84 85 /* General DPI hardware state. */ 86 struct vc4_dpi { 87 struct platform_device *pdev; 88 89 struct drm_encoder *encoder; 90 91 void __iomem *regs; 92 93 struct clk *pixel_clock; 94 struct clk *core_clock; 95 96 struct debugfs_regset32 regset; 97 }; 98 99 #define DPI_READ(offset) readl(dpi->regs + (offset)) 100 #define DPI_WRITE(offset, val) writel(val, dpi->regs + (offset)) 101 102 /* VC4 DPI encoder KMS struct */ 103 struct vc4_dpi_encoder { 104 struct vc4_encoder base; 105 struct vc4_dpi *dpi; 106 }; 107 108 static inline struct vc4_dpi_encoder * 109 to_vc4_dpi_encoder(struct drm_encoder *encoder) 110 { 111 return container_of(encoder, struct vc4_dpi_encoder, base.base); 112 } 113 114 static const struct debugfs_reg32 dpi_regs[] = { 115 VC4_REG32(DPI_C), 116 VC4_REG32(DPI_ID), 117 }; 118 119 static void vc4_dpi_encoder_disable(struct drm_encoder *encoder) 120 { 121 struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder); 122 struct vc4_dpi *dpi = vc4_encoder->dpi; 123 124 clk_disable_unprepare(dpi->pixel_clock); 125 } 126 127 static void vc4_dpi_encoder_enable(struct drm_encoder *encoder) 128 { 129 struct drm_device *dev = encoder->dev; 130 struct drm_display_mode *mode = &encoder->crtc->mode; 131 struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder); 132 struct vc4_dpi *dpi = vc4_encoder->dpi; 133 struct drm_connector_list_iter conn_iter; 134 struct drm_connector *connector = NULL, *connector_scan; 135 u32 dpi_c = DPI_ENABLE; 136 int ret; 137 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. 142 */ 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; 147 break; 148 } 149 } 150 drm_connector_list_iter_end(&conn_iter); 151 152 /* Default to 24bit if no connector or format found. */ 153 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB, DPI_FORMAT); 154 155 if (connector) { 156 if (connector->display_info.num_bus_formats) { 157 u32 bus_format = connector->display_info.bus_formats[0]; 158 159 dpi_c &= ~DPI_FORMAT_MASK; 160 161 switch (bus_format) { 162 case MEDIA_BUS_FMT_RGB888_1X24: 163 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB, 164 DPI_FORMAT); 165 break; 166 case MEDIA_BUS_FMT_BGR888_1X24: 167 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB, 168 DPI_FORMAT); 169 dpi_c |= VC4_SET_FIELD(DPI_ORDER_BGR, 170 DPI_ORDER); 171 break; 172 case MEDIA_BUS_FMT_RGB666_1X24_CPADHI: 173 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_2, 174 DPI_FORMAT); 175 break; 176 case MEDIA_BUS_FMT_RGB666_1X18: 177 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_1, 178 DPI_FORMAT); 179 break; 180 case MEDIA_BUS_FMT_RGB565_1X16: 181 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_16BIT_565_RGB_3, 182 DPI_FORMAT); 183 break; 184 default: 185 DRM_ERROR("Unknown media bus format %d\n", 186 bus_format); 187 break; 188 } 189 } 190 191 if (connector->display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE) 192 dpi_c |= DPI_PIXEL_CLK_INVERT; 193 194 if (connector->display_info.bus_flags & DRM_BUS_FLAG_DE_LOW) 195 dpi_c |= DPI_OUTPUT_ENABLE_INVERT; 196 } 197 198 if (mode->flags & DRM_MODE_FLAG_CSYNC) { 199 if (mode->flags & DRM_MODE_FLAG_NCSYNC) 200 dpi_c |= DPI_OUTPUT_ENABLE_INVERT; 201 } else { 202 dpi_c |= DPI_OUTPUT_ENABLE_MODE; 203 204 if (mode->flags & DRM_MODE_FLAG_NHSYNC) 205 dpi_c |= DPI_HSYNC_INVERT; 206 else if (!(mode->flags & DRM_MODE_FLAG_PHSYNC)) 207 dpi_c |= DPI_HSYNC_DISABLE; 208 209 if (mode->flags & DRM_MODE_FLAG_NVSYNC) 210 dpi_c |= DPI_VSYNC_INVERT; 211 else if (!(mode->flags & DRM_MODE_FLAG_PVSYNC)) 212 dpi_c |= DPI_VSYNC_DISABLE; 213 } 214 215 DPI_WRITE(DPI_C, dpi_c); 216 217 ret = clk_set_rate(dpi->pixel_clock, mode->clock * 1000); 218 if (ret) 219 DRM_ERROR("Failed to set clock rate: %d\n", ret); 220 221 ret = clk_prepare_enable(dpi->pixel_clock); 222 if (ret) 223 DRM_ERROR("Failed to set clock rate: %d\n", ret); 224 } 225 226 static enum drm_mode_status vc4_dpi_encoder_mode_valid(struct drm_encoder *encoder, 227 const struct drm_display_mode *mode) 228 { 229 if (mode->flags & DRM_MODE_FLAG_INTERLACE) 230 return MODE_NO_INTERLACE; 231 232 return MODE_OK; 233 } 234 235 static const struct drm_encoder_helper_funcs vc4_dpi_encoder_helper_funcs = { 236 .disable = vc4_dpi_encoder_disable, 237 .enable = vc4_dpi_encoder_enable, 238 .mode_valid = vc4_dpi_encoder_mode_valid, 239 }; 240 241 static const struct of_device_id vc4_dpi_dt_match[] = { 242 { .compatible = "brcm,bcm2835-dpi", .data = NULL }, 243 {} 244 }; 245 246 /* Sets up the next link in the display chain, whether it's a panel or 247 * a bridge. 248 */ 249 static int vc4_dpi_init_bridge(struct vc4_dpi *dpi) 250 { 251 struct device *dev = &dpi->pdev->dev; 252 struct drm_bridge *bridge; 253 254 bridge = devm_drm_of_get_bridge(dev, dev->of_node, 0, 0); 255 if (IS_ERR(bridge)) { 256 /* If nothing was connected in the DT, that's not an 257 * error. 258 */ 259 if (PTR_ERR(bridge) == -ENODEV) 260 return 0; 261 else 262 return PTR_ERR(bridge); 263 } 264 265 return drm_bridge_attach(dpi->encoder, bridge, NULL, 0); 266 } 267 268 static int vc4_dpi_bind(struct device *dev, struct device *master, void *data) 269 { 270 struct platform_device *pdev = to_platform_device(dev); 271 struct drm_device *drm = dev_get_drvdata(master); 272 struct vc4_dev *vc4 = to_vc4_dev(drm); 273 struct vc4_dpi *dpi; 274 struct vc4_dpi_encoder *vc4_dpi_encoder; 275 int ret; 276 277 dpi = devm_kzalloc(dev, sizeof(*dpi), GFP_KERNEL); 278 if (!dpi) 279 return -ENOMEM; 280 281 vc4_dpi_encoder = devm_kzalloc(dev, sizeof(*vc4_dpi_encoder), 282 GFP_KERNEL); 283 if (!vc4_dpi_encoder) 284 return -ENOMEM; 285 vc4_dpi_encoder->base.type = VC4_ENCODER_TYPE_DPI; 286 vc4_dpi_encoder->dpi = dpi; 287 dpi->encoder = &vc4_dpi_encoder->base.base; 288 289 dpi->pdev = pdev; 290 dpi->regs = vc4_ioremap_regs(pdev, 0); 291 if (IS_ERR(dpi->regs)) 292 return PTR_ERR(dpi->regs); 293 dpi->regset.base = dpi->regs; 294 dpi->regset.regs = dpi_regs; 295 dpi->regset.nregs = ARRAY_SIZE(dpi_regs); 296 297 if (DPI_READ(DPI_ID) != DPI_ID_VALUE) { 298 dev_err(dev, "Port returned 0x%08x for ID instead of 0x%08x\n", 299 DPI_READ(DPI_ID), DPI_ID_VALUE); 300 return -ENODEV; 301 } 302 303 dpi->core_clock = devm_clk_get(dev, "core"); 304 if (IS_ERR(dpi->core_clock)) { 305 ret = PTR_ERR(dpi->core_clock); 306 if (ret != -EPROBE_DEFER) 307 DRM_ERROR("Failed to get core clock: %d\n", ret); 308 return ret; 309 } 310 dpi->pixel_clock = devm_clk_get(dev, "pixel"); 311 if (IS_ERR(dpi->pixel_clock)) { 312 ret = PTR_ERR(dpi->pixel_clock); 313 if (ret != -EPROBE_DEFER) 314 DRM_ERROR("Failed to get pixel clock: %d\n", ret); 315 return ret; 316 } 317 318 ret = clk_prepare_enable(dpi->core_clock); 319 if (ret) 320 DRM_ERROR("Failed to turn on core clock: %d\n", ret); 321 322 drm_simple_encoder_init(drm, dpi->encoder, DRM_MODE_ENCODER_DPI); 323 drm_encoder_helper_add(dpi->encoder, &vc4_dpi_encoder_helper_funcs); 324 325 ret = vc4_dpi_init_bridge(dpi); 326 if (ret) 327 goto err_destroy_encoder; 328 329 dev_set_drvdata(dev, dpi); 330 331 vc4->dpi = dpi; 332 333 vc4_debugfs_add_regset32(drm, "dpi_regs", &dpi->regset); 334 335 return 0; 336 337 err_destroy_encoder: 338 drm_encoder_cleanup(dpi->encoder); 339 clk_disable_unprepare(dpi->core_clock); 340 return ret; 341 } 342 343 static void vc4_dpi_unbind(struct device *dev, struct device *master, 344 void *data) 345 { 346 struct drm_device *drm = dev_get_drvdata(master); 347 struct vc4_dev *vc4 = to_vc4_dev(drm); 348 struct vc4_dpi *dpi = dev_get_drvdata(dev); 349 350 drm_of_panel_bridge_remove(dev->of_node, 0, 0); 351 352 drm_encoder_cleanup(dpi->encoder); 353 354 clk_disable_unprepare(dpi->core_clock); 355 356 vc4->dpi = NULL; 357 } 358 359 static const struct component_ops vc4_dpi_ops = { 360 .bind = vc4_dpi_bind, 361 .unbind = vc4_dpi_unbind, 362 }; 363 364 static int vc4_dpi_dev_probe(struct platform_device *pdev) 365 { 366 return component_add(&pdev->dev, &vc4_dpi_ops); 367 } 368 369 static int vc4_dpi_dev_remove(struct platform_device *pdev) 370 { 371 component_del(&pdev->dev, &vc4_dpi_ops); 372 return 0; 373 } 374 375 struct platform_driver vc4_dpi_driver = { 376 .probe = vc4_dpi_dev_probe, 377 .remove = vc4_dpi_dev_remove, 378 .driver = { 379 .name = "vc4_dpi", 380 .of_match_table = vc4_dpi_dt_match, 381 }, 382 }; 383