2008-06-10 04:56:16 +08:00
|
|
|
/*
|
2011-03-23 07:30:26 +08:00
|
|
|
* Backlight Driver for Macbooks
|
2008-06-10 04:56:16 +08:00
|
|
|
*
|
|
|
|
* Copyright (c) Red Hat <mjg@redhat.com>
|
|
|
|
* 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 <jb@jblache.org>
|
|
|
|
*
|
|
|
|
* 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/backlight.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/io.h>
|
2011-03-23 07:30:26 +08:00
|
|
|
#include <linux/pci.h>
|
|
|
|
#include <linux/acpi.h>
|
2008-06-10 04:56:16 +08:00
|
|
|
|
2011-03-23 07:30:26 +08:00
|
|
|
static struct backlight_device *mb_backlight_device;
|
2008-06-10 04:56:16 +08:00
|
|
|
|
2011-03-23 07:30:26 +08:00
|
|
|
struct hw_data {
|
2009-01-11 08:11:34 +08:00
|
|
|
/* I/O resource to allocate. */
|
|
|
|
unsigned long iostart;
|
|
|
|
unsigned long iolen;
|
|
|
|
/* Backlight operations structure. */
|
2009-12-14 07:58:57 +08:00
|
|
|
const struct backlight_ops backlight_ops;
|
2011-03-23 07:30:26 +08:00
|
|
|
void (*set_brightness)(int);
|
2008-06-10 04:56:16 +08:00
|
|
|
};
|
|
|
|
|
2011-03-23 07:30:26 +08:00
|
|
|
static const struct hw_data *hw_data;
|
|
|
|
|
|
|
|
#define DRIVER "mb_backlight: "
|
|
|
|
|
2009-01-11 08:19:31 +08:00
|
|
|
/* Module parameters. */
|
|
|
|
static int debug;
|
|
|
|
module_param_named(debug, debug, int, 0644);
|
|
|
|
MODULE_PARM_DESC(debug, "Set to one to enable debugging messages.");
|
|
|
|
|
2009-01-11 08:11:34 +08:00
|
|
|
/*
|
|
|
|
* Implementation for MacBooks with Intel chipset.
|
|
|
|
*/
|
2011-03-23 07:30:26 +08:00
|
|
|
static void intel_chipset_set_brightness(int intensity)
|
|
|
|
{
|
|
|
|
outb(0x04 | (intensity << 4), 0xb3);
|
|
|
|
outb(0xbf, 0xb2);
|
|
|
|
}
|
|
|
|
|
2009-01-11 08:11:34 +08:00
|
|
|
static int intel_chipset_send_intensity(struct backlight_device *bd)
|
2008-06-10 04:56:16 +08:00
|
|
|
{
|
|
|
|
int intensity = bd->props.brightness;
|
|
|
|
|
2009-01-11 08:19:31 +08:00
|
|
|
if (debug)
|
2011-03-23 07:30:26 +08:00
|
|
|
printk(KERN_DEBUG DRIVER "setting brightness to %d\n",
|
2009-01-11 08:19:31 +08:00
|
|
|
intensity);
|
|
|
|
|
2011-03-23 07:30:26 +08:00
|
|
|
intel_chipset_set_brightness(intensity);
|
2008-06-10 04:56:16 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-11 08:11:34 +08:00
|
|
|
static int intel_chipset_get_intensity(struct backlight_device *bd)
|
2008-06-10 04:56:16 +08:00
|
|
|
{
|
2009-01-11 08:19:31 +08:00
|
|
|
int intensity;
|
|
|
|
|
2008-06-10 04:56:16 +08:00
|
|
|
outb(0x03, 0xb3);
|
|
|
|
outb(0xbf, 0xb2);
|
2009-01-11 08:19:31 +08:00
|
|
|
intensity = inb(0xb3) >> 4;
|
|
|
|
|
|
|
|
if (debug)
|
2011-03-23 07:30:26 +08:00
|
|
|
printk(KERN_DEBUG DRIVER "read brightness of %d\n",
|
2009-01-11 08:19:31 +08:00
|
|
|
intensity);
|
|
|
|
|
|
|
|
return intensity;
|
2008-06-10 04:56:16 +08:00
|
|
|
}
|
|
|
|
|
2011-03-23 07:30:26 +08:00
|
|
|
static const struct hw_data intel_chipset_data = {
|
2009-01-11 08:11:34 +08:00
|
|
|
.iostart = 0xb2,
|
|
|
|
.iolen = 2,
|
|
|
|
.backlight_ops = {
|
|
|
|
.options = BL_CORE_SUSPENDRESUME,
|
|
|
|
.get_brightness = intel_chipset_get_intensity,
|
|
|
|
.update_status = intel_chipset_send_intensity,
|
2011-03-23 07:30:26 +08:00
|
|
|
},
|
|
|
|
.set_brightness = intel_chipset_set_brightness,
|
2009-01-11 08:11:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation for MacBooks with Nvidia chipset.
|
|
|
|
*/
|
2011-03-23 07:30:26 +08:00
|
|
|
static void nvidia_chipset_set_brightness(int intensity)
|
|
|
|
{
|
|
|
|
outb(0x04 | (intensity << 4), 0x52f);
|
|
|
|
outb(0xbf, 0x52e);
|
|
|
|
}
|
|
|
|
|
2009-01-11 08:11:34 +08:00
|
|
|
static int nvidia_chipset_send_intensity(struct backlight_device *bd)
|
|
|
|
{
|
|
|
|
int intensity = bd->props.brightness;
|
|
|
|
|
2009-01-11 08:19:31 +08:00
|
|
|
if (debug)
|
2011-03-23 07:30:26 +08:00
|
|
|
printk(KERN_DEBUG DRIVER "setting brightness to %d\n",
|
2009-01-11 08:19:31 +08:00
|
|
|
intensity);
|
|
|
|
|
2011-03-23 07:30:26 +08:00
|
|
|
nvidia_chipset_set_brightness(intensity);
|
2009-01-11 08:11:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nvidia_chipset_get_intensity(struct backlight_device *bd)
|
|
|
|
{
|
2009-01-11 08:19:31 +08:00
|
|
|
int intensity;
|
|
|
|
|
2009-01-11 08:11:34 +08:00
|
|
|
outb(0x03, 0x52f);
|
|
|
|
outb(0xbf, 0x52e);
|
2009-01-11 08:19:31 +08:00
|
|
|
intensity = inb(0x52f) >> 4;
|
|
|
|
|
|
|
|
if (debug)
|
2011-03-23 07:30:26 +08:00
|
|
|
printk(KERN_DEBUG DRIVER "read brightness of %d\n",
|
2009-01-11 08:19:31 +08:00
|
|
|
intensity);
|
|
|
|
|
|
|
|
return intensity;
|
2009-01-11 08:11:34 +08:00
|
|
|
}
|
|
|
|
|
2011-03-23 07:30:26 +08:00
|
|
|
static const struct hw_data nvidia_chipset_data = {
|
2009-01-11 08:11:34 +08:00
|
|
|
.iostart = 0x52e,
|
|
|
|
.iolen = 2,
|
|
|
|
.backlight_ops = {
|
|
|
|
.options = BL_CORE_SUSPENDRESUME,
|
|
|
|
.get_brightness = nvidia_chipset_get_intensity,
|
|
|
|
.update_status = nvidia_chipset_send_intensity
|
2011-03-23 07:30:26 +08:00
|
|
|
},
|
|
|
|
.set_brightness = nvidia_chipset_set_brightness,
|
2009-01-11 08:11:34 +08:00
|
|
|
};
|
|
|
|
|
2011-03-23 07:30:26 +08:00
|
|
|
static int __devinit mb_bl_add(struct acpi_device *dev)
|
2009-01-11 08:11:34 +08:00
|
|
|
{
|
2011-03-23 07:30:26 +08:00
|
|
|
struct backlight_properties props;
|
|
|
|
struct pci_dev *host;
|
2011-03-23 07:30:27 +08:00
|
|
|
int intensity;
|
2009-01-11 08:11:34 +08:00
|
|
|
|
2011-03-23 07:30:26 +08:00
|
|
|
host = pci_get_bus_and_slot(0, 0);
|
2009-01-11 08:11:34 +08:00
|
|
|
|
2011-03-23 07:30:26 +08:00
|
|
|
if (!host) {
|
|
|
|
printk(KERN_ERR DRIVER "unable to find PCI host\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2008-06-10 04:56:16 +08:00
|
|
|
|
2011-03-23 07:30:26 +08:00
|
|
|
if (host->vendor == PCI_VENDOR_ID_INTEL)
|
|
|
|
hw_data = &intel_chipset_data;
|
|
|
|
else if (host->vendor == PCI_VENDOR_ID_NVIDIA)
|
|
|
|
hw_data = &nvidia_chipset_data;
|
|
|
|
|
|
|
|
pci_dev_put(host);
|
|
|
|
|
|
|
|
if (!hw_data) {
|
|
|
|
printk(KERN_ERR DRIVER "unknown hardware\n");
|
2008-06-10 04:56:16 +08:00
|
|
|
return -ENODEV;
|
2011-03-23 07:30:26 +08:00
|
|
|
}
|
2008-06-10 04:56:16 +08:00
|
|
|
|
2011-03-23 07:30:27 +08:00
|
|
|
/* Check that the hardware responds - this may not work under EFI */
|
|
|
|
|
|
|
|
intensity = hw_data->backlight_ops.get_brightness(NULL);
|
|
|
|
|
|
|
|
if (!intensity) {
|
|
|
|
hw_data->set_brightness(1);
|
|
|
|
if (!hw_data->backlight_ops.get_brightness(NULL))
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
hw_data->set_brightness(0);
|
|
|
|
}
|
|
|
|
|
2011-03-23 07:30:26 +08:00
|
|
|
if (!request_region(hw_data->iostart, hw_data->iolen,
|
|
|
|
"Macbook backlight"))
|
2008-06-10 04:56:16 +08:00
|
|
|
return -ENXIO;
|
|
|
|
|
2010-02-18 05:39:44 +08:00
|
|
|
memset(&props, 0, sizeof(struct backlight_properties));
|
2011-03-23 07:30:21 +08:00
|
|
|
props.type = BACKLIGHT_PLATFORM;
|
2010-02-18 05:39:44 +08:00
|
|
|
props.max_brightness = 15;
|
2011-03-23 07:30:26 +08:00
|
|
|
mb_backlight_device = backlight_device_register("mb_backlight", NULL,
|
|
|
|
NULL, &hw_data->backlight_ops, &props);
|
|
|
|
|
|
|
|
if (IS_ERR(mb_backlight_device)) {
|
|
|
|
release_region(hw_data->iostart, hw_data->iolen);
|
|
|
|
return PTR_ERR(mb_backlight_device);
|
2008-06-10 04:56:16 +08:00
|
|
|
}
|
|
|
|
|
2011-03-23 07:30:26 +08:00
|
|
|
mb_backlight_device->props.brightness =
|
|
|
|
hw_data->backlight_ops.get_brightness(mb_backlight_device);
|
|
|
|
backlight_update_status(mb_backlight_device);
|
2008-06-10 04:56:16 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-23 07:30:26 +08:00
|
|
|
static int __devexit mb_bl_remove(struct acpi_device *dev, int type)
|
2008-06-10 04:56:16 +08:00
|
|
|
{
|
2011-03-23 07:30:26 +08:00
|
|
|
backlight_device_unregister(mb_backlight_device);
|
2008-06-10 04:56:16 +08:00
|
|
|
|
2011-03-23 07:30:26 +08:00
|
|
|
release_region(hw_data->iostart, hw_data->iolen);
|
|
|
|
hw_data = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct acpi_device_id mb_bl_ids[] = {
|
|
|
|
{"APP0002", 0},
|
|
|
|
{"", 0},
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct acpi_driver mb_bl_driver = {
|
|
|
|
.name = "Macbook backlight",
|
|
|
|
.ids = mb_bl_ids,
|
|
|
|
.ops = {
|
|
|
|
.add = mb_bl_add,
|
|
|
|
.remove = mb_bl_remove,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init mb_init(void)
|
|
|
|
{
|
|
|
|
return acpi_bus_register_driver(&mb_bl_driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit mb_exit(void)
|
|
|
|
{
|
|
|
|
acpi_bus_unregister_driver(&mb_bl_driver);
|
2008-06-10 04:56:16 +08:00
|
|
|
}
|
|
|
|
|
2011-03-23 07:30:26 +08:00
|
|
|
module_init(mb_init);
|
|
|
|
module_exit(mb_exit);
|
2008-06-10 04:56:16 +08:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
|
2011-03-23 07:30:26 +08:00
|
|
|
MODULE_DESCRIPTION("Macbook Backlight Driver");
|
2008-06-10 04:56:16 +08:00
|
|
|
MODULE_LICENSE("GPL");
|
2011-03-23 07:30:26 +08:00
|
|
|
MODULE_DEVICE_TABLE(acpi, mb_bl_ids);
|