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