blob: a523b255e124cc3ff44e4b39d15c9ba4a6f5761a [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>
Matthew Garrett7be35c72008-06-09 21:56:16 +010028
Matthew Garrett39b3dee2011-03-22 16:30:28 -070029static struct backlight_device *apple_backlight_device;
Matthew Garrett7be35c72008-06-09 21:56:16 +010030
Matthew Garrett23a98472011-03-22 16:30:26 -070031struct hw_data {
Mario Schwalbec78a6282009-01-11 00:11:34 +000032 /* I/O resource to allocate. */
33 unsigned long iostart;
34 unsigned long iolen;
35 /* Backlight operations structure. */
Emese Revfy9905a432009-12-14 00:58:57 +010036 const struct backlight_ops backlight_ops;
Matthew Garrett23a98472011-03-22 16:30:26 -070037 void (*set_brightness)(int);
Matthew Garrett7be35c72008-06-09 21:56:16 +010038};
39
Matthew Garrett23a98472011-03-22 16:30:26 -070040static const struct hw_data *hw_data;
41
Matthew Garrett39b3dee2011-03-22 16:30:28 -070042#define DRIVER "apple_backlight: "
Matthew Garrett23a98472011-03-22 16:30:26 -070043
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000044/* Module parameters. */
45static int debug;
46module_param_named(debug, debug, int, 0644);
47MODULE_PARM_DESC(debug, "Set to one to enable debugging messages.");
48
Mario Schwalbec78a6282009-01-11 00:11:34 +000049/*
Matthew Garrett39b3dee2011-03-22 16:30:28 -070050 * Implementation for machines with Intel chipset.
Mario Schwalbec78a6282009-01-11 00:11:34 +000051 */
Matthew Garrett23a98472011-03-22 16:30:26 -070052static void intel_chipset_set_brightness(int intensity)
53{
54 outb(0x04 | (intensity << 4), 0xb3);
55 outb(0xbf, 0xb2);
56}
57
Mario Schwalbec78a6282009-01-11 00:11:34 +000058static int intel_chipset_send_intensity(struct backlight_device *bd)
Matthew Garrett7be35c72008-06-09 21:56:16 +010059{
60 int intensity = bd->props.brightness;
61
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000062 if (debug)
Matthew Garrett23a98472011-03-22 16:30:26 -070063 printk(KERN_DEBUG DRIVER "setting brightness to %d\n",
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000064 intensity);
65
Matthew Garrett23a98472011-03-22 16:30:26 -070066 intel_chipset_set_brightness(intensity);
Matthew Garrett7be35c72008-06-09 21:56:16 +010067 return 0;
68}
69
Mario Schwalbec78a6282009-01-11 00:11:34 +000070static int intel_chipset_get_intensity(struct backlight_device *bd)
Matthew Garrett7be35c72008-06-09 21:56:16 +010071{
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000072 int intensity;
73
Matthew Garrett7be35c72008-06-09 21:56:16 +010074 outb(0x03, 0xb3);
75 outb(0xbf, 0xb2);
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000076 intensity = inb(0xb3) >> 4;
77
78 if (debug)
Matthew Garrett23a98472011-03-22 16:30:26 -070079 printk(KERN_DEBUG DRIVER "read brightness of %d\n",
Mario Schwalbe1a468ba2009-01-11 00:19:31 +000080 intensity);
81
82 return intensity;
Matthew Garrett7be35c72008-06-09 21:56:16 +010083}
84
Matthew Garrett23a98472011-03-22 16:30:26 -070085static const struct hw_data intel_chipset_data = {
Mario Schwalbec78a6282009-01-11 00:11:34 +000086 .iostart = 0xb2,
87 .iolen = 2,
88 .backlight_ops = {
89 .options = BL_CORE_SUSPENDRESUME,
90 .get_brightness = intel_chipset_get_intensity,
91 .update_status = intel_chipset_send_intensity,
Matthew Garrett23a98472011-03-22 16:30:26 -070092 },
93 .set_brightness = intel_chipset_set_brightness,
Mario Schwalbec78a6282009-01-11 00:11:34 +000094};
95
96/*
Matthew Garrett39b3dee2011-03-22 16:30:28 -070097 * Implementation for machines with Nvidia chipset.
Mario Schwalbec78a6282009-01-11 00:11:34 +000098 */
Matthew Garrett23a98472011-03-22 16:30:26 -070099static void nvidia_chipset_set_brightness(int intensity)
100{
101 outb(0x04 | (intensity << 4), 0x52f);
102 outb(0xbf, 0x52e);
103}
104
Mario Schwalbec78a6282009-01-11 00:11:34 +0000105static int nvidia_chipset_send_intensity(struct backlight_device *bd)
106{
107 int intensity = bd->props.brightness;
108
Mario Schwalbe1a468ba2009-01-11 00:19:31 +0000109 if (debug)
Matthew Garrett23a98472011-03-22 16:30:26 -0700110 printk(KERN_DEBUG DRIVER "setting brightness to %d\n",
Mario Schwalbe1a468ba2009-01-11 00:19:31 +0000111 intensity);
112
Matthew Garrett23a98472011-03-22 16:30:26 -0700113 nvidia_chipset_set_brightness(intensity);
Mario Schwalbec78a6282009-01-11 00:11:34 +0000114 return 0;
115}
116
117static int nvidia_chipset_get_intensity(struct backlight_device *bd)
118{
Mario Schwalbe1a468ba2009-01-11 00:19:31 +0000119 int intensity;
120
Mario Schwalbec78a6282009-01-11 00:11:34 +0000121 outb(0x03, 0x52f);
122 outb(0xbf, 0x52e);
Mario Schwalbe1a468ba2009-01-11 00:19:31 +0000123 intensity = inb(0x52f) >> 4;
124
125 if (debug)
Matthew Garrett23a98472011-03-22 16:30:26 -0700126 printk(KERN_DEBUG DRIVER "read brightness of %d\n",
Mario Schwalbe1a468ba2009-01-11 00:19:31 +0000127 intensity);
128
129 return intensity;
Mario Schwalbec78a6282009-01-11 00:11:34 +0000130}
131
Matthew Garrett23a98472011-03-22 16:30:26 -0700132static const struct hw_data nvidia_chipset_data = {
Mario Schwalbec78a6282009-01-11 00:11:34 +0000133 .iostart = 0x52e,
134 .iolen = 2,
135 .backlight_ops = {
136 .options = BL_CORE_SUSPENDRESUME,
137 .get_brightness = nvidia_chipset_get_intensity,
138 .update_status = nvidia_chipset_send_intensity
Matthew Garrett23a98472011-03-22 16:30:26 -0700139 },
140 .set_brightness = nvidia_chipset_set_brightness,
Mario Schwalbec78a6282009-01-11 00:11:34 +0000141};
142
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700143static int __devinit apple_bl_add(struct acpi_device *dev)
Matthew Garrett7be35c72008-06-09 21:56:16 +0100144{
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500145 struct backlight_properties props;
Matthew Garrett23a98472011-03-22 16:30:26 -0700146 struct pci_dev *host;
Matthew Garrett99fd28e2011-03-22 16:30:27 -0700147 int intensity;
Matthew Garrett7be35c72008-06-09 21:56:16 +0100148
Matthew Garrett23a98472011-03-22 16:30:26 -0700149 host = pci_get_bus_and_slot(0, 0);
150
151 if (!host) {
152 printk(KERN_ERR DRIVER "unable to find PCI host\n");
153 return -ENODEV;
154 }
155
156 if (host->vendor == PCI_VENDOR_ID_INTEL)
157 hw_data = &intel_chipset_data;
158 else if (host->vendor == PCI_VENDOR_ID_NVIDIA)
159 hw_data = &nvidia_chipset_data;
160
161 pci_dev_put(host);
162
163 if (!hw_data) {
164 printk(KERN_ERR DRIVER "unknown hardware\n");
165 return -ENODEV;
166 }
167
Matthew Garrett99fd28e2011-03-22 16:30:27 -0700168 /* Check that the hardware responds - this may not work under EFI */
169
170 intensity = hw_data->backlight_ops.get_brightness(NULL);
171
172 if (!intensity) {
173 hw_data->set_brightness(1);
174 if (!hw_data->backlight_ops.get_brightness(NULL))
175 return -ENODEV;
176
177 hw_data->set_brightness(0);
178 }
179
Matthew Garrett23a98472011-03-22 16:30:26 -0700180 if (!request_region(hw_data->iostart, hw_data->iolen,
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700181 "Apple backlight"))
Matthew Garrett7be35c72008-06-09 21:56:16 +0100182 return -ENXIO;
183
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500184 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700185 props.type = BACKLIGHT_PLATFORM;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500186 props.max_brightness = 15;
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700187 apple_backlight_device = backlight_device_register("apple_backlight",
188 NULL, NULL, &hw_data->backlight_ops, &props);
Matthew Garrett23a98472011-03-22 16:30:26 -0700189
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700190 if (IS_ERR(apple_backlight_device)) {
Matthew Garrett23a98472011-03-22 16:30:26 -0700191 release_region(hw_data->iostart, hw_data->iolen);
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700192 return PTR_ERR(apple_backlight_device);
Matthew Garrett7be35c72008-06-09 21:56:16 +0100193 }
194
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700195 apple_backlight_device->props.brightness =
196 hw_data->backlight_ops.get_brightness(apple_backlight_device);
197 backlight_update_status(apple_backlight_device);
Matthew Garrett7be35c72008-06-09 21:56:16 +0100198
199 return 0;
200}
201
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700202static int __devexit apple_bl_remove(struct acpi_device *dev, int type)
Matthew Garrett7be35c72008-06-09 21:56:16 +0100203{
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700204 backlight_device_unregister(apple_backlight_device);
Matthew Garrett7be35c72008-06-09 21:56:16 +0100205
Matthew Garrett23a98472011-03-22 16:30:26 -0700206 release_region(hw_data->iostart, hw_data->iolen);
207 hw_data = NULL;
208 return 0;
Matthew Garrett7be35c72008-06-09 21:56:16 +0100209}
210
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700211static const struct acpi_device_id apple_bl_ids[] = {
Matthew Garrett23a98472011-03-22 16:30:26 -0700212 {"APP0002", 0},
213 {"", 0},
214};
215
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700216static struct acpi_driver apple_bl_driver = {
217 .name = "Apple backlight",
218 .ids = apple_bl_ids,
Matthew Garrett23a98472011-03-22 16:30:26 -0700219 .ops = {
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700220 .add = apple_bl_add,
221 .remove = apple_bl_remove,
Matthew Garrett23a98472011-03-22 16:30:26 -0700222 },
223};
224
Seth Forshee83e72dd2012-03-16 14:41:21 -0500225static atomic_t apple_bl_registered = ATOMIC_INIT(0);
226
227int apple_bl_register(void)
228{
229 if (atomic_xchg(&apple_bl_registered, 1) == 0)
230 return acpi_bus_register_driver(&apple_bl_driver);
231
232 return 0;
233}
234EXPORT_SYMBOL_GPL(apple_bl_register);
235
236void apple_bl_unregister(void)
237{
238 if (atomic_xchg(&apple_bl_registered, 0) == 1)
239 acpi_bus_unregister_driver(&apple_bl_driver);
240}
241EXPORT_SYMBOL_GPL(apple_bl_unregister);
242
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700243static int __init apple_bl_init(void)
Matthew Garrett23a98472011-03-22 16:30:26 -0700244{
Seth Forshee83e72dd2012-03-16 14:41:21 -0500245 return apple_bl_register();
Matthew Garrett23a98472011-03-22 16:30:26 -0700246}
247
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700248static void __exit apple_bl_exit(void)
Matthew Garrett23a98472011-03-22 16:30:26 -0700249{
Seth Forshee83e72dd2012-03-16 14:41:21 -0500250 apple_bl_unregister();
Matthew Garrett23a98472011-03-22 16:30:26 -0700251}
252
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700253module_init(apple_bl_init);
254module_exit(apple_bl_exit);
Matthew Garrett7be35c72008-06-09 21:56:16 +0100255
256MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700257MODULE_DESCRIPTION("Apple Backlight Driver");
Matthew Garrett7be35c72008-06-09 21:56:16 +0100258MODULE_LICENSE("GPL");
Matthew Garrett39b3dee2011-03-22 16:30:28 -0700259MODULE_DEVICE_TABLE(acpi, apple_bl_ids);
260MODULE_ALIAS("mbp_nvidia_bl");