Skip to contentSkip to conversation

Add backlight driver for Nvidia-based Apple Macbook Pros

PATCH Superseded Matthew Garrett 7 participants 14 messages Jun 7, 2008 – Jun 9, 2008

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add backlight driver for Nvidia-based Apple Macbook Pros

Nvidia-based Apple Macbook Pros don't appear to handle backlight control through the graphics card registers or ACPI, but instead trigger changes via SMI calls. This driver registers a generic backlight device that lets existing userspace deal with it. Code derived from Julien Blache's Pommed application.

---

Lennart - as far as I could tell from your description, the DMI autoloading code for this should be correct. However, it's not working for me on F9. Have I missed something, or does it just not work there?

drivers/video/backlight/Kconfig +8 -0
index dcd8073..56f4572 100644
@@ -112,3 +112,11 @@ config BACKLIGHT_CARILLO_RANCH
 	help
 	  If you have a Intel LE80578 (Carillo Ranch) say Y to enable the
 	  backlight driver.
+
+config BACKLIGHT_MBP_NVIDIA
+       tristate "Macbook Pro Nvidia Backlight Driver"
+       depends on BACKLIGHT_CLASS_DEVICE && X86
+       default n
+       help
+         If you have an Apple Macbook Pro with Nvidia graphics hardware say Y
+	 to enable a driver for its backlight
drivers/video/backlight/Makefile +1 -0
index 33f6c7c..5b91515 100644
@@ -10,3 +10,4 @@ obj-$(CONFIG_BACKLIGHT_LOCOMO)	+= locomolcd.o
 obj-$(CONFIG_BACKLIGHT_OMAP1)	+= omap1_bl.o
 obj-$(CONFIG_BACKLIGHT_PROGEAR) += progear_bl.o
 obj-$(CONFIG_BACKLIGHT_CARILLO_RANCH) += cr_bllcd.o
+obj-$(CONFIG_BACKLIGHT_MBP_NVIDIA) += mbp_nvidia_bl.o
drivers/video/backlight/mbp_nvidia_bl.c new file +114 -0

114 lines not on this page · raw message

Signature · 1 line
Matthew Garrett | [email protected]
#1 lore raw
Quoted 6 lines · Matthew Garrett

Nvidia-based Apple Macbook Pros don't appear to handle backlight control through the graphics card registers or ACPI, but instead trigger changes via SMI calls. This driver registers a generic backlight device that lets existing userspace deal with it. Code derived from Julien Blache's Pommed application.

Quoted 7 lines

---

Lennart - as far as I could tell from your description, the DMI autoloading code for this should be correct. However, it's not working for me on F9. Have I missed something, or does it just not work there?

BTW do we have any documentation to help ordinary human understand and generate these aliases ?

Quoted 49 lines
drivers/video/backlight/Kconfig +8 -0
index dcd8073..56f4572 100644
@@ -112,3 +112,11 @@ config BACKLIGHT_CARILLO_RANCH
 	help
 	  If you have a Intel LE80578 (Carillo Ranch) say Y to enable the
 	  backlight driver.
+
+config BACKLIGHT_MBP_NVIDIA
+       tristate "Macbook Pro Nvidia Backlight Driver"
+       depends on BACKLIGHT_CLASS_DEVICE && X86
+       default n
+       help
+         If you have an Apple Macbook Pro with Nvidia graphics hardware say Y
+	 to enable a driver for its backlight
drivers/video/backlight/Makefile +1 -0
index 33f6c7c..5b91515 100644
@@ -10,3 +10,4 @@ obj-$(CONFIG_BACKLIGHT_LOCOMO)	+= locomolcd.o
 obj-$(CONFIG_BACKLIGHT_OMAP1)	+= omap1_bl.o
 obj-$(CONFIG_BACKLIGHT_PROGEAR) += progear_bl.o
 obj-$(CONFIG_BACKLIGHT_CARILLO_RANCH) += cr_bllcd.o
+obj-$(CONFIG_BACKLIGHT_MBP_NVIDIA) += mbp_nvidia_bl.o
drivers/video/backlight/mbp_nvidia_bl.c new file +18 -0
new file mode 100644
index 0000000..c622d22
@@ -0,0 +1,114 @@
+/*
+ *  Backlight Driver for Nvidia 8600 in Macbook Pro
+ *
+ *  Copyright (c) Red Hat <[email protected]>
+ *  Based on code from Pommed:
+ *  Copyright (C) 2006 Nicolas Boichat <nicolas @boichat.ch>
+ *  Copyright (C) 2006 Felipe Alfaro Solana <felipe_alfaro @linuxmail.org>
+ *  Copyright (C) 2007 Julien BLACHE <[email protected]>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This driver triggers SMIs which cause the firmware to change the
+ *  backlight brightness. This is icky in many ways, but it's impractical to
+ *  get at the firmware code in order to figure out what it's actually doing.
+ */
+

drivers/video/backlight/mbp_nvidia_bl.c line 28

Quoted 10 lines
drivers/video/backlight/mbp_nvidia_bl.c new file +10 -0
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/platform_device.h>
+#include <linux/backlight.h>
+#include <linux/err.h>
+#include <linux/dmi.h>
+#include <linux/io.h>
+
+struct backlight_device *mbp_backlight_device;

Missing static ?

Quoted 51 lines
drivers/video/backlight/mbp_nvidia_bl.c new file +51 -0
+
+static struct dmi_system_id __initdata mbp_device_table[] = {
+	{
+		.ident = "3,1",
+		.matches = {
+			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
+			DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3,1"),
+		},
+	},
+	{
+		.ident = "3,2",
+		.matches = {
+			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
+			DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3,2"),
+		},
+	},
+	{
+		.ident = "4,1",
+		.matches = {
+			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
+			DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro4,1"),
+		},
+	},
+	{ }
+};
+
+static int mbp_send_intensity(struct backlight_device *bd)
+{
+	int intensity = bd->props.brightness;
+
+	outb(0x04 | (intensity << 4), 0xb3);
+	outb(0xbf, 0xb2);
+
+	return 0;
+}
+
+static int mbp_get_intensity(struct backlight_device *bd)
+{
+	outb(0x03, 0xb3);
+	outb(0xbf, 0xb2);
+	return inb(0xb3) >> 4;
+}
+
+static struct backlight_ops mbp_ops = {
+	.get_brightness = mbp_get_intensity,
+	.update_status  = mbp_send_intensity,
+};
+
+static int __init mbp_init(void)
+{
+	if (!dmi_check_system(mbp_device_table))

drivers/video/backlight/mbp_nvidia_bl.c line 89

Quoted 10 lines
drivers/video/backlight/mbp_nvidia_bl.c new file +10 -0
+		return -ENODEV;
+
+	if (!request_region(0xb2, 2, "Macbook Pro backlight"))
+		return -ENXIO;
+
+	mbp_backlight_device = backlight_device_register("mbp_backlight",
+							 NULL, NULL,
+							 &mbp_ops);
+	if (IS_ERR(mbp_backlight_device))
+		return PTR_ERR(mbp_backlight_device);

You leak ioport region here.

Quoted 25 lines
drivers/video/backlight/mbp_nvidia_bl.c new file +25 -0
+
+	mbp_backlight_device->props.max_brightness = 15;
+	mbp_backlight_device->props.brightness =
+		mbp_get_intensity(mbp_backlight_device);
+	backlight_update_status(mbp_backlight_device);
+
+	return 0;
+}
+
+static void __exit mbp_exit(void)
+{
+	backlight_device_unregister(mbp_backlight_device);
+
+	release_region(0xb2, 2);
+}
+
+module_init(mbp_init);
+module_exit(mbp_exit);
+
+MODULE_AUTHOR("Matthew Garrett <[email protected]>");
+MODULE_DESCRIPTION("Nvidia-based Macbook Pro Backlight Driver");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("dmi:*:svnApple Inc.:pnMacBookPro3,1:*");
+MODULE_ALIAS("dmi:*:svnApple Inc.:pnMacBookPro3,2:*");
+MODULE_ALIAS("dmi:*:svnApple Inc.:pnMacBookPro4,1:*");
Signature · 2 lines
Andrey Panin		| Linux and UNIX system administrator
[email protected]		| PGP key: wwwkeys.pgp.net
#2 lore raw
In reply to Matthew Garrett
Quoted 13 lines · On Sat, 7 Jun 2008 03:54:36 +0100 Matthew Garrett <[email protected]> wrote:

Nvidia-based Apple Macbook Pros don't appear to handle backlight control through the graphics card registers or ACPI, but instead trigger changes via SMI calls. This driver registers a generic backlight device that lets existing userspace deal with it. Code derived from Julien Blache's Pommed application.

...

drivers/video/backlight/mbp_nvidia_bl.c line 8

Quoted 8 lines
drivers/video/backlight/mbp_nvidia_bl.c new file +8 -0
@@ -0,0 +1,114 @@
+/*
+ *  Backlight Driver for Nvidia 8600 in Macbook Pro
+ *
+ *  Copyright (c) Red Hat <[email protected]>
+ *  Based on code from Pommed:
+ *  Copyright (C) 2006 Nicolas Boichat <nicolas @boichat.ch>
+ *  Copyright (C) 2006 Felipe Alfaro Solana <felipe_alfaro @linuxmail.org>
+ *  Copyright (C) 2007 Julien BLACHE <[email protected]>

Did this patch have appropriate attribution and signoffs?

Quoted 70 lines
drivers/video/backlight/mbp_nvidia_bl.c new file +70 -0
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This driver triggers SMIs which cause the firmware to change the
+ *  backlight brightness. This is icky in many ways, but it's impractical to
+ *  get at the firmware code in order to figure out what it's actually doing.
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/platform_device.h>
+#include <linux/backlight.h>
+#include <linux/err.h>
+#include <linux/dmi.h>
+#include <linux/io.h>
+
+struct backlight_device *mbp_backlight_device;
+
+static struct dmi_system_id __initdata mbp_device_table[] = {
+	{
+		.ident = "3,1",
+		.matches = {
+			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
+			DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3,1"),
+		},
+	},
+	{
+		.ident = "3,2",
+		.matches = {
+			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
+			DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3,2"),
+		},
+	},
+	{
+		.ident = "4,1",
+		.matches = {
+			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
+			DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro4,1"),
+		},
+	},
+	{ }
+};
+
+static int mbp_send_intensity(struct backlight_device *bd)
+{
+	int intensity = bd->props.brightness;
+
+	outb(0x04 | (intensity << 4), 0xb3);
+	outb(0xbf, 0xb2);
+
+	return 0;
+}
+
+static int mbp_get_intensity(struct backlight_device *bd)
+{
+	outb(0x03, 0xb3);
+	outb(0xbf, 0xb2);
+	return inb(0xb3) >> 4;
+}
+
+static struct backlight_ops mbp_ops = {
+	.get_brightness = mbp_get_intensity,
+	.update_status  = mbp_send_intensity,
+};
+
+static int __init mbp_init(void)
+{
+	if (!dmi_check_system(mbp_device_table))

drivers/video/backlight/mbp_nvidia_bl.c line 89

Quoted 10 lines
drivers/video/backlight/mbp_nvidia_bl.c new file +10 -0
+		return -ENODEV;
+
+	if (!request_region(0xb2, 2, "Macbook Pro backlight"))
+		return -ENXIO;
+
+	mbp_backlight_device = backlight_device_register("mbp_backlight",
+							 NULL, NULL,
+							 &mbp_ops);
+	if (IS_ERR(mbp_backlight_device))
+		return PTR_ERR(mbp_backlight_device);

Missing release_region()?

Quoted 24 lines
drivers/video/backlight/mbp_nvidia_bl.c new file +24 -0
+	mbp_backlight_device->props.max_brightness = 15;
+	mbp_backlight_device->props.brightness =
+		mbp_get_intensity(mbp_backlight_device);
+	backlight_update_status(mbp_backlight_device);
+
+	return 0;
+}
+
+static void __exit mbp_exit(void)
+{
+	backlight_device_unregister(mbp_backlight_device);
+
+	release_region(0xb2, 2);
+}
+
+module_init(mbp_init);
+module_exit(mbp_exit);
+
+MODULE_AUTHOR("Matthew Garrett <[email protected]>");
+MODULE_DESCRIPTION("Nvidia-based Macbook Pro Backlight Driver");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("dmi:*:svnApple Inc.:pnMacBookPro3,1:*");
+MODULE_ALIAS("dmi:*:svnApple Inc.:pnMacBookPro3,2:*");
+MODULE_ALIAS("dmi:*:svnApple Inc.:pnMacBookPro4,1:*");
#3 lore raw
Quoted 6 lines · Andrew Morton
Quoted 3 lines · On Sat, 7 Jun 2008 03:54:36 +0100 Matthew Garrett <[email protected]> wrote:
+ *  Copyright (C) 2006 Nicolas Boichat <nicolas @boichat.ch>
+ *  Copyright (C) 2006 Felipe Alfaro Solana <felipe_alfaro @linuxmail.org>
+ *  Copyright (C) 2007 Julien BLACHE <[email protected]>

Did this patch have appropriate attribution and signoffs?

Pommed is GPLv2.

Quoted 1 line

Missing release_region()?

Yup.

Signature · 1 line
Matthew Garrett | [email protected]
#4 lore raw

Hi Matthew,

Quoted 1 line · Andrew Morton
Quoted 1 line

Did this patch have appropriate attribution and signoffs?

Quoted 1 line · Matthew Garrett

Pommed is GPLv2.

Andrew has required signoffs from all copyright holders of a patch in the past regardless of that.

#5 lore raw
Quoted 10 lines · Pekka Enberg

Hi Matthew,

Quoted 1 line · Andrew Morton
Quoted 1 line

Did this patch have appropriate attribution and signoffs?

Quoted 1 line · Matthew Garrett

Pommed is GPLv2.

Andrew has required signoffs from all copyright holders of a patch in the past regardless of that.

SubmittingPatches says (in part):

(b) The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same open source license (unless I am permitted to submit under a different license), as indicated in the file; or

If that's not sufficient, then the documentation needs fixing. It's also not current practice, as far as I can tell - did ath5k have sign-offs from the BSD people?

Signature · 1 line
Matthew Garrett | [email protected]
#6 lore raw
Quoted 3 lines · Matthew Garrett

If that's not sufficient, then the documentation needs fixing. It's also not current practice, as far as I can tell - did ath5k have sign-offs from the BSD people?

I don't know about ath5k. But when we submitted the IP1000 driver, we had to contact the copyright holder (a company) and get proper signoffs before Andrew acked the merge.

#7 lore raw
In reply to Pekka Enberg
Quoted 10 lines · On Sat, 7 Jun 2008 13:05:23 +0300 "Pekka Enberg" <[email protected]> wrote:

Hi Matthew,

Quoted 1 line · Andrew Morton
Quoted 1 line

Did this patch have appropriate attribution and signoffs?

Quoted 1 line · Matthew Garrett

Pommed is GPLv2.

Andrew has required signoffs from all copyright holders of a patch in the past regardless of that.

More like "asked for". It's best to gather those signoffs if the people are around.

Getting the From: correct is (IMO) more important.

#8 lore raw

PATCH v2 1/1 Add backlight driver for Nvidia-based Apple Macbook Pros

In reply to Andrew Morton

Nvidia-based Apple Macbook Pros don't appear to handle backlight control through the graphics card registers or ACPI, but instead trigger changes via SMI calls. This driver registers a generic backlight device that lets existing userspace deal with it. Code derived from Julien Blache's Pommed application.

---

Added a missing static and a release_region() in the error path pointed out by Andrey Panin and Andrew Morton

drivers/video/backlight/Kconfig +8 -0
index dcd8073..56f4572 100644
@@ -112,3 +112,11 @@ config BACKLIGHT_CARILLO_RANCH
 	help
 	  If you have a Intel LE80578 (Carillo Ranch) say Y to enable the
 	  backlight driver.
+
+config BACKLIGHT_MBP_NVIDIA
+       tristate "Macbook Pro Nvidia Backlight Driver"
+       depends on BACKLIGHT_CLASS_DEVICE && X86
+       default n
+       help
+         If you have an Apple Macbook Pro with Nvidia graphics hardware say Y
+	 to enable a driver for its backlight
drivers/video/backlight/Makefile +1 -0
index 33f6c7c..5b91515 100644
@@ -10,3 +10,4 @@ obj-$(CONFIG_BACKLIGHT_LOCOMO)	+= locomolcd.o
 obj-$(CONFIG_BACKLIGHT_OMAP1)	+= omap1_bl.o
 obj-$(CONFIG_BACKLIGHT_PROGEAR) += progear_bl.o
 obj-$(CONFIG_BACKLIGHT_CARILLO_RANCH) += cr_bllcd.o
+obj-$(CONFIG_BACKLIGHT_MBP_NVIDIA) += mbp_nvidia_bl.o
drivers/video/backlight/mbp_nvidia_bl.c new file +116 -0

116 lines not on this page · raw message

Signature · 1 line
Matthew Garrett | [email protected]
#9 lore raw

Matthew Garrett <[email protected]> wrote:

Hi,

Quoted 1 line

If you need it, here you have it:

Minor nitpicks follow:

drivers/video/backlight/Kconfig config BACKLIGHT_CARILLO_RANCH · line 117

Quoted 2 lines
drivers/video/backlight/Kconfig +2 -0
+config BACKLIGHT_MBP_NVIDIA
+       tristate "Macbook Pro Nvidia Backlight Driver"
                    ^^^^^^^   

Proper capitalization is "MacBook", also to be consistent with other help texts in the kernel (I hope).

Quoted 3 lines
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.

This particular file in pommed is GPL v2 or later; it's external code I included in pommed and the original license was GPL v2 or later.

JB.

Signature · 2 lines
Julien BLACHE                                   <http://www.jblache.org> 
<[email protected]>                                  GPG KeyID 0xF5D65169
#10 lore raw
In reply to Matthew Garrett
Quoted 3 lines · Matthew Garrett

Lennart - as far as I could tell from your description, the DMI autoloading code for this should be correct. However, it's not working for me on F9. Have I missed something, or does it just not work there?

It should be pretty easy to figure out the right string for DMI autoloading. Just do a cat /sys/class/dmi/id/modalias and replace the part of the string you consider not very useful for identifying the machine with (dates, version numbers, empty strings, 0123456 rubbish data, ...) with an asterisk.

Looking at those strings included in your patch they look pretty much correct, but since I don't have access to a Mac I cannot really check this.

It's udev's job to match the modalias strings of your driver module with the modalias string from /sys/class/dmi/id/modalias. You can retrigger this matching by doing something like "echo 1 > /sys/class/dmi/id/uevent" or suchlike, but I don't remember the details.

Lennart

Signature · 3 lines
Lennart Poettering                        Red Hat, Inc.
lennart [at] poettering [dot] net         ICQ# 11060553
http://0pointer.net/lennart/           GnuPG 0x1A015CC4
#11 lore raw
Quoted 5 lines · Lennart Poettering

It should be pretty easy to figure out the right string for DMI autoloading. Just do a cat /sys/class/dmi/id/modalias and replace the part of the string you consider not very useful for identifying the machine with (dates, version numbers, empty strings, 0123456 rubbish data, ...) with an asterisk.

Ah! Spaces are removed in the sysfs interface. That would explain the problem. I'll send a new version.

Signature · 1 line
Matthew Garrett | [email protected]
#12 lore raw

PATCH v2 1/1 Add backlight driver for Nvidia-based Apple Macbook Pros

In reply to Lennart Poettering

Nvidia-based Apple Macbook Pros don't appear to handle backlight control through the graphics card registers or ACPI, but instead trigger changes via SMI calls. This driver registers a generic backlight device that lets existing userspace deal with it. Code derived from Julien Blache's Pommed application.

---

Fixed the DMI strings and changed the capitalisation of "MacBook" in the Kconfig text.

drivers/video/backlight/Kconfig +8 -0
index dcd8073..56f4572 100644
@@ -112,3 +112,11 @@ config BACKLIGHT_CARILLO_RANCH
 	help
 	  If you have a Intel LE80578 (Carillo Ranch) say Y to enable the
 	  backlight driver.
+
+config BACKLIGHT_MBP_NVIDIA
+       tristate "MacBook Pro Nvidia Backlight Driver"
+       depends on BACKLIGHT_CLASS_DEVICE && X86
+       default n
+       help
+         If you have an Apple Macbook Pro with Nvidia graphics hardware say Y
+	 to enable a driver for its backlight
drivers/video/backlight/Makefile +1 -0
index 33f6c7c..5b91515 100644
@@ -10,3 +10,4 @@ obj-$(CONFIG_BACKLIGHT_LOCOMO)	+= locomolcd.o
 obj-$(CONFIG_BACKLIGHT_OMAP1)	+= omap1_bl.o
 obj-$(CONFIG_BACKLIGHT_PROGEAR) += progear_bl.o
 obj-$(CONFIG_BACKLIGHT_CARILLO_RANCH) += cr_bllcd.o
+obj-$(CONFIG_BACKLIGHT_MBP_NVIDIA) += mbp_nvidia_bl.o
drivers/video/backlight/mbp_nvidia_bl.c new file +116 -0

116 lines not on this page · raw message

Signature · 1 line
Matthew Garrett | [email protected]
#13 lore raw
Quoted 7 lines · Matthew Garrett

Nvidia-based Apple Macbook Pros don't appear to handle backlight control through the graphics card registers or ACPI, but instead trigger changes via SMI calls. This driver registers a generic backlight device that lets existing userspace deal with it. Code derived from Julien Blache's Pommed application.

Queued in the backlight tree, thanks.

Richard