public inbox for linux-efi@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/2] Save Brightness on Macs
@ 2026-03-15 22:19 Atharva Tiwari
  2026-03-15 22:19 ` [PATCH v3 1/2] efi: Save Brightness using EFI " Atharva Tiwari
  2026-03-15 22:20 ` [PATCH v3 2/2] platform/apple-gmux: use apple_brightness to save brightness to EFI Atharva Tiwari
  0 siblings, 2 replies; 5+ messages in thread
From: Atharva Tiwari @ 2026-03-15 22:19 UTC (permalink / raw)
  Cc: Atharva Tiwari, Ard Biesheuvel, Hans de Goede, Ilpo Järvinen,
	open list, open list:EXTENSIBLE FIRMWARE INTERFACE (EFI),
	open list:X86 PLATFORM DRIVERS

We fix this by writing the brightness value to efivar 'backlight-level'
at shutdown only, when it is changed since the last boot.

Changes in v3:
- Fixed a bug, in which apple_brightness_shutdown is still called on
   non-supported devices.
Changes in v2:
- Saved the brightness only when it is different than the previous boot.

Link to v2:
https://lore.kernel.org/all/20260315215302.24087-1-atharvatiwarilinuxdev@gmail.com/
Link to v1:
https://lore.kernel.org/all/20260206125641.12983-1-atharvatiwarilinuxdev@gmail.com/

Atharva Tiwari (2):
  efi: Save Brightness using EFI on Macs
  platform/apple-gmux: use apple_brightness to save brightness to EFI

 drivers/firmware/efi/Kconfig                  | 10 +++
 drivers/firmware/efi/Makefile                 |  1 +
 drivers/firmware/efi/apple-brightness.c       | 67 +++++++++++++++++++
 drivers/platform/x86/apple-gmux.c             | 21 ++++++
 .../linux/platform_data/apple-brightness.h    | 20 ++++++
 5 files changed, 119 insertions(+)
 create mode 100644 drivers/firmware/efi/apple-brightness.c
 create mode 100644 include/linux/platform_data/apple-brightness.h

-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v3 1/2] efi: Save Brightness using EFI on Macs
  2026-03-15 22:19 [PATCH v3 0/2] Save Brightness on Macs Atharva Tiwari
@ 2026-03-15 22:19 ` Atharva Tiwari
  2026-03-15 22:47   ` Randy Dunlap
  2026-03-15 22:20 ` [PATCH v3 2/2] platform/apple-gmux: use apple_brightness to save brightness to EFI Atharva Tiwari
  1 sibling, 1 reply; 5+ messages in thread
From: Atharva Tiwari @ 2026-03-15 22:19 UTC (permalink / raw)
  Cc: Atharva Tiwari, Lukas Wunner, Ard Biesheuvel, Hans de Goede,
	Ilpo Järvinen, open list,
	open list:EXTENSIBLE FIRMWARE INTERFACE (EFI),
	open list:X86 PLATFORM DRIVERS

Currently when a Mac reboots, the brightness is not the same
as the previous boot instead its the same as the last time the
Mac booted macOS.

We can fix this issue by saving the brightness level to the efivar
backlight-level.

(tested on iMac20,1)

Suggested-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>
---
 drivers/firmware/efi/Kconfig                  | 10 +++
 drivers/firmware/efi/Makefile                 |  1 +
 drivers/firmware/efi/apple-brightness.c       | 67 +++++++++++++++++++
 .../linux/platform_data/apple-brightness.h    | 20 ++++++
 4 files changed, 98 insertions(+)
 create mode 100644 drivers/firmware/efi/apple-brightness.c
 create mode 100644 include/linux/platform_data/apple-brightness.h

diff --git a/drivers/firmware/efi/Kconfig b/drivers/firmware/efi/Kconfig
index 29e0729299f5..dd0a9c9a772a 100644
--- a/drivers/firmware/efi/Kconfig
+++ b/drivers/firmware/efi/Kconfig
@@ -167,6 +167,16 @@ config APPLE_PROPERTIES
 
 	  If unsure, say Y if you have a Mac.  Otherwise N.
 
+config APPLE_BRIGHTNESS
+	bool "Apple Backlight control for EFI"
+	depends on X86
+	help
+	  This will save the brightness level to EFI, so brightness
+	  level is preserved across reboots and shutdows. allowing
+	  for improved support of Apple hardware.
+
+	  If unsure, say Y if you have a Mac, Otherwise N.
+
 config RESET_ATTACK_MITIGATION
 	bool "Reset memory attack mitigation"
 	depends on EFI_STUB
diff --git a/drivers/firmware/efi/Makefile b/drivers/firmware/efi/Makefile
index 8efbcf699e4f..1f5705cc87a2 100644
--- a/drivers/firmware/efi/Makefile
+++ b/drivers/firmware/efi/Makefile
@@ -26,6 +26,7 @@ obj-$(CONFIG_EFI_BOOTLOADER_CONTROL)	+= efibc.o
 obj-$(CONFIG_EFI_TEST)			+= test/
 obj-$(CONFIG_EFI_DEV_PATH_PARSER)	+= dev-path-parser.o
 obj-$(CONFIG_APPLE_PROPERTIES)		+= apple-properties.o
+obj-$(CONFIG_APPLE_BRIGHTNESS)		+= apple-brightness.o
 obj-$(CONFIG_EFI_RCI2_TABLE)		+= rci2-table.o
 obj-$(CONFIG_EFI_EMBEDDED_FIRMWARE)	+= embedded-firmware.o
 obj-$(CONFIG_LOAD_UEFI_KEYS)		+= mokvar-table.o
diff --git a/drivers/firmware/efi/apple-brightness.c b/drivers/firmware/efi/apple-brightness.c
new file mode 100644
index 000000000000..b060861b0795
--- /dev/null
+++ b/drivers/firmware/efi/apple-brightness.c
@@ -0,0 +1,67 @@
+// SPDX-License-Identifier: GPL-2.0-only OR MIT
+/*
+ * apple-brightness.c - EFI brightness saver on Macs
+ * Copyright (C) 2026 Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/backlight.h>
+#include <linux/efi.h>
+#include <linux/platform_data/apple-brightness.h>
+
+static u32 efi_attr;
+static u16 last_saved_level;
+
+static int (*get_brightness)(struct backlight_device *bl);
+static struct backlight_device *bl_dev;
+
+void apple_brightness_shutdown(void)
+{
+	u16 level;
+	efi_status_t status;
+
+	level = (u16)get_brightness(bl_dev);
+
+	if (level == last_saved_level)
+		return;
+
+	status = efivar_set_variable(APPLE_BRIGHTNESS_NAME, &APPLE_BRIGHTNESS_GUID,
+				efi_attr, sizeof(level), &level);
+	if (status != EFI_SUCCESS)
+		pr_debug("Unable to set brightness: 0x%lx\n", status);
+}
+EXPORT_SYMBOL(apple_brightness_shutdown);
+
+int apple_brightness_probe(struct backlight_device *bl,
+	int (*get_brightnessfn)(struct backlight_device *bl))
+{
+	efi_status_t status;
+	unsigned long size = sizeof(last_saved_level);
+	int ret;
+
+	bl_dev = bl;
+	get_brightness = get_brightnessfn;
+
+	if (!efi_rt_services_supported(EFI_RT_SUPPORTED_SET_VARIABLE))
+		return -ENODEV;
+
+	ret = efivar_lock();
+	if (ret)
+		return ret;
+
+	status = efivar_get_variable(APPLE_BRIGHTNESS_NAME, &APPLE_BRIGHTNESS_GUID,
+				&efi_attr, &size, &last_saved_level);
+
+	efivar_unlock();
+
+	if (status != EFI_SUCCESS)
+		return -ENODEV;
+
+	return 0;
+}
+EXPORT_SYMBOL(apple_brightness_probe);
+
+MODULE_AUTHOR("Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>");
+MODULE_DESCRIPTION("EFI Brightness saver for Macs");
+MODULE_LICENSE("Dual MIT/GPL");
diff --git a/include/linux/platform_data/apple-brightness.h b/include/linux/platform_data/apple-brightness.h
new file mode 100644
index 000000000000..54e3f3f8aa1c
--- /dev/null
+++ b/include/linux/platform_data/apple-brightness.h
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0-only OR MIT */
+/*
+ * apple-brightness.h - EFI brightness saver for Macs
+ * Copyright (C) 2026 Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>
+ */
+
+#ifndef _APPLE_BL_H_
+#define _APPLE_BL_H_
+
+#include <linux/backlight.h>
+#include <linux/efi.h>
+
+#define APPLE_BRIGHTNESS_NAME           L"backlight-level"
+#define APPLE_BRIGHTNESS_GUID           EFI_GUID(0x7c436110, 0xab2a, 0x4bbb, 0xa8, 0x80, 0xfe, 0x41, 0x99, 0x5c, 0x9f, 0x82)
+
+int apple_brightness_probe(struct backlight_device *bl,
+	int (*get_brightnessfn)(struct backlight_device *bl));
+
+void apple_brightness_shutdown(void);
+#endif /* _APPLE_BL_H */
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v3 2/2] platform/apple-gmux: use apple_brightness to save brightness to EFI
  2026-03-15 22:19 [PATCH v3 0/2] Save Brightness on Macs Atharva Tiwari
  2026-03-15 22:19 ` [PATCH v3 1/2] efi: Save Brightness using EFI " Atharva Tiwari
@ 2026-03-15 22:20 ` Atharva Tiwari
  2026-03-16 10:16   ` kernel test robot
  1 sibling, 1 reply; 5+ messages in thread
From: Atharva Tiwari @ 2026-03-15 22:20 UTC (permalink / raw)
  Cc: Atharva Tiwari, Ard Biesheuvel, Hans de Goede, Ilpo Järvinen,
	open list, open list:EXTENSIBLE FIRMWARE INTERFACE (EFI),
	open list:X86 PLATFORM DRIVERS

use apple_brightness to save brightness to EFI.

(tested on iMac20,1)

Signed-off-by: Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>
---
 drivers/platform/x86/apple-gmux.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/drivers/platform/x86/apple-gmux.c b/drivers/platform/x86/apple-gmux.c
index 1417e230edbd..3797a0e7ff40 100644
--- a/drivers/platform/x86/apple-gmux.c
+++ b/drivers/platform/x86/apple-gmux.c
@@ -22,6 +22,7 @@
 #include <linux/pci.h>
 #include <linux/vga_switcheroo.h>
 #include <linux/debugfs.h>
+#include <linux/platform_data/apple-brightness.h>
 #include <acpi/video.h>
 #include <asm/io.h>
 
@@ -78,6 +79,8 @@ struct apple_gmux_data {
 	/* debugfs data */
 	u8 selected_port;
 	struct dentry *debug_dentry;
+
+	bool save_efi;
 };
 
 static struct apple_gmux_data *apple_gmux_data;
@@ -960,6 +963,13 @@ static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
 	}
 
 	gmux_init_debugfs(gmux_data);
+	if (IS_ENABLED(CONFIG_APPLE_BRIGHTNESS)) {
+		ret = apple_brightness_probe(gmux_data->bdev, &gmux_get_brightness);
+		if (ret)
+			pr_warn("Unable to Enable EFI brightness save: %d\n", ret);
+		else
+			gmux_data->save_efi = true;
+	}
 	return 0;
 
 err_register_handler:
@@ -1012,6 +1022,16 @@ static void gmux_remove(struct pnp_dev *pnp)
 	kfree(gmux_data);
 }
 
+static void gmux_shutdown(struct pnp_dev *pnp)
+{
+	struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp);
+
+	if (gmux_data->save_efi)
+		apple_brightness_shutdown();
+
+	gmux_remove(pnp);
+}
+
 static const struct pnp_device_id gmux_device_ids[] = {
 	{GMUX_ACPI_HID, 0},
 	{"", 0}
@@ -1026,6 +1046,7 @@ static struct pnp_driver gmux_pnp_driver = {
 	.name		= "apple-gmux",
 	.probe		= gmux_probe,
 	.remove		= gmux_remove,
+	.shutdown	= gmux_shutdown,
 	.id_table	= gmux_device_ids,
 	.driver		= {
 			.pm = &gmux_dev_pm_ops,
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 1/2] efi: Save Brightness using EFI on Macs
  2026-03-15 22:19 ` [PATCH v3 1/2] efi: Save Brightness using EFI " Atharva Tiwari
@ 2026-03-15 22:47   ` Randy Dunlap
  0 siblings, 0 replies; 5+ messages in thread
From: Randy Dunlap @ 2026-03-15 22:47 UTC (permalink / raw)
  To: Atharva Tiwari
  Cc: Lukas Wunner, Ard Biesheuvel, Hans de Goede, Ilpo Järvinen,
	open list, open list:EXTENSIBLE FIRMWARE INTERFACE (EFI),
	open list:X86 PLATFORM DRIVERS



On 3/15/26 3:19 PM, Atharva Tiwari wrote:
> diff --git a/drivers/firmware/efi/Kconfig b/drivers/firmware/efi/Kconfig
> index 29e0729299f5..dd0a9c9a772a 100644
> --- a/drivers/firmware/efi/Kconfig
> +++ b/drivers/firmware/efi/Kconfig
> @@ -167,6 +167,16 @@ config APPLE_PROPERTIES
>  
>  	  If unsure, say Y if you have a Mac.  Otherwise N.
>  
> +config APPLE_BRIGHTNESS
> +	bool "Apple Backlight control for EFI"
> +	depends on X86
> +	help
> +	  This will save the brightness level to EFI, so brightness
> +	  level is preserved across reboots and shutdows. allowing

	                                        shutdowns, allowing

> +	  for improved support of Apple hardware.
> +
> +	  If unsure, say Y if you have a Mac, Otherwise N.

	                                      otherwise N.


-- 
~Randy


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 2/2] platform/apple-gmux: use apple_brightness to save brightness to EFI
  2026-03-15 22:20 ` [PATCH v3 2/2] platform/apple-gmux: use apple_brightness to save brightness to EFI Atharva Tiwari
@ 2026-03-16 10:16   ` kernel test robot
  0 siblings, 0 replies; 5+ messages in thread
From: kernel test robot @ 2026-03-16 10:16 UTC (permalink / raw)
  To: Atharva Tiwari
  Cc: oe-kbuild-all, Atharva Tiwari, Ard Biesheuvel, Hans de Goede,
	Ilpo Järvinen, linux-kernel,
	(open list:EXTENSIBLE FIRMWARE INTERFACE \(EFI\)),
	platform-driver-x86

Hi Atharva,

kernel test robot noticed the following build errors:

[auto build test ERROR on efi/next]
[also build test ERROR on linus/master v7.0-rc4 next-20260313]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Atharva-Tiwari/efi-Save-Brightness-using-EFI-on-Macs/20260316-084441
base:   https://git.kernel.org/pub/scm/linux/kernel/git/efi/efi.git next
patch link:    https://lore.kernel.org/r/20260315222020.24341-3-atharvatiwarilinuxdev%40gmail.com
patch subject: [PATCH v3 2/2] platform/apple-gmux: use apple_brightness to save brightness to EFI
config: x86_64-rhel-9.4 (https://download.01.org/0day-ci/archive/20260316/202603161105.ILZkpASC-lkp@intel.com/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260316/202603161105.ILZkpASC-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202603161105.ILZkpASC-lkp@intel.com/

All errors (new ones prefixed by >>, old ones prefixed by <<):

>> ERROR: modpost: "apple_brightness_shutdown" [drivers/platform/x86/apple-gmux.ko] undefined!

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-03-16 10:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-15 22:19 [PATCH v3 0/2] Save Brightness on Macs Atharva Tiwari
2026-03-15 22:19 ` [PATCH v3 1/2] efi: Save Brightness using EFI " Atharva Tiwari
2026-03-15 22:47   ` Randy Dunlap
2026-03-15 22:20 ` [PATCH v3 2/2] platform/apple-gmux: use apple_brightness to save brightness to EFI Atharva Tiwari
2026-03-16 10:16   ` kernel test robot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox