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