1 /* 2 * Copyright (C) 2013, NVIDIA Corporation. All rights reserved. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sub license, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the 12 * next paragraph) shall be included in all copies or substantial portions 13 * of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21 * DEALINGS IN THE SOFTWARE. 22 */ 23 24 #include <linux/backlight.h> 25 #include <linux/err.h> 26 #include <linux/export.h> 27 #include <linux/module.h> 28 #include <linux/of.h> 29 30 #include <drm/drm_crtc.h> 31 #include <drm/drm_of.h> 32 #include <drm/drm_panel.h> 33 #include <drm/drm_print.h> 34 35 static DEFINE_MUTEX(panel_lock); 36 static LIST_HEAD(panel_list); 37 38 /** 39 * DOC: drm panel 40 * 41 * The DRM panel helpers allow drivers to register panel objects with a 42 * central registry and provide functions to retrieve those panels in display 43 * drivers. 44 * 45 * For easy integration into drivers using the &drm_bridge infrastructure please 46 * take look at drm_panel_bridge_add() and devm_drm_panel_bridge_add(). 47 */ 48 49 /** 50 * drm_panel_init - initialize a panel 51 * @panel: DRM panel 52 * @dev: parent device of the panel 53 * @funcs: panel operations 54 * @connector_type: the connector type (DRM_MODE_CONNECTOR_*) corresponding to 55 * the panel interface (must NOT be DRM_MODE_CONNECTOR_Unknown) 56 * 57 * Initialize the panel structure for subsequent registration with 58 * drm_panel_add(). 59 */ 60 static void drm_panel_init(struct drm_panel *panel, struct device *dev, 61 const struct drm_panel_funcs *funcs, 62 int connector_type) 63 { 64 if (connector_type == DRM_MODE_CONNECTOR_Unknown) 65 DRM_WARN("%s: %s: a valid connector type is required!\n", __func__, dev_name(dev)); 66 67 INIT_LIST_HEAD(&panel->list); 68 INIT_LIST_HEAD(&panel->followers); 69 mutex_init(&panel->follower_lock); 70 panel->dev = dev; 71 panel->funcs = funcs; 72 panel->connector_type = connector_type; 73 } 74 75 /** 76 * drm_panel_add - add a panel to the global registry 77 * @panel: panel to add 78 * 79 * Add a panel to the global registry so that it can be looked 80 * up by display drivers. The panel to be added must have been 81 * allocated by devm_drm_panel_alloc(). 82 */ 83 void drm_panel_add(struct drm_panel *panel) 84 { 85 mutex_lock(&panel_lock); 86 list_add_tail(&panel->list, &panel_list); 87 mutex_unlock(&panel_lock); 88 } 89 EXPORT_SYMBOL(drm_panel_add); 90 91 /** 92 * drm_panel_remove - remove a panel from the global registry 93 * @panel: DRM panel 94 * 95 * Removes a panel from the global registry. 96 */ 97 void drm_panel_remove(struct drm_panel *panel) 98 { 99 mutex_lock(&panel_lock); 100 list_del_init(&panel->list); 101 mutex_unlock(&panel_lock); 102 } 103 EXPORT_SYMBOL(drm_panel_remove); 104 105 static void drm_panel_add_release(void *data) 106 { 107 drm_panel_remove(data); 108 } 109 110 /** 111 * devm_drm_panel_add - add a panel to the global registry using devres 112 * @dev: device to which the panel is attached 113 * @panel: panel to add 114 * 115 * Add a panel to the global registry so that it can be looked 116 * up by display drivers. The panel to be added must have been 117 * allocated by devm_drm_panel_alloc(). Unlike drm_panel_add() with this 118 * function there is no need to call drm_panel_remove(), it will be called 119 * automatically. 120 */ 121 int devm_drm_panel_add(struct device *dev, struct drm_panel *panel) 122 { 123 drm_panel_add(panel); 124 125 return devm_add_action_or_reset(dev, drm_panel_add_release, panel); 126 } 127 EXPORT_SYMBOL(devm_drm_panel_add); 128 129 /** 130 * drm_panel_prepare - power on a panel 131 * @panel: DRM panel 132 * 133 * Calling this function will enable power and deassert any reset signals to 134 * the panel. After this has completed it is possible to communicate with any 135 * integrated circuitry via a command bus. This function cannot fail (as it is 136 * called from the pre_enable call chain). There will always be a call to 137 * drm_panel_disable() afterwards. 138 */ 139 void drm_panel_prepare(struct drm_panel *panel) 140 { 141 struct drm_panel_follower *follower; 142 int ret; 143 144 if (!panel) 145 return; 146 147 if (panel->prepared) { 148 dev_warn(panel->dev, "Skipping prepare of already prepared panel\n"); 149 return; 150 } 151 152 mutex_lock(&panel->follower_lock); 153 154 if (panel->funcs && panel->funcs->prepare) { 155 ret = panel->funcs->prepare(panel); 156 if (ret < 0) 157 goto exit; 158 } 159 panel->prepared = true; 160 161 list_for_each_entry(follower, &panel->followers, list) { 162 if (!follower->funcs->panel_prepared) 163 continue; 164 165 ret = follower->funcs->panel_prepared(follower); 166 if (ret < 0) 167 dev_info(panel->dev, "%ps failed: %d\n", 168 follower->funcs->panel_prepared, ret); 169 } 170 171 exit: 172 mutex_unlock(&panel->follower_lock); 173 } 174 EXPORT_SYMBOL(drm_panel_prepare); 175 176 /** 177 * drm_panel_unprepare - power off a panel 178 * @panel: DRM panel 179 * 180 * Calling this function will completely power off a panel (assert the panel's 181 * reset, turn off power supplies, ...). After this function has completed, it 182 * is usually no longer possible to communicate with the panel until another 183 * call to drm_panel_prepare(). 184 */ 185 void drm_panel_unprepare(struct drm_panel *panel) 186 { 187 struct drm_panel_follower *follower; 188 int ret; 189 190 if (!panel) 191 return; 192 193 /* 194 * If you are seeing the warning below it likely means one of two things: 195 * - Your panel driver incorrectly calls drm_panel_unprepare() in its 196 * shutdown routine. You should delete this. 197 * - You are using panel-edp or panel-simple and your DRM modeset 198 * driver's shutdown() callback happened after the panel's shutdown(). 199 * In this case the warning is harmless though ideally you should 200 * figure out how to reverse the order of the shutdown() callbacks. 201 */ 202 if (!panel->prepared) { 203 dev_warn(panel->dev, "Skipping unprepare of already unprepared panel\n"); 204 return; 205 } 206 207 mutex_lock(&panel->follower_lock); 208 209 list_for_each_entry(follower, &panel->followers, list) { 210 if (!follower->funcs->panel_unpreparing) 211 continue; 212 213 ret = follower->funcs->panel_unpreparing(follower); 214 if (ret < 0) 215 dev_info(panel->dev, "%ps failed: %d\n", 216 follower->funcs->panel_unpreparing, ret); 217 } 218 219 if (panel->funcs && panel->funcs->unprepare) { 220 ret = panel->funcs->unprepare(panel); 221 if (ret < 0) 222 goto exit; 223 } 224 panel->prepared = false; 225 226 exit: 227 mutex_unlock(&panel->follower_lock); 228 } 229 EXPORT_SYMBOL(drm_panel_unprepare); 230 231 /** 232 * drm_panel_enable - enable a panel 233 * @panel: DRM panel 234 * 235 * Calling this function will cause the panel display drivers to be turned on 236 * and the backlight to be enabled. Content will be visible on screen after 237 * this call completes. This function cannot fail (as it is called from the 238 * enable call chain). There will always be a call to drm_panel_disable() 239 * afterwards. 240 */ 241 void drm_panel_enable(struct drm_panel *panel) 242 { 243 struct drm_panel_follower *follower; 244 int ret; 245 246 if (!panel) 247 return; 248 249 if (panel->enabled) { 250 dev_warn(panel->dev, "Skipping enable of already enabled panel\n"); 251 return; 252 } 253 254 mutex_lock(&panel->follower_lock); 255 256 if (panel->funcs && panel->funcs->enable) { 257 ret = panel->funcs->enable(panel); 258 if (ret < 0) 259 goto exit; 260 } 261 panel->enabled = true; 262 263 ret = backlight_enable(panel->backlight); 264 if (ret < 0) 265 DRM_DEV_INFO(panel->dev, "failed to enable backlight: %d\n", 266 ret); 267 268 list_for_each_entry(follower, &panel->followers, list) { 269 if (!follower->funcs->panel_enabled) 270 continue; 271 272 ret = follower->funcs->panel_enabled(follower); 273 if (ret < 0) 274 dev_info(panel->dev, "%ps failed: %d\n", 275 follower->funcs->panel_enabled, ret); 276 } 277 278 exit: 279 mutex_unlock(&panel->follower_lock); 280 } 281 EXPORT_SYMBOL(drm_panel_enable); 282 283 /** 284 * drm_panel_disable - disable a panel 285 * @panel: DRM panel 286 * 287 * This will typically turn off the panel's backlight or disable the display 288 * drivers. For smart panels it should still be possible to communicate with 289 * the integrated circuitry via any command bus after this call. 290 */ 291 void drm_panel_disable(struct drm_panel *panel) 292 { 293 struct drm_panel_follower *follower; 294 int ret; 295 296 if (!panel) 297 return; 298 299 /* 300 * If you are seeing the warning below it likely means one of two things: 301 * - Your panel driver incorrectly calls drm_panel_disable() in its 302 * shutdown routine. You should delete this. 303 * - You are using panel-edp or panel-simple and your DRM modeset 304 * driver's shutdown() callback happened after the panel's shutdown(). 305 * In this case the warning is harmless though ideally you should 306 * figure out how to reverse the order of the shutdown() callbacks. 307 */ 308 if (!panel->enabled) { 309 dev_warn(panel->dev, "Skipping disable of already disabled panel\n"); 310 return; 311 } 312 313 mutex_lock(&panel->follower_lock); 314 315 list_for_each_entry(follower, &panel->followers, list) { 316 if (!follower->funcs->panel_disabling) 317 continue; 318 319 ret = follower->funcs->panel_disabling(follower); 320 if (ret < 0) 321 dev_info(panel->dev, "%ps failed: %d\n", 322 follower->funcs->panel_disabling, ret); 323 } 324 325 ret = backlight_disable(panel->backlight); 326 if (ret < 0) 327 DRM_DEV_INFO(panel->dev, "failed to disable backlight: %d\n", 328 ret); 329 330 if (panel->funcs && panel->funcs->disable) { 331 ret = panel->funcs->disable(panel); 332 if (ret < 0) 333 goto exit; 334 } 335 panel->enabled = false; 336 337 exit: 338 mutex_unlock(&panel->follower_lock); 339 } 340 EXPORT_SYMBOL(drm_panel_disable); 341 342 /** 343 * drm_panel_get_modes - probe the available display modes of a panel 344 * @panel: DRM panel 345 * @connector: DRM connector 346 * 347 * The modes probed from the panel are automatically added to the connector 348 * that the panel is attached to. 349 * 350 * Return: The number of modes available from the panel on success, or 0 on 351 * failure (no modes). 352 */ 353 int drm_panel_get_modes(struct drm_panel *panel, 354 struct drm_connector *connector) 355 { 356 if (!panel) 357 return 0; 358 359 if (panel->funcs && panel->funcs->get_modes) { 360 int num; 361 362 num = panel->funcs->get_modes(panel, connector); 363 if (num > 0) 364 return num; 365 } 366 367 return 0; 368 } 369 EXPORT_SYMBOL(drm_panel_get_modes); 370 371 static void __drm_panel_free(struct kref *kref) 372 { 373 struct drm_panel *panel = container_of(kref, struct drm_panel, refcount); 374 375 kfree(panel->container); 376 } 377 378 /** 379 * drm_panel_get - Acquire a panel reference 380 * @panel: DRM panel 381 * 382 * This function increments the panel's refcount. 383 * Returns: 384 * Pointer to @panel 385 */ 386 struct drm_panel *drm_panel_get(struct drm_panel *panel) 387 { 388 if (!panel) 389 return panel; 390 391 kref_get(&panel->refcount); 392 393 return panel; 394 } 395 EXPORT_SYMBOL(drm_panel_get); 396 397 /** 398 * drm_panel_put - Release a panel reference 399 * @panel: DRM panel 400 * 401 * This function decrements the panel's reference count and frees the 402 * object if the reference count drops to zero. 403 */ 404 void drm_panel_put(struct drm_panel *panel) 405 { 406 if (panel) 407 kref_put(&panel->refcount, __drm_panel_free); 408 } 409 EXPORT_SYMBOL(drm_panel_put); 410 411 /** 412 * drm_panel_put_void - wrapper to drm_panel_put() taking a void pointer 413 * 414 * @data: pointer to @struct drm_panel, cast to a void pointer 415 * 416 * Wrapper of drm_panel_put() to be used when a function taking a void 417 * pointer is needed, for example as a devm action. 418 */ 419 static void drm_panel_put_void(void *data) 420 { 421 struct drm_panel *panel = (struct drm_panel *)data; 422 423 drm_panel_put(panel); 424 } 425 426 void *__devm_drm_panel_alloc(struct device *dev, size_t size, size_t offset, 427 const struct drm_panel_funcs *funcs, 428 int connector_type) 429 { 430 void *container; 431 struct drm_panel *panel; 432 int err; 433 434 if (!funcs) { 435 dev_warn(dev, "Missing funcs pointer\n"); 436 return ERR_PTR(-EINVAL); 437 } 438 439 container = kzalloc(size, GFP_KERNEL); 440 if (!container) 441 return ERR_PTR(-ENOMEM); 442 443 panel = container + offset; 444 panel->container = container; 445 panel->funcs = funcs; 446 kref_init(&panel->refcount); 447 448 err = devm_add_action_or_reset(dev, drm_panel_put_void, panel); 449 if (err) 450 return ERR_PTR(err); 451 452 drm_panel_init(panel, dev, funcs, connector_type); 453 454 return container; 455 } 456 EXPORT_SYMBOL(__devm_drm_panel_alloc); 457 458 #ifdef CONFIG_OF 459 /** 460 * of_drm_find_panel - look up a panel using a device tree node 461 * @np: device tree node of the panel 462 * 463 * Searches the set of registered panels for one that matches the given device 464 * tree node. If a matching panel is found, return a pointer to it. 465 * 466 * Return: A pointer to the panel registered for the specified device tree 467 * node or an ERR_PTR() if no panel matching the device tree node can be found. 468 * 469 * Possible error codes returned by this function: 470 * 471 * - EPROBE_DEFER: the panel device has not been probed yet, and the caller 472 * should retry later 473 * - ENODEV: the device is not available (status != "okay" or "ok") 474 */ 475 struct drm_panel *of_drm_find_panel(const struct device_node *np) 476 { 477 struct drm_panel *panel; 478 479 if (!of_device_is_available(np)) 480 return ERR_PTR(-ENODEV); 481 482 mutex_lock(&panel_lock); 483 484 list_for_each_entry(panel, &panel_list, list) { 485 if (panel->dev->of_node == np) { 486 mutex_unlock(&panel_lock); 487 return panel; 488 } 489 } 490 491 mutex_unlock(&panel_lock); 492 return ERR_PTR(-EPROBE_DEFER); 493 } 494 EXPORT_SYMBOL(of_drm_find_panel); 495 #endif 496 497 /* Find panel by fwnode. This should be identical to of_drm_find_panel(). */ 498 static struct drm_panel *find_panel_by_fwnode(const struct fwnode_handle *fwnode) 499 { 500 struct drm_panel *panel; 501 502 if (!fwnode_device_is_available(fwnode)) 503 return ERR_PTR(-ENODEV); 504 505 mutex_lock(&panel_lock); 506 507 list_for_each_entry(panel, &panel_list, list) { 508 if (dev_fwnode(panel->dev) == fwnode) { 509 mutex_unlock(&panel_lock); 510 return panel; 511 } 512 } 513 514 mutex_unlock(&panel_lock); 515 516 return ERR_PTR(-EPROBE_DEFER); 517 } 518 519 /* Find panel by follower device */ 520 static struct drm_panel *find_panel_by_dev(struct device *follower_dev) 521 { 522 struct fwnode_handle *fwnode; 523 struct drm_panel *panel; 524 525 fwnode = fwnode_find_reference(dev_fwnode(follower_dev), "panel", 0); 526 if (IS_ERR(fwnode)) 527 return ERR_PTR(-ENODEV); 528 529 panel = find_panel_by_fwnode(fwnode); 530 fwnode_handle_put(fwnode); 531 532 return panel; 533 } 534 535 /** 536 * drm_is_panel_follower() - Check if the device is a panel follower 537 * @dev: The 'struct device' to check 538 * 539 * This checks to see if a device needs to be power sequenced together with 540 * a panel using the panel follower API. 541 * 542 * The "panel" property of the follower points to the panel to be followed. 543 * 544 * Return: true if we should be power sequenced with a panel; false otherwise. 545 */ 546 bool drm_is_panel_follower(struct device *dev) 547 { 548 /* 549 * The "panel" property is actually a phandle, but for simplicity we 550 * don't bother trying to parse it here. We just need to know if the 551 * property is there. 552 */ 553 return device_property_present(dev, "panel"); 554 } 555 EXPORT_SYMBOL(drm_is_panel_follower); 556 557 /** 558 * drm_panel_add_follower() - Register something to follow panel state. 559 * @follower_dev: The 'struct device' for the follower. 560 * @follower: The panel follower descriptor for the follower. 561 * 562 * A panel follower is called right after preparing/enabling the panel and right 563 * before unpreparing/disabling the panel. It's primary intention is to power on 564 * an associated touchscreen, though it could be used for any similar devices. 565 * Multiple devices are allowed the follow the same panel. 566 * 567 * If a follower is added to a panel that's already been prepared/enabled, the 568 * follower's prepared/enabled callback is called right away. 569 * 570 * The "panel" property of the follower points to the panel to be followed. 571 * 572 * Return: 0 or an error code. Note that -ENODEV means that we detected that 573 * follower_dev is not actually following a panel. The caller may 574 * choose to ignore this return value if following a panel is optional. 575 */ 576 int drm_panel_add_follower(struct device *follower_dev, 577 struct drm_panel_follower *follower) 578 { 579 struct drm_panel *panel; 580 int ret; 581 582 panel = find_panel_by_dev(follower_dev); 583 if (IS_ERR(panel)) 584 return PTR_ERR(panel); 585 586 get_device(panel->dev); 587 follower->panel = panel; 588 589 mutex_lock(&panel->follower_lock); 590 591 list_add_tail(&follower->list, &panel->followers); 592 if (panel->prepared && follower->funcs->panel_prepared) { 593 ret = follower->funcs->panel_prepared(follower); 594 if (ret < 0) 595 dev_info(panel->dev, "%ps failed: %d\n", 596 follower->funcs->panel_prepared, ret); 597 } 598 if (panel->enabled && follower->funcs->panel_enabled) { 599 ret = follower->funcs->panel_enabled(follower); 600 if (ret < 0) 601 dev_info(panel->dev, "%ps failed: %d\n", 602 follower->funcs->panel_enabled, ret); 603 } 604 605 mutex_unlock(&panel->follower_lock); 606 607 return 0; 608 } 609 EXPORT_SYMBOL(drm_panel_add_follower); 610 611 /** 612 * drm_panel_remove_follower() - Reverse drm_panel_add_follower(). 613 * @follower: The panel follower descriptor for the follower. 614 * 615 * Undo drm_panel_add_follower(). This includes calling the follower's 616 * unpreparing/disabling function if we're removed from a panel that's currently 617 * prepared/enabled. 618 * 619 * Return: 0 or an error code. 620 */ 621 void drm_panel_remove_follower(struct drm_panel_follower *follower) 622 { 623 struct drm_panel *panel = follower->panel; 624 int ret; 625 626 mutex_lock(&panel->follower_lock); 627 628 if (panel->enabled && follower->funcs->panel_disabling) { 629 ret = follower->funcs->panel_disabling(follower); 630 if (ret < 0) 631 dev_info(panel->dev, "%ps failed: %d\n", 632 follower->funcs->panel_disabling, ret); 633 } 634 if (panel->prepared && follower->funcs->panel_unpreparing) { 635 ret = follower->funcs->panel_unpreparing(follower); 636 if (ret < 0) 637 dev_info(panel->dev, "%ps failed: %d\n", 638 follower->funcs->panel_unpreparing, ret); 639 } 640 list_del_init(&follower->list); 641 642 mutex_unlock(&panel->follower_lock); 643 644 put_device(panel->dev); 645 } 646 EXPORT_SYMBOL(drm_panel_remove_follower); 647 648 static void drm_panel_remove_follower_void(void *follower) 649 { 650 drm_panel_remove_follower(follower); 651 } 652 653 /** 654 * devm_drm_panel_add_follower() - devm version of drm_panel_add_follower() 655 * @follower_dev: The 'struct device' for the follower. 656 * @follower: The panel follower descriptor for the follower. 657 * 658 * Handles calling drm_panel_remove_follower() using devm on the follower_dev. 659 * 660 * Return: 0 or an error code. 661 */ 662 int devm_drm_panel_add_follower(struct device *follower_dev, 663 struct drm_panel_follower *follower) 664 { 665 int ret; 666 667 ret = drm_panel_add_follower(follower_dev, follower); 668 if (ret) 669 return ret; 670 671 return devm_add_action_or_reset(follower_dev, 672 drm_panel_remove_follower_void, follower); 673 } 674 EXPORT_SYMBOL(devm_drm_panel_add_follower); 675 676 #if IS_REACHABLE(CONFIG_BACKLIGHT_CLASS_DEVICE) 677 /** 678 * drm_panel_of_backlight - use backlight device node for backlight 679 * @panel: DRM panel 680 * 681 * Use this function to enable backlight handling if your panel 682 * uses device tree and has a backlight phandle. 683 * 684 * When the panel is enabled backlight will be enabled after a 685 * successful call to &drm_panel_funcs.enable() 686 * 687 * When the panel is disabled backlight will be disabled before the 688 * call to &drm_panel_funcs.disable(). 689 * 690 * A typical implementation for a panel driver supporting device tree 691 * will call this function at probe time. Backlight will then be handled 692 * transparently without requiring any intervention from the driver. 693 * 694 * Return: 0 on success or a negative error code on failure. 695 */ 696 int drm_panel_of_backlight(struct drm_panel *panel) 697 { 698 struct backlight_device *backlight; 699 700 if (!panel || !panel->dev) 701 return -EINVAL; 702 703 backlight = devm_of_find_backlight(panel->dev); 704 705 if (IS_ERR(backlight)) 706 return PTR_ERR(backlight); 707 708 panel->backlight = backlight; 709 return 0; 710 } 711 EXPORT_SYMBOL(drm_panel_of_backlight); 712 #endif 713 714 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>"); 715 MODULE_DESCRIPTION("DRM panel infrastructure"); 716 MODULE_LICENSE("GPL and additional rights"); 717