Commit | Line | Data |
---|---|---|
7be35c72 | 1 | /* |
39b3dee7 | 2 | * Backlight Driver for Intel-based Apples |
7be35c72 MG |
3 | * |
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 | ||
7b12c1b9 JH |
19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
20 | ||
7be35c72 MG |
21 | #include <linux/module.h> |
22 | #include <linux/kernel.h> | |
23 | #include <linux/init.h> | |
7be35c72 MG |
24 | #include <linux/backlight.h> |
25 | #include <linux/err.h> | |
7be35c72 | 26 | #include <linux/io.h> |
23a9847f MG |
27 | #include <linux/pci.h> |
28 | #include <linux/acpi.h> | |
83e72dd9 | 29 | #include <linux/atomic.h> |
1615d210 | 30 | #include <linux/apple_bl.h> |
7be35c72 | 31 | |
39b3dee7 | 32 | static struct backlight_device *apple_backlight_device; |
7be35c72 | 33 | |
23a9847f | 34 | struct hw_data { |
c78a6288 MS |
35 | /* I/O resource to allocate. */ |
36 | unsigned long iostart; | |
37 | unsigned long iolen; | |
38 | /* Backlight operations structure. */ | |
9905a43b | 39 | const struct backlight_ops backlight_ops; |
23a9847f | 40 | void (*set_brightness)(int); |
7be35c72 MG |
41 | }; |
42 | ||
23a9847f MG |
43 | static const struct hw_data *hw_data; |
44 | ||
1a468ba1 MS |
45 | /* Module parameters. */ |
46 | static int debug; | |
47 | module_param_named(debug, debug, int, 0644); | |
48 | MODULE_PARM_DESC(debug, "Set to one to enable debugging messages."); | |
49 | ||
c78a6288 | 50 | /* |
39b3dee7 | 51 | * Implementation for machines with Intel chipset. |
c78a6288 | 52 | */ |
23a9847f MG |
53 | static void intel_chipset_set_brightness(int intensity) |
54 | { | |
55 | outb(0x04 | (intensity << 4), 0xb3); | |
56 | outb(0xbf, 0xb2); | |
57 | } | |
58 | ||
c78a6288 | 59 | static int intel_chipset_send_intensity(struct backlight_device *bd) |
7be35c72 MG |
60 | { |
61 | int intensity = bd->props.brightness; | |
62 | ||
1a468ba1 | 63 | if (debug) |
7b12c1b9 | 64 | pr_debug("setting brightness to %d\n", intensity); |
1a468ba1 | 65 | |
23a9847f | 66 | intel_chipset_set_brightness(intensity); |
7be35c72 MG |
67 | return 0; |
68 | } | |
69 | ||
c78a6288 | 70 | static int intel_chipset_get_intensity(struct backlight_device *bd) |
7be35c72 | 71 | { |
1a468ba1 MS |
72 | int intensity; |
73 | ||
7be35c72 MG |
74 | outb(0x03, 0xb3); |
75 | outb(0xbf, 0xb2); | |
1a468ba1 MS |
76 | intensity = inb(0xb3) >> 4; |
77 | ||
78 | if (debug) | |
7b12c1b9 | 79 | pr_debug("read brightness of %d\n", intensity); |
1a468ba1 MS |
80 | |
81 | return intensity; | |
7be35c72 MG |
82 | } |
83 | ||
23a9847f | 84 | static const struct hw_data intel_chipset_data = { |
c78a6288 MS |
85 | .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, | |
23a9847f MG |
91 | }, |
92 | .set_brightness = intel_chipset_set_brightness, | |
c78a6288 MS |
93 | }; |
94 | ||
95 | /* | |
39b3dee7 | 96 | * Implementation for machines with Nvidia chipset. |
c78a6288 | 97 | */ |
23a9847f MG |
98 | static void nvidia_chipset_set_brightness(int intensity) |
99 | { | |
100 | outb(0x04 | (intensity << 4), 0x52f); | |
101 | outb(0xbf, 0x52e); | |
102 | } | |
103 | ||
c78a6288 MS |
104 | static int nvidia_chipset_send_intensity(struct backlight_device *bd) |
105 | { | |
106 | int intensity = bd->props.brightness; | |
107 | ||
1a468ba1 | 108 | if (debug) |
7b12c1b9 | 109 | pr_debug("setting brightness to %d\n", intensity); |
1a468ba1 | 110 | |
23a9847f | 111 | nvidia_chipset_set_brightness(intensity); |
c78a6288 MS |
112 | return 0; |
113 | } | |
114 | ||
115 | static int nvidia_chipset_get_intensity(struct backlight_device *bd) | |
116 | { | |
1a468ba1 MS |
117 | int intensity; |
118 | ||
c78a6288 MS |
119 | outb(0x03, 0x52f); |
120 | outb(0xbf, 0x52e); | |
1a468ba1 MS |
121 | intensity = inb(0x52f) >> 4; |
122 | ||
123 | if (debug) | |
7b12c1b9 | 124 | pr_debug("read brightness of %d\n", intensity); |
1a468ba1 MS |
125 | |
126 | return intensity; | |
c78a6288 MS |
127 | } |
128 | ||
23a9847f | 129 | static const struct hw_data nvidia_chipset_data = { |
c78a6288 MS |
130 | .iostart = 0x52e, |
131 | .iolen = 2, | |
132 | .backlight_ops = { | |
133 | .options = BL_CORE_SUSPENDRESUME, | |
134 | .get_brightness = nvidia_chipset_get_intensity, | |
135 | .update_status = nvidia_chipset_send_intensity | |
23a9847f MG |
136 | }, |
137 | .set_brightness = nvidia_chipset_set_brightness, | |
c78a6288 MS |
138 | }; |
139 | ||
39b3dee7 | 140 | static int __devinit apple_bl_add(struct acpi_device *dev) |
c78a6288 | 141 | { |
23a9847f MG |
142 | struct backlight_properties props; |
143 | struct pci_dev *host; | |
99fd28e1 | 144 | int intensity; |
c78a6288 | 145 | |
23a9847f | 146 | host = pci_get_bus_and_slot(0, 0); |
c78a6288 | 147 | |
23a9847f | 148 | if (!host) { |
7b12c1b9 | 149 | pr_err("unable to find PCI host\n"); |
23a9847f MG |
150 | return -ENODEV; |
151 | } | |
7be35c72 | 152 | |
23a9847f MG |
153 | if (host->vendor == PCI_VENDOR_ID_INTEL) |
154 | hw_data = &intel_chipset_data; | |
155 | else if (host->vendor == PCI_VENDOR_ID_NVIDIA) | |
156 | hw_data = &nvidia_chipset_data; | |
157 | ||
158 | pci_dev_put(host); | |
159 | ||
160 | if (!hw_data) { | |
7b12c1b9 | 161 | pr_err("unknown hardware\n"); |
7be35c72 | 162 | return -ENODEV; |
23a9847f | 163 | } |
7be35c72 | 164 | |
99fd28e1 MG |
165 | /* Check that the hardware responds - this may not work under EFI */ |
166 | ||
167 | intensity = hw_data->backlight_ops.get_brightness(NULL); | |
168 | ||
169 | if (!intensity) { | |
170 | hw_data->set_brightness(1); | |
171 | if (!hw_data->backlight_ops.get_brightness(NULL)) | |
172 | return -ENODEV; | |
173 | ||
174 | hw_data->set_brightness(0); | |
175 | } | |
176 | ||
23a9847f | 177 | if (!request_region(hw_data->iostart, hw_data->iolen, |
39b3dee7 | 178 | "Apple backlight")) |
7be35c72 MG |
179 | return -ENXIO; |
180 | ||
a19a6ee6 | 181 | memset(&props, 0, sizeof(struct backlight_properties)); |
bb7ca747 | 182 | props.type = BACKLIGHT_PLATFORM; |
a19a6ee6 | 183 | props.max_brightness = 15; |
39b3dee7 MG |
184 | apple_backlight_device = backlight_device_register("apple_backlight", |
185 | NULL, NULL, &hw_data->backlight_ops, &props); | |
23a9847f | 186 | |
39b3dee7 | 187 | if (IS_ERR(apple_backlight_device)) { |
23a9847f | 188 | release_region(hw_data->iostart, hw_data->iolen); |
39b3dee7 | 189 | return PTR_ERR(apple_backlight_device); |
7be35c72 MG |
190 | } |
191 | ||
39b3dee7 MG |
192 | apple_backlight_device->props.brightness = |
193 | hw_data->backlight_ops.get_brightness(apple_backlight_device); | |
194 | backlight_update_status(apple_backlight_device); | |
7be35c72 MG |
195 | |
196 | return 0; | |
197 | } | |
198 | ||
39b3dee7 | 199 | static int __devexit apple_bl_remove(struct acpi_device *dev, int type) |
7be35c72 | 200 | { |
39b3dee7 | 201 | backlight_device_unregister(apple_backlight_device); |
7be35c72 | 202 | |
23a9847f MG |
203 | release_region(hw_data->iostart, hw_data->iolen); |
204 | hw_data = NULL; | |
205 | return 0; | |
206 | } | |
207 | ||
39b3dee7 | 208 | static const struct acpi_device_id apple_bl_ids[] = { |
23a9847f MG |
209 | {"APP0002", 0}, |
210 | {"", 0}, | |
211 | }; | |
212 | ||
39b3dee7 MG |
213 | static struct acpi_driver apple_bl_driver = { |
214 | .name = "Apple backlight", | |
215 | .ids = apple_bl_ids, | |
23a9847f | 216 | .ops = { |
39b3dee7 MG |
217 | .add = apple_bl_add, |
218 | .remove = apple_bl_remove, | |
23a9847f MG |
219 | }, |
220 | }; | |
221 | ||
83e72dd9 SF |
222 | static atomic_t apple_bl_registered = ATOMIC_INIT(0); |
223 | ||
224 | int apple_bl_register(void) | |
225 | { | |
226 | if (atomic_xchg(&apple_bl_registered, 1) == 0) | |
227 | return acpi_bus_register_driver(&apple_bl_driver); | |
228 | ||
229 | return 0; | |
230 | } | |
231 | EXPORT_SYMBOL_GPL(apple_bl_register); | |
232 | ||
233 | void apple_bl_unregister(void) | |
234 | { | |
235 | if (atomic_xchg(&apple_bl_registered, 0) == 1) | |
236 | acpi_bus_unregister_driver(&apple_bl_driver); | |
237 | } | |
238 | EXPORT_SYMBOL_GPL(apple_bl_unregister); | |
239 | ||
39b3dee7 | 240 | static int __init apple_bl_init(void) |
23a9847f | 241 | { |
83e72dd9 | 242 | return apple_bl_register(); |
23a9847f MG |
243 | } |
244 | ||
39b3dee7 | 245 | static void __exit apple_bl_exit(void) |
23a9847f | 246 | { |
83e72dd9 | 247 | apple_bl_unregister(); |
7be35c72 MG |
248 | } |
249 | ||
39b3dee7 MG |
250 | module_init(apple_bl_init); |
251 | module_exit(apple_bl_exit); | |
7be35c72 MG |
252 | |
253 | MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>"); | |
39b3dee7 | 254 | MODULE_DESCRIPTION("Apple Backlight Driver"); |
7be35c72 | 255 | MODULE_LICENSE("GPL"); |
39b3dee7 MG |
256 | MODULE_DEVICE_TABLE(acpi, apple_bl_ids); |
257 | MODULE_ALIAS("mbp_nvidia_bl"); |