1 // SPDX-License-Identifier: GPL-2.0-only 2 3 /* 4 * Versatile family (ARM reference designs) handling for the PL11x. 5 * This is based on code and know-how in the previous frame buffer 6 * driver in drivers/video/fbdev/amba-clcd.c: 7 * Copyright (C) 2001 ARM Limited, by David A Rusling 8 * Updated to 2.5 by Deep Blue Solutions Ltd. 9 * Major contributions and discoveries by Russell King. 10 */ 11 12 #include <linux/bitops.h> 13 #include <linux/device.h> 14 #include <linux/mfd/syscon.h> 15 #include <linux/module.h> 16 #include <linux/of.h> 17 #include <linux/of_platform.h> 18 #include <linux/platform_device.h> 19 #include <linux/regmap.h> 20 #include <linux/vexpress.h> 21 22 #include <drm/drm_fourcc.h> 23 #include <drm/drm_print.h> 24 25 #include "pl111_versatile.h" 26 #include "pl111_drm.h" 27 28 static struct regmap *versatile_syscon_map; 29 30 /* 31 * We detect the different syscon types from the compatible strings. 32 */ 33 enum versatile_clcd { 34 INTEGRATOR_IMPD1, 35 INTEGRATOR_CLCD_CM, 36 VERSATILE_CLCD, 37 REALVIEW_CLCD_EB, 38 REALVIEW_CLCD_PB1176, 39 REALVIEW_CLCD_PB11MP, 40 REALVIEW_CLCD_PBA8, 41 REALVIEW_CLCD_PBX, 42 VEXPRESS_CLCD_V2M, 43 }; 44 45 static const struct of_device_id versatile_clcd_of_match[] = { 46 { 47 .compatible = "arm,core-module-integrator", 48 .data = (void *)INTEGRATOR_CLCD_CM, 49 }, 50 { 51 .compatible = "arm,versatile-sysreg", 52 .data = (void *)VERSATILE_CLCD, 53 }, 54 { 55 .compatible = "arm,realview-eb-syscon", 56 .data = (void *)REALVIEW_CLCD_EB, 57 }, 58 { 59 .compatible = "arm,realview-pb1176-syscon", 60 .data = (void *)REALVIEW_CLCD_PB1176, 61 }, 62 { 63 .compatible = "arm,realview-pb11mp-syscon", 64 .data = (void *)REALVIEW_CLCD_PB11MP, 65 }, 66 { 67 .compatible = "arm,realview-pba8-syscon", 68 .data = (void *)REALVIEW_CLCD_PBA8, 69 }, 70 { 71 .compatible = "arm,realview-pbx-syscon", 72 .data = (void *)REALVIEW_CLCD_PBX, 73 }, 74 { 75 .compatible = "arm,vexpress-muxfpga", 76 .data = (void *)VEXPRESS_CLCD_V2M, 77 }, 78 {}, 79 }; 80 81 static const struct of_device_id impd1_clcd_of_match[] = { 82 { 83 .compatible = "arm,im-pd1-syscon", 84 .data = (void *)INTEGRATOR_IMPD1, 85 }, 86 {}, 87 }; 88 89 /* 90 * Core module CLCD control on the Integrator/CP, bits 91 * 8 thru 19 of the CM_CONTROL register controls a bunch 92 * of CLCD settings. 93 */ 94 #define INTEGRATOR_HDR_CTRL_OFFSET 0x0C 95 #define INTEGRATOR_CLCD_LCDBIASEN BIT(8) 96 #define INTEGRATOR_CLCD_LCDBIASUP BIT(9) 97 #define INTEGRATOR_CLCD_LCDBIASDN BIT(10) 98 /* Bits 11,12,13 controls the LCD or VGA bridge type */ 99 #define INTEGRATOR_CLCD_LCDMUX_LCD24 BIT(11) 100 #define INTEGRATOR_CLCD_LCDMUX_SHARP (BIT(11)|BIT(12)) 101 #define INTEGRATOR_CLCD_LCDMUX_VGA555 BIT(13) 102 #define INTEGRATOR_CLCD_LCDMUX_VGA24 (BIT(11)|BIT(12)|BIT(13)) 103 #define INTEGRATOR_CLCD_LCD0_EN BIT(14) 104 #define INTEGRATOR_CLCD_LCD1_EN BIT(15) 105 /* R/L flip on Sharp */ 106 #define INTEGRATOR_CLCD_LCD_STATIC1 BIT(16) 107 /* U/D flip on Sharp */ 108 #define INTEGRATOR_CLCD_LCD_STATIC2 BIT(17) 109 /* No connection on Sharp */ 110 #define INTEGRATOR_CLCD_LCD_STATIC BIT(18) 111 /* 0 = 24bit VGA, 1 = 18bit VGA */ 112 #define INTEGRATOR_CLCD_LCD_N24BITEN BIT(19) 113 114 #define INTEGRATOR_CLCD_MASK GENMASK(19, 8) 115 116 static void pl111_integrator_enable(struct drm_device *drm, u32 format) 117 { 118 u32 val; 119 120 drm_info(drm, "enable Integrator CLCD connectors\n"); 121 122 /* FIXME: really needed? */ 123 val = INTEGRATOR_CLCD_LCD_STATIC1 | INTEGRATOR_CLCD_LCD_STATIC2 | 124 INTEGRATOR_CLCD_LCD0_EN | INTEGRATOR_CLCD_LCD1_EN; 125 126 switch (format) { 127 case DRM_FORMAT_XBGR8888: 128 case DRM_FORMAT_XRGB8888: 129 /* 24bit formats */ 130 val |= INTEGRATOR_CLCD_LCDMUX_VGA24; 131 break; 132 case DRM_FORMAT_XBGR1555: 133 case DRM_FORMAT_XRGB1555: 134 /* Pseudocolor, RGB555, BGR555 */ 135 val |= INTEGRATOR_CLCD_LCDMUX_VGA555; 136 break; 137 default: 138 drm_err(drm, "unhandled format on Integrator 0x%08x\n", 139 format); 140 break; 141 } 142 143 regmap_update_bits(versatile_syscon_map, 144 INTEGRATOR_HDR_CTRL_OFFSET, 145 INTEGRATOR_CLCD_MASK, 146 val); 147 } 148 149 #define IMPD1_CTRL_OFFSET 0x18 150 #define IMPD1_CTRL_DISP_LCD (0 << 0) 151 #define IMPD1_CTRL_DISP_VGA (1 << 0) 152 #define IMPD1_CTRL_DISP_LCD1 (2 << 0) 153 #define IMPD1_CTRL_DISP_ENABLE (1 << 2) 154 #define IMPD1_CTRL_DISP_MASK (7 << 0) 155 156 static void pl111_impd1_enable(struct drm_device *drm, u32 format) 157 { 158 u32 val; 159 160 drm_info(drm, "enable IM-PD1 CLCD connectors\n"); 161 val = IMPD1_CTRL_DISP_VGA | IMPD1_CTRL_DISP_ENABLE; 162 163 regmap_update_bits(versatile_syscon_map, 164 IMPD1_CTRL_OFFSET, 165 IMPD1_CTRL_DISP_MASK, 166 val); 167 } 168 169 static void pl111_impd1_disable(struct drm_device *drm) 170 { 171 drm_info(drm, "disable IM-PD1 CLCD connectors\n"); 172 173 regmap_update_bits(versatile_syscon_map, 174 IMPD1_CTRL_OFFSET, 175 IMPD1_CTRL_DISP_MASK, 176 0); 177 } 178 179 /* 180 * This configuration register in the Versatile and RealView 181 * family is uniformly present but appears more and more 182 * unutilized starting with the RealView series. 183 */ 184 #define SYS_CLCD 0x50 185 #define SYS_CLCD_MODE_MASK (BIT(0)|BIT(1)) 186 #define SYS_CLCD_MODE_888 0 187 #define SYS_CLCD_MODE_5551 BIT(0) 188 #define SYS_CLCD_MODE_565_R_LSB BIT(1) 189 #define SYS_CLCD_MODE_565_B_LSB (BIT(0)|BIT(1)) 190 #define SYS_CLCD_CONNECTOR_MASK (BIT(2)|BIT(3)|BIT(4)|BIT(5)) 191 #define SYS_CLCD_NLCDIOON BIT(2) 192 #define SYS_CLCD_VDDPOSSWITCH BIT(3) 193 #define SYS_CLCD_PWR3V5SWITCH BIT(4) 194 #define SYS_CLCD_VDDNEGSWITCH BIT(5) 195 196 static void pl111_versatile_disable(struct drm_device *drm) 197 { 198 drm_info(drm, "disable Versatile CLCD connectors\n"); 199 regmap_update_bits(versatile_syscon_map, 200 SYS_CLCD, 201 SYS_CLCD_CONNECTOR_MASK, 202 0); 203 } 204 205 static void pl111_versatile_enable(struct drm_device *drm, u32 format) 206 { 207 u32 val = 0; 208 209 drm_info(drm, "enable Versatile CLCD connectors\n"); 210 211 switch (format) { 212 case DRM_FORMAT_XBGR8888: 213 case DRM_FORMAT_XRGB8888: 214 val |= SYS_CLCD_MODE_888; 215 break; 216 case DRM_FORMAT_BGR565: 217 val |= SYS_CLCD_MODE_565_R_LSB; 218 break; 219 case DRM_FORMAT_RGB565: 220 val |= SYS_CLCD_MODE_565_B_LSB; 221 break; 222 case DRM_FORMAT_XBGR1555: 223 case DRM_FORMAT_XRGB1555: 224 val |= SYS_CLCD_MODE_5551; 225 break; 226 default: 227 drm_err(drm, "unhandled format on Versatile 0x%08x\n", 228 format); 229 break; 230 } 231 232 /* Set up the MUX */ 233 regmap_update_bits(versatile_syscon_map, 234 SYS_CLCD, 235 SYS_CLCD_MODE_MASK, 236 val); 237 238 /* Then enable the display */ 239 regmap_update_bits(versatile_syscon_map, 240 SYS_CLCD, 241 SYS_CLCD_CONNECTOR_MASK, 242 SYS_CLCD_NLCDIOON | SYS_CLCD_PWR3V5SWITCH); 243 } 244 245 static void pl111_realview_clcd_disable(struct drm_device *drm) 246 { 247 drm_info(drm, "disable RealView CLCD connectors\n"); 248 regmap_update_bits(versatile_syscon_map, 249 SYS_CLCD, 250 SYS_CLCD_CONNECTOR_MASK, 251 0); 252 } 253 254 static void pl111_realview_clcd_enable(struct drm_device *drm, u32 format) 255 { 256 drm_info(drm, "enable RealView CLCD connectors\n"); 257 regmap_update_bits(versatile_syscon_map, 258 SYS_CLCD, 259 SYS_CLCD_CONNECTOR_MASK, 260 SYS_CLCD_NLCDIOON | SYS_CLCD_PWR3V5SWITCH); 261 } 262 263 /* PL110 pixel formats for Integrator, vanilla PL110 */ 264 static const u32 pl110_integrator_pixel_formats[] = { 265 DRM_FORMAT_XBGR8888, 266 DRM_FORMAT_XRGB8888, 267 DRM_FORMAT_XBGR1555, 268 DRM_FORMAT_XRGB1555, 269 }; 270 271 /* Extended PL110 pixel formats for Integrator and Versatile */ 272 static const u32 pl110_versatile_pixel_formats[] = { 273 DRM_FORMAT_XBGR8888, 274 DRM_FORMAT_XRGB8888, 275 DRM_FORMAT_BGR565, /* Uses external PLD */ 276 DRM_FORMAT_RGB565, /* Uses external PLD */ 277 DRM_FORMAT_XBGR1555, 278 DRM_FORMAT_XRGB1555, 279 }; 280 281 static const u32 pl111_realview_pixel_formats[] = { 282 DRM_FORMAT_XBGR8888, 283 DRM_FORMAT_XRGB8888, 284 DRM_FORMAT_BGR565, 285 DRM_FORMAT_RGB565, 286 DRM_FORMAT_XBGR1555, 287 DRM_FORMAT_XRGB1555, 288 DRM_FORMAT_XBGR4444, 289 DRM_FORMAT_XRGB4444, 290 }; 291 292 /* 293 * The Integrator variant is a PL110 with a bunch of broken, or not 294 * yet implemented features 295 */ 296 static const struct pl111_variant_data pl110_integrator = { 297 .name = "PL110 Integrator", 298 .is_pl110 = true, 299 .broken_clockdivider = true, 300 .broken_vblank = true, 301 .formats = pl110_integrator_pixel_formats, 302 .nformats = ARRAY_SIZE(pl110_integrator_pixel_formats), 303 .fb_depth = 16, 304 }; 305 306 /* 307 * The IM-PD1 variant is a PL110 with a bunch of broken, or not 308 * yet implemented features 309 */ 310 static const struct pl111_variant_data pl110_impd1 = { 311 .name = "PL110 IM-PD1", 312 .is_pl110 = true, 313 .broken_clockdivider = true, 314 .broken_vblank = true, 315 .formats = pl110_integrator_pixel_formats, 316 .nformats = ARRAY_SIZE(pl110_integrator_pixel_formats), 317 .fb_depth = 15, 318 }; 319 320 /* 321 * This is the in-between PL110 variant found in the ARM Versatile, 322 * supporting RGB565/BGR565 323 */ 324 static const struct pl111_variant_data pl110_versatile = { 325 .name = "PL110 Versatile", 326 .is_pl110 = true, 327 .external_bgr = true, 328 .formats = pl110_versatile_pixel_formats, 329 .nformats = ARRAY_SIZE(pl110_versatile_pixel_formats), 330 .fb_depth = 16, 331 }; 332 333 /* 334 * RealView PL111 variant, the only real difference from the vanilla 335 * PL111 is that we select 16bpp framebuffer by default to be able 336 * to get 1024x768 without saturating the memory bus. 337 */ 338 static const struct pl111_variant_data pl111_realview = { 339 .name = "PL111 RealView", 340 .formats = pl111_realview_pixel_formats, 341 .nformats = ARRAY_SIZE(pl111_realview_pixel_formats), 342 .fb_depth = 16, 343 }; 344 345 /* 346 * Versatile Express PL111 variant, again we just push the maximum 347 * BPP to 16 to be able to get 1024x768 without saturating the memory 348 * bus. The clockdivider also seems broken on the Versatile Express. 349 */ 350 static const struct pl111_variant_data pl111_vexpress = { 351 .name = "PL111 Versatile Express", 352 .formats = pl111_realview_pixel_formats, 353 .nformats = ARRAY_SIZE(pl111_realview_pixel_formats), 354 .fb_depth = 16, 355 .broken_clockdivider = true, 356 }; 357 358 #define VEXPRESS_FPGAMUX_MOTHERBOARD 0x00 359 #define VEXPRESS_FPGAMUX_DAUGHTERBOARD_1 0x01 360 #define VEXPRESS_FPGAMUX_DAUGHTERBOARD_2 0x02 361 362 static int pl111_vexpress_clcd_init(struct drm_device *dev, struct device_node *np, 363 struct pl111_drm_dev_private *priv) 364 { 365 struct platform_device *pdev; 366 struct device_node *root; 367 struct device_node *child; 368 struct device_node *ct_clcd = NULL; 369 struct regmap *map; 370 bool has_coretile_clcd = false; 371 bool has_coretile_hdlcd = false; 372 bool mux_motherboard = true; 373 u32 val; 374 int ret; 375 376 if (!IS_ENABLED(CONFIG_VEXPRESS_CONFIG)) 377 return -ENODEV; 378 379 /* 380 * Check if we have a CLCD or HDLCD on the core tile by checking if a 381 * CLCD or HDLCD is available in the root of the device tree. 382 */ 383 root = of_find_node_by_path("/"); 384 if (!root) 385 return -EINVAL; 386 387 for_each_available_child_of_node(root, child) { 388 if (of_device_is_compatible(child, "arm,pl111")) { 389 has_coretile_clcd = true; 390 ct_clcd = child; 391 of_node_put(child); 392 break; 393 } 394 if (of_device_is_compatible(child, "arm,hdlcd")) { 395 has_coretile_hdlcd = true; 396 of_node_put(child); 397 break; 398 } 399 } 400 401 of_node_put(root); 402 403 /* 404 * If there is a coretile HDLCD and it has a driver, 405 * do not mux the CLCD on the motherboard to the DVI. 406 */ 407 if (has_coretile_hdlcd && IS_ENABLED(CONFIG_DRM_HDLCD)) 408 mux_motherboard = false; 409 410 /* 411 * On the Vexpress CA9 we let the CLCD on the coretile 412 * take precedence, so also in this case do not mux the 413 * motherboard to the DVI. 414 */ 415 if (has_coretile_clcd) 416 mux_motherboard = false; 417 418 if (mux_motherboard) { 419 drm_info(dev, "DVI muxed to motherboard CLCD\n"); 420 val = VEXPRESS_FPGAMUX_MOTHERBOARD; 421 } else if (ct_clcd == dev->dev->of_node) { 422 drm_info(dev, 423 "DVI muxed to daughterboard 1 (core tile) CLCD\n"); 424 val = VEXPRESS_FPGAMUX_DAUGHTERBOARD_1; 425 } else { 426 drm_info(dev, "core tile graphics present\n"); 427 drm_info(dev, "this device will be deactivated\n"); 428 return -ENODEV; 429 } 430 431 /* Call into deep Vexpress configuration API */ 432 pdev = of_find_device_by_node(np); 433 if (!pdev) { 434 drm_err(dev, "can't find the sysreg device, deferring\n"); 435 return -EPROBE_DEFER; 436 } 437 438 map = devm_regmap_init_vexpress_config(&pdev->dev); 439 if (IS_ERR(map)) { 440 platform_device_put(pdev); 441 return PTR_ERR(map); 442 } 443 444 ret = regmap_write(map, 0, val); 445 platform_device_put(pdev); 446 if (ret) { 447 drm_err(dev, "error setting DVI muxmode\n"); 448 return -ENODEV; 449 } 450 451 priv->variant = &pl111_vexpress; 452 drm_info(dev, "initializing Versatile Express PL111\n"); 453 454 return 0; 455 } 456 457 int pl111_versatile_init(struct drm_device *dev, struct pl111_drm_dev_private *priv) 458 { 459 const struct of_device_id *clcd_id; 460 enum versatile_clcd versatile_clcd_type; 461 struct device_node *np; 462 struct regmap *map; 463 464 np = of_find_matching_node_and_match(NULL, versatile_clcd_of_match, 465 &clcd_id); 466 if (!np) { 467 /* Non-ARM reference designs, just bail out */ 468 return 0; 469 } 470 471 versatile_clcd_type = (enum versatile_clcd)clcd_id->data; 472 473 /* Versatile Express special handling */ 474 if (versatile_clcd_type == VEXPRESS_CLCD_V2M) { 475 int ret = pl111_vexpress_clcd_init(dev, np, priv); 476 of_node_put(np); 477 if (ret) 478 drm_err(dev, "Versatile Express init failed - %d", ret); 479 return ret; 480 } 481 482 /* 483 * On the Integrator, check if we should use the IM-PD1 instead, 484 * if we find it, it will take precedence. This is on the Integrator/AP 485 * which only has this option for PL110 graphics. 486 */ 487 if (versatile_clcd_type == INTEGRATOR_CLCD_CM) { 488 np = of_find_matching_node_and_match(NULL, impd1_clcd_of_match, 489 &clcd_id); 490 if (np) 491 versatile_clcd_type = (enum versatile_clcd)clcd_id->data; 492 } 493 494 map = syscon_node_to_regmap(np); 495 of_node_put(np); 496 if (IS_ERR(map)) { 497 drm_err(dev, "no Versatile syscon regmap\n"); 498 return PTR_ERR(map); 499 } 500 501 switch (versatile_clcd_type) { 502 case INTEGRATOR_CLCD_CM: 503 versatile_syscon_map = map; 504 priv->variant = &pl110_integrator; 505 priv->variant_display_enable = pl111_integrator_enable; 506 drm_info(dev, "set up callbacks for Integrator PL110\n"); 507 break; 508 case INTEGRATOR_IMPD1: 509 versatile_syscon_map = map; 510 priv->variant = &pl110_impd1; 511 priv->variant_display_enable = pl111_impd1_enable; 512 priv->variant_display_disable = pl111_impd1_disable; 513 drm_info(dev, "set up callbacks for IM-PD1 PL110\n"); 514 break; 515 case VERSATILE_CLCD: 516 versatile_syscon_map = map; 517 /* This can do RGB565 with external PLD */ 518 priv->variant = &pl110_versatile; 519 priv->variant_display_enable = pl111_versatile_enable; 520 priv->variant_display_disable = pl111_versatile_disable; 521 /* 522 * The Versatile has a variant halfway between PL110 523 * and PL111 where these two registers have already been 524 * swapped. 525 */ 526 priv->ienb = CLCD_PL111_IENB; 527 priv->ctrl = CLCD_PL111_CNTL; 528 drm_info(dev, "set up callbacks for Versatile PL110\n"); 529 break; 530 case REALVIEW_CLCD_EB: 531 case REALVIEW_CLCD_PB1176: 532 case REALVIEW_CLCD_PB11MP: 533 case REALVIEW_CLCD_PBA8: 534 case REALVIEW_CLCD_PBX: 535 versatile_syscon_map = map; 536 priv->variant = &pl111_realview; 537 priv->variant_display_enable = pl111_realview_clcd_enable; 538 priv->variant_display_disable = pl111_realview_clcd_disable; 539 drm_info(dev, "set up callbacks for RealView PL111\n"); 540 break; 541 default: 542 drm_info(dev, "unknown Versatile system controller\n"); 543 break; 544 } 545 546 return 0; 547 } 548 EXPORT_SYMBOL_GPL(pl111_versatile_init); 549