]>
Commit | Line | Data |
---|---|---|
917ee75a SF |
1 | /* |
2 | * Gmux driver for Apple laptops | |
3 | * | |
4 | * Copyright (C) Canonical Ltd. <seth.forshee@canonical.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | */ | |
10 | ||
11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
12 | ||
13 | #include <linux/module.h> | |
14 | #include <linux/kernel.h> | |
15 | #include <linux/init.h> | |
16 | #include <linux/backlight.h> | |
17 | #include <linux/acpi.h> | |
18 | #include <linux/pnp.h> | |
19 | #include <linux/apple_bl.h> | |
20 | #include <linux/slab.h> | |
21 | #include <acpi/video.h> | |
22 | #include <asm/io.h> | |
23 | ||
24 | struct apple_gmux_data { | |
25 | unsigned long iostart; | |
26 | unsigned long iolen; | |
27 | ||
28 | struct backlight_device *bdev; | |
29 | }; | |
30 | ||
31 | /* | |
32 | * gmux port offsets. Many of these are not yet used, but may be in the | |
33 | * future, and it's useful to have them documented here anyhow. | |
34 | */ | |
35 | #define GMUX_PORT_VERSION_MAJOR 0x04 | |
36 | #define GMUX_PORT_VERSION_MINOR 0x05 | |
37 | #define GMUX_PORT_VERSION_RELEASE 0x06 | |
38 | #define GMUX_PORT_SWITCH_DISPLAY 0x10 | |
39 | #define GMUX_PORT_SWITCH_GET_DISPLAY 0x11 | |
40 | #define GMUX_PORT_INTERRUPT_ENABLE 0x14 | |
41 | #define GMUX_PORT_INTERRUPT_STATUS 0x16 | |
42 | #define GMUX_PORT_SWITCH_DDC 0x28 | |
43 | #define GMUX_PORT_SWITCH_EXTERNAL 0x40 | |
44 | #define GMUX_PORT_SWITCH_GET_EXTERNAL 0x41 | |
45 | #define GMUX_PORT_DISCRETE_POWER 0x50 | |
46 | #define GMUX_PORT_MAX_BRIGHTNESS 0x70 | |
47 | #define GMUX_PORT_BRIGHTNESS 0x74 | |
48 | ||
49 | #define GMUX_MIN_IO_LEN (GMUX_PORT_BRIGHTNESS + 4) | |
50 | ||
51 | #define GMUX_INTERRUPT_ENABLE 0xff | |
52 | #define GMUX_INTERRUPT_DISABLE 0x00 | |
53 | ||
54 | #define GMUX_INTERRUPT_STATUS_ACTIVE 0 | |
55 | #define GMUX_INTERRUPT_STATUS_DISPLAY (1 << 0) | |
56 | #define GMUX_INTERRUPT_STATUS_POWER (1 << 2) | |
57 | #define GMUX_INTERRUPT_STATUS_HOTPLUG (1 << 3) | |
58 | ||
59 | #define GMUX_BRIGHTNESS_MASK 0x00ffffff | |
60 | #define GMUX_MAX_BRIGHTNESS GMUX_BRIGHTNESS_MASK | |
61 | ||
62 | static inline u8 gmux_read8(struct apple_gmux_data *gmux_data, int port) | |
63 | { | |
64 | return inb(gmux_data->iostart + port); | |
65 | } | |
66 | ||
67 | static inline void gmux_write8(struct apple_gmux_data *gmux_data, int port, | |
68 | u8 val) | |
69 | { | |
70 | outb(val, gmux_data->iostart + port); | |
71 | } | |
72 | ||
73 | static inline u32 gmux_read32(struct apple_gmux_data *gmux_data, int port) | |
74 | { | |
75 | return inl(gmux_data->iostart + port); | |
76 | } | |
77 | ||
7e30ed6b MG |
78 | static inline u32 gmux_write32(struct apple_gmux_data *gmux_data, int port, |
79 | u32 val) | |
80 | { | |
81 | int i; | |
82 | u8 tmpval; | |
83 | ||
84 | for (i = 0; i < 4; i++) { | |
85 | tmpval = (val >> (i * 8)) & 0xff; | |
86 | outb(tmpval, port + i); | |
87 | } | |
88 | } | |
89 | ||
917ee75a SF |
90 | static int gmux_get_brightness(struct backlight_device *bd) |
91 | { | |
92 | struct apple_gmux_data *gmux_data = bl_get_data(bd); | |
93 | return gmux_read32(gmux_data, GMUX_PORT_BRIGHTNESS) & | |
94 | GMUX_BRIGHTNESS_MASK; | |
95 | } | |
96 | ||
97 | static int gmux_update_status(struct backlight_device *bd) | |
98 | { | |
99 | struct apple_gmux_data *gmux_data = bl_get_data(bd); | |
100 | u32 brightness = bd->props.brightness; | |
101 | ||
96960880 | 102 | if (bd->props.state & BL_CORE_SUSPENDED) |
a2f01a89 | 103 | return 0; |
96960880 | 104 | |
7e30ed6b | 105 | gmux_write32(gmux_data, GMUX_PORT_BRIGHTNESS, brightness); |
917ee75a SF |
106 | |
107 | return 0; | |
108 | } | |
109 | ||
110 | static const struct backlight_ops gmux_bl_ops = { | |
96960880 | 111 | .options = BL_CORE_SUSPENDRESUME, |
917ee75a SF |
112 | .get_brightness = gmux_get_brightness, |
113 | .update_status = gmux_update_status, | |
114 | }; | |
115 | ||
116 | static int __devinit gmux_probe(struct pnp_dev *pnp, | |
117 | const struct pnp_device_id *id) | |
118 | { | |
119 | struct apple_gmux_data *gmux_data; | |
120 | struct resource *res; | |
121 | struct backlight_properties props; | |
122 | struct backlight_device *bdev; | |
123 | u8 ver_major, ver_minor, ver_release; | |
124 | int ret = -ENXIO; | |
125 | ||
126 | gmux_data = kzalloc(sizeof(*gmux_data), GFP_KERNEL); | |
127 | if (!gmux_data) | |
128 | return -ENOMEM; | |
129 | pnp_set_drvdata(pnp, gmux_data); | |
130 | ||
131 | res = pnp_get_resource(pnp, IORESOURCE_IO, 0); | |
132 | if (!res) { | |
133 | pr_err("Failed to find gmux I/O resource\n"); | |
134 | goto err_free; | |
135 | } | |
136 | ||
137 | gmux_data->iostart = res->start; | |
138 | gmux_data->iolen = res->end - res->start; | |
139 | ||
140 | if (gmux_data->iolen < GMUX_MIN_IO_LEN) { | |
141 | pr_err("gmux I/O region too small (%lu < %u)\n", | |
142 | gmux_data->iolen, GMUX_MIN_IO_LEN); | |
143 | goto err_free; | |
144 | } | |
145 | ||
146 | if (!request_region(gmux_data->iostart, gmux_data->iolen, | |
147 | "Apple gmux")) { | |
148 | pr_err("gmux I/O already in use\n"); | |
149 | goto err_free; | |
150 | } | |
151 | ||
152 | /* | |
153 | * On some machines the gmux is in ACPI even thought the machine | |
154 | * doesn't really have a gmux. Check for invalid version information | |
155 | * to detect this. | |
156 | */ | |
157 | ver_major = gmux_read8(gmux_data, GMUX_PORT_VERSION_MAJOR); | |
158 | ver_minor = gmux_read8(gmux_data, GMUX_PORT_VERSION_MINOR); | |
159 | ver_release = gmux_read8(gmux_data, GMUX_PORT_VERSION_RELEASE); | |
160 | if (ver_major == 0xff && ver_minor == 0xff && ver_release == 0xff) { | |
161 | pr_info("gmux device not present\n"); | |
162 | ret = -ENODEV; | |
163 | goto err_release; | |
164 | } | |
165 | ||
166 | pr_info("Found gmux version %d.%d.%d\n", ver_major, ver_minor, | |
167 | ver_release); | |
168 | ||
169 | memset(&props, 0, sizeof(props)); | |
170 | props.type = BACKLIGHT_PLATFORM; | |
171 | props.max_brightness = gmux_read32(gmux_data, GMUX_PORT_MAX_BRIGHTNESS); | |
172 | ||
173 | /* | |
174 | * Currently it's assumed that the maximum brightness is less than | |
175 | * 2^24 for compatibility with old gmux versions. Cap the max | |
176 | * brightness at this value, but print a warning if the hardware | |
177 | * reports something higher so that it can be fixed. | |
178 | */ | |
179 | if (WARN_ON(props.max_brightness > GMUX_MAX_BRIGHTNESS)) | |
180 | props.max_brightness = GMUX_MAX_BRIGHTNESS; | |
181 | ||
182 | bdev = backlight_device_register("gmux_backlight", &pnp->dev, | |
183 | gmux_data, &gmux_bl_ops, &props); | |
184 | if (IS_ERR(bdev)) { | |
185 | ret = PTR_ERR(bdev); | |
186 | goto err_release; | |
187 | } | |
188 | ||
189 | gmux_data->bdev = bdev; | |
190 | bdev->props.brightness = gmux_get_brightness(bdev); | |
191 | backlight_update_status(bdev); | |
192 | ||
193 | /* | |
194 | * The backlight situation on Macs is complicated. If the gmux is | |
195 | * present it's the best choice, because it always works for | |
196 | * backlight control and supports more levels than other options. | |
197 | * Disable the other backlight choices. | |
198 | */ | |
a60b2176 CC |
199 | acpi_video_dmi_promote_vendor(); |
200 | #ifdef CONFIG_ACPI_VIDEO | |
917ee75a | 201 | acpi_video_unregister(); |
a60b2176 | 202 | #endif |
917ee75a SF |
203 | apple_bl_unregister(); |
204 | ||
205 | return 0; | |
206 | ||
207 | err_release: | |
208 | release_region(gmux_data->iostart, gmux_data->iolen); | |
209 | err_free: | |
210 | kfree(gmux_data); | |
211 | return ret; | |
212 | } | |
213 | ||
214 | static void __devexit gmux_remove(struct pnp_dev *pnp) | |
215 | { | |
216 | struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp); | |
217 | ||
218 | backlight_device_unregister(gmux_data->bdev); | |
219 | release_region(gmux_data->iostart, gmux_data->iolen); | |
220 | kfree(gmux_data); | |
221 | ||
a60b2176 CC |
222 | acpi_video_dmi_demote_vendor(); |
223 | #ifdef CONFIG_ACPI_VIDEO | |
917ee75a | 224 | acpi_video_register(); |
a60b2176 | 225 | #endif |
917ee75a SF |
226 | apple_bl_register(); |
227 | } | |
228 | ||
229 | static const struct pnp_device_id gmux_device_ids[] = { | |
230 | {"APP000B", 0}, | |
231 | {"", 0} | |
232 | }; | |
233 | ||
234 | static struct pnp_driver gmux_pnp_driver = { | |
235 | .name = "apple-gmux", | |
236 | .probe = gmux_probe, | |
237 | .remove = __devexit_p(gmux_remove), | |
238 | .id_table = gmux_device_ids, | |
239 | }; | |
240 | ||
241 | static int __init apple_gmux_init(void) | |
242 | { | |
243 | return pnp_register_driver(&gmux_pnp_driver); | |
244 | } | |
245 | ||
246 | static void __exit apple_gmux_exit(void) | |
247 | { | |
248 | pnp_unregister_driver(&gmux_pnp_driver); | |
249 | } | |
250 | ||
251 | module_init(apple_gmux_init); | |
252 | module_exit(apple_gmux_exit); | |
253 | ||
254 | MODULE_AUTHOR("Seth Forshee <seth.forshee@canonical.com>"); | |
255 | MODULE_DESCRIPTION("Apple Gmux Driver"); | |
256 | MODULE_LICENSE("GPL"); | |
257 | MODULE_DEVICE_TABLE(pnp, gmux_device_ids); |