1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include <linux/clk.h> 4 #include <linux/of_clk.h> 5 #include <linux/minmax.h> 6 #include <linux/platform_data/simplefb.h> 7 #include <linux/platform_device.h> 8 #include <linux/regulator/consumer.h> 9 10 #include <drm/drm_aperture.h> 11 #include <drm/drm_atomic.h> 12 #include <drm/drm_atomic_state_helper.h> 13 #include <drm/drm_connector.h> 14 #include <drm/drm_damage_helper.h> 15 #include <drm/drm_device.h> 16 #include <drm/drm_drv.h> 17 #include <drm/drm_fb_helper.h> 18 #include <drm/drm_format_helper.h> 19 #include <drm/drm_gem_atomic_helper.h> 20 #include <drm/drm_gem_framebuffer_helper.h> 21 #include <drm/drm_gem_shmem_helper.h> 22 #include <drm/drm_managed.h> 23 #include <drm/drm_modeset_helper_vtables.h> 24 #include <drm/drm_plane_helper.h> 25 #include <drm/drm_probe_helper.h> 26 27 #define DRIVER_NAME "simpledrm" 28 #define DRIVER_DESC "DRM driver for simple-framebuffer platform devices" 29 #define DRIVER_DATE "20200625" 30 #define DRIVER_MAJOR 1 31 #define DRIVER_MINOR 0 32 33 /* 34 * Helpers for simplefb 35 */ 36 37 static int 38 simplefb_get_validated_int(struct drm_device *dev, const char *name, 39 uint32_t value) 40 { 41 if (value > INT_MAX) { 42 drm_err(dev, "simplefb: invalid framebuffer %s of %u\n", 43 name, value); 44 return -EINVAL; 45 } 46 return (int)value; 47 } 48 49 static int 50 simplefb_get_validated_int0(struct drm_device *dev, const char *name, 51 uint32_t value) 52 { 53 if (!value) { 54 drm_err(dev, "simplefb: invalid framebuffer %s of %u\n", 55 name, value); 56 return -EINVAL; 57 } 58 return simplefb_get_validated_int(dev, name, value); 59 } 60 61 static const struct drm_format_info * 62 simplefb_get_validated_format(struct drm_device *dev, const char *format_name) 63 { 64 static const struct simplefb_format formats[] = SIMPLEFB_FORMATS; 65 const struct simplefb_format *fmt = formats; 66 const struct simplefb_format *end = fmt + ARRAY_SIZE(formats); 67 const struct drm_format_info *info; 68 69 if (!format_name) { 70 drm_err(dev, "simplefb: missing framebuffer format\n"); 71 return ERR_PTR(-EINVAL); 72 } 73 74 while (fmt < end) { 75 if (!strcmp(format_name, fmt->name)) { 76 info = drm_format_info(fmt->fourcc); 77 if (!info) 78 return ERR_PTR(-EINVAL); 79 return info; 80 } 81 ++fmt; 82 } 83 84 drm_err(dev, "simplefb: unknown framebuffer format %s\n", 85 format_name); 86 87 return ERR_PTR(-EINVAL); 88 } 89 90 static int 91 simplefb_get_width_pd(struct drm_device *dev, 92 const struct simplefb_platform_data *pd) 93 { 94 return simplefb_get_validated_int0(dev, "width", pd->width); 95 } 96 97 static int 98 simplefb_get_height_pd(struct drm_device *dev, 99 const struct simplefb_platform_data *pd) 100 { 101 return simplefb_get_validated_int0(dev, "height", pd->height); 102 } 103 104 static int 105 simplefb_get_stride_pd(struct drm_device *dev, 106 const struct simplefb_platform_data *pd) 107 { 108 return simplefb_get_validated_int(dev, "stride", pd->stride); 109 } 110 111 static const struct drm_format_info * 112 simplefb_get_format_pd(struct drm_device *dev, 113 const struct simplefb_platform_data *pd) 114 { 115 return simplefb_get_validated_format(dev, pd->format); 116 } 117 118 static int 119 simplefb_read_u32_of(struct drm_device *dev, struct device_node *of_node, 120 const char *name, u32 *value) 121 { 122 int ret = of_property_read_u32(of_node, name, value); 123 124 if (ret) 125 drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n", 126 name, ret); 127 return ret; 128 } 129 130 static int 131 simplefb_read_string_of(struct drm_device *dev, struct device_node *of_node, 132 const char *name, const char **value) 133 { 134 int ret = of_property_read_string(of_node, name, value); 135 136 if (ret) 137 drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n", 138 name, ret); 139 return ret; 140 } 141 142 static int 143 simplefb_get_width_of(struct drm_device *dev, struct device_node *of_node) 144 { 145 u32 width; 146 int ret = simplefb_read_u32_of(dev, of_node, "width", &width); 147 148 if (ret) 149 return ret; 150 return simplefb_get_validated_int0(dev, "width", width); 151 } 152 153 static int 154 simplefb_get_height_of(struct drm_device *dev, struct device_node *of_node) 155 { 156 u32 height; 157 int ret = simplefb_read_u32_of(dev, of_node, "height", &height); 158 159 if (ret) 160 return ret; 161 return simplefb_get_validated_int0(dev, "height", height); 162 } 163 164 static int 165 simplefb_get_stride_of(struct drm_device *dev, struct device_node *of_node) 166 { 167 u32 stride; 168 int ret = simplefb_read_u32_of(dev, of_node, "stride", &stride); 169 170 if (ret) 171 return ret; 172 return simplefb_get_validated_int(dev, "stride", stride); 173 } 174 175 static const struct drm_format_info * 176 simplefb_get_format_of(struct drm_device *dev, struct device_node *of_node) 177 { 178 const char *format; 179 int ret = simplefb_read_string_of(dev, of_node, "format", &format); 180 181 if (ret) 182 return ERR_PTR(ret); 183 return simplefb_get_validated_format(dev, format); 184 } 185 186 /* 187 * Simple Framebuffer device 188 */ 189 190 struct simpledrm_device { 191 struct drm_device dev; 192 193 /* clocks */ 194 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK 195 unsigned int clk_count; 196 struct clk **clks; 197 #endif 198 /* regulators */ 199 #if defined CONFIG_OF && defined CONFIG_REGULATOR 200 unsigned int regulator_count; 201 struct regulator **regulators; 202 #endif 203 204 /* simplefb settings */ 205 struct drm_display_mode mode; 206 const struct drm_format_info *format; 207 unsigned int pitch; 208 209 /* memory management */ 210 void __iomem *screen_base; 211 212 /* modesetting */ 213 uint32_t formats[8]; 214 size_t nformats; 215 struct drm_plane primary_plane; 216 struct drm_crtc crtc; 217 struct drm_encoder encoder; 218 struct drm_connector connector; 219 }; 220 221 static struct simpledrm_device *simpledrm_device_of_dev(struct drm_device *dev) 222 { 223 return container_of(dev, struct simpledrm_device, dev); 224 } 225 226 /* 227 * Hardware 228 */ 229 230 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK 231 /* 232 * Clock handling code. 233 * 234 * Here we handle the clocks property of our "simple-framebuffer" dt node. 235 * This is necessary so that we can make sure that any clocks needed by 236 * the display engine that the bootloader set up for us (and for which it 237 * provided a simplefb dt node), stay up, for the life of the simplefb 238 * driver. 239 * 240 * When the driver unloads, we cleanly disable, and then release the clocks. 241 * 242 * We only complain about errors here, no action is taken as the most likely 243 * error can only happen due to a mismatch between the bootloader which set 244 * up simplefb, and the clock definitions in the device tree. Chances are 245 * that there are no adverse effects, and if there are, a clean teardown of 246 * the fb probe will not help us much either. So just complain and carry on, 247 * and hope that the user actually gets a working fb at the end of things. 248 */ 249 250 static void simpledrm_device_release_clocks(void *res) 251 { 252 struct simpledrm_device *sdev = simpledrm_device_of_dev(res); 253 unsigned int i; 254 255 for (i = 0; i < sdev->clk_count; ++i) { 256 if (sdev->clks[i]) { 257 clk_disable_unprepare(sdev->clks[i]); 258 clk_put(sdev->clks[i]); 259 } 260 } 261 } 262 263 static int simpledrm_device_init_clocks(struct simpledrm_device *sdev) 264 { 265 struct drm_device *dev = &sdev->dev; 266 struct platform_device *pdev = to_platform_device(dev->dev); 267 struct device_node *of_node = pdev->dev.of_node; 268 struct clk *clock; 269 unsigned int i; 270 int ret; 271 272 if (dev_get_platdata(&pdev->dev) || !of_node) 273 return 0; 274 275 sdev->clk_count = of_clk_get_parent_count(of_node); 276 if (!sdev->clk_count) 277 return 0; 278 279 sdev->clks = drmm_kzalloc(dev, sdev->clk_count * sizeof(sdev->clks[0]), 280 GFP_KERNEL); 281 if (!sdev->clks) 282 return -ENOMEM; 283 284 for (i = 0; i < sdev->clk_count; ++i) { 285 clock = of_clk_get(of_node, i); 286 if (IS_ERR(clock)) { 287 ret = PTR_ERR(clock); 288 if (ret == -EPROBE_DEFER) 289 goto err; 290 drm_err(dev, "clock %u not found: %d\n", i, ret); 291 continue; 292 } 293 ret = clk_prepare_enable(clock); 294 if (ret) { 295 drm_err(dev, "failed to enable clock %u: %d\n", 296 i, ret); 297 clk_put(clock); 298 continue; 299 } 300 sdev->clks[i] = clock; 301 } 302 303 return devm_add_action_or_reset(&pdev->dev, 304 simpledrm_device_release_clocks, 305 sdev); 306 307 err: 308 while (i) { 309 --i; 310 if (sdev->clks[i]) { 311 clk_disable_unprepare(sdev->clks[i]); 312 clk_put(sdev->clks[i]); 313 } 314 } 315 return ret; 316 } 317 #else 318 static int simpledrm_device_init_clocks(struct simpledrm_device *sdev) 319 { 320 return 0; 321 } 322 #endif 323 324 #if defined CONFIG_OF && defined CONFIG_REGULATOR 325 326 #define SUPPLY_SUFFIX "-supply" 327 328 /* 329 * Regulator handling code. 330 * 331 * Here we handle the num-supplies and vin*-supply properties of our 332 * "simple-framebuffer" dt node. This is necessary so that we can make sure 333 * that any regulators needed by the display hardware that the bootloader 334 * set up for us (and for which it provided a simplefb dt node), stay up, 335 * for the life of the simplefb driver. 336 * 337 * When the driver unloads, we cleanly disable, and then release the 338 * regulators. 339 * 340 * We only complain about errors here, no action is taken as the most likely 341 * error can only happen due to a mismatch between the bootloader which set 342 * up simplefb, and the regulator definitions in the device tree. Chances are 343 * that there are no adverse effects, and if there are, a clean teardown of 344 * the fb probe will not help us much either. So just complain and carry on, 345 * and hope that the user actually gets a working fb at the end of things. 346 */ 347 348 static void simpledrm_device_release_regulators(void *res) 349 { 350 struct simpledrm_device *sdev = simpledrm_device_of_dev(res); 351 unsigned int i; 352 353 for (i = 0; i < sdev->regulator_count; ++i) { 354 if (sdev->regulators[i]) { 355 regulator_disable(sdev->regulators[i]); 356 regulator_put(sdev->regulators[i]); 357 } 358 } 359 } 360 361 static int simpledrm_device_init_regulators(struct simpledrm_device *sdev) 362 { 363 struct drm_device *dev = &sdev->dev; 364 struct platform_device *pdev = to_platform_device(dev->dev); 365 struct device_node *of_node = pdev->dev.of_node; 366 struct property *prop; 367 struct regulator *regulator; 368 const char *p; 369 unsigned int count = 0, i = 0; 370 int ret; 371 372 if (dev_get_platdata(&pdev->dev) || !of_node) 373 return 0; 374 375 /* Count the number of regulator supplies */ 376 for_each_property_of_node(of_node, prop) { 377 p = strstr(prop->name, SUPPLY_SUFFIX); 378 if (p && p != prop->name) 379 ++count; 380 } 381 382 if (!count) 383 return 0; 384 385 sdev->regulators = drmm_kzalloc(dev, 386 count * sizeof(sdev->regulators[0]), 387 GFP_KERNEL); 388 if (!sdev->regulators) 389 return -ENOMEM; 390 391 for_each_property_of_node(of_node, prop) { 392 char name[32]; /* 32 is max size of property name */ 393 size_t len; 394 395 p = strstr(prop->name, SUPPLY_SUFFIX); 396 if (!p || p == prop->name) 397 continue; 398 len = strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1; 399 strscpy(name, prop->name, min(sizeof(name), len)); 400 401 regulator = regulator_get_optional(&pdev->dev, name); 402 if (IS_ERR(regulator)) { 403 ret = PTR_ERR(regulator); 404 if (ret == -EPROBE_DEFER) 405 goto err; 406 drm_err(dev, "regulator %s not found: %d\n", 407 name, ret); 408 continue; 409 } 410 411 ret = regulator_enable(regulator); 412 if (ret) { 413 drm_err(dev, "failed to enable regulator %u: %d\n", 414 i, ret); 415 regulator_put(regulator); 416 continue; 417 } 418 419 sdev->regulators[i++] = regulator; 420 } 421 sdev->regulator_count = i; 422 423 return devm_add_action_or_reset(&pdev->dev, 424 simpledrm_device_release_regulators, 425 sdev); 426 427 err: 428 while (i) { 429 --i; 430 if (sdev->regulators[i]) { 431 regulator_disable(sdev->regulators[i]); 432 regulator_put(sdev->regulators[i]); 433 } 434 } 435 return ret; 436 } 437 #else 438 static int simpledrm_device_init_regulators(struct simpledrm_device *sdev) 439 { 440 return 0; 441 } 442 #endif 443 444 /* 445 * Modesetting 446 */ 447 448 /* 449 * Support all formats of simplefb and maybe more; in order 450 * of preference. The display's update function will do any 451 * conversion necessary. 452 * 453 * TODO: Add blit helpers for remaining formats and uncomment 454 * constants. 455 */ 456 static const uint32_t simpledrm_primary_plane_formats[] = { 457 DRM_FORMAT_XRGB8888, 458 DRM_FORMAT_ARGB8888, 459 DRM_FORMAT_RGB565, 460 //DRM_FORMAT_XRGB1555, 461 //DRM_FORMAT_ARGB1555, 462 DRM_FORMAT_RGB888, 463 DRM_FORMAT_XRGB2101010, 464 DRM_FORMAT_ARGB2101010, 465 }; 466 467 static const uint64_t simpledrm_primary_plane_format_modifiers[] = { 468 DRM_FORMAT_MOD_LINEAR, 469 DRM_FORMAT_MOD_INVALID 470 }; 471 472 static void simpledrm_primary_plane_helper_atomic_update(struct drm_plane *plane, 473 struct drm_atomic_state *state) 474 { 475 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane); 476 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane); 477 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state); 478 struct drm_framebuffer *fb = plane_state->fb; 479 struct drm_device *dev = plane->dev; 480 struct simpledrm_device *sdev = simpledrm_device_of_dev(dev); 481 struct drm_atomic_helper_damage_iter iter; 482 struct drm_rect damage; 483 int ret, idx; 484 485 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE); 486 if (ret) 487 return; 488 489 if (!drm_dev_enter(dev, &idx)) 490 goto out_drm_gem_fb_end_cpu_access; 491 492 drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state); 493 drm_atomic_for_each_plane_damage(&iter, &damage) { 494 struct iosys_map dst = IOSYS_MAP_INIT_VADDR(sdev->screen_base); 495 struct drm_rect dst_clip = plane_state->dst; 496 497 if (!drm_rect_intersect(&dst_clip, &damage)) 498 continue; 499 500 iosys_map_incr(&dst, drm_fb_clip_offset(sdev->pitch, sdev->format, &dst_clip)); 501 drm_fb_blit(&dst, &sdev->pitch, sdev->format->format, shadow_plane_state->data, fb, 502 &damage); 503 } 504 505 drm_dev_exit(idx); 506 out_drm_gem_fb_end_cpu_access: 507 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE); 508 } 509 510 static void simpledrm_primary_plane_helper_atomic_disable(struct drm_plane *plane, 511 struct drm_atomic_state *state) 512 { 513 struct drm_device *dev = plane->dev; 514 struct simpledrm_device *sdev = simpledrm_device_of_dev(dev); 515 int idx; 516 517 if (!drm_dev_enter(dev, &idx)) 518 return; 519 520 /* Clear screen to black if disabled */ 521 memset_io(sdev->screen_base, 0, sdev->pitch * sdev->mode.vdisplay); 522 523 drm_dev_exit(idx); 524 } 525 526 static const struct drm_plane_helper_funcs simpledrm_primary_plane_helper_funcs = { 527 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS, 528 .atomic_check = drm_plane_helper_atomic_check, 529 .atomic_update = simpledrm_primary_plane_helper_atomic_update, 530 .atomic_disable = simpledrm_primary_plane_helper_atomic_disable, 531 }; 532 533 static const struct drm_plane_funcs simpledrm_primary_plane_funcs = { 534 .update_plane = drm_atomic_helper_update_plane, 535 .disable_plane = drm_atomic_helper_disable_plane, 536 .destroy = drm_plane_cleanup, 537 DRM_GEM_SHADOW_PLANE_FUNCS, 538 }; 539 540 static enum drm_mode_status simpledrm_crtc_helper_mode_valid(struct drm_crtc *crtc, 541 const struct drm_display_mode *mode) 542 { 543 struct simpledrm_device *sdev = simpledrm_device_of_dev(crtc->dev); 544 545 return drm_crtc_helper_mode_valid_fixed(crtc, mode, &sdev->mode); 546 } 547 548 static int simpledrm_crtc_helper_atomic_check(struct drm_crtc *crtc, 549 struct drm_atomic_state *new_state) 550 { 551 struct drm_crtc_state *new_crtc_state = drm_atomic_get_new_crtc_state(new_state, crtc); 552 int ret; 553 554 if (!new_crtc_state->enable) 555 goto out; 556 557 ret = drm_atomic_helper_check_crtc_primary_plane(new_crtc_state); 558 if (ret) 559 return ret; 560 561 out: 562 return drm_atomic_add_affected_planes(new_state, crtc); 563 } 564 565 /* 566 * The CRTC is always enabled. Screen updates are performed by 567 * the primary plane's atomic_update function. Disabling clears 568 * the screen in the primary plane's atomic_disable function. 569 */ 570 static const struct drm_crtc_helper_funcs simpledrm_crtc_helper_funcs = { 571 .mode_valid = simpledrm_crtc_helper_mode_valid, 572 .atomic_check = simpledrm_crtc_helper_atomic_check, 573 }; 574 575 static const struct drm_crtc_funcs simpledrm_crtc_funcs = { 576 .reset = drm_atomic_helper_crtc_reset, 577 .destroy = drm_crtc_cleanup, 578 .set_config = drm_atomic_helper_set_config, 579 .page_flip = drm_atomic_helper_page_flip, 580 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, 581 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, 582 }; 583 584 static const struct drm_encoder_funcs simpledrm_encoder_funcs = { 585 .destroy = drm_encoder_cleanup, 586 }; 587 588 static int simpledrm_connector_helper_get_modes(struct drm_connector *connector) 589 { 590 struct simpledrm_device *sdev = simpledrm_device_of_dev(connector->dev); 591 592 return drm_connector_helper_get_modes_fixed(connector, &sdev->mode); 593 } 594 595 static const struct drm_connector_helper_funcs simpledrm_connector_helper_funcs = { 596 .get_modes = simpledrm_connector_helper_get_modes, 597 }; 598 599 static const struct drm_connector_funcs simpledrm_connector_funcs = { 600 .reset = drm_atomic_helper_connector_reset, 601 .fill_modes = drm_helper_probe_single_connector_modes, 602 .destroy = drm_connector_cleanup, 603 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 604 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 605 }; 606 607 static const struct drm_mode_config_funcs simpledrm_mode_config_funcs = { 608 .fb_create = drm_gem_fb_create_with_dirty, 609 .atomic_check = drm_atomic_helper_check, 610 .atomic_commit = drm_atomic_helper_commit, 611 }; 612 613 /* 614 * Init / Cleanup 615 */ 616 617 static struct drm_display_mode simpledrm_mode(unsigned int width, 618 unsigned int height) 619 { 620 /* 621 * Assume a monitor resolution of 96 dpi to 622 * get a somewhat reasonable screen size. 623 */ 624 const struct drm_display_mode mode = { 625 DRM_MODE_INIT(60, width, height, 626 DRM_MODE_RES_MM(width, 96ul), 627 DRM_MODE_RES_MM(height, 96ul)) 628 }; 629 630 return mode; 631 } 632 633 static struct simpledrm_device *simpledrm_device_create(struct drm_driver *drv, 634 struct platform_device *pdev) 635 { 636 const struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev); 637 struct device_node *of_node = pdev->dev.of_node; 638 struct simpledrm_device *sdev; 639 struct drm_device *dev; 640 int width, height, stride; 641 const struct drm_format_info *format; 642 struct resource *res, *mem; 643 void __iomem *screen_base; 644 struct drm_plane *primary_plane; 645 struct drm_crtc *crtc; 646 struct drm_encoder *encoder; 647 struct drm_connector *connector; 648 unsigned long max_width, max_height; 649 size_t nformats; 650 int ret; 651 652 sdev = devm_drm_dev_alloc(&pdev->dev, drv, struct simpledrm_device, dev); 653 if (IS_ERR(sdev)) 654 return ERR_CAST(sdev); 655 dev = &sdev->dev; 656 platform_set_drvdata(pdev, sdev); 657 658 /* 659 * Hardware settings 660 */ 661 662 ret = simpledrm_device_init_clocks(sdev); 663 if (ret) 664 return ERR_PTR(ret); 665 ret = simpledrm_device_init_regulators(sdev); 666 if (ret) 667 return ERR_PTR(ret); 668 669 if (pd) { 670 width = simplefb_get_width_pd(dev, pd); 671 if (width < 0) 672 return ERR_PTR(width); 673 height = simplefb_get_height_pd(dev, pd); 674 if (height < 0) 675 return ERR_PTR(height); 676 stride = simplefb_get_stride_pd(dev, pd); 677 if (stride < 0) 678 return ERR_PTR(stride); 679 format = simplefb_get_format_pd(dev, pd); 680 if (IS_ERR(format)) 681 return ERR_CAST(format); 682 } else if (of_node) { 683 width = simplefb_get_width_of(dev, of_node); 684 if (width < 0) 685 return ERR_PTR(width); 686 height = simplefb_get_height_of(dev, of_node); 687 if (height < 0) 688 return ERR_PTR(height); 689 stride = simplefb_get_stride_of(dev, of_node); 690 if (stride < 0) 691 return ERR_PTR(stride); 692 format = simplefb_get_format_of(dev, of_node); 693 if (IS_ERR(format)) 694 return ERR_CAST(format); 695 } else { 696 drm_err(dev, "no simplefb configuration found\n"); 697 return ERR_PTR(-ENODEV); 698 } 699 if (!stride) { 700 stride = drm_format_info_min_pitch(format, 0, width); 701 if (drm_WARN_ON(dev, !stride)) 702 return ERR_PTR(-EINVAL); 703 } 704 705 sdev->mode = simpledrm_mode(width, height); 706 sdev->format = format; 707 sdev->pitch = stride; 708 709 drm_dbg(dev, "display mode={" DRM_MODE_FMT "}\n", DRM_MODE_ARG(&sdev->mode)); 710 drm_dbg(dev, "framebuffer format=%p4cc, size=%dx%d, stride=%d byte\n", 711 &format->format, width, height, stride); 712 713 /* 714 * Memory management 715 */ 716 717 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 718 if (!res) 719 return ERR_PTR(-EINVAL); 720 721 ret = devm_aperture_acquire_from_firmware(dev, res->start, resource_size(res)); 722 if (ret) { 723 drm_err(dev, "could not acquire memory range %pr: error %d\n", res, ret); 724 return ERR_PTR(ret); 725 } 726 727 mem = devm_request_mem_region(&pdev->dev, res->start, resource_size(res), drv->name); 728 if (!mem) { 729 /* 730 * We cannot make this fatal. Sometimes this comes from magic 731 * spaces our resource handlers simply don't know about. Use 732 * the I/O-memory resource as-is and try to map that instead. 733 */ 734 drm_warn(dev, "could not acquire memory region %pr\n", res); 735 mem = res; 736 } 737 738 screen_base = devm_ioremap_wc(&pdev->dev, mem->start, resource_size(mem)); 739 if (!screen_base) 740 return ERR_PTR(-ENOMEM); 741 sdev->screen_base = screen_base; 742 743 /* 744 * Modesetting 745 */ 746 747 ret = drmm_mode_config_init(dev); 748 if (ret) 749 return ERR_PTR(ret); 750 751 max_width = max_t(unsigned long, width, DRM_SHADOW_PLANE_MAX_WIDTH); 752 max_height = max_t(unsigned long, height, DRM_SHADOW_PLANE_MAX_HEIGHT); 753 754 dev->mode_config.min_width = width; 755 dev->mode_config.max_width = max_width; 756 dev->mode_config.min_height = height; 757 dev->mode_config.max_height = max_height; 758 dev->mode_config.preferred_depth = format->cpp[0] * 8; 759 dev->mode_config.funcs = &simpledrm_mode_config_funcs; 760 761 /* Primary plane */ 762 763 nformats = drm_fb_build_fourcc_list(dev, &format->format, 1, 764 simpledrm_primary_plane_formats, 765 ARRAY_SIZE(simpledrm_primary_plane_formats), 766 sdev->formats, ARRAY_SIZE(sdev->formats)); 767 768 primary_plane = &sdev->primary_plane; 769 ret = drm_universal_plane_init(dev, primary_plane, 0, &simpledrm_primary_plane_funcs, 770 sdev->formats, nformats, 771 simpledrm_primary_plane_format_modifiers, 772 DRM_PLANE_TYPE_PRIMARY, NULL); 773 if (ret) 774 return ERR_PTR(ret); 775 drm_plane_helper_add(primary_plane, &simpledrm_primary_plane_helper_funcs); 776 drm_plane_enable_fb_damage_clips(primary_plane); 777 778 /* CRTC */ 779 780 crtc = &sdev->crtc; 781 ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL, 782 &simpledrm_crtc_funcs, NULL); 783 if (ret) 784 return ERR_PTR(ret); 785 drm_crtc_helper_add(crtc, &simpledrm_crtc_helper_funcs); 786 787 /* Encoder */ 788 789 encoder = &sdev->encoder; 790 ret = drm_encoder_init(dev, encoder, &simpledrm_encoder_funcs, 791 DRM_MODE_ENCODER_NONE, NULL); 792 if (ret) 793 return ERR_PTR(ret); 794 encoder->possible_crtcs = drm_crtc_mask(crtc); 795 796 /* Connector */ 797 798 connector = &sdev->connector; 799 ret = drm_connector_init(dev, connector, &simpledrm_connector_funcs, 800 DRM_MODE_CONNECTOR_Unknown); 801 if (ret) 802 return ERR_PTR(ret); 803 drm_connector_helper_add(connector, &simpledrm_connector_helper_funcs); 804 drm_connector_set_panel_orientation_with_quirk(connector, 805 DRM_MODE_PANEL_ORIENTATION_UNKNOWN, 806 width, height); 807 808 ret = drm_connector_attach_encoder(connector, encoder); 809 if (ret) 810 return ERR_PTR(ret); 811 812 drm_mode_config_reset(dev); 813 814 return sdev; 815 } 816 817 /* 818 * DRM driver 819 */ 820 821 DEFINE_DRM_GEM_FOPS(simpledrm_fops); 822 823 static struct drm_driver simpledrm_driver = { 824 DRM_GEM_SHMEM_DRIVER_OPS, 825 .name = DRIVER_NAME, 826 .desc = DRIVER_DESC, 827 .date = DRIVER_DATE, 828 .major = DRIVER_MAJOR, 829 .minor = DRIVER_MINOR, 830 .driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET, 831 .fops = &simpledrm_fops, 832 }; 833 834 /* 835 * Platform driver 836 */ 837 838 static int simpledrm_probe(struct platform_device *pdev) 839 { 840 struct simpledrm_device *sdev; 841 struct drm_device *dev; 842 int ret; 843 844 sdev = simpledrm_device_create(&simpledrm_driver, pdev); 845 if (IS_ERR(sdev)) 846 return PTR_ERR(sdev); 847 dev = &sdev->dev; 848 849 ret = drm_dev_register(dev, 0); 850 if (ret) 851 return ret; 852 853 drm_fbdev_generic_setup(dev, 0); 854 855 return 0; 856 } 857 858 static int simpledrm_remove(struct platform_device *pdev) 859 { 860 struct simpledrm_device *sdev = platform_get_drvdata(pdev); 861 struct drm_device *dev = &sdev->dev; 862 863 drm_dev_unplug(dev); 864 865 return 0; 866 } 867 868 static const struct of_device_id simpledrm_of_match_table[] = { 869 { .compatible = "simple-framebuffer", }, 870 { }, 871 }; 872 MODULE_DEVICE_TABLE(of, simpledrm_of_match_table); 873 874 static struct platform_driver simpledrm_platform_driver = { 875 .driver = { 876 .name = "simple-framebuffer", /* connect to sysfb */ 877 .of_match_table = simpledrm_of_match_table, 878 }, 879 .probe = simpledrm_probe, 880 .remove = simpledrm_remove, 881 }; 882 883 module_platform_driver(simpledrm_platform_driver); 884 885 MODULE_DESCRIPTION(DRIVER_DESC); 886 MODULE_LICENSE("GPL v2"); 887