blob: b22cfb24bd55ae57f66c3fe360a176e2db944411 [file] [log] [blame]
Matthew Garrett7be35c72008-06-09 21:56:16 +01001/*
Matthew Garrett23a98472011-03-22 16:30:26 -07002 * Backlight Driver for Macbooks
Matthew Garrett7be35c72008-06-09 21:56:16 +01003 *
4 * Copyright (c) Red Hat <mjg@redhat.com>
5 * Based on code from Pommed:
6 * Copyright (C) 2006 Nicolas Boichat <nicolas @boichat.ch>
7 * Copyright (C) 2006 Felipe Alfaro Solana <felipe_alfaro @linuxmail.org>
8 * Copyright (C) 2007 Julien BLACHE <jb@jblache.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 * This driver triggers SMIs which cause the firmware to change the
15 * backlight brightness. This is icky in many ways, but it's impractical to
16 * get at the firmware code in order to figure out what it's actually doing.
17 */
18
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/init.h>
Matthew Garrett7be35c72008-06-09 21:56:16 +010022#include <linux/backlight.h>
23#include <linux/err.h>
Matthew Garrett7be35c72008-06-09 21:56:16 +010024#include <linux/io.h>
Matthew Garrett23a98472011-03-22 16:30:26 -070025#include <linux/pci.h>
26#include <linux/acpi.h>
Matthew Garrett7be35c72008-06-09 21:56:16 +010027
Matthew Garrett23a98472011-03-22 16:30:26 -070028static struct backlight_device *mb_backlight_device;
Matthew Garrett7be35c72008-06-09 21:56:16 +010029
Matthew Garrett23a98472011-03-22 16:30:26 -070030struct hw_data {
Mario Schwalbec78a6282009-01-11 00:11:34 +000031 /* I/O resource to allocate. */
32 unsigned long iostart;
33 unsigned long iolen;
34 /* Backlight operations structure. */
Emese Revfy9905a432009-12-14 00:58:57 +010035 const struct backlight_ops backlight_ops;
Matthew Garrett23a98472011-03-22 16:30:26 -070036 void (*set_brightness)(int);
Matthew Garrett7be35c72008-06-09 21:56:16 +010037};
38
Matthew Garrett23a98472011-03-22 16:30:26 -070039static const struct hw_data *hw_data;
40
41#define DRIVER "mb_backlight: "
42
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000043/* Module parameters. */
44static int debug;
45module_param_named(debug, debug, int, 0644);
46MODULE_PARM_DESC(debug, "Set to one to enable debugging messages.");
47
Mario Schwalbec78a6282009-01-11 00:11:34 +000048/*
49 * Implementation for MacBooks with Intel chipset.
50 */
Matthew Garrett23a98472011-03-22 16:30:26 -070051static void intel_chipset_set_brightness(int intensity)
52{
53 outb(0x04 | (intensity << 4), 0xb3);
54 outb(0xbf, 0xb2);
55}
56
Mario Schwalbec78a6282009-01-11 00:11:34 +000057static int intel_chipset_send_intensity(struct backlight_device *bd)
Matthew Garrett7be35c72008-06-09 21:56:16 +010058{
59 int intensity = bd->props.brightness;
60
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000061 if (debug)
Matthew Garrett23a98472011-03-22 16:30:26 -070062 printk(KERN_DEBUG DRIVER "setting brightness to %d\n",
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000063 intensity);
64
Matthew Garrett23a98472011-03-22 16:30:26 -070065 intel_chipset_set_brightness(intensity);
Matthew Garrett7be35c72008-06-09 21:56:16 +010066 return 0;
67}
68
Mario Schwalbec78a6282009-01-11 00:11:34 +000069static int intel_chipset_get_intensity(struct backlight_device *bd)
Matthew Garrett7be35c72008-06-09 21:56:16 +010070{
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000071 int intensity;
72
Matthew Garrett7be35c72008-06-09 21:56:16 +010073 outb(0x03, 0xb3);
74 outb(0xbf, 0xb2);
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000075 intensity = inb(0xb3) >> 4;
76
77 if (debug)
Matthew Garrett23a98472011-03-22 16:30:26 -070078 printk(KERN_DEBUG DRIVER "read brightness of %d\n",
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000079 intensity);
80
81 return intensity;
Matthew Garrett7be35c72008-06-09 21:56:16 +010082}
83
Matthew Garrett23a98472011-03-22 16:30:26 -070084static const struct hw_data intel_chipset_data = {
Mario Schwalbec78a6282009-01-11 00:11:34 +000085 .iostart = 0xb2,
86 .iolen = 2,
87 .backlight_ops = {
88 .options = BL_CORE_SUSPENDRESUME,
89 .get_brightness = intel_chipset_get_intensity,
90 .update_status = intel_chipset_send_intensity,
Matthew Garrett23a98472011-03-22 16:30:26 -070091 },
92 .set_brightness = intel_chipset_set_brightness,
Mario Schwalbec78a6282009-01-11 00:11:34 +000093};
94
95/*
96 * Implementation for MacBooks with Nvidia chipset.
97 */
Matthew Garrett23a98472011-03-22 16:30:26 -070098static void nvidia_chipset_set_brightness(int intensity)
99{
100 outb(0x04 | (intensity << 4), 0x52f);
101 outb(0xbf, 0x52e);
102}
103
Mario Schwalbec78a6282009-01-11 00:11:34 +0000104static int nvidia_chipset_send_intensity(struct backlight_device *bd)
105{
106 int intensity = bd->props.brightness;
107
Mario Schwalbe1a468ba2009-01-11 00:19:31 +0000108 if (debug)
Matthew Garrett23a98472011-03-22 16:30:26 -0700109 printk(KERN_DEBUG DRIVER "setting brightness to %d\n",
Mario Schwalbe1a468ba2009-01-11 00:19:31 +0000110 intensity);
111
Matthew Garrett23a98472011-03-22 16:30:26 -0700112 nvidia_chipset_set_brightness(intensity);
Mario Schwalbec78a6282009-01-11 00:11:34 +0000113 return 0;
114}
115
116static int nvidia_chipset_get_intensity(struct backlight_device *bd)
117{
Mario Schwalbe1a468ba2009-01-11 00:19:31 +0000118 int intensity;
119
Mario Schwalbec78a6282009-01-11 00:11:34 +0000120 outb(0x03, 0x52f);
121 outb(0xbf, 0x52e);
Mario Schwalbe1a468ba2009-01-11 00:19:31 +0000122 intensity = inb(0x52f) >> 4;
123
124 if (debug)
Matthew Garrett23a98472011-03-22 16:30:26 -0700125 printk(KERN_DEBUG DRIVER "read brightness of %d\n",
Mario Schwalbe1a468ba2009-01-11 00:19:31 +0000126 intensity);
127
128 return intensity;
Mario Schwalbec78a6282009-01-11 00:11:34 +0000129}
130
Matthew Garrett23a98472011-03-22 16:30:26 -0700131static const struct hw_data nvidia_chipset_data = {
Mario Schwalbec78a6282009-01-11 00:11:34 +0000132 .iostart = 0x52e,
133 .iolen = 2,
134 .backlight_ops = {
135 .options = BL_CORE_SUSPENDRESUME,
136 .get_brightness = nvidia_chipset_get_intensity,
137 .update_status = nvidia_chipset_send_intensity
Matthew Garrett23a98472011-03-22 16:30:26 -0700138 },
139 .set_brightness = nvidia_chipset_set_brightness,
Mario Schwalbec78a6282009-01-11 00:11:34 +0000140};
141
Matthew Garrett23a98472011-03-22 16:30:26 -0700142static int __devinit mb_bl_add(struct acpi_device *dev)
Matthew Garrett7be35c72008-06-09 21:56:16 +0100143{
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500144 struct backlight_properties props;
Matthew Garrett23a98472011-03-22 16:30:26 -0700145 struct pci_dev *host;
Matthew Garrett7be35c72008-06-09 21:56:16 +0100146
Matthew Garrett23a98472011-03-22 16:30:26 -0700147 host = pci_get_bus_and_slot(0, 0);
148
149 if (!host) {
150 printk(KERN_ERR DRIVER "unable to find PCI host\n");
151 return -ENODEV;
152 }
153
154 if (host->vendor == PCI_VENDOR_ID_INTEL)
155 hw_data = &intel_chipset_data;
156 else if (host->vendor == PCI_VENDOR_ID_NVIDIA)
157 hw_data = &nvidia_chipset_data;
158
159 pci_dev_put(host);
160
161 if (!hw_data) {
162 printk(KERN_ERR DRIVER "unknown hardware\n");
163 return -ENODEV;
164 }
165
166 if (!request_region(hw_data->iostart, hw_data->iolen,
167 "Macbook backlight"))
Matthew Garrett7be35c72008-06-09 21:56:16 +0100168 return -ENXIO;
169
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500170 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700171 props.type = BACKLIGHT_PLATFORM;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500172 props.max_brightness = 15;
Matthew Garrett23a98472011-03-22 16:30:26 -0700173 mb_backlight_device = backlight_device_register("mb_backlight", NULL,
174 NULL, &hw_data->backlight_ops, &props);
175
176 if (IS_ERR(mb_backlight_device)) {
177 release_region(hw_data->iostart, hw_data->iolen);
178 return PTR_ERR(mb_backlight_device);
Matthew Garrett7be35c72008-06-09 21:56:16 +0100179 }
180
Matthew Garrett23a98472011-03-22 16:30:26 -0700181 mb_backlight_device->props.brightness =
182 hw_data->backlight_ops.get_brightness(mb_backlight_device);
183 backlight_update_status(mb_backlight_device);
Matthew Garrett7be35c72008-06-09 21:56:16 +0100184
185 return 0;
186}
187
Matthew Garrett23a98472011-03-22 16:30:26 -0700188static int __devexit mb_bl_remove(struct acpi_device *dev, int type)
Matthew Garrett7be35c72008-06-09 21:56:16 +0100189{
Matthew Garrett23a98472011-03-22 16:30:26 -0700190 backlight_device_unregister(mb_backlight_device);
Matthew Garrett7be35c72008-06-09 21:56:16 +0100191
Matthew Garrett23a98472011-03-22 16:30:26 -0700192 release_region(hw_data->iostart, hw_data->iolen);
193 hw_data = NULL;
194 return 0;
Matthew Garrett7be35c72008-06-09 21:56:16 +0100195}
196
Matthew Garrett23a98472011-03-22 16:30:26 -0700197static const struct acpi_device_id mb_bl_ids[] = {
198 {"APP0002", 0},
199 {"", 0},
200};
201
202static struct acpi_driver mb_bl_driver = {
203 .name = "Macbook backlight",
204 .ids = mb_bl_ids,
205 .ops = {
206 .add = mb_bl_add,
207 .remove = mb_bl_remove,
208 },
209};
210
211static int __init mb_init(void)
212{
213 return acpi_bus_register_driver(&mb_bl_driver);
214}
215
216static void __exit mb_exit(void)
217{
218 acpi_bus_unregister_driver(&mb_bl_driver);
219}
220
221module_init(mb_init);
222module_exit(mb_exit);
Matthew Garrett7be35c72008-06-09 21:56:16 +0100223
224MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
Matthew Garrett23a98472011-03-22 16:30:26 -0700225MODULE_DESCRIPTION("Macbook Backlight Driver");
Matthew Garrett7be35c72008-06-09 21:56:16 +0100226MODULE_LICENSE("GPL");
Matthew Garrett23a98472011-03-22 16:30:26 -0700227MODULE_DEVICE_TABLE(acpi, mb_bl_ids);