1 /* 2 * Copyright (C) 2016 Broadcom Limited 3 * 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. 7 * 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 11 * more details. 12 * 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/>. 15 */ 16 17 /** 18 * DOC: VC4 DPI module 19 * 20 * The VC4 DPI hardware supports MIPI DPI type 4 and Nokia ViSSI 21 * signals. On BCM2835, these can be routed out to GPIO0-27 with the 22 * ALT2 function. 23 */ 24 25 #include "drm_atomic_helper.h" 26 #include "drm_crtc_helper.h" 27 #include "drm_edid.h" 28 #include "drm_panel.h" 29 #include "linux/clk.h" 30 #include "linux/component.h" 31 #include "linux/of_graph.h" 32 #include "linux/of_platform.h" 33 #include "vc4_drv.h" 34 #include "vc4_regs.h" 35 36 #define DPI_C 0x00 37 # define DPI_OUTPUT_ENABLE_MODE BIT(16) 38 39 /* The order field takes the incoming 24 bit RGB from the pixel valve 40 * and shuffles the 3 channels. 41 */ 42 # define DPI_ORDER_MASK VC4_MASK(15, 14) 43 # define DPI_ORDER_SHIFT 14 44 # define DPI_ORDER_RGB 0 45 # define DPI_ORDER_BGR 1 46 # define DPI_ORDER_GRB 2 47 # define DPI_ORDER_BRG 3 48 49 /* The format field takes the ORDER-shuffled pixel valve data and 50 * formats it onto the output lines. 51 */ 52 # define DPI_FORMAT_MASK VC4_MASK(13, 11) 53 # define DPI_FORMAT_SHIFT 11 54 /* This define is named in the hardware, but actually just outputs 0. */ 55 # define DPI_FORMAT_9BIT_666_RGB 0 56 /* Outputs 00000000rrrrrggggggbbbbb */ 57 # define DPI_FORMAT_16BIT_565_RGB_1 1 58 /* Outputs 000rrrrr00gggggg000bbbbb */ 59 # define DPI_FORMAT_16BIT_565_RGB_2 2 60 /* Outputs 00rrrrr000gggggg00bbbbb0 */ 61 # define DPI_FORMAT_16BIT_565_RGB_3 3 62 /* Outputs 000000rrrrrrggggggbbbbbb */ 63 # define DPI_FORMAT_18BIT_666_RGB_1 4 64 /* Outputs 00rrrrrr00gggggg00bbbbbb */ 65 # define DPI_FORMAT_18BIT_666_RGB_2 5 66 /* Outputs rrrrrrrrggggggggbbbbbbbb */ 67 # define DPI_FORMAT_24BIT_888_RGB 6 68 69 /* Reverses the polarity of the corresponding signal */ 70 # define DPI_PIXEL_CLK_INVERT BIT(10) 71 # define DPI_HSYNC_INVERT BIT(9) 72 # define DPI_VSYNC_INVERT BIT(8) 73 # define DPI_OUTPUT_ENABLE_INVERT BIT(7) 74 75 /* Outputs the signal the falling clock edge instead of rising. */ 76 # define DPI_HSYNC_NEGATE BIT(6) 77 # define DPI_VSYNC_NEGATE BIT(5) 78 # define DPI_OUTPUT_ENABLE_NEGATE BIT(4) 79 80 /* Disables the signal */ 81 # define DPI_HSYNC_DISABLE BIT(3) 82 # define DPI_VSYNC_DISABLE BIT(2) 83 # define DPI_OUTPUT_ENABLE_DISABLE BIT(1) 84 85 /* Power gate to the device, full reset at 0 -> 1 transition */ 86 # define DPI_ENABLE BIT(0) 87 88 /* All other registers besides DPI_C return the ID */ 89 #define DPI_ID 0x04 90 # define DPI_ID_VALUE 0x00647069 91 92 /* General DPI hardware state. */ 93 struct vc4_dpi { 94 struct platform_device *pdev; 95 96 struct drm_encoder *encoder; 97 struct drm_connector *connector; 98 struct drm_panel *panel; 99 100 void __iomem *regs; 101 102 struct clk *pixel_clock; 103 struct clk *core_clock; 104 }; 105 106 #define DPI_READ(offset) readl(dpi->regs + (offset)) 107 #define DPI_WRITE(offset, val) writel(val, dpi->regs + (offset)) 108 109 /* VC4 DPI encoder KMS struct */ 110 struct vc4_dpi_encoder { 111 struct vc4_encoder base; 112 struct vc4_dpi *dpi; 113 }; 114 115 static inline struct vc4_dpi_encoder * 116 to_vc4_dpi_encoder(struct drm_encoder *encoder) 117 { 118 return container_of(encoder, struct vc4_dpi_encoder, base.base); 119 } 120 121 /* VC4 DPI connector KMS struct */ 122 struct vc4_dpi_connector { 123 struct drm_connector base; 124 struct vc4_dpi *dpi; 125 126 /* Since the connector is attached to just the one encoder, 127 * this is the reference to it so we can do the best_encoder() 128 * hook. 129 */ 130 struct drm_encoder *encoder; 131 }; 132 133 static inline struct vc4_dpi_connector * 134 to_vc4_dpi_connector(struct drm_connector *connector) 135 { 136 return container_of(connector, struct vc4_dpi_connector, base); 137 } 138 139 #define DPI_REG(reg) { reg, #reg } 140 static const struct { 141 u32 reg; 142 const char *name; 143 } dpi_regs[] = { 144 DPI_REG(DPI_C), 145 DPI_REG(DPI_ID), 146 }; 147 148 #ifdef CONFIG_DEBUG_FS 149 int vc4_dpi_debugfs_regs(struct seq_file *m, void *unused) 150 { 151 struct drm_info_node *node = (struct drm_info_node *)m->private; 152 struct drm_device *dev = node->minor->dev; 153 struct vc4_dev *vc4 = to_vc4_dev(dev); 154 struct vc4_dpi *dpi = vc4->dpi; 155 int i; 156 157 if (!dpi) 158 return 0; 159 160 for (i = 0; i < ARRAY_SIZE(dpi_regs); i++) { 161 seq_printf(m, "%s (0x%04x): 0x%08x\n", 162 dpi_regs[i].name, dpi_regs[i].reg, 163 DPI_READ(dpi_regs[i].reg)); 164 } 165 166 return 0; 167 } 168 #endif 169 170 static enum drm_connector_status 171 vc4_dpi_connector_detect(struct drm_connector *connector, bool force) 172 { 173 struct vc4_dpi_connector *vc4_connector = 174 to_vc4_dpi_connector(connector); 175 struct vc4_dpi *dpi = vc4_connector->dpi; 176 177 if (dpi->panel) 178 return connector_status_connected; 179 else 180 return connector_status_disconnected; 181 } 182 183 static void vc4_dpi_connector_destroy(struct drm_connector *connector) 184 { 185 drm_connector_unregister(connector); 186 drm_connector_cleanup(connector); 187 } 188 189 static int vc4_dpi_connector_get_modes(struct drm_connector *connector) 190 { 191 struct vc4_dpi_connector *vc4_connector = 192 to_vc4_dpi_connector(connector); 193 struct vc4_dpi *dpi = vc4_connector->dpi; 194 195 if (dpi->panel) 196 return drm_panel_get_modes(dpi->panel); 197 198 return 0; 199 } 200 201 static const struct drm_connector_funcs vc4_dpi_connector_funcs = { 202 .dpms = drm_atomic_helper_connector_dpms, 203 .detect = vc4_dpi_connector_detect, 204 .fill_modes = drm_helper_probe_single_connector_modes, 205 .destroy = vc4_dpi_connector_destroy, 206 .reset = drm_atomic_helper_connector_reset, 207 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 208 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 209 }; 210 211 static const struct drm_connector_helper_funcs vc4_dpi_connector_helper_funcs = { 212 .get_modes = vc4_dpi_connector_get_modes, 213 }; 214 215 static struct drm_connector *vc4_dpi_connector_init(struct drm_device *dev, 216 struct vc4_dpi *dpi) 217 { 218 struct drm_connector *connector = NULL; 219 struct vc4_dpi_connector *dpi_connector; 220 221 dpi_connector = devm_kzalloc(dev->dev, sizeof(*dpi_connector), 222 GFP_KERNEL); 223 if (!dpi_connector) 224 return ERR_PTR(-ENOMEM); 225 226 connector = &dpi_connector->base; 227 228 dpi_connector->encoder = dpi->encoder; 229 dpi_connector->dpi = dpi; 230 231 drm_connector_init(dev, connector, &vc4_dpi_connector_funcs, 232 DRM_MODE_CONNECTOR_DPI); 233 drm_connector_helper_add(connector, &vc4_dpi_connector_helper_funcs); 234 235 connector->polled = 0; 236 connector->interlace_allowed = 0; 237 connector->doublescan_allowed = 0; 238 239 drm_mode_connector_attach_encoder(connector, dpi->encoder); 240 241 return connector; 242 } 243 244 static const struct drm_encoder_funcs vc4_dpi_encoder_funcs = { 245 .destroy = drm_encoder_cleanup, 246 }; 247 248 static void vc4_dpi_encoder_disable(struct drm_encoder *encoder) 249 { 250 struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder); 251 struct vc4_dpi *dpi = vc4_encoder->dpi; 252 253 drm_panel_disable(dpi->panel); 254 255 clk_disable_unprepare(dpi->pixel_clock); 256 257 drm_panel_unprepare(dpi->panel); 258 } 259 260 static void vc4_dpi_encoder_enable(struct drm_encoder *encoder) 261 { 262 struct drm_display_mode *mode = &encoder->crtc->mode; 263 struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder); 264 struct vc4_dpi *dpi = vc4_encoder->dpi; 265 u32 dpi_c = DPI_ENABLE | DPI_OUTPUT_ENABLE_MODE; 266 int ret; 267 268 ret = drm_panel_prepare(dpi->panel); 269 if (ret) { 270 DRM_ERROR("Panel failed to prepare\n"); 271 return; 272 } 273 274 if (dpi->connector->display_info.num_bus_formats) { 275 u32 bus_format = dpi->connector->display_info.bus_formats[0]; 276 277 switch (bus_format) { 278 case MEDIA_BUS_FMT_RGB888_1X24: 279 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB, 280 DPI_FORMAT); 281 break; 282 case MEDIA_BUS_FMT_BGR888_1X24: 283 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB, 284 DPI_FORMAT); 285 dpi_c |= VC4_SET_FIELD(DPI_ORDER_BGR, DPI_ORDER); 286 break; 287 case MEDIA_BUS_FMT_RGB666_1X24_CPADHI: 288 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_2, 289 DPI_FORMAT); 290 break; 291 case MEDIA_BUS_FMT_RGB666_1X18: 292 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_1, 293 DPI_FORMAT); 294 break; 295 case MEDIA_BUS_FMT_RGB565_1X16: 296 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_16BIT_565_RGB_3, 297 DPI_FORMAT); 298 break; 299 default: 300 DRM_ERROR("Unknown media bus format %d\n", bus_format); 301 break; 302 } 303 } 304 305 if (mode->flags & DRM_MODE_FLAG_NHSYNC) 306 dpi_c |= DPI_HSYNC_INVERT; 307 else if (!(mode->flags & DRM_MODE_FLAG_PHSYNC)) 308 dpi_c |= DPI_HSYNC_DISABLE; 309 310 if (mode->flags & DRM_MODE_FLAG_NVSYNC) 311 dpi_c |= DPI_VSYNC_INVERT; 312 else if (!(mode->flags & DRM_MODE_FLAG_PVSYNC)) 313 dpi_c |= DPI_VSYNC_DISABLE; 314 315 DPI_WRITE(DPI_C, dpi_c); 316 317 ret = clk_set_rate(dpi->pixel_clock, mode->clock * 1000); 318 if (ret) 319 DRM_ERROR("Failed to set clock rate: %d\n", ret); 320 321 ret = clk_prepare_enable(dpi->pixel_clock); 322 if (ret) 323 DRM_ERROR("Failed to set clock rate: %d\n", ret); 324 325 ret = drm_panel_enable(dpi->panel); 326 if (ret) { 327 DRM_ERROR("Panel failed to enable\n"); 328 drm_panel_unprepare(dpi->panel); 329 return; 330 } 331 } 332 333 static bool vc4_dpi_encoder_mode_fixup(struct drm_encoder *encoder, 334 const struct drm_display_mode *mode, 335 struct drm_display_mode *adjusted_mode) 336 { 337 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) 338 return false; 339 340 return true; 341 } 342 343 static const struct drm_encoder_helper_funcs vc4_dpi_encoder_helper_funcs = { 344 .disable = vc4_dpi_encoder_disable, 345 .enable = vc4_dpi_encoder_enable, 346 .mode_fixup = vc4_dpi_encoder_mode_fixup, 347 }; 348 349 static const struct of_device_id vc4_dpi_dt_match[] = { 350 { .compatible = "brcm,bcm2835-dpi", .data = NULL }, 351 {} 352 }; 353 354 /* Walks the OF graph to find the panel node and then asks DRM to look 355 * up the panel. 356 */ 357 static struct drm_panel *vc4_dpi_get_panel(struct device *dev) 358 { 359 struct device_node *panel_node; 360 struct device_node *np = dev->of_node; 361 struct drm_panel *panel; 362 363 /* don't proceed if we have an endpoint but no panel_node tied to it */ 364 panel_node = of_graph_get_remote_node(np, 0, 0); 365 if (!panel_node) 366 return NULL; 367 368 panel = of_drm_find_panel(panel_node); 369 of_node_put(panel_node); 370 371 return panel; 372 } 373 374 static int vc4_dpi_bind(struct device *dev, struct device *master, void *data) 375 { 376 struct platform_device *pdev = to_platform_device(dev); 377 struct drm_device *drm = dev_get_drvdata(master); 378 struct vc4_dev *vc4 = to_vc4_dev(drm); 379 struct vc4_dpi *dpi; 380 struct vc4_dpi_encoder *vc4_dpi_encoder; 381 int ret; 382 383 dpi = devm_kzalloc(dev, sizeof(*dpi), GFP_KERNEL); 384 if (!dpi) 385 return -ENOMEM; 386 387 vc4_dpi_encoder = devm_kzalloc(dev, sizeof(*vc4_dpi_encoder), 388 GFP_KERNEL); 389 if (!vc4_dpi_encoder) 390 return -ENOMEM; 391 vc4_dpi_encoder->base.type = VC4_ENCODER_TYPE_DPI; 392 vc4_dpi_encoder->dpi = dpi; 393 dpi->encoder = &vc4_dpi_encoder->base.base; 394 395 dpi->pdev = pdev; 396 dpi->regs = vc4_ioremap_regs(pdev, 0); 397 if (IS_ERR(dpi->regs)) 398 return PTR_ERR(dpi->regs); 399 400 if (DPI_READ(DPI_ID) != DPI_ID_VALUE) { 401 dev_err(dev, "Port returned 0x%08x for ID instead of 0x%08x\n", 402 DPI_READ(DPI_ID), DPI_ID_VALUE); 403 return -ENODEV; 404 } 405 406 dpi->core_clock = devm_clk_get(dev, "core"); 407 if (IS_ERR(dpi->core_clock)) { 408 ret = PTR_ERR(dpi->core_clock); 409 if (ret != -EPROBE_DEFER) 410 DRM_ERROR("Failed to get core clock: %d\n", ret); 411 return ret; 412 } 413 dpi->pixel_clock = devm_clk_get(dev, "pixel"); 414 if (IS_ERR(dpi->pixel_clock)) { 415 ret = PTR_ERR(dpi->pixel_clock); 416 if (ret != -EPROBE_DEFER) 417 DRM_ERROR("Failed to get pixel clock: %d\n", ret); 418 return ret; 419 } 420 421 ret = clk_prepare_enable(dpi->core_clock); 422 if (ret) 423 DRM_ERROR("Failed to turn on core clock: %d\n", ret); 424 425 dpi->panel = vc4_dpi_get_panel(dev); 426 427 drm_encoder_init(drm, dpi->encoder, &vc4_dpi_encoder_funcs, 428 DRM_MODE_ENCODER_DPI, NULL); 429 drm_encoder_helper_add(dpi->encoder, &vc4_dpi_encoder_helper_funcs); 430 431 dpi->connector = vc4_dpi_connector_init(drm, dpi); 432 if (IS_ERR(dpi->connector)) { 433 ret = PTR_ERR(dpi->connector); 434 goto err_destroy_encoder; 435 } 436 437 if (dpi->panel) 438 drm_panel_attach(dpi->panel, dpi->connector); 439 440 dev_set_drvdata(dev, dpi); 441 442 vc4->dpi = dpi; 443 444 return 0; 445 446 err_destroy_encoder: 447 drm_encoder_cleanup(dpi->encoder); 448 clk_disable_unprepare(dpi->core_clock); 449 return ret; 450 } 451 452 static void vc4_dpi_unbind(struct device *dev, struct device *master, 453 void *data) 454 { 455 struct drm_device *drm = dev_get_drvdata(master); 456 struct vc4_dev *vc4 = to_vc4_dev(drm); 457 struct vc4_dpi *dpi = dev_get_drvdata(dev); 458 459 if (dpi->panel) 460 drm_panel_detach(dpi->panel); 461 462 vc4_dpi_connector_destroy(dpi->connector); 463 drm_encoder_cleanup(dpi->encoder); 464 465 clk_disable_unprepare(dpi->core_clock); 466 467 vc4->dpi = NULL; 468 } 469 470 static const struct component_ops vc4_dpi_ops = { 471 .bind = vc4_dpi_bind, 472 .unbind = vc4_dpi_unbind, 473 }; 474 475 static int vc4_dpi_dev_probe(struct platform_device *pdev) 476 { 477 return component_add(&pdev->dev, &vc4_dpi_ops); 478 } 479 480 static int vc4_dpi_dev_remove(struct platform_device *pdev) 481 { 482 component_del(&pdev->dev, &vc4_dpi_ops); 483 return 0; 484 } 485 486 struct platform_driver vc4_dpi_driver = { 487 .probe = vc4_dpi_dev_probe, 488 .remove = vc4_dpi_dev_remove, 489 .driver = { 490 .name = "vc4_dpi", 491 .of_match_table = vc4_dpi_dt_match, 492 }, 493 }; 494