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