blob: ef5ca0d6cd208b1fc3d6f9f69c4ef3eea82cc1c7 [file] [log] [blame]
Matthew Garrett7be35c72008-06-09 21:56:16 +01001/*
Matthew Garrett39b3dee2011-03-22 16:30:28 -07002 * Backlight Driver for Intel-based Apples
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>
Seth Forshee83e72dd2012-03-16 14:41:21 -050027#include <linux/atomic.h>
H Hartley Sweeten1615d2102012-05-29 15:07:14 -070028#include <linux/apple_bl.h>
Matthew Garrett7be35c72008-06-09 21:56:16 +010029
Matthew Garrett39b3dee2011-03-22 16:30:28 -070030static struct backlight_device *apple_backlight_device;
Matthew Garrett7be35c72008-06-09 21:56:16 +010031
Matthew Garrett23a98472011-03-22 16:30:26 -070032struct hw_data {
Mario Schwalbec78a6282009-01-11 00:11:34 +000033 /* I/O resource to allocate. */
34 unsigned long iostart;
35 unsigned long iolen;
36 /* Backlight operations structure. */
Emese Revfy9905a432009-12-14 00:58:57 +010037 const struct backlight_ops backlight_ops;
Matthew Garrett23a98472011-03-22 16:30:26 -070038 void (*set_brightness)(int);
Matthew Garrett7be35c72008-06-09 21:56:16 +010039};
40
Matthew Garrett23a98472011-03-22 16:30:26 -070041static const struct hw_data *hw_data;
42
Matthew Garrett39b3dee2011-03-22 16:30:28 -070043#define DRIVER "apple_backlight: "
Matthew Garrett23a98472011-03-22 16:30:26 -070044
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000045/* Module parameters. */
46static int debug;
47module_param_named(debug, debug, int, 0644);
48MODULE_PARM_DESC(debug, "Set to one to enable debugging messages.");
49
Mario Schwalbec78a6282009-01-11 00:11:34 +000050/*
Matthew Garrett39b3dee2011-03-22 16:30:28 -070051 * Implementation for machines with Intel chipset.
Mario Schwalbec78a6282009-01-11 00:11:34 +000052 */
Matthew Garrett23a98472011-03-22 16:30:26 -070053static void intel_chipset_set_brightness(int intensity)
54{
55 outb(0x04 | (intensity << 4), 0xb3);
56 outb(0xbf, 0xb2);
57}
58
Mario Schwalbec78a6282009-01-11 00:11:34 +000059static int intel_chipset_send_intensity(struct backlight_device *bd)
Matthew Garrett7be35c72008-06-09 21:56:16 +010060{
61 int intensity = bd->props.brightness;
62
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000063 if (debug)
Matthew Garrett23a98472011-03-22 16:30:26 -070064 printk(KERN_DEBUG DRIVER "setting brightness to %d\n",
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000065 intensity);
66
Matthew Garrett23a98472011-03-22 16:30:26 -070067 intel_chipset_set_brightness(intensity);
Matthew Garrett7be35c72008-06-09 21:56:16 +010068 return 0;
69}
70
Mario Schwalbec78a6282009-01-11 00:11:34 +000071static int intel_chipset_get_intensity(struct backlight_device *bd)
Matthew Garrett7be35c72008-06-09 21:56:16 +010072{
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000073 int intensity;
74
Matthew Garrett7be35c72008-06-09 21:56:16 +010075 outb(0x03, 0xb3);
76 outb(0xbf, 0xb2);
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000077 intensity = inb(0xb3) >> 4;
78
79 if (debug)
Matthew Garrett23a98472011-03-22 16:30:26 -070080 printk(KERN_DEBUG DRIVER "read brightness of %d\n",
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000081 intensity);
82
83 return intensity;
Matthew Garrett7be35c72008-06-09 21:56:16 +010084}
85
Matthew Garrett23a98472011-03-22 16:30:26 -070086static const struct hw_data intel_chipset_data = {
Mario Schwalbec78a6282009-01-11 00:11:34 +000087 .iostart = 0xb2,
88 .iolen = 2,
89 .backlight_ops = {
90 .options = BL_CORE_SUSPENDRESUME,
91 .get_brightness = intel_chipset_get_intensity,
92 .update_status = intel_chipset_send_intensity,
Matthew Garrett23a98472011-03-22 16:30:26 -070093 },
94 .set_brightness = intel_chipset_set_brightness,
Mario Schwalbec78a6282009-01-11 00:11:34 +000095};
96
97/*
Matthew Garrett39b3dee2011-03-22 16:30:28 -070098 * Implementation for machines with Nvidia chipset.
Mario Schwalbec78a6282009-01-11 00:11:34 +000099 */
Matthew Garrett23a98472011-03-22 16:30:26 -0700100static void nvidia_chipset_set_brightness(int intensity)
101{
102 outb(0x04 | (intensity << 4), 0x52f);
103 outb(0xbf, 0x52e);
104}
105
Mario Schwalbec78a6282009-01-11 00:11:34 +0000106static int nvidia_chipset_send_intensity(struct backlight_device *bd)
107{
108 int intensity = bd->props.brightness;
109
Mario Schwalbe1a468ba2009-01-11 00:19:31 +0000110 if (debug)
Matthew Garrett23a98472011-03-22 16:30:26 -0700111 printk(KERN_DEBUG DRIVER "setting brightness to %d\n",
Mario Schwalbe1a468ba2009-01-11 00:19:31 +0000112 intensity);
113
Matthew Garrett23a98472011-03-22 16:30:26 -0700114 nvidia_chipset_set_brightness(intensity);
Mario Schwalbec78a6282009-01-11 00:11:34 +0000115 return 0;
116}
117
118static int nvidia_chipset_get_intensity(struct backlight_device *bd)
119{
Mario Schwalbe1a468ba2009-01-11 00:19:31 +0000120 int intensity;
121
Mario Schwalbec78a6282009-01-11 00:11:34 +0000122 outb(0x03, 0x52f);
123 outb(0xbf, 0x52e);
Mario Schwalbe1a468ba2009-01-11 00:19:31 +0000124 intensity = inb(0x52f) >> 4;
125
126 if (debug)
Matthew Garrett23a98472011-03-22 16:30:26 -0700127 printk(KERN_DEBUG DRIVER "read brightness of %d\n",
Mario Schwalbe1a468ba2009-01-11 00:19:31 +0000128 intensity);
129
130 return intensity;
Mario Schwalbec78a6282009-01-11 00:11:34 +0000131}
132
Matthew Garrett23a98472011-03-22 16:30:26 -0700133static const struct hw_data nvidia_chipset_data = {
Mario Schwalbec78a6282009-01-11 00:11:34 +0000134 .iostart = 0x52e,
135 .iolen = 2,
136 .backlight_ops = {
137 .options = BL_CORE_SUSPENDRESUME,
138 .get_brightness = nvidia_chipset_get_intensity,
139 .update_status = nvidia_chipset_send_intensity
Matthew Garrett23a98472011-03-22 16:30:26 -0700140 },
141 .set_brightness = nvidia_chipset_set_brightness,
Mario Schwalbec78a6282009-01-11 00:11:34 +0000142};
143
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700144static int __devinit apple_bl_add(struct acpi_device *dev)
Matthew Garrett7be35c72008-06-09 21:56:16 +0100145{
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500146 struct backlight_properties props;
Matthew Garrett23a98472011-03-22 16:30:26 -0700147 struct pci_dev *host;
Matthew Garrett99fd28e2011-03-22 16:30:27 -0700148 int intensity;
Matthew Garrett7be35c72008-06-09 21:56:16 +0100149
Matthew Garrett23a98472011-03-22 16:30:26 -0700150 host = pci_get_bus_and_slot(0, 0);
151
152 if (!host) {
153 printk(KERN_ERR DRIVER "unable to find PCI host\n");
154 return -ENODEV;
155 }
156
157 if (host->vendor == PCI_VENDOR_ID_INTEL)
158 hw_data = &intel_chipset_data;
159 else if (host->vendor == PCI_VENDOR_ID_NVIDIA)
160 hw_data = &nvidia_chipset_data;
161
162 pci_dev_put(host);
163
164 if (!hw_data) {
165 printk(KERN_ERR DRIVER "unknown hardware\n");
166 return -ENODEV;
167 }
168
Matthew Garrett99fd28e2011-03-22 16:30:27 -0700169 /* Check that the hardware responds - this may not work under EFI */
170
171 intensity = hw_data->backlight_ops.get_brightness(NULL);
172
173 if (!intensity) {
174 hw_data->set_brightness(1);
175 if (!hw_data->backlight_ops.get_brightness(NULL))
176 return -ENODEV;
177
178 hw_data->set_brightness(0);
179 }
180
Matthew Garrett23a98472011-03-22 16:30:26 -0700181 if (!request_region(hw_data->iostart, hw_data->iolen,
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700182 "Apple backlight"))
Matthew Garrett7be35c72008-06-09 21:56:16 +0100183 return -ENXIO;
184
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500185 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700186 props.type = BACKLIGHT_PLATFORM;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500187 props.max_brightness = 15;
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700188 apple_backlight_device = backlight_device_register("apple_backlight",
189 NULL, NULL, &hw_data->backlight_ops, &props);
Matthew Garrett23a98472011-03-22 16:30:26 -0700190
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700191 if (IS_ERR(apple_backlight_device)) {
Matthew Garrett23a98472011-03-22 16:30:26 -0700192 release_region(hw_data->iostart, hw_data->iolen);
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700193 return PTR_ERR(apple_backlight_device);
Matthew Garrett7be35c72008-06-09 21:56:16 +0100194 }
195
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700196 apple_backlight_device->props.brightness =
197 hw_data->backlight_ops.get_brightness(apple_backlight_device);
198 backlight_update_status(apple_backlight_device);
Matthew Garrett7be35c72008-06-09 21:56:16 +0100199
200 return 0;
201}
202
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700203static int __devexit apple_bl_remove(struct acpi_device *dev, int type)
Matthew Garrett7be35c72008-06-09 21:56:16 +0100204{
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700205 backlight_device_unregister(apple_backlight_device);
Matthew Garrett7be35c72008-06-09 21:56:16 +0100206
Matthew Garrett23a98472011-03-22 16:30:26 -0700207 release_region(hw_data->iostart, hw_data->iolen);
208 hw_data = NULL;
209 return 0;
Matthew Garrett7be35c72008-06-09 21:56:16 +0100210}
211
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700212static const struct acpi_device_id apple_bl_ids[] = {
Matthew Garrett23a98472011-03-22 16:30:26 -0700213 {"APP0002", 0},
214 {"", 0},
215};
216
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700217static struct acpi_driver apple_bl_driver = {
218 .name = "Apple backlight",
219 .ids = apple_bl_ids,
Matthew Garrett23a98472011-03-22 16:30:26 -0700220 .ops = {
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700221 .add = apple_bl_add,
222 .remove = apple_bl_remove,
Matthew Garrett23a98472011-03-22 16:30:26 -0700223 },
224};
225
Seth Forshee83e72dd2012-03-16 14:41:21 -0500226static atomic_t apple_bl_registered = ATOMIC_INIT(0);
227
228int apple_bl_register(void)
229{
230 if (atomic_xchg(&apple_bl_registered, 1) == 0)
231 return acpi_bus_register_driver(&apple_bl_driver);
232
233 return 0;
234}
235EXPORT_SYMBOL_GPL(apple_bl_register);
236
237void apple_bl_unregister(void)
238{
239 if (atomic_xchg(&apple_bl_registered, 0) == 1)
240 acpi_bus_unregister_driver(&apple_bl_driver);
241}
242EXPORT_SYMBOL_GPL(apple_bl_unregister);
243
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700244static int __init apple_bl_init(void)
Matthew Garrett23a98472011-03-22 16:30:26 -0700245{
Seth Forshee83e72dd2012-03-16 14:41:21 -0500246 return apple_bl_register();
Matthew Garrett23a98472011-03-22 16:30:26 -0700247}
248
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700249static void __exit apple_bl_exit(void)
Matthew Garrett23a98472011-03-22 16:30:26 -0700250{
Seth Forshee83e72dd2012-03-16 14:41:21 -0500251 apple_bl_unregister();
Matthew Garrett23a98472011-03-22 16:30:26 -0700252}
253
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700254module_init(apple_bl_init);
255module_exit(apple_bl_exit);
Matthew Garrett7be35c72008-06-09 21:56:16 +0100256
257MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700258MODULE_DESCRIPTION("Apple Backlight Driver");
Matthew Garrett7be35c72008-06-09 21:56:16 +0100259MODULE_LICENSE("GPL");
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700260MODULE_DEVICE_TABLE(acpi, apple_bl_ids);
261MODULE_ALIAS("mbp_nvidia_bl");