sysfb.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Generic System Framebuffers
  4. * Copyright (c) 2012-2013 David Herrmann <[email protected]>
  5. */
  6. /*
  7. * Simple-Framebuffer support
  8. * Create a platform-device for any available boot framebuffer. The
  9. * simple-framebuffer platform device is already available on DT systems, so
  10. * this module parses the global "screen_info" object and creates a suitable
  11. * platform device compatible with the "simple-framebuffer" DT object. If
  12. * the framebuffer is incompatible, we instead create a legacy
  13. * "vesa-framebuffer", "efi-framebuffer" or "platform-framebuffer" device and
  14. * pass the screen_info as platform_data. This allows legacy drivers
  15. * to pick these devices up without messing with simple-framebuffer drivers.
  16. * The global "screen_info" is still valid at all times.
  17. *
  18. * If CONFIG_SYSFB_SIMPLEFB is not selected, never register "simple-framebuffer"
  19. * platform devices, but only use legacy framebuffer devices for
  20. * backwards compatibility.
  21. *
  22. * TODO: We set the dev_id field of all platform-devices to 0. This allows
  23. * other OF/DT parsers to create such devices, too. However, they must
  24. * start at offset 1 for this to work.
  25. */
  26. #include <linux/err.h>
  27. #include <linux/init.h>
  28. #include <linux/kernel.h>
  29. #include <linux/mm.h>
  30. #include <linux/platform_data/simplefb.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/screen_info.h>
  33. #include <linux/sysfb.h>
  34. static struct platform_device *pd;
  35. static DEFINE_MUTEX(disable_lock);
  36. static bool disabled;
  37. static bool sysfb_unregister(void)
  38. {
  39. if (IS_ERR_OR_NULL(pd))
  40. return false;
  41. platform_device_unregister(pd);
  42. pd = NULL;
  43. return true;
  44. }
  45. /**
  46. * sysfb_disable() - disable the Generic System Framebuffers support
  47. *
  48. * This disables the registration of system framebuffer devices that match the
  49. * generic drivers that make use of the system framebuffer set up by firmware.
  50. *
  51. * It also unregisters a device if this was already registered by sysfb_init().
  52. *
  53. * Context: The function can sleep. A @disable_lock mutex is acquired to serialize
  54. * against sysfb_init(), that registers a system framebuffer device.
  55. */
  56. void sysfb_disable(void)
  57. {
  58. mutex_lock(&disable_lock);
  59. sysfb_unregister();
  60. disabled = true;
  61. mutex_unlock(&disable_lock);
  62. }
  63. EXPORT_SYMBOL_GPL(sysfb_disable);
  64. static __init int sysfb_init(void)
  65. {
  66. struct screen_info *si = &screen_info;
  67. struct simplefb_platform_data mode;
  68. const char *name;
  69. bool compatible;
  70. int ret = 0;
  71. mutex_lock(&disable_lock);
  72. if (disabled)
  73. goto unlock_mutex;
  74. sysfb_apply_efi_quirks();
  75. /* try to create a simple-framebuffer device */
  76. compatible = sysfb_parse_mode(si, &mode);
  77. if (compatible) {
  78. pd = sysfb_create_simplefb(si, &mode);
  79. if (!IS_ERR(pd))
  80. goto unlock_mutex;
  81. }
  82. /* if the FB is incompatible, create a legacy framebuffer device */
  83. if (si->orig_video_isVGA == VIDEO_TYPE_EFI)
  84. name = "efi-framebuffer";
  85. else if (si->orig_video_isVGA == VIDEO_TYPE_VLFB)
  86. name = "vesa-framebuffer";
  87. else if (si->orig_video_isVGA == VIDEO_TYPE_VGAC)
  88. name = "vga-framebuffer";
  89. else if (si->orig_video_isVGA == VIDEO_TYPE_EGAC)
  90. name = "ega-framebuffer";
  91. else
  92. name = "platform-framebuffer";
  93. pd = platform_device_alloc(name, 0);
  94. if (!pd) {
  95. ret = -ENOMEM;
  96. goto unlock_mutex;
  97. }
  98. sysfb_set_efifb_fwnode(pd);
  99. ret = platform_device_add_data(pd, si, sizeof(*si));
  100. if (ret)
  101. goto err;
  102. ret = platform_device_add(pd);
  103. if (ret)
  104. goto err;
  105. goto unlock_mutex;
  106. err:
  107. platform_device_put(pd);
  108. unlock_mutex:
  109. mutex_unlock(&disable_lock);
  110. return ret;
  111. }
  112. /* must execute after PCI subsystem for EFI quirks */
  113. device_initcall(sysfb_init);