1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
/*
* Backlight Driver for Macbooks
*
* 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>
#include <linux/pci.h>
#include <linux/acpi.h>
static struct backlight_device *mb_backlight_device;
struct hw_data {
/* I/O resource to allocate. */
unsigned long iostart;
unsigned long iolen;
/* Backlight operations structure. */
const struct backlight_ops backlight_ops;
void (*set_brightness)(int);
};
static const struct hw_data *hw_data;
#define DRIVER "mb_backlight: "
/* Module parameters. */
static int debug;
module_param_named(debug, debug, int, 0644);
MODULE_PARM_DESC(debug, "Set to one to enable debugging messages.");
/*
* Implementation for MacBooks with Intel chipset.
*/
static void intel_chipset_set_brightness(int intensity)
{
outb(0x04 | (intensity << 4), 0xb3);
outb(0xbf, 0xb2);
}
static int intel_chipset_send_intensity(struct backlight_device *bd)
{
int intensity = bd->props.brightness;
if (debug)
printk(KERN_DEBUG DRIVER "setting brightness to %d\n",
intensity);
intel_chipset_set_brightness(intensity);
return 0;
}
static int intel_chipset_get_intensity(struct backlight_device *bd)
{
int intensity;
outb(0x03, 0xb3);
outb(0xbf, 0xb2);
intensity = inb(0xb3) >> 4;
if (debug)
printk(KERN_DEBUG DRIVER "read brightness of %d\n",
intensity);
return intensity;
}
static const struct hw_data intel_chipset_data = {
.iostart = 0xb2,
.iolen = 2,
.backlight_ops = {
.options = BL_CORE_SUSPENDRESUME,
.get_brightness = intel_chipset_get_intensity,
.update_status = intel_chipset_send_intensity,
},
.set_brightness = intel_chipset_set_brightness,
};
/*
* Implementation for MacBooks with Nvidia chipset.
*/
static void nvidia_chipset_set_brightness(int intensity)
{
outb(0x04 | (intensity << 4), 0x52f);
outb(0xbf, 0x52e);
}
static int nvidia_chipset_send_intensity(struct backlight_device *bd)
{
int intensity = bd->props.brightness;
if (debug)
printk(KERN_DEBUG DRIVER "setting brightness to %d\n",
intensity);
nvidia_chipset_set_brightness(intensity);
return 0;
}
static int nvidia_chipset_get_intensity(struct backlight_device *bd)
{
int intensity;
outb(0x03, 0x52f);
outb(0xbf, 0x52e);
intensity = inb(0x52f) >> 4;
if (debug)
printk(KERN_DEBUG DRIVER "read brightness of %d\n",
intensity);
return intensity;
}
static const struct hw_data nvidia_chipset_data = {
.iostart = 0x52e,
.iolen = 2,
.backlight_ops = {
.options = BL_CORE_SUSPENDRESUME,
.get_brightness = nvidia_chipset_get_intensity,
.update_status = nvidia_chipset_send_intensity
},
.set_brightness = nvidia_chipset_set_brightness,
};
static int __devinit mb_bl_add(struct acpi_device *dev)
{
struct backlight_properties props;
struct pci_dev *host;
host = pci_get_bus_and_slot(0, 0);
if (!host) {
printk(KERN_ERR DRIVER "unable to find PCI host\n");
return -ENODEV;
}
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");
return -ENODEV;
}
if (!request_region(hw_data->iostart, hw_data->iolen,
"Macbook backlight"))
return -ENXIO;
memset(&props, 0, sizeof(struct backlight_properties));
props.type = BACKLIGHT_PLATFORM;
props.max_brightness = 15;
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);
}
mb_backlight_device->props.brightness =
hw_data->backlight_ops.get_brightness(mb_backlight_device);
backlight_update_status(mb_backlight_device);
return 0;
}
static int __devexit mb_bl_remove(struct acpi_device *dev, int type)
{
backlight_device_unregister(mb_backlight_device);
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);
}
module_init(mb_init);
module_exit(mb_exit);
MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
MODULE_DESCRIPTION("Macbook Backlight Driver");
MODULE_LICENSE("GPL");
MODULE_DEVICE_TABLE(acpi, mb_bl_ids);
|