blob: ce09b83244ac8693389498e237edcfd6d23520e7 [file] [log] [blame]
Matthew Garrett7be35c72008-06-09 21:56:16 +01001/*
2 * Backlight Driver for Nvidia 8600 in Macbook Pro
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
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/init.h>
22#include <linux/platform_device.h>
23#include <linux/backlight.h>
24#include <linux/err.h>
25#include <linux/dmi.h>
26#include <linux/io.h>
27
28static struct backlight_device *mbp_backlight_device;
29
Mario Schwalbec78a6282009-01-11 00:11:34 +000030/* Structure to be passed to the DMI_MATCH function. */
31struct dmi_match_data {
32 /* I/O resource to allocate. */
33 unsigned long iostart;
34 unsigned long iolen;
35 /* Backlight operations structure. */
36 struct backlight_ops backlight_ops;
Matthew Garrett7be35c72008-06-09 21:56:16 +010037};
38
Mario Schwalbec78a6282009-01-11 00:11:34 +000039/*
40 * Implementation for MacBooks with Intel chipset.
41 */
42static int intel_chipset_send_intensity(struct backlight_device *bd)
Matthew Garrett7be35c72008-06-09 21:56:16 +010043{
44 int intensity = bd->props.brightness;
45
46 outb(0x04 | (intensity << 4), 0xb3);
47 outb(0xbf, 0xb2);
Matthew Garrett7be35c72008-06-09 21:56:16 +010048 return 0;
49}
50
Mario Schwalbec78a6282009-01-11 00:11:34 +000051static int intel_chipset_get_intensity(struct backlight_device *bd)
Matthew Garrett7be35c72008-06-09 21:56:16 +010052{
53 outb(0x03, 0xb3);
54 outb(0xbf, 0xb2);
55 return inb(0xb3) >> 4;
56}
57
Mario Schwalbec78a6282009-01-11 00:11:34 +000058static const struct dmi_match_data intel_chipset_data = {
59 .iostart = 0xb2,
60 .iolen = 2,
61 .backlight_ops = {
62 .options = BL_CORE_SUSPENDRESUME,
63 .get_brightness = intel_chipset_get_intensity,
64 .update_status = intel_chipset_send_intensity,
65 }
66};
67
68/*
69 * Implementation for MacBooks with Nvidia chipset.
70 */
71static int nvidia_chipset_send_intensity(struct backlight_device *bd)
72{
73 int intensity = bd->props.brightness;
74
75 outb(0x04 | (intensity << 4), 0x52f);
76 outb(0xbf, 0x52e);
77 return 0;
78}
79
80static int nvidia_chipset_get_intensity(struct backlight_device *bd)
81{
82 outb(0x03, 0x52f);
83 outb(0xbf, 0x52e);
84 return inb(0x52f) >> 4;
85}
86
87static const struct dmi_match_data nvidia_chipset_data = {
88 .iostart = 0x52e,
89 .iolen = 2,
90 .backlight_ops = {
91 .options = BL_CORE_SUSPENDRESUME,
92 .get_brightness = nvidia_chipset_get_intensity,
93 .update_status = nvidia_chipset_send_intensity
94 }
95};
96
97/*
98 * DMI matching.
99 */
100static /* const */ struct dmi_match_data *driver_data;
101
102static int mbp_dmi_match(const struct dmi_system_id *id)
103{
104 driver_data = id->driver_data;
105
106 printk(KERN_INFO "mbp_nvidia_bl: %s detected\n", id->ident);
107 return 1;
108}
109
110static const struct dmi_system_id __initdata mbp_device_table[] = {
111 {
112 .callback = mbp_dmi_match,
113 .ident = "MacBookPro 3,1",
114 .matches = {
115 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
116 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3,1"),
117 },
118 .driver_data = (void *)&intel_chipset_data,
119 },
120 {
121 .callback = mbp_dmi_match,
122 .ident = "MacBookPro 3,2",
123 .matches = {
124 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
125 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3,2"),
126 },
127 .driver_data = (void *)&intel_chipset_data,
128 },
129 {
130 .callback = mbp_dmi_match,
131 .ident = "MacBookPro 4,1",
132 .matches = {
133 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
134 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro4,1"),
135 },
136 .driver_data = (void *)&intel_chipset_data,
137 },
138 {
139 .callback = mbp_dmi_match,
140 .ident = "MacBook 5,1",
141 .matches = {
142 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
143 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5,1"),
144 },
145 .driver_data = (void *)&nvidia_chipset_data,
146 },
147 {
148 .callback = mbp_dmi_match,
149 .ident = "MacBookAir 2,1",
150 .matches = {
151 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
152 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir2,1"),
153 },
154 .driver_data = (void *)&nvidia_chipset_data,
155 },
156 {
157 .callback = mbp_dmi_match,
158 .ident = "MacBookPro 5,1",
159 .matches = {
160 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
161 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,1"),
162 },
163 .driver_data = (void *)&nvidia_chipset_data,
164 },
165 { }
Matthew Garrett7be35c72008-06-09 21:56:16 +0100166};
167
168static int __init mbp_init(void)
169{
170 if (!dmi_check_system(mbp_device_table))
171 return -ENODEV;
172
Mario Schwalbec78a6282009-01-11 00:11:34 +0000173 if (!request_region(driver_data->iostart, driver_data->iolen,
174 "Macbook Pro backlight"))
Matthew Garrett7be35c72008-06-09 21:56:16 +0100175 return -ENXIO;
176
177 mbp_backlight_device = backlight_device_register("mbp_backlight",
Mario Schwalbec78a6282009-01-11 00:11:34 +0000178 NULL, NULL, &driver_data->backlight_ops);
Matthew Garrett7be35c72008-06-09 21:56:16 +0100179 if (IS_ERR(mbp_backlight_device)) {
Mario Schwalbec78a6282009-01-11 00:11:34 +0000180 release_region(driver_data->iostart, driver_data->iolen);
Matthew Garrett7be35c72008-06-09 21:56:16 +0100181 return PTR_ERR(mbp_backlight_device);
182 }
183
184 mbp_backlight_device->props.max_brightness = 15;
185 mbp_backlight_device->props.brightness =
Mario Schwalbec78a6282009-01-11 00:11:34 +0000186 driver_data->backlight_ops.get_brightness(mbp_backlight_device);
Matthew Garrett7be35c72008-06-09 21:56:16 +0100187 backlight_update_status(mbp_backlight_device);
188
189 return 0;
190}
191
192static void __exit mbp_exit(void)
193{
194 backlight_device_unregister(mbp_backlight_device);
195
Mario Schwalbec78a6282009-01-11 00:11:34 +0000196 release_region(driver_data->iostart, driver_data->iolen);
Matthew Garrett7be35c72008-06-09 21:56:16 +0100197}
198
199module_init(mbp_init);
200module_exit(mbp_exit);
201
202MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
203MODULE_DESCRIPTION("Nvidia-based Macbook Pro Backlight Driver");
204MODULE_LICENSE("GPL");
David Woodhouse239cfbd2008-09-16 16:25:24 -0700205MODULE_DEVICE_TABLE(dmi, mbp_device_table);